mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Igor Skalkin <igor.skalkin@oss.qualcomm.com>
To: "Michael S . Tsirkin" <mst@redhat.com>,
	Jason Wang <jasowangio@gmail.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: virtualization@lists.linux.dev, linux-usb@vger.kernel.org,
	linux-kernel@vger.kernel.org, Eugenio Perez <eperezma@redhat.com>,
	Xuan Zhuo <xuanzhuo@linux.alibaba.com>,
	Vasilii Ianikeev <vasilii.ianikeev@oss.qualcomm.com>,
	Aiswarya Cyriac <aiswarya.cyriac@oss.qualcomm.com>,
	Anton Yakovlev <anton.yakovlev@oss.qualcomm.com>,
	Trilok Soni <trilok.soni@oss.qualcomm.com>,
	Matias Ezequiel Vara Larsen <mvaralar@redhat.com>,
	Igor Skalkin <igor.skalkin@oss.qualcomm.com>
Subject: [RFC PATCH v2 4/7] virtio-usb: add device role (USB Device Controller) support
Date: Tue,  1 Dec 2026 18:35:33 +0100	[thread overview]
Message-ID: <3f0c8f183e321f032bdf30bb06f6837dadb6bc94.1796144390.git.igor.skalkin@oss.qualcomm.com> (raw)
In-Reply-To: <cover.1796144390.git.igor.skalkin@oss.qualcomm.com>

From: Aiswarya Cyriac <aiswarya.cyriac@oss.qualcomm.com>

Add the device (UDC) role: implement the usb_gadget_ops and
usb_ep_ops callback sets, handle BIND/UNBIND/DISCONNECTED events
from the host backend and forward setup/suspend/resume/reset
notifications to the UDC core, and extend the protocol header with
the endpoint and command messages the device role needs.

Each device-role virtual port gets its own struct virtio_usb_dc and
UDC registration, one per vports[] entry, instead of a single global
device controller - mirroring the per-VP model host role already
uses, since a device-role port is just as independent as a host-role
one. Each gadget gets a name derived from its port index, and its
endpoint name buffer is allocated dynamically instead of using a
stack variable that would outlive the calling function.

This driver virtualizes an entire UDC per device-role port: each port
maps 1:1 to a single struct usb_gadget/UDC instance owned by exactly
one guest. A separate approach - USB function-level virtualization,
where a single physical UDC hosts a composite gadget whose
individual functions are each routed to a different guest - is
currently being explored and is out of scope for this driver.

Hardware endpoint names are used directly as the virtual endpoint
names, since the endpoint name has a relation with the endpoint
address.

Keep the device lifecycle separate from link-state notifications
from the start: BIND registers the UDC and UNBIND tears it down,
while DISCONNECTED is only a transient link-state notification (a
disconnect may be followed by SETUP without a new bind, so it must
not free UDC resources). This aligns the guest-side lifecycle with
the host-side callbacks: BIND -> (SETUP / DISCONNECT / RESET /
SUSPEND / RESUME)* -> UNBIND.

Each port also registers its UDC under its own intermediate
platform_device instead of the shared virtio_device, created on BIND
and torn down on the matching UNBIND. usb_add_gadget_udc() derives
the UDC's class name from its parent kobject's name, and every
device-role port registering under the same shared parent would
collide.

With both host_role and device_role negotiated, a port's actual role
is ambiguous until a later commit adds a per-port role query -
every port defaults to DEVICE for now as a placeholder.

Parse the gadget's max_speed from the BIND event's new max_speed
field instead of hardcoding USB_SPEED_HIGH, clamping to a supported
set (LOW/FULL/HIGH/SUPER/SUPER_PLUS) and defaulting to HIGH on
invalid data, so the EP0 max packet size set_speed() configures
matches what the host backend actually negotiated instead of always
assuming High-Speed.

Every submission of a usb_request gets a new tag from the per-device
counter the host role uses. A gadget driver queues the same request
again with the same address, so the address cannot tell a DEV_CANCEL
for an earlier submission from one for the current one.

The USB gadget API's usb_ep_ops/usb_gadget_ops callbacks are documented
as atomic - they must not sleep. This driver's endpoint and command
handling needs to send a virtio command and wait for the backend's
response, which is exactly what those callbacks cannot do.

The driver therefore honors the atomic contract itself, without
requiring any change to the gadget core.

The endpoint lifecycle is an explicit state machine (EP_DISABLED,
EP_ENABLING, EP_ENABLED, EP_HALTED) with per-UDC/per-request work
items and a pending-request queue, so every usb_ep_ops/usb_gadget_ops
callback returns immediately and the actual virtio round trip happens
asynchronously on a workqueue. ep_enable()/ep_disable()/
set_selfpowered()/vbus_draw() all follow this split-phase pattern;
ep_queue()/ep_dequeue() are built on top of the same state machine.
ep_enable() validates the endpoint and SuperSpeed companion
descriptors under the UDC lock, and returns -EBUSY for an endpoint
that is not disabled.

ep_dequeue() schedules a cancel work that sends DEV_CANCEL. The work
holds its own reference on the request, since the function driver can
complete and free the request before the work runs. A request is on
req_queue before it reaches the data virtqueue, and a DEV_CANCEL sent
in that window would not match anything on the device side, would be
answered with success and dropped. So the cancel work only records the
cancel while the request is not submitted yet, and do_real_queue()
schedules it again once the request is queued. The work also records
which submission it was asked to cancel and does nothing if the
request completed and was queued again in the meantime;
do_real_queue() likewise only updates the state if it still belongs
to the submission it queued.

Completion keeps -ECONNRESET/-ESHUTDOWN statuses set by dequeue or
disable rather than overwriting them, and a request that completes
before its queue path has finished is given back exactly once. SETUP
handling waits for in-flight EP0 requests and holds new EP0 requests
while any endpoint is still EP_ENABLING, so control transfers never
overlap; if EP0 does not become idle in time, EP0 is halted.

Signed-off-by: Aiswarya Cyriac <aiswarya.cyriac@oss.qualcomm.com>
Co-developed-by: Anton Yakovlev <anton.yakovlev@oss.qualcomm.com>
Signed-off-by: Anton Yakovlev <anton.yakovlev@oss.qualcomm.com>
Co-developed-by: Igor Skalkin <igor.skalkin@oss.qualcomm.com>
Signed-off-by: Igor Skalkin <igor.skalkin@oss.qualcomm.com>
---
 drivers/usb/virtio_usb/Makefile     |    3 +-
 drivers/usb/virtio_usb/controller.c |   75 +-
 drivers/usb/virtio_usb/controller.h |   17 +
 drivers/usb/virtio_usb/device.c     | 2016 +++++++++++++++++++++++++++
 drivers/usb/virtio_usb/device.h     |  126 ++
 include/uapi/linux/virtio_usb.h     |   45 +-
 6 files changed, 2269 insertions(+), 13 deletions(-)
 create mode 100644 drivers/usb/virtio_usb/device.c
 create mode 100644 drivers/usb/virtio_usb/device.h

diff --git a/drivers/usb/virtio_usb/Makefile b/drivers/usb/virtio_usb/Makefile
index c6951c68b8c2..ac382d81d8af 100644
--- a/drivers/usb/virtio_usb/Makefile
+++ b/drivers/usb/virtio_usb/Makefile
@@ -4,4 +4,5 @@ obj-$(CONFIG_USB_VIRTIO)	+= virtio-usb.o
 
 virtio-usb-y := controller.o \
 		vq_common.o \
-		host.o
+		host.o \
+		device.o
diff --git a/drivers/usb/virtio_usb/controller.c b/drivers/usb/virtio_usb/controller.c
index eb343f026a46..b463513b2da4 100644
--- a/drivers/usb/virtio_usb/controller.c
+++ b/drivers/usb/virtio_usb/controller.c
@@ -10,6 +10,7 @@
 
 #include "controller.h"
 #include "host.h"
+#include "device.h"
 #include "vq_common.h"
 
 /**
@@ -72,7 +73,8 @@ static int virtio_usb_validate(struct virtio_device *vdev)
 		return -EINVAL;
 	}
 
-	if (!virtio_has_feature(vdev, VIRTIO_USB_F_HOST)) {
+	if (!virtio_has_feature(vdev, VIRTIO_USB_F_HOST) &&
+	    !virtio_has_feature(vdev, VIRTIO_USB_F_DEVICE)) {
 		dev_err(&vdev->dev,
 			"device should support at least one usb role\n");
 		return -EINVAL;
@@ -113,17 +115,40 @@ static int virtio_usb_probe(struct virtio_device *vdev)
 	if (virtio_has_feature(vdev, VIRTIO_USB_F_HOST))
 		vusb->host_role = 1;
 
-	/* Only host_role exists so far, so every port is unambiguously a
-	 * host-role port. Later commits (device role, per-port role
-	 * query) will replace this with real per-port role resolution.
+	if (virtio_has_feature(vdev, VIRTIO_USB_F_DEVICE))
+		vusb->device_role = 1;
+
+	/* Only allocate/negotiate the virtqueue triplets this instance
+	 * actually needs: HOST_* only exists when host_role is negotiated,
+	 * DEV_* only when device_role is negotiated. A pure single-role
+	 * instance therefore has exactly VIRTIO_USB_VQ_HOST_MAX (3) or
+	 * VIRTIO_USB_VQ_DEV_MAX (3) virtqueues, not a fixed layout - queues
+	 * that don't exist on the wire must not be created, since a peer
+	 * with no host-role VP has no host command/event/data queues to
+	 * negotiate at all.
 	 */
 	vusb->host_vq_base = -1;
+	vusb->dev_vq_base = -1;
+
 	if (vusb->host_role) {
 		vusb->host_vq_base = nvqs;
 		nvqs += VIRTIO_USB_VQ_HOST_MAX;
+	}
+	if (vusb->device_role) {
+		vusb->dev_vq_base = nvqs;
+		nvqs += VIRTIO_USB_VQ_DEV_MAX;
+	}
 
-		for (i = 0; i < vusb->nports; i++)
+	/* Resolve every port's role. With only one role negotiated, every
+	 * port unambiguously has that role. With both negotiated, a port's
+	 * own role is ambiguous until a later commit adds a per-port
+	 * role query - default to DEVICE for now as a placeholder.
+	 */
+	for (i = 0; i < vusb->nports; i++) {
+		if (vusb->host_role && !vusb->device_role)
 			vusb->vports[i].role = VIRTIO_USB_ROLE_HOST;
+		else if (vusb->device_role)
+			vusb->vports[i].role = VIRTIO_USB_ROLE_DEVICE;
 	}
 
 	vusb->vqueues = devm_kcalloc(&vdev->dev, nvqs, sizeof(*vusb->vqueues),
@@ -145,6 +170,18 @@ static int virtio_usb_probe(struct virtio_device *vdev)
 				host_vqueues[i].stop;
 		}
 
+	if (vusb->dev_vq_base >= 0)
+		for (i = 0; i < VIRTIO_USB_VQ_DEV_MAX; i++) {
+			vusb->vqueues[vusb->dev_vq_base + i].name =
+				dev_vqueues[i].name;
+			vusb->vqueues[vusb->dev_vq_base + i].callback =
+				dev_vqueues[i].callback;
+			vusb->vqueues[vusb->dev_vq_base + i].process =
+				dev_vqueues[i].process;
+			vusb->vqueues[vusb->dev_vq_base + i].stop =
+				dev_vqueues[i].stop;
+		}
+
 	rc = virtio_usb_find_vqs(vusb);
 	if (rc) {
 		dev_err(&vdev->dev, "%s virtio_usb_find_vqs() error(%d)\n",
@@ -180,6 +217,30 @@ static int virtio_usb_probe(struct virtio_device *vdev)
 		}
 	}
 
+	if (vusb->device_role) {
+		INIT_WORK(&vusb->vq_dev_data_rx_work, virtio_usb_dc_data_work);
+		INIT_WORK(&vusb->vq_dev_event_work, virtio_usb_dc_event_work);
+
+		for (i = 0; i < vusb->nports; i++) {
+			if (vusb->vports[i].role != VIRTIO_USB_ROLE_DEVICE)
+				continue;
+			rc = virtio_usb_dc_init(vusb, i);
+			if (rc) {
+				dev_err(&vdev->dev,
+					"%s virtio_usb_dc_init() port=%d error(%d)\n",
+					__func__, i, rc);
+				goto on_error;
+			}
+		}
+		rc = virtio_usb_dc_event_populate(vusb);
+		if (rc) {
+			dev_err(&vdev->dev,
+				"%s virtio_usb_dc_event_populate() error(%d)\n",
+				__func__, rc);
+			goto on_error;
+		}
+	}
+
 	virtio_device_ready(vdev);
 
 	return rc;
@@ -209,6 +270,9 @@ static void virtio_usb_remove(struct virtio_device *vdev)
 			virtio_usb_hc_vp_deinit(vusb, i);
 	}
 
+	if (vusb->device_role && vusb->vports)
+		virtio_usb_dc_deinit(vusb);
+
 	virtio_reset_device(vdev);
 
 	vdev->config->del_vqs(vdev);
