source: mainline/uspace/drv/usbkbd/kbddev.c@ 7787dd81

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

Merged fix to output report creation (+ change in reseting LED array).

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