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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ddab093 was 56fd7cf, checked in by Jiri Svoboda <jiri@…>, 13 years ago

Make ddf_dev_t and ddf_fun_t opaque. This further tighthens the DDF interface.

  • 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/* XXX Fix this */
38#define _DDF_DATA_IMPLANT
39
40#include <usb/debug.h>
41#include <usb/classes/classes.h>
42#include <usb/hid/hid.h>
43#include <usb/hid/request.h>
44#include <usb/hid/usages/core.h>
45#include <errno.h>
46#include <async.h>
47#include <str_error.h>
48#include <ipc/mouseev.h>
49#include <io/console.h>
50
51#include <ipc/kbdev.h>
52#include <io/keycode.h>
53
54#include "mousedev.h"
55#include "../usbhid.h"
56
57#define NAME "mouse"
58
59static void default_connection_handler(ddf_fun_t *, ipc_callid_t, ipc_call_t *);
60
61static ddf_dev_ops_t ops = { .default_handler = default_connection_handler };
62
63
64const usb_endpoint_description_t usb_hid_mouse_poll_endpoint_description = {
65 .transfer_type = USB_TRANSFER_INTERRUPT,
66 .direction = USB_DIRECTION_IN,
67 .interface_class = USB_CLASS_HID,
68 .interface_subclass = USB_HID_SUBCLASS_BOOT,
69 .interface_protocol = USB_HID_PROTOCOL_MOUSE,
70 .flags = 0
71};
72
73const char *HID_MOUSE_FUN_NAME = "mouse";
74const char *HID_MOUSE_CATEGORY = "mouse";
75
76/** Default idle rate for mouses. */
77static const uint8_t IDLE_RATE = 0;
78
79
80static const uint8_t USB_MOUSE_BOOT_REPORT_DESCRIPTOR[] = {
81 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
82 0x09, 0x02, // USAGE (Mouse)
83 0xa1, 0x01, // COLLECTION (Application)
84 0x09, 0x01, // USAGE (Pointer)
85 0xa1, 0x00, // COLLECTION (Physical)
86 0x95, 0x03, // REPORT_COUNT (3)
87 0x75, 0x01, // REPORT_SIZE (1)
88 0x05, 0x09, // USAGE_PAGE (Button)
89 0x19, 0x01, // USAGE_MINIMUM (Button 1)
90 0x29, 0x03, // USAGE_MAXIMUM (Button 3)
91 0x15, 0x00, // LOGICAL_MINIMUM (0)
92 0x25, 0x01, // LOGICAL_MAXIMUM (1)
93 0x81, 0x02, // INPUT (Data,Var,Abs)
94 0x95, 0x01, // REPORT_COUNT (1)
95 0x75, 0x05, // REPORT_SIZE (5)
96 0x81, 0x01, // INPUT (Cnst)
97 0x75, 0x08, // REPORT_SIZE (8)
98 0x95, 0x02, // REPORT_COUNT (2)
99 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
100 0x09, 0x30, // USAGE (X)
101 0x09, 0x31, // USAGE (Y)
102 0x15, 0x81, // LOGICAL_MINIMUM (-127)
103 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
104 0x81, 0x06, // INPUT (Data,Var,Rel)
105 0xc0, // END_COLLECTION
106 0xc0 // END_COLLECTION
107};
108
109
110
111/** Default handler for IPC methods not handled by DDF.
112 *
113 * @param fun Device function handling the call.
114 * @param icallid Call id.
115 * @param icall Call data.
116 */
117static void default_connection_handler(ddf_fun_t *fun,
118 ipc_callid_t icallid, ipc_call_t *icall)
119{
120 usb_mouse_t *mouse_dev = ddf_fun_data_get(fun);
121
122 if (mouse_dev == NULL) {
123 usb_log_debug("%s: Missing parameters.\n", __FUNCTION__);
124 async_answer_0(icallid, EINVAL);
125 return;
126 }
127
128 usb_log_debug("%s: fun->name: %s\n", __FUNCTION__, ddf_fun_get_name(fun));
129 usb_log_debug("%s: mouse_sess: %p\n",
130 __FUNCTION__, mouse_dev->mouse_sess);
131
132 async_sess_t *sess =
133 async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
134 if (sess != NULL) {
135 if (mouse_dev->mouse_sess == NULL) {
136 mouse_dev->mouse_sess = sess;
137 usb_log_debug("Console session to %s set ok (%p).\n",
138 ddf_fun_get_name(fun), sess);
139 async_answer_0(icallid, EOK);
140 } else {
141 usb_log_error("Console session to %s already set.\n",
142 ddf_fun_get_name(fun));
143 async_answer_0(icallid, ELIMIT);
144 async_hangup(sess);
145 }
146 } else {
147 usb_log_debug("%s: Invalid function.\n", __FUNCTION__);
148 async_answer_0(icallid, EINVAL);
149 }
150}
151
152static int get_mouse_axis_move_value(uint8_t rid, usb_hid_report_t *report,
153 int32_t usage)
154{
155 int result = 0;
156
157 usb_hid_report_path_t *path = usb_hid_report_path();
158 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_GENERIC_DESKTOP,
159 usage);
160
161 usb_hid_report_path_set_report_id(path, rid);
162
163 usb_hid_report_field_t *field = usb_hid_report_get_sibling(
164 report, NULL, path, USB_HID_PATH_COMPARE_END,
165 USB_HID_REPORT_TYPE_INPUT);
166
167 if (field != NULL) {
168 result = field->value;
169 }
170
171 usb_hid_report_path_free(path);
172
173 return result;
174}
175
176static bool usb_mouse_process_report(usb_hid_dev_t *hid_dev,
177 usb_mouse_t *mouse_dev)
178{
179 assert(mouse_dev != NULL);
180
181 if (mouse_dev->mouse_sess == NULL) {
182 usb_log_warning(NAME " No console session.\n");
183 return true;
184 }
185
186 const int shift_x = get_mouse_axis_move_value(hid_dev->report_id,
187 &hid_dev->report, USB_HIDUT_USAGE_GENERIC_DESKTOP_X);
188 const int shift_y = get_mouse_axis_move_value(hid_dev->report_id,
189 &hid_dev->report, USB_HIDUT_USAGE_GENERIC_DESKTOP_Y);
190 const int wheel = get_mouse_axis_move_value(hid_dev->report_id,
191 &hid_dev->report, USB_HIDUT_USAGE_GENERIC_DESKTOP_WHEEL);
192
193 if (shift_x || shift_y || wheel) {
194 async_exch_t *exch =
195 async_exchange_begin(mouse_dev->mouse_sess);
196 if (exch != NULL) {
197 async_msg_3(exch, MOUSEEV_MOVE_EVENT,
198 shift_x, shift_y, wheel);
199 async_exchange_end(exch);
200 }
201 }
202
203 /* Buttons */
204 usb_hid_report_path_t *path = usb_hid_report_path();
205 if (path == NULL) {
206 usb_log_warning("Failed to create USB HID report path.\n");
207 return true;
208 }
209 int ret =
210 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_BUTTON, 0);
211 if (ret != EOK) {
212 usb_hid_report_path_free(path);
213 usb_log_warning("Failed to add buttons to report path.\n");
214 return true;
215 }
216 usb_hid_report_path_set_report_id(path, hid_dev->report_id);
217
218 usb_hid_report_field_t *field = usb_hid_report_get_sibling(
219 &hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END
220 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, USB_HID_REPORT_TYPE_INPUT);
221
222 while (field != NULL) {
223 usb_log_debug2(NAME " VALUE(%X) USAGE(%X)\n", field->value,
224 field->usage);
225 assert(field->usage > field->usage_minimum);
226 const unsigned index = field->usage - field->usage_minimum;
227 assert(index < mouse_dev->buttons_count);
228
229 if (mouse_dev->buttons[index] != field->value) {
230 async_exch_t *exch =
231 async_exchange_begin(mouse_dev->mouse_sess);
232 if (exch != NULL) {
233 async_req_2_0(exch, MOUSEEV_BUTTON_EVENT,
234 field->usage, (field->value != 0) ? 1 : 0);
235 async_exchange_end(exch);
236 mouse_dev->buttons[index] = field->value;
237 }
238 }
239
240 field = usb_hid_report_get_sibling(
241 &hid_dev->report, field, path, USB_HID_PATH_COMPARE_END
242 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
243 USB_HID_REPORT_TYPE_INPUT);
244 }
245
246 usb_hid_report_path_free(path);
247
248 return true;
249}
250
251#define FUN_UNBIND_DESTROY(fun) \
252if (fun) { \
253 if (ddf_fun_unbind((fun)) == EOK) { \
254 ddf_fun_destroy((fun)); \
255 } else { \
256 usb_log_error("Could not unbind function `%s', it " \
257 "will not be destroyed.\n", ddf_fun_get_name(fun)); \
258 } \
259} else (void)0
260
261static int usb_mouse_create_function(usb_hid_dev_t *hid_dev, usb_mouse_t *mouse)
262{
263 assert(hid_dev != NULL);
264 assert(mouse != NULL);
265
266 /* Create the exposed function. */
267 usb_log_debug("Creating DDF function %s...\n", HID_MOUSE_FUN_NAME);
268 ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed,
269 HID_MOUSE_FUN_NAME);
270 if (fun == NULL) {
271 usb_log_error("Could not create DDF function node `%s'.\n",
272 HID_MOUSE_FUN_NAME);
273 return ENOMEM;
274 }
275
276 ddf_fun_set_ops(fun, &ops);
277 ddf_fun_data_implant(fun, mouse);
278
279 int rc = ddf_fun_bind(fun);
280 if (rc != EOK) {
281 usb_log_error("Could not bind DDF function `%s': %s.\n",
282 ddf_fun_get_name(fun), str_error(rc));
283 ddf_fun_destroy(fun);
284 return rc;
285 }
286
287 usb_log_debug("Adding DDF function `%s' to category %s...\n",
288 ddf_fun_get_name(fun), HID_MOUSE_CATEGORY);
289 rc = ddf_fun_add_to_category(fun, HID_MOUSE_CATEGORY);
290 if (rc != EOK) {
291 usb_log_error(
292 "Could not add DDF function to category %s: %s.\n",
293 HID_MOUSE_CATEGORY, str_error(rc));
294 FUN_UNBIND_DESTROY(fun);
295 return rc;
296 }
297 mouse->mouse_fun = fun;
298 return EOK;
299}
300
301/** Get highest index of a button mentioned in given report.
302 *
303 * @param report HID report.
304 * @param report_id Report id we are interested in.
305 * @return Highest button mentioned in the report.
306 * @retval 1 No button was mentioned.
307 *
308 */
309static size_t usb_mouse_get_highest_button(usb_hid_report_t *report, uint8_t report_id)
310{
311 size_t highest_button = 0;
312
313 usb_hid_report_path_t *path = usb_hid_report_path();
314 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_BUTTON, 0);
315 usb_hid_report_path_set_report_id(path, report_id);
316
317 usb_hid_report_field_t *field = NULL;
318
319 /* Break from within. */
320 while (1) {
321 field = usb_hid_report_get_sibling(
322 report, field, path,
323 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
324 USB_HID_REPORT_TYPE_INPUT);
325 /* No more buttons? */
326 if (field == NULL) {
327 break;
328 }
329
330 size_t current_button = field->usage - field->usage_minimum;
331 if (current_button > highest_button) {
332 highest_button = current_button;
333 }
334 }
335
336 usb_hid_report_path_free(path);
337
338 return highest_button;
339}
340
341int usb_mouse_init(usb_hid_dev_t *hid_dev, void **data)
342{
343 usb_log_debug("Initializing HID/Mouse structure...\n");
344
345 if (hid_dev == NULL) {
346 usb_log_error("Failed to init keyboard structure: no structure"
347 " given.\n");
348 return EINVAL;
349 }
350
351 usb_mouse_t *mouse_dev = calloc(1, sizeof(usb_mouse_t));
352 if (mouse_dev == NULL) {
353 usb_log_error("Error while creating USB/HID Mouse device "
354 "structure.\n");
355 return ENOMEM;
356 }
357
358 // FIXME: This may not be optimal since stupid hardware vendor may
359 // use buttons 1, 2, 3 and 6000 and we would allocate array of
360 // 6001*4B and use only 4 items in it.
361 // Since I doubt that hardware producers would do that, I think
362 // that the current solution is good enough.
363 /* Adding 1 because we will be accessing buttons[highest]. */
364 mouse_dev->buttons_count = 1 + usb_mouse_get_highest_button(
365 &hid_dev->report, hid_dev->report_id);
366 mouse_dev->buttons = calloc(mouse_dev->buttons_count, sizeof(int32_t));
367
368 if (mouse_dev->buttons == NULL) {
369 usb_log_error(NAME ": out of memory, giving up on device!\n");
370 free(mouse_dev);
371 return ENOMEM;
372 }
373
374 // TODO: how to know if the device supports the request???
375 usbhid_req_set_idle(&hid_dev->usb_dev->ctrl_pipe,
376 hid_dev->usb_dev->interface_no, IDLE_RATE);
377
378 int rc = usb_mouse_create_function(hid_dev, mouse_dev);
379 if (rc != EOK) {
380 free(mouse_dev->buttons);
381 free(mouse_dev);
382 return rc;
383 }
384
385 /* Save the Mouse device structure into the HID device structure. */
386 *data = mouse_dev;
387
388 return EOK;
389}
390
391bool usb_mouse_polling_callback(usb_hid_dev_t *hid_dev, void *data)
392{
393 if (hid_dev == NULL || data == NULL) {
394 usb_log_error(
395 "Missing argument to the mouse polling callback.\n");
396 return false;
397 }
398
399 usb_mouse_t *mouse_dev = data;
400
401 return usb_mouse_process_report(hid_dev, mouse_dev);
402}
403
404void usb_mouse_deinit(usb_hid_dev_t *hid_dev, void *data)
405{
406 if (data == NULL)
407 return;
408
409 usb_mouse_t *mouse_dev = data;
410
411 /* Hangup session to the console */
412 if (mouse_dev->mouse_sess != NULL) {
413 const int ret = async_hangup(mouse_dev->mouse_sess);
414 if (ret != EOK)
415 usb_log_warning("Failed to hang up mouse session: "
416 "%p, %s.\n", mouse_dev->mouse_sess, str_error(ret));
417 }
418
419 FUN_UNBIND_DESTROY(mouse_dev->mouse_fun);
420
421 free(mouse_dev->buttons);
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.