linux/sound/usb/usx2y/us122l.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2007, 2008 Karsten Wiese <fzu@wemgehoertderstaat.de>
   3 *
   4 * This program is free software; you can redistribute it and/or modify it
   5 * under the terms of the GNU General Public License as published by the
   6 * Free Software Foundation; either version 2 of the License, or (at your
   7 * option) any later version.
   8 *
   9 * This program is distributed in the hope that it will be useful, but
  10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12 * for more details.
  13 *
  14 * You should have received a copy of the GNU General Public License
  15 * along with this program; if not, write to the Free Software Foundation,
  16 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17 */
  18
  19#include <linux/slab.h>
  20#include <linux/usb.h>
  21#include <linux/usb/audio.h>
  22#include <sound/core.h>
  23#include <sound/hwdep.h>
  24#include <sound/pcm.h>
  25#include <sound/initval.h>
  26#define MODNAME "US122L"
  27#include "usb_stream.c"
  28#include "../usbaudio.h"
  29#include "../midi.h"
  30#include "us122l.h"
  31
  32MODULE_AUTHOR("Karsten Wiese <fzu@wemgehoertderstaat.de>");
  33MODULE_DESCRIPTION("TASCAM "NAME_ALLCAPS" Version 0.5");
  34MODULE_LICENSE("GPL");
  35
  36static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-max */
  37static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* Id for this card */
  38                                                        /* Enable this card */
  39static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  40
  41module_param_array(index, int, NULL, 0444);
  42MODULE_PARM_DESC(index, "Index value for "NAME_ALLCAPS".");
  43module_param_array(id, charp, NULL, 0444);
  44MODULE_PARM_DESC(id, "ID string for "NAME_ALLCAPS".");
  45module_param_array(enable, bool, NULL, 0444);
  46MODULE_PARM_DESC(enable, "Enable "NAME_ALLCAPS".");
  47
  48static int snd_us122l_card_used[SNDRV_CARDS];
  49
  50
  51static int us122l_create_usbmidi(struct snd_card *card)
  52{
  53        static struct snd_usb_midi_endpoint_info quirk_data = {
  54                .out_ep = 4,
  55                .in_ep = 3,
  56                .out_cables =   0x001,
  57                .in_cables =    0x001
  58        };
  59        static struct snd_usb_audio_quirk quirk = {
  60                .vendor_name =  "US122L",
  61                .product_name = NAME_ALLCAPS,
  62                .ifnum =        1,
  63                .type = QUIRK_MIDI_US122L,
  64                .data = &quirk_data
  65        };
  66        struct usb_device *dev = US122L(card)->dev;
  67        struct usb_interface *iface = usb_ifnum_to_if(dev, 1);
  68
  69        return snd_usbmidi_create(card, iface,
  70                                  &US122L(card)->midi_list, &quirk);
  71}
  72
  73static int us144_create_usbmidi(struct snd_card *card)
  74{
  75        static struct snd_usb_midi_endpoint_info quirk_data = {
  76                .out_ep = 4,
  77                .in_ep = 3,
  78                .out_cables =   0x001,
  79                .in_cables =    0x001
  80        };
  81        static struct snd_usb_audio_quirk quirk = {
  82                .vendor_name =  "US144",
  83                .product_name = NAME_ALLCAPS,
  84                .ifnum =        0,
  85                .type = QUIRK_MIDI_US122L,
  86                .data = &quirk_data
  87        };
  88        struct usb_device *dev = US122L(card)->dev;
  89        struct usb_interface *iface = usb_ifnum_to_if(dev, 0);
  90
  91        return snd_usbmidi_create(card, iface,
  92                                  &US122L(card)->midi_list, &quirk);
  93}
  94
  95/*
  96 * Wrapper for usb_control_msg().
  97 * Allocates a temp buffer to prevent dmaing from/to the stack.
  98 */
  99static int us122l_ctl_msg(struct usb_device *dev, unsigned int pipe,
 100                          __u8 request, __u8 requesttype,
 101                          __u16 value, __u16 index, void *data,
 102                          __u16 size, int timeout)
 103{
 104        int err;
 105        void *buf = NULL;
 106
 107        if (size > 0) {
 108                buf = kmemdup(data, size, GFP_KERNEL);
 109                if (!buf)
 110                        return -ENOMEM;
 111        }
 112        err = usb_control_msg(dev, pipe, request, requesttype,
 113                              value, index, buf, size, timeout);
 114        if (size > 0) {
 115                memcpy(data, buf, size);
 116                kfree(buf);
 117        }
 118        return err;
 119}
 120
 121static void pt_info_set(struct usb_device *dev, u8 v)
 122{
 123        int ret;
 124
 125        ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
 126                              'I',
 127                              USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
 128                              v, 0, NULL, 0, 1000);
 129        snd_printdd(KERN_DEBUG "%i\n", ret);
 130}
 131
 132static void usb_stream_hwdep_vm_open(struct vm_area_struct *area)
 133{
 134        struct us122l *us122l = area->vm_private_data;
 135        atomic_inc(&us122l->mmap_count);
 136        snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count));
 137}
 138
 139static int usb_stream_hwdep_vm_fault(struct vm_area_struct *area,
 140                                     struct vm_fault *vmf)
 141{
 142        unsigned long offset;
 143        struct page *page;
 144        void *vaddr;
 145        struct us122l *us122l = area->vm_private_data;
 146        struct usb_stream *s;
 147
 148        mutex_lock(&us122l->mutex);
 149        s = us122l->sk.s;
 150        if (!s)
 151                goto unlock;
 152
 153        offset = vmf->pgoff << PAGE_SHIFT;
 154        if (offset < PAGE_ALIGN(s->read_size))
 155                vaddr = (char *)s + offset;
 156        else {
 157                offset -= PAGE_ALIGN(s->read_size);
 158                if (offset >= PAGE_ALIGN(s->write_size))
 159                        goto unlock;
 160
 161                vaddr = us122l->sk.write_page + offset;
 162        }
 163        page = virt_to_page(vaddr);
 164
 165        get_page(page);
 166        mutex_unlock(&us122l->mutex);
 167
 168        vmf->page = page;
 169
 170        return 0;
 171unlock:
 172        mutex_unlock(&us122l->mutex);
 173        return VM_FAULT_SIGBUS;
 174}
 175
 176static void usb_stream_hwdep_vm_close(struct vm_area_struct *area)
 177{
 178        struct us122l *us122l = area->vm_private_data;
 179        atomic_dec(&us122l->mmap_count);
 180        snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count));
 181}
 182
 183static const struct vm_operations_struct usb_stream_hwdep_vm_ops = {
 184        .open = usb_stream_hwdep_vm_open,
 185        .fault = usb_stream_hwdep_vm_fault,
 186        .close = usb_stream_hwdep_vm_close,
 187};
 188
 189
 190static int usb_stream_hwdep_open(struct snd_hwdep *hw, struct file *file)
 191{
 192        struct us122l   *us122l = hw->private_data;
 193        struct usb_interface *iface;
 194        snd_printdd(KERN_DEBUG "%p %p\n", hw, file);
 195        if (hw->used >= 2)
 196                return -EBUSY;
 197
 198        if (!us122l->first)
 199                us122l->first = file;
 200
 201        if (us122l->dev->descriptor.idProduct == USB_ID_US144 ||
 202            us122l->dev->descriptor.idProduct == USB_ID_US144MKII) {
 203                iface = usb_ifnum_to_if(us122l->dev, 0);
 204                usb_autopm_get_interface(iface);
 205        }
 206        iface = usb_ifnum_to_if(us122l->dev, 1);
 207        usb_autopm_get_interface(iface);
 208        return 0;
 209}
 210
 211static int usb_stream_hwdep_release(struct snd_hwdep *hw, struct file *file)
 212{
 213        struct us122l   *us122l = hw->private_data;
 214        struct usb_interface *iface;
 215        snd_printdd(KERN_DEBUG "%p %p\n", hw, file);
 216
 217        if (us122l->dev->descriptor.idProduct == USB_ID_US144 ||
 218            us122l->dev->descriptor.idProduct == USB_ID_US144MKII) {
 219                iface = usb_ifnum_to_if(us122l->dev, 0);
 220                usb_autopm_put_interface(iface);
 221        }
 222        iface = usb_ifnum_to_if(us122l->dev, 1);
 223        usb_autopm_put_interface(iface);
 224        if (us122l->first == file)
 225                us122l->first = NULL;
 226        mutex_lock(&us122l->mutex);
 227        if (us122l->master == file)
 228                us122l->master = us122l->slave;
 229
 230        us122l->slave = NULL;
 231        mutex_unlock(&us122l->mutex);
 232        return 0;
 233}
 234
 235static int usb_stream_hwdep_mmap(struct snd_hwdep *hw,
 236                                 struct file *filp, struct vm_area_struct *area)
 237{
 238        unsigned long   size = area->vm_end - area->vm_start;
 239        struct us122l   *us122l = hw->private_data;
 240        unsigned long offset;
 241        struct usb_stream *s;
 242        int err = 0;
 243        bool read;
 244
 245        offset = area->vm_pgoff << PAGE_SHIFT;
 246        mutex_lock(&us122l->mutex);
 247        s = us122l->sk.s;
 248        read = offset < s->read_size;
 249        if (read && area->vm_flags & VM_WRITE) {
 250                err = -EPERM;
 251                goto out;
 252        }
 253        snd_printdd(KERN_DEBUG "%lu %u\n", size,
 254                    read ? s->read_size : s->write_size);
 255        /* if userspace tries to mmap beyond end of our buffer, fail */
 256        if (size > PAGE_ALIGN(read ? s->read_size : s->write_size)) {
 257                snd_printk(KERN_WARNING "%lu > %u\n", size,
 258                           read ? s->read_size : s->write_size);
 259                err = -EINVAL;
 260                goto out;
 261        }
 262
 263        area->vm_ops = &usb_stream_hwdep_vm_ops;
 264        area->vm_flags |= VM_RESERVED;
 265        area->vm_private_data = us122l;
 266        atomic_inc(&us122l->mmap_count);
 267out:
 268        mutex_unlock(&us122l->mutex);
 269        return err;
 270}
 271
 272static unsigned int usb_stream_hwdep_poll(struct snd_hwdep *hw,
 273                                          struct file *file, poll_table *wait)
 274{
 275        struct us122l   *us122l = hw->private_data;
 276        unsigned        *polled;
 277        unsigned int    mask;
 278
 279        poll_wait(file, &us122l->sk.sleep, wait);
 280
 281        mask = POLLIN | POLLOUT | POLLWRNORM | POLLERR;
 282        if (mutex_trylock(&us122l->mutex)) {
 283                struct usb_stream *s = us122l->sk.s;
 284                if (s && s->state == usb_stream_ready) {
 285                        if (us122l->first == file)
 286                                polled = &s->periods_polled;
 287                        else
 288                                polled = &us122l->second_periods_polled;
 289                        if (*polled != s->periods_done) {
 290                                *polled = s->periods_done;
 291                                mask = POLLIN | POLLOUT | POLLWRNORM;
 292                        } else
 293                                mask = 0;
 294                }
 295                mutex_unlock(&us122l->mutex);
 296        }
 297        return mask;
 298}
 299
 300static void us122l_stop(struct us122l *us122l)
 301{
 302        struct list_head *p;
 303        list_for_each(p, &us122l->midi_list)
 304                snd_usbmidi_input_stop(p);
 305
 306        usb_stream_stop(&us122l->sk);
 307        usb_stream_free(&us122l->sk);
 308}
 309
 310static int us122l_set_sample_rate(struct usb_device *dev, int rate)
 311{
 312        unsigned int ep = 0x81;
 313        unsigned char data[3];
 314        int err;
 315
 316        data[0] = rate;
 317        data[1] = rate >> 8;
 318        data[2] = rate >> 16;
 319        err = us122l_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
 320                             USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
 321                             UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, data, 3, 1000);
 322        if (err < 0)
 323                snd_printk(KERN_ERR "%d: cannot set freq %d to ep 0x%x\n",
 324                           dev->devnum, rate, ep);
 325        return err;
 326}
 327
 328static bool us122l_start(struct us122l *us122l,
 329                         unsigned rate, unsigned period_frames)
 330{
 331        struct list_head *p;
 332        int err;
 333        unsigned use_packsize = 0;
 334        bool success = false;
 335
 336        if (us122l->dev->speed == USB_SPEED_HIGH) {
 337                /* The us-122l's descriptor defaults to iso max_packsize 78,
 338                   which isn't needed for samplerates <= 48000.
 339                   Lets save some memory:
 340                */
 341                switch (rate) {
 342                case 44100:
 343                        use_packsize = 36;
 344                        break;
 345                case 48000:
 346                        use_packsize = 42;
 347                        break;
 348                case 88200:
 349                        use_packsize = 72;
 350                        break;
 351                }
 352        }
 353        if (!usb_stream_new(&us122l->sk, us122l->dev, 1, 2,
 354                            rate, use_packsize, period_frames, 6))
 355                goto out;
 356
 357        err = us122l_set_sample_rate(us122l->dev, rate);
 358        if (err < 0) {
 359                us122l_stop(us122l);
 360                snd_printk(KERN_ERR "us122l_set_sample_rate error \n");
 361                goto out;
 362        }
 363        err = usb_stream_start(&us122l->sk);
 364        if (err < 0) {
 365                us122l_stop(us122l);
 366                snd_printk(KERN_ERR "us122l_start error %i \n", err);
 367                goto out;
 368        }
 369        list_for_each(p, &us122l->midi_list)
 370                snd_usbmidi_input_start(p);
 371        success = true;
 372out:
 373        return success;
 374}
 375
 376static int usb_stream_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
 377                                  unsigned cmd, unsigned long arg)
 378{
 379        struct usb_stream_config *cfg;
 380        struct us122l *us122l = hw->private_data;
 381        struct usb_stream *s;
 382        unsigned min_period_frames;
 383        int err = 0;
 384        bool high_speed;
 385
 386        if (cmd != SNDRV_USB_STREAM_IOCTL_SET_PARAMS)
 387                return -ENOTTY;
 388
 389        cfg = memdup_user((void *)arg, sizeof(*cfg));
 390        if (IS_ERR(cfg))
 391                return PTR_ERR(cfg);
 392
 393        if (cfg->version != USB_STREAM_INTERFACE_VERSION) {
 394                err = -ENXIO;
 395                goto free;
 396        }
 397        high_speed = us122l->dev->speed == USB_SPEED_HIGH;
 398        if ((cfg->sample_rate != 44100 && cfg->sample_rate != 48000  &&
 399             (!high_speed ||
 400              (cfg->sample_rate != 88200 && cfg->sample_rate != 96000))) ||
 401            cfg->frame_size != 6 ||
 402            cfg->period_frames > 0x3000) {
 403                err = -EINVAL;
 404                goto free;
 405        }
 406        switch (cfg->sample_rate) {
 407        case 44100:
 408                min_period_frames = 48;
 409                break;
 410        case 48000:
 411                min_period_frames = 52;
 412                break;
 413        default:
 414                min_period_frames = 104;
 415                break;
 416        }
 417        if (!high_speed)
 418                min_period_frames <<= 1;
 419        if (cfg->period_frames < min_period_frames) {
 420                err = -EINVAL;
 421                goto free;
 422        }
 423
 424        snd_power_wait(hw->card, SNDRV_CTL_POWER_D0);
 425
 426        mutex_lock(&us122l->mutex);
 427        s = us122l->sk.s;
 428        if (!us122l->master)
 429                us122l->master = file;
 430        else if (us122l->master != file) {
 431                if (!s || memcmp(cfg, &s->cfg, sizeof(*cfg))) {
 432                        err = -EIO;
 433                        goto unlock;
 434                }
 435                us122l->slave = file;
 436        }
 437        if (!s || memcmp(cfg, &s->cfg, sizeof(*cfg)) ||
 438            s->state == usb_stream_xrun) {
 439                us122l_stop(us122l);
 440                if (!us122l_start(us122l, cfg->sample_rate, cfg->period_frames))
 441                        err = -EIO;
 442                else
 443                        err = 1;
 444        }
 445unlock:
 446        mutex_unlock(&us122l->mutex);
 447free:
 448        kfree(cfg);
 449        wake_up_all(&us122l->sk.sleep);
 450        return err;
 451}
 452
 453#define SND_USB_STREAM_ID "USB STREAM"
 454static int usb_stream_hwdep_new(struct snd_card *card)
 455{
 456        int err;
 457        struct snd_hwdep *hw;
 458        struct usb_device *dev = US122L(card)->dev;
 459
 460        err = snd_hwdep_new(card, SND_USB_STREAM_ID, 0, &hw);
 461        if (err < 0)
 462                return err;
 463
 464        hw->iface = SNDRV_HWDEP_IFACE_USB_STREAM;
 465        hw->private_data = US122L(card);
 466        hw->ops.open = usb_stream_hwdep_open;
 467        hw->ops.release = usb_stream_hwdep_release;
 468        hw->ops.ioctl = usb_stream_hwdep_ioctl;
 469        hw->ops.ioctl_compat = usb_stream_hwdep_ioctl;
 470        hw->ops.mmap = usb_stream_hwdep_mmap;
 471        hw->ops.poll = usb_stream_hwdep_poll;
 472
 473        sprintf(hw->name, "/proc/bus/usb/%03d/%03d/hwdeppcm",
 474                dev->bus->busnum, dev->devnum);
 475        return 0;
 476}
 477
 478
 479static bool us122l_create_card(struct snd_card *card)
 480{
 481        int err;
 482        struct us122l *us122l = US122L(card);
 483
 484        if (us122l->dev->descriptor.idProduct == USB_ID_US144 ||
 485            us122l->dev->descriptor.idProduct == USB_ID_US144MKII) {
 486                err = usb_set_interface(us122l->dev, 0, 1);
 487                if (err) {
 488                        snd_printk(KERN_ERR "usb_set_interface error \n");
 489                        return false;
 490                }
 491        }
 492        err = usb_set_interface(us122l->dev, 1, 1);
 493        if (err) {
 494                snd_printk(KERN_ERR "usb_set_interface error \n");
 495                return false;
 496        }
 497
 498        pt_info_set(us122l->dev, 0x11);
 499        pt_info_set(us122l->dev, 0x10);
 500
 501        if (!us122l_start(us122l, 44100, 256))
 502                return false;
 503
 504        if (us122l->dev->descriptor.idProduct == USB_ID_US144 ||
 505            us122l->dev->descriptor.idProduct == USB_ID_US144MKII)
 506                err = us144_create_usbmidi(card);
 507        else
 508                err = us122l_create_usbmidi(card);
 509        if (err < 0) {
 510                snd_printk(KERN_ERR "us122l_create_usbmidi error %i \n", err);
 511                us122l_stop(us122l);
 512                return false;
 513        }
 514        err = usb_stream_hwdep_new(card);
 515        if (err < 0) {
 516/* release the midi resources */
 517                struct list_head *p;
 518                list_for_each(p, &us122l->midi_list)
 519                        snd_usbmidi_disconnect(p);
 520
 521                us122l_stop(us122l);
 522                return false;
 523        }
 524        return true;
 525}
 526
 527static void snd_us122l_free(struct snd_card *card)
 528{
 529        struct us122l   *us122l = US122L(card);
 530        int             index = us122l->card_index;
 531        if (index >= 0  &&  index < SNDRV_CARDS)
 532                snd_us122l_card_used[index] = 0;
 533}
 534
 535static int usx2y_create_card(struct usb_device *device, struct snd_card **cardp)
 536{
 537        int             dev;
 538        struct snd_card *card;
 539        int err;
 540
 541        for (dev = 0; dev < SNDRV_CARDS; ++dev)
 542                if (enable[dev] && !snd_us122l_card_used[dev])
 543                        break;
 544        if (dev >= SNDRV_CARDS)
 545                return -ENODEV;
 546        err = snd_card_create(index[dev], id[dev], THIS_MODULE,
 547                              sizeof(struct us122l), &card);
 548        if (err < 0)
 549                return err;
 550        snd_us122l_card_used[US122L(card)->card_index = dev] = 1;
 551        card->private_free = snd_us122l_free;
 552        US122L(card)->dev = device;
 553        mutex_init(&US122L(card)->mutex);
 554        init_waitqueue_head(&US122L(card)->sk.sleep);
 555        INIT_LIST_HEAD(&US122L(card)->midi_list);
 556        strcpy(card->driver, "USB "NAME_ALLCAPS"");
 557        sprintf(card->shortname, "TASCAM "NAME_ALLCAPS"");
 558        sprintf(card->longname, "%s (%x:%x if %d at %03d/%03d)",
 559                card->shortname,
 560                le16_to_cpu(device->descriptor.idVendor),
 561                le16_to_cpu(device->descriptor.idProduct),
 562                0,
 563                US122L(card)->dev->bus->busnum,
 564                US122L(card)->dev->devnum
 565                );
 566        *cardp = card;
 567        return 0;
 568}
 569
 570static int us122l_usb_probe(struct usb_interface *intf,
 571                            const struct usb_device_id *device_id,
 572                            struct snd_card **cardp)
 573{
 574        struct usb_device *device = interface_to_usbdev(intf);
 575        struct snd_card *card;
 576        int err;
 577
 578        err = usx2y_create_card(device, &card);
 579        if (err < 0)
 580                return err;
 581
 582        snd_card_set_dev(card, &intf->dev);
 583        if (!us122l_create_card(card)) {
 584                snd_card_free(card);
 585                return -EINVAL;
 586        }
 587
 588        err = snd_card_register(card);
 589        if (err < 0) {
 590                snd_card_free(card);
 591                return err;
 592        }
 593
 594        usb_get_intf(usb_ifnum_to_if(device, 0));
 595        usb_get_dev(device);
 596        *cardp = card;
 597        return 0;
 598}
 599
 600static int snd_us122l_probe(struct usb_interface *intf,
 601                            const struct usb_device_id *id)
 602{
 603        struct usb_device *device = interface_to_usbdev(intf);
 604        struct snd_card *card;
 605        int err;
 606
 607        if ((device->descriptor.idProduct == USB_ID_US144 ||
 608             device->descriptor.idProduct == USB_ID_US144MKII)
 609                && device->speed == USB_SPEED_HIGH) {
 610                snd_printk(KERN_ERR "disable ehci-hcd to run US-144 \n");
 611                return -ENODEV;
 612        }
 613
 614        snd_printdd(KERN_DEBUG"%p:%i\n",
 615                    intf, intf->cur_altsetting->desc.bInterfaceNumber);
 616        if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
 617                return 0;
 618
 619        err = us122l_usb_probe(usb_get_intf(intf), id, &card);
 620        if (err < 0) {
 621                usb_put_intf(intf);
 622                return err;
 623        }
 624
 625        usb_set_intfdata(intf, card);
 626        return 0;
 627}
 628
 629static void snd_us122l_disconnect(struct usb_interface *intf)
 630{
 631        struct snd_card *card;
 632        struct us122l *us122l;
 633        struct list_head *p;
 634
 635        card = usb_get_intfdata(intf);
 636        if (!card)
 637                return;
 638
 639        snd_card_disconnect(card);
 640
 641        us122l = US122L(card);
 642        mutex_lock(&us122l->mutex);
 643        us122l_stop(us122l);
 644        mutex_unlock(&us122l->mutex);
 645
 646/* release the midi resources */
 647        list_for_each(p, &us122l->midi_list) {
 648                snd_usbmidi_disconnect(p);
 649        }
 650
 651        usb_put_intf(usb_ifnum_to_if(us122l->dev, 0));
 652        usb_put_intf(usb_ifnum_to_if(us122l->dev, 1));
 653        usb_put_dev(us122l->dev);
 654
 655        while (atomic_read(&us122l->mmap_count))
 656                msleep(500);
 657
 658        snd_card_free(card);
 659}
 660
 661static int snd_us122l_suspend(struct usb_interface *intf, pm_message_t message)
 662{
 663        struct snd_card *card;
 664        struct us122l *us122l;
 665        struct list_head *p;
 666
 667        card = usb_get_intfdata(intf);
 668        if (!card)
 669                return 0;
 670        snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
 671
 672        us122l = US122L(card);
 673        if (!us122l)
 674                return 0;
 675
 676        list_for_each(p, &us122l->midi_list)
 677                snd_usbmidi_input_stop(p);
 678
 679        mutex_lock(&us122l->mutex);
 680        usb_stream_stop(&us122l->sk);
 681        mutex_unlock(&us122l->mutex);
 682
 683        return 0;
 684}
 685
 686static int snd_us122l_resume(struct usb_interface *intf)
 687{
 688        struct snd_card *card;
 689        struct us122l *us122l;
 690        struct list_head *p;
 691        int err;
 692
 693        card = usb_get_intfdata(intf);
 694        if (!card)
 695                return 0;
 696
 697        us122l = US122L(card);
 698        if (!us122l)
 699                return 0;
 700
 701        mutex_lock(&us122l->mutex);
 702        /* needed, doesn't restart without: */
 703        if (us122l->dev->descriptor.idProduct == USB_ID_US144 ||
 704            us122l->dev->descriptor.idProduct == USB_ID_US144MKII) {
 705                err = usb_set_interface(us122l->dev, 0, 1);
 706                if (err) {
 707                        snd_printk(KERN_ERR "usb_set_interface error \n");
 708                        goto unlock;
 709                }
 710        }
 711        err = usb_set_interface(us122l->dev, 1, 1);
 712        if (err) {
 713                snd_printk(KERN_ERR "usb_set_interface error \n");
 714                goto unlock;
 715        }
 716
 717        pt_info_set(us122l->dev, 0x11);
 718        pt_info_set(us122l->dev, 0x10);
 719
 720        err = us122l_set_sample_rate(us122l->dev,
 721                                     us122l->sk.s->cfg.sample_rate);
 722        if (err < 0) {
 723                snd_printk(KERN_ERR "us122l_set_sample_rate error \n");
 724                goto unlock;
 725        }
 726        err = usb_stream_start(&us122l->sk);
 727        if (err)
 728                goto unlock;
 729
 730        list_for_each(p, &us122l->midi_list)
 731                snd_usbmidi_input_start(p);
 732unlock:
 733        mutex_unlock(&us122l->mutex);
 734        snd_power_change_state(card, SNDRV_CTL_POWER_D0);
 735        return err;
 736}
 737
 738static struct usb_device_id snd_us122l_usb_id_table[] = {
 739        {
 740                .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
 741                .idVendor =     0x0644,
 742                .idProduct =    USB_ID_US122L
 743        },
 744        {       /* US-144 only works at USB1.1! Disable module ehci-hcd. */
 745                .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
 746                .idVendor =     0x0644,
 747                .idProduct =    USB_ID_US144
 748        },
 749        {
 750                .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
 751                .idVendor =     0x0644,
 752                .idProduct =    USB_ID_US122MKII
 753        },
 754        {
 755                .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
 756                .idVendor =     0x0644,
 757                .idProduct =    USB_ID_US144MKII
 758        },
 759        { /* terminator */ }
 760};
 761
 762MODULE_DEVICE_TABLE(usb, snd_us122l_usb_id_table);
 763static struct usb_driver snd_us122l_usb_driver = {
 764        .name =         "snd-usb-us122l",
 765        .probe =        snd_us122l_probe,
 766        .disconnect =   snd_us122l_disconnect,
 767        .suspend =      snd_us122l_suspend,
 768        .resume =       snd_us122l_resume,
 769        .reset_resume = snd_us122l_resume,
 770        .id_table =     snd_us122l_usb_id_table,
 771        .supports_autosuspend = 1
 772};
 773
 774
 775static int __init snd_us122l_module_init(void)
 776{
 777        return usb_register(&snd_us122l_usb_driver);
 778}
 779
 780static void __exit snd_us122l_module_exit(void)
 781{
 782        usb_deregister(&snd_us122l_usb_driver);
 783}
 784
 785module_init(snd_us122l_module_init)
 786module_exit(snd_us122l_module_exit)
 787