1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2015 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6 
7 #define LOG_CATEGORY UCLASS_USB_EMUL
8 
9 #include <common.h>
10 #include <dm.h>
11 #include <log.h>
12 #include <usb.h>
13 #include <dm/device-internal.h>
14 
copy_to_unicode(char * buff,int length,const char * str)15 static int copy_to_unicode(char *buff, int length, const char *str)
16 {
17 	int ptr;
18 
19 	if (length < 2)
20 		return 0;
21 	buff[1] = USB_DT_STRING;
22 	for (ptr = 2; ptr + 1 < length && *str; str++, ptr += 2) {
23 		buff[ptr] = *str;
24 		buff[ptr + 1] = 0;
25 	}
26 	buff[0] = ptr;
27 
28 	return ptr;
29 }
30 
usb_emul_get_string(struct usb_string * strings,int index,char * buff,int length)31 static int usb_emul_get_string(struct usb_string *strings, int index,
32 			       char *buff, int length)
33 {
34 	if (index == 0) {
35 		char *desc = buff;
36 
37 		desc[0] = 4;
38 		desc[1] = USB_DT_STRING;
39 		desc[2] = 0x09;
40 		desc[3] = 0x14;
41 		return 4;
42 	} else if (strings) {
43 		struct usb_string *ptr;
44 
45 		for (ptr = strings; ptr->s; ptr++) {
46 			if (ptr->id == index)
47 				return copy_to_unicode(buff, length, ptr->s);
48 		}
49 	}
50 
51 	return -EINVAL;
52 }
53 
usb_emul_find_descriptor(struct usb_generic_descriptor ** ptr,int type,int index)54 struct usb_generic_descriptor **usb_emul_find_descriptor(
55 		struct usb_generic_descriptor **ptr, int type, int index)
56 {
57 	debug("%s: type=%x, index=%d\n", __func__, type, index);
58 	for (; *ptr; ptr++) {
59 		if ((*ptr)->bDescriptorType != type)
60 			continue;
61 		switch (type) {
62 		case USB_DT_CONFIG: {
63 			struct usb_config_descriptor *cdesc;
64 
65 			cdesc = (struct usb_config_descriptor *)*ptr;
66 			if (cdesc && cdesc->bConfigurationValue == index)
67 				return ptr;
68 			break;
69 		}
70 		default:
71 			return ptr;
72 		}
73 	}
74 	debug("%s: config ptr=%p\n", __func__, *ptr);
75 
76 	return ptr;
77 }
78 
usb_emul_get_descriptor(struct usb_dev_plat * plat,int value,void * buffer,int length)79 static int usb_emul_get_descriptor(struct usb_dev_plat *plat, int value,
80 				   void *buffer, int length)
81 {
82 	struct usb_generic_descriptor **ptr;
83 	int type = value >> 8;
84 	int index = value & 0xff;
85 	int upto, todo;
86 
87 	debug("%s: type=%d, index=%d, plat=%p\n", __func__, type, index, plat);
88 	if (type == USB_DT_STRING) {
89 		return usb_emul_get_string(plat->strings, index, buffer,
90 					   length);
91 	}
92 
93 	ptr = usb_emul_find_descriptor(plat->desc_list, type, index);
94 	if (!ptr) {
95 		debug("%s: Could not find descriptor type %d, index %d\n",
96 		      __func__, type, index);
97 		return -ENOENT;
98 	}
99 	for (upto = 0; *ptr && upto < length; ptr++, upto += todo) {
100 		todo = min(length - upto, (int)(*ptr)->bLength);
101 
102 		memcpy(buffer + upto, *ptr, todo);
103 	}
104 
105 	return upto ? upto : length ? -EIO : 0;
106 }
107 
usb_emul_find_devnum(int devnum,int port1,struct udevice ** emulp)108 static int usb_emul_find_devnum(int devnum, int port1, struct udevice **emulp)
109 {
110 	struct udevice *dev;
111 	struct uclass *uc;
112 	int ret;
113 
114 	*emulp = NULL;
115 	ret = uclass_get(UCLASS_USB_EMUL, &uc);
116 	if (ret)
117 		return ret;
118 	uclass_foreach_dev(dev, uc) {
119 		struct usb_dev_plat *udev = dev_get_parent_plat(dev);
120 
121 		/*
122 		 * devnum is initialzied to zero at the beginning of the
123 		 * enumeration process in usb_setup_device(). At this
124 		 * point, udev->devnum has not been assigned to any valid
125 		 * USB address either, so we can't rely on the comparison
126 		 * result between udev->devnum and devnum to select an
127 		 * emulator device.
128 		 */
129 		if (!devnum) {
130 			struct usb_emul_plat *plat;
131 
132 			/*
133 			 * If the parent is sandbox USB controller, we are
134 			 * the root hub. And there is only one root hub
135 			 * in the system.
136 			 */
137 			if (device_get_uclass_id(dev->parent) == UCLASS_USB) {
138 				debug("%s: Found emulator '%s'\n",
139 				      __func__, dev->name);
140 				*emulp = dev;
141 				return 0;
142 			}
143 
144 			plat = dev_get_uclass_plat(dev);
145 			if (plat->port1 == port1) {
146 				debug("%s: Found emulator '%s', port %d\n",
147 				      __func__, dev->name, port1);
148 				*emulp = dev;
149 				return 0;
150 			}
151 		} else if (udev->devnum == devnum) {
152 			debug("%s: Found emulator '%s', addr %d\n", __func__,
153 			      dev->name, udev->devnum);
154 			*emulp = dev;
155 			return 0;
156 		}
157 	}
158 
159 	debug("%s: No emulator found, addr %d\n", __func__, devnum);
160 	return -ENOENT;
161 }
162 
usb_emul_find(struct udevice * bus,ulong pipe,int port1,struct udevice ** emulp)163 int usb_emul_find(struct udevice *bus, ulong pipe, int port1,
164 		  struct udevice **emulp)
165 {
166 	int devnum = usb_pipedevice(pipe);
167 
168 	return usb_emul_find_devnum(devnum, port1, emulp);
169 }
170 
usb_emul_find_for_dev(struct udevice * dev,struct udevice ** emulp)171 int usb_emul_find_for_dev(struct udevice *dev, struct udevice **emulp)
172 {
173 	struct usb_dev_plat *udev = dev_get_parent_plat(dev);
174 
175 	return usb_emul_find_devnum(udev->devnum, 0, emulp);
176 }
177 
usb_emul_control(struct udevice * emul,struct usb_device * udev,unsigned long pipe,void * buffer,int length,struct devrequest * setup)178 int usb_emul_control(struct udevice *emul, struct usb_device *udev,
179 		     unsigned long pipe, void *buffer, int length,
180 		     struct devrequest *setup)
181 {
182 	struct dm_usb_ops *ops = usb_get_emul_ops(emul);
183 	struct usb_dev_plat *plat;
184 	int ret;
185 
186 	/* We permit getting the descriptor before we are probed */
187 	plat = dev_get_parent_plat(emul);
188 	if (!ops->control)
189 		return -ENOSYS;
190 	debug("%s: dev=%s\n", __func__, emul->name);
191 	if (pipe == usb_rcvctrlpipe(udev, 0)) {
192 		switch (setup->request) {
193 		case USB_REQ_GET_DESCRIPTOR: {
194 			return usb_emul_get_descriptor(plat, setup->value,
195 						       buffer, length);
196 		}
197 		default:
198 			ret = device_probe(emul);
199 			if (ret)
200 				return ret;
201 			return ops->control(emul, udev, pipe, buffer, length,
202 					    setup);
203 		}
204 	} else if (pipe == usb_snddefctrl(udev)) {
205 		switch (setup->request) {
206 		case USB_REQ_SET_ADDRESS:
207 			debug("   ** set address %s %d\n", emul->name,
208 			      setup->value);
209 			plat->devnum = setup->value;
210 			return 0;
211 		default:
212 			debug("requestsend =%x\n", setup->request);
213 			break;
214 		}
215 	} else if (pipe == usb_sndctrlpipe(udev, 0)) {
216 		switch (setup->request) {
217 		case USB_REQ_SET_CONFIGURATION:
218 			plat->configno = setup->value;
219 			return 0;
220 		default:
221 			ret = device_probe(emul);
222 			if (ret)
223 				return ret;
224 			return ops->control(emul, udev, pipe, buffer, length,
225 					    setup);
226 		}
227 	}
228 	debug("pipe=%lx\n", pipe);
229 
230 	return -EIO;
231 }
232 
usb_emul_bulk(struct udevice * emul,struct usb_device * udev,unsigned long pipe,void * buffer,int length)233 int usb_emul_bulk(struct udevice *emul, struct usb_device *udev,
234 		  unsigned long pipe, void *buffer, int length)
235 {
236 	struct dm_usb_ops *ops = usb_get_emul_ops(emul);
237 	int ret;
238 
239 	/* We permit getting the descriptor before we are probed */
240 	if (!ops->bulk)
241 		return -ENOSYS;
242 	debug("%s: dev=%s\n", __func__, emul->name);
243 	ret = device_probe(emul);
244 	if (ret)
245 		return ret;
246 	return ops->bulk(emul, udev, pipe, buffer, length);
247 }
248 
usb_emul_int(struct udevice * emul,struct usb_device * udev,unsigned long pipe,void * buffer,int length,int interval,bool nonblock)249 int usb_emul_int(struct udevice *emul, struct usb_device *udev,
250 		  unsigned long pipe, void *buffer, int length, int interval,
251 		  bool nonblock)
252 {
253 	struct dm_usb_ops *ops = usb_get_emul_ops(emul);
254 
255 	if (!ops->interrupt)
256 		return -ENOSYS;
257 	debug("%s: dev=%s\n", __func__, emul->name);
258 
259 	return ops->interrupt(emul, udev, pipe, buffer, length, interval,
260 			      nonblock);
261 }
262 
usb_emul_setup_device(struct udevice * dev,struct usb_string * strings,void ** desc_list)263 int usb_emul_setup_device(struct udevice *dev, struct usb_string *strings,
264 			  void **desc_list)
265 {
266 	struct usb_dev_plat *plat = dev_get_parent_plat(dev);
267 	struct usb_generic_descriptor **ptr;
268 	struct usb_config_descriptor *cdesc;
269 	int upto;
270 
271 	plat->strings = strings;
272 	plat->desc_list = (struct usb_generic_descriptor **)desc_list;
273 
274 	/* Fill in wTotalLength for each configuration descriptor */
275 	ptr = plat->desc_list;
276 	for (cdesc = NULL, upto = 0; *ptr; upto += (*ptr)->bLength, ptr++) {
277 		debug("   - upto=%d, type=%d\n", upto, (*ptr)->bDescriptorType);
278 		if ((*ptr)->bDescriptorType == USB_DT_CONFIG) {
279 			if (cdesc) {
280 				cdesc->wTotalLength = upto;
281 				debug("%s: config %d length %d\n", __func__,
282 				      cdesc->bConfigurationValue,
283 				      cdesc->bLength);
284 			}
285 			cdesc = (struct usb_config_descriptor *)*ptr;
286 			upto = 0;
287 		}
288 	}
289 	if (cdesc) {
290 		cdesc->wTotalLength = upto;
291 		debug("%s: config %d length %d\n", __func__,
292 		      cdesc->bConfigurationValue, cdesc->wTotalLength);
293 	}
294 
295 	return 0;
296 }
297 
298 UCLASS_DRIVER(usb_emul) = {
299 	.id		= UCLASS_USB_EMUL,
300 	.name		= "usb_emul",
301 	.post_bind	= dm_scan_fdt_dev,
302 	.per_device_plat_auto	= sizeof(struct usb_emul_plat),
303 	.per_child_auto	= sizeof(struct usb_device),
304 	.per_child_plat_auto	= sizeof(struct usb_dev_plat),
305 };
306