Lines Matching refs:ep
77 usb_free_coherent(u->ep->chip->dev, u->buffer_size,
113 * @ep: The snd_usb_endpoint
118 int snd_usb_endpoint_implicit_feedback_sink(struct snd_usb_endpoint *ep)
120 return ep->sync_master &&
121 ep->sync_master->type == SND_USB_ENDPOINT_TYPE_DATA &&
122 ep->type == SND_USB_ENDPOINT_TYPE_DATA &&
123 usb_pipeout(ep->pipe);
133 int snd_usb_endpoint_slave_next_packet_size(struct snd_usb_endpoint *ep)
138 if (ep->fill_max)
139 return ep->maxframesize;
141 spin_lock_irqsave(&ep->lock, flags);
142 ep->phase = (ep->phase & 0xffff)
143 + (ep->freqm << ep->datainterval);
144 ret = min(ep->phase >> 16, ep->maxframesize);
145 spin_unlock_irqrestore(&ep->lock, flags);
155 int snd_usb_endpoint_next_packet_size(struct snd_usb_endpoint *ep)
159 if (ep->fill_max)
160 return ep->maxframesize;
162 ep->sample_accum += ep->sample_rem;
163 if (ep->sample_accum >= ep->pps) {
164 ep->sample_accum -= ep->pps;
165 ret = ep->packsize[1];
167 ret = ep->packsize[0];
173 static void retire_outbound_urb(struct snd_usb_endpoint *ep,
176 if (ep->retire_data_urb)
177 ep->retire_data_urb(ep->data_subs, urb_ctx->urb);
180 static void retire_inbound_urb(struct snd_usb_endpoint *ep,
185 if (unlikely(ep->skip_packets > 0)) {
186 ep->skip_packets--;
190 if (ep->sync_slave)
191 snd_usb_handle_sync_urb(ep->sync_slave, ep, urb);
193 if (ep->retire_data_urb)
194 ep->retire_data_urb(ep->data_subs, urb);
197 static void prepare_silent_urb(struct snd_usb_endpoint *ep,
207 if (ep->chip->tx_length_quirk)
217 else if (ep->sync_master)
218 counts = snd_usb_endpoint_slave_next_packet_size(ep);
220 counts = snd_usb_endpoint_next_packet_size(ep);
222 length = counts * ep->stride; /* number of silent bytes */
223 offset = offs * ep->stride + extra * i;
232 ep->silence_value, length);
237 urb->transfer_buffer_length = offs * ep->stride + ctx->packets * extra;
243 static void prepare_outbound_urb(struct snd_usb_endpoint *ep,
249 urb->dev = ep->chip->dev; /* we need to set this at each time */
251 switch (ep->type) {
253 if (ep->prepare_data_urb) {
254 ep->prepare_data_urb(ep->data_subs, urb);
257 prepare_silent_urb(ep, ctx);
262 if (snd_usb_get_speed(ep->chip->dev) >= USB_SPEED_HIGH) {
269 cp[0] = ep->freqn;
270 cp[1] = ep->freqn >> 8;
271 cp[2] = ep->freqn >> 16;
272 cp[3] = ep->freqn >> 24;
280 cp[0] = ep->freqn >> 2;
281 cp[1] = ep->freqn >> 10;
282 cp[2] = ep->freqn >> 18;
292 static inline void prepare_inbound_urb(struct snd_usb_endpoint *ep,
298 urb->dev = ep->chip->dev; /* we need to set this at each time */
300 switch (ep->type) {
305 urb->iso_frame_desc[i].length = ep->curpacksize;
306 offs += ep->curpacksize;
314 urb->iso_frame_desc[0].length = min(4u, ep->syncmaxsize);
322 * from ep->ready_playback_urbs and in case there aren't any available
334 static void queue_pending_output_urbs(struct snd_usb_endpoint *ep)
336 while (test_bit(EP_FLAG_RUNNING, &ep->flags)) {
343 spin_lock_irqsave(&ep->lock, flags);
344 if (ep->next_packet_read_pos != ep->next_packet_write_pos) {
345 packet = ep->next_packet + ep->next_packet_read_pos;
346 ep->next_packet_read_pos++;
347 ep->next_packet_read_pos %= MAX_URBS;
350 if (!list_empty(&ep->ready_playback_urbs)) {
351 ctx = list_first_entry(&ep->ready_playback_urbs,
356 spin_unlock_irqrestore(&ep->lock, flags);
366 prepare_outbound_urb(ep, ctx);
370 usb_audio_err(ep->chip,
374 set_bit(ctx->index, &ep->active_mask);
384 struct snd_usb_endpoint *ep = ctx->ep;
395 if (unlikely(atomic_read(&ep->chip->shutdown)))
398 if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))
401 if (usb_pipeout(ep->pipe)) {
402 retire_outbound_urb(ep, ctx);
404 if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))
407 if (snd_usb_endpoint_implicit_feedback_sink(ep)) {
408 spin_lock_irqsave(&ep->lock, flags);
409 list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs);
410 spin_unlock_irqrestore(&ep->lock, flags);
411 queue_pending_output_urbs(ep);
416 prepare_outbound_urb(ep, ctx);
418 if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))
421 retire_inbound_urb(ep, ctx);
423 if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))
426 prepare_inbound_urb(ep, ctx);
433 usb_audio_err(ep->chip, "cannot submit urb (err = %d)\n", err);
434 if (ep->data_subs && ep->data_subs->pcm_substream) {
435 substream = ep->data_subs->pcm_substream;
440 clear_bit(ctx->index, &ep->active_mask);
466 struct snd_usb_endpoint *ep;
474 list_for_each_entry(ep, &chip->ep_list, list) {
475 if (ep->ep_num == ep_num &&
476 ep->iface == alts->desc.bInterfaceNumber &&
477 ep->altsetting == alts->desc.bAlternateSetting) {
478 usb_audio_dbg(ep->chip,
480 ep_num, ep->iface, ep->altsetting, ep);
490 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
491 if (!ep)
494 ep->chip = chip;
495 spin_lock_init(&ep->lock);
496 ep->type = type;
497 ep->ep_num = ep_num;
498 ep->iface = alts->desc.bInterfaceNumber;
499 ep->altsetting = alts->desc.bAlternateSetting;
500 INIT_LIST_HEAD(&ep->ready_playback_urbs);
504 ep->pipe = usb_sndisocpipe(chip->dev, ep_num);
506 ep->pipe = usb_rcvisocpipe(chip->dev, ep_num);
512 ep->syncinterval = get_endpoint(alts, 1)->bRefresh;
514 ep->syncinterval = 1;
517 ep->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
519 ep->syncinterval = 3;
521 ep->syncmaxsize = le16_to_cpu(get_endpoint(alts, 1)->wMaxPacketSize);
524 list_add_tail(&ep->list, &chip->ep_list);
526 ep->is_implicit_feedback = 0;
531 return ep;
537 static int wait_clear_urbs(struct snd_usb_endpoint *ep)
543 alive = bitmap_weight(&ep->active_mask, ep->nurbs);
551 usb_audio_err(ep->chip,
553 alive, ep->ep_num);
554 clear_bit(EP_FLAG_STOPPING, &ep->flags);
556 ep->data_subs = NULL;
557 ep->sync_slave = NULL;
558 ep->retire_data_urb = NULL;
559 ep->prepare_data_urb = NULL;
567 void snd_usb_endpoint_sync_pending_stop(struct snd_usb_endpoint *ep)
569 if (ep && test_bit(EP_FLAG_STOPPING, &ep->flags))
570 wait_clear_urbs(ep);
576 static int deactivate_urbs(struct snd_usb_endpoint *ep, bool force)
580 clear_bit(EP_FLAG_RUNNING, &ep->flags);
582 INIT_LIST_HEAD(&ep->ready_playback_urbs);
583 ep->next_packet_read_pos = 0;
584 ep->next_packet_write_pos = 0;
586 for (i = 0; i < ep->nurbs; i++) {
587 if (test_bit(i, &ep->active_mask)) {
588 if (!test_and_set_bit(i, &ep->unlink_mask)) {
589 struct urb *u = ep->urb[i].urb;
601 static void release_urbs(struct snd_usb_endpoint *ep, int force)
606 ep->retire_data_urb = NULL;
607 ep->prepare_data_urb = NULL;
610 deactivate_urbs(ep, force);
611 wait_clear_urbs(ep);
613 for (i = 0; i < ep->nurbs; i++)
614 release_urb_ctx(&ep->urb[i]);
616 usb_free_coherent(ep->chip->dev, SYNC_URBS * 4,
617 ep->syncbuf, ep->sync_dma);
619 ep->syncbuf = NULL;
620 ep->nurbs = 0;
626 static bool check_ep_params(struct snd_usb_endpoint *ep,
639 int tx_length_quirk = (ep->chip->tx_length_quirk &&
640 usb_pipeout(ep->pipe));
652 ret = ret && (ep->datainterval == fmt->datainterval);
653 ret = ret && (ep->stride == frame_bits >> 3);
657 ret = ret && (ep->silence_value == 0x80);
664 ret = ret && (ep->silence_value == 0x69);
667 ret = ret && (ep->silence_value == 0);
671 ret = ret && (ep->freqmax == ep->freqn + (ep->freqn >> 1));
677 * the data interval is more than 1 (i.e. ep->datainterval > 0),
682 * (ep->freqmax << ep->datainterval overflows at 8.192 MHz for the
684 * USB high speed, noting that ep->freqmax is in units of
687 maxsize = (((ep->freqmax << ep->datainterval) + 0xffff) >> 16) *
692 if (ep->maxpacksize && ep->maxpacksize < maxsize) {
694 unsigned int data_maxsize = maxsize = ep->maxpacksize;
699 ret = ret && (ep->freqmax == (data_maxsize / (frame_bits >> 3))
700 << (16 - ep->datainterval));
703 if (ep->fill_max)
704 ret = ret && (ep->curpacksize == ep->maxpacksize);
706 ret = ret && (ep->curpacksize == maxsize);
708 if (snd_usb_get_speed(ep->chip->dev) != USB_SPEED_FULL) {
709 packs_per_ms = 8 >> ep->datainterval;
715 if (sync_ep && !snd_usb_endpoint_implicit_feedback_sink(ep))
718 max_packs_per_urb = max(1u, max_packs_per_urb >> ep->datainterval);
728 if (usb_pipein(ep->pipe) ||
729 snd_usb_endpoint_implicit_feedback_sink(ep)) {
738 if (snd_usb_get_speed(ep->chip->dev) == USB_SPEED_WIRELESS) {
739 int interval = ep->datainterval;
750 ret = ret && (ep->nurbs == MAX_URBS);
760 minsize = (ep->freqn >> (16 - ep->datainterval)) *
777 ret = ret && (ep->max_urb_frames ==
783 ret = ret && (ep->nurbs == min(max_urbs,
787 ret = ret && (ep->datainterval == fmt->datainterval);
788 ret = ret && (ep->maxpacksize == fmt->maxpacksize);
790 (ep->fill_max == !!(fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX));
798 static int data_ep_set_params(struct snd_usb_endpoint *ep,
811 int tx_length_quirk = (ep->chip->tx_length_quirk &&
812 usb_pipeout(ep->pipe));
823 ep->datainterval = fmt->datainterval;
824 ep->stride = frame_bits >> 3;
828 ep->silence_value = 0x80;
835 ep->silence_value = 0x69;
838 ep->silence_value = 0;
842 ep->freqmax = ep->freqn + (ep->freqn >> 1);
848 * the data interval is more than 1 (i.e. ep->datainterval > 0),
853 * (ep->freqmax << ep->datainterval overflows at 8.192 MHz for the
855 * USB high speed, noting that ep->freqmax is in units of
858 maxsize = (((ep->freqmax << ep->datainterval) + 0xffff) >> 16) *
863 if (ep->maxpacksize && ep->maxpacksize < maxsize) {
865 unsigned int data_maxsize = maxsize = ep->maxpacksize;
870 ep->freqmax = (data_maxsize / (frame_bits >> 3))
871 << (16 - ep->datainterval);
874 if (ep->fill_max)
875 ep->curpacksize = ep->maxpacksize;
877 ep->curpacksize = maxsize;
879 if (snd_usb_get_speed(ep->chip->dev) != USB_SPEED_FULL) {
880 packs_per_ms = 8 >> ep->datainterval;
886 if (sync_ep && !snd_usb_endpoint_implicit_feedback_sink(ep))
889 max_packs_per_urb = max(1u, max_packs_per_urb >> ep->datainterval);
899 if (usb_pipein(ep->pipe) ||
900 snd_usb_endpoint_implicit_feedback_sink(ep)) {
909 if (snd_usb_get_speed(ep->chip->dev) == USB_SPEED_WIRELESS) {
910 int interval = ep->datainterval;
920 ep->nurbs = MAX_URBS;
930 minsize = (ep->freqn >> (16 - ep->datainterval)) *
947 ep->max_urb_frames = DIV_ROUND_UP(frames_per_period,
953 ep->nurbs = min(max_urbs, urbs_per_period * periods_per_buffer);
957 for (i = 0; i < ep->nurbs; i++) {
958 struct snd_urb_ctx *u = &ep->urb[i];
960 u->ep = ep;
971 usb_alloc_coherent(ep->chip->dev, u->buffer_size,
975 u->urb->pipe = ep->pipe;
977 u->urb->interval = 1 << ep->datainterval;
986 release_urbs(ep, 0);
993 static int sync_ep_set_params(struct snd_usb_endpoint *ep)
997 ep->syncbuf = usb_alloc_coherent(ep->chip->dev, SYNC_URBS * 4,
998 GFP_KERNEL, &ep->sync_dma);
999 if (!ep->syncbuf)
1002 ep->nurbs = SYNC_URBS;
1004 struct snd_urb_ctx *u = &ep->urb[i];
1006 u->ep = ep;
1011 u->urb->transfer_buffer = ep->syncbuf + i * 4;
1012 u->urb->transfer_dma = ep->sync_dma + i * 4;
1014 u->urb->pipe = ep->pipe;
1017 u->urb->interval = 1 << ep->syncinterval;
1025 release_urbs(ep, 0);
1032 * @ep: the snd_usb_endpoint to configure
1046 int snd_usb_endpoint_set_params(struct snd_usb_endpoint *ep,
1058 if (ep->use_count != 0) {
1059 bool check = ep->is_implicit_feedback &&
1060 check_ep_params(ep, pcm_format,
1066 usb_audio_warn(ep->chip,
1067 "Unable to change format on ep #%x: already in use\n",
1068 ep->ep_num);
1072 usb_audio_dbg(ep->chip,
1074 ep->ep_num);
1079 release_urbs(ep, 0);
1081 ep->datainterval = fmt->datainterval;
1082 ep->maxpacksize = fmt->maxpacksize;
1083 ep->fill_max = !!(fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX);
1085 if (snd_usb_get_speed(ep->chip->dev) == USB_SPEED_FULL) {
1086 ep->freqn = get_usb_full_speed_rate(rate);
1087 ep->pps = 1000 >> ep->datainterval;
1089 ep->freqn = get_usb_high_speed_rate(rate);
1090 ep->pps = 8000 >> ep->datainterval;
1093 ep->sample_rem = rate % ep->pps;
1094 ep->packsize[0] = rate / ep->pps;
1095 ep->packsize[1] = (rate + (ep->pps - 1)) / ep->pps;
1098 ep->freqm = ep->freqn;
1099 ep->freqshift = INT_MIN;
1101 ep->phase = 0;
1103 switch (ep->type) {
1105 err = data_ep_set_params(ep, pcm_format, channels,
1110 err = sync_ep_set_params(ep);
1116 usb_audio_dbg(ep->chip,
1117 "Setting params for ep #%x (type %d, %d urbs), ret=%d\n",
1118 ep->ep_num, ep->type, ep->nurbs, err);
1126 * @ep: the endpoint to start
1136 int snd_usb_endpoint_start(struct snd_usb_endpoint *ep)
1141 if (atomic_read(&ep->chip->shutdown))
1145 if (++ep->use_count != 1)
1149 deactivate_urbs(ep, false);
1151 ep->active_mask = 0;
1152 ep->unlink_mask = 0;
1153 ep->phase = 0;
1154 ep->sample_accum = 0;
1156 snd_usb_endpoint_start_quirk(ep);
1165 set_bit(EP_FLAG_RUNNING, &ep->flags);
1167 if (snd_usb_endpoint_implicit_feedback_sink(ep)) {
1168 for (i = 0; i < ep->nurbs; i++) {
1169 struct snd_urb_ctx *ctx = ep->urb + i;
1170 list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs);
1176 for (i = 0; i < ep->nurbs; i++) {
1177 struct urb *urb = ep->urb[i].urb;
1182 if (usb_pipeout(ep->pipe)) {
1183 prepare_outbound_urb(ep, urb->context);
1185 prepare_inbound_urb(ep, urb->context);
1190 usb_audio_err(ep->chip,
1195 set_bit(i, &ep->active_mask);
1201 clear_bit(EP_FLAG_RUNNING, &ep->flags);
1202 ep->use_count--;
1203 deactivate_urbs(ep, false);
1210 * @ep: the endpoint to stop (may be NULL)
1221 void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep)
1223 if (!ep)
1226 if (snd_BUG_ON(ep->use_count == 0))
1229 if (--ep->use_count == 0) {
1230 deactivate_urbs(ep, false);
1231 set_bit(EP_FLAG_STOPPING, &ep->flags);
1238 * @ep: the endpoint to deactivate
1245 void snd_usb_endpoint_deactivate(struct snd_usb_endpoint *ep)
1247 if (!ep)
1250 if (ep->use_count != 0)
1253 deactivate_urbs(ep, true);
1254 wait_clear_urbs(ep);
1260 * @ep: the endpoint to release
1265 void snd_usb_endpoint_release(struct snd_usb_endpoint *ep)
1267 release_urbs(ep, 1);
1273 * @ep: the endpoint to free
1275 * This free all resources of the given ep.
1277 void snd_usb_endpoint_free(struct snd_usb_endpoint *ep)
1279 kfree(ep);
1285 * @ep: the endpoint to handle the packet
1292 void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
1300 snd_BUG_ON(ep == sender);
1308 if (snd_usb_endpoint_implicit_feedback_sink(ep) &&
1309 ep->use_count != 0) {
1330 spin_lock_irqsave(&ep->lock, flags);
1331 out_packet = ep->next_packet + ep->next_packet_write_pos;
1352 ep->next_packet_write_pos++;
1353 ep->next_packet_write_pos %= MAX_URBS;
1354 spin_unlock_irqrestore(&ep->lock, flags);
1355 queue_pending_output_urbs(ep);
1394 if (f < ep->freqn - 0x8000)
1396 else if (f > ep->freqn + 0x8000)
1398 } else if (unlikely(ep->freqshift == INT_MIN)) {
1406 while (f < ep->freqn - ep->freqn / 4) {
1410 while (f > ep->freqn + ep->freqn / 2) {
1414 ep->freqshift = shift;
1415 } else if (ep->freqshift >= 0)
1416 f <<= ep->freqshift;
1418 f >>= -ep->freqshift;
1420 if (likely(f >= ep->freqn - ep->freqn / 8 && f <= ep->freqmax)) {
1425 spin_lock_irqsave(&ep->lock, flags);
1426 ep->freqm = f;
1427 spin_unlock_irqrestore(&ep->lock, flags);
1433 ep->freqshift = INT_MIN;