1
2
3
4
5#include <linux/gfp.h>
6#include <linux/init.h>
7#include <linux/ratelimit.h>
8#include <linux/usb.h>
9#include <linux/usb/audio.h>
10#include <linux/slab.h>
11
12#include <sound/core.h>
13#include <sound/pcm.h>
14#include <sound/pcm_params.h>
15
16#include "usbaudio.h"
17#include "helper.h"
18#include "card.h"
19#include "endpoint.h"
20#include "pcm.h"
21#include "clock.h"
22#include "quirks.h"
23
24enum {
25 EP_STATE_STOPPED,
26 EP_STATE_RUNNING,
27 EP_STATE_STOPPING,
28};
29
30
31struct snd_usb_iface_ref {
32 unsigned char iface;
33 bool need_setup;
34 int opened;
35 struct list_head list;
36};
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69static inline unsigned get_usb_full_speed_rate(unsigned int rate)
70{
71 return ((rate << 13) + 62) / 125;
72}
73
74
75
76
77
78static inline unsigned get_usb_high_speed_rate(unsigned int rate)
79{
80 return ((rate << 10) + 62) / 125;
81}
82
83
84
85
86static void release_urb_ctx(struct snd_urb_ctx *u)
87{
88 if (u->buffer_size)
89 usb_free_coherent(u->ep->chip->dev, u->buffer_size,
90 u->urb->transfer_buffer,
91 u->urb->transfer_dma);
92 usb_free_urb(u->urb);
93 u->urb = NULL;
94}
95
96static const char *usb_error_string(int err)
97{
98 switch (err) {
99 case -ENODEV:
100 return "no device";
101 case -ENOENT:
102 return "endpoint not enabled";
103 case -EPIPE:
104 return "endpoint stalled";
105 case -ENOSPC:
106 return "not enough bandwidth";
107 case -ESHUTDOWN:
108 return "device disabled";
109 case -EHOSTUNREACH:
110 return "device suspended";
111 case -EINVAL:
112 case -EAGAIN:
113 case -EFBIG:
114 case -EMSGSIZE:
115 return "internal error";
116 default:
117 return "unknown error";
118 }
119}
120
121static inline bool ep_state_running(struct snd_usb_endpoint *ep)
122{
123 return atomic_read(&ep->state) == EP_STATE_RUNNING;
124}
125
126static inline bool ep_state_update(struct snd_usb_endpoint *ep, int old, int new)
127{
128 return atomic_cmpxchg(&ep->state, old, new) == old;
129}
130
131
132
133
134
135
136
137
138
139int snd_usb_endpoint_implicit_feedback_sink(struct snd_usb_endpoint *ep)
140{
141 return ep->implicit_fb_sync && usb_pipeout(ep->pipe);
142}
143
144
145
146
147
148
149
150
151static int slave_next_packet_size(struct snd_usb_endpoint *ep)
152{
153 unsigned long flags;
154 int ret;
155
156 if (ep->fill_max)
157 return ep->maxframesize;
158
159 spin_lock_irqsave(&ep->lock, flags);
160 ep->phase = (ep->phase & 0xffff)
161 + (ep->freqm << ep->datainterval);
162 ret = min(ep->phase >> 16, ep->maxframesize);
163 spin_unlock_irqrestore(&ep->lock, flags);
164
165 return ret;
166}
167
168
169
170
171
172static int next_packet_size(struct snd_usb_endpoint *ep)
173{
174 int ret;
175
176 if (ep->fill_max)
177 return ep->maxframesize;
178
179 ep->sample_accum += ep->sample_rem;
180 if (ep->sample_accum >= ep->pps) {
181 ep->sample_accum -= ep->pps;
182 ret = ep->packsize[1];
183 } else {
184 ret = ep->packsize[0];
185 }
186
187 return ret;
188}
189
190
191
192
193
194int snd_usb_endpoint_next_packet_size(struct snd_usb_endpoint *ep,
195 struct snd_urb_ctx *ctx, int idx)
196{
197 if (ctx->packet_size[idx])
198 return ctx->packet_size[idx];
199 else if (ep->sync_source)
200 return slave_next_packet_size(ep);
201 else
202 return next_packet_size(ep);
203}
204
205static void call_retire_callback(struct snd_usb_endpoint *ep,
206 struct urb *urb)
207{
208 struct snd_usb_substream *data_subs;
209
210 data_subs = READ_ONCE(ep->data_subs);
211 if (data_subs && ep->retire_data_urb)
212 ep->retire_data_urb(data_subs, urb);
213}
214
215static void retire_outbound_urb(struct snd_usb_endpoint *ep,
216 struct snd_urb_ctx *urb_ctx)
217{
218 call_retire_callback(ep, urb_ctx->urb);
219}
220
221static void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
222 struct snd_usb_endpoint *sender,
223 const struct urb *urb);
224
225static void retire_inbound_urb(struct snd_usb_endpoint *ep,
226 struct snd_urb_ctx *urb_ctx)
227{
228 struct urb *urb = urb_ctx->urb;
229 struct snd_usb_endpoint *sync_sink;
230
231 if (unlikely(ep->skip_packets > 0)) {
232 ep->skip_packets--;
233 return;
234 }
235
236 sync_sink = READ_ONCE(ep->sync_sink);
237 if (sync_sink)
238 snd_usb_handle_sync_urb(sync_sink, ep, urb);
239
240 call_retire_callback(ep, urb);
241}
242
243static inline bool has_tx_length_quirk(struct snd_usb_audio *chip)
244{
245 return chip->quirk_flags & QUIRK_FLAG_TX_LENGTH;
246}
247
248static void prepare_silent_urb(struct snd_usb_endpoint *ep,
249 struct snd_urb_ctx *ctx)
250{
251 struct urb *urb = ctx->urb;
252 unsigned int offs = 0;
253 unsigned int extra = 0;
254 __le32 packet_length;
255 int i;
256
257
258 if (has_tx_length_quirk(ep->chip))
259 extra = sizeof(packet_length);
260
261 for (i = 0; i < ctx->packets; ++i) {
262 unsigned int offset;
263 unsigned int length;
264 int counts;
265
266 counts = snd_usb_endpoint_next_packet_size(ep, ctx, i);
267 length = counts * ep->stride;
268 offset = offs * ep->stride + extra * i;
269 urb->iso_frame_desc[i].offset = offset;
270 urb->iso_frame_desc[i].length = length + extra;
271 if (extra) {
272 packet_length = cpu_to_le32(length);
273 memcpy(urb->transfer_buffer + offset,
274 &packet_length, sizeof(packet_length));
275 }
276 memset(urb->transfer_buffer + offset + extra,
277 ep->silence_value, length);
278 offs += counts;
279 }
280
281 urb->number_of_packets = ctx->packets;
282 urb->transfer_buffer_length = offs * ep->stride + ctx->packets * extra;
283 ctx->queued = 0;
284}
285
286
287
288
289static void prepare_outbound_urb(struct snd_usb_endpoint *ep,
290 struct snd_urb_ctx *ctx)
291{
292 struct urb *urb = ctx->urb;
293 unsigned char *cp = urb->transfer_buffer;
294 struct snd_usb_substream *data_subs;
295
296 urb->dev = ep->chip->dev;
297
298 switch (ep->type) {
299 case SND_USB_ENDPOINT_TYPE_DATA:
300 data_subs = READ_ONCE(ep->data_subs);
301 if (data_subs && ep->prepare_data_urb)
302 ep->prepare_data_urb(data_subs, urb);
303 else
304 prepare_silent_urb(ep, ctx);
305 break;
306
307 case SND_USB_ENDPOINT_TYPE_SYNC:
308 if (snd_usb_get_speed(ep->chip->dev) >= USB_SPEED_HIGH) {
309
310
311
312
313 urb->iso_frame_desc[0].length = 4;
314 urb->iso_frame_desc[0].offset = 0;
315 cp[0] = ep->freqn;
316 cp[1] = ep->freqn >> 8;
317 cp[2] = ep->freqn >> 16;
318 cp[3] = ep->freqn >> 24;
319 } else {
320
321
322
323
324 urb->iso_frame_desc[0].length = 3;
325 urb->iso_frame_desc[0].offset = 0;
326 cp[0] = ep->freqn >> 2;
327 cp[1] = ep->freqn >> 10;
328 cp[2] = ep->freqn >> 18;
329 }
330
331 break;
332 }
333}
334
335
336
337
338static inline void prepare_inbound_urb(struct snd_usb_endpoint *ep,
339 struct snd_urb_ctx *urb_ctx)
340{
341 int i, offs;
342 struct urb *urb = urb_ctx->urb;
343
344 urb->dev = ep->chip->dev;
345
346 switch (ep->type) {
347 case SND_USB_ENDPOINT_TYPE_DATA:
348 offs = 0;
349 for (i = 0; i < urb_ctx->packets; i++) {
350 urb->iso_frame_desc[i].offset = offs;
351 urb->iso_frame_desc[i].length = ep->curpacksize;
352 offs += ep->curpacksize;
353 }
354
355 urb->transfer_buffer_length = offs;
356 urb->number_of_packets = urb_ctx->packets;
357 break;
358
359 case SND_USB_ENDPOINT_TYPE_SYNC:
360 urb->iso_frame_desc[0].length = min(4u, ep->syncmaxsize);
361 urb->iso_frame_desc[0].offset = 0;
362 break;
363 }
364}
365
366
367static void notify_xrun(struct snd_usb_endpoint *ep)
368{
369 struct snd_usb_substream *data_subs;
370
371 data_subs = READ_ONCE(ep->data_subs);
372 if (data_subs && data_subs->pcm_substream)
373 snd_pcm_stop_xrun(data_subs->pcm_substream);
374}
375
376static struct snd_usb_packet_info *
377next_packet_fifo_enqueue(struct snd_usb_endpoint *ep)
378{
379 struct snd_usb_packet_info *p;
380
381 p = ep->next_packet + (ep->next_packet_head + ep->next_packet_queued) %
382 ARRAY_SIZE(ep->next_packet);
383 ep->next_packet_queued++;
384 return p;
385}
386
387static struct snd_usb_packet_info *
388next_packet_fifo_dequeue(struct snd_usb_endpoint *ep)
389{
390 struct snd_usb_packet_info *p;
391
392 p = ep->next_packet + ep->next_packet_head;
393 ep->next_packet_head++;
394 ep->next_packet_head %= ARRAY_SIZE(ep->next_packet);
395 ep->next_packet_queued--;
396 return p;
397}
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413static void queue_pending_output_urbs(struct snd_usb_endpoint *ep)
414{
415 while (ep_state_running(ep)) {
416
417 unsigned long flags;
418 struct snd_usb_packet_info *packet;
419 struct snd_urb_ctx *ctx = NULL;
420 int err, i;
421
422 spin_lock_irqsave(&ep->lock, flags);
423 if (ep->next_packet_queued > 0 &&
424 !list_empty(&ep->ready_playback_urbs)) {
425
426 ctx = list_first_entry(&ep->ready_playback_urbs,
427 struct snd_urb_ctx, ready_list);
428 list_del_init(&ctx->ready_list);
429
430 packet = next_packet_fifo_dequeue(ep);
431 }
432 spin_unlock_irqrestore(&ep->lock, flags);
433
434 if (ctx == NULL)
435 return;
436
437
438 for (i = 0; i < packet->packets; i++)
439 ctx->packet_size[i] = packet->packet_size[i];
440
441
442 prepare_outbound_urb(ep, ctx);
443
444 err = usb_submit_urb(ctx->urb, GFP_ATOMIC);
445 if (err < 0) {
446 usb_audio_err(ep->chip,
447 "Unable to submit urb #%d: %d at %s\n",
448 ctx->index, err, __func__);
449 notify_xrun(ep);
450 return;
451 }
452
453 set_bit(ctx->index, &ep->active_mask);
454 }
455}
456
457
458
459
460static void snd_complete_urb(struct urb *urb)
461{
462 struct snd_urb_ctx *ctx = urb->context;
463 struct snd_usb_endpoint *ep = ctx->ep;
464 unsigned long flags;
465 int err;
466
467 if (unlikely(urb->status == -ENOENT ||
468 urb->status == -ENODEV ||
469 urb->status == -ECONNRESET ||
470 urb->status == -ESHUTDOWN))
471 goto exit_clear;
472
473 if (unlikely(atomic_read(&ep->chip->shutdown)))
474 goto exit_clear;
475
476 if (unlikely(!ep_state_running(ep)))
477 goto exit_clear;
478
479 if (usb_pipeout(ep->pipe)) {
480 retire_outbound_urb(ep, ctx);
481
482 if (unlikely(!ep_state_running(ep)))
483 goto exit_clear;
484
485 if (snd_usb_endpoint_implicit_feedback_sink(ep)) {
486 spin_lock_irqsave(&ep->lock, flags);
487 list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs);
488 clear_bit(ctx->index, &ep->active_mask);
489 spin_unlock_irqrestore(&ep->lock, flags);
490 queue_pending_output_urbs(ep);
491 return;
492 }
493
494 prepare_outbound_urb(ep, ctx);
495
496 if (unlikely(!ep_state_running(ep)))
497 goto exit_clear;
498 } else {
499 retire_inbound_urb(ep, ctx);
500
501 if (unlikely(!ep_state_running(ep)))
502 goto exit_clear;
503
504 prepare_inbound_urb(ep, ctx);
505 }
506
507 err = usb_submit_urb(urb, GFP_ATOMIC);
508 if (err == 0)
509 return;
510
511 usb_audio_err(ep->chip, "cannot submit urb (err = %d)\n", err);
512 notify_xrun(ep);
513
514exit_clear:
515 clear_bit(ctx->index, &ep->active_mask);
516}
517
518
519
520
521
522
523static struct snd_usb_iface_ref *
524iface_ref_find(struct snd_usb_audio *chip, int iface)
525{
526 struct snd_usb_iface_ref *ip;
527
528 list_for_each_entry(ip, &chip->iface_ref_list, list)
529 if (ip->iface == iface)
530 return ip;
531
532 ip = kzalloc(sizeof(*ip), GFP_KERNEL);
533 if (!ip)
534 return NULL;
535 ip->iface = iface;
536 list_add_tail(&ip->list, &chip->iface_ref_list);
537 return ip;
538}
539
540
541
542
543
544struct snd_usb_endpoint *
545snd_usb_get_endpoint(struct snd_usb_audio *chip, int ep_num)
546{
547 struct snd_usb_endpoint *ep;
548
549 list_for_each_entry(ep, &chip->ep_list, list) {
550 if (ep->ep_num == ep_num)
551 return ep;
552 }
553
554 return NULL;
555}
556
557#define ep_type_name(type) \
558 (type == SND_USB_ENDPOINT_TYPE_DATA ? "data" : "sync")
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578int snd_usb_add_endpoint(struct snd_usb_audio *chip, int ep_num, int type)
579{
580 struct snd_usb_endpoint *ep;
581 bool is_playback;
582
583 ep = snd_usb_get_endpoint(chip, ep_num);
584 if (ep)
585 return 0;
586
587 usb_audio_dbg(chip, "Creating new %s endpoint #%x\n",
588 ep_type_name(type),
589 ep_num);
590 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
591 if (!ep)
592 return -ENOMEM;
593
594 ep->chip = chip;
595 spin_lock_init(&ep->lock);
596 ep->type = type;
597 ep->ep_num = ep_num;
598 INIT_LIST_HEAD(&ep->ready_playback_urbs);
599
600 is_playback = ((ep_num & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT);
601 ep_num &= USB_ENDPOINT_NUMBER_MASK;
602 if (is_playback)
603 ep->pipe = usb_sndisocpipe(chip->dev, ep_num);
604 else
605 ep->pipe = usb_rcvisocpipe(chip->dev, ep_num);
606
607 list_add_tail(&ep->list, &chip->ep_list);
608 return 0;
609}
610
611
612static void endpoint_set_syncinterval(struct snd_usb_audio *chip,
613 struct snd_usb_endpoint *ep)
614{
615 struct usb_host_interface *alts;
616 struct usb_endpoint_descriptor *desc;
617
618 alts = snd_usb_get_host_interface(chip, ep->iface, ep->altsetting);
619 if (!alts)
620 return;
621
622 desc = get_endpoint(alts, ep->ep_idx);
623 if (desc->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
624 desc->bRefresh >= 1 && desc->bRefresh <= 9)
625 ep->syncinterval = desc->bRefresh;
626 else if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL)
627 ep->syncinterval = 1;
628 else if (desc->bInterval >= 1 && desc->bInterval <= 16)
629 ep->syncinterval = desc->bInterval - 1;
630 else
631 ep->syncinterval = 3;
632
633 ep->syncmaxsize = le16_to_cpu(desc->wMaxPacketSize);
634}
635
636static bool endpoint_compatible(struct snd_usb_endpoint *ep,
637 const struct audioformat *fp,
638 const struct snd_pcm_hw_params *params)
639{
640 if (!ep->opened)
641 return false;
642 if (ep->cur_audiofmt != fp)
643 return false;
644 if (ep->cur_rate != params_rate(params) ||
645 ep->cur_format != params_format(params) ||
646 ep->cur_period_frames != params_period_size(params) ||
647 ep->cur_buffer_periods != params_periods(params))
648 return false;
649 return true;
650}
651
652
653
654
655
656bool snd_usb_endpoint_compatible(struct snd_usb_audio *chip,
657 struct snd_usb_endpoint *ep,
658 const struct audioformat *fp,
659 const struct snd_pcm_hw_params *params)
660{
661 bool ret;
662
663 mutex_lock(&chip->mutex);
664 ret = endpoint_compatible(ep, fp, params);
665 mutex_unlock(&chip->mutex);
666 return ret;
667}
668
669
670
671
672
673
674
675
676
677
678
679
680
681struct snd_usb_endpoint *
682snd_usb_endpoint_open(struct snd_usb_audio *chip,
683 const struct audioformat *fp,
684 const struct snd_pcm_hw_params *params,
685 bool is_sync_ep)
686{
687 struct snd_usb_endpoint *ep;
688 int ep_num = is_sync_ep ? fp->sync_ep : fp->endpoint;
689
690 mutex_lock(&chip->mutex);
691 ep = snd_usb_get_endpoint(chip, ep_num);
692 if (!ep) {
693 usb_audio_err(chip, "Cannot find EP 0x%x to open\n", ep_num);
694 goto unlock;
695 }
696
697 if (!ep->opened) {
698 if (is_sync_ep) {
699 ep->iface = fp->sync_iface;
700 ep->altsetting = fp->sync_altsetting;
701 ep->ep_idx = fp->sync_ep_idx;
702 } else {
703 ep->iface = fp->iface;
704 ep->altsetting = fp->altsetting;
705 ep->ep_idx = fp->ep_idx;
706 }
707 usb_audio_dbg(chip, "Open EP 0x%x, iface=%d:%d, idx=%d\n",
708 ep_num, ep->iface, ep->altsetting, ep->ep_idx);
709
710 ep->iface_ref = iface_ref_find(chip, ep->iface);
711 if (!ep->iface_ref) {
712 ep = NULL;
713 goto unlock;
714 }
715
716 ep->cur_audiofmt = fp;
717 ep->cur_channels = fp->channels;
718 ep->cur_rate = params_rate(params);
719 ep->cur_format = params_format(params);
720 ep->cur_frame_bytes = snd_pcm_format_physical_width(ep->cur_format) *
721 ep->cur_channels / 8;
722 ep->cur_period_frames = params_period_size(params);
723 ep->cur_period_bytes = ep->cur_period_frames * ep->cur_frame_bytes;
724 ep->cur_buffer_periods = params_periods(params);
725
726 if (ep->type == SND_USB_ENDPOINT_TYPE_SYNC)
727 endpoint_set_syncinterval(chip, ep);
728
729 ep->implicit_fb_sync = fp->implicit_fb;
730 ep->need_setup = true;
731
732 usb_audio_dbg(chip, " channels=%d, rate=%d, format=%s, period_bytes=%d, periods=%d, implicit_fb=%d\n",
733 ep->cur_channels, ep->cur_rate,
734 snd_pcm_format_name(ep->cur_format),
735 ep->cur_period_bytes, ep->cur_buffer_periods,
736 ep->implicit_fb_sync);
737
738 } else {
739 if (WARN_ON(!ep->iface_ref)) {
740 ep = NULL;
741 goto unlock;
742 }
743
744 if (!endpoint_compatible(ep, fp, params)) {
745 usb_audio_err(chip, "Incompatible EP setup for 0x%x\n",
746 ep_num);
747 ep = NULL;
748 goto unlock;
749 }
750
751 usb_audio_dbg(chip, "Reopened EP 0x%x (count %d)\n",
752 ep_num, ep->opened);
753 }
754
755 if (!ep->iface_ref->opened++)
756 ep->iface_ref->need_setup = true;
757
758 ep->opened++;
759
760 unlock:
761 mutex_unlock(&chip->mutex);
762 return ep;
763}
764
765
766
767
768
769
770void snd_usb_endpoint_set_sync(struct snd_usb_audio *chip,
771 struct snd_usb_endpoint *data_ep,
772 struct snd_usb_endpoint *sync_ep)
773{
774 data_ep->sync_source = sync_ep;
775}
776
777
778
779
780
781
782
783void snd_usb_endpoint_set_callback(struct snd_usb_endpoint *ep,
784 void (*prepare)(struct snd_usb_substream *subs,
785 struct urb *urb),
786 void (*retire)(struct snd_usb_substream *subs,
787 struct urb *urb),
788 struct snd_usb_substream *data_subs)
789{
790 ep->prepare_data_urb = prepare;
791 ep->retire_data_urb = retire;
792 WRITE_ONCE(ep->data_subs, data_subs);
793}
794
795static int endpoint_set_interface(struct snd_usb_audio *chip,
796 struct snd_usb_endpoint *ep,
797 bool set)
798{
799 int altset = set ? ep->altsetting : 0;
800 int err;
801
802 usb_audio_dbg(chip, "Setting usb interface %d:%d for EP 0x%x\n",
803 ep->iface, altset, ep->ep_num);
804 err = usb_set_interface(chip->dev, ep->iface, altset);
805 if (err < 0) {
806 usb_audio_err(chip, "%d:%d: usb_set_interface failed (%d)\n",
807 ep->iface, altset, err);
808 return err;
809 }
810
811 if (chip->quirk_flags & QUIRK_FLAG_IFACE_DELAY)
812 msleep(50);
813 return 0;
814}
815
816
817
818
819
820
821void snd_usb_endpoint_close(struct snd_usb_audio *chip,
822 struct snd_usb_endpoint *ep)
823{
824 mutex_lock(&chip->mutex);
825 usb_audio_dbg(chip, "Closing EP 0x%x (count %d)\n",
826 ep->ep_num, ep->opened);
827
828 if (!--ep->iface_ref->opened)
829 endpoint_set_interface(chip, ep, false);
830
831 if (!--ep->opened) {
832 ep->iface = 0;
833 ep->altsetting = 0;
834 ep->cur_audiofmt = NULL;
835 ep->cur_rate = 0;
836 ep->iface_ref = NULL;
837 usb_audio_dbg(chip, "EP 0x%x closed\n", ep->ep_num);
838 }
839 mutex_unlock(&chip->mutex);
840}
841
842
843void snd_usb_endpoint_suspend(struct snd_usb_endpoint *ep)
844{
845 ep->need_setup = true;
846 if (ep->iface_ref)
847 ep->iface_ref->need_setup = true;
848}
849
850
851
852
853static int wait_clear_urbs(struct snd_usb_endpoint *ep)
854{
855 unsigned long end_time = jiffies + msecs_to_jiffies(1000);
856 int alive;
857
858 if (atomic_read(&ep->state) != EP_STATE_STOPPING)
859 return 0;
860
861 do {
862 alive = bitmap_weight(&ep->active_mask, ep->nurbs);
863 if (!alive)
864 break;
865
866 schedule_timeout_uninterruptible(1);
867 } while (time_before(jiffies, end_time));
868
869 if (alive)
870 usb_audio_err(ep->chip,
871 "timeout: still %d active urbs on EP #%x\n",
872 alive, ep->ep_num);
873
874 if (ep_state_update(ep, EP_STATE_STOPPING, EP_STATE_STOPPED)) {
875 ep->sync_sink = NULL;
876 snd_usb_endpoint_set_callback(ep, NULL, NULL, NULL);
877 }
878
879 return 0;
880}
881
882
883
884
885void snd_usb_endpoint_sync_pending_stop(struct snd_usb_endpoint *ep)
886{
887 if (ep)
888 wait_clear_urbs(ep);
889}
890
891
892
893
894
895
896static int stop_urbs(struct snd_usb_endpoint *ep, bool force)
897{
898 unsigned int i;
899
900 if (!force && atomic_read(&ep->running))
901 return -EBUSY;
902
903 if (!ep_state_update(ep, EP_STATE_RUNNING, EP_STATE_STOPPING))
904 return 0;
905
906 INIT_LIST_HEAD(&ep->ready_playback_urbs);
907 ep->next_packet_head = 0;
908 ep->next_packet_queued = 0;
909
910 for (i = 0; i < ep->nurbs; i++) {
911 if (test_bit(i, &ep->active_mask)) {
912 if (!test_and_set_bit(i, &ep->unlink_mask)) {
913 struct urb *u = ep->urb[i].urb;
914 usb_unlink_urb(u);
915 }
916 }
917 }
918
919 return 0;
920}
921
922
923
924
925static int release_urbs(struct snd_usb_endpoint *ep, bool force)
926{
927 int i, err;
928
929
930 snd_usb_endpoint_set_callback(ep, NULL, NULL, NULL);
931
932
933 err = stop_urbs(ep, force);
934 if (err)
935 return err;
936
937 wait_clear_urbs(ep);
938
939 for (i = 0; i < ep->nurbs; i++)
940 release_urb_ctx(&ep->urb[i]);
941
942 usb_free_coherent(ep->chip->dev, SYNC_URBS * 4,
943 ep->syncbuf, ep->sync_dma);
944
945 ep->syncbuf = NULL;
946 ep->nurbs = 0;
947 return 0;
948}
949
950
951
952
953static int data_ep_set_params(struct snd_usb_endpoint *ep)
954{
955 struct snd_usb_audio *chip = ep->chip;
956 unsigned int maxsize, minsize, packs_per_ms, max_packs_per_urb;
957 unsigned int max_packs_per_period, urbs_per_period, urb_packs;
958 unsigned int max_urbs, i;
959 const struct audioformat *fmt = ep->cur_audiofmt;
960 int frame_bits = ep->cur_frame_bytes * 8;
961 int tx_length_quirk = (has_tx_length_quirk(chip) &&
962 usb_pipeout(ep->pipe));
963
964 usb_audio_dbg(chip, "Setting params for data EP 0x%x, pipe 0x%x\n",
965 ep->ep_num, ep->pipe);
966
967 if (ep->cur_format == SNDRV_PCM_FORMAT_DSD_U16_LE && fmt->dsd_dop) {
968
969
970
971
972
973 frame_bits += ep->cur_channels << 3;
974 }
975
976 ep->datainterval = fmt->datainterval;
977 ep->stride = frame_bits >> 3;
978
979 switch (ep->cur_format) {
980 case SNDRV_PCM_FORMAT_U8:
981 ep->silence_value = 0x80;
982 break;
983 case SNDRV_PCM_FORMAT_DSD_U8:
984 case SNDRV_PCM_FORMAT_DSD_U16_LE:
985 case SNDRV_PCM_FORMAT_DSD_U32_LE:
986 case SNDRV_PCM_FORMAT_DSD_U16_BE:
987 case SNDRV_PCM_FORMAT_DSD_U32_BE:
988 ep->silence_value = 0x69;
989 break;
990 default:
991 ep->silence_value = 0;
992 }
993
994
995 ep->freqmax = ep->freqn + (ep->freqn >> 1);
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011 maxsize = (((ep->freqmax << ep->datainterval) + 0xffff) >> 16) *
1012 (frame_bits >> 3);
1013 if (tx_length_quirk)
1014 maxsize += sizeof(__le32);
1015
1016 if (ep->maxpacksize && ep->maxpacksize < maxsize) {
1017
1018 unsigned int data_maxsize = maxsize = ep->maxpacksize;
1019
1020 if (tx_length_quirk)
1021
1022 data_maxsize -= sizeof(__le32);
1023 ep->freqmax = (data_maxsize / (frame_bits >> 3))
1024 << (16 - ep->datainterval);
1025 }
1026
1027 if (ep->fill_max)
1028 ep->curpacksize = ep->maxpacksize;
1029 else
1030 ep->curpacksize = maxsize;
1031
1032 if (snd_usb_get_speed(chip->dev) != USB_SPEED_FULL) {
1033 packs_per_ms = 8 >> ep->datainterval;
1034 max_packs_per_urb = MAX_PACKS_HS;
1035 } else {
1036 packs_per_ms = 1;
1037 max_packs_per_urb = MAX_PACKS;
1038 }
1039 if (ep->sync_source && !ep->implicit_fb_sync)
1040 max_packs_per_urb = min(max_packs_per_urb,
1041 1U << ep->sync_source->syncinterval);
1042 max_packs_per_urb = max(1u, max_packs_per_urb >> ep->datainterval);
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052 if (usb_pipein(ep->pipe) || ep->implicit_fb_sync) {
1053
1054 urb_packs = packs_per_ms;
1055
1056
1057
1058
1059
1060
1061 if (snd_usb_get_speed(chip->dev) == USB_SPEED_WIRELESS) {
1062 int interval = ep->datainterval;
1063 while (interval < 5) {
1064 urb_packs <<= 1;
1065 ++interval;
1066 }
1067 }
1068
1069 urb_packs = min(max_packs_per_urb, urb_packs);
1070 while (urb_packs > 1 && urb_packs * maxsize >= ep->cur_period_bytes)
1071 urb_packs >>= 1;
1072 ep->nurbs = MAX_URBS;
1073
1074
1075
1076
1077
1078
1079
1080 } else {
1081
1082 minsize = (ep->freqn >> (16 - ep->datainterval)) *
1083 (frame_bits >> 3);
1084
1085 if (ep->sync_source)
1086 minsize -= minsize >> 3;
1087 minsize = max(minsize, 1u);
1088
1089
1090 max_packs_per_period = DIV_ROUND_UP(ep->cur_period_bytes, minsize);
1091
1092
1093 urbs_per_period = DIV_ROUND_UP(max_packs_per_period,
1094 max_packs_per_urb);
1095
1096 urb_packs = DIV_ROUND_UP(max_packs_per_period, urbs_per_period);
1097
1098
1099 ep->max_urb_frames = DIV_ROUND_UP(ep->cur_period_frames,
1100 urbs_per_period);
1101
1102
1103 max_urbs = min((unsigned) MAX_URBS,
1104 MAX_QUEUE * packs_per_ms / urb_packs);
1105 ep->nurbs = min(max_urbs, urbs_per_period * ep->cur_buffer_periods);
1106 }
1107
1108
1109 for (i = 0; i < ep->nurbs; i++) {
1110 struct snd_urb_ctx *u = &ep->urb[i];
1111 u->index = i;
1112 u->ep = ep;
1113 u->packets = urb_packs;
1114 u->buffer_size = maxsize * u->packets;
1115
1116 if (fmt->fmt_type == UAC_FORMAT_TYPE_II)
1117 u->packets++;
1118 u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
1119 if (!u->urb)
1120 goto out_of_memory;
1121
1122 u->urb->transfer_buffer =
1123 usb_alloc_coherent(chip->dev, u->buffer_size,
1124 GFP_KERNEL, &u->urb->transfer_dma);
1125 if (!u->urb->transfer_buffer)
1126 goto out_of_memory;
1127 u->urb->pipe = ep->pipe;
1128 u->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1129 u->urb->interval = 1 << ep->datainterval;
1130 u->urb->context = u;
1131 u->urb->complete = snd_complete_urb;
1132 INIT_LIST_HEAD(&u->ready_list);
1133 }
1134
1135
1136
1137
1138 ep->nominal_queue_size = maxsize * urb_packs * (ep->nurbs + 1);
1139 return 0;
1140
1141out_of_memory:
1142 release_urbs(ep, false);
1143 return -ENOMEM;
1144}
1145
1146
1147
1148
1149static int sync_ep_set_params(struct snd_usb_endpoint *ep)
1150{
1151 struct snd_usb_audio *chip = ep->chip;
1152 int i;
1153
1154 usb_audio_dbg(chip, "Setting params for sync EP 0x%x, pipe 0x%x\n",
1155 ep->ep_num, ep->pipe);
1156
1157 ep->syncbuf = usb_alloc_coherent(chip->dev, SYNC_URBS * 4,
1158 GFP_KERNEL, &ep->sync_dma);
1159 if (!ep->syncbuf)
1160 return -ENOMEM;
1161
1162 for (i = 0; i < SYNC_URBS; i++) {
1163 struct snd_urb_ctx *u = &ep->urb[i];
1164 u->index = i;
1165 u->ep = ep;
1166 u->packets = 1;
1167 u->urb = usb_alloc_urb(1, GFP_KERNEL);
1168 if (!u->urb)
1169 goto out_of_memory;
1170 u->urb->transfer_buffer = ep->syncbuf + i * 4;
1171 u->urb->transfer_dma = ep->sync_dma + i * 4;
1172 u->urb->transfer_buffer_length = 4;
1173 u->urb->pipe = ep->pipe;
1174 u->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1175 u->urb->number_of_packets = 1;
1176 u->urb->interval = 1 << ep->syncinterval;
1177 u->urb->context = u;
1178 u->urb->complete = snd_complete_urb;
1179 }
1180
1181 ep->nurbs = SYNC_URBS;
1182
1183 return 0;
1184
1185out_of_memory:
1186 release_urbs(ep, false);
1187 return -ENOMEM;
1188}
1189
1190
1191
1192
1193
1194
1195
1196
1197static int snd_usb_endpoint_set_params(struct snd_usb_audio *chip,
1198 struct snd_usb_endpoint *ep)
1199{
1200 const struct audioformat *fmt = ep->cur_audiofmt;
1201 int err;
1202
1203
1204 err = release_urbs(ep, false);
1205 if (err < 0)
1206 return err;
1207
1208 ep->datainterval = fmt->datainterval;
1209 ep->maxpacksize = fmt->maxpacksize;
1210 ep->fill_max = !!(fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX);
1211
1212 if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL) {
1213 ep->freqn = get_usb_full_speed_rate(ep->cur_rate);
1214 ep->pps = 1000 >> ep->datainterval;
1215 } else {
1216 ep->freqn = get_usb_high_speed_rate(ep->cur_rate);
1217 ep->pps = 8000 >> ep->datainterval;
1218 }
1219
1220 ep->sample_rem = ep->cur_rate % ep->pps;
1221 ep->packsize[0] = ep->cur_rate / ep->pps;
1222 ep->packsize[1] = (ep->cur_rate + (ep->pps - 1)) / ep->pps;
1223
1224
1225 ep->freqm = ep->freqn;
1226 ep->freqshift = INT_MIN;
1227
1228 ep->phase = 0;
1229
1230 switch (ep->type) {
1231 case SND_USB_ENDPOINT_TYPE_DATA:
1232 err = data_ep_set_params(ep);
1233 break;
1234 case SND_USB_ENDPOINT_TYPE_SYNC:
1235 err = sync_ep_set_params(ep);
1236 break;
1237 default:
1238 err = -EINVAL;
1239 }
1240
1241 usb_audio_dbg(chip, "Set up %d URBS, ret=%d\n", ep->nurbs, err);
1242
1243 if (err < 0)
1244 return err;
1245
1246
1247 ep->maxframesize = ep->maxpacksize / ep->cur_frame_bytes;
1248 ep->curframesize = ep->curpacksize / ep->cur_frame_bytes;
1249
1250 return 0;
1251}
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264int snd_usb_endpoint_configure(struct snd_usb_audio *chip,
1265 struct snd_usb_endpoint *ep)
1266{
1267 bool iface_first;
1268 int err = 0;
1269
1270 mutex_lock(&chip->mutex);
1271 if (WARN_ON(!ep->iface_ref))
1272 goto unlock;
1273 if (!ep->need_setup)
1274 goto unlock;
1275
1276
1277 if (!ep->iface_ref->need_setup) {
1278
1279
1280
1281 if (ep->cur_audiofmt->protocol == UAC_VERSION_1) {
1282 err = snd_usb_init_sample_rate(chip, ep->cur_audiofmt,
1283 ep->cur_rate);
1284 if (err < 0)
1285 goto unlock;
1286 }
1287 err = snd_usb_endpoint_set_params(chip, ep);
1288 if (err < 0)
1289 goto unlock;
1290 goto done;
1291 }
1292
1293
1294 endpoint_set_interface(chip, ep, false);
1295
1296
1297
1298
1299 iface_first = ep->cur_audiofmt->protocol == UAC_VERSION_1;
1300
1301 if (chip->quirk_flags & QUIRK_FLAG_SET_IFACE_FIRST)
1302 iface_first = true;
1303 if (iface_first) {
1304 err = endpoint_set_interface(chip, ep, true);
1305 if (err < 0)
1306 goto unlock;
1307 }
1308
1309 err = snd_usb_init_pitch(chip, ep->cur_audiofmt);
1310 if (err < 0)
1311 goto unlock;
1312
1313 err = snd_usb_init_sample_rate(chip, ep->cur_audiofmt, ep->cur_rate);
1314 if (err < 0)
1315 goto unlock;
1316
1317 err = snd_usb_endpoint_set_params(chip, ep);
1318 if (err < 0)
1319 goto unlock;
1320
1321 err = snd_usb_select_mode_quirk(chip, ep->cur_audiofmt);
1322 if (err < 0)
1323 goto unlock;
1324
1325
1326 if (!iface_first) {
1327 err = endpoint_set_interface(chip, ep, true);
1328 if (err < 0)
1329 goto unlock;
1330 }
1331
1332 ep->iface_ref->need_setup = false;
1333
1334 done:
1335 ep->need_setup = false;
1336 err = 1;
1337
1338unlock:
1339 mutex_unlock(&chip->mutex);
1340 return err;
1341}
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356int snd_usb_endpoint_start(struct snd_usb_endpoint *ep)
1357{
1358 int err;
1359 unsigned int i;
1360
1361 if (atomic_read(&ep->chip->shutdown))
1362 return -EBADFD;
1363
1364 if (ep->sync_source)
1365 WRITE_ONCE(ep->sync_source->sync_sink, ep);
1366
1367 usb_audio_dbg(ep->chip, "Starting %s EP 0x%x (running %d)\n",
1368 ep_type_name(ep->type), ep->ep_num,
1369 atomic_read(&ep->running));
1370
1371
1372 if (atomic_inc_return(&ep->running) != 1)
1373 return 0;
1374
1375 ep->active_mask = 0;
1376 ep->unlink_mask = 0;
1377 ep->phase = 0;
1378 ep->sample_accum = 0;
1379
1380 snd_usb_endpoint_start_quirk(ep);
1381
1382
1383
1384
1385
1386
1387
1388
1389 if (!ep_state_update(ep, EP_STATE_STOPPED, EP_STATE_RUNNING))
1390 goto __error;
1391
1392 if (snd_usb_endpoint_implicit_feedback_sink(ep) &&
1393 !(ep->chip->quirk_flags & QUIRK_FLAG_PLAYBACK_FIRST)) {
1394 for (i = 0; i < ep->nurbs; i++) {
1395 struct snd_urb_ctx *ctx = ep->urb + i;
1396 list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs);
1397 }
1398
1399 usb_audio_dbg(ep->chip, "No URB submission due to implicit fb sync\n");
1400 return 0;
1401 }
1402
1403 for (i = 0; i < ep->nurbs; i++) {
1404 struct urb *urb = ep->urb[i].urb;
1405
1406 if (snd_BUG_ON(!urb))
1407 goto __error;
1408
1409 if (usb_pipeout(ep->pipe)) {
1410 prepare_outbound_urb(ep, urb->context);
1411 } else {
1412 prepare_inbound_urb(ep, urb->context);
1413 }
1414
1415 err = usb_submit_urb(urb, GFP_ATOMIC);
1416 if (err < 0) {
1417 usb_audio_err(ep->chip,
1418 "cannot submit urb %d, error %d: %s\n",
1419 i, err, usb_error_string(err));
1420 goto __error;
1421 }
1422 set_bit(i, &ep->active_mask);
1423 }
1424
1425 usb_audio_dbg(ep->chip, "%d URBs submitted for EP 0x%x\n",
1426 ep->nurbs, ep->ep_num);
1427 return 0;
1428
1429__error:
1430 snd_usb_endpoint_stop(ep);
1431 return -EPIPE;
1432}
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep)
1449{
1450 if (!ep)
1451 return;
1452
1453 usb_audio_dbg(ep->chip, "Stopping %s EP 0x%x (running %d)\n",
1454 ep_type_name(ep->type), ep->ep_num,
1455 atomic_read(&ep->running));
1456
1457 if (snd_BUG_ON(!atomic_read(&ep->running)))
1458 return;
1459
1460 if (!atomic_dec_return(&ep->running)) {
1461 if (ep->sync_source)
1462 WRITE_ONCE(ep->sync_source->sync_sink, NULL);
1463 stop_urbs(ep, false);
1464 }
1465}
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475void snd_usb_endpoint_release(struct snd_usb_endpoint *ep)
1476{
1477 release_urbs(ep, true);
1478}
1479
1480
1481
1482
1483
1484
1485
1486void snd_usb_endpoint_free_all(struct snd_usb_audio *chip)
1487{
1488 struct snd_usb_endpoint *ep, *en;
1489 struct snd_usb_iface_ref *ip, *in;
1490
1491 list_for_each_entry_safe(ep, en, &chip->ep_list, list)
1492 kfree(ep);
1493
1494 list_for_each_entry_safe(ip, in, &chip->iface_ref_list, list)
1495 kfree(ip);
1496}
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508static void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
1509 struct snd_usb_endpoint *sender,
1510 const struct urb *urb)
1511{
1512 int shift;
1513 unsigned int f;
1514 unsigned long flags;
1515
1516 snd_BUG_ON(ep == sender);
1517
1518
1519
1520
1521
1522
1523
1524 if (snd_usb_endpoint_implicit_feedback_sink(ep) &&
1525 atomic_read(&ep->running)) {
1526
1527
1528 int i, bytes = 0;
1529 struct snd_urb_ctx *in_ctx;
1530 struct snd_usb_packet_info *out_packet;
1531
1532 in_ctx = urb->context;
1533
1534
1535 for (i = 0; i < in_ctx->packets; i++)
1536 if (urb->iso_frame_desc[i].status == 0)
1537 bytes += urb->iso_frame_desc[i].actual_length;
1538
1539
1540
1541
1542
1543 if (bytes == 0)
1544 return;
1545
1546 spin_lock_irqsave(&ep->lock, flags);
1547 if (ep->next_packet_queued >= ARRAY_SIZE(ep->next_packet)) {
1548 spin_unlock_irqrestore(&ep->lock, flags);
1549 usb_audio_err(ep->chip,
1550 "next package FIFO overflow EP 0x%x\n",
1551 ep->ep_num);
1552 notify_xrun(ep);
1553 return;
1554 }
1555
1556 out_packet = next_packet_fifo_enqueue(ep);
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568 out_packet->packets = in_ctx->packets;
1569 for (i = 0; i < in_ctx->packets; i++) {
1570 if (urb->iso_frame_desc[i].status == 0)
1571 out_packet->packet_size[i] =
1572 urb->iso_frame_desc[i].actual_length / sender->stride;
1573 else
1574 out_packet->packet_size[i] = 0;
1575 }
1576
1577 spin_unlock_irqrestore(&ep->lock, flags);
1578 queue_pending_output_urbs(ep);
1579
1580 return;
1581 }
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598 if (urb->iso_frame_desc[0].status != 0 ||
1599 urb->iso_frame_desc[0].actual_length < 3)
1600 return;
1601
1602 f = le32_to_cpup(urb->transfer_buffer);
1603 if (urb->iso_frame_desc[0].actual_length == 3)
1604 f &= 0x00ffffff;
1605 else
1606 f &= 0x0fffffff;
1607
1608 if (f == 0)
1609 return;
1610
1611 if (unlikely(sender->tenor_fb_quirk)) {
1612
1613
1614
1615
1616
1617 if (f < ep->freqn - 0x8000)
1618 f += 0xf000;
1619 else if (f > ep->freqn + 0x8000)
1620 f -= 0xf000;
1621 } else if (unlikely(ep->freqshift == INT_MIN)) {
1622
1623
1624
1625
1626
1627
1628 shift = 0;
1629 while (f < ep->freqn - ep->freqn / 4) {
1630 f <<= 1;
1631 shift++;
1632 }
1633 while (f > ep->freqn + ep->freqn / 2) {
1634 f >>= 1;
1635 shift--;
1636 }
1637 ep->freqshift = shift;
1638 } else if (ep->freqshift >= 0)
1639 f <<= ep->freqshift;
1640 else
1641 f >>= -ep->freqshift;
1642
1643 if (likely(f >= ep->freqn - ep->freqn / 8 && f <= ep->freqmax)) {
1644
1645
1646
1647
1648 spin_lock_irqsave(&ep->lock, flags);
1649 ep->freqm = f;
1650 spin_unlock_irqrestore(&ep->lock, flags);
1651 } else {
1652
1653
1654
1655
1656 ep->freqshift = INT_MIN;
1657 }
1658}
1659
1660