Linux v2.6.15-rc7
[pohmelfs.git] / drivers / ieee1394 / iso.c
blob615541b8b90f5fa00c312e571a5fb6ac8d3aaa3f
1 /*
2 * IEEE 1394 for Linux
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>
14 #include "iso.h"
16 void hpsb_iso_stop(struct hpsb_iso *iso)
18 if (!(iso->flags & HPSB_ISO_DRIVER_STARTED))
19 return;
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) {
29 hpsb_iso_stop(iso);
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);
36 kfree(iso);
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,
42 int channel,
43 int dma_mode,
44 int irq_interval,
45 void (*callback)(struct hpsb_iso*))
47 struct hpsb_iso *iso;
48 int dma_direction;
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",
53 host->driver->name);
54 return NULL;
57 /* sanitize parameters */
59 if (buf_packets < 2)
60 buf_packets = 2;
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) || (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;
70 if (channel < -1 || channel >= 64)
71 return NULL;
73 /* channel = -1 is OK for multi-channel recv but not for xmit */
74 if (type == HPSB_ISO_XMIT && channel < 0)
75 return NULL;
77 /* allocate and write the struct hpsb_iso */
79 iso = kmalloc(sizeof(*iso) + buf_packets * sizeof(struct hpsb_iso_packet_info), GFP_KERNEL);
80 if (!iso)
81 return NULL;
83 iso->infos = (struct hpsb_iso_packet_info *)(iso + 1);
85 iso->type = type;
86 iso->host = host;
87 iso->hostdata = NULL;
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;
96 iso->pkt_dma = 0;
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;
103 } else {
104 iso->n_ready_packets = 0;
105 dma_direction = PCI_DMA_FROMDEVICE;
108 atomic_set(&iso->overflows, 0);
109 iso->bytes_discarded = 0;
110 iso->flags = 0;
111 iso->prebuffer = 0;
113 /* allocate the packet buffer */
114 if (dma_region_alloc(&iso->data_buf, iso->buf_size, host->pdev, dma_direction))
115 goto err;
117 return iso;
119 err:
120 hpsb_iso_shutdown(iso);
121 return NULL;
124 int hpsb_iso_n_ready(struct hpsb_iso* iso)
126 unsigned long flags;
127 int val;
129 spin_lock_irqsave(&iso->lock, flags);
130 val = iso->n_ready_packets;
131 spin_unlock_irqrestore(&iso->lock, flags);
133 return val;
137 struct hpsb_iso* hpsb_iso_xmit_init(struct hpsb_host *host,
138 unsigned int data_buf_size,
139 unsigned int buf_packets,
140 int channel,
141 int speed,
142 int irq_interval,
143 void (*callback)(struct hpsb_iso*))
145 struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_XMIT,
146 data_buf_size, buf_packets,
147 channel, HPSB_ISO_DMA_DEFAULT, irq_interval, callback);
148 if (!iso)
149 return NULL;
151 iso->speed = speed;
153 /* tell the driver to start working */
154 if (host->driver->isoctl(iso, XMIT_INIT, 0))
155 goto err;
157 iso->flags |= HPSB_ISO_DRIVER_INIT;
158 return iso;
160 err:
161 hpsb_iso_shutdown(iso);
162 return NULL;
165 struct hpsb_iso* hpsb_iso_recv_init(struct hpsb_host *host,
166 unsigned int data_buf_size,
167 unsigned int buf_packets,
168 int channel,
169 int dma_mode,
170 int irq_interval,
171 void (*callback)(struct hpsb_iso*))
173 struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_RECV,
174 data_buf_size, buf_packets,
175 channel, dma_mode, irq_interval, callback);
176 if (!iso)
177 return NULL;
179 /* tell the driver to start working */
180 if (host->driver->isoctl(iso, RECV_INIT, 0))
181 goto err;
183 iso->flags |= HPSB_ISO_DRIVER_INIT;
184 return iso;
186 err:
187 hpsb_iso_shutdown(iso);
188 return NULL;
191 int hpsb_iso_recv_listen_channel(struct hpsb_iso *iso, unsigned char channel)
193 if (iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64)
194 return -EINVAL;
195 return iso->host->driver->isoctl(iso, RECV_LISTEN_CHANNEL, channel);
198 int hpsb_iso_recv_unlisten_channel(struct hpsb_iso *iso, unsigned char channel)
200 if (iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64)
201 return -EINVAL;
202 return iso->host->driver->isoctl(iso, RECV_UNLISTEN_CHANNEL, channel);
205 int hpsb_iso_recv_set_channel_mask(struct hpsb_iso *iso, u64 mask)
207 if (iso->type != HPSB_ISO_RECV || iso->channel != -1)
208 return -EINVAL;
209 return iso->host->driver->isoctl(iso, RECV_SET_CHANNEL_MASK, (unsigned long) &mask);
212 int hpsb_iso_recv_flush(struct hpsb_iso *iso)
214 if (iso->type != HPSB_ISO_RECV)
215 return -EINVAL;
216 return iso->host->driver->isoctl(iso, RECV_FLUSH, 0);
219 static int do_iso_xmit_start(struct hpsb_iso *iso, int cycle)
221 int retval = iso->host->driver->isoctl(iso, XMIT_START, cycle);
222 if (retval)
223 return retval;
225 iso->flags |= HPSB_ISO_DRIVER_STARTED;
226 return retval;
229 int hpsb_iso_xmit_start(struct hpsb_iso *iso, int cycle, int prebuffer)
231 if (iso->type != HPSB_ISO_XMIT)
232 return -1;
234 if (iso->flags & HPSB_ISO_DRIVER_STARTED)
235 return 0;
237 if (cycle < -1)
238 cycle = -1;
239 else if (cycle >= 8000)
240 cycle %= 8000;
242 iso->xmit_cycle = cycle;
244 if (prebuffer < 0)
245 prebuffer = iso->buf_packets - 1;
246 else if (prebuffer == 0)
247 prebuffer = 1;
249 if (prebuffer >= iso->buf_packets)
250 prebuffer = iso->buf_packets - 1;
252 iso->prebuffer = prebuffer;
254 /* remember the starting cycle; DMA will commence from xmit_queue_packets()
255 once enough packets have been buffered */
256 iso->start_cycle = cycle;
258 return 0;
261 int hpsb_iso_recv_start(struct hpsb_iso *iso, int cycle, int tag_mask, int sync)
263 int retval = 0;
264 int isoctl_args[3];
266 if (iso->type != HPSB_ISO_RECV)
267 return -1;
269 if (iso->flags & HPSB_ISO_DRIVER_STARTED)
270 return 0;
272 if (cycle < -1)
273 cycle = -1;
274 else if (cycle >= 8000)
275 cycle %= 8000;
277 isoctl_args[0] = cycle;
279 if (tag_mask < 0)
280 /* match all tags */
281 tag_mask = 0xF;
282 isoctl_args[1] = tag_mask;
284 isoctl_args[2] = sync;
286 retval = iso->host->driver->isoctl(iso, RECV_START, (unsigned long) &isoctl_args[0]);
287 if (retval)
288 return retval;
290 iso->flags |= HPSB_ISO_DRIVER_STARTED;
291 return retval;
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 */
297 static int hpsb_iso_check_offset_len(struct hpsb_iso *iso,
298 unsigned int offset, unsigned short len,
299 unsigned int *out_offset, unsigned short *out_len)
301 if (offset >= iso->buf_size)
302 return -EFAULT;
304 /* make sure the packet does not go beyond the end of the buffer */
305 if (offset + len > iso->buf_size)
306 return -EFAULT;
308 /* check for wrap-around */
309 if (offset + len < offset)
310 return -EFAULT;
312 /* now we can trust 'offset' and 'length' */
313 *out_offset = offset;
314 *out_len = len;
316 return 0;
320 int hpsb_iso_xmit_queue_packet(struct hpsb_iso *iso, u32 offset, u16 len, u8 tag, u8 sy)
322 struct hpsb_iso_packet_info *info;
323 unsigned long flags;
324 int rv;
326 if (iso->type != HPSB_ISO_XMIT)
327 return -EINVAL;
329 /* is there space in the buffer? */
330 if (iso->n_ready_packets <= 0) {
331 return -EBUSY;
334 info = &iso->infos[iso->first_packet];
336 /* check for bogus offset/length */
337 if (hpsb_iso_check_offset_len(iso, offset, len, &info->offset, &info->len))
338 return -EFAULT;
340 info->tag = tag;
341 info->sy = sy;
343 spin_lock_irqsave(&iso->lock, flags);
345 rv = iso->host->driver->isoctl(iso, XMIT_QUEUE, (unsigned long) info);
346 if (rv)
347 goto out;
349 /* increment cursors */
350 iso->first_packet = (iso->first_packet+1) % iso->buf_packets;
351 iso->xmit_cycle = (iso->xmit_cycle+1) % 8000;
352 iso->n_ready_packets--;
354 if (iso->prebuffer != 0) {
355 iso->prebuffer--;
356 if (iso->prebuffer <= 0) {
357 iso->prebuffer = 0;
358 rv = do_iso_xmit_start(iso, iso->start_cycle);
362 out:
363 spin_unlock_irqrestore(&iso->lock, flags);
364 return rv;
367 int hpsb_iso_xmit_sync(struct hpsb_iso *iso)
369 if (iso->type != HPSB_ISO_XMIT)
370 return -EINVAL;
372 return wait_event_interruptible(iso->waitq, hpsb_iso_n_ready(iso) == iso->buf_packets);
375 void hpsb_iso_packet_sent(struct hpsb_iso *iso, int cycle, int error)
377 unsigned long flags;
378 spin_lock_irqsave(&iso->lock, flags);
380 /* 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;
386 iso->xmit_cycle = cycle;
387 iso->n_ready_packets++;
388 iso->pkt_dma = (iso->pkt_dma + 1) % iso->buf_packets;
390 if (iso->n_ready_packets == iso->buf_packets || error != 0) {
391 /* the buffer has run empty! */
392 atomic_inc(&iso->overflows);
395 spin_unlock_irqrestore(&iso->lock, flags);
398 void hpsb_iso_packet_received(struct hpsb_iso *iso, u32 offset, u16 len,
399 u16 total_len, u16 cycle, u8 channel, u8 tag, u8 sy)
401 unsigned long flags;
402 spin_lock_irqsave(&iso->lock, flags);
404 if (iso->n_ready_packets == iso->buf_packets) {
405 /* overflow! */
406 atomic_inc(&iso->overflows);
407 /* Record size of this discarded packet */
408 iso->bytes_discarded += total_len;
409 } else {
410 struct hpsb_iso_packet_info *info = &iso->infos[iso->pkt_dma];
411 info->offset = offset;
412 info->len = len;
413 info->total_len = total_len;
414 info->cycle = cycle;
415 info->channel = channel;
416 info->tag = tag;
417 info->sy = sy;
419 iso->pkt_dma = (iso->pkt_dma+1) % iso->buf_packets;
420 iso->n_ready_packets++;
423 spin_unlock_irqrestore(&iso->lock, flags);
426 int hpsb_iso_recv_release_packets(struct hpsb_iso *iso, unsigned int n_packets)
428 unsigned long flags;
429 unsigned int i;
430 int rv = 0;
432 if (iso->type != HPSB_ISO_RECV)
433 return -1;
435 spin_lock_irqsave(&iso->lock, flags);
436 for (i = 0; i < n_packets; i++) {
437 rv = iso->host->driver->isoctl(iso, RECV_RELEASE,
438 (unsigned long) &iso->infos[iso->first_packet]);
439 if (rv)
440 break;
442 iso->first_packet = (iso->first_packet+1) % iso->buf_packets;
443 iso->n_ready_packets--;
445 /* release memory from packets discarded when queue was full */
446 if (iso->n_ready_packets == 0) { /* Release only after all prior packets handled */
447 if (iso->bytes_discarded != 0) {
448 struct hpsb_iso_packet_info inf;
449 inf.total_len = iso->bytes_discarded;
450 iso->host->driver->isoctl(iso, RECV_RELEASE,
451 (unsigned long) &inf);
452 iso->bytes_discarded = 0;
456 spin_unlock_irqrestore(&iso->lock, flags);
457 return rv;
460 void hpsb_iso_wake(struct hpsb_iso *iso)
462 wake_up_interruptible(&iso->waitq);
464 if (iso->callback)
465 iso->callback(iso);