Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / media / firewire / firedtv-fw.c
blob8a8585261bb803794c42e5f794d1825ce6bb1599
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * FireDTV driver -- firewire I/O backend
4 */
6 #include <linux/device.h>
7 #include <linux/errno.h>
8 #include <linux/firewire.h>
9 #include <linux/firewire-constants.h>
10 #include <linux/kernel.h>
11 #include <linux/list.h>
12 #include <linux/mm.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/slab.h>
17 #include <linux/spinlock.h>
18 #include <linux/string.h>
19 #include <linux/types.h>
20 #include <linux/wait.h>
21 #include <linux/workqueue.h>
23 #include <asm/page.h>
25 #include <media/dvb_demux.h>
27 #include "firedtv.h"
29 static LIST_HEAD(node_list);
30 static DEFINE_SPINLOCK(node_list_lock);
32 static inline struct fw_device *device_of(struct firedtv *fdtv)
34 return fw_device(fdtv->device->parent);
37 static int node_req(struct firedtv *fdtv, u64 addr, void *data, size_t len,
38 int tcode)
40 struct fw_device *device = device_of(fdtv);
41 int rcode, generation = device->generation;
43 smp_rmb(); /* node_id vs. generation */
45 rcode = fw_run_transaction(device->card, tcode, device->node_id,
46 generation, device->max_speed, addr, data, len);
48 return rcode != RCODE_COMPLETE ? -EIO : 0;
51 int fdtv_lock(struct firedtv *fdtv, u64 addr, void *data)
53 return node_req(fdtv, addr, data, 8, TCODE_LOCK_COMPARE_SWAP);
56 int fdtv_read(struct firedtv *fdtv, u64 addr, void *data)
58 return node_req(fdtv, addr, data, 4, TCODE_READ_QUADLET_REQUEST);
61 int fdtv_write(struct firedtv *fdtv, u64 addr, void *data, size_t len)
63 return node_req(fdtv, addr, data, len, TCODE_WRITE_BLOCK_REQUEST);
66 #define ISO_HEADER_SIZE 4
67 #define CIP_HEADER_SIZE 8
68 #define MPEG2_TS_HEADER_SIZE 4
69 #define MPEG2_TS_SOURCE_PACKET_SIZE (4 + 188)
71 #define MAX_PACKET_SIZE 1024 /* 776, rounded up to 2^n */
72 #define PACKETS_PER_PAGE (PAGE_SIZE / MAX_PACKET_SIZE)
73 #define N_PACKETS 64 /* buffer size */
74 #define N_PAGES DIV_ROUND_UP(N_PACKETS, PACKETS_PER_PAGE)
75 #define IRQ_INTERVAL 16
77 struct fdtv_ir_context {
78 struct fw_iso_context *context;
79 struct fw_iso_buffer buffer;
80 int interrupt_packet;
81 int current_packet;
82 char *pages[N_PAGES];
85 static int queue_iso(struct fdtv_ir_context *ctx, int index)
87 struct fw_iso_packet p;
89 p.payload_length = MAX_PACKET_SIZE;
90 p.interrupt = !(++ctx->interrupt_packet & (IRQ_INTERVAL - 1));
91 p.skip = 0;
92 p.header_length = ISO_HEADER_SIZE;
94 return fw_iso_context_queue(ctx->context, &p, &ctx->buffer,
95 index * MAX_PACKET_SIZE);
98 static void handle_iso(struct fw_iso_context *context, u32 cycle,
99 size_t header_length, void *header, void *data)
101 struct firedtv *fdtv = data;
102 struct fdtv_ir_context *ctx = fdtv->ir_context;
103 __be32 *h, *h_end;
104 int length, err, i = ctx->current_packet;
105 char *p, *p_end;
107 for (h = header, h_end = h + header_length / 4; h < h_end; h++) {
108 length = be32_to_cpup(h) >> 16;
109 if (unlikely(length > MAX_PACKET_SIZE)) {
110 dev_err(fdtv->device, "length = %d\n", length);
111 length = MAX_PACKET_SIZE;
114 p = ctx->pages[i / PACKETS_PER_PAGE]
115 + (i % PACKETS_PER_PAGE) * MAX_PACKET_SIZE;
116 p_end = p + length;
118 for (p += CIP_HEADER_SIZE + MPEG2_TS_HEADER_SIZE; p < p_end;
119 p += MPEG2_TS_SOURCE_PACKET_SIZE)
120 dvb_dmx_swfilter_packets(&fdtv->demux, p, 1);
122 err = queue_iso(ctx, i);
123 if (unlikely(err))
124 dev_err(fdtv->device, "requeue failed\n");
126 i = (i + 1) & (N_PACKETS - 1);
128 fw_iso_context_queue_flush(ctx->context);
129 ctx->current_packet = i;
132 int fdtv_start_iso(struct firedtv *fdtv)
134 struct fdtv_ir_context *ctx;
135 struct fw_device *device = device_of(fdtv);
136 int i, err;
138 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
139 if (!ctx)
140 return -ENOMEM;
142 ctx->context = fw_iso_context_create(device->card,
143 FW_ISO_CONTEXT_RECEIVE, fdtv->isochannel,
144 device->max_speed, ISO_HEADER_SIZE, handle_iso, fdtv);
145 if (IS_ERR(ctx->context)) {
146 err = PTR_ERR(ctx->context);
147 goto fail_free;
150 err = fw_iso_buffer_init(&ctx->buffer, device->card,
151 N_PAGES, DMA_FROM_DEVICE);
152 if (err)
153 goto fail_context_destroy;
155 ctx->interrupt_packet = 0;
156 ctx->current_packet = 0;
158 for (i = 0; i < N_PAGES; i++)
159 ctx->pages[i] = page_address(ctx->buffer.pages[i]);
161 for (i = 0; i < N_PACKETS; i++) {
162 err = queue_iso(ctx, i);
163 if (err)
164 goto fail;
167 err = fw_iso_context_start(ctx->context, -1, 0,
168 FW_ISO_CONTEXT_MATCH_ALL_TAGS);
169 if (err)
170 goto fail;
172 fdtv->ir_context = ctx;
174 return 0;
175 fail:
176 fw_iso_buffer_destroy(&ctx->buffer, device->card);
177 fail_context_destroy:
178 fw_iso_context_destroy(ctx->context);
179 fail_free:
180 kfree(ctx);
182 return err;
185 void fdtv_stop_iso(struct firedtv *fdtv)
187 struct fdtv_ir_context *ctx = fdtv->ir_context;
189 fw_iso_context_stop(ctx->context);
190 fw_iso_buffer_destroy(&ctx->buffer, device_of(fdtv)->card);
191 fw_iso_context_destroy(ctx->context);
192 kfree(ctx);
195 static void handle_fcp(struct fw_card *card, struct fw_request *request,
196 int tcode, int destination, int source, int generation,
197 unsigned long long offset, void *payload, size_t length,
198 void *callback_data)
200 struct firedtv *f, *fdtv = NULL;
201 struct fw_device *device;
202 unsigned long flags;
203 int su;
205 if (length < 2 || (((u8 *)payload)[0] & 0xf0) != 0)
206 return;
208 su = ((u8 *)payload)[1] & 0x7;
210 spin_lock_irqsave(&node_list_lock, flags);
211 list_for_each_entry(f, &node_list, list) {
212 device = device_of(f);
213 if (device->generation != generation)
214 continue;
216 smp_rmb(); /* node_id vs. generation */
218 if (device->card == card &&
219 device->node_id == source &&
220 (f->subunit == su || (f->subunit == 0 && su == 0x7))) {
221 fdtv = f;
222 break;
225 spin_unlock_irqrestore(&node_list_lock, flags);
227 if (fdtv)
228 avc_recv(fdtv, payload, length);
231 static struct fw_address_handler fcp_handler = {
232 .length = CSR_FCP_END - CSR_FCP_RESPONSE,
233 .address_callback = handle_fcp,
236 static const struct fw_address_region fcp_region = {
237 .start = CSR_REGISTER_BASE + CSR_FCP_RESPONSE,
238 .end = CSR_REGISTER_BASE + CSR_FCP_END,
241 static const char * const model_names[] = {
242 [FIREDTV_UNKNOWN] = "unknown type",
243 [FIREDTV_DVB_S] = "FireDTV S/CI",
244 [FIREDTV_DVB_C] = "FireDTV C/CI",
245 [FIREDTV_DVB_T] = "FireDTV T/CI",
246 [FIREDTV_DVB_S2] = "FireDTV S2 ",
249 /* Adjust the template string if models with longer names appear. */
250 #define MAX_MODEL_NAME_LEN sizeof("FireDTV ????")
252 static int node_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
254 struct firedtv *fdtv;
255 char name[MAX_MODEL_NAME_LEN];
256 int name_len, i, err;
258 fdtv = kzalloc(sizeof(*fdtv), GFP_KERNEL);
259 if (!fdtv)
260 return -ENOMEM;
262 dev_set_drvdata(&unit->device, fdtv);
263 fdtv->device = &unit->device;
264 fdtv->isochannel = -1;
265 fdtv->voltage = 0xff;
266 fdtv->tone = 0xff;
268 mutex_init(&fdtv->avc_mutex);
269 init_waitqueue_head(&fdtv->avc_wait);
270 mutex_init(&fdtv->demux_mutex);
271 INIT_WORK(&fdtv->remote_ctrl_work, avc_remote_ctrl_work);
273 name_len = fw_csr_string(unit->directory, CSR_MODEL,
274 name, sizeof(name));
275 if (name_len < 0) {
276 err = name_len;
277 goto fail_free;
279 for (i = ARRAY_SIZE(model_names); --i; )
280 if (strlen(model_names[i]) <= name_len &&
281 strncmp(name, model_names[i], name_len) == 0)
282 break;
283 fdtv->type = i;
285 err = fdtv_register_rc(fdtv, &unit->device);
286 if (err)
287 goto fail_free;
289 spin_lock_irq(&node_list_lock);
290 list_add_tail(&fdtv->list, &node_list);
291 spin_unlock_irq(&node_list_lock);
293 err = avc_identify_subunit(fdtv);
294 if (err)
295 goto fail;
297 err = fdtv_dvb_register(fdtv, model_names[fdtv->type]);
298 if (err)
299 goto fail;
301 avc_register_remote_control(fdtv);
303 return 0;
304 fail:
305 spin_lock_irq(&node_list_lock);
306 list_del(&fdtv->list);
307 spin_unlock_irq(&node_list_lock);
308 fdtv_unregister_rc(fdtv);
309 fail_free:
310 kfree(fdtv);
312 return err;
315 static void node_remove(struct fw_unit *unit)
317 struct firedtv *fdtv = dev_get_drvdata(&unit->device);
319 fdtv_dvb_unregister(fdtv);
321 spin_lock_irq(&node_list_lock);
322 list_del(&fdtv->list);
323 spin_unlock_irq(&node_list_lock);
325 fdtv_unregister_rc(fdtv);
327 kfree(fdtv);
330 static void node_update(struct fw_unit *unit)
332 struct firedtv *fdtv = dev_get_drvdata(&unit->device);
334 if (fdtv->isochannel >= 0)
335 cmp_establish_pp_connection(fdtv, fdtv->subunit,
336 fdtv->isochannel);
339 #define MATCH_FLAGS (IEEE1394_MATCH_VENDOR_ID | IEEE1394_MATCH_MODEL_ID | \
340 IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION)
342 #define DIGITAL_EVERYWHERE_OUI 0x001287
343 #define AVC_UNIT_SPEC_ID_ENTRY 0x00a02d
344 #define AVC_SW_VERSION_ENTRY 0x010001
346 static const struct ieee1394_device_id fdtv_id_table[] = {
348 /* FloppyDTV S/CI and FloppyDTV S2 */
349 .match_flags = MATCH_FLAGS,
350 .vendor_id = DIGITAL_EVERYWHERE_OUI,
351 .model_id = 0x000024,
352 .specifier_id = AVC_UNIT_SPEC_ID_ENTRY,
353 .version = AVC_SW_VERSION_ENTRY,
354 }, {
355 /* FloppyDTV T/CI */
356 .match_flags = MATCH_FLAGS,
357 .vendor_id = DIGITAL_EVERYWHERE_OUI,
358 .model_id = 0x000025,
359 .specifier_id = AVC_UNIT_SPEC_ID_ENTRY,
360 .version = AVC_SW_VERSION_ENTRY,
361 }, {
362 /* FloppyDTV C/CI */
363 .match_flags = MATCH_FLAGS,
364 .vendor_id = DIGITAL_EVERYWHERE_OUI,
365 .model_id = 0x000026,
366 .specifier_id = AVC_UNIT_SPEC_ID_ENTRY,
367 .version = AVC_SW_VERSION_ENTRY,
368 }, {
369 /* FireDTV S/CI and FloppyDTV S2 */
370 .match_flags = MATCH_FLAGS,
371 .vendor_id = DIGITAL_EVERYWHERE_OUI,
372 .model_id = 0x000034,
373 .specifier_id = AVC_UNIT_SPEC_ID_ENTRY,
374 .version = AVC_SW_VERSION_ENTRY,
375 }, {
376 /* FireDTV T/CI */
377 .match_flags = MATCH_FLAGS,
378 .vendor_id = DIGITAL_EVERYWHERE_OUI,
379 .model_id = 0x000035,
380 .specifier_id = AVC_UNIT_SPEC_ID_ENTRY,
381 .version = AVC_SW_VERSION_ENTRY,
382 }, {
383 /* FireDTV C/CI */
384 .match_flags = MATCH_FLAGS,
385 .vendor_id = DIGITAL_EVERYWHERE_OUI,
386 .model_id = 0x000036,
387 .specifier_id = AVC_UNIT_SPEC_ID_ENTRY,
388 .version = AVC_SW_VERSION_ENTRY,
389 }, {}
391 MODULE_DEVICE_TABLE(ieee1394, fdtv_id_table);
393 static struct fw_driver fdtv_driver = {
394 .driver = {
395 .owner = THIS_MODULE,
396 .name = "firedtv",
397 .bus = &fw_bus_type,
399 .probe = node_probe,
400 .update = node_update,
401 .remove = node_remove,
402 .id_table = fdtv_id_table,
405 static int __init fdtv_init(void)
407 int ret;
409 ret = fw_core_add_address_handler(&fcp_handler, &fcp_region);
410 if (ret < 0)
411 return ret;
413 ret = driver_register(&fdtv_driver.driver);
414 if (ret < 0)
415 fw_core_remove_address_handler(&fcp_handler);
417 return ret;
420 static void __exit fdtv_exit(void)
422 driver_unregister(&fdtv_driver.driver);
423 fw_core_remove_address_handler(&fcp_handler);
426 module_init(fdtv_init);
427 module_exit(fdtv_exit);
429 MODULE_AUTHOR("Andreas Monitzer <andy@monitzer.com>");
430 MODULE_AUTHOR("Ben Backx <ben@bbackx.com>");
431 MODULE_DESCRIPTION("FireDTV DVB Driver");
432 MODULE_LICENSE("GPL");
433 MODULE_SUPPORTED_DEVICE("FireDTV DVB");