source: mainline/uspace/drv/usbmouse/mouse.c@ d4beec3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d4beec3 was 019cff6, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

Add buffer dumping function usb_debug_str_buffer

Example usage in USB mouse driver.

Checking etc. will be added in later revisions.

  • Property mode set to 100644
File size: 4.2 KB
Line 
1/*
2 * Copyright (c) 2011 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 drvusbmouse
30 * @{
31 */
32/**
33 * @file
34 * Actual handling of USB mouse protocol.
35 */
36#include "mouse.h"
37#include <usb/debug.h>
38#include <errno.h>
39#include <str_error.h>
40#include <ipc/mouse.h>
41
42int usb_mouse_polling_fibril(void *arg)
43{
44 assert(arg != NULL);
45 ddf_dev_t *dev = (ddf_dev_t *) arg;
46 usb_mouse_t *mouse = (usb_mouse_t *) dev->driver_data;
47
48 assert(mouse);
49
50 size_t buffer_size = mouse->poll_pipe.max_packet_size;
51
52 if (buffer_size < 4) {
53 usb_log_error("Weird mouse, results will be skewed.\n");
54 buffer_size = 4;
55 }
56
57 uint8_t *buffer = malloc(buffer_size);
58 if (buffer == NULL) {
59 usb_log_error("Out of memory, poll fibril aborted.\n");
60 return ENOMEM;
61 }
62
63 while (true) {
64 async_usleep(mouse->poll_interval_us);
65
66 size_t actual_size;
67 int rc;
68
69 /*
70 * Error checking note:
71 * - failure when starting a session is considered
72 * temporary (e.g. out of phones, next try might succeed)
73 * - failure of transfer considered fatal (probably the
74 * device was unplugged)
75 * - session closing not checked (shall not fail anyway)
76 */
77
78 rc = usb_endpoint_pipe_start_session(&mouse->poll_pipe);
79 if (rc != EOK) {
80 usb_log_warning("Failed to start session, will try again: %s.\n",
81 str_error(rc));
82 continue;
83 }
84
85 rc = usb_endpoint_pipe_read(&mouse->poll_pipe,
86 buffer, buffer_size, &actual_size);
87
88 usb_endpoint_pipe_end_session(&mouse->poll_pipe);
89
90 if (rc != EOK) {
91 usb_log_error("Failed reading mouse input: %s.\n",
92 str_error(rc));
93 break;
94 }
95
96 usb_log_debug2("got buffer: %s.\n",
97 usb_debug_str_buffer(buffer, buffer_size, 0));
98
99 uint8_t butt = buffer[0];
100 char str_buttons[4] = {
101 butt & 1 ? '#' : '.',
102 butt & 2 ? '#' : '.',
103 butt & 4 ? '#' : '.',
104 0
105 };
106
107 int shift_x = ((int) buffer[1]) - 127;
108 int shift_y = ((int) buffer[2]) - 127;
109 int wheel = ((int) buffer[3]) - 127;
110
111 if (buffer[1] == 0) {
112 shift_x = 0;
113 }
114 if (buffer[2] == 0) {
115 shift_y = 0;
116 }
117 if (buffer[3] == 0) {
118 wheel = 0;
119 }
120
121 if (mouse->console_phone >= 0) {
122 if ((shift_x != 0) || (shift_y != 0)) {
123 /* FIXME: guessed for QEMU */
124 async_req_2_0(mouse->console_phone,
125 MEVENT_MOVE,
126 - shift_x / 10, - shift_y / 10);
127 }
128 if (butt) {
129 /* FIXME: proper button clicking. */
130 async_req_2_0(mouse->console_phone,
131 MEVENT_BUTTON, 1, 1);
132 async_req_2_0(mouse->console_phone,
133 MEVENT_BUTTON, 1, 0);
134 }
135 }
136
137 usb_log_debug("buttons=%s dX=%+3d dY=%+3d wheel=%+3d\n",
138 str_buttons, shift_x, shift_y, wheel);
139 }
140
141 /*
142 * Device was probably unplugged.
143 * Hang-up the phone to the console.
144 * FIXME: release allocated memory.
145 */
146 async_hangup(mouse->console_phone);
147 mouse->console_phone = -1;
148
149 usb_log_error("Mouse polling fibril terminated.\n");
150
151 return EOK;
152}
153
154
155/**
156 * @}
157 */
Note: See TracBrowser for help on using the repository browser.