iso.c 13 KB

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