source: mainline/uspace/drv/usbhid/kbd/kbddev.c@ 46b60e6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 46b60e6 was 46b60e6, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Update comments.

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