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

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

usbmid, usbhid, usbhub, usbflbk: Don't access ddf_dev directly, use provided functions.

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