0001
0002
0003
0004
0005
0006
0007 #include <linux/init.h>
0008 #include <linux/module.h>
0009 #include <linux/slab.h>
0010 #include <linux/usb.h>
0011 #include <linux/usb/audio.h>
0012 #include <sound/core.h>
0013 #include <sound/initval.h>
0014 #include <sound/pcm.h>
0015 #include <sound/pcm_params.h>
0016 #include "../usbaudio.h"
0017 #include "../midi.h"
0018
0019 MODULE_DESCRIPTION("Edirol UA-101/1000 driver");
0020 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
0021 MODULE_LICENSE("GPL v2");
0022
0023
0024
0025
0026
0027
0028 #define MIN_QUEUE_LENGTH 12
0029
0030 #define MAX_QUEUE_LENGTH 30
0031
0032
0033
0034
0035
0036 #define DEFAULT_QUEUE_LENGTH 21
0037
0038 #define MAX_PACKET_SIZE 672
0039 #define MAX_MEMORY_BUFFERS DIV_ROUND_UP(MAX_QUEUE_LENGTH, \
0040 PAGE_SIZE / MAX_PACKET_SIZE)
0041
0042 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
0043 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
0044 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
0045 static unsigned int queue_length = 21;
0046
0047 module_param_array(index, int, NULL, 0444);
0048 MODULE_PARM_DESC(index, "card index");
0049 module_param_array(id, charp, NULL, 0444);
0050 MODULE_PARM_DESC(id, "ID string");
0051 module_param_array(enable, bool, NULL, 0444);
0052 MODULE_PARM_DESC(enable, "enable card");
0053 module_param(queue_length, uint, 0644);
0054 MODULE_PARM_DESC(queue_length, "USB queue length in microframes, "
0055 __stringify(MIN_QUEUE_LENGTH)"-"__stringify(MAX_QUEUE_LENGTH));
0056
0057 enum {
0058 INTF_PLAYBACK,
0059 INTF_CAPTURE,
0060 INTF_MIDI,
0061
0062 INTF_COUNT
0063 };
0064
0065
0066 enum {
0067 USB_CAPTURE_RUNNING,
0068 USB_PLAYBACK_RUNNING,
0069 ALSA_CAPTURE_OPEN,
0070 ALSA_PLAYBACK_OPEN,
0071 ALSA_CAPTURE_RUNNING,
0072 ALSA_PLAYBACK_RUNNING,
0073 CAPTURE_URB_COMPLETED,
0074 PLAYBACK_URB_COMPLETED,
0075 DISCONNECTED,
0076 };
0077
0078 struct ua101 {
0079 struct usb_device *dev;
0080 struct snd_card *card;
0081 struct usb_interface *intf[INTF_COUNT];
0082 int card_index;
0083 struct snd_pcm *pcm;
0084 struct list_head midi_list;
0085 u64 format_bit;
0086 unsigned int rate;
0087 unsigned int packets_per_second;
0088 spinlock_t lock;
0089 struct mutex mutex;
0090 unsigned long states;
0091
0092
0093 unsigned int rate_feedback_start;
0094 unsigned int rate_feedback_count;
0095 u8 rate_feedback[MAX_QUEUE_LENGTH];
0096
0097 struct list_head ready_playback_urbs;
0098 struct work_struct playback_work;
0099 wait_queue_head_t alsa_capture_wait;
0100 wait_queue_head_t rate_feedback_wait;
0101 wait_queue_head_t alsa_playback_wait;
0102 struct ua101_stream {
0103 struct snd_pcm_substream *substream;
0104 unsigned int usb_pipe;
0105 unsigned int channels;
0106 unsigned int frame_bytes;
0107 unsigned int max_packet_bytes;
0108 unsigned int period_pos;
0109 unsigned int buffer_pos;
0110 unsigned int queue_length;
0111 struct ua101_urb {
0112 struct urb urb;
0113 struct usb_iso_packet_descriptor iso_frame_desc[1];
0114 struct list_head ready_list;
0115 } *urbs[MAX_QUEUE_LENGTH];
0116 struct {
0117 unsigned int size;
0118 void *addr;
0119 dma_addr_t dma;
0120 } buffers[MAX_MEMORY_BUFFERS];
0121 } capture, playback;
0122 };
0123
0124 static DEFINE_MUTEX(devices_mutex);
0125 static unsigned int devices_used;
0126 static struct usb_driver ua101_driver;
0127
0128 static void abort_alsa_playback(struct ua101 *ua);
0129 static void abort_alsa_capture(struct ua101 *ua);
0130
0131 static const char *usb_error_string(int err)
0132 {
0133 switch (err) {
0134 case -ENODEV:
0135 return "no device";
0136 case -ENOENT:
0137 return "endpoint not enabled";
0138 case -EPIPE:
0139 return "endpoint stalled";
0140 case -ENOSPC:
0141 return "not enough bandwidth";
0142 case -ESHUTDOWN:
0143 return "device disabled";
0144 case -EHOSTUNREACH:
0145 return "device suspended";
0146 case -EINVAL:
0147 case -EAGAIN:
0148 case -EFBIG:
0149 case -EMSGSIZE:
0150 return "internal error";
0151 default:
0152 return "unknown error";
0153 }
0154 }
0155
0156 static void abort_usb_capture(struct ua101 *ua)
0157 {
0158 if (test_and_clear_bit(USB_CAPTURE_RUNNING, &ua->states)) {
0159 wake_up(&ua->alsa_capture_wait);
0160 wake_up(&ua->rate_feedback_wait);
0161 }
0162 }
0163
0164 static void abort_usb_playback(struct ua101 *ua)
0165 {
0166 if (test_and_clear_bit(USB_PLAYBACK_RUNNING, &ua->states))
0167 wake_up(&ua->alsa_playback_wait);
0168 }
0169
0170 static void playback_urb_complete(struct urb *usb_urb)
0171 {
0172 struct ua101_urb *urb = (struct ua101_urb *)usb_urb;
0173 struct ua101 *ua = urb->urb.context;
0174 unsigned long flags;
0175
0176 if (unlikely(urb->urb.status == -ENOENT ||
0177 urb->urb.status == -ENODEV ||
0178 urb->urb.status == -ECONNRESET ||
0179 urb->urb.status == -ESHUTDOWN)) {
0180 abort_usb_playback(ua);
0181 abort_alsa_playback(ua);
0182 return;
0183 }
0184
0185 if (test_bit(USB_PLAYBACK_RUNNING, &ua->states)) {
0186
0187 spin_lock_irqsave(&ua->lock, flags);
0188 list_add_tail(&urb->ready_list, &ua->ready_playback_urbs);
0189 if (ua->rate_feedback_count > 0)
0190 queue_work(system_highpri_wq, &ua->playback_work);
0191 ua->playback.substream->runtime->delay -=
0192 urb->urb.iso_frame_desc[0].length /
0193 ua->playback.frame_bytes;
0194 spin_unlock_irqrestore(&ua->lock, flags);
0195 }
0196 }
0197
0198 static void first_playback_urb_complete(struct urb *urb)
0199 {
0200 struct ua101 *ua = urb->context;
0201
0202 urb->complete = playback_urb_complete;
0203 playback_urb_complete(urb);
0204
0205 set_bit(PLAYBACK_URB_COMPLETED, &ua->states);
0206 wake_up(&ua->alsa_playback_wait);
0207 }
0208
0209
0210 static bool copy_playback_data(struct ua101_stream *stream, struct urb *urb,
0211 unsigned int frames)
0212 {
0213 struct snd_pcm_runtime *runtime;
0214 unsigned int frame_bytes, frames1;
0215 const u8 *source;
0216
0217 runtime = stream->substream->runtime;
0218 frame_bytes = stream->frame_bytes;
0219 source = runtime->dma_area + stream->buffer_pos * frame_bytes;
0220 if (stream->buffer_pos + frames <= runtime->buffer_size) {
0221 memcpy(urb->transfer_buffer, source, frames * frame_bytes);
0222 } else {
0223
0224 frames1 = runtime->buffer_size - stream->buffer_pos;
0225 memcpy(urb->transfer_buffer, source, frames1 * frame_bytes);
0226 memcpy(urb->transfer_buffer + frames1 * frame_bytes,
0227 runtime->dma_area, (frames - frames1) * frame_bytes);
0228 }
0229
0230 stream->buffer_pos += frames;
0231 if (stream->buffer_pos >= runtime->buffer_size)
0232 stream->buffer_pos -= runtime->buffer_size;
0233 stream->period_pos += frames;
0234 if (stream->period_pos >= runtime->period_size) {
0235 stream->period_pos -= runtime->period_size;
0236 return true;
0237 }
0238 return false;
0239 }
0240
0241 static inline void add_with_wraparound(struct ua101 *ua,
0242 unsigned int *value, unsigned int add)
0243 {
0244 *value += add;
0245 if (*value >= ua->playback.queue_length)
0246 *value -= ua->playback.queue_length;
0247 }
0248
0249 static void playback_work(struct work_struct *work)
0250 {
0251 struct ua101 *ua = container_of(work, struct ua101, playback_work);
0252 unsigned long flags;
0253 unsigned int frames;
0254 struct ua101_urb *urb;
0255 bool do_period_elapsed = false;
0256 int err;
0257
0258 if (unlikely(!test_bit(USB_PLAYBACK_RUNNING, &ua->states)))
0259 return;
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272 spin_lock_irqsave(&ua->lock, flags);
0273 while (ua->rate_feedback_count > 0 &&
0274 !list_empty(&ua->ready_playback_urbs)) {
0275
0276 frames = ua->rate_feedback[ua->rate_feedback_start];
0277 add_with_wraparound(ua, &ua->rate_feedback_start, 1);
0278 ua->rate_feedback_count--;
0279
0280
0281 urb = list_first_entry(&ua->ready_playback_urbs,
0282 struct ua101_urb, ready_list);
0283 list_del(&urb->ready_list);
0284
0285
0286 urb->urb.iso_frame_desc[0].length =
0287 frames * ua->playback.frame_bytes;
0288 if (test_bit(ALSA_PLAYBACK_RUNNING, &ua->states))
0289 do_period_elapsed |= copy_playback_data(&ua->playback,
0290 &urb->urb,
0291 frames);
0292 else
0293 memset(urb->urb.transfer_buffer, 0,
0294 urb->urb.iso_frame_desc[0].length);
0295
0296
0297 err = usb_submit_urb(&urb->urb, GFP_ATOMIC);
0298 if (unlikely(err < 0)) {
0299 spin_unlock_irqrestore(&ua->lock, flags);
0300 abort_usb_playback(ua);
0301 abort_alsa_playback(ua);
0302 dev_err(&ua->dev->dev, "USB request error %d: %s\n",
0303 err, usb_error_string(err));
0304 return;
0305 }
0306 ua->playback.substream->runtime->delay += frames;
0307 }
0308 spin_unlock_irqrestore(&ua->lock, flags);
0309 if (do_period_elapsed)
0310 snd_pcm_period_elapsed(ua->playback.substream);
0311 }
0312
0313
0314 static bool copy_capture_data(struct ua101_stream *stream, struct urb *urb,
0315 unsigned int frames)
0316 {
0317 struct snd_pcm_runtime *runtime;
0318 unsigned int frame_bytes, frames1;
0319 u8 *dest;
0320
0321 runtime = stream->substream->runtime;
0322 frame_bytes = stream->frame_bytes;
0323 dest = runtime->dma_area + stream->buffer_pos * frame_bytes;
0324 if (stream->buffer_pos + frames <= runtime->buffer_size) {
0325 memcpy(dest, urb->transfer_buffer, frames * frame_bytes);
0326 } else {
0327
0328 frames1 = runtime->buffer_size - stream->buffer_pos;
0329 memcpy(dest, urb->transfer_buffer, frames1 * frame_bytes);
0330 memcpy(runtime->dma_area,
0331 urb->transfer_buffer + frames1 * frame_bytes,
0332 (frames - frames1) * frame_bytes);
0333 }
0334
0335 stream->buffer_pos += frames;
0336 if (stream->buffer_pos >= runtime->buffer_size)
0337 stream->buffer_pos -= runtime->buffer_size;
0338 stream->period_pos += frames;
0339 if (stream->period_pos >= runtime->period_size) {
0340 stream->period_pos -= runtime->period_size;
0341 return true;
0342 }
0343 return false;
0344 }
0345
0346 static void capture_urb_complete(struct urb *urb)
0347 {
0348 struct ua101 *ua = urb->context;
0349 struct ua101_stream *stream = &ua->capture;
0350 unsigned long flags;
0351 unsigned int frames, write_ptr;
0352 bool do_period_elapsed;
0353 int err;
0354
0355 if (unlikely(urb->status == -ENOENT ||
0356 urb->status == -ENODEV ||
0357 urb->status == -ECONNRESET ||
0358 urb->status == -ESHUTDOWN))
0359 goto stream_stopped;
0360
0361 if (urb->status >= 0 && urb->iso_frame_desc[0].status >= 0)
0362 frames = urb->iso_frame_desc[0].actual_length /
0363 stream->frame_bytes;
0364 else
0365 frames = 0;
0366
0367 spin_lock_irqsave(&ua->lock, flags);
0368
0369 if (frames > 0 && test_bit(ALSA_CAPTURE_RUNNING, &ua->states))
0370 do_period_elapsed = copy_capture_data(stream, urb, frames);
0371 else
0372 do_period_elapsed = false;
0373
0374 if (test_bit(USB_CAPTURE_RUNNING, &ua->states)) {
0375 err = usb_submit_urb(urb, GFP_ATOMIC);
0376 if (unlikely(err < 0)) {
0377 spin_unlock_irqrestore(&ua->lock, flags);
0378 dev_err(&ua->dev->dev, "USB request error %d: %s\n",
0379 err, usb_error_string(err));
0380 goto stream_stopped;
0381 }
0382
0383
0384 write_ptr = ua->rate_feedback_start;
0385 add_with_wraparound(ua, &write_ptr, ua->rate_feedback_count);
0386 ua->rate_feedback[write_ptr] = frames;
0387 if (ua->rate_feedback_count < ua->playback.queue_length) {
0388 ua->rate_feedback_count++;
0389 if (ua->rate_feedback_count ==
0390 ua->playback.queue_length)
0391 wake_up(&ua->rate_feedback_wait);
0392 } else {
0393
0394
0395
0396
0397
0398
0399 add_with_wraparound(ua, &ua->rate_feedback_start, 1);
0400 }
0401 if (test_bit(USB_PLAYBACK_RUNNING, &ua->states) &&
0402 !list_empty(&ua->ready_playback_urbs))
0403 queue_work(system_highpri_wq, &ua->playback_work);
0404 }
0405
0406 spin_unlock_irqrestore(&ua->lock, flags);
0407
0408 if (do_period_elapsed)
0409 snd_pcm_period_elapsed(stream->substream);
0410
0411 return;
0412
0413 stream_stopped:
0414 abort_usb_playback(ua);
0415 abort_usb_capture(ua);
0416 abort_alsa_playback(ua);
0417 abort_alsa_capture(ua);
0418 }
0419
0420 static void first_capture_urb_complete(struct urb *urb)
0421 {
0422 struct ua101 *ua = urb->context;
0423
0424 urb->complete = capture_urb_complete;
0425 capture_urb_complete(urb);
0426
0427 set_bit(CAPTURE_URB_COMPLETED, &ua->states);
0428 wake_up(&ua->alsa_capture_wait);
0429 }
0430
0431 static int submit_stream_urbs(struct ua101 *ua, struct ua101_stream *stream)
0432 {
0433 unsigned int i;
0434
0435 for (i = 0; i < stream->queue_length; ++i) {
0436 int err = usb_submit_urb(&stream->urbs[i]->urb, GFP_KERNEL);
0437 if (err < 0) {
0438 dev_err(&ua->dev->dev, "USB request error %d: %s\n",
0439 err, usb_error_string(err));
0440 return err;
0441 }
0442 }
0443 return 0;
0444 }
0445
0446 static void kill_stream_urbs(struct ua101_stream *stream)
0447 {
0448 unsigned int i;
0449
0450 for (i = 0; i < stream->queue_length; ++i)
0451 if (stream->urbs[i])
0452 usb_kill_urb(&stream->urbs[i]->urb);
0453 }
0454
0455 static int enable_iso_interface(struct ua101 *ua, unsigned int intf_index)
0456 {
0457 struct usb_host_interface *alts;
0458
0459 alts = ua->intf[intf_index]->cur_altsetting;
0460 if (alts->desc.bAlternateSetting != 1) {
0461 int err = usb_set_interface(ua->dev,
0462 alts->desc.bInterfaceNumber, 1);
0463 if (err < 0) {
0464 dev_err(&ua->dev->dev,
0465 "cannot initialize interface; error %d: %s\n",
0466 err, usb_error_string(err));
0467 return err;
0468 }
0469 }
0470 return 0;
0471 }
0472
0473 static void disable_iso_interface(struct ua101 *ua, unsigned int intf_index)
0474 {
0475 struct usb_host_interface *alts;
0476
0477 if (!ua->intf[intf_index])
0478 return;
0479
0480 alts = ua->intf[intf_index]->cur_altsetting;
0481 if (alts->desc.bAlternateSetting != 0) {
0482 int err = usb_set_interface(ua->dev,
0483 alts->desc.bInterfaceNumber, 0);
0484 if (err < 0 && !test_bit(DISCONNECTED, &ua->states))
0485 dev_warn(&ua->dev->dev,
0486 "interface reset failed; error %d: %s\n",
0487 err, usb_error_string(err));
0488 }
0489 }
0490
0491 static void stop_usb_capture(struct ua101 *ua)
0492 {
0493 clear_bit(USB_CAPTURE_RUNNING, &ua->states);
0494
0495 kill_stream_urbs(&ua->capture);
0496
0497 disable_iso_interface(ua, INTF_CAPTURE);
0498 }
0499
0500 static int start_usb_capture(struct ua101 *ua)
0501 {
0502 int err;
0503
0504 if (test_bit(DISCONNECTED, &ua->states))
0505 return -ENODEV;
0506
0507 if (test_bit(USB_CAPTURE_RUNNING, &ua->states))
0508 return 0;
0509
0510 kill_stream_urbs(&ua->capture);
0511
0512 err = enable_iso_interface(ua, INTF_CAPTURE);
0513 if (err < 0)
0514 return err;
0515
0516 clear_bit(CAPTURE_URB_COMPLETED, &ua->states);
0517 ua->capture.urbs[0]->urb.complete = first_capture_urb_complete;
0518 ua->rate_feedback_start = 0;
0519 ua->rate_feedback_count = 0;
0520
0521 set_bit(USB_CAPTURE_RUNNING, &ua->states);
0522 err = submit_stream_urbs(ua, &ua->capture);
0523 if (err < 0)
0524 stop_usb_capture(ua);
0525 return err;
0526 }
0527
0528 static void stop_usb_playback(struct ua101 *ua)
0529 {
0530 clear_bit(USB_PLAYBACK_RUNNING, &ua->states);
0531
0532 kill_stream_urbs(&ua->playback);
0533
0534 cancel_work_sync(&ua->playback_work);
0535
0536 disable_iso_interface(ua, INTF_PLAYBACK);
0537 }
0538
0539 static int start_usb_playback(struct ua101 *ua)
0540 {
0541 unsigned int i, frames;
0542 struct urb *urb;
0543 int err = 0;
0544
0545 if (test_bit(DISCONNECTED, &ua->states))
0546 return -ENODEV;
0547
0548 if (test_bit(USB_PLAYBACK_RUNNING, &ua->states))
0549 return 0;
0550
0551 kill_stream_urbs(&ua->playback);
0552 cancel_work_sync(&ua->playback_work);
0553
0554 err = enable_iso_interface(ua, INTF_PLAYBACK);
0555 if (err < 0)
0556 return err;
0557
0558 clear_bit(PLAYBACK_URB_COMPLETED, &ua->states);
0559 ua->playback.urbs[0]->urb.complete =
0560 first_playback_urb_complete;
0561 spin_lock_irq(&ua->lock);
0562 INIT_LIST_HEAD(&ua->ready_playback_urbs);
0563 spin_unlock_irq(&ua->lock);
0564
0565
0566
0567
0568
0569 wait_event(ua->rate_feedback_wait,
0570 ua->rate_feedback_count >= ua->playback.queue_length ||
0571 !test_bit(USB_CAPTURE_RUNNING, &ua->states) ||
0572 test_bit(DISCONNECTED, &ua->states));
0573 if (test_bit(DISCONNECTED, &ua->states)) {
0574 stop_usb_playback(ua);
0575 return -ENODEV;
0576 }
0577 if (!test_bit(USB_CAPTURE_RUNNING, &ua->states)) {
0578 stop_usb_playback(ua);
0579 return -EIO;
0580 }
0581
0582 for (i = 0; i < ua->playback.queue_length; ++i) {
0583
0584 spin_lock_irq(&ua->lock);
0585 frames = ua->rate_feedback[ua->rate_feedback_start];
0586 add_with_wraparound(ua, &ua->rate_feedback_start, 1);
0587 ua->rate_feedback_count--;
0588 spin_unlock_irq(&ua->lock);
0589 urb = &ua->playback.urbs[i]->urb;
0590 urb->iso_frame_desc[0].length =
0591 frames * ua->playback.frame_bytes;
0592 memset(urb->transfer_buffer, 0,
0593 urb->iso_frame_desc[0].length);
0594 }
0595
0596 set_bit(USB_PLAYBACK_RUNNING, &ua->states);
0597 err = submit_stream_urbs(ua, &ua->playback);
0598 if (err < 0)
0599 stop_usb_playback(ua);
0600 return err;
0601 }
0602
0603 static void abort_alsa_capture(struct ua101 *ua)
0604 {
0605 if (test_bit(ALSA_CAPTURE_RUNNING, &ua->states))
0606 snd_pcm_stop_xrun(ua->capture.substream);
0607 }
0608
0609 static void abort_alsa_playback(struct ua101 *ua)
0610 {
0611 if (test_bit(ALSA_PLAYBACK_RUNNING, &ua->states))
0612 snd_pcm_stop_xrun(ua->playback.substream);
0613 }
0614
0615 static int set_stream_hw(struct ua101 *ua, struct snd_pcm_substream *substream,
0616 unsigned int channels)
0617 {
0618 int err;
0619
0620 substream->runtime->hw.info =
0621 SNDRV_PCM_INFO_MMAP |
0622 SNDRV_PCM_INFO_MMAP_VALID |
0623 SNDRV_PCM_INFO_BATCH |
0624 SNDRV_PCM_INFO_INTERLEAVED |
0625 SNDRV_PCM_INFO_BLOCK_TRANSFER |
0626 SNDRV_PCM_INFO_FIFO_IN_FRAMES;
0627 substream->runtime->hw.formats = ua->format_bit;
0628 substream->runtime->hw.rates = snd_pcm_rate_to_rate_bit(ua->rate);
0629 substream->runtime->hw.rate_min = ua->rate;
0630 substream->runtime->hw.rate_max = ua->rate;
0631 substream->runtime->hw.channels_min = channels;
0632 substream->runtime->hw.channels_max = channels;
0633 substream->runtime->hw.buffer_bytes_max = 45000 * 1024;
0634 substream->runtime->hw.period_bytes_min = 1;
0635 substream->runtime->hw.period_bytes_max = UINT_MAX;
0636 substream->runtime->hw.periods_min = 2;
0637 substream->runtime->hw.periods_max = UINT_MAX;
0638 err = snd_pcm_hw_constraint_minmax(substream->runtime,
0639 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
0640 1500000 / ua->packets_per_second,
0641 UINT_MAX);
0642 if (err < 0)
0643 return err;
0644 err = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 32, 24);
0645 return err;
0646 }
0647
0648 static int capture_pcm_open(struct snd_pcm_substream *substream)
0649 {
0650 struct ua101 *ua = substream->private_data;
0651 int err;
0652
0653 ua->capture.substream = substream;
0654 err = set_stream_hw(ua, substream, ua->capture.channels);
0655 if (err < 0)
0656 return err;
0657 substream->runtime->hw.fifo_size =
0658 DIV_ROUND_CLOSEST(ua->rate, ua->packets_per_second);
0659 substream->runtime->delay = substream->runtime->hw.fifo_size;
0660
0661 mutex_lock(&ua->mutex);
0662 err = start_usb_capture(ua);
0663 if (err >= 0)
0664 set_bit(ALSA_CAPTURE_OPEN, &ua->states);
0665 mutex_unlock(&ua->mutex);
0666 return err;
0667 }
0668
0669 static int playback_pcm_open(struct snd_pcm_substream *substream)
0670 {
0671 struct ua101 *ua = substream->private_data;
0672 int err;
0673
0674 ua->playback.substream = substream;
0675 err = set_stream_hw(ua, substream, ua->playback.channels);
0676 if (err < 0)
0677 return err;
0678 substream->runtime->hw.fifo_size =
0679 DIV_ROUND_CLOSEST(ua->rate * ua->playback.queue_length,
0680 ua->packets_per_second);
0681
0682 mutex_lock(&ua->mutex);
0683 err = start_usb_capture(ua);
0684 if (err < 0)
0685 goto error;
0686 err = start_usb_playback(ua);
0687 if (err < 0) {
0688 if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states))
0689 stop_usb_capture(ua);
0690 goto error;
0691 }
0692 set_bit(ALSA_PLAYBACK_OPEN, &ua->states);
0693 error:
0694 mutex_unlock(&ua->mutex);
0695 return err;
0696 }
0697
0698 static int capture_pcm_close(struct snd_pcm_substream *substream)
0699 {
0700 struct ua101 *ua = substream->private_data;
0701
0702 mutex_lock(&ua->mutex);
0703 clear_bit(ALSA_CAPTURE_OPEN, &ua->states);
0704 if (!test_bit(ALSA_PLAYBACK_OPEN, &ua->states))
0705 stop_usb_capture(ua);
0706 mutex_unlock(&ua->mutex);
0707 return 0;
0708 }
0709
0710 static int playback_pcm_close(struct snd_pcm_substream *substream)
0711 {
0712 struct ua101 *ua = substream->private_data;
0713
0714 mutex_lock(&ua->mutex);
0715 stop_usb_playback(ua);
0716 clear_bit(ALSA_PLAYBACK_OPEN, &ua->states);
0717 if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states))
0718 stop_usb_capture(ua);
0719 mutex_unlock(&ua->mutex);
0720 return 0;
0721 }
0722
0723 static int capture_pcm_hw_params(struct snd_pcm_substream *substream,
0724 struct snd_pcm_hw_params *hw_params)
0725 {
0726 struct ua101 *ua = substream->private_data;
0727 int err;
0728
0729 mutex_lock(&ua->mutex);
0730 err = start_usb_capture(ua);
0731 mutex_unlock(&ua->mutex);
0732 return err;
0733 }
0734
0735 static int playback_pcm_hw_params(struct snd_pcm_substream *substream,
0736 struct snd_pcm_hw_params *hw_params)
0737 {
0738 struct ua101 *ua = substream->private_data;
0739 int err;
0740
0741 mutex_lock(&ua->mutex);
0742 err = start_usb_capture(ua);
0743 if (err >= 0)
0744 err = start_usb_playback(ua);
0745 mutex_unlock(&ua->mutex);
0746 return err;
0747 }
0748
0749 static int capture_pcm_prepare(struct snd_pcm_substream *substream)
0750 {
0751 struct ua101 *ua = substream->private_data;
0752 int err;
0753
0754 mutex_lock(&ua->mutex);
0755 err = start_usb_capture(ua);
0756 mutex_unlock(&ua->mutex);
0757 if (err < 0)
0758 return err;
0759
0760
0761
0762
0763
0764
0765
0766 wait_event(ua->alsa_capture_wait,
0767 test_bit(CAPTURE_URB_COMPLETED, &ua->states) ||
0768 !test_bit(USB_CAPTURE_RUNNING, &ua->states));
0769 if (test_bit(DISCONNECTED, &ua->states))
0770 return -ENODEV;
0771 if (!test_bit(USB_CAPTURE_RUNNING, &ua->states))
0772 return -EIO;
0773
0774 ua->capture.period_pos = 0;
0775 ua->capture.buffer_pos = 0;
0776 return 0;
0777 }
0778
0779 static int playback_pcm_prepare(struct snd_pcm_substream *substream)
0780 {
0781 struct ua101 *ua = substream->private_data;
0782 int err;
0783
0784 mutex_lock(&ua->mutex);
0785 err = start_usb_capture(ua);
0786 if (err >= 0)
0787 err = start_usb_playback(ua);
0788 mutex_unlock(&ua->mutex);
0789 if (err < 0)
0790 return err;
0791
0792
0793 wait_event(ua->alsa_playback_wait,
0794 test_bit(PLAYBACK_URB_COMPLETED, &ua->states) ||
0795 !test_bit(USB_PLAYBACK_RUNNING, &ua->states));
0796 if (test_bit(DISCONNECTED, &ua->states))
0797 return -ENODEV;
0798 if (!test_bit(USB_PLAYBACK_RUNNING, &ua->states))
0799 return -EIO;
0800
0801 substream->runtime->delay = 0;
0802 ua->playback.period_pos = 0;
0803 ua->playback.buffer_pos = 0;
0804 return 0;
0805 }
0806
0807 static int capture_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
0808 {
0809 struct ua101 *ua = substream->private_data;
0810
0811 switch (cmd) {
0812 case SNDRV_PCM_TRIGGER_START:
0813 if (!test_bit(USB_CAPTURE_RUNNING, &ua->states))
0814 return -EIO;
0815 set_bit(ALSA_CAPTURE_RUNNING, &ua->states);
0816 return 0;
0817 case SNDRV_PCM_TRIGGER_STOP:
0818 clear_bit(ALSA_CAPTURE_RUNNING, &ua->states);
0819 return 0;
0820 default:
0821 return -EINVAL;
0822 }
0823 }
0824
0825 static int playback_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
0826 {
0827 struct ua101 *ua = substream->private_data;
0828
0829 switch (cmd) {
0830 case SNDRV_PCM_TRIGGER_START:
0831 if (!test_bit(USB_PLAYBACK_RUNNING, &ua->states))
0832 return -EIO;
0833 set_bit(ALSA_PLAYBACK_RUNNING, &ua->states);
0834 return 0;
0835 case SNDRV_PCM_TRIGGER_STOP:
0836 clear_bit(ALSA_PLAYBACK_RUNNING, &ua->states);
0837 return 0;
0838 default:
0839 return -EINVAL;
0840 }
0841 }
0842
0843 static inline snd_pcm_uframes_t ua101_pcm_pointer(struct ua101 *ua,
0844 struct ua101_stream *stream)
0845 {
0846 unsigned long flags;
0847 unsigned int pos;
0848
0849 spin_lock_irqsave(&ua->lock, flags);
0850 pos = stream->buffer_pos;
0851 spin_unlock_irqrestore(&ua->lock, flags);
0852 return pos;
0853 }
0854
0855 static snd_pcm_uframes_t capture_pcm_pointer(struct snd_pcm_substream *subs)
0856 {
0857 struct ua101 *ua = subs->private_data;
0858
0859 return ua101_pcm_pointer(ua, &ua->capture);
0860 }
0861
0862 static snd_pcm_uframes_t playback_pcm_pointer(struct snd_pcm_substream *subs)
0863 {
0864 struct ua101 *ua = subs->private_data;
0865
0866 return ua101_pcm_pointer(ua, &ua->playback);
0867 }
0868
0869 static const struct snd_pcm_ops capture_pcm_ops = {
0870 .open = capture_pcm_open,
0871 .close = capture_pcm_close,
0872 .hw_params = capture_pcm_hw_params,
0873 .prepare = capture_pcm_prepare,
0874 .trigger = capture_pcm_trigger,
0875 .pointer = capture_pcm_pointer,
0876 };
0877
0878 static const struct snd_pcm_ops playback_pcm_ops = {
0879 .open = playback_pcm_open,
0880 .close = playback_pcm_close,
0881 .hw_params = playback_pcm_hw_params,
0882 .prepare = playback_pcm_prepare,
0883 .trigger = playback_pcm_trigger,
0884 .pointer = playback_pcm_pointer,
0885 };
0886
0887 static const struct uac_format_type_i_discrete_descriptor *
0888 find_format_descriptor(struct usb_interface *interface)
0889 {
0890 struct usb_host_interface *alt;
0891 u8 *extra;
0892 int extralen;
0893
0894 if (interface->num_altsetting != 2) {
0895 dev_err(&interface->dev, "invalid num_altsetting\n");
0896 return NULL;
0897 }
0898
0899 alt = &interface->altsetting[0];
0900 if (alt->desc.bNumEndpoints != 0) {
0901 dev_err(&interface->dev, "invalid bNumEndpoints\n");
0902 return NULL;
0903 }
0904
0905 alt = &interface->altsetting[1];
0906 if (alt->desc.bNumEndpoints != 1) {
0907 dev_err(&interface->dev, "invalid bNumEndpoints\n");
0908 return NULL;
0909 }
0910
0911 extra = alt->extra;
0912 extralen = alt->extralen;
0913 while (extralen >= sizeof(struct usb_descriptor_header)) {
0914 struct uac_format_type_i_discrete_descriptor *desc;
0915
0916 desc = (struct uac_format_type_i_discrete_descriptor *)extra;
0917 if (desc->bLength > extralen) {
0918 dev_err(&interface->dev, "descriptor overflow\n");
0919 return NULL;
0920 }
0921 if (desc->bLength == UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1) &&
0922 desc->bDescriptorType == USB_DT_CS_INTERFACE &&
0923 desc->bDescriptorSubtype == UAC_FORMAT_TYPE) {
0924 if (desc->bFormatType != UAC_FORMAT_TYPE_I_PCM ||
0925 desc->bSamFreqType != 1) {
0926 dev_err(&interface->dev,
0927 "invalid format type\n");
0928 return NULL;
0929 }
0930 return desc;
0931 }
0932 extralen -= desc->bLength;
0933 extra += desc->bLength;
0934 }
0935 dev_err(&interface->dev, "sample format descriptor not found\n");
0936 return NULL;
0937 }
0938
0939 static int detect_usb_format(struct ua101 *ua)
0940 {
0941 const struct uac_format_type_i_discrete_descriptor *fmt_capture;
0942 const struct uac_format_type_i_discrete_descriptor *fmt_playback;
0943 const struct usb_endpoint_descriptor *epd;
0944 unsigned int rate2;
0945
0946 fmt_capture = find_format_descriptor(ua->intf[INTF_CAPTURE]);
0947 fmt_playback = find_format_descriptor(ua->intf[INTF_PLAYBACK]);
0948 if (!fmt_capture || !fmt_playback)
0949 return -ENXIO;
0950
0951 switch (fmt_capture->bSubframeSize) {
0952 case 3:
0953 ua->format_bit = SNDRV_PCM_FMTBIT_S24_3LE;
0954 break;
0955 case 4:
0956 ua->format_bit = SNDRV_PCM_FMTBIT_S32_LE;
0957 break;
0958 default:
0959 dev_err(&ua->dev->dev, "sample width is not 24 or 32 bits\n");
0960 return -ENXIO;
0961 }
0962 if (fmt_capture->bSubframeSize != fmt_playback->bSubframeSize) {
0963 dev_err(&ua->dev->dev,
0964 "playback/capture sample widths do not match\n");
0965 return -ENXIO;
0966 }
0967
0968 if (fmt_capture->bBitResolution != 24 ||
0969 fmt_playback->bBitResolution != 24) {
0970 dev_err(&ua->dev->dev, "sample width is not 24 bits\n");
0971 return -ENXIO;
0972 }
0973
0974 ua->rate = combine_triple(fmt_capture->tSamFreq[0]);
0975 rate2 = combine_triple(fmt_playback->tSamFreq[0]);
0976 if (ua->rate != rate2) {
0977 dev_err(&ua->dev->dev,
0978 "playback/capture rates do not match: %u/%u\n",
0979 rate2, ua->rate);
0980 return -ENXIO;
0981 }
0982
0983 switch (ua->dev->speed) {
0984 case USB_SPEED_FULL:
0985 ua->packets_per_second = 1000;
0986 break;
0987 case USB_SPEED_HIGH:
0988 ua->packets_per_second = 8000;
0989 break;
0990 default:
0991 dev_err(&ua->dev->dev, "unknown device speed\n");
0992 return -ENXIO;
0993 }
0994
0995 ua->capture.channels = fmt_capture->bNrChannels;
0996 ua->playback.channels = fmt_playback->bNrChannels;
0997 ua->capture.frame_bytes =
0998 fmt_capture->bSubframeSize * ua->capture.channels;
0999 ua->playback.frame_bytes =
1000 fmt_playback->bSubframeSize * ua->playback.channels;
1001
1002 epd = &ua->intf[INTF_CAPTURE]->altsetting[1].endpoint[0].desc;
1003 if (!usb_endpoint_is_isoc_in(epd) || usb_endpoint_maxp(epd) == 0) {
1004 dev_err(&ua->dev->dev, "invalid capture endpoint\n");
1005 return -ENXIO;
1006 }
1007 ua->capture.usb_pipe = usb_rcvisocpipe(ua->dev, usb_endpoint_num(epd));
1008 ua->capture.max_packet_bytes = usb_endpoint_maxp(epd);
1009
1010 epd = &ua->intf[INTF_PLAYBACK]->altsetting[1].endpoint[0].desc;
1011 if (!usb_endpoint_is_isoc_out(epd) || usb_endpoint_maxp(epd) == 0) {
1012 dev_err(&ua->dev->dev, "invalid playback endpoint\n");
1013 return -ENXIO;
1014 }
1015 ua->playback.usb_pipe = usb_sndisocpipe(ua->dev, usb_endpoint_num(epd));
1016 ua->playback.max_packet_bytes = usb_endpoint_maxp(epd);
1017 return 0;
1018 }
1019
1020 static int alloc_stream_buffers(struct ua101 *ua, struct ua101_stream *stream)
1021 {
1022 unsigned int remaining_packets, packets, packets_per_page, i;
1023 size_t size;
1024
1025 stream->queue_length = queue_length;
1026 stream->queue_length = max(stream->queue_length,
1027 (unsigned int)MIN_QUEUE_LENGTH);
1028 stream->queue_length = min(stream->queue_length,
1029 (unsigned int)MAX_QUEUE_LENGTH);
1030
1031
1032
1033
1034
1035
1036
1037 remaining_packets = stream->queue_length;
1038 packets_per_page = PAGE_SIZE / stream->max_packet_bytes;
1039 for (i = 0; i < ARRAY_SIZE(stream->buffers); ++i) {
1040 packets = min(remaining_packets, packets_per_page);
1041 size = packets * stream->max_packet_bytes;
1042 stream->buffers[i].addr =
1043 usb_alloc_coherent(ua->dev, size, GFP_KERNEL,
1044 &stream->buffers[i].dma);
1045 if (!stream->buffers[i].addr)
1046 return -ENOMEM;
1047 stream->buffers[i].size = size;
1048 remaining_packets -= packets;
1049 if (!remaining_packets)
1050 break;
1051 }
1052 if (remaining_packets) {
1053 dev_err(&ua->dev->dev, "too many packets\n");
1054 return -ENXIO;
1055 }
1056 return 0;
1057 }
1058
1059 static void free_stream_buffers(struct ua101 *ua, struct ua101_stream *stream)
1060 {
1061 unsigned int i;
1062
1063 for (i = 0; i < ARRAY_SIZE(stream->buffers); ++i)
1064 usb_free_coherent(ua->dev,
1065 stream->buffers[i].size,
1066 stream->buffers[i].addr,
1067 stream->buffers[i].dma);
1068 }
1069
1070 static int alloc_stream_urbs(struct ua101 *ua, struct ua101_stream *stream,
1071 void (*urb_complete)(struct urb *))
1072 {
1073 unsigned max_packet_size = stream->max_packet_bytes;
1074 struct ua101_urb *urb;
1075 unsigned int b, u = 0;
1076
1077 for (b = 0; b < ARRAY_SIZE(stream->buffers); ++b) {
1078 unsigned int size = stream->buffers[b].size;
1079 u8 *addr = stream->buffers[b].addr;
1080 dma_addr_t dma = stream->buffers[b].dma;
1081
1082 while (size >= max_packet_size) {
1083 if (u >= stream->queue_length)
1084 goto bufsize_error;
1085 urb = kmalloc(sizeof(*urb), GFP_KERNEL);
1086 if (!urb)
1087 return -ENOMEM;
1088 usb_init_urb(&urb->urb);
1089 urb->urb.dev = ua->dev;
1090 urb->urb.pipe = stream->usb_pipe;
1091 urb->urb.transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1092 urb->urb.transfer_buffer = addr;
1093 urb->urb.transfer_dma = dma;
1094 urb->urb.transfer_buffer_length = max_packet_size;
1095 urb->urb.number_of_packets = 1;
1096 urb->urb.interval = 1;
1097 urb->urb.context = ua;
1098 urb->urb.complete = urb_complete;
1099 urb->urb.iso_frame_desc[0].offset = 0;
1100 urb->urb.iso_frame_desc[0].length = max_packet_size;
1101 stream->urbs[u++] = urb;
1102 size -= max_packet_size;
1103 addr += max_packet_size;
1104 dma += max_packet_size;
1105 }
1106 }
1107 if (u == stream->queue_length)
1108 return 0;
1109 bufsize_error:
1110 dev_err(&ua->dev->dev, "internal buffer size error\n");
1111 return -ENXIO;
1112 }
1113
1114 static void free_stream_urbs(struct ua101_stream *stream)
1115 {
1116 unsigned int i;
1117
1118 for (i = 0; i < stream->queue_length; ++i) {
1119 kfree(stream->urbs[i]);
1120 stream->urbs[i] = NULL;
1121 }
1122 }
1123
1124 static void free_usb_related_resources(struct ua101 *ua,
1125 struct usb_interface *interface)
1126 {
1127 unsigned int i;
1128 struct usb_interface *intf;
1129
1130 mutex_lock(&ua->mutex);
1131 free_stream_urbs(&ua->capture);
1132 free_stream_urbs(&ua->playback);
1133 mutex_unlock(&ua->mutex);
1134 free_stream_buffers(ua, &ua->capture);
1135 free_stream_buffers(ua, &ua->playback);
1136
1137 for (i = 0; i < ARRAY_SIZE(ua->intf); ++i) {
1138 mutex_lock(&ua->mutex);
1139 intf = ua->intf[i];
1140 ua->intf[i] = NULL;
1141 mutex_unlock(&ua->mutex);
1142 if (intf) {
1143 usb_set_intfdata(intf, NULL);
1144 if (intf != interface)
1145 usb_driver_release_interface(&ua101_driver,
1146 intf);
1147 }
1148 }
1149 }
1150
1151 static void ua101_card_free(struct snd_card *card)
1152 {
1153 struct ua101 *ua = card->private_data;
1154
1155 mutex_destroy(&ua->mutex);
1156 }
1157
1158 static int ua101_probe(struct usb_interface *interface,
1159 const struct usb_device_id *usb_id)
1160 {
1161 static const struct snd_usb_midi_endpoint_info midi_ep = {
1162 .out_cables = 0x0001,
1163 .in_cables = 0x0001
1164 };
1165 static const struct snd_usb_audio_quirk midi_quirk = {
1166 .type = QUIRK_MIDI_FIXED_ENDPOINT,
1167 .data = &midi_ep
1168 };
1169 static const int intf_numbers[2][3] = {
1170 {
1171 [INTF_PLAYBACK] = 0,
1172 [INTF_CAPTURE] = 1,
1173 [INTF_MIDI] = 2,
1174 },
1175 {
1176 [INTF_CAPTURE] = 1,
1177 [INTF_PLAYBACK] = 2,
1178 [INTF_MIDI] = 3,
1179 },
1180 };
1181 struct snd_card *card;
1182 struct ua101 *ua;
1183 unsigned int card_index, i;
1184 int is_ua1000;
1185 const char *name;
1186 char usb_path[32];
1187 int err;
1188
1189 is_ua1000 = usb_id->idProduct == 0x0044;
1190
1191 if (interface->altsetting->desc.bInterfaceNumber !=
1192 intf_numbers[is_ua1000][0])
1193 return -ENODEV;
1194
1195 mutex_lock(&devices_mutex);
1196
1197 for (card_index = 0; card_index < SNDRV_CARDS; ++card_index)
1198 if (enable[card_index] && !(devices_used & (1 << card_index)))
1199 break;
1200 if (card_index >= SNDRV_CARDS) {
1201 mutex_unlock(&devices_mutex);
1202 return -ENOENT;
1203 }
1204 err = snd_card_new(&interface->dev,
1205 index[card_index], id[card_index], THIS_MODULE,
1206 sizeof(*ua), &card);
1207 if (err < 0) {
1208 mutex_unlock(&devices_mutex);
1209 return err;
1210 }
1211 card->private_free = ua101_card_free;
1212 ua = card->private_data;
1213 ua->dev = interface_to_usbdev(interface);
1214 ua->card = card;
1215 ua->card_index = card_index;
1216 INIT_LIST_HEAD(&ua->midi_list);
1217 spin_lock_init(&ua->lock);
1218 mutex_init(&ua->mutex);
1219 INIT_LIST_HEAD(&ua->ready_playback_urbs);
1220 INIT_WORK(&ua->playback_work, playback_work);
1221 init_waitqueue_head(&ua->alsa_capture_wait);
1222 init_waitqueue_head(&ua->rate_feedback_wait);
1223 init_waitqueue_head(&ua->alsa_playback_wait);
1224
1225 ua->intf[0] = interface;
1226 for (i = 1; i < ARRAY_SIZE(ua->intf); ++i) {
1227 ua->intf[i] = usb_ifnum_to_if(ua->dev,
1228 intf_numbers[is_ua1000][i]);
1229 if (!ua->intf[i]) {
1230 dev_err(&ua->dev->dev, "interface %u not found\n",
1231 intf_numbers[is_ua1000][i]);
1232 err = -ENXIO;
1233 goto probe_error;
1234 }
1235 err = usb_driver_claim_interface(&ua101_driver,
1236 ua->intf[i], ua);
1237 if (err < 0) {
1238 ua->intf[i] = NULL;
1239 err = -EBUSY;
1240 goto probe_error;
1241 }
1242 }
1243
1244 err = detect_usb_format(ua);
1245 if (err < 0)
1246 goto probe_error;
1247
1248 name = usb_id->idProduct == 0x0044 ? "UA-1000" : "UA-101";
1249 strcpy(card->driver, "UA-101");
1250 strcpy(card->shortname, name);
1251 usb_make_path(ua->dev, usb_path, sizeof(usb_path));
1252 snprintf(ua->card->longname, sizeof(ua->card->longname),
1253 "EDIROL %s (serial %s), %u Hz at %s, %s speed", name,
1254 ua->dev->serial ? ua->dev->serial : "?", ua->rate, usb_path,
1255 ua->dev->speed == USB_SPEED_HIGH ? "high" : "full");
1256
1257 err = alloc_stream_buffers(ua, &ua->capture);
1258 if (err < 0)
1259 goto probe_error;
1260 err = alloc_stream_buffers(ua, &ua->playback);
1261 if (err < 0)
1262 goto probe_error;
1263
1264 err = alloc_stream_urbs(ua, &ua->capture, capture_urb_complete);
1265 if (err < 0)
1266 goto probe_error;
1267 err = alloc_stream_urbs(ua, &ua->playback, playback_urb_complete);
1268 if (err < 0)
1269 goto probe_error;
1270
1271 err = snd_pcm_new(card, name, 0, 1, 1, &ua->pcm);
1272 if (err < 0)
1273 goto probe_error;
1274 ua->pcm->private_data = ua;
1275 strcpy(ua->pcm->name, name);
1276 snd_pcm_set_ops(ua->pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_pcm_ops);
1277 snd_pcm_set_ops(ua->pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_pcm_ops);
1278 snd_pcm_set_managed_buffer_all(ua->pcm, SNDRV_DMA_TYPE_VMALLOC,
1279 NULL, 0, 0);
1280
1281 err = snd_usbmidi_create(card, ua->intf[INTF_MIDI],
1282 &ua->midi_list, &midi_quirk);
1283 if (err < 0)
1284 goto probe_error;
1285
1286 err = snd_card_register(card);
1287 if (err < 0)
1288 goto probe_error;
1289
1290 usb_set_intfdata(interface, ua);
1291 devices_used |= 1 << card_index;
1292
1293 mutex_unlock(&devices_mutex);
1294 return 0;
1295
1296 probe_error:
1297 free_usb_related_resources(ua, interface);
1298 snd_card_free(card);
1299 mutex_unlock(&devices_mutex);
1300 return err;
1301 }
1302
1303 static void ua101_disconnect(struct usb_interface *interface)
1304 {
1305 struct ua101 *ua = usb_get_intfdata(interface);
1306 struct list_head *midi;
1307
1308 if (!ua)
1309 return;
1310
1311 mutex_lock(&devices_mutex);
1312
1313 set_bit(DISCONNECTED, &ua->states);
1314 wake_up(&ua->rate_feedback_wait);
1315
1316
1317 snd_card_disconnect(ua->card);
1318
1319
1320 list_for_each(midi, &ua->midi_list)
1321 snd_usbmidi_disconnect(midi);
1322 abort_alsa_playback(ua);
1323 abort_alsa_capture(ua);
1324 mutex_lock(&ua->mutex);
1325 stop_usb_playback(ua);
1326 stop_usb_capture(ua);
1327 mutex_unlock(&ua->mutex);
1328
1329 free_usb_related_resources(ua, interface);
1330
1331 devices_used &= ~(1 << ua->card_index);
1332
1333 snd_card_free_when_closed(ua->card);
1334
1335 mutex_unlock(&devices_mutex);
1336 }
1337
1338 static const struct usb_device_id ua101_ids[] = {
1339 { USB_DEVICE(0x0582, 0x0044) },
1340 { USB_DEVICE(0x0582, 0x007d) },
1341 { USB_DEVICE(0x0582, 0x008d) },
1342 { }
1343 };
1344 MODULE_DEVICE_TABLE(usb, ua101_ids);
1345
1346 static struct usb_driver ua101_driver = {
1347 .name = "snd-ua101",
1348 .id_table = ua101_ids,
1349 .probe = ua101_probe,
1350 .disconnect = ua101_disconnect,
1351 #if 0
1352 .suspend = ua101_suspend,
1353 .resume = ua101_resume,
1354 #endif
1355 };
1356
1357 module_usb_driver(ua101_driver);