source: mainline/uspace/drv/usbkbd/kbddev.c@ cf81757

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cf81757 was cf81757, checked in by Matej Klonfar <maklf@…>, 14 years ago

testing code removed

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