source: mainline/uspace/drv/bus/usb/usbhid/mouse/mousedev.c@ bfc5c9dd

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bfc5c9dd was 70922c2, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

Mainline changes.

  • Property mode set to 100644
File size: 13.9 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 = fun->driver_data;
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__, fun->name);
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 fun->name, sess);
136 async_answer_0(icallid, EOK);
137 } else {
138 usb_log_error("Console session to %s already set.\n",
139 fun->name);
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 (fun)->driver_data = NULL; \
252 ddf_fun_destroy((fun)); \
253 } else { \
254 usb_log_error("Could not unbind function `%s', it " \
255 "will not be destroyed.\n", (fun)->name); \
256 } \
257} else (void)0
258/*----------------------------------------------------------------------------*/
259static int usb_mouse_create_function(usb_hid_dev_t *hid_dev, usb_mouse_t *mouse)
260{
261 assert(hid_dev != NULL);
262 assert(mouse != NULL);
263
264 /* Create the exposed function. */
265 usb_log_debug("Creating DDF function %s...\n", HID_MOUSE_FUN_NAME);
266 ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed,
267 HID_MOUSE_FUN_NAME);
268 if (fun == NULL) {
269 usb_log_error("Could not create DDF function node `%s'.\n",
270 HID_MOUSE_FUN_NAME);
271 return ENOMEM;
272 }
273
274 fun->ops = &ops;
275 fun->driver_data = mouse;
276
277 int rc = ddf_fun_bind(fun);
278 if (rc != EOK) {
279 usb_log_error("Could not bind DDF function `%s': %s.\n",
280 fun->name, str_error(rc));
281 fun->driver_data = NULL;
282 ddf_fun_destroy(fun);
283 return rc;
284 }
285
286 usb_log_debug("Adding DDF function `%s' to category %s...\n",
287 fun->name, HID_MOUSE_CATEGORY);
288 rc = ddf_fun_add_to_category(fun, HID_MOUSE_CATEGORY);
289 if (rc != EOK) {
290 usb_log_error(
291 "Could not add DDF function to category %s: %s.\n",
292 HID_MOUSE_CATEGORY, str_error(rc));
293 FUN_UNBIND_DESTROY(fun);
294 return rc;
295 }
296 mouse->mouse_fun = fun;
297 return EOK;
298}
299
300/** Get highest index of a button mentioned in given report.
301 *
302 * @param report HID report.
303 * @param report_id Report id we are interested in.
304 * @return Highest button mentioned in the report.
305 * @retval 1 No button was mentioned.
306 *
307 */
308static size_t usb_mouse_get_highest_button(usb_hid_report_t *report, uint8_t report_id)
309{
310 size_t highest_button = 0;
311
312 usb_hid_report_path_t *path = usb_hid_report_path();
313 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_BUTTON, 0);
314 usb_hid_report_path_set_report_id(path, report_id);
315
316 usb_hid_report_field_t *field = NULL;
317
318 /* Break from within. */
319 while (1) {
320 field = usb_hid_report_get_sibling(
321 report, field, path,
322 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
323 USB_HID_REPORT_TYPE_INPUT);
324 /* No more buttons? */
325 if (field == NULL) {
326 break;
327 }
328
329 size_t current_button = field->usage - field->usage_minimum;
330 if (current_button > highest_button) {
331 highest_button = current_button;
332 }
333 }
334
335 usb_hid_report_path_free(path);
336
337 return highest_button;
338}
339/*----------------------------------------------------------------------------*/
340int usb_mouse_init(usb_hid_dev_t *hid_dev, void **data)
341{
342 usb_log_debug("Initializing HID/Mouse structure...\n");
343
344 if (hid_dev == NULL) {
345 usb_log_error("Failed to init keyboard structure: no structure"
346 " given.\n");
347 return EINVAL;
348 }
349
350 usb_mouse_t *mouse_dev = calloc(1, sizeof(usb_mouse_t));
351 if (mouse_dev == NULL) {
352 usb_log_error("Error while creating USB/HID Mouse device "
353 "structure.\n");
354 return ENOMEM;
355 }
356
357 // FIXME: This may not be optimal since stupid hardware vendor may
358 // use buttons 1, 2, 3 and 6000 and we would allocate array of
359 // 6001*4B and use only 4 items in it.
360 // Since I doubt that hardware producers would do that, I think
361 // that the current solution is good enough.
362 /* Adding 1 because we will be accessing buttons[highest]. */
363 mouse_dev->buttons_count = 1 + usb_mouse_get_highest_button(
364 &hid_dev->report, hid_dev->report_id);
365 mouse_dev->buttons = calloc(mouse_dev->buttons_count, sizeof(int32_t));
366
367 if (mouse_dev->buttons == NULL) {
368 usb_log_error(NAME ": out of memory, giving up on device!\n");
369 free(mouse_dev);
370 return ENOMEM;
371 }
372
373 // TODO: how to know if the device supports the request???
374 usbhid_req_set_idle(&hid_dev->usb_dev->ctrl_pipe,
375 hid_dev->usb_dev->interface_no, IDLE_RATE);
376
377 int rc = usb_mouse_create_function(hid_dev, mouse_dev);
378 if (rc != EOK) {
379 free(mouse_dev->buttons);
380 free(mouse_dev);
381 return rc;
382 }
383
384 /* Save the Mouse device structure into the HID device structure. */
385 *data = mouse_dev;
386
387 return EOK;
388}
389/*----------------------------------------------------------------------------*/
390bool usb_mouse_polling_callback(usb_hid_dev_t *hid_dev, void *data)
391{
392 if (hid_dev == NULL || data == NULL) {
393 usb_log_error(
394 "Missing argument to the mouse polling callback.\n");
395 return false;
396 }
397
398 usb_mouse_t *mouse_dev = data;
399
400 return usb_mouse_process_report(hid_dev, mouse_dev);
401}
402/*----------------------------------------------------------------------------*/
403void usb_mouse_deinit(usb_hid_dev_t *hid_dev, void *data)
404{
405 if (data == NULL)
406 return;
407
408 usb_mouse_t *mouse_dev = data;
409
410 /* Hangup session to the console */
411 if (mouse_dev->mouse_sess != NULL) {
412 const int ret = async_hangup(mouse_dev->mouse_sess);
413 if (ret != EOK)
414 usb_log_warning("Failed to hang up mouse session: "
415 "%p, %s.\n", mouse_dev->mouse_sess, str_error(ret));
416 }
417
418 FUN_UNBIND_DESTROY(mouse_dev->mouse_fun);
419
420 free(mouse_dev->buttons);
421 free(mouse_dev);
422}
423/*----------------------------------------------------------------------------*/
424int usb_mouse_set_boot_protocol(usb_hid_dev_t *hid_dev)
425{
426 int rc = usb_hid_parse_report_descriptor(
427 &hid_dev->report, USB_MOUSE_BOOT_REPORT_DESCRIPTOR,
428 sizeof(USB_MOUSE_BOOT_REPORT_DESCRIPTOR));
429
430 if (rc != EOK) {
431 usb_log_error("Failed to parse boot report descriptor: %s\n",
432 str_error(rc));
433 return rc;
434 }
435
436 rc = usbhid_req_set_protocol(&hid_dev->usb_dev->ctrl_pipe,
437 hid_dev->usb_dev->interface_no, USB_HID_PROTOCOL_BOOT);
438
439 if (rc != EOK) {
440 usb_log_warning("Failed to set boot protocol to the device: "
441 "%s\n", str_error(rc));
442 return rc;
443 }
444
445 return EOK;
446}
447
448/**
449 * @}
450 */
Note: See TracBrowser for help on using the repository browser.