iso.c 11 KB

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