source: mainline/uspace/drv/usbkbd/kbddev.c@ 85fa1e1

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

HID parser output report API used to create output reports.

+ Added constants for LED Page of HID Usage Tables

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