source: mainline/uspace/drv/usbhid/kbd/kbddev.c@ 4d3c13e

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

Unused code removal

  • 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 <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 if (!usb_kbd_is_lock(key)) {
400 usb_kbd_repeat_start(kbd_dev, key);
401 }
402 usb_kbd_push_ev(hid_dev, kbd_dev, KEY_PRESS, key);
403 } else {
404 // found, nothing happens
405 }
406 }
407
408 memcpy(kbd_dev->keys_old, kbd_dev->keys, kbd_dev->key_count * 4);
409
410 usb_log_debug2("New stored keys: ");
411 for (i = 0; i < kbd_dev->key_count; ++i) {
412 usb_log_debug2("%d ", kbd_dev->keys_old[i]);
413 }
414 usb_log_debug2("\n");
415}
416
417/*----------------------------------------------------------------------------*/
418/* General kbd functions */
419/*----------------------------------------------------------------------------*/
420/**
421 * Processes data received from the device in form of report.
422 *
423 * This function uses the HID report parser to translate the data received from
424 * the device into generic USB HID key codes and into generic modifiers bitmap.
425 * The parser then calls the given callback (usb_kbd_process_keycodes()).
426 *
427 * @note Currently, only the boot protocol is supported.
428 *
429 * @param kbd_dev Keyboard device structure (must be initialized).
430 * @param buffer Data from the keyboard (i.e. the report).
431 * @param actual_size Size of the data from keyboard (report size) in bytes.
432 *
433 * @sa usb_kbd_process_keycodes(), usb_hid_boot_keyboard_input_report(),
434 * usb_hid_parse_report().
435 */
436static void usb_kbd_process_data(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev)
437{
438 assert(hid_dev->report != NULL);
439 assert(hid_dev != NULL);
440 assert(kbd_dev != NULL);
441
442 usb_hid_report_path_t *path = usb_hid_report_path();
443 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_KEYBOARD, 0);
444
445 usb_hid_report_path_set_report_id (path, hid_dev->report_id);
446
447 // fill in the currently pressed keys
448
449 usb_hid_report_field_t *field = usb_hid_report_get_sibling(
450 hid_dev->report, NULL, path,
451 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
452 USB_HID_REPORT_TYPE_INPUT);
453 unsigned i = 0;
454
455 while (field != NULL) {
456 usb_log_debug2("FIELD (%p) - VALUE(%d) USAGE(%u)\n",
457 field, field->value, field->usage);
458
459 assert(i < kbd_dev->key_count);
460
461 // save the key usage
462 if (field->value != 0) {
463 kbd_dev->keys[i] = field->usage;
464 }
465 else {
466 kbd_dev->keys[i] = 0;
467 }
468 usb_log_debug2("Saved %u. key usage %d\n", i, kbd_dev->keys[i]);
469
470 ++i;
471 field = usb_hid_report_get_sibling(hid_dev->report, field, path,
472 USB_HID_PATH_COMPARE_END
473 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
474 USB_HID_REPORT_TYPE_INPUT);
475 }
476
477 usb_hid_report_path_free(path);
478
479 usb_kbd_check_key_changes(hid_dev, kbd_dev);
480}
481
482/*----------------------------------------------------------------------------*/
483/* HID/KBD structure manipulation */
484/*----------------------------------------------------------------------------*/
485
486static void usb_kbd_mark_unusable(usb_kbd_t *kbd_dev)
487{
488 kbd_dev->initialized = USB_KBD_STATUS_TO_DESTROY;
489}
490
491/*----------------------------------------------------------------------------*/
492
493/**
494 * Creates a new USB/HID keyboard structure.
495 *
496 * The structure returned by this function is not initialized. Use
497 * usb_kbd_init() to initialize it prior to polling.
498 *
499 * @return New uninitialized structure for representing a USB/HID keyboard or
500 * NULL if not successful (memory error).
501 */
502static usb_kbd_t *usb_kbd_new(void)
503{
504 usb_kbd_t *kbd_dev =
505 (usb_kbd_t *)calloc(1, sizeof(usb_kbd_t));
506
507 if (kbd_dev == NULL) {
508 usb_log_fatal("No memory!\n");
509 return NULL;
510 }
511
512 kbd_dev->console_phone = -1;
513 kbd_dev->initialized = USB_KBD_STATUS_UNINITIALIZED;
514
515 return kbd_dev;
516}
517
518/*----------------------------------------------------------------------------*/
519
520static int usb_kbd_create_function(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev)
521{
522 assert(hid_dev != NULL);
523 assert(hid_dev->usb_dev != NULL);
524 assert(kbd_dev != NULL);
525
526 /* Create the function exposed under /dev/devices. */
527 usb_log_debug("Creating DDF function %s...\n", HID_KBD_FUN_NAME);
528 ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed,
529 HID_KBD_FUN_NAME);
530 if (fun == NULL) {
531 usb_log_error("Could not create DDF function node.\n");
532 return ENOMEM;
533 }
534
535 /*
536 * Store the initialized HID device and HID ops
537 * to the DDF function.
538 */
539 fun->ops = &kbd_dev->ops;
540 fun->driver_data = kbd_dev;
541
542 int rc = ddf_fun_bind(fun);
543 if (rc != EOK) {
544 usb_log_error("Could not bind DDF function: %s.\n",
545 str_error(rc));
546 ddf_fun_destroy(fun);
547 return rc;
548 }
549
550 usb_log_debug("%s function created. Handle: %" PRIun "\n",
551 HID_KBD_FUN_NAME, fun->handle);
552
553 usb_log_debug("Adding DDF function to class %s...\n",
554 HID_KBD_CLASS_NAME);
555 rc = ddf_fun_add_to_class(fun, HID_KBD_CLASS_NAME);
556 if (rc != EOK) {
557 usb_log_error(
558 "Could not add DDF function to class %s: %s.\n",
559 HID_KBD_CLASS_NAME, str_error(rc));
560 ddf_fun_destroy(fun);
561 return rc;
562 }
563
564 return EOK;
565}
566
567/*----------------------------------------------------------------------------*/
568/* API functions */
569/*----------------------------------------------------------------------------*/
570/**
571 * Initialization of the USB/HID keyboard structure.
572 *
573 * This functions initializes required structures from the device's descriptors.
574 *
575 * During initialization, the keyboard is switched into boot protocol, the idle
576 * rate is set to 0 (infinity), resulting in the keyboard only reporting event
577 * when a key is pressed or released. Finally, the LED lights are turned on
578 * according to the default setup of lock keys.
579 *
580 * @note By default, the keyboards is initialized with Num Lock turned on and
581 * other locks turned off.
582 *
583 * @param kbd_dev Keyboard device structure to be initialized.
584 * @param dev DDF device structure of the keyboard.
585 *
586 * @retval EOK if successful.
587 * @retval EINVAL if some parameter is not given.
588 * @return Other value inherited from function usbhid_dev_init().
589 */
590int usb_kbd_init(usb_hid_dev_t *hid_dev, void **data)
591{
592 usb_log_debug("Initializing HID/KBD structure...\n");
593
594 if (hid_dev == NULL) {
595 usb_log_error("Failed to init keyboard structure: no structure"
596 " given.\n");
597 return EINVAL;
598 }
599
600 usb_kbd_t *kbd_dev = usb_kbd_new();
601 if (kbd_dev == NULL) {
602 usb_log_error("Error while creating USB/HID KBD device "
603 "structure.\n");
604 return ENOMEM; // TODO: some other code??
605 }
606
607 /* Store link to HID device */
608 kbd_dev->hid_dev = hid_dev;
609
610 /*
611 * TODO: make more general
612 */
613 usb_hid_report_path_t *path = usb_hid_report_path();
614 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_KEYBOARD, 0);
615
616 usb_hid_report_path_set_report_id(path, 0);
617
618 kbd_dev->key_count = usb_hid_report_size(
619 hid_dev->report, 0, USB_HID_REPORT_TYPE_INPUT);
620 usb_hid_report_path_free(path);
621
622 usb_log_debug("Size of the input report: %zu\n", kbd_dev->key_count);
623
624 kbd_dev->keys = (int32_t *)calloc(kbd_dev->key_count, sizeof(int32_t));
625
626 if (kbd_dev->keys == NULL) {
627 usb_log_fatal("No memory!\n");
628 free(kbd_dev);
629 return ENOMEM;
630 }
631
632 kbd_dev->keys_old =
633 (int32_t *)calloc(kbd_dev->key_count, sizeof(int32_t));
634
635 if (kbd_dev->keys_old == NULL) {
636 usb_log_fatal("No memory!\n");
637 free(kbd_dev->keys);
638 free(kbd_dev);
639 return ENOMEM;
640 }
641
642 /*
643 * Output report
644 */
645 kbd_dev->output_size = 0;
646 kbd_dev->output_buffer = usb_hid_report_output(hid_dev->report,
647 &kbd_dev->output_size, 0);
648 if (kbd_dev->output_buffer == NULL) {
649 usb_log_warning("Error creating output report buffer.\n");
650 free(kbd_dev->keys);
651 return ENOMEM;
652 }
653
654 usb_log_debug("Output buffer size: %zu\n", kbd_dev->output_size);
655
656 kbd_dev->led_path = usb_hid_report_path();
657 usb_hid_report_path_append_item(
658 kbd_dev->led_path, USB_HIDUT_PAGE_LED, 0);
659
660 kbd_dev->led_output_size = usb_hid_report_size(hid_dev->report,
661 0, USB_HID_REPORT_TYPE_OUTPUT);
662
663 usb_log_debug("Output report size (in items): %zu\n",
664 kbd_dev->led_output_size);
665
666 kbd_dev->led_data = (int32_t *)calloc(
667 kbd_dev->led_output_size, sizeof(int32_t));
668
669 if (kbd_dev->led_data == NULL) {
670 usb_log_warning("Error creating buffer for LED output report."
671 "\n");
672 free(kbd_dev->keys);
673 usb_hid_report_output_free(kbd_dev->output_buffer);
674 free(kbd_dev);
675 return ENOMEM;
676 }
677
678 /*
679 * Modifiers and locks
680 */
681 kbd_dev->modifiers = 0;
682 kbd_dev->mods = DEFAULT_ACTIVE_MODS;
683 kbd_dev->lock_keys = 0;
684
685 /*
686 * Autorepeat
687 */
688 kbd_dev->repeat.key_new = 0;
689 kbd_dev->repeat.key_repeated = 0;
690 kbd_dev->repeat.delay_before = DEFAULT_DELAY_BEFORE_FIRST_REPEAT;
691 kbd_dev->repeat.delay_between = DEFAULT_REPEAT_DELAY;
692
693 kbd_dev->repeat_mtx = (fibril_mutex_t *)(
694 malloc(sizeof(fibril_mutex_t)));
695 if (kbd_dev->repeat_mtx == NULL) {
696 usb_log_fatal("No memory!\n");
697 free(kbd_dev->keys);
698 usb_hid_report_output_free(kbd_dev->output_buffer);
699 free(kbd_dev);
700 return ENOMEM;
701 }
702
703 fibril_mutex_initialize(kbd_dev->repeat_mtx);
704
705 // save the KBD device structure into the HID device structure
706 *data = kbd_dev;
707
708 // set handler for incoming calls
709 kbd_dev->ops.default_handler = default_connection_handler;
710
711 /*
712 * Set LEDs according to initial setup.
713 * Set Idle rate
714 */
715 usb_kbd_set_led(hid_dev, kbd_dev);
716
717 usbhid_req_set_idle(&hid_dev->usb_dev->ctrl_pipe,
718 hid_dev->usb_dev->interface_no, IDLE_RATE);
719
720 /*
721 * Create new fibril for auto-repeat
722 */
723 fid_t fid = fibril_create(usb_kbd_repeat_fibril, kbd_dev);
724 if (fid == 0) {
725 usb_log_error("Failed to start fibril for KBD auto-repeat");
726 return ENOMEM;
727 }
728 fibril_add_ready(fid);
729
730 kbd_dev->initialized = USB_KBD_STATUS_INITIALIZED;
731 usb_log_debug("HID/KBD device structure initialized.\n");
732
733 usb_log_debug("Creating KBD function...\n");
734 int rc = usb_kbd_create_function(hid_dev, kbd_dev);
735 if (rc != EOK) {
736 usb_kbd_free(&kbd_dev);
737 return rc;
738 }
739
740 return EOK;
741}
742
743/*----------------------------------------------------------------------------*/
744
745bool usb_kbd_polling_callback(usb_hid_dev_t *hid_dev, void *data)
746{
747 if (hid_dev == NULL/* || buffer == NULL*/ || data == NULL) {
748 // do not continue polling (???)
749 return false;
750 }
751
752 usb_kbd_t *kbd_dev = (usb_kbd_t *)data;
753 assert(kbd_dev != NULL);
754
755 // TODO: add return value from this function
756 usb_kbd_process_data(hid_dev, kbd_dev);
757
758 return true;
759}
760
761/*----------------------------------------------------------------------------*/
762
763int usb_kbd_is_initialized(const usb_kbd_t *kbd_dev)
764{
765 return (kbd_dev->initialized == USB_KBD_STATUS_INITIALIZED);
766}
767
768/*----------------------------------------------------------------------------*/
769
770int usb_kbd_is_ready_to_destroy(const usb_kbd_t *kbd_dev)
771{
772 return (kbd_dev->initialized == USB_KBD_STATUS_TO_DESTROY);
773}
774
775/*----------------------------------------------------------------------------*/
776/**
777 * Properly destroys the USB/HID keyboard structure.
778 *
779 * @param kbd_dev Pointer to the structure to be destroyed.
780 */
781void usb_kbd_free(usb_kbd_t **kbd_dev)
782{
783 if (kbd_dev == NULL || *kbd_dev == NULL) {
784 return;
785 }
786
787 // hangup phone to the console
788 async_obsolete_hangup((*kbd_dev)->console_phone);
789
790 if ((*kbd_dev)->repeat_mtx != NULL) {
791 //assert(!fibril_mutex_is_locked((*kbd_dev)->repeat_mtx));
792 // FIXME - the fibril_mutex_is_locked may not cause
793 // fibril scheduling
794 while (fibril_mutex_is_locked((*kbd_dev)->repeat_mtx)) {}
795 free((*kbd_dev)->repeat_mtx);
796 }
797
798 // free all buffers
799 if ((*kbd_dev)->keys != NULL) {
800 free((*kbd_dev)->keys);
801 }
802 if ((*kbd_dev)->keys_old != NULL) {
803 free((*kbd_dev)->keys_old);
804 }
805 if ((*kbd_dev)->led_data != NULL) {
806 free((*kbd_dev)->led_data);
807 }
808 if ((*kbd_dev)->led_path != NULL) {
809 usb_hid_report_path_free((*kbd_dev)->led_path);
810 }
811 if ((*kbd_dev)->output_buffer != NULL) {
812 usb_hid_report_output_free((*kbd_dev)->output_buffer);
813 }
814
815 free(*kbd_dev);
816 *kbd_dev = NULL;
817}
818
819/*----------------------------------------------------------------------------*/
820
821void usb_kbd_deinit(usb_hid_dev_t *hid_dev, void *data)
822{
823 if (hid_dev == NULL) {
824 return;
825 }
826
827 if (data != NULL) {
828 usb_kbd_t *kbd_dev = (usb_kbd_t *)data;
829 if (usb_kbd_is_initialized(kbd_dev)) {
830 usb_kbd_mark_unusable(kbd_dev);
831 } else {
832 usb_kbd_free(&kbd_dev);
833 }
834 }
835}
836
837/*----------------------------------------------------------------------------*/
838
839int usb_kbd_set_boot_protocol(usb_hid_dev_t *hid_dev)
840{
841 int rc = usb_hid_parse_report_descriptor(hid_dev->report,
842 USB_KBD_BOOT_REPORT_DESCRIPTOR,
843 USB_KBD_BOOT_REPORT_DESCRIPTOR_SIZE);
844
845 if (rc != EOK) {
846 usb_log_error("Failed to parse boot report descriptor: %s\n",
847 str_error(rc));
848 return rc;
849 }
850
851 rc = usbhid_req_set_protocol(&hid_dev->usb_dev->ctrl_pipe,
852 hid_dev->usb_dev->interface_no, USB_HID_PROTOCOL_BOOT);
853
854 if (rc != EOK) {
855 usb_log_warning("Failed to set boot protocol to the device: "
856 "%s\n", str_error(rc));
857 return rc;
858 }
859
860 return EOK;
861}
862
863/**
864 * @}
865 */
Note: See TracBrowser for help on using the repository browser.