Changes in uspace/drv/bus/usb/usbhid/mouse/mousedev.c [5cc9eba:5f6e25e] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/bus/usb/usbhid/mouse/mousedev.c
r5cc9eba r5f6e25e 42 42 #include <errno.h> 43 43 #include <async.h> 44 #include <async_obsolete.h> 44 45 #include <str_error.h> 45 46 #include <ipc/mouseev.h> … … 52 53 #include "../usbhid.h" 53 54 55 /** Number of simulated arrow-key presses for singel wheel step. */ 56 #define ARROWS_PER_SINGLE_WHEEL 3 57 58 // FIXME: remove this header 59 #include <abi/ipc/methods.h> 60 54 61 #define NAME "mouse" 55 62 56 63 /*----------------------------------------------------------------------------*/ 57 64 58 constusb_endpoint_description_t usb_hid_mouse_poll_endpoint_description = {65 usb_endpoint_description_t usb_hid_mouse_poll_endpoint_description = { 59 66 .transfer_type = USB_TRANSFER_INTERRUPT, 60 67 .direction = USB_DIRECTION_IN, … … 66 73 67 74 const char *HID_MOUSE_FUN_NAME = "mouse"; 75 const char *HID_MOUSE_WHEEL_FUN_NAME = "mouse-wheel"; 68 76 const char *HID_MOUSE_CATEGORY = "mouse"; 77 const char *HID_MOUSE_WHEEL_CATEGORY = "keyboard"; 69 78 70 79 /** Default idle rate for mouses. */ 71 80 static const uint8_t IDLE_RATE = 0; 72 73 /*----------------------------------------------------------------------------*/ 74 static const uint8_t USB_MOUSE_BOOT_REPORT_DESCRIPTOR[] = { 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] = { 75 91 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 76 92 0x09, 0x02, // USAGE (Mouse) … … 112 128 ipc_callid_t icallid, ipc_call_t *icall) 113 129 { 114 usb_mouse_t *mouse_dev = fun->driver_data; 115 130 sysarg_t method = IPC_GET_IMETHOD(*icall); 131 132 usb_mouse_t *mouse_dev = (usb_mouse_t *)fun->driver_data; 133 116 134 if (mouse_dev == NULL) { 117 usb_log_debug("%s: Missing parameters.\n", __FUNCTION__); 135 usb_log_debug("default_connection_handler: Missing " 136 "parameters.\n"); 118 137 async_answer_0(icallid, EINVAL); 119 138 return; 120 139 } 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); 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"); 137 155 async_answer_0(icallid, ELIMIT); 138 async_hangup(sess);156 return; 139 157 } 140 } else { 141 usb_log_debug("%s: Invalid function.\n", __FUNCTION__); 142 async_answer_0(icallid, EINVAL); 143 } 144 } 145 /*----------------------------------------------------------------------------*/ 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 146 226 static int get_mouse_axis_move_value(uint8_t rid, usb_hid_report_t *report, 147 227 int32_t usage) … … 172 252 { 173 253 assert(mouse_dev != NULL); 174 175 if (mouse_dev->mouse_ sess == NULL) {176 usb_log_warning(NAME " No console session.\n");254 255 if (mouse_dev->mouse_phone < 0) { 256 usb_log_warning(NAME " No console phone.\n"); 177 257 return true; 178 258 } 179 259 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 */ 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 */ 198 279 usb_hid_report_path_t *path = usb_hid_report_path(); 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 } 280 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_BUTTON, 0); 210 281 usb_hid_report_path_set_report_id(path, hid_dev->report_id); 211 282 212 283 usb_hid_report_field_t *field = usb_hid_report_get_sibling( 213 &hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END 214 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, USB_HID_REPORT_TYPE_INPUT); 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); 215 287 216 288 while (field != NULL) { 217 289 usb_log_debug2(NAME " VALUE(%X) USAGE(%X)\n", field->value, 218 290 field->usage); 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 } 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; 242 304 } 243 305 244 306 field = usb_hid_report_get_sibling( 245 &hid_dev->report, field, path, USB_HID_PATH_COMPARE_END246 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, 307 hid_dev->report, field, path, USB_HID_PATH_COMPARE_END 308 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, 247 309 USB_HID_REPORT_TYPE_INPUT); 248 310 } 249 311 250 312 usb_hid_report_path_free(path); 251 313 252 314 return true; 253 315 } 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 /*----------------------------------------------------------------------------*/ 316 317 /*----------------------------------------------------------------------------*/ 318 266 319 static int usb_mouse_create_function(usb_hid_dev_t *hid_dev, usb_mouse_t *mouse) 267 320 { 268 321 assert(hid_dev != NULL); 269 322 assert(mouse != NULL); 270 323 271 324 /* Create the exposed function. */ 272 325 usb_log_debug("Creating DDF function %s...\n", HID_MOUSE_FUN_NAME); 273 ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed, 326 ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed, 274 327 HID_MOUSE_FUN_NAME); 275 328 if (fun == NULL) { 276 usb_log_error("Could not create DDF function node `%s'.\n", 277 HID_MOUSE_FUN_NAME); 329 usb_log_error("Could not create DDF function node.\n"); 278 330 return ENOMEM; 279 331 } 280 332 281 333 fun->ops = &mouse->ops; 282 334 fun->driver_data = mouse; … … 284 336 int rc = ddf_fun_bind(fun); 285 337 if (rc != EOK) { 286 usb_log_error("Could not bind DDF function `%s': %s.\n", 287 fun->name, str_error(rc)); 288 fun->driver_data = NULL; 338 usb_log_error("Could not bind DDF function: %s.\n", 339 str_error(rc)); 289 340 ddf_fun_destroy(fun); 290 341 return rc; 291 342 } 292 293 usb_log_debug("Adding DDF function `%s' to category %s...\n",294 fun->name,HID_MOUSE_CATEGORY);343 344 usb_log_debug("Adding DDF function to category %s...\n", 345 HID_MOUSE_CATEGORY); 295 346 rc = ddf_fun_add_to_category(fun, HID_MOUSE_CATEGORY); 296 347 if (rc != EOK) { … … 298 349 "Could not add DDF function to category %s: %s.\n", 299 350 HID_MOUSE_CATEGORY, str_error(rc)); 300 FUN_UNBIND_DESTROY(fun); 301 return rc; 302 } 303 mouse->mouse_fun = fun; 304 351 ddf_fun_destroy(fun); 352 return rc; 353 } 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 305 393 return EOK; 306 394 } 307 395 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 /*----------------------------------------------------------------------------*/ 396 /*----------------------------------------------------------------------------*/ 397 348 398 int usb_mouse_init(usb_hid_dev_t *hid_dev, void **data) 349 399 { 350 400 usb_log_debug("Initializing HID/Mouse structure...\n"); 351 401 352 402 if (hid_dev == NULL) { 353 403 usb_log_error("Failed to init keyboard structure: no structure" … … 355 405 return EINVAL; 356 406 } 357 358 usb_mouse_t *mouse_dev = calloc(1, sizeof(usb_mouse_t));407 408 usb_mouse_t *mouse_dev = usb_mouse_new(); 359 409 if (mouse_dev == NULL) { 360 410 usb_log_error("Error while creating USB/HID Mouse device " … … 362 412 return ENOMEM; 363 413 } 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 414 415 mouse_dev->buttons = (int32_t *)calloc(USB_MOUSE_BUTTON_COUNT, 416 sizeof(int32_t)); 417 375 418 if (mouse_dev->buttons == NULL) { 376 usb_log_ error(NAME ": out of memory, giving up on device!\n");419 usb_log_fatal("No memory!\n"); 377 420 free(mouse_dev); 378 421 return ENOMEM; 379 422 } 380 423 424 // save the Mouse device structure into the HID device structure 425 *data = mouse_dev; 426 381 427 // set handler for incoming calls 382 428 mouse_dev->ops.default_handler = default_connection_handler; 383 429 384 430 // TODO: how to know if the device supports the request??? 385 usbhid_req_set_idle(&hid_dev->usb_dev->ctrl_pipe, 431 usbhid_req_set_idle(&hid_dev->usb_dev->ctrl_pipe, 386 432 hid_dev->usb_dev->interface_no, IDLE_RATE); 387 433 388 434 int rc = usb_mouse_create_function(hid_dev, mouse_dev); 389 435 if (rc != EOK) { 390 free(mouse_dev->buttons); 391 free(mouse_dev); 392 return rc; 393 } 394 395 /* Save the Mouse device structure into the HID device structure. */ 396 *data = mouse_dev; 397 436 usb_mouse_destroy(mouse_dev); 437 return rc; 438 } 439 398 440 return EOK; 399 441 } 400 /*----------------------------------------------------------------------------*/ 442 443 /*----------------------------------------------------------------------------*/ 444 401 445 bool usb_mouse_polling_callback(usb_hid_dev_t *hid_dev, void *data) 402 446 { 403 447 if (hid_dev == NULL || data == NULL) { 404 usb_log_error( 405 " Missing argument to the mouse polling callback.\n");448 usb_log_error("Missing argument to the mouse polling callback." 449 "\n"); 406 450 return false; 407 451 } 408 409 usb_mouse_t *mouse_dev = data;410 452 453 usb_mouse_t *mouse_dev = (usb_mouse_t *)data; 454 411 455 return usb_mouse_process_report(hid_dev, mouse_dev); 412 456 } 413 /*----------------------------------------------------------------------------*/ 457 458 /*----------------------------------------------------------------------------*/ 459 414 460 void usb_mouse_deinit(usb_hid_dev_t *hid_dev, void *data) 415 461 { 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 /*----------------------------------------------------------------------------*/ 462 if (data != NULL) { 463 usb_mouse_destroy((usb_mouse_t *)data); 464 } 465 } 466 467 /*----------------------------------------------------------------------------*/ 468 435 469 int usb_mouse_set_boot_protocol(usb_hid_dev_t *hid_dev) 436 470 { 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 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 441 475 if (rc != EOK) { 442 476 usb_log_error("Failed to parse boot report descriptor: %s\n", … … 444 478 return rc; 445 479 } 446 447 rc = usbhid_req_set_protocol(&hid_dev->usb_dev->ctrl_pipe, 480 481 rc = usbhid_req_set_protocol(&hid_dev->usb_dev->ctrl_pipe, 448 482 hid_dev->usb_dev->interface_no, USB_HID_PROTOCOL_BOOT); 449 483 450 484 if (rc != EOK) { 451 485 usb_log_warning("Failed to set boot protocol to the device: " … … 453 487 return rc; 454 488 } 455 489 456 490 return EOK; 457 491 }
Note:
See TracChangeset
for help on using the changeset viewer.