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"
29 MODULE_DESCRIPTION("Edirol UA-101/1000 driver");
30 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
31 MODULE_LICENSE("GPL v2");
32 MODULE_SUPPORTED_DEVICE("{{Edirol,UA-101},{Edirol,UA-1000}}");
35 * Should not be lower than the minimum scheduling delay of the host
36 * controller. Some Intel controllers need more than one frame; as long as
37 * that driver doesn't tell us about this, use 1.5 frames just to be sure.
39 #define MIN_QUEUE_LENGTH 12
40 /* Somewhat random. */
41 #define MAX_QUEUE_LENGTH 30
43 * This magic value optimizes memory usage efficiency for the UA-101's packet
44 * sizes at all sample rates, taking into account the stupid cache pool sizes
45 * that usb_alloc_coherent() uses.
47 #define DEFAULT_QUEUE_LENGTH 21
49 #define MAX_PACKET_SIZE 672 /* hardware specific */
50 #define MAX_MEMORY_BUFFERS DIV_ROUND_UP(MAX_QUEUE_LENGTH, \
51 PAGE_SIZE / MAX_PACKET_SIZE)
53 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
54 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
55 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
56 static unsigned int queue_length = 21;
58 module_param_array(index, int, NULL, 0444);
59 MODULE_PARM_DESC(index, "card index");
60 module_param_array(id, charp, NULL, 0444);
61 MODULE_PARM_DESC(id, "ID string");
62 module_param_array(enable, bool, NULL, 0444);
63 MODULE_PARM_DESC(enable, "enable card");
64 module_param(queue_length, uint, 0644);
65 MODULE_PARM_DESC(queue_length, "USB queue length in microframes, "
66 __stringify(MIN_QUEUE_LENGTH)"-"__stringify(MAX_QUEUE_LENGTH));
76 /* bits in struct ua101::states */
83 ALSA_PLAYBACK_RUNNING,
84 CAPTURE_URB_COMPLETED,
85 PLAYBACK_URB_COMPLETED,
90 struct usb_device *dev;
91 struct snd_card *card;
92 struct usb_interface *intf[INTF_COUNT];
95 struct list_head midi_list;
98 unsigned int packets_per_second;
101 unsigned long states;
103 /* FIFO to synchronize playback rate to capture rate */
104 unsigned int rate_feedback_start;
105 unsigned int rate_feedback_count;
106 u8 rate_feedback[MAX_QUEUE_LENGTH];
108 struct list_head ready_playback_urbs;
109 struct tasklet_struct playback_tasklet;
110 wait_queue_head_t alsa_capture_wait;
111 wait_queue_head_t rate_feedback_wait;
112 wait_queue_head_t alsa_playback_wait;
113 struct ua101_stream {
114 struct snd_pcm_substream *substream;
115 unsigned int usb_pipe;
116 unsigned int channels;
117 unsigned int frame_bytes;
118 unsigned int max_packet_bytes;
119 unsigned int period_pos;
120 unsigned int buffer_pos;
121 unsigned int queue_length;
124 struct usb_iso_packet_descriptor iso_frame_desc[1];
125 struct list_head ready_list;
126 } *urbs[MAX_QUEUE_LENGTH];
131 } buffers[MAX_MEMORY_BUFFERS];
135 static DEFINE_MUTEX(devices_mutex);
136 static unsigned int devices_used;
137 static struct usb_driver ua101_driver;
139 static void abort_alsa_playback(struct ua101 *ua);
140 static void abort_alsa_capture(struct ua101 *ua);
142 static const char *usb_error_string(int err)
148 return "endpoint not enabled";
150 return "endpoint stalled";
152 return "not enough bandwidth";
154 return "device disabled";
156 return "device suspended";
161 return "internal error";
163 return "unknown error";
167 static void abort_usb_capture(struct ua101 *ua)
169 if (test_and_clear_bit(USB_CAPTURE_RUNNING, &ua->states)) {
170 wake_up(&ua->alsa_capture_wait);
171 wake_up(&ua->rate_feedback_wait);
175 static void abort_usb_playback(struct ua101 *ua)
177 if (test_and_clear_bit(USB_PLAYBACK_RUNNING, &ua->states))
178 wake_up(&ua->alsa_playback_wait);
181 static void playback_urb_complete(struct urb *usb_urb)
183 struct ua101_urb *urb = (struct ua101_urb *)usb_urb;
184 struct ua101 *ua = urb->urb.context;
187 if (unlikely(urb->urb.status == -ENOENT || /* unlinked */
188 urb->urb.status == -ENODEV || /* device removed */
189 urb->urb.status == -ECONNRESET || /* unlinked */
190 urb->urb.status == -ESHUTDOWN)) { /* device disabled */
191 abort_usb_playback(ua);
192 abort_alsa_playback(ua);
196 if (test_bit(USB_PLAYBACK_RUNNING, &ua->states)) {
197 /* append URB to FIFO */
198 spin_lock_irqsave(&ua->lock, flags);
199 list_add_tail(&urb->ready_list, &ua->ready_playback_urbs);
200 if (ua->rate_feedback_count > 0)
201 tasklet_schedule(&ua->playback_tasklet);
202 ua->playback.substream->runtime->delay -=
203 urb->urb.iso_frame_desc[0].length /
204 ua->playback.frame_bytes;
205 spin_unlock_irqrestore(&ua->lock, flags);
209 static void first_playback_urb_complete(struct urb *urb)
211 struct ua101 *ua = urb->context;
213 urb->complete = playback_urb_complete;
214 playback_urb_complete(urb);
216 set_bit(PLAYBACK_URB_COMPLETED, &ua->states);
217 wake_up(&ua->alsa_playback_wait);
220 /* copy data from the ALSA ring buffer into the URB buffer */
221 static bool copy_playback_data(struct ua101_stream *stream, struct urb *urb,
224 struct snd_pcm_runtime *runtime;
225 unsigned int frame_bytes, frames1;
228 runtime = stream->substream->runtime;
229 frame_bytes = stream->frame_bytes;
230 source = runtime->dma_area + stream->buffer_pos * frame_bytes;
231 if (stream->buffer_pos + frames <= runtime->buffer_size) {
232 memcpy(urb->transfer_buffer, source, frames * frame_bytes);
234 /* wrap around at end of ring buffer */
235 frames1 = runtime->buffer_size - stream->buffer_pos;
236 memcpy(urb->transfer_buffer, source, frames1 * frame_bytes);
237 memcpy(urb->transfer_buffer + frames1 * frame_bytes,
238 runtime->dma_area, (frames - frames1) * frame_bytes);
241 stream->buffer_pos += frames;
242 if (stream->buffer_pos >= runtime->buffer_size)
243 stream->buffer_pos -= runtime->buffer_size;
244 stream->period_pos += frames;
245 if (stream->period_pos >= runtime->period_size) {
246 stream->period_pos -= runtime->period_size;
252 static inline void add_with_wraparound(struct ua101 *ua,
253 unsigned int *value, unsigned int add)
256 if (*value >= ua->playback.queue_length)
257 *value -= ua->playback.queue_length;
260 static void playback_tasklet(unsigned long data)
262 struct ua101 *ua = (void *)data;
265 struct ua101_urb *urb;
266 bool do_period_elapsed = false;
269 if (unlikely(!test_bit(USB_PLAYBACK_RUNNING, &ua->states)))
273 * Synchronizing the playback rate to the capture rate is done by using
274 * the same sequence of packet sizes for both streams.
275 * Submitting a playback URB therefore requires both a ready URB and
276 * the size of the corresponding capture packet, i.e., both playback
277 * and capture URBs must have been completed. Since the USB core does
278 * not guarantee that playback and capture complete callbacks are
279 * called alternately, we use two FIFOs for packet sizes and read URBs;
280 * submitting playback URBs is possible as long as both FIFOs are
283 spin_lock_irqsave(&ua->lock, flags);
284 while (ua->rate_feedback_count > 0 &&
285 !list_empty(&ua->ready_playback_urbs)) {
286 /* take packet size out of FIFO */
287 frames = ua->rate_feedback[ua->rate_feedback_start];
288 add_with_wraparound(ua, &ua->rate_feedback_start, 1);
289 ua->rate_feedback_count--;
291 /* take URB out of FIFO */
292 urb = list_first_entry(&ua->ready_playback_urbs,
293 struct ua101_urb, ready_list);
294 list_del(&urb->ready_list);
296 /* fill packet with data or silence */
297 urb->urb.iso_frame_desc[0].length =
298 frames * ua->playback.frame_bytes;
299 if (test_bit(ALSA_PLAYBACK_RUNNING, &ua->states))
300 do_period_elapsed |= copy_playback_data(&ua->playback,
304 memset(urb->urb.transfer_buffer, 0,
305 urb->urb.iso_frame_desc[0].length);
307 /* and off you go ... */
308 err = usb_submit_urb(&urb->urb, GFP_ATOMIC);
309 if (unlikely(err < 0)) {
310 spin_unlock_irqrestore(&ua->lock, flags);
311 abort_usb_playback(ua);
312 abort_alsa_playback(ua);
313 dev_err(&ua->dev->dev, "USB request error %d: %s\n",
314 err, usb_error_string(err));
317 ua->playback.substream->runtime->delay += frames;
319 spin_unlock_irqrestore(&ua->lock, flags);
320 if (do_period_elapsed)
321 snd_pcm_period_elapsed(ua->playback.substream);
324 /* copy data from the URB buffer into the ALSA ring buffer */
325 static bool copy_capture_data(struct ua101_stream *stream, struct urb *urb,
328 struct snd_pcm_runtime *runtime;
329 unsigned int frame_bytes, frames1;
332 runtime = stream->substream->runtime;
333 frame_bytes = stream->frame_bytes;
334 dest = runtime->dma_area + stream->buffer_pos * frame_bytes;
335 if (stream->buffer_pos + frames <= runtime->buffer_size) {
336 memcpy(dest, urb->transfer_buffer, frames * frame_bytes);
338 /* wrap around at end of ring buffer */
339 frames1 = runtime->buffer_size - stream->buffer_pos;
340 memcpy(dest, urb->transfer_buffer, frames1 * frame_bytes);
341 memcpy(runtime->dma_area,
342 urb->transfer_buffer + frames1 * frame_bytes,
343 (frames - frames1) * frame_bytes);
346 stream->buffer_pos += frames;
347 if (stream->buffer_pos >= runtime->buffer_size)
348 stream->buffer_pos -= runtime->buffer_size;
349 stream->period_pos += frames;
350 if (stream->period_pos >= runtime->period_size) {
351 stream->period_pos -= runtime->period_size;
357 static void capture_urb_complete(struct urb *urb)
359 struct ua101 *ua = urb->context;
360 struct ua101_stream *stream = &ua->capture;
362 unsigned int frames, write_ptr;
363 bool do_period_elapsed;
366 if (unlikely(urb->status == -ENOENT || /* unlinked */
367 urb->status == -ENODEV || /* device removed */
368 urb->status == -ECONNRESET || /* unlinked */
369 urb->status == -ESHUTDOWN)) /* device disabled */
372 if (urb->status >= 0 && urb->iso_frame_desc[0].status >= 0)
373 frames = urb->iso_frame_desc[0].actual_length /
378 spin_lock_irqsave(&ua->lock, flags);
380 if (frames > 0 && test_bit(ALSA_CAPTURE_RUNNING, &ua->states))
381 do_period_elapsed = copy_capture_data(stream, urb, frames);
383 do_period_elapsed = false;
385 if (test_bit(USB_CAPTURE_RUNNING, &ua->states)) {
386 err = usb_submit_urb(urb, GFP_ATOMIC);
387 if (unlikely(err < 0)) {
388 spin_unlock_irqrestore(&ua->lock, flags);
389 dev_err(&ua->dev->dev, "USB request error %d: %s\n",
390 err, usb_error_string(err));
394 /* append packet size to FIFO */
395 write_ptr = ua->rate_feedback_start;
396 add_with_wraparound(ua, &write_ptr, ua->rate_feedback_count);
397 ua->rate_feedback[write_ptr] = frames;
398 if (ua->rate_feedback_count < ua->playback.queue_length) {
399 ua->rate_feedback_count++;
400 if (ua->rate_feedback_count ==
401 ua->playback.queue_length)
402 wake_up(&ua->rate_feedback_wait);
405 * Ring buffer overflow; this happens when the playback
406 * stream is not running. Throw away the oldest entry,
407 * so that the playback stream, when it starts, sees
408 * the most recent packet sizes.
410 add_with_wraparound(ua, &ua->rate_feedback_start, 1);
412 if (test_bit(USB_PLAYBACK_RUNNING, &ua->states) &&
413 !list_empty(&ua->ready_playback_urbs))
414 tasklet_schedule(&ua->playback_tasklet);
417 spin_unlock_irqrestore(&ua->lock, flags);
419 if (do_period_elapsed)
420 snd_pcm_period_elapsed(stream->substream);
425 abort_usb_playback(ua);
426 abort_usb_capture(ua);
427 abort_alsa_playback(ua);
428 abort_alsa_capture(ua);
431 static void first_capture_urb_complete(struct urb *urb)
433 struct ua101 *ua = urb->context;
435 urb->complete = capture_urb_complete;
436 capture_urb_complete(urb);
438 set_bit(CAPTURE_URB_COMPLETED, &ua->states);
439 wake_up(&ua->alsa_capture_wait);
442 static int submit_stream_urbs(struct ua101 *ua, struct ua101_stream *stream)
446 for (i = 0; i < stream->queue_length; ++i) {
447 int err = usb_submit_urb(&stream->urbs[i]->urb, GFP_KERNEL);
449 dev_err(&ua->dev->dev, "USB request error %d: %s\n",
450 err, usb_error_string(err));
457 static void kill_stream_urbs(struct ua101_stream *stream)
461 for (i = 0; i < stream->queue_length; ++i)
463 usb_kill_urb(&stream->urbs[i]->urb);
466 static int enable_iso_interface(struct ua101 *ua, unsigned int intf_index)
468 struct usb_host_interface *alts;
470 alts = ua->intf[intf_index]->cur_altsetting;
471 if (alts->desc.bAlternateSetting != 1) {
472 int err = usb_set_interface(ua->dev,
473 alts->desc.bInterfaceNumber, 1);
475 dev_err(&ua->dev->dev,
476 "cannot initialize interface; error %d: %s\n",
477 err, usb_error_string(err));
484 static void disable_iso_interface(struct ua101 *ua, unsigned int intf_index)
486 struct usb_host_interface *alts;
488 if (!ua->intf[intf_index])
491 alts = ua->intf[intf_index]->cur_altsetting;
492 if (alts->desc.bAlternateSetting != 0) {
493 int err = usb_set_interface(ua->dev,
494 alts->desc.bInterfaceNumber, 0);
495 if (err < 0 && !test_bit(DISCONNECTED, &ua->states))
496 dev_warn(&ua->dev->dev,
497 "interface reset failed; error %d: %s\n",
498 err, usb_error_string(err));
502 static void stop_usb_capture(struct ua101 *ua)
504 clear_bit(USB_CAPTURE_RUNNING, &ua->states);
506 kill_stream_urbs(&ua->capture);
508 disable_iso_interface(ua, INTF_CAPTURE);
511 static int start_usb_capture(struct ua101 *ua)
515 if (test_bit(DISCONNECTED, &ua->states))
518 if (test_bit(USB_CAPTURE_RUNNING, &ua->states))
521 kill_stream_urbs(&ua->capture);
523 err = enable_iso_interface(ua, INTF_CAPTURE);
527 clear_bit(CAPTURE_URB_COMPLETED, &ua->states);
528 ua->capture.urbs[0]->urb.complete = first_capture_urb_complete;
529 ua->rate_feedback_start = 0;
530 ua->rate_feedback_count = 0;
532 set_bit(USB_CAPTURE_RUNNING, &ua->states);
533 err = submit_stream_urbs(ua, &ua->capture);
535 stop_usb_capture(ua);
539 static void stop_usb_playback(struct ua101 *ua)
541 clear_bit(USB_PLAYBACK_RUNNING, &ua->states);
543 kill_stream_urbs(&ua->playback);
545 tasklet_kill(&ua->playback_tasklet);
547 disable_iso_interface(ua, INTF_PLAYBACK);
550 static int start_usb_playback(struct ua101 *ua)
552 unsigned int i, frames;
556 if (test_bit(DISCONNECTED, &ua->states))
559 if (test_bit(USB_PLAYBACK_RUNNING, &ua->states))
562 kill_stream_urbs(&ua->playback);
563 tasklet_kill(&ua->playback_tasklet);
565 err = enable_iso_interface(ua, INTF_PLAYBACK);
569 clear_bit(PLAYBACK_URB_COMPLETED, &ua->states);
570 ua->playback.urbs[0]->urb.complete =
571 first_playback_urb_complete;
572 spin_lock_irq(&ua->lock);
573 INIT_LIST_HEAD(&ua->ready_playback_urbs);
574 spin_unlock_irq(&ua->lock);
577 * We submit the initial URBs all at once, so we have to wait for the
578 * packet size FIFO to be full.
580 wait_event(ua->rate_feedback_wait,
581 ua->rate_feedback_count >= ua->playback.queue_length ||
582 !test_bit(USB_CAPTURE_RUNNING, &ua->states) ||
583 test_bit(DISCONNECTED, &ua->states));
584 if (test_bit(DISCONNECTED, &ua->states)) {
585 stop_usb_playback(ua);
588 if (!test_bit(USB_CAPTURE_RUNNING, &ua->states)) {
589 stop_usb_playback(ua);
593 for (i = 0; i < ua->playback.queue_length; ++i) {
594 /* all initial URBs contain silence */
595 spin_lock_irq(&ua->lock);
596 frames = ua->rate_feedback[ua->rate_feedback_start];
597 add_with_wraparound(ua, &ua->rate_feedback_start, 1);
598 ua->rate_feedback_count--;
599 spin_unlock_irq(&ua->lock);
600 urb = &ua->playback.urbs[i]->urb;
601 urb->iso_frame_desc[0].length =
602 frames * ua->playback.frame_bytes;
603 memset(urb->transfer_buffer, 0,
604 urb->iso_frame_desc[0].length);
607 set_bit(USB_PLAYBACK_RUNNING, &ua->states);
608 err = submit_stream_urbs(ua, &ua->playback);
610 stop_usb_playback(ua);
614 static void abort_alsa_capture(struct ua101 *ua)
616 if (test_bit(ALSA_CAPTURE_RUNNING, &ua->states))
617 snd_pcm_stop(ua->capture.substream, SNDRV_PCM_STATE_XRUN);
620 static void abort_alsa_playback(struct ua101 *ua)
622 if (test_bit(ALSA_PLAYBACK_RUNNING, &ua->states))
623 snd_pcm_stop(ua->playback.substream, SNDRV_PCM_STATE_XRUN);
626 static int set_stream_hw(struct ua101 *ua, struct snd_pcm_substream *substream,
627 unsigned int channels)
631 substream->runtime->hw.info =
632 SNDRV_PCM_INFO_MMAP |
633 SNDRV_PCM_INFO_MMAP_VALID |
634 SNDRV_PCM_INFO_BATCH |
635 SNDRV_PCM_INFO_INTERLEAVED |
636 SNDRV_PCM_INFO_BLOCK_TRANSFER |
637 SNDRV_PCM_INFO_FIFO_IN_FRAMES;
638 substream->runtime->hw.formats = ua->format_bit;
639 substream->runtime->hw.rates = snd_pcm_rate_to_rate_bit(ua->rate);
640 substream->runtime->hw.rate_min = ua->rate;
641 substream->runtime->hw.rate_max = ua->rate;
642 substream->runtime->hw.channels_min = channels;
643 substream->runtime->hw.channels_max = channels;
644 substream->runtime->hw.buffer_bytes_max = 45000 * 1024;
645 substream->runtime->hw.period_bytes_min = 1;
646 substream->runtime->hw.period_bytes_max = UINT_MAX;
647 substream->runtime->hw.periods_min = 2;
648 substream->runtime->hw.periods_max = UINT_MAX;
649 err = snd_pcm_hw_constraint_minmax(substream->runtime,
650 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
651 1500000 / ua->packets_per_second,
655 err = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 32, 24);
659 static int capture_pcm_open(struct snd_pcm_substream *substream)
661 struct ua101 *ua = substream->private_data;
664 ua->capture.substream = substream;
665 err = set_stream_hw(ua, substream, ua->capture.channels);
668 substream->runtime->hw.fifo_size =
669 DIV_ROUND_CLOSEST(ua->rate, ua->packets_per_second);
670 substream->runtime->delay = substream->runtime->hw.fifo_size;
672 mutex_lock(&ua->mutex);
673 err = start_usb_capture(ua);
675 set_bit(ALSA_CAPTURE_OPEN, &ua->states);
676 mutex_unlock(&ua->mutex);
680 static int playback_pcm_open(struct snd_pcm_substream *substream)
682 struct ua101 *ua = substream->private_data;
685 ua->playback.substream = substream;
686 err = set_stream_hw(ua, substream, ua->playback.channels);
689 substream->runtime->hw.fifo_size =
690 DIV_ROUND_CLOSEST(ua->rate * ua->playback.queue_length,
691 ua->packets_per_second);
693 mutex_lock(&ua->mutex);
694 err = start_usb_capture(ua);
697 err = start_usb_playback(ua);
699 if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states))
700 stop_usb_capture(ua);
703 set_bit(ALSA_PLAYBACK_OPEN, &ua->states);
705 mutex_unlock(&ua->mutex);
709 static int capture_pcm_close(struct snd_pcm_substream *substream)
711 struct ua101 *ua = substream->private_data;
713 mutex_lock(&ua->mutex);
714 clear_bit(ALSA_CAPTURE_OPEN, &ua->states);
715 if (!test_bit(ALSA_PLAYBACK_OPEN, &ua->states))
716 stop_usb_capture(ua);
717 mutex_unlock(&ua->mutex);
721 static int playback_pcm_close(struct snd_pcm_substream *substream)
723 struct ua101 *ua = substream->private_data;
725 mutex_lock(&ua->mutex);
726 stop_usb_playback(ua);
727 clear_bit(ALSA_PLAYBACK_OPEN, &ua->states);
728 if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states))
729 stop_usb_capture(ua);
730 mutex_unlock(&ua->mutex);
734 static int capture_pcm_hw_params(struct snd_pcm_substream *substream,
735 struct snd_pcm_hw_params *hw_params)
737 struct ua101 *ua = substream->private_data;
740 mutex_lock(&ua->mutex);
741 err = start_usb_capture(ua);
742 mutex_unlock(&ua->mutex);
746 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
747 params_buffer_bytes(hw_params));
750 static int playback_pcm_hw_params(struct snd_pcm_substream *substream,
751 struct snd_pcm_hw_params *hw_params)
753 struct ua101 *ua = substream->private_data;
756 mutex_lock(&ua->mutex);
757 err = start_usb_capture(ua);
759 err = start_usb_playback(ua);
760 mutex_unlock(&ua->mutex);
764 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
765 params_buffer_bytes(hw_params));
768 static int ua101_pcm_hw_free(struct snd_pcm_substream *substream)
770 return snd_pcm_lib_free_vmalloc_buffer(substream);
773 static int capture_pcm_prepare(struct snd_pcm_substream *substream)
775 struct ua101 *ua = substream->private_data;
778 mutex_lock(&ua->mutex);
779 err = start_usb_capture(ua);
780 mutex_unlock(&ua->mutex);
785 * The EHCI driver schedules the first packet of an iso stream at 10 ms
786 * in the future, i.e., no data is actually captured for that long.
787 * Take the wait here so that the stream is known to be actually
788 * running when the start trigger has been called.
790 wait_event(ua->alsa_capture_wait,
791 test_bit(CAPTURE_URB_COMPLETED, &ua->states) ||
792 !test_bit(USB_CAPTURE_RUNNING, &ua->states));
793 if (test_bit(DISCONNECTED, &ua->states))
795 if (!test_bit(USB_CAPTURE_RUNNING, &ua->states))
798 ua->capture.period_pos = 0;
799 ua->capture.buffer_pos = 0;
803 static int playback_pcm_prepare(struct snd_pcm_substream *substream)
805 struct ua101 *ua = substream->private_data;
808 mutex_lock(&ua->mutex);
809 err = start_usb_capture(ua);
811 err = start_usb_playback(ua);
812 mutex_unlock(&ua->mutex);
816 /* see the comment in capture_pcm_prepare() */
817 wait_event(ua->alsa_playback_wait,
818 test_bit(PLAYBACK_URB_COMPLETED, &ua->states) ||
819 !test_bit(USB_PLAYBACK_RUNNING, &ua->states));
820 if (test_bit(DISCONNECTED, &ua->states))
822 if (!test_bit(USB_PLAYBACK_RUNNING, &ua->states))
825 substream->runtime->delay = 0;
826 ua->playback.period_pos = 0;
827 ua->playback.buffer_pos = 0;
831 static int capture_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
833 struct ua101 *ua = substream->private_data;
836 case SNDRV_PCM_TRIGGER_START:
837 if (!test_bit(USB_CAPTURE_RUNNING, &ua->states))
839 set_bit(ALSA_CAPTURE_RUNNING, &ua->states);
841 case SNDRV_PCM_TRIGGER_STOP:
842 clear_bit(ALSA_CAPTURE_RUNNING, &ua->states);
849 static int playback_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
851 struct ua101 *ua = substream->private_data;
854 case SNDRV_PCM_TRIGGER_START:
855 if (!test_bit(USB_PLAYBACK_RUNNING, &ua->states))
857 set_bit(ALSA_PLAYBACK_RUNNING, &ua->states);
859 case SNDRV_PCM_TRIGGER_STOP:
860 clear_bit(ALSA_PLAYBACK_RUNNING, &ua->states);
867 static inline snd_pcm_uframes_t ua101_pcm_pointer(struct ua101 *ua,
868 struct ua101_stream *stream)
873 spin_lock_irqsave(&ua->lock, flags);
874 pos = stream->buffer_pos;
875 spin_unlock_irqrestore(&ua->lock, flags);
879 static snd_pcm_uframes_t capture_pcm_pointer(struct snd_pcm_substream *subs)
881 struct ua101 *ua = subs->private_data;
883 return ua101_pcm_pointer(ua, &ua->capture);
886 static snd_pcm_uframes_t playback_pcm_pointer(struct snd_pcm_substream *subs)
888 struct ua101 *ua = subs->private_data;
890 return ua101_pcm_pointer(ua, &ua->playback);
893 static struct snd_pcm_ops capture_pcm_ops = {
894 .open = capture_pcm_open,
895 .close = capture_pcm_close,
896 .ioctl = snd_pcm_lib_ioctl,
897 .hw_params = capture_pcm_hw_params,
898 .hw_free = ua101_pcm_hw_free,
899 .prepare = capture_pcm_prepare,
900 .trigger = capture_pcm_trigger,
901 .pointer = capture_pcm_pointer,
902 .page = snd_pcm_lib_get_vmalloc_page,
903 .mmap = snd_pcm_lib_mmap_vmalloc,
906 static struct snd_pcm_ops playback_pcm_ops = {
907 .open = playback_pcm_open,
908 .close = playback_pcm_close,
909 .ioctl = snd_pcm_lib_ioctl,
910 .hw_params = playback_pcm_hw_params,
911 .hw_free = ua101_pcm_hw_free,
912 .prepare = playback_pcm_prepare,
913 .trigger = playback_pcm_trigger,
914 .pointer = playback_pcm_pointer,
915 .page = snd_pcm_lib_get_vmalloc_page,
916 .mmap = snd_pcm_lib_mmap_vmalloc,
919 static const struct uac_format_type_i_discrete_descriptor *
920 find_format_descriptor(struct usb_interface *interface)
922 struct usb_host_interface *alt;
926 if (interface->num_altsetting != 2) {
927 dev_err(&interface->dev, "invalid num_altsetting\n");
931 alt = &interface->altsetting[0];
932 if (alt->desc.bNumEndpoints != 0) {
933 dev_err(&interface->dev, "invalid bNumEndpoints\n");
937 alt = &interface->altsetting[1];
938 if (alt->desc.bNumEndpoints != 1) {
939 dev_err(&interface->dev, "invalid bNumEndpoints\n");
944 extralen = alt->extralen;
945 while (extralen >= sizeof(struct usb_descriptor_header)) {
946 struct uac_format_type_i_discrete_descriptor *desc;
948 desc = (struct uac_format_type_i_discrete_descriptor *)extra;
949 if (desc->bLength > extralen) {
950 dev_err(&interface->dev, "descriptor overflow\n");
953 if (desc->bLength == UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1) &&
954 desc->bDescriptorType == USB_DT_CS_INTERFACE &&
955 desc->bDescriptorSubtype == UAC_FORMAT_TYPE) {
956 if (desc->bFormatType != UAC_FORMAT_TYPE_I_PCM ||
957 desc->bSamFreqType != 1) {
958 dev_err(&interface->dev,
959 "invalid format type\n");
964 extralen -= desc->bLength;
965 extra += desc->bLength;
967 dev_err(&interface->dev, "sample format descriptor not found\n");
971 static int detect_usb_format(struct ua101 *ua)
973 const struct uac_format_type_i_discrete_descriptor *fmt_capture;
974 const struct uac_format_type_i_discrete_descriptor *fmt_playback;
975 const struct usb_endpoint_descriptor *epd;
978 fmt_capture = find_format_descriptor(ua->intf[INTF_CAPTURE]);
979 fmt_playback = find_format_descriptor(ua->intf[INTF_PLAYBACK]);
980 if (!fmt_capture || !fmt_playback)
983 switch (fmt_capture->bSubframeSize) {
985 ua->format_bit = SNDRV_PCM_FMTBIT_S24_3LE;
988 ua->format_bit = SNDRV_PCM_FMTBIT_S32_LE;
991 dev_err(&ua->dev->dev, "sample width is not 24 or 32 bits\n");
994 if (fmt_capture->bSubframeSize != fmt_playback->bSubframeSize) {
995 dev_err(&ua->dev->dev,
996 "playback/capture sample widths do not match\n");
1000 if (fmt_capture->bBitResolution != 24 ||
1001 fmt_playback->bBitResolution != 24) {
1002 dev_err(&ua->dev->dev, "sample width is not 24 bits\n");
1006 ua->rate = combine_triple(fmt_capture->tSamFreq[0]);
1007 rate2 = combine_triple(fmt_playback->tSamFreq[0]);
1008 if (ua->rate != rate2) {
1009 dev_err(&ua->dev->dev,
1010 "playback/capture rates do not match: %u/%u\n",
1015 switch (ua->dev->speed) {
1016 case USB_SPEED_FULL:
1017 ua->packets_per_second = 1000;
1019 case USB_SPEED_HIGH:
1020 ua->packets_per_second = 8000;
1023 dev_err(&ua->dev->dev, "unknown device speed\n");
1027 ua->capture.channels = fmt_capture->bNrChannels;
1028 ua->playback.channels = fmt_playback->bNrChannels;
1029 ua->capture.frame_bytes =
1030 fmt_capture->bSubframeSize * ua->capture.channels;
1031 ua->playback.frame_bytes =
1032 fmt_playback->bSubframeSize * ua->playback.channels;
1034 epd = &ua->intf[INTF_CAPTURE]->altsetting[1].endpoint[0].desc;
1035 if (!usb_endpoint_is_isoc_in(epd)) {
1036 dev_err(&ua->dev->dev, "invalid capture endpoint\n");
1039 ua->capture.usb_pipe = usb_rcvisocpipe(ua->dev, usb_endpoint_num(epd));
1040 ua->capture.max_packet_bytes = le16_to_cpu(epd->wMaxPacketSize);
1042 epd = &ua->intf[INTF_PLAYBACK]->altsetting[1].endpoint[0].desc;
1043 if (!usb_endpoint_is_isoc_out(epd)) {
1044 dev_err(&ua->dev->dev, "invalid playback endpoint\n");
1047 ua->playback.usb_pipe = usb_sndisocpipe(ua->dev, usb_endpoint_num(epd));
1048 ua->playback.max_packet_bytes = le16_to_cpu(epd->wMaxPacketSize);
1052 static int alloc_stream_buffers(struct ua101 *ua, struct ua101_stream *stream)
1054 unsigned int remaining_packets, packets, packets_per_page, i;
1057 stream->queue_length = queue_length;
1058 stream->queue_length = max(stream->queue_length,
1059 (unsigned int)MIN_QUEUE_LENGTH);
1060 stream->queue_length = min(stream->queue_length,
1061 (unsigned int)MAX_QUEUE_LENGTH);
1064 * The cache pool sizes used by usb_alloc_coherent() (128, 512, 2048) are
1065 * quite bad when used with the packet sizes of this device (e.g. 280,
1066 * 520, 624). Therefore, we allocate and subdivide entire pages, using
1067 * a smaller buffer only for the last chunk.
1069 remaining_packets = stream->queue_length;
1070 packets_per_page = PAGE_SIZE / stream->max_packet_bytes;
1071 for (i = 0; i < ARRAY_SIZE(stream->buffers); ++i) {
1072 packets = min(remaining_packets, packets_per_page);
1073 size = packets * stream->max_packet_bytes;
1074 stream->buffers[i].addr =
1075 usb_alloc_coherent(ua->dev, size, GFP_KERNEL,
1076 &stream->buffers[i].dma);
1077 if (!stream->buffers[i].addr)
1079 stream->buffers[i].size = size;
1080 remaining_packets -= packets;
1081 if (!remaining_packets)
1084 if (remaining_packets) {
1085 dev_err(&ua->dev->dev, "too many packets\n");
1091 static void free_stream_buffers(struct ua101 *ua, struct ua101_stream *stream)
1095 for (i = 0; i < ARRAY_SIZE(stream->buffers); ++i)
1096 usb_free_coherent(ua->dev,
1097 stream->buffers[i].size,
1098 stream->buffers[i].addr,
1099 stream->buffers[i].dma);
1102 static int alloc_stream_urbs(struct ua101 *ua, struct ua101_stream *stream,
1103 void (*urb_complete)(struct urb *))
1105 unsigned max_packet_size = stream->max_packet_bytes;
1106 struct ua101_urb *urb;
1107 unsigned int b, u = 0;
1109 for (b = 0; b < ARRAY_SIZE(stream->buffers); ++b) {
1110 unsigned int size = stream->buffers[b].size;
1111 u8 *addr = stream->buffers[b].addr;
1112 dma_addr_t dma = stream->buffers[b].dma;
1114 while (size >= max_packet_size) {
1115 if (u >= stream->queue_length)
1117 urb = kmalloc(sizeof(*urb), GFP_KERNEL);
1120 usb_init_urb(&urb->urb);
1121 urb->urb.dev = ua->dev;
1122 urb->urb.pipe = stream->usb_pipe;
1123 urb->urb.transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1124 urb->urb.transfer_buffer = addr;
1125 urb->urb.transfer_dma = dma;
1126 urb->urb.transfer_buffer_length = max_packet_size;
1127 urb->urb.number_of_packets = 1;
1128 urb->urb.interval = 1;
1129 urb->urb.context = ua;
1130 urb->urb.complete = urb_complete;
1131 urb->urb.iso_frame_desc[0].offset = 0;
1132 urb->urb.iso_frame_desc[0].length = max_packet_size;
1133 stream->urbs[u++] = urb;
1134 size -= max_packet_size;
1135 addr += max_packet_size;
1136 dma += max_packet_size;
1139 if (u == stream->queue_length)
1142 dev_err(&ua->dev->dev, "internal buffer size error\n");
1146 static void free_stream_urbs(struct ua101_stream *stream)
1150 for (i = 0; i < stream->queue_length; ++i) {
1151 kfree(stream->urbs[i]);
1152 stream->urbs[i] = NULL;
1156 static void free_usb_related_resources(struct ua101 *ua,
1157 struct usb_interface *interface)
1160 struct usb_interface *intf;
1162 mutex_lock(&ua->mutex);
1163 free_stream_urbs(&ua->capture);
1164 free_stream_urbs(&ua->playback);
1165 mutex_unlock(&ua->mutex);
1166 free_stream_buffers(ua, &ua->capture);
1167 free_stream_buffers(ua, &ua->playback);
1169 for (i = 0; i < ARRAY_SIZE(ua->intf); ++i) {
1170 mutex_lock(&ua->mutex);
1173 mutex_unlock(&ua->mutex);
1175 usb_set_intfdata(intf, NULL);
1176 if (intf != interface)
1177 usb_driver_release_interface(&ua101_driver,
1183 static void ua101_card_free(struct snd_card *card)
1185 struct ua101 *ua = card->private_data;
1187 mutex_destroy(&ua->mutex);
1190 static int ua101_probe(struct usb_interface *interface,
1191 const struct usb_device_id *usb_id)
1193 static const struct snd_usb_midi_endpoint_info midi_ep = {
1194 .out_cables = 0x0001,
1197 static const struct snd_usb_audio_quirk midi_quirk = {
1198 .type = QUIRK_MIDI_FIXED_ENDPOINT,
1201 static const int intf_numbers[2][3] = {
1203 [INTF_PLAYBACK] = 0,
1209 [INTF_PLAYBACK] = 2,
1213 struct snd_card *card;
1215 unsigned int card_index, i;
1221 is_ua1000 = usb_id->idProduct == 0x0044;
1223 if (interface->altsetting->desc.bInterfaceNumber !=
1224 intf_numbers[is_ua1000][0])
1227 mutex_lock(&devices_mutex);
1229 for (card_index = 0; card_index < SNDRV_CARDS; ++card_index)
1230 if (enable[card_index] && !(devices_used & (1 << card_index)))
1232 if (card_index >= SNDRV_CARDS) {
1233 mutex_unlock(&devices_mutex);
1236 err = snd_card_create(index[card_index], id[card_index], THIS_MODULE,
1237 sizeof(*ua), &card);
1239 mutex_unlock(&devices_mutex);
1242 card->private_free = ua101_card_free;
1243 ua = card->private_data;
1244 ua->dev = interface_to_usbdev(interface);
1246 ua->card_index = card_index;
1247 INIT_LIST_HEAD(&ua->midi_list);
1248 spin_lock_init(&ua->lock);
1249 mutex_init(&ua->mutex);
1250 INIT_LIST_HEAD(&ua->ready_playback_urbs);
1251 tasklet_init(&ua->playback_tasklet,
1252 playback_tasklet, (unsigned long)ua);
1253 init_waitqueue_head(&ua->alsa_capture_wait);
1254 init_waitqueue_head(&ua->rate_feedback_wait);
1255 init_waitqueue_head(&ua->alsa_playback_wait);
1257 ua->intf[0] = interface;
1258 for (i = 1; i < ARRAY_SIZE(ua->intf); ++i) {
1259 ua->intf[i] = usb_ifnum_to_if(ua->dev,
1260 intf_numbers[is_ua1000][i]);
1262 dev_err(&ua->dev->dev, "interface %u not found\n",
1263 intf_numbers[is_ua1000][i]);
1267 err = usb_driver_claim_interface(&ua101_driver,
1276 snd_card_set_dev(card, &interface->dev);
1278 err = detect_usb_format(ua);
1282 name = usb_id->idProduct == 0x0044 ? "UA-1000" : "UA-101";
1283 strcpy(card->driver, "UA-101");
1284 strcpy(card->shortname, name);
1285 usb_make_path(ua->dev, usb_path, sizeof(usb_path));
1286 snprintf(ua->card->longname, sizeof(ua->card->longname),
1287 "EDIROL %s (serial %s), %u Hz at %s, %s speed", name,
1288 ua->dev->serial ? ua->dev->serial : "?", ua->rate, usb_path,
1289 ua->dev->speed == USB_SPEED_HIGH ? "high" : "full");
1291 err = alloc_stream_buffers(ua, &ua->capture);
1294 err = alloc_stream_buffers(ua, &ua->playback);
1298 err = alloc_stream_urbs(ua, &ua->capture, capture_urb_complete);
1301 err = alloc_stream_urbs(ua, &ua->playback, playback_urb_complete);
1305 err = snd_pcm_new(card, name, 0, 1, 1, &ua->pcm);
1308 ua->pcm->private_data = ua;
1309 strcpy(ua->pcm->name, name);
1310 snd_pcm_set_ops(ua->pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_pcm_ops);
1311 snd_pcm_set_ops(ua->pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_pcm_ops);
1313 err = snd_usbmidi_create(card, ua->intf[INTF_MIDI],
1314 &ua->midi_list, &midi_quirk);
1318 err = snd_card_register(card);
1322 usb_set_intfdata(interface, ua);
1323 devices_used |= 1 << card_index;
1325 mutex_unlock(&devices_mutex);
1329 free_usb_related_resources(ua, interface);
1330 snd_card_free(card);
1331 mutex_unlock(&devices_mutex);
1335 static void ua101_disconnect(struct usb_interface *interface)
1337 struct ua101 *ua = usb_get_intfdata(interface);
1338 struct list_head *midi;
1343 mutex_lock(&devices_mutex);
1345 set_bit(DISCONNECTED, &ua->states);
1346 wake_up(&ua->rate_feedback_wait);
1348 /* make sure that userspace cannot create new requests */
1349 snd_card_disconnect(ua->card);
1351 /* make sure that there are no pending USB requests */
1352 list_for_each(midi, &ua->midi_list)
1353 snd_usbmidi_disconnect(midi);
1354 abort_alsa_playback(ua);
1355 abort_alsa_capture(ua);
1356 mutex_lock(&ua->mutex);
1357 stop_usb_playback(ua);
1358 stop_usb_capture(ua);
1359 mutex_unlock(&ua->mutex);
1361 free_usb_related_resources(ua, interface);
1363 devices_used &= ~(1 << ua->card_index);
1365 snd_card_free_when_closed(ua->card);
1367 mutex_unlock(&devices_mutex);
1370 static struct usb_device_id ua101_ids[] = {
1371 { USB_DEVICE(0x0582, 0x0044) }, /* UA-1000 high speed */
1372 { USB_DEVICE(0x0582, 0x007d) }, /* UA-101 high speed */
1373 { USB_DEVICE(0x0582, 0x008d) }, /* UA-101 full speed */
1376 MODULE_DEVICE_TABLE(usb, ua101_ids);
1378 static struct usb_driver ua101_driver = {
1379 .name = "snd-ua101",
1380 .id_table = ua101_ids,
1381 .probe = ua101_probe,
1382 .disconnect = ua101_disconnect,
1384 .suspend = ua101_suspend,
1385 .resume = ua101_resume,
1389 module_usb_driver(ua101_driver);