22 #include <linux/usb/audio.h>
23 #include <linux/slab.h>
36 #define EP_FLAG_ACTIVATED 0
37 #define EP_FLAG_RUNNING 1
38 #define EP_FLAG_STOPPING 2
71 static inline unsigned get_usb_full_speed_rate(
unsigned int rate)
73 return ((rate << 13) + 62) / 125;
80 static inline unsigned get_usb_high_speed_rate(
unsigned int rate)
82 return ((rate << 10) + 62) / 125;
92 u->
urb->transfer_buffer,
93 u->
urb->transfer_dma);
98 static const char *usb_error_string(
int err)
104 return "endpoint not enabled";
106 return "endpoint stalled";
108 return "not enough bandwidth";
110 return "device disabled";
112 return "device suspended";
117 return "internal error";
119 return "unknown error";
136 usb_pipeout(ep->
pipe);
158 spin_unlock_irqrestore(&ep->
lock, flags);
194 struct urb *urb = ctx->
urb;
195 unsigned char *
cp = urb->transfer_buffer;
197 urb->dev = ep->
chip->dev;
205 unsigned int offs = 0;
206 for (i = 0; i < ctx->
packets; ++
i) {
214 urb->iso_frame_desc[
i].offset = offs * ep->
stride;
215 urb->iso_frame_desc[
i].length = counts * ep->
stride;
219 urb->number_of_packets = ctx->
packets;
220 urb->transfer_buffer_length = offs * ep->
stride;
232 urb->iso_frame_desc[0].length = 4;
233 urb->iso_frame_desc[0].offset = 0;
235 cp[1] = ep->
freqn >> 8;
236 cp[2] = ep->
freqn >> 16;
237 cp[3] = ep->
freqn >> 24;
243 urb->iso_frame_desc[0].length = 3;
244 urb->iso_frame_desc[0].offset = 0;
245 cp[0] = ep->
freqn >> 2;
246 cp[1] = ep->
freqn >> 10;
247 cp[2] = ep->
freqn >> 18;
261 struct urb *urb = urb_ctx->
urb;
263 urb->dev = ep->
chip->dev;
268 for (i = 0; i < urb_ctx->
packets; i++) {
269 urb->iso_frame_desc[
i].offset =
offs;
274 urb->transfer_buffer_length =
offs;
275 urb->number_of_packets = urb_ctx->
packets;
280 urb->iso_frame_desc[0].offset = 0;
320 spin_unlock_irqrestore(&ep->
lock, flags);
329 for (i = 0; i <
packet->packets; i++)
333 prepare_outbound_urb(ep, ctx);
347 static void snd_complete_urb(
struct urb *urb)
360 if (usb_pipeout(ep->
pipe)) {
361 retire_outbound_urb(ep, ctx);
371 spin_unlock_irqrestore(&ep->
lock, flags);
372 queue_pending_output_urbs(ep);
377 prepare_outbound_urb(ep, ctx);
379 retire_inbound_urb(ep, ctx);
384 prepare_inbound_urb(ep, ctx);
415 struct usb_host_interface *alts,
426 if (ep->
ep_num == ep_num &&
427 ep->
iface == alts->desc.bInterfaceNumber &&
428 ep->
alt_idx == alts->desc.bAlternateSetting) {
436 is_playback ?
"playback" :
"capture",
448 ep->
iface = alts->desc.bInterfaceNumber;
449 ep->
alt_idx = alts->desc.bAlternateSetting;
454 ep->
pipe = usb_sndisocpipe(chip->
dev, ep_num);
456 ep->
pipe = usb_rcvisocpipe(chip->
dev, ep_num);
493 for (i = 0; i < ep->
nurbs; i++)
528 if (!force && ep->
chip->shutdown)
531 async = !can_sleep && ep->
chip->async_unlink;
542 for (i = 0; i < ep->
nurbs; i++) {
545 struct urb *u = ep->
urb[
i].urb;
569 deactivate_urbs(ep, force, 1);
572 for (i = 0; i < ep->
nurbs; i++)
573 release_urb_ctx(&ep->
urb[i]);
589 unsigned int period_bytes,
593 unsigned int maxsize,
i, urb_packs, total_packs, packs_per_ms;
594 int is_playback = usb_pipeout(ep->
pipe);
598 ep->
stride = frame_bits >> 3;
605 ep->
freqmax = (maxsize / (frame_bits >> 3))
610 maxsize = ((ep->
freqmax + 0xffff) * (frame_bits >> 3))
625 urb_packs =
max(ep->
chip->nrpacks, 1);
631 urb_packs *= packs_per_ms;
638 unsigned int minsize, maxpacks;
644 minsize -= minsize >> 3;
645 minsize =
max(minsize, 1u);
646 total_packs = (period_bytes + minsize - 1) / minsize;
648 if (total_packs < 2) {
652 maxpacks =
max(
MAX_QUEUE * packs_per_ms, urb_packs * 2);
653 total_packs =
min(total_packs, maxpacks);
656 while (urb_packs > 1 && urb_packs * maxsize >= period_bytes)
661 ep->
nurbs = (total_packs + urb_packs - 1) / urb_packs;
666 }
else if (ep->
nurbs < 2) {
674 for (i = 0; i < ep->
nurbs; i++) {
679 - i * total_packs / ep->
nurbs;
688 u->
urb->transfer_buffer =
691 if (!u->
urb->transfer_buffer)
694 u->
urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
697 u->
urb->complete = snd_complete_urb;
729 u->
urb->transfer_buffer = ep->
syncbuf + i * 4;
731 u->
urb->transfer_buffer_length = 4;
733 u->
urb->transfer_flags = URB_ISO_ASAP |
734 URB_NO_TRANSFER_DMA_MAP;
735 u->
urb->number_of_packets = 1;
738 u->
urb->complete = snd_complete_urb;
767 unsigned int channels,
768 unsigned int period_bytes,
789 ep->
freqn = get_usb_full_speed_rate(rate);
791 ep->
freqn = get_usb_high_speed_rate(rate);
801 err = data_ep_set_params(ep, pcm_format, channels,
802 period_bytes, fmt, sync_ep);
805 err = sync_ep_set_params(ep, fmt);
837 if (ep->
chip->shutdown)
845 deactivate_urbs(ep, 0, can_sleep);
865 for (i = 0; i < ep->
nurbs; i++) {
873 for (i = 0; i < ep->
nurbs; i++) {
874 struct urb *urb = ep->
urb[
i].urb;
879 if (usb_pipeout(ep->
pipe)) {
880 prepare_outbound_urb(ep, urb->context);
882 prepare_inbound_urb(ep, urb->context);
888 i, err, usb_error_string(err));
899 deactivate_urbs(ep, 0, 0);
915 int force,
int can_sleep,
int wait)
924 deactivate_urbs(ep, force, can_sleep);
955 deactivate_urbs(ep, 1, 1);
995 const struct urb *urb)
1015 struct snd_usb_packet_info *out_packet;
1017 in_ctx = urb->context;
1020 for (i = 0; i < in_ctx->
packets; i++)
1021 if (urb->iso_frame_desc[i].status == 0)
1022 bytes += urb->iso_frame_desc[
i].actual_length;
1041 out_packet->packets = in_ctx->
packets;
1042 for (i = 0; i < in_ctx->
packets; i++) {
1043 if (urb->iso_frame_desc[i].status == 0)
1044 out_packet->packet_size[
i] =
1045 urb->iso_frame_desc[
i].actual_length / ep->
stride;
1047 out_packet->packet_size[
i] = 0;
1052 spin_unlock_irqrestore(&ep->
lock, flags);
1053 queue_pending_output_urbs(ep);
1073 if (urb->iso_frame_desc[0].status != 0 ||
1074 urb->iso_frame_desc[0].actual_length < 3)
1078 if (urb->iso_frame_desc[0].actual_length == 3)
1094 while (f < ep->freqn - ep->
freqn / 4) {
1115 spin_unlock_irqrestore(&ep->
lock, flags);