2 * USB Mass Storage Device emulation
4 * Copyright (c) 2006 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the LGPL.
10 #include "qemu-common.h"
11 #include "qemu/option.h"
12 #include "qemu/config-file.h"
14 #include "hw/usb/desc.h"
15 #include "hw/scsi/scsi.h"
16 #include "ui/console.h"
17 #include "monitor/monitor.h"
18 #include "sysemu/sysemu.h"
19 #include "sysemu/blockdev.h"
20 #include "qapi/visitor.h"
25 #define DPRINTF(fmt, ...) \
26 do { printf("usb-msd: " fmt , ## __VA_ARGS__); } while (0)
28 #define DPRINTF(fmt, ...) do {} while(0)
32 #define MassStorageReset 0xff
33 #define GetMaxLun 0xfe
36 USB_MSDM_CBW
, /* Command Block. */
37 USB_MSDM_DATAOUT
, /* Transfer data to device. */
38 USB_MSDM_DATAIN
, /* Transfer data from device. */
39 USB_MSDM_CSW
/* Command Status. */
55 struct usb_msd_csw csw
;
58 /* For async completion. */
60 /* usb-storage only */
85 static const USBDescStrings desc_strings
= {
86 [STR_MANUFACTURER
] = "QEMU",
87 [STR_PRODUCT
] = "QEMU USB HARDDRIVE",
88 [STR_SERIALNUMBER
] = "1",
89 [STR_CONFIG_FULL
] = "Full speed config (usb 1.1)",
90 [STR_CONFIG_HIGH
] = "High speed config (usb 2.0)",
91 [STR_CONFIG_SUPER
] = "Super speed config (usb 3.0)",
94 static const USBDescIface desc_iface_full
= {
95 .bInterfaceNumber
= 0,
97 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
98 .bInterfaceSubClass
= 0x06, /* SCSI */
99 .bInterfaceProtocol
= 0x50, /* Bulk */
100 .eps
= (USBDescEndpoint
[]) {
102 .bEndpointAddress
= USB_DIR_IN
| 0x01,
103 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
104 .wMaxPacketSize
= 64,
106 .bEndpointAddress
= USB_DIR_OUT
| 0x02,
107 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
108 .wMaxPacketSize
= 64,
113 static const USBDescDevice desc_device_full
= {
115 .bMaxPacketSize0
= 8,
116 .bNumConfigurations
= 1,
117 .confs
= (USBDescConfig
[]) {
120 .bConfigurationValue
= 1,
121 .iConfiguration
= STR_CONFIG_FULL
,
122 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_SELFPOWER
,
124 .ifs
= &desc_iface_full
,
129 static const USBDescIface desc_iface_high
= {
130 .bInterfaceNumber
= 0,
132 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
133 .bInterfaceSubClass
= 0x06, /* SCSI */
134 .bInterfaceProtocol
= 0x50, /* Bulk */
135 .eps
= (USBDescEndpoint
[]) {
137 .bEndpointAddress
= USB_DIR_IN
| 0x01,
138 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
139 .wMaxPacketSize
= 512,
141 .bEndpointAddress
= USB_DIR_OUT
| 0x02,
142 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
143 .wMaxPacketSize
= 512,
148 static const USBDescDevice desc_device_high
= {
150 .bMaxPacketSize0
= 64,
151 .bNumConfigurations
= 1,
152 .confs
= (USBDescConfig
[]) {
155 .bConfigurationValue
= 1,
156 .iConfiguration
= STR_CONFIG_HIGH
,
157 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_SELFPOWER
,
159 .ifs
= &desc_iface_high
,
164 static const USBDescIface desc_iface_super
= {
165 .bInterfaceNumber
= 0,
167 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
168 .bInterfaceSubClass
= 0x06, /* SCSI */
169 .bInterfaceProtocol
= 0x50, /* Bulk */
170 .eps
= (USBDescEndpoint
[]) {
172 .bEndpointAddress
= USB_DIR_IN
| 0x01,
173 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
174 .wMaxPacketSize
= 1024,
177 .bEndpointAddress
= USB_DIR_OUT
| 0x02,
178 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
179 .wMaxPacketSize
= 1024,
185 static const USBDescDevice desc_device_super
= {
187 .bMaxPacketSize0
= 9,
188 .bNumConfigurations
= 1,
189 .confs
= (USBDescConfig
[]) {
192 .bConfigurationValue
= 1,
193 .iConfiguration
= STR_CONFIG_SUPER
,
194 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_SELFPOWER
,
196 .ifs
= &desc_iface_super
,
201 static const USBDesc desc
= {
203 .idVendor
= 0x46f4, /* CRC16() of "QEMU" */
206 .iManufacturer
= STR_MANUFACTURER
,
207 .iProduct
= STR_PRODUCT
,
208 .iSerialNumber
= STR_SERIALNUMBER
,
210 .full
= &desc_device_full
,
211 .high
= &desc_device_high
,
212 .super
= &desc_device_super
,
216 static void usb_msd_copy_data(MSDState
*s
, USBPacket
*p
)
219 len
= p
->iov
.size
- p
->actual_length
;
220 if (len
> s
->scsi_len
)
222 usb_packet_copy(p
, scsi_req_get_buf(s
->req
) + s
->scsi_off
, len
);
226 if (s
->scsi_len
== 0 || s
->data_len
== 0) {
227 scsi_req_continue(s
->req
);
231 static void usb_msd_send_status(MSDState
*s
, USBPacket
*p
)
235 DPRINTF("Command status %d tag 0x%x, len %zd\n",
236 s
->csw
.status
, le32_to_cpu(s
->csw
.tag
), p
->iov
.size
);
238 assert(s
->csw
.sig
== cpu_to_le32(0x53425355));
239 len
= MIN(sizeof(s
->csw
), p
->iov
.size
);
240 usb_packet_copy(p
, &s
->csw
, len
);
241 memset(&s
->csw
, 0, sizeof(s
->csw
));
244 static void usb_msd_packet_complete(MSDState
*s
)
246 USBPacket
*p
= s
->packet
;
248 /* Set s->packet to NULL before calling usb_packet_complete
249 because another request may be issued before
250 usb_packet_complete returns. */
251 DPRINTF("Packet complete %p\n", p
);
253 usb_packet_complete(&s
->dev
, p
);
256 static void usb_msd_transfer_data(SCSIRequest
*req
, uint32_t len
)
258 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
259 USBPacket
*p
= s
->packet
;
261 assert((s
->mode
== USB_MSDM_DATAOUT
) == (req
->cmd
.mode
== SCSI_XFER_TO_DEV
));
265 usb_msd_copy_data(s
, p
);
267 if (p
&& p
->actual_length
== p
->iov
.size
) {
268 p
->status
= USB_RET_SUCCESS
; /* Clear previous ASYNC status */
269 usb_msd_packet_complete(s
);
274 static void usb_msd_command_complete(SCSIRequest
*req
, uint32_t status
, size_t resid
)
276 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
277 USBPacket
*p
= s
->packet
;
279 DPRINTF("Command complete %d tag 0x%x\n", status
, req
->tag
);
281 s
->csw
.sig
= cpu_to_le32(0x53425355);
282 s
->csw
.tag
= cpu_to_le32(req
->tag
);
283 s
->csw
.residue
= cpu_to_le32(s
->data_len
);
284 s
->csw
.status
= status
!= 0;
287 if (s
->data_len
== 0 && s
->mode
== USB_MSDM_DATAOUT
) {
288 /* A deferred packet with no write data remaining must be
289 the status read packet. */
290 usb_msd_send_status(s
, p
);
291 s
->mode
= USB_MSDM_CBW
;
292 } else if (s
->mode
== USB_MSDM_CSW
) {
293 usb_msd_send_status(s
, p
);
294 s
->mode
= USB_MSDM_CBW
;
297 int len
= (p
->iov
.size
- p
->actual_length
);
298 usb_packet_skip(p
, len
);
301 if (s
->data_len
== 0) {
302 s
->mode
= USB_MSDM_CSW
;
305 p
->status
= USB_RET_SUCCESS
; /* Clear previous ASYNC status */
306 usb_msd_packet_complete(s
);
307 } else if (s
->data_len
== 0) {
308 s
->mode
= USB_MSDM_CSW
;
314 static void usb_msd_request_cancelled(SCSIRequest
*req
)
316 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
319 scsi_req_unref(s
->req
);
325 static void usb_msd_handle_reset(USBDevice
*dev
)
327 MSDState
*s
= (MSDState
*)dev
;
331 scsi_req_cancel(s
->req
);
333 assert(s
->req
== NULL
);
336 s
->packet
->status
= USB_RET_STALL
;
337 usb_msd_packet_complete(s
);
340 s
->mode
= USB_MSDM_CBW
;
343 static void usb_msd_handle_control(USBDevice
*dev
, USBPacket
*p
,
344 int request
, int value
, int index
, int length
, uint8_t *data
)
346 MSDState
*s
= (MSDState
*)dev
;
347 SCSIDevice
*scsi_dev
;
350 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
356 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
358 /* Class specific requests. */
359 case ClassInterfaceOutRequest
| MassStorageReset
:
360 /* Reset state ready for the next CBW. */
361 s
->mode
= USB_MSDM_CBW
;
363 case ClassInterfaceRequest
| GetMaxLun
:
366 scsi_dev
= scsi_device_find(&s
->bus
, 0, 0, maxlun
+1);
367 if (scsi_dev
== NULL
) {
370 if (scsi_dev
->lun
!= maxlun
+1) {
375 DPRINTF("MaxLun %d\n", maxlun
);
377 p
->actual_length
= 1;
380 p
->status
= USB_RET_STALL
;
385 static void usb_msd_cancel_io(USBDevice
*dev
, USBPacket
*p
)
387 MSDState
*s
= DO_UPCAST(MSDState
, dev
, dev
);
389 assert(s
->packet
== p
);
393 scsi_req_cancel(s
->req
);
397 static void usb_msd_handle_data(USBDevice
*dev
, USBPacket
*p
)
399 MSDState
*s
= (MSDState
*)dev
;
401 struct usb_msd_cbw cbw
;
402 uint8_t devep
= p
->ep
->nr
;
403 SCSIDevice
*scsi_dev
;
413 if (p
->iov
.size
!= 31) {
414 error_report("usb-msd: Bad CBW size");
417 usb_packet_copy(p
, &cbw
, 31);
418 if (le32_to_cpu(cbw
.sig
) != 0x43425355) {
419 error_report("usb-msd: Bad signature %08x",
420 le32_to_cpu(cbw
.sig
));
423 DPRINTF("Command on LUN %d\n", cbw
.lun
);
424 scsi_dev
= scsi_device_find(&s
->bus
, 0, 0, cbw
.lun
);
425 if (scsi_dev
== NULL
) {
426 error_report("usb-msd: Bad LUN %d", cbw
.lun
);
429 tag
= le32_to_cpu(cbw
.tag
);
430 s
->data_len
= le32_to_cpu(cbw
.data_len
);
431 if (s
->data_len
== 0) {
432 s
->mode
= USB_MSDM_CSW
;
433 } else if (cbw
.flags
& 0x80) {
434 s
->mode
= USB_MSDM_DATAIN
;
436 s
->mode
= USB_MSDM_DATAOUT
;
438 DPRINTF("Command tag 0x%x flags %08x len %d data %d\n",
439 tag
, cbw
.flags
, cbw
.cmd_len
, s
->data_len
);
440 assert(le32_to_cpu(s
->csw
.residue
) == 0);
442 s
->req
= scsi_req_new(scsi_dev
, tag
, cbw
.lun
, cbw
.cmd
, NULL
);
444 scsi_req_print(s
->req
);
446 len
= scsi_req_enqueue(s
->req
);
448 scsi_req_continue(s
->req
);
452 case USB_MSDM_DATAOUT
:
453 DPRINTF("Data out %zd/%d\n", p
->iov
.size
, s
->data_len
);
454 if (p
->iov
.size
> s
->data_len
) {
459 usb_msd_copy_data(s
, p
);
461 if (le32_to_cpu(s
->csw
.residue
)) {
462 int len
= p
->iov
.size
- p
->actual_length
;
464 usb_packet_skip(p
, len
);
466 if (s
->data_len
== 0) {
467 s
->mode
= USB_MSDM_CSW
;
471 if (p
->actual_length
< p
->iov
.size
) {
472 DPRINTF("Deferring packet %p [wait data-out]\n", p
);
474 p
->status
= USB_RET_ASYNC
;
479 DPRINTF("Unexpected write (len %zd)\n", p
->iov
.size
);
489 case USB_MSDM_DATAOUT
:
490 if (s
->data_len
!= 0 || p
->iov
.size
< 13) {
493 /* Waiting for SCSI write to complete. */
495 p
->status
= USB_RET_ASYNC
;
499 if (p
->iov
.size
< 13) {
504 /* still in flight */
505 DPRINTF("Deferring packet %p [wait status]\n", p
);
507 p
->status
= USB_RET_ASYNC
;
509 usb_msd_send_status(s
, p
);
510 s
->mode
= USB_MSDM_CBW
;
514 case USB_MSDM_DATAIN
:
515 DPRINTF("Data in %zd/%d, scsi_len %d\n",
516 p
->iov
.size
, s
->data_len
, s
->scsi_len
);
518 usb_msd_copy_data(s
, p
);
520 if (le32_to_cpu(s
->csw
.residue
)) {
521 int len
= p
->iov
.size
- p
->actual_length
;
523 usb_packet_skip(p
, len
);
525 if (s
->data_len
== 0) {
526 s
->mode
= USB_MSDM_CSW
;
530 if (p
->actual_length
< p
->iov
.size
) {
531 DPRINTF("Deferring packet %p [wait data-in]\n", p
);
533 p
->status
= USB_RET_ASYNC
;
538 DPRINTF("Unexpected read (len %zd)\n", p
->iov
.size
);
544 DPRINTF("Bad token\n");
546 p
->status
= USB_RET_STALL
;
551 static void usb_msd_password_cb(void *opaque
, int err
)
553 MSDState
*s
= opaque
;
554 Error
*local_err
= NULL
;
557 usb_device_attach(&s
->dev
, &local_err
);
561 qerror_report_err(local_err
);
562 error_free(local_err
);
563 qdev_unplug(&s
->dev
.qdev
, NULL
);
567 static void *usb_msd_load_request(QEMUFile
*f
, SCSIRequest
*req
)
569 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
571 /* nothing to load, just store req in our state struct */
572 assert(s
->req
== NULL
);
578 static const struct SCSIBusInfo usb_msd_scsi_info_storage
= {
583 .transfer_data
= usb_msd_transfer_data
,
584 .complete
= usb_msd_command_complete
,
585 .cancel
= usb_msd_request_cancelled
,
586 .load_request
= usb_msd_load_request
,
589 static const struct SCSIBusInfo usb_msd_scsi_info_bot
= {
594 .transfer_data
= usb_msd_transfer_data
,
595 .complete
= usb_msd_command_complete
,
596 .cancel
= usb_msd_request_cancelled
,
597 .load_request
= usb_msd_load_request
,
600 static void usb_msd_realize_storage(USBDevice
*dev
, Error
**errp
)
602 MSDState
*s
= DO_UPCAST(MSDState
, dev
, dev
);
603 BlockDriverState
*bs
= s
->conf
.bs
;
604 SCSIDevice
*scsi_dev
;
608 error_setg(errp
, "drive property not set");
612 blkconf_serial(&s
->conf
, &dev
->serial
);
615 * Hack alert: this pretends to be a block device, but it's really
616 * a SCSI bus that can serve only a single device, which it
617 * creates automatically. But first it needs to detach from its
618 * blockdev, or else scsi_bus_legacy_add_drive() dies when it
621 * The hack is probably a bad idea.
623 bdrv_detach_dev(bs
, &s
->dev
.qdev
);
626 usb_desc_create_serial(dev
);
628 scsi_bus_new(&s
->bus
, sizeof(s
->bus
), DEVICE(dev
),
629 &usb_msd_scsi_info_storage
, NULL
);
630 scsi_dev
= scsi_bus_legacy_add_drive(&s
->bus
, bs
, 0, !!s
->removable
,
631 s
->conf
.bootindex
, dev
->serial
,
634 error_propagate(errp
, err
);
637 s
->bus
.qbus
.allow_hotplug
= 0;
638 usb_msd_handle_reset(dev
);
639 s
->scsi_dev
= scsi_dev
;
641 if (bdrv_key_required(bs
)) {
643 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
, s
);
644 s
->dev
.auto_attach
= 0;
651 static void usb_msd_realize_bot(USBDevice
*dev
, Error
**errp
)
653 MSDState
*s
= DO_UPCAST(MSDState
, dev
, dev
);
655 usb_desc_create_serial(dev
);
657 scsi_bus_new(&s
->bus
, sizeof(s
->bus
), DEVICE(dev
),
658 &usb_msd_scsi_info_bot
, NULL
);
659 s
->bus
.qbus
.allow_hotplug
= 0;
660 usb_msd_handle_reset(dev
);
663 static USBDevice
*usb_msd_init(USBBus
*bus
, const char *filename
)
673 /* parse -usbdevice disk: syntax into drive opts */
675 snprintf(id
, sizeof(id
), "usb%d", nr
++);
676 opts
= qemu_opts_create(qemu_find_opts("drive"), id
, 1, NULL
);
679 p1
= strchr(filename
, ':');
683 if (strstart(filename
, "format=", &p2
)) {
684 int len
= MIN(p1
- p2
, sizeof(fmt
));
685 pstrcpy(fmt
, len
, p2
);
686 qemu_opt_set(opts
, "format", fmt
);
687 } else if (*filename
!= ':') {
688 error_report("unrecognized USB mass-storage option %s", filename
);
694 error_report("block device specification needed");
697 qemu_opt_set(opts
, "file", filename
);
698 qemu_opt_set(opts
, "if", "none");
700 /* create host drive */
701 dinfo
= drive_new(opts
, 0);
707 /* create guest device */
708 dev
= usb_create(bus
, "usb-storage");
712 if (qdev_prop_set_drive(&dev
->qdev
, "drive", dinfo
->bdrv
) < 0) {
713 object_unparent(OBJECT(dev
));
716 if (qdev_init(&dev
->qdev
) < 0)
722 static const VMStateDescription vmstate_usb_msd
= {
723 .name
= "usb-storage",
725 .minimum_version_id
= 1,
726 .fields
= (VMStateField
[]) {
727 VMSTATE_USB_DEVICE(dev
, MSDState
),
728 VMSTATE_UINT32(mode
, MSDState
),
729 VMSTATE_UINT32(scsi_len
, MSDState
),
730 VMSTATE_UINT32(scsi_off
, MSDState
),
731 VMSTATE_UINT32(data_len
, MSDState
),
732 VMSTATE_UINT32(csw
.sig
, MSDState
),
733 VMSTATE_UINT32(csw
.tag
, MSDState
),
734 VMSTATE_UINT32(csw
.residue
, MSDState
),
735 VMSTATE_UINT8(csw
.status
, MSDState
),
736 VMSTATE_END_OF_LIST()
740 static Property msd_properties
[] = {
741 DEFINE_BLOCK_PROPERTIES(MSDState
, conf
),
742 DEFINE_PROP_BIT("removable", MSDState
, removable
, 0, false),
743 DEFINE_PROP_END_OF_LIST(),
746 static void usb_msd_class_initfn_common(ObjectClass
*klass
)
748 DeviceClass
*dc
= DEVICE_CLASS(klass
);
749 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
751 uc
->product_desc
= "QEMU USB MSD";
752 uc
->usb_desc
= &desc
;
753 uc
->cancel_packet
= usb_msd_cancel_io
;
754 uc
->handle_attach
= usb_desc_attach
;
755 uc
->handle_reset
= usb_msd_handle_reset
;
756 uc
->handle_control
= usb_msd_handle_control
;
757 uc
->handle_data
= usb_msd_handle_data
;
758 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
759 dc
->fw_name
= "storage";
760 dc
->vmsd
= &vmstate_usb_msd
;
763 static void usb_msd_class_initfn_storage(ObjectClass
*klass
, void *data
)
765 DeviceClass
*dc
= DEVICE_CLASS(klass
);
766 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
768 uc
->realize
= usb_msd_realize_storage
;
769 dc
->props
= msd_properties
;
770 usb_msd_class_initfn_common(klass
);
773 static void usb_msd_get_bootindex(Object
*obj
, Visitor
*v
, void *opaque
,
774 const char *name
, Error
**errp
)
776 USBDevice
*dev
= USB_DEVICE(obj
);
777 MSDState
*s
= DO_UPCAST(MSDState
, dev
, dev
);
779 visit_type_int32(v
, &s
->conf
.bootindex
, name
, errp
);
782 static void usb_msd_set_bootindex(Object
*obj
, Visitor
*v
, void *opaque
,
783 const char *name
, Error
**errp
)
785 USBDevice
*dev
= USB_DEVICE(obj
);
786 MSDState
*s
= DO_UPCAST(MSDState
, dev
, dev
);
788 Error
*local_err
= NULL
;
790 visit_type_int32(v
, &boot_index
, name
, &local_err
);
794 /* check whether bootindex is present in fw_boot_order list */
795 check_boot_index(boot_index
, &local_err
);
799 /* change bootindex to a new one */
800 s
->conf
.bootindex
= boot_index
;
803 object_property_set_int(OBJECT(s
->scsi_dev
), boot_index
, "bootindex",
809 error_propagate(errp
, local_err
);
813 static void usb_msd_instance_init(Object
*obj
)
815 object_property_add(obj
, "bootindex", "int32",
816 usb_msd_get_bootindex
,
817 usb_msd_set_bootindex
, NULL
, NULL
, NULL
);
818 object_property_set_int(obj
, -1, "bootindex", NULL
);
821 static void usb_msd_class_initfn_bot(ObjectClass
*klass
, void *data
)
823 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
825 uc
->realize
= usb_msd_realize_bot
;
826 usb_msd_class_initfn_common(klass
);
829 static const TypeInfo msd_info
= {
830 .name
= "usb-storage",
831 .parent
= TYPE_USB_DEVICE
,
832 .instance_size
= sizeof(MSDState
),
833 .class_init
= usb_msd_class_initfn_storage
,
834 .instance_init
= usb_msd_instance_init
,
837 static const TypeInfo bot_info
= {
839 .parent
= TYPE_USB_DEVICE
,
840 .instance_size
= sizeof(MSDState
),
841 .class_init
= usb_msd_class_initfn_bot
,
844 static void usb_msd_register_types(void)
846 type_register_static(&msd_info
);
847 type_register_static(&bot_info
);
848 usb_legacy_register("usb-storage", "disk", usb_msd_init
);
851 type_init(usb_msd_register_types
)