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

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

Subdriver-specific data moved to the subdriver structure.

+ DDF ops removed from hid_dev_t.

Should fix #225

  • Property mode set to 100644
File size: 15.7 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/classes/hid.h>
40#include <usb/classes/hidreq.h>
41#include <usb/classes/hidut.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 int *phone = (str_cmp(fun->name, HID_MOUSE_FUN_NAME) == 0)
134 ? &mouse_dev->mouse_phone : &mouse_dev->wheel_phone;
135
136 if (method == IPC_M_CONNECT_TO_ME) {
137 int callback = IPC_GET_ARG5(*icall);
138
139 if (*phone != -1) {
140 usb_log_debug("default_connection_handler: Console "
141 "phone to mouse already set.\n");
142 async_answer_0(icallid, ELIMIT);
143 return;
144 }
145
146 *phone = callback;
147 usb_log_debug("Console phone to mouse set ok (%d).\n", callback);
148 async_answer_0(icallid, EOK);
149 return;
150 }
151
152 usb_log_debug("default_connection_handler: Invalid function.\n");
153 async_answer_0(icallid, EINVAL);
154}
155
156/*----------------------------------------------------------------------------*/
157
158static usb_mouse_t *usb_mouse_new(void)
159{
160 usb_mouse_t *mouse = calloc(1, sizeof(usb_mouse_t));
161 if (mouse == NULL) {
162 return NULL;
163 }
164 mouse->mouse_phone = -1;
165 mouse->wheel_phone = -1;
166
167 return mouse;
168}
169
170/*----------------------------------------------------------------------------*/
171
172static void usb_mouse_free(usb_mouse_t **mouse_dev)
173{
174 assert(mouse_dev != NULL && *mouse_dev != NULL);
175
176 // hangup phone to the console
177 if ((*mouse_dev)->mouse_phone >= 0) {
178 async_hangup((*mouse_dev)->mouse_phone);
179 }
180
181 if ((*mouse_dev)->wheel_phone >= 0) {
182 async_hangup((*mouse_dev)->wheel_phone);
183 }
184
185 free(*mouse_dev);
186 *mouse_dev = NULL;
187}
188
189/*----------------------------------------------------------------------------*/
190
191static void usb_mouse_send_wheel(const usb_mouse_t *mouse_dev, int wheel)
192{
193 console_event_t ev;
194
195 ev.type = KEY_PRESS;
196 ev.key = (wheel > 0) ? KC_UP : (wheel < 0) ? KC_DOWN : 0;
197 ev.mods = 0;
198 ev.c = 0;
199
200 if (mouse_dev->wheel_phone < 0) {
201 usb_log_warning(
202 "Connection to console not ready, key discarded.\n");
203 return;
204 }
205
206 int count = (wheel < 0) ? -wheel : wheel;
207 int i;
208
209 for (i = 0; i < count * 3; ++i) {
210 usb_log_debug2("Sending key %d to the console\n", ev.key);
211 async_msg_4(mouse_dev->wheel_phone, KBD_EVENT, ev.type,
212 ev.key, ev.mods, ev.c);
213 // send key release right away
214 async_msg_4(mouse_dev->wheel_phone, KBD_EVENT, KEY_RELEASE,
215 ev.key, ev.mods, ev.c);
216 }
217}
218
219/*----------------------------------------------------------------------------*/
220
221static bool usb_mouse_process_report(usb_hid_dev_t *hid_dev,
222 usb_mouse_t *mouse_dev, uint8_t *buffer,
223 size_t buffer_size)
224{
225 assert(mouse_dev != NULL);
226
227 usb_log_debug2("got buffer: %s.\n",
228 usb_debug_str_buffer(buffer, buffer_size, 0));
229
230 if (mouse_dev->mouse_phone < 0) {
231 usb_log_error(NAME " No console phone.\n");
232 return false; // ??
233 }
234
235 /*
236 * parse the input report
237 */
238
239 usb_log_debug(NAME " Calling usb_hid_parse_report() with "
240 "buffer %s\n", usb_debug_str_buffer(buffer, buffer_size, 0));
241
242 uint8_t report_id;
243
244 int rc = usb_hid_parse_report(hid_dev->report, buffer, buffer_size,
245 &report_id);
246
247 if (rc != EOK) {
248 usb_log_warning(NAME "Error in usb_hid_parse_report(): %s\n",
249 str_error(rc));
250 return true;
251 }
252
253 /*
254 * X
255 */
256 int shift_x = 0;
257
258 usb_hid_report_path_t *path = usb_hid_report_path();
259 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_GENERIC_DESKTOP,
260 USB_HIDUT_USAGE_GENERIC_DESKTOP_X);
261
262 usb_hid_report_path_set_report_id(path, report_id);
263
264 usb_hid_report_field_t *field = usb_hid_report_get_sibling(
265 hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END,
266 USB_HID_REPORT_TYPE_INPUT);
267
268 if (field != NULL) {
269 usb_log_debug(NAME " VALUE(%X) USAGE(%X)\n", field->value,
270 field->usage);
271 shift_x = field->value;
272 }
273
274 usb_hid_report_path_free(path);
275
276 /*
277 * Y
278 */
279 int shift_y = 0;
280
281 path = usb_hid_report_path();
282 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_GENERIC_DESKTOP,
283 USB_HIDUT_USAGE_GENERIC_DESKTOP_Y);
284
285 usb_hid_report_path_set_report_id(path, report_id);
286
287 field = usb_hid_report_get_sibling(
288 hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END,
289 USB_HID_REPORT_TYPE_INPUT);
290
291 if (field != NULL) {
292 usb_log_debug(NAME " VALUE(%X) USAGE(%X)\n", field->value,
293 field->usage);
294 shift_y = field->value;
295 }
296
297 usb_hid_report_path_free(path);
298
299 if ((shift_x != 0) || (shift_y != 0)) {
300 async_req_2_0(mouse_dev->mouse_phone,
301 MEVENT_MOVE, shift_x, shift_y);
302 }
303
304 /*
305 * Wheel
306 */
307 int wheel = 0;
308
309 path = usb_hid_report_path();
310 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_GENERIC_DESKTOP,
311 USB_HIDUT_USAGE_GENERIC_DESKTOP_WHEEL);
312
313 usb_hid_report_path_set_report_id(path, report_id);
314
315 field = usb_hid_report_get_sibling(
316 hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END,
317 USB_HID_REPORT_TYPE_INPUT);
318
319 if (field != NULL) {
320 usb_log_debug(NAME " VALUE(%X) USAGE(%X)\n", field->value,
321 field->usage);
322 wheel = field->value;
323 }
324
325 usb_hid_report_path_free(path);
326
327 // send arrow up for positive direction and arrow down for negative
328 // direction; three arrows for difference of 1
329 usb_mouse_send_wheel(mouse_dev, wheel);
330
331
332 /*
333 * Buttons
334 */
335 path = usb_hid_report_path();
336 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_BUTTON, 0);
337 usb_hid_report_path_set_report_id(path, report_id);
338
339 field = usb_hid_report_get_sibling(
340 hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END
341 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
342 USB_HID_REPORT_TYPE_INPUT);
343
344 while (field != NULL) {
345 usb_log_debug(NAME " VALUE(%X) USAGE(%X)\n", field->value,
346 field->usage);
347
348 if (mouse_dev->buttons[field->usage - field->usage_minimum] == 0
349 && field->value != 0) {
350 async_req_2_0(mouse_dev->mouse_phone,
351 MEVENT_BUTTON, field->usage, 1);
352 mouse_dev->buttons[field->usage - field->usage_minimum]
353 = field->value;
354 } else if (
355 mouse_dev->buttons[field->usage - field->usage_minimum] != 0
356 && field->value == 0) {
357 async_req_2_0(mouse_dev->mouse_phone,
358 MEVENT_BUTTON, field->usage, 0);
359 mouse_dev->buttons[field->usage - field->usage_minimum]
360 = field->value;
361 }
362
363 field = usb_hid_report_get_sibling(
364 hid_dev->report, field, path, USB_HID_PATH_COMPARE_END
365 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
366 USB_HID_REPORT_TYPE_INPUT);
367 }
368
369 usb_hid_report_path_free(path);
370
371 return true;
372}
373
374/*----------------------------------------------------------------------------*/
375
376static int usb_mouse_create_function(usb_hid_dev_t *hid_dev, usb_mouse_t *mouse)
377{
378 assert(hid_dev != NULL);
379 assert(mouse != NULL);
380
381 /* Create the function exposed under /dev/devices. */
382 usb_log_debug("Creating DDF function %s...\n", HID_MOUSE_FUN_NAME);
383 ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed,
384 HID_MOUSE_FUN_NAME);
385 if (fun == NULL) {
386 usb_log_error("Could not create DDF function node.\n");
387 return ENOMEM;
388 }
389
390 fun->ops = &mouse->ops;
391 fun->driver_data = mouse; // TODO: maybe change to hid_dev->data
392
393 int rc = ddf_fun_bind(fun);
394 if (rc != EOK) {
395 usb_log_error("Could not bind DDF function: %s.\n",
396 str_error(rc));
397 ddf_fun_destroy(fun);
398 return rc;
399 }
400
401 usb_log_debug("Adding DDF function to class %s...\n",
402 HID_MOUSE_CLASS_NAME);
403 rc = ddf_fun_add_to_class(fun, HID_MOUSE_CLASS_NAME);
404 if (rc != EOK) {
405 usb_log_error(
406 "Could not add DDF function to class %s: %s.\n",
407 HID_MOUSE_CLASS_NAME, str_error(rc));
408 ddf_fun_destroy(fun);
409 return rc;
410 }
411
412 /*
413 * Special function for acting as keyboard (wheel)
414 */
415 usb_log_debug("Creating DDF function %s...\n",
416 HID_MOUSE_WHEEL_FUN_NAME);
417 fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed,
418 HID_MOUSE_WHEEL_FUN_NAME);
419 if (fun == NULL) {
420 usb_log_error("Could not create DDF function node.\n");
421 return ENOMEM;
422 }
423
424 /*
425 * Store the initialized HID device and HID ops
426 * to the DDF function.
427 */
428 fun->ops = &mouse->ops;
429 fun->driver_data = hid_dev; // TODO: maybe change to hid_dev->data
430
431 rc = ddf_fun_bind(fun);
432 if (rc != EOK) {
433 usb_log_error("Could not bind DDF function: %s.\n",
434 str_error(rc));
435 ddf_fun_destroy(fun);
436 return rc;
437 }
438
439 usb_log_debug("Adding DDF function to class %s...\n",
440 HID_MOUSE_WHEEL_CLASS_NAME);
441 rc = ddf_fun_add_to_class(fun, HID_MOUSE_WHEEL_CLASS_NAME);
442 if (rc != EOK) {
443 usb_log_error(
444 "Could not add DDF function to class %s: %s.\n",
445 HID_MOUSE_WHEEL_CLASS_NAME, str_error(rc));
446 ddf_fun_destroy(fun);
447 return rc;
448 }
449
450 return EOK;
451}
452
453/*----------------------------------------------------------------------------*/
454
455int usb_mouse_init(usb_hid_dev_t *hid_dev, void **data)
456{
457 usb_log_debug("Initializing HID/Mouse structure...\n");
458
459 if (hid_dev == NULL) {
460 usb_log_error("Failed to init keyboard structure: no structure"
461 " given.\n");
462 return EINVAL;
463 }
464
465 usb_mouse_t *mouse_dev = usb_mouse_new();
466 if (mouse_dev == NULL) {
467 usb_log_error("Error while creating USB/HID Mouse device "
468 "structure.\n");
469 return ENOMEM;
470 }
471
472 mouse_dev->buttons = (int32_t *)calloc(USB_MOUSE_BUTTON_COUNT,
473 sizeof(int32_t));
474
475 if (mouse_dev->buttons == NULL) {
476 usb_log_fatal("No memory!\n");
477 free(mouse_dev);
478 return ENOMEM;
479 }
480
481 // save the Mouse device structure into the HID device structure
482 *data = mouse_dev;
483
484 // set handler for incoming calls
485 // TODO: must be one for each subdriver!!
486 mouse_dev->ops.default_handler = default_connection_handler;
487
488 // TODO: how to know if the device supports the request???
489// usbhid_req_set_idle(&hid_dev->usb_dev->ctrl_pipe,
490// hid_dev->usb_dev->interface_no, IDLE_RATE);
491
492 int rc = usb_mouse_create_function(hid_dev, mouse_dev);
493 if (rc != EOK) {
494 usb_mouse_free(&mouse_dev);
495 return rc;
496 }
497
498 return EOK;
499}
500
501/*----------------------------------------------------------------------------*/
502
503bool usb_mouse_polling_callback(usb_hid_dev_t *hid_dev, void *data,
504 uint8_t *buffer, size_t buffer_size)
505{
506 usb_log_debug("usb_mouse_polling_callback()\n");
507 usb_debug_str_buffer(buffer, buffer_size, 0);
508
509 if (hid_dev == NULL || data == NULL) {
510 usb_log_error("Missing argument to the mouse polling callback."
511 "\n");
512 return false;
513 }
514
515 usb_mouse_t *mouse_dev = (usb_mouse_t *)data;
516
517 return usb_mouse_process_report(hid_dev, mouse_dev, buffer,
518 buffer_size);
519}
520
521/*----------------------------------------------------------------------------*/
522
523void usb_mouse_deinit(usb_hid_dev_t *hid_dev, void *data)
524{
525 if (data != NULL) {
526 usb_mouse_free((usb_mouse_t **)&data);
527 }
528}
529
530/*----------------------------------------------------------------------------*/
531
532int usb_mouse_set_boot_protocol(usb_hid_dev_t *hid_dev)
533{
534 int rc = usb_hid_parse_report_descriptor(hid_dev->report,
535 USB_MOUSE_BOOT_REPORT_DESCRIPTOR,
536 USB_MOUSE_BOOT_REPORT_DESCRIPTOR_SIZE);
537
538 if (rc != EOK) {
539 usb_log_error("Failed to parse boot report descriptor: %s\n",
540 str_error(rc));
541 return rc;
542 }
543
544 rc = usbhid_req_set_protocol(&hid_dev->usb_dev->ctrl_pipe,
545 hid_dev->usb_dev->interface_no, USB_HID_PROTOCOL_BOOT);
546
547 if (rc != EOK) {
548 usb_log_warning("Failed to set boot protocol to the device: "
549 "%s\n", str_error(rc));
550 return rc;
551 }
552
553 return EOK;
554}
555
556/**
557 * @}
558 */
Note: See TracBrowser for help on using the repository browser.