source: mainline/uspace/drv/usbhid/main.c@ 7ce0fe3

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

Rename usbkbd' driver to usbhid'

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