Changeset 1916d1f in mainline for uspace/drv/bus/usb/usbmast/mast.c


Ignore:
Timestamp:
2011-07-12T13:41:26Z (13 years ago)
Author:
Petr Koupy <petr.koupy@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
50fc490
Parents:
11809eab (diff), 6817eba (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge libposix changes.

File:
1 edited

Legend:

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

    r11809eab r1916d1f  
    4242#include <usb/dev/request.h>
    4343
    44 bool usb_mast_verbose = true;
     44bool usb_mast_verbose = false;
    4545
    4646#define MASTLOG(format, ...) \
    4747        do { \
    4848                if (usb_mast_verbose) { \
    49                         usb_log_debug("USB cl08: " format, ##__VA_ARGS__); \
     49                        usb_log_debug2("USB cl08: " format, ##__VA_ARGS__); \
    5050                } \
    5151        } while (false)
    5252
    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  */
    66 int 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)
     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 */
     67static 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)
    7070{
    7171        int rc;
    7272        size_t act_size;
    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;
     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;
    7575
    7676        /* Prepare CBW - command block wrapper */
    7777        usb_massstor_cbw_t 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. */
     78        usb_massstor_cbw_prepare(&cbw, tag, dbuf_size, ddir, lun, cmd_size,
     79            cmd);
     80
     81        /* Send the CBW. */
    8282        rc = usb_pipe_write(bulk_out_pipe, &cbw, sizeof(cbw));
    8383        MASTLOG("CBW '%s' sent: %s.\n",
     
    8888        }
    8989
    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) {
     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                 */
    97109                return rc;
    98110        }
     
    102114        size_t csw_size;
    103115        rc = usb_pipe_read(bulk_in_pipe, &csw, sizeof(csw), &csw_size);
    104         MASTLOG("CSW '%s' received (%zuB): %s.\n",
     116        MASTLOG("CSW '%s' received (%zu bytes): %s.\n",
    105117            usb_debug_str_buffer((uint8_t *) &csw, csw_size, 0), csw_size,
    106118            str_error(rc));
    107119        if (rc != EOK) {
    108                 return rc;
    109         }
     120                MASTLOG("rc != EOK\n");
     121                return rc;
     122        }
     123
    110124        if (csw_size != sizeof(csw)) {
     125                MASTLOG("csw_size != sizeof(csw)\n");
    111126                return ERANGE;
    112127        }
    113128
    114129        if (csw.dCSWTag != tag) {
     130                MASTLOG("csw.dCSWTag != tag\n");
    115131                return EBADCHECKSUM;
    116132        }
     
    120136         */
    121137        if (csw.dCSWStatus != 0) {
     138                MASTLOG("csw.dCSWStatus != 0\n");
    122139                // FIXME: better error code
    123140                // FIXME: distinguish 0x01 and 0x02
     
    126143
    127144        size_t residue = (size_t) uint32_usb2host(csw.dCSWDataResidue);
    128         if (residue > in_buffer_size) {
     145        if (residue > dbuf_size) {
     146                MASTLOG("residue > dbuf_size\n");
    129147                return ERANGE;
    130148        }
    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         }
     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;
    137160
    138161        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 */
     177int 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 */
     197int 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);
    139203}
    140204
     
    157221 *
    158222 * @param dev Device to be reseted.
    159  * @param bulk_in_idx Index of bulk in pipe.
    160  * @param bulk_out_idx Index of bulk out pipe.
    161  */
    162 void usb_massstor_reset_recovery(usb_device_t *dev,
    163     size_t bulk_in_idx, size_t bulk_out_idx)
     223 */
     224void usb_massstor_reset_recovery(usb_device_t *dev)
    164225{
    165226        /* We would ignore errors here because if this fails
     
    167228         */
    168229        usb_massstor_reset(dev);
    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);
     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);
    171232}
    172233
Note: See TracChangeset for help on using the changeset viewer.