1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2007, 2008 Karsten Wiese <fzu@wemgehoertderstaat.de>
9 #include "usb_stream.h"
13 static unsigned int usb_stream_next_packet_size(struct usb_stream_kernel *sk)
15 struct usb_stream *s = sk->s;
17 sk->out_phase_peeked = (sk->out_phase & 0xffff) + sk->freqn;
18 return (sk->out_phase_peeked >> 16) * s->cfg.frame_size;
21 static void playback_prep_freqn(struct usb_stream_kernel *sk, struct urb *urb)
23 struct usb_stream *s = sk->s;
26 for (pack = 0; pack < sk->n_o_ps; pack++) {
27 int l = usb_stream_next_packet_size(sk);
29 if (s->idle_outsize + lb + l > s->period_size)
32 sk->out_phase = sk->out_phase_peeked;
33 urb->iso_frame_desc[pack].offset = lb;
34 urb->iso_frame_desc[pack].length = l;
37 snd_printdd(KERN_DEBUG "%i\n", lb);
40 urb->number_of_packets = pack;
41 urb->transfer_buffer_length = lb;
42 s->idle_outsize += lb - s->period_size;
43 snd_printdd(KERN_DEBUG "idle=%i ul=%i ps=%i\n", s->idle_outsize,
47 static int init_pipe_urbs(struct usb_stream_kernel *sk,
48 unsigned int use_packsize,
49 struct urb **urbs, char *transfer,
50 struct usb_device *dev, int pipe)
53 int maxpacket = use_packsize ?
54 use_packsize : usb_maxpacket(dev, pipe);
55 int transfer_length = maxpacket * sk->n_o_ps;
57 for (u = 0; u < USB_STREAM_NURBS;
58 ++u, transfer += transfer_length) {
59 struct urb *urb = urbs[u];
60 struct usb_iso_packet_descriptor *desc;
62 urb->transfer_buffer = transfer;
65 urb->number_of_packets = sk->n_o_ps;
68 if (usb_pipeout(pipe))
70 if (usb_urb_ep_type_check(urb))
73 urb->transfer_buffer_length = transfer_length;
74 desc = urb->iso_frame_desc;
76 desc->length = maxpacket;
77 for (p = 1; p < sk->n_o_ps; ++p) {
78 desc[p].offset = desc[p - 1].offset + maxpacket;
79 desc[p].length = maxpacket;
86 static int init_urbs(struct usb_stream_kernel *sk, unsigned int use_packsize,
87 struct usb_device *dev, int in_pipe, int out_pipe)
89 struct usb_stream *s = sk->s;
91 (char *)s + sizeof(*s) + sizeof(struct usb_stream_packet) * s->inpackets;
94 for (u = 0; u < USB_STREAM_NURBS; ++u) {
95 sk->inurb[u] = usb_alloc_urb(sk->n_o_ps, GFP_KERNEL);
99 sk->outurb[u] = usb_alloc_urb(sk->n_o_ps, GFP_KERNEL);
104 if (init_pipe_urbs(sk, use_packsize, sk->inurb, indata, dev, in_pipe) ||
105 init_pipe_urbs(sk, use_packsize, sk->outurb, sk->write_page, dev,
113 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
114 * this will overflow at approx 524 kHz
116 static inline unsigned int get_usb_full_speed_rate(unsigned int rate)
118 return ((rate << 13) + 62) / 125;
122 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
123 * this will overflow at approx 4 MHz
125 static inline unsigned int get_usb_high_speed_rate(unsigned int rate)
127 return ((rate << 10) + 62) / 125;
130 void usb_stream_free(struct usb_stream_kernel *sk)
132 struct usb_stream *s;
135 for (u = 0; u < USB_STREAM_NURBS; ++u) {
136 usb_free_urb(sk->inurb[u]);
138 usb_free_urb(sk->outurb[u]);
139 sk->outurb[u] = NULL;
146 if (sk->write_page) {
147 free_pages_exact(sk->write_page, s->write_size);
148 sk->write_page = NULL;
151 free_pages_exact(s, s->read_size);
155 struct usb_stream *usb_stream_new(struct usb_stream_kernel *sk,
156 struct usb_device *dev,
157 unsigned int in_endpoint,
158 unsigned int out_endpoint,
159 unsigned int sample_rate,
160 unsigned int use_packsize,
161 unsigned int period_frames,
162 unsigned int frame_size)
164 int packets, max_packsize;
165 int in_pipe, out_pipe;
166 int read_size = sizeof(struct usb_stream);
168 int usb_frames = dev->speed == USB_SPEED_HIGH ? 8000 : 1000;
170 in_pipe = usb_rcvisocpipe(dev, in_endpoint);
171 out_pipe = usb_sndisocpipe(dev, out_endpoint);
173 max_packsize = use_packsize ?
174 use_packsize : usb_maxpacket(dev, in_pipe);
177 t_period = period_frames / sample_rate
178 iso_packs = t_period / t_iso_frame
179 = (period_frames / sample_rate) * (1 / t_iso_frame)
182 packets = period_frames * usb_frames / sample_rate + 1;
184 if (dev->speed == USB_SPEED_HIGH)
185 packets = (packets + 7) & ~7;
187 read_size += packets * USB_STREAM_URBDEPTH *
188 (max_packsize + sizeof(struct usb_stream_packet));
190 max_packsize = usb_maxpacket(dev, out_pipe);
191 write_size = max_packsize * packets * USB_STREAM_URBDEPTH;
193 if (read_size >= 256*PAGE_SIZE || write_size >= 256*PAGE_SIZE) {
194 snd_printk(KERN_WARNING "a size exceeds 128*PAGE_SIZE\n");
198 sk->s = alloc_pages_exact(read_size,
199 GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN);
201 pr_warn("us122l: couldn't allocate read buffer\n");
204 sk->s->cfg.version = USB_STREAM_INTERFACE_VERSION;
206 sk->s->read_size = read_size;
208 sk->s->cfg.sample_rate = sample_rate;
209 sk->s->cfg.frame_size = frame_size;
210 sk->n_o_ps = packets;
211 sk->s->inpackets = packets * USB_STREAM_URBDEPTH;
212 sk->s->cfg.period_frames = period_frames;
213 sk->s->period_size = frame_size * period_frames;
215 sk->s->write_size = write_size;
217 sk->write_page = alloc_pages_exact(write_size,
218 GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN);
219 if (!sk->write_page) {
220 pr_warn("us122l: couldn't allocate write buffer\n");
225 /* calculate the frequency in 16.16 format */
226 if (dev->speed == USB_SPEED_FULL)
227 sk->freqn = get_usb_full_speed_rate(sample_rate);
229 sk->freqn = get_usb_high_speed_rate(sample_rate);
231 if (init_urbs(sk, use_packsize, dev, in_pipe, out_pipe) < 0) {
236 sk->s->state = usb_stream_stopped;
243 static bool balance_check(struct usb_stream_kernel *sk, struct urb *urb)
247 if (unlikely(urb->status)) {
248 if (urb->status != -ESHUTDOWN && urb->status != -ENOENT)
249 snd_printk(KERN_WARNING "status=%i\n", urb->status);
250 sk->iso_frame_balance = 0x7FFFFFFF;
253 r = sk->iso_frame_balance == 0;
259 static bool balance_playback(struct usb_stream_kernel *sk, struct urb *urb)
261 sk->iso_frame_balance += urb->number_of_packets;
262 return balance_check(sk, urb);
265 static bool balance_capture(struct usb_stream_kernel *sk, struct urb *urb)
267 sk->iso_frame_balance -= urb->number_of_packets;
268 return balance_check(sk, urb);
271 static void subs_set_complete(struct urb **urbs, void (*complete)(struct urb *))
275 for (u = 0; u < USB_STREAM_NURBS; u++) {
276 struct urb *urb = urbs[u];
278 urb->complete = complete;
282 static int usb_stream_prepare_playback(struct usb_stream_kernel *sk,
285 struct usb_stream *s = sk->s;
287 struct usb_iso_packet_descriptor *id, *od;
288 int p = 0, lb = 0, l = 0;
290 io = sk->idle_outurb;
291 od = io->iso_frame_desc;
293 for (; s->sync_packet < 0; ++p, ++s->sync_packet) {
294 struct urb *ii = sk->completed_inurb;
296 id = ii->iso_frame_desc +
297 ii->number_of_packets + s->sync_packet;
298 l = id->actual_length;
306 s->sync_packet < inurb->number_of_packets && p < sk->n_o_ps;
307 ++p, ++s->sync_packet) {
308 l = inurb->iso_frame_desc[s->sync_packet].actual_length;
310 if (s->idle_outsize + lb + l > s->period_size)
319 s->sync_packet -= inurb->number_of_packets;
320 if (unlikely(s->sync_packet < -2 || s->sync_packet > 0)) {
321 snd_printk(KERN_WARNING "invalid sync_packet = %i;"
322 " p=%i nop=%i %i %x %x %x > %x\n",
323 s->sync_packet, p, inurb->number_of_packets,
324 s->idle_outsize + lb + l,
325 s->idle_outsize, lb, l,
329 if (unlikely(lb % s->cfg.frame_size)) {
330 snd_printk(KERN_WARNING"invalid outsize = %i\n",
334 s->idle_outsize += lb - s->period_size;
335 io->number_of_packets = p;
336 io->transfer_buffer_length = lb;
337 if (s->idle_outsize <= 0)
340 snd_printk(KERN_WARNING "idle=%i\n", s->idle_outsize);
344 static void prepare_inurb(int number_of_packets, struct urb *iu)
346 struct usb_iso_packet_descriptor *id;
349 iu->number_of_packets = number_of_packets;
350 id = iu->iso_frame_desc;
352 for (p = 0; p < iu->number_of_packets - 1; ++p)
353 id[p + 1].offset = id[p].offset + id[p].length;
355 iu->transfer_buffer_length =
356 id[0].length * iu->number_of_packets;
359 static int submit_urbs(struct usb_stream_kernel *sk,
360 struct urb *inurb, struct urb *outurb)
364 prepare_inurb(sk->idle_outurb->number_of_packets, sk->idle_inurb);
365 err = usb_submit_urb(sk->idle_inurb, GFP_ATOMIC);
369 sk->idle_inurb = sk->completed_inurb;
370 sk->completed_inurb = inurb;
371 err = usb_submit_urb(sk->idle_outurb, GFP_ATOMIC);
375 sk->idle_outurb = sk->completed_outurb;
376 sk->completed_outurb = outurb;
380 snd_printk(KERN_ERR "%i\n", err);
384 #ifdef DEBUG_LOOP_BACK
386 This loop_back() shows how to read/write the period data.
388 static void loop_back(struct usb_stream *s)
393 struct usb_iso_packet_descriptor *id;
395 o = s->playback1st_to;
396 ol = s->playback1st_size;
399 if (s->insplit_pack >= 0) {
401 id = iu->iso_frame_desc;
406 for (; p < iu->number_of_packets && l < s->period_size; ++p) {
407 i = iu->transfer_buffer + id[p].offset;
408 il = id[p].actual_length;
409 if (l + il > s->period_size)
410 il = s->period_size - l;
419 memcpy(o, i + ol, il - ol);
421 ol = s->period_size - s->playback1st_size;
425 if (iu == sk->completed_inurb) {
426 if (l != s->period_size)
427 printk(KERN_DEBUG"%s:%i %i\n", __func__, __LINE__,
428 l/(int)s->cfg.frame_size);
433 iu = sk->completed_inurb;
434 id = iu->iso_frame_desc;
440 static void loop_back(struct usb_stream *s)
445 static void stream_idle(struct usb_stream_kernel *sk,
446 struct urb *inurb, struct urb *outurb)
448 struct usb_stream *s = sk->s;
450 int insize = s->idle_insize;
453 s->inpacket_split = s->next_inpacket_split;
454 s->inpacket_split_at = s->next_inpacket_split_at;
455 s->next_inpacket_split = -1;
456 s->next_inpacket_split_at = 0;
458 for (p = 0; p < inurb->number_of_packets; ++p) {
459 struct usb_iso_packet_descriptor *id = inurb->iso_frame_desc;
461 l = id[p].actual_length;
462 if (unlikely(l == 0 || id[p].status)) {
463 snd_printk(KERN_WARNING "underrun, status=%u\n",
468 s->inpacket_head %= s->inpackets;
469 if (s->inpacket_split == -1)
470 s->inpacket_split = s->inpacket_head;
472 s->inpacket[s->inpacket_head].offset =
473 id[p].offset + (inurb->transfer_buffer - (void *)s);
474 s->inpacket[s->inpacket_head].length = l;
475 if (insize + l > s->period_size &&
476 s->next_inpacket_split == -1) {
477 s->next_inpacket_split = s->inpacket_head;
478 s->next_inpacket_split_at = s->period_size - insize;
483 s->idle_insize += urb_size - s->period_size;
484 if (s->idle_insize < 0) {
485 snd_printk(KERN_WARNING "%i\n",
486 (s->idle_insize)/(int)s->cfg.frame_size);
489 s->insize_done += urb_size;
492 s->outpacket[0].offset = (sk->idle_outurb->transfer_buffer -
495 if (usb_stream_prepare_playback(sk, inurb) < 0)
498 s->outpacket[0].length = sk->idle_outurb->transfer_buffer_length + l;
499 s->outpacket[1].offset = sk->completed_outurb->transfer_buffer -
502 if (submit_urbs(sk, inurb, outurb) < 0)
507 wake_up_all(&sk->sleep);
510 s->state = usb_stream_xrun;
511 wake_up_all(&sk->sleep);
514 static void i_capture_idle(struct urb *urb)
516 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;
526 if (balance_playback(sk, urb))
527 stream_idle(sk, sk->i_urb, urb);
530 static void stream_start(struct usb_stream_kernel *sk,
531 struct urb *inurb, struct urb *outurb)
533 struct usb_stream *s = sk->s;
535 if (s->state >= usb_stream_sync1) {
536 int l, p, max_diff, max_diff_0;
538 unsigned int frames_per_packet, min_frames = 0;
540 frames_per_packet = (s->period_size - s->idle_insize);
541 frames_per_packet <<= 8;
543 s->cfg.frame_size * inurb->number_of_packets;
546 max_diff_0 = s->cfg.frame_size;
547 if (s->cfg.period_frames >= 256)
549 if (s->cfg.period_frames >= 1024)
551 max_diff = max_diff_0;
552 for (p = 0; p < inurb->number_of_packets; ++p) {
555 l = inurb->iso_frame_desc[p].actual_length;
558 min_frames += frames_per_packet;
560 (min_frames >> 8) * s->cfg.frame_size;
561 if (diff < max_diff) {
562 snd_printdd(KERN_DEBUG "%i %i %i %i\n",
564 urb_size / (int)s->cfg.frame_size,
565 inurb->number_of_packets, diff);
569 s->idle_insize -= max_diff - max_diff_0;
570 s->idle_insize += urb_size - s->period_size;
571 if (s->idle_insize < 0) {
572 snd_printk(KERN_WARNING "%i %i %i\n",
573 s->idle_insize, urb_size, s->period_size);
575 } else if (s->idle_insize == 0) {
576 s->next_inpacket_split =
577 (s->inpacket_head + 1) % s->inpackets;
578 s->next_inpacket_split_at = 0;
580 unsigned int split = s->inpacket_head;
583 while (l > s->inpacket[split].length) {
584 l -= s->inpacket[split].length;
586 split = s->inpackets - 1;
590 s->next_inpacket_split = split;
591 s->next_inpacket_split_at =
592 s->inpacket[split].length - l;
595 s->insize_done += urb_size;
597 if (usb_stream_prepare_playback(sk, inurb) < 0)
601 playback_prep_freqn(sk, sk->idle_outurb);
603 if (submit_urbs(sk, inurb, outurb) < 0)
606 if (s->state == usb_stream_sync1 && s->insize_done > 360000) {
607 /* just guesswork ^^^^^^ */
608 s->state = usb_stream_ready;
609 subs_set_complete(sk->inurb, i_capture_idle);
610 subs_set_complete(sk->outurb, i_playback_idle);
614 static void i_capture_start(struct urb *urb)
616 struct usb_iso_packet_descriptor *id = urb->iso_frame_desc;
617 struct usb_stream_kernel *sk = urb->context;
618 struct usb_stream *s = sk->s;
623 snd_printk(KERN_WARNING "status=%i\n", urb->status);
627 for (p = 0; p < urb->number_of_packets; ++p) {
628 int l = id[p].actual_length;
630 if (l < s->cfg.frame_size) {
632 if (s->state >= usb_stream_sync0) {
633 snd_printk(KERN_WARNING "%i\n", l);
638 s->inpacket_head %= s->inpackets;
639 s->inpacket[s->inpacket_head].offset =
640 id[p].offset + (urb->transfer_buffer - (void *)s);
641 s->inpacket[s->inpacket_head].length = l;
645 printk(KERN_DEBUG"%s:%i: %i", __func__, __LINE__,
646 urb->iso_frame_desc[0].actual_length);
647 for (pack = 1; pack < urb->number_of_packets; ++pack) {
648 int l = urb->iso_frame_desc[pack].actual_length;
650 printk(KERN_CONT " %i", l);
652 printk(KERN_CONT "\n");
655 if (!empty && s->state < usb_stream_sync1)
658 if (balance_capture(sk, urb))
659 stream_start(sk, urb, sk->i_urb);
662 static void i_playback_start(struct urb *urb)
664 struct usb_stream_kernel *sk = urb->context;
666 if (balance_playback(sk, urb))
667 stream_start(sk, sk->i_urb, urb);
670 int usb_stream_start(struct usb_stream_kernel *sk)
672 struct usb_stream *s = sk->s;
673 int frame = 0, iters = 0;
677 if (s->state != usb_stream_stopped)
680 subs_set_complete(sk->inurb, i_capture_start);
681 subs_set_complete(sk->outurb, i_playback_start);
682 memset(sk->write_page, 0, s->write_size);
688 s->inpacket_head = -1;
689 sk->iso_frame_balance = 0;
691 for (u = 0; u < 2; u++) {
692 struct urb *inurb = sk->inurb[u];
693 struct urb *outurb = sk->outurb[u];
695 playback_prep_freqn(sk, outurb);
696 inurb->number_of_packets = outurb->number_of_packets;
697 inurb->transfer_buffer_length =
698 inurb->number_of_packets *
699 inurb->iso_frame_desc[0].length;
703 struct usb_device *dev = inurb->dev;
705 frame = usb_get_current_frame_number(dev);
707 now = usb_get_current_frame_number(dev);
709 } while (now > -1 && now == frame);
711 err = usb_submit_urb(inurb, GFP_ATOMIC);
714 "usb_submit_urb(sk->inurb[%i]) returned %i\n",
718 err = usb_submit_urb(outurb, GFP_ATOMIC);
721 "usb_submit_urb(sk->outurb[%i]) returned %i\n",
726 if (inurb->start_frame != outurb->start_frame) {
727 snd_printd(KERN_DEBUG
728 "u[%i] start_frames differ in:%u out:%u\n",
729 u, inurb->start_frame, outurb->start_frame);
733 snd_printdd(KERN_DEBUG "%i %i\n", frame, iters);
740 snd_printd(KERN_DEBUG "goto dotry;\n");
743 snd_printk(KERN_WARNING
744 "couldn't start all urbs on the same start_frame.\n");
748 sk->idle_inurb = sk->inurb[USB_STREAM_NURBS - 2];
749 sk->idle_outurb = sk->outurb[USB_STREAM_NURBS - 2];
750 sk->completed_inurb = sk->inurb[USB_STREAM_NURBS - 1];
751 sk->completed_outurb = sk->outurb[USB_STREAM_NURBS - 1];
757 while (s->state != usb_stream_ready && wait_ms > 0) {
758 snd_printdd(KERN_DEBUG "%i\n", s->state);
764 return s->state == usb_stream_ready ? 0 : -EFAULT;
770 void usb_stream_stop(struct usb_stream_kernel *sk)
776 for (u = 0; u < USB_STREAM_NURBS; ++u) {
777 usb_kill_urb(sk->inurb[u]);
778 usb_kill_urb(sk->outurb[u]);
780 sk->s->state = usb_stream_stopped;