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