2 * (C) Copyright 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
12 #include <dm/device-internal.h>
14 DECLARE_GLOBAL_DATA_PTR;
16 static int copy_to_unicode(char *buff, int length, const char *str)
23 buff[1] = USB_DT_STRING;
24 for (ptr = 2, i = 0; ptr + 1 < length && *str; i++, ptr += 2) {
33 static int usb_emul_get_string(struct usb_string *strings, int index,
34 char *buff, int length)
40 desc[1] = USB_DT_STRING;
45 struct usb_string *ptr;
47 for (ptr = strings; ptr->s; ptr++) {
49 return copy_to_unicode(buff, length, ptr->s);
56 static struct usb_generic_descriptor **find_descriptor(
57 struct usb_generic_descriptor **ptr, int type, int index)
59 debug("%s: type=%x, index=%d\n", __func__, type, index);
61 if ((*ptr)->bDescriptorType != type)
65 struct usb_config_descriptor *cdesc;
67 cdesc = (struct usb_config_descriptor *)*ptr;
68 if (cdesc && cdesc->bConfigurationValue == index)
76 debug("%s: config ptr=%p\n", __func__, *ptr);
81 static int usb_emul_get_descriptor(struct usb_dev_platdata *plat, int value,
82 void *buffer, int length)
84 struct usb_generic_descriptor **ptr;
85 int type = value >> 8;
86 int index = value & 0xff;
89 debug("%s: type=%d, index=%d, plat=%p\n", __func__, type, index, plat);
90 if (type == USB_DT_STRING) {
91 return usb_emul_get_string(plat->strings, index, buffer,
95 ptr = find_descriptor((struct usb_generic_descriptor **)plat->desc_list,
98 debug("%s: Could not find descriptor type %d, index %d\n",
99 __func__, type, index);
102 for (upto = 0; *ptr && upto < length; ptr++, upto += todo) {
103 todo = min(length - upto, (int)(*ptr)->bLength);
105 memcpy(buffer + upto, *ptr, todo);
108 return upto ? upto : length ? -EIO : 0;
111 int usb_emul_find(struct udevice *bus, ulong pipe, struct udevice **emulp)
113 int devnum = usb_pipedevice(pipe);
119 ret = uclass_get(UCLASS_USB_EMUL, &uc);
122 uclass_foreach_dev(dev, uc) {
123 struct usb_dev_platdata *udev = dev_get_parent_platdata(dev);
125 if (udev->devnum == devnum) {
126 debug("%s: Found emulator '%s', addr %d\n", __func__,
127 dev->name, udev->devnum);
133 debug("%s: No emulator found, addr %d\n", __func__, devnum);
137 int usb_emul_control(struct udevice *emul, struct usb_device *udev,
138 unsigned long pipe, void *buffer, int length,
139 struct devrequest *setup)
141 struct dm_usb_ops *ops = usb_get_emul_ops(emul);
142 struct usb_dev_platdata *plat;
145 /* We permit getting the descriptor before we are probed */
146 plat = dev_get_parent_platdata(emul);
149 debug("%s: dev=%s\n", __func__, emul->name);
150 if (pipe == usb_rcvctrlpipe(udev, 0)) {
151 switch (setup->request) {
152 case USB_REQ_GET_DESCRIPTOR: {
153 return usb_emul_get_descriptor(plat, setup->value,
157 ret = device_probe(emul);
160 return ops->control(emul, udev, pipe, buffer, length,
163 } else if (pipe == usb_snddefctrl(udev)) {
164 switch (setup->request) {
165 case USB_REQ_SET_ADDRESS:
166 debug(" ** set address %s %d\n", emul->name,
168 plat->devnum = setup->value;
171 debug("requestsend =%x\n", setup->request);
174 } else if (pipe == usb_sndctrlpipe(udev, 0)) {
175 switch (setup->request) {
176 case USB_REQ_SET_CONFIGURATION:
177 plat->configno = setup->value;
180 ret = device_probe(emul);
183 return ops->control(emul, udev, pipe, buffer, length,
187 debug("pipe=%lx\n", pipe);
192 int usb_emul_bulk(struct udevice *emul, struct usb_device *udev,
193 unsigned long pipe, void *buffer, int length)
195 struct dm_usb_ops *ops = usb_get_emul_ops(emul);
198 /* We permit getting the descriptor before we are probed */
201 debug("%s: dev=%s\n", __func__, emul->name);
202 ret = device_probe(emul);
205 return ops->bulk(emul, udev, pipe, buffer, length);
208 int usb_emul_setup_device(struct udevice *dev, int maxpacketsize,
209 struct usb_string *strings, void **desc_list)
211 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
212 struct usb_generic_descriptor **ptr;
213 struct usb_config_descriptor *cdesc;
216 plat->strings = strings;
217 plat->desc_list = (struct usb_generic_descriptor **)desc_list;
219 /* Fill in wTotalLength for each configuration descriptor */
220 ptr = plat->desc_list;
221 for (cdesc = NULL, upto = 0; *ptr; upto += (*ptr)->bLength, ptr++) {
222 debug(" - upto=%d, type=%d\n", upto, (*ptr)->bDescriptorType);
223 if ((*ptr)->bDescriptorType == USB_DT_CONFIG) {
225 cdesc->wTotalLength = upto;
226 debug("%s: config %d length %d\n", __func__,
227 cdesc->bConfigurationValue,
230 cdesc = (struct usb_config_descriptor *)*ptr;
235 cdesc->wTotalLength = upto;
236 debug("%s: config %d length %d\n", __func__,
237 cdesc->bConfigurationValue, cdesc->wTotalLength);
243 int usb_emul_post_bind(struct udevice *dev)
245 /* Scan the bus for devices */
246 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
249 void usb_emul_reset(struct udevice *dev)
251 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
257 UCLASS_DRIVER(usb_emul) = {
258 .id = UCLASS_USB_EMUL,
260 .post_bind = usb_emul_post_bind,
261 .per_child_auto_alloc_size = sizeof(struct usb_device),
262 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),