0001
0002
0003
0004
0005
0006 #include <linux/device.h>
0007 #include <linux/errno.h>
0008 #include <linux/firewire.h>
0009 #include <linux/firewire-constants.h>
0010 #include <linux/kernel.h>
0011 #include <linux/list.h>
0012 #include <linux/mm.h>
0013 #include <linux/mod_devicetable.h>
0014 #include <linux/module.h>
0015 #include <linux/mutex.h>
0016 #include <linux/slab.h>
0017 #include <linux/spinlock.h>
0018 #include <linux/string.h>
0019 #include <linux/types.h>
0020 #include <linux/wait.h>
0021 #include <linux/workqueue.h>
0022
0023 #include <asm/page.h>
0024
0025 #include <media/dvb_demux.h>
0026
0027 #include "firedtv.h"
0028
0029 static LIST_HEAD(node_list);
0030 static DEFINE_SPINLOCK(node_list_lock);
0031
0032 static inline struct fw_device *device_of(struct firedtv *fdtv)
0033 {
0034 return fw_device(fdtv->device->parent);
0035 }
0036
0037 static int node_req(struct firedtv *fdtv, u64 addr, void *data, size_t len,
0038 int tcode)
0039 {
0040 struct fw_device *device = device_of(fdtv);
0041 int rcode, generation = device->generation;
0042
0043 smp_rmb();
0044
0045 rcode = fw_run_transaction(device->card, tcode, device->node_id,
0046 generation, device->max_speed, addr, data, len);
0047
0048 return rcode != RCODE_COMPLETE ? -EIO : 0;
0049 }
0050
0051 int fdtv_lock(struct firedtv *fdtv, u64 addr, void *data)
0052 {
0053 return node_req(fdtv, addr, data, 8, TCODE_LOCK_COMPARE_SWAP);
0054 }
0055
0056 int fdtv_read(struct firedtv *fdtv, u64 addr, void *data)
0057 {
0058 return node_req(fdtv, addr, data, 4, TCODE_READ_QUADLET_REQUEST);
0059 }
0060
0061 int fdtv_write(struct firedtv *fdtv, u64 addr, void *data, size_t len)
0062 {
0063 return node_req(fdtv, addr, data, len, TCODE_WRITE_BLOCK_REQUEST);
0064 }
0065
0066 #define ISO_HEADER_SIZE 4
0067 #define CIP_HEADER_SIZE 8
0068 #define MPEG2_TS_HEADER_SIZE 4
0069 #define MPEG2_TS_SOURCE_PACKET_SIZE (4 + 188)
0070
0071 #define MAX_PACKET_SIZE 1024
0072 #define PACKETS_PER_PAGE (PAGE_SIZE / MAX_PACKET_SIZE)
0073 #define N_PACKETS 64
0074 #define N_PAGES DIV_ROUND_UP(N_PACKETS, PACKETS_PER_PAGE)
0075 #define IRQ_INTERVAL 16
0076
0077 struct fdtv_ir_context {
0078 struct fw_iso_context *context;
0079 struct fw_iso_buffer buffer;
0080 int interrupt_packet;
0081 int current_packet;
0082 char *pages[N_PAGES];
0083 };
0084
0085 static int queue_iso(struct fdtv_ir_context *ctx, int index)
0086 {
0087 struct fw_iso_packet p;
0088
0089 p.payload_length = MAX_PACKET_SIZE;
0090 p.interrupt = !(++ctx->interrupt_packet & (IRQ_INTERVAL - 1));
0091 p.skip = 0;
0092 p.header_length = ISO_HEADER_SIZE;
0093
0094 return fw_iso_context_queue(ctx->context, &p, &ctx->buffer,
0095 index * MAX_PACKET_SIZE);
0096 }
0097
0098 static void handle_iso(struct fw_iso_context *context, u32 cycle,
0099 size_t header_length, void *header, void *data)
0100 {
0101 struct firedtv *fdtv = data;
0102 struct fdtv_ir_context *ctx = fdtv->ir_context;
0103 __be32 *h, *h_end;
0104 int length, err, i = ctx->current_packet;
0105 char *p, *p_end;
0106
0107 for (h = header, h_end = h + header_length / 4; h < h_end; h++) {
0108 length = be32_to_cpup(h) >> 16;
0109 if (unlikely(length > MAX_PACKET_SIZE)) {
0110 dev_err(fdtv->device, "length = %d\n", length);
0111 length = MAX_PACKET_SIZE;
0112 }
0113
0114 p = ctx->pages[i / PACKETS_PER_PAGE]
0115 + (i % PACKETS_PER_PAGE) * MAX_PACKET_SIZE;
0116 p_end = p + length;
0117
0118 for (p += CIP_HEADER_SIZE + MPEG2_TS_HEADER_SIZE; p < p_end;
0119 p += MPEG2_TS_SOURCE_PACKET_SIZE)
0120 dvb_dmx_swfilter_packets(&fdtv->demux, p, 1);
0121
0122 err = queue_iso(ctx, i);
0123 if (unlikely(err))
0124 dev_err(fdtv->device, "requeue failed\n");
0125
0126 i = (i + 1) & (N_PACKETS - 1);
0127 }
0128 fw_iso_context_queue_flush(ctx->context);
0129 ctx->current_packet = i;
0130 }
0131
0132 int fdtv_start_iso(struct firedtv *fdtv)
0133 {
0134 struct fdtv_ir_context *ctx;
0135 struct fw_device *device = device_of(fdtv);
0136 int i, err;
0137
0138 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
0139 if (!ctx)
0140 return -ENOMEM;
0141
0142 ctx->context = fw_iso_context_create(device->card,
0143 FW_ISO_CONTEXT_RECEIVE, fdtv->isochannel,
0144 device->max_speed, ISO_HEADER_SIZE, handle_iso, fdtv);
0145 if (IS_ERR(ctx->context)) {
0146 err = PTR_ERR(ctx->context);
0147 goto fail_free;
0148 }
0149
0150 err = fw_iso_buffer_init(&ctx->buffer, device->card,
0151 N_PAGES, DMA_FROM_DEVICE);
0152 if (err)
0153 goto fail_context_destroy;
0154
0155 ctx->interrupt_packet = 0;
0156 ctx->current_packet = 0;
0157
0158 for (i = 0; i < N_PAGES; i++)
0159 ctx->pages[i] = page_address(ctx->buffer.pages[i]);
0160
0161 for (i = 0; i < N_PACKETS; i++) {
0162 err = queue_iso(ctx, i);
0163 if (err)
0164 goto fail;
0165 }
0166
0167 err = fw_iso_context_start(ctx->context, -1, 0,
0168 FW_ISO_CONTEXT_MATCH_ALL_TAGS);
0169 if (err)
0170 goto fail;
0171
0172 fdtv->ir_context = ctx;
0173
0174 return 0;
0175 fail:
0176 fw_iso_buffer_destroy(&ctx->buffer, device->card);
0177 fail_context_destroy:
0178 fw_iso_context_destroy(ctx->context);
0179 fail_free:
0180 kfree(ctx);
0181
0182 return err;
0183 }
0184
0185 void fdtv_stop_iso(struct firedtv *fdtv)
0186 {
0187 struct fdtv_ir_context *ctx = fdtv->ir_context;
0188
0189 fw_iso_context_stop(ctx->context);
0190 fw_iso_buffer_destroy(&ctx->buffer, device_of(fdtv)->card);
0191 fw_iso_context_destroy(ctx->context);
0192 kfree(ctx);
0193 }
0194
0195 static void handle_fcp(struct fw_card *card, struct fw_request *request,
0196 int tcode, int destination, int source, int generation,
0197 unsigned long long offset, void *payload, size_t length,
0198 void *callback_data)
0199 {
0200 struct firedtv *f, *fdtv = NULL;
0201 struct fw_device *device;
0202 unsigned long flags;
0203 int su;
0204
0205 if (length < 2 || (((u8 *)payload)[0] & 0xf0) != 0)
0206 return;
0207
0208 su = ((u8 *)payload)[1] & 0x7;
0209
0210 spin_lock_irqsave(&node_list_lock, flags);
0211 list_for_each_entry(f, &node_list, list) {
0212 device = device_of(f);
0213 if (device->generation != generation)
0214 continue;
0215
0216 smp_rmb();
0217
0218 if (device->card == card &&
0219 device->node_id == source &&
0220 (f->subunit == su || (f->subunit == 0 && su == 0x7))) {
0221 fdtv = f;
0222 break;
0223 }
0224 }
0225 spin_unlock_irqrestore(&node_list_lock, flags);
0226
0227 if (fdtv)
0228 avc_recv(fdtv, payload, length);
0229 }
0230
0231 static struct fw_address_handler fcp_handler = {
0232 .length = CSR_FCP_END - CSR_FCP_RESPONSE,
0233 .address_callback = handle_fcp,
0234 };
0235
0236 static const struct fw_address_region fcp_region = {
0237 .start = CSR_REGISTER_BASE + CSR_FCP_RESPONSE,
0238 .end = CSR_REGISTER_BASE + CSR_FCP_END,
0239 };
0240
0241 static const char * const model_names[] = {
0242 [FIREDTV_UNKNOWN] = "unknown type",
0243 [FIREDTV_DVB_S] = "FireDTV S/CI",
0244 [FIREDTV_DVB_C] = "FireDTV C/CI",
0245 [FIREDTV_DVB_T] = "FireDTV T/CI",
0246 [FIREDTV_DVB_S2] = "FireDTV S2 ",
0247 };
0248
0249
0250 #define MAX_MODEL_NAME_LEN sizeof("FireDTV ????")
0251
0252 static int node_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
0253 {
0254 struct firedtv *fdtv;
0255 char name[MAX_MODEL_NAME_LEN];
0256 int name_len, i, err;
0257
0258 fdtv = kzalloc(sizeof(*fdtv), GFP_KERNEL);
0259 if (!fdtv)
0260 return -ENOMEM;
0261
0262 dev_set_drvdata(&unit->device, fdtv);
0263 fdtv->device = &unit->device;
0264 fdtv->isochannel = -1;
0265 fdtv->voltage = 0xff;
0266 fdtv->tone = 0xff;
0267
0268 mutex_init(&fdtv->avc_mutex);
0269 init_waitqueue_head(&fdtv->avc_wait);
0270 mutex_init(&fdtv->demux_mutex);
0271 INIT_WORK(&fdtv->remote_ctrl_work, avc_remote_ctrl_work);
0272
0273 name_len = fw_csr_string(unit->directory, CSR_MODEL,
0274 name, sizeof(name));
0275 if (name_len < 0) {
0276 err = name_len;
0277 goto fail_free;
0278 }
0279 for (i = ARRAY_SIZE(model_names); --i; )
0280 if (strlen(model_names[i]) <= name_len &&
0281 strncmp(name, model_names[i], name_len) == 0)
0282 break;
0283 fdtv->type = i;
0284
0285 err = fdtv_register_rc(fdtv, &unit->device);
0286 if (err)
0287 goto fail_free;
0288
0289 spin_lock_irq(&node_list_lock);
0290 list_add_tail(&fdtv->list, &node_list);
0291 spin_unlock_irq(&node_list_lock);
0292
0293 err = avc_identify_subunit(fdtv);
0294 if (err)
0295 goto fail;
0296
0297 err = fdtv_dvb_register(fdtv, model_names[fdtv->type]);
0298 if (err)
0299 goto fail;
0300
0301 avc_register_remote_control(fdtv);
0302
0303 return 0;
0304 fail:
0305 spin_lock_irq(&node_list_lock);
0306 list_del(&fdtv->list);
0307 spin_unlock_irq(&node_list_lock);
0308 fdtv_unregister_rc(fdtv);
0309 fail_free:
0310 kfree(fdtv);
0311
0312 return err;
0313 }
0314
0315 static void node_remove(struct fw_unit *unit)
0316 {
0317 struct firedtv *fdtv = dev_get_drvdata(&unit->device);
0318
0319 fdtv_dvb_unregister(fdtv);
0320
0321 spin_lock_irq(&node_list_lock);
0322 list_del(&fdtv->list);
0323 spin_unlock_irq(&node_list_lock);
0324
0325 fdtv_unregister_rc(fdtv);
0326
0327 kfree(fdtv);
0328 }
0329
0330 static void node_update(struct fw_unit *unit)
0331 {
0332 struct firedtv *fdtv = dev_get_drvdata(&unit->device);
0333
0334 if (fdtv->isochannel >= 0)
0335 cmp_establish_pp_connection(fdtv, fdtv->subunit,
0336 fdtv->isochannel);
0337 }
0338
0339 #define MATCH_FLAGS (IEEE1394_MATCH_VENDOR_ID | IEEE1394_MATCH_MODEL_ID | \
0340 IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION)
0341
0342 #define DIGITAL_EVERYWHERE_OUI 0x001287
0343 #define AVC_UNIT_SPEC_ID_ENTRY 0x00a02d
0344 #define AVC_SW_VERSION_ENTRY 0x010001
0345
0346 static const struct ieee1394_device_id fdtv_id_table[] = {
0347 {
0348
0349 .match_flags = MATCH_FLAGS,
0350 .vendor_id = DIGITAL_EVERYWHERE_OUI,
0351 .model_id = 0x000024,
0352 .specifier_id = AVC_UNIT_SPEC_ID_ENTRY,
0353 .version = AVC_SW_VERSION_ENTRY,
0354 }, {
0355
0356 .match_flags = MATCH_FLAGS,
0357 .vendor_id = DIGITAL_EVERYWHERE_OUI,
0358 .model_id = 0x000025,
0359 .specifier_id = AVC_UNIT_SPEC_ID_ENTRY,
0360 .version = AVC_SW_VERSION_ENTRY,
0361 }, {
0362
0363 .match_flags = MATCH_FLAGS,
0364 .vendor_id = DIGITAL_EVERYWHERE_OUI,
0365 .model_id = 0x000026,
0366 .specifier_id = AVC_UNIT_SPEC_ID_ENTRY,
0367 .version = AVC_SW_VERSION_ENTRY,
0368 }, {
0369
0370 .match_flags = MATCH_FLAGS,
0371 .vendor_id = DIGITAL_EVERYWHERE_OUI,
0372 .model_id = 0x000034,
0373 .specifier_id = AVC_UNIT_SPEC_ID_ENTRY,
0374 .version = AVC_SW_VERSION_ENTRY,
0375 }, {
0376
0377 .match_flags = MATCH_FLAGS,
0378 .vendor_id = DIGITAL_EVERYWHERE_OUI,
0379 .model_id = 0x000035,
0380 .specifier_id = AVC_UNIT_SPEC_ID_ENTRY,
0381 .version = AVC_SW_VERSION_ENTRY,
0382 }, {
0383
0384 .match_flags = MATCH_FLAGS,
0385 .vendor_id = DIGITAL_EVERYWHERE_OUI,
0386 .model_id = 0x000036,
0387 .specifier_id = AVC_UNIT_SPEC_ID_ENTRY,
0388 .version = AVC_SW_VERSION_ENTRY,
0389 }, {}
0390 };
0391 MODULE_DEVICE_TABLE(ieee1394, fdtv_id_table);
0392
0393 static struct fw_driver fdtv_driver = {
0394 .driver = {
0395 .owner = THIS_MODULE,
0396 .name = "firedtv",
0397 .bus = &fw_bus_type,
0398 },
0399 .probe = node_probe,
0400 .update = node_update,
0401 .remove = node_remove,
0402 .id_table = fdtv_id_table,
0403 };
0404
0405 static int __init fdtv_init(void)
0406 {
0407 int ret;
0408
0409 ret = fw_core_add_address_handler(&fcp_handler, &fcp_region);
0410 if (ret < 0)
0411 return ret;
0412
0413 ret = driver_register(&fdtv_driver.driver);
0414 if (ret < 0)
0415 fw_core_remove_address_handler(&fcp_handler);
0416
0417 return ret;
0418 }
0419
0420 static void __exit fdtv_exit(void)
0421 {
0422 driver_unregister(&fdtv_driver.driver);
0423 fw_core_remove_address_handler(&fcp_handler);
0424 }
0425
0426 module_init(fdtv_init);
0427 module_exit(fdtv_exit);
0428
0429 MODULE_AUTHOR("Andreas Monitzer <andy@monitzer.com>");
0430 MODULE_AUTHOR("Ben Backx <ben@bbackx.com>");
0431 MODULE_DESCRIPTION("FireDTV DVB Driver");
0432 MODULE_LICENSE("GPL");