1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Driver for USB Mass Storage compliant devices 4 * 5 * Current development and maintenance by: 6 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net) 7 * 8 * Developed with the assistance of: 9 * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org) 10 * (c) 2002 Alan Stern (stern@rowland.org) 11 * 12 * Initial work by: 13 * (c) 1999 Michael Gee (michael@linuxspecific.com) 14 * 15 * This driver is based on the 'USB Mass Storage Class' document. This 16 * describes in detail the protocol used to communicate with such 17 * devices. Clearly, the designers had SCSI and ATAPI commands in 18 * mind when they created this document. The commands are all very 19 * similar to commands in the SCSI-II and ATAPI specifications. 20 * 21 * It is important to note that in a number of cases this class 22 * exhibits class-specific exemptions from the USB specification. 23 * Notably the usage of NAK, STALL and ACK differs from the norm, in 24 * that they are used to communicate wait, failed and OK on commands. 25 * 26 * Also, for certain devices, the interrupt endpoint is used to convey 27 * status of a command. 28 * 29 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more 30 * information about this driver. 31 */ 32 33 #include <linux/highmem.h> 34 #include <linux/export.h> 35 #include <scsi/scsi.h> 36 #include <scsi/scsi_cmnd.h> 37 38 #include "usb.h" 39 #include "protocol.h" 40 #include "debug.h" 41 #include "scsiglue.h" 42 #include "transport.h" 43 44 /*********************************************************************** 45 * Protocol routines 46 ***********************************************************************/ 47 48 void usb_stor_pad12_command(struct scsi_cmnd *srb, struct us_data *us) 49 { 50 /* 51 * Pad the SCSI command with zeros out to 12 bytes. If the 52 * command already is 12 bytes or longer, leave it alone. 53 * 54 * NOTE: This only works because a scsi_cmnd struct field contains 55 * a unsigned char cmnd[16], so we know we have storage available 56 */ 57 for (; srb->cmd_len < 12; srb->cmd_len++) 58 srb->cmnd[srb->cmd_len] = 0; 59 60 /* send the command to the transport layer */ 61 usb_stor_invoke_transport(srb, us); 62 } 63 64 void usb_stor_ufi_command(struct scsi_cmnd *srb, struct us_data *us) 65 { 66 /* 67 * fix some commands -- this is a form of mode translation 68 * UFI devices only accept 12 byte long commands 69 * 70 * NOTE: This only works because a scsi_cmnd struct field contains 71 * a unsigned char cmnd[16], so we know we have storage available 72 */ 73 74 /* Pad the ATAPI command with zeros */ 75 for (; srb->cmd_len < 12; srb->cmd_len++) 76 srb->cmnd[srb->cmd_len] = 0; 77 78 /* set command length to 12 bytes (this affects the transport layer) */ 79 srb->cmd_len = 12; 80 81 /* XXX We should be constantly re-evaluating the need for these */ 82 83 /* determine the correct data length for these commands */ 84 switch (srb->cmnd[0]) { 85 86 /* for INQUIRY, UFI devices only ever return 36 bytes */ 87 case INQUIRY: 88 srb->cmnd[4] = 36; 89 break; 90 91 /* again, for MODE_SENSE_10, we get the minimum (8) */ 92 case MODE_SENSE_10: 93 srb->cmnd[7] = 0; 94 srb->cmnd[8] = 8; 95 break; 96 97 /* for REQUEST_SENSE, UFI devices only ever return 18 bytes */ 98 case REQUEST_SENSE: 99 srb->cmnd[4] = 18; 100 break; 101 } /* end switch on cmnd[0] */ 102 103 /* send the command to the transport layer */ 104 usb_stor_invoke_transport(srb, us); 105 } 106 107 void usb_stor_transparent_scsi_command(struct scsi_cmnd *srb, 108 struct us_data *us) 109 { 110 /* send the command to the transport layer */ 111 usb_stor_invoke_transport(srb, us); 112 } 113 EXPORT_SYMBOL_GPL(usb_stor_transparent_scsi_command); 114 115 /*********************************************************************** 116 * Scatter-gather transfer buffer access routines 117 ***********************************************************************/ 118 119 /* 120 * Copy a buffer of length buflen to/from the srb's transfer buffer. 121 * Update the **sgptr and *offset variables so that the next copy will 122 * pick up from where this one left off. 123 */ 124 unsigned int usb_stor_access_xfer_buf(unsigned char *buffer, 125 unsigned int buflen, struct scsi_cmnd *srb, struct scatterlist **sgptr, 126 unsigned int *offset, enum xfer_buf_dir dir) 127 { 128 unsigned int cnt = 0; 129 struct scatterlist *sg = *sgptr; 130 struct sg_mapping_iter miter; 131 unsigned int nents = scsi_sg_count(srb); 132 133 if (sg) 134 nents = sg_nents(sg); 135 else 136 sg = scsi_sglist(srb); 137 138 sg_miter_start(&miter, sg, nents, dir == FROM_XFER_BUF ? 139 SG_MITER_FROM_SG: SG_MITER_TO_SG); 140 141 if (!sg_miter_skip(&miter, *offset)) 142 return cnt; 143 144 while (sg_miter_next(&miter) && cnt < buflen) { 145 unsigned int len = min_t(unsigned int, miter.length, 146 buflen - cnt); 147 148 if (dir == FROM_XFER_BUF) 149 memcpy(buffer + cnt, miter.addr, len); 150 else 151 memcpy(miter.addr, buffer + cnt, len); 152 153 if (*offset + len < miter.piter.sg->length) { 154 *offset += len; 155 *sgptr = miter.piter.sg; 156 } else { 157 *offset = 0; 158 *sgptr = sg_next(miter.piter.sg); 159 } 160 cnt += len; 161 } 162 sg_miter_stop(&miter); 163 164 return cnt; 165 } 166 EXPORT_SYMBOL_GPL(usb_stor_access_xfer_buf); 167 168 /* 169 * Store the contents of buffer into srb's transfer buffer and set the 170 * SCSI residue. 171 */ 172 void usb_stor_set_xfer_buf(unsigned char *buffer, 173 unsigned int buflen, struct scsi_cmnd *srb) 174 { 175 unsigned int offset = 0; 176 struct scatterlist *sg = NULL; 177 178 buflen = min(buflen, scsi_bufflen(srb)); 179 buflen = usb_stor_access_xfer_buf(buffer, buflen, srb, &sg, &offset, 180 TO_XFER_BUF); 181 if (buflen < scsi_bufflen(srb)) 182 scsi_set_resid(srb, scsi_bufflen(srb) - buflen); 183 } 184 EXPORT_SYMBOL_GPL(usb_stor_set_xfer_buf); 185