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 |
|
---|
29 | /** @addtogroup usb
|
---|
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 |
|
---|
47 | #include <usb/usb.h>
|
---|
48 | #include <usb/descriptor.h>
|
---|
49 | #include <usb/classes/hid.h>
|
---|
50 | #include <usbvirt/device.h>
|
---|
51 | #include <usbvirt/hub.h>
|
---|
52 |
|
---|
53 | #include "kbdconfig.h"
|
---|
54 | #include "keys.h"
|
---|
55 | #include "stdreq.h"
|
---|
56 |
|
---|
57 | #define LOOPS 5
|
---|
58 | #define NAME "virt-usb-kbd"
|
---|
59 |
|
---|
60 |
|
---|
61 | #define __QUOTEME(x) #x
|
---|
62 | #define _QUOTEME(x) __QUOTEME(x)
|
---|
63 |
|
---|
64 | #define VERBOSE_EXEC(cmd, fmt, ...) \
|
---|
65 | (printf("%s: %s" fmt "\n", NAME, _QUOTEME(cmd), __VA_ARGS__), cmd(__VA_ARGS__))
|
---|
66 |
|
---|
67 | kb_status_t status;
|
---|
68 |
|
---|
69 | static int on_incoming_data(struct usbvirt_device *dev,
|
---|
70 | usb_endpoint_t endpoint, void *buffer, size_t size)
|
---|
71 | {
|
---|
72 | printf("%s: ignoring incomming data to endpoint %d\n", NAME, endpoint);
|
---|
73 |
|
---|
74 | return EOK;
|
---|
75 | }
|
---|
76 |
|
---|
77 | static int on_class_request(struct usbvirt_device *dev,
|
---|
78 | usb_device_request_setup_packet_t *request, uint8_t *data)
|
---|
79 | {
|
---|
80 | printf("%s: class request (%d)\n", NAME, (int) request->request);
|
---|
81 |
|
---|
82 | return EOK;
|
---|
83 | }
|
---|
84 |
|
---|
85 | static int on_request_for_data(struct usbvirt_device *dev,
|
---|
86 | usb_endpoint_t endpoint, void *buffer, size_t size, size_t *actual_size)
|
---|
87 | {
|
---|
88 | if (size < 2 + KB_MAX_KEYS_AT_ONCE) {
|
---|
89 | return EINVAL;
|
---|
90 | }
|
---|
91 |
|
---|
92 | *actual_size = 2 + KB_MAX_KEYS_AT_ONCE;
|
---|
93 |
|
---|
94 | uint8_t data[2 + KB_MAX_KEYS_AT_ONCE];
|
---|
95 | data[0] = status.modifiers;
|
---|
96 | data[1] = 0;
|
---|
97 |
|
---|
98 | size_t i;
|
---|
99 | for (i = 0; i < KB_MAX_KEYS_AT_ONCE; i++) {
|
---|
100 | data[2 + i] = status.pressed_keys[i];
|
---|
101 | }
|
---|
102 |
|
---|
103 | memcpy(buffer, &data, *actual_size);
|
---|
104 |
|
---|
105 | return EOK;
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | /** Keyboard callbacks.
|
---|
110 | * We abuse the fact that static variables are zero-filled.
|
---|
111 | */
|
---|
112 | static usbvirt_device_ops_t keyboard_ops = {
|
---|
113 | .standard_request_ops = &standard_request_ops,
|
---|
114 | .on_class_device_request = on_class_request,
|
---|
115 | .on_data = on_incoming_data,
|
---|
116 | .on_data_request = on_request_for_data
|
---|
117 | };
|
---|
118 |
|
---|
119 | usbvirt_device_configuration_extras_t extra_descriptors[] = {
|
---|
120 | {
|
---|
121 | .data = (uint8_t *) &std_interface_descriptor,
|
---|
122 | .length = sizeof(std_interface_descriptor)
|
---|
123 | },
|
---|
124 | {
|
---|
125 | .data = (uint8_t *) &hid_descriptor,
|
---|
126 | .length = sizeof(hid_descriptor)
|
---|
127 | },
|
---|
128 | {
|
---|
129 | .data = (uint8_t *) &endpoint_descriptor,
|
---|
130 | .length = sizeof(endpoint_descriptor)
|
---|
131 | }
|
---|
132 | };
|
---|
133 |
|
---|
134 | /** Keyboard configuration. */
|
---|
135 | usbvirt_device_configuration_t configuration = {
|
---|
136 | .descriptor = &std_configuration_descriptor,
|
---|
137 | .extra = extra_descriptors,
|
---|
138 | .extra_count = sizeof(extra_descriptors)/sizeof(extra_descriptors[0])
|
---|
139 | };
|
---|
140 |
|
---|
141 | /** Keyboard standard descriptors. */
|
---|
142 | usbvirt_descriptors_t descriptors = {
|
---|
143 | .device = &std_device_descriptor,
|
---|
144 | .configuration = &configuration,
|
---|
145 | .configuration_count = 1,
|
---|
146 | };
|
---|
147 |
|
---|
148 | /** Keyboard device.
|
---|
149 | * Rest of the items will be initialized later.
|
---|
150 | */
|
---|
151 | static usbvirt_device_t keyboard_dev = {
|
---|
152 | .ops = &keyboard_ops,
|
---|
153 | .descriptors = &descriptors,
|
---|
154 | .name = "keyboard"
|
---|
155 | };
|
---|
156 |
|
---|
157 |
|
---|
158 | static void fibril_sleep(size_t sec)
|
---|
159 | {
|
---|
160 | while (sec-- > 0) {
|
---|
161 | async_usleep(1000*1000);
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | /** Callback when keyboard status changed.
|
---|
167 | *
|
---|
168 | * @param status Current keyboard status.
|
---|
169 | */
|
---|
170 | static void on_keyboard_change(kb_status_t *status)
|
---|
171 | {
|
---|
172 | printf("%s: Current keyboard status: %02hhx", NAME, status->modifiers);
|
---|
173 | size_t i;
|
---|
174 | for (i = 0; i < KB_MAX_KEYS_AT_ONCE; i++) {
|
---|
175 | printf(" 0x%02X", (int)status->pressed_keys[i]);
|
---|
176 | }
|
---|
177 | printf("\n");
|
---|
178 |
|
---|
179 | fibril_sleep(1);
|
---|
180 | }
|
---|
181 |
|
---|
182 | /** Simulated keyboard events. */
|
---|
183 | static kb_event_t keyboard_events[] = {
|
---|
184 | /* Switch to VT6 (Alt+F6) */
|
---|
185 | M_DOWN(KB_MOD_LEFT_ALT),
|
---|
186 | K_PRESS(KB_KEY_F6),
|
---|
187 | M_UP(KB_MOD_LEFT_ALT),
|
---|
188 | /* Type the word 'Hello' */
|
---|
189 | M_DOWN(KB_MOD_LEFT_SHIFT),
|
---|
190 | K_PRESS(KB_KEY_H),
|
---|
191 | M_UP(KB_MOD_LEFT_SHIFT),
|
---|
192 | K_PRESS(KB_KEY_E),
|
---|
193 | K_PRESS(KB_KEY_L),
|
---|
194 | K_PRESS(KB_KEY_L),
|
---|
195 | K_PRESS(KB_KEY_O)
|
---|
196 | };
|
---|
197 | static size_t keyboard_events_count =
|
---|
198 | sizeof(keyboard_events)/sizeof(keyboard_events[0]);
|
---|
199 |
|
---|
200 |
|
---|
201 |
|
---|
202 | int main(int argc, char * argv[])
|
---|
203 | {
|
---|
204 | printf("Dump of report descriptor (%zu bytes):\n", report_descriptor_size);
|
---|
205 | size_t i;
|
---|
206 | for (i = 0; i < report_descriptor_size; i++) {
|
---|
207 | printf(" 0x%02X", report_descriptor[i]);
|
---|
208 | if (((i > 0) && (((i+1) % 10) == 0))
|
---|
209 | || (i + 1 == report_descriptor_size)) {
|
---|
210 | printf("\n");
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | kb_init(&status);
|
---|
215 |
|
---|
216 |
|
---|
217 | int rc = usbvirt_connect(&keyboard_dev);
|
---|
218 | if (rc != EOK) {
|
---|
219 | printf("%s: Unable to start communication with VHCD (%s).\n",
|
---|
220 | NAME, str_error(rc));
|
---|
221 | return rc;
|
---|
222 | }
|
---|
223 |
|
---|
224 | printf("%s: Simulating keyboard events...\n", NAME);
|
---|
225 | kb_process_events(&status, keyboard_events, keyboard_events_count,
|
---|
226 | on_keyboard_change);
|
---|
227 |
|
---|
228 | printf("%s: Terminating...\n", NAME);
|
---|
229 |
|
---|
230 | usbvirt_disconnect(&keyboard_dev);
|
---|
231 |
|
---|
232 | return 0;
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 | /** @}
|
---|
237 | */
|
---|