summaryrefslogtreecommitdiff
path: root/minix/drivers/usb/usb_storage/scsi.c
blob: 128039f8b57ca72b45fd1e2a5de8f9e5f1e7a504 (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
/*
 * SCSI commands related implementation
 */

#include <minix/blockdriver.h>				/* SECTOR_SIZE */

#include <assert.h>
#include <string.h>					/* strncpy */

#include "common.h"
#include "scsi.h"

/*---------------------------*
 *    declared functions     *
 *---------------------------*/
/* To work correctly cbw->CBWCB must be zeroed before calling these: */
static int create_inquiry_scsi_cmd(mass_storage_cbw *);
static int create_test_scsi_cmd(mass_storage_cbw *);
static int create_read_capacity_scsi_cmd(mass_storage_cbw *);
static int create_write_scsi_cmd(mass_storage_cbw *, scsi_transfer *);
static int create_read_scsi_cmd(mass_storage_cbw *, scsi_transfer *);
static int create_mode_sense_scsi_cmd(mass_storage_cbw *);
static int create_request_sense_scsi_cmd(mass_storage_cbw *);

/*---------------------------*
 *    defined functions      *
 *---------------------------*/
/*===========================================================================*
 *    create_scsi_cmd                                                        *
 *===========================================================================*/
int
create_scsi_cmd(mass_storage_cbw * cbw, int cmd, scsi_transfer * info)
{
	MASS_DEBUG_DUMP;

	assert(NULL != cbw);

	switch (cmd) {
		case SCSI_INQUIRY:
			return create_inquiry_scsi_cmd(cbw);
		case SCSI_TEST_UNIT_READY:
			return create_test_scsi_cmd(cbw);
		case SCSI_READ_CAPACITY:
			return create_read_capacity_scsi_cmd(cbw);
		case SCSI_WRITE:
			return create_write_scsi_cmd(cbw, info);
		case SCSI_READ:
			return create_read_scsi_cmd(cbw, info);
		case SCSI_MODE_SENSE:
			return create_mode_sense_scsi_cmd(cbw);
		case SCSI_REQUEST_SENSE:
			return create_request_sense_scsi_cmd(cbw);
		default:
			MASS_MSG("Invalid SCSI command!");
			return EXIT_FAILURE;
	}
}


/*===========================================================================*
 *    create_inquiry_scsi_cmd                                                *
 *===========================================================================*/
static int
create_inquiry_scsi_cmd(mass_storage_cbw * cbw)
{
	MASS_DEBUG_DUMP;

	cbw->dCBWDataTransferLength = SCSI_INQUIRY_DATA_LEN;
	cbw->bCBWFlags = CBW_FLAGS_IN;
	cbw->bCDBLength = SCSI_INQUIRY_CMD_LEN;

	SCSI_SET_INQUIRY_OP_CODE(cbw->CBWCB);
	SCSI_SET_INQUIRY_PAGE_CODE(cbw->CBWCB, 0);
	SCSI_SET_INQUIRY_ALLOC_LEN(cbw->CBWCB, SCSI_INQUIRY_DATA_LEN);

	return EXIT_SUCCESS;
}


/*===========================================================================*
 *    create_test_scsi_cmd                                                   *
 *===========================================================================*/
static int
create_test_scsi_cmd(mass_storage_cbw * cbw)
{
	MASS_DEBUG_DUMP;

	cbw->bCDBLength = SCSI_TEST_CMD_LEN;

	SCSI_SET_TEST_OP_CODE(cbw->CBWCB);

	return EXIT_SUCCESS;
}


/*===========================================================================*
 *    create_read_capacity_scsi_cmd                                          *
 *===========================================================================*/
static int
create_read_capacity_scsi_cmd(mass_storage_cbw * cbw)
{
	MASS_DEBUG_DUMP;

	cbw->dCBWDataTransferLength = SCSI_READ_CAPACITY_DATA_LEN;
	cbw->bCBWFlags = CBW_FLAGS_IN;
	cbw->bCDBLength = SCSI_READ_CAPACITY_CMD_LEN;

	SCSI_SET_READ_CAPACITY_OP_CODE(cbw->CBWCB);
	SCSI_SET_READ_CAPACITY_LBA(cbw->CBWCB, 0x00);

	return EXIT_SUCCESS;
}


/*===========================================================================*
 *    create_write_scsi_cmd                                                  *
 *===========================================================================*/
static int
create_write_scsi_cmd(mass_storage_cbw * cbw, scsi_transfer * info)
{
	MASS_DEBUG_DUMP;

	assert(NULL != info);
	assert(0 == (info->length % SECTOR_SIZE));

	cbw->dCBWDataTransferLength = info->length;
	cbw->bCBWFlags = CBW_FLAGS_OUT;
	cbw->bCDBLength = SCSI_WRITE_CMD_LEN;

	SCSI_SET_WRITE_OP_CODE(cbw->CBWCB);
	SCSI_SET_WRITE_LBA(cbw->CBWCB, info->lba);
	SCSI_SET_WRITE_BLEN(cbw->CBWCB, info->length / SECTOR_SIZE);

	return EXIT_SUCCESS;
}


/*===========================================================================*
 *    create_read_scsi_cmd                                                   *
 *===========================================================================*/
static int
create_read_scsi_cmd(mass_storage_cbw * cbw, scsi_transfer * info)
{
	MASS_DEBUG_DUMP;

	assert(NULL != info);
	assert(0 == (info->length % SECTOR_SIZE));

	cbw->dCBWDataTransferLength = info->length;
	cbw->bCBWFlags = CBW_FLAGS_IN;
	cbw->bCDBLength = SCSI_READ_CMD_LEN;

	SCSI_SET_READ_OP_CODE(cbw->CBWCB);
	SCSI_SET_READ_LBA(cbw->CBWCB, info->lba);
	SCSI_SET_READ_BLEN(cbw->CBWCB, info->length / SECTOR_SIZE);

	return EXIT_SUCCESS;
}


/*===========================================================================*
 *    create_mode_sense_scsi_cmd                                             *
 *===========================================================================*/
static int
create_mode_sense_scsi_cmd(mass_storage_cbw * cbw)
{
	MASS_DEBUG_DUMP;

	cbw->dCBWDataTransferLength = SCSI_MODE_SENSE_FLEX_DATA_LEN;
	cbw->bCBWFlags = CBW_FLAGS_IN;
	cbw->bCDBLength = SCSI_MODE_SENSE_CMD_LEN;

	SCSI_SET_MODE_SENSE_OP_CODE(cbw->CBWCB);
	SCSI_SET_MODE_SENSE_PAGE_CODE(cbw->CBWCB,
					SCSI_MODE_SENSE_FLEXIBLE_DISK_PAGE);

	return EXIT_SUCCESS;
}


/*===========================================================================*
 *    create_request_sense_scsi_cmd                                          *
 *===========================================================================*/
static int
create_request_sense_scsi_cmd(mass_storage_cbw * cbw)
{
	MASS_DEBUG_DUMP;

	cbw->dCBWDataTransferLength = SCSI_REQUEST_SENSE_DATA_LEN;
	cbw->bCBWFlags = CBW_FLAGS_IN;
	cbw->bCDBLength = SCSI_REQUEST_SENSE_CMD_LEN;

	SCSI_SET_REQUEST_SENSE_OP_CODE(cbw->CBWCB);
	SCSI_SET_REQUEST_SENSE_ALLOC(cbw->CBWCB, SCSI_REQUEST_SENSE_DATA_LEN);

	return EXIT_SUCCESS;
}


/*===========================================================================*
 *    check_inquiry_reply                                                    *
 *===========================================================================*/
int
check_inquiry_reply(uint8_t * scsi_reply)
{
	char vendor_name[SCSI_INQUIRY_VENDOR_NAME_LEN + 1];
	char product_name[SCSI_INQUIRY_PRODUCT_NAME_LEN + 1];

	MASS_DEBUG_DUMP;

	/* Stop condition for printing as strncpy() does not add it */
	vendor_name[SCSI_INQUIRY_VENDOR_NAME_LEN] = '\0';
	product_name[SCSI_INQUIRY_PRODUCT_NAME_LEN] = '\0';

	if (SCSI_GET_INQUIRY_PERIPH_QUALIF(scsi_reply)) {
		MASS_MSG("Device not connected");
		return EXIT_FAILURE;
	}

	strncpy(vendor_name, SCSI_GET_INQUIRY_VENDOR_NAME(scsi_reply),
		SCSI_INQUIRY_VENDOR_NAME_LEN);
	strncpy(product_name, SCSI_GET_INQUIRY_PRODUCT_NAME(scsi_reply),
		SCSI_INQUIRY_PRODUCT_NAME_LEN);

	MASS_DEBUG_MSG("Vendor name: %s", vendor_name);
	MASS_DEBUG_MSG("Product name %s", product_name);

	return EXIT_SUCCESS;
}


/*===========================================================================*
 *    check_read_capacity_reply                                              *
 *===========================================================================*/
int
check_read_capacity_reply(uint8_t * scsi_reply, uint32_t * lba, uint32_t * blen)
{
	MASS_DEBUG_DUMP;

	*lba = SCSI_GET_READ_CAPACITY_LBA(scsi_reply);
	*blen = SCSI_GET_READ_CAPACITY_BLEN(scsi_reply);

	return EXIT_SUCCESS;
}


/*===========================================================================*
 *    check_mode_sense_reply                                                 *
 *===========================================================================*/
int
check_mode_sense_reply(uint8_t * scsi_reply, unsigned * cyl,
			unsigned * head, unsigned * sect)
{
	MASS_DEBUG_DUMP;

	*cyl = SCSI_GET_MODE_SENSE_CYLINDERS(scsi_reply);
	*head = SCSI_GET_MODE_SENSE_HEADS(scsi_reply);
	*sect = SCSI_GET_MODE_SENSE_SECTORS(scsi_reply);

	return EXIT_SUCCESS;
}


/*===========================================================================*
 *    check_csw                                                              *
 *===========================================================================*/
int
check_csw(mass_storage_csw * csw, unsigned int tag)
{
	MASS_DEBUG_DUMP;

	if (csw->dCSWTag != tag) {
		MASS_MSG("CSW tag mismatch!");
		return EXIT_FAILURE;
	}

	if (CSW_SIGNATURE != csw->dCSWSignature) {
		MASS_MSG("CSW signature mismatch!");
		return EXIT_FAILURE;
	}

	if (CSW_STATUS_GOOD != csw->bCSWStatus) {
		MASS_MSG("CSW status error (0x%02X)!", csw->bCSWStatus);
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;
}