source: mainline/uspace/drv/usbkbd/kbddev.c@ 890961a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 890961a was 2571089, checked in by Matej Klonfar <maklf@…>, 14 years ago

HID parser refactoring

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