2007-09-16 21:08:06 +00:00
|
|
|
/*
|
2006-05-25 23:58:51 +00:00
|
|
|
* USB Mass Storage Device emulation
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006 CodeSourcery.
|
|
|
|
* Written by Paul Brook
|
|
|
|
*
|
2011-06-26 02:21:35 +00:00
|
|
|
* This code is licensed under the LGPL.
|
2006-05-25 23:58:51 +00:00
|
|
|
*/
|
|
|
|
|
2016-01-26 18:17:12 +00:00
|
|
|
#include "qemu/osdep.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 08:01:28 +00:00
|
|
|
#include "qapi/error.h"
|
2015-03-17 17:29:20 +00:00
|
|
|
#include "qemu/error-report.h"
|
2019-05-23 14:35:07 +00:00
|
|
|
#include "qemu/module.h"
|
2012-12-17 17:20:00 +00:00
|
|
|
#include "qemu/option.h"
|
|
|
|
#include "qemu/config-file.h"
|
2012-03-07 13:55:18 +00:00
|
|
|
#include "hw/usb.h"
|
2018-05-03 19:50:48 +00:00
|
|
|
#include "desc.h"
|
2019-08-12 05:23:51 +00:00
|
|
|
#include "hw/qdev-properties.h"
|
2013-02-05 16:06:20 +00:00
|
|
|
#include "hw/scsi/scsi.h"
|
2019-08-12 05:23:45 +00:00
|
|
|
#include "migration/vmstate.h"
|
2012-12-17 17:20:04 +00:00
|
|
|
#include "sysemu/sysemu.h"
|
2014-10-07 11:59:13 +00:00
|
|
|
#include "sysemu/block-backend.h"
|
2014-10-07 08:00:33 +00:00
|
|
|
#include "qapi/visitor.h"
|
2016-03-20 17:16:19 +00:00
|
|
|
#include "qemu/cutils.h"
|
2006-05-25 23:58:51 +00:00
|
|
|
|
|
|
|
//#define DEBUG_MSD
|
|
|
|
|
|
|
|
#ifdef DEBUG_MSD
|
2009-05-13 17:53:17 +00:00
|
|
|
#define DPRINTF(fmt, ...) \
|
|
|
|
do { printf("usb-msd: " fmt , ## __VA_ARGS__); } while (0)
|
2006-05-25 23:58:51 +00:00
|
|
|
#else
|
2009-05-13 17:53:17 +00:00
|
|
|
#define DPRINTF(fmt, ...) do {} while(0)
|
2006-05-25 23:58:51 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* USB requests. */
|
|
|
|
#define MassStorageReset 0xff
|
|
|
|
#define GetMaxLun 0xfe
|
|
|
|
|
|
|
|
enum USBMSDMode {
|
|
|
|
USB_MSDM_CBW, /* Command Block. */
|
2011-04-13 09:45:31 +00:00
|
|
|
USB_MSDM_DATAOUT, /* Transfer data to device. */
|
2006-05-25 23:58:51 +00:00
|
|
|
USB_MSDM_DATAIN, /* Transfer data from device. */
|
|
|
|
USB_MSDM_CSW /* Command Status. */
|
|
|
|
};
|
|
|
|
|
2011-11-21 10:29:27 +00:00
|
|
|
struct usb_msd_csw {
|
|
|
|
uint32_t sig;
|
|
|
|
uint32_t tag;
|
|
|
|
uint32_t residue;
|
|
|
|
uint8_t status;
|
|
|
|
};
|
|
|
|
|
2006-05-25 23:58:51 +00:00
|
|
|
typedef struct {
|
|
|
|
USBDevice dev;
|
|
|
|
enum USBMSDMode mode;
|
2012-05-16 13:03:40 +00:00
|
|
|
uint32_t scsi_off;
|
2006-08-29 04:52:16 +00:00
|
|
|
uint32_t scsi_len;
|
2006-05-25 23:58:51 +00:00
|
|
|
uint32_t data_len;
|
2011-11-21 10:29:27 +00:00
|
|
|
struct usb_msd_csw csw;
|
2011-04-18 10:35:39 +00:00
|
|
|
SCSIRequest *req;
|
2009-09-16 20:25:28 +00:00
|
|
|
SCSIBus bus;
|
2013-01-14 14:29:44 +00:00
|
|
|
/* For async completion. */
|
|
|
|
USBPacket *packet;
|
|
|
|
/* usb-storage only */
|
block: add topology qdev properties
Add three new qdev properties to export block topology information to
the guest. This is needed to get optimal I/O alignment for RAID arrays
or SSDs.
The options are:
- physical_block_size to specify the physical block size of the device,
this is going to increase from 512 bytes to 4096 kilobytes for many
modern storage devices
- min_io_size to specify the minimal I/O size without performance impact,
this is typically set to the RAID chunk size for arrays.
- opt_io_size to specify the optimal sustained I/O size, this is
typically the RAID stripe width for arrays.
I decided to not auto-probe these values from blkid which might easily
be possible as I don't know how to deal with these issues on migration.
Note that we specificly only set the physical_block_size, and not the
logial one which is the unit all I/O is described in. The reason for
that is that IDE does not support increasing the logical block size and
at last for now I want to stick to one meachnisms in queue and allow
for easy switching of transports for a given backing image which would
not be possible if scsi and virtio use real 4k sectors, while ide only
uses the physical block exponent.
To make this more common for the different block drivers introduce a
new BlockConf structure holding all common block properties and a
DEFINE_BLOCK_PROPERTIES macro to add them all together, mirroring
what is done for network drivers. Also switch over all block drivers
to use it, except for the floppy driver which has weird driveA/driveB
properties and probably won't require any advanced block options ever.
Example usage for a virtio device with 4k physical block size and
8k optimal I/O size:
-drive file=scratch.img,media=disk,cache=none,id=scratch \
-device virtio-blk-pci,drive=scratch,physical_block_size=4096,opt_io_size=8192
aliguori: updated patch to take into account BLOCK events
Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
2010-02-10 22:37:09 +00:00
|
|
|
BlockConf conf;
|
2011-01-24 15:35:00 +00:00
|
|
|
uint32_t removable;
|
2014-10-07 08:00:33 +00:00
|
|
|
SCSIDevice *scsi_dev;
|
2006-05-25 23:58:51 +00:00
|
|
|
} MSDState;
|
|
|
|
|
2015-05-06 12:55:32 +00:00
|
|
|
#define TYPE_USB_STORAGE "usb-storage-dev"
|
|
|
|
#define USB_STORAGE_DEV(obj) OBJECT_CHECK(MSDState, (obj), TYPE_USB_STORAGE)
|
|
|
|
|
2006-08-29 04:52:16 +00:00
|
|
|
struct usb_msd_cbw {
|
|
|
|
uint32_t sig;
|
|
|
|
uint32_t tag;
|
|
|
|
uint32_t data_len;
|
|
|
|
uint8_t flags;
|
|
|
|
uint8_t lun;
|
|
|
|
uint8_t cmd_len;
|
|
|
|
uint8_t cmd[16];
|
|
|
|
};
|
|
|
|
|
2010-11-17 10:05:41 +00:00
|
|
|
enum {
|
|
|
|
STR_MANUFACTURER = 1,
|
|
|
|
STR_PRODUCT,
|
|
|
|
STR_SERIALNUMBER,
|
2010-12-03 16:12:49 +00:00
|
|
|
STR_CONFIG_FULL,
|
|
|
|
STR_CONFIG_HIGH,
|
2012-08-28 15:29:15 +00:00
|
|
|
STR_CONFIG_SUPER,
|
2006-05-25 23:58:51 +00:00
|
|
|
};
|
|
|
|
|
2010-11-17 10:05:41 +00:00
|
|
|
static const USBDescStrings desc_strings = {
|
2012-05-30 03:35:51 +00:00
|
|
|
[STR_MANUFACTURER] = "QEMU",
|
2010-11-17 10:05:41 +00:00
|
|
|
[STR_PRODUCT] = "QEMU USB HARDDRIVE",
|
|
|
|
[STR_SERIALNUMBER] = "1",
|
2010-12-03 16:12:49 +00:00
|
|
|
[STR_CONFIG_FULL] = "Full speed config (usb 1.1)",
|
|
|
|
[STR_CONFIG_HIGH] = "High speed config (usb 2.0)",
|
2012-08-28 15:29:15 +00:00
|
|
|
[STR_CONFIG_SUPER] = "Super speed config (usb 3.0)",
|
2010-11-17 10:05:41 +00:00
|
|
|
};
|
|
|
|
|
2010-12-03 16:12:49 +00:00
|
|
|
static const USBDescIface desc_iface_full = {
|
2010-11-17 10:05:41 +00:00
|
|
|
.bInterfaceNumber = 0,
|
|
|
|
.bNumEndpoints = 2,
|
|
|
|
.bInterfaceClass = USB_CLASS_MASS_STORAGE,
|
|
|
|
.bInterfaceSubClass = 0x06, /* SCSI */
|
|
|
|
.bInterfaceProtocol = 0x50, /* Bulk */
|
|
|
|
.eps = (USBDescEndpoint[]) {
|
|
|
|
{
|
|
|
|
.bEndpointAddress = USB_DIR_IN | 0x01,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
|
.wMaxPacketSize = 64,
|
|
|
|
},{
|
|
|
|
.bEndpointAddress = USB_DIR_OUT | 0x02,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
|
.wMaxPacketSize = 64,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-12-03 16:12:49 +00:00
|
|
|
static const USBDescDevice desc_device_full = {
|
|
|
|
.bcdUSB = 0x0200,
|
2010-11-17 10:05:41 +00:00
|
|
|
.bMaxPacketSize0 = 8,
|
|
|
|
.bNumConfigurations = 1,
|
|
|
|
.confs = (USBDescConfig[]) {
|
|
|
|
{
|
|
|
|
.bNumInterfaces = 1,
|
|
|
|
.bConfigurationValue = 1,
|
2010-12-03 16:12:49 +00:00
|
|
|
.iConfiguration = STR_CONFIG_FULL,
|
2013-12-16 07:42:49 +00:00
|
|
|
.bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER,
|
2011-04-03 05:33:19 +00:00
|
|
|
.nif = 1,
|
2010-12-03 16:12:49 +00:00
|
|
|
.ifs = &desc_iface_full,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const USBDescIface desc_iface_high = {
|
|
|
|
.bInterfaceNumber = 0,
|
|
|
|
.bNumEndpoints = 2,
|
|
|
|
.bInterfaceClass = USB_CLASS_MASS_STORAGE,
|
|
|
|
.bInterfaceSubClass = 0x06, /* SCSI */
|
|
|
|
.bInterfaceProtocol = 0x50, /* Bulk */
|
|
|
|
.eps = (USBDescEndpoint[]) {
|
|
|
|
{
|
|
|
|
.bEndpointAddress = USB_DIR_IN | 0x01,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
|
.wMaxPacketSize = 512,
|
|
|
|
},{
|
|
|
|
.bEndpointAddress = USB_DIR_OUT | 0x02,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
|
.wMaxPacketSize = 512,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const USBDescDevice desc_device_high = {
|
|
|
|
.bcdUSB = 0x0200,
|
|
|
|
.bMaxPacketSize0 = 64,
|
|
|
|
.bNumConfigurations = 1,
|
|
|
|
.confs = (USBDescConfig[]) {
|
|
|
|
{
|
|
|
|
.bNumInterfaces = 1,
|
|
|
|
.bConfigurationValue = 1,
|
|
|
|
.iConfiguration = STR_CONFIG_HIGH,
|
2013-12-16 07:42:49 +00:00
|
|
|
.bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER,
|
2011-04-03 05:33:19 +00:00
|
|
|
.nif = 1,
|
2010-12-03 16:12:49 +00:00
|
|
|
.ifs = &desc_iface_high,
|
2010-11-17 10:05:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2012-08-28 15:29:15 +00:00
|
|
|
static const USBDescIface desc_iface_super = {
|
|
|
|
.bInterfaceNumber = 0,
|
|
|
|
.bNumEndpoints = 2,
|
|
|
|
.bInterfaceClass = USB_CLASS_MASS_STORAGE,
|
|
|
|
.bInterfaceSubClass = 0x06, /* SCSI */
|
|
|
|
.bInterfaceProtocol = 0x50, /* Bulk */
|
|
|
|
.eps = (USBDescEndpoint[]) {
|
|
|
|
{
|
|
|
|
.bEndpointAddress = USB_DIR_IN | 0x01,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
|
.wMaxPacketSize = 1024,
|
|
|
|
.bMaxBurst = 15,
|
|
|
|
},{
|
|
|
|
.bEndpointAddress = USB_DIR_OUT | 0x02,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
|
.wMaxPacketSize = 1024,
|
|
|
|
.bMaxBurst = 15,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const USBDescDevice desc_device_super = {
|
|
|
|
.bcdUSB = 0x0300,
|
|
|
|
.bMaxPacketSize0 = 9,
|
|
|
|
.bNumConfigurations = 1,
|
|
|
|
.confs = (USBDescConfig[]) {
|
|
|
|
{
|
|
|
|
.bNumInterfaces = 1,
|
|
|
|
.bConfigurationValue = 1,
|
|
|
|
.iConfiguration = STR_CONFIG_SUPER,
|
2013-12-16 07:42:49 +00:00
|
|
|
.bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER,
|
2012-08-28 15:29:15 +00:00
|
|
|
.nif = 1,
|
|
|
|
.ifs = &desc_iface_super,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2010-11-17 10:05:41 +00:00
|
|
|
static const USBDesc desc = {
|
|
|
|
.id = {
|
2011-09-15 03:25:47 +00:00
|
|
|
.idVendor = 0x46f4, /* CRC16() of "QEMU" */
|
|
|
|
.idProduct = 0x0001,
|
2010-11-17 10:05:41 +00:00
|
|
|
.bcdDevice = 0,
|
|
|
|
.iManufacturer = STR_MANUFACTURER,
|
|
|
|
.iProduct = STR_PRODUCT,
|
|
|
|
.iSerialNumber = STR_SERIALNUMBER,
|
|
|
|
},
|
2012-08-28 15:29:15 +00:00
|
|
|
.full = &desc_device_full,
|
|
|
|
.high = &desc_device_high,
|
|
|
|
.super = &desc_device_super,
|
|
|
|
.str = desc_strings,
|
2006-05-25 23:58:51 +00:00
|
|
|
};
|
|
|
|
|
2011-07-13 10:32:06 +00:00
|
|
|
static void usb_msd_copy_data(MSDState *s, USBPacket *p)
|
2006-08-29 04:52:16 +00:00
|
|
|
{
|
|
|
|
uint32_t len;
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 16:15:01 +00:00
|
|
|
len = p->iov.size - p->actual_length;
|
2006-08-29 04:52:16 +00:00
|
|
|
if (len > s->scsi_len)
|
|
|
|
len = s->scsi_len;
|
2012-05-16 13:03:40 +00:00
|
|
|
usb_packet_copy(p, scsi_req_get_buf(s->req) + s->scsi_off, len);
|
2006-08-29 04:52:16 +00:00
|
|
|
s->scsi_len -= len;
|
2012-05-16 13:03:40 +00:00
|
|
|
s->scsi_off += len;
|
2020-05-20 23:53:47 +00:00
|
|
|
if (len > s->data_len) {
|
|
|
|
len = s->data_len;
|
|
|
|
}
|
2006-08-29 04:52:16 +00:00
|
|
|
s->data_len -= len;
|
2010-12-09 22:31:49 +00:00
|
|
|
if (s->scsi_len == 0 || s->data_len == 0) {
|
2011-04-18 13:28:11 +00:00
|
|
|
scsi_req_continue(s->req);
|
2006-08-29 04:52:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-09 09:36:35 +00:00
|
|
|
static void usb_msd_send_status(MSDState *s, USBPacket *p)
|
2006-08-29 04:52:16 +00:00
|
|
|
{
|
2010-12-09 09:36:35 +00:00
|
|
|
int len;
|
2006-08-29 04:52:16 +00:00
|
|
|
|
2011-11-21 10:17:59 +00:00
|
|
|
DPRINTF("Command status %d tag 0x%x, len %zd\n",
|
2012-03-08 00:41:10 +00:00
|
|
|
s->csw.status, le32_to_cpu(s->csw.tag), p->iov.size);
|
2011-11-21 10:29:27 +00:00
|
|
|
|
2012-03-08 00:41:10 +00:00
|
|
|
assert(s->csw.sig == cpu_to_le32(0x53425355));
|
2011-11-21 10:29:27 +00:00
|
|
|
len = MIN(sizeof(s->csw), p->iov.size);
|
|
|
|
usb_packet_copy(p, &s->csw, len);
|
|
|
|
memset(&s->csw, 0, sizeof(s->csw));
|
2006-08-29 04:52:16 +00:00
|
|
|
}
|
|
|
|
|
2012-05-22 12:30:20 +00:00
|
|
|
static void usb_msd_packet_complete(MSDState *s)
|
|
|
|
{
|
|
|
|
USBPacket *p = s->packet;
|
|
|
|
|
|
|
|
/* Set s->packet to NULL before calling usb_packet_complete
|
|
|
|
because another request may be issued before
|
|
|
|
usb_packet_complete returns. */
|
|
|
|
DPRINTF("Packet complete %p\n", p);
|
|
|
|
s->packet = NULL;
|
|
|
|
usb_packet_complete(&s->dev, p);
|
|
|
|
}
|
|
|
|
|
2011-05-20 18:18:07 +00:00
|
|
|
static void usb_msd_transfer_data(SCSIRequest *req, uint32_t len)
|
2006-05-25 23:58:51 +00:00
|
|
|
{
|
2011-04-18 10:35:39 +00:00
|
|
|
MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
|
2006-08-29 04:52:16 +00:00
|
|
|
USBPacket *p = s->packet;
|
2006-08-12 01:04:27 +00:00
|
|
|
|
2011-04-18 13:28:11 +00:00
|
|
|
assert((s->mode == USB_MSDM_DATAOUT) == (req->cmd.mode == SCSI_XFER_TO_DEV));
|
2011-05-20 18:18:07 +00:00
|
|
|
s->scsi_len = len;
|
2012-05-16 13:03:40 +00:00
|
|
|
s->scsi_off = 0;
|
2006-08-29 04:52:16 +00:00
|
|
|
if (p) {
|
2011-07-13 10:32:06 +00:00
|
|
|
usb_msd_copy_data(s, p);
|
|
|
|
p = s->packet;
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 16:15:01 +00:00
|
|
|
if (p && p->actual_length == p->iov.size) {
|
|
|
|
p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */
|
2012-05-22 12:30:20 +00:00
|
|
|
usb_msd_packet_complete(s);
|
2006-08-29 04:52:16 +00:00
|
|
|
}
|
2006-08-12 01:04:27 +00:00
|
|
|
}
|
2006-05-25 23:58:51 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 09:55:37 +00:00
|
|
|
static void usb_msd_command_complete(SCSIRequest *req, uint32_t status, size_t resid)
|
2011-04-22 10:27:30 +00:00
|
|
|
{
|
|
|
|
MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
|
|
|
|
USBPacket *p = s->packet;
|
|
|
|
|
2011-11-21 10:36:17 +00:00
|
|
|
DPRINTF("Command complete %d tag 0x%x\n", status, req->tag);
|
2011-11-21 10:29:27 +00:00
|
|
|
|
|
|
|
s->csw.sig = cpu_to_le32(0x53425355);
|
2011-11-21 10:36:17 +00:00
|
|
|
s->csw.tag = cpu_to_le32(req->tag);
|
2012-05-14 12:00:02 +00:00
|
|
|
s->csw.residue = cpu_to_le32(s->data_len);
|
2011-11-21 10:41:30 +00:00
|
|
|
s->csw.status = status != 0;
|
2011-11-21 10:29:27 +00:00
|
|
|
|
2011-04-22 10:27:30 +00:00
|
|
|
if (s->packet) {
|
|
|
|
if (s->data_len == 0 && s->mode == USB_MSDM_DATAOUT) {
|
|
|
|
/* A deferred packet with no write data remaining must be
|
|
|
|
the status read packet. */
|
|
|
|
usb_msd_send_status(s, p);
|
|
|
|
s->mode = USB_MSDM_CBW;
|
2012-07-13 09:38:13 +00:00
|
|
|
} else if (s->mode == USB_MSDM_CSW) {
|
|
|
|
usb_msd_send_status(s, p);
|
|
|
|
s->mode = USB_MSDM_CBW;
|
2011-04-22 10:27:30 +00:00
|
|
|
} else {
|
|
|
|
if (s->data_len) {
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 16:15:01 +00:00
|
|
|
int len = (p->iov.size - p->actual_length);
|
2011-07-13 10:32:06 +00:00
|
|
|
usb_packet_skip(p, len);
|
2020-05-20 23:53:47 +00:00
|
|
|
if (len > s->data_len) {
|
|
|
|
len = s->data_len;
|
|
|
|
}
|
2011-07-13 10:32:06 +00:00
|
|
|
s->data_len -= len;
|
2011-04-22 10:27:30 +00:00
|
|
|
}
|
|
|
|
if (s->data_len == 0) {
|
|
|
|
s->mode = USB_MSDM_CSW;
|
|
|
|
}
|
|
|
|
}
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 16:15:01 +00:00
|
|
|
p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */
|
2012-05-22 12:30:20 +00:00
|
|
|
usb_msd_packet_complete(s);
|
2011-04-22 10:27:30 +00:00
|
|
|
} else if (s->data_len == 0) {
|
|
|
|
s->mode = USB_MSDM_CSW;
|
|
|
|
}
|
|
|
|
scsi_req_unref(req);
|
|
|
|
s->req = NULL;
|
|
|
|
}
|
|
|
|
|
2011-04-18 20:53:08 +00:00
|
|
|
static void usb_msd_request_cancelled(SCSIRequest *req)
|
|
|
|
{
|
|
|
|
MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
|
|
|
|
|
|
|
|
if (req == s->req) {
|
|
|
|
scsi_req_unref(s->req);
|
|
|
|
s->req = NULL;
|
|
|
|
s->scsi_len = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-19 18:06:15 +00:00
|
|
|
static void usb_msd_handle_reset(USBDevice *dev)
|
2006-05-25 23:58:51 +00:00
|
|
|
{
|
|
|
|
MSDState *s = (MSDState *)dev;
|
|
|
|
|
|
|
|
DPRINTF("Reset\n");
|
2012-01-04 17:13:54 +00:00
|
|
|
if (s->req) {
|
|
|
|
scsi_req_cancel(s->req);
|
|
|
|
}
|
|
|
|
assert(s->req == NULL);
|
|
|
|
|
|
|
|
if (s->packet) {
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 16:15:01 +00:00
|
|
|
s->packet->status = USB_RET_STALL;
|
2012-05-22 12:30:20 +00:00
|
|
|
usb_msd_packet_complete(s);
|
2012-01-04 17:13:54 +00:00
|
|
|
}
|
|
|
|
|
2006-05-25 23:58:51 +00:00
|
|
|
s->mode = USB_MSDM_CBW;
|
|
|
|
}
|
|
|
|
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 16:15:01 +00:00
|
|
|
static void usb_msd_handle_control(USBDevice *dev, USBPacket *p,
|
2011-02-02 15:33:13 +00:00
|
|
|
int request, int value, int index, int length, uint8_t *data)
|
2006-05-25 23:58:51 +00:00
|
|
|
{
|
|
|
|
MSDState *s = (MSDState *)dev;
|
2013-01-14 14:29:44 +00:00
|
|
|
SCSIDevice *scsi_dev;
|
|
|
|
int ret, maxlun;
|
2006-05-25 23:58:51 +00:00
|
|
|
|
2011-02-02 15:33:13 +00:00
|
|
|
ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
|
2010-11-17 10:05:41 +00:00
|
|
|
if (ret >= 0) {
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 16:15:01 +00:00
|
|
|
return;
|
2010-11-17 10:05:41 +00:00
|
|
|
}
|
|
|
|
|
2006-05-25 23:58:51 +00:00
|
|
|
switch (request) {
|
|
|
|
case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
|
2010-03-10 09:45:12 +00:00
|
|
|
break;
|
2006-05-25 23:58:51 +00:00
|
|
|
/* Class specific requests. */
|
2010-03-14 11:19:03 +00:00
|
|
|
case ClassInterfaceOutRequest | MassStorageReset:
|
2006-05-25 23:58:51 +00:00
|
|
|
/* Reset state ready for the next CBW. */
|
|
|
|
s->mode = USB_MSDM_CBW;
|
|
|
|
break;
|
2010-03-14 11:19:03 +00:00
|
|
|
case ClassInterfaceRequest | GetMaxLun:
|
2013-01-14 14:29:44 +00:00
|
|
|
maxlun = 0;
|
|
|
|
for (;;) {
|
|
|
|
scsi_dev = scsi_device_find(&s->bus, 0, 0, maxlun+1);
|
|
|
|
if (scsi_dev == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (scsi_dev->lun != maxlun+1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
maxlun++;
|
|
|
|
}
|
|
|
|
DPRINTF("MaxLun %d\n", maxlun);
|
|
|
|
data[0] = maxlun;
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 16:15:01 +00:00
|
|
|
p->actual_length = 1;
|
2006-05-25 23:58:51 +00:00
|
|
|
break;
|
|
|
|
default:
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 16:15:01 +00:00
|
|
|
p->status = USB_RET_STALL;
|
2006-05-25 23:58:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-16 08:34:53 +00:00
|
|
|
static void usb_msd_cancel_io(USBDevice *dev, USBPacket *p)
|
2006-08-12 01:04:27 +00:00
|
|
|
{
|
2015-05-06 12:55:32 +00:00
|
|
|
MSDState *s = USB_STORAGE_DEV(dev);
|
2011-09-02 11:05:13 +00:00
|
|
|
|
2012-04-18 10:08:29 +00:00
|
|
|
assert(s->packet == p);
|
|
|
|
s->packet = NULL;
|
|
|
|
|
2011-09-02 11:05:13 +00:00
|
|
|
if (s->req) {
|
|
|
|
scsi_req_cancel(s->req);
|
|
|
|
}
|
2006-08-12 01:04:27 +00:00
|
|
|
}
|
|
|
|
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 16:15:01 +00:00
|
|
|
static void usb_msd_handle_data(USBDevice *dev, USBPacket *p)
|
2006-05-25 23:58:51 +00:00
|
|
|
{
|
|
|
|
MSDState *s = (MSDState *)dev;
|
2011-11-21 10:36:17 +00:00
|
|
|
uint32_t tag;
|
2006-05-25 23:58:51 +00:00
|
|
|
struct usb_msd_cbw cbw;
|
2012-01-12 12:23:01 +00:00
|
|
|
uint8_t devep = p->ep->nr;
|
2013-01-14 14:29:44 +00:00
|
|
|
SCSIDevice *scsi_dev;
|
2013-01-22 13:17:05 +00:00
|
|
|
uint32_t len;
|
2006-05-25 23:58:51 +00:00
|
|
|
|
2006-08-12 01:04:27 +00:00
|
|
|
switch (p->pid) {
|
2006-05-25 23:58:51 +00:00
|
|
|
case USB_TOKEN_OUT:
|
|
|
|
if (devep != 2)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
switch (s->mode) {
|
|
|
|
case USB_MSDM_CBW:
|
2011-07-13 10:32:06 +00:00
|
|
|
if (p->iov.size != 31) {
|
2014-09-19 06:48:30 +00:00
|
|
|
error_report("usb-msd: Bad CBW size");
|
2006-05-25 23:58:51 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
2011-07-13 10:32:06 +00:00
|
|
|
usb_packet_copy(p, &cbw, 31);
|
2006-05-25 23:58:51 +00:00
|
|
|
if (le32_to_cpu(cbw.sig) != 0x43425355) {
|
2014-09-19 06:48:30 +00:00
|
|
|
error_report("usb-msd: Bad signature %08x",
|
|
|
|
le32_to_cpu(cbw.sig));
|
2006-05-25 23:58:51 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
DPRINTF("Command on LUN %d\n", cbw.lun);
|
2013-01-14 14:29:44 +00:00
|
|
|
scsi_dev = scsi_device_find(&s->bus, 0, 0, cbw.lun);
|
|
|
|
if (scsi_dev == NULL) {
|
2014-09-19 06:48:30 +00:00
|
|
|
error_report("usb-msd: Bad LUN %d", cbw.lun);
|
2006-05-25 23:58:51 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
2011-11-21 10:36:17 +00:00
|
|
|
tag = le32_to_cpu(cbw.tag);
|
2006-05-25 23:58:51 +00:00
|
|
|
s->data_len = le32_to_cpu(cbw.data_len);
|
|
|
|
if (s->data_len == 0) {
|
|
|
|
s->mode = USB_MSDM_CSW;
|
|
|
|
} else if (cbw.flags & 0x80) {
|
|
|
|
s->mode = USB_MSDM_DATAIN;
|
|
|
|
} else {
|
|
|
|
s->mode = USB_MSDM_DATAOUT;
|
|
|
|
}
|
|
|
|
DPRINTF("Command tag 0x%x flags %08x len %d data %d\n",
|
2011-11-21 10:36:17 +00:00
|
|
|
tag, cbw.flags, cbw.cmd_len, s->data_len);
|
2012-05-14 12:00:02 +00:00
|
|
|
assert(le32_to_cpu(s->csw.residue) == 0);
|
2011-05-04 14:49:56 +00:00
|
|
|
s->scsi_len = 0;
|
2013-01-14 14:29:44 +00:00
|
|
|
s->req = scsi_req_new(scsi_dev, tag, cbw.lun, cbw.cmd, NULL);
|
2012-07-13 09:35:51 +00:00
|
|
|
#ifdef DEBUG_MSD
|
|
|
|
scsi_req_print(s->req);
|
|
|
|
#endif
|
2013-01-22 13:17:05 +00:00
|
|
|
len = scsi_req_enqueue(s->req);
|
|
|
|
if (len) {
|
2011-04-18 13:28:11 +00:00
|
|
|
scsi_req_continue(s->req);
|
2006-08-29 04:52:16 +00:00
|
|
|
}
|
2006-05-25 23:58:51 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_MSDM_DATAOUT:
|
2011-07-13 10:32:06 +00:00
|
|
|
DPRINTF("Data out %zd/%d\n", p->iov.size, s->data_len);
|
|
|
|
if (p->iov.size > s->data_len) {
|
2006-05-25 23:58:51 +00:00
|
|
|
goto fail;
|
2011-07-13 10:32:06 +00:00
|
|
|
}
|
2006-05-25 23:58:51 +00:00
|
|
|
|
2006-08-29 04:52:16 +00:00
|
|
|
if (s->scsi_len) {
|
2011-07-13 10:32:06 +00:00
|
|
|
usb_msd_copy_data(s, p);
|
2006-08-29 04:52:16 +00:00
|
|
|
}
|
2012-05-14 12:00:02 +00:00
|
|
|
if (le32_to_cpu(s->csw.residue)) {
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 16:15:01 +00:00
|
|
|
int len = p->iov.size - p->actual_length;
|
2011-07-13 10:32:06 +00:00
|
|
|
if (len) {
|
|
|
|
usb_packet_skip(p, len);
|
2020-05-20 23:53:47 +00:00
|
|
|
if (len > s->data_len) {
|
|
|
|
len = s->data_len;
|
|
|
|
}
|
2011-07-13 10:32:06 +00:00
|
|
|
s->data_len -= len;
|
|
|
|
if (s->data_len == 0) {
|
|
|
|
s->mode = USB_MSDM_CSW;
|
|
|
|
}
|
|
|
|
}
|
2006-08-29 04:52:16 +00:00
|
|
|
}
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 16:15:01 +00:00
|
|
|
if (p->actual_length < p->iov.size) {
|
2012-07-13 09:35:51 +00:00
|
|
|
DPRINTF("Deferring packet %p [wait data-out]\n", p);
|
2006-08-12 01:04:27 +00:00
|
|
|
s->packet = p;
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 16:15:01 +00:00
|
|
|
p->status = USB_RET_ASYNC;
|
2006-08-12 01:04:27 +00:00
|
|
|
}
|
2006-05-25 23:58:51 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-07-13 10:32:06 +00:00
|
|
|
DPRINTF("Unexpected write (len %zd)\n", p->iov.size);
|
2006-05-25 23:58:51 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_TOKEN_IN:
|
|
|
|
if (devep != 1)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
switch (s->mode) {
|
2006-08-29 04:52:16 +00:00
|
|
|
case USB_MSDM_DATAOUT:
|
2011-07-13 10:32:06 +00:00
|
|
|
if (s->data_len != 0 || p->iov.size < 13) {
|
2006-08-29 04:52:16 +00:00
|
|
|
goto fail;
|
2011-07-13 10:32:06 +00:00
|
|
|
}
|
2006-08-29 04:52:16 +00:00
|
|
|
/* Waiting for SCSI write to complete. */
|
|
|
|
s->packet = p;
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 16:15:01 +00:00
|
|
|
p->status = USB_RET_ASYNC;
|
2006-08-29 04:52:16 +00:00
|
|
|
break;
|
|
|
|
|
2006-05-25 23:58:51 +00:00
|
|
|
case USB_MSDM_CSW:
|
2011-07-13 10:32:06 +00:00
|
|
|
if (p->iov.size < 13) {
|
2006-05-25 23:58:51 +00:00
|
|
|
goto fail;
|
2011-07-13 10:32:06 +00:00
|
|
|
}
|
2006-05-25 23:58:51 +00:00
|
|
|
|
2011-11-21 13:01:26 +00:00
|
|
|
if (s->req) {
|
|
|
|
/* still in flight */
|
2012-07-13 09:35:51 +00:00
|
|
|
DPRINTF("Deferring packet %p [wait status]\n", p);
|
2011-11-21 13:01:26 +00:00
|
|
|
s->packet = p;
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 16:15:01 +00:00
|
|
|
p->status = USB_RET_ASYNC;
|
2011-11-21 13:01:26 +00:00
|
|
|
} else {
|
|
|
|
usb_msd_send_status(s, p);
|
|
|
|
s->mode = USB_MSDM_CBW;
|
|
|
|
}
|
2006-05-25 23:58:51 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_MSDM_DATAIN:
|
2011-07-13 10:32:06 +00:00
|
|
|
DPRINTF("Data in %zd/%d, scsi_len %d\n",
|
|
|
|
p->iov.size, s->data_len, s->scsi_len);
|
2006-08-29 04:52:16 +00:00
|
|
|
if (s->scsi_len) {
|
2011-07-13 10:32:06 +00:00
|
|
|
usb_msd_copy_data(s, p);
|
2006-08-29 04:52:16 +00:00
|
|
|
}
|
2012-05-14 12:00:02 +00:00
|
|
|
if (le32_to_cpu(s->csw.residue)) {
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 16:15:01 +00:00
|
|
|
int len = p->iov.size - p->actual_length;
|
2011-07-13 10:32:06 +00:00
|
|
|
if (len) {
|
|
|
|
usb_packet_skip(p, len);
|
2020-05-20 23:53:47 +00:00
|
|
|
if (len > s->data_len) {
|
|
|
|
len = s->data_len;
|
|
|
|
}
|
2011-07-13 10:32:06 +00:00
|
|
|
s->data_len -= len;
|
|
|
|
if (s->data_len == 0) {
|
|
|
|
s->mode = USB_MSDM_CSW;
|
|
|
|
}
|
|
|
|
}
|
2006-08-29 04:52:16 +00:00
|
|
|
}
|
2020-05-20 23:53:47 +00:00
|
|
|
if (p->actual_length < p->iov.size && (p->short_not_ok ||
|
|
|
|
s->scsi_len >= p->ep->max_packet_size)) {
|
2012-07-13 09:35:51 +00:00
|
|
|
DPRINTF("Deferring packet %p [wait data-in]\n", p);
|
2006-08-12 01:04:27 +00:00
|
|
|
s->packet = p;
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 16:15:01 +00:00
|
|
|
p->status = USB_RET_ASYNC;
|
2006-08-12 01:04:27 +00:00
|
|
|
}
|
2006-05-25 23:58:51 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-07-13 10:32:06 +00:00
|
|
|
DPRINTF("Unexpected read (len %zd)\n", p->iov.size);
|
2006-05-25 23:58:51 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DPRINTF("Bad token\n");
|
|
|
|
fail:
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 16:15:01 +00:00
|
|
|
p->status = USB_RET_STALL;
|
2006-05-25 23:58:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-14 11:56:40 +00:00
|
|
|
static void *usb_msd_load_request(QEMUFile *f, SCSIRequest *req)
|
|
|
|
{
|
|
|
|
MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
|
|
|
|
|
|
|
|
/* nothing to load, just store req in our state struct */
|
|
|
|
assert(s->req == NULL);
|
|
|
|
scsi_req_ref(req);
|
|
|
|
s->req = req;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-01-14 14:29:44 +00:00
|
|
|
static const struct SCSIBusInfo usb_msd_scsi_info_storage = {
|
2011-08-13 13:44:45 +00:00
|
|
|
.tcq = false,
|
2011-08-13 16:55:17 +00:00
|
|
|
.max_target = 0,
|
|
|
|
.max_lun = 0,
|
2011-08-13 13:44:45 +00:00
|
|
|
|
2011-04-22 10:27:30 +00:00
|
|
|
.transfer_data = usb_msd_transfer_data,
|
2011-04-18 20:53:08 +00:00
|
|
|
.complete = usb_msd_command_complete,
|
2012-05-14 11:56:40 +00:00
|
|
|
.cancel = usb_msd_request_cancelled,
|
|
|
|
.load_request = usb_msd_load_request,
|
2011-04-18 15:11:14 +00:00
|
|
|
};
|
|
|
|
|
2013-01-14 14:29:44 +00:00
|
|
|
static const struct SCSIBusInfo usb_msd_scsi_info_bot = {
|
|
|
|
.tcq = false,
|
|
|
|
.max_target = 0,
|
|
|
|
.max_lun = 15,
|
|
|
|
|
|
|
|
.transfer_data = usb_msd_transfer_data,
|
|
|
|
.complete = usb_msd_command_complete,
|
|
|
|
.cancel = usb_msd_request_cancelled,
|
|
|
|
.load_request = usb_msd_load_request,
|
|
|
|
};
|
|
|
|
|
2017-11-22 03:08:46 +00:00
|
|
|
static void usb_msd_storage_realize(USBDevice *dev, Error **errp)
|
2009-08-31 12:23:59 +00:00
|
|
|
{
|
2015-05-06 12:55:32 +00:00
|
|
|
MSDState *s = USB_STORAGE_DEV(dev);
|
2014-10-07 11:59:18 +00:00
|
|
|
BlockBackend *blk = s->conf.blk;
|
2013-01-14 14:29:44 +00:00
|
|
|
SCSIDevice *scsi_dev;
|
2009-08-31 12:23:59 +00:00
|
|
|
|
2014-10-07 11:59:18 +00:00
|
|
|
if (!blk) {
|
2014-09-19 06:48:29 +00:00
|
|
|
error_setg(errp, "drive property not set");
|
|
|
|
return;
|
2009-08-31 12:24:05 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 22:55:10 +00:00
|
|
|
if (!blkconf_blocksizes(&s->conf, errp)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:08:45 +00:00
|
|
|
if (!blkconf_apply_backend_options(&s->conf, blk_is_read_only(blk), true,
|
|
|
|
errp)) {
|
2017-01-24 12:43:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-07-11 13:08:37 +00:00
|
|
|
|
2010-06-25 06:09:10 +00:00
|
|
|
/*
|
|
|
|
* Hack alert: this pretends to be a block device, but it's really
|
|
|
|
* a SCSI bus that can serve only a single device, which it
|
2010-06-29 14:58:30 +00:00
|
|
|
* creates automatically. But first it needs to detach from its
|
|
|
|
* blockdev, or else scsi_bus_legacy_add_drive() dies when it
|
2016-06-21 18:46:05 +00:00
|
|
|
* attaches again. We also need to take another reference so that
|
|
|
|
* blk_detach_dev() doesn't free blk while we still need it.
|
2010-06-25 06:09:10 +00:00
|
|
|
*
|
|
|
|
* The hack is probably a bad idea.
|
|
|
|
*/
|
2016-06-21 18:46:05 +00:00
|
|
|
blk_ref(blk);
|
2019-05-28 16:40:18 +00:00
|
|
|
blk_detach_dev(blk, DEVICE(s));
|
2014-10-07 11:59:18 +00:00
|
|
|
s->conf.blk = NULL;
|
2010-06-25 06:09:10 +00:00
|
|
|
|
2013-06-12 11:01:49 +00:00
|
|
|
usb_desc_create_serial(dev);
|
2010-11-26 19:20:41 +00:00
|
|
|
usb_desc_init(dev);
|
2013-08-23 18:30:03 +00:00
|
|
|
scsi_bus_new(&s->bus, sizeof(s->bus), DEVICE(dev),
|
|
|
|
&usb_msd_scsi_info_storage, NULL);
|
2014-10-07 11:59:18 +00:00
|
|
|
scsi_dev = scsi_bus_legacy_add_drive(&s->bus, blk, 0, !!s->removable,
|
2018-01-17 00:52:22 +00:00
|
|
|
s->conf.bootindex, s->conf.share_rw,
|
2018-06-25 16:39:00 +00:00
|
|
|
s->conf.rerror, s->conf.werror,
|
2018-01-17 00:52:22 +00:00
|
|
|
dev->serial,
|
2017-11-22 03:08:45 +00:00
|
|
|
errp);
|
2016-06-21 18:46:05 +00:00
|
|
|
blk_unref(blk);
|
2013-01-14 14:29:44 +00:00
|
|
|
if (!scsi_dev) {
|
2014-09-19 06:48:29 +00:00
|
|
|
return;
|
2010-06-25 16:53:21 +00:00
|
|
|
}
|
2009-08-31 12:24:05 +00:00
|
|
|
usb_msd_handle_reset(dev);
|
2014-10-07 08:00:33 +00:00
|
|
|
s->scsi_dev = scsi_dev;
|
2009-08-31 12:23:59 +00:00
|
|
|
}
|
|
|
|
|
2017-11-22 03:08:46 +00:00
|
|
|
static void usb_msd_bot_realize(USBDevice *dev, Error **errp)
|
2013-01-14 14:29:44 +00:00
|
|
|
{
|
2015-05-06 12:55:32 +00:00
|
|
|
MSDState *s = USB_STORAGE_DEV(dev);
|
2016-06-15 09:46:58 +00:00
|
|
|
DeviceState *d = DEVICE(dev);
|
2013-01-14 14:29:44 +00:00
|
|
|
|
|
|
|
usb_desc_create_serial(dev);
|
|
|
|
usb_desc_init(dev);
|
2016-06-15 09:46:58 +00:00
|
|
|
if (d->hotplugged) {
|
|
|
|
s->dev.auto_attach = 0;
|
|
|
|
}
|
|
|
|
|
2013-08-23 18:30:03 +00:00
|
|
|
scsi_bus_new(&s->bus, sizeof(s->bus), DEVICE(dev),
|
|
|
|
&usb_msd_scsi_info_bot, NULL);
|
2013-01-14 14:29:44 +00:00
|
|
|
usb_msd_handle_reset(dev);
|
|
|
|
}
|
|
|
|
|
2010-12-10 13:58:41 +00:00
|
|
|
static const VMStateDescription vmstate_usb_msd = {
|
|
|
|
.name = "usb-storage",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2014-04-16 11:31:26 +00:00
|
|
|
.fields = (VMStateField[]) {
|
2010-12-10 13:58:41 +00:00
|
|
|
VMSTATE_USB_DEVICE(dev, MSDState),
|
2012-05-14 11:56:40 +00:00
|
|
|
VMSTATE_UINT32(mode, MSDState),
|
|
|
|
VMSTATE_UINT32(scsi_len, MSDState),
|
|
|
|
VMSTATE_UINT32(scsi_off, MSDState),
|
|
|
|
VMSTATE_UINT32(data_len, MSDState),
|
|
|
|
VMSTATE_UINT32(csw.sig, MSDState),
|
|
|
|
VMSTATE_UINT32(csw.tag, MSDState),
|
|
|
|
VMSTATE_UINT32(csw.residue, MSDState),
|
|
|
|
VMSTATE_UINT8(csw.status, MSDState),
|
2010-12-10 13:58:41 +00:00
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-12-08 03:34:16 +00:00
|
|
|
static Property msd_properties[] = {
|
|
|
|
DEFINE_BLOCK_PROPERTIES(MSDState, conf),
|
2018-06-25 16:39:00 +00:00
|
|
|
DEFINE_BLOCK_ERROR_PROPERTIES(MSDState, conf),
|
2011-12-08 03:34:16 +00:00
|
|
|
DEFINE_PROP_BIT("removable", MSDState, removable, 0, false),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
2015-05-06 12:55:32 +00:00
|
|
|
static void usb_msd_class_initfn_common(ObjectClass *klass, void *data)
|
2011-12-15 20:53:10 +00:00
|
|
|
{
|
2011-12-08 03:34:16 +00:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2011-12-15 20:53:10 +00:00
|
|
|
USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
|
|
|
|
|
|
|
|
uc->product_desc = "QEMU USB MSD";
|
|
|
|
uc->usb_desc = &desc;
|
|
|
|
uc->cancel_packet = usb_msd_cancel_io;
|
|
|
|
uc->handle_attach = usb_desc_attach;
|
|
|
|
uc->handle_reset = usb_msd_handle_reset;
|
|
|
|
uc->handle_control = usb_msd_handle_control;
|
|
|
|
uc->handle_data = usb_msd_handle_data;
|
2013-07-29 14:17:45 +00:00
|
|
|
set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
|
2011-12-08 03:34:16 +00:00
|
|
|
dc->fw_name = "storage";
|
|
|
|
dc->vmsd = &vmstate_usb_msd;
|
2013-01-14 14:29:44 +00:00
|
|
|
}
|
|
|
|
|
2017-11-22 03:08:46 +00:00
|
|
|
static void usb_msd_class_storage_initfn(ObjectClass *klass, void *data)
|
2013-01-14 14:29:44 +00:00
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
|
|
|
USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
|
|
|
|
|
2017-11-22 03:08:46 +00:00
|
|
|
uc->realize = usb_msd_storage_realize;
|
2020-01-10 15:30:32 +00:00
|
|
|
device_class_set_props(dc, msd_properties);
|
2013-01-14 14:29:44 +00:00
|
|
|
}
|
|
|
|
|
qom: Swap 'name' next to visitor in ObjectPropertyAccessor
Similar to the previous patch, it's nice to have all functions
in the tree that involve a visitor and a name for conversion to
or from QAPI to consistently stick the 'name' parameter next
to the Visitor parameter.
Done by manually changing include/qom/object.h and qom/object.c,
then running this Coccinelle script and touching up the fallout
(Coccinelle insisted on adding some trailing whitespace).
@ rule1 @
identifier fn;
typedef Object, Visitor, Error;
identifier obj, v, opaque, name, errp;
@@
void fn
- (Object *obj, Visitor *v, void *opaque, const char *name,
+ (Object *obj, Visitor *v, const char *name, void *opaque,
Error **errp) { ... }
@@
identifier rule1.fn;
expression obj, v, opaque, name, errp;
@@
fn(obj, v,
- opaque, name,
+ name, opaque,
errp)
Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <1454075341-13658-20-git-send-email-eblake@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2016-01-29 13:48:55 +00:00
|
|
|
static void usb_msd_get_bootindex(Object *obj, Visitor *v, const char *name,
|
|
|
|
void *opaque, Error **errp)
|
2014-10-07 08:00:33 +00:00
|
|
|
{
|
|
|
|
USBDevice *dev = USB_DEVICE(obj);
|
2015-05-06 12:55:32 +00:00
|
|
|
MSDState *s = USB_STORAGE_DEV(dev);
|
2014-10-07 08:00:33 +00:00
|
|
|
|
qapi: Swap visit_* arguments for consistent 'name' placement
JSON uses "name":value, but many of our visitor interfaces were
called with visit_type_FOO(v, &value, name, errp). This can be
a bit confusing to have to mentally swap the parameter order to
match JSON order. It's particularly bad for visit_start_struct(),
where the 'name' parameter is smack in the middle of the
otherwise-related group of 'obj, kind, size' parameters! It's
time to do a global swap of the parameter ordering, so that the
'name' parameter is always immediately after the Visitor argument.
Additional reason in favor of the swap: the existing include/qjson.h
prefers listing 'name' first in json_prop_*(), and I have plans to
unify that file with the qapi visitors; listing 'name' first in
qapi will minimize churn to the (admittedly few) qjson.h clients.
Later patches will then fix docs, object.h, visitor-impl.h, and
those clients to match.
Done by first patching scripts/qapi*.py by hand to make generated
files do what I want, then by running the following Coccinelle
script to affect the rest of the code base:
$ spatch --sp-file script `git grep -l '\bvisit_' -- '**/*.[ch]'`
I then had to apply some touchups (Coccinelle insisted on TAB
indentation in visitor.h, and botched the signature of
visit_type_enum() by rewriting 'const char *const strings[]' to
the syntactically invalid 'const char*const[] strings'). The
movement of parameters is sufficient to provoke compiler errors
if any callers were missed.
// Part 1: Swap declaration order
@@
type TV, TErr, TObj, T1, T2;
identifier OBJ, ARG1, ARG2;
@@
void visit_start_struct
-(TV v, TObj OBJ, T1 ARG1, const char *name, T2 ARG2, TErr errp)
+(TV v, const char *name, TObj OBJ, T1 ARG1, T2 ARG2, TErr errp)
{ ... }
@@
type bool, TV, T1;
identifier ARG1;
@@
bool visit_optional
-(TV v, T1 ARG1, const char *name)
+(TV v, const char *name, T1 ARG1)
{ ... }
@@
type TV, TErr, TObj, T1;
identifier OBJ, ARG1;
@@
void visit_get_next_type
-(TV v, TObj OBJ, T1 ARG1, const char *name, TErr errp)
+(TV v, const char *name, TObj OBJ, T1 ARG1, TErr errp)
{ ... }
@@
type TV, TErr, TObj, T1, T2;
identifier OBJ, ARG1, ARG2;
@@
void visit_type_enum
-(TV v, TObj OBJ, T1 ARG1, T2 ARG2, const char *name, TErr errp)
+(TV v, const char *name, TObj OBJ, T1 ARG1, T2 ARG2, TErr errp)
{ ... }
@@
type TV, TErr, TObj;
identifier OBJ;
identifier VISIT_TYPE =~ "^visit_type_";
@@
void VISIT_TYPE
-(TV v, TObj OBJ, const char *name, TErr errp)
+(TV v, const char *name, TObj OBJ, TErr errp)
{ ... }
// Part 2: swap caller order
@@
expression V, NAME, OBJ, ARG1, ARG2, ERR;
identifier VISIT_TYPE =~ "^visit_type_";
@@
(
-visit_start_struct(V, OBJ, ARG1, NAME, ARG2, ERR)
+visit_start_struct(V, NAME, OBJ, ARG1, ARG2, ERR)
|
-visit_optional(V, ARG1, NAME)
+visit_optional(V, NAME, ARG1)
|
-visit_get_next_type(V, OBJ, ARG1, NAME, ERR)
+visit_get_next_type(V, NAME, OBJ, ARG1, ERR)
|
-visit_type_enum(V, OBJ, ARG1, ARG2, NAME, ERR)
+visit_type_enum(V, NAME, OBJ, ARG1, ARG2, ERR)
|
-VISIT_TYPE(V, OBJ, NAME, ERR)
+VISIT_TYPE(V, NAME, OBJ, ERR)
)
Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <1454075341-13658-19-git-send-email-eblake@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2016-01-29 13:48:54 +00:00
|
|
|
visit_type_int32(v, name, &s->conf.bootindex, errp);
|
2014-10-07 08:00:33 +00:00
|
|
|
}
|
|
|
|
|
qom: Swap 'name' next to visitor in ObjectPropertyAccessor
Similar to the previous patch, it's nice to have all functions
in the tree that involve a visitor and a name for conversion to
or from QAPI to consistently stick the 'name' parameter next
to the Visitor parameter.
Done by manually changing include/qom/object.h and qom/object.c,
then running this Coccinelle script and touching up the fallout
(Coccinelle insisted on adding some trailing whitespace).
@ rule1 @
identifier fn;
typedef Object, Visitor, Error;
identifier obj, v, opaque, name, errp;
@@
void fn
- (Object *obj, Visitor *v, void *opaque, const char *name,
+ (Object *obj, Visitor *v, const char *name, void *opaque,
Error **errp) { ... }
@@
identifier rule1.fn;
expression obj, v, opaque, name, errp;
@@
fn(obj, v,
- opaque, name,
+ name, opaque,
errp)
Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <1454075341-13658-20-git-send-email-eblake@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2016-01-29 13:48:55 +00:00
|
|
|
static void usb_msd_set_bootindex(Object *obj, Visitor *v, const char *name,
|
|
|
|
void *opaque, Error **errp)
|
2014-10-07 08:00:33 +00:00
|
|
|
{
|
|
|
|
USBDevice *dev = USB_DEVICE(obj);
|
2015-05-06 12:55:32 +00:00
|
|
|
MSDState *s = USB_STORAGE_DEV(dev);
|
2014-10-07 08:00:33 +00:00
|
|
|
int32_t boot_index;
|
|
|
|
Error *local_err = NULL;
|
|
|
|
|
2020-07-07 16:05:47 +00:00
|
|
|
if (!visit_type_int32(v, name, &boot_index, errp)) {
|
|
|
|
return;
|
2014-10-07 08:00:33 +00:00
|
|
|
}
|
|
|
|
/* check whether bootindex is present in fw_boot_order list */
|
|
|
|
check_boot_index(boot_index, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* change bootindex to a new one */
|
|
|
|
s->conf.bootindex = boot_index;
|
|
|
|
|
|
|
|
if (s->scsi_dev) {
|
qom: Put name parameter before value / visitor parameter
The object_property_set_FOO() setters take property name and value in
an unusual order:
void object_property_set_FOO(Object *obj, FOO_TYPE value,
const char *name, Error **errp)
Having to pass value before name feels grating. Swap them.
Same for object_property_set(), object_property_get(), and
object_property_parse().
Convert callers with this Coccinelle script:
@@
identifier fun = {
object_property_get, object_property_parse, object_property_set_str,
object_property_set_link, object_property_set_bool,
object_property_set_int, object_property_set_uint, object_property_set,
object_property_set_qobject
};
expression obj, v, name, errp;
@@
- fun(obj, v, name, errp)
+ fun(obj, name, v, errp)
Chokes on hw/arm/musicpal.c's lcd_refresh() with the unhelpful error
message "no position information". Convert that one manually.
Fails to convert hw/arm/armsse.c, because Coccinelle gets confused by
ARMSSE being used both as typedef and function-like macro there.
Convert manually.
Fails to convert hw/rx/rx-gdbsim.c, because Coccinelle gets confused
by RXCPU being used both as typedef and function-like macro there.
Convert manually. The other files using RXCPU that way don't need
conversion.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <20200707160613.848843-27-armbru@redhat.com>
[Straightforwad conflict with commit 2336172d9b "audio: set default
value for pcspk.iobase property" resolved]
2020-07-07 16:05:54 +00:00
|
|
|
object_property_set_int(OBJECT(s->scsi_dev), "bootindex", boot_index,
|
2014-10-07 08:00:33 +00:00
|
|
|
&error_abort);
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2016-06-13 21:57:56 +00:00
|
|
|
error_propagate(errp, local_err);
|
2014-10-07 08:00:33 +00:00
|
|
|
}
|
|
|
|
|
2015-05-06 12:55:32 +00:00
|
|
|
static const TypeInfo usb_storage_dev_type_info = {
|
|
|
|
.name = TYPE_USB_STORAGE,
|
|
|
|
.parent = TYPE_USB_DEVICE,
|
|
|
|
.instance_size = sizeof(MSDState),
|
|
|
|
.abstract = true,
|
|
|
|
.class_init = usb_msd_class_initfn_common,
|
|
|
|
};
|
|
|
|
|
2014-10-07 08:00:33 +00:00
|
|
|
static void usb_msd_instance_init(Object *obj)
|
|
|
|
{
|
|
|
|
object_property_add(obj, "bootindex", "int32",
|
|
|
|
usb_msd_get_bootindex,
|
qom: Drop parameter @errp of object_property_add() & friends
The only way object_property_add() can fail is when a property with
the same name already exists. Since our property names are all
hardcoded, failure is a programming error, and the appropriate way to
handle it is passing &error_abort.
Same for its variants, except for object_property_add_child(), which
additionally fails when the child already has a parent. Parentage is
also under program control, so this is a programming error, too.
We have a bit over 500 callers. Almost half of them pass
&error_abort, slightly fewer ignore errors, one test case handles
errors, and the remaining few callers pass them to their own callers.
The previous few commits demonstrated once again that ignoring
programming errors is a bad idea.
Of the few ones that pass on errors, several violate the Error API.
The Error ** argument must be NULL, &error_abort, &error_fatal, or a
pointer to a variable containing NULL. Passing an argument of the
latter kind twice without clearing it in between is wrong: if the
first call sets an error, it no longer points to NULL for the second
call. ich9_pm_add_properties(), sparc32_ledma_realize(),
sparc32_dma_realize(), xilinx_axidma_realize(), xilinx_enet_realize()
are wrong that way.
When the one appropriate choice of argument is &error_abort, letting
users pick the argument is a bad idea.
Drop parameter @errp and assert the preconditions instead.
There's one exception to "duplicate property name is a programming
error": the way object_property_add() implements the magic (and
undocumented) "automatic arrayification". Don't drop @errp there.
Instead, rename object_property_add() to object_property_try_add(),
and add the obvious wrapper object_property_add().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200505152926.18877-15-armbru@redhat.com>
[Two semantic rebase conflicts resolved]
2020-05-05 15:29:22 +00:00
|
|
|
usb_msd_set_bootindex, NULL, NULL);
|
qom: Put name parameter before value / visitor parameter
The object_property_set_FOO() setters take property name and value in
an unusual order:
void object_property_set_FOO(Object *obj, FOO_TYPE value,
const char *name, Error **errp)
Having to pass value before name feels grating. Swap them.
Same for object_property_set(), object_property_get(), and
object_property_parse().
Convert callers with this Coccinelle script:
@@
identifier fun = {
object_property_get, object_property_parse, object_property_set_str,
object_property_set_link, object_property_set_bool,
object_property_set_int, object_property_set_uint, object_property_set,
object_property_set_qobject
};
expression obj, v, name, errp;
@@
- fun(obj, v, name, errp)
+ fun(obj, name, v, errp)
Chokes on hw/arm/musicpal.c's lcd_refresh() with the unhelpful error
message "no position information". Convert that one manually.
Fails to convert hw/arm/armsse.c, because Coccinelle gets confused by
ARMSSE being used both as typedef and function-like macro there.
Convert manually.
Fails to convert hw/rx/rx-gdbsim.c, because Coccinelle gets confused
by RXCPU being used both as typedef and function-like macro there.
Convert manually. The other files using RXCPU that way don't need
conversion.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <20200707160613.848843-27-armbru@redhat.com>
[Straightforwad conflict with commit 2336172d9b "audio: set default
value for pcspk.iobase property" resolved]
2020-07-07 16:05:54 +00:00
|
|
|
object_property_set_int(obj, "bootindex", -1, NULL);
|
2014-10-07 08:00:33 +00:00
|
|
|
}
|
|
|
|
|
2017-11-22 03:08:46 +00:00
|
|
|
static void usb_msd_class_bot_initfn(ObjectClass *klass, void *data)
|
2013-01-14 14:29:44 +00:00
|
|
|
{
|
|
|
|
USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
|
|
|
|
|
2017-11-22 03:08:46 +00:00
|
|
|
uc->realize = usb_msd_bot_realize;
|
2016-06-15 09:46:58 +00:00
|
|
|
uc->attached_settable = true;
|
2011-12-15 20:53:10 +00:00
|
|
|
}
|
|
|
|
|
2013-01-10 15:19:07 +00:00
|
|
|
static const TypeInfo msd_info = {
|
2011-12-08 03:34:16 +00:00
|
|
|
.name = "usb-storage",
|
2015-05-06 12:55:32 +00:00
|
|
|
.parent = TYPE_USB_STORAGE,
|
2017-11-22 03:08:46 +00:00
|
|
|
.class_init = usb_msd_class_storage_initfn,
|
2014-10-07 08:00:33 +00:00
|
|
|
.instance_init = usb_msd_instance_init,
|
2013-01-14 14:29:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const TypeInfo bot_info = {
|
|
|
|
.name = "usb-bot",
|
2015-05-06 12:55:32 +00:00
|
|
|
.parent = TYPE_USB_STORAGE,
|
2017-11-22 03:08:46 +00:00
|
|
|
.class_init = usb_msd_class_bot_initfn,
|
2009-08-31 12:23:59 +00:00
|
|
|
};
|
|
|
|
|
2012-02-09 14:20:55 +00:00
|
|
|
static void usb_msd_register_types(void)
|
2009-08-31 12:23:59 +00:00
|
|
|
{
|
2015-05-06 12:55:32 +00:00
|
|
|
type_register_static(&usb_storage_dev_type_info);
|
2011-12-08 03:34:16 +00:00
|
|
|
type_register_static(&msd_info);
|
2013-01-14 14:29:44 +00:00
|
|
|
type_register_static(&bot_info);
|
2009-08-31 12:23:59 +00:00
|
|
|
}
|
2012-02-09 14:20:55 +00:00
|
|
|
|
|
|
|
type_init(usb_msd_register_types)
|