linux/drivers/mmc/host/vub300.c
<<
>>
Prefs
   1/*
   2 * Remote VUB300 SDIO/SDmem Host Controller Driver
   3 *
   4 * Copyright (C) 2010 Elan Digital Systems Limited
   5 *
   6 * based on USB Skeleton driver - 2.2
   7 *
   8 * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
   9 *
  10 * This program is free software; you can redistribute it and/or
  11 * modify it under the terms of the GNU General Public License as
  12 * published by the Free Software Foundation, version 2
  13 *
  14 * VUB300: is a USB 2.0 client device with a single SDIO/SDmem/MMC slot
  15 *         Any SDIO/SDmem/MMC device plugged into the VUB300 will appear,
  16 *         by virtue of this driver, to have been plugged into a local
  17 *         SDIO host controller, similar to, say, a PCI Ricoh controller
  18 *         This is because this kernel device driver is both a USB 2.0
  19 *         client device driver AND an MMC host controller driver. Thus
  20 *         if there is an existing driver for the inserted SDIO/SDmem/MMC
  21 *         device then that driver will be used by the kernel to manage
  22 *         the device in exactly the same fashion as if it had been
  23 *         directly plugged into, say, a local pci bus Ricoh controller
  24 *
  25 * RANT: this driver was written using a display 128x48 - converting it
  26 *       to a line width of 80 makes it very difficult to support. In
  27 *       particular functions have been broken down into sub functions
  28 *       and the original meaningful names have been shortened into
  29 *       cryptic ones.
  30 *       The problem is that executing a fragment of code subject to
  31 *       two conditions means an indentation of 24, thus leaving only
  32 *       56 characters for a C statement. And that is quite ridiculous!
  33 *
  34 * Data types: data passed to/from the VUB300 is fixed to a number of
  35 *             bits and driver data fields reflect that limit by using
  36 *             u8, u16, u32
  37 */
  38#include <linux/kernel.h>
  39#include <linux/errno.h>
  40#include <linux/init.h>
  41#include <linux/slab.h>
  42#include <linux/module.h>
  43#include <linux/kref.h>
  44#include <linux/uaccess.h>
  45#include <linux/usb.h>
  46#include <linux/mutex.h>
  47#include <linux/mmc/host.h>
  48#include <linux/mmc/card.h>
  49#include <linux/mmc/sdio_func.h>
  50#include <linux/mmc/sdio_ids.h>
  51#include <linux/workqueue.h>
  52#include <linux/ctype.h>
  53#include <linux/firmware.h>
  54#include <linux/scatterlist.h>
  55
  56struct host_controller_info {
  57        u8 info_size;
  58        u16 firmware_version;
  59        u8 number_of_ports;
  60} __packed;
  61
  62#define FIRMWARE_BLOCK_BOUNDARY 1024
  63struct sd_command_header {
  64        u8 header_size;
  65        u8 header_type;
  66        u8 port_number;
  67        u8 command_type; /* Bit7 - Rd/Wr */
  68        u8 command_index;
  69        u8 transfer_size[4]; /* ReadSize + ReadSize */
  70        u8 response_type;
  71        u8 arguments[4];
  72        u8 block_count[2];
  73        u8 block_size[2];
  74        u8 block_boundary[2];
  75        u8 reserved[44]; /* to pad out to 64 bytes */
  76} __packed;
  77
  78struct sd_irqpoll_header {
  79        u8 header_size;
  80        u8 header_type;
  81        u8 port_number;
  82        u8 command_type; /* Bit7 - Rd/Wr */
  83        u8 padding[16]; /* don't ask why !! */
  84        u8 poll_timeout_msb;
  85        u8 poll_timeout_lsb;
  86        u8 reserved[42]; /* to pad out to 64 bytes */
  87} __packed;
  88
  89struct sd_common_header {
  90        u8 header_size;
  91        u8 header_type;
  92        u8 port_number;
  93} __packed;
  94
  95struct sd_response_header {
  96        u8 header_size;
  97        u8 header_type;
  98        u8 port_number;
  99        u8 command_type;
 100        u8 command_index;
 101        u8 command_response[0];
 102} __packed;
 103
 104struct sd_status_header {
 105        u8 header_size;
 106        u8 header_type;
 107        u8 port_number;
 108        u16 port_flags;
 109        u32 sdio_clock;
 110        u16 host_header_size;
 111        u16 func_header_size;
 112        u16 ctrl_header_size;
 113} __packed;
 114
 115struct sd_error_header {
 116        u8 header_size;
 117        u8 header_type;
 118        u8 port_number;
 119        u8 error_code;
 120} __packed;
 121
 122struct sd_interrupt_header {
 123        u8 header_size;
 124        u8 header_type;
 125        u8 port_number;
 126} __packed;
 127
 128struct offload_registers_access {
 129        u8 command_byte[4];
 130        u8 Respond_Byte[4];
 131} __packed;
 132
 133#define INTERRUPT_REGISTER_ACCESSES 15
 134struct sd_offloaded_interrupt {
 135        u8 header_size;
 136        u8 header_type;
 137        u8 port_number;
 138        struct offload_registers_access reg[INTERRUPT_REGISTER_ACCESSES];
 139} __packed;
 140
 141struct sd_register_header {
 142        u8 header_size;
 143        u8 header_type;
 144        u8 port_number;
 145        u8 command_type;
 146        u8 command_index;
 147        u8 command_response[6];
 148} __packed;
 149
 150#define PIGGYBACK_REGISTER_ACCESSES 14
 151struct sd_offloaded_piggyback {
 152        struct sd_register_header sdio;
 153        struct offload_registers_access reg[PIGGYBACK_REGISTER_ACCESSES];
 154} __packed;
 155
 156union sd_response {
 157        struct sd_common_header common;
 158        struct sd_status_header status;
 159        struct sd_error_header error;
 160        struct sd_interrupt_header interrupt;
 161        struct sd_response_header response;
 162        struct sd_offloaded_interrupt irq;
 163        struct sd_offloaded_piggyback pig;
 164} __packed;
 165
 166union sd_command {
 167        struct sd_command_header head;
 168        struct sd_irqpoll_header poll;
 169} __packed;
 170
 171enum SD_RESPONSE_TYPE {
 172        SDRT_UNSPECIFIED = 0,
 173        SDRT_NONE,
 174        SDRT_1,
 175        SDRT_1B,
 176        SDRT_2,
 177        SDRT_3,
 178        SDRT_4,
 179        SDRT_5,
 180        SDRT_5B,
 181        SDRT_6,
 182        SDRT_7,
 183};
 184
 185#define RESPONSE_INTERRUPT                      0x01
 186#define RESPONSE_ERROR                          0x02
 187#define RESPONSE_STATUS                         0x03
 188#define RESPONSE_IRQ_DISABLED                   0x05
 189#define RESPONSE_IRQ_ENABLED                    0x06
 190#define RESPONSE_PIGGYBACKED                    0x07
 191#define RESPONSE_NO_INTERRUPT                   0x08
 192#define RESPONSE_PIG_DISABLED                   0x09
 193#define RESPONSE_PIG_ENABLED                    0x0A
 194#define SD_ERROR_1BIT_TIMEOUT                   0x01
 195#define SD_ERROR_4BIT_TIMEOUT                   0x02
 196#define SD_ERROR_1BIT_CRC_WRONG                 0x03
 197#define SD_ERROR_4BIT_CRC_WRONG                 0x04
 198#define SD_ERROR_1BIT_CRC_ERROR                 0x05
 199#define SD_ERROR_4BIT_CRC_ERROR                 0x06
 200#define SD_ERROR_NO_CMD_ENDBIT                  0x07
 201#define SD_ERROR_NO_1BIT_DATEND                 0x08
 202#define SD_ERROR_NO_4BIT_DATEND                 0x09
 203#define SD_ERROR_1BIT_UNEXPECTED_TIMEOUT        0x0A
 204#define SD_ERROR_4BIT_UNEXPECTED_TIMEOUT        0x0B
 205#define SD_ERROR_ILLEGAL_COMMAND                0x0C
 206#define SD_ERROR_NO_DEVICE                      0x0D
 207#define SD_ERROR_TRANSFER_LENGTH                0x0E
 208#define SD_ERROR_1BIT_DATA_TIMEOUT              0x0F
 209#define SD_ERROR_4BIT_DATA_TIMEOUT              0x10
 210#define SD_ERROR_ILLEGAL_STATE                  0x11
 211#define SD_ERROR_UNKNOWN_ERROR                  0x12
 212#define SD_ERROR_RESERVED_ERROR                 0x13
 213#define SD_ERROR_INVALID_FUNCTION               0x14
 214#define SD_ERROR_OUT_OF_RANGE                   0x15
 215#define SD_ERROR_STAT_CMD                       0x16
 216#define SD_ERROR_STAT_DATA                      0x17
 217#define SD_ERROR_STAT_CMD_TIMEOUT               0x18
 218#define SD_ERROR_SDCRDY_STUCK                   0x19
 219#define SD_ERROR_UNHANDLED                      0x1A
 220#define SD_ERROR_OVERRUN                        0x1B
 221#define SD_ERROR_PIO_TIMEOUT                    0x1C
 222
 223#define FUN(c) (0x000007 & (c->arg>>28))
 224#define REG(c) (0x01FFFF & (c->arg>>9))
 225
 226static bool limit_speed_to_24_MHz;
 227module_param(limit_speed_to_24_MHz, bool, 0644);
 228MODULE_PARM_DESC(limit_speed_to_24_MHz, "Limit Max SDIO Clock Speed to 24 MHz");
 229
 230static bool pad_input_to_usb_pkt;
 231module_param(pad_input_to_usb_pkt, bool, 0644);
 232MODULE_PARM_DESC(pad_input_to_usb_pkt,
 233                 "Pad USB data input transfers to whole USB Packet");
 234
 235static bool disable_offload_processing;
 236module_param(disable_offload_processing, bool, 0644);
 237MODULE_PARM_DESC(disable_offload_processing, "Disable Offload Processing");
 238
 239static bool force_1_bit_data_xfers;
 240module_param(force_1_bit_data_xfers, bool, 0644);
 241MODULE_PARM_DESC(force_1_bit_data_xfers,
 242                 "Force SDIO Data Transfers to 1-bit Mode");
 243
 244static bool force_polling_for_irqs;
 245module_param(force_polling_for_irqs, bool, 0644);
 246MODULE_PARM_DESC(force_polling_for_irqs, "Force Polling for SDIO interrupts");
 247
 248static int firmware_irqpoll_timeout = 1024;
 249module_param(firmware_irqpoll_timeout, int, 0644);
 250MODULE_PARM_DESC(firmware_irqpoll_timeout, "VUB300 firmware irqpoll timeout");
 251
 252static int force_max_req_size = 128;
 253module_param(force_max_req_size, int, 0644);
 254MODULE_PARM_DESC(force_max_req_size, "set max request size in kBytes");
 255
 256#ifdef SMSC_DEVELOPMENT_BOARD
 257static int firmware_rom_wait_states = 0x04;
 258#else
 259static int firmware_rom_wait_states = 0x1C;
 260#endif
 261
 262module_param(firmware_rom_wait_states, int, 0644);
 263MODULE_PARM_DESC(firmware_rom_wait_states,
 264                 "ROM wait states byte=RRRIIEEE (Reserved Internal External)");
 265
 266#define ELAN_VENDOR_ID          0x2201
 267#define VUB300_VENDOR_ID        0x0424
 268#define VUB300_PRODUCT_ID       0x012C
 269static struct usb_device_id vub300_table[] = {
 270        {USB_DEVICE(ELAN_VENDOR_ID, VUB300_PRODUCT_ID)},
 271        {USB_DEVICE(VUB300_VENDOR_ID, VUB300_PRODUCT_ID)},
 272        {} /* Terminating entry */
 273};
 274MODULE_DEVICE_TABLE(usb, vub300_table);
 275
 276static struct workqueue_struct *cmndworkqueue;
 277static struct workqueue_struct *pollworkqueue;
 278static struct workqueue_struct *deadworkqueue;
 279
 280static inline int interface_to_InterfaceNumber(struct usb_interface *interface)
 281{
 282        if (!interface)
 283                return -1;
 284        if (!interface->cur_altsetting)
 285                return -1;
 286        return interface->cur_altsetting->desc.bInterfaceNumber;
 287}
 288
 289struct sdio_register {
 290        unsigned func_num:3;
 291        unsigned sdio_reg:17;
 292        unsigned activate:1;
 293        unsigned prepared:1;
 294        unsigned regvalue:8;
 295        unsigned response:8;
 296        unsigned sparebit:26;
 297};
 298
 299struct vub300_mmc_host {
 300        struct usb_device *udev;
 301        struct usb_interface *interface;
 302        struct kref kref;
 303        struct mutex cmd_mutex;
 304        struct mutex irq_mutex;
 305        char vub_name[3 + (9 * 8) + 4 + 1]; /* max of 7 sdio fn's */
 306        u8 cmnd_out_ep; /* EndPoint for commands */
 307        u8 cmnd_res_ep; /* EndPoint for responses */
 308        u8 data_out_ep; /* EndPoint for out data */
 309        u8 data_inp_ep; /* EndPoint for inp data */
 310        bool card_powered;
 311        bool card_present;
 312        bool read_only;
 313        bool large_usb_packets;
 314        bool app_spec; /* ApplicationSpecific */
 315        bool irq_enabled; /* by the MMC CORE */
 316        bool irq_disabled; /* in the firmware */
 317        unsigned bus_width:4;
 318        u8 total_offload_count;
 319        u8 dynamic_register_count;
 320        u8 resp_len;
 321        u32 datasize;
 322        int errors;
 323        int usb_transport_fail;
 324        int usb_timed_out;
 325        int irqs_queued;
 326        struct sdio_register sdio_register[16];
 327        struct offload_interrupt_function_register {
 328#define MAXREGBITS 4
 329#define MAXREGS (1<<MAXREGBITS)
 330#define MAXREGMASK (MAXREGS-1)
 331                u8 offload_count;
 332                u32 offload_point;
 333                struct offload_registers_access reg[MAXREGS];
 334        } fn[8];
 335        u16 fbs[8]; /* Function Block Size */
 336        struct mmc_command *cmd;
 337        struct mmc_request *req;
 338        struct mmc_data *data;
 339        struct mmc_host *mmc;
 340        struct urb *urb;
 341        struct urb *command_out_urb;
 342        struct urb *command_res_urb;
 343        struct completion command_complete;
 344        struct completion irqpoll_complete;
 345        union sd_command cmnd;
 346        union sd_response resp;
 347        struct timer_list sg_transfer_timer;
 348        struct usb_sg_request sg_request;
 349        struct timer_list inactivity_timer;
 350        struct work_struct deadwork;
 351        struct work_struct cmndwork;
 352        struct delayed_work pollwork;
 353        struct host_controller_info hc_info;
 354        struct sd_status_header system_port_status;
 355        u8 padded_buffer[64];
 356};
 357
 358#define kref_to_vub300_mmc_host(d) container_of(d, struct vub300_mmc_host, kref)
 359#define SET_TRANSFER_PSEUDOCODE         21
 360#define SET_INTERRUPT_PSEUDOCODE        20
 361#define SET_FAILURE_MODE                18
 362#define SET_ROM_WAIT_STATES             16
 363#define SET_IRQ_ENABLE                  13
 364#define SET_CLOCK_SPEED                 11
 365#define SET_FUNCTION_BLOCK_SIZE         9
 366#define SET_SD_DATA_MODE                6
 367#define SET_SD_POWER                    4
 368#define ENTER_DFU_MODE                  3
 369#define GET_HC_INF0                     1
 370#define GET_SYSTEM_PORT_STATUS          0
 371
 372static void vub300_delete(struct kref *kref)
 373{                               /* kref callback - softirq */
 374        struct vub300_mmc_host *vub300 = kref_to_vub300_mmc_host(kref);
 375        struct mmc_host *mmc = vub300->mmc;
 376        usb_free_urb(vub300->command_out_urb);
 377        vub300->command_out_urb = NULL;
 378        usb_free_urb(vub300->command_res_urb);
 379        vub300->command_res_urb = NULL;
 380        usb_put_dev(vub300->udev);
 381        mmc_free_host(mmc);
 382        /*
 383         * and hence also frees vub300
 384         * which is contained at the end of struct mmc
 385         */
 386}
 387
 388static void vub300_queue_cmnd_work(struct vub300_mmc_host *vub300)
 389{
 390        kref_get(&vub300->kref);
 391        if (queue_work(cmndworkqueue, &vub300->cmndwork)) {
 392                /*
 393                 * then the cmndworkqueue was not previously
 394                 * running and the above get ref is obvious
 395                 * required and will be put when the thread
 396                 * terminates by a specific call
 397                 */
 398        } else {
 399                /*
 400                 * the cmndworkqueue was already running from
 401                 * a previous invocation and thus to keep the
 402                 * kref counts correct we must undo the get
 403                 */
 404                kref_put(&vub300->kref, vub300_delete);
 405        }
 406}
 407
 408static void vub300_queue_poll_work(struct vub300_mmc_host *vub300, int delay)
 409{
 410        kref_get(&vub300->kref);
 411        if (queue_delayed_work(pollworkqueue, &vub300->pollwork, delay)) {
 412                /*
 413                 * then the pollworkqueue was not previously
 414                 * running and the above get ref is obvious
 415                 * required and will be put when the thread
 416                 * terminates by a specific call
 417                 */
 418        } else {
 419                /*
 420                 * the pollworkqueue was already running from
 421                 * a previous invocation and thus to keep the
 422                 * kref counts correct we must undo the get
 423                 */
 424                kref_put(&vub300->kref, vub300_delete);
 425        }
 426}
 427
 428static void vub300_queue_dead_work(struct vub300_mmc_host *vub300)
 429{
 430        kref_get(&vub300->kref);
 431        if (queue_work(deadworkqueue, &vub300->deadwork)) {
 432                /*
 433                 * then the deadworkqueue was not previously
 434                 * running and the above get ref is obvious
 435                 * required and will be put when the thread
 436                 * terminates by a specific call
 437                 */
 438        } else {
 439                /*
 440                 * the deadworkqueue was already running from
 441                 * a previous invocation and thus to keep the
 442                 * kref counts correct we must undo the get
 443                 */
 444                kref_put(&vub300->kref, vub300_delete);
 445        }
 446}
 447
 448static void irqpoll_res_completed(struct urb *urb)
 449{                               /* urb completion handler - hardirq */
 450        struct vub300_mmc_host *vub300 = (struct vub300_mmc_host *)urb->context;
 451        if (urb->status)
 452                vub300->usb_transport_fail = urb->status;
 453        complete(&vub300->irqpoll_complete);
 454}
 455
 456static void irqpoll_out_completed(struct urb *urb)
 457{                               /* urb completion handler - hardirq */
 458        struct vub300_mmc_host *vub300 = (struct vub300_mmc_host *)urb->context;
 459        if (urb->status) {
 460                vub300->usb_transport_fail = urb->status;
 461                complete(&vub300->irqpoll_complete);
 462                return;
 463        } else {
 464                int ret;
 465                unsigned int pipe =
 466                        usb_rcvbulkpipe(vub300->udev, vub300->cmnd_res_ep);
 467                usb_fill_bulk_urb(vub300->command_res_urb, vub300->udev, pipe,
 468                                  &vub300->resp, sizeof(vub300->resp),
 469                                  irqpoll_res_completed, vub300);
 470                vub300->command_res_urb->actual_length = 0;
 471                ret = usb_submit_urb(vub300->command_res_urb, GFP_ATOMIC);
 472                if (ret) {
 473                        vub300->usb_transport_fail = ret;
 474                        complete(&vub300->irqpoll_complete);
 475                }
 476                return;
 477        }
 478}
 479
 480static void send_irqpoll(struct vub300_mmc_host *vub300)
 481{
 482        /* cmd_mutex is held by vub300_pollwork_thread */
 483        int retval;
 484        int timeout = 0xFFFF & (0x0001FFFF - firmware_irqpoll_timeout);
 485        vub300->cmnd.poll.header_size = 22;
 486        vub300->cmnd.poll.header_type = 1;
 487        vub300->cmnd.poll.port_number = 0;
 488        vub300->cmnd.poll.command_type = 2;
 489        vub300->cmnd.poll.poll_timeout_lsb = 0xFF & (unsigned)timeout;
 490        vub300->cmnd.poll.poll_timeout_msb = 0xFF & (unsigned)(timeout >> 8);
 491        usb_fill_bulk_urb(vub300->command_out_urb, vub300->udev,
 492                          usb_sndbulkpipe(vub300->udev, vub300->cmnd_out_ep)
 493                          , &vub300->cmnd, sizeof(vub300->cmnd)
 494                          , irqpoll_out_completed, vub300);
 495        retval = usb_submit_urb(vub300->command_out_urb, GFP_KERNEL);
 496        if (0 > retval) {
 497                vub300->usb_transport_fail = retval;
 498                vub300_queue_poll_work(vub300, 1);
 499                complete(&vub300->irqpoll_complete);
 500                return;
 501        } else {
 502                return;
 503        }
 504}
 505
 506static void new_system_port_status(struct vub300_mmc_host *vub300)
 507{
 508        int old_card_present = vub300->card_present;
 509        int new_card_present =
 510                (0x0001 & vub300->system_port_status.port_flags) ? 1 : 0;
 511        vub300->read_only =
 512                (0x0010 & vub300->system_port_status.port_flags) ? 1 : 0;
 513        if (new_card_present && !old_card_present) {
 514                dev_info(&vub300->udev->dev, "card just inserted\n");
 515                vub300->card_present = 1;
 516                vub300->bus_width = 0;
 517                if (disable_offload_processing)
 518                        strncpy(vub300->vub_name, "EMPTY Processing Disabled",
 519                                sizeof(vub300->vub_name));
 520                else
 521                        vub300->vub_name[0] = 0;
 522                mmc_detect_change(vub300->mmc, 1);
 523        } else if (!new_card_present && old_card_present) {
 524                dev_info(&vub300->udev->dev, "card just ejected\n");
 525                vub300->card_present = 0;
 526                mmc_detect_change(vub300->mmc, 0);
 527        } else {
 528                /* no change */
 529        }
 530}
 531
 532static void __add_offloaded_reg_to_fifo(struct vub300_mmc_host *vub300,
 533                                        struct offload_registers_access
 534                                        *register_access, u8 func)
 535{
 536        u8 r = vub300->fn[func].offload_point + vub300->fn[func].offload_count;
 537        memcpy(&vub300->fn[func].reg[MAXREGMASK & r], register_access,
 538               sizeof(struct offload_registers_access));
 539        vub300->fn[func].offload_count += 1;
 540        vub300->total_offload_count += 1;
 541}
 542
 543static void add_offloaded_reg(struct vub300_mmc_host *vub300,
 544                              struct offload_registers_access *register_access)
 545{
 546        u32 Register = ((0x03 & register_access->command_byte[0]) << 15)
 547                        | ((0xFF & register_access->command_byte[1]) << 7)
 548                        | ((0xFE & register_access->command_byte[2]) >> 1);
 549        u8 func = ((0x70 & register_access->command_byte[0]) >> 4);
 550        u8 regs = vub300->dynamic_register_count;
 551        u8 i = 0;
 552        while (0 < regs-- && 1 == vub300->sdio_register[i].activate) {
 553                if (vub300->sdio_register[i].func_num == func &&
 554                    vub300->sdio_register[i].sdio_reg == Register) {
 555                        if (vub300->sdio_register[i].prepared == 0)
 556                                vub300->sdio_register[i].prepared = 1;
 557                        vub300->sdio_register[i].response =
 558                                register_access->Respond_Byte[2];
 559                        vub300->sdio_register[i].regvalue =
 560                                register_access->Respond_Byte[3];
 561                        return;
 562                } else {
 563                        i += 1;
 564                        continue;
 565                }
 566        }
 567        __add_offloaded_reg_to_fifo(vub300, register_access, func);
 568}
 569
 570static void check_vub300_port_status(struct vub300_mmc_host *vub300)
 571{
 572        /*
 573         * cmd_mutex is held by vub300_pollwork_thread,
 574         * vub300_deadwork_thread or vub300_cmndwork_thread
 575         */
 576        int retval;
 577        retval =
 578                usb_control_msg(vub300->udev, usb_rcvctrlpipe(vub300->udev, 0),
 579                                GET_SYSTEM_PORT_STATUS,
 580                                USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
 581                                0x0000, 0x0000, &vub300->system_port_status,
 582                                sizeof(vub300->system_port_status), HZ);
 583        if (sizeof(vub300->system_port_status) == retval)
 584                new_system_port_status(vub300);
 585}
 586
 587static void __vub300_irqpoll_response(struct vub300_mmc_host *vub300)
 588{
 589        /* cmd_mutex is held by vub300_pollwork_thread */
 590        if (vub300->command_res_urb->actual_length == 0)
 591                return;
 592
 593        switch (vub300->resp.common.header_type) {
 594        case RESPONSE_INTERRUPT:
 595                mutex_lock(&vub300->irq_mutex);
 596                if (vub300->irq_enabled)
 597                        mmc_signal_sdio_irq(vub300->mmc);
 598                else
 599                        vub300->irqs_queued += 1;
 600                vub300->irq_disabled = 1;
 601                mutex_unlock(&vub300->irq_mutex);
 602                break;
 603        case RESPONSE_ERROR:
 604                if (vub300->resp.error.error_code == SD_ERROR_NO_DEVICE)
 605                        check_vub300_port_status(vub300);
 606                break;
 607        case RESPONSE_STATUS:
 608                vub300->system_port_status = vub300->resp.status;
 609                new_system_port_status(vub300);
 610                if (!vub300->card_present)
 611                        vub300_queue_poll_work(vub300, HZ / 5);
 612                break;
 613        case RESPONSE_IRQ_DISABLED:
 614        {
 615                int offloaded_data_length = vub300->resp.common.header_size - 3;
 616                int register_count = offloaded_data_length >> 3;
 617                int ri = 0;
 618                while (register_count--) {
 619                        add_offloaded_reg(vub300, &vub300->resp.irq.reg[ri]);
 620                        ri += 1;
 621                }
 622                mutex_lock(&vub300->irq_mutex);
 623                if (vub300->irq_enabled)
 624                        mmc_signal_sdio_irq(vub300->mmc);
 625                else
 626                        vub300->irqs_queued += 1;
 627                vub300->irq_disabled = 1;
 628                mutex_unlock(&vub300->irq_mutex);
 629                break;
 630        }
 631        case RESPONSE_IRQ_ENABLED:
 632        {
 633                int offloaded_data_length = vub300->resp.common.header_size - 3;
 634                int register_count = offloaded_data_length >> 3;
 635                int ri = 0;
 636                while (register_count--) {
 637                        add_offloaded_reg(vub300, &vub300->resp.irq.reg[ri]);
 638                        ri += 1;
 639                }
 640                mutex_lock(&vub300->irq_mutex);
 641                if (vub300->irq_enabled)
 642                        mmc_signal_sdio_irq(vub300->mmc);
 643                else if (vub300->irqs_queued)
 644                        vub300->irqs_queued += 1;
 645                else
 646                        vub300->irqs_queued += 1;
 647                vub300->irq_disabled = 0;
 648                mutex_unlock(&vub300->irq_mutex);
 649                break;
 650        }
 651        case RESPONSE_NO_INTERRUPT:
 652                vub300_queue_poll_work(vub300, 1);
 653                break;
 654        default:
 655                break;
 656        }
 657}
 658
 659static void __do_poll(struct vub300_mmc_host *vub300)
 660{
 661        /* cmd_mutex is held by vub300_pollwork_thread */
 662        unsigned long commretval;
 663        mod_timer(&vub300->inactivity_timer, jiffies + HZ);
 664        init_completion(&vub300->irqpoll_complete);
 665        send_irqpoll(vub300);
 666        commretval = wait_for_completion_timeout(&vub300->irqpoll_complete,
 667                                                 msecs_to_jiffies(500));
 668        if (vub300->usb_transport_fail) {
 669                /* no need to do anything */
 670        } else if (commretval == 0) {
 671                vub300->usb_timed_out = 1;
 672                usb_kill_urb(vub300->command_out_urb);
 673                usb_kill_urb(vub300->command_res_urb);
 674        } else { /* commretval > 0 */
 675                __vub300_irqpoll_response(vub300);
 676        }
 677}
 678
 679/* this thread runs only when the driver
 680 * is trying to poll the device for an IRQ
 681 */
 682static void vub300_pollwork_thread(struct work_struct *work)
 683{                               /* NOT irq */
 684        struct vub300_mmc_host *vub300 = container_of(work,
 685                              struct vub300_mmc_host, pollwork.work);
 686        if (!vub300->interface) {
 687                kref_put(&vub300->kref, vub300_delete);
 688                return;
 689        }
 690        mutex_lock(&vub300->cmd_mutex);
 691        if (vub300->cmd) {
 692                vub300_queue_poll_work(vub300, 1);
 693        } else if (!vub300->card_present) {
 694                /* no need to do anything */
 695        } else { /* vub300->card_present */
 696                mutex_lock(&vub300->irq_mutex);
 697                if (!vub300->irq_enabled) {
 698                        mutex_unlock(&vub300->irq_mutex);
 699                } else if (vub300->irqs_queued) {
 700                        vub300->irqs_queued -= 1;
 701                        mmc_signal_sdio_irq(vub300->mmc);
 702                        mod_timer(&vub300->inactivity_timer, jiffies + HZ);
 703                        mutex_unlock(&vub300->irq_mutex);
 704                } else { /* NOT vub300->irqs_queued */
 705                        mutex_unlock(&vub300->irq_mutex);
 706                        __do_poll(vub300);
 707                }
 708        }
 709        mutex_unlock(&vub300->cmd_mutex);
 710        kref_put(&vub300->kref, vub300_delete);
 711}
 712
 713static void vub300_deadwork_thread(struct work_struct *work)
 714{                               /* NOT irq */
 715        struct vub300_mmc_host *vub300 =
 716                container_of(work, struct vub300_mmc_host, deadwork);
 717        if (!vub300->interface) {
 718                kref_put(&vub300->kref, vub300_delete);
 719                return;
 720        }
 721        mutex_lock(&vub300->cmd_mutex);
 722        if (vub300->cmd) {
 723                /*
 724                 * a command got in as the inactivity
 725                 * timer expired - so we just let the
 726                 * processing of the command show if
 727                 * the device is dead
 728                 */
 729        } else if (vub300->card_present) {
 730                check_vub300_port_status(vub300);
 731        } else if (vub300->mmc && vub300->mmc->card) {
 732                /*
 733                 * the MMC core must not have responded
 734                 * to the previous indication - lets
 735                 * hope that it eventually does so we
 736                 * will just ignore this for now
 737                 */
 738        } else {
 739                check_vub300_port_status(vub300);
 740        }
 741        mod_timer(&vub300->inactivity_timer, jiffies + HZ);
 742        mutex_unlock(&vub300->cmd_mutex);
 743        kref_put(&vub300->kref, vub300_delete);
 744}
 745
 746static void vub300_inactivity_timer_expired(unsigned long data)
 747{                               /* softirq */
 748        struct vub300_mmc_host *vub300 = (struct vub300_mmc_host *)data;
 749        if (!vub300->interface) {
 750                kref_put(&vub300->kref, vub300_delete);
 751        } else if (vub300->cmd) {
 752                mod_timer(&vub300->inactivity_timer, jiffies + HZ);
 753        } else {
 754                vub300_queue_dead_work(vub300);
 755                mod_timer(&vub300->inactivity_timer, jiffies + HZ);
 756        }
 757}
 758
 759static int vub300_response_error(u8 error_code)
 760{
 761        switch (error_code) {
 762        case SD_ERROR_PIO_TIMEOUT:
 763        case SD_ERROR_1BIT_TIMEOUT:
 764        case SD_ERROR_4BIT_TIMEOUT:
 765                return -ETIMEDOUT;
 766        case SD_ERROR_STAT_DATA:
 767        case SD_ERROR_OVERRUN:
 768        case SD_ERROR_STAT_CMD:
 769        case SD_ERROR_STAT_CMD_TIMEOUT:
 770        case SD_ERROR_SDCRDY_STUCK:
 771        case SD_ERROR_UNHANDLED:
 772        case SD_ERROR_1BIT_CRC_WRONG:
 773        case SD_ERROR_4BIT_CRC_WRONG:
 774        case SD_ERROR_1BIT_CRC_ERROR:
 775        case SD_ERROR_4BIT_CRC_ERROR:
 776        case SD_ERROR_NO_CMD_ENDBIT:
 777        case SD_ERROR_NO_1BIT_DATEND:
 778        case SD_ERROR_NO_4BIT_DATEND:
 779        case SD_ERROR_1BIT_DATA_TIMEOUT:
 780        case SD_ERROR_4BIT_DATA_TIMEOUT:
 781        case SD_ERROR_1BIT_UNEXPECTED_TIMEOUT:
 782        case SD_ERROR_4BIT_UNEXPECTED_TIMEOUT:
 783                return -EILSEQ;
 784        case 33:
 785                return -EILSEQ;
 786        case SD_ERROR_ILLEGAL_COMMAND:
 787                return -EINVAL;
 788        case SD_ERROR_NO_DEVICE:
 789                return -ENOMEDIUM;
 790        default:
 791                return -ENODEV;
 792        }
 793}
 794
 795static void command_res_completed(struct urb *urb)
 796{                               /* urb completion handler - hardirq */
 797        struct vub300_mmc_host *vub300 = (struct vub300_mmc_host *)urb->context;
 798        if (urb->status) {
 799                /* we have to let the initiator handle the error */
 800        } else if (vub300->command_res_urb->actual_length == 0) {
 801                /*
 802                 * we have seen this happen once or twice and
 803                 * we suspect a buggy USB host controller
 804                 */
 805        } else if (!vub300->data) {
 806                /* this means that the command (typically CMD52) succeeded */
 807        } else if (vub300->resp.common.header_type != 0x02) {
 808                /*
 809                 * this is an error response from the VUB300 chip
 810                 * and we let the initiator handle it
 811                 */
 812        } else if (vub300->urb) {
 813                vub300->cmd->error =
 814                        vub300_response_error(vub300->resp.error.error_code);
 815                usb_unlink_urb(vub300->urb);
 816        } else {
 817                vub300->cmd->error =
 818                        vub300_response_error(vub300->resp.error.error_code);
 819                usb_sg_cancel(&vub300->sg_request);
 820        }
 821        complete(&vub300->command_complete);    /* got_response_in */
 822}
 823
 824static void command_out_completed(struct urb *urb)
 825{                               /* urb completion handler - hardirq */
 826        struct vub300_mmc_host *vub300 = (struct vub300_mmc_host *)urb->context;
 827        if (urb->status) {
 828                complete(&vub300->command_complete);
 829        } else {
 830                int ret;
 831                unsigned int pipe =
 832                        usb_rcvbulkpipe(vub300->udev, vub300->cmnd_res_ep);
 833                usb_fill_bulk_urb(vub300->command_res_urb, vub300->udev, pipe,
 834                                  &vub300->resp, sizeof(vub300->resp),
 835                                  command_res_completed, vub300);
 836                vub300->command_res_urb->actual_length = 0;
 837                ret = usb_submit_urb(vub300->command_res_urb, GFP_ATOMIC);
 838                if (ret == 0) {
 839                        /*
 840                         * the urb completion handler will call
 841                         * our completion handler
 842                         */
 843                } else {
 844                        /*
 845                         * and thus we only call it directly
 846                         * when it will not be called
 847                         */
 848                        complete(&vub300->command_complete);
 849                }
 850        }
 851}
 852
 853/*
 854 * the STUFF bits are masked out for the comparisons
 855 */
 856static void snoop_block_size_and_bus_width(struct vub300_mmc_host *vub300,
 857                                           u32 cmd_arg)
 858{
 859        if ((0xFBFFFE00 & cmd_arg) == 0x80022200)
 860                vub300->fbs[1] = (cmd_arg << 8) | (0x00FF & vub300->fbs[1]);
 861        else if ((0xFBFFFE00 & cmd_arg) == 0x80022000)
 862                vub300->fbs[1] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[1]);
 863        else if ((0xFBFFFE00 & cmd_arg) == 0x80042200)
 864                vub300->fbs[2] = (cmd_arg << 8) | (0x00FF & vub300->fbs[2]);
 865        else if ((0xFBFFFE00 & cmd_arg) == 0x80042000)
 866                vub300->fbs[2] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[2]);
 867        else if ((0xFBFFFE00 & cmd_arg) == 0x80062200)
 868                vub300->fbs[3] = (cmd_arg << 8) | (0x00FF & vub300->fbs[3]);
 869        else if ((0xFBFFFE00 & cmd_arg) == 0x80062000)
 870                vub300->fbs[3] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[3]);
 871        else if ((0xFBFFFE00 & cmd_arg) == 0x80082200)
 872                vub300->fbs[4] = (cmd_arg << 8) | (0x00FF & vub300->fbs[4]);
 873        else if ((0xFBFFFE00 & cmd_arg) == 0x80082000)
 874                vub300->fbs[4] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[4]);
 875        else if ((0xFBFFFE00 & cmd_arg) == 0x800A2200)
 876                vub300->fbs[5] = (cmd_arg << 8) | (0x00FF & vub300->fbs[5]);
 877        else if ((0xFBFFFE00 & cmd_arg) == 0x800A2000)
 878                vub300->fbs[5] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[5]);
 879        else if ((0xFBFFFE00 & cmd_arg) == 0x800C2200)
 880                vub300->fbs[6] = (cmd_arg << 8) | (0x00FF & vub300->fbs[6]);
 881        else if ((0xFBFFFE00 & cmd_arg) == 0x800C2000)
 882                vub300->fbs[6] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[6]);
 883        else if ((0xFBFFFE00 & cmd_arg) == 0x800E2200)
 884                vub300->fbs[7] = (cmd_arg << 8) | (0x00FF & vub300->fbs[7]);
 885        else if ((0xFBFFFE00 & cmd_arg) == 0x800E2000)
 886                vub300->fbs[7] = (0xFF & cmd_arg) | (0xFF00 & vub300->fbs[7]);
 887        else if ((0xFBFFFE03 & cmd_arg) == 0x80000E00)
 888                vub300->bus_width = 1;
 889        else if ((0xFBFFFE03 & cmd_arg) == 0x80000E02)
 890                vub300->bus_width = 4;
 891}
 892
 893static void send_command(struct vub300_mmc_host *vub300)
 894{
 895        /* cmd_mutex is held by vub300_cmndwork_thread */
 896        struct mmc_command *cmd = vub300->cmd;
 897        struct mmc_data *data = vub300->data;
 898        int retval;
 899        int i;
 900        u8 response_type;
 901        if (vub300->app_spec) {
 902                switch (cmd->opcode) {
 903                case 6:
 904                        response_type = SDRT_1;
 905                        vub300->resp_len = 6;
 906                        if (0x00000000 == (0x00000003 & cmd->arg))
 907                                vub300->bus_width = 1;
 908                        else if (0x00000002 == (0x00000003 & cmd->arg))
 909                                vub300->bus_width = 4;
 910                        else
 911                                dev_err(&vub300->udev->dev,
 912                                        "unexpected ACMD6 bus_width=%d\n",
 913                                        0x00000003 & cmd->arg);
 914                        break;
 915                case 13:
 916                        response_type = SDRT_1;
 917                        vub300->resp_len = 6;
 918                        break;
 919                case 22:
 920                        response_type = SDRT_1;
 921                        vub300->resp_len = 6;
 922                        break;
 923                case 23:
 924                        response_type = SDRT_1;
 925                        vub300->resp_len = 6;
 926                        break;
 927                case 41:
 928                        response_type = SDRT_3;
 929                        vub300->resp_len = 6;
 930                        break;
 931                case 42:
 932                        response_type = SDRT_1;
 933                        vub300->resp_len = 6;
 934                        break;
 935                case 51:
 936                        response_type = SDRT_1;
 937                        vub300->resp_len = 6;
 938                        break;
 939                case 55:
 940                        response_type = SDRT_1;
 941                        vub300->resp_len = 6;
 942                        break;
 943                default:
 944                        vub300->resp_len = 0;
 945                        cmd->error = -EINVAL;
 946                        complete(&vub300->command_complete);
 947                        return;
 948                }
 949                vub300->app_spec = 0;
 950        } else {
 951                switch (cmd->opcode) {
 952                case 0:
 953                        response_type = SDRT_NONE;
 954                        vub300->resp_len = 0;
 955                        break;
 956                case 1:
 957                        response_type = SDRT_3;
 958                        vub300->resp_len = 6;
 959                        break;
 960                case 2:
 961                        response_type = SDRT_2;
 962                        vub300->resp_len = 17;
 963                        break;
 964                case 3:
 965                        response_type = SDRT_6;
 966                        vub300->resp_len = 6;
 967                        break;
 968                case 4:
 969                        response_type = SDRT_NONE;
 970                        vub300->resp_len = 0;
 971                        break;
 972                case 5:
 973                        response_type = SDRT_4;
 974                        vub300->resp_len = 6;
 975                        break;
 976                case 6:
 977                        response_type = SDRT_1;
 978                        vub300->resp_len = 6;
 979                        break;
 980                case 7:
 981                        response_type = SDRT_1B;
 982                        vub300->resp_len = 6;
 983                        break;
 984                case 8:
 985                        response_type = SDRT_7;
 986                        vub300->resp_len = 6;
 987                        break;
 988                case 9:
 989                        response_type = SDRT_2;
 990                        vub300->resp_len = 17;
 991                        break;
 992                case 10:
 993                        response_type = SDRT_2;
 994                        vub300->resp_len = 17;
 995                        break;
 996                case 12:
 997                        response_type = SDRT_1B;
 998                        vub300->resp_len = 6;
 999                        break;
1000                case 13:
1001                        response_type = SDRT_1;
1002                        vub300->resp_len = 6;
1003                        break;
1004                case 15:
1005                        response_type = SDRT_NONE;
1006                        vub300->resp_len = 0;
1007                        break;
1008                case 16:
1009                        for (i = 0; i < ARRAY_SIZE(vub300->fbs); i++)
1010                                vub300->fbs[i] = 0xFFFF & cmd->arg;
1011                        response_type = SDRT_1;
1012                        vub300->resp_len = 6;
1013                        break;
1014                case 17:
1015                case 18:
1016                case 24:
1017                case 25:
1018                case 27:
1019                        response_type = SDRT_1;
1020                        vub300->resp_len = 6;
1021                        break;
1022                case 28:
1023                case 29:
1024                        response_type = SDRT_1B;
1025                        vub300->resp_len = 6;
1026                        break;
1027                case 30:
1028                case 32:
1029                case 33:
1030                        response_type = SDRT_1;
1031                        vub300->resp_len = 6;
1032                        break;
1033                case 38:
1034                        response_type = SDRT_1B;
1035                        vub300->resp_len = 6;
1036                        break;
1037                case 42:
1038                        response_type = SDRT_1;
1039                        vub300->resp_len = 6;
1040                        break;
1041                case 52:
1042                        response_type = SDRT_5;
1043                        vub300->resp_len = 6;
1044                        snoop_block_size_and_bus_width(vub300, cmd->arg);
1045                        break;
1046                case 53:
1047                        response_type = SDRT_5;
1048                        vub300->resp_len = 6;
1049                        break;
1050                case 55:
1051                        response_type = SDRT_1;
1052                        vub300->resp_len = 6;
1053                        vub300->app_spec = 1;
1054                        break;
1055                case 56:
1056                        response_type = SDRT_1;
1057                        vub300->resp_len = 6;
1058                        break;
1059                default:
1060                        vub300->resp_len = 0;
1061                        cmd->error = -EINVAL;
1062                        complete(&vub300->command_complete);
1063                        return;
1064                }
1065        }
1066        /*
1067         * it is a shame that we can not use "sizeof(struct sd_command_header)"
1068         * this is because the packet _must_ be padded to 64 bytes
1069         */
1070        vub300->cmnd.head.header_size = 20;
1071        vub300->cmnd.head.header_type = 0x00;
1072        vub300->cmnd.head.port_number = 0; /* "0" means port 1 */
1073        vub300->cmnd.head.command_type = 0x00; /* standard read command */
1074        vub300->cmnd.head.response_type = response_type;
1075        vub300->cmnd.head.command_index = cmd->opcode;
1076        vub300->cmnd.head.arguments[0] = cmd->arg >> 24;
1077        vub300->cmnd.head.arguments[1] = cmd->arg >> 16;
1078        vub300->cmnd.head.arguments[2] = cmd->arg >> 8;
1079        vub300->cmnd.head.arguments[3] = cmd->arg >> 0;
1080        if (cmd->opcode == 52) {
1081                int fn = 0x7 & (cmd->arg >> 28);
1082                vub300->cmnd.head.block_count[0] = 0;
1083                vub300->cmnd.head.block_count[1] = 0;
1084                vub300->cmnd.head.block_size[0] = (vub300->fbs[fn] >> 8) & 0xFF;
1085                vub300->cmnd.head.block_size[1] = (vub300->fbs[fn] >> 0) & 0xFF;
1086                vub300->cmnd.head.command_type = 0x00;
1087                vub300->cmnd.head.transfer_size[0] = 0;
1088                vub300->cmnd.head.transfer_size[1] = 0;
1089                vub300->cmnd.head.transfer_size[2] = 0;
1090                vub300->cmnd.head.transfer_size[3] = 0;
1091        } else if (!data) {
1092                vub300->cmnd.head.block_count[0] = 0;
1093                vub300->cmnd.head.block_count[1] = 0;
1094                vub300->cmnd.head.block_size[0] = (vub300->fbs[0] >> 8) & 0xFF;
1095                vub300->cmnd.head.block_size[1] = (vub300->fbs[0] >> 0) & 0xFF;
1096                vub300->cmnd.head.command_type = 0x00;
1097                vub300->cmnd.head.transfer_size[0] = 0;
1098                vub300->cmnd.head.transfer_size[1] = 0;
1099                vub300->cmnd.head.transfer_size[2] = 0;
1100                vub300->cmnd.head.transfer_size[3] = 0;
1101        } else if (cmd->opcode == 53) {
1102                int fn = 0x7 & (cmd->arg >> 28);
1103                if (0x08 & vub300->cmnd.head.arguments[0]) { /* BLOCK MODE */
1104                        vub300->cmnd.head.block_count[0] =
1105                                (data->blocks >> 8) & 0xFF;
1106                        vub300->cmnd.head.block_count[1] =
1107                                (data->blocks >> 0) & 0xFF;
1108                        vub300->cmnd.head.block_size[0] =
1109                                (data->blksz >> 8) & 0xFF;
1110                        vub300->cmnd.head.block_size[1] =
1111                                (data->blksz >> 0) & 0xFF;
1112                } else {        /* BYTE MODE */
1113                        vub300->cmnd.head.block_count[0] = 0;
1114                        vub300->cmnd.head.block_count[1] = 0;
1115                        vub300->cmnd.head.block_size[0] =
1116                                (vub300->datasize >> 8) & 0xFF;
1117                        vub300->cmnd.head.block_size[1] =
1118                                (vub300->datasize >> 0) & 0xFF;
1119                }
1120                vub300->cmnd.head.command_type =
1121                        (MMC_DATA_READ & data->flags) ? 0x00 : 0x80;
1122                vub300->cmnd.head.transfer_size[0] =
1123                        (vub300->datasize >> 24) & 0xFF;
1124                vub300->cmnd.head.transfer_size[1] =
1125                        (vub300->datasize >> 16) & 0xFF;
1126                vub300->cmnd.head.transfer_size[2] =
1127                        (vub300->datasize >> 8) & 0xFF;
1128                vub300->cmnd.head.transfer_size[3] =
1129                        (vub300->datasize >> 0) & 0xFF;
1130                if (vub300->datasize < vub300->fbs[fn]) {
1131                        vub300->cmnd.head.block_count[0] = 0;
1132                        vub300->cmnd.head.block_count[1] = 0;
1133                }
1134        } else {
1135                vub300->cmnd.head.block_count[0] = (data->blocks >> 8) & 0xFF;
1136                vub300->cmnd.head.block_count[1] = (data->blocks >> 0) & 0xFF;
1137                vub300->cmnd.head.block_size[0] = (data->blksz >> 8) & 0xFF;
1138                vub300->cmnd.head.block_size[1] = (data->blksz >> 0) & 0xFF;
1139                vub300->cmnd.head.command_type =
1140                        (MMC_DATA_READ & data->flags) ? 0x00 : 0x80;
1141                vub300->cmnd.head.transfer_size[0] =
1142                        (vub300->datasize >> 24) & 0xFF;
1143                vub300->cmnd.head.transfer_size[1] =
1144                        (vub300->datasize >> 16) & 0xFF;
1145                vub300->cmnd.head.transfer_size[2] =
1146                        (vub300->datasize >> 8) & 0xFF;
1147                vub300->cmnd.head.transfer_size[3] =
1148                        (vub300->datasize >> 0) & 0xFF;
1149                if (vub300->datasize < vub300->fbs[0]) {
1150                        vub300->cmnd.head.block_count[0] = 0;
1151                        vub300->cmnd.head.block_count[1] = 0;
1152                }
1153        }
1154        if (vub300->cmnd.head.block_size[0] || vub300->cmnd.head.block_size[1]) {
1155                u16 block_size = vub300->cmnd.head.block_size[1] |
1156                        (vub300->cmnd.head.block_size[0] << 8);
1157                u16 block_boundary = FIRMWARE_BLOCK_BOUNDARY -
1158                        (FIRMWARE_BLOCK_BOUNDARY % block_size);
1159                vub300->cmnd.head.block_boundary[0] =
1160                        (block_boundary >> 8) & 0xFF;
1161                vub300->cmnd.head.block_boundary[1] =
1162                        (block_boundary >> 0) & 0xFF;
1163        } else {
1164                vub300->cmnd.head.block_boundary[0] = 0;
1165                vub300->cmnd.head.block_boundary[1] = 0;
1166        }
1167        usb_fill_bulk_urb(vub300->command_out_urb, vub300->udev,
1168                          usb_sndbulkpipe(vub300->udev, vub300->cmnd_out_ep),
1169                          &vub300->cmnd, sizeof(vub300->cmnd),
1170                          command_out_completed, vub300);
1171        retval = usb_submit_urb(vub300->command_out_urb, GFP_KERNEL);
1172        if (retval < 0) {
1173                cmd->error = retval;
1174                complete(&vub300->command_complete);
1175                return;
1176        } else {
1177                return;
1178        }
1179}
1180
1181/*
1182 * timer callback runs in atomic mode
1183 *       so it cannot call usb_kill_urb()
1184 */
1185static void vub300_sg_timed_out(unsigned long data)
1186{
1187        struct vub300_mmc_host *vub300 = (struct vub300_mmc_host *)data;
1188        vub300->usb_timed_out = 1;
1189        usb_sg_cancel(&vub300->sg_request);
1190        usb_unlink_urb(vub300->command_out_urb);
1191        usb_unlink_urb(vub300->command_res_urb);
1192}
1193
1194static u16 roundup_to_multiple_of_64(u16 number)
1195{
1196        return 0xFFC0 & (0x3F + number);
1197}
1198
1199/*
1200 * this is a separate function to solve the 80 column width restriction
1201 */
1202static void __download_offload_pseudocode(struct vub300_mmc_host *vub300,
1203                                          const struct firmware *fw)
1204{
1205        u8 register_count = 0;
1206        u16 ts = 0;
1207        u16 interrupt_size = 0;
1208        const u8 *data = fw->data;
1209        int size = fw->size;
1210        u8 c;
1211        dev_info(&vub300->udev->dev, "using %s for SDIO offload processing\n",
1212                 vub300->vub_name);
1213        do {
1214                c = *data++;
1215        } while (size-- && c); /* skip comment */
1216        dev_info(&vub300->udev->dev, "using offload firmware %s %s\n", fw->data,
1217                 vub300->vub_name);
1218        if (size < 4) {
1219                dev_err(&vub300->udev->dev,
1220                        "corrupt offload pseudocode in firmware %s\n",
1221                        vub300->vub_name);
1222                strncpy(vub300->vub_name, "corrupt offload pseudocode",
1223                        sizeof(vub300->vub_name));
1224                return;
1225        }
1226        interrupt_size += *data++;
1227        size -= 1;
1228        interrupt_size <<= 8;
1229        interrupt_size += *data++;
1230        size -= 1;
1231        if (interrupt_size < size) {
1232                u16 xfer_length = roundup_to_multiple_of_64(interrupt_size);
1233                u8 *xfer_buffer = kmalloc(xfer_length, GFP_KERNEL);
1234                if (xfer_buffer) {
1235                        int retval;
1236                        memcpy(xfer_buffer, data, interrupt_size);
1237                        memset(xfer_buffer + interrupt_size, 0,
1238                               xfer_length - interrupt_size);
1239                        size -= interrupt_size;
1240                        data += interrupt_size;
1241                        retval =
1242                                usb_control_msg(vub300->udev,
1243                                                usb_sndctrlpipe(vub300->udev, 0),
1244                                                SET_INTERRUPT_PSEUDOCODE,
1245                                                USB_DIR_OUT | USB_TYPE_VENDOR |
1246                                                USB_RECIP_DEVICE, 0x0000, 0x0000,
1247                                                xfer_buffer, xfer_length, HZ);
1248                        kfree(xfer_buffer);
1249                        if (retval < 0) {
1250                                strncpy(vub300->vub_name,
1251                                        "SDIO pseudocode download failed",
1252                                        sizeof(vub300->vub_name));
1253                                return;
1254                        }
1255                } else {
1256                        dev_err(&vub300->udev->dev,
1257                                "not enough memory for xfer buffer to send"
1258                                " INTERRUPT_PSEUDOCODE for %s %s\n", fw->data,
1259                                vub300->vub_name);
1260                        strncpy(vub300->vub_name,
1261                                "SDIO interrupt pseudocode download failed",
1262                                sizeof(vub300->vub_name));
1263                        return;
1264                }
1265        } else {
1266                dev_err(&vub300->udev->dev,
1267                        "corrupt interrupt pseudocode in firmware %s %s\n",
1268                        fw->data, vub300->vub_name);
1269                strncpy(vub300->vub_name, "corrupt interrupt pseudocode",
1270                        sizeof(vub300->vub_name));
1271                return;
1272        }
1273        ts += *data++;
1274        size -= 1;
1275        ts <<= 8;
1276        ts += *data++;
1277        size -= 1;
1278        if (ts < size) {
1279                u16 xfer_length = roundup_to_multiple_of_64(ts);
1280                u8 *xfer_buffer = kmalloc(xfer_length, GFP_KERNEL);
1281                if (xfer_buffer) {
1282                        int retval;
1283                        memcpy(xfer_buffer, data, ts);
1284                        memset(xfer_buffer + ts, 0,
1285                               xfer_length - ts);
1286                        size -= ts;
1287                        data += ts;
1288                        retval =
1289                                usb_control_msg(vub300->udev,
1290                                                usb_sndctrlpipe(vub300->udev, 0),
1291                                                SET_TRANSFER_PSEUDOCODE,
1292                                                USB_DIR_OUT | USB_TYPE_VENDOR |
1293                                                USB_RECIP_DEVICE, 0x0000, 0x0000,
1294                                                xfer_buffer, xfer_length, HZ);
1295                        kfree(xfer_buffer);
1296                        if (retval < 0) {
1297                                strncpy(vub300->vub_name,
1298                                        "SDIO pseudocode download failed",
1299                                        sizeof(vub300->vub_name));
1300                                return;
1301                        }
1302                } else {
1303                        dev_err(&vub300->udev->dev,
1304                                "not enough memory for xfer buffer to send"
1305                                " TRANSFER_PSEUDOCODE for %s %s\n", fw->data,
1306                                vub300->vub_name);
1307                        strncpy(vub300->vub_name,
1308                                "SDIO transfer pseudocode download failed",
1309                                sizeof(vub300->vub_name));
1310                        return;
1311                }
1312        } else {
1313                dev_err(&vub300->udev->dev,
1314                        "corrupt transfer pseudocode in firmware %s %s\n",
1315                        fw->data, vub300->vub_name);
1316                strncpy(vub300->vub_name, "corrupt transfer pseudocode",
1317                        sizeof(vub300->vub_name));
1318                return;
1319        }
1320        register_count += *data++;
1321        size -= 1;
1322        if (register_count * 4 == size) {
1323                int I = vub300->dynamic_register_count = register_count;
1324                int i = 0;
1325                while (I--) {
1326                        unsigned int func_num = 0;
1327                        vub300->sdio_register[i].func_num = *data++;
1328                        size -= 1;
1329                        func_num += *data++;
1330                        size -= 1;
1331                        func_num <<= 8;
1332                        func_num += *data++;
1333                        size -= 1;
1334                        func_num <<= 8;
1335                        func_num += *data++;
1336                        size -= 1;
1337                        vub300->sdio_register[i].sdio_reg = func_num;
1338                        vub300->sdio_register[i].activate = 1;
1339                        vub300->sdio_register[i].prepared = 0;
1340                        i += 1;
1341                }
1342                dev_info(&vub300->udev->dev,
1343                         "initialized %d dynamic pseudocode registers\n",
1344                         vub300->dynamic_register_count);
1345                return;
1346        } else {
1347                dev_err(&vub300->udev->dev,
1348                        "corrupt dynamic registers in firmware %s\n",
1349                        vub300->vub_name);
1350                strncpy(vub300->vub_name, "corrupt dynamic registers",
1351                        sizeof(vub300->vub_name));
1352                return;
1353        }
1354}
1355
1356/*
1357 * if the binary containing the EMPTY PseudoCode can not be found
1358 * vub300->vub_name is set anyway in order to prevent an automatic retry
1359 */
1360static void download_offload_pseudocode(struct vub300_mmc_host *vub300)
1361{
1362        struct mmc_card *card = vub300->mmc->card;
1363        int sdio_funcs = card->sdio_funcs;
1364        const struct firmware *fw = NULL;
1365        int l = snprintf(vub300->vub_name, sizeof(vub300->vub_name),
1366                         "vub_%04X%04X", card->cis.vendor, card->cis.device);
1367        int n = 0;
1368        int retval;
1369        for (n = 0; n < sdio_funcs; n++) {
1370                struct sdio_func *sf = card->sdio_func[n];
1371                l += snprintf(vub300->vub_name + l,
1372                              sizeof(vub300->vub_name) - l, "_%04X%04X",
1373                              sf->vendor, sf->device);
1374        }
1375        snprintf(vub300->vub_name + l, sizeof(vub300->vub_name) - l, ".bin");
1376        dev_info(&vub300->udev->dev, "requesting offload firmware %s\n",
1377                 vub300->vub_name);
1378        retval = request_firmware(&fw, vub300->vub_name, &card->dev);
1379        if (retval < 0) {
1380                strncpy(vub300->vub_name, "vub_default.bin",
1381                        sizeof(vub300->vub_name));
1382                retval = request_firmware(&fw, vub300->vub_name, &card->dev);
1383                if (retval < 0) {
1384                        strncpy(vub300->vub_name,
1385                                "no SDIO offload firmware found",
1386                                sizeof(vub300->vub_name));
1387                } else {
1388                        __download_offload_pseudocode(vub300, fw);
1389                        release_firmware(fw);
1390                }
1391        } else {
1392                __download_offload_pseudocode(vub300, fw);
1393                release_firmware(fw);
1394        }
1395}
1396
1397static void vub300_usb_bulk_msg_completion(struct urb *urb)
1398{                               /* urb completion handler - hardirq */
1399        complete((struct completion *)urb->context);
1400}
1401
1402static int vub300_usb_bulk_msg(struct vub300_mmc_host *vub300,
1403                               unsigned int pipe, void *data, int len,
1404                               int *actual_length, int timeout_msecs)
1405{
1406        /* cmd_mutex is held by vub300_cmndwork_thread */
1407        struct usb_device *usb_dev = vub300->udev;
1408        struct completion done;
1409        int retval;
1410        vub300->urb = usb_alloc_urb(0, GFP_KERNEL);
1411        if (!vub300->urb)
1412                return -ENOMEM;
1413        usb_fill_bulk_urb(vub300->urb, usb_dev, pipe, data, len,
1414                          vub300_usb_bulk_msg_completion, NULL);
1415        init_completion(&done);
1416        vub300->urb->context = &done;
1417        vub300->urb->actual_length = 0;
1418        retval = usb_submit_urb(vub300->urb, GFP_KERNEL);
1419        if (unlikely(retval))
1420                goto out;
1421        if (!wait_for_completion_timeout
1422            (&done, msecs_to_jiffies(timeout_msecs))) {
1423                retval = -ETIMEDOUT;
1424                usb_kill_urb(vub300->urb);
1425        } else {
1426                retval = vub300->urb->status;
1427        }
1428out:
1429        *actual_length = vub300->urb->actual_length;
1430        usb_free_urb(vub300->urb);
1431        vub300->urb = NULL;
1432        return retval;
1433}
1434
1435static int __command_read_data(struct vub300_mmc_host *vub300,
1436                               struct mmc_command *cmd, struct mmc_data *data)
1437{
1438        /* cmd_mutex is held by vub300_cmndwork_thread */
1439        int linear_length = vub300->datasize;
1440        int padded_length = vub300->large_usb_packets ?
1441                ((511 + linear_length) >> 9) << 9 :
1442                ((63 + linear_length) >> 6) << 6;
1443        if ((padded_length == linear_length) || !pad_input_to_usb_pkt) {
1444                int result;
1445                unsigned pipe;
1446                pipe = usb_rcvbulkpipe(vub300->udev, vub300->data_inp_ep);
1447                result = usb_sg_init(&vub300->sg_request, vub300->udev,
1448                                     pipe, 0, data->sg,
1449                                     data->sg_len, 0, GFP_KERNEL);
1450                if (result < 0) {
1451                        usb_unlink_urb(vub300->command_out_urb);
1452                        usb_unlink_urb(vub300->command_res_urb);
1453                        cmd->error = result;
1454                        data->bytes_xfered = 0;
1455                        return 0;
1456                } else {
1457                        vub300->sg_transfer_timer.expires =
1458                                jiffies + msecs_to_jiffies(2000 +
1459                                                  (linear_length / 16384));
1460                        add_timer(&vub300->sg_transfer_timer);
1461                        usb_sg_wait(&vub300->sg_request);
1462                        del_timer(&vub300->sg_transfer_timer);
1463                        if (vub300->sg_request.status < 0) {
1464                                cmd->error = vub300->sg_request.status;
1465                                data->bytes_xfered = 0;
1466                                return 0;
1467                        } else {
1468                                data->bytes_xfered = vub300->datasize;
1469                                return linear_length;
1470                        }
1471                }
1472        } else {
1473                u8 *buf = kmalloc(padded_length, GFP_KERNEL);
1474                if (buf) {
1475                        int result;
1476                        unsigned pipe = usb_rcvbulkpipe(vub300->udev,
1477                                                        vub300->data_inp_ep);
1478                        int actual_length = 0;
1479                        result = vub300_usb_bulk_msg(vub300, pipe, buf,
1480                                             padded_length, &actual_length,
1481                                             2000 + (padded_length / 16384));
1482                        if (result < 0) {
1483                                cmd->error = result;
1484                                data->bytes_xfered = 0;
1485                                kfree(buf);
1486                                return 0;
1487                        } else if (actual_length < linear_length) {
1488                                cmd->error = -EREMOTEIO;
1489                                data->bytes_xfered = 0;
1490                                kfree(buf);
1491                                return 0;
1492                        } else {
1493                                sg_copy_from_buffer(data->sg, data->sg_len, buf,
1494                                                    linear_length);
1495                                kfree(buf);
1496                                data->bytes_xfered = vub300->datasize;
1497                                return linear_length;
1498                        }
1499                } else {
1500                        cmd->error = -ENOMEM;
1501                        data->bytes_xfered = 0;
1502                        return 0;
1503                }
1504        }
1505}
1506
1507static int __command_write_data(struct vub300_mmc_host *vub300,
1508                                struct mmc_command *cmd, struct mmc_data *data)
1509{
1510        /* cmd_mutex is held by vub300_cmndwork_thread */
1511        unsigned pipe = usb_sndbulkpipe(vub300->udev, vub300->data_out_ep);
1512        int linear_length = vub300->datasize;
1513        int modulo_64_length = linear_length & 0x003F;
1514        int modulo_512_length = linear_length & 0x01FF;
1515        if (linear_length < 64) {
1516                int result;
1517                int actual_length;
1518                sg_copy_to_buffer(data->sg, data->sg_len,
1519                                  vub300->padded_buffer,
1520                                  sizeof(vub300->padded_buffer));
1521                memset(vub300->padded_buffer + linear_length, 0,
1522                       sizeof(vub300->padded_buffer) - linear_length);
1523                result = vub300_usb_bulk_msg(vub300, pipe, vub300->padded_buffer,
1524                                             sizeof(vub300->padded_buffer),
1525                                             &actual_length, 2000 +
1526                                             (sizeof(vub300->padded_buffer) /
1527                                              16384));
1528                if (result < 0) {
1529                        cmd->error = result;
1530                        data->bytes_xfered = 0;
1531                } else {
1532                        data->bytes_xfered = vub300->datasize;
1533                }
1534        } else if ((!vub300->large_usb_packets && (0 < modulo_64_length)) ||
1535                    (vub300->large_usb_packets && (64 > modulo_512_length))
1536                ) {             /* don't you just love these work-rounds */
1537                int padded_length = ((63 + linear_length) >> 6) << 6;
1538                u8 *buf = kmalloc(padded_length, GFP_KERNEL);
1539                if (buf) {
1540                        int result;
1541                        int actual_length;
1542                        sg_copy_to_buffer(data->sg, data->sg_len, buf,
1543                                          padded_length);
1544                        memset(buf + linear_length, 0,
1545                               padded_length - linear_length);
1546                        result =
1547                                vub300_usb_bulk_msg(vub300, pipe, buf,
1548                                                    padded_length, &actual_length,
1549                                                    2000 + padded_length / 16384);
1550                        kfree(buf);
1551                        if (result < 0) {
1552                                cmd->error = result;
1553                                data->bytes_xfered = 0;
1554                        } else {
1555                                data->bytes_xfered = vub300->datasize;
1556                        }
1557                } else {
1558                        cmd->error = -ENOMEM;
1559                        data->bytes_xfered = 0;
1560                }
1561        } else {                /* no data padding required */
1562                int result;
1563                unsigned char buf[64 * 4];
1564                sg_copy_to_buffer(data->sg, data->sg_len, buf, sizeof(buf));
1565                result = usb_sg_init(&vub300->sg_request, vub300->udev,
1566                                     pipe, 0, data->sg,
1567                                     data->sg_len, 0, GFP_KERNEL);
1568                if (result < 0) {
1569                        usb_unlink_urb(vub300->command_out_urb);
1570                        usb_unlink_urb(vub300->command_res_urb);
1571                        cmd->error = result;
1572                        data->bytes_xfered = 0;
1573                } else {
1574                        vub300->sg_transfer_timer.expires =
1575                                jiffies + msecs_to_jiffies(2000 +
1576                                                           linear_length / 16384);
1577                        add_timer(&vub300->sg_transfer_timer);
1578                        usb_sg_wait(&vub300->sg_request);
1579                        if (cmd->error) {
1580                                data->bytes_xfered = 0;
1581                        } else {
1582                                del_timer(&vub300->sg_transfer_timer);
1583                                if (vub300->sg_request.status < 0) {
1584                                        cmd->error = vub300->sg_request.status;
1585                                        data->bytes_xfered = 0;
1586                                } else {
1587                                        data->bytes_xfered = vub300->datasize;
1588                                }
1589                        }
1590                }
1591        }
1592        return linear_length;
1593}
1594
1595static void __vub300_command_response(struct vub300_mmc_host *vub300,
1596                                      struct mmc_command *cmd,
1597                                      struct mmc_data *data, int data_length)
1598{
1599        /* cmd_mutex is held by vub300_cmndwork_thread */
1600        long respretval;
1601        int msec_timeout = 1000 + data_length / 4;
1602        respretval =
1603                wait_for_completion_timeout(&vub300->command_complete,
1604                                            msecs_to_jiffies(msec_timeout));
1605        if (respretval == 0) { /* TIMED OUT */
1606                /* we don't know which of "out" and "res" if any failed */
1607                int result;
1608                vub300->usb_timed_out = 1;
1609                usb_kill_urb(vub300->command_out_urb);
1610                usb_kill_urb(vub300->command_res_urb);
1611                cmd->error = -ETIMEDOUT;
1612                result = usb_lock_device_for_reset(vub300->udev,
1613                                                   vub300->interface);
1614                if (result == 0) {
1615                        result = usb_reset_device(vub300->udev);
1616                        usb_unlock_device(vub300->udev);
1617                }
1618        } else if (respretval < 0) {
1619                /* we don't know which of "out" and "res" if any failed */
1620                usb_kill_urb(vub300->command_out_urb);
1621                usb_kill_urb(vub300->command_res_urb);
1622                cmd->error = respretval;
1623        } else if (cmd->error) {
1624                /*
1625                 * the error occurred sending the command
1626                 * or receiving the response
1627                 */
1628        } else if (vub300->command_out_urb->status) {
1629                vub300->usb_transport_fail = vub300->command_out_urb->status;
1630                cmd->error = -EPROTO == vub300->command_out_urb->status ?
1631                        -ESHUTDOWN : vub300->command_out_urb->status;
1632        } else if (vub300->command_res_urb->status) {
1633                vub300->usb_transport_fail = vub300->command_res_urb->status;
1634                cmd->error = -EPROTO == vub300->command_res_urb->status ?
1635                        -ESHUTDOWN : vub300->command_res_urb->status;
1636        } else if (vub300->resp.common.header_type == 0x00) {
1637                /*
1638                 * the command completed successfully
1639                 * and there was no piggybacked data
1640                 */
1641        } else if (vub300->resp.common.header_type == RESPONSE_ERROR) {
1642                cmd->error =
1643                        vub300_response_error(vub300->resp.error.error_code);
1644                if (vub300->data)
1645                        usb_sg_cancel(&vub300->sg_request);
1646        } else if (vub300->resp.common.header_type == RESPONSE_PIGGYBACKED) {
1647                int offloaded_data_length =
1648                        vub300->resp.common.header_size -
1649                        sizeof(struct sd_register_header);
1650                int register_count = offloaded_data_length >> 3;
1651                int ri = 0;
1652                while (register_count--) {
1653                        add_offloaded_reg(vub300, &vub300->resp.pig.reg[ri]);
1654                        ri += 1;
1655                }
1656                vub300->resp.common.header_size =
1657                        sizeof(struct sd_register_header);
1658                vub300->resp.common.header_type = 0x00;
1659                cmd->error = 0;
1660        } else if (vub300->resp.common.header_type == RESPONSE_PIG_DISABLED) {
1661                int offloaded_data_length =
1662                        vub300->resp.common.header_size -
1663                        sizeof(struct sd_register_header);
1664                int register_count = offloaded_data_length >> 3;
1665                int ri = 0;
1666                while (register_count--) {
1667                        add_offloaded_reg(vub300, &vub300->resp.pig.reg[ri]);
1668                        ri += 1;
1669                }
1670                mutex_lock(&vub300->irq_mutex);
1671                if (vub300->irqs_queued) {
1672                        vub300->irqs_queued += 1;
1673                } else if (vub300->irq_enabled) {
1674                        vub300->irqs_queued += 1;
1675                        vub300_queue_poll_work(vub300, 0);
1676                } else {
1677                        vub300->irqs_queued += 1;
1678                }
1679                vub300->irq_disabled = 1;
1680                mutex_unlock(&vub300->irq_mutex);
1681                vub300->resp.common.header_size =
1682                        sizeof(struct sd_register_header);
1683                vub300->resp.common.header_type = 0x00;
1684                cmd->error = 0;
1685        } else if (vub300->resp.common.header_type == RESPONSE_PIG_ENABLED) {
1686                int offloaded_data_length =
1687                        vub300->resp.common.header_size -
1688                        sizeof(struct sd_register_header);
1689                int register_count = offloaded_data_length >> 3;
1690                int ri = 0;
1691                while (register_count--) {
1692                        add_offloaded_reg(vub300, &vub300->resp.pig.reg[ri]);
1693                        ri += 1;
1694                }
1695                mutex_lock(&vub300->irq_mutex);
1696                if (vub300->irqs_queued) {
1697                        vub300->irqs_queued += 1;
1698                } else if (vub300->irq_enabled) {
1699                        vub300->irqs_queued += 1;
1700                        vub300_queue_poll_work(vub300, 0);
1701                } else {
1702                        vub300->irqs_queued += 1;
1703                }
1704                vub300->irq_disabled = 0;
1705                mutex_unlock(&vub300->irq_mutex);
1706                vub300->resp.common.header_size =
1707                        sizeof(struct sd_register_header);
1708                vub300->resp.common.header_type = 0x00;
1709                cmd->error = 0;
1710        } else {
1711                cmd->error = -EINVAL;
1712        }
1713}
1714
1715static void construct_request_response(struct vub300_mmc_host *vub300,
1716                                       struct mmc_command *cmd)
1717{
1718        int resp_len = vub300->resp_len;
1719        int less_cmd = (17 == resp_len) ? resp_len : resp_len - 1;
1720        int bytes = 3 & less_cmd;
1721        int words = less_cmd >> 2;
1722        u8 *r = vub300->resp.response.command_response;
1723        if (bytes == 3) {
1724                cmd->resp[words] = (r[1 + (words << 2)] << 24)
1725                        | (r[2 + (words << 2)] << 16)
1726                        | (r[3 + (words << 2)] << 8);
1727        } else if (bytes == 2) {
1728                cmd->resp[words] = (r[1 + (words << 2)] << 24)
1729                        | (r[2 + (words << 2)] << 16);
1730        } else if (bytes == 1) {
1731                cmd->resp[words] = (r[1 + (words << 2)] << 24);
1732        }
1733        while (words-- > 0) {
1734                cmd->resp[words] = (r[1 + (words << 2)] << 24)
1735                        | (r[2 + (words << 2)] << 16)
1736                        | (r[3 + (words << 2)] << 8)
1737                        | (r[4 + (words << 2)] << 0);
1738        }
1739        if ((cmd->opcode == 53) && (0x000000FF & cmd->resp[0]))
1740                cmd->resp[0] &= 0xFFFFFF00;
1741}
1742
1743/* this thread runs only when there is an upper level command req outstanding */
1744static void vub300_cmndwork_thread(struct work_struct *work)
1745{
1746        struct vub300_mmc_host *vub300 =
1747                container_of(work, struct vub300_mmc_host, cmndwork);
1748        if (!vub300->interface) {
1749                kref_put(&vub300->kref, vub300_delete);
1750                return;
1751        } else {
1752                struct mmc_request *req = vub300->req;
1753                struct mmc_command *cmd = vub300->cmd;
1754                struct mmc_data *data = vub300->data;
1755                int data_length;
1756                mutex_lock(&vub300->cmd_mutex);
1757                init_completion(&vub300->command_complete);
1758                if (likely(vub300->vub_name[0]) || !vub300->mmc->card) {
1759                        /*
1760                         * the name of the EMPTY Pseudo firmware file
1761                         * is used as a flag to indicate that the file
1762                         * has been already downloaded to the VUB300 chip
1763                         */
1764                } else if (0 == vub300->mmc->card->sdio_funcs) {
1765                        strncpy(vub300->vub_name, "SD memory device",
1766                                sizeof(vub300->vub_name));
1767                } else {
1768                        download_offload_pseudocode(vub300);
1769                }
1770                send_command(vub300);
1771                if (!data)
1772                        data_length = 0;
1773                else if (MMC_DATA_READ & data->flags)
1774                        data_length = __command_read_data(vub300, cmd, data);
1775                else
1776                        data_length = __command_write_data(vub300, cmd, data);
1777                __vub300_command_response(vub300, cmd, data, data_length);
1778                vub300->req = NULL;
1779                vub300->cmd = NULL;
1780                vub300->data = NULL;
1781                if (cmd->error) {
1782                        if (cmd->error == -ENOMEDIUM)
1783                                check_vub300_port_status(vub300);
1784                        mutex_unlock(&vub300->cmd_mutex);
1785                        mmc_request_done(vub300->mmc, req);
1786                        kref_put(&vub300->kref, vub300_delete);
1787                        return;
1788                } else {
1789                        construct_request_response(vub300, cmd);
1790                        vub300->resp_len = 0;
1791                        mutex_unlock(&vub300->cmd_mutex);
1792                        kref_put(&vub300->kref, vub300_delete);
1793                        mmc_request_done(vub300->mmc, req);
1794                        return;
1795                }
1796        }
1797}
1798
1799static int examine_cyclic_buffer(struct vub300_mmc_host *vub300,
1800                                 struct mmc_command *cmd, u8 Function)
1801{
1802        /* cmd_mutex is held by vub300_mmc_request */
1803        u8 cmd0 = 0xFF & (cmd->arg >> 24);
1804        u8 cmd1 = 0xFF & (cmd->arg >> 16);
1805        u8 cmd2 = 0xFF & (cmd->arg >> 8);
1806        u8 cmd3 = 0xFF & (cmd->arg >> 0);
1807        int first = MAXREGMASK & vub300->fn[Function].offload_point;
1808        struct offload_registers_access *rf = &vub300->fn[Function].reg[first];
1809        if (cmd0 == rf->command_byte[0] &&
1810            cmd1 == rf->command_byte[1] &&
1811            cmd2 == rf->command_byte[2] &&
1812            cmd3 == rf->command_byte[3]) {
1813                u8 checksum = 0x00;
1814                cmd->resp[1] = checksum << 24;
1815                cmd->resp[0] = (rf->Respond_Byte[0] << 24)
1816                        | (rf->Respond_Byte[1] << 16)
1817                        | (rf->Respond_Byte[2] << 8)
1818                        | (rf->Respond_Byte[3] << 0);
1819                vub300->fn[Function].offload_point += 1;
1820                vub300->fn[Function].offload_count -= 1;
1821                vub300->total_offload_count -= 1;
1822                return 1;
1823        } else {
1824                int delta = 1;  /* because it does not match the first one */
1825                u8 register_count = vub300->fn[Function].offload_count - 1;
1826                u32 register_point = vub300->fn[Function].offload_point + 1;
1827                while (0 < register_count) {
1828                        int point = MAXREGMASK & register_point;
1829                        struct offload_registers_access *r =
1830                                &vub300->fn[Function].reg[point];
1831                        if (cmd0 == r->command_byte[0] &&
1832                            cmd1 == r->command_byte[1] &&
1833                            cmd2 == r->command_byte[2] &&
1834                            cmd3 == r->command_byte[3]) {
1835                                u8 checksum = 0x00;
1836                                cmd->resp[1] = checksum << 24;
1837                                cmd->resp[0] = (r->Respond_Byte[0] << 24)
1838                                        | (r->Respond_Byte[1] << 16)
1839                                        | (r->Respond_Byte[2] << 8)
1840                                        | (r->Respond_Byte[3] << 0);
1841                                vub300->fn[Function].offload_point += delta;
1842                                vub300->fn[Function].offload_count -= delta;
1843                                vub300->total_offload_count -= delta;
1844                                return 1;
1845                        } else {
1846                                register_point += 1;
1847                                register_count -= 1;
1848                                delta += 1;
1849                                continue;
1850                        }
1851                }
1852                return 0;
1853        }
1854}
1855
1856static int satisfy_request_from_offloaded_data(struct vub300_mmc_host *vub300,
1857                                               struct mmc_command *cmd)
1858{
1859        /* cmd_mutex is held by vub300_mmc_request */
1860        u8 regs = vub300->dynamic_register_count;
1861        u8 i = 0;
1862        u8 func = FUN(cmd);
1863        u32 reg = REG(cmd);
1864        while (0 < regs--) {
1865                if ((vub300->sdio_register[i].func_num == func) &&
1866                    (vub300->sdio_register[i].sdio_reg == reg)) {
1867                        if (!vub300->sdio_register[i].prepared) {
1868                                return 0;
1869                        } else if ((0x80000000 & cmd->arg) == 0x80000000) {
1870                                /*
1871                                 * a write to a dynamic register
1872                                 * nullifies our offloaded value
1873                                 */
1874                                vub300->sdio_register[i].prepared = 0;
1875                                return 0;
1876                        } else {
1877                                u8 checksum = 0x00;
1878                                u8 rsp0 = 0x00;
1879                                u8 rsp1 = 0x00;
1880                                u8 rsp2 = vub300->sdio_register[i].response;
1881                                u8 rsp3 = vub300->sdio_register[i].regvalue;
1882                                vub300->sdio_register[i].prepared = 0;
1883                                cmd->resp[1] = checksum << 24;
1884                                cmd->resp[0] = (rsp0 << 24)
1885                                        | (rsp1 << 16)
1886                                        | (rsp2 << 8)
1887                                        | (rsp3 << 0);
1888                                return 1;
1889                        }
1890                } else {
1891                        i += 1;
1892                        continue;
1893                }
1894        }
1895        if (vub300->total_offload_count == 0)
1896                return 0;
1897        else if (vub300->fn[func].offload_count == 0)
1898                return 0;
1899        else
1900                return examine_cyclic_buffer(vub300, cmd, func);
1901}
1902
1903static void vub300_mmc_request(struct mmc_host *mmc, struct mmc_request *req)
1904{                               /* NOT irq */
1905        struct mmc_command *cmd = req->cmd;
1906        struct vub300_mmc_host *vub300 = mmc_priv(mmc);
1907        if (!vub300->interface) {
1908                cmd->error = -ESHUTDOWN;
1909                mmc_request_done(mmc, req);
1910                return;
1911        } else {
1912                struct mmc_data *data = req->data;
1913                if (!vub300->card_powered) {
1914                        cmd->error = -ENOMEDIUM;
1915                        mmc_request_done(mmc, req);
1916                        return;
1917                }
1918                if (!vub300->card_present) {
1919                        cmd->error = -ENOMEDIUM;
1920                        mmc_request_done(mmc, req);
1921                        return;
1922                }
1923                if (vub300->usb_transport_fail) {
1924                        cmd->error = vub300->usb_transport_fail;
1925                        mmc_request_done(mmc, req);
1926                        return;
1927                }
1928                if (!vub300->interface) {
1929                        cmd->error = -ENODEV;
1930                        mmc_request_done(mmc, req);
1931                        return;
1932                }
1933                kref_get(&vub300->kref);
1934                mutex_lock(&vub300->cmd_mutex);
1935                mod_timer(&vub300->inactivity_timer, jiffies + HZ);
1936                /*
1937                 * for performance we have to return immediately
1938                 * if the requested data has been offloaded
1939                 */
1940                if (cmd->opcode == 52 &&
1941                    satisfy_request_from_offloaded_data(vub300, cmd)) {
1942                        cmd->error = 0;
1943                        mutex_unlock(&vub300->cmd_mutex);
1944                        kref_put(&vub300->kref, vub300_delete);
1945                        mmc_request_done(mmc, req);
1946                        return;
1947                } else {
1948                        vub300->cmd = cmd;
1949                        vub300->req = req;
1950                        vub300->data = data;
1951                        if (data)
1952                                vub300->datasize = data->blksz * data->blocks;
1953                        else
1954                                vub300->datasize = 0;
1955                        vub300_queue_cmnd_work(vub300);
1956                        mutex_unlock(&vub300->cmd_mutex);
1957                        kref_put(&vub300->kref, vub300_delete);
1958                        /*
1959                         * the kernel lock diagnostics complain
1960                         * if the cmd_mutex * is "passed on"
1961                         * to the cmndwork thread,
1962                         * so we must release it now
1963                         * and re-acquire it in the cmndwork thread
1964                         */
1965                }
1966        }
1967}
1968
1969static void __set_clock_speed(struct vub300_mmc_host *vub300, u8 buf[8],
1970                              struct mmc_ios *ios)
1971{
1972        int buf_array_size = 8; /* ARRAY_SIZE(buf) does not work !!! */
1973        int retval;
1974        u32 kHzClock;
1975        if (ios->clock >= 48000000)
1976                kHzClock = 48000;
1977        else if (ios->clock >= 24000000)
1978                kHzClock = 24000;
1979        else if (ios->clock >= 20000000)
1980                kHzClock = 20000;
1981        else if (ios->clock >= 15000000)
1982                kHzClock = 15000;
1983        else if (ios->clock >= 200000)
1984                kHzClock = 200;
1985        else
1986                kHzClock = 0;
1987        {
1988                int i;
1989                u64 c = kHzClock;
1990                for (i = 0; i < buf_array_size; i++) {
1991                        buf[i] = c;
1992                        c >>= 8;
1993                }
1994        }
1995        retval =
1996                usb_control_msg(vub300->udev, usb_sndctrlpipe(vub300->udev, 0),
1997                                SET_CLOCK_SPEED,
1998                                USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1999                                0x00, 0x00, buf, buf_array_size, HZ);
2000        if (retval != 8) {
2001                dev_err(&vub300->udev->dev, "SET_CLOCK_SPEED"
2002                        " %dkHz failed with retval=%d\n", kHzClock, retval);
2003        } else {
2004                dev_dbg(&vub300->udev->dev, "SET_CLOCK_SPEED"
2005                        " %dkHz\n", kHzClock);
2006        }
2007}
2008
2009static void vub300_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
2010{                               /* NOT irq */
2011        struct vub300_mmc_host *vub300 = mmc_priv(mmc);
2012        if (!vub300->interface)
2013                return;
2014        kref_get(&vub300->kref);
2015        mutex_lock(&vub300->cmd_mutex);
2016        if ((ios->power_mode == MMC_POWER_OFF) && vub300->card_powered) {
2017                vub300->card_powered = 0;
2018                usb_control_msg(vub300->udev, usb_sndctrlpipe(vub300->udev, 0),
2019                                SET_SD_POWER,
2020                                USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2021                                0x0000, 0x0000, NULL, 0, HZ);
2022                /* must wait for the VUB300 u-proc to boot up */
2023                msleep(600);
2024        } else if ((ios->power_mode == MMC_POWER_UP) && !vub300->card_powered) {
2025                usb_control_msg(vub300->udev, usb_sndctrlpipe(vub300->udev, 0),
2026                                SET_SD_POWER,
2027                                USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2028                                0x0001, 0x0000, NULL, 0, HZ);
2029                msleep(600);
2030                vub300->card_powered = 1;
2031        } else if (ios->power_mode == MMC_POWER_ON) {
2032                u8 *buf = kmalloc(8, GFP_KERNEL);
2033                if (buf) {
2034                        __set_clock_speed(vub300, buf, ios);
2035                        kfree(buf);
2036                }
2037        } else {
2038                /* this should mean no change of state */
2039        }
2040        mutex_unlock(&vub300->cmd_mutex);
2041        kref_put(&vub300->kref, vub300_delete);
2042}
2043
2044static int vub300_mmc_get_ro(struct mmc_host *mmc)
2045{
2046        struct vub300_mmc_host *vub300 = mmc_priv(mmc);
2047        return vub300->read_only;
2048}
2049
2050static void vub300_enable_sdio_irq(struct mmc_host *mmc, int enable)
2051{                               /* NOT irq */
2052        struct vub300_mmc_host *vub300 = mmc_priv(mmc);
2053        if (!vub300->interface)
2054                return;
2055        kref_get(&vub300->kref);
2056        if (enable) {
2057                mutex_lock(&vub300->irq_mutex);
2058                if (vub300->irqs_queued) {
2059                        vub300->irqs_queued -= 1;
2060                        mmc_signal_sdio_irq(vub300->mmc);
2061                } else if (vub300->irq_disabled) {
2062                        vub300->irq_disabled = 0;
2063                        vub300->irq_enabled = 1;
2064                        vub300_queue_poll_work(vub300, 0);
2065                } else if (vub300->irq_enabled) {
2066                        /* this should not happen, so we will just ignore it */
2067                } else {
2068                        vub300->irq_enabled = 1;
2069                        vub300_queue_poll_work(vub300, 0);
2070                }
2071                mutex_unlock(&vub300->irq_mutex);
2072        } else {
2073                vub300->irq_enabled = 0;
2074        }
2075        kref_put(&vub300->kref, vub300_delete);
2076}
2077
2078static void vub300_init_card(struct mmc_host *mmc, struct mmc_card *card)
2079{                               /* NOT irq */
2080        struct vub300_mmc_host *vub300 = mmc_priv(mmc);
2081        dev_info(&vub300->udev->dev, "NO host QUIRKS for this card\n");
2082}
2083
2084static const struct mmc_host_ops vub300_mmc_ops = {
2085        .request = vub300_mmc_request,
2086        .set_ios = vub300_mmc_set_ios,
2087        .get_ro = vub300_mmc_get_ro,
2088        .enable_sdio_irq = vub300_enable_sdio_irq,
2089        .init_card = vub300_init_card,
2090};
2091
2092static int vub300_probe(struct usb_interface *interface,
2093                        const struct usb_device_id *id)
2094{                               /* NOT irq */
2095        struct vub300_mmc_host *vub300;
2096        struct usb_host_interface *iface_desc;
2097        struct usb_device *udev = usb_get_dev(interface_to_usbdev(interface));
2098        int i;
2099        int retval = -ENOMEM;
2100        struct urb *command_out_urb;
2101        struct urb *command_res_urb;
2102        struct mmc_host *mmc;
2103        char manufacturer[48];
2104        char product[32];
2105        char serial_number[32];
2106        usb_string(udev, udev->descriptor.iManufacturer, manufacturer,
2107                   sizeof(manufacturer));
2108        usb_string(udev, udev->descriptor.iProduct, product, sizeof(product));
2109        usb_string(udev, udev->descriptor.iSerialNumber, serial_number,
2110                   sizeof(serial_number));
2111        dev_info(&udev->dev, "probing VID:PID(%04X:%04X) %s %s %s\n",
2112                 udev->descriptor.idVendor, udev->descriptor.idProduct,
2113                 manufacturer, product, serial_number);
2114        command_out_urb = usb_alloc_urb(0, GFP_KERNEL);
2115        if (!command_out_urb) {
2116                retval = -ENOMEM;
2117                goto error0;
2118        }
2119        command_res_urb = usb_alloc_urb(0, GFP_KERNEL);
2120        if (!command_res_urb) {
2121                retval = -ENOMEM;
2122                goto error1;
2123        }
2124        /* this also allocates memory for our VUB300 mmc host device */
2125        mmc = mmc_alloc_host(sizeof(struct vub300_mmc_host), &udev->dev);
2126        if (!mmc) {
2127                retval = -ENOMEM;
2128                dev_err(&udev->dev, "not enough memory for the mmc_host\n");
2129                goto error4;
2130        }
2131        /* MMC core transfer sizes tunable parameters */
2132        mmc->caps = 0;
2133        if (!force_1_bit_data_xfers)
2134                mmc->caps |= MMC_CAP_4_BIT_DATA;
2135        if (!force_polling_for_irqs)
2136                mmc->caps |= MMC_CAP_SDIO_IRQ;
2137        mmc->caps &= ~MMC_CAP_NEEDS_POLL;
2138        /*
2139         * MMC_CAP_NEEDS_POLL causes core.c:mmc_rescan() to poll
2140         * for devices which results in spurious CMD7's being
2141         * issued which stops some SDIO cards from working
2142         */
2143        if (limit_speed_to_24_MHz) {
2144                mmc->caps |= MMC_CAP_MMC_HIGHSPEED;
2145                mmc->caps |= MMC_CAP_SD_HIGHSPEED;
2146                mmc->f_max = 24000000;
2147                dev_info(&udev->dev, "limiting SDIO speed to 24_MHz\n");
2148        } else {
2149                mmc->caps |= MMC_CAP_MMC_HIGHSPEED;
2150                mmc->caps |= MMC_CAP_SD_HIGHSPEED;
2151                mmc->f_max = 48000000;
2152        }
2153        mmc->f_min = 200000;
2154        mmc->max_blk_count = 511;
2155        mmc->max_blk_size = 512;
2156        mmc->max_segs = 128;
2157        if (force_max_req_size)
2158                mmc->max_req_size = force_max_req_size * 1024;
2159        else
2160                mmc->max_req_size = 64 * 1024;
2161        mmc->max_seg_size = mmc->max_req_size;
2162        mmc->ocr_avail = 0;
2163        mmc->ocr_avail |= MMC_VDD_165_195;
2164        mmc->ocr_avail |= MMC_VDD_20_21;
2165        mmc->ocr_avail |= MMC_VDD_21_22;
2166        mmc->ocr_avail |= MMC_VDD_22_23;
2167        mmc->ocr_avail |= MMC_VDD_23_24;
2168        mmc->ocr_avail |= MMC_VDD_24_25;
2169        mmc->ocr_avail |= MMC_VDD_25_26;
2170        mmc->ocr_avail |= MMC_VDD_26_27;
2171        mmc->ocr_avail |= MMC_VDD_27_28;
2172        mmc->ocr_avail |= MMC_VDD_28_29;
2173        mmc->ocr_avail |= MMC_VDD_29_30;
2174        mmc->ocr_avail |= MMC_VDD_30_31;
2175        mmc->ocr_avail |= MMC_VDD_31_32;
2176        mmc->ocr_avail |= MMC_VDD_32_33;
2177        mmc->ocr_avail |= MMC_VDD_33_34;
2178        mmc->ocr_avail |= MMC_VDD_34_35;
2179        mmc->ocr_avail |= MMC_VDD_35_36;
2180        mmc->ops = &vub300_mmc_ops;
2181        vub300 = mmc_priv(mmc);
2182        vub300->mmc = mmc;
2183        vub300->card_powered = 0;
2184        vub300->bus_width = 0;
2185        vub300->cmnd.head.block_size[0] = 0x00;
2186        vub300->cmnd.head.block_size[1] = 0x00;
2187        vub300->app_spec = 0;
2188        mutex_init(&vub300->cmd_mutex);
2189        mutex_init(&vub300->irq_mutex);
2190        vub300->command_out_urb = command_out_urb;
2191        vub300->command_res_urb = command_res_urb;
2192        vub300->usb_timed_out = 0;
2193        vub300->dynamic_register_count = 0;
2194
2195        for (i = 0; i < ARRAY_SIZE(vub300->fn); i++) {
2196                vub300->fn[i].offload_point = 0;
2197                vub300->fn[i].offload_count = 0;
2198        }
2199
2200        vub300->total_offload_count = 0;
2201        vub300->irq_enabled = 0;
2202        vub300->irq_disabled = 0;
2203        vub300->irqs_queued = 0;
2204
2205        for (i = 0; i < ARRAY_SIZE(vub300->sdio_register); i++)
2206                vub300->sdio_register[i++].activate = 0;
2207
2208        vub300->udev = udev;
2209        vub300->interface = interface;
2210        vub300->cmnd_res_ep = 0;
2211        vub300->cmnd_out_ep = 0;
2212        vub300->data_inp_ep = 0;
2213        vub300->data_out_ep = 0;
2214
2215        for (i = 0; i < ARRAY_SIZE(vub300->fbs); i++)
2216                vub300->fbs[i] = 512;
2217
2218        /*
2219         *      set up the endpoint information
2220         *
2221         * use the first pair of bulk-in and bulk-out
2222         *     endpoints for Command/Response+Interrupt
2223         *
2224         * use the second pair of bulk-in and bulk-out
2225         *     endpoints for Data In/Out
2226         */
2227        vub300->large_usb_packets = 0;
2228        iface_desc = interface->cur_altsetting;
2229        for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2230                struct usb_endpoint_descriptor *endpoint =
2231                        &iface_desc->endpoint[i].desc;
2232                dev_info(&vub300->udev->dev,
2233                         "vub300 testing %s EndPoint(%d) %02X\n",
2234                         usb_endpoint_is_bulk_in(endpoint) ? "BULK IN" :
2235                         usb_endpoint_is_bulk_out(endpoint) ? "BULK OUT" :
2236                         "UNKNOWN", i, endpoint->bEndpointAddress);
2237                if (endpoint->wMaxPacketSize > 64)
2238                        vub300->large_usb_packets = 1;
2239                if (usb_endpoint_is_bulk_in(endpoint)) {
2240                        if (!vub300->cmnd_res_ep) {
2241                                vub300->cmnd_res_ep =
2242                                        endpoint->bEndpointAddress;
2243                        } else if (!vub300->data_inp_ep) {
2244                                vub300->data_inp_ep =
2245                                        endpoint->bEndpointAddress;
2246                        } else {
2247                                dev_warn(&vub300->udev->dev,
2248                                         "ignoring"
2249                                         " unexpected bulk_in endpoint");
2250                        }
2251                } else if (usb_endpoint_is_bulk_out(endpoint)) {
2252                        if (!vub300->cmnd_out_ep) {
2253                                vub300->cmnd_out_ep =
2254                                        endpoint->bEndpointAddress;
2255                        } else if (!vub300->data_out_ep) {
2256                                vub300->data_out_ep =
2257                                        endpoint->bEndpointAddress;
2258                        } else {
2259                                dev_warn(&vub300->udev->dev,
2260                                         "ignoring"
2261                                         " unexpected bulk_out endpoint");
2262                        }
2263                } else {
2264                        dev_warn(&vub300->udev->dev,
2265                                 "vub300 ignoring EndPoint(%d) %02X", i,
2266                                 endpoint->bEndpointAddress);
2267                }
2268        }
2269        if (vub300->cmnd_res_ep && vub300->cmnd_out_ep &&
2270            vub300->data_inp_ep && vub300->data_out_ep) {
2271                dev_info(&vub300->udev->dev,
2272                         "vub300 %s packets"
2273                         " using EndPoints %02X %02X %02X %02X\n",
2274                         vub300->large_usb_packets ? "LARGE" : "SMALL",
2275                         vub300->cmnd_out_ep, vub300->cmnd_res_ep,
2276                         vub300->data_out_ep, vub300->data_inp_ep);
2277                /* we have the expected EndPoints */
2278        } else {
2279                dev_err(&vub300->udev->dev,
2280                    "Could not find two sets of bulk-in/out endpoint pairs\n");
2281                retval = -EINVAL;
2282                goto error5;
2283        }
2284        retval =
2285                usb_control_msg(vub300->udev, usb_rcvctrlpipe(vub300->udev, 0),
2286                                GET_HC_INF0,
2287                                USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2288                                0x0000, 0x0000, &vub300->hc_info,
2289                                sizeof(vub300->hc_info), HZ);
2290        if (retval < 0)
2291                goto error5;
2292        retval =
2293                usb_control_msg(vub300->udev, usb_rcvctrlpipe(vub300->udev, 0),
2294                                SET_ROM_WAIT_STATES,
2295                                USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2296                                firmware_rom_wait_states, 0x0000, NULL, 0, HZ);
2297        if (retval < 0)
2298                goto error5;
2299        dev_info(&vub300->udev->dev,
2300                 "operating_mode = %s %s %d MHz %s %d byte USB packets\n",
2301                 (mmc->caps & MMC_CAP_SDIO_IRQ) ? "IRQs" : "POLL",
2302                 (mmc->caps & MMC_CAP_4_BIT_DATA) ? "4-bit" : "1-bit",
2303                 mmc->f_max / 1000000,
2304                 pad_input_to_usb_pkt ? "padding input data to" : "with",
2305                 vub300->large_usb_packets ? 512 : 64);
2306        retval =
2307                usb_control_msg(vub300->udev, usb_rcvctrlpipe(vub300->udev, 0),
2308                                GET_SYSTEM_PORT_STATUS,
2309                                USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2310                                0x0000, 0x0000, &vub300->system_port_status,
2311                                sizeof(vub300->system_port_status), HZ);
2312        if (retval < 0) {
2313                goto error4;
2314        } else if (sizeof(vub300->system_port_status) == retval) {
2315                vub300->card_present =
2316                        (0x0001 & vub300->system_port_status.port_flags) ? 1 : 0;
2317                vub300->read_only =
2318                        (0x0010 & vub300->system_port_status.port_flags) ? 1 : 0;
2319        } else {
2320                goto error4;
2321        }
2322        usb_set_intfdata(interface, vub300);
2323        INIT_DELAYED_WORK(&vub300->pollwork, vub300_pollwork_thread);
2324        INIT_WORK(&vub300->cmndwork, vub300_cmndwork_thread);
2325        INIT_WORK(&vub300->deadwork, vub300_deadwork_thread);
2326        kref_init(&vub300->kref);
2327        init_timer(&vub300->sg_transfer_timer);
2328        vub300->sg_transfer_timer.data = (unsigned long)vub300;
2329        vub300->sg_transfer_timer.function = vub300_sg_timed_out;
2330        kref_get(&vub300->kref);
2331        init_timer(&vub300->inactivity_timer);
2332        vub300->inactivity_timer.data = (unsigned long)vub300;
2333        vub300->inactivity_timer.function = vub300_inactivity_timer_expired;
2334        vub300->inactivity_timer.expires = jiffies + HZ;
2335        add_timer(&vub300->inactivity_timer);
2336        if (vub300->card_present)
2337                dev_info(&vub300->udev->dev,
2338                         "USB vub300 remote SDIO host controller[%d]"
2339                         "connected with SD/SDIO card inserted\n",
2340                         interface_to_InterfaceNumber(interface));
2341        else
2342                dev_info(&vub300->udev->dev,
2343                         "USB vub300 remote SDIO host controller[%d]"
2344                         "connected with no SD/SDIO card inserted\n",
2345                         interface_to_InterfaceNumber(interface));
2346        mmc_add_host(mmc);
2347        return 0;
2348error5:
2349        mmc_free_host(mmc);
2350        /*
2351         * and hence also frees vub300
2352         * which is contained at the end of struct mmc
2353         */
2354error4:
2355        usb_free_urb(command_res_urb);
2356error1:
2357        usb_free_urb(command_out_urb);
2358error0:
2359        usb_put_dev(udev);
2360        return retval;
2361}
2362
2363static void vub300_disconnect(struct usb_interface *interface)
2364{                               /* NOT irq */
2365        struct vub300_mmc_host *vub300 = usb_get_intfdata(interface);
2366        if (!vub300 || !vub300->mmc) {
2367                return;
2368        } else {
2369                struct mmc_host *mmc = vub300->mmc;
2370                if (!vub300->mmc) {
2371                        return;
2372                } else {
2373                        int ifnum = interface_to_InterfaceNumber(interface);
2374                        usb_set_intfdata(interface, NULL);
2375                        /* prevent more I/O from starting */
2376                        vub300->interface = NULL;
2377                        kref_put(&vub300->kref, vub300_delete);
2378                        mmc_remove_host(mmc);
2379                        pr_info("USB vub300 remote SDIO host controller[%d]"
2380                                " now disconnected", ifnum);
2381                        return;
2382                }
2383        }
2384}
2385
2386#ifdef CONFIG_PM
2387static int vub300_suspend(struct usb_interface *intf, pm_message_t message)
2388{
2389        return 0;
2390}
2391
2392static int vub300_resume(struct usb_interface *intf)
2393{
2394        return 0;
2395}
2396#else
2397#define vub300_suspend NULL
2398#define vub300_resume NULL
2399#endif
2400static int vub300_pre_reset(struct usb_interface *intf)
2401{                               /* NOT irq */
2402        struct vub300_mmc_host *vub300 = usb_get_intfdata(intf);
2403        mutex_lock(&vub300->cmd_mutex);
2404        return 0;
2405}
2406
2407static int vub300_post_reset(struct usb_interface *intf)
2408{                               /* NOT irq */
2409        struct vub300_mmc_host *vub300 = usb_get_intfdata(intf);
2410        /* we are sure no URBs are active - no locking needed */
2411        vub300->errors = -EPIPE;
2412        mutex_unlock(&vub300->cmd_mutex);
2413        return 0;
2414}
2415
2416static struct usb_driver vub300_driver = {
2417        .name = "vub300",
2418        .probe = vub300_probe,
2419        .disconnect = vub300_disconnect,
2420        .suspend = vub300_suspend,
2421        .resume = vub300_resume,
2422        .pre_reset = vub300_pre_reset,
2423        .post_reset = vub300_post_reset,
2424        .id_table = vub300_table,
2425        .supports_autosuspend = 1,
2426};
2427
2428static int __init vub300_init(void)
2429{                               /* NOT irq */
2430        int result;
2431
2432        pr_info("VUB300 Driver rom wait states = %02X irqpoll timeout = %04X",
2433                firmware_rom_wait_states, 0x0FFFF & firmware_irqpoll_timeout);
2434        cmndworkqueue = create_singlethread_workqueue("kvub300c");
2435        if (!cmndworkqueue) {
2436                pr_err("not enough memory for the REQUEST workqueue");
2437                result = -ENOMEM;
2438                goto out1;
2439        }
2440        pollworkqueue = create_singlethread_workqueue("kvub300p");
2441        if (!pollworkqueue) {
2442                pr_err("not enough memory for the IRQPOLL workqueue");
2443                result = -ENOMEM;
2444                goto out2;
2445        }
2446        deadworkqueue = create_singlethread_workqueue("kvub300d");
2447        if (!deadworkqueue) {
2448                pr_err("not enough memory for the EXPIRED workqueue");
2449                result = -ENOMEM;
2450                goto out3;
2451        }
2452        result = usb_register(&vub300_driver);
2453        if (result) {
2454                pr_err("usb_register failed. Error number %d", result);
2455                goto out4;
2456        }
2457        return 0;
2458out4:
2459        destroy_workqueue(deadworkqueue);
2460out3:
2461        destroy_workqueue(pollworkqueue);
2462out2:
2463        destroy_workqueue(cmndworkqueue);
2464out1:
2465        return result;
2466}
2467
2468static void __exit vub300_exit(void)
2469{
2470        usb_deregister(&vub300_driver);
2471        flush_workqueue(cmndworkqueue);
2472        flush_workqueue(pollworkqueue);
2473        flush_workqueue(deadworkqueue);
2474        destroy_workqueue(cmndworkqueue);
2475        destroy_workqueue(pollworkqueue);
2476        destroy_workqueue(deadworkqueue);
2477}
2478
2479module_init(vub300_init);
2480module_exit(vub300_exit);
2481
2482MODULE_AUTHOR("Tony Olech <tony.olech@elandigitalsystems.com>");
2483MODULE_DESCRIPTION("VUB300 USB to SD/MMC/SDIO adapter driver");
2484MODULE_LICENSE("GPL");
2485