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