firedtv-fw.c 10 KB

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