source: mainline/uspace/drv/usbkbd/kbddev.c@ 302a4b6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 302a4b6 was 4125b7d, checked in by Vojtech Horky <vojtechhorky@…>, 15 years ago

usb_log_printf() checks for printf correctness

It is surprising how many printf warnings simple check could
produce ;-).

Next time, it won't compile. Bad, huh?

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