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