Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | ||
de4394f1 | 12 | #include <linux/pci.h> |
1da177e4 | 13 | #include <linux/sched.h> |
de4394f1 SR |
14 | #include <linux/slab.h> |
15 | ||
16 | #include "hosts.h" | |
1da177e4 LT |
17 | #include "iso.h" |
18 | ||
19 | void 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 | ||
29 | void 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 | ||
066ef9c2 JMH |
42 | static struct hpsb_iso *hpsb_iso_common_init(struct hpsb_host *host, |
43 | enum hpsb_iso_type type, | |
1da177e4 LT |
44 | unsigned int data_buf_size, |
45 | unsigned int buf_packets, | |
066ef9c2 | 46 | int channel, int dma_mode, |
1da177e4 | 47 | int irq_interval, |
066ef9c2 JMH |
48 | void (*callback) (struct hpsb_iso |
49 | *)) | |
1da177e4 LT |
50 | { |
51 | struct hpsb_iso *iso; | |
52 | int dma_direction; | |
53 | ||
54 | /* make sure driver supports the ISO API */ | |
55 | if (!host->driver->isoctl) { | |
066ef9c2 JMH |
56 | printk(KERN_INFO |
57 | "ieee1394: host driver '%s' does not support the rawiso API\n", | |
1da177e4 LT |
58 | host->driver->name); |
59 | return NULL; | |
60 | } | |
61 | ||
62 | /* sanitize parameters */ | |
63 | ||
64 | if (buf_packets < 2) | |
65 | buf_packets = 2; | |
66 | ||
066ef9c2 JMH |
67 | if ((dma_mode < HPSB_ISO_DMA_DEFAULT) |
68 | || (dma_mode > HPSB_ISO_DMA_PACKET_PER_BUFFER)) | |
69 | dma_mode = HPSB_ISO_DMA_DEFAULT; | |
1da177e4 | 70 | |
1934b8b6 | 71 | if ((irq_interval < 0) || (irq_interval > buf_packets / 4)) |
066ef9c2 JMH |
72 | irq_interval = buf_packets / 4; |
73 | if (irq_interval == 0) /* really interrupt for each packet */ | |
1da177e4 | 74 | irq_interval = 1; |
1da177e4 LT |
75 | |
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 | ||
066ef9c2 JMH |
85 | iso = |
86 | kmalloc(sizeof(*iso) + | |
87 | buf_packets * sizeof(struct hpsb_iso_packet_info), | |
88 | GFP_KERNEL); | |
1da177e4 LT |
89 | 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); | |
1934b8b6 | 118 | iso->bytes_discarded = 0; |
1da177e4 LT |
119 | iso->flags = 0; |
120 | iso->prebuffer = 0; | |
121 | ||
122 | /* allocate the packet buffer */ | |
066ef9c2 JMH |
123 | if (dma_region_alloc |
124 | (&iso->data_buf, iso->buf_size, host->pdev, dma_direction)) | |
1da177e4 LT |
125 | goto err; |
126 | ||
127 | return iso; | |
128 | ||
066ef9c2 | 129 | err: |
1da177e4 LT |
130 | hpsb_iso_shutdown(iso); |
131 | return NULL; | |
132 | } | |
133 | ||
066ef9c2 | 134 | int hpsb_iso_n_ready(struct hpsb_iso *iso) |
1da177e4 LT |
135 | { |
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 | ||
066ef9c2 | 146 | struct hpsb_iso *hpsb_iso_xmit_init(struct hpsb_host *host, |
1da177e4 LT |
147 | unsigned int data_buf_size, |
148 | unsigned int buf_packets, | |
149 | int channel, | |
150 | int speed, | |
151 | int irq_interval, | |
066ef9c2 | 152 | void (*callback) (struct hpsb_iso *)) |
1da177e4 LT |
153 | { |
154 | struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_XMIT, | |
155 | data_buf_size, buf_packets, | |
066ef9c2 JMH |
156 | channel, |
157 | HPSB_ISO_DMA_DEFAULT, | |
158 | irq_interval, callback); | |
1da177e4 LT |
159 | 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 | ||
066ef9c2 | 171 | err: |
1da177e4 LT |
172 | hpsb_iso_shutdown(iso); |
173 | return NULL; | |
174 | } | |
175 | ||
066ef9c2 | 176 | struct hpsb_iso *hpsb_iso_recv_init(struct hpsb_host *host, |
1da177e4 LT |
177 | unsigned int data_buf_size, |
178 | unsigned int buf_packets, | |
179 | int channel, | |
180 | int dma_mode, | |
181 | int irq_interval, | |
066ef9c2 | 182 | void (*callback) (struct hpsb_iso *)) |
1da177e4 LT |
183 | { |
184 | struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_RECV, | |
185 | data_buf_size, buf_packets, | |
066ef9c2 JMH |
186 | channel, dma_mode, |
187 | irq_interval, callback); | |
1da177e4 LT |
188 | 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 | ||
066ef9c2 | 198 | err: |
1da177e4 LT |
199 | hpsb_iso_shutdown(iso); |
200 | return NULL; | |
201 | } | |
202 | ||
203 | int 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 | ||
210 | int hpsb_iso_recv_unlisten_channel(struct hpsb_iso *iso, unsigned char channel) | |
211 | { | |
066ef9c2 JMH |
212 | 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); | |
1da177e4 LT |
215 | } |
216 | ||
217 | int 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; | |
066ef9c2 JMH |
221 | return iso->host->driver->isoctl(iso, RECV_SET_CHANNEL_MASK, |
222 | (unsigned long)&mask); | |
1da177e4 LT |
223 | } |
224 | ||
225 | int 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 | ||
232 | static 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 | ||
242 | int 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) | |
1934b8b6 | 258 | prebuffer = iso->buf_packets - 1; |
1da177e4 LT |
259 | else if (prebuffer == 0) |
260 | prebuffer = 1; | |
261 | ||
1934b8b6 BC |
262 | if (prebuffer >= iso->buf_packets) |
263 | prebuffer = iso->buf_packets - 1; | |
1da177e4 LT |
264 | |
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 | ||
274 | int 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 | ||
066ef9c2 JMH |
299 | retval = |
300 | iso->host->driver->isoctl(iso, RECV_START, | |
301 | (unsigned long)&isoctl_args[0]); | |
1da177e4 LT |
302 | 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 | ||
312 | static int hpsb_iso_check_offset_len(struct hpsb_iso *iso, | |
313 | unsigned int offset, unsigned short len, | |
066ef9c2 JMH |
314 | unsigned int *out_offset, |
315 | unsigned short *out_len) | |
1da177e4 LT |
316 | { |
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 | ||
066ef9c2 JMH |
335 | int hpsb_iso_xmit_queue_packet(struct hpsb_iso *iso, u32 offset, u16 len, |
336 | u8 tag, u8 sy) | |
1da177e4 LT |
337 | { |
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 */ | |
066ef9c2 JMH |
353 | if (hpsb_iso_check_offset_len |
354 | (iso, offset, len, &info->offset, &info->len)) | |
1da177e4 LT |
355 | return -EFAULT; |
356 | ||
357 | info->tag = tag; | |
358 | info->sy = sy; | |
359 | ||
360 | spin_lock_irqsave(&iso->lock, flags); | |
361 | ||
066ef9c2 | 362 | rv = iso->host->driver->isoctl(iso, XMIT_QUEUE, (unsigned long)info); |
1da177e4 LT |
363 | if (rv) |
364 | goto out; | |
365 | ||
366 | /* increment cursors */ | |
066ef9c2 JMH |
367 | iso->first_packet = (iso->first_packet + 1) % iso->buf_packets; |
368 | iso->xmit_cycle = (iso->xmit_cycle + 1) % 8000; | |
1da177e4 LT |
369 | 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 | ||
066ef9c2 | 379 | out: |
1da177e4 LT |
380 | spin_unlock_irqrestore(&iso->lock, flags); |
381 | return rv; | |
382 | } | |
383 | ||
384 | int hpsb_iso_xmit_sync(struct hpsb_iso *iso) | |
385 | { | |
386 | if (iso->type != HPSB_ISO_XMIT) | |
387 | return -EINVAL; | |
388 | ||
066ef9c2 JMH |
389 | return wait_event_interruptible(iso->waitq, |
390 | hpsb_iso_n_ready(iso) == | |
391 | iso->buf_packets); | |
1da177e4 LT |
392 | } |
393 | ||
394 | void 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 | ||
417 | void hpsb_iso_packet_received(struct hpsb_iso *iso, u32 offset, u16 len, | |
066ef9c2 JMH |
418 | u16 total_len, u16 cycle, u8 channel, u8 tag, |
419 | u8 sy) | |
1da177e4 LT |
420 | { |
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); | |
1934b8b6 BC |
427 | /* Record size of this discarded packet */ |
428 | iso->bytes_discarded += total_len; | |
1da177e4 LT |
429 | } else { |
430 | struct hpsb_iso_packet_info *info = &iso->infos[iso->pkt_dma]; | |
431 | info->offset = offset; | |
432 | info->len = len; | |
1934b8b6 | 433 | info->total_len = total_len; |
1da177e4 LT |
434 | info->cycle = cycle; |
435 | info->channel = channel; | |
436 | info->tag = tag; | |
437 | info->sy = sy; | |
438 | ||
066ef9c2 | 439 | iso->pkt_dma = (iso->pkt_dma + 1) % iso->buf_packets; |
1da177e4 LT |
440 | iso->n_ready_packets++; |
441 | } | |
442 | ||
443 | spin_unlock_irqrestore(&iso->lock, flags); | |
444 | } | |
445 | ||
446 | int 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, | |
066ef9c2 JMH |
458 | (unsigned long)&iso->infos[iso-> |
459 | first_packet]); | |
1da177e4 LT |
460 | if (rv) |
461 | break; | |
462 | ||
066ef9c2 | 463 | iso->first_packet = (iso->first_packet + 1) % iso->buf_packets; |
1da177e4 | 464 | iso->n_ready_packets--; |
1934b8b6 BC |
465 | |
466 | /* release memory from packets discarded when queue was full */ | |
066ef9c2 | 467 | if (iso->n_ready_packets == 0) { /* Release only after all prior packets handled */ |
1934b8b6 BC |
468 | 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, | |
066ef9c2 | 472 | (unsigned long)&inf); |
1934b8b6 BC |
473 | iso->bytes_discarded = 0; |
474 | } | |
475 | } | |
1da177e4 LT |
476 | } |
477 | spin_unlock_irqrestore(&iso->lock, flags); | |
478 | return rv; | |
479 | } | |
480 | ||
481 | void hpsb_iso_wake(struct hpsb_iso *iso) | |
482 | { | |
483 | wake_up_interruptible(&iso->waitq); | |
484 | ||
485 | if (iso->callback) | |
486 | iso->callback(iso); | |
487 | } |