vserver 1.9.3
[linux-2.6.git] / drivers / usb / storage / protocol.c
1 /* Driver for USB Mass Storage compliant devices
2  *
3  * $Id: protocol.c,v 1.14 2002/04/22 03:39:43 mdharm Exp $
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>
48 #include <scsi/scsi.h>
49 #include <scsi/scsi_cmnd.h>
50 #include "protocol.h"
51 #include "usb.h"
52 #include "debug.h"
53 #include "scsiglue.h"
54 #include "transport.h"
55
56 /***********************************************************************
57  * Helper routines
58  ***********************************************************************/
59
60 /*
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
63  */
64 static void fix_inquiry_data(struct scsi_cmnd *srb)
65 {
66         unsigned char databuf[3];
67         unsigned int index, offset;
68
69         /* verify that it's an INQUIRY command */
70         if (srb->cmnd[0] != INQUIRY)
71                 return;
72
73         index = offset = 0;
74         if (usb_stor_access_xfer_buf(databuf, sizeof(databuf), srb,
75                         &index, &offset, FROM_XFER_BUF) != sizeof(databuf))
76                 return;
77
78         if ((databuf[2] & 7) == 2)
79                 return;
80
81         US_DEBUGP("Fixing INQUIRY data to show SCSI rev 2 - was %d\n",
82                   databuf[2] & 7);
83
84         /* Change the SCSI revision number */
85         databuf[2] = (databuf[2] & ~7) | 2;
86
87         index = offset = 0;
88         usb_stor_access_xfer_buf(databuf, sizeof(databuf), srb,
89                         &index, &offset, TO_XFER_BUF);
90 }
91
92 /*
93  * Fix-up the return data from a READ CAPACITY command. My Feiya reader
94  * returns a value that is 1 too large.
95  */
96 static void fix_read_capacity(struct scsi_cmnd *srb)
97 {
98         unsigned int index, offset;
99         __be32 c;
100         unsigned long capacity;
101
102         /* verify that it's a READ CAPACITY command */
103         if (srb->cmnd[0] != READ_CAPACITY)
104                 return;
105
106         index = offset = 0;
107         if (usb_stor_access_xfer_buf((unsigned char *) &c, 4, srb,
108                         &index, &offset, FROM_XFER_BUF) != 4)
109                 return;
110
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);
115
116         index = offset = 0;
117         usb_stor_access_xfer_buf((unsigned char *) &c, 4, srb,
118                         &index, &offset, TO_XFER_BUF);
119 }
120
121 /***********************************************************************
122  * Protocol routines
123  ***********************************************************************/
124
125 void usb_stor_qic157_command(struct scsi_cmnd *srb, struct us_data *us)
126 {
127         /* Pad the ATAPI command with zeros 
128          *
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
131          */
132         for (; srb->cmd_len<12; srb->cmd_len++)
133                 srb->cmnd[srb->cmd_len] = 0;
134
135         /* set command length to 12 bytes */
136         srb->cmd_len = 12;
137
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);
143         }
144 }
145
146 void usb_stor_ATAPI_command(struct scsi_cmnd *srb, struct us_data *us)
147 {
148         /* Pad the ATAPI command with zeros 
149          *
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
152          */
153
154         /* Pad the ATAPI command with zeros */
155         for (; srb->cmd_len<12; srb->cmd_len++)
156                 srb->cmnd[srb->cmd_len] = 0;
157
158         /* set command length to 12 bytes */
159         srb->cmd_len = 12;
160
161         /* send the command to the transport layer */
162         usb_stor_invoke_transport(srb, us);
163
164         if (srb->result == SAM_STAT_GOOD) {
165                 /* fix the INQUIRY data if necessary */
166                 fix_inquiry_data(srb);
167         }
168 }
169
170
171 void usb_stor_ufi_command(struct scsi_cmnd *srb, struct us_data *us)
172 {
173         /* fix some commands -- this is a form of mode translation
174          * UFI devices only accept 12 byte long commands 
175          *
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
178          */
179
180         /* Pad the ATAPI command with zeros */
181         for (; srb->cmd_len<12; srb->cmd_len++)
182                 srb->cmnd[srb->cmd_len] = 0;
183
184         /* set command length to 12 bytes (this affects the transport layer) */
185         srb->cmd_len = 12;
186
187         /* XXX We should be constantly re-evaluating the need for these */
188
189         /* determine the correct data length for these commands */
190         switch (srb->cmnd[0]) {
191
192                 /* for INQUIRY, UFI devices only ever return 36 bytes */
193         case INQUIRY:
194                 srb->cmnd[4] = 36;
195                 break;
196
197                 /* again, for MODE_SENSE_10, we get the minimum (8) */
198         case MODE_SENSE_10:
199                 srb->cmnd[7] = 0;
200                 srb->cmnd[8] = 8;
201                 break;
202
203                 /* for REQUEST_SENSE, UFI devices only ever return 18 bytes */
204         case REQUEST_SENSE:
205                 srb->cmnd[4] = 18;
206                 break;
207         } /* end switch on cmnd[0] */
208
209         /* send the command to the transport layer */
210         usb_stor_invoke_transport(srb, us);
211
212         if (srb->result == SAM_STAT_GOOD) {
213                 /* Fix the data for an INQUIRY, if necessary */
214                 fix_inquiry_data(srb);
215         }
216 }
217
218 void usb_stor_transparent_scsi_command(struct scsi_cmnd *srb,
219                                        struct us_data *us)
220 {
221         /* send the command to the transport layer */
222         usb_stor_invoke_transport(srb, us);
223
224         if (srb->result == SAM_STAT_GOOD) {
225                 /* Fix the INQUIRY data if necessary */
226                 fix_inquiry_data(srb);
227
228                 /* Fix the READ CAPACITY result if necessary */
229                 if (us->flags & US_FL_FIX_CAPACITY)
230                         fix_read_capacity(srb);
231         }
232 }
233
234 /***********************************************************************
235  * Scatter-gather transfer buffer access routines
236  ***********************************************************************/
237
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. */
245
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)
249 {
250         unsigned int cnt;
251
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)
256                         return 0;
257                 cnt = min(buflen, srb->request_bufflen - *offset);
258                 if (dir == TO_XFER_BUF)
259                         memcpy((unsigned char *) srb->request_buffer + *offset,
260                                         buffer, cnt);
261                 else
262                         memcpy(buffer, (unsigned char *) srb->request_buffer +
263                                         *offset, cnt);
264                 *offset += cnt;
265
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. */
273         } else {
274                 struct scatterlist *sg =
275                                 (struct scatterlist *) srb->request_buffer
276                                 + *index;
277
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. */
282                 cnt = 0;
283                 while (cnt < buflen && *index < srb->use_sg) {
284                         struct page *page = sg->page +
285                                         ((sg->offset + *offset) >> PAGE_SHIFT);
286                         unsigned int poff =
287                                         (sg->offset + *offset) & (PAGE_SIZE-1);
288                         unsigned int sglen = sg->length - *offset;
289
290                         if (sglen > buflen - cnt) {
291
292                                 /* Transfer ends within this s-g entry */
293                                 sglen = buflen - cnt;
294                                 *offset += sglen;
295                         } else {
296
297                                 /* Transfer continues to next s-g entry */
298                                 *offset = 0;
299                                 ++*index;
300                                 ++sg;
301                         }
302
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. */
306                         while (sglen > 0) {
307                                 unsigned int plen = min(sglen, (unsigned int)
308                                                 PAGE_SIZE - poff);
309                                 unsigned char *ptr = kmap(page);
310
311                                 if (dir == TO_XFER_BUF)
312                                         memcpy(ptr + poff, buffer + cnt, plen);
313                                 else
314                                         memcpy(buffer + cnt, ptr + poff, plen);
315                                 kunmap(page);
316
317                                 /* Start at the beginning of the next page */
318                                 poff = 0;
319                                 ++page;
320                                 cnt += plen;
321                                 sglen -= plen;
322                         }
323                 }
324         }
325
326         /* Return the amount actually transferred */
327         return cnt;
328 }
329
330 /* Store the contents of buffer into srb's transfer buffer and set the
331  * SCSI residue. */
332 void usb_stor_set_xfer_buf(unsigned char *buffer,
333         unsigned int buflen, struct scsi_cmnd *srb)
334 {
335         unsigned int index = 0, offset = 0;
336
337         usb_stor_access_xfer_buf(buffer, buflen, srb, &index, &offset,
338                         TO_XFER_BUF);
339         if (buflen < srb->request_bufflen)
340                 srb->resid = srb->request_bufflen - buflen;
341 }