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"
23 #define DPRINTF(fmt, ...) \
24 do { printf("usb-msd: " fmt , ## __VA_ARGS__); } while (0)
26 #define DPRINTF(fmt, ...) do {} while(0)
30 #define MassStorageReset 0xff
31 #define GetMaxLun 0xfe
34 USB_MSDM_CBW
, /* Command Block. */
35 USB_MSDM_DATAOUT
, /* Tranfer data to device. */
36 USB_MSDM_DATAIN
, /* Transfer data from device. */
37 USB_MSDM_CSW
/* Command Status. */
54 /* For async completion. */
75 static const uint8_t qemu_msd_dev_descriptor
[] = {
76 0x12, /* u8 bLength; */
77 0x01, /* u8 bDescriptorType; Device */
78 0x00, 0x01, /* u16 bcdUSB; v1.0 */
80 0x00, /* u8 bDeviceClass; */
81 0x00, /* u8 bDeviceSubClass; */
82 0x00, /* u8 bDeviceProtocol; [ low/full speeds only ] */
83 0x08, /* u8 bMaxPacketSize0; 8 Bytes */
85 /* Vendor and product id are arbitrary. */
86 0x00, 0x00, /* u16 idVendor; */
87 0x00, 0x00, /* u16 idProduct; */
88 0x00, 0x00, /* u16 bcdDevice */
90 0x01, /* u8 iManufacturer; */
91 0x02, /* u8 iProduct; */
92 0x03, /* u8 iSerialNumber; */
93 0x01 /* u8 bNumConfigurations; */
96 static const uint8_t qemu_msd_config_descriptor
[] = {
98 /* one configuration */
99 0x09, /* u8 bLength; */
100 0x02, /* u8 bDescriptorType; Configuration */
101 0x20, 0x00, /* u16 wTotalLength; */
102 0x01, /* u8 bNumInterfaces; (1) */
103 0x01, /* u8 bConfigurationValue; */
104 0x00, /* u8 iConfiguration; */
105 0xc0, /* u8 bmAttributes;
110 0x00, /* u8 MaxPower; */
113 0x09, /* u8 if_bLength; */
114 0x04, /* u8 if_bDescriptorType; Interface */
115 0x00, /* u8 if_bInterfaceNumber; */
116 0x00, /* u8 if_bAlternateSetting; */
117 0x02, /* u8 if_bNumEndpoints; */
118 0x08, /* u8 if_bInterfaceClass; MASS STORAGE */
119 0x06, /* u8 if_bInterfaceSubClass; SCSI */
120 0x50, /* u8 if_bInterfaceProtocol; Bulk Only */
121 0x00, /* u8 if_iInterface; */
123 /* Bulk-In endpoint */
124 0x07, /* u8 ep_bLength; */
125 0x05, /* u8 ep_bDescriptorType; Endpoint */
126 0x81, /* u8 ep_bEndpointAddress; IN Endpoint 1 */
127 0x02, /* u8 ep_bmAttributes; Bulk */
128 0x40, 0x00, /* u16 ep_wMaxPacketSize; */
129 0x00, /* u8 ep_bInterval; */
131 /* Bulk-Out endpoint */
132 0x07, /* u8 ep_bLength; */
133 0x05, /* u8 ep_bDescriptorType; Endpoint */
134 0x02, /* u8 ep_bEndpointAddress; OUT Endpoint 2 */
135 0x02, /* u8 ep_bmAttributes; Bulk */
136 0x40, 0x00, /* u16 ep_wMaxPacketSize; */
137 0x00 /* u8 ep_bInterval; */
140 static void usb_msd_copy_data(MSDState
*s
)
144 if (len
> s
->scsi_len
)
146 if (s
->mode
== USB_MSDM_DATAIN
) {
147 memcpy(s
->usb_buf
, s
->scsi_buf
, len
);
149 memcpy(s
->scsi_buf
, s
->usb_buf
, len
);
156 if (s
->scsi_len
== 0) {
157 if (s
->mode
== USB_MSDM_DATAIN
) {
158 s
->scsi_dev
->info
->read_data(s
->scsi_dev
, s
->tag
);
159 } else if (s
->mode
== USB_MSDM_DATAOUT
) {
160 s
->scsi_dev
->info
->write_data(s
->scsi_dev
, s
->tag
);
165 static void usb_msd_send_status(MSDState
*s
)
167 struct usb_msd_csw csw
;
169 csw
.sig
= cpu_to_le32(0x53425355);
170 csw
.tag
= cpu_to_le32(s
->tag
);
171 csw
.residue
= s
->residue
;
172 csw
.status
= s
->result
;
173 memcpy(s
->usb_buf
, &csw
, 13);
176 static void usb_msd_command_complete(SCSIBus
*bus
, int reason
, uint32_t tag
,
179 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, bus
->qbus
.parent
);
180 USBPacket
*p
= s
->packet
;
183 fprintf(stderr
, "usb-msd: Unexpected SCSI Tag 0x%x\n", tag
);
185 if (reason
== SCSI_REASON_DONE
) {
186 DPRINTF("Command complete %d\n", arg
);
187 s
->residue
= s
->data_len
;
188 s
->result
= arg
!= 0;
190 if (s
->data_len
== 0 && s
->mode
== USB_MSDM_DATAOUT
) {
191 /* A deferred packet with no write data remaining must be
192 the status read packet. */
193 usb_msd_send_status(s
);
194 s
->mode
= USB_MSDM_CBW
;
197 s
->data_len
-= s
->usb_len
;
198 if (s
->mode
== USB_MSDM_DATAIN
)
199 memset(s
->usb_buf
, 0, s
->usb_len
);
202 if (s
->data_len
== 0)
203 s
->mode
= USB_MSDM_CSW
;
206 usb_packet_complete(p
);
207 } else if (s
->data_len
== 0) {
208 s
->mode
= USB_MSDM_CSW
;
213 s
->scsi_buf
= s
->scsi_dev
->info
->get_buf(s
->scsi_dev
, tag
);
215 usb_msd_copy_data(s
);
216 if (s
->usb_len
== 0) {
217 /* Set s->packet to NULL before calling usb_packet_complete
218 because annother request may be issued before
219 usb_packet_complete returns. */
220 DPRINTF("Packet complete %p\n", p
);
222 usb_packet_complete(p
);
227 static void usb_msd_handle_reset(USBDevice
*dev
)
229 MSDState
*s
= (MSDState
*)dev
;
232 s
->mode
= USB_MSDM_CBW
;
235 static int usb_msd_handle_control(USBDevice
*dev
, int request
, int value
,
236 int index
, int length
, uint8_t *data
)
238 MSDState
*s
= (MSDState
*)dev
;
242 case DeviceRequest
| USB_REQ_GET_STATUS
:
243 data
[0] = (1 << USB_DEVICE_SELF_POWERED
) |
244 (dev
->remote_wakeup
<< USB_DEVICE_REMOTE_WAKEUP
);
248 case DeviceOutRequest
| USB_REQ_CLEAR_FEATURE
:
249 if (value
== USB_DEVICE_REMOTE_WAKEUP
) {
250 dev
->remote_wakeup
= 0;
256 case DeviceOutRequest
| USB_REQ_SET_FEATURE
:
257 if (value
== USB_DEVICE_REMOTE_WAKEUP
) {
258 dev
->remote_wakeup
= 1;
264 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
268 case DeviceRequest
| USB_REQ_GET_DESCRIPTOR
:
271 memcpy(data
, qemu_msd_dev_descriptor
,
272 sizeof(qemu_msd_dev_descriptor
));
273 ret
= sizeof(qemu_msd_dev_descriptor
);
276 memcpy(data
, qemu_msd_config_descriptor
,
277 sizeof(qemu_msd_config_descriptor
));
278 ret
= sizeof(qemu_msd_config_descriptor
);
281 switch(value
& 0xff) {
291 /* vendor description */
292 ret
= set_usb_string(data
, "QEMU " QEMU_VERSION
);
295 /* product description */
296 ret
= set_usb_string(data
, "QEMU USB HARDDRIVE");
300 ret
= set_usb_string(data
, "1");
310 case DeviceRequest
| USB_REQ_GET_CONFIGURATION
:
314 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
317 case DeviceRequest
| USB_REQ_GET_INTERFACE
:
321 case DeviceOutRequest
| USB_REQ_SET_INTERFACE
:
324 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
327 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
330 /* Class specific requests. */
331 case ClassInterfaceOutRequest
| MassStorageReset
:
332 /* Reset state ready for the next CBW. */
333 s
->mode
= USB_MSDM_CBW
;
336 case ClassInterfaceRequest
| GetMaxLun
:
348 static void usb_msd_cancel_io(USBPacket
*p
, void *opaque
)
350 MSDState
*s
= opaque
;
351 s
->scsi_dev
->info
->cancel_io(s
->scsi_dev
, s
->tag
);
356 static int usb_msd_handle_data(USBDevice
*dev
, USBPacket
*p
)
358 MSDState
*s
= (MSDState
*)dev
;
360 struct usb_msd_cbw cbw
;
361 uint8_t devep
= p
->devep
;
362 uint8_t *data
= p
->data
;
373 fprintf(stderr
, "usb-msd: Bad CBW size");
376 memcpy(&cbw
, data
, 31);
377 if (le32_to_cpu(cbw
.sig
) != 0x43425355) {
378 fprintf(stderr
, "usb-msd: Bad signature %08x\n",
379 le32_to_cpu(cbw
.sig
));
382 DPRINTF("Command on LUN %d\n", cbw
.lun
);
384 fprintf(stderr
, "usb-msd: Bad LUN %d\n", cbw
.lun
);
387 s
->tag
= le32_to_cpu(cbw
.tag
);
388 s
->data_len
= le32_to_cpu(cbw
.data_len
);
389 if (s
->data_len
== 0) {
390 s
->mode
= USB_MSDM_CSW
;
391 } else if (cbw
.flags
& 0x80) {
392 s
->mode
= USB_MSDM_DATAIN
;
394 s
->mode
= USB_MSDM_DATAOUT
;
396 DPRINTF("Command tag 0x%x flags %08x len %d data %d\n",
397 s
->tag
, cbw
.flags
, cbw
.cmd_len
, s
->data_len
);
399 s
->scsi_dev
->info
->send_command(s
->scsi_dev
, s
->tag
, cbw
.cmd
, 0);
400 /* ??? Should check that USB and SCSI data transfer
402 if (s
->residue
== 0) {
403 if (s
->mode
== USB_MSDM_DATAIN
) {
404 s
->scsi_dev
->info
->read_data(s
->scsi_dev
, s
->tag
);
405 } else if (s
->mode
== USB_MSDM_DATAOUT
) {
406 s
->scsi_dev
->info
->write_data(s
->scsi_dev
, s
->tag
);
412 case USB_MSDM_DATAOUT
:
413 DPRINTF("Data out %d/%d\n", len
, s
->data_len
);
414 if (len
> s
->data_len
)
420 usb_msd_copy_data(s
);
422 if (s
->residue
&& s
->usb_len
) {
423 s
->data_len
-= s
->usb_len
;
424 if (s
->data_len
== 0)
425 s
->mode
= USB_MSDM_CSW
;
429 DPRINTF("Deferring packet %p\n", p
);
430 usb_defer_packet(p
, usb_msd_cancel_io
, s
);
439 DPRINTF("Unexpected write (len %d)\n", len
);
449 case USB_MSDM_DATAOUT
:
450 if (s
->data_len
!= 0 || len
< 13)
452 /* Waiting for SCSI write to complete. */
453 usb_defer_packet(p
, usb_msd_cancel_io
, s
);
459 DPRINTF("Command status %d tag 0x%x, len %d\n",
460 s
->result
, s
->tag
, len
);
466 usb_msd_send_status(s
);
467 s
->mode
= USB_MSDM_CBW
;
471 case USB_MSDM_DATAIN
:
472 DPRINTF("Data in %d/%d\n", len
, s
->data_len
);
473 if (len
> s
->data_len
)
478 usb_msd_copy_data(s
);
480 if (s
->residue
&& s
->usb_len
) {
481 s
->data_len
-= s
->usb_len
;
482 memset(s
->usb_buf
, 0, s
->usb_len
);
483 if (s
->data_len
== 0)
484 s
->mode
= USB_MSDM_CSW
;
488 DPRINTF("Deferring packet %p\n", p
);
489 usb_defer_packet(p
, usb_msd_cancel_io
, s
);
498 DPRINTF("Unexpected read (len %d)\n", len
);
504 DPRINTF("Bad token\n");
513 static void usb_msd_password_cb(void *opaque
, int err
)
515 MSDState
*s
= opaque
;
518 usb_device_attach(&s
->dev
);
520 qdev_unplug(&s
->dev
.qdev
);
523 static int usb_msd_initfn(USBDevice
*dev
)
525 MSDState
*s
= DO_UPCAST(MSDState
, dev
, dev
);
526 BlockDriverState
*bs
= s
->conf
.bs
;
529 error_report("usb-msd: drive property not set");
534 * Hack alert: this pretends to be a block device, but it's really
535 * a SCSI bus that can serve only a single device, which it
536 * creates automatically. But first it needs to detach from its
537 * blockdev, or else scsi_bus_legacy_add_drive() dies when it
540 * The hack is probably a bad idea.
542 bdrv_detach(bs
, &s
->dev
.qdev
);
545 s
->dev
.speed
= USB_SPEED_FULL
;
546 scsi_bus_new(&s
->bus
, &s
->dev
.qdev
, 0, 1, usb_msd_command_complete
);
547 s
->scsi_dev
= scsi_bus_legacy_add_drive(&s
->bus
, bs
, 0);
551 s
->bus
.qbus
.allow_hotplug
= 0;
552 usb_msd_handle_reset(dev
);
554 if (bdrv_key_required(bs
)) {
556 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
, s
);
557 s
->dev
.auto_attach
= 0;
566 static USBDevice
*usb_msd_init(const char *filename
)
577 /* parse -usbdevice disk: syntax into drive opts */
578 snprintf(id
, sizeof(id
), "usb%d", nr
++);
579 opts
= qemu_opts_create(qemu_find_opts("drive"), id
, 0);
581 p1
= strchr(filename
, ':');
585 if (strstart(filename
, "format=", &p2
)) {
586 int len
= MIN(p1
- p2
, sizeof(fmt
));
587 pstrcpy(fmt
, len
, p2
);
588 qemu_opt_set(opts
, "format", fmt
);
589 } else if (*filename
!= ':') {
590 printf("unrecognized USB mass-storage option %s\n", filename
);
596 printf("block device specification needed\n");
599 qemu_opt_set(opts
, "file", filename
);
600 qemu_opt_set(opts
, "if", "none");
602 /* create host drive */
603 dinfo
= drive_init(opts
, 0, &fatal_error
);
609 /* create guest device */
610 dev
= usb_create(NULL
/* FIXME */, "usb-storage");
614 if (qdev_prop_set_drive(&dev
->qdev
, "drive", dinfo
->bdrv
) < 0) {
615 qdev_free(&dev
->qdev
);
618 if (qdev_init(&dev
->qdev
) < 0)
624 static struct USBDeviceInfo msd_info
= {
625 .product_desc
= "QEMU USB MSD",
626 .qdev
.name
= "usb-storage",
627 .qdev
.size
= sizeof(MSDState
),
628 .init
= usb_msd_initfn
,
629 .handle_packet
= usb_generic_handle_packet
,
630 .handle_reset
= usb_msd_handle_reset
,
631 .handle_control
= usb_msd_handle_control
,
632 .handle_data
= usb_msd_handle_data
,
633 .usbdevice_name
= "disk",
634 .usbdevice_init
= usb_msd_init
,
635 .qdev
.props
= (Property
[]) {
636 DEFINE_BLOCK_PROPERTIES(MSDState
, conf
),
637 DEFINE_PROP_END_OF_LIST(),
641 static void usb_msd_register_devices(void)
643 usb_qdev_register(&msd_info
);
645 device_init(usb_msd_register_devices
)