xref: /linux/sound/usb/misc/ua101.c (revision 07b01b0d8ac4b5f89cbe74e52376221f21db260d)
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 	return 0;
1011 }
1012 
1013 static int alloc_stream_buffers(struct ua101 *ua, struct ua101_stream *stream)
1014 {
1015 	unsigned int remaining_packets, packets, packets_per_page, i;
1016 	size_t size;
1017 
1018 	stream->queue_length = queue_length;
1019 	stream->queue_length = max(stream->queue_length,
1020 				   (unsigned int)MIN_QUEUE_LENGTH);
1021 	stream->queue_length = min(stream->queue_length,
1022 				   (unsigned int)MAX_QUEUE_LENGTH);
1023 
1024 	/*
1025 	 * The cache pool sizes used by usb_alloc_coherent() (128, 512, 2048) are
1026 	 * quite bad when used with the packet sizes of this device (e.g. 280,
1027 	 * 520, 624).  Therefore, we allocate and subdivide entire pages, using
1028 	 * a smaller buffer only for the last chunk.
1029 	 */
1030 	remaining_packets = stream->queue_length;
1031 	packets_per_page = PAGE_SIZE / stream->max_packet_bytes;
1032 	for (i = 0; i < ARRAY_SIZE(stream->buffers); ++i) {
1033 		packets = min(remaining_packets, packets_per_page);
1034 		size = packets * stream->max_packet_bytes;
1035 		stream->buffers[i].addr =
1036 			usb_alloc_coherent(ua->dev, size, GFP_KERNEL,
1037 					   &stream->buffers[i].dma);
1038 		if (!stream->buffers[i].addr)
1039 			return -ENOMEM;
1040 		stream->buffers[i].size = size;
1041 		remaining_packets -= packets;
1042 		if (!remaining_packets)
1043 			break;
1044 	}
1045 	if (remaining_packets) {
1046 		dev_err(&ua->dev->dev, "too many packets\n");
1047 		return -ENXIO;
1048 	}
1049 	return 0;
1050 }
1051 
1052 static void free_stream_buffers(struct ua101 *ua, struct ua101_stream *stream)
1053 {
1054 	unsigned int i;
1055 
1056 	for (i = 0; i < ARRAY_SIZE(stream->buffers); ++i)
1057 		usb_free_coherent(ua->dev,
1058 				  stream->buffers[i].size,
1059 				  stream->buffers[i].addr,
1060 				  stream->buffers[i].dma);
1061 }
1062 
1063 static int alloc_stream_urbs(struct ua101 *ua, struct ua101_stream *stream,
1064 			     void (*urb_complete)(struct urb *))
1065 {
1066 	unsigned max_packet_size = stream->max_packet_bytes;
1067 	struct urb *urb;
1068 	unsigned int b, u = 0;
1069 
1070 	for (b = 0; b < ARRAY_SIZE(stream->buffers); ++b) {
1071 		unsigned int size = stream->buffers[b].size;
1072 		u8 *addr = stream->buffers[b].addr;
1073 		dma_addr_t dma = stream->buffers[b].dma;
1074 
1075 		while (size >= max_packet_size) {
1076 			if (u >= stream->queue_length)
1077 				goto bufsize_error;
1078 			urb = usb_alloc_urb(1, GFP_KERNEL);
1079 			if (!urb)
1080 				return -ENOMEM;
1081 			urb->dev = ua->dev;
1082 			urb->pipe = stream->usb_pipe;
1083 			urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1084 			urb->transfer_buffer = addr;
1085 			urb->transfer_dma = dma;
1086 			urb->transfer_buffer_length = max_packet_size;
1087 			urb->number_of_packets = 1;
1088 			urb->interval = 1;
1089 			urb->context = &stream->urbs[u];
1090 			urb->complete = urb_complete;
1091 			urb->iso_frame_desc[0].offset = 0;
1092 			urb->iso_frame_desc[0].length = max_packet_size;
1093 			stream->urbs[u].ua = ua;
1094 			stream->urbs[u].urb = urb;
1095 			u++;
1096 			size -= max_packet_size;
1097 			addr += max_packet_size;
1098 			dma += max_packet_size;
1099 		}
1100 	}
1101 	if (u == stream->queue_length)
1102 		return 0;
1103 bufsize_error:
1104 	dev_err(&ua->dev->dev, "internal buffer size error\n");
1105 	return -ENXIO;
1106 }
1107 
1108 static void free_stream_urbs(struct ua101_stream *stream)
1109 {
1110 	unsigned int i;
1111 
1112 	for (i = 0; i < stream->queue_length; ++i) {
1113 		usb_free_urb(stream->urbs[i].urb);
1114 		stream->urbs[i].urb = NULL;
1115 	}
1116 }
1117 
1118 static void free_usb_related_resources(struct ua101 *ua,
1119 				       struct usb_interface *interface)
1120 {
1121 	unsigned int i;
1122 	struct usb_interface *intf;
1123 
1124 	scoped_guard(mutex, &ua->mutex) {
1125 		free_stream_urbs(&ua->capture);
1126 		free_stream_urbs(&ua->playback);
1127 	}
1128 	free_stream_buffers(ua, &ua->capture);
1129 	free_stream_buffers(ua, &ua->playback);
1130 
1131 	for (i = 0; i < ARRAY_SIZE(ua->intf); ++i) {
1132 		scoped_guard(mutex, &ua->mutex) {
1133 			intf = ua->intf[i];
1134 			ua->intf[i] = NULL;
1135 		}
1136 		if (intf) {
1137 			usb_set_intfdata(intf, NULL);
1138 			if (intf != interface)
1139 				usb_driver_release_interface(&ua101_driver,
1140 							     intf);
1141 		}
1142 	}
1143 }
1144 
1145 static void ua101_card_free(struct snd_card *card)
1146 {
1147 	struct ua101 *ua = card->private_data;
1148 
1149 	mutex_destroy(&ua->mutex);
1150 }
1151 
1152 static int ua101_probe(struct usb_interface *interface,
1153 		       const struct usb_device_id *usb_id)
1154 {
1155 	static const struct snd_usb_midi_endpoint_info midi_ep = {
1156 		.out_cables = 0x0001,
1157 		.in_cables = 0x0001
1158 	};
1159 	static const struct snd_usb_audio_quirk midi_quirk = {
1160 		.type = QUIRK_MIDI_FIXED_ENDPOINT,
1161 		.data = &midi_ep
1162 	};
1163 	static const int intf_numbers[2][3] = {
1164 		{	/* UA-101 */
1165 			[INTF_PLAYBACK] = 0,
1166 			[INTF_CAPTURE] = 1,
1167 			[INTF_MIDI] = 2,
1168 		},
1169 		{	/* UA-1000 */
1170 			[INTF_CAPTURE] = 1,
1171 			[INTF_PLAYBACK] = 2,
1172 			[INTF_MIDI] = 3,
1173 		},
1174 	};
1175 	struct snd_card *card;
1176 	struct ua101 *ua;
1177 	unsigned int card_index, i;
1178 	int is_ua1000;
1179 	const char *name;
1180 	char usb_path[32];
1181 	int err;
1182 
1183 	is_ua1000 = usb_id->idProduct == 0x0044;
1184 
1185 	if (interface->altsetting->desc.bInterfaceNumber !=
1186 	    intf_numbers[is_ua1000][0])
1187 		return -ENODEV;
1188 
1189 	guard(mutex)(&devices_mutex);
1190 
1191 	for (card_index = 0; card_index < SNDRV_CARDS; ++card_index)
1192 		if (enable[card_index] && !(devices_used & (1 << card_index)))
1193 			break;
1194 	if (card_index >= SNDRV_CARDS)
1195 		return -ENOENT;
1196 	err = snd_card_new(&interface->dev,
1197 			   index[card_index], id[card_index], THIS_MODULE,
1198 			   sizeof(*ua), &card);
1199 	if (err < 0)
1200 		return err;
1201 	card->private_free = ua101_card_free;
1202 	ua = card->private_data;
1203 	ua->dev = interface_to_usbdev(interface);
1204 	ua->card = card;
1205 	ua->card_index = card_index;
1206 	INIT_LIST_HEAD(&ua->midi_list);
1207 	spin_lock_init(&ua->lock);
1208 	mutex_init(&ua->mutex);
1209 	INIT_LIST_HEAD(&ua->ready_playback_urbs);
1210 	INIT_WORK(&ua->playback_work, playback_work);
1211 	init_waitqueue_head(&ua->alsa_capture_wait);
1212 	init_waitqueue_head(&ua->rate_feedback_wait);
1213 	init_waitqueue_head(&ua->alsa_playback_wait);
1214 
1215 	ua->intf[0] = interface;
1216 	for (i = 1; i < ARRAY_SIZE(ua->intf); ++i) {
1217 		ua->intf[i] = usb_ifnum_to_if(ua->dev,
1218 					      intf_numbers[is_ua1000][i]);
1219 		if (!ua->intf[i]) {
1220 			dev_err(&ua->dev->dev, "interface %u not found\n",
1221 				intf_numbers[is_ua1000][i]);
1222 			err = -ENXIO;
1223 			goto probe_error;
1224 		}
1225 		err = usb_driver_claim_interface(&ua101_driver,
1226 						 ua->intf[i], ua);
1227 		if (err < 0) {
1228 			ua->intf[i] = NULL;
1229 			err = -EBUSY;
1230 			goto probe_error;
1231 		}
1232 	}
1233 
1234 	err = detect_usb_format(ua);
1235 	if (err < 0)
1236 		goto probe_error;
1237 
1238 	name = usb_id->idProduct == 0x0044 ? "UA-1000" : "UA-101";
1239 	strscpy(card->driver, "UA-101");
1240 	strscpy(card->shortname, name);
1241 	usb_make_path(ua->dev, usb_path, sizeof(usb_path));
1242 	snprintf(ua->card->longname, sizeof(ua->card->longname),
1243 		 "EDIROL %s (serial %s), %u Hz at %s, %s speed", name,
1244 		 ua->dev->serial ? ua->dev->serial : "?", ua->rate, usb_path,
1245 		 ua->dev->speed == USB_SPEED_HIGH ? "high" : "full");
1246 
1247 	err = alloc_stream_buffers(ua, &ua->capture);
1248 	if (err < 0)
1249 		goto probe_error;
1250 	err = alloc_stream_buffers(ua, &ua->playback);
1251 	if (err < 0)
1252 		goto probe_error;
1253 
1254 	err = alloc_stream_urbs(ua, &ua->capture, capture_urb_complete);
1255 	if (err < 0)
1256 		goto probe_error;
1257 	err = alloc_stream_urbs(ua, &ua->playback, playback_urb_complete);
1258 	if (err < 0)
1259 		goto probe_error;
1260 
1261 	err = snd_pcm_new(card, name, 0, 1, 1, &ua->pcm);
1262 	if (err < 0)
1263 		goto probe_error;
1264 	ua->pcm->private_data = ua;
1265 	strscpy(ua->pcm->name, name);
1266 	snd_pcm_set_ops(ua->pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_pcm_ops);
1267 	snd_pcm_set_ops(ua->pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_pcm_ops);
1268 	snd_pcm_set_managed_buffer_all(ua->pcm, SNDRV_DMA_TYPE_VMALLOC,
1269 				       NULL, 0, 0);
1270 
1271 	err = snd_usbmidi_create(card, ua->intf[INTF_MIDI],
1272 				 &ua->midi_list, &midi_quirk);
1273 	if (err < 0)
1274 		goto probe_error;
1275 
1276 	err = snd_card_register(card);
1277 	if (err < 0)
1278 		goto probe_error;
1279 
1280 	usb_set_intfdata(interface, ua);
1281 	devices_used |= 1 << card_index;
1282 
1283 	return 0;
1284 
1285 probe_error:
1286 	free_usb_related_resources(ua, interface);
1287 	snd_card_free(card);
1288 	return err;
1289 }
1290 
1291 static void ua101_disconnect(struct usb_interface *interface)
1292 {
1293 	struct ua101 *ua = usb_get_intfdata(interface);
1294 	struct list_head *midi;
1295 
1296 	if (!ua)
1297 		return;
1298 
1299 	guard(mutex)(&devices_mutex);
1300 
1301 	set_bit(DISCONNECTED, &ua->states);
1302 	wake_up(&ua->rate_feedback_wait);
1303 
1304 	/* make sure that userspace cannot create new requests */
1305 	snd_card_disconnect(ua->card);
1306 
1307 	/* make sure that there are no pending USB requests */
1308 	list_for_each(midi, &ua->midi_list)
1309 		snd_usbmidi_disconnect(midi);
1310 	abort_alsa_playback(ua);
1311 	abort_alsa_capture(ua);
1312 	scoped_guard(mutex, &ua->mutex) {
1313 		stop_usb_playback(ua);
1314 		stop_usb_capture(ua);
1315 	}
1316 
1317 	free_usb_related_resources(ua, interface);
1318 
1319 	devices_used &= ~(1 << ua->card_index);
1320 
1321 	snd_card_free_when_closed(ua->card);
1322 }
1323 
1324 static const struct usb_device_id ua101_ids[] = {
1325 	{ USB_DEVICE(0x0582, 0x0044) }, /* UA-1000 high speed */
1326 	{ USB_DEVICE(0x0582, 0x007d) }, /* UA-101 high speed */
1327 	{ USB_DEVICE(0x0582, 0x008d) }, /* UA-101 full speed */
1328 	{ }
1329 };
1330 MODULE_DEVICE_TABLE(usb, ua101_ids);
1331 
1332 static struct usb_driver ua101_driver = {
1333 	.name = "snd-ua101",
1334 	.id_table = ua101_ids,
1335 	.probe = ua101_probe,
1336 	.disconnect = ua101_disconnect,
1337 #if 0
1338 	.suspend = ua101_suspend,
1339 	.resume = ua101_resume,
1340 #endif
1341 };
1342 
1343 module_usb_driver(ua101_driver);
1344