aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClemens Ladisch <clemens@ladisch.de>2009-12-14 06:48:35 -0500
committerTakashi Iwai <tiwai@suse.de>2009-12-14 11:58:13 -0500
commit63978ab3e3e963db28093b53bb4598f2702e1ad7 (patch)
treefff9fa47e90317b1e5914e0a6f51d8e896760bf0
parent0d64b568fcd48b133721c1d322e7c51d85eb12df (diff)
sound: add Edirol UA-101 support
Add experimental support for the Edirol UA-101 audio/MIDI interface. Signed-off-by: Clemens Ladisch <clemens@ladisch.de> Signed-off-by: Takashi Iwai <tiwai@suse.de>
-rw-r--r--Documentation/sound/alsa/ALSA-Configuration.txt7
-rw-r--r--sound/usb/Kconfig12
-rw-r--r--sound/usb/Makefile2
-rw-r--r--sound/usb/ua101.c1457
-rw-r--r--sound/usb/usbaudio.c54
-rw-r--r--sound/usb/usbaudio.h1
-rw-r--r--sound/usb/usbquirks.h31
7 files changed, 1478 insertions, 86 deletions
diff --git a/Documentation/sound/alsa/ALSA-Configuration.txt b/Documentation/sound/alsa/ALSA-Configuration.txt
index 8923597bd2bd..7a0a4a9dc18d 100644
--- a/Documentation/sound/alsa/ALSA-Configuration.txt
+++ b/Documentation/sound/alsa/ALSA-Configuration.txt
@@ -1791,6 +1791,13 @@ Prior to version 0.9.0rc4 options had a 'snd_' prefix. This was removed.
1791 1791
1792 The power-management is supported. 1792 The power-management is supported.
1793 1793
1794 Module snd-ua101
1795 ----------------
1796
1797 Module for the Edirol UA-101 audio/MIDI interface.
1798
1799 This module supports multiple devices, autoprobe and hotplugging.
1800
1794 Module snd-usb-audio 1801 Module snd-usb-audio
1795 -------------------- 1802 --------------------
1796 1803
diff --git a/sound/usb/Kconfig b/sound/usb/Kconfig
index 73525c048e7f..8c2925814ce4 100644
--- a/sound/usb/Kconfig
+++ b/sound/usb/Kconfig
@@ -21,6 +21,18 @@ config SND_USB_AUDIO
21 To compile this driver as a module, choose M here: the module 21 To compile this driver as a module, choose M here: the module
22 will be called snd-usb-audio. 22 will be called snd-usb-audio.
23 23
24config SND_USB_UA101
25 tristate "Edirol UA-101 driver (EXPERIMENTAL)"
26 depends on EXPERIMENTAL
27 select SND_PCM
28 select SND_RAWMIDI
29 help
30 Say Y here to include support for the Edirol UA-101 audio/MIDI
31 interface.
32
33 To compile this driver as a module, choose M here: the module
34 will be called snd-ua101.
35
24config SND_USB_USX2Y 36config SND_USB_USX2Y
25 tristate "Tascam US-122, US-224 and US-428 USB driver" 37 tristate "Tascam US-122, US-224 and US-428 USB driver"
26 depends on X86 || PPC || ALPHA 38 depends on X86 || PPC || ALPHA
diff --git a/sound/usb/Makefile b/sound/usb/Makefile
index abb288bfe35d..5bf64aef9558 100644
--- a/sound/usb/Makefile
+++ b/sound/usb/Makefile
@@ -4,9 +4,11 @@
4 4
5snd-usb-audio-objs := usbaudio.o usbmixer.o 5snd-usb-audio-objs := usbaudio.o usbmixer.o
6snd-usb-lib-objs := usbmidi.o 6snd-usb-lib-objs := usbmidi.o
7snd-ua101-objs := ua101.o
7 8
8# Toplevel Module Dependency 9# Toplevel Module Dependency
9obj-$(CONFIG_SND_USB_AUDIO) += snd-usb-audio.o snd-usb-lib.o 10obj-$(CONFIG_SND_USB_AUDIO) += snd-usb-audio.o snd-usb-lib.o
11obj-$(CONFIG_SND_USB_UA101) += snd-ua101.o snd-usb-lib.o
10obj-$(CONFIG_SND_USB_USX2Y) += snd-usb-lib.o 12obj-$(CONFIG_SND_USB_USX2Y) += snd-usb-lib.o
11obj-$(CONFIG_SND_USB_US122L) += snd-usb-lib.o 13obj-$(CONFIG_SND_USB_US122L) += snd-usb-lib.o
12 14
diff --git a/sound/usb/ua101.c b/sound/usb/ua101.c
new file mode 100644
index 000000000000..ab9f8a2e1938
--- /dev/null
+++ b/sound/usb/ua101.c
@@ -0,0 +1,1457 @@
1/*
2 * Edirol UA-101 driver
3 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
4 *
5 * This driver is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2.
7 *
8 * This driver is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this driver. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include <linux/init.h>
18#include <linux/module.h>
19#include <linux/slab.h>
20#include <linux/usb.h>
21#include <linux/usb/audio.h>
22#include <linux/vmalloc.h>
23#include <sound/core.h>
24#include <sound/initval.h>
25#include <sound/pcm.h>
26#include <sound/pcm_params.h>
27#include "usbaudio.h"
28
29MODULE_DESCRIPTION("Edirol UA-101 driver");
30MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
31MODULE_LICENSE("GPL v2");
32MODULE_SUPPORTED_DEVICE("{{Edirol,UA-101}}");
33
34/* I use my UA-1A for testing because I don't have a UA-101 ... */
35#define UA1A_HACK
36
37/*
38 * Should not be lower than the minimum scheduling delay of the host
39 * controller. Some Intel controllers need more than one frame; as long as
40 * that driver doesn't tell us about this, use 1.5 frames just to be sure.
41 */
42#define MIN_QUEUE_LENGTH 12
43/* Somewhat random. */
44#define MAX_QUEUE_LENGTH 30
45/*
46 * This magic value optimizes memory usage efficiency for the UA-101's packet
47 * sizes at all sample rates, taking into account the stupid cache pool sizes
48 * that usb_buffer_alloc() uses.
49 */
50#define DEFAULT_QUEUE_LENGTH 21
51
52#define MAX_PACKET_SIZE 672 /* hardware specific */
53#define MAX_MEMORY_BUFFERS DIV_ROUND_UP(MAX_QUEUE_LENGTH, \
54 PAGE_SIZE / MAX_PACKET_SIZE)
55
56static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
57static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
58static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
59static unsigned int queue_length = 21;
60
61module_param_array(index, int, NULL, 0444);
62MODULE_PARM_DESC(index, "card index");
63module_param_array(id, charp, NULL, 0444);
64MODULE_PARM_DESC(id, "ID string");
65module_param_array(enable, bool, NULL, 0444);
66MODULE_PARM_DESC(enable, "enable card");
67module_param(queue_length, uint, 0644);
68MODULE_PARM_DESC(queue_length, "USB queue length in microframes, "
69 __stringify(MIN_QUEUE_LENGTH)"-"__stringify(MAX_QUEUE_LENGTH));
70
71enum {
72 INTF_PLAYBACK,
73 INTF_CAPTURE,
74 INTF_MIDI,
75
76 INTF_COUNT
77};
78
79/* bits in struct ua101::states */
80enum {
81 USB_CAPTURE_RUNNING,
82 USB_PLAYBACK_RUNNING,
83 ALSA_CAPTURE_OPEN,
84 ALSA_PLAYBACK_OPEN,
85 ALSA_CAPTURE_RUNNING,
86 ALSA_PLAYBACK_RUNNING,
87 CAPTURE_URB_COMPLETED,
88 PLAYBACK_URB_COMPLETED,
89 DISCONNECTED,
90};
91
92struct ua101 {
93 struct usb_device *dev;
94 struct snd_card *card;
95 struct usb_interface *intf[INTF_COUNT];
96 int card_index;
97 struct snd_pcm *pcm;
98 struct list_head midi_list;
99 u64 format_bit;
100 unsigned int rate;
101 unsigned int packets_per_second;
102 spinlock_t lock;
103 struct mutex mutex;
104 unsigned long states;
105
106 /* FIFO to synchronize playback rate to capture rate */
107 unsigned int rate_feedback_start;
108 unsigned int rate_feedback_count;
109 u8 rate_feedback[MAX_QUEUE_LENGTH];
110
111 struct list_head ready_playback_urbs;
112 struct tasklet_struct playback_tasklet;
113 wait_queue_head_t alsa_capture_wait;
114 wait_queue_head_t rate_feedback_wait;
115 wait_queue_head_t alsa_playback_wait;
116 struct ua101_stream {
117 struct snd_pcm_substream *substream;
118 unsigned int usb_pipe;
119 unsigned int channels;
120 unsigned int frame_bytes;
121 unsigned int max_packet_bytes;
122 unsigned int period_pos;
123 unsigned int buffer_pos;
124 unsigned int queue_length;
125 struct ua101_urb {
126 struct urb urb;
127 struct usb_iso_packet_descriptor iso_frame_desc[1];
128 struct list_head ready_list;
129 } *urbs[MAX_QUEUE_LENGTH];
130 struct {
131 unsigned int size;
132 void *addr;
133 dma_addr_t dma;
134 } buffers[MAX_MEMORY_BUFFERS];
135 } capture, playback;
136
137 unsigned int fps[10];
138 unsigned int frame_counter;
139};
140
141static DEFINE_MUTEX(devices_mutex);
142static unsigned int devices_used;
143static struct usb_driver ua101_driver;
144
145static void abort_alsa_playback(struct ua101 *ua);
146static void abort_alsa_capture(struct ua101 *ua);
147
148/* allocate virtual buffer; may be called more than once */
149static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
150 size_t size)
151{
152 struct snd_pcm_runtime *runtime = subs->runtime;
153
154 if (runtime->dma_area) {
155 if (runtime->dma_bytes >= size)
156 return 0; /* already large enough */
157 vfree(runtime->dma_area);
158 }
159 runtime->dma_area = vmalloc_user(size);
160 if (!runtime->dma_area)
161 return -ENOMEM;
162 runtime->dma_bytes = size;
163 return 0;
164}
165
166/* free virtual buffer; may be called more than once */
167static int snd_pcm_free_vmalloc_buffer(struct snd_pcm_substream *subs)
168{
169 struct snd_pcm_runtime *runtime = subs->runtime;
170
171 vfree(runtime->dma_area);
172 runtime->dma_area = NULL;
173 return 0;
174}
175
176/* get the physical page pointer at the given offset */
177static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
178 unsigned long offset)
179{
180 void *pageptr = subs->runtime->dma_area + offset;
181 return vmalloc_to_page(pageptr);
182}
183
184static const char *usb_error_string(int err)
185{
186 switch (err) {
187 case -ENODEV:
188 return "no device";
189 case -ENOENT:
190 return "endpoint not enabled";
191 case -EPIPE:
192 return "endpoint stalled";
193 case -ENOSPC:
194 return "not enough bandwidth";
195 case -ESHUTDOWN:
196 return "device disabled";
197 case -EHOSTUNREACH:
198 return "device suspended";
199 case -EINVAL:
200 case -EAGAIN:
201 case -EFBIG:
202 case -EMSGSIZE:
203 return "internal error";
204 default:
205 return "unknown error";
206 }
207}
208
209static void abort_usb_capture(struct ua101 *ua)
210{
211 if (test_and_clear_bit(USB_CAPTURE_RUNNING, &ua->states)) {
212 wake_up(&ua->alsa_capture_wait);
213 wake_up(&ua->rate_feedback_wait);
214 }
215}
216
217static void abort_usb_playback(struct ua101 *ua)
218{
219 if (test_and_clear_bit(USB_PLAYBACK_RUNNING, &ua->states))
220 wake_up(&ua->alsa_playback_wait);
221}
222
223static void playback_urb_complete(struct urb *usb_urb)
224{
225 struct ua101_urb *urb = (struct ua101_urb *)usb_urb;
226 struct ua101 *ua = urb->urb.context;
227 unsigned long flags;
228
229 if (unlikely(urb->urb.status == -ENOENT || /* unlinked */
230 urb->urb.status == -ENODEV || /* device removed */
231 urb->urb.status == -ECONNRESET || /* unlinked */
232 urb->urb.status == -ESHUTDOWN)) { /* device disabled */
233 abort_usb_playback(ua);
234 abort_alsa_playback(ua);
235 return;
236 }
237
238 if (test_bit(USB_PLAYBACK_RUNNING, &ua->states)) {
239 /* append URB to FIFO */
240 spin_lock_irqsave(&ua->lock, flags);
241 list_add_tail(&urb->ready_list, &ua->ready_playback_urbs);
242 if (ua->rate_feedback_count > 0)
243 tasklet_schedule(&ua->playback_tasklet);
244 ua->playback.substream->runtime->delay -=
245 urb->urb.iso_frame_desc[0].length /
246 ua->playback.frame_bytes;
247 spin_unlock_irqrestore(&ua->lock, flags);
248 }
249}
250
251static void first_playback_urb_complete(struct urb *urb)
252{
253 struct ua101 *ua = urb->context;
254
255 urb->complete = playback_urb_complete;
256 playback_urb_complete(urb);
257
258 set_bit(PLAYBACK_URB_COMPLETED, &ua->states);
259 wake_up(&ua->alsa_playback_wait);
260}
261
262/* copy data from the ALSA ring buffer into the URB buffer */
263static bool copy_playback_data(struct ua101_stream *stream, struct urb *urb,
264 unsigned int frames)
265{
266 struct snd_pcm_runtime *runtime;
267 unsigned int frame_bytes, frames1;
268 const u8 *source;
269
270 runtime = stream->substream->runtime;
271 frame_bytes = stream->frame_bytes;
272 source = runtime->dma_area + stream->buffer_pos * frame_bytes;
273 if (stream->buffer_pos + frames <= runtime->buffer_size) {
274 memcpy(urb->transfer_buffer, source, frames * frame_bytes);
275 } else {
276 /* wrap around at end of ring buffer */
277 frames1 = runtime->buffer_size - stream->buffer_pos;
278 memcpy(urb->transfer_buffer, source, frames1 * frame_bytes);
279 memcpy(urb->transfer_buffer + frames1 * frame_bytes,
280 runtime->dma_area, (frames - frames1) * frame_bytes);
281 }
282
283 stream->buffer_pos += frames;
284 if (stream->buffer_pos >= runtime->buffer_size)
285 stream->buffer_pos -= runtime->buffer_size;
286 stream->period_pos += frames;
287 if (stream->period_pos >= runtime->period_size) {
288 stream->period_pos -= runtime->period_size;
289 return true;
290 }
291 return false;
292}
293
294static inline void add_with_wraparound(struct ua101 *ua,
295 unsigned int *value, unsigned int add)
296{
297 *value += add;
298 if (*value >= ua->playback.queue_length)
299 *value -= ua->playback.queue_length;
300}
301
302static void playback_tasklet(unsigned long data)
303{
304 struct ua101 *ua = (void *)data;
305 unsigned long flags;
306 unsigned int frames;
307 struct ua101_urb *urb;
308 bool do_period_elapsed = false;
309 int err;
310
311 if (unlikely(!test_bit(USB_PLAYBACK_RUNNING, &ua->states)))
312 return;
313
314 /*
315 * Synchronizing the playback rate to the capture rate is done by using
316 * the same sequence of packet sizes for both streams.
317 * Submitting a playback URB therefore requires both a ready URB and
318 * the size of the corresponding capture packet, i.e., both playback
319 * and capture URBs must have been completed. Since the USB core does
320 * not guarantee that playback and capture complete callbacks are
321 * called alternately, we use two FIFOs for packet sizes and read URBs;
322 * submitting playback URBs is possible as long as both FIFOs are
323 * nonempty.
324 */
325 spin_lock_irqsave(&ua->lock, flags);
326 while (ua->rate_feedback_count > 0 &&
327 !list_empty(&ua->ready_playback_urbs)) {
328 /* take packet size out of FIFO */
329 frames = ua->rate_feedback[ua->rate_feedback_start];
330 add_with_wraparound(ua, &ua->rate_feedback_start, 1);
331 ua->rate_feedback_count--;
332
333 /* take URB out of FIFO */
334 urb = list_first_entry(&ua->ready_playback_urbs,
335 struct ua101_urb, ready_list);
336 list_del(&urb->ready_list);
337
338 /* fill packet with data or silence */
339 urb->urb.iso_frame_desc[0].length =
340 frames * ua->playback.frame_bytes;
341 if (test_bit(ALSA_PLAYBACK_RUNNING, &ua->states))
342 do_period_elapsed |= copy_playback_data(&ua->playback,
343 &urb->urb,
344 frames);
345 else
346 memset(urb->urb.transfer_buffer, 0,
347 urb->urb.iso_frame_desc[0].length);
348
349 /* and off you go ... */
350 err = usb_submit_urb(&urb->urb, GFP_ATOMIC);
351 if (unlikely(err < 0)) {
352 spin_unlock_irqrestore(&ua->lock, flags);
353 abort_usb_playback(ua);
354 abort_alsa_playback(ua);
355 dev_err(&ua->dev->dev, "USB request error %d: %s\n",
356 err, usb_error_string(err));
357 return;
358 }
359 ua->playback.substream->runtime->delay += frames;
360 }
361 spin_unlock_irqrestore(&ua->lock, flags);
362 if (do_period_elapsed)
363 snd_pcm_period_elapsed(ua->playback.substream);
364}
365
366/* copy data from the URB buffer into the ALSA ring buffer */
367static bool copy_capture_data(struct ua101_stream *stream, struct urb *urb,
368 unsigned int frames)
369{
370 struct snd_pcm_runtime *runtime;
371 unsigned int frame_bytes, frames1;
372 u8 *dest;
373
374 runtime = stream->substream->runtime;
375 frame_bytes = stream->frame_bytes;
376 dest = runtime->dma_area + stream->buffer_pos * frame_bytes;
377 if (stream->buffer_pos + frames <= runtime->buffer_size) {
378 memcpy(dest, urb->transfer_buffer, frames * frame_bytes);
379 } else {
380 /* wrap around at end of ring buffer */
381 frames1 = runtime->buffer_size - stream->buffer_pos;
382 memcpy(dest, urb->transfer_buffer, frames1 * frame_bytes);
383 memcpy(runtime->dma_area,
384 urb->transfer_buffer + frames1 * frame_bytes,
385 (frames - frames1) * frame_bytes);
386 }
387
388 stream->buffer_pos += frames;
389 if (stream->buffer_pos >= runtime->buffer_size)
390 stream->buffer_pos -= runtime->buffer_size;
391 stream->period_pos += frames;
392 if (stream->period_pos >= runtime->period_size) {
393 stream->period_pos -= runtime->period_size;
394 return true;
395 }
396 return false;
397}
398
399static void capture_urb_complete(struct urb *urb)
400{
401 struct ua101 *ua = urb->context;
402 struct ua101_stream *stream = &ua->capture;
403 unsigned long flags;
404 unsigned int frames, write_ptr;
405 bool do_period_elapsed;
406 int err;
407
408 if (unlikely(urb->status == -ENOENT || /* unlinked */
409 urb->status == -ENODEV || /* device removed */
410 urb->status == -ECONNRESET || /* unlinked */
411 urb->status == -ESHUTDOWN)) /* device disabled */
412 goto stream_stopped;
413
414 if (urb->status >= 0 && urb->iso_frame_desc[0].status >= 0)
415 frames = urb->iso_frame_desc[0].actual_length /
416 stream->frame_bytes;
417 else
418 frames = 0;
419
420 spin_lock_irqsave(&ua->lock, flags);
421
422 if (frames > 0 && test_bit(ALSA_CAPTURE_RUNNING, &ua->states))
423 do_period_elapsed = copy_capture_data(stream, urb, frames);
424 else
425 do_period_elapsed = false;
426
427 if (test_bit(USB_CAPTURE_RUNNING, &ua->states)) {
428 err = usb_submit_urb(urb, GFP_ATOMIC);
429 if (unlikely(err < 0)) {
430 spin_unlock_irqrestore(&ua->lock, flags);
431 dev_err(&ua->dev->dev, "USB request error %d: %s\n",
432 err, usb_error_string(err));
433 goto stream_stopped;
434 }
435
436 /* append packet size to FIFO */
437 write_ptr = ua->rate_feedback_start;
438 add_with_wraparound(ua, &write_ptr, ua->rate_feedback_count);
439 ua->rate_feedback[write_ptr] = frames;
440 if (ua->rate_feedback_count < ua->playback.queue_length) {
441 ua->rate_feedback_count++;
442 if (ua->rate_feedback_count ==
443 ua->playback.queue_length)
444 wake_up(&ua->rate_feedback_wait);
445 } else {
446 /*
447 * Ring buffer overflow; this happens when the playback
448 * stream is not running. Throw away the oldest entry,
449 * so that the playback stream, when it starts, sees
450 * the most recent packet sizes.
451 */
452 add_with_wraparound(ua, &ua->rate_feedback_start, 1);
453 }
454 if (test_bit(USB_PLAYBACK_RUNNING, &ua->states) &&
455 !list_empty(&ua->ready_playback_urbs))
456 tasklet_schedule(&ua->playback_tasklet);
457 }
458
459 spin_unlock_irqrestore(&ua->lock, flags);
460
461 if (do_period_elapsed)
462 snd_pcm_period_elapsed(stream->substream);
463
464 /* for debugging: measure the sample rate relative to the USB clock */
465 ua->fps[ua->frame_counter++ / ua->packets_per_second] += frames;
466 if (ua->frame_counter >= ARRAY_SIZE(ua->fps) * ua->packets_per_second) {
467 printk(KERN_DEBUG "capture rate:");
468 for (frames = 0; frames < ARRAY_SIZE(ua->fps); ++frames)
469 printk(KERN_CONT " %u", ua->fps[frames]);
470 printk(KERN_CONT "\n");
471 memset(ua->fps, 0, sizeof(ua->fps));
472 ua->frame_counter = 0;
473 }
474 return;
475
476stream_stopped:
477 abort_usb_playback(ua);
478 abort_usb_capture(ua);
479 abort_alsa_playback(ua);
480 abort_alsa_capture(ua);
481}
482
483static void first_capture_urb_complete(struct urb *urb)
484{
485 struct ua101 *ua = urb->context;
486
487 urb->complete = capture_urb_complete;
488 capture_urb_complete(urb);
489
490 set_bit(CAPTURE_URB_COMPLETED, &ua->states);
491 wake_up(&ua->alsa_capture_wait);
492}
493
494static int submit_stream_urbs(struct ua101 *ua, struct ua101_stream *stream)
495{
496 unsigned int i;
497
498 for (i = 0; i < stream->queue_length; ++i) {
499 int err = usb_submit_urb(&stream->urbs[i]->urb, GFP_KERNEL);
500 if (err < 0) {
501 dev_err(&ua->dev->dev, "USB request error %d: %s\n",
502 err, usb_error_string(err));
503 return err;
504 }
505 }
506 return 0;
507}
508
509static void kill_stream_urbs(struct ua101_stream *stream)
510{
511 unsigned int i;
512
513 for (i = 0; i < stream->queue_length; ++i)
514 usb_kill_urb(&stream->urbs[i]->urb);
515}
516
517static int enable_iso_interface(struct ua101 *ua, unsigned int intf_index)
518{
519 struct usb_host_interface *alts;
520
521 alts = ua->intf[intf_index]->cur_altsetting;
522 if (alts->desc.bAlternateSetting != 1) {
523 int err = usb_set_interface(ua->dev,
524 alts->desc.bInterfaceNumber, 1);
525 if (err < 0) {
526 dev_err(&ua->dev->dev,
527 "cannot initialize interface; error %d: %s\n",
528 err, usb_error_string(err));
529 return err;
530 }
531 }
532 return 0;
533}
534
535static void disable_iso_interface(struct ua101 *ua, unsigned int intf_index)
536{
537 struct usb_host_interface *alts;
538
539 alts = ua->intf[intf_index]->cur_altsetting;
540 if (alts->desc.bAlternateSetting != 0) {
541 int err = usb_set_interface(ua->dev,
542 alts->desc.bInterfaceNumber, 0);
543 if (err < 0 && !test_bit(DISCONNECTED, &ua->states))
544 dev_warn(&ua->dev->dev,
545 "interface reset failed; error %d: %s\n",
546 err, usb_error_string(err));
547 }
548}
549
550static void stop_usb_capture(struct ua101 *ua)
551{
552 clear_bit(USB_CAPTURE_RUNNING, &ua->states);
553
554 kill_stream_urbs(&ua->capture);
555
556 disable_iso_interface(ua, INTF_CAPTURE);
557}
558
559static int start_usb_capture(struct ua101 *ua)
560{
561 int err;
562
563 if (test_bit(DISCONNECTED, &ua->states))
564 return -ENODEV;
565
566 if (test_bit(USB_CAPTURE_RUNNING, &ua->states))
567 return 0;
568
569 kill_stream_urbs(&ua->capture);
570
571 err = enable_iso_interface(ua, INTF_CAPTURE);
572 if (err < 0)
573 return err;
574
575 clear_bit(CAPTURE_URB_COMPLETED, &ua->states);
576 ua->capture.urbs[0]->urb.complete = first_capture_urb_complete;
577 ua->rate_feedback_start = 0;
578 ua->rate_feedback_count = 0;
579
580 set_bit(USB_CAPTURE_RUNNING, &ua->states);
581 err = submit_stream_urbs(ua, &ua->capture);
582 if (err < 0)
583 stop_usb_capture(ua);
584 return err;
585}
586
587static void stop_usb_playback(struct ua101 *ua)
588{
589 clear_bit(USB_PLAYBACK_RUNNING, &ua->states);
590
591 kill_stream_urbs(&ua->playback);
592
593 tasklet_kill(&ua->playback_tasklet);
594
595 disable_iso_interface(ua, INTF_PLAYBACK);
596}
597
598static int start_usb_playback(struct ua101 *ua)
599{
600 unsigned int i, frames;
601 struct urb *urb;
602 int err = 0;
603
604 if (test_bit(DISCONNECTED, &ua->states))
605 return -ENODEV;
606
607 if (test_bit(USB_PLAYBACK_RUNNING, &ua->states))
608 return 0;
609
610 kill_stream_urbs(&ua->playback);
611 tasklet_kill(&ua->playback_tasklet);
612
613 err = enable_iso_interface(ua, INTF_PLAYBACK);
614 if (err < 0)
615 return err;
616
617 clear_bit(PLAYBACK_URB_COMPLETED, &ua->states);
618 ua->playback.urbs[0]->urb.complete =
619 first_playback_urb_complete;
620 spin_lock_irq(&ua->lock);
621 INIT_LIST_HEAD(&ua->ready_playback_urbs);
622 spin_unlock_irq(&ua->lock);
623
624 /*
625 * We submit the initial URBs all at once, so we have to wait for the
626 * packet size FIFO to be full.
627 */
628 wait_event(ua->rate_feedback_wait,
629 ua->rate_feedback_count >= ua->playback.queue_length ||
630 !test_bit(USB_CAPTURE_RUNNING, &ua->states) ||
631 test_bit(DISCONNECTED, &ua->states));
632 if (test_bit(DISCONNECTED, &ua->states)) {
633 stop_usb_playback(ua);
634 return -ENODEV;
635 }
636 if (!test_bit(USB_CAPTURE_RUNNING, &ua->states)) {
637 stop_usb_playback(ua);
638 return -EIO;
639 }
640
641 for (i = 0; i < ua->playback.queue_length; ++i) {
642 /* all initial URBs contain silence */
643 spin_lock_irq(&ua->lock);
644 frames = ua->rate_feedback[ua->rate_feedback_start];
645 add_with_wraparound(ua, &ua->rate_feedback_start, 1);
646 ua->rate_feedback_count--;
647 spin_unlock_irq(&ua->lock);
648 urb = &ua->playback.urbs[i]->urb;
649 urb->iso_frame_desc[0].length =
650 frames * ua->playback.frame_bytes;
651 memset(urb->transfer_buffer, 0,
652 urb->iso_frame_desc[0].length);
653 }
654
655 set_bit(USB_PLAYBACK_RUNNING, &ua->states);
656 err = submit_stream_urbs(ua, &ua->playback);
657 if (err < 0)
658 stop_usb_playback(ua);
659 return err;
660}
661
662static void abort_alsa_capture(struct ua101 *ua)
663{
664 if (test_bit(ALSA_CAPTURE_RUNNING, &ua->states))
665 snd_pcm_stop(ua->capture.substream, SNDRV_PCM_STATE_XRUN);
666}
667
668static void abort_alsa_playback(struct ua101 *ua)
669{
670 if (test_bit(ALSA_PLAYBACK_RUNNING, &ua->states))
671 snd_pcm_stop(ua->playback.substream, SNDRV_PCM_STATE_XRUN);
672}
673
674static int set_stream_hw(struct ua101 *ua, struct snd_pcm_substream *substream,
675 unsigned int channels)
676{
677 int err;
678
679 substream->runtime->hw.info =
680 SNDRV_PCM_INFO_MMAP |
681 SNDRV_PCM_INFO_MMAP_VALID |
682 SNDRV_PCM_INFO_BATCH |
683 SNDRV_PCM_INFO_INTERLEAVED |
684 SNDRV_PCM_INFO_BLOCK_TRANSFER |
685 SNDRV_PCM_INFO_FIFO_IN_FRAMES;
686 substream->runtime->hw.formats = ua->format_bit;
687 substream->runtime->hw.rates = snd_pcm_rate_to_rate_bit(ua->rate);
688 substream->runtime->hw.rate_min = ua->rate;
689 substream->runtime->hw.rate_max = ua->rate;
690 substream->runtime->hw.channels_min = channels;
691 substream->runtime->hw.channels_max = channels;
692 substream->runtime->hw.buffer_bytes_max = 45000 * 1024;
693 substream->runtime->hw.period_bytes_min = 1;
694 substream->runtime->hw.period_bytes_max = UINT_MAX;
695 substream->runtime->hw.periods_min = 2;
696 substream->runtime->hw.periods_max = UINT_MAX;
697 err = snd_pcm_hw_constraint_minmax(substream->runtime,
698 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
699 1500000 / ua->packets_per_second,
700 8192000);
701 if (err < 0)
702 return err;
703 err = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 32, 24);
704 return err;
705}
706
707static int capture_pcm_open(struct snd_pcm_substream *substream)
708{
709 struct ua101 *ua = substream->private_data;
710 int err;
711
712 ua->capture.substream = substream;
713 err = set_stream_hw(ua, substream, ua->capture.channels);
714 if (err < 0)
715 return err;
716 substream->runtime->hw.fifo_size =
717 DIV_ROUND_CLOSEST(ua->rate, ua->packets_per_second);
718 substream->runtime->delay = substream->runtime->hw.fifo_size;
719
720 mutex_lock(&ua->mutex);
721 err = start_usb_capture(ua);
722 if (err >= 0)
723 set_bit(ALSA_CAPTURE_OPEN, &ua->states);
724 mutex_unlock(&ua->mutex);
725 return err;
726}
727
728static int playback_pcm_open(struct snd_pcm_substream *substream)
729{
730 struct ua101 *ua = substream->private_data;
731 int err;
732
733 ua->playback.substream = substream;
734 err = set_stream_hw(ua, substream, ua->playback.channels);
735 if (err < 0)
736 return err;
737 substream->runtime->hw.fifo_size =
738 DIV_ROUND_CLOSEST(ua->rate * ua->playback.queue_length,
739 ua->packets_per_second);
740
741 mutex_lock(&ua->mutex);
742 err = start_usb_capture(ua);
743 if (err < 0)
744 goto error;
745 err = start_usb_playback(ua);
746 if (err < 0) {
747 if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states))
748 stop_usb_capture(ua);
749 goto error;
750 }
751 set_bit(ALSA_PLAYBACK_OPEN, &ua->states);
752error:
753 mutex_unlock(&ua->mutex);
754 return err;
755}
756
757static int capture_pcm_close(struct snd_pcm_substream *substream)
758{
759 struct ua101 *ua = substream->private_data;
760
761 mutex_lock(&ua->mutex);
762 clear_bit(ALSA_CAPTURE_OPEN, &ua->states);
763 if (!test_bit(ALSA_PLAYBACK_OPEN, &ua->states))
764 stop_usb_capture(ua);
765 mutex_unlock(&ua->mutex);
766 return 0;
767}
768
769static int playback_pcm_close(struct snd_pcm_substream *substream)
770{
771 struct ua101 *ua = substream->private_data;
772
773 mutex_lock(&ua->mutex);
774 stop_usb_playback(ua);
775 clear_bit(ALSA_PLAYBACK_OPEN, &ua->states);
776 if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states))
777 stop_usb_capture(ua);
778 mutex_unlock(&ua->mutex);
779 return 0;
780}
781
782static int capture_pcm_hw_params(struct snd_pcm_substream *substream,
783 struct snd_pcm_hw_params *hw_params)
784{
785 struct ua101 *ua = substream->private_data;
786 int err;
787
788 mutex_lock(&ua->mutex);
789 err = start_usb_capture(ua);
790 mutex_unlock(&ua->mutex);
791 if (err < 0)
792 return err;
793
794 return snd_pcm_alloc_vmalloc_buffer(substream,
795 params_buffer_bytes(hw_params));
796}
797
798static int playback_pcm_hw_params(struct snd_pcm_substream *substream,
799 struct snd_pcm_hw_params *hw_params)
800{
801 struct ua101 *ua = substream->private_data;
802 int err;
803
804 mutex_lock(&ua->mutex);
805 err = start_usb_capture(ua);
806 if (err >= 0)
807 err = start_usb_playback(ua);
808 mutex_unlock(&ua->mutex);
809 if (err < 0)
810 return err;
811
812 return snd_pcm_alloc_vmalloc_buffer(substream,
813 params_buffer_bytes(hw_params));
814}
815
816static int ua101_pcm_hw_free(struct snd_pcm_substream *substream)
817{
818 snd_pcm_free_vmalloc_buffer(substream);
819 return 0;
820}
821
822static int capture_pcm_prepare(struct snd_pcm_substream *substream)
823{
824 struct ua101 *ua = substream->private_data;
825 int err;
826
827 mutex_lock(&ua->mutex);
828 err = start_usb_capture(ua);
829 mutex_unlock(&ua->mutex);
830 if (err < 0)
831 return err;
832
833 /*
834 * The EHCI driver schedules the first packet of an iso stream at 10 ms
835 * in the future, i.e., no data is actually captured for that long.
836 * Take the wait here so that the stream is known to be actually
837 * running when the start trigger has been called.
838 */
839 wait_event(ua->alsa_capture_wait,
840 test_bit(CAPTURE_URB_COMPLETED, &ua->states) ||
841 !test_bit(USB_CAPTURE_RUNNING, &ua->states));
842 if (test_bit(DISCONNECTED, &ua->states))
843 return -ENODEV;
844 if (!test_bit(USB_CAPTURE_RUNNING, &ua->states))
845 return -EIO;
846
847 ua->capture.period_pos = 0;
848 ua->capture.buffer_pos = 0;
849 return 0;
850}
851
852static int playback_pcm_prepare(struct snd_pcm_substream *substream)
853{
854 struct ua101 *ua = substream->private_data;
855 int err;
856
857 mutex_lock(&ua->mutex);
858 err = start_usb_capture(ua);
859 if (err >= 0)
860 err = start_usb_playback(ua);
861 mutex_unlock(&ua->mutex);
862 if (err < 0)
863 return err;
864
865 /* see the comment in capture_pcm_prepare() */
866 wait_event(ua->alsa_playback_wait,
867 test_bit(PLAYBACK_URB_COMPLETED, &ua->states) ||
868 !test_bit(USB_PLAYBACK_RUNNING, &ua->states));
869 if (test_bit(DISCONNECTED, &ua->states))
870 return -ENODEV;
871 if (!test_bit(USB_PLAYBACK_RUNNING, &ua->states))
872 return -EIO;
873
874 substream->runtime->delay = 0;
875 ua->playback.period_pos = 0;
876 ua->playback.buffer_pos = 0;
877 return 0;
878}
879
880static int capture_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
881{
882 struct ua101 *ua = substream->private_data;
883
884 switch (cmd) {
885 case SNDRV_PCM_TRIGGER_START:
886 if (!test_bit(USB_CAPTURE_RUNNING, &ua->states))
887 return -EIO;
888 set_bit(ALSA_CAPTURE_RUNNING, &ua->states);
889 return 0;
890 case SNDRV_PCM_TRIGGER_STOP:
891 clear_bit(ALSA_CAPTURE_RUNNING, &ua->states);
892 return 0;
893 default:
894 return -EINVAL;
895 }
896}
897
898static int playback_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
899{
900 struct ua101 *ua = substream->private_data;
901
902 switch (cmd) {
903 case SNDRV_PCM_TRIGGER_START:
904 if (!test_bit(USB_PLAYBACK_RUNNING, &ua->states))
905 return -EIO;
906 set_bit(ALSA_PLAYBACK_RUNNING, &ua->states);
907 return 0;
908 case SNDRV_PCM_TRIGGER_STOP:
909 clear_bit(ALSA_PLAYBACK_RUNNING, &ua->states);
910 return 0;
911 default:
912 return -EINVAL;
913 }
914}
915
916static inline snd_pcm_uframes_t ua101_pcm_pointer(struct ua101 *ua,
917 struct ua101_stream *stream)
918{
919 unsigned long flags;
920 unsigned int pos;
921
922 spin_lock_irqsave(&ua->lock, flags);
923 pos = stream->buffer_pos;
924 spin_unlock_irqrestore(&ua->lock, flags);
925 return pos;
926}
927
928static snd_pcm_uframes_t capture_pcm_pointer(struct snd_pcm_substream *subs)
929{
930 struct ua101 *ua = subs->private_data;
931
932 return ua101_pcm_pointer(ua, &ua->capture);
933}
934
935static snd_pcm_uframes_t playback_pcm_pointer(struct snd_pcm_substream *subs)
936{
937 struct ua101 *ua = subs->private_data;
938
939 return ua101_pcm_pointer(ua, &ua->playback);
940}
941
942static struct snd_pcm_ops capture_pcm_ops = {
943 .open = capture_pcm_open,
944 .close = capture_pcm_close,
945 .ioctl = snd_pcm_lib_ioctl,
946 .hw_params = capture_pcm_hw_params,
947 .hw_free = ua101_pcm_hw_free,
948 .prepare = capture_pcm_prepare,
949 .trigger = capture_pcm_trigger,
950 .pointer = capture_pcm_pointer,
951 .page = snd_pcm_get_vmalloc_page,
952};
953
954static struct snd_pcm_ops playback_pcm_ops = {
955 .open = playback_pcm_open,
956 .close = playback_pcm_close,
957 .ioctl = snd_pcm_lib_ioctl,
958 .hw_params = playback_pcm_hw_params,
959 .hw_free = ua101_pcm_hw_free,
960 .prepare = playback_pcm_prepare,
961 .trigger = playback_pcm_trigger,
962 .pointer = playback_pcm_pointer,
963 .page = snd_pcm_get_vmalloc_page,
964};
965
966static const struct uac_format_type_i_discrete_descriptor *
967find_format_descriptor(struct usb_interface *interface)
968{
969 struct usb_host_interface *alt;
970 u8 *extra;
971 int extralen;
972
973 if (interface->num_altsetting != 2) {
974 dev_err(&interface->dev, "invalid num_altsetting\n");
975 return NULL;
976 }
977
978 alt = &interface->altsetting[0];
979 if (alt->desc.bNumEndpoints != 0) {
980 dev_err(&interface->dev, "invalid bNumEndpoints\n");
981 return NULL;
982 }
983
984 alt = &interface->altsetting[1];
985 if (alt->desc.bNumEndpoints != 1) {
986 dev_err(&interface->dev, "invalid bNumEndpoints\n");
987 return NULL;
988 }
989
990 extra = alt->extra;
991 extralen = alt->extralen;
992 while (extralen >= sizeof(struct usb_descriptor_header)) {
993 struct uac_format_type_i_discrete_descriptor *desc;
994
995 desc = (struct uac_format_type_i_discrete_descriptor *)extra;
996 if (desc->bLength > extralen) {
997 dev_err(&interface->dev, "descriptor overflow\n");
998 return NULL;
999 }
1000 if (desc->bLength == UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1) &&
1001 desc->bDescriptorType == USB_DT_CS_INTERFACE &&
1002 desc->bDescriptorSubtype == UAC_FORMAT_TYPE) {
1003 if (desc->bFormatType != UAC_FORMAT_TYPE_I_PCM ||
1004 desc->bSamFreqType != 1) {
1005 dev_err(&interface->dev,
1006 "invalid format type\n");
1007 return NULL;
1008 }
1009 return desc;
1010 }
1011 extralen -= desc->bLength;
1012 extra += desc->bLength;
1013 }
1014 dev_err(&interface->dev, "sample format descriptor not found\n");
1015 return NULL;
1016}
1017
1018static int detect_usb_format(struct ua101 *ua)
1019{
1020 const struct uac_format_type_i_discrete_descriptor *fmt_capture;
1021 const struct uac_format_type_i_discrete_descriptor *fmt_playback;
1022 const struct usb_endpoint_descriptor *epd;
1023 unsigned int rate2;
1024
1025 fmt_capture = find_format_descriptor(ua->intf[INTF_CAPTURE]);
1026 fmt_playback = find_format_descriptor(ua->intf[INTF_PLAYBACK]);
1027 if (!fmt_capture || !fmt_playback)
1028 return -ENXIO;
1029
1030 switch (fmt_capture->bSubframeSize) {
1031 case 3:
1032 ua->format_bit = SNDRV_PCM_FMTBIT_S24_3LE;
1033 break;
1034 case 4:
1035 ua->format_bit = SNDRV_PCM_FMTBIT_S32_LE;
1036 break;
1037 default:
1038 dev_err(&ua->dev->dev, "sample width is not 24 or 32 bits\n");
1039 return -ENXIO;
1040 }
1041 if (fmt_capture->bSubframeSize != fmt_playback->bSubframeSize) {
1042 dev_err(&ua->dev->dev,
1043 "playback/capture sample widths do not match\n");
1044 return -ENXIO;
1045 }
1046
1047 if (fmt_capture->bBitResolution != 24 ||
1048 fmt_playback->bBitResolution != 24) {
1049 dev_err(&ua->dev->dev, "sample width is not 24 bits\n");
1050 return -ENXIO;
1051 }
1052
1053 ua->rate = combine_triple(fmt_capture->tSamFreq[0]);
1054 rate2 = combine_triple(fmt_playback->tSamFreq[0]);
1055 if (ua->rate != rate2) {
1056 dev_err(&ua->dev->dev,
1057 "playback/capture rates do not match: %u/%u\n",
1058 rate2, ua->rate);
1059 return -ENXIO;
1060 }
1061
1062 switch (ua->dev->speed) {
1063 case USB_SPEED_FULL:
1064 ua->packets_per_second = 1000;
1065 break;
1066 case USB_SPEED_HIGH:
1067 ua->packets_per_second = 8000;
1068 break;
1069 default:
1070 dev_err(&ua->dev->dev, "unknown device speed\n");
1071 return -ENXIO;
1072 }
1073
1074 ua->capture.channels = fmt_capture->bNrChannels;
1075 ua->playback.channels = fmt_playback->bNrChannels;
1076 ua->capture.frame_bytes =
1077 fmt_capture->bSubframeSize * ua->capture.channels;
1078 ua->playback.frame_bytes =
1079 fmt_playback->bSubframeSize * ua->playback.channels;
1080
1081 epd = &ua->intf[INTF_CAPTURE]->altsetting[1].endpoint[0].desc;
1082 if (!usb_endpoint_is_isoc_in(epd)) {
1083 dev_err(&ua->dev->dev, "invalid capture endpoint\n");
1084 return -ENXIO;
1085 }
1086 ua->capture.usb_pipe = usb_rcvisocpipe(ua->dev, usb_endpoint_num(epd));
1087 ua->capture.max_packet_bytes = le16_to_cpu(epd->wMaxPacketSize);
1088
1089 epd = &ua->intf[INTF_PLAYBACK]->altsetting[1].endpoint[0].desc;
1090 if (!usb_endpoint_is_isoc_out(epd)) {
1091 dev_err(&ua->dev->dev, "invalid playback endpoint\n");
1092 return -ENXIO;
1093 }
1094 ua->playback.usb_pipe = usb_sndisocpipe(ua->dev, usb_endpoint_num(epd));
1095 ua->playback.max_packet_bytes = le16_to_cpu(epd->wMaxPacketSize);
1096 return 0;
1097}
1098
1099static int alloc_stream_buffers(struct ua101 *ua, struct ua101_stream *stream)
1100{
1101 unsigned int remaining_packets, packets, packets_per_page, i;
1102 size_t size;
1103
1104 stream->queue_length = queue_length;
1105 stream->queue_length = max(stream->queue_length,
1106 (unsigned int)MIN_QUEUE_LENGTH);
1107 stream->queue_length = min(stream->queue_length,
1108 (unsigned int)MAX_QUEUE_LENGTH);
1109
1110 /*
1111 * The cache pool sizes used by usb_buffer_alloc() (128, 512, 2048) are
1112 * quite bad when used with the packet sizes of this device (e.g. 280,
1113 * 520, 624). Therefore, we allocate and subdivide entire pages, using
1114 * a smaller buffer only for the last chunk.
1115 */
1116 remaining_packets = stream->queue_length;
1117 packets_per_page = PAGE_SIZE / stream->max_packet_bytes;
1118 for (i = 0; i < ARRAY_SIZE(stream->buffers); ++i) {
1119 packets = min(remaining_packets, packets_per_page);
1120 size = packets * stream->max_packet_bytes;
1121 stream->buffers[i].addr =
1122 usb_buffer_alloc(ua->dev, size, GFP_KERNEL,
1123 &stream->buffers[i].dma);
1124 if (!stream->buffers[i].addr)
1125 return -ENOMEM;
1126 stream->buffers[i].size = size;
1127 remaining_packets -= packets;
1128 if (!remaining_packets)
1129 break;
1130 }
1131 if (remaining_packets) {
1132 dev_err(&ua->dev->dev, "too many packets\n");
1133 return -ENXIO;
1134 }
1135 return 0;
1136}
1137
1138static void free_stream_buffers(struct ua101 *ua, struct ua101_stream *stream)
1139{
1140 unsigned int i;
1141
1142 for (i = 0; i < ARRAY_SIZE(stream->buffers); ++i)
1143 usb_buffer_free(ua->dev,
1144 stream->buffers[i].size,
1145 stream->buffers[i].addr,
1146 stream->buffers[i].dma);
1147}
1148
1149static int alloc_stream_urbs(struct ua101 *ua, struct ua101_stream *stream,
1150 void (*urb_complete)(struct urb *))
1151{
1152 unsigned max_packet_size = stream->max_packet_bytes;
1153 struct ua101_urb *urb;
1154 unsigned int b, u = 0;
1155
1156 for (b = 0; b < ARRAY_SIZE(stream->buffers); ++b) {
1157 unsigned int size = stream->buffers[b].size;
1158 u8 *addr = stream->buffers[b].addr;
1159 dma_addr_t dma = stream->buffers[b].dma;
1160
1161 while (size >= max_packet_size) {
1162 if (u >= stream->queue_length)
1163 goto bufsize_error;
1164 urb = kmalloc(sizeof(*urb), GFP_KERNEL);
1165 if (!urb)
1166 return -ENOMEM;
1167 usb_init_urb(&urb->urb);
1168 urb->urb.dev = ua->dev;
1169 urb->urb.pipe = stream->usb_pipe;
1170 urb->urb.transfer_flags = URB_ISO_ASAP |
1171 URB_NO_TRANSFER_DMA_MAP;
1172 urb->urb.transfer_buffer = addr;
1173 urb->urb.transfer_dma = dma;
1174 urb->urb.transfer_buffer_length = max_packet_size;
1175 urb->urb.number_of_packets = 1;
1176 urb->urb.interval = 1;
1177 urb->urb.context = ua;
1178 urb->urb.complete = urb_complete;
1179 urb->urb.iso_frame_desc[0].offset = 0;
1180 urb->urb.iso_frame_desc[0].length = max_packet_size;
1181 stream->urbs[u++] = urb;
1182 size -= max_packet_size;
1183 addr += max_packet_size;
1184 dma += max_packet_size;
1185 }
1186 }
1187 if (u == stream->queue_length)
1188 return 0;
1189bufsize_error:
1190 dev_err(&ua->dev->dev, "internal buffer size error\n");
1191 return -ENXIO;
1192}
1193
1194static void free_stream_urbs(struct ua101_stream *stream)
1195{
1196 unsigned int i;
1197
1198 for (i = 0; i < stream->queue_length; ++i)
1199 kfree(stream->urbs[i]);
1200}
1201
1202static void free_usb_related_resources(struct ua101 *ua,
1203 struct usb_interface *interface)
1204{
1205 unsigned int i;
1206
1207 free_stream_urbs(&ua->capture);
1208 free_stream_urbs(&ua->playback);
1209 free_stream_buffers(ua, &ua->capture);
1210 free_stream_buffers(ua, &ua->playback);
1211
1212 for (i = 0; i < ARRAY_SIZE(ua->intf); ++i)
1213 if (ua->intf[i]) {
1214 usb_set_intfdata(ua->intf[i], NULL);
1215 if (ua->intf[i] != interface)
1216 usb_driver_release_interface(&ua101_driver,
1217 ua->intf[i]);
1218 }
1219}
1220
1221static void ua101_card_free(struct snd_card *card)
1222{
1223 struct ua101 *ua = card->private_data;
1224
1225 mutex_destroy(&ua->mutex);
1226}
1227
1228static int ua101_probe(struct usb_interface *interface,
1229 const struct usb_device_id *usb_id)
1230{
1231 static const struct snd_usb_midi_endpoint_info midi_ep = {
1232 .out_cables = 0x0001,
1233 .in_cables = 0x0001
1234 };
1235 static const struct snd_usb_audio_quirk midi_quirk = {
1236 .type = QUIRK_MIDI_FIXED_ENDPOINT,
1237 .data = &midi_ep
1238 };
1239 struct snd_card *card;
1240 struct ua101 *ua;
1241 unsigned int card_index, i;
1242 char usb_path[32];
1243 int err;
1244
1245 if (interface->altsetting->desc.bInterfaceNumber != 0)
1246 return -ENODEV;
1247
1248 mutex_lock(&devices_mutex);
1249
1250 for (card_index = 0; card_index < SNDRV_CARDS; ++card_index)
1251 if (enable[card_index] && !(devices_used & (1 << card_index)))
1252 break;
1253 if (card_index >= SNDRV_CARDS) {
1254 mutex_unlock(&devices_mutex);
1255 return -ENOENT;
1256 }
1257 err = snd_card_create(index[card_index], id[card_index], THIS_MODULE,
1258 sizeof(*ua), &card);
1259 if (err < 0) {
1260 mutex_unlock(&devices_mutex);
1261 return err;
1262 }
1263 card->private_free = ua101_card_free;
1264 ua = card->private_data;
1265 ua->dev = interface_to_usbdev(interface);
1266 ua->card = card;
1267 ua->card_index = card_index;
1268 INIT_LIST_HEAD(&ua->midi_list);
1269 spin_lock_init(&ua->lock);
1270 mutex_init(&ua->mutex);
1271 INIT_LIST_HEAD(&ua->ready_playback_urbs);
1272 tasklet_init(&ua->playback_tasklet,
1273 playback_tasklet, (unsigned long)ua);
1274 init_waitqueue_head(&ua->alsa_capture_wait);
1275 init_waitqueue_head(&ua->rate_feedback_wait);
1276 init_waitqueue_head(&ua->alsa_playback_wait);
1277
1278#ifdef UA1A_HACK
1279 if (ua->dev->descriptor.idProduct == cpu_to_le16(0x0018)) {
1280 ua->intf[2] = interface;
1281 ua->intf[0] = usb_ifnum_to_if(ua->dev, 1);
1282 ua->intf[1] = usb_ifnum_to_if(ua->dev, 2);
1283 usb_driver_claim_interface(&ua101_driver, ua->intf[0], ua);
1284 usb_driver_claim_interface(&ua101_driver, ua->intf[1], ua);
1285 } else {
1286#endif
1287 ua->intf[0] = interface;
1288 for (i = 1; i < ARRAY_SIZE(ua->intf); ++i) {
1289 ua->intf[i] = usb_ifnum_to_if(ua->dev, i);
1290 if (!ua->intf[i]) {
1291 dev_err(&ua->dev->dev, "interface %u not found\n", i);
1292 err = -ENXIO;
1293 goto probe_error;
1294 }
1295 err = usb_driver_claim_interface(&ua101_driver,
1296 ua->intf[i], ua);
1297 if (err < 0) {
1298 ua->intf[i] = NULL;
1299 err = -EBUSY;
1300 goto probe_error;
1301 }
1302 }
1303#ifdef UA1A_HACK
1304 }
1305#endif
1306
1307 snd_card_set_dev(card, &interface->dev);
1308
1309#ifdef UA1A_HACK
1310 if (ua->dev->descriptor.idProduct == cpu_to_le16(0x0018)) {
1311 ua->format_bit = SNDRV_PCM_FMTBIT_S16_LE;
1312 ua->rate = 44100;
1313 ua->packets_per_second = 1000;
1314 ua->capture.channels = 2;
1315 ua->playback.channels = 2;
1316 ua->capture.frame_bytes = 4;
1317 ua->playback.frame_bytes = 4;
1318 ua->capture.usb_pipe = usb_rcvisocpipe(ua->dev, 2);
1319 ua->playback.usb_pipe = usb_sndisocpipe(ua->dev, 1);
1320 ua->capture.max_packet_bytes = 192;
1321 ua->playback.max_packet_bytes = 192;
1322 } else {
1323#endif
1324 err = detect_usb_format(ua);
1325 if (err < 0)
1326 goto probe_error;
1327#ifdef UA1A_HACK
1328 }
1329#endif
1330
1331 strcpy(card->driver, "UA-101");
1332 strcpy(card->shortname, "UA-101");
1333 usb_make_path(ua->dev, usb_path, sizeof(usb_path));
1334 snprintf(ua->card->longname, sizeof(ua->card->longname),
1335 "EDIROL UA-101 (serial %s), %u Hz at %s, %s speed",
1336 ua->dev->serial ? ua->dev->serial : "?", ua->rate, usb_path,
1337 ua->dev->speed == USB_SPEED_HIGH ? "high" : "full");
1338
1339 err = alloc_stream_buffers(ua, &ua->capture);
1340 if (err < 0)
1341 goto probe_error;
1342 err = alloc_stream_buffers(ua, &ua->playback);
1343 if (err < 0)
1344 goto probe_error;
1345
1346 err = alloc_stream_urbs(ua, &ua->capture, capture_urb_complete);
1347 if (err < 0)
1348 goto probe_error;
1349 err = alloc_stream_urbs(ua, &ua->playback, playback_urb_complete);
1350 if (err < 0)
1351 goto probe_error;
1352
1353 err = snd_pcm_new(card, "UA-101", 0, 1, 1, &ua->pcm);
1354 if (err < 0)
1355 goto probe_error;
1356 ua->pcm->private_data = ua;
1357 strcpy(ua->pcm->name, "UA-101");
1358 snd_pcm_set_ops(ua->pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_pcm_ops);
1359 snd_pcm_set_ops(ua->pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_pcm_ops);
1360
1361#ifdef UA1A_HACK
1362 if (ua->dev->descriptor.idProduct != cpu_to_le16(0x0018)) {
1363#endif
1364 err = snd_usbmidi_create(card, ua->intf[INTF_MIDI],
1365 &ua->midi_list, &midi_quirk);
1366 if (err < 0)
1367 goto probe_error;
1368#ifdef UA1A_HACK
1369 }
1370#endif
1371
1372 err = snd_card_register(card);
1373 if (err < 0)
1374 goto probe_error;
1375
1376 usb_set_intfdata(interface, ua);
1377 devices_used |= 1 << card_index;
1378
1379 mutex_unlock(&devices_mutex);
1380 return 0;
1381
1382probe_error:
1383 free_usb_related_resources(ua, interface);
1384 snd_card_free(card);
1385 mutex_unlock(&devices_mutex);
1386 return err;
1387}
1388
1389static void ua101_disconnect(struct usb_interface *interface)
1390{
1391 struct ua101 *ua = usb_get_intfdata(interface);
1392 struct list_head *midi;
1393
1394 if (!ua)
1395 return;
1396
1397 mutex_lock(&devices_mutex);
1398
1399 set_bit(DISCONNECTED, &ua->states);
1400 wake_up(&ua->rate_feedback_wait);
1401
1402 /* make sure that userspace cannot create new requests */
1403 snd_card_disconnect(ua->card);
1404
1405 /* make sure that there are no pending USB requests */
1406 __list_for_each(midi, &ua->midi_list)
1407 snd_usbmidi_disconnect(midi);
1408 abort_alsa_playback(ua);
1409 abort_alsa_capture(ua);
1410 mutex_lock(&ua->mutex);
1411 stop_usb_playback(ua);
1412 stop_usb_capture(ua);
1413 mutex_unlock(&ua->mutex);
1414
1415 free_usb_related_resources(ua, interface);
1416
1417 devices_used &= ~(1 << ua->card_index);
1418
1419 snd_card_free_when_closed(ua->card);
1420
1421 mutex_unlock(&devices_mutex);
1422}
1423
1424static struct usb_device_id ua101_ids[] = {
1425#ifdef UA1A_HACK
1426 { USB_DEVICE(0x0582, 0x0018) },
1427#endif
1428 { USB_DEVICE(0x0582, 0x007d) },
1429 { USB_DEVICE(0x0582, 0x008d) },
1430 { }
1431};
1432MODULE_DEVICE_TABLE(usb, ua101_ids);
1433
1434static struct usb_driver ua101_driver = {
1435 .name = "snd-ua101",
1436 .id_table = ua101_ids,
1437 .probe = ua101_probe,
1438 .disconnect = ua101_disconnect,
1439#if 0
1440 .suspend = ua101_suspend,
1441 .resume = ua101_resume,
1442#endif
1443};
1444
1445static int __init alsa_card_ua101_init(void)
1446{
1447 return usb_register(&ua101_driver);
1448}
1449
1450static void __exit alsa_card_ua101_exit(void)
1451{
1452 usb_deregister(&ua101_driver);
1453 mutex_destroy(&devices_mutex);
1454}
1455
1456module_init(alsa_card_ua101_init);
1457module_exit(alsa_card_ua101_exit);
diff --git a/sound/usb/usbaudio.c b/sound/usb/usbaudio.c
index b074a594c595..f352141cf8ee 100644
--- a/sound/usb/usbaudio.c
+++ b/sound/usb/usbaudio.c
@@ -3142,59 +3142,6 @@ static int create_ua1000_quirk(struct snd_usb_audio *chip,
3142 return 0; 3142 return 0;
3143} 3143}
3144 3144
3145/*
3146 * Create a stream for an Edirol UA-101 interface.
3147 * Copy, paste and modify from Edirol UA-1000
3148 */
3149static int create_ua101_quirk(struct snd_usb_audio *chip,
3150 struct usb_interface *iface,
3151 const struct snd_usb_audio_quirk *quirk)
3152{
3153 static const struct audioformat ua101_format = {
3154 .format = SNDRV_PCM_FORMAT_S32_LE,
3155 .fmt_type = USB_FORMAT_TYPE_I,
3156 .altsetting = 1,
3157 .altset_idx = 1,
3158 .attributes = 0,
3159 .rates = SNDRV_PCM_RATE_CONTINUOUS,
3160 };
3161 struct usb_host_interface *alts;
3162 struct usb_interface_descriptor *altsd;
3163 struct audioformat *fp;
3164 int stream, err;
3165
3166 if (iface->num_altsetting != 2)
3167 return -ENXIO;
3168 alts = &iface->altsetting[1];
3169 altsd = get_iface_desc(alts);
3170 if (alts->extralen != 18 || alts->extra[1] != USB_DT_CS_INTERFACE ||
3171 altsd->bNumEndpoints != 1)
3172 return -ENXIO;
3173
3174 fp = kmemdup(&ua101_format, sizeof(*fp), GFP_KERNEL);
3175 if (!fp)
3176 return -ENOMEM;
3177
3178 fp->channels = alts->extra[11];
3179 fp->iface = altsd->bInterfaceNumber;
3180 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
3181 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
3182 fp->datainterval = parse_datainterval(chip, alts);
3183 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
3184 fp->rate_max = fp->rate_min = combine_triple(&alts->extra[15]);
3185
3186 stream = (fp->endpoint & USB_DIR_IN)
3187 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
3188 err = add_audio_endpoint(chip, stream, fp);
3189 if (err < 0) {
3190 kfree(fp);
3191 return err;
3192 }
3193 /* FIXME: playback must be synchronized to capture */
3194 usb_set_interface(chip->dev, fp->iface, 0);
3195 return 0;
3196}
3197
3198static int snd_usb_create_quirk(struct snd_usb_audio *chip, 3145static int snd_usb_create_quirk(struct snd_usb_audio *chip,
3199 struct usb_interface *iface, 3146 struct usb_interface *iface,
3200 const struct snd_usb_audio_quirk *quirk); 3147 const struct snd_usb_audio_quirk *quirk);
@@ -3406,7 +3353,6 @@ static int snd_usb_create_quirk(struct snd_usb_audio *chip,
3406 [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk, 3353 [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
3407 [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk, 3354 [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
3408 [QUIRK_AUDIO_EDIROL_UA1000] = create_ua1000_quirk, 3355 [QUIRK_AUDIO_EDIROL_UA1000] = create_ua1000_quirk,
3409 [QUIRK_AUDIO_EDIROL_UA101] = create_ua101_quirk,
3410 [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk 3356 [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk
3411 }; 3357 };
3412 3358
diff --git a/sound/usb/usbaudio.h b/sound/usb/usbaudio.h
index 40ba8115fb81..9826337c76b8 100644
--- a/sound/usb/usbaudio.h
+++ b/sound/usb/usbaudio.h
@@ -159,7 +159,6 @@ enum quirk_type {
159 QUIRK_AUDIO_STANDARD_INTERFACE, 159 QUIRK_AUDIO_STANDARD_INTERFACE,
160 QUIRK_AUDIO_FIXED_ENDPOINT, 160 QUIRK_AUDIO_FIXED_ENDPOINT,
161 QUIRK_AUDIO_EDIROL_UA1000, 161 QUIRK_AUDIO_EDIROL_UA1000,
162 QUIRK_AUDIO_EDIROL_UA101,
163 QUIRK_AUDIO_EDIROL_UAXX, 162 QUIRK_AUDIO_EDIROL_UAXX,
164 163
165 QUIRK_TYPE_COUNT 164 QUIRK_TYPE_COUNT
diff --git a/sound/usb/usbquirks.h b/sound/usb/usbquirks.h
index a892bda03df9..bd6706c2d534 100644
--- a/sound/usb/usbquirks.h
+++ b/sound/usb/usbquirks.h
@@ -1266,37 +1266,6 @@ YAMAHA_DEVICE(0x7010, "UB99"),
1266 } 1266 }
1267 } 1267 }
1268}, 1268},
1269/* Roland UA-101 in High-Speed Mode only */
1270{
1271 USB_DEVICE(0x0582, 0x007d),
1272 .driver_info = (unsigned long) & (const struct snd_usb_audio_quirk) {
1273 .vendor_name = "Roland",
1274 .product_name = "UA-101",
1275 .ifnum = QUIRK_ANY_INTERFACE,
1276 .type = QUIRK_COMPOSITE,
1277 .data = (const struct snd_usb_audio_quirk[]) {
1278 {
1279 .ifnum = 0,
1280 .type = QUIRK_AUDIO_EDIROL_UA101
1281 },
1282 {
1283 .ifnum = 1,
1284 .type = QUIRK_AUDIO_EDIROL_UA101
1285 },
1286 {
1287 .ifnum = 2,
1288 .type = QUIRK_MIDI_FIXED_ENDPOINT,
1289 .data = & (const struct snd_usb_midi_endpoint_info) {
1290 .out_cables = 0x0001,
1291 .in_cables = 0x0001
1292 }
1293 },
1294 {
1295 .ifnum = -1
1296 }
1297 }
1298 }
1299},
1300{ 1269{
1301 /* has ID 0x0081 when not in "Advanced Driver" mode */ 1270 /* has ID 0x0081 when not in "Advanced Driver" mode */
1302 USB_DEVICE(0x0582, 0x0080), 1271 USB_DEVICE(0x0582, 0x0080),