blob: 205f2c54dfb2b312c1442323a73a1758a79a32b8 [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
111int usb_emul_find(struct udevice *bus, ulong pipe, struct udevice **emulp)
112{
113 int devnum = usb_pipedevice(pipe);
114 struct udevice *dev;
115 struct uclass *uc;
116 int ret;
117
118 *emulp = NULL;
119 ret = uclass_get(UCLASS_USB_EMUL, &uc);
120 if (ret)
121 return ret;
122 uclass_foreach_dev(dev, uc) {
123 struct usb_dev_platdata *udev = dev_get_parent_platdata(dev);
124
125 if (udev->devnum == devnum) {
126 debug("%s: Found emulator '%s', addr %d\n", __func__,
127 dev->name, udev->devnum);
128 *emulp = dev;
129 return 0;
130 }
131 }
132
133 debug("%s: No emulator found, addr %d\n", __func__, devnum);
134 return -ENOENT;
135}
136
137int usb_emul_control(struct udevice *emul, struct usb_device *udev,
138 unsigned long pipe, void *buffer, int length,
139 struct devrequest *setup)
140{
141 struct dm_usb_ops *ops = usb_get_emul_ops(emul);
142 struct usb_dev_platdata *plat;
143 int ret;
144
145 /* We permit getting the descriptor before we are probed */
146 plat = dev_get_parent_platdata(emul);
147 if (!ops->control)
148 return -ENOSYS;
149 debug("%s: dev=%s\n", __func__, emul->name);
150 if (pipe == usb_rcvctrlpipe(udev, 0)) {
151 switch (setup->request) {
152 case USB_REQ_GET_DESCRIPTOR: {
153 return usb_emul_get_descriptor(plat, setup->value,
154 buffer, length);
155 }
156 default:
157 ret = device_probe(emul);
158 if (ret)
159 return ret;
160 return ops->control(emul, udev, pipe, buffer, length,
161 setup);
162 }
163 } else if (pipe == usb_snddefctrl(udev)) {
164 switch (setup->request) {
165 case USB_REQ_SET_ADDRESS:
166 debug(" ** set address %s %d\n", emul->name,
167 setup->value);
168 plat->devnum = setup->value;
169 return 0;
170 default:
171 debug("requestsend =%x\n", setup->request);
172 break;
173 }
174 } else if (pipe == usb_sndctrlpipe(udev, 0)) {
175 switch (setup->request) {
176 case USB_REQ_SET_CONFIGURATION:
177 plat->configno = setup->value;
178 return 0;
179 default:
180 ret = device_probe(emul);
181 if (ret)
182 return ret;
183 return ops->control(emul, udev, pipe, buffer, length,
184 setup);
185 }
186 }
187 debug("pipe=%lx\n", pipe);
188
189 return -EIO;
190}
191
192int usb_emul_bulk(struct udevice *emul, struct usb_device *udev,
193 unsigned long pipe, void *buffer, int length)
194{
195 struct dm_usb_ops *ops = usb_get_emul_ops(emul);
196 int ret;
197
198 /* We permit getting the descriptor before we are probed */
199 if (!ops->bulk)
200 return -ENOSYS;
201 debug("%s: dev=%s\n", __func__, emul->name);
202 ret = device_probe(emul);
203 if (ret)
204 return ret;
205 return ops->bulk(emul, udev, pipe, buffer, length);
206}
207
208int usb_emul_setup_device(struct udevice *dev, int maxpacketsize,
209 struct usb_string *strings, void **desc_list)
210{
211 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
212 struct usb_generic_descriptor **ptr;
213 struct usb_config_descriptor *cdesc;
214 int upto;
215
216 plat->strings = strings;
217 plat->desc_list = (struct usb_generic_descriptor **)desc_list;
218
219 /* Fill in wTotalLength for each configuration descriptor */
220 ptr = plat->desc_list;
221 for (cdesc = NULL, upto = 0; *ptr; upto += (*ptr)->bLength, ptr++) {
222 debug(" - upto=%d, type=%d\n", upto, (*ptr)->bDescriptorType);
223 if ((*ptr)->bDescriptorType == USB_DT_CONFIG) {
224 if (cdesc) {
225 cdesc->wTotalLength = upto;
226 debug("%s: config %d length %d\n", __func__,
227 cdesc->bConfigurationValue,
228 cdesc->bLength);
229 }
230 cdesc = (struct usb_config_descriptor *)*ptr;
231 upto = 0;
232 }
233 }
234 if (cdesc) {
235 cdesc->wTotalLength = upto;
236 debug("%s: config %d length %d\n", __func__,
237 cdesc->bConfigurationValue, cdesc->wTotalLength);
238 }
239
240 return 0;
241}
242
243int usb_emul_post_bind(struct udevice *dev)
244{
245 /* Scan the bus for devices */
246 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
247}
248
249void usb_emul_reset(struct udevice *dev)
250{
251 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
252
253 plat->devnum = 0;
254 plat->configno = 0;
255}
256
257UCLASS_DRIVER(usb_emul) = {
258 .id = UCLASS_USB_EMUL,
259 .name = "usb_emul",
260 .post_bind = usb_emul_post_bind,
261 .per_child_auto_alloc_size = sizeof(struct usb_device),
262 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
263};