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>
48 #include <scsi/scsi.h>
49 #include <scsi/scsi_cmnd.h>
54 #include "transport.h"
56 /***********************************************************************
58 ***********************************************************************/
61 * Fix-up the return data from an INQUIRY command to show
62 * ANSI SCSI rev 2 so we don't confuse the SCSI layers above us
64 static void fix_inquiry_data(struct scsi_cmnd *srb)
66 unsigned char databuf[3];
67 unsigned int index, offset;
69 /* verify that it's an INQUIRY command */
70 if (srb->cmnd[0] != INQUIRY)
74 if (usb_stor_access_xfer_buf(databuf, sizeof(databuf), srb,
75 &index, &offset, FROM_XFER_BUF) != sizeof(databuf))
78 if ((databuf[2] & 7) == 2)
81 US_DEBUGP("Fixing INQUIRY data to show SCSI rev 2 - was %d\n",
84 /* Change the SCSI revision number */
85 databuf[2] = (databuf[2] & ~7) | 2;
88 usb_stor_access_xfer_buf(databuf, sizeof(databuf), srb,
89 &index, &offset, TO_XFER_BUF);
93 * Fix-up the return data from a READ CAPACITY command. My Feiya reader
94 * returns a value that is 1 too large.
96 static void fix_read_capacity(struct scsi_cmnd *srb)
98 unsigned int index, offset;
100 unsigned long capacity;
102 /* verify that it's a READ CAPACITY command */
103 if (srb->cmnd[0] != READ_CAPACITY)
107 if (usb_stor_access_xfer_buf((unsigned char *) &c, 4, srb,
108 &index, &offset, FROM_XFER_BUF) != 4)
111 capacity = be32_to_cpu(c);
112 US_DEBUGP("US: Fixing capacity: from %ld to %ld\n",
113 capacity+1, capacity);
114 c = cpu_to_be32(capacity - 1);
117 usb_stor_access_xfer_buf((unsigned char *) &c, 4, srb,
118 &index, &offset, TO_XFER_BUF);
121 /***********************************************************************
123 ***********************************************************************/
125 void usb_stor_qic157_command(struct scsi_cmnd *srb, struct us_data *us)
127 /* Pad the ATAPI command with zeros
129 * NOTE: This only works because a scsi_cmnd struct field contains
130 * a unsigned char cmnd[16], so we know we have storage available
132 for (; srb->cmd_len<12; srb->cmd_len++)
133 srb->cmnd[srb->cmd_len] = 0;
135 /* set command length to 12 bytes */
138 /* send the command to the transport layer */
139 usb_stor_invoke_transport(srb, us);
140 if (srb->result == SAM_STAT_GOOD) {
141 /* fix the INQUIRY data if necessary */
142 fix_inquiry_data(srb);
146 void usb_stor_ATAPI_command(struct scsi_cmnd *srb, struct us_data *us)
148 /* Pad the ATAPI command with zeros
150 * NOTE: This only works because a scsi_cmnd struct field contains
151 * a unsigned char cmnd[16], so we know we have storage available
154 /* Pad the ATAPI command with zeros */
155 for (; srb->cmd_len<12; srb->cmd_len++)
156 srb->cmnd[srb->cmd_len] = 0;
158 /* set command length to 12 bytes */
161 /* send the command to the transport layer */
162 usb_stor_invoke_transport(srb, us);
164 if (srb->result == SAM_STAT_GOOD) {
165 /* fix the INQUIRY data if necessary */
166 fix_inquiry_data(srb);
171 void usb_stor_ufi_command(struct scsi_cmnd *srb, struct us_data *us)
173 /* fix some commands -- this is a form of mode translation
174 * UFI devices only accept 12 byte long commands
176 * NOTE: This only works because a scsi_cmnd struct field contains
177 * a unsigned char cmnd[16], so we know we have storage available
180 /* Pad the ATAPI command with zeros */
181 for (; srb->cmd_len<12; srb->cmd_len++)
182 srb->cmnd[srb->cmd_len] = 0;
184 /* set command length to 12 bytes (this affects the transport layer) */
187 /* XXX We should be constantly re-evaluating the need for these */
189 /* determine the correct data length for these commands */
190 switch (srb->cmnd[0]) {
192 /* for INQUIRY, UFI devices only ever return 36 bytes */
197 /* again, for MODE_SENSE_10, we get the minimum (8) */
203 /* for REQUEST_SENSE, UFI devices only ever return 18 bytes */
207 } /* end switch on cmnd[0] */
209 /* send the command to the transport layer */
210 usb_stor_invoke_transport(srb, us);
212 if (srb->result == SAM_STAT_GOOD) {
213 /* Fix the data for an INQUIRY, if necessary */
214 fix_inquiry_data(srb);
218 void usb_stor_transparent_scsi_command(struct scsi_cmnd *srb,
221 /* send the command to the transport layer */
222 usb_stor_invoke_transport(srb, us);
224 if (srb->result == SAM_STAT_GOOD) {
225 /* Fix the INQUIRY data if necessary */
226 fix_inquiry_data(srb);
228 /* Fix the READ CAPACITY result if necessary */
229 if (us->flags & US_FL_FIX_CAPACITY)
230 fix_read_capacity(srb);
234 /***********************************************************************
235 * Scatter-gather transfer buffer access routines
236 ***********************************************************************/
238 /* Copy a buffer of length buflen to/from the srb's transfer buffer.
239 * (Note: for scatter-gather transfers (srb->use_sg > 0), srb->request_buffer
240 * points to a list of s-g entries and we ignore srb->request_bufflen.
241 * For non-scatter-gather transfers, srb->request_buffer points to the
242 * transfer buffer itself and srb->request_bufflen is the buffer's length.)
243 * Update the *index and *offset variables so that the next copy will
244 * pick up from where this one left off. */
246 unsigned int usb_stor_access_xfer_buf(unsigned char *buffer,
247 unsigned int buflen, struct scsi_cmnd *srb, unsigned int *index,
248 unsigned int *offset, enum xfer_buf_dir dir)
252 /* If not using scatter-gather, just transfer the data directly.
253 * Make certain it will fit in the available buffer space. */
254 if (srb->use_sg == 0) {
255 if (*offset >= srb->request_bufflen)
257 cnt = min(buflen, srb->request_bufflen - *offset);
258 if (dir == TO_XFER_BUF)
259 memcpy((unsigned char *) srb->request_buffer + *offset,
262 memcpy(buffer, (unsigned char *) srb->request_buffer +
266 /* Using scatter-gather. We have to go through the list one entry
267 * at a time. Each s-g entry contains some number of pages, and
268 * each page has to be kmap()'ed separately. If the page is already
269 * in kernel-addressable memory then kmap() will return its address.
270 * If the page is not directly accessible -- such as a user buffer
271 * located in high memory -- then kmap() will map it to a temporary
272 * position in the kernel's virtual address space. */
274 struct scatterlist *sg =
275 (struct scatterlist *) srb->request_buffer
278 /* This loop handles a single s-g list entry, which may
279 * include multiple pages. Find the initial page structure
280 * and the starting offset within the page, and update
281 * the *offset and *index values for the next loop. */
283 while (cnt < buflen && *index < srb->use_sg) {
284 struct page *page = sg->page +
285 ((sg->offset + *offset) >> PAGE_SHIFT);
287 (sg->offset + *offset) & (PAGE_SIZE-1);
288 unsigned int sglen = sg->length - *offset;
290 if (sglen > buflen - cnt) {
292 /* Transfer ends within this s-g entry */
293 sglen = buflen - cnt;
297 /* Transfer continues to next s-g entry */
303 /* Transfer the data for all the pages in this
304 * s-g entry. For each page: call kmap(), do the
305 * transfer, and call kunmap() immediately after. */
307 unsigned int plen = min(sglen, (unsigned int)
309 unsigned char *ptr = kmap(page);
311 if (dir == TO_XFER_BUF)
312 memcpy(ptr + poff, buffer + cnt, plen);
314 memcpy(buffer + cnt, ptr + poff, plen);
317 /* Start at the beginning of the next page */
326 /* Return the amount actually transferred */
330 /* Store the contents of buffer into srb's transfer buffer and set the
332 void usb_stor_set_xfer_buf(unsigned char *buffer,
333 unsigned int buflen, struct scsi_cmnd *srb)
335 unsigned int index = 0, offset = 0;
337 usb_stor_access_xfer_buf(buffer, buflen, srb, &index, &offset,
339 if (buflen < srb->request_bufflen)
340 srb->resid = srb->request_bufflen - buflen;