iso.c 13 KB

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