Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / usb / storage / protocol.c
blob015f048fd6d9245e64905557f98665731de7fdd5
1 /* Driver for USB Mass Storage compliant devices
3 * $Id: protocol.c,v 1.14 2002/04/22 03:39:43 mdharm Exp $
5 * Current development and maintenance by:
6 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
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)
12 * Initial work by:
13 * (c) 1999 Michael Gee (michael@linuxspecific.com)
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.
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.
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.
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.
47 #include <linux/highmem.h>
48 #include <scsi/scsi.h>
49 #include <scsi/scsi_cmnd.h>
51 #include "usb.h"
52 #include "protocol.h"
53 #include "debug.h"
54 #include "scsiglue.h"
55 #include "transport.h"
57 /***********************************************************************
58 * Protocol routines
59 ***********************************************************************/
61 void usb_stor_qic157_command(struct scsi_cmnd *srb, struct us_data *us)
63 /* Pad the ATAPI command with zeros
65 * NOTE: This only works because a scsi_cmnd struct field contains
66 * a unsigned char cmnd[16], so we know we have storage available
68 for (; srb->cmd_len<12; srb->cmd_len++)
69 srb->cmnd[srb->cmd_len] = 0;
71 /* set command length to 12 bytes */
72 srb->cmd_len = 12;
74 /* send the command to the transport layer */
75 usb_stor_invoke_transport(srb, us);
78 void usb_stor_ATAPI_command(struct scsi_cmnd *srb, struct us_data *us)
80 /* Pad the ATAPI command with zeros
82 * NOTE: This only works because a scsi_cmnd struct field contains
83 * a unsigned char cmnd[16], so we know we have storage available
86 /* Pad the ATAPI command with zeros */
87 for (; srb->cmd_len<12; srb->cmd_len++)
88 srb->cmnd[srb->cmd_len] = 0;
90 /* set command length to 12 bytes */
91 srb->cmd_len = 12;
93 /* send the command to the transport layer */
94 usb_stor_invoke_transport(srb, us);
98 void usb_stor_ufi_command(struct scsi_cmnd *srb, struct us_data *us)
100 /* fix some commands -- this is a form of mode translation
101 * UFI devices only accept 12 byte long commands
103 * NOTE: This only works because a scsi_cmnd struct field contains
104 * a unsigned char cmnd[16], so we know we have storage available
107 /* Pad the ATAPI command with zeros */
108 for (; srb->cmd_len<12; srb->cmd_len++)
109 srb->cmnd[srb->cmd_len] = 0;
111 /* set command length to 12 bytes (this affects the transport layer) */
112 srb->cmd_len = 12;
114 /* XXX We should be constantly re-evaluating the need for these */
116 /* determine the correct data length for these commands */
117 switch (srb->cmnd[0]) {
119 /* for INQUIRY, UFI devices only ever return 36 bytes */
120 case INQUIRY:
121 srb->cmnd[4] = 36;
122 break;
124 /* again, for MODE_SENSE_10, we get the minimum (8) */
125 case MODE_SENSE_10:
126 srb->cmnd[7] = 0;
127 srb->cmnd[8] = 8;
128 break;
130 /* for REQUEST_SENSE, UFI devices only ever return 18 bytes */
131 case REQUEST_SENSE:
132 srb->cmnd[4] = 18;
133 break;
134 } /* end switch on cmnd[0] */
136 /* send the command to the transport layer */
137 usb_stor_invoke_transport(srb, us);
140 void usb_stor_transparent_scsi_command(struct scsi_cmnd *srb,
141 struct us_data *us)
143 /* send the command to the transport layer */
144 usb_stor_invoke_transport(srb, us);
147 /***********************************************************************
148 * Scatter-gather transfer buffer access routines
149 ***********************************************************************/
151 /* Copy a buffer of length buflen to/from the srb's transfer buffer.
152 * Update the **sgptr and *offset variables so that the next copy will
153 <<<<<<< HEAD:drivers/usb/storage/protocol.c
154 * pick up from where this one left off. */
156 =======
157 * pick up from where this one left off.
159 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/usb/storage/protocol.c
160 unsigned int usb_stor_access_xfer_buf(unsigned char *buffer,
161 unsigned int buflen, struct scsi_cmnd *srb, struct scatterlist **sgptr,
162 unsigned int *offset, enum xfer_buf_dir dir)
164 unsigned int cnt;
165 <<<<<<< HEAD:drivers/usb/storage/protocol.c
166 =======
167 struct scatterlist *sg = *sgptr;
168 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/usb/storage/protocol.c
170 /* We have to go through the list one entry
171 * at a time. Each s-g entry contains some number of pages, and
172 * each page has to be kmap()'ed separately. If the page is already
173 * in kernel-addressable memory then kmap() will return its address.
174 * If the page is not directly accessible -- such as a user buffer
175 * located in high memory -- then kmap() will map it to a temporary
176 <<<<<<< HEAD:drivers/usb/storage/protocol.c
177 * position in the kernel's virtual address space. */
178 struct scatterlist *sg = *sgptr;
179 =======
180 * position in the kernel's virtual address space.
182 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/usb/storage/protocol.c
184 if (!sg)
185 sg = scsi_sglist(srb);
187 /* This loop handles a single s-g list entry, which may
188 <<<<<<< HEAD:drivers/usb/storage/protocol.c
189 * include multiple pages. Find the initial page structure
190 * and the starting offset within the page, and update
191 * the *offset and **sgptr values for the next loop. */
192 =======
193 * include multiple pages. Find the initial page structure
194 * and the starting offset within the page, and update
195 * the *offset and **sgptr values for the next loop.
197 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/usb/storage/protocol.c
198 cnt = 0;
199 <<<<<<< HEAD:drivers/usb/storage/protocol.c
200 while (cnt < buflen) {
201 =======
202 while (cnt < buflen && sg) {
203 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/usb/storage/protocol.c
204 struct page *page = sg_page(sg) +
205 ((sg->offset + *offset) >> PAGE_SHIFT);
206 <<<<<<< HEAD:drivers/usb/storage/protocol.c
207 unsigned int poff =
208 (sg->offset + *offset) & (PAGE_SIZE-1);
209 =======
210 unsigned int poff = (sg->offset + *offset) & (PAGE_SIZE-1);
211 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/usb/storage/protocol.c
212 unsigned int sglen = sg->length - *offset;
214 if (sglen > buflen - cnt) {
216 /* Transfer ends within this s-g entry */
217 sglen = buflen - cnt;
218 *offset += sglen;
219 } else {
221 /* Transfer continues to next s-g entry */
222 *offset = 0;
223 sg = sg_next(sg);
226 /* Transfer the data for all the pages in this
227 * s-g entry. For each page: call kmap(), do the
228 * transfer, and call kunmap() immediately after. */
229 while (sglen > 0) {
230 unsigned int plen = min(sglen, (unsigned int)
231 PAGE_SIZE - poff);
232 unsigned char *ptr = kmap(page);
234 if (dir == TO_XFER_BUF)
235 memcpy(ptr + poff, buffer + cnt, plen);
236 else
237 memcpy(buffer + cnt, ptr + poff, plen);
238 kunmap(page);
240 /* Start at the beginning of the next page */
241 poff = 0;
242 ++page;
243 cnt += plen;
244 sglen -= plen;
247 *sgptr = sg;
249 /* Return the amount actually transferred */
250 return cnt;
253 /* Store the contents of buffer into srb's transfer buffer and set the
254 <<<<<<< HEAD:drivers/usb/storage/protocol.c
255 * SCSI residue. */
256 =======
257 * SCSI residue.
259 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/usb/storage/protocol.c
260 void usb_stor_set_xfer_buf(unsigned char *buffer,
261 unsigned int buflen, struct scsi_cmnd *srb)
263 unsigned int offset = 0;
264 struct scatterlist *sg = NULL;
266 <<<<<<< HEAD:drivers/usb/storage/protocol.c
267 usb_stor_access_xfer_buf(buffer, buflen, srb, &sg, &offset,
268 =======
269 buflen = min(buflen, scsi_bufflen(srb));
270 buflen = usb_stor_access_xfer_buf(buffer, buflen, srb, &sg, &offset,
271 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/usb/storage/protocol.c
272 TO_XFER_BUF);
273 if (buflen < scsi_bufflen(srb))
274 scsi_set_resid(srb, scsi_bufflen(srb) - buflen);