summaryrefslogtreecommitdiffstats
path: root/uClinux-2.4.20-uc1/drivers/usb/storage/protocol.c
blob: 2b7b5f7337114c40221b4f407d24b2b99bf2345e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/* Driver for USB Mass Storage compliant devices
 *
 * $Id: protocol.c,v 1.13 2002/02/25 00:34:56 mdharm Exp $
 *
 * Current development and maintenance by:
 *   (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
 *
 * Developed with the assistance of:
 *   (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
 *   (c) 2002 Alan Stern (stern@rowland.org)
 *
 * Initial work by:
 *   (c) 1999 Michael Gee (michael@linuxspecific.com)
 *
 * This driver is based on the 'USB Mass Storage Class' document. This
 * describes in detail the protocol used to communicate with such
 * devices.  Clearly, the designers had SCSI and ATAPI commands in
 * mind when they created this document.  The commands are all very
 * similar to commands in the SCSI-II and ATAPI specifications.
 *
 * It is important to note that in a number of cases this class
 * exhibits class-specific exemptions from the USB specification.
 * Notably the usage of NAK, STALL and ACK differs from the norm, in
 * that they are used to communicate wait, failed and OK on commands.
 *
 * Also, for certain devices, the interrupt endpoint is used to convey
 * status of a command.
 *
 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
 * information about this driver.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "protocol.h"
#include "usb.h"
#include "debug.h"
#include "scsiglue.h"
#include "transport.h"

/***********************************************************************
 * Helper routines
 ***********************************************************************/

/* Fix-up the return data from an INQUIRY command to show 
 * ANSI SCSI rev 2 so we don't confuse the SCSI layers above us
 */
void fix_inquiry_data(Scsi_Cmnd *srb)
{
	unsigned char *data_ptr;

	/* verify that it's an INQUIRY command */
	if (srb->cmnd[0] != INQUIRY)
		return;

	US_DEBUGP("Fixing INQUIRY data to show SCSI rev 2\n");

	/* find the location of the data */
	if (srb->use_sg) {
		struct scatterlist *sg;

		sg = (struct scatterlist *) srb->request_buffer;
		data_ptr = (unsigned char *) sg[0].address;
	} else
		data_ptr = (unsigned char *)srb->request_buffer;

	/* Change the SCSI revision number */
	data_ptr[2] = (data_ptr[2] & ~7) | 2;
}

/***********************************************************************
 * Protocol routines
 ***********************************************************************/

void usb_stor_qic157_command(Scsi_Cmnd *srb, struct us_data *us)
{
	/* Pad the ATAPI command with zeros 
	 * NOTE: This only works because a Scsi_Cmnd struct field contains
	 * a unsigned char cmnd[12], so we know we have storage available
	 */
	for (; srb->cmd_len<12; srb->cmd_len++)
#ifndef CONFIG_BOARD_W90N745		
		srb->cmnd[srb->cmd_len] = 0;
#else
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[srb->cmd_len] = 0;	
#endif		

	/* set command length to 12 bytes */
	srb->cmd_len = 12;

	/* send the command to the transport layer */
	usb_stor_invoke_transport(srb, us);
	if (srb->result == GOOD << 1) {

		/* fix the INQUIRY data if necessary */
		fix_inquiry_data(srb);
	}
}

