iso.c 11 KB

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