[PATCH] hdaps: use ENODEV
[deliverable/linux.git] / drivers / ieee1394 / iso.c
1 /*
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
16 void 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
26 void 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
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 *))
47 {
48 struct hpsb_iso *iso;
49 int dma_direction;
50
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;
57 }
58
59 /* sanitize parameters */
60
61 if (buf_packets < 2)
62 buf_packets = 2;
63
64 if ((dma_mode < HPSB_ISO_DMA_DEFAULT)
65 || (dma_mode > HPSB_ISO_DMA_PACKET_PER_BUFFER))
66 dma_mode = HPSB_ISO_DMA_DEFAULT;
67
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;
72
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
82 iso =
83 kmalloc(sizeof(*iso) +
84 buf_packets * sizeof(struct hpsb_iso_packet_info),
85 GFP_KERNEL);
86 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);
115 iso->bytes_discarded = 0;
116 iso->flags = 0;
117 iso->prebuffer = 0;
118
119 /* allocate the packet buffer */
120 if (dma_region_alloc
121 (&iso->data_buf, iso->buf_size, host->pdev, dma_direction))
122 goto err;
123
124 return iso;
125
126 err:
127 hpsb_iso_shutdown(iso);
128 return NULL;
129 }
130
131 int hpsb_iso_n_ready(struct hpsb_iso *iso)
132 {
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
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 *))
150 {
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;
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
168 err:
169 hpsb_iso_shutdown(iso);
170 return NULL;
171 }
172
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 *))
180 {
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;
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
195 err:
196 hpsb_iso_shutdown(iso);
197 return NULL;
198 }
199
200 int 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
207 int hpsb_iso_recv_unlisten_channel(struct hpsb_iso *iso, unsigned char channel)
208 {
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);
212 }
213
214 int 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;
218 return iso->host->driver->isoctl(iso, RECV_SET_CHANNEL_MASK,
219 (unsigned long)&mask);
220 }
221
222 int 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
229 static 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
239 int 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)
255 prebuffer = iso->buf_packets - 1;
256 else if (prebuffer == 0)
257 prebuffer = 1;
258
259 if (prebuffer >= iso->buf_packets)
260 prebuffer = iso->buf_packets - 1;
261
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
271 int 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
296 retval =
297 iso->host->driver->isoctl(iso, RECV_START,
298 (unsigned long)&isoctl_args[0]);
299 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
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)
313 {
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
332 int hpsb_iso_xmit_queue_packet(struct hpsb_iso *iso, u32 offset, u16 len,
333 u8 tag, u8 sy)
334 {
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 */
350 if (hpsb_iso_check_offset_len
351 (iso, offset, len, &info->offset, &info->len))
352 return -EFAULT;
353
354 info->tag = tag;
355 info->sy = sy;
356
357 spin_lock_irqsave(&iso->lock, flags);
358
359 rv = iso->host->driver->isoctl(iso, XMIT_QUEUE, (unsigned long)info);
360 if (rv)
361 goto out;
362
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--;
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
376 out:
377 spin_unlock_irqrestore(&iso->lock, flags);
378 return rv;
379 }
380
381 int hpsb_iso_xmit_sync(struct hpsb_iso *iso)
382 {
383 if (iso->type != HPSB_ISO_XMIT)
384 return -EINVAL;
385
386 return wait_event_interruptible(iso->waitq,
387 hpsb_iso_n_ready(iso) ==
388 iso->buf_packets);
389 }
390
391 void 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
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)
417 {
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);
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;
435
436 iso->pkt_dma = (iso->pkt_dma + 1) % iso->buf_packets;
437 iso->n_ready_packets++;
438 }
439
440 spin_unlock_irqrestore(&iso->lock, flags);
441 }
442
443 int 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,
455 (unsigned long)&iso->infos[iso->
456 first_packet]);
457 if (rv)
458 break;
459
460 iso->first_packet = (iso->first_packet + 1) % iso->buf_packets;
461 iso->n_ready_packets--;
462
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;
471 }
472 }
473 }
474 spin_unlock_irqrestore(&iso->lock, flags);
475 return rv;
476 }
477
478 void hpsb_iso_wake(struct hpsb_iso *iso)
479 {
480 wake_up_interruptible(&iso->waitq);
481
482 if (iso->callback)
483 iso->callback(iso);
484 }
This page took 0.039906 seconds and 5 git commands to generate.