| 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 |
#include <endian.h> |
| 31 |
#include <limits.h> |
| 32 |
#include <usb/usb.h> |
| 33 |
#include <usb/usbmsc.h> |
| 34 |
#include <usb/usbdisk.h> |
| 35 |
|
| 36 |
enum { |
| 37 |
msc_subclass_rbc = 0x1, |
| 38 |
msc_subclass_mmc2 = 0x2, |
| 39 |
msc_subclass_qic157 = 0x3, |
| 40 |
msc_subclass_ufi = 0x4, |
| 41 |
msc_subclass_sff8070i = 0x5, |
| 42 |
msc_subclass_scsitrans = 0x6 |
| 43 |
}; |
| 44 |
|
| 45 |
static const char *msc_subclass_strings[7] = { |
| 46 |
"(none)", |
| 47 |
"RBC", |
| 48 |
"MMC-2", |
| 49 |
"QIC-157", |
| 50 |
"UFI", |
| 51 |
"SFF-8070i", |
| 52 |
"SCSI transparent" |
| 53 |
}; |
| 54 |
enum { |
| 55 |
msc_proto_cbi_wcomp = 0x0, |
| 56 |
msc_proto_cbi_wocomp = 0x1, |
| 57 |
msc_proto_bulk_only = 0x50 |
| 58 |
}; |
| 59 |
static const char *msc_protocol_strings[0x51] = { |
| 60 |
"Control/Bulk/Interrupt protocol (with command completion interrupt)", |
| 61 |
"Control/Bulk/Interrupt protocol (with no command completion interrupt)", |
| 62 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 63 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 64 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 65 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 66 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 67 |
"Bulk-Only Transport" |
| 68 |
}; |
| 69 |
|
| 70 |
static void |
| 71 |
usb_msc_create_disk(usbdev_t *dev) |
| 72 |
{ |
| 73 |
if (usbdisk_create) { |
| 74 |
usbdisk_create(dev); |
| 75 |
MSC_INST(dev)->usbdisk_created = 1; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
static void |
| 80 |
usb_msc_remove_disk(usbdev_t *dev) |
| 81 |
{ |
| 82 |
if (MSC_INST(dev)->usbdisk_created && usbdisk_remove) { |
| 83 |
usbdisk_remove(dev); |
| 84 |
MSC_INST(dev)->usbdisk_created = 0; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
static void |
| 89 |
usb_msc_destroy(usbdev_t *dev) |
| 90 |
{ |
| 91 |
if (dev->data) { |
| 92 |
usb_msc_remove_disk(dev); |
| 93 |
free(dev->data); |
| 94 |
} |
| 95 |
dev->data = 0; |
| 96 |
} |
| 97 |
|
| 98 |
const int DEV_RESET = 0xff; |
| 99 |
const int GET_MAX_LUN = 0xfe; |
| 100 |
|
| 101 |
|
| 102 |
const int MAX_CHUNK_BYTES = 1024 * 64; |
| 103 |
|
| 104 |
const unsigned int cbw_signature = 0x43425355; |
| 105 |
const unsigned int csw_signature = 0x53425355; |
| 106 |
|
| 107 |
typedef struct { |
| 108 |
unsigned int dCBWSignature; |
| 109 |
unsigned int dCBWTag; |
| 110 |
unsigned int dCBWDataTransferLength; |
| 111 |
unsigned char bmCBWFlags; |
| 112 |
unsigned long bCBWLUN:4; |
| 113 |
unsigned long:4; |
| 114 |
unsigned long bCBWCBLength:5; |
| 115 |
unsigned long:3; |
| 116 |
unsigned char CBWCB[31 - 15]; |
| 117 |
} __packed cbw_t; |
| 118 |
|
| 119 |
typedef struct { |
| 120 |
unsigned int dCSWSignature; |
| 121 |
unsigned int dCSWTag; |
| 122 |
unsigned int dCSWDataResidue; |
| 123 |
unsigned char bCSWStatus; |
| 124 |
} __packed csw_t; |
| 125 |
|
| 126 |
enum { |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
MSC_COMMAND_OK = 0, MSC_COMMAND_FAIL, MSC_COMMAND_DETACHED |
| 137 |
}; |
| 138 |
|
| 139 |
static int |
| 140 |
request_sense(usbdev_t *dev); |
| 141 |
static int |
| 142 |
request_sense_no_media(usbdev_t *dev); |
| 143 |
static void |
| 144 |
usb_msc_poll(usbdev_t *dev); |
| 145 |
|
| 146 |
static int |
| 147 |
reset_transport(usbdev_t *dev) |
| 148 |
{ |
| 149 |
dev_req_t dr; |
| 150 |
memset(&dr, 0, sizeof(dr)); |
| 151 |
dr.bmRequestType = 0; |
| 152 |
dr.data_dir = host_to_device; |
| 153 |
#ifndef QEMU |
| 154 |
dr.req_type = class_type; |
| 155 |
dr.req_recp = iface_recp; |
| 156 |
#endif |
| 157 |
dr.bRequest = DEV_RESET; |
| 158 |
dr.wValue = 0; |
| 159 |
dr.wIndex = 0; |
| 160 |
dr.wLength = 0; |
| 161 |
|
| 162 |
if (MSC_INST(dev)->quirks & USB_MSC_QUIRK_NO_RESET) |
| 163 |
return MSC_COMMAND_FAIL; |
| 164 |
|
| 165 |
|
| 166 |
if (dev->controller->control(dev, OUT, sizeof(dr), &dr, 0, 0) < 0 || |
| 167 |
clear_stall(MSC_INST(dev)->bulk_in) || |
| 168 |
clear_stall(MSC_INST(dev)->bulk_out)) { |
| 169 |
usb_debug("Detaching unresponsive device.\n"); |
| 170 |
usb_detach_device(dev->controller, dev->address); |
| 171 |
return MSC_COMMAND_DETACHED; |
| 172 |
} |
| 173 |
|
| 174 |
return MSC_COMMAND_FAIL; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
static void |
| 179 |
initialize_luns(usbdev_t *dev) |
| 180 |
{ |
| 181 |
usbmsc_inst_t *msc = MSC_INST(dev); |
| 182 |
dev_req_t dr; |
| 183 |
dr.bmRequestType = 0; |
| 184 |
dr.data_dir = device_to_host; |
| 185 |
#ifndef QEMU |
| 186 |
dr.req_type = class_type; |
| 187 |
dr.req_recp = iface_recp; |
| 188 |
#endif |
| 189 |
dr.bRequest = GET_MAX_LUN; |
| 190 |
dr.wValue = 0; |
| 191 |
dr.wIndex = 0; |
| 192 |
dr.wLength = 1; |
| 193 |
if (MSC_INST(dev)->quirks & USB_MSC_QUIRK_NO_LUNS || |
| 194 |
dev->controller->control(dev, IN, sizeof(dr), &dr, |
| 195 |
sizeof(msc->num_luns), &msc->num_luns) < 0) |
| 196 |
msc->num_luns = 0; |
| 197 |
msc->num_luns++; |
| 198 |
msc->lun = 0; |
| 199 |
} |
| 200 |
|
| 201 |
unsigned int tag; |
| 202 |
|
| 203 |
static void |
| 204 |
wrap_cbw(cbw_t *cbw, int datalen, cbw_direction dir, const u8 *cmd, |
| 205 |
int cmdlen, u8 lun) |
| 206 |
{ |
| 207 |
memset(cbw, 0, sizeof(cbw_t)); |
| 208 |
|
| 209 |
|
| 210 |
if (cmdlen > sizeof(cbw->CBWCB)) { |
| 211 |
cmdlen = sizeof(cbw->CBWCB); |
| 212 |
} |
| 213 |
|
| 214 |
cbw->dCBWSignature = cbw_signature; |
| 215 |
cbw->dCBWTag = ++tag; |
| 216 |
cbw->bCBWLUN = lun; |
| 217 |
|
| 218 |
cbw->dCBWDataTransferLength = datalen; |
| 219 |
cbw->bmCBWFlags = dir; |
| 220 |
memcpy(cbw->CBWCB, cmd, cmdlen); |
| 221 |
cbw->bCBWCBLength = cmdlen; |
| 222 |
} |
| 223 |
|
| 224 |
static int |
| 225 |
get_csw(endpoint_t *ep, csw_t *csw) |
| 226 |
{ |
| 227 |
hci_t *ctrlr = ep->dev->controller; |
| 228 |
int ret = ctrlr->bulk(ep, sizeof(csw_t), (u8 *) csw, 1); |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
if (ret == 0) |
| 233 |
ret = ctrlr->bulk(ep, sizeof(csw_t), (u8 *) csw, 1); |
| 234 |
|
| 235 |
if (ret < 0) { |
| 236 |
clear_stall(ep); |
| 237 |
ret = ctrlr->bulk(ep, sizeof(csw_t), (u8 *) csw, 1); |
| 238 |
if (ret < 0) |
| 239 |
return reset_transport(ep->dev); |
| 240 |
} |
| 241 |
if (ret != sizeof(csw_t) || csw->dCSWTag != tag || |
| 242 |
csw->dCSWSignature != csw_signature) { |
| 243 |
usb_debug("MSC: received malformed CSW\n"); |
| 244 |
return reset_transport(ep->dev); |
| 245 |
} |
| 246 |
return MSC_COMMAND_OK; |
| 247 |
} |
| 248 |
|
| 249 |
static int |
| 250 |
execute_command(usbdev_t *dev, cbw_direction dir, const u8 *cb, int cblen, |
| 251 |
u8 *buf, int buflen, int residue_ok) |
| 252 |
{ |
| 253 |
cbw_t cbw; |
| 254 |
csw_t csw; |
| 255 |
|
| 256 |
int always_succeed = 0; |
| 257 |
if ((cb[0] == 0x1b) && (cb[4] == 1)) { |
| 258 |
always_succeed = 1; |
| 259 |
} |
| 260 |
wrap_cbw(&cbw, buflen, dir, cb, cblen, MSC_INST(dev)->lun); |
| 261 |
if (dev->controller-> |
| 262 |
bulk(MSC_INST(dev)->bulk_out, sizeof(cbw), (u8 *) &cbw, 0) < 0) { |
| 263 |
return reset_transport(dev); |
| 264 |
} |
| 265 |
if (buflen > 0) { |
| 266 |
if (dir == cbw_direction_data_in) { |
| 267 |
if (dev->controller-> |
| 268 |
bulk(MSC_INST(dev)->bulk_in, buflen, buf, 0) < 0) |
| 269 |
clear_stall(MSC_INST(dev)->bulk_in); |
| 270 |
} else { |
| 271 |
if (dev->controller-> |
| 272 |
bulk(MSC_INST(dev)->bulk_out, buflen, buf, 0) < 0) |
| 273 |
clear_stall(MSC_INST(dev)->bulk_out); |
| 274 |
} |
| 275 |
} |
| 276 |
int ret = get_csw(MSC_INST(dev)->bulk_in, &csw); |
| 277 |
if (ret) { |
| 278 |
return ret; |
| 279 |
} else if (always_succeed == 1) { |
| 280 |
|
| 281 |
return MSC_COMMAND_OK; |
| 282 |
} else if (csw.bCSWStatus == 2) { |
| 283 |
|
| 284 |
return reset_transport(dev); |
| 285 |
} else if (csw.bCSWStatus == 0) { |
| 286 |
if ((csw.dCSWDataResidue == 0) || residue_ok) |
| 287 |
|
| 288 |
return MSC_COMMAND_OK; |
| 289 |
else |
| 290 |
|
| 291 |
return MSC_COMMAND_FAIL; |
| 292 |
} else { |
| 293 |
if (cb[0] == 0x03) |
| 294 |
|
| 295 |
return MSC_COMMAND_FAIL; |
| 296 |
else if (cb[0] == 0) |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
return request_sense_no_media(dev); |
| 301 |
|
| 302 |
ret = request_sense(dev); |
| 303 |
|
| 304 |
return ret ? ret : MSC_COMMAND_FAIL; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
typedef struct { |
| 309 |
unsigned char command; |
| 310 |
unsigned char res1; |
| 311 |
unsigned int block; |
| 312 |
unsigned char res2; |
| 313 |
unsigned short numblocks; |
| 314 |
unsigned char control; |
| 315 |
} __packed cmdblock_t; |
| 316 |
|
| 317 |
typedef struct { |
| 318 |
unsigned char command; |
| 319 |
unsigned char res1; |
| 320 |
unsigned char res2; |
| 321 |
unsigned char res3; |
| 322 |
union { |
| 323 |
struct { |
| 324 |
unsigned long start:1; |
| 325 |
unsigned long:7; |
| 326 |
}; |
| 327 |
unsigned char length; |
| 328 |
}; |
| 329 |
unsigned char control; |
| 330 |
} __packed cmdblock6_t; |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
int |
| 346 |
readwrite_blocks_512(usbdev_t *dev, int start, int n, |
| 347 |
cbw_direction dir, u8 *buf) |
| 348 |
{ |
| 349 |
int blocksize_divider = MSC_INST(dev)->blocksize / 512; |
| 350 |
return readwrite_blocks(dev, start / blocksize_divider, |
| 351 |
n / blocksize_divider, dir, buf); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
static int |
| 367 |
readwrite_chunk(usbdev_t *dev, int start, int n, cbw_direction dir, u8 *buf) |
| 368 |
{ |
| 369 |
cmdblock_t cb; |
| 370 |
memset(&cb, 0, sizeof(cb)); |
| 371 |
if (dir == cbw_direction_data_in) { |
| 372 |
|
| 373 |
cb.command = 0x28; |
| 374 |
} else { |
| 375 |
|
| 376 |
cb.command = 0x2a; |
| 377 |
} |
| 378 |
cb.block = htonl(start); |
| 379 |
cb.numblocks = htonw(n); |
| 380 |
|
| 381 |
return execute_command(dev, dir, (u8 *) &cb, sizeof(cb), buf, |
| 382 |
n * MSC_INST(dev)->blocksize, 0) |
| 383 |
!= MSC_COMMAND_OK ? 1 : 0; |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
int |
| 403 |
readwrite_blocks(usbdev_t *dev, int start, int n, cbw_direction dir, u8 *buf) |
| 404 |
{ |
| 405 |
int chunk_size = MAX_CHUNK_BYTES / MSC_INST(dev)->blocksize; |
| 406 |
int chunk; |
| 407 |
|
| 408 |
|
| 409 |
for (chunk = 0; chunk < (n / chunk_size); chunk++) { |
| 410 |
if (readwrite_chunk(dev, start + (chunk * chunk_size), |
| 411 |
chunk_size, dir, |
| 412 |
buf + (chunk * MAX_CHUNK_BYTES)) |
| 413 |
!= MSC_COMMAND_OK) |
| 414 |
return 1; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
if (n % chunk_size) { |
| 419 |
if (readwrite_chunk(dev, start + (chunk * chunk_size), |
| 420 |
n % chunk_size, dir, |
| 421 |
buf + (chunk * MAX_CHUNK_BYTES)) |
| 422 |
!= MSC_COMMAND_OK) |
| 423 |
return 1; |
| 424 |
} |
| 425 |
|
| 426 |
return 0; |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
static int |
| 433 |
request_sense(usbdev_t *dev) |
| 434 |
{ |
| 435 |
u8 buf[19]; |
| 436 |
cmdblock6_t cb; |
| 437 |
memset(&cb, 0, sizeof(cb)); |
| 438 |
cb.command = 0x3; |
| 439 |
cb.length = sizeof(buf); |
| 440 |
|
| 441 |
return execute_command(dev, cbw_direction_data_in, (u8 *) &cb, |
| 442 |
sizeof(cb), buf, sizeof(buf), 1); |
| 443 |
} |
| 444 |
|
| 445 |
static int request_sense_no_media(usbdev_t *dev) |
| 446 |
{ |
| 447 |
u8 buf[19]; |
| 448 |
int ret; |
| 449 |
cmdblock6_t cb; |
| 450 |
memset(&cb, 0, sizeof(cb)); |
| 451 |
cb.command = 0x3; |
| 452 |
cb.length = sizeof(buf); |
| 453 |
|
| 454 |
ret = execute_command(dev, cbw_direction_data_in, (u8 *) &cb, |
| 455 |
sizeof(cb), buf, sizeof(buf), 1); |
| 456 |
|
| 457 |
if (ret) |
| 458 |
return ret; |
| 459 |
|
| 460 |
|
| 461 |
if ((buf[2] & 0xf) != 2) |
| 462 |
return MSC_COMMAND_FAIL; |
| 463 |
|
| 464 |
|
| 465 |
if (buf[12] != 0x3a) |
| 466 |
return MSC_COMMAND_FAIL; |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
usb_debug("Empty media found.\n"); |
| 471 |
MSC_INST(dev)->ready = USB_MSC_NOT_READY; |
| 472 |
return MSC_COMMAND_OK; |
| 473 |
} |
| 474 |
|
| 475 |
static int |
| 476 |
test_unit_ready(usbdev_t *dev) |
| 477 |
{ |
| 478 |
cmdblock6_t cb; |
| 479 |
memset(&cb, 0, sizeof(cb)); |
| 480 |
return execute_command(dev, cbw_direction_data_out, (u8 *) &cb, |
| 481 |
sizeof(cb), 0, 0, 0); |
| 482 |
} |
| 483 |
|
| 484 |
static int |
| 485 |
spin_up(usbdev_t *dev) |
| 486 |
{ |
| 487 |
cmdblock6_t cb; |
| 488 |
memset(&cb, 0, sizeof(cb)); |
| 489 |
cb.command = 0x1b; |
| 490 |
cb.start = 1; |
| 491 |
return execute_command(dev, cbw_direction_data_out, (u8 *) &cb, |
| 492 |
sizeof(cb), 0, 0, 0); |
| 493 |
} |
| 494 |
|
| 495 |
static int |
| 496 |
read_capacity(usbdev_t *dev) |
| 497 |
{ |
| 498 |
cmdblock_t cb; |
| 499 |
memset(&cb, 0, sizeof(cb)); |
| 500 |
cb.command = 0x25; |
| 501 |
u32 buf[2]; |
| 502 |
|
| 503 |
usb_debug("Reading capacity of mass storage device.\n"); |
| 504 |
int count = 0, ret; |
| 505 |
while (count++ < 20) { |
| 506 |
switch (ret = execute_command |
| 507 |
(dev, cbw_direction_data_in, (u8 *) &cb, |
| 508 |
sizeof(cb), (u8 *)buf, 8, 0)) { |
| 509 |
case MSC_COMMAND_OK: |
| 510 |
break; |
| 511 |
case MSC_COMMAND_FAIL: |
| 512 |
continue; |
| 513 |
default: |
| 514 |
return ret; |
| 515 |
} |
| 516 |
break; |
| 517 |
} |
| 518 |
if (count >= 20) { |
| 519 |
|
| 520 |
usb_debug(" assuming 2 TB with 512-byte sectors as READ CAPACITY didn't answer.\n"); |
| 521 |
MSC_INST(dev)->numblocks = 0xffffffff; |
| 522 |
MSC_INST(dev)->blocksize = 512; |
| 523 |
} else { |
| 524 |
MSC_INST(dev)->numblocks = MIN(ntohl(buf[0]), UINT_MAX - 1) + 1; |
| 525 |
MSC_INST(dev)->blocksize = ntohl(buf[1]); |
| 526 |
} |
| 527 |
usb_debug(" %d %d-byte sectors (%d MB)\n", MSC_INST(dev)->numblocks, |
| 528 |
MSC_INST(dev)->blocksize, |
| 529 |
|
| 530 |
MSC_INST(dev)->numblocks > 1000000 |
| 531 |
? (MSC_INST(dev)->numblocks / 1000) * MSC_INST(dev)->blocksize / 1000 : |
| 532 |
MSC_INST(dev)->numblocks * MSC_INST(dev)->blocksize / 1000 / 1000); |
| 533 |
return MSC_COMMAND_OK; |
| 534 |
} |
| 535 |
|
| 536 |
static int |
| 537 |
usb_msc_test_unit_ready(usbdev_t *dev) |
| 538 |
{ |
| 539 |
int i; |
| 540 |
time_t start_time_secs; |
| 541 |
struct timeval tv; |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
const int timeout_secs = 5; |
| 546 |
|
| 547 |
usb_debug(" Waiting for device to become ready..."); |
| 548 |
|
| 549 |
|
| 550 |
MSC_INST(dev)->ready = USB_MSC_READY; |
| 551 |
gettimeofday(&tv, NULL); |
| 552 |
start_time_secs = tv.tv_sec; |
| 553 |
|
| 554 |
while (tv.tv_sec - start_time_secs < timeout_secs) { |
| 555 |
switch (test_unit_ready(dev)) { |
| 556 |
case MSC_COMMAND_OK: |
| 557 |
break; |
| 558 |
case MSC_COMMAND_FAIL: |
| 559 |
mdelay(100); |
| 560 |
usb_debug("."); |
| 561 |
gettimeofday(&tv, NULL); |
| 562 |
continue; |
| 563 |
default: |
| 564 |
|
| 565 |
return USB_MSC_DETACHED; |
| 566 |
} |
| 567 |
break; |
| 568 |
} |
| 569 |
if (!(tv.tv_sec - start_time_secs < timeout_secs)) { |
| 570 |
usb_debug("timeout. Device not ready.\n"); |
| 571 |
MSC_INST(dev)->ready = USB_MSC_NOT_READY; |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
if (!MSC_INST(dev)->ready) |
| 578 |
return MSC_INST(dev)->ready; |
| 579 |
|
| 580 |
usb_debug("ok.\n"); |
| 581 |
|
| 582 |
usb_debug(" spin up"); |
| 583 |
for (i = 0; i < 30; i++) { |
| 584 |
usb_debug("."); |
| 585 |
switch (spin_up(dev)) { |
| 586 |
case MSC_COMMAND_OK: |
| 587 |
usb_debug(" OK."); |
| 588 |
break; |
| 589 |
case MSC_COMMAND_FAIL: |
| 590 |
mdelay(100); |
| 591 |
continue; |
| 592 |
default: |
| 593 |
|
| 594 |
return USB_MSC_DETACHED; |
| 595 |
} |
| 596 |
break; |
| 597 |
} |
| 598 |
usb_debug("\n"); |
| 599 |
|
| 600 |
if (read_capacity(dev) == MSC_COMMAND_DETACHED) |
| 601 |
return USB_MSC_DETACHED; |
| 602 |
|
| 603 |
return MSC_INST(dev)->ready; |
| 604 |
} |
| 605 |
|
| 606 |
void |
| 607 |
usb_msc_init(usbdev_t *dev) |
| 608 |
{ |
| 609 |
configuration_descriptor_t *cd = |
| 610 |
(configuration_descriptor_t *) dev->configuration; |
| 611 |
interface_descriptor_t *interface = |
| 612 |
(interface_descriptor_t *) (((char *) cd) + cd->bLength); |
| 613 |
|
| 614 |
usb_debug(" it uses %s command set\n", |
| 615 |
msc_subclass_strings[interface->bInterfaceSubClass]); |
| 616 |
usb_debug(" it uses %s protocol\n", |
| 617 |
msc_protocol_strings[interface->bInterfaceProtocol]); |
| 618 |
|
| 619 |
if (interface->bInterfaceProtocol != 0x50) { |
| 620 |
usb_debug(" Protocol not supported.\n"); |
| 621 |
usb_detach_device(dev->controller, dev->address); |
| 622 |
return; |
| 623 |
} |
| 624 |
|
| 625 |
if ((interface->bInterfaceSubClass != 2) && |
| 626 |
(interface->bInterfaceSubClass != 5) && |
| 627 |
(interface->bInterfaceSubClass != 6)) { |
| 628 |
|
| 629 |
usb_debug(" Interface SubClass not supported.\n"); |
| 630 |
usb_detach_device(dev->controller, dev->address); |
| 631 |
return; |
| 632 |
} |
| 633 |
|
| 634 |
usb_msc_force_init(dev, 0); |
| 635 |
} |
| 636 |
|
| 637 |
void usb_msc_force_init(usbdev_t *dev, u32 quirks) |
| 638 |
{ |
| 639 |
int i; |
| 640 |
|
| 641 |
|
| 642 |
dev->data = NULL; |
| 643 |
|
| 644 |
dev->destroy = usb_msc_destroy; |
| 645 |
dev->poll = usb_msc_poll; |
| 646 |
|
| 647 |
dev->data = malloc(sizeof(usbmsc_inst_t)); |
| 648 |
if (!dev->data) |
| 649 |
fatal("Not enough memory for USB MSC device.\n"); |
| 650 |
|
| 651 |
MSC_INST(dev)->bulk_in = 0; |
| 652 |
MSC_INST(dev)->bulk_out = 0; |
| 653 |
MSC_INST(dev)->usbdisk_created = 0; |
| 654 |
MSC_INST(dev)->quirks = quirks; |
| 655 |
|
| 656 |
for (i = 1; i <= dev->num_endp; i++) { |
| 657 |
if (dev->endpoints[i].endpoint == 0) |
| 658 |
continue; |
| 659 |
if (dev->endpoints[i].type != BULK) |
| 660 |
continue; |
| 661 |
if ((dev->endpoints[i].direction == IN) |
| 662 |
&& (MSC_INST(dev)->bulk_in == 0)) |
| 663 |
MSC_INST(dev)->bulk_in = &dev->endpoints[i]; |
| 664 |
if ((dev->endpoints[i].direction == OUT) |
| 665 |
&& (MSC_INST(dev)->bulk_out == 0)) |
| 666 |
MSC_INST(dev)->bulk_out = &dev->endpoints[i]; |
| 667 |
} |
| 668 |
|
| 669 |
if (MSC_INST(dev)->bulk_in == 0) { |
| 670 |
usb_debug("couldn't find bulk-in endpoint.\n"); |
| 671 |
usb_detach_device(dev->controller, dev->address); |
| 672 |
return; |
| 673 |
} |
| 674 |
if (MSC_INST(dev)->bulk_out == 0) { |
| 675 |
usb_debug("couldn't find bulk-out endpoint.\n"); |
| 676 |
usb_detach_device(dev->controller, dev->address); |
| 677 |
return; |
| 678 |
} |
| 679 |
usb_debug(" using endpoint %x as in, %x as out\n", |
| 680 |
MSC_INST(dev)->bulk_in->endpoint, |
| 681 |
MSC_INST(dev)->bulk_out->endpoint); |
| 682 |
|
| 683 |
|
| 684 |
udelay(50); |
| 685 |
|
| 686 |
initialize_luns(dev); |
| 687 |
usb_debug(" has %d luns\n", MSC_INST(dev)->num_luns); |
| 688 |
|
| 689 |
|
| 690 |
if (usb_msc_test_unit_ready(dev) != USB_MSC_READY) |
| 691 |
return; |
| 692 |
|
| 693 |
|
| 694 |
usb_msc_create_disk(dev); |
| 695 |
} |
| 696 |
|
| 697 |
static void |
| 698 |
usb_msc_poll(usbdev_t *dev) |
| 699 |
{ |
| 700 |
usbmsc_inst_t *msc = MSC_INST(dev); |
| 701 |
int prev_ready = msc->ready; |
| 702 |
|
| 703 |
if (usb_msc_test_unit_ready(dev) == USB_MSC_DETACHED) |
| 704 |
return; |
| 705 |
|
| 706 |
if (!prev_ready && msc->ready) { |
| 707 |
usb_debug("USB msc: not ready -> ready (lun %d)\n", msc->lun); |
| 708 |
usb_msc_create_disk(dev); |
| 709 |
} else if (prev_ready && !msc->ready) { |
| 710 |
usb_debug("USB msc: ready -> not ready (lun %d)\n", msc->lun); |
| 711 |
usb_msc_remove_disk(dev); |
| 712 |
} else if (!prev_ready && !msc->ready) { |
| 713 |
u8 new_lun = (msc->lun + 1) % msc->num_luns; |
| 714 |
usb_debug("USB msc: not ready (lun %d) -> lun %d\n", msc->lun, |
| 715 |
new_lun); |
| 716 |
msc->lun = new_lun; |
| 717 |
} |
| 718 |
} |