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/slab.h>
13 #include <linux/sched.h>
16 void hpsb_iso_stop(struct hpsb_iso *iso)
18 if (!(iso->flags & HPSB_ISO_DRIVER_STARTED))
21 iso->host->driver->isoctl(iso, iso->type == HPSB_ISO_XMIT ?
22 XMIT_STOP : RECV_STOP, 0);
23 iso->flags &= ~HPSB_ISO_DRIVER_STARTED;
26 void hpsb_iso_shutdown(struct hpsb_iso *iso)
28 if (iso->flags & HPSB_ISO_DRIVER_INIT) {
30 iso->host->driver->isoctl(iso, iso->type == HPSB_ISO_XMIT ?
31 XMIT_SHUTDOWN : RECV_SHUTDOWN, 0);
32 iso->flags &= ~HPSB_ISO_DRIVER_INIT;
35 dma_region_free(&iso->data_buf);
39 static struct hpsb_iso* hpsb_iso_common_init(struct hpsb_host *host, enum hpsb_iso_type type,
40 unsigned int data_buf_size,
41 unsigned int buf_packets,
45 void (*callback)(struct hpsb_iso*))
50 /* make sure driver supports the ISO API */
51 if (!host->driver->isoctl) {
52 printk(KERN_INFO "ieee1394: host driver '%s' does not support the rawiso API\n",
57 /* sanitize parameters */
62 if ((dma_mode < HPSB_ISO_DMA_DEFAULT) || (dma_mode > HPSB_ISO_DMA_PACKET_PER_BUFFER))
63 dma_mode=HPSB_ISO_DMA_DEFAULT;
65 if (irq_interval == 0) /* really interrupt for each packet*/
67 else if ((irq_interval < 0) || (irq_interval > buf_packets / 4))
68 irq_interval = buf_packets / 4;
70 if (channel < -1 || channel >= 64)
73 /* channel = -1 is OK for multi-channel recv but not for xmit */
74 if (type == HPSB_ISO_XMIT && channel < 0)
77 /* allocate and write the struct hpsb_iso */
79 iso = kmalloc(sizeof(*iso) + buf_packets * sizeof(struct hpsb_iso_packet_info), GFP_KERNEL);
83 iso->infos = (struct hpsb_iso_packet_info *)(iso + 1);
88 iso->callback = callback;
89 init_waitqueue_head(&iso->waitq);
90 iso->channel = channel;
91 iso->irq_interval = irq_interval;
92 iso->dma_mode = dma_mode;
93 dma_region_init(&iso->data_buf);
94 iso->buf_size = PAGE_ALIGN(data_buf_size);
95 iso->buf_packets = buf_packets;
97 iso->first_packet = 0;
98 spin_lock_init(&iso->lock);
100 if (iso->type == HPSB_ISO_XMIT) {
101 iso->n_ready_packets = iso->buf_packets;
102 dma_direction = PCI_DMA_TODEVICE;
104 iso->n_ready_packets = 0;
105 dma_direction = PCI_DMA_FROMDEVICE;
108 atomic_set(&iso->overflows, 0);
112 /* allocate the packet buffer */
113 if (dma_region_alloc(&iso->data_buf, iso->buf_size, host->pdev, dma_direction))
119 hpsb_iso_shutdown(iso);
123 int hpsb_iso_n_ready(struct hpsb_iso* iso)
128 spin_lock_irqsave(&iso->lock, flags);
129 val = iso->n_ready_packets;
130 spin_unlock_irqrestore(&iso->lock, flags);
136 struct hpsb_iso* hpsb_iso_xmit_init(struct hpsb_host *host,
137 unsigned int data_buf_size,
138 unsigned int buf_packets,
142 void (*callback)(struct hpsb_iso*))
144 struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_XMIT,
145 data_buf_size, buf_packets,
146 channel, HPSB_ISO_DMA_DEFAULT, irq_interval, callback);
152 /* tell the driver to start working */
153 if (host->driver->isoctl(iso, XMIT_INIT, 0))
156 iso->flags |= HPSB_ISO_DRIVER_INIT;
160 hpsb_iso_shutdown(iso);
164 struct hpsb_iso* hpsb_iso_recv_init(struct hpsb_host *host,
165 unsigned int data_buf_size,
166 unsigned int buf_packets,
170 void (*callback)(struct hpsb_iso*))
172 struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_RECV,
173 data_buf_size, buf_packets,
174 channel, dma_mode, irq_interval, callback);
178 /* tell the driver to start working */
179 if (host->driver->isoctl(iso, RECV_INIT, 0))
182 iso->flags |= HPSB_ISO_DRIVER_INIT;
186 hpsb_iso_shutdown(iso);
190 int hpsb_iso_recv_listen_channel(struct hpsb_iso *iso, unsigned char channel)
192 if (iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64)
194 return iso->host->driver->isoctl(iso, RECV_LISTEN_CHANNEL, channel);
197 int hpsb_iso_recv_unlisten_channel(struct hpsb_iso *iso, unsigned char channel)
199 if (iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64)
201 return iso->host->driver->isoctl(iso, RECV_UNLISTEN_CHANNEL, channel);
204 int hpsb_iso_recv_set_channel_mask(struct hpsb_iso *iso, u64 mask)
206 if (iso->type != HPSB_ISO_RECV || iso->channel != -1)
208 return iso->host->driver->isoctl(iso, RECV_SET_CHANNEL_MASK, (unsigned long) &mask);
211 int hpsb_iso_recv_flush(struct hpsb_iso *iso)
213 if (iso->type != HPSB_ISO_RECV)
215 return iso->host->driver->isoctl(iso, RECV_FLUSH, 0);
218 static int do_iso_xmit_start(struct hpsb_iso *iso, int cycle)
220 int retval = iso->host->driver->isoctl(iso, XMIT_START, cycle);
224 iso->flags |= HPSB_ISO_DRIVER_STARTED;
228 int hpsb_iso_xmit_start(struct hpsb_iso *iso, int cycle, int prebuffer)
230 if (iso->type != HPSB_ISO_XMIT)
233 if (iso->flags & HPSB_ISO_DRIVER_STARTED)
238 else if (cycle >= 8000)
241 iso->xmit_cycle = cycle;
244 prebuffer = iso->buf_packets;
245 else if (prebuffer == 0)
248 if (prebuffer > iso->buf_packets)
249 prebuffer = iso->buf_packets;
251 iso->prebuffer = prebuffer;
253 /* remember the starting cycle; DMA will commence from xmit_queue_packets()
254 once enough packets have been buffered */
255 iso->start_cycle = cycle;
260 int hpsb_iso_recv_start(struct hpsb_iso *iso, int cycle, int tag_mask, int sync)
265 if (iso->type != HPSB_ISO_RECV)
268 if (iso->flags & HPSB_ISO_DRIVER_STARTED)
273 else if (cycle >= 8000)
276 isoctl_args[0] = cycle;
281 isoctl_args[1] = tag_mask;
283 isoctl_args[2] = sync;
285 retval = iso->host->driver->isoctl(iso, RECV_START, (unsigned long) &isoctl_args[0]);
289 iso->flags |= HPSB_ISO_DRIVER_STARTED;
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 */
296 static int hpsb_iso_check_offset_len(struct hpsb_iso *iso,
297 unsigned int offset, unsigned short len,
298 unsigned int *out_offset, unsigned short *out_len)
300 if (offset >= iso->buf_size)
303 /* make sure the packet does not go beyond the end of the buffer */
304 if (offset + len > iso->buf_size)
307 /* check for wrap-around */
308 if (offset + len < offset)
311 /* now we can trust 'offset' and 'length' */
312 *out_offset = offset;
319 int hpsb_iso_xmit_queue_packet(struct hpsb_iso *iso, u32 offset, u16 len, u8 tag, u8 sy)
321 struct hpsb_iso_packet_info *info;
325 if (iso->type != HPSB_ISO_XMIT)
328 /* is there space in the buffer? */
329 if (iso->n_ready_packets <= 0) {
333 info = &iso->infos[iso->first_packet];
335 /* check for bogus offset/length */
336 if (hpsb_iso_check_offset_len(iso, offset, len, &info->offset, &info->len))
342 spin_lock_irqsave(&iso->lock, flags);
344 rv = iso->host->driver->isoctl(iso, XMIT_QUEUE, (unsigned long) info);
348 /* increment cursors */
349 iso->first_packet = (iso->first_packet+1) % iso->buf_packets;
350 iso->xmit_cycle = (iso->xmit_cycle+1) % 8000;
351 iso->n_ready_packets--;
353 if (iso->prebuffer != 0) {
355 if (iso->prebuffer <= 0) {
357 rv = do_iso_xmit_start(iso, iso->start_cycle);
362 spin_unlock_irqrestore(&iso->lock, flags);
366 int hpsb_iso_xmit_sync(struct hpsb_iso *iso)
368 if (iso->type != HPSB_ISO_XMIT)
371 return wait_event_interruptible(iso->waitq, hpsb_iso_n_ready(iso) == iso->buf_packets);
374 void hpsb_iso_packet_sent(struct hpsb_iso *iso, int cycle, int error)
377 spin_lock_irqsave(&iso->lock, flags);
379 /* 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;
385 iso->xmit_cycle = cycle;
386 iso->n_ready_packets++;
387 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);
394 spin_unlock_irqrestore(&iso->lock, flags);
397 void hpsb_iso_packet_received(struct hpsb_iso *iso, u32 offset, u16 len,
398 u16 cycle, u8 channel, u8 tag, u8 sy)
401 spin_lock_irqsave(&iso->lock, flags);
403 if (iso->n_ready_packets == iso->buf_packets) {
405 atomic_inc(&iso->overflows);
407 struct hpsb_iso_packet_info *info = &iso->infos[iso->pkt_dma];
408 info->offset = offset;
411 info->channel = channel;
415 iso->pkt_dma = (iso->pkt_dma+1) % iso->buf_packets;
416 iso->n_ready_packets++;
419 spin_unlock_irqrestore(&iso->lock, flags);
422 int hpsb_iso_recv_release_packets(struct hpsb_iso *iso, unsigned int n_packets)
428 if (iso->type != HPSB_ISO_RECV)
431 spin_lock_irqsave(&iso->lock, flags);
432 for (i = 0; i < n_packets; i++) {
433 rv = iso->host->driver->isoctl(iso, RECV_RELEASE,
434 (unsigned long) &iso->infos[iso->first_packet]);
438 iso->first_packet = (iso->first_packet+1) % iso->buf_packets;
439 iso->n_ready_packets--;
441 spin_unlock_irqrestore(&iso->lock, flags);
445 void hpsb_iso_wake(struct hpsb_iso *iso)
447 wake_up_interruptible(&iso->waitq);