2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2015-03-25 18:22:41 +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:41 +00:00
|
|
|
#include <usb.h>
|
|
|
|
#include <dm/root.h>
|
2021-09-10 14:16:22 +00:00
|
|
|
#include <linux/usb/gadget.h>
|
|
|
|
|
|
|
|
struct sandbox_udc {
|
|
|
|
struct usb_gadget gadget;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct sandbox_udc *this_controller;
|
2015-03-25 18:22:41 +00:00
|
|
|
|
2017-10-01 13:19:38 +00:00
|
|
|
struct sandbox_usb_ctrl {
|
|
|
|
int rootdev;
|
|
|
|
};
|
|
|
|
|
2015-03-25 18:22:41 +00:00
|
|
|
static void usbmon_trace(struct udevice *bus, ulong pipe,
|
|
|
|
struct devrequest *setup, struct udevice *emul)
|
|
|
|
{
|
|
|
|
static const char types[] = "ZICB";
|
|
|
|
int type;
|
|
|
|
|
|
|
|
type = (pipe & USB_PIPE_TYPE_MASK) >> USB_PIPE_TYPE_SHIFT;
|
|
|
|
debug("0 0 S %c%c:%d:%03ld:%ld", types[type],
|
|
|
|
pipe & USB_DIR_IN ? 'i' : 'o',
|
2020-12-17 04:20:07 +00:00
|
|
|
dev_seq(bus),
|
2015-03-25 18:22:41 +00:00
|
|
|
(pipe & USB_PIPE_DEV_MASK) >> USB_PIPE_DEV_SHIFT,
|
|
|
|
(pipe & USB_PIPE_EP_MASK) >> USB_PIPE_EP_SHIFT);
|
|
|
|
if (setup) {
|
|
|
|
debug(" s %02x %02x %04x %04x %04x", setup->requesttype,
|
|
|
|
setup->request, setup->value, setup->index,
|
|
|
|
setup->length);
|
|
|
|
}
|
|
|
|
debug(" %s", emul ? emul->name : "(no emul found)");
|
|
|
|
|
|
|
|
debug("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sandbox_submit_control(struct udevice *bus,
|
|
|
|
struct usb_device *udev,
|
|
|
|
unsigned long pipe,
|
|
|
|
void *buffer, int length,
|
|
|
|
struct devrequest *setup)
|
|
|
|
{
|
2017-10-01 13:19:38 +00:00
|
|
|
struct sandbox_usb_ctrl *ctrl = dev_get_priv(bus);
|
2015-03-25 18:22:41 +00:00
|
|
|
struct udevice *emul;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Just use child of dev as emulator? */
|
|
|
|
debug("%s: bus=%s\n", __func__, bus->name);
|
2017-10-01 13:19:39 +00:00
|
|
|
ret = usb_emul_find(bus, pipe, udev->portnr, &emul);
|
2015-03-25 18:22:41 +00:00
|
|
|
usbmon_trace(bus, pipe, setup, emul);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-10-01 13:19:38 +00:00
|
|
|
|
|
|
|
if (usb_pipedevice(pipe) == ctrl->rootdev) {
|
|
|
|
if (setup->request == USB_REQ_SET_ADDRESS) {
|
|
|
|
debug("%s: Set root hub's USB address\n", __func__);
|
|
|
|
ctrl->rootdev = le16_to_cpu(setup->value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 18:22:41 +00:00
|
|
|
ret = usb_emul_control(emul, udev, pipe, buffer, length, setup);
|
|
|
|
if (ret < 0) {
|
|
|
|
debug("ret=%d\n", ret);
|
|
|
|
udev->status = ret;
|
|
|
|
udev->act_len = 0;
|
|
|
|
} else {
|
|
|
|
udev->status = 0;
|
|
|
|
udev->act_len = ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sandbox_submit_bulk(struct udevice *bus, struct usb_device *udev,
|
|
|
|
unsigned long pipe, void *buffer, int length)
|
|
|
|
{
|
|
|
|
struct udevice *emul;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Just use child of dev as emulator? */
|
|
|
|
debug("%s: bus=%s\n", __func__, bus->name);
|
2017-10-01 13:19:39 +00:00
|
|
|
ret = usb_emul_find(bus, pipe, udev->portnr, &emul);
|
2015-03-25 18:22:41 +00:00
|
|
|
usbmon_trace(bus, pipe, NULL, emul);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
ret = usb_emul_bulk(emul, udev, pipe, buffer, length);
|
|
|
|
if (ret < 0) {
|
|
|
|
debug("ret=%d\n", ret);
|
|
|
|
udev->status = ret;
|
|
|
|
udev->act_len = 0;
|
|
|
|
} else {
|
|
|
|
udev->status = 0;
|
|
|
|
udev->act_len = ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-11-09 06:48:05 +00:00
|
|
|
static int sandbox_submit_int(struct udevice *bus, struct usb_device *udev,
|
|
|
|
unsigned long pipe, void *buffer, int length,
|
2019-08-18 08:55:27 +00:00
|
|
|
int interval, bool nonblock)
|
2015-11-09 06:48:05 +00:00
|
|
|
{
|
|
|
|
struct udevice *emul;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Just use child of dev as emulator? */
|
|
|
|
debug("%s: bus=%s\n", __func__, bus->name);
|
2017-10-01 13:19:39 +00:00
|
|
|
ret = usb_emul_find(bus, pipe, udev->portnr, &emul);
|
2015-11-09 06:48:05 +00:00
|
|
|
usbmon_trace(bus, pipe, NULL, emul);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2019-08-18 08:55:27 +00:00
|
|
|
ret = usb_emul_int(emul, udev, pipe, buffer, length, interval,
|
|
|
|
nonblock);
|
2015-11-09 06:48:05 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-09-10 14:16:22 +00:00
|
|
|
int usb_gadget_handle_interrupts(int index)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_gadget_register_driver(struct usb_gadget_driver *driver)
|
|
|
|
{
|
|
|
|
struct sandbox_udc *dev = this_controller;
|
|
|
|
|
|
|
|
return driver->bind(&dev->gadget);
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
|
|
|
|
{
|
|
|
|
struct sandbox_udc *dev = this_controller;
|
|
|
|
|
|
|
|
driver->unbind(&dev->gadget);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-25 18:22:41 +00:00
|
|
|
static int sandbox_alloc_device(struct udevice *dev, struct usb_device *udev)
|
|
|
|
{
|
2017-10-01 13:19:38 +00:00
|
|
|
struct sandbox_usb_ctrl *ctrl = dev_get_priv(dev);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Root hub will be the first device to be initailized.
|
|
|
|
* If this device is a root hub, initialize its device speed
|
|
|
|
* to high speed as we are a USB 2.0 controller.
|
|
|
|
*/
|
|
|
|
if (ctrl->rootdev == 0)
|
|
|
|
udev->speed = USB_SPEED_HIGH;
|
|
|
|
|
2015-03-25 18:22:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sandbox_usb_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct dm_usb_ops sandbox_usb_ops = {
|
|
|
|
.control = sandbox_submit_control,
|
|
|
|
.bulk = sandbox_submit_bulk,
|
2015-11-09 06:48:05 +00:00
|
|
|
.interrupt = sandbox_submit_int,
|
2015-03-25 18:22:41 +00:00
|
|
|
.alloc_device = sandbox_alloc_device,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id sandbox_usb_ids[] = {
|
|
|
|
{ .compatible = "sandbox,usb" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(usb_sandbox) = {
|
|
|
|
.name = "usb_sandbox",
|
|
|
|
.id = UCLASS_USB,
|
|
|
|
.of_match = sandbox_usb_ids,
|
|
|
|
.probe = sandbox_usb_probe,
|
|
|
|
.ops = &sandbox_usb_ops,
|
2020-12-03 23:55:17 +00:00
|
|
|
.priv_auto = sizeof(struct sandbox_usb_ctrl),
|
2015-03-25 18:22:41 +00:00
|
|
|
};
|