USB: add SPDX identifiers to all remaining files in drivers/usb/
[linux-2.6-block.git] / drivers / usb / storage / protocol.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0+
f0183a33
FB
2/*
3 * Driver for USB Mass Storage compliant devices
1da177e4
LT
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>
f940fcd8 48#include <linux/export.h>
1da177e4
LT
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
3dae5345 62void usb_stor_pad12_command(struct scsi_cmnd *srb, struct us_data *us)
1da177e4 63{
2f640bf4
AS
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.
1da177e4
LT
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 */
fc8ef481 71 for (; srb->cmd_len < 12; srb->cmd_len++)
1da177e4
LT
72 srb->cmnd[srb->cmd_len] = 0;
73
1da177e4
LT
74 /* send the command to the transport layer */
75 usb_stor_invoke_transport(srb, us);
76}
77
1da177e4
LT
78void usb_stor_ufi_command(struct scsi_cmnd *srb, struct us_data *us)
79{
f0183a33
FB
80 /*
81 * fix some commands -- this is a form of mode translation
fc8ef481 82 * UFI devices only accept 12 byte long commands
1da177e4
LT
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 */
fc8ef481 89 for (; srb->cmd_len < 12; srb->cmd_len++)
1da177e4
LT
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
121void 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}
e6e244b6 127EXPORT_SYMBOL_GPL(usb_stor_transparent_scsi_command);
1da177e4
LT
128
129/***********************************************************************
130 * Scatter-gather transfer buffer access routines
131 ***********************************************************************/
132
f0183a33
FB
133/*
134 * Copy a buffer of length buflen to/from the srb's transfer buffer.
dd829d23 135 * Update the **sgptr and *offset variables so that the next copy will
7084191d
AS
136 * pick up from where this one left off.
137 */
1da177e4 138unsigned int usb_stor_access_xfer_buf(unsigned char *buffer,
1f6f31a0 139 unsigned int buflen, struct scsi_cmnd *srb, struct scatterlist **sgptr,
1da177e4
LT
140 unsigned int *offset, enum xfer_buf_dir dir)
141{
e5fc70d5 142 unsigned int cnt = 0;
7084191d 143 struct scatterlist *sg = *sgptr;
e5fc70d5
ML
144 struct sg_mapping_iter miter;
145 unsigned int nents = scsi_sg_count(srb);
1da177e4 146
e5fc70d5
ML
147 if (sg)
148 nents = sg_nents(sg);
149 else
dd829d23
BH
150 sg = scsi_sglist(srb);
151
e5fc70d5
ML
152 sg_miter_start(&miter, sg, nents, dir == FROM_XFER_BUF ?
153 SG_MITER_FROM_SG: SG_MITER_TO_SG);
dd829d23 154
e5fc70d5
ML
155 if (!sg_miter_skip(&miter, *offset))
156 return cnt;
157
158 while (sg_miter_next(&miter) && cnt < buflen) {
40fcd88b
ML
159 unsigned int len = min_t(unsigned int, miter.length,
160 buflen - cnt);
e5fc70d5
ML
161
162 if (dir == FROM_XFER_BUF)
163 memcpy(buffer + cnt, miter.addr, len);
164 else
165 memcpy(miter.addr, buffer + cnt, len);
dd829d23 166
e5fc70d5
ML
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);
1da177e4 173 }
e5fc70d5 174 cnt += len;
1da177e4 175 }
e5fc70d5 176 sg_miter_stop(&miter);
1da177e4 177
1da177e4
LT
178 return cnt;
179}
e6e244b6 180EXPORT_SYMBOL_GPL(usb_stor_access_xfer_buf);
1da177e4 181
f0183a33
FB
182/*
183 * Store the contents of buffer into srb's transfer buffer and set the
7084191d
AS
184 * SCSI residue.
185 */
1da177e4
LT
186void usb_stor_set_xfer_buf(unsigned char *buffer,
187 unsigned int buflen, struct scsi_cmnd *srb)
188{
1f6f31a0
JA
189 unsigned int offset = 0;
190 struct scatterlist *sg = NULL;
1da177e4 191
6d512a80 192 buflen = min(buflen, scsi_bufflen(srb));
7084191d 193 buflen = usb_stor_access_xfer_buf(buffer, buflen, srb, &sg, &offset,
1da177e4 194 TO_XFER_BUF);
dd829d23
BH
195 if (buflen < scsi_bufflen(srb))
196 scsi_set_resid(srb, scsi_bufflen(srb) - buflen);
1da177e4 197}
e6e244b6 198EXPORT_SYMBOL_GPL(usb_stor_set_xfer_buf);