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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d4a829e was d4a829e, checked in by Jakub Jermar <jakub@…>, 8 years ago

Revert "Reformat copyright messages"

This reverts commit 999efa910aa347a786e88adecf88756dfaf9ea65.

There might actually be a semantical difference between a single-line
copyright notice with multiple authors (a collective of authors) and
multiple copyright notices following each other (individual authors
making gradual changes).

  • Property mode set to 100644
File size: 13.2 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 int get_mouse_axis_move_value(uint8_t rid, usb_hid_report_t *report,
150 int32_t usage)
151{
152 int result = 0;
153
154 usb_hid_report_path_t *path = usb_hid_report_path();
155 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_GENERIC_DESKTOP,
156 usage);
157
158 usb_hid_report_path_set_report_id(path, rid);
159
160 usb_hid_report_field_t *field = usb_hid_report_get_sibling(
161 report, NULL, path, USB_HID_PATH_COMPARE_END,
162 USB_HID_REPORT_TYPE_INPUT);
163
164 if (field != NULL) {
165 result = field->value;
166 }
167
168 usb_hid_report_path_free(path);
169
170 return result;
171}
172
173static bool usb_mouse_process_report(usb_hid_dev_t *hid_dev,
174 usb_mouse_t *mouse_dev)
175{
176 assert(mouse_dev != NULL);
177
178 if (mouse_dev->mouse_sess == NULL) {
179 usb_log_warning(NAME " No console session.\n");
180 return true;
181 }
182
183 const int shift_x = get_mouse_axis_move_value(hid_dev->report_id,
184 &hid_dev->report, USB_HIDUT_USAGE_GENERIC_DESKTOP_X);
185 const int shift_y = get_mouse_axis_move_value(hid_dev->report_id,
186 &hid_dev->report, USB_HIDUT_USAGE_GENERIC_DESKTOP_Y);
187 const int wheel = get_mouse_axis_move_value(hid_dev->report_id,
188 &hid_dev->report, USB_HIDUT_USAGE_GENERIC_DESKTOP_WHEEL);
189
190 if (shift_x || shift_y || wheel) {
191 async_exch_t *exch =
192 async_exchange_begin(mouse_dev->mouse_sess);
193 if (exch != NULL) {
194 async_msg_3(exch, MOUSEEV_MOVE_EVENT,
195 shift_x, shift_y, wheel);
196 async_exchange_end(exch);
197 }
198 }
199
200 /* Buttons */
201 usb_hid_report_path_t *path = usb_hid_report_path();
202 if (path == NULL) {
203 usb_log_warning("Failed to create USB HID report path.\n");
204 return true;
205 }
206 int ret =
207 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_BUTTON, 0);
208 if (ret != EOK) {
209 usb_hid_report_path_free(path);
210 usb_log_warning("Failed to add buttons to report path.\n");
211 return true;
212 }
213 usb_hid_report_path_set_report_id(path, hid_dev->report_id);
214
215 usb_hid_report_field_t *field = usb_hid_report_get_sibling(
216 &hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END
217 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, USB_HID_REPORT_TYPE_INPUT);
218
219 while (field != NULL) {
220 usb_log_debug2(NAME " VALUE(%X) USAGE(%X)\n", field->value,
221 field->usage);
222 assert(field->usage > field->usage_minimum);
223 const unsigned index = field->usage - field->usage_minimum;
224 assert(index < mouse_dev->buttons_count);
225
226 if (mouse_dev->buttons[index] != field->value) {
227 async_exch_t *exch =
228 async_exchange_begin(mouse_dev->mouse_sess);
229 if (exch != NULL) {
230 async_req_2_0(exch, MOUSEEV_BUTTON_EVENT,
231 field->usage, (field->value != 0) ? 1 : 0);
232 async_exchange_end(exch);
233 mouse_dev->buttons[index] = field->value;
234 }
235 }
236
237 field = usb_hid_report_get_sibling(
238 &hid_dev->report, field, path, USB_HID_PATH_COMPARE_END
239 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
240 USB_HID_REPORT_TYPE_INPUT);
241 }
242
243 usb_hid_report_path_free(path);
244
245 return true;
246}
247
248#define FUN_UNBIND_DESTROY(fun) \
249if (fun) { \
250 if (ddf_fun_unbind((fun)) == EOK) { \
251 ddf_fun_destroy((fun)); \
252 } else { \
253 usb_log_error("Could not unbind function `%s', it " \
254 "will not be destroyed.\n", ddf_fun_get_name(fun)); \
255 } \
256} else (void) 0
257
258/** Get highest index of a button mentioned in given report.
259 *
260 * @param report HID report.
261 * @param report_id Report id we are interested in.
262 * @return Highest button mentioned in the report.
263 * @retval 1 No button was mentioned.
264 *
265 */
266static size_t usb_mouse_get_highest_button(usb_hid_report_t *report, uint8_t report_id)
267{
268 size_t highest_button = 0;
269
270 usb_hid_report_path_t *path = usb_hid_report_path();
271 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_BUTTON, 0);
272 usb_hid_report_path_set_report_id(path, report_id);
273
274 usb_hid_report_field_t *field = NULL;
275
276 /* Break from within. */
277 while (1) {
278 field = usb_hid_report_get_sibling(
279 report, field, path,
280 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
281 USB_HID_REPORT_TYPE_INPUT);
282 /* No more buttons? */
283 if (field == NULL) {
284 break;
285 }
286
287 size_t current_button = field->usage - field->usage_minimum;
288 if (current_button > highest_button) {
289 highest_button = current_button;
290 }
291 }
292
293 usb_hid_report_path_free(path);
294
295 return highest_button;
296}
297
298static int mouse_dev_init(usb_mouse_t *mouse_dev, usb_hid_dev_t *hid_dev)
299{
300 // FIXME: This may not be optimal since stupid hardware vendor may
301 // use buttons 1, 2, 3 and 6000 and we would allocate array of
302 // 6001*4B and use only 4 items in it.
303 // Since I doubt that hardware producers would do that, I think
304 // that the current solution is good enough.
305 /* Adding 1 because we will be accessing buttons[highest]. */
306 mouse_dev->buttons_count = 1 + usb_mouse_get_highest_button(
307 &hid_dev->report, hid_dev->report_id);
308 mouse_dev->buttons = calloc(mouse_dev->buttons_count, sizeof(int32_t));
309
310 if (mouse_dev->buttons == NULL) {
311 usb_log_error(NAME ": out of memory, giving up on device!\n");
312 free(mouse_dev);
313 return ENOMEM;
314 }
315
316 // TODO: how to know if the device supports the request???
317 usbhid_req_set_idle(usb_device_get_default_pipe(hid_dev->usb_dev),
318 usb_device_get_iface_number(hid_dev->usb_dev), IDLE_RATE);
319 return EOK;
320}
321
322int usb_mouse_init(usb_hid_dev_t *hid_dev, void **data)
323{
324 usb_log_debug("Initializing HID/Mouse structure...\n");
325
326 if (hid_dev == NULL) {
327 usb_log_error("Failed to init mouse structure: no structure"
328 " given.\n");
329 return EINVAL;
330 }
331
332 /* Create the exposed function. */
333 usb_log_debug("Creating DDF function %s...\n", HID_MOUSE_FUN_NAME);
334 ddf_fun_t *fun = usb_device_ddf_fun_create(hid_dev->usb_dev,
335 fun_exposed, HID_MOUSE_FUN_NAME);
336 if (fun == NULL) {
337 usb_log_error("Could not create DDF function node `%s'.\n",
338 HID_MOUSE_FUN_NAME);
339 return ENOMEM;
340 }
341
342 usb_mouse_t *mouse_dev = ddf_fun_data_alloc(fun, sizeof(usb_mouse_t));
343 if (mouse_dev == NULL) {
344 usb_log_error("Failed to alloc HID mouse device structure.\n");
345 ddf_fun_destroy(fun);
346 return ENOMEM;
347 }
348
349 int ret = mouse_dev_init(mouse_dev, hid_dev);
350 if (ret != EOK) {
351 usb_log_error("Failed to init HID mouse device structure.\n");
352 return ret;
353 }
354
355 ddf_fun_set_ops(fun, &ops);
356
357 ret = ddf_fun_bind(fun);
358 if (ret != EOK) {
359 usb_log_error("Could not bind DDF function `%s': %s.\n",
360 ddf_fun_get_name(fun), str_error(ret));
361 ddf_fun_destroy(fun);
362 return ret;
363 }
364
365 usb_log_debug("Adding DDF function `%s' to category %s...\n",
366 ddf_fun_get_name(fun), HID_MOUSE_CATEGORY);
367 ret = ddf_fun_add_to_category(fun, HID_MOUSE_CATEGORY);
368 if (ret != EOK) {
369 usb_log_error(
370 "Could not add DDF function to category %s: %s.\n",
371 HID_MOUSE_CATEGORY, str_error(ret));
372 FUN_UNBIND_DESTROY(fun);
373 return ret;
374 }
375 mouse_dev->mouse_fun = fun;
376
377 /* Save the Mouse device structure into the HID device structure. */
378 *data = mouse_dev;
379
380 return EOK;
381}
382
383bool usb_mouse_polling_callback(usb_hid_dev_t *hid_dev, void *data)
384{
385 if (hid_dev == NULL || data == NULL) {
386 usb_log_error(
387 "Missing argument to the mouse polling callback.\n");
388 return false;
389 }
390
391 usb_mouse_t *mouse_dev = data;
392
393 return usb_mouse_process_report(hid_dev, mouse_dev);
394}
395
396void usb_mouse_deinit(usb_hid_dev_t *hid_dev, void *data)
397{
398 if (data == NULL)
399 return;
400
401 usb_mouse_t *mouse_dev = data;
402
403 /* Hangup session to the console */
404 if (mouse_dev->mouse_sess != NULL) {
405 const int ret = async_hangup(mouse_dev->mouse_sess);
406 if (ret != EOK)
407 usb_log_warning("Failed to hang up mouse session: "
408 "%p, %s.\n", mouse_dev->mouse_sess, str_error(ret));
409 }
410
411 free(mouse_dev->buttons);
412 FUN_UNBIND_DESTROY(mouse_dev->mouse_fun);
413}
414
415int usb_mouse_set_boot_protocol(usb_hid_dev_t *hid_dev)
416{
417 int rc = usb_hid_parse_report_descriptor(
418 &hid_dev->report, USB_MOUSE_BOOT_REPORT_DESCRIPTOR,
419 sizeof(USB_MOUSE_BOOT_REPORT_DESCRIPTOR));
420
421 if (rc != EOK) {
422 usb_log_error("Failed to parse boot report descriptor: %s\n",
423 str_error(rc));
424 return rc;
425 }
426
427 rc = usbhid_req_set_protocol(
428 usb_device_get_default_pipe(hid_dev->usb_dev),
429 usb_device_get_iface_number(hid_dev->usb_dev),
430 USB_HID_PROTOCOL_BOOT);
431
432 if (rc != EOK) {
433 usb_log_warning("Failed to set boot protocol to the device: "
434 "%s\n", str_error(rc));
435 return rc;
436 }
437
438 return EOK;
439}
440
441/**
442 * @}
443 */
Note: See TracBrowser for help on using the repository browser.