source: mainline/uspace/drv/usbhid/mouse/mousedev.c@ 310c4df

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 310c4df was 310c4df, checked in by Lubos Slovak <lubos.slovak@…>, 14 years ago

Fixes in USB HID driver.

  • Missing loop in mouse subdriver.
  • Double free in kbd_free().
  • Property mode set to 100644
File size: 12.7 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/classes/hid.h>
40#include <usb/classes/hidreq.h>
41#include <usb/classes/hidut.h>
42#include <errno.h>
43#include <str_error.h>
44#include <ipc/mouse.h>
45
46#include "mousedev.h"
47#include "../usbhid.h"
48
49#define NAME "mouse"
50
51/*----------------------------------------------------------------------------*/
52
53usb_endpoint_description_t usb_hid_mouse_poll_endpoint_description = {
54 .transfer_type = USB_TRANSFER_INTERRUPT,
55 .direction = USB_DIRECTION_IN,
56 .interface_class = USB_CLASS_HID,
57 .interface_subclass = USB_HID_SUBCLASS_BOOT,
58 .interface_protocol = USB_HID_PROTOCOL_MOUSE,
59 .flags = 0
60};
61
62const char *HID_MOUSE_FUN_NAME = "mouse";
63const char *HID_MOUSE_CLASS_NAME = "mouse";
64
65/** Default idle rate for mouses. */
66static const uint8_t IDLE_RATE = 0;
67static const size_t USB_MOUSE_BUTTON_COUNT = 3;
68
69/*----------------------------------------------------------------------------*/
70
71enum {
72 USB_MOUSE_BOOT_REPORT_DESCRIPTOR_SIZE = 63
73};
74
75static const uint8_t USB_MOUSE_BOOT_REPORT_DESCRIPTOR[
76 USB_MOUSE_BOOT_REPORT_DESCRIPTOR_SIZE] = {
77 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
78 0x09, 0x02, // USAGE (Mouse)
79 0xa1, 0x01, // COLLECTION (Application)
80 0x09, 0x01, // USAGE (Pointer)
81 0xa1, 0x00, // COLLECTION (Physical)
82 0x95, 0x03, // REPORT_COUNT (3)
83 0x75, 0x01, // REPORT_SIZE (1)
84 0x05, 0x09, // USAGE_PAGE (Button)
85 0x19, 0x01, // USAGE_MINIMUM (Button 1)
86 0x29, 0x03, // USAGE_MAXIMUM (Button 3)
87 0x15, 0x00, // LOGICAL_MINIMUM (0)
88 0x25, 0x01, // LOGICAL_MAXIMUM (1)
89 0x81, 0x02, // INPUT (Data,Var,Abs)
90 0x95, 0x01, // REPORT_COUNT (1)
91 0x75, 0x05, // REPORT_SIZE (5)
92 0x81, 0x01, // INPUT (Cnst)
93 0x75, 0x08, // REPORT_SIZE (8)
94 0x95, 0x02, // REPORT_COUNT (2)
95 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
96 0x09, 0x30, // USAGE (X)
97 0x09, 0x31, // USAGE (Y)
98 0x15, 0x81, // LOGICAL_MINIMUM (-127)
99 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
100 0x81, 0x06, // INPUT (Data,Var,Rel)
101 0xc0, // END_COLLECTION
102 0xc0 // END_COLLECTION
103};
104
105/*----------------------------------------------------------------------------*/
106
107/** Default handler for IPC methods not handled by DDF.
108 *
109 * @param fun Device function handling the call.
110 * @param icallid Call id.
111 * @param icall Call data.
112 */
113static void default_connection_handler(ddf_fun_t *fun,
114 ipc_callid_t icallid, ipc_call_t *icall)
115{
116 sysarg_t method = IPC_GET_IMETHOD(*icall);
117
118 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
119
120 if (hid_dev == NULL || hid_dev->data == NULL) {
121 async_answer_0(icallid, EINVAL);
122 return;
123 }
124
125 assert(hid_dev != NULL);
126 assert(hid_dev->data != NULL);
127 usb_mouse_t *mouse_dev = (usb_mouse_t *)hid_dev->data;
128
129 if (method == IPC_M_CONNECT_TO_ME) {
130 int callback = IPC_GET_ARG5(*icall);
131
132 if (mouse_dev->console_phone != -1) {
133 async_answer_0(icallid, ELIMIT);
134 return;
135 }
136
137 mouse_dev->console_phone = callback;
138 usb_log_debug("Console phone to mouse set ok (%d).\n", callback);
139 async_answer_0(icallid, EOK);
140 return;
141 }
142
143 async_answer_0(icallid, EINVAL);
144}
145
146/*----------------------------------------------------------------------------*/
147
148static usb_mouse_t *usb_mouse_new(void)
149{
150 usb_mouse_t *mouse = calloc(1, sizeof(usb_mouse_t));
151 if (mouse == NULL) {
152 return NULL;
153 }
154 mouse->console_phone = -1;
155
156 return mouse;
157}
158
159/*----------------------------------------------------------------------------*/
160
161static void usb_mouse_free(usb_mouse_t **mouse_dev)
162{
163 assert(mouse_dev != NULL && *mouse_dev != NULL);
164
165 // hangup phone to the console
166 if ((*mouse_dev)->console_phone >= 0) {
167 async_hangup((*mouse_dev)->console_phone);
168 }
169
170 free(*mouse_dev);
171 *mouse_dev = NULL;
172}
173
174/*----------------------------------------------------------------------------*/
175
176static bool usb_mouse_process_boot_report(usb_hid_dev_t *hid_dev,
177 uint8_t *buffer, size_t buffer_size)
178{
179 usb_mouse_t *mouse_dev = (usb_mouse_t *)hid_dev->data;
180
181 usb_log_debug2("got buffer: %s.\n",
182 usb_debug_str_buffer(buffer, buffer_size, 0));
183
184 if (mouse_dev->console_phone < 0) {
185 usb_log_error(NAME " No console phone.\n");
186 return false; // ??
187 }
188
189 /*
190 * parse the input report
191 */
192
193 usb_log_debug(NAME " Calling usb_hid_parse_report() with "
194 "buffer %s\n", usb_debug_str_buffer(buffer, buffer_size, 0));
195
196 uint8_t report_id;
197
198 int rc = usb_hid_parse_report(hid_dev->report, buffer, buffer_size,
199 &report_id);
200
201 if (rc != EOK) {
202 usb_log_warning(NAME "Error in usb_hid_parse_report(): %s\n",
203 str_error(rc));
204 return true;
205 }
206
207 /*
208 * X
209 */
210 int shift_x = 0;
211
212 usb_hid_report_path_t *path = usb_hid_report_path();
213 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_GENERIC_DESKTOP,
214 USB_HIDUT_USAGE_GENERIC_DESKTOP_X);
215
216 usb_hid_report_path_set_report_id(path, 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_REPORT_TYPE_INPUT);
221
222 if (field != NULL) {
223 usb_log_debug(NAME " VALUE(%X) USAGE(%X)\n", field->value,
224 field->usage);
225 shift_x = field->value;
226 }
227
228 usb_hid_report_path_free(path);
229
230 /*
231 * Y
232 */
233 int shift_y = 0;
234
235 path = usb_hid_report_path();
236 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_GENERIC_DESKTOP,
237 USB_HIDUT_USAGE_GENERIC_DESKTOP_Y);
238
239 usb_hid_report_path_set_report_id(path, report_id);
240
241 field = usb_hid_report_get_sibling(
242 hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END,
243 USB_HID_REPORT_TYPE_INPUT);
244
245 if (field != NULL) {
246 usb_log_debug(NAME " VALUE(%X) USAGE(%X)\n", field->value,
247 field->usage);
248 shift_y = field->value;
249 }
250
251 usb_hid_report_path_free(path);
252
253 if ((shift_x != 0) || (shift_y != 0)) {
254 async_req_2_0(mouse_dev->console_phone,
255 MEVENT_MOVE, shift_x, shift_y);
256 }
257
258 /*
259 * Buttons
260 */
261 path = usb_hid_report_path();
262 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_BUTTON, 0);
263 usb_hid_report_path_set_report_id(path, report_id);
264
265 field = usb_hid_report_get_sibling(
266 hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END
267 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
268 USB_HID_REPORT_TYPE_INPUT);
269
270 while (field != NULL) {
271 usb_log_debug(NAME " VALUE(%X) USAGE(%X)\n", field->value,
272 field->usage);
273
274 if (mouse_dev->buttons[field->usage - field->usage_minimum] == 0
275 && field->value != 0) {
276 async_req_2_0(mouse_dev->console_phone,
277 MEVENT_BUTTON, field->usage, 1);
278 mouse_dev->buttons[field->usage - field->usage_minimum]
279 = field->value;
280 } else if (
281 mouse_dev->buttons[field->usage - field->usage_minimum] != 0
282 && field->value == 0) {
283 async_req_2_0(mouse_dev->console_phone,
284 MEVENT_BUTTON, field->usage, 0);
285 mouse_dev->buttons[field->usage - field->usage_minimum]
286 = field->value;
287 }
288
289 field = usb_hid_report_get_sibling(
290 hid_dev->report, field, path, USB_HID_PATH_COMPARE_END
291 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
292 USB_HID_REPORT_TYPE_INPUT);
293 }
294
295 usb_hid_report_path_free(path);
296
297 return true;
298}
299
300/*----------------------------------------------------------------------------*/
301
302static int usb_mouse_create_function(usb_hid_dev_t *hid_dev)
303{
304 /* Create the function exposed under /dev/devices. */
305 usb_log_debug("Creating DDF function %s...\n", HID_MOUSE_FUN_NAME);
306 ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed,
307 HID_MOUSE_FUN_NAME);
308 if (fun == NULL) {
309 usb_log_error("Could not create DDF function node.\n");
310 return ENOMEM;
311 }
312
313 /*
314 * Store the initialized HID device and HID ops
315 * to the DDF function.
316 */
317 fun->ops = &hid_dev->ops;
318 fun->driver_data = hid_dev; // TODO: maybe change to hid_dev->data
319
320 int rc = ddf_fun_bind(fun);
321 if (rc != EOK) {
322 usb_log_error("Could not bind DDF function: %s.\n",
323 str_error(rc));
324 ddf_fun_destroy(fun);
325 return rc;
326 }
327
328 usb_log_debug("Adding DDF function to class %s...\n",
329 HID_MOUSE_CLASS_NAME);
330 rc = ddf_fun_add_to_class(fun, HID_MOUSE_CLASS_NAME);
331 if (rc != EOK) {
332 usb_log_error(
333 "Could not add DDF function to class %s: %s.\n",
334 HID_MOUSE_CLASS_NAME, str_error(rc));
335 ddf_fun_destroy(fun);
336 return rc;
337 }
338
339 return EOK;
340}
341
342/*----------------------------------------------------------------------------*/
343
344int usb_mouse_init(usb_hid_dev_t *hid_dev)
345{
346 usb_log_debug("Initializing HID/Mouse structure...\n");
347
348 if (hid_dev == NULL) {
349 usb_log_error("Failed to init keyboard structure: no structure"
350 " given.\n");
351 return EINVAL;
352 }
353
354 usb_mouse_t *mouse_dev = usb_mouse_new();
355 if (mouse_dev == NULL) {
356 usb_log_error("Error while creating USB/HID Mouse device "
357 "structure.\n");
358 return ENOMEM;
359 }
360
361 mouse_dev->buttons = (int32_t *)calloc(USB_MOUSE_BUTTON_COUNT,
362 sizeof(int32_t));
363
364 if (mouse_dev->buttons == NULL) {
365 usb_log_fatal("No memory!\n");
366 free(mouse_dev);
367 return ENOMEM;
368 }
369
370 // save the Mouse device structure into the HID device structure
371 hid_dev->data = mouse_dev;
372
373 // set handler for incoming calls
374 hid_dev->ops.default_handler = default_connection_handler;
375
376 // TODO: how to know if the device supports the request???
377// usbhid_req_set_idle(&hid_dev->usb_dev->ctrl_pipe,
378// hid_dev->usb_dev->interface_no, IDLE_RATE);
379
380 int rc = usb_mouse_create_function(hid_dev);
381 if (rc != EOK) {
382 usb_mouse_free(&mouse_dev);
383 return rc;
384 }
385
386 return EOK;
387}
388
389/*----------------------------------------------------------------------------*/
390
391bool usb_mouse_polling_callback(usb_hid_dev_t *hid_dev, uint8_t *buffer,
392 size_t buffer_size)
393{
394 usb_log_debug("usb_mouse_polling_callback()\n");
395 usb_debug_str_buffer(buffer, buffer_size, 0);
396
397 if (hid_dev == NULL) {
398 usb_log_error("Missing argument to the mouse polling callback."
399 "\n");
400 return false;
401 }
402
403 if (hid_dev->data == NULL) {
404 usb_log_error("Wrong argument to the mouse polling callback."
405 "\n");
406 return false;
407 }
408
409 return usb_mouse_process_boot_report(hid_dev, buffer, buffer_size);
410}
411
412/*----------------------------------------------------------------------------*/
413
414void usb_mouse_deinit(usb_hid_dev_t *hid_dev)
415{
416 usb_mouse_free((usb_mouse_t **)&hid_dev->data);
417}
418
419/*----------------------------------------------------------------------------*/
420
421int usb_mouse_set_boot_protocol(usb_hid_dev_t *hid_dev)
422{
423 int rc = usb_hid_parse_report_descriptor(hid_dev->report,
424 USB_MOUSE_BOOT_REPORT_DESCRIPTOR,
425 USB_MOUSE_BOOT_REPORT_DESCRIPTOR_SIZE);
426
427 if (rc != EOK) {
428 usb_log_error("Failed to parse boot report descriptor: %s\n",
429 str_error(rc));
430 return rc;
431 }
432
433 rc = usbhid_req_set_protocol(&hid_dev->usb_dev->ctrl_pipe,
434 hid_dev->usb_dev->interface_no, USB_HID_PROTOCOL_BOOT);
435
436 if (rc != EOK) {
437 usb_log_warning("Failed to set boot protocol to the device: "
438 "%s\n", str_error(rc));
439 return rc;
440 }
441
442 return EOK;
443}
444
445/**
446 * @}
447 */
Note: See TracBrowser for help on using the repository browser.