vserver 1.9.5.x5
[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 a READ CAPACITY command. My Feiya reader
62  * returns a value that is 1 too large.
63  */
64 static void fix_read_capacity(struct scsi_cmnd *srb)
65 {
66         unsigned int index, offset;
67         __be32 c;
68         unsigned long capacity;
69
70         /* verify that it's a READ CAPACITY command */
71         if (srb->cmnd[0] != READ_CAPACITY)
72                 return;
73
74         index = offset = 0;
75         if (usb_stor_access_xfer_buf((unsigned char *) &c, 4, srb,
76                         &index, &offset, FROM_XFER_BUF) != 4)
77                 return;
78
79         capacity = be32_to_cpu(c);
80         US_DEBUGP("US: Fixing capacity: from %ld to %ld\n",
81                capacity+1, capacity);
82         c = cpu_to_be32(capacity - 1);
83
84         index = offset = 0;
85         usb_stor_access_xfer_buf((unsigned char *) &c, 4, srb,
86                         &index, &offset, TO_XFER_BUF);
87 }
88
89 /***********************************************************************
90  * Protocol routines
91  ***********************************************************************/
92
93 void usb_stor_qic157_command(struct scsi_cmnd *srb, struct us_data *us)
94 {
95         /* Pad the ATAPI command with zeros 
96          *
97          * NOTE: This only works because a scsi_cmnd struct field contains
98          * a unsigned char cmnd[16], so we know we have storage available
99          */
100         for (; srb->cmd_len<12; srb->cmd_len++)
101                 srb->cmnd[srb->cmd_len] = 0;
102
103         /* set command length to 12 bytes */
104         srb->cmd_len = 12;
105
106         /* send the command to the transport layer */
107         usb_stor_invoke_transport(srb, us);
108 }
109
110 void usb_stor_ATAPI_command(struct scsi_cmnd *srb, struct us_data *us)
111 {
112         /* Pad the ATAPI command with zeros 
113          *
114          * NOTE: This only works because a scsi_cmnd struct field contains
115          * a unsigned char cmnd[16], so we know we have storage available
116          */
117
118         /* Pad the ATAPI command with zeros */
119         for (; srb->cmd_len<12; srb->cmd_len++)
120                 srb->cmnd[srb->cmd_len] = 0;
121
122         /* set command length to 12 bytes */
123         srb->cmd_len = 12;
124
125         /* send the command to the transport layer */
126         usb_stor_invoke_transport(srb, us);
127 }
128
129
130 void usb_stor_ufi_command(struct scsi_cmnd *srb, struct us_data *us)
131 {
132         /* fix some commands -- this is a form of mode translation
133          * UFI devices only accept 12 byte long commands 
134          *
135          * NOTE: This only works because a scsi_cmnd struct field contains
136          * a unsigned char cmnd[16], so we know we have storage available
137          */
138
139         /* Pad the ATAPI command with zeros */
140         for (; srb->cmd_len<12; srb->cmd_len++)
141                 srb->cmnd[srb->cmd_len] = 0;
142
143         /* set command length to 12 bytes (this affects the transport layer) */
144         srb->cmd_len = 12;
145
146         /* XXX We should be constantly re-evaluating the need for these */
147
148         /* determine the correct data length for these commands */
149         switch (srb->cmnd[0]) {
150
151                 /* for INQUIRY, UFI devices only ever return 36 bytes */
152         case INQUIRY:
153                 srb->cmnd[4] = 36;
154                 break;
155
156                 /* again, for MODE_SENSE_10, we get the minimum (8) */
157         case MODE_SENSE_10:
158                 srb->cmnd[7] = 0;
159                 srb->cmnd[8] = 8;
160                 break;
161
162                 /* for REQUEST_SENSE, UFI devices only ever return 18 bytes */
163         case REQUEST_SENSE:
164                 srb->cmnd[4] = 18;
165                 break;
166         } /* end switch on cmnd[0] */
167
168         /* send the command to the transport layer */
169         usb_stor_invoke_transport(srb, us);
170 }
171
172 void usb_stor_transparent_scsi_command(struct scsi_cmnd *srb,
173                                        struct us_data *us)
174 {
175         /* send the command to the transport layer */
176         usb_stor_invoke_transport(srb, us);
177
178         if (srb->result == SAM_STAT_GOOD) {
179                 /* Fix the READ CAPACITY result if necessary */
180                 if (us->flags & US_FL_FIX_CAPACITY)
181                         fix_read_capacity(srb);
182         }
183 }
184
185 /***********************************************************************
186  * Scatter-gather transfer buffer access routines
187  ***********************************************************************/
188
189 /* Copy a buffer of length buflen to/from the srb's transfer buffer.
190  * (Note: for scatter-gather transfers (srb->use_sg > 0), srb->request_buffer
191  * points to a list of s-g entries and we ignore srb->request_bufflen.
192  * For non-scatter-gather transfers, srb->request_buffer points to the
193  * transfer buffer itself and srb->request_bufflen is the buffer's length.)
194  * Update the *index and *offset variables so that the next copy will
195  * pick up from where this one left off. */
196
197 unsigned int usb_stor_access_xfer_buf(unsigned char *buffer,
198         unsigned int buflen, struct scsi_cmnd *srb, unsigned int *index,
199         unsigned int *offset, enum xfer_buf_dir dir)
200 {
201         unsigned int cnt;
202
203         /* If not using scatter-gather, just transfer the data directly.
204          * Make certain it will fit in the available buffer space. */
205         if (srb->use_sg == 0) {
206                 if (*offset >= srb->request_bufflen)
207                         return 0;
208                 cnt = min(buflen, srb->request_bufflen - *offset);
209                 if (dir == TO_XFER_BUF)
210                         memcpy((unsigned char *) srb->request_buffer + *offset,
211                                         buffer, cnt);
212                 else
213                         memcpy(buffer, (unsigned char *) srb->request_buffer +
214                                         *offset, cnt);
215                 *offset += cnt;
216
217         /* Using scatter-gather.  We have to go through the list one entry
218          * at a time.  Each s-g entry contains some number of pages, and
219          * each page has to be kmap()'ed separately.  If the page is already
220          * in kernel-addressable memory then kmap() will return its address.
221          * If the page is not directly accessible -- such as a user buffer
222          * located in high memory -- then kmap() will map it to a temporary
223          * position in the kernel's virtual address space. */
224         } else {
225                 struct scatterlist *sg =
226                                 (struct scatterlist *) srb->request_buffer
227                                 + *index;
228
229                 /* This loop handles a single s-g list entry, which may
230                  * include multiple pages.  Find the initial page structure
231                  * and the starting offset within the page, and update
232                  * the *offset and *index values for the next loop. */
233                 cnt = 0;
234                 while (cnt < buflen && *index < srb->use_sg) {
235                         struct page *page = sg->page +
236                                         ((sg->offset + *offset) >> PAGE_SHIFT);
237                         unsigned int poff =
238                                         (sg->offset + *offset) & (PAGE_SIZE-1);
239                         unsigned int sglen = sg->length - *offset;
240
241                         if (sglen > buflen - cnt) {
242
243                                 /* Transfer ends within this s-g entry */
244                                 sglen = buflen - cnt;
245                                 *offset += sglen;
246                         } else {
247
248                                 /* Transfer continues to next s-g entry */
249                                 *offset = 0;
250                                 ++*index;
251                                 ++sg;
252                         }
253
254                         /* Transfer the data for all the pages in this
255                          * s-g entry.  For each page: call kmap(), do the
256                          * transfer, and call kunmap() immediately after. */
257                         while (sglen > 0) {
258                                 unsigned int plen = min(sglen, (unsigned int)
259                                                 PAGE_SIZE - poff);
260                                 unsigned char *ptr = kmap(page);
261
262                                 if (dir == TO_XFER_BUF)
263                                         memcpy(ptr + poff, buffer + cnt, plen);
264                                 else
265                                         memcpy(buffer + cnt, ptr + poff, plen);
266                                 kunmap(page);
267
268                                 /* Start at the beginning of the next page */
269                                 poff = 0;
270                                 ++page;
271                                 cnt += plen;
272                                 sglen -= plen;
273                         }
274                 }
275         }
276
277         /* Return the amount actually transferred */
278         return cnt;
279 }
280
281 /* Store the contents of buffer into srb's transfer buffer and set the
282  * SCSI residue. */
283 void usb_stor_set_xfer_buf(unsigned char *buffer,
284         unsigned int buflen, struct scsi_cmnd *srb)
285 {
286         unsigned int index = 0, offset = 0;
287
288         usb_stor_access_xfer_buf(buffer, buflen, srb, &index, &offset,
289                         TO_XFER_BUF);
290         if (buflen < srb->request_bufflen)
291                 srb->resid = srb->request_bufflen - buflen;
292 }