2 * Copyright (C) 2007, 2008 Karsten Wiese <fzu@wemgehoertderstaat.de>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <linux/usb.h>
20 #include <linux/gfp.h>
22 #include "usb_stream.h"
27 static unsigned usb_stream_next_packet_size(struct usb_stream_kernel
*sk
)
29 struct usb_stream
*s
= sk
->s
;
30 sk
->out_phase_peeked
= (sk
->out_phase
& 0xffff) + sk
->freqn
;
31 return (sk
->out_phase_peeked
>> 16) * s
->cfg
.frame_size
;
34 static void playback_prep_freqn(struct usb_stream_kernel
*sk
, struct urb
*urb
)
36 struct usb_stream
*s
= sk
->s
;
39 for (pack
= 0; pack
< sk
->n_o_ps
; pack
++) {
40 int l
= usb_stream_next_packet_size(sk
);
41 if (s
->idle_outsize
+ lb
+ l
> s
->period_size
)
44 sk
->out_phase
= sk
->out_phase_peeked
;
45 urb
->iso_frame_desc
[pack
].offset
= lb
;
46 urb
->iso_frame_desc
[pack
].length
= l
;
49 snd_printdd(KERN_DEBUG
"%i\n", lb
);
52 urb
->number_of_packets
= pack
;
53 urb
->transfer_buffer_length
= lb
;
54 s
->idle_outsize
+= lb
- s
->period_size
;
55 snd_printdd(KERN_DEBUG
"idle=%i ul=%i ps=%i\n", s
->idle_outsize
,
59 static int init_pipe_urbs(struct usb_stream_kernel
*sk
, unsigned use_packsize
,
60 struct urb
**urbs
, char *transfer
,
61 struct usb_device
*dev
, int pipe
)
64 int maxpacket
= use_packsize
?
65 use_packsize
: usb_maxpacket(dev
, pipe
, usb_pipeout(pipe
));
66 int transfer_length
= maxpacket
* sk
->n_o_ps
;
68 for (u
= 0; u
< USB_STREAM_NURBS
;
69 ++u
, transfer
+= transfer_length
) {
70 struct urb
*urb
= urbs
[u
];
71 struct usb_iso_packet_descriptor
*desc
;
72 urb
->transfer_buffer
= transfer
;
75 urb
->number_of_packets
= sk
->n_o_ps
;
78 if (usb_pipeout(pipe
))
80 if (usb_urb_ep_type_check(urb
))
83 urb
->transfer_buffer_length
= transfer_length
;
84 desc
= urb
->iso_frame_desc
;
86 desc
->length
= maxpacket
;
87 for (p
= 1; p
< sk
->n_o_ps
; ++p
) {
88 desc
[p
].offset
= desc
[p
- 1].offset
+ maxpacket
;
89 desc
[p
].length
= maxpacket
;
96 static int init_urbs(struct usb_stream_kernel
*sk
, unsigned use_packsize
,
97 struct usb_device
*dev
, int in_pipe
, int out_pipe
)
99 struct usb_stream
*s
= sk
->s
;
100 char *indata
= (char *)s
+ sizeof(*s
) +
101 sizeof(struct usb_stream_packet
) *
105 for (u
= 0; u
< USB_STREAM_NURBS
; ++u
) {
106 sk
->inurb
[u
] = usb_alloc_urb(sk
->n_o_ps
, GFP_KERNEL
);
107 sk
->outurb
[u
] = usb_alloc_urb(sk
->n_o_ps
, GFP_KERNEL
);
110 if (init_pipe_urbs(sk
, use_packsize
, sk
->inurb
, indata
, dev
, in_pipe
) ||
111 init_pipe_urbs(sk
, use_packsize
, sk
->outurb
, sk
->write_page
, dev
,
120 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
121 * this will overflow at approx 524 kHz
123 static inline unsigned get_usb_full_speed_rate(unsigned rate
)
125 return ((rate
<< 13) + 62) / 125;
129 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
130 * this will overflow at approx 4 MHz
132 static inline unsigned get_usb_high_speed_rate(unsigned rate
)
134 return ((rate
<< 10) + 62) / 125;
137 void usb_stream_free(struct usb_stream_kernel
*sk
)
139 struct usb_stream
*s
;
142 for (u
= 0; u
< USB_STREAM_NURBS
; ++u
) {
143 usb_free_urb(sk
->inurb
[u
]);
145 usb_free_urb(sk
->outurb
[u
]);
146 sk
->outurb
[u
] = NULL
;
153 free_pages((unsigned long)sk
->write_page
, get_order(s
->write_size
));
154 sk
->write_page
= NULL
;
155 free_pages((unsigned long)s
, get_order(s
->read_size
));
159 struct usb_stream
*usb_stream_new(struct usb_stream_kernel
*sk
,
160 struct usb_device
*dev
,
161 unsigned in_endpoint
, unsigned out_endpoint
,
162 unsigned sample_rate
, unsigned use_packsize
,
163 unsigned period_frames
, unsigned frame_size
)
165 int packets
, max_packsize
;
166 int in_pipe
, out_pipe
;
167 int read_size
= sizeof(struct usb_stream
);
169 int usb_frames
= dev
->speed
== USB_SPEED_HIGH
? 8000 : 1000;
172 in_pipe
= usb_rcvisocpipe(dev
, in_endpoint
);
173 out_pipe
= usb_sndisocpipe(dev
, out_endpoint
);
175 max_packsize
= use_packsize
?
176 use_packsize
: usb_maxpacket(dev
, in_pipe
, 0);
179 t_period = period_frames / sample_rate
180 iso_packs = t_period / t_iso_frame
181 = (period_frames / sample_rate) * (1 / t_iso_frame)
184 packets
= period_frames
* usb_frames
/ sample_rate
+ 1;
186 if (dev
->speed
== USB_SPEED_HIGH
)
187 packets
= (packets
+ 7) & ~7;
189 read_size
+= packets
* USB_STREAM_URBDEPTH
*
190 (max_packsize
+ sizeof(struct usb_stream_packet
));
192 max_packsize
= usb_maxpacket(dev
, out_pipe
, 1);
193 write_size
= max_packsize
* packets
* USB_STREAM_URBDEPTH
;
195 if (read_size
>= 256*PAGE_SIZE
|| write_size
>= 256*PAGE_SIZE
) {
196 snd_printk(KERN_WARNING
"a size exceeds 128*PAGE_SIZE\n");
200 pg
= get_order(read_size
);
201 sk
->s
= (void *) __get_free_pages(GFP_KERNEL
|__GFP_COMP
|__GFP_ZERO
|
204 snd_printk(KERN_WARNING
"couldn't __get_free_pages()\n");
207 sk
->s
->cfg
.version
= USB_STREAM_INTERFACE_VERSION
;
209 sk
->s
->read_size
= read_size
;
211 sk
->s
->cfg
.sample_rate
= sample_rate
;
212 sk
->s
->cfg
.frame_size
= frame_size
;
213 sk
->n_o_ps
= packets
;
214 sk
->s
->inpackets
= packets
* USB_STREAM_URBDEPTH
;
215 sk
->s
->cfg
.period_frames
= period_frames
;
216 sk
->s
->period_size
= frame_size
* period_frames
;
218 sk
->s
->write_size
= write_size
;
219 pg
= get_order(write_size
);
222 (void *)__get_free_pages(GFP_KERNEL
|__GFP_COMP
|__GFP_ZERO
|
224 if (!sk
->write_page
) {
225 snd_printk(KERN_WARNING
"couldn't __get_free_pages()\n");
230 /* calculate the frequency in 16.16 format */
231 if (dev
->speed
== USB_SPEED_FULL
)
232 sk
->freqn
= get_usb_full_speed_rate(sample_rate
);
234 sk
->freqn
= get_usb_high_speed_rate(sample_rate
);
236 if (init_urbs(sk
, use_packsize
, dev
, in_pipe
, out_pipe
) < 0) {
241 sk
->s
->state
= usb_stream_stopped
;
249 static bool balance_check(struct usb_stream_kernel
*sk
, struct urb
*urb
)
252 if (unlikely(urb
->status
)) {
253 if (urb
->status
!= -ESHUTDOWN
&& urb
->status
!= -ENOENT
)
254 snd_printk(KERN_WARNING
"status=%i\n", urb
->status
);
255 sk
->iso_frame_balance
= 0x7FFFFFFF;
258 r
= sk
->iso_frame_balance
== 0;
264 static bool balance_playback(struct usb_stream_kernel
*sk
, struct urb
*urb
)
266 sk
->iso_frame_balance
+= urb
->number_of_packets
;
267 return balance_check(sk
, urb
);
270 static bool balance_capture(struct usb_stream_kernel
*sk
, struct urb
*urb
)
272 sk
->iso_frame_balance
-= urb
->number_of_packets
;
273 return balance_check(sk
, urb
);
276 static void subs_set_complete(struct urb
**urbs
, void (*complete
)(struct urb
*))
280 for (u
= 0; u
< USB_STREAM_NURBS
; u
++) {
281 struct urb
*urb
= urbs
[u
];
282 urb
->complete
= complete
;
286 static int usb_stream_prepare_playback(struct usb_stream_kernel
*sk
,
289 struct usb_stream
*s
= sk
->s
;
291 struct usb_iso_packet_descriptor
*id
, *od
;
292 int p
= 0, lb
= 0, l
= 0;
294 io
= sk
->idle_outurb
;
295 od
= io
->iso_frame_desc
;
297 for (; s
->sync_packet
< 0; ++p
, ++s
->sync_packet
) {
298 struct urb
*ii
= sk
->completed_inurb
;
299 id
= ii
->iso_frame_desc
+
300 ii
->number_of_packets
+ s
->sync_packet
;
301 l
= id
->actual_length
;
309 s
->sync_packet
< inurb
->number_of_packets
&& p
< sk
->n_o_ps
;
310 ++p
, ++s
->sync_packet
) {
311 l
= inurb
->iso_frame_desc
[s
->sync_packet
].actual_length
;
313 if (s
->idle_outsize
+ lb
+ l
> s
->period_size
)
322 s
->sync_packet
-= inurb
->number_of_packets
;
323 if (unlikely(s
->sync_packet
< -2 || s
->sync_packet
> 0)) {
324 snd_printk(KERN_WARNING
"invalid sync_packet = %i;"
325 " p=%i nop=%i %i %x %x %x > %x\n",
326 s
->sync_packet
, p
, inurb
->number_of_packets
,
327 s
->idle_outsize
+ lb
+ l
,
328 s
->idle_outsize
, lb
, l
,
332 if (unlikely(lb
% s
->cfg
.frame_size
)) {
333 snd_printk(KERN_WARNING
"invalid outsize = %i\n",
337 s
->idle_outsize
+= lb
- s
->period_size
;
338 io
->number_of_packets
= p
;
339 io
->transfer_buffer_length
= lb
;
340 if (s
->idle_outsize
<= 0)
343 snd_printk(KERN_WARNING
"idle=%i\n", s
->idle_outsize
);
347 static void prepare_inurb(int number_of_packets
, struct urb
*iu
)
349 struct usb_iso_packet_descriptor
*id
;
352 iu
->number_of_packets
= number_of_packets
;
353 id
= iu
->iso_frame_desc
;
355 for (p
= 0; p
< iu
->number_of_packets
- 1; ++p
)
356 id
[p
+ 1].offset
= id
[p
].offset
+ id
[p
].length
;
358 iu
->transfer_buffer_length
=
359 id
[0].length
* iu
->number_of_packets
;
362 static int submit_urbs(struct usb_stream_kernel
*sk
,
363 struct urb
*inurb
, struct urb
*outurb
)
366 prepare_inurb(sk
->idle_outurb
->number_of_packets
, sk
->idle_inurb
);
367 err
= usb_submit_urb(sk
->idle_inurb
, GFP_ATOMIC
);
371 sk
->idle_inurb
= sk
->completed_inurb
;
372 sk
->completed_inurb
= inurb
;
373 err
= usb_submit_urb(sk
->idle_outurb
, GFP_ATOMIC
);
377 sk
->idle_outurb
= sk
->completed_outurb
;
378 sk
->completed_outurb
= outurb
;
382 snd_printk(KERN_ERR
"%i\n", err
);
386 #ifdef DEBUG_LOOP_BACK
388 This loop_back() shows how to read/write the period data.
390 static void loop_back(struct usb_stream
*s
)
395 struct usb_iso_packet_descriptor
*id
;
397 o
= s
->playback1st_to
;
398 ol
= s
->playback1st_size
;
401 if (s
->insplit_pack
>= 0) {
403 id
= iu
->iso_frame_desc
;
408 for (; p
< iu
->number_of_packets
&& l
< s
->period_size
; ++p
) {
409 i
= iu
->transfer_buffer
+ id
[p
].offset
;
410 il
= id
[p
].actual_length
;
411 if (l
+ il
> s
->period_size
)
412 il
= s
->period_size
- l
;
421 memcpy(o
, i
+ ol
, il
- ol
);
423 ol
= s
->period_size
- s
->playback1st_size
;
427 if (iu
== sk
->completed_inurb
) {
428 if (l
!= s
->period_size
)
429 printk(KERN_DEBUG
"%s:%i %i\n", __func__
, __LINE__
,
430 l
/(int)s
->cfg
.frame_size
);
435 iu
= sk
->completed_inurb
;
436 id
= iu
->iso_frame_desc
;
442 static void loop_back(struct usb_stream
*s
)
447 static void stream_idle(struct usb_stream_kernel
*sk
,
448 struct urb
*inurb
, struct urb
*outurb
)
450 struct usb_stream
*s
= sk
->s
;
452 int insize
= s
->idle_insize
;
455 s
->inpacket_split
= s
->next_inpacket_split
;
456 s
->inpacket_split_at
= s
->next_inpacket_split_at
;
457 s
->next_inpacket_split
= -1;
458 s
->next_inpacket_split_at
= 0;
460 for (p
= 0; p
< inurb
->number_of_packets
; ++p
) {
461 struct usb_iso_packet_descriptor
*id
= inurb
->iso_frame_desc
;
462 l
= id
[p
].actual_length
;
463 if (unlikely(l
== 0 || id
[p
].status
)) {
464 snd_printk(KERN_WARNING
"underrun, status=%u\n",
469 s
->inpacket_head
%= s
->inpackets
;
470 if (s
->inpacket_split
== -1)
471 s
->inpacket_split
= s
->inpacket_head
;
473 s
->inpacket
[s
->inpacket_head
].offset
=
474 id
[p
].offset
+ (inurb
->transfer_buffer
- (void *)s
);
475 s
->inpacket
[s
->inpacket_head
].length
= l
;
476 if (insize
+ l
> s
->period_size
&&
477 s
->next_inpacket_split
== -1) {
478 s
->next_inpacket_split
= s
->inpacket_head
;
479 s
->next_inpacket_split_at
= s
->period_size
- insize
;
484 s
->idle_insize
+= urb_size
- s
->period_size
;
485 if (s
->idle_insize
< 0) {
486 snd_printk(KERN_WARNING
"%i\n",
487 (s
->idle_insize
)/(int)s
->cfg
.frame_size
);
490 s
->insize_done
+= urb_size
;
493 s
->outpacket
[0].offset
= (sk
->idle_outurb
->transfer_buffer
-
496 if (usb_stream_prepare_playback(sk
, inurb
) < 0)
499 s
->outpacket
[0].length
= sk
->idle_outurb
->transfer_buffer_length
+ l
;
500 s
->outpacket
[1].offset
= sk
->completed_outurb
->transfer_buffer
-
503 if (submit_urbs(sk
, inurb
, outurb
) < 0)
508 wake_up_all(&sk
->sleep
);
511 s
->state
= usb_stream_xrun
;
512 wake_up_all(&sk
->sleep
);
515 static void i_capture_idle(struct urb
*urb
)
517 struct usb_stream_kernel
*sk
= urb
->context
;
518 if (balance_capture(sk
, urb
))
519 stream_idle(sk
, urb
, sk
->i_urb
);
522 static void i_playback_idle(struct urb
*urb
)
524 struct usb_stream_kernel
*sk
= urb
->context
;
525 if (balance_playback(sk
, urb
))
526 stream_idle(sk
, sk
->i_urb
, urb
);
529 static void stream_start(struct usb_stream_kernel
*sk
,
530 struct urb
*inurb
, struct urb
*outurb
)
532 struct usb_stream
*s
= sk
->s
;
533 if (s
->state
>= usb_stream_sync1
) {
534 int l
, p
, max_diff
, max_diff_0
;
536 unsigned frames_per_packet
, min_frames
= 0;
537 frames_per_packet
= (s
->period_size
- s
->idle_insize
);
538 frames_per_packet
<<= 8;
540 s
->cfg
.frame_size
* inurb
->number_of_packets
;
543 max_diff_0
= s
->cfg
.frame_size
;
544 if (s
->cfg
.period_frames
>= 256)
546 if (s
->cfg
.period_frames
>= 1024)
548 max_diff
= max_diff_0
;
549 for (p
= 0; p
< inurb
->number_of_packets
; ++p
) {
551 l
= inurb
->iso_frame_desc
[p
].actual_length
;
554 min_frames
+= frames_per_packet
;
556 (min_frames
>> 8) * s
->cfg
.frame_size
;
557 if (diff
< max_diff
) {
558 snd_printdd(KERN_DEBUG
"%i %i %i %i\n",
560 urb_size
/ (int)s
->cfg
.frame_size
,
561 inurb
->number_of_packets
, diff
);
565 s
->idle_insize
-= max_diff
- max_diff_0
;
566 s
->idle_insize
+= urb_size
- s
->period_size
;
567 if (s
->idle_insize
< 0) {
568 snd_printk(KERN_WARNING
"%i %i %i\n",
569 s
->idle_insize
, urb_size
, s
->period_size
);
571 } else if (s
->idle_insize
== 0) {
572 s
->next_inpacket_split
=
573 (s
->inpacket_head
+ 1) % s
->inpackets
;
574 s
->next_inpacket_split_at
= 0;
576 unsigned split
= s
->inpacket_head
;
578 while (l
> s
->inpacket
[split
].length
) {
579 l
-= s
->inpacket
[split
].length
;
581 split
= s
->inpackets
- 1;
585 s
->next_inpacket_split
= split
;
586 s
->next_inpacket_split_at
=
587 s
->inpacket
[split
].length
- l
;
590 s
->insize_done
+= urb_size
;
592 if (usb_stream_prepare_playback(sk
, inurb
) < 0)
596 playback_prep_freqn(sk
, sk
->idle_outurb
);
598 if (submit_urbs(sk
, inurb
, outurb
) < 0)
601 if (s
->state
== usb_stream_sync1
&& s
->insize_done
> 360000) {
602 /* just guesswork ^^^^^^ */
603 s
->state
= usb_stream_ready
;
604 subs_set_complete(sk
->inurb
, i_capture_idle
);
605 subs_set_complete(sk
->outurb
, i_playback_idle
);
609 static void i_capture_start(struct urb
*urb
)
611 struct usb_iso_packet_descriptor
*id
= urb
->iso_frame_desc
;
612 struct usb_stream_kernel
*sk
= urb
->context
;
613 struct usb_stream
*s
= sk
->s
;
618 snd_printk(KERN_WARNING
"status=%i\n", urb
->status
);
622 for (p
= 0; p
< urb
->number_of_packets
; ++p
) {
623 int l
= id
[p
].actual_length
;
624 if (l
< s
->cfg
.frame_size
) {
626 if (s
->state
>= usb_stream_sync0
) {
627 snd_printk(KERN_WARNING
"%i\n", l
);
632 s
->inpacket_head
%= s
->inpackets
;
633 s
->inpacket
[s
->inpacket_head
].offset
=
634 id
[p
].offset
+ (urb
->transfer_buffer
- (void *)s
);
635 s
->inpacket
[s
->inpacket_head
].length
= l
;
639 printk(KERN_DEBUG
"%s:%i: %i", __func__
, __LINE__
,
640 urb
->iso_frame_desc
[0].actual_length
);
641 for (pack
= 1; pack
< urb
->number_of_packets
; ++pack
) {
642 int l
= urb
->iso_frame_desc
[pack
].actual_length
;
643 printk(KERN_CONT
" %i", l
);
645 printk(KERN_CONT
"\n");
648 if (!empty
&& s
->state
< usb_stream_sync1
)
651 if (balance_capture(sk
, urb
))
652 stream_start(sk
, urb
, sk
->i_urb
);
655 static void i_playback_start(struct urb
*urb
)
657 struct usb_stream_kernel
*sk
= urb
->context
;
658 if (balance_playback(sk
, urb
))
659 stream_start(sk
, sk
->i_urb
, urb
);
662 int usb_stream_start(struct usb_stream_kernel
*sk
)
664 struct usb_stream
*s
= sk
->s
;
665 int frame
= 0, iters
= 0;
669 if (s
->state
!= usb_stream_stopped
)
672 subs_set_complete(sk
->inurb
, i_capture_start
);
673 subs_set_complete(sk
->outurb
, i_playback_start
);
674 memset(sk
->write_page
, 0, s
->write_size
);
680 s
->inpacket_head
= -1;
681 sk
->iso_frame_balance
= 0;
683 for (u
= 0; u
< 2; u
++) {
684 struct urb
*inurb
= sk
->inurb
[u
];
685 struct urb
*outurb
= sk
->outurb
[u
];
686 playback_prep_freqn(sk
, outurb
);
687 inurb
->number_of_packets
= outurb
->number_of_packets
;
688 inurb
->transfer_buffer_length
=
689 inurb
->number_of_packets
*
690 inurb
->iso_frame_desc
[0].length
;
694 struct usb_device
*dev
= inurb
->dev
;
695 frame
= usb_get_current_frame_number(dev
);
697 now
= usb_get_current_frame_number(dev
);
699 } while (now
> -1 && now
== frame
);
701 err
= usb_submit_urb(inurb
, GFP_ATOMIC
);
703 snd_printk(KERN_ERR
"usb_submit_urb(sk->inurb[%i])"
704 " returned %i\n", u
, err
);
707 err
= usb_submit_urb(outurb
, GFP_ATOMIC
);
709 snd_printk(KERN_ERR
"usb_submit_urb(sk->outurb[%i])"
710 " returned %i\n", u
, err
);
714 if (inurb
->start_frame
!= outurb
->start_frame
) {
715 snd_printd(KERN_DEBUG
716 "u[%i] start_frames differ in:%u out:%u\n",
717 u
, inurb
->start_frame
, outurb
->start_frame
);
721 snd_printdd(KERN_DEBUG
"%i %i\n", frame
, iters
);
728 snd_printd(KERN_DEBUG
"goto dotry;\n");
731 snd_printk(KERN_WARNING
"couldn't start"
732 " all urbs on the same start_frame.\n");
736 sk
->idle_inurb
= sk
->inurb
[USB_STREAM_NURBS
- 2];
737 sk
->idle_outurb
= sk
->outurb
[USB_STREAM_NURBS
- 2];
738 sk
->completed_inurb
= sk
->inurb
[USB_STREAM_NURBS
- 1];
739 sk
->completed_outurb
= sk
->outurb
[USB_STREAM_NURBS
- 1];
744 while (s
->state
!= usb_stream_ready
&& wait_ms
> 0) {
745 snd_printdd(KERN_DEBUG
"%i\n", s
->state
);
751 return s
->state
== usb_stream_ready
? 0 : -EFAULT
;
757 void usb_stream_stop(struct usb_stream_kernel
*sk
)
762 for (u
= 0; u
< USB_STREAM_NURBS
; ++u
) {
763 usb_kill_urb(sk
->inurb
[u
]);
764 usb_kill_urb(sk
->outurb
[u
]);
766 sk
->s
->state
= usb_stream_stopped
;