18 #include <linux/module.h>
19 #include <linux/slab.h>
21 #include <linux/usb/audio.h>
26 #include "../usbaudio.h"
39 #define MIN_QUEUE_LENGTH 12
41 #define MAX_QUEUE_LENGTH 30
47 #define DEFAULT_QUEUE_LENGTH 21
49 #define MAX_PACKET_SIZE 672
50 #define MAX_MEMORY_BUFFERS DIV_ROUND_UP(MAX_QUEUE_LENGTH, \
51 PAGE_SIZE / MAX_PACKET_SIZE)
56 static unsigned int queue_length = 21;
90 struct usb_device *
dev;
136 static unsigned int devices_used;
137 static struct usb_driver ua101_driver;
139 static void abort_alsa_playback(
struct ua101 *
ua);
140 static void abort_alsa_capture(
struct ua101 *
ua);
142 static const char *usb_error_string(
int err)
148 return "endpoint not enabled";
150 return "endpoint stalled";
152 return "not enough bandwidth";
154 return "device disabled";
156 return "device suspended";
161 return "internal error";
163 return "unknown error";
167 static void abort_usb_capture(
struct ua101 *
ua)
175 static void abort_usb_playback(
struct ua101 *ua)
181 static void playback_urb_complete(
struct urb *usb_urb)
183 struct ua101_urb *
urb = (
struct ua101_urb *)usb_urb;
184 struct ua101 *ua = urb->urb.context;
188 urb->urb.status == -
ENODEV ||
191 abort_usb_playback(ua);
192 abort_alsa_playback(ua);
202 ua->
playback.substream->runtime->delay -=
203 urb->urb.iso_frame_desc[0].length /
205 spin_unlock_irqrestore(&ua->
lock, flags);
209 static void first_playback_urb_complete(
struct urb *urb)
211 struct ua101 *ua = urb->context;
213 urb->complete = playback_urb_complete;
214 playback_urb_complete(urb);
221 static bool copy_playback_data(
struct ua101_stream *stream,
struct urb *urb,
225 unsigned int frame_bytes, frames1;
228 runtime = stream->substream->runtime;
229 frame_bytes = stream->frame_bytes;
230 source = runtime->
dma_area + stream->buffer_pos * frame_bytes;
231 if (stream->buffer_pos + frames <= runtime->
buffer_size) {
232 memcpy(urb->transfer_buffer, source, frames * frame_bytes);
235 frames1 = runtime->
buffer_size - stream->buffer_pos;
236 memcpy(urb->transfer_buffer, source, frames1 * frame_bytes);
237 memcpy(urb->transfer_buffer + frames1 * frame_bytes,
238 runtime->
dma_area, (frames - frames1) * frame_bytes);
241 stream->buffer_pos += frames;
244 stream->period_pos += frames;
252 static inline void add_with_wraparound(
struct ua101 *ua,
253 unsigned int *
value,
unsigned int add)
256 if (*value >= ua->
playback.queue_length)
257 *value -= ua->
playback.queue_length;
260 static void playback_tasklet(
unsigned long data)
262 struct ua101 *ua = (
void *)data;
265 struct ua101_urb *
urb;
266 bool do_period_elapsed =
false;
293 struct ua101_urb, ready_list);
297 urb->urb.iso_frame_desc[0].length =
300 do_period_elapsed |= copy_playback_data(&ua->
playback,
304 memset(urb->urb.transfer_buffer, 0,
305 urb->urb.iso_frame_desc[0].length);
310 spin_unlock_irqrestore(&ua->
lock, flags);
311 abort_usb_playback(ua);
312 abort_alsa_playback(ua);
313 dev_err(&ua->
dev->dev,
"USB request error %d: %s\n",
314 err, usb_error_string(err));
317 ua->
playback.substream->runtime->delay += frames;
319 spin_unlock_irqrestore(&ua->
lock, flags);
320 if (do_period_elapsed)
325 static bool copy_capture_data(
struct ua101_stream *stream,
struct urb *urb,
329 unsigned int frame_bytes, frames1;
332 runtime = stream->substream->runtime;
333 frame_bytes = stream->frame_bytes;
334 dest = runtime->
dma_area + stream->buffer_pos * frame_bytes;
335 if (stream->buffer_pos + frames <= runtime->buffer_size) {
336 memcpy(dest, urb->transfer_buffer, frames * frame_bytes);
339 frames1 = runtime->
buffer_size - stream->buffer_pos;
340 memcpy(dest, urb->transfer_buffer, frames1 * frame_bytes);
342 urb->transfer_buffer + frames1 * frame_bytes,
343 (frames - frames1) * frame_bytes);
346 stream->buffer_pos += frames;
349 stream->period_pos += frames;
357 static void capture_urb_complete(
struct urb *urb)
359 struct ua101 *ua = urb->context;
360 struct ua101_stream *stream = &ua->
capture;
362 unsigned int frames, write_ptr;
363 bool do_period_elapsed;
372 if (urb->status >= 0 && urb->iso_frame_desc[0].status >= 0)
373 frames = urb->iso_frame_desc[0].actual_length /
381 do_period_elapsed = copy_capture_data(stream, urb, frames);
383 do_period_elapsed =
false;
388 spin_unlock_irqrestore(&ua->
lock, flags);
389 dev_err(&ua->
dev->dev,
"USB request error %d: %s\n",
390 err, usb_error_string(err));
417 spin_unlock_irqrestore(&ua->
lock, flags);
419 if (do_period_elapsed)
425 abort_usb_playback(ua);
426 abort_usb_capture(ua);
427 abort_alsa_playback(ua);
428 abort_alsa_capture(ua);
431 static void first_capture_urb_complete(
struct urb *urb)
433 struct ua101 *ua = urb->context;
435 urb->complete = capture_urb_complete;
436 capture_urb_complete(urb);
442 static int submit_stream_urbs(
struct ua101 *ua,
struct ua101_stream *stream)
446 for (i = 0; i < stream->queue_length; ++
i) {
449 dev_err(&ua->
dev->dev,
"USB request error %d: %s\n",
450 err, usb_error_string(err));
457 static void kill_stream_urbs(
struct ua101_stream *stream)
461 for (i = 0; i < stream->queue_length; ++
i)
466 static int enable_iso_interface(
struct ua101 *ua,
unsigned int intf_index)
468 struct usb_host_interface *alts;
470 alts = ua->
intf[intf_index]->cur_altsetting;
471 if (alts->desc.bAlternateSetting != 1) {
473 alts->desc.bInterfaceNumber, 1);
476 "cannot initialize interface; error %d: %s\n",
477 err, usb_error_string(err));
484 static void disable_iso_interface(
struct ua101 *ua,
unsigned int intf_index)
486 struct usb_host_interface *alts;
488 if (!ua->
intf[intf_index])
491 alts = ua->
intf[intf_index]->cur_altsetting;
492 if (alts->desc.bAlternateSetting != 0) {
494 alts->desc.bInterfaceNumber, 0);
497 "interface reset failed; error %d: %s\n",
498 err, usb_error_string(err));
502 static void stop_usb_capture(
struct ua101 *ua)
506 kill_stream_urbs(&ua->
capture);
511 static int start_usb_capture(
struct ua101 *ua)
521 kill_stream_urbs(&ua->
capture);
528 ua->
capture.urbs[0]->urb.complete = first_capture_urb_complete;
533 err = submit_stream_urbs(ua, &ua->
capture);
535 stop_usb_capture(ua);
539 static void stop_usb_playback(
struct ua101 *ua)
550 static int start_usb_playback(
struct ua101 *ua)
552 unsigned int i, frames;
570 ua->
playback.urbs[0]->urb.complete =
571 first_playback_urb_complete;
572 spin_lock_irq(&ua->
lock);
574 spin_unlock_irq(&ua->
lock);
585 stop_usb_playback(ua);
589 stop_usb_playback(ua);
593 for (i = 0; i < ua->
playback.queue_length; ++
i) {
595 spin_lock_irq(&ua->
lock);
599 spin_unlock_irq(&ua->
lock);
601 urb->iso_frame_desc[0].length =
603 memset(urb->transfer_buffer, 0,
604 urb->iso_frame_desc[0].length);
608 err = submit_stream_urbs(ua, &ua->
playback);
610 stop_usb_playback(ua);
614 static void abort_alsa_capture(
struct ua101 *ua)
620 static void abort_alsa_playback(
struct ua101 *ua)
644 substream->
runtime->hw.buffer_bytes_max = 45000 * 1024;
645 substream->
runtime->hw.period_bytes_min = 1;
647 substream->
runtime->hw.periods_min = 2;
664 ua->
capture.substream = substream;
665 err = set_stream_hw(ua, substream, ua->
capture.channels);
668 substream->
runtime->hw.fifo_size =
673 err = start_usb_capture(ua);
686 err = set_stream_hw(ua, substream, ua->
playback.channels);
689 substream->
runtime->hw.fifo_size =
694 err = start_usb_capture(ua);
697 err = start_usb_playback(ua);
700 stop_usb_capture(ua);
716 stop_usb_capture(ua);
726 stop_usb_playback(ua);
729 stop_usb_capture(ua);
741 err = start_usb_capture(ua);
757 err = start_usb_capture(ua);
759 err = start_usb_playback(ua);
779 err = start_usb_capture(ua);
809 err = start_usb_capture(ua);
811 err = start_usb_playback(ua);
868 struct ua101_stream *stream)
874 pos = stream->buffer_pos;
875 spin_unlock_irqrestore(&ua->
lock, flags);
883 return ua101_pcm_pointer(ua, &ua->
capture);
890 return ua101_pcm_pointer(ua, &ua->
playback);
894 .open = capture_pcm_open,
895 .close = capture_pcm_close,
897 .hw_params = capture_pcm_hw_params,
898 .hw_free = ua101_pcm_hw_free,
899 .prepare = capture_pcm_prepare,
900 .trigger = capture_pcm_trigger,
901 .pointer = capture_pcm_pointer,
907 .open = playback_pcm_open,
908 .close = playback_pcm_close,
910 .hw_params = playback_pcm_hw_params,
911 .hw_free = ua101_pcm_hw_free,
912 .prepare = playback_pcm_prepare,
913 .trigger = playback_pcm_trigger,
914 .pointer = playback_pcm_pointer,
922 struct usb_host_interface *alt;
926 if (interface->num_altsetting != 2) {
927 dev_err(&interface->dev,
"invalid num_altsetting\n");
931 alt = &interface->altsetting[0];
932 if (alt->desc.bNumEndpoints != 0) {
933 dev_err(&interface->dev,
"invalid bNumEndpoints\n");
937 alt = &interface->altsetting[1];
938 if (alt->desc.bNumEndpoints != 1) {
939 dev_err(&interface->dev,
"invalid bNumEndpoints\n");
944 extralen = alt->extralen;
949 if (desc->
bLength > extralen) {
950 dev_err(&interface->dev,
"descriptor overflow\n");
959 "invalid format type\n");
967 dev_err(&interface->dev,
"sample format descriptor not found\n");
971 static int detect_usb_format(
struct ua101 *ua)
980 if (!fmt_capture || !fmt_playback)
991 dev_err(&ua->
dev->dev,
"sample width is not 24 or 32 bits\n");
996 "playback/capture sample widths do not match\n");
1002 dev_err(&ua->
dev->dev,
"sample width is not 24 bits\n");
1008 if (ua->
rate != rate2) {
1010 "playback/capture rates do not match: %u/%u\n",
1015 switch (ua->
dev->speed) {
1023 dev_err(&ua->
dev->dev,
"unknown device speed\n");
1035 if (!usb_endpoint_is_isoc_in(epd)) {
1036 dev_err(&ua->
dev->dev,
"invalid capture endpoint\n");
1039 ua->
capture.usb_pipe = usb_rcvisocpipe(ua->
dev, usb_endpoint_num(epd));
1043 if (!usb_endpoint_is_isoc_out(epd)) {
1044 dev_err(&ua->
dev->dev,
"invalid playback endpoint\n");
1047 ua->
playback.usb_pipe = usb_sndisocpipe(ua->
dev, usb_endpoint_num(epd));
1052 static int alloc_stream_buffers(
struct ua101 *ua,
struct ua101_stream *stream)
1054 unsigned int remaining_packets,
packets, packets_per_page,
i;
1057 stream->queue_length = queue_length;
1058 stream->queue_length =
max(stream->queue_length,
1060 stream->queue_length =
min(stream->queue_length,
1069 remaining_packets = stream->queue_length;
1070 packets_per_page =
PAGE_SIZE / stream->max_packet_bytes;
1071 for (i = 0; i <
ARRAY_SIZE(stream->buffers); ++
i) {
1072 packets =
min(remaining_packets, packets_per_page);
1073 size = packets * stream->max_packet_bytes;
1074 stream->buffers[
i].addr =
1076 &stream->buffers[i].dma);
1077 if (!stream->buffers[i].addr)
1079 stream->buffers[
i].size =
size;
1081 if (!remaining_packets)
1084 if (remaining_packets) {
1085 dev_err(&ua->
dev->dev,
"too many packets\n");
1091 static void free_stream_buffers(
struct ua101 *ua,
struct ua101_stream *stream)
1097 stream->buffers[i].size,
1098 stream->buffers[i].addr,
1099 stream->buffers[i].dma);
1102 static int alloc_stream_urbs(
struct ua101 *ua,
struct ua101_stream *stream,
1103 void (*urb_complete)(
struct urb *))
1105 unsigned max_packet_size = stream->max_packet_bytes;
1106 struct ua101_urb *
urb;
1107 unsigned int b,
u = 0;
1109 for (b = 0; b <
ARRAY_SIZE(stream->buffers); ++
b) {
1110 unsigned int size = stream->buffers[
b].size;
1114 while (size >= max_packet_size) {
1115 if (u >= stream->queue_length)
1121 urb->urb.dev = ua->
dev;
1122 urb->urb.pipe = stream->usb_pipe;
1123 urb->urb.transfer_flags = URB_ISO_ASAP |
1124 URB_NO_TRANSFER_DMA_MAP;
1125 urb->urb.transfer_buffer =
addr;
1126 urb->urb.transfer_dma =
dma;
1127 urb->urb.transfer_buffer_length = max_packet_size;
1128 urb->urb.number_of_packets = 1;
1129 urb->urb.interval = 1;
1130 urb->urb.context =
ua;
1131 urb->urb.complete = urb_complete;
1132 urb->urb.iso_frame_desc[0].offset = 0;
1133 urb->urb.iso_frame_desc[0].length = max_packet_size;
1134 stream->urbs[u++] =
urb;
1135 size -= max_packet_size;
1136 addr += max_packet_size;
1137 dma += max_packet_size;
1140 if (u == stream->queue_length)
1143 dev_err(&ua->
dev->dev,
"internal buffer size error\n");
1147 static void free_stream_urbs(
struct ua101_stream *stream)
1151 for (i = 0; i < stream->queue_length; ++
i) {
1152 kfree(stream->urbs[i]);
1153 stream->urbs[
i] =
NULL;
1157 static void free_usb_related_resources(
struct ua101 *ua,
1164 free_stream_urbs(&ua->
capture);
1167 free_stream_buffers(ua, &ua->
capture);
1168 free_stream_buffers(ua, &ua->
playback);
1176 usb_set_intfdata(intf,
NULL);
1177 if (intf != interface)
1202 static const int intf_numbers[2][3] = {
1222 is_ua1000 = usb_id->
idProduct == 0x0044;
1224 if (interface->altsetting->desc.bInterfaceNumber !=
1225 intf_numbers[is_ua1000][0])
1231 if (
enable[card_index] && !(devices_used & (1 << card_index)))
1233 if (card_index >= SNDRV_CARDS) {
1238 sizeof(*ua), &card);
1245 ua->
dev = interface_to_usbdev(interface);
1261 intf_numbers[is_ua1000][i]);
1263 dev_err(&ua->
dev->dev,
"interface %u not found\n",
1264 intf_numbers[is_ua1000][i]);
1279 err = detect_usb_format(ua);
1283 name = usb_id->
idProduct == 0x0044 ?
"UA-1000" :
"UA-101";
1286 usb_make_path(ua->
dev, usb_path,
sizeof(usb_path));
1288 "EDIROL %s (serial %s), %u Hz at %s, %s speed", name,
1289 ua->
dev->serial ? ua->
dev->serial :
"?", ua->
rate, usb_path,
1292 err = alloc_stream_buffers(ua, &ua->
capture);
1295 err = alloc_stream_buffers(ua, &ua->
playback);
1299 err = alloc_stream_urbs(ua, &ua->
capture, capture_urb_complete);
1302 err = alloc_stream_urbs(ua, &ua->
playback, playback_urb_complete);
1309 ua->
pcm->private_data =
ua;
1323 usb_set_intfdata(interface, ua);
1330 free_usb_related_resources(ua, interface);
1336 static void ua101_disconnect(
struct usb_interface *interface)
1338 struct ua101 *ua = usb_get_intfdata(interface);
1355 abort_alsa_playback(ua);
1356 abort_alsa_capture(ua);
1358 stop_usb_playback(ua);
1359 stop_usb_capture(ua);
1362 free_usb_related_resources(ua, interface);
1364 devices_used &= ~(1 << ua->card_index);
1372 { USB_DEVICE(0x0582, 0x0044) },
1373 { USB_DEVICE(0x0582, 0x007d) },
1374 { USB_DEVICE(0x0582, 0x008d) },
1379 static struct usb_driver ua101_driver = {
1380 .name =
"snd-ua101",
1381 .id_table = ua101_ids,
1382 .probe = ua101_probe,
1383 .disconnect = ua101_disconnect,
1385 .suspend = ua101_suspend,
1386 .resume = ua101_resume,