2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2015-03-25 18:22:37 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2015 Google, Inc
|
|
|
|
* Written by Simon Glass <sjg@chromium.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2015-03-25 18:22:37 +00:00
|
|
|
#include <usb.h>
|
|
|
|
#include <dm/device-internal.h>
|
|
|
|
|
|
|
|
static int copy_to_unicode(char *buff, int length, const char *str)
|
|
|
|
{
|
|
|
|
int ptr;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (length < 2)
|
|
|
|
return 0;
|
|
|
|
buff[1] = USB_DT_STRING;
|
|
|
|
for (ptr = 2, i = 0; ptr + 1 < length && *str; i++, ptr += 2) {
|
|
|
|
buff[ptr] = str[i];
|
|
|
|
buff[ptr + 1] = 0;
|
|
|
|
}
|
|
|
|
buff[0] = ptr;
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int usb_emul_get_string(struct usb_string *strings, int index,
|
|
|
|
char *buff, int length)
|
|
|
|
{
|
|
|
|
if (index == 0) {
|
|
|
|
char *desc = buff;
|
|
|
|
|
|
|
|
desc[0] = 4;
|
|
|
|
desc[1] = USB_DT_STRING;
|
|
|
|
desc[2] = 0x09;
|
|
|
|
desc[3] = 0x14;
|
|
|
|
return 4;
|
|
|
|
} else if (strings) {
|
|
|
|
struct usb_string *ptr;
|
|
|
|
|
|
|
|
for (ptr = strings; ptr->s; ptr++) {
|
|
|
|
if (ptr->id == index)
|
|
|
|
return copy_to_unicode(buff, length, ptr->s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-10-01 13:19:40 +00:00
|
|
|
struct usb_generic_descriptor **usb_emul_find_descriptor(
|
2015-03-25 18:22:37 +00:00
|
|
|
struct usb_generic_descriptor **ptr, int type, int index)
|
|
|
|
{
|
|
|
|
debug("%s: type=%x, index=%d\n", __func__, type, index);
|
|
|
|
for (; *ptr; ptr++) {
|
|
|
|
if ((*ptr)->bDescriptorType != type)
|
|
|
|
continue;
|
|
|
|
switch (type) {
|
|
|
|
case USB_DT_CONFIG: {
|
|
|
|
struct usb_config_descriptor *cdesc;
|
|
|
|
|
|
|
|
cdesc = (struct usb_config_descriptor *)*ptr;
|
|
|
|
if (cdesc && cdesc->bConfigurationValue == index)
|
|
|
|
return ptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug("%s: config ptr=%p\n", __func__, *ptr);
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int usb_emul_get_descriptor(struct usb_dev_platdata *plat, int value,
|
|
|
|
void *buffer, int length)
|
|
|
|
{
|
|
|
|
struct usb_generic_descriptor **ptr;
|
|
|
|
int type = value >> 8;
|
|
|
|
int index = value & 0xff;
|
|
|
|
int upto, todo;
|
|
|
|
|
|
|
|
debug("%s: type=%d, index=%d, plat=%p\n", __func__, type, index, plat);
|
|
|
|
if (type == USB_DT_STRING) {
|
|
|
|
return usb_emul_get_string(plat->strings, index, buffer,
|
|
|
|
length);
|
|
|
|
}
|
|
|
|
|
2017-10-01 13:19:40 +00:00
|
|
|
ptr = usb_emul_find_descriptor(plat->desc_list, type, index);
|
2015-03-25 18:22:37 +00:00
|
|
|
if (!ptr) {
|
|
|
|
debug("%s: Could not find descriptor type %d, index %d\n",
|
|
|
|
__func__, type, index);
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
for (upto = 0; *ptr && upto < length; ptr++, upto += todo) {
|
|
|
|
todo = min(length - upto, (int)(*ptr)->bLength);
|
|
|
|
|
|
|
|
memcpy(buffer + upto, *ptr, todo);
|
|
|
|
}
|
|
|
|
|
|
|
|
return upto ? upto : length ? -EIO : 0;
|
|
|
|
}
|
|
|
|
|
2017-10-01 13:19:39 +00:00
|
|
|
static int usb_emul_find_devnum(int devnum, int port1, struct udevice **emulp)
|
2015-03-25 18:22:37 +00:00
|
|
|
{
|
|
|
|
struct udevice *dev;
|
|
|
|
struct uclass *uc;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
*emulp = NULL;
|
|
|
|
ret = uclass_get(UCLASS_USB_EMUL, &uc);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
uclass_foreach_dev(dev, uc) {
|
2020-12-03 23:55:18 +00:00
|
|
|
struct usb_dev_platdata *udev = dev_get_parent_plat(dev);
|
2015-03-25 18:22:37 +00:00
|
|
|
|
2017-10-01 13:19:39 +00:00
|
|
|
/*
|
|
|
|
* devnum is initialzied to zero at the beginning of the
|
|
|
|
* enumeration process in usb_setup_device(). At this
|
|
|
|
* point, udev->devnum has not been assigned to any valid
|
|
|
|
* USB address either, so we can't rely on the comparison
|
|
|
|
* result between udev->devnum and devnum to select an
|
|
|
|
* emulator device.
|
|
|
|
*/
|
|
|
|
if (!devnum) {
|
|
|
|
struct usb_emul_platdata *plat;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the parent is sandbox USB controller, we are
|
|
|
|
* the root hub. And there is only one root hub
|
|
|
|
* in the system.
|
|
|
|
*/
|
|
|
|
if (device_get_uclass_id(dev->parent) == UCLASS_USB) {
|
|
|
|
debug("%s: Found emulator '%s'\n",
|
|
|
|
__func__, dev->name);
|
|
|
|
*emulp = dev;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-03 23:55:18 +00:00
|
|
|
plat = dev_get_uclass_plat(dev);
|
2017-10-01 13:19:39 +00:00
|
|
|
if (plat->port1 == port1) {
|
|
|
|
debug("%s: Found emulator '%s', port %d\n",
|
|
|
|
__func__, dev->name, port1);
|
|
|
|
*emulp = dev;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else if (udev->devnum == devnum) {
|
2015-03-25 18:22:37 +00:00
|
|
|
debug("%s: Found emulator '%s', addr %d\n", __func__,
|
|
|
|
dev->name, udev->devnum);
|
|
|
|
*emulp = dev;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("%s: No emulator found, addr %d\n", __func__, devnum);
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2017-10-01 13:19:39 +00:00
|
|
|
int usb_emul_find(struct udevice *bus, ulong pipe, int port1,
|
|
|
|
struct udevice **emulp)
|
2015-11-09 06:47:55 +00:00
|
|
|
{
|
|
|
|
int devnum = usb_pipedevice(pipe);
|
|
|
|
|
2017-10-01 13:19:39 +00:00
|
|
|
return usb_emul_find_devnum(devnum, port1, emulp);
|
2015-11-09 06:47:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int usb_emul_find_for_dev(struct udevice *dev, struct udevice **emulp)
|
|
|
|
{
|
2020-12-03 23:55:18 +00:00
|
|
|
struct usb_dev_platdata *udev = dev_get_parent_plat(dev);
|
2015-11-09 06:47:55 +00:00
|
|
|
|
2017-10-01 13:19:39 +00:00
|
|
|
return usb_emul_find_devnum(udev->devnum, 0, emulp);
|
2015-11-09 06:47:55 +00:00
|
|
|
}
|
|
|
|
|
2015-03-25 18:22:37 +00:00
|
|
|
int usb_emul_control(struct udevice *emul, struct usb_device *udev,
|
|
|
|
unsigned long pipe, void *buffer, int length,
|
|
|
|
struct devrequest *setup)
|
|
|
|
{
|
|
|
|
struct dm_usb_ops *ops = usb_get_emul_ops(emul);
|
|
|
|
struct usb_dev_platdata *plat;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* We permit getting the descriptor before we are probed */
|
2020-12-03 23:55:18 +00:00
|
|
|
plat = dev_get_parent_plat(emul);
|
2015-03-25 18:22:37 +00:00
|
|
|
if (!ops->control)
|
|
|
|
return -ENOSYS;
|
|
|
|
debug("%s: dev=%s\n", __func__, emul->name);
|
|
|
|
if (pipe == usb_rcvctrlpipe(udev, 0)) {
|
|
|
|
switch (setup->request) {
|
|
|
|
case USB_REQ_GET_DESCRIPTOR: {
|
|
|
|
return usb_emul_get_descriptor(plat, setup->value,
|
|
|
|
buffer, length);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
ret = device_probe(emul);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
return ops->control(emul, udev, pipe, buffer, length,
|
|
|
|
setup);
|
|
|
|
}
|
|
|
|
} else if (pipe == usb_snddefctrl(udev)) {
|
|
|
|
switch (setup->request) {
|
|
|
|
case USB_REQ_SET_ADDRESS:
|
|
|
|
debug(" ** set address %s %d\n", emul->name,
|
|
|
|
setup->value);
|
|
|
|
plat->devnum = setup->value;
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
debug("requestsend =%x\n", setup->request);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (pipe == usb_sndctrlpipe(udev, 0)) {
|
|
|
|
switch (setup->request) {
|
|
|
|
case USB_REQ_SET_CONFIGURATION:
|
|
|
|
plat->configno = setup->value;
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
ret = device_probe(emul);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
return ops->control(emul, udev, pipe, buffer, length,
|
|
|
|
setup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug("pipe=%lx\n", pipe);
|
|
|
|
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_emul_bulk(struct udevice *emul, struct usb_device *udev,
|
|
|
|
unsigned long pipe, void *buffer, int length)
|
|
|
|
{
|
|
|
|
struct dm_usb_ops *ops = usb_get_emul_ops(emul);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* We permit getting the descriptor before we are probed */
|
|
|
|
if (!ops->bulk)
|
|
|
|
return -ENOSYS;
|
|
|
|
debug("%s: dev=%s\n", __func__, emul->name);
|
|
|
|
ret = device_probe(emul);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
return ops->bulk(emul, udev, pipe, buffer, length);
|
|
|
|
}
|
|
|
|
|
2015-11-09 06:48:05 +00:00
|
|
|
int usb_emul_int(struct udevice *emul, struct usb_device *udev,
|
2019-08-18 08:55:27 +00:00
|
|
|
unsigned long pipe, void *buffer, int length, int interval,
|
|
|
|
bool nonblock)
|
2015-11-09 06:48:05 +00:00
|
|
|
{
|
|
|
|
struct dm_usb_ops *ops = usb_get_emul_ops(emul);
|
|
|
|
|
|
|
|
if (!ops->interrupt)
|
|
|
|
return -ENOSYS;
|
|
|
|
debug("%s: dev=%s\n", __func__, emul->name);
|
|
|
|
|
2019-08-18 08:55:27 +00:00
|
|
|
return ops->interrupt(emul, udev, pipe, buffer, length, interval,
|
|
|
|
nonblock);
|
2015-11-09 06:48:05 +00:00
|
|
|
}
|
|
|
|
|
2017-10-01 13:19:36 +00:00
|
|
|
int usb_emul_setup_device(struct udevice *dev, struct usb_string *strings,
|
|
|
|
void **desc_list)
|
2015-03-25 18:22:37 +00:00
|
|
|
{
|
2020-12-03 23:55:18 +00:00
|
|
|
struct usb_dev_platdata *plat = dev_get_parent_plat(dev);
|
2015-03-25 18:22:37 +00:00
|
|
|
struct usb_generic_descriptor **ptr;
|
|
|
|
struct usb_config_descriptor *cdesc;
|
|
|
|
int upto;
|
|
|
|
|
|
|
|
plat->strings = strings;
|
|
|
|
plat->desc_list = (struct usb_generic_descriptor **)desc_list;
|
|
|
|
|
|
|
|
/* Fill in wTotalLength for each configuration descriptor */
|
|
|
|
ptr = plat->desc_list;
|
|
|
|
for (cdesc = NULL, upto = 0; *ptr; upto += (*ptr)->bLength, ptr++) {
|
|
|
|
debug(" - upto=%d, type=%d\n", upto, (*ptr)->bDescriptorType);
|
|
|
|
if ((*ptr)->bDescriptorType == USB_DT_CONFIG) {
|
|
|
|
if (cdesc) {
|
|
|
|
cdesc->wTotalLength = upto;
|
|
|
|
debug("%s: config %d length %d\n", __func__,
|
|
|
|
cdesc->bConfigurationValue,
|
|
|
|
cdesc->bLength);
|
|
|
|
}
|
|
|
|
cdesc = (struct usb_config_descriptor *)*ptr;
|
|
|
|
upto = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cdesc) {
|
|
|
|
cdesc->wTotalLength = upto;
|
|
|
|
debug("%s: config %d length %d\n", __func__,
|
|
|
|
cdesc->bConfigurationValue, cdesc->wTotalLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
UCLASS_DRIVER(usb_emul) = {
|
|
|
|
.id = UCLASS_USB_EMUL,
|
|
|
|
.name = "usb_emul",
|
2016-07-05 23:10:10 +00:00
|
|
|
.post_bind = dm_scan_fdt_dev,
|
2020-12-03 23:55:18 +00:00
|
|
|
.per_device_plat_auto = sizeof(struct usb_emul_platdata),
|
2020-12-03 23:55:17 +00:00
|
|
|
.per_child_auto = sizeof(struct usb_device),
|
2020-12-03 23:55:18 +00:00
|
|
|
.per_child_plat_auto = sizeof(struct usb_dev_platdata),
|
2015-03-25 18:22:37 +00:00
|
|
|
};
|