2008-09-02 18:06:22 +02:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 coresystems GmbH
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2013-05-03 01:16:41 +02:00
|
|
|
//#define USB_DEBUG
|
2012-03-19 11:06:46 +01:00
|
|
|
#include <endian.h>
|
2008-09-11 19:29:00 +02:00
|
|
|
#include <usb/usb.h>
|
|
|
|
#include <usb/usbmsc.h>
|
|
|
|
#include <usb/usbdisk.h>
|
2008-09-02 18:06:22 +02:00
|
|
|
|
|
|
|
enum {
|
|
|
|
msc_subclass_rbc = 0x1,
|
|
|
|
msc_subclass_mmc2 = 0x2,
|
|
|
|
msc_subclass_qic157 = 0x3,
|
|
|
|
msc_subclass_ufi = 0x4,
|
|
|
|
msc_subclass_sff8070i = 0x5,
|
|
|
|
msc_subclass_scsitrans = 0x6
|
|
|
|
};
|
2010-03-25 23:17:36 +01:00
|
|
|
|
2008-09-02 18:06:22 +02:00
|
|
|
static const char *msc_subclass_strings[7] = {
|
|
|
|
"(none)",
|
|
|
|
"RBC",
|
|
|
|
"MMC-2",
|
|
|
|
"QIC-157",
|
|
|
|
"UFI",
|
|
|
|
"SFF-8070i",
|
|
|
|
"SCSI transparent"
|
|
|
|
};
|
|
|
|
enum {
|
|
|
|
msc_proto_cbi_wcomp = 0x0,
|
|
|
|
msc_proto_cbi_wocomp = 0x1,
|
|
|
|
msc_proto_bulk_only = 0x50
|
|
|
|
};
|
|
|
|
static const char *msc_protocol_strings[0x51] = {
|
|
|
|
"Control/Bulk/Interrupt protocol (with command completion interrupt)",
|
|
|
|
"Control/Bulk/Interrupt protocol (with no command completion interrupt)",
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
"Bulk-Only Transport"
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_msc_create_disk(usbdev_t *dev)
|
2008-09-02 18:06:22 +02:00
|
|
|
{
|
2013-06-07 19:31:21 +02:00
|
|
|
if (usbdisk_create) {
|
2022-08-10 08:59:18 +02:00
|
|
|
usbdisk_create(dev);
|
|
|
|
MSC_INST(dev)->usbdisk_created = 1;
|
2012-06-21 10:52:49 +02:00
|
|
|
}
|
2008-09-02 18:06:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_msc_remove_disk(usbdev_t *dev)
|
2008-09-02 18:06:22 +02:00
|
|
|
{
|
2022-08-10 08:59:18 +02:00
|
|
|
if (MSC_INST(dev)->usbdisk_created && usbdisk_remove) {
|
|
|
|
usbdisk_remove(dev);
|
|
|
|
MSC_INST(dev)->usbdisk_created = 0;
|
2022-08-05 09:40:48 +02:00
|
|
|
}
|
2013-06-07 19:31:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_msc_destroy(usbdev_t *dev)
|
2013-06-07 19:31:21 +02:00
|
|
|
{
|
|
|
|
if (dev->data) {
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_msc_remove_disk(dev);
|
|
|
|
free(dev->data);
|
2013-06-07 19:31:21 +02:00
|
|
|
}
|
|
|
|
dev->data = 0;
|
2008-09-02 18:06:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const int DEV_RESET = 0xff;
|
|
|
|
const int GET_MAX_LUN = 0xfe;
|
2013-09-12 22:27:15 +02:00
|
|
|
/* Many USB3 devices do not work with large transfer requests.
|
|
|
|
* Limit the request size to 64KB chunks to ensure maximum compatibility. */
|
|
|
|
const int MAX_CHUNK_BYTES = 1024 * 64;
|
2008-09-02 18:06:22 +02:00
|
|
|
|
|
|
|
const unsigned int cbw_signature = 0x43425355;
|
|
|
|
const unsigned int csw_signature = 0x53425355;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
unsigned int dCBWSignature;
|
|
|
|
unsigned int dCBWTag;
|
|
|
|
unsigned int dCBWDataTransferLength;
|
|
|
|
unsigned char bmCBWFlags;
|
|
|
|
unsigned long bCBWLUN:4;
|
|
|
|
unsigned long:4;
|
|
|
|
unsigned long bCBWCBLength:5;
|
|
|
|
unsigned long:3;
|
|
|
|
unsigned char CBWCB[31 - 15];
|
2017-07-13 02:20:27 +02:00
|
|
|
} __packed cbw_t;
|
2010-03-25 23:17:36 +01:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
unsigned int dCSWSignature;
|
|
|
|
unsigned int dCSWTag;
|
|
|
|
unsigned int dCSWDataResidue;
|
|
|
|
unsigned char bCSWStatus;
|
2017-07-13 02:20:27 +02:00
|
|
|
} __packed csw_t;
|
2010-03-25 23:17:36 +01:00
|
|
|
|
2012-06-01 09:50:11 +02:00
|
|
|
enum {
|
|
|
|
/*
|
|
|
|
* MSC commands can be
|
|
|
|
* successful,
|
|
|
|
* fail with proper response or
|
2020-02-24 13:26:04 +01:00
|
|
|
* fail totally, which results in detaching of the USB device
|
2013-09-25 22:54:57 +02:00
|
|
|
* and immediate cleanup of the usbdev_t structure.
|
2012-06-01 09:50:11 +02:00
|
|
|
* In the latter case the caller has to make sure, that he won't
|
|
|
|
* use the device any more.
|
|
|
|
*/
|
|
|
|
MSC_COMMAND_OK = 0, MSC_COMMAND_FAIL, MSC_COMMAND_DETACHED
|
|
|
|
};
|
|
|
|
|
2010-03-25 23:17:36 +01:00
|
|
|
static int
|
2022-08-10 08:59:18 +02:00
|
|
|
request_sense(usbdev_t *dev);
|
2013-06-06 23:14:21 +02:00
|
|
|
static int
|
2022-08-10 08:59:18 +02:00
|
|
|
request_sense_no_media(usbdev_t *dev);
|
2013-06-07 19:31:21 +02:00
|
|
|
static void
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_msc_poll(usbdev_t *dev);
|
2010-03-25 23:17:36 +01:00
|
|
|
|
2012-06-01 09:50:11 +02:00
|
|
|
static int
|
2022-08-10 08:59:18 +02:00
|
|
|
reset_transport(usbdev_t *dev)
|
2008-09-02 18:06:22 +02:00
|
|
|
{
|
|
|
|
dev_req_t dr;
|
2022-08-10 08:59:18 +02:00
|
|
|
memset(&dr, 0, sizeof(dr));
|
2008-09-02 18:06:22 +02:00
|
|
|
dr.bmRequestType = 0;
|
|
|
|
dr.data_dir = host_to_device;
|
|
|
|
#ifndef QEMU
|
|
|
|
dr.req_type = class_type;
|
|
|
|
dr.req_recp = iface_recp;
|
|
|
|
#endif
|
|
|
|
dr.bRequest = DEV_RESET;
|
|
|
|
dr.wValue = 0;
|
|
|
|
dr.wIndex = 0;
|
|
|
|
dr.wLength = 0;
|
2012-06-01 09:50:11 +02:00
|
|
|
|
2022-08-10 08:59:18 +02:00
|
|
|
if (MSC_INST(dev)->quirks & USB_MSC_QUIRK_NO_RESET)
|
2019-07-11 00:53:59 +02:00
|
|
|
return MSC_COMMAND_FAIL;
|
|
|
|
|
2012-06-01 09:50:11 +02:00
|
|
|
/* if any of these fails, detach device, as we are lost */
|
2022-08-10 08:59:18 +02:00
|
|
|
if (dev->controller->control(dev, OUT, sizeof(dr), &dr, 0, 0) < 0 ||
|
|
|
|
clear_stall(MSC_INST(dev)->bulk_in) ||
|
|
|
|
clear_stall(MSC_INST(dev)->bulk_out)) {
|
|
|
|
usb_debug("Detaching unresponsive device.\n");
|
|
|
|
usb_detach_device(dev->controller, dev->address);
|
2012-06-01 09:50:11 +02:00
|
|
|
return MSC_COMMAND_DETACHED;
|
|
|
|
}
|
|
|
|
/* return fail as we are only called in case of failure */
|
|
|
|
return MSC_COMMAND_FAIL;
|
2008-09-02 18:06:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* device may stall this command, so beware! */
|
2014-05-03 01:35:50 +02:00
|
|
|
static void
|
2022-08-10 08:59:18 +02:00
|
|
|
initialize_luns(usbdev_t *dev)
|
2008-09-02 18:06:22 +02:00
|
|
|
{
|
2022-08-10 08:59:18 +02:00
|
|
|
usbmsc_inst_t *msc = MSC_INST(dev);
|
2008-09-02 18:06:22 +02:00
|
|
|
dev_req_t dr;
|
|
|
|
dr.bmRequestType = 0;
|
|
|
|
dr.data_dir = device_to_host;
|
|
|
|
#ifndef QEMU
|
|
|
|
dr.req_type = class_type;
|
|
|
|
dr.req_recp = iface_recp;
|
|
|
|
#endif
|
|
|
|
dr.bRequest = GET_MAX_LUN;
|
|
|
|
dr.wValue = 0;
|
|
|
|
dr.wIndex = 0;
|
|
|
|
dr.wLength = 1;
|
2022-08-10 08:59:18 +02:00
|
|
|
if (MSC_INST(dev)->quirks & USB_MSC_QUIRK_NO_LUNS ||
|
|
|
|
dev->controller->control(dev, IN, sizeof(dr), &dr,
|
|
|
|
sizeof(msc->num_luns), &msc->num_luns) < 0)
|
2014-05-03 01:35:50 +02:00
|
|
|
msc->num_luns = 0; /* assume only 1 lun if req fails */
|
|
|
|
msc->num_luns++; /* Get Max LUN returns number of last LUN */
|
|
|
|
msc->lun = 0;
|
2008-09-02 18:06:22 +02:00
|
|
|
}
|
|
|
|
|
2012-05-21 14:05:41 +02:00
|
|
|
unsigned int tag;
|
2008-09-02 18:06:22 +02:00
|
|
|
|
|
|
|
static void
|
2022-08-10 08:59:18 +02:00
|
|
|
wrap_cbw(cbw_t *cbw, int datalen, cbw_direction dir, const u8 *cmd,
|
2014-05-03 01:35:50 +02:00
|
|
|
int cmdlen, u8 lun)
|
2008-09-02 18:06:22 +02:00
|
|
|
{
|
2022-08-10 08:59:18 +02:00
|
|
|
memset(cbw, 0, sizeof(cbw_t));
|
2008-09-02 18:06:22 +02:00
|
|
|
|
2014-12-29 20:37:45 +01:00
|
|
|
/* commands are typically shorter, but we don't want overflows */
|
|
|
|
if (cmdlen > sizeof(cbw->CBWCB)) {
|
|
|
|
cmdlen = sizeof(cbw->CBWCB);
|
|
|
|
}
|
|
|
|
|
2008-09-02 18:06:22 +02:00
|
|
|
cbw->dCBWSignature = cbw_signature;
|
2012-05-21 14:05:41 +02:00
|
|
|
cbw->dCBWTag = ++tag;
|
2014-05-03 01:35:50 +02:00
|
|
|
cbw->bCBWLUN = lun;
|
2008-09-02 18:06:22 +02:00
|
|
|
|
|
|
|
cbw->dCBWDataTransferLength = datalen;
|
|
|
|
cbw->bmCBWFlags = dir;
|
2022-08-10 08:59:18 +02:00
|
|
|
memcpy(cbw->CBWCB, cmd, cmdlen);
|
2008-09-02 18:06:22 +02:00
|
|
|
cbw->bCBWCBLength = cmdlen;
|
|
|
|
}
|
|
|
|
|
2012-05-21 14:05:41 +02:00
|
|
|
static int
|
2022-08-10 08:59:18 +02:00
|
|
|
get_csw(endpoint_t *ep, csw_t *csw)
|
2008-09-02 18:06:22 +02:00
|
|
|
{
|
2019-07-22 20:06:40 +02:00
|
|
|
hci_t *ctrlr = ep->dev->controller;
|
2022-08-10 08:59:18 +02:00
|
|
|
int ret = ctrlr->bulk(ep, sizeof(csw_t), (u8 *) csw, 1);
|
2019-07-22 20:06:40 +02:00
|
|
|
|
|
|
|
/* Some broken sticks send a zero-length packet at the end of their data
|
|
|
|
transfer which would show up here. Skip it to get the actual CSW. */
|
|
|
|
if (ret == 0)
|
2022-08-10 08:59:18 +02:00
|
|
|
ret = ctrlr->bulk(ep, sizeof(csw_t), (u8 *) csw, 1);
|
2019-07-22 20:06:40 +02:00
|
|
|
|
|
|
|
if (ret < 0) {
|
2022-08-10 08:59:18 +02:00
|
|
|
clear_stall(ep);
|
|
|
|
ret = ctrlr->bulk(ep, sizeof(csw_t), (u8 *) csw, 1);
|
2019-11-08 11:24:17 +01:00
|
|
|
if (ret < 0)
|
2022-08-10 08:59:18 +02:00
|
|
|
return reset_transport(ep->dev);
|
2012-05-21 14:05:41 +02:00
|
|
|
}
|
2019-07-22 20:06:40 +02:00
|
|
|
if (ret != sizeof(csw_t) || csw->dCSWTag != tag ||
|
|
|
|
csw->dCSWSignature != csw_signature) {
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_debug("MSC: received malformed CSW\n");
|
|
|
|
return reset_transport(ep->dev);
|
2012-05-21 14:05:41 +02:00
|
|
|
}
|
2012-06-01 09:50:11 +02:00
|
|
|
return MSC_COMMAND_OK;
|
2008-09-02 18:06:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2022-08-10 08:59:18 +02:00
|
|
|
execute_command(usbdev_t *dev, cbw_direction dir, const u8 *cb, int cblen,
|
2012-06-01 09:50:11 +02:00
|
|
|
u8 *buf, int buflen, int residue_ok)
|
2008-09-02 18:06:22 +02:00
|
|
|
{
|
|
|
|
cbw_t cbw;
|
|
|
|
csw_t csw;
|
|
|
|
|
|
|
|
int always_succeed = 0;
|
|
|
|
if ((cb[0] == 0x1b) && (cb[4] == 1)) { //start command, always succeed
|
|
|
|
always_succeed = 1;
|
|
|
|
}
|
2022-08-10 08:59:18 +02:00
|
|
|
wrap_cbw(&cbw, buflen, dir, cb, cblen, MSC_INST(dev)->lun);
|
2008-09-02 18:06:22 +02:00
|
|
|
if (dev->controller->
|
2022-08-10 08:59:18 +02:00
|
|
|
bulk(MSC_INST(dev)->bulk_out, sizeof(cbw), (u8 *) &cbw, 0) < 0) {
|
|
|
|
return reset_transport(dev);
|
2008-09-02 18:06:22 +02:00
|
|
|
}
|
2010-03-25 23:17:36 +01:00
|
|
|
if (buflen > 0) {
|
|
|
|
if (dir == cbw_direction_data_in) {
|
|
|
|
if (dev->controller->
|
2022-08-10 08:59:18 +02:00
|
|
|
bulk(MSC_INST(dev)->bulk_in, buflen, buf, 0) < 0)
|
|
|
|
clear_stall(MSC_INST(dev)->bulk_in);
|
2010-03-25 23:17:36 +01:00
|
|
|
} else {
|
|
|
|
if (dev->controller->
|
2022-08-10 08:59:18 +02:00
|
|
|
bulk(MSC_INST(dev)->bulk_out, buflen, buf, 0) < 0)
|
|
|
|
clear_stall(MSC_INST(dev)->bulk_out);
|
2008-09-02 18:06:22 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-10 08:59:18 +02:00
|
|
|
int ret = get_csw(MSC_INST(dev)->bulk_in, &csw);
|
2012-06-01 09:50:11 +02:00
|
|
|
if (ret) {
|
|
|
|
return ret;
|
|
|
|
} else if (always_succeed == 1) {
|
|
|
|
/* return success, regardless of message */
|
|
|
|
return MSC_COMMAND_OK;
|
|
|
|
} else if (csw.bCSWStatus == 2) {
|
|
|
|
/* phase error, reset transport */
|
2022-08-10 08:59:18 +02:00
|
|
|
return reset_transport(dev);
|
2012-06-01 09:50:11 +02:00
|
|
|
} else if (csw.bCSWStatus == 0) {
|
|
|
|
if ((csw.dCSWDataResidue == 0) || residue_ok)
|
|
|
|
/* no error, exit */
|
|
|
|
return MSC_COMMAND_OK;
|
|
|
|
else
|
|
|
|
/* missed some bytes */
|
|
|
|
return MSC_COMMAND_FAIL;
|
|
|
|
} else {
|
|
|
|
if (cb[0] == 0x03)
|
|
|
|
/* requesting sense failed, that's bad */
|
|
|
|
return MSC_COMMAND_FAIL;
|
2013-06-06 23:14:21 +02:00
|
|
|
else if (cb[0] == 0)
|
|
|
|
/* If command was TEST UNIT READY determine if the
|
|
|
|
* device is of removable type indicating no media
|
|
|
|
* found. */
|
2022-08-10 08:59:18 +02:00
|
|
|
return request_sense_no_media(dev);
|
2012-06-01 09:50:11 +02:00
|
|
|
/* error "check condition" or reserved error */
|
2022-08-10 08:59:18 +02:00
|
|
|
ret = request_sense(dev);
|
2012-06-01 09:50:11 +02:00
|
|
|
/* return fail or the status of request_sense if it's worse */
|
|
|
|
return ret ? ret : MSC_COMMAND_FAIL;
|
2008-09-02 18:06:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
unsigned char command; //0
|
|
|
|
unsigned char res1; //1
|
|
|
|
unsigned int block; //2-5
|
|
|
|
unsigned char res2; //6
|
|
|
|
unsigned short numblocks; //7-8
|
2014-05-02 23:51:03 +02:00
|
|
|
unsigned char control; //9 - the block is 10 bytes long
|
2017-07-13 02:20:27 +02:00
|
|
|
} __packed cmdblock_t;
|
2008-09-02 18:06:22 +02:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
unsigned char command; //0
|
|
|
|
unsigned char res1; //1
|
|
|
|
unsigned char res2; //2
|
|
|
|
unsigned char res3; //3
|
2014-05-02 23:51:03 +02:00
|
|
|
union { //4
|
|
|
|
struct {
|
|
|
|
unsigned long start:1; // for START STOP UNIT
|
|
|
|
unsigned long:7;
|
|
|
|
};
|
|
|
|
unsigned char length; // for REQUEST SENSE
|
|
|
|
};
|
|
|
|
unsigned char control; //5
|
2017-07-13 02:20:27 +02:00
|
|
|
} __packed cmdblock6_t;
|
2008-09-02 18:06:22 +02:00
|
|
|
|
2010-03-25 23:17:36 +01:00
|
|
|
/**
|
|
|
|
* Like readwrite_blocks, but for soft-sectors of 512b size. Converts the
|
|
|
|
* start and count from 512b units.
|
|
|
|
* Start and count must be aligned so that they match the native
|
|
|
|
* sector size.
|
|
|
|
*
|
|
|
|
* @param dev device to access
|
|
|
|
* @param start first sector to access
|
|
|
|
* @param n number of sectors to access
|
|
|
|
* @param dir direction of access: cbw_direction_data_in == read, cbw_direction_data_out == write
|
|
|
|
* @param buf buffer to read into or write from. Must be at least n*512 bytes
|
|
|
|
* @return 0 on success, 1 on failure
|
|
|
|
*/
|
|
|
|
int
|
2022-08-10 08:59:18 +02:00
|
|
|
readwrite_blocks_512(usbdev_t *dev, int start, int n,
|
2010-03-25 23:17:36 +01:00
|
|
|
cbw_direction dir, u8 *buf)
|
|
|
|
{
|
|
|
|
int blocksize_divider = MSC_INST(dev)->blocksize / 512;
|
2022-08-10 08:59:18 +02:00
|
|
|
return readwrite_blocks(dev, start / blocksize_divider,
|
2010-03-25 23:17:36 +01:00
|
|
|
n / blocksize_divider, dir, buf);
|
|
|
|
}
|
2008-09-02 18:06:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads or writes a number of sequential blocks on a USB storage device.
|
|
|
|
* As it uses the READ(10) SCSI-2 command, it's limited to storage devices
|
|
|
|
* of at most 2TB. It assumes sectors of 512 bytes.
|
|
|
|
*
|
|
|
|
* @param dev device to access
|
|
|
|
* @param start first sector to access
|
|
|
|
* @param n number of sectors to access
|
|
|
|
* @param dir direction of access: cbw_direction_data_in == read, cbw_direction_data_out == write
|
2010-03-25 23:17:36 +01:00
|
|
|
* @param buf buffer to read into or write from. Must be at least n*sectorsize bytes
|
2008-09-02 18:06:22 +02:00
|
|
|
* @return 0 on success, 1 on failure
|
|
|
|
*/
|
2013-09-18 14:37:20 +02:00
|
|
|
static int
|
2022-08-10 08:59:18 +02:00
|
|
|
readwrite_chunk(usbdev_t *dev, int start, int n, cbw_direction dir, u8 *buf)
|
2008-09-02 18:06:22 +02:00
|
|
|
{
|
|
|
|
cmdblock_t cb;
|
2022-08-10 08:59:18 +02:00
|
|
|
memset(&cb, 0, sizeof(cb));
|
2008-09-02 18:06:22 +02:00
|
|
|
if (dir == cbw_direction_data_in) {
|
|
|
|
// read
|
|
|
|
cb.command = 0x28;
|
|
|
|
} else {
|
|
|
|
// write
|
|
|
|
cb.command = 0x2a;
|
|
|
|
}
|
2022-08-10 08:59:18 +02:00
|
|
|
cb.block = htonl(start);
|
|
|
|
cb.numblocks = htonw(n);
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2022-08-10 08:59:18 +02:00
|
|
|
return execute_command(dev, dir, (u8 *) &cb, sizeof(cb), buf,
|
2012-06-01 09:50:11 +02:00
|
|
|
n * MSC_INST(dev)->blocksize, 0)
|
|
|
|
!= MSC_COMMAND_OK ? 1 : 0;
|
2010-03-25 23:17:36 +01:00
|
|
|
}
|
|
|
|
|
2013-09-12 22:27:15 +02:00
|
|
|
/**
|
|
|
|
* Reads or writes a number of sequential blocks on a USB storage device
|
|
|
|
* that is split into MAX_CHUNK_BYTES size requests.
|
|
|
|
*
|
|
|
|
* As it uses the READ(10) SCSI-2 command, it's limited to storage devices
|
|
|
|
* of at most 2TB. It assumes sectors of 512 bytes.
|
|
|
|
*
|
|
|
|
* @param dev device to access
|
|
|
|
* @param start first sector to access
|
|
|
|
* @param n number of sectors to access
|
|
|
|
* @param dir direction of access: cbw_direction_data_in == read,
|
|
|
|
* cbw_direction_data_out == write
|
|
|
|
* @param buf buffer to read into or write from.
|
|
|
|
* Must be at least n*sectorsize bytes
|
|
|
|
* @return 0 on success, 1 on failure
|
|
|
|
*/
|
|
|
|
int
|
2022-08-10 08:59:18 +02:00
|
|
|
readwrite_blocks(usbdev_t *dev, int start, int n, cbw_direction dir, u8 *buf)
|
2013-09-12 22:27:15 +02:00
|
|
|
{
|
|
|
|
int chunk_size = MAX_CHUNK_BYTES / MSC_INST(dev)->blocksize;
|
|
|
|
int chunk;
|
|
|
|
|
|
|
|
/* Read as many full chunks as needed. */
|
|
|
|
for (chunk = 0; chunk < (n / chunk_size); chunk++) {
|
2022-08-10 08:59:18 +02:00
|
|
|
if (readwrite_chunk(dev, start + (chunk * chunk_size),
|
2013-09-12 22:27:15 +02:00
|
|
|
chunk_size, dir,
|
|
|
|
buf + (chunk * MAX_CHUNK_BYTES))
|
|
|
|
!= MSC_COMMAND_OK)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read any remaining partial chunk at the end. */
|
|
|
|
if (n % chunk_size) {
|
2022-08-10 08:59:18 +02:00
|
|
|
if (readwrite_chunk(dev, start + (chunk * chunk_size),
|
2013-09-12 22:27:15 +02:00
|
|
|
n % chunk_size, dir,
|
|
|
|
buf + (chunk * MAX_CHUNK_BYTES))
|
|
|
|
!= MSC_COMMAND_OK)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-25 23:17:36 +01:00
|
|
|
/* Only request it, we don't interpret it.
|
|
|
|
On certain errors, that's necessary to get devices out of
|
|
|
|
a special state called "Contingent Allegiance Condition" */
|
|
|
|
static int
|
2022-08-10 08:59:18 +02:00
|
|
|
request_sense(usbdev_t *dev)
|
2010-03-25 23:17:36 +01:00
|
|
|
{
|
|
|
|
u8 buf[19];
|
|
|
|
cmdblock6_t cb;
|
2022-08-10 08:59:18 +02:00
|
|
|
memset(&cb, 0, sizeof(cb));
|
2010-03-25 23:17:36 +01:00
|
|
|
cb.command = 0x3;
|
2022-08-10 08:59:18 +02:00
|
|
|
cb.length = sizeof(buf);
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2022-08-10 08:59:18 +02:00
|
|
|
return execute_command(dev, cbw_direction_data_in, (u8 *) &cb,
|
|
|
|
sizeof(cb), buf, sizeof(buf), 1);
|
2008-09-02 18:06:22 +02:00
|
|
|
}
|
|
|
|
|
2022-08-10 08:59:18 +02:00
|
|
|
static int request_sense_no_media(usbdev_t *dev)
|
2013-06-06 23:14:21 +02:00
|
|
|
{
|
|
|
|
u8 buf[19];
|
|
|
|
int ret;
|
|
|
|
cmdblock6_t cb;
|
2022-08-10 08:59:18 +02:00
|
|
|
memset(&cb, 0, sizeof(cb));
|
2013-06-06 23:14:21 +02:00
|
|
|
cb.command = 0x3;
|
2022-08-10 08:59:18 +02:00
|
|
|
cb.length = sizeof(buf);
|
2013-06-06 23:14:21 +02:00
|
|
|
|
2022-08-10 08:59:18 +02:00
|
|
|
ret = execute_command(dev, cbw_direction_data_in, (u8 *) &cb,
|
|
|
|
sizeof(cb), buf, sizeof(buf), 1);
|
2013-06-06 23:14:21 +02:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Check if sense key is set to NOT READY. */
|
|
|
|
if ((buf[2] & 0xf) != 2)
|
|
|
|
return MSC_COMMAND_FAIL;
|
|
|
|
|
|
|
|
/* Check if additional sense code is 0x3a. */
|
|
|
|
if (buf[12] != 0x3a)
|
|
|
|
return MSC_COMMAND_FAIL;
|
|
|
|
|
2013-06-07 19:31:21 +02:00
|
|
|
/* No media is present. Return MSC_COMMAND_OK while marking the disk
|
|
|
|
* not ready. */
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_debug("Empty media found.\n");
|
|
|
|
MSC_INST(dev)->ready = USB_MSC_NOT_READY;
|
2013-06-07 19:31:21 +02:00
|
|
|
return MSC_COMMAND_OK;
|
2013-06-06 23:14:21 +02:00
|
|
|
}
|
|
|
|
|
2008-09-02 18:06:22 +02:00
|
|
|
static int
|
2022-08-10 08:59:18 +02:00
|
|
|
test_unit_ready(usbdev_t *dev)
|
2008-09-02 18:06:22 +02:00
|
|
|
{
|
|
|
|
cmdblock6_t cb;
|
2022-08-10 08:59:18 +02:00
|
|
|
memset(&cb, 0, sizeof(cb)); // full initialization for T-U-R
|
|
|
|
return execute_command(dev, cbw_direction_data_out, (u8 *) &cb,
|
|
|
|
sizeof(cb), 0, 0, 0);
|
2008-09-02 18:06:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2022-08-10 08:59:18 +02:00
|
|
|
spin_up(usbdev_t *dev)
|
2008-09-02 18:06:22 +02:00
|
|
|
{
|
|
|
|
cmdblock6_t cb;
|
2022-08-10 08:59:18 +02:00
|
|
|
memset(&cb, 0, sizeof(cb));
|
2008-09-02 18:06:22 +02:00
|
|
|
cb.command = 0x1b;
|
2014-05-02 23:51:03 +02:00
|
|
|
cb.start = 1;
|
2022-08-10 08:59:18 +02:00
|
|
|
return execute_command(dev, cbw_direction_data_out, (u8 *) &cb,
|
|
|
|
sizeof(cb), 0, 0, 0);
|
2008-09-02 18:06:22 +02:00
|
|
|
}
|
|
|
|
|
2012-06-01 09:50:11 +02:00
|
|
|
static int
|
2022-08-10 08:59:18 +02:00
|
|
|
read_capacity(usbdev_t *dev)
|
2008-09-02 18:06:22 +02:00
|
|
|
{
|
|
|
|
cmdblock_t cb;
|
2022-08-10 08:59:18 +02:00
|
|
|
memset(&cb, 0, sizeof(cb));
|
2008-09-02 18:06:22 +02:00
|
|
|
cb.command = 0x25; // read capacity
|
2013-01-16 01:22:04 +01:00
|
|
|
u32 buf[2];
|
2009-04-30 18:46:12 +02:00
|
|
|
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_debug("Reading capacity of mass storage device.\n");
|
2012-06-01 09:50:11 +02:00
|
|
|
int count = 0, ret;
|
|
|
|
while (count++ < 20) {
|
|
|
|
switch (ret = execute_command
|
|
|
|
(dev, cbw_direction_data_in, (u8 *) &cb,
|
2022-08-10 08:59:18 +02:00
|
|
|
sizeof(cb), (u8 *)buf, 8, 0)) {
|
2012-06-01 09:50:11 +02:00
|
|
|
case MSC_COMMAND_OK:
|
|
|
|
break;
|
|
|
|
case MSC_COMMAND_FAIL:
|
|
|
|
continue;
|
|
|
|
default: /* if it's worse return */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2008-09-02 18:06:22 +02:00
|
|
|
if (count >= 20) {
|
2009-04-30 18:46:12 +02:00
|
|
|
// still not successful, assume 2tb in 512byte sectors, which is just the same garbage as any other number, but probably more usable.
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_debug(" assuming 2 TB with 512-byte sectors as READ CAPACITY didn't answer.\n");
|
|
|
|
MSC_INST(dev)->numblocks = 0xffffffff;
|
|
|
|
MSC_INST(dev)->blocksize = 512;
|
2008-09-02 18:06:22 +02:00
|
|
|
} else {
|
2022-08-10 08:59:18 +02:00
|
|
|
MSC_INST(dev)->numblocks = ntohl(buf[0]) + 1;
|
|
|
|
MSC_INST(dev)->blocksize = ntohl(buf[1]);
|
2008-09-02 18:06:22 +02:00
|
|
|
}
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_debug(" %d %d-byte sectors (%d MB)\n", MSC_INST(dev)->numblocks,
|
|
|
|
MSC_INST(dev)->blocksize,
|
2012-05-21 13:59:43 +02:00
|
|
|
/* round down high block counts to avoid integer overflow */
|
2022-08-10 08:59:18 +02:00
|
|
|
MSC_INST(dev)->numblocks > 1000000
|
|
|
|
? (MSC_INST(dev)->numblocks / 1000) * MSC_INST(dev)->blocksize / 1000 :
|
|
|
|
MSC_INST(dev)->numblocks * MSC_INST(dev)->blocksize / 1000 / 1000);
|
2012-06-01 09:50:11 +02:00
|
|
|
return MSC_COMMAND_OK;
|
2008-09-02 18:06:22 +02:00
|
|
|
}
|
|
|
|
|
2013-09-25 22:54:57 +02:00
|
|
|
static int
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_msc_test_unit_ready(usbdev_t *dev)
|
2013-06-07 19:31:21 +02:00
|
|
|
{
|
|
|
|
int i;
|
2013-09-13 03:09:39 +02:00
|
|
|
time_t start_time_secs;
|
|
|
|
struct timeval tv;
|
2013-09-13 03:23:09 +02:00
|
|
|
/* SCSI/ATA specs say we have to wait up to 30s, but most devices
|
2020-02-15 09:27:11 +01:00
|
|
|
* are ready much sooner. Use a 5 sec timeout to better accommodate
|
2013-09-13 03:23:09 +02:00
|
|
|
* devices which fail to respond. */
|
|
|
|
const int timeout_secs = 5;
|
2013-06-07 19:31:21 +02:00
|
|
|
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_debug(" Waiting for device to become ready...");
|
2013-06-07 19:31:21 +02:00
|
|
|
|
|
|
|
/* Initially mark the device ready. */
|
2022-08-10 08:59:18 +02:00
|
|
|
MSC_INST(dev)->ready = USB_MSC_READY;
|
|
|
|
gettimeofday(&tv, NULL);
|
2013-09-13 03:09:39 +02:00
|
|
|
start_time_secs = tv.tv_sec;
|
2013-06-07 19:31:21 +02:00
|
|
|
|
2013-09-13 03:09:39 +02:00
|
|
|
while (tv.tv_sec - start_time_secs < timeout_secs) {
|
2022-08-10 08:59:18 +02:00
|
|
|
switch (test_unit_ready(dev)) {
|
2013-06-07 19:31:21 +02:00
|
|
|
case MSC_COMMAND_OK:
|
|
|
|
break;
|
|
|
|
case MSC_COMMAND_FAIL:
|
2022-08-10 08:59:18 +02:00
|
|
|
mdelay(100);
|
|
|
|
usb_debug(".");
|
|
|
|
gettimeofday(&tv, NULL);
|
2013-06-07 19:31:21 +02:00
|
|
|
continue;
|
|
|
|
default:
|
2013-09-25 22:54:57 +02:00
|
|
|
/* Device detached, return immediately */
|
|
|
|
return USB_MSC_DETACHED;
|
2013-06-07 19:31:21 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-09-13 03:09:39 +02:00
|
|
|
if (!(tv.tv_sec - start_time_secs < timeout_secs)) {
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_debug("timeout. Device not ready.\n");
|
|
|
|
MSC_INST(dev)->ready = USB_MSC_NOT_READY;
|
2013-06-07 19:31:21 +02:00
|
|
|
}
|
|
|
|
|
2020-02-15 09:27:11 +01:00
|
|
|
/* Don't bother spinning up the storage device if the device is not
|
2013-06-07 19:31:21 +02:00
|
|
|
* ready. This can happen when empty card readers are present.
|
|
|
|
* Polling will pick it back up if readiness changes. */
|
2022-08-10 08:59:18 +02:00
|
|
|
if (!MSC_INST(dev)->ready)
|
|
|
|
return MSC_INST(dev)->ready;
|
2013-06-07 19:31:21 +02:00
|
|
|
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_debug("ok.\n");
|
2013-06-07 19:31:21 +02:00
|
|
|
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_debug(" spin up");
|
2013-06-07 19:31:21 +02:00
|
|
|
for (i = 0; i < 30; i++) {
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_debug(".");
|
|
|
|
switch (spin_up(dev)) {
|
2013-06-07 19:31:21 +02:00
|
|
|
case MSC_COMMAND_OK:
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_debug(" OK.");
|
2013-06-07 19:31:21 +02:00
|
|
|
break;
|
|
|
|
case MSC_COMMAND_FAIL:
|
2022-08-10 08:59:18 +02:00
|
|
|
mdelay(100);
|
2013-06-07 19:31:21 +02:00
|
|
|
continue;
|
|
|
|
default:
|
2013-09-25 22:54:57 +02:00
|
|
|
/* Device detached, return immediately */
|
|
|
|
return USB_MSC_DETACHED;
|
2013-06-07 19:31:21 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_debug("\n");
|
2013-06-07 19:31:21 +02:00
|
|
|
|
2022-08-10 08:59:18 +02:00
|
|
|
if (read_capacity(dev) == MSC_COMMAND_DETACHED)
|
2013-09-25 22:54:57 +02:00
|
|
|
return USB_MSC_DETACHED;
|
|
|
|
|
2022-08-10 08:59:18 +02:00
|
|
|
return MSC_INST(dev)->ready;
|
2013-06-07 19:31:21 +02:00
|
|
|
}
|
|
|
|
|
2008-09-02 18:06:22 +02:00
|
|
|
void
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_msc_init(usbdev_t *dev)
|
2008-09-02 18:06:22 +02:00
|
|
|
{
|
|
|
|
configuration_descriptor_t *cd =
|
|
|
|
(configuration_descriptor_t *) dev->configuration;
|
|
|
|
interface_descriptor_t *interface =
|
|
|
|
(interface_descriptor_t *) (((char *) cd) + cd->bLength);
|
|
|
|
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_debug(" it uses %s command set\n",
|
2008-09-02 18:06:22 +02:00
|
|
|
msc_subclass_strings[interface->bInterfaceSubClass]);
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_debug(" it uses %s protocol\n",
|
2008-09-02 18:06:22 +02:00
|
|
|
msc_protocol_strings[interface->bInterfaceProtocol]);
|
|
|
|
|
2010-03-25 23:17:36 +01:00
|
|
|
if (interface->bInterfaceProtocol != 0x50) {
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_debug(" Protocol not supported.\n");
|
|
|
|
usb_detach_device(dev->controller, dev->address);
|
2010-03-25 23:17:36 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((interface->bInterfaceSubClass != 2) && // ATAPI 8020
|
|
|
|
(interface->bInterfaceSubClass != 5) && // ATAPI 8070
|
|
|
|
(interface->bInterfaceSubClass != 6)) { // SCSI
|
2008-09-02 18:06:22 +02:00
|
|
|
/* Other protocols, such as ATAPI don't seem to be very popular. looks like ATAPI would be really easy to add, if necessary. */
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_debug(" Interface SubClass not supported.\n");
|
|
|
|
usb_detach_device(dev->controller, dev->address);
|
2008-09-02 18:06:22 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_msc_force_init(dev, 0);
|
2019-07-11 00:53:59 +02:00
|
|
|
}
|
|
|
|
|
2022-08-10 08:59:18 +02:00
|
|
|
void usb_msc_force_init(usbdev_t *dev, u32 quirks)
|
2019-07-11 00:53:59 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* init .data before setting .destroy */
|
|
|
|
dev->data = NULL;
|
|
|
|
|
|
|
|
dev->destroy = usb_msc_destroy;
|
|
|
|
dev->poll = usb_msc_poll;
|
|
|
|
|
2022-08-10 08:59:18 +02:00
|
|
|
dev->data = malloc(sizeof(usbmsc_inst_t));
|
2009-07-31 13:39:55 +02:00
|
|
|
if (!dev->data)
|
2011-11-04 11:50:03 +01:00
|
|
|
fatal("Not enough memory for USB MSC device.\n");
|
2009-07-31 13:39:55 +02:00
|
|
|
|
2022-08-10 08:59:18 +02:00
|
|
|
MSC_INST(dev)->bulk_in = 0;
|
|
|
|
MSC_INST(dev)->bulk_out = 0;
|
|
|
|
MSC_INST(dev)->usbdisk_created = 0;
|
|
|
|
MSC_INST(dev)->quirks = quirks;
|
2008-09-02 18:06:22 +02:00
|
|
|
|
|
|
|
for (i = 1; i <= dev->num_endp; i++) {
|
|
|
|
if (dev->endpoints[i].endpoint == 0)
|
|
|
|
continue;
|
|
|
|
if (dev->endpoints[i].type != BULK)
|
|
|
|
continue;
|
|
|
|
if ((dev->endpoints[i].direction == IN)
|
2022-08-10 08:59:18 +02:00
|
|
|
&& (MSC_INST(dev)->bulk_in == 0))
|
|
|
|
MSC_INST(dev)->bulk_in = &dev->endpoints[i];
|
2008-09-02 18:06:22 +02:00
|
|
|
if ((dev->endpoints[i].direction == OUT)
|
2022-08-10 08:59:18 +02:00
|
|
|
&& (MSC_INST(dev)->bulk_out == 0))
|
|
|
|
MSC_INST(dev)->bulk_out = &dev->endpoints[i];
|
2008-09-02 18:06:22 +02:00
|
|
|
}
|
|
|
|
|
2022-08-10 08:59:18 +02:00
|
|
|
if (MSC_INST(dev)->bulk_in == 0) {
|
2013-09-25 21:30:07 +02:00
|
|
|
usb_debug("couldn't find bulk-in endpoint.\n");
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_detach_device(dev->controller, dev->address);
|
2012-06-21 10:52:49 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-08-10 08:59:18 +02:00
|
|
|
if (MSC_INST(dev)->bulk_out == 0) {
|
2013-09-25 21:30:07 +02:00
|
|
|
usb_debug("couldn't find bulk-out endpoint.\n");
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_detach_device(dev->controller, dev->address);
|
2012-06-21 10:52:49 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_debug(" using endpoint %x as in, %x as out\n",
|
|
|
|
MSC_INST(dev)->bulk_in->endpoint,
|
|
|
|
MSC_INST(dev)->bulk_out->endpoint);
|
2008-09-02 18:06:22 +02:00
|
|
|
|
2015-10-26 19:44:00 +01:00
|
|
|
/* Some sticks need a little more time to get ready after SET_CONFIG. */
|
|
|
|
udelay(50);
|
|
|
|
|
2022-08-10 08:59:18 +02:00
|
|
|
initialize_luns(dev);
|
|
|
|
usb_debug(" has %d luns\n", MSC_INST(dev)->num_luns);
|
2008-09-02 18:06:22 +02:00
|
|
|
|
2013-09-25 22:54:57 +02:00
|
|
|
/* Test if unit is ready (nothing to do if it isn't). */
|
2022-08-10 08:59:18 +02:00
|
|
|
if (usb_msc_test_unit_ready(dev) != USB_MSC_READY)
|
2013-06-07 19:31:21 +02:00
|
|
|
return;
|
2008-09-02 18:06:22 +02:00
|
|
|
|
2013-06-07 19:31:21 +02:00
|
|
|
/* Create the disk. */
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_msc_create_disk(dev);
|
2013-06-07 19:31:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_msc_poll(usbdev_t *dev)
|
2013-06-07 19:31:21 +02:00
|
|
|
{
|
2022-08-10 08:59:18 +02:00
|
|
|
usbmsc_inst_t *msc = MSC_INST(dev);
|
2014-05-03 01:35:50 +02:00
|
|
|
int prev_ready = msc->ready;
|
2013-06-07 19:31:21 +02:00
|
|
|
|
2022-08-10 08:59:18 +02:00
|
|
|
if (usb_msc_test_unit_ready(dev) == USB_MSC_DETACHED)
|
2013-06-07 19:31:21 +02:00
|
|
|
return;
|
|
|
|
|
2014-05-03 01:35:50 +02:00
|
|
|
if (!prev_ready && msc->ready) {
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_debug("USB msc: not ready -> ready (lun %d)\n", msc->lun);
|
|
|
|
usb_msc_create_disk(dev);
|
2014-05-03 01:35:50 +02:00
|
|
|
} else if (prev_ready && !msc->ready) {
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_debug("USB msc: ready -> not ready (lun %d)\n", msc->lun);
|
|
|
|
usb_msc_remove_disk(dev);
|
2014-05-03 01:35:50 +02:00
|
|
|
} else if (!prev_ready && !msc->ready) {
|
|
|
|
u8 new_lun = (msc->lun + 1) % msc->num_luns;
|
2020-02-24 13:26:04 +01:00
|
|
|
usb_debug("USB msc: not ready (lun %d) -> lun %d\n", msc->lun,
|
2014-05-03 01:35:50 +02:00
|
|
|
new_lun);
|
|
|
|
msc->lun = new_lun;
|
2012-06-21 10:52:49 +02:00
|
|
|
}
|
2008-09-02 18:06:22 +02:00
|
|
|
}
|