source: mainline/uspace/drv/usbhid/main.c@ 707ffcf

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

USB HID driver uses pipes for interrupt endpoint

  • Property mode set to 100644
File size: 14.0 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 32
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 ipc_answer_0(icallid, ELIMIT);
86 return;
87 }
88
89 console_callback_phone = callback;
90 ipc_answer_0(icallid, EOK);
91 return;
92 }
93
94 ipc_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_drv_req_get_descriptor(kbd_dev->device->parent_phone,
283 kbd_dev->address, USB_REQUEST_TYPE_CLASS, USB_DESCTYPE_HID_REPORT,
284 0, i, kbd_dev->conf->interfaces[i].report_desc, length,
285 &actual_size);
286
287 if (rc != EOK) {
288 return rc;
289 }
290
291 assert(actual_size == length);
292
293 //dump_hid_class_descriptor(0, USB_DESCTYPE_HID_REPORT,
294 // kbd_dev->conf->interfaces[i].report_desc, length);
295 }
296
297 return EOK;
298}
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
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 // TODO: get descriptors, parse descriptors and save endpoints
407 usbkbd_process_descriptors(kbd_dev);
408
409
410
411 /*
412 * Initialize the backing connection to the host controller.
413 */
414 rc = usb_device_connection_initialize(&kbd_dev->wire, dev);
415 if (rc != EOK) {
416 printf("Problem initializing connection to device: %s.\n",
417 str_error(rc));
418 goto error_leave;
419 }
420
421 /*
422 * Initialize device pipes.
423 */
424 rc = usb_endpoint_pipe_initialize(&kbd_dev->poll_pipe, &kbd_dev->wire,
425 GUESSED_POLL_ENDPOINT, USB_TRANSFER_INTERRUPT, USB_DIRECTION_IN);
426 if (rc != EOK) {
427 printf("Failed to initialize interrupt in pipe: %s.\n",
428 str_error(rc));
429 goto error_leave;
430 }
431
432
433 return kbd_dev;
434
435error_leave:
436 free(kbd_dev);
437 return NULL;
438}
439
440static void usbkbd_process_interrupt_in(usb_hid_dev_kbd_t *kbd_dev,
441 uint8_t *buffer, size_t actual_size)
442{
443 usb_hid_report_in_callbacks_t *callbacks =
444 (usb_hid_report_in_callbacks_t *)malloc(
445 sizeof(usb_hid_report_in_callbacks_t));
446 callbacks->keyboard = usbkbd_process_keycodes;
447
448 //usb_hid_parse_report(kbd_dev->parser, buffer, actual_size, callbacks,
449 // NULL);
450 printf("Calling usb_hid_boot_keyboard_input_report() with size %zu\n",
451 actual_size);
452 //dump_buffer("bufffer: ", buffer, actual_size);
453 int rc = usb_hid_boot_keyboard_input_report(buffer, actual_size, callbacks,
454 NULL);
455 if (rc != EOK) {
456 printf("Error in usb_hid_boot_keyboard_input_report(): %d\n", rc);
457 }
458}
459
460static void usbkbd_poll_keyboard(usb_hid_dev_kbd_t *kbd_dev)
461{
462 int rc, sess_rc;
463 uint8_t buffer[BUFFER_SIZE];
464 size_t actual_size;
465
466 printf("Polling keyboard...\n");
467
468 while (true) {
469 async_usleep(1000 * 1000 * 2);
470
471 sess_rc = usb_endpoint_pipe_start_session(&kbd_dev->poll_pipe);
472 if (sess_rc != EOK) {
473 printf("Failed to start a session: %s.\n",
474 str_error(sess_rc));
475 continue;
476 }
477
478 rc = usb_endpoint_pipe_read(&kbd_dev->poll_pipe, buffer,
479 BUFFER_SIZE, &actual_size);
480 sess_rc = usb_endpoint_pipe_end_session(&kbd_dev->poll_pipe);
481
482 if (rc != EOK) {
483 printf("Error polling the keyboard: %s.\n",
484 str_error(rc));
485 continue;
486 }
487
488 if (sess_rc != EOK) {
489 printf("Error closing session: %s.\n",
490 str_error(sess_rc));
491 continue;
492 }
493
494 /*
495 * If the keyboard answered with NAK, it returned no data.
496 * This implies that no change happened since last query.
497 */
498 if (actual_size == 0) {
499 printf("Keyboard returned NAK\n");
500 continue;
501 }
502
503 /*
504 * TODO: Process pressed keys.
505 */
506 printf("Calling usbkbd_process_interrupt_in()\n");
507 usbkbd_process_interrupt_in(kbd_dev, buffer, actual_size);
508 }
509
510 // not reached
511 assert(0);
512}
513
514static int usbkbd_fibril_device(void *arg)
515{
516 printf("!!! USB device fibril\n");
517
518 if (arg == NULL) {
519 printf("No device!\n");
520 return -1;
521 }
522
523 device_t *dev = (device_t *)arg;
524
525 // initialize device (get and process descriptors, get address, etc.)
526 usb_hid_dev_kbd_t *kbd_dev = usbkbd_init_device(dev);
527 if (kbd_dev == NULL) {
528 printf("Error while initializing device.\n");
529 return -1;
530 }
531
532 usbkbd_poll_keyboard(kbd_dev);
533
534 return EOK;
535}
536
537static int usbkbd_add_device(device_t *dev)
538{
539 /* For now, fail immediately. */
540 //return ENOTSUP;
541
542 /*
543 * When everything is okay, connect to "our" HC.
544 *
545 * Not supported yet, skip..
546 */
547// int phone = usb_drv_hc_connect_auto(dev, 0);
548// if (phone < 0) {
549// /*
550// * Connecting to HC failed, roll-back and announce
551// * failure.
552// */
553// return phone;
554// }
555
556// dev->parent_phone = phone;
557
558 /*
559 * Create new fibril for handling this keyboard
560 */
561 fid_t fid = fibril_create(usbkbd_fibril_device, dev);
562 if (fid == 0) {
563 printf("%s: failed to start fibril for HID device\n", NAME);
564 return ENOMEM;
565 }
566 fibril_add_ready(fid);
567
568 dev->ops = &keyboard_ops;
569
570 add_device_to_class(dev, "keyboard");
571
572 /*
573 * Hurrah, device is initialized.
574 */
575 return EOK;
576}
577
578static driver_ops_t kbd_driver_ops = {
579 .add_device = usbkbd_add_device,
580};
581
582static driver_t kbd_driver = {
583 .name = NAME,
584 .driver_ops = &kbd_driver_ops
585};
586
587int main(int argc, char *argv[])
588{
589 return driver_main(&kbd_driver);
590}
591
592/**
593 * @}
594 */
Note: See TracBrowser for help on using the repository browser.