Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/bus/usb/usbmast/mast.c

    r026793d r5203e256  
    4242#include <usb/dev/request.h>
    4343
    44 bool usb_mast_verbose = false;
     44bool usb_mast_verbose = true;
    4545
    4646#define MASTLOG(format, ...) \
    4747        do { \
    4848                if (usb_mast_verbose) { \
    49                         usb_log_debug2("USB cl08: " format, ##__VA_ARGS__); \
     49                        usb_log_debug("USB cl08: " format, ##__VA_ARGS__); \
    5050                } \
    5151        } while (false)
    5252
    53 /** Send command via bulk-only transport.
    54  *
    55  * @param tag           Command block wrapper tag (automatically compared
    56  *                      with answer)
    57  * @param lun           LUN
    58  * @param cmd           Command block
    59  * @param cmd_size      Command block size in bytes
    60  * @param ddir          Direction in which data will be transferred
    61  * @param dbuf          Data send/receive buffer
    62  * @param dbuf_size     Size of the data buffer
    63  * @param xferred_size  Number of bytes actually transferred
    64  *
    65  * @return              Error code
    66  */
    67 static int usb_massstor_cmd(usb_device_t *dev, uint32_t tag, uint8_t lun,
    68     const void *cmd, size_t cmd_size, usb_direction_t ddir, void *dbuf,
    69     size_t dbuf_size, size_t *xferred_size)
     53/** Request data from mass storage device.
     54 *
     55 * @param bulk_in_pipe Bulk in pipe to the device.
     56 * @param bulk_out_pipe Bulk out pipe to the device.
     57 * @param tag Command block wrapper tag (automatically compared with answer).
     58 * @param lun LUN index.
     59 * @param cmd SCSI command buffer (in SCSI endianness).
     60 * @param cmd_size Length of SCSI command @p cmd in bytes.
     61 * @param in_buffer Buffer where to store the answer (CSW is not returned).
     62 * @param in_buffer_size Size of the buffer (size of the request to the device).
     63 * @param received_size Number of actually received bytes.
     64 * @return Error code.
     65 */
     66int usb_massstor_data_in(usb_device_t *dev,
     67    size_t bulk_in_pipe_index, size_t bulk_out_pipe_index,
     68    uint32_t tag, uint8_t lun, void *cmd, size_t cmd_size,
     69    void *in_buffer, size_t in_buffer_size, size_t *received_size)
    7070{
    7171        int rc;
    7272        size_t act_size;
    73         usb_pipe_t *bulk_in_pipe = dev->pipes[BULK_IN_EP].pipe;
    74         usb_pipe_t *bulk_out_pipe = dev->pipes[BULK_OUT_EP].pipe;
     73        usb_pipe_t *bulk_in_pipe = dev->pipes[bulk_in_pipe_index].pipe;
     74        usb_pipe_t *bulk_out_pipe = dev->pipes[bulk_out_pipe_index].pipe;
    7575
    7676        /* Prepare CBW - command block wrapper */
    7777        usb_massstor_cbw_t cbw;
    78         usb_massstor_cbw_prepare(&cbw, tag, dbuf_size, ddir, lun, cmd_size,
    79             cmd);
    80 
    81         /* Send the CBW. */
     78        usb_massstor_cbw_prepare(&cbw, tag, in_buffer_size,
     79            USB_DIRECTION_IN, lun, cmd_size, cmd);
     80
     81        /* First, send the CBW. */
    8282        rc = usb_pipe_write(bulk_out_pipe, &cbw, sizeof(cbw));
    8383        MASTLOG("CBW '%s' sent: %s.\n",
     
    8888        }
    8989
    90         if (ddir == USB_DIRECTION_IN) {
    91                 /* Recieve data from the device. */
    92                 rc = usb_pipe_read(bulk_in_pipe, dbuf, dbuf_size, &act_size);
    93                 MASTLOG("Received %zu bytes (%s): %s.\n", act_size,
    94                     usb_debug_str_buffer((uint8_t *) dbuf, act_size, 0),
    95                     str_error(rc));
    96         } else {
    97                 /* Send data to the device. */
    98                 rc = usb_pipe_write(bulk_out_pipe, dbuf, dbuf_size);
    99                 MASTLOG("Sent %zu bytes (%s): %s.\n", act_size,
    100                     usb_debug_str_buffer((uint8_t *) dbuf, act_size, 0),
    101                     str_error(rc));
    102         }
    103 
    104         if (rc != EOK) {
    105                 /*
    106                  * XXX If the pipe is stalled, we should clear it
    107                  * and read CSW.
    108                  */
     90        /* Try to retrieve the data from the device. */
     91        act_size = 0;
     92        rc = usb_pipe_read(bulk_in_pipe, in_buffer, in_buffer_size, &act_size);
     93        MASTLOG("Received %zuB (%s): %s.\n", act_size,
     94            usb_debug_str_buffer((uint8_t *) in_buffer, act_size, 0),
     95            str_error(rc));
     96        if (rc != EOK) {
    10997                return rc;
    11098        }
     
    114102        size_t csw_size;
    115103        rc = usb_pipe_read(bulk_in_pipe, &csw, sizeof(csw), &csw_size);
    116         MASTLOG("CSW '%s' received (%zu bytes): %s.\n",
     104        MASTLOG("CSW '%s' received (%zuB): %s.\n",
    117105            usb_debug_str_buffer((uint8_t *) &csw, csw_size, 0), csw_size,
    118106            str_error(rc));
    119107        if (rc != EOK) {
    120                 MASTLOG("rc != EOK\n");
    121                 return rc;
    122         }
    123 
     108                return rc;
     109        }
    124110        if (csw_size != sizeof(csw)) {
    125                 MASTLOG("csw_size != sizeof(csw)\n");
    126111                return ERANGE;
    127112        }
    128113
    129114        if (csw.dCSWTag != tag) {
    130                 MASTLOG("csw.dCSWTag != tag\n");
    131115                return EBADCHECKSUM;
    132116        }
     
    136120         */
    137121        if (csw.dCSWStatus != 0) {
    138                 MASTLOG("csw.dCSWStatus != 0\n");
    139122                // FIXME: better error code
    140123                // FIXME: distinguish 0x01 and 0x02
     
    143126
    144127        size_t residue = (size_t) uint32_usb2host(csw.dCSWDataResidue);
    145         if (residue > dbuf_size) {
    146                 MASTLOG("residue > dbuf_size\n");
     128        if (residue > in_buffer_size) {
    147129                return ERANGE;
    148130        }
    149 
    150         /*
    151          * When the device has less data to send than requested (or cannot
    152          * receive moredata), it can either stall the pipe or send garbage
    153          * (ignore data) and indicate that via the residue field in CSW.
    154          * That means dbuf_size - residue is the authoritative size of data
    155          * received (sent).
    156          */
    157 
    158         if (xferred_size != NULL)
    159                 *xferred_size = dbuf_size - residue;
     131        if (act_size != in_buffer_size - residue) {
     132                return ERANGE;
     133        }
     134        if (received_size != NULL) {
     135                *received_size = in_buffer_size - residue;
     136        }
    160137
    161138        return EOK;
    162 }
    163 
    164 /** Perform data-in command.
    165  *
    166  * @param tag           Command block wrapper tag (automatically compared with
    167  *                      answer)
    168  * @param lun           LUN
    169  * @param cmd           CDB (Command Descriptor)
    170  * @param cmd_size      CDB length in bytes
    171  * @param dbuf          Data receive buffer
    172  * @param dbuf_size     Data receive buffer size in bytes
    173  * @param proc_size     Number of bytes actually processed by device
    174  *
    175  * @return Error code
    176  */
    177 int usb_massstor_data_in(usb_device_t *dev, uint32_t tag, uint8_t lun,
    178     const void *cmd, size_t cmd_size, void *dbuf, size_t dbuf_size, size_t *proc_size)
    179 {
    180         return usb_massstor_cmd(dev, tag, lun, cmd, cmd_size, USB_DIRECTION_IN,
    181             dbuf, dbuf_size, proc_size);
    182 }
    183 
    184 /** Perform data-out command.
    185  *
    186  * @param tag           Command block wrapper tag (automatically compared with
    187  *                      answer)
    188  * @param lun           LUN
    189  * @param cmd           CDB (Command Descriptor)
    190  * @param cmd_size      CDB length in bytes
    191  * @param data          Command data
    192  * @param data_size     Size of @a data in bytes
    193  * @param proc_size     Number of bytes actually processed by device
    194  *
    195  * @return Error code
    196  */
    197 int usb_massstor_data_out(usb_device_t *dev, uint32_t tag, uint8_t lun,
    198     const void *cmd, size_t cmd_size, const void *data, size_t data_size,
    199     size_t *proc_size)
    200 {
    201         return usb_massstor_cmd(dev, tag, lun, cmd, cmd_size, USB_DIRECTION_OUT,
    202             (void *) data, data_size, proc_size);
    203139}
    204140
     
    221157 *
    222158 * @param dev Device to be reseted.
    223  */
    224 void usb_massstor_reset_recovery(usb_device_t *dev)
     159 * @param bulk_in_idx Index of bulk in pipe.
     160 * @param bulk_out_idx Index of bulk out pipe.
     161 */
     162void usb_massstor_reset_recovery(usb_device_t *dev,
     163    size_t bulk_in_idx, size_t bulk_out_idx)
    225164{
    226165        /* We would ignore errors here because if this fails
     
    228167         */
    229168        usb_massstor_reset(dev);
    230         usb_pipe_clear_halt(&dev->ctrl_pipe, dev->pipes[BULK_IN_EP].pipe);
    231         usb_pipe_clear_halt(&dev->ctrl_pipe, dev->pipes[BULK_OUT_EP].pipe);
     169        usb_pipe_clear_halt(&dev->ctrl_pipe, dev->pipes[bulk_in_idx].pipe);
     170        usb_pipe_clear_halt(&dev->ctrl_pipe, dev->pipes[bulk_out_idx].pipe);
    232171}
    233172
Note: See TracChangeset for help on using the changeset viewer.