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