iso.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * IEEE 1394 for Linux
  3. *
  4. * kernel ISO transmission/reception
  5. *
  6. * Copyright (C) 2002 Maas Digital LLC
  7. *
  8. * This code is licensed under the GPL. See the file COPYING in the root
  9. * directory of the kernel sources for details.
  10. */
  11. #include <linux/slab.h>
  12. #include <linux/sched.h>
  13. #include "iso.h"
  14. void hpsb_iso_stop(struct hpsb_iso *iso)
  15. {
  16. if (!(iso->flags & HPSB_ISO_DRIVER_STARTED))
  17. return;
  18. iso->host->driver->isoctl(iso, iso->type == HPSB_ISO_XMIT ?
  19. XMIT_STOP : RECV_STOP, 0);
  20. iso->flags &= ~HPSB_ISO_DRIVER_STARTED;
  21. }
  22. void hpsb_iso_shutdown(struct hpsb_iso *iso)
  23. {
  24. if (iso->flags & HPSB_ISO_DRIVER_INIT) {
  25. hpsb_iso_stop(iso);
  26. iso->host->driver->isoctl(iso, iso->type == HPSB_ISO_XMIT ?
  27. XMIT_SHUTDOWN : RECV_SHUTDOWN, 0);
  28. iso->flags &= ~HPSB_ISO_DRIVER_INIT;
  29. }
  30. dma_region_free(&iso->data_buf);
  31. kfree(iso);
  32. }
  33. static struct hpsb_iso* hpsb_iso_common_init(struct hpsb_host *host, enum hpsb_iso_type type,
  34. unsigned int data_buf_size,
  35. unsigned int buf_packets,
  36. int channel,
  37. int dma_mode,
  38. int irq_interval,
  39. void (*callback)(struct hpsb_iso*))
  40. {
  41. struct hpsb_iso *iso;
  42. int dma_direction;
  43. /* make sure driver supports the ISO API */
  44. if (!host->driver->isoctl) {
  45. printk(KERN_INFO "ieee1394: host driver '%s' does not support the rawiso API\n",
  46. host->driver->name);
  47. return NULL;
  48. }
  49. /* sanitize parameters */
  50. if (buf_packets < 2)
  51. buf_packets = 2;
  52. if ((dma_mode < HPSB_ISO_DMA_DEFAULT) || (dma_mode > HPSB_ISO_DMA_PACKET_PER_BUFFER))
  53. dma_mode=HPSB_ISO_DMA_DEFAULT;
  54. if (irq_interval == 0) /* really interrupt for each packet*/
  55. irq_interval = 1;
  56. else if ((irq_interval < 0) || (irq_interval > buf_packets / 4))
  57. irq_interval = buf_packets / 4;
  58. if (channel < -1 || channel >= 64)
  59. return NULL;
  60. /* channel = -1 is OK for multi-channel recv but not for xmit */
  61. if (type == HPSB_ISO_XMIT && channel < 0)
  62. return NULL;
  63. /* allocate and write the struct hpsb_iso */
  64. iso = kmalloc(sizeof(*iso) + buf_packets * sizeof(struct hpsb_iso_packet_info), GFP_KERNEL);
  65. if (!iso)
  66. return NULL;
  67. iso->infos = (struct hpsb_iso_packet_info *)(iso + 1);
  68. iso->type = type;
  69. iso->host = host;
  70. iso->hostdata = NULL;
  71. iso->callback = callback;
  72. init_waitqueue_head(&iso->waitq);
  73. iso->channel = channel;
  74. iso->irq_interval = irq_interval;
  75. iso->dma_mode = dma_mode;
  76. dma_region_init(&iso->data_buf);
  77. iso->buf_size = PAGE_ALIGN(data_buf_size);
  78. iso->buf_packets = buf_packets;
  79. iso->pkt_dma = 0;
  80. iso->first_packet = 0;
  81. spin_lock_init(&iso->lock);
  82. if (iso->type == HPSB_ISO_XMIT) {
  83. iso->n_ready_packets = iso->buf_packets;
  84. dma_direction = PCI_DMA_TODEVICE;
  85. } else {
  86. iso->n_ready_packets = 0;
  87. dma_direction = PCI_DMA_FROMDEVICE;
  88. }
  89. atomic_set(&iso->overflows, 0);
  90. iso->flags = 0;
  91. iso->prebuffer = 0;
  92. /* allocate the packet buffer */
  93. if (dma_region_alloc(&iso->data_buf, iso->buf_size, host->pdev, dma_direction))
  94. goto err;
  95. return iso;
  96. err:
  97. hpsb_iso_shutdown(iso);
  98. return NULL;
  99. }
  100. int hpsb_iso_n_ready(struct hpsb_iso* iso)
  101. {
  102. unsigned long flags;
  103. int val;
  104. spin_lock_irqsave(&iso->lock, flags);
  105. val = iso->n_ready_packets;
  106. spin_unlock_irqrestore(&iso->lock, flags);
  107. return val;
  108. }
  109. struct hpsb_iso* hpsb_iso_xmit_init(struct hpsb_host *host,
  110. unsigned int data_buf_size,
  111. unsigned int buf_packets,
  112. int channel,
  113. int speed,
  114. int irq_interval,
  115. void (*callback)(struct hpsb_iso*))
  116. {
  117. struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_XMIT,
  118. data_buf_size, buf_packets,
  119. channel, HPSB_ISO_DMA_DEFAULT, irq_interval, callback);
  120. if (!iso)
  121. return NULL;
  122. iso->speed = speed;
  123. /* tell the driver to start working */
  124. if (host->driver->isoctl(iso, XMIT_INIT, 0))
  125. goto err;
  126. iso->flags |= HPSB_ISO_DRIVER_INIT;
  127. return iso;
  128. err:
  129. hpsb_iso_shutdown(iso);
  130. return NULL;
  131. }
  132. struct hpsb_iso* hpsb_iso_recv_init(struct hpsb_host *host,
  133. unsigned int data_buf_size,
  134. unsigned int buf_packets,
  135. int channel,
  136. int dma_mode,
  137. int irq_interval,
  138. void (*callback)(struct hpsb_iso*))
  139. {
  140. struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_RECV,
  141. data_buf_size, buf_packets,
  142. channel, dma_mode, irq_interval, callback);
  143. if (!iso)
  144. return NULL;
  145. /* tell the driver to start working */
  146. if (host->driver->isoctl(iso, RECV_INIT, 0))
  147. goto err;
  148. iso->flags |= HPSB_ISO_DRIVER_INIT;
  149. return iso;
  150. err:
  151. hpsb_iso_shutdown(iso);
  152. return NULL;
  153. }
  154. int hpsb_iso_recv_listen_channel(struct hpsb_iso *iso, unsigned char channel)
  155. {
  156. if (iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64)
  157. return -EINVAL;
  158. return iso->host->driver->isoctl(iso, RECV_LISTEN_CHANNEL, channel);
  159. }
  160. int hpsb_iso_recv_unlisten_channel(struct hpsb_iso *iso, unsigned char channel)
  161. {
  162. if (iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64)
  163. return -EINVAL;
  164. return iso->host->driver->isoctl(iso, RECV_UNLISTEN_CHANNEL, channel);
  165. }
  166. int hpsb_iso_recv_set_channel_mask(struct hpsb_iso *iso, u64 mask)
  167. {
  168. if (iso->type != HPSB_ISO_RECV || iso->channel != -1)
  169. return -EINVAL;
  170. return iso->host->driver->isoctl(iso, RECV_SET_CHANNEL_MASK, (unsigned long) &mask);
  171. }
  172. int hpsb_iso_recv_flush(struct hpsb_iso *iso)
  173. {
  174. if (iso->type != HPSB_ISO_RECV)
  175. return -EINVAL;
  176. return iso->host->driver->isoctl(iso, RECV_FLUSH, 0);
  177. }
  178. static int do_iso_xmit_start(struct hpsb_iso *iso, int cycle)
  179. {
  180. int retval = iso->host->driver->isoctl(iso, XMIT_START, cycle);
  181. if (retval)
  182. return retval;
  183. iso->flags |= HPSB_ISO_DRIVER_STARTED;
  184. return retval;
  185. }
  186. int hpsb_iso_xmit_start(struct hpsb_iso *iso, int cycle, int prebuffer)
  187. {
  188. if (iso->type != HPSB_ISO_XMIT)
  189. return -1;
  190. if (iso->flags & HPSB_ISO_DRIVER_STARTED)
  191. return 0;
  192. if (cycle < -1)
  193. cycle = -1;
  194. else if (cycle >= 8000)
  195. cycle %= 8000;
  196. iso->xmit_cycle = cycle;
  197. if (prebuffer < 0)
  198. prebuffer = iso->buf_packets;
  199. else if (prebuffer == 0)
  200. prebuffer = 1;
  201. if (prebuffer > iso->buf_packets)
  202. prebuffer = iso->buf_packets;
  203. iso->prebuffer = prebuffer;
  204. /* remember the starting cycle; DMA will commence from xmit_queue_packets()
  205. once enough packets have been buffered */
  206. iso->start_cycle = cycle;
  207. return 0;
  208. }
  209. int hpsb_iso_recv_start(struct hpsb_iso *iso, int cycle, int tag_mask, int sync)
  210. {
  211. int retval = 0;
  212. int isoctl_args[3];
  213. if (iso->type != HPSB_ISO_RECV)
  214. return -1;
  215. if (iso->flags & HPSB_ISO_DRIVER_STARTED)
  216. return 0;
  217. if (cycle < -1)
  218. cycle = -1;
  219. else if (cycle >= 8000)
  220. cycle %= 8000;
  221. isoctl_args[0] = cycle;
  222. if (tag_mask < 0)
  223. /* match all tags */
  224. tag_mask = 0xF;
  225. isoctl_args[1] = tag_mask;
  226. isoctl_args[2] = sync;
  227. retval = iso->host->driver->isoctl(iso, RECV_START, (unsigned long) &isoctl_args[0]);
  228. if (retval)
  229. return retval;
  230. iso->flags |= HPSB_ISO_DRIVER_STARTED;
  231. return retval;
  232. }
  233. /* check to make sure the user has not supplied bogus values of offset/len
  234. that would cause the kernel to access memory outside the buffer */
  235. static int hpsb_iso_check_offset_len(struct hpsb_iso *iso,
  236. unsigned int offset, unsigned short len,
  237. unsigned int *out_offset, unsigned short *out_len)
  238. {
  239. if (offset >= iso->buf_size)
  240. return -EFAULT;
  241. /* make sure the packet does not go beyond the end of the buffer */
  242. if (offset + len > iso->buf_size)
  243. return -EFAULT;
  244. /* check for wrap-around */
  245. if (offset + len < offset)
  246. return -EFAULT;
  247. /* now we can trust 'offset' and 'length' */
  248. *out_offset = offset;
  249. *out_len = len;
  250. return 0;
  251. }
  252. int hpsb_iso_xmit_queue_packet(struct hpsb_iso *iso, u32 offset, u16 len, u8 tag, u8 sy)
  253. {
  254. struct hpsb_iso_packet_info *info;
  255. unsigned long flags;
  256. int rv;
  257. if (iso->type != HPSB_ISO_XMIT)
  258. return -EINVAL;
  259. /* is there space in the buffer? */
  260. if (iso->n_ready_packets <= 0) {
  261. return -EBUSY;
  262. }
  263. info = &iso->infos[iso->first_packet];
  264. /* check for bogus offset/length */
  265. if (hpsb_iso_check_offset_len(iso, offset, len, &info->offset, &info->len))
  266. return -EFAULT;
  267. info->tag = tag;
  268. info->sy = sy;
  269. spin_lock_irqsave(&iso->lock, flags);
  270. rv = iso->host->driver->isoctl(iso, XMIT_QUEUE, (unsigned long) info);
  271. if (rv)
  272. goto out;
  273. /* increment cursors */
  274. iso->first_packet = (iso->first_packet+1) % iso->buf_packets;
  275. iso->xmit_cycle = (iso->xmit_cycle+1) % 8000;
  276. iso->n_ready_packets--;
  277. if (iso->prebuffer != 0) {
  278. iso->prebuffer--;
  279. if (iso->prebuffer <= 0) {
  280. iso->prebuffer = 0;
  281. rv = do_iso_xmit_start(iso, iso->start_cycle);
  282. }
  283. }
  284. out:
  285. spin_unlock_irqrestore(&iso->lock, flags);
  286. return rv;
  287. }
  288. int hpsb_iso_xmit_sync(struct hpsb_iso *iso)
  289. {
  290. if (iso->type != HPSB_ISO_XMIT)
  291. return -EINVAL;
  292. return wait_event_interruptible(iso->waitq, hpsb_iso_n_ready(iso) == iso->buf_packets);
  293. }
  294. void hpsb_iso_packet_sent(struct hpsb_iso *iso, int cycle, int error)
  295. {
  296. unsigned long flags;
  297. spin_lock_irqsave(&iso->lock, flags);
  298. /* predict the cycle of the next packet to be queued */
  299. /* jump ahead by the number of packets that are already buffered */
  300. cycle += iso->buf_packets - iso->n_ready_packets;
  301. cycle %= 8000;
  302. iso->xmit_cycle = cycle;
  303. iso->n_ready_packets++;
  304. iso->pkt_dma = (iso->pkt_dma + 1) % iso->buf_packets;
  305. if (iso->n_ready_packets == iso->buf_packets || error != 0) {
  306. /* the buffer has run empty! */
  307. atomic_inc(&iso->overflows);
  308. }
  309. spin_unlock_irqrestore(&iso->lock, flags);
  310. }
  311. void hpsb_iso_packet_received(struct hpsb_iso *iso, u32 offset, u16 len,
  312. u16 cycle, u8 channel, u8 tag, u8 sy)
  313. {
  314. unsigned long flags;
  315. spin_lock_irqsave(&iso->lock, flags);
  316. if (iso->n_ready_packets == iso->buf_packets) {
  317. /* overflow! */
  318. atomic_inc(&iso->overflows);
  319. } else {
  320. struct hpsb_iso_packet_info *info = &iso->infos[iso->pkt_dma];
  321. info->offset = offset;
  322. info->len = len;
  323. info->cycle = cycle;
  324. info->channel = channel;
  325. info->tag = tag;
  326. info->sy = sy;
  327. iso->pkt_dma = (iso->pkt_dma+1) % iso->buf_packets;
  328. iso->n_ready_packets++;
  329. }
  330. spin_unlock_irqrestore(&iso->lock, flags);
  331. }
  332. int hpsb_iso_recv_release_packets(struct hpsb_iso *iso, unsigned int n_packets)
  333. {
  334. unsigned long flags;
  335. unsigned int i;
  336. int rv = 0;
  337. if (iso->type != HPSB_ISO_RECV)
  338. return -1;
  339. spin_lock_irqsave(&iso->lock, flags);
  340. for (i = 0; i < n_packets; i++) {
  341. rv = iso->host->driver->isoctl(iso, RECV_RELEASE,
  342. (unsigned long) &iso->infos[iso->first_packet]);
  343. if (rv)
  344. break;
  345. iso->first_packet = (iso->first_packet+1) % iso->buf_packets;
  346. iso->n_ready_packets--;
  347. }
  348. spin_unlock_irqrestore(&iso->lock, flags);
  349. return rv;
  350. }
  351. void hpsb_iso_wake(struct hpsb_iso *iso)
  352. {
  353. wake_up_interruptible(&iso->waitq);
  354. if (iso->callback)
  355. iso->callback(iso);
  356. }