firedtv-1394.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * FireDTV driver -- ieee1394 I/O backend
  3. *
  4. * Copyright (C) 2004 Andreas Monitzer <andy@monitzer.com>
  5. * Copyright (C) 2007-2008 Ben Backx <ben@bbackx.com>
  6. * Copyright (C) 2008 Henrik Kurelid <henrik@kurelid.se>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. */
  13. #include <linux/device.h>
  14. #include <linux/errno.h>
  15. #include <linux/kernel.h>
  16. #include <linux/list.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/types.h>
  19. #include <dma.h>
  20. #include <csr1212.h>
  21. #include <highlevel.h>
  22. #include <hosts.h>
  23. #include <ieee1394.h>
  24. #include <iso.h>
  25. #include <nodemgr.h>
  26. #include <dvb_demux.h>
  27. #include "firedtv.h"
  28. static LIST_HEAD(node_list);
  29. static DEFINE_SPINLOCK(node_list_lock);
  30. #define CIP_HEADER_SIZE 8
  31. #define MPEG2_TS_HEADER_SIZE 4
  32. #define MPEG2_TS_SOURCE_PACKET_SIZE (4 + 188)
  33. static void rawiso_activity_cb(struct hpsb_iso *iso)
  34. {
  35. struct firedtv *f, *fdtv = NULL;
  36. unsigned int i, num, packet;
  37. unsigned char *buf;
  38. unsigned long flags;
  39. int count;
  40. spin_lock_irqsave(&node_list_lock, flags);
  41. list_for_each_entry(f, &node_list, list)
  42. if (f->backend_data == iso) {
  43. fdtv = f;
  44. break;
  45. }
  46. spin_unlock_irqrestore(&node_list_lock, flags);
  47. packet = iso->first_packet;
  48. num = hpsb_iso_n_ready(iso);
  49. if (!fdtv) {
  50. dev_err(fdtv->device, "received at unknown iso channel\n");
  51. goto out;
  52. }
  53. for (i = 0; i < num; i++, packet = (packet + 1) % iso->buf_packets) {
  54. buf = dma_region_i(&iso->data_buf, unsigned char,
  55. iso->infos[packet].offset + CIP_HEADER_SIZE);
  56. count = (iso->infos[packet].len - CIP_HEADER_SIZE) /
  57. MPEG2_TS_SOURCE_PACKET_SIZE;
  58. /* ignore empty packet */
  59. if (iso->infos[packet].len <= CIP_HEADER_SIZE)
  60. continue;
  61. while (count--) {
  62. if (buf[MPEG2_TS_HEADER_SIZE] == 0x47)
  63. dvb_dmx_swfilter_packets(&fdtv->demux,
  64. &buf[MPEG2_TS_HEADER_SIZE], 1);
  65. else
  66. dev_err(fdtv->device,
  67. "skipping invalid packet\n");
  68. buf += MPEG2_TS_SOURCE_PACKET_SIZE;
  69. }
  70. }
  71. out:
  72. hpsb_iso_recv_release_packets(iso, num);
  73. }
  74. static inline struct node_entry *node_of(struct firedtv *fdtv)
  75. {
  76. return container_of(fdtv->device, struct unit_directory, device)->ne;
  77. }
  78. static int node_lock(struct firedtv *fdtv, u64 addr, __be32 data[])
  79. {
  80. int ret;
  81. ret = hpsb_node_lock(node_of(fdtv), addr, EXTCODE_COMPARE_SWAP,
  82. (__force quadlet_t *)&data[1], (__force quadlet_t)data[0]);
  83. data[0] = data[1];
  84. return ret;
  85. }
  86. static int node_read(struct firedtv *fdtv, u64 addr, void *data)
  87. {
  88. return hpsb_node_read(node_of(fdtv), addr, data, 4);
  89. }
  90. static int node_write(struct firedtv *fdtv, u64 addr, void *data, size_t len)
  91. {
  92. return hpsb_node_write(node_of(fdtv), addr, data, len);
  93. }
  94. #define FDTV_ISO_BUFFER_PACKETS 256
  95. #define FDTV_ISO_BUFFER_SIZE (FDTV_ISO_BUFFER_PACKETS * 200)
  96. static int start_iso(struct firedtv *fdtv)
  97. {
  98. struct hpsb_iso *iso_handle;
  99. int ret;
  100. iso_handle = hpsb_iso_recv_init(node_of(fdtv)->host,
  101. FDTV_ISO_BUFFER_SIZE, FDTV_ISO_BUFFER_PACKETS,
  102. fdtv->isochannel, HPSB_ISO_DMA_DEFAULT,
  103. -1, /* stat.config.irq_interval */
  104. rawiso_activity_cb);
  105. if (iso_handle == NULL) {
  106. dev_err(fdtv->device, "cannot initialize iso receive\n");
  107. return -ENOMEM;
  108. }
  109. fdtv->backend_data = iso_handle;
  110. ret = hpsb_iso_recv_start(iso_handle, -1, -1, 0);
  111. if (ret != 0) {
  112. dev_err(fdtv->device, "cannot start iso receive\n");
  113. hpsb_iso_shutdown(iso_handle);
  114. fdtv->backend_data = NULL;
  115. }
  116. return ret;
  117. }
  118. static void stop_iso(struct firedtv *fdtv)
  119. {
  120. struct hpsb_iso *iso_handle = fdtv->backend_data;
  121. if (iso_handle != NULL) {
  122. hpsb_iso_stop(iso_handle);
  123. hpsb_iso_shutdown(iso_handle);
  124. }
  125. fdtv->backend_data = NULL;
  126. }
  127. static const struct firedtv_backend fdtv_1394_backend = {
  128. .lock = node_lock,
  129. .read = node_read,
  130. .write = node_write,
  131. .start_iso = start_iso,
  132. .stop_iso = stop_iso,
  133. };
  134. static void fcp_request(struct hpsb_host *host, int nodeid, int direction,
  135. int cts, u8 *data, size_t length)
  136. {
  137. struct firedtv *f, *fdtv = NULL;
  138. unsigned long flags;
  139. int su;
  140. if (length == 0 || (data[0] & 0xf0) != 0)
  141. return;
  142. su = data[1] & 0x7;
  143. spin_lock_irqsave(&node_list_lock, flags);
  144. list_for_each_entry(f, &node_list, list)
  145. if (node_of(f)->host == host &&
  146. node_of(f)->nodeid == nodeid &&
  147. (f->subunit == su || (f->subunit == 0 && su == 0x7))) {
  148. fdtv = f;
  149. break;
  150. }
  151. spin_unlock_irqrestore(&node_list_lock, flags);
  152. if (fdtv)
  153. avc_recv(fdtv, data, length);
  154. }
  155. static int node_probe(struct device *dev)
  156. {
  157. struct unit_directory *ud =
  158. container_of(dev, struct unit_directory, device);
  159. struct firedtv *fdtv;
  160. int kv_len, err;
  161. void *kv_str;
  162. kv_len = (ud->model_name_kv->value.leaf.len - 2) * sizeof(quadlet_t);
  163. kv_str = CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA(ud->model_name_kv);
  164. fdtv = fdtv_alloc(dev, &fdtv_1394_backend, kv_str, kv_len);
  165. if (!fdtv)
  166. return -ENOMEM;
  167. /*
  168. * Work around a bug in udev's path_id script: Use the fw-host's dev
  169. * instead of the unit directory's dev as parent of the input device.
  170. */
  171. err = fdtv_register_rc(fdtv, dev->parent->parent);
  172. if (err)
  173. goto fail_free;
  174. spin_lock_irq(&node_list_lock);
  175. list_add_tail(&fdtv->list, &node_list);
  176. spin_unlock_irq(&node_list_lock);
  177. err = avc_identify_subunit(fdtv);
  178. if (err)
  179. goto fail;
  180. err = fdtv_dvb_register(fdtv);
  181. if (err)
  182. goto fail;
  183. avc_register_remote_control(fdtv);
  184. return 0;
  185. fail:
  186. spin_lock_irq(&node_list_lock);
  187. list_del(&fdtv->list);
  188. spin_unlock_irq(&node_list_lock);
  189. fdtv_unregister_rc(fdtv);
  190. fail_free:
  191. kfree(fdtv);
  192. return err;
  193. }
  194. static int node_remove(struct device *dev)
  195. {
  196. struct firedtv *fdtv = dev_get_drvdata(dev);
  197. fdtv_dvb_unregister(fdtv);
  198. spin_lock_irq(&node_list_lock);
  199. list_del(&fdtv->list);
  200. spin_unlock_irq(&node_list_lock);
  201. fdtv_unregister_rc(fdtv);
  202. kfree(fdtv);
  203. return 0;
  204. }
  205. static int node_update(struct unit_directory *ud)
  206. {
  207. struct firedtv *fdtv = dev_get_drvdata(&ud->device);
  208. if (fdtv->isochannel >= 0)
  209. cmp_establish_pp_connection(fdtv, fdtv->subunit,
  210. fdtv->isochannel);
  211. return 0;
  212. }
  213. static struct hpsb_protocol_driver fdtv_driver = {
  214. .name = "firedtv",
  215. .id_table = fdtv_id_table,
  216. .update = node_update,
  217. .driver = {
  218. .probe = node_probe,
  219. .remove = node_remove,
  220. },
  221. };
  222. static struct hpsb_highlevel fdtv_highlevel = {
  223. .name = "firedtv",
  224. .fcp_request = fcp_request,
  225. };
  226. int __init fdtv_1394_init(void)
  227. {
  228. int ret;
  229. hpsb_register_highlevel(&fdtv_highlevel);
  230. ret = hpsb_register_protocol(&fdtv_driver);
  231. if (ret) {
  232. printk(KERN_ERR "firedtv: failed to register protocol\n");
  233. hpsb_unregister_highlevel(&fdtv_highlevel);
  234. }
  235. return ret;
  236. }
  237. void __exit fdtv_1394_exit(void)
  238. {
  239. hpsb_unregister_protocol(&fdtv_driver);
  240. hpsb_unregister_highlevel(&fdtv_highlevel);
  241. }