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