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