source: mainline/uspace/drv/usbkbd/main.c@ c113056

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

Fix wrong printf directive

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