1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Edirol UA-101/UA-1000 driver 4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de> 5 */ 6 7 #include <linux/init.h> 8 #include <linux/module.h> 9 #include <linux/slab.h> 10 #include <linux/usb.h> 11 #include <linux/usb/audio.h> 12 #include <sound/core.h> 13 #include <sound/initval.h> 14 #include <sound/pcm.h> 15 #include <sound/pcm_params.h> 16 #include "../usbaudio.h" 17 #include "../midi.h" 18 19 MODULE_DESCRIPTION("Edirol UA-101/1000 driver"); 20 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>"); 21 MODULE_LICENSE("GPL v2"); 22 23 /* 24 * Should not be lower than the minimum scheduling delay of the host 25 * controller. Some Intel controllers need more than one frame; as long as 26 * that driver doesn't tell us about this, use 1.5 frames just to be sure. 27 */ 28 #define MIN_QUEUE_LENGTH 12 29 /* Somewhat random. */ 30 #define MAX_QUEUE_LENGTH 30 31 /* 32 * This magic value optimizes memory usage efficiency for the UA-101's packet 33 * sizes at all sample rates, taking into account the stupid cache pool sizes 34 * that usb_alloc_coherent() uses. 35 */ 36 #define DEFAULT_QUEUE_LENGTH 21 37 38 #define MAX_PACKET_SIZE 672 /* hardware specific */ 39 #define MAX_MEMORY_BUFFERS DIV_ROUND_UP(MAX_QUEUE_LENGTH, \ 40 PAGE_SIZE / MAX_PACKET_SIZE) 41 42 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; 43 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; 44 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; 45 static unsigned int queue_length = 21; 46 47 module_param_array(index, int, NULL, 0444); 48 MODULE_PARM_DESC(index, "card index"); 49 module_param_array(id, charp, NULL, 0444); 50 MODULE_PARM_DESC(id, "ID string"); 51 module_param_array(enable, bool, NULL, 0444); 52 MODULE_PARM_DESC(enable, "enable card"); 53 module_param(queue_length, uint, 0644); 54 MODULE_PARM_DESC(queue_length, "USB queue length in microframes, " 55 __stringify(MIN_QUEUE_LENGTH)"-"__stringify(MAX_QUEUE_LENGTH)); 56 57 enum { 58 INTF_PLAYBACK, 59 INTF_CAPTURE, 60 INTF_MIDI, 61 62 INTF_COUNT 63 }; 64 65 /* bits in struct ua101::states */ 66 enum { 67 USB_CAPTURE_RUNNING, 68 USB_PLAYBACK_RUNNING, 69 ALSA_CAPTURE_OPEN, 70 ALSA_PLAYBACK_OPEN, 71 ALSA_CAPTURE_RUNNING, 72 ALSA_PLAYBACK_RUNNING, 73 CAPTURE_URB_COMPLETED, 74 PLAYBACK_URB_COMPLETED, 75 DISCONNECTED, 76 }; 77 78 struct ua101 { 79 struct usb_device *dev; 80 struct snd_card *card; 81 struct usb_interface *intf[INTF_COUNT]; 82 int card_index; 83 struct snd_pcm *pcm; 84 struct list_head midi_list; 85 u64 format_bit; 86 unsigned int rate; 87 unsigned int packets_per_second; 88 spinlock_t lock; 89 struct mutex mutex; 90 unsigned long states; 91 92 /* FIFO to synchronize playback rate to capture rate */ 93 unsigned int rate_feedback_start; 94 unsigned int rate_feedback_count; 95 u8 rate_feedback[MAX_QUEUE_LENGTH]; 96 97 struct list_head ready_playback_urbs; 98 struct work_struct playback_work; 99 wait_queue_head_t alsa_capture_wait; 100 wait_queue_head_t rate_feedback_wait; 101 wait_queue_head_t alsa_playback_wait; 102 struct ua101_stream { 103 struct snd_pcm_substream *substream; 104 unsigned int usb_pipe; 105 unsigned int channels; 106 unsigned int frame_bytes; 107 unsigned int max_packet_bytes; 108 unsigned int period_pos; 109 unsigned int buffer_pos; 110 unsigned int queue_length; 111 struct ua101_urb { 112 struct urb urb; 113 struct usb_iso_packet_descriptor iso_frame_desc[1]; 114 struct list_head ready_list; 115 } *urbs[MAX_QUEUE_LENGTH]; 116 struct { 117 unsigned int size; 118 void *addr; 119 dma_addr_t dma; 120 } buffers[MAX_MEMORY_BUFFERS]; 121 } capture, playback; 122 }; 123 124 static DEFINE_MUTEX(devices_mutex); 125 static unsigned int devices_used; 126 static struct usb_driver ua101_driver; 127 128 static void abort_alsa_playback(struct ua101 *ua); 129 static void abort_alsa_capture(struct ua101 *ua); 130 131 static const char *usb_error_string(int err) 132 { 133 switch (err) { 134 case -ENODEV: 135 return "no device"; 136 case -ENOENT: 137 return "endpoint not enabled"; 138 case -EPIPE: 139 return "endpoint stalled"; 140 case -ENOSPC: 141 return "not enough bandwidth"; 142 case -ESHUTDOWN: 143 return "device disabled"; 144 case -EHOSTUNREACH: 145 return "device suspended"; 146 case -EINVAL: 147 case -EAGAIN: 148 case -EFBIG: 149 case -EMSGSIZE: 150 return "internal error"; 151 default: 152 return "unknown error"; 153 } 154 } 155 156 static void abort_usb_capture(struct ua101 *ua) 157 { 158 if (test_and_clear_bit(USB_CAPTURE_RUNNING, &ua->states)) { 159 wake_up(&ua->alsa_capture_wait); 160 wake_up(&ua->rate_feedback_wait); 161 } 162 } 163 164 static void abort_usb_playback(struct ua101 *ua) 165 { 166 if (test_and_clear_bit(USB_PLAYBACK_RUNNING, &ua->states)) 167 wake_up(&ua->alsa_playback_wait); 168 } 169 170 static void playback_urb_complete(struct urb *usb_urb) 171 { 172 struct ua101_urb *urb = (struct ua101_urb *)usb_urb; 173 struct ua101 *ua = urb->urb.context; 174 unsigned long flags; 175 176 if (unlikely(urb->urb.status == -ENOENT || /* unlinked */ 177 urb->urb.status == -ENODEV || /* device removed */ 178 urb->urb.status == -ECONNRESET || /* unlinked */ 179 urb->urb.status == -ESHUTDOWN)) { /* device disabled */ 180 abort_usb_playback(ua); 181 abort_alsa_playback(ua); 182 return; 183 } 184 185 if (test_bit(USB_PLAYBACK_RUNNING, &ua->states)) { 186 /* append URB to FIFO */ 187 spin_lock_irqsave(&ua->lock, flags); 188 list_add_tail(&urb->ready_list, &ua->ready_playback_urbs); 189 if (ua->rate_feedback_count > 0) 190 queue_work(system_highpri_wq, &ua->playback_work); 191 ua->playback.substream->runtime->delay -= 192 urb->urb.iso_frame_desc[0].length / 193 ua->playback.frame_bytes; 194 spin_unlock_irqrestore(&ua->lock, flags); 195 } 196 } 197 198 static void first_playback_urb_complete(struct urb *urb) 199 { 200 struct ua101 *ua = urb->context; 201 202 urb->complete = playback_urb_complete; 203 playback_urb_complete(urb); 204 205 set_bit(PLAYBACK_URB_COMPLETED, &ua->states); 206 wake_up(&ua->alsa_playback_wait); 207 } 208 209 /* copy data from the ALSA ring buffer into the URB buffer */ 210 static bool copy_playback_data(struct ua101_stream *stream, struct urb *urb, 211 unsigned int frames) 212 { 213 struct snd_pcm_runtime *runtime; 214 unsigned int frame_bytes, frames1; 215 const u8 *source; 216 217 runtime = stream->substream->runtime; 218 frame_bytes = stream->frame_bytes; 219 source = runtime->dma_area + stream->buffer_pos * frame_bytes; 220 if (stream->buffer_pos + frames <= runtime->buffer_size) { 221 memcpy(urb->transfer_buffer, source, frames * frame_bytes); 222 } else { 223 /* wrap around at end of ring buffer */ 224 frames1 = runtime->buffer_size - stream->buffer_pos; 225 memcpy(urb->transfer_buffer, source, frames1 * frame_bytes); 226 memcpy(urb->transfer_buffer + frames1 * frame_bytes, 227 runtime->dma_area, (frames - frames1) * frame_bytes); 228 } 229 230 stream->buffer_pos += frames; 231 if (stream->buffer_pos >= runtime->buffer_size) 232 stream->buffer_pos -= runtime->buffer_size; 233 stream->period_pos += frames; 234 if (stream->period_pos >= runtime->period_size) { 235 stream->period_pos -= runtime->period_size; 236 return true; 237 } 238 return false; 239 } 240 241 static inline void add_with_wraparound(struct ua101 *ua, 242 unsigned int *value, unsigned int add) 243 { 244 *value += add; 245 if (*value >= ua->playback.queue_length) 246 *value -= ua->playback.queue_length; 247 } 248 249 static void playback_work(struct work_struct *work) 250 { 251 struct ua101 *ua = container_of(work, struct ua101, playback_work); 252 unsigned long flags; 253 unsigned int frames; 254 struct ua101_urb *urb; 255 bool do_period_elapsed = false; 256 int err; 257 258 if (unlikely(!test_bit(USB_PLAYBACK_RUNNING, &ua->states))) 259 return; 260 261 /* 262 * Synchronizing the playback rate to the capture rate is done by using 263 * the same sequence of packet sizes for both streams. 264 * Submitting a playback URB therefore requires both a ready URB and 265 * the size of the corresponding capture packet, i.e., both playback 266 * and capture URBs must have been completed. Since the USB core does 267 * not guarantee that playback and capture complete callbacks are 268 * called alternately, we use two FIFOs for packet sizes and read URBs; 269 * submitting playback URBs is possible as long as both FIFOs are 270 * nonempty. 271 */ 272 spin_lock_irqsave(&ua->lock, flags); 273 while (ua->rate_feedback_count > 0 && 274 !list_empty(&ua->ready_playback_urbs)) { 275 /* take packet size out of FIFO */ 276 frames = ua->rate_feedback[ua->rate_feedback_start]; 277 add_with_wraparound(ua, &ua->rate_feedback_start, 1); 278 ua->rate_feedback_count--; 279 280 /* take URB out of FIFO */ 281 urb = list_first_entry(&ua->ready_playback_urbs, 282 struct ua101_urb, ready_list); 283 list_del(&urb->ready_list); 284 285 /* fill packet with data or silence */ 286 urb->urb.iso_frame_desc[0].length = 287 frames * ua->playback.frame_bytes; 288 if (test_bit(ALSA_PLAYBACK_RUNNING, &ua->states)) 289 do_period_elapsed |= copy_playback_data(&ua->playback, 290 &urb->urb, 291 frames); 292 else 293 memset(urb->urb.transfer_buffer, 0, 294 urb->urb.iso_frame_desc[0].length); 295 296 /* and off you go ... */ 297 err = usb_submit_urb(&urb->urb, GFP_ATOMIC); 298 if (unlikely(err < 0)) { 299 spin_unlock_irqrestore(&ua->lock, flags); 300 abort_usb_playback(ua); 301 abort_alsa_playback(ua); 302 dev_err(&ua->dev->dev, "USB request error %d: %s\n", 303 err, usb_error_string(err)); 304 return; 305 } 306 ua->playback.substream->runtime->delay += frames; 307 } 308 spin_unlock_irqrestore(&ua->lock, flags); 309 if (do_period_elapsed) 310 snd_pcm_period_elapsed(ua->playback.substream); 311 } 312 313 /* copy data from the URB buffer into the ALSA ring buffer */ 314 static bool copy_capture_data(struct ua101_stream *stream, struct urb *urb, 315 unsigned int frames) 316 { 317 struct snd_pcm_runtime *runtime; 318 unsigned int frame_bytes, frames1; 319 u8 *dest; 320 321 runtime = stream->substream->runtime; 322 frame_bytes = stream->frame_bytes; 323 dest = runtime->dma_area + stream->buffer_pos * frame_bytes; 324 if (stream->buffer_pos + frames <= runtime->buffer_size) { 325 memcpy(dest, urb->transfer_buffer, frames * frame_bytes); 326 } else { 327 /* wrap around at end of ring buffer */ 328 frames1 = runtime->buffer_size - stream->buffer_pos; 329 memcpy(dest, urb->transfer_buffer, frames1 * frame_bytes); 330 memcpy(runtime->dma_area, 331 urb->transfer_buffer + frames1 * frame_bytes, 332 (frames - frames1) * frame_bytes); 333 } 334 335 stream->buffer_pos += frames; 336 if (stream->buffer_pos >= runtime->buffer_size) 337 stream->buffer_pos -= runtime->buffer_size; 338 stream->period_pos += frames; 339 if (stream->period_pos >= runtime->period_size) { 340 stream->period_pos -= runtime->period_size; 341 return true; 342 } 343 return false; 344 } 345 346 static void capture_urb_complete(struct urb *urb) 347 { 348 struct ua101 *ua = urb->context; 349 struct ua101_stream *stream = &ua->capture; 350 unsigned long flags; 351 unsigned int frames, write_ptr; 352 bool do_period_elapsed; 353 int err; 354 355 if (unlikely(urb->status == -ENOENT || /* unlinked */ 356 urb->status == -ENODEV || /* device removed */ 357 urb->status == -ECONNRESET || /* unlinked */ 358 urb->status == -ESHUTDOWN)) /* device disabled */ 359 goto stream_stopped; 360 361 if (urb->status >= 0 && urb->iso_frame_desc[0].status >= 0) 362 frames = urb->iso_frame_desc[0].actual_length / 363 stream->frame_bytes; 364 else 365 frames = 0; 366 367 spin_lock_irqsave(&ua->lock, flags); 368 369 if (frames > 0 && test_bit(ALSA_CAPTURE_RUNNING, &ua->states)) 370 do_period_elapsed = copy_capture_data(stream, urb, frames); 371 else 372 do_period_elapsed = false; 373 374 if (test_bit(USB_CAPTURE_RUNNING, &ua->states)) { 375 err = usb_submit_urb(urb, GFP_ATOMIC); 376 if (unlikely(err < 0)) { 377 spin_unlock_irqrestore(&ua->lock, flags); 378 dev_err(&ua->dev->dev, "USB request error %d: %s\n", 379 err, usb_error_string(err)); 380 goto stream_stopped; 381 } 382 383 /* append packet size to FIFO */ 384 write_ptr = ua->rate_feedback_start; 385 add_with_wraparound(ua, &write_ptr, ua->rate_feedback_count); 386 ua->rate_feedback[write_ptr] = frames; 387 if (ua->rate_feedback_count < ua->playback.queue_length) { 388 ua->rate_feedback_count++; 389 if (ua->rate_feedback_count == 390 ua->playback.queue_length) 391 wake_up(&ua->rate_feedback_wait); 392 } else { 393 /* 394 * Ring buffer overflow; this happens when the playback 395 * stream is not running. Throw away the oldest entry, 396 * so that the playback stream, when it starts, sees 397 * the most recent packet sizes. 398 */ 399 add_with_wraparound(ua, &ua->rate_feedback_start, 1); 400 } 401 if (test_bit(USB_PLAYBACK_RUNNING, &ua->states) && 402 !list_empty(&ua->ready_playback_urbs)) 403 queue_work(system_highpri_wq, &ua->playback_work); 404 } 405 406 spin_unlock_irqrestore(&ua->lock, flags); 407 408 if (do_period_elapsed) 409 snd_pcm_period_elapsed(stream->substream); 410 411 return; 412 413 stream_stopped: 414 abort_usb_playback(ua); 415 abort_usb_capture(ua); 416 abort_alsa_playback(ua); 417 abort_alsa_capture(ua); 418 } 419 420 static void first_capture_urb_complete(struct urb *urb) 421 { 422 struct ua101 *ua = urb->context; 423 424 urb->complete = capture_urb_complete; 425 capture_urb_complete(urb); 426 427 set_bit(CAPTURE_URB_COMPLETED, &ua->states); 428 wake_up(&ua->alsa_capture_wait); 429 } 430 431 static int submit_stream_urbs(struct ua101 *ua, struct ua101_stream *stream) 432 { 433 unsigned int i; 434 435 for (i = 0; i < stream->queue_length; ++i) { 436 int err = usb_submit_urb(&stream->urbs[i]->urb, GFP_KERNEL); 437 if (err < 0) { 438 dev_err(&ua->dev->dev, "USB request error %d: %s\n", 439 err, usb_error_string(err)); 440 return err; 441 } 442 } 443 return 0; 444 } 445 446 static void kill_stream_urbs(struct ua101_stream *stream) 447 { 448 unsigned int i; 449 450 for (i = 0; i < stream->queue_length; ++i) 451 if (stream->urbs[i]) 452 usb_kill_urb(&stream->urbs[i]->urb); 453 } 454 455 static int enable_iso_interface(struct ua101 *ua, unsigned int intf_index) 456 { 457 struct usb_host_interface *alts; 458 459 alts = ua->intf[intf_index]->cur_altsetting; 460 if (alts->desc.bAlternateSetting != 1) { 461 int err = usb_set_interface(ua->dev, 462 alts->desc.bInterfaceNumber, 1); 463 if (err < 0) { 464 dev_err(&ua->dev->dev, 465 "cannot initialize interface; error %d: %s\n", 466 err, usb_error_string(err)); 467 return err; 468 } 469 } 470 return 0; 471 } 472 473 static void disable_iso_interface(struct ua101 *ua, unsigned int intf_index) 474 { 475 struct usb_host_interface *alts; 476 477 if (!ua->intf[intf_index]) 478 return; 479 480 alts = ua->intf[intf_index]->cur_altsetting; 481 if (alts->desc.bAlternateSetting != 0) { 482 int err = usb_set_interface(ua->dev, 483 alts->desc.bInterfaceNumber, 0); 484 if (err < 0 && !test_bit(DISCONNECTED, &ua->states)) 485 dev_warn(&ua->dev->dev, 486 "interface reset failed; error %d: %s\n", 487 err, usb_error_string(err)); 488 } 489 } 490 491 static void stop_usb_capture(struct ua101 *ua) 492 { 493 clear_bit(USB_CAPTURE_RUNNING, &ua->states); 494 495 kill_stream_urbs(&ua->capture); 496 497 disable_iso_interface(ua, INTF_CAPTURE); 498 } 499 500 static int start_usb_capture(struct ua101 *ua) 501 { 502 int err; 503 504 if (test_bit(DISCONNECTED, &ua->states)) 505 return -ENODEV; 506 507 if (test_bit(USB_CAPTURE_RUNNING, &ua->states)) 508 return 0; 509 510 kill_stream_urbs(&ua->capture); 511 512 err = enable_iso_interface(ua, INTF_CAPTURE); 513 if (err < 0) 514 return err; 515 516 clear_bit(CAPTURE_URB_COMPLETED, &ua->states); 517 ua->capture.urbs[0]->urb.complete = first_capture_urb_complete; 518 ua->rate_feedback_start = 0; 519 ua->rate_feedback_count = 0; 520 521 set_bit(USB_CAPTURE_RUNNING, &ua->states); 522 err = submit_stream_urbs(ua, &ua->capture); 523 if (err < 0) 524 stop_usb_capture(ua); 525 return err; 526 } 527 528 static void stop_usb_playback(struct ua101 *ua) 529 { 530 clear_bit(USB_PLAYBACK_RUNNING, &ua->states); 531 532 kill_stream_urbs(&ua->playback); 533 534 cancel_work_sync(&ua->playback_work); 535 536 disable_iso_interface(ua, INTF_PLAYBACK); 537 } 538 539 static int start_usb_playback(struct ua101 *ua) 540 { 541 unsigned int i, frames; 542 struct urb *urb; 543 int err = 0; 544 545 if (test_bit(DISCONNECTED, &ua->states)) 546 return -ENODEV; 547 548 if (test_bit(USB_PLAYBACK_RUNNING, &ua->states)) 549 return 0; 550 551 kill_stream_urbs(&ua->playback); 552 cancel_work_sync(&ua->playback_work); 553 554 err = enable_iso_interface(ua, INTF_PLAYBACK); 555 if (err < 0) 556 return err; 557 558 clear_bit(PLAYBACK_URB_COMPLETED, &ua->states); 559 ua->playback.urbs[0]->urb.complete = 560 first_playback_urb_complete; 561 spin_lock_irq(&ua->lock); 562 INIT_LIST_HEAD(&ua->ready_playback_urbs); 563 spin_unlock_irq(&ua->lock); 564 565 /* 566 * We submit the initial URBs all at once, so we have to wait for the 567 * packet size FIFO to be full. 568 */ 569 wait_event(ua->rate_feedback_wait, 570 ua->rate_feedback_count >= ua->playback.queue_length || 571 !test_bit(USB_CAPTURE_RUNNING, &ua->states) || 572 test_bit(DISCONNECTED, &ua->states)); 573 if (test_bit(DISCONNECTED, &ua->states)) { 574 stop_usb_playback(ua); 575 return -ENODEV; 576 } 577 if (!test_bit(USB_CAPTURE_RUNNING, &ua->states)) { 578 stop_usb_playback(ua); 579 return -EIO; 580 } 581 582 for (i = 0; i < ua->playback.queue_length; ++i) { 583 /* all initial URBs contain silence */ 584 spin_lock_irq(&ua->lock); 585 frames = ua->rate_feedback[ua->rate_feedback_start]; 586 add_with_wraparound(ua, &ua->rate_feedback_start, 1); 587 ua->rate_feedback_count--; 588 spin_unlock_irq(&ua->lock); 589 urb = &ua->playback.urbs[i]->urb; 590 urb->iso_frame_desc[0].length = 591 frames * ua->playback.frame_bytes; 592 memset(urb->transfer_buffer, 0, 593 urb->iso_frame_desc[0].length); 594 } 595 596 set_bit(USB_PLAYBACK_RUNNING, &ua->states); 597 err = submit_stream_urbs(ua, &ua->playback); 598 if (err < 0) 599 stop_usb_playback(ua); 600 return err; 601 } 602 603 static void abort_alsa_capture(struct ua101 *ua) 604 { 605 if (test_bit(ALSA_CAPTURE_RUNNING, &ua->states)) 606 snd_pcm_stop_xrun(ua->capture.substream); 607 } 608 609 static void abort_alsa_playback(struct ua101 *ua) 610 { 611 if (test_bit(ALSA_PLAYBACK_RUNNING, &ua->states)) 612 snd_pcm_stop_xrun(ua->playback.substream); 613 } 614 615 static int set_stream_hw(struct ua101 *ua, struct snd_pcm_substream *substream, 616 unsigned int channels) 617 { 618 int err; 619 620 substream->runtime->hw.info = 621 SNDRV_PCM_INFO_MMAP | 622 SNDRV_PCM_INFO_MMAP_VALID | 623 SNDRV_PCM_INFO_BATCH | 624 SNDRV_PCM_INFO_INTERLEAVED | 625 SNDRV_PCM_INFO_BLOCK_TRANSFER | 626 SNDRV_PCM_INFO_FIFO_IN_FRAMES; 627 substream->runtime->hw.formats = ua->format_bit; 628 substream->runtime->hw.rates = snd_pcm_rate_to_rate_bit(ua->rate); 629 substream->runtime->hw.rate_min = ua->rate; 630 substream->runtime->hw.rate_max = ua->rate; 631 substream->runtime->hw.channels_min = channels; 632 substream->runtime->hw.channels_max = channels; 633 substream->runtime->hw.buffer_bytes_max = 45000 * 1024; 634 substream->runtime->hw.period_bytes_min = 1; 635 substream->runtime->hw.period_bytes_max = UINT_MAX; 636 substream->runtime->hw.periods_min = 2; 637 substream->runtime->hw.periods_max = UINT_MAX; 638 err = snd_pcm_hw_constraint_minmax(substream->runtime, 639 SNDRV_PCM_HW_PARAM_PERIOD_TIME, 640 1500000 / ua->packets_per_second, 641 UINT_MAX); 642 if (err < 0) 643 return err; 644 err = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 32, 24); 645 return err; 646 } 647 648 static int capture_pcm_open(struct snd_pcm_substream *substream) 649 { 650 struct ua101 *ua = substream->private_data; 651 int err; 652 653 ua->capture.substream = substream; 654 err = set_stream_hw(ua, substream, ua->capture.channels); 655 if (err < 0) 656 return err; 657 substream->runtime->hw.fifo_size = 658 DIV_ROUND_CLOSEST(ua->rate, ua->packets_per_second); 659 substream->runtime->delay = substream->runtime->hw.fifo_size; 660 661 mutex_lock(&ua->mutex); 662 err = start_usb_capture(ua); 663 if (err >= 0) 664 set_bit(ALSA_CAPTURE_OPEN, &ua->states); 665 mutex_unlock(&ua->mutex); 666 return err; 667 } 668 669 static int playback_pcm_open(struct snd_pcm_substream *substream) 670 { 671 struct ua101 *ua = substream->private_data; 672 int err; 673 674 ua->playback.substream = substream; 675 err = set_stream_hw(ua, substream, ua->playback.channels); 676 if (err < 0) 677 return err; 678 substream->runtime->hw.fifo_size = 679 DIV_ROUND_CLOSEST(ua->rate * ua->playback.queue_length, 680 ua->packets_per_second); 681 682 mutex_lock(&ua->mutex); 683 err = start_usb_capture(ua); 684 if (err < 0) 685 goto error; 686 err = start_usb_playback(ua); 687 if (err < 0) { 688 if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states)) 689 stop_usb_capture(ua); 690 goto error; 691 } 692 set_bit(ALSA_PLAYBACK_OPEN, &ua->states); 693 error: 694 mutex_unlock(&ua->mutex); 695 return err; 696 } 697 698 static int capture_pcm_close(struct snd_pcm_substream *substream) 699 { 700 struct ua101 *ua = substream->private_data; 701 702 mutex_lock(&ua->mutex); 703 clear_bit(ALSA_CAPTURE_OPEN, &ua->states); 704 if (!test_bit(ALSA_PLAYBACK_OPEN, &ua->states)) 705 stop_usb_capture(ua); 706 mutex_unlock(&ua->mutex); 707 return 0; 708 } 709 710 static int playback_pcm_close(struct snd_pcm_substream *substream) 711 { 712 struct ua101 *ua = substream->private_data; 713 714 mutex_lock(&ua->mutex); 715 stop_usb_playback(ua); 716 clear_bit(ALSA_PLAYBACK_OPEN, &ua->states); 717 if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states)) 718 stop_usb_capture(ua); 719 mutex_unlock(&ua->mutex); 720 return 0; 721 } 722 723 static int capture_pcm_hw_params(struct snd_pcm_substream *substream, 724 struct snd_pcm_hw_params *hw_params) 725 { 726 struct ua101 *ua = substream->private_data; 727 int err; 728 729 mutex_lock(&ua->mutex); 730 err = start_usb_capture(ua); 731 mutex_unlock(&ua->mutex); 732 return err; 733 } 734 735 static int playback_pcm_hw_params(struct snd_pcm_substream *substream, 736 struct snd_pcm_hw_params *hw_params) 737 { 738 struct ua101 *ua = substream->private_data; 739 int err; 740 741 mutex_lock(&ua->mutex); 742 err = start_usb_capture(ua); 743 if (err >= 0) 744 err = start_usb_playback(ua); 745 mutex_unlock(&ua->mutex); 746 return err; 747 } 748 749 static int capture_pcm_prepare(struct snd_pcm_substream *substream) 750 { 751 struct ua101 *ua = substream->private_data; 752 int err; 753 754 mutex_lock(&ua->mutex); 755 err = start_usb_capture(ua); 756 mutex_unlock(&ua->mutex); 757 if (err < 0) 758 return err; 759 760 /* 761 * The EHCI driver schedules the first packet of an iso stream at 10 ms 762 * in the future, i.e., no data is actually captured for that long. 763 * Take the wait here so that the stream is known to be actually 764 * running when the start trigger has been called. 765 */ 766 wait_event(ua->alsa_capture_wait, 767 test_bit(CAPTURE_URB_COMPLETED, &ua->states) || 768 !test_bit(USB_CAPTURE_RUNNING, &ua->states)); 769 if (test_bit(DISCONNECTED, &ua->states)) 770 return -ENODEV; 771 if (!test_bit(USB_CAPTURE_RUNNING, &ua->states)) 772 return -EIO; 773 774 ua->capture.period_pos = 0; 775 ua->capture.buffer_pos = 0; 776 return 0; 777 } 778 779 static int playback_pcm_prepare(struct snd_pcm_substream *substream) 780 { 781 struct ua101 *ua = substream->private_data; 782 int err; 783 784 mutex_lock(&ua->mutex); 785 err = start_usb_capture(ua); 786 if (err >= 0) 787 err = start_usb_playback(ua); 788 mutex_unlock(&ua->mutex); 789 if (err < 0) 790 return err; 791 792 /* see the comment in capture_pcm_prepare() */ 793 wait_event(ua->alsa_playback_wait, 794 test_bit(PLAYBACK_URB_COMPLETED, &ua->states) || 795 !test_bit(USB_PLAYBACK_RUNNING, &ua->states)); 796 if (test_bit(DISCONNECTED, &ua->states)) 797 return -ENODEV; 798 if (!test_bit(USB_PLAYBACK_RUNNING, &ua->states)) 799 return -EIO; 800 801 substream->runtime->delay = 0; 802 ua->playback.period_pos = 0; 803 ua->playback.buffer_pos = 0; 804 return 0; 805 } 806 807 static int capture_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 808 { 809 struct ua101 *ua = substream->private_data; 810 811 switch (cmd) { 812 case SNDRV_PCM_TRIGGER_START: 813 if (!test_bit(USB_CAPTURE_RUNNING, &ua->states)) 814 return -EIO; 815 set_bit(ALSA_CAPTURE_RUNNING, &ua->states); 816 return 0; 817 case SNDRV_PCM_TRIGGER_STOP: 818 clear_bit(ALSA_CAPTURE_RUNNING, &ua->states); 819 return 0; 820 default: 821 return -EINVAL; 822 } 823 } 824 825 static int playback_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 826 { 827 struct ua101 *ua = substream->private_data; 828 829 switch (cmd) { 830 case SNDRV_PCM_TRIGGER_START: 831 if (!test_bit(USB_PLAYBACK_RUNNING, &ua->states)) 832 return -EIO; 833 set_bit(ALSA_PLAYBACK_RUNNING, &ua->states); 834 return 0; 835 case SNDRV_PCM_TRIGGER_STOP: 836 clear_bit(ALSA_PLAYBACK_RUNNING, &ua->states); 837 return 0; 838 default: 839 return -EINVAL; 840 } 841 } 842 843 static inline snd_pcm_uframes_t ua101_pcm_pointer(struct ua101 *ua, 844 struct ua101_stream *stream) 845 { 846 unsigned long flags; 847 unsigned int pos; 848 849 spin_lock_irqsave(&ua->lock, flags); 850 pos = stream->buffer_pos; 851 spin_unlock_irqrestore(&ua->lock, flags); 852 return pos; 853 } 854 855 static snd_pcm_uframes_t capture_pcm_pointer(struct snd_pcm_substream *subs) 856 { 857 struct ua101 *ua = subs->private_data; 858 859 return ua101_pcm_pointer(ua, &ua->capture); 860 } 861 862 static snd_pcm_uframes_t playback_pcm_pointer(struct snd_pcm_substream *subs) 863 { 864 struct ua101 *ua = subs->private_data; 865 866 return ua101_pcm_pointer(ua, &ua->playback); 867 } 868 869 static const struct snd_pcm_ops capture_pcm_ops = { 870 .open = capture_pcm_open, 871 .close = capture_pcm_close, 872 .hw_params = capture_pcm_hw_params, 873 .prepare = capture_pcm_prepare, 874 .trigger = capture_pcm_trigger, 875 .pointer = capture_pcm_pointer, 876 }; 877 878 static const struct snd_pcm_ops playback_pcm_ops = { 879 .open = playback_pcm_open, 880 .close = playback_pcm_close, 881 .hw_params = playback_pcm_hw_params, 882 .prepare = playback_pcm_prepare, 883 .trigger = playback_pcm_trigger, 884 .pointer = playback_pcm_pointer, 885 }; 886 887 static const struct uac_format_type_i_discrete_descriptor * 888 find_format_descriptor(struct usb_interface *interface) 889 { 890 struct usb_host_interface *alt; 891 u8 *extra; 892 int extralen; 893 894 if (interface->num_altsetting != 2) { 895 dev_err(&interface->dev, "invalid num_altsetting\n"); 896 return NULL; 897 } 898 899 alt = &interface->altsetting[0]; 900 if (alt->desc.bNumEndpoints != 0) { 901 dev_err(&interface->dev, "invalid bNumEndpoints\n"); 902 return NULL; 903 } 904 905 alt = &interface->altsetting[1]; 906 if (alt->desc.bNumEndpoints != 1) { 907 dev_err(&interface->dev, "invalid bNumEndpoints\n"); 908 return NULL; 909 } 910 911 extra = alt->extra; 912 extralen = alt->extralen; 913 while (extralen >= sizeof(struct usb_descriptor_header)) { 914 struct uac_format_type_i_discrete_descriptor *desc; 915 916 desc = (struct uac_format_type_i_discrete_descriptor *)extra; 917 if (desc->bLength > extralen) { 918 dev_err(&interface->dev, "descriptor overflow\n"); 919 return NULL; 920 } 921 if (desc->bLength == UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1) && 922 desc->bDescriptorType == USB_DT_CS_INTERFACE && 923 desc->bDescriptorSubtype == UAC_FORMAT_TYPE) { 924 if (desc->bFormatType != UAC_FORMAT_TYPE_I_PCM || 925 desc->bSamFreqType != 1) { 926 dev_err(&interface->dev, 927 "invalid format type\n"); 928 return NULL; 929 } 930 return desc; 931 } 932 extralen -= desc->bLength; 933 extra += desc->bLength; 934 } 935 dev_err(&interface->dev, "sample format descriptor not found\n"); 936 return NULL; 937 } 938 939 static int detect_usb_format(struct ua101 *ua) 940 { 941 const struct uac_format_type_i_discrete_descriptor *fmt_capture; 942 const struct uac_format_type_i_discrete_descriptor *fmt_playback; 943 const struct usb_endpoint_descriptor *epd; 944 unsigned int rate2; 945 946 fmt_capture = find_format_descriptor(ua->intf[INTF_CAPTURE]); 947 fmt_playback = find_format_descriptor(ua->intf[INTF_PLAYBACK]); 948 if (!fmt_capture || !fmt_playback) 949 return -ENXIO; 950 951 switch (fmt_capture->bSubframeSize) { 952 case 3: 953 ua->format_bit = SNDRV_PCM_FMTBIT_S24_3LE; 954 break; 955 case 4: 956 ua->format_bit = SNDRV_PCM_FMTBIT_S32_LE; 957 break; 958 default: 959 dev_err(&ua->dev->dev, "sample width is not 24 or 32 bits\n"); 960 return -ENXIO; 961 } 962 if (fmt_capture->bSubframeSize != fmt_playback->bSubframeSize) { 963 dev_err(&ua->dev->dev, 964 "playback/capture sample widths do not match\n"); 965 return -ENXIO; 966 } 967 968 if (fmt_capture->bBitResolution != 24 || 969 fmt_playback->bBitResolution != 24) { 970 dev_err(&ua->dev->dev, "sample width is not 24 bits\n"); 971 return -ENXIO; 972 } 973 974 ua->rate = combine_triple(fmt_capture->tSamFreq[0]); 975 rate2 = combine_triple(fmt_playback->tSamFreq[0]); 976 if (ua->rate != rate2) { 977 dev_err(&ua->dev->dev, 978 "playback/capture rates do not match: %u/%u\n", 979 rate2, ua->rate); 980 return -ENXIO; 981 } 982 983 switch (ua->dev->speed) { 984 case USB_SPEED_FULL: 985 ua->packets_per_second = 1000; 986 break; 987 case USB_SPEED_HIGH: 988 ua->packets_per_second = 8000; 989 break; 990 default: 991 dev_err(&ua->dev->dev, "unknown device speed\n"); 992 return -ENXIO; 993 } 994 995 ua->capture.channels = fmt_capture->bNrChannels; 996 ua->playback.channels = fmt_playback->bNrChannels; 997 ua->capture.frame_bytes = 998 fmt_capture->bSubframeSize * ua->capture.channels; 999 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)) { 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)) { 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 * The cache pool sizes used by usb_alloc_coherent() (128, 512, 2048) are 1033 * quite bad when used with the packet sizes of this device (e.g. 280, 1034 * 520, 624). Therefore, we allocate and subdivide entire pages, using 1035 * a smaller buffer only for the last chunk. 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 { /* UA-101 */ 1171 [INTF_PLAYBACK] = 0, 1172 [INTF_CAPTURE] = 1, 1173 [INTF_MIDI] = 2, 1174 }, 1175 { /* UA-1000 */ 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 /* make sure that userspace cannot create new requests */ 1317 snd_card_disconnect(ua->card); 1318 1319 /* make sure that there are no pending USB requests */ 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) }, /* UA-1000 high speed */ 1340 { USB_DEVICE(0x0582, 0x007d) }, /* UA-101 high speed */ 1341 { USB_DEVICE(0x0582, 0x008d) }, /* UA-101 full speed */ 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); 1358