linux/drivers/staging/line6/driver.c
<<
>>
Prefs
   1/*
   2 * Line6 Linux USB driver - 0.9.1beta
   3 *
   4 * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
   5 *
   6 *      This program is free software; you can redistribute it and/or
   7 *      modify it under the terms of the GNU General Public License as
   8 *      published by the Free Software Foundation, version 2.
   9 *
  10 */
  11
  12#include <linux/kernel.h>
  13#include <linux/module.h>
  14#include <linux/slab.h>
  15#include <linux/usb.h>
  16
  17#include "audio.h"
  18#include "capture.h"
  19#include "driver.h"
  20#include "midi.h"
  21#include "playback.h"
  22#include "pod.h"
  23#include "podhd.h"
  24#include "revision.h"
  25#include "toneport.h"
  26#include "usbdefs.h"
  27#include "variax.h"
  28
  29#define DRIVER_AUTHOR  "Markus Grabner <grabner@icg.tugraz.at>"
  30#define DRIVER_DESC    "Line6 USB Driver"
  31#define DRIVER_VERSION "0.9.1beta" DRIVER_REVISION
  32
  33/* table of devices that work with this driver */
  34static const struct usb_device_id line6_id_table[] = {
  35        {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_BASSPODXT)},
  36        {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_BASSPODXTLIVE)},
  37        {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_BASSPODXTPRO)},
  38        {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_GUITARPORT)},
  39        {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_POCKETPOD)},
  40        {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODHD300)},
  41        {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODHD500)},
  42        {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODSTUDIO_GX)},
  43        {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODSTUDIO_UX1)},
  44        {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODSTUDIO_UX2)},
  45        {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODX3)},
  46        {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODX3LIVE)},
  47        {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODXT)},
  48        {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODXTLIVE)},
  49        {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODXTPRO)},
  50        {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_TONEPORT_GX)},
  51        {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_TONEPORT_UX1)},
  52        {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_TONEPORT_UX2)},
  53        {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_VARIAX)},
  54        {},
  55};
  56
  57MODULE_DEVICE_TABLE(usb, line6_id_table);
  58
  59/* *INDENT-OFF* */
  60static struct line6_properties line6_properties_table[] = {
  61        { LINE6_BIT_BASSPODXT,     "BassPODxt",     "BassPODxt",        LINE6_BIT_CONTROL_PCM_HWMON },
  62        { LINE6_BIT_BASSPODXTLIVE, "BassPODxtLive", "BassPODxt Live",   LINE6_BIT_CONTROL_PCM_HWMON },
  63        { LINE6_BIT_BASSPODXTPRO,  "BassPODxtPro",  "BassPODxt Pro",    LINE6_BIT_CONTROL_PCM_HWMON },
  64        { LINE6_BIT_GUITARPORT,    "GuitarPort",    "GuitarPort",       LINE6_BIT_PCM               },
  65        { LINE6_BIT_POCKETPOD,     "PocketPOD",     "Pocket POD",       LINE6_BIT_CONTROL           },
  66        { LINE6_BIT_PODHD300,      "PODHD300",      "POD HD300",        LINE6_BIT_CONTROL_PCM_HWMON },
  67        { LINE6_BIT_PODHD500,      "PODHD500",      "POD HD500",        LINE6_BIT_CONTROL_PCM_HWMON },
  68        { LINE6_BIT_PODSTUDIO_GX,  "PODStudioGX",   "POD Studio GX",    LINE6_BIT_PCM               },
  69        { LINE6_BIT_PODSTUDIO_UX1, "PODStudioUX1",  "POD Studio UX1",   LINE6_BIT_PCM               },
  70        { LINE6_BIT_PODSTUDIO_UX2, "PODStudioUX2",  "POD Studio UX2",   LINE6_BIT_PCM               },
  71        { LINE6_BIT_PODX3,         "PODX3",         "POD X3",           LINE6_BIT_PCM               },
  72        { LINE6_BIT_PODX3LIVE,     "PODX3Live",     "POD X3 Live",      LINE6_BIT_PCM               },
  73        { LINE6_BIT_PODXT,         "PODxt",         "PODxt",            LINE6_BIT_CONTROL_PCM_HWMON },
  74        { LINE6_BIT_PODXTLIVE,     "PODxtLive",     "PODxt Live",       LINE6_BIT_CONTROL_PCM_HWMON },
  75        { LINE6_BIT_PODXTPRO,      "PODxtPro",      "PODxt Pro",        LINE6_BIT_CONTROL_PCM_HWMON },
  76        { LINE6_BIT_TONEPORT_GX,   "TonePortGX",    "TonePort GX",      LINE6_BIT_PCM               },
  77        { LINE6_BIT_TONEPORT_UX1,  "TonePortUX1",   "TonePort UX1",     LINE6_BIT_PCM               },
  78        { LINE6_BIT_TONEPORT_UX2,  "TonePortUX2",   "TonePort UX2",     LINE6_BIT_PCM               },
  79        { LINE6_BIT_VARIAX,        "Variax",        "Variax Workbench", LINE6_BIT_CONTROL           },
  80};
  81/* *INDENT-ON* */
  82
  83/*
  84        This is Line6's MIDI manufacturer ID.
  85*/
  86const unsigned char line6_midi_id[] = {
  87        0x00, 0x01, 0x0c
  88};
  89
  90/*
  91        Code to request version of POD, Variax interface
  92        (and maybe other devices).
  93*/
  94static const char line6_request_version[] = {
  95        0xf0, 0x7e, 0x7f, 0x06, 0x01, 0xf7
  96};
  97
  98/**
  99         Class for asynchronous messages.
 100*/
 101struct message {
 102        struct usb_line6 *line6;
 103        const char *buffer;
 104        int size;
 105        int done;
 106};
 107
 108/*
 109        Forward declarations.
 110*/
 111static void line6_data_received(struct urb *urb);
 112static int line6_send_raw_message_async_part(struct message *msg,
 113                                             struct urb *urb);
 114
 115/*
 116        Start to listen on endpoint.
 117*/
 118static int line6_start_listen(struct usb_line6 *line6)
 119{
 120        int err;
 121        usb_fill_int_urb(line6->urb_listen, line6->usbdev,
 122                         usb_rcvintpipe(line6->usbdev, line6->ep_control_read),
 123                         line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
 124                         line6_data_received, line6, line6->interval);
 125        line6->urb_listen->actual_length = 0;
 126        err = usb_submit_urb(line6->urb_listen, GFP_ATOMIC);
 127        return err;
 128}
 129
 130/*
 131        Stop listening on endpoint.
 132*/
 133static void line6_stop_listen(struct usb_line6 *line6)
 134{
 135        usb_kill_urb(line6->urb_listen);
 136}
 137
 138/*
 139        Send raw message in pieces of wMaxPacketSize bytes.
 140*/
 141int line6_send_raw_message(struct usb_line6 *line6, const char *buffer,
 142                           int size)
 143{
 144        int i, done = 0;
 145
 146        for (i = 0; i < size; i += line6->max_packet_size) {
 147                int partial;
 148                const char *frag_buf = buffer + i;
 149                int frag_size = min(line6->max_packet_size, size - i);
 150                int retval;
 151
 152                retval = usb_interrupt_msg(line6->usbdev,
 153                                           usb_sndintpipe(line6->usbdev,
 154                                                          line6->ep_control_write),
 155                                           (char *)frag_buf, frag_size,
 156                                           &partial, LINE6_TIMEOUT * HZ);
 157
 158                if (retval) {
 159                        dev_err(line6->ifcdev,
 160                                "usb_interrupt_msg failed (%d)\n", retval);
 161                        break;
 162                }
 163
 164                done += frag_size;
 165        }
 166
 167        return done;
 168}
 169
 170/*
 171        Notification of completion of asynchronous request transmission.
 172*/
 173static void line6_async_request_sent(struct urb *urb)
 174{
 175        struct message *msg = (struct message *)urb->context;
 176
 177        if (msg->done >= msg->size) {
 178                usb_free_urb(urb);
 179                kfree(msg);
 180        } else
 181                line6_send_raw_message_async_part(msg, urb);
 182}
 183
 184/*
 185        Asynchronously send part of a raw message.
 186*/
 187static int line6_send_raw_message_async_part(struct message *msg,
 188                                             struct urb *urb)
 189{
 190        int retval;
 191        struct usb_line6 *line6 = msg->line6;
 192        int done = msg->done;
 193        int bytes = min(msg->size - done, line6->max_packet_size);
 194
 195        usb_fill_int_urb(urb, line6->usbdev,
 196                         usb_sndintpipe(line6->usbdev, line6->ep_control_write),
 197                         (char *)msg->buffer + done, bytes,
 198                         line6_async_request_sent, msg, line6->interval);
 199
 200        msg->done += bytes;
 201        retval = usb_submit_urb(urb, GFP_ATOMIC);
 202
 203        if (retval < 0) {
 204                dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n",
 205                        __func__, retval);
 206                usb_free_urb(urb);
 207                kfree(msg);
 208                return -EINVAL;
 209        }
 210
 211        return 0;
 212}
 213
 214/*
 215        Setup and start timer.
 216*/
 217void line6_start_timer(struct timer_list *timer, unsigned int msecs,
 218                       void (*function) (unsigned long), unsigned long data)
 219{
 220        setup_timer(timer, function, data);
 221        timer->expires = jiffies + msecs * HZ / 1000;
 222        add_timer(timer);
 223}
 224
 225/*
 226        Asynchronously send raw message.
 227*/
 228int line6_send_raw_message_async(struct usb_line6 *line6, const char *buffer,
 229                                 int size)
 230{
 231        struct message *msg;
 232        struct urb *urb;
 233
 234        /* create message: */
 235        msg = kmalloc(sizeof(struct message), GFP_ATOMIC);
 236        if (msg == NULL)
 237                return -ENOMEM;
 238
 239        /* create URB: */
 240        urb = usb_alloc_urb(0, GFP_ATOMIC);
 241
 242        if (urb == NULL) {
 243                kfree(msg);
 244                dev_err(line6->ifcdev, "Out of memory\n");
 245                return -ENOMEM;
 246        }
 247
 248        /* set message data: */
 249        msg->line6 = line6;
 250        msg->buffer = buffer;
 251        msg->size = size;
 252        msg->done = 0;
 253
 254        /* start sending: */
 255        return line6_send_raw_message_async_part(msg, urb);
 256}
 257
 258/*
 259        Send asynchronous device version request.
 260*/
 261int line6_version_request_async(struct usb_line6 *line6)
 262{
 263        char *buffer;
 264        int retval;
 265
 266        buffer = kmemdup(line6_request_version,
 267                        sizeof(line6_request_version), GFP_ATOMIC);
 268        if (buffer == NULL) {
 269                dev_err(line6->ifcdev, "Out of memory");
 270                return -ENOMEM;
 271        }
 272
 273        retval = line6_send_raw_message_async(line6, buffer,
 274                                              sizeof(line6_request_version));
 275        kfree(buffer);
 276        return retval;
 277}
 278
 279/*
 280        Send sysex message in pieces of wMaxPacketSize bytes.
 281*/
 282int line6_send_sysex_message(struct usb_line6 *line6, const char *buffer,
 283                             int size)
 284{
 285        return line6_send_raw_message(line6, buffer,
 286                                      size + SYSEX_EXTRA_SIZE) -
 287            SYSEX_EXTRA_SIZE;
 288}
 289
 290/*
 291        Allocate buffer for sysex message and prepare header.
 292        @param code sysex message code
 293        @param size number of bytes between code and sysex end
 294*/
 295char *line6_alloc_sysex_buffer(struct usb_line6 *line6, int code1, int code2,
 296                               int size)
 297{
 298        char *buffer = kmalloc(size + SYSEX_EXTRA_SIZE, GFP_ATOMIC);
 299
 300        if (!buffer)
 301                return NULL;
 302
 303        buffer[0] = LINE6_SYSEX_BEGIN;
 304        memcpy(buffer + 1, line6_midi_id, sizeof(line6_midi_id));
 305        buffer[sizeof(line6_midi_id) + 1] = code1;
 306        buffer[sizeof(line6_midi_id) + 2] = code2;
 307        buffer[sizeof(line6_midi_id) + 3 + size] = LINE6_SYSEX_END;
 308        return buffer;
 309}
 310
 311/*
 312        Notification of data received from the Line6 device.
 313*/
 314static void line6_data_received(struct urb *urb)
 315{
 316        struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
 317        struct midi_buffer *mb = &line6->line6midi->midibuf_in;
 318        int done;
 319
 320        if (urb->status == -ESHUTDOWN)
 321                return;
 322
 323        done =
 324            line6_midibuf_write(mb, urb->transfer_buffer, urb->actual_length);
 325
 326        if (done < urb->actual_length) {
 327                line6_midibuf_ignore(mb, done);
 328                dev_dbg(line6->ifcdev, "%d %d buffer overflow - message skipped\n",
 329                        done, urb->actual_length);
 330        }
 331
 332        for (;;) {
 333                done =
 334                    line6_midibuf_read(mb, line6->buffer_message,
 335                                       LINE6_MESSAGE_MAXLEN);
 336
 337                if (done == 0)
 338                        break;
 339
 340                line6->message_length = done;
 341                line6_midi_receive(line6, line6->buffer_message, done);
 342
 343                switch (line6->usbdev->descriptor.idProduct) {
 344                case LINE6_DEVID_BASSPODXT:
 345                case LINE6_DEVID_BASSPODXTLIVE:
 346                case LINE6_DEVID_BASSPODXTPRO:
 347                case LINE6_DEVID_PODXT:
 348                case LINE6_DEVID_PODXTPRO:
 349                case LINE6_DEVID_POCKETPOD:
 350                        line6_pod_process_message((struct usb_line6_pod *)
 351                                                  line6);
 352                        break;
 353
 354                case LINE6_DEVID_PODHD300:
 355                case LINE6_DEVID_PODHD500:
 356                        break; /* let userspace handle MIDI */
 357
 358                case LINE6_DEVID_PODXTLIVE:
 359                        switch (line6->interface_number) {
 360                        case PODXTLIVE_INTERFACE_POD:
 361                                line6_pod_process_message((struct usb_line6_pod
 362                                                           *)line6);
 363                                break;
 364
 365                        case PODXTLIVE_INTERFACE_VARIAX:
 366                                line6_variax_process_message((struct
 367                                                              usb_line6_variax
 368                                                              *)line6);
 369                                break;
 370
 371                        default:
 372                                dev_err(line6->ifcdev,
 373                                        "PODxt Live interface %d not supported\n",
 374                                        line6->interface_number);
 375                        }
 376                        break;
 377
 378                case LINE6_DEVID_VARIAX:
 379                        line6_variax_process_message((struct usb_line6_variax *)
 380                                                     line6);
 381                        break;
 382
 383                default:
 384                        MISSING_CASE;
 385                }
 386        }
 387
 388        line6_start_listen(line6);
 389}
 390
 391/*
 392        Send channel number (i.e., switch to a different sound).
 393*/
 394int line6_send_program(struct usb_line6 *line6, u8 value)
 395{
 396        int retval;
 397        unsigned char *buffer;
 398        int partial;
 399
 400        buffer = kmalloc(2, GFP_KERNEL);
 401        if (!buffer)
 402                return -ENOMEM;
 403
 404        buffer[0] = LINE6_PROGRAM_CHANGE | LINE6_CHANNEL_HOST;
 405        buffer[1] = value;
 406
 407        retval = usb_interrupt_msg(line6->usbdev,
 408                                   usb_sndintpipe(line6->usbdev,
 409                                                  line6->ep_control_write),
 410                                   buffer, 2, &partial, LINE6_TIMEOUT * HZ);
 411
 412        if (retval)
 413                dev_err(line6->ifcdev, "usb_interrupt_msg failed (%d)\n",
 414                        retval);
 415
 416        kfree(buffer);
 417        return retval;
 418}
 419
 420/*
 421        Transmit Line6 control parameter.
 422*/
 423int line6_transmit_parameter(struct usb_line6 *line6, int param, u8 value)
 424{
 425        int retval;
 426        unsigned char *buffer;
 427        int partial;
 428
 429        buffer = kmalloc(3, GFP_KERNEL);
 430        if (!buffer)
 431                return -ENOMEM;
 432
 433        buffer[0] = LINE6_PARAM_CHANGE | LINE6_CHANNEL_HOST;
 434        buffer[1] = param;
 435        buffer[2] = value;
 436
 437        retval = usb_interrupt_msg(line6->usbdev,
 438                                   usb_sndintpipe(line6->usbdev,
 439                                                  line6->ep_control_write),
 440                                   buffer, 3, &partial, LINE6_TIMEOUT * HZ);
 441
 442        if (retval)
 443                dev_err(line6->ifcdev, "usb_interrupt_msg failed (%d)\n",
 444                        retval);
 445
 446        kfree(buffer);
 447        return retval;
 448}
 449
 450/*
 451        Read data from device.
 452*/
 453int line6_read_data(struct usb_line6 *line6, int address, void *data,
 454                    size_t datalen)
 455{
 456        struct usb_device *usbdev = line6->usbdev;
 457        int ret;
 458        unsigned char len;
 459
 460        /* query the serial number: */
 461        ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
 462                              USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
 463                              (datalen << 8) | 0x21, address,
 464                              NULL, 0, LINE6_TIMEOUT * HZ);
 465
 466        if (ret < 0) {
 467                dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
 468                return ret;
 469        }
 470
 471        /* Wait for data length. We'll get 0xff until length arrives. */
 472        do {
 473                ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
 474                                      USB_TYPE_VENDOR | USB_RECIP_DEVICE |
 475                                      USB_DIR_IN,
 476                                      0x0012, 0x0000, &len, 1,
 477                                      LINE6_TIMEOUT * HZ);
 478                if (ret < 0) {
 479                        dev_err(line6->ifcdev,
 480                                "receive length failed (error %d)\n", ret);
 481                        return ret;
 482                }
 483        } while (len == 0xff);
 484
 485        if (len != datalen) {
 486                /* should be equal or something went wrong */
 487                dev_err(line6->ifcdev,
 488                        "length mismatch (expected %d, got %d)\n",
 489                        (int)datalen, (int)len);
 490                return -EINVAL;
 491        }
 492
 493        /* receive the result: */
 494        ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
 495                              USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
 496                              0x0013, 0x0000, data, datalen,
 497                              LINE6_TIMEOUT * HZ);
 498
 499        if (ret < 0) {
 500                dev_err(line6->ifcdev, "read failed (error %d)\n", ret);
 501                return ret;
 502        }
 503
 504        return 0;
 505}
 506
 507/*
 508        Write data to device.
 509*/
 510int line6_write_data(struct usb_line6 *line6, int address, void *data,
 511                     size_t datalen)
 512{
 513        struct usb_device *usbdev = line6->usbdev;
 514        int ret;
 515        unsigned char status;
 516
 517        ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
 518                              USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
 519                              0x0022, address, data, datalen,
 520                              LINE6_TIMEOUT * HZ);
 521
 522        if (ret < 0) {
 523                dev_err(line6->ifcdev,
 524                        "write request failed (error %d)\n", ret);
 525                return ret;
 526        }
 527
 528        do {
 529                ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
 530                                      0x67,
 531                                      USB_TYPE_VENDOR | USB_RECIP_DEVICE |
 532                                      USB_DIR_IN,
 533                                      0x0012, 0x0000,
 534                                      &status, 1, LINE6_TIMEOUT * HZ);
 535
 536                if (ret < 0) {
 537                        dev_err(line6->ifcdev,
 538                                "receiving status failed (error %d)\n", ret);
 539                        return ret;
 540                }
 541        } while (status == 0xff);
 542
 543        if (status != 0) {
 544                dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
 545                return -EINVAL;
 546        }
 547
 548        return 0;
 549}
 550
 551/*
 552        Read Line6 device serial number.
 553        (POD, TonePort, GuitarPort)
 554*/
 555int line6_read_serial_number(struct usb_line6 *line6, int *serial_number)
 556{
 557        return line6_read_data(line6, 0x80d0, serial_number,
 558                               sizeof(*serial_number));
 559}
 560
 561/*
 562        No operation (i.e., unsupported).
 563*/
 564ssize_t line6_nop_read(struct device *dev, struct device_attribute *attr,
 565                       char *buf)
 566{
 567        return 0;
 568}
 569
 570/*
 571        Generic destructor.
 572*/
 573static void line6_destruct(struct usb_interface *interface)
 574{
 575        struct usb_line6 *line6;
 576
 577        if (interface == NULL)
 578                return;
 579        line6 = usb_get_intfdata(interface);
 580        if (line6 == NULL)
 581                return;
 582
 583        /* free buffer memory first: */
 584        kfree(line6->buffer_message);
 585        kfree(line6->buffer_listen);
 586
 587        /* then free URBs: */
 588        usb_free_urb(line6->urb_listen);
 589
 590        /* make sure the device isn't destructed twice: */
 591        usb_set_intfdata(interface, NULL);
 592
 593        /* free interface data: */
 594        kfree(line6);
 595}
 596
 597/*
 598        Probe USB device.
 599*/
 600static int line6_probe(struct usb_interface *interface,
 601                       const struct usb_device_id *id)
 602{
 603        int devtype;
 604        struct usb_device *usbdev;
 605        struct usb_line6 *line6;
 606        const struct line6_properties *properties;
 607        int interface_number, alternate = 0;
 608        int product;
 609        int size = 0;
 610        int ep_read = 0, ep_write = 0;
 611        int ret;
 612
 613        if (interface == NULL)
 614                return -ENODEV;
 615        usbdev = interface_to_usbdev(interface);
 616        if (usbdev == NULL)
 617                return -ENODEV;
 618
 619        /* we don't handle multiple configurations */
 620        if (usbdev->descriptor.bNumConfigurations != 1) {
 621                ret = -ENODEV;
 622                goto err_put;
 623        }
 624
 625        /* check vendor and product id */
 626        for (devtype = ARRAY_SIZE(line6_id_table) - 1; devtype--;) {
 627                u16 idVendor = le16_to_cpu(usbdev->descriptor.idVendor);
 628                u16 idProduct = le16_to_cpu(usbdev->descriptor.idProduct);
 629
 630                if (idVendor == line6_id_table[devtype].idVendor &&
 631                    idProduct == line6_id_table[devtype].idProduct)
 632                        break;
 633        }
 634
 635        if (devtype < 0) {
 636                ret = -ENODEV;
 637                goto err_put;
 638        }
 639
 640        /* initialize device info: */
 641        properties = &line6_properties_table[devtype];
 642        dev_info(&interface->dev, "Line6 %s found\n", properties->name);
 643        product = le16_to_cpu(usbdev->descriptor.idProduct);
 644
 645        /* query interface number */
 646        interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
 647
 648        switch (product) {
 649        case LINE6_DEVID_BASSPODXTLIVE:
 650        case LINE6_DEVID_PODXTLIVE:
 651        case LINE6_DEVID_VARIAX:
 652                alternate = 1;
 653                break;
 654
 655        case LINE6_DEVID_POCKETPOD:
 656                switch (interface_number) {
 657                case 0:
 658                        return 0;       /* this interface has no endpoints */
 659                case 1:
 660                        alternate = 0;
 661                        break;
 662                default:
 663                        MISSING_CASE;
 664                }
 665                break;
 666
 667        case LINE6_DEVID_PODHD500:
 668        case LINE6_DEVID_PODX3:
 669        case LINE6_DEVID_PODX3LIVE:
 670                switch (interface_number) {
 671                case 0:
 672                        alternate = 1;
 673                        break;
 674                case 1:
 675                        alternate = 0;
 676                        break;
 677                default:
 678                        MISSING_CASE;
 679                }
 680                break;
 681
 682        case LINE6_DEVID_BASSPODXT:
 683        case LINE6_DEVID_BASSPODXTPRO:
 684        case LINE6_DEVID_PODXT:
 685        case LINE6_DEVID_PODXTPRO:
 686        case LINE6_DEVID_PODHD300:
 687                alternate = 5;
 688                break;
 689
 690        case LINE6_DEVID_GUITARPORT:
 691        case LINE6_DEVID_PODSTUDIO_GX:
 692        case LINE6_DEVID_PODSTUDIO_UX1:
 693        case LINE6_DEVID_TONEPORT_GX:
 694        case LINE6_DEVID_TONEPORT_UX1:
 695                alternate = 2;  /* 1..4 seem to be ok */
 696                break;
 697
 698        case LINE6_DEVID_TONEPORT_UX2:
 699        case LINE6_DEVID_PODSTUDIO_UX2:
 700                switch (interface_number) {
 701                case 0:
 702                        /* defaults to 44.1kHz, 16-bit */
 703                        alternate = 2;
 704                        break;
 705                case 1:
 706                        /* don't know yet what this is ...
 707                           alternate = 1;
 708                           break;
 709                         */
 710                        return -ENODEV;
 711                default:
 712                        MISSING_CASE;
 713                }
 714                break;
 715
 716        default:
 717                MISSING_CASE;
 718                ret = -ENODEV;
 719                goto err_put;
 720        }
 721
 722        ret = usb_set_interface(usbdev, interface_number, alternate);
 723        if (ret < 0) {
 724                dev_err(&interface->dev, "set_interface failed\n");
 725                goto err_put;
 726        }
 727
 728        /* initialize device data based on product id: */
 729        switch (product) {
 730        case LINE6_DEVID_BASSPODXT:
 731        case LINE6_DEVID_BASSPODXTLIVE:
 732        case LINE6_DEVID_BASSPODXTPRO:
 733        case LINE6_DEVID_PODXT:
 734        case LINE6_DEVID_PODXTPRO:
 735                size = sizeof(struct usb_line6_pod);
 736                ep_read = 0x84;
 737                ep_write = 0x03;
 738                break;
 739
 740        case LINE6_DEVID_PODHD300:
 741                size = sizeof(struct usb_line6_podhd);
 742                ep_read = 0x84;
 743                ep_write = 0x03;
 744                break;
 745
 746        case LINE6_DEVID_PODHD500:
 747                size = sizeof(struct usb_line6_podhd);
 748                ep_read = 0x81;
 749                ep_write = 0x01;
 750                break;
 751
 752        case LINE6_DEVID_POCKETPOD:
 753                size = sizeof(struct usb_line6_pod);
 754                ep_read = 0x82;
 755                ep_write = 0x02;
 756                break;
 757
 758        case LINE6_DEVID_PODX3:
 759        case LINE6_DEVID_PODX3LIVE:
 760                /* currently unused! */
 761                size = sizeof(struct usb_line6_pod);
 762                ep_read = 0x81;
 763                ep_write = 0x01;
 764                break;
 765
 766        case LINE6_DEVID_PODSTUDIO_GX:
 767        case LINE6_DEVID_PODSTUDIO_UX1:
 768        case LINE6_DEVID_PODSTUDIO_UX2:
 769        case LINE6_DEVID_TONEPORT_GX:
 770        case LINE6_DEVID_TONEPORT_UX1:
 771        case LINE6_DEVID_TONEPORT_UX2:
 772        case LINE6_DEVID_GUITARPORT:
 773                size = sizeof(struct usb_line6_toneport);
 774                /* these don't have a control channel */
 775                break;
 776
 777        case LINE6_DEVID_PODXTLIVE:
 778                switch (interface_number) {
 779                case PODXTLIVE_INTERFACE_POD:
 780                        size = sizeof(struct usb_line6_pod);
 781                        ep_read = 0x84;
 782                        ep_write = 0x03;
 783                        break;
 784
 785                case PODXTLIVE_INTERFACE_VARIAX:
 786                        size = sizeof(struct usb_line6_variax);
 787                        ep_read = 0x86;
 788                        ep_write = 0x05;
 789                        break;
 790
 791                default:
 792                        ret = -ENODEV;
 793                        goto err_put;
 794                }
 795                break;
 796
 797        case LINE6_DEVID_VARIAX:
 798                size = sizeof(struct usb_line6_variax);
 799                ep_read = 0x82;
 800                ep_write = 0x01;
 801                break;
 802
 803        default:
 804                MISSING_CASE;
 805                ret = -ENODEV;
 806                goto err_put;
 807        }
 808
 809        if (size == 0) {
 810                dev_err(&interface->dev,
 811                        "driver bug: interface data size not set\n");
 812                ret = -ENODEV;
 813                goto err_put;
 814        }
 815
 816        line6 = kzalloc(size, GFP_KERNEL);
 817        if (line6 == NULL) {
 818                ret = -ENODEV;
 819                goto err_put;
 820        }
 821
 822        /* store basic data: */
 823        line6->interface_number = interface_number;
 824        line6->properties = properties;
 825        line6->usbdev = usbdev;
 826        line6->ifcdev = &interface->dev;
 827        line6->ep_control_read = ep_read;
 828        line6->ep_control_write = ep_write;
 829        line6->product = product;
 830
 831        /* get data from endpoint descriptor (see usb_maxpacket): */
 832        {
 833                struct usb_host_endpoint *ep;
 834                unsigned epnum =
 835                    usb_pipeendpoint(usb_rcvintpipe(usbdev, ep_read));
 836                ep = usbdev->ep_in[epnum];
 837
 838                if (ep != NULL) {
 839                        line6->interval = ep->desc.bInterval;
 840                        line6->max_packet_size =
 841                            le16_to_cpu(ep->desc.wMaxPacketSize);
 842                } else {
 843                        line6->interval = LINE6_FALLBACK_INTERVAL;
 844                        line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
 845                        dev_err(line6->ifcdev,
 846                                "endpoint not available, using fallback values");
 847                }
 848        }
 849
 850        usb_set_intfdata(interface, line6);
 851
 852        if (properties->capabilities & LINE6_BIT_CONTROL) {
 853                /* initialize USB buffers: */
 854                line6->buffer_listen =
 855                    kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
 856                if (line6->buffer_listen == NULL) {
 857                        ret = -ENOMEM;
 858                        goto err_destruct;
 859                }
 860
 861                line6->buffer_message =
 862                    kmalloc(LINE6_MESSAGE_MAXLEN, GFP_KERNEL);
 863                if (line6->buffer_message == NULL) {
 864                        ret = -ENOMEM;
 865                        goto err_destruct;
 866                }
 867
 868                line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
 869
 870                if (line6->urb_listen == NULL) {
 871                        dev_err(&interface->dev, "Out of memory\n");
 872                        line6_destruct(interface);
 873                        ret = -ENOMEM;
 874                        goto err_destruct;
 875                }
 876
 877                ret = line6_start_listen(line6);
 878                if (ret < 0) {
 879                        dev_err(&interface->dev, "%s: usb_submit_urb failed\n",
 880                                __func__);
 881                        goto err_destruct;
 882                }
 883        }
 884
 885        /* initialize device data based on product id: */
 886        switch (product) {
 887        case LINE6_DEVID_BASSPODXT:
 888        case LINE6_DEVID_BASSPODXTLIVE:
 889        case LINE6_DEVID_BASSPODXTPRO:
 890        case LINE6_DEVID_POCKETPOD:
 891        case LINE6_DEVID_PODX3:
 892        case LINE6_DEVID_PODX3LIVE:
 893        case LINE6_DEVID_PODXT:
 894        case LINE6_DEVID_PODXTPRO:
 895                ret = line6_pod_init(interface, (struct usb_line6_pod *)line6);
 896                break;
 897
 898        case LINE6_DEVID_PODHD300:
 899        case LINE6_DEVID_PODHD500:
 900                ret = line6_podhd_init(interface,
 901                                       (struct usb_line6_podhd *)line6);
 902                break;
 903
 904        case LINE6_DEVID_PODXTLIVE:
 905                switch (interface_number) {
 906                case PODXTLIVE_INTERFACE_POD:
 907                        ret =
 908                            line6_pod_init(interface,
 909                                           (struct usb_line6_pod *)line6);
 910                        break;
 911
 912                case PODXTLIVE_INTERFACE_VARIAX:
 913                        ret =
 914                            line6_variax_init(interface,
 915                                              (struct usb_line6_variax *)line6);
 916                        break;
 917
 918                default:
 919                        dev_err(&interface->dev,
 920                                "PODxt Live interface %d not supported\n",
 921                                interface_number);
 922                        ret = -ENODEV;
 923                }
 924
 925                break;
 926
 927        case LINE6_DEVID_VARIAX:
 928                ret =
 929                    line6_variax_init(interface,
 930                                      (struct usb_line6_variax *)line6);
 931                break;
 932
 933        case LINE6_DEVID_PODSTUDIO_GX:
 934        case LINE6_DEVID_PODSTUDIO_UX1:
 935        case LINE6_DEVID_PODSTUDIO_UX2:
 936        case LINE6_DEVID_TONEPORT_GX:
 937        case LINE6_DEVID_TONEPORT_UX1:
 938        case LINE6_DEVID_TONEPORT_UX2:
 939        case LINE6_DEVID_GUITARPORT:
 940                ret =
 941                    line6_toneport_init(interface,
 942                                        (struct usb_line6_toneport *)line6);
 943                break;
 944
 945        default:
 946                MISSING_CASE;
 947                ret = -ENODEV;
 948        }
 949
 950        if (ret < 0)
 951                goto err_destruct;
 952
 953        ret = sysfs_create_link(&interface->dev.kobj, &usbdev->dev.kobj,
 954                                "usb_device");
 955        if (ret < 0)
 956                goto err_destruct;
 957
 958        /* creation of additional special files should go here */
 959
 960        dev_info(&interface->dev, "Line6 %s now attached\n",
 961                 line6->properties->name);
 962
 963        switch (product) {
 964        case LINE6_DEVID_PODX3:
 965        case LINE6_DEVID_PODX3LIVE:
 966                dev_info(&interface->dev,
 967                         "NOTE: the Line6 %s is detected, but not yet supported\n",
 968                         line6->properties->name);
 969        }
 970
 971        /* increment reference counters: */
 972        usb_get_intf(interface);
 973        usb_get_dev(usbdev);
 974
 975        return 0;
 976
 977err_destruct:
 978        line6_destruct(interface);
 979err_put:
 980        return ret;
 981}
 982
 983/*
 984        Line6 device disconnected.
 985*/
 986static void line6_disconnect(struct usb_interface *interface)
 987{
 988        struct usb_line6 *line6;
 989        struct usb_device *usbdev;
 990        int interface_number;
 991
 992        if (interface == NULL)
 993                return;
 994        usbdev = interface_to_usbdev(interface);
 995        if (usbdev == NULL)
 996                return;
 997
 998        /* removal of additional special files should go here */
 999
1000        sysfs_remove_link(&interface->dev.kobj, "usb_device");
1001
1002        interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
1003        line6 = usb_get_intfdata(interface);
1004
1005        if (line6 != NULL) {
1006                if (line6->urb_listen != NULL)
1007                        line6_stop_listen(line6);
1008
1009                if (usbdev != line6->usbdev)
1010                        dev_err(line6->ifcdev,
1011                                "driver bug: inconsistent usb device\n");
1012
1013                switch (line6->usbdev->descriptor.idProduct) {
1014                case LINE6_DEVID_BASSPODXT:
1015                case LINE6_DEVID_BASSPODXTLIVE:
1016                case LINE6_DEVID_BASSPODXTPRO:
1017                case LINE6_DEVID_POCKETPOD:
1018                case LINE6_DEVID_PODX3:
1019                case LINE6_DEVID_PODX3LIVE:
1020                case LINE6_DEVID_PODXT:
1021                case LINE6_DEVID_PODXTPRO:
1022                        line6_pod_disconnect(interface);
1023                        break;
1024
1025                case LINE6_DEVID_PODHD300:
1026                case LINE6_DEVID_PODHD500:
1027                        line6_podhd_disconnect(interface);
1028                        break;
1029
1030                case LINE6_DEVID_PODXTLIVE:
1031                        switch (interface_number) {
1032                        case PODXTLIVE_INTERFACE_POD:
1033                                line6_pod_disconnect(interface);
1034                                break;
1035
1036                        case PODXTLIVE_INTERFACE_VARIAX:
1037                                line6_variax_disconnect(interface);
1038                                break;
1039                        }
1040
1041                        break;
1042
1043                case LINE6_DEVID_VARIAX:
1044                        line6_variax_disconnect(interface);
1045                        break;
1046
1047                case LINE6_DEVID_PODSTUDIO_GX:
1048                case LINE6_DEVID_PODSTUDIO_UX1:
1049                case LINE6_DEVID_PODSTUDIO_UX2:
1050                case LINE6_DEVID_TONEPORT_GX:
1051                case LINE6_DEVID_TONEPORT_UX1:
1052                case LINE6_DEVID_TONEPORT_UX2:
1053                case LINE6_DEVID_GUITARPORT:
1054                        line6_toneport_disconnect(interface);
1055                        break;
1056
1057                default:
1058                        MISSING_CASE;
1059                }
1060
1061                dev_info(&interface->dev, "Line6 %s now disconnected\n",
1062                         line6->properties->name);
1063        }
1064
1065        line6_destruct(interface);
1066
1067        /* decrement reference counters: */
1068        usb_put_intf(interface);
1069        usb_put_dev(usbdev);
1070}
1071
1072#ifdef CONFIG_PM
1073
1074/*
1075        Suspend Line6 device.
1076*/
1077static int line6_suspend(struct usb_interface *interface, pm_message_t message)
1078{
1079        struct usb_line6 *line6 = usb_get_intfdata(interface);
1080        struct snd_line6_pcm *line6pcm = line6->line6pcm;
1081
1082        snd_power_change_state(line6->card, SNDRV_CTL_POWER_D3hot);
1083
1084        if (line6->properties->capabilities & LINE6_BIT_CONTROL)
1085                line6_stop_listen(line6);
1086
1087        if (line6pcm != NULL) {
1088                snd_pcm_suspend_all(line6pcm->pcm);
1089                line6_pcm_disconnect(line6pcm);
1090                line6pcm->flags = 0;
1091        }
1092
1093        return 0;
1094}
1095
1096/*
1097        Resume Line6 device.
1098*/
1099static int line6_resume(struct usb_interface *interface)
1100{
1101        struct usb_line6 *line6 = usb_get_intfdata(interface);
1102
1103        if (line6->properties->capabilities & LINE6_BIT_CONTROL)
1104                line6_start_listen(line6);
1105
1106        snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0);
1107        return 0;
1108}
1109
1110/*
1111        Resume Line6 device after reset.
1112*/
1113static int line6_reset_resume(struct usb_interface *interface)
1114{
1115        struct usb_line6 *line6 = usb_get_intfdata(interface);
1116
1117        switch (line6->usbdev->descriptor.idProduct) {
1118        case LINE6_DEVID_PODSTUDIO_GX:
1119        case LINE6_DEVID_PODSTUDIO_UX1:
1120        case LINE6_DEVID_PODSTUDIO_UX2:
1121        case LINE6_DEVID_TONEPORT_GX:
1122        case LINE6_DEVID_TONEPORT_UX1:
1123        case LINE6_DEVID_TONEPORT_UX2:
1124        case LINE6_DEVID_GUITARPORT:
1125                line6_toneport_reset_resume((struct usb_line6_toneport *)line6);
1126        }
1127
1128        return line6_resume(interface);
1129}
1130
1131#endif /* CONFIG_PM */
1132
1133static struct usb_driver line6_driver = {
1134        .name = DRIVER_NAME,
1135        .probe = line6_probe,
1136        .disconnect = line6_disconnect,
1137#ifdef CONFIG_PM
1138        .suspend = line6_suspend,
1139        .resume = line6_resume,
1140        .reset_resume = line6_reset_resume,
1141#endif
1142        .id_table = line6_id_table,
1143};
1144
1145module_usb_driver(line6_driver);
1146
1147MODULE_AUTHOR(DRIVER_AUTHOR);
1148MODULE_DESCRIPTION(DRIVER_DESC);
1149MODULE_LICENSE("GPL");
1150MODULE_VERSION(DRIVER_VERSION);
1151