firedtv-fw.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * FireDTV driver -- firewire I/O backend
  3. */
  4. #include <linux/device.h>
  5. #include <linux/errno.h>
  6. #include <linux/firewire.h>
  7. #include <linux/firewire-constants.h>
  8. #include <linux/kernel.h>
  9. #include <linux/list.h>
  10. #include <linux/mm.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/module.h>
  13. #include <linux/mutex.h>
  14. #include <linux/slab.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/string.h>
  17. #include <linux/types.h>
  18. #include <linux/wait.h>
  19. #include <linux/workqueue.h>
  20. #include <asm/page.h>
  21. #include <dvb_demux.h>
  22. #include "firedtv.h"
  23. static LIST_HEAD(node_list);
  24. static DEFINE_SPINLOCK(node_list_lock);
  25. static inline struct fw_device *device_of(struct firedtv *fdtv)
  26. {
  27. return fw_device(fdtv->device->parent);
  28. }
  29. static int node_req(struct firedtv *fdtv, u64 addr, void *data, size_t len,
  30. int tcode)
  31. {
  32. struct fw_device *device = device_of(fdtv);
  33. int rcode, generation = device->generation;
  34. smp_rmb(); /* node_id vs. generation */
  35. rcode = fw_run_transaction(device->card, tcode, device->node_id,
  36. generation, device->max_speed, addr, data, len);
  37. return rcode != RCODE_COMPLETE ? -EIO : 0;
  38. }
  39. int fdtv_lock(struct firedtv *fdtv, u64 addr, void *data)
  40. {
  41. return node_req(fdtv, addr, data, 8, TCODE_LOCK_COMPARE_SWAP);
  42. }
  43. int fdtv_read(struct firedtv *fdtv, u64 addr, void *data)
  44. {
  45. return node_req(fdtv, addr, data, 4, TCODE_READ_QUADLET_REQUEST);
  46. }
  47. int fdtv_write(struct firedtv *fdtv, u64 addr, void *data, size_t len)
  48. {
  49. return node_req(fdtv, addr, data, len, TCODE_WRITE_BLOCK_REQUEST);
  50. }
  51. #define ISO_HEADER_SIZE 4
  52. #define CIP_HEADER_SIZE 8
  53. #define MPEG2_TS_HEADER_SIZE 4
  54. #define MPEG2_TS_SOURCE_PACKET_SIZE (4 + 188)
  55. #define MAX_PACKET_SIZE 1024 /* 776, rounded up to 2^n */
  56. #define PACKETS_PER_PAGE (PAGE_SIZE / MAX_PACKET_SIZE)
  57. #define N_PACKETS 64 /* buffer size */
  58. #define N_PAGES DIV_ROUND_UP(N_PACKETS, PACKETS_PER_PAGE)
  59. #define IRQ_INTERVAL 16
  60. struct fdtv_ir_context {
  61. struct fw_iso_context *context;
  62. struct fw_iso_buffer buffer;
  63. int interrupt_packet;
  64. int current_packet;
  65. char *pages[N_PAGES];
  66. };
  67. static int queue_iso(struct fdtv_ir_context *ctx, int index)
  68. {
  69. struct fw_iso_packet p;
  70. p.payload_length = MAX_PACKET_SIZE;
  71. p.interrupt = !(++ctx->interrupt_packet & (IRQ_INTERVAL - 1));
  72. p.skip = 0;
  73. p.header_length = ISO_HEADER_SIZE;
  74. return fw_iso_context_queue(ctx->context, &p, &ctx->buffer,
  75. index * MAX_PACKET_SIZE);
  76. }
  77. static void handle_iso(struct fw_iso_context *context, u32 cycle,
  78. size_t header_length, void *header, void *data)
  79. {
  80. struct firedtv *fdtv = data;
  81. struct fdtv_ir_context *ctx = fdtv->ir_context;
  82. __be32 *h, *h_end;
  83. int length, err, i = ctx->current_packet;
  84. char *p, *p_end;
  85. for (h = header, h_end = h + header_length / 4; h < h_end; h++) {
  86. length = be32_to_cpup(h) >> 16;
  87. if (unlikely(length > MAX_PACKET_SIZE)) {
  88. dev_err(fdtv->device, "length = %d\n", length);
  89. length = MAX_PACKET_SIZE;
  90. }
  91. p = ctx->pages[i / PACKETS_PER_PAGE]
  92. + (i % PACKETS_PER_PAGE) * MAX_PACKET_SIZE;
  93. p_end = p + length;
  94. for (p += CIP_HEADER_SIZE + MPEG2_TS_HEADER_SIZE; p < p_end;
  95. p += MPEG2_TS_SOURCE_PACKET_SIZE)
  96. dvb_dmx_swfilter_packets(&fdtv->demux, p, 1);
  97. err = queue_iso(ctx, i);
  98. if (unlikely(err))
  99. dev_err(fdtv->device, "requeue failed\n");
  100. i = (i + 1) & (N_PACKETS - 1);
  101. }
  102. fw_iso_context_queue_flush(ctx->context);
  103. ctx->current_packet = i;
  104. }
  105. int fdtv_start_iso(struct firedtv *fdtv)
  106. {
  107. struct fdtv_ir_context *ctx;
  108. struct fw_device *device = device_of(fdtv);
  109. int i, err;
  110. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  111. if (!ctx)
  112. return -ENOMEM;
  113. ctx->context = fw_iso_context_create(device->card,
  114. FW_ISO_CONTEXT_RECEIVE, fdtv->isochannel,
  115. device->max_speed, ISO_HEADER_SIZE, handle_iso, fdtv);
  116. if (IS_ERR(ctx->context)) {
  117. err = PTR_ERR(ctx->context);
  118. goto fail_free;
  119. }
  120. err = fw_iso_buffer_init(&ctx->buffer, device->card,
  121. N_PAGES, DMA_FROM_DEVICE);
  122. if (err)
  123. goto fail_context_destroy;
  124. ctx->interrupt_packet = 0;
  125. ctx->current_packet = 0;
  126. for (i = 0; i < N_PAGES; i++)
  127. ctx->pages[i] = page_address(ctx->buffer.pages[i]);
  128. for (i = 0; i < N_PACKETS; i++) {
  129. err = queue_iso(ctx, i);
  130. if (err)
  131. goto fail;
  132. }
  133. err = fw_iso_context_start(ctx->context, -1, 0,
  134. FW_ISO_CONTEXT_MATCH_ALL_TAGS);
  135. if (err)
  136. goto fail;
  137. fdtv->ir_context = ctx;
  138. return 0;
  139. fail:
  140. fw_iso_buffer_destroy(&ctx->buffer, device->card);
  141. fail_context_destroy:
  142. fw_iso_context_destroy(ctx->context);
  143. fail_free:
  144. kfree(ctx);
  145. return err;
  146. }
  147. void fdtv_stop_iso(struct firedtv *fdtv)
  148. {
  149. struct fdtv_ir_context *ctx = fdtv->ir_context;
  150. fw_iso_context_stop(ctx->context);
  151. fw_iso_buffer_destroy(&ctx->buffer, device_of(fdtv)->card);
  152. fw_iso_context_destroy(ctx->context);
  153. kfree(ctx);
  154. }
  155. static void handle_fcp(struct fw_card *card, struct fw_request *request,
  156. int tcode, int destination, int source, int generation,
  157. unsigned long long offset, void *payload, size_t length,
  158. void *callback_data)
  159. {
  160. struct firedtv *f, *fdtv = NULL;
  161. struct fw_device *device;
  162. unsigned long flags;
  163. int su;
  164. if (length < 2 || (((u8 *)payload)[0] & 0xf0) != 0)
  165. return;
  166. su = ((u8 *)payload)[1] & 0x7;
  167. spin_lock_irqsave(&node_list_lock, flags);
  168. list_for_each_entry(f, &node_list, list) {
  169. device = device_of(f);
  170. if (device->generation != generation)
  171. continue;
  172. smp_rmb(); /* node_id vs. generation */
  173. if (device->card == card &&
  174. device->node_id == source &&
  175. (f->subunit == su || (f->subunit == 0 && su == 0x7))) {
  176. fdtv = f;
  177. break;
  178. }
  179. }
  180. spin_unlock_irqrestore(&node_list_lock, flags);
  181. if (fdtv)
  182. avc_recv(fdtv, payload, length);
  183. }
  184. static struct fw_address_handler fcp_handler = {
  185. .length = CSR_FCP_END - CSR_FCP_RESPONSE,
  186. .address_callback = handle_fcp,
  187. };
  188. static const struct fw_address_region fcp_region = {
  189. .start = CSR_REGISTER_BASE + CSR_FCP_RESPONSE,
  190. .end = CSR_REGISTER_BASE + CSR_FCP_END,
  191. };
  192. static const char * const model_names[] = {
  193. [FIREDTV_UNKNOWN] = "unknown type",
  194. [FIREDTV_DVB_S] = "FireDTV S/CI",
  195. [FIREDTV_DVB_C] = "FireDTV C/CI",
  196. [FIREDTV_DVB_T] = "FireDTV T/CI",
  197. [FIREDTV_DVB_S2] = "FireDTV S2 ",
  198. };
  199. /* Adjust the template string if models with longer names appear. */
  200. #define MAX_MODEL_NAME_LEN sizeof("FireDTV ????")
  201. static int node_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
  202. {
  203. struct firedtv *fdtv;
  204. char name[MAX_MODEL_NAME_LEN];
  205. int name_len, i, err;
  206. fdtv = kzalloc(sizeof(*fdtv), GFP_KERNEL);
  207. if (!fdtv)
  208. return -ENOMEM;
  209. dev_set_drvdata(&unit->device, fdtv);
  210. fdtv->device = &unit->device;
  211. fdtv->isochannel = -1;
  212. fdtv->voltage = 0xff;
  213. fdtv->tone = 0xff;
  214. mutex_init(&fdtv->avc_mutex);
  215. init_waitqueue_head(&fdtv->avc_wait);
  216. mutex_init(&fdtv->demux_mutex);
  217. INIT_WORK(&fdtv->remote_ctrl_work, avc_remote_ctrl_work);
  218. name_len = fw_csr_string(unit->directory, CSR_MODEL,
  219. name, sizeof(name));
  220. for (i = ARRAY_SIZE(model_names); --i; )
  221. if (strlen(model_names[i]) <= name_len &&
  222. strncmp(name, model_names[i], name_len) == 0)
  223. break;
  224. fdtv->type = i;
  225. err = fdtv_register_rc(fdtv, &unit->device);
  226. if (err)
  227. goto fail_free;
  228. spin_lock_irq(&node_list_lock);
  229. list_add_tail(&fdtv->list, &node_list);
  230. spin_unlock_irq(&node_list_lock);
  231. err = avc_identify_subunit(fdtv);
  232. if (err)
  233. goto fail;
  234. err = fdtv_dvb_register(fdtv, model_names[fdtv->type]);
  235. if (err)
  236. goto fail;
  237. avc_register_remote_control(fdtv);
  238. return 0;
  239. fail:
  240. spin_lock_irq(&node_list_lock);
  241. list_del(&fdtv->list);
  242. spin_unlock_irq(&node_list_lock);
  243. fdtv_unregister_rc(fdtv);
  244. fail_free:
  245. kfree(fdtv);
  246. return err;
  247. }
  248. static void node_remove(struct fw_unit *unit)
  249. {
  250. struct firedtv *fdtv = dev_get_drvdata(&unit->device);
  251. fdtv_dvb_unregister(fdtv);
  252. spin_lock_irq(&node_list_lock);
  253. list_del(&fdtv->list);
  254. spin_unlock_irq(&node_list_lock);
  255. fdtv_unregister_rc(fdtv);
  256. kfree(fdtv);
  257. }
  258. static void node_update(struct fw_unit *unit)
  259. {
  260. struct firedtv *fdtv = dev_get_drvdata(&unit->device);
  261. if (fdtv->isochannel >= 0)
  262. cmp_establish_pp_connection(fdtv, fdtv->subunit,
  263. fdtv->isochannel);
  264. }
  265. #define MATCH_FLAGS (IEEE1394_MATCH_VENDOR_ID | IEEE1394_MATCH_MODEL_ID | \
  266. IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION)
  267. #define DIGITAL_EVERYWHERE_OUI 0x001287
  268. #define AVC_UNIT_SPEC_ID_ENTRY 0x00a02d
  269. #define AVC_SW_VERSION_ENTRY 0x010001
  270. static const struct ieee1394_device_id fdtv_id_table[] = {
  271. {
  272. /* FloppyDTV S/CI and FloppyDTV S2 */
  273. .match_flags = MATCH_FLAGS,
  274. .vendor_id = DIGITAL_EVERYWHERE_OUI,
  275. .model_id = 0x000024,
  276. .specifier_id = AVC_UNIT_SPEC_ID_ENTRY,
  277. .version = AVC_SW_VERSION_ENTRY,
  278. }, {
  279. /* FloppyDTV T/CI */
  280. .match_flags = MATCH_FLAGS,
  281. .vendor_id = DIGITAL_EVERYWHERE_OUI,
  282. .model_id = 0x000025,
  283. .specifier_id = AVC_UNIT_SPEC_ID_ENTRY,
  284. .version = AVC_SW_VERSION_ENTRY,
  285. }, {
  286. /* FloppyDTV C/CI */
  287. .match_flags = MATCH_FLAGS,
  288. .vendor_id = DIGITAL_EVERYWHERE_OUI,
  289. .model_id = 0x000026,
  290. .specifier_id = AVC_UNIT_SPEC_ID_ENTRY,
  291. .version = AVC_SW_VERSION_ENTRY,
  292. }, {
  293. /* FireDTV S/CI and FloppyDTV S2 */
  294. .match_flags = MATCH_FLAGS,
  295. .vendor_id = DIGITAL_EVERYWHERE_OUI,
  296. .model_id = 0x000034,
  297. .specifier_id = AVC_UNIT_SPEC_ID_ENTRY,
  298. .version = AVC_SW_VERSION_ENTRY,
  299. }, {
  300. /* FireDTV T/CI */
  301. .match_flags = MATCH_FLAGS,
  302. .vendor_id = DIGITAL_EVERYWHERE_OUI,
  303. .model_id = 0x000035,
  304. .specifier_id = AVC_UNIT_SPEC_ID_ENTRY,
  305. .version = AVC_SW_VERSION_ENTRY,
  306. }, {
  307. /* FireDTV C/CI */
  308. .match_flags = MATCH_FLAGS,
  309. .vendor_id = DIGITAL_EVERYWHERE_OUI,
  310. .model_id = 0x000036,
  311. .specifier_id = AVC_UNIT_SPEC_ID_ENTRY,
  312. .version = AVC_SW_VERSION_ENTRY,
  313. }, {}
  314. };
  315. MODULE_DEVICE_TABLE(ieee1394, fdtv_id_table);
  316. static struct fw_driver fdtv_driver = {
  317. .driver = {
  318. .owner = THIS_MODULE,
  319. .name = "firedtv",
  320. .bus = &fw_bus_type,
  321. },
  322. .probe = node_probe,
  323. .update = node_update,
  324. .remove = node_remove,
  325. .id_table = fdtv_id_table,
  326. };
  327. static int __init fdtv_init(void)
  328. {
  329. int ret;
  330. ret = fw_core_add_address_handler(&fcp_handler, &fcp_region);
  331. if (ret < 0)
  332. return ret;
  333. ret = driver_register(&fdtv_driver.driver);
  334. if (ret < 0)
  335. fw_core_remove_address_handler(&fcp_handler);
  336. return ret;
  337. }
  338. static void __exit fdtv_exit(void)
  339. {
  340. driver_unregister(&fdtv_driver.driver);
  341. fw_core_remove_address_handler(&fcp_handler);
  342. }
  343. module_init(fdtv_init);
  344. module_exit(fdtv_exit);
  345. MODULE_AUTHOR("Andreas Monitzer <andy@monitzer.com>");
  346. MODULE_AUTHOR("Ben Backx <ben@bbackx.com>");
  347. MODULE_DESCRIPTION("FireDTV DVB Driver");
  348. MODULE_LICENSE("GPL");
  349. MODULE_SUPPORTED_DEVICE("FireDTV DVB");