source: mainline/uspace/drv/bus/usb/usbhid/kbd/kbddev.c@ bcf54ac

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bcf54ac was bcf54ac, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

usbhid, keyboard: Do not destroy functions that fail to unbind.

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