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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 61f49a0 was 61f49a0, checked in by Lubos Slovak <lubos.slovak@…>, 14 years ago

Smaller interval in mkbd, fix in mouse subdriver

  • 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 <str_error.h>
44#include <ipc/mouse.h>
45#include <io/console.h>
46
47#include <ipc/kbd.h>
48#include <io/keycode.h>
49
50#include "mousedev.h"
51#include "../usbhid.h"
52
53#define NAME "mouse"
54
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";
67const char *HID_MOUSE_WHEEL_FUN_NAME = "mouse-wheel";
68const char *HID_MOUSE_CLASS_NAME = "mouse";
69const char *HID_MOUSE_WHEEL_CLASS_NAME = "keyboard";
70
71/** Default idle rate for mouses. */
72static const uint8_t IDLE_RATE = 0;
73static const size_t USB_MOUSE_BUTTON_COUNT = 3;
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_mouse_t *mouse_dev = (usb_mouse_t *)fun->driver_data;
125
126 if (mouse_dev == NULL) {
127 usb_log_debug("default_connection_handler: Missing "
128 "parameters.\n");
129 async_answer_0(icallid, EINVAL);
130 return;
131 }
132
133 usb_log_debug("default_connection_handler: fun->name: %s\n",
134 fun->name);
135 usb_log_debug("default_connection_handler: mouse_phone: %d, wheel "
136 "phone: %d\n", mouse_dev->mouse_phone, mouse_dev->wheel_phone);
137
138 int *phone = (str_cmp(fun->name, HID_MOUSE_FUN_NAME) == 0)
139 ? &mouse_dev->mouse_phone : &mouse_dev->wheel_phone;
140
141 if (method == IPC_M_CONNECT_TO_ME) {
142 int callback = IPC_GET_ARG5(*icall);
143
144 if (*phone != -1) {
145 usb_log_debug("default_connection_handler: Console "
146 "phone to mouse already set.\n");
147 async_answer_0(icallid, ELIMIT);
148 return;
149 }
150
151 *phone = callback;
152 usb_log_debug("Console phone to mouse set ok (%d).\n", *phone);
153 async_answer_0(icallid, EOK);
154 return;
155 }
156
157 usb_log_debug("default_connection_handler: Invalid function.\n");
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 }
169 mouse->mouse_phone = -1;
170 mouse->wheel_phone = -1;
171
172 return mouse;
173}
174
175/*----------------------------------------------------------------------------*/
176
177static void usb_mouse_free(usb_mouse_t **mouse_dev)
178{
179 assert(mouse_dev != NULL && *mouse_dev != NULL);
180
181 // hangup phone to the console
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);
188 }
189
190 free(*mouse_dev);
191 *mouse_dev = NULL;
192}
193
194/*----------------------------------------------------------------------------*/
195
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,
227 usb_mouse_t *mouse_dev, uint8_t *buffer,
228 size_t buffer_size)
229{
230 assert(mouse_dev != NULL);
231
232 usb_log_debug2("got buffer: %s.\n",
233 usb_debug_str_buffer(buffer, buffer_size, 0));
234
235 if (mouse_dev->mouse_phone < 0) {
236 usb_log_warning(NAME " No console phone.\n");
237 return true;
238 }
239
240 /*
241 * parse the input report
242 */
243
244 usb_log_debug(NAME " Calling usb_hid_parse_report() with "
245 "buffer %s\n", usb_debug_str_buffer(buffer, buffer_size, 0));
246
247 uint8_t report_id;
248
249 int rc = usb_hid_parse_report(hid_dev->report, buffer, buffer_size,
250 &report_id);
251
252 if (rc != EOK) {
253 usb_log_warning(NAME "Error in usb_hid_parse_report(): %s\n",
254 str_error(rc));
255 return true;
256 }
257
258 /*
259 * X
260 */
261 int shift_x = 0;
262
263 usb_hid_report_path_t *path = usb_hid_report_path();
264 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_GENERIC_DESKTOP,
265 USB_HIDUT_USAGE_GENERIC_DESKTOP_X);
266
267 usb_hid_report_path_set_report_id(path, report_id);
268
269 usb_hid_report_field_t *field = usb_hid_report_get_sibling(
270 hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END,
271 USB_HID_REPORT_TYPE_INPUT);
272
273 if (field != NULL) {
274 usb_log_debug(NAME " VALUE(%X) USAGE(%X)\n", field->value,
275 field->usage);
276 shift_x = field->value;
277 }
278
279 usb_hid_report_path_free(path);
280
281 /*
282 * Y
283 */
284 int shift_y = 0;
285
286 path = usb_hid_report_path();
287 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_GENERIC_DESKTOP,
288 USB_HIDUT_USAGE_GENERIC_DESKTOP_Y);
289
290 usb_hid_report_path_set_report_id(path, report_id);
291
292 field = usb_hid_report_get_sibling(
293 hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END,
294 USB_HID_REPORT_TYPE_INPUT);
295
296 if (field != NULL) {
297 usb_log_debug(NAME " VALUE(%X) USAGE(%X)\n", field->value,
298 field->usage);
299 shift_y = field->value;
300 }
301
302 usb_hid_report_path_free(path);
303
304 if ((shift_x != 0) || (shift_y != 0)) {
305 async_req_2_0(mouse_dev->mouse_phone,
306 MEVENT_MOVE, shift_x, shift_y);
307 }
308
309 /*
310 * Wheel
311 */
312 int wheel = 0;
313
314 path = usb_hid_report_path();
315 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_GENERIC_DESKTOP,
316 USB_HIDUT_USAGE_GENERIC_DESKTOP_WHEEL);
317
318 usb_hid_report_path_set_report_id(path, report_id);
319
320 field = usb_hid_report_get_sibling(
321 hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END,
322 USB_HID_REPORT_TYPE_INPUT);
323
324 if (field != NULL) {
325 usb_log_debug(NAME " VALUE(%X) USAGE(%X)\n", field->value,
326 field->usage);
327 wheel = field->value;
328 }
329
330 usb_hid_report_path_free(path);
331
332 // send arrow up for positive direction and arrow down for negative
333 // direction; three arrows for difference of 1
334 usb_mouse_send_wheel(mouse_dev, wheel);
335
336
337 /*
338 * Buttons
339 */
340 path = usb_hid_report_path();
341 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_BUTTON, 0);
342 usb_hid_report_path_set_report_id(path, report_id);
343
344 field = usb_hid_report_get_sibling(
345 hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END
346 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
347 USB_HID_REPORT_TYPE_INPUT);
348
349 while (field != NULL) {
350 usb_log_debug(NAME " VALUE(%X) USAGE(%X)\n", field->value,
351 field->usage);
352
353 if (mouse_dev->buttons[field->usage - field->usage_minimum] == 0
354 && field->value != 0) {
355 async_req_2_0(mouse_dev->mouse_phone,
356 MEVENT_BUTTON, field->usage, 1);
357 mouse_dev->buttons[field->usage - field->usage_minimum]
358 = field->value;
359 } else if (
360 mouse_dev->buttons[field->usage - field->usage_minimum] != 0
361 && field->value == 0) {
362 async_req_2_0(mouse_dev->mouse_phone,
363 MEVENT_BUTTON, field->usage, 0);
364 mouse_dev->buttons[field->usage - field->usage_minimum]
365 = field->value;
366 }
367
368 field = usb_hid_report_get_sibling(
369 hid_dev->report, field, path, USB_HID_PATH_COMPARE_END
370 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
371 USB_HID_REPORT_TYPE_INPUT);
372 }
373
374 usb_hid_report_path_free(path);
375
376 return true;
377}
378
379/*----------------------------------------------------------------------------*/
380
381static int usb_mouse_create_function(usb_hid_dev_t *hid_dev, usb_mouse_t *mouse)
382{
383 assert(hid_dev != NULL);
384 assert(mouse != NULL);
385
386 /* Create the function exposed under /dev/devices. */
387 usb_log_debug("Creating DDF function %s...\n", HID_MOUSE_FUN_NAME);
388 ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed,
389 HID_MOUSE_FUN_NAME);
390 if (fun == NULL) {
391 usb_log_error("Could not create DDF function node.\n");
392 return ENOMEM;
393 }
394
395 fun->ops = &mouse->ops;
396 fun->driver_data = mouse; // 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
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 = &mouse->ops;
434 fun->driver_data = mouse; // 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
455 return EOK;
456}
457
458/*----------------------------------------------------------------------------*/
459
460int usb_mouse_init(usb_hid_dev_t *hid_dev, void **data)
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
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
486 // save the Mouse device structure into the HID device structure
487 *data = mouse_dev;
488
489 // set handler for incoming calls
490 // TODO: must be one for each subdriver!!
491 mouse_dev->ops.default_handler = default_connection_handler;
492
493 // TODO: how to know if the device supports the request???
494// usbhid_req_set_idle(&hid_dev->usb_dev->ctrl_pipe,
495// hid_dev->usb_dev->interface_no, IDLE_RATE);
496
497 int rc = usb_mouse_create_function(hid_dev, mouse_dev);
498 if (rc != EOK) {
499 usb_mouse_free(&mouse_dev);
500 return rc;
501 }
502
503 return EOK;
504}
505
506/*----------------------------------------------------------------------------*/
507
508bool usb_mouse_polling_callback(usb_hid_dev_t *hid_dev, void *data,
509 uint8_t *buffer, size_t buffer_size)
510{
511 usb_log_debug("usb_mouse_polling_callback()\n");
512 usb_debug_str_buffer(buffer, buffer_size, 0);
513
514 if (hid_dev == NULL || data == NULL) {
515 usb_log_error("Missing argument to the mouse polling callback."
516 "\n");
517 return false;
518 }
519
520 usb_mouse_t *mouse_dev = (usb_mouse_t *)data;
521
522 return usb_mouse_process_report(hid_dev, mouse_dev, buffer,
523 buffer_size);
524}
525
526/*----------------------------------------------------------------------------*/
527
528void usb_mouse_deinit(usb_hid_dev_t *hid_dev, void *data)
529{
530 if (data != NULL) {
531 usb_mouse_free((usb_mouse_t **)&data);
532 }
533}
534
535/*----------------------------------------------------------------------------*/
536
537int usb_mouse_set_boot_protocol(usb_hid_dev_t *hid_dev)
538{
539 int rc = usb_hid_parse_report_descriptor(hid_dev->report,
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.