Import changeset
[linux-flexiantxendom0-3.2.10.git] / drivers / usb / storage / protocol.c
1 /* Driver for USB Mass Storage compliant devices
2  *
3  * $Id: protocol.c,v 1.7 2000/11/13 22:28:33 mdharm Exp $
4  *
5  * Current development and maintenance by:
6  *   (c) 1999, 2000 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  *
11  * Initial work by:
12  *   (c) 1999 Michael Gee (michael@linuxspecific.com)
13  *
14  * This driver is based on the 'USB Mass Storage Class' document. This
15  * describes in detail the protocol used to communicate with such
16  * devices.  Clearly, the designers had SCSI and ATAPI commands in
17  * mind when they created this document.  The commands are all very
18  * similar to commands in the SCSI-II and ATAPI specifications.
19  *
20  * It is important to note that in a number of cases this class
21  * exhibits class-specific exemptions from the USB specification.
22  * Notably the usage of NAK, STALL and ACK differs from the norm, in
23  * that they are used to communicate wait, failed and OK on commands.
24  *
25  * Also, for certain devices, the interrupt endpoint is used to convey
26  * status of a command.
27  *
28  * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
29  * information about this driver.
30  *
31  * This program is free software; you can redistribute it and/or modify it
32  * under the terms of the GNU General Public License as published by the
33  * Free Software Foundation; either version 2, or (at your option) any
34  * later version.
35  *
36  * This program is distributed in the hope that it will be useful, but
37  * WITHOUT ANY WARRANTY; without even the implied warranty of
38  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
39  * General Public License for more details.
40  *
41  * You should have received a copy of the GNU General Public License along
42  * with this program; if not, write to the Free Software Foundation, Inc.,
43  * 675 Mass Ave, Cambridge, MA 02139, USA.
44  */
45
46 #include "protocol.h"
47 #include "usb.h"
48 #include "debug.h"
49 #include "scsiglue.h"
50 #include "transport.h"
51
52 /***********************************************************************
53  * Helper routines
54  ***********************************************************************/
55
56 /* Fix-up the return data from an INQUIRY command to show 
57  * ANSI SCSI rev 2 so we don't confuse the SCSI layers above us
58  */
59 void fix_inquiry_data(Scsi_Cmnd *srb)
60 {
61         unsigned char *data_ptr;
62
63         /* verify that it's an INQUIRY command */
64         if (srb->cmnd[0] != INQUIRY)
65                 return;
66
67         US_DEBUGP("Fixing INQUIRY data to show SCSI rev 2\n");
68
69         /* find the location of the data */
70         if (srb->use_sg) {
71                 struct scatterlist *sg;
72
73                 sg = (struct scatterlist *) srb->request_buffer;
74                 data_ptr = (unsigned char *) sg[0].address;
75         } else
76                 data_ptr = (unsigned char *)srb->request_buffer;
77
78         /* Change the SCSI revision number */
79         data_ptr[2] |= 0x2;
80 }
81
82 /***********************************************************************
83  * Protocol routines
84  ***********************************************************************/
85
86 void usb_stor_qic157_command(Scsi_Cmnd *srb, struct us_data *us)
87 {
88         /* Pad the ATAPI command with zeros 
89          * NOTE: This only works because a Scsi_Cmnd struct field contains
90          * a unsigned char cmnd[12], so we know we have storage available
91          */
92         for (; srb->cmd_len<12; srb->cmd_len++)
93                 srb->cmnd[srb->cmd_len] = 0;
94
95         /* set command length to 12 bytes */
96         srb->cmd_len = 12;
97
98         /* send the command to the transport layer */
99         usb_stor_invoke_transport(srb, us);
100
101         /* fix the INQUIRY data if necessary */
102         fix_inquiry_data(srb);
103 }
104
105 void usb_stor_ATAPI_command(Scsi_Cmnd *srb, struct us_data *us)
106 {
107         int old_cmnd = 0;
108
109         /* Fix some commands -- this is a form of mode translation
110          * ATAPI devices only accept 12 byte long commands 
111          *
112          * NOTE: This only works because a Scsi_Cmnd struct field contains
113          * a unsigned char cmnd[12], so we know we have storage available
114          */
115
116         /* Pad the ATAPI command with zeros */
117         for (; srb->cmd_len<12; srb->cmd_len++)
118                 srb->cmnd[srb->cmd_len] = 0;
119
120         /* set command length to 12 bytes */
121         srb->cmd_len = 12;
122
123         /* determine the correct (or minimum) data length for these commands */
124         switch (srb->cmnd[0]) {
125
126                 /* change MODE_SENSE/MODE_SELECT from 6 to 10 byte commands */
127         case MODE_SENSE:
128         case MODE_SELECT:
129                 /* save the command so we can tell what it was */
130                 old_cmnd = srb->cmnd[0];
131
132                 srb->cmnd[11] = 0;
133                 srb->cmnd[10] = 0;
134                 srb->cmnd[9] = 0;
135                 srb->cmnd[8] = srb->cmnd[4];
136                 srb->cmnd[7] = 0;
137                 srb->cmnd[6] = 0;
138                 srb->cmnd[5] = 0;
139                 srb->cmnd[4] = 0;
140                 srb->cmnd[3] = 0;
141                 srb->cmnd[2] = srb->cmnd[2];
142                 srb->cmnd[1] = srb->cmnd[1];
143                 srb->cmnd[0] = srb->cmnd[0] | 0x40;
144                 break;
145
146                 /* change READ_6/WRITE_6 to READ_10/WRITE_10, which 
147                  * are ATAPI commands */
148         case WRITE_6:
149         case READ_6:
150                 srb->cmnd[11] = 0;
151                 srb->cmnd[10] = 0;
152                 srb->cmnd[9] = 0;
153                 srb->cmnd[8] = srb->cmnd[4];
154                 srb->cmnd[7] = 0;
155                 srb->cmnd[6] = 0;
156                 srb->cmnd[5] = srb->cmnd[3];
157                 srb->cmnd[4] = srb->cmnd[2];
158                 srb->cmnd[3] = srb->cmnd[1] & 0x1F;
159                 srb->cmnd[2] = 0;
160                 srb->cmnd[1] = srb->cmnd[1] & 0xE0;
161                 srb->cmnd[0] = srb->cmnd[0] | 0x20;
162                 break;
163         } /* end switch on cmnd[0] */
164
165         /* convert MODE_SELECT data here */
166         if (old_cmnd == MODE_SELECT)
167                 usb_stor_scsiSense6to10(srb);
168
169         /* send the command to the transport layer */
170         usb_stor_invoke_transport(srb, us);
171
172         /* Fix the MODE_SENSE data if we translated the command */
173         if ((old_cmnd == MODE_SENSE) && (status_byte(srb->result) == GOOD))
174                 usb_stor_scsiSense10to6(srb);
175
176         /* fix the INQUIRY data if necessary */
177         fix_inquiry_data(srb);
178 }
179
180
181 void usb_stor_ufi_command(Scsi_Cmnd *srb, struct us_data *us)
182 {
183         int old_cmnd = 0;
184
185         /* fix some commands -- this is a form of mode translation
186          * UFI devices only accept 12 byte long commands 
187          *
188          * NOTE: This only works because a Scsi_Cmnd struct field contains
189          * a unsigned char cmnd[12], so we know we have storage available
190          */
191
192         /* set command length to 12 bytes (this affects the transport layer) */
193         srb->cmd_len = 12;
194
195         /* determine the correct (or minimum) data length for these commands */
196         switch (srb->cmnd[0]) {
197
198                 /* for INQUIRY, UFI devices only ever return 36 bytes */
199         case INQUIRY:
200                 srb->cmnd[4] = 36;
201                 break;
202
203                 /* change MODE_SENSE/MODE_SELECT from 6 to 10 byte commands */
204         case MODE_SENSE:
205         case MODE_SELECT:
206                 /* save the command so we can tell what it was */
207                 old_cmnd = srb->cmnd[0];
208
209                 srb->cmnd[11] = 0;
210                 srb->cmnd[10] = 0;
211                 srb->cmnd[9] = 0;
212
213                 /* if we're sending data, we send all.  If getting data, 
214                  * get the minimum */
215                 if (srb->cmnd[0] == MODE_SELECT)
216                         srb->cmnd[8] = srb->cmnd[4];
217                 else
218                         srb->cmnd[8] = 8;
219
220                 srb->cmnd[7] = 0;
221                 srb->cmnd[6] = 0;
222                 srb->cmnd[5] = 0;
223                 srb->cmnd[4] = 0;
224                 srb->cmnd[3] = 0;
225                 srb->cmnd[2] = srb->cmnd[2];
226                 srb->cmnd[1] = srb->cmnd[1];
227                 srb->cmnd[0] = srb->cmnd[0] | 0x40;
228                 break;
229
230                 /* again, for MODE_SENSE_10, we get the minimum (8) */
231         case MODE_SENSE_10:
232                 srb->cmnd[7] = 0;
233                 srb->cmnd[8] = 8;
234                 break;
235
236                 /* for REQUEST_SENSE, UFI devices only ever return 18 bytes */
237         case REQUEST_SENSE:
238                 srb->cmnd[4] = 18;
239                 break;
240
241                 /* change READ_6/WRITE_6 to READ_10/WRITE_10, which 
242                  * are UFI commands */
243         case WRITE_6:
244         case READ_6:
245                 srb->cmnd[11] = 0;
246                 srb->cmnd[10] = 0;
247                 srb->cmnd[9] = 0;
248                 srb->cmnd[8] = srb->cmnd[4];
249                 srb->cmnd[7] = 0;
250                 srb->cmnd[6] = 0;
251                 srb->cmnd[5] = srb->cmnd[3];
252                 srb->cmnd[4] = srb->cmnd[2];
253                 srb->cmnd[3] = srb->cmnd[1] & 0x1F;
254                 srb->cmnd[2] = 0;
255                 srb->cmnd[1] = srb->cmnd[1] & 0xE0;
256                 srb->cmnd[0] = srb->cmnd[0] | 0x20;
257                 break;
258         } /* end switch on cmnd[0] */
259
260         /* convert MODE_SELECT data here */
261         if (old_cmnd == MODE_SELECT)
262                 usb_stor_scsiSense6to10(srb);
263
264         /* send the command to the transport layer */
265         usb_stor_invoke_transport(srb, us);
266
267         /* Fix the MODE_SENSE data if we translated the command */
268         if ((old_cmnd == MODE_SENSE) && (status_byte(srb->result) == GOOD))
269                 usb_stor_scsiSense10to6(srb);
270
271         /* Fix the data for an INQUIRY, if necessary */
272         fix_inquiry_data(srb);
273 }
274
275 void usb_stor_transparent_scsi_command(Scsi_Cmnd *srb, struct us_data *us)
276 {
277         /* This code supports devices which do not support {READ|WRITE}_6
278          * Apparently, neither Windows or MacOS will use these commands,
279          * so some devices do not support them
280          */
281         if (us->flags & US_FL_MODE_XLATE) {
282
283                 /* translate READ_6 to READ_10 */
284                 if (srb->cmnd[0] == 0x08) {
285
286                         /* get the control */
287                         srb->cmnd[9] = us->srb->cmnd[5];
288
289                         /* get the length */
290                         srb->cmnd[8] = us->srb->cmnd[6];
291                         srb->cmnd[7] = 0;
292
293                         /* set the reserved area to 0 */
294                         srb->cmnd[6] = 0;           
295
296                         /* get LBA */
297                         srb->cmnd[5] = us->srb->cmnd[3];
298                         srb->cmnd[4] = us->srb->cmnd[2];
299                         srb->cmnd[3] = 0;
300                         srb->cmnd[2] = 0;
301
302                         /* LUN and other info in cmnd[1] can stay */
303
304                         /* fix command code */
305                         srb->cmnd[0] = 0x28;
306
307                         US_DEBUGP("Changing READ_6 to READ_10\n");
308                         US_DEBUG(usb_stor_show_command(srb));
309                 }
310
311                 /* translate WRITE_6 to WRITE_10 */
312                 if (srb->cmnd[0] == 0x0A) {
313
314                         /* get the control */
315                         srb->cmnd[9] = us->srb->cmnd[5];
316
317                         /* get the length */
318                         srb->cmnd[8] = us->srb->cmnd[4];
319                         srb->cmnd[7] = 0;
320
321                         /* set the reserved area to 0 */
322                         srb->cmnd[6] = 0;           
323
324                         /* get LBA */
325                         srb->cmnd[5] = us->srb->cmnd[3];
326                         srb->cmnd[4] = us->srb->cmnd[2];
327                         srb->cmnd[3] = 0;
328                         srb->cmnd[2] = 0;
329             
330                         /* LUN and other info in cmnd[1] can stay */
331
332                         /* fix command code */
333                         srb->cmnd[0] = 0x2A;
334
335                         US_DEBUGP("Changing WRITE_6 to WRITE_10\n");
336                         US_DEBUG(usb_stor_show_command(us->srb));
337                 }
338         } /* if (us->flags & US_FL_MODE_XLATE) */
339
340         /* send the command to the transport layer */
341         usb_stor_invoke_transport(srb, us);
342
343         /* fix the INQUIRY data if necessary */
344         fix_inquiry_data(srb);
345 }
346