source: mainline/uspace/drv/hid/usbhid/kbd/kbddev.c@ eb13ef8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since eb13ef8 was eb13ef8, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Change IPC_GET_* and IPC_SET_* to accept pointer instead of lvalue

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