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"
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. */
55 struct usb_msd_csw csw
;
62 /* For async completion. */
84 static const USBDescStrings desc_strings
= {
85 [STR_MANUFACTURER
] = "QEMU " QEMU_VERSION
,
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)",
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
= 0xc0,
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
= 0xc0,
157 .ifs
= &desc_iface_high
,
162 static const USBDesc desc
= {
164 .idVendor
= 0x46f4, /* CRC16() of "QEMU" */
167 .iManufacturer
= STR_MANUFACTURER
,
168 .iProduct
= STR_PRODUCT
,
169 .iSerialNumber
= STR_SERIALNUMBER
,
171 .full
= &desc_device_full
,
172 .high
= &desc_device_high
,
176 static void usb_msd_copy_data(MSDState
*s
, USBPacket
*p
)
179 len
= p
->iov
.size
- p
->result
;
180 if (len
> s
->scsi_len
)
182 usb_packet_copy(p
, s
->scsi_buf
, len
);
186 if (s
->scsi_len
== 0 || s
->data_len
== 0) {
187 scsi_req_continue(s
->req
);
191 static void usb_msd_send_status(MSDState
*s
, USBPacket
*p
)
195 DPRINTF("Command status %d tag 0x%x, len %zd\n",
196 s
->csw
.status
, s
->csw
.tag
, p
->iov
.size
);
198 assert(s
->csw
.sig
== 0x53425355);
199 len
= MIN(sizeof(s
->csw
), p
->iov
.size
);
200 usb_packet_copy(p
, &s
->csw
, len
);
201 memset(&s
->csw
, 0, sizeof(s
->csw
));
204 static void usb_msd_transfer_data(SCSIRequest
*req
, uint32_t len
)
206 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
207 USBPacket
*p
= s
->packet
;
209 assert((s
->mode
== USB_MSDM_DATAOUT
) == (req
->cmd
.mode
== SCSI_XFER_TO_DEV
));
211 s
->scsi_buf
= scsi_req_get_buf(req
);
213 usb_msd_copy_data(s
, p
);
215 if (p
&& p
->result
== p
->iov
.size
) {
216 /* Set s->packet to NULL before calling usb_packet_complete
217 because another request may be issued before
218 usb_packet_complete returns. */
219 DPRINTF("Packet complete %p\n", p
);
221 usb_packet_complete(&s
->dev
, p
);
226 static void usb_msd_command_complete(SCSIRequest
*req
, uint32_t status
)
228 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
229 USBPacket
*p
= s
->packet
;
231 DPRINTF("Command complete %d tag 0x%x\n", status
, req
->tag
);
232 s
->residue
= s
->data_len
;
234 s
->csw
.sig
= cpu_to_le32(0x53425355);
235 s
->csw
.tag
= cpu_to_le32(req
->tag
);
236 s
->csw
.residue
= s
->residue
;
237 s
->csw
.status
= status
!= 0;
240 if (s
->data_len
== 0 && s
->mode
== USB_MSDM_DATAOUT
) {
241 /* A deferred packet with no write data remaining must be
242 the status read packet. */
243 usb_msd_send_status(s
, p
);
244 s
->mode
= USB_MSDM_CBW
;
247 int len
= (p
->iov
.size
- p
->result
);
248 usb_packet_skip(p
, len
);
251 if (s
->data_len
== 0) {
252 s
->mode
= USB_MSDM_CSW
;
256 usb_packet_complete(&s
->dev
, p
);
257 } else if (s
->data_len
== 0) {
258 s
->mode
= USB_MSDM_CSW
;
264 static void usb_msd_request_cancelled(SCSIRequest
*req
)
266 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
269 scsi_req_unref(s
->req
);
276 static void usb_msd_handle_reset(USBDevice
*dev
)
278 MSDState
*s
= (MSDState
*)dev
;
282 scsi_req_cancel(s
->req
);
284 assert(s
->req
== NULL
);
287 USBPacket
*p
= s
->packet
;
289 p
->result
= USB_RET_STALL
;
290 usb_packet_complete(dev
, p
);
293 s
->mode
= USB_MSDM_CBW
;
296 static int usb_msd_handle_control(USBDevice
*dev
, USBPacket
*p
,
297 int request
, int value
, int index
, int length
, uint8_t *data
)
299 MSDState
*s
= (MSDState
*)dev
;
302 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
309 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
312 /* Class specific requests. */
313 case ClassInterfaceOutRequest
| MassStorageReset
:
314 /* Reset state ready for the next CBW. */
315 s
->mode
= USB_MSDM_CBW
;
318 case ClassInterfaceRequest
| GetMaxLun
:
329 static void usb_msd_cancel_io(USBDevice
*dev
, USBPacket
*p
)
331 MSDState
*s
= DO_UPCAST(MSDState
, dev
, dev
);
334 scsi_req_cancel(s
->req
);
338 static int usb_msd_handle_data(USBDevice
*dev
, USBPacket
*p
)
340 MSDState
*s
= (MSDState
*)dev
;
343 struct usb_msd_cbw cbw
;
344 uint8_t devep
= p
->devep
;
353 if (p
->iov
.size
!= 31) {
354 fprintf(stderr
, "usb-msd: Bad CBW size");
357 usb_packet_copy(p
, &cbw
, 31);
358 if (le32_to_cpu(cbw
.sig
) != 0x43425355) {
359 fprintf(stderr
, "usb-msd: Bad signature %08x\n",
360 le32_to_cpu(cbw
.sig
));
363 DPRINTF("Command on LUN %d\n", cbw
.lun
);
365 fprintf(stderr
, "usb-msd: Bad LUN %d\n", cbw
.lun
);
368 tag
= le32_to_cpu(cbw
.tag
);
369 s
->data_len
= le32_to_cpu(cbw
.data_len
);
370 if (s
->data_len
== 0) {
371 s
->mode
= USB_MSDM_CSW
;
372 } else if (cbw
.flags
& 0x80) {
373 s
->mode
= USB_MSDM_DATAIN
;
375 s
->mode
= USB_MSDM_DATAOUT
;
377 DPRINTF("Command tag 0x%x flags %08x len %d data %d\n",
378 tag
, cbw
.flags
, cbw
.cmd_len
, s
->data_len
);
381 s
->req
= scsi_req_new(s
->scsi_dev
, tag
, 0, cbw
.cmd
, NULL
);
382 scsi_req_enqueue(s
->req
);
383 if (s
->req
&& s
->req
->cmd
.xfer
!= SCSI_XFER_NONE
) {
384 scsi_req_continue(s
->req
);
389 case USB_MSDM_DATAOUT
:
390 DPRINTF("Data out %zd/%d\n", p
->iov
.size
, s
->data_len
);
391 if (p
->iov
.size
> s
->data_len
) {
396 usb_msd_copy_data(s
, p
);
399 int len
= p
->iov
.size
- p
->result
;
401 usb_packet_skip(p
, len
);
403 if (s
->data_len
== 0) {
404 s
->mode
= USB_MSDM_CSW
;
408 if (p
->result
< p
->iov
.size
) {
409 DPRINTF("Deferring packet %p\n", p
);
418 DPRINTF("Unexpected write (len %zd)\n", p
->iov
.size
);
428 case USB_MSDM_DATAOUT
:
429 if (s
->data_len
!= 0 || p
->iov
.size
< 13) {
432 /* Waiting for SCSI write to complete. */
438 if (p
->iov
.size
< 13) {
443 /* still in flight */
447 usb_msd_send_status(s
, p
);
448 s
->mode
= USB_MSDM_CBW
;
453 case USB_MSDM_DATAIN
:
454 DPRINTF("Data in %zd/%d, scsi_len %d\n",
455 p
->iov
.size
, s
->data_len
, s
->scsi_len
);
457 usb_msd_copy_data(s
, p
);
460 int len
= p
->iov
.size
- p
->result
;
462 usb_packet_skip(p
, len
);
464 if (s
->data_len
== 0) {
465 s
->mode
= USB_MSDM_CSW
;
469 if (p
->result
< p
->iov
.size
) {
470 DPRINTF("Deferring packet %p\n", p
);
479 DPRINTF("Unexpected read (len %zd)\n", p
->iov
.size
);
485 DPRINTF("Bad token\n");
494 static void usb_msd_password_cb(void *opaque
, int err
)
496 MSDState
*s
= opaque
;
499 err
= usb_device_attach(&s
->dev
);
502 qdev_unplug(&s
->dev
.qdev
);
505 static const struct SCSIBusInfo usb_msd_scsi_info
= {
510 .transfer_data
= usb_msd_transfer_data
,
511 .complete
= usb_msd_command_complete
,
512 .cancel
= usb_msd_request_cancelled
515 static int usb_msd_initfn(USBDevice
*dev
)
517 MSDState
*s
= DO_UPCAST(MSDState
, dev
, dev
);
518 BlockDriverState
*bs
= s
->conf
.bs
;
522 error_report("drive property not set");
527 * Hack alert: this pretends to be a block device, but it's really
528 * a SCSI bus that can serve only a single device, which it
529 * creates automatically. But first it needs to detach from its
530 * blockdev, or else scsi_bus_legacy_add_drive() dies when it
533 * The hack is probably a bad idea.
535 bdrv_detach_dev(bs
, &s
->dev
.qdev
);
539 /* try to fall back to value set with legacy -drive serial=... */
540 dinfo
= drive_get_by_blockdev(bs
);
541 if (*dinfo
->serial
) {
542 s
->serial
= strdup(dinfo
->serial
);
546 usb_desc_set_string(dev
, STR_SERIALNUMBER
, s
->serial
);
550 scsi_bus_new(&s
->bus
, &s
->dev
.qdev
, &usb_msd_scsi_info
);
551 s
->scsi_dev
= scsi_bus_legacy_add_drive(&s
->bus
, bs
, 0, !!s
->removable
,
556 s
->bus
.qbus
.allow_hotplug
= 0;
557 usb_msd_handle_reset(dev
);
559 if (bdrv_key_required(bs
)) {
561 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
, s
);
562 s
->dev
.auto_attach
= 0;
571 static USBDevice
*usb_msd_init(const char *filename
)
581 /* parse -usbdevice disk: syntax into drive opts */
582 snprintf(id
, sizeof(id
), "usb%d", nr
++);
583 opts
= qemu_opts_create(qemu_find_opts("drive"), id
, 0);
585 p1
= strchr(filename
, ':');
589 if (strstart(filename
, "format=", &p2
)) {
590 int len
= MIN(p1
- p2
, sizeof(fmt
));
591 pstrcpy(fmt
, len
, p2
);
592 qemu_opt_set(opts
, "format", fmt
);
593 } else if (*filename
!= ':') {
594 printf("unrecognized USB mass-storage option %s\n", filename
);
600 printf("block device specification needed\n");
603 qemu_opt_set(opts
, "file", filename
);
604 qemu_opt_set(opts
, "if", "none");
606 /* create host drive */
607 dinfo
= drive_init(opts
, 0);
613 /* create guest device */
614 dev
= usb_create(NULL
/* FIXME */, "usb-storage");
618 if (qdev_prop_set_drive(&dev
->qdev
, "drive", dinfo
->bdrv
) < 0) {
619 qdev_free(&dev
->qdev
);
622 if (qdev_init(&dev
->qdev
) < 0)
628 static const VMStateDescription vmstate_usb_msd
= {
629 .name
= "usb-storage",
630 .unmigratable
= 1, /* FIXME: handle transactions which are in flight */
632 .minimum_version_id
= 1,
633 .fields
= (VMStateField
[]) {
634 VMSTATE_USB_DEVICE(dev
, MSDState
),
635 VMSTATE_END_OF_LIST()
639 static struct USBDeviceInfo msd_info
= {
640 .product_desc
= "QEMU USB MSD",
641 .qdev
.name
= "usb-storage",
642 .qdev
.fw_name
= "storage",
643 .qdev
.size
= sizeof(MSDState
),
644 .qdev
.vmsd
= &vmstate_usb_msd
,
646 .init
= usb_msd_initfn
,
647 .handle_packet
= usb_generic_handle_packet
,
648 .cancel_packet
= usb_msd_cancel_io
,
649 .handle_attach
= usb_desc_attach
,
650 .handle_reset
= usb_msd_handle_reset
,
651 .handle_control
= usb_msd_handle_control
,
652 .handle_data
= usb_msd_handle_data
,
653 .usbdevice_name
= "disk",
654 .usbdevice_init
= usb_msd_init
,
655 .qdev
.props
= (Property
[]) {
656 DEFINE_BLOCK_PROPERTIES(MSDState
, conf
),
657 DEFINE_PROP_STRING("serial", MSDState
, serial
),
658 DEFINE_PROP_BIT("removable", MSDState
, removable
, 0, false),
659 DEFINE_PROP_END_OF_LIST(),
663 static void usb_msd_register_devices(void)
665 usb_qdev_register(&msd_info
);
667 device_init(usb_msd_register_devices
)