Finally move the INQUIRY/TEST UNIT READY/READ CAPACITY sequence to generic code, so that virtio-scsi will be able to use it.
Signed-off-by: Paolo Bonzini pbonzini@redhat.com --- src/blockcmd.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/blockcmd.h | 1 + src/usb-msc.c | 51 ++++++------------------------------------------- 3 files changed, 65 insertions(+), 44 deletions(-)
diff --git a/src/blockcmd.c b/src/blockcmd.c index e86fe29..cbc31b2 100644 --- a/src/blockcmd.c +++ b/src/blockcmd.c @@ -75,6 +75,63 @@ scsi_is_ready(struct disk_op_s *op) return 0; }
+// Validate drive and find block size and sector count. +int +scsi_init_drive(struct drive_s *drive, const char *s, int *pdt, char **desc) +{ + if (!CONFIG_USB_MSC) + return 0; + + struct disk_op_s dop; + memset(&dop, 0, sizeof(dop)); + dop.drive_g = drive; + struct cdbres_inquiry data; + int ret = cdb_get_inquiry(&dop, &data); + if (ret) + return ret; + char vendor[sizeof(data.vendor)+1], product[sizeof(data.product)+1]; + char rev[sizeof(data.rev)+1]; + strtcpy(vendor, data.vendor, sizeof(vendor)); + nullTrailingSpace(vendor); + strtcpy(product, data.product, sizeof(product)); + nullTrailingSpace(product); + strtcpy(rev, data.rev, sizeof(rev)); + nullTrailingSpace(rev); + *pdt = data.pdt & 0x1f; + int removable = !!(data.removable & 0x80); + dprintf(1, "%s vendor='%s' product='%s' rev='%s' type=%d removable=%d\n" + , s, vendor, product, rev, *pdt, removable); + drive->removable = removable; + + if (*pdt == SCSI_TYPE_CDROM) { + drive->blksize = CDROM_SECTOR_SIZE; + drive->sectors = (u64)-1; + + *desc = znprintf(MAXDESCSIZE, "DVD/CD [%s Drive %s %s %s]" + , s, vendor, product, rev); + return 0; + } + + if (scsi_is_ready(&dop)) + return ret; + + struct cdbres_read_capacity capdata; + ret = cdb_read_capacity(&dop, &capdata); + if (ret) + return ret; + + // READ CAPACITY returns the address of the last block. + // TODO: send READ CAPACITY(16) if capdata.sectors is 0xFFFFFFFF + drive->blksize = ntohl(capdata.blksize); + drive->sectors = (u64)ntohl(capdata.sectors) + 1; + dprintf(1, "%s blksize=%d sectors=%d\n" + , s, drive->blksize, (unsigned)drive->sectors); + + *desc = znprintf(MAXDESCSIZE, "%s Drive %s %s %s" + , s, vendor, product, rev); + return 0; +} + int cdb_get_inquiry(struct disk_op_s *op, struct cdbres_inquiry *data) { diff --git a/src/blockcmd.h b/src/blockcmd.h index 106a529..13ae991 100644 --- a/src/blockcmd.h +++ b/src/blockcmd.h @@ -81,5 +81,6 @@ int cdb_read(struct disk_op_s *op); int cdb_write(struct disk_op_s *op);
int scsi_is_ready(struct disk_op_s *op); +int scsi_init_drive(struct drive_s *drive, const char *s, int *pdt, char **desc);
#endif // blockcmd.h diff --git a/src/usb-msc.c b/src/usb-msc.c index 6617a9f..2ec5356 100644 --- a/src/usb-msc.c +++ b/src/usb-msc.c @@ -216,54 +216,17 @@ usb_msc_init(struct usb_pipe *pipe if (!udrive_g->bulkin || !udrive_g->bulkout) goto fail;
- // Validate drive and find block size and sector count. - struct disk_op_s dop; - memset(&dop, 0, sizeof(dop)); - dop.drive_g = &udrive_g->drive; - struct cdbres_inquiry data; - int ret = cdb_get_inquiry(&dop, &data); + int ret, pdt; + char *desc = NULL; + ret = scsi_init_drive(&udrive_g->drive, "USB MSC", &pdt, &desc); if (ret) goto fail; - char vendor[sizeof(data.vendor)+1], product[sizeof(data.product)+1]; - char rev[sizeof(data.rev)+1]; - strtcpy(vendor, data.vendor, sizeof(vendor)); - nullTrailingSpace(vendor); - strtcpy(product, data.product, sizeof(product)); - nullTrailingSpace(product); - strtcpy(rev, data.rev, sizeof(rev)); - nullTrailingSpace(rev); - int pdt = data.pdt & 0x1f; - int removable = !!(data.removable & 0x80); - dprintf(1, "USB MSC vendor='%s' product='%s' rev='%s' type=%d removable=%d\n" - , vendor, product, rev, pdt, removable); - udrive_g->drive.removable = removable; - - if (pdt == SCSI_TYPE_CDROM) { - char *desc = znprintf(MAXDESCSIZE, "DVD/CD [USB Drive %s %s %s]" - , vendor, product, rev); - ret = setup_drive_cdrom(&udrive_g->drive, desc); - } else { - ret = scsi_is_ready(&dop); - if (ret) { - dprintf(1, "scsi_is_ready returned %d\n", ret); - return ret; - } - - struct cdbres_read_capacity capdata; - ret = cdb_read_capacity(&dop, &capdata); - if (ret) - return ret;
- // READ CAPACITY returns the address of the last block - udrive_g->drive.blksize = ntohl(capdata.blksize); - udrive_g->drive.sectors = ntohl(capdata.sectors) + 1; - dprintf(1, "USB MSC blksize=%d sectors=%d\n", - udrive_g->drive.blksize, (int)udrive_g->drive.sectors); - - char *desc = znprintf(MAXDESCSIZE, "USB Drive %s %s %s" - , vendor, product, rev); + if (pdt == SCSI_TYPE_CDROM) + ret = setup_drive_cdrom(&udrive_g->drive, desc); + else ret = setup_drive_hd(&udrive_g->drive, desc); - } + if (ret) goto fail;