[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>
|
---|
[faa44e58] | 39 | #include <usb/hid/hid.h>
|
---|
| 40 | #include <usb/hid/request.h>
|
---|
| 41 | #include <usb/hid/usages/core.h>
|
---|
[e9f0348] | 42 | #include <errno.h>
|
---|
[79ae36dd] | 43 | #include <async.h>
|
---|
[e9f0348] | 44 | #include <str_error.h>
|
---|
[1875a0c] | 45 | #include <ipc/mouseev.h>
|
---|
[f8e549b] | 46 | #include <io/console.h>
|
---|
| 47 |
|
---|
[5f88293] | 48 | #include <ipc/kbdev.h>
|
---|
[f8e549b] | 49 | #include <io/keycode.h>
|
---|
[e9f0348] | 50 |
|
---|
| 51 | #include "mousedev.h"
|
---|
| 52 | #include "../usbhid.h"
|
---|
| 53 |
|
---|
[a3a2fdb] | 54 | /** Number of simulated arrow-key presses for singel wheel step. */
|
---|
| 55 | #define ARROWS_PER_SINGLE_WHEEL 3
|
---|
| 56 |
|
---|
[5da7199] | 57 | #define NAME "mouse"
|
---|
[30710035] | 58 |
|
---|
[e9f0348] | 59 | /*----------------------------------------------------------------------------*/
|
---|
| 60 |
|
---|
[b803845] | 61 | const usb_endpoint_description_t usb_hid_mouse_poll_endpoint_description = {
|
---|
[e9f0348] | 62 | .transfer_type = USB_TRANSFER_INTERRUPT,
|
---|
| 63 | .direction = USB_DIRECTION_IN,
|
---|
| 64 | .interface_class = USB_CLASS_HID,
|
---|
| 65 | .interface_subclass = USB_HID_SUBCLASS_BOOT,
|
---|
| 66 | .interface_protocol = USB_HID_PROTOCOL_MOUSE,
|
---|
| 67 | .flags = 0
|
---|
| 68 | };
|
---|
| 69 |
|
---|
| 70 | const char *HID_MOUSE_FUN_NAME = "mouse";
|
---|
[f8e549b] | 71 | const char *HID_MOUSE_WHEEL_FUN_NAME = "mouse-wheel";
|
---|
[1dc4a5e] | 72 | const char *HID_MOUSE_CATEGORY = "mouse";
|
---|
| 73 | const char *HID_MOUSE_WHEEL_CATEGORY = "keyboard";
|
---|
[e9f0348] | 74 |
|
---|
| 75 | /** Default idle rate for mouses. */
|
---|
| 76 | static const uint8_t IDLE_RATE = 0;
|
---|
| 77 |
|
---|
| 78 | /*----------------------------------------------------------------------------*/
|
---|
| 79 |
|
---|
| 80 | enum {
|
---|
| 81 | USB_MOUSE_BOOT_REPORT_DESCRIPTOR_SIZE = 63
|
---|
| 82 | };
|
---|
| 83 |
|
---|
| 84 | static const uint8_t USB_MOUSE_BOOT_REPORT_DESCRIPTOR[
|
---|
| 85 | USB_MOUSE_BOOT_REPORT_DESCRIPTOR_SIZE] = {
|
---|
| 86 | 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
|
---|
| 87 | 0x09, 0x02, // USAGE (Mouse)
|
---|
| 88 | 0xa1, 0x01, // COLLECTION (Application)
|
---|
| 89 | 0x09, 0x01, // USAGE (Pointer)
|
---|
| 90 | 0xa1, 0x00, // COLLECTION (Physical)
|
---|
| 91 | 0x95, 0x03, // REPORT_COUNT (3)
|
---|
| 92 | 0x75, 0x01, // REPORT_SIZE (1)
|
---|
| 93 | 0x05, 0x09, // USAGE_PAGE (Button)
|
---|
| 94 | 0x19, 0x01, // USAGE_MINIMUM (Button 1)
|
---|
| 95 | 0x29, 0x03, // USAGE_MAXIMUM (Button 3)
|
---|
| 96 | 0x15, 0x00, // LOGICAL_MINIMUM (0)
|
---|
| 97 | 0x25, 0x01, // LOGICAL_MAXIMUM (1)
|
---|
| 98 | 0x81, 0x02, // INPUT (Data,Var,Abs)
|
---|
| 99 | 0x95, 0x01, // REPORT_COUNT (1)
|
---|
| 100 | 0x75, 0x05, // REPORT_SIZE (5)
|
---|
| 101 | 0x81, 0x01, // INPUT (Cnst)
|
---|
| 102 | 0x75, 0x08, // REPORT_SIZE (8)
|
---|
| 103 | 0x95, 0x02, // REPORT_COUNT (2)
|
---|
| 104 | 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
|
---|
| 105 | 0x09, 0x30, // USAGE (X)
|
---|
| 106 | 0x09, 0x31, // USAGE (Y)
|
---|
| 107 | 0x15, 0x81, // LOGICAL_MINIMUM (-127)
|
---|
| 108 | 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
|
---|
| 109 | 0x81, 0x06, // INPUT (Data,Var,Rel)
|
---|
| 110 | 0xc0, // END_COLLECTION
|
---|
| 111 | 0xc0 // END_COLLECTION
|
---|
| 112 | };
|
---|
| 113 |
|
---|
| 114 | /*----------------------------------------------------------------------------*/
|
---|
| 115 |
|
---|
| 116 | /** Default handler for IPC methods not handled by DDF.
|
---|
| 117 | *
|
---|
| 118 | * @param fun Device function handling the call.
|
---|
| 119 | * @param icallid Call id.
|
---|
| 120 | * @param icall Call data.
|
---|
| 121 | */
|
---|
| 122 | static void default_connection_handler(ddf_fun_t *fun,
|
---|
| 123 | ipc_callid_t icallid, ipc_call_t *icall)
|
---|
| 124 | {
|
---|
[5da7199] | 125 | usb_mouse_t *mouse_dev = (usb_mouse_t *) fun->driver_data;
|
---|
[cc29622] | 126 |
|
---|
[65b458c4] | 127 | if (mouse_dev == NULL) {
|
---|
[f8e549b] | 128 | usb_log_debug("default_connection_handler: Missing "
|
---|
| 129 | "parameters.\n");
|
---|
[e9f0348] | 130 | async_answer_0(icallid, EINVAL);
|
---|
| 131 | return;
|
---|
| 132 | }
|
---|
[cc29622] | 133 |
|
---|
[37f87fa] | 134 | usb_log_debug("default_connection_handler: fun->name: %s\n",
|
---|
| 135 | fun->name);
|
---|
[5da7199] | 136 | usb_log_debug("default_connection_handler: mouse_sess: %p, "
|
---|
| 137 | "wheel_sess: %p\n", mouse_dev->mouse_sess, mouse_dev->wheel_sess);
|
---|
[cc29622] | 138 |
|
---|
[5da7199] | 139 | async_sess_t **sess_ptr =
|
---|
| 140 | (str_cmp(fun->name, HID_MOUSE_FUN_NAME) == 0) ?
|
---|
| 141 | &mouse_dev->mouse_sess : &mouse_dev->wheel_sess;
|
---|
[cc29622] | 142 |
|
---|
[5da7199] | 143 | async_sess_t *sess =
|
---|
| 144 | async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
|
---|
| 145 | if (sess != NULL) {
|
---|
| 146 | if (*sess_ptr == NULL) {
|
---|
| 147 | *sess_ptr = sess;
|
---|
| 148 | usb_log_debug("Console session to mouse set ok (%p).\n",
|
---|
| 149 | sess);
|
---|
| 150 | async_answer_0(icallid, EOK);
|
---|
| 151 | } else {
|
---|
[f8e549b] | 152 | usb_log_debug("default_connection_handler: Console "
|
---|
[5da7199] | 153 | "session to mouse already set.\n");
|
---|
[e9f0348] | 154 | async_answer_0(icallid, ELIMIT);
|
---|
| 155 | }
|
---|
[5da7199] | 156 | } else {
|
---|
| 157 | usb_log_debug("default_connection_handler: Invalid function.\n");
|
---|
| 158 | async_answer_0(icallid, EINVAL);
|
---|
[e9f0348] | 159 | }
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | /*----------------------------------------------------------------------------*/
|
---|
| 163 |
|
---|
| 164 | static usb_mouse_t *usb_mouse_new(void)
|
---|
| 165 | {
|
---|
| 166 | usb_mouse_t *mouse = calloc(1, sizeof(usb_mouse_t));
|
---|
| 167 | if (mouse == NULL) {
|
---|
| 168 | return NULL;
|
---|
| 169 | }
|
---|
[5da7199] | 170 | mouse->mouse_sess = NULL;
|
---|
| 171 | mouse->wheel_sess = NULL;
|
---|
[cc29622] | 172 |
|
---|
[e9f0348] | 173 | return mouse;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | /*----------------------------------------------------------------------------*/
|
---|
| 177 |
|
---|
[5f6e25e] | 178 | static void usb_mouse_destroy(usb_mouse_t *mouse_dev)
|
---|
[e9f0348] | 179 | {
|
---|
[5f6e25e] | 180 | assert(mouse_dev != NULL);
|
---|
[cc29622] | 181 |
|
---|
[5da7199] | 182 | // hangup session to the console
|
---|
| 183 | if (mouse_dev->mouse_sess != NULL)
|
---|
| 184 | async_hangup(mouse_dev->mouse_sess);
|
---|
[cc29622] | 185 |
|
---|
[5da7199] | 186 | if (mouse_dev->wheel_sess != NULL)
|
---|
| 187 | async_hangup(mouse_dev->wheel_sess);
|
---|
[f2964e45] | 188 | int ret = ddf_fun_unbind(mouse_dev->mouse_fun);
|
---|
| 189 | if (ret != EOK) {
|
---|
| 190 | usb_log_error("Failed to unbind mouse function.\n");
|
---|
| 191 | } else {
|
---|
| 192 | ddf_fun_destroy(mouse_dev->mouse_fun);
|
---|
| 193 | /* Prevent double free */
|
---|
| 194 | mouse_dev->wheel_fun->driver_data = NULL;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | ret = ddf_fun_unbind(mouse_dev->wheel_fun);
|
---|
| 198 | if (ret != EOK) {
|
---|
| 199 | usb_log_error("Failed to unbind wheel function.\n");
|
---|
| 200 | } else {
|
---|
| 201 | ddf_fun_destroy(mouse_dev->wheel_fun);
|
---|
| 202 | }
|
---|
[e9f0348] | 203 | }
|
---|
| 204 |
|
---|
| 205 | /*----------------------------------------------------------------------------*/
|
---|
| 206 |
|
---|
[f8e549b] | 207 | static void usb_mouse_send_wheel(const usb_mouse_t *mouse_dev, int wheel)
|
---|
| 208 | {
|
---|
[a3a2fdb] | 209 | unsigned int key = (wheel > 0) ? KC_UP : KC_DOWN;
|
---|
[f8e549b] | 210 |
|
---|
[5da7199] | 211 | if (mouse_dev->wheel_sess == NULL) {
|
---|
[f8e549b] | 212 | usb_log_warning(
|
---|
[ae5f77d5] | 213 | "Connection to console not ready, wheel roll discarded.\n");
|
---|
[f8e549b] | 214 | return;
|
---|
| 215 | }
|
---|
[cc29622] | 216 |
|
---|
[a3a2fdb] | 217 | int count = ((wheel < 0) ? -wheel : wheel) * ARROWS_PER_SINGLE_WHEEL;
|
---|
[f8e549b] | 218 | int i;
|
---|
[cc29622] | 219 |
|
---|
[a3a2fdb] | 220 | for (i = 0; i < count; i++) {
|
---|
| 221 | /* Send arrow press and release. */
|
---|
| 222 | usb_log_debug2("Sending key %d to the console\n", key);
|
---|
[5da7199] | 223 |
|
---|
| 224 | async_exch_t *exch = async_exchange_begin(mouse_dev->wheel_sess);
|
---|
| 225 |
|
---|
| 226 | async_msg_4(exch, KBDEV_EVENT, KEY_PRESS, key, 0, 0);
|
---|
| 227 | async_msg_4(exch, KBDEV_EVENT, KEY_RELEASE, key, 0, 0);
|
---|
| 228 |
|
---|
| 229 | async_exchange_end(exch);
|
---|
[f8e549b] | 230 | }
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | /*----------------------------------------------------------------------------*/
|
---|
| 234 |
|
---|
[a3a2fdb] | 235 | static int get_mouse_axis_move_value(uint8_t rid, usb_hid_report_t *report,
|
---|
| 236 | int32_t usage)
|
---|
[e9f0348] | 237 | {
|
---|
[a3a2fdb] | 238 | int result = 0;
|
---|
[30710035] | 239 |
|
---|
| 240 | usb_hid_report_path_t *path = usb_hid_report_path();
|
---|
[a3a2fdb] | 241 | usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_GENERIC_DESKTOP,
|
---|
| 242 | usage);
|
---|
[e9f0348] | 243 |
|
---|
[a3a2fdb] | 244 | usb_hid_report_path_set_report_id(path, rid);
|
---|
[e9f0348] | 245 |
|
---|
[30710035] | 246 | usb_hid_report_field_t *field = usb_hid_report_get_sibling(
|
---|
[a3a2fdb] | 247 | report, NULL, path, USB_HID_PATH_COMPARE_END,
|
---|
[30710035] | 248 | USB_HID_REPORT_TYPE_INPUT);
|
---|
[e9f0348] | 249 |
|
---|
[30710035] | 250 | if (field != NULL) {
|
---|
[a3a2fdb] | 251 | result = field->value;
|
---|
[e9f0348] | 252 | }
|
---|
[30710035] | 253 |
|
---|
| 254 | usb_hid_report_path_free(path);
|
---|
| 255 |
|
---|
[a3a2fdb] | 256 | return result;
|
---|
| 257 | }
|
---|
[30710035] | 258 |
|
---|
[a3a2fdb] | 259 | static bool usb_mouse_process_report(usb_hid_dev_t *hid_dev,
|
---|
| 260 | usb_mouse_t *mouse_dev)
|
---|
| 261 | {
|
---|
| 262 | assert(mouse_dev != NULL);
|
---|
[cc29622] | 263 |
|
---|
[5da7199] | 264 | if (mouse_dev->mouse_sess == NULL) {
|
---|
| 265 | usb_log_warning(NAME " No console session.\n");
|
---|
[a3a2fdb] | 266 | return true;
|
---|
[e9f0348] | 267 | }
|
---|
[30710035] | 268 |
|
---|
[a3a2fdb] | 269 | int shift_x = get_mouse_axis_move_value(hid_dev->report_id,
|
---|
[a8c4e871] | 270 | &hid_dev->report, USB_HIDUT_USAGE_GENERIC_DESKTOP_X);
|
---|
[a3a2fdb] | 271 | int shift_y = get_mouse_axis_move_value(hid_dev->report_id,
|
---|
[a8c4e871] | 272 | &hid_dev->report, USB_HIDUT_USAGE_GENERIC_DESKTOP_Y);
|
---|
[a3a2fdb] | 273 | int wheel = get_mouse_axis_move_value(hid_dev->report_id,
|
---|
[a8c4e871] | 274 | &hid_dev->report, USB_HIDUT_USAGE_GENERIC_DESKTOP_WHEEL);
|
---|
[a3a2fdb] | 275 |
|
---|
[30710035] | 276 | if ((shift_x != 0) || (shift_y != 0)) {
|
---|
[a8c4e871] | 277 | async_exch_t *exch =
|
---|
| 278 | async_exchange_begin(mouse_dev->mouse_sess);
|
---|
[5da7199] | 279 | async_req_2_0(exch, MOUSEEV_MOVE_EVENT, shift_x, shift_y);
|
---|
| 280 | async_exchange_end(exch);
|
---|
[e9f0348] | 281 | }
|
---|
[cc29622] | 282 |
|
---|
[5da7199] | 283 | if (wheel != 0)
|
---|
[a3a2fdb] | 284 | usb_mouse_send_wheel(mouse_dev, wheel);
|
---|
[cc29622] | 285 |
|
---|
[30710035] | 286 | /*
|
---|
| 287 | * Buttons
|
---|
| 288 | */
|
---|
[a3a2fdb] | 289 | usb_hid_report_path_t *path = usb_hid_report_path();
|
---|
[30710035] | 290 | usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_BUTTON, 0);
|
---|
[65c3794] | 291 | usb_hid_report_path_set_report_id(path, hid_dev->report_id);
|
---|
[cc29622] | 292 |
|
---|
[a3a2fdb] | 293 | usb_hid_report_field_t *field = usb_hid_report_get_sibling(
|
---|
[a8c4e871] | 294 | &hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END
|
---|
| 295 | | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, USB_HID_REPORT_TYPE_INPUT);
|
---|
[30710035] | 296 |
|
---|
[310c4df] | 297 | while (field != NULL) {
|
---|
[ae5f77d5] | 298 | usb_log_debug2(NAME " VALUE(%X) USAGE(%X)\n", field->value,
|
---|
[30710035] | 299 | field->usage);
|
---|
| 300 |
|
---|
| 301 | if (mouse_dev->buttons[field->usage - field->usage_minimum] == 0
|
---|
| 302 | && field->value != 0) {
|
---|
[5da7199] | 303 | async_exch_t *exch =
|
---|
| 304 | async_exchange_begin(mouse_dev->mouse_sess);
|
---|
| 305 | async_req_2_0(exch, MOUSEEV_BUTTON_EVENT, field->usage, 1);
|
---|
| 306 | async_exchange_end(exch);
|
---|
| 307 |
|
---|
[30710035] | 308 | mouse_dev->buttons[field->usage - field->usage_minimum]
|
---|
| 309 | = field->value;
|
---|
[1875a0c] | 310 | } else if (mouse_dev->buttons[field->usage - field->usage_minimum] != 0
|
---|
[30710035] | 311 | && field->value == 0) {
|
---|
[5da7199] | 312 | async_exch_t *exch =
|
---|
| 313 | async_exchange_begin(mouse_dev->mouse_sess);
|
---|
| 314 | async_req_2_0(exch, MOUSEEV_BUTTON_EVENT, field->usage, 0);
|
---|
| 315 | async_exchange_end(exch);
|
---|
[a8c4e871] | 316 |
|
---|
[1875a0c] | 317 | mouse_dev->buttons[field->usage - field->usage_minimum] =
|
---|
| 318 | field->value;
|
---|
| 319 | }
|
---|
[a8c4e871] | 320 |
|
---|
[30710035] | 321 | field = usb_hid_report_get_sibling(
|
---|
[a8c4e871] | 322 | &hid_dev->report, field, path, USB_HID_PATH_COMPARE_END
|
---|
| 323 | | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
|
---|
[30710035] | 324 | USB_HID_REPORT_TYPE_INPUT);
|
---|
[e9f0348] | 325 | }
|
---|
[cc29622] | 326 |
|
---|
[30710035] | 327 | usb_hid_report_path_free(path);
|
---|
[e9f0348] | 328 |
|
---|
| 329 | return true;
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 | /*----------------------------------------------------------------------------*/
|
---|
| 333 |
|
---|
[65b458c4] | 334 | static int usb_mouse_create_function(usb_hid_dev_t *hid_dev, usb_mouse_t *mouse)
|
---|
[31cfee16] | 335 | {
|
---|
[65b458c4] | 336 | assert(hid_dev != NULL);
|
---|
| 337 | assert(mouse != NULL);
|
---|
[cc29622] | 338 |
|
---|
[15f3c3f] | 339 | /* Create the exposed function. */
|
---|
[31cfee16] | 340 | usb_log_debug("Creating DDF function %s...\n", HID_MOUSE_FUN_NAME);
|
---|
| 341 | ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed,
|
---|
| 342 | HID_MOUSE_FUN_NAME);
|
---|
| 343 | if (fun == NULL) {
|
---|
| 344 | usb_log_error("Could not create DDF function node.\n");
|
---|
| 345 | return ENOMEM;
|
---|
| 346 | }
|
---|
[cc29622] | 347 |
|
---|
[65b458c4] | 348 | fun->ops = &mouse->ops;
|
---|
[19e0560e] | 349 | fun->driver_data = mouse;
|
---|
[31cfee16] | 350 |
|
---|
| 351 | int rc = ddf_fun_bind(fun);
|
---|
| 352 | if (rc != EOK) {
|
---|
| 353 | usb_log_error("Could not bind DDF function: %s.\n",
|
---|
| 354 | str_error(rc));
|
---|
| 355 | return rc;
|
---|
| 356 | }
|
---|
[cc29622] | 357 |
|
---|
[1dc4a5e] | 358 | usb_log_debug("Adding DDF function to category %s...\n",
|
---|
| 359 | HID_MOUSE_CATEGORY);
|
---|
| 360 | rc = ddf_fun_add_to_category(fun, HID_MOUSE_CATEGORY);
|
---|
[31cfee16] | 361 | if (rc != EOK) {
|
---|
| 362 | usb_log_error(
|
---|
[1dc4a5e] | 363 | "Could not add DDF function to category %s: %s.\n",
|
---|
| 364 | HID_MOUSE_CATEGORY, str_error(rc));
|
---|
[31cfee16] | 365 | return rc;
|
---|
| 366 | }
|
---|
[f2964e45] | 367 | mouse->mouse_fun = fun;
|
---|
[cc29622] | 368 |
|
---|
[f8e549b] | 369 | /*
|
---|
| 370 | * Special function for acting as keyboard (wheel)
|
---|
| 371 | */
|
---|
| 372 | usb_log_debug("Creating DDF function %s...\n",
|
---|
| 373 | HID_MOUSE_WHEEL_FUN_NAME);
|
---|
| 374 | fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed,
|
---|
| 375 | HID_MOUSE_WHEEL_FUN_NAME);
|
---|
| 376 | if (fun == NULL) {
|
---|
| 377 | usb_log_error("Could not create DDF function node.\n");
|
---|
| 378 | return ENOMEM;
|
---|
| 379 | }
|
---|
[cc29622] | 380 |
|
---|
[f8e549b] | 381 | /*
|
---|
| 382 | * Store the initialized HID device and HID ops
|
---|
| 383 | * to the DDF function.
|
---|
| 384 | */
|
---|
[65b458c4] | 385 | fun->ops = &mouse->ops;
|
---|
[19e0560e] | 386 | fun->driver_data = mouse;
|
---|
[f8e549b] | 387 |
|
---|
| 388 | rc = ddf_fun_bind(fun);
|
---|
| 389 | if (rc != EOK) {
|
---|
| 390 | usb_log_error("Could not bind DDF function: %s.\n",
|
---|
| 391 | str_error(rc));
|
---|
| 392 | return rc;
|
---|
| 393 | }
|
---|
[cc29622] | 394 |
|
---|
[1dc4a5e] | 395 | usb_log_debug("Adding DDF function to category %s...\n",
|
---|
| 396 | HID_MOUSE_WHEEL_CATEGORY);
|
---|
| 397 | rc = ddf_fun_add_to_category(fun, HID_MOUSE_WHEEL_CATEGORY);
|
---|
[f8e549b] | 398 | if (rc != EOK) {
|
---|
| 399 | usb_log_error(
|
---|
[1dc4a5e] | 400 | "Could not add DDF function to category %s: %s.\n",
|
---|
| 401 | HID_MOUSE_WHEEL_CATEGORY, str_error(rc));
|
---|
[f8e549b] | 402 | return rc;
|
---|
| 403 | }
|
---|
[f2964e45] | 404 | mouse->wheel_fun = fun;
|
---|
[cc29622] | 405 |
|
---|
[31cfee16] | 406 | return EOK;
|
---|
| 407 | }
|
---|
| 408 |
|
---|
| 409 | /*----------------------------------------------------------------------------*/
|
---|
| 410 |
|
---|
[4093b14] | 411 | /** Get highest index of a button mentioned in given report.
|
---|
| 412 | *
|
---|
| 413 | * @param report HID report.
|
---|
| 414 | * @param report_id Report id we are interested in.
|
---|
| 415 | * @return Highest button mentioned in the report.
|
---|
| 416 | * @retval 1 No button was mentioned.
|
---|
| 417 | *
|
---|
| 418 | */
|
---|
| 419 | static size_t usb_mouse_get_highest_button(usb_hid_report_t *report, uint8_t report_id)
|
---|
| 420 | {
|
---|
| 421 | size_t highest_button = 0;
|
---|
| 422 |
|
---|
| 423 | usb_hid_report_path_t *path = usb_hid_report_path();
|
---|
| 424 | usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_BUTTON, 0);
|
---|
| 425 | usb_hid_report_path_set_report_id(path, report_id);
|
---|
| 426 |
|
---|
| 427 | usb_hid_report_field_t *field = NULL;
|
---|
| 428 |
|
---|
| 429 | /* Break from within. */
|
---|
| 430 | while (1) {
|
---|
| 431 | field = usb_hid_report_get_sibling(
|
---|
| 432 | report, field, path,
|
---|
| 433 | USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
|
---|
| 434 | USB_HID_REPORT_TYPE_INPUT);
|
---|
| 435 | /* No more buttons? */
|
---|
| 436 | if (field == NULL) {
|
---|
| 437 | break;
|
---|
| 438 | }
|
---|
| 439 |
|
---|
| 440 | size_t current_button = field->usage - field->usage_minimum;
|
---|
| 441 | if (current_button > highest_button) {
|
---|
| 442 | highest_button = current_button;
|
---|
| 443 | }
|
---|
| 444 | }
|
---|
| 445 |
|
---|
| 446 | usb_hid_report_path_free(path);
|
---|
| 447 |
|
---|
| 448 | return highest_button;
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 | /*----------------------------------------------------------------------------*/
|
---|
| 452 |
|
---|
[65b458c4] | 453 | int usb_mouse_init(usb_hid_dev_t *hid_dev, void **data)
|
---|
[e9f0348] | 454 | {
|
---|
| 455 | usb_log_debug("Initializing HID/Mouse structure...\n");
|
---|
[cc29622] | 456 |
|
---|
[e9f0348] | 457 | if (hid_dev == NULL) {
|
---|
| 458 | usb_log_error("Failed to init keyboard structure: no structure"
|
---|
| 459 | " given.\n");
|
---|
| 460 | return EINVAL;
|
---|
| 461 | }
|
---|
[cc29622] | 462 |
|
---|
[e9f0348] | 463 | usb_mouse_t *mouse_dev = usb_mouse_new();
|
---|
| 464 | if (mouse_dev == NULL) {
|
---|
| 465 | usb_log_error("Error while creating USB/HID Mouse device "
|
---|
| 466 | "structure.\n");
|
---|
| 467 | return ENOMEM;
|
---|
| 468 | }
|
---|
[cc29622] | 469 |
|
---|
[4093b14] | 470 | // FIXME: This may not be optimal since stupid hardware vendor may
|
---|
| 471 | // use buttons 1, 2, 3 and 6000 and we would allocate array of
|
---|
| 472 | // 6001*4B and use only 4 items in it.
|
---|
| 473 | // Since I doubt that hardware producers would do that, I think
|
---|
| 474 | // that the current solution is good enough.
|
---|
| 475 | /* Adding 1 because we will be accessing buttons[highest]. */
|
---|
[a8c4e871] | 476 | mouse_dev->buttons_count = 1 + usb_mouse_get_highest_button(
|
---|
| 477 | &hid_dev->report, hid_dev->report_id);
|
---|
[4093b14] | 478 | mouse_dev->buttons = calloc(mouse_dev->buttons_count, sizeof(int32_t));
|
---|
[cc29622] | 479 |
|
---|
[30710035] | 480 | if (mouse_dev->buttons == NULL) {
|
---|
[4093b14] | 481 | usb_log_error(NAME ": out of memory, giving up on device!\n");
|
---|
[30710035] | 482 | free(mouse_dev);
|
---|
| 483 | return ENOMEM;
|
---|
| 484 | }
|
---|
[4093b14] | 485 |
|
---|
| 486 |
|
---|
[e9f0348] | 487 | // save the Mouse device structure into the HID device structure
|
---|
[65b458c4] | 488 | *data = mouse_dev;
|
---|
[cc29622] | 489 |
|
---|
[e9f0348] | 490 | // set handler for incoming calls
|
---|
[65b458c4] | 491 | mouse_dev->ops.default_handler = default_connection_handler;
|
---|
[cc29622] | 492 |
|
---|
[e9f0348] | 493 | // TODO: how to know if the device supports the request???
|
---|
[19e0560e] | 494 | usbhid_req_set_idle(&hid_dev->usb_dev->ctrl_pipe,
|
---|
| 495 | hid_dev->usb_dev->interface_no, IDLE_RATE);
|
---|
[cc29622] | 496 |
|
---|
[65b458c4] | 497 | int rc = usb_mouse_create_function(hid_dev, mouse_dev);
|
---|
[31cfee16] | 498 | if (rc != EOK) {
|
---|
[5f6e25e] | 499 | usb_mouse_destroy(mouse_dev);
|
---|
[31cfee16] | 500 | return rc;
|
---|
| 501 | }
|
---|
[cc29622] | 502 |
|
---|
[e9f0348] | 503 | return EOK;
|
---|
| 504 | }
|
---|
| 505 |
|
---|
| 506 | /*----------------------------------------------------------------------------*/
|
---|
| 507 |
|
---|
[4d3c13e] | 508 | bool usb_mouse_polling_callback(usb_hid_dev_t *hid_dev, void *data)
|
---|
[e9f0348] | 509 | {
|
---|
[65b458c4] | 510 | if (hid_dev == NULL || data == NULL) {
|
---|
[e9f0348] | 511 | usb_log_error("Missing argument to the mouse polling callback."
|
---|
| 512 | "\n");
|
---|
| 513 | return false;
|
---|
| 514 | }
|
---|
[cc29622] | 515 |
|
---|
[65b458c4] | 516 | usb_mouse_t *mouse_dev = (usb_mouse_t *)data;
|
---|
| 517 |
|
---|
[4d3c13e] | 518 | return usb_mouse_process_report(hid_dev, mouse_dev);
|
---|
[e9f0348] | 519 | }
|
---|
| 520 |
|
---|
| 521 | /*----------------------------------------------------------------------------*/
|
---|
| 522 |
|
---|
[65b458c4] | 523 | void usb_mouse_deinit(usb_hid_dev_t *hid_dev, void *data)
|
---|
[e9f0348] | 524 | {
|
---|
[65b458c4] | 525 | if (data != NULL) {
|
---|
[f2964e45] | 526 | usb_mouse_destroy(data);
|
---|
[65b458c4] | 527 | }
|
---|
[e9f0348] | 528 | }
|
---|
| 529 |
|
---|
| 530 | /*----------------------------------------------------------------------------*/
|
---|
| 531 |
|
---|
[60c0573] | 532 | int usb_mouse_set_boot_protocol(usb_hid_dev_t *hid_dev)
|
---|
[e9f0348] | 533 | {
|
---|
[a8c4e871] | 534 | int rc = usb_hid_parse_report_descriptor(
|
---|
| 535 | &hid_dev->report, USB_MOUSE_BOOT_REPORT_DESCRIPTOR,
|
---|
[e9f0348] | 536 | USB_MOUSE_BOOT_REPORT_DESCRIPTOR_SIZE);
|
---|
[cc29622] | 537 |
|
---|
[e9f0348] | 538 | if (rc != EOK) {
|
---|
| 539 | usb_log_error("Failed to parse boot report descriptor: %s\n",
|
---|
| 540 | str_error(rc));
|
---|
| 541 | return rc;
|
---|
| 542 | }
|
---|
[cc29622] | 543 |
|
---|
[a8c4e871] | 544 | rc = usbhid_req_set_protocol(&hid_dev->usb_dev->ctrl_pipe,
|
---|
[e9f0348] | 545 | hid_dev->usb_dev->interface_no, USB_HID_PROTOCOL_BOOT);
|
---|
[cc29622] | 546 |
|
---|
[e9f0348] | 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 | }
|
---|
[cc29622] | 552 |
|
---|
[e9f0348] | 553 | return EOK;
|
---|
| 554 | }
|
---|
| 555 |
|
---|
| 556 | /**
|
---|
| 557 | * @}
|
---|
| 558 | */
|
---|