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

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

Mouse wheel acting as arrow keys.

  • Added some debug output.
  • Another function for mouse, added to class keyboard.
  • Another phone to console stored in mouse device.
  • Property mode set to 100644
File size: 33.3 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
179//static void usb_kbd_process_keycodes(const uint8_t *key_codes, size_t count,
180// uint8_t report_id, void *arg);
181
182//static const usb_hid_report_in_callbacks_t usb_kbd_parser_callbacks = {
183// .keyboard = usb_kbd_process_keycodes
184//};
185
186/*----------------------------------------------------------------------------*/
187/* Keyboard layouts */
188/*----------------------------------------------------------------------------*/
189
190#define NUM_LAYOUTS 3
191
192/** Keyboard layout map. */
193static layout_op_t *layout[NUM_LAYOUTS] = {
194 &us_qwerty_op,
195 &us_dvorak_op,
196 &cz_op
197};
198
199static int active_layout = 0;
200
201/*----------------------------------------------------------------------------*/
202/* Modifier constants */
203/*----------------------------------------------------------------------------*/
204/** Mapping of USB modifier key codes to generic modifier key codes. */
205//static const keycode_t usbhid_modifiers_keycodes[USB_HID_MOD_COUNT] = {
206// KC_LCTRL, /* USB_HID_MOD_LCTRL */
207// KC_LSHIFT, /* USB_HID_MOD_LSHIFT */
208// KC_LALT, /* USB_HID_MOD_LALT */
209// 0, /* USB_HID_MOD_LGUI */
210// KC_RCTRL, /* USB_HID_MOD_RCTRL */
211// KC_RSHIFT, /* USB_HID_MOD_RSHIFT */
212// KC_RALT, /* USB_HID_MOD_RALT */
213// 0, /* USB_HID_MOD_RGUI */
214//};
215
216//typedef enum usbhid_lock_code {
217// USB_KBD_LOCK_NUM = 0x53,
218// USB_KBD_LOCK_CAPS = 0x39,
219// USB_KBD_LOCK_SCROLL = 0x47,
220// USB_KBD_LOCK_COUNT = 3
221//} usbhid_lock_code;
222
223//static const usbhid_lock_code usbhid_lock_codes[USB_KBD_LOCK_COUNT] = {
224// USB_KBD_LOCK_NUM,
225// USB_KBD_LOCK_CAPS,
226// USB_KBD_LOCK_SCROLL
227//};
228
229/*----------------------------------------------------------------------------*/
230/* IPC method handler */
231/*----------------------------------------------------------------------------*/
232
233static void default_connection_handler(ddf_fun_t *, ipc_callid_t, ipc_call_t *);
234//ddf_dev_ops_t keyboard_ops = {
235// .default_handler = default_connection_handler
236//};
237
238/**
239 * Default handler for IPC methods not handled by DDF.
240 *
241 * Currently recognizes only one method (IPC_M_CONNECT_TO_ME), in which case it
242 * assumes the caller is the console and thus it stores IPC phone to it for
243 * later use by the driver to notify about key events.
244 *
245 * @param fun Device function handling the call.
246 * @param icallid Call id.
247 * @param icall Call data.
248 */
249static void default_connection_handler(ddf_fun_t *fun,
250 ipc_callid_t icallid, ipc_call_t *icall)
251{
252 sysarg_t method = IPC_GET_IMETHOD(*icall);
253
254 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
255
256 if (hid_dev == NULL || hid_dev->data == NULL) {
257 usb_log_debug("default_connection_handler: "
258 "Missing parameter.\n");
259 async_answer_0(icallid, EINVAL);
260 return;
261 }
262
263 assert(hid_dev != NULL);
264 assert(hid_dev->data != NULL);
265 usb_kbd_t *kbd_dev = (usb_kbd_t *)hid_dev->data;
266
267 if (method == IPC_M_CONNECT_TO_ME) {
268 int callback = IPC_GET_ARG5(*icall);
269
270 if (kbd_dev->console_phone != -1) {
271 usb_log_debug("default_connection_handler: "
272 "console phone already set\n");
273 async_answer_0(icallid, ELIMIT);
274 return;
275 }
276
277 kbd_dev->console_phone = callback;
278
279 usb_log_debug("default_connection_handler: OK\n");
280 async_answer_0(icallid, EOK);
281 return;
282 }
283
284 usb_log_debug("default_connection_handler: Wrong function.\n");
285 async_answer_0(icallid, EINVAL);
286}
287
288/*----------------------------------------------------------------------------*/
289/* Key processing functions */
290/*----------------------------------------------------------------------------*/
291/**
292 * Handles turning of LED lights on and off.
293 *
294 * In case of USB keyboards, the LEDs are handled in the driver, not in the
295 * device. When there should be a change (lock key was pressed), the driver
296 * uses a Set_Report request sent to the device to set the state of the LEDs.
297 *
298 * This functions sets the LED lights according to current settings of modifiers
299 * kept in the keyboard device structure.
300 *
301 * @param kbd_dev Keyboard device structure.
302 */
303static void usb_kbd_set_led(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev)
304{
305 if (kbd_dev->output_size == 0) {
306 return;
307 }
308
309 /* Reset the LED data. */
310 memset(kbd_dev->led_data, 0, kbd_dev->led_output_size * sizeof(int32_t));
311 usb_log_debug("Creating output report:\n");
312
313 usb_hid_report_field_t *field = usb_hid_report_get_sibling(
314 hid_dev->report, NULL, kbd_dev->led_path,
315 USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY | USB_HID_PATH_COMPARE_END,
316 USB_HID_REPORT_TYPE_OUTPUT);
317
318 while (field != NULL) {
319
320 if ((field->usage == USB_HID_LED_NUM_LOCK)
321 && (kbd_dev->mods & KM_NUM_LOCK)){
322 field->value = 1;
323 }
324
325 if ((field->usage == USB_HID_LED_CAPS_LOCK)
326 && (kbd_dev->mods & KM_CAPS_LOCK)){
327 field->value = 1;
328 }
329
330 if ((field->usage == USB_HID_LED_SCROLL_LOCK)
331 && (kbd_dev->mods & KM_SCROLL_LOCK)){
332 field->value = 1;
333 }
334
335 field = usb_hid_report_get_sibling(hid_dev->report, field,
336 kbd_dev->led_path, USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY
337 | USB_HID_PATH_COMPARE_END, USB_HID_REPORT_TYPE_OUTPUT);
338 }
339
340 // TODO: what about the Report ID?
341 int rc = usb_hid_report_output_translate(hid_dev->report, 0,
342 kbd_dev->output_buffer, kbd_dev->output_size);
343
344 if (rc != EOK) {
345 usb_log_warning("Error translating LED output to output report"
346 ".\n");
347 return;
348 }
349
350 usb_log_debug("Output report buffer: %s\n",
351 usb_debug_str_buffer(kbd_dev->output_buffer, kbd_dev->output_size,
352 0));
353
354 usbhid_req_set_report(&hid_dev->usb_dev->ctrl_pipe,
355 hid_dev->usb_dev->interface_no, USB_HID_REPORT_TYPE_OUTPUT,
356 kbd_dev->output_buffer, kbd_dev->output_size);
357}
358
359/*----------------------------------------------------------------------------*/
360/**
361 * Processes key events.
362 *
363 * @note This function was copied from AT keyboard driver and modified to suit
364 * USB keyboard.
365 *
366 * @note Lock keys are not sent to the console, as they are completely handled
367 * in the driver. It may, however, be required later that the driver
368 * sends also these keys to application (otherwise it cannot use those
369 * keys at all).
370 *
371 * @param kbd_dev Keyboard device structure.
372 * @param type Type of the event (press / release). Recognized values:
373 * KEY_PRESS, KEY_RELEASE
374 * @param key Key code of the key according to HID Usage Tables.
375 */
376void usb_kbd_push_ev(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev, int type,
377 unsigned int key)
378{
379 console_event_t ev;
380 unsigned mod_mask;
381
382 /*
383 * These parts are copy-pasted from the AT keyboard driver.
384 *
385 * They definitely require some refactoring, but will keep it for later
386 * when the console and keyboard system is changed in HelenOS.
387 */
388 switch (key) {
389 case KC_LCTRL: mod_mask = KM_LCTRL; break;
390 case KC_RCTRL: mod_mask = KM_RCTRL; break;
391 case KC_LSHIFT: mod_mask = KM_LSHIFT; break;
392 case KC_RSHIFT: mod_mask = KM_RSHIFT; break;
393 case KC_LALT: mod_mask = KM_LALT; break;
394 case KC_RALT: mod_mask = KM_RALT; break;
395 default: mod_mask = 0; break;
396 }
397
398 if (mod_mask != 0) {
399 if (type == KEY_PRESS)
400 kbd_dev->mods = kbd_dev->mods | mod_mask;
401 else
402 kbd_dev->mods = kbd_dev->mods & ~mod_mask;
403 }
404
405 switch (key) {
406 case KC_CAPS_LOCK: mod_mask = KM_CAPS_LOCK; break;
407 case KC_NUM_LOCK: mod_mask = KM_NUM_LOCK; break;
408 case KC_SCROLL_LOCK: mod_mask = KM_SCROLL_LOCK; break;
409 default: mod_mask = 0; break;
410 }
411
412 if (mod_mask != 0) {
413 if (type == KEY_PRESS) {
414 /*
415 * Only change lock state on transition from released
416 * to pressed. This prevents autorepeat from messing
417 * up the lock state.
418 */
419 unsigned int locks_old = kbd_dev->lock_keys;
420
421 kbd_dev->mods =
422 kbd_dev->mods ^ (mod_mask & ~kbd_dev->lock_keys);
423 kbd_dev->lock_keys = kbd_dev->lock_keys | mod_mask;
424
425 /* Update keyboard lock indicator lights. */
426 if (kbd_dev->lock_keys != locks_old
427 && hid_dev != NULL) { // ugly hack
428 usb_kbd_set_led(hid_dev, kbd_dev);
429 }
430 } else {
431 kbd_dev->lock_keys = kbd_dev->lock_keys & ~mod_mask;
432 }
433 }
434
435 if (key == KC_CAPS_LOCK || key == KC_NUM_LOCK || key == KC_SCROLL_LOCK) {
436 // do not send anything to the console, this is our business
437 return;
438 }
439
440 if (type == KEY_PRESS && (kbd_dev->mods & KM_LCTRL) && key == KC_F1) {
441 active_layout = 0;
442 layout[active_layout]->reset();
443 return;
444 }
445
446 if (type == KEY_PRESS && (kbd_dev->mods & KM_LCTRL) && key == KC_F2) {
447 active_layout = 1;
448 layout[active_layout]->reset();
449 return;
450 }
451
452 if (type == KEY_PRESS && (kbd_dev->mods & KM_LCTRL) && key == KC_F3) {
453 active_layout = 2;
454 layout[active_layout]->reset();
455 return;
456 }
457
458 ev.type = type;
459 ev.key = key;
460 ev.mods = kbd_dev->mods;
461
462 ev.c = layout[active_layout]->parse_ev(&ev);
463
464 usb_log_debug2("Sending key %d to the console\n", ev.key);
465 if (kbd_dev->console_phone < 0) {
466 usb_log_warning(
467 "Connection to console not ready, key discarded.\n");
468 return;
469 }
470
471 async_msg_4(kbd_dev->console_phone, KBD_EVENT, ev.type, ev.key,
472 ev.mods, ev.c);
473}
474
475/*----------------------------------------------------------------------------*/
476
477static inline int usb_kbd_is_lock(unsigned int key_code)
478{
479 return (key_code == KC_NUM_LOCK
480 || key_code == KC_SCROLL_LOCK
481 || key_code == KC_CAPS_LOCK);
482}
483
484/*----------------------------------------------------------------------------*/
485/**
486 * Checks if some keys were pressed or released and generates key events.
487 *
488 * An event is created only when key is pressed or released. Besides handling
489 * the events (usb_kbd_push_ev()), the auto-repeat fibril is notified about
490 * key presses and releases (see usb_kbd_repeat_start() and
491 * usb_kbd_repeat_stop()).
492 *
493 * @param kbd_dev Keyboard device structure.
494 * @param key_codes Parsed keyboard report - codes of currently pressed keys
495 * according to HID Usage Tables.
496 * @param count Number of key codes in report (size of the report).
497 *
498 * @sa usb_kbd_push_ev(), usb_kbd_repeat_start(), usb_kbd_repeat_stop()
499 */
500static void usb_kbd_check_key_changes(usb_hid_dev_t *hid_dev,
501 usb_kbd_t *kbd_dev/*, const uint8_t *key_codes, size_t count*/)
502{
503 unsigned int key;
504 unsigned int i, j;
505
506 /*
507 * First of all, check if the kbd have reported phantom state.
508 *
509 * As there is no way to distinguish keys from modifiers, we do not have
510 * a way to check that 'all keys report Error Rollover'. We thus check
511 * if there is at least one such error and in such case we ignore the
512 * whole input report.
513 */
514 i = 0;
515 while (i < kbd_dev->key_count && kbd_dev->keys[i] != ERROR_ROLLOVER) {
516 ++i;
517 }
518 if (i != kbd_dev->key_count) {
519 usb_log_debug("Phantom state occured.\n");
520 // phantom state, do nothing
521 return;
522 }
523
524 /*
525 * 1) Key releases
526 */
527 for (j = 0; j < kbd_dev->key_count; ++j) {
528 // try to find the old key in the new key list
529 i = 0;
530 while (i < kbd_dev->key_count
531 && kbd_dev->keys[i] != kbd_dev->keys_old[j]) {
532 ++i;
533 }
534
535 if (i == kbd_dev->key_count) {
536 // not found, i.e. the key was released
537 key = usbhid_parse_scancode(kbd_dev->keys_old[j]);
538 if (!usb_kbd_is_lock(key)) {
539 usb_kbd_repeat_stop(kbd_dev, key);
540 }
541 usb_kbd_push_ev(hid_dev, kbd_dev, KEY_RELEASE, key);
542 usb_log_debug2("Key released: %d\n", key);
543 } else {
544 // found, nothing happens
545 }
546 }
547
548 /*
549 * 1) Key presses
550 */
551 for (i = 0; i < kbd_dev->key_count; ++i) {
552 // try to find the new key in the old key list
553 j = 0;
554 while (j < kbd_dev->key_count
555 && kbd_dev->keys_old[j] != kbd_dev->keys[i]) {
556 ++j;
557 }
558
559 if (j == kbd_dev->key_count) {
560 // not found, i.e. new key pressed
561 key = usbhid_parse_scancode(kbd_dev->keys[i]);
562 usb_log_debug2("Key pressed: %d (keycode: %d)\n", key,
563 kbd_dev->keys[i]);
564 usb_kbd_push_ev(hid_dev, kbd_dev, KEY_PRESS, key);
565 if (!usb_kbd_is_lock(key)) {
566 usb_kbd_repeat_start(kbd_dev, key);
567 }
568 } else {
569 // found, nothing happens
570 }
571 }
572
573// usb_log_debug("Old keys: ");
574// for (i = 0; i < kbd_dev->key_count; ++i) {
575// usb_log_debug("%d ", kbd_dev->keys_old[i]);
576// }
577// usb_log_debug("\n");
578
579
580// usb_log_debug("New keys: ");
581// for (i = 0; i < kbd_dev->key_count; ++i) {
582// usb_log_debug("%d ", kbd_dev->keys[i]);
583// }
584// usb_log_debug("\n");
585
586 memcpy(kbd_dev->keys_old, kbd_dev->keys, kbd_dev->key_count * 4);
587
588 usb_log_debug2("New stored keys: ");
589 for (i = 0; i < kbd_dev->key_count; ++i) {
590 usb_log_debug2("%d ", kbd_dev->keys_old[i]);
591 }
592 usb_log_debug2("\n");
593}
594
595/*----------------------------------------------------------------------------*/
596/* Callbacks for parser */
597/*----------------------------------------------------------------------------*/
598/**
599 * Callback function for the HID report parser.
600 *
601 * This function is called by the HID report parser with the parsed report.
602 * The parsed report is used to check if any events occured (key was pressed or
603 * released, modifier was pressed or released).
604 *
605 * @param key_codes Parsed keyboard report - codes of currently pressed keys
606 * according to HID Usage Tables.
607 * @param count Number of key codes in report (size of the report).
608 * @param report_id
609 * @param arg User-specified argument. Expects pointer to the keyboard device
610 * structure representing the keyboard.
611 *
612 * @sa usb_kbd_check_key_changes(), usb_kbd_check_modifier_changes()
613 */
614//static void usb_kbd_process_keycodes(const uint8_t *key_codes, size_t count,
615// uint8_t report_id, void *arg)
616//{
617// if (arg == NULL) {
618// usb_log_warning("Missing argument in callback "
619// "usbhid_process_keycodes().\n");
620// return;
621// }
622
623// usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
624
625// if (hid_dev->data == NULL) {
626// usb_log_warning("Missing KBD device structure in callback.\n");
627// return;
628// }
629
630// usb_kbd_t *kbd_dev = (usb_kbd_t *)hid_dev->data;
631
632// usb_log_debug("Got keys from parser (report id: %u): %s\n",
633// report_id, usb_debug_str_buffer(key_codes, count, 0));
634
635// if (count != kbd_dev->key_count) {
636// usb_log_warning("Number of received keycodes (%zu) differs from"
637// " expected (%zu).\n", count, kbd_dev->key_count);
638// return;
639// }
640
641// ///usb_kbd_check_modifier_changes(kbd_dev, key_codes, count);
642// usb_kbd_check_key_changes(hid_dev, kbd_dev, key_codes, count);
643//}
644
645/*----------------------------------------------------------------------------*/
646/* General kbd functions */
647/*----------------------------------------------------------------------------*/
648/**
649 * Processes data received from the device in form of report.
650 *
651 * This function uses the HID report parser to translate the data received from
652 * the device into generic USB HID key codes and into generic modifiers bitmap.
653 * The parser then calls the given callback (usb_kbd_process_keycodes()).
654 *
655 * @note Currently, only the boot protocol is supported.
656 *
657 * @param kbd_dev Keyboard device structure (must be initialized).
658 * @param buffer Data from the keyboard (i.e. the report).
659 * @param actual_size Size of the data from keyboard (report size) in bytes.
660 *
661 * @sa usb_kbd_process_keycodes(), usb_hid_boot_keyboard_input_report(),
662 * usb_hid_parse_report().
663 */
664static void usb_kbd_process_data(usb_hid_dev_t *hid_dev,
665 uint8_t *buffer, size_t actual_size)
666{
667 assert(hid_dev->report != NULL);
668 assert(hid_dev != NULL);
669 assert(hid_dev->data != NULL);
670
671 usb_kbd_t *kbd_dev = (usb_kbd_t *)hid_dev->data;
672
673 usb_log_debug("Calling usb_hid_parse_report() with "
674 "buffer %s\n", usb_debug_str_buffer(buffer, actual_size, 0));
675
676// int rc = usb_hid_boot_keyboard_input_report(buffer, actual_size,
677// callbacks, kbd_dev);
678 usb_hid_report_path_t *path = usb_hid_report_path();
679 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_KEYBOARD, 0);
680 //usb_hid_report_path_set_report_id(path, 0);
681
682 uint8_t report_id;
683 int rc = usb_hid_parse_report(hid_dev->report, buffer, actual_size,
684 &report_id);
685
686 if (rc != EOK) {
687 usb_log_warning("Error in usb_hid_parse_report():"
688 "%s\n", str_error(rc));
689 }
690
691 usb_hid_report_path_set_report_id (path, report_id);
692
693 // fill in the currently pressed keys
694
695 usb_hid_report_field_t *field = usb_hid_report_get_sibling(
696 hid_dev->report, NULL, path,
697 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
698 USB_HID_REPORT_TYPE_INPUT);
699 unsigned i = 0;
700
701 while (field != NULL) {
702 usb_log_debug2("FIELD (%p) - VALUE(%d) USAGE(%u)\n",
703 field, field->value, field->usage);
704
705 assert(i < kbd_dev->key_count);
706// if (i == kbd_dev->key_count) {
707// break;
708// }
709
710 // save the key usage
711 /* TODO: maybe it's not good to save value, nor usage
712 * as the value may be e.g. 1 for LEDs and usage may be
713 * value of the LED. On the other hand, in case of normal
714 * keys, the usage is more important and we must check
715 * that. One possible solution: distinguish between those
716 * two parts of the Report somehow.
717 */
718 if (field->value != 0) {
719 kbd_dev->keys[i] = field->usage;
720 }
721 else {
722 kbd_dev->keys[i] = 0;
723 }
724 usb_log_debug2("Saved %u. key usage %d\n", i, kbd_dev->keys[i]);
725
726 ++i;
727 field = usb_hid_report_get_sibling(hid_dev->report, field, path,
728 USB_HID_PATH_COMPARE_END
729 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
730 USB_HID_REPORT_TYPE_INPUT);
731 }
732
733 usb_hid_report_path_free(path);
734
735 usb_kbd_check_key_changes(hid_dev, kbd_dev);
736}
737
738/*----------------------------------------------------------------------------*/
739/* HID/KBD structure manipulation */
740/*----------------------------------------------------------------------------*/
741
742static void usb_kbd_mark_unusable(usb_kbd_t *kbd_dev)
743{
744 kbd_dev->initialized = USB_KBD_STATUS_TO_DESTROY;
745}
746
747/*----------------------------------------------------------------------------*/
748
749/**
750 * Creates a new USB/HID keyboard structure.
751 *
752 * The structure returned by this function is not initialized. Use
753 * usb_kbd_init() to initialize it prior to polling.
754 *
755 * @return New uninitialized structure for representing a USB/HID keyboard or
756 * NULL if not successful (memory error).
757 */
758static usb_kbd_t *usb_kbd_new(void)
759{
760 usb_kbd_t *kbd_dev =
761 (usb_kbd_t *)calloc(1, sizeof(usb_kbd_t));
762
763 if (kbd_dev == NULL) {
764 usb_log_fatal("No memory!\n");
765 return NULL;
766 }
767
768 kbd_dev->console_phone = -1;
769 kbd_dev->initialized = USB_KBD_STATUS_UNINITIALIZED;
770
771 return kbd_dev;
772}
773
774/*----------------------------------------------------------------------------*/
775
776static int usb_kbd_create_function(usb_hid_dev_t *hid_dev)
777{
778 assert(hid_dev != NULL);
779 assert(hid_dev->usb_dev != NULL);
780
781 /* Create the function exposed under /dev/devices. */
782 usb_log_debug("Creating DDF function %s...\n", HID_KBD_FUN_NAME);
783 ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed,
784 HID_KBD_FUN_NAME);
785 if (fun == NULL) {
786 usb_log_error("Could not create DDF function node.\n");
787 return ENOMEM;
788 }
789
790 /*
791 * Store the initialized HID device and HID ops
792 * to the DDF function.
793 */
794 fun->ops = &hid_dev->ops;
795 fun->driver_data = hid_dev; // TODO: maybe change to hid_dev->data
796
797 int rc = ddf_fun_bind(fun);
798 if (rc != EOK) {
799 usb_log_error("Could not bind DDF function: %s.\n",
800 str_error(rc));
801 ddf_fun_destroy(fun);
802 return rc;
803 }
804
805 usb_log_debug("Adding DDF function to class %s...\n",
806 HID_KBD_CLASS_NAME);
807 rc = ddf_fun_add_to_class(fun, HID_KBD_CLASS_NAME);
808 if (rc != EOK) {
809 usb_log_error(
810 "Could not add DDF function to class %s: %s.\n",
811 HID_KBD_CLASS_NAME, str_error(rc));
812 ddf_fun_destroy(fun);
813 return rc;
814 }
815
816 return EOK;
817}
818
819/*----------------------------------------------------------------------------*/
820/* API functions */
821/*----------------------------------------------------------------------------*/
822/**
823 * Initialization of the USB/HID keyboard structure.
824 *
825 * This functions initializes required structures from the device's descriptors.
826 *
827 * During initialization, the keyboard is switched into boot protocol, the idle
828 * rate is set to 0 (infinity), resulting in the keyboard only reporting event
829 * when a key is pressed or released. Finally, the LED lights are turned on
830 * according to the default setup of lock keys.
831 *
832 * @note By default, the keyboards is initialized with Num Lock turned on and
833 * other locks turned off.
834 *
835 * @param kbd_dev Keyboard device structure to be initialized.
836 * @param dev DDF device structure of the keyboard.
837 *
838 * @retval EOK if successful.
839 * @retval EINVAL if some parameter is not given.
840 * @return Other value inherited from function usbhid_dev_init().
841 */
842int usb_kbd_init(usb_hid_dev_t *hid_dev)
843{
844 usb_log_debug("Initializing HID/KBD structure...\n");
845
846 if (hid_dev == NULL) {
847 usb_log_error("Failed to init keyboard structure: no structure"
848 " given.\n");
849 return EINVAL;
850 }
851
852 usb_kbd_t *kbd_dev = usb_kbd_new();
853 if (kbd_dev == NULL) {
854 usb_log_error("Error while creating USB/HID KBD device "
855 "structure.\n");
856 return ENOMEM; // TODO: some other code??
857 }
858
859 /*
860 * TODO: make more general
861 */
862 usb_hid_report_path_t *path = usb_hid_report_path();
863 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_KEYBOARD, 0);
864
865 usb_hid_report_path_set_report_id(path, 0);
866
867 kbd_dev->key_count = usb_hid_report_input_length(
868 hid_dev->report, path,
869 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY);
870 usb_hid_report_path_free(path);
871
872 usb_log_debug("Size of the input report: %zu\n", kbd_dev->key_count);
873
874 kbd_dev->keys = (int32_t *)calloc(kbd_dev->key_count, sizeof(int32_t));
875
876 if (kbd_dev->keys == NULL) {
877 usb_log_fatal("No memory!\n");
878 free(kbd_dev);
879 return ENOMEM;
880 }
881
882 kbd_dev->keys_old =
883 (int32_t *)calloc(kbd_dev->key_count, sizeof(int32_t));
884
885 if (kbd_dev->keys_old == NULL) {
886 usb_log_fatal("No memory!\n");
887 free(kbd_dev->keys);
888 free(kbd_dev);
889 return ENOMEM;
890 }
891
892 /*
893 * Output report
894 */
895 kbd_dev->output_size = 0;
896 kbd_dev->output_buffer = usb_hid_report_output(hid_dev->report,
897 &kbd_dev->output_size, 0);
898 if (kbd_dev->output_buffer == NULL) {
899 usb_log_warning("Error creating output report buffer.\n");
900 free(kbd_dev->keys);
901 return ENOMEM; /* TODO: other error code */
902 }
903
904 usb_log_debug("Output buffer size: %zu\n", kbd_dev->output_size);
905
906 kbd_dev->led_path = usb_hid_report_path();
907 usb_hid_report_path_append_item(
908 kbd_dev->led_path, USB_HIDUT_PAGE_LED, 0);
909
910 kbd_dev->led_output_size = usb_hid_report_output_size(hid_dev->report,
911 kbd_dev->led_path,
912 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY);
913
914 usb_log_debug("Output report size (in items): %zu\n",
915 kbd_dev->led_output_size);
916
917 kbd_dev->led_data = (int32_t *)calloc(
918 kbd_dev->led_output_size, sizeof(int32_t));
919
920 if (kbd_dev->led_data == NULL) {
921 usb_log_warning("Error creating buffer for LED output report."
922 "\n");
923 free(kbd_dev->keys);
924 usb_hid_report_output_free(kbd_dev->output_buffer);
925 free(kbd_dev);
926 return ENOMEM;
927 }
928
929 /*
930 * Modifiers and locks
931 */
932 kbd_dev->modifiers = 0;
933 kbd_dev->mods = DEFAULT_ACTIVE_MODS;
934 kbd_dev->lock_keys = 0;
935
936 /*
937 * Autorepeat
938 */
939 kbd_dev->repeat.key_new = 0;
940 kbd_dev->repeat.key_repeated = 0;
941 kbd_dev->repeat.delay_before = DEFAULT_DELAY_BEFORE_FIRST_REPEAT;
942 kbd_dev->repeat.delay_between = DEFAULT_REPEAT_DELAY;
943
944 kbd_dev->repeat_mtx = (fibril_mutex_t *)(
945 malloc(sizeof(fibril_mutex_t)));
946 if (kbd_dev->repeat_mtx == NULL) {
947 usb_log_fatal("No memory!\n");
948 free(kbd_dev->keys);
949 usb_hid_report_output_free(kbd_dev->output_buffer);
950 free(kbd_dev);
951 return ENOMEM;
952 }
953
954 fibril_mutex_initialize(kbd_dev->repeat_mtx);
955
956 // save the KBD device structure into the HID device structure
957 hid_dev->data = kbd_dev;
958
959 // set handler for incoming calls
960 hid_dev->ops.default_handler = default_connection_handler;
961
962 /*
963 * Set LEDs according to initial setup.
964 * Set Idle rate
965 */
966 usb_kbd_set_led(hid_dev, kbd_dev);
967
968 usbhid_req_set_idle(&hid_dev->usb_dev->ctrl_pipe,
969 hid_dev->usb_dev->interface_no, IDLE_RATE);
970
971 /*
972 * Create new fibril for auto-repeat
973 */
974 fid_t fid = fibril_create(usb_kbd_repeat_fibril, kbd_dev);
975 if (fid == 0) {
976 usb_log_error("Failed to start fibril for KBD auto-repeat");
977 return ENOMEM;
978 }
979 fibril_add_ready(fid);
980
981 kbd_dev->initialized = USB_KBD_STATUS_INITIALIZED;
982 usb_log_debug("HID/KBD device structure initialized.\n");
983
984 usb_log_debug("Creating KBD function...\n");
985 int rc = usb_kbd_create_function(hid_dev);
986 if (rc != EOK) {
987 usb_kbd_free(&kbd_dev);
988 return rc;
989 }
990
991 return EOK;
992}
993
994/*----------------------------------------------------------------------------*/
995
996bool usb_kbd_polling_callback(usb_hid_dev_t *hid_dev, uint8_t *buffer,
997 size_t buffer_size)
998{
999 if (hid_dev == NULL || buffer == NULL) {
1000 // do not continue polling (???)
1001 return false;
1002 }
1003
1004 // TODO: add return value from this function
1005 usb_kbd_process_data(hid_dev, buffer, buffer_size);
1006
1007 return true;
1008}
1009
1010/*----------------------------------------------------------------------------*/
1011
1012int usb_kbd_is_initialized(const usb_kbd_t *kbd_dev)
1013{
1014 return (kbd_dev->initialized == USB_KBD_STATUS_INITIALIZED);
1015}
1016
1017/*----------------------------------------------------------------------------*/
1018
1019int usb_kbd_is_ready_to_destroy(const usb_kbd_t *kbd_dev)
1020{
1021 return (kbd_dev->initialized == USB_KBD_STATUS_TO_DESTROY);
1022}
1023
1024/*----------------------------------------------------------------------------*/
1025/**
1026 * Properly destroys the USB/HID keyboard structure.
1027 *
1028 * @param kbd_dev Pointer to the structure to be destroyed.
1029 */
1030void usb_kbd_free(usb_kbd_t **kbd_dev)
1031{
1032 if (kbd_dev == NULL || *kbd_dev == NULL) {
1033 return;
1034 }
1035
1036 // hangup phone to the console
1037 async_hangup((*kbd_dev)->console_phone);
1038
1039 if ((*kbd_dev)->repeat_mtx != NULL) {
1040 /* TODO: replace by some check and wait */
1041 assert(!fibril_mutex_is_locked((*kbd_dev)->repeat_mtx));
1042 free((*kbd_dev)->repeat_mtx);
1043 }
1044
1045 // free all buffers
1046 if ((*kbd_dev)->keys != NULL) {
1047 free((*kbd_dev)->keys);
1048 }
1049 if ((*kbd_dev)->keys_old != NULL) {
1050 free((*kbd_dev)->keys_old);
1051 }
1052 if ((*kbd_dev)->led_data != NULL) {
1053 free((*kbd_dev)->led_data);
1054 }
1055 if ((*kbd_dev)->led_path != NULL) {
1056 usb_hid_report_path_free((*kbd_dev)->led_path);
1057 }
1058 if ((*kbd_dev)->output_buffer != NULL) {
1059 usb_hid_report_output_free((*kbd_dev)->output_buffer);
1060 }
1061
1062 free(*kbd_dev);
1063 *kbd_dev = NULL;
1064}
1065
1066/*----------------------------------------------------------------------------*/
1067
1068void usb_kbd_deinit(usb_hid_dev_t *hid_dev)
1069{
1070 if (hid_dev == NULL) {
1071 return;
1072 }
1073
1074 if (hid_dev->data != NULL) {
1075 usb_kbd_t *kbd_dev = (usb_kbd_t *)hid_dev->data;
1076 if (usb_kbd_is_initialized(kbd_dev)) {
1077 usb_kbd_mark_unusable(kbd_dev);
1078 } else {
1079 usb_kbd_free(&kbd_dev);
1080 hid_dev->data = NULL;
1081 }
1082 }
1083}
1084
1085/*----------------------------------------------------------------------------*/
1086
1087int usb_kbd_set_boot_protocol(usb_hid_dev_t *hid_dev)
1088{
1089 int rc = usb_hid_parse_report_descriptor(hid_dev->report,
1090 USB_KBD_BOOT_REPORT_DESCRIPTOR,
1091 USB_KBD_BOOT_REPORT_DESCRIPTOR_SIZE);
1092
1093 if (rc != EOK) {
1094 usb_log_error("Failed to parse boot report descriptor: %s\n",
1095 str_error(rc));
1096 return rc;
1097 }
1098
1099 rc = usbhid_req_set_protocol(&hid_dev->usb_dev->ctrl_pipe,
1100 hid_dev->usb_dev->interface_no, USB_HID_PROTOCOL_BOOT);
1101
1102 if (rc != EOK) {
1103 usb_log_warning("Failed to set boot protocol to the device: "
1104 "%s\n", str_error(rc));
1105 return rc;
1106 }
1107
1108 return EOK;
1109}
1110
1111/**
1112 * @}
1113 */
Note: See TracBrowser for help on using the repository browser.