Simon Glass | dfd8400 | 2015-03-25 12:22:41 -0600 | [diff] [blame] | 1 | /* |
| 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 | |
| 13 | DECLARE_GLOBAL_DATA_PTR; |
| 14 | |
| 15 | static void usbmon_trace(struct udevice *bus, ulong pipe, |
| 16 | struct devrequest *setup, struct udevice *emul) |
| 17 | { |
| 18 | static const char types[] = "ZICB"; |
| 19 | int type; |
| 20 | |
| 21 | type = (pipe & USB_PIPE_TYPE_MASK) >> USB_PIPE_TYPE_SHIFT; |
| 22 | debug("0 0 S %c%c:%d:%03ld:%ld", types[type], |
| 23 | pipe & USB_DIR_IN ? 'i' : 'o', |
| 24 | bus->seq, |
| 25 | (pipe & USB_PIPE_DEV_MASK) >> USB_PIPE_DEV_SHIFT, |
| 26 | (pipe & USB_PIPE_EP_MASK) >> USB_PIPE_EP_SHIFT); |
| 27 | if (setup) { |
| 28 | debug(" s %02x %02x %04x %04x %04x", setup->requesttype, |
| 29 | setup->request, setup->value, setup->index, |
| 30 | setup->length); |
| 31 | } |
| 32 | debug(" %s", emul ? emul->name : "(no emul found)"); |
| 33 | |
| 34 | debug("\n"); |
| 35 | } |
| 36 | |
| 37 | static int sandbox_submit_control(struct udevice *bus, |
| 38 | struct usb_device *udev, |
| 39 | unsigned long pipe, |
| 40 | void *buffer, int length, |
| 41 | struct devrequest *setup) |
| 42 | { |
| 43 | struct udevice *emul; |
| 44 | int ret; |
| 45 | |
| 46 | /* Just use child of dev as emulator? */ |
| 47 | debug("%s: bus=%s\n", __func__, bus->name); |
| 48 | ret = usb_emul_find(bus, pipe, &emul); |
| 49 | usbmon_trace(bus, pipe, setup, emul); |
| 50 | if (ret) |
| 51 | return ret; |
| 52 | ret = usb_emul_control(emul, udev, pipe, buffer, length, setup); |
| 53 | if (ret < 0) { |
| 54 | debug("ret=%d\n", ret); |
| 55 | udev->status = ret; |
| 56 | udev->act_len = 0; |
| 57 | } else { |
| 58 | udev->status = 0; |
| 59 | udev->act_len = ret; |
| 60 | } |
| 61 | |
| 62 | return ret; |
| 63 | } |
| 64 | |
| 65 | static int sandbox_submit_bulk(struct udevice *bus, struct usb_device *udev, |
| 66 | unsigned long pipe, void *buffer, int length) |
| 67 | { |
| 68 | struct udevice *emul; |
| 69 | int ret; |
| 70 | |
| 71 | /* Just use child of dev as emulator? */ |
| 72 | debug("%s: bus=%s\n", __func__, bus->name); |
| 73 | ret = usb_emul_find(bus, pipe, &emul); |
| 74 | usbmon_trace(bus, pipe, NULL, emul); |
| 75 | if (ret) |
| 76 | return ret; |
| 77 | ret = usb_emul_bulk(emul, udev, pipe, buffer, length); |
| 78 | if (ret < 0) { |
| 79 | debug("ret=%d\n", ret); |
| 80 | udev->status = ret; |
| 81 | udev->act_len = 0; |
| 82 | } else { |
| 83 | udev->status = 0; |
| 84 | udev->act_len = ret; |
| 85 | } |
| 86 | |
| 87 | return ret; |
| 88 | } |
| 89 | |
Simon Glass | b70a3fe | 2015-11-08 23:48:05 -0700 | [diff] [blame] | 90 | static int sandbox_submit_int(struct udevice *bus, struct usb_device *udev, |
| 91 | unsigned long pipe, void *buffer, int length, |
| 92 | int interval) |
| 93 | { |
| 94 | struct udevice *emul; |
| 95 | int ret; |
| 96 | |
| 97 | /* Just use child of dev as emulator? */ |
| 98 | debug("%s: bus=%s\n", __func__, bus->name); |
| 99 | ret = usb_emul_find(bus, pipe, &emul); |
| 100 | usbmon_trace(bus, pipe, NULL, emul); |
| 101 | if (ret) |
| 102 | return ret; |
| 103 | ret = usb_emul_int(emul, udev, pipe, buffer, length, interval); |
| 104 | |
| 105 | return ret; |
| 106 | } |
| 107 | |
Simon Glass | dfd8400 | 2015-03-25 12:22:41 -0600 | [diff] [blame] | 108 | static int sandbox_alloc_device(struct udevice *dev, struct usb_device *udev) |
| 109 | { |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static int sandbox_usb_probe(struct udevice *dev) |
| 114 | { |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static const struct dm_usb_ops sandbox_usb_ops = { |
| 119 | .control = sandbox_submit_control, |
| 120 | .bulk = sandbox_submit_bulk, |
Simon Glass | b70a3fe | 2015-11-08 23:48:05 -0700 | [diff] [blame] | 121 | .interrupt = sandbox_submit_int, |
Simon Glass | dfd8400 | 2015-03-25 12:22:41 -0600 | [diff] [blame] | 122 | .alloc_device = sandbox_alloc_device, |
| 123 | }; |
| 124 | |
| 125 | static const struct udevice_id sandbox_usb_ids[] = { |
| 126 | { .compatible = "sandbox,usb" }, |
| 127 | { } |
| 128 | }; |
| 129 | |
| 130 | U_BOOT_DRIVER(usb_sandbox) = { |
| 131 | .name = "usb_sandbox", |
| 132 | .id = UCLASS_USB, |
| 133 | .of_match = sandbox_usb_ids, |
| 134 | .probe = sandbox_usb_probe, |
| 135 | .ops = &sandbox_usb_ops, |
| 136 | }; |