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