iso.c 13 KB

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