source: mainline/uspace/drv/hid/usbhid/mouse/mousedev.c@ 49e62998

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

usbhid: join polling fibrils in device_remove()

  • Property mode set to 100644
File size: 14.1 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/hid/hid.h>
40#include <usb/hid/request.h>
41#include <usb/hid/usages/core.h>
42#include <errno.h>
43#include <async.h>
44#include <str_error.h>
45#include <ipc/mouseev.h>
46#include <io/console.h>
47
48#include <ipc/kbdev.h>
49#include <io/keycode.h>
50
51#include "mousedev.h"
52#include "../usbhid.h"
53
54#define NAME "mouse"
55
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 };
59
60
61const usb_endpoint_description_t usb_hid_mouse_poll_endpoint_description = {
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";
71const char *HID_MOUSE_CATEGORY = "mouse";
72
73/** Default idle rate for mouses. */
74static const uint8_t IDLE_RATE = 0;
75
76
77static const uint8_t USB_MOUSE_BOOT_REPORT_DESCRIPTOR[] = {
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
106
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{
117 usb_mouse_t *mouse_dev = ddf_fun_data_get(fun);
118
119 if (mouse_dev == NULL) {
120 usb_log_debug("%s: Missing parameters.\n", __FUNCTION__);
121 async_answer_0(icallid, EINVAL);
122 return;
123 }
124
125 usb_log_debug("%s: fun->name: %s\n", __FUNCTION__, ddf_fun_get_name(fun));
126 usb_log_debug("%s: mouse_sess: %p\n",
127 __FUNCTION__, mouse_dev->mouse_sess);
128
129 async_sess_t *sess =
130 async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
131 if (sess != NULL) {
132 if (mouse_dev->mouse_sess == NULL) {
133 mouse_dev->mouse_sess = sess;
134 usb_log_debug("Console session to %s set ok (%p).\n",
135 ddf_fun_get_name(fun), sess);
136 async_answer_0(icallid, EOK);
137 } else {
138 usb_log_error("Console session to %s already set.\n",
139 ddf_fun_get_name(fun));
140 async_answer_0(icallid, ELIMIT);
141 async_hangup(sess);
142 }
143 } else {
144 usb_log_debug("%s: Invalid function.\n", __FUNCTION__);
145 async_answer_0(icallid, EINVAL);
146 }
147}
148
149static const usb_hid_report_field_t *get_mouse_axis_move_field(uint8_t rid, usb_hid_report_t *report,
150 int32_t usage)
151{
152 usb_hid_report_path_t *path = usb_hid_report_path();
153 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_GENERIC_DESKTOP,
154 usage);
155
156 usb_hid_report_path_set_report_id(path, rid);
157
158 const usb_hid_report_field_t *field = usb_hid_report_get_sibling(
159 report, NULL, path, USB_HID_PATH_COMPARE_END,
160 USB_HID_REPORT_TYPE_INPUT);
161
162 usb_hid_report_path_free(path);
163
164 return field;
165}
166
167static void usb_mouse_process_report(usb_hid_dev_t *hid_dev,
168 usb_mouse_t *mouse_dev)
169{
170 assert(mouse_dev != NULL);
171
172 if (mouse_dev->mouse_sess == NULL) {
173 usb_log_warning(NAME " No console session.\n");
174 return;
175 }
176
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.");
193 return;
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
213
214 if (shift_x || shift_y || shift_z) {
215 async_exch_t *exch =
216 async_exchange_begin(mouse_dev->mouse_sess);
217 if (exch != NULL) {
218 async_msg_3(exch, MOUSEEV_MOVE_EVENT,
219 shift_x, shift_y, shift_z);
220 async_exchange_end(exch);
221 }
222 }
223
224 /* Buttons */
225 usb_hid_report_path_t *path = usb_hid_report_path();
226 if (path == NULL) {
227 usb_log_warning("Failed to create USB HID report path.\n");
228 return;
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");
235 return;
236 }
237 usb_hid_report_path_set_report_id(path, hid_dev->report_id);
238
239 usb_hid_report_field_t *field = usb_hid_report_get_sibling(
240 &hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END
241 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, USB_HID_REPORT_TYPE_INPUT);
242
243 while (field != NULL) {
244 usb_log_debug2(NAME " VALUE(%X) USAGE(%X)\n", field->value,
245 field->usage);
246 assert(field->usage > field->usage_minimum);
247 const unsigned index = field->usage - field->usage_minimum;
248 assert(index < mouse_dev->buttons_count);
249
250 if (mouse_dev->buttons[index] != field->value) {
251 async_exch_t *exch =
252 async_exchange_begin(mouse_dev->mouse_sess);
253 if (exch != NULL) {
254 async_req_2_0(exch, MOUSEEV_BUTTON_EVENT,
255 field->usage, (field->value != 0) ? 1 : 0);
256 async_exchange_end(exch);
257 mouse_dev->buttons[index] = field->value;
258 }
259 }
260
261 field = usb_hid_report_get_sibling(
262 &hid_dev->report, field, path, USB_HID_PATH_COMPARE_END
263 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
264 USB_HID_REPORT_TYPE_INPUT);
265 }
266
267 usb_hid_report_path_free(path);
268}
269
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 " \
276 "will not be destroyed.\n", ddf_fun_get_name(fun)); \
277 } \
278} else (void) 0
279
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}
319
320static int mouse_dev_init(usb_mouse_t *mouse_dev, usb_hid_dev_t *hid_dev)
321{
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]. */
328 mouse_dev->buttons_count = 1 + usb_mouse_get_highest_button(
329 &hid_dev->report, hid_dev->report_id);
330 mouse_dev->buttons = calloc(mouse_dev->buttons_count, sizeof(int32_t));
331
332 if (mouse_dev->buttons == NULL) {
333 usb_log_error(NAME ": out of memory, giving up on device!\n");
334 free(mouse_dev);
335 return ENOMEM;
336 }
337
338 // TODO: how to know if the device supports the request???
339 usbhid_req_set_idle(usb_device_get_default_pipe(hid_dev->usb_dev),
340 usb_device_get_iface_number(hid_dev->usb_dev), IDLE_RATE);
341 return EOK;
342}
343
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) {
349 usb_log_error("Failed to init mouse structure: no structure"
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);
356 ddf_fun_t *fun = usb_device_ddf_fun_create(hid_dev->usb_dev,
357 fun_exposed, HID_MOUSE_FUN_NAME);
358 if (fun == NULL) {
359 usb_log_error("Could not create DDF function node `%s'.\n",
360 HID_MOUSE_FUN_NAME);
361 return ENOMEM;
362 }
363
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
399 /* Save the Mouse device structure into the HID device structure. */
400 *data = mouse_dev;
401
402 return EOK;
403}
404
405bool usb_mouse_polling_callback(usb_hid_dev_t *hid_dev, void *data)
406{
407 if (hid_dev == NULL || data == NULL) {
408 usb_log_error(
409 "Missing argument to the mouse polling callback.\n");
410 return false;
411 }
412
413 usb_mouse_t *mouse_dev = data;
414 usb_mouse_process_report(hid_dev, mouse_dev);
415
416 /* Continue polling until the device is about to be removed. */
417 return !hid_dev->will_deinit;
418}
419
420void usb_mouse_deinit(usb_hid_dev_t *hid_dev, void *data)
421{
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);
436 FUN_UNBIND_DESTROY(mouse_dev->mouse_fun);
437}
438
439int usb_mouse_set_boot_protocol(usb_hid_dev_t *hid_dev)
440{
441 int rc = usb_hid_parse_report_descriptor(
442 &hid_dev->report, USB_MOUSE_BOOT_REPORT_DESCRIPTOR,
443 sizeof(USB_MOUSE_BOOT_REPORT_DESCRIPTOR));
444
445 if (rc != EOK) {
446 usb_log_error("Failed to parse boot report descriptor: %s\n",
447 str_error(rc));
448 return rc;
449 }
450
451 rc = usbhid_req_set_protocol(
452 usb_device_get_default_pipe(hid_dev->usb_dev),
453 usb_device_get_iface_number(hid_dev->usb_dev),
454 USB_HID_PROTOCOL_BOOT);
455
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 }
461
462 return EOK;
463}
464
465/**
466 * @}
467 */
Note: See TracBrowser for help on using the repository browser.