void usb_stor_ATAPI_command(Scsi_Cmnd *srb, struct us_data *us)
{
	int old_cmnd = 0;

	/* Fix some commands -- this is a form of mode translation
	 * ATAPI devices only accept 12 byte long commands 
	 *
	 * NOTE: This only works because a Scsi_Cmnd struct field contains
	 * a unsigned char cmnd[12], so we know we have storage available
	 */

	/* Pad the ATAPI command with zeros */
	for (; srb->cmd_len<12; srb->cmd_len++)
#ifndef CONFIG_BOARD_W90N745
		srb->cmnd[srb->cmd_len] = 0;
#else
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[srb->cmd_len] = 0;
#endif		

	/* set command length to 12 bytes */
	srb->cmd_len = 12;

	/* determine the correct (or minimum) data length for these commands */
	switch (srb->cmnd[0]) {

		/* change MODE_SENSE/MODE_SELECT from 6 to 10 byte commands */
	case MODE_SENSE:
	case MODE_SELECT:
		/* save the command so we can tell what it was */
		old_cmnd = srb->cmnd[0];

#ifndef CONFIG_BOARD_W90N745
		srb->cmnd[11] = 0;
		srb->cmnd[10] = 0;
		srb->cmnd[9] = 0;
		srb->cmnd[8] = srb->cmnd[4];
		srb->cmnd[7] = 0;
		srb->cmnd[6] = 0;
		srb->cmnd[5] = 0;
		srb->cmnd[4] = 0;
		srb->cmnd[3] = 0;
		srb->cmnd[2] = srb->cmnd[2];
		srb->cmnd[1] = srb->cmnd[1];
		srb->cmnd[0] = srb->cmnd[0] | 0x40;
#else
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[11] = 0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[10] = 0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[9] = 0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[8] = srb->cmnd[4];
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[7] = 0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[6] = 0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[5] = 0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[4] = 0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[3] = 0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[2] = srb->cmnd[2];
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[1] = srb->cmnd[1];
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[0] = srb->cmnd[0] | 0x40;
#endif		
		break;

		/* change READ_6/WRITE_6 to READ_10/WRITE_10, which 
		 * are ATAPI commands */
	case WRITE_6:
	case READ_6:
#ifndef CONFIG_BOARD_W90N745
		srb->cmnd[11] = 0;
		srb->cmnd[10] = 0;
		srb->cmnd[9] = 0;
		srb->cmnd[8] = srb->cmnd[4];
		srb->cmnd[7] = 0;
		srb->cmnd[6] = 0;
		srb->cmnd[5] = srb->cmnd[3];
		srb->cmnd[4] = srb->cmnd[2];
		srb->cmnd[3] = srb->cmnd[1] & 0x1F;
		srb->cmnd[2] = 0;
		srb->cmnd[1] = srb->cmnd[1] & 0xE0;
		srb->cmnd[0] = srb->cmnd[0] | 0x20;
#else
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[11] = 0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[10] = 0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[9] = 0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[8] = srb->cmnd[4];
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[7] = 0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[6] = 0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[5] = srb->cmnd[3];
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[4] = srb->cmnd[2];
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[3] = srb->cmnd[1] & 0x1F;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[2] = 0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[1] = srb->cmnd[1] & 0xE0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[0] = srb->cmnd[0] | 0x20;
#endif		
		break;
	} /* end switch on cmnd[0] */

	/* convert MODE_SELECT data here */
	if (old_cmnd == MODE_SELECT)
		usb_stor_scsiSense6to10(srb);

	/* send the command to the transport layer */
	usb_stor_invoke_transport(srb, us);
	if (srb->result == GOOD << 1) {

		/* Fix the MODE_SENSE data if we translated the command */
		if (old_cmnd == MODE_SENSE)
			usb_stor_scsiSense10to6(srb);

		/* fix the INQUIRY data if necessary */
		fix_inquiry_data(srb);
	}
}


