blob: 1203eb2f11a8e88dc22aa6def5fa509d5eb5e5d2 [file] [log] [blame]
Simon Glass019808f2015-03-25 12:22:37 -06001/*
2 * (C) Copyright 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <dm.h>
10#include <usb.h>
Simon Glass019808f2015-03-25 12:22:37 -060011#include <dm/device-internal.h>
12
13DECLARE_GLOBAL_DATA_PTR;
14
15static int copy_to_unicode(char *buff, int length, const char *str)
16{
17 int ptr;
18 int i;
19
20 if (length < 2)
21 return 0;
22 buff[1] = USB_DT_STRING;
23 for (ptr = 2, i = 0; ptr + 1 < length && *str; i++, ptr += 2) {
24 buff[ptr] = str[i];
25 buff[ptr + 1] = 0;
26 }
27 buff[0] = ptr;
28
29 return ptr;
30}
31
32static int usb_emul_get_string(struct usb_string *strings, int index,
33 char *buff, int length)
34{
35 if (index == 0) {
36 char *desc = buff;
37
38 desc[0] = 4;
39 desc[1] = USB_DT_STRING;
40 desc[2] = 0x09;
41 desc[3] = 0x14;
42 return 4;
43 } else if (strings) {
44 struct usb_string *ptr;
45
46 for (ptr = strings; ptr->s; ptr++) {
47 if (ptr->id == index)
48 return copy_to_unicode(buff, length, ptr->s);
49 }
50 }
51
52 return -EINVAL;
53}
54
55static struct usb_generic_descriptor **find_descriptor(
56 struct usb_generic_descriptor **ptr, int type, int index)
57{
58 debug("%s: type=%x, index=%d\n", __func__, type, index);
59 for (; *ptr; ptr++) {
60 if ((*ptr)->bDescriptorType != type)
61 continue;
62 switch (type) {
63 case USB_DT_CONFIG: {
64 struct usb_config_descriptor *cdesc;
65
66 cdesc = (struct usb_config_descriptor *)*ptr;
67 if (cdesc && cdesc->bConfigurationValue == index)
68 return ptr;
69 break;
70 }
71 default:
72 return ptr;
73 }
74 }
75 debug("%s: config ptr=%p\n", __func__, *ptr);
76
77 return ptr;
78}
79
80static int usb_emul_get_descriptor(struct usb_dev_platdata *plat, int value,
81 void *buffer, int length)
82{
83 struct usb_generic_descriptor **ptr;
84 int type = value >> 8;
85 int index = value & 0xff;
86 int upto, todo;
87
88 debug("%s: type=%d, index=%d, plat=%p\n", __func__, type, index, plat);
89 if (type == USB_DT_STRING) {
90 return usb_emul_get_string(plat->strings, index, buffer,
91 length);
92 }
93
94 ptr = find_descriptor((struct usb_generic_descriptor **)plat->desc_list,
95 type, index);
96 if (!ptr) {
97 debug("%s: Could not find descriptor type %d, index %d\n",
98 __func__, type, index);
99 return -ENOENT;
100 }
101 for (upto = 0; *ptr && upto < length; ptr++, upto += todo) {
102 todo = min(length - upto, (int)(*ptr)->bLength);
103
104 memcpy(buffer + upto, *ptr, todo);
105 }
106
107 return upto ? upto : length ? -EIO : 0;
108}
109
Bin Meng84aa8532017-10-01 06:19:39 -0700110static int usb_emul_find_devnum(int devnum, int port1, struct udevice **emulp)
Simon Glass019808f2015-03-25 12:22:37 -0600111{
Simon Glass019808f2015-03-25 12:22:37 -0600112 struct udevice *dev;
113 struct uclass *uc;
114 int ret;
115
116 *emulp = NULL;
117 ret = uclass_get(UCLASS_USB_EMUL, &uc);
118 if (ret)
119 return ret;
120 uclass_foreach_dev(dev, uc) {
121 struct usb_dev_platdata *udev = dev_get_parent_platdata(dev);
122
Bin Meng84aa8532017-10-01 06:19:39 -0700123 /*
124 * devnum is initialzied to zero at the beginning of the
125 * enumeration process in usb_setup_device(). At this
126 * point, udev->devnum has not been assigned to any valid
127 * USB address either, so we can't rely on the comparison
128 * result between udev->devnum and devnum to select an
129 * emulator device.
130 */
131 if (!devnum) {
132 struct usb_emul_platdata *plat;
133
134 /*
135 * If the parent is sandbox USB controller, we are
136 * the root hub. And there is only one root hub
137 * in the system.
138 */
139 if (device_get_uclass_id(dev->parent) == UCLASS_USB) {
140 debug("%s: Found emulator '%s'\n",
141 __func__, dev->name);
142 *emulp = dev;
143 return 0;
144 }
145
146 plat = dev_get_uclass_platdata(dev);
147 if (plat->port1 == port1) {
148 debug("%s: Found emulator '%s', port %d\n",
149 __func__, dev->name, port1);
150 *emulp = dev;
151 return 0;
152 }
153 } else if (udev->devnum == devnum) {
Simon Glass019808f2015-03-25 12:22:37 -0600154 debug("%s: Found emulator '%s', addr %d\n", __func__,
155 dev->name, udev->devnum);
156 *emulp = dev;
157 return 0;
158 }
159 }
160
161 debug("%s: No emulator found, addr %d\n", __func__, devnum);
162 return -ENOENT;
163}
164
Bin Meng84aa8532017-10-01 06:19:39 -0700165int usb_emul_find(struct udevice *bus, ulong pipe, int port1,
166 struct udevice **emulp)
Simon Glassaf9c7c12015-11-08 23:47:55 -0700167{
168 int devnum = usb_pipedevice(pipe);
169
Bin Meng84aa8532017-10-01 06:19:39 -0700170 return usb_emul_find_devnum(devnum, port1, emulp);
Simon Glassaf9c7c12015-11-08 23:47:55 -0700171}
172
173int usb_emul_find_for_dev(struct udevice *dev, struct udevice **emulp)
174{
175 struct usb_dev_platdata *udev = dev_get_parent_platdata(dev);
176
Bin Meng84aa8532017-10-01 06:19:39 -0700177 return usb_emul_find_devnum(udev->devnum, 0, emulp);
Simon Glassaf9c7c12015-11-08 23:47:55 -0700178}
179
Simon Glass019808f2015-03-25 12:22:37 -0600180int usb_emul_control(struct udevice *emul, struct usb_device *udev,
181 unsigned long pipe, void *buffer, int length,
182 struct devrequest *setup)
183{
184 struct dm_usb_ops *ops = usb_get_emul_ops(emul);
185 struct usb_dev_platdata *plat;
186 int ret;
187
188 /* We permit getting the descriptor before we are probed */
189 plat = dev_get_parent_platdata(emul);
190 if (!ops->control)
191 return -ENOSYS;
192 debug("%s: dev=%s\n", __func__, emul->name);
193 if (pipe == usb_rcvctrlpipe(udev, 0)) {
194 switch (setup->request) {
195 case USB_REQ_GET_DESCRIPTOR: {
196 return usb_emul_get_descriptor(plat, setup->value,
197 buffer, length);
198 }
199 default:
200 ret = device_probe(emul);
201 if (ret)
202 return ret;
203 return ops->control(emul, udev, pipe, buffer, length,
204 setup);
205 }
206 } else if (pipe == usb_snddefctrl(udev)) {
207 switch (setup->request) {
208 case USB_REQ_SET_ADDRESS:
209 debug(" ** set address %s %d\n", emul->name,
210 setup->value);
211 plat->devnum = setup->value;
212 return 0;
213 default:
214 debug("requestsend =%x\n", setup->request);
215 break;
216 }
217 } else if (pipe == usb_sndctrlpipe(udev, 0)) {
218 switch (setup->request) {
219 case USB_REQ_SET_CONFIGURATION:
220 plat->configno = setup->value;
221 return 0;
222 default:
223 ret = device_probe(emul);
224 if (ret)
225 return ret;
226 return ops->control(emul, udev, pipe, buffer, length,
227 setup);
228 }
229 }
230 debug("pipe=%lx\n", pipe);
231
232 return -EIO;
233}
234
235int usb_emul_bulk(struct udevice *emul, struct usb_device *udev,
236 unsigned long pipe, void *buffer, int length)
237{
238 struct dm_usb_ops *ops = usb_get_emul_ops(emul);
239 int ret;
240
241 /* We permit getting the descriptor before we are probed */
242 if (!ops->bulk)
243 return -ENOSYS;
244 debug("%s: dev=%s\n", __func__, emul->name);
245 ret = device_probe(emul);
246 if (ret)
247 return ret;
248 return ops->bulk(emul, udev, pipe, buffer, length);
249}
250
Simon Glassb70a3fe2015-11-08 23:48:05 -0700251int usb_emul_int(struct udevice *emul, struct usb_device *udev,
252 unsigned long pipe, void *buffer, int length, int interval)
253{
254 struct dm_usb_ops *ops = usb_get_emul_ops(emul);
255
256 if (!ops->interrupt)
257 return -ENOSYS;
258 debug("%s: dev=%s\n", __func__, emul->name);
259
260 return ops->interrupt(emul, udev, pipe, buffer, length, interval);
261}
262
Bin Meng98b639f2017-10-01 06:19:36 -0700263int usb_emul_setup_device(struct udevice *dev, struct usb_string *strings,
264 void **desc_list)
Simon Glass019808f2015-03-25 12:22:37 -0600265{
266 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
267 struct usb_generic_descriptor **ptr;
268 struct usb_config_descriptor *cdesc;
269 int upto;
270
271 plat->strings = strings;
272 plat->desc_list = (struct usb_generic_descriptor **)desc_list;
273
274 /* Fill in wTotalLength for each configuration descriptor */
275 ptr = plat->desc_list;
276 for (cdesc = NULL, upto = 0; *ptr; upto += (*ptr)->bLength, ptr++) {
277 debug(" - upto=%d, type=%d\n", upto, (*ptr)->bDescriptorType);
278 if ((*ptr)->bDescriptorType == USB_DT_CONFIG) {
279 if (cdesc) {
280 cdesc->wTotalLength = upto;
281 debug("%s: config %d length %d\n", __func__,
282 cdesc->bConfigurationValue,
283 cdesc->bLength);
284 }
285 cdesc = (struct usb_config_descriptor *)*ptr;
286 upto = 0;
287 }
288 }
289 if (cdesc) {
290 cdesc->wTotalLength = upto;
291 debug("%s: config %d length %d\n", __func__,
292 cdesc->bConfigurationValue, cdesc->wTotalLength);
293 }
294
295 return 0;
296}
297
Simon Glass019808f2015-03-25 12:22:37 -0600298void usb_emul_reset(struct udevice *dev)
299{
300 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
301
302 plat->devnum = 0;
303 plat->configno = 0;
304}
305
306UCLASS_DRIVER(usb_emul) = {
307 .id = UCLASS_USB_EMUL,
308 .name = "usb_emul",
Simon Glass91195482016-07-05 17:10:10 -0600309 .post_bind = dm_scan_fdt_dev,
Bin Meng84aa8532017-10-01 06:19:39 -0700310 .per_device_platdata_auto_alloc_size = sizeof(struct usb_emul_platdata),
Simon Glass019808f2015-03-25 12:22:37 -0600311 .per_child_auto_alloc_size = sizeof(struct usb_device),
312 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
313};