Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | be4ebd1 | 2015-11-08 23:48:06 -0700 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | be4ebd1 | 2015-11-08 23:48:06 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <os.h> |
| 10 | #include <scsi.h> |
| 11 | #include <usb.h> |
| 12 | |
Simon Glass | be4ebd1 | 2015-11-08 23:48:06 -0700 | [diff] [blame] | 13 | /* |
| 14 | * This driver emulates a USB keyboard using the USB HID specification (boot |
| 15 | * protocol) |
| 16 | */ |
| 17 | |
| 18 | enum { |
| 19 | SANDBOX_KEYB_EP_IN = 1, /* endpoints */ |
| 20 | }; |
| 21 | |
| 22 | enum cmd_phase { |
| 23 | PHASE_START, |
| 24 | PHASE_DATA, |
| 25 | PHASE_STATUS, |
| 26 | }; |
| 27 | |
| 28 | enum { |
| 29 | STRINGID_MANUFACTURER = 1, |
| 30 | STRINGID_PRODUCT, |
| 31 | STRINGID_SERIAL, |
| 32 | |
| 33 | STRINGID_COUNT, |
| 34 | }; |
| 35 | |
| 36 | /** |
| 37 | * struct sandbox_keyb_priv - private state for this driver |
| 38 | * |
| 39 | */ |
| 40 | struct sandbox_keyb_priv { |
| 41 | struct membuff in; |
| 42 | }; |
| 43 | |
| 44 | struct sandbox_keyb_plat { |
| 45 | struct usb_string keyb_strings[STRINGID_COUNT]; |
| 46 | }; |
| 47 | |
| 48 | static struct usb_device_descriptor keyb_device_desc = { |
| 49 | .bLength = sizeof(keyb_device_desc), |
| 50 | .bDescriptorType = USB_DT_DEVICE, |
| 51 | |
| 52 | .bcdUSB = __constant_cpu_to_le16(0x0100), |
| 53 | |
| 54 | .bDeviceClass = 0, |
| 55 | .bDeviceSubClass = 0, |
| 56 | .bDeviceProtocol = 0, |
| 57 | |
| 58 | .idVendor = __constant_cpu_to_le16(0x1234), |
| 59 | .idProduct = __constant_cpu_to_le16(0x5679), |
| 60 | .iManufacturer = STRINGID_MANUFACTURER, |
| 61 | .iProduct = STRINGID_PRODUCT, |
| 62 | .iSerialNumber = STRINGID_SERIAL, |
| 63 | .bNumConfigurations = 1, |
| 64 | }; |
| 65 | |
| 66 | static struct usb_config_descriptor keyb_config0 = { |
| 67 | .bLength = sizeof(keyb_config0), |
| 68 | .bDescriptorType = USB_DT_CONFIG, |
| 69 | |
| 70 | /* wTotalLength is set up by usb-emul-uclass */ |
| 71 | .bNumInterfaces = 2, |
| 72 | .bConfigurationValue = 0, |
| 73 | .iConfiguration = 0, |
| 74 | .bmAttributes = 1 << 7 | 1 << 5, |
| 75 | .bMaxPower = 50, |
| 76 | }; |
| 77 | |
| 78 | static struct usb_interface_descriptor keyb_interface0 = { |
| 79 | .bLength = sizeof(keyb_interface0), |
| 80 | .bDescriptorType = USB_DT_INTERFACE, |
| 81 | |
| 82 | .bInterfaceNumber = 0, |
| 83 | .bAlternateSetting = 0, |
| 84 | .bNumEndpoints = 1, |
| 85 | .bInterfaceClass = USB_CLASS_HID, |
| 86 | .bInterfaceSubClass = USB_SUB_HID_BOOT, |
| 87 | .bInterfaceProtocol = USB_PROT_HID_KEYBOARD, |
| 88 | .iInterface = 0, |
| 89 | }; |
| 90 | |
| 91 | static struct usb_class_hid_descriptor keyb_report0 = { |
| 92 | .bLength = sizeof(keyb_report0), |
| 93 | .bDescriptorType = USB_DT_HID, |
| 94 | .bcdCDC = 0x101, |
| 95 | .bCountryCode = 0, |
| 96 | .bNumDescriptors = 1, |
| 97 | .bDescriptorType0 = USB_DT_HID_REPORT, |
| 98 | .wDescriptorLength0 = 0x3f, |
| 99 | }; |
| 100 | |
| 101 | static struct usb_endpoint_descriptor keyb_endpoint0_in = { |
| 102 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 103 | .bDescriptorType = USB_DT_ENDPOINT, |
| 104 | |
| 105 | .bEndpointAddress = SANDBOX_KEYB_EP_IN | USB_ENDPOINT_DIR_MASK, |
| 106 | .bmAttributes = USB_ENDPOINT_XFER_BULK | |
| 107 | USB_ENDPOINT_XFER_ISOC, |
| 108 | .wMaxPacketSize = __constant_cpu_to_le16(8), |
| 109 | .bInterval = 0xa, |
| 110 | }; |
| 111 | |
| 112 | static struct usb_interface_descriptor keyb_interface1 = { |
| 113 | .bLength = sizeof(keyb_interface1), |
| 114 | .bDescriptorType = USB_DT_INTERFACE, |
| 115 | |
| 116 | .bInterfaceNumber = 1, |
| 117 | .bAlternateSetting = 0, |
| 118 | .bNumEndpoints = 1, |
| 119 | .bInterfaceClass = USB_CLASS_HID, |
| 120 | .bInterfaceSubClass = USB_SUB_HID_BOOT, |
| 121 | .bInterfaceProtocol = USB_PROT_HID_MOUSE, |
| 122 | .iInterface = 0, |
| 123 | }; |
| 124 | |
| 125 | static struct usb_class_hid_descriptor keyb_report1 = { |
| 126 | .bLength = sizeof(struct usb_class_hid_descriptor), |
| 127 | .bDescriptorType = USB_DT_HID, |
| 128 | .bcdCDC = 0x101, |
| 129 | .bCountryCode = 0, |
| 130 | .bNumDescriptors = 1, |
| 131 | .bDescriptorType0 = USB_DT_HID_REPORT, |
| 132 | .wDescriptorLength0 = 0x32, |
| 133 | }; |
| 134 | |
| 135 | static struct usb_endpoint_descriptor keyb_endpoint1_in = { |
| 136 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 137 | .bDescriptorType = USB_DT_ENDPOINT, |
| 138 | |
| 139 | .bEndpointAddress = SANDBOX_KEYB_EP_IN | USB_ENDPOINT_DIR_MASK, |
| 140 | .bmAttributes = USB_ENDPOINT_XFER_BULK | |
| 141 | USB_ENDPOINT_XFER_ISOC, |
| 142 | .wMaxPacketSize = __constant_cpu_to_le16(8), |
| 143 | .bInterval = 0xa, |
| 144 | }; |
| 145 | |
| 146 | static void *keyb_desc_list[] = { |
| 147 | &keyb_device_desc, |
| 148 | &keyb_config0, |
| 149 | &keyb_interface0, |
| 150 | &keyb_report0, |
| 151 | &keyb_endpoint0_in, |
| 152 | &keyb_interface1, |
| 153 | &keyb_report1, |
| 154 | &keyb_endpoint1_in, |
| 155 | NULL, |
| 156 | }; |
| 157 | |
| 158 | int sandbox_usb_keyb_add_string(struct udevice *dev, const char *str) |
| 159 | { |
| 160 | struct sandbox_keyb_priv *priv = dev_get_priv(dev); |
| 161 | int len, ret; |
| 162 | |
| 163 | len = strlen(str); |
| 164 | ret = membuff_put(&priv->in, str, len); |
| 165 | if (ret != len) |
| 166 | return -ENOSPC; |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | static int sandbox_keyb_control(struct udevice *dev, struct usb_device *udev, |
| 172 | unsigned long pipe, void *buff, int len, |
| 173 | struct devrequest *setup) |
| 174 | { |
| 175 | debug("pipe=%lx\n", pipe); |
| 176 | |
| 177 | return -EIO; |
| 178 | } |
| 179 | |
| 180 | static int sandbox_keyb_interrupt(struct udevice *dev, struct usb_device *udev, |
Michal Suchanek | 1c95b9f | 2019-08-18 10:55:27 +0200 | [diff] [blame] | 181 | unsigned long pipe, void *buffer, int length, int interval, |
| 182 | bool nonblock) |
Simon Glass | be4ebd1 | 2015-11-08 23:48:06 -0700 | [diff] [blame] | 183 | { |
| 184 | struct sandbox_keyb_priv *priv = dev_get_priv(dev); |
| 185 | uint8_t *data = buffer; |
| 186 | int ch; |
| 187 | |
| 188 | memset(data, '\0', length); |
| 189 | ch = membuff_getbyte(&priv->in); |
| 190 | if (ch != -1) |
| 191 | data[2] = 4 + ch - 'a'; |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static int sandbox_keyb_bind(struct udevice *dev) |
| 197 | { |
| 198 | struct sandbox_keyb_plat *plat = dev_get_platdata(dev); |
| 199 | struct usb_string *fs; |
| 200 | |
| 201 | fs = plat->keyb_strings; |
| 202 | fs[0].id = STRINGID_MANUFACTURER; |
| 203 | fs[0].s = "sandbox"; |
| 204 | fs[1].id = STRINGID_PRODUCT; |
| 205 | fs[1].s = "keyboard"; |
| 206 | fs[2].id = STRINGID_SERIAL; |
| 207 | fs[2].s = dev->name; |
| 208 | |
Bin Meng | d502cf1 | 2017-10-01 06:19:36 -0700 | [diff] [blame] | 209 | return usb_emul_setup_device(dev, plat->keyb_strings, keyb_desc_list); |
Simon Glass | be4ebd1 | 2015-11-08 23:48:06 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static int sandbox_keyb_probe(struct udevice *dev) |
| 213 | { |
| 214 | struct sandbox_keyb_priv *priv = dev_get_priv(dev); |
| 215 | |
| 216 | return membuff_new(&priv->in, 256); |
| 217 | } |
| 218 | |
| 219 | static const struct dm_usb_ops sandbox_usb_keyb_ops = { |
| 220 | .control = sandbox_keyb_control, |
| 221 | .interrupt = sandbox_keyb_interrupt, |
| 222 | }; |
| 223 | |
| 224 | static const struct udevice_id sandbox_usb_keyb_ids[] = { |
| 225 | { .compatible = "sandbox,usb-keyb" }, |
| 226 | { } |
| 227 | }; |
| 228 | |
| 229 | U_BOOT_DRIVER(usb_sandbox_keyb) = { |
| 230 | .name = "usb_sandbox_keyb", |
| 231 | .id = UCLASS_USB_EMUL, |
| 232 | .of_match = sandbox_usb_keyb_ids, |
| 233 | .bind = sandbox_keyb_bind, |
| 234 | .probe = sandbox_keyb_probe, |
| 235 | .ops = &sandbox_usb_keyb_ops, |
| 236 | .priv_auto_alloc_size = sizeof(struct sandbox_keyb_priv), |
| 237 | .platdata_auto_alloc_size = sizeof(struct sandbox_keyb_plat), |
| 238 | }; |