libpayload: usbmsc: Implement limited LUN support

I always thought the support for multiple logical SCSI units in the USB
mass storage class was a dead feature. Turns out that it's actually used
by SD card readers that provide multiple slots (e.g. one regular sized
and one micro-SD). Implementing perfect support for that would require a
major redesign of the whole MSC stack, since the one device -> one disk
assumption is deeply embedded in our data structures.

Instead, this patch implements a poor man's LUN support that will just
cycle through all available LUNs (in multiple calls to usb_msc_poll())
until it finds a connected device. This should be reasonable enough to
allow these card readers to be usable while only requiring superficial
changes.

Also removes the unused 'protocol' attribute of usb_msc_inst_t.

BRANCH=rambi?,nyan
BUG=chrome-os-partner:28437
TEST=Alternatively plug an SD or micro-SD card (or both) into my card
reader, confirm that one of them is correctly detected at all times.

Original-Change-Id: I3df4ca88afe2dcf7928b823aa2a73c2b0f599cf2
Original-Signed-off-by: Julius Werner <jwerner@chromium.org>
Original-Reviewed-on: https://chromium-review.googlesource.com/198101
Original-Reviewed-by: Aaron Durbin <adurbin@chromium.org>
(cherry picked from commit 960534a20e4334772c29355bb0d310b3f41b31ee)
Signed-off-by: Marc Jones <marc.jones@se-eng.com>

Change-Id: I39909fc96e32c9a5d76651d91c2b5c16c89ace9e
Reviewed-on: http://review.coreboot.org/7904
Tested-by: build bot (Jenkins)
Reviewed-by: Paul Menzel <paulepanter@users.sourceforge.net>
Reviewed-by: Edward O'Callaghan <eocallaghan@alterapraxis.com>
This commit is contained in:
Julius Werner 2014-05-02 16:35:50 -07:00 committed by Marc Jones
parent 7124788b33
commit e30e4e7efa
2 changed files with 28 additions and 20 deletions

View File

