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 static struct usb_generic_descriptor **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 = find_descriptor((struct usb_generic_descriptor **)plat->desc_list,
97 debug("%s: Could not find descriptor type %d, index %d\n",
98 __func__, type, index);
101 for (upto = 0; *ptr && upto < length; ptr++, upto += todo) {
102 todo = min(length - upto, (int)(*ptr)->bLength);
104 memcpy(buffer + upto, *ptr, todo);
107 return upto ? upto : length ? -EIO : 0;
110 static int usb_emul_find_devnum(int devnum, struct udevice **emulp)
117 ret = uclass_get(UCLASS_USB_EMUL, &uc);
120 uclass_foreach_dev(dev, uc) {
121 struct usb_dev_platdata *udev = dev_get_parent_platdata(dev);
123 if (udev->devnum == devnum) {
124 debug("%s: Found emulator '%s', addr %d\n", __func__,
125 dev->name, udev->devnum);
131 debug("%s: No emulator found, addr %d\n", __func__, devnum);
135 int usb_emul_find(struct udevice *bus, ulong pipe, struct udevice **emulp)
137 int devnum = usb_pipedevice(pipe);
139 return usb_emul_find_devnum(devnum, emulp);
142 int usb_emul_find_for_dev(struct udevice *dev, struct udevice **emulp)
144 struct usb_dev_platdata *udev = dev_get_parent_platdata(dev);
146 return usb_emul_find_devnum(udev->devnum, emulp);
149 int usb_emul_control(struct udevice *emul, struct usb_device *udev,
150 unsigned long pipe, void *buffer, int length,
151 struct devrequest *setup)
153 struct dm_usb_ops *ops = usb_get_emul_ops(emul);
154 struct usb_dev_platdata *plat;
157 /* We permit getting the descriptor before we are probed */
158 plat = dev_get_parent_platdata(emul);
161 debug("%s: dev=%s\n", __func__, emul->name);
162 if (pipe == usb_rcvctrlpipe(udev, 0)) {
163 switch (setup->request) {
164 case USB_REQ_GET_DESCRIPTOR: {
165 return usb_emul_get_descriptor(plat, setup->value,
169 ret = device_probe(emul);
172 return ops->control(emul, udev, pipe, buffer, length,
175 } else if (pipe == usb_snddefctrl(udev)) {
176 switch (setup->request) {
177 case USB_REQ_SET_ADDRESS:
178 debug(" ** set address %s %d\n", emul->name,
180 plat->devnum = setup->value;
183 debug("requestsend =%x\n", setup->request);
186 } else if (pipe == usb_sndctrlpipe(udev, 0)) {
187 switch (setup->request) {
188 case USB_REQ_SET_CONFIGURATION:
189 plat->configno = setup->value;
192 ret = device_probe(emul);
195 return ops->control(emul, udev, pipe, buffer, length,
199 debug("pipe=%lx\n", pipe);
204 int usb_emul_bulk(struct udevice *emul, struct usb_device *udev,
205 unsigned long pipe, void *buffer, int length)
207 struct dm_usb_ops *ops = usb_get_emul_ops(emul);
210 /* We permit getting the descriptor before we are probed */
213 debug("%s: dev=%s\n", __func__, emul->name);
214 ret = device_probe(emul);
217 return ops->bulk(emul, udev, pipe, buffer, length);
220 int usb_emul_int(struct udevice *emul, struct usb_device *udev,
221 unsigned long pipe, void *buffer, int length, int interval)
223 struct dm_usb_ops *ops = usb_get_emul_ops(emul);
227 debug("%s: dev=%s\n", __func__, emul->name);
229 return ops->interrupt(emul, udev, pipe, buffer, length, interval);
232 int usb_emul_setup_device(struct udevice *dev, int maxpacketsize,
233 struct usb_string *strings, void **desc_list)
235 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
236 struct usb_generic_descriptor **ptr;
237 struct usb_config_descriptor *cdesc;
240 plat->strings = strings;
241 plat->desc_list = (struct usb_generic_descriptor **)desc_list;
243 /* Fill in wTotalLength for each configuration descriptor */
244 ptr = plat->desc_list;
245 for (cdesc = NULL, upto = 0; *ptr; upto += (*ptr)->bLength, ptr++) {
246 debug(" - upto=%d, type=%d\n", upto, (*ptr)->bDescriptorType);
247 if ((*ptr)->bDescriptorType == USB_DT_CONFIG) {
249 cdesc->wTotalLength = upto;
250 debug("%s: config %d length %d\n", __func__,
251 cdesc->bConfigurationValue,
254 cdesc = (struct usb_config_descriptor *)*ptr;
259 cdesc->wTotalLength = upto;
260 debug("%s: config %d length %d\n", __func__,
261 cdesc->bConfigurationValue, cdesc->wTotalLength);
267 void usb_emul_reset(struct udevice *dev)
269 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
275 UCLASS_DRIVER(usb_emul) = {
276 .id = UCLASS_USB_EMUL,
278 .post_bind = dm_scan_fdt_dev,
279 .per_child_auto_alloc_size = sizeof(struct usb_device),
280 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),