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. */
57 /* For async completion. */
86 static const USBDescStrings desc_strings
= {
87 [STR_MANUFACTURER
] = "QEMU " QEMU_VERSION
,
88 [STR_PRODUCT
] = "QEMU USB HARDDRIVE",
89 [STR_SERIALNUMBER
] = "1",
90 [STR_CONFIG_FULL
] = "Full speed config (usb 1.1)",
91 [STR_CONFIG_HIGH
] = "High speed config (usb 2.0)",
94 static const USBDescIface desc_iface_full
= {
95 .bInterfaceNumber
= 0,
97 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
98 .bInterfaceSubClass
= 0x06, /* SCSI */
99 .bInterfaceProtocol
= 0x50, /* Bulk */
100 .eps
= (USBDescEndpoint
[]) {
102 .bEndpointAddress
= USB_DIR_IN
| 0x01,
103 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
104 .wMaxPacketSize
= 64,
106 .bEndpointAddress
= USB_DIR_OUT
| 0x02,
107 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
108 .wMaxPacketSize
= 64,
113 static const USBDescDevice desc_device_full
= {
115 .bMaxPacketSize0
= 8,
116 .bNumConfigurations
= 1,
117 .confs
= (USBDescConfig
[]) {
120 .bConfigurationValue
= 1,
121 .iConfiguration
= STR_CONFIG_FULL
,
122 .bmAttributes
= 0xc0,
124 .ifs
= &desc_iface_full
,
129 static const USBDescIface desc_iface_high
= {
130 .bInterfaceNumber
= 0,
132 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
133 .bInterfaceSubClass
= 0x06, /* SCSI */
134 .bInterfaceProtocol
= 0x50, /* Bulk */
135 .eps
= (USBDescEndpoint
[]) {
137 .bEndpointAddress
= USB_DIR_IN
| 0x01,
138 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
139 .wMaxPacketSize
= 512,
141 .bEndpointAddress
= USB_DIR_OUT
| 0x02,
142 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
143 .wMaxPacketSize
= 512,
148 static const USBDescDevice desc_device_high
= {
150 .bMaxPacketSize0
= 64,
151 .bNumConfigurations
= 1,
152 .confs
= (USBDescConfig
[]) {
155 .bConfigurationValue
= 1,
156 .iConfiguration
= STR_CONFIG_HIGH
,
157 .bmAttributes
= 0xc0,
159 .ifs
= &desc_iface_high
,
164 static const USBDesc desc
= {
169 .iManufacturer
= STR_MANUFACTURER
,
170 .iProduct
= STR_PRODUCT
,
171 .iSerialNumber
= STR_SERIALNUMBER
,
173 .full
= &desc_device_full
,
174 .high
= &desc_device_high
,
178 static void usb_msd_copy_data(MSDState
*s
)
182 if (len
> s
->scsi_len
)
184 if (s
->mode
== USB_MSDM_DATAIN
) {
185 memcpy(s
->usb_buf
, s
->scsi_buf
, len
);
187 memcpy(s
->scsi_buf
, s
->usb_buf
, len
);
194 if (s
->scsi_len
== 0 || s
->data_len
== 0) {
195 scsi_req_continue(s
->req
);
199 static void usb_msd_send_status(MSDState
*s
, USBPacket
*p
)
201 struct usb_msd_csw csw
;
204 csw
.sig
= cpu_to_le32(0x53425355);
205 csw
.tag
= cpu_to_le32(s
->tag
);
206 csw
.residue
= s
->residue
;
207 csw
.status
= s
->result
;
209 len
= MIN(sizeof(csw
), p
->len
);
210 memcpy(p
->data
, &csw
, len
);
213 static void usb_msd_transfer_data(SCSIRequest
*req
, uint32_t len
)
215 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
216 USBPacket
*p
= s
->packet
;
218 if (req
->tag
!= s
->tag
) {
219 fprintf(stderr
, "usb-msd: Unexpected SCSI Tag 0x%x\n", req
->tag
);
222 assert((s
->mode
== USB_MSDM_DATAOUT
) == (req
->cmd
.mode
== SCSI_XFER_TO_DEV
));
224 s
->scsi_buf
= scsi_req_get_buf(req
);
226 usb_msd_copy_data(s
);
227 if (s
->packet
&& s
->usb_len
== 0) {
228 /* Set s->packet to NULL before calling usb_packet_complete
229 because another request may be issued before
230 usb_packet_complete returns. */
231 DPRINTF("Packet complete %p\n", p
);
233 usb_packet_complete(&s
->dev
, p
);
238 static void usb_msd_command_complete(SCSIRequest
*req
, uint32_t status
)
240 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
241 USBPacket
*p
= s
->packet
;
243 if (req
->tag
!= s
->tag
) {
244 fprintf(stderr
, "usb-msd: Unexpected SCSI Tag 0x%x\n", req
->tag
);
246 DPRINTF("Command complete %d\n", status
);
247 s
->residue
= s
->data_len
;
248 s
->result
= status
!= 0;
250 if (s
->data_len
== 0 && s
->mode
== USB_MSDM_DATAOUT
) {
251 /* A deferred packet with no write data remaining must be
252 the status read packet. */
253 usb_msd_send_status(s
, p
);
254 s
->mode
= USB_MSDM_CBW
;
257 s
->data_len
-= s
->usb_len
;
258 if (s
->mode
== USB_MSDM_DATAIN
) {
259 memset(s
->usb_buf
, 0, s
->usb_len
);
263 if (s
->data_len
== 0) {
264 s
->mode
= USB_MSDM_CSW
;
268 usb_packet_complete(&s
->dev
, p
);
269 } else if (s
->data_len
== 0) {
270 s
->mode
= USB_MSDM_CSW
;
276 static void usb_msd_request_cancelled(SCSIRequest
*req
)
278 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
281 scsi_req_unref(s
->req
);
288 static void usb_msd_handle_reset(USBDevice
*dev
)
290 MSDState
*s
= (MSDState
*)dev
;
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 DeviceRequest
| USB_REQ_GET_INTERFACE
:
313 case DeviceOutRequest
| USB_REQ_SET_INTERFACE
:
316 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
319 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
322 /* Class specific requests. */
323 case ClassInterfaceOutRequest
| MassStorageReset
:
324 /* Reset state ready for the next CBW. */
325 s
->mode
= USB_MSDM_CBW
;
328 case ClassInterfaceRequest
| GetMaxLun
:
339 static void usb_msd_cancel_io(USBDevice
*dev
, USBPacket
*p
)
341 MSDState
*s
= DO_UPCAST(MSDState
, dev
, dev
);
342 scsi_req_cancel(s
->req
);
345 static int usb_msd_handle_data(USBDevice
*dev
, USBPacket
*p
)
347 MSDState
*s
= (MSDState
*)dev
;
349 struct usb_msd_cbw cbw
;
350 uint8_t devep
= p
->devep
;
351 uint8_t *data
= p
->data
;
362 fprintf(stderr
, "usb-msd: Bad CBW size");
365 memcpy(&cbw
, data
, 31);
366 if (le32_to_cpu(cbw
.sig
) != 0x43425355) {
367 fprintf(stderr
, "usb-msd: Bad signature %08x\n",
368 le32_to_cpu(cbw
.sig
));
371 DPRINTF("Command on LUN %d\n", cbw
.lun
);
373 fprintf(stderr
, "usb-msd: Bad LUN %d\n", cbw
.lun
);
376 s
->tag
= le32_to_cpu(cbw
.tag
);
377 s
->data_len
= le32_to_cpu(cbw
.data_len
);
378 if (s
->data_len
== 0) {
379 s
->mode
= USB_MSDM_CSW
;
380 } else if (cbw
.flags
& 0x80) {
381 s
->mode
= USB_MSDM_DATAIN
;
383 s
->mode
= USB_MSDM_DATAOUT
;
385 DPRINTF("Command tag 0x%x flags %08x len %d data %d\n",
386 s
->tag
, cbw
.flags
, cbw
.cmd_len
, s
->data_len
);
389 s
->req
= scsi_req_new(s
->scsi_dev
, s
->tag
, 0);
390 scsi_req_enqueue(s
->req
, cbw
.cmd
);
391 /* ??? Should check that USB and SCSI data transfer
393 if (s
->mode
!= USB_MSDM_CSW
&& s
->residue
== 0) {
394 scsi_req_continue(s
->req
);
399 case USB_MSDM_DATAOUT
:
400 DPRINTF("Data out %d/%d\n", len
, s
->data_len
);
401 if (len
> s
->data_len
)
407 usb_msd_copy_data(s
);
409 if (s
->residue
&& s
->usb_len
) {
410 s
->data_len
-= s
->usb_len
;
411 if (s
->data_len
== 0)
412 s
->mode
= USB_MSDM_CSW
;
416 DPRINTF("Deferring packet %p\n", p
);
425 DPRINTF("Unexpected write (len %d)\n", len
);
435 case USB_MSDM_DATAOUT
:
436 if (s
->data_len
!= 0 || len
< 13)
438 /* Waiting for SCSI write to complete. */
444 DPRINTF("Command status %d tag 0x%x, len %d\n",
445 s
->result
, s
->tag
, len
);
449 usb_msd_send_status(s
, p
);
450 s
->mode
= USB_MSDM_CBW
;
454 case USB_MSDM_DATAIN
:
455 DPRINTF("Data in %d/%d, scsi_len %d\n", len
, s
->data_len
, s
->scsi_len
);
456 if (len
> s
->data_len
)
461 usb_msd_copy_data(s
);
463 if (s
->residue
&& s
->usb_len
) {
464 s
->data_len
-= s
->usb_len
;
465 memset(s
->usb_buf
, 0, s
->usb_len
);
466 if (s
->data_len
== 0)
467 s
->mode
= USB_MSDM_CSW
;
471 DPRINTF("Deferring packet %p\n", p
);
480 DPRINTF("Unexpected read (len %d)\n", len
);
486 DPRINTF("Bad token\n");
495 static void usb_msd_password_cb(void *opaque
, int err
)
497 MSDState
*s
= opaque
;
500 usb_device_attach(&s
->dev
);
502 qdev_unplug(&s
->dev
.qdev
);
505 static const struct SCSIBusOps usb_msd_scsi_ops
= {
506 .transfer_data
= usb_msd_transfer_data
,
507 .complete
= usb_msd_command_complete
,
508 .cancel
= usb_msd_request_cancelled
511 static int usb_msd_initfn(USBDevice
*dev
)
513 MSDState
*s
= DO_UPCAST(MSDState
, dev
, dev
);
514 BlockDriverState
*bs
= s
->conf
.bs
;
518 error_report("usb-msd: drive property not set");
523 * Hack alert: this pretends to be a block device, but it's really
524 * a SCSI bus that can serve only a single device, which it
525 * creates automatically. But first it needs to detach from its
526 * blockdev, or else scsi_bus_legacy_add_drive() dies when it
529 * The hack is probably a bad idea.
531 bdrv_detach(bs
, &s
->dev
.qdev
);
534 dinfo
= drive_get_by_blockdev(bs
);
535 if (dinfo
&& dinfo
->serial
) {
536 usb_desc_set_string(dev
, STR_SERIALNUMBER
, dinfo
->serial
);
540 scsi_bus_new(&s
->bus
, &s
->dev
.qdev
, 0, 1, &usb_msd_scsi_ops
);
541 s
->scsi_dev
= scsi_bus_legacy_add_drive(&s
->bus
, bs
, 0, !!s
->removable
);
545 s
->bus
.qbus
.allow_hotplug
= 0;
546 usb_msd_handle_reset(dev
);
548 if (bdrv_key_required(bs
)) {
550 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
, s
);
551 s
->dev
.auto_attach
= 0;
557 add_boot_device_path(s
->conf
.bootindex
, &dev
->qdev
, "/disk@0,0");
561 static USBDevice
*usb_msd_init(const char *filename
)
571 /* parse -usbdevice disk: syntax into drive opts */
572 snprintf(id
, sizeof(id
), "usb%d", nr
++);
573 opts
= qemu_opts_create(qemu_find_opts("drive"), id
, 0);
575 p1
= strchr(filename
, ':');
579 if (strstart(filename
, "format=", &p2
)) {
580 int len
= MIN(p1
- p2
, sizeof(fmt
));
581 pstrcpy(fmt
, len
, p2
);
582 qemu_opt_set(opts
, "format", fmt
);
583 } else if (*filename
!= ':') {
584 printf("unrecognized USB mass-storage option %s\n", filename
);
590 printf("block device specification needed\n");
593 qemu_opt_set(opts
, "file", filename
);
594 qemu_opt_set(opts
, "if", "none");
596 /* create host drive */
597 dinfo
= drive_init(opts
, 0);
603 /* create guest device */
604 dev
= usb_create(NULL
/* FIXME */, "usb-storage");
608 if (qdev_prop_set_drive(&dev
->qdev
, "drive", dinfo
->bdrv
) < 0) {
609 qdev_free(&dev
->qdev
);
612 if (qdev_init(&dev
->qdev
) < 0)
618 static struct USBDeviceInfo msd_info
= {
619 .product_desc
= "QEMU USB MSD",
620 .qdev
.name
= "usb-storage",
621 .qdev
.fw_name
= "storage",
622 .qdev
.size
= sizeof(MSDState
),
624 .init
= usb_msd_initfn
,
625 .handle_packet
= usb_generic_handle_packet
,
626 .cancel_packet
= usb_msd_cancel_io
,
627 .handle_attach
= usb_desc_attach
,
628 .handle_reset
= usb_msd_handle_reset
,
629 .handle_control
= usb_msd_handle_control
,
630 .handle_data
= usb_msd_handle_data
,
631 .usbdevice_name
= "disk",
632 .usbdevice_init
= usb_msd_init
,
633 .qdev
.props
= (Property
[]) {
634 DEFINE_BLOCK_PROPERTIES(MSDState
, conf
),
635 DEFINE_PROP_BIT("removable", MSDState
, removable
, 0, false),
636 DEFINE_PROP_END_OF_LIST(),
640 static void usb_msd_register_devices(void)
642 usb_qdev_register(&msd_info
);
644 device_init(usb_msd_register_devices
)