CTC++ Coverage Report - Execution Profile    #803/1532

Files Summary | Functions Summary | Execution Profile | Index | No Index
First | Previous | Next | Last


File: drivers/usb/storage/shuttle_usbat.c
Instrumentation mode: function-decision-multicondition
TER: 0 % ( 0/516)

Start/ End/    
True False - Line Source

  1 /* Driver for SCM Microsystems (a.k.a. Shuttle) USB-ATAPI cable
  2  *
  3  * $Id: shuttle_usbat.c,v 1.17 2002/04/22 03:39:43 mdharm Exp $
  4  *
  5  * Current development and maintenance by:
  6  *   (c) 2000, 2001 Robert Baruch (autophile@starband.net)
  7  *   (c) 2004, 2005 Daniel Drake <dsd@gentoo.org>
  8  *
  9  * Developed with the assistance of:
  10  *   (c) 2002 Alan Stern <stern@rowland.org>
  11  *
  12  * Flash support based on earlier work by:
  13  *   (c) 2002 Thomas Kreiling <usbdev@sm04.de>
  14  *
  15  * Many originally ATAPI devices were slightly modified to meet the USB
  16  * market by using some kind of translation from ATAPI to USB on the host,
  17  * and the peripheral would translate from USB back to ATAPI.
  18  *
  19  * SCM Microsystems (www.scmmicro.com) makes a device, sold to OEM's only, 
  20  * which does the USB-to-ATAPI conversion.  By obtaining the data sheet on
  21  * their device under nondisclosure agreement, I have been able to write
  22  * this driver for Linux.
  23  *
  24  * The chip used in the device can also be used for EPP and ISA translation
  25  * as well. This driver is only guaranteed to work with the ATAPI
  26  * translation.
  27  *
  28  * See the Kconfig help text for a list of devices known to be supported by
  29  * this driver.
  30  *
  31  * This program is free software; you can redistribute it and/or modify it
  32  * under the terms of the GNU General Public License as published by the
  33  * Free Software Foundation; either version 2, or (at your option) any
  34  * later version.
  35  *
  36  * This program is distributed in the hope that it will be useful, but
  37  * WITHOUT ANY WARRANTY; without even the implied warranty of
  38  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  39  * General Public License for more details.
  40  *
  41  * You should have received a copy of the GNU General Public License along
  42  * with this program; if not, write to the Free Software Foundation, Inc.,
  43  * 675 Mass Ave, Cambridge, MA 02139, USA.
  44  */
  45 
  46 #include <linux/sched.h>
  47 #include <linux/errno.h>
  48 #include <linux/slab.h>
  49 #include <linux/cdrom.h>
  50 
  51 #include <scsi/scsi.h>
  52 #include <scsi/scsi_cmnd.h>
  53 
  54 #include "usb.h"
  55 #include "transport.h"
  56 #include "protocol.h"
  57 #include "debug.h"
  58 #include "shuttle_usbat.h"
  59 
  60 #define short_pack(LSB,MSB) ( ((u16)(LSB)) | ( ((u16)(MSB))<<8 ) )
  61 #define LSB_of(s) ((s)&0xFF)
  62 #define MSB_of(s) ((s)>>8)
  63 
  64 static int transferred = 0;
  65 
  66 static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us);
  67 static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us);
  68 
  69 /*
  70  * Convenience function to produce an ATA read/write sectors command
  71  * Use cmd=0x20 for read, cmd=0x30 for write
  72  */
 
- 73 static void usbat_pack_ata_sector_cmd(unsigned char *buf,
  74                unsigned char thistime,
  75                u32 sector, unsigned char cmd)
  76 {
  77    buf[0] = 0;
  78    buf[1] = thistime;
  79    buf[2] = sector & 0xFF;
  80    buf[3] = (sector >>  8) & 0xFF;
  81    buf[4] = (sector >> 16) & 0xFF;
  82    buf[5] = 0xE0 | ((sector >> 24) & 0x0F);
  83    buf[6] = cmd;
  84 }
  85 
  86 /*
  87  * Convenience function to get the device type (flash or hp8200)
  88  */
 
