source: mainline/uspace/drv/usbhid/main.c@ 4e38d69

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

HID driver uses pipe API (again)

The SET_CONFIGURATION request is commented because the implementation behind
is wrong. Will fix soon.

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