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 | |
Heinrich Schuchardt | 513255b | 2019-11-23 18:15:23 +0100 | [diff] [blame] | 158 | /** |
| 159 | * sandbox_usb_keyb_add_string() - provide a USB scancode buffer |
| 160 | * |
| 161 | * @dev: the keyboard emulation device |
| 162 | * @scancode: scancode buffer with USB_KBD_BOOT_REPORT_SIZE bytes |
| 163 | */ |
| 164 | int sandbox_usb_keyb_add_string(struct udevice *dev, |
| 165 | const char scancode[USB_KBD_BOOT_REPORT_SIZE]) |
Simon Glass | be4ebd1 | 2015-11-08 23:48:06 -0700 | [diff] [blame] | 166 | { |
| 167 | struct sandbox_keyb_priv *priv = dev_get_priv(dev); |
Heinrich Schuchardt | 513255b | 2019-11-23 18:15:23 +0100 | [diff] [blame] | 168 | int ret; |
Simon Glass | be4ebd1 | 2015-11-08 23:48:06 -0700 | [diff] [blame] | 169 | |
Heinrich Schuchardt | 513255b | 2019-11-23 18:15:23 +0100 | [diff] [blame] | 170 | ret = membuff_put(&priv->in, scancode, USB_KBD_BOOT_REPORT_SIZE); |
| 171 | if (ret != USB_KBD_BOOT_REPORT_SIZE) |
Simon Glass | be4ebd1 | 2015-11-08 23:48:06 -0700 | [diff] [blame] | 172 | return -ENOSPC; |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static int sandbox_keyb_control(struct udevice *dev, struct usb_device *udev, |
| 178 | unsigned long pipe, void *buff, int len, |
| 179 | struct devrequest *setup) |
| 180 | { |
| 181 | debug("pipe=%lx\n", pipe); |
| 182 | |
| 183 | return -EIO; |
| 184 | } |
| 185 | |
| 186 | static int sandbox_keyb_interrupt(struct udevice *dev, struct usb_device *udev, |
Michal Suchanek | 1c95b9f | 2019-08-18 10:55:27 +0200 | [diff] [blame] | 187 | unsigned long pipe, void *buffer, int length, int interval, |
| 188 | bool nonblock) |
Simon Glass | be4ebd1 | 2015-11-08 23:48:06 -0700 | [diff] [blame] | 189 | { |
| 190 | struct sandbox_keyb_priv *priv = dev_get_priv(dev); |
| 191 | uint8_t *data = buffer; |
Simon Glass | be4ebd1 | 2015-11-08 23:48:06 -0700 | [diff] [blame] | 192 | |
| 193 | memset(data, '\0', length); |
Heinrich Schuchardt | 513255b | 2019-11-23 18:15:23 +0100 | [diff] [blame] | 194 | if (length < USB_KBD_BOOT_REPORT_SIZE) |
| 195 | return 0; |
| 196 | |
| 197 | membuff_get(&priv->in, buffer, USB_KBD_BOOT_REPORT_SIZE); |
Simon Glass | be4ebd1 | 2015-11-08 23:48:06 -0700 | [diff] [blame] | 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | static int sandbox_keyb_bind(struct udevice *dev) |
| 203 | { |
| 204 | struct sandbox_keyb_plat *plat = dev_get_platdata(dev); |
| 205 | struct usb_string *fs; |
| 206 | |
| 207 | fs = plat->keyb_strings; |
| 208 | fs[0].id = STRINGID_MANUFACTURER; |
| 209 | fs[0].s = "sandbox"; |
| 210 | fs[1].id = STRINGID_PRODUCT; |
| 211 | fs[1].s = "keyboard"; |
| 212 | fs[2].id = STRINGID_SERIAL; |
| 213 | fs[2].s = dev->name; |
| 214 | |
Bin Meng | d502cf1 | 2017-10-01 06:19:36 -0700 | [diff] [blame] | 215 | return usb_emul_setup_device(dev, plat->keyb_strings, keyb_desc_list); |
Simon Glass | be4ebd1 | 2015-11-08 23:48:06 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | static int sandbox_keyb_probe(struct udevice *dev) |
| 219 | { |
| 220 | struct sandbox_keyb_priv *priv = dev_get_priv(dev); |
| 221 | |
Heinrich Schuchardt | 513255b | 2019-11-23 18:15:23 +0100 | [diff] [blame] | 222 | /* Provide an 80 character keyboard buffer */ |
| 223 | return membuff_new(&priv->in, 80 * USB_KBD_BOOT_REPORT_SIZE); |
Simon Glass | be4ebd1 | 2015-11-08 23:48:06 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | static const struct dm_usb_ops sandbox_usb_keyb_ops = { |
| 227 | .control = sandbox_keyb_control, |
| 228 | .interrupt = sandbox_keyb_interrupt, |
| 229 | }; |
| 230 | |
| 231 | static const struct udevice_id sandbox_usb_keyb_ids[] = { |
| 232 | { .compatible = "sandbox,usb-keyb" }, |
| 233 | { } |
| 234 | }; |
| 235 | |
| 236 | U_BOOT_DRIVER(usb_sandbox_keyb) = { |
| 237 | .name = "usb_sandbox_keyb", |
| 238 | .id = UCLASS_USB_EMUL, |
| 239 | .of_match = sandbox_usb_keyb_ids, |
| 240 | .bind = sandbox_keyb_bind, |
| 241 | .probe = sandbox_keyb_probe, |
| 242 | .ops = &sandbox_usb_keyb_ops, |
| 243 | .priv_auto_alloc_size = sizeof(struct sandbox_keyb_priv), |
| 244 | .platdata_auto_alloc_size = sizeof(struct sandbox_keyb_plat), |
| 245 | }; |