2 * (C) Copyright 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
13 DECLARE_GLOBAL_DATA_PTR;
15 struct sandbox_usb_ctrl {
19 static void usbmon_trace(struct udevice *bus, ulong pipe,
20 struct devrequest *setup, struct udevice *emul)
22 static const char types[] = "ZICB";
25 type = (pipe & USB_PIPE_TYPE_MASK) >> USB_PIPE_TYPE_SHIFT;
26 debug("0 0 S %c%c:%d:%03ld:%ld", types[type],
27 pipe & USB_DIR_IN ? 'i' : 'o',
29 (pipe & USB_PIPE_DEV_MASK) >> USB_PIPE_DEV_SHIFT,
30 (pipe & USB_PIPE_EP_MASK) >> USB_PIPE_EP_SHIFT);
32 debug(" s %02x %02x %04x %04x %04x", setup->requesttype,
33 setup->request, setup->value, setup->index,
36 debug(" %s", emul ? emul->name : "(no emul found)");
41 static int sandbox_submit_control(struct udevice *bus,
42 struct usb_device *udev,
44 void *buffer, int length,
45 struct devrequest *setup)
47 struct sandbox_usb_ctrl *ctrl = dev_get_priv(bus);
51 /* Just use child of dev as emulator? */
52 debug("%s: bus=%s\n", __func__, bus->name);
53 ret = usb_emul_find(bus, pipe, udev->portnr, &emul);
54 usbmon_trace(bus, pipe, setup, emul);
58 if (usb_pipedevice(pipe) == ctrl->rootdev) {
59 if (setup->request == USB_REQ_SET_ADDRESS) {
60 debug("%s: Set root hub's USB address\n", __func__);
61 ctrl->rootdev = le16_to_cpu(setup->value);
65 ret = usb_emul_control(emul, udev, pipe, buffer, length, setup);
67 debug("ret=%d\n", ret);
78 static int sandbox_submit_bulk(struct udevice *bus, struct usb_device *udev,
79 unsigned long pipe, void *buffer, int length)
84 /* Just use child of dev as emulator? */
85 debug("%s: bus=%s\n", __func__, bus->name);
86 ret = usb_emul_find(bus, pipe, udev->portnr, &emul);
87 usbmon_trace(bus, pipe, NULL, emul);
90 ret = usb_emul_bulk(emul, udev, pipe, buffer, length);
92 debug("ret=%d\n", ret);
103 static int sandbox_submit_int(struct udevice *bus, struct usb_device *udev,
104 unsigned long pipe, void *buffer, int length,
107 struct udevice *emul;
110 /* Just use child of dev as emulator? */
111 debug("%s: bus=%s\n", __func__, bus->name);
112 ret = usb_emul_find(bus, pipe, udev->portnr, &emul);
113 usbmon_trace(bus, pipe, NULL, emul);
116 ret = usb_emul_int(emul, udev, pipe, buffer, length, interval);
121 static int sandbox_alloc_device(struct udevice *dev, struct usb_device *udev)
123 struct sandbox_usb_ctrl *ctrl = dev_get_priv(dev);
126 * Root hub will be the first device to be initailized.
127 * If this device is a root hub, initialize its device speed
128 * to high speed as we are a USB 2.0 controller.
130 if (ctrl->rootdev == 0)
131 udev->speed = USB_SPEED_HIGH;
136 static int sandbox_usb_probe(struct udevice *dev)
141 static const struct dm_usb_ops sandbox_usb_ops = {
142 .control = sandbox_submit_control,
143 .bulk = sandbox_submit_bulk,
144 .interrupt = sandbox_submit_int,
145 .alloc_device = sandbox_alloc_device,
148 static const struct udevice_id sandbox_usb_ids[] = {
149 { .compatible = "sandbox,usb" },
153 U_BOOT_DRIVER(usb_sandbox) = {
154 .name = "usb_sandbox",
156 .of_match = sandbox_usb_ids,
157 .probe = sandbox_usb_probe,
158 .ops = &sandbox_usb_ops,
159 .priv_auto_alloc_size = sizeof(struct sandbox_usb_ctrl),