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

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

new report structure fix

  • 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");
[ef354b6]291
292 usb_hid_report_output_set_data(kbd_dev->parser, kbd_dev->led_path,
293 USB_HID_PATH_COMPARE_END , kbd_dev->led_data,
294 kbd_dev->led_output_size);
[175ad13e]295 int rc = usb_hid_report_output_translate(kbd_dev->parser, 0,
296 kbd_dev->output_buffer, kbd_dev->output_size);
[85fa1e1]297
298 if (rc != EOK) {
299 usb_log_warning("Error translating LED output to output report"
300 ".\n");
[2391aaf]301 return;
302 }
303
[85fa1e1]304 usb_log_debug("Output report buffer: %s\n",
305 usb_debug_str_buffer(kbd_dev->output_buffer, kbd_dev->output_size,
306 0));
[f8e4cb6]307
308 usbhid_req_set_report(&kbd_dev->usb_dev->ctrl_pipe,
309 kbd_dev->usb_dev->interface_no, USB_HID_REPORT_TYPE_OUTPUT,
[85fa1e1]310 kbd_dev->output_buffer, kbd_dev->output_size);
[2391aaf]311}
312
313/*----------------------------------------------------------------------------*/
[17ada7a]314/**
315 * Processes key events.
316 *
317 * @note This function was copied from AT keyboard driver and modified to suit
318 * USB keyboard.
319 *
320 * @note Lock keys are not sent to the console, as they are completely handled
321 * in the driver. It may, however, be required later that the driver
322 * sends also these keys to application (otherwise it cannot use those
323 * keys at all).
324 *
325 * @param kbd_dev Keyboard device structure.
326 * @param type Type of the event (press / release). Recognized values:
327 * KEY_PRESS, KEY_RELEASE
328 * @param key Key code of the key according to HID Usage Tables.
329 */
[252e30c]330void usb_kbd_push_ev(usb_kbd_t *kbd_dev, int type, unsigned int key)
[2391aaf]331{
332 console_event_t ev;
333 unsigned mod_mask;
334
[48d2765]335 /*
336 * These parts are copy-pasted from the AT keyboard driver.
337 *
338 * They definitely require some refactoring, but will keep it for later
339 * when the console and keyboard system is changed in HelenOS.
340 */
[2391aaf]341 switch (key) {
342 case KC_LCTRL: mod_mask = KM_LCTRL; break;
343 case KC_RCTRL: mod_mask = KM_RCTRL; break;
344 case KC_LSHIFT: mod_mask = KM_LSHIFT; break;
345 case KC_RSHIFT: mod_mask = KM_RSHIFT; break;
346 case KC_LALT: mod_mask = KM_LALT; break;
347 case KC_RALT: mod_mask = KM_RALT; break;
348 default: mod_mask = 0; break;
349 }
350
351 if (mod_mask != 0) {
352 if (type == KEY_PRESS)
353 kbd_dev->mods = kbd_dev->mods | mod_mask;
354 else
355 kbd_dev->mods = kbd_dev->mods & ~mod_mask;
356 }
357
358 switch (key) {
359 case KC_CAPS_LOCK: mod_mask = KM_CAPS_LOCK; break;
360 case KC_NUM_LOCK: mod_mask = KM_NUM_LOCK; break;
361 case KC_SCROLL_LOCK: mod_mask = KM_SCROLL_LOCK; break;
362 default: mod_mask = 0; break;
363 }
364
365 if (mod_mask != 0) {
366 if (type == KEY_PRESS) {
367 /*
368 * Only change lock state on transition from released
369 * to pressed. This prevents autorepeat from messing
370 * up the lock state.
371 */
[dfe53af]372 unsigned int locks_old = kbd_dev->lock_keys;
373
[2391aaf]374 kbd_dev->mods =
375 kbd_dev->mods ^ (mod_mask & ~kbd_dev->lock_keys);
376 kbd_dev->lock_keys = kbd_dev->lock_keys | mod_mask;
377
378 /* Update keyboard lock indicator lights. */
[dfe53af]379 if (kbd_dev->lock_keys != locks_old) {
[252e30c]380 usb_kbd_set_led(kbd_dev);
[dfe53af]381 }
[2391aaf]382 } else {
383 kbd_dev->lock_keys = kbd_dev->lock_keys & ~mod_mask;
384 }
385 }
386
[ac8285d]387 if (key == KC_CAPS_LOCK || key == KC_NUM_LOCK || key == KC_SCROLL_LOCK) {
[1c6c4092]388 // do not send anything to the console, this is our business
389 return;
390 }
391
[2391aaf]392 if (type == KEY_PRESS && (kbd_dev->mods & KM_LCTRL) && key == KC_F1) {
393 active_layout = 0;
394 layout[active_layout]->reset();
395 return;
396 }
397
398 if (type == KEY_PRESS && (kbd_dev->mods & KM_LCTRL) && key == KC_F2) {
399 active_layout = 1;
400 layout[active_layout]->reset();
401 return;
402 }
403
404 if (type == KEY_PRESS && (kbd_dev->mods & KM_LCTRL) && key == KC_F3) {
405 active_layout = 2;
406 layout[active_layout]->reset();
407 return;
408 }
409
410 ev.type = type;
411 ev.key = key;
412 ev.mods = kbd_dev->mods;
413
414 ev.c = layout[active_layout]->parse_ev(&ev);
415
416 usb_log_debug2("Sending key %d to the console\n", ev.key);
[7a2f8ea0]417 if (kbd_dev->console_phone < 0) {
418 usb_log_warning(
419 "Connection to console not ready, key discarded.\n");
420 return;
421 }
[2391aaf]422
423 async_msg_4(kbd_dev->console_phone, KBD_EVENT, ev.type, ev.key,
424 ev.mods, ev.c);
425}
426
427/*----------------------------------------------------------------------------*/
[fc5ed5d]428
[252e30c]429static inline int usb_kbd_is_lock(unsigned int key_code)
[2391aaf]430{
[fc5ed5d]431 return (key_code == KC_NUM_LOCK
432 || key_code == KC_SCROLL_LOCK
433 || key_code == KC_CAPS_LOCK);
[2391aaf]434}
435
436/*----------------------------------------------------------------------------*/
[17ada7a]437/**
438 * Checks if some keys were pressed or released and generates key events.
439 *
440 * An event is created only when key is pressed or released. Besides handling
[252e30c]441 * the events (usb_kbd_push_ev()), the auto-repeat fibril is notified about
442 * key presses and releases (see usb_kbd_repeat_start() and
443 * usb_kbd_repeat_stop()).
[17ada7a]444 *
445 * @param kbd_dev Keyboard device structure.
446 * @param key_codes Parsed keyboard report - codes of currently pressed keys
447 * according to HID Usage Tables.
448 * @param count Number of key codes in report (size of the report).
449 *
[252e30c]450 * @sa usb_kbd_push_ev(), usb_kbd_repeat_start(), usb_kbd_repeat_stop()
[17ada7a]451 */
[252e30c]452static void usb_kbd_check_key_changes(usb_kbd_t *kbd_dev,
[17ada7a]453 const uint8_t *key_codes, size_t count)
[2391aaf]454{
455 unsigned int key;
456 unsigned int i, j;
457
[d477734]458 /*
459 * First of all, check if the kbd have reported phantom state.
[fc5ed5d]460 *
[e4153ea]461 * As there is no way to distinguish keys from modifiers, we do not have
462 * a way to check that 'all keys report Error Rollover'. We thus check
463 * if there is at least one such error and in such case we ignore the
464 * whole input report.
[d477734]465 */
466 i = 0;
[e4153ea]467 while (i < count && key_codes[i] != ERROR_ROLLOVER) {
[d477734]468 ++i;
469 }
[e4153ea]470 if (i != count) {
[d477734]471 usb_log_debug("Phantom state occured.\n");
472 // phantom state, do nothing
473 return;
474 }
475
[17ada7a]476 /* TODO: quite dummy right now, think of better implementation */
477 assert(count == kbd_dev->key_count);
[2391aaf]478
479 /*
480 * 1) Key releases
481 */
[17ada7a]482 for (j = 0; j < count; ++j) {
[2391aaf]483 // try to find the old key in the new key list
484 i = 0;
[48d2765]485 while (i < kbd_dev->key_count
486 && key_codes[i] != kbd_dev->keys[j]) {
[2391aaf]487 ++i;
488 }
489
[17ada7a]490 if (i == count) {
[2391aaf]491 // not found, i.e. the key was released
[48d2765]492 key = usbhid_parse_scancode(kbd_dev->keys[j]);
[252e30c]493 if (!usb_kbd_is_lock(key)) {
494 usb_kbd_repeat_stop(kbd_dev, key);
[fc5ed5d]495 }
[252e30c]496 usb_kbd_push_ev(kbd_dev, KEY_RELEASE, key);
[d477734]497 usb_log_debug2("Key released: %d\n", key);
[2391aaf]498 } else {
499 // found, nothing happens
500 }
501 }
502
503 /*
504 * 1) Key presses
505 */
[48d2765]506 for (i = 0; i < kbd_dev->key_count; ++i) {
[2391aaf]507 // try to find the new key in the old key list
508 j = 0;
[17ada7a]509 while (j < count && kbd_dev->keys[j] != key_codes[i]) {
[2391aaf]510 ++j;
511 }
512
[17ada7a]513 if (j == count) {
[2391aaf]514 // not found, i.e. new key pressed
515 key = usbhid_parse_scancode(key_codes[i]);
[d477734]516 usb_log_debug2("Key pressed: %d (keycode: %d)\n", key,
[2391aaf]517 key_codes[i]);
[252e30c]518 usb_kbd_push_ev(kbd_dev, KEY_PRESS, key);
519 if (!usb_kbd_is_lock(key)) {
520 usb_kbd_repeat_start(kbd_dev, key);
[fc5ed5d]521 }
[a8def7d]522 } else {
[2391aaf]523 // found, nothing happens
524 }
525 }
526
[17ada7a]527 memcpy(kbd_dev->keys, key_codes, count);
[6bb456c]528
529 usb_log_debug("New stored keycodes: %s\n",
[48d2765]530 usb_debug_str_buffer(kbd_dev->keys, kbd_dev->key_count, 0));
[2391aaf]531}
532
533/*----------------------------------------------------------------------------*/
534/* Callbacks for parser */
535/*----------------------------------------------------------------------------*/
[17ada7a]536/**
537 * Callback function for the HID report parser.
538 *
539 * This function is called by the HID report parser with the parsed report.
540 * The parsed report is used to check if any events occured (key was pressed or
541 * released, modifier was pressed or released).
542 *
543 * @param key_codes Parsed keyboard report - codes of currently pressed keys
544 * according to HID Usage Tables.
545 * @param count Number of key codes in report (size of the report).
546 * @param modifiers Bitmap of modifiers (Ctrl, Alt, Shift, GUI).
[7309799]547 * @param arg User-specified argument. Expects pointer to the keyboard device
548 * structure representing the keyboard.
[17ada7a]549 *
[252e30c]550 * @sa usb_kbd_check_key_changes(), usb_kbd_check_modifier_changes()
[17ada7a]551 */
[252e30c]552static void usb_kbd_process_keycodes(const uint8_t *key_codes, size_t count,
[2391aaf]553 uint8_t modifiers, void *arg)
554{
555 if (arg == NULL) {
556 usb_log_warning("Missing argument in callback "
557 "usbhid_process_keycodes().\n");
558 return;
559 }
560
[252e30c]561 usb_kbd_t *kbd_dev = (usb_kbd_t *)arg;
[2391aaf]562 assert(kbd_dev != NULL);
[7ac73a4]563
[a694a58]564 usb_log_debug("Got keys from parser (report id: %d): %s\n", modifiers,
[a8def7d]565 usb_debug_str_buffer(key_codes, count, 0));
[2391aaf]566
[48d2765]567 if (count != kbd_dev->key_count) {
[2391aaf]568 usb_log_warning("Number of received keycodes (%d) differs from"
[48d2765]569 " expected number (%d).\n", count, kbd_dev->key_count);
[2391aaf]570 return;
571 }
572
[252e30c]573 ///usb_kbd_check_modifier_changes(kbd_dev, key_codes, count);
574 usb_kbd_check_key_changes(kbd_dev, key_codes, count);
[2391aaf]575}
576
577/*----------------------------------------------------------------------------*/
578/* General kbd functions */
579/*----------------------------------------------------------------------------*/
[48d2765]580/**
581 * Processes data received from the device in form of report.
582 *
583 * This function uses the HID report parser to translate the data received from
584 * the device into generic USB HID key codes and into generic modifiers bitmap.
[252e30c]585 * The parser then calls the given callback (usb_kbd_process_keycodes()).
[48d2765]586 *
587 * @note Currently, only the boot protocol is supported.
588 *
589 * @param kbd_dev Keyboard device structure (must be initialized).
590 * @param buffer Data from the keyboard (i.e. the report).
591 * @param actual_size Size of the data from keyboard (report size) in bytes.
592 *
[252e30c]593 * @sa usb_kbd_process_keycodes(), usb_hid_boot_keyboard_input_report(),
[f8e4cb6]594 * usb_hid_parse_report().
[48d2765]595 */
[252e30c]596static void usb_kbd_process_data(usb_kbd_t *kbd_dev,
[85fa1e1]597 uint8_t *buffer, size_t actual_size)
[2391aaf]598{
[252e30c]599 assert(kbd_dev->initialized == USB_KBD_STATUS_INITIALIZED);
[f8e4cb6]600 assert(kbd_dev->parser != NULL);
[a8def7d]601
[2391aaf]602 usb_hid_report_in_callbacks_t *callbacks =
603 (usb_hid_report_in_callbacks_t *)malloc(
604 sizeof(usb_hid_report_in_callbacks_t));
605
[252e30c]606 callbacks->keyboard = usb_kbd_process_keycodes;
[2391aaf]607
[2f593872]608 usb_log_debug("Calling usb_hid_parse_report() with "
[6bb456c]609 "buffer %s\n", usb_debug_str_buffer(buffer, actual_size, 0));
[2391aaf]610
[2f593872]611// int rc = usb_hid_boot_keyboard_input_report(buffer, actual_size,
612// callbacks, kbd_dev);
[b53d3b7]613 usb_hid_report_path_t *path = usb_hid_report_path();
614 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_KEYBOARD, 0);
[a694a58]615
[175ad13e]616 int rc = usb_hid_parse_report(kbd_dev->parser, buffer, actual_size);
[ef354b6]617 usb_hid_descriptor_print (kbd_dev->parser);
[b53d3b7]618
619 usb_hid_report_path_free (path);
[2391aaf]620
621 if (rc != EOK) {
622 usb_log_warning("Error in usb_hid_boot_keyboard_input_report():"
623 "%s\n", str_error(rc));
624 }
625}
626
627/*----------------------------------------------------------------------------*/
628/* HID/KBD structure manipulation */
629/*----------------------------------------------------------------------------*/
[f8e4cb6]630
[252e30c]631static void usb_kbd_mark_unusable(usb_kbd_t *kbd_dev)
[f8e4cb6]632{
[252e30c]633 kbd_dev->initialized = USB_KBD_STATUS_TO_DESTROY;
[f8e4cb6]634}
635
636
637/*----------------------------------------------------------------------------*/
638/* API functions */
639/*----------------------------------------------------------------------------*/
[48d2765]640/**
641 * Creates a new USB/HID keyboard structure.
642 *
643 * The structure returned by this function is not initialized. Use
[252e30c]644 * usb_kbd_init() to initialize it prior to polling.
[48d2765]645 *
646 * @return New uninitialized structure for representing a USB/HID keyboard or
647 * NULL if not successful (memory error).
648 */
[252e30c]649usb_kbd_t *usb_kbd_new(void)
[2391aaf]650{
[252e30c]651 usb_kbd_t *kbd_dev =
652 (usb_kbd_t *)malloc(sizeof(usb_kbd_t));
[2391aaf]653
654 if (kbd_dev == NULL) {
655 usb_log_fatal("No memory!\n");
656 return NULL;
657 }
658
[252e30c]659 memset(kbd_dev, 0, sizeof(usb_kbd_t));
[2391aaf]660
[175ad13e]661 kbd_dev->parser = (usb_hid_report_t *)(malloc(sizeof(
662 usb_hid_report_t)));
[82d04a48]663 if (kbd_dev->parser == NULL) {
664 usb_log_fatal("No memory!\n");
665 free(kbd_dev);
666 return NULL;
667 }
668
[27270db]669 kbd_dev->console_phone = -1;
[252e30c]670 kbd_dev->initialized = USB_KBD_STATUS_UNINITIALIZED;
[2391aaf]671
672 return kbd_dev;
673}
674
[1c6c4092]675/*----------------------------------------------------------------------------*/
[48d2765]676/**
677 * Initialization of the USB/HID keyboard structure.
678 *
679 * This functions initializes required structures from the device's descriptors.
680 *
681 * During initialization, the keyboard is switched into boot protocol, the idle
682 * rate is set to 0 (infinity), resulting in the keyboard only reporting event
683 * when a key is pressed or released. Finally, the LED lights are turned on
684 * according to the default setup of lock keys.
685 *
686 * @note By default, the keyboards is initialized with Num Lock turned on and
687 * other locks turned off.
688 *
689 * @param kbd_dev Keyboard device structure to be initialized.
690 * @param dev DDF device structure of the keyboard.
691 *
692 * @retval EOK if successful.
693 * @retval EINVAL if some parameter is not given.
694 * @return Other value inherited from function usbhid_dev_init().
695 */
[252e30c]696int usb_kbd_init(usb_kbd_t *kbd_dev, usb_device_t *dev)
[2391aaf]697{
698 int rc;
699
[fbefd0e]700 usb_log_debug("Initializing HID/KBD structure...\n");
[2391aaf]701
702 if (kbd_dev == NULL) {
703 usb_log_error("Failed to init keyboard structure: no structure"
704 " given.\n");
705 return EINVAL;
706 }
707
708 if (dev == NULL) {
[f8e4cb6]709 usb_log_error("Failed to init keyboard structure: no USB device"
[2391aaf]710 " given.\n");
711 return EINVAL;
712 }
713
[252e30c]714 if (kbd_dev->initialized == USB_KBD_STATUS_INITIALIZED) {
[2391aaf]715 usb_log_warning("Keyboard structure already initialized.\n");
716 return EINVAL;
717 }
718
[0d92638]719 /* TODO: does not work! */
[1cbfd6b]720 if (!dev->pipes[USB_KBD_POLL_EP_NO].present) {
[0d92638]721 usb_log_warning("Required endpoint not found - probably not "
722 "a supported device.\n");
723 return ENOTSUP;
724 }
[fc5ed5d]725
[f8e4cb6]726 /* The USB device should already be initialized, save it in structure */
727 kbd_dev->usb_dev = dev;
728
[82d04a48]729 /* Initialize the report parser. */
[175ad13e]730 //rc = usb_hid_parser_init(kbd_dev->parser);
731 //if (rc != EOK) {
732 // usb_log_error("Failed to initialize report parser.\n");
733 // return rc;
734 //}
[82d04a48]735
736 /* Get the report descriptor and parse it. */
[9698695]737 rc = usb_hid_process_report_descriptor(kbd_dev->usb_dev,
738 kbd_dev->parser);
739 if (rc != EOK) {
[252e30c]740 usb_log_warning("Could not process report descriptor, "
741 "falling back to boot protocol.\n");
742 rc = usb_hid_parse_report_descriptor(kbd_dev->parser,
743 BOOT_REPORT_DESCRIPTOR, BOOT_REPORT_DESCRIPTOR_SIZE);
744 if (rc != EOK) {
745 usb_log_error("Failed to parse boot report descriptor:"
746 " %s.\n", str_error(rc));
747 return rc;
748 }
749
750 rc = usbhid_req_set_protocol(&kbd_dev->usb_dev->ctrl_pipe,
751 kbd_dev->usb_dev->interface_no, USB_HID_PROTOCOL_BOOT);
752
753 if (rc != EOK) {
754 usb_log_warning("Failed to set boot protocol to the "
755 "device: %s\n", str_error(rc));
756 return rc;
757 }
[f8e4cb6]758 }
759
760 /*
761 * TODO: make more general
762 */
[0533b03]763 usb_hid_report_path_t *path = usb_hid_report_path();
[b53d3b7]764 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_KEYBOARD, 0);
[fc5ed5d]765 kbd_dev->key_count = usb_hid_report_input_length(
[dc9f122]766 kbd_dev->parser, path, USB_HID_PATH_COMPARE_END);
[b53d3b7]767 usb_hid_report_path_free (path);
[fc5ed5d]768
769 usb_log_debug("Size of the input report: %zu\n", kbd_dev->key_count);
770
[85fa1e1]771 kbd_dev->keys = (uint8_t *)calloc(kbd_dev->key_count, sizeof(uint8_t));
[2391aaf]772
[48d2765]773 if (kbd_dev->keys == NULL) {
[2391aaf]774 usb_log_fatal("No memory!\n");
[48d2765]775 return ENOMEM;
[2391aaf]776 }
777
[85fa1e1]778 /*
779 * Output report
780 */
781 kbd_dev->output_size = 0;
782 kbd_dev->output_buffer = usb_hid_report_output(kbd_dev->parser,
[a694a58]783 &kbd_dev->output_size, 0x00);
[85fa1e1]784 if (kbd_dev->output_buffer == NULL) {
785 usb_log_warning("Error creating output report buffer.\n");
786 free(kbd_dev->keys);
787 return ENOMEM; /* TODO: other error code */
788 }
789
790 usb_log_debug("Output buffer size: %zu\n", kbd_dev->output_size);
791
792 kbd_dev->led_path = usb_hid_report_path();
793 usb_hid_report_path_append_item(
794 kbd_dev->led_path, USB_HIDUT_PAGE_LED, 0);
[a694a58]795 usb_hid_report_path_set_report_id(kbd_dev->led_path, 0x00);
[85fa1e1]796
797 kbd_dev->led_output_size = usb_hid_report_output_size(kbd_dev->parser,
798 kbd_dev->led_path, USB_HID_PATH_COMPARE_END);
799
800 usb_log_debug("Output report size (in items): %zu\n",
801 kbd_dev->led_output_size);
802
803 kbd_dev->led_data = (int32_t *)calloc(
804 kbd_dev->led_output_size, sizeof(int32_t));
805
806 if (kbd_dev->led_data == NULL) {
807 usb_log_warning("Error creating buffer for LED output report."
808 "\n");
809 free(kbd_dev->keys);
810 usb_hid_report_output_free(kbd_dev->output_buffer);
811 return ENOMEM;
812 }
813
814 /*
815 * Modifiers and locks
816 */
[2391aaf]817 kbd_dev->modifiers = 0;
818 kbd_dev->mods = DEFAULT_ACTIVE_MODS;
819 kbd_dev->lock_keys = 0;
820
[85fa1e1]821 /*
822 * Autorepeat
823 */
[dfe53af]824 kbd_dev->repeat.key_new = 0;
825 kbd_dev->repeat.key_repeated = 0;
826 kbd_dev->repeat.delay_before = DEFAULT_DELAY_BEFORE_FIRST_REPEAT;
827 kbd_dev->repeat.delay_between = DEFAULT_REPEAT_DELAY;
828
829 kbd_dev->repeat_mtx = (fibril_mutex_t *)(
830 malloc(sizeof(fibril_mutex_t)));
831 if (kbd_dev->repeat_mtx == NULL) {
832 usb_log_fatal("No memory!\n");
833 free(kbd_dev->keys);
834 return ENOMEM;
835 }
836
837 fibril_mutex_initialize(kbd_dev->repeat_mtx);
838
[2391aaf]839 /*
840 * Set LEDs according to initial setup.
[7ac73a4]841 * Set Idle rate
[2391aaf]842 */
[ef354b6]843 usb_kbd_set_led(kbd_dev);
[2391aaf]844
[f8e4cb6]845 usbhid_req_set_idle(&kbd_dev->usb_dev->ctrl_pipe,
846 kbd_dev->usb_dev->interface_no, IDLE_RATE);
[7ac73a4]847
[252e30c]848 kbd_dev->initialized = USB_KBD_STATUS_INITIALIZED;
[fbefd0e]849 usb_log_debug("HID/KBD device structure initialized.\n");
[2391aaf]850
851 return EOK;
852}
853
854/*----------------------------------------------------------------------------*/
855
[252e30c]856bool usb_kbd_polling_callback(usb_device_t *dev, uint8_t *buffer,
[f8e4cb6]857 size_t buffer_size, void *arg)
[2391aaf]858{
[f8e4cb6]859 if (dev == NULL || buffer == NULL || arg == NULL) {
860 // do not continue polling (???)
861 return false;
[2391aaf]862 }
863
[252e30c]864 usb_kbd_t *kbd_dev = (usb_kbd_t *)arg;
[1c6c4092]865
[f8e4cb6]866 // TODO: add return value from this function
[252e30c]867 usb_kbd_process_data(kbd_dev, buffer, buffer_size);
[f8e4cb6]868
869 return true;
[2391aaf]870}
871
872/*----------------------------------------------------------------------------*/
873
[252e30c]874void usb_kbd_polling_ended_callback(usb_device_t *dev, bool reason,
[f8e4cb6]875 void *arg)
876{
877 if (dev == NULL || arg == NULL) {
878 return;
[2391aaf]879 }
880
[252e30c]881 usb_kbd_t *kbd = (usb_kbd_t *)arg;
[dfe53af]882
[252e30c]883 usb_kbd_mark_unusable(kbd);
[2391aaf]884}
885
[00b13408]886/*----------------------------------------------------------------------------*/
887
[252e30c]888int usb_kbd_is_initialized(const usb_kbd_t *kbd_dev)
[00b13408]889{
[252e30c]890 return (kbd_dev->initialized == USB_KBD_STATUS_INITIALIZED);
[00b13408]891}
892
[18b3cfd]893/*----------------------------------------------------------------------------*/
894
[252e30c]895int usb_kbd_is_ready_to_destroy(const usb_kbd_t *kbd_dev)
[18b3cfd]896{
[252e30c]897 return (kbd_dev->initialized == USB_KBD_STATUS_TO_DESTROY);
[18b3cfd]898}
899
[00b13408]900/*----------------------------------------------------------------------------*/
901/**
902 * Properly destroys the USB/HID keyboard structure.
903 *
904 * @param kbd_dev Pointer to the structure to be destroyed.
905 */
[252e30c]906void usb_kbd_free(usb_kbd_t **kbd_dev)
[00b13408]907{
908 if (kbd_dev == NULL || *kbd_dev == NULL) {
909 return;
910 }
911
912 // hangup phone to the console
913 async_hangup((*kbd_dev)->console_phone);
914
[f8e4cb6]915// if ((*kbd_dev)->hid_dev != NULL) {
916// usbhid_dev_free(&(*kbd_dev)->hid_dev);
917// assert((*kbd_dev)->hid_dev == NULL);
918// }
[00b13408]919
920 if ((*kbd_dev)->repeat_mtx != NULL) {
921 /* TODO: replace by some check and wait */
922 assert(!fibril_mutex_is_locked((*kbd_dev)->repeat_mtx));
923 free((*kbd_dev)->repeat_mtx);
924 }
[f8e4cb6]925
[82d04a48]926 // destroy the parser
927 if ((*kbd_dev)->parser != NULL) {
[175ad13e]928 usb_hid_free_report((*kbd_dev)->parser);
[82d04a48]929 }
930
[85fa1e1]931 // free the output buffer
932 usb_hid_report_output_free((*kbd_dev)->output_buffer);
933
[f8e4cb6]934 /* TODO: what about the USB device structure?? */
[00b13408]935
936 free(*kbd_dev);
937 *kbd_dev = NULL;
938}
939
[2391aaf]940/**
941 * @}
942 */
Note: See TracBrowser for help on using the repository browser.