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