- 89 static int usbat_get_device_type(struct us_data *us)
  90 {
 - 91    return ((struct usbat_info*)us->extra)->devicetype;
  92 }
  93 
  94 /*
  95  * Read a register from the device
  96  */
 
- 97 static int usbat_read(struct us_data *us,
  98             unsigned char access,
  99             unsigned char reg,
  100             unsigned char *content)
  101 {
  102    return usb_stor_ctrl_transfer(us,
  103       us->recv_ctrl_pipe,
  104       access | USBAT_CMD_READ_REG,
  105       0xC0,
  106       (u16)reg,
  107       0,
  108       content,
 - 109       1);
  110 }
  111 
  112 /*
  113  * Write to a register on the device
  114  */
 
- 115 static int usbat_write(struct us_data *us,
  116              unsigned char access,
  117              unsigned char reg,
  118              unsigned char content)
  119 {
  120    return usb_stor_ctrl_transfer(us,
  121       us->send_ctrl_pipe,
  122       access | USBAT_CMD_WRITE_REG,
  123       0x40,
  124       short_pack(reg, content),
  125       0,
  126       NULL,
 - 127       0);
  128 }
  129 
  130 /*
  131  * Convenience function to perform a bulk read
  132  */
 
- 133 static int usbat_bulk_read(struct us_data *us,
  134                       unsigned char *data,
  135                       unsigned int len)
  136 {
- 137    if (len == 0)
 - 138       return USB_STOR_XFER_GOOD;
  139 
  140    US_DEBUGP("usbat_bulk_read: len = %d\n", len);
 - 141    return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, data, len, NULL);
  142 }
  143 
  144 /*
  145  * Convenience function to perform a bulk write
  146  */
 
- 147 static int usbat_bulk_write(struct us_data *us,
  148                      unsigned char *data,
  149                      unsigned int len)
  150 {
- 151    if (len == 0)
 - 152       return USB_STOR_XFER_GOOD;
  153 
  154    US_DEBUGP("usbat_bulk_write:  len = %d\n", len);
 - 155    return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, data, len, NULL);
  156 }
  157 
  158 /*
  159  * Some USBAT-specific commands can only be executed over a command transport
  160  * This transport allows one (len=8) or two (len=16) vendor-specific commands
  161  * to be executed.
  162  */
 
- 163 static int usbat_execute_command(struct us_data *us,
  164                          unsigned char *commands,
  165                          unsigned int len)
  166 {
  167    return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
  168                           USBAT_CMD_EXEC_CMD, 0x40, 0, 0,
 - 169                           commands, len);
  170 }
  171 
  172 /*
  173  * Read the status register
  174  */
 
- 175 static int usbat_get_status(struct us_data *us, unsigned char *status)
  176 {
  177    int rc;
  178    rc = usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status);
  179 
  180    US_DEBUGP("usbat_get_status: 0x%02X\n", (unsigned short) (*status));
 - 181    return rc;
  182 }
  183 
  184 /*
  185  * Check the device status
  186  */
 
- 187 static int usbat_check_status(struct us_data *us)
  188 {
  189    unsigned char *reply = us->iobuf;
  190    int rc;
  191 
- 192    if (!us)
 - 193       return USB_STOR_TRANSPORT_ERROR;
  194 
  195    rc = usbat_get_status(us, reply);
- 196    if (rc != USB_STOR_XFER_GOOD)
 - 197       return USB_STOR_TRANSPORT_FAILED;
  198 
  199    /* error/check condition (0x51 is ok) */
- 200    if (*reply & 0x01 && *reply != 0x51)
 - 200   T && T
 - 200   T && F
 - 200   F && _
 - 201       return USB_STOR_TRANSPORT_FAILED;
  202 
  203    /* device fault */
- 204    if (*reply & 0x20)
 - 205       return USB_STOR_TRANSPORT_FAILED;
  206 
 - 207    return USB_STOR_TRANSPORT_GOOD;
  208 }
  209 
  210 /*
  211  * Stores critical information in internal registers in prepartion for the execution
  212  * of a conditional usbat_read_blocks or usbat_write_blocks call.
  213  */
 
