ALSA: ua101: add Edirol UA-1000 support
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / usb / ua101.c
blob047dc1ca84d04f1c7ba502f43d05c187293209fb
1 /*
2 * Edirol UA-101/UA-1000 driver
3 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
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.
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.
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/>.
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 <sound/core.h>
23 #include <sound/initval.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include "usbaudio.h"
28 MODULE_DESCRIPTION("Edirol UA-101/1000 driver");
29 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
30 MODULE_LICENSE("GPL v2");
31 MODULE_SUPPORTED_DEVICE("{{Edirol,UA-101},{Edirol,UA-1000}}");
33 /* I use my UA-1A for testing because I don't have a UA-101 ... */
34 #define UA1A_HACK
37 * Should not be lower than the minimum scheduling delay of the host
38 * controller. Some Intel controllers need more than one frame; as long as
39 * that driver doesn't tell us about this, use 1.5 frames just to be sure.
41 #define MIN_QUEUE_LENGTH 12
42 /* Somewhat random. */
43 #define MAX_QUEUE_LENGTH 30
45 * This magic value optimizes memory usage efficiency for the UA-101's packet
46 * sizes at all sample rates, taking into account the stupid cache pool sizes
47 * that usb_buffer_alloc() uses.
49 #define DEFAULT_QUEUE_LENGTH 21
51 #define MAX_PACKET_SIZE 672 /* hardware specific */
52 #define MAX_MEMORY_BUFFERS DIV_ROUND_UP(MAX_QUEUE_LENGTH, \
53 PAGE_SIZE / MAX_PACKET_SIZE)
55 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
56 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
57 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
58 static unsigned int queue_length = 21;
60 module_param_array(index, int, NULL, 0444);
61 MODULE_PARM_DESC(index, "card index");
62 module_param_array(id, charp, NULL, 0444);
63 MODULE_PARM_DESC(id, "ID string");
64 module_param_array(enable, bool, NULL, 0444);
65 MODULE_PARM_DESC(enable, "enable card");
66 module_param(queue_length, uint, 0644);
67 MODULE_PARM_DESC(queue_length, "USB queue length in microframes, "
68 __stringify(MIN_QUEUE_LENGTH)"-"__stringify(MAX_QUEUE_LENGTH));
70 enum {
71 INTF_PLAYBACK,
72 INTF_CAPTURE,
73 INTF_MIDI,
75 INTF_COUNT
78 /* bits in struct ua101::states */
79 enum {
80 USB_CAPTURE_RUNNING,
81 USB_PLAYBACK_RUNNING,
82 ALSA_CAPTURE_OPEN,
83 ALSA_PLAYBACK_OPEN,
84 ALSA_CAPTURE_RUNNING,
85 ALSA_PLAYBACK_RUNNING,
86 CAPTURE_URB_COMPLETED,
87 PLAYBACK_URB_COMPLETED,
88 DISCONNECTED,
91 struct ua101 {
92 struct usb_device *dev;
93 struct snd_card *card;
94 struct usb_interface *intf[INTF_COUNT];
95 int card_index;
96 struct snd_pcm *pcm;
97 struct list_head midi_list;
98 u64 format_bit;
99 unsigned int rate;
100 unsigned int packets_per_second;
101 spinlock_t lock;
102 struct mutex mutex;
103 unsigned long states;
105 /* FIFO to synchronize playback rate to capture rate */
106 unsigned int rate_feedback_start;
107 unsigned int rate_feedback_count;
108 u8 rate_feedback[MAX_QUEUE_LENGTH];
110 struct list_head ready_playback_urbs;
111 struct tasklet_struct playback_tasklet;
112 wait_queue_head_t alsa_capture_wait;
113 wait_queue_head_t rate_feedback_wait;
114 wait_queue_head_t alsa_playback_wait;
115 struct ua101_stream {
116 struct snd_pcm_substream *substream;
117 unsigned int usb_pipe;
118 unsigned int channels;
119 unsigned int frame_bytes;
120 unsigned int max_packet_bytes;
121 unsigned int period_pos;
122 unsigned int buffer_pos;
123 unsigned int queue_length;
124 struct ua101_urb {
125 struct urb urb;
126 struct usb_iso_packet_descriptor iso_frame_desc[1];
127 struct list_head ready_list;
128 } *urbs[MAX_QUEUE_LENGTH];
129 struct {
130 unsigned int size;
131 void *addr;
132 dma_addr_t dma;
133 } buffers[MAX_MEMORY_BUFFERS];
134 } capture, playback;
136 unsigned int fps[10];
137 unsigned int frame_counter;
140 static DEFINE_MUTEX(devices_mutex);
141 static unsigned int devices_used;
142 static struct usb_driver ua101_driver;
144 static void abort_alsa_playback(struct ua101 *ua);
145 static void abort_alsa_capture(struct ua101 *ua);
147 static const char *usb_error_string(int err)
149 switch (err) {
150 case -ENODEV:
151 return "no device";
152 case -ENOENT:
153 return "endpoint not enabled";
154 case -EPIPE:
155 return "endpoint stalled";
156 case -ENOSPC:
157 return "not enough bandwidth";
158 case -ESHUTDOWN:
159 return "device disabled";
160 case -EHOSTUNREACH:
161 return "device suspended";
162 case -EINVAL:
163 case -EAGAIN:
164 case -EFBIG:
165 case -EMSGSIZE:
166 return "internal error";
167 default:
168 return "unknown error";
172 static void abort_usb_capture(struct ua101 *ua)
174 if (test_and_clear_bit(USB_CAPTURE_RUNNING, &ua->states)) {
175 wake_up(&ua->alsa_capture_wait);
176 wake_up(&ua->rate_feedback_wait);
180 static void abort_usb_playback(struct ua101 *ua)
182 if (test_and_clear_bit(USB_PLAYBACK_RUNNING, &ua->states))
183 wake_up(&ua->alsa_playback_wait);
186 static void playback_urb_complete(struct urb *usb_urb)
188 struct ua101_urb *urb = (struct ua101_urb *)usb_urb;
189 struct ua101 *ua = urb->urb.context;
190 unsigned long flags;
192 if (unlikely(urb->urb.status == -ENOENT || /* unlinked */
193 urb->urb.status == -ENODEV || /* device removed */
194 urb->urb.status == -ECONNRESET || /* unlinked */
195 urb->urb.status == -ESHUTDOWN)) { /* device disabled */
196 abort_usb_playback(ua);
197 abort_alsa_playback(ua);
198 return;
201 if (test_bit(USB_PLAYBACK_RUNNING, &ua->states)) {
202 /* append URB to FIFO */
203 spin_lock_irqsave(&ua->lock, flags);
204 list_add_tail(&urb->ready_list, &ua->ready_playback_urbs);
205 if (ua->rate_feedback_count > 0)
206 tasklet_schedule(&ua->playback_tasklet);
207 ua->playback.substream->runtime->delay -=
208 urb->urb.iso_frame_desc[0].length /
209 ua->playback.frame_bytes;
210 spin_unlock_irqrestore(&ua->lock, flags);
214 static void first_playback_urb_complete(struct urb *urb)
216 struct ua101 *ua = urb->context;
218 urb->complete = playback_urb_complete;
219 playback_urb_complete(urb);
221 set_bit(PLAYBACK_URB_COMPLETED, &ua->states);
222 wake_up(&ua->alsa_playback_wait);
225 /* copy data from the ALSA ring buffer into the URB buffer */
226 static bool copy_playback_data(struct ua101_stream *stream, struct urb *urb,
227 unsigned int frames)
229 struct snd_pcm_runtime *runtime;
230 unsigned int frame_bytes, frames1;
231 const u8 *source;
233 runtime = stream->substream->runtime;
234 frame_bytes = stream->frame_bytes;
235 source = runtime->dma_area + stream->buffer_pos * frame_bytes;
236 if (stream->buffer_pos + frames <= runtime->buffer_size) {
237 memcpy(urb->transfer_buffer, source, frames * frame_bytes);
238 } else {
239 /* wrap around at end of ring buffer */
240 frames1 = runtime->buffer_size - stream->buffer_pos;
241 memcpy(urb->transfer_buffer, source, frames1 * frame_bytes);
242 memcpy(urb->transfer_buffer + frames1 * frame_bytes,
243 runtime->dma_area, (frames - frames1) * frame_bytes);
246 stream->buffer_pos += frames;
247 if (stream->buffer_pos >= runtime->buffer_size)
248 stream->buffer_pos -= runtime->buffer_size;
249 stream->period_pos += frames;
250 if (stream->period_pos >= runtime->period_size) {
251 stream->period_pos -= runtime->period_size;
252 return true;
254 return false;
257 static inline void add_with_wraparound(struct ua101 *ua,
258 unsigned int *value, unsigned int add)
260 *value += add;
261 if (*value >= ua->playback.queue_length)
262 *value -= ua->playback.queue_length;
265 static void playback_tasklet(unsigned long data)
267 struct ua101 *ua = (void *)data;
268 unsigned long flags;
269 unsigned int frames;
270 struct ua101_urb *urb;
271 bool do_period_elapsed = false;
272 int err;
274 if (unlikely(!test_bit(USB_PLAYBACK_RUNNING, &ua->states)))
275 return;
278 * Synchronizing the playback rate to the capture rate is done by using
279 * the same sequence of packet sizes for both streams.
280 * Submitting a playback URB therefore requires both a ready URB and
281 * the size of the corresponding capture packet, i.e., both playback
282 * and capture URBs must have been completed. Since the USB core does
283 * not guarantee that playback and capture complete callbacks are
284 * called alternately, we use two FIFOs for packet sizes and read URBs;
285 * submitting playback URBs is possible as long as both FIFOs are
286 * nonempty.
288 spin_lock_irqsave(&ua->lock, flags);
289 while (ua->rate_feedback_count > 0 &&
290 !list_empty(&ua->ready_playback_urbs)) {
291 /* take packet size out of FIFO */
292 frames = ua->rate_feedback[ua->rate_feedback_start];
293 add_with_wraparound(ua, &ua->rate_feedback_start, 1);
294 ua->rate_feedback_count--;
296 /* take URB out of FIFO */
297 urb = list_first_entry(&ua->ready_playback_urbs,
298 struct ua101_urb, ready_list);
299 list_del(&urb->ready_list);
301 /* fill packet with data or silence */
302 urb->urb.iso_frame_desc[0].length =
303 frames * ua->playback.frame_bytes;
304 if (test_bit(ALSA_PLAYBACK_RUNNING, &ua->states))
305 do_period_elapsed |= copy_playback_data(&ua->playback,
306 &urb->urb,
307 frames);
308 else
309 memset(urb->urb.transfer_buffer, 0,
310 urb->urb.iso_frame_desc[0].length);
312 /* and off you go ... */
313 err = usb_submit_urb(&urb->urb, GFP_ATOMIC);
314 if (unlikely(err < 0)) {
315 spin_unlock_irqrestore(&ua->lock, flags);
316 abort_usb_playback(ua);
317 abort_alsa_playback(ua);
318 dev_err(&ua->dev->dev, "USB request error %d: %s\n",
319 err, usb_error_string(err));
320 return;
322 ua->playback.substream->runtime->delay += frames;
324 spin_unlock_irqrestore(&ua->lock, flags);
325 if (do_period_elapsed)
326 snd_pcm_period_elapsed(ua->playback.substream);
329 /* copy data from the URB buffer into the ALSA ring buffer */
330 static bool copy_capture_data(struct ua101_stream *stream, struct urb *urb,
331 unsigned int frames)
333 struct snd_pcm_runtime *runtime;
334 unsigned int frame_bytes, frames1;
335 u8 *dest;
337 runtime = stream->substream->runtime;
338 frame_bytes = stream->frame_bytes;
339 dest = runtime->dma_area + stream->buffer_pos * frame_bytes;
340 if (stream->buffer_pos + frames <= runtime->buffer_size) {
341 memcpy(dest, urb->transfer_buffer, frames * frame_bytes);
342 } else {
343 /* wrap around at end of ring buffer */
344 frames1 = runtime->buffer_size - stream->buffer_pos;
345 memcpy(dest, urb->transfer_buffer, frames1 * frame_bytes);
346 memcpy(runtime->dma_area,
347 urb->transfer_buffer + frames1 * frame_bytes,
348 (frames - frames1) * frame_bytes);
351 stream->buffer_pos += frames;
352 if (stream->buffer_pos >= runtime->buffer_size)
353 stream->buffer_pos -= runtime->buffer_size;
354 stream->period_pos += frames;
355 if (stream->period_pos >= runtime->period_size) {
356 stream->period_pos -= runtime->period_size;
357 return true;
359 return false;
362 static void capture_urb_complete(struct urb *urb)
364 struct ua101 *ua = urb->context;
365 struct ua101_stream *stream = &ua->capture;
366 unsigned long flags;
367 unsigned int frames, write_ptr;
368 bool do_period_elapsed;
369 int err;
371 if (unlikely(urb->status == -ENOENT || /* unlinked */
372 urb->status == -ENODEV || /* device removed */
373 urb->status == -ECONNRESET || /* unlinked */
374 urb->status == -ESHUTDOWN)) /* device disabled */
375 goto stream_stopped;
377 if (urb->status >= 0 && urb->iso_frame_desc[0].status >= 0)
378 frames = urb->iso_frame_desc[0].actual_length /
379 stream->frame_bytes;
380 else
381 frames = 0;
383 spin_lock_irqsave(&ua->lock, flags);
385 if (frames > 0 && test_bit(ALSA_CAPTURE_RUNNING, &ua->states))
386 do_period_elapsed = copy_capture_data(stream, urb, frames);
387 else
388 do_period_elapsed = false;
390 if (test_bit(USB_CAPTURE_RUNNING, &ua->states)) {
391 err = usb_submit_urb(urb, GFP_ATOMIC);
392 if (unlikely(err < 0)) {
393 spin_unlock_irqrestore(&ua->lock, flags);
394 dev_err(&ua->dev->dev, "USB request error %d: %s\n",
395 err, usb_error_string(err));
396 goto stream_stopped;
399 /* append packet size to FIFO */
400 write_ptr = ua->rate_feedback_start;
401 add_with_wraparound(ua, &write_ptr, ua->rate_feedback_count);
402 ua->rate_feedback[write_ptr] = frames;
403 if (ua->rate_feedback_count < ua->playback.queue_length) {
404 ua->rate_feedback_count++;
405 if (ua->rate_feedback_count ==
406 ua->playback.queue_length)
407 wake_up(&ua->rate_feedback_wait);
408 } else {
410 * Ring buffer overflow; this happens when the playback
411 * stream is not running. Throw away the oldest entry,
412 * so that the playback stream, when it starts, sees
413 * the most recent packet sizes.
415 add_with_wraparound(ua, &ua->rate_feedback_start, 1);
417 if (test_bit(USB_PLAYBACK_RUNNING, &ua->states) &&
418 !list_empty(&ua->ready_playback_urbs))
419 tasklet_schedule(&ua->playback_tasklet);
422 spin_unlock_irqrestore(&ua->lock, flags);
424 if (do_period_elapsed)
425 snd_pcm_period_elapsed(stream->substream);
427 /* for debugging: measure the sample rate relative to the USB clock */
428 ua->fps[ua->frame_counter++ / ua->packets_per_second] += frames;
429 if (ua->frame_counter >= ARRAY_SIZE(ua->fps) * ua->packets_per_second) {
430 printk(KERN_DEBUG "capture rate:");
431 for (frames = 0; frames < ARRAY_SIZE(ua->fps); ++frames)
432 printk(KERN_CONT " %u", ua->fps[frames]);
433 printk(KERN_CONT "\n");
434 memset(ua->fps, 0, sizeof(ua->fps));
435 ua->frame_counter = 0;
437 return;
439 stream_stopped:
440 abort_usb_playback(ua);
441 abort_usb_capture(ua);
442 abort_alsa_playback(ua);
443 abort_alsa_capture(ua);
446 static void first_capture_urb_complete(struct urb *urb)
448 struct ua101 *ua = urb->context;
450 urb->complete = capture_urb_complete;
451 capture_urb_complete(urb);
453 set_bit(CAPTURE_URB_COMPLETED, &ua->states);
454 wake_up(&ua->alsa_capture_wait);
457 static int submit_stream_urbs(struct ua101 *ua, struct ua101_stream *stream)
459 unsigned int i;
461 for (i = 0; i < stream->queue_length; ++i) {
462 int err = usb_submit_urb(&stream->urbs[i]->urb, GFP_KERNEL);
463 if (err < 0) {
464 dev_err(&ua->dev->dev, "USB request error %d: %s\n",
465 err, usb_error_string(err));
466 return err;
469 return 0;
472 static void kill_stream_urbs(struct ua101_stream *stream)
474 unsigned int i;
476 for (i = 0; i < stream->queue_length; ++i)
477 usb_kill_urb(&stream->urbs[i]->urb);
480 static int enable_iso_interface(struct ua101 *ua, unsigned int intf_index)
482 struct usb_host_interface *alts;
484 alts = ua->intf[intf_index]->cur_altsetting;
485 if (alts->desc.bAlternateSetting != 1) {
486 int err = usb_set_interface(ua->dev,
487 alts->desc.bInterfaceNumber, 1);
488 if (err < 0) {
489 dev_err(&ua->dev->dev,
490 "cannot initialize interface; error %d: %s\n",
491 err, usb_error_string(err));
492 return err;
495 return 0;
498 static void disable_iso_interface(struct ua101 *ua, unsigned int intf_index)
500 struct usb_host_interface *alts;
502 alts = ua->intf[intf_index]->cur_altsetting;
503 if (alts->desc.bAlternateSetting != 0) {
504 int err = usb_set_interface(ua->dev,
505 alts->desc.bInterfaceNumber, 0);
506 if (err < 0 && !test_bit(DISCONNECTED, &ua->states))
507 dev_warn(&ua->dev->dev,
508 "interface reset failed; error %d: %s\n",
509 err, usb_error_string(err));
513 static void stop_usb_capture(struct ua101 *ua)
515 clear_bit(USB_CAPTURE_RUNNING, &ua->states);
517 kill_stream_urbs(&ua->capture);
519 disable_iso_interface(ua, INTF_CAPTURE);
522 static int start_usb_capture(struct ua101 *ua)
524 int err;
526 if (test_bit(DISCONNECTED, &ua->states))
527 return -ENODEV;
529 if (test_bit(USB_CAPTURE_RUNNING, &ua->states))
530 return 0;
532 kill_stream_urbs(&ua->capture);
534 err = enable_iso_interface(ua, INTF_CAPTURE);
535 if (err < 0)
536 return err;
538 clear_bit(CAPTURE_URB_COMPLETED, &ua->states);
539 ua->capture.urbs[0]->urb.complete = first_capture_urb_complete;
540 ua->rate_feedback_start = 0;
541 ua->rate_feedback_count = 0;
543 set_bit(USB_CAPTURE_RUNNING, &ua->states);
544 err = submit_stream_urbs(ua, &ua->capture);
545 if (err < 0)
546 stop_usb_capture(ua);
547 return err;
550 static void stop_usb_playback(struct ua101 *ua)
552 clear_bit(USB_PLAYBACK_RUNNING, &ua->states);
554 kill_stream_urbs(&ua->playback);
556 tasklet_kill(&ua->playback_tasklet);
558 disable_iso_interface(ua, INTF_PLAYBACK);
561 static int start_usb_playback(struct ua101 *ua)
563 unsigned int i, frames;
564 struct urb *urb;
565 int err = 0;
567 if (test_bit(DISCONNECTED, &ua->states))
568 return -ENODEV;
570 if (test_bit(USB_PLAYBACK_RUNNING, &ua->states))
571 return 0;
573 kill_stream_urbs(&ua->playback);
574 tasklet_kill(&ua->playback_tasklet);
576 err = enable_iso_interface(ua, INTF_PLAYBACK);
577 if (err < 0)
578 return err;
580 clear_bit(PLAYBACK_URB_COMPLETED, &ua->states);
581 ua->playback.urbs[0]->urb.complete =
582 first_playback_urb_complete;
583 spin_lock_irq(&ua->lock);
584 INIT_LIST_HEAD(&ua->ready_playback_urbs);
585 spin_unlock_irq(&ua->lock);
588 * We submit the initial URBs all at once, so we have to wait for the
589 * packet size FIFO to be full.
591 wait_event(ua->rate_feedback_wait,
592 ua->rate_feedback_count >= ua->playback.queue_length ||
593 !test_bit(USB_CAPTURE_RUNNING, &ua->states) ||
594 test_bit(DISCONNECTED, &ua->states));
595 if (test_bit(DISCONNECTED, &ua->states)) {
596 stop_usb_playback(ua);
597 return -ENODEV;
599 if (!test_bit(USB_CAPTURE_RUNNING, &ua->states)) {
600 stop_usb_playback(ua);
601 return -EIO;
604 for (i = 0; i < ua->playback.queue_length; ++i) {
605 /* all initial URBs contain silence */
606 spin_lock_irq(&ua->lock);
607 frames = ua->rate_feedback[ua->rate_feedback_start];
608 add_with_wraparound(ua, &ua->rate_feedback_start, 1);
609 ua->rate_feedback_count--;
610 spin_unlock_irq(&ua->lock);
611 urb = &ua->playback.urbs[i]->urb;
612 urb->iso_frame_desc[0].length =
613 frames * ua->playback.frame_bytes;
614 memset(urb->transfer_buffer, 0,
615 urb->iso_frame_desc[0].length);
618 set_bit(USB_PLAYBACK_RUNNING, &ua->states);
619 err = submit_stream_urbs(ua, &ua->playback);
620 if (err < 0)
621 stop_usb_playback(ua);
622 return err;
625 static void abort_alsa_capture(struct ua101 *ua)
627 if (test_bit(ALSA_CAPTURE_RUNNING, &ua->states))
628 snd_pcm_stop(ua->capture.substream, SNDRV_PCM_STATE_XRUN);
631 static void abort_alsa_playback(struct ua101 *ua)
633 if (test_bit(ALSA_PLAYBACK_RUNNING, &ua->states))
634 snd_pcm_stop(ua->playback.substream, SNDRV_PCM_STATE_XRUN);
637 static int set_stream_hw(struct ua101 *ua, struct snd_pcm_substream *substream,
638 unsigned int channels)
640 int err;
642 substream->runtime->hw.info =
643 SNDRV_PCM_INFO_MMAP |
644 SNDRV_PCM_INFO_MMAP_VALID |
645 SNDRV_PCM_INFO_BATCH |
646 SNDRV_PCM_INFO_INTERLEAVED |
647 SNDRV_PCM_INFO_BLOCK_TRANSFER |
648 SNDRV_PCM_INFO_FIFO_IN_FRAMES;
649 substream->runtime->hw.formats = ua->format_bit;
650 substream->runtime->hw.rates = snd_pcm_rate_to_rate_bit(ua->rate);
651 substream->runtime->hw.rate_min = ua->rate;
652 substream->runtime->hw.rate_max = ua->rate;
653 substream->runtime->hw.channels_min = channels;
654 substream->runtime->hw.channels_max = channels;
655 substream->runtime->hw.buffer_bytes_max = 45000 * 1024;
656 substream->runtime->hw.period_bytes_min = 1;
657 substream->runtime->hw.period_bytes_max = UINT_MAX;
658 substream->runtime->hw.periods_min = 2;
659 substream->runtime->hw.periods_max = UINT_MAX;
660 err = snd_pcm_hw_constraint_minmax(substream->runtime,
661 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
662 1500000 / ua->packets_per_second,
663 8192000);
664 if (err < 0)
665 return err;
666 err = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 32, 24);
667 return err;
670 static int capture_pcm_open(struct snd_pcm_substream *substream)
672 struct ua101 *ua = substream->private_data;
673 int err;
675 ua->capture.substream = substream;
676 err = set_stream_hw(ua, substream, ua->capture.channels);
677 if (err < 0)
678 return err;
679 substream->runtime->hw.fifo_size =
680 DIV_ROUND_CLOSEST(ua->rate, ua->packets_per_second);
681 substream->runtime->delay = substream->runtime->hw.fifo_size;
683 mutex_lock(&ua->mutex);
684 err = start_usb_capture(ua);
685 if (err >= 0)
686 set_bit(ALSA_CAPTURE_OPEN, &ua->states);
687 mutex_unlock(&ua->mutex);
688 return err;
691 static int playback_pcm_open(struct snd_pcm_substream *substream)
693 struct ua101 *ua = substream->private_data;
694 int err;
696 ua->playback.substream = substream;
697 err = set_stream_hw(ua, substream, ua->playback.channels);
698 if (err < 0)
699 return err;
700 substream->runtime->hw.fifo_size =
701 DIV_ROUND_CLOSEST(ua->rate * ua->playback.queue_length,
702 ua->packets_per_second);
704 mutex_lock(&ua->mutex);
705 err = start_usb_capture(ua);
706 if (err < 0)
707 goto error;
708 err = start_usb_playback(ua);
709 if (err < 0) {
710 if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states))
711 stop_usb_capture(ua);
712 goto error;
714 set_bit(ALSA_PLAYBACK_OPEN, &ua->states);
715 error:
716 mutex_unlock(&ua->mutex);
717 return err;
720 static int capture_pcm_close(struct snd_pcm_substream *substream)
722 struct ua101 *ua = substream->private_data;
724 mutex_lock(&ua->mutex);
725 clear_bit(ALSA_CAPTURE_OPEN, &ua->states);
726 if (!test_bit(ALSA_PLAYBACK_OPEN, &ua->states))
727 stop_usb_capture(ua);
728 mutex_unlock(&ua->mutex);
729 return 0;
732 static int playback_pcm_close(struct snd_pcm_substream *substream)
734 struct ua101 *ua = substream->private_data;
736 mutex_lock(&ua->mutex);
737 stop_usb_playback(ua);
738 clear_bit(ALSA_PLAYBACK_OPEN, &ua->states);
739 if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states))
740 stop_usb_capture(ua);
741 mutex_unlock(&ua->mutex);
742 return 0;
745 static int capture_pcm_hw_params(struct snd_pcm_substream *substream,
746 struct snd_pcm_hw_params *hw_params)
748 struct ua101 *ua = substream->private_data;
749 int err;
751 mutex_lock(&ua->mutex);
752 err = start_usb_capture(ua);
753 mutex_unlock(&ua->mutex);
754 if (err < 0)
755 return err;
757 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
758 params_buffer_bytes(hw_params));
761 static int playback_pcm_hw_params(struct snd_pcm_substream *substream,
762 struct snd_pcm_hw_params *hw_params)
764 struct ua101 *ua = substream->private_data;
765 int err;
767 mutex_lock(&ua->mutex);
768 err = start_usb_capture(ua);
769 if (err >= 0)
770 err = start_usb_playback(ua);
771 mutex_unlock(&ua->mutex);
772 if (err < 0)
773 return err;
775 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
776 params_buffer_bytes(hw_params));
779 static int ua101_pcm_hw_free(struct snd_pcm_substream *substream)
781 return snd_pcm_lib_free_vmalloc_buffer(substream);
784 static int capture_pcm_prepare(struct snd_pcm_substream *substream)
786 struct ua101 *ua = substream->private_data;
787 int err;
789 mutex_lock(&ua->mutex);
790 err = start_usb_capture(ua);
791 mutex_unlock(&ua->mutex);
792 if (err < 0)
793 return err;
796 * The EHCI driver schedules the first packet of an iso stream at 10 ms
797 * in the future, i.e., no data is actually captured for that long.
798 * Take the wait here so that the stream is known to be actually
799 * running when the start trigger has been called.
801 wait_event(ua->alsa_capture_wait,
802 test_bit(CAPTURE_URB_COMPLETED, &ua->states) ||
803 !test_bit(USB_CAPTURE_RUNNING, &ua->states));
804 if (test_bit(DISCONNECTED, &ua->states))
805 return -ENODEV;
806 if (!test_bit(USB_CAPTURE_RUNNING, &ua->states))
807 return -EIO;
809 ua->capture.period_pos = 0;
810 ua->capture.buffer_pos = 0;
811 return 0;
814 static int playback_pcm_prepare(struct snd_pcm_substream *substream)
816 struct ua101 *ua = substream->private_data;
817 int err;
819 mutex_lock(&ua->mutex);
820 err = start_usb_capture(ua);
821 if (err >= 0)
822 err = start_usb_playback(ua);
823 mutex_unlock(&ua->mutex);
824 if (err < 0)
825 return err;
827 /* see the comment in capture_pcm_prepare() */
828 wait_event(ua->alsa_playback_wait,
829 test_bit(PLAYBACK_URB_COMPLETED, &ua->states) ||
830 !test_bit(USB_PLAYBACK_RUNNING, &ua->states));
831 if (test_bit(DISCONNECTED, &ua->states))
832 return -ENODEV;
833 if (!test_bit(USB_PLAYBACK_RUNNING, &ua->states))
834 return -EIO;
836 substream->runtime->delay = 0;
837 ua->playback.period_pos = 0;
838 ua->playback.buffer_pos = 0;
839 return 0;
842 static int capture_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
844 struct ua101 *ua = substream->private_data;
846 switch (cmd) {
847 case SNDRV_PCM_TRIGGER_START:
848 if (!test_bit(USB_CAPTURE_RUNNING, &ua->states))
849 return -EIO;
850 set_bit(ALSA_CAPTURE_RUNNING, &ua->states);
851 return 0;
852 case SNDRV_PCM_TRIGGER_STOP:
853 clear_bit(ALSA_CAPTURE_RUNNING, &ua->states);
854 return 0;
855 default:
856 return -EINVAL;
860 static int playback_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
862 struct ua101 *ua = substream->private_data;
864 switch (cmd) {
865 case SNDRV_PCM_TRIGGER_START:
866 if (!test_bit(USB_PLAYBACK_RUNNING, &ua->states))
867 return -EIO;
868 set_bit(ALSA_PLAYBACK_RUNNING, &ua->states);
869 return 0;
870 case SNDRV_PCM_TRIGGER_STOP:
871 clear_bit(ALSA_PLAYBACK_RUNNING, &ua->states);
872 return 0;
873 default:
874 return -EINVAL;
878 static inline snd_pcm_uframes_t ua101_pcm_pointer(struct ua101 *ua,
879 struct ua101_stream *stream)
881 unsigned long flags;
882 unsigned int pos;
884 spin_lock_irqsave(&ua->lock, flags);
885 pos = stream->buffer_pos;
886 spin_unlock_irqrestore(&ua->lock, flags);
887 return pos;
890 static snd_pcm_uframes_t capture_pcm_pointer(struct snd_pcm_substream *subs)
892 struct ua101 *ua = subs->private_data;
894 return ua101_pcm_pointer(ua, &ua->capture);
897 static snd_pcm_uframes_t playback_pcm_pointer(struct snd_pcm_substream *subs)
899 struct ua101 *ua = subs->private_data;
901 return ua101_pcm_pointer(ua, &ua->playback);
904 static struct snd_pcm_ops capture_pcm_ops = {
905 .open = capture_pcm_open,
906 .close = capture_pcm_close,
907 .ioctl = snd_pcm_lib_ioctl,
908 .hw_params = capture_pcm_hw_params,
909 .hw_free = ua101_pcm_hw_free,
910 .prepare = capture_pcm_prepare,
911 .trigger = capture_pcm_trigger,
912 .pointer = capture_pcm_pointer,
913 .page = snd_pcm_lib_get_vmalloc_page,
914 .mmap = snd_pcm_lib_mmap_vmalloc,
917 static struct snd_pcm_ops playback_pcm_ops = {
918 .open = playback_pcm_open,
919 .close = playback_pcm_close,
920 .ioctl = snd_pcm_lib_ioctl,
921 .hw_params = playback_pcm_hw_params,
922 .hw_free = ua101_pcm_hw_free,
923 .prepare = playback_pcm_prepare,
924 .trigger = playback_pcm_trigger,
925 .pointer = playback_pcm_pointer,
926 .page = snd_pcm_lib_get_vmalloc_page,
927 .mmap = snd_pcm_lib_mmap_vmalloc,
930 static const struct uac_format_type_i_discrete_descriptor *
931 find_format_descriptor(struct usb_interface *interface)
933 struct usb_host_interface *alt;
934 u8 *extra;
935 int extralen;
937 if (interface->num_altsetting != 2) {
938 dev_err(&interface->dev, "invalid num_altsetting\n");
939 return NULL;
942 alt = &interface->altsetting[0];
943 if (alt->desc.bNumEndpoints != 0) {
944 dev_err(&interface->dev, "invalid bNumEndpoints\n");
945 return NULL;
948 alt = &interface->altsetting[1];
949 if (alt->desc.bNumEndpoints != 1) {
950 dev_err(&interface->dev, "invalid bNumEndpoints\n");
951 return NULL;
954 extra = alt->extra;
955 extralen = alt->extralen;
956 while (extralen >= sizeof(struct usb_descriptor_header)) {
957 struct uac_format_type_i_discrete_descriptor *desc;
959 desc = (struct uac_format_type_i_discrete_descriptor *)extra;
960 if (desc->bLength > extralen) {
961 dev_err(&interface->dev, "descriptor overflow\n");
962 return NULL;
964 if (desc->bLength == UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1) &&
965 desc->bDescriptorType == USB_DT_CS_INTERFACE &&
966 desc->bDescriptorSubtype == UAC_FORMAT_TYPE) {
967 if (desc->bFormatType != UAC_FORMAT_TYPE_I_PCM ||
968 desc->bSamFreqType != 1) {
969 dev_err(&interface->dev,
970 "invalid format type\n");
971 return NULL;
973 return desc;
975 extralen -= desc->bLength;
976 extra += desc->bLength;
978 dev_err(&interface->dev, "sample format descriptor not found\n");
979 return NULL;
982 static int detect_usb_format(struct ua101 *ua)
984 const struct uac_format_type_i_discrete_descriptor *fmt_capture;
985 const struct uac_format_type_i_discrete_descriptor *fmt_playback;
986 const struct usb_endpoint_descriptor *epd;
987 unsigned int rate2;
989 fmt_capture = find_format_descriptor(ua->intf[INTF_CAPTURE]);
990 fmt_playback = find_format_descriptor(ua->intf[INTF_PLAYBACK]);
991 if (!fmt_capture || !fmt_playback)
992 return -ENXIO;
994 switch (fmt_capture->bSubframeSize) {
995 case 3:
996 ua->format_bit = SNDRV_PCM_FMTBIT_S24_3LE;
997 break;
998 case 4:
999 ua->format_bit = SNDRV_PCM_FMTBIT_S32_LE;
1000 break;
1001 default:
1002 dev_err(&ua->dev->dev, "sample width is not 24 or 32 bits\n");
1003 return -ENXIO;
1005 if (fmt_capture->bSubframeSize != fmt_playback->bSubframeSize) {
1006 dev_err(&ua->dev->dev,
1007 "playback/capture sample widths do not match\n");
1008 return -ENXIO;
1011 if (fmt_capture->bBitResolution != 24 ||
1012 fmt_playback->bBitResolution != 24) {
1013 dev_err(&ua->dev->dev, "sample width is not 24 bits\n");
1014 return -ENXIO;
1017 ua->rate = combine_triple(fmt_capture->tSamFreq[0]);
1018 rate2 = combine_triple(fmt_playback->tSamFreq[0]);
1019 if (ua->rate != rate2) {
1020 dev_err(&ua->dev->dev,
1021 "playback/capture rates do not match: %u/%u\n",
1022 rate2, ua->rate);
1023 return -ENXIO;
1026 switch (ua->dev->speed) {
1027 case USB_SPEED_FULL:
1028 ua->packets_per_second = 1000;
1029 break;
1030 case USB_SPEED_HIGH:
1031 ua->packets_per_second = 8000;
1032 break;
1033 default:
1034 dev_err(&ua->dev->dev, "unknown device speed\n");
1035 return -ENXIO;
1038 ua->capture.channels = fmt_capture->bNrChannels;
1039 ua->playback.channels = fmt_playback->bNrChannels;
1040 ua->capture.frame_bytes =
1041 fmt_capture->bSubframeSize * ua->capture.channels;
1042 ua->playback.frame_bytes =
1043 fmt_playback->bSubframeSize * ua->playback.channels;
1045 epd = &ua->intf[INTF_CAPTURE]->altsetting[1].endpoint[0].desc;
1046 if (!usb_endpoint_is_isoc_in(epd)) {
1047 dev_err(&ua->dev->dev, "invalid capture endpoint\n");
1048 return -ENXIO;
1050 ua->capture.usb_pipe = usb_rcvisocpipe(ua->dev, usb_endpoint_num(epd));
1051 ua->capture.max_packet_bytes = le16_to_cpu(epd->wMaxPacketSize);
1053 epd = &ua->intf[INTF_PLAYBACK]->altsetting[1].endpoint[0].desc;
1054 if (!usb_endpoint_is_isoc_out(epd)) {
1055 dev_err(&ua->dev->dev, "invalid playback endpoint\n");
1056 return -ENXIO;
1058 ua->playback.usb_pipe = usb_sndisocpipe(ua->dev, usb_endpoint_num(epd));
1059 ua->playback.max_packet_bytes = le16_to_cpu(epd->wMaxPacketSize);
1060 return 0;
1063 static int alloc_stream_buffers(struct ua101 *ua, struct ua101_stream *stream)
1065 unsigned int remaining_packets, packets, packets_per_page, i;
1066 size_t size;
1068 stream->queue_length = queue_length;
1069 stream->queue_length = max(stream->queue_length,
1070 (unsigned int)MIN_QUEUE_LENGTH);
1071 stream->queue_length = min(stream->queue_length,
1072 (unsigned int)MAX_QUEUE_LENGTH);
1075 * The cache pool sizes used by usb_buffer_alloc() (128, 512, 2048) are
1076 * quite bad when used with the packet sizes of this device (e.g. 280,
1077 * 520, 624). Therefore, we allocate and subdivide entire pages, using
1078 * a smaller buffer only for the last chunk.
1080 remaining_packets = stream->queue_length;
1081 packets_per_page = PAGE_SIZE / stream->max_packet_bytes;
1082 for (i = 0; i < ARRAY_SIZE(stream->buffers); ++i) {
1083 packets = min(remaining_packets, packets_per_page);
1084 size = packets * stream->max_packet_bytes;
1085 stream->buffers[i].addr =
1086 usb_buffer_alloc(ua->dev, size, GFP_KERNEL,
1087 &stream->buffers[i].dma);
1088 if (!stream->buffers[i].addr)
1089 return -ENOMEM;
1090 stream->buffers[i].size = size;
1091 remaining_packets -= packets;
1092 if (!remaining_packets)
1093 break;
1095 if (remaining_packets) {
1096 dev_err(&ua->dev->dev, "too many packets\n");
1097 return -ENXIO;
1099 return 0;
1102 static void free_stream_buffers(struct ua101 *ua, struct ua101_stream *stream)
1104 unsigned int i;
1106 for (i = 0; i < ARRAY_SIZE(stream->buffers); ++i)
1107 usb_buffer_free(ua->dev,
1108 stream->buffers[i].size,
1109 stream->buffers[i].addr,
1110 stream->buffers[i].dma);
1113 static int alloc_stream_urbs(struct ua101 *ua, struct ua101_stream *stream,
1114 void (*urb_complete)(struct urb *))
1116 unsigned max_packet_size = stream->max_packet_bytes;
1117 struct ua101_urb *urb;
1118 unsigned int b, u = 0;
1120 for (b = 0; b < ARRAY_SIZE(stream->buffers); ++b) {
1121 unsigned int size = stream->buffers[b].size;
1122 u8 *addr = stream->buffers[b].addr;
1123 dma_addr_t dma = stream->buffers[b].dma;
1125 while (size >= max_packet_size) {
1126 if (u >= stream->queue_length)
1127 goto bufsize_error;
1128 urb = kmalloc(sizeof(*urb), GFP_KERNEL);
1129 if (!urb)
1130 return -ENOMEM;
1131 usb_init_urb(&urb->urb);
1132 urb->urb.dev = ua->dev;
1133 urb->urb.pipe = stream->usb_pipe;
1134 urb->urb.transfer_flags = URB_ISO_ASAP |
1135 URB_NO_TRANSFER_DMA_MAP;
1136 urb->urb.transfer_buffer = addr;
1137 urb->urb.transfer_dma = dma;
1138 urb->urb.transfer_buffer_length = max_packet_size;
1139 urb->urb.number_of_packets = 1;
1140 urb->urb.interval = 1;
1141 urb->urb.context = ua;
1142 urb->urb.complete = urb_complete;
1143 urb->urb.iso_frame_desc[0].offset = 0;
1144 urb->urb.iso_frame_desc[0].length = max_packet_size;
1145 stream->urbs[u++] = urb;
1146 size -= max_packet_size;
1147 addr += max_packet_size;
1148 dma += max_packet_size;
1151 if (u == stream->queue_length)
1152 return 0;
1153 bufsize_error:
1154 dev_err(&ua->dev->dev, "internal buffer size error\n");
1155 return -ENXIO;
1158 static void free_stream_urbs(struct ua101_stream *stream)
1160 unsigned int i;
1162 for (i = 0; i < stream->queue_length; ++i)
1163 kfree(stream->urbs[i]);
1166 static void free_usb_related_resources(struct ua101 *ua,
1167 struct usb_interface *interface)
1169 unsigned int i;
1171 free_stream_urbs(&ua->capture);
1172 free_stream_urbs(&ua->playback);
1173 free_stream_buffers(ua, &ua->capture);
1174 free_stream_buffers(ua, &ua->playback);
1176 for (i = 0; i < ARRAY_SIZE(ua->intf); ++i)
1177 if (ua->intf[i]) {
1178 usb_set_intfdata(ua->intf[i], NULL);
1179 if (ua->intf[i] != interface)
1180 usb_driver_release_interface(&ua101_driver,
1181 ua->intf[i]);
1185 static void ua101_card_free(struct snd_card *card)
1187 struct ua101 *ua = card->private_data;
1189 mutex_destroy(&ua->mutex);
1192 static int ua101_probe(struct usb_interface *interface,
1193 const struct usb_device_id *usb_id)
1195 static const struct snd_usb_midi_endpoint_info midi_ep = {
1196 .out_cables = 0x0001,
1197 .in_cables = 0x0001
1199 static const struct snd_usb_audio_quirk midi_quirk = {
1200 .type = QUIRK_MIDI_FIXED_ENDPOINT,
1201 .data = &midi_ep
1203 static const int intf_numbers[2][3] = {
1204 { /* UA-101 */
1205 [INTF_PLAYBACK] = 0,
1206 [INTF_CAPTURE] = 1,
1207 [INTF_MIDI] = 2,
1209 { /* UA-1000 */
1210 [INTF_CAPTURE] = 1,
1211 [INTF_PLAYBACK] = 2,
1212 [INTF_MIDI] = 3,
1215 struct snd_card *card;
1216 struct ua101 *ua;
1217 unsigned int card_index, i;
1218 int is_ua1000;
1219 const char *name;
1220 char usb_path[32];
1221 int err;
1223 is_ua1000 = usb_id->idProduct == 0x0044;
1225 if (interface->altsetting->desc.bInterfaceNumber !=
1226 intf_numbers[is_ua1000][0])
1227 return -ENODEV;
1229 mutex_lock(&devices_mutex);
1231 for (card_index = 0; card_index < SNDRV_CARDS; ++card_index)
1232 if (enable[card_index] && !(devices_used & (1 << card_index)))
1233 break;
1234 if (card_index >= SNDRV_CARDS) {
1235 mutex_unlock(&devices_mutex);
1236 return -ENOENT;
1238 err = snd_card_create(index[card_index], id[card_index], THIS_MODULE,
1239 sizeof(*ua), &card);
1240 if (err < 0) {
1241 mutex_unlock(&devices_mutex);
1242 return err;
1244 card->private_free = ua101_card_free;
1245 ua = card->private_data;
1246 ua->dev = interface_to_usbdev(interface);
1247 ua->card = card;
1248 ua->card_index = card_index;
1249 INIT_LIST_HEAD(&ua->midi_list);
1250 spin_lock_init(&ua->lock);
1251 mutex_init(&ua->mutex);
1252 INIT_LIST_HEAD(&ua->ready_playback_urbs);
1253 tasklet_init(&ua->playback_tasklet,
1254 playback_tasklet, (unsigned long)ua);
1255 init_waitqueue_head(&ua->alsa_capture_wait);
1256 init_waitqueue_head(&ua->rate_feedback_wait);
1257 init_waitqueue_head(&ua->alsa_playback_wait);
1259 #ifdef UA1A_HACK
1260 if (ua->dev->descriptor.idProduct == cpu_to_le16(0x0018)) {
1261 ua->intf[2] = interface;
1262 ua->intf[0] = usb_ifnum_to_if(ua->dev, 1);
1263 ua->intf[1] = usb_ifnum_to_if(ua->dev, 2);
1264 usb_driver_claim_interface(&ua101_driver, ua->intf[0], ua);
1265 usb_driver_claim_interface(&ua101_driver, ua->intf[1], ua);
1266 } else {
1267 #endif
1268 ua->intf[0] = interface;
1269 for (i = 1; i < ARRAY_SIZE(ua->intf); ++i) {
1270 ua->intf[i] = usb_ifnum_to_if(ua->dev,
1271 intf_numbers[is_ua1000][i]);
1272 if (!ua->intf[i]) {
1273 dev_err(&ua->dev->dev, "interface %u not found\n",
1274 intf_numbers[is_ua1000][i]);
1275 err = -ENXIO;
1276 goto probe_error;
1278 err = usb_driver_claim_interface(&ua101_driver,
1279 ua->intf[i], ua);
1280 if (err < 0) {
1281 ua->intf[i] = NULL;
1282 err = -EBUSY;
1283 goto probe_error;
1286 #ifdef UA1A_HACK
1288 #endif
1290 snd_card_set_dev(card, &interface->dev);
1292 #ifdef UA1A_HACK
1293 if (ua->dev->descriptor.idProduct == cpu_to_le16(0x0018)) {
1294 ua->format_bit = SNDRV_PCM_FMTBIT_S16_LE;
1295 ua->rate = 44100;
1296 ua->packets_per_second = 1000;
1297 ua->capture.channels = 2;
1298 ua->playback.channels = 2;
1299 ua->capture.frame_bytes = 4;
1300 ua->playback.frame_bytes = 4;
1301 ua->capture.usb_pipe = usb_rcvisocpipe(ua->dev, 2);
1302 ua->playback.usb_pipe = usb_sndisocpipe(ua->dev, 1);
1303 ua->capture.max_packet_bytes = 192;
1304 ua->playback.max_packet_bytes = 192;
1305 } else {
1306 #endif
1307 err = detect_usb_format(ua);
1308 if (err < 0)
1309 goto probe_error;
1310 #ifdef UA1A_HACK
1312 #endif
1314 name = usb_id->idProduct == 0x0044 ? "UA-1000" : "UA-101";
1315 strcpy(card->driver, "UA-101");
1316 strcpy(card->shortname, name);
1317 usb_make_path(ua->dev, usb_path, sizeof(usb_path));
1318 snprintf(ua->card->longname, sizeof(ua->card->longname),
1319 "EDIROL %s (serial %s), %u Hz at %s, %s speed", name,
1320 ua->dev->serial ? ua->dev->serial : "?", ua->rate, usb_path,
1321 ua->dev->speed == USB_SPEED_HIGH ? "high" : "full");
1323 err = alloc_stream_buffers(ua, &ua->capture);
1324 if (err < 0)
1325 goto probe_error;
1326 err = alloc_stream_buffers(ua, &ua->playback);
1327 if (err < 0)
1328 goto probe_error;
1330 err = alloc_stream_urbs(ua, &ua->capture, capture_urb_complete);
1331 if (err < 0)
1332 goto probe_error;
1333 err = alloc_stream_urbs(ua, &ua->playback, playback_urb_complete);
1334 if (err < 0)
1335 goto probe_error;
1337 err = snd_pcm_new(card, name, 0, 1, 1, &ua->pcm);
1338 if (err < 0)
1339 goto probe_error;
1340 ua->pcm->private_data = ua;
1341 strcpy(ua->pcm->name, name);
1342 snd_pcm_set_ops(ua->pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_pcm_ops);
1343 snd_pcm_set_ops(ua->pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_pcm_ops);
1345 #ifdef UA1A_HACK
1346 if (ua->dev->descriptor.idProduct != cpu_to_le16(0x0018)) {
1347 #endif
1348 err = snd_usbmidi_create(card, ua->intf[INTF_MIDI],
1349 &ua->midi_list, &midi_quirk);
1350 if (err < 0)
1351 goto probe_error;
1352 #ifdef UA1A_HACK
1354 #endif
1356 err = snd_card_register(card);
1357 if (err < 0)
1358 goto probe_error;
1360 usb_set_intfdata(interface, ua);
1361 devices_used |= 1 << card_index;
1363 mutex_unlock(&devices_mutex);
1364 return 0;
1366 probe_error:
1367 free_usb_related_resources(ua, interface);
1368 snd_card_free(card);
1369 mutex_unlock(&devices_mutex);
1370 return err;
1373 static void ua101_disconnect(struct usb_interface *interface)
1375 struct ua101 *ua = usb_get_intfdata(interface);
1376 struct list_head *midi;
1378 if (!ua)
1379 return;
1381 mutex_lock(&devices_mutex);
1383 set_bit(DISCONNECTED, &ua->states);
1384 wake_up(&ua->rate_feedback_wait);
1386 /* make sure that userspace cannot create new requests */
1387 snd_card_disconnect(ua->card);
1389 /* make sure that there are no pending USB requests */
1390 __list_for_each(midi, &ua->midi_list)
1391 snd_usbmidi_disconnect(midi);
1392 abort_alsa_playback(ua);
1393 abort_alsa_capture(ua);
1394 mutex_lock(&ua->mutex);
1395 stop_usb_playback(ua);
1396 stop_usb_capture(ua);
1397 mutex_unlock(&ua->mutex);
1399 free_usb_related_resources(ua, interface);
1401 devices_used &= ~(1 << ua->card_index);
1403 snd_card_free_when_closed(ua->card);
1405 mutex_unlock(&devices_mutex);
1408 static struct usb_device_id ua101_ids[] = {
1409 #ifdef UA1A_HACK
1410 { USB_DEVICE(0x0582, 0x0018) },
1411 #endif
1412 { USB_DEVICE(0x0582, 0x0044) }, /* UA-1000 high speed */
1413 { USB_DEVICE(0x0582, 0x007d) }, /* UA-101 high speed */
1414 { USB_DEVICE(0x0582, 0x008d) }, /* UA-101 full speed */
1417 MODULE_DEVICE_TABLE(usb, ua101_ids);
1419 static struct usb_driver ua101_driver = {
1420 .name = "snd-ua101",
1421 .id_table = ua101_ids,
1422 .probe = ua101_probe,
1423 .disconnect = ua101_disconnect,
1424 #if 0
1425 .suspend = ua101_suspend,
1426 .resume = ua101_resume,
1427 #endif
1430 static int __init alsa_card_ua101_init(void)
1432 return usb_register(&ua101_driver);
1435 static void __exit alsa_card_ua101_exit(void)
1437 usb_deregister(&ua101_driver);
1438 mutex_destroy(&devices_mutex);
1441 module_init(alsa_card_ua101_init);
1442 module_exit(alsa_card_ua101_exit);