2
0

dev-storage-classic.c 4.0 KB

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