- 214 static int usbat_set_shuttle_features(struct us_data *us,
  215                   unsigned char external_trigger,
  216                   unsigned char epp_control,
  217                   unsigned char mask_byte,
  218                   unsigned char test_pattern,
  219                   unsigned char subcountH,
  220                   unsigned char subcountL)
  221 {
  222    unsigned char *command = us->iobuf;
  223 
  224    command[0] = 0x40;
  225    command[1] = USBAT_CMD_SET_FEAT;
  226 
  227    /*
  228     * The only bit relevant to ATA access is bit 6
  229     * which defines 8 bit data access (set) or 16 bit (unset)
  230     */
  231    command[2] = epp_control;
  232 
  233    /*
  234     * If FCQ is set in the qualifier (defined in R/W cmd), then bits U0, U1,
  235     * ET1 and ET2 define an external event to be checked for on event of a
  236     * _read_blocks or _write_blocks operation. The read/write will not take
  237     * place unless the defined trigger signal is active.
  238     */
  239    command[3] = external_trigger;
  240 
  241    /*
  242     * The resultant byte of the mask operation (see mask_byte) is compared for
  243     * equivalence with this test pattern. If equal, the read/write will take
  244     * place.
  245     */
  246    command[4] = test_pattern;
  247 
  248    /*
  249     * This value is logically ANDed with the status register field specified
  250     * in the read/write command.
  251     */
  252    command[5] = mask_byte;
  253 
  254    /*
  255     * If ALQ is set in the qualifier, this field contains the address of the
  256     * registers where the byte count should be read for transferring the data.
  257     * If ALQ is not set, then this field contains the number of bytes to be
  258     * transferred.
  259     */
  260    command[6] = subcountL;
  261    command[7] = subcountH;
  262 
 - 263    return usbat_execute_command(us, command, 8);
  264 }
  265 
  266 /*
  267  * Block, waiting for an ATA device to become not busy or to report
  268  * an error condition.
  269  */
 
- 270 static int usbat_wait_not_busy(struct us_data *us, int minutes)
  271 {
  272    int i;
  273    int result;
  274    unsigned char *status = us->iobuf;
  275 
  276    /* Synchronizing cache on a CDR could take a heck of a long time,
  277     * but probably not more than 10 minutes or so. On the other hand,
  278     * doing a full blank on a CDRW at speed 1 will take about 75
  279     * minutes!
  280     */
  281 
- 282    for (i=0; i<1200+minutes*60; i++) {
  283 
  284        result = usbat_get_status(us, status);
  285 
- 286       if (result!=USB_STOR_XFER_GOOD)
 - 287          return USB_STOR_TRANSPORT_ERROR;
- 288       if (*status & 0x01) { /* check condition */
  289          result = usbat_read(us, USBAT_ATA, 0x10, status);
 - 290          return USB_STOR_TRANSPORT_FAILED;
  291       }
- 292       if (*status & 0x20) /* device fault */
 - 293          return USB_STOR_TRANSPORT_FAILED;
  294 
- 295       if ((*status & 0x80)==0x00) { /* not busy */
  296          US_DEBUGP("Waited not busy for %d steps\n", i);
 - 297          return USB_STOR_TRANSPORT_GOOD;
  298       }
  299 
- 300       if (i<500)
  301          msleep(10); /* 5 seconds */
- 302       else if (i<700)
  303          msleep(50); /* 10 seconds */
- 304       else if (i<1200)
  305          msleep(100); /* 50 seconds */
    306       else
  307          msleep(1000); /* X minutes */
  308    }
  309 
  310    US_DEBUGP("Waited not busy for %d minutes, timing out.\n",
  311       minutes);
 - 312    return USB_STOR_TRANSPORT_FAILED;
  313 }
  314 
  315 /*
  316  * Read block data from the data register
  317  */
 
