Lines Matching full:ep
100 usb_free_coherent(u->ep->chip->dev, u->buffer_size, in release_urb_ctx()
133 static inline bool ep_state_running(struct snd_usb_endpoint *ep) in ep_state_running() argument
135 return atomic_read(&ep->state) == EP_STATE_RUNNING; in ep_state_running()
138 static inline bool ep_state_update(struct snd_usb_endpoint *ep, int old, int new) in ep_state_update() argument
140 return atomic_try_cmpxchg(&ep->state, &old, new); in ep_state_update()
146 * @ep: The snd_usb_endpoint
151 int snd_usb_endpoint_implicit_feedback_sink(struct snd_usb_endpoint *ep) in snd_usb_endpoint_implicit_feedback_sink() argument
153 return ep->implicit_fb_sync && usb_pipeout(ep->pipe); in snd_usb_endpoint_implicit_feedback_sink()
163 static int slave_next_packet_size(struct snd_usb_endpoint *ep, in slave_next_packet_size() argument
170 if (ep->fill_max) in slave_next_packet_size()
171 return ep->maxframesize; in slave_next_packet_size()
173 spin_lock_irqsave(&ep->lock, flags); in slave_next_packet_size()
174 phase = (ep->phase & 0xffff) + (ep->freqm << ep->datainterval); in slave_next_packet_size()
175 ret = min(phase >> 16, ep->maxframesize); in slave_next_packet_size()
179 ep->phase = phase; in slave_next_packet_size()
180 spin_unlock_irqrestore(&ep->lock, flags); in slave_next_packet_size()
189 static int next_packet_size(struct snd_usb_endpoint *ep, unsigned int avail) in next_packet_size() argument
194 if (ep->fill_max) in next_packet_size()
195 return ep->maxframesize; in next_packet_size()
197 sample_accum = ep->sample_accum + ep->sample_rem; in next_packet_size()
198 if (sample_accum >= ep->pps) { in next_packet_size()
199 sample_accum -= ep->pps; in next_packet_size()
200 ret = ep->packsize[1]; in next_packet_size()
202 ret = ep->packsize[0]; in next_packet_size()
207 ep->sample_accum = sample_accum; in next_packet_size()
219 int snd_usb_endpoint_next_packet_size(struct snd_usb_endpoint *ep, in snd_usb_endpoint_next_packet_size() argument
232 if (ep->sync_source) in snd_usb_endpoint_next_packet_size()
233 return slave_next_packet_size(ep, avail); in snd_usb_endpoint_next_packet_size()
235 return next_packet_size(ep, avail); in snd_usb_endpoint_next_packet_size()
238 static void call_retire_callback(struct snd_usb_endpoint *ep, in call_retire_callback() argument
243 data_subs = READ_ONCE(ep->data_subs); in call_retire_callback()
244 if (data_subs && ep->retire_data_urb) in call_retire_callback()
245 ep->retire_data_urb(data_subs, urb); in call_retire_callback()
248 static void retire_outbound_urb(struct snd_usb_endpoint *ep, in retire_outbound_urb() argument
251 call_retire_callback(ep, urb_ctx->urb); in retire_outbound_urb()
254 static void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
258 static void retire_inbound_urb(struct snd_usb_endpoint *ep, in retire_inbound_urb() argument
264 if (unlikely(ep->skip_packets > 0)) { in retire_inbound_urb()
265 ep->skip_packets--; in retire_inbound_urb()
269 sync_sink = READ_ONCE(ep->sync_sink); in retire_inbound_urb()
271 snd_usb_handle_sync_urb(sync_sink, ep, urb); in retire_inbound_urb()
273 call_retire_callback(ep, urb); in retire_inbound_urb()
281 static void prepare_silent_urb(struct snd_usb_endpoint *ep, in prepare_silent_urb() argument
291 if (has_tx_length_quirk(ep->chip)) in prepare_silent_urb()
299 counts = snd_usb_endpoint_next_packet_size(ep, ctx, i, 0); in prepare_silent_urb()
300 length = counts * ep->stride; /* number of silent bytes */ in prepare_silent_urb()
301 offset = offs * ep->stride + extra * i; in prepare_silent_urb()
310 ep->silence_value, length); in prepare_silent_urb()
315 urb->transfer_buffer_length = offs * ep->stride + ctx->packets * extra; in prepare_silent_urb()
322 static int prepare_outbound_urb(struct snd_usb_endpoint *ep, in prepare_outbound_urb() argument
330 urb->dev = ep->chip->dev; /* we need to set this at each time */ in prepare_outbound_urb()
332 switch (ep->type) { in prepare_outbound_urb()
334 data_subs = READ_ONCE(ep->data_subs); in prepare_outbound_urb()
335 if (data_subs && ep->prepare_data_urb) in prepare_outbound_urb()
336 return ep->prepare_data_urb(data_subs, urb, in_stream_lock); in prepare_outbound_urb()
338 prepare_silent_urb(ep, ctx); in prepare_outbound_urb()
342 if (snd_usb_get_speed(ep->chip->dev) >= USB_SPEED_HIGH) { in prepare_outbound_urb()
349 cp[0] = ep->freqn; in prepare_outbound_urb()
350 cp[1] = ep->freqn >> 8; in prepare_outbound_urb()
351 cp[2] = ep->freqn >> 16; in prepare_outbound_urb()
352 cp[3] = ep->freqn >> 24; in prepare_outbound_urb()
360 cp[0] = ep->freqn >> 2; in prepare_outbound_urb()
361 cp[1] = ep->freqn >> 10; in prepare_outbound_urb()
362 cp[2] = ep->freqn >> 18; in prepare_outbound_urb()
373 static int prepare_inbound_urb(struct snd_usb_endpoint *ep, in prepare_inbound_urb() argument
379 urb->dev = ep->chip->dev; /* we need to set this at each time */ in prepare_inbound_urb()
381 switch (ep->type) { in prepare_inbound_urb()
386 urb->iso_frame_desc[i].length = ep->curpacksize; in prepare_inbound_urb()
387 offs += ep->curpacksize; in prepare_inbound_urb()
395 urb->iso_frame_desc[0].length = min(4u, ep->syncmaxsize); in prepare_inbound_urb()
403 static void notify_xrun(struct snd_usb_endpoint *ep) in notify_xrun() argument
408 data_subs = READ_ONCE(ep->data_subs); in notify_xrun()
418 next_packet_fifo_enqueue(struct snd_usb_endpoint *ep) in next_packet_fifo_enqueue() argument
422 p = ep->next_packet + (ep->next_packet_head + ep->next_packet_queued) % in next_packet_fifo_enqueue()
423 ARRAY_SIZE(ep->next_packet); in next_packet_fifo_enqueue()
424 ep->next_packet_queued++; in next_packet_fifo_enqueue()
429 next_packet_fifo_dequeue(struct snd_usb_endpoint *ep) in next_packet_fifo_dequeue() argument
433 p = ep->next_packet + ep->next_packet_head; in next_packet_fifo_dequeue()
434 ep->next_packet_head++; in next_packet_fifo_dequeue()
435 ep->next_packet_head %= ARRAY_SIZE(ep->next_packet); in next_packet_fifo_dequeue()
436 ep->next_packet_queued--; in next_packet_fifo_dequeue()
440 static void push_back_to_ready_list(struct snd_usb_endpoint *ep, in push_back_to_ready_list() argument
445 spin_lock_irqsave(&ep->lock, flags); in push_back_to_ready_list()
446 list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs); in push_back_to_ready_list()
447 spin_unlock_irqrestore(&ep->lock, flags); in push_back_to_ready_list()
452 * from ep->ready_playback_urbs and in case there aren't any available
463 int snd_usb_queue_pending_output_urbs(struct snd_usb_endpoint *ep, in snd_usb_queue_pending_output_urbs() argument
466 bool implicit_fb = snd_usb_endpoint_implicit_feedback_sink(ep); in snd_usb_queue_pending_output_urbs()
468 while (ep_state_running(ep)) { in snd_usb_queue_pending_output_urbs()
475 spin_lock_irqsave(&ep->lock, flags); in snd_usb_queue_pending_output_urbs()
476 if ((!implicit_fb || ep->next_packet_queued > 0) && in snd_usb_queue_pending_output_urbs()
477 !list_empty(&ep->ready_playback_urbs)) { in snd_usb_queue_pending_output_urbs()
479 ctx = list_first_entry(&ep->ready_playback_urbs, in snd_usb_queue_pending_output_urbs()
483 packet = next_packet_fifo_dequeue(ep); in snd_usb_queue_pending_output_urbs()
485 spin_unlock_irqrestore(&ep->lock, flags); in snd_usb_queue_pending_output_urbs()
497 err = prepare_outbound_urb(ep, ctx, in_stream_lock); in snd_usb_queue_pending_output_urbs()
499 if (unlikely(!ep_state_running(ep))) in snd_usb_queue_pending_output_urbs()
504 push_back_to_ready_list(ep, ctx); in snd_usb_queue_pending_output_urbs()
509 notify_xrun(ep); in snd_usb_queue_pending_output_urbs()
513 if (!atomic_read(&ep->chip->shutdown)) in snd_usb_queue_pending_output_urbs()
518 if (!atomic_read(&ep->chip->shutdown)) { in snd_usb_queue_pending_output_urbs()
519 usb_audio_err(ep->chip, in snd_usb_queue_pending_output_urbs()
523 notify_xrun(ep); in snd_usb_queue_pending_output_urbs()
528 set_bit(ctx->index, &ep->active_mask); in snd_usb_queue_pending_output_urbs()
529 atomic_inc(&ep->submitted_urbs); in snd_usb_queue_pending_output_urbs()
541 struct snd_usb_endpoint *ep = ctx->ep; in snd_complete_urb() local
550 if (unlikely(atomic_read(&ep->chip->shutdown))) in snd_complete_urb()
553 if (unlikely(!ep_state_running(ep))) in snd_complete_urb()
556 if (usb_pipeout(ep->pipe)) { in snd_complete_urb()
557 retire_outbound_urb(ep, ctx); in snd_complete_urb()
559 if (unlikely(!ep_state_running(ep))) in snd_complete_urb()
565 if (ep->lowlatency_playback || in snd_complete_urb()
566 snd_usb_endpoint_implicit_feedback_sink(ep)) { in snd_complete_urb()
567 push_back_to_ready_list(ep, ctx); in snd_complete_urb()
568 clear_bit(ctx->index, &ep->active_mask); in snd_complete_urb()
569 snd_usb_queue_pending_output_urbs(ep, false); in snd_complete_urb()
571 if (atomic_dec_and_test(&ep->submitted_urbs) && in snd_complete_urb()
572 !snd_usb_endpoint_implicit_feedback_sink(ep)) in snd_complete_urb()
573 notify_xrun(ep); in snd_complete_urb()
578 prepare_outbound_urb(ep, ctx, false); in snd_complete_urb()
580 if (unlikely(!ep_state_running(ep))) in snd_complete_urb()
583 retire_inbound_urb(ep, ctx); in snd_complete_urb()
585 if (unlikely(!ep_state_running(ep))) in snd_complete_urb()
588 prepare_inbound_urb(ep, ctx); in snd_complete_urb()
591 if (!atomic_read(&ep->chip->shutdown)) in snd_complete_urb()
598 if (!atomic_read(&ep->chip->shutdown)) { in snd_complete_urb()
599 usb_audio_err(ep->chip, "cannot submit urb (err = %d)\n", err); in snd_complete_urb()
600 notify_xrun(ep); in snd_complete_urb()
604 clear_bit(ctx->index, &ep->active_mask); in snd_complete_urb()
605 atomic_dec(&ep->submitted_urbs); in snd_complete_urb()
650 * Get the existing endpoint object corresponding EP
656 struct snd_usb_endpoint *ep; in snd_usb_get_endpoint() local
658 list_for_each_entry(ep, &chip->ep_list, list) { in snd_usb_get_endpoint()
659 if (ep->ep_num == ep_num) in snd_usb_get_endpoint()
660 return ep; in snd_usb_get_endpoint()
689 struct snd_usb_endpoint *ep; in snd_usb_add_endpoint() local
692 ep = snd_usb_get_endpoint(chip, ep_num); in snd_usb_add_endpoint()
693 if (ep) in snd_usb_add_endpoint()
699 ep = kzalloc(sizeof(*ep), GFP_KERNEL); in snd_usb_add_endpoint()
700 if (!ep) in snd_usb_add_endpoint()
703 ep->chip = chip; in snd_usb_add_endpoint()
704 spin_lock_init(&ep->lock); in snd_usb_add_endpoint()
705 ep->type = type; in snd_usb_add_endpoint()
706 ep->ep_num = ep_num; in snd_usb_add_endpoint()
707 INIT_LIST_HEAD(&ep->ready_playback_urbs); in snd_usb_add_endpoint()
708 atomic_set(&ep->submitted_urbs, 0); in snd_usb_add_endpoint()
713 ep->pipe = usb_sndisocpipe(chip->dev, ep_num); in snd_usb_add_endpoint()
715 ep->pipe = usb_rcvisocpipe(chip->dev, ep_num); in snd_usb_add_endpoint()
717 list_add_tail(&ep->list, &chip->ep_list); in snd_usb_add_endpoint()
721 /* Set up syncinterval and maxsyncsize for a sync EP */
723 struct snd_usb_endpoint *ep) in endpoint_set_syncinterval() argument
728 alts = snd_usb_get_host_interface(chip, ep->iface, ep->altsetting); in endpoint_set_syncinterval()
732 desc = get_endpoint(alts, ep->ep_idx); in endpoint_set_syncinterval()
735 ep->syncinterval = desc->bRefresh; in endpoint_set_syncinterval()
737 ep->syncinterval = 1; in endpoint_set_syncinterval()
739 ep->syncinterval = desc->bInterval - 1; in endpoint_set_syncinterval()
741 ep->syncinterval = 3; in endpoint_set_syncinterval()
743 ep->syncmaxsize = le16_to_cpu(desc->wMaxPacketSize); in endpoint_set_syncinterval()
746 static bool endpoint_compatible(struct snd_usb_endpoint *ep, in endpoint_compatible() argument
750 if (!ep->opened) in endpoint_compatible()
752 if (ep->cur_audiofmt != fp) in endpoint_compatible()
754 if (ep->cur_rate != params_rate(params) || in endpoint_compatible()
755 ep->cur_format != params_format(params) || in endpoint_compatible()
756 ep->cur_period_frames != params_period_size(params) || in endpoint_compatible()
757 ep->cur_buffer_periods != params_periods(params)) in endpoint_compatible()
764 * setup of the target EP for implicit feedback sync
767 struct snd_usb_endpoint *ep, in snd_usb_endpoint_compatible() argument
774 ret = endpoint_compatible(ep, fp, params); in snd_usb_endpoint_compatible()
799 struct snd_usb_endpoint *ep; in snd_usb_endpoint_open() local
803 ep = snd_usb_get_endpoint(chip, ep_num); in snd_usb_endpoint_open()
804 if (!ep) { in snd_usb_endpoint_open()
805 usb_audio_err(chip, "Cannot find EP 0x%x to open\n", ep_num); in snd_usb_endpoint_open()
809 if (!ep->opened) { in snd_usb_endpoint_open()
811 ep->iface = fp->sync_iface; in snd_usb_endpoint_open()
812 ep->altsetting = fp->sync_altsetting; in snd_usb_endpoint_open()
813 ep->ep_idx = fp->sync_ep_idx; in snd_usb_endpoint_open()
815 ep->iface = fp->iface; in snd_usb_endpoint_open()
816 ep->altsetting = fp->altsetting; in snd_usb_endpoint_open()
817 ep->ep_idx = fp->ep_idx; in snd_usb_endpoint_open()
819 usb_audio_dbg(chip, "Open EP 0x%x, iface=%d:%d, idx=%d\n", in snd_usb_endpoint_open()
820 ep_num, ep->iface, ep->altsetting, ep->ep_idx); in snd_usb_endpoint_open()
822 ep->iface_ref = iface_ref_find(chip, ep->iface); in snd_usb_endpoint_open()
823 if (!ep->iface_ref) { in snd_usb_endpoint_open()
824 ep = NULL; in snd_usb_endpoint_open()
829 ep->clock_ref = clock_ref_find(chip, fp->clock); in snd_usb_endpoint_open()
830 if (!ep->clock_ref) { in snd_usb_endpoint_open()
831 ep = NULL; in snd_usb_endpoint_open()
834 ep->clock_ref->opened++; in snd_usb_endpoint_open()
837 ep->cur_audiofmt = fp; in snd_usb_endpoint_open()
838 ep->cur_channels = fp->channels; in snd_usb_endpoint_open()
839 ep->cur_rate = params_rate(params); in snd_usb_endpoint_open()
840 ep->cur_format = params_format(params); in snd_usb_endpoint_open()
841 ep->cur_frame_bytes = snd_pcm_format_physical_width(ep->cur_format) * in snd_usb_endpoint_open()
842 ep->cur_channels / 8; in snd_usb_endpoint_open()
843 ep->cur_period_frames = params_period_size(params); in snd_usb_endpoint_open()
844 ep->cur_period_bytes = ep->cur_period_frames * ep->cur_frame_bytes; in snd_usb_endpoint_open()
845 ep->cur_buffer_periods = params_periods(params); in snd_usb_endpoint_open()
847 if (ep->type == SND_USB_ENDPOINT_TYPE_SYNC) in snd_usb_endpoint_open()
848 endpoint_set_syncinterval(chip, ep); in snd_usb_endpoint_open()
850 ep->implicit_fb_sync = fp->implicit_fb; in snd_usb_endpoint_open()
851 ep->need_setup = true; in snd_usb_endpoint_open()
852 ep->need_prepare = true; in snd_usb_endpoint_open()
853 ep->fixed_rate = fixed_rate; in snd_usb_endpoint_open()
856 ep->cur_channels, ep->cur_rate, in snd_usb_endpoint_open()
857 snd_pcm_format_name(ep->cur_format), in snd_usb_endpoint_open()
858 ep->cur_period_bytes, ep->cur_buffer_periods, in snd_usb_endpoint_open()
859 ep->implicit_fb_sync); in snd_usb_endpoint_open()
862 if (WARN_ON(!ep->iface_ref)) { in snd_usb_endpoint_open()
863 ep = NULL; in snd_usb_endpoint_open()
867 if (!endpoint_compatible(ep, fp, params)) { in snd_usb_endpoint_open()
868 usb_audio_err(chip, "Incompatible EP setup for 0x%x\n", in snd_usb_endpoint_open()
870 ep = NULL; in snd_usb_endpoint_open()
874 usb_audio_dbg(chip, "Reopened EP 0x%x (count %d)\n", in snd_usb_endpoint_open()
875 ep_num, ep->opened); in snd_usb_endpoint_open()
878 if (!ep->iface_ref->opened++) in snd_usb_endpoint_open()
879 ep->iface_ref->need_setup = true; in snd_usb_endpoint_open()
881 ep->opened++; in snd_usb_endpoint_open()
885 return ep; in snd_usb_endpoint_open()
906 void snd_usb_endpoint_set_callback(struct snd_usb_endpoint *ep, in snd_usb_endpoint_set_callback() argument
914 ep->prepare_data_urb = prepare; in snd_usb_endpoint_set_callback()
915 ep->retire_data_urb = retire; in snd_usb_endpoint_set_callback()
917 ep->lowlatency_playback = data_subs->lowlatency_playback; in snd_usb_endpoint_set_callback()
919 ep->lowlatency_playback = false; in snd_usb_endpoint_set_callback()
920 WRITE_ONCE(ep->data_subs, data_subs); in snd_usb_endpoint_set_callback()
924 struct snd_usb_endpoint *ep, in endpoint_set_interface() argument
927 int altset = set ? ep->altsetting : 0; in endpoint_set_interface()
930 if (ep->iface_ref->altset == altset) in endpoint_set_interface()
936 usb_audio_dbg(chip, "Setting usb interface %d:%d for EP 0x%x\n", in endpoint_set_interface()
937 ep->iface, altset, ep->ep_num); in endpoint_set_interface()
938 err = usb_set_interface(chip->dev, ep->iface, altset); in endpoint_set_interface()
942 ep->iface, altset, err); in endpoint_set_interface()
948 ep->iface_ref->altset = altset; in endpoint_set_interface()
958 struct snd_usb_endpoint *ep) in snd_usb_endpoint_close() argument
961 usb_audio_dbg(chip, "Closing EP 0x%x (count %d)\n", in snd_usb_endpoint_close()
962 ep->ep_num, ep->opened); in snd_usb_endpoint_close()
964 if (!--ep->iface_ref->opened && in snd_usb_endpoint_close()
966 endpoint_set_interface(chip, ep, false); in snd_usb_endpoint_close()
968 if (!--ep->opened) { in snd_usb_endpoint_close()
969 if (ep->clock_ref) { in snd_usb_endpoint_close()
970 if (!--ep->clock_ref->opened) in snd_usb_endpoint_close()
971 ep->clock_ref->rate = 0; in snd_usb_endpoint_close()
973 ep->iface = 0; in snd_usb_endpoint_close()
974 ep->altsetting = 0; in snd_usb_endpoint_close()
975 ep->cur_audiofmt = NULL; in snd_usb_endpoint_close()
976 ep->cur_rate = 0; in snd_usb_endpoint_close()
977 ep->iface_ref = NULL; in snd_usb_endpoint_close()
978 ep->clock_ref = NULL; in snd_usb_endpoint_close()
979 usb_audio_dbg(chip, "EP 0x%x closed\n", ep->ep_num); in snd_usb_endpoint_close()
984 /* Prepare for suspening EP, called from the main suspend handler */
985 void snd_usb_endpoint_suspend(struct snd_usb_endpoint *ep) in snd_usb_endpoint_suspend() argument
987 ep->need_prepare = true; in snd_usb_endpoint_suspend()
988 if (ep->iface_ref) in snd_usb_endpoint_suspend()
989 ep->iface_ref->need_setup = true; in snd_usb_endpoint_suspend()
990 if (ep->clock_ref) in snd_usb_endpoint_suspend()
991 ep->clock_ref->rate = 0; in snd_usb_endpoint_suspend()
997 static int wait_clear_urbs(struct snd_usb_endpoint *ep) in wait_clear_urbs() argument
1002 if (atomic_read(&ep->state) != EP_STATE_STOPPING) in wait_clear_urbs()
1006 alive = atomic_read(&ep->submitted_urbs); in wait_clear_urbs()
1014 usb_audio_err(ep->chip, in wait_clear_urbs()
1015 "timeout: still %d active urbs on EP #%x\n", in wait_clear_urbs()
1016 alive, ep->ep_num); in wait_clear_urbs()
1018 if (ep_state_update(ep, EP_STATE_STOPPING, EP_STATE_STOPPED)) { in wait_clear_urbs()
1019 ep->sync_sink = NULL; in wait_clear_urbs()
1020 snd_usb_endpoint_set_callback(ep, NULL, NULL, NULL); in wait_clear_urbs()
1029 void snd_usb_endpoint_sync_pending_stop(struct snd_usb_endpoint *ep) in snd_usb_endpoint_sync_pending_stop() argument
1031 if (ep) in snd_usb_endpoint_sync_pending_stop()
1032 wait_clear_urbs(ep); in snd_usb_endpoint_sync_pending_stop()
1038 * This function moves the EP to STOPPING state if it's being RUNNING.
1040 static int stop_urbs(struct snd_usb_endpoint *ep, bool force, bool keep_pending) in stop_urbs() argument
1045 if (!force && atomic_read(&ep->running)) in stop_urbs()
1048 if (!ep_state_update(ep, EP_STATE_RUNNING, EP_STATE_STOPPING)) in stop_urbs()
1051 spin_lock_irqsave(&ep->lock, flags); in stop_urbs()
1052 INIT_LIST_HEAD(&ep->ready_playback_urbs); in stop_urbs()
1053 ep->next_packet_head = 0; in stop_urbs()
1054 ep->next_packet_queued = 0; in stop_urbs()
1055 spin_unlock_irqrestore(&ep->lock, flags); in stop_urbs()
1060 for (i = 0; i < ep->nurbs; i++) { in stop_urbs()
1061 if (test_bit(i, &ep->active_mask)) { in stop_urbs()
1062 if (!test_and_set_bit(i, &ep->unlink_mask)) { in stop_urbs()
1063 struct urb *u = ep->urb[i].urb; in stop_urbs()
1075 static int release_urbs(struct snd_usb_endpoint *ep, bool force) in release_urbs() argument
1080 snd_usb_endpoint_set_callback(ep, NULL, NULL, NULL); in release_urbs()
1083 err = stop_urbs(ep, force, false); in release_urbs()
1087 wait_clear_urbs(ep); in release_urbs()
1089 for (i = 0; i < ep->nurbs; i++) in release_urbs()
1090 release_urb_ctx(&ep->urb[i]); in release_urbs()
1092 usb_free_coherent(ep->chip->dev, SYNC_URBS * 4, in release_urbs()
1093 ep->syncbuf, ep->sync_dma); in release_urbs()
1095 ep->syncbuf = NULL; in release_urbs()
1096 ep->nurbs = 0; in release_urbs()
1103 static int data_ep_set_params(struct snd_usb_endpoint *ep) in data_ep_set_params() argument
1105 struct snd_usb_audio *chip = ep->chip; in data_ep_set_params()
1109 const struct audioformat *fmt = ep->cur_audiofmt; in data_ep_set_params()
1110 int frame_bits = ep->cur_frame_bytes * 8; in data_ep_set_params()
1112 usb_pipeout(ep->pipe)); in data_ep_set_params()
1114 usb_audio_dbg(chip, "Setting params for data EP 0x%x, pipe 0x%x\n", in data_ep_set_params()
1115 ep->ep_num, ep->pipe); in data_ep_set_params()
1117 if (ep->cur_format == SNDRV_PCM_FORMAT_DSD_U16_LE && fmt->dsd_dop) { in data_ep_set_params()
1123 frame_bits += ep->cur_channels << 3; in data_ep_set_params()
1126 ep->datainterval = fmt->datainterval; in data_ep_set_params()
1127 ep->stride = frame_bits >> 3; in data_ep_set_params()
1129 switch (ep->cur_format) { in data_ep_set_params()
1131 ep->silence_value = 0x80; in data_ep_set_params()
1138 ep->silence_value = 0x69; in data_ep_set_params()
1141 ep->silence_value = 0; in data_ep_set_params()
1145 ep->freqmax = ep->freqn + (ep->freqn >> 1); in data_ep_set_params()
1151 * the data interval is more than 1 (i.e. ep->datainterval > 0), in data_ep_set_params()
1156 * (ep->freqmax << ep->datainterval overflows at 8.192 MHz for the in data_ep_set_params()
1158 * USB high speed, noting that ep->freqmax is in units of in data_ep_set_params()
1161 maxsize = (((ep->freqmax << ep->datainterval) + 0xffff) >> 16) * in data_ep_set_params()
1166 if (ep->maxpacksize && ep->maxpacksize < maxsize) { in data_ep_set_params()
1168 unsigned int data_maxsize = maxsize = ep->maxpacksize; in data_ep_set_params()
1173 ep->freqmax = (data_maxsize / (frame_bits >> 3)) in data_ep_set_params()
1174 << (16 - ep->datainterval); in data_ep_set_params()
1177 if (ep->fill_max) in data_ep_set_params()
1178 ep->curpacksize = ep->maxpacksize; in data_ep_set_params()
1180 ep->curpacksize = maxsize; in data_ep_set_params()
1183 packs_per_ms = 8 >> ep->datainterval; in data_ep_set_params()
1189 if (ep->sync_source && !ep->implicit_fb_sync) in data_ep_set_params()
1191 1U << ep->sync_source->syncinterval); in data_ep_set_params()
1192 max_packs_per_urb = max(1u, max_packs_per_urb >> ep->datainterval); in data_ep_set_params()
1202 if (usb_pipein(ep->pipe) || ep->implicit_fb_sync) { in data_ep_set_params()
1206 while (urb_packs > 1 && urb_packs * maxsize >= ep->cur_period_bytes) in data_ep_set_params()
1208 ep->nurbs = MAX_URBS; in data_ep_set_params()
1218 minsize = (ep->freqn >> (16 - ep->datainterval)) * in data_ep_set_params()
1221 if (ep->sync_source) in data_ep_set_params()
1226 max_packs_per_period = DIV_ROUND_UP(ep->cur_period_bytes, minsize); in data_ep_set_params()
1235 ep->max_urb_frames = DIV_ROUND_UP(ep->cur_period_frames, in data_ep_set_params()
1241 ep->nurbs = min(max_urbs, urbs_per_period * ep->cur_buffer_periods); in data_ep_set_params()
1245 for (i = 0; i < ep->nurbs; i++) { in data_ep_set_params()
1246 struct snd_urb_ctx *u = &ep->urb[i]; in data_ep_set_params()
1248 u->ep = ep; in data_ep_set_params()
1263 u->urb->pipe = ep->pipe; in data_ep_set_params()
1265 u->urb->interval = 1 << ep->datainterval; in data_ep_set_params()
1274 release_urbs(ep, false); in data_ep_set_params()
1281 static int sync_ep_set_params(struct snd_usb_endpoint *ep) in sync_ep_set_params() argument
1283 struct snd_usb_audio *chip = ep->chip; in sync_ep_set_params()
1286 usb_audio_dbg(chip, "Setting params for sync EP 0x%x, pipe 0x%x\n", in sync_ep_set_params()
1287 ep->ep_num, ep->pipe); in sync_ep_set_params()
1289 ep->syncbuf = usb_alloc_coherent(chip->dev, SYNC_URBS * 4, in sync_ep_set_params()
1290 GFP_KERNEL, &ep->sync_dma); in sync_ep_set_params()
1291 if (!ep->syncbuf) in sync_ep_set_params()
1294 ep->nurbs = SYNC_URBS; in sync_ep_set_params()
1296 struct snd_urb_ctx *u = &ep->urb[i]; in sync_ep_set_params()
1298 u->ep = ep; in sync_ep_set_params()
1303 u->urb->transfer_buffer = ep->syncbuf + i * 4; in sync_ep_set_params()
1304 u->urb->transfer_dma = ep->sync_dma + i * 4; in sync_ep_set_params()
1306 u->urb->pipe = ep->pipe; in sync_ep_set_params()
1309 u->urb->interval = 1 << ep->syncinterval; in sync_ep_set_params()
1317 release_urbs(ep, false); in sync_ep_set_params()
1323 struct snd_usb_endpoint *ep) in update_clock_ref_rate() argument
1325 struct snd_usb_clock_ref *clock = ep->clock_ref; in update_clock_ref_rate()
1326 int rate = ep->cur_rate; in update_clock_ref_rate()
1334 usb_audio_err(chip, "Mismatched sample rate %d vs %d for EP 0x%x\n", in update_clock_ref_rate()
1335 clock->rate, rate, ep->ep_num); in update_clock_ref_rate()
1353 struct snd_usb_endpoint *ep) in snd_usb_endpoint_set_params() argument
1355 const struct audioformat *fmt = ep->cur_audiofmt; in snd_usb_endpoint_set_params()
1359 if (!ep->need_setup) in snd_usb_endpoint_set_params()
1363 err = release_urbs(ep, false); in snd_usb_endpoint_set_params()
1367 ep->datainterval = fmt->datainterval; in snd_usb_endpoint_set_params()
1368 ep->maxpacksize = fmt->maxpacksize; in snd_usb_endpoint_set_params()
1369 ep->fill_max = !!(fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX); in snd_usb_endpoint_set_params()
1372 ep->freqn = get_usb_full_speed_rate(ep->cur_rate); in snd_usb_endpoint_set_params()
1373 ep->pps = 1000 >> ep->datainterval; in snd_usb_endpoint_set_params()
1375 ep->freqn = get_usb_high_speed_rate(ep->cur_rate); in snd_usb_endpoint_set_params()
1376 ep->pps = 8000 >> ep->datainterval; in snd_usb_endpoint_set_params()
1379 ep->sample_rem = ep->cur_rate % ep->pps; in snd_usb_endpoint_set_params()
1380 ep->packsize[0] = ep->cur_rate / ep->pps; in snd_usb_endpoint_set_params()
1381 ep->packsize[1] = (ep->cur_rate + (ep->pps - 1)) / ep->pps; in snd_usb_endpoint_set_params()
1384 ep->freqm = ep->freqn; in snd_usb_endpoint_set_params()
1385 ep->freqshift = INT_MIN; in snd_usb_endpoint_set_params()
1387 ep->phase = 0; in snd_usb_endpoint_set_params()
1389 switch (ep->type) { in snd_usb_endpoint_set_params()
1391 err = data_ep_set_params(ep); in snd_usb_endpoint_set_params()
1394 err = sync_ep_set_params(ep); in snd_usb_endpoint_set_params()
1400 usb_audio_dbg(chip, "Set up %d URBS, ret=%d\n", ep->nurbs, err); in snd_usb_endpoint_set_params()
1406 ep->maxframesize = ep->maxpacksize / ep->cur_frame_bytes; in snd_usb_endpoint_set_params()
1407 ep->curframesize = ep->curpacksize / ep->cur_frame_bytes; in snd_usb_endpoint_set_params()
1409 err = update_clock_ref_rate(chip, ep); in snd_usb_endpoint_set_params()
1411 ep->need_setup = false; in snd_usb_endpoint_set_params()
1421 struct snd_usb_endpoint *ep) in init_sample_rate() argument
1423 struct snd_usb_clock_ref *clock = ep->clock_ref; in init_sample_rate()
1426 rate = update_clock_ref_rate(chip, ep); in init_sample_rate()
1432 if (!ep->fixed_rate) { in init_sample_rate()
1433 err = snd_usb_init_sample_rate(chip, ep->cur_audiofmt, rate); in init_sample_rate()
1449 * This function sets up the EP to be fully usable state.
1458 struct snd_usb_endpoint *ep) in snd_usb_endpoint_prepare() argument
1464 if (WARN_ON(!ep->iface_ref)) in snd_usb_endpoint_prepare()
1466 if (!ep->need_prepare) in snd_usb_endpoint_prepare()
1469 /* If the interface has been already set up, just set EP parameters */ in snd_usb_endpoint_prepare()
1470 if (!ep->iface_ref->need_setup) { in snd_usb_endpoint_prepare()
1472 * to update at each EP configuration in snd_usb_endpoint_prepare()
1474 if (ep->cur_audiofmt->protocol == UAC_VERSION_1) { in snd_usb_endpoint_prepare()
1475 err = init_sample_rate(chip, ep); in snd_usb_endpoint_prepare()
1483 endpoint_set_interface(chip, ep, false); in snd_usb_endpoint_prepare()
1488 iface_first = ep->cur_audiofmt->protocol == UAC_VERSION_1; in snd_usb_endpoint_prepare()
1493 err = endpoint_set_interface(chip, ep, true); in snd_usb_endpoint_prepare()
1498 err = snd_usb_init_pitch(chip, ep->cur_audiofmt); in snd_usb_endpoint_prepare()
1502 err = init_sample_rate(chip, ep); in snd_usb_endpoint_prepare()
1506 err = snd_usb_select_mode_quirk(chip, ep->cur_audiofmt); in snd_usb_endpoint_prepare()
1512 err = endpoint_set_interface(chip, ep, true); in snd_usb_endpoint_prepare()
1517 ep->iface_ref->need_setup = false; in snd_usb_endpoint_prepare()
1520 ep->need_prepare = false; in snd_usb_endpoint_prepare()
1550 * @ep: the endpoint to start
1560 int snd_usb_endpoint_start(struct snd_usb_endpoint *ep) in snd_usb_endpoint_start() argument
1562 bool is_playback = usb_pipeout(ep->pipe); in snd_usb_endpoint_start()
1566 if (atomic_read(&ep->chip->shutdown)) in snd_usb_endpoint_start()
1569 if (ep->sync_source) in snd_usb_endpoint_start()
1570 WRITE_ONCE(ep->sync_source->sync_sink, ep); in snd_usb_endpoint_start()
1572 usb_audio_dbg(ep->chip, "Starting %s EP 0x%x (running %d)\n", in snd_usb_endpoint_start()
1573 ep_type_name(ep->type), ep->ep_num, in snd_usb_endpoint_start()
1574 atomic_read(&ep->running)); in snd_usb_endpoint_start()
1577 if (atomic_inc_return(&ep->running) != 1) in snd_usb_endpoint_start()
1580 if (ep->clock_ref) in snd_usb_endpoint_start()
1581 atomic_inc(&ep->clock_ref->locked); in snd_usb_endpoint_start()
1583 ep->active_mask = 0; in snd_usb_endpoint_start()
1584 ep->unlink_mask = 0; in snd_usb_endpoint_start()
1585 ep->phase = 0; in snd_usb_endpoint_start()
1586 ep->sample_accum = 0; in snd_usb_endpoint_start()
1588 snd_usb_endpoint_start_quirk(ep); in snd_usb_endpoint_start()
1597 if (!ep_state_update(ep, EP_STATE_STOPPED, EP_STATE_RUNNING)) in snd_usb_endpoint_start()
1600 if (snd_usb_endpoint_implicit_feedback_sink(ep) && in snd_usb_endpoint_start()
1601 !(ep->chip->quirk_flags & QUIRK_FLAG_PLAYBACK_FIRST)) { in snd_usb_endpoint_start()
1602 usb_audio_dbg(ep->chip, "No URB submission due to implicit fb sync\n"); in snd_usb_endpoint_start()
1607 for (i = 0; i < ep->nurbs; i++) { in snd_usb_endpoint_start()
1608 struct urb *urb = ep->urb[i].urb; in snd_usb_endpoint_start()
1614 err = prepare_outbound_urb(ep, urb->context, true); in snd_usb_endpoint_start()
1616 err = prepare_inbound_urb(ep, urb->context); in snd_usb_endpoint_start()
1621 usb_audio_dbg(ep->chip, in snd_usb_endpoint_start()
1622 "EP 0x%x: failed to prepare urb: %d\n", in snd_usb_endpoint_start()
1623 ep->ep_num, err); in snd_usb_endpoint_start()
1627 if (!atomic_read(&ep->chip->shutdown)) in snd_usb_endpoint_start()
1632 if (!atomic_read(&ep->chip->shutdown)) in snd_usb_endpoint_start()
1633 usb_audio_err(ep->chip, in snd_usb_endpoint_start()
1638 set_bit(i, &ep->active_mask); in snd_usb_endpoint_start()
1639 atomic_inc(&ep->submitted_urbs); in snd_usb_endpoint_start()
1643 usb_audio_dbg(ep->chip, "XRUN at starting EP 0x%x\n", in snd_usb_endpoint_start()
1644 ep->ep_num); in snd_usb_endpoint_start()
1648 usb_audio_dbg(ep->chip, "%d URBs submitted for EP 0x%x\n", in snd_usb_endpoint_start()
1649 i, ep->ep_num); in snd_usb_endpoint_start()
1654 for (; i < ep->nurbs; i++) in snd_usb_endpoint_start()
1655 push_back_to_ready_list(ep, ep->urb + i); in snd_usb_endpoint_start()
1661 snd_usb_endpoint_stop(ep, false); in snd_usb_endpoint_start()
1668 * @ep: the endpoint to stop (may be NULL)
1680 void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep, bool keep_pending) in snd_usb_endpoint_stop() argument
1682 if (!ep) in snd_usb_endpoint_stop()
1685 usb_audio_dbg(ep->chip, "Stopping %s EP 0x%x (running %d)\n", in snd_usb_endpoint_stop()
1686 ep_type_name(ep->type), ep->ep_num, in snd_usb_endpoint_stop()
1687 atomic_read(&ep->running)); in snd_usb_endpoint_stop()
1689 if (snd_BUG_ON(!atomic_read(&ep->running))) in snd_usb_endpoint_stop()
1692 if (!atomic_dec_return(&ep->running)) { in snd_usb_endpoint_stop()
1693 if (ep->sync_source) in snd_usb_endpoint_stop()
1694 WRITE_ONCE(ep->sync_source->sync_sink, NULL); in snd_usb_endpoint_stop()
1695 stop_urbs(ep, false, keep_pending); in snd_usb_endpoint_stop()
1696 if (ep->clock_ref) in snd_usb_endpoint_stop()
1697 atomic_dec(&ep->clock_ref->locked); in snd_usb_endpoint_stop()
1699 if (ep->chip->quirk_flags & QUIRK_FLAG_FORCE_IFACE_RESET && in snd_usb_endpoint_stop()
1700 usb_pipeout(ep->pipe)) { in snd_usb_endpoint_stop()
1701 ep->need_prepare = true; in snd_usb_endpoint_stop()
1702 if (ep->iface_ref) in snd_usb_endpoint_stop()
1703 ep->iface_ref->need_setup = true; in snd_usb_endpoint_stop()
1711 * @ep: the endpoint to release
1716 void snd_usb_endpoint_release(struct snd_usb_endpoint *ep) in snd_usb_endpoint_release() argument
1718 release_urbs(ep, true); in snd_usb_endpoint_release()
1729 struct snd_usb_endpoint *ep, *en; in snd_usb_endpoint_free_all() local
1733 list_for_each_entry_safe(ep, en, &chip->ep_list, list) in snd_usb_endpoint_free_all()
1734 kfree(ep); in snd_usb_endpoint_free_all()
1746 * @ep: the endpoint to handle the packet
1753 static void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep, in snd_usb_handle_sync_urb() argument
1761 snd_BUG_ON(ep == sender); in snd_usb_handle_sync_urb()
1769 if (snd_usb_endpoint_implicit_feedback_sink(ep) && in snd_usb_handle_sync_urb()
1770 atomic_read(&ep->running)) { in snd_usb_handle_sync_urb()
1791 spin_lock_irqsave(&ep->lock, flags); in snd_usb_handle_sync_urb()
1792 if (ep->next_packet_queued >= ARRAY_SIZE(ep->next_packet)) { in snd_usb_handle_sync_urb()
1793 spin_unlock_irqrestore(&ep->lock, flags); in snd_usb_handle_sync_urb()
1794 usb_audio_err(ep->chip, in snd_usb_handle_sync_urb()
1795 "next package FIFO overflow EP 0x%x\n", in snd_usb_handle_sync_urb()
1796 ep->ep_num); in snd_usb_handle_sync_urb()
1797 notify_xrun(ep); in snd_usb_handle_sync_urb()
1801 out_packet = next_packet_fifo_enqueue(ep); in snd_usb_handle_sync_urb()
1822 spin_unlock_irqrestore(&ep->lock, flags); in snd_usb_handle_sync_urb()
1823 snd_usb_queue_pending_output_urbs(ep, false); in snd_usb_handle_sync_urb()
1862 if (f < ep->freqn - 0x8000) in snd_usb_handle_sync_urb()
1864 else if (f > ep->freqn + 0x8000) in snd_usb_handle_sync_urb()
1866 } else if (unlikely(ep->freqshift == INT_MIN)) { in snd_usb_handle_sync_urb()
1874 while (f < ep->freqn - ep->freqn / 4) { in snd_usb_handle_sync_urb()
1878 while (f > ep->freqn + ep->freqn / 2) { in snd_usb_handle_sync_urb()
1882 ep->freqshift = shift; in snd_usb_handle_sync_urb()
1883 } else if (ep->freqshift >= 0) in snd_usb_handle_sync_urb()
1884 f <<= ep->freqshift; in snd_usb_handle_sync_urb()
1886 f >>= -ep->freqshift; in snd_usb_handle_sync_urb()
1888 if (likely(f >= ep->freqn - ep->freqn / 8 && f <= ep->freqmax)) { in snd_usb_handle_sync_urb()
1893 spin_lock_irqsave(&ep->lock, flags); in snd_usb_handle_sync_urb()
1894 ep->freqm = f; in snd_usb_handle_sync_urb()
1895 spin_unlock_irqrestore(&ep->lock, flags); in snd_usb_handle_sync_urb()
1901 ep->freqshift = INT_MIN; in snd_usb_handle_sync_urb()