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"
22 #define DPRINTF(fmt, ...) \
23 do { printf("usb-msd: " fmt , ## __VA_ARGS__); } while (0)
25 #define DPRINTF(fmt, ...) do {} while(0)
29 #define MassStorageReset 0xff
30 #define GetMaxLun 0xfe
33 USB_MSDM_CBW
, /* Command Block. */
34 USB_MSDM_DATAOUT
, /* Tranfer data to device. */
35 USB_MSDM_DATAIN
, /* Transfer data from device. */
36 USB_MSDM_CSW
/* Command Status. */
53 /* For async completion. */
74 static const uint8_t qemu_msd_dev_descriptor
[] = {
75 0x12, /* u8 bLength; */
76 0x01, /* u8 bDescriptorType; Device */
77 0x00, 0x01, /* u16 bcdUSB; v1.0 */
79 0x00, /* u8 bDeviceClass; */
80 0x00, /* u8 bDeviceSubClass; */
81 0x00, /* u8 bDeviceProtocol; [ low/full speeds only ] */
82 0x08, /* u8 bMaxPacketSize0; 8 Bytes */
84 /* Vendor and product id are arbitrary. */
85 0x00, 0x00, /* u16 idVendor; */
86 0x00, 0x00, /* u16 idProduct; */
87 0x00, 0x00, /* u16 bcdDevice */
89 0x01, /* u8 iManufacturer; */
90 0x02, /* u8 iProduct; */
91 0x03, /* u8 iSerialNumber; */
92 0x01 /* u8 bNumConfigurations; */
95 static const uint8_t qemu_msd_config_descriptor
[] = {
97 /* one configuration */
98 0x09, /* u8 bLength; */
99 0x02, /* u8 bDescriptorType; Configuration */
100 0x20, 0x00, /* u16 wTotalLength; */
101 0x01, /* u8 bNumInterfaces; (1) */
102 0x01, /* u8 bConfigurationValue; */
103 0x00, /* u8 iConfiguration; */
104 0xc0, /* u8 bmAttributes;
109 0x00, /* u8 MaxPower; */
112 0x09, /* u8 if_bLength; */
113 0x04, /* u8 if_bDescriptorType; Interface */
114 0x00, /* u8 if_bInterfaceNumber; */
115 0x00, /* u8 if_bAlternateSetting; */
116 0x02, /* u8 if_bNumEndpoints; */
117 0x08, /* u8 if_bInterfaceClass; MASS STORAGE */
118 0x06, /* u8 if_bInterfaceSubClass; SCSI */
119 0x50, /* u8 if_bInterfaceProtocol; Bulk Only */
120 0x00, /* u8 if_iInterface; */
122 /* Bulk-In endpoint */
123 0x07, /* u8 ep_bLength; */
124 0x05, /* u8 ep_bDescriptorType; Endpoint */
125 0x81, /* u8 ep_bEndpointAddress; IN Endpoint 1 */
126 0x02, /* u8 ep_bmAttributes; Bulk */
127 0x40, 0x00, /* u16 ep_wMaxPacketSize; */
128 0x00, /* u8 ep_bInterval; */
130 /* Bulk-Out endpoint */
131 0x07, /* u8 ep_bLength; */
132 0x05, /* u8 ep_bDescriptorType; Endpoint */
133 0x02, /* u8 ep_bEndpointAddress; OUT Endpoint 2 */
134 0x02, /* u8 ep_bmAttributes; Bulk */
135 0x40, 0x00, /* u16 ep_wMaxPacketSize; */
136 0x00 /* u8 ep_bInterval; */
139 static void usb_msd_copy_data(MSDState
*s
)
143 if (len
> s
->scsi_len
)
145 if (s
->mode
== USB_MSDM_DATAIN
) {
146 memcpy(s
->usb_buf
, s
->scsi_buf
, len
);
148 memcpy(s
->scsi_buf
, s
->usb_buf
, len
);
155 if (s
->scsi_len
== 0) {
156 if (s
->mode
== USB_MSDM_DATAIN
) {
157 s
->scsi_dev
->info
->read_data(s
->scsi_dev
, s
->tag
);
158 } else if (s
->mode
== USB_MSDM_DATAOUT
) {
159 s
->scsi_dev
->info
->write_data(s
->scsi_dev
, s
->tag
);
164 static void usb_msd_send_status(MSDState
*s
)
166 struct usb_msd_csw csw
;
168 csw
.sig
= cpu_to_le32(0x53425355);
169 csw
.tag
= cpu_to_le32(s
->tag
);
170 csw
.residue
= s
->residue
;
171 csw
.status
= s
->result
;
172 memcpy(s
->usb_buf
, &csw
, 13);
175 static void usb_msd_command_complete(SCSIBus
*bus
, int reason
, uint32_t tag
,
178 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, bus
->qbus
.parent
);
179 USBPacket
*p
= s
->packet
;
182 fprintf(stderr
, "usb-msd: Unexpected SCSI Tag 0x%x\n", tag
);
184 if (reason
== SCSI_REASON_DONE
) {
185 DPRINTF("Command complete %d\n", arg
);
186 s
->residue
= s
->data_len
;
187 s
->result
= arg
!= 0;
189 if (s
->data_len
== 0 && s
->mode
== USB_MSDM_DATAOUT
) {
190 /* A deferred packet with no write data remaining must be
191 the status read packet. */
192 usb_msd_send_status(s
);
193 s
->mode
= USB_MSDM_CBW
;
196 s
->data_len
-= s
->usb_len
;
197 if (s
->mode
== USB_MSDM_DATAIN
)
198 memset(s
->usb_buf
, 0, s
->usb_len
);
201 if (s
->data_len
== 0)
202 s
->mode
= USB_MSDM_CSW
;
205 usb_packet_complete(p
);
206 } else if (s
->data_len
== 0) {
207 s
->mode
= USB_MSDM_CSW
;
212 s
->scsi_buf
= s
->scsi_dev
->info
->get_buf(s
->scsi_dev
, tag
);
214 usb_msd_copy_data(s
);
215 if (s
->usb_len
== 0) {
216 /* Set s->packet to NULL before calling usb_packet_complete
217 because annother request may be issued before
218 usb_packet_complete returns. */
219 DPRINTF("Packet complete %p\n", p
);
221 usb_packet_complete(p
);
226 static void usb_msd_handle_reset(USBDevice
*dev
)
228 MSDState
*s
= (MSDState
*)dev
;
231 s
->mode
= USB_MSDM_CBW
;
234 static int usb_msd_handle_control(USBDevice
*dev
, int request
, int value
,
235 int index
, int length
, uint8_t *data
)
237 MSDState
*s
= (MSDState
*)dev
;
241 case DeviceRequest
| USB_REQ_GET_STATUS
:
242 data
[0] = (1 << USB_DEVICE_SELF_POWERED
) |
243 (dev
->remote_wakeup
<< USB_DEVICE_REMOTE_WAKEUP
);
247 case DeviceOutRequest
| USB_REQ_CLEAR_FEATURE
:
248 if (value
== USB_DEVICE_REMOTE_WAKEUP
) {
249 dev
->remote_wakeup
= 0;
255 case DeviceOutRequest
| USB_REQ_SET_FEATURE
:
256 if (value
== USB_DEVICE_REMOTE_WAKEUP
) {
257 dev
->remote_wakeup
= 1;
263 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
267 case DeviceRequest
| USB_REQ_GET_DESCRIPTOR
:
270 memcpy(data
, qemu_msd_dev_descriptor
,
271 sizeof(qemu_msd_dev_descriptor
));
272 ret
= sizeof(qemu_msd_dev_descriptor
);
275 memcpy(data
, qemu_msd_config_descriptor
,
276 sizeof(qemu_msd_config_descriptor
));
277 ret
= sizeof(qemu_msd_config_descriptor
);
280 switch(value
& 0xff) {
290 /* vendor description */
291 ret
= set_usb_string(data
, "QEMU " QEMU_VERSION
);
294 /* product description */
295 ret
= set_usb_string(data
, "QEMU USB HARDDRIVE");
299 ret
= set_usb_string(data
, "1");
309 case DeviceRequest
| USB_REQ_GET_CONFIGURATION
:
313 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
316 case DeviceRequest
| USB_REQ_GET_INTERFACE
:
320 case DeviceOutRequest
| USB_REQ_SET_INTERFACE
:
323 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
324 if (value
== 0 && index
!= 0x81) { /* clear ep halt */
329 /* Class specific requests. */
330 case MassStorageReset
:
331 /* Reset state ready for the next CBW. */
332 s
->mode
= USB_MSDM_CBW
;
347 static void usb_msd_cancel_io(USBPacket
*p
, void *opaque
)
349 MSDState
*s
= opaque
;
350 s
->scsi_dev
->info
->cancel_io(s
->scsi_dev
, s
->tag
);
355 static int usb_msd_handle_data(USBDevice
*dev
, USBPacket
*p
)
357 MSDState
*s
= (MSDState
*)dev
;
359 struct usb_msd_cbw cbw
;
360 uint8_t devep
= p
->devep
;
361 uint8_t *data
= p
->data
;
372 fprintf(stderr
, "usb-msd: Bad CBW size");
375 memcpy(&cbw
, data
, 31);
376 if (le32_to_cpu(cbw
.sig
) != 0x43425355) {
377 fprintf(stderr
, "usb-msd: Bad signature %08x\n",
378 le32_to_cpu(cbw
.sig
));
381 DPRINTF("Command on LUN %d\n", cbw
.lun
);
383 fprintf(stderr
, "usb-msd: Bad LUN %d\n", cbw
.lun
);
386 s
->tag
= le32_to_cpu(cbw
.tag
);
387 s
->data_len
= le32_to_cpu(cbw
.data_len
);
388 if (s
->data_len
== 0) {
389 s
->mode
= USB_MSDM_CSW
;
390 } else if (cbw
.flags
& 0x80) {
391 s
->mode
= USB_MSDM_DATAIN
;
393 s
->mode
= USB_MSDM_DATAOUT
;
395 DPRINTF("Command tag 0x%x flags %08x len %d data %d\n",
396 s
->tag
, cbw
.flags
, cbw
.cmd_len
, s
->data_len
);
398 s
->scsi_dev
->info
->send_command(s
->scsi_dev
, s
->tag
, cbw
.cmd
, 0);
399 /* ??? Should check that USB and SCSI data transfer
401 if (s
->residue
== 0) {
402 if (s
->mode
== USB_MSDM_DATAIN
) {
403 s
->scsi_dev
->info
->read_data(s
->scsi_dev
, s
->tag
);
404 } else if (s
->mode
== USB_MSDM_DATAOUT
) {
405 s
->scsi_dev
->info
->write_data(s
->scsi_dev
, s
->tag
);
411 case USB_MSDM_DATAOUT
:
412 DPRINTF("Data out %d/%d\n", len
, s
->data_len
);
413 if (len
> s
->data_len
)
419 usb_msd_copy_data(s
);
421 if (s
->residue
&& s
->usb_len
) {
422 s
->data_len
-= s
->usb_len
;
423 if (s
->data_len
== 0)
424 s
->mode
= USB_MSDM_CSW
;
428 DPRINTF("Deferring packet %p\n", p
);
429 usb_defer_packet(p
, usb_msd_cancel_io
, s
);
438 DPRINTF("Unexpected write (len %d)\n", len
);
448 case USB_MSDM_DATAOUT
:
449 if (s
->data_len
!= 0 || len
< 13)
451 /* Waiting for SCSI write to complete. */
452 usb_defer_packet(p
, usb_msd_cancel_io
, s
);
458 DPRINTF("Command status %d tag 0x%x, len %d\n",
459 s
->result
, s
->tag
, len
);
465 usb_msd_send_status(s
);
466 s
->mode
= USB_MSDM_CBW
;
470 case USB_MSDM_DATAIN
:
471 DPRINTF("Data in %d/%d\n", len
, s
->data_len
);
472 if (len
> s
->data_len
)
477 usb_msd_copy_data(s
);
479 if (s
->residue
&& s
->usb_len
) {
480 s
->data_len
-= s
->usb_len
;
481 memset(s
->usb_buf
, 0, s
->usb_len
);
482 if (s
->data_len
== 0)
483 s
->mode
= USB_MSDM_CSW
;
487 DPRINTF("Deferring packet %p\n", p
);
488 usb_defer_packet(p
, usb_msd_cancel_io
, s
);
497 DPRINTF("Unexpected read (len %d)\n", len
);
503 DPRINTF("Bad token\n");
512 static void usb_msd_password_cb(void *opaque
, int err
)
514 MSDState
*s
= opaque
;
517 usb_device_attach(&s
->dev
);
519 qdev_unplug(&s
->dev
.qdev
);
522 static int usb_msd_initfn(USBDevice
*dev
)
524 MSDState
*s
= DO_UPCAST(MSDState
, dev
, dev
);
526 if (!s
->dinfo
|| !s
->dinfo
->bdrv
) {
527 qemu_error("usb-msd: drive property not set\n");
531 s
->dev
.speed
= USB_SPEED_FULL
;
532 scsi_bus_new(&s
->bus
, &s
->dev
.qdev
, 0, 1, usb_msd_command_complete
);
533 s
->scsi_dev
= scsi_bus_legacy_add_drive(&s
->bus
, s
->dinfo
, 0);
534 s
->bus
.qbus
.allow_hotplug
= 0;
535 usb_msd_handle_reset(dev
);
537 if (bdrv_key_required(s
->dinfo
->bdrv
)) {
538 if (s
->dev
.qdev
.hotplugged
) {
539 monitor_read_bdrv_key_start(cur_mon
, s
->dinfo
->bdrv
,
540 usb_msd_password_cb
, s
);
541 s
->dev
.auto_attach
= 0;
550 static USBDevice
*usb_msd_init(const char *filename
)
561 /* parse -usbdevice disk: syntax into drive opts */
562 snprintf(id
, sizeof(id
), "usb%d", nr
++);
563 opts
= qemu_opts_create(&qemu_drive_opts
, id
, 0);
565 p1
= strchr(filename
, ':');
569 if (strstart(filename
, "format=", &p2
)) {
570 int len
= MIN(p1
- p2
, sizeof(fmt
));
571 pstrcpy(fmt
, len
, p2
);
572 qemu_opt_set(opts
, "format", fmt
);
573 } else if (*filename
!= ':') {
574 printf("unrecognized USB mass-storage option %s\n", filename
);
580 printf("block device specification needed\n");
583 qemu_opt_set(opts
, "file", filename
);
584 qemu_opt_set(opts
, "if", "none");
586 /* create host drive */
587 dinfo
= drive_init(opts
, NULL
, &fatal_error
);
593 /* create guest device */
594 dev
= usb_create(NULL
/* FIXME */, "QEMU USB MSD");
595 qdev_prop_set_drive(&dev
->qdev
, "drive", dinfo
);
596 if (qdev_init(&dev
->qdev
) < 0)
602 static struct USBDeviceInfo msd_info
= {
603 .qdev
.name
= "QEMU USB MSD",
604 .qdev
.alias
= "usb-storage",
605 .qdev
.size
= sizeof(MSDState
),
606 .init
= usb_msd_initfn
,
607 .handle_packet
= usb_generic_handle_packet
,
608 .handle_reset
= usb_msd_handle_reset
,
609 .handle_control
= usb_msd_handle_control
,
610 .handle_data
= usb_msd_handle_data
,
611 .usbdevice_name
= "disk",
612 .usbdevice_init
= usb_msd_init
,
613 .qdev
.props
= (Property
[]) {
614 DEFINE_PROP_DRIVE("drive", MSDState
, dinfo
),
615 DEFINE_PROP_END_OF_LIST(),
619 static void usb_msd_register_devices(void)
621 usb_qdev_register(&msd_info
);
623 device_init(usb_msd_register_devices
)