blob: 6b5f3c0af491ecdc3889ed1e765e59b88ccc5ec7 [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>
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 Glass668ef3d2015-11-08 23:47:55 -0700111static int usb_emul_find_devnum(int devnum, struct udevice **emulp)
Simon Glass59d66d22015-03-25 12:22:37 -0600112{
Simon Glass59d66d22015-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 Glass668ef3d2015-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 Glass59d66d22015-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
221int usb_emul_setup_device(struct udevice *dev, int maxpacketsize,
222 struct usb_string *strings, void **desc_list)
223{
224 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
225 struct usb_generic_descriptor **ptr;
226 struct usb_config_descriptor *cdesc;
227 int upto;
228
229 plat->strings = strings;
230 plat->desc_list = (struct usb_generic_descriptor **)desc_list;
231
232 /* Fill in wTotalLength for each configuration descriptor */
233 ptr = plat->desc_list;
234 for (cdesc = NULL, upto = 0; *ptr; upto += (*ptr)->bLength, ptr++) {
235 debug(" - upto=%d, type=%d\n", upto, (*ptr)->bDescriptorType);
236 if ((*ptr)->bDescriptorType == USB_DT_CONFIG) {
237 if (cdesc) {
238 cdesc->wTotalLength = upto;
239 debug("%s: config %d length %d\n", __func__,
240 cdesc->bConfigurationValue,
241 cdesc->bLength);
242 }
243 cdesc = (struct usb_config_descriptor *)*ptr;
244 upto = 0;
245 }
246 }
247 if (cdesc) {
248 cdesc->wTotalLength = upto;
249 debug("%s: config %d length %d\n", __func__,
250 cdesc->bConfigurationValue, cdesc->wTotalLength);
251 }
252
253 return 0;
254}
255
256int usb_emul_post_bind(struct udevice *dev)
257{
258 /* Scan the bus for devices */
259 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
260}
261
262void usb_emul_reset(struct udevice *dev)
263{
264 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
265
266 plat->devnum = 0;
267 plat->configno = 0;
268}
269
270UCLASS_DRIVER(usb_emul) = {
271 .id = UCLASS_USB_EMUL,
272 .name = "usb_emul",
273 .post_bind = usb_emul_post_bind,
274 .per_child_auto_alloc_size = sizeof(struct usb_device),
275 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
276};