firedtv-1394.c 6.7 KB

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