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"
24 #define DPRINTF(fmt, ...) \
25 do { printf("usb-msd: " fmt , ## __VA_ARGS__); } while (0)
27 #define DPRINTF(fmt, ...) do {} while(0)
31 #define MassStorageReset 0xff
32 #define GetMaxLun 0xfe
35 USB_MSDM_CBW
, /* Command Block. */
36 USB_MSDM_DATAOUT
, /* Transfer data to device. */
37 USB_MSDM_DATAIN
, /* Transfer data from device. */
38 USB_MSDM_CSW
/* Command Status. */
54 struct usb_msd_csw csw
;
57 /* For async completion. */
59 /* usb-storage only */
83 static const USBDescStrings desc_strings
= {
84 [STR_MANUFACTURER
] = "QEMU",
85 [STR_PRODUCT
] = "QEMU USB HARDDRIVE",
86 [STR_SERIALNUMBER
] = "1",
87 [STR_CONFIG_FULL
] = "Full speed config (usb 1.1)",
88 [STR_CONFIG_HIGH
] = "High speed config (usb 2.0)",
89 [STR_CONFIG_SUPER
] = "Super speed config (usb 3.0)",
92 static const USBDescIface desc_iface_full
= {
93 .bInterfaceNumber
= 0,
95 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
96 .bInterfaceSubClass
= 0x06, /* SCSI */
97 .bInterfaceProtocol
= 0x50, /* Bulk */
98 .eps
= (USBDescEndpoint
[]) {
100 .bEndpointAddress
= USB_DIR_IN
| 0x01,
101 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
102 .wMaxPacketSize
= 64,
104 .bEndpointAddress
= USB_DIR_OUT
| 0x02,
105 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
106 .wMaxPacketSize
= 64,
111 static const USBDescDevice desc_device_full
= {
113 .bMaxPacketSize0
= 8,
114 .bNumConfigurations
= 1,
115 .confs
= (USBDescConfig
[]) {
118 .bConfigurationValue
= 1,
119 .iConfiguration
= STR_CONFIG_FULL
,
120 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_SELFPOWER
,
122 .ifs
= &desc_iface_full
,
127 static const USBDescIface desc_iface_high
= {
128 .bInterfaceNumber
= 0,
130 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
131 .bInterfaceSubClass
= 0x06, /* SCSI */
132 .bInterfaceProtocol
= 0x50, /* Bulk */
133 .eps
= (USBDescEndpoint
[]) {
135 .bEndpointAddress
= USB_DIR_IN
| 0x01,
136 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
137 .wMaxPacketSize
= 512,
139 .bEndpointAddress
= USB_DIR_OUT
| 0x02,
140 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
141 .wMaxPacketSize
= 512,
146 static const USBDescDevice desc_device_high
= {
148 .bMaxPacketSize0
= 64,
149 .bNumConfigurations
= 1,
150 .confs
= (USBDescConfig
[]) {
153 .bConfigurationValue
= 1,
154 .iConfiguration
= STR_CONFIG_HIGH
,
155 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_SELFPOWER
,
157 .ifs
= &desc_iface_high
,
162 static const USBDescIface desc_iface_super
= {
163 .bInterfaceNumber
= 0,
165 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
166 .bInterfaceSubClass
= 0x06, /* SCSI */
167 .bInterfaceProtocol
= 0x50, /* Bulk */
168 .eps
= (USBDescEndpoint
[]) {
170 .bEndpointAddress
= USB_DIR_IN
| 0x01,
171 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
172 .wMaxPacketSize
= 1024,
175 .bEndpointAddress
= USB_DIR_OUT
| 0x02,
176 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
177 .wMaxPacketSize
= 1024,
183 static const USBDescDevice desc_device_super
= {
185 .bMaxPacketSize0
= 9,
186 .bNumConfigurations
= 1,
187 .confs
= (USBDescConfig
[]) {
190 .bConfigurationValue
= 1,
191 .iConfiguration
= STR_CONFIG_SUPER
,
192 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_SELFPOWER
,
194 .ifs
= &desc_iface_super
,
199 static const USBDesc desc
= {
201 .idVendor
= 0x46f4, /* CRC16() of "QEMU" */
204 .iManufacturer
= STR_MANUFACTURER
,
205 .iProduct
= STR_PRODUCT
,
206 .iSerialNumber
= STR_SERIALNUMBER
,
208 .full
= &desc_device_full
,
209 .high
= &desc_device_high
,
210 .super
= &desc_device_super
,
214 static void usb_msd_copy_data(MSDState
*s
, USBPacket
*p
)
217 len
= p
->iov
.size
- p
->actual_length
;
218 if (len
> s
->scsi_len
)
220 usb_packet_copy(p
, scsi_req_get_buf(s
->req
) + s
->scsi_off
, len
);
224 if (s
->scsi_len
== 0 || s
->data_len
== 0) {
225 scsi_req_continue(s
->req
);
229 static void usb_msd_send_status(MSDState
*s
, USBPacket
*p
)
233 DPRINTF("Command status %d tag 0x%x, len %zd\n",
234 s
->csw
.status
, le32_to_cpu(s
->csw
.tag
), p
->iov
.size
);
236 assert(s
->csw
.sig
== cpu_to_le32(0x53425355));
237 len
= MIN(sizeof(s
->csw
), p
->iov
.size
);
238 usb_packet_copy(p
, &s
->csw
, len
);
239 memset(&s
->csw
, 0, sizeof(s
->csw
));
242 static void usb_msd_packet_complete(MSDState
*s
)
244 USBPacket
*p
= s
->packet
;
246 /* Set s->packet to NULL before calling usb_packet_complete
247 because another request may be issued before
248 usb_packet_complete returns. */
249 DPRINTF("Packet complete %p\n", p
);
251 usb_packet_complete(&s
->dev
, p
);
254 static void usb_msd_transfer_data(SCSIRequest
*req
, uint32_t len
)
256 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
257 USBPacket
*p
= s
->packet
;
259 assert((s
->mode
== USB_MSDM_DATAOUT
) == (req
->cmd
.mode
== SCSI_XFER_TO_DEV
));
263 usb_msd_copy_data(s
, p
);
265 if (p
&& p
->actual_length
== p
->iov
.size
) {
266 p
->status
= USB_RET_SUCCESS
; /* Clear previous ASYNC status */
267 usb_msd_packet_complete(s
);
272 static void usb_msd_command_complete(SCSIRequest
*req
, uint32_t status
, size_t resid
)
274 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
275 USBPacket
*p
= s
->packet
;
277 DPRINTF("Command complete %d tag 0x%x\n", status
, req
->tag
);
279 s
->csw
.sig
= cpu_to_le32(0x53425355);
280 s
->csw
.tag
= cpu_to_le32(req
->tag
);
281 s
->csw
.residue
= cpu_to_le32(s
->data_len
);
282 s
->csw
.status
= status
!= 0;
285 if (s
->data_len
== 0 && s
->mode
== USB_MSDM_DATAOUT
) {
286 /* A deferred packet with no write data remaining must be
287 the status read packet. */
288 usb_msd_send_status(s
, p
);
289 s
->mode
= USB_MSDM_CBW
;
290 } else if (s
->mode
== USB_MSDM_CSW
) {
291 usb_msd_send_status(s
, p
);
292 s
->mode
= USB_MSDM_CBW
;
295 int len
= (p
->iov
.size
- p
->actual_length
);
296 usb_packet_skip(p
, len
);
299 if (s
->data_len
== 0) {
300 s
->mode
= USB_MSDM_CSW
;
303 p
->status
= USB_RET_SUCCESS
; /* Clear previous ASYNC status */
304 usb_msd_packet_complete(s
);
305 } else if (s
->data_len
== 0) {
306 s
->mode
= USB_MSDM_CSW
;
312 static void usb_msd_request_cancelled(SCSIRequest
*req
)
314 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
317 scsi_req_unref(s
->req
);
323 static void usb_msd_handle_reset(USBDevice
*dev
)
325 MSDState
*s
= (MSDState
*)dev
;
329 scsi_req_cancel(s
->req
);
331 assert(s
->req
== NULL
);
334 s
->packet
->status
= USB_RET_STALL
;
335 usb_msd_packet_complete(s
);
338 s
->mode
= USB_MSDM_CBW
;
341 static void usb_msd_handle_control(USBDevice
*dev
, USBPacket
*p
,
342 int request
, int value
, int index
, int length
, uint8_t *data
)
344 MSDState
*s
= (MSDState
*)dev
;
345 SCSIDevice
*scsi_dev
;
348 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
354 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
356 /* Class specific requests. */
357 case ClassInterfaceOutRequest
| MassStorageReset
:
358 /* Reset state ready for the next CBW. */
359 s
->mode
= USB_MSDM_CBW
;
361 case ClassInterfaceRequest
| GetMaxLun
:
364 scsi_dev
= scsi_device_find(&s
->bus
, 0, 0, maxlun
+1);
365 if (scsi_dev
== NULL
) {
368 if (scsi_dev
->lun
!= maxlun
+1) {
373 DPRINTF("MaxLun %d\n", maxlun
);
375 p
->actual_length
= 1;
378 p
->status
= USB_RET_STALL
;
383 static void usb_msd_cancel_io(USBDevice
*dev
, USBPacket
*p
)
385 MSDState
*s
= DO_UPCAST(MSDState
, dev
, dev
);
387 assert(s
->packet
== p
);
391 scsi_req_cancel(s
->req
);
395 static void usb_msd_handle_data(USBDevice
*dev
, USBPacket
*p
)
397 MSDState
*s
= (MSDState
*)dev
;
399 struct usb_msd_cbw cbw
;
400 uint8_t devep
= p
->ep
->nr
;
401 SCSIDevice
*scsi_dev
;
411 if (p
->iov
.size
!= 31) {
412 fprintf(stderr
, "usb-msd: Bad CBW size");
415 usb_packet_copy(p
, &cbw
, 31);
416 if (le32_to_cpu(cbw
.sig
) != 0x43425355) {
417 fprintf(stderr
, "usb-msd: Bad signature %08x\n",
418 le32_to_cpu(cbw
.sig
));
421 DPRINTF("Command on LUN %d\n", cbw
.lun
);
422 scsi_dev
= scsi_device_find(&s
->bus
, 0, 0, cbw
.lun
);
423 if (scsi_dev
== NULL
) {
424 fprintf(stderr
, "usb-msd: Bad LUN %d\n", cbw
.lun
);
427 tag
= le32_to_cpu(cbw
.tag
);
428 s
->data_len
= le32_to_cpu(cbw
.data_len
);
429 if (s
->data_len
== 0) {
430 s
->mode
= USB_MSDM_CSW
;
431 } else if (cbw
.flags
& 0x80) {
432 s
->mode
= USB_MSDM_DATAIN
;
434 s
->mode
= USB_MSDM_DATAOUT
;
436 DPRINTF("Command tag 0x%x flags %08x len %d data %d\n",
437 tag
, cbw
.flags
, cbw
.cmd_len
, s
->data_len
);
438 assert(le32_to_cpu(s
->csw
.residue
) == 0);
440 s
->req
= scsi_req_new(scsi_dev
, tag
, cbw
.lun
, cbw
.cmd
, NULL
);
442 scsi_req_print(s
->req
);
444 len
= scsi_req_enqueue(s
->req
);
446 scsi_req_continue(s
->req
);
450 case USB_MSDM_DATAOUT
:
451 DPRINTF("Data out %zd/%d\n", p
->iov
.size
, s
->data_len
);
452 if (p
->iov
.size
> s
->data_len
) {
457 usb_msd_copy_data(s
, p
);
459 if (le32_to_cpu(s
->csw
.residue
)) {
460 int len
= p
->iov
.size
- p
->actual_length
;
462 usb_packet_skip(p
, len
);
464 if (s
->data_len
== 0) {
465 s
->mode
= USB_MSDM_CSW
;
469 if (p
->actual_length
< p
->iov
.size
) {
470 DPRINTF("Deferring packet %p [wait data-out]\n", p
);
472 p
->status
= USB_RET_ASYNC
;
477 DPRINTF("Unexpected write (len %zd)\n", p
->iov
.size
);
487 case USB_MSDM_DATAOUT
:
488 if (s
->data_len
!= 0 || p
->iov
.size
< 13) {
491 /* Waiting for SCSI write to complete. */
493 p
->status
= USB_RET_ASYNC
;
497 if (p
->iov
.size
< 13) {
502 /* still in flight */
503 DPRINTF("Deferring packet %p [wait status]\n", p
);
505 p
->status
= USB_RET_ASYNC
;
507 usb_msd_send_status(s
, p
);
508 s
->mode
= USB_MSDM_CBW
;
512 case USB_MSDM_DATAIN
:
513 DPRINTF("Data in %zd/%d, scsi_len %d\n",
514 p
->iov
.size
, s
->data_len
, s
->scsi_len
);
516 usb_msd_copy_data(s
, p
);
518 if (le32_to_cpu(s
->csw
.residue
)) {
519 int len
= p
->iov
.size
- p
->actual_length
;
521 usb_packet_skip(p
, len
);
523 if (s
->data_len
== 0) {
524 s
->mode
= USB_MSDM_CSW
;
528 if (p
->actual_length
< p
->iov
.size
) {
529 DPRINTF("Deferring packet %p [wait data-in]\n", p
);
531 p
->status
= USB_RET_ASYNC
;
536 DPRINTF("Unexpected read (len %zd)\n", p
->iov
.size
);
542 DPRINTF("Bad token\n");
544 p
->status
= USB_RET_STALL
;
549 static void usb_msd_password_cb(void *opaque
, int err
)
551 MSDState
*s
= opaque
;
554 err
= usb_device_attach(&s
->dev
);
557 qdev_unplug(&s
->dev
.qdev
, NULL
);
560 static void *usb_msd_load_request(QEMUFile
*f
, SCSIRequest
*req
)
562 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
564 /* nothing to load, just store req in our state struct */
565 assert(s
->req
== NULL
);
571 static const struct SCSIBusInfo usb_msd_scsi_info_storage
= {
576 .transfer_data
= usb_msd_transfer_data
,
577 .complete
= usb_msd_command_complete
,
578 .cancel
= usb_msd_request_cancelled
,
579 .load_request
= usb_msd_load_request
,
582 static const struct SCSIBusInfo usb_msd_scsi_info_bot
= {
587 .transfer_data
= usb_msd_transfer_data
,
588 .complete
= usb_msd_command_complete
,
589 .cancel
= usb_msd_request_cancelled
,
590 .load_request
= usb_msd_load_request
,
593 static int usb_msd_initfn_storage(USBDevice
*dev
)
595 MSDState
*s
= DO_UPCAST(MSDState
, dev
, dev
);
596 BlockDriverState
*bs
= s
->conf
.bs
;
597 SCSIDevice
*scsi_dev
;
601 error_report("drive property not set");
605 blkconf_serial(&s
->conf
, &dev
->serial
);
608 * Hack alert: this pretends to be a block device, but it's really
609 * a SCSI bus that can serve only a single device, which it
610 * creates automatically. But first it needs to detach from its
611 * blockdev, or else scsi_bus_legacy_add_drive() dies when it
614 * The hack is probably a bad idea.
616 bdrv_detach_dev(bs
, &s
->dev
.qdev
);
619 usb_desc_create_serial(dev
);
621 scsi_bus_new(&s
->bus
, sizeof(s
->bus
), DEVICE(dev
),
622 &usb_msd_scsi_info_storage
, NULL
);
623 scsi_dev
= scsi_bus_legacy_add_drive(&s
->bus
, bs
, 0, !!s
->removable
,
624 s
->conf
.bootindex
, dev
->serial
,
629 s
->bus
.qbus
.allow_hotplug
= 0;
630 usb_msd_handle_reset(dev
);
632 if (bdrv_key_required(bs
)) {
634 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
, s
);
635 s
->dev
.auto_attach
= 0;
644 static int usb_msd_initfn_bot(USBDevice
*dev
)
646 MSDState
*s
= DO_UPCAST(MSDState
, dev
, dev
);
648 usb_desc_create_serial(dev
);
650 scsi_bus_new(&s
->bus
, sizeof(s
->bus
), DEVICE(dev
),
651 &usb_msd_scsi_info_bot
, NULL
);
652 s
->bus
.qbus
.allow_hotplug
= 0;
653 usb_msd_handle_reset(dev
);
658 static USBDevice
*usb_msd_init(USBBus
*bus
, const char *filename
)
668 /* parse -usbdevice disk: syntax into drive opts */
669 snprintf(id
, sizeof(id
), "usb%d", nr
++);
670 opts
= qemu_opts_create(qemu_find_opts("drive"), id
, 0, NULL
);
672 p1
= strchr(filename
, ':');
676 if (strstart(filename
, "format=", &p2
)) {
677 int len
= MIN(p1
- p2
, sizeof(fmt
));
678 pstrcpy(fmt
, len
, p2
);
679 qemu_opt_set(opts
, "format", fmt
);
680 } else if (*filename
!= ':') {
681 printf("unrecognized USB mass-storage option %s\n", filename
);
687 printf("block device specification needed\n");
690 qemu_opt_set(opts
, "file", filename
);
691 qemu_opt_set(opts
, "if", "none");
693 /* create host drive */
694 dinfo
= drive_init(opts
, 0);
700 /* create guest device */
701 dev
= usb_create(bus
, "usb-storage");
705 if (qdev_prop_set_drive(&dev
->qdev
, "drive", dinfo
->bdrv
) < 0) {
706 object_unparent(OBJECT(dev
));
709 if (qdev_init(&dev
->qdev
) < 0)
715 static const VMStateDescription vmstate_usb_msd
= {
716 .name
= "usb-storage",
718 .minimum_version_id
= 1,
719 .fields
= (VMStateField
[]) {
720 VMSTATE_USB_DEVICE(dev
, MSDState
),
721 VMSTATE_UINT32(mode
, MSDState
),
722 VMSTATE_UINT32(scsi_len
, MSDState
),
723 VMSTATE_UINT32(scsi_off
, MSDState
),
724 VMSTATE_UINT32(data_len
, MSDState
),
725 VMSTATE_UINT32(csw
.sig
, MSDState
),
726 VMSTATE_UINT32(csw
.tag
, MSDState
),
727 VMSTATE_UINT32(csw
.residue
, MSDState
),
728 VMSTATE_UINT8(csw
.status
, MSDState
),
729 VMSTATE_END_OF_LIST()
733 static Property msd_properties
[] = {
734 DEFINE_BLOCK_PROPERTIES(MSDState
, conf
),
735 DEFINE_PROP_BIT("removable", MSDState
, removable
, 0, false),
736 DEFINE_PROP_END_OF_LIST(),
739 static void usb_msd_class_initfn_common(ObjectClass
*klass
)
741 DeviceClass
*dc
= DEVICE_CLASS(klass
);
742 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
744 uc
->product_desc
= "QEMU USB MSD";
745 uc
->usb_desc
= &desc
;
746 uc
->cancel_packet
= usb_msd_cancel_io
;
747 uc
->handle_attach
= usb_desc_attach
;
748 uc
->handle_reset
= usb_msd_handle_reset
;
749 uc
->handle_control
= usb_msd_handle_control
;
750 uc
->handle_data
= usb_msd_handle_data
;
751 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
752 dc
->fw_name
= "storage";
753 dc
->vmsd
= &vmstate_usb_msd
;
756 static void usb_msd_class_initfn_storage(ObjectClass
*klass
, void *data
)
758 DeviceClass
*dc
= DEVICE_CLASS(klass
);
759 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
761 uc
->init
= usb_msd_initfn_storage
;
762 dc
->props
= msd_properties
;
763 usb_msd_class_initfn_common(klass
);
766 static void usb_msd_class_initfn_bot(ObjectClass
*klass
, void *data
)
768 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
770 uc
->init
= usb_msd_initfn_bot
;
771 usb_msd_class_initfn_common(klass
);
774 static const TypeInfo msd_info
= {
775 .name
= "usb-storage",
776 .parent
= TYPE_USB_DEVICE
,
777 .instance_size
= sizeof(MSDState
),
778 .class_init
= usb_msd_class_initfn_storage
,
781 static const TypeInfo bot_info
= {
783 .parent
= TYPE_USB_DEVICE
,
784 .instance_size
= sizeof(MSDState
),
785 .class_init
= usb_msd_class_initfn_bot
,
788 static void usb_msd_register_types(void)
790 type_register_static(&msd_info
);
791 type_register_static(&bot_info
);
792 usb_legacy_register("usb-storage", "disk", usb_msd_init
);
795 type_init(usb_msd_register_types
)