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
;
281 s
->mode
= USB_MSDM_CBW
;
284 static int usb_msd_handle_control(USBDevice
*dev
, USBPacket
*p
,
285 int request
, int value
, int index
, int length
, uint8_t *data
)
287 MSDState
*s
= (MSDState
*)dev
;
290 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
297 case DeviceRequest
| USB_REQ_GET_INTERFACE
:
301 case DeviceOutRequest
| USB_REQ_SET_INTERFACE
:
304 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
307 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
310 /* Class specific requests. */
311 case ClassInterfaceOutRequest
| MassStorageReset
:
312 /* Reset state ready for the next CBW. */
313 s
->mode
= USB_MSDM_CBW
;
316 case ClassInterfaceRequest
| GetMaxLun
:
327 static void usb_msd_cancel_io(USBDevice
*dev
, USBPacket
*p
)
329 MSDState
*s
= DO_UPCAST(MSDState
, dev
, dev
);
332 scsi_req_cancel(s
->req
);
336 static int usb_msd_handle_data(USBDevice
*dev
, USBPacket
*p
)
338 MSDState
*s
= (MSDState
*)dev
;
341 struct usb_msd_cbw cbw
;
342 uint8_t devep
= p
->devep
;
351 if (p
->iov
.size
!= 31) {
352 fprintf(stderr
, "usb-msd: Bad CBW size");
355 usb_packet_copy(p
, &cbw
, 31);
356 if (le32_to_cpu(cbw
.sig
) != 0x43425355) {
357 fprintf(stderr
, "usb-msd: Bad signature %08x\n",
358 le32_to_cpu(cbw
.sig
));
361 DPRINTF("Command on LUN %d\n", cbw
.lun
);
363 fprintf(stderr
, "usb-msd: Bad LUN %d\n", cbw
.lun
);
366 tag
= le32_to_cpu(cbw
.tag
);
367 s
->data_len
= le32_to_cpu(cbw
.data_len
);
368 if (s
->data_len
== 0) {
369 s
->mode
= USB_MSDM_CSW
;
370 } else if (cbw
.flags
& 0x80) {
371 s
->mode
= USB_MSDM_DATAIN
;
373 s
->mode
= USB_MSDM_DATAOUT
;
375 DPRINTF("Command tag 0x%x flags %08x len %d data %d\n",
376 tag
, cbw
.flags
, cbw
.cmd_len
, s
->data_len
);
379 s
->req
= scsi_req_new(s
->scsi_dev
, tag
, 0, cbw
.cmd
, NULL
);
380 scsi_req_enqueue(s
->req
);
381 if (s
->req
&& s
->req
->cmd
.xfer
!= SCSI_XFER_NONE
) {
382 scsi_req_continue(s
->req
);
387 case USB_MSDM_DATAOUT
:
388 DPRINTF("Data out %zd/%d\n", p
->iov
.size
, s
->data_len
);
389 if (p
->iov
.size
> s
->data_len
) {
394 usb_msd_copy_data(s
, p
);
397 int len
= p
->iov
.size
- p
->result
;
399 usb_packet_skip(p
, len
);
401 if (s
->data_len
== 0) {
402 s
->mode
= USB_MSDM_CSW
;
406 if (p
->result
< p
->iov
.size
) {
407 DPRINTF("Deferring packet %p\n", p
);
416 DPRINTF("Unexpected write (len %zd)\n", p
->iov
.size
);
426 case USB_MSDM_DATAOUT
:
427 if (s
->data_len
!= 0 || p
->iov
.size
< 13) {
430 /* Waiting for SCSI write to complete. */
436 if (p
->iov
.size
< 13) {
441 /* still in flight */
445 usb_msd_send_status(s
, p
);
446 s
->mode
= USB_MSDM_CBW
;
451 case USB_MSDM_DATAIN
:
452 DPRINTF("Data in %zd/%d, scsi_len %d\n",
453 p
->iov
.size
, s
->data_len
, s
->scsi_len
);
455 usb_msd_copy_data(s
, p
);
458 int len
= p
->iov
.size
- p
->result
;
460 usb_packet_skip(p
, len
);
462 if (s
->data_len
== 0) {
463 s
->mode
= USB_MSDM_CSW
;
467 if (p
->result
< p
->iov
.size
) {
468 DPRINTF("Deferring packet %p\n", p
);
477 DPRINTF("Unexpected read (len %zd)\n", p
->iov
.size
);
483 DPRINTF("Bad token\n");
492 static void usb_msd_password_cb(void *opaque
, int err
)
494 MSDState
*s
= opaque
;
497 err
= usb_device_attach(&s
->dev
);
500 qdev_unplug(&s
->dev
.qdev
);
503 static const struct SCSIBusInfo usb_msd_scsi_info
= {
508 .transfer_data
= usb_msd_transfer_data
,
509 .complete
= usb_msd_command_complete
,
510 .cancel
= usb_msd_request_cancelled
513 static int usb_msd_initfn(USBDevice
*dev
)
515 MSDState
*s
= DO_UPCAST(MSDState
, dev
, dev
);
516 BlockDriverState
*bs
= s
->conf
.bs
;
520 error_report("usb-msd: drive property not set");
525 * Hack alert: this pretends to be a block device, but it's really
526 * a SCSI bus that can serve only a single device, which it
527 * creates automatically. But first it needs to detach from its
528 * blockdev, or else scsi_bus_legacy_add_drive() dies when it
531 * The hack is probably a bad idea.
533 bdrv_detach_dev(bs
, &s
->dev
.qdev
);
537 /* try to fall back to value set with legacy -drive serial=... */
538 dinfo
= drive_get_by_blockdev(bs
);
539 if (*dinfo
->serial
) {
540 s
->serial
= strdup(dinfo
->serial
);
544 usb_desc_set_string(dev
, STR_SERIALNUMBER
, s
->serial
);
548 scsi_bus_new(&s
->bus
, &s
->dev
.qdev
, &usb_msd_scsi_info
);
549 s
->scsi_dev
= scsi_bus_legacy_add_drive(&s
->bus
, bs
, 0, !!s
->removable
,
554 s
->bus
.qbus
.allow_hotplug
= 0;
555 usb_msd_handle_reset(dev
);
557 if (bdrv_key_required(bs
)) {
559 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
, s
);
560 s
->dev
.auto_attach
= 0;
569 static USBDevice
*usb_msd_init(const char *filename
)
579 /* parse -usbdevice disk: syntax into drive opts */
580 snprintf(id
, sizeof(id
), "usb%d", nr
++);
581 opts
= qemu_opts_create(qemu_find_opts("drive"), id
, 0);
583 p1
= strchr(filename
, ':');
587 if (strstart(filename
, "format=", &p2
)) {
588 int len
= MIN(p1
- p2
, sizeof(fmt
));
589 pstrcpy(fmt
, len
, p2
);
590 qemu_opt_set(opts
, "format", fmt
);
591 } else if (*filename
!= ':') {
592 printf("unrecognized USB mass-storage option %s\n", filename
);
598 printf("block device specification needed\n");
601 qemu_opt_set(opts
, "file", filename
);
602 qemu_opt_set(opts
, "if", "none");
604 /* create host drive */
605 dinfo
= drive_init(opts
, 0);
611 /* create guest device */
612 dev
= usb_create(NULL
/* FIXME */, "usb-storage");
616 if (qdev_prop_set_drive(&dev
->qdev
, "drive", dinfo
->bdrv
) < 0) {
617 qdev_free(&dev
->qdev
);
620 if (qdev_init(&dev
->qdev
) < 0)
626 static const VMStateDescription vmstate_usb_msd
= {
627 .name
= "usb-storage",
628 .unmigratable
= 1, /* FIXME: handle transactions which are in flight */
630 .minimum_version_id
= 1,
631 .fields
= (VMStateField
[]) {
632 VMSTATE_USB_DEVICE(dev
, MSDState
),
633 VMSTATE_END_OF_LIST()
637 static struct USBDeviceInfo msd_info
= {
638 .product_desc
= "QEMU USB MSD",
639 .qdev
.name
= "usb-storage",
640 .qdev
.fw_name
= "storage",
641 .qdev
.size
= sizeof(MSDState
),
642 .qdev
.vmsd
= &vmstate_usb_msd
,
644 .init
= usb_msd_initfn
,
645 .handle_packet
= usb_generic_handle_packet
,
646 .cancel_packet
= usb_msd_cancel_io
,
647 .handle_attach
= usb_desc_attach
,
648 .handle_reset
= usb_msd_handle_reset
,
649 .handle_control
= usb_msd_handle_control
,
650 .handle_data
= usb_msd_handle_data
,
651 .usbdevice_name
= "disk",
652 .usbdevice_init
= usb_msd_init
,
653 .qdev
.props
= (Property
[]) {
654 DEFINE_BLOCK_PROPERTIES(MSDState
, conf
),
655 DEFINE_PROP_STRING("serial", MSDState
, serial
),
656 DEFINE_PROP_BIT("removable", MSDState
, removable
, 0, false),
657 DEFINE_PROP_END_OF_LIST(),
661 static void usb_msd_register_devices(void)
663 usb_qdev_register(&msd_info
);
665 device_init(usb_msd_register_devices
)