Merge branch 'devel' of master.kernel.org:/home/rmk/linux-2.6-arm
[deliverable/linux.git] / drivers / usb / storage / protocol.c
CommitLineData
1da177e4 1/* Driver for USB Mass Storage compliant devices
1da177e4
LT
2 *
3 * Current development and maintenance by:
4 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
5 *
6 * Developed with the assistance of:
7 * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
8 * (c) 2002 Alan Stern (stern@rowland.org)
9 *
10 * Initial work by:
11 * (c) 1999 Michael Gee (michael@linuxspecific.com)
12 *
13 * This driver is based on the 'USB Mass Storage Class' document. This
14 * describes in detail the protocol used to communicate with such
15 * devices. Clearly, the designers had SCSI and ATAPI commands in
16 * mind when they created this document. The commands are all very
17 * similar to commands in the SCSI-II and ATAPI specifications.
18 *
19 * It is important to note that in a number of cases this class
20 * exhibits class-specific exemptions from the USB specification.
21 * Notably the usage of NAK, STALL and ACK differs from the norm, in
22 * that they are used to communicate wait, failed and OK on commands.
23 *
24 * Also, for certain devices, the interrupt endpoint is used to convey
25 * status of a command.
26 *
27 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
28 * information about this driver.
29 *
30 * This program is free software; you can redistribute it and/or modify it
31 * under the terms of the GNU General Public License as published by the
32 * Free Software Foundation; either version 2, or (at your option) any
33 * later version.
34 *
35 * This program is distributed in the hope that it will be useful, but
36 * WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
38 * General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 675 Mass Ave, Cambridge, MA 02139, USA.
43 */
44
45#include <linux/highmem.h>
46#include <scsi/scsi.h>
47#include <scsi/scsi_cmnd.h>
48
49#include "usb.h"
50#include "protocol.h"
51#include "debug.h"
52#include "scsiglue.h"
53#include "transport.h"
54
55/***********************************************************************
56 * Protocol routines
57 ***********************************************************************/
58
59void usb_stor_qic157_command(struct scsi_cmnd *srb, struct us_data *us)
60{
61 /* Pad the ATAPI command with zeros
62 *
63 * NOTE: This only works because a scsi_cmnd struct field contains
64 * a unsigned char cmnd[16], so we know we have storage available
65 */
66 for (; srb->cmd_len<12; srb->cmd_len++)
67 srb->cmnd[srb->cmd_len] = 0;
68
69 /* set command length to 12 bytes */
70 srb->cmd_len = 12;
71
72 /* send the command to the transport layer */
73 usb_stor_invoke_transport(srb, us);
74}
75
76void usb_stor_ATAPI_command(struct scsi_cmnd *srb, struct us_data *us)
77{
78 /* Pad the ATAPI command with zeros
79 *
80 * NOTE: This only works because a scsi_cmnd struct field contains
81 * a unsigned char cmnd[16], so we know we have storage available
82 */
83
84 /* Pad the ATAPI command with zeros */
85 for (; srb->cmd_len<12; srb->cmd_len++)
86 srb->cmnd[srb->cmd_len] = 0;
87
88 /* set command length to 12 bytes */
89 srb->cmd_len = 12;
90
91 /* send the command to the transport layer */
92 usb_stor_invoke_transport(srb, us);
93}
94
95
96void usb_stor_ufi_command(struct scsi_cmnd *srb, struct us_data *us)
97{
98 /* fix some commands -- this is a form of mode translation
99 * UFI devices only accept 12 byte long commands
100 *
101 * NOTE: This only works because a scsi_cmnd struct field contains
102 * a unsigned char cmnd[16], so we know we have storage available
103 */
104
105 /* Pad the ATAPI command with zeros */
106 for (; srb->cmd_len<12; srb->cmd_len++)
107 srb->cmnd[srb->cmd_len] = 0;
108
109 /* set command length to 12 bytes (this affects the transport layer) */
110 srb->cmd_len = 12;
111
112 /* XXX We should be constantly re-evaluating the need for these */
113
114 /* determine the correct data length for these commands */
115 switch (srb->cmnd[0]) {
116
117 /* for INQUIRY, UFI devices only ever return 36 bytes */
118 case INQUIRY:
119 srb->cmnd[4] = 36;
120 break;
121
122 /* again, for MODE_SENSE_10, we get the minimum (8) */
123 case MODE_SENSE_10:
124 srb->cmnd[7] = 0;
125 srb->cmnd[8] = 8;
126 break;
127
128 /* for REQUEST_SENSE, UFI devices only ever return 18 bytes */
129 case REQUEST_SENSE:
130 srb->cmnd[4] = 18;
131 break;
132 } /* end switch on cmnd[0] */
133
134 /* send the command to the transport layer */
135 usb_stor_invoke_transport(srb, us);
136}
137
138void usb_stor_transparent_scsi_command(struct scsi_cmnd *srb,
139 struct us_data *us)
140{
141 /* send the command to the transport layer */
142 usb_stor_invoke_transport(srb, us);
143}
144
145/***********************************************************************
146 * Scatter-gather transfer buffer access routines
147 ***********************************************************************/
148
149/* Copy a buffer of length buflen to/from the srb's transfer buffer.
dd829d23 150 * Update the **sgptr and *offset variables so that the next copy will
7084191d
AS
151 * pick up from where this one left off.
152 */
1da177e4 153unsigned int usb_stor_access_xfer_buf(unsigned char *buffer,
1f6f31a0 154 unsigned int buflen, struct scsi_cmnd *srb, struct scatterlist **sgptr,
1da177e4
LT
155 unsigned int *offset, enum xfer_buf_dir dir)
156{
157 unsigned int cnt;
7084191d 158 struct scatterlist *sg = *sgptr;
1da177e4 159
dd829d23 160 /* We have to go through the list one entry
1da177e4
LT
161 * at a time. Each s-g entry contains some number of pages, and
162 * each page has to be kmap()'ed separately. If the page is already
163 * in kernel-addressable memory then kmap() will return its address.
164 * If the page is not directly accessible -- such as a user buffer
165 * located in high memory -- then kmap() will map it to a temporary
7084191d
AS
166 * position in the kernel's virtual address space.
167 */
dd829d23
BH
168
169 if (!sg)
170 sg = scsi_sglist(srb);
171
172 /* This loop handles a single s-g list entry, which may
7084191d
AS
173 * include multiple pages. Find the initial page structure
174 * and the starting offset within the page, and update
175 * the *offset and **sgptr values for the next loop.
176 */
dd829d23 177 cnt = 0;
7084191d 178 while (cnt < buflen && sg) {
dd829d23
BH
179 struct page *page = sg_page(sg) +
180 ((sg->offset + *offset) >> PAGE_SHIFT);
7084191d 181 unsigned int poff = (sg->offset + *offset) & (PAGE_SIZE-1);
dd829d23
BH
182 unsigned int sglen = sg->length - *offset;
183
184 if (sglen > buflen - cnt) {
185
186 /* Transfer ends within this s-g entry */
187 sglen = buflen - cnt;
188 *offset += sglen;
189 } else {
190
191 /* Transfer continues to next s-g entry */
192 *offset = 0;
193 sg = sg_next(sg);
194 }
195
196 /* Transfer the data for all the pages in this
197 * s-g entry. For each page: call kmap(), do the
198 * transfer, and call kunmap() immediately after. */
199 while (sglen > 0) {
200 unsigned int plen = min(sglen, (unsigned int)
201 PAGE_SIZE - poff);
202 unsigned char *ptr = kmap(page);
203
204 if (dir == TO_XFER_BUF)
205 memcpy(ptr + poff, buffer + cnt, plen);
206 else
207 memcpy(buffer + cnt, ptr + poff, plen);
208 kunmap(page);
209
210 /* Start at the beginning of the next page */
211 poff = 0;
212 ++page;
213 cnt += plen;
214 sglen -= plen;
1da177e4
LT
215 }
216 }
dd829d23 217 *sgptr = sg;
1da177e4
LT
218
219 /* Return the amount actually transferred */
220 return cnt;
221}
222
223/* Store the contents of buffer into srb's transfer buffer and set the
7084191d
AS
224 * SCSI residue.
225 */
1da177e4
LT
226void usb_stor_set_xfer_buf(unsigned char *buffer,
227 unsigned int buflen, struct scsi_cmnd *srb)
228{
1f6f31a0
JA
229 unsigned int offset = 0;
230 struct scatterlist *sg = NULL;
1da177e4 231
6d512a80 232 buflen = min(buflen, scsi_bufflen(srb));
7084191d 233 buflen = usb_stor_access_xfer_buf(buffer, buflen, srb, &sg, &offset,
1da177e4 234 TO_XFER_BUF);
dd829d23
BH
235 if (buflen < scsi_bufflen(srb))
236 scsi_set_resid(srb, scsi_bufflen(srb) - buflen);
1da177e4 237}
This page took 0.52893 seconds and 5 git commands to generate.