firedtv-1394.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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, void *data)
  79. {
  80. quadlet_t *d = data;
  81. int ret;
  82. ret = hpsb_node_lock(node_of(fdtv), addr,
  83. EXTCODE_COMPARE_SWAP, &d[1], d[0]);
  84. d[0] = d[1];
  85. return ret;
  86. }
  87. static int node_read(struct firedtv *fdtv, u64 addr, void *data)
  88. {
  89. return hpsb_node_read(node_of(fdtv), addr, data, 4);
  90. }
  91. static int node_write(struct firedtv *fdtv, u64 addr, void *data, size_t len)
  92. {
  93. return hpsb_node_write(node_of(fdtv), addr, data, len);
  94. }
  95. #define FDTV_ISO_BUFFER_PACKETS 256
  96. #define FDTV_ISO_BUFFER_SIZE (FDTV_ISO_BUFFER_PACKETS * 200)
  97. static int start_iso(struct firedtv *fdtv)
  98. {
  99. struct hpsb_iso *iso_handle;
  100. int ret;
  101. iso_handle = hpsb_iso_recv_init(node_of(fdtv)->host,
  102. FDTV_ISO_BUFFER_SIZE, FDTV_ISO_BUFFER_PACKETS,
  103. fdtv->isochannel, HPSB_ISO_DMA_DEFAULT,
  104. -1, /* stat.config.irq_interval */
  105. rawiso_activity_cb);
  106. if (iso_handle == NULL) {
  107. dev_err(fdtv->device, "cannot initialize iso receive\n");
  108. return -ENOMEM;
  109. }
  110. fdtv->backend_data = iso_handle;
  111. ret = hpsb_iso_recv_start(iso_handle, -1, -1, 0);
  112. if (ret != 0) {
  113. dev_err(fdtv->device, "cannot start iso receive\n");
  114. hpsb_iso_shutdown(iso_handle);
  115. fdtv->backend_data = NULL;
  116. }
  117. return ret;
  118. }
  119. static void stop_iso(struct firedtv *fdtv)
  120. {
  121. struct hpsb_iso *iso_handle = fdtv->backend_data;
  122. if (iso_handle != NULL) {
  123. hpsb_iso_stop(iso_handle);
  124. hpsb_iso_shutdown(iso_handle);
  125. }
  126. fdtv->backend_data = NULL;
  127. }
  128. static const struct firedtv_backend fdtv_1394_backend = {
  129. .lock = node_lock,
  130. .read = node_read,
  131. .write = node_write,
  132. .start_iso = start_iso,
  133. .stop_iso = stop_iso,
  134. };
  135. static void fcp_request(struct hpsb_host *host, int nodeid, int direction,
  136. int cts, u8 *data, size_t length)
  137. {
  138. struct firedtv *f, *fdtv = NULL;
  139. unsigned long flags;
  140. int su;
  141. if (length == 0 || (data[0] & 0xf0) != 0)
  142. return;
  143. su = data[1] & 0x7;
  144. spin_lock_irqsave(&node_list_lock, flags);
  145. list_for_each_entry(f, &node_list, list)
  146. if (node_of(f)->host == host &&
  147. node_of(f)->nodeid == nodeid &&
  148. (f->subunit == su || (f->subunit == 0 && su == 0x7))) {
  149. fdtv = f;
  150. break;
  151. }
  152. spin_unlock_irqrestore(&node_list_lock, flags);
  153. if (fdtv)
  154. avc_recv(fdtv, data, length);
  155. }
  156. static int node_probe(struct device *dev)
  157. {
  158. struct unit_directory *ud =
  159. container_of(dev, struct unit_directory, device);
  160. struct firedtv *fdtv;
  161. int kv_len, err;
  162. void *kv_str;
  163. if (ud->model_name_kv) {
  164. kv_len = (ud->model_name_kv->value.leaf.len - 2) * 4;
  165. kv_str = CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA(ud->model_name_kv);
  166. } else {
  167. kv_len = 0;
  168. kv_str = NULL;
  169. }
  170. fdtv = fdtv_alloc(dev, &fdtv_1394_backend, kv_str, kv_len);
  171. if (!fdtv)
  172. return -ENOMEM;
  173. /*
  174. * Work around a bug in udev's path_id script: Use the fw-host's dev
  175. * instead of the unit directory's dev as parent of the input device.
  176. */
  177. err = fdtv_register_rc(fdtv, dev->parent->parent);
  178. if (err)
  179. goto fail_free;
  180. spin_lock_irq(&node_list_lock);
  181. list_add_tail(&fdtv->list, &node_list);
  182. spin_unlock_irq(&node_list_lock);
  183. err = avc_identify_subunit(fdtv);
  184. if (err)
  185. goto fail;
  186. err = fdtv_dvb_register(fdtv);
  187. if (err)
  188. goto fail;
  189. avc_register_remote_control(fdtv);
  190. return 0;
  191. fail:
  192. spin_lock_irq(&node_list_lock);
  193. list_del(&fdtv->list);
  194. spin_unlock_irq(&node_list_lock);
  195. fdtv_unregister_rc(fdtv);
  196. fail_free:
  197. kfree(fdtv);
  198. return err;
  199. }
  200. static int node_remove(struct device *dev)
  201. {
  202. struct firedtv *fdtv = dev_get_drvdata(dev);
  203. fdtv_dvb_unregister(fdtv);
  204. spin_lock_irq(&node_list_lock);
  205. list_del(&fdtv->list);
  206. spin_unlock_irq(&node_list_lock);
  207. fdtv_unregister_rc(fdtv);
  208. kfree(fdtv);
  209. return 0;
  210. }
  211. static int node_update(struct unit_directory *ud)
  212. {
  213. struct firedtv *fdtv = dev_get_drvdata(&ud->device);
  214. if (fdtv->isochannel >= 0)
  215. cmp_establish_pp_connection(fdtv, fdtv->subunit,
  216. fdtv->isochannel);
  217. return 0;
  218. }
  219. static struct hpsb_protocol_driver fdtv_driver = {
  220. .name = "firedtv",
  221. .id_table = fdtv_id_table,
  222. .update = node_update,
  223. .driver = {
  224. .probe = node_probe,
  225. .remove = node_remove,
  226. },
  227. };
  228. static struct hpsb_highlevel fdtv_highlevel = {
  229. .name = "firedtv",
  230. .fcp_request = fcp_request,
  231. };
  232. int __init fdtv_1394_init(void)
  233. {
  234. int ret;
  235. hpsb_register_highlevel(&fdtv_highlevel);
  236. ret = hpsb_register_protocol(&fdtv_driver);
  237. if (ret) {
  238. printk(KERN_ERR "firedtv: failed to register protocol\n");
  239. hpsb_unregister_highlevel(&fdtv_highlevel);
  240. }
  241. return ret;
  242. }
  243. void __exit fdtv_1394_exit(void)
  244. {
  245. hpsb_unregister_protocol(&fdtv_driver);
  246. hpsb_unregister_highlevel(&fdtv_highlevel);
  247. }