- 318 static int usbat_read_block(struct us_data *us,
  319              unsigned char *content,
  320              unsigned short len)
  321 {
  322    int result;
  323    unsigned char *command = us->iobuf;
  324 
- 325    if (!len)
 - 326       return USB_STOR_TRANSPORT_GOOD;
  327 
  328    command[0] = 0xC0;
  329    command[1] = USBAT_ATA | USBAT_CMD_READ_BLOCK;
  330    command[2] = USBAT_ATA_DATA;
  331    command[3] = 0;
  332    command[4] = 0;
  333    command[5] = 0;
  334    command[6] = LSB_of(len);
  335    command[7] = MSB_of(len);
  336 
  337    result = usbat_execute_command(us, command, 8);
- 338    if (result != USB_STOR_XFER_GOOD)
 - 339       return USB_STOR_TRANSPORT_ERROR;
  340 
  341    result = usbat_bulk_read(us, content, len);
    342    return (result == USB_STOR_XFER_GOOD ?
- 342 ternary-?: result == 0
 - 343          USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR);
  344 }
  345 
  346 /*
  347  * Write block data via the data register
  348  */
 
- 349 static int usbat_write_block(struct us_data *us,
  350               unsigned char access,
  351               unsigned char *content,
  352               unsigned short len,
  353               int minutes)
  354 {
  355    int result;
  356    unsigned char *command = us->iobuf;
  357 
- 358    if (!len)
 - 359       return USB_STOR_TRANSPORT_GOOD;
  360 
  361    command[0] = 0x40;
  362    command[1] = access | USBAT_CMD_WRITE_BLOCK;
  363    command[2] = USBAT_ATA_DATA;
  364    command[3] = 0;
  365    command[4] = 0;
  366    command[5] = 0;
  367    command[6] = LSB_of(len);
  368    command[7] = MSB_of(len);
  369 
  370    result = usbat_execute_command(us, command, 8);
  371 
- 372    if (result != USB_STOR_XFER_GOOD)
 - 373       return USB_STOR_TRANSPORT_ERROR;
  374 
  375    result = usbat_bulk_write(us, content, len);
- 376    if (result != USB_STOR_XFER_GOOD)
 - 377       return USB_STOR_TRANSPORT_ERROR;
  378 
 - 379    return usbat_wait_not_busy(us, minutes);
  380 }
  381 
  382 /*
  383  * Process read and write requests
  384  */
 
