1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
12 #include <linux/usb/gadget.h>
15 struct usb_gadget gadget;
18 struct sandbox_udc *this_controller;
20 struct sandbox_usb_ctrl {
24 static void usbmon_trace(struct udevice *bus, ulong pipe,
25 struct devrequest *setup, struct udevice *emul)
27 static const char types[] = "ZICB";
30 type = (pipe & USB_PIPE_TYPE_MASK) >> USB_PIPE_TYPE_SHIFT;
31 debug("0 0 S %c%c:%d:%03ld:%ld", types[type],
32 pipe & USB_DIR_IN ? 'i' : 'o',
34 (pipe & USB_PIPE_DEV_MASK) >> USB_PIPE_DEV_SHIFT,
35 (pipe & USB_PIPE_EP_MASK) >> USB_PIPE_EP_SHIFT);
37 debug(" s %02x %02x %04x %04x %04x", setup->requesttype,
38 setup->request, setup->value, setup->index,
41 debug(" %s", emul ? emul->name : "(no emul found)");
46 static int sandbox_submit_control(struct udevice *bus,
47 struct usb_device *udev,
49 void *buffer, int length,
50 struct devrequest *setup)
52 struct sandbox_usb_ctrl *ctrl = dev_get_priv(bus);
56 /* Just use child of dev as emulator? */
57 debug("%s: bus=%s\n", __func__, bus->name);
58 ret = usb_emul_find(bus, pipe, udev->portnr, &emul);
59 usbmon_trace(bus, pipe, setup, emul);
63 if (usb_pipedevice(pipe) == ctrl->rootdev) {
64 if (setup->request == USB_REQ_SET_ADDRESS) {
65 debug("%s: Set root hub's USB address\n", __func__);
66 ctrl->rootdev = le16_to_cpu(setup->value);
70 ret = usb_emul_control(emul, udev, pipe, buffer, length, setup);
72 debug("ret=%d\n", ret);
83 static int sandbox_submit_bulk(struct udevice *bus, struct usb_device *udev,
84 unsigned long pipe, void *buffer, int length)
89 /* Just use child of dev as emulator? */
90 debug("%s: bus=%s\n", __func__, bus->name);
91 ret = usb_emul_find(bus, pipe, udev->portnr, &emul);
92 usbmon_trace(bus, pipe, NULL, emul);
95 ret = usb_emul_bulk(emul, udev, pipe, buffer, length);
97 debug("ret=%d\n", ret);
108 static int sandbox_submit_int(struct udevice *bus, struct usb_device *udev,
109 unsigned long pipe, void *buffer, int length,
110 int interval, bool nonblock)
112 struct udevice *emul;
115 /* Just use child of dev as emulator? */
116 debug("%s: bus=%s\n", __func__, bus->name);
117 ret = usb_emul_find(bus, pipe, udev->portnr, &emul);
118 usbmon_trace(bus, pipe, NULL, emul);
121 ret = usb_emul_int(emul, udev, pipe, buffer, length, interval,
127 int usb_gadget_handle_interrupts(int index)
132 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
134 struct sandbox_udc *dev = this_controller;
136 return driver->bind(&dev->gadget);
139 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
141 struct sandbox_udc *dev = this_controller;
143 driver->unbind(&dev->gadget);
148 static int sandbox_alloc_device(struct udevice *dev, struct usb_device *udev)
150 struct sandbox_usb_ctrl *ctrl = dev_get_priv(dev);
153 * Root hub will be the first device to be initailized.
154 * If this device is a root hub, initialize its device speed
155 * to high speed as we are a USB 2.0 controller.
157 if (ctrl->rootdev == 0)
158 udev->speed = USB_SPEED_HIGH;
163 static int sandbox_usb_probe(struct udevice *dev)
168 static const struct dm_usb_ops sandbox_usb_ops = {
169 .control = sandbox_submit_control,
170 .bulk = sandbox_submit_bulk,
171 .interrupt = sandbox_submit_int,
172 .alloc_device = sandbox_alloc_device,
175 static const struct udevice_id sandbox_usb_ids[] = {
176 { .compatible = "sandbox,usb" },
180 U_BOOT_DRIVER(usb_sandbox) = {
181 .name = "usb_sandbox",
183 .of_match = sandbox_usb_ids,
184 .probe = sandbox_usb_probe,
185 .ops = &sandbox_usb_ops,
186 .priv_auto = sizeof(struct sandbox_usb_ctrl),