source: mainline/uspace/drv/hid/usbhid/mouse/mousedev.c@ 17c1d9db

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 17c1d9db was 91173333, checked in by Petr Manek <petr.manek@…>, 8 years ago

usbdev: use centralized joining mechanism, move away from device_removed() callback

  • Property mode set to 100644
File size: 14.1 KB
RevLine 
[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>
[1875a0cf]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
[e04182d]54#define NAME "mouse"
[30710035]55
[2b621cb]56static void default_connection_handler(ddf_fun_t *, ipc_callid_t, ipc_call_t *);
57
58static ddf_dev_ops_t ops = { .default_handler = default_connection_handler };
[e9f0348]59
[76fbd9a]60
[b803845]61const 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
70const char *HID_MOUSE_FUN_NAME = "mouse";
[1dc4a5e]71const char *HID_MOUSE_CATEGORY = "mouse";
[e9f0348]72
73/** Default idle rate for mouses. */
74static const uint8_t IDLE_RATE = 0;
75
[76fbd9a]76
[e3c78efc]77static const uint8_t USB_MOUSE_BOOT_REPORT_DESCRIPTOR[] = {
[e9f0348]78 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
79 0x09, 0x02, // USAGE (Mouse)
80 0xa1, 0x01, // COLLECTION (Application)
81 0x09, 0x01, // USAGE (Pointer)
82 0xa1, 0x00, // COLLECTION (Physical)
83 0x95, 0x03, // REPORT_COUNT (3)
84 0x75, 0x01, // REPORT_SIZE (1)
85 0x05, 0x09, // USAGE_PAGE (Button)
86 0x19, 0x01, // USAGE_MINIMUM (Button 1)
87 0x29, 0x03, // USAGE_MAXIMUM (Button 3)
88 0x15, 0x00, // LOGICAL_MINIMUM (0)
89 0x25, 0x01, // LOGICAL_MAXIMUM (1)
90 0x81, 0x02, // INPUT (Data,Var,Abs)
91 0x95, 0x01, // REPORT_COUNT (1)
92 0x75, 0x05, // REPORT_SIZE (5)
93 0x81, 0x01, // INPUT (Cnst)
94 0x75, 0x08, // REPORT_SIZE (8)
95 0x95, 0x02, // REPORT_COUNT (2)
96 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
97 0x09, 0x30, // USAGE (X)
98 0x09, 0x31, // USAGE (Y)
99 0x15, 0x81, // LOGICAL_MINIMUM (-127)
100 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
101 0x81, 0x06, // INPUT (Data,Var,Rel)
102 0xc0, // END_COLLECTION
103 0xc0 // END_COLLECTION
104};
105
[76fbd9a]106
[e9f0348]107
108/** Default handler for IPC methods not handled by DDF.
109 *
110 * @param fun Device function handling the call.
111 * @param icallid Call id.
112 * @param icall Call data.
113 */
114static void default_connection_handler(ddf_fun_t *fun,
115 ipc_callid_t icallid, ipc_call_t *icall)
116{
[56fd7cf]117 usb_mouse_t *mouse_dev = ddf_fun_data_get(fun);
[cc29622]118
[65b458c4]119 if (mouse_dev == NULL) {
[e04182d]120 usb_log_debug("%s: Missing parameters.\n", __FUNCTION__);
[e9f0348]121 async_answer_0(icallid, EINVAL);
122 return;
123 }
[cc29622]124
[56fd7cf]125 usb_log_debug("%s: fun->name: %s\n", __FUNCTION__, ddf_fun_get_name(fun));
[70172dc4]126 usb_log_debug("%s: mouse_sess: %p\n",
127 __FUNCTION__, mouse_dev->mouse_sess);
[cc29622]128
[5da7199]129 async_sess_t *sess =
130 async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
131 if (sess != NULL) {
[70172dc4]132 if (mouse_dev->mouse_sess == NULL) {
133 mouse_dev->mouse_sess = sess;
[e04182d]134 usb_log_debug("Console session to %s set ok (%p).\n",
[56fd7cf]135 ddf_fun_get_name(fun), sess);
[5da7199]136 async_answer_0(icallid, EOK);
137 } else {
[e04182d]138 usb_log_error("Console session to %s already set.\n",
[56fd7cf]139 ddf_fun_get_name(fun));
[e9f0348]140 async_answer_0(icallid, ELIMIT);
[70172dc4]141 async_hangup(sess);
[e9f0348]142 }
[5da7199]143 } else {
[e04182d]144 usb_log_debug("%s: Invalid function.\n", __FUNCTION__);
[5da7199]145 async_answer_0(icallid, EINVAL);
[e9f0348]146 }
147}
[76fbd9a]148
[ae303ad]149static const usb_hid_report_field_t *get_mouse_axis_move_field(uint8_t rid, usb_hid_report_t *report,
[a3a2fdb]150 int32_t usage)
[e9f0348]151{
[30710035]152 usb_hid_report_path_t *path = usb_hid_report_path();
[a3a2fdb]153 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_GENERIC_DESKTOP,
154 usage);
[e9f0348]155
[a3a2fdb]156 usb_hid_report_path_set_report_id(path, rid);
[e9f0348]157
[ae303ad]158 const usb_hid_report_field_t *field = usb_hid_report_get_sibling(
[a3a2fdb]159 report, NULL, path, USB_HID_PATH_COMPARE_END,
[30710035]160 USB_HID_REPORT_TYPE_INPUT);
[e9f0348]161
[30710035]162 usb_hid_report_path_free(path);
163
[ae303ad]164 return field;
[a3a2fdb]165}
[5cc9eba]166
[970f6e1]167static void usb_mouse_process_report(usb_hid_dev_t *hid_dev,
[a3a2fdb]168 usb_mouse_t *mouse_dev)
169{
170 assert(mouse_dev != NULL);
[cc29622]171
[5da7199]172 if (mouse_dev->mouse_sess == NULL) {
173 usb_log_warning(NAME " No console session.\n");
[970f6e1]174 return;
[e9f0348]175 }
[30710035]176
[ae303ad]177 const usb_hid_report_field_t *move_x = get_mouse_axis_move_field(
178 hid_dev->report_id, &hid_dev->report,
179 USB_HIDUT_USAGE_GENERIC_DESKTOP_X);
180 const usb_hid_report_field_t *move_y = get_mouse_axis_move_field(
181 hid_dev->report_id, &hid_dev->report,
182 USB_HIDUT_USAGE_GENERIC_DESKTOP_Y);
183 const usb_hid_report_field_t *wheel= get_mouse_axis_move_field(
184 hid_dev->report_id, &hid_dev->report,
185 USB_HIDUT_USAGE_GENERIC_DESKTOP_WHEEL);
186
187 bool absolute_x = move_x && !USB_HID_ITEM_FLAG_RELATIVE(move_x->item_flags);
188 bool absolute_y = move_y && !USB_HID_ITEM_FLAG_RELATIVE(move_y->item_flags);
189
190 /* Tablet shall always report both X and Y */
191 if (absolute_x != absolute_y) {
192 usb_log_error(NAME " cannot handle mix of absolute and relative mouse move.");
[970f6e1]193 return;
[ae303ad]194 }
195
196 int shift_x = move_x ? move_x->value : 0;
197 int shift_y = move_y ? move_y->value : 0;
198 int shift_z = wheel ? wheel->value : 0;
199
200 if (absolute_x && absolute_y) {
201 async_exch_t *exch =
202 async_exchange_begin(mouse_dev->mouse_sess);
203 if (exch != NULL) {
204 async_msg_4(exch, MOUSEEV_ABS_MOVE_EVENT,
205 shift_x, shift_y, move_x->logical_maximum, move_y->logical_maximum);
206 async_exchange_end(exch);
207 }
208
209 // Even if we move the mouse absolutely, we need to resolve wheel
210 shift_x = shift_y = 0;
211 }
212
[a3a2fdb]213
[ae303ad]214 if (shift_x || shift_y || shift_z) {
[a8c4e871]215 async_exch_t *exch =
216 async_exchange_begin(mouse_dev->mouse_sess);
[e3e0953]217 if (exch != NULL) {
[edb3cf2]218 async_msg_3(exch, MOUSEEV_MOVE_EVENT,
[ae303ad]219 shift_x, shift_y, shift_z);
[e3e0953]220 async_exchange_end(exch);
221 }
[e9f0348]222 }
[cc29622]223
[e3e0953]224 /* Buttons */
[a3a2fdb]225 usb_hid_report_path_t *path = usb_hid_report_path();
[e3e0953]226 if (path == NULL) {
227 usb_log_warning("Failed to create USB HID report path.\n");
[970f6e1]228 return;
[e3e0953]229 }
230 int ret =
231 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_BUTTON, 0);
232 if (ret != EOK) {
233 usb_hid_report_path_free(path);
234 usb_log_warning("Failed to add buttons to report path.\n");
[970f6e1]235 return;
[e3e0953]236 }
[65c3794]237 usb_hid_report_path_set_report_id(path, hid_dev->report_id);
[cc29622]238
[a3a2fdb]239 usb_hid_report_field_t *field = usb_hid_report_get_sibling(
[a8c4e871]240 &hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END
241 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, USB_HID_REPORT_TYPE_INPUT);
[30710035]242
[310c4df]243 while (field != NULL) {
[ae5f77d5]244 usb_log_debug2(NAME " VALUE(%X) USAGE(%X)\n", field->value,
[30710035]245 field->usage);
[5723ae49]246 assert(field->usage > field->usage_minimum);
247 const unsigned index = field->usage - field->usage_minimum;
248 assert(index < mouse_dev->buttons_count);
[e3e0953]249
[295f658]250 if (mouse_dev->buttons[index] != field->value) {
[5da7199]251 async_exch_t *exch =
252 async_exchange_begin(mouse_dev->mouse_sess);
[e3e0953]253 if (exch != NULL) {
254 async_req_2_0(exch, MOUSEEV_BUTTON_EVENT,
[295f658]255 field->usage, (field->value != 0) ? 1 : 0);
[e3e0953]256 async_exchange_end(exch);
257 mouse_dev->buttons[index] = field->value;
258 }
[1875a0cf]259 }
[a8c4e871]260
[30710035]261 field = usb_hid_report_get_sibling(
[a8c4e871]262 &hid_dev->report, field, path, USB_HID_PATH_COMPARE_END
263 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
[30710035]264 USB_HID_REPORT_TYPE_INPUT);
[e9f0348]265 }
[cc29622]266
[30710035]267 usb_hid_report_path_free(path);
[e9f0348]268}
[76fbd9a]269
[0f12c17]270#define FUN_UNBIND_DESTROY(fun) \
271if (fun) { \
272 if (ddf_fun_unbind((fun)) == EOK) { \
273 ddf_fun_destroy((fun)); \
274 } else { \
275 usb_log_error("Could not unbind function `%s', it " \
[56fd7cf]276 "will not be destroyed.\n", ddf_fun_get_name(fun)); \
[0f12c17]277 } \
[58563585]278} else (void) 0
[76fbd9a]279
[4093b14]280/** Get highest index of a button mentioned in given report.
281 *
282 * @param report HID report.
283 * @param report_id Report id we are interested in.
284 * @return Highest button mentioned in the report.
285 * @retval 1 No button was mentioned.
286 *
287 */
288static size_t usb_mouse_get_highest_button(usb_hid_report_t *report, uint8_t report_id)
289{
290 size_t highest_button = 0;
291
292 usb_hid_report_path_t *path = usb_hid_report_path();
293 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_BUTTON, 0);
294 usb_hid_report_path_set_report_id(path, report_id);
295
296 usb_hid_report_field_t *field = NULL;
297
298 /* Break from within. */
299 while (1) {
300 field = usb_hid_report_get_sibling(
301 report, field, path,
302 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
303 USB_HID_REPORT_TYPE_INPUT);
304 /* No more buttons? */
305 if (field == NULL) {
306 break;
307 }
308
309 size_t current_button = field->usage - field->usage_minimum;
310 if (current_button > highest_button) {
311 highest_button = current_button;
312 }
313 }
314
315 usb_hid_report_path_free(path);
316
317 return highest_button;
318}
[76fbd9a]319
[38b4a25]320static int mouse_dev_init(usb_mouse_t *mouse_dev, usb_hid_dev_t *hid_dev)
[e9f0348]321{
[4093b14]322 // FIXME: This may not be optimal since stupid hardware vendor may
323 // use buttons 1, 2, 3 and 6000 and we would allocate array of
324 // 6001*4B and use only 4 items in it.
325 // Since I doubt that hardware producers would do that, I think
326 // that the current solution is good enough.
327 /* Adding 1 because we will be accessing buttons[highest]. */
[a8c4e871]328 mouse_dev->buttons_count = 1 + usb_mouse_get_highest_button(
329 &hid_dev->report, hid_dev->report_id);
[4093b14]330 mouse_dev->buttons = calloc(mouse_dev->buttons_count, sizeof(int32_t));
[cc29622]331
[30710035]332 if (mouse_dev->buttons == NULL) {
[4093b14]333 usb_log_error(NAME ": out of memory, giving up on device!\n");
[30710035]334 free(mouse_dev);
335 return ENOMEM;
336 }
[4093b14]337
[e9f0348]338 // TODO: how to know if the device supports the request???
[c39e9fb]339 usbhid_req_set_idle(usb_device_get_default_pipe(hid_dev->usb_dev),
[8e10ef4]340 usb_device_get_iface_number(hid_dev->usb_dev), IDLE_RATE);
[38b4a25]341 return EOK;
342}
[cc29622]343
[38b4a25]344int usb_mouse_init(usb_hid_dev_t *hid_dev, void **data)
345{
346 usb_log_debug("Initializing HID/Mouse structure...\n");
347
348 if (hid_dev == NULL) {
[58563585]349 usb_log_error("Failed to init mouse structure: no structure"
[38b4a25]350 " given.\n");
351 return EINVAL;
352 }
353
354 /* Create the exposed function. */
355 usb_log_debug("Creating DDF function %s...\n", HID_MOUSE_FUN_NAME);
[6785b538]356 ddf_fun_t *fun = usb_device_ddf_fun_create(hid_dev->usb_dev,
357 fun_exposed, HID_MOUSE_FUN_NAME);
[38b4a25]358 if (fun == NULL) {
359 usb_log_error("Could not create DDF function node `%s'.\n",
360 HID_MOUSE_FUN_NAME);
361 return ENOMEM;
[31cfee16]362 }
[cc29622]363
[38b4a25]364 usb_mouse_t *mouse_dev = ddf_fun_data_alloc(fun, sizeof(usb_mouse_t));
365 if (mouse_dev == NULL) {
366 usb_log_error("Failed to alloc HID mouse device structure.\n");
367 ddf_fun_destroy(fun);
368 return ENOMEM;
369 }
370
371 int ret = mouse_dev_init(mouse_dev, hid_dev);
372 if (ret != EOK) {
373 usb_log_error("Failed to init HID mouse device structure.\n");
374 return ret;
375 }
376
377 ddf_fun_set_ops(fun, &ops);
378
379 ret = ddf_fun_bind(fun);
380 if (ret != EOK) {
381 usb_log_error("Could not bind DDF function `%s': %s.\n",
382 ddf_fun_get_name(fun), str_error(ret));
383 ddf_fun_destroy(fun);
384 return ret;
385 }
386
387 usb_log_debug("Adding DDF function `%s' to category %s...\n",
388 ddf_fun_get_name(fun), HID_MOUSE_CATEGORY);
389 ret = ddf_fun_add_to_category(fun, HID_MOUSE_CATEGORY);
390 if (ret != EOK) {
391 usb_log_error(
392 "Could not add DDF function to category %s: %s.\n",
393 HID_MOUSE_CATEGORY, str_error(ret));
394 FUN_UNBIND_DESTROY(fun);
395 return ret;
396 }
397 mouse_dev->mouse_fun = fun;
398
[5723ae49]399 /* Save the Mouse device structure into the HID device structure. */
400 *data = mouse_dev;
401
[e9f0348]402 return EOK;
403}
[76fbd9a]404
[4d3c13e]405bool usb_mouse_polling_callback(usb_hid_dev_t *hid_dev, void *data)
[e9f0348]406{
[65b458c4]407 if (hid_dev == NULL || data == NULL) {
[e3e0953]408 usb_log_error(
409 "Missing argument to the mouse polling callback.\n");
[e9f0348]410 return false;
411 }
[cc29622]412
[5723ae49]413 usb_mouse_t *mouse_dev = data;
[970f6e1]414 usb_mouse_process_report(hid_dev, mouse_dev);
[5723ae49]415
[970f6e1]416 /* Continue polling until the device is about to be removed. */
[91173333]417 return true;
[e9f0348]418}
[76fbd9a]419
[65b458c4]420void usb_mouse_deinit(usb_hid_dev_t *hid_dev, void *data)
[e9f0348]421{
[5723ae49]422 if (data == NULL)
423 return;
424
425 usb_mouse_t *mouse_dev = data;
426
427 /* Hangup session to the console */
428 if (mouse_dev->mouse_sess != NULL) {
429 const int ret = async_hangup(mouse_dev->mouse_sess);
430 if (ret != EOK)
431 usb_log_warning("Failed to hang up mouse session: "
432 "%p, %s.\n", mouse_dev->mouse_sess, str_error(ret));
433 }
434
435 free(mouse_dev->buttons);
[38b4a25]436 FUN_UNBIND_DESTROY(mouse_dev->mouse_fun);
[e9f0348]437}
[76fbd9a]438
[60c0573]439int usb_mouse_set_boot_protocol(usb_hid_dev_t *hid_dev)
[e9f0348]440{
[a8c4e871]441 int rc = usb_hid_parse_report_descriptor(
442 &hid_dev->report, USB_MOUSE_BOOT_REPORT_DESCRIPTOR,
[e3c78efc]443 sizeof(USB_MOUSE_BOOT_REPORT_DESCRIPTOR));
[cc29622]444
[e9f0348]445 if (rc != EOK) {
446 usb_log_error("Failed to parse boot report descriptor: %s\n",
447 str_error(rc));
448 return rc;
449 }
[cc29622]450
[c39e9fb]451 rc = usbhid_req_set_protocol(
452 usb_device_get_default_pipe(hid_dev->usb_dev),
[8e10ef4]453 usb_device_get_iface_number(hid_dev->usb_dev),
454 USB_HID_PROTOCOL_BOOT);
[cc29622]455
[e9f0348]456 if (rc != EOK) {
457 usb_log_warning("Failed to set boot protocol to the device: "
458 "%s\n", str_error(rc));
459 return rc;
460 }
[cc29622]461
[e9f0348]462 return EOK;
463}
464
465/**
466 * @}
467 */
Note: See TracBrowser for help on using the repository browser.