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