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

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

Subdriver-specific data moved to the subdriver structure.

+ DDF ops removed from hid_dev_t.

Should fix #225

  • Property mode set to 100644
File size: 33.1 KB
Line 
1/*
2 * Copyright (c) 2011 Lubos Slovak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup drvusbhid
30 * @{
31 */
32/**
33 * @file
34 * USB HID keyboard device structure and API.
35 */
36
37#include <errno.h>
38#include <str_error.h>
39#include <stdio.h>
40
41#include <io/keycode.h>
42#include <ipc/kbd.h>
43#include <async.h>
44#include <fibril.h>
45#include <fibril_synch.h>
46
47#include <usb/usb.h>
48#include <usb/dp.h>
49#include <usb/request.h>
50#include <usb/classes/hid.h>
51#include <usb/pipes.h>
52#include <usb/debug.h>
53#include <usb/classes/hidparser.h>
54#include <usb/classes/classes.h>
55#include <usb/classes/hidut.h>
56#include <usb/classes/hidreq.h>
57#include <usb/classes/hidreport.h>
58#include <usb/classes/hid/utled.h>
59
60#include <usb/devdrv.h>
61
62#include "kbddev.h"
63
64#include "layout.h"
65#include "conv.h"
66#include "kbdrepeat.h"
67
68#include "../usbhid.h"
69
70/*----------------------------------------------------------------------------*/
71/** Default modifiers when the keyboard is initialized. */
72static const unsigned DEFAULT_ACTIVE_MODS = KM_NUM_LOCK;
73
74///** Boot protocol report size (key part). */
75//static const size_t BOOTP_REPORT_SIZE = 6;
76
77///** Boot protocol total report size. */
78//static const size_t BOOTP_BUFFER_SIZE = 8;
79
80///** Boot protocol output report size. */
81//static const size_t BOOTP_BUFFER_OUT_SIZE = 1;
82
83///** Boot protocol error key code. */
84//static const uint8_t BOOTP_ERROR_ROLLOVER = 1;
85static const uint8_t ERROR_ROLLOVER = 1;
86
87/** Default idle rate for keyboards. */
88static const uint8_t IDLE_RATE = 0;
89
90/** Delay before a pressed key starts auto-repeating. */
91static const unsigned int DEFAULT_DELAY_BEFORE_FIRST_REPEAT = 500 * 1000;
92
93/** Delay between two repeats of a pressed key when auto-repeating. */
94static const unsigned int DEFAULT_REPEAT_DELAY = 50 * 1000;
95
96/*----------------------------------------------------------------------------*/
97
98/** Keyboard polling endpoint description for boot protocol class. */
99usb_endpoint_description_t usb_hid_kbd_poll_endpoint_description = {
100 .transfer_type = USB_TRANSFER_INTERRUPT,
101 .direction = USB_DIRECTION_IN,
102 .interface_class = USB_CLASS_HID,
103 .interface_subclass = USB_HID_SUBCLASS_BOOT,
104 .interface_protocol = USB_HID_PROTOCOL_KEYBOARD,
105 .flags = 0
106};
107
108//static usb_endpoint_description_t hid_poll_endpoint_description = {
109// .transfer_type = USB_TRANSFER_INTERRUPT,
110// .direction = USB_DIRECTION_IN,
111// .interface_class = USB_CLASS_HID,
112// .flags = 0
113//};
114
115///* Array of endpoints expected on the device, NULL terminated. */
116//usb_endpoint_description_t
117// *usb_kbd_endpoints[USB_KBD_POLL_EP_COUNT + 1] = {
118// &boot_poll_endpoint_description,
119// &hid_poll_endpoint_description,
120// NULL
121//};
122
123const char *HID_KBD_FUN_NAME = "keyboard";
124const char *HID_KBD_CLASS_NAME = "keyboard";
125
126/*----------------------------------------------------------------------------*/
127
128enum {
129 USB_KBD_BOOT_REPORT_DESCRIPTOR_SIZE = 63
130};
131
132static const uint8_t USB_KBD_BOOT_REPORT_DESCRIPTOR[
133 USB_KBD_BOOT_REPORT_DESCRIPTOR_SIZE] = {
134 0x05, 0x01, // Usage Page (Generic Desktop),
135 0x09, 0x06, // Usage (Keyboard),
136 0xA1, 0x01, // Collection (Application),
137 0x75, 0x01, // Report Size (1),
138 0x95, 0x08, // Report Count (8),
139 0x05, 0x07, // Usage Page (Key Codes);
140 0x19, 0xE0, // Usage Minimum (224),
141 0x29, 0xE7, // Usage Maximum (231),
142 0x15, 0x00, // Logical Minimum (0),
143 0x25, 0x01, // Logical Maximum (1),
144 0x81, 0x02, // Input (Data, Variable, Absolute), ; Modifier byte
145 0x95, 0x01, // Report Count (1),
146 0x75, 0x08, // Report Size (8),
147 0x81, 0x01, // Input (Constant), ; Reserved byte
148 0x95, 0x05, // Report Count (5),
149 0x75, 0x01, // Report Size (1),
150 0x05, 0x08, // Usage Page (Page# for LEDs),
151 0x19, 0x01, // Usage Minimum (1),
152 0x29, 0x05, // Usage Maxmimum (5),
153 0x91, 0x02, // Output (Data, Variable, Absolute), ; LED report
154 0x95, 0x01, // Report Count (1),
155 0x75, 0x03, // Report Size (3),
156 0x91, 0x01, // Output (Constant), ; LED report padding
157 0x95, 0x06, // Report Count (6),
158 0x75, 0x08, // Report Size (8),
159 0x15, 0x00, // Logical Minimum (0),
160 0x25, 0xff, // Logical Maximum (255),
161 0x05, 0x07, // Usage Page (Key Codes),
162 0x19, 0x00, // Usage Minimum (0),
163 0x29, 0xff, // Usage Maximum (255),
164 0x81, 0x00, // Input (Data, Array), ; Key arrays (6 bytes)
165 0xC0 // End Collection
166
167};
168
169/*----------------------------------------------------------------------------*/
170
171typedef enum usb_kbd_flags {
172 USB_KBD_STATUS_UNINITIALIZED = 0,
173 USB_KBD_STATUS_INITIALIZED = 1,
174 USB_KBD_STATUS_TO_DESTROY = -1
175} usb_kbd_flags;
176
177/*----------------------------------------------------------------------------*/
178
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_kbd_t *kbd_dev = (usb_kbd_t *)fun->driver_data;
255 if (kbd_dev == NULL) {
256 usb_log_debug("default_connection_handler: "
257 "Missing parameter.\n");
258 async_answer_0(icallid, EINVAL);
259 return;
260 }
261
262 if (method == IPC_M_CONNECT_TO_ME) {
263 int callback = IPC_GET_ARG5(*icall);
264
265 if (kbd_dev->console_phone != -1) {
266 usb_log_debug("default_connection_handler: "
267 "console phone already set\n");
268 async_answer_0(icallid, ELIMIT);
269 return;
270 }
271
272 kbd_dev->console_phone = callback;
273
274 usb_log_debug("default_connection_handler: OK\n");
275 async_answer_0(icallid, EOK);
276 return;
277 }
278
279 usb_log_debug("default_connection_handler: Wrong function.\n");
280 async_answer_0(icallid, EINVAL);
281}
282
283/*----------------------------------------------------------------------------*/
284/* Key processing functions */
285/*----------------------------------------------------------------------------*/
286/**
287 * Handles turning of LED lights on and off.
288 *
289 * In case of USB keyboards, the LEDs are handled in the driver, not in the
290 * device. When there should be a change (lock key was pressed), the driver
291 * uses a Set_Report request sent to the device to set the state of the LEDs.
292 *
293 * This functions sets the LED lights according to current settings of modifiers
294 * kept in the keyboard device structure.
295 *
296 * @param kbd_dev Keyboard device structure.
297 */
298static void usb_kbd_set_led(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev)
299{
300 if (kbd_dev->output_size == 0) {
301 return;
302 }
303
304 /* Reset the LED data. */
305 memset(kbd_dev->led_data, 0, kbd_dev->led_output_size * sizeof(int32_t));
306 usb_log_debug("Creating output report:\n");
307
308 usb_hid_report_field_t *field = usb_hid_report_get_sibling(
309 hid_dev->report, NULL, kbd_dev->led_path,
310 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
311 USB_HID_REPORT_TYPE_OUTPUT);
312
313 while (field != NULL) {
314
315 if ((field->usage == USB_HID_LED_NUM_LOCK)
316 && (kbd_dev->mods & KM_NUM_LOCK)){
317 field->value = 1;
318 }
319
320 if ((field->usage == USB_HID_LED_CAPS_LOCK)
321 && (kbd_dev->mods & KM_CAPS_LOCK)){
322 field->value = 1;
323 }
324
325 if ((field->usage == USB_HID_LED_SCROLL_LOCK)
326 && (kbd_dev->mods & KM_SCROLL_LOCK)){
327 field->value = 1;
328 }
329
330 field = usb_hid_report_get_sibling(hid_dev->report, field,
331 kbd_dev->led_path,
332 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
333 USB_HID_REPORT_TYPE_OUTPUT);
334 }
335
336 // TODO: what about the Report ID?
337 int rc = usb_hid_report_output_translate(hid_dev->report, 0,
338 kbd_dev->output_buffer, kbd_dev->output_size);
339
340 if (rc != EOK) {
341 usb_log_warning("Error translating LED output to output report"
342 ".\n");
343 return;
344 }
345
346 usb_log_debug("Output report buffer: %s\n",
347 usb_debug_str_buffer(kbd_dev->output_buffer, kbd_dev->output_size,
348 0));
349
350 usbhid_req_set_report(&hid_dev->usb_dev->ctrl_pipe,
351 hid_dev->usb_dev->interface_no, USB_HID_REPORT_TYPE_OUTPUT,
352 kbd_dev->output_buffer, kbd_dev->output_size);
353}
354
355/*----------------------------------------------------------------------------*/
356/**
357 * Processes key events.
358 *
359 * @note This function was copied from AT keyboard driver and modified to suit
360 * USB keyboard.
361 *
362 * @note Lock keys are not sent to the console, as they are completely handled
363 * in the driver. It may, however, be required later that the driver
364 * sends also these keys to application (otherwise it cannot use those
365 * keys at all).
366 *
367 * @param kbd_dev Keyboard device structure.
368 * @param type Type of the event (press / release). Recognized values:
369 * KEY_PRESS, KEY_RELEASE
370 * @param key Key code of the key according to HID Usage Tables.
371 */
372void usb_kbd_push_ev(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev, int type,
373 unsigned int key)
374{
375 console_event_t ev;
376 unsigned mod_mask;
377
378 /*
379 * These parts are copy-pasted from the AT keyboard driver.
380 *
381 * They definitely require some refactoring, but will keep it for later
382 * when the console and keyboard system is changed in HelenOS.
383 */
384 switch (key) {
385 case KC_LCTRL: mod_mask = KM_LCTRL; break;
386 case KC_RCTRL: mod_mask = KM_RCTRL; break;
387 case KC_LSHIFT: mod_mask = KM_LSHIFT; break;
388 case KC_RSHIFT: mod_mask = KM_RSHIFT; break;
389 case KC_LALT: mod_mask = KM_LALT; break;
390 case KC_RALT: mod_mask = KM_RALT; break;
391 default: mod_mask = 0; break;
392 }
393
394 if (mod_mask != 0) {
395 if (type == KEY_PRESS)
396 kbd_dev->mods = kbd_dev->mods | mod_mask;
397 else
398 kbd_dev->mods = kbd_dev->mods & ~mod_mask;
399 }
400
401 switch (key) {
402 case KC_CAPS_LOCK: mod_mask = KM_CAPS_LOCK; break;
403 case KC_NUM_LOCK: mod_mask = KM_NUM_LOCK; break;
404 case KC_SCROLL_LOCK: mod_mask = KM_SCROLL_LOCK; break;
405 default: mod_mask = 0; break;
406 }
407
408 if (mod_mask != 0) {
409 if (type == KEY_PRESS) {
410 /*
411 * Only change lock state on transition from released
412 * to pressed. This prevents autorepeat from messing
413 * up the lock state.
414 */
415 unsigned int locks_old = kbd_dev->lock_keys;
416
417 kbd_dev->mods =
418 kbd_dev->mods ^ (mod_mask & ~kbd_dev->lock_keys);
419 kbd_dev->lock_keys = kbd_dev->lock_keys | mod_mask;
420
421 /* Update keyboard lock indicator lights. */
422 if (kbd_dev->lock_keys != locks_old
423 && hid_dev != NULL) { // ugly hack
424 usb_kbd_set_led(hid_dev, kbd_dev);
425 }
426 } else {
427 kbd_dev->lock_keys = kbd_dev->lock_keys & ~mod_mask;
428 }
429 }
430
431 if (key == KC_CAPS_LOCK || key == KC_NUM_LOCK || key == KC_SCROLL_LOCK) {
432 // do not send anything to the console, this is our business
433 return;
434 }
435
436 if (type == KEY_PRESS && (kbd_dev->mods & KM_LCTRL) && key == KC_F1) {
437 active_layout = 0;
438 layout[active_layout]->reset();
439 return;
440 }
441
442 if (type == KEY_PRESS && (kbd_dev->mods & KM_LCTRL) && key == KC_F2) {
443 active_layout = 1;
444 layout[active_layout]->reset();
445 return;
446 }
447
448 if (type == KEY_PRESS && (kbd_dev->mods & KM_LCTRL) && key == KC_F3) {
449 active_layout = 2;
450 layout[active_layout]->reset();
451 return;
452 }
453
454 ev.type = type;
455 ev.key = key;
456 ev.mods = kbd_dev->mods;
457
458 ev.c = layout[active_layout]->parse_ev(&ev);
459
460 usb_log_debug2("Sending key %d to the console\n", ev.key);
461 if (kbd_dev->console_phone < 0) {
462 usb_log_warning(
463 "Connection to console not ready, key discarded.\n");
464 return;
465 }
466
467 async_msg_4(kbd_dev->console_phone, KBD_EVENT, ev.type, ev.key,
468 ev.mods, ev.c);
469}
470
471/*----------------------------------------------------------------------------*/
472
473static inline int usb_kbd_is_lock(unsigned int key_code)
474{
475 return (key_code == KC_NUM_LOCK
476 || key_code == KC_SCROLL_LOCK
477 || key_code == KC_CAPS_LOCK);
478}
479
480/*----------------------------------------------------------------------------*/
481/**
482 * Checks if some keys were pressed or released and generates key events.
483 *
484 * An event is created only when key is pressed or released. Besides handling
485 * the events (usb_kbd_push_ev()), the auto-repeat fibril is notified about
486 * key presses and releases (see usb_kbd_repeat_start() and
487 * usb_kbd_repeat_stop()).
488 *
489 * @param kbd_dev Keyboard device structure.
490 * @param key_codes Parsed keyboard report - codes of currently pressed keys
491 * according to HID Usage Tables.
492 * @param count Number of key codes in report (size of the report).
493 *
494 * @sa usb_kbd_push_ev(), usb_kbd_repeat_start(), usb_kbd_repeat_stop()
495 */
496static void usb_kbd_check_key_changes(usb_hid_dev_t *hid_dev,
497 usb_kbd_t *kbd_dev/*, const uint8_t *key_codes, size_t count*/)
498{
499 unsigned int key;
500 unsigned int i, j;
501
502 /*
503 * First of all, check if the kbd have reported phantom state.
504 *
505 * As there is no way to distinguish keys from modifiers, we do not have
506 * a way to check that 'all keys report Error Rollover'. We thus check
507 * if there is at least one such error and in such case we ignore the
508 * whole input report.
509 */
510 i = 0;
511 while (i < kbd_dev->key_count && kbd_dev->keys[i] != ERROR_ROLLOVER) {
512 ++i;
513 }
514 if (i != kbd_dev->key_count) {
515 usb_log_debug("Phantom state occured.\n");
516 // phantom state, do nothing
517 return;
518 }
519
520 /*
521 * 1) Key releases
522 */
523 for (j = 0; j < kbd_dev->key_count; ++j) {
524 // try to find the old key in the new key list
525 i = 0;
526 while (i < kbd_dev->key_count
527 && kbd_dev->keys[i] != kbd_dev->keys_old[j]) {
528 ++i;
529 }
530
531 if (i == kbd_dev->key_count) {
532 // not found, i.e. the key was released
533 key = usbhid_parse_scancode(kbd_dev->keys_old[j]);
534 if (!usb_kbd_is_lock(key)) {
535 usb_kbd_repeat_stop(kbd_dev, key);
536 }
537 usb_kbd_push_ev(hid_dev, kbd_dev, KEY_RELEASE, key);
538 usb_log_debug2("Key released: %d\n", key);
539 } else {
540 // found, nothing happens
541 }
542 }
543
544 /*
545 * 1) Key presses
546 */
547 for (i = 0; i < kbd_dev->key_count; ++i) {
548 // try to find the new key in the old key list
549 j = 0;
550 while (j < kbd_dev->key_count
551 && kbd_dev->keys_old[j] != kbd_dev->keys[i]) {
552 ++j;
553 }
554
555 if (j == kbd_dev->key_count) {
556 // not found, i.e. new key pressed
557 key = usbhid_parse_scancode(kbd_dev->keys[i]);
558 usb_log_debug2("Key pressed: %d (keycode: %d)\n", key,
559 kbd_dev->keys[i]);
560 if (!usb_kbd_is_lock(key)) {
561 usb_kbd_repeat_start(kbd_dev, key);
562 }
563 usb_kbd_push_ev(hid_dev, kbd_dev, KEY_PRESS, key);
564 } else {
565 // found, nothing happens
566 }
567 }
568
569// usb_log_debug("Old keys: ");
570// for (i = 0; i < kbd_dev->key_count; ++i) {
571// usb_log_debug("%d ", kbd_dev->keys_old[i]);
572// }
573// usb_log_debug("\n");
574
575
576// usb_log_debug("New keys: ");
577// for (i = 0; i < kbd_dev->key_count; ++i) {
578// usb_log_debug("%d ", kbd_dev->keys[i]);
579// }
580// usb_log_debug("\n");
581
582 memcpy(kbd_dev->keys_old, kbd_dev->keys, kbd_dev->key_count * 4);
583
584 usb_log_debug2("New stored keys: ");
585 for (i = 0; i < kbd_dev->key_count; ++i) {
586 usb_log_debug2("%d ", kbd_dev->keys_old[i]);
587 }
588 usb_log_debug2("\n");
589}
590
591/*----------------------------------------------------------------------------*/
592/* Callbacks for parser */
593/*----------------------------------------------------------------------------*/
594/**
595 * Callback function for the HID report parser.
596 *
597 * This function is called by the HID report parser with the parsed report.
598 * The parsed report is used to check if any events occured (key was pressed or
599 * released, modifier was pressed or released).
600 *
601 * @param key_codes Parsed keyboard report - codes of currently pressed keys
602 * according to HID Usage Tables.
603 * @param count Number of key codes in report (size of the report).
604 * @param report_id
605 * @param arg User-specified argument. Expects pointer to the keyboard device
606 * structure representing the keyboard.
607 *
608 * @sa usb_kbd_check_key_changes(), usb_kbd_check_modifier_changes()
609 */
610//static void usb_kbd_process_keycodes(const uint8_t *key_codes, size_t count,
611// uint8_t report_id, void *arg)
612//{
613// if (arg == NULL) {
614// usb_log_warning("Missing argument in callback "
615// "usbhid_process_keycodes().\n");
616// return;
617// }
618
619// usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
620
621// if (hid_dev->data == NULL) {
622// usb_log_warning("Missing KBD device structure in callback.\n");
623// return;
624// }
625
626// usb_kbd_t *kbd_dev = (usb_kbd_t *)hid_dev->data;
627
628// usb_log_debug("Got keys from parser (report id: %u): %s\n",
629// report_id, usb_debug_str_buffer(key_codes, count, 0));
630
631// if (count != kbd_dev->key_count) {
632// usb_log_warning("Number of received keycodes (%zu) differs from"
633// " expected (%zu).\n", count, kbd_dev->key_count);
634// return;
635// }
636
637// ///usb_kbd_check_modifier_changes(kbd_dev, key_codes, count);
638// usb_kbd_check_key_changes(hid_dev, kbd_dev, key_codes, count);
639//}
640
641/*----------------------------------------------------------------------------*/
642/* General kbd functions */
643/*----------------------------------------------------------------------------*/
644/**
645 * Processes data received from the device in form of report.
646 *
647 * This function uses the HID report parser to translate the data received from
648 * the device into generic USB HID key codes and into generic modifiers bitmap.
649 * The parser then calls the given callback (usb_kbd_process_keycodes()).
650 *
651 * @note Currently, only the boot protocol is supported.
652 *
653 * @param kbd_dev Keyboard device structure (must be initialized).
654 * @param buffer Data from the keyboard (i.e. the report).
655 * @param actual_size Size of the data from keyboard (report size) in bytes.
656 *
657 * @sa usb_kbd_process_keycodes(), usb_hid_boot_keyboard_input_report(),
658 * usb_hid_parse_report().
659 */
660static void usb_kbd_process_data(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev,
661 uint8_t *buffer, size_t actual_size)
662{
663 assert(hid_dev->report != NULL);
664 assert(hid_dev != NULL);
665 assert(kbd_dev != NULL);
666
667 usb_log_debug("Calling usb_hid_parse_report() with "
668 "buffer %s\n", usb_debug_str_buffer(buffer, actual_size, 0));
669
670// int rc = usb_hid_boot_keyboard_input_report(buffer, actual_size,
671// callbacks, kbd_dev);
672 usb_hid_report_path_t *path = usb_hid_report_path();
673 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_KEYBOARD, 0);
674 //usb_hid_report_path_set_report_id(path, 0);
675
676 uint8_t report_id;
677 int rc = usb_hid_parse_report(hid_dev->report, buffer, actual_size,
678 &report_id);
679
680 if (rc != EOK) {
681 usb_log_warning("Error in usb_hid_parse_report():"
682 "%s\n", str_error(rc));
683 }
684
685 usb_hid_report_path_set_report_id (path, report_id);
686
687 // fill in the currently pressed keys
688
689 usb_hid_report_field_t *field = usb_hid_report_get_sibling(
690 hid_dev->report, NULL, path,
691 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
692 USB_HID_REPORT_TYPE_INPUT);
693 unsigned i = 0;
694
695 while (field != NULL) {
696 usb_log_debug2("FIELD (%p) - VALUE(%d) USAGE(%u)\n",
697 field, field->value, field->usage);
698
699 assert(i < kbd_dev->key_count);
700// if (i == kbd_dev->key_count) {
701// break;
702// }
703
704 // save the key usage
705 /* TODO: maybe it's not good to save value, nor usage
706 * as the value may be e.g. 1 for LEDs and usage may be
707 * value of the LED. On the other hand, in case of normal
708 * keys, the usage is more important and we must check
709 * that. One possible solution: distinguish between those
710 * two parts of the Report somehow.
711 */
712 if (field->value != 0) {
713 kbd_dev->keys[i] = field->usage;
714 }
715 else {
716 kbd_dev->keys[i] = 0;
717 }
718 usb_log_debug2("Saved %u. key usage %d\n", i, kbd_dev->keys[i]);
719
720 ++i;
721 field = usb_hid_report_get_sibling(hid_dev->report, field, path,
722 USB_HID_PATH_COMPARE_END
723 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
724 USB_HID_REPORT_TYPE_INPUT);
725 }
726
727 usb_hid_report_path_free(path);
728
729 usb_kbd_check_key_changes(hid_dev, kbd_dev);
730}
731
732/*----------------------------------------------------------------------------*/
733/* HID/KBD structure manipulation */
734/*----------------------------------------------------------------------------*/
735
736static void usb_kbd_mark_unusable(usb_kbd_t *kbd_dev)
737{
738 kbd_dev->initialized = USB_KBD_STATUS_TO_DESTROY;
739}
740
741/*----------------------------------------------------------------------------*/
742
743/**
744 * Creates a new USB/HID keyboard structure.
745 *
746 * The structure returned by this function is not initialized. Use
747 * usb_kbd_init() to initialize it prior to polling.
748 *
749 * @return New uninitialized structure for representing a USB/HID keyboard or
750 * NULL if not successful (memory error).
751 */
752static usb_kbd_t *usb_kbd_new(void)
753{
754 usb_kbd_t *kbd_dev =
755 (usb_kbd_t *)calloc(1, sizeof(usb_kbd_t));
756
757 if (kbd_dev == NULL) {
758 usb_log_fatal("No memory!\n");
759 return NULL;
760 }
761
762 kbd_dev->console_phone = -1;
763 kbd_dev->initialized = USB_KBD_STATUS_UNINITIALIZED;
764
765 return kbd_dev;
766}
767
768/*----------------------------------------------------------------------------*/
769
770static int usb_kbd_create_function(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev)
771{
772 assert(hid_dev != NULL);
773 assert(hid_dev->usb_dev != NULL);
774 assert(kbd_dev != NULL);
775
776 /* Create the function exposed under /dev/devices. */
777 usb_log_debug("Creating DDF function %s...\n", HID_KBD_FUN_NAME);
778 ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed,
779 HID_KBD_FUN_NAME);
780 if (fun == NULL) {
781 usb_log_error("Could not create DDF function node.\n");
782 return ENOMEM;
783 }
784
785 /*
786 * Store the initialized HID device and HID ops
787 * to the DDF function.
788 */
789 fun->ops = &kbd_dev->ops;
790 fun->driver_data = kbd_dev;
791
792 int rc = ddf_fun_bind(fun);
793 if (rc != EOK) {
794 usb_log_error("Could not bind DDF function: %s.\n",
795 str_error(rc));
796 ddf_fun_destroy(fun);
797 return rc;
798 }
799
800 usb_log_debug("Adding DDF function to class %s...\n",
801 HID_KBD_CLASS_NAME);
802 rc = ddf_fun_add_to_class(fun, HID_KBD_CLASS_NAME);
803 if (rc != EOK) {
804 usb_log_error(
805 "Could not add DDF function to class %s: %s.\n",
806 HID_KBD_CLASS_NAME, str_error(rc));
807 ddf_fun_destroy(fun);
808 return rc;
809 }
810
811 return EOK;
812}
813
814/*----------------------------------------------------------------------------*/
815/* API functions */
816/*----------------------------------------------------------------------------*/
817/**
818 * Initialization of the USB/HID keyboard structure.
819 *
820 * This functions initializes required structures from the device's descriptors.
821 *
822 * During initialization, the keyboard is switched into boot protocol, the idle
823 * rate is set to 0 (infinity), resulting in the keyboard only reporting event
824 * when a key is pressed or released. Finally, the LED lights are turned on
825 * according to the default setup of lock keys.
826 *
827 * @note By default, the keyboards is initialized with Num Lock turned on and
828 * other locks turned off.
829 *
830 * @param kbd_dev Keyboard device structure to be initialized.
831 * @param dev DDF device structure of the keyboard.
832 *
833 * @retval EOK if successful.
834 * @retval EINVAL if some parameter is not given.
835 * @return Other value inherited from function usbhid_dev_init().
836 */
837int usb_kbd_init(usb_hid_dev_t *hid_dev, void **data)
838{
839 usb_log_debug("Initializing HID/KBD structure...\n");
840
841 if (hid_dev == NULL) {
842 usb_log_error("Failed to init keyboard structure: no structure"
843 " given.\n");
844 return EINVAL;
845 }
846
847 usb_kbd_t *kbd_dev = usb_kbd_new();
848 if (kbd_dev == NULL) {
849 usb_log_error("Error while creating USB/HID KBD device "
850 "structure.\n");
851 return ENOMEM; // TODO: some other code??
852 }
853
854 /*
855 * TODO: make more general
856 */
857 usb_hid_report_path_t *path = usb_hid_report_path();
858 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_KEYBOARD, 0);
859
860 usb_hid_report_path_set_report_id(path, 0);
861
862 kbd_dev->key_count = usb_hid_report_size(
863 hid_dev->report, 0, USB_HID_REPORT_TYPE_INPUT);
864 usb_hid_report_path_free(path);
865
866 usb_log_debug("Size of the input report: %zu\n", kbd_dev->key_count);
867
868 kbd_dev->keys = (int32_t *)calloc(kbd_dev->key_count, sizeof(int32_t));
869
870 if (kbd_dev->keys == NULL) {
871 usb_log_fatal("No memory!\n");
872 free(kbd_dev);
873 return ENOMEM;
874 }
875
876 kbd_dev->keys_old =
877 (int32_t *)calloc(kbd_dev->key_count, sizeof(int32_t));
878
879 if (kbd_dev->keys_old == NULL) {
880 usb_log_fatal("No memory!\n");
881 free(kbd_dev->keys);
882 free(kbd_dev);
883 return ENOMEM;
884 }
885
886 /*
887 * Output report
888 */
889 kbd_dev->output_size = 0;
890 kbd_dev->output_buffer = usb_hid_report_output(hid_dev->report,
891 &kbd_dev->output_size, 0);
892 if (kbd_dev->output_buffer == NULL) {
893 usb_log_warning("Error creating output report buffer.\n");
894 free(kbd_dev->keys);
895 return ENOMEM; /* TODO: other error code */
896 }
897
898 usb_log_debug("Output buffer size: %zu\n", kbd_dev->output_size);
899
900 kbd_dev->led_path = usb_hid_report_path();
901 usb_hid_report_path_append_item(
902 kbd_dev->led_path, USB_HIDUT_PAGE_LED, 0);
903
904 kbd_dev->led_output_size = usb_hid_report_size(hid_dev->report,
905 0, USB_HID_REPORT_TYPE_OUTPUT);
906
907 usb_log_debug("Output report size (in items): %zu\n",
908 kbd_dev->led_output_size);
909
910 kbd_dev->led_data = (int32_t *)calloc(
911 kbd_dev->led_output_size, sizeof(int32_t));
912
913 if (kbd_dev->led_data == NULL) {
914 usb_log_warning("Error creating buffer for LED output report."
915 "\n");
916 free(kbd_dev->keys);
917 usb_hid_report_output_free(kbd_dev->output_buffer);
918 free(kbd_dev);
919 return ENOMEM;
920 }
921
922 /*
923 * Modifiers and locks
924 */
925 kbd_dev->modifiers = 0;
926 kbd_dev->mods = DEFAULT_ACTIVE_MODS;
927 kbd_dev->lock_keys = 0;
928
929 /*
930 * Autorepeat
931 */
932 kbd_dev->repeat.key_new = 0;
933 kbd_dev->repeat.key_repeated = 0;
934 kbd_dev->repeat.delay_before = DEFAULT_DELAY_BEFORE_FIRST_REPEAT;
935 kbd_dev->repeat.delay_between = DEFAULT_REPEAT_DELAY;
936
937 kbd_dev->repeat_mtx = (fibril_mutex_t *)(
938 malloc(sizeof(fibril_mutex_t)));
939 if (kbd_dev->repeat_mtx == NULL) {
940 usb_log_fatal("No memory!\n");
941 free(kbd_dev->keys);
942 usb_hid_report_output_free(kbd_dev->output_buffer);
943 free(kbd_dev);
944 return ENOMEM;
945 }
946
947 fibril_mutex_initialize(kbd_dev->repeat_mtx);
948
949 // save the KBD device structure into the HID device structure
950 //hid_dev->data = kbd_dev;
951 *data = kbd_dev;
952
953 // set handler for incoming calls
954 kbd_dev->ops.default_handler = default_connection_handler;
955
956 /*
957 * Set LEDs according to initial setup.
958 * Set Idle rate
959 */
960 usb_kbd_set_led(hid_dev, kbd_dev);
961
962 usbhid_req_set_idle(&hid_dev->usb_dev->ctrl_pipe,
963 hid_dev->usb_dev->interface_no, IDLE_RATE);
964
965 /*
966 * Create new fibril for auto-repeat
967 */
968 fid_t fid = fibril_create(usb_kbd_repeat_fibril, kbd_dev);
969 if (fid == 0) {
970 usb_log_error("Failed to start fibril for KBD auto-repeat");
971 return ENOMEM;
972 }
973 fibril_add_ready(fid);
974
975 kbd_dev->initialized = USB_KBD_STATUS_INITIALIZED;
976 usb_log_debug("HID/KBD device structure initialized.\n");
977
978 usb_log_debug("Creating KBD function...\n");
979 int rc = usb_kbd_create_function(hid_dev, kbd_dev);
980 if (rc != EOK) {
981 usb_kbd_free(&kbd_dev);
982 return rc;
983 }
984
985 return EOK;
986}
987
988/*----------------------------------------------------------------------------*/
989
990bool usb_kbd_polling_callback(usb_hid_dev_t *hid_dev, void *data,
991 uint8_t *buffer, size_t buffer_size)
992{
993 if (hid_dev == NULL || buffer == NULL || data == NULL) {
994 // do not continue polling (???)
995 return false;
996 }
997
998 usb_kbd_t *kbd_dev = (usb_kbd_t *)data;
999 assert(kbd_dev != NULL);
1000
1001 // TODO: add return value from this function
1002 usb_kbd_process_data(hid_dev, kbd_dev, buffer, buffer_size);
1003
1004 return true;
1005}
1006
1007/*----------------------------------------------------------------------------*/
1008
1009int usb_kbd_is_initialized(const usb_kbd_t *kbd_dev)
1010{
1011 return (kbd_dev->initialized == USB_KBD_STATUS_INITIALIZED);
1012}
1013
1014/*----------------------------------------------------------------------------*/
1015
1016int usb_kbd_is_ready_to_destroy(const usb_kbd_t *kbd_dev)
1017{
1018 return (kbd_dev->initialized == USB_KBD_STATUS_TO_DESTROY);
1019}
1020
1021/*----------------------------------------------------------------------------*/
1022/**
1023 * Properly destroys the USB/HID keyboard structure.
1024 *
1025 * @param kbd_dev Pointer to the structure to be destroyed.
1026 */
1027void usb_kbd_free(usb_kbd_t **kbd_dev)
1028{
1029 if (kbd_dev == NULL || *kbd_dev == NULL) {
1030 return;
1031 }
1032
1033 // hangup phone to the console
1034 async_hangup((*kbd_dev)->console_phone);
1035
1036 if ((*kbd_dev)->repeat_mtx != NULL) {
1037 /* TODO: replace by some check and wait */
1038 assert(!fibril_mutex_is_locked((*kbd_dev)->repeat_mtx));
1039 free((*kbd_dev)->repeat_mtx);
1040 }
1041
1042 // free all buffers
1043 if ((*kbd_dev)->keys != NULL) {
1044 free((*kbd_dev)->keys);
1045 }
1046 if ((*kbd_dev)->keys_old != NULL) {
1047 free((*kbd_dev)->keys_old);
1048 }
1049 if ((*kbd_dev)->led_data != NULL) {
1050 free((*kbd_dev)->led_data);
1051 }
1052 if ((*kbd_dev)->led_path != NULL) {
1053 usb_hid_report_path_free((*kbd_dev)->led_path);
1054 }
1055 if ((*kbd_dev)->output_buffer != NULL) {
1056 usb_hid_report_output_free((*kbd_dev)->output_buffer);
1057 }
1058
1059 free(*kbd_dev);
1060 *kbd_dev = NULL;
1061}
1062
1063/*----------------------------------------------------------------------------*/
1064
1065void usb_kbd_deinit(usb_hid_dev_t *hid_dev, void *data)
1066{
1067 if (hid_dev == NULL) {
1068 return;
1069 }
1070
1071 if (data != NULL) {
1072 usb_kbd_t *kbd_dev = (usb_kbd_t *)data;
1073 if (usb_kbd_is_initialized(kbd_dev)) {
1074 usb_kbd_mark_unusable(kbd_dev);
1075 } else {
1076 usb_kbd_free(&kbd_dev);
1077 }
1078 }
1079}
1080
1081/*----------------------------------------------------------------------------*/
1082
1083int usb_kbd_set_boot_protocol(usb_hid_dev_t *hid_dev)
1084{
1085 int rc = usb_hid_parse_report_descriptor(hid_dev->report,
1086 USB_KBD_BOOT_REPORT_DESCRIPTOR,
1087 USB_KBD_BOOT_REPORT_DESCRIPTOR_SIZE);
1088
1089 if (rc != EOK) {
1090 usb_log_error("Failed to parse boot report descriptor: %s\n",
1091 str_error(rc));
1092 return rc;
1093 }
1094
1095 rc = usbhid_req_set_protocol(&hid_dev->usb_dev->ctrl_pipe,
1096 hid_dev->usb_dev->interface_no, USB_HID_PROTOCOL_BOOT);
1097
1098 if (rc != EOK) {
1099 usb_log_warning("Failed to set boot protocol to the device: "
1100 "%s\n", str_error(rc));
1101 return rc;
1102 }
1103
1104 return EOK;
1105}
1106
1107/**
1108 * @}
1109 */
Note: See TracBrowser for help on using the repository browser.