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 * This program is free software; you can redistribute it and/or modify it 33 * under the terms of the GNU General Public License as published by the 34 * Free Software Foundation; either version 2, or (at your option) any 35 * later version. 36 * 37 * This program is distributed in the hope that it will be useful, but 38 * WITHOUT ANY WARRANTY; without even the implied warranty of 39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 40 * General Public License for more details. 41 * 42 * You should have received a copy of the GNU General Public License along 43 * with this program; if not, write to the Free Software Foundation, Inc., 44 * 675 Mass Ave, Cambridge, MA 02139, USA. 45 */ 46 47 #include <linux/highmem.h> 48 #include <linux/export.h> 49 #include <scsi/scsi.h> 50 #include <scsi/scsi_cmnd.h> 51 52 #include "usb.h" 53 #include "protocol.h" 54 #include "debug.h" 55 #include "scsiglue.h" 56 #include "transport.h" 57 58 /*********************************************************************** 59 * Protocol routines 60 ***********************************************************************/ 61 62 void usb_stor_pad12_command(struct scsi_cmnd *srb, struct us_data *us) 63 { 64 /* 65 * Pad the SCSI command with zeros out to 12 bytes. If the 66 * command already is 12 bytes or longer, leave it alone. 67 * 68 * NOTE: This only works because a scsi_cmnd struct field contains 69 * a unsigned char cmnd[16], so we know we have storage available 70 */ 71 for (; srb->cmd_len < 12; srb->cmd_len++) 72 srb->cmnd[srb->cmd_len] = 0; 73 74 /* send the command to the transport layer */ 75 usb_stor_invoke_transport(srb, us); 76 } 77 78 void usb_stor_ufi_command(struct scsi_cmnd *srb, struct us_data *us) 79 { 80 /* 81 * fix some commands -- this is a form of mode translation 82 * UFI devices only accept 12 byte long commands 83 * 84 * NOTE: This only works because a scsi_cmnd struct field contains 85 * a unsigned char cmnd[16], so we know we have storage available 86 */ 87 88 /* Pad the ATAPI command with zeros */ 89 for (; srb->cmd_len < 12; srb->cmd_len++) 90 srb->cmnd[srb->cmd_len] = 0; 91 92 /* set command length to 12 bytes (this affects the transport layer) */ 93 srb->cmd_len = 12; 94 95 /* XXX We should be constantly re-evaluating the need for these */ 96 97 /* determine the correct data length for these commands */ 98 switch (srb->cmnd[0]) { 99 100 /* for INQUIRY, UFI devices only ever return 36 bytes */ 101 case INQUIRY: 102 srb->cmnd[4] = 36; 103 break; 104 105 /* again, for MODE_SENSE_10, we get the minimum (8) */ 106 case MODE_SENSE_10: 107 srb->cmnd[7] = 0; 108 srb->cmnd[8] = 8; 109 break; 110 111 /* for REQUEST_SENSE, UFI devices only ever return 18 bytes */ 112 case REQUEST_SENSE: 113 srb->cmnd[4] = 18; 114 break; 115 } /* end switch on cmnd[0] */ 116 117 /* send the command to the transport layer */ 118 usb_stor_invoke_transport(srb, us); 119 } 120 121 void usb_stor_transparent_scsi_command(struct scsi_cmnd *srb, 122 struct us_data *us) 123 { 124 /* send the command to the transport layer */ 125 usb_stor_invoke_transport(srb, us); 126 } 127 EXPORT_SYMBOL_GPL(usb_stor_transparent_scsi_command); 128 129 /*********************************************************************** 130 * Scatter-gather transfer buffer access routines 131 ***********************************************************************/ 132 133 /* 134 * Copy a buffer of length buflen to/from the srb's transfer buffer. 135 * Update the **sgptr and *offset variables so that the next copy will 136 * pick up from where this one left off. 137 */ 138 unsigned int usb_stor_access_xfer_buf(unsigned char *buffer, 139 unsigned int buflen, struct scsi_cmnd *srb, struct scatterlist **sgptr, 140 unsigned int *offset, enum xfer_buf_dir dir) 141 { 142 unsigned int cnt = 0; 143 struct scatterlist *sg = *sgptr; 144 struct sg_mapping_iter miter; 145 unsigned int nents = scsi_sg_count(srb); 146 147 if (sg) 148 nents = sg_nents(sg); 149 else 150 sg = scsi_sglist(srb); 151 152 sg_miter_start(&miter, sg, nents, dir == FROM_XFER_BUF ? 153 SG_MITER_FROM_SG: SG_MITER_TO_SG); 154 155 if (!sg_miter_skip(&miter, *offset)) 156 return cnt; 157 158 while (sg_miter_next(&miter) && cnt < buflen) { 159 unsigned int len = min_t(unsigned int, miter.length, 160 buflen - cnt); 161 162 if (dir == FROM_XFER_BUF) 163 memcpy(buffer + cnt, miter.addr, len); 164 else 165 memcpy(miter.addr, buffer + cnt, len); 166 167 if (*offset + len < miter.piter.sg->length) { 168 *offset += len; 169 *sgptr = miter.piter.sg; 170 } else { 171 *offset = 0; 172 *sgptr = sg_next(miter.piter.sg); 173 } 174 cnt += len; 175 } 176 sg_miter_stop(&miter); 177 178 return cnt; 179 } 180 EXPORT_SYMBOL_GPL(usb_stor_access_xfer_buf); 181 182 /* 183 * Store the contents of buffer into srb's transfer buffer and set the 184 * SCSI residue. 185 */ 186 void usb_stor_set_xfer_buf(unsigned char *buffer, 187 unsigned int buflen, struct scsi_cmnd *srb) 188 { 189 unsigned int offset = 0; 190 struct scatterlist *sg = NULL; 191 192 buflen = min(buflen, scsi_bufflen(srb)); 193 buflen = usb_stor_access_xfer_buf(buffer, buflen, srb, &sg, &offset, 194 TO_XFER_BUF); 195 if (buflen < scsi_bufflen(srb)) 196 scsi_set_resid(srb, scsi_bufflen(srb) - buflen); 197 } 198 EXPORT_SYMBOL_GPL(usb_stor_set_xfer_buf); 199