[bc9a629] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Vojtech Horky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[679a135] | 29 | /** @addtogroup usbvirtkbd
|
---|
[bc9a629] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | * @brief Virtual USB keyboard.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <stdio.h>
|
---|
| 38 | #include <stdlib.h>
|
---|
| 39 | #include <unistd.h>
|
---|
| 40 | #include <vfs/vfs.h>
|
---|
| 41 | #include <fcntl.h>
|
---|
| 42 | #include <errno.h>
|
---|
| 43 | #include <str_error.h>
|
---|
| 44 | #include <bool.h>
|
---|
| 45 | #include <async.h>
|
---|
| 46 |
|
---|
[4b4c797] | 47 | #include <usb/usb.h>
|
---|
[2193471] | 48 | #include <usb/descriptor.h>
|
---|
[4b4c797] | 49 | #include <usb/classes/hid.h>
|
---|
[b8100da] | 50 | #include <usbvirt/device.h>
|
---|
| 51 | #include <usbvirt/hub.h>
|
---|
[bc9a629] | 52 |
|
---|
[bbf79e0] | 53 | #include "kbdconfig.h"
|
---|
[7da3219] | 54 | #include "keys.h"
|
---|
[73301a0] | 55 | #include "stdreq.h"
|
---|
[5247411] | 56 |
|
---|
[2aee3e06] | 57 | /** Pause between individual key-presses in seconds. */
|
---|
| 58 | #define KEY_PRESS_DELAY 2
|
---|
[bc9a629] | 59 | #define NAME "virt-usb-kbd"
|
---|
| 60 |
|
---|
| 61 |
|
---|
| 62 | #define __QUOTEME(x) #x
|
---|
| 63 | #define _QUOTEME(x) __QUOTEME(x)
|
---|
| 64 |
|
---|
| 65 | #define VERBOSE_EXEC(cmd, fmt, ...) \
|
---|
| 66 | (printf("%s: %s" fmt "\n", NAME, _QUOTEME(cmd), __VA_ARGS__), cmd(__VA_ARGS__))
|
---|
| 67 |
|
---|
[7a7bfeb3] | 68 | kb_status_t status;
|
---|
| 69 |
|
---|
[b8100da] | 70 | static int on_incoming_data(struct usbvirt_device *dev,
|
---|
| 71 | usb_endpoint_t endpoint, void *buffer, size_t size)
|
---|
| 72 | {
|
---|
| 73 | printf("%s: ignoring incomming data to endpoint %d\n", NAME, endpoint);
|
---|
| 74 |
|
---|
| 75 | return EOK;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[d97d209] | 78 |
|
---|
[2aee3e06] | 79 | /** Compares current and last status of pressed keys.
|
---|
| 80 | *
|
---|
| 81 | * @warning Has side-efect - changes status_last field.
|
---|
| 82 | *
|
---|
| 83 | * @param status_now Status now.
|
---|
| 84 | * @param status_last Last status.
|
---|
| 85 | * @param len Size of status.
|
---|
| 86 | * @return Whether they are the same.
|
---|
| 87 | */
|
---|
| 88 | static bool keypress_check_with_last_request(uint8_t *status_now,
|
---|
| 89 | uint8_t *status_last, size_t len)
|
---|
| 90 | {
|
---|
| 91 | bool same = true;
|
---|
| 92 | size_t i;
|
---|
| 93 | for (i = 0; i < len; i++) {
|
---|
| 94 | if (status_now[i] != status_last[i]) {
|
---|
| 95 | status_last[i] = status_now[i];
|
---|
| 96 | same = false;
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 | return same;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[7a7bfeb3] | 102 | static int on_request_for_data(struct usbvirt_device *dev,
|
---|
| 103 | usb_endpoint_t endpoint, void *buffer, size_t size, size_t *actual_size)
|
---|
| 104 | {
|
---|
[2aee3e06] | 105 | static uint8_t last_data[2 + KB_MAX_KEYS_AT_ONCE];
|
---|
| 106 |
|
---|
[7a7bfeb3] | 107 | if (size < 2 + KB_MAX_KEYS_AT_ONCE) {
|
---|
| 108 | return EINVAL;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | *actual_size = 2 + KB_MAX_KEYS_AT_ONCE;
|
---|
| 112 |
|
---|
| 113 | uint8_t data[2 + KB_MAX_KEYS_AT_ONCE];
|
---|
| 114 | data[0] = status.modifiers;
|
---|
| 115 | data[1] = 0;
|
---|
| 116 |
|
---|
| 117 | size_t i;
|
---|
| 118 | for (i = 0; i < KB_MAX_KEYS_AT_ONCE; i++) {
|
---|
| 119 | data[2 + i] = status.pressed_keys[i];
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[2aee3e06] | 122 | if (keypress_check_with_last_request(data, last_data,
|
---|
| 123 | 2 + KB_MAX_KEYS_AT_ONCE)) {
|
---|
| 124 | *actual_size = 0;
|
---|
| 125 | return EOK;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[7a7bfeb3] | 128 | memcpy(buffer, &data, *actual_size);
|
---|
| 129 |
|
---|
| 130 | return EOK;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[7feeb84] | 133 | static usbvirt_control_transfer_handler_t endpoint_zero_handlers[] = {
|
---|
| 134 | {
|
---|
| 135 | .request_type = USBVIRT_MAKE_CONTROL_REQUEST_TYPE(
|
---|
| 136 | USB_DIRECTION_IN,
|
---|
| 137 | USBVIRT_REQUEST_TYPE_STANDARD,
|
---|
| 138 | USBVIRT_REQUEST_RECIPIENT_DEVICE),
|
---|
| 139 | .request = USB_DEVREQ_GET_DESCRIPTOR,
|
---|
[d5e7668] | 140 | .name = "GetDescriptor",
|
---|
[7feeb84] | 141 | .callback = stdreq_on_get_descriptor
|
---|
| 142 | },
|
---|
| 143 | {
|
---|
| 144 | .request_type = USBVIRT_MAKE_CONTROL_REQUEST_TYPE(
|
---|
| 145 | USB_DIRECTION_IN,
|
---|
| 146 | USBVIRT_REQUEST_TYPE_CLASS,
|
---|
| 147 | USBVIRT_REQUEST_RECIPIENT_DEVICE),
|
---|
| 148 | .request = USB_DEVREQ_GET_DESCRIPTOR,
|
---|
[d5e7668] | 149 | .name = "GetDescriptor",
|
---|
[7feeb84] | 150 | .callback = stdreq_on_get_descriptor
|
---|
| 151 | },
|
---|
| 152 | USBVIRT_CONTROL_TRANSFER_HANDLER_LAST
|
---|
| 153 | };
|
---|
[7a7bfeb3] | 154 |
|
---|
[b8100da] | 155 | /** Keyboard callbacks.
|
---|
| 156 | * We abuse the fact that static variables are zero-filled.
|
---|
| 157 | */
|
---|
| 158 | static usbvirt_device_ops_t keyboard_ops = {
|
---|
[7feeb84] | 159 | .control_transfer_handlers = endpoint_zero_handlers,
|
---|
[7a7bfeb3] | 160 | .on_data = on_incoming_data,
|
---|
| 161 | .on_data_request = on_request_for_data
|
---|
[b8100da] | 162 | };
|
---|
| 163 |
|
---|
[2c381250] | 164 | usbvirt_device_configuration_extras_t extra_descriptors[] = {
|
---|
| 165 | {
|
---|
| 166 | .data = (uint8_t *) &std_interface_descriptor,
|
---|
| 167 | .length = sizeof(std_interface_descriptor)
|
---|
| 168 | },
|
---|
| 169 | {
|
---|
| 170 | .data = (uint8_t *) &hid_descriptor,
|
---|
| 171 | .length = sizeof(hid_descriptor)
|
---|
| 172 | },
|
---|
| 173 | {
|
---|
| 174 | .data = (uint8_t *) &endpoint_descriptor,
|
---|
| 175 | .length = sizeof(endpoint_descriptor)
|
---|
| 176 | }
|
---|
| 177 | };
|
---|
| 178 |
|
---|
| 179 | /** Keyboard configuration. */
|
---|
| 180 | usbvirt_device_configuration_t configuration = {
|
---|
| 181 | .descriptor = &std_configuration_descriptor,
|
---|
| 182 | .extra = extra_descriptors,
|
---|
| 183 | .extra_count = sizeof(extra_descriptors)/sizeof(extra_descriptors[0])
|
---|
| 184 | };
|
---|
| 185 |
|
---|
| 186 | /** Keyboard standard descriptors. */
|
---|
| 187 | usbvirt_descriptors_t descriptors = {
|
---|
| 188 | .device = &std_device_descriptor,
|
---|
| 189 | .configuration = &configuration,
|
---|
| 190 | .configuration_count = 1,
|
---|
| 191 | };
|
---|
| 192 |
|
---|
[b8100da] | 193 | /** Keyboard device.
|
---|
| 194 | * Rest of the items will be initialized later.
|
---|
| 195 | */
|
---|
| 196 | static usbvirt_device_t keyboard_dev = {
|
---|
| 197 | .ops = &keyboard_ops,
|
---|
[2c381250] | 198 | .descriptors = &descriptors,
|
---|
[2aee3e06] | 199 | .lib_debug_level = 3,
|
---|
| 200 | .lib_debug_enabled_tags = USBVIRT_DEBUGTAG_ALL,
|
---|
[ca07cd3] | 201 | .name = "keyboard"
|
---|
[b8100da] | 202 | };
|
---|
| 203 |
|
---|
| 204 |
|
---|
[bc9a629] | 205 | static void fibril_sleep(size_t sec)
|
---|
| 206 | {
|
---|
| 207 | while (sec-- > 0) {
|
---|
| 208 | async_usleep(1000*1000);
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 |
|
---|
[7da3219] | 213 | /** Callback when keyboard status changed.
|
---|
| 214 | *
|
---|
| 215 | * @param status Current keyboard status.
|
---|
| 216 | */
|
---|
| 217 | static void on_keyboard_change(kb_status_t *status)
|
---|
| 218 | {
|
---|
[bbe7848] | 219 | printf("%s: Current keyboard status: %02hhx", NAME, status->modifiers);
|
---|
[7da3219] | 220 | size_t i;
|
---|
| 221 | for (i = 0; i < KB_MAX_KEYS_AT_ONCE; i++) {
|
---|
| 222 | printf(" 0x%02X", (int)status->pressed_keys[i]);
|
---|
| 223 | }
|
---|
| 224 | printf("\n");
|
---|
| 225 |
|
---|
[2aee3e06] | 226 | fibril_sleep(KEY_PRESS_DELAY);
|
---|
[7da3219] | 227 | }
|
---|
| 228 |
|
---|
| 229 | /** Simulated keyboard events. */
|
---|
| 230 | static kb_event_t keyboard_events[] = {
|
---|
| 231 | /* Switch to VT6 (Alt+F6) */
|
---|
| 232 | M_DOWN(KB_MOD_LEFT_ALT),
|
---|
[1880c65] | 233 | K_PRESS(KB_KEY_F6),
|
---|
[7da3219] | 234 | M_UP(KB_MOD_LEFT_ALT),
|
---|
| 235 | /* Type the word 'Hello' */
|
---|
| 236 | M_DOWN(KB_MOD_LEFT_SHIFT),
|
---|
| 237 | K_PRESS(KB_KEY_H),
|
---|
| 238 | M_UP(KB_MOD_LEFT_SHIFT),
|
---|
| 239 | K_PRESS(KB_KEY_E),
|
---|
| 240 | K_PRESS(KB_KEY_L),
|
---|
| 241 | K_PRESS(KB_KEY_L),
|
---|
| 242 | K_PRESS(KB_KEY_O)
|
---|
| 243 | };
|
---|
| 244 | static size_t keyboard_events_count =
|
---|
| 245 | sizeof(keyboard_events)/sizeof(keyboard_events[0]);
|
---|
| 246 |
|
---|
| 247 |
|
---|
| 248 |
|
---|
[bc9a629] | 249 | int main(int argc, char * argv[])
|
---|
| 250 | {
|
---|
[ad79d80] | 251 | printf("Dump of report descriptor (%zu bytes):\n", report_descriptor_size);
|
---|
[5247411] | 252 | size_t i;
|
---|
| 253 | for (i = 0; i < report_descriptor_size; i++) {
|
---|
| 254 | printf(" 0x%02X", report_descriptor[i]);
|
---|
| 255 | if (((i > 0) && (((i+1) % 10) == 0))
|
---|
| 256 | || (i + 1 == report_descriptor_size)) {
|
---|
| 257 | printf("\n");
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
| 260 |
|
---|
[7a7bfeb3] | 261 | kb_init(&status);
|
---|
| 262 |
|
---|
[5247411] | 263 |
|
---|
[e27595b] | 264 | int rc = usbvirt_connect(&keyboard_dev);
|
---|
[b8100da] | 265 | if (rc != EOK) {
|
---|
[e27595b] | 266 | printf("%s: Unable to start communication with VHCD (%s).\n",
|
---|
| 267 | NAME, str_error(rc));
|
---|
[b8100da] | 268 | return rc;
|
---|
[bc9a629] | 269 | }
|
---|
| 270 |
|
---|
[7da3219] | 271 | printf("%s: Simulating keyboard events...\n", NAME);
|
---|
[d5e7668] | 272 | fibril_sleep(10);
|
---|
[1f383dde] | 273 | //while (1) {
|
---|
[9223dc5c] | 274 | kb_process_events(&status, keyboard_events, keyboard_events_count,
|
---|
| 275 | on_keyboard_change);
|
---|
[1f383dde] | 276 | //}
|
---|
[bc9a629] | 277 |
|
---|
| 278 | printf("%s: Terminating...\n", NAME);
|
---|
| 279 |
|
---|
[ca07cd3] | 280 | usbvirt_disconnect(&keyboard_dev);
|
---|
[bc9a629] | 281 |
|
---|
| 282 | return 0;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 |
|
---|
| 286 | /** @}
|
---|
| 287 | */
|
---|