firedtv-fw.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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, size_t len)
  38. {
  39. return node_req(fdtv, addr, data, len, len == 4 ?
  40. TCODE_READ_QUADLET_REQUEST : TCODE_READ_BLOCK_REQUEST);
  41. }
  42. static int node_write(struct firedtv *fdtv, u64 addr, void *data, size_t len)
  43. {
  44. return node_req(fdtv, addr, data, len, TCODE_WRITE_BLOCK_REQUEST);
  45. }
  46. #define ISO_HEADER_SIZE 4
  47. #define CIP_HEADER_SIZE 8
  48. #define MPEG2_TS_HEADER_SIZE 4
  49. #define MPEG2_TS_SOURCE_PACKET_SIZE (4 + 188)
  50. #define MAX_PACKET_SIZE 1024 /* 776, rounded up to 2^n */
  51. #define PACKETS_PER_PAGE (PAGE_SIZE / MAX_PACKET_SIZE)
  52. #define N_PACKETS 64 /* buffer size */
  53. #define N_PAGES DIV_ROUND_UP(N_PACKETS, PACKETS_PER_PAGE)
  54. #define IRQ_INTERVAL 16
  55. struct firedtv_receive_context {
  56. struct fw_iso_context *context;
  57. struct fw_iso_buffer buffer;
  58. int interrupt_packet;
  59. int current_packet;
  60. char *pages[N_PAGES];
  61. };
  62. static int queue_iso(struct firedtv_receive_context *ctx, int index)
  63. {
  64. struct fw_iso_packet p;
  65. p.payload_length = MAX_PACKET_SIZE;
  66. p.interrupt = !(++ctx->interrupt_packet & (IRQ_INTERVAL - 1));
  67. p.skip = 0;
  68. p.header_length = ISO_HEADER_SIZE;
  69. return fw_iso_context_queue(ctx->context, &p, &ctx->buffer,
  70. index * MAX_PACKET_SIZE);
  71. }
  72. static void handle_iso(struct fw_iso_context *context, u32 cycle,
  73. size_t header_length, void *header, void *data)
  74. {
  75. struct firedtv *fdtv = data;
  76. struct firedtv_receive_context *ctx = fdtv->backend_data;
  77. __be32 *h, *h_end;
  78. int length, err, i = ctx->current_packet;
  79. char *p, *p_end;
  80. for (h = header, h_end = h + header_length / 4; h < h_end; h++) {
  81. length = be32_to_cpup(h) >> 16;
  82. if (unlikely(length > MAX_PACKET_SIZE)) {
  83. dev_err(fdtv->device, "length = %d\n", length);
  84. length = MAX_PACKET_SIZE;
  85. }
  86. p = ctx->pages[i / PACKETS_PER_PAGE]
  87. + (i % PACKETS_PER_PAGE) * MAX_PACKET_SIZE;
  88. p_end = p + length;
  89. for (p += CIP_HEADER_SIZE + MPEG2_TS_HEADER_SIZE; p < p_end;
  90. p += MPEG2_TS_SOURCE_PACKET_SIZE)
  91. dvb_dmx_swfilter_packets(&fdtv->demux, p, 1);
  92. err = queue_iso(ctx, i);
  93. if (unlikely(err))
  94. dev_err(fdtv->device, "requeue failed\n");
  95. i = (i + 1) & (N_PACKETS - 1);
  96. }
  97. ctx->current_packet = i;
  98. }
  99. static int start_iso(struct firedtv *fdtv)
  100. {
  101. struct firedtv_receive_context *ctx;
  102. struct fw_device *device = device_of(fdtv);
  103. int i, err;
  104. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  105. if (!ctx)
  106. return -ENOMEM;
  107. ctx->context = fw_iso_context_create(device->card,
  108. FW_ISO_CONTEXT_RECEIVE, fdtv->isochannel,
  109. device->max_speed, ISO_HEADER_SIZE, handle_iso, fdtv);
  110. if (IS_ERR(ctx->context)) {
  111. err = PTR_ERR(ctx->context);
  112. goto fail_free;
  113. }
  114. err = fw_iso_buffer_init(&ctx->buffer, device->card,
  115. N_PAGES, DMA_FROM_DEVICE);
  116. if (err)
  117. goto fail_context_destroy;
  118. ctx->interrupt_packet = 0;
  119. ctx->current_packet = 0;
  120. for (i = 0; i < N_PAGES; i++)
  121. ctx->pages[i] = page_address(ctx->buffer.pages[i]);
  122. for (i = 0; i < N_PACKETS; i++) {
  123. err = queue_iso(ctx, i);
  124. if (err)
  125. goto fail;
  126. }
  127. err = fw_iso_context_start(ctx->context, -1, 0,
  128. FW_ISO_CONTEXT_MATCH_ALL_TAGS);
  129. if (err)
  130. goto fail;
  131. fdtv->backend_data = ctx;
  132. return 0;
  133. fail:
  134. fw_iso_buffer_destroy(&ctx->buffer, device->card);
  135. fail_context_destroy:
  136. fw_iso_context_destroy(ctx->context);
  137. fail_free:
  138. kfree(ctx);
  139. return err;
  140. }
  141. static void stop_iso(struct firedtv *fdtv)
  142. {
  143. struct firedtv_receive_context *ctx = fdtv->backend_data;
  144. fw_iso_context_stop(ctx->context);
  145. fw_iso_buffer_destroy(&ctx->buffer, device_of(fdtv)->card);
  146. fw_iso_context_destroy(ctx->context);
  147. kfree(ctx);
  148. }
  149. static const struct firedtv_backend backend = {
  150. .lock = node_lock,
  151. .read = node_read,
  152. .write = node_write,
  153. .start_iso = start_iso,
  154. .stop_iso = stop_iso,
  155. };
  156. static void handle_fcp(struct fw_card *card, struct fw_request *request,
  157. int tcode, int destination, int source, int generation,
  158. int speed, unsigned long long offset,
  159. void *payload, size_t length, void *callback_data)
  160. {
  161. struct firedtv *f, *fdtv = NULL;
  162. struct fw_device *device;
  163. unsigned long flags;
  164. int su;
  165. if ((tcode != TCODE_WRITE_QUADLET_REQUEST &&
  166. tcode != TCODE_WRITE_BLOCK_REQUEST) ||
  167. offset != CSR_REGISTER_BASE + CSR_FCP_RESPONSE ||
  168. length == 0 ||
  169. (((u8 *)payload)[0] & 0xf0) != 0) {
  170. fw_send_response(card, request, RCODE_TYPE_ERROR);
  171. return;
  172. }
  173. su = ((u8 *)payload)[1] & 0x7;
  174. spin_lock_irqsave(&node_list_lock, flags);
  175. list_for_each_entry(f, &node_list, list) {
  176. device = device_of(f);
  177. if (device->generation != generation)
  178. continue;
  179. smp_rmb(); /* node_id vs. generation */
  180. if (device->card == card &&
  181. device->node_id == source &&
  182. (f->subunit == su || (f->subunit == 0 && su == 0x7))) {
  183. fdtv = f;
  184. break;
  185. }
  186. }
  187. spin_unlock_irqrestore(&node_list_lock, flags);
  188. if (fdtv) {
  189. avc_recv(fdtv, payload, length);
  190. fw_send_response(card, request, RCODE_COMPLETE);
  191. }
  192. }
  193. static struct fw_address_handler fcp_handler = {
  194. .length = CSR_FCP_END - CSR_FCP_RESPONSE,
  195. .address_callback = handle_fcp,
  196. };
  197. static const struct fw_address_region fcp_region = {
  198. .start = CSR_REGISTER_BASE + CSR_FCP_RESPONSE,
  199. .end = CSR_REGISTER_BASE + CSR_FCP_END,
  200. };
  201. /* Adjust the template string if models with longer names appear. */
  202. #define MAX_MODEL_NAME_LEN ((int)DIV_ROUND_UP(sizeof("FireDTV ????"), 4))
  203. static size_t model_name(u32 *directory, __be32 *buffer)
  204. {
  205. struct fw_csr_iterator ci;
  206. int i, length, key, value, last_key = 0;
  207. u32 *block = NULL;
  208. fw_csr_iterator_init(&ci, directory);
  209. while (fw_csr_iterator_next(&ci, &key, &value)) {
  210. if (last_key == CSR_MODEL &&
  211. key == (CSR_DESCRIPTOR | CSR_LEAF))
  212. block = ci.p - 1 + value;
  213. last_key = key;
  214. }
  215. if (block == NULL)
  216. return 0;
  217. length = min((int)(block[0] >> 16) - 2, MAX_MODEL_NAME_LEN);
  218. if (length <= 0)
  219. return 0;
  220. /* fast-forward to text string */
  221. block += 3;
  222. for (i = 0; i < length; i++)
  223. buffer[i] = cpu_to_be32(block[i]);
  224. return length * 4;
  225. }
  226. static int node_probe(struct device *dev)
  227. {
  228. struct firedtv *fdtv;
  229. __be32 name[MAX_MODEL_NAME_LEN];
  230. int name_len, err;
  231. name_len = model_name(fw_unit(dev)->directory, name);
  232. fdtv = fdtv_alloc(dev, &backend, (char *)name, name_len);
  233. if (!fdtv)
  234. return -ENOMEM;
  235. err = fdtv_register_rc(fdtv, dev);
  236. if (err)
  237. goto fail_free;
  238. spin_lock_irq(&node_list_lock);
  239. list_add_tail(&fdtv->list, &node_list);
  240. spin_unlock_irq(&node_list_lock);
  241. err = avc_identify_subunit(fdtv);
  242. if (err)
  243. goto fail;
  244. err = fdtv_dvb_register(fdtv);
  245. if (err)
  246. goto fail;
  247. avc_register_remote_control(fdtv);
  248. return 0;
  249. fail:
  250. spin_lock_irq(&node_list_lock);
  251. list_del(&fdtv->list);
  252. spin_unlock_irq(&node_list_lock);
  253. fdtv_unregister_rc(fdtv);
  254. fail_free:
  255. kfree(fdtv);
  256. return err;
  257. }
  258. static int node_remove(struct device *dev)
  259. {
  260. struct firedtv *fdtv = dev_get_drvdata(dev);
  261. fdtv_dvb_unregister(fdtv);
  262. spin_lock_irq(&node_list_lock);
  263. list_del(&fdtv->list);
  264. spin_unlock_irq(&node_list_lock);
  265. fdtv_unregister_rc(fdtv);
  266. kfree(fdtv);
  267. return 0;
  268. }
  269. static void node_update(struct fw_unit *unit)
  270. {
  271. struct firedtv *fdtv = dev_get_drvdata(&unit->device);
  272. if (fdtv->isochannel >= 0)
  273. cmp_establish_pp_connection(fdtv, fdtv->subunit,
  274. fdtv->isochannel);
  275. }
  276. static struct fw_driver fdtv_driver = {
  277. .driver = {
  278. .owner = THIS_MODULE,
  279. .name = "firedtv",
  280. .bus = &fw_bus_type,
  281. .probe = node_probe,
  282. .remove = node_remove,
  283. },
  284. .update = node_update,
  285. .id_table = fdtv_id_table,
  286. };
  287. int __init fdtv_fw_init(void)
  288. {
  289. int ret;
  290. ret = fw_core_add_address_handler(&fcp_handler, &fcp_region);
  291. if (ret < 0)
  292. return ret;
  293. return driver_register(&fdtv_driver.driver);
  294. }
  295. void fdtv_fw_exit(void)
  296. {
  297. driver_unregister(&fdtv_driver.driver);
  298. fw_core_remove_address_handler(&fcp_handler);
  299. }