blob: 247f0e7cb5f7f6975857534a76f12aab2a53b005 [file] [log] [blame]
Stefan Richter87918332009-11-08 18:30:54 -03001/*
2 * FireDTV driver -- firewire I/O backend
3 */
4
5#include <linux/device.h>
6#include <linux/errno.h>
7#include <linux/firewire.h>
8#include <linux/firewire-constants.h>
Stefan Richter87918332009-11-08 18:30:54 -03009#include <linux/kernel.h>
10#include <linux/list.h>
Stefan Richtera8aeb782009-11-18 16:00:55 -030011#include <linux/mm.h>
Stefan Richter92374e82011-02-06 11:41:44 -030012#include <linux/mod_devicetable.h>
13#include <linux/module.h>
14#include <linux/mutex.h>
Stefan Richter87918332009-11-08 18:30:54 -030015#include <linux/slab.h>
16#include <linux/spinlock.h>
Stefan Richter92374e82011-02-06 11:41:44 -030017#include <linux/string.h>
Stefan Richter87918332009-11-08 18:30:54 -030018#include <linux/types.h>
Stefan Richter92374e82011-02-06 11:41:44 -030019#include <linux/wait.h>
20#include <linux/workqueue.h>
Stefan Richter87918332009-11-08 18:30:54 -030021
22#include <asm/page.h>
23
24#include <dvb_demux.h>
25
26#include "firedtv.h"
27
28static LIST_HEAD(node_list);
29static DEFINE_SPINLOCK(node_list_lock);
30
31static inline struct fw_device *device_of(struct firedtv *fdtv)
32{
33 return fw_device(fdtv->device->parent);
34}
35
36static int node_req(struct firedtv *fdtv, u64 addr, void *data, size_t len,
37 int tcode)
38{
39 struct fw_device *device = device_of(fdtv);
40 int rcode, generation = device->generation;
41
42 smp_rmb(); /* node_id vs. generation */
43
44 rcode = fw_run_transaction(device->card, tcode, device->node_id,
45 generation, device->max_speed, addr, data, len);
46
47 return rcode != RCODE_COMPLETE ? -EIO : 0;
48}
49
Stefan Richter92374e82011-02-06 11:41:44 -030050int fdtv_lock(struct firedtv *fdtv, u64 addr, void *data)
Stefan Richter87918332009-11-08 18:30:54 -030051{
52 return node_req(fdtv, addr, data, 8, TCODE_LOCK_COMPARE_SWAP);
53}
54
Stefan Richter92374e82011-02-06 11:41:44 -030055int fdtv_read(struct firedtv *fdtv, u64 addr, void *data)
Stefan Richter87918332009-11-08 18:30:54 -030056{
Stefan Richter53756592009-11-18 16:01:34 -030057 return node_req(fdtv, addr, data, 4, TCODE_READ_QUADLET_REQUEST);
Stefan Richter87918332009-11-08 18:30:54 -030058}
59
Stefan Richter92374e82011-02-06 11:41:44 -030060int fdtv_write(struct firedtv *fdtv, u64 addr, void *data, size_t len)
Stefan Richter87918332009-11-08 18:30:54 -030061{
62 return node_req(fdtv, addr, data, len, TCODE_WRITE_BLOCK_REQUEST);
63}
64
65#define ISO_HEADER_SIZE 4
66#define CIP_HEADER_SIZE 8
67#define MPEG2_TS_HEADER_SIZE 4
68#define MPEG2_TS_SOURCE_PACKET_SIZE (4 + 188)
69
70#define MAX_PACKET_SIZE 1024 /* 776, rounded up to 2^n */
71#define PACKETS_PER_PAGE (PAGE_SIZE / MAX_PACKET_SIZE)
72#define N_PACKETS 64 /* buffer size */
73#define N_PAGES DIV_ROUND_UP(N_PACKETS, PACKETS_PER_PAGE)
74#define IRQ_INTERVAL 16
75
Stefan Richter92374e82011-02-06 11:41:44 -030076struct fdtv_ir_context {
Stefan Richter87918332009-11-08 18:30:54 -030077 struct fw_iso_context *context;
78 struct fw_iso_buffer buffer;
79 int interrupt_packet;
80 int current_packet;
Stefan Richtera8aeb782009-11-18 16:00:55 -030081 char *pages[N_PAGES];
Stefan Richter87918332009-11-08 18:30:54 -030082};
83
Stefan Richter92374e82011-02-06 11:41:44 -030084static int queue_iso(struct fdtv_ir_context *ctx, int index)
Stefan Richter87918332009-11-08 18:30:54 -030085{
86 struct fw_iso_packet p;
Stefan Richter87918332009-11-08 18:30:54 -030087
88 p.payload_length = MAX_PACKET_SIZE;
Stefan Richterb1d33f42009-11-18 16:01:14 -030089 p.interrupt = !(++ctx->interrupt_packet & (IRQ_INTERVAL - 1));
Stefan Richter87918332009-11-08 18:30:54 -030090 p.skip = 0;
91 p.header_length = ISO_HEADER_SIZE;
92
Stefan Richterb1d33f42009-11-18 16:01:14 -030093 return fw_iso_context_queue(ctx->context, &p, &ctx->buffer,
94 index * MAX_PACKET_SIZE);
Stefan Richter87918332009-11-08 18:30:54 -030095}
96
97static void handle_iso(struct fw_iso_context *context, u32 cycle,
98 size_t header_length, void *header, void *data)
99{
100 struct firedtv *fdtv = data;
Stefan Richter92374e82011-02-06 11:41:44 -0300101 struct fdtv_ir_context *ctx = fdtv->ir_context;
Stefan Richter87918332009-11-08 18:30:54 -0300102 __be32 *h, *h_end;
Stefan Richtera8aeb782009-11-18 16:00:55 -0300103 int length, err, i = ctx->current_packet;
Stefan Richter87918332009-11-08 18:30:54 -0300104 char *p, *p_end;
105
106 for (h = header, h_end = h + header_length / 4; h < h_end; h++) {
107 length = be32_to_cpup(h) >> 16;
108 if (unlikely(length > MAX_PACKET_SIZE)) {
109 dev_err(fdtv->device, "length = %d\n", length);
110 length = MAX_PACKET_SIZE;
111 }
112
Stefan Richtera8aeb782009-11-18 16:00:55 -0300113 p = ctx->pages[i / PACKETS_PER_PAGE]
114 + (i % PACKETS_PER_PAGE) * MAX_PACKET_SIZE;
Stefan Richter87918332009-11-08 18:30:54 -0300115 p_end = p + length;
116
117 for (p += CIP_HEADER_SIZE + MPEG2_TS_HEADER_SIZE; p < p_end;
118 p += MPEG2_TS_SOURCE_PACKET_SIZE)
119 dvb_dmx_swfilter_packets(&fdtv->demux, p, 1);
120
121 err = queue_iso(ctx, i);
122 if (unlikely(err))
123 dev_err(fdtv->device, "requeue failed\n");
124
125 i = (i + 1) & (N_PACKETS - 1);
126 }
Clemens Ladisch13882a82011-05-02 09:33:56 +0200127 fw_iso_context_queue_flush(ctx->context);
Stefan Richter87918332009-11-08 18:30:54 -0300128 ctx->current_packet = i;
129}
130
Stefan Richter92374e82011-02-06 11:41:44 -0300131int fdtv_start_iso(struct firedtv *fdtv)
Stefan Richter87918332009-11-08 18:30:54 -0300132{
Stefan Richter92374e82011-02-06 11:41:44 -0300133 struct fdtv_ir_context *ctx;
Stefan Richter87918332009-11-08 18:30:54 -0300134 struct fw_device *device = device_of(fdtv);
Stefan Richtera8aeb782009-11-18 16:00:55 -0300135 int i, err;
Stefan Richter87918332009-11-08 18:30:54 -0300136
137 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
138 if (!ctx)
139 return -ENOMEM;
140
141 ctx->context = fw_iso_context_create(device->card,
142 FW_ISO_CONTEXT_RECEIVE, fdtv->isochannel,
143 device->max_speed, ISO_HEADER_SIZE, handle_iso, fdtv);
144 if (IS_ERR(ctx->context)) {
145 err = PTR_ERR(ctx->context);
146 goto fail_free;
147 }
148
149 err = fw_iso_buffer_init(&ctx->buffer, device->card,
150 N_PAGES, DMA_FROM_DEVICE);
151 if (err)
152 goto fail_context_destroy;
153
Stefan Richterb1d33f42009-11-18 16:01:14 -0300154 ctx->interrupt_packet = 0;
Stefan Richter87918332009-11-08 18:30:54 -0300155 ctx->current_packet = 0;
156
Stefan Richtera8aeb782009-11-18 16:00:55 -0300157 for (i = 0; i < N_PAGES; i++)
158 ctx->pages[i] = page_address(ctx->buffer.pages[i]);
Stefan Richter87918332009-11-08 18:30:54 -0300159
160 for (i = 0; i < N_PACKETS; i++) {
161 err = queue_iso(ctx, i);
162 if (err)
163 goto fail;
164 }
165
166 err = fw_iso_context_start(ctx->context, -1, 0,
167 FW_ISO_CONTEXT_MATCH_ALL_TAGS);
168 if (err)
169 goto fail;
170
Stefan Richter92374e82011-02-06 11:41:44 -0300171 fdtv->ir_context = ctx;
Stefan Richter87918332009-11-08 18:30:54 -0300172
173 return 0;
174fail:
175 fw_iso_buffer_destroy(&ctx->buffer, device->card);
176fail_context_destroy:
177 fw_iso_context_destroy(ctx->context);
178fail_free:
179 kfree(ctx);
180
181 return err;
182}
183
Stefan Richter92374e82011-02-06 11:41:44 -0300184void fdtv_stop_iso(struct firedtv *fdtv)
Stefan Richter87918332009-11-08 18:30:54 -0300185{
Stefan Richter92374e82011-02-06 11:41:44 -0300186 struct fdtv_ir_context *ctx = fdtv->ir_context;
Stefan Richter87918332009-11-08 18:30:54 -0300187
188 fw_iso_context_stop(ctx->context);
189 fw_iso_buffer_destroy(&ctx->buffer, device_of(fdtv)->card);
190 fw_iso_context_destroy(ctx->context);
191 kfree(ctx);
192}
193
Stefan Richter87918332009-11-08 18:30:54 -0300194static void handle_fcp(struct fw_card *card, struct fw_request *request,
195 int tcode, int destination, int source, int generation,
Stefan Richter33e553f2010-06-20 22:50:35 +0200196 unsigned long long offset, void *payload, size_t length,
197 void *callback_data)
Stefan Richter87918332009-11-08 18:30:54 -0300198{
199 struct firedtv *f, *fdtv = NULL;
200 struct fw_device *device;
201 unsigned long flags;
202 int su;
203
Clemens Ladischdb5d2472009-12-24 12:05:58 +0100204 if (length < 2 || (((u8 *)payload)[0] & 0xf0) != 0)
Stefan Richter87918332009-11-08 18:30:54 -0300205 return;
Stefan Richter87918332009-11-08 18:30:54 -0300206
207 su = ((u8 *)payload)[1] & 0x7;
208
209 spin_lock_irqsave(&node_list_lock, flags);
210 list_for_each_entry(f, &node_list, list) {
211 device = device_of(f);
212 if (device->generation != generation)
213 continue;
214
215 smp_rmb(); /* node_id vs. generation */
216
217 if (device->card == card &&
218 device->node_id == source &&
219 (f->subunit == su || (f->subunit == 0 && su == 0x7))) {
220 fdtv = f;
221 break;
222 }
223 }
224 spin_unlock_irqrestore(&node_list_lock, flags);
225
Clemens Ladischdb5d2472009-12-24 12:05:58 +0100226 if (fdtv)
Stefan Richter87918332009-11-08 18:30:54 -0300227 avc_recv(fdtv, payload, length);
Stefan Richter87918332009-11-08 18:30:54 -0300228}
229
230static struct fw_address_handler fcp_handler = {
231 .length = CSR_FCP_END - CSR_FCP_RESPONSE,
232 .address_callback = handle_fcp,
233};
234
235static const struct fw_address_region fcp_region = {
236 .start = CSR_REGISTER_BASE + CSR_FCP_RESPONSE,
237 .end = CSR_REGISTER_BASE + CSR_FCP_END,
238};
239
Stefan Richter92374e82011-02-06 11:41:44 -0300240static const char * const model_names[] = {
241 [FIREDTV_UNKNOWN] = "unknown type",
242 [FIREDTV_DVB_S] = "FireDTV S/CI",
243 [FIREDTV_DVB_C] = "FireDTV C/CI",
244 [FIREDTV_DVB_T] = "FireDTV T/CI",
245 [FIREDTV_DVB_S2] = "FireDTV S2 ",
246};
247
Stefan Richter87918332009-11-08 18:30:54 -0300248/* Adjust the template string if models with longer names appear. */
Clemens Ladisch1f8fef72009-12-24 11:59:57 +0100249#define MAX_MODEL_NAME_LEN sizeof("FireDTV ????")
Stefan Richter87918332009-11-08 18:30:54 -0300250
Stefan Richter94a87152013-06-09 18:15:00 +0200251static int node_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
Stefan Richter87918332009-11-08 18:30:54 -0300252{
253 struct firedtv *fdtv;
Clemens Ladisch1f8fef72009-12-24 11:59:57 +0100254 char name[MAX_MODEL_NAME_LEN];
Stefan Richter92374e82011-02-06 11:41:44 -0300255 int name_len, i, err;
256
257 fdtv = kzalloc(sizeof(*fdtv), GFP_KERNEL);
258 if (!fdtv)
259 return -ENOMEM;
260
Stefan Richter94a87152013-06-09 18:15:00 +0200261 dev_set_drvdata(&unit->device, fdtv);
262 fdtv->device = &unit->device;
Stefan Richter92374e82011-02-06 11:41:44 -0300263 fdtv->isochannel = -1;
264 fdtv->voltage = 0xff;
265 fdtv->tone = 0xff;
266
267 mutex_init(&fdtv->avc_mutex);
268 init_waitqueue_head(&fdtv->avc_wait);
269 mutex_init(&fdtv->demux_mutex);
270 INIT_WORK(&fdtv->remote_ctrl_work, avc_remote_ctrl_work);
Stefan Richter87918332009-11-08 18:30:54 -0300271
Stefan Richter94a87152013-06-09 18:15:00 +0200272 name_len = fw_csr_string(unit->directory, CSR_MODEL,
Clemens Ladisch1f8fef72009-12-24 11:59:57 +0100273 name, sizeof(name));
Stefan Richter92374e82011-02-06 11:41:44 -0300274 for (i = ARRAY_SIZE(model_names); --i; )
275 if (strlen(model_names[i]) <= name_len &&
276 strncmp(name, model_names[i], name_len) == 0)
277 break;
278 fdtv->type = i;
Stefan Richter87918332009-11-08 18:30:54 -0300279
Stefan Richter94a87152013-06-09 18:15:00 +0200280 err = fdtv_register_rc(fdtv, &unit->device);
Stefan Richter87918332009-11-08 18:30:54 -0300281 if (err)
282 goto fail_free;
283
284 spin_lock_irq(&node_list_lock);
285 list_add_tail(&fdtv->list, &node_list);
286 spin_unlock_irq(&node_list_lock);
287
288 err = avc_identify_subunit(fdtv);
289 if (err)
290 goto fail;
291
Stefan Richter92374e82011-02-06 11:41:44 -0300292 err = fdtv_dvb_register(fdtv, model_names[fdtv->type]);
Stefan Richter87918332009-11-08 18:30:54 -0300293 if (err)
294 goto fail;
295
296 avc_register_remote_control(fdtv);
297
298 return 0;
299fail:
300 spin_lock_irq(&node_list_lock);
301 list_del(&fdtv->list);
302 spin_unlock_irq(&node_list_lock);
303 fdtv_unregister_rc(fdtv);
304fail_free:
305 kfree(fdtv);
306
307 return err;
308}
309
Stefan Richter94a87152013-06-09 18:15:00 +0200310static void node_remove(struct fw_unit *unit)
Stefan Richter87918332009-11-08 18:30:54 -0300311{
Stefan Richter94a87152013-06-09 18:15:00 +0200312 struct firedtv *fdtv = dev_get_drvdata(&unit->device);
Stefan Richter87918332009-11-08 18:30:54 -0300313
314 fdtv_dvb_unregister(fdtv);
315
316 spin_lock_irq(&node_list_lock);
317 list_del(&fdtv->list);
318 spin_unlock_irq(&node_list_lock);
319
320 fdtv_unregister_rc(fdtv);
321
322 kfree(fdtv);
Stefan Richter87918332009-11-08 18:30:54 -0300323}
324
325static void node_update(struct fw_unit *unit)
326{
327 struct firedtv *fdtv = dev_get_drvdata(&unit->device);
328
329 if (fdtv->isochannel >= 0)
330 cmp_establish_pp_connection(fdtv, fdtv->subunit,
331 fdtv->isochannel);
332}
333
Stefan Richter92374e82011-02-06 11:41:44 -0300334#define MATCH_FLAGS (IEEE1394_MATCH_VENDOR_ID | IEEE1394_MATCH_MODEL_ID | \
335 IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION)
336
337#define DIGITAL_EVERYWHERE_OUI 0x001287
338#define AVC_UNIT_SPEC_ID_ENTRY 0x00a02d
339#define AVC_SW_VERSION_ENTRY 0x010001
340
341static const struct ieee1394_device_id fdtv_id_table[] = {
342 {
343 /* FloppyDTV S/CI and FloppyDTV S2 */
344 .match_flags = MATCH_FLAGS,
345 .vendor_id = DIGITAL_EVERYWHERE_OUI,
346 .model_id = 0x000024,
347 .specifier_id = AVC_UNIT_SPEC_ID_ENTRY,
348 .version = AVC_SW_VERSION_ENTRY,
349 }, {
350 /* FloppyDTV T/CI */
351 .match_flags = MATCH_FLAGS,
352 .vendor_id = DIGITAL_EVERYWHERE_OUI,
353 .model_id = 0x000025,
354 .specifier_id = AVC_UNIT_SPEC_ID_ENTRY,
355 .version = AVC_SW_VERSION_ENTRY,
356 }, {
357 /* FloppyDTV C/CI */
358 .match_flags = MATCH_FLAGS,
359 .vendor_id = DIGITAL_EVERYWHERE_OUI,
360 .model_id = 0x000026,
361 .specifier_id = AVC_UNIT_SPEC_ID_ENTRY,
362 .version = AVC_SW_VERSION_ENTRY,
363 }, {
364 /* FireDTV S/CI and FloppyDTV S2 */
365 .match_flags = MATCH_FLAGS,
366 .vendor_id = DIGITAL_EVERYWHERE_OUI,
367 .model_id = 0x000034,
368 .specifier_id = AVC_UNIT_SPEC_ID_ENTRY,
369 .version = AVC_SW_VERSION_ENTRY,
370 }, {
371 /* FireDTV T/CI */
372 .match_flags = MATCH_FLAGS,
373 .vendor_id = DIGITAL_EVERYWHERE_OUI,
374 .model_id = 0x000035,
375 .specifier_id = AVC_UNIT_SPEC_ID_ENTRY,
376 .version = AVC_SW_VERSION_ENTRY,
377 }, {
378 /* FireDTV C/CI */
379 .match_flags = MATCH_FLAGS,
380 .vendor_id = DIGITAL_EVERYWHERE_OUI,
381 .model_id = 0x000036,
382 .specifier_id = AVC_UNIT_SPEC_ID_ENTRY,
383 .version = AVC_SW_VERSION_ENTRY,
384 }, {}
385};
386MODULE_DEVICE_TABLE(ieee1394, fdtv_id_table);
387
Stefan Richter87918332009-11-08 18:30:54 -0300388static struct fw_driver fdtv_driver = {
389 .driver = {
390 .owner = THIS_MODULE,
391 .name = "firedtv",
392 .bus = &fw_bus_type,
Stefan Richter87918332009-11-08 18:30:54 -0300393 },
Stefan Richter94a87152013-06-09 18:15:00 +0200394 .probe = node_probe,
Stefan Richter87918332009-11-08 18:30:54 -0300395 .update = node_update,
Stefan Richter94a87152013-06-09 18:15:00 +0200396 .remove = node_remove,
Stefan Richter87918332009-11-08 18:30:54 -0300397 .id_table = fdtv_id_table,
398};
399
Stefan Richter92374e82011-02-06 11:41:44 -0300400static int __init fdtv_init(void)
Stefan Richter87918332009-11-08 18:30:54 -0300401{
402 int ret;
403
404 ret = fw_core_add_address_handler(&fcp_handler, &fcp_region);
405 if (ret < 0)
406 return ret;
407
Stefan Richter92374e82011-02-06 11:41:44 -0300408 ret = driver_register(&fdtv_driver.driver);
409 if (ret < 0)
410 fw_core_remove_address_handler(&fcp_handler);
411
412 return ret;
Stefan Richter87918332009-11-08 18:30:54 -0300413}
414
Stefan Richter92374e82011-02-06 11:41:44 -0300415static void __exit fdtv_exit(void)
Stefan Richter87918332009-11-08 18:30:54 -0300416{
417 driver_unregister(&fdtv_driver.driver);
418 fw_core_remove_address_handler(&fcp_handler);
419}
Stefan Richter92374e82011-02-06 11:41:44 -0300420
421module_init(fdtv_init);
422module_exit(fdtv_exit);
423
424MODULE_AUTHOR("Andreas Monitzer <andy@monitzer.com>");
425MODULE_AUTHOR("Ben Backx <ben@bbackx.com>");
426MODULE_DESCRIPTION("FireDTV DVB Driver");
427MODULE_LICENSE("GPL");
428MODULE_SUPPORTED_DEVICE("FireDTV DVB");