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
RevLine 
[e9f0348]1/*
[d4a829e]2 * Copyright (c) 2011 Lubos Slovak, Vojtech Horky
[e9f0348]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>
[1875a0c]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
[a3a2fdb]149static int get_mouse_axis_move_value(uint8_t rid, usb_hid_report_t *report,
150 int32_t usage)
[e9f0348]151{
[a3a2fdb]152 int result = 0;
[30710035]153
154 usb_hid_report_path_t *path = usb_hid_report_path();
[a3a2fdb]155 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_GENERIC_DESKTOP,
156 usage);
[e9f0348]157
[a3a2fdb]158 usb_hid_report_path_set_report_id(path, rid);
[e9f0348]159
[30710035]160 usb_hid_report_field_t *field = usb_hid_report_get_sibling(
[a3a2fdb]161 report, NULL, path, USB_HID_PATH_COMPARE_END,
[30710035]162 USB_HID_REPORT_TYPE_INPUT);
[e9f0348]163
[30710035]164 if (field != NULL) {
[a3a2fdb]165 result = field->value;
[e9f0348]166 }
[30710035]167
168 usb_hid_report_path_free(path);
169
[a3a2fdb]170 return result;
171}
[5cc9eba]172
[a3a2fdb]173static bool usb_mouse_process_report(usb_hid_dev_t *hid_dev,
174 usb_mouse_t *mouse_dev)
175{
176 assert(mouse_dev != NULL);
[cc29622]177
[5da7199]178 if (mouse_dev->mouse_sess == NULL) {
179 usb_log_warning(NAME " No console session.\n");
[a3a2fdb]180 return true;
[e9f0348]181 }
[30710035]182
[e3e0953]183 const int shift_x = get_mouse_axis_move_value(hid_dev->report_id,
[a8c4e871]184 &hid_dev->report, USB_HIDUT_USAGE_GENERIC_DESKTOP_X);
[e3e0953]185 const int shift_y = get_mouse_axis_move_value(hid_dev->report_id,
[a8c4e871]186 &hid_dev->report, USB_HIDUT_USAGE_GENERIC_DESKTOP_Y);
[e3e0953]187 const int wheel = get_mouse_axis_move_value(hid_dev->report_id,
[a8c4e871]188 &hid_dev->report, USB_HIDUT_USAGE_GENERIC_DESKTOP_WHEEL);
[a3a2fdb]189
[edb3cf2]190 if (shift_x || shift_y || wheel) {
[a8c4e871]191 async_exch_t *exch =
192 async_exchange_begin(mouse_dev->mouse_sess);
[e3e0953]193 if (exch != NULL) {
[edb3cf2]194 async_msg_3(exch, MOUSEEV_MOVE_EVENT,
195 shift_x, shift_y, wheel);
[e3e0953]196 async_exchange_end(exch);
197 }
[e9f0348]198 }
[cc29622]199
[e3e0953]200 /* Buttons */
[a3a2fdb]201 usb_hid_report_path_t *path = usb_hid_report_path();
[e3e0953]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 }
[65c3794]213 usb_hid_report_path_set_report_id(path, hid_dev->report_id);
[cc29622]214
[a3a2fdb]215 usb_hid_report_field_t *field = usb_hid_report_get_sibling(
[a8c4e871]216 &hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END
217 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, USB_HID_REPORT_TYPE_INPUT);
[30710035]218
[310c4df]219 while (field != NULL) {
[ae5f77d5]220 usb_log_debug2(NAME " VALUE(%X) USAGE(%X)\n", field->value,
[30710035]221 field->usage);
[5723ae49]222 assert(field->usage > field->usage_minimum);
223 const unsigned index = field->usage - field->usage_minimum;
224 assert(index < mouse_dev->buttons_count);
[e3e0953]225
[295f658]226 if (mouse_dev->buttons[index] != field->value) {
[5da7199]227 async_exch_t *exch =
228 async_exchange_begin(mouse_dev->mouse_sess);
[e3e0953]229 if (exch != NULL) {
230 async_req_2_0(exch, MOUSEEV_BUTTON_EVENT,
[295f658]231 field->usage, (field->value != 0) ? 1 : 0);
[e3e0953]232 async_exchange_end(exch);
233 mouse_dev->buttons[index] = field->value;
234 }
[1875a0c]235 }
[a8c4e871]236
[30710035]237 field = usb_hid_report_get_sibling(
[a8c4e871]238 &hid_dev->report, field, path, USB_HID_PATH_COMPARE_END
239 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
[30710035]240 USB_HID_REPORT_TYPE_INPUT);
[e9f0348]241 }
[cc29622]242
[30710035]243 usb_hid_report_path_free(path);
[e9f0348]244
245 return true;
246}
[76fbd9a]247
[0f12c17]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 " \
[56fd7cf]254 "will not be destroyed.\n", ddf_fun_get_name(fun)); \
[0f12c17]255 } \
[58563585]256} else (void) 0
[76fbd9a]257
[4093b14]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}
[76fbd9a]297
[38b4a25]298static int mouse_dev_init(usb_mouse_t *mouse_dev, usb_hid_dev_t *hid_dev)
[e9f0348]299{
[4093b14]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]. */
[a8c4e871]306 mouse_dev->buttons_count = 1 + usb_mouse_get_highest_button(
307 &hid_dev->report, hid_dev->report_id);
[4093b14]308 mouse_dev->buttons = calloc(mouse_dev->buttons_count, sizeof(int32_t));
[cc29622]309
[30710035]310 if (mouse_dev->buttons == NULL) {
[4093b14]311 usb_log_error(NAME ": out of memory, giving up on device!\n");
[30710035]312 free(mouse_dev);
313 return ENOMEM;
314 }
[4093b14]315
[e9f0348]316 // TODO: how to know if the device supports the request???
[c39e9fb]317 usbhid_req_set_idle(usb_device_get_default_pipe(hid_dev->usb_dev),
[8e10ef4]318 usb_device_get_iface_number(hid_dev->usb_dev), IDLE_RATE);
[38b4a25]319 return EOK;
320}
[cc29622]321
[38b4a25]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) {
[58563585]327 usb_log_error("Failed to init mouse structure: no structure"
[38b4a25]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);
[6785b538]334 ddf_fun_t *fun = usb_device_ddf_fun_create(hid_dev->usb_dev,
335 fun_exposed, HID_MOUSE_FUN_NAME);
[38b4a25]336 if (fun == NULL) {
337 usb_log_error("Could not create DDF function node `%s'.\n",
338 HID_MOUSE_FUN_NAME);
339 return ENOMEM;
[31cfee16]340 }
[cc29622]341
[38b4a25]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
[5723ae49]377 /* Save the Mouse device structure into the HID device structure. */
378 *data = mouse_dev;
379
[e9f0348]380 return EOK;
381}
[76fbd9a]382
[4d3c13e]383bool usb_mouse_polling_callback(usb_hid_dev_t *hid_dev, void *data)
[e9f0348]384{
[65b458c4]385 if (hid_dev == NULL || data == NULL) {
[e3e0953]386 usb_log_error(
387 "Missing argument to the mouse polling callback.\n");
[e9f0348]388 return false;
389 }
[cc29622]390
[5723ae49]391 usb_mouse_t *mouse_dev = data;
392
[4d3c13e]393 return usb_mouse_process_report(hid_dev, mouse_dev);
[e9f0348]394}
[76fbd9a]395
[65b458c4]396void usb_mouse_deinit(usb_hid_dev_t *hid_dev, void *data)
[e9f0348]397{
[5723ae49]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);
[38b4a25]412 FUN_UNBIND_DESTROY(mouse_dev->mouse_fun);
[e9f0348]413}
[76fbd9a]414
[60c0573]415int usb_mouse_set_boot_protocol(usb_hid_dev_t *hid_dev)
[e9f0348]416{
[a8c4e871]417 int rc = usb_hid_parse_report_descriptor(
418 &hid_dev->report, USB_MOUSE_BOOT_REPORT_DESCRIPTOR,
[e3c78efc]419 sizeof(USB_MOUSE_BOOT_REPORT_DESCRIPTOR));
[cc29622]420
[e9f0348]421 if (rc != EOK) {
422 usb_log_error("Failed to parse boot report descriptor: %s\n",
423 str_error(rc));
424 return rc;
425 }
[cc29622]426
[c39e9fb]427 rc = usbhid_req_set_protocol(
428 usb_device_get_default_pipe(hid_dev->usb_dev),
[8e10ef4]429 usb_device_get_iface_number(hid_dev->usb_dev),
430 USB_HID_PROTOCOL_BOOT);
[cc29622]431
[e9f0348]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 }
[cc29622]437
[e9f0348]438 return EOK;
439}
440
441/**
442 * @}
443 */
Note: See TracBrowser for help on using the repository browser.