1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * (C) Copyright 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
5 */
6
7 #include <dm.h>
8 #include <log.h>
9 #include <usb.h>
10 #include <dm/root.h>
11 #include <linux/usb/gadget.h>
12
13 struct sandbox_udc {
14 struct usb_gadget gadget;
15 };
16
17 struct sandbox_udc *this_controller;
18
19 struct sandbox_usb_ctrl {
20 int rootdev;
21 };
22
usbmon_trace(struct udevice * bus,ulong pipe,struct devrequest * setup,struct udevice * emul)23 static 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',
32 dev_seq(bus),
33 (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
sandbox_submit_control(struct udevice * bus,struct usb_device * udev,unsigned long pipe,void * buffer,int length,struct devrequest * setup)45 static 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 {
51 struct sandbox_usb_ctrl *ctrl = dev_get_priv(bus);
52 struct udevice *emul;
53 int ret;
54
55 /* Just use child of dev as emulator? */
56 debug("%s: bus=%s\n", __func__, bus->name);
57 ret = usb_emul_find(bus, pipe, udev->portnr, &emul);
58 usbmon_trace(bus, pipe, setup, emul);
59 if (ret)
60 return ret;
61
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
69 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
sandbox_submit_bulk(struct udevice * bus,struct usb_device * udev,unsigned long pipe,void * buffer,int length)82 static 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);
90 ret = usb_emul_find(bus, pipe, udev->portnr, &emul);
91 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
sandbox_submit_int(struct udevice * bus,struct usb_device * udev,unsigned long pipe,void * buffer,int length,int interval,bool nonblock)107 static int sandbox_submit_int(struct udevice *bus, struct usb_device *udev,
108 unsigned long pipe, void *buffer, int length,
109 int interval, bool nonblock)
110 {
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);
116 ret = usb_emul_find(bus, pipe, udev->portnr, &emul);
117 usbmon_trace(bus, pipe, NULL, emul);
118 if (ret)
119 return ret;
120 ret = usb_emul_int(emul, udev, pipe, buffer, length, interval,
121 nonblock);
122
123 return ret;
124 }
125
126 #if !CONFIG_IS_ENABLED(DM_USB_GADGET)
usb_gadget_register_driver(struct usb_gadget_driver * driver)127 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
128 {
129 struct sandbox_udc *dev = this_controller;
130
131 return driver->bind(&dev->gadget);
132 }
133
usb_gadget_unregister_driver(struct usb_gadget_driver * driver)134 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
135 {
136 struct sandbox_udc *dev = this_controller;
137
138 driver->unbind(&dev->gadget);
139
140 return 0;
141 }
142 #endif
143
sandbox_alloc_device(struct udevice * dev,struct usb_device * udev)144 static int sandbox_alloc_device(struct udevice *dev, struct usb_device *udev)
145 {
146 struct sandbox_usb_ctrl *ctrl = dev_get_priv(dev);
147
148 /*
149 * Root hub will be the first device to be initailized.
150 * If this device is a root hub, initialize its device speed
151 * to high speed as we are a USB 2.0 controller.
152 */
153 if (ctrl->rootdev == 0)
154 udev->speed = USB_SPEED_HIGH;
155
156 return 0;
157 }
158
sandbox_usb_probe(struct udevice * dev)159 static int sandbox_usb_probe(struct udevice *dev)
160 {
161 return 0;
162 }
163
164 static const struct dm_usb_ops sandbox_usb_ops = {
165 .control = sandbox_submit_control,
166 .bulk = sandbox_submit_bulk,
167 .interrupt = sandbox_submit_int,
168 .alloc_device = sandbox_alloc_device,
169 };
170
171 static const struct udevice_id sandbox_usb_ids[] = {
172 { .compatible = "sandbox,usb" },
173 { }
174 };
175
176 U_BOOT_DRIVER(usb_sandbox) = {
177 .name = "usb_sandbox",
178 .id = UCLASS_USB,
179 .of_match = sandbox_usb_ids,
180 .probe = sandbox_usb_probe,
181 .ops = &sandbox_usb_ops,
182 .priv_auto = sizeof(struct sandbox_usb_ctrl),
183 };
184