void usb_stor_ufi_command(Scsi_Cmnd *srb, struct us_data *us)
{
	int old_cmnd = 0;

	/* fix some commands -- this is a form of mode translation
	 * UFI devices only accept 12 byte long commands 
	 *
	 * NOTE: This only works because a Scsi_Cmnd struct field contains
	 * a unsigned char cmnd[12], so we know we have storage available
	 */

	/* set command length to 12 bytes (this affects the transport layer) */
	srb->cmd_len = 12;

	/* determine the correct (or minimum) data length for these commands */
	switch (srb->cmnd[0]) {

		/* for INQUIRY, UFI devices only ever return 36 bytes */
	case INQUIRY:
#ifndef CONFIG_BOARD_W90N745		
		srb->cmnd[4] = 36;
#else
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[4] = 36;
#endif		
		break;

		/* change MODE_SENSE/MODE_SELECT from 6 to 10 byte commands */
	case MODE_SENSE:
	case MODE_SELECT:
		/* save the command so we can tell what it was */
		old_cmnd = srb->cmnd[0];

#ifndef CONFIG_BOARD_W90N745
		srb->cmnd[11] = 0;
		srb->cmnd[10] = 0;
		srb->cmnd[9] = 0;
#else
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[11] = 0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[10] = 0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[9] = 0;
#endif		

		/* if we're sending data, we send all.	If getting data, 
		 * get the minimum */
#ifndef CONFIG_BOARD_W90N745		
		if (srb->cmnd[0] == MODE_SELECT)
			srb->cmnd[8] = srb->cmnd[4];
		else
			srb->cmnd[8] = 8;

		srb->cmnd[7] = 0;
		srb->cmnd[6] = 0;
		srb->cmnd[5] = 0;
		srb->cmnd[4] = 0;
		srb->cmnd[3] = 0;
		srb->cmnd[2] = srb->cmnd[2];
		srb->cmnd[1] = srb->cmnd[1];
		srb->cmnd[0] = srb->cmnd[0] | 0x40;
#else
		if (srb->cmnd[0] == MODE_SELECT)
			((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[8] = srb->cmnd[4];
		else
			((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[8] = 8;

		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[7] = 0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[6] = 0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[5] = 0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[4] = 0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[3] = 0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[2] = srb->cmnd[2];
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[1] = srb->cmnd[1];
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[0] = srb->cmnd[0] | 0x40;
#endif		
		break;

		/* again, for MODE_SENSE_10, we get the minimum (8) */
	case MODE_SENSE_10:
		srb->cmnd[7] = 0;
		srb->cmnd[8] = 8;
		break;

		/* for REQUEST_SENSE, UFI devices only ever return 18 bytes */
	case REQUEST_SENSE:
#ifndef CONFIG_BOARD_W90N745		
		srb->cmnd[4] = 18;
#else
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[4] = 18;
#endif		
		break;

		/* change READ_6/WRITE_6 to READ_10/WRITE_10, which 
		 * are UFI commands */
	case WRITE_6:
	case READ_6:
#ifndef CONFIG_BOARD_W90N745	
		srb->cmnd[11] = 0;
		srb->cmnd[10] = 0;
		srb->cmnd[9] = 0;
		srb->cmnd[8] = srb->cmnd[4];
		srb->cmnd[7] = 0;
		srb->cmnd[6] = 0;
		srb->cmnd[5] = srb->cmnd[3];
		srb->cmnd[4] = srb->cmnd[2];
		srb->cmnd[3] = srb->cmnd[1] & 0x1F;
		srb->cmnd[2] = 0;
		srb->cmnd[1] = srb->cmnd[1] & 0xE0;
		srb->cmnd[0] = srb->cmnd[0] | 0x20;
#else
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[11] = 0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[10] = 0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[9] = 0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[8] = srb->cmnd[4];
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[7] = 0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[6] = 0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[5] = srb->cmnd[3];
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[4] = srb->cmnd[2];
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[3] = srb->cmnd[1] & 0x1F;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[2] = 0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[1] = srb->cmnd[1] & 0xE0;
		((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[0] = srb->cmnd[0] | 0x20;
#endif		
		break;
	} /* end switch on cmnd[0] */

	/* convert MODE_SELECT data here */
	if (old_cmnd == MODE_SELECT)
		usb_stor_scsiSense6to10(srb);

	/* send the command to the transport layer */
	usb_stor_invoke_transport(srb, us);
	if (srb->result == GOOD << 1) {

		/* Fix the MODE_SENSE data if we translated the command */
		if (old_cmnd == MODE_SENSE)
			usb_stor_scsiSense10to6(srb);

		/* Fix the data for an INQUIRY, if necessary */
		fix_inquiry_data(srb);
	}
}

void usb_stor_transparent_scsi_command(Scsi_Cmnd *srb, struct us_data *us)
{
	int old_cmnd = 0;

	/* This code supports devices which do not support {READ|WRITE}_6
	 * Apparently, neither Windows or MacOS will use these commands,
	 * so some devices do not support them
	 */
	if (us->flags & US_FL_MODE_XLATE) {
		US_DEBUGP("Invoking Mode Translation\n");
		/* save the old command for later */
		old_cmnd = srb->cmnd[0];

		switch (srb->cmnd[0]) {
		/* change READ_6/WRITE_6 to READ_10/WRITE_10 */
		case WRITE_6:
		case READ_6:
			srb->cmd_len = 12;
#ifndef CONFIG_BOARD_W90N745 					
			srb->cmnd[11] = 0;
			srb->cmnd[10] = 0;
			srb->cmnd[9] = 0;
			srb->cmnd[8] = srb->cmnd[4];
			srb->cmnd[7] = 0;
			srb->cmnd[6] = 0;
			srb->cmnd[5] = srb->cmnd[3];
			srb->cmnd[4] = srb->cmnd[2];
			srb->cmnd[3] = srb->cmnd[1] & 0x1F;
			srb->cmnd[2] = 0;
			srb->cmnd[1] = srb->cmnd[1] & 0xE0;
			srb->cmnd[0] = srb->cmnd[0] | 0x20;
#else
			((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[11] = 0;
			((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[10] = 0;
			((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[9] = 0;
			((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[8] = srb->cmnd[4];
			((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[7] = 0;
			((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[6] = 0;
			((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[5] = srb->cmnd[3];
			((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[4] = srb->cmnd[2];
			((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[3] = srb->cmnd[1] & 0x1F;
			((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[2] = 0;
			((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[1] = srb->cmnd[1] & 0xE0;
			((unsigned char *)((unsigned long)srb->cmnd |0x80000000))[0] = srb->cmnd[0] | 0x20;
#endif			
			break;

		/* convert MODE_SELECT data here */
		case MODE_SENSE:
		case MODE_SELECT:
			srb->cmd_len = 12;
#ifndef CONFIG_BOARD_W90N745
			srb->cmnd[11] = 0;
			srb->cmnd[10] = 0;
			srb->cmnd[9] = 0;
			srb->cmnd[8] = srb->cmnd[4];
			srb->cmnd[7] = 0;
			srb->cmnd[6] = 0;
			srb->cmnd[5] = 0;
			srb->cmnd[4] = 0;
			srb->cmnd[3] = 0;
			srb->cmnd[2] = srb->cmnd[2];
			srb->cmnd[1] = srb->cmnd[1];
			srb->cmnd[0] = srb->cmnd[0] | 0x40;
#else
			((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[11] = 0;
			((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[10] = 0;
			((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[9] = 0;
			((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[8] = srb->cmnd[4];
			((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[7] = 0;
			((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[6] = 0;
			((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[5] = 0;
			((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[4] = 0;
			((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[3] = 0;
			((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[2] = srb->cmnd[2];
			((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[1] = srb->cmnd[1];
			((unsigned char *)((unsigned long)srb->cmnd | 0x80000000))[0] = srb->cmnd[0] | 0x40;
#endif			
			break;
		} /* switch (srb->cmnd[0]) */
	} /* if (us->flags & US_FL_MODE_XLATE) */

	/* convert MODE_SELECT data here */
	if ((us->flags & US_FL_MODE_XLATE) && (old_cmnd == MODE_SELECT))
		usb_stor_scsiSense6to10(srb);

	/* send the command to the transport layer */
	usb_stor_invoke_transport(srb, us);
	if (srb->result == GOOD << 1) {

		/* Fix the MODE_SENSE data if we translated the command */
		if ((us->flags & US_FL_MODE_XLATE) && (old_cmnd == MODE_SENSE))
			usb_stor_scsiSense10to6(srb);

		/* fix the INQUIRY data if necessary */
		fix_inquiry_data(srb);
	}
}