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