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

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

Small fixes

Bad printf directive (again) and uninitialized variable.

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