@@ -216,6 +280,7 @@ static void virtio_usb_remove(struct virtio_device *vdev)
 
 static const unsigned int virtio_usb_features[] = {
 	VIRTIO_USB_F_HOST,
+	VIRTIO_USB_F_DEVICE,
 };
 
 static const struct virtio_device_id id_table[] = {
diff --git a/drivers/usb/virtio_usb/controller.h b/drivers/usb/virtio_usb/controller.h
index dfa16a3d42a8..446a9274871c 100644
--- a/drivers/usb/virtio_usb/controller.h
+++ b/drivers/usb/virtio_usb/controller.h
@@ -17,21 +17,26 @@
 
 /* Forward declaration - full definition in host.h */
 struct virtio_usb_hc_vp;
+/* Forward declaration - full definition in device.h */
+struct virtio_usb_dc;
 
 #define VIRTIO_USB_VQ_COMMAND_IDX 0
 #define VIRTIO_USB_VQ_EVENT_IDX 1
 #define VIRTIO_USB_VQ_DATA_IDX 2
 
 #define VIRTIO_USB_VQ_HOST_MAX 3
+#define VIRTIO_USB_VQ_DEV_MAX 3
 
 /**
  * struct virtio_usb_port - Per-virtual-port state.
  * @role: Role of this port (VIRTIO_USB_ROLE_HOST or _DEVICE).
  * @vhc: Host controller - non-NULL when role is HOST.
+ * @vudc: Device controller - non-NULL when role is DEVICE.
  */
 struct virtio_usb_port {
 	unsigned int role;
 	struct virtio_usb_hc_vp *vhc;
+	struct virtio_usb_dc *vudc;
 };
 
 /**
@@ -42,14 +47,22 @@ struct virtio_usb_port {
  * @nports: number of supported ports
  * @nvqs: number of virtqueues for the device
  * @host_role: flag indicating support for host role
+ * @device_role: flag indicating support for device role
  * @host_vq_base: index into vqueues[] where the HOST_COMMAND/EVENT/DATA
  *                triplet starts, or -1 if this instance has no host-role
  *                VP (in which case those queues do not exist on the wire
  *                and must not be negotiated).
+ * @dev_vq_base: index into vqueues[] where the DEV_COMMAND/EVENT/DATA
+ *               triplet starts, or -1 if this instance has no
+ *               device-role VP.
  * @vq_host_data_rx_work: Kernel work draining the host data queue, shared
  *                        across every host-role VP.
  * @vq_host_evt_work: Kernel work draining the host event queue, shared
  *                     across every host-role VP.
+ * @vq_dev_data_rx_work: Kernel work draining the device data queue, shared
+ *                       across every device-role port.
+ * @vq_dev_event_work: Kernel work draining the device event queue, shared
+ *                      across every device-role port.
  * @data_tag_seq: Source of data request tags, see virtio_usb_data_tag_next().
  */
 struct virtio_usb {
@@ -59,9 +72,13 @@ struct virtio_usb {
 	unsigned int nports;
 	u32 nvqs;
 	bool host_role;
+	bool device_role;
 	int host_vq_base;
+	int dev_vq_base;
 	struct work_struct vq_host_data_rx_work;
 	struct work_struct vq_host_evt_work;
+	struct work_struct vq_dev_data_rx_work;
+	struct work_struct vq_dev_event_work;
 	atomic64_t data_tag_seq;
 };
 
diff --git a/drivers/usb/virtio_usb/device.c b/drivers/usb/virtio_usb/device.c
new file mode 100644
index 000000000000..af82a0e7e27e
--- /dev/null
+++ b/drivers/usb/virtio_usb/device.c
@@ -0,0 +1,2016 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * virtio_usb: VirtIO USB device
+ *
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ */
+
+#include <linux/virtio_usb.h>
+
+#include "controller.h"
+#include "device.h"
+
+#define GADGET_NAME "virtio_usb_dc"
+#define VIRTIO_USB_EP0_IDLE_TIMEOUT_MS 5000
+
+/**
+ * struct virtio_usb_dc_priv - Device controller data priv
+ * Structure for UDC related data in data messages
+ * @req: usb_request structure
+ * @vep: virtio usb endpoint
+ * @vreq: virtio-usb request
+ * @cancel_work: Sends DEV_CANCEL for this request, see
+ *               virtio_usb_dc_cancel_work().
+ * @completed_early: The request was given back by virtio_ep_disable()
+ *                   while still in flight; the completion handler only
+ *                   drops the last reference.
+ * @submitted: True once the request has been placed on the data
+ *             virtqueue. Protected by vudc->lock.
+ * @cancel_pending: Set by virtio_usb_dc_cancel_work() when the request had
+ *                  not reached the data virtqueue yet; do_real_queue()
+ *                  reschedules the cancel work once it has. Protected by
+ *                  vudc->lock.
+ * @tag: Tag of the current submission, see virtio_usb_data_tag_next().
+ *       Protected by vudc->lock.
+ * @cancel_tag: Tag of the submission virtio_ep_dequeue() asked to cancel.
+ *              The cancel work only acts while it still equals @tag.
+ *              Protected by vudc->lock.
+ */
+struct virtio_usb_dc_priv {
+	struct usb_request req;
+	struct virtio_usb_ep *vep;
+	struct virtio_usb_data *vreq;
+	struct work_struct cancel_work;
+	bool completed_early;
+	bool submitted;
+	bool cancel_pending;
+	u64 tag;
+	u64 cancel_tag;
+};
+
+/**
+ * usb_ep_to_virtio_ep() - Get the virtio usb endpoint from usb endpoint
+ * @ep: usb endpoint
+ *
+ * Context: Any context.
+ * Return: Pointer to virtio_usb_ep
+ */
+static struct virtio_usb_ep *usb_ep_to_virtio_ep(struct usb_ep *ep)
+{
+	return container_of(ep, struct virtio_usb_ep, ep);
+}
+
+/**
+ * usb_ep_dir_in() - check if the endpoint has IN direction
+ * @ep: usb endpoint
+ *
+ * Context: Any context.
+ * Return: 1 if direction is USB_DIR_IN else 0
+ */
+static unsigned int usb_ep_dir_in(struct usb_ep *ep)
+{
+	struct virtio_usb_ep *vep = usb_ep_to_virtio_ep(ep);
+	unsigned int direction = 0;
+
+	if (ep->address == 0)
+		direction = vep->setup.bRequestType & USB_DIR_IN ? 1 : 0;
+	else if (ep->desc && ep->desc->bEndpointAddress)
+		direction = usb_endpoint_dir_in(ep->desc) ? 1 : 0;
+
+	return direction;
+}
+
+/**
+ * usb_req_to_virtio_data() - Get the virtio usb data message from usb_request
+ * @req: usb_request structure
+ *
+ * Context: Any context.
+ * Return: virtio usb data message
+ */
+static struct virtio_usb_data *usb_req_to_virtio_data(struct usb_request *req)
+{
+	struct virtio_usb_dc_priv *priv =
+		container_of(req, struct virtio_usb_dc_priv, req);
+
+	return priv->vreq;
+}
+
+static bool virtio_usb_dc_req_queue_empty(struct virtio_usb_ep *vep)
+{
+	struct virtio_usb_dc *vudc = vep->vudc;
+	unsigned long flags;
+	bool empty;
+
+	spin_lock_irqsave(&vudc->lock, flags);
+	empty = list_empty(&vep->req_queue);
+	spin_unlock_irqrestore(&vudc->lock, flags);
+
+	return empty;
+}
+
+static int virtio_usb_dc_wait_ep0_idle(struct virtio_usb_dc *vudc)
+{
+	struct virtio_usb_ep *vep = usb_ep_to_virtio_ep(vudc->gadget.ep0);
+	long ret;
+
+	if (virtio_usb_dc_req_queue_empty(vep))
+		return 0;
+
+	ret = wait_event_timeout(
+		vudc->ep0_idle, virtio_usb_dc_req_queue_empty(vep),
+		msecs_to_jiffies(VIRTIO_USB_EP0_IDLE_TIMEOUT_MS));
+
+	return ret ? 0 : -ETIMEDOUT;
+}
+
+/**
+ * virtio_usb_dc_complete_req() - Completes a usb request
+ * @vreq: virtio usb data message.
+ *
+ * Context: Process context.
+ */
+static void virtio_usb_dc_complete_req(struct virtio_usb_data *vreq)
+{
+	struct virtio_usb_response *response = virtio_usb_data_response(vreq);
+	struct virtio_usb_dc_priv *priv = virtio_usb_data_priv(vreq);
+	unsigned int status = le32_to_cpu(response->status);
+	struct usb_request *req = &priv->req;
+	struct virtio_usb_ep *vep = priv->vep;
+	struct virtio_usb_dc *vudc = vep->vudc;
+	struct virtio_usb_data *vreq_iter;
+	unsigned int found = 0;
+	unsigned long flags;
+	bool wake_ep0 = false;
+	bool early;
+
+	spin_lock_irqsave(&vudc->lock, flags);
+
+	list_for_each_entry(vreq_iter, &vep->req_queue, list) {
+		if (vreq_iter == vreq) {
+			found = 1;
+			break;
+		}
+	}
+	early = priv->completed_early;
+	if (!found) {
+		spin_unlock_irqrestore(&vudc->lock, flags);
+		/* Arrived after ep_disable() already gave back */
+		if (early)
+			virtio_usb_data_unref(vreq);
+		return;
+	}
+	/* Common case - request still belongs to in-flight queue */
+	if (req->status != -ECONNRESET && req->status != -ESHUTDOWN) {
+		req->status = virtio_error_to_usb(status);
+		if (!req->status)
+			req->actual = le32_to_cpu(response->actual_length);
+	}
+
+	list_del_init(&vreq->list);
+	wake_ep0 = !vep->ep_id && list_empty(&vep->req_queue);
+	spin_unlock_irqrestore(&vudc->lock, flags);
+	if (wake_ep0)
+		wake_up(&vudc->ep0_idle);
+
+	/* Normal completion: giveback now */
+	if (!early)
+		usb_gadget_giveback_request(&vep->ep, req);
+
+	/* Drop final reference */
+	virtio_usb_data_unref(vreq);
+}
+
+/**
+ * virtio_usb_dc_data_work() - Worker to get all completed data message
+ * from the virtqueue and call the completion handler.
+ * @work: kernel work to handle data message completion.
+ *
+ * Context: Process context.
+ */
+void virtio_usb_dc_data_work(struct work_struct *work)
+{
+	struct virtio_usb *vusb =
+		container_of(work, struct virtio_usb, vq_dev_data_rx_work);
+	struct virtio_usb_queue *dataq =
+		&vusb->vqueues[vusb->dev_vq_base + VIRTIO_USB_VQ_DATA_IDX];
+	struct virtio_usb_data *vreq;
+	unsigned long flags;
+	unsigned int length;
+
+	spin_lock_irqsave(&dataq->lock, flags);
+	do {
+		virtqueue_disable_cb(dataq->vqueue);
+		while ((vreq = virtqueue_get_buf(dataq->vqueue, &length))) {
+			spin_unlock_irqrestore(&dataq->lock, flags);
+			virtio_usb_dc_complete_req(vreq);
+			spin_lock_irqsave(&dataq->lock, flags);
+		}
+		if (unlikely(virtqueue_is_broken(dataq->vqueue)))
+			break;
+	} while (!virtqueue_enable_cb(dataq->vqueue));
+	spin_unlock_irqrestore(&dataq->lock, flags);
+}
+
+static void virtio_usb_dc_cancel_work(struct work_struct *work);
+/**
+ * virtio_usb_dc_data_alloc() - Allocate and initialize a device controller
+ * data message.
+ * @ep: VirtIO usb device
+ * @gfp: Kernel flags for memory allocation.
+ *
+ * The message will be automatically freed when the ref_count value is 0.
+ *
+ * Context: Any context. May sleep if @gfp flags permit.
+ * Return: Allocated data message on success, NULL on failure.
+ */
+static struct virtio_usb_data *virtio_usb_dc_data_alloc(struct usb_ep *ep,
+							gfp_t gfp)
+{
+	struct virtio_usb_ep *vep = usb_ep_to_virtio_ep(ep);
+	size_t request_size = sizeof(struct virtio_usb_request);
+	size_t response_size = sizeof(struct virtio_usb_response);
+	struct virtio_usb_dc_priv *priv;
+	struct virtio_usb_data *vreq;
+
+	vreq = virtio_usb_data_alloc(request_size, response_size,
+				     sizeof(struct virtio_usb_dc_priv), gfp);
+	if (!vreq)
+		return NULL;
+
+	priv = virtio_usb_data_priv(vreq);
+	priv->vep = vep;
+	priv->vreq = vreq;
+	priv->completed_early = false;
+	INIT_WORK(&priv->cancel_work, virtio_usb_dc_cancel_work);
+	vreq->msg.queue =
+		&vep->vudc->vusb->vqueues[vep->vudc->vusb->dev_vq_base +
+					  VIRTIO_USB_VQ_DATA_IDX];
+
+	return vreq;
+}
+
+/**
+ * virtio_usb_dc_cmd_alloc() - Allocate and initialize the device controller
+ * command message.
+ * @vudc: VirtIO usb device controller
+ * @command: command message
+ * @gfp: Kernel flags for memory allocation.
+ *
+ * The message will be automatically freed when the ref_count value is 0.
+ *
+ * Context: Any context. May sleep if @gfp flags permit.
+ * Return: Allocated command message on success, NULL on failure.
+ */
+static struct virtio_usb_cmd *
+virtio_usb_dc_cmd_alloc(struct virtio_usb_dc *vudc, unsigned int command,
+			gfp_t gfp)
+{
+	size_t request_size = sizeof(struct virtio_usb_dev_cmd_hdr);
+	size_t response_size = sizeof(struct virtio_usb_cmd_status);
+	struct virtio_usb_cmd_status *status;
+	struct virtio_usb_dev_cmd_hdr *hdr;
+	struct virtio_usb_cmd *cmd;
+
+	switch (command) {
+	case VIRTIO_USB_CMD_DEV_GET_ENDPOINT_COUNT:
+		response_size = sizeof(struct virtio_usb_dev_ep_count);
+		break;
+	case VIRTIO_USB_CMD_DEV_GET_FRAME_NUMBER:
+		response_size = sizeof(struct virtio_usb_dev_frame_number);
+		break;
+	case VIRTIO_USB_CMD_DEV_CANCEL:
+		request_size = sizeof(struct virtio_usb_dev_cmd_cancel);
+		break;
+	case VIRTIO_USB_CMD_DEV_VBUS_DRAW:
+	case VIRTIO_USB_CMD_DEV_PULLUP:
+	case VIRTIO_USB_CMD_DEV_EP_SET_HALT:
+	case VIRTIO_USB_CMD_DEV_SET_SELF_POWERED:
+		request_size = sizeof(struct virtio_usb_dev_cmd_set_value);
+		break;
+	}
+
+	cmd = virtio_usb_cmd_alloc(request_size, response_size, gfp);
+	if (!cmd)
+		return NULL;
+
+	hdr = virtio_usb_cmd_request(cmd);
+	status = virtio_usb_cmd_response(cmd);
+
+	hdr->code = cpu_to_le32(command);
+	hdr->port = cpu_to_le16(vudc->port);
+	cmd->msg.queue = &vudc->vusb->vqueues[vudc->vusb->dev_vq_base +
+					      VIRTIO_USB_VQ_COMMAND_IDX];
+	status->code = cpu_to_le32(VIRTIO_USB_S_ERR_CANCELLED);
+	return cmd;
+}
+
+/* Endpoint callbacks */
+
+/**
+ * virtio_usb_dc_schedule_cancel() - Schedule the cancel work of a request.
+ * @vreq: virtio usb data message.
+ *
+ * The cancel work dereferences the request, and the request can be
+ * completed and freed by the function driver (usb_ep_free_request()) before
+ * the work runs. Hold a reference for the work; virtio_usb_dc_cancel_work()
+ * drops it.
+ *
+ * Context: Any context. The caller must hold a reference on @vreq.
+ */
+static void virtio_usb_dc_schedule_cancel(struct virtio_usb_data *vreq)
+{
+	struct virtio_usb_dc_priv *priv = virtio_usb_data_priv(vreq);
+
+	virtio_usb_data_ref(vreq);
+	if (!schedule_work(&priv->cancel_work))
+		/* Already pending, that instance owns a reference. */
+		virtio_usb_data_unref(vreq);
+}
+
+static int do_real_queue(struct virtio_usb_ep *vep, struct usb_request *req)
+{
+	struct virtio_usb_dc *vudc = vep->vudc;
+	struct virtio_usb_data *vreq = usb_req_to_virtio_data(req);
+	struct virtio_usb_dc_priv *priv = virtio_usb_data_priv(vreq);
+	struct virtio_usb_request *request;
+	struct virtio_usb_response *response;
+	struct scatterlist *out_sgs = NULL, *in_sgs = NULL, *psg_data = NULL;
+	struct scatterlist sg;
+	unsigned long flags;
+	u16 endpoint;
+	u16 transfer_flags = 0;
+	u16 transfer_type = VIRTIO_USB_EP_CONTROL;
+	bool cancel_pending;
+	u64 tag;
+	int rc;
+
+	virtio_usb_data_ref(vreq);
+
+	request = virtio_usb_data_request(vreq);
+	response = virtio_usb_data_response(vreq);
+	response->actual_length = cpu_to_le32(0);
+	response->status = cpu_to_le32(VIRTIO_USB_S_ERR_INTERNAL);
+
+	if (req->short_not_ok)
+		transfer_flags |= VIRTIO_USB_FLAG_SHORT_NOT_OK;
+	else if (req->zero)
+		transfer_flags |= VIRTIO_USB_FLAG_ZERO_PACKET;
+	request->transfer_flags = cpu_to_le16(transfer_flags);
+
+	endpoint = vep->ep_id |
+		   (usb_ep_dir_in(&vep->ep) ? VIRTIO_USB_EP_DIR_IN :
+					      VIRTIO_USB_EP_DIR_OUT);
+	request->endpoint = cpu_to_le16(endpoint);
+	request->port = cpu_to_le16(vudc->port);
+	if (vep->ep_id && vep->ep.desc) {
+		switch (usb_endpoint_type(vep->ep.desc)) {
+		case USB_ENDPOINT_XFER_ISOC:
+			transfer_type = VIRTIO_USB_EP_ISOCHRONOUS;
+			break;
+		case USB_ENDPOINT_XFER_BULK:
+			transfer_type = VIRTIO_USB_EP_BULK;
+			break;
+		case USB_ENDPOINT_XFER_INT:
+			transfer_type = VIRTIO_USB_EP_INTERRUPT;
+			break;
+		default:
+			transfer_type = VIRTIO_USB_EP_CONTROL;
+			break;
+		}
+	}
+	request->transfer_type = cpu_to_le16(transfer_type);
+
+	if (req->length && req->buf) {
+		psg_data = &sg;
+		sg_init_one(psg_data, req->buf, req->length);
+	} else if (req->length && req->num_sgs > 0) {
+		psg_data = req->sg;
+	} else if (req->length && req->sg) {
+		psg_data = &sg;
+		sg_init_one(psg_data, sg_virt(req->sg), req->length);
+	} else {
+		psg_data = NULL;
+	}
+
+	if (usb_ep_dir_in(&vep->ep))
+		out_sgs = psg_data;
+	else
+		in_sgs = psg_data;
+
+	spin_lock_irqsave(&vudc->lock, flags);
+	priv->completed_early = false;
+	priv->submitted = false;
+	priv->cancel_pending = false;
+	/* A new tag for every submission: the request may be queued again
+	 * while a cancel for its previous submission is still pending or in
+	 * flight, and that cancel must not hit this one.
+	 */
+	tag = virtio_usb_data_tag_next(vudc->vusb);
+	priv->tag = tag;
+	request->tag = cpu_to_le64(tag);
+	req->actual = 0;
+	req->status = -EINPROGRESS;
+	list_add_tail(&vreq->list, &vep->req_queue);
+	spin_unlock_irqrestore(&vudc->lock, flags);
+
+	/*
+	 * virtio_ep_dequeue() can see the request on req_queue and schedule
+	 * the cancel work before the request reaches the data virtqueue. A
+	 * DEV_CANCEL sent that early would not match anything on the device
+	 * side, would be answered with success and dropped, and the request
+	 * queued right after would never be cancelled. In that case the
+	 * cancel work only records the request and this path reschedules it.
+	 *
+	 * Hold a reference across the window so the request cannot be
+	 * completed and freed while this is being decided.
+	 */
+	virtio_usb_data_ref(vreq);
+
+	rc = virtio_usb_data_send(vudc->vusb, vreq, out_sgs, in_sgs);
+	if (rc) {
+		spin_lock_irqsave(&vudc->lock, flags);
+		list_del_init(&vreq->list);
+		spin_unlock_irqrestore(&vudc->lock, flags);
+
+		virtio_usb_data_unref(vreq);
+		virtio_usb_data_unref(vreq);
+		return rc;
+	}
+
+	/*
+	 * The request may already have completed and, from its completion
+	 * callback, been queued again by the function driver. The flags then
+	 * belong to that newer submission, which handles them itself.
+	 */
+	spin_lock_irqsave(&vudc->lock, flags);
+	cancel_pending = false;
+	if (priv->tag == tag) {
+		priv->submitted = true;
+		cancel_pending = priv->cancel_pending;
+	}
+	spin_unlock_irqrestore(&vudc->lock, flags);
+
+	if (cancel_pending)
+		virtio_usb_dc_schedule_cancel(vreq);
+
+	virtio_usb_data_unref(vreq);
+
+	return 0;
+}
+
+static bool virtio_usb_dc_ep_enabling_locked(struct virtio_usb_dc *vudc)
+{
+	u32 i;
+
+	for (i = 1; i < vudc->neps; i++) {
+		if (vudc->veps[i].state == EP_ENABLING)
+			return true;
+	}
+
+	return false;
+}
+
+static void virtio_usb_dc_flush_ep0_pend(struct virtio_usb_dc *vudc)
+{
+	struct virtio_usb_ep *vep = &vudc->veps[0];
+	struct virtio_usb_data *vreq;
+	unsigned long flags;
+
+	spin_lock_irqsave(&vudc->lock, flags);
+	if (virtio_usb_dc_ep_enabling_locked(vudc)) {
+		spin_unlock_irqrestore(&vudc->lock, flags);
+		return;
+	}
+
+	while (!list_empty(&vep->pend_queue)) {
+		struct virtio_usb_dc_priv *priv;
+		int qrc;
+
+		vreq = list_first_entry(&vep->pend_queue,
+					struct virtio_usb_data, list);
+		priv = virtio_usb_data_priv(vreq);
+		list_del_init(&vreq->list);
+		spin_unlock_irqrestore(&vudc->lock, flags);
+
+		qrc = do_real_queue(vep, &priv->req);
+		virtio_usb_data_unref(vreq);
+		if (qrc) {
+			priv->req.status = qrc;
+			usb_gadget_giveback_request(&vep->ep, &priv->req);
+		}
+
+		spin_lock_irqsave(&vudc->lock, flags);
+	}
+	spin_unlock_irqrestore(&vudc->lock, flags);
+}
+
+static bool
+virtio_usb_ep_comp_valid(const struct usb_ss_ep_comp_descriptor *comp)
+{
+	return comp && comp->bLength == USB_DT_SS_EP_COMP_SIZE &&
+	       comp->bDescriptorType == USB_DT_SS_ENDPOINT_COMP;
+}
+
+static void vep_enable_work(struct work_struct *work)
+{
+	struct virtio_usb_ep *vep =
+		container_of(work, struct virtio_usb_ep, enable_work);
+	struct virtio_usb_dc *vudc = vep->vudc;
+	const struct usb_endpoint_descriptor *desc = vep->ep.desc;
+	const struct usb_ss_ep_comp_descriptor *comp = vep->ep.comp_desc;
+	struct scatterlist sgs[2];
+	struct scatterlist *psg_data = sgs;
+	struct virtio_usb_dev_cmd_hdr *hdr;
+	struct virtio_usb_cmd *cmd;
+	unsigned long flags;
+	u16 endpoint;
+	u8 n_sgs = 1;
+	int rc;
+
+	if (!desc)
+		return;
+
+	endpoint = vep->ep_id |
+		   (usb_ep_dir_in(&vep->ep) ? VIRTIO_USB_EP_DIR_IN :
+					      VIRTIO_USB_EP_DIR_OUT);
+
+	cmd = virtio_usb_dc_cmd_alloc(vudc, VIRTIO_USB_CMD_DEV_EP_ENABLE,
+				      GFP_KERNEL);
+	if (!cmd) {
+		rc = -ENOMEM;
+		goto done;
+	}
+
+	if (virtio_usb_ep_comp_valid(comp))
+		n_sgs = 2;
+
+	sg_init_table(psg_data, n_sgs);
+	sg_set_buf(&psg_data[0], desc, sizeof(*desc));
+	if (n_sgs == 2)
+		sg_set_buf(&psg_data[1], comp, sizeof(*comp));
+	hdr = virtio_usb_cmd_request(cmd);
+	hdr->endpoint = cpu_to_le16(endpoint);
+
+	rc = virtio_usb_cmd_send_sync(vudc->vusb, psg_data, NULL, cmd);
+
+done:
+	spin_lock_irqsave(&vudc->lock, flags);
+	if (!rc) {
+		vep->state = EP_ENABLED;
+		vep->last_err = 0;
+		/* Flush pend_queue */
+		while (!list_empty(&vep->pend_queue)) {
+			struct virtio_usb_data *vreq = list_first_entry(
+				&vep->pend_queue, struct virtio_usb_data, list);
+			struct virtio_usb_dc_priv *priv =
+				virtio_usb_data_priv(vreq);
+			int qrc;
+
+			list_del_init(&vreq->list);
+			spin_unlock_irqrestore(&vudc->lock, flags);
+			qrc = do_real_queue(vep, &priv->req);
+			/*
+			 * do_real_queue() takes its own ref for the
+			 * in-flight/vring lifetime; drop the ref that kept
+			 * vreq alive while it was parked in pend_queue.
+			 */
+			virtio_usb_data_unref(vreq);
+			if (qrc) {
+				/*
+				 * Request was accepted while EP was ENABLING.
+				 * If forwarding now fails, complete it with
+				 * error instead of silently dropping it.
+				 */
+				priv->req.status = qrc;
+				usb_gadget_giveback_request(&vep->ep,
+							    &priv->req);
+			}
+			spin_lock_irqsave(&vudc->lock, flags);
+		}
+	} else {
+		vep->state = EP_HALTED;
+		vep->last_err = rc;
+
+		while (!list_empty(&vep->pend_queue)) {
+			struct virtio_usb_data *vreq = list_first_entry(
+				&vep->pend_queue, struct virtio_usb_data, list);
+			struct virtio_usb_dc_priv *priv =
+				virtio_usb_data_priv(vreq);
+
+			list_del_init(&vreq->list);
+			priv->req.status = rc;
+			spin_unlock_irqrestore(&vudc->lock, flags);
+			virtio_usb_data_unref(vreq);
+			usb_gadget_giveback_request(&vep->ep, &priv->req);
+			spin_lock_irqsave(&vudc->lock, flags);
+		}
+	}
+	spin_unlock_irqrestore(&vudc->lock, flags);
+	virtio_usb_dc_flush_ep0_pend(vudc);
+}
+
+static void vep_disable_work(struct work_struct *work)
+{
+	struct virtio_usb_ep *vep =
+		container_of(work, struct virtio_usb_ep, disable_work);
+	struct virtio_usb_dc *vudc = vep->vudc;
+	struct virtio_usb_cmd *cmd;
+	struct virtio_usb_dev_cmd_hdr *hdr;
+	u16 endpoint;
+
+	if (!READ_ONCE(vudc->driver) || !vudc->registered)
+		return;
+
+	endpoint = vep->ep_id |
+		   (usb_ep_dir_in(&vep->ep) ? VIRTIO_USB_EP_DIR_IN :
+					      VIRTIO_USB_EP_DIR_OUT);
+
+	cmd = virtio_usb_dc_cmd_alloc(vudc, VIRTIO_USB_CMD_DEV_EP_DISABLE,
+				      GFP_KERNEL);
+	if (!cmd)
+		return;
+
+	hdr = virtio_usb_cmd_request(cmd);
+	hdr->endpoint = cpu_to_le16(endpoint);
+
+	(void)virtio_usb_cmd_send_sync(vudc->vusb, NULL, NULL, cmd);
+}
+
+/**
+ * virtio_ep_enable() - Enable endpoint
+ * This callback is called to configure endpoint and make it usable.
+ * It is called to enable all endpoints except ep0
+ * @ep: endpoint object
+ * @desc: endpoint descriptor
+ *
+ * Context: Any context.
+ * Return: 0 on success, -errno on failure.
+ */
+static int virtio_ep_enable(struct usb_ep *ep,
+			    const struct usb_endpoint_descriptor *desc)
+{
+	struct virtio_usb_ep *vep = usb_ep_to_virtio_ep(ep);
+	struct virtio_usb_dc *vudc = vep->vudc;
+	const struct usb_ss_ep_comp_descriptor *comp = ep->comp_desc;
+	unsigned long flags;
+
+	spin_lock_irqsave(&vudc->lock, flags);
+
+	if (!vudc->driver || !ep || !desc || ep->caps.type_control ||
+	    desc->bDescriptorType != USB_DT_ENDPOINT) {
+		spin_unlock_irqrestore(&vudc->lock, flags);
+		return -EINVAL;
+	}
+
+	if (vep->state != EP_DISABLED) {
+		spin_unlock_irqrestore(&vudc->lock, flags);
+		return -EBUSY;
+	}
+
+	if (comp) {
+		if (!virtio_usb_ep_comp_valid(comp) || comp->bMaxBurst > 15) {
+			spin_unlock_irqrestore(&vudc->lock, flags);
+			return -EINVAL;
+		}
+	} else if (vudc->gadget.speed >= USB_SPEED_SUPER) {
+		spin_unlock_irqrestore(&vudc->lock, flags);
+		return -EINVAL;
+	}
+
+	ep->desc = desc;
+	ep->maxpacket = usb_endpoint_maxp(desc);
+	vep->state = EP_ENABLING;
+
+	queue_work(vudc->ep_cmd_wq, &vep->enable_work);
+
+	spin_unlock_irqrestore(&vudc->lock, flags);
+
+	return 0;
+}
+
+/**
+ * virtio_ep_disable() - Disable endpoint
+ * This callback is called to disable endpoint which was
+ * enabled using ep_enable callback
+ * @ep: endpoint object
+ *
+ * Context: Any context.
+ * Return: 0 on success, -errno on failure.
+ */
+static int virtio_ep_disable(struct usb_ep *ep)
+{
+	struct virtio_usb_ep *vep = usb_ep_to_virtio_ep(ep);
+	struct virtio_usb_data *vreq = NULL, *vreq_tmp;
+	struct virtio_usb_dc *vudc = vep->vudc;
+	struct virtio_usb_dc_priv *priv = NULL;
+	LIST_HEAD(giveback_inflight);
+	LIST_HEAD(giveback_pended);
+	unsigned long flags;
+
+	if (!ep || ep->caps.type_control) // EP0 not called disable
+		return -EINVAL;
+
+	spin_lock_irqsave(&vudc->lock, flags);
+
+	if (vep->state == EP_ENABLING) {
+		/*
+		 * cancel_work_sync() can sleep and must not be called while
+		 * holding vudc->lock: vep_enable_work() only re-acquires
+		 * this same lock after its (possibly blocking) virtio
+		 * command completes, so holding the lock here across
+		 * cancel_work_sync() would deadlock against it.
+		 */
+		spin_unlock_irqrestore(&vudc->lock, flags);
+		cancel_work_sync(&vep->enable_work);
+		spin_lock_irqsave(&vudc->lock, flags);
+	}
+
+	if (vep->state == EP_DISABLED) {
+		spin_unlock_irqrestore(&vudc->lock, flags);
+		return 0;
+	}
+
+	vep->state = EP_DISABLED;
+
+	/*
+	 * All pending (waiting for the ENABLING) - put to local list for
+	 * giveback(-ESHUTDOWN). These requests are not in the virtqueue,
+	 * can be unref now.
+	 */
+	list_for_each_entry_safe(vreq, vreq_tmp, &vep->pend_queue, list) {
+		list_del_init(&vreq->list);
+		list_add_tail(&vreq->list, &giveback_pended);
+	}
+
+	/*
+	 * in-flight requests - make early giveback, remove from req_queue,
+	 * mark completed_early, but do not unref: unref will be in complete
+	 * handler.
+	 */
+	list_for_each_entry_safe(vreq, vreq_tmp, &vep->req_queue, list) {
+		priv = virtio_usb_data_priv(vreq);
+		priv->req.status = -ESHUTDOWN;
+		priv->completed_early = true;
+		list_del_init(&vreq->list);
+		list_add_tail(&vreq->list, &giveback_inflight);
+	}
+
+	spin_unlock_irqrestore(&vudc->lock, flags);
+
+	/* pending: just giveback and unref */
+	list_for_each_entry_safe(vreq, vreq_tmp, &giveback_pended, list) {
+		struct virtio_usb_dc_priv *p = virtio_usb_data_priv(vreq);
+
+		list_del_init(&vreq->list);
+		p->req.status = -ESHUTDOWN;
+		usb_gadget_giveback_request(&vep->ep, &p->req);
+		virtio_usb_data_unref(
+			vreq); /* drop the pend_queue ref taken in virtio_ep_queue() */
+	}
+
+	/* in-flight: giveback now, unref will be in complete_req */
+	list_for_each_entry_safe(vreq, vreq_tmp, &giveback_inflight, list) {
+		struct virtio_usb_dc_priv *p = virtio_usb_data_priv(vreq);
+
+		list_del_init(&vreq->list);
+		usb_gadget_giveback_request(&vep->ep, &p->req);
+	}
+
+	queue_work(vudc->ep_cmd_wq, &vep->disable_work);
+
+	return 0;
+}
+
+/**
+ * virtio_ep_alloc_request() - Allocates request
+ * @ep: Endpoint object associated with request
+ * @mem_flags: mem flags
+ *
+ * Return: allocated request address, NULL on allocation error
+ */
+static struct usb_request *virtio_ep_alloc_request(struct usb_ep *ep,
+						   gfp_t mem_flags)
+{
+	struct virtio_usb_dc_priv *priv;
+	struct virtio_usb_data *vreq;
+
+	vreq = virtio_usb_dc_data_alloc(ep, mem_flags);
+	if (!vreq)
+		return NULL;
+
+	priv = virtio_usb_data_priv(vreq);
+
+	return &priv->req;
+}
+
+/**
+ * virtio_ep_free_request() - Free memory occupied by request
+ * @ep: Endpoint object associated with request
+ * @req: Request to be freed
+ */
+static void virtio_ep_free_request(struct usb_ep *ep, struct usb_request *req)
+{
+	struct virtio_usb_data *vreq = usb_req_to_virtio_data(req);
+
+	virtio_usb_data_unref(vreq);
+}
+
+/**
+ * virtio_ep_queue() - Transfer data on and endpoint
+ * @ep: Pointer to endpoint object
+ * @req: Pointer to request object
+ * @mem_flags: gfp flags
+ *
+ * Context: Any context.
+ * Return: 0 on success, -errno on failure.
+ */
+static int virtio_ep_queue(struct usb_ep *ep, struct usb_request *req,
+			   gfp_t mem_flags)
+{
+	struct virtio_usb_data *vreq = usb_req_to_virtio_data(req);
+	struct virtio_usb_ep *vep = usb_ep_to_virtio_ep(ep);
+	struct virtio_usb_dc *vudc = vep->vudc;
+	unsigned long flags;
+	int rc = 0;
+
+	spin_lock_irqsave(&vudc->lock, flags);
+
+	switch (vep->state) {
+	case EP_ENABLED:
+		if (!vep->ep_id && virtio_usb_dc_ep_enabling_locked(vudc)) {
+			virtio_usb_data_ref(vreq);
+			req->actual = 0;
+			req->status = -EINPROGRESS;
+			list_add_tail(&vreq->list, &vep->pend_queue);
+			spin_unlock_irqrestore(&vudc->lock, flags);
+			return 0;
+		}
+		spin_unlock_irqrestore(&vudc->lock, flags);
+		rc = do_real_queue(vep, req);
+		/* if rc==0 - unref will be in completion, on failure
+		 * do_real_queue() has already dropped its own ref
+		 */
+		return rc;
+
+	case EP_ENABLING:
+		/* Waiting for ENABLED state */
+		virtio_usb_data_ref(vreq);
+		req->actual = 0;
+		req->status = -EINPROGRESS;
+		list_add_tail(&vreq->list, &vep->pend_queue);
+		spin_unlock_irqrestore(&vudc->lock, flags);
+		// Additional ref waited for the enable_work()
+		return 0;
+
+	case EP_HALTED:
+		/*
+		 * Endpoint enable failed. Fail the submission without calling
+		 * the completion: gadget drivers commonly resubmit from their
+		 * completion handler, so a giveback here would recurse.
+		 */
+		rc = vep->last_err ? vep->last_err : -EPIPE;
+		spin_unlock_irqrestore(&vudc->lock, flags);
+		return rc;
+
+	case EP_DISABLED:
+	default:
+		spin_unlock_irqrestore(&vudc->lock, flags);
+		return -ESHUTDOWN;
+	}
+}
+
+/**
+ * virtio_ep_dequeue() - Remove request from transfer queue
+ * @ep: Endpoint object associated with request
+ * @req: Request object
+ *
+ * Context: Any context.
+ * Return: 0 on success, -errno on failure.
+ */
+static int virtio_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
+{
+	struct virtio_usb_ep *vep = usb_ep_to_virtio_ep(ep);
+	struct virtio_usb_dc *vudc = vep->vudc;
+	struct virtio_usb_dc_priv *priv;
+	struct virtio_usb_data *vreq;
+	unsigned long flags;
+	int rc = -EINVAL;
+
+	if (!vudc->driver)
+		return -ESHUTDOWN;
+
+	spin_lock_irqsave(&vudc->lock, flags);
+
+	/* If request is still pending (ENABLING) */
+	list_for_each_entry(vreq, &vep->pend_queue, list) {
+		priv = virtio_usb_data_priv(vreq);
+		if (&priv->req == req) {
+			list_del_init(&vreq->list);
+			req->status = -ECONNRESET;
+			spin_unlock_irqrestore(&vudc->lock, flags);
+
+			virtio_usb_data_unref(vreq);
+			usb_gadget_giveback_request(ep, req);
+			return 0;
+		}
+	}
+
+	/* in-flight - schedule async cancel */
+	list_for_each_entry(vreq, &vep->req_queue, list) {
+		priv = virtio_usb_data_priv(vreq);
+		if (req == &priv->req) {
+			pr_debug("dequeue for vreq = %p, tag %llu\n", vreq,
+				 priv->tag);
+			// request will be completed from the completion handler
+			priv->req.status = -ECONNRESET;
+			priv->cancel_tag = priv->tag;
+			virtio_usb_dc_schedule_cancel(vreq);
+			rc = 0;
+			break;
+		}
+	}
+	spin_unlock_irqrestore(&vudc->lock, flags);
+
+	return rc;
+}
+
+/**
+ * virtio_ep_set_halt() - Sets/clears stall on selected endpoint
+ * @ep: Endpoint object to set/clear stall on
+ * @value: 1 for set stall, 0 for clear stall
+ *
+ * Context: Any context.
+ * Return: 0 on success, -errno on failure.
+ */
+static int virtio_ep_set_halt(struct usb_ep *ep, int value)
+{
+	struct virtio_usb_ep *vep = usb_ep_to_virtio_ep(ep);
+	struct virtio_usb_dev_cmd_set_value *req;
+	struct virtio_usb_dc *vudc = vep->vudc;
+	struct virtio_usb_cmd *cmd;
+	unsigned long flags;
+	u16 endpoint;
+	int rc;
+
+	spin_lock_irqsave(&vudc->lock, flags);
+
+	/*
+	 * EP0 MUST NOT STALL if a control request is still pending.
+	 * Composite expects -EAGAIN instead of a forced STALL, otherwise
+	 * status stage collapses and the host sees EPROTO/EPIPE.
+	 */
+	if (&vep->ep == vudc->gadget.ep0) {
+		if (value && !list_empty(&vep->req_queue)) {
+			spin_unlock_irqrestore(&vudc->lock, flags);
+			return -EAGAIN;
+		}
+	}
+
+	if (value && ep->desc && usb_ep_dir_in(ep) &&
+	    !list_empty(&vep->req_queue)) {
+		spin_unlock_irqrestore(&vudc->lock, flags);
+		return -EAGAIN;
+	}
+	endpoint = vep->ep_id | (usb_ep_dir_in(ep) ? VIRTIO_USB_EP_DIR_IN :
+						     VIRTIO_USB_EP_DIR_OUT);
+	spin_unlock_irqrestore(&vudc->lock, flags);
+
+	cmd = virtio_usb_dc_cmd_alloc(vudc, VIRTIO_USB_CMD_DEV_EP_SET_HALT,
+				      GFP_ATOMIC);
+	if (!cmd)
+		return -ENOMEM;
+
+	req = virtio_usb_cmd_request(cmd);
+	req->value = cpu_to_le32(value);
+	req->hdr.endpoint = cpu_to_le16(endpoint);
+
+	rc = virtio_usb_cmd_send_sync(vudc->vusb, NULL, NULL, cmd);
+
+	return rc;
+}
+
+/**
+ * virtio_ep_set_wedge() - Set wedge on selected endpoint
+ * @ep: Endpoint object
+ *
+ * Context: Any context.
+ * Return: 0 on success, -errno on failure.
+ */
+static int virtio_ep_set_wedge(struct usb_ep *ep)
+{
+	struct virtio_usb_ep *vep = usb_ep_to_virtio_ep(ep);
+	struct virtio_usb_dc *vudc = vep->vudc;
+	struct virtio_usb_dev_cmd_hdr *hdr;
+	struct virtio_usb_cmd *cmd;
+	unsigned long flags;
+	u16 endpoint;
+	int rc;
+
+	spin_lock_irqsave(&vudc->lock, flags);
+	endpoint = vep->ep_id | (usb_ep_dir_in(ep) ? VIRTIO_USB_EP_DIR_IN :
+						     VIRTIO_USB_EP_DIR_OUT);
+	spin_unlock_irqrestore(&vudc->lock, flags);
+
+	cmd = virtio_usb_dc_cmd_alloc(vudc, VIRTIO_USB_CMD_DEV_EP_SET_WEDGE,
+				      GFP_ATOMIC);
+	if (!cmd)
+		return -ENOMEM;
+
+	hdr = virtio_usb_cmd_request(cmd);
+	hdr->endpoint = cpu_to_le16(endpoint);
+
+	rc = virtio_usb_cmd_send_sync(vudc->vusb, NULL, NULL, cmd);
+
+	return rc;
+}
+
+static const struct usb_ep_ops virtio_usb_ep_ops = {
+	.enable = virtio_ep_enable,
+	.disable = virtio_ep_disable,
+
+	.alloc_request = virtio_ep_alloc_request,
+	.free_request = virtio_ep_free_request,
+
+	.queue = virtio_ep_queue,
+	.dequeue = virtio_ep_dequeue,
+
+	.set_halt = virtio_ep_set_halt,
+	.set_wedge = virtio_ep_set_wedge,
+};
+
+/*-------------------------------------------------------------------------*/
+/* UDC callbacks */
+
+static void virtio_usb_dc_cancel_work(struct work_struct *work)
+{
+	struct virtio_usb_dc_priv *priv =
+		container_of(work, struct virtio_usb_dc_priv, cancel_work);
+	struct virtio_usb_ep *vep = priv->vep;
+	struct virtio_usb_dc *vudc = vep->vudc;
+	struct virtio_usb_cmd *cmd;
+	struct virtio_usb_dev_cmd_cancel *cancel;
+	struct virtio_usb_data *vreq = priv->vreq;
+	unsigned long flags;
+	u16 endpoint;
+	struct virtio_usb_data *iter;
+	bool found = false;
+	u64 tag;
+
+	spin_lock_irqsave(&vudc->lock, flags);
+
+	list_for_each_entry(iter, &vep->req_queue, list) {
+		if (iter == vreq) {
+			found = true;
+			break;
+		}
+	}
+	/*
+	 * The request may have completed and been queued again since
+	 * virtio_ep_dequeue() scheduled this work. Then the submission to
+	 * cancel is gone and the one on req_queue must be left alone.
+	 */
+	if (!found || priv->cancel_tag != priv->tag) {
+		spin_unlock_irqrestore(&vudc->lock, flags);
+		goto out;
+	}
+	/*
+	 * The request is on req_queue but may not have reached the data
+	 * virtqueue yet, in which case the device side cannot match the tag:
+	 * it would answer the DEV_CANCEL with success and drop it, and the
+	 * request queued right after would never be cancelled. Let
+	 * do_real_queue() reschedule this work once the request is queued.
+	 */
+	if (!priv->submitted) {
+		priv->cancel_pending = true;
+		spin_unlock_irqrestore(&vudc->lock, flags);
+		goto out;
+	}
+	endpoint = vep->ep_id |
+		   (usb_ep_dir_in(&vep->ep) ? VIRTIO_USB_EP_DIR_IN :
+					      VIRTIO_USB_EP_DIR_OUT);
+	/*
+	 * Once the lock is dropped the request can still complete and be
+	 * queued again before the DEV_CANCEL reaches the device. The cancel
+	 * carries the tag of the dequeued submission, so the device side
+	 * does not find it and answers with success, leaving the new
+	 * submission alone.
+	 */
+	tag = priv->tag;
+	spin_unlock_irqrestore(&vudc->lock, flags);
+
+	cmd = virtio_usb_dc_cmd_alloc(vudc, VIRTIO_USB_CMD_DEV_CANCEL,
+				      GFP_KERNEL);
+	if (!cmd)
+		goto out;
+
+	cancel = virtio_usb_cmd_request(cmd);
+	cancel->hdr.endpoint = cpu_to_le16(endpoint);
+	cancel->hdr.port = cpu_to_le16(vudc->port);
+	cancel->tag = cpu_to_le64(tag);
+
+	(void)virtio_usb_cmd_send_sync(vudc->vusb, NULL, NULL, cmd);
+
+out:
+	/* Taken by virtio_usb_dc_schedule_cancel(). May free the request,
+	 * and with it this work item, so nothing may touch priv after this.
+	 */
+	virtio_usb_data_unref(vreq);
+}
+
+static int virtio_usb_dc_send_cmd(struct virtio_usb_dc *vudc,
+				  unsigned int cmd_code, unsigned int val)
+{
+	struct virtio_usb_dev_cmd_set_value *req;
+	struct virtio_usb_cmd *cmd;
+
+	cmd = virtio_usb_dc_cmd_alloc(vudc, cmd_code, GFP_ATOMIC);
+	if (!cmd)
+		return -ENOMEM;
+
+	req = virtio_usb_cmd_request(cmd);
+	req->value = cpu_to_le32(val);
+
+	return virtio_usb_cmd_send_sync(vudc->vusb, NULL, NULL, cmd);
+}
+
+static void virtio_usb_dc_work(struct work_struct *work)
+{
+	struct virtio_usb_dc *vudc =
+		container_of(work, struct virtio_usb_dc, dc_cmd_work);
+	int sp, ma;
+
+	mutex_lock(&vudc->dc_cmd_work_lock);
+
+	sp = atomic_xchg(&vudc->pending_self_pwr, -1);
+	ma = atomic_xchg(&vudc->pending_vbus_ma, -1);
+
+	if (sp >= 0) {
+		bool new_sp = (sp != 0);
+
+		if (new_sp != vudc->cur_self_pwr) {
+			(void)virtio_usb_dc_send_cmd(
+				vudc, VIRTIO_USB_CMD_DEV_SET_SELF_POWERED,
+				new_sp);
+			vudc->cur_self_pwr = new_sp;
+		}
+	}
+
+	if (ma >= 0) {
+		unsigned int new_ma = (unsigned int)ma;
+
+		if (vudc->cur_self_pwr)
+			new_ma = 0;
+
+		if (new_ma != vudc->cur_vbus_ma) {
+			(void)virtio_usb_dc_send_cmd(
+				vudc, VIRTIO_USB_CMD_DEV_VBUS_DRAW, new_ma);
+			vudc->cur_vbus_ma = new_ma;
+		}
+	}
+
+	mutex_unlock(&vudc->dc_cmd_work_lock);
+
+	if (atomic_read(&vudc->pending_self_pwr) >= 0 ||
+	    atomic_read(&vudc->pending_vbus_ma) >= 0) {
+		queue_work(vudc->dc_cmd_wq, &vudc->dc_cmd_work);
+	}
+}
+
+/**
+ * virtio_usb_dc_set_selfpowered() - Sets the device selfpowered feature.
+ * @gadget: The device being declared as self-powered
+ * @is_selfpowered: Flag indicates if gadget is selfpowered
+ *
+ * Return: 0 on success, -errno on failure.
+ */
+static int virtio_usb_dc_set_selfpowered(struct usb_gadget *gadget,
+					 int is_selfpowered)
+{
+	struct virtio_usb_dc *vudc =
+		container_of(gadget, struct virtio_usb_dc, gadget);
+
+	atomic_set(&vudc->pending_self_pwr, is_selfpowered ? 1 : 0);
+	queue_work(vudc->dc_cmd_wq, &vudc->dc_cmd_work);
+
+	return 0;
+}
+
+/**
+ * virtio_usb_dc_pullup() - Software-controlled connect/disconnect to USB host
+ * @gadget: Pointer to the usb gadget structure.
+ * @is_on: flag to start or stop
+ *
+ * Return: 0 on success, -errno on failure.
+ */
+static int virtio_usb_dc_pullup(struct usb_gadget *gadget, int is_on)
+{
+	struct virtio_usb_dc *vudc =
+		container_of(gadget, struct virtio_usb_dc, gadget);
+	unsigned int code = VIRTIO_USB_CMD_DEV_PULLUP;
+	struct virtio_usb_dev_cmd_set_value *req;
+	struct virtio_usb_cmd *cmd;
+	unsigned long flags;
+	int rc;
+
+	spin_lock_irqsave(&vudc->lock, flags);
+	is_on = !!is_on;
+	if (is_on == vudc->pullup) {
+		rc = -EALREADY;
+		goto on_unlock;
+	}
+
+	vudc->pullup = is_on;
+	spin_unlock_irqrestore(&vudc->lock, flags);
+
+	cmd = virtio_usb_dc_cmd_alloc(vudc, code, GFP_ATOMIC);
+	if (!cmd)
+		return -ENOMEM;
+
+	req = virtio_usb_cmd_request(cmd);
+	req->value = cpu_to_le32(is_on);
+
+	rc = virtio_usb_cmd_send_sync(vudc->vusb, NULL, NULL, cmd);
+
+	return rc;
+
+on_unlock:
+	spin_unlock_irqrestore(&vudc->lock, flags);
+
+	return rc;
+}
+
+/**
+ * virtio_usb_dc_set_speed() - Sets maximum speed supported by gadget
+ * @gadget: Pointer to the usb gadget structure.
+ * @speed: The maximum speed to allowed to run
+ */
+static void virtio_usb_dc_set_speed(struct usb_gadget *gadget,
+				    enum usb_device_speed speed)
+{
+	struct virtio_usb_dc *vudc =
+		container_of(gadget, struct virtio_usb_dc, gadget);
+
+	vudc->gadget.speed = min_t(u8, vudc->gadget.max_speed, speed);
+
+	switch (vudc->gadget.speed) {
+	case USB_SPEED_SUPER_PLUS:
+	case USB_SPEED_SUPER:
+		vudc->veps[0].ep.maxpacket = 512;
+		break;
+	case USB_SPEED_HIGH:
+	case USB_SPEED_FULL:
+		vudc->veps[0].ep.maxpacket = 64;
+		break;
+	case USB_SPEED_LOW:
+		vudc->veps[0].ep.maxpacket = 8;
+		break;
+	default:
+		break;
+	}
+}
+
+/**
+ * virtio_usb_dc_start() - Starts the device controller.
+ * @gadget: Pointer to the usb gadget structure
+ * @driver: Pointer to gadget driver structure
+ *
+ * Return: zero always
+ */
+static int virtio_usb_dc_start(struct usb_gadget *gadget,
+			       struct usb_gadget_driver *driver)
+{
+	struct virtio_usb_dc *vudc =
+		container_of(gadget, struct virtio_usb_dc, gadget);
+	unsigned long flags;
+
+	spin_lock_irqsave(&vudc->lock, flags);
+	vudc->driver = driver;
+	vudc->pullup = 0;
+	spin_unlock_irqrestore(&vudc->lock, flags);
+
+	return 0;
+}
+
+/**
+ * virtio_usb_dc_stop() - Stops the device controller.
+ * @gadget: Pointer to the usb gadget structure
+ *
+ * Return: zero always
+ */
+static int virtio_usb_dc_stop(struct usb_gadget *gadget)
+{
+	struct virtio_usb_dc *vudc =
+		container_of(gadget, struct virtio_usb_dc, gadget);
+	unsigned long flags;
+
+	spin_lock_irqsave(&vudc->lock, flags);
+	vudc->driver = NULL;
+	vudc->pullup = 0;
+	spin_unlock_irqrestore(&vudc->lock, flags);
+
+	return 0;
+}
+
+/**
+ * virtio_usb_dc_vbus_draw() - Constrain controller's VBUS power usage
+ * @gadget: The device whose VBUS usage is being described
+ * @ma: How much current to draw, in milliAmperes.
+ *
+ * Return: 0 on success, -errno on failure.
+ */
+static int virtio_usb_dc_vbus_draw(struct usb_gadget *gadget, unsigned int ma)
+{
+	struct virtio_usb_dc *vudc =
+		container_of(gadget, struct virtio_usb_dc, gadget);
+
+	atomic_set(&vudc->pending_vbus_ma, ma);
+	queue_work(vudc->dc_cmd_wq, &vudc->dc_cmd_work);
+	return 0;
+}
+
+/**
+ * virtio_usb_dc_get_frame() - returns the current frame number
+ * @gadget: controller that reports the frame number
+ *
+ * Return: Returns the usb frame number, normally eleven bits from
+ * a SOF packet, or -errno on failure.
+ */
+static int virtio_usb_dc_get_frame(struct usb_gadget *gadget)
+{
+	struct virtio_usb_dc *vudc =
+		container_of(gadget, struct virtio_usb_dc, gadget);
+	unsigned int code = VIRTIO_USB_CMD_DEV_GET_FRAME_NUMBER;
+	struct virtio_usb_dev_frame_number *resp;
+	struct virtio_usb_cmd *cmd;
+	int rc;
+
+	cmd = virtio_usb_dc_cmd_alloc(vudc, code, GFP_ATOMIC);
+	if (!cmd)
+		return -ENOMEM;
+
+	rc = virtio_usb_cmd_send_sync(vudc->vusb, NULL, NULL, cmd);
+	if (rc)
+		return rc;
+
+	resp = virtio_usb_cmd_response(cmd);
+	rc = le32_to_cpu(resp->frame_number);
+
+	return rc;
+}
+
+static const struct usb_gadget_ops virtio_gadget_ops = {
+	.set_selfpowered = virtio_usb_dc_set_selfpowered,
+	.pullup = virtio_usb_dc_pullup,
+	.udc_start = virtio_usb_dc_start,
+	.udc_stop = virtio_usb_dc_stop,
+	.vbus_draw = virtio_usb_dc_vbus_draw,
+	.udc_set_speed = virtio_usb_dc_set_speed,
+	.get_frame = virtio_usb_dc_get_frame,
+};
+
+/*-------------------------------------------------------------------------*/
+
+/**
+ * virtio_usb_dc_get_endpoint_count() - Function to get the number of
+ * endpoints from the virtio-usb device
+ * @vudc: VirtIO usb device controller
+ *
+ * Context: Process context.
+ * Return: 0 on success, -errno on failure.
+ */
+static int virtio_usb_dc_get_endpoint_count(struct virtio_usb_dc *vudc)
+{
+	unsigned int code = VIRTIO_USB_CMD_DEV_GET_ENDPOINT_COUNT;
+	struct virtio_usb_dev_ep_count *ep_count;
+	struct virtio_usb_cmd *cmd;
+	int rc;
+
+	cmd = virtio_usb_dc_cmd_alloc(vudc, code, GFP_KERNEL);
+	if (!cmd)
+		return -ENOMEM;
+
+	virtio_usb_cmd_ref(cmd);
+	rc = virtio_usb_cmd_send_sync(vudc->vusb, NULL, NULL, cmd);
+	if (rc)
+		goto on_exit;
+
+	ep_count = virtio_usb_cmd_response(cmd);
+	vudc->neps = le32_to_cpu(ep_count->count);
+	if (!vudc->neps)
+		rc = -EINVAL;
+
+on_exit:
+	virtio_usb_cmd_unref(cmd);
+	return rc;
+}
+
+/**
+ * virtio_usb_dc_get_endpoint_info() - Function to get the endpoint
+ * info from virtio-usb device.
+ * @vudc: VirtIO usb device controller
+ *
+ * Context: Process context.
+ * Return: 0 on success, -errno on failure.
+ */
+static int virtio_usb_dc_get_endpoint_info(struct virtio_usb_dc *vudc)
+{
+	unsigned int code = VIRTIO_USB_CMD_DEV_GET_ENDPOINT_INFO;
+	struct virtio_usb_dev_ep_info *epinfo;
+	struct virtio_usb_cmd *cmd;
+	struct scatterlist sg;
+	struct scatterlist *psg_data = &sg;
+	int rc, i;
+
+	vudc->veps = kcalloc(vudc->neps, sizeof(*vudc->veps) + sizeof(*epinfo),
+			     GFP_KERNEL);
+	if (!vudc->veps)
+		return -ENOMEM;
+
+	epinfo = (void *)vudc->veps + (sizeof(*vudc->veps) * vudc->neps);
+
+	cmd = virtio_usb_dc_cmd_alloc(vudc, code, GFP_KERNEL);
+	if (!cmd) {
+		rc = -ENOMEM;
+		goto on_error;
+	}
+	virtio_usb_cmd_ref(cmd);
+
+	sg_init_one(psg_data, epinfo, sizeof(*epinfo) * vudc->neps);
+
+	rc = virtio_usb_cmd_send_sync(vudc->vusb, NULL, psg_data, cmd);
+	if (rc) {
+		virtio_usb_cmd_unref(cmd);
+		goto on_error;
+	}
+
+	INIT_LIST_HEAD(&vudc->gadget.ep_list);
+
+	for (i = 0; i < vudc->neps; i++) {
+		struct virtio_usb_ep *vep = &vudc->veps[i];
+		u16 types = le16_to_cpu(epinfo[i].types);
+		u16 directions = le16_to_cpu(epinfo[i].directions);
+		struct usb_ep_caps caps = VIRTIO_USB_EP_CAPS(types, directions);
+		unsigned int maxpacket_limit =
+			le16_to_cpu(epinfo[i].maxpacket_limit);
+
+		strscpy(vep->name, epinfo[i].name, sizeof(vep->name));
+		vep->ep_id = i;
+		vep->ep.caps = caps;
+		vep->ep.name = vep->name;
+		vep->ep.ops = &virtio_usb_ep_ops;
+		INIT_LIST_HEAD(&vep->req_queue);
+		INIT_LIST_HEAD(&vep->pend_queue);
+		vep->state = i ? EP_DISABLED : EP_ENABLED; // EP0 always enabled
+		INIT_WORK(&vep->enable_work, vep_enable_work);
+		INIT_WORK(&vep->disable_work, vep_disable_work);
+		vep->last_err = 0;
+		list_add_tail(&vep->ep.ep_list, &vudc->gadget.ep_list);
+		usb_ep_set_maxpacket_limit(&vep->ep, maxpacket_limit);
+		vep->ep.max_streams = le16_to_cpu(epinfo[i].max_streams);
+		vep->vudc = vudc;
+	}
+	virtio_usb_cmd_unref(cmd);
+	vudc->gadget.ep0 = &vudc->veps[0].ep;
+	list_del_init(&vudc->veps[0].ep.ep_list);
+
+	return rc;
+
+on_error:
+	kfree(vudc->veps);
+	vudc->veps = NULL;
+	return rc;
+}
+
+static int virtio_usb_dc_parent_create(struct virtio_usb_dc *vudc)
+{
+	int rc;
+
+	vudc->pdev = platform_device_alloc(GADGET_NAME, vudc->port);
+	if (!vudc->pdev)
+		return -ENOMEM;
+
+	vudc->pdev->dev.parent = &vudc->vusb->vdev->dev;
+	rc = platform_device_add(vudc->pdev);
+	if (rc) {
+		platform_device_put(vudc->pdev);
+		vudc->pdev = NULL;
+		return rc;
+	}
+
+	return 0;
+}
+
+/**
+ * virtio_usb_dc_event_padding_ok() - Check that reserved event bytes are zero.
+ * @evt: Received device event buffer.
+ *
+ * The padding fields are reserved for future extensions. Rejecting
+ * non-zero values now keeps them usable later without a feature bit.
+ * BIND uses those bytes for max_speed/udc_name_prefix and has no
+ * padding.
+ *
+ * Context: Any context.
+ * Return: true if all reserved bytes for the event code are zero.
+ */
+static bool virtio_usb_dc_event_padding_ok(struct virtio_usb_dev_event *evt)
+{
+	struct virtio_usb_dev_setup_event *setup_evt =
+		(struct virtio_usb_dev_setup_event *)evt;
+
+	switch (le32_to_cpu(evt->code)) {
+	case VIRTIO_USB_EVT_DEV_BIND:
+		return true;
+	case VIRTIO_USB_EVT_DEV_SETUP:
+		return !memchr_inv(setup_evt->padding, 0,
+				   sizeof(setup_evt->padding));
+	default:
+		return !memchr_inv(evt->padding, 0, sizeof(evt->padding));
+	}
+}
+
+/**
+ * virtio_usb_dc_event_process() - Event process function
+ * @event: virtio_usb_event
+ *
+ * Context: Process context.
+ * Return: 0 on success, -errno on failure.
+ */
+static void virtio_usb_dc_event_process(struct virtio_usb_event *event)
+{
+	struct virtio_usb_dev_event *evt = virtio_usb_event_buf(event);
+	struct virtio_usb_dev_bind_event *bind_evt =
+		(struct virtio_usb_dev_bind_event *)evt;
+	struct virtio_usb_dev_setup_event *setup_evt;
+	struct virtio_usb *vusb = event->vusb;
+	struct virtio_usb_dc *vudc;
+	struct usb_gadget_driver *driver;
+	struct virtio_usb_ep *vep;
+	unsigned long flags;
+	int rc = 0;
+	unsigned short port_id = le16_to_cpu(evt->port);
+
+	if (!vusb->nports || port_id >= vusb->nports) {
+		dev_err(&vusb->vdev->dev, "%s port[%d] index out of range\n",
+			__func__, port_id);
+		return;
+	}
+
+	if (!virtio_usb_dc_event_padding_ok(evt)) {
+		dev_err(&vusb->vdev->dev,
+			"port %d: event %u with non-zero padding, ignoring\n",
+			port_id, le32_to_cpu(evt->code));
+		return;
+	}
+
+	vudc = vusb->vports[port_id].vudc;
+	driver = vudc->driver;
+
+	switch (le32_to_cpu(evt->code)) {
+	case VIRTIO_USB_EVT_DEV_BIND: {
+		u8 max_speed;
+
+		if (vudc->registered) {
+			dev_err(&vusb->vdev->dev,
+				"port %d: BIND while still registered, ignoring\n",
+				port_id);
+			rc = -EBUSY;
+			break;
+		}
+
+		memzero_explicit(&vudc->gadget, sizeof(struct usb_gadget));
+		vudc->gadget.sg_supported = 1;
+		max_speed = bind_evt->max_speed;
+
+		rc = virtio_usb_dc_get_endpoint_count(vudc);
+		if (rc) {
+			dev_err(&vusb->vdev->dev,
+				"Failed to get endpoint count\n");
+			break;
+		}
+		rc = virtio_usb_dc_get_endpoint_info(vudc);
+		if (rc) {
+			dev_err(&vusb->vdev->dev,
+				"Failed to get endpoint info\n");
+			break;
+		}
+		if (!vudc->pdev) {
+			rc = virtio_usb_dc_parent_create(vudc);
+			if (rc) {
+				dev_err(&vusb->vdev->dev,
+					"Failed to create UDC parent device\n");
+				kfree(vudc->veps);
+				vudc->veps = NULL;
+				break;
+			}
+		}
+		vudc->gadget.name =
+			kasprintf(GFP_KERNEL, "%s_%d", GADGET_NAME, port_id);
+		if (!vudc->gadget.name) {
+			rc = -ENOMEM;
+			kfree(vudc->veps);
+			vudc->veps = NULL;
+			break;
+		}
+		vudc->gadget.ops = &virtio_gadget_ops;
+		switch (max_speed) {
+		case USB_SPEED_LOW:
+		case USB_SPEED_FULL:
+		case USB_SPEED_HIGH:
+		case USB_SPEED_SUPER:
+		case USB_SPEED_SUPER_PLUS:
+			vudc->gadget.max_speed = max_speed;
+			break;
+		default:
+			pr_info("virtio-usb: unrecognized max_speed %u from host, defaulting to HIGH\n",
+				max_speed);
+			vudc->gadget.max_speed = USB_SPEED_HIGH;
+			break;
+		}
+
+		//vudc->gadget.dev.init_name = gadget_name;
+		vudc->gadget.dev.parent = &vudc->pdev->dev;
+		rc = usb_add_gadget_udc(&vudc->pdev->dev, &vudc->gadget);
+		if (rc) {
+			dev_err(&vudc->pdev->dev, "Failed to add udc\n");
+			kfree(vudc->veps);
+			vudc->veps = NULL;
+			kfree(vudc->gadget.name);
+			vudc->gadget.name = NULL;
+			break;
+		}
+		spin_lock_irqsave(&vudc->lock, flags);
+		vudc->registered = 1;
+		spin_unlock_irqrestore(&vudc->lock, flags);
+		break;
+	}
+	case VIRTIO_USB_EVT_DEV_DISCONNECTED: {
+		if (vudc->driver && vudc->driver->disconnect)
+			vudc->driver->disconnect(&vudc->gadget);
+		if (vudc->registered)
+			usb_gadget_set_state(&vudc->gadget,
+					     USB_STATE_NOTATTACHED);
+		break;
+	}
+	case VIRTIO_USB_EVT_DEV_SETUP: {
+		if (!driver)
+			break;
+
+		vep = usb_ep_to_virtio_ep(vudc->gadget.ep0);
+		setup_evt = virtio_usb_event_buf(event);
+		rc = virtio_usb_dc_wait_ep0_idle(vudc);
+		if (rc) {
+			dev_warn(
+				&vusb->vdev->dev,
+				"SETUP while ep0 request is still pending, timing out\n");
+			virtio_ep_set_halt(vudc->gadget.ep0, 1);
+			break;
+		}
+
+		memcpy(&vep->setup, setup_evt->setup,
+		       sizeof(struct usb_ctrlrequest));
+
+		rc = driver->setup(&vudc->gadget,
+				   (struct usb_ctrlrequest *)setup_evt->setup);
+		if (rc < 0 && rc != -ESHUTDOWN)
+			virtio_ep_set_halt(vudc->gadget.ep0, 1);
+		break;
+	}
+	case VIRTIO_USB_EVT_DEV_RESET: {
+		if (driver)
+			usb_gadget_udc_reset(&vudc->gadget, driver);
+		break;
+	}
+	case VIRTIO_USB_EVT_DEV_SUSPEND: {
+		if (driver && driver->suspend)
+			driver->suspend(&vudc->gadget);
+		break;
+	}
+	case VIRTIO_USB_EVT_DEV_RESUME: {
+		if (driver && driver->resume)
+			driver->resume(&vudc->gadget);
+		break;
+	}
+	case VIRTIO_USB_EVT_DEV_UNBIND: {
+		unsigned int registered;
+
+		pr_info("%s port_id %d VIRTIO_USB_EVT_DEV_UNBIND\n", __func__,
+			port_id);
+
+		spin_lock_irqsave(&vudc->lock, flags);
+		registered = vudc->registered;
+		vudc->registered = 0;
+		spin_unlock_irqrestore(&vudc->lock, flags);
+
+		if (registered) {
+			usb_del_gadget_udc(&vudc->gadget);
+			kfree(vudc->gadget.name);
+			vudc->gadget.name = NULL;
+		}
+
+		if (vudc->veps) {
+			unsigned int i;
+
+			/*
+			 * usb_del_gadget_udc() disabled every endpoint, and
+			 * virtio_ep_disable() queued each one's disable_work
+			 * on ep_cmd_wq. Those work items live in veps, so let
+			 * them run (they return at once now that registered
+			 * is 0) before freeing it.
+			 */
+			flush_workqueue(vudc->ep_cmd_wq);
+			for (i = 0; i < vudc->neps; i++) {
+				cancel_work_sync(&vudc->veps[i].enable_work);
+				cancel_work_sync(&vudc->veps[i].disable_work);
+			}
+			kfree(vudc->veps);
+			vudc->veps = NULL;
+		}
+
+		if (vudc->pdev) {
+			platform_device_unregister(vudc->pdev);
+			vudc->pdev = NULL;
+		}
+		break;
+	}
+	default:
+		rc = -EINVAL;
+		break;
+	}
+}
+
+/**
+ * virtio_usb_dc_event_work() - Worker to get all events
+ * from the virtqueue and process them.
+ * @work: Kernel work to handle event completion.
+ *
+ * Context: Process context.
+ */
+void virtio_usb_dc_event_work(struct work_struct *work)
+{
+	struct virtio_usb *vusb =
+		container_of(work, struct virtio_usb, vq_dev_event_work);
+	struct virtio_usb_queue *evtq =
+		&vusb->vqueues[vusb->dev_vq_base + VIRTIO_USB_VQ_EVENT_IDX];
+	virtio_usb_evt_work(evtq, virtio_usb_dc_event_process);
+}
+
+/**
+ * virtio_usb_dc_event_populate() - Add events to the device event queue.
+ * @vusb: VirtIO USB device
+ *
+ * Context: Any context.
+ * Return: 0 on success -errno on failure
+ */
+int virtio_usb_dc_event_populate(struct virtio_usb *vusb)
+{
+	struct virtio_usb_queue *evt_queue =
+		&vusb->vqueues[vusb->dev_vq_base + VIRTIO_USB_VQ_EVENT_IDX];
+	struct virtio_usb_event *events;
+	int rc;
+
+	events = virtio_usb_events_alloc(vusb, evt_queue,
+					 sizeof(struct virtio_usb_dev_event));
+
+	if (!events)
+		return -ENOMEM;
+
+	rc = virtio_usb_events_populate(events);
+
+	return rc;
+}
+
+/**
+ * virtio_usb_dc_init() - Initializes the device role.
+ * @vusb: VirtIO USB device
+ * @port_id: Index of the virtual port to initialize
+ *
+ * Context: Any context.
+ * Return: 0 on success -errno on failure
+ */
+int virtio_usb_dc_init(struct virtio_usb *vusb, int port_id)
+{
+	struct virtio_usb_dc *vudc;
+	unsigned int i;
+
+	vudc = devm_kzalloc(&vusb->vdev->dev, sizeof(*vudc), GFP_KERNEL);
+	if (!vudc)
+		return -ENOMEM;
+
+	vudc->vusb = vusb;
+	vudc->port = port_id;
+	for (i = 0; i < VIRTIO_USB_VQ_DEV_MAX; i++)
+		vudc->dcqs[i] = &vusb->vqueues[vusb->dev_vq_base + i];
+
+	vudc->ep_cmd_wq = alloc_ordered_workqueue(
+		"vudc_ep_work_%d", WQ_MEM_RECLAIM | WQ_UNBOUND, port_id);
+	vudc->dc_cmd_wq = alloc_ordered_workqueue(
+		"vudc_dc_work_%d", WQ_MEM_RECLAIM | WQ_UNBOUND, port_id);
+	if (!vudc->ep_cmd_wq || !vudc->dc_cmd_wq) {
+		if (vudc->ep_cmd_wq)
+			destroy_workqueue(vudc->ep_cmd_wq);
+		if (vudc->dc_cmd_wq)
+			destroy_workqueue(vudc->dc_cmd_wq);
+		devm_kfree(&vusb->vdev->dev, vudc);
+		return -ENOMEM;
+	}
+
+	/* Publish only once fully initialized - vports[port_id].vudc must
+	 * never point at a partially-constructed (or already-freed, on the
+	 * failure path above) struct virtio_usb_dc.
+	 */
+	vusb->vports[port_id].vudc = vudc;
+
+	INIT_WORK(&vudc->dc_cmd_work, virtio_usb_dc_work);
+	mutex_init(&vudc->dc_cmd_work_lock);
+	atomic_set(&vudc->pending_self_pwr, -1);
+	atomic_set(&vudc->pending_vbus_ma, -1);
+	vudc->cur_vbus_ma = 0;
+	vudc->cur_self_pwr = false;
+
+	spin_lock_init(&vudc->lock);
+	init_waitqueue_head(&vudc->ep0_idle);
+
+	vudc->registered = 0;
+
+	return 0;
+}
+
+/**
+ * virtio_usb_dc_deinit() - Deinitialize the device role.
+ * @vusb: VirtIO USB device
+ *
+ * Context: Any context.
+ * Return: 0 on success -errno on failure
+ */
+int virtio_usb_dc_deinit(struct virtio_usb *vusb)
+{
+	int port_id;
+
+	if (!vusb->device_role)
+		return -ENODEV;
+
+	for (port_id = 0; port_id < vusb->nports; port_id++) {
+		struct virtio_usb_dc *vudc = vusb->vports[port_id].vudc;
+		unsigned int registered;
+		unsigned long flags;
+
+		if (!vudc)
+			continue;
+
+		spin_lock_irqsave(&vudc->lock, flags);
+		registered = vudc->registered;
+		spin_unlock_irqrestore(&vudc->lock, flags);
+
+		if (registered) {
+			usb_del_gadget_udc(&vudc->gadget);
+			kfree(vudc->gadget.name);
+			vudc->gadget.name = NULL;
+		}
+
+		if (vudc->dc_cmd_wq) {
+			cancel_work_sync(&vudc->dc_cmd_work);
+			destroy_workqueue(vudc->dc_cmd_wq);
+		}
+
+		if (vudc->ep_cmd_wq) {
+			flush_workqueue(vudc->ep_cmd_wq);
+			if (vudc->veps) {
+				unsigned int i;
+
+				for (i = 0; i < vudc->neps; i++)
+					cancel_work_sync(
+						&vudc->veps[i].enable_work);
+			}
+			destroy_workqueue(vudc->ep_cmd_wq);
+		}
+		kfree(vudc->veps);
+		vudc->veps = NULL;
+
+		if (vudc->pdev) {
+			platform_device_unregister(vudc->pdev);
+			vudc->pdev = NULL;
+		}
+
+		vusb->vports[port_id].vudc = NULL;
+	}
+
+	return 0;
+}
+
+/**
+ * virtio_usb_dc_dataq_stop_cb() - Stops the data virtqueue
+ * @vusb: VirtIO usb device.
+ * @dataq: data virtqueue wrapper
+ *
+ * Context: Any context.
+ */
+static void virtio_usb_dc_dataq_stop_cb(struct virtio_usb *vusb,
+					struct virtio_usb_queue *dataq)
+{
+	int port_id;
+
+	if (!vusb->device_role)
+		return;
+
+	virtio_usb_dataq_stop_cb(vusb, dataq);
+	cancel_work_sync(&vusb->vq_dev_data_rx_work);
+
+	for (port_id = 0; port_id < vusb->nports; port_id++) {
+		struct virtio_usb_dc *vudc = vusb->vports[port_id].vudc;
+		struct virtio_usb_data *vreq, *vreq_tmp;
+		struct virtio_usb_dc_priv *priv;
+		unsigned int i;
+
+		if (!vudc)
+			continue;
+
+		for (i = 0; i < vudc->neps; i++) {
+			list_for_each_entry_safe(vreq, vreq_tmp,
+						 &vudc->veps[i].req_queue,
+						 list) {
+				priv = virtio_usb_data_priv(vreq);
+				priv->req.status = -ESHUTDOWN;
+				list_del_init(&vreq->list);
+				virtio_usb_data_unref(vreq);
+				usb_gadget_giveback_request(&priv->vep->ep,
+							    &priv->req);
+			}
+		}
+	}
+}
+
+/**
+ * virtio_usb_dc_data_notify_cb() - Data virtqueue notification callback
+ * @vqueue: Underlying event virtqueue.
+ *
+ * This callback function is called upon a vring interrupt request from the
+ * device.
+ *
+ * Context: Interrupt context.
+ */
+static void virtio_usb_dc_data_notify_cb(struct virtqueue *vqueue)
+{
+	struct virtio_usb *vusb = vqueue->vdev->priv;
+
+	schedule_work(&vusb->vq_dev_data_rx_work);
+}
+
+/**
+ * virtio_usb_dc_evt_notify_cb() - Event virtqueue notification callback
+ * @vqueue: Underlying event virtqueue.
+ *
+ * This callback function is called upon a vring interrupt request from the
+ * device.
+ *
+ * Context: Interrupt context.
+ */
+static void virtio_usb_dc_evt_notify_cb(struct virtqueue *vqueue)
+{
+	struct virtio_usb *vusb = vqueue->vdev->priv;
+
+	schedule_work(&vusb->vq_dev_event_work);
+}
+
+/**
+ * virtio_usb_dc_evtq_stop_cb() - Stops the event virtqueue.
+ * @vusb: VirtIO usb device.
+ * @vq: virtio usb vq wrapper
+ *
+ * Context: Any context.
+ */
+static void virtio_usb_dc_evtq_stop_cb(struct virtio_usb *vusb,
+				       struct virtio_usb_queue *vq)
+{
+	/*
+	 * Unlike the host evtq stop path (which safely updates in-memory
+	 * port status bits), dc event processing drives the UDC state
+	 * machine and calls back into gadget drivers and UDC core. Doing
+	 * so here, when the UDC may be only partially initialized (probe
+	 * failure) or already torn down (remove path), risks
+	 * use-after-free and crashes - virtio_usb_evt_drain_stop_cb()
+	 * drains without processing for exactly this reason.
+	 */
+	cancel_work_sync((struct work_struct *)&vusb->vq_dev_event_work);
+	virtio_usb_evt_drain_stop_cb(vq, NULL);
+}
+
+const struct virtio_usb_vq_desc dev_vqueues[VIRTIO_USB_VQ_DEV_MAX] = {
+			[VIRTIO_USB_VQ_COMMAND_IDX] = {
+			.callback = virtio_usb_cmd_notify_cb,
+			.name = "virtusb-dev-cmd",
+			.process = virtio_usb_cmd_process_cb,
+			.stop = virtio_usb_cmdq_stop_cb,
+		},
+		[VIRTIO_USB_VQ_EVENT_IDX] = {
+			.callback = virtio_usb_dc_evt_notify_cb,
+			.name = "virtusb-dev-evt",
+			.process = NULL,
+			.stop = virtio_usb_dc_evtq_stop_cb,
+		},
+		[VIRTIO_USB_VQ_DATA_IDX] = {
+			.callback = virtio_usb_dc_data_notify_cb,
+			.name = "virtusb-dev-data",
+			.process = NULL,
+			.stop = virtio_usb_dc_dataq_stop_cb,
+		},
+};
diff --git a/drivers/usb/virtio_usb/device.h b/drivers/usb/virtio_usb/device.h
new file mode 100644
index 000000000000..e4132741fe7f
--- /dev/null
+++ b/drivers/usb/virtio_usb/device.h
@@ -0,0 +1,126 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * virtio-usb: Virtio usb device
+ *
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ */
+
+#ifndef VIRTIO_USB_DC_H
+#define VIRTIO_USB_DC_H
+
+#include <linux/usb.h>
+#include <linux/list.h>
+#include <linux/platform_device.h>
+#include <linux/wait.h>
+#include <linux/usb/ch11.h>
+#include <linux/usb/ch9.h>
+#include <linux/usb/gadget.h>
+
+#include "vq_common.h"
+
+#include <linux/virtio_usb.h>
+
+#define VIRTIO_USB_EP_CAPS_TYPE_CONTROL 0x01
+#define VIRTIO_USB_EP_CAPS_TYPE_INTERRUPT 0x02
+#define VIRTIO_USB_EP_CAPS_TYPE_BULK 0x04
+#define VIRTIO_USB_EP_CAPS_TYPE_ISOCHRONOUS 0x08
+
+#define VIRTIO_USB_EP_CAPS_DIR_IN 0x02
+#define VIRTIO_USB_EP_CAPS_DIR_OUT 0x01
+
+#define VIRTIO_USB_EP_CAPS(_type, _dir)                                        \
+	{                                                                      \
+		.type_control = !!((_type) & VIRTIO_USB_EP_CAPS_TYPE_CONTROL), \
+		.type_iso = !!((_type) & VIRTIO_USB_EP_CAPS_TYPE_ISOCHRONOUS), \
+		.type_bulk = !!((_type) & VIRTIO_USB_EP_CAPS_TYPE_BULK),       \
+		.type_int = !!((_type) & VIRTIO_USB_EP_CAPS_TYPE_INTERRUPT),   \
+		.dir_in = !!((_dir) & VIRTIO_USB_EP_CAPS_DIR_IN),              \
+		.dir_out = !!((_dir) & VIRTIO_USB_EP_CAPS_DIR_OUT),            \
+	}
+
+enum vep_state { EP_DISABLED, EP_ENABLING, EP_ENABLED, EP_HALTED };
+
+/**
+ * struct virtio_usb_ep - virtio usb device endpoint
+ * @ep: usb ep
+ * @vudc: Device controller this endpoint belongs to
+ * @setup: setup packet for control endpoint
+ * @req_queue: list of usb requests submitted to ep awaiting response
+ * @pend_queue: list of usb requests waiting for submit while EP is ENABLING
+ * @state: endpoint state (necessary to support split-phase async processing)
+ * @enable_work:  workstruct for the async ep_enable()
+ * @disable_work:  workstruct for the async ep_disable()
+ * @last_err: error code if async processeing brings us to HALTED state
+ * @ep_id: Id of the endpoint
+ * @name: Endpoint name
+ */
+struct virtio_usb_ep {
+	struct usb_ep ep;
+	struct virtio_usb_dc *vudc;
+	struct usb_ctrlrequest setup;
+	struct list_head req_queue;
+	struct list_head pend_queue;
+	enum vep_state state;
+	struct work_struct enable_work;
+	struct work_struct disable_work;
+	int last_err;
+	u16 ep_id;
+	char name[16];
+};
+
+/**
+ * struct virtio_usb_dc - VirtIO USB Device controller (USBDC) device.
+ * @pdev: Platform device that is the parent of the gadget's UDC
+ * @port: Index of the virtual port this controller serves
+ * @gadget: Pointer to the usb gadget structure
+ * @driver: Pointer to the usb gadget  driver structure
+ * @veps: Virtual endpoints
+ * @neps: Number of virtual endpoints
+ * @dcqs: Device virtqueue wrappers, indexed by VIRTIO_USB_VQ_*_IDX.
+ * @vusb: VirtIO usb device
+ * @registered: Flag indicating registration status to the UDC core.
+ * @pullup:  Software-controlled connect/disconnect status USB host.
+ * @lock: Spinlock that protects device state
+ * @ep0_idle: Wait queue for SETUP serialization while ep0 has an in-flight req
+ * @ep_cmd_wq: Ordered workqueue for endpoint enable/disable/cancel commands
+ * @dc_cmd_wq: Ordered workqueue for @dc_cmd_work
+ * @dc_cmd_work: Sends the latest pending self-powered and VBUS draw
+ *               settings to the device
+ * @dc_cmd_work_lock: Serializes @dc_cmd_work and protects @cur_vbus_ma
+ *                    and @cur_self_pwr
+ * @pending_self_pwr: Self-powered state to send (0 or 1), or -1 if none
+ * @pending_vbus_ma: VBUS draw in mA to send, or -1 if none
+ * @cur_vbus_ma: VBUS draw last sent to the device
+ * @cur_self_pwr: Self-powered state last sent to the device
+ */
+struct virtio_usb_dc {
+	struct platform_device *pdev;
+	u16 port;
+	struct usb_gadget gadget;
+	struct usb_gadget_driver *driver;
+	struct virtio_usb_ep *veps;
+	u32 neps;
+	struct virtio_usb_queue *dcqs[VIRTIO_USB_VQ_DEV_MAX];
+	struct virtio_usb *vusb;
+	unsigned registered : 1;
+	unsigned pullup : 1;
+	spinlock_t lock;
+	wait_queue_head_t ep0_idle;
+	struct workqueue_struct *ep_cmd_wq;
+	struct workqueue_struct *dc_cmd_wq;
+	struct work_struct dc_cmd_work;
+	struct mutex dc_cmd_work_lock;
+	atomic_t pending_self_pwr;
+	atomic_t pending_vbus_ma;
+	unsigned int cur_vbus_ma;
+	bool cur_self_pwr;
+};
+
+extern const struct virtio_usb_vq_desc dev_vqueues[VIRTIO_USB_VQ_DEV_MAX];
+
+int virtio_usb_dc_init(struct virtio_usb *vusb, int port_id);
+int virtio_usb_dc_deinit(struct virtio_usb *vusb);
+void virtio_usb_dc_event_work(struct work_struct *work);
+void virtio_usb_dc_data_work(struct work_struct *work);
+int virtio_usb_dc_event_populate(struct virtio_usb *vusb);
+#endif /* VIRTIO_USB_DC_H */
diff --git a/include/uapi/linux/virtio_usb.h b/include/uapi/linux/virtio_usb.h
index b601c88f1c7e..238052b4f0a9 100644
--- a/include/uapi/linux/virtio_usb.h
+++ b/include/uapi/linux/virtio_usb.h
@@ -57,11 +57,10 @@ struct virtio_usb_config {
 #define VIRTIO_USB_S_ERR_STALL 10
 #define VIRTIO_USB_S_ERR_SHORT_PKT 11
 #define VIRTIO_USB_S_ERR_CANCELLED 12
-#define VIRTIO_USB_S_ERR_HOST 13
 
 struct virtio_usb_cmd_status {
 	__le32 code; /* VIRTIO_USB_S_XXX */
-};
+} __packed;
 
 /*****************************************************************************
  * HOST COMMAND MESSAGES
@@ -129,6 +128,8 @@ struct virtio_usb_host_port_event {
 #define VIRTIO_USB_CMD_DEV_EP_SET_HALT 6
 #define VIRTIO_USB_CMD_DEV_EP_SET_WEDGE 7
 #define VIRTIO_USB_CMD_DEV_CANCEL 8
+#define VIRTIO_USB_CMD_DEV_GET_FRAME_NUMBER 9
+#define VIRTIO_USB_CMD_DEV_SET_SELF_POWERED 10
 
 struct virtio_usb_dev_cmd_hdr {
 	__le32 code; /* VIRTIO_USB_CMD_DEV_XXX */
@@ -136,12 +137,30 @@ struct virtio_usb_dev_cmd_hdr {
 	__le16 endpoint; /* Endpoint ID */
 };
 
+/* VIRTIO_USB_CMD_DEV_CANCEL */
+struct virtio_usb_dev_cmd_cancel {
+	struct virtio_usb_dev_cmd_hdr hdr;
+	__le64 tag;
+};
+
 /* VIRTIO_USB_CMD_DEV_GET_ENDPOINT_COUNT */
-struct virtio_usb_dev_cmd_ep_count {
+struct virtio_usb_dev_ep_count {
 	struct virtio_usb_cmd_status status;
 	__le32 count; /* # of supported endpoints */
 };
 
+struct virtio_usb_dev_cmd_set_value {
+	struct virtio_usb_dev_cmd_hdr hdr;
+	__le32 value;
+	__le32 padding;
+};
+
+/* VIRTIO_USB_CMD_DEV_GET_FRAME_NUMBER */
+struct virtio_usb_dev_frame_number {
+	struct virtio_usb_cmd_status status;
+	__le32 frame_number;
+};
+
 #define VIRTIO_USB_DIR_OUT 0
 #define VIRTIO_USB_DIR_IN 1
 
@@ -155,33 +174,45 @@ struct virtio_usb_dev_cmd_ep_count {
  *     struct virtio_usb_cmd_status
  *     struct virtio_usb_dev_ep_info [count]
  */
-struct virtio_usb_dev_cmd_ep_info {
+struct virtio_usb_dev_ep_info {
 	__le16 types; /* supported type bit map (1 << VIRTIO_USB_EP_XXX) */
 	__le16 directions; /* supported direction bit map (1 << VIRTIO_USB_DIR_XXX) */
 	__le16 maxpacket_limit;
 	__le16 max_streams;
+	__u8 name[16];
 };
 
 /*******************************************************************************
  * DEVICE EVENT MESSAGES
  */
-#define VIRTIO_USB_EVT_DEV_CONNECTED 0
+#define VIRTIO_USB_EVT_DEV_BIND 0
 #define VIRTIO_USB_EVT_DEV_DISCONNECTED 1
 #define VIRTIO_USB_EVT_DEV_SETUP 2
 #define VIRTIO_USB_EVT_DEV_RESET 3
 #define VIRTIO_USB_EVT_DEV_SUSPEND 4
 #define VIRTIO_USB_EVT_DEV_RESUME 5
+#define VIRTIO_USB_EVT_DEV_UNBIND 6
 
 struct virtio_usb_dev_event {
 	__le32 code; /* VIRTIO_USB_EVT_DEV_XXX */
-	__u8 padding[12];
+	__le16 port; /* Device ID */
+	__u8 padding[10];
+};
+
+/* VIRTIO_USB_EVT_DEV_BIND */
+struct virtio_usb_dev_bind_event {
+	__le32 code; /* VIRTIO_USB_EVT_DEV_BIND */
+	__le16 port; /* Device ID */
+	__u8 max_speed; /* enum usb_device_speed */
+	__u8 padding[9];
 };
 
 /* VIRTIO_USB_EVT_DEV_SETUP */
 struct virtio_usb_dev_setup_event {
 	__le32 code; /* VIRTIO_USB_EVT_DEV_SETUP */
+	__le16 port; /* Device ID */
 	__u8 setup[8]; /* setup packet contents */
-	__u8 padding[4];
+	__u8 padding[2];
 };
 
 /*******************************************************************************
-- 
2.49.0


  parent reply	other threads:[~2026-12-01 17:36 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20260924160907.145405-1-igor.skalkin@oss.qualcomm.com>
2026-12-01 17:35 ` [RFC PATCH v2 0/7] virtio-usb: add dual-role virtio USB driver Igor Skalkin
2026-12-01 17:35   ` [RFC PATCH v2 1/7] uapi: virtio_ids: reserve ID for virtio USB device Igor Skalkin
2026-12-01 17:35   ` [RFC PATCH v2 2/7] virtio-usb: add protocol header and skeleton dual-role driver Igor Skalkin
2026-12-01 17:35   ` [RFC PATCH v2 3/7] virtio-usb: add host role (USB Host Controller) support Igor Skalkin
2026-12-01 17:35   ` Igor Skalkin [this message]
2026-12-01 17:35   ` [RFC PATCH v2 5/7] virtio-usb: add per-port role query support Igor Skalkin
2026-12-01 17:35   ` [RFC PATCH v2 6/7] virtio-usb: add host/device role switch support Igor Skalkin
2026-12-01 17:35   ` [RFC PATCH v2 7/7] virtio-usb: support a guest UDC name prefix from the bind event Igor Skalkin
2026-12-01 17:49   ` [RFC PATCH v2 0/7] virtio-usb: add dual-role virtio USB driver Michael S. Tsirkin
2026-12-01 20:09   ` Greg Kroah-Hartman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3f0c8f183e321f032bdf30bb06f6837dadb6bc94.1796144390.git.igor.skalkin@oss.qualcomm.com \
    --to=igor.skalkin@oss.qualcomm.com \
    --cc=aiswarya.cyriac@oss.qualcomm.com \
    --cc=anton.yakovlev@oss.qualcomm.com \
    --cc=eperezma@redhat.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jasowangio@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=mvaralar@redhat.com \
    --cc=trilok.soni@oss.qualcomm.com \
    --cc=vasilii.ianikeev@oss.qualcomm.com \
    --cc=virtualization@lists.linux.dev \
    --cc=xuanzhuo@linux.alibaba.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®