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