firedtv-1394.c 6.7 KB

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