Changes in uspace/drv/bus/usb/usbmast/mast.c [026793d:5203e256] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/bus/usb/usbmast/mast.c
r026793d r5203e256 42 42 #include <usb/dev/request.h> 43 43 44 bool usb_mast_verbose = false;44 bool usb_mast_verbose = true; 45 45 46 46 #define MASTLOG(format, ...) \ 47 47 do { \ 48 48 if (usb_mast_verbose) { \ 49 usb_log_debug 2("USB cl08: " format, ##__VA_ARGS__); \49 usb_log_debug("USB cl08: " format, ##__VA_ARGS__); \ 50 50 } \ 51 51 } while (false) 52 52 53 /** Send command via bulk-only transport.54 * 55 * @param tag Command block wrapper tag (automatically compared56 * with answer)57 * @param lun LUN58 * @param cmd Command block59 * @param cmd _size Command block size in bytes60 * @param ddir Direction in which data will be transferred61 * @param dbuf Data send/receive buffer62 * @param dbuf_size Size of the data buffer63 * @param xferred_size Number of bytes actually transferred64 * 65 * @return Error code66 */ 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 */ 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) 70 70 { 71 71 int rc; 72 72 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; 75 75 76 76 /* Prepare CBW - command block wrapper */ 77 77 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. */ 82 82 rc = usb_pipe_write(bulk_out_pipe, &cbw, sizeof(cbw)); 83 83 MASTLOG("CBW '%s' sent: %s.\n", … … 88 88 } 89 89 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) { 109 97 return rc; 110 98 } … … 114 102 size_t csw_size; 115 103 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", 117 105 usb_debug_str_buffer((uint8_t *) &csw, csw_size, 0), csw_size, 118 106 str_error(rc)); 119 107 if (rc != EOK) { 120 MASTLOG("rc != EOK\n"); 121 return rc; 122 } 123 108 return rc; 109 } 124 110 if (csw_size != sizeof(csw)) { 125 MASTLOG("csw_size != sizeof(csw)\n");126 111 return ERANGE; 127 112 } 128 113 129 114 if (csw.dCSWTag != tag) { 130 MASTLOG("csw.dCSWTag != tag\n");131 115 return EBADCHECKSUM; 132 116 } … … 136 120 */ 137 121 if (csw.dCSWStatus != 0) { 138 MASTLOG("csw.dCSWStatus != 0\n");139 122 // FIXME: better error code 140 123 // FIXME: distinguish 0x01 and 0x02 … … 143 126 144 127 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) { 147 129 return ERANGE; 148 130 } 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 } 160 137 161 138 return EOK; 162 }163 164 /** Perform data-in command.165 *166 * @param tag Command block wrapper tag (automatically compared with167 * answer)168 * @param lun LUN169 * @param cmd CDB (Command Descriptor)170 * @param cmd_size CDB length in bytes171 * @param dbuf Data receive buffer172 * @param dbuf_size Data receive buffer size in bytes173 * @param proc_size Number of bytes actually processed by device174 *175 * @return Error code176 */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 with187 * answer)188 * @param lun LUN189 * @param cmd CDB (Command Descriptor)190 * @param cmd_size CDB length in bytes191 * @param data Command data192 * @param data_size Size of @a data in bytes193 * @param proc_size Number of bytes actually processed by device194 *195 * @return Error code196 */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);203 139 } 204 140 … … 221 157 * 222 158 * @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 */ 162 void usb_massstor_reset_recovery(usb_device_t *dev, 163 size_t bulk_in_idx, size_t bulk_out_idx) 225 164 { 226 165 /* We would ignore errors here because if this fails … … 228 167 */ 229 168 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); 232 171 } 233 172
Note:
See TracChangeset
for help on using the changeset viewer.