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

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

Renaming to reflect classes are gone.

  • Property mode set to 100644
File size: 25.4 KB
RevLine 
[61257f4]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>
[f2f99ae]42#include <io/console.h>
[5f88293]43#include <ipc/kbdev.h>
[61257f4]44#include <async.h>
[79ae36dd]45#include <async_obsolete.h>
[61257f4]46#include <fibril.h>
47#include <fibril_synch.h>
48
[313775b]49#include <ddf/log.h>
50
[61257f4]51#include <usb/usb.h>
[7d521e24]52#include <usb/dev/dp.h>
53#include <usb/dev/request.h>
[faa44e58]54#include <usb/hid/hid.h>
[7d521e24]55#include <usb/dev/pipes.h>
[61257f4]56#include <usb/debug.h>
[faa44e58]57#include <usb/hid/hidparser.h>
[61257f4]58#include <usb/classes/classes.h>
[faa44e58]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>
[61257f4]63
[7d521e24]64#include <usb/dev/driver.h>
[61257f4]65
66#include "kbddev.h"
67
68#include "conv.h"
69#include "kbdrepeat.h"
70
71#include "../usbhid.h"
72
[79ae36dd]73// FIXME: remove this header
[c0699467]74#include <abi/ipc/methods.h>
[79ae36dd]75
[61257f4]76/*----------------------------------------------------------------------------*/
[f2f99ae]77
[61257f4]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. */
[73ae3373]94usb_endpoint_description_t usb_hid_kbd_poll_endpoint_description = {
[61257f4]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";
[1dc4a5e]104const char *HID_KBD_CATEGORY_NAME = "keyboard";
[61257f4]105
[60e5a856]106static void usb_kbd_set_led(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev);
107
[61257f4]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
[46b60e6]165/**
[61257f4]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 */
[74a1ba9]176static void default_connection_handler(ddf_fun_t *fun,
[61257f4]177 ipc_callid_t icallid, ipc_call_t *icall)
178{
179 sysarg_t method = IPC_GET_IMETHOD(*icall);
[60e5a856]180 int callback;
[61257f4]181
[65b458c4]182 usb_kbd_t *kbd_dev = (usb_kbd_t *)fun->driver_data;
183 if (kbd_dev == NULL) {
[f8e549b]184 usb_log_debug("default_connection_handler: "
185 "Missing parameter.\n");
[73ae3373]186 async_answer_0(icallid, EINVAL);
187 return;
188 }
[61257f4]189
[60e5a856]190 switch (method) {
191 case IPC_M_CONNECT_TO_ME:
192 callback = IPC_GET_ARG5(*icall);
[61257f4]193
194 if (kbd_dev->console_phone != -1) {
[f8e549b]195 usb_log_debug("default_connection_handler: "
196 "console phone already set\n");
[61257f4]197 async_answer_0(icallid, ELIMIT);
198 return;
199 }
200
201 kbd_dev->console_phone = callback;
[f8e549b]202
203 usb_log_debug("default_connection_handler: OK\n");
[61257f4]204 async_answer_0(icallid, EOK);
[60e5a856]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;
[61257f4]215 }
216}
217
218/*----------------------------------------------------------------------------*/
219/* Key processing functions */
220/*----------------------------------------------------------------------------*/
221/**
222 * Handles turning of LED lights on and off.
223 *
[46b60e6]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.
[61257f4]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 }
[19e0560e]239
[61257f4]240 /* Reset the LED data. */
241 memset(kbd_dev->led_data, 0, kbd_dev->led_output_size * sizeof(int32_t));
[cfbbe1d3]242 usb_log_debug("Creating output report:\n");
[61257f4]243
[6513110]244 usb_hid_report_field_t *field = usb_hid_report_get_sibling(
245 hid_dev->report, NULL, kbd_dev->led_path,
[3a6e423]246 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
[6513110]247 USB_HID_REPORT_TYPE_OUTPUT);
248
[46b60e6]249 while (field != NULL) {
[3a6e423]250
[6513110]251 if ((field->usage == USB_HID_LED_NUM_LOCK)
252 && (kbd_dev->mods & KM_NUM_LOCK)){
[cfbbe1d3]253 field->value = 1;
254 }
255
[6513110]256 if ((field->usage == USB_HID_LED_CAPS_LOCK)
257 && (kbd_dev->mods & KM_CAPS_LOCK)){
[cfbbe1d3]258 field->value = 1;
259 }
260
[6513110]261 if ((field->usage == USB_HID_LED_SCROLL_LOCK)
262 && (kbd_dev->mods & KM_SCROLL_LOCK)){
[cfbbe1d3]263 field->value = 1;
264 }
265
[6513110]266 field = usb_hid_report_get_sibling(hid_dev->report, field,
[3a6e423]267 kbd_dev->led_path,
268 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
269 USB_HID_REPORT_TYPE_OUTPUT);
[cfbbe1d3]270 }
[6513110]271
272 // TODO: what about the Report ID?
273 int rc = usb_hid_report_output_translate(hid_dev->report, 0,
[e50cd7f]274 kbd_dev->output_buffer, kbd_dev->output_size);
[61257f4]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/*----------------------------------------------------------------------------*/
[46b60e6]292/** Send key event.
[61257f4]293 *
294 * @param kbd_dev Keyboard device structure.
[46b60e6]295 * @param type Type of the event (press / release). Recognized values:
[61257f4]296 * KEY_PRESS, KEY_RELEASE
[46b60e6]297 * @param key Key code
[61257f4]298 */
299void usb_kbd_push_ev(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev, int type,
300 unsigned int key)
301{
[f2f99ae]302 usb_log_debug2("Sending kbdev event %d/%d to the console\n", type, key);
[61257f4]303 if (kbd_dev->console_phone < 0) {
304 usb_log_warning(
305 "Connection to console not ready, key discarded.\n");
306 return;
307 }
308
[5f88293]309 async_obsolete_msg_2(kbd_dev->console_phone, KBDEV_EVENT, type, key);
[61257f4]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
[44e2c1a]321static size_t find_in_array_int32(int32_t val, int32_t *arr, size_t arr_size)
322{
323 for (size_t i = 0; i < arr_size; i++) {
324 if (arr[i] == val) {
325 return i;
326 }
327 }
328
329 return (size_t) -1;
330}
331
[61257f4]332/*----------------------------------------------------------------------------*/
333/**
334 * Checks if some keys were pressed or released and generates key events.
335 *
336 * An event is created only when key is pressed or released. Besides handling
337 * the events (usb_kbd_push_ev()), the auto-repeat fibril is notified about
338 * key presses and releases (see usb_kbd_repeat_start() and
339 * usb_kbd_repeat_stop()).
340 *
341 * @param kbd_dev Keyboard device structure.
342 * @param key_codes Parsed keyboard report - codes of currently pressed keys
343 * according to HID Usage Tables.
344 * @param count Number of key codes in report (size of the report).
345 *
346 * @sa usb_kbd_push_ev(), usb_kbd_repeat_start(), usb_kbd_repeat_stop()
347 */
348static void usb_kbd_check_key_changes(usb_hid_dev_t *hid_dev,
[19e0560e]349 usb_kbd_t *kbd_dev)
[61257f4]350{
351 unsigned int key;
[44e2c1a]352 size_t i;
[61257f4]353
354 /*
355 * First of all, check if the kbd have reported phantom state.
356 *
357 * As there is no way to distinguish keys from modifiers, we do not have
358 * a way to check that 'all keys report Error Rollover'. We thus check
359 * if there is at least one such error and in such case we ignore the
360 * whole input report.
361 */
[44e2c1a]362 i = find_in_array_int32(ERROR_ROLLOVER, kbd_dev->keys,
363 kbd_dev->key_count);
364 if (i != (size_t) -1) {
365 usb_log_debug("Detected phantom state.\n");
[61257f4]366 return;
367 }
368
369 /*
[44e2c1a]370 * Key releases
[61257f4]371 */
[44e2c1a]372 for (i = 0; i < kbd_dev->key_count; i++) {
373 int32_t old_key = kbd_dev->keys_old[i];
374 /* Find the old key among currently pressed keys. */
375 size_t pos = find_in_array_int32(old_key, kbd_dev->keys,
376 kbd_dev->key_count);
377 /* If the key was not found, we need to signal release. */
378 if (pos == (size_t) -1) {
379 key = usbhid_parse_scancode(old_key);
[61257f4]380 if (!usb_kbd_is_lock(key)) {
381 usb_kbd_repeat_stop(kbd_dev, key);
382 }
383 usb_kbd_push_ev(hid_dev, kbd_dev, KEY_RELEASE, key);
[44e2c1a]384 usb_log_debug2("Key released: %u "
385 "(USB code %" PRIu32 ")\n", key, old_key);
[61257f4]386 }
387 }
388
389 /*
[44e2c1a]390 * Key presses
[61257f4]391 */
392 for (i = 0; i < kbd_dev->key_count; ++i) {
[44e2c1a]393 int32_t new_key = kbd_dev->keys[i];
394 /* Find the new key among already pressed keys. */
395 size_t pos = find_in_array_int32(new_key, kbd_dev->keys_old,
396 kbd_dev->key_count);
397 /* If the key was not found, we need to signal press. */
398 if (pos == (size_t) -1) {
[e60436b]399 key = usbhid_parse_scancode(kbd_dev->keys[i]);
[61257f4]400 if (!usb_kbd_is_lock(key)) {
401 usb_kbd_repeat_start(kbd_dev, key);
402 }
[1b7dc5e9]403 usb_kbd_push_ev(hid_dev, kbd_dev, KEY_PRESS, key);
[44e2c1a]404 usb_log_debug2("Key pressed: %u "
405 "(USB code %" PRIu32 ")\n", key, new_key);
[61257f4]406 }
407 }
408
[e60436b]409 memcpy(kbd_dev->keys_old, kbd_dev->keys, kbd_dev->key_count * 4);
410
[313775b]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);
[61257f4]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 */
[4d3c13e]436static void usb_kbd_process_data(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev)
[61257f4]437{
[e60436b]438 assert(hid_dev->report != NULL);
439 assert(hid_dev != NULL);
[65b458c4]440 assert(kbd_dev != NULL);
[61257f4]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);
[cfbbe1d3]444
[65c3794]445 usb_hid_report_path_set_report_id (path, hid_dev->report_id);
[f240d30]446
[e60436b]447 // fill in the currently pressed keys
448
[f240d30]449 usb_hid_report_field_t *field = usb_hid_report_get_sibling(
450 hid_dev->report, NULL, path,
[6513110]451 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
452 USB_HID_REPORT_TYPE_INPUT);
[e60436b]453 unsigned i = 0;
454
455 while (field != NULL) {
456 usb_log_debug2("FIELD (%p) - VALUE(%d) USAGE(%u)\n",
[7304663]457 field, field->value, field->usage);
[e50cd7f]458
[e60436b]459 assert(i < kbd_dev->key_count);
460
461 // save the key usage
[323b0ec]462 if (field->value != 0) {
[7304663]463 kbd_dev->keys[i] = field->usage;
464 }
465 else {
466 kbd_dev->keys[i] = 0;
467 }
[e60436b]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 }
[e50cd7f]476
[61257f4]477 usb_hid_report_path_free(path);
478
[e60436b]479 usb_kbd_check_key_changes(hid_dev, kbd_dev);
[61257f4]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
[31cfee16]518/*----------------------------------------------------------------------------*/
519
[65b458c4]520static int usb_kbd_create_function(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev)
[31cfee16]521{
522 assert(hid_dev != NULL);
523 assert(hid_dev->usb_dev != NULL);
[65b458c4]524 assert(kbd_dev != NULL);
[31cfee16]525
[15f3c3f]526 /* Create the exposed function. */
[31cfee16]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 */
[65b458c4]539 fun->ops = &kbd_dev->ops;
540 fun->driver_data = kbd_dev;
[31cfee16]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
[0d59d0e9]550 usb_log_debug("%s function created. Handle: %" PRIun "\n",
551 HID_KBD_FUN_NAME, fun->handle);
[4939490]552
[1dc4a5e]553 usb_log_debug("Adding DDF function to category %s...\n",
[31cfee16]554 HID_KBD_CLASS_NAME);
[1dc4a5e]555 rc = ddf_fun_add_to_category(fun, HID_KBD_CATEGORY_NAME);
[31cfee16]556 if (rc != EOK) {
557 usb_log_error(
[1dc4a5e]558 "Could not add DDF function to category %s: %s.\n",
[31cfee16]559 HID_KBD_CLASS_NAME, str_error(rc));
560 ddf_fun_destroy(fun);
561 return rc;
562 }
563
564 return EOK;
565}
566
[61257f4]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 */
[65b458c4]590int usb_kbd_init(usb_hid_dev_t *hid_dev, void **data)
[61257f4]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 }
[60e5a856]606
607 /* Store link to HID device */
608 kbd_dev->hid_dev = hid_dev;
[61257f4]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
[7f2e33a]616 usb_hid_report_path_set_report_id(path, 0);
[61257f4]617
[3a6e423]618 kbd_dev->key_count = usb_hid_report_size(
619 hid_dev->report, 0, USB_HID_REPORT_TYPE_INPUT);
[61257f4]620 usb_hid_report_path_free(path);
621
622 usb_log_debug("Size of the input report: %zu\n", kbd_dev->key_count);
623
[e60436b]624 kbd_dev->keys = (int32_t *)calloc(kbd_dev->key_count, sizeof(int32_t));
[61257f4]625
626 if (kbd_dev->keys == NULL) {
627 usb_log_fatal("No memory!\n");
628 free(kbd_dev);
629 return ENOMEM;
630 }
631
[e60436b]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
[61257f4]642 /*
643 * Output report
644 */
645 kbd_dev->output_size = 0;
[e60436b]646 kbd_dev->output_buffer = usb_hid_report_output(hid_dev->report,
647 &kbd_dev->output_size, 0);
[e50cd7f]648 if (kbd_dev->output_buffer == NULL) {
[61257f4]649 usb_log_warning("Error creating output report buffer.\n");
650 free(kbd_dev->keys);
[19e0560e]651 return ENOMEM;
[61257f4]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
[3a6e423]660 kbd_dev->led_output_size = usb_hid_report_size(hid_dev->report,
661 0, USB_HID_REPORT_TYPE_OUTPUT);
[61257f4]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
[f2f99ae]680 */
[61257f4]681 kbd_dev->modifiers = 0;
682 kbd_dev->mods = DEFAULT_ACTIVE_MODS;
683 kbd_dev->lock_keys = 0;
684
685 /*
686 * Autorepeat
[f2f99ae]687 */
[61257f4]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
[65b458c4]706 *data = kbd_dev;
[61257f4]707
708 // set handler for incoming calls
[65b458c4]709 kbd_dev->ops.default_handler = default_connection_handler;
[61257f4]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
[31cfee16]733 usb_log_debug("Creating KBD function...\n");
[65b458c4]734 int rc = usb_kbd_create_function(hid_dev, kbd_dev);
[31cfee16]735 if (rc != EOK) {
736 usb_kbd_free(&kbd_dev);
737 return rc;
738 }
739
[61257f4]740 return EOK;
741}
742
743/*----------------------------------------------------------------------------*/
744
[4d3c13e]745bool usb_kbd_polling_callback(usb_hid_dev_t *hid_dev, void *data)
[61257f4]746{
[65c3794]747 if (hid_dev == NULL/* || buffer == NULL*/ || data == NULL) {
[61257f4]748 // do not continue polling (???)
749 return false;
750 }
751
[65b458c4]752 usb_kbd_t *kbd_dev = (usb_kbd_t *)data;
753 assert(kbd_dev != NULL);
754
[61257f4]755 // TODO: add return value from this function
[4d3c13e]756 usb_kbd_process_data(hid_dev, kbd_dev);
[61257f4]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
[79ae36dd]788 async_obsolete_hangup((*kbd_dev)->console_phone);
[61257f4]789
790 if ((*kbd_dev)->repeat_mtx != NULL) {
[19e0560e]791 //assert(!fibril_mutex_is_locked((*kbd_dev)->repeat_mtx));
[4d3c13e]792 // FIXME - the fibril_mutex_is_locked may not cause
793 // fibril scheduling
[19e0560e]794 while (fibril_mutex_is_locked((*kbd_dev)->repeat_mtx)) {}
[61257f4]795 free((*kbd_dev)->repeat_mtx);
796 }
797
[323b0ec]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 }
[61257f4]814
815 free(*kbd_dev);
816 *kbd_dev = NULL;
817}
818
819/*----------------------------------------------------------------------------*/
820
[65b458c4]821void usb_kbd_deinit(usb_hid_dev_t *hid_dev, void *data)
[61257f4]822{
823 if (hid_dev == NULL) {
824 return;
825 }
826
[65b458c4]827 if (data != NULL) {
828 usb_kbd_t *kbd_dev = (usb_kbd_t *)data;
[61257f4]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{
[e60436b]841 int rc = usb_hid_parse_report_descriptor(hid_dev->report,
[61257f4]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.