blob: 08bd15d2a7b6cea63a675664ea333e9e0837b376 [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
Stefan Richterde4394f2006-07-03 12:02:29 -040012#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/sched.h>
Stefan Richterde4394f2006-07-03 12:02:29 -040014#include <linux/slab.h>
15
16#include "hosts.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "iso.h"
18
19void hpsb_iso_stop(struct hpsb_iso *iso)
20{
21 if (!(iso->flags & HPSB_ISO_DRIVER_STARTED))
22 return;
23
24 iso->host->driver->isoctl(iso, iso->type == HPSB_ISO_XMIT ?
25 XMIT_STOP : RECV_STOP, 0);
26 iso->flags &= ~HPSB_ISO_DRIVER_STARTED;
27}
28
29void hpsb_iso_shutdown(struct hpsb_iso *iso)
30{
31 if (iso->flags & HPSB_ISO_DRIVER_INIT) {
32 hpsb_iso_stop(iso);
33 iso->host->driver->isoctl(iso, iso->type == HPSB_ISO_XMIT ?
34 XMIT_SHUTDOWN : RECV_SHUTDOWN, 0);
35 iso->flags &= ~HPSB_ISO_DRIVER_INIT;
36 }
37
38 dma_region_free(&iso->data_buf);
39 kfree(iso);
40}
41
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -050042static struct hpsb_iso *hpsb_iso_common_init(struct hpsb_host *host,
43 enum hpsb_iso_type type,
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 unsigned int data_buf_size,
45 unsigned int buf_packets,
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -050046 int channel, int dma_mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 int irq_interval,
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -050048 void (*callback) (struct hpsb_iso
49 *))
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 struct hpsb_iso *iso;
52 int dma_direction;
53
54 /* make sure driver supports the ISO API */
55 if (!host->driver->isoctl) {
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -050056 printk(KERN_INFO
57 "ieee1394: host driver '%s' does not support the rawiso API\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 host->driver->name);
59 return NULL;
60 }
61
62 /* sanitize parameters */
63
64 if (buf_packets < 2)
65 buf_packets = 2;
66
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -050067 if ((dma_mode < HPSB_ISO_DMA_DEFAULT)
68 || (dma_mode > HPSB_ISO_DMA_PACKET_PER_BUFFER))
69 dma_mode = HPSB_ISO_DMA_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Ben Collins1934b8b2005-07-09 20:01:23 -040071 if ((irq_interval < 0) || (irq_interval > buf_packets / 4))
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -050072 irq_interval = buf_packets / 4;
73 if (irq_interval == 0) /* really interrupt for each packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 irq_interval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 if (channel < -1 || channel >= 64)
77 return NULL;
78
79 /* channel = -1 is OK for multi-channel recv but not for xmit */
80 if (type == HPSB_ISO_XMIT && channel < 0)
81 return NULL;
82
83 /* allocate and write the struct hpsb_iso */
84
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -050085 iso =
86 kmalloc(sizeof(*iso) +
87 buf_packets * sizeof(struct hpsb_iso_packet_info),
88 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 if (!iso)
90 return NULL;
91
92 iso->infos = (struct hpsb_iso_packet_info *)(iso + 1);
93
94 iso->type = type;
95 iso->host = host;
96 iso->hostdata = NULL;
97 iso->callback = callback;
98 init_waitqueue_head(&iso->waitq);
99 iso->channel = channel;
100 iso->irq_interval = irq_interval;
101 iso->dma_mode = dma_mode;
102 dma_region_init(&iso->data_buf);
103 iso->buf_size = PAGE_ALIGN(data_buf_size);
104 iso->buf_packets = buf_packets;
105 iso->pkt_dma = 0;
106 iso->first_packet = 0;
107 spin_lock_init(&iso->lock);
108
109 if (iso->type == HPSB_ISO_XMIT) {
110 iso->n_ready_packets = iso->buf_packets;
111 dma_direction = PCI_DMA_TODEVICE;
112 } else {
113 iso->n_ready_packets = 0;
114 dma_direction = PCI_DMA_FROMDEVICE;
115 }
116
117 atomic_set(&iso->overflows, 0);
Ben Collins1934b8b2005-07-09 20:01:23 -0400118 iso->bytes_discarded = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 iso->flags = 0;
120 iso->prebuffer = 0;
121
122 /* allocate the packet buffer */
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500123 if (dma_region_alloc
124 (&iso->data_buf, iso->buf_size, host->pdev, dma_direction))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 goto err;
126
127 return iso;
128
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500129 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 hpsb_iso_shutdown(iso);
131 return NULL;
132}
133
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500134int hpsb_iso_n_ready(struct hpsb_iso *iso)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 unsigned long flags;
137 int val;
138
139 spin_lock_irqsave(&iso->lock, flags);
140 val = iso->n_ready_packets;
141 spin_unlock_irqrestore(&iso->lock, flags);
142
143 return val;
144}
145
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500146struct hpsb_iso *hpsb_iso_xmit_init(struct hpsb_host *host,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 unsigned int data_buf_size,
148 unsigned int buf_packets,
149 int channel,
150 int speed,
151 int irq_interval,
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500152 void (*callback) (struct hpsb_iso *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_XMIT,
155 data_buf_size, buf_packets,
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500156 channel,
157 HPSB_ISO_DMA_DEFAULT,
158 irq_interval, callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (!iso)
160 return NULL;
161
162 iso->speed = speed;
163
164 /* tell the driver to start working */
165 if (host->driver->isoctl(iso, XMIT_INIT, 0))
166 goto err;
167
168 iso->flags |= HPSB_ISO_DRIVER_INIT;
169 return iso;
170
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500171 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 hpsb_iso_shutdown(iso);
173 return NULL;
174}
175
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500176struct hpsb_iso *hpsb_iso_recv_init(struct hpsb_host *host,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 unsigned int data_buf_size,
178 unsigned int buf_packets,
179 int channel,
180 int dma_mode,
181 int irq_interval,
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500182 void (*callback) (struct hpsb_iso *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_RECV,
185 data_buf_size, buf_packets,
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500186 channel, dma_mode,
187 irq_interval, callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (!iso)
189 return NULL;
190
191 /* tell the driver to start working */
192 if (host->driver->isoctl(iso, RECV_INIT, 0))
193 goto err;
194
195 iso->flags |= HPSB_ISO_DRIVER_INIT;
196 return iso;
197
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500198 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 hpsb_iso_shutdown(iso);
200 return NULL;
201}
202
203int hpsb_iso_recv_listen_channel(struct hpsb_iso *iso, unsigned char channel)
204{
205 if (iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64)
206 return -EINVAL;
207 return iso->host->driver->isoctl(iso, RECV_LISTEN_CHANNEL, channel);
208}
209
210int hpsb_iso_recv_unlisten_channel(struct hpsb_iso *iso, unsigned char channel)
211{
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500212 if (iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64)
213 return -EINVAL;
214 return iso->host->driver->isoctl(iso, RECV_UNLISTEN_CHANNEL, channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217int hpsb_iso_recv_set_channel_mask(struct hpsb_iso *iso, u64 mask)
218{
219 if (iso->type != HPSB_ISO_RECV || iso->channel != -1)
220 return -EINVAL;
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500221 return iso->host->driver->isoctl(iso, RECV_SET_CHANNEL_MASK,
222 (unsigned long)&mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225int hpsb_iso_recv_flush(struct hpsb_iso *iso)
226{
227 if (iso->type != HPSB_ISO_RECV)
228 return -EINVAL;
229 return iso->host->driver->isoctl(iso, RECV_FLUSH, 0);
230}
231
232static int do_iso_xmit_start(struct hpsb_iso *iso, int cycle)
233{
234 int retval = iso->host->driver->isoctl(iso, XMIT_START, cycle);
235 if (retval)
236 return retval;
237
238 iso->flags |= HPSB_ISO_DRIVER_STARTED;
239 return retval;
240}
241
242int hpsb_iso_xmit_start(struct hpsb_iso *iso, int cycle, int prebuffer)
243{
244 if (iso->type != HPSB_ISO_XMIT)
245 return -1;
246
247 if (iso->flags & HPSB_ISO_DRIVER_STARTED)
248 return 0;
249
250 if (cycle < -1)
251 cycle = -1;
252 else if (cycle >= 8000)
253 cycle %= 8000;
254
255 iso->xmit_cycle = cycle;
256
257 if (prebuffer < 0)
Ben Collins1934b8b2005-07-09 20:01:23 -0400258 prebuffer = iso->buf_packets - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 else if (prebuffer == 0)
260 prebuffer = 1;
261
Ben Collins1934b8b2005-07-09 20:01:23 -0400262 if (prebuffer >= iso->buf_packets)
263 prebuffer = iso->buf_packets - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 iso->prebuffer = prebuffer;
266
267 /* remember the starting cycle; DMA will commence from xmit_queue_packets()
268 once enough packets have been buffered */
269 iso->start_cycle = cycle;
270
271 return 0;
272}
273
274int hpsb_iso_recv_start(struct hpsb_iso *iso, int cycle, int tag_mask, int sync)
275{
276 int retval = 0;
277 int isoctl_args[3];
278
279 if (iso->type != HPSB_ISO_RECV)
280 return -1;
281
282 if (iso->flags & HPSB_ISO_DRIVER_STARTED)
283 return 0;
284
285 if (cycle < -1)
286 cycle = -1;
287 else if (cycle >= 8000)
288 cycle %= 8000;
289
290 isoctl_args[0] = cycle;
291
292 if (tag_mask < 0)
293 /* match all tags */
294 tag_mask = 0xF;
295 isoctl_args[1] = tag_mask;
296
297 isoctl_args[2] = sync;
298
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500299 retval =
300 iso->host->driver->isoctl(iso, RECV_START,
301 (unsigned long)&isoctl_args[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (retval)
303 return retval;
304
305 iso->flags |= HPSB_ISO_DRIVER_STARTED;
306 return retval;
307}
308
309/* check to make sure the user has not supplied bogus values of offset/len
310 that would cause the kernel to access memory outside the buffer */
311
312static int hpsb_iso_check_offset_len(struct hpsb_iso *iso,
313 unsigned int offset, unsigned short len,
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500314 unsigned int *out_offset,
315 unsigned short *out_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 if (offset >= iso->buf_size)
318 return -EFAULT;
319
320 /* make sure the packet does not go beyond the end of the buffer */
321 if (offset + len > iso->buf_size)
322 return -EFAULT;
323
324 /* check for wrap-around */
325 if (offset + len < offset)
326 return -EFAULT;
327
328 /* now we can trust 'offset' and 'length' */
329 *out_offset = offset;
330 *out_len = len;
331
332 return 0;
333}
334
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500335int hpsb_iso_xmit_queue_packet(struct hpsb_iso *iso, u32 offset, u16 len,
336 u8 tag, u8 sy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 struct hpsb_iso_packet_info *info;
339 unsigned long flags;
340 int rv;
341
342 if (iso->type != HPSB_ISO_XMIT)
343 return -EINVAL;
344
345 /* is there space in the buffer? */
346 if (iso->n_ready_packets <= 0) {
347 return -EBUSY;
348 }
349
350 info = &iso->infos[iso->first_packet];
351
352 /* check for bogus offset/length */
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500353 if (hpsb_iso_check_offset_len
354 (iso, offset, len, &info->offset, &info->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return -EFAULT;
356
357 info->tag = tag;
358 info->sy = sy;
359
360 spin_lock_irqsave(&iso->lock, flags);
361
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500362 rv = iso->host->driver->isoctl(iso, XMIT_QUEUE, (unsigned long)info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (rv)
364 goto out;
365
366 /* increment cursors */
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500367 iso->first_packet = (iso->first_packet + 1) % iso->buf_packets;
368 iso->xmit_cycle = (iso->xmit_cycle + 1) % 8000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 iso->n_ready_packets--;
370
371 if (iso->prebuffer != 0) {
372 iso->prebuffer--;
373 if (iso->prebuffer <= 0) {
374 iso->prebuffer = 0;
375 rv = do_iso_xmit_start(iso, iso->start_cycle);
376 }
377 }
378
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500379 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 spin_unlock_irqrestore(&iso->lock, flags);
381 return rv;
382}
383
384int hpsb_iso_xmit_sync(struct hpsb_iso *iso)
385{
386 if (iso->type != HPSB_ISO_XMIT)
387 return -EINVAL;
388
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500389 return wait_event_interruptible(iso->waitq,
390 hpsb_iso_n_ready(iso) ==
391 iso->buf_packets);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
394void hpsb_iso_packet_sent(struct hpsb_iso *iso, int cycle, int error)
395{
396 unsigned long flags;
397 spin_lock_irqsave(&iso->lock, flags);
398
399 /* predict the cycle of the next packet to be queued */
400
401 /* jump ahead by the number of packets that are already buffered */
402 cycle += iso->buf_packets - iso->n_ready_packets;
403 cycle %= 8000;
404
405 iso->xmit_cycle = cycle;
406 iso->n_ready_packets++;
407 iso->pkt_dma = (iso->pkt_dma + 1) % iso->buf_packets;
408
409 if (iso->n_ready_packets == iso->buf_packets || error != 0) {
410 /* the buffer has run empty! */
411 atomic_inc(&iso->overflows);
412 }
413
414 spin_unlock_irqrestore(&iso->lock, flags);
415}
416
417void hpsb_iso_packet_received(struct hpsb_iso *iso, u32 offset, u16 len,
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500418 u16 total_len, u16 cycle, u8 channel, u8 tag,
419 u8 sy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 unsigned long flags;
422 spin_lock_irqsave(&iso->lock, flags);
423
424 if (iso->n_ready_packets == iso->buf_packets) {
425 /* overflow! */
426 atomic_inc(&iso->overflows);
Ben Collins1934b8b2005-07-09 20:01:23 -0400427 /* Record size of this discarded packet */
428 iso->bytes_discarded += total_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 } else {
430 struct hpsb_iso_packet_info *info = &iso->infos[iso->pkt_dma];
431 info->offset = offset;
432 info->len = len;
Ben Collins1934b8b2005-07-09 20:01:23 -0400433 info->total_len = total_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 info->cycle = cycle;
435 info->channel = channel;
436 info->tag = tag;
437 info->sy = sy;
438
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500439 iso->pkt_dma = (iso->pkt_dma + 1) % iso->buf_packets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 iso->n_ready_packets++;
441 }
442
443 spin_unlock_irqrestore(&iso->lock, flags);
444}
445
446int hpsb_iso_recv_release_packets(struct hpsb_iso *iso, unsigned int n_packets)
447{
448 unsigned long flags;
449 unsigned int i;
450 int rv = 0;
451
452 if (iso->type != HPSB_ISO_RECV)
453 return -1;
454
455 spin_lock_irqsave(&iso->lock, flags);
456 for (i = 0; i < n_packets; i++) {
457 rv = iso->host->driver->isoctl(iso, RECV_RELEASE,
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500458 (unsigned long)&iso->infos[iso->
459 first_packet]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (rv)
461 break;
462
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500463 iso->first_packet = (iso->first_packet + 1) % iso->buf_packets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 iso->n_ready_packets--;
Ben Collins1934b8b2005-07-09 20:01:23 -0400465
466 /* release memory from packets discarded when queue was full */
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500467 if (iso->n_ready_packets == 0) { /* Release only after all prior packets handled */
Ben Collins1934b8b2005-07-09 20:01:23 -0400468 if (iso->bytes_discarded != 0) {
469 struct hpsb_iso_packet_info inf;
470 inf.total_len = iso->bytes_discarded;
471 iso->host->driver->isoctl(iso, RECV_RELEASE,
Jens-Michael Hoffmann066ef9c2005-11-22 12:35:23 -0500472 (unsigned long)&inf);
Ben Collins1934b8b2005-07-09 20:01:23 -0400473 iso->bytes_discarded = 0;
474 }
475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477 spin_unlock_irqrestore(&iso->lock, flags);
478 return rv;
479}
480
481void hpsb_iso_wake(struct hpsb_iso *iso)
482{
483 wake_up_interruptible(&iso->waitq);
484
485 if (iso->callback)
486 iso->callback(iso);
487}