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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since fec47d4 was 73ae3373, checked in by Lubos Slovak <lubos.slovak@…>, 14 years ago

Fixed default connection handler + typo

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