iso.c 11 KB

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