@ -170,10 +170,10 @@ reset_transport (usbdev_t *dev)
} }
/* device may stall this command, so beware! */ /* device may stall this command, so beware! */
static int static void
get_max_luns (usbdev_t *dev) initialize_luns (usbdev_t *dev)
{ {
unsigned char luns = 75; usbmsc_inst_t *msc = MSC_INST (dev);
dev_req_t dr; dev_req_t dr;
dr.bmRequestType = 0; dr.bmRequestType = 0;
dr.data_dir = device_to_host; dr.data_dir = device_to_host;
@ -185,23 +185,24 @@ get_max_luns (usbdev_t *dev)
dr.wValue = 0; dr.wValue = 0;
dr.wIndex = 0; dr.wIndex = 0;
dr.wLength = 1; dr.wLength = 1;
if (dev->controller->control (dev, IN, sizeof (dr), &dr, 1, &luns) < 0) if (dev->controller->control (dev, IN, sizeof (dr), &dr,
luns = 0; // assume only 1 lun if req fails sizeof (msc->num_luns), &msc->num_luns) < 0)
return luns; 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;
} }
unsigned int tag; unsigned int tag;
unsigned char lun = 0;
static void static void
wrap_cbw (cbw_t *cbw, int datalen, cbw_direction dir, const u8 *cmd, wrap_cbw (cbw_t *cbw, int datalen, cbw_direction dir, const u8 *cmd,
int cmdlen) int cmdlen, u8 lun)
{ {
memset (cbw, 0, sizeof (cbw_t)); memset (cbw, 0, sizeof (cbw_t));
cbw->dCBWSignature = cbw_signature; cbw->dCBWSignature = cbw_signature;
cbw->dCBWTag = ++tag; cbw->dCBWTag = ++tag;
cbw->bCBWLUN = lun; // static value per device cbw->bCBWLUN = lun;
cbw->dCBWDataTransferLength = datalen; cbw->dCBWDataTransferLength = datalen;
cbw->bmCBWFlags = dir; cbw->bmCBWFlags = dir;
@ -236,7 +237,7 @@ execute_command (usbdev_t *dev, cbw_direction dir, const u8 *cb, int cblen,
if ((cb[0] == 0x1b) && (cb[4] == 1)) { //start command, always succeed if ((cb[0] == 0x1b) && (cb[4] == 1)) { //start command, always succeed
always_succeed = 1; always_succeed = 1;
} }
wrap_cbw (&cbw, buflen, dir, cb, cblen); wrap_cbw (&cbw, buflen, dir, cb, cblen, MSC_INST (dev)->lun);
if (dev->controller-> if (dev->controller->
bulk (MSC_INST (dev)->bulk_out, sizeof (cbw), (u8 *) &cbw, 0) < 0) { bulk (MSC_INST (dev)->bulk_out, sizeof (cbw), (u8 *) &cbw, 0) < 0) {
return reset_transport (dev); return reset_transport (dev);
@ -623,7 +624,6 @@ usb_msc_init (usbdev_t *dev)
if (!dev->data) if (!dev->data)
fatal("Not enough memory for USB MSC device.\n"); fatal("Not enough memory for USB MSC device.\n");
MSC_INST (dev)->protocol = interface->bInterfaceSubClass;
MSC_INST (dev)->bulk_in = 0; MSC_INST (dev)->bulk_in = 0;
MSC_INST (dev)->bulk_out = 0; MSC_INST (dev)->bulk_out = 0;
MSC_INST (dev)->usbdisk_created = 0; MSC_INST (dev)->usbdisk_created = 0;
@ -655,7 +655,8 @@ usb_msc_init (usbdev_t *dev)
MSC_INST (dev)->bulk_in->endpoint, MSC_INST (dev)->bulk_in->endpoint,
MSC_INST (dev)->bulk_out->endpoint); MSC_INST (dev)->bulk_out->endpoint);
usb_debug (" has %d luns\n", get_max_luns (dev) + 1); initialize_luns (dev);
usb_debug (" has %d luns\n", MSC_INST (dev)->num_luns);
/* Test if unit is ready (nothing to do if it isn't). */ /* Test if unit is ready (nothing to do if it isn't). */
if (usb_msc_test_unit_ready (dev) != USB_MSC_READY) if (usb_msc_test_unit_ready (dev) != USB_MSC_READY)
@ -668,16 +669,22 @@ usb_msc_init (usbdev_t *dev)
static void static void
usb_msc_poll (usbdev_t *dev) usb_msc_poll (usbdev_t *dev)
{ {
int prev_ready = MSC_INST (dev)->ready; usbmsc_inst_t *msc = MSC_INST (dev);
int prev_ready = msc->ready;
if (usb_msc_test_unit_ready (dev) == USB_MSC_DETACHED) if (usb_msc_test_unit_ready (dev) == USB_MSC_DETACHED)
return; return;
if (!prev_ready && MSC_INST (dev)->ready) { if (!prev_ready && msc->ready) {
usb_debug ("usb msc: not ready -> ready\n"); usb_debug ("usb msc: not ready -> ready (lun %d)\n", msc->lun);
usb_msc_create_disk (dev); usb_msc_create_disk (dev);
} else if (prev_ready && !MSC_INST (dev)->ready) { } else if (prev_ready && !msc->ready) {
usb_debug ("usb msc: ready -> not ready\n"); usb_debug ("usb msc: ready -> not ready (lun %d)\n", msc->lun);
usb_msc_remove_disk (dev); usb_msc_remove_disk (dev);
} else if (!prev_ready && !msc->ready) {
u8 new_lun = (msc->lun + 1) % msc->num_luns;
usb_debug("usb msc: not ready (lun %d) -> lun %d\n", msc->lun,
new_lun);
msc->lun = new_lun;
} }
} }

View File

@ -32,11 +32,12 @@
typedef struct { typedef struct {
unsigned int blocksize; unsigned int blocksize;
unsigned int numblocks; unsigned int numblocks;
unsigned int protocol;
endpoint_t *bulk_in; endpoint_t *bulk_in;
endpoint_t *bulk_out; endpoint_t *bulk_out;
int usbdisk_created; u8 usbdisk_created;
int ready; s8 ready;
u8 lun;
u8 num_luns;
void *data; /* For use by consumers of libpayload. */ void *data; /* For use by consumers of libpayload. */
} usbmsc_inst_t; } usbmsc_inst_t;