1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
10 #include <dm/device-internal.h>
12 static int copy_to_unicode(char *buff, int length, const char *str)
19 buff[1] = USB_DT_STRING;
20 for (ptr = 2, i = 0; ptr + 1 < length && *str; i++, ptr += 2) {
29 static int usb_emul_get_string(struct usb_string *strings, int index,
30 char *buff, int length)
36 desc[1] = USB_DT_STRING;
41 struct usb_string *ptr;
43 for (ptr = strings; ptr->s; ptr++) {
45 return copy_to_unicode(buff, length, ptr->s);
52 struct usb_generic_descriptor **usb_emul_find_descriptor(
53 struct usb_generic_descriptor **ptr, int type, int index)
55 debug("%s: type=%x, index=%d\n", __func__, type, index);
57 if ((*ptr)->bDescriptorType != type)
61 struct usb_config_descriptor *cdesc;
63 cdesc = (struct usb_config_descriptor *)*ptr;
64 if (cdesc && cdesc->bConfigurationValue == index)
72 debug("%s: config ptr=%p\n", __func__, *ptr);
77 static int usb_emul_get_descriptor(struct usb_dev_platdata *plat, int value,
78 void *buffer, int length)
80 struct usb_generic_descriptor **ptr;
81 int type = value >> 8;
82 int index = value & 0xff;
85 debug("%s: type=%d, index=%d, plat=%p\n", __func__, type, index, plat);
86 if (type == USB_DT_STRING) {
87 return usb_emul_get_string(plat->strings, index, buffer,
91 ptr = usb_emul_find_descriptor(plat->desc_list, type, index);
93 debug("%s: Could not find descriptor type %d, index %d\n",
94 __func__, type, index);
97 for (upto = 0; *ptr && upto < length; ptr++, upto += todo) {
98 todo = min(length - upto, (int)(*ptr)->bLength);
100 memcpy(buffer + upto, *ptr, todo);
103 return upto ? upto : length ? -EIO : 0;
106 static int usb_emul_find_devnum(int devnum, int port1, struct udevice **emulp)
113 ret = uclass_get(UCLASS_USB_EMUL, &uc);
116 uclass_foreach_dev(dev, uc) {
117 struct usb_dev_platdata *udev = dev_get_parent_platdata(dev);
120 * devnum is initialzied to zero at the beginning of the
121 * enumeration process in usb_setup_device(). At this
122 * point, udev->devnum has not been assigned to any valid
123 * USB address either, so we can't rely on the comparison
124 * result between udev->devnum and devnum to select an
128 struct usb_emul_platdata *plat;
131 * If the parent is sandbox USB controller, we are
132 * the root hub. And there is only one root hub
135 if (device_get_uclass_id(dev->parent) == UCLASS_USB) {
136 debug("%s: Found emulator '%s'\n",
137 __func__, dev->name);
142 plat = dev_get_uclass_platdata(dev);
143 if (plat->port1 == port1) {
144 debug("%s: Found emulator '%s', port %d\n",
145 __func__, dev->name, port1);
149 } else if (udev->devnum == devnum) {
150 debug("%s: Found emulator '%s', addr %d\n", __func__,
151 dev->name, udev->devnum);
157 debug("%s: No emulator found, addr %d\n", __func__, devnum);
161 int usb_emul_find(struct udevice *bus, ulong pipe, int port1,
162 struct udevice **emulp)
164 int devnum = usb_pipedevice(pipe);
166 return usb_emul_find_devnum(devnum, port1, emulp);
169 int usb_emul_find_for_dev(struct udevice *dev, struct udevice **emulp)
171 struct usb_dev_platdata *udev = dev_get_parent_platdata(dev);
173 return usb_emul_find_devnum(udev->devnum, 0, emulp);
176 int usb_emul_control(struct udevice *emul, struct usb_device *udev,
177 unsigned long pipe, void *buffer, int length,
178 struct devrequest *setup)
180 struct dm_usb_ops *ops = usb_get_emul_ops(emul);
181 struct usb_dev_platdata *plat;
184 /* We permit getting the descriptor before we are probed */
185 plat = dev_get_parent_platdata(emul);
188 debug("%s: dev=%s\n", __func__, emul->name);
189 if (pipe == usb_rcvctrlpipe(udev, 0)) {
190 switch (setup->request) {
191 case USB_REQ_GET_DESCRIPTOR: {
192 return usb_emul_get_descriptor(plat, setup->value,
196 ret = device_probe(emul);
199 return ops->control(emul, udev, pipe, buffer, length,
202 } else if (pipe == usb_snddefctrl(udev)) {
203 switch (setup->request) {
204 case USB_REQ_SET_ADDRESS:
205 debug(" ** set address %s %d\n", emul->name,
207 plat->devnum = setup->value;
210 debug("requestsend =%x\n", setup->request);
213 } else if (pipe == usb_sndctrlpipe(udev, 0)) {
214 switch (setup->request) {
215 case USB_REQ_SET_CONFIGURATION:
216 plat->configno = setup->value;
219 ret = device_probe(emul);
222 return ops->control(emul, udev, pipe, buffer, length,
226 debug("pipe=%lx\n", pipe);
231 int usb_emul_bulk(struct udevice *emul, struct usb_device *udev,
232 unsigned long pipe, void *buffer, int length)
234 struct dm_usb_ops *ops = usb_get_emul_ops(emul);
237 /* We permit getting the descriptor before we are probed */
240 debug("%s: dev=%s\n", __func__, emul->name);
241 ret = device_probe(emul);
244 return ops->bulk(emul, udev, pipe, buffer, length);
247 int usb_emul_int(struct udevice *emul, struct usb_device *udev,
248 unsigned long pipe, void *buffer, int length, int interval)
250 struct dm_usb_ops *ops = usb_get_emul_ops(emul);
254 debug("%s: dev=%s\n", __func__, emul->name);
256 return ops->interrupt(emul, udev, pipe, buffer, length, interval);
259 int usb_emul_setup_device(struct udevice *dev, struct usb_string *strings,
262 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
263 struct usb_generic_descriptor **ptr;
264 struct usb_config_descriptor *cdesc;
267 plat->strings = strings;
268 plat->desc_list = (struct usb_generic_descriptor **)desc_list;
270 /* Fill in wTotalLength for each configuration descriptor */
271 ptr = plat->desc_list;
272 for (cdesc = NULL, upto = 0; *ptr; upto += (*ptr)->bLength, ptr++) {
273 debug(" - upto=%d, type=%d\n", upto, (*ptr)->bDescriptorType);
274 if ((*ptr)->bDescriptorType == USB_DT_CONFIG) {
276 cdesc->wTotalLength = upto;
277 debug("%s: config %d length %d\n", __func__,
278 cdesc->bConfigurationValue,
281 cdesc = (struct usb_config_descriptor *)*ptr;
286 cdesc->wTotalLength = upto;
287 debug("%s: config %d length %d\n", __func__,
288 cdesc->bConfigurationValue, cdesc->wTotalLength);
294 UCLASS_DRIVER(usb_emul) = {
295 .id = UCLASS_USB_EMUL,
297 .post_bind = dm_scan_fdt_dev,
298 .per_device_platdata_auto_alloc_size = sizeof(struct usb_emul_platdata),
299 .per_child_auto_alloc_size = sizeof(struct usb_device),
300 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),