firedtv-fw.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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/slab.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/types.h>
  14. #include <asm/page.h>
  15. #include <dvb_demux.h>
  16. #include "firedtv.h"
  17. static LIST_HEAD(node_list);
  18. static DEFINE_SPINLOCK(node_list_lock);
  19. static inline struct fw_device *device_of(struct firedtv *fdtv)
  20. {
  21. return fw_device(fdtv->device->parent);
  22. }
  23. static int node_req(struct firedtv *fdtv, u64 addr, void *data, size_t len,
  24. int tcode)
  25. {
  26. struct fw_device *device = device_of(fdtv);
  27. int rcode, generation = device->generation;
  28. smp_rmb(); /* node_id vs. generation */
  29. rcode = fw_run_transaction(device->card, tcode, device->node_id,
  30. generation, device->max_speed, addr, data, len);
  31. return rcode != RCODE_COMPLETE ? -EIO : 0;
  32. }
  33. static int node_lock(struct firedtv *fdtv, u64 addr, __be32 data[])
  34. {
  35. return node_req(fdtv, addr, data, 8, TCODE_LOCK_COMPARE_SWAP);
  36. }
  37. static int node_read(struct firedtv *fdtv, u64 addr, void *data)
  38. {
  39. return node_req(fdtv, addr, data, 4, TCODE_READ_QUADLET_REQUEST);
  40. }
  41. static int node_write(struct firedtv *fdtv, u64 addr, void *data, size_t len)
  42. {
  43. return node_req(fdtv, addr, data, len, TCODE_WRITE_BLOCK_REQUEST);
  44. }
  45. #define ISO_HEADER_SIZE 4
  46. #define CIP_HEADER_SIZE 8
  47. #define MPEG2_TS_HEADER_SIZE 4
  48. #define MPEG2_TS_SOURCE_PACKET_SIZE (4 + 188)
  49. #define MAX_PACKET_SIZE 1024 /* 776, rounded up to 2^n */
  50. #define PACKETS_PER_PAGE (PAGE_SIZE / MAX_PACKET_SIZE)
  51. #define N_PACKETS 64 /* buffer size */
  52. #define N_PAGES DIV_ROUND_UP(N_PACKETS, PACKETS_PER_PAGE)
  53. #define IRQ_INTERVAL 16
  54. struct firedtv_receive_context {
  55. struct fw_iso_context *context;
  56. struct fw_iso_buffer buffer;
  57. int interrupt_packet;
  58. int current_packet;
  59. char *pages[N_PAGES];
  60. };
  61. static int queue_iso(struct firedtv_receive_context *ctx, int index)
  62. {
  63. struct fw_iso_packet p;
  64. p.payload_length = MAX_PACKET_SIZE;
  65. p.interrupt = !(++ctx->interrupt_packet & (IRQ_INTERVAL - 1));
  66. p.skip = 0;
  67. p.header_length = ISO_HEADER_SIZE;
  68. return fw_iso_context_queue(ctx->context, &p, &ctx->buffer,
  69. index * MAX_PACKET_SIZE);
  70. }
  71. static void handle_iso(struct fw_iso_context *context, u32 cycle,
  72. size_t header_length, void *header, void *data)
  73. {
  74. struct firedtv *fdtv = data;
  75. struct firedtv_receive_context *ctx = fdtv->backend_data;
  76. __be32 *h, *h_end;
  77. int length, err, i = ctx->current_packet;
  78. char *p, *p_end;
  79. for (h = header, h_end = h + header_length / 4; h < h_end; h++) {
  80. length = be32_to_cpup(h) >> 16;
  81. if (unlikely(length > MAX_PACKET_SIZE)) {
  82. dev_err(fdtv->device, "length = %d\n", length);
  83. length = MAX_PACKET_SIZE;
  84. }
  85. p = ctx->pages[i / PACKETS_PER_PAGE]
  86. + (i % PACKETS_PER_PAGE) * MAX_PACKET_SIZE;
  87. p_end = p + length;
  88. for (p += CIP_HEADER_SIZE + MPEG2_TS_HEADER_SIZE; p < p_end;
  89. p += MPEG2_TS_SOURCE_PACKET_SIZE)
  90. dvb_dmx_swfilter_packets(&fdtv->demux, p, 1);
  91. err = queue_iso(ctx, i);
  92. if (unlikely(err))
  93. dev_err(fdtv->device, "requeue failed\n");
  94. i = (i + 1) & (N_PACKETS - 1);
  95. }
  96. ctx->current_packet = i;
  97. }
  98. static int start_iso(struct firedtv *fdtv)
  99. {
  100. struct firedtv_receive_context *ctx;
  101. struct fw_device *device = device_of(fdtv);
  102. int i, err;
  103. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  104. if (!ctx)
  105. return -ENOMEM;
  106. ctx->context = fw_iso_context_create(device->card,
  107. FW_ISO_CONTEXT_RECEIVE, fdtv->isochannel,
  108. device->max_speed, ISO_HEADER_SIZE, handle_iso, fdtv);
  109. if (IS_ERR(ctx->context)) {
  110. err = PTR_ERR(ctx->context);
  111. goto fail_free;
  112. }
  113. err = fw_iso_buffer_init(&ctx->buffer, device->card,
  114. N_PAGES, DMA_FROM_DEVICE);
  115. if (err)
  116. goto fail_context_destroy;
  117. ctx->interrupt_packet = 0;
  118. ctx->current_packet = 0;
  119. for (i = 0; i < N_PAGES; i++)
  120. ctx->pages[i] = page_address(ctx->buffer.pages[i]);
  121. for (i = 0; i < N_PACKETS; i++) {
  122. err = queue_iso(ctx, i);
  123. if (err)
  124. goto fail;
  125. }
  126. err = fw_iso_context_start(ctx->context, -1, 0,
  127. FW_ISO_CONTEXT_MATCH_ALL_TAGS);
  128. if (err)
  129. goto fail;
  130. fdtv->backend_data = ctx;
  131. return 0;
  132. fail:
  133. fw_iso_buffer_destroy(&ctx->buffer, device->card);
  134. fail_context_destroy:
  135. fw_iso_context_destroy(ctx->context);
  136. fail_free:
  137. kfree(ctx);
  138. return err;
  139. }
  140. static void stop_iso(struct firedtv *fdtv)
  141. {
  142. struct firedtv_receive_context *ctx = fdtv->backend_data;
  143. fw_iso_context_stop(ctx->context);
  144. fw_iso_buffer_destroy(&ctx->buffer, device_of(fdtv)->card);
  145. fw_iso_context_destroy(ctx->context);
  146. kfree(ctx);
  147. }
  148. static const struct firedtv_backend backend = {
  149. .lock = node_lock,
  150. .read = node_read,
  151. .write = node_write,
  152. .start_iso = start_iso,
  153. .stop_iso = stop_iso,
  154. };
  155. static void handle_fcp(struct fw_card *card, struct fw_request *request,
  156. int tcode, int destination, int source, int generation,
  157. int speed, unsigned long long offset,
  158. void *payload, size_t length, 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. /* Adjust the template string if models with longer names appear. */
  193. #define MAX_MODEL_NAME_LEN ((int)DIV_ROUND_UP(sizeof("FireDTV ????"), 4))
  194. static size_t model_name(u32 *directory, __be32 *buffer)
  195. {
  196. struct fw_csr_iterator ci;
  197. int i, length, key, value, last_key = 0;
  198. u32 *block = NULL;
  199. fw_csr_iterator_init(&ci, directory);
  200. while (fw_csr_iterator_next(&ci, &key, &value)) {
  201. if (last_key == CSR_MODEL &&
  202. key == (CSR_DESCRIPTOR | CSR_LEAF))
  203. block = ci.p - 1 + value;
  204. last_key = key;
  205. }
  206. if (block == NULL)
  207. return 0;
  208. length = min((int)(block[0] >> 16) - 2, MAX_MODEL_NAME_LEN);
  209. if (length <= 0)
  210. return 0;
  211. /* fast-forward to text string */
  212. block += 3;
  213. for (i = 0; i < length; i++)
  214. buffer[i] = cpu_to_be32(block[i]);
  215. return length * 4;
  216. }
  217. static int node_probe(struct device *dev)
  218. {
  219. struct firedtv *fdtv;
  220. __be32 name[MAX_MODEL_NAME_LEN];
  221. int name_len, err;
  222. name_len = model_name(fw_unit(dev)->directory, name);
  223. fdtv = fdtv_alloc(dev, &backend, (char *)name, name_len);
  224. if (!fdtv)
  225. return -ENOMEM;
  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);
  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. static struct fw_driver fdtv_driver = {
  268. .driver = {
  269. .owner = THIS_MODULE,
  270. .name = "firedtv",
  271. .bus = &fw_bus_type,
  272. .probe = node_probe,
  273. .remove = node_remove,
  274. },
  275. .update = node_update,
  276. .id_table = fdtv_id_table,
  277. };
  278. int __init fdtv_fw_init(void)
  279. {
  280. int ret;
  281. ret = fw_core_add_address_handler(&fcp_handler, &fcp_region);
  282. if (ret < 0)
  283. return ret;
  284. return driver_register(&fdtv_driver.driver);
  285. }
  286. void fdtv_fw_exit(void)
  287. {
  288. driver_unregister(&fdtv_driver.driver);
  289. fw_core_remove_address_handler(&fcp_handler);
  290. }