blob: f26680ebef7cdcd5387627a98c79120d48dc77bb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IEEE 1394 for Linux
3 *
4 * kernel ISO transmission/reception
5 *
6 * Copyright (C) 2002 Maas Digital LLC
7 *
8 * This code is licensed under the GPL. See the file COPYING in the root
9 * directory of the kernel sources for details.
10 */
11
12#include <linux/slab.h>
13#include <linux/sched.h>
14#include "iso.h"
15
16void hpsb_iso_stop(struct hpsb_iso *iso)
17{
18 if (!(iso->flags & HPSB_ISO_DRIVER_STARTED))
19 return;
20
21 iso->host->driver->isoctl(iso, iso->type == HPSB_ISO_XMIT ?
22 XMIT_STOP : RECV_STOP, 0);
23 iso->flags &= ~HPSB_ISO_DRIVER_STARTED;
24}
25
26void hpsb_iso_shutdown(struct hpsb_iso *iso)
27{
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;
33 }
34
35 dma_region_free(&iso->data_buf);
36 kfree(iso);
37}
38
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -050039static struct hpsb_iso *hpsb_iso_common_init(struct hpsb_host *host,
40 enum hpsb_iso_type type,
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 unsigned int data_buf_size,
42 unsigned int buf_packets,
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -050043 int channel, int dma_mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 int irq_interval,
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -050045 void (*callback) (struct hpsb_iso
46 *))
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 struct hpsb_iso *iso;
49 int dma_direction;
50
51 /* make sure driver supports the ISO API */
52 if (!host->driver->isoctl) {
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -050053 printk(KERN_INFO
54 "ieee1394: host driver '%s' does not support the rawiso API\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 host->driver->name);
56 return NULL;
57 }
58
59 /* sanitize parameters */
60
61 if (buf_packets < 2)
62 buf_packets = 2;
63
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -050064 if ((dma_mode < HPSB_ISO_DMA_DEFAULT)
65 || (dma_mode > HPSB_ISO_DMA_PACKET_PER_BUFFER))
66 dma_mode = HPSB_ISO_DMA_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Ben Collins1934b8b2005-07-09 20:01:23 -040068 if ((irq_interval < 0) || (irq_interval > buf_packets / 4))
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -050069 irq_interval = buf_packets / 4;
70 if (irq_interval == 0) /* really interrupt for each packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 irq_interval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 if (channel < -1 || channel >= 64)
74 return NULL;
75
76 /* channel = -1 is OK for multi-channel recv but not for xmit */
77 if (type == HPSB_ISO_XMIT && channel < 0)
78 return NULL;
79
80 /* allocate and write the struct hpsb_iso */
81
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -050082 iso =
83 kmalloc(sizeof(*iso) +
84 buf_packets * sizeof(struct hpsb_iso_packet_info),
85 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 if (!iso)
87 return NULL;
88
89 iso->infos = (struct hpsb_iso_packet_info *)(iso + 1);
90
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);
105
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;
112 }
113
114 atomic_set(&iso->overflows, 0);
Ben Collins1934b8b2005-07-09 20:01:23 -0400115 iso->bytes_discarded = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 iso->flags = 0;
117 iso->prebuffer = 0;
118
119 /* allocate the packet buffer */
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500120 if (dma_region_alloc
121 (&iso->data_buf, iso->buf_size, host->pdev, dma_direction))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 goto err;
123
124 return iso;
125
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500126 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 hpsb_iso_shutdown(iso);
128 return NULL;
129}
130
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500131int hpsb_iso_n_ready(struct hpsb_iso *iso)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 unsigned long flags;
134 int val;
135
136 spin_lock_irqsave(&iso->lock, flags);
137 val = iso->n_ready_packets;
138 spin_unlock_irqrestore(&iso->lock, flags);
139
140 return val;
141}
142
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500143struct hpsb_iso *hpsb_iso_xmit_init(struct hpsb_host *host,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 unsigned int data_buf_size,
145 unsigned int buf_packets,
146 int channel,
147 int speed,
148 int irq_interval,
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500149 void (*callback) (struct hpsb_iso *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_XMIT,
152 data_buf_size, buf_packets,
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500153 channel,
154 HPSB_ISO_DMA_DEFAULT,
155 irq_interval, callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 if (!iso)
157 return NULL;
158
159 iso->speed = speed;
160
161 /* tell the driver to start working */
162 if (host->driver->isoctl(iso, XMIT_INIT, 0))
163 goto err;
164
165 iso->flags |= HPSB_ISO_DRIVER_INIT;
166 return iso;
167
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500168 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 hpsb_iso_shutdown(iso);
170 return NULL;
171}
172
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500173struct hpsb_iso *hpsb_iso_recv_init(struct hpsb_host *host,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 unsigned int data_buf_size,
175 unsigned int buf_packets,
176 int channel,
177 int dma_mode,
178 int irq_interval,
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500179 void (*callback) (struct hpsb_iso *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_RECV,
182 data_buf_size, buf_packets,
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500183 channel, dma_mode,
184 irq_interval, callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (!iso)
186 return NULL;
187
188 /* tell the driver to start working */
189 if (host->driver->isoctl(iso, RECV_INIT, 0))
190 goto err;
191
192 iso->flags |= HPSB_ISO_DRIVER_INIT;
193 return iso;
194
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500195 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 hpsb_iso_shutdown(iso);
197 return NULL;
198}
199
200int hpsb_iso_recv_listen_channel(struct hpsb_iso *iso, unsigned char channel)
201{
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);
205}
206
207int hpsb_iso_recv_unlisten_channel(struct hpsb_iso *iso, unsigned char channel)
208{
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500209 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214int hpsb_iso_recv_set_channel_mask(struct hpsb_iso *iso, u64 mask)
215{
216 if (iso->type != HPSB_ISO_RECV || iso->channel != -1)
217 return -EINVAL;
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500218 return iso->host->driver->isoctl(iso, RECV_SET_CHANNEL_MASK,
219 (unsigned long)&mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
222int hpsb_iso_recv_flush(struct hpsb_iso *iso)
223{
224 if (iso->type != HPSB_ISO_RECV)
225 return -EINVAL;
226 return iso->host->driver->isoctl(iso, RECV_FLUSH, 0);
227}
228
229static int do_iso_xmit_start(struct hpsb_iso *iso, int cycle)
230{
231 int retval = iso->host->driver->isoctl(iso, XMIT_START, cycle);
232 if (retval)
233 return retval;
234
235 iso->flags |= HPSB_ISO_DRIVER_STARTED;
236 return retval;
237}
238
239int hpsb_iso_xmit_start(struct hpsb_iso *iso, int cycle, int prebuffer)
240{
241 if (iso->type != HPSB_ISO_XMIT)
242 return -1;
243
244 if (iso->flags & HPSB_ISO_DRIVER_STARTED)
245 return 0;
246
247 if (cycle < -1)
248 cycle = -1;
249 else if (cycle >= 8000)
250 cycle %= 8000;
251
252 iso->xmit_cycle = cycle;
253
254 if (prebuffer < 0)
Ben Collins1934b8b2005-07-09 20:01:23 -0400255 prebuffer = iso->buf_packets - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 else if (prebuffer == 0)
257 prebuffer = 1;
258
Ben Collins1934b8b2005-07-09 20:01:23 -0400259 if (prebuffer >= iso->buf_packets)
260 prebuffer = iso->buf_packets - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 iso->prebuffer = prebuffer;
263
264 /* remember the starting cycle; DMA will commence from xmit_queue_packets()
265 once enough packets have been buffered */
266 iso->start_cycle = cycle;
267
268 return 0;
269}
270
271int hpsb_iso_recv_start(struct hpsb_iso *iso, int cycle, int tag_mask, int sync)
272{
273 int retval = 0;
274 int isoctl_args[3];
275
276 if (iso->type != HPSB_ISO_RECV)
277 return -1;
278
279 if (iso->flags & HPSB_ISO_DRIVER_STARTED)
280 return 0;
281
282 if (cycle < -1)
283 cycle = -1;
284 else if (cycle >= 8000)
285 cycle %= 8000;
286
287 isoctl_args[0] = cycle;
288
289 if (tag_mask < 0)
290 /* match all tags */
291 tag_mask = 0xF;
292 isoctl_args[1] = tag_mask;
293
294 isoctl_args[2] = sync;
295
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500296 retval =
297 iso->host->driver->isoctl(iso, RECV_START,
298 (unsigned long)&isoctl_args[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (retval)
300 return retval;
301
302 iso->flags |= HPSB_ISO_DRIVER_STARTED;
303 return retval;
304}
305
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 */
308
309static int hpsb_iso_check_offset_len(struct hpsb_iso *iso,
310 unsigned int offset, unsigned short len,
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500311 unsigned int *out_offset,
312 unsigned short *out_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 if (offset >= iso->buf_size)
315 return -EFAULT;
316
317 /* make sure the packet does not go beyond the end of the buffer */
318 if (offset + len > iso->buf_size)
319 return -EFAULT;
320
321 /* check for wrap-around */
322 if (offset + len < offset)
323 return -EFAULT;
324
325 /* now we can trust 'offset' and 'length' */
326 *out_offset = offset;
327 *out_len = len;
328
329 return 0;
330}
331
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500332int hpsb_iso_xmit_queue_packet(struct hpsb_iso *iso, u32 offset, u16 len,
333 u8 tag, u8 sy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 struct hpsb_iso_packet_info *info;
336 unsigned long flags;
337 int rv;
338
339 if (iso->type != HPSB_ISO_XMIT)
340 return -EINVAL;
341
342 /* is there space in the buffer? */
343 if (iso->n_ready_packets <= 0) {
344 return -EBUSY;
345 }
346
347 info = &iso->infos[iso->first_packet];
348
349 /* check for bogus offset/length */
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500350 if (hpsb_iso_check_offset_len
351 (iso, offset, len, &info->offset, &info->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return -EFAULT;
353
354 info->tag = tag;
355 info->sy = sy;
356
357 spin_lock_irqsave(&iso->lock, flags);
358
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500359 rv = iso->host->driver->isoctl(iso, XMIT_QUEUE, (unsigned long)info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (rv)
361 goto out;
362
363 /* increment cursors */
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500364 iso->first_packet = (iso->first_packet + 1) % iso->buf_packets;
365 iso->xmit_cycle = (iso->xmit_cycle + 1) % 8000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 iso->n_ready_packets--;
367
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);
373 }
374 }
375
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500376 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 spin_unlock_irqrestore(&iso->lock, flags);
378 return rv;
379}
380
381int hpsb_iso_xmit_sync(struct hpsb_iso *iso)
382{
383 if (iso->type != HPSB_ISO_XMIT)
384 return -EINVAL;
385
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500386 return wait_event_interruptible(iso->waitq,
387 hpsb_iso_n_ready(iso) ==
388 iso->buf_packets);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
391void hpsb_iso_packet_sent(struct hpsb_iso *iso, int cycle, int error)
392{
393 unsigned long flags;
394 spin_lock_irqsave(&iso->lock, flags);
395
396 /* predict the cycle of the next packet to be queued */
397
398 /* jump ahead by the number of packets that are already buffered */
399 cycle += iso->buf_packets - iso->n_ready_packets;
400 cycle %= 8000;
401
402 iso->xmit_cycle = cycle;
403 iso->n_ready_packets++;
404 iso->pkt_dma = (iso->pkt_dma + 1) % iso->buf_packets;
405
406 if (iso->n_ready_packets == iso->buf_packets || error != 0) {
407 /* the buffer has run empty! */
408 atomic_inc(&iso->overflows);
409 }
410
411 spin_unlock_irqrestore(&iso->lock, flags);
412}
413
414void hpsb_iso_packet_received(struct hpsb_iso *iso, u32 offset, u16 len,
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500415 u16 total_len, u16 cycle, u8 channel, u8 tag,
416 u8 sy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 unsigned long flags;
419 spin_lock_irqsave(&iso->lock, flags);
420
421 if (iso->n_ready_packets == iso->buf_packets) {
422 /* overflow! */
423 atomic_inc(&iso->overflows);
Ben Collins1934b8b2005-07-09 20:01:23 -0400424 /* Record size of this discarded packet */
425 iso->bytes_discarded += total_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 } else {
427 struct hpsb_iso_packet_info *info = &iso->infos[iso->pkt_dma];
428 info->offset = offset;
429 info->len = len;
Ben Collins1934b8b2005-07-09 20:01:23 -0400430 info->total_len = total_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 info->cycle = cycle;
432 info->channel = channel;
433 info->tag = tag;
434 info->sy = sy;
435
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500436 iso->pkt_dma = (iso->pkt_dma + 1) % iso->buf_packets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 iso->n_ready_packets++;
438 }
439
440 spin_unlock_irqrestore(&iso->lock, flags);
441}
442
443int hpsb_iso_recv_release_packets(struct hpsb_iso *iso, unsigned int n_packets)
444{
445 unsigned long flags;
446 unsigned int i;
447 int rv = 0;
448
449 if (iso->type != HPSB_ISO_RECV)
450 return -1;
451
452 spin_lock_irqsave(&iso->lock, flags);
453 for (i = 0; i < n_packets; i++) {
454 rv = iso->host->driver->isoctl(iso, RECV_RELEASE,
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500455 (unsigned long)&iso->infos[iso->
456 first_packet]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (rv)
458 break;
459
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500460 iso->first_packet = (iso->first_packet + 1) % iso->buf_packets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 iso->n_ready_packets--;
Ben Collins1934b8b2005-07-09 20:01:23 -0400462
463 /* release memory from packets discarded when queue was full */
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500464 if (iso->n_ready_packets == 0) { /* Release only after all prior packets handled */
Ben Collins1934b8b2005-07-09 20:01:23 -0400465 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,
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500469 (unsigned long)&inf);
Ben Collins1934b8b2005-07-09 20:01:23 -0400470 iso->bytes_discarded = 0;
471 }
472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474 spin_unlock_irqrestore(&iso->lock, flags);
475 return rv;
476}
477
478void hpsb_iso_wake(struct hpsb_iso *iso)
479{
480 wake_up_interruptible(&iso->waitq);
481
482 if (iso->callback)
483 iso->callback(iso);
484}