dev-storage-classic.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 "sysemu/sysemu.h"
  16. #include "sysemu/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. if (!blkconf_blocksizes(&s->conf, errp)) {
  36. return;
  37. }
  38. if (!blkconf_apply_backend_options(&s->conf, !blk_supports_write_perm(blk),
  39. true, errp)) {
  40. return;
  41. }
  42. /*
  43. * Hack alert: this pretends to be a block device, but it's really
  44. * a SCSI bus that can serve only a single device, which it
  45. * creates automatically. But first it needs to detach from its
  46. * blockdev, or else scsi_bus_legacy_add_drive() dies when it
  47. * attaches again. We also need to take another reference so that
  48. * blk_detach_dev() doesn't free blk while we still need it.
  49. *
  50. * The hack is probably a bad idea.
  51. */
  52. blk_ref(blk);
  53. blk_detach_dev(blk, DEVICE(s));
  54. s->conf.blk = NULL;
  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.bootindex, s->conf.share_rw,
  62. s->conf.rerror, s->conf.werror,
  63. dev->serial,
  64. errp);
  65. blk_unref(blk);
  66. if (!scsi_dev) {
  67. return;
  68. }
  69. usb_msd_handle_reset(dev);
  70. s->scsi_dev = scsi_dev;
  71. }
  72. static Property msd_properties[] = {
  73. DEFINE_BLOCK_PROPERTIES(MSDState, conf),
  74. DEFINE_BLOCK_ERROR_PROPERTIES(MSDState, conf),
  75. DEFINE_PROP_BOOL("removable", MSDState, removable, false),
  76. DEFINE_PROP_BOOL("commandlog", MSDState, commandlog, false),
  77. DEFINE_PROP_END_OF_LIST(),
  78. };
  79. static void usb_msd_class_storage_initfn(ObjectClass *klass, void *data)
  80. {
  81. DeviceClass *dc = DEVICE_CLASS(klass);
  82. USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
  83. uc->realize = usb_msd_storage_realize;
  84. device_class_set_props(dc, msd_properties);
  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. visit_type_int32(v, name, &s->conf.bootindex, errp);
  92. }
  93. static void usb_msd_set_bootindex(Object *obj, Visitor *v, const char *name,
  94. void *opaque, Error **errp)
  95. {
  96. USBDevice *dev = USB_DEVICE(obj);
  97. MSDState *s = USB_STORAGE_DEV(dev);
  98. int32_t boot_index;
  99. Error *local_err = NULL;
  100. if (!visit_type_int32(v, name, &boot_index, errp)) {
  101. return;
  102. }
  103. /* check whether bootindex is present in fw_boot_order list */
  104. check_boot_index(boot_index, &local_err);
  105. if (local_err) {
  106. goto out;
  107. }
  108. /* change bootindex to a new one */
  109. s->conf.bootindex = boot_index;
  110. if (s->scsi_dev) {
  111. object_property_set_int(OBJECT(s->scsi_dev), "bootindex", boot_index,
  112. &error_abort);
  113. }
  114. out:
  115. error_propagate(errp, local_err);
  116. }
  117. static void usb_msd_instance_init(Object *obj)
  118. {
  119. object_property_add(obj, "bootindex", "int32",
  120. usb_msd_get_bootindex,
  121. usb_msd_set_bootindex, NULL, NULL);
  122. object_property_set_int(obj, "bootindex", -1, NULL);
  123. }
  124. static const TypeInfo msd_info = {
  125. .name = "usb-storage",
  126. .parent = TYPE_USB_STORAGE,
  127. .class_init = usb_msd_class_storage_initfn,
  128. .instance_init = usb_msd_instance_init,
  129. };
  130. static void register_types(void)
  131. {
  132. type_register_static(&msd_info);
  133. }
  134. type_init(register_types)