1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for USB Mass Storage compliant devices
5 * Current development and maintenance by:
8 * Developed with the assistance of:
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.
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.
26 * Also, for certain devices, the interrupt endpoint is used to convey
27 * status of a command.
29 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
30 * information about this driver.
33 #include <linux/highmem.h>
34 #include <linux/export.h>
35 #include <scsi/scsi.h>
36 #include <scsi/scsi_cmnd.h>
42 #include "transport.h"
44 /***********************************************************************
46 ***********************************************************************/
48 void usb_stor_pad12_command(struct scsi_cmnd *srb, struct us_data *us)
51 * Pad the SCSI command with zeros out to 12 bytes. If the
52 * command already is 12 bytes or longer, leave it alone.
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
57 for (; srb->cmd_len < 12; srb->cmd_len++)
58 srb->cmnd[srb->cmd_len] = 0;
60 /* send the command to the transport layer */
61 usb_stor_invoke_transport(srb, us);
64 void usb_stor_ufi_command(struct scsi_cmnd *srb, struct us_data *us)
67 * fix some commands -- this is a form of mode translation
68 * UFI devices only accept 12 byte long commands
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
74 /* Pad the ATAPI command with zeros */
75 for (; srb->cmd_len < 12; srb->cmd_len++)
76 srb->cmnd[srb->cmd_len] = 0;
78 /* set command length to 12 bytes (this affects the transport layer) */
81 /* XXX We should be constantly re-evaluating the need for these */
83 /* determine the correct data length for these commands */
84 switch (srb->cmnd[0]) {
86 /* for INQUIRY, UFI devices only ever return 36 bytes */
91 /* again, for MODE_SENSE_10, we get the minimum (8) */
97 /* for REQUEST_SENSE, UFI devices only ever return 18 bytes */
101 } /* end switch on cmnd[0] */
103 /* send the command to the transport layer */
104 usb_stor_invoke_transport(srb, us);
107 void usb_stor_transparent_scsi_command(struct scsi_cmnd *srb,
110 /* send the command to the transport layer */
111 usb_stor_invoke_transport(srb, us);
113 EXPORT_SYMBOL_GPL(usb_stor_transparent_scsi_command);
115 /***********************************************************************
116 * Scatter-gather transfer buffer access routines
117 ***********************************************************************/
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.
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)
128 unsigned int cnt = 0;
129 struct scatterlist *sg = *sgptr;
130 struct sg_mapping_iter miter;
131 unsigned int nents = scsi_sg_count(srb);
134 nents = sg_nents(sg);
136 sg = scsi_sglist(srb);
138 sg_miter_start(&miter, sg, nents, dir == FROM_XFER_BUF ?
139 SG_MITER_FROM_SG: SG_MITER_TO_SG);
141 if (!sg_miter_skip(&miter, *offset))
144 while (sg_miter_next(&miter) && cnt < buflen) {
145 unsigned int len = min_t(unsigned int, miter.length,
148 if (dir == FROM_XFER_BUF)
149 memcpy(buffer + cnt, miter.addr, len);
151 memcpy(miter.addr, buffer + cnt, len);
153 if (*offset + len < miter.piter.sg->length) {
155 *sgptr = miter.piter.sg;
158 *sgptr = sg_next(miter.piter.sg);
162 sg_miter_stop(&miter);
166 EXPORT_SYMBOL_GPL(usb_stor_access_xfer_buf);
169 * Store the contents of buffer into srb's transfer buffer and set the
172 void usb_stor_set_xfer_buf(unsigned char *buffer,
173 unsigned int buflen, struct scsi_cmnd *srb)
175 unsigned int offset = 0;
176 struct scatterlist *sg = NULL;
178 buflen = min(buflen, scsi_bufflen(srb));
179 buflen = usb_stor_access_xfer_buf(buffer, buflen, srb, &sg, &offset,
181 if (buflen < scsi_bufflen(srb))
182 scsi_set_resid(srb, scsi_bufflen(srb) - buflen);
184 EXPORT_SYMBOL_GPL(usb_stor_set_xfer_buf);