source: mainline/uspace/app/virtusbkbd/virtusbkbd.c@ c82135a8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c82135a8 was 6cb58e6, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

Virtual USB layer rewritten

Major changes include

  • IPC sends whole transfers (not transactions)
  • separate transfer queues for each device in host controller
  • possibility to return NAK from virtual device (handled by HC)
  • better implementation of callbacks for non-zero endpoints

Still missing

  • communication for some transfer types (bulk)
  • face-lift ;-)
  • documentation
  • Property mode set to 100644
File size: 6.5 KB
Line 
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 usbvirtkbd
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 <usb/debug.h>
51#include <usbvirt/device.h>
52
53#include "kbdconfig.h"
54#include "keys.h"
55#include "stdreq.h"
56
57/** Pause between individual key-presses in seconds. */
58#define KEY_PRESS_DELAY 2
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
68kb_status_t status;
69
70/** Compares current and last status of pressed keys.
71 *
72 * @warning Has side-efect - changes status_last field.
73 *
74 * @param status_now Status now.
75 * @param status_last Last status.
76 * @param len Size of status.
77 * @return Whether they are the same.
78 */
79static bool keypress_check_with_last_request(uint8_t *status_now,
80 uint8_t *status_last, size_t len)
81{
82 bool same = true;
83 size_t i;
84 for (i = 0; i < len; i++) {
85 if (status_now[i] != status_last[i]) {
86 status_last[i] = status_now[i];
87 same = false;
88 }
89 }
90 return same;
91}
92
93static int on_request_for_data(usbvirt_device_t *dev,
94 usb_endpoint_t endpoint, usb_transfer_type_t transfer_type,
95 void *buffer, size_t size, size_t *actual_size)
96{
97 static uint8_t last_data[2 + KB_MAX_KEYS_AT_ONCE];
98
99 if (size < 2 + KB_MAX_KEYS_AT_ONCE) {
100 return EINVAL;
101 }
102
103 *actual_size = 2 + KB_MAX_KEYS_AT_ONCE;
104
105 uint8_t data[2 + KB_MAX_KEYS_AT_ONCE];
106 data[0] = status.modifiers;
107 data[1] = 0;
108
109 size_t i;
110 for (i = 0; i < KB_MAX_KEYS_AT_ONCE; i++) {
111 data[2 + i] = status.pressed_keys[i];
112 }
113
114 if (keypress_check_with_last_request(data, last_data,
115 2 + KB_MAX_KEYS_AT_ONCE)) {
116 return ENAK;
117 }
118
119 memcpy(buffer, &data, *actual_size);
120
121 return EOK;
122}
123
124static usbvirt_control_request_handler_t endpoint_zero_handlers[] = {
125 {
126 .req_direction = USB_DIRECTION_IN,
127 .req_type = USB_REQUEST_TYPE_STANDARD,
128 .req_recipient = USB_REQUEST_RECIPIENT_INTERFACE,
129 .request = USB_DEVREQ_GET_DESCRIPTOR,
130 .name = "GetDescriptor",
131 .callback = req_get_descriptor
132 },
133 {
134 .callback = NULL
135 }
136};
137
138/** Keyboard callbacks.
139 * We abuse the fact that static variables are zero-filled.
140 */
141static usbvirt_device_ops_t keyboard_ops = {
142 .control = endpoint_zero_handlers,
143 .data_in[1] = on_request_for_data
144};
145
146usbvirt_device_configuration_extras_t extra_descriptors[] = {
147 {
148 .data = (uint8_t *) &std_interface_descriptor,
149 .length = sizeof(std_interface_descriptor)
150 },
151 {
152 .data = (uint8_t *) &hid_descriptor,
153 .length = sizeof(hid_descriptor)
154 },
155 {
156 .data = (uint8_t *) &endpoint_descriptor,
157 .length = sizeof(endpoint_descriptor)
158 }
159};
160
161/** Keyboard configuration. */
162usbvirt_device_configuration_t configuration = {
163 .descriptor = &std_configuration_descriptor,
164 .extra = extra_descriptors,
165 .extra_count = sizeof(extra_descriptors)/sizeof(extra_descriptors[0])
166};
167
168/** Keyboard standard descriptors. */
169usbvirt_descriptors_t descriptors = {
170 .device = &std_device_descriptor,
171 .configuration = &configuration,
172 .configuration_count = 1,
173};
174
175/** Keyboard device.
176 * Rest of the items will be initialized later.
177 */
178static usbvirt_device_t keyboard_dev = {
179 .ops = &keyboard_ops,
180 .descriptors = &descriptors,
181 .name = "keyboard"
182};
183
184
185static void fibril_sleep(size_t sec)
186{
187 while (sec-- > 0) {
188 async_usleep(1000*1000);
189 }
190}
191
192
193/** Callback when keyboard status changed.
194 *
195 * @param status Current keyboard status.
196 */
197static void on_keyboard_change(kb_status_t *status)
198{
199 printf("%s: Current keyboard status: %02hhx", NAME, status->modifiers);
200 size_t i;
201 for (i = 0; i < KB_MAX_KEYS_AT_ONCE; i++) {
202 printf(" 0x%02X", (int)status->pressed_keys[i]);
203 }
204 printf("\n");
205
206 fibril_sleep(KEY_PRESS_DELAY);
207}
208
209/** Simulated keyboard events. */
210static kb_event_t keyboard_events[] = {
211 /* Switch to VT6 (Alt+F6) */
212 M_DOWN(KB_MOD_LEFT_ALT),
213 K_PRESS(KB_KEY_F6),
214 M_UP(KB_MOD_LEFT_ALT),
215 /* Type the word 'Hello' */
216 M_DOWN(KB_MOD_LEFT_SHIFT),
217 K_PRESS(KB_KEY_H),
218 M_UP(KB_MOD_LEFT_SHIFT),
219 K_PRESS(KB_KEY_E),
220 K_PRESS(KB_KEY_L),
221 K_PRESS(KB_KEY_L),
222 K_PRESS(KB_KEY_O)
223};
224static size_t keyboard_events_count =
225 sizeof(keyboard_events)/sizeof(keyboard_events[0]);
226
227
228
229int main(int argc, char * argv[])
230{
231 printf("Dump of report descriptor (%zu bytes):\n", report_descriptor_size);
232 size_t i;
233 for (i = 0; i < report_descriptor_size; i++) {
234 printf(" 0x%02X", report_descriptor[i]);
235 if (((i > 0) && (((i+1) % 10) == 0))
236 || (i + 1 == report_descriptor_size)) {
237 printf("\n");
238 }
239 }
240
241 kb_init(&status);
242
243
244 int rc = usbvirt_device_plug(&keyboard_dev, "/virt/usbhc/hc");
245 if (rc != EOK) {
246 printf("%s: Unable to start communication with VHCD (%s).\n",
247 NAME, str_error(rc));
248 return rc;
249 }
250
251 printf("%s: Simulating keyboard events...\n", NAME);
252 fibril_sleep(10);
253 //while (1) {
254 kb_process_events(&status, keyboard_events, keyboard_events_count,
255 on_keyboard_change);
256 //}
257
258 printf("%s: Terminating...\n", NAME);
259
260 //usbvirt_disconnect(&keyboard_dev);
261
262 return 0;
263}
264
265
266/** @}
267 */
Note: See TracBrowser for help on using the repository browser.