1 /* Driver for USB Mass Storage compliant devices
2  *
3  * $Id: protocol.c,v 1.13 2002/02/25 00:34:56 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 "protocol.h"
48 #include "usb.h"
49 #include "debug.h"
50 #include "scsiglue.h"
51 #include "transport.h"
52 
53 /***********************************************************************
54  * Helper routines
55  ***********************************************************************/
56 
find_data_location(Scsi_Cmnd * srb)57 static void * find_data_location(Scsi_Cmnd *srb)
58 {
59 	if (srb->use_sg) {
60 		/*
61 		 * This piece of code only works if the first page is
62 		 * big enough to hold more than 3 bytes -- which is
63 		 * _very_ likely.
64 		 */
65 		struct scatterlist *sg;
66 
67 		sg = (struct scatterlist *) srb->request_buffer;
68 		return (void *) sg[0].address;
69 	} else
70 		return (void *) srb->request_buffer;
71 }
72 
73 /* Fix-up the return data from an INQUIRY command to show
74  * ANSI SCSI rev 2 so we don't confuse the SCSI layers above us
75  */
fix_inquiry_data(Scsi_Cmnd * srb)76 void fix_inquiry_data(Scsi_Cmnd *srb)
77 {
78 	unsigned char *data_ptr;
79 
80 	/* verify that it's an INQUIRY command */
81 	if (srb->cmnd[0] != INQUIRY)
82 		return;
83 
84 	/* oddly short buffer -- bail out */
85 	if (srb->request_bufflen < 3)
86 		return;
87 
88 	data_ptr = find_data_location(srb);
89 
90 	/* if it's already 2, bail */
91 	if ((data_ptr[2] & 7) == 2)
92 		return;
93 
94 	US_DEBUGP("Fixing INQUIRY data to show SCSI rev 2 - was %d\n",
95 		data_ptr[2] & 7);
96 
97 	/* Change the SCSI revision number */
98 	data_ptr[2] = (data_ptr[2] & ~7) | 2;
99 }
100 
101 /*
102  * Fix-up the return data from a READ CAPACITY command. A Feiya reader
103  * returns a value that is 1 too large.
104  */
fix_read_capacity(Scsi_Cmnd * srb)105 static void fix_read_capacity(Scsi_Cmnd *srb)
106 {
107 	unsigned char *dp;
108 	unsigned long capacity;
109 
110 	/* verify that it's a READ CAPACITY command */
111 	if (srb->cmnd[0] != READ_CAPACITY)
112 		return;
113 
114 	dp = find_data_location(srb);
115 
116 	capacity = (dp[0]<<24) + (dp[1]<<16) + (dp[2]<<8) + (dp[3]);
117 	US_DEBUGP("US: Fixing capacity: from %ld to %ld\n",
118 		capacity+1, capacity);
119 	capacity--;
120 	dp[0] = (capacity >> 24);
121 	dp[1] = (capacity >> 16);
122 	dp[2] = (capacity >> 8);
123 	dp[3] = (capacity);
124 }
125 
126 /***********************************************************************
127  * Protocol routines
128  ***********************************************************************/
129 
usb_stor_qic157_command(Scsi_Cmnd * srb,struct us_data * us)130 void usb_stor_qic157_command(Scsi_Cmnd *srb, struct us_data *us)
131 {
132 	/* Pad the ATAPI command with zeros
133 	 * NOTE: This only works because a Scsi_Cmnd struct field contains
134 	 * a unsigned char cmnd[12], so we know we have storage available
135 	 */
136 	for (; srb->cmd_len<12; srb->cmd_len++)
137 		srb->cmnd[srb->cmd_len] = 0;
138 
139 	/* set command length to 12 bytes */
140 	srb->cmd_len = 12;
141 
142 	/* send the command to the transport layer */
143 	usb_stor_invoke_transport(srb, us);
144 	if (srb->result == GOOD << 1) {
145 
146 		/* fix the INQUIRY data if necessary */
147 		fix_inquiry_data(srb);
148 	}
149 }
150 
usb_stor_ATAPI_command(Scsi_Cmnd * srb,struct us_data * us)151 void usb_stor_ATAPI_command(Scsi_Cmnd *srb, struct us_data *us)
152 {
153 	int old_cmnd = 0;
154 
155 	/* Fix some commands -- this is a form of mode translation
156 	 * ATAPI devices only accept 12 byte long commands
157 	 *
158 	 * NOTE: This only works because a Scsi_Cmnd struct field contains
159 	 * a unsigned char cmnd[12], so we know we have storage available
160 	 */
161 
162 	/* Pad the ATAPI command with zeros */
163 	for (; srb->cmd_len<12; srb->cmd_len++)
164 		srb->cmnd[srb->cmd_len] = 0;
165 
166 	/* set command length to 12 bytes */
167 	srb->cmd_len = 12;
168 
169 	/* determine the correct (or minimum) data length for these commands */
170 	switch (srb->cmnd[0]) {
171 
172 		/* change MODE_SENSE/MODE_SELECT from 6 to 10 byte commands */
173 	case MODE_SENSE:
174 	case MODE_SELECT:
175 		/* save the command so we can tell what it was */
176 		old_cmnd = srb->cmnd[0];
177 
178 		srb->cmnd[11] = 0;
179 		srb->cmnd[10] = 0;
180 		srb->cmnd[9] = 0;
181 		srb->cmnd[8] = srb->cmnd[4];
182 		srb->cmnd[7] = 0;
183 		srb->cmnd[6] = 0;
184 		srb->cmnd[5] = 0;
185 		srb->cmnd[4] = 0;
186 		srb->cmnd[3] = 0;
187 		srb->cmnd[2] = srb->cmnd[2];
188 		srb->cmnd[1] = srb->cmnd[1];
189 		srb->cmnd[0] = srb->cmnd[0] | 0x40;
190 		break;
191 
192 		/* change READ_6/WRITE_6 to READ_10/WRITE_10, which
193 		 * are ATAPI commands */
194 	case WRITE_6:
195 	case READ_6:
196 		srb->cmnd[11] = 0;
197 		srb->cmnd[10] = 0;
198 		srb->cmnd[9] = 0;
199 		srb->cmnd[8] = srb->cmnd[4];
200 		srb->cmnd[7] = 0;
201 		srb->cmnd[6] = 0;
202 		srb->cmnd[5] = srb->cmnd[3];
203 		srb->cmnd[4] = srb->cmnd[2];
204 		srb->cmnd[3] = srb->cmnd[1] & 0x1F;
205 		srb->cmnd[2] = 0;
206 		srb->cmnd[1] = srb->cmnd[1] & 0xE0;
207 		srb->cmnd[0] = srb->cmnd[0] | 0x20;
208 		break;
209 	} /* end switch on cmnd[0] */
210 
211 	/* convert MODE_SELECT data here */
212 	if (old_cmnd == MODE_SELECT)
213 		usb_stor_scsiSense6to10(srb);
214 
215 	/* send the command to the transport layer */
216 	usb_stor_invoke_transport(srb, us);
217 	if (srb->result == GOOD << 1) {
218 
219 		/* Fix the MODE_SENSE data if we translated the command */
220 		if (old_cmnd == MODE_SENSE)
221 			usb_stor_scsiSense10to6(srb);
222 
223 		/* fix the INQUIRY data if necessary */
224 		fix_inquiry_data(srb);
225 	}
226 }
227 
228 
usb_stor_ufi_command(Scsi_Cmnd * srb,struct us_data * us)229 void usb_stor_ufi_command(Scsi_Cmnd *srb, struct us_data *us)
230 {
231 	int old_cmnd = 0;
232 
233 	/* fix some commands -- this is a form of mode translation
234 	 * UFI devices only accept 12 byte long commands
235 	 *
236 	 * NOTE: This only works because a Scsi_Cmnd struct field contains
237 	 * a unsigned char cmnd[12], so we know we have storage available
238 	 */
239 
240 	/* Pad the ATAPI command with zeros */
241 	for (; srb->cmd_len<12; srb->cmd_len++)
242 		srb->cmnd[srb->cmd_len] = 0;
243 
244 	/* set command length to 12 bytes (this affects the transport layer) */
245 	srb->cmd_len = 12;
246 
247 	/* determine the correct (or minimum) data length for these commands */
248 	switch (srb->cmnd[0]) {
249 
250 		/* for INQUIRY, UFI devices only ever return 36 bytes */
251 	case INQUIRY:
252 		srb->cmnd[4] = 36;
253 		break;
254 
255 		/* change MODE_SENSE/MODE_SELECT from 6 to 10 byte commands */
256 	case MODE_SENSE:
257 	case MODE_SELECT:
258 		/* save the command so we can tell what it was */
259 		old_cmnd = srb->cmnd[0];
260 
261 		srb->cmnd[11] = 0;
262 		srb->cmnd[10] = 0;
263 		srb->cmnd[9] = 0;
264 
265 		/* if we're sending data, we send all.	If getting data,
266 		 * get the minimum */
267 		if (srb->cmnd[0] == MODE_SELECT)
268 			srb->cmnd[8] = srb->cmnd[4];
269 		else
270 			srb->cmnd[8] = 8;
271 
272 		srb->cmnd[7] = 0;
273 		srb->cmnd[6] = 0;
274 		srb->cmnd[5] = 0;
275 		srb->cmnd[4] = 0;
276 		srb->cmnd[3] = 0;
277 		srb->cmnd[2] = srb->cmnd[2];
278 		srb->cmnd[1] = srb->cmnd[1];
279 		srb->cmnd[0] = srb->cmnd[0] | 0x40;
280 		break;
281 
282 		/* again, for MODE_SENSE_10, we get the minimum (8) */
283 	case MODE_SENSE_10:
284 		srb->cmnd[7] = 0;
285 		srb->cmnd[8] = 8;
286 		break;
287 
288 		/* for REQUEST_SENSE, UFI devices only ever return 18 bytes */
289 	case REQUEST_SENSE:
290 		srb->cmnd[4] = 18;
291 		break;
292 
293 		/* change READ_6/WRITE_6 to READ_10/WRITE_10, which
294 		 * are UFI commands */
295 	case WRITE_6:
296 	case READ_6:
297 		srb->cmnd[11] = 0;
298 		srb->cmnd[10] = 0;
299 		srb->cmnd[9] = 0;
300 		srb->cmnd[8] = srb->cmnd[4];
301 		srb->cmnd[7] = 0;
302 		srb->cmnd[6] = 0;
303 		srb->cmnd[5] = srb->cmnd[3];
304 		srb->cmnd[4] = srb->cmnd[2];
305 		srb->cmnd[3] = srb->cmnd[1] & 0x1F;
306 		srb->cmnd[2] = 0;
307 		srb->cmnd[1] = srb->cmnd[1] & 0xE0;
308 		srb->cmnd[0] = srb->cmnd[0] | 0x20;
309 		break;
310 	} /* end switch on cmnd[0] */
311 
312 	/* convert MODE_SELECT data here */
313 	if (old_cmnd == MODE_SELECT)
314 		usb_stor_scsiSense6to10(srb);
315 
316 	/* send the command to the transport layer */
317 	usb_stor_invoke_transport(srb, us);
318 	if (srb->result == GOOD << 1) {
319 
320 		/* Fix the MODE_SENSE data if we translated the command */
321 		if (old_cmnd == MODE_SENSE)
322 			usb_stor_scsiSense10to6(srb);
323 
324 		/* Fix the data for an INQUIRY, if necessary */
325 		fix_inquiry_data(srb);
326 	}
327 }
328 
usb_stor_transparent_scsi_command(Scsi_Cmnd * srb,struct us_data * us)329 void usb_stor_transparent_scsi_command(Scsi_Cmnd *srb, struct us_data *us)
330 {
331 	int old_cmnd = 0;
332 
333 	/* This code supports devices which do not support {READ|WRITE}_6
334 	 * Apparently, neither Windows or MacOS will use these commands,
335 	 * so some devices do not support them
336 	 */
337 	if (us->flags & US_FL_MODE_XLATE) {
338 		US_DEBUGP("Invoking Mode Translation\n");
339 		/* save the old command for later */
340 		old_cmnd = srb->cmnd[0];
341 
342 		switch (srb->cmnd[0]) {
343 		/* change READ_6/WRITE_6 to READ_10/WRITE_10 */
344 		case WRITE_6:
345 		case READ_6:
346 			srb->cmd_len = 12;
347 			srb->cmnd[11] = 0;
348 			srb->cmnd[10] = 0;
349 			srb->cmnd[9] = 0;
350 			srb->cmnd[8] = srb->cmnd[4];
351 			srb->cmnd[7] = 0;
352 			srb->cmnd[6] = 0;
353 			srb->cmnd[5] = srb->cmnd[3];
354 			srb->cmnd[4] = srb->cmnd[2];
355 			srb->cmnd[3] = srb->cmnd[1] & 0x1F;
356 			srb->cmnd[2] = 0;
357 			srb->cmnd[1] = srb->cmnd[1] & 0xE0;
358 			srb->cmnd[0] = srb->cmnd[0] | 0x20;
359 			break;
360 
361 		/* convert MODE_SELECT data here */
362 		case MODE_SENSE:
363 		case MODE_SELECT:
364 			srb->cmd_len = 12;
365 			srb->cmnd[11] = 0;
366 			srb->cmnd[10] = 0;
367 			srb->cmnd[9] = 0;
368 			srb->cmnd[8] = srb->cmnd[4];
369 			srb->cmnd[7] = 0;
370 			srb->cmnd[6] = 0;
371 			srb->cmnd[5] = 0;
372 			srb->cmnd[4] = 0;
373 			srb->cmnd[3] = 0;
374 			srb->cmnd[2] = srb->cmnd[2];
375 			srb->cmnd[1] = srb->cmnd[1];
376 			srb->cmnd[0] = srb->cmnd[0] | 0x40;
377 			break;
378 		} /* switch (srb->cmnd[0]) */
379 	} /* if (us->flags & US_FL_MODE_XLATE) */
380 
381 	/* convert MODE_SELECT data here */
382 	if ((us->flags & US_FL_MODE_XLATE) && (old_cmnd == MODE_SELECT))
383 		usb_stor_scsiSense6to10(srb);
384 
385 	/* send the command to the transport layer */
386 	usb_stor_invoke_transport(srb, us);
387 	if (srb->result == GOOD << 1) {
388 
389 		/* Fix the MODE_SENSE data if we translated the command */
390 		if ((us->flags & US_FL_MODE_XLATE) && (old_cmnd == MODE_SENSE))
391 			usb_stor_scsiSense10to6(srb);
392 
393 		/* fix the INQUIRY data if necessary */
394 		fix_inquiry_data(srb);
395 
396 		/* Fix the READ CAPACITY result if necessary */
397 		if (us->flags & US_FL_FIX_CAPACITY)
398 			fix_read_capacity(srb);
399 	}
400 }
401 
402