source: mainline/uspace/drv/usbkbd/kbddev.c@ 2e6bbcf

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

Output report using HID parser API finished + fix in phantom state detection.

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