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 | ||
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, 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*)) | |
46 | { | |
47 | struct hpsb_iso *iso; | |
48 | int dma_direction; | |
49 | ||
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; | |
55 | } | |
56 | ||
57 | /* sanitize parameters */ | |
58 | ||
59 | if (buf_packets < 2) | |
60 | buf_packets = 2; | |
61 | ||
62 | if ((dma_mode < HPSB_ISO_DMA_DEFAULT) || (dma_mode > HPSB_ISO_DMA_PACKET_PER_BUFFER)) | |
63 | dma_mode=HPSB_ISO_DMA_DEFAULT; | |
64 | ||
1934b8b6 BC |
65 | if ((irq_interval < 0) || (irq_interval > buf_packets / 4)) |
66 | irq_interval = buf_packets / 4; | |
1da177e4 LT |
67 | if (irq_interval == 0) /* really interrupt for each packet*/ |
68 | irq_interval = 1; | |
1da177e4 LT |
69 | |
70 | if (channel < -1 || channel >= 64) | |
71 | return NULL; | |
72 | ||
73 | /* channel = -1 is OK for multi-channel recv but not for xmit */ | |
74 | if (type == HPSB_ISO_XMIT && channel < 0) | |
75 | return NULL; | |
76 | ||
77 | /* allocate and write the struct hpsb_iso */ | |
78 | ||
79 | iso = kmalloc(sizeof(*iso) + buf_packets * sizeof(struct hpsb_iso_packet_info), GFP_KERNEL); | |
80 | if (!iso) | |
81 | return NULL; | |
82 | ||
83 | iso->infos = (struct hpsb_iso_packet_info *)(iso + 1); | |
84 | ||
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); | |
99 | ||
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; | |
106 | } | |
107 | ||
108 | atomic_set(&iso->overflows, 0); | |
1934b8b6 | 109 | iso->bytes_discarded = 0; |
1da177e4 LT |
110 | iso->flags = 0; |
111 | iso->prebuffer = 0; | |
112 | ||
113 | /* allocate the packet buffer */ | |
114 | if (dma_region_alloc(&iso->data_buf, iso->buf_size, host->pdev, dma_direction)) | |
115 | goto err; | |
116 | ||
117 | return iso; | |
118 | ||
119 | err: | |
120 | hpsb_iso_shutdown(iso); | |
121 | return NULL; | |
122 | } | |
123 | ||
124 | int hpsb_iso_n_ready(struct hpsb_iso* iso) | |
125 | { | |
126 | unsigned long flags; | |
127 | int val; | |
128 | ||
129 | spin_lock_irqsave(&iso->lock, flags); | |
130 | val = iso->n_ready_packets; | |
131 | spin_unlock_irqrestore(&iso->lock, flags); | |
132 | ||
133 | return val; | |
134 | } | |
135 | ||
136 | ||
137 | struct hpsb_iso* hpsb_iso_xmit_init(struct hpsb_host *host, | |
138 | unsigned int data_buf_size, | |
139 | unsigned int buf_packets, | |
140 | int channel, | |
141 | int speed, | |
142 | int irq_interval, | |
143 | void (*callback)(struct hpsb_iso*)) | |
144 | { | |
145 | struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_XMIT, | |
146 | data_buf_size, buf_packets, | |
147 | channel, HPSB_ISO_DMA_DEFAULT, irq_interval, callback); | |
148 | if (!iso) | |
149 | return NULL; | |
150 | ||
151 | iso->speed = speed; | |
152 | ||
153 | /* tell the driver to start working */ | |
154 | if (host->driver->isoctl(iso, XMIT_INIT, 0)) | |
155 | goto err; | |
156 | ||
157 | iso->flags |= HPSB_ISO_DRIVER_INIT; | |
158 | return iso; | |
159 | ||
160 | err: | |
161 | hpsb_iso_shutdown(iso); | |
162 | return NULL; | |
163 | } | |
164 | ||
165 | struct hpsb_iso* hpsb_iso_recv_init(struct hpsb_host *host, | |
166 | unsigned int data_buf_size, | |
167 | unsigned int buf_packets, | |
168 | int channel, | |
169 | int dma_mode, | |
170 | int irq_interval, | |
171 | void (*callback)(struct hpsb_iso*)) | |
172 | { | |
173 | struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_RECV, | |
174 | data_buf_size, buf_packets, | |
175 | channel, dma_mode, irq_interval, callback); | |
176 | if (!iso) | |
177 | return NULL; | |
178 | ||
179 | /* tell the driver to start working */ | |
180 | if (host->driver->isoctl(iso, RECV_INIT, 0)) | |
181 | goto err; | |
182 | ||
183 | iso->flags |= HPSB_ISO_DRIVER_INIT; | |
184 | return iso; | |
185 | ||
186 | err: | |
187 | hpsb_iso_shutdown(iso); | |
188 | return NULL; | |
189 | } | |
190 | ||
191 | int hpsb_iso_recv_listen_channel(struct hpsb_iso *iso, unsigned char channel) | |
192 | { | |
193 | if (iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64) | |
194 | return -EINVAL; | |
195 | return iso->host->driver->isoctl(iso, RECV_LISTEN_CHANNEL, channel); | |
196 | } | |
197 | ||
198 | int hpsb_iso_recv_unlisten_channel(struct hpsb_iso *iso, unsigned char channel) | |
199 | { | |
200 | if (iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64) | |
201 | return -EINVAL; | |
202 | return iso->host->driver->isoctl(iso, RECV_UNLISTEN_CHANNEL, channel); | |
203 | } | |
204 | ||
205 | int hpsb_iso_recv_set_channel_mask(struct hpsb_iso *iso, u64 mask) | |
206 | { | |
207 | if (iso->type != HPSB_ISO_RECV || iso->channel != -1) | |
208 | return -EINVAL; | |
209 | return iso->host->driver->isoctl(iso, RECV_SET_CHANNEL_MASK, (unsigned long) &mask); | |
210 | } | |
211 | ||
212 | int hpsb_iso_recv_flush(struct hpsb_iso *iso) | |
213 | { | |
214 | if (iso->type != HPSB_ISO_RECV) | |
215 | return -EINVAL; | |
216 | return iso->host->driver->isoctl(iso, RECV_FLUSH, 0); | |
217 | } | |
218 | ||
219 | static int do_iso_xmit_start(struct hpsb_iso *iso, int cycle) | |
220 | { | |
221 | int retval = iso->host->driver->isoctl(iso, XMIT_START, cycle); | |
222 | if (retval) | |
223 | return retval; | |
224 | ||
225 | iso->flags |= HPSB_ISO_DRIVER_STARTED; | |
226 | return retval; | |
227 | } | |
228 | ||
229 | int hpsb_iso_xmit_start(struct hpsb_iso *iso, int cycle, int prebuffer) | |
230 | { | |
231 | if (iso->type != HPSB_ISO_XMIT) | |
232 | return -1; | |
233 | ||
234 | if (iso->flags & HPSB_ISO_DRIVER_STARTED) | |
235 | return 0; | |
236 | ||
237 | if (cycle < -1) | |
238 | cycle = -1; | |
239 | else if (cycle >= 8000) | |
240 | cycle %= 8000; | |
241 | ||
242 | iso->xmit_cycle = cycle; | |
243 | ||
244 | if (prebuffer < 0) | |
1934b8b6 | 245 | prebuffer = iso->buf_packets - 1; |
1da177e4 LT |
246 | else if (prebuffer == 0) |
247 | prebuffer = 1; | |
248 | ||
1934b8b6 BC |
249 | if (prebuffer >= iso->buf_packets) |
250 | prebuffer = iso->buf_packets - 1; | |
1da177e4 LT |
251 | |
252 | iso->prebuffer = prebuffer; | |
253 | ||
254 | /* remember the starting cycle; DMA will commence from xmit_queue_packets() | |
255 | once enough packets have been buffered */ | |
256 | iso->start_cycle = cycle; | |
257 | ||
258 | return 0; | |
259 | } | |
260 | ||
261 | int hpsb_iso_recv_start(struct hpsb_iso *iso, int cycle, int tag_mask, int sync) | |
262 | { | |
263 | int retval = 0; | |
264 | int isoctl_args[3]; | |
265 | ||
266 | if (iso->type != HPSB_ISO_RECV) | |
267 | return -1; | |
268 | ||
269 | if (iso->flags & HPSB_ISO_DRIVER_STARTED) | |
270 | return 0; | |
271 | ||
272 | if (cycle < -1) | |
273 | cycle = -1; | |
274 | else if (cycle >= 8000) | |
275 | cycle %= 8000; | |
276 | ||
277 | isoctl_args[0] = cycle; | |
278 | ||
279 | if (tag_mask < 0) | |
280 | /* match all tags */ | |
281 | tag_mask = 0xF; | |
282 | isoctl_args[1] = tag_mask; | |
283 | ||
284 | isoctl_args[2] = sync; | |
285 | ||
286 | retval = iso->host->driver->isoctl(iso, RECV_START, (unsigned long) &isoctl_args[0]); | |
287 | if (retval) | |
288 | return retval; | |
289 | ||
290 | iso->flags |= HPSB_ISO_DRIVER_STARTED; | |
291 | return retval; | |
292 | } | |
293 | ||
294 | /* check to make sure the user has not supplied bogus values of offset/len | |
295 | that would cause the kernel to access memory outside the buffer */ | |
296 | ||
297 | static int hpsb_iso_check_offset_len(struct hpsb_iso *iso, | |
298 | unsigned int offset, unsigned short len, | |
299 | unsigned int *out_offset, unsigned short *out_len) | |
300 | { | |
301 | if (offset >= iso->buf_size) | |
302 | return -EFAULT; | |
303 | ||
304 | /* make sure the packet does not go beyond the end of the buffer */ | |
305 | if (offset + len > iso->buf_size) | |
306 | return -EFAULT; | |
307 | ||
308 | /* check for wrap-around */ | |
309 | if (offset + len < offset) | |
310 | return -EFAULT; | |
311 | ||
312 | /* now we can trust 'offset' and 'length' */ | |
313 | *out_offset = offset; | |
314 | *out_len = len; | |
315 | ||
316 | return 0; | |
317 | } | |
318 | ||
319 | ||
320 | int hpsb_iso_xmit_queue_packet(struct hpsb_iso *iso, u32 offset, u16 len, u8 tag, u8 sy) | |
321 | { | |
322 | struct hpsb_iso_packet_info *info; | |
323 | unsigned long flags; | |
324 | int rv; | |
325 | ||
326 | if (iso->type != HPSB_ISO_XMIT) | |
327 | return -EINVAL; | |
328 | ||
329 | /* is there space in the buffer? */ | |
330 | if (iso->n_ready_packets <= 0) { | |
331 | return -EBUSY; | |
332 | } | |
333 | ||
334 | info = &iso->infos[iso->first_packet]; | |
335 | ||
336 | /* check for bogus offset/length */ | |
337 | if (hpsb_iso_check_offset_len(iso, offset, len, &info->offset, &info->len)) | |
338 | return -EFAULT; | |
339 | ||
340 | info->tag = tag; | |
341 | info->sy = sy; | |
342 | ||
343 | spin_lock_irqsave(&iso->lock, flags); | |
344 | ||
345 | rv = iso->host->driver->isoctl(iso, XMIT_QUEUE, (unsigned long) info); | |
346 | if (rv) | |
347 | goto out; | |
348 | ||
349 | /* increment cursors */ | |
350 | iso->first_packet = (iso->first_packet+1) % iso->buf_packets; | |
351 | iso->xmit_cycle = (iso->xmit_cycle+1) % 8000; | |
352 | iso->n_ready_packets--; | |
353 | ||
354 | if (iso->prebuffer != 0) { | |
355 | iso->prebuffer--; | |
356 | if (iso->prebuffer <= 0) { | |
357 | iso->prebuffer = 0; | |
358 | rv = do_iso_xmit_start(iso, iso->start_cycle); | |
359 | } | |
360 | } | |
361 | ||
362 | out: | |
363 | spin_unlock_irqrestore(&iso->lock, flags); | |
364 | return rv; | |
365 | } | |
366 | ||
367 | int hpsb_iso_xmit_sync(struct hpsb_iso *iso) | |
368 | { | |
369 | if (iso->type != HPSB_ISO_XMIT) | |
370 | return -EINVAL; | |
371 | ||
372 | return wait_event_interruptible(iso->waitq, hpsb_iso_n_ready(iso) == iso->buf_packets); | |
373 | } | |
374 | ||
375 | void hpsb_iso_packet_sent(struct hpsb_iso *iso, int cycle, int error) | |
376 | { | |
377 | unsigned long flags; | |
378 | spin_lock_irqsave(&iso->lock, flags); | |
379 | ||
380 | /* predict the cycle of the next packet to be queued */ | |
381 | ||
382 | /* jump ahead by the number of packets that are already buffered */ | |
383 | cycle += iso->buf_packets - iso->n_ready_packets; | |
384 | cycle %= 8000; | |
385 | ||
386 | iso->xmit_cycle = cycle; | |
387 | iso->n_ready_packets++; | |
388 | iso->pkt_dma = (iso->pkt_dma + 1) % iso->buf_packets; | |
389 | ||
390 | if (iso->n_ready_packets == iso->buf_packets || error != 0) { | |
391 | /* the buffer has run empty! */ | |
392 | atomic_inc(&iso->overflows); | |
393 | } | |
394 | ||
395 | spin_unlock_irqrestore(&iso->lock, flags); | |
396 | } | |
397 | ||
398 | void hpsb_iso_packet_received(struct hpsb_iso *iso, u32 offset, u16 len, | |
1934b8b6 | 399 | u16 total_len, u16 cycle, u8 channel, u8 tag, u8 sy) |
1da177e4 LT |
400 | { |
401 | unsigned long flags; | |
402 | spin_lock_irqsave(&iso->lock, flags); | |
403 | ||
404 | if (iso->n_ready_packets == iso->buf_packets) { | |
405 | /* overflow! */ | |
406 | atomic_inc(&iso->overflows); | |
1934b8b6 BC |
407 | /* Record size of this discarded packet */ |
408 | iso->bytes_discarded += total_len; | |
1da177e4 LT |
409 | } else { |
410 | struct hpsb_iso_packet_info *info = &iso->infos[iso->pkt_dma]; | |
411 | info->offset = offset; | |
412 | info->len = len; | |
1934b8b6 | 413 | info->total_len = total_len; |
1da177e4 LT |
414 | info->cycle = cycle; |
415 | info->channel = channel; | |
416 | info->tag = tag; | |
417 | info->sy = sy; | |
418 | ||
419 | iso->pkt_dma = (iso->pkt_dma+1) % iso->buf_packets; | |
420 | iso->n_ready_packets++; | |
421 | } | |
422 | ||
423 | spin_unlock_irqrestore(&iso->lock, flags); | |
424 | } | |
425 | ||
426 | int hpsb_iso_recv_release_packets(struct hpsb_iso *iso, unsigned int n_packets) | |
427 | { | |
428 | unsigned long flags; | |
429 | unsigned int i; | |
430 | int rv = 0; | |
431 | ||
432 | if (iso->type != HPSB_ISO_RECV) | |
433 | return -1; | |
434 | ||
435 | spin_lock_irqsave(&iso->lock, flags); | |
436 | for (i = 0; i < n_packets; i++) { | |
437 | rv = iso->host->driver->isoctl(iso, RECV_RELEASE, | |
438 | (unsigned long) &iso->infos[iso->first_packet]); | |
439 | if (rv) | |
440 | break; | |
441 | ||
442 | iso->first_packet = (iso->first_packet+1) % iso->buf_packets; | |
443 | iso->n_ready_packets--; | |
1934b8b6 BC |
444 | |
445 | /* release memory from packets discarded when queue was full */ | |
446 | if (iso->n_ready_packets == 0) { /* Release only after all prior packets handled */ | |
447 | if (iso->bytes_discarded != 0) { | |
448 | struct hpsb_iso_packet_info inf; | |
449 | inf.total_len = iso->bytes_discarded; | |
450 | iso->host->driver->isoctl(iso, RECV_RELEASE, | |
451 | (unsigned long) &inf); | |
452 | iso->bytes_discarded = 0; | |
453 | } | |
454 | } | |
1da177e4 LT |
455 | } |
456 | spin_unlock_irqrestore(&iso->lock, flags); | |
457 | return rv; | |
458 | } | |
459 | ||
460 | void hpsb_iso_wake(struct hpsb_iso *iso) | |
461 | { | |
462 | wake_up_interruptible(&iso->waitq); | |
463 | ||
464 | if (iso->callback) | |
465 | iso->callback(iso); | |
466 | } |