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