source: mainline/uspace/drv/usbhid/kbd/kbddev.c@ 313775b

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

Nicer buffer dump in USB keyboard subdriver

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