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