1 /* 2 * USB Mass Storage Device emulation 3 * 4 * Copyright (c) 2006 CodeSourcery. 5 * Written by Paul Brook 6 * 7 * This code is licensed under the LGPL. 8 */ 9 10 #include "qemu/osdep.h" 11 #include "qapi/error.h" 12 #include "qapi/visitor.h" 13 #include "hw/usb.h" 14 #include "hw/usb/desc.h" 15 #include "hw/usb/msd.h" 16 #include "system/system.h" 17 #include "system/block-backend.h" 18 19 static const struct SCSIBusInfo usb_msd_scsi_info_storage = { 20 .tcq = false, 21 .max_target = 0, 22 .max_lun = 0, 23 24 .transfer_data = usb_msd_transfer_data, 25 .complete = usb_msd_command_complete, 26 .cancel = usb_msd_request_cancelled, 27 .load_request = usb_msd_load_request, 28 }; 29 30 static void usb_msd_storage_realize(USBDevice *dev, Error **errp) 31 { 32 MSDState *s = USB_STORAGE_DEV(dev); 33 BlockBackend *blk = s->conf.blk; 34 SCSIDevice *scsi_dev; 35 36 if (!blk) { 37 error_setg(errp, "drive property not set"); 38 return; 39 } 40 41 /* 42 * Hack alert: this pretends to be a block device, but it's really 43 * a SCSI bus that can serve only a single device, which it 44 * creates automatically. But first it needs to detach from its 45 * blockdev, or else scsi_bus_legacy_add_drive() dies when it 46 * attaches again. We also need to take another reference so that 47 * blk_detach_dev() doesn't free blk while we still need it. 48 * 49 * The hack is probably a bad idea. 50 */ 51 blk_ref(blk); 52 blk_detach_dev(blk, DEVICE(s)); 53 s->conf.blk = NULL; 54 55 usb_desc_create_serial(dev); 56 usb_desc_init(dev); 57 dev->flags |= (1 << USB_DEV_FLAG_IS_SCSI_STORAGE); 58 scsi_bus_init(&s->bus, sizeof(s->bus), DEVICE(dev), 59 &usb_msd_scsi_info_storage); 60 scsi_dev = scsi_bus_legacy_add_drive(&s->bus, blk, 0, !!s->removable, 61 &s->conf, dev->serial, errp); 62 blk_unref(blk); 63 if (!scsi_dev) { 64 return; 65 } 66 usb_msd_handle_reset(dev); 67 s->scsi_dev = scsi_dev; 68 } 69 70 static const Property msd_properties[] = { 71 DEFINE_BLOCK_PROPERTIES(MSDState, conf), 72 DEFINE_BLOCK_ERROR_PROPERTIES(MSDState, conf), 73 DEFINE_PROP_BOOL("removable", MSDState, removable, false), 74 DEFINE_PROP_BOOL("commandlog", MSDState, commandlog, false), 75 }; 76 77 static void usb_msd_class_storage_initfn(ObjectClass *klass, void *data) 78 { 79 DeviceClass *dc = DEVICE_CLASS(klass); 80 USBDeviceClass *uc = USB_DEVICE_CLASS(klass); 81 82 uc->realize = usb_msd_storage_realize; 83 device_class_set_props(dc, msd_properties); 84 } 85 86 static void usb_msd_get_bootindex(Object *obj, Visitor *v, const char *name, 87 void *opaque, Error **errp) 88 { 89 USBDevice *dev = USB_DEVICE(obj); 90 MSDState *s = USB_STORAGE_DEV(dev); 91 92 visit_type_int32(v, name, &s->conf.bootindex, errp); 93 } 94 95 static void usb_msd_set_bootindex(Object *obj, Visitor *v, const char *name, 96 void *opaque, Error **errp) 97 { 98 USBDevice *dev = USB_DEVICE(obj); 99 MSDState *s = USB_STORAGE_DEV(dev); 100 int32_t boot_index; 101 Error *local_err = NULL; 102 103 if (!visit_type_int32(v, name, &boot_index, errp)) { 104 return; 105 } 106 /* check whether bootindex is present in fw_boot_order list */ 107 check_boot_index(boot_index, &local_err); 108 if (local_err) { 109 goto out; 110 } 111 /* change bootindex to a new one */ 112 s->conf.bootindex = boot_index; 113 114 if (s->scsi_dev) { 115 object_property_set_int(OBJECT(s->scsi_dev), "bootindex", boot_index, 116 &error_abort); 117 } 118 119 out: 120 error_propagate(errp, local_err); 121 } 122 123 static void usb_msd_instance_init(Object *obj) 124 { 125 object_property_add(obj, "bootindex", "int32", 126 usb_msd_get_bootindex, 127 usb_msd_set_bootindex, NULL, NULL); 128 object_property_set_int(obj, "bootindex", -1, NULL); 129 } 130 131 static const TypeInfo msd_info = { 132 .name = "usb-storage", 133 .parent = TYPE_USB_STORAGE, 134 .class_init = usb_msd_class_storage_initfn, 135 .instance_init = usb_msd_instance_init, 136 }; 137 138 static void register_types(void) 139 { 140 type_register_static(&msd_info); 141 } 142 143 type_init(register_types) 144