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"
|
2021-03-12 09:04:22 +00:00
|
|
|
#include "hw/usb/msd.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"
|
2016-03-20 17:16:19 +00:00
|
|
|
#include "qemu/cutils.h"
|
2020-09-03 20:43:22 +00:00
|
|
|
#include "qom/object.h"
|
2020-11-05 13:41:07 +00:00
|
|
|
#include "trace.h"
|
2006-05-25 23:58:51 +00:00
|
|
|
|
|
|
|
/* USB requests. */
|
|
|
|
#define MassStorageReset 0xff
|
|
|
|
#define GetMaxLun 0xfe
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2022-08-30 06:38:26 +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.
|
|
|
|
*/
|
|
|
|
trace_usb_msd_packet_complete();
|
|
|
|
s->packet = NULL;
|
|
|
|
usb_packet_complete(&s->dev, p);
|
|
|
|
}
|
|
|
|
|
2022-08-30 06:38:27 +00:00
|
|
|
static void usb_msd_fatal_error(MSDState *s)
|
|
|
|
{
|
|
|
|
trace_usb_msd_fatal_error();
|
|
|
|
|
|
|
|
if (s->packet) {
|
|
|
|
s->packet->status = USB_RET_STALL;
|
|
|
|
usb_msd_packet_complete(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Guest messed up up device state with illegal requests. Go
|
|
|
|
* ignore any requests until the guests resets the device (and
|
|
|
|
* brings it into a known state that way).
|
|
|
|
*/
|
|
|
|
s->needs_reset = true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-11-05 13:41:07 +00:00
|
|
|
trace_usb_msd_send_status(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
|
|
|
}
|
|
|
|
|
2021-03-12 09:04:22 +00:00
|
|
|
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
|
|
|
|
2022-08-30 06:38:27 +00:00
|
|
|
if ((s->mode == USB_MSDM_DATAOUT) != (req->cmd.mode == SCSI_XFER_TO_DEV)) {
|
|
|
|
usb_msd_fatal_error(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-03-12 09:04:22 +00:00
|
|
|
void usb_msd_command_complete(SCSIRequest *req, 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;
|
|
|
|
|
2020-11-16 18:40:36 +00:00
|
|
|
trace_usb_msd_cmd_complete(req->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);
|
2020-11-16 18:40:36 +00:00
|
|
|
s->csw.status = req->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;
|
|
|
|
}
|
|
|
|
|
2021-03-12 09:04:22 +00:00
|
|
|
void usb_msd_request_cancelled(SCSIRequest *req)
|
2011-04-18 20:53:08 +00:00
|
|
|
{
|
|
|
|
MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
|
|
|
|
|
2020-11-05 13:41:07 +00:00
|
|
|
trace_usb_msd_cmd_cancel(req->tag);
|
|
|
|
|
2011-04-18 20:53:08 +00:00
|
|
|
if (req == s->req) {
|
2020-11-05 13:41:10 +00:00
|
|
|
s->csw.sig = cpu_to_le32(0x53425355);
|
|
|
|
s->csw.tag = cpu_to_le32(req->tag);
|
|
|
|
s->csw.status = 1; /* error */
|
|
|
|
|
2011-04-18 20:53:08 +00:00
|
|
|
scsi_req_unref(s->req);
|
|
|
|
s->req = NULL;
|
|
|
|
s->scsi_len = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 09:04:22 +00:00
|
|
|
void usb_msd_handle_reset(USBDevice *dev)
|
2006-05-25 23:58:51 +00:00
|
|
|
{
|
|
|
|
MSDState *s = (MSDState *)dev;
|
|
|
|
|
2020-11-05 13:41:07 +00:00
|
|
|
trace_usb_msd_reset();
|
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
|
|
|
}
|
|
|
|
|
2021-03-12 09:49:54 +00:00
|
|
|
memset(&s->csw, 0, sizeof(s->csw));
|
2006-05-25 23:58:51 +00:00
|
|
|
s->mode = USB_MSDM_CBW;
|
2022-08-30 06:38:27 +00:00
|
|
|
|
|
|
|
s->needs_reset = false;
|
2006-05-25 23:58:51 +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_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++;
|
|
|
|
}
|
2020-11-05 13:41:07 +00:00
|
|
|
trace_usb_msd_maxlun(maxlun);
|
2013-01-14 14:29:44 +00:00
|
|
|
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
|
|
|
|
2022-08-30 06:38:27 +00:00
|
|
|
if (s->needs_reset) {
|
|
|
|
p->status = USB_RET_STALL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
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;
|
|
|
|
}
|
2020-11-05 13:41:07 +00:00
|
|
|
trace_usb_msd_cmd_submit(cbw.lun, 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;
|
2022-08-17 05:34:58 +00:00
|
|
|
s->req = scsi_req_new(scsi_dev, tag, cbw.lun, cbw.cmd, cbw.cmd_len, NULL);
|
2020-11-05 13:41:08 +00:00
|
|
|
if (s->commandlog) {
|
|
|
|
scsi_req_print(s->req);
|
|
|
|
}
|
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:
|
2020-11-05 13:41:07 +00:00
|
|
|
trace_usb_msd_data_out(p->iov.size, s->data_len);
|
2011-07-13 10:32:06 +00:00
|
|
|
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) {
|
2020-11-05 13:41:07 +00:00
|
|
|
trace_usb_msd_packet_async();
|
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:
|
|
|
|
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. */
|
2020-11-05 13:41:07 +00:00
|
|
|
trace_usb_msd_packet_async();
|
2006-08-29 04:52:16 +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-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 */
|
2020-11-05 13:41:07 +00:00
|
|
|
trace_usb_msd_packet_async();
|
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:
|
2020-11-05 13:41:07 +00:00
|
|
|
trace_usb_msd_data_in(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-07-13 06:27:12 +00:00
|
|
|
if (p->actual_length < p->iov.size && s->mode == USB_MSDM_DATAIN) {
|
2020-11-05 13:41:07 +00:00
|
|
|
trace_usb_msd_packet_async();
|
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:
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 09:04:22 +00:00
|
|
|
void *usb_msd_load_request(QEMUFile *f, SCSIRequest *req)
|
2012-05-14 11:56:40 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
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);
|
2009-08-31 12:23:59 +00:00
|
|
|
}
|
2012-02-09 14:20:55 +00:00
|
|
|
|
|
|
|
type_init(usb_msd_register_types)
|