1 | /*
|
---|
2 | * Copyright (c) 2010 Vojtech Horky
|
---|
3 | * Copyright (c) 2011 Lubos Slovak
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup drvusbhid
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /**
|
---|
34 | * @file
|
---|
35 | * Main routines of USB HID driver.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <ddf/driver.h>
|
---|
39 | #include <ipc/driver.h>
|
---|
40 | #include <ipc/kbd.h>
|
---|
41 | #include <io/keycode.h>
|
---|
42 | #include <io/console.h>
|
---|
43 | #include <errno.h>
|
---|
44 | #include <str_error.h>
|
---|
45 | #include <fibril.h>
|
---|
46 | #include <usb/debug.h>
|
---|
47 | #include <usb/classes/classes.h>
|
---|
48 | #include <usb/classes/hid.h>
|
---|
49 | #include <usb/classes/hidparser.h>
|
---|
50 | #include <usb/request.h>
|
---|
51 | #include <usb/descriptor.h>
|
---|
52 | #include <io/console.h>
|
---|
53 | #include <stdint.h>
|
---|
54 | #include "hid.h"
|
---|
55 | #include "descparser.h"
|
---|
56 | #include "descdump.h"
|
---|
57 | #include "conv.h"
|
---|
58 | #include "layout.h"
|
---|
59 |
|
---|
60 | #define BUFFER_SIZE 8
|
---|
61 | #define NAME "usbhid"
|
---|
62 |
|
---|
63 | #define GUESSED_POLL_ENDPOINT 1
|
---|
64 | #define BOOTP_REPORT_SIZE 6
|
---|
65 |
|
---|
66 | /** Keyboard polling endpoint description for boot protocol class. */
|
---|
67 | static usb_endpoint_description_t poll_endpoint_description = {
|
---|
68 | .transfer_type = USB_TRANSFER_INTERRUPT,
|
---|
69 | .direction = USB_DIRECTION_IN,
|
---|
70 | .interface_class = USB_CLASS_HID,
|
---|
71 | .interface_subclass = USB_HID_SUBCLASS_BOOT,
|
---|
72 | .interface_protocol = USB_HID_PROTOCOL_KEYBOARD,
|
---|
73 | .flags = 0
|
---|
74 | };
|
---|
75 |
|
---|
76 | static void default_connection_handler(ddf_fun_t *, ipc_callid_t, ipc_call_t *);
|
---|
77 | static ddf_dev_ops_t keyboard_ops = {
|
---|
78 | .default_handler = default_connection_handler
|
---|
79 | };
|
---|
80 |
|
---|
81 | static int console_callback_phone = -1;
|
---|
82 |
|
---|
83 | /** Default handler for IPC methods not handled by DDF.
|
---|
84 | *
|
---|
85 | * @param dev Device handling the call.
|
---|
86 | * @param icallid Call id.
|
---|
87 | * @param icall Call data.
|
---|
88 | */
|
---|
89 | void default_connection_handler(ddf_fun_t *fun,
|
---|
90 | ipc_callid_t icallid, ipc_call_t *icall)
|
---|
91 | {
|
---|
92 | sysarg_t method = IPC_GET_IMETHOD(*icall);
|
---|
93 |
|
---|
94 | if (method == IPC_M_CONNECT_TO_ME) {
|
---|
95 | int callback = IPC_GET_ARG5(*icall);
|
---|
96 |
|
---|
97 | if (console_callback_phone != -1) {
|
---|
98 | async_answer_0(icallid, ELIMIT);
|
---|
99 | return;
|
---|
100 | }
|
---|
101 |
|
---|
102 | console_callback_phone = callback;
|
---|
103 | async_answer_0(icallid, EOK);
|
---|
104 | return;
|
---|
105 | }
|
---|
106 |
|
---|
107 | async_answer_0(icallid, EINVAL);
|
---|
108 | }
|
---|
109 |
|
---|
110 | #if 0
|
---|
111 | static void send_key(int key, int type, wchar_t c) {
|
---|
112 | async_msg_4(console_callback_phone, KBD_EVENT, type, key,
|
---|
113 | KM_NUM_LOCK, c);
|
---|
114 | }
|
---|
115 | #endif
|
---|
116 |
|
---|
117 | /*
|
---|
118 | * TODO: Move somewhere else
|
---|
119 | */
|
---|
120 | /*
|
---|
121 | #define BYTES_PER_LINE 12
|
---|
122 |
|
---|
123 | static void dump_buffer(const char *msg, const uint8_t *buffer, size_t length)
|
---|
124 | {
|
---|
125 | printf("%s\n", msg);
|
---|
126 |
|
---|
127 | size_t i;
|
---|
128 | for (i = 0; i < length; i++) {
|
---|
129 | printf(" 0x%02X", buffer[i]);
|
---|
130 | if (((i > 0) && (((i+1) % BYTES_PER_LINE) == 0))
|
---|
131 | || (i + 1 == length)) {
|
---|
132 | printf("\n");
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 | */
|
---|
137 | /*
|
---|
138 | * Copy-paste from srv/hid/kbd/generic/kbd.c
|
---|
139 | */
|
---|
140 |
|
---|
141 | /** Currently active modifiers.
|
---|
142 | *
|
---|
143 | * TODO: put to device?
|
---|
144 | */
|
---|
145 | static unsigned mods = KM_NUM_LOCK;
|
---|
146 |
|
---|
147 | /** Currently pressed lock keys. We track these to tackle autorepeat.
|
---|
148 | *
|
---|
149 | * TODO: put to device?
|
---|
150 | */
|
---|
151 | static unsigned lock_keys;
|
---|
152 |
|
---|
153 | #define NUM_LAYOUTS 3
|
---|
154 |
|
---|
155 | static layout_op_t *layout[NUM_LAYOUTS] = {
|
---|
156 | &us_qwerty_op,
|
---|
157 | &us_dvorak_op,
|
---|
158 | &cz_op
|
---|
159 | };
|
---|
160 |
|
---|
161 | static int active_layout = 0;
|
---|
162 |
|
---|
163 | static void kbd_push_ev(int type, unsigned int key)
|
---|
164 | {
|
---|
165 | console_event_t ev;
|
---|
166 | unsigned mod_mask;
|
---|
167 |
|
---|
168 | // TODO: replace by our own parsing?? or are the key codes identical??
|
---|
169 | switch (key) {
|
---|
170 | case KC_LCTRL: mod_mask = KM_LCTRL; break;
|
---|
171 | case KC_RCTRL: mod_mask = KM_RCTRL; break;
|
---|
172 | case KC_LSHIFT: mod_mask = KM_LSHIFT; break;
|
---|
173 | case KC_RSHIFT: mod_mask = KM_RSHIFT; break;
|
---|
174 | case KC_LALT: mod_mask = KM_LALT; break;
|
---|
175 | case KC_RALT: mod_mask = KM_RALT; break;
|
---|
176 | default: mod_mask = 0; break;
|
---|
177 | }
|
---|
178 |
|
---|
179 | if (mod_mask != 0) {
|
---|
180 | if (type == KEY_PRESS)
|
---|
181 | mods = mods | mod_mask;
|
---|
182 | else
|
---|
183 | mods = mods & ~mod_mask;
|
---|
184 | }
|
---|
185 |
|
---|
186 | switch (key) {
|
---|
187 | case KC_CAPS_LOCK: mod_mask = KM_CAPS_LOCK; break;
|
---|
188 | case KC_NUM_LOCK: mod_mask = KM_NUM_LOCK; break;
|
---|
189 | case KC_SCROLL_LOCK: mod_mask = KM_SCROLL_LOCK; break;
|
---|
190 | default: mod_mask = 0; break;
|
---|
191 | }
|
---|
192 |
|
---|
193 | if (mod_mask != 0) {
|
---|
194 | if (type == KEY_PRESS) {
|
---|
195 | /*
|
---|
196 | * Only change lock state on transition from released
|
---|
197 | * to pressed. This prevents autorepeat from messing
|
---|
198 | * up the lock state.
|
---|
199 | */
|
---|
200 | mods = mods ^ (mod_mask & ~lock_keys);
|
---|
201 | lock_keys = lock_keys | mod_mask;
|
---|
202 |
|
---|
203 | /* Update keyboard lock indicator lights. */
|
---|
204 | // TODO
|
---|
205 | //kbd_ctl_set_ind(mods);
|
---|
206 | } else {
|
---|
207 | lock_keys = lock_keys & ~mod_mask;
|
---|
208 | }
|
---|
209 | }
|
---|
210 | /*
|
---|
211 | usb_log_debug2("type: %d\n", type);
|
---|
212 | usb_log_debug2("mods: 0x%x\n", mods);
|
---|
213 | usb_log_debug2("keycode: %u\n", key);
|
---|
214 | */
|
---|
215 |
|
---|
216 | if (type == KEY_PRESS && (mods & KM_LCTRL) &&
|
---|
217 | key == KC_F1) {
|
---|
218 | active_layout = 0;
|
---|
219 | layout[active_layout]->reset();
|
---|
220 | return;
|
---|
221 | }
|
---|
222 |
|
---|
223 | if (type == KEY_PRESS && (mods & KM_LCTRL) &&
|
---|
224 | key == KC_F2) {
|
---|
225 | active_layout = 1;
|
---|
226 | layout[active_layout]->reset();
|
---|
227 | return;
|
---|
228 | }
|
---|
229 |
|
---|
230 | if (type == KEY_PRESS && (mods & KM_LCTRL) &&
|
---|
231 | key == KC_F3) {
|
---|
232 | active_layout = 2;
|
---|
233 | layout[active_layout]->reset();
|
---|
234 | return;
|
---|
235 | }
|
---|
236 |
|
---|
237 | ev.type = type;
|
---|
238 | ev.key = key;
|
---|
239 | ev.mods = mods;
|
---|
240 |
|
---|
241 | ev.c = layout[active_layout]->parse_ev(&ev);
|
---|
242 |
|
---|
243 | usb_log_debug("Sending key %d to the console\n", ev.key);
|
---|
244 | assert(console_callback_phone != -1);
|
---|
245 | async_msg_4(console_callback_phone, KBD_EVENT, ev.type, ev.key,
|
---|
246 | ev.mods, ev.c);
|
---|
247 | }
|
---|
248 | /*
|
---|
249 | * End of copy-paste
|
---|
250 | */
|
---|
251 |
|
---|
252 | /*
|
---|
253 | * TODO:
|
---|
254 | * 1) key press / key release - how does the keyboard notify about
|
---|
255 | * release?
|
---|
256 | * 2) layouts (use the already defined), not important now
|
---|
257 | * 3)
|
---|
258 | */
|
---|
259 |
|
---|
260 | static const keycode_t usb_hid_modifiers_boot_keycodes[5] = {
|
---|
261 | KC_NUM_LOCK, /* USB_HID_MOD_BOOT_NUM_LOCK */
|
---|
262 | KC_CAPS_LOCK, /* USB_HID_MOD_BOOT_CAPS_LOCK */
|
---|
263 | KC_SCROLL_LOCK, /* USB_HID_MOD_BOOT_SCROLL_LOCK */
|
---|
264 | 0, /* USB_HID_MOD_BOOT_COMPOSE */
|
---|
265 | 0 /* USB_HID_MOD_BOOT_KANA */
|
---|
266 | };
|
---|
267 |
|
---|
268 | static void usbkbd_check_modifier_changes(usb_hid_dev_kbd_t *kbd_dev,
|
---|
269 | uint8_t modifiers)
|
---|
270 | {
|
---|
271 | /*
|
---|
272 | * TODO: why the USB keyboard has NUM_, SCROLL_ and CAPS_LOCK
|
---|
273 | * both as modifiers and as keys with their own scancodes???
|
---|
274 | *
|
---|
275 | * modifiers should be sent as normal keys to usbkbd_parse_scancode()!!
|
---|
276 | * so maybe it would be better if I received it from report parser in
|
---|
277 | * that way
|
---|
278 | */
|
---|
279 |
|
---|
280 | int i;
|
---|
281 | for (i = 0; i < USB_HID_MOD_BOOT_COUNT; ++i) {
|
---|
282 | if ((modifiers & usb_hid_modifiers_boot_consts[i]) &&
|
---|
283 | !(kbd_dev->modifiers & usb_hid_modifiers_boot_consts[i])) {
|
---|
284 | // modifier pressed
|
---|
285 | if (usb_hid_modifiers_boot_keycodes[i] != 0) {
|
---|
286 | kbd_push_ev(KEY_PRESS,
|
---|
287 | usb_hid_modifiers_boot_keycodes[i]);
|
---|
288 | }
|
---|
289 | } else if (!(modifiers & usb_hid_modifiers_boot_consts[i]) &&
|
---|
290 | (kbd_dev->modifiers & usb_hid_modifiers_boot_consts[i])) {
|
---|
291 | // modifier released
|
---|
292 | if (usb_hid_modifiers_boot_keycodes[i] != 0) {
|
---|
293 | kbd_push_ev(KEY_RELEASE,
|
---|
294 | usb_hid_modifiers_boot_keycodes[i]);
|
---|
295 | }
|
---|
296 | } // no change
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | static void usbkbd_check_key_changes(usb_hid_dev_kbd_t *kbd_dev,
|
---|
301 | const uint8_t *key_codes)
|
---|
302 | {
|
---|
303 | // TODO: phantom state!!
|
---|
304 |
|
---|
305 | unsigned int key;
|
---|
306 | unsigned int i, j;
|
---|
307 |
|
---|
308 | // TODO: quite dummy right now, think of better implementation
|
---|
309 |
|
---|
310 | // key releases
|
---|
311 | for (j = 0; j < kbd_dev->keycode_count; ++j) {
|
---|
312 | // try to find the old key in the new key list
|
---|
313 | i = 0;
|
---|
314 | while (i < kbd_dev->keycode_count
|
---|
315 | && key_codes[i] != kbd_dev->keycodes[j]) {
|
---|
316 | ++j;
|
---|
317 | }
|
---|
318 |
|
---|
319 | if (j == kbd_dev->keycode_count) {
|
---|
320 | // not found, i.e. the key was released
|
---|
321 | key = usbkbd_parse_scancode(kbd_dev->keycodes[j]);
|
---|
322 | kbd_push_ev(KEY_RELEASE, key);
|
---|
323 | } else {
|
---|
324 | // found, nothing happens
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | // key presses
|
---|
329 | for (i = 0; i < kbd_dev->keycode_count; ++i) {
|
---|
330 | // try to find the new key in the old key list
|
---|
331 | j = 0;
|
---|
332 | while (j < kbd_dev->keycode_count
|
---|
333 | && kbd_dev->keycodes[j] != key_codes[i]) {
|
---|
334 | ++j;
|
---|
335 | }
|
---|
336 |
|
---|
337 | assert(kbd_dev->keycode_count <= kbd_dev->keycode_count);
|
---|
338 |
|
---|
339 | if (j == kbd_dev->keycode_count) {
|
---|
340 | // not found, i.e. new key pressed
|
---|
341 | key = usbkbd_parse_scancode(key_codes[i]);
|
---|
342 | kbd_push_ev(KEY_PRESS, key);
|
---|
343 | } else {
|
---|
344 | // found, nothing happens
|
---|
345 | }
|
---|
346 | }
|
---|
347 | }
|
---|
348 |
|
---|
349 | /*
|
---|
350 | * Callbacks for parser
|
---|
351 | */
|
---|
352 | static void usbkbd_process_keycodes(const uint8_t *key_codes, size_t count,
|
---|
353 | uint8_t modifiers, void *arg)
|
---|
354 | {
|
---|
355 | if (arg == NULL) {
|
---|
356 | usb_log_warning("Missing argument in callback "
|
---|
357 | "usbkbd_process_keycodes().\n");
|
---|
358 | return;
|
---|
359 | }
|
---|
360 |
|
---|
361 | usb_log_debug2("Got keys from parser: ");
|
---|
362 | unsigned i;
|
---|
363 | for (i = 0; i < count; ++i) {
|
---|
364 | usb_log_debug2("%d ", key_codes[i]);
|
---|
365 | }
|
---|
366 | usb_log_debug2("\n");
|
---|
367 |
|
---|
368 | usb_hid_dev_kbd_t *kbd_dev = (usb_hid_dev_kbd_t *)arg;
|
---|
369 |
|
---|
370 | if (count != kbd_dev->keycode_count) {
|
---|
371 | usb_log_warning("Number of received keycodes (%d) differs from"
|
---|
372 | " expected number (%d).\n", count, kbd_dev->keycode_count);
|
---|
373 | return;
|
---|
374 | }
|
---|
375 |
|
---|
376 | usbkbd_check_modifier_changes(kbd_dev, modifiers);
|
---|
377 | usbkbd_check_key_changes(kbd_dev, key_codes);
|
---|
378 | }
|
---|
379 |
|
---|
380 | /*
|
---|
381 | * Kbd functions
|
---|
382 | */
|
---|
383 | static int usbkbd_get_report_descriptor(usb_hid_dev_kbd_t *kbd_dev)
|
---|
384 | {
|
---|
385 | // iterate over all configurations and interfaces
|
---|
386 | // TODO: more configurations!!
|
---|
387 | unsigned i;
|
---|
388 | for (i = 0; i < kbd_dev->conf->config_descriptor.interface_count; ++i) {
|
---|
389 | // TODO: endianness
|
---|
390 | uint16_t length = kbd_dev->conf->interfaces[i].hid_desc.
|
---|
391 | report_desc_info.length;
|
---|
392 | size_t actual_size = 0;
|
---|
393 |
|
---|
394 | // allocate space for the report descriptor
|
---|
395 | kbd_dev->conf->interfaces[i].report_desc =
|
---|
396 | (uint8_t *)malloc(length);
|
---|
397 |
|
---|
398 | // get the descriptor from the device
|
---|
399 | int rc = usb_request_get_descriptor(&kbd_dev->ctrl_pipe,
|
---|
400 | USB_REQUEST_TYPE_CLASS, USB_DESCTYPE_HID_REPORT,
|
---|
401 | i, 0,
|
---|
402 | kbd_dev->conf->interfaces[i].report_desc, length,
|
---|
403 | &actual_size);
|
---|
404 |
|
---|
405 | if (rc != EOK) {
|
---|
406 | return rc;
|
---|
407 | }
|
---|
408 |
|
---|
409 | assert(actual_size == length);
|
---|
410 |
|
---|
411 | //dump_hid_class_descriptor(0, USB_DESCTYPE_HID_REPORT,
|
---|
412 | // kbd_dev->conf->interfaces[i].report_desc, length);
|
---|
413 | }
|
---|
414 |
|
---|
415 | return EOK;
|
---|
416 | }
|
---|
417 |
|
---|
418 | static int usbkbd_process_descriptors(usb_hid_dev_kbd_t *kbd_dev)
|
---|
419 | {
|
---|
420 | // get the first configuration descriptor (TODO: parse also other!)
|
---|
421 | usb_standard_configuration_descriptor_t config_desc;
|
---|
422 |
|
---|
423 | int rc;
|
---|
424 | rc = usb_request_get_bare_configuration_descriptor(&kbd_dev->ctrl_pipe,
|
---|
425 | 0, &config_desc);
|
---|
426 |
|
---|
427 | if (rc != EOK) {
|
---|
428 | return rc;
|
---|
429 | }
|
---|
430 |
|
---|
431 | // prepare space for all underlying descriptors
|
---|
432 | uint8_t *descriptors = (uint8_t *)malloc(config_desc.total_length);
|
---|
433 | if (descriptors == NULL) {
|
---|
434 | return ENOMEM;
|
---|
435 | }
|
---|
436 |
|
---|
437 | size_t transferred = 0;
|
---|
438 | // get full configuration descriptor
|
---|
439 | rc = usb_request_get_full_configuration_descriptor(&kbd_dev->ctrl_pipe,
|
---|
440 | 0, descriptors,
|
---|
441 | config_desc.total_length, &transferred);
|
---|
442 |
|
---|
443 | if (rc != EOK) {
|
---|
444 | return rc;
|
---|
445 | }
|
---|
446 | if (transferred != config_desc.total_length) {
|
---|
447 | return ELIMIT;
|
---|
448 | }
|
---|
449 |
|
---|
450 | /*
|
---|
451 | * Initialize the interrupt in endpoint.
|
---|
452 | */
|
---|
453 | usb_endpoint_mapping_t endpoint_mapping[1] = {
|
---|
454 | {
|
---|
455 | .pipe = &kbd_dev->poll_pipe,
|
---|
456 | .description = &poll_endpoint_description,
|
---|
457 | .interface_no =
|
---|
458 | usb_device_get_assigned_interface(kbd_dev->device)
|
---|
459 | }
|
---|
460 | };
|
---|
461 | rc = usb_endpoint_pipe_initialize_from_configuration(
|
---|
462 | endpoint_mapping, 1,
|
---|
463 | descriptors, config_desc.total_length,
|
---|
464 | &kbd_dev->wire);
|
---|
465 | if (rc != EOK) {
|
---|
466 | usb_log_error("Failed to initialize poll pipe: %s.\n",
|
---|
467 | str_error(rc));
|
---|
468 | return rc;
|
---|
469 | }
|
---|
470 | if (!endpoint_mapping[0].present) {
|
---|
471 | usb_log_warning("Not accepting device, " \
|
---|
472 | "not boot-protocol keyboard.\n");
|
---|
473 | return EREFUSED;
|
---|
474 | }
|
---|
475 |
|
---|
476 |
|
---|
477 |
|
---|
478 |
|
---|
479 | kbd_dev->conf = (usb_hid_configuration_t *)calloc(1,
|
---|
480 | sizeof(usb_hid_configuration_t));
|
---|
481 | if (kbd_dev->conf == NULL) {
|
---|
482 | free(descriptors);
|
---|
483 | return ENOMEM;
|
---|
484 | }
|
---|
485 |
|
---|
486 | /*rc = usbkbd_parse_descriptors(descriptors, transferred, kbd_dev->conf);
|
---|
487 | free(descriptors);
|
---|
488 | if (rc != EOK) {
|
---|
489 | usb_log_warning("Problem with parsing standard descriptors.\n");
|
---|
490 | return rc;
|
---|
491 | }
|
---|
492 |
|
---|
493 | // get and report descriptors*/
|
---|
494 | rc = usbkbd_get_report_descriptor(kbd_dev);
|
---|
495 | if (rc != EOK) {
|
---|
496 | usb_log_warning("Problem with parsing REPORT descriptor.\n");
|
---|
497 | return rc;
|
---|
498 | }
|
---|
499 |
|
---|
500 | //usbkbd_print_config(kbd_dev->conf);
|
---|
501 |
|
---|
502 | /*
|
---|
503 | * TODO:
|
---|
504 | * 1) select one configuration (lets say the first)
|
---|
505 | * 2) how many interfaces?? how to select one??
|
---|
506 | * ("The default setting for an interface is always alternate
|
---|
507 | * setting zero.")
|
---|
508 | * 3) find endpoint which is IN and INTERRUPT (parse), save its number
|
---|
509 | * as the endpoint for polling
|
---|
510 | */
|
---|
511 |
|
---|
512 | return EOK;
|
---|
513 | }
|
---|
514 |
|
---|
515 | static usb_hid_dev_kbd_t *usbkbd_init_device(ddf_dev_t *dev)
|
---|
516 | {
|
---|
517 | int rc;
|
---|
518 |
|
---|
519 | usb_hid_dev_kbd_t *kbd_dev = (usb_hid_dev_kbd_t *)calloc(1,
|
---|
520 | sizeof(usb_hid_dev_kbd_t));
|
---|
521 |
|
---|
522 | if (kbd_dev == NULL) {
|
---|
523 | usb_log_fatal("No memory!\n");
|
---|
524 | return NULL;
|
---|
525 | }
|
---|
526 |
|
---|
527 | kbd_dev->device = dev;
|
---|
528 |
|
---|
529 | /*
|
---|
530 | * Initialize the backing connection to the host controller.
|
---|
531 | */
|
---|
532 | rc = usb_device_connection_initialize_from_device(&kbd_dev->wire, dev);
|
---|
533 | if (rc != EOK) {
|
---|
534 | printf("Problem initializing connection to device: %s.\n",
|
---|
535 | str_error(rc));
|
---|
536 | goto error_leave;
|
---|
537 | }
|
---|
538 |
|
---|
539 | /*
|
---|
540 | * Initialize device pipes.
|
---|
541 | */
|
---|
542 | rc = usb_endpoint_pipe_initialize_default_control(&kbd_dev->ctrl_pipe,
|
---|
543 | &kbd_dev->wire);
|
---|
544 | if (rc != EOK) {
|
---|
545 | printf("Failed to initialize default control pipe: %s.\n",
|
---|
546 | str_error(rc));
|
---|
547 | goto error_leave;
|
---|
548 | }
|
---|
549 |
|
---|
550 | /*
|
---|
551 | * will need all descriptors:
|
---|
552 | * 1) choose one configuration from configuration descriptors
|
---|
553 | * (set it to the device)
|
---|
554 | * 2) set endpoints from endpoint descriptors
|
---|
555 | */
|
---|
556 |
|
---|
557 | // TODO: get descriptors, parse descriptors and save endpoints
|
---|
558 | usb_endpoint_pipe_start_session(&kbd_dev->ctrl_pipe);
|
---|
559 | rc = usbkbd_process_descriptors(kbd_dev);
|
---|
560 | usb_endpoint_pipe_end_session(&kbd_dev->ctrl_pipe);
|
---|
561 | if (rc != EOK) {
|
---|
562 | goto error_leave;
|
---|
563 | }
|
---|
564 |
|
---|
565 | // save the size of the report
|
---|
566 | kbd_dev->keycode_count = BOOTP_REPORT_SIZE;
|
---|
567 | kbd_dev->keycodes = (uint8_t *)calloc(
|
---|
568 | kbd_dev->keycode_count, sizeof(uint8_t));
|
---|
569 |
|
---|
570 | if (kbd_dev->keycodes == NULL) {
|
---|
571 | usb_log_fatal("No memory!\n");
|
---|
572 | goto error_leave;
|
---|
573 | }
|
---|
574 |
|
---|
575 | // set configuration to the first one
|
---|
576 | // TODO: handle case with no configurations
|
---|
577 | usb_endpoint_pipe_start_session(&kbd_dev->ctrl_pipe);
|
---|
578 | usb_request_set_configuration(&kbd_dev->ctrl_pipe,
|
---|
579 | kbd_dev->conf->config_descriptor.configuration_number);
|
---|
580 | usb_endpoint_pipe_end_session(&kbd_dev->ctrl_pipe);
|
---|
581 |
|
---|
582 | return kbd_dev;
|
---|
583 |
|
---|
584 | error_leave:
|
---|
585 | free(kbd_dev);
|
---|
586 | return NULL;
|
---|
587 | }
|
---|
588 |
|
---|
589 | static void usbkbd_process_interrupt_in(usb_hid_dev_kbd_t *kbd_dev,
|
---|
590 | uint8_t *buffer, size_t actual_size)
|
---|
591 | {
|
---|
592 | usb_hid_report_in_callbacks_t *callbacks =
|
---|
593 | (usb_hid_report_in_callbacks_t *)malloc(
|
---|
594 | sizeof(usb_hid_report_in_callbacks_t));
|
---|
595 | callbacks->keyboard = usbkbd_process_keycodes;
|
---|
596 |
|
---|
597 | //usb_hid_parse_report(kbd_dev->parser, buffer, actual_size, callbacks,
|
---|
598 | // NULL);
|
---|
599 | /*usb_log_debug2("Calling usb_hid_boot_keyboard_input_report() with size"
|
---|
600 | " %zu\n", actual_size);*/
|
---|
601 | //dump_buffer("bufffer: ", buffer, actual_size);
|
---|
602 | int rc = usb_hid_boot_keyboard_input_report(buffer, actual_size,
|
---|
603 | callbacks, kbd_dev);
|
---|
604 |
|
---|
605 | if (rc != EOK) {
|
---|
606 | usb_log_warning("Error in usb_hid_boot_keyboard_input_report():"
|
---|
607 | "%s\n", str_error(rc));
|
---|
608 | }
|
---|
609 | }
|
---|
610 |
|
---|
611 | static void usbkbd_poll_keyboard(usb_hid_dev_kbd_t *kbd_dev)
|
---|
612 | {
|
---|
613 | int rc, sess_rc;
|
---|
614 | uint8_t buffer[BUFFER_SIZE];
|
---|
615 | size_t actual_size;
|
---|
616 |
|
---|
617 | usb_log_info("Polling keyboard...\n");
|
---|
618 |
|
---|
619 | while (true) {
|
---|
620 | async_usleep(1000 * 10);
|
---|
621 |
|
---|
622 | sess_rc = usb_endpoint_pipe_start_session(&kbd_dev->poll_pipe);
|
---|
623 | if (sess_rc != EOK) {
|
---|
624 | usb_log_warning("Failed to start a session: %s.\n",
|
---|
625 | str_error(sess_rc));
|
---|
626 | continue;
|
---|
627 | }
|
---|
628 |
|
---|
629 | rc = usb_endpoint_pipe_read(&kbd_dev->poll_pipe, buffer,
|
---|
630 | BUFFER_SIZE, &actual_size);
|
---|
631 | sess_rc = usb_endpoint_pipe_end_session(&kbd_dev->poll_pipe);
|
---|
632 |
|
---|
633 | if (rc != EOK) {
|
---|
634 | usb_log_warning("Error polling the keyboard: %s.\n",
|
---|
635 | str_error(rc));
|
---|
636 | continue;
|
---|
637 | }
|
---|
638 |
|
---|
639 | if (sess_rc != EOK) {
|
---|
640 | usb_log_warning("Error closing session: %s.\n",
|
---|
641 | str_error(sess_rc));
|
---|
642 | continue;
|
---|
643 | }
|
---|
644 |
|
---|
645 | /*
|
---|
646 | * If the keyboard answered with NAK, it returned no data.
|
---|
647 | * This implies that no change happened since last query.
|
---|
648 | */
|
---|
649 | if (actual_size == 0) {
|
---|
650 | usb_log_debug("Keyboard returned NAK\n");
|
---|
651 | continue;
|
---|
652 | }
|
---|
653 |
|
---|
654 | /*
|
---|
655 | * TODO: Process pressed keys.
|
---|
656 | */
|
---|
657 | usb_log_debug("Calling usbkbd_process_interrupt_in()\n");
|
---|
658 | usbkbd_process_interrupt_in(kbd_dev, buffer, actual_size);
|
---|
659 | }
|
---|
660 |
|
---|
661 | // not reached
|
---|
662 | assert(0);
|
---|
663 | }
|
---|
664 |
|
---|
665 | static int usbkbd_fibril_device(void *arg)
|
---|
666 | {
|
---|
667 | if (arg == NULL) {
|
---|
668 | usb_log_error("No device!\n");
|
---|
669 | return -1;
|
---|
670 | }
|
---|
671 |
|
---|
672 | ddf_dev_t *dev = (ddf_dev_t *)arg;
|
---|
673 |
|
---|
674 | // initialize device (get and process descriptors, get address, etc.)
|
---|
675 | usb_hid_dev_kbd_t *kbd_dev = usbkbd_init_device(dev);
|
---|
676 | if (kbd_dev == NULL) {
|
---|
677 | usb_log_error("Error while initializing device.\n");
|
---|
678 | return -1;
|
---|
679 | }
|
---|
680 |
|
---|
681 | usbkbd_poll_keyboard(kbd_dev);
|
---|
682 |
|
---|
683 | return EOK;
|
---|
684 | }
|
---|
685 |
|
---|
686 | static int usbkbd_add_device(ddf_dev_t *dev)
|
---|
687 | {
|
---|
688 | /* For now, fail immediately. */
|
---|
689 | //return ENOTSUP;
|
---|
690 |
|
---|
691 | /*
|
---|
692 | * When everything is okay, connect to "our" HC.
|
---|
693 | *
|
---|
694 | * Not supported yet, skip..
|
---|
695 | */
|
---|
696 | // int phone = usb_drv_hc_connect_auto(dev, 0);
|
---|
697 | // if (phone < 0) {
|
---|
698 | // /*
|
---|
699 | // * Connecting to HC failed, roll-back and announce
|
---|
700 | // * failure.
|
---|
701 | // */
|
---|
702 | // return phone;
|
---|
703 | // }
|
---|
704 |
|
---|
705 | // dev->parent_phone = phone;
|
---|
706 |
|
---|
707 | /*
|
---|
708 | * Create default function.
|
---|
709 | */
|
---|
710 | // FIXME - check for errors
|
---|
711 | ddf_fun_t *kbd_fun = ddf_fun_create(dev, fun_exposed, "keyboard");
|
---|
712 | assert(kbd_fun != NULL);
|
---|
713 | kbd_fun->ops = &keyboard_ops;
|
---|
714 |
|
---|
715 | int rc = ddf_fun_bind(kbd_fun);
|
---|
716 | assert(rc == EOK);
|
---|
717 | rc = ddf_fun_add_to_class(kbd_fun, "keyboard");
|
---|
718 | assert(rc == EOK);
|
---|
719 |
|
---|
720 | /*
|
---|
721 | * Create new fibril for handling this keyboard
|
---|
722 | */
|
---|
723 | fid_t fid = fibril_create(usbkbd_fibril_device, dev);
|
---|
724 | if (fid == 0) {
|
---|
725 | usb_log_error("Failed to start fibril for HID device\n");
|
---|
726 | return ENOMEM;
|
---|
727 | }
|
---|
728 | fibril_add_ready(fid);
|
---|
729 |
|
---|
730 | //dev->ops = &keyboard_ops;
|
---|
731 | (void)keyboard_ops;
|
---|
732 |
|
---|
733 | //add_device_to_class(dev, "keyboard");
|
---|
734 |
|
---|
735 | /*
|
---|
736 | * Hurrah, device is initialized.
|
---|
737 | */
|
---|
738 | return EOK;
|
---|
739 | }
|
---|
740 |
|
---|
741 | static driver_ops_t kbd_driver_ops = {
|
---|
742 | .add_device = usbkbd_add_device,
|
---|
743 | };
|
---|
744 |
|
---|
745 | static driver_t kbd_driver = {
|
---|
746 | .name = NAME,
|
---|
747 | .driver_ops = &kbd_driver_ops
|
---|
748 | };
|
---|
749 |
|
---|
750 | int main(int argc, char *argv[])
|
---|
751 | {
|
---|
752 | usb_log_enable(USB_LOG_LEVEL_MAX, NAME);
|
---|
753 | return ddf_driver_main(&kbd_driver);
|
---|
754 | }
|
---|
755 |
|
---|
756 | /**
|
---|
757 | * @}
|
---|
758 | */
|
---|