source: mainline/uspace/drv/usbhid/main.c@ 3cc5ccda

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3cc5ccda was ad29045, checked in by Jan Vesely <jano.vesely@…>, 15 years ago

Enable usbhid

  • Property mode set to 100644
File size: 14.1 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/devreq.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# if 0
265/*
266 * Kbd functions
267 */
268static int usbkbd_get_report_descriptor(usb_hid_dev_kbd_t *kbd_dev)
269{
270 // iterate over all configurations and interfaces
271 // TODO: more configurations!!
272 unsigned i;
273 for (i = 0; i < kbd_dev->conf->config_descriptor.interface_count; ++i) {
274 // TODO: endianness
275 uint16_t length =
276 kbd_dev->conf->interfaces[i].hid_desc.report_desc_info.length;
277 size_t actual_size = 0;
278
279 // allocate space for the report descriptor
280 kbd_dev->conf->interfaces[i].report_desc = (uint8_t *)malloc(length);
281
282 // get the descriptor from the device
283 int rc = usb_drv_req_get_descriptor(kbd_dev->device->parent_phone,
284 kbd_dev->address, USB_REQUEST_TYPE_CLASS, USB_DESCTYPE_HID_REPORT,
285 0, i, 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 = usb_drv_req_get_bare_configuration_descriptor(
306 kbd_dev->device->parent_phone, kbd_dev->address, 0, &config_desc);
307
308 if (rc != EOK) {
309 return rc;
310 }
311
312 // prepare space for all underlying descriptors
313 uint8_t *descriptors = (uint8_t *)malloc(config_desc.total_length);
314 if (descriptors == NULL) {
315 return ENOMEM;
316 }
317
318 size_t transferred = 0;
319 // get full configuration descriptor
320 rc = usb_drv_req_get_full_configuration_descriptor(
321 kbd_dev->device->parent_phone, kbd_dev->address, 0, descriptors,
322 config_desc.total_length, &transferred);
323
324 if (rc != EOK) {
325 return rc;
326 }
327 if (transferred != config_desc.total_length) {
328 return ELIMIT;
329 }
330
331 kbd_dev->conf = (usb_hid_configuration_t *)calloc(1,
332 sizeof(usb_hid_configuration_t));
333 if (kbd_dev->conf == NULL) {
334 free(descriptors);
335 return ENOMEM;
336 }
337
338 rc = usbkbd_parse_descriptors(descriptors, transferred, kbd_dev->conf);
339 free(descriptors);
340 if (rc != EOK) {
341 printf("Problem with parsing standard descriptors.\n");
342 return rc;
343 }
344
345 // get and report descriptors
346 rc = usbkbd_get_report_descriptor(kbd_dev);
347 if (rc != EOK) {
348 printf("Problem with parsing HID REPORT descriptor.\n");
349 return rc;
350 }
351
352 //usbkbd_print_config(kbd_dev->conf);
353
354 /*
355 * TODO:
356 * 1) select one configuration (lets say the first)
357 * 2) how many interfaces?? how to select one??
358 * ("The default setting for an interface is always alternate setting zero.")
359 * 3) find endpoint which is IN and INTERRUPT (parse), save its number
360 * as the endpoint for polling
361 */
362
363 return EOK;
364}
365#endif
366static usb_hid_dev_kbd_t *usbkbd_init_device(device_t *dev)
367{
368 usb_hid_dev_kbd_t *kbd_dev = (usb_hid_dev_kbd_t *)calloc(1,
369 sizeof(usb_hid_dev_kbd_t));
370
371 if (kbd_dev == NULL) {
372 fprintf(stderr, NAME ": No memory!\n");
373 return NULL;
374 }
375
376 kbd_dev->device = dev;
377
378 // get phone to my HC and save it as my parent's phone
379 // TODO: maybe not a good idea if DDF will use parent_phone
380 int rc = kbd_dev->device->parent_phone = usb_drv_hc_connect_auto(dev, 0);
381 if (rc < 0) {
382 printf("Problem setting phone to HC.\n");
383 goto error_leave;
384 }
385
386 rc = kbd_dev->address = usb_drv_get_my_address(dev->parent_phone, dev);
387 if (rc < 0) {
388 printf("Problem getting address of the device.\n");
389 goto error_leave;
390 }
391
392 // doesn't matter now that we have no address
393// if (kbd_dev->address < 0) {
394// fprintf(stderr, NAME ": No device address!\n");
395// free(kbd_dev);
396// return NULL;
397// }
398
399 /*
400 * will need all descriptors:
401 * 1) choose one configuration from configuration descriptors
402 * (set it to the device)
403 * 2) set endpoints from endpoint descriptors
404 */
405
406
407 // TODO: get descriptors, parse descriptors and save endpoints
408 //usbkbd_process_descriptors(kbd_dev);
409 usb_drv_req_set_configuration(
410 kbd_dev->device->parent_phone, kbd_dev->address, 1);
411
412
413
414 /*
415 * Initialize the backing connection to the host controller.
416 */
417 rc = usb_device_connection_initialize_from_device(&kbd_dev->wire, dev);
418 if (rc != EOK) {
419 printf("Problem initializing connection to device: %s.\n",
420 str_error(rc));
421 goto error_leave;
422 }
423
424 /*
425 * Initialize device pipes.
426 */
427 rc = usb_endpoint_pipe_initialize(&kbd_dev->poll_pipe, &kbd_dev->wire,
428 GUESSED_POLL_ENDPOINT, USB_TRANSFER_INTERRUPT, USB_DIRECTION_IN);
429 if (rc != EOK) {
430 printf("Failed to initialize interrupt in pipe: %s.\n",
431 str_error(rc));
432 goto error_leave;
433 }
434
435
436 return kbd_dev;
437
438error_leave:
439 free(kbd_dev);
440 return NULL;
441}
442
443static void usbkbd_process_interrupt_in(usb_hid_dev_kbd_t *kbd_dev,
444 uint8_t *buffer, size_t actual_size)
445{
446 usb_hid_report_in_callbacks_t *callbacks =
447 (usb_hid_report_in_callbacks_t *)malloc(
448 sizeof(usb_hid_report_in_callbacks_t));
449 callbacks->keyboard = usbkbd_process_keycodes;
450
451 //usb_hid_parse_report(kbd_dev->parser, buffer, actual_size, callbacks,
452 // NULL);
453 printf("Calling usb_hid_boot_keyboard_input_report() with size %zu\n",
454 actual_size);
455 //dump_buffer("bufffer: ", buffer, actual_size);
456 int rc = usb_hid_boot_keyboard_input_report(buffer, actual_size, callbacks,
457 NULL);
458 if (rc != EOK) {
459 printf("Error in usb_hid_boot_keyboard_input_report(): %d\n", rc);
460 }
461}
462
463static void usbkbd_poll_keyboard(usb_hid_dev_kbd_t *kbd_dev)
464{
465 int rc, sess_rc;
466 uint8_t buffer[BUFFER_SIZE];
467 size_t actual_size;
468
469 printf("Polling keyboard...\n");
470
471 while (true) {
472 async_usleep(1000 * 10);
473
474 sess_rc = usb_endpoint_pipe_start_session(&kbd_dev->poll_pipe);
475 if (sess_rc != EOK) {
476 printf("Failed to start a session: %s.\n",
477 str_error(sess_rc));
478 continue;
479 }
480
481 rc = usb_endpoint_pipe_read(&kbd_dev->poll_pipe, buffer,
482 BUFFER_SIZE, &actual_size);
483 sess_rc = usb_endpoint_pipe_end_session(&kbd_dev->poll_pipe);
484
485 if (rc != EOK) {
486 printf("Error polling the keyboard: %s.\n",
487 str_error(rc));
488 continue;
489 }
490
491 if (sess_rc != EOK) {
492 printf("Error closing session: %s.\n",
493 str_error(sess_rc));
494 continue;
495 }
496
497 /*
498 * If the keyboard answered with NAK, it returned no data.
499 * This implies that no change happened since last query.
500 */
501 if (actual_size == 0) {
502 printf("Keyboard returned NAK\n");
503 continue;
504 }
505
506 /*
507 * TODO: Process pressed keys.
508 */
509 printf("Calling usbkbd_process_interrupt_in()\n");
510 usbkbd_process_interrupt_in(kbd_dev, buffer, actual_size);
511 }
512
513 // not reached
514 assert(0);
515}
516
517static int usbkbd_fibril_device(void *arg)
518{
519 printf("!!! USB device fibril\n");
520
521 if (arg == NULL) {
522 printf("No device!\n");
523 return -1;
524 }
525
526 device_t *dev = (device_t *)arg;
527
528 // initialize device (get and process descriptors, get address, etc.)
529 usb_hid_dev_kbd_t *kbd_dev = usbkbd_init_device(dev);
530 if (kbd_dev == NULL) {
531 printf("Error while initializing device.\n");
532 return -1;
533 }
534
535 usbkbd_poll_keyboard(kbd_dev);
536
537 return EOK;
538}
539
540static int usbkbd_add_device(device_t *dev)
541{
542 /* For now, fail immediately. */
543 //return ENOTSUP;
544
545 /*
546 * When everything is okay, connect to "our" HC.
547 *
548 * Not supported yet, skip..
549 */
550// int phone = usb_drv_hc_connect_auto(dev, 0);
551// if (phone < 0) {
552// /*
553// * Connecting to HC failed, roll-back and announce
554// * failure.
555// */
556// return phone;
557// }
558
559// dev->parent_phone = phone;
560
561 /*
562 * Create new fibril for handling this keyboard
563 */
564 fid_t fid = fibril_create(usbkbd_fibril_device, dev);
565 if (fid == 0) {
566 printf("%s: failed to start fibril for HID device\n", NAME);
567 return ENOMEM;
568 }
569 fibril_add_ready(fid);
570
571 dev->ops = &keyboard_ops;
572
573 add_device_to_class(dev, "keyboard");
574
575 /*
576 * Hurrah, device is initialized.
577 */
578 return EOK;
579}
580
581static driver_ops_t kbd_driver_ops = {
582 .add_device = usbkbd_add_device,
583};
584
585static driver_t kbd_driver = {
586 .name = NAME,
587 .driver_ops = &kbd_driver_ops
588};
589
590int main(int argc, char *argv[])
591{
592 return driver_main(&kbd_driver);
593}
594
595/**
596 * @}
597 */
Note: See TracBrowser for help on using the repository browser.