4 * kernel ISO transmission/reception
6 * Copyright (C) 2002 Maas Digital LLC
8 * This code is licensed under the GPL. See the file COPYING in the root
9 * directory of the kernel sources for details.
12 #include <linux/slab.h>
13 #include <linux/sched.h>
16 void hpsb_iso_stop(struct hpsb_iso
*iso
)
18 if (!(iso
->flags
& HPSB_ISO_DRIVER_STARTED
))
21 iso
->host
->driver
->isoctl(iso
, iso
->type
== HPSB_ISO_XMIT
?
22 XMIT_STOP
: RECV_STOP
, 0);
23 iso
->flags
&= ~HPSB_ISO_DRIVER_STARTED
;
26 void hpsb_iso_shutdown(struct hpsb_iso
*iso
)
28 if (iso
->flags
& HPSB_ISO_DRIVER_INIT
) {
30 iso
->host
->driver
->isoctl(iso
, iso
->type
== HPSB_ISO_XMIT
?
31 XMIT_SHUTDOWN
: RECV_SHUTDOWN
, 0);
32 iso
->flags
&= ~HPSB_ISO_DRIVER_INIT
;
35 dma_region_free(&iso
->data_buf
);
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
,
45 void (*callback
) (struct hpsb_iso
51 /* make sure driver supports the ISO API */
52 if (!host
->driver
->isoctl
) {
54 "ieee1394: host driver '%s' does not support the rawiso API\n",
59 /* sanitize parameters */
64 if ((dma_mode
< HPSB_ISO_DMA_DEFAULT
)
65 || (dma_mode
> HPSB_ISO_DMA_PACKET_PER_BUFFER
))
66 dma_mode
= HPSB_ISO_DMA_DEFAULT
;
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 */
73 if (channel
< -1 || channel
>= 64)
76 /* channel = -1 is OK for multi-channel recv but not for xmit */
77 if (type
== HPSB_ISO_XMIT
&& channel
< 0)
80 /* allocate and write the struct hpsb_iso */
83 kmalloc(sizeof(*iso
) +
84 buf_packets
* sizeof(struct hpsb_iso_packet_info
),
89 iso
->infos
= (struct hpsb_iso_packet_info
*)(iso
+ 1);
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
;
103 iso
->first_packet
= 0;
104 spin_lock_init(&iso
->lock
);
106 if (iso
->type
== HPSB_ISO_XMIT
) {
107 iso
->n_ready_packets
= iso
->buf_packets
;
108 dma_direction
= PCI_DMA_TODEVICE
;
110 iso
->n_ready_packets
= 0;
111 dma_direction
= PCI_DMA_FROMDEVICE
;
114 atomic_set(&iso
->overflows
, 0);
115 iso
->bytes_discarded
= 0;
119 /* allocate the packet buffer */
121 (&iso
->data_buf
, iso
->buf_size
, host
->pdev
, dma_direction
))
127 hpsb_iso_shutdown(iso
);
131 int hpsb_iso_n_ready(struct hpsb_iso
*iso
)
136 spin_lock_irqsave(&iso
->lock
, flags
);
137 val
= iso
->n_ready_packets
;
138 spin_unlock_irqrestore(&iso
->lock
, flags
);
143 struct hpsb_iso
*hpsb_iso_xmit_init(struct hpsb_host
*host
,
144 unsigned int data_buf_size
,
145 unsigned int buf_packets
,
149 void (*callback
) (struct hpsb_iso
*))
151 struct hpsb_iso
*iso
= hpsb_iso_common_init(host
, HPSB_ISO_XMIT
,
152 data_buf_size
, buf_packets
,
154 HPSB_ISO_DMA_DEFAULT
,
155 irq_interval
, callback
);
161 /* tell the driver to start working */
162 if (host
->driver
->isoctl(iso
, XMIT_INIT
, 0))
165 iso
->flags
|= HPSB_ISO_DRIVER_INIT
;
169 hpsb_iso_shutdown(iso
);
173 struct hpsb_iso
*hpsb_iso_recv_init(struct hpsb_host
*host
,
174 unsigned int data_buf_size
,
175 unsigned int buf_packets
,
179 void (*callback
) (struct hpsb_iso
*))
181 struct hpsb_iso
*iso
= hpsb_iso_common_init(host
, HPSB_ISO_RECV
,
182 data_buf_size
, buf_packets
,
184 irq_interval
, callback
);
188 /* tell the driver to start working */
189 if (host
->driver
->isoctl(iso
, RECV_INIT
, 0))
192 iso
->flags
|= HPSB_ISO_DRIVER_INIT
;
196 hpsb_iso_shutdown(iso
);
200 int hpsb_iso_recv_listen_channel(struct hpsb_iso
*iso
, unsigned char channel
)
202 if (iso
->type
!= HPSB_ISO_RECV
|| iso
->channel
!= -1 || channel
>= 64)
204 return iso
->host
->driver
->isoctl(iso
, RECV_LISTEN_CHANNEL
, channel
);
207 int hpsb_iso_recv_unlisten_channel(struct hpsb_iso
*iso
, unsigned char channel
)
209 if (iso
->type
!= HPSB_ISO_RECV
|| iso
->channel
!= -1 || channel
>= 64)
211 return iso
->host
->driver
->isoctl(iso
, RECV_UNLISTEN_CHANNEL
, channel
);
214 int hpsb_iso_recv_set_channel_mask(struct hpsb_iso
*iso
, u64 mask
)
216 if (iso
->type
!= HPSB_ISO_RECV
|| iso
->channel
!= -1)
218 return iso
->host
->driver
->isoctl(iso
, RECV_SET_CHANNEL_MASK
,
219 (unsigned long)&mask
);
222 int hpsb_iso_recv_flush(struct hpsb_iso
*iso
)
224 if (iso
->type
!= HPSB_ISO_RECV
)
226 return iso
->host
->driver
->isoctl(iso
, RECV_FLUSH
, 0);
229 static int do_iso_xmit_start(struct hpsb_iso
*iso
, int cycle
)
231 int retval
= iso
->host
->driver
->isoctl(iso
, XMIT_START
, cycle
);
235 iso
->flags
|= HPSB_ISO_DRIVER_STARTED
;
239 int hpsb_iso_xmit_start(struct hpsb_iso
*iso
, int cycle
, int prebuffer
)
241 if (iso
->type
!= HPSB_ISO_XMIT
)
244 if (iso
->flags
& HPSB_ISO_DRIVER_STARTED
)
249 else if (cycle
>= 8000)
252 iso
->xmit_cycle
= cycle
;
255 prebuffer
= iso
->buf_packets
- 1;
256 else if (prebuffer
== 0)
259 if (prebuffer
>= iso
->buf_packets
)
260 prebuffer
= iso
->buf_packets
- 1;
262 iso
->prebuffer
= prebuffer
;
264 /* remember the starting cycle; DMA will commence from xmit_queue_packets()
265 once enough packets have been buffered */
266 iso
->start_cycle
= cycle
;
271 int hpsb_iso_recv_start(struct hpsb_iso
*iso
, int cycle
, int tag_mask
, int sync
)
276 if (iso
->type
!= HPSB_ISO_RECV
)
279 if (iso
->flags
& HPSB_ISO_DRIVER_STARTED
)
284 else if (cycle
>= 8000)
287 isoctl_args
[0] = cycle
;
292 isoctl_args
[1] = tag_mask
;
294 isoctl_args
[2] = sync
;
297 iso
->host
->driver
->isoctl(iso
, RECV_START
,
298 (unsigned long)&isoctl_args
[0]);
302 iso
->flags
|= HPSB_ISO_DRIVER_STARTED
;
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 */
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
)
314 if (offset
>= iso
->buf_size
)
317 /* make sure the packet does not go beyond the end of the buffer */
318 if (offset
+ len
> iso
->buf_size
)
321 /* check for wrap-around */
322 if (offset
+ len
< offset
)
325 /* now we can trust 'offset' and 'length' */
326 *out_offset
= offset
;
332 int hpsb_iso_xmit_queue_packet(struct hpsb_iso
*iso
, u32 offset
, u16 len
,
335 struct hpsb_iso_packet_info
*info
;
339 if (iso
->type
!= HPSB_ISO_XMIT
)
342 /* is there space in the buffer? */
343 if (iso
->n_ready_packets
<= 0) {
347 info
= &iso
->infos
[iso
->first_packet
];
349 /* check for bogus offset/length */
350 if (hpsb_iso_check_offset_len
351 (iso
, offset
, len
, &info
->offset
, &info
->len
))
357 spin_lock_irqsave(&iso
->lock
, flags
);
359 rv
= iso
->host
->driver
->isoctl(iso
, XMIT_QUEUE
, (unsigned long)info
);
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
--;
368 if (iso
->prebuffer
!= 0) {
370 if (iso
->prebuffer
<= 0) {
372 rv
= do_iso_xmit_start(iso
, iso
->start_cycle
);
377 spin_unlock_irqrestore(&iso
->lock
, flags
);
381 int hpsb_iso_xmit_sync(struct hpsb_iso
*iso
)
383 if (iso
->type
!= HPSB_ISO_XMIT
)
386 return wait_event_interruptible(iso
->waitq
,
387 hpsb_iso_n_ready(iso
) ==
391 void hpsb_iso_packet_sent(struct hpsb_iso
*iso
, int cycle
, int error
)
394 spin_lock_irqsave(&iso
->lock
, flags
);
396 /* predict the cycle of the next packet to be queued */
398 /* jump ahead by the number of packets that are already buffered */
399 cycle
+= iso
->buf_packets
- iso
->n_ready_packets
;
402 iso
->xmit_cycle
= cycle
;
403 iso
->n_ready_packets
++;
404 iso
->pkt_dma
= (iso
->pkt_dma
+ 1) % iso
->buf_packets
;
406 if (iso
->n_ready_packets
== iso
->buf_packets
|| error
!= 0) {
407 /* the buffer has run empty! */
408 atomic_inc(&iso
->overflows
);
411 spin_unlock_irqrestore(&iso
->lock
, flags
);
414 void hpsb_iso_packet_received(struct hpsb_iso
*iso
, u32 offset
, u16 len
,
415 u16 total_len
, u16 cycle
, u8 channel
, u8 tag
,
419 spin_lock_irqsave(&iso
->lock
, flags
);
421 if (iso
->n_ready_packets
== iso
->buf_packets
) {
423 atomic_inc(&iso
->overflows
);
424 /* Record size of this discarded packet */
425 iso
->bytes_discarded
+= total_len
;
427 struct hpsb_iso_packet_info
*info
= &iso
->infos
[iso
->pkt_dma
];
428 info
->offset
= offset
;
430 info
->total_len
= total_len
;
432 info
->channel
= channel
;
436 iso
->pkt_dma
= (iso
->pkt_dma
+ 1) % iso
->buf_packets
;
437 iso
->n_ready_packets
++;
440 spin_unlock_irqrestore(&iso
->lock
, flags
);
443 int hpsb_iso_recv_release_packets(struct hpsb_iso
*iso
, unsigned int n_packets
)
449 if (iso
->type
!= HPSB_ISO_RECV
)
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
->
460 iso
->first_packet
= (iso
->first_packet
+ 1) % iso
->buf_packets
;
461 iso
->n_ready_packets
--;
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;
474 spin_unlock_irqrestore(&iso
->lock
, flags
);
478 void hpsb_iso_wake(struct hpsb_iso
*iso
)
480 wake_up_interruptible(&iso
->waitq
);