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)
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
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>
52 #include "transport.h"
54 /***********************************************************************
56 ***********************************************************************/
59 * Fix-up the return data from an INQUIRY command to show
60 * ANSI SCSI rev 2 so we don't confuse the SCSI layers above us
62 static void fix_inquiry_data(Scsi_Cmnd *srb)
64 unsigned char databuf[3];
65 unsigned int index, offset;
67 /* verify that it's an INQUIRY command */
68 if (srb->cmnd[0] != INQUIRY)
72 if (usb_stor_access_xfer_buf(databuf, sizeof(databuf), srb,
73 &index, &offset, FROM_XFER_BUF) != sizeof(databuf))
76 if ((databuf[2] & 7) == 2)
79 US_DEBUGP("Fixing INQUIRY data to show SCSI rev 2 - was %d\n",
82 /* Change the SCSI revision number */
83 databuf[2] = (databuf[2] & ~7) | 2;
86 usb_stor_access_xfer_buf(databuf, sizeof(databuf), srb,
87 &index, &offset, TO_XFER_BUF);
91 * Fix-up the return data from a READ CAPACITY command. My Feiya reader
92 * returns a value that is 1 too large.
94 static void fix_read_capacity(Scsi_Cmnd *srb)
96 unsigned int index, offset;
98 unsigned long capacity;
100 /* verify that it's a READ CAPACITY command */
101 if (srb->cmnd[0] != READ_CAPACITY)
105 if (usb_stor_access_xfer_buf((unsigned char *) &c, 4, srb,
106 &index, &offset, FROM_XFER_BUF) != 4)
109 capacity = be32_to_cpu(c);
110 US_DEBUGP("US: Fixing capacity: from %ld to %ld\n",
111 capacity+1, capacity);
112 c = cpu_to_be32(capacity - 1);
115 usb_stor_access_xfer_buf((unsigned char *) &c, 4, srb,
116 &index, &offset, TO_XFER_BUF);
119 /***********************************************************************
121 ***********************************************************************/
123 void usb_stor_qic157_command(Scsi_Cmnd *srb, struct us_data *us)
125 /* Pad the ATAPI command with zeros
127 * NOTE: This only works because a Scsi_Cmnd struct field contains
128 * a unsigned char cmnd[16], so we know we have storage available
130 for (; srb->cmd_len<12; srb->cmd_len++)
131 srb->cmnd[srb->cmd_len] = 0;
133 /* set command length to 12 bytes */
136 /* send the command to the transport layer */
137 usb_stor_invoke_transport(srb, us);
138 if (srb->result == SAM_STAT_GOOD) {
139 /* fix the INQUIRY data if necessary */
140 fix_inquiry_data(srb);
144 void usb_stor_ATAPI_command(Scsi_Cmnd *srb, struct us_data *us)
146 /* Pad the ATAPI command with zeros
148 * NOTE: This only works because a Scsi_Cmnd struct field contains
149 * a unsigned char cmnd[16], so we know we have storage available
152 /* Pad the ATAPI command with zeros */
153 for (; srb->cmd_len<12; srb->cmd_len++)
154 srb->cmnd[srb->cmd_len] = 0;
156 /* set command length to 12 bytes */
159 /* send the command to the transport layer */
160 usb_stor_invoke_transport(srb, us);
162 if (srb->result == SAM_STAT_GOOD) {
163 /* fix the INQUIRY data if necessary */
164 fix_inquiry_data(srb);
169 void usb_stor_ufi_command(Scsi_Cmnd *srb, struct us_data *us)
171 /* fix some commands -- this is a form of mode translation
172 * UFI devices only accept 12 byte long commands
174 * NOTE: This only works because a Scsi_Cmnd struct field contains
175 * a unsigned char cmnd[16], so we know we have storage available
178 /* Pad the ATAPI command with zeros */
179 for (; srb->cmd_len<12; srb->cmd_len++)
180 srb->cmnd[srb->cmd_len] = 0;
182 /* set command length to 12 bytes (this affects the transport layer) */
185 /* XXX We should be constantly re-evaluating the need for these */
187 /* determine the correct data length for these commands */
188 switch (srb->cmnd[0]) {
190 /* for INQUIRY, UFI devices only ever return 36 bytes */
195 /* again, for MODE_SENSE_10, we get the minimum (8) */
201 /* for REQUEST_SENSE, UFI devices only ever return 18 bytes */
205 } /* end switch on cmnd[0] */
207 /* send the command to the transport layer */
208 usb_stor_invoke_transport(srb, us);
210 if (srb->result == SAM_STAT_GOOD) {
211 /* Fix the data for an INQUIRY, if necessary */
212 fix_inquiry_data(srb);
216 void usb_stor_transparent_scsi_command(Scsi_Cmnd *srb, struct us_data *us)
218 /* send the command to the transport layer */
219 usb_stor_invoke_transport(srb, us);
221 if (srb->result == SAM_STAT_GOOD) {
222 /* Fix the INQUIRY data if necessary */
223 fix_inquiry_data(srb);
225 /* Fix the READ CAPACITY result if necessary */
226 if (us->flags & US_FL_FIX_CAPACITY)
227 fix_read_capacity(srb);
231 /***********************************************************************
232 * Scatter-gather transfer buffer access routines
233 ***********************************************************************/
235 /* Copy a buffer of length buflen to/from the srb's transfer buffer.
236 * (Note: for scatter-gather transfers (srb->use_sg > 0), srb->request_buffer
237 * points to a list of s-g entries and we ignore srb->request_bufflen.
238 * For non-scatter-gather transfers, srb->request_buffer points to the
239 * transfer buffer itself and srb->request_bufflen is the buffer's length.)
240 * Update the *index and *offset variables so that the next copy will
241 * pick up from where this one left off. */
243 unsigned int usb_stor_access_xfer_buf(unsigned char *buffer,
244 unsigned int buflen, Scsi_Cmnd *srb, unsigned int *index,
245 unsigned int *offset, enum xfer_buf_dir dir)
249 /* If not using scatter-gather, just transfer the data directly.
250 * Make certain it will fit in the available buffer space. */
251 if (srb->use_sg == 0) {
252 if (*offset >= srb->request_bufflen)
254 cnt = min(buflen, srb->request_bufflen - *offset);
255 if (dir == TO_XFER_BUF)
256 memcpy((unsigned char *) srb->request_buffer + *offset,
259 memcpy(buffer, (unsigned char *) srb->request_buffer +
263 /* Using scatter-gather. We have to go through the list one entry
264 * at a time. Each s-g entry contains some number of pages, and
265 * each page has to be kmap()'ed separately. If the page is already
266 * in kernel-addressable memory then kmap() will return its address.
267 * If the page is not directly accessible -- such as a user buffer
268 * located in high memory -- then kmap() will map it to a temporary
269 * position in the kernel's virtual address space. */
271 struct scatterlist *sg =
272 (struct scatterlist *) srb->request_buffer
275 /* This loop handles a single s-g list entry, which may
276 * include multiple pages. Find the initial page structure
277 * and the starting offset within the page, and update
278 * the *offset and *index values for the next loop. */
280 while (cnt < buflen && *index < srb->use_sg) {
281 struct page *page = sg->page +
282 ((sg->offset + *offset) >> PAGE_SHIFT);
284 (sg->offset + *offset) & (PAGE_SIZE-1);
285 unsigned int sglen = sg->length - *offset;
287 if (sglen > buflen - cnt) {
289 /* Transfer ends within this s-g entry */
290 sglen = buflen - cnt;
294 /* Transfer continues to next s-g entry */
300 /* Transfer the data for all the pages in this
301 * s-g entry. For each page: call kmap(), do the
302 * transfer, and call kunmap() immediately after. */
304 unsigned int plen = min(sglen, (unsigned int)
306 unsigned char *ptr = kmap(page);
308 if (dir == TO_XFER_BUF)
309 memcpy(ptr + poff, buffer + cnt, plen);
311 memcpy(buffer + cnt, ptr + poff, plen);
314 /* Start at the beginning of the next page */
323 /* Return the amount actually transferred */
327 /* Store the contents of buffer into srb's transfer buffer and set the
329 void usb_stor_set_xfer_buf(unsigned char *buffer,
330 unsigned int buflen, Scsi_Cmnd *srb)
332 unsigned int index = 0, offset = 0;
334 usb_stor_access_xfer_buf(buffer, buflen, srb, &index, &offset,
336 if (buflen < srb->request_bufflen)
337 srb->resid = srb->request_bufflen - buflen;