[PATCH] w1: Make w1 connector notifications depend on connector.
[linux-2.6/verdex.git] / drivers / ieee1394 / iso.c
blobf26680ebef7cdcd5387627a98c79120d48dc77bb
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,
40 enum hpsb_iso_type type,
41 unsigned int data_buf_size,
42 unsigned int buf_packets,
43 int channel, int dma_mode,
44 int irq_interval,
45 void (*callback) (struct hpsb_iso
46 *))
48 struct hpsb_iso *iso;
49 int dma_direction;
51 /* make sure driver supports the ISO API */
52 if (!host->driver->isoctl) {
53 printk(KERN_INFO
54 "ieee1394: host driver '%s' does not support the rawiso API\n",
55 host->driver->name);
56 return NULL;
59 /* sanitize parameters */
61 if (buf_packets < 2)
62 buf_packets = 2;
64 if ((dma_mode < HPSB_ISO_DMA_DEFAULT)
65 || (dma_mode > HPSB_ISO_DMA_PACKET_PER_BUFFER))
66 dma_mode = HPSB_ISO_DMA_DEFAULT;
68 if ((irq_interval < 0) || (irq_interval > buf_packets / 4))
69 irq_interval = buf_packets / 4;
70 if (irq_interval == 0) /* really interrupt for each packet */
71 irq_interval = 1;
73 if (channel < -1 || channel >= 64)
74 return NULL;
76 /* channel = -1 is OK for multi-channel recv but not for xmit */
77 if (type == HPSB_ISO_XMIT && channel < 0)
78 return NULL;
80 /* allocate and write the struct hpsb_iso */
82 iso =
83 kmalloc(sizeof(*iso) +
84 buf_packets * sizeof(struct hpsb_iso_packet_info),
85 GFP_KERNEL);
86 if (!iso)
87 return NULL;
89 iso->infos = (struct hpsb_iso_packet_info *)(iso + 1);
91 iso->type = type;
92 iso->host = host;
93 iso->hostdata = NULL;
94 iso->callback = callback;
95 init_waitqueue_head(&iso->waitq);
96 iso->channel = channel;
97 iso->irq_interval = irq_interval;
98 iso->dma_mode = dma_mode;
99 dma_region_init(&iso->data_buf);
100 iso->buf_size = PAGE_ALIGN(data_buf_size);
101 iso->buf_packets = buf_packets;
102 iso->pkt_dma = 0;
103 iso->first_packet = 0;
104 spin_lock_init(&iso->lock);
106 if (iso->type == HPSB_ISO_XMIT) {
107 iso->n_ready_packets = iso->buf_packets;
108 dma_direction = PCI_DMA_TODEVICE;
109 } else {
110 iso->n_ready_packets = 0;
111 dma_direction = PCI_DMA_FROMDEVICE;
114 atomic_set(&iso->overflows, 0);
115 iso->bytes_discarded = 0;
116 iso->flags = 0;
117 iso->prebuffer = 0;
119 /* allocate the packet buffer */
120 if (dma_region_alloc
121 (&iso->data_buf, iso->buf_size, host->pdev, dma_direction))
122 goto err;
124 return iso;
126 err:
127 hpsb_iso_shutdown(iso);
128 return NULL;
131 int hpsb_iso_n_ready(struct hpsb_iso *iso)
133 unsigned long flags;
134 int val;
136 spin_lock_irqsave(&iso->lock, flags);
137 val = iso->n_ready_packets;
138 spin_unlock_irqrestore(&iso->lock, flags);
140 return val;
143 struct hpsb_iso *hpsb_iso_xmit_init(struct hpsb_host *host,
144 unsigned int data_buf_size,
145 unsigned int buf_packets,
146 int channel,
147 int speed,
148 int irq_interval,
149 void (*callback) (struct hpsb_iso *))
151 struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_XMIT,
152 data_buf_size, buf_packets,
153 channel,
154 HPSB_ISO_DMA_DEFAULT,
155 irq_interval, callback);
156 if (!iso)
157 return NULL;
159 iso->speed = speed;
161 /* tell the driver to start working */
162 if (host->driver->isoctl(iso, XMIT_INIT, 0))
163 goto err;
165 iso->flags |= HPSB_ISO_DRIVER_INIT;
166 return iso;
168 err:
169 hpsb_iso_shutdown(iso);
170 return NULL;
173 struct hpsb_iso *hpsb_iso_recv_init(struct hpsb_host *host,
174 unsigned int data_buf_size,
175 unsigned int buf_packets,
176 int channel,
177 int dma_mode,
178 int irq_interval,
179 void (*callback) (struct hpsb_iso *))
181 struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_RECV,
182 data_buf_size, buf_packets,
183 channel, dma_mode,
184 irq_interval, callback);
185 if (!iso)
186 return NULL;
188 /* tell the driver to start working */
189 if (host->driver->isoctl(iso, RECV_INIT, 0))
190 goto err;
192 iso->flags |= HPSB_ISO_DRIVER_INIT;
193 return iso;
195 err:
196 hpsb_iso_shutdown(iso);
197 return NULL;
200 int hpsb_iso_recv_listen_channel(struct hpsb_iso *iso, unsigned char channel)
202 if (iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64)
203 return -EINVAL;
204 return iso->host->driver->isoctl(iso, RECV_LISTEN_CHANNEL, channel);
207 int hpsb_iso_recv_unlisten_channel(struct hpsb_iso *iso, unsigned char channel)
209 if (iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64)
210 return -EINVAL;
211 return iso->host->driver->isoctl(iso, RECV_UNLISTEN_CHANNEL, channel);
214 int hpsb_iso_recv_set_channel_mask(struct hpsb_iso *iso, u64 mask)
216 if (iso->type != HPSB_ISO_RECV || iso->channel != -1)
217 return -EINVAL;
218 return iso->host->driver->isoctl(iso, RECV_SET_CHANNEL_MASK,
219 (unsigned long)&mask);
222 int hpsb_iso_recv_flush(struct hpsb_iso *iso)
224 if (iso->type != HPSB_ISO_RECV)
225 return -EINVAL;
226 return iso->host->driver->isoctl(iso, RECV_FLUSH, 0);
229 static int do_iso_xmit_start(struct hpsb_iso *iso, int cycle)
231 int retval = iso->host->driver->isoctl(iso, XMIT_START, cycle);
232 if (retval)
233 return retval;
235 iso->flags |= HPSB_ISO_DRIVER_STARTED;
236 return retval;
239 int hpsb_iso_xmit_start(struct hpsb_iso *iso, int cycle, int prebuffer)
241 if (iso->type != HPSB_ISO_XMIT)
242 return -1;
244 if (iso->flags & HPSB_ISO_DRIVER_STARTED)
245 return 0;
247 if (cycle < -1)
248 cycle = -1;
249 else if (cycle >= 8000)
250 cycle %= 8000;
252 iso->xmit_cycle = cycle;
254 if (prebuffer < 0)
255 prebuffer = iso->buf_packets - 1;
256 else if (prebuffer == 0)
257 prebuffer = 1;
259 if (prebuffer >= iso->buf_packets)
260 prebuffer = iso->buf_packets - 1;
262 iso->prebuffer = prebuffer;
264 /* remember the starting cycle; DMA will commence from xmit_queue_packets()
265 once enough packets have been buffered */
266 iso->start_cycle = cycle;
268 return 0;
271 int hpsb_iso_recv_start(struct hpsb_iso *iso, int cycle, int tag_mask, int sync)
273 int retval = 0;
274 int isoctl_args[3];
276 if (iso->type != HPSB_ISO_RECV)
277 return -1;
279 if (iso->flags & HPSB_ISO_DRIVER_STARTED)
280 return 0;
282 if (cycle < -1)
283 cycle = -1;
284 else if (cycle >= 8000)
285 cycle %= 8000;
287 isoctl_args[0] = cycle;
289 if (tag_mask < 0)
290 /* match all tags */
291 tag_mask = 0xF;
292 isoctl_args[1] = tag_mask;
294 isoctl_args[2] = sync;
296 retval =
297 iso->host->driver->isoctl(iso, RECV_START,
298 (unsigned long)&isoctl_args[0]);
299 if (retval)
300 return retval;
302 iso->flags |= HPSB_ISO_DRIVER_STARTED;
303 return retval;
306 /* check to make sure the user has not supplied bogus values of offset/len
307 that would cause the kernel to access memory outside the buffer */
309 static int hpsb_iso_check_offset_len(struct hpsb_iso *iso,
310 unsigned int offset, unsigned short len,
311 unsigned int *out_offset,
312 unsigned short *out_len)
314 if (offset >= iso->buf_size)
315 return -EFAULT;
317 /* make sure the packet does not go beyond the end of the buffer */
318 if (offset + len > iso->buf_size)
319 return -EFAULT;
321 /* check for wrap-around */
322 if (offset + len < offset)
323 return -EFAULT;
325 /* now we can trust 'offset' and 'length' */
326 *out_offset = offset;
327 *out_len = len;
329 return 0;
332 int hpsb_iso_xmit_queue_packet(struct hpsb_iso *iso, u32 offset, u16 len,
333 u8 tag, u8 sy)
335 struct hpsb_iso_packet_info *info;
336 unsigned long flags;
337 int rv;
339 if (iso->type != HPSB_ISO_XMIT)
340 return -EINVAL;
342 /* is there space in the buffer? */
343 if (iso->n_ready_packets <= 0) {
344 return -EBUSY;
347 info = &iso->infos[iso->first_packet];
349 /* check for bogus offset/length */
350 if (hpsb_iso_check_offset_len
351 (iso, offset, len, &info->offset, &info->len))
352 return -EFAULT;
354 info->tag = tag;
355 info->sy = sy;
357 spin_lock_irqsave(&iso->lock, flags);
359 rv = iso->host->driver->isoctl(iso, XMIT_QUEUE, (unsigned long)info);
360 if (rv)
361 goto out;
363 /* increment cursors */
364 iso->first_packet = (iso->first_packet + 1) % iso->buf_packets;
365 iso->xmit_cycle = (iso->xmit_cycle + 1) % 8000;
366 iso->n_ready_packets--;
368 if (iso->prebuffer != 0) {
369 iso->prebuffer--;
370 if (iso->prebuffer <= 0) {
371 iso->prebuffer = 0;
372 rv = do_iso_xmit_start(iso, iso->start_cycle);
376 out:
377 spin_unlock_irqrestore(&iso->lock, flags);
378 return rv;
381 int hpsb_iso_xmit_sync(struct hpsb_iso *iso)
383 if (iso->type != HPSB_ISO_XMIT)
384 return -EINVAL;
386 return wait_event_interruptible(iso->waitq,
387 hpsb_iso_n_ready(iso) ==
388 iso->buf_packets);
391 void hpsb_iso_packet_sent(struct hpsb_iso *iso, int cycle, int error)
393 unsigned long flags;
394 spin_lock_irqsave(&iso->lock, flags);
396 /* predict the cycle of the next packet to be queued */
398 /* jump ahead by the number of packets that are already buffered */
399 cycle += iso->buf_packets - iso->n_ready_packets;
400 cycle %= 8000;
402 iso->xmit_cycle = cycle;
403 iso->n_ready_packets++;
404 iso->pkt_dma = (iso->pkt_dma + 1) % iso->buf_packets;
406 if (iso->n_ready_packets == iso->buf_packets || error != 0) {
407 /* the buffer has run empty! */
408 atomic_inc(&iso->overflows);
411 spin_unlock_irqrestore(&iso->lock, flags);
414 void hpsb_iso_packet_received(struct hpsb_iso *iso, u32 offset, u16 len,
415 u16 total_len, u16 cycle, u8 channel, u8 tag,
416 u8 sy)
418 unsigned long flags;
419 spin_lock_irqsave(&iso->lock, flags);
421 if (iso->n_ready_packets == iso->buf_packets) {
422 /* overflow! */
423 atomic_inc(&iso->overflows);
424 /* Record size of this discarded packet */
425 iso->bytes_discarded += total_len;
426 } else {
427 struct hpsb_iso_packet_info *info = &iso->infos[iso->pkt_dma];
428 info->offset = offset;
429 info->len = len;
430 info->total_len = total_len;
431 info->cycle = cycle;
432 info->channel = channel;
433 info->tag = tag;
434 info->sy = sy;
436 iso->pkt_dma = (iso->pkt_dma + 1) % iso->buf_packets;
437 iso->n_ready_packets++;
440 spin_unlock_irqrestore(&iso->lock, flags);
443 int hpsb_iso_recv_release_packets(struct hpsb_iso *iso, unsigned int n_packets)
445 unsigned long flags;
446 unsigned int i;
447 int rv = 0;
449 if (iso->type != HPSB_ISO_RECV)
450 return -1;
452 spin_lock_irqsave(&iso->lock, flags);
453 for (i = 0; i < n_packets; i++) {
454 rv = iso->host->driver->isoctl(iso, RECV_RELEASE,
455 (unsigned long)&iso->infos[iso->
456 first_packet]);
457 if (rv)
458 break;
460 iso->first_packet = (iso->first_packet + 1) % iso->buf_packets;
461 iso->n_ready_packets--;
463 /* release memory from packets discarded when queue was full */
464 if (iso->n_ready_packets == 0) { /* Release only after all prior packets handled */
465 if (iso->bytes_discarded != 0) {
466 struct hpsb_iso_packet_info inf;
467 inf.total_len = iso->bytes_discarded;
468 iso->host->driver->isoctl(iso, RECV_RELEASE,
469 (unsigned long)&inf);
470 iso->bytes_discarded = 0;
474 spin_unlock_irqrestore(&iso->lock, flags);
475 return rv;
478 void hpsb_iso_wake(struct hpsb_iso *iso)
480 wake_up_interruptible(&iso->waitq);
482 if (iso->callback)
483 iso->callback(iso);