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

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

Creating DDF functions in subdrivers + saving input report.

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