firedtv-1394.c 6.7 KB

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