blob: e26f0b292ed534f65644f9137a50b99618c155cc [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassbc4546a2015-03-25 12:22:41 -06002/*
3 * (C) Copyright 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassbc4546a2015-03-25 12:22:41 -06005 */
6
Simon Glassbc4546a2015-03-25 12:22:41 -06007#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Simon Glassbc4546a2015-03-25 12:22:41 -06009#include <usb.h>
10#include <dm/root.h>
Patrice Chotardd54d2942021-09-10 16:16:22 +020011#include <linux/usb/gadget.h>
12
13struct sandbox_udc {
14 struct usb_gadget gadget;
15};
16
17struct sandbox_udc *this_controller;
Simon Glassbc4546a2015-03-25 12:22:41 -060018
Bin Meng33662d62017-10-01 06:19:38 -070019struct sandbox_usb_ctrl {
20 int rootdev;
21};
22
Simon Glassbc4546a2015-03-25 12:22:41 -060023static void usbmon_trace(struct udevice *bus, ulong pipe,
24 struct devrequest *setup, struct udevice *emul)
25{
26 static const char types[] = "ZICB";
27 int type;
28
29 type = (pipe & USB_PIPE_TYPE_MASK) >> USB_PIPE_TYPE_SHIFT;
30 debug("0 0 S %c%c:%d:%03ld:%ld", types[type],
31 pipe & USB_DIR_IN ? 'i' : 'o',
Simon Glass75e534b2020-12-16 21:20:07 -070032 dev_seq(bus),
Simon Glassbc4546a2015-03-25 12:22:41 -060033 (pipe & USB_PIPE_DEV_MASK) >> USB_PIPE_DEV_SHIFT,
34 (pipe & USB_PIPE_EP_MASK) >> USB_PIPE_EP_SHIFT);
35 if (setup) {
36 debug(" s %02x %02x %04x %04x %04x", setup->requesttype,
37 setup->request, setup->value, setup->index,
38 setup->length);
39 }
40 debug(" %s", emul ? emul->name : "(no emul found)");
41
42 debug("\n");
43}
44
45static int sandbox_submit_control(struct udevice *bus,
46 struct usb_device *udev,
47 unsigned long pipe,
48 void *buffer, int length,
49 struct devrequest *setup)
50{
Bin Meng33662d62017-10-01 06:19:38 -070051 struct sandbox_usb_ctrl *ctrl = dev_get_priv(bus);
Simon Glassbc4546a2015-03-25 12:22:41 -060052 struct udevice *emul;
53 int ret;
54
55 /* Just use child of dev as emulator? */
56 debug("%s: bus=%s\n", __func__, bus->name);
Bin Mengf1966442017-10-01 06:19:39 -070057 ret = usb_emul_find(bus, pipe, udev->portnr, &emul);
Simon Glassbc4546a2015-03-25 12:22:41 -060058 usbmon_trace(bus, pipe, setup, emul);
59 if (ret)
60 return ret;
Bin Meng33662d62017-10-01 06:19:38 -070061
62 if (usb_pipedevice(pipe) == ctrl->rootdev) {
63 if (setup->request == USB_REQ_SET_ADDRESS) {
64 debug("%s: Set root hub's USB address\n", __func__);
65 ctrl->rootdev = le16_to_cpu(setup->value);
66 }
67 }
68
Simon Glassbc4546a2015-03-25 12:22:41 -060069 ret = usb_emul_control(emul, udev, pipe, buffer, length, setup);
70 if (ret < 0) {
71 debug("ret=%d\n", ret);
72 udev->status = ret;
73 udev->act_len = 0;
74 } else {
75 udev->status = 0;
76 udev->act_len = ret;
77 }
78
79 return ret;
80}
81
82static int sandbox_submit_bulk(struct udevice *bus, struct usb_device *udev,
83 unsigned long pipe, void *buffer, int length)
84{
85 struct udevice *emul;
86 int ret;
87
88 /* Just use child of dev as emulator? */
89 debug("%s: bus=%s\n", __func__, bus->name);
Bin Mengf1966442017-10-01 06:19:39 -070090 ret = usb_emul_find(bus, pipe, udev->portnr, &emul);
Simon Glassbc4546a2015-03-25 12:22:41 -060091 usbmon_trace(bus, pipe, NULL, emul);
92 if (ret)
93 return ret;
94 ret = usb_emul_bulk(emul, udev, pipe, buffer, length);
95 if (ret < 0) {
96 debug("ret=%d\n", ret);
97 udev->status = ret;
98 udev->act_len = 0;
99 } else {
100 udev->status = 0;
101 udev->act_len = ret;
102 }
103
104 return ret;
105}
106
Simon Glass90421792015-11-08 23:48:05 -0700107static int sandbox_submit_int(struct udevice *bus, struct usb_device *udev,
108 unsigned long pipe, void *buffer, int length,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +0200109 int interval, bool nonblock)
Simon Glass90421792015-11-08 23:48:05 -0700110{
111 struct udevice *emul;
112 int ret;
113
114 /* Just use child of dev as emulator? */
115 debug("%s: bus=%s\n", __func__, bus->name);
Bin Mengf1966442017-10-01 06:19:39 -0700116 ret = usb_emul_find(bus, pipe, udev->portnr, &emul);
Simon Glass90421792015-11-08 23:48:05 -0700117 usbmon_trace(bus, pipe, NULL, emul);
118 if (ret)
119 return ret;
Michal Suchanek1c95b9f2019-08-18 10:55:27 +0200120 ret = usb_emul_int(emul, udev, pipe, buffer, length, interval,
121 nonblock);
Simon Glass90421792015-11-08 23:48:05 -0700122
123 return ret;
124}
125
Marek Vasuted07a672023-09-01 11:49:48 +0200126#if CONFIG_IS_ENABLED(DM_USB_GADGET)
127int dm_usb_gadget_handle_interrupts(struct udevice *dev)
128{
129 return 0;
130}
131#else
Patrice Chotardd54d2942021-09-10 16:16:22 +0200132int usb_gadget_register_driver(struct usb_gadget_driver *driver)
133{
134 struct sandbox_udc *dev = this_controller;
135
136 return driver->bind(&dev->gadget);
137}
138
139int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
140{
141 struct sandbox_udc *dev = this_controller;
142
143 driver->unbind(&dev->gadget);
144
145 return 0;
146}
Marek Vasuted07a672023-09-01 11:49:48 +0200147#endif
Patrice Chotardd54d2942021-09-10 16:16:22 +0200148
Simon Glassbc4546a2015-03-25 12:22:41 -0600149static int sandbox_alloc_device(struct udevice *dev, struct usb_device *udev)
150{
Bin Meng33662d62017-10-01 06:19:38 -0700151 struct sandbox_usb_ctrl *ctrl = dev_get_priv(dev);
152
153 /*
154 * Root hub will be the first device to be initailized.
155 * If this device is a root hub, initialize its device speed
156 * to high speed as we are a USB 2.0 controller.
157 */
158 if (ctrl->rootdev == 0)
159 udev->speed = USB_SPEED_HIGH;
160
Simon Glassbc4546a2015-03-25 12:22:41 -0600161 return 0;
162}
163
164static int sandbox_usb_probe(struct udevice *dev)
165{
166 return 0;
167}
168
169static const struct dm_usb_ops sandbox_usb_ops = {
170 .control = sandbox_submit_control,
171 .bulk = sandbox_submit_bulk,
Simon Glass90421792015-11-08 23:48:05 -0700172 .interrupt = sandbox_submit_int,
Simon Glassbc4546a2015-03-25 12:22:41 -0600173 .alloc_device = sandbox_alloc_device,
174};
175
176static const struct udevice_id sandbox_usb_ids[] = {
177 { .compatible = "sandbox,usb" },
178 { }
179};
180
181U_BOOT_DRIVER(usb_sandbox) = {
182 .name = "usb_sandbox",
183 .id = UCLASS_USB,
184 .of_match = sandbox_usb_ids,
185 .probe = sandbox_usb_probe,
186 .ops = &sandbox_usb_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700187 .priv_auto = sizeof(struct sandbox_usb_ctrl),
Simon Glassbc4546a2015-03-25 12:22:41 -0600188};