1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2007, 2008 Karsten Wiese <fzu@wemgehoertderstaat.de>
9 #include "usb_stream.h"
14 static unsigned usb_stream_next_packet_size(struct usb_stream_kernel *sk)
16 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);
28 if (s->idle_outsize + lb + l > s->period_size)
31 sk->out_phase = sk->out_phase_peeked;
32 urb->iso_frame_desc[pack].offset = lb;
33 urb->iso_frame_desc[pack].length = l;
36 snd_printdd(KERN_DEBUG "%i\n", lb);
39 urb->number_of_packets = pack;
40 urb->transfer_buffer_length = lb;
41 s->idle_outsize += lb - s->period_size;
42 snd_printdd(KERN_DEBUG "idle=%i ul=%i ps=%i\n", s->idle_outsize,
46 static int init_pipe_urbs(struct usb_stream_kernel *sk, unsigned use_packsize,
47 struct urb **urbs, char *transfer,
48 struct usb_device *dev, int pipe)
51 int maxpacket = use_packsize ?
52 use_packsize : usb_maxpacket(dev, pipe, usb_pipeout(pipe));
53 int transfer_length = maxpacket * sk->n_o_ps;
55 for (u = 0; u < USB_STREAM_NURBS;
56 ++u, transfer += transfer_length) {
57 struct urb *urb = urbs[u];
58 struct usb_iso_packet_descriptor *desc;
59 urb->transfer_buffer = transfer;
62 urb->number_of_packets = sk->n_o_ps;
65 if (usb_pipeout(pipe))
67 if (usb_urb_ep_type_check(urb))
70 urb->transfer_buffer_length = transfer_length;
71 desc = urb->iso_frame_desc;
73 desc->length = maxpacket;
74 for (p = 1; p < sk->n_o_ps; ++p) {
75 desc[p].offset = desc[p - 1].offset + maxpacket;
76 desc[p].length = maxpacket;
83 static int init_urbs(struct usb_stream_kernel *sk, unsigned use_packsize,
84 struct usb_device *dev, int in_pipe, int out_pipe)
86 struct usb_stream *s = sk->s;
87 char *indata = (char *)s + sizeof(*s) +
88 sizeof(struct usb_stream_packet) *
92 for (u = 0; u < USB_STREAM_NURBS; ++u) {
93 sk->inurb[u] = usb_alloc_urb(sk->n_o_ps, GFP_KERNEL);
97 sk->outurb[u] = usb_alloc_urb(sk->n_o_ps, GFP_KERNEL);
102 if (init_pipe_urbs(sk, use_packsize, sk->inurb, indata, dev, in_pipe) ||
103 init_pipe_urbs(sk, use_packsize, sk->outurb, sk->write_page, dev,
112 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
113 * this will overflow at approx 524 kHz
115 static inline unsigned get_usb_full_speed_rate(unsigned rate)
117 return ((rate << 13) + 62) / 125;
121 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
122 * this will overflow at approx 4 MHz
124 static inline unsigned get_usb_high_speed_rate(unsigned rate)
126 return ((rate << 10) + 62) / 125;
129 void usb_stream_free(struct usb_stream_kernel *sk)
131 struct usb_stream *s;
134 for (u = 0; u < USB_STREAM_NURBS; ++u) {
135 usb_free_urb(sk->inurb[u]);
137 usb_free_urb(sk->outurb[u]);
138 sk->outurb[u] = NULL;
145 free_pages_exact(sk->write_page, s->write_size);
146 sk->write_page = NULL;
147 free_pages_exact(s, s->read_size);
151 struct usb_stream *usb_stream_new(struct usb_stream_kernel *sk,
152 struct usb_device *dev,
153 unsigned in_endpoint, unsigned out_endpoint,
154 unsigned sample_rate, unsigned use_packsize,
155 unsigned period_frames, unsigned frame_size)
157 int packets, max_packsize;
158 int in_pipe, out_pipe;
159 int read_size = sizeof(struct usb_stream);
161 int usb_frames = dev->speed == USB_SPEED_HIGH ? 8000 : 1000;
163 in_pipe = usb_rcvisocpipe(dev, in_endpoint);
164 out_pipe = usb_sndisocpipe(dev, out_endpoint);
166 max_packsize = use_packsize ?
167 use_packsize : usb_maxpacket(dev, in_pipe, 0);
170 t_period = period_frames / sample_rate
171 iso_packs = t_period / t_iso_frame
172 = (period_frames / sample_rate) * (1 / t_iso_frame)
175 packets = period_frames * usb_frames / sample_rate + 1;
177 if (dev->speed == USB_SPEED_HIGH)
178 packets = (packets + 7) & ~7;
180 read_size += packets * USB_STREAM_URBDEPTH *
181 (max_packsize + sizeof(struct usb_stream_packet));
183 max_packsize = usb_maxpacket(dev, out_pipe, 1);
184 write_size = max_packsize * packets * USB_STREAM_URBDEPTH;
186 if (read_size >= 256*PAGE_SIZE || write_size >= 256*PAGE_SIZE) {
187 snd_printk(KERN_WARNING "a size exceeds 128*PAGE_SIZE\n");
191 sk->s = alloc_pages_exact(read_size,
192 GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN);
194 pr_warn("us122l: couldn't allocate read buffer\n");
197 sk->s->cfg.version = USB_STREAM_INTERFACE_VERSION;
199 sk->s->read_size = read_size;
201 sk->s->cfg.sample_rate = sample_rate;
202 sk->s->cfg.frame_size = frame_size;
203 sk->n_o_ps = packets;
204 sk->s->inpackets = packets * USB_STREAM_URBDEPTH;
205 sk->s->cfg.period_frames = period_frames;
206 sk->s->period_size = frame_size * period_frames;
208 sk->s->write_size = write_size;
210 sk->write_page = alloc_pages_exact(write_size,
211 GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN);
212 if (!sk->write_page) {
213 pr_warn("us122l: couldn't allocate write buffer\n");
218 /* calculate the frequency in 16.16 format */
219 if (dev->speed == USB_SPEED_FULL)
220 sk->freqn = get_usb_full_speed_rate(sample_rate);
222 sk->freqn = get_usb_high_speed_rate(sample_rate);
224 if (init_urbs(sk, use_packsize, dev, in_pipe, out_pipe) < 0) {
229 sk->s->state = usb_stream_stopped;
237 static bool balance_check(struct usb_stream_kernel *sk, struct urb *urb)
240 if (unlikely(urb->status)) {
241 if (urb->status != -ESHUTDOWN && urb->status != -ENOENT)
242 snd_printk(KERN_WARNING "status=%i\n", urb->status);
243 sk->iso_frame_balance = 0x7FFFFFFF;
246 r = sk->iso_frame_balance == 0;
252 static bool balance_playback(struct usb_stream_kernel *sk, struct urb *urb)
254 sk->iso_frame_balance += urb->number_of_packets;
255 return balance_check(sk, urb);
258 static bool balance_capture(struct usb_stream_kernel *sk, struct urb *urb)
260 sk->iso_frame_balance -= urb->number_of_packets;
261 return balance_check(sk, urb);
264 static void subs_set_complete(struct urb **urbs, void (*complete)(struct urb *))
268 for (u = 0; u < USB_STREAM_NURBS; u++) {
269 struct urb *urb = urbs[u];
270 urb->complete = complete;
274 static int usb_stream_prepare_playback(struct usb_stream_kernel *sk,
277 struct usb_stream *s = sk->s;
279 struct usb_iso_packet_descriptor *id, *od;
280 int p = 0, lb = 0, l = 0;
282 io = sk->idle_outurb;
283 od = io->iso_frame_desc;
285 for (; s->sync_packet < 0; ++p, ++s->sync_packet) {
286 struct urb *ii = sk->completed_inurb;
287 id = ii->iso_frame_desc +
288 ii->number_of_packets + s->sync_packet;
289 l = id->actual_length;
297 s->sync_packet < inurb->number_of_packets && p < sk->n_o_ps;
298 ++p, ++s->sync_packet) {
299 l = inurb->iso_frame_desc[s->sync_packet].actual_length;
301 if (s->idle_outsize + lb + l > s->period_size)
310 s->sync_packet -= inurb->number_of_packets;
311 if (unlikely(s->sync_packet < -2 || s->sync_packet > 0)) {
312 snd_printk(KERN_WARNING "invalid sync_packet = %i;"
313 " p=%i nop=%i %i %x %x %x > %x\n",
314 s->sync_packet, p, inurb->number_of_packets,
315 s->idle_outsize + lb + l,
316 s->idle_outsize, lb, l,
320 if (unlikely(lb % s->cfg.frame_size)) {
321 snd_printk(KERN_WARNING"invalid outsize = %i\n",
325 s->idle_outsize += lb - s->period_size;
326 io->number_of_packets = p;
327 io->transfer_buffer_length = lb;
328 if (s->idle_outsize <= 0)
331 snd_printk(KERN_WARNING "idle=%i\n", s->idle_outsize);
335 static void prepare_inurb(int number_of_packets, struct urb *iu)
337 struct usb_iso_packet_descriptor *id;
340 iu->number_of_packets = number_of_packets;
341 id = iu->iso_frame_desc;
343 for (p = 0; p < iu->number_of_packets - 1; ++p)
344 id[p + 1].offset = id[p].offset + id[p].length;
346 iu->transfer_buffer_length =
347 id[0].length * iu->number_of_packets;
350 static int submit_urbs(struct usb_stream_kernel *sk,
351 struct urb *inurb, struct urb *outurb)
354 prepare_inurb(sk->idle_outurb->number_of_packets, sk->idle_inurb);
355 err = usb_submit_urb(sk->idle_inurb, GFP_ATOMIC);
359 sk->idle_inurb = sk->completed_inurb;
360 sk->completed_inurb = inurb;
361 err = usb_submit_urb(sk->idle_outurb, GFP_ATOMIC);
365 sk->idle_outurb = sk->completed_outurb;
366 sk->completed_outurb = outurb;
370 snd_printk(KERN_ERR "%i\n", err);
374 #ifdef DEBUG_LOOP_BACK
376 This loop_back() shows how to read/write the period data.
378 static void loop_back(struct usb_stream *s)
383 struct usb_iso_packet_descriptor *id;
385 o = s->playback1st_to;
386 ol = s->playback1st_size;
389 if (s->insplit_pack >= 0) {
391 id = iu->iso_frame_desc;
396 for (; p < iu->number_of_packets && l < s->period_size; ++p) {
397 i = iu->transfer_buffer + id[p].offset;
398 il = id[p].actual_length;
399 if (l + il > s->period_size)
400 il = s->period_size - l;
409 memcpy(o, i + ol, il - ol);
411 ol = s->period_size - s->playback1st_size;
415 if (iu == sk->completed_inurb) {
416 if (l != s->period_size)
417 printk(KERN_DEBUG"%s:%i %i\n", __func__, __LINE__,
418 l/(int)s->cfg.frame_size);
423 iu = sk->completed_inurb;
424 id = iu->iso_frame_desc;
430 static void loop_back(struct usb_stream *s)
435 static void stream_idle(struct usb_stream_kernel *sk,
436 struct urb *inurb, struct urb *outurb)
438 struct usb_stream *s = sk->s;
440 int insize = s->idle_insize;
443 s->inpacket_split = s->next_inpacket_split;
444 s->inpacket_split_at = s->next_inpacket_split_at;
445 s->next_inpacket_split = -1;
446 s->next_inpacket_split_at = 0;
448 for (p = 0; p < inurb->number_of_packets; ++p) {
449 struct usb_iso_packet_descriptor *id = inurb->iso_frame_desc;
450 l = id[p].actual_length;
451 if (unlikely(l == 0 || id[p].status)) {
452 snd_printk(KERN_WARNING "underrun, status=%u\n",
457 s->inpacket_head %= s->inpackets;
458 if (s->inpacket_split == -1)
459 s->inpacket_split = s->inpacket_head;
461 s->inpacket[s->inpacket_head].offset =
462 id[p].offset + (inurb->transfer_buffer - (void *)s);
463 s->inpacket[s->inpacket_head].length = l;
464 if (insize + l > s->period_size &&
465 s->next_inpacket_split == -1) {
466 s->next_inpacket_split = s->inpacket_head;
467 s->next_inpacket_split_at = s->period_size - insize;
472 s->idle_insize += urb_size - s->period_size;
473 if (s->idle_insize < 0) {
474 snd_printk(KERN_WARNING "%i\n",
475 (s->idle_insize)/(int)s->cfg.frame_size);
478 s->insize_done += urb_size;
481 s->outpacket[0].offset = (sk->idle_outurb->transfer_buffer -
484 if (usb_stream_prepare_playback(sk, inurb) < 0)
487 s->outpacket[0].length = sk->idle_outurb->transfer_buffer_length + l;
488 s->outpacket[1].offset = sk->completed_outurb->transfer_buffer -
491 if (submit_urbs(sk, inurb, outurb) < 0)
496 wake_up_all(&sk->sleep);
499 s->state = usb_stream_xrun;
500 wake_up_all(&sk->sleep);
503 static void i_capture_idle(struct urb *urb)
505 struct usb_stream_kernel *sk = urb->context;
506 if (balance_capture(sk, urb))
507 stream_idle(sk, urb, sk->i_urb);
510 static void i_playback_idle(struct urb *urb)
512 struct usb_stream_kernel *sk = urb->context;
513 if (balance_playback(sk, urb))
514 stream_idle(sk, sk->i_urb, urb);
517 static void stream_start(struct usb_stream_kernel *sk,
518 struct urb *inurb, struct urb *outurb)
520 struct usb_stream *s = sk->s;
521 if (s->state >= usb_stream_sync1) {
522 int l, p, max_diff, max_diff_0;
524 unsigned frames_per_packet, min_frames = 0;
525 frames_per_packet = (s->period_size - s->idle_insize);
526 frames_per_packet <<= 8;
528 s->cfg.frame_size * inurb->number_of_packets;
531 max_diff_0 = s->cfg.frame_size;
532 if (s->cfg.period_frames >= 256)
534 if (s->cfg.period_frames >= 1024)
536 max_diff = max_diff_0;
537 for (p = 0; p < inurb->number_of_packets; ++p) {
539 l = inurb->iso_frame_desc[p].actual_length;
542 min_frames += frames_per_packet;
544 (min_frames >> 8) * s->cfg.frame_size;
545 if (diff < max_diff) {
546 snd_printdd(KERN_DEBUG "%i %i %i %i\n",
548 urb_size / (int)s->cfg.frame_size,
549 inurb->number_of_packets, diff);
553 s->idle_insize -= max_diff - max_diff_0;
554 s->idle_insize += urb_size - s->period_size;
555 if (s->idle_insize < 0) {
556 snd_printk(KERN_WARNING "%i %i %i\n",
557 s->idle_insize, urb_size, s->period_size);
559 } else if (s->idle_insize == 0) {
560 s->next_inpacket_split =
561 (s->inpacket_head + 1) % s->inpackets;
562 s->next_inpacket_split_at = 0;
564 unsigned split = s->inpacket_head;
566 while (l > s->inpacket[split].length) {
567 l -= s->inpacket[split].length;
569 split = s->inpackets - 1;
573 s->next_inpacket_split = split;
574 s->next_inpacket_split_at =
575 s->inpacket[split].length - l;
578 s->insize_done += urb_size;
580 if (usb_stream_prepare_playback(sk, inurb) < 0)
584 playback_prep_freqn(sk, sk->idle_outurb);
586 if (submit_urbs(sk, inurb, outurb) < 0)
589 if (s->state == usb_stream_sync1 && s->insize_done > 360000) {
590 /* just guesswork ^^^^^^ */
591 s->state = usb_stream_ready;
592 subs_set_complete(sk->inurb, i_capture_idle);
593 subs_set_complete(sk->outurb, i_playback_idle);
597 static void i_capture_start(struct urb *urb)
599 struct usb_iso_packet_descriptor *id = urb->iso_frame_desc;
600 struct usb_stream_kernel *sk = urb->context;
601 struct usb_stream *s = sk->s;
606 snd_printk(KERN_WARNING "status=%i\n", urb->status);
610 for (p = 0; p < urb->number_of_packets; ++p) {
611 int l = id[p].actual_length;
612 if (l < s->cfg.frame_size) {
614 if (s->state >= usb_stream_sync0) {
615 snd_printk(KERN_WARNING "%i\n", l);
620 s->inpacket_head %= s->inpackets;
621 s->inpacket[s->inpacket_head].offset =
622 id[p].offset + (urb->transfer_buffer - (void *)s);
623 s->inpacket[s->inpacket_head].length = l;
627 printk(KERN_DEBUG"%s:%i: %i", __func__, __LINE__,
628 urb->iso_frame_desc[0].actual_length);
629 for (pack = 1; pack < urb->number_of_packets; ++pack) {
630 int l = urb->iso_frame_desc[pack].actual_length;
631 printk(KERN_CONT " %i", l);
633 printk(KERN_CONT "\n");
636 if (!empty && s->state < usb_stream_sync1)
639 if (balance_capture(sk, urb))
640 stream_start(sk, urb, sk->i_urb);
643 static void i_playback_start(struct urb *urb)
645 struct usb_stream_kernel *sk = urb->context;
646 if (balance_playback(sk, urb))
647 stream_start(sk, sk->i_urb, urb);
650 int usb_stream_start(struct usb_stream_kernel *sk)
652 struct usb_stream *s = sk->s;
653 int frame = 0, iters = 0;
657 if (s->state != usb_stream_stopped)
660 subs_set_complete(sk->inurb, i_capture_start);
661 subs_set_complete(sk->outurb, i_playback_start);
662 memset(sk->write_page, 0, s->write_size);
668 s->inpacket_head = -1;
669 sk->iso_frame_balance = 0;
671 for (u = 0; u < 2; u++) {
672 struct urb *inurb = sk->inurb[u];
673 struct urb *outurb = sk->outurb[u];
674 playback_prep_freqn(sk, outurb);
675 inurb->number_of_packets = outurb->number_of_packets;
676 inurb->transfer_buffer_length =
677 inurb->number_of_packets *
678 inurb->iso_frame_desc[0].length;
682 struct usb_device *dev = inurb->dev;
683 frame = usb_get_current_frame_number(dev);
685 now = usb_get_current_frame_number(dev);
687 } while (now > -1 && now == frame);
689 err = usb_submit_urb(inurb, GFP_ATOMIC);
691 snd_printk(KERN_ERR"usb_submit_urb(sk->inurb[%i])"
692 " returned %i\n", u, err);
695 err = usb_submit_urb(outurb, GFP_ATOMIC);
697 snd_printk(KERN_ERR"usb_submit_urb(sk->outurb[%i])"
698 " returned %i\n", u, err);
702 if (inurb->start_frame != outurb->start_frame) {
703 snd_printd(KERN_DEBUG
704 "u[%i] start_frames differ in:%u out:%u\n",
705 u, inurb->start_frame, outurb->start_frame);
709 snd_printdd(KERN_DEBUG "%i %i\n", frame, iters);
716 snd_printd(KERN_DEBUG "goto dotry;\n");
719 snd_printk(KERN_WARNING"couldn't start"
720 " all urbs on the same start_frame.\n");
724 sk->idle_inurb = sk->inurb[USB_STREAM_NURBS - 2];
725 sk->idle_outurb = sk->outurb[USB_STREAM_NURBS - 2];
726 sk->completed_inurb = sk->inurb[USB_STREAM_NURBS - 1];
727 sk->completed_outurb = sk->outurb[USB_STREAM_NURBS - 1];
732 while (s->state != usb_stream_ready && wait_ms > 0) {
733 snd_printdd(KERN_DEBUG "%i\n", s->state);
739 return s->state == usb_stream_ready ? 0 : -EFAULT;
745 void usb_stream_stop(struct usb_stream_kernel *sk)
750 for (u = 0; u < USB_STREAM_NURBS; ++u) {
751 usb_kill_urb(sk->inurb[u]);
752 usb_kill_urb(sk->outurb[u]);
754 sk->s->state = usb_stream_stopped;