1 | /*
|
---|
2 | * Copyright (c) 2011 Lubos Slovak
|
---|
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 drvusbhid
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * USB HID keyboard device structure and API.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <errno.h>
|
---|
38 | #include <str_error.h>
|
---|
39 | #include <stdio.h>
|
---|
40 |
|
---|
41 | #include <io/keycode.h>
|
---|
42 | #include <io/console.h>
|
---|
43 | #include <abi/ipc/methods.h>
|
---|
44 | #include <ipc/kbdev.h>
|
---|
45 | #include <async.h>
|
---|
46 | #include <fibril.h>
|
---|
47 | #include <fibril_synch.h>
|
---|
48 |
|
---|
49 | #include <ddf/log.h>
|
---|
50 |
|
---|
51 | #include <usb/usb.h>
|
---|
52 | #include <usb/dev/dp.h>
|
---|
53 | #include <usb/dev/request.h>
|
---|
54 | #include <usb/hid/hid.h>
|
---|
55 | #include <usb/dev/pipes.h>
|
---|
56 | #include <usb/debug.h>
|
---|
57 | #include <usb/hid/hidparser.h>
|
---|
58 | #include <usb/classes/classes.h>
|
---|
59 | #include <usb/hid/usages/core.h>
|
---|
60 | #include <usb/hid/request.h>
|
---|
61 | #include <usb/hid/hidreport.h>
|
---|
62 | #include <usb/hid/usages/led.h>
|
---|
63 |
|
---|
64 | #include <usb/dev/driver.h>
|
---|
65 |
|
---|
66 | #include "kbddev.h"
|
---|
67 |
|
---|
68 | #include "conv.h"
|
---|
69 | #include "kbdrepeat.h"
|
---|
70 |
|
---|
71 | #include "../usbhid.h"
|
---|
72 |
|
---|
73 | static void default_connection_handler(ddf_fun_t *, ipc_callid_t, ipc_call_t *);
|
---|
74 | static ddf_dev_ops_t kbdops = { .default_handler = default_connection_handler };
|
---|
75 |
|
---|
76 |
|
---|
77 | static const unsigned DEFAULT_ACTIVE_MODS = KM_NUM_LOCK;
|
---|
78 |
|
---|
79 | static const uint8_t ERROR_ROLLOVER = 1;
|
---|
80 |
|
---|
81 | /** Default idle rate for keyboards. */
|
---|
82 | static const uint8_t IDLE_RATE = 0;
|
---|
83 |
|
---|
84 | /** Delay before a pressed key starts auto-repeating. */
|
---|
85 | static const unsigned int DEFAULT_DELAY_BEFORE_FIRST_REPEAT = 500 * 1000;
|
---|
86 |
|
---|
87 | /** Delay between two repeats of a pressed key when auto-repeating. */
|
---|
88 | static const unsigned int DEFAULT_REPEAT_DELAY = 50 * 1000;
|
---|
89 |
|
---|
90 |
|
---|
91 | /** Keyboard polling endpoint description for boot protocol class. */
|
---|
92 | const usb_endpoint_description_t usb_hid_kbd_poll_endpoint_description = {
|
---|
93 | .transfer_type = USB_TRANSFER_INTERRUPT,
|
---|
94 | .direction = USB_DIRECTION_IN,
|
---|
95 | .interface_class = USB_CLASS_HID,
|
---|
96 | .interface_subclass = USB_HID_SUBCLASS_BOOT,
|
---|
97 | .interface_protocol = USB_HID_PROTOCOL_KEYBOARD,
|
---|
98 | .flags = 0
|
---|
99 | };
|
---|
100 |
|
---|
101 | const char *HID_KBD_FUN_NAME = "keyboard";
|
---|
102 | const char *HID_KBD_CATEGORY_NAME = "keyboard";
|
---|
103 |
|
---|
104 | static void usb_kbd_set_led(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev);
|
---|
105 |
|
---|
106 | static const uint8_t USB_KBD_BOOT_REPORT_DESCRIPTOR[] = {
|
---|
107 | 0x05, 0x01, /* Usage Page (Generic Desktop), */
|
---|
108 | 0x09, 0x06, /* Usage (Keyboard), */
|
---|
109 | 0xA1, 0x01, /* Collection (Application), */
|
---|
110 | 0x75, 0x01, /* Report Size (1), */
|
---|
111 | 0x95, 0x08, /* Report Count (8), */
|
---|
112 | 0x05, 0x07, /* Usage Page (Key Codes); */
|
---|
113 | 0x19, 0xE0, /* Usage Minimum (224), */
|
---|
114 | 0x29, 0xE7, /* Usage Maximum (231), */
|
---|
115 | 0x15, 0x00, /* Logical Minimum (0), */
|
---|
116 | 0x25, 0x01, /* Logical Maximum (1), */
|
---|
117 | 0x81, 0x02, /* Input (Data, Variable, Absolute), ; Modifier byte */
|
---|
118 | 0x95, 0x01, /* Report Count (1), */
|
---|
119 | 0x75, 0x08, /* Report Size (8), */
|
---|
120 | 0x81, 0x01, /* Input (Constant), ; Reserved byte */
|
---|
121 | 0x95, 0x05, /* Report Count (5), */
|
---|
122 | 0x75, 0x01, /* Report Size (1), */
|
---|
123 | 0x05, 0x08, /* Usage Page (Page# for LEDs), */
|
---|
124 | 0x19, 0x01, /* Usage Minimum (1), */
|
---|
125 | 0x29, 0x05, /* Usage Maxmimum (5), */
|
---|
126 | 0x91, 0x02, /* Output (Data, Variable, Absolute), ; LED report */
|
---|
127 | 0x95, 0x01, /* Report Count (1), */
|
---|
128 | 0x75, 0x03, /* Report Size (3), */
|
---|
129 | 0x91, 0x01, /* Output (Constant), ; LED report padding */
|
---|
130 | 0x95, 0x06, /* Report Count (6), */
|
---|
131 | 0x75, 0x08, /* Report Size (8), */
|
---|
132 | 0x15, 0x00, /* Logical Minimum (0), */
|
---|
133 | 0x25, 0xff, /* Logical Maximum (255), */
|
---|
134 | 0x05, 0x07, /* Usage Page (Key Codes), */
|
---|
135 | 0x19, 0x00, /* Usage Minimum (0), */
|
---|
136 | 0x29, 0xff, /* Usage Maximum (255), */
|
---|
137 | 0x81, 0x00, /* Input (Data, Array), ; Key arrays (6 bytes) */
|
---|
138 | 0xC0 /* End Collection */
|
---|
139 | };
|
---|
140 |
|
---|
141 | typedef enum usb_kbd_flags {
|
---|
142 | USB_KBD_STATUS_UNINITIALIZED = 0,
|
---|
143 | USB_KBD_STATUS_INITIALIZED = 1,
|
---|
144 | USB_KBD_STATUS_TO_DESTROY = -1
|
---|
145 | } usb_kbd_flags;
|
---|
146 |
|
---|
147 | /* IPC method handler */
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Default handler for IPC methods not handled by DDF.
|
---|
151 | *
|
---|
152 | * Currently recognizes only two methods (IPC_M_CONNECT_TO_ME and KBDEV_SET_IND)
|
---|
153 | * IPC_M_CONNECT_TO_ME assumes the caller is the console and stores IPC
|
---|
154 | * session to it for later use by the driver to notify about key events.
|
---|
155 | * KBDEV_SET_IND sets LED keyboard indicators.
|
---|
156 | *
|
---|
157 | * @param fun Device function handling the call.
|
---|
158 | * @param icallid Call id.
|
---|
159 | * @param icall Call data.
|
---|
160 | */
|
---|
161 | static void default_connection_handler(ddf_fun_t *fun,
|
---|
162 | ipc_callid_t icallid, ipc_call_t *icall)
|
---|
163 | {
|
---|
164 | const sysarg_t method = IPC_GET_IMETHOD(*icall);
|
---|
165 | usb_kbd_t *kbd_dev = ddf_fun_data_get(fun);
|
---|
166 |
|
---|
167 | switch (method) {
|
---|
168 | case KBDEV_SET_IND:
|
---|
169 | kbd_dev->mods = IPC_GET_ARG1(*icall);
|
---|
170 | usb_kbd_set_led(kbd_dev->hid_dev, kbd_dev);
|
---|
171 | async_answer_0(icallid, EOK);
|
---|
172 | break;
|
---|
173 | /* This might be ugly but async_callback_receive_start makes no
|
---|
174 | * difference for incorrect call and malloc failure. */
|
---|
175 | case IPC_M_CONNECT_TO_ME: {
|
---|
176 | async_sess_t *sess =
|
---|
177 | async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
|
---|
178 | /* Probably ENOMEM error, try again. */
|
---|
179 | if (sess == NULL) {
|
---|
180 | usb_log_warning(
|
---|
181 | "Failed to create start console session.\n");
|
---|
182 | async_answer_0(icallid, EAGAIN);
|
---|
183 | break;
|
---|
184 | }
|
---|
185 | if (kbd_dev->client_sess == NULL) {
|
---|
186 | kbd_dev->client_sess = sess;
|
---|
187 | usb_log_debug("%s: OK\n", __FUNCTION__);
|
---|
188 | async_answer_0(icallid, EOK);
|
---|
189 | } else {
|
---|
190 | usb_log_error("%s: console session already set\n",
|
---|
191 | __FUNCTION__);
|
---|
192 | async_answer_0(icallid, ELIMIT);
|
---|
193 | }
|
---|
194 | break;
|
---|
195 | }
|
---|
196 | default:
|
---|
197 | usb_log_error("%s: Unknown method: %d.\n",
|
---|
198 | __FUNCTION__, (int) method);
|
---|
199 | async_answer_0(icallid, EINVAL);
|
---|
200 | break;
|
---|
201 | }
|
---|
202 |
|
---|
203 | }
|
---|
204 |
|
---|
205 | /* Key processing functions */
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Handles turning of LED lights on and off.
|
---|
209 | *
|
---|
210 | * As with most other keyboards, the LED indicators in USB keyboards are
|
---|
211 | * driven by software. When state of some modifier changes, the input server
|
---|
212 | * will call us and tell us to update the LED state and what the new state
|
---|
213 | * should be.
|
---|
214 | *
|
---|
215 | * This functions sets the LED lights according to current settings of modifiers
|
---|
216 | * kept in the keyboard device structure.
|
---|
217 | *
|
---|
218 | * @param kbd_dev Keyboard device structure.
|
---|
219 | */
|
---|
220 | static void usb_kbd_set_led(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev)
|
---|
221 | {
|
---|
222 | if (kbd_dev->output_size == 0) {
|
---|
223 | return;
|
---|
224 | }
|
---|
225 |
|
---|
226 | /* Reset the LED data. */
|
---|
227 | memset(kbd_dev->led_data, 0, kbd_dev->led_output_size * sizeof(int32_t));
|
---|
228 | usb_log_debug("Creating output report:\n");
|
---|
229 |
|
---|
230 | usb_hid_report_field_t *field = usb_hid_report_get_sibling(
|
---|
231 | &hid_dev->report, NULL, kbd_dev->led_path,
|
---|
232 | USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
|
---|
233 | USB_HID_REPORT_TYPE_OUTPUT);
|
---|
234 |
|
---|
235 | while (field != NULL) {
|
---|
236 |
|
---|
237 | if ((field->usage == USB_HID_LED_NUM_LOCK)
|
---|
238 | && (kbd_dev->mods & KM_NUM_LOCK)){
|
---|
239 | field->value = 1;
|
---|
240 | }
|
---|
241 |
|
---|
242 | if ((field->usage == USB_HID_LED_CAPS_LOCK)
|
---|
243 | && (kbd_dev->mods & KM_CAPS_LOCK)){
|
---|
244 | field->value = 1;
|
---|
245 | }
|
---|
246 |
|
---|
247 | if ((field->usage == USB_HID_LED_SCROLL_LOCK)
|
---|
248 | && (kbd_dev->mods & KM_SCROLL_LOCK)){
|
---|
249 | field->value = 1;
|
---|
250 | }
|
---|
251 |
|
---|
252 | field = usb_hid_report_get_sibling(
|
---|
253 | &hid_dev->report, field, kbd_dev->led_path,
|
---|
254 | USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
|
---|
255 | USB_HID_REPORT_TYPE_OUTPUT);
|
---|
256 | }
|
---|
257 |
|
---|
258 | // TODO: what about the Report ID?
|
---|
259 | int rc = usb_hid_report_output_translate(&hid_dev->report, 0,
|
---|
260 | kbd_dev->output_buffer, kbd_dev->output_size);
|
---|
261 |
|
---|
262 | if (rc != EOK) {
|
---|
263 | usb_log_warning("Could not translate LED output to output"
|
---|
264 | "report.\n");
|
---|
265 | return;
|
---|
266 | }
|
---|
267 |
|
---|
268 | usb_log_debug("Output report buffer: %s\n",
|
---|
269 | usb_debug_str_buffer(kbd_dev->output_buffer, kbd_dev->output_size,
|
---|
270 | 0));
|
---|
271 |
|
---|
272 | rc = usbhid_req_set_report(
|
---|
273 | usb_device_get_default_pipe(hid_dev->usb_dev),
|
---|
274 | usb_device_get_iface_number(hid_dev->usb_dev),
|
---|
275 | USB_HID_REPORT_TYPE_OUTPUT,
|
---|
276 | kbd_dev->output_buffer, kbd_dev->output_size);
|
---|
277 | if (rc != EOK) {
|
---|
278 | usb_log_warning("Failed to set kbd indicators.\n");
|
---|
279 | }
|
---|
280 | }
|
---|
281 |
|
---|
282 | /** Send key event.
|
---|
283 | *
|
---|
284 | * @param kbd_dev Keyboard device structure.
|
---|
285 | * @param type Type of the event (press / release). Recognized values:
|
---|
286 | * KEY_PRESS, KEY_RELEASE
|
---|
287 | * @param key Key code
|
---|
288 | */
|
---|
289 | void usb_kbd_push_ev(usb_kbd_t *kbd_dev, int type, unsigned key)
|
---|
290 | {
|
---|
291 | usb_log_debug2("Sending kbdev event %d/%d to the console\n", type, key);
|
---|
292 | if (kbd_dev->client_sess == NULL) {
|
---|
293 | usb_log_warning(
|
---|
294 | "Connection to console not ready, key discarded.\n");
|
---|
295 | return;
|
---|
296 | }
|
---|
297 |
|
---|
298 | async_exch_t *exch = async_exchange_begin(kbd_dev->client_sess);
|
---|
299 | if (exch != NULL) {
|
---|
300 | async_msg_2(exch, KBDEV_EVENT, type, key);
|
---|
301 | async_exchange_end(exch);
|
---|
302 | } else {
|
---|
303 | usb_log_warning("Failed to send key to console.\n");
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | static inline int usb_kbd_is_lock(unsigned int key_code)
|
---|
308 | {
|
---|
309 | return (key_code == KC_NUM_LOCK
|
---|
310 | || key_code == KC_SCROLL_LOCK
|
---|
311 | || key_code == KC_CAPS_LOCK);
|
---|
312 | }
|
---|
313 |
|
---|
314 | static size_t find_in_array_int32(int32_t val, int32_t *arr, size_t arr_size)
|
---|
315 | {
|
---|
316 | for (size_t i = 0; i < arr_size; i++) {
|
---|
317 | if (arr[i] == val) {
|
---|
318 | return i;
|
---|
319 | }
|
---|
320 | }
|
---|
321 |
|
---|
322 | return (size_t) -1;
|
---|
323 | }
|
---|
324 |
|
---|
325 | /**
|
---|
326 | * Checks if some keys were pressed or released and generates key events.
|
---|
327 | *
|
---|
328 | * An event is created only when key is pressed or released. Besides handling
|
---|
329 | * the events (usb_kbd_push_ev()), the auto-repeat fibril is notified about
|
---|
330 | * key presses and releases (see usb_kbd_repeat_start() and
|
---|
331 | * usb_kbd_repeat_stop()).
|
---|
332 | *
|
---|
333 | * @param kbd_dev Keyboard device structure.
|
---|
334 | * @param key_codes Parsed keyboard report - codes of currently pressed keys
|
---|
335 | * according to HID Usage Tables.
|
---|
336 | * @param count Number of key codes in report (size of the report).
|
---|
337 | *
|
---|
338 | * @sa usb_kbd_push_ev(), usb_kbd_repeat_start(), usb_kbd_repeat_stop()
|
---|
339 | */
|
---|
340 | static void usb_kbd_check_key_changes(usb_hid_dev_t *hid_dev,
|
---|
341 | usb_kbd_t *kbd_dev)
|
---|
342 | {
|
---|
343 |
|
---|
344 | /*
|
---|
345 | * First of all, check if the kbd have reported phantom state.
|
---|
346 | *
|
---|
347 | * As there is no way to distinguish keys from modifiers, we do not have
|
---|
348 | * a way to check that 'all keys report Error Rollover'. We thus check
|
---|
349 | * if there is at least one such error and in such case we ignore the
|
---|
350 | * whole input report.
|
---|
351 | */
|
---|
352 | size_t i = find_in_array_int32(ERROR_ROLLOVER, kbd_dev->keys,
|
---|
353 | kbd_dev->key_count);
|
---|
354 | if (i != (size_t) -1) {
|
---|
355 | usb_log_error("Detected phantom state.\n");
|
---|
356 | return;
|
---|
357 | }
|
---|
358 |
|
---|
359 | /*
|
---|
360 | * Key releases
|
---|
361 | */
|
---|
362 | for (i = 0; i < kbd_dev->key_count; i++) {
|
---|
363 | const int32_t old_key = kbd_dev->keys_old[i];
|
---|
364 | /* Find the old key among currently pressed keys. */
|
---|
365 | const size_t pos = find_in_array_int32(old_key, kbd_dev->keys,
|
---|
366 | kbd_dev->key_count);
|
---|
367 | /* If the key was not found, we need to signal release. */
|
---|
368 | if (pos == (size_t) -1) {
|
---|
369 | const unsigned key = usbhid_parse_scancode(old_key);
|
---|
370 | if (!usb_kbd_is_lock(key)) {
|
---|
371 | usb_kbd_repeat_stop(kbd_dev, key);
|
---|
372 | }
|
---|
373 | usb_kbd_push_ev(kbd_dev, KEY_RELEASE, key);
|
---|
374 | usb_log_debug2("Key released: %u "
|
---|
375 | "(USB code %" PRIu32 ")\n", key, old_key);
|
---|
376 | }
|
---|
377 | }
|
---|
378 |
|
---|
379 | /*
|
---|
380 | * Key presses
|
---|
381 | */
|
---|
382 | for (i = 0; i < kbd_dev->key_count; ++i) {
|
---|
383 | const int32_t new_key = kbd_dev->keys[i];
|
---|
384 | /* Find the new key among already pressed keys. */
|
---|
385 | const size_t pos = find_in_array_int32(new_key,
|
---|
386 | kbd_dev->keys_old, kbd_dev->key_count);
|
---|
387 | /* If the key was not found, we need to signal press. */
|
---|
388 | if (pos == (size_t) -1) {
|
---|
389 | unsigned key = usbhid_parse_scancode(kbd_dev->keys[i]);
|
---|
390 | if (!usb_kbd_is_lock(key)) {
|
---|
391 | usb_kbd_repeat_start(kbd_dev, key);
|
---|
392 | }
|
---|
393 | usb_kbd_push_ev(kbd_dev, KEY_PRESS, key);
|
---|
394 | usb_log_debug2("Key pressed: %u "
|
---|
395 | "(USB code %" PRIu32 ")\n", key, new_key);
|
---|
396 | }
|
---|
397 | }
|
---|
398 |
|
---|
399 | memcpy(kbd_dev->keys_old, kbd_dev->keys, kbd_dev->key_count * 4);
|
---|
400 |
|
---|
401 | // TODO Get rid of this
|
---|
402 | char key_buffer[512];
|
---|
403 | ddf_dump_buffer(key_buffer, 512,
|
---|
404 | kbd_dev->keys_old, 4, kbd_dev->key_count, 0);
|
---|
405 | usb_log_debug2("Stored keys %s.\n", key_buffer);
|
---|
406 | }
|
---|
407 |
|
---|
408 | /* General kbd functions */
|
---|
409 |
|
---|
410 | /**
|
---|
411 | * Processes data received from the device in form of report.
|
---|
412 | *
|
---|
413 | * This function uses the HID report parser to translate the data received from
|
---|
414 | * the device into generic USB HID key codes and into generic modifiers bitmap.
|
---|
415 | * The parser then calls the given callback (usb_kbd_process_keycodes()).
|
---|
416 | *
|
---|
417 | * @note Currently, only the boot protocol is supported.
|
---|
418 | *
|
---|
419 | * @param kbd_dev Keyboard device structure (must be initialized).
|
---|
420 | * @param buffer Data from the keyboard (i.e. the report).
|
---|
421 | * @param actual_size Size of the data from keyboard (report size) in bytes.
|
---|
422 | *
|
---|
423 | * @sa usb_kbd_process_keycodes(), usb_hid_boot_keyboard_input_report(),
|
---|
424 | * usb_hid_parse_report().
|
---|
425 | */
|
---|
426 | static void usb_kbd_process_data(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev)
|
---|
427 | {
|
---|
428 | assert(hid_dev != NULL);
|
---|
429 | assert(kbd_dev != NULL);
|
---|
430 |
|
---|
431 | usb_hid_report_path_t *path = usb_hid_report_path();
|
---|
432 | if (path == NULL) {
|
---|
433 | usb_log_error("Failed to create hid/kbd report path.\n");
|
---|
434 | return;
|
---|
435 | }
|
---|
436 |
|
---|
437 | int ret =
|
---|
438 | usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_KEYBOARD, 0);
|
---|
439 | if (ret != EOK) {
|
---|
440 | usb_log_error("Failed to append to hid/kbd report path.\n");
|
---|
441 | return;
|
---|
442 | }
|
---|
443 |
|
---|
444 | usb_hid_report_path_set_report_id(path, hid_dev->report_id);
|
---|
445 |
|
---|
446 | /* Fill in the currently pressed keys. */
|
---|
447 | usb_hid_report_field_t *field = usb_hid_report_get_sibling(
|
---|
448 | &hid_dev->report, NULL, path,
|
---|
449 | USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
|
---|
450 | USB_HID_REPORT_TYPE_INPUT);
|
---|
451 | unsigned i = 0;
|
---|
452 |
|
---|
453 | while (field != NULL) {
|
---|
454 | usb_log_debug2("FIELD (%p) - VALUE(%d) USAGE(%u)\n",
|
---|
455 | field, field->value, field->usage);
|
---|
456 |
|
---|
457 | assert(i < kbd_dev->key_count);
|
---|
458 |
|
---|
459 | /* Save the key usage. */
|
---|
460 | if (field->value != 0) {
|
---|
461 | kbd_dev->keys[i] = field->usage;
|
---|
462 | }
|
---|
463 | else {
|
---|
464 | kbd_dev->keys[i] = 0;
|
---|
465 | }
|
---|
466 | usb_log_debug2("Saved %u. key usage %d\n", i, kbd_dev->keys[i]);
|
---|
467 |
|
---|
468 | ++i;
|
---|
469 | field = usb_hid_report_get_sibling(
|
---|
470 | &hid_dev->report, field, path, USB_HID_PATH_COMPARE_END
|
---|
471 | | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
|
---|
472 | USB_HID_REPORT_TYPE_INPUT);
|
---|
473 | }
|
---|
474 |
|
---|
475 | usb_hid_report_path_free(path);
|
---|
476 |
|
---|
477 | usb_kbd_check_key_changes(hid_dev, kbd_dev);
|
---|
478 | }
|
---|
479 |
|
---|
480 | /* HID/KBD structure manipulation */
|
---|
481 |
|
---|
482 | static int kbd_dev_init(usb_kbd_t *kbd_dev, usb_hid_dev_t *hid_dev)
|
---|
483 | {
|
---|
484 | assert(kbd_dev);
|
---|
485 | assert(hid_dev);
|
---|
486 |
|
---|
487 | /* Default values */
|
---|
488 | fibril_mutex_initialize(&kbd_dev->repeat_mtx);
|
---|
489 | kbd_dev->initialized = USB_KBD_STATUS_UNINITIALIZED;
|
---|
490 |
|
---|
491 | /* Store link to HID device */
|
---|
492 | kbd_dev->hid_dev = hid_dev;
|
---|
493 |
|
---|
494 | /* Modifiers and locks */
|
---|
495 | kbd_dev->mods = DEFAULT_ACTIVE_MODS;
|
---|
496 |
|
---|
497 | /* Autorepeat */
|
---|
498 | kbd_dev->repeat.delay_before = DEFAULT_DELAY_BEFORE_FIRST_REPEAT;
|
---|
499 | kbd_dev->repeat.delay_between = DEFAULT_REPEAT_DELAY;
|
---|
500 |
|
---|
501 | // TODO: make more general
|
---|
502 | usb_hid_report_path_t *path = usb_hid_report_path();
|
---|
503 | if (path == NULL) {
|
---|
504 | usb_log_error("Failed to create kbd report path.\n");
|
---|
505 | usb_kbd_destroy(kbd_dev);
|
---|
506 | return ENOMEM;
|
---|
507 | }
|
---|
508 |
|
---|
509 | int ret =
|
---|
510 | usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_KEYBOARD, 0);
|
---|
511 | if (ret != EOK) {
|
---|
512 | usb_log_error("Failed to append item to kbd report path.\n");
|
---|
513 | usb_hid_report_path_free(path);
|
---|
514 | usb_kbd_destroy(kbd_dev);
|
---|
515 | return ret;
|
---|
516 | }
|
---|
517 |
|
---|
518 | usb_hid_report_path_set_report_id(path, 0);
|
---|
519 |
|
---|
520 | kbd_dev->key_count =
|
---|
521 | usb_hid_report_size(&hid_dev->report, 0, USB_HID_REPORT_TYPE_INPUT);
|
---|
522 |
|
---|
523 | usb_hid_report_path_free(path);
|
---|
524 |
|
---|
525 | usb_log_debug("Size of the input report: %zu\n", kbd_dev->key_count);
|
---|
526 |
|
---|
527 | kbd_dev->keys = calloc(kbd_dev->key_count, sizeof(int32_t));
|
---|
528 | if (kbd_dev->keys == NULL) {
|
---|
529 | usb_log_error("Failed to allocate key buffer.\n");
|
---|
530 | usb_kbd_destroy(kbd_dev);
|
---|
531 | return ENOMEM;
|
---|
532 | }
|
---|
533 |
|
---|
534 | kbd_dev->keys_old = calloc(kbd_dev->key_count, sizeof(int32_t));
|
---|
535 | if (kbd_dev->keys_old == NULL) {
|
---|
536 | usb_log_error("Failed to allocate old_key buffer.\n");
|
---|
537 | usb_kbd_destroy(kbd_dev);
|
---|
538 | return ENOMEM;
|
---|
539 | }
|
---|
540 |
|
---|
541 | /* Output report */
|
---|
542 | kbd_dev->output_size = 0;
|
---|
543 | kbd_dev->output_buffer = usb_hid_report_output(&hid_dev->report,
|
---|
544 | &kbd_dev->output_size, 0);
|
---|
545 | if (kbd_dev->output_buffer == NULL) {
|
---|
546 | usb_log_error("Error creating output report buffer.\n");
|
---|
547 | usb_kbd_destroy(kbd_dev);
|
---|
548 | return ENOMEM;
|
---|
549 | }
|
---|
550 |
|
---|
551 | usb_log_debug("Output buffer size: %zu\n", kbd_dev->output_size);
|
---|
552 |
|
---|
553 | kbd_dev->led_path = usb_hid_report_path();
|
---|
554 | if (kbd_dev->led_path == NULL) {
|
---|
555 | usb_log_error("Failed to create kbd led report path.\n");
|
---|
556 | usb_kbd_destroy(kbd_dev);
|
---|
557 | return ENOMEM;
|
---|
558 | }
|
---|
559 |
|
---|
560 | ret = usb_hid_report_path_append_item(
|
---|
561 | kbd_dev->led_path, USB_HIDUT_PAGE_LED, 0);
|
---|
562 | if (ret != EOK) {
|
---|
563 | usb_log_error("Failed to append to kbd/led report path.\n");
|
---|
564 | usb_kbd_destroy(kbd_dev);
|
---|
565 | return ret;
|
---|
566 | }
|
---|
567 |
|
---|
568 | kbd_dev->led_output_size = usb_hid_report_size(
|
---|
569 | &hid_dev->report, 0, USB_HID_REPORT_TYPE_OUTPUT);
|
---|
570 |
|
---|
571 | usb_log_debug("Output report size (in items): %zu\n",
|
---|
572 | kbd_dev->led_output_size);
|
---|
573 |
|
---|
574 | kbd_dev->led_data = calloc(kbd_dev->led_output_size, sizeof(int32_t));
|
---|
575 | if (kbd_dev->led_data == NULL) {
|
---|
576 | usb_log_error("Error creating buffer for LED output report.\n");
|
---|
577 | usb_kbd_destroy(kbd_dev);
|
---|
578 | return ENOMEM;
|
---|
579 | }
|
---|
580 |
|
---|
581 | /* Set LEDs according to initial setup.
|
---|
582 | * Set Idle rate */
|
---|
583 | usb_kbd_set_led(hid_dev, kbd_dev);
|
---|
584 |
|
---|
585 | usbhid_req_set_idle(usb_device_get_default_pipe(hid_dev->usb_dev),
|
---|
586 | usb_device_get_iface_number(hid_dev->usb_dev), IDLE_RATE);
|
---|
587 |
|
---|
588 |
|
---|
589 | kbd_dev->initialized = USB_KBD_STATUS_INITIALIZED;
|
---|
590 | usb_log_debug("HID/KBD device structure initialized.\n");
|
---|
591 |
|
---|
592 | return EOK;
|
---|
593 | }
|
---|
594 |
|
---|
595 |
|
---|
596 | /* API functions */
|
---|
597 |
|
---|
598 | /**
|
---|
599 | * Initialization of the USB/HID keyboard structure.
|
---|
600 | *
|
---|
601 | * This functions initializes required structures from the device's descriptors.
|
---|
602 | *
|
---|
603 | * During initialization, the keyboard is switched into boot protocol, the idle
|
---|
604 | * rate is set to 0 (infinity), resulting in the keyboard only reporting event
|
---|
605 | * when a key is pressed or released. Finally, the LED lights are turned on
|
---|
606 | * according to the default setup of lock keys.
|
---|
607 | *
|
---|
608 | * @note By default, the keyboards is initialized with Num Lock turned on and
|
---|
609 | * other locks turned off.
|
---|
610 | *
|
---|
611 | * @param kbd_dev Keyboard device structure to be initialized.
|
---|
612 | * @param dev DDF device structure of the keyboard.
|
---|
613 | *
|
---|
614 | * @retval EOK if successful.
|
---|
615 | * @retval EINVAL if some parameter is not given.
|
---|
616 | * @return Other value inherited from function usbhid_dev_init().
|
---|
617 | */
|
---|
618 | int usb_kbd_init(usb_hid_dev_t *hid_dev, void **data)
|
---|
619 | {
|
---|
620 | usb_log_debug("Initializing HID/KBD structure...\n");
|
---|
621 |
|
---|
622 | if (hid_dev == NULL) {
|
---|
623 | usb_log_error(
|
---|
624 | "Failed to init keyboard structure: no structure given.\n");
|
---|
625 | return EINVAL;
|
---|
626 | }
|
---|
627 |
|
---|
628 | /* Create the exposed function. */
|
---|
629 | usb_log_debug("Creating DDF function %s...\n", HID_KBD_FUN_NAME);
|
---|
630 | ddf_fun_t *fun = usb_device_ddf_fun_create(hid_dev->usb_dev,
|
---|
631 | fun_exposed, HID_KBD_FUN_NAME);
|
---|
632 | if (fun == NULL) {
|
---|
633 | usb_log_error("Could not create DDF function node.\n");
|
---|
634 | return ENOMEM;
|
---|
635 | }
|
---|
636 |
|
---|
637 | usb_kbd_t *kbd_dev = ddf_fun_data_alloc(fun, sizeof(usb_kbd_t));
|
---|
638 | if (kbd_dev == NULL) {
|
---|
639 | usb_log_error("Failed to allocate KBD device structure.\n");
|
---|
640 | ddf_fun_destroy(fun);
|
---|
641 | return ENOMEM;
|
---|
642 | }
|
---|
643 |
|
---|
644 | int ret = kbd_dev_init(kbd_dev, hid_dev);
|
---|
645 | if (ret != EOK) {
|
---|
646 | usb_log_error("Failed to initialize KBD device structure.\n");
|
---|
647 | ddf_fun_destroy(fun);
|
---|
648 | return ret;
|
---|
649 | }
|
---|
650 |
|
---|
651 | /* Store the initialized HID device and HID ops
|
---|
652 | * to the DDF function. */
|
---|
653 | ddf_fun_set_ops(fun, &kbdops);
|
---|
654 |
|
---|
655 | ret = ddf_fun_bind(fun);
|
---|
656 | if (ret != EOK) {
|
---|
657 | usb_log_error("Could not bind DDF function: %s.\n",
|
---|
658 | str_error(ret));
|
---|
659 | usb_kbd_destroy(kbd_dev);
|
---|
660 | ddf_fun_destroy(fun);
|
---|
661 | return ret;
|
---|
662 | }
|
---|
663 |
|
---|
664 | usb_log_debug("%s function created. Handle: %" PRIun "\n",
|
---|
665 | HID_KBD_FUN_NAME, ddf_fun_get_handle(fun));
|
---|
666 |
|
---|
667 | usb_log_debug("Adding DDF function to category %s...\n",
|
---|
668 | HID_KBD_CATEGORY_NAME);
|
---|
669 | ret = ddf_fun_add_to_category(fun, HID_KBD_CATEGORY_NAME);
|
---|
670 | if (ret != EOK) {
|
---|
671 | usb_log_error(
|
---|
672 | "Could not add DDF function to category %s: %s.\n",
|
---|
673 | HID_KBD_CATEGORY_NAME, str_error(ret));
|
---|
674 | usb_kbd_destroy(kbd_dev);
|
---|
675 | if (ddf_fun_unbind(fun) == EOK) {
|
---|
676 | ddf_fun_destroy(fun);
|
---|
677 | } else {
|
---|
678 | usb_log_error(
|
---|
679 | "Failed to unbind `%s', will not destroy.\n",
|
---|
680 | ddf_fun_get_name(fun));
|
---|
681 | }
|
---|
682 | return ret;
|
---|
683 | }
|
---|
684 |
|
---|
685 | /* Create new fibril for auto-repeat. */
|
---|
686 | fid_t fid = fibril_create(usb_kbd_repeat_fibril, kbd_dev);
|
---|
687 | if (fid == 0) {
|
---|
688 | usb_log_error("Failed to start fibril for KBD auto-repeat");
|
---|
689 | usb_kbd_destroy(kbd_dev);
|
---|
690 | return ENOMEM;
|
---|
691 | }
|
---|
692 | fibril_add_ready(fid);
|
---|
693 | kbd_dev->fun = fun;
|
---|
694 | /* Save the KBD device structure into the HID device structure. */
|
---|
695 | *data = kbd_dev;
|
---|
696 |
|
---|
697 | return EOK;
|
---|
698 | }
|
---|
699 |
|
---|
700 | bool usb_kbd_polling_callback(usb_hid_dev_t *hid_dev, void *data)
|
---|
701 | {
|
---|
702 | if (hid_dev == NULL || data == NULL) {
|
---|
703 | /* This means something serious */
|
---|
704 | return false;
|
---|
705 | }
|
---|
706 |
|
---|
707 | usb_kbd_t *kbd_dev = data;
|
---|
708 | // TODO: add return value from this function
|
---|
709 | usb_kbd_process_data(hid_dev, kbd_dev);
|
---|
710 |
|
---|
711 | return true;
|
---|
712 | }
|
---|
713 |
|
---|
714 | int usb_kbd_is_initialized(const usb_kbd_t *kbd_dev)
|
---|
715 | {
|
---|
716 | return (kbd_dev->initialized == USB_KBD_STATUS_INITIALIZED);
|
---|
717 | }
|
---|
718 |
|
---|
719 | int usb_kbd_is_ready_to_destroy(const usb_kbd_t *kbd_dev)
|
---|
720 | {
|
---|
721 | return (kbd_dev->initialized == USB_KBD_STATUS_TO_DESTROY);
|
---|
722 | }
|
---|
723 |
|
---|
724 | /**
|
---|
725 | * Properly destroys the USB/HID keyboard structure.
|
---|
726 | *
|
---|
727 | * @param kbd_dev Pointer to the structure to be destroyed.
|
---|
728 | */
|
---|
729 | void usb_kbd_destroy(usb_kbd_t *kbd_dev)
|
---|
730 | {
|
---|
731 | if (kbd_dev == NULL) {
|
---|
732 | return;
|
---|
733 | }
|
---|
734 |
|
---|
735 | /* Hangup session to the console. */
|
---|
736 | if (kbd_dev->client_sess)
|
---|
737 | async_hangup(kbd_dev->client_sess);
|
---|
738 |
|
---|
739 | //assert(!fibril_mutex_is_locked((*kbd_dev)->repeat_mtx));
|
---|
740 | // FIXME - the fibril_mutex_is_locked may not cause
|
---|
741 | // fibril scheduling
|
---|
742 | while (fibril_mutex_is_locked(&kbd_dev->repeat_mtx)) {}
|
---|
743 |
|
---|
744 | /* Free all buffers. */
|
---|
745 | free(kbd_dev->keys);
|
---|
746 | free(kbd_dev->keys_old);
|
---|
747 | free(kbd_dev->led_data);
|
---|
748 |
|
---|
749 | usb_hid_report_path_free(kbd_dev->led_path);
|
---|
750 | usb_hid_report_output_free(kbd_dev->output_buffer);
|
---|
751 |
|
---|
752 | if (kbd_dev->fun) {
|
---|
753 | if (ddf_fun_unbind(kbd_dev->fun) != EOK) {
|
---|
754 | usb_log_warning("Failed to unbind %s.\n",
|
---|
755 | ddf_fun_get_name(kbd_dev->fun));
|
---|
756 | } else {
|
---|
757 | usb_log_debug2("%s unbound.\n",
|
---|
758 | ddf_fun_get_name(kbd_dev->fun));
|
---|
759 | ddf_fun_destroy(kbd_dev->fun);
|
---|
760 | }
|
---|
761 | }
|
---|
762 | }
|
---|
763 |
|
---|
764 | void usb_kbd_deinit(usb_hid_dev_t *hid_dev, void *data)
|
---|
765 | {
|
---|
766 | if (data != NULL) {
|
---|
767 | usb_kbd_t *kbd_dev = data;
|
---|
768 | if (usb_kbd_is_initialized(kbd_dev)) {
|
---|
769 | kbd_dev->initialized = USB_KBD_STATUS_TO_DESTROY;
|
---|
770 | /* Wait for autorepeat */
|
---|
771 | async_usleep(CHECK_DELAY);
|
---|
772 | }
|
---|
773 | usb_kbd_destroy(kbd_dev);
|
---|
774 | }
|
---|
775 | }
|
---|
776 |
|
---|
777 | int usb_kbd_set_boot_protocol(usb_hid_dev_t *hid_dev)
|
---|
778 | {
|
---|
779 | assert(hid_dev);
|
---|
780 | int rc = usb_hid_parse_report_descriptor(
|
---|
781 | &hid_dev->report, USB_KBD_BOOT_REPORT_DESCRIPTOR,
|
---|
782 | sizeof(USB_KBD_BOOT_REPORT_DESCRIPTOR));
|
---|
783 |
|
---|
784 | if (rc != EOK) {
|
---|
785 | usb_log_error("Failed to parse boot report descriptor: %s\n",
|
---|
786 | str_error(rc));
|
---|
787 | return rc;
|
---|
788 | }
|
---|
789 |
|
---|
790 | rc = usbhid_req_set_protocol(
|
---|
791 | usb_device_get_default_pipe(hid_dev->usb_dev),
|
---|
792 | usb_device_get_iface_number(hid_dev->usb_dev),
|
---|
793 | USB_HID_PROTOCOL_BOOT);
|
---|
794 |
|
---|
795 | if (rc != EOK) {
|
---|
796 | usb_log_warning("Failed to set boot protocol to the device: "
|
---|
797 | "%s\n", str_error(rc));
|
---|
798 | return rc;
|
---|
799 | }
|
---|
800 |
|
---|
801 | return EOK;
|
---|
802 | }
|
---|
803 | /**
|
---|
804 | * @}
|
---|
805 | */
|
---|