4 * kernel ISO transmission/reception
6 * Copyright (C) 2002 Maas Digital LLC
8 * This code is licensed under the GPL. See the file COPYING in the root
9 * directory of the kernel sources for details.
12 #include <linux/pci.h>
13 #include <linux/slab.h>
19 * hpsb_iso_stop - stop DMA
21 void hpsb_iso_stop(struct hpsb_iso *iso)
23 if (!(iso->flags & HPSB_ISO_DRIVER_STARTED))
26 iso->host->driver->isoctl(iso, iso->type == HPSB_ISO_XMIT ?
27 XMIT_STOP : RECV_STOP, 0);
28 iso->flags &= ~HPSB_ISO_DRIVER_STARTED;
32 * hpsb_iso_shutdown - deallocate buffer and DMA context
34 void hpsb_iso_shutdown(struct hpsb_iso *iso)
36 if (iso->flags & HPSB_ISO_DRIVER_INIT) {
38 iso->host->driver->isoctl(iso, iso->type == HPSB_ISO_XMIT ?
39 XMIT_SHUTDOWN : RECV_SHUTDOWN, 0);
40 iso->flags &= ~HPSB_ISO_DRIVER_INIT;
43 dma_region_free(&iso->data_buf);
47 static struct hpsb_iso *hpsb_iso_common_init(struct hpsb_host *host,
48 enum hpsb_iso_type type,
49 unsigned int data_buf_size,
50 unsigned int buf_packets,
51 int channel, int dma_mode,
53 void (*callback) (struct hpsb_iso
59 /* make sure driver supports the ISO API */
60 if (!host->driver->isoctl) {
62 "ieee1394: host driver '%s' does not support the rawiso API\n",
67 /* sanitize parameters */
72 if ((dma_mode < HPSB_ISO_DMA_DEFAULT)
73 || (dma_mode > HPSB_ISO_DMA_PACKET_PER_BUFFER))
74 dma_mode = HPSB_ISO_DMA_DEFAULT;
76 if ((irq_interval < 0) || (irq_interval > buf_packets / 4))
77 irq_interval = buf_packets / 4;
78 if (irq_interval == 0) /* really interrupt for each packet */
81 if (channel < -1 || channel >= 64)
84 /* channel = -1 is OK for multi-channel recv but not for xmit */
85 if (type == HPSB_ISO_XMIT && channel < 0)
88 /* allocate and write the struct hpsb_iso */
91 kmalloc(sizeof(*iso) +
92 buf_packets * sizeof(struct hpsb_iso_packet_info),
97 iso->infos = (struct hpsb_iso_packet_info *)(iso + 1);
101 iso->hostdata = NULL;
102 iso->callback = callback;
103 init_waitqueue_head(&iso->waitq);
104 iso->channel = channel;
105 iso->irq_interval = irq_interval;
106 iso->dma_mode = dma_mode;
107 dma_region_init(&iso->data_buf);
108 iso->buf_size = PAGE_ALIGN(data_buf_size);
109 iso->buf_packets = buf_packets;
111 iso->first_packet = 0;
112 spin_lock_init(&iso->lock);
114 if (iso->type == HPSB_ISO_XMIT) {
115 iso->n_ready_packets = iso->buf_packets;
116 dma_direction = PCI_DMA_TODEVICE;
118 iso->n_ready_packets = 0;
119 dma_direction = PCI_DMA_FROMDEVICE;
122 atomic_set(&iso->overflows, 0);
123 iso->bytes_discarded = 0;
127 /* allocate the packet buffer */
129 (&iso->data_buf, iso->buf_size, host->pdev, dma_direction))
135 hpsb_iso_shutdown(iso);
140 * hpsb_iso_n_ready - returns number of packets ready to send or receive
142 int hpsb_iso_n_ready(struct hpsb_iso *iso)
147 spin_lock_irqsave(&iso->lock, flags);
148 val = iso->n_ready_packets;
149 spin_unlock_irqrestore(&iso->lock, flags);
155 * hpsb_iso_xmit_init - allocate the buffer and DMA context
157 struct hpsb_iso *hpsb_iso_xmit_init(struct hpsb_host *host,
158 unsigned int data_buf_size,
159 unsigned int buf_packets,
163 void (*callback) (struct hpsb_iso *))
165 struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_XMIT,
166 data_buf_size, buf_packets,
168 HPSB_ISO_DMA_DEFAULT,
169 irq_interval, callback);
175 /* tell the driver to start working */
176 if (host->driver->isoctl(iso, XMIT_INIT, 0))
179 iso->flags |= HPSB_ISO_DRIVER_INIT;
183 hpsb_iso_shutdown(iso);
188 * hpsb_iso_recv_init - allocate the buffer and DMA context
190 * Note, if channel = -1, multi-channel receive is enabled.
192 struct hpsb_iso *hpsb_iso_recv_init(struct hpsb_host *host,
193 unsigned int data_buf_size,
194 unsigned int buf_packets,
198 void (*callback) (struct hpsb_iso *))
200 struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_RECV,
201 data_buf_size, buf_packets,
203 irq_interval, callback);
207 /* tell the driver to start working */
208 if (host->driver->isoctl(iso, RECV_INIT, 0))
211 iso->flags |= HPSB_ISO_DRIVER_INIT;
215 hpsb_iso_shutdown(iso);
220 * hpsb_iso_recv_listen_channel
224 int hpsb_iso_recv_listen_channel(struct hpsb_iso *iso, unsigned char channel)
226 if (iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64)
228 return iso->host->driver->isoctl(iso, RECV_LISTEN_CHANNEL, channel);
232 * hpsb_iso_recv_unlisten_channel
236 int hpsb_iso_recv_unlisten_channel(struct hpsb_iso *iso, unsigned char channel)
238 if (iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64)
240 return iso->host->driver->isoctl(iso, RECV_UNLISTEN_CHANNEL, channel);
244 * hpsb_iso_recv_set_channel_mask
248 int hpsb_iso_recv_set_channel_mask(struct hpsb_iso *iso, u64 mask)
250 if (iso->type != HPSB_ISO_RECV || iso->channel != -1)
252 return iso->host->driver->isoctl(iso, RECV_SET_CHANNEL_MASK,
253 (unsigned long)&mask);
257 * hpsb_iso_recv_flush - check for arrival of new packets
259 * check for arrival of new packets immediately (even if irq_interval
260 * has not yet been reached)
262 int hpsb_iso_recv_flush(struct hpsb_iso *iso)
264 if (iso->type != HPSB_ISO_RECV)
266 return iso->host->driver->isoctl(iso, RECV_FLUSH, 0);
269 static int do_iso_xmit_start(struct hpsb_iso *iso, int cycle)
271 int retval = iso->host->driver->isoctl(iso, XMIT_START, cycle);
275 iso->flags |= HPSB_ISO_DRIVER_STARTED;
280 * hpsb_iso_xmit_start - start DMA
282 int hpsb_iso_xmit_start(struct hpsb_iso *iso, int cycle, int prebuffer)
284 if (iso->type != HPSB_ISO_XMIT)
287 if (iso->flags & HPSB_ISO_DRIVER_STARTED)
292 else if (cycle >= 8000)
295 iso->xmit_cycle = cycle;
298 prebuffer = iso->buf_packets - 1;
299 else if (prebuffer == 0)
302 if (prebuffer >= iso->buf_packets)
303 prebuffer = iso->buf_packets - 1;
305 iso->prebuffer = prebuffer;
307 /* remember the starting cycle; DMA will commence from xmit_queue_packets()
308 once enough packets have been buffered */
309 iso->start_cycle = cycle;
315 * hpsb_iso_recv_start - start DMA
317 int hpsb_iso_recv_start(struct hpsb_iso *iso, int cycle, int tag_mask, int sync)
322 if (iso->type != HPSB_ISO_RECV)
325 if (iso->flags & HPSB_ISO_DRIVER_STARTED)
330 else if (cycle >= 8000)
333 isoctl_args[0] = cycle;
338 isoctl_args[1] = tag_mask;
340 isoctl_args[2] = sync;
343 iso->host->driver->isoctl(iso, RECV_START,
344 (unsigned long)&isoctl_args[0]);
348 iso->flags |= HPSB_ISO_DRIVER_STARTED;
352 /* check to make sure the user has not supplied bogus values of offset/len
353 * that would cause the kernel to access memory outside the buffer */
354 static int hpsb_iso_check_offset_len(struct hpsb_iso *iso,
355 unsigned int offset, unsigned short len,
356 unsigned int *out_offset,
357 unsigned short *out_len)
359 if (offset >= iso->buf_size)
362 /* make sure the packet does not go beyond the end of the buffer */
363 if (offset + len > iso->buf_size)
366 /* check for wrap-around */
367 if (offset + len < offset)
370 /* now we can trust 'offset' and 'length' */
371 *out_offset = offset;
378 * hpsb_iso_xmit_queue_packet - queue a packet for transmission.
380 * @offset is relative to the beginning of the DMA buffer, where the packet's
381 * data payload should already have been placed.
383 int hpsb_iso_xmit_queue_packet(struct hpsb_iso *iso, u32 offset, u16 len,
386 struct hpsb_iso_packet_info *info;
390 if (iso->type != HPSB_ISO_XMIT)
393 /* is there space in the buffer? */
394 if (iso->n_ready_packets <= 0) {
398 info = &iso->infos[iso->first_packet];
400 /* check for bogus offset/length */
401 if (hpsb_iso_check_offset_len
402 (iso, offset, len, &info->offset, &info->len))
408 spin_lock_irqsave(&iso->lock, flags);
410 rv = iso->host->driver->isoctl(iso, XMIT_QUEUE, (unsigned long)info);
414 /* increment cursors */
415 iso->first_packet = (iso->first_packet + 1) % iso->buf_packets;
416 iso->xmit_cycle = (iso->xmit_cycle + 1) % 8000;
417 iso->n_ready_packets--;
419 if (iso->prebuffer != 0) {
421 if (iso->prebuffer <= 0) {
423 rv = do_iso_xmit_start(iso, iso->start_cycle);
428 spin_unlock_irqrestore(&iso->lock, flags);
433 * hpsb_iso_xmit_sync - wait until all queued packets have been transmitted
435 int hpsb_iso_xmit_sync(struct hpsb_iso *iso)
437 if (iso->type != HPSB_ISO_XMIT)
440 return wait_event_interruptible(iso->waitq,
441 hpsb_iso_n_ready(iso) ==
446 * hpsb_iso_packet_sent
448 * Available to low-level drivers.
450 * Call after a packet has been transmitted to the bus (interrupt context is
451 * OK). @cycle is the _exact_ cycle the packet was sent on. @error should be
452 * non-zero if some sort of error occurred when sending the packet.
454 void hpsb_iso_packet_sent(struct hpsb_iso *iso, int cycle, int error)
457 spin_lock_irqsave(&iso->lock, flags);
459 /* predict the cycle of the next packet to be queued */
461 /* jump ahead by the number of packets that are already buffered */
462 cycle += iso->buf_packets - iso->n_ready_packets;
465 iso->xmit_cycle = cycle;
466 iso->n_ready_packets++;
467 iso->pkt_dma = (iso->pkt_dma + 1) % iso->buf_packets;
469 if (iso->n_ready_packets == iso->buf_packets || error != 0) {
470 /* the buffer has run empty! */
471 atomic_inc(&iso->overflows);
474 spin_unlock_irqrestore(&iso->lock, flags);
478 * hpsb_iso_packet_received
480 * Available to low-level drivers.
482 * Call after a packet has been received (interrupt context is OK).
484 void hpsb_iso_packet_received(struct hpsb_iso *iso, u32 offset, u16 len,
485 u16 total_len, u16 cycle, u8 channel, u8 tag,
489 spin_lock_irqsave(&iso->lock, flags);
491 if (iso->n_ready_packets == iso->buf_packets) {
493 atomic_inc(&iso->overflows);
494 /* Record size of this discarded packet */
495 iso->bytes_discarded += total_len;
497 struct hpsb_iso_packet_info *info = &iso->infos[iso->pkt_dma];
498 info->offset = offset;
500 info->total_len = total_len;
502 info->channel = channel;
506 iso->pkt_dma = (iso->pkt_dma + 1) % iso->buf_packets;
507 iso->n_ready_packets++;
510 spin_unlock_irqrestore(&iso->lock, flags);
514 * hpsb_iso_recv_release_packets - release packets, reuse buffer
516 * @n_packets have been read out of the buffer, re-use the buffer space
518 int hpsb_iso_recv_release_packets(struct hpsb_iso *iso, unsigned int n_packets)
524 if (iso->type != HPSB_ISO_RECV)
527 spin_lock_irqsave(&iso->lock, flags);
528 for (i = 0; i < n_packets; i++) {
529 rv = iso->host->driver->isoctl(iso, RECV_RELEASE,
530 (unsigned long)&iso->infos[iso->
535 iso->first_packet = (iso->first_packet + 1) % iso->buf_packets;
536 iso->n_ready_packets--;
538 /* release memory from packets discarded when queue was full */
539 if (iso->n_ready_packets == 0) { /* Release only after all prior packets handled */
540 if (iso->bytes_discarded != 0) {
541 struct hpsb_iso_packet_info inf;
542 inf.total_len = iso->bytes_discarded;
543 iso->host->driver->isoctl(iso, RECV_RELEASE,
544 (unsigned long)&inf);
545 iso->bytes_discarded = 0;
549 spin_unlock_irqrestore(&iso->lock, flags);
556 * Available to low-level drivers.
558 * Call to wake waiting processes after buffer space has opened up.
560 void hpsb_iso_wake(struct hpsb_iso *iso)
562 wake_up_interruptible(&iso->waitq);