- 385 static int usbat_hp8200e_rw_block_test(struct us_data *us,
  386                    unsigned char access,
  387                    unsigned char *registers,
  388                    unsigned char *data_out,
  389                    unsigned short num_registers,
  390                    unsigned char data_reg,
  391                    unsigned char status_reg,
  392                    unsigned char timeout,
  393                    unsigned char qualifier,
  394                    int direction,
  395                    unsigned char *content,
  396                    unsigned short len,
  397                    int use_sg,
  398                    int minutes)
  399 {
  400    int result;
    401    unsigned int pipe = (direction == DMA_FROM_DEVICE) ?
- 401 ternary-?: ( direction == DMA_FROM_DEVICE )
  402          us->recv_bulk_pipe : us->send_bulk_pipe;
  403 
  404    unsigned char *command = us->iobuf;
  405    int i, j;
  406    int cmdlen;
  407    unsigned char *data = us->iobuf;
  408    unsigned char *status = us->iobuf;
  409 
    410    BUG_ON(num_registers > US_IOBUF_SIZE/2);
- 410   if (__builtin_expect ( ! ! ( ( num_registers..
- 410 do-while (0)
  411 
- 412    for (i=0; i<20; i++) {
  413 
  414       /*
  415        * The first time we send the full command, which consists
  416        * of downloading the SCSI command followed by downloading
  417        * the data via a write-and-test.  Any other time we only
  418        * send the command to download the data -- the SCSI command
  419        * is still 'active' in some sense in the device.
  420        * 
  421        * We're only going to try sending the data 10 times. After
  422        * that, we just return a failure.
  423        */
  424 
- 425       if (i==0) {
  426          cmdlen = 16;
  427          /*
  428           * Write to multiple registers
  429           * Not really sure the 0x07, 0x17, 0xfc, 0xe7 is
  430           * necessary here, but that's what came out of the
  431           * trace every single time.
  432           */
  433          command[0] = 0x40;
  434          command[1] = access | USBAT_CMD_WRITE_REGS;
  435          command[2] = 0x07;
  436          command[3] = 0x17;
  437          command[4] = 0xFC;
  438          command[5] = 0xE7;
  439          command[6] = LSB_of(num_registers*2);
  440          command[7] = MSB_of(num_registers*2);
    441       } else
  442          cmdlen = 8;
  443 
  444       /* Conditionally read or write blocks */
    445       command[cmdlen-8] = (direction==DMA_TO_DEVICE ? 0x40 : 0xC0);
- 445   ternary-?: direction == DMA_TO_DEVICE
  446       command[cmdlen-7] = access |
    447             (direction==DMA_TO_DEVICE ?
- 447   ternary-?: direction == DMA_TO_DEVICE
  448              USBAT_CMD_COND_WRITE_BLOCK : USBAT_CMD_COND_READ_BLOCK);
  449       command[cmdlen-6] = data_reg;
  450       command[cmdlen-5] = status_reg;
  451       command[cmdlen-4] = timeout;
  452       command[cmdlen-3] = qualifier;
  453       command[cmdlen-2] = LSB_of(len);
  454       command[cmdlen-1] = MSB_of(len);
  455 
  456       result = usbat_execute_command(us, command, cmdlen);
  457 
- 458       if (result != USB_STOR_XFER_GOOD)
 - 459          return USB_STOR_TRANSPORT_ERROR;
  460 
- 461       if (i==0) {
  462 
- 463          for (j=0; j<num_registers; j++) {
  464             data[j<<1] = registers[j];
  465             data[1+(j<<1)] = data_out[j];
  466          }
  467 
  468          result = usbat_bulk_write(us, data, num_registers*2);
- 469          if (result != USB_STOR_XFER_GOOD)
 - 470             return USB_STOR_TRANSPORT_ERROR;
  471 
  472       }
  473 
  474       result = usb_stor_bulk_transfer_sg(us,
  475          pipe, content, len, use_sg, NULL);
  476 
  477       /*
  478        * If we get a stall on the bulk download, we'll retry
  479        * the bulk download -- but not the SCSI command because
  480        * in some sense the SCSI command is still 'active' and
  481        * waiting for the data. Don't ask me why this should be;
  482        * I'm only following what the Windoze driver did.
  483        *
  484        * Note that a stall for the test-and-read/write command means
  485        * that the test failed. In this case we're testing to make
  486        * sure that the device is error-free
  487        * (i.e. bit 0 -- CHK -- of status is 0). The most likely
  488        * hypothesis is that the USBAT chip somehow knows what
  489        * the device will accept, but doesn't give the device any
  490        * data until all data is received. Thus, the device would
  491        * still be waiting for the first byte of data if a stall
  492        * occurs, even if the stall implies that some data was
  493        * transferred.
  494        */
  495 
  496       if (result == USB_STOR_XFER_SHORT ||
- 497             result == USB_STOR_XFER_STALLED) {
 - 497     T || _
 - 497     F || T
 - 497     F || F
  498 
  499          /*
  500           * If we're reading and we stalled, then clear
  501           * the bulk output pipe only the first time.
  502           */
  503 
- 504          if (direction==DMA_FROM_DEVICE && i==0) {
 - 504       T && T
 - 504       T && F
 - 504       F && _
  505             if (usb_stor_clear_halt(us,
- 506                   us->send_bulk_pipe) < 0)
 - 507                return USB_STOR_TRANSPORT_ERROR;
  508          }
  509 
  510          /*
  511           * Read status: is the device angry, or just busy?
  512           */
  513 
  514           result = usbat_read(us, USBAT_ATA, 
    515             direction==DMA_TO_DEVICE ?
- 515     ternary-?: direction == DMA_TO_DEVICE
  516                USBAT_ATA_STATUS : USBAT_ATA_ALTSTATUS,
  517             status);
  518 
- 519          if (result!=USB_STOR_XFER_GOOD)
 - 520             return USB_STOR_TRANSPORT_ERROR;
0&nbs