source: mainline/uspace/drv/usbhid/mouse/mousedev.c@ 79ae36dd

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 79ae36dd was 79ae36dd, checked in by Martin Decky <martin@…>, 14 years ago

new async framework with integrated exchange tracking

  • strict isolation between low-level IPC and high-level async framework with integrated exchange tracking
    • each IPC connection is represented by an async_sess_t structure
    • each IPC exchange is represented by an async_exch_t structure
    • exchange management is either based on atomic messages (EXCHANGE_ATOMIC), locking (EXCHANGE_SERIALIZE) or connection cloning (EXCHANGE_CLONE)
  • async_obsolete: temporary compatibility layer to keep old async clients working (several pieces of code are currently broken, but only non-essential functionality)
  • IPC_M_PHONE_HANGUP is now method no. 0 (for elegant boolean evaluation)
  • IPC_M_DEBUG_ALL has been renamed to IPC_M_DEBUG
  • IPC_M_PING has been removed (VFS protocol now has VFS_IN_PING)
  • console routines in libc have been rewritten for better abstraction
  • additional use for libc-private header files (FILE structure opaque to the client)
  • various cstyle changes (typos, indentation, missing externs in header files, improved comments, etc.)
  • Property mode set to 100644
File size: 15.9 KB
Line 
1/*
2 * Copyright (c) 2011 Lubos Slovak, 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
29/** @addtogroup drvusbhid
30 * @{
31 */
32/**
33 * @file
34 * USB Mouse driver API.
35 */
36
37#include <usb/debug.h>
38#include <usb/classes/classes.h>
39#include <usb/hid/hid.h>
40#include <usb/hid/request.h>
41#include <usb/hid/usages/core.h>
42#include <errno.h>
43#include <async.h>
44#include <async_obsolete.h>
45#include <str_error.h>
46#include <ipc/mouse.h>
47#include <io/console.h>
48
49#include <ipc/kbd.h>
50#include <io/keycode.h>
51
52#include "mousedev.h"
53#include "../usbhid.h"
54
55// FIXME: remove this header
56#include <kernel/ipc/ipc_methods.h>
57
58#define NAME "mouse"
59
60/*----------------------------------------------------------------------------*/
61
62usb_endpoint_description_t usb_hid_mouse_poll_endpoint_description = {
63 .transfer_type = USB_TRANSFER_INTERRUPT,
64 .direction = USB_DIRECTION_IN,
65 .interface_class = USB_CLASS_HID,
66 .interface_subclass = USB_HID_SUBCLASS_BOOT,
67 .interface_protocol = USB_HID_PROTOCOL_MOUSE,
68 .flags = 0
69};
70
71const char *HID_MOUSE_FUN_NAME = "mouse";
72const char *HID_MOUSE_WHEEL_FUN_NAME = "mouse-wheel";
73const char *HID_MOUSE_CLASS_NAME = "mouse";
74const char *HID_MOUSE_WHEEL_CLASS_NAME = "keyboard";
75
76/** Default idle rate for mouses. */
77static const uint8_t IDLE_RATE = 0;
78static const size_t USB_MOUSE_BUTTON_COUNT = 3;
79
80/*----------------------------------------------------------------------------*/
81
82enum {
83 USB_MOUSE_BOOT_REPORT_DESCRIPTOR_SIZE = 63
84};
85
86static const uint8_t USB_MOUSE_BOOT_REPORT_DESCRIPTOR[
87 USB_MOUSE_BOOT_REPORT_DESCRIPTOR_SIZE] = {
88 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
89 0x09, 0x02, // USAGE (Mouse)
90 0xa1, 0x01, // COLLECTION (Application)
91 0x09, 0x01, // USAGE (Pointer)
92 0xa1, 0x00, // COLLECTION (Physical)
93 0x95, 0x03, // REPORT_COUNT (3)
94 0x75, 0x01, // REPORT_SIZE (1)
95 0x05, 0x09, // USAGE_PAGE (Button)
96 0x19, 0x01, // USAGE_MINIMUM (Button 1)
97 0x29, 0x03, // USAGE_MAXIMUM (Button 3)
98 0x15, 0x00, // LOGICAL_MINIMUM (0)
99 0x25, 0x01, // LOGICAL_MAXIMUM (1)
100 0x81, 0x02, // INPUT (Data,Var,Abs)
101 0x95, 0x01, // REPORT_COUNT (1)
102 0x75, 0x05, // REPORT_SIZE (5)
103 0x81, 0x01, // INPUT (Cnst)
104 0x75, 0x08, // REPORT_SIZE (8)
105 0x95, 0x02, // REPORT_COUNT (2)
106 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
107 0x09, 0x30, // USAGE (X)
108 0x09, 0x31, // USAGE (Y)
109 0x15, 0x81, // LOGICAL_MINIMUM (-127)
110 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
111 0x81, 0x06, // INPUT (Data,Var,Rel)
112 0xc0, // END_COLLECTION
113 0xc0 // END_COLLECTION
114};
115
116/*----------------------------------------------------------------------------*/
117
118/** Default handler for IPC methods not handled by DDF.
119 *
120 * @param fun Device function handling the call.
121 * @param icallid Call id.
122 * @param icall Call data.
123 */
124static void default_connection_handler(ddf_fun_t *fun,
125 ipc_callid_t icallid, ipc_call_t *icall)
126{
127 sysarg_t method = IPC_GET_IMETHOD(*icall);
128
129 usb_mouse_t *mouse_dev = (usb_mouse_t *)fun->driver_data;
130
131 if (mouse_dev == NULL) {
132 usb_log_debug("default_connection_handler: Missing "
133 "parameters.\n");
134 async_answer_0(icallid, EINVAL);
135 return;
136 }
137
138 usb_log_debug("default_connection_handler: fun->name: %s\n",
139 fun->name);
140 usb_log_debug("default_connection_handler: mouse_phone: %d, wheel "
141 "phone: %d\n", mouse_dev->mouse_phone, mouse_dev->wheel_phone);
142
143 int *phone = (str_cmp(fun->name, HID_MOUSE_FUN_NAME) == 0)
144 ? &mouse_dev->mouse_phone : &mouse_dev->wheel_phone;
145
146 if (method == IPC_M_CONNECT_TO_ME) {
147 int callback = IPC_GET_ARG5(*icall);
148
149 if (*phone != -1) {
150 usb_log_debug("default_connection_handler: Console "
151 "phone to mouse already set.\n");
152 async_answer_0(icallid, ELIMIT);
153 return;
154 }
155
156 *phone = callback;
157 usb_log_debug("Console phone to mouse set ok (%d).\n", *phone);
158 async_answer_0(icallid, EOK);
159 return;
160 }
161
162 usb_log_debug("default_connection_handler: Invalid function.\n");
163 async_answer_0(icallid, EINVAL);
164}
165
166/*----------------------------------------------------------------------------*/
167
168static usb_mouse_t *usb_mouse_new(void)
169{
170 usb_mouse_t *mouse = calloc(1, sizeof(usb_mouse_t));
171 if (mouse == NULL) {
172 return NULL;
173 }
174 mouse->mouse_phone = -1;
175 mouse->wheel_phone = -1;
176
177 return mouse;
178}
179
180/*----------------------------------------------------------------------------*/
181
182static void usb_mouse_free(usb_mouse_t **mouse_dev)
183{
184 assert(mouse_dev != NULL && *mouse_dev != NULL);
185
186 // hangup phone to the console
187 if ((*mouse_dev)->mouse_phone >= 0) {
188 async_obsolete_hangup((*mouse_dev)->mouse_phone);
189 }
190
191 if ((*mouse_dev)->wheel_phone >= 0) {
192 async_obsolete_hangup((*mouse_dev)->wheel_phone);
193 }
194
195 free(*mouse_dev);
196 *mouse_dev = NULL;
197}
198
199/*----------------------------------------------------------------------------*/
200
201static void usb_mouse_send_wheel(const usb_mouse_t *mouse_dev, int wheel)
202{
203 kbd_event_t ev;
204
205 ev.type = KEY_PRESS;
206 ev.key = (wheel > 0) ? KC_UP : (wheel < 0) ? KC_DOWN : 0;
207 ev.mods = 0;
208 ev.c = 0;
209
210 if (mouse_dev->wheel_phone < 0) {
211 usb_log_warning(
212 "Connection to console not ready, key discarded.\n");
213 return;
214 }
215
216 int count = (wheel < 0) ? -wheel : wheel;
217 int i;
218
219 for (i = 0; i < count * 3; ++i) {
220 usb_log_debug2("Sending key %d to the console\n", ev.key);
221 async_obsolete_msg_4(mouse_dev->wheel_phone, KBD_EVENT, ev.type,
222 ev.key, ev.mods, ev.c);
223 // send key release right away
224 async_obsolete_msg_4(mouse_dev->wheel_phone, KBD_EVENT, KEY_RELEASE,
225 ev.key, ev.mods, ev.c);
226 }
227}
228
229/*----------------------------------------------------------------------------*/
230
231static bool usb_mouse_process_report(usb_hid_dev_t *hid_dev,
232 usb_mouse_t *mouse_dev, uint8_t *buffer,
233 size_t buffer_size)
234{
235 assert(mouse_dev != NULL);
236
237 usb_log_debug2("got buffer: %s.\n",
238 usb_debug_str_buffer(buffer, buffer_size, 0));
239
240 if (mouse_dev->mouse_phone < 0) {
241 usb_log_warning(NAME " No console phone.\n");
242 return true;
243 }
244
245 /*
246 * parse the input report
247 */
248
249 usb_log_debug(NAME " Calling usb_hid_parse_report() with "
250 "buffer %s\n", usb_debug_str_buffer(buffer, buffer_size, 0));
251
252 uint8_t report_id;
253
254 int rc = usb_hid_parse_report(hid_dev->report, buffer, buffer_size,
255 &report_id);
256
257 if (rc != EOK) {
258 usb_log_warning(NAME "Error in usb_hid_parse_report(): %s\n",
259 str_error(rc));
260 return true;
261 }
262
263 /*
264 * X
265 */
266 int shift_x = 0;
267
268 usb_hid_report_path_t *path = usb_hid_report_path();
269 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_GENERIC_DESKTOP,
270 USB_HIDUT_USAGE_GENERIC_DESKTOP_X);
271
272 usb_hid_report_path_set_report_id(path, report_id);
273
274 usb_hid_report_field_t *field = usb_hid_report_get_sibling(
275 hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END,
276 USB_HID_REPORT_TYPE_INPUT);
277
278 if (field != NULL) {
279 usb_log_debug(NAME " VALUE(%X) USAGE(%X)\n", field->value,
280 field->usage);
281 shift_x = field->value;
282 }
283
284 usb_hid_report_path_free(path);
285
286 /*
287 * Y
288 */
289 int shift_y = 0;
290
291 path = usb_hid_report_path();
292 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_GENERIC_DESKTOP,
293 USB_HIDUT_USAGE_GENERIC_DESKTOP_Y);
294
295 usb_hid_report_path_set_report_id(path, report_id);
296
297 field = usb_hid_report_get_sibling(
298 hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END,
299 USB_HID_REPORT_TYPE_INPUT);
300
301 if (field != NULL) {
302 usb_log_debug(NAME " VALUE(%X) USAGE(%X)\n", field->value,
303 field->usage);
304 shift_y = field->value;
305 }
306
307 usb_hid_report_path_free(path);
308
309 if ((shift_x != 0) || (shift_y != 0)) {
310 async_obsolete_req_2_0(mouse_dev->mouse_phone,
311 MEVENT_MOVE, shift_x, shift_y);
312 }
313
314 /*
315 * Wheel
316 */
317 int wheel = 0;
318
319 path = usb_hid_report_path();
320 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_GENERIC_DESKTOP,
321 USB_HIDUT_USAGE_GENERIC_DESKTOP_WHEEL);
322
323 usb_hid_report_path_set_report_id(path, report_id);
324
325 field = usb_hid_report_get_sibling(
326 hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END,
327 USB_HID_REPORT_TYPE_INPUT);
328
329 if (field != NULL) {
330 usb_log_debug(NAME " VALUE(%X) USAGE(%X)\n", field->value,
331 field->usage);
332 wheel = field->value;
333 }
334
335 usb_hid_report_path_free(path);
336
337 // send arrow up for positive direction and arrow down for negative
338 // direction; three arrows for difference of 1
339 usb_mouse_send_wheel(mouse_dev, wheel);
340
341
342 /*
343 * Buttons
344 */
345 path = usb_hid_report_path();
346 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_BUTTON, 0);
347 usb_hid_report_path_set_report_id(path, report_id);
348
349 field = usb_hid_report_get_sibling(
350 hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END
351 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
352 USB_HID_REPORT_TYPE_INPUT);
353
354 while (field != NULL) {
355 usb_log_debug(NAME " VALUE(%X) USAGE(%X)\n", field->value,
356 field->usage);
357
358 if (mouse_dev->buttons[field->usage - field->usage_minimum] == 0
359 && field->value != 0) {
360 async_obsolete_req_2_0(mouse_dev->mouse_phone,
361 MEVENT_BUTTON, field->usage, 1);
362 mouse_dev->buttons[field->usage - field->usage_minimum]
363 = field->value;
364 } else if (
365 mouse_dev->buttons[field->usage - field->usage_minimum] != 0
366 && field->value == 0) {
367 async_obsolete_req_2_0(mouse_dev->mouse_phone,
368 MEVENT_BUTTON, field->usage, 0);
369 mouse_dev->buttons[field->usage - field->usage_minimum]
370 = field->value;
371 }
372
373 field = usb_hid_report_get_sibling(
374 hid_dev->report, field, path, USB_HID_PATH_COMPARE_END
375 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
376 USB_HID_REPORT_TYPE_INPUT);
377 }
378
379 usb_hid_report_path_free(path);
380
381 return true;
382}
383
384/*----------------------------------------------------------------------------*/
385
386static int usb_mouse_create_function(usb_hid_dev_t *hid_dev, usb_mouse_t *mouse)
387{
388 assert(hid_dev != NULL);
389 assert(mouse != NULL);
390
391 /* Create the function exposed under /dev/devices. */
392 usb_log_debug("Creating DDF function %s...\n", HID_MOUSE_FUN_NAME);
393 ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed,
394 HID_MOUSE_FUN_NAME);
395 if (fun == NULL) {
396 usb_log_error("Could not create DDF function node.\n");
397 return ENOMEM;
398 }
399
400 fun->ops = &mouse->ops;
401 fun->driver_data = mouse;
402
403 int rc = ddf_fun_bind(fun);
404 if (rc != EOK) {
405 usb_log_error("Could not bind DDF function: %s.\n",
406 str_error(rc));
407 ddf_fun_destroy(fun);
408 return rc;
409 }
410
411 usb_log_debug("Adding DDF function to class %s...\n",
412 HID_MOUSE_CLASS_NAME);
413 rc = ddf_fun_add_to_class(fun, HID_MOUSE_CLASS_NAME);
414 if (rc != EOK) {
415 usb_log_error(
416 "Could not add DDF function to class %s: %s.\n",
417 HID_MOUSE_CLASS_NAME, str_error(rc));
418 ddf_fun_destroy(fun);
419 return rc;
420 }
421
422 /*
423 * Special function for acting as keyboard (wheel)
424 */
425 usb_log_debug("Creating DDF function %s...\n",
426 HID_MOUSE_WHEEL_FUN_NAME);
427 fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed,
428 HID_MOUSE_WHEEL_FUN_NAME);
429 if (fun == NULL) {
430 usb_log_error("Could not create DDF function node.\n");
431 return ENOMEM;
432 }
433
434 /*
435 * Store the initialized HID device and HID ops
436 * to the DDF function.
437 */
438 fun->ops = &mouse->ops;
439 fun->driver_data = mouse;
440
441 rc = ddf_fun_bind(fun);
442 if (rc != EOK) {
443 usb_log_error("Could not bind DDF function: %s.\n",
444 str_error(rc));
445 ddf_fun_destroy(fun);
446 return rc;
447 }
448
449 usb_log_debug("Adding DDF function to class %s...\n",
450 HID_MOUSE_WHEEL_CLASS_NAME);
451 rc = ddf_fun_add_to_class(fun, HID_MOUSE_WHEEL_CLASS_NAME);
452 if (rc != EOK) {
453 usb_log_error(
454 "Could not add DDF function to class %s: %s.\n",
455 HID_MOUSE_WHEEL_CLASS_NAME, str_error(rc));
456 ddf_fun_destroy(fun);
457 return rc;
458 }
459
460 return EOK;
461}
462
463/*----------------------------------------------------------------------------*/
464
465int usb_mouse_init(usb_hid_dev_t *hid_dev, void **data)
466{
467 usb_log_debug("Initializing HID/Mouse structure...\n");
468
469 if (hid_dev == NULL) {
470 usb_log_error("Failed to init keyboard structure: no structure"
471 " given.\n");
472 return EINVAL;
473 }
474
475 usb_mouse_t *mouse_dev = usb_mouse_new();
476 if (mouse_dev == NULL) {
477 usb_log_error("Error while creating USB/HID Mouse device "
478 "structure.\n");
479 return ENOMEM;
480 }
481
482 mouse_dev->buttons = (int32_t *)calloc(USB_MOUSE_BUTTON_COUNT,
483 sizeof(int32_t));
484
485 if (mouse_dev->buttons == NULL) {
486 usb_log_fatal("No memory!\n");
487 free(mouse_dev);
488 return ENOMEM;
489 }
490
491 // save the Mouse device structure into the HID device structure
492 *data = mouse_dev;
493
494 // set handler for incoming calls
495 mouse_dev->ops.default_handler = default_connection_handler;
496
497 // TODO: how to know if the device supports the request???
498 usbhid_req_set_idle(&hid_dev->usb_dev->ctrl_pipe,
499 hid_dev->usb_dev->interface_no, IDLE_RATE);
500
501 int rc = usb_mouse_create_function(hid_dev, mouse_dev);
502 if (rc != EOK) {
503 usb_mouse_free(&mouse_dev);
504 return rc;
505 }
506
507 return EOK;
508}
509
510/*----------------------------------------------------------------------------*/
511
512bool usb_mouse_polling_callback(usb_hid_dev_t *hid_dev, void *data,
513 uint8_t *buffer, size_t buffer_size)
514{
515 usb_log_debug("usb_mouse_polling_callback()\n");
516 usb_debug_str_buffer(buffer, buffer_size, 0);
517
518 if (hid_dev == NULL || data == NULL) {
519 usb_log_error("Missing argument to the mouse polling callback."
520 "\n");
521 return false;
522 }
523
524 usb_mouse_t *mouse_dev = (usb_mouse_t *)data;
525
526 return usb_mouse_process_report(hid_dev, mouse_dev, buffer,
527 buffer_size);
528}
529
530/*----------------------------------------------------------------------------*/
531
532void usb_mouse_deinit(usb_hid_dev_t *hid_dev, void *data)
533{
534 if (data != NULL) {
535 usb_mouse_free((usb_mouse_t **)&data);
536 }
537}
538
539/*----------------------------------------------------------------------------*/
540
541int usb_mouse_set_boot_protocol(usb_hid_dev_t *hid_dev)
542{
543 int rc = usb_hid_parse_report_descriptor(hid_dev->report,
544 USB_MOUSE_BOOT_REPORT_DESCRIPTOR,
545 USB_MOUSE_BOOT_REPORT_DESCRIPTOR_SIZE);
546
547 if (rc != EOK) {
548 usb_log_error("Failed to parse boot report descriptor: %s\n",
549 str_error(rc));
550 return rc;
551 }
552
553 rc = usbhid_req_set_protocol(&hid_dev->usb_dev->ctrl_pipe,
554 hid_dev->usb_dev->interface_no, USB_HID_PROTOCOL_BOOT);
555
556 if (rc != EOK) {
557 usb_log_warning("Failed to set boot protocol to the device: "
558 "%s\n", str_error(rc));
559 return rc;
560 }
561
562 return EOK;
563}
564
565/**
566 * @}
567 */
Note: See TracBrowser for help on using the repository browser.