Changes in uspace/drv/bus/usb/usbhid/mouse/mousedev.c [5f6e25e:5cc9eba] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/bus/usb/usbhid/mouse/mousedev.c
r5f6e25e r5cc9eba 42 42 #include <errno.h> 43 43 #include <async.h> 44 #include <async_obsolete.h>45 44 #include <str_error.h> 46 45 #include <ipc/mouseev.h> … … 53 52 #include "../usbhid.h" 54 53 55 /** Number of simulated arrow-key presses for singel wheel step. */56 #define ARROWS_PER_SINGLE_WHEEL 357 58 // FIXME: remove this header59 #include <abi/ipc/methods.h>60 61 54 #define NAME "mouse" 62 55 63 56 /*----------------------------------------------------------------------------*/ 64 57 65 usb_endpoint_description_t usb_hid_mouse_poll_endpoint_description = {58 const usb_endpoint_description_t usb_hid_mouse_poll_endpoint_description = { 66 59 .transfer_type = USB_TRANSFER_INTERRUPT, 67 60 .direction = USB_DIRECTION_IN, … … 73 66 74 67 const char *HID_MOUSE_FUN_NAME = "mouse"; 75 const char *HID_MOUSE_WHEEL_FUN_NAME = "mouse-wheel";76 68 const char *HID_MOUSE_CATEGORY = "mouse"; 77 const char *HID_MOUSE_WHEEL_CATEGORY = "keyboard";78 69 79 70 /** Default idle rate for mouses. */ 80 71 static const uint8_t IDLE_RATE = 0; 81 static const size_t USB_MOUSE_BUTTON_COUNT = 3; 82 83 /*----------------------------------------------------------------------------*/ 84 85 enum { 86 USB_MOUSE_BOOT_REPORT_DESCRIPTOR_SIZE = 63 87 }; 88 89 static const uint8_t USB_MOUSE_BOOT_REPORT_DESCRIPTOR[ 90 USB_MOUSE_BOOT_REPORT_DESCRIPTOR_SIZE] = { 72 73 /*----------------------------------------------------------------------------*/ 74 static const uint8_t USB_MOUSE_BOOT_REPORT_DESCRIPTOR[] = { 91 75 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 92 76 0x09, 0x02, // USAGE (Mouse) … … 128 112 ipc_callid_t icallid, ipc_call_t *icall) 129 113 { 130 sysarg_t method = IPC_GET_IMETHOD(*icall); 131 132 usb_mouse_t *mouse_dev = (usb_mouse_t *)fun->driver_data; 133 114 usb_mouse_t *mouse_dev = fun->driver_data; 115 134 116 if (mouse_dev == NULL) { 135 usb_log_debug("default_connection_handler: Missing " 136 "parameters.\n"); 117 usb_log_debug("%s: Missing parameters.\n", __FUNCTION__); 137 118 async_answer_0(icallid, EINVAL); 138 119 return; 139 120 } 140 141 usb_log_debug("default_connection_handler: fun->name: %s\n", 142 fun->name); 143 usb_log_debug("default_connection_handler: mouse_phone: %d, wheel " 144 "phone: %d\n", mouse_dev->mouse_phone, mouse_dev->wheel_phone); 145 146 int *phone = (str_cmp(fun->name, HID_MOUSE_FUN_NAME) == 0) 147 ? &mouse_dev->mouse_phone : &mouse_dev->wheel_phone; 148 149 if (method == IPC_M_CONNECT_TO_ME) { 150 int callback = IPC_GET_ARG5(*icall); 151 152 if (*phone != -1) { 153 usb_log_debug("default_connection_handler: Console " 154 "phone to mouse already set.\n"); 121 122 usb_log_debug("%s: fun->name: %s\n", __FUNCTION__, fun->name); 123 usb_log_debug("%s: mouse_sess: %p\n", 124 __FUNCTION__, mouse_dev->mouse_sess); 125 126 async_sess_t *sess = 127 async_callback_receive_start(EXCHANGE_SERIALIZE, icall); 128 if (sess != NULL) { 129 if (mouse_dev->mouse_sess == NULL) { 130 mouse_dev->mouse_sess = sess; 131 usb_log_debug("Console session to %s set ok (%p).\n", 132 fun->name, sess); 133 async_answer_0(icallid, EOK); 134 } else { 135 usb_log_error("Console session to %s already set.\n", 136 fun->name); 155 137 async_answer_0(icallid, ELIMIT); 156 return;138 async_hangup(sess); 157 139 } 158 159 *phone = callback; 160 usb_log_debug("Console phone to mouse set ok (%d).\n", *phone); 161 async_answer_0(icallid, EOK); 162 return; 163 } 164 165 usb_log_debug("default_connection_handler: Invalid function.\n"); 166 async_answer_0(icallid, EINVAL); 167 } 168 169 /*----------------------------------------------------------------------------*/ 170 171 static usb_mouse_t *usb_mouse_new(void) 172 { 173 usb_mouse_t *mouse = calloc(1, sizeof(usb_mouse_t)); 174 if (mouse == NULL) { 175 return NULL; 176 } 177 mouse->mouse_phone = -1; 178 mouse->wheel_phone = -1; 179 180 return mouse; 181 } 182 183 /*----------------------------------------------------------------------------*/ 184 185 static void usb_mouse_destroy(usb_mouse_t *mouse_dev) 186 { 187 assert(mouse_dev != NULL); 188 189 // hangup phone to the console 190 if (mouse_dev->mouse_phone >= 0) { 191 async_obsolete_hangup(mouse_dev->mouse_phone); 192 } 193 194 if (mouse_dev->wheel_phone >= 0) { 195 async_obsolete_hangup(mouse_dev->wheel_phone); 196 } 197 } 198 199 /*----------------------------------------------------------------------------*/ 200 201 static void usb_mouse_send_wheel(const usb_mouse_t *mouse_dev, int wheel) 202 { 203 unsigned int key = (wheel > 0) ? KC_UP : KC_DOWN; 204 205 if (mouse_dev->wheel_phone < 0) { 206 usb_log_warning( 207 "Connection to console not ready, wheel roll discarded.\n"); 208 return; 209 } 210 211 int count = ((wheel < 0) ? -wheel : wheel) * ARROWS_PER_SINGLE_WHEEL; 212 int i; 213 214 for (i = 0; i < count; i++) { 215 /* Send arrow press and release. */ 216 usb_log_debug2("Sending key %d to the console\n", key); 217 async_obsolete_msg_4(mouse_dev->wheel_phone, KBDEV_EVENT, 218 KEY_PRESS, key, 0, 0); 219 async_obsolete_msg_4(mouse_dev->wheel_phone, KBDEV_EVENT, 220 KEY_RELEASE, key, 0, 0); 221 } 222 } 223 224 /*----------------------------------------------------------------------------*/ 225 140 } else { 141 usb_log_debug("%s: Invalid function.\n", __FUNCTION__); 142 async_answer_0(icallid, EINVAL); 143 } 144 } 145 /*----------------------------------------------------------------------------*/ 226 146 static int get_mouse_axis_move_value(uint8_t rid, usb_hid_report_t *report, 227 147 int32_t usage) … … 252 172 { 253 173 assert(mouse_dev != NULL); 254 255 if (mouse_dev->mouse_ phone < 0) {256 usb_log_warning(NAME " No console phone.\n");174 175 if (mouse_dev->mouse_sess == NULL) { 176 usb_log_warning(NAME " No console session.\n"); 257 177 return true; 258 178 } 259 179 260 int shift_x = get_mouse_axis_move_value(hid_dev->report_id, 261 hid_dev->report, USB_HIDUT_USAGE_GENERIC_DESKTOP_X); 262 int shift_y = get_mouse_axis_move_value(hid_dev->report_id, 263 hid_dev->report, USB_HIDUT_USAGE_GENERIC_DESKTOP_Y); 264 int wheel = get_mouse_axis_move_value(hid_dev->report_id, 265 hid_dev->report, USB_HIDUT_USAGE_GENERIC_DESKTOP_WHEEL); 266 267 if ((shift_x != 0) || (shift_y != 0)) { 268 async_obsolete_req_2_0(mouse_dev->mouse_phone, 269 MOUSEEV_MOVE_EVENT, shift_x, shift_y); 270 } 271 272 if (wheel != 0) { 273 usb_mouse_send_wheel(mouse_dev, wheel); 274 } 275 276 /* 277 * Buttons 278 */ 180 const int shift_x = get_mouse_axis_move_value(hid_dev->report_id, 181 &hid_dev->report, USB_HIDUT_USAGE_GENERIC_DESKTOP_X); 182 const int shift_y = get_mouse_axis_move_value(hid_dev->report_id, 183 &hid_dev->report, USB_HIDUT_USAGE_GENERIC_DESKTOP_Y); 184 const int wheel = get_mouse_axis_move_value(hid_dev->report_id, 185 &hid_dev->report, USB_HIDUT_USAGE_GENERIC_DESKTOP_WHEEL); 186 187 if (shift_x || shift_y || wheel) { 188 async_exch_t *exch = 189 async_exchange_begin(mouse_dev->mouse_sess); 190 if (exch != NULL) { 191 async_msg_3(exch, MOUSEEV_MOVE_EVENT, 192 shift_x, shift_y, wheel); 193 async_exchange_end(exch); 194 } 195 } 196 197 /* Buttons */ 279 198 usb_hid_report_path_t *path = usb_hid_report_path(); 280 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_BUTTON, 0); 199 if (path == NULL) { 200 usb_log_warning("Failed to create USB HID report path.\n"); 201 return true; 202 } 203 int ret = 204 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_BUTTON, 0); 205 if (ret != EOK) { 206 usb_hid_report_path_free(path); 207 usb_log_warning("Failed to add buttons to report path.\n"); 208 return true; 209 } 281 210 usb_hid_report_path_set_report_id(path, hid_dev->report_id); 282 211 283 212 usb_hid_report_field_t *field = usb_hid_report_get_sibling( 284 hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END 285 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, 286 USB_HID_REPORT_TYPE_INPUT); 213 &hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END 214 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, USB_HID_REPORT_TYPE_INPUT); 287 215 288 216 while (field != NULL) { 289 217 usb_log_debug2(NAME " VALUE(%X) USAGE(%X)\n", field->value, 290 218 field->usage); 291 292 if (mouse_dev->buttons[field->usage - field->usage_minimum] == 0 293 && field->value != 0) { 294 async_obsolete_req_2_0(mouse_dev->mouse_phone, 295 MOUSEEV_BUTTON_EVENT, field->usage, 1); 296 mouse_dev->buttons[field->usage - field->usage_minimum] 297 = field->value; 298 } else if (mouse_dev->buttons[field->usage - field->usage_minimum] != 0 299 && field->value == 0) { 300 async_obsolete_req_2_0(mouse_dev->mouse_phone, 301 MOUSEEV_BUTTON_EVENT, field->usage, 0); 302 mouse_dev->buttons[field->usage - field->usage_minimum] = 303 field->value; 219 assert(field->usage > field->usage_minimum); 220 const unsigned index = field->usage - field->usage_minimum; 221 assert(index < mouse_dev->buttons_count); 222 223 if (mouse_dev->buttons[index] == 0 && field->value != 0) { 224 async_exch_t *exch = 225 async_exchange_begin(mouse_dev->mouse_sess); 226 if (exch != NULL) { 227 async_req_2_0(exch, MOUSEEV_BUTTON_EVENT, 228 field->usage, 1); 229 async_exchange_end(exch); 230 mouse_dev->buttons[index] = field->value; 231 } 232 233 } else if (mouse_dev->buttons[index] != 0 && field->value == 0) { 234 async_exch_t *exch = 235 async_exchange_begin(mouse_dev->mouse_sess); 236 if (exch != NULL) { 237 async_req_2_0(exch, MOUSEEV_BUTTON_EVENT, 238 field->usage, 0); 239 async_exchange_end(exch); 240 mouse_dev->buttons[index] = field->value; 241 } 304 242 } 305 243 306 244 field = usb_hid_report_get_sibling( 307 hid_dev->report, field, path, USB_HID_PATH_COMPARE_END308 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, 245 &hid_dev->report, field, path, USB_HID_PATH_COMPARE_END 246 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, 309 247 USB_HID_REPORT_TYPE_INPUT); 310 248 } 311 249 312 250 usb_hid_report_path_free(path); 313 251 314 252 return true; 315 253 } 316 317 /*----------------------------------------------------------------------------*/ 318 254 /*----------------------------------------------------------------------------*/ 255 #define FUN_UNBIND_DESTROY(fun) \ 256 if (fun) { \ 257 if (ddf_fun_unbind((fun)) == EOK) { \ 258 (fun)->driver_data = NULL; \ 259 ddf_fun_destroy((fun)); \ 260 } else { \ 261 usb_log_error("Could not unbind function `%s', it " \ 262 "will not be destroyed.\n", (fun)->name); \ 263 } \ 264 } else (void)0 265 /*----------------------------------------------------------------------------*/ 319 266 static int usb_mouse_create_function(usb_hid_dev_t *hid_dev, usb_mouse_t *mouse) 320 267 { 321 268 assert(hid_dev != NULL); 322 269 assert(mouse != NULL); 323 270 324 271 /* Create the exposed function. */ 325 272 usb_log_debug("Creating DDF function %s...\n", HID_MOUSE_FUN_NAME); 326 ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed, 273 ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed, 327 274 HID_MOUSE_FUN_NAME); 328 275 if (fun == NULL) { 329 usb_log_error("Could not create DDF function node.\n"); 276 usb_log_error("Could not create DDF function node `%s'.\n", 277 HID_MOUSE_FUN_NAME); 330 278 return ENOMEM; 331 279 } 332 280 333 281 fun->ops = &mouse->ops; 334 282 fun->driver_data = mouse; … … 336 284 int rc = ddf_fun_bind(fun); 337 285 if (rc != EOK) { 338 usb_log_error("Could not bind DDF function: %s.\n", 339 str_error(rc)); 286 usb_log_error("Could not bind DDF function `%s': %s.\n", 287 fun->name, str_error(rc)); 288 fun->driver_data = NULL; 340 289 ddf_fun_destroy(fun); 341 290 return rc; 342 291 } 343 344 usb_log_debug("Adding DDF function to category %s...\n",345 HID_MOUSE_CATEGORY);292 293 usb_log_debug("Adding DDF function `%s' to category %s...\n", 294 fun->name, HID_MOUSE_CATEGORY); 346 295 rc = ddf_fun_add_to_category(fun, HID_MOUSE_CATEGORY); 347 296 if (rc != EOK) { … … 349 298 "Could not add DDF function to category %s: %s.\n", 350 299 HID_MOUSE_CATEGORY, str_error(rc)); 351 ddf_fun_destroy(fun);300 FUN_UNBIND_DESTROY(fun); 352 301 return rc; 353 302 } 354 355 /* 356 * Special function for acting as keyboard (wheel) 357 */ 358 usb_log_debug("Creating DDF function %s...\n", 359 HID_MOUSE_WHEEL_FUN_NAME); 360 fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed, 361 HID_MOUSE_WHEEL_FUN_NAME); 362 if (fun == NULL) { 363 usb_log_error("Could not create DDF function node.\n"); 364 return ENOMEM; 365 } 366 367 /* 368 * Store the initialized HID device and HID ops 369 * to the DDF function. 370 */ 371 fun->ops = &mouse->ops; 372 fun->driver_data = mouse; 373 374 rc = ddf_fun_bind(fun); 375 if (rc != EOK) { 376 usb_log_error("Could not bind DDF function: %s.\n", 377 str_error(rc)); 378 ddf_fun_destroy(fun); 379 return rc; 380 } 381 382 usb_log_debug("Adding DDF function to category %s...\n", 383 HID_MOUSE_WHEEL_CATEGORY); 384 rc = ddf_fun_add_to_category(fun, HID_MOUSE_WHEEL_CATEGORY); 385 if (rc != EOK) { 386 usb_log_error( 387 "Could not add DDF function to category %s: %s.\n", 388 HID_MOUSE_WHEEL_CATEGORY, str_error(rc)); 389 ddf_fun_destroy(fun); 390 return rc; 391 } 392 303 mouse->mouse_fun = fun; 304 393 305 return EOK; 394 306 } 395 307 396 /*----------------------------------------------------------------------------*/ 397 308 /** Get highest index of a button mentioned in given report. 309 * 310 * @param report HID report. 311 * @param report_id Report id we are interested in. 312 * @return Highest button mentioned in the report. 313 * @retval 1 No button was mentioned. 314 * 315 */ 316 static size_t usb_mouse_get_highest_button(usb_hid_report_t *report, uint8_t report_id) 317 { 318 size_t highest_button = 0; 319 320 usb_hid_report_path_t *path = usb_hid_report_path(); 321 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_BUTTON, 0); 322 usb_hid_report_path_set_report_id(path, report_id); 323 324 usb_hid_report_field_t *field = NULL; 325 326 /* Break from within. */ 327 while (1) { 328 field = usb_hid_report_get_sibling( 329 report, field, path, 330 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, 331 USB_HID_REPORT_TYPE_INPUT); 332 /* No more buttons? */ 333 if (field == NULL) { 334 break; 335 } 336 337 size_t current_button = field->usage - field->usage_minimum; 338 if (current_button > highest_button) { 339 highest_button = current_button; 340 } 341 } 342 343 usb_hid_report_path_free(path); 344 345 return highest_button; 346 } 347 /*----------------------------------------------------------------------------*/ 398 348 int usb_mouse_init(usb_hid_dev_t *hid_dev, void **data) 399 349 { 400 350 usb_log_debug("Initializing HID/Mouse structure...\n"); 401 351 402 352 if (hid_dev == NULL) { 403 353 usb_log_error("Failed to init keyboard structure: no structure" … … 405 355 return EINVAL; 406 356 } 407 408 usb_mouse_t *mouse_dev = usb_mouse_new();357 358 usb_mouse_t *mouse_dev = calloc(1, sizeof(usb_mouse_t)); 409 359 if (mouse_dev == NULL) { 410 360 usb_log_error("Error while creating USB/HID Mouse device " … … 412 362 return ENOMEM; 413 363 } 414 415 mouse_dev->buttons = (int32_t *)calloc(USB_MOUSE_BUTTON_COUNT, 416 sizeof(int32_t)); 417 364 365 // FIXME: This may not be optimal since stupid hardware vendor may 366 // use buttons 1, 2, 3 and 6000 and we would allocate array of 367 // 6001*4B and use only 4 items in it. 368 // Since I doubt that hardware producers would do that, I think 369 // that the current solution is good enough. 370 /* Adding 1 because we will be accessing buttons[highest]. */ 371 mouse_dev->buttons_count = 1 + usb_mouse_get_highest_button( 372 &hid_dev->report, hid_dev->report_id); 373 mouse_dev->buttons = calloc(mouse_dev->buttons_count, sizeof(int32_t)); 374 418 375 if (mouse_dev->buttons == NULL) { 419 usb_log_ fatal("No memory!\n");376 usb_log_error(NAME ": out of memory, giving up on device!\n"); 420 377 free(mouse_dev); 421 378 return ENOMEM; 422 379 } 423 424 // save the Mouse device structure into the HID device structure 425 *data = mouse_dev; 426 380 427 381 // set handler for incoming calls 428 382 mouse_dev->ops.default_handler = default_connection_handler; 429 383 430 384 // TODO: how to know if the device supports the request??? 431 usbhid_req_set_idle(&hid_dev->usb_dev->ctrl_pipe, 385 usbhid_req_set_idle(&hid_dev->usb_dev->ctrl_pipe, 432 386 hid_dev->usb_dev->interface_no, IDLE_RATE); 433 387 434 388 int rc = usb_mouse_create_function(hid_dev, mouse_dev); 435 389 if (rc != EOK) { 436 usb_mouse_destroy(mouse_dev); 390 free(mouse_dev->buttons); 391 free(mouse_dev); 437 392 return rc; 438 393 } 439 394 395 /* Save the Mouse device structure into the HID device structure. */ 396 *data = mouse_dev; 397 440 398 return EOK; 441 399 } 442 443 /*----------------------------------------------------------------------------*/ 444 400 /*----------------------------------------------------------------------------*/ 445 401 bool usb_mouse_polling_callback(usb_hid_dev_t *hid_dev, void *data) 446 402 { 447 403 if (hid_dev == NULL || data == NULL) { 448 usb_log_error( "Missing argument to the mouse polling callback."449 " \n");404 usb_log_error( 405 "Missing argument to the mouse polling callback.\n"); 450 406 return false; 451 407 } 452 453 usb_mouse_t *mouse_dev = (usb_mouse_t *)data;454 408 409 usb_mouse_t *mouse_dev = data; 410 455 411 return usb_mouse_process_report(hid_dev, mouse_dev); 456 412 } 457 458 /*----------------------------------------------------------------------------*/ 459 413 /*----------------------------------------------------------------------------*/ 460 414 void usb_mouse_deinit(usb_hid_dev_t *hid_dev, void *data) 461 415 { 462 if (data != NULL) { 463 usb_mouse_destroy((usb_mouse_t *)data); 464 } 465 } 466 467 /*----------------------------------------------------------------------------*/ 468 416 if (data == NULL) 417 return; 418 419 usb_mouse_t *mouse_dev = data; 420 421 /* Hangup session to the console */ 422 if (mouse_dev->mouse_sess != NULL) { 423 const int ret = async_hangup(mouse_dev->mouse_sess); 424 if (ret != EOK) 425 usb_log_warning("Failed to hang up mouse session: " 426 "%p, %s.\n", mouse_dev->mouse_sess, str_error(ret)); 427 } 428 429 FUN_UNBIND_DESTROY(mouse_dev->mouse_fun); 430 431 free(mouse_dev->buttons); 432 free(mouse_dev); 433 } 434 /*----------------------------------------------------------------------------*/ 469 435 int usb_mouse_set_boot_protocol(usb_hid_dev_t *hid_dev) 470 436 { 471 int rc = usb_hid_parse_report_descriptor( hid_dev->report,472 USB_MOUSE_BOOT_REPORT_DESCRIPTOR,473 USB_MOUSE_BOOT_REPORT_DESCRIPTOR_SIZE);474 437 int rc = usb_hid_parse_report_descriptor( 438 &hid_dev->report, USB_MOUSE_BOOT_REPORT_DESCRIPTOR, 439 sizeof(USB_MOUSE_BOOT_REPORT_DESCRIPTOR)); 440 475 441 if (rc != EOK) { 476 442 usb_log_error("Failed to parse boot report descriptor: %s\n", … … 478 444 return rc; 479 445 } 480 481 rc = usbhid_req_set_protocol(&hid_dev->usb_dev->ctrl_pipe, 446 447 rc = usbhid_req_set_protocol(&hid_dev->usb_dev->ctrl_pipe, 482 448 hid_dev->usb_dev->interface_no, USB_HID_PROTOCOL_BOOT); 483 449 484 450 if (rc != EOK) { 485 451 usb_log_warning("Failed to set boot protocol to the device: " … … 487 453 return rc; 488 454 } 489 455 490 456 return EOK; 491 457 }
Note:
See TracChangeset
for help on using the changeset viewer.