2 * USB Mass Storage Device emulation
4 * Copyright (c) 2006 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced 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. */
58 /* For async completion. */
87 static const USBDescStrings desc_strings
= {
88 [STR_MANUFACTURER
] = "QEMU " QEMU_VERSION
,
89 [STR_PRODUCT
] = "QEMU USB HARDDRIVE",
90 [STR_SERIALNUMBER
] = "1",
91 [STR_CONFIG_FULL
] = "Full speed config (usb 1.1)",
92 [STR_CONFIG_HIGH
] = "High speed config (usb 2.0)",
95 static const USBDescIface desc_iface_full
= {
96 .bInterfaceNumber
= 0,
98 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
99 .bInterfaceSubClass
= 0x06, /* SCSI */
100 .bInterfaceProtocol
= 0x50, /* Bulk */
101 .eps
= (USBDescEndpoint
[]) {
103 .bEndpointAddress
= USB_DIR_IN
| 0x01,
104 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
105 .wMaxPacketSize
= 64,
107 .bEndpointAddress
= USB_DIR_OUT
| 0x02,
108 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
109 .wMaxPacketSize
= 64,
114 static const USBDescDevice desc_device_full
= {
116 .bMaxPacketSize0
= 8,
117 .bNumConfigurations
= 1,
118 .confs
= (USBDescConfig
[]) {
121 .bConfigurationValue
= 1,
122 .iConfiguration
= STR_CONFIG_FULL
,
123 .bmAttributes
= 0xc0,
125 .ifs
= &desc_iface_full
,
130 static const USBDescIface desc_iface_high
= {
131 .bInterfaceNumber
= 0,
133 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
134 .bInterfaceSubClass
= 0x06, /* SCSI */
135 .bInterfaceProtocol
= 0x50, /* Bulk */
136 .eps
= (USBDescEndpoint
[]) {
138 .bEndpointAddress
= USB_DIR_IN
| 0x01,
139 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
140 .wMaxPacketSize
= 512,
142 .bEndpointAddress
= USB_DIR_OUT
| 0x02,
143 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
144 .wMaxPacketSize
= 512,
149 static const USBDescDevice desc_device_high
= {
151 .bMaxPacketSize0
= 64,
152 .bNumConfigurations
= 1,
153 .confs
= (USBDescConfig
[]) {
156 .bConfigurationValue
= 1,
157 .iConfiguration
= STR_CONFIG_HIGH
,
158 .bmAttributes
= 0xc0,
160 .ifs
= &desc_iface_high
,
165 static const USBDesc desc
= {
170 .iManufacturer
= STR_MANUFACTURER
,
171 .iProduct
= STR_PRODUCT
,
172 .iSerialNumber
= STR_SERIALNUMBER
,
174 .full
= &desc_device_full
,
175 .high
= &desc_device_high
,
179 static void usb_msd_copy_data(MSDState
*s
)
183 if (len
> s
->scsi_len
)
185 if (s
->mode
== USB_MSDM_DATAIN
) {
186 memcpy(s
->usb_buf
, s
->scsi_buf
, len
);
188 memcpy(s
->scsi_buf
, s
->usb_buf
, len
);
195 if (s
->scsi_len
== 0 || s
->data_len
== 0) {
196 scsi_req_continue(s
->req
);
200 static void usb_msd_send_status(MSDState
*s
, USBPacket
*p
)
202 struct usb_msd_csw csw
;
205 csw
.sig
= cpu_to_le32(0x53425355);
206 csw
.tag
= cpu_to_le32(s
->tag
);
207 csw
.residue
= s
->residue
;
208 csw
.status
= s
->result
;
210 len
= MIN(sizeof(csw
), p
->len
);
211 memcpy(p
->data
, &csw
, len
);
214 static void usb_msd_transfer_data(SCSIRequest
*req
, uint32_t len
)
216 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
217 USBPacket
*p
= s
->packet
;
219 if (req
->tag
!= s
->tag
) {
220 fprintf(stderr
, "usb-msd: Unexpected SCSI Tag 0x%x\n", req
->tag
);
223 assert((s
->mode
== USB_MSDM_DATAOUT
) == (req
->cmd
.mode
== SCSI_XFER_TO_DEV
));
225 s
->scsi_buf
= scsi_req_get_buf(req
);
227 usb_msd_copy_data(s
);
228 if (s
->packet
&& s
->usb_len
== 0) {
229 /* Set s->packet to NULL before calling usb_packet_complete
230 because another request may be issued before
231 usb_packet_complete returns. */
232 DPRINTF("Packet complete %p\n", p
);
234 usb_packet_complete(&s
->dev
, p
);
239 static void usb_msd_command_complete(SCSIRequest
*req
, uint32_t status
)
241 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
242 USBPacket
*p
= s
->packet
;
244 if (req
->tag
!= s
->tag
) {
245 fprintf(stderr
, "usb-msd: Unexpected SCSI Tag 0x%x\n", req
->tag
);
247 DPRINTF("Command complete %d\n", status
);
248 s
->residue
= s
->data_len
;
249 s
->result
= status
!= 0;
251 if (s
->data_len
== 0 && s
->mode
== USB_MSDM_DATAOUT
) {
252 /* A deferred packet with no write data remaining must be
253 the status read packet. */
254 usb_msd_send_status(s
, p
);
255 s
->mode
= USB_MSDM_CBW
;
258 s
->data_len
-= s
->usb_len
;
259 if (s
->mode
== USB_MSDM_DATAIN
) {
260 memset(s
->usb_buf
, 0, s
->usb_len
);
264 if (s
->data_len
== 0) {
265 s
->mode
= USB_MSDM_CSW
;
269 usb_packet_complete(&s
->dev
, p
);
270 } else if (s
->data_len
== 0) {
271 s
->mode
= USB_MSDM_CSW
;
277 static void usb_msd_request_cancelled(SCSIRequest
*req
)
279 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
282 scsi_req_unref(s
->req
);
289 static void usb_msd_handle_reset(USBDevice
*dev
)
291 MSDState
*s
= (MSDState
*)dev
;
294 s
->mode
= USB_MSDM_CBW
;
297 static int usb_msd_handle_control(USBDevice
*dev
, USBPacket
*p
,
298 int request
, int value
, int index
, int length
, uint8_t *data
)
300 MSDState
*s
= (MSDState
*)dev
;
303 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
310 case DeviceRequest
| USB_REQ_GET_INTERFACE
:
314 case DeviceOutRequest
| USB_REQ_SET_INTERFACE
:
317 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
320 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
323 /* Class specific requests. */
324 case ClassInterfaceOutRequest
| MassStorageReset
:
325 /* Reset state ready for the next CBW. */
326 s
->mode
= USB_MSDM_CBW
;
329 case ClassInterfaceRequest
| GetMaxLun
:
340 static void usb_msd_cancel_io(USBDevice
*dev
, USBPacket
*p
)
342 MSDState
*s
= DO_UPCAST(MSDState
, dev
, dev
);
343 scsi_req_cancel(s
->req
);
346 static int usb_msd_handle_data(USBDevice
*dev
, USBPacket
*p
)
348 MSDState
*s
= (MSDState
*)dev
;
350 struct usb_msd_cbw cbw
;
351 uint8_t devep
= p
->devep
;
352 uint8_t *data
= p
->data
;
363 fprintf(stderr
, "usb-msd: Bad CBW size");
366 memcpy(&cbw
, data
, 31);
367 if (le32_to_cpu(cbw
.sig
) != 0x43425355) {
368 fprintf(stderr
, "usb-msd: Bad signature %08x\n",
369 le32_to_cpu(cbw
.sig
));
372 DPRINTF("Command on LUN %d\n", cbw
.lun
);
374 fprintf(stderr
, "usb-msd: Bad LUN %d\n", cbw
.lun
);
377 s
->tag
= le32_to_cpu(cbw
.tag
);
378 s
->data_len
= le32_to_cpu(cbw
.data_len
);
379 if (s
->data_len
== 0) {
380 s
->mode
= USB_MSDM_CSW
;
381 } else if (cbw
.flags
& 0x80) {
382 s
->mode
= USB_MSDM_DATAIN
;
384 s
->mode
= USB_MSDM_DATAOUT
;
386 DPRINTF("Command tag 0x%x flags %08x len %d data %d\n",
387 s
->tag
, cbw
.flags
, cbw
.cmd_len
, s
->data_len
);
390 s
->req
= scsi_req_new(s
->scsi_dev
, s
->tag
, 0);
391 scsi_req_enqueue(s
->req
, cbw
.cmd
);
392 /* ??? Should check that USB and SCSI data transfer
394 if (s
->mode
!= USB_MSDM_CSW
&& s
->residue
== 0) {
395 scsi_req_continue(s
->req
);
400 case USB_MSDM_DATAOUT
:
401 DPRINTF("Data out %d/%d\n", len
, s
->data_len
);
402 if (len
> s
->data_len
)
408 usb_msd_copy_data(s
);
410 if (s
->residue
&& s
->usb_len
) {
411 s
->data_len
-= s
->usb_len
;
412 if (s
->data_len
== 0)
413 s
->mode
= USB_MSDM_CSW
;
417 DPRINTF("Deferring packet %p\n", p
);
426 DPRINTF("Unexpected write (len %d)\n", len
);
436 case USB_MSDM_DATAOUT
:
437 if (s
->data_len
!= 0 || len
< 13)
439 /* Waiting for SCSI write to complete. */
445 DPRINTF("Command status %d tag 0x%x, len %d\n",
446 s
->result
, s
->tag
, len
);
450 usb_msd_send_status(s
, p
);
451 s
->mode
= USB_MSDM_CBW
;
455 case USB_MSDM_DATAIN
:
456 DPRINTF("Data in %d/%d, scsi_len %d\n", len
, s
->data_len
, s
->scsi_len
);
457 if (len
> s
->data_len
)
462 usb_msd_copy_data(s
);
464 if (s
->residue
&& s
->usb_len
) {
465 s
->data_len
-= s
->usb_len
;
466 memset(s
->usb_buf
, 0, s
->usb_len
);
467 if (s
->data_len
== 0)
468 s
->mode
= USB_MSDM_CSW
;
472 DPRINTF("Deferring packet %p\n", p
);
481 DPRINTF("Unexpected read (len %d)\n", len
);
487 DPRINTF("Bad token\n");
496 static void usb_msd_password_cb(void *opaque
, int err
)
498 MSDState
*s
= opaque
;
501 err
= usb_device_attach(&s
->dev
);
504 qdev_unplug(&s
->dev
.qdev
);
507 static const struct SCSIBusOps usb_msd_scsi_ops
= {
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(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
, 0, 1, &usb_msd_scsi_ops
);
549 s
->scsi_dev
= scsi_bus_legacy_add_drive(&s
->bus
, bs
, 0, !!s
->removable
);
553 s
->bus
.qbus
.allow_hotplug
= 0;
554 usb_msd_handle_reset(dev
);
556 if (bdrv_key_required(bs
)) {
558 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
, s
);
559 s
->dev
.auto_attach
= 0;
565 add_boot_device_path(s
->conf
.bootindex
, &dev
->qdev
, "/disk@0,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 struct USBDeviceInfo msd_info
= {
627 .product_desc
= "QEMU USB MSD",
628 .qdev
.name
= "usb-storage",
629 .qdev
.fw_name
= "storage",
630 .qdev
.size
= sizeof(MSDState
),
632 .init
= usb_msd_initfn
,
633 .handle_packet
= usb_generic_handle_packet
,
634 .cancel_packet
= usb_msd_cancel_io
,
635 .handle_attach
= usb_desc_attach
,
636 .handle_reset
= usb_msd_handle_reset
,
637 .handle_control
= usb_msd_handle_control
,
638 .handle_data
= usb_msd_handle_data
,
639 .usbdevice_name
= "disk",
640 .usbdevice_init
= usb_msd_init
,
641 .qdev
.props
= (Property
[]) {
642 DEFINE_BLOCK_PROPERTIES(MSDState
, conf
),
643 DEFINE_PROP_STRING("serial", MSDState
, serial
),
644 DEFINE_PROP_BIT("removable", MSDState
, removable
, 0, false),
645 DEFINE_PROP_END_OF_LIST(),
649 static void usb_msd_register_devices(void)
651 usb_qdev_register(&msd_info
);
653 device_init(usb_msd_register_devices
)