blob: 6e03c1e0d9e7f1273990fd2967e77a17c2efa862 [file] [log] [blame]
Simon Glass59d66d22015-03-25 12:22:37 -06001/*
2 * (C) Copyright 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <dm.h>
10#include <usb.h>
Simon Glass59d66d22015-03-25 12:22:37 -060011#include <dm/device-internal.h>
12
13DECLARE_GLOBAL_DATA_PTR;
14
15static int copy_to_unicode(char *buff, int length, const char *str)
16{
17 int ptr;
18 int i;
19
20 if (length < 2)
21 return 0;
22 buff[1] = USB_DT_STRING;
23 for (ptr = 2, i = 0; ptr + 1 < length && *str; i++, ptr += 2) {
24 buff[ptr] = str[i];
25 buff[ptr + 1] = 0;
26 }
27 buff[0] = ptr;
28
29 return ptr;
30}
31
32static int usb_emul_get_string(struct usb_string *strings, int index,
33 char *buff, int length)
34{
35 if (index == 0) {
36 char *desc = buff;
37
38 desc[0] = 4;
39 desc[1] = USB_DT_STRING;
40 desc[2] = 0x09;
41 desc[3] = 0x14;
42 return 4;
43 } else if (strings) {
44 struct usb_string *ptr;
45
46 for (ptr = strings; ptr->s; ptr++) {
47 if (ptr->id == index)
48 return copy_to_unicode(buff, length, ptr->s);
49 }
50 }
51
52 return -EINVAL;
53}
54
55static struct usb_generic_descriptor **find_descriptor(
56 struct usb_generic_descriptor **ptr, int type, int index)
57{
58 debug("%s: type=%x, index=%d\n", __func__, type, index);
59 for (; *ptr; ptr++) {
60 if ((*ptr)->bDescriptorType != type)
61 continue;
62 switch (type) {
63 case USB_DT_CONFIG: {
64 struct usb_config_descriptor *cdesc;
65
66 cdesc = (struct usb_config_descriptor *)*ptr;
67 if (cdesc && cdesc->bConfigurationValue == index)
68 return ptr;
69 break;
70 }
71 default:
72 return ptr;
73 }
74 }
75 debug("%s: config ptr=%p\n", __func__, *ptr);
76
77 return ptr;
78}
79
80static int usb_emul_get_descriptor(struct usb_dev_platdata *plat, int value,
81 void *buffer, int length)
82{
83 struct usb_generic_descriptor **ptr;
84 int type = value >> 8;
85 int index = value & 0xff;
86 int upto, todo;
87
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,
91 length);
92 }
93
94 ptr = find_descriptor((struct usb_generic_descriptor **)plat->desc_list,
95 type, index);
96 if (!ptr) {
97 debug("%s: Could not find descriptor type %d, index %d\n",
98 __func__, type, index);
99 return -ENOENT;
100 }
101 for (upto = 0; *ptr && upto < length; ptr++, upto += todo) {
102 todo = min(length - upto, (int)(*ptr)->bLength);
103
104 memcpy(buffer + upto, *ptr, todo);
105 }
106
107 return upto ? upto : length ? -EIO : 0;
108}
109
Simon Glass668ef3d2015-11-08 23:47:55 -0700110static int usb_emul_find_devnum(int devnum, struct udevice **emulp)
Simon Glass59d66d22015-03-25 12:22:37 -0600111{
Simon Glass59d66d22015-03-25 12:22:37 -0600112 struct udevice *dev;
113 struct uclass *uc;
114 int ret;
115
116 *emulp = NULL;
117 ret = uclass_get(UCLASS_USB_EMUL, &uc);
118 if (ret)
119 return ret;
120 uclass_foreach_dev(dev, uc) {
121 struct usb_dev_platdata *udev = dev_get_parent_platdata(dev);
122
123 if (udev->devnum == devnum) {
124 debug("%s: Found emulator '%s', addr %d\n", __func__,
125 dev->name, udev->devnum);
126 *emulp = dev;
127 return 0;
128 }
129 }
130
131 debug("%s: No emulator found, addr %d\n", __func__, devnum);
132 return -ENOENT;
133}
134
Simon Glass668ef3d2015-11-08 23:47:55 -0700135int usb_emul_find(struct udevice *bus, ulong pipe, struct udevice **emulp)
136{
137 int devnum = usb_pipedevice(pipe);
138
139 return usb_emul_find_devnum(devnum, emulp);
140}
141
142int usb_emul_find_for_dev(struct udevice *dev, struct udevice **emulp)
143{
144 struct usb_dev_platdata *udev = dev_get_parent_platdata(dev);
145
146 return usb_emul_find_devnum(udev->devnum, emulp);
147}
148
Simon Glass59d66d22015-03-25 12:22:37 -0600149int usb_emul_control(struct udevice *emul, struct usb_device *udev,
150 unsigned long pipe, void *buffer, int length,
151 struct devrequest *setup)
152{
153 struct dm_usb_ops *ops = usb_get_emul_ops(emul);
154 struct usb_dev_platdata *plat;
155 int ret;
156
157 /* We permit getting the descriptor before we are probed */
158 plat = dev_get_parent_platdata(emul);
159 if (!ops->control)
160 return -ENOSYS;
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,
166 buffer, length);
167 }
168 default:
169 ret = device_probe(emul);
170 if (ret)
171 return ret;
172 return ops->control(emul, udev, pipe, buffer, length,
173 setup);
174 }
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,
179 setup->value);
180 plat->devnum = setup->value;
181 return 0;
182 default:
183 debug("requestsend =%x\n", setup->request);
184 break;
185 }
186 } else if (pipe == usb_sndctrlpipe(udev, 0)) {
187 switch (setup->request) {
188 case USB_REQ_SET_CONFIGURATION:
189 plat->configno = setup->value;
190 return 0;
191 default:
192 ret = device_probe(emul);
193 if (ret)
194 return ret;
195 return ops->control(emul, udev, pipe, buffer, length,
196 setup);
197 }
198 }
199 debug("pipe=%lx\n", pipe);
200
201 return -EIO;
202}
203
204int usb_emul_bulk(struct udevice *emul, struct usb_device *udev,
205 unsigned long pipe, void *buffer, int length)
206{
207 struct dm_usb_ops *ops = usb_get_emul_ops(emul);
208 int ret;
209
210 /* We permit getting the descriptor before we are probed */
211 if (!ops->bulk)
212 return -ENOSYS;
213 debug("%s: dev=%s\n", __func__, emul->name);
214 ret = device_probe(emul);
215 if (ret)
216 return ret;
217 return ops->bulk(emul, udev, pipe, buffer, length);
218}
219
Simon Glass90421792015-11-08 23:48:05 -0700220int usb_emul_int(struct udevice *emul, struct usb_device *udev,
221 unsigned long pipe, void *buffer, int length, int interval)
222{
223 struct dm_usb_ops *ops = usb_get_emul_ops(emul);
224
225 if (!ops->interrupt)
226 return -ENOSYS;
227 debug("%s: dev=%s\n", __func__, emul->name);
228
229 return ops->interrupt(emul, udev, pipe, buffer, length, interval);
230}
231
Simon Glass59d66d22015-03-25 12:22:37 -0600232int usb_emul_setup_device(struct udevice *dev, int maxpacketsize,
233 struct usb_string *strings, void **desc_list)
234{
235 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
236 struct usb_generic_descriptor **ptr;
237 struct usb_config_descriptor *cdesc;
238 int upto;
239
240 plat->strings = strings;
241 plat->desc_list = (struct usb_generic_descriptor **)desc_list;
242
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) {
248 if (cdesc) {
249 cdesc->wTotalLength = upto;
250 debug("%s: config %d length %d\n", __func__,
251 cdesc->bConfigurationValue,
252 cdesc->bLength);
253 }
254 cdesc = (struct usb_config_descriptor *)*ptr;
255 upto = 0;
256 }
257 }
258 if (cdesc) {
259 cdesc->wTotalLength = upto;
260 debug("%s: config %d length %d\n", __func__,
261 cdesc->bConfigurationValue, cdesc->wTotalLength);
262 }
263
264 return 0;
265}
266
Simon Glass59d66d22015-03-25 12:22:37 -0600267void usb_emul_reset(struct udevice *dev)
268{
269 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
270
271 plat->devnum = 0;
272 plat->configno = 0;
273}
274
275UCLASS_DRIVER(usb_emul) = {
276 .id = UCLASS_USB_EMUL,
277 .name = "usb_emul",
Simon Glass18230342016-07-05 17:10:10 -0600278 .post_bind = dm_scan_fdt_dev,
Simon Glass59d66d22015-03-25 12:22:37 -0600279 .per_child_auto_alloc_size = sizeof(struct usb_device),
280 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
281};