source: mainline/uspace/drv/bus/usb/usbhid/kbd/kbddev.c@ 9871bca

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9871bca was b803845, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

usb: Make endpoint descriptions const.

  • Property mode set to 100644
File size: 25.2 KB
RevLine 
[61257f4]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>
[f2f99ae]42#include <io/console.h>
[5f88293]43#include <ipc/kbdev.h>
[61257f4]44#include <async.h>
45#include <fibril.h>
46#include <fibril_synch.h>
47
[313775b]48#include <ddf/log.h>
49
[61257f4]50#include <usb/usb.h>
[7d521e24]51#include <usb/dev/dp.h>
52#include <usb/dev/request.h>
[faa44e58]53#include <usb/hid/hid.h>
[7d521e24]54#include <usb/dev/pipes.h>
[61257f4]55#include <usb/debug.h>
[faa44e58]56#include <usb/hid/hidparser.h>
[61257f4]57#include <usb/classes/classes.h>
[faa44e58]58#include <usb/hid/usages/core.h>
59#include <usb/hid/request.h>
60#include <usb/hid/hidreport.h>
61#include <usb/hid/usages/led.h>
[61257f4]62
[7d521e24]63#include <usb/dev/driver.h>
[61257f4]64
65#include "kbddev.h"
66
67#include "conv.h"
68#include "kbdrepeat.h"
69
70#include "../usbhid.h"
71
72/*----------------------------------------------------------------------------*/
[f2f99ae]73
[61257f4]74static const unsigned DEFAULT_ACTIVE_MODS = KM_NUM_LOCK;
75
76static const uint8_t ERROR_ROLLOVER = 1;
77
78/** Default idle rate for keyboards. */
79static const uint8_t IDLE_RATE = 0;
80
81/** Delay before a pressed key starts auto-repeating. */
82static const unsigned int DEFAULT_DELAY_BEFORE_FIRST_REPEAT = 500 * 1000;
83
84/** Delay between two repeats of a pressed key when auto-repeating. */
85static const unsigned int DEFAULT_REPEAT_DELAY = 50 * 1000;
86
87/*----------------------------------------------------------------------------*/
88
89/** Keyboard polling endpoint description for boot protocol class. */
[b803845]90const usb_endpoint_description_t usb_hid_kbd_poll_endpoint_description = {
[61257f4]91 .transfer_type = USB_TRANSFER_INTERRUPT,
92 .direction = USB_DIRECTION_IN,
93 .interface_class = USB_CLASS_HID,
94 .interface_subclass = USB_HID_SUBCLASS_BOOT,
95 .interface_protocol = USB_HID_PROTOCOL_KEYBOARD,
96 .flags = 0
97};
98
99const char *HID_KBD_FUN_NAME = "keyboard";
[1dc4a5e]100const char *HID_KBD_CATEGORY_NAME = "keyboard";
[61257f4]101
[60e5a856]102static void usb_kbd_set_led(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev);
103
[61257f4]104/*----------------------------------------------------------------------------*/
105
106enum {
107 USB_KBD_BOOT_REPORT_DESCRIPTOR_SIZE = 63
108};
109
110static const uint8_t USB_KBD_BOOT_REPORT_DESCRIPTOR[
111 USB_KBD_BOOT_REPORT_DESCRIPTOR_SIZE] = {
112 0x05, 0x01, // Usage Page (Generic Desktop),
113 0x09, 0x06, // Usage (Keyboard),
114 0xA1, 0x01, // Collection (Application),
115 0x75, 0x01, // Report Size (1),
116 0x95, 0x08, // Report Count (8),
117 0x05, 0x07, // Usage Page (Key Codes);
118 0x19, 0xE0, // Usage Minimum (224),
119 0x29, 0xE7, // Usage Maximum (231),
120 0x15, 0x00, // Logical Minimum (0),
121 0x25, 0x01, // Logical Maximum (1),
122 0x81, 0x02, // Input (Data, Variable, Absolute), ; Modifier byte
123 0x95, 0x01, // Report Count (1),
124 0x75, 0x08, // Report Size (8),
125 0x81, 0x01, // Input (Constant), ; Reserved byte
126 0x95, 0x05, // Report Count (5),
127 0x75, 0x01, // Report Size (1),
128 0x05, 0x08, // Usage Page (Page# for LEDs),
129 0x19, 0x01, // Usage Minimum (1),
130 0x29, 0x05, // Usage Maxmimum (5),
131 0x91, 0x02, // Output (Data, Variable, Absolute), ; LED report
132 0x95, 0x01, // Report Count (1),
133 0x75, 0x03, // Report Size (3),
134 0x91, 0x01, // Output (Constant), ; LED report padding
135 0x95, 0x06, // Report Count (6),
136 0x75, 0x08, // Report Size (8),
137 0x15, 0x00, // Logical Minimum (0),
138 0x25, 0xff, // Logical Maximum (255),
139 0x05, 0x07, // Usage Page (Key Codes),
140 0x19, 0x00, // Usage Minimum (0),
141 0x29, 0xff, // Usage Maximum (255),
142 0x81, 0x00, // Input (Data, Array), ; Key arrays (6 bytes)
143 0xC0 // End Collection
144
145};
146
147/*----------------------------------------------------------------------------*/
148
149typedef enum usb_kbd_flags {
150 USB_KBD_STATUS_UNINITIALIZED = 0,
151 USB_KBD_STATUS_INITIALIZED = 1,
152 USB_KBD_STATUS_TO_DESTROY = -1
153} usb_kbd_flags;
154
155/*----------------------------------------------------------------------------*/
156/* IPC method handler */
157/*----------------------------------------------------------------------------*/
158
159static void default_connection_handler(ddf_fun_t *, ipc_callid_t, ipc_call_t *);
160
[46b60e6]161/**
[61257f4]162 * Default handler for IPC methods not handled by DDF.
163 *
164 * Currently recognizes only one method (IPC_M_CONNECT_TO_ME), in which case it
[5da7199]165 * assumes the caller is the console and thus it stores IPC session to it for
[61257f4]166 * later use by the driver to notify about key events.
167 *
168 * @param fun Device function handling the call.
169 * @param icallid Call id.
170 * @param icall Call data.
171 */
[74a1ba9]172static void default_connection_handler(ddf_fun_t *fun,
[61257f4]173 ipc_callid_t icallid, ipc_call_t *icall)
174{
175 sysarg_t method = IPC_GET_IMETHOD(*icall);
[cc29622]176
[5da7199]177 usb_kbd_t *kbd_dev = (usb_kbd_t *) fun->driver_data;
[65b458c4]178 if (kbd_dev == NULL) {
[f8e549b]179 usb_log_debug("default_connection_handler: "
180 "Missing parameter.\n");
[73ae3373]181 async_answer_0(icallid, EINVAL);
182 return;
183 }
[cc29622]184
[5da7199]185 async_sess_t *sess =
186 async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
187 if (sess != NULL) {
188 if (kbd_dev->console_sess == NULL) {
189 kbd_dev->console_sess = sess;
190 usb_log_debug("default_connection_handler: OK\n");
191 async_answer_0(icallid, EOK);
192 } else {
[f8e549b]193 usb_log_debug("default_connection_handler: "
[5da7199]194 "console session already set\n");
[61257f4]195 async_answer_0(icallid, ELIMIT);
196 }
[5da7199]197 } else {
198 switch (method) {
199 case KBDEV_SET_IND:
200 kbd_dev->mods = IPC_GET_ARG1(*icall);
201 usb_kbd_set_led(kbd_dev->hid_dev, kbd_dev);
202 async_answer_0(icallid, EOK);
203 break;
204 default:
205 usb_log_debug("default_connection_handler: Wrong function.\n");
206 async_answer_0(icallid, EINVAL);
207 break;
208 }
[61257f4]209 }
210}
211
212/*----------------------------------------------------------------------------*/
213/* Key processing functions */
214/*----------------------------------------------------------------------------*/
215/**
216 * Handles turning of LED lights on and off.
217 *
[46b60e6]218 * As with most other keyboards, the LED indicators in USB keyboards are
219 * driven by software. When state of some modifier changes, the input server
220 * will call us and tell us to update the LED state and what the new state
221 * should be.
[61257f4]222 *
223 * This functions sets the LED lights according to current settings of modifiers
224 * kept in the keyboard device structure.
225 *
226 * @param kbd_dev Keyboard device structure.
227 */
228static void usb_kbd_set_led(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev)
229{
230 if (kbd_dev->output_size == 0) {
231 return;
232 }
[19e0560e]233
[61257f4]234 /* Reset the LED data. */
235 memset(kbd_dev->led_data, 0, kbd_dev->led_output_size * sizeof(int32_t));
[cfbbe1d3]236 usb_log_debug("Creating output report:\n");
[61257f4]237
[6513110]238 usb_hid_report_field_t *field = usb_hid_report_get_sibling(
[a8c4e871]239 &hid_dev->report, NULL, kbd_dev->led_path,
[3a6e423]240 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
[6513110]241 USB_HID_REPORT_TYPE_OUTPUT);
[cc29622]242
[46b60e6]243 while (field != NULL) {
[a8c4e871]244
245 if ((field->usage == USB_HID_LED_NUM_LOCK)
[6513110]246 && (kbd_dev->mods & KM_NUM_LOCK)){
[cfbbe1d3]247 field->value = 1;
248 }
249
[a8c4e871]250 if ((field->usage == USB_HID_LED_CAPS_LOCK)
[6513110]251 && (kbd_dev->mods & KM_CAPS_LOCK)){
[cfbbe1d3]252 field->value = 1;
253 }
254
[a8c4e871]255 if ((field->usage == USB_HID_LED_SCROLL_LOCK)
[6513110]256 && (kbd_dev->mods & KM_SCROLL_LOCK)){
[cfbbe1d3]257 field->value = 1;
258 }
[a8c4e871]259
260 field = usb_hid_report_get_sibling(
261 &hid_dev->report, field, kbd_dev->led_path,
262 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
263 USB_HID_REPORT_TYPE_OUTPUT);
[cfbbe1d3]264 }
[cc29622]265
[6513110]266 // TODO: what about the Report ID?
[a8c4e871]267 int rc = usb_hid_report_output_translate(&hid_dev->report, 0,
[e50cd7f]268 kbd_dev->output_buffer, kbd_dev->output_size);
[cc29622]269
[61257f4]270 if (rc != EOK) {
271 usb_log_warning("Error translating LED output to output report"
272 ".\n");
273 return;
274 }
[cc29622]275
[61257f4]276 usb_log_debug("Output report buffer: %s\n",
277 usb_debug_str_buffer(kbd_dev->output_buffer, kbd_dev->output_size,
278 0));
[cc29622]279
[61257f4]280 usbhid_req_set_report(&hid_dev->usb_dev->ctrl_pipe,
281 hid_dev->usb_dev->interface_no, USB_HID_REPORT_TYPE_OUTPUT,
282 kbd_dev->output_buffer, kbd_dev->output_size);
283}
284
285/*----------------------------------------------------------------------------*/
[46b60e6]286/** Send key event.
[61257f4]287 *
288 * @param kbd_dev Keyboard device structure.
[46b60e6]289 * @param type Type of the event (press / release). Recognized values:
[61257f4]290 * KEY_PRESS, KEY_RELEASE
[46b60e6]291 * @param key Key code
[61257f4]292 */
293void usb_kbd_push_ev(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev, int type,
294 unsigned int key)
295{
[f2f99ae]296 usb_log_debug2("Sending kbdev event %d/%d to the console\n", type, key);
[5da7199]297 if (kbd_dev->console_sess == NULL) {
[61257f4]298 usb_log_warning(
299 "Connection to console not ready, key discarded.\n");
300 return;
301 }
[cc29622]302
[5da7199]303 async_exch_t *exch = async_exchange_begin(kbd_dev->console_sess);
304 async_msg_2(exch, KBDEV_EVENT, type, key);
305 async_exchange_end(exch);
[61257f4]306}
307
308/*----------------------------------------------------------------------------*/
309
310static inline int usb_kbd_is_lock(unsigned int key_code)
311{
312 return (key_code == KC_NUM_LOCK
313 || key_code == KC_SCROLL_LOCK
314 || key_code == KC_CAPS_LOCK);
315}
316
[44e2c1a]317static size_t find_in_array_int32(int32_t val, int32_t *arr, size_t arr_size)
318{
319 for (size_t i = 0; i < arr_size; i++) {
320 if (arr[i] == val) {
321 return i;
322 }
323 }
324
325 return (size_t) -1;
326}
327
[61257f4]328/*----------------------------------------------------------------------------*/
329/**
330 * Checks if some keys were pressed or released and generates key events.
331 *
332 * An event is created only when key is pressed or released. Besides handling
333 * the events (usb_kbd_push_ev()), the auto-repeat fibril is notified about
334 * key presses and releases (see usb_kbd_repeat_start() and
335 * usb_kbd_repeat_stop()).
336 *
337 * @param kbd_dev Keyboard device structure.
338 * @param key_codes Parsed keyboard report - codes of currently pressed keys
339 * according to HID Usage Tables.
340 * @param count Number of key codes in report (size of the report).
341 *
342 * @sa usb_kbd_push_ev(), usb_kbd_repeat_start(), usb_kbd_repeat_stop()
343 */
344static void usb_kbd_check_key_changes(usb_hid_dev_t *hid_dev,
[19e0560e]345 usb_kbd_t *kbd_dev)
[61257f4]346{
347 unsigned int key;
[44e2c1a]348 size_t i;
[cc29622]349
[61257f4]350 /*
351 * First of all, check if the kbd have reported phantom state.
352 *
353 * As there is no way to distinguish keys from modifiers, we do not have
354 * a way to check that 'all keys report Error Rollover'. We thus check
355 * if there is at least one such error and in such case we ignore the
356 * whole input report.
357 */
[44e2c1a]358 i = find_in_array_int32(ERROR_ROLLOVER, kbd_dev->keys,
359 kbd_dev->key_count);
360 if (i != (size_t) -1) {
361 usb_log_debug("Detected phantom state.\n");
[61257f4]362 return;
363 }
[cc29622]364
[61257f4]365 /*
[44e2c1a]366 * Key releases
[61257f4]367 */
[44e2c1a]368 for (i = 0; i < kbd_dev->key_count; i++) {
369 int32_t old_key = kbd_dev->keys_old[i];
370 /* Find the old key among currently pressed keys. */
371 size_t pos = find_in_array_int32(old_key, kbd_dev->keys,
372 kbd_dev->key_count);
373 /* If the key was not found, we need to signal release. */
374 if (pos == (size_t) -1) {
375 key = usbhid_parse_scancode(old_key);
[61257f4]376 if (!usb_kbd_is_lock(key)) {
377 usb_kbd_repeat_stop(kbd_dev, key);
378 }
379 usb_kbd_push_ev(hid_dev, kbd_dev, KEY_RELEASE, key);
[44e2c1a]380 usb_log_debug2("Key released: %u "
381 "(USB code %" PRIu32 ")\n", key, old_key);
[61257f4]382 }
383 }
[cc29622]384
[61257f4]385 /*
[44e2c1a]386 * Key presses
[61257f4]387 */
388 for (i = 0; i < kbd_dev->key_count; ++i) {
[44e2c1a]389 int32_t new_key = kbd_dev->keys[i];
390 /* Find the new key among already pressed keys. */
391 size_t pos = find_in_array_int32(new_key, kbd_dev->keys_old,
392 kbd_dev->key_count);
393 /* If the key was not found, we need to signal press. */
394 if (pos == (size_t) -1) {
[e60436b]395 key = usbhid_parse_scancode(kbd_dev->keys[i]);
[61257f4]396 if (!usb_kbd_is_lock(key)) {
397 usb_kbd_repeat_start(kbd_dev, key);
398 }
[1b7dc5e9]399 usb_kbd_push_ev(hid_dev, kbd_dev, KEY_PRESS, key);
[44e2c1a]400 usb_log_debug2("Key pressed: %u "
401 "(USB code %" PRIu32 ")\n", key, new_key);
[61257f4]402 }
403 }
[cc29622]404
[e60436b]405 memcpy(kbd_dev->keys_old, kbd_dev->keys, kbd_dev->key_count * 4);
[cc29622]406
[313775b]407 char key_buffer[512];
408 ddf_dump_buffer(key_buffer, 512,
409 kbd_dev->keys_old, 4, kbd_dev->key_count, 0);
410 usb_log_debug2("Stored keys %s.\n", key_buffer);
[61257f4]411}
412
413/*----------------------------------------------------------------------------*/
414/* General kbd functions */
415/*----------------------------------------------------------------------------*/
416/**
417 * Processes data received from the device in form of report.
418 *
419 * This function uses the HID report parser to translate the data received from
420 * the device into generic USB HID key codes and into generic modifiers bitmap.
421 * The parser then calls the given callback (usb_kbd_process_keycodes()).
422 *
423 * @note Currently, only the boot protocol is supported.
424 *
425 * @param kbd_dev Keyboard device structure (must be initialized).
426 * @param buffer Data from the keyboard (i.e. the report).
427 * @param actual_size Size of the data from keyboard (report size) in bytes.
428 *
429 * @sa usb_kbd_process_keycodes(), usb_hid_boot_keyboard_input_report(),
430 * usb_hid_parse_report().
431 */
[4d3c13e]432static void usb_kbd_process_data(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev)
[61257f4]433{
[e60436b]434 assert(hid_dev != NULL);
[65b458c4]435 assert(kbd_dev != NULL);
[cc29622]436
[61257f4]437 usb_hid_report_path_t *path = usb_hid_report_path();
438 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_KEYBOARD, 0);
[cfbbe1d3]439
[65c3794]440 usb_hid_report_path_set_report_id (path, hid_dev->report_id);
[cc29622]441
[e60436b]442 // fill in the currently pressed keys
[cc29622]443
[f240d30]444 usb_hid_report_field_t *field = usb_hid_report_get_sibling(
[a8c4e871]445 &hid_dev->report, NULL, path,
446 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
[6513110]447 USB_HID_REPORT_TYPE_INPUT);
[e60436b]448 unsigned i = 0;
[cc29622]449
[e60436b]450 while (field != NULL) {
451 usb_log_debug2("FIELD (%p) - VALUE(%d) USAGE(%u)\n",
[7304663]452 field, field->value, field->usage);
[e50cd7f]453
[e60436b]454 assert(i < kbd_dev->key_count);
[a8c4e871]455
[e60436b]456 // save the key usage
[323b0ec]457 if (field->value != 0) {
[7304663]458 kbd_dev->keys[i] = field->usage;
459 }
460 else {
461 kbd_dev->keys[i] = 0;
462 }
[e60436b]463 usb_log_debug2("Saved %u. key usage %d\n", i, kbd_dev->keys[i]);
[a8c4e871]464
[e60436b]465 ++i;
[a8c4e871]466 field = usb_hid_report_get_sibling(
467 &hid_dev->report, field, path, USB_HID_PATH_COMPARE_END
468 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
[e60436b]469 USB_HID_REPORT_TYPE_INPUT);
470 }
[cc29622]471
[61257f4]472 usb_hid_report_path_free(path);
[cc29622]473
[e60436b]474 usb_kbd_check_key_changes(hid_dev, kbd_dev);
[61257f4]475}
476
477/*----------------------------------------------------------------------------*/
478/* HID/KBD structure manipulation */
479/*----------------------------------------------------------------------------*/
480
481static void usb_kbd_mark_unusable(usb_kbd_t *kbd_dev)
482{
483 kbd_dev->initialized = USB_KBD_STATUS_TO_DESTROY;
484}
485
486/*----------------------------------------------------------------------------*/
487
488/**
489 * Creates a new USB/HID keyboard structure.
490 *
491 * The structure returned by this function is not initialized. Use
492 * usb_kbd_init() to initialize it prior to polling.
493 *
494 * @return New uninitialized structure for representing a USB/HID keyboard or
495 * NULL if not successful (memory error).
496 */
497static usb_kbd_t *usb_kbd_new(void)
498{
499 usb_kbd_t *kbd_dev =
500 (usb_kbd_t *)calloc(1, sizeof(usb_kbd_t));
501
502 if (kbd_dev == NULL) {
[2a5b62b]503 usb_log_error("No memory!\n");
[61257f4]504 return NULL;
505 }
[cc29622]506
[5da7199]507 kbd_dev->console_sess = NULL;
[61257f4]508 kbd_dev->initialized = USB_KBD_STATUS_UNINITIALIZED;
[cc29622]509
[61257f4]510 return kbd_dev;
511}
512
[31cfee16]513/*----------------------------------------------------------------------------*/
514
[7b54b99]515static int usb_kbd_create_function(usb_kbd_t *kbd_dev)
[31cfee16]516{
[65b458c4]517 assert(kbd_dev != NULL);
[7b54b99]518 assert(kbd_dev->hid_dev != NULL);
519 assert(kbd_dev->hid_dev->usb_dev != NULL);
[cc29622]520
[15f3c3f]521 /* Create the exposed function. */
[31cfee16]522 usb_log_debug("Creating DDF function %s...\n", HID_KBD_FUN_NAME);
[7b54b99]523 ddf_fun_t *fun = ddf_fun_create(kbd_dev->hid_dev->usb_dev->ddf_dev,
524 fun_exposed, HID_KBD_FUN_NAME);
[31cfee16]525 if (fun == NULL) {
526 usb_log_error("Could not create DDF function node.\n");
527 return ENOMEM;
528 }
[cc29622]529
[31cfee16]530 /*
531 * Store the initialized HID device and HID ops
532 * to the DDF function.
533 */
[65b458c4]534 fun->ops = &kbd_dev->ops;
535 fun->driver_data = kbd_dev;
[31cfee16]536
537 int rc = ddf_fun_bind(fun);
538 if (rc != EOK) {
539 usb_log_error("Could not bind DDF function: %s.\n",
540 str_error(rc));
[cddd151]541 fun->driver_data = NULL; /* We need this later */
[31cfee16]542 ddf_fun_destroy(fun);
543 return rc;
544 }
[cc29622]545
[0d59d0e9]546 usb_log_debug("%s function created. Handle: %" PRIun "\n",
547 HID_KBD_FUN_NAME, fun->handle);
[cc29622]548
[1dc4a5e]549 usb_log_debug("Adding DDF function to category %s...\n",
[31cfee16]550 HID_KBD_CLASS_NAME);
[1dc4a5e]551 rc = ddf_fun_add_to_category(fun, HID_KBD_CATEGORY_NAME);
[31cfee16]552 if (rc != EOK) {
553 usb_log_error(
[1dc4a5e]554 "Could not add DDF function to category %s: %s.\n",
[31cfee16]555 HID_KBD_CLASS_NAME, str_error(rc));
[cddd151]556 fun->driver_data = NULL; /* We need this later */
[31cfee16]557 ddf_fun_destroy(fun);
558 return rc;
559 }
[cddd151]560 kbd_dev->fun = fun;
[cc29622]561
[31cfee16]562 return EOK;
563}
564
[61257f4]565/*----------------------------------------------------------------------------*/
566/* API functions */
567/*----------------------------------------------------------------------------*/
568/**
569 * Initialization of the USB/HID keyboard structure.
570 *
571 * This functions initializes required structures from the device's descriptors.
572 *
573 * During initialization, the keyboard is switched into boot protocol, the idle
574 * rate is set to 0 (infinity), resulting in the keyboard only reporting event
575 * when a key is pressed or released. Finally, the LED lights are turned on
576 * according to the default setup of lock keys.
577 *
578 * @note By default, the keyboards is initialized with Num Lock turned on and
579 * other locks turned off.
580 *
581 * @param kbd_dev Keyboard device structure to be initialized.
582 * @param dev DDF device structure of the keyboard.
583 *
584 * @retval EOK if successful.
585 * @retval EINVAL if some parameter is not given.
586 * @return Other value inherited from function usbhid_dev_init().
587 */
[65b458c4]588int usb_kbd_init(usb_hid_dev_t *hid_dev, void **data)
[61257f4]589{
590 usb_log_debug("Initializing HID/KBD structure...\n");
[cc29622]591
[61257f4]592 if (hid_dev == NULL) {
593 usb_log_error("Failed to init keyboard structure: no structure"
594 " given.\n");
595 return EINVAL;
596 }
[cc29622]597
[61257f4]598 usb_kbd_t *kbd_dev = usb_kbd_new();
599 if (kbd_dev == NULL) {
600 usb_log_error("Error while creating USB/HID KBD device "
601 "structure.\n");
602 return ENOMEM; // TODO: some other code??
603 }
[60e5a856]604
605 /* Store link to HID device */
606 kbd_dev->hid_dev = hid_dev;
[cc29622]607
[61257f4]608 /*
609 * TODO: make more general
610 */
611 usb_hid_report_path_t *path = usb_hid_report_path();
612 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_KEYBOARD, 0);
[cc29622]613
[7f2e33a]614 usb_hid_report_path_set_report_id(path, 0);
[cc29622]615
[3a6e423]616 kbd_dev->key_count = usb_hid_report_size(
[a8c4e871]617 &hid_dev->report, 0, USB_HID_REPORT_TYPE_INPUT);
[61257f4]618 usb_hid_report_path_free(path);
[cc29622]619
[61257f4]620 usb_log_debug("Size of the input report: %zu\n", kbd_dev->key_count);
[cc29622]621
[a8c4e871]622 kbd_dev->keys = calloc(kbd_dev->key_count, sizeof(int32_t));
[cc29622]623
[61257f4]624 if (kbd_dev->keys == NULL) {
[2a5b62b]625 usb_log_error("No memory!\n");
[61257f4]626 free(kbd_dev);
627 return ENOMEM;
628 }
[cc29622]629
[e60436b]630 kbd_dev->keys_old =
631 (int32_t *)calloc(kbd_dev->key_count, sizeof(int32_t));
[cc29622]632
[e60436b]633 if (kbd_dev->keys_old == NULL) {
[2a5b62b]634 usb_log_error("No memory!\n");
[e60436b]635 free(kbd_dev->keys);
636 free(kbd_dev);
637 return ENOMEM;
638 }
[cc29622]639
[61257f4]640 /*
641 * Output report
642 */
643 kbd_dev->output_size = 0;
[a8c4e871]644 kbd_dev->output_buffer = usb_hid_report_output(&hid_dev->report,
[e60436b]645 &kbd_dev->output_size, 0);
[e50cd7f]646 if (kbd_dev->output_buffer == NULL) {
[61257f4]647 usb_log_warning("Error creating output report buffer.\n");
648 free(kbd_dev->keys);
[19e0560e]649 return ENOMEM;
[61257f4]650 }
[cc29622]651
[61257f4]652 usb_log_debug("Output buffer size: %zu\n", kbd_dev->output_size);
[cc29622]653
[61257f4]654 kbd_dev->led_path = usb_hid_report_path();
655 usb_hid_report_path_append_item(
656 kbd_dev->led_path, USB_HIDUT_PAGE_LED, 0);
[cc29622]657
[a8c4e871]658 kbd_dev->led_output_size = usb_hid_report_size(
659 &hid_dev->report, 0, USB_HID_REPORT_TYPE_OUTPUT);
[cc29622]660
[61257f4]661 usb_log_debug("Output report size (in items): %zu\n",
662 kbd_dev->led_output_size);
[cc29622]663
[61257f4]664 kbd_dev->led_data = (int32_t *)calloc(
665 kbd_dev->led_output_size, sizeof(int32_t));
[cc29622]666
[61257f4]667 if (kbd_dev->led_data == NULL) {
668 usb_log_warning("Error creating buffer for LED output report."
669 "\n");
670 free(kbd_dev->keys);
671 usb_hid_report_output_free(kbd_dev->output_buffer);
672 free(kbd_dev);
673 return ENOMEM;
674 }
[cc29622]675
[61257f4]676 /*
677 * Modifiers and locks
[f2f99ae]678 */
[61257f4]679 kbd_dev->modifiers = 0;
680 kbd_dev->mods = DEFAULT_ACTIVE_MODS;
681 kbd_dev->lock_keys = 0;
[cc29622]682
[61257f4]683 /*
684 * Autorepeat
[f2f99ae]685 */
[61257f4]686 kbd_dev->repeat.key_new = 0;
687 kbd_dev->repeat.key_repeated = 0;
688 kbd_dev->repeat.delay_before = DEFAULT_DELAY_BEFORE_FIRST_REPEAT;
689 kbd_dev->repeat.delay_between = DEFAULT_REPEAT_DELAY;
[cc29622]690
[cddd151]691 fibril_mutex_initialize(&kbd_dev->repeat_mtx);
[cc29622]692
[61257f4]693 // save the KBD device structure into the HID device structure
[65b458c4]694 *data = kbd_dev;
[cc29622]695
[61257f4]696 // set handler for incoming calls
[65b458c4]697 kbd_dev->ops.default_handler = default_connection_handler;
[cc29622]698
[61257f4]699 /*
700 * Set LEDs according to initial setup.
701 * Set Idle rate
702 */
703 usb_kbd_set_led(hid_dev, kbd_dev);
[cc29622]704
[61257f4]705 usbhid_req_set_idle(&hid_dev->usb_dev->ctrl_pipe,
706 hid_dev->usb_dev->interface_no, IDLE_RATE);
[cc29622]707
[61257f4]708 /*
709 * Create new fibril for auto-repeat
710 */
711 fid_t fid = fibril_create(usb_kbd_repeat_fibril, kbd_dev);
712 if (fid == 0) {
713 usb_log_error("Failed to start fibril for KBD auto-repeat");
714 return ENOMEM;
715 }
716 fibril_add_ready(fid);
[cc29622]717
[61257f4]718 kbd_dev->initialized = USB_KBD_STATUS_INITIALIZED;
719 usb_log_debug("HID/KBD device structure initialized.\n");
[cc29622]720
[31cfee16]721 usb_log_debug("Creating KBD function...\n");
[7b54b99]722 int rc = usb_kbd_create_function(kbd_dev);
[31cfee16]723 if (rc != EOK) {
[5f6e25e]724 usb_kbd_destroy(kbd_dev);
[31cfee16]725 return rc;
726 }
[cc29622]727
[61257f4]728 return EOK;
729}
730
731/*----------------------------------------------------------------------------*/
732
[4d3c13e]733bool usb_kbd_polling_callback(usb_hid_dev_t *hid_dev, void *data)
[61257f4]734{
[65c3794]735 if (hid_dev == NULL/* || buffer == NULL*/ || data == NULL) {
[61257f4]736 // do not continue polling (???)
737 return false;
738 }
[cc29622]739
[65b458c4]740 usb_kbd_t *kbd_dev = (usb_kbd_t *)data;
741 assert(kbd_dev != NULL);
[cc29622]742
[61257f4]743 // TODO: add return value from this function
[4d3c13e]744 usb_kbd_process_data(hid_dev, kbd_dev);
[cc29622]745
[61257f4]746 return true;
747}
748
749/*----------------------------------------------------------------------------*/
750
751int usb_kbd_is_initialized(const usb_kbd_t *kbd_dev)
752{
753 return (kbd_dev->initialized == USB_KBD_STATUS_INITIALIZED);
754}
755
756/*----------------------------------------------------------------------------*/
757
758int usb_kbd_is_ready_to_destroy(const usb_kbd_t *kbd_dev)
759{
760 return (kbd_dev->initialized == USB_KBD_STATUS_TO_DESTROY);
761}
762
763/*----------------------------------------------------------------------------*/
764/**
765 * Properly destroys the USB/HID keyboard structure.
766 *
767 * @param kbd_dev Pointer to the structure to be destroyed.
768 */
[5f6e25e]769void usb_kbd_destroy(usb_kbd_t *kbd_dev)
[61257f4]770{
[5f6e25e]771 if (kbd_dev == NULL) {
[61257f4]772 return;
773 }
[cc29622]774
[5da7199]775 // hangup session to the console
776 async_hangup(kbd_dev->console_sess);
[cc29622]777
[cddd151]778 //assert(!fibril_mutex_is_locked((*kbd_dev)->repeat_mtx));
779 // FIXME - the fibril_mutex_is_locked may not cause
780 // fibril scheduling
781 while (fibril_mutex_is_locked(&kbd_dev->repeat_mtx)) {}
[cc29622]782
[323b0ec]783 // free all buffers
[cddd151]784 free(kbd_dev->keys);
785 free(kbd_dev->keys_old);
786 free(kbd_dev->led_data);
[7b54b99]787
[5f6e25e]788 if (kbd_dev->led_path != NULL) {
789 usb_hid_report_path_free(kbd_dev->led_path);
[323b0ec]790 }
[5f6e25e]791 if (kbd_dev->output_buffer != NULL) {
792 usb_hid_report_output_free(kbd_dev->output_buffer);
[323b0ec]793 }
[7b54b99]794
795 if (ddf_fun_unbind(kbd_dev->fun) != EOK) {
796 usb_log_warning("Failed to unbind kbd function.\n");
797 } else {
[2a5b62b]798 usb_log_debug2("%s unbound.\n", kbd_dev->fun->name);
[7b54b99]799 kbd_dev->fun->driver_data = NULL;
800 ddf_fun_destroy(kbd_dev->fun);
801 }
[61257f4]802}
803
804/*----------------------------------------------------------------------------*/
805
[65b458c4]806void usb_kbd_deinit(usb_hid_dev_t *hid_dev, void *data)
[61257f4]807{
808 if (hid_dev == NULL) {
809 return;
810 }
[cc29622]811
[65b458c4]812 if (data != NULL) {
[2a5b62b]813 usb_kbd_t *kbd_dev = data;
[61257f4]814 if (usb_kbd_is_initialized(kbd_dev)) {
815 usb_kbd_mark_unusable(kbd_dev);
[2a5b62b]816 /* wait for autorepeat */
817 async_usleep(CHECK_DELAY);
[5f6e25e]818 usb_kbd_destroy(kbd_dev);
[61257f4]819 }
820 }
821}
822
823/*----------------------------------------------------------------------------*/
824
825int usb_kbd_set_boot_protocol(usb_hid_dev_t *hid_dev)
826{
[a8c4e871]827 int rc = usb_hid_parse_report_descriptor(
828 &hid_dev->report, USB_KBD_BOOT_REPORT_DESCRIPTOR,
[61257f4]829 USB_KBD_BOOT_REPORT_DESCRIPTOR_SIZE);
[cc29622]830
[61257f4]831 if (rc != EOK) {
832 usb_log_error("Failed to parse boot report descriptor: %s\n",
833 str_error(rc));
834 return rc;
835 }
[cc29622]836
[a8c4e871]837 rc = usbhid_req_set_protocol(&hid_dev->usb_dev->ctrl_pipe,
[61257f4]838 hid_dev->usb_dev->interface_no, USB_HID_PROTOCOL_BOOT);
[cc29622]839
[61257f4]840 if (rc != EOK) {
841 usb_log_warning("Failed to set boot protocol to the device: "
842 "%s\n", str_error(rc));
843 return rc;
844 }
[cc29622]845
[61257f4]846 return EOK;
847}
848
849/**
850 * @}
851 */
Note: See TracBrowser for help on using the repository browser.