2007-09-16 23:08:06 +02:00
|
|
|
/*
|
2006-05-26 01:58:51 +02:00
|
|
|
* USB Mass Storage Device emulation
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006 CodeSourcery.
|
|
|
|
* Written by Paul Brook
|
|
|
|
*
|
2011-06-26 04:21:35 +02:00
|
|
|
* This code is licensed under the LGPL.
|
2006-05-26 01:58:51 +02:00
|
|
|
*/
|
|
|
|
|
2016-01-26 19:17:12 +01: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 09:01:28 +01:00
|
|
|
#include "qapi/error.h"
|
2015-03-17 18:29:20 +01:00
|
|
|
#include "qemu/error-report.h"
|
2019-05-23 16:35:07 +02:00
|
|
|
#include "qemu/module.h"
|
2012-12-17 18:20:00 +01:00
|
|
|
#include "qemu/option.h"
|
|
|
|
#include "qemu/config-file.h"
|
2012-03-07 14:55:18 +01:00
|
|
|
#include "hw/usb.h"
|
2021-03-12 10:04:22 +01:00
|
|
|
#include "hw/usb/msd.h"
|
2018-05-03 21:50:48 +02:00
|
|
|
#include "desc.h"
|
2019-08-12 07:23:51 +02:00
|
|
|
#include "hw/qdev-properties.h"
|
2013-02-05 17:06:20 +01:00
|
|
|
#include "hw/scsi/scsi.h"
|
2019-08-12 07:23:45 +02:00
|
|
|
#include "migration/vmstate.h"
|
2016-03-20 18:16:19 +01:00
|
|
|
#include "qemu/cutils.h"
|
2020-09-03 22:43:22 +02:00
|
|
|
#include "qom/object.h"
|
2020-11-05 14:41:07 +01:00
|
|
|
#include "trace.h"
|
2006-05-26 01:58:51 +02:00
|
|
|
|
|
|
|
/* USB requests. */
|
|
|
|
#define MassStorageReset 0xff
|
|
|
|
#define GetMaxLun 0xfe
|
|
|
|
|
2006-08-29 06:52:16 +02: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 11:05:41 +01:00
|
|
|
enum {
|
|
|
|
STR_MANUFACTURER = 1,
|
|
|
|
STR_PRODUCT,
|
|
|
|
STR_SERIALNUMBER,
|
2010-12-03 17:12:49 +01:00
|
|
|
STR_CONFIG_FULL,
|
|
|
|
STR_CONFIG_HIGH,
|
2012-08-28 17:29:15 +02:00
|
|
|
STR_CONFIG_SUPER,
|
2006-05-26 01:58:51 +02:00
|
|
|
};
|
|
|
|
|
2010-11-17 11:05:41 +01:00
|
|
|
static const USBDescStrings desc_strings = {
|
2012-05-30 05:35:51 +02:00
|
|
|
[STR_MANUFACTURER] = "QEMU",
|
2010-11-17 11:05:41 +01:00
|
|
|
[STR_PRODUCT] = "QEMU USB HARDDRIVE",
|
|
|
|
[STR_SERIALNUMBER] = "1",
|
2010-12-03 17:12:49 +01:00
|
|
|
[STR_CONFIG_FULL] = "Full speed config (usb 1.1)",
|
|
|
|
[STR_CONFIG_HIGH] = "High speed config (usb 2.0)",
|
2012-08-28 17:29:15 +02:00
|
|
|
[STR_CONFIG_SUPER] = "Super speed config (usb 3.0)",
|
2010-11-17 11:05:41 +01:00
|
|
|
};
|
|
|
|
|
2010-12-03 17:12:49 +01:00
|
|
|
static const USBDescIface desc_iface_full = {
|
2010-11-17 11:05:41 +01: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 17:12:49 +01:00
|
|
|
static const USBDescDevice desc_device_full = {
|
|
|
|
.bcdUSB = 0x0200,
|
2010-11-17 11:05:41 +01:00
|
|
|
.bMaxPacketSize0 = 8,
|
|
|
|
.bNumConfigurations = 1,
|
|
|
|
.confs = (USBDescConfig[]) {
|
|
|
|
{
|
|
|
|
.bNumInterfaces = 1,
|
|
|
|
.bConfigurationValue = 1,
|
2010-12-03 17:12:49 +01:00
|
|
|
.iConfiguration = STR_CONFIG_FULL,
|
2013-12-16 08:42:49 +01:00
|
|
|
.bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER,
|
2011-04-03 07:33:19 +02:00
|
|
|
.nif = 1,
|
2010-12-03 17:12:49 +01: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 08:42:49 +01:00
|
|
|
.bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER,
|
2011-04-03 07:33:19 +02:00
|
|
|
.nif = 1,
|
2010-12-03 17:12:49 +01:00
|
|
|
.ifs = &desc_iface_high,
|
2010-11-17 11:05:41 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2012-08-28 17:29:15 +02: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 08:42:49 +01:00
|
|
|
.bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER,
|
2012-08-28 17:29:15 +02:00
|
|
|
.nif = 1,
|
|
|
|
.ifs = &desc_iface_super,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2010-11-17 11:05:41 +01:00
|
|
|
static const USBDesc desc = {
|
|
|
|
.id = {
|
2011-09-15 05:25:47 +02:00
|
|
|
.idVendor = 0x46f4, /* CRC16() of "QEMU" */
|
|
|
|
.idProduct = 0x0001,
|
2010-11-17 11:05:41 +01:00
|
|
|
.bcdDevice = 0,
|
|
|
|
.iManufacturer = STR_MANUFACTURER,
|
|
|
|
.iProduct = STR_PRODUCT,
|
|
|
|
.iSerialNumber = STR_SERIALNUMBER,
|
|
|
|
},
|
2012-08-28 17:29:15 +02:00
|
|
|
.full = &desc_device_full,
|
|
|
|
.high = &desc_device_high,
|
|
|
|
.super = &desc_device_super,
|
|
|
|
.str = desc_strings,
|
2006-05-26 01:58:51 +02:00
|
|
|
};
|
|
|
|
|
2011-07-13 12:32:06 +02:00
|
|
|
static void usb_msd_copy_data(MSDState *s, USBPacket *p)
|
2006-08-29 06:52:16 +02: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 17:15:01 +01:00
|
|
|
len = p->iov.size - p->actual_length;
|
2006-08-29 06:52:16 +02:00
|
|
|
if (len > s->scsi_len)
|
|
|
|
len = s->scsi_len;
|
2012-05-16 15:03:40 +02:00
|
|
|
usb_packet_copy(p, scsi_req_get_buf(s->req) + s->scsi_off, len);
|
2006-08-29 06:52:16 +02:00
|
|
|
s->scsi_len -= len;
|
2012-05-16 15:03:40 +02:00
|
|
|
s->scsi_off += len;
|
2020-05-21 01:53:47 +02:00
|
|
|
if (len > s->data_len) {
|
|
|
|
len = s->data_len;
|
|
|
|
}
|
2006-08-29 06:52:16 +02:00
|
|
|
s->data_len -= len;
|
2010-12-09 23:31:49 +01:00
|
|
|
if (s->scsi_len == 0 || s->data_len == 0) {
|
2011-04-18 15:28:11 +02:00
|
|
|
scsi_req_continue(s->req);
|
2006-08-29 06:52:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-09 10:36:35 +01:00
|
|
|
static void usb_msd_send_status(MSDState *s, USBPacket *p)
|
2006-08-29 06:52:16 +02:00
|
|
|
{
|
2010-12-09 10:36:35 +01:00
|
|
|
int len;
|
2006-08-29 06:52:16 +02:00
|
|
|
|
2020-11-05 14:41:07 +01:00
|
|
|
trace_usb_msd_send_status(s->csw.status, le32_to_cpu(s->csw.tag),
|
|
|
|
p->iov.size);
|
2011-11-21 11:29:27 +01:00
|
|
|
|
2012-03-08 01:41:10 +01:00
|
|
|
assert(s->csw.sig == cpu_to_le32(0x53425355));
|
2011-11-21 11:29:27 +01: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 06:52:16 +02:00
|
|
|
}
|
|
|
|
|
2012-05-22 14:30:20 +02: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. */
|
2020-11-05 14:41:07 +01:00
|
|
|
trace_usb_msd_packet_complete();
|
2012-05-22 14:30:20 +02:00
|
|
|
s->packet = NULL;
|
|
|
|
usb_packet_complete(&s->dev, p);
|
|
|
|
}
|
|
|
|
|
2021-03-12 10:04:22 +01:00
|
|
|
void usb_msd_transfer_data(SCSIRequest *req, uint32_t len)
|
2006-05-26 01:58:51 +02:00
|
|
|
{
|
2011-04-18 12:35:39 +02:00
|
|
|
MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
|
2006-08-29 06:52:16 +02:00
|
|
|
USBPacket *p = s->packet;
|
2006-08-12 03:04:27 +02:00
|
|
|
|
2011-04-18 15:28:11 +02:00
|
|
|
assert((s->mode == USB_MSDM_DATAOUT) == (req->cmd.mode == SCSI_XFER_TO_DEV));
|
2011-05-20 20:18:07 +02:00
|
|
|
s->scsi_len = len;
|
2012-05-16 15:03:40 +02:00
|
|
|
s->scsi_off = 0;
|
2006-08-29 06:52:16 +02:00
|
|
|
if (p) {
|
2011-07-13 12:32:06 +02: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 17:15:01 +01:00
|
|
|
if (p && p->actual_length == p->iov.size) {
|
|
|
|
p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */
|
2012-05-22 14:30:20 +02:00
|
|
|
usb_msd_packet_complete(s);
|
2006-08-29 06:52:16 +02:00
|
|
|
}
|
2006-08-12 03:04:27 +02:00
|
|
|
}
|
2006-05-26 01:58:51 +02:00
|
|
|
}
|
|
|
|
|
2021-03-12 10:04:22 +01:00
|
|
|
void usb_msd_command_complete(SCSIRequest *req, size_t resid)
|
2011-04-22 12:27:30 +02:00
|
|
|
{
|
|
|
|
MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
|
|
|
|
USBPacket *p = s->packet;
|
|
|
|
|
2020-11-16 19:40:36 +01:00
|
|
|
trace_usb_msd_cmd_complete(req->status, req->tag);
|
2011-11-21 11:29:27 +01:00
|
|
|
|
|
|
|
s->csw.sig = cpu_to_le32(0x53425355);
|
2011-11-21 11:36:17 +01:00
|
|
|
s->csw.tag = cpu_to_le32(req->tag);
|
2012-05-14 14:00:02 +02:00
|
|
|
s->csw.residue = cpu_to_le32(s->data_len);
|
2020-11-16 19:40:36 +01:00
|
|
|
s->csw.status = req->status != 0;
|
2011-11-21 11:29:27 +01:00
|
|
|
|
2011-04-22 12:27:30 +02: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 11:38:13 +02:00
|
|
|
} else if (s->mode == USB_MSDM_CSW) {
|
|
|
|
usb_msd_send_status(s, p);
|
|
|
|
s->mode = USB_MSDM_CBW;
|
2011-04-22 12:27:30 +02: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 17:15:01 +01:00
|
|
|
int len = (p->iov.size - p->actual_length);
|
2011-07-13 12:32:06 +02:00
|
|
|
usb_packet_skip(p, len);
|
2020-05-21 01:53:47 +02:00
|
|
|
if (len > s->data_len) {
|
|
|
|
len = s->data_len;
|
|
|
|
}
|
2011-07-13 12:32:06 +02:00
|
|
|
s->data_len -= len;
|
2011-04-22 12:27:30 +02: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 17:15:01 +01:00
|
|
|
p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */
|
2012-05-22 14:30:20 +02:00
|
|
|
usb_msd_packet_complete(s);
|
2011-04-22 12:27:30 +02:00
|
|
|
} else if (s->data_len == 0) {
|
|
|
|
s->mode = USB_MSDM_CSW;
|
|
|
|
}
|
|
|
|
scsi_req_unref(req);
|
|
|
|
s->req = NULL;
|
|
|
|
}
|
|
|
|
|
2021-03-12 10:04:22 +01:00
|
|
|
void usb_msd_request_cancelled(SCSIRequest *req)
|
2011-04-18 22:53:08 +02:00
|
|
|
{
|
|
|
|
MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
|
|
|
|
|
2020-11-05 14:41:07 +01:00
|
|
|
trace_usb_msd_cmd_cancel(req->tag);
|
|
|
|
|
2011-04-18 22:53:08 +02:00
|
|
|
if (req == s->req) {
|
2020-11-05 14:41:10 +01:00
|
|
|
s->csw.sig = cpu_to_le32(0x53425355);
|
|
|
|
s->csw.tag = cpu_to_le32(req->tag);
|
|
|
|
s->csw.status = 1; /* error */
|
|
|
|
|
2011-04-18 22:53:08 +02:00
|
|
|
scsi_req_unref(s->req);
|
|
|
|
s->req = NULL;
|
|
|
|
s->scsi_len = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 10:04:22 +01:00
|
|
|
void usb_msd_handle_reset(USBDevice *dev)
|
2006-05-26 01:58:51 +02:00
|
|
|
{
|
|
|
|
MSDState *s = (MSDState *)dev;
|
|
|
|
|
2020-11-05 14:41:07 +01:00
|
|
|
trace_usb_msd_reset();
|
2012-01-04 18:13:54 +01: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 17:15:01 +01:00
|
|
|
s->packet->status = USB_RET_STALL;
|
2012-05-22 14:30:20 +02:00
|
|
|
usb_msd_packet_complete(s);
|
2012-01-04 18:13:54 +01:00
|
|
|
}
|
|
|
|
|
2021-03-12 10:49:54 +01:00
|
|
|
memset(&s->csw, 0, sizeof(s->csw));
|
2006-05-26 01:58:51 +02: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 17:15:01 +01:00
|
|
|
static void usb_msd_handle_control(USBDevice *dev, USBPacket *p,
|
2011-02-02 16:33:13 +01:00
|
|
|
int request, int value, int index, int length, uint8_t *data)
|
2006-05-26 01:58:51 +02:00
|
|
|
{
|
|
|
|
MSDState *s = (MSDState *)dev;
|
2013-01-14 15:29:44 +01:00
|
|
|
SCSIDevice *scsi_dev;
|
|
|
|
int ret, maxlun;
|
2006-05-26 01:58:51 +02:00
|
|
|
|
2011-02-02 16:33:13 +01:00
|
|
|
ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
|
2010-11-17 11:05:41 +01: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 17:15:01 +01:00
|
|
|
return;
|
2010-11-17 11:05:41 +01:00
|
|
|
}
|
|
|
|
|
2006-05-26 01:58:51 +02:00
|
|
|
switch (request) {
|
|
|
|
case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
|
2010-03-10 10:45:12 +01:00
|
|
|
break;
|
2006-05-26 01:58:51 +02:00
|
|
|
/* Class specific requests. */
|
2010-03-14 12:19:03 +01:00
|
|
|
case ClassInterfaceOutRequest | MassStorageReset:
|
2006-05-26 01:58:51 +02:00
|
|
|
/* Reset state ready for the next CBW. */
|
|
|
|
s->mode = USB_MSDM_CBW;
|
|
|
|
break;
|
2010-03-14 12:19:03 +01:00
|
|
|
case ClassInterfaceRequest | GetMaxLun:
|
2013-01-14 15:29:44 +01: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 14:41:07 +01:00
|
|
|
trace_usb_msd_maxlun(maxlun);
|
2013-01-14 15:29:44 +01: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 17:15:01 +01:00
|
|
|
p->actual_length = 1;
|
2006-05-26 01:58:51 +02: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 17:15:01 +01:00
|
|
|
p->status = USB_RET_STALL;
|
2006-05-26 01:58:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-16 10:34:53 +02:00
|
|
|
static void usb_msd_cancel_io(USBDevice *dev, USBPacket *p)
|
2006-08-12 03:04:27 +02:00
|
|
|
{
|
2015-05-06 14:55:32 +02:00
|
|
|
MSDState *s = USB_STORAGE_DEV(dev);
|
2011-09-02 13:05:13 +02:00
|
|
|
|
2012-04-18 12:08:29 +02:00
|
|
|
assert(s->packet == p);
|
|
|
|
s->packet = NULL;
|
|
|
|
|
2011-09-02 13:05:13 +02:00
|
|
|
if (s->req) {
|
|
|
|
scsi_req_cancel(s->req);
|
|
|
|
}
|
2006-08-12 03:04:27 +02: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 17:15:01 +01:00
|
|
|
static void usb_msd_handle_data(USBDevice *dev, USBPacket *p)
|
2006-05-26 01:58:51 +02:00
|
|
|
{
|
|
|
|
MSDState *s = (MSDState *)dev;
|
2011-11-21 11:36:17 +01:00
|
|
|
uint32_t tag;
|
2006-05-26 01:58:51 +02:00
|
|
|
struct usb_msd_cbw cbw;
|
2012-01-12 13:23:01 +01:00
|
|
|
uint8_t devep = p->ep->nr;
|
2013-01-14 15:29:44 +01:00
|
|
|
SCSIDevice *scsi_dev;
|
2013-01-22 14:17:05 +01:00
|
|
|
uint32_t len;
|
2006-05-26 01:58:51 +02:00
|
|
|
|
2006-08-12 03:04:27 +02:00
|
|
|
switch (p->pid) {
|
2006-05-26 01:58:51 +02:00
|
|
|
case USB_TOKEN_OUT:
|
|
|
|
if (devep != 2)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
switch (s->mode) {
|
|
|
|
case USB_MSDM_CBW:
|
2011-07-13 12:32:06 +02:00
|
|
|
if (p->iov.size != 31) {
|
2014-09-19 08:48:30 +02:00
|
|
|
error_report("usb-msd: Bad CBW size");
|
2006-05-26 01:58:51 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2011-07-13 12:32:06 +02:00
|
|
|
usb_packet_copy(p, &cbw, 31);
|
2006-05-26 01:58:51 +02:00
|
|
|
if (le32_to_cpu(cbw.sig) != 0x43425355) {
|
2014-09-19 08:48:30 +02:00
|
|
|
error_report("usb-msd: Bad signature %08x",
|
|
|
|
le32_to_cpu(cbw.sig));
|
2006-05-26 01:58:51 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2013-01-14 15:29:44 +01:00
|
|
|
scsi_dev = scsi_device_find(&s->bus, 0, 0, cbw.lun);
|
|
|
|
if (scsi_dev == NULL) {
|
2014-09-19 08:48:30 +02:00
|
|
|
error_report("usb-msd: Bad LUN %d", cbw.lun);
|
2006-05-26 01:58:51 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2011-11-21 11:36:17 +01:00
|
|
|
tag = le32_to_cpu(cbw.tag);
|
2006-05-26 01:58:51 +02: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 14:41:07 +01:00
|
|
|
trace_usb_msd_cmd_submit(cbw.lun, tag, cbw.flags,
|
|
|
|
cbw.cmd_len, s->data_len);
|
2012-05-14 14:00:02 +02:00
|
|
|
assert(le32_to_cpu(s->csw.residue) == 0);
|
2011-05-04 16:49:56 +02:00
|
|
|
s->scsi_len = 0;
|
2013-01-14 15:29:44 +01:00
|
|
|
s->req = scsi_req_new(scsi_dev, tag, cbw.lun, cbw.cmd, NULL);
|
2020-11-05 14:41:08 +01:00
|
|
|
if (s->commandlog) {
|
|
|
|
scsi_req_print(s->req);
|
|
|
|
}
|
2013-01-22 14:17:05 +01:00
|
|
|
len = scsi_req_enqueue(s->req);
|
|
|
|
if (len) {
|
2011-04-18 15:28:11 +02:00
|
|
|
scsi_req_continue(s->req);
|
2006-08-29 06:52:16 +02:00
|
|
|
}
|
2006-05-26 01:58:51 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_MSDM_DATAOUT:
|
2020-11-05 14:41:07 +01:00
|
|
|
trace_usb_msd_data_out(p->iov.size, s->data_len);
|
2011-07-13 12:32:06 +02:00
|
|
|
if (p->iov.size > s->data_len) {
|
2006-05-26 01:58:51 +02:00
|
|
|
goto fail;
|
2011-07-13 12:32:06 +02:00
|
|
|
}
|
2006-05-26 01:58:51 +02:00
|
|
|
|
2006-08-29 06:52:16 +02:00
|
|
|
if (s->scsi_len) {
|
2011-07-13 12:32:06 +02:00
|
|
|
usb_msd_copy_data(s, p);
|
2006-08-29 06:52:16 +02:00
|
|
|
}
|
2012-05-14 14:00:02 +02: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 17:15:01 +01:00
|
|
|
int len = p->iov.size - p->actual_length;
|
2011-07-13 12:32:06 +02:00
|
|
|
if (len) {
|
|
|
|
usb_packet_skip(p, len);
|
2020-05-21 01:53:47 +02:00
|
|
|
if (len > s->data_len) {
|
|
|
|
len = s->data_len;
|
|
|
|
}
|
2011-07-13 12:32:06 +02:00
|
|
|
s->data_len -= len;
|
|
|
|
if (s->data_len == 0) {
|
|
|
|
s->mode = USB_MSDM_CSW;
|
|
|
|
}
|
|
|
|
}
|
2006-08-29 06:52:16 +02: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 17:15:01 +01:00
|
|
|
if (p->actual_length < p->iov.size) {
|
2020-11-05 14:41:07 +01:00
|
|
|
trace_usb_msd_packet_async();
|
2006-08-12 03:04:27 +02: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 17:15:01 +01:00
|
|
|
p->status = USB_RET_ASYNC;
|
2006-08-12 03:04:27 +02:00
|
|
|
}
|
2006-05-26 01:58:51 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_TOKEN_IN:
|
|
|
|
if (devep != 1)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
switch (s->mode) {
|
2006-08-29 06:52:16 +02:00
|
|
|
case USB_MSDM_DATAOUT:
|
2011-07-13 12:32:06 +02:00
|
|
|
if (s->data_len != 0 || p->iov.size < 13) {
|
2006-08-29 06:52:16 +02:00
|
|
|
goto fail;
|
2011-07-13 12:32:06 +02:00
|
|
|
}
|
2006-08-29 06:52:16 +02:00
|
|
|
/* Waiting for SCSI write to complete. */
|
2020-11-05 14:41:07 +01:00
|
|
|
trace_usb_msd_packet_async();
|
2006-08-29 06:52:16 +02: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 17:15:01 +01:00
|
|
|
p->status = USB_RET_ASYNC;
|
2006-08-29 06:52:16 +02:00
|
|
|
break;
|
|
|
|
|
2006-05-26 01:58:51 +02:00
|
|
|
case USB_MSDM_CSW:
|
2011-07-13 12:32:06 +02:00
|
|
|
if (p->iov.size < 13) {
|
2006-05-26 01:58:51 +02:00
|
|
|
goto fail;
|
2011-07-13 12:32:06 +02:00
|
|
|
}
|
2006-05-26 01:58:51 +02:00
|
|
|
|
2011-11-21 14:01:26 +01:00
|
|
|
if (s->req) {
|
|
|
|
/* still in flight */
|
2020-11-05 14:41:07 +01:00
|
|
|
trace_usb_msd_packet_async();
|
2011-11-21 14:01:26 +01: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 17:15:01 +01:00
|
|
|
p->status = USB_RET_ASYNC;
|
2011-11-21 14:01:26 +01:00
|
|
|
} else {
|
|
|
|
usb_msd_send_status(s, p);
|
|
|
|
s->mode = USB_MSDM_CBW;
|
|
|
|
}
|
2006-05-26 01:58:51 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_MSDM_DATAIN:
|
2020-11-05 14:41:07 +01:00
|
|
|
trace_usb_msd_data_in(p->iov.size, s->data_len, s->scsi_len);
|
2006-08-29 06:52:16 +02:00
|
|
|
if (s->scsi_len) {
|
2011-07-13 12:32:06 +02:00
|
|
|
usb_msd_copy_data(s, p);
|
2006-08-29 06:52:16 +02:00
|
|
|
}
|
2012-05-14 14:00:02 +02: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 17:15:01 +01:00
|
|
|
int len = p->iov.size - p->actual_length;
|
2011-07-13 12:32:06 +02:00
|
|
|
if (len) {
|
|
|
|
usb_packet_skip(p, len);
|
2020-05-21 01:53:47 +02:00
|
|
|
if (len > s->data_len) {
|
|
|
|
len = s->data_len;
|
|
|
|
}
|
2011-07-13 12:32:06 +02:00
|
|
|
s->data_len -= len;
|
|
|
|
if (s->data_len == 0) {
|
|
|
|
s->mode = USB_MSDM_CSW;
|
|
|
|
}
|
|
|
|
}
|
2006-08-29 06:52:16 +02:00
|
|
|
}
|
2020-07-13 08:27:12 +02:00
|
|
|
if (p->actual_length < p->iov.size && s->mode == USB_MSDM_DATAIN) {
|
2020-11-05 14:41:07 +01:00
|
|
|
trace_usb_msd_packet_async();
|
2006-08-12 03:04:27 +02: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 17:15:01 +01:00
|
|
|
p->status = USB_RET_ASYNC;
|
2006-08-12 03:04:27 +02:00
|
|
|
}
|
2006-05-26 01:58:51 +02: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 17:15:01 +01:00
|
|
|
p->status = USB_RET_STALL;
|
2006-05-26 01:58:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 10:04:22 +01:00
|
|
|
void *usb_msd_load_request(QEMUFile *f, SCSIRequest *req)
|
2012-05-14 13:56:40 +02: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 14:58:41 +01:00
|
|
|
static const VMStateDescription vmstate_usb_msd = {
|
|
|
|
.name = "usb-storage",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2014-04-16 13:31:26 +02:00
|
|
|
.fields = (VMStateField[]) {
|
2010-12-10 14:58:41 +01:00
|
|
|
VMSTATE_USB_DEVICE(dev, MSDState),
|
2012-05-14 13:56:40 +02: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 14:58:41 +01:00
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-06 14:55:32 +02:00
|
|
|
static void usb_msd_class_initfn_common(ObjectClass *klass, void *data)
|
2011-12-15 21:53:10 +01:00
|
|
|
{
|
2011-12-08 04:34:16 +01:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2011-12-15 21:53:10 +01: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 16:17:45 +02:00
|
|
|
set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
|
2011-12-08 04:34:16 +01:00
|
|
|
dc->fw_name = "storage";
|
|
|
|
dc->vmsd = &vmstate_usb_msd;
|
2013-01-14 15:29:44 +01:00
|
|
|
}
|
|
|
|
|
2015-05-06 14:55:32 +02: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 15:20:55 +01:00
|
|
|
static void usb_msd_register_types(void)
|
2009-08-31 14:23:59 +02:00
|
|
|
{
|
2015-05-06 14:55:32 +02:00
|
|
|
type_register_static(&usb_storage_dev_type_info);
|
2009-08-31 14:23:59 +02:00
|
|
|
}
|
2012-02-09 15:20:55 +01:00
|
|
|
|
|
|
|
type_init(usb_msd_register_types)
|