pc: acpi: cpuhp: move CPMA() method into SSDT
[qemu/ar7.git] / hw / usb / dev-storage.c
blob597d8fd18455958f1b46ddd5c6e0e6d58321d287
1 /*
2 * USB Mass Storage Device emulation
4 * Copyright (c) 2006 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the LGPL.
8 */
10 #include "qemu-common.h"
11 #include "qemu/error-report.h"
12 #include "qemu/option.h"
13 #include "qemu/config-file.h"
14 #include "hw/usb.h"
15 #include "hw/usb/desc.h"
16 #include "hw/scsi/scsi.h"
17 #include "ui/console.h"
18 #include "monitor/monitor.h"
19 #include "sysemu/sysemu.h"
20 #include "sysemu/block-backend.h"
21 #include "sysemu/blockdev.h"
22 #include "qapi/visitor.h"
24 //#define DEBUG_MSD
26 #ifdef DEBUG_MSD
27 #define DPRINTF(fmt, ...) \
28 do { printf("usb-msd: " fmt , ## __VA_ARGS__); } while (0)
29 #else
30 #define DPRINTF(fmt, ...) do {} while(0)
31 #endif
33 /* USB requests. */
34 #define MassStorageReset 0xff
35 #define GetMaxLun 0xfe
37 enum USBMSDMode {
38 USB_MSDM_CBW, /* Command Block. */
39 USB_MSDM_DATAOUT, /* Transfer data to device. */
40 USB_MSDM_DATAIN, /* Transfer data from device. */
41 USB_MSDM_CSW /* Command Status. */
44 struct usb_msd_csw {
45 uint32_t sig;
46 uint32_t tag;
47 uint32_t residue;
48 uint8_t status;
51 typedef struct {
52 USBDevice dev;
53 enum USBMSDMode mode;
54 uint32_t scsi_off;
55 uint32_t scsi_len;
56 uint32_t data_len;
57 struct usb_msd_csw csw;
58 SCSIRequest *req;
59 SCSIBus bus;
60 /* For async completion. */
61 USBPacket *packet;
62 /* usb-storage only */
63 BlockConf conf;
64 uint32_t removable;
65 SCSIDevice *scsi_dev;
66 } MSDState;
68 #define TYPE_USB_STORAGE "usb-storage-dev"
69 #define USB_STORAGE_DEV(obj) OBJECT_CHECK(MSDState, (obj), TYPE_USB_STORAGE)
71 struct usb_msd_cbw {
72 uint32_t sig;
73 uint32_t tag;
74 uint32_t data_len;
75 uint8_t flags;
76 uint8_t lun;
77 uint8_t cmd_len;
78 uint8_t cmd[16];
81 enum {
82 STR_MANUFACTURER = 1,
83 STR_PRODUCT,
84 STR_SERIALNUMBER,
85 STR_CONFIG_FULL,
86 STR_CONFIG_HIGH,
87 STR_CONFIG_SUPER,
90 static const USBDescStrings desc_strings = {
91 [STR_MANUFACTURER] = "QEMU",
92 [STR_PRODUCT] = "QEMU USB HARDDRIVE",
93 [STR_SERIALNUMBER] = "1",
94 [STR_CONFIG_FULL] = "Full speed config (usb 1.1)",
95 [STR_CONFIG_HIGH] = "High speed config (usb 2.0)",
96 [STR_CONFIG_SUPER] = "Super speed config (usb 3.0)",
99 static const USBDescIface desc_iface_full = {
100 .bInterfaceNumber = 0,
101 .bNumEndpoints = 2,
102 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
103 .bInterfaceSubClass = 0x06, /* SCSI */
104 .bInterfaceProtocol = 0x50, /* Bulk */
105 .eps = (USBDescEndpoint[]) {
107 .bEndpointAddress = USB_DIR_IN | 0x01,
108 .bmAttributes = USB_ENDPOINT_XFER_BULK,
109 .wMaxPacketSize = 64,
111 .bEndpointAddress = USB_DIR_OUT | 0x02,
112 .bmAttributes = USB_ENDPOINT_XFER_BULK,
113 .wMaxPacketSize = 64,
118 static const USBDescDevice desc_device_full = {
119 .bcdUSB = 0x0200,
120 .bMaxPacketSize0 = 8,
121 .bNumConfigurations = 1,
122 .confs = (USBDescConfig[]) {
124 .bNumInterfaces = 1,
125 .bConfigurationValue = 1,
126 .iConfiguration = STR_CONFIG_FULL,
127 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER,
128 .nif = 1,
129 .ifs = &desc_iface_full,
134 static const USBDescIface desc_iface_high = {
135 .bInterfaceNumber = 0,
136 .bNumEndpoints = 2,
137 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
138 .bInterfaceSubClass = 0x06, /* SCSI */
139 .bInterfaceProtocol = 0x50, /* Bulk */
140 .eps = (USBDescEndpoint[]) {
142 .bEndpointAddress = USB_DIR_IN | 0x01,
143 .bmAttributes = USB_ENDPOINT_XFER_BULK,
144 .wMaxPacketSize = 512,
146 .bEndpointAddress = USB_DIR_OUT | 0x02,
147 .bmAttributes = USB_ENDPOINT_XFER_BULK,
148 .wMaxPacketSize = 512,
153 static const USBDescDevice desc_device_high = {
154 .bcdUSB = 0x0200,
155 .bMaxPacketSize0 = 64,
156 .bNumConfigurations = 1,
157 .confs = (USBDescConfig[]) {
159 .bNumInterfaces = 1,
160 .bConfigurationValue = 1,
161 .iConfiguration = STR_CONFIG_HIGH,
162 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER,
163 .nif = 1,
164 .ifs = &desc_iface_high,
169 static const USBDescIface desc_iface_super = {
170 .bInterfaceNumber = 0,
171 .bNumEndpoints = 2,
172 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
173 .bInterfaceSubClass = 0x06, /* SCSI */
174 .bInterfaceProtocol = 0x50, /* Bulk */
175 .eps = (USBDescEndpoint[]) {
177 .bEndpointAddress = USB_DIR_IN | 0x01,
178 .bmAttributes = USB_ENDPOINT_XFER_BULK,
179 .wMaxPacketSize = 1024,
180 .bMaxBurst = 15,
182 .bEndpointAddress = USB_DIR_OUT | 0x02,
183 .bmAttributes = USB_ENDPOINT_XFER_BULK,
184 .wMaxPacketSize = 1024,
185 .bMaxBurst = 15,
190 static const USBDescDevice desc_device_super = {
191 .bcdUSB = 0x0300,
192 .bMaxPacketSize0 = 9,
193 .bNumConfigurations = 1,
194 .confs = (USBDescConfig[]) {
196 .bNumInterfaces = 1,
197 .bConfigurationValue = 1,
198 .iConfiguration = STR_CONFIG_SUPER,
199 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER,
200 .nif = 1,
201 .ifs = &desc_iface_super,
206 static const USBDesc desc = {
207 .id = {
208 .idVendor = 0x46f4, /* CRC16() of "QEMU" */
209 .idProduct = 0x0001,
210 .bcdDevice = 0,
211 .iManufacturer = STR_MANUFACTURER,
212 .iProduct = STR_PRODUCT,
213 .iSerialNumber = STR_SERIALNUMBER,
215 .full = &desc_device_full,
216 .high = &desc_device_high,
217 .super = &desc_device_super,
218 .str = desc_strings,
221 static void usb_msd_copy_data(MSDState *s, USBPacket *p)
223 uint32_t len;
224 len = p->iov.size - p->actual_length;
225 if (len > s->scsi_len)
226 len = s->scsi_len;
227 usb_packet_copy(p, scsi_req_get_buf(s->req) + s->scsi_off, len);
228 s->scsi_len -= len;
229 s->scsi_off += len;
230 s->data_len -= len;
231 if (s->scsi_len == 0 || s->data_len == 0) {
232 scsi_req_continue(s->req);
236 static void usb_msd_send_status(MSDState *s, USBPacket *p)
238 int len;
240 DPRINTF("Command status %d tag 0x%x, len %zd\n",
241 s->csw.status, le32_to_cpu(s->csw.tag), p->iov.size);
243 assert(s->csw.sig == cpu_to_le32(0x53425355));
244 len = MIN(sizeof(s->csw), p->iov.size);
245 usb_packet_copy(p, &s->csw, len);
246 memset(&s->csw, 0, sizeof(s->csw));
249 static void usb_msd_packet_complete(MSDState *s)
251 USBPacket *p = s->packet;
253 /* Set s->packet to NULL before calling usb_packet_complete
254 because another request may be issued before
255 usb_packet_complete returns. */
256 DPRINTF("Packet complete %p\n", p);
257 s->packet = NULL;
258 usb_packet_complete(&s->dev, p);
261 static void usb_msd_transfer_data(SCSIRequest *req, uint32_t len)
263 MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
264 USBPacket *p = s->packet;
266 assert((s->mode == USB_MSDM_DATAOUT) == (req->cmd.mode == SCSI_XFER_TO_DEV));
267 s->scsi_len = len;
268 s->scsi_off = 0;
269 if (p) {
270 usb_msd_copy_data(s, p);
271 p = s->packet;
272 if (p && p->actual_length == p->iov.size) {
273 p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */
274 usb_msd_packet_complete(s);
279 static void usb_msd_command_complete(SCSIRequest *req, uint32_t status, size_t resid)
281 MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
282 USBPacket *p = s->packet;
284 DPRINTF("Command complete %d tag 0x%x\n", status, req->tag);
286 s->csw.sig = cpu_to_le32(0x53425355);
287 s->csw.tag = cpu_to_le32(req->tag);
288 s->csw.residue = cpu_to_le32(s->data_len);
289 s->csw.status = status != 0;
291 if (s->packet) {
292 if (s->data_len == 0 && s->mode == USB_MSDM_DATAOUT) {
293 /* A deferred packet with no write data remaining must be
294 the status read packet. */
295 usb_msd_send_status(s, p);
296 s->mode = USB_MSDM_CBW;
297 } else if (s->mode == USB_MSDM_CSW) {
298 usb_msd_send_status(s, p);
299 s->mode = USB_MSDM_CBW;
300 } else {
301 if (s->data_len) {
302 int len = (p->iov.size - p->actual_length);
303 usb_packet_skip(p, len);
304 s->data_len -= len;
306 if (s->data_len == 0) {
307 s->mode = USB_MSDM_CSW;
310 p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */
311 usb_msd_packet_complete(s);
312 } else if (s->data_len == 0) {
313 s->mode = USB_MSDM_CSW;
315 scsi_req_unref(req);
316 s->req = NULL;
319 static void usb_msd_request_cancelled(SCSIRequest *req)
321 MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
323 if (req == s->req) {
324 scsi_req_unref(s->req);
325 s->req = NULL;
326 s->scsi_len = 0;
330 static void usb_msd_handle_reset(USBDevice *dev)
332 MSDState *s = (MSDState *)dev;
334 DPRINTF("Reset\n");
335 if (s->req) {
336 scsi_req_cancel(s->req);
338 assert(s->req == NULL);
340 if (s->packet) {
341 s->packet->status = USB_RET_STALL;
342 usb_msd_packet_complete(s);
345 s->mode = USB_MSDM_CBW;
348 static void usb_msd_handle_control(USBDevice *dev, USBPacket *p,
349 int request, int value, int index, int length, uint8_t *data)
351 MSDState *s = (MSDState *)dev;
352 SCSIDevice *scsi_dev;
353 int ret, maxlun;
355 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
356 if (ret >= 0) {
357 return;
360 switch (request) {
361 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
362 break;
363 /* Class specific requests. */
364 case ClassInterfaceOutRequest | MassStorageReset:
365 /* Reset state ready for the next CBW. */
366 s->mode = USB_MSDM_CBW;
367 break;
368 case ClassInterfaceRequest | GetMaxLun:
369 maxlun = 0;
370 for (;;) {
371 scsi_dev = scsi_device_find(&s->bus, 0, 0, maxlun+1);
372 if (scsi_dev == NULL) {
373 break;
375 if (scsi_dev->lun != maxlun+1) {
376 break;
378 maxlun++;
380 DPRINTF("MaxLun %d\n", maxlun);
381 data[0] = maxlun;
382 p->actual_length = 1;
383 break;
384 default:
385 p->status = USB_RET_STALL;
386 break;
390 static void usb_msd_cancel_io(USBDevice *dev, USBPacket *p)
392 MSDState *s = USB_STORAGE_DEV(dev);
394 assert(s->packet == p);
395 s->packet = NULL;
397 if (s->req) {
398 scsi_req_cancel(s->req);
402 static void usb_msd_handle_data(USBDevice *dev, USBPacket *p)
404 MSDState *s = (MSDState *)dev;
405 uint32_t tag;
406 struct usb_msd_cbw cbw;
407 uint8_t devep = p->ep->nr;
408 SCSIDevice *scsi_dev;
409 uint32_t len;
411 switch (p->pid) {
412 case USB_TOKEN_OUT:
413 if (devep != 2)
414 goto fail;
416 switch (s->mode) {
417 case USB_MSDM_CBW:
418 if (p->iov.size != 31) {
419 error_report("usb-msd: Bad CBW size");
420 goto fail;
422 usb_packet_copy(p, &cbw, 31);
423 if (le32_to_cpu(cbw.sig) != 0x43425355) {
424 error_report("usb-msd: Bad signature %08x",
425 le32_to_cpu(cbw.sig));
426 goto fail;
428 DPRINTF("Command on LUN %d\n", cbw.lun);
429 scsi_dev = scsi_device_find(&s->bus, 0, 0, cbw.lun);
430 if (scsi_dev == NULL) {
431 error_report("usb-msd: Bad LUN %d", cbw.lun);
432 goto fail;
434 tag = le32_to_cpu(cbw.tag);
435 s->data_len = le32_to_cpu(cbw.data_len);
436 if (s->data_len == 0) {
437 s->mode = USB_MSDM_CSW;
438 } else if (cbw.flags & 0x80) {
439 s->mode = USB_MSDM_DATAIN;
440 } else {
441 s->mode = USB_MSDM_DATAOUT;
443 DPRINTF("Command tag 0x%x flags %08x len %d data %d\n",
444 tag, cbw.flags, cbw.cmd_len, s->data_len);
445 assert(le32_to_cpu(s->csw.residue) == 0);
446 s->scsi_len = 0;
447 s->req = scsi_req_new(scsi_dev, tag, cbw.lun, cbw.cmd, NULL);
448 #ifdef DEBUG_MSD
449 scsi_req_print(s->req);
450 #endif
451 len = scsi_req_enqueue(s->req);
452 if (len) {
453 scsi_req_continue(s->req);
455 break;
457 case USB_MSDM_DATAOUT:
458 DPRINTF("Data out %zd/%d\n", p->iov.size, s->data_len);
459 if (p->iov.size > s->data_len) {
460 goto fail;
463 if (s->scsi_len) {
464 usb_msd_copy_data(s, p);
466 if (le32_to_cpu(s->csw.residue)) {
467 int len = p->iov.size - p->actual_length;
468 if (len) {
469 usb_packet_skip(p, len);
470 s->data_len -= len;
471 if (s->data_len == 0) {
472 s->mode = USB_MSDM_CSW;
476 if (p->actual_length < p->iov.size) {
477 DPRINTF("Deferring packet %p [wait data-out]\n", p);
478 s->packet = p;
479 p->status = USB_RET_ASYNC;
481 break;
483 default:
484 DPRINTF("Unexpected write (len %zd)\n", p->iov.size);
485 goto fail;
487 break;
489 case USB_TOKEN_IN:
490 if (devep != 1)
491 goto fail;
493 switch (s->mode) {
494 case USB_MSDM_DATAOUT:
495 if (s->data_len != 0 || p->iov.size < 13) {
496 goto fail;
498 /* Waiting for SCSI write to complete. */
499 s->packet = p;
500 p->status = USB_RET_ASYNC;
501 break;
503 case USB_MSDM_CSW:
504 if (p->iov.size < 13) {
505 goto fail;
508 if (s->req) {
509 /* still in flight */
510 DPRINTF("Deferring packet %p [wait status]\n", p);
511 s->packet = p;
512 p->status = USB_RET_ASYNC;
513 } else {
514 usb_msd_send_status(s, p);
515 s->mode = USB_MSDM_CBW;
517 break;
519 case USB_MSDM_DATAIN:
520 DPRINTF("Data in %zd/%d, scsi_len %d\n",
521 p->iov.size, s->data_len, s->scsi_len);
522 if (s->scsi_len) {
523 usb_msd_copy_data(s, p);
525 if (le32_to_cpu(s->csw.residue)) {
526 int len = p->iov.size - p->actual_length;
527 if (len) {
528 usb_packet_skip(p, len);
529 s->data_len -= len;
530 if (s->data_len == 0) {
531 s->mode = USB_MSDM_CSW;
535 if (p->actual_length < p->iov.size) {
536 DPRINTF("Deferring packet %p [wait data-in]\n", p);
537 s->packet = p;
538 p->status = USB_RET_ASYNC;
540 break;
542 default:
543 DPRINTF("Unexpected read (len %zd)\n", p->iov.size);
544 goto fail;
546 break;
548 default:
549 DPRINTF("Bad token\n");
550 fail:
551 p->status = USB_RET_STALL;
552 break;
556 static void usb_msd_password_cb(void *opaque, int err)
558 MSDState *s = opaque;
559 Error *local_err = NULL;
561 if (!err) {
562 usb_device_attach(&s->dev, &local_err);
565 if (local_err) {
566 error_report_err(local_err);
567 qdev_unplug(&s->dev.qdev, NULL);
571 static void *usb_msd_load_request(QEMUFile *f, SCSIRequest *req)
573 MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
575 /* nothing to load, just store req in our state struct */
576 assert(s->req == NULL);
577 scsi_req_ref(req);
578 s->req = req;
579 return NULL;
582 static const struct SCSIBusInfo usb_msd_scsi_info_storage = {
583 .tcq = false,
584 .max_target = 0,
585 .max_lun = 0,
587 .transfer_data = usb_msd_transfer_data,
588 .complete = usb_msd_command_complete,
589 .cancel = usb_msd_request_cancelled,
590 .load_request = usb_msd_load_request,
593 static const struct SCSIBusInfo usb_msd_scsi_info_bot = {
594 .tcq = false,
595 .max_target = 0,
596 .max_lun = 15,
598 .transfer_data = usb_msd_transfer_data,
599 .complete = usb_msd_command_complete,
600 .cancel = usb_msd_request_cancelled,
601 .load_request = usb_msd_load_request,
604 static void usb_msd_realize_storage(USBDevice *dev, Error **errp)
606 MSDState *s = USB_STORAGE_DEV(dev);
607 BlockBackend *blk = s->conf.blk;
608 SCSIDevice *scsi_dev;
609 Error *err = NULL;
611 if (!blk) {
612 error_setg(errp, "drive property not set");
613 return;
616 if (blk_bs(blk)) {
617 bdrv_add_key(blk_bs(blk), NULL, &err);
618 if (err) {
619 if (monitor_cur_is_qmp()) {
620 error_propagate(errp, err);
621 return;
623 error_free(err);
624 err = NULL;
625 if (cur_mon) {
626 monitor_read_bdrv_key_start(cur_mon, blk_bs(blk),
627 usb_msd_password_cb, s);
628 s->dev.auto_attach = 0;
629 } else {
630 autostart = 0;
635 blkconf_serial(&s->conf, &dev->serial);
636 blkconf_blocksizes(&s->conf);
639 * Hack alert: this pretends to be a block device, but it's really
640 * a SCSI bus that can serve only a single device, which it
641 * creates automatically. But first it needs to detach from its
642 * blockdev, or else scsi_bus_legacy_add_drive() dies when it
643 * attaches again.
645 * The hack is probably a bad idea.
647 blk_detach_dev(blk, &s->dev.qdev);
648 s->conf.blk = NULL;
650 usb_desc_create_serial(dev);
651 usb_desc_init(dev);
652 scsi_bus_new(&s->bus, sizeof(s->bus), DEVICE(dev),
653 &usb_msd_scsi_info_storage, NULL);
654 scsi_dev = scsi_bus_legacy_add_drive(&s->bus, blk, 0, !!s->removable,
655 s->conf.bootindex, dev->serial,
656 &err);
657 if (!scsi_dev) {
658 error_propagate(errp, err);
659 return;
661 usb_msd_handle_reset(dev);
662 s->scsi_dev = scsi_dev;
665 static void usb_msd_realize_bot(USBDevice *dev, Error **errp)
667 MSDState *s = USB_STORAGE_DEV(dev);
669 usb_desc_create_serial(dev);
670 usb_desc_init(dev);
671 scsi_bus_new(&s->bus, sizeof(s->bus), DEVICE(dev),
672 &usb_msd_scsi_info_bot, NULL);
673 usb_msd_handle_reset(dev);
676 static USBDevice *usb_msd_init(USBBus *bus, const char *filename)
678 static int nr=0;
679 Error *err = NULL;
680 char id[8];
681 QemuOpts *opts;
682 DriveInfo *dinfo;
683 USBDevice *dev;
684 const char *p1;
685 char fmt[32];
687 /* parse -usbdevice disk: syntax into drive opts */
688 do {
689 snprintf(id, sizeof(id), "usb%d", nr++);
690 opts = qemu_opts_create(qemu_find_opts("drive"), id, 1, NULL);
691 } while (!opts);
693 p1 = strchr(filename, ':');
694 if (p1++) {
695 const char *p2;
697 if (strstart(filename, "format=", &p2)) {
698 int len = MIN(p1 - p2, sizeof(fmt));
699 pstrcpy(fmt, len, p2);
700 qemu_opt_set(opts, "format", fmt, &error_abort);
701 } else if (*filename != ':') {
702 error_report("unrecognized USB mass-storage option %s", filename);
703 return NULL;
705 filename = p1;
707 if (!*filename) {
708 error_report("block device specification needed");
709 return NULL;
711 qemu_opt_set(opts, "file", filename, &error_abort);
712 qemu_opt_set(opts, "if", "none", &error_abort);
714 /* create host drive */
715 dinfo = drive_new(opts, 0);
716 if (!dinfo) {
717 qemu_opts_del(opts);
718 return NULL;
721 /* create guest device */
722 dev = usb_create(bus, "usb-storage");
723 qdev_prop_set_drive(&dev->qdev, "drive", blk_by_legacy_dinfo(dinfo),
724 &err);
725 if (err) {
726 error_report_err(err);
727 object_unparent(OBJECT(dev));
728 return NULL;
730 return dev;
733 static const VMStateDescription vmstate_usb_msd = {
734 .name = "usb-storage",
735 .version_id = 1,
736 .minimum_version_id = 1,
737 .fields = (VMStateField[]) {
738 VMSTATE_USB_DEVICE(dev, MSDState),
739 VMSTATE_UINT32(mode, MSDState),
740 VMSTATE_UINT32(scsi_len, MSDState),
741 VMSTATE_UINT32(scsi_off, MSDState),
742 VMSTATE_UINT32(data_len, MSDState),
743 VMSTATE_UINT32(csw.sig, MSDState),
744 VMSTATE_UINT32(csw.tag, MSDState),
745 VMSTATE_UINT32(csw.residue, MSDState),
746 VMSTATE_UINT8(csw.status, MSDState),
747 VMSTATE_END_OF_LIST()
751 static Property msd_properties[] = {
752 DEFINE_BLOCK_PROPERTIES(MSDState, conf),
753 DEFINE_PROP_BIT("removable", MSDState, removable, 0, false),
754 DEFINE_PROP_END_OF_LIST(),
757 static void usb_msd_class_initfn_common(ObjectClass *klass, void *data)
759 DeviceClass *dc = DEVICE_CLASS(klass);
760 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
762 uc->product_desc = "QEMU USB MSD";
763 uc->usb_desc = &desc;
764 uc->cancel_packet = usb_msd_cancel_io;
765 uc->handle_attach = usb_desc_attach;
766 uc->handle_reset = usb_msd_handle_reset;
767 uc->handle_control = usb_msd_handle_control;
768 uc->handle_data = usb_msd_handle_data;
769 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
770 dc->fw_name = "storage";
771 dc->vmsd = &vmstate_usb_msd;
774 static void usb_msd_class_initfn_storage(ObjectClass *klass, void *data)
776 DeviceClass *dc = DEVICE_CLASS(klass);
777 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
779 uc->realize = usb_msd_realize_storage;
780 dc->props = msd_properties;
783 static void usb_msd_get_bootindex(Object *obj, Visitor *v, void *opaque,
784 const char *name, Error **errp)
786 USBDevice *dev = USB_DEVICE(obj);
787 MSDState *s = USB_STORAGE_DEV(dev);
789 visit_type_int32(v, &s->conf.bootindex, name, errp);
792 static void usb_msd_set_bootindex(Object *obj, Visitor *v, void *opaque,
793 const char *name, Error **errp)
795 USBDevice *dev = USB_DEVICE(obj);
796 MSDState *s = USB_STORAGE_DEV(dev);
797 int32_t boot_index;
798 Error *local_err = NULL;
800 visit_type_int32(v, &boot_index, name, &local_err);
801 if (local_err) {
802 goto out;
804 /* check whether bootindex is present in fw_boot_order list */
805 check_boot_index(boot_index, &local_err);
806 if (local_err) {
807 goto out;
809 /* change bootindex to a new one */
810 s->conf.bootindex = boot_index;
812 if (s->scsi_dev) {
813 object_property_set_int(OBJECT(s->scsi_dev), boot_index, "bootindex",
814 &error_abort);
817 out:
818 if (local_err) {
819 error_propagate(errp, local_err);
823 static const TypeInfo usb_storage_dev_type_info = {
824 .name = TYPE_USB_STORAGE,
825 .parent = TYPE_USB_DEVICE,
826 .instance_size = sizeof(MSDState),
827 .abstract = true,
828 .class_init = usb_msd_class_initfn_common,
831 static void usb_msd_instance_init(Object *obj)
833 object_property_add(obj, "bootindex", "int32",
834 usb_msd_get_bootindex,
835 usb_msd_set_bootindex, NULL, NULL, NULL);
836 object_property_set_int(obj, -1, "bootindex", NULL);
839 static void usb_msd_class_initfn_bot(ObjectClass *klass, void *data)
841 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
842 DeviceClass *dc = DEVICE_CLASS(klass);
844 uc->realize = usb_msd_realize_bot;
845 dc->hotpluggable = false;
848 static const TypeInfo msd_info = {
849 .name = "usb-storage",
850 .parent = TYPE_USB_STORAGE,
851 .class_init = usb_msd_class_initfn_storage,
852 .instance_init = usb_msd_instance_init,
855 static const TypeInfo bot_info = {
856 .name = "usb-bot",
857 .parent = TYPE_USB_STORAGE,
858 .class_init = usb_msd_class_initfn_bot,
861 static void usb_msd_register_types(void)
863 type_register_static(&usb_storage_dev_type_info);
864 type_register_static(&msd_info);
865 type_register_static(&bot_info);
866 usb_legacy_register("usb-storage", "disk", usb_msd_init);
869 type_init(usb_msd_register_types)