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

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

usbhid: Remove redundant array size enums.

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