blob: 15055b351a5a5a5b25724f3173422b2085991458 [file] [log] [blame]
Simon Glassbc4546a2015-03-25 12:22:41 -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
13DECLARE_GLOBAL_DATA_PTR;
14
Bin Meng33662d62017-10-01 06:19:38 -070015struct sandbox_usb_ctrl {
16 int rootdev;
17};
18
Simon Glassbc4546a2015-03-25 12:22:41 -060019static void usbmon_trace(struct udevice *bus, ulong pipe,
20 struct devrequest *setup, struct udevice *emul)
21{
22 static const char types[] = "ZICB";
23 int type;
24
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',
28 bus->seq,
29 (pipe & USB_PIPE_DEV_MASK) >> USB_PIPE_DEV_SHIFT,
30 (pipe & USB_PIPE_EP_MASK) >> USB_PIPE_EP_SHIFT);
31 if (setup) {
32 debug(" s %02x %02x %04x %04x %04x", setup->requesttype,
33 setup->request, setup->value, setup->index,
34 setup->length);
35 }
36 debug(" %s", emul ? emul->name : "(no emul found)");
37
38 debug("\n");
39}
40
41static int sandbox_submit_control(struct udevice *bus,
42 struct usb_device *udev,
43 unsigned long pipe,
44 void *buffer, int length,
45 struct devrequest *setup)
46{
Bin Meng33662d62017-10-01 06:19:38 -070047 struct sandbox_usb_ctrl *ctrl = dev_get_priv(bus);
Simon Glassbc4546a2015-03-25 12:22:41 -060048 struct udevice *emul;
49 int ret;
50
51 /* Just use child of dev as emulator? */
52 debug("%s: bus=%s\n", __func__, bus->name);
Bin Mengf1966442017-10-01 06:19:39 -070053 ret = usb_emul_find(bus, pipe, udev->portnr, &emul);
Simon Glassbc4546a2015-03-25 12:22:41 -060054 usbmon_trace(bus, pipe, setup, emul);
55 if (ret)
56 return ret;
Bin Meng33662d62017-10-01 06:19:38 -070057
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);
62 }
63 }
64
Simon Glassbc4546a2015-03-25 12:22:41 -060065 ret = usb_emul_control(emul, udev, pipe, buffer, length, setup);
66 if (ret < 0) {
67 debug("ret=%d\n", ret);
68 udev->status = ret;
69 udev->act_len = 0;
70 } else {
71 udev->status = 0;
72 udev->act_len = ret;
73 }
74
75 return ret;
76}
77
78static int sandbox_submit_bulk(struct udevice *bus, struct usb_device *udev,
79 unsigned long pipe, void *buffer, int length)
80{
81 struct udevice *emul;
82 int ret;
83
84 /* Just use child of dev as emulator? */
85 debug("%s: bus=%s\n", __func__, bus->name);
Bin Mengf1966442017-10-01 06:19:39 -070086 ret = usb_emul_find(bus, pipe, udev->portnr, &emul);
Simon Glassbc4546a2015-03-25 12:22:41 -060087 usbmon_trace(bus, pipe, NULL, emul);
88 if (ret)
89 return ret;
90 ret = usb_emul_bulk(emul, udev, pipe, buffer, length);
91 if (ret < 0) {
92 debug("ret=%d\n", ret);
93 udev->status = ret;
94 udev->act_len = 0;
95 } else {
96 udev->status = 0;
97 udev->act_len = ret;
98 }
99
100 return ret;
101}
102
Simon Glass90421792015-11-08 23:48:05 -0700103static int sandbox_submit_int(struct udevice *bus, struct usb_device *udev,
104 unsigned long pipe, void *buffer, int length,
105 int interval)
106{
107 struct udevice *emul;
108 int ret;
109
110 /* Just use child of dev as emulator? */
111 debug("%s: bus=%s\n", __func__, bus->name);
Bin Mengf1966442017-10-01 06:19:39 -0700112 ret = usb_emul_find(bus, pipe, udev->portnr, &emul);
Simon Glass90421792015-11-08 23:48:05 -0700113 usbmon_trace(bus, pipe, NULL, emul);
114 if (ret)
115 return ret;
116 ret = usb_emul_int(emul, udev, pipe, buffer, length, interval);
117
118 return ret;
119}
120
Simon Glassbc4546a2015-03-25 12:22:41 -0600121static int sandbox_alloc_device(struct udevice *dev, struct usb_device *udev)
122{
Bin Meng33662d62017-10-01 06:19:38 -0700123 struct sandbox_usb_ctrl *ctrl = dev_get_priv(dev);
124
125 /*
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.
129 */
130 if (ctrl->rootdev == 0)
131 udev->speed = USB_SPEED_HIGH;
132
Simon Glassbc4546a2015-03-25 12:22:41 -0600133 return 0;
134}
135
136static int sandbox_usb_probe(struct udevice *dev)
137{
138 return 0;
139}
140
141static const struct dm_usb_ops sandbox_usb_ops = {
142 .control = sandbox_submit_control,
143 .bulk = sandbox_submit_bulk,
Simon Glass90421792015-11-08 23:48:05 -0700144 .interrupt = sandbox_submit_int,
Simon Glassbc4546a2015-03-25 12:22:41 -0600145 .alloc_device = sandbox_alloc_device,
146};
147
148static const struct udevice_id sandbox_usb_ids[] = {
149 { .compatible = "sandbox,usb" },
150 { }
151};
152
153U_BOOT_DRIVER(usb_sandbox) = {
154 .name = "usb_sandbox",
155 .id = UCLASS_USB,
156 .of_match = sandbox_usb_ids,
157 .probe = sandbox_usb_probe,
158 .ops = &sandbox_usb_ops,
Bin Meng33662d62017-10-01 06:19:38 -0700159 .priv_auto_alloc_size = sizeof(struct sandbox_usb_ctrl),
Simon Glassbc4546a2015-03-25 12:22:41 -0600160};