|
|
@@ -0,0 +1,520 @@
|
|
|
+/* USB mass-storage class driver: Bulk-Only Transport (BOT) + SCSI.
|
|
|
+ *
|
|
|
+ * The USB core's device probe sees every enumerated device; this driver
|
|
|
+ * parses the configuration for an interface with class 8 / subclass 6 /
|
|
|
+ * protocol 0x50 (SCSI transparent, bulk-only), claims its bulk IN/OUT
|
|
|
+ * endpoints and exposes the disk as `usb0..` once usbmsc_blk_scan() runs.
|
|
|
+ *
|
|
|
+ * Only EHCI and xHCI implement the bulk-transfer ops; UHCI/OHCI devices are
|
|
|
+ * left unclaimed (a full-speed stick on a companion controller cannot be
|
|
|
+ * used). The block layer calls usbmsc_read/write/flush from blkdev.c.
|
|
|
+ */
|
|
|
+
|
|
|
+#include "usbmsc.h"
|
|
|
+#include "blkdev.h"
|
|
|
+#include "serial.h"
|
|
|
+#include "timer.h"
|
|
|
+#include "usb.h"
|
|
|
+
|
|
|
+#define USB_MSC_MAX 4
|
|
|
+/* Keep one SCSI data stage below EHCI's 0x7FFF-byte bulk-OUT limit:
|
|
|
+ * 62 sectors = 31,744 bytes. */
|
|
|
+#define MSC_CHUNK_SECTORS 62u
|
|
|
+#define MSC_BULK_CHUNK 2048u /* EHCI's fixed bulk-IN buffer */
|
|
|
+#define MSC_TIMEOUT_MS 3000u
|
|
|
+#define MSC_CBW_SIG 0x43425355u /* "USBC" LE */
|
|
|
+#define MSC_CSW_SIG 0x53425355u /* "USBS" LE */
|
|
|
+
|
|
|
+struct usb_msc {
|
|
|
+ int present;
|
|
|
+ unsigned int ctrl, addr, mps, port;
|
|
|
+ int low;
|
|
|
+ unsigned int ep_in, ep_in_mps; /* endpoint numbers (1..15) */
|
|
|
+ unsigned int ep_out, ep_out_mps;
|
|
|
+ unsigned int iface;
|
|
|
+ unsigned int sectors;
|
|
|
+ unsigned int tag;
|
|
|
+ char model[41];
|
|
|
+};
|
|
|
+
|
|
|
+static struct usb_msc msc[USB_MSC_MAX];
|
|
|
+static char msc_names[USB_MSC_MAX][6];
|
|
|
+static unsigned int msc_generation;
|
|
|
+
|
|
|
+static unsigned int rd32le(const unsigned char *p)
|
|
|
+{
|
|
|
+ return (unsigned int)p[0] | ((unsigned int)p[1] << 8) |
|
|
|
+ ((unsigned int)p[2] << 16) | ((unsigned int)p[3] << 24);
|
|
|
+}
|
|
|
+
|
|
|
+static unsigned int rd32be(const unsigned char *p)
|
|
|
+{
|
|
|
+ return ((unsigned int)p[0] << 24) | ((unsigned int)p[1] << 16) |
|
|
|
+ ((unsigned int)p[2] << 8) | (unsigned int)p[3];
|
|
|
+}
|
|
|
+
|
|
|
+static void put32le(unsigned char *p, unsigned int v)
|
|
|
+{
|
|
|
+ p[0] = (unsigned char)(v & 0xFF);
|
|
|
+ p[1] = (unsigned char)((v >> 8) & 0xFF);
|
|
|
+ p[2] = (unsigned char)((v >> 16) & 0xFF);
|
|
|
+ p[3] = (unsigned char)((v >> 24) & 0xFF);
|
|
|
+}
|
|
|
+
|
|
|
+static void put16be(unsigned char *p, unsigned int v)
|
|
|
+{
|
|
|
+ p[0] = (unsigned char)((v >> 8) & 0xFF);
|
|
|
+ p[1] = (unsigned char)(v & 0xFF);
|
|
|
+}
|
|
|
+
|
|
|
+static void put32be(unsigned char *p, unsigned int v)
|
|
|
+{
|
|
|
+ p[0] = (unsigned char)((v >> 24) & 0xFF);
|
|
|
+ p[1] = (unsigned char)((v >> 16) & 0xFF);
|
|
|
+ p[2] = (unsigned char)((v >> 8) & 0xFF);
|
|
|
+ p[3] = (unsigned char)(v & 0xFF);
|
|
|
+}
|
|
|
+
|
|
|
+static void msc_delay(unsigned int ms)
|
|
|
+{
|
|
|
+ unsigned int t0 = get_ticks_ms();
|
|
|
+
|
|
|
+ while (get_ticks_ms() - t0 < ms)
|
|
|
+ ;
|
|
|
+}
|
|
|
+
|
|
|
+/* Bulk IN with a deadline. Transfers larger than the host controller's
|
|
|
+ * internal bulk-IN buffer are split; EHCI's is 2048 bytes. */
|
|
|
+static int msc_bulk_in(struct usb_msc *m, unsigned char *buf, unsigned int len)
|
|
|
+{
|
|
|
+ unsigned int got = 0, t0 = get_ticks_ms();
|
|
|
+
|
|
|
+ while (got < len) {
|
|
|
+ unsigned int want = len - got;
|
|
|
+ int n;
|
|
|
+
|
|
|
+ if (want > MSC_BULK_CHUNK)
|
|
|
+ want = MSC_BULK_CHUNK;
|
|
|
+ n = usb_bulk_in(m->ctrl, m->addr, m->ep_in, m->ep_in_mps,
|
|
|
+ buf + got, want);
|
|
|
+ if (n < 0)
|
|
|
+ return -1;
|
|
|
+ if (n > 0) {
|
|
|
+ got += (unsigned int)n;
|
|
|
+ t0 = get_ticks_ms();
|
|
|
+ /* A short packet before the requested end means the
|
|
|
+ * device ended the data stage early. */
|
|
|
+ if (got < len && (unsigned int)n < want)
|
|
|
+ return -1;
|
|
|
+ } else if (get_ticks_ms() - t0 > MSC_TIMEOUT_MS) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+static int msc_bulk_out(struct usb_msc *m, const unsigned char *buf,
|
|
|
+ unsigned int len)
|
|
|
+{
|
|
|
+ return usb_bulk_out(m->ctrl, m->addr, m->ep_out, m->ep_out_mps,
|
|
|
+ buf, len);
|
|
|
+}
|
|
|
+
|
|
|
+/* BOT reset + clear both bulk endpoint halts. */
|
|
|
+static void msc_reset(struct usb_msc *m)
|
|
|
+{
|
|
|
+ usb_control_out(m->ctrl, m->addr, m->mps, m->low,
|
|
|
+ 0x21, 0xFF, 0, (unsigned short)m->iface);
|
|
|
+ usb_control_out(m->ctrl, m->addr, m->mps, m->low,
|
|
|
+ 0x02, 0x01, 0, (unsigned short)(0x80u | m->ep_in));
|
|
|
+ usb_control_out(m->ctrl, m->addr, m->mps, m->low,
|
|
|
+ 0x02, 0x01, 0, (unsigned short)m->ep_out);
|
|
|
+ m->tag++;
|
|
|
+}
|
|
|
+
|
|
|
+static int bot_cmd(struct usb_msc *m, const unsigned char *cb,
|
|
|
+ unsigned int cb_len, void *data, unsigned int data_len,
|
|
|
+ int dir_in)
|
|
|
+{
|
|
|
+ unsigned char cbw[31];
|
|
|
+ unsigned char csw[13];
|
|
|
+ unsigned int tag = ++m->tag;
|
|
|
+
|
|
|
+ for (unsigned int i = 0; i < sizeof(cbw); i++)
|
|
|
+ cbw[i] = 0;
|
|
|
+ put32le(cbw, MSC_CBW_SIG);
|
|
|
+ put32le(cbw + 4, tag);
|
|
|
+ put32le(cbw + 8, data_len);
|
|
|
+ cbw[12] = (unsigned char)(dir_in ? 0x80 : 0x00);
|
|
|
+ cbw[13] = 0; /* LUN 0 */
|
|
|
+ cbw[14] = (unsigned char)cb_len;
|
|
|
+ for (unsigned int i = 0; i < cb_len && i < 16; i++)
|
|
|
+ cbw[15 + i] = cb[i];
|
|
|
+
|
|
|
+ if (msc_bulk_out(m, cbw, sizeof(cbw)) != 0)
|
|
|
+ return -1;
|
|
|
+ if (data_len) {
|
|
|
+ if (dir_in) {
|
|
|
+ if (msc_bulk_in(m, (unsigned char *)data, data_len) != 0)
|
|
|
+ return -1;
|
|
|
+ } else if (msc_bulk_out(m, (const unsigned char *)data,
|
|
|
+ data_len) != 0) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (msc_bulk_in(m, csw, sizeof(csw)) != 0)
|
|
|
+ return -1;
|
|
|
+ if (rd32le(csw) != MSC_CSW_SIG || rd32le(csw + 4) != tag)
|
|
|
+ return -1;
|
|
|
+ if (csw[12] != 0)
|
|
|
+ return -1;
|
|
|
+ if (rd32le(csw + 8) != 0) /* residue */
|
|
|
+ return -1;
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+static int msc_test_unit_ready(struct usb_msc *m)
|
|
|
+{
|
|
|
+ unsigned char cb[6] = { 0x00, 0, 0, 0, 0, 0 };
|
|
|
+
|
|
|
+ return bot_cmd(m, cb, sizeof(cb), 0, 0, 0);
|
|
|
+}
|
|
|
+
|
|
|
+static int msc_inquiry(struct usb_msc *m, unsigned char data[36])
|
|
|
+{
|
|
|
+ unsigned char cb[6] = { 0x12, 0, 0, 0, 36, 0 };
|
|
|
+
|
|
|
+ return bot_cmd(m, cb, sizeof(cb), data, 36, 1);
|
|
|
+}
|
|
|
+
|
|
|
+static int msc_read_capacity(struct usb_msc *m, unsigned int *sectors)
|
|
|
+{
|
|
|
+ unsigned char cb[10] = { 0x25, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
|
+ unsigned char data[8];
|
|
|
+ unsigned int last, block;
|
|
|
+
|
|
|
+ if (bot_cmd(m, cb, sizeof(cb), data, sizeof(data), 1) != 0)
|
|
|
+ return -1;
|
|
|
+ last = rd32be(data);
|
|
|
+ block = rd32be(data + 4);
|
|
|
+ if (block != 512 || last == 0xFFFFFFFFu)
|
|
|
+ return -1;
|
|
|
+ *sectors = last + 1;
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+static int msc_read10(struct usb_msc *m, unsigned int lba, unsigned int count,
|
|
|
+ void *buf)
|
|
|
+{
|
|
|
+ unsigned char cb[10] = { 0x28, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
|
+
|
|
|
+ put32be(cb + 2, lba);
|
|
|
+ put16be(cb + 7, count);
|
|
|
+ return bot_cmd(m, cb, sizeof(cb), buf, count * 512u, 1);
|
|
|
+}
|
|
|
+
|
|
|
+static int msc_write10(struct usb_msc *m, unsigned int lba, unsigned int count,
|
|
|
+ const void *buf)
|
|
|
+{
|
|
|
+ unsigned char cb[10] = { 0x2A, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
|
+
|
|
|
+ put32be(cb + 2, lba);
|
|
|
+ put16be(cb + 7, count);
|
|
|
+ return bot_cmd(m, cb, sizeof(cb), (void *)buf, count * 512u, 0);
|
|
|
+}
|
|
|
+
|
|
|
+static int msc_sync_cache(struct usb_msc *m)
|
|
|
+{
|
|
|
+ unsigned char cb[10] = { 0x35, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
|
+
|
|
|
+ return bot_cmd(m, cb, sizeof(cb), 0, 0, 0);
|
|
|
+}
|
|
|
+
|
|
|
+/* Find and configure a bulk-only mass-storage interface. */
|
|
|
+static int msc_config(struct usb_msc *m)
|
|
|
+{
|
|
|
+ unsigned char hdr[9], cfg[256];
|
|
|
+ unsigned int total, i, config = 0;
|
|
|
+ unsigned int ep_in = 0, ep_out = 0, ep_in_mps = 0, ep_out_mps = 0;
|
|
|
+ unsigned int iface = 0;
|
|
|
+ int found = 0;
|
|
|
+
|
|
|
+ if (usb_control_in(m->ctrl, m->addr, m->mps, m->low,
|
|
|
+ 0x80, 0x06, 0x0200, 0, hdr, sizeof(hdr)) < 0)
|
|
|
+ return -1;
|
|
|
+ total = (unsigned int)hdr[2] | ((unsigned int)hdr[3] << 8);
|
|
|
+ if (total < 9 || total > sizeof(cfg))
|
|
|
+ return -1;
|
|
|
+ if (usb_control_in(m->ctrl, m->addr, m->mps, m->low,
|
|
|
+ 0x80, 0x06, 0x0200, 0, cfg, total) != (int)total)
|
|
|
+ return -1;
|
|
|
+ config = cfg[5];
|
|
|
+ if (!config)
|
|
|
+ return -1;
|
|
|
+
|
|
|
+ i = 0;
|
|
|
+ while (i + 2 <= total) {
|
|
|
+ unsigned int blen = cfg[i];
|
|
|
+ unsigned int btype = cfg[i + 1];
|
|
|
+
|
|
|
+ if (blen < 2 || i + blen > total)
|
|
|
+ break;
|
|
|
+ if (btype == 0x04 && blen >= 9) { /* interface */
|
|
|
+ if (cfg[i + 5] == 8 && cfg[i + 6] == 6 &&
|
|
|
+ cfg[i + 7] == 0x50) {
|
|
|
+ found = 1;
|
|
|
+ iface = cfg[i + 2];
|
|
|
+ ep_in = ep_out = 0;
|
|
|
+ ep_in_mps = ep_out_mps = 0;
|
|
|
+ } else {
|
|
|
+ found = 0;
|
|
|
+ }
|
|
|
+ } else if (btype == 0x05 && blen >= 7 && found) {
|
|
|
+ unsigned int epa = cfg[i + 2];
|
|
|
+ unsigned int attr = cfg[i + 3] & 3u;
|
|
|
+ unsigned int mps = ((unsigned int)cfg[i + 4] |
|
|
|
+ ((unsigned int)cfg[i + 5] << 8)) &
|
|
|
+ 0x7FFu;
|
|
|
+
|
|
|
+ if (attr == 2) {
|
|
|
+ if (epa & 0x80) {
|
|
|
+ ep_in = epa & 0x0F;
|
|
|
+ ep_in_mps = mps;
|
|
|
+ } else {
|
|
|
+ ep_out = epa & 0x0F;
|
|
|
+ ep_out_mps = mps;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ i += blen;
|
|
|
+ }
|
|
|
+ if (!found || !ep_in || !ep_out)
|
|
|
+ return -1;
|
|
|
+ if (usb_control_out(m->ctrl, m->addr, m->mps, m->low,
|
|
|
+ 0x00, 0x09, (unsigned short)config, 0) < 0)
|
|
|
+ return -1;
|
|
|
+
|
|
|
+ m->iface = iface;
|
|
|
+ m->ep_in = ep_in;
|
|
|
+ m->ep_in_mps = ep_in_mps ? ep_in_mps : 64;
|
|
|
+ m->ep_out = ep_out;
|
|
|
+ m->ep_out_mps = ep_out_mps ? ep_out_mps : 64;
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+static void msc_set_model(struct usb_msc *m, const unsigned char data[36])
|
|
|
+{
|
|
|
+ unsigned int i, n = 0;
|
|
|
+
|
|
|
+ for (i = 0; i < 8; i++)
|
|
|
+ m->model[n++] = (char)data[8 + i];
|
|
|
+ m->model[n++] = ' ';
|
|
|
+ for (i = 0; i < 16; i++)
|
|
|
+ m->model[n++] = (char)data[16 + i];
|
|
|
+ while (n && (m->model[n - 1] == ' ' || m->model[n - 1] == '\0'))
|
|
|
+ n--;
|
|
|
+ m->model[n] = '\0';
|
|
|
+}
|
|
|
+
|
|
|
+static int device_probe(unsigned int ctrl, unsigned int addr,
|
|
|
+ unsigned int mps, int low_speed, unsigned int port,
|
|
|
+ unsigned int vendor, unsigned int product)
|
|
|
+{
|
|
|
+ struct usb_msc *m = 0;
|
|
|
+ unsigned char inquiry[36];
|
|
|
+ unsigned int i, sectors;
|
|
|
+ int slot = -1;
|
|
|
+
|
|
|
+ (void)vendor;
|
|
|
+ (void)product;
|
|
|
+
|
|
|
+ /* A new bus scan re-enumerates every device with fresh addresses;
|
|
|
+ * forget the old set on its first probe. */
|
|
|
+ {
|
|
|
+ unsigned int gen = usb_generation();
|
|
|
+
|
|
|
+ if (gen != msc_generation) {
|
|
|
+ for (i = 0; i < USB_MSC_MAX; i++)
|
|
|
+ msc[i].present = 0;
|
|
|
+ msc_generation = gen;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for (i = 0; i < USB_MSC_MAX; i++)
|
|
|
+ if (msc[i].present && msc[i].ctrl == ctrl &&
|
|
|
+ msc[i].addr == addr) {
|
|
|
+ slot = (int)i;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (slot < 0)
|
|
|
+ for (i = 0; i < USB_MSC_MAX; i++)
|
|
|
+ if (!msc[i].present) {
|
|
|
+ slot = (int)i;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (slot < 0)
|
|
|
+ return 0;
|
|
|
+
|
|
|
+ m = &msc[(unsigned int)slot];
|
|
|
+ m->present = 0;
|
|
|
+ m->ctrl = ctrl;
|
|
|
+ m->addr = addr;
|
|
|
+ m->mps = mps;
|
|
|
+ m->low = low_speed;
|
|
|
+ m->port = port;
|
|
|
+ m->tag = 0;
|
|
|
+ m->iface = 0;
|
|
|
+
|
|
|
+ if (msc_config(m) != 0)
|
|
|
+ return 0;
|
|
|
+
|
|
|
+ for (i = 0; i < 4; i++) {
|
|
|
+ if (msc_test_unit_ready(m) == 0)
|
|
|
+ break;
|
|
|
+ msc_delay(50);
|
|
|
+ msc_reset(m);
|
|
|
+ }
|
|
|
+ if (i == 4) {
|
|
|
+ serial_puts(SERIAL_COM1, "usbmsc: no bulk transfer path / not ready\n");
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (msc_inquiry(m, inquiry) != 0) {
|
|
|
+ serial_puts(SERIAL_COM1, "usbmsc: inquiry failed\n");
|
|
|
+ msc_reset(m);
|
|
|
+ if (msc_inquiry(m, inquiry) != 0)
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ if (msc_read_capacity(m, §ors) != 0) {
|
|
|
+ serial_puts(SERIAL_COM1, "usbmsc: read capacity failed\n");
|
|
|
+ msc_reset(m);
|
|
|
+ if (msc_read_capacity(m, §ors) != 0)
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ if (sectors == 0)
|
|
|
+ return 0;
|
|
|
+
|
|
|
+ msc_set_model(m, inquiry);
|
|
|
+ m->sectors = sectors;
|
|
|
+ m->present = 1;
|
|
|
+
|
|
|
+ serial_puts(SERIAL_COM1, "usbmsc: ");
|
|
|
+ serial_puts(SERIAL_COM1, m->model);
|
|
|
+ serial_puts(SERIAL_COM1, " ");
|
|
|
+ serial_putdec(SERIAL_COM1, sectors);
|
|
|
+ serial_puts(SERIAL_COM1, " sectors on ctrl ");
|
|
|
+ serial_putdec(SERIAL_COM1, ctrl);
|
|
|
+ serial_puts(SERIAL_COM1, " addr ");
|
|
|
+ serial_putdec(SERIAL_COM1, addr);
|
|
|
+ serial_puts(SERIAL_COM1, "\n");
|
|
|
+ return 1;
|
|
|
+}
|
|
|
+
|
|
|
+void usbmsc_register(void)
|
|
|
+{
|
|
|
+ unsigned int i;
|
|
|
+
|
|
|
+ for (i = 0; i < USB_MSC_MAX; i++) {
|
|
|
+ msc_names[i][0] = 'u';
|
|
|
+ msc_names[i][1] = 's';
|
|
|
+ msc_names[i][2] = 'b';
|
|
|
+ msc_names[i][3] = (char)('0' + i);
|
|
|
+ msc_names[i][4] = '\0';
|
|
|
+ }
|
|
|
+ usb_register_device_probe(device_probe);
|
|
|
+}
|
|
|
+
|
|
|
+void usbmsc_blk_scan(void)
|
|
|
+{
|
|
|
+ unsigned int i;
|
|
|
+
|
|
|
+ for (i = 0; i < USB_MSC_MAX; i++) {
|
|
|
+ blk_dev_t *d;
|
|
|
+ unsigned int j;
|
|
|
+
|
|
|
+ if (!msc[i].present || blk_dev_count >= BLKDEV_MAX)
|
|
|
+ continue;
|
|
|
+ d = &blk_devs[blk_dev_count++];
|
|
|
+ d->name = msc_names[i];
|
|
|
+ d->kind = BLK_USB;
|
|
|
+ d->bus = 0;
|
|
|
+ d->unit = i;
|
|
|
+ d->present = 1;
|
|
|
+ d->total_sectors = msc[i].sectors;
|
|
|
+ for (j = 0; j < sizeof(d->model) - 1 && msc[i].model[j]; j++)
|
|
|
+ d->model[j] = msc[i].model[j];
|
|
|
+ d->model[j] = '\0';
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static int msc_get(unsigned int unit, struct usb_msc **out)
|
|
|
+{
|
|
|
+ if (unit >= USB_MSC_MAX || !msc[unit].present)
|
|
|
+ return -1;
|
|
|
+ *out = &msc[unit];
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+int usbmsc_read(unsigned int unit, unsigned int lba, void *buf,
|
|
|
+ unsigned int count)
|
|
|
+{
|
|
|
+ struct usb_msc *m;
|
|
|
+ unsigned char *p = (unsigned char *)buf;
|
|
|
+
|
|
|
+ if (msc_get(unit, &m) != 0)
|
|
|
+ return -1;
|
|
|
+ if (lba > m->sectors || count > m->sectors - lba)
|
|
|
+ return -1;
|
|
|
+ while (count) {
|
|
|
+ unsigned int n = count > MSC_CHUNK_SECTORS ? MSC_CHUNK_SECTORS : count;
|
|
|
+
|
|
|
+ if (msc_read10(m, lba, n, p) != 0) {
|
|
|
+ msc_reset(m);
|
|
|
+ if (msc_read10(m, lba, n, p) != 0)
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ lba += n;
|
|
|
+ p += n * 512u;
|
|
|
+ count -= n;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+int usbmsc_write(unsigned int unit, unsigned int lba, const void *buf,
|
|
|
+ unsigned int count)
|
|
|
+{
|
|
|
+ struct usb_msc *m;
|
|
|
+ const unsigned char *p = (const unsigned char *)buf;
|
|
|
+
|
|
|
+ if (msc_get(unit, &m) != 0)
|
|
|
+ return -1;
|
|
|
+ if (lba > m->sectors || count > m->sectors - lba)
|
|
|
+ return -1;
|
|
|
+ while (count) {
|
|
|
+ unsigned int n = count > MSC_CHUNK_SECTORS ? MSC_CHUNK_SECTORS : count;
|
|
|
+
|
|
|
+ if (msc_write10(m, lba, n, p) != 0) {
|
|
|
+ msc_reset(m);
|
|
|
+ if (msc_write10(m, lba, n, p) != 0)
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ lba += n;
|
|
|
+ p += n * 512u;
|
|
|
+ count -= n;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+int usbmsc_flush(unsigned int unit)
|
|
|
+{
|
|
|
+ struct usb_msc *m;
|
|
|
+
|
|
|
+ if (msc_get(unit, &m) != 0)
|
|
|
+ return -1;
|
|
|
+ if (msc_sync_cache(m) != 0) {
|
|
|
+ msc_reset(m);
|
|
|
+ if (msc_sync_cache(m) != 0)
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|