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