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"
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 */
84 static const USBDescStrings desc_strings
= {
85 [STR_MANUFACTURER
] = "QEMU",
86 [STR_PRODUCT
] = "QEMU USB HARDDRIVE",
87 [STR_SERIALNUMBER
] = "1",
88 [STR_CONFIG_FULL
] = "Full speed config (usb 1.1)",
89 [STR_CONFIG_HIGH
] = "High speed config (usb 2.0)",
90 [STR_CONFIG_SUPER
] = "Super speed config (usb 3.0)",
93 static const USBDescIface desc_iface_full
= {
94 .bInterfaceNumber
= 0,
96 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
97 .bInterfaceSubClass
= 0x06, /* SCSI */
98 .bInterfaceProtocol
= 0x50, /* Bulk */
99 .eps
= (USBDescEndpoint
[]) {
101 .bEndpointAddress
= USB_DIR_IN
| 0x01,
102 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
103 .wMaxPacketSize
= 64,
105 .bEndpointAddress
= USB_DIR_OUT
| 0x02,
106 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
107 .wMaxPacketSize
= 64,
112 static const USBDescDevice desc_device_full
= {
114 .bMaxPacketSize0
= 8,
115 .bNumConfigurations
= 1,
116 .confs
= (USBDescConfig
[]) {
119 .bConfigurationValue
= 1,
120 .iConfiguration
= STR_CONFIG_FULL
,
121 .bmAttributes
= 0xc0,
123 .ifs
= &desc_iface_full
,
128 static const USBDescIface desc_iface_high
= {
129 .bInterfaceNumber
= 0,
131 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
132 .bInterfaceSubClass
= 0x06, /* SCSI */
133 .bInterfaceProtocol
= 0x50, /* Bulk */
134 .eps
= (USBDescEndpoint
[]) {
136 .bEndpointAddress
= USB_DIR_IN
| 0x01,
137 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
138 .wMaxPacketSize
= 512,
140 .bEndpointAddress
= USB_DIR_OUT
| 0x02,
141 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
142 .wMaxPacketSize
= 512,
147 static const USBDescDevice desc_device_high
= {
149 .bMaxPacketSize0
= 64,
150 .bNumConfigurations
= 1,
151 .confs
= (USBDescConfig
[]) {
154 .bConfigurationValue
= 1,
155 .iConfiguration
= STR_CONFIG_HIGH
,
156 .bmAttributes
= 0xc0,
158 .ifs
= &desc_iface_high
,
163 static const USBDescIface desc_iface_super
= {
164 .bInterfaceNumber
= 0,
166 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
167 .bInterfaceSubClass
= 0x06, /* SCSI */
168 .bInterfaceProtocol
= 0x50, /* Bulk */
169 .eps
= (USBDescEndpoint
[]) {
171 .bEndpointAddress
= USB_DIR_IN
| 0x01,
172 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
173 .wMaxPacketSize
= 1024,
176 .bEndpointAddress
= USB_DIR_OUT
| 0x02,
177 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
178 .wMaxPacketSize
= 1024,
184 static const USBDescDevice desc_device_super
= {
186 .bMaxPacketSize0
= 9,
187 .bNumConfigurations
= 1,
188 .confs
= (USBDescConfig
[]) {
191 .bConfigurationValue
= 1,
192 .iConfiguration
= STR_CONFIG_SUPER
,
193 .bmAttributes
= 0xc0,
195 .ifs
= &desc_iface_super
,
200 static const USBDesc desc
= {
202 .idVendor
= 0x46f4, /* CRC16() of "QEMU" */
205 .iManufacturer
= STR_MANUFACTURER
,
206 .iProduct
= STR_PRODUCT
,
207 .iSerialNumber
= STR_SERIALNUMBER
,
209 .full
= &desc_device_full
,
210 .high
= &desc_device_high
,
211 .super
= &desc_device_super
,
215 static void usb_msd_copy_data(MSDState
*s
, USBPacket
*p
)
218 len
= p
->iov
.size
- p
->actual_length
;
219 if (len
> s
->scsi_len
)
221 usb_packet_copy(p
, scsi_req_get_buf(s
->req
) + s
->scsi_off
, len
);
225 if (s
->scsi_len
== 0 || s
->data_len
== 0) {
226 scsi_req_continue(s
->req
);
230 static void usb_msd_send_status(MSDState
*s
, USBPacket
*p
)
234 DPRINTF("Command status %d tag 0x%x, len %zd\n",
235 s
->csw
.status
, le32_to_cpu(s
->csw
.tag
), p
->iov
.size
);
237 assert(s
->csw
.sig
== cpu_to_le32(0x53425355));
238 len
= MIN(sizeof(s
->csw
), p
->iov
.size
);
239 usb_packet_copy(p
, &s
->csw
, len
);
240 memset(&s
->csw
, 0, sizeof(s
->csw
));
243 static void usb_msd_packet_complete(MSDState
*s
)
245 USBPacket
*p
= s
->packet
;
247 /* Set s->packet to NULL before calling usb_packet_complete
248 because another request may be issued before
249 usb_packet_complete returns. */
250 DPRINTF("Packet complete %p\n", p
);
252 usb_packet_complete(&s
->dev
, p
);
255 static void usb_msd_transfer_data(SCSIRequest
*req
, uint32_t len
)
257 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
258 USBPacket
*p
= s
->packet
;
260 assert((s
->mode
== USB_MSDM_DATAOUT
) == (req
->cmd
.mode
== SCSI_XFER_TO_DEV
));
264 usb_msd_copy_data(s
, p
);
266 if (p
&& p
->actual_length
== p
->iov
.size
) {
267 p
->status
= USB_RET_SUCCESS
; /* Clear previous ASYNC status */
268 usb_msd_packet_complete(s
);
273 static void usb_msd_command_complete(SCSIRequest
*req
, uint32_t status
, size_t resid
)
275 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
276 USBPacket
*p
= s
->packet
;
278 DPRINTF("Command complete %d tag 0x%x\n", status
, req
->tag
);
280 s
->csw
.sig
= cpu_to_le32(0x53425355);
281 s
->csw
.tag
= cpu_to_le32(req
->tag
);
282 s
->csw
.residue
= cpu_to_le32(s
->data_len
);
283 s
->csw
.status
= status
!= 0;
286 if (s
->data_len
== 0 && s
->mode
== USB_MSDM_DATAOUT
) {
287 /* A deferred packet with no write data remaining must be
288 the status read packet. */
289 usb_msd_send_status(s
, p
);
290 s
->mode
= USB_MSDM_CBW
;
291 } else if (s
->mode
== USB_MSDM_CSW
) {
292 usb_msd_send_status(s
, p
);
293 s
->mode
= USB_MSDM_CBW
;
296 int len
= (p
->iov
.size
- p
->actual_length
);
297 usb_packet_skip(p
, len
);
300 if (s
->data_len
== 0) {
301 s
->mode
= USB_MSDM_CSW
;
304 p
->status
= USB_RET_SUCCESS
; /* Clear previous ASYNC status */
305 usb_msd_packet_complete(s
);
306 } else if (s
->data_len
== 0) {
307 s
->mode
= USB_MSDM_CSW
;
313 static void usb_msd_request_cancelled(SCSIRequest
*req
)
315 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
318 scsi_req_unref(s
->req
);
324 static void usb_msd_handle_reset(USBDevice
*dev
)
326 MSDState
*s
= (MSDState
*)dev
;
330 scsi_req_cancel(s
->req
);
332 assert(s
->req
== NULL
);
335 s
->packet
->status
= USB_RET_STALL
;
336 usb_msd_packet_complete(s
);
339 s
->mode
= USB_MSDM_CBW
;
342 static void usb_msd_handle_control(USBDevice
*dev
, USBPacket
*p
,
343 int request
, int value
, int index
, int length
, uint8_t *data
)
345 MSDState
*s
= (MSDState
*)dev
;
346 SCSIDevice
*scsi_dev
;
349 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
355 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
357 /* Class specific requests. */
358 case ClassInterfaceOutRequest
| MassStorageReset
:
359 /* Reset state ready for the next CBW. */
360 s
->mode
= USB_MSDM_CBW
;
362 case ClassInterfaceRequest
| GetMaxLun
:
365 scsi_dev
= scsi_device_find(&s
->bus
, 0, 0, maxlun
+1);
366 if (scsi_dev
== NULL
) {
369 if (scsi_dev
->lun
!= maxlun
+1) {
374 DPRINTF("MaxLun %d\n", maxlun
);
376 p
->actual_length
= 1;
379 p
->status
= USB_RET_STALL
;
384 static void usb_msd_cancel_io(USBDevice
*dev
, USBPacket
*p
)
386 MSDState
*s
= DO_UPCAST(MSDState
, dev
, dev
);
388 assert(s
->packet
== p
);
392 scsi_req_cancel(s
->req
);
396 static void usb_msd_handle_data(USBDevice
*dev
, USBPacket
*p
)
398 MSDState
*s
= (MSDState
*)dev
;
400 struct usb_msd_cbw cbw
;
401 uint8_t devep
= p
->ep
->nr
;
402 SCSIDevice
*scsi_dev
;
412 if (p
->iov
.size
!= 31) {
413 fprintf(stderr
, "usb-msd: Bad CBW size");
416 usb_packet_copy(p
, &cbw
, 31);
417 if (le32_to_cpu(cbw
.sig
) != 0x43425355) {
418 fprintf(stderr
, "usb-msd: Bad signature %08x\n",
419 le32_to_cpu(cbw
.sig
));
422 DPRINTF("Command on LUN %d\n", cbw
.lun
);
423 scsi_dev
= scsi_device_find(&s
->bus
, 0, 0, cbw
.lun
);
424 if (scsi_dev
== NULL
) {
425 fprintf(stderr
, "usb-msd: Bad LUN %d\n", cbw
.lun
);
428 tag
= le32_to_cpu(cbw
.tag
);
429 s
->data_len
= le32_to_cpu(cbw
.data_len
);
430 if (s
->data_len
== 0) {
431 s
->mode
= USB_MSDM_CSW
;
432 } else if (cbw
.flags
& 0x80) {
433 s
->mode
= USB_MSDM_DATAIN
;
435 s
->mode
= USB_MSDM_DATAOUT
;
437 DPRINTF("Command tag 0x%x flags %08x len %d data %d\n",
438 tag
, cbw
.flags
, cbw
.cmd_len
, s
->data_len
);
439 assert(le32_to_cpu(s
->csw
.residue
) == 0);
441 s
->req
= scsi_req_new(scsi_dev
, tag
, cbw
.lun
, cbw
.cmd
, NULL
);
443 scsi_req_print(s
->req
);
445 len
= scsi_req_enqueue(s
->req
);
447 scsi_req_continue(s
->req
);
451 case USB_MSDM_DATAOUT
:
452 DPRINTF("Data out %zd/%d\n", p
->iov
.size
, s
->data_len
);
453 if (p
->iov
.size
> s
->data_len
) {
458 usb_msd_copy_data(s
, p
);
460 if (le32_to_cpu(s
->csw
.residue
)) {
461 int len
= p
->iov
.size
- p
->actual_length
;
463 usb_packet_skip(p
, len
);
465 if (s
->data_len
== 0) {
466 s
->mode
= USB_MSDM_CSW
;
470 if (p
->actual_length
< p
->iov
.size
) {
471 DPRINTF("Deferring packet %p [wait data-out]\n", p
);
473 p
->status
= USB_RET_ASYNC
;
478 DPRINTF("Unexpected write (len %zd)\n", p
->iov
.size
);
488 case USB_MSDM_DATAOUT
:
489 if (s
->data_len
!= 0 || p
->iov
.size
< 13) {
492 /* Waiting for SCSI write to complete. */
494 p
->status
= USB_RET_ASYNC
;
498 if (p
->iov
.size
< 13) {
503 /* still in flight */
504 DPRINTF("Deferring packet %p [wait status]\n", p
);
506 p
->status
= USB_RET_ASYNC
;
508 usb_msd_send_status(s
, p
);
509 s
->mode
= USB_MSDM_CBW
;
513 case USB_MSDM_DATAIN
:
514 DPRINTF("Data in %zd/%d, scsi_len %d\n",
515 p
->iov
.size
, s
->data_len
, s
->scsi_len
);
517 usb_msd_copy_data(s
, p
);
519 if (le32_to_cpu(s
->csw
.residue
)) {
520 int len
= p
->iov
.size
- p
->actual_length
;
522 usb_packet_skip(p
, len
);
524 if (s
->data_len
== 0) {
525 s
->mode
= USB_MSDM_CSW
;
529 if (p
->actual_length
< p
->iov
.size
) {
530 DPRINTF("Deferring packet %p [wait data-in]\n", p
);
532 p
->status
= USB_RET_ASYNC
;
537 DPRINTF("Unexpected read (len %zd)\n", p
->iov
.size
);
543 DPRINTF("Bad token\n");
545 p
->status
= USB_RET_STALL
;
550 static void usb_msd_password_cb(void *opaque
, int err
)
552 MSDState
*s
= opaque
;
555 err
= usb_device_attach(&s
->dev
);
558 qdev_unplug(&s
->dev
.qdev
, NULL
);
561 static void *usb_msd_load_request(QEMUFile
*f
, SCSIRequest
*req
)
563 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
565 /* nothing to load, just store req in our state struct */
566 assert(s
->req
== NULL
);
572 static const struct SCSIBusInfo usb_msd_scsi_info_storage
= {
577 .transfer_data
= usb_msd_transfer_data
,
578 .complete
= usb_msd_command_complete
,
579 .cancel
= usb_msd_request_cancelled
,
580 .load_request
= usb_msd_load_request
,
583 static const struct SCSIBusInfo usb_msd_scsi_info_bot
= {
588 .transfer_data
= usb_msd_transfer_data
,
589 .complete
= usb_msd_command_complete
,
590 .cancel
= usb_msd_request_cancelled
,
591 .load_request
= usb_msd_load_request
,
594 static int usb_msd_initfn_storage(USBDevice
*dev
)
596 MSDState
*s
= DO_UPCAST(MSDState
, dev
, dev
);
597 BlockDriverState
*bs
= s
->conf
.bs
;
598 SCSIDevice
*scsi_dev
;
601 error_report("drive property not set");
605 blkconf_serial(&s
->conf
, &s
->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
);
620 usb_desc_set_string(dev
, STR_SERIALNUMBER
, s
->serial
);
622 usb_desc_create_serial(dev
);
626 scsi_bus_new(&s
->bus
, &s
->dev
.qdev
, &usb_msd_scsi_info_storage
);
627 scsi_dev
= scsi_bus_legacy_add_drive(&s
->bus
, bs
, 0, !!s
->removable
,
632 s
->bus
.qbus
.allow_hotplug
= 0;
633 usb_msd_handle_reset(dev
);
635 if (bdrv_key_required(bs
)) {
637 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
, s
);
638 s
->dev
.auto_attach
= 0;
647 static int usb_msd_initfn_bot(USBDevice
*dev
)
649 MSDState
*s
= DO_UPCAST(MSDState
, dev
, dev
);
651 usb_desc_create_serial(dev
);
653 scsi_bus_new(&s
->bus
, &s
->dev
.qdev
, &usb_msd_scsi_info_bot
);
654 s
->bus
.qbus
.allow_hotplug
= 0;
655 usb_msd_handle_reset(dev
);
660 static USBDevice
*usb_msd_init(USBBus
*bus
, const char *filename
)
670 /* parse -usbdevice disk: syntax into drive opts */
671 snprintf(id
, sizeof(id
), "usb%d", nr
++);
672 opts
= qemu_opts_create(qemu_find_opts("drive"), id
, 0, NULL
);
674 p1
= strchr(filename
, ':');
678 if (strstart(filename
, "format=", &p2
)) {
679 int len
= MIN(p1
- p2
, sizeof(fmt
));
680 pstrcpy(fmt
, len
, p2
);
681 qemu_opt_set(opts
, "format", fmt
);
682 } else if (*filename
!= ':') {
683 printf("unrecognized USB mass-storage option %s\n", filename
);
689 printf("block device specification needed\n");
692 qemu_opt_set(opts
, "file", filename
);
693 qemu_opt_set(opts
, "if", "none");
695 /* create host drive */
696 dinfo
= drive_init(opts
, 0);
702 /* create guest device */
703 dev
= usb_create(bus
, "usb-storage");
707 if (qdev_prop_set_drive(&dev
->qdev
, "drive", dinfo
->bdrv
) < 0) {
708 qdev_free(&dev
->qdev
);
711 if (qdev_init(&dev
->qdev
) < 0)
717 static const VMStateDescription vmstate_usb_msd
= {
718 .name
= "usb-storage",
720 .minimum_version_id
= 1,
721 .fields
= (VMStateField
[]) {
722 VMSTATE_USB_DEVICE(dev
, MSDState
),
723 VMSTATE_UINT32(mode
, MSDState
),
724 VMSTATE_UINT32(scsi_len
, MSDState
),
725 VMSTATE_UINT32(scsi_off
, MSDState
),
726 VMSTATE_UINT32(data_len
, MSDState
),
727 VMSTATE_UINT32(csw
.sig
, MSDState
),
728 VMSTATE_UINT32(csw
.tag
, MSDState
),
729 VMSTATE_UINT32(csw
.residue
, MSDState
),
730 VMSTATE_UINT8(csw
.status
, MSDState
),
731 VMSTATE_END_OF_LIST()
735 static Property msd_properties
[] = {
736 DEFINE_BLOCK_PROPERTIES(MSDState
, conf
),
737 DEFINE_PROP_STRING("serial", MSDState
, serial
),
738 DEFINE_PROP_BIT("removable", MSDState
, removable
, 0, false),
739 DEFINE_PROP_END_OF_LIST(),
742 static void usb_msd_class_initfn_common(ObjectClass
*klass
)
744 DeviceClass
*dc
= DEVICE_CLASS(klass
);
745 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
747 uc
->product_desc
= "QEMU USB MSD";
748 uc
->usb_desc
= &desc
;
749 uc
->cancel_packet
= usb_msd_cancel_io
;
750 uc
->handle_attach
= usb_desc_attach
;
751 uc
->handle_reset
= usb_msd_handle_reset
;
752 uc
->handle_control
= usb_msd_handle_control
;
753 uc
->handle_data
= usb_msd_handle_data
;
754 dc
->fw_name
= "storage";
755 dc
->vmsd
= &vmstate_usb_msd
;
758 static void usb_msd_class_initfn_storage(ObjectClass
*klass
, void *data
)
760 DeviceClass
*dc
= DEVICE_CLASS(klass
);
761 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
763 uc
->init
= usb_msd_initfn_storage
;
764 dc
->props
= msd_properties
;
765 usb_msd_class_initfn_common(klass
);
768 static void usb_msd_class_initfn_bot(ObjectClass
*klass
, void *data
)
770 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
772 uc
->init
= usb_msd_initfn_bot
;
773 usb_msd_class_initfn_common(klass
);
776 static const TypeInfo msd_info
= {
777 .name
= "usb-storage",
778 .parent
= TYPE_USB_DEVICE
,
779 .instance_size
= sizeof(MSDState
),
780 .class_init
= usb_msd_class_initfn_storage
,
783 static const TypeInfo bot_info
= {
785 .parent
= TYPE_USB_DEVICE
,
786 .instance_size
= sizeof(MSDState
),
787 .class_init
= usb_msd_class_initfn_bot
,
790 static void usb_msd_register_types(void)
792 type_register_static(&msd_info
);
793 type_register_static(&bot_info
);
794 usb_legacy_register("usb-storage", "disk", usb_msd_init
);
797 type_init(usb_msd_register_types
)