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 static int usb_emul_find_devnum(int devnum, struct udevice **emulp)
118 ret = uclass_get(UCLASS_USB_EMUL, &uc);
121 uclass_foreach_dev(dev, uc) {
122 struct usb_dev_platdata *udev = dev_get_parent_platdata(dev);
124 if (udev->devnum == devnum) {
125 debug("%s: Found emulator '%s', addr %d\n", __func__,
126 dev->name, udev->devnum);
132 debug("%s: No emulator found, addr %d\n", __func__, devnum);
136 int usb_emul_find(struct udevice *bus, ulong pipe, struct udevice **emulp)
138 int devnum = usb_pipedevice(pipe);
140 return usb_emul_find_devnum(devnum, emulp);
143 int usb_emul_find_for_dev(struct udevice *dev, struct udevice **emulp)
145 struct usb_dev_platdata *udev = dev_get_parent_platdata(dev);
147 return usb_emul_find_devnum(udev->devnum, emulp);
150 int usb_emul_control(struct udevice *emul, struct usb_device *udev,
151 unsigned long pipe, void *buffer, int length,
152 struct devrequest *setup)
154 struct dm_usb_ops *ops = usb_get_emul_ops(emul);
155 struct usb_dev_platdata *plat;
158 /* We permit getting the descriptor before we are probed */
159 plat = dev_get_parent_platdata(emul);
162 debug("%s: dev=%s\n", __func__, emul->name);
163 if (pipe == usb_rcvctrlpipe(udev, 0)) {
164 switch (setup->request) {
165 case USB_REQ_GET_DESCRIPTOR: {
166 return usb_emul_get_descriptor(plat, setup->value,
170 ret = device_probe(emul);
173 return ops->control(emul, udev, pipe, buffer, length,
176 } else if (pipe == usb_snddefctrl(udev)) {
177 switch (setup->request) {
178 case USB_REQ_SET_ADDRESS:
179 debug(" ** set address %s %d\n", emul->name,
181 plat->devnum = setup->value;
184 debug("requestsend =%x\n", setup->request);
187 } else if (pipe == usb_sndctrlpipe(udev, 0)) {
188 switch (setup->request) {
189 case USB_REQ_SET_CONFIGURATION:
190 plat->configno = setup->value;
193 ret = device_probe(emul);
196 return ops->control(emul, udev, pipe, buffer, length,
200 debug("pipe=%lx\n", pipe);
205 int usb_emul_bulk(struct udevice *emul, struct usb_device *udev,
206 unsigned long pipe, void *buffer, int length)
208 struct dm_usb_ops *ops = usb_get_emul_ops(emul);
211 /* We permit getting the descriptor before we are probed */
214 debug("%s: dev=%s\n", __func__, emul->name);
215 ret = device_probe(emul);
218 return ops->bulk(emul, udev, pipe, buffer, length);
221 int usb_emul_int(struct udevice *emul, struct usb_device *udev,
222 unsigned long pipe, void *buffer, int length, int interval)
224 struct dm_usb_ops *ops = usb_get_emul_ops(emul);
228 debug("%s: dev=%s\n", __func__, emul->name);
230 return ops->interrupt(emul, udev, pipe, buffer, length, interval);
233 int usb_emul_setup_device(struct udevice *dev, int maxpacketsize,
234 struct usb_string *strings, void **desc_list)
236 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
237 struct usb_generic_descriptor **ptr;
238 struct usb_config_descriptor *cdesc;
241 plat->strings = strings;
242 plat->desc_list = (struct usb_generic_descriptor **)desc_list;
244 /* Fill in wTotalLength for each configuration descriptor */
245 ptr = plat->desc_list;
246 for (cdesc = NULL, upto = 0; *ptr; upto += (*ptr)->bLength, ptr++) {
247 debug(" - upto=%d, type=%d\n", upto, (*ptr)->bDescriptorType);
248 if ((*ptr)->bDescriptorType == USB_DT_CONFIG) {
250 cdesc->wTotalLength = upto;
251 debug("%s: config %d length %d\n", __func__,
252 cdesc->bConfigurationValue,
255 cdesc = (struct usb_config_descriptor *)*ptr;
260 cdesc->wTotalLength = upto;
261 debug("%s: config %d length %d\n", __func__,
262 cdesc->bConfigurationValue, cdesc->wTotalLength);
268 int usb_emul_post_bind(struct udevice *dev)
270 /* Scan the bus for devices */
271 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
274 void usb_emul_reset(struct udevice *dev)
276 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
282 UCLASS_DRIVER(usb_emul) = {
283 .id = UCLASS_USB_EMUL,
285 .post_bind = usb_emul_post_bind,
286 .per_child_auto_alloc_size = sizeof(struct usb_device),
287 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),