[PATCH] swsusp: clean assembly parts
[wandboard.git] / drivers / ieee1394 / iso.c
blobf05759107f7e72c0fe9de781fa3c3fb7f05f575d
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) /* really interrupt for each packet*/
66 irq_interval = 1;
67 else if ((irq_interval < 0) || (irq_interval > buf_packets / 4))
68 irq_interval = buf_packets / 4;
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->flags = 0;
110 iso->prebuffer = 0;
112 /* allocate the packet buffer */
113 if (dma_region_alloc(&iso->data_buf, iso->buf_size, host->pdev, dma_direction))
114 goto err;
116 return iso;
118 err:
119 hpsb_iso_shutdown(iso);
120 return NULL;
123 int hpsb_iso_n_ready(struct hpsb_iso* iso)
125 unsigned long flags;
126 int val;
128 spin_lock_irqsave(&iso->lock, flags);
129 val = iso->n_ready_packets;
130 spin_unlock_irqrestore(&iso->lock, flags);
132 return val;
136 struct hpsb_iso* hpsb_iso_xmit_init(struct hpsb_host *host,
137 unsigned int data_buf_size,
138 unsigned int buf_packets,
139 int channel,
140 int speed,
141 int irq_interval,
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);
147 if (!iso)
148 return NULL;
150 iso->speed = speed;
152 /* tell the driver to start working */
153 if (host->driver->isoctl(iso, XMIT_INIT, 0))
154 goto err;
156 iso->flags |= HPSB_ISO_DRIVER_INIT;
157 return iso;
159 err:
160 hpsb_iso_shutdown(iso);
161 return NULL;
164 struct hpsb_iso* hpsb_iso_recv_init(struct hpsb_host *host,
165 unsigned int data_buf_size,
166 unsigned int buf_packets,
167 int channel,
168 int dma_mode,
169 int irq_interval,
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);
175 if (!iso)
176 return NULL;
178 /* tell the driver to start working */
179 if (host->driver->isoctl(iso, RECV_INIT, 0))
180 goto err;
182 iso->flags |= HPSB_ISO_DRIVER_INIT;
183 return iso;
185 err:
186 hpsb_iso_shutdown(iso);
187 return NULL;
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)
193 return -EINVAL;
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)
200 return -EINVAL;
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)
207 return -EINVAL;
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)
214 return -EINVAL;
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);
221 if (retval)
222 return retval;
224 iso->flags |= HPSB_ISO_DRIVER_STARTED;
225 return retval;
228 int hpsb_iso_xmit_start(struct hpsb_iso *iso, int cycle, int prebuffer)
230 if (iso->type != HPSB_ISO_XMIT)
231 return -1;
233 if (iso->flags & HPSB_ISO_DRIVER_STARTED)
234 return 0;
236 if (cycle < -1)
237 cycle = -1;
238 else if (cycle >= 8000)
239 cycle %= 8000;
241 iso->xmit_cycle = cycle;
243 if (prebuffer < 0)
244 prebuffer = iso->buf_packets;
245 else if (prebuffer == 0)
246 prebuffer = 1;
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;
257 return 0;
260 int hpsb_iso_recv_start(struct hpsb_iso *iso, int cycle, int tag_mask, int sync)
262 int retval = 0;
263 int isoctl_args[3];
265 if (iso->type != HPSB_ISO_RECV)
266 return -1;
268 if (iso->flags & HPSB_ISO_DRIVER_STARTED)
269 return 0;
271 if (cycle < -1)
272 cycle = -1;
273 else if (cycle >= 8000)
274 cycle %= 8000;
276 isoctl_args[0] = cycle;
278 if (tag_mask < 0)
279 /* match all tags */
280 tag_mask = 0xF;
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]);
286 if (retval)
287 return retval;
289 iso->flags |= HPSB_ISO_DRIVER_STARTED;
290 return retval;
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)
301 return -EFAULT;
303 /* make sure the packet does not go beyond the end of the buffer */
304 if (offset + len > iso->buf_size)
305 return -EFAULT;
307 /* check for wrap-around */
308 if (offset + len < offset)
309 return -EFAULT;
311 /* now we can trust 'offset' and 'length' */
312 *out_offset = offset;
313 *out_len = len;
315 return 0;
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;
322 unsigned long flags;
323 int rv;
325 if (iso->type != HPSB_ISO_XMIT)
326 return -EINVAL;
328 /* is there space in the buffer? */
329 if (iso->n_ready_packets <= 0) {
330 return -EBUSY;
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))
337 return -EFAULT;
339 info->tag = tag;
340 info->sy = sy;
342 spin_lock_irqsave(&iso->lock, flags);
344 rv = iso->host->driver->isoctl(iso, XMIT_QUEUE, (unsigned long) info);
345 if (rv)
346 goto out;
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) {
354 iso->prebuffer--;
355 if (iso->prebuffer <= 0) {
356 iso->prebuffer = 0;
357 rv = do_iso_xmit_start(iso, iso->start_cycle);
361 out:
362 spin_unlock_irqrestore(&iso->lock, flags);
363 return rv;
366 int hpsb_iso_xmit_sync(struct hpsb_iso *iso)
368 if (iso->type != HPSB_ISO_XMIT)
369 return -EINVAL;
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)
376 unsigned long flags;
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;
383 cycle %= 8000;
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)
400 unsigned long flags;
401 spin_lock_irqsave(&iso->lock, flags);
403 if (iso->n_ready_packets == iso->buf_packets) {
404 /* overflow! */
405 atomic_inc(&iso->overflows);
406 } else {
407 struct hpsb_iso_packet_info *info = &iso->infos[iso->pkt_dma];
408 info->offset = offset;
409 info->len = len;
410 info->cycle = cycle;
411 info->channel = channel;
412 info->tag = tag;
413 info->sy = sy;
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)
424 unsigned long flags;
425 unsigned int i;
426 int rv = 0;
428 if (iso->type != HPSB_ISO_RECV)
429 return -1;
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]);
435 if (rv)
436 break;
438 iso->first_packet = (iso->first_packet+1) % iso->buf_packets;
439 iso->n_ready_packets--;
441 spin_unlock_irqrestore(&iso->lock, flags);
442 return rv;
445 void hpsb_iso_wake(struct hpsb_iso *iso)
447 wake_up_interruptible(&iso->waitq);
449 if (iso->callback)
450 iso->callback(iso);