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/osdep.h"
11 #include "qapi/error.h"
12 #include "qemu/error-report.h"
13 #include "qemu/module.h"
14 #include "qemu/option.h"
15 #include "qemu/config-file.h"
17 #include "hw/usb/msd.h"
19 #include "hw/qdev-properties.h"
20 #include "hw/scsi/scsi.h"
21 #include "migration/vmstate.h"
22 #include "qemu/cutils.h"
23 #include "qom/object.h"
27 #define MassStorageReset 0xff
28 #define GetMaxLun 0xfe
49 static const USBDescStrings desc_strings
= {
50 [STR_MANUFACTURER
] = "QEMU",
51 [STR_PRODUCT
] = "QEMU USB HARDDRIVE",
52 [STR_SERIALNUMBER
] = "1",
53 [STR_CONFIG_FULL
] = "Full speed config (usb 1.1)",
54 [STR_CONFIG_HIGH
] = "High speed config (usb 2.0)",
55 [STR_CONFIG_SUPER
] = "Super speed config (usb 3.0)",
58 static const USBDescIface desc_iface_full
= {
59 .bInterfaceNumber
= 0,
61 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
62 .bInterfaceSubClass
= 0x06, /* SCSI */
63 .bInterfaceProtocol
= 0x50, /* Bulk */
64 .eps
= (USBDescEndpoint
[]) {
66 .bEndpointAddress
= USB_DIR_IN
| 0x01,
67 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
70 .bEndpointAddress
= USB_DIR_OUT
| 0x02,
71 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
77 static const USBDescDevice desc_device_full
= {
80 .bNumConfigurations
= 1,
81 .confs
= (USBDescConfig
[]) {
84 .bConfigurationValue
= 1,
85 .iConfiguration
= STR_CONFIG_FULL
,
86 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_SELFPOWER
,
88 .ifs
= &desc_iface_full
,
93 static const USBDescIface desc_iface_high
= {
94 .bInterfaceNumber
= 0,
96 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
97 .bInterfaceSubClass
= 0x06, /* SCSI */
98 .bInterfaceProtocol
= 0x50, /* Bulk */
99 .eps
= (USBDescEndpoint
[]) {
101 .bEndpointAddress
= USB_DIR_IN
| 0x01,
102 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
103 .wMaxPacketSize
= 512,
105 .bEndpointAddress
= USB_DIR_OUT
| 0x02,
106 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
107 .wMaxPacketSize
= 512,
112 static const USBDescDevice desc_device_high
= {
114 .bMaxPacketSize0
= 64,
115 .bNumConfigurations
= 1,
116 .confs
= (USBDescConfig
[]) {
119 .bConfigurationValue
= 1,
120 .iConfiguration
= STR_CONFIG_HIGH
,
121 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_SELFPOWER
,
123 .ifs
= &desc_iface_high
,
128 static const USBDescIface desc_iface_super
= {
129 .bInterfaceNumber
= 0,
131 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
132 .bInterfaceSubClass
= 0x06, /* SCSI */
133 .bInterfaceProtocol
= 0x50, /* Bulk */
134 .eps
= (USBDescEndpoint
[]) {
136 .bEndpointAddress
= USB_DIR_IN
| 0x01,
137 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
138 .wMaxPacketSize
= 1024,
141 .bEndpointAddress
= USB_DIR_OUT
| 0x02,
142 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
143 .wMaxPacketSize
= 1024,
149 static const USBDescDevice desc_device_super
= {
151 .bMaxPacketSize0
= 9,
152 .bNumConfigurations
= 1,
153 .confs
= (USBDescConfig
[]) {
156 .bConfigurationValue
= 1,
157 .iConfiguration
= STR_CONFIG_SUPER
,
158 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_SELFPOWER
,
160 .ifs
= &desc_iface_super
,
165 static const USBDesc desc
= {
167 .idVendor
= 0x46f4, /* CRC16() of "QEMU" */
170 .iManufacturer
= STR_MANUFACTURER
,
171 .iProduct
= STR_PRODUCT
,
172 .iSerialNumber
= STR_SERIALNUMBER
,
174 .full
= &desc_device_full
,
175 .high
= &desc_device_high
,
176 .super
= &desc_device_super
,
180 static void usb_msd_packet_complete(MSDState
*s
)
182 USBPacket
*p
= s
->packet
;
185 * Set s->packet to NULL before calling usb_packet_complete
186 * because another request may be issued before
187 * usb_packet_complete returns.
189 trace_usb_msd_packet_complete();
191 usb_packet_complete(&s
->dev
, p
);
194 static void usb_msd_fatal_error(MSDState
*s
)
196 trace_usb_msd_fatal_error();
199 s
->packet
->status
= USB_RET_STALL
;
200 usb_msd_packet_complete(s
);
204 * Guest messed up up device state with illegal requests. Go
205 * ignore any requests until the guests resets the device (and
206 * brings it into a known state that way).
208 s
->needs_reset
= true;
211 static void usb_msd_copy_data(MSDState
*s
, USBPacket
*p
)
214 len
= p
->iov
.size
- p
->actual_length
;
215 if (len
> s
->scsi_len
)
217 usb_packet_copy(p
, scsi_req_get_buf(s
->req
) + s
->scsi_off
, len
);
220 if (len
> s
->data_len
) {
224 if (s
->scsi_len
== 0 || s
->data_len
== 0) {
225 scsi_req_continue(s
->req
);
229 static void usb_msd_send_status(MSDState
*s
, USBPacket
*p
)
233 trace_usb_msd_send_status(s
->csw
.status
, le32_to_cpu(s
->csw
.tag
),
236 assert(s
->csw
.sig
== cpu_to_le32(0x53425355));
237 len
= MIN(sizeof(s
->csw
), p
->iov
.size
);
238 usb_packet_copy(p
, &s
->csw
, len
);
239 memset(&s
->csw
, 0, sizeof(s
->csw
));
242 void usb_msd_transfer_data(SCSIRequest
*req
, uint32_t len
)
244 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
245 USBPacket
*p
= s
->packet
;
247 if ((s
->mode
== USB_MSDM_DATAOUT
) != (req
->cmd
.mode
== SCSI_XFER_TO_DEV
)) {
248 usb_msd_fatal_error(s
);
255 usb_msd_copy_data(s
, p
);
257 if (p
&& p
->actual_length
== p
->iov
.size
) {
258 p
->status
= USB_RET_SUCCESS
; /* Clear previous ASYNC status */
259 usb_msd_packet_complete(s
);
264 void usb_msd_command_complete(SCSIRequest
*req
, size_t resid
)
266 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
267 USBPacket
*p
= s
->packet
;
269 trace_usb_msd_cmd_complete(req
->status
, req
->tag
);
271 s
->csw
.sig
= cpu_to_le32(0x53425355);
272 s
->csw
.tag
= cpu_to_le32(req
->tag
);
273 s
->csw
.residue
= cpu_to_le32(s
->data_len
);
274 s
->csw
.status
= req
->status
!= 0;
277 if (s
->data_len
== 0 && s
->mode
== USB_MSDM_DATAOUT
) {
278 /* A deferred packet with no write data remaining must be
279 the status read packet. */
280 usb_msd_send_status(s
, p
);
281 s
->mode
= USB_MSDM_CBW
;
282 } else if (s
->mode
== USB_MSDM_CSW
) {
283 usb_msd_send_status(s
, p
);
284 s
->mode
= USB_MSDM_CBW
;
287 int len
= (p
->iov
.size
- p
->actual_length
);
288 usb_packet_skip(p
, len
);
289 if (len
> s
->data_len
) {
294 if (s
->data_len
== 0) {
295 s
->mode
= USB_MSDM_CSW
;
298 p
->status
= USB_RET_SUCCESS
; /* Clear previous ASYNC status */
299 usb_msd_packet_complete(s
);
300 } else if (s
->data_len
== 0) {
301 s
->mode
= USB_MSDM_CSW
;
307 void usb_msd_request_cancelled(SCSIRequest
*req
)
309 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
311 trace_usb_msd_cmd_cancel(req
->tag
);
314 s
->csw
.sig
= cpu_to_le32(0x53425355);
315 s
->csw
.tag
= cpu_to_le32(req
->tag
);
316 s
->csw
.status
= 1; /* error */
318 scsi_req_unref(s
->req
);
324 void usb_msd_handle_reset(USBDevice
*dev
)
326 MSDState
*s
= (MSDState
*)dev
;
328 trace_usb_msd_reset();
330 scsi_req_cancel(s
->req
);
332 assert(s
->req
== NULL
);
335 s
->packet
->status
= USB_RET_STALL
;
336 usb_msd_packet_complete(s
);
339 memset(&s
->csw
, 0, sizeof(s
->csw
));
340 s
->mode
= USB_MSDM_CBW
;
342 s
->needs_reset
= false;
345 static void usb_msd_handle_control(USBDevice
*dev
, USBPacket
*p
,
346 int request
, int value
, int index
, int length
, uint8_t *data
)
348 MSDState
*s
= (MSDState
*)dev
;
349 SCSIDevice
*scsi_dev
;
352 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
358 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
360 /* Class specific requests. */
361 case ClassInterfaceOutRequest
| MassStorageReset
:
362 /* Reset state ready for the next CBW. */
363 s
->mode
= USB_MSDM_CBW
;
365 case ClassInterfaceRequest
| GetMaxLun
:
368 scsi_dev
= scsi_device_find(&s
->bus
, 0, 0, maxlun
+1);
369 if (scsi_dev
== NULL
) {
372 if (scsi_dev
->lun
!= maxlun
+1) {
377 trace_usb_msd_maxlun(maxlun
);
379 p
->actual_length
= 1;
382 p
->status
= USB_RET_STALL
;
387 static void usb_msd_cancel_io(USBDevice
*dev
, USBPacket
*p
)
389 MSDState
*s
= USB_STORAGE_DEV(dev
);
391 assert(s
->packet
== p
);
395 scsi_req_cancel(s
->req
);
399 static void usb_msd_handle_data(USBDevice
*dev
, USBPacket
*p
)
401 MSDState
*s
= (MSDState
*)dev
;
403 struct usb_msd_cbw cbw
;
404 uint8_t devep
= p
->ep
->nr
;
405 SCSIDevice
*scsi_dev
;
408 if (s
->needs_reset
) {
409 p
->status
= USB_RET_STALL
;
420 if (p
->iov
.size
!= 31) {
421 error_report("usb-msd: Bad CBW size");
424 usb_packet_copy(p
, &cbw
, 31);
425 if (le32_to_cpu(cbw
.sig
) != 0x43425355) {
426 error_report("usb-msd: Bad signature %08x",
427 le32_to_cpu(cbw
.sig
));
430 scsi_dev
= scsi_device_find(&s
->bus
, 0, 0, cbw
.lun
);
431 if (scsi_dev
== NULL
) {
432 error_report("usb-msd: Bad LUN %d", cbw
.lun
);
435 tag
= le32_to_cpu(cbw
.tag
);
436 s
->data_len
= le32_to_cpu(cbw
.data_len
);
437 if (s
->data_len
== 0) {
438 s
->mode
= USB_MSDM_CSW
;
439 } else if (cbw
.flags
& 0x80) {
440 s
->mode
= USB_MSDM_DATAIN
;
442 s
->mode
= USB_MSDM_DATAOUT
;
444 trace_usb_msd_cmd_submit(cbw
.lun
, tag
, cbw
.flags
,
445 cbw
.cmd_len
, s
->data_len
);
446 assert(le32_to_cpu(s
->csw
.residue
) == 0);
448 s
->req
= scsi_req_new(scsi_dev
, tag
, cbw
.lun
, cbw
.cmd
, cbw
.cmd_len
, NULL
);
450 scsi_req_print(s
->req
);
452 len
= scsi_req_enqueue(s
->req
);
454 scsi_req_continue(s
->req
);
458 case USB_MSDM_DATAOUT
:
459 trace_usb_msd_data_out(p
->iov
.size
, s
->data_len
);
460 if (p
->iov
.size
> s
->data_len
) {
465 usb_msd_copy_data(s
, p
);
467 if (le32_to_cpu(s
->csw
.residue
)) {
468 len
= p
->iov
.size
- p
->actual_length
;
470 usb_packet_skip(p
, len
);
471 if (len
> s
->data_len
) {
475 if (s
->data_len
== 0) {
476 s
->mode
= USB_MSDM_CSW
;
480 if (p
->actual_length
< p
->iov
.size
) {
481 trace_usb_msd_packet_async();
483 p
->status
= USB_RET_ASYNC
;
497 case USB_MSDM_DATAOUT
:
498 if (s
->data_len
!= 0 || p
->iov
.size
< 13) {
501 /* Waiting for SCSI write to complete. */
502 trace_usb_msd_packet_async();
504 p
->status
= USB_RET_ASYNC
;
508 if (p
->iov
.size
< 13) {
513 /* still in flight */
514 trace_usb_msd_packet_async();
516 p
->status
= USB_RET_ASYNC
;
518 usb_msd_send_status(s
, p
);
519 s
->mode
= USB_MSDM_CBW
;
523 case USB_MSDM_DATAIN
:
524 trace_usb_msd_data_in(p
->iov
.size
, s
->data_len
, s
->scsi_len
);
526 usb_msd_copy_data(s
, p
);
528 if (le32_to_cpu(s
->csw
.residue
)) {
529 len
= p
->iov
.size
- p
->actual_length
;
531 usb_packet_skip(p
, len
);
532 if (len
> s
->data_len
) {
536 if (s
->data_len
== 0) {
537 s
->mode
= USB_MSDM_CSW
;
541 if (p
->actual_length
< p
->iov
.size
&& s
->mode
== USB_MSDM_DATAIN
) {
542 trace_usb_msd_packet_async();
544 p
->status
= USB_RET_ASYNC
;
555 p
->status
= USB_RET_STALL
;
560 void *usb_msd_load_request(QEMUFile
*f
, SCSIRequest
*req
)
562 MSDState
*s
= DO_UPCAST(MSDState
, dev
.qdev
, req
->bus
->qbus
.parent
);
564 /* nothing to load, just store req in our state struct */
565 assert(s
->req
== NULL
);
571 static const VMStateDescription vmstate_usb_msd
= {
572 .name
= "usb-storage",
574 .minimum_version_id
= 1,
575 .fields
= (VMStateField
[]) {
576 VMSTATE_USB_DEVICE(dev
, MSDState
),
577 VMSTATE_UINT32(mode
, MSDState
),
578 VMSTATE_UINT32(scsi_len
, MSDState
),
579 VMSTATE_UINT32(scsi_off
, MSDState
),
580 VMSTATE_UINT32(data_len
, MSDState
),
581 VMSTATE_UINT32(csw
.sig
, MSDState
),
582 VMSTATE_UINT32(csw
.tag
, MSDState
),
583 VMSTATE_UINT32(csw
.residue
, MSDState
),
584 VMSTATE_UINT8(csw
.status
, MSDState
),
585 VMSTATE_END_OF_LIST()
589 static void usb_msd_class_initfn_common(ObjectClass
*klass
, void *data
)
591 DeviceClass
*dc
= DEVICE_CLASS(klass
);
592 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
594 uc
->product_desc
= "QEMU USB MSD";
595 uc
->usb_desc
= &desc
;
596 uc
->cancel_packet
= usb_msd_cancel_io
;
597 uc
->handle_attach
= usb_desc_attach
;
598 uc
->handle_reset
= usb_msd_handle_reset
;
599 uc
->handle_control
= usb_msd_handle_control
;
600 uc
->handle_data
= usb_msd_handle_data
;
601 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
602 dc
->fw_name
= "storage";
603 dc
->vmsd
= &vmstate_usb_msd
;
606 static const TypeInfo usb_storage_dev_type_info
= {
607 .name
= TYPE_USB_STORAGE
,
608 .parent
= TYPE_USB_DEVICE
,
609 .instance_size
= sizeof(MSDState
),
611 .class_init
= usb_msd_class_initfn_common
,
614 static void usb_msd_register_types(void)
616 type_register_static(&usb_storage_dev_type_info
);
619 type_init(usb_msd_register_types
)