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 <usb/usbdrv.h>
|
---|
39 | #include <driver.h>
|
---|
40 | #include <ipc/driver.h>
|
---|
41 | #include <ipc/kbd.h>
|
---|
42 | #include <io/keycode.h>
|
---|
43 | #include <io/console.h>
|
---|
44 | #include <errno.h>
|
---|
45 | #include <str_error.h>
|
---|
46 | #include <fibril.h>
|
---|
47 | #include <usb/debug.h>
|
---|
48 | #include <usb/classes/classes.h>
|
---|
49 | #include <usb/classes/hid.h>
|
---|
50 | #include <usb/classes/hidparser.h>
|
---|
51 | #include <usb/request.h>
|
---|
52 | #include <usb/descriptor.h>
|
---|
53 | #include <io/console.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 |
|
---|
65 | /** Keyboard polling endpoint description for boot protocol class. */
|
---|
66 | static usb_endpoint_description_t poll_endpoint_description = {
|
---|
67 | .transfer_type = USB_TRANSFER_INTERRUPT,
|
---|
68 | .direction = USB_DIRECTION_IN,
|
---|
69 | .interface_class = USB_CLASS_HID,
|
---|
70 | .interface_subclass = USB_HID_SUBCLASS_BOOT,
|
---|
71 | .interface_protocol = USB_HID_PROTOCOL_KEYBOARD,
|
---|
72 | .flags = 0
|
---|
73 | };
|
---|
74 |
|
---|
75 | static void default_connection_handler(device_t *, ipc_callid_t, ipc_call_t *);
|
---|
76 | static device_ops_t keyboard_ops = {
|
---|
77 | .default_handler = default_connection_handler
|
---|
78 | };
|
---|
79 |
|
---|
80 | static int console_callback_phone = -1;
|
---|
81 |
|
---|
82 | /** Default handler for IPC methods not handled by DDF.
|
---|
83 | *
|
---|
84 | * @param dev Device handling the call.
|
---|
85 | * @param icallid Call id.
|
---|
86 | * @param icall Call data.
|
---|
87 | */
|
---|
88 | void default_connection_handler(device_t *dev,
|
---|
89 | ipc_callid_t icallid, ipc_call_t *icall)
|
---|
90 | {
|
---|
91 | sysarg_t method = IPC_GET_IMETHOD(*icall);
|
---|
92 |
|
---|
93 | if (method == IPC_M_CONNECT_TO_ME) {
|
---|
94 | int callback = IPC_GET_ARG5(*icall);
|
---|
95 |
|
---|
96 | if (console_callback_phone != -1) {
|
---|
97 | async_answer_0(icallid, ELIMIT);
|
---|
98 | return;
|
---|
99 | }
|
---|
100 |
|
---|
101 | console_callback_phone = callback;
|
---|
102 | async_answer_0(icallid, EOK);
|
---|
103 | return;
|
---|
104 | }
|
---|
105 |
|
---|
106 | async_answer_0(icallid, EINVAL);
|
---|
107 | }
|
---|
108 |
|
---|
109 | #if 0
|
---|
110 | static void send_key(int key, int type, wchar_t c) {
|
---|
111 | async_msg_4(console_callback_phone, KBD_EVENT, type, key,
|
---|
112 | KM_NUM_LOCK, c);
|
---|
113 | }
|
---|
114 | #endif
|
---|
115 |
|
---|
116 | /*
|
---|
117 | * TODO: Move somewhere else
|
---|
118 | */
|
---|
119 | /*
|
---|
120 | #define BYTES_PER_LINE 12
|
---|
121 |
|
---|
122 | static void dump_buffer(const char *msg, const uint8_t *buffer, size_t length)
|
---|
123 | {
|
---|
124 | printf("%s\n", msg);
|
---|
125 |
|
---|
126 | size_t i;
|
---|
127 | for (i = 0; i < length; i++) {
|
---|
128 | printf(" 0x%02X", buffer[i]);
|
---|
129 | if (((i > 0) && (((i+1) % BYTES_PER_LINE) == 0))
|
---|
130 | || (i + 1 == length)) {
|
---|
131 | printf("\n");
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|
135 | */
|
---|
136 | /*
|
---|
137 | * Copy-paste from srv/hid/kbd/generic/kbd.c
|
---|
138 | */
|
---|
139 |
|
---|
140 | /** Currently active modifiers.
|
---|
141 | *
|
---|
142 | * TODO: put to device?
|
---|
143 | */
|
---|
144 | static unsigned mods = KM_NUM_LOCK;
|
---|
145 |
|
---|
146 | /** Currently pressed lock keys. We track these to tackle autorepeat.
|
---|
147 | *
|
---|
148 | * TODO: put to device?
|
---|
149 | */
|
---|
150 | static unsigned lock_keys;
|
---|
151 |
|
---|
152 | #define NUM_LAYOUTS 3
|
---|
153 |
|
---|
154 | static layout_op_t *layout[NUM_LAYOUTS] = {
|
---|
155 | &us_qwerty_op,
|
---|
156 | &us_dvorak_op,
|
---|
157 | &cz_op
|
---|
158 | };
|
---|
159 |
|
---|
160 | static int active_layout = 0;
|
---|
161 |
|
---|
162 | static void kbd_push_ev(int type, unsigned int key)
|
---|
163 | {
|
---|
164 | console_event_t ev;
|
---|
165 | unsigned mod_mask;
|
---|
166 |
|
---|
167 | // TODO: replace by our own parsing?? or are the key codes identical??
|
---|
168 | switch (key) {
|
---|
169 | case KC_LCTRL: mod_mask = KM_LCTRL; break;
|
---|
170 | case KC_RCTRL: mod_mask = KM_RCTRL; break;
|
---|
171 | case KC_LSHIFT: mod_mask = KM_LSHIFT; break;
|
---|
172 | case KC_RSHIFT: mod_mask = KM_RSHIFT; break;
|
---|
173 | case KC_LALT: mod_mask = KM_LALT; break;
|
---|
174 | case KC_RALT: mod_mask = KM_RALT; break;
|
---|
175 | default: mod_mask = 0; break;
|
---|
176 | }
|
---|
177 |
|
---|
178 | if (mod_mask != 0) {
|
---|
179 | if (type == KEY_PRESS)
|
---|
180 | mods = mods | mod_mask;
|
---|
181 | else
|
---|
182 | mods = mods & ~mod_mask;
|
---|
183 | }
|
---|
184 |
|
---|
185 | switch (key) {
|
---|
186 | case KC_CAPS_LOCK: mod_mask = KM_CAPS_LOCK; break;
|
---|
187 | case KC_NUM_LOCK: mod_mask = KM_NUM_LOCK; break;
|
---|
188 | case KC_SCROLL_LOCK: mod_mask = KM_SCROLL_LOCK; break;
|
---|
189 | default: mod_mask = 0; break;
|
---|
190 | }
|
---|
191 |
|
---|
192 | if (mod_mask != 0) {
|
---|
193 | if (type == KEY_PRESS) {
|
---|
194 | /*
|
---|
195 | * Only change lock state on transition from released
|
---|
196 | * to pressed. This prevents autorepeat from messing
|
---|
197 | * up the lock state.
|
---|
198 | */
|
---|
199 | mods = mods ^ (mod_mask & ~lock_keys);
|
---|
200 | lock_keys = lock_keys | mod_mask;
|
---|
201 |
|
---|
202 | /* Update keyboard lock indicator lights. */
|
---|
203 | // TODO
|
---|
204 | //kbd_ctl_set_ind(mods);
|
---|
205 | } else {
|
---|
206 | lock_keys = lock_keys & ~mod_mask;
|
---|
207 | }
|
---|
208 | }
|
---|
209 | /*
|
---|
210 | printf("type: %d\n", type);
|
---|
211 | printf("mods: 0x%x\n", mods);
|
---|
212 | printf("keycode: %u\n", key);
|
---|
213 | */
|
---|
214 |
|
---|
215 | if (type == KEY_PRESS && (mods & KM_LCTRL) &&
|
---|
216 | key == KC_F1) {
|
---|
217 | active_layout = 0;
|
---|
218 | layout[active_layout]->reset();
|
---|
219 | return;
|
---|
220 | }
|
---|
221 |
|
---|
222 | if (type == KEY_PRESS && (mods & KM_LCTRL) &&
|
---|
223 | key == KC_F2) {
|
---|
224 | active_layout = 1;
|
---|
225 | layout[active_layout]->reset();
|
---|
226 | return;
|
---|
227 | }
|
---|
228 |
|
---|
229 | if (type == KEY_PRESS && (mods & KM_LCTRL) &&
|
---|
230 | key == KC_F3) {
|
---|
231 | active_layout = 2;
|
---|
232 | layout[active_layout]->reset();
|
---|
233 | return;
|
---|
234 | }
|
---|
235 |
|
---|
236 | ev.type = type;
|
---|
237 | ev.key = key;
|
---|
238 | ev.mods = mods;
|
---|
239 |
|
---|
240 | ev.c = layout[active_layout]->parse_ev(&ev);
|
---|
241 |
|
---|
242 | printf("Sending key %d to the console\n", ev.key);
|
---|
243 | assert(console_callback_phone != -1);
|
---|
244 | async_msg_4(console_callback_phone, KBD_EVENT, ev.type, ev.key, ev.mods, ev.c);
|
---|
245 | }
|
---|
246 | /*
|
---|
247 | * End of copy-paste
|
---|
248 | */
|
---|
249 |
|
---|
250 | /*
|
---|
251 | * TODO:
|
---|
252 | * 1) key press / key release - how does the keyboard notify about release?
|
---|
253 | * 2) layouts (use the already defined), not important now
|
---|
254 | * 3)
|
---|
255 | */
|
---|
256 |
|
---|
257 | /*
|
---|
258 | * Callbacks for parser
|
---|
259 | */
|
---|
260 | static void usbkbd_process_keycodes(const uint8_t *key_codes, size_t count,
|
---|
261 | uint8_t modifiers, void *arg)
|
---|
262 | {
|
---|
263 | printf("Got keys: ");
|
---|
264 | unsigned i;
|
---|
265 | for (i = 0; i < count; ++i) {
|
---|
266 | printf("%d ", key_codes[i]);
|
---|
267 | // TODO: Key press / release
|
---|
268 |
|
---|
269 | // TODO: NOT WORKING
|
---|
270 | unsigned int key = usbkbd_parse_scancode(key_codes[i]);
|
---|
271 | kbd_push_ev(KEY_PRESS, key);
|
---|
272 | }
|
---|
273 | printf("\n");
|
---|
274 | }
|
---|
275 |
|
---|
276 | /*
|
---|
277 | * Kbd functions
|
---|
278 | */
|
---|
279 | static int usbkbd_get_report_descriptor(usb_hid_dev_kbd_t *kbd_dev)
|
---|
280 | {
|
---|
281 | // iterate over all configurations and interfaces
|
---|
282 | // TODO: more configurations!!
|
---|
283 | unsigned i;
|
---|
284 | for (i = 0; i < kbd_dev->conf->config_descriptor.interface_count; ++i) {
|
---|
285 | // TODO: endianness
|
---|
286 | uint16_t length =
|
---|
287 | kbd_dev->conf->interfaces[i].hid_desc.report_desc_info.length;
|
---|
288 | size_t actual_size = 0;
|
---|
289 |
|
---|
290 | // allocate space for the report descriptor
|
---|
291 | kbd_dev->conf->interfaces[i].report_desc = (uint8_t *)malloc(length);
|
---|
292 |
|
---|
293 | // get the descriptor from the device
|
---|
294 | int rc = usb_request_get_descriptor(&kbd_dev->ctrl_pipe,
|
---|
295 | USB_REQUEST_TYPE_CLASS, USB_DESCTYPE_HID_REPORT,
|
---|
296 | i, 0,
|
---|
297 | kbd_dev->conf->interfaces[i].report_desc, length,
|
---|
298 | &actual_size);
|
---|
299 |
|
---|
300 | if (rc != EOK) {
|
---|
301 | return rc;
|
---|
302 | }
|
---|
303 |
|
---|
304 | assert(actual_size == length);
|
---|
305 |
|
---|
306 | //dump_hid_class_descriptor(0, USB_DESCTYPE_HID_REPORT,
|
---|
307 | // kbd_dev->conf->interfaces[i].report_desc, length);
|
---|
308 | }
|
---|
309 |
|
---|
310 | return EOK;
|
---|
311 | }
|
---|
312 | static int usbkbd_process_descriptors(usb_hid_dev_kbd_t *kbd_dev)
|
---|
313 | {
|
---|
314 | // get the first configuration descriptor (TODO: parse also other!)
|
---|
315 | usb_standard_configuration_descriptor_t config_desc;
|
---|
316 |
|
---|
317 | int rc;
|
---|
318 | rc = usb_request_get_bare_configuration_descriptor(&kbd_dev->ctrl_pipe,
|
---|
319 | 0, &config_desc);
|
---|
320 |
|
---|
321 | if (rc != EOK) {
|
---|
322 | return rc;
|
---|
323 | }
|
---|
324 |
|
---|
325 | // prepare space for all underlying descriptors
|
---|
326 | uint8_t *descriptors = (uint8_t *)malloc(config_desc.total_length);
|
---|
327 | if (descriptors == NULL) {
|
---|
328 | return ENOMEM;
|
---|
329 | }
|
---|
330 |
|
---|
331 | size_t transferred = 0;
|
---|
332 | // get full configuration descriptor
|
---|
333 | rc = usb_request_get_full_configuration_descriptor(&kbd_dev->ctrl_pipe,
|
---|
334 | 0, descriptors,
|
---|
335 | config_desc.total_length, &transferred);
|
---|
336 |
|
---|
337 | if (rc != EOK) {
|
---|
338 | return rc;
|
---|
339 | }
|
---|
340 | if (transferred != config_desc.total_length) {
|
---|
341 | return ELIMIT;
|
---|
342 | }
|
---|
343 |
|
---|
344 | /*
|
---|
345 | * Initialize the interrupt in endpoint.
|
---|
346 | */
|
---|
347 | usb_endpoint_mapping_t endpoint_mapping[1] = {
|
---|
348 | {
|
---|
349 | .pipe = &kbd_dev->poll_pipe,
|
---|
350 | .description = &poll_endpoint_description
|
---|
351 | }
|
---|
352 | };
|
---|
353 | rc = usb_endpoint_pipe_initialize_from_configuration(
|
---|
354 | endpoint_mapping, 1,
|
---|
355 | descriptors, config_desc.total_length,
|
---|
356 | &kbd_dev->wire);
|
---|
357 | if (rc != EOK) {
|
---|
358 | usb_log_error("Failed to initialize poll pipe: %s.\n",
|
---|
359 | str_error(rc));
|
---|
360 | return rc;
|
---|
361 | }
|
---|
362 | if (!endpoint_mapping[0].present) {
|
---|
363 | usb_log_warning("Not accepting device, " \
|
---|
364 | "not boot-protocol keyboard.\n");
|
---|
365 | return EREFUSED;
|
---|
366 | }
|
---|
367 |
|
---|
368 |
|
---|
369 |
|
---|
370 |
|
---|
371 | kbd_dev->conf = (usb_hid_configuration_t *)calloc(1,
|
---|
372 | sizeof(usb_hid_configuration_t));
|
---|
373 | if (kbd_dev->conf == NULL) {
|
---|
374 | free(descriptors);
|
---|
375 | return ENOMEM;
|
---|
376 | }
|
---|
377 |
|
---|
378 | /*rc = usbkbd_parse_descriptors(descriptors, transferred, kbd_dev->conf);
|
---|
379 | free(descriptors);
|
---|
380 | if (rc != EOK) {
|
---|
381 | printf("Problem with parsing standard descriptors.\n");
|
---|
382 | return rc;
|
---|
383 | }
|
---|
384 |
|
---|
385 | // get and report descriptors*/
|
---|
386 | rc = usbkbd_get_report_descriptor(kbd_dev);
|
---|
387 | if (rc != EOK) {
|
---|
388 | printf("Problem with parsing HID REPORT descriptor.\n");
|
---|
389 | return rc;
|
---|
390 | }
|
---|
391 |
|
---|
392 | //usbkbd_print_config(kbd_dev->conf);
|
---|
393 |
|
---|
394 | /*
|
---|
395 | * TODO:
|
---|
396 | * 1) select one configuration (lets say the first)
|
---|
397 | * 2) how many interfaces?? how to select one??
|
---|
398 | * ("The default setting for an interface is always alternate setting zero.")
|
---|
399 | * 3) find endpoint which is IN and INTERRUPT (parse), save its number
|
---|
400 | * as the endpoint for polling
|
---|
401 | */
|
---|
402 |
|
---|
403 | return EOK;
|
---|
404 | }
|
---|
405 |
|
---|
406 | static usb_hid_dev_kbd_t *usbkbd_init_device(device_t *dev)
|
---|
407 | {
|
---|
408 | int rc;
|
---|
409 |
|
---|
410 | usb_hid_dev_kbd_t *kbd_dev = (usb_hid_dev_kbd_t *)calloc(1,
|
---|
411 | sizeof(usb_hid_dev_kbd_t));
|
---|
412 |
|
---|
413 | if (kbd_dev == NULL) {
|
---|
414 | fprintf(stderr, NAME ": No memory!\n");
|
---|
415 | return NULL;
|
---|
416 | }
|
---|
417 |
|
---|
418 | kbd_dev->device = dev;
|
---|
419 |
|
---|
420 | /*
|
---|
421 | * Initialize the backing connection to the host controller.
|
---|
422 | */
|
---|
423 | rc = usb_device_connection_initialize_from_device(&kbd_dev->wire, dev);
|
---|
424 | if (rc != EOK) {
|
---|
425 | printf("Problem initializing connection to device: %s.\n",
|
---|
426 | str_error(rc));
|
---|
427 | goto error_leave;
|
---|
428 | }
|
---|
429 |
|
---|
430 | /*
|
---|
431 | * Initialize device pipes.
|
---|
432 | */
|
---|
433 | rc = usb_endpoint_pipe_initialize_default_control(&kbd_dev->ctrl_pipe,
|
---|
434 | &kbd_dev->wire);
|
---|
435 | if (rc != EOK) {
|
---|
436 | printf("Failed to initialize default control pipe: %s.\n",
|
---|
437 | str_error(rc));
|
---|
438 | goto error_leave;
|
---|
439 | }
|
---|
440 |
|
---|
441 | /*
|
---|
442 | * will need all descriptors:
|
---|
443 | * 1) choose one configuration from configuration descriptors
|
---|
444 | * (set it to the device)
|
---|
445 | * 2) set endpoints from endpoint descriptors
|
---|
446 | */
|
---|
447 |
|
---|
448 | // TODO: get descriptors, parse descriptors and save endpoints
|
---|
449 | usb_endpoint_pipe_start_session(&kbd_dev->ctrl_pipe);
|
---|
450 | //usb_request_set_configuration(&kbd_dev->ctrl_pipe, 1);
|
---|
451 | rc = usbkbd_process_descriptors(kbd_dev);
|
---|
452 | usb_endpoint_pipe_end_session(&kbd_dev->ctrl_pipe);
|
---|
453 | if (rc != EOK) {
|
---|
454 | goto error_leave;
|
---|
455 | }
|
---|
456 |
|
---|
457 | return kbd_dev;
|
---|
458 |
|
---|
459 | error_leave:
|
---|
460 | free(kbd_dev);
|
---|
461 | return NULL;
|
---|
462 | }
|
---|
463 |
|
---|
464 | static void usbkbd_process_interrupt_in(usb_hid_dev_kbd_t *kbd_dev,
|
---|
465 | uint8_t *buffer, size_t actual_size)
|
---|
466 | {
|
---|
467 | usb_hid_report_in_callbacks_t *callbacks =
|
---|
468 | (usb_hid_report_in_callbacks_t *)malloc(
|
---|
469 | sizeof(usb_hid_report_in_callbacks_t));
|
---|
470 | callbacks->keyboard = usbkbd_process_keycodes;
|
---|
471 |
|
---|
472 | //usb_hid_parse_report(kbd_dev->parser, buffer, actual_size, callbacks,
|
---|
473 | // NULL);
|
---|
474 | printf("Calling usb_hid_boot_keyboard_input_report() with size %zu\n",
|
---|
475 | actual_size);
|
---|
476 | //dump_buffer("bufffer: ", buffer, actual_size);
|
---|
477 | int rc = usb_hid_boot_keyboard_input_report(buffer, actual_size, callbacks,
|
---|
478 | NULL);
|
---|
479 | if (rc != EOK) {
|
---|
480 | printf("Error in usb_hid_boot_keyboard_input_report(): %d\n", rc);
|
---|
481 | }
|
---|
482 | }
|
---|
483 |
|
---|
484 | static void usbkbd_poll_keyboard(usb_hid_dev_kbd_t *kbd_dev)
|
---|
485 | {
|
---|
486 | int rc, sess_rc;
|
---|
487 | uint8_t buffer[BUFFER_SIZE];
|
---|
488 | size_t actual_size;
|
---|
489 |
|
---|
490 | printf("Polling keyboard...\n");
|
---|
491 |
|
---|
492 | while (true) {
|
---|
493 | async_usleep(1000 * 10);
|
---|
494 |
|
---|
495 | sess_rc = usb_endpoint_pipe_start_session(&kbd_dev->poll_pipe);
|
---|
496 | if (sess_rc != EOK) {
|
---|
497 | printf("Failed to start a session: %s.\n",
|
---|
498 | str_error(sess_rc));
|
---|
499 | continue;
|
---|
500 | }
|
---|
501 |
|
---|
502 | rc = usb_endpoint_pipe_read(&kbd_dev->poll_pipe, buffer,
|
---|
503 | BUFFER_SIZE, &actual_size);
|
---|
504 | sess_rc = usb_endpoint_pipe_end_session(&kbd_dev->poll_pipe);
|
---|
505 |
|
---|
506 | if (rc != EOK) {
|
---|
507 | printf("Error polling the keyboard: %s.\n",
|
---|
508 | str_error(rc));
|
---|
509 | continue;
|
---|
510 | }
|
---|
511 |
|
---|
512 | if (sess_rc != EOK) {
|
---|
513 | printf("Error closing session: %s.\n",
|
---|
514 | str_error(sess_rc));
|
---|
515 | continue;
|
---|
516 | }
|
---|
517 |
|
---|
518 | /*
|
---|
519 | * If the keyboard answered with NAK, it returned no data.
|
---|
520 | * This implies that no change happened since last query.
|
---|
521 | */
|
---|
522 | if (actual_size == 0) {
|
---|
523 | printf("Keyboard returned NAK\n");
|
---|
524 | continue;
|
---|
525 | }
|
---|
526 |
|
---|
527 | /*
|
---|
528 | * TODO: Process pressed keys.
|
---|
529 | */
|
---|
530 | printf("Calling usbkbd_process_interrupt_in()\n");
|
---|
531 | usbkbd_process_interrupt_in(kbd_dev, buffer, actual_size);
|
---|
532 | }
|
---|
533 |
|
---|
534 | // not reached
|
---|
535 | assert(0);
|
---|
536 | }
|
---|
537 |
|
---|
538 | static int usbkbd_fibril_device(void *arg)
|
---|
539 | {
|
---|
540 | printf("!!! USB device fibril\n");
|
---|
541 |
|
---|
542 | if (arg == NULL) {
|
---|
543 | printf("No device!\n");
|
---|
544 | return -1;
|
---|
545 | }
|
---|
546 |
|
---|
547 | device_t *dev = (device_t *)arg;
|
---|
548 |
|
---|
549 | // initialize device (get and process descriptors, get address, etc.)
|
---|
550 | usb_hid_dev_kbd_t *kbd_dev = usbkbd_init_device(dev);
|
---|
551 | if (kbd_dev == NULL) {
|
---|
552 | printf("Error while initializing device.\n");
|
---|
553 | return -1;
|
---|
554 | }
|
---|
555 |
|
---|
556 | usbkbd_poll_keyboard(kbd_dev);
|
---|
557 |
|
---|
558 | return EOK;
|
---|
559 | }
|
---|
560 |
|
---|
561 | static int usbkbd_add_device(device_t *dev)
|
---|
562 | {
|
---|
563 | /* For now, fail immediately. */
|
---|
564 | //return ENOTSUP;
|
---|
565 |
|
---|
566 | /*
|
---|
567 | * When everything is okay, connect to "our" HC.
|
---|
568 | *
|
---|
569 | * Not supported yet, skip..
|
---|
570 | */
|
---|
571 | // int phone = usb_drv_hc_connect_auto(dev, 0);
|
---|
572 | // if (phone < 0) {
|
---|
573 | // /*
|
---|
574 | // * Connecting to HC failed, roll-back and announce
|
---|
575 | // * failure.
|
---|
576 | // */
|
---|
577 | // return phone;
|
---|
578 | // }
|
---|
579 |
|
---|
580 | // dev->parent_phone = phone;
|
---|
581 |
|
---|
582 | /*
|
---|
583 | * Create new fibril for handling this keyboard
|
---|
584 | */
|
---|
585 | fid_t fid = fibril_create(usbkbd_fibril_device, dev);
|
---|
586 | if (fid == 0) {
|
---|
587 | printf("%s: failed to start fibril for HID device\n", NAME);
|
---|
588 | return ENOMEM;
|
---|
589 | }
|
---|
590 | fibril_add_ready(fid);
|
---|
591 |
|
---|
592 | dev->ops = &keyboard_ops;
|
---|
593 |
|
---|
594 | add_device_to_class(dev, "keyboard");
|
---|
595 |
|
---|
596 | /*
|
---|
597 | * Hurrah, device is initialized.
|
---|
598 | */
|
---|
599 | return EOK;
|
---|
600 | }
|
---|
601 |
|
---|
602 | static driver_ops_t kbd_driver_ops = {
|
---|
603 | .add_device = usbkbd_add_device,
|
---|
604 | };
|
---|
605 |
|
---|
606 | static driver_t kbd_driver = {
|
---|
607 | .name = NAME,
|
---|
608 | .driver_ops = &kbd_driver_ops
|
---|
609 | };
|
---|
610 |
|
---|
611 | int main(int argc, char *argv[])
|
---|
612 | {
|
---|
613 | usb_log_enable(USB_LOG_LEVEL_INFO, "usbhid");
|
---|
614 | return driver_main(&kbd_driver);
|
---|
615 | }
|
---|
616 |
|
---|
617 | /**
|
---|
618 | * @}
|
---|
619 | */
|
---|