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,
44 void (*callback)(struct hpsb_iso*))
49 /* make sure driver supports the ISO API */
50 if (!host->driver->isoctl) {
51 printk(KERN_INFO "ieee1394: host driver '%s' does not support the rawiso API\n",
56 /* sanitize parameters */
61 if (irq_interval < 1 || irq_interval > buf_packets / 2)
62 irq_interval = buf_packets / 2;
64 if (channel < -1 || channel >= 64)
67 /* channel = -1 is OK for multi-channel recv but not for xmit */
68 if (type == HPSB_ISO_XMIT && channel < 0)
71 /* allocate and write the struct hpsb_iso */
73 iso = kmalloc(sizeof(*iso) + buf_packets * sizeof(struct hpsb_iso_packet_info), GFP_KERNEL);
77 iso->infos = (struct hpsb_iso_packet_info *)(iso + 1);
82 iso->callback = callback;
83 init_waitqueue_head(&iso->waitq);
84 iso->channel = channel;
85 iso->irq_interval = irq_interval;
86 dma_region_init(&iso->data_buf);
87 iso->buf_size = round_up_to_page(data_buf_size);
88 iso->buf_packets = buf_packets;
90 iso->first_packet = 0;
91 spin_lock_init(&iso->lock);
93 if (iso->type == HPSB_ISO_XMIT) {
94 iso->n_ready_packets = iso->buf_packets;
95 dma_direction = PCI_DMA_TODEVICE;
97 iso->n_ready_packets = 0;
98 dma_direction = PCI_DMA_FROMDEVICE;
101 atomic_set(&iso->overflows, 0);
105 /* allocate the packet buffer */
106 if (dma_region_alloc(&iso->data_buf, iso->buf_size, host->pdev, dma_direction))
112 hpsb_iso_shutdown(iso);
116 int hpsb_iso_n_ready(struct hpsb_iso* iso)
121 spin_lock_irqsave(&iso->lock, flags);
122 val = iso->n_ready_packets;
123 spin_unlock_irqrestore(&iso->lock, flags);
129 struct hpsb_iso* hpsb_iso_xmit_init(struct hpsb_host *host,
130 unsigned int data_buf_size,
131 unsigned int buf_packets,
135 void (*callback)(struct hpsb_iso*))
137 struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_XMIT,
138 data_buf_size, buf_packets,
139 channel, irq_interval, callback);
145 /* tell the driver to start working */
146 if (host->driver->isoctl(iso, XMIT_INIT, 0))
149 iso->flags |= HPSB_ISO_DRIVER_INIT;
153 hpsb_iso_shutdown(iso);
157 struct hpsb_iso* hpsb_iso_recv_init(struct hpsb_host *host,
158 unsigned int data_buf_size,
159 unsigned int buf_packets,
162 void (*callback)(struct hpsb_iso*))
164 struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_RECV,
165 data_buf_size, buf_packets,
166 channel, irq_interval, callback);
170 /* tell the driver to start working */
171 if (host->driver->isoctl(iso, RECV_INIT, 0))
174 iso->flags |= HPSB_ISO_DRIVER_INIT;
178 hpsb_iso_shutdown(iso);
182 int hpsb_iso_recv_listen_channel(struct hpsb_iso *iso, unsigned char channel)
184 if (iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64)
186 return iso->host->driver->isoctl(iso, RECV_LISTEN_CHANNEL, channel);
189 int hpsb_iso_recv_unlisten_channel(struct hpsb_iso *iso, unsigned char channel)
191 if (iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64)
193 return iso->host->driver->isoctl(iso, RECV_UNLISTEN_CHANNEL, channel);
196 int hpsb_iso_recv_set_channel_mask(struct hpsb_iso *iso, u64 mask)
198 if (iso->type != HPSB_ISO_RECV || iso->channel != -1)
200 return iso->host->driver->isoctl(iso, RECV_SET_CHANNEL_MASK, (unsigned long) &mask);
203 int hpsb_iso_recv_flush(struct hpsb_iso *iso)
205 if (iso->type != HPSB_ISO_RECV)
207 return iso->host->driver->isoctl(iso, RECV_FLUSH, 0);
210 static int do_iso_xmit_start(struct hpsb_iso *iso, int cycle)
212 int retval = iso->host->driver->isoctl(iso, XMIT_START, cycle);
216 iso->flags |= HPSB_ISO_DRIVER_STARTED;
220 int hpsb_iso_xmit_start(struct hpsb_iso *iso, int cycle, int prebuffer)
222 if (iso->type != HPSB_ISO_XMIT)
225 if (iso->flags & HPSB_ISO_DRIVER_STARTED)
230 else if (cycle >= 8000)
233 iso->xmit_cycle = cycle;
236 prebuffer = iso->buf_packets;
237 else if (prebuffer == 0)
240 if (prebuffer > iso->buf_packets)
241 prebuffer = iso->buf_packets;
243 iso->prebuffer = prebuffer;
245 /* remember the starting cycle; DMA will commence from xmit_queue_packets()
246 once enough packets have been buffered */
247 iso->start_cycle = cycle;
252 int hpsb_iso_recv_start(struct hpsb_iso *iso, int cycle, int tag_mask, int sync)
257 if (iso->type != HPSB_ISO_RECV)
260 if (iso->flags & HPSB_ISO_DRIVER_STARTED)
265 else if (cycle >= 8000)
268 isoctl_args[0] = cycle;
273 isoctl_args[1] = tag_mask;
275 isoctl_args[2] = sync;
277 retval = iso->host->driver->isoctl(iso, RECV_START, (unsigned long) &isoctl_args[0]);
281 iso->flags |= HPSB_ISO_DRIVER_STARTED;
285 /* check to make sure the user has not supplied bogus values of offset/len
286 that would cause the kernel to access memory outside the buffer */
288 static int hpsb_iso_check_offset_len(struct hpsb_iso *iso,
289 unsigned int offset, unsigned short len,
290 unsigned int *out_offset, unsigned short *out_len)
292 if (offset >= iso->buf_size)
295 /* make sure the packet does not go beyond the end of the buffer */
296 if (offset + len > iso->buf_size)
299 /* check for wrap-around */
300 if (offset + len < offset)
303 /* now we can trust 'offset' and 'length' */
304 *out_offset = offset;
311 int hpsb_iso_xmit_queue_packet(struct hpsb_iso *iso, u32 offset, u16 len, u8 tag, u8 sy)
313 struct hpsb_iso_packet_info *info;
317 if (iso->type != HPSB_ISO_XMIT)
320 /* is there space in the buffer? */
321 if (iso->n_ready_packets <= 0) {
325 info = &iso->infos[iso->first_packet];
327 /* check for bogus offset/length */
328 if (hpsb_iso_check_offset_len(iso, offset, len, &info->offset, &info->len))
334 spin_lock_irqsave(&iso->lock, flags);
336 rv = iso->host->driver->isoctl(iso, XMIT_QUEUE, (unsigned long) info);
340 /* increment cursors */
341 iso->first_packet = (iso->first_packet+1) % iso->buf_packets;
342 iso->xmit_cycle = (iso->xmit_cycle+1) % 8000;
343 iso->n_ready_packets--;
345 if (iso->prebuffer != 0) {
347 if (iso->prebuffer <= 0) {
349 rv = do_iso_xmit_start(iso, iso->start_cycle);
354 spin_unlock_irqrestore(&iso->lock, flags);
358 int hpsb_iso_xmit_sync(struct hpsb_iso *iso)
360 if (iso->type != HPSB_ISO_XMIT)
363 return wait_event_interruptible(iso->waitq, hpsb_iso_n_ready(iso) == iso->buf_packets);
366 void hpsb_iso_packet_sent(struct hpsb_iso *iso, int cycle, int error)
369 spin_lock_irqsave(&iso->lock, flags);
371 /* predict the cycle of the next packet to be queued */
373 /* jump ahead by the number of packets that are already buffered */
374 cycle += iso->buf_packets - iso->n_ready_packets;
377 iso->xmit_cycle = cycle;
378 iso->n_ready_packets++;
379 iso->pkt_dma = (iso->pkt_dma + 1) % iso->buf_packets;
381 if (iso->n_ready_packets == iso->buf_packets || error != 0) {
382 /* the buffer has run empty! */
383 atomic_inc(&iso->overflows);
386 spin_unlock_irqrestore(&iso->lock, flags);
389 void hpsb_iso_packet_received(struct hpsb_iso *iso, u32 offset, u16 len,
390 u16 cycle, u8 channel, u8 tag, u8 sy)
393 spin_lock_irqsave(&iso->lock, flags);
395 if (iso->n_ready_packets == iso->buf_packets) {
397 atomic_inc(&iso->overflows);
399 struct hpsb_iso_packet_info *info = &iso->infos[iso->pkt_dma];
400 info->offset = offset;
403 info->channel = channel;
407 iso->pkt_dma = (iso->pkt_dma+1) % iso->buf_packets;
408 iso->n_ready_packets++;
411 spin_unlock_irqrestore(&iso->lock, flags);
414 int hpsb_iso_recv_release_packets(struct hpsb_iso *iso, unsigned int n_packets)
420 if (iso->type != HPSB_ISO_RECV)
423 spin_lock_irqsave(&iso->lock, flags);
424 for (i = 0; i < n_packets; i++) {
425 rv = iso->host->driver->isoctl(iso, RECV_RELEASE,
426 (unsigned long) &iso->infos[iso->first_packet]);
430 iso->first_packet = (iso->first_packet+1) % iso->buf_packets;
431 iso->n_ready_packets--;
433 spin_unlock_irqrestore(&iso->lock, flags);
437 void hpsb_iso_wake(struct hpsb_iso *iso)
439 wake_up_interruptible(&iso->waitq);