2 * (C) Copyright 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
11 #include <dm/device-internal.h>
13 DECLARE_GLOBAL_DATA_PTR;
15 static int copy_to_unicode(char *buff, int length, const char *str)
22 buff[1] = USB_DT_STRING;
23 for (ptr = 2, i = 0; ptr + 1 < length && *str; i++, ptr += 2) {
32 static int usb_emul_get_string(struct usb_string *strings, int index,
33 char *buff, int length)
39 desc[1] = USB_DT_STRING;
44 struct usb_string *ptr;
46 for (ptr = strings; ptr->s; ptr++) {
48 return copy_to_unicode(buff, length, ptr->s);
55 struct usb_generic_descriptor **usb_emul_find_descriptor(
56 struct usb_generic_descriptor **ptr, int type, int index)
58 debug("%s: type=%x, index=%d\n", __func__, type, index);
60 if ((*ptr)->bDescriptorType != type)
64 struct usb_config_descriptor *cdesc;
66 cdesc = (struct usb_config_descriptor *)*ptr;
67 if (cdesc && cdesc->bConfigurationValue == index)
75 debug("%s: config ptr=%p\n", __func__, *ptr);
80 static int usb_emul_get_descriptor(struct usb_dev_platdata *plat, int value,
81 void *buffer, int length)
83 struct usb_generic_descriptor **ptr;
84 int type = value >> 8;
85 int index = value & 0xff;
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,
94 ptr = usb_emul_find_descriptor(plat->desc_list, type, index);
96 debug("%s: Could not find descriptor type %d, index %d\n",
97 __func__, type, index);
100 for (upto = 0; *ptr && upto < length; ptr++, upto += todo) {
101 todo = min(length - upto, (int)(*ptr)->bLength);
103 memcpy(buffer + upto, *ptr, todo);
106 return upto ? upto : length ? -EIO : 0;
109 static int usb_emul_find_devnum(int devnum, int port1, struct udevice **emulp)
116 ret = uclass_get(UCLASS_USB_EMUL, &uc);
119 uclass_foreach_dev(dev, uc) {
120 struct usb_dev_platdata *udev = dev_get_parent_platdata(dev);
123 * devnum is initialzied to zero at the beginning of the
124 * enumeration process in usb_setup_device(). At this
125 * point, udev->devnum has not been assigned to any valid
126 * USB address either, so we can't rely on the comparison
127 * result between udev->devnum and devnum to select an
131 struct usb_emul_platdata *plat;
134 * If the parent is sandbox USB controller, we are
135 * the root hub. And there is only one root hub
138 if (device_get_uclass_id(dev->parent) == UCLASS_USB) {
139 debug("%s: Found emulator '%s'\n",
140 __func__, dev->name);
145 plat = dev_get_uclass_platdata(dev);
146 if (plat->port1 == port1) {
147 debug("%s: Found emulator '%s', port %d\n",
148 __func__, dev->name, port1);
152 } else if (udev->devnum == devnum) {
153 debug("%s: Found emulator '%s', addr %d\n", __func__,
154 dev->name, udev->devnum);
160 debug("%s: No emulator found, addr %d\n", __func__, devnum);
164 int usb_emul_find(struct udevice *bus, ulong pipe, int port1,
165 struct udevice **emulp)
167 int devnum = usb_pipedevice(pipe);
169 return usb_emul_find_devnum(devnum, port1, emulp);
172 int usb_emul_find_for_dev(struct udevice *dev, struct udevice **emulp)
174 struct usb_dev_platdata *udev = dev_get_parent_platdata(dev);
176 return usb_emul_find_devnum(udev->devnum, 0, emulp);
179 int usb_emul_control(struct udevice *emul, struct usb_device *udev,
180 unsigned long pipe, void *buffer, int length,
181 struct devrequest *setup)
183 struct dm_usb_ops *ops = usb_get_emul_ops(emul);
184 struct usb_dev_platdata *plat;
187 /* We permit getting the descriptor before we are probed */
188 plat = dev_get_parent_platdata(emul);
191 debug("%s: dev=%s\n", __func__, emul->name);
192 if (pipe == usb_rcvctrlpipe(udev, 0)) {
193 switch (setup->request) {
194 case USB_REQ_GET_DESCRIPTOR: {
195 return usb_emul_get_descriptor(plat, setup->value,
199 ret = device_probe(emul);
202 return ops->control(emul, udev, pipe, buffer, length,
205 } else if (pipe == usb_snddefctrl(udev)) {
206 switch (setup->request) {
207 case USB_REQ_SET_ADDRESS:
208 debug(" ** set address %s %d\n", emul->name,
210 plat->devnum = setup->value;
213 debug("requestsend =%x\n", setup->request);
216 } else if (pipe == usb_sndctrlpipe(udev, 0)) {
217 switch (setup->request) {
218 case USB_REQ_SET_CONFIGURATION:
219 plat->configno = setup->value;
222 ret = device_probe(emul);
225 return ops->control(emul, udev, pipe, buffer, length,
229 debug("pipe=%lx\n", pipe);
234 int usb_emul_bulk(struct udevice *emul, struct usb_device *udev,
235 unsigned long pipe, void *buffer, int length)
237 struct dm_usb_ops *ops = usb_get_emul_ops(emul);
240 /* We permit getting the descriptor before we are probed */
243 debug("%s: dev=%s\n", __func__, emul->name);
244 ret = device_probe(emul);
247 return ops->bulk(emul, udev, pipe, buffer, length);
250 int usb_emul_int(struct udevice *emul, struct usb_device *udev,
251 unsigned long pipe, void *buffer, int length, int interval)
253 struct dm_usb_ops *ops = usb_get_emul_ops(emul);
257 debug("%s: dev=%s\n", __func__, emul->name);
259 return ops->interrupt(emul, udev, pipe, buffer, length, interval);
262 int usb_emul_setup_device(struct udevice *dev, struct usb_string *strings,
265 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
266 struct usb_generic_descriptor **ptr;
267 struct usb_config_descriptor *cdesc;
270 plat->strings = strings;
271 plat->desc_list = (struct usb_generic_descriptor **)desc_list;
273 /* Fill in wTotalLength for each configuration descriptor */
274 ptr = plat->desc_list;
275 for (cdesc = NULL, upto = 0; *ptr; upto += (*ptr)->bLength, ptr++) {
276 debug(" - upto=%d, type=%d\n", upto, (*ptr)->bDescriptorType);
277 if ((*ptr)->bDescriptorType == USB_DT_CONFIG) {
279 cdesc->wTotalLength = upto;
280 debug("%s: config %d length %d\n", __func__,
281 cdesc->bConfigurationValue,
284 cdesc = (struct usb_config_descriptor *)*ptr;
289 cdesc->wTotalLength = upto;
290 debug("%s: config %d length %d\n", __func__,
291 cdesc->bConfigurationValue, cdesc->wTotalLength);
297 UCLASS_DRIVER(usb_emul) = {
298 .id = UCLASS_USB_EMUL,
300 .post_bind = dm_scan_fdt_dev,
301 .per_device_platdata_auto_alloc_size = sizeof(struct usb_emul_platdata),
302 .per_child_auto_alloc_size = sizeof(struct usb_device),
303 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),