1 // SPDX-License-Identifier: GPL-2.0-only
3 * Edirol UA-101/UA-1000 driver
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
7 #include <linux/init.h>
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/usb.h>
11 #include <linux/usb/audio.h>
12 #include <sound/core.h>
13 #include <sound/initval.h>
14 #include <sound/pcm.h>
15 #include <sound/pcm_params.h>
16 #include "../usbaudio.h"
19 MODULE_DESCRIPTION("Edirol UA-101/1000 driver");
20 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
21 MODULE_LICENSE("GPL v2");
24 * Should not be lower than the minimum scheduling delay of the host
25 * controller. Some Intel controllers need more than one frame; as long as
26 * that driver doesn't tell us about this, use 1.5 frames just to be sure.
28 #define MIN_QUEUE_LENGTH 12
29 /* Somewhat random. */
30 #define MAX_QUEUE_LENGTH 30
32 * This magic value optimizes memory usage efficiency for the UA-101's packet
33 * sizes at all sample rates, taking into account the stupid cache pool sizes
34 * that usb_alloc_coherent() uses.
36 #define DEFAULT_QUEUE_LENGTH 21
38 #define MAX_PACKET_SIZE 672 /* hardware specific */
39 #define MAX_MEMORY_BUFFERS DIV_ROUND_UP(MAX_QUEUE_LENGTH, \
40 PAGE_SIZE / MAX_PACKET_SIZE)
42 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
43 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
44 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
45 static unsigned int queue_length = 21;
47 module_param_array(index, int, NULL, 0444);
48 MODULE_PARM_DESC(index, "card index");
49 module_param_array(id, charp, NULL, 0444);
50 MODULE_PARM_DESC(id, "ID string");
51 module_param_array(enable, bool, NULL, 0444);
52 MODULE_PARM_DESC(enable, "enable card");
53 module_param(queue_length, uint, 0644);
54 MODULE_PARM_DESC(queue_length, "USB queue length in microframes, "
55 __stringify(MIN_QUEUE_LENGTH)"-"__stringify(MAX_QUEUE_LENGTH));
65 /* bits in struct ua101::states */
72 ALSA_PLAYBACK_RUNNING,
73 CAPTURE_URB_COMPLETED,
74 PLAYBACK_URB_COMPLETED,
79 struct usb_device *dev;
80 struct snd_card *card;
81 struct usb_interface *intf[INTF_COUNT];
84 struct list_head midi_list;
87 unsigned int packets_per_second;
92 /* FIFO to synchronize playback rate to capture rate */
93 unsigned int rate_feedback_start;
94 unsigned int rate_feedback_count;
95 u8 rate_feedback[MAX_QUEUE_LENGTH];
97 struct list_head ready_playback_urbs;
98 struct work_struct playback_work;
99 wait_queue_head_t alsa_capture_wait;
100 wait_queue_head_t rate_feedback_wait;
101 wait_queue_head_t alsa_playback_wait;
102 struct ua101_stream {
103 struct snd_pcm_substream *substream;
104 unsigned int usb_pipe;
105 unsigned int channels;
106 unsigned int frame_bytes;
107 unsigned int max_packet_bytes;
108 unsigned int period_pos;
109 unsigned int buffer_pos;
110 unsigned int queue_length;
113 struct usb_iso_packet_descriptor iso_frame_desc[1];
114 struct list_head ready_list;
115 } *urbs[MAX_QUEUE_LENGTH];
120 } buffers[MAX_MEMORY_BUFFERS];
124 static DEFINE_MUTEX(devices_mutex);
125 static unsigned int devices_used;
126 static struct usb_driver ua101_driver;
128 static void abort_alsa_playback(struct ua101 *ua);
129 static void abort_alsa_capture(struct ua101 *ua);
131 static const char *usb_error_string(int err)
137 return "endpoint not enabled";
139 return "endpoint stalled";
141 return "not enough bandwidth";
143 return "device disabled";
145 return "device suspended";
150 return "internal error";
152 return "unknown error";
156 static void abort_usb_capture(struct ua101 *ua)
158 if (test_and_clear_bit(USB_CAPTURE_RUNNING, &ua->states)) {
159 wake_up(&ua->alsa_capture_wait);
160 wake_up(&ua->rate_feedback_wait);
164 static void abort_usb_playback(struct ua101 *ua)
166 if (test_and_clear_bit(USB_PLAYBACK_RUNNING, &ua->states))
167 wake_up(&ua->alsa_playback_wait);
170 static void playback_urb_complete(struct urb *usb_urb)
172 struct ua101_urb *urb = (struct ua101_urb *)usb_urb;
173 struct ua101 *ua = urb->urb.context;
176 if (unlikely(urb->urb.status == -ENOENT || /* unlinked */
177 urb->urb.status == -ENODEV || /* device removed */
178 urb->urb.status == -ECONNRESET || /* unlinked */
179 urb->urb.status == -ESHUTDOWN)) { /* device disabled */
180 abort_usb_playback(ua);
181 abort_alsa_playback(ua);
185 if (test_bit(USB_PLAYBACK_RUNNING, &ua->states)) {
186 /* append URB to FIFO */
187 spin_lock_irqsave(&ua->lock, flags);
188 list_add_tail(&urb->ready_list, &ua->ready_playback_urbs);
189 if (ua->rate_feedback_count > 0)
190 queue_work(system_highpri_wq, &ua->playback_work);
191 ua->playback.substream->runtime->delay -=
192 urb->urb.iso_frame_desc[0].length /
193 ua->playback.frame_bytes;
194 spin_unlock_irqrestore(&ua->lock, flags);
198 static void first_playback_urb_complete(struct urb *urb)
200 struct ua101 *ua = urb->context;
202 urb->complete = playback_urb_complete;
203 playback_urb_complete(urb);
205 set_bit(PLAYBACK_URB_COMPLETED, &ua->states);
206 wake_up(&ua->alsa_playback_wait);
209 /* copy data from the ALSA ring buffer into the URB buffer */
210 static bool copy_playback_data(struct ua101_stream *stream, struct urb *urb,
213 struct snd_pcm_runtime *runtime;
214 unsigned int frame_bytes, frames1;
217 runtime = stream->substream->runtime;
218 frame_bytes = stream->frame_bytes;
219 source = runtime->dma_area + stream->buffer_pos * frame_bytes;
220 if (stream->buffer_pos + frames <= runtime->buffer_size) {
221 memcpy(urb->transfer_buffer, source, frames * frame_bytes);
223 /* wrap around at end of ring buffer */
224 frames1 = runtime->buffer_size - stream->buffer_pos;
225 memcpy(urb->transfer_buffer, source, frames1 * frame_bytes);
226 memcpy(urb->transfer_buffer + frames1 * frame_bytes,
227 runtime->dma_area, (frames - frames1) * frame_bytes);
230 stream->buffer_pos += frames;
231 if (stream->buffer_pos >= runtime->buffer_size)
232 stream->buffer_pos -= runtime->buffer_size;
233 stream->period_pos += frames;
234 if (stream->period_pos >= runtime->period_size) {
235 stream->period_pos -= runtime->period_size;
241 static inline void add_with_wraparound(struct ua101 *ua,
242 unsigned int *value, unsigned int add)
245 if (*value >= ua->playback.queue_length)
246 *value -= ua->playback.queue_length;
249 static void playback_work(struct work_struct *work)
251 struct ua101 *ua = container_of(work, struct ua101, playback_work);
254 struct ua101_urb *urb;
255 bool do_period_elapsed = false;
258 if (unlikely(!test_bit(USB_PLAYBACK_RUNNING, &ua->states)))
262 * Synchronizing the playback rate to the capture rate is done by using
263 * the same sequence of packet sizes for both streams.
264 * Submitting a playback URB therefore requires both a ready URB and
265 * the size of the corresponding capture packet, i.e., both playback
266 * and capture URBs must have been completed. Since the USB core does
267 * not guarantee that playback and capture complete callbacks are
268 * called alternately, we use two FIFOs for packet sizes and read URBs;
269 * submitting playback URBs is possible as long as both FIFOs are
272 spin_lock_irqsave(&ua->lock, flags);
273 while (ua->rate_feedback_count > 0 &&
274 !list_empty(&ua->ready_playback_urbs)) {
275 /* take packet size out of FIFO */
276 frames = ua->rate_feedback[ua->rate_feedback_start];
277 add_with_wraparound(ua, &ua->rate_feedback_start, 1);
278 ua->rate_feedback_count--;
280 /* take URB out of FIFO */
281 urb = list_first_entry(&ua->ready_playback_urbs,
282 struct ua101_urb, ready_list);
283 list_del(&urb->ready_list);
285 /* fill packet with data or silence */
286 urb->urb.iso_frame_desc[0].length =
287 frames * ua->playback.frame_bytes;
288 if (test_bit(ALSA_PLAYBACK_RUNNING, &ua->states))
289 do_period_elapsed |= copy_playback_data(&ua->playback,
293 memset(urb->urb.transfer_buffer, 0,
294 urb->urb.iso_frame_desc[0].length);
296 /* and off you go ... */
297 err = usb_submit_urb(&urb->urb, GFP_ATOMIC);
298 if (unlikely(err < 0)) {
299 spin_unlock_irqrestore(&ua->lock, flags);
300 abort_usb_playback(ua);
301 abort_alsa_playback(ua);
302 dev_err(&ua->dev->dev, "USB request error %d: %s\n",
303 err, usb_error_string(err));
306 ua->playback.substream->runtime->delay += frames;
308 spin_unlock_irqrestore(&ua->lock, flags);
309 if (do_period_elapsed)
310 snd_pcm_period_elapsed(ua->playback.substream);
313 /* copy data from the URB buffer into the ALSA ring buffer */
314 static bool copy_capture_data(struct ua101_stream *stream, struct urb *urb,
317 struct snd_pcm_runtime *runtime;
318 unsigned int frame_bytes, frames1;
321 runtime = stream->substream->runtime;
322 frame_bytes = stream->frame_bytes;
323 dest = runtime->dma_area + stream->buffer_pos * frame_bytes;
324 if (stream->buffer_pos + frames <= runtime->buffer_size) {
325 memcpy(dest, urb->transfer_buffer, frames * frame_bytes);
327 /* wrap around at end of ring buffer */
328 frames1 = runtime->buffer_size - stream->buffer_pos;
329 memcpy(dest, urb->transfer_buffer, frames1 * frame_bytes);
330 memcpy(runtime->dma_area,
331 urb->transfer_buffer + frames1 * frame_bytes,
332 (frames - frames1) * frame_bytes);
335 stream->buffer_pos += frames;
336 if (stream->buffer_pos >= runtime->buffer_size)
337 stream->buffer_pos -= runtime->buffer_size;
338 stream->period_pos += frames;
339 if (stream->period_pos >= runtime->period_size) {
340 stream->period_pos -= runtime->period_size;
346 static void capture_urb_complete(struct urb *urb)
348 struct ua101 *ua = urb->context;
349 struct ua101_stream *stream = &ua->capture;
351 unsigned int frames, write_ptr;
352 bool do_period_elapsed;
355 if (unlikely(urb->status == -ENOENT || /* unlinked */
356 urb->status == -ENODEV || /* device removed */
357 urb->status == -ECONNRESET || /* unlinked */
358 urb->status == -ESHUTDOWN)) /* device disabled */
361 if (urb->status >= 0 && urb->iso_frame_desc[0].status >= 0)
362 frames = urb->iso_frame_desc[0].actual_length /
367 spin_lock_irqsave(&ua->lock, flags);
369 if (frames > 0 && test_bit(ALSA_CAPTURE_RUNNING, &ua->states))
370 do_period_elapsed = copy_capture_data(stream, urb, frames);
372 do_period_elapsed = false;
374 if (test_bit(USB_CAPTURE_RUNNING, &ua->states)) {
375 err = usb_submit_urb(urb, GFP_ATOMIC);
376 if (unlikely(err < 0)) {
377 spin_unlock_irqrestore(&ua->lock, flags);
378 dev_err(&ua->dev->dev, "USB request error %d: %s\n",
379 err, usb_error_string(err));
383 /* append packet size to FIFO */
384 write_ptr = ua->rate_feedback_start;
385 add_with_wraparound(ua, &write_ptr, ua->rate_feedback_count);
386 ua->rate_feedback[write_ptr] = frames;
387 if (ua->rate_feedback_count < ua->playback.queue_length) {
388 ua->rate_feedback_count++;
389 if (ua->rate_feedback_count ==
390 ua->playback.queue_length)
391 wake_up(&ua->rate_feedback_wait);
394 * Ring buffer overflow; this happens when the playback
395 * stream is not running. Throw away the oldest entry,
396 * so that the playback stream, when it starts, sees
397 * the most recent packet sizes.
399 add_with_wraparound(ua, &ua->rate_feedback_start, 1);
401 if (test_bit(USB_PLAYBACK_RUNNING, &ua->states) &&
402 !list_empty(&ua->ready_playback_urbs))
403 queue_work(system_highpri_wq, &ua->playback_work);
406 spin_unlock_irqrestore(&ua->lock, flags);
408 if (do_period_elapsed)
409 snd_pcm_period_elapsed(stream->substream);
414 abort_usb_playback(ua);
415 abort_usb_capture(ua);
416 abort_alsa_playback(ua);
417 abort_alsa_capture(ua);
420 static void first_capture_urb_complete(struct urb *urb)
422 struct ua101 *ua = urb->context;
424 urb->complete = capture_urb_complete;
425 capture_urb_complete(urb);
427 set_bit(CAPTURE_URB_COMPLETED, &ua->states);
428 wake_up(&ua->alsa_capture_wait);
431 static int submit_stream_urbs(struct ua101 *ua, struct ua101_stream *stream)
435 for (i = 0; i < stream->queue_length; ++i) {
436 int err = usb_submit_urb(&stream->urbs[i]->urb, GFP_KERNEL);
438 dev_err(&ua->dev->dev, "USB request error %d: %s\n",
439 err, usb_error_string(err));
446 static void kill_stream_urbs(struct ua101_stream *stream)
450 for (i = 0; i < stream->queue_length; ++i)
452 usb_kill_urb(&stream->urbs[i]->urb);
455 static int enable_iso_interface(struct ua101 *ua, unsigned int intf_index)
457 struct usb_host_interface *alts;
459 alts = ua->intf[intf_index]->cur_altsetting;
460 if (alts->desc.bAlternateSetting != 1) {
461 int err = usb_set_interface(ua->dev,
462 alts->desc.bInterfaceNumber, 1);
464 dev_err(&ua->dev->dev,
465 "cannot initialize interface; error %d: %s\n",
466 err, usb_error_string(err));
473 static void disable_iso_interface(struct ua101 *ua, unsigned int intf_index)
475 struct usb_host_interface *alts;
477 if (!ua->intf[intf_index])
480 alts = ua->intf[intf_index]->cur_altsetting;
481 if (alts->desc.bAlternateSetting != 0) {
482 int err = usb_set_interface(ua->dev,
483 alts->desc.bInterfaceNumber, 0);
484 if (err < 0 && !test_bit(DISCONNECTED, &ua->states))
485 dev_warn(&ua->dev->dev,
486 "interface reset failed; error %d: %s\n",
487 err, usb_error_string(err));
491 static void stop_usb_capture(struct ua101 *ua)
493 clear_bit(USB_CAPTURE_RUNNING, &ua->states);
495 kill_stream_urbs(&ua->capture);
497 disable_iso_interface(ua, INTF_CAPTURE);
500 static int start_usb_capture(struct ua101 *ua)
504 if (test_bit(DISCONNECTED, &ua->states))
507 if (test_bit(USB_CAPTURE_RUNNING, &ua->states))
510 kill_stream_urbs(&ua->capture);
512 err = enable_iso_interface(ua, INTF_CAPTURE);
516 clear_bit(CAPTURE_URB_COMPLETED, &ua->states);
517 ua->capture.urbs[0]->urb.complete = first_capture_urb_complete;
518 ua->rate_feedback_start = 0;
519 ua->rate_feedback_count = 0;
521 set_bit(USB_CAPTURE_RUNNING, &ua->states);
522 err = submit_stream_urbs(ua, &ua->capture);
524 stop_usb_capture(ua);
528 static void stop_usb_playback(struct ua101 *ua)
530 clear_bit(USB_PLAYBACK_RUNNING, &ua->states);
532 kill_stream_urbs(&ua->playback);
534 cancel_work_sync(&ua->playback_work);
536 disable_iso_interface(ua, INTF_PLAYBACK);
539 static int start_usb_playback(struct ua101 *ua)
541 unsigned int i, frames;
545 if (test_bit(DISCONNECTED, &ua->states))
548 if (test_bit(USB_PLAYBACK_RUNNING, &ua->states))
551 kill_stream_urbs(&ua->playback);
552 cancel_work_sync(&ua->playback_work);
554 err = enable_iso_interface(ua, INTF_PLAYBACK);
558 clear_bit(PLAYBACK_URB_COMPLETED, &ua->states);
559 ua->playback.urbs[0]->urb.complete =
560 first_playback_urb_complete;
561 spin_lock_irq(&ua->lock);
562 INIT_LIST_HEAD(&ua->ready_playback_urbs);
563 spin_unlock_irq(&ua->lock);
566 * We submit the initial URBs all at once, so we have to wait for the
567 * packet size FIFO to be full.
569 wait_event(ua->rate_feedback_wait,
570 ua->rate_feedback_count >= ua->playback.queue_length ||
571 !test_bit(USB_CAPTURE_RUNNING, &ua->states) ||
572 test_bit(DISCONNECTED, &ua->states));
573 if (test_bit(DISCONNECTED, &ua->states)) {
574 stop_usb_playback(ua);
577 if (!test_bit(USB_CAPTURE_RUNNING, &ua->states)) {
578 stop_usb_playback(ua);
582 for (i = 0; i < ua->playback.queue_length; ++i) {
583 /* all initial URBs contain silence */
584 spin_lock_irq(&ua->lock);
585 frames = ua->rate_feedback[ua->rate_feedback_start];
586 add_with_wraparound(ua, &ua->rate_feedback_start, 1);
587 ua->rate_feedback_count--;
588 spin_unlock_irq(&ua->lock);
589 urb = &ua->playback.urbs[i]->urb;
590 urb->iso_frame_desc[0].length =
591 frames * ua->playback.frame_bytes;
592 memset(urb->transfer_buffer, 0,
593 urb->iso_frame_desc[0].length);
596 set_bit(USB_PLAYBACK_RUNNING, &ua->states);
597 err = submit_stream_urbs(ua, &ua->playback);
599 stop_usb_playback(ua);
603 static void abort_alsa_capture(struct ua101 *ua)
605 if (test_bit(ALSA_CAPTURE_RUNNING, &ua->states))
606 snd_pcm_stop_xrun(ua->capture.substream);
609 static void abort_alsa_playback(struct ua101 *ua)
611 if (test_bit(ALSA_PLAYBACK_RUNNING, &ua->states))
612 snd_pcm_stop_xrun(ua->playback.substream);
615 static int set_stream_hw(struct ua101 *ua, struct snd_pcm_substream *substream,
616 unsigned int channels)
620 substream->runtime->hw.info =
621 SNDRV_PCM_INFO_MMAP |
622 SNDRV_PCM_INFO_MMAP_VALID |
623 SNDRV_PCM_INFO_BATCH |
624 SNDRV_PCM_INFO_INTERLEAVED |
625 SNDRV_PCM_INFO_BLOCK_TRANSFER |
626 SNDRV_PCM_INFO_FIFO_IN_FRAMES;
627 substream->runtime->hw.formats = ua->format_bit;
628 substream->runtime->hw.rates = snd_pcm_rate_to_rate_bit(ua->rate);
629 substream->runtime->hw.rate_min = ua->rate;
630 substream->runtime->hw.rate_max = ua->rate;
631 substream->runtime->hw.channels_min = channels;
632 substream->runtime->hw.channels_max = channels;
633 substream->runtime->hw.buffer_bytes_max = 45000 * 1024;
634 substream->runtime->hw.period_bytes_min = 1;
635 substream->runtime->hw.period_bytes_max = UINT_MAX;
636 substream->runtime->hw.periods_min = 2;
637 substream->runtime->hw.periods_max = UINT_MAX;
638 err = snd_pcm_hw_constraint_minmax(substream->runtime,
639 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
640 1500000 / ua->packets_per_second,
644 err = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 32, 24);
648 static int capture_pcm_open(struct snd_pcm_substream *substream)
650 struct ua101 *ua = substream->private_data;
653 ua->capture.substream = substream;
654 err = set_stream_hw(ua, substream, ua->capture.channels);
657 substream->runtime->hw.fifo_size =
658 DIV_ROUND_CLOSEST(ua->rate, ua->packets_per_second);
659 substream->runtime->delay = substream->runtime->hw.fifo_size;
661 mutex_lock(&ua->mutex);
662 err = start_usb_capture(ua);
664 set_bit(ALSA_CAPTURE_OPEN, &ua->states);
665 mutex_unlock(&ua->mutex);
669 static int playback_pcm_open(struct snd_pcm_substream *substream)
671 struct ua101 *ua = substream->private_data;
674 ua->playback.substream = substream;
675 err = set_stream_hw(ua, substream, ua->playback.channels);
678 substream->runtime->hw.fifo_size =
679 DIV_ROUND_CLOSEST(ua->rate * ua->playback.queue_length,
680 ua->packets_per_second);
682 mutex_lock(&ua->mutex);
683 err = start_usb_capture(ua);
686 err = start_usb_playback(ua);
688 if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states))
689 stop_usb_capture(ua);
692 set_bit(ALSA_PLAYBACK_OPEN, &ua->states);
694 mutex_unlock(&ua->mutex);
698 static int capture_pcm_close(struct snd_pcm_substream *substream)
700 struct ua101 *ua = substream->private_data;
702 mutex_lock(&ua->mutex);
703 clear_bit(ALSA_CAPTURE_OPEN, &ua->states);
704 if (!test_bit(ALSA_PLAYBACK_OPEN, &ua->states))
705 stop_usb_capture(ua);
706 mutex_unlock(&ua->mutex);
710 static int playback_pcm_close(struct snd_pcm_substream *substream)
712 struct ua101 *ua = substream->private_data;
714 mutex_lock(&ua->mutex);
715 stop_usb_playback(ua);
716 clear_bit(ALSA_PLAYBACK_OPEN, &ua->states);
717 if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states))
718 stop_usb_capture(ua);
719 mutex_unlock(&ua->mutex);
723 static int capture_pcm_hw_params(struct snd_pcm_substream *substream,
724 struct snd_pcm_hw_params *hw_params)
726 struct ua101 *ua = substream->private_data;
729 mutex_lock(&ua->mutex);
730 err = start_usb_capture(ua);
731 mutex_unlock(&ua->mutex);
735 static int playback_pcm_hw_params(struct snd_pcm_substream *substream,
736 struct snd_pcm_hw_params *hw_params)
738 struct ua101 *ua = substream->private_data;
741 mutex_lock(&ua->mutex);
742 err = start_usb_capture(ua);
744 err = start_usb_playback(ua);
745 mutex_unlock(&ua->mutex);
749 static int capture_pcm_prepare(struct snd_pcm_substream *substream)
751 struct ua101 *ua = substream->private_data;
754 mutex_lock(&ua->mutex);
755 err = start_usb_capture(ua);
756 mutex_unlock(&ua->mutex);
761 * The EHCI driver schedules the first packet of an iso stream at 10 ms
762 * in the future, i.e., no data is actually captured for that long.
763 * Take the wait here so that the stream is known to be actually
764 * running when the start trigger has been called.
766 wait_event(ua->alsa_capture_wait,
767 test_bit(CAPTURE_URB_COMPLETED, &ua->states) ||
768 !test_bit(USB_CAPTURE_RUNNING, &ua->states));
769 if (test_bit(DISCONNECTED, &ua->states))
771 if (!test_bit(USB_CAPTURE_RUNNING, &ua->states))
774 ua->capture.period_pos = 0;
775 ua->capture.buffer_pos = 0;
779 static int playback_pcm_prepare(struct snd_pcm_substream *substream)
781 struct ua101 *ua = substream->private_data;
784 mutex_lock(&ua->mutex);
785 err = start_usb_capture(ua);
787 err = start_usb_playback(ua);
788 mutex_unlock(&ua->mutex);
792 /* see the comment in capture_pcm_prepare() */
793 wait_event(ua->alsa_playback_wait,
794 test_bit(PLAYBACK_URB_COMPLETED, &ua->states) ||
795 !test_bit(USB_PLAYBACK_RUNNING, &ua->states));
796 if (test_bit(DISCONNECTED, &ua->states))
798 if (!test_bit(USB_PLAYBACK_RUNNING, &ua->states))
801 substream->runtime->delay = 0;
802 ua->playback.period_pos = 0;
803 ua->playback.buffer_pos = 0;
807 static int capture_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
809 struct ua101 *ua = substream->private_data;
812 case SNDRV_PCM_TRIGGER_START:
813 if (!test_bit(USB_CAPTURE_RUNNING, &ua->states))
815 set_bit(ALSA_CAPTURE_RUNNING, &ua->states);
817 case SNDRV_PCM_TRIGGER_STOP:
818 clear_bit(ALSA_CAPTURE_RUNNING, &ua->states);
825 static int playback_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
827 struct ua101 *ua = substream->private_data;
830 case SNDRV_PCM_TRIGGER_START:
831 if (!test_bit(USB_PLAYBACK_RUNNING, &ua->states))
833 set_bit(ALSA_PLAYBACK_RUNNING, &ua->states);
835 case SNDRV_PCM_TRIGGER_STOP:
836 clear_bit(ALSA_PLAYBACK_RUNNING, &ua->states);
843 static inline snd_pcm_uframes_t ua101_pcm_pointer(struct ua101 *ua,
844 struct ua101_stream *stream)
849 spin_lock_irqsave(&ua->lock, flags);
850 pos = stream->buffer_pos;
851 spin_unlock_irqrestore(&ua->lock, flags);
855 static snd_pcm_uframes_t capture_pcm_pointer(struct snd_pcm_substream *subs)
857 struct ua101 *ua = subs->private_data;
859 return ua101_pcm_pointer(ua, &ua->capture);
862 static snd_pcm_uframes_t playback_pcm_pointer(struct snd_pcm_substream *subs)
864 struct ua101 *ua = subs->private_data;
866 return ua101_pcm_pointer(ua, &ua->playback);
869 static const struct snd_pcm_ops capture_pcm_ops = {
870 .open = capture_pcm_open,
871 .close = capture_pcm_close,
872 .hw_params = capture_pcm_hw_params,
873 .prepare = capture_pcm_prepare,
874 .trigger = capture_pcm_trigger,
875 .pointer = capture_pcm_pointer,
878 static const struct snd_pcm_ops playback_pcm_ops = {
879 .open = playback_pcm_open,
880 .close = playback_pcm_close,
881 .hw_params = playback_pcm_hw_params,
882 .prepare = playback_pcm_prepare,
883 .trigger = playback_pcm_trigger,
884 .pointer = playback_pcm_pointer,
887 static const struct uac_format_type_i_discrete_descriptor *
888 find_format_descriptor(struct usb_interface *interface)
890 struct usb_host_interface *alt;
894 if (interface->num_altsetting != 2) {
895 dev_err(&interface->dev, "invalid num_altsetting\n");
899 alt = &interface->altsetting[0];
900 if (alt->desc.bNumEndpoints != 0) {
901 dev_err(&interface->dev, "invalid bNumEndpoints\n");
905 alt = &interface->altsetting[1];
906 if (alt->desc.bNumEndpoints != 1) {
907 dev_err(&interface->dev, "invalid bNumEndpoints\n");
912 extralen = alt->extralen;
913 while (extralen >= sizeof(struct usb_descriptor_header)) {
914 struct uac_format_type_i_discrete_descriptor *desc;
916 desc = (struct uac_format_type_i_discrete_descriptor *)extra;
917 if (desc->bLength > extralen) {
918 dev_err(&interface->dev, "descriptor overflow\n");
921 if (desc->bLength == UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1) &&
922 desc->bDescriptorType == USB_DT_CS_INTERFACE &&
923 desc->bDescriptorSubtype == UAC_FORMAT_TYPE) {
924 if (desc->bFormatType != UAC_FORMAT_TYPE_I_PCM ||
925 desc->bSamFreqType != 1) {
926 dev_err(&interface->dev,
927 "invalid format type\n");
932 extralen -= desc->bLength;
933 extra += desc->bLength;
935 dev_err(&interface->dev, "sample format descriptor not found\n");
939 static int detect_usb_format(struct ua101 *ua)
941 const struct uac_format_type_i_discrete_descriptor *fmt_capture;
942 const struct uac_format_type_i_discrete_descriptor *fmt_playback;
943 const struct usb_endpoint_descriptor *epd;
946 fmt_capture = find_format_descriptor(ua->intf[INTF_CAPTURE]);
947 fmt_playback = find_format_descriptor(ua->intf[INTF_PLAYBACK]);
948 if (!fmt_capture || !fmt_playback)
951 switch (fmt_capture->bSubframeSize) {
953 ua->format_bit = SNDRV_PCM_FMTBIT_S24_3LE;
956 ua->format_bit = SNDRV_PCM_FMTBIT_S32_LE;
959 dev_err(&ua->dev->dev, "sample width is not 24 or 32 bits\n");
962 if (fmt_capture->bSubframeSize != fmt_playback->bSubframeSize) {
963 dev_err(&ua->dev->dev,
964 "playback/capture sample widths do not match\n");
968 if (fmt_capture->bBitResolution != 24 ||
969 fmt_playback->bBitResolution != 24) {
970 dev_err(&ua->dev->dev, "sample width is not 24 bits\n");
974 ua->rate = combine_triple(fmt_capture->tSamFreq[0]);
975 rate2 = combine_triple(fmt_playback->tSamFreq[0]);
976 if (ua->rate != rate2) {
977 dev_err(&ua->dev->dev,
978 "playback/capture rates do not match: %u/%u\n",
983 switch (ua->dev->speed) {
985 ua->packets_per_second = 1000;
988 ua->packets_per_second = 8000;
991 dev_err(&ua->dev->dev, "unknown device speed\n");
995 ua->capture.channels = fmt_capture->bNrChannels;
996 ua->playback.channels = fmt_playback->bNrChannels;
997 ua->capture.frame_bytes =
998 fmt_capture->bSubframeSize * ua->capture.channels;
999 ua->playback.frame_bytes =
1000 fmt_playback->bSubframeSize * ua->playback.channels;
1002 epd = &ua->intf[INTF_CAPTURE]->altsetting[1].endpoint[0].desc;
1003 if (!usb_endpoint_is_isoc_in(epd) || usb_endpoint_maxp(epd) == 0) {
1004 dev_err(&ua->dev->dev, "invalid capture endpoint\n");
1007 ua->capture.usb_pipe = usb_rcvisocpipe(ua->dev, usb_endpoint_num(epd));
1008 ua->capture.max_packet_bytes = usb_endpoint_maxp(epd);
1010 epd = &ua->intf[INTF_PLAYBACK]->altsetting[1].endpoint[0].desc;
1011 if (!usb_endpoint_is_isoc_out(epd) || usb_endpoint_maxp(epd) == 0) {
1012 dev_err(&ua->dev->dev, "invalid playback endpoint\n");
1015 ua->playback.usb_pipe = usb_sndisocpipe(ua->dev, usb_endpoint_num(epd));
1016 ua->playback.max_packet_bytes = usb_endpoint_maxp(epd);
1020 static int alloc_stream_buffers(struct ua101 *ua, struct ua101_stream *stream)
1022 unsigned int remaining_packets, packets, packets_per_page, i;
1025 stream->queue_length = queue_length;
1026 stream->queue_length = max(stream->queue_length,
1027 (unsigned int)MIN_QUEUE_LENGTH);
1028 stream->queue_length = min(stream->queue_length,
1029 (unsigned int)MAX_QUEUE_LENGTH);
1032 * The cache pool sizes used by usb_alloc_coherent() (128, 512, 2048) are
1033 * quite bad when used with the packet sizes of this device (e.g. 280,
1034 * 520, 624). Therefore, we allocate and subdivide entire pages, using
1035 * a smaller buffer only for the last chunk.
1037 remaining_packets = stream->queue_length;
1038 packets_per_page = PAGE_SIZE / stream->max_packet_bytes;
1039 for (i = 0; i < ARRAY_SIZE(stream->buffers); ++i) {
1040 packets = min(remaining_packets, packets_per_page);
1041 size = packets * stream->max_packet_bytes;
1042 stream->buffers[i].addr =
1043 usb_alloc_coherent(ua->dev, size, GFP_KERNEL,
1044 &stream->buffers[i].dma);
1045 if (!stream->buffers[i].addr)
1047 stream->buffers[i].size = size;
1048 remaining_packets -= packets;
1049 if (!remaining_packets)
1052 if (remaining_packets) {
1053 dev_err(&ua->dev->dev, "too many packets\n");
1059 static void free_stream_buffers(struct ua101 *ua, struct ua101_stream *stream)
1063 for (i = 0; i < ARRAY_SIZE(stream->buffers); ++i)
1064 usb_free_coherent(ua->dev,
1065 stream->buffers[i].size,
1066 stream->buffers[i].addr,
1067 stream->buffers[i].dma);
1070 static int alloc_stream_urbs(struct ua101 *ua, struct ua101_stream *stream,
1071 void (*urb_complete)(struct urb *))
1073 unsigned max_packet_size = stream->max_packet_bytes;
1074 struct ua101_urb *urb;
1075 unsigned int b, u = 0;
1077 for (b = 0; b < ARRAY_SIZE(stream->buffers); ++b) {
1078 unsigned int size = stream->buffers[b].size;
1079 u8 *addr = stream->buffers[b].addr;
1080 dma_addr_t dma = stream->buffers[b].dma;
1082 while (size >= max_packet_size) {
1083 if (u >= stream->queue_length)
1085 urb = kmalloc(sizeof(*urb), GFP_KERNEL);
1088 usb_init_urb(&urb->urb);
1089 urb->urb.dev = ua->dev;
1090 urb->urb.pipe = stream->usb_pipe;
1091 urb->urb.transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1092 urb->urb.transfer_buffer = addr;
1093 urb->urb.transfer_dma = dma;
1094 urb->urb.transfer_buffer_length = max_packet_size;
1095 urb->urb.number_of_packets = 1;
1096 urb->urb.interval = 1;
1097 urb->urb.context = ua;
1098 urb->urb.complete = urb_complete;
1099 urb->urb.iso_frame_desc[0].offset = 0;
1100 urb->urb.iso_frame_desc[0].length = max_packet_size;
1101 stream->urbs[u++] = urb;
1102 size -= max_packet_size;
1103 addr += max_packet_size;
1104 dma += max_packet_size;
1107 if (u == stream->queue_length)
1110 dev_err(&ua->dev->dev, "internal buffer size error\n");
1114 static void free_stream_urbs(struct ua101_stream *stream)
1118 for (i = 0; i < stream->queue_length; ++i) {
1119 kfree(stream->urbs[i]);
1120 stream->urbs[i] = NULL;
1124 static void free_usb_related_resources(struct ua101 *ua,
1125 struct usb_interface *interface)
1128 struct usb_interface *intf;
1130 mutex_lock(&ua->mutex);
1131 free_stream_urbs(&ua->capture);
1132 free_stream_urbs(&ua->playback);
1133 mutex_unlock(&ua->mutex);
1134 free_stream_buffers(ua, &ua->capture);
1135 free_stream_buffers(ua, &ua->playback);
1137 for (i = 0; i < ARRAY_SIZE(ua->intf); ++i) {
1138 mutex_lock(&ua->mutex);
1141 mutex_unlock(&ua->mutex);
1143 usb_set_intfdata(intf, NULL);
1144 if (intf != interface)
1145 usb_driver_release_interface(&ua101_driver,
1151 static void ua101_card_free(struct snd_card *card)
1153 struct ua101 *ua = card->private_data;
1155 mutex_destroy(&ua->mutex);
1158 static int ua101_probe(struct usb_interface *interface,
1159 const struct usb_device_id *usb_id)
1161 static const struct snd_usb_midi_endpoint_info midi_ep = {
1162 .out_cables = 0x0001,
1165 static const struct snd_usb_audio_quirk midi_quirk = {
1166 .type = QUIRK_MIDI_FIXED_ENDPOINT,
1169 static const int intf_numbers[2][3] = {
1171 [INTF_PLAYBACK] = 0,
1177 [INTF_PLAYBACK] = 2,
1181 struct snd_card *card;
1183 unsigned int card_index, i;
1189 is_ua1000 = usb_id->idProduct == 0x0044;
1191 if (interface->altsetting->desc.bInterfaceNumber !=
1192 intf_numbers[is_ua1000][0])
1195 mutex_lock(&devices_mutex);
1197 for (card_index = 0; card_index < SNDRV_CARDS; ++card_index)
1198 if (enable[card_index] && !(devices_used & (1 << card_index)))
1200 if (card_index >= SNDRV_CARDS) {
1201 mutex_unlock(&devices_mutex);
1204 err = snd_card_new(&interface->dev,
1205 index[card_index], id[card_index], THIS_MODULE,
1206 sizeof(*ua), &card);
1208 mutex_unlock(&devices_mutex);
1211 card->private_free = ua101_card_free;
1212 ua = card->private_data;
1213 ua->dev = interface_to_usbdev(interface);
1215 ua->card_index = card_index;
1216 INIT_LIST_HEAD(&ua->midi_list);
1217 spin_lock_init(&ua->lock);
1218 mutex_init(&ua->mutex);
1219 INIT_LIST_HEAD(&ua->ready_playback_urbs);
1220 INIT_WORK(&ua->playback_work, playback_work);
1221 init_waitqueue_head(&ua->alsa_capture_wait);
1222 init_waitqueue_head(&ua->rate_feedback_wait);
1223 init_waitqueue_head(&ua->alsa_playback_wait);
1225 ua->intf[0] = interface;
1226 for (i = 1; i < ARRAY_SIZE(ua->intf); ++i) {
1227 ua->intf[i] = usb_ifnum_to_if(ua->dev,
1228 intf_numbers[is_ua1000][i]);
1230 dev_err(&ua->dev->dev, "interface %u not found\n",
1231 intf_numbers[is_ua1000][i]);
1235 err = usb_driver_claim_interface(&ua101_driver,
1244 err = detect_usb_format(ua);
1248 name = usb_id->idProduct == 0x0044 ? "UA-1000" : "UA-101";
1249 strcpy(card->driver, "UA-101");
1250 strcpy(card->shortname, name);
1251 usb_make_path(ua->dev, usb_path, sizeof(usb_path));
1252 snprintf(ua->card->longname, sizeof(ua->card->longname),
1253 "EDIROL %s (serial %s), %u Hz at %s, %s speed", name,
1254 ua->dev->serial ? ua->dev->serial : "?", ua->rate, usb_path,
1255 ua->dev->speed == USB_SPEED_HIGH ? "high" : "full");
1257 err = alloc_stream_buffers(ua, &ua->capture);
1260 err = alloc_stream_buffers(ua, &ua->playback);
1264 err = alloc_stream_urbs(ua, &ua->capture, capture_urb_complete);
1267 err = alloc_stream_urbs(ua, &ua->playback, playback_urb_complete);
1271 err = snd_pcm_new(card, name, 0, 1, 1, &ua->pcm);
1274 ua->pcm->private_data = ua;
1275 strcpy(ua->pcm->name, name);
1276 snd_pcm_set_ops(ua->pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_pcm_ops);
1277 snd_pcm_set_ops(ua->pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_pcm_ops);
1278 snd_pcm_set_managed_buffer_all(ua->pcm, SNDRV_DMA_TYPE_VMALLOC,
1281 err = snd_usbmidi_create(card, ua->intf[INTF_MIDI],
1282 &ua->midi_list, &midi_quirk);
1286 err = snd_card_register(card);
1290 usb_set_intfdata(interface, ua);
1291 devices_used |= 1 << card_index;
1293 mutex_unlock(&devices_mutex);
1297 free_usb_related_resources(ua, interface);
1298 snd_card_free(card);
1299 mutex_unlock(&devices_mutex);
1303 static void ua101_disconnect(struct usb_interface *interface)
1305 struct ua101 *ua = usb_get_intfdata(interface);
1306 struct list_head *midi;
1311 mutex_lock(&devices_mutex);
1313 set_bit(DISCONNECTED, &ua->states);
1314 wake_up(&ua->rate_feedback_wait);
1316 /* make sure that userspace cannot create new requests */
1317 snd_card_disconnect(ua->card);
1319 /* make sure that there are no pending USB requests */
1320 list_for_each(midi, &ua->midi_list)
1321 snd_usbmidi_disconnect(midi);
1322 abort_alsa_playback(ua);
1323 abort_alsa_capture(ua);
1324 mutex_lock(&ua->mutex);
1325 stop_usb_playback(ua);
1326 stop_usb_capture(ua);
1327 mutex_unlock(&ua->mutex);
1329 free_usb_related_resources(ua, interface);
1331 devices_used &= ~(1 << ua->card_index);
1333 snd_card_free_when_closed(ua->card);
1335 mutex_unlock(&devices_mutex);
1338 static const struct usb_device_id ua101_ids[] = {
1339 { USB_DEVICE(0x0582, 0x0044) }, /* UA-1000 high speed */
1340 { USB_DEVICE(0x0582, 0x007d) }, /* UA-101 high speed */
1341 { USB_DEVICE(0x0582, 0x008d) }, /* UA-101 full speed */
1344 MODULE_DEVICE_TABLE(usb, ua101_ids);
1346 static struct usb_driver ua101_driver = {
1347 .name = "snd-ua101",
1348 .id_table = ua101_ids,
1349 .probe = ua101_probe,
1350 .disconnect = ua101_disconnect,
1352 .suspend = ua101_suspend,
1353 .resume = ua101_resume,
1357 module_usb_driver(ua101_driver);