Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | bc4546a | 2015-03-25 12:22:41 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | bc4546a | 2015-03-25 12:22:41 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
Simon Glass | bc4546a | 2015-03-25 12:22:41 -0600 | [diff] [blame] | 7 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Simon Glass | bc4546a | 2015-03-25 12:22:41 -0600 | [diff] [blame] | 9 | #include <usb.h> |
| 10 | #include <dm/root.h> |
Patrice Chotard | d54d294 | 2021-09-10 16:16:22 +0200 | [diff] [blame] | 11 | #include <linux/usb/gadget.h> |
| 12 | |
| 13 | struct sandbox_udc { |
| 14 | struct usb_gadget gadget; |
| 15 | }; |
| 16 | |
| 17 | struct sandbox_udc *this_controller; |
Simon Glass | bc4546a | 2015-03-25 12:22:41 -0600 | [diff] [blame] | 18 | |
Bin Meng | 33662d6 | 2017-10-01 06:19:38 -0700 | [diff] [blame] | 19 | struct sandbox_usb_ctrl { |
| 20 | int rootdev; |
| 21 | }; |
| 22 | |
Simon Glass | bc4546a | 2015-03-25 12:22:41 -0600 | [diff] [blame] | 23 | static void usbmon_trace(struct udevice *bus, ulong pipe, |
| 24 | struct devrequest *setup, struct udevice *emul) |
| 25 | { |
| 26 | static const char types[] = "ZICB"; |
| 27 | int type; |
| 28 | |
| 29 | type = (pipe & USB_PIPE_TYPE_MASK) >> USB_PIPE_TYPE_SHIFT; |
| 30 | debug("0 0 S %c%c:%d:%03ld:%ld", types[type], |
| 31 | pipe & USB_DIR_IN ? 'i' : 'o', |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 32 | dev_seq(bus), |
Simon Glass | bc4546a | 2015-03-25 12:22:41 -0600 | [diff] [blame] | 33 | (pipe & USB_PIPE_DEV_MASK) >> USB_PIPE_DEV_SHIFT, |
| 34 | (pipe & USB_PIPE_EP_MASK) >> USB_PIPE_EP_SHIFT); |
| 35 | if (setup) { |
| 36 | debug(" s %02x %02x %04x %04x %04x", setup->requesttype, |
| 37 | setup->request, setup->value, setup->index, |
| 38 | setup->length); |
| 39 | } |
| 40 | debug(" %s", emul ? emul->name : "(no emul found)"); |
| 41 | |
| 42 | debug("\n"); |
| 43 | } |
| 44 | |
| 45 | static int sandbox_submit_control(struct udevice *bus, |
| 46 | struct usb_device *udev, |
| 47 | unsigned long pipe, |
| 48 | void *buffer, int length, |
| 49 | struct devrequest *setup) |
| 50 | { |
Bin Meng | 33662d6 | 2017-10-01 06:19:38 -0700 | [diff] [blame] | 51 | struct sandbox_usb_ctrl *ctrl = dev_get_priv(bus); |
Simon Glass | bc4546a | 2015-03-25 12:22:41 -0600 | [diff] [blame] | 52 | struct udevice *emul; |
| 53 | int ret; |
| 54 | |
| 55 | /* Just use child of dev as emulator? */ |
| 56 | debug("%s: bus=%s\n", __func__, bus->name); |
Bin Meng | f196644 | 2017-10-01 06:19:39 -0700 | [diff] [blame] | 57 | ret = usb_emul_find(bus, pipe, udev->portnr, &emul); |
Simon Glass | bc4546a | 2015-03-25 12:22:41 -0600 | [diff] [blame] | 58 | usbmon_trace(bus, pipe, setup, emul); |
| 59 | if (ret) |
| 60 | return ret; |
Bin Meng | 33662d6 | 2017-10-01 06:19:38 -0700 | [diff] [blame] | 61 | |
| 62 | if (usb_pipedevice(pipe) == ctrl->rootdev) { |
| 63 | if (setup->request == USB_REQ_SET_ADDRESS) { |
| 64 | debug("%s: Set root hub's USB address\n", __func__); |
| 65 | ctrl->rootdev = le16_to_cpu(setup->value); |
| 66 | } |
| 67 | } |
| 68 | |
Simon Glass | bc4546a | 2015-03-25 12:22:41 -0600 | [diff] [blame] | 69 | ret = usb_emul_control(emul, udev, pipe, buffer, length, setup); |
| 70 | if (ret < 0) { |
| 71 | debug("ret=%d\n", ret); |
| 72 | udev->status = ret; |
| 73 | udev->act_len = 0; |
| 74 | } else { |
| 75 | udev->status = 0; |
| 76 | udev->act_len = ret; |
| 77 | } |
| 78 | |
| 79 | return ret; |
| 80 | } |
| 81 | |
| 82 | static int sandbox_submit_bulk(struct udevice *bus, struct usb_device *udev, |
| 83 | unsigned long pipe, void *buffer, int length) |
| 84 | { |
| 85 | struct udevice *emul; |
| 86 | int ret; |
| 87 | |
| 88 | /* Just use child of dev as emulator? */ |
| 89 | debug("%s: bus=%s\n", __func__, bus->name); |
Bin Meng | f196644 | 2017-10-01 06:19:39 -0700 | [diff] [blame] | 90 | ret = usb_emul_find(bus, pipe, udev->portnr, &emul); |
Simon Glass | bc4546a | 2015-03-25 12:22:41 -0600 | [diff] [blame] | 91 | usbmon_trace(bus, pipe, NULL, emul); |
| 92 | if (ret) |
| 93 | return ret; |
| 94 | ret = usb_emul_bulk(emul, udev, pipe, buffer, length); |
| 95 | if (ret < 0) { |
| 96 | debug("ret=%d\n", ret); |
| 97 | udev->status = ret; |
| 98 | udev->act_len = 0; |
| 99 | } else { |
| 100 | udev->status = 0; |
| 101 | udev->act_len = ret; |
| 102 | } |
| 103 | |
| 104 | return ret; |
| 105 | } |
| 106 | |
Simon Glass | 9042179 | 2015-11-08 23:48:05 -0700 | [diff] [blame] | 107 | static int sandbox_submit_int(struct udevice *bus, struct usb_device *udev, |
| 108 | unsigned long pipe, void *buffer, int length, |
Michal Suchanek | 1c95b9f | 2019-08-18 10:55:27 +0200 | [diff] [blame] | 109 | int interval, bool nonblock) |
Simon Glass | 9042179 | 2015-11-08 23:48:05 -0700 | [diff] [blame] | 110 | { |
| 111 | struct udevice *emul; |
| 112 | int ret; |
| 113 | |
| 114 | /* Just use child of dev as emulator? */ |
| 115 | debug("%s: bus=%s\n", __func__, bus->name); |
Bin Meng | f196644 | 2017-10-01 06:19:39 -0700 | [diff] [blame] | 116 | ret = usb_emul_find(bus, pipe, udev->portnr, &emul); |
Simon Glass | 9042179 | 2015-11-08 23:48:05 -0700 | [diff] [blame] | 117 | usbmon_trace(bus, pipe, NULL, emul); |
| 118 | if (ret) |
| 119 | return ret; |
Michal Suchanek | 1c95b9f | 2019-08-18 10:55:27 +0200 | [diff] [blame] | 120 | ret = usb_emul_int(emul, udev, pipe, buffer, length, interval, |
| 121 | nonblock); |
Simon Glass | 9042179 | 2015-11-08 23:48:05 -0700 | [diff] [blame] | 122 | |
| 123 | return ret; |
| 124 | } |
| 125 | |
Marek Vasut | ed07a67 | 2023-09-01 11:49:48 +0200 | [diff] [blame] | 126 | #if CONFIG_IS_ENABLED(DM_USB_GADGET) |
| 127 | int dm_usb_gadget_handle_interrupts(struct udevice *dev) |
| 128 | { |
| 129 | return 0; |
| 130 | } |
| 131 | #else |
Patrice Chotard | d54d294 | 2021-09-10 16:16:22 +0200 | [diff] [blame] | 132 | int usb_gadget_register_driver(struct usb_gadget_driver *driver) |
| 133 | { |
| 134 | struct sandbox_udc *dev = this_controller; |
| 135 | |
| 136 | return driver->bind(&dev->gadget); |
| 137 | } |
| 138 | |
| 139 | int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) |
| 140 | { |
| 141 | struct sandbox_udc *dev = this_controller; |
| 142 | |
| 143 | driver->unbind(&dev->gadget); |
| 144 | |
| 145 | return 0; |
| 146 | } |
Marek Vasut | ed07a67 | 2023-09-01 11:49:48 +0200 | [diff] [blame] | 147 | #endif |
Patrice Chotard | d54d294 | 2021-09-10 16:16:22 +0200 | [diff] [blame] | 148 | |
Simon Glass | bc4546a | 2015-03-25 12:22:41 -0600 | [diff] [blame] | 149 | static int sandbox_alloc_device(struct udevice *dev, struct usb_device *udev) |
| 150 | { |
Bin Meng | 33662d6 | 2017-10-01 06:19:38 -0700 | [diff] [blame] | 151 | struct sandbox_usb_ctrl *ctrl = dev_get_priv(dev); |
| 152 | |
| 153 | /* |
| 154 | * Root hub will be the first device to be initailized. |
| 155 | * If this device is a root hub, initialize its device speed |
| 156 | * to high speed as we are a USB 2.0 controller. |
| 157 | */ |
| 158 | if (ctrl->rootdev == 0) |
| 159 | udev->speed = USB_SPEED_HIGH; |
| 160 | |
Simon Glass | bc4546a | 2015-03-25 12:22:41 -0600 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static int sandbox_usb_probe(struct udevice *dev) |
| 165 | { |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static const struct dm_usb_ops sandbox_usb_ops = { |
| 170 | .control = sandbox_submit_control, |
| 171 | .bulk = sandbox_submit_bulk, |
Simon Glass | 9042179 | 2015-11-08 23:48:05 -0700 | [diff] [blame] | 172 | .interrupt = sandbox_submit_int, |
Simon Glass | bc4546a | 2015-03-25 12:22:41 -0600 | [diff] [blame] | 173 | .alloc_device = sandbox_alloc_device, |
| 174 | }; |
| 175 | |
| 176 | static const struct udevice_id sandbox_usb_ids[] = { |
| 177 | { .compatible = "sandbox,usb" }, |
| 178 | { } |
| 179 | }; |
| 180 | |
| 181 | U_BOOT_DRIVER(usb_sandbox) = { |
| 182 | .name = "usb_sandbox", |
| 183 | .id = UCLASS_USB, |
| 184 | .of_match = sandbox_usb_ids, |
| 185 | .probe = sandbox_usb_probe, |
| 186 | .ops = &sandbox_usb_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 187 | .priv_auto = sizeof(struct sandbox_usb_ctrl), |
Simon Glass | bc4546a | 2015-03-25 12:22:41 -0600 | [diff] [blame] | 188 | }; |