source: mainline/uspace/drv/usbhid/main.c@ b9d910f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b9d910f was b43bcf1, checked in by Vojtech Horky <vojtechhorky@…>, 15 years ago

Better initialization of poll pipe in HID driver

  • Property mode set to 100644
File size: 14.6 KB
RevLine 
[c7137738]1/*
2 * Copyright (c) 2010 Vojtech Horky
[1c13dac]3 * Copyright (c) 2011 Lubos Slovak
[c7137738]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 */
[1c13dac]29
[ba54451]30/** @addtogroup drvusbhid
31 * @{
32 */
[1c13dac]33/**
34 * @file
35 * Main routines of USB HID driver.
36 */
37
[c7137738]38#include <usb/usbdrv.h>
39#include <driver.h>
[2e15ac40]40#include <ipc/driver.h>
[700af62]41#include <ipc/kbd.h>
42#include <io/keycode.h>
43#include <io/console.h>
[c7137738]44#include <errno.h>
[707ffcf]45#include <str_error.h>
[2e15ac40]46#include <fibril.h>
[b43bcf1]47#include <usb/debug.h>
48#include <usb/classes/classes.h>
[2e15ac40]49#include <usb/classes/hid.h>
[243cb86]50#include <usb/classes/hidparser.h>
[03197ffc]51#include <usb/request.h>
[dafab9e0]52#include <usb/descriptor.h>
[6336b6e]53#include <io/console.h>
[54b5625]54#include "hid.h"
[ca038b4]55#include "descparser.h"
[45019865]56#include "descdump.h"
[6336b6e]57#include "conv.h"
[7c169ce]58#include "layout.h"
[c7137738]59
[2899980]60#define BUFFER_SIZE 8
[66d5062]61#define NAME "usbhid"
[2e15ac40]62
[1b29d6fa]63#define GUESSED_POLL_ENDPOINT 1
[2e15ac40]64
[b43bcf1]65/** Keyboard polling endpoint description for boot protocol class. */
66static 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
[700af62]75static void default_connection_handler(device_t *, ipc_callid_t, ipc_call_t *);
76static device_ops_t keyboard_ops = {
77 .default_handler = default_connection_handler
78};
79
80static 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 */
88void 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) {
[17aca1c]97 async_answer_0(icallid, ELIMIT);
[700af62]98 return;
99 }
100
101 console_callback_phone = callback;
[17aca1c]102 async_answer_0(icallid, EOK);
[700af62]103 return;
104 }
105
[17aca1c]106 async_answer_0(icallid, EINVAL);
[700af62]107}
108
[38c5dfa]109#if 0
[700af62]110static 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}
[38c5dfa]114#endif
[700af62]115
[6336b6e]116/*
117 * TODO: Move somewhere else
118 */
119/*
120#define BYTES_PER_LINE 12
121
122static 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 */
144static unsigned mods = KM_NUM_LOCK;
145
146/** Currently pressed lock keys. We track these to tackle autorepeat.
147 *
148 * TODO: put to device?
149 */
150static unsigned lock_keys;
151
[7c169ce]152#define NUM_LAYOUTS 3
153
154static layout_op_t *layout[NUM_LAYOUTS] = {
155 &us_qwerty_op,
156 &us_dvorak_op,
157 &cz_op
158};
159
160static int active_layout = 0;
[6336b6e]161
162static 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*/
[11797d5]214
[6336b6e]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 }
[11797d5]235
[6336b6e]236 ev.type = type;
237 ev.key = key;
238 ev.mods = mods;
239
[7c169ce]240 ev.c = layout[active_layout]->parse_ev(&ev);
[6336b6e]241
242 printf("Sending key %d to the console\n", ev.key);
243 assert(console_callback_phone != -1);
[7c169ce]244 async_msg_4(console_callback_phone, KBD_EVENT, ev.type, ev.key, ev.mods, ev.c);
[6336b6e]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
[243cb86]257/*
258 * Callbacks for parser
259 */
[0a9ea4a]260static void usbkbd_process_keycodes(const uint8_t *key_codes, size_t count,
[11797d5]261 uint8_t modifiers, void *arg)
[91db50ac]262{
[692f13e4]263 printf("Got keys: ");
264 unsigned i;
265 for (i = 0; i < count; ++i) {
266 printf("%d ", key_codes[i]);
[6336b6e]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);
[692f13e4]272 }
273 printf("\n");
[243cb86]274}
[0e126be7]275
[243cb86]276/*
277 * Kbd functions
278 */
[6986418]279static 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;
[45019865]288 size_t actual_size = 0;
[6986418]289
290 // allocate space for the report descriptor
291 kbd_dev->conf->interfaces[i].report_desc = (uint8_t *)malloc(length);
292
[45019865]293 // get the descriptor from the device
[03197ffc]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,
[45019865]298 &actual_size);
299
300 if (rc != EOK) {
301 return rc;
302 }
303
304 assert(actual_size == length);
305
[fbddf94]306 //dump_hid_class_descriptor(0, USB_DESCTYPE_HID_REPORT,
307 // kbd_dev->conf->interfaces[i].report_desc, length);
[6986418]308 }
[45019865]309
310 return EOK;
[6986418]311}
[ca038b4]312static int usbkbd_process_descriptors(usb_hid_dev_kbd_t *kbd_dev)
[243cb86]313{
[ca038b4]314 // get the first configuration descriptor (TODO: parse also other!)
[dafab9e0]315 usb_standard_configuration_descriptor_t config_desc;
316
[03197ffc]317 int rc;
318 rc = usb_request_get_bare_configuration_descriptor(&kbd_dev->ctrl_pipe,
319 0, &config_desc);
[dafab9e0]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
[03197ffc]333 rc = usb_request_get_full_configuration_descriptor(&kbd_dev->ctrl_pipe,
334 0, descriptors,
[dafab9e0]335 config_desc.total_length, &transferred);
336
[91db50ac]337 if (rc != EOK) {
[243cb86]338 return rc;
[91db50ac]339 }
[dafab9e0]340 if (transferred != config_desc.total_length) {
341 return ELIMIT;
342 }
343
[b43bcf1]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
[ca038b4]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
[b43bcf1]378 /*rc = usbkbd_parse_descriptors(descriptors, transferred, kbd_dev->conf);
[dafab9e0]379 free(descriptors);
[45019865]380 if (rc != EOK) {
381 printf("Problem with parsing standard descriptors.\n");
382 return rc;
383 }
[6986418]384
[b43bcf1]385 // get and report descriptors*/
[6986418]386 rc = usbkbd_get_report_descriptor(kbd_dev);
[45019865]387 if (rc != EOK) {
388 printf("Problem with parsing HID REPORT descriptor.\n");
389 return rc;
390 }
[243cb86]391
[76cb03c]392 //usbkbd_print_config(kbd_dev->conf);
[0a9ea4a]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 */
[b43bcf1]402
[45019865]403 return EOK;
[243cb86]404}
[03197ffc]405
[2e15ac40]406static usb_hid_dev_kbd_t *usbkbd_init_device(device_t *dev)
407{
[03197ffc]408 int rc;
409
[ca038b4]410 usb_hid_dev_kbd_t *kbd_dev = (usb_hid_dev_kbd_t *)calloc(1,
411 sizeof(usb_hid_dev_kbd_t));
[2e15ac40]412
413 if (kbd_dev == NULL) {
414 fprintf(stderr, NAME ": No memory!\n");
415 return NULL;
416 }
417
418 kbd_dev->device = dev;
419
[707ffcf]420 /*
421 * Initialize the backing connection to the host controller.
422 */
[23c7f4d]423 rc = usb_device_connection_initialize_from_device(&kbd_dev->wire, dev);
[707ffcf]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 */
[03197ffc]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);
[b43bcf1]451 rc = usbkbd_process_descriptors(kbd_dev);
[03197ffc]452 usb_endpoint_pipe_end_session(&kbd_dev->ctrl_pipe);
[b43bcf1]453 if (rc != EOK) {
454 goto error_leave;
455 }
[707ffcf]456
[2e15ac40]457 return kbd_dev;
[707ffcf]458
459error_leave:
460 free(kbd_dev);
461 return NULL;
[2e15ac40]462}
[91db50ac]463
[101ef25c]464static void usbkbd_process_interrupt_in(usb_hid_dev_kbd_t *kbd_dev,
[243cb86]465 uint8_t *buffer, size_t actual_size)
[101ef25c]466{
[243cb86]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
[0a9ea4a]472 //usb_hid_parse_report(kbd_dev->parser, buffer, actual_size, callbacks,
473 // NULL);
[c113056]474 printf("Calling usb_hid_boot_keyboard_input_report() with size %zu\n",
[7c169ce]475 actual_size);
476 //dump_buffer("bufffer: ", buffer, actual_size);
[6336b6e]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 }
[101ef25c]482}
483
[2e15ac40]484static void usbkbd_poll_keyboard(usb_hid_dev_kbd_t *kbd_dev)
[91db50ac]485{
[707ffcf]486 int rc, sess_rc;
[243cb86]487 uint8_t buffer[BUFFER_SIZE];
[91db50ac]488 size_t actual_size;
[0e126be7]489
[6336b6e]490 printf("Polling keyboard...\n");
491
[101ef25c]492 while (true) {
[2899980]493 async_usleep(1000 * 10);
[101ef25c]494
[707ffcf]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));
[101ef25c]499 continue;
500 }
501
[707ffcf]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
[101ef25c]506 if (rc != EOK) {
[707ffcf]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));
[101ef25c]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) {
[7c169ce]523 printf("Keyboard returned NAK\n");
[101ef25c]524 continue;
525 }
526
527 /*
528 * TODO: Process pressed keys.
529 */
[6336b6e]530 printf("Calling usbkbd_process_interrupt_in()\n");
[101ef25c]531 usbkbd_process_interrupt_in(kbd_dev, buffer, actual_size);
[91db50ac]532 }
533
[101ef25c]534 // not reached
535 assert(0);
[91db50ac]536}
537
[2e15ac40]538static 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);
[6336b6e]551 if (kbd_dev == NULL) {
552 printf("Error while initializing device.\n");
553 return -1;
554 }
[2e15ac40]555
556 usbkbd_poll_keyboard(kbd_dev);
557
558 return EOK;
559}
560
561static int usbkbd_add_device(device_t *dev)
[c7137738]562{
563 /* For now, fail immediately. */
[101ef25c]564 //return ENOTSUP;
[c7137738]565
566 /*
567 * When everything is okay, connect to "our" HC.
[2e15ac40]568 *
569 * Not supported yet, skip..
[c7137738]570 */
[71ed4849]571// int phone = usb_drv_hc_connect_auto(dev, 0);
[2e15ac40]572// if (phone < 0) {
573// /*
574// * Connecting to HC failed, roll-back and announce
575// * failure.
576// */
577// return phone;
578// }
[c7137738]579
[2e15ac40]580// dev->parent_phone = phone;
[91db50ac]581
582 /*
[2e15ac40]583 * Create new fibril for handling this keyboard
[91db50ac]584 */
[2e15ac40]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);
[91db50ac]591
[700af62]592 dev->ops = &keyboard_ops;
593
594 add_device_to_class(dev, "keyboard");
595
[c7137738]596 /*
597 * Hurrah, device is initialized.
598 */
599 return EOK;
600}
601
602static driver_ops_t kbd_driver_ops = {
[2e15ac40]603 .add_device = usbkbd_add_device,
[c7137738]604};
605
606static driver_t kbd_driver = {
[2e15ac40]607 .name = NAME,
[c7137738]608 .driver_ops = &kbd_driver_ops
609};
610
611int main(int argc, char *argv[])
612{
[b43bcf1]613 usb_log_enable(USB_LOG_LEVEL_INFO, "usbhid");
[c7137738]614 return driver_main(&kbd_driver);
615}
[ba54451]616
617/**
618 * @}
619 */
Note: See TracBrowser for help on using the repository browser.