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.h"
14 #include "hw/usb/desc.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
;
61 /* For async completion. */
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)",
91 static const USBDescIface desc_iface_full
= {
92 .bInterfaceNumber
= 0,
94 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
95 .bInterfaceSubClass
= 0x06, /* SCSI */
96 .bInterfaceProtocol
= 0x50, /* Bulk */
97 .eps
= (USBDescEndpoint
[]) {
99 .bEndpointAddress
= USB_DIR_IN
| 0x01,
100 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
101 .wMaxPacketSize
= 64,
103 .bEndpointAddress
= USB_DIR_OUT
| 0x02,
104 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
105 .wMaxPacketSize
= 64,
110 static const USBDescDevice desc_device_full
= {
112 .bMaxPacketSize0
= 8,
113 .bNumConfigurations
= 1,
114 .confs
= (USBDescConfig
[]) {
117 .bConfigurationValue
= 1,
118 .iConfiguration
= STR_CONFIG_FULL
,
119 .bmAttributes
= 0xc0,
121 .ifs
= &desc_iface_full
,
126 static const USBDescIface desc_iface_high
= {
127 .bInterfaceNumber
= 0,
129 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
130 .bInterfaceSubClass
= 0x06, /* SCSI */
131 .bInterfaceProtocol
= 0x50, /* Bulk */
132 .eps
= (USBDescEndpoint
[]) {
134 .bEndpointAddress
= USB_DIR_IN
| 0x01,
135 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
136 .wMaxPacketSize
= 512,
138 .bEndpointAddress
= USB_DIR_OUT
| 0x02,
139 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
140 .wMaxPacketSize
= 512,
145 static const USBDescDevice desc_device_high
= {
147 .bMaxPacketSize0
= 64,
148 .bNumConfigurations
= 1,
149 .confs
= (USBDescConfig
[]) {
152 .bConfigurationValue
= 1,
153 .iConfiguration
= STR_CONFIG_HIGH
,
154 .bmAttributes
= 0xc0,
156 .ifs
= &desc_iface_high
,
161 static const USBDesc desc
= {
163 .idVendor
= 0x46f4, /* CRC16() of "QEMU" */
166 .iManufacturer
= STR_MANUFACTURER
,
167 .iProduct
= STR_PRODUCT
,
168 .iSerialNumber
= STR_SERIALNUMBER
,
170 .full
= &desc_device_full
,
171 .high
= &desc_device_high
,
175 static void usb_msd_copy_data(MSDState
*s
, USBPacket
*p
)
178 len
= p
->iov
.size
- p
->result
;
179 if (len
> s
->scsi_len
)
181 usb_packet_copy(p
, scsi_req_get_buf(s
->req
) + s
->scsi_off
, len
);
185 if (s
->scsi_len
== 0 || s
->data_len
== 0) {
186 scsi_req_continue(s
->req
);
190 static void usb_msd_send_status(MSDState
*s
, USBPacket
*p
)
194 DPRINTF("Command status %d tag 0x%x, len %zd\n",
195 s
->csw
.status
, le32_to_cpu(s
->csw
.tag
), p
->iov
.size
);
197 assert(s
->csw
.sig
== cpu_to_le32(0x53425355));
198 len
= MIN(sizeof(s
->csw
), p
->iov
.size
);
199 usb_packet_copy(p
, &s
->csw
, len
);
200 memset(&s
->csw
, 0, sizeof(s
->csw
));
203 static void usb_msd_packet_complete(MSDState
*s
)
205 USBPacket
*p
= s
->packet
;
207 /* Set s->packet to NULL before calling usb_packet_complete
208 because another request may be issued before
209 usb_packet_complete returns. */
210 DPRINTF("Packet complete %p\n", p
);
212 usb_packet_complete(&s
->dev
, p
);
215 static void usb_msd_transfer_data(SCSIRequest
*req
, uint32_t len
)
217 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
218 USBPacket
*p
= s
->packet
;
220 assert((s
->mode
== USB_MSDM_DATAOUT
) == (req
->cmd
.mode
== SCSI_XFER_TO_DEV
));
224 usb_msd_copy_data(s
, p
);
226 if (p
&& p
->result
== p
->iov
.size
) {
227 usb_msd_packet_complete(s
);
232 static void usb_msd_command_complete(SCSIRequest
*req
, uint32_t status
, size_t resid
)
234 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
235 USBPacket
*p
= s
->packet
;
237 DPRINTF("Command complete %d tag 0x%x\n", status
, req
->tag
);
239 s
->csw
.sig
= cpu_to_le32(0x53425355);
240 s
->csw
.tag
= cpu_to_le32(req
->tag
);
241 s
->csw
.residue
= cpu_to_le32(s
->data_len
);
242 s
->csw
.status
= status
!= 0;
245 if (s
->data_len
== 0 && s
->mode
== USB_MSDM_DATAOUT
) {
246 /* A deferred packet with no write data remaining must be
247 the status read packet. */
248 usb_msd_send_status(s
, p
);
249 s
->mode
= USB_MSDM_CBW
;
250 } else if (s
->mode
== USB_MSDM_CSW
) {
251 usb_msd_send_status(s
, p
);
252 s
->mode
= USB_MSDM_CBW
;
255 int len
= (p
->iov
.size
- p
->result
);
256 usb_packet_skip(p
, len
);
259 if (s
->data_len
== 0) {
260 s
->mode
= USB_MSDM_CSW
;
263 usb_msd_packet_complete(s
);
264 } else if (s
->data_len
== 0) {
265 s
->mode
= USB_MSDM_CSW
;
271 static void usb_msd_request_cancelled(SCSIRequest
*req
)
273 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
276 scsi_req_unref(s
->req
);
282 static void usb_msd_handle_reset(USBDevice
*dev
)
284 MSDState
*s
= (MSDState
*)dev
;
288 scsi_req_cancel(s
->req
);
290 assert(s
->req
== NULL
);
293 s
->packet
->result
= USB_RET_STALL
;
294 usb_msd_packet_complete(s
);
297 s
->mode
= USB_MSDM_CBW
;
300 static int usb_msd_handle_control(USBDevice
*dev
, USBPacket
*p
,
301 int request
, int value
, int index
, int length
, uint8_t *data
)
303 MSDState
*s
= (MSDState
*)dev
;
306 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
313 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
316 /* Class specific requests. */
317 case ClassInterfaceOutRequest
| MassStorageReset
:
318 /* Reset state ready for the next CBW. */
319 s
->mode
= USB_MSDM_CBW
;
322 case ClassInterfaceRequest
| GetMaxLun
:
333 static void usb_msd_cancel_io(USBDevice
*dev
, USBPacket
*p
)
335 MSDState
*s
= DO_UPCAST(MSDState
, dev
, dev
);
337 assert(s
->packet
== p
);
341 scsi_req_cancel(s
->req
);
345 static int usb_msd_handle_data(USBDevice
*dev
, USBPacket
*p
)
347 MSDState
*s
= (MSDState
*)dev
;
350 struct usb_msd_cbw cbw
;
351 uint8_t devep
= p
->ep
->nr
;
360 if (p
->iov
.size
!= 31) {
361 fprintf(stderr
, "usb-msd: Bad CBW size");
364 usb_packet_copy(p
, &cbw
, 31);
365 if (le32_to_cpu(cbw
.sig
) != 0x43425355) {
366 fprintf(stderr
, "usb-msd: Bad signature %08x\n",
367 le32_to_cpu(cbw
.sig
));
370 DPRINTF("Command on LUN %d\n", cbw
.lun
);
372 fprintf(stderr
, "usb-msd: Bad LUN %d\n", cbw
.lun
);
375 tag
= le32_to_cpu(cbw
.tag
);
376 s
->data_len
= le32_to_cpu(cbw
.data_len
);
377 if (s
->data_len
== 0) {
378 s
->mode
= USB_MSDM_CSW
;
379 } else if (cbw
.flags
& 0x80) {
380 s
->mode
= USB_MSDM_DATAIN
;
382 s
->mode
= USB_MSDM_DATAOUT
;
384 DPRINTF("Command tag 0x%x flags %08x len %d data %d\n",
385 tag
, cbw
.flags
, cbw
.cmd_len
, s
->data_len
);
386 assert(le32_to_cpu(s
->csw
.residue
) == 0);
388 s
->req
= scsi_req_new(s
->scsi_dev
, tag
, 0, cbw
.cmd
, NULL
);
390 scsi_req_print(s
->req
);
392 scsi_req_enqueue(s
->req
);
393 if (s
->req
&& s
->req
->cmd
.xfer
!= SCSI_XFER_NONE
) {
394 scsi_req_continue(s
->req
);
399 case USB_MSDM_DATAOUT
:
400 DPRINTF("Data out %zd/%d\n", p
->iov
.size
, s
->data_len
);
401 if (p
->iov
.size
> s
->data_len
) {
406 usb_msd_copy_data(s
, p
);
408 if (le32_to_cpu(s
->csw
.residue
)) {
409 int len
= p
->iov
.size
- p
->result
;
411 usb_packet_skip(p
, len
);
413 if (s
->data_len
== 0) {
414 s
->mode
= USB_MSDM_CSW
;
418 if (p
->result
< p
->iov
.size
) {
419 DPRINTF("Deferring packet %p [wait data-out]\n", p
);
428 DPRINTF("Unexpected write (len %zd)\n", p
->iov
.size
);
438 case USB_MSDM_DATAOUT
:
439 if (s
->data_len
!= 0 || p
->iov
.size
< 13) {
442 /* Waiting for SCSI write to complete. */
448 if (p
->iov
.size
< 13) {
453 /* still in flight */
454 DPRINTF("Deferring packet %p [wait status]\n", p
);
458 usb_msd_send_status(s
, p
);
459 s
->mode
= USB_MSDM_CBW
;
464 case USB_MSDM_DATAIN
:
465 DPRINTF("Data in %zd/%d, scsi_len %d\n",
466 p
->iov
.size
, s
->data_len
, s
->scsi_len
);
468 usb_msd_copy_data(s
, p
);
470 if (le32_to_cpu(s
->csw
.residue
)) {
471 int len
= p
->iov
.size
- p
->result
;
473 usb_packet_skip(p
, len
);
475 if (s
->data_len
== 0) {
476 s
->mode
= USB_MSDM_CSW
;
480 if (p
->result
< p
->iov
.size
) {
481 DPRINTF("Deferring packet %p [wait data-in]\n", p
);
490 DPRINTF("Unexpected read (len %zd)\n", p
->iov
.size
);
496 DPRINTF("Bad token\n");
505 static void usb_msd_password_cb(void *opaque
, int err
)
507 MSDState
*s
= opaque
;
510 err
= usb_device_attach(&s
->dev
);
513 qdev_unplug(&s
->dev
.qdev
, NULL
);
516 static void *usb_msd_load_request(QEMUFile
*f
, SCSIRequest
*req
)
518 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
520 /* nothing to load, just store req in our state struct */
521 assert(s
->req
== NULL
);
527 static const struct SCSIBusInfo usb_msd_scsi_info
= {
532 .transfer_data
= usb_msd_transfer_data
,
533 .complete
= usb_msd_command_complete
,
534 .cancel
= usb_msd_request_cancelled
,
535 .load_request
= usb_msd_load_request
,
538 static int usb_msd_initfn(USBDevice
*dev
)
540 MSDState
*s
= DO_UPCAST(MSDState
, dev
, dev
);
541 BlockDriverState
*bs
= s
->conf
.bs
;
544 error_report("drive property not set");
548 blkconf_serial(&s
->conf
, &s
->serial
);
551 * Hack alert: this pretends to be a block device, but it's really
552 * a SCSI bus that can serve only a single device, which it
553 * creates automatically. But first it needs to detach from its
554 * blockdev, or else scsi_bus_legacy_add_drive() dies when it
557 * The hack is probably a bad idea.
559 bdrv_detach_dev(bs
, &s
->dev
.qdev
);
563 usb_desc_set_string(dev
, STR_SERIALNUMBER
, s
->serial
);
565 usb_desc_create_serial(dev
);
569 scsi_bus_new(&s
->bus
, &s
->dev
.qdev
, &usb_msd_scsi_info
);
570 s
->scsi_dev
= scsi_bus_legacy_add_drive(&s
->bus
, bs
, 0, !!s
->removable
,
575 s
->bus
.qbus
.allow_hotplug
= 0;
576 usb_msd_handle_reset(dev
);
578 if (bdrv_key_required(bs
)) {
580 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
, s
);
581 s
->dev
.auto_attach
= 0;
590 static USBDevice
*usb_msd_init(USBBus
*bus
, const char *filename
)
600 /* parse -usbdevice disk: syntax into drive opts */
601 snprintf(id
, sizeof(id
), "usb%d", nr
++);
602 opts
= qemu_opts_create(qemu_find_opts("drive"), id
, 0, NULL
);
604 p1
= strchr(filename
, ':');
608 if (strstart(filename
, "format=", &p2
)) {
609 int len
= MIN(p1
- p2
, sizeof(fmt
));
610 pstrcpy(fmt
, len
, p2
);
611 qemu_opt_set(opts
, "format", fmt
);
612 } else if (*filename
!= ':') {
613 printf("unrecognized USB mass-storage option %s\n", filename
);
619 printf("block device specification needed\n");
622 qemu_opt_set(opts
, "file", filename
);
623 qemu_opt_set(opts
, "if", "none");
625 /* create host drive */
626 dinfo
= drive_init(opts
, 0);
632 /* create guest device */
633 dev
= usb_create(bus
, "usb-storage");
637 if (qdev_prop_set_drive(&dev
->qdev
, "drive", dinfo
->bdrv
) < 0) {
638 qdev_free(&dev
->qdev
);
641 if (qdev_init(&dev
->qdev
) < 0)
647 static const VMStateDescription vmstate_usb_msd
= {
648 .name
= "usb-storage",
650 .minimum_version_id
= 1,
651 .fields
= (VMStateField
[]) {
652 VMSTATE_USB_DEVICE(dev
, MSDState
),
653 VMSTATE_UINT32(mode
, MSDState
),
654 VMSTATE_UINT32(scsi_len
, MSDState
),
655 VMSTATE_UINT32(scsi_off
, MSDState
),
656 VMSTATE_UINT32(data_len
, MSDState
),
657 VMSTATE_UINT32(csw
.sig
, MSDState
),
658 VMSTATE_UINT32(csw
.tag
, MSDState
),
659 VMSTATE_UINT32(csw
.residue
, MSDState
),
660 VMSTATE_UINT8(csw
.status
, MSDState
),
661 VMSTATE_END_OF_LIST()
665 static Property msd_properties
[] = {
666 DEFINE_BLOCK_PROPERTIES(MSDState
, conf
),
667 DEFINE_PROP_STRING("serial", MSDState
, serial
),
668 DEFINE_PROP_BIT("removable", MSDState
, removable
, 0, false),
669 DEFINE_PROP_END_OF_LIST(),
672 static void usb_msd_class_initfn(ObjectClass
*klass
, void *data
)
674 DeviceClass
*dc
= DEVICE_CLASS(klass
);
675 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
677 uc
->init
= usb_msd_initfn
;
678 uc
->product_desc
= "QEMU USB MSD";
679 uc
->usb_desc
= &desc
;
680 uc
->cancel_packet
= usb_msd_cancel_io
;
681 uc
->handle_attach
= usb_desc_attach
;
682 uc
->handle_reset
= usb_msd_handle_reset
;
683 uc
->handle_control
= usb_msd_handle_control
;
684 uc
->handle_data
= usb_msd_handle_data
;
685 dc
->fw_name
= "storage";
686 dc
->vmsd
= &vmstate_usb_msd
;
687 dc
->props
= msd_properties
;
690 static TypeInfo msd_info
= {
691 .name
= "usb-storage",
692 .parent
= TYPE_USB_DEVICE
,
693 .instance_size
= sizeof(MSDState
),
694 .class_init
= usb_msd_class_initfn
,
697 static void usb_msd_register_types(void)
699 type_register_static(&msd_info
);
700 usb_legacy_register("usb-storage", "disk", usb_msd_init
);
703 type_init(usb_msd_register_types
)