source: mainline/uspace/drv/usbhid/main.c@ c2772b8

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

Merge development/ changes

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