iso.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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) || (irq_interval > buf_packets / 4))
  55. irq_interval = buf_packets / 4;
  56. if (irq_interval == 0) /* really interrupt for each packet*/
  57. irq_interval = 1;
  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->bytes_discarded = 0;
  91. iso->flags = 0;
  92. iso->prebuffer = 0;
  93. /* allocate the packet buffer */
  94. if (dma_region_alloc(&iso->data_buf, iso->buf_size, host->pdev, dma_direction))
  95. goto err;
  96. return iso;
  97. err:
  98. hpsb_iso_shutdown(iso);
  99. return NULL;
  100. }
  101. int hpsb_iso_n_ready(struct hpsb_iso* iso)
  102. {
  103. unsigned long flags;
  104. int val;
  105. spin_lock_irqsave(&iso->lock, flags);
  106. val = iso->n_ready_packets;
  107. spin_unlock_irqrestore(&iso->lock, flags);
  108. return val;
  109. }
  110. struct hpsb_iso* hpsb_iso_xmit_init(struct hpsb_host *host,
  111. unsigned int data_buf_size,
  112. unsigned int buf_packets,
  113. int channel,
  114. int speed,
  115. int irq_interval,
  116. void (*callback)(struct hpsb_iso*))
  117. {
  118. struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_XMIT,
  119. data_buf_size, buf_packets,
  120. channel, HPSB_ISO_DMA_DEFAULT, irq_interval, callback);
  121. if (!iso)
  122. return NULL;
  123. iso->speed = speed;
  124. /* tell the driver to start working */
  125. if (host->driver->isoctl(iso, XMIT_INIT, 0))
  126. goto err;
  127. iso->flags |= HPSB_ISO_DRIVER_INIT;
  128. return iso;
  129. err:
  130. hpsb_iso_shutdown(iso);
  131. return NULL;
  132. }
  133. struct hpsb_iso* hpsb_iso_recv_init(struct hpsb_host *host,
  134. unsigned int data_buf_size,
  135. unsigned int buf_packets,
  136. int channel,
  137. int dma_mode,
  138. int irq_interval,
  139. void (*callback)(struct hpsb_iso*))
  140. {
  141. struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_RECV,
  142. data_buf_size, buf_packets,
  143. channel, dma_mode, irq_interval, callback);
  144. if (!iso)
  145. return NULL;
  146. /* tell the driver to start working */
  147. if (host->driver->isoctl(iso, RECV_INIT, 0))
  148. goto err;
  149. iso->flags |= HPSB_ISO_DRIVER_INIT;
  150. return iso;
  151. err:
  152. hpsb_iso_shutdown(iso);
  153. return NULL;
  154. }
  155. int hpsb_iso_recv_listen_channel(struct hpsb_iso *iso, unsigned char channel)
  156. {
  157. if (iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64)
  158. return -EINVAL;
  159. return iso->host->driver->isoctl(iso, RECV_LISTEN_CHANNEL, channel);
  160. }
  161. int hpsb_iso_recv_unlisten_channel(struct hpsb_iso *iso, unsigned char channel)
  162. {
  163. if (iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64)
  164. return -EINVAL;
  165. return iso->host->driver->isoctl(iso, RECV_UNLISTEN_CHANNEL, channel);
  166. }
  167. int hpsb_iso_recv_set_channel_mask(struct hpsb_iso *iso, u64 mask)
  168. {
  169. if (iso->type != HPSB_ISO_RECV || iso->channel != -1)
  170. return -EINVAL;
  171. return iso->host->driver->isoctl(iso, RECV_SET_CHANNEL_MASK, (unsigned long) &mask);
  172. }
  173. int hpsb_iso_recv_flush(struct hpsb_iso *iso)
  174. {
  175. if (iso->type != HPSB_ISO_RECV)
  176. return -EINVAL;
  177. return iso->host->driver->isoctl(iso, RECV_FLUSH, 0);
  178. }
  179. static int do_iso_xmit_start(struct hpsb_iso *iso, int cycle)
  180. {
  181. int retval = iso->host->driver->isoctl(iso, XMIT_START, cycle);
  182. if (retval)
  183. return retval;
  184. iso->flags |= HPSB_ISO_DRIVER_STARTED;
  185. return retval;
  186. }
  187. int hpsb_iso_xmit_start(struct hpsb_iso *iso, int cycle, int prebuffer)
  188. {
  189. if (iso->type != HPSB_ISO_XMIT)
  190. return -1;
  191. if (iso->flags & HPSB_ISO_DRIVER_STARTED)
  192. return 0;
  193. if (cycle < -1)
  194. cycle = -1;
  195. else if (cycle >= 8000)
  196. cycle %= 8000;
  197. iso->xmit_cycle = cycle;
  198. if (prebuffer < 0)
  199. prebuffer = iso->buf_packets - 1;
  200. else if (prebuffer == 0)
  201. prebuffer = 1;
  202. if (prebuffer >= iso->buf_packets)
  203. prebuffer = iso->buf_packets - 1;
  204. iso->prebuffer = prebuffer;
  205. /* remember the starting cycle; DMA will commence from xmit_queue_packets()
  206. once enough packets have been buffered */
  207. iso->start_cycle = cycle;
  208. return 0;
  209. }
  210. int hpsb_iso_recv_start(struct hpsb_iso *iso, int cycle, int tag_mask, int sync)
  211. {
  212. int retval = 0;
  213. int isoctl_args[3];
  214. if (iso->type != HPSB_ISO_RECV)
  215. return -1;
  216. if (iso->flags & HPSB_ISO_DRIVER_STARTED)
  217. return 0;
  218. if (cycle < -1)
  219. cycle = -1;
  220. else if (cycle >= 8000)
  221. cycle %= 8000;
  222. isoctl_args[0] = cycle;
  223. if (tag_mask < 0)
  224. /* match all tags */
  225. tag_mask = 0xF;
  226. isoctl_args[1] = tag_mask;
  227. isoctl_args[2] = sync;
  228. retval = iso->host->driver->isoctl(iso, RECV_START, (unsigned long) &isoctl_args[0]);
  229. if (retval)
  230. return retval;
  231. iso->flags |= HPSB_ISO_DRIVER_STARTED;
  232. return retval;
  233. }
  234. /* check to make sure the user has not supplied bogus values of offset/len
  235. that would cause the kernel to access memory outside the buffer */
  236. static int hpsb_iso_check_offset_len(struct hpsb_iso *iso,
  237. unsigned int offset, unsigned short len,
  238. unsigned int *out_offset, unsigned short *out_len)
  239. {
  240. if (offset >= iso->buf_size)
  241. return -EFAULT;
  242. /* make sure the packet does not go beyond the end of the buffer */
  243. if (offset + len > iso->buf_size)
  244. return -EFAULT;
  245. /* check for wrap-around */
  246. if (offset + len < offset)
  247. return -EFAULT;
  248. /* now we can trust 'offset' and 'length' */
  249. *out_offset = offset;
  250. *out_len = len;
  251. return 0;
  252. }
  253. int hpsb_iso_xmit_queue_packet(struct hpsb_iso *iso, u32 offset, u16 len, u8 tag, u8 sy)
  254. {
  255. struct hpsb_iso_packet_info *info;
  256. unsigned long flags;
  257. int rv;
  258. if (iso->type != HPSB_ISO_XMIT)
  259. return -EINVAL;
  260. /* is there space in the buffer? */
  261. if (iso->n_ready_packets <= 0) {
  262. return -EBUSY;
  263. }
  264. info = &iso->infos[iso->first_packet];
  265. /* check for bogus offset/length */
  266. if (hpsb_iso_check_offset_len(iso, offset, len, &info->offset, &info->len))
  267. return -EFAULT;
  268. info->tag = tag;
  269. info->sy = sy;
  270. spin_lock_irqsave(&iso->lock, flags);
  271. rv = iso->host->driver->isoctl(iso, XMIT_QUEUE, (unsigned long) info);
  272. if (rv)
  273. goto out;
  274. /* increment cursors */
  275. iso->first_packet = (iso->first_packet+1) % iso->buf_packets;
  276. iso->xmit_cycle = (iso->xmit_cycle+1) % 8000;
  277. iso->n_ready_packets--;
  278. if (iso->prebuffer != 0) {
  279. iso->prebuffer--;
  280. if (iso->prebuffer <= 0) {
  281. iso->prebuffer = 0;
  282. rv = do_iso_xmit_start(iso, iso->start_cycle);
  283. }
  284. }
  285. out:
  286. spin_unlock_irqrestore(&iso->lock, flags);
  287. return rv;
  288. }
  289. int hpsb_iso_xmit_sync(struct hpsb_iso *iso)
  290. {
  291. if (iso->type != HPSB_ISO_XMIT)
  292. return -EINVAL;
  293. return wait_event_interruptible(iso->waitq, hpsb_iso_n_ready(iso) == iso->buf_packets);
  294. }
  295. void hpsb_iso_packet_sent(struct hpsb_iso *iso, int cycle, int error)
  296. {
  297. unsigned long flags;
  298. spin_lock_irqsave(&iso->lock, flags);
  299. /* predict the cycle of the next packet to be queued */
  300. /* jump ahead by the number of packets that are already buffered */
  301. cycle += iso->buf_packets - iso->n_ready_packets;
  302. cycle %= 8000;
  303. iso->xmit_cycle = cycle;
  304. iso->n_ready_packets++;
  305. iso->pkt_dma = (iso->pkt_dma + 1) % iso->buf_packets;
  306. if (iso->n_ready_packets == iso->buf_packets || error != 0) {
  307. /* the buffer has run empty! */
  308. atomic_inc(&iso->overflows);
  309. }
  310. spin_unlock_irqrestore(&iso->lock, flags);
  311. }
  312. void hpsb_iso_packet_received(struct hpsb_iso *iso, u32 offset, u16 len,
  313. u16 total_len, u16 cycle, u8 channel, u8 tag, u8 sy)
  314. {
  315. unsigned long flags;
  316. spin_lock_irqsave(&iso->lock, flags);
  317. if (iso->n_ready_packets == iso->buf_packets) {
  318. /* overflow! */
  319. atomic_inc(&iso->overflows);
  320. /* Record size of this discarded packet */
  321. iso->bytes_discarded += total_len;
  322. } else {
  323. struct hpsb_iso_packet_info *info = &iso->infos[iso->pkt_dma];
  324. info->offset = offset;
  325. info->len = len;
  326. info->total_len = total_len;
  327. info->cycle = cycle;
  328. info->channel = channel;
  329. info->tag = tag;
  330. info->sy = sy;
  331. iso->pkt_dma = (iso->pkt_dma+1) % iso->buf_packets;
  332. iso->n_ready_packets++;
  333. }
  334. spin_unlock_irqrestore(&iso->lock, flags);
  335. }
  336. int hpsb_iso_recv_release_packets(struct hpsb_iso *iso, unsigned int n_packets)
  337. {
  338. unsigned long flags;
  339. unsigned int i;
  340. int rv = 0;
  341. if (iso->type != HPSB_ISO_RECV)
  342. return -1;
  343. spin_lock_irqsave(&iso->lock, flags);
  344. for (i = 0; i < n_packets; i++) {
  345. rv = iso->host->driver->isoctl(iso, RECV_RELEASE,
  346. (unsigned long) &iso->infos[iso->first_packet]);
  347. if (rv)
  348. break;
  349. iso->first_packet = (iso->first_packet+1) % iso->buf_packets;
  350. iso->n_ready_packets--;
  351. /* release memory from packets discarded when queue was full */
  352. if (iso->n_ready_packets == 0) { /* Release only after all prior packets handled */
  353. if (iso->bytes_discarded != 0) {
  354. struct hpsb_iso_packet_info inf;
  355. inf.total_len = iso->bytes_discarded;
  356. iso->host->driver->isoctl(iso, RECV_RELEASE,
  357. (unsigned long) &inf);
  358. iso->bytes_discarded = 0;
  359. }
  360. }
  361. }
  362. spin_unlock_irqrestore(&iso->lock, flags);
  363. return rv;
  364. }
  365. void hpsb_iso_wake(struct hpsb_iso *iso)
  366. {
  367. wake_up_interruptible(&iso->waitq);
  368. if (iso->callback)
  369. iso->callback(iso);
  370. }