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 Keyboard multimedia keys subdriver.
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | #include "multimedia.h"
|
---|
39 | #include "../usbhid.h"
|
---|
40 | #include "keymap.h"
|
---|
41 |
|
---|
42 | #include <usb/hid/hidparser.h>
|
---|
43 | #include <usb/debug.h>
|
---|
44 | #include <usb/hid/usages/core.h>
|
---|
45 | #include <usb/hid/usages/consumer.h>
|
---|
46 |
|
---|
47 | #include <errno.h>
|
---|
48 | #include <str_error.h>
|
---|
49 |
|
---|
50 | #include <ipc/kbd.h>
|
---|
51 | #include <io/console.h>
|
---|
52 |
|
---|
53 | #define NAME "multimedia-keys"
|
---|
54 |
|
---|
55 | /*----------------------------------------------------------------------------*/
|
---|
56 | /**
|
---|
57 | * Logitech UltraX device type.
|
---|
58 | */
|
---|
59 | typedef struct usb_multimedia_t {
|
---|
60 | /** Previously pressed keys (not translated to key codes). */
|
---|
61 | //int32_t *keys_old;
|
---|
62 | /** Currently pressed keys (not translated to key codes). */
|
---|
63 | //int32_t *keys;
|
---|
64 | /** Count of stored keys (i.e. number of keys in the report). */
|
---|
65 | //size_t key_count;
|
---|
66 | /** IPC phone to the console device (for sending key events). */
|
---|
67 | int console_phone;
|
---|
68 | } usb_multimedia_t;
|
---|
69 |
|
---|
70 |
|
---|
71 | /*----------------------------------------------------------------------------*/
|
---|
72 | /**
|
---|
73 | * Default handler for IPC methods not handled by DDF.
|
---|
74 | *
|
---|
75 | * Currently recognizes only one method (IPC_M_CONNECT_TO_ME), in which case it
|
---|
76 | * assumes the caller is the console and thus it stores IPC phone to it for
|
---|
77 | * later use by the driver to notify about key events.
|
---|
78 | *
|
---|
79 | * @param fun Device function handling the call.
|
---|
80 | * @param icallid Call id.
|
---|
81 | * @param icall Call data.
|
---|
82 | */
|
---|
83 | static void default_connection_handler(ddf_fun_t *fun,
|
---|
84 | ipc_callid_t icallid, ipc_call_t *icall)
|
---|
85 | {
|
---|
86 | usb_log_debug(NAME " default_connection_handler()\n");
|
---|
87 |
|
---|
88 | sysarg_t method = IPC_GET_IMETHOD(*icall);
|
---|
89 |
|
---|
90 | usb_multimedia_t *multim_dev = (usb_multimedia_t *)fun->driver_data;
|
---|
91 | //usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
|
---|
92 |
|
---|
93 | if (multim_dev == NULL) {
|
---|
94 | async_answer_0(icallid, EINVAL);
|
---|
95 | return;
|
---|
96 | }
|
---|
97 |
|
---|
98 | if (method == IPC_M_CONNECT_TO_ME) {
|
---|
99 | int callback = IPC_GET_ARG5(*icall);
|
---|
100 |
|
---|
101 | if (multim_dev->console_phone != -1) {
|
---|
102 | async_answer_0(icallid, ELIMIT);
|
---|
103 | return;
|
---|
104 | }
|
---|
105 |
|
---|
106 | multim_dev->console_phone = callback;
|
---|
107 | usb_log_debug(NAME " Saved phone to console: %d\n", callback);
|
---|
108 | async_answer_0(icallid, EOK);
|
---|
109 | return;
|
---|
110 | }
|
---|
111 |
|
---|
112 | async_answer_0(icallid, EINVAL);
|
---|
113 | }
|
---|
114 |
|
---|
115 | /*----------------------------------------------------------------------------*/
|
---|
116 |
|
---|
117 | static ddf_dev_ops_t multimedia_ops = {
|
---|
118 | .default_handler = default_connection_handler
|
---|
119 | };
|
---|
120 |
|
---|
121 | /*----------------------------------------------------------------------------*/
|
---|
122 | /**
|
---|
123 | * Processes key events.
|
---|
124 | *
|
---|
125 | * @note This function was copied from AT keyboard driver and modified to suit
|
---|
126 | * USB keyboard.
|
---|
127 | *
|
---|
128 | * @note Lock keys are not sent to the console, as they are completely handled
|
---|
129 | * in the driver. It may, however, be required later that the driver
|
---|
130 | * sends also these keys to application (otherwise it cannot use those
|
---|
131 | * keys at all).
|
---|
132 | *
|
---|
133 | * @param hid_dev
|
---|
134 | * @param lgtch_dev
|
---|
135 | * @param type Type of the event (press / release). Recognized values:
|
---|
136 | * KEY_PRESS, KEY_RELEASE
|
---|
137 | * @param key Key code of the key according to HID Usage Tables.
|
---|
138 | */
|
---|
139 | static void usb_multimedia_push_ev(usb_hid_dev_t *hid_dev,
|
---|
140 | usb_multimedia_t *multim_dev, int type, unsigned int key)
|
---|
141 | {
|
---|
142 | assert(hid_dev != NULL);
|
---|
143 | assert(multim_dev != NULL);
|
---|
144 |
|
---|
145 | console_event_t ev;
|
---|
146 |
|
---|
147 | ev.type = type;
|
---|
148 | ev.key = key;
|
---|
149 | ev.mods = 0;
|
---|
150 | ev.c = 0;
|
---|
151 |
|
---|
152 | usb_log_debug2(NAME " Sending key %d to the console\n", ev.key);
|
---|
153 | if (multim_dev->console_phone < 0) {
|
---|
154 | usb_log_warning(
|
---|
155 | "Connection to console not ready, key discarded.\n");
|
---|
156 | return;
|
---|
157 | }
|
---|
158 |
|
---|
159 | async_msg_4(multim_dev->console_phone, KBD_EVENT, ev.type, ev.key,
|
---|
160 | ev.mods, ev.c);
|
---|
161 | }
|
---|
162 |
|
---|
163 | /*----------------------------------------------------------------------------*/
|
---|
164 |
|
---|
165 | static void usb_multimedia_free(usb_multimedia_t **multim_dev)
|
---|
166 | {
|
---|
167 | if (multim_dev == NULL || *multim_dev == NULL) {
|
---|
168 | return;
|
---|
169 | }
|
---|
170 |
|
---|
171 | // hangup phone to the console
|
---|
172 | async_hangup((*multim_dev)->console_phone);
|
---|
173 |
|
---|
174 | free(*multim_dev);
|
---|
175 | *multim_dev = NULL;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /*----------------------------------------------------------------------------*/
|
---|
179 |
|
---|
180 | static int usb_multimedia_create_function(usb_hid_dev_t *hid_dev,
|
---|
181 | usb_multimedia_t *multim_dev)
|
---|
182 | {
|
---|
183 | /* Create the function exposed under /dev/devices. */
|
---|
184 | ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed,
|
---|
185 | NAME);
|
---|
186 | if (fun == NULL) {
|
---|
187 | usb_log_error("Could not create DDF function node.\n");
|
---|
188 | return ENOMEM;
|
---|
189 | }
|
---|
190 |
|
---|
191 | fun->ops = &multimedia_ops;
|
---|
192 | fun->driver_data = multim_dev; // TODO: maybe change to hid_dev->data
|
---|
193 |
|
---|
194 | int rc = ddf_fun_bind(fun);
|
---|
195 | if (rc != EOK) {
|
---|
196 | usb_log_error("Could not bind DDF function: %s.\n",
|
---|
197 | str_error(rc));
|
---|
198 | // TODO: Can / should I destroy the DDF function?
|
---|
199 | ddf_fun_destroy(fun);
|
---|
200 | return rc;
|
---|
201 | }
|
---|
202 |
|
---|
203 | usb_log_debug("%s function created (jandle: %" PRIun ").\n",
|
---|
204 | NAME, fun->handle);
|
---|
205 |
|
---|
206 | rc = ddf_fun_add_to_class(fun, "keyboard");
|
---|
207 | if (rc != EOK) {
|
---|
208 | usb_log_error(
|
---|
209 | "Could not add DDF function to class 'keyboard': %s.\n",
|
---|
210 | str_error(rc));
|
---|
211 | // TODO: Can / should I destroy the DDF function?
|
---|
212 | ddf_fun_destroy(fun);
|
---|
213 | return rc;
|
---|
214 | }
|
---|
215 |
|
---|
216 | return EOK;
|
---|
217 | }
|
---|
218 |
|
---|
219 | /*----------------------------------------------------------------------------*/
|
---|
220 |
|
---|
221 | int usb_multimedia_init(struct usb_hid_dev *hid_dev, void **data)
|
---|
222 | {
|
---|
223 | if (hid_dev == NULL || hid_dev->usb_dev == NULL) {
|
---|
224 | return EINVAL; /*! @todo Other return code? */
|
---|
225 | }
|
---|
226 |
|
---|
227 | usb_log_debug(NAME " Initializing HID/multimedia structure...\n");
|
---|
228 |
|
---|
229 | usb_multimedia_t *multim_dev = (usb_multimedia_t *)malloc(
|
---|
230 | sizeof(usb_multimedia_t));
|
---|
231 | if (multim_dev == NULL) {
|
---|
232 | return ENOMEM;
|
---|
233 | }
|
---|
234 |
|
---|
235 | multim_dev->console_phone = -1;
|
---|
236 |
|
---|
237 | /*! @todo Autorepeat */
|
---|
238 |
|
---|
239 | // save the KBD device structure into the HID device structure
|
---|
240 | *data = multim_dev;
|
---|
241 |
|
---|
242 | usb_log_debug(NAME " HID/multimedia device structure initialized.\n");
|
---|
243 |
|
---|
244 | int rc = usb_multimedia_create_function(hid_dev, multim_dev);
|
---|
245 | if (rc != EOK) {
|
---|
246 | usb_multimedia_free(&multim_dev);
|
---|
247 | return rc;
|
---|
248 | }
|
---|
249 |
|
---|
250 | usb_log_debug(NAME " HID/multimedia structure initialized.\n");
|
---|
251 |
|
---|
252 | return EOK;
|
---|
253 | }
|
---|
254 |
|
---|
255 | /*----------------------------------------------------------------------------*/
|
---|
256 |
|
---|
257 | void usb_multimedia_deinit(struct usb_hid_dev *hid_dev, void *data)
|
---|
258 | {
|
---|
259 | if (hid_dev == NULL) {
|
---|
260 | return;
|
---|
261 | }
|
---|
262 |
|
---|
263 | if (data != NULL) {
|
---|
264 | usb_multimedia_t *multim_dev = (usb_multimedia_t *)data;
|
---|
265 | usb_multimedia_free(&multim_dev);
|
---|
266 | }
|
---|
267 | }
|
---|
268 |
|
---|
269 | /*----------------------------------------------------------------------------*/
|
---|
270 |
|
---|
271 | bool usb_multimedia_polling_callback(struct usb_hid_dev *hid_dev, void *data,
|
---|
272 | uint8_t *buffer, size_t buffer_size)
|
---|
273 | {
|
---|
274 | // TODO: checks
|
---|
275 | if (hid_dev == NULL || data == NULL || buffer == NULL) {
|
---|
276 | return false;
|
---|
277 | }
|
---|
278 |
|
---|
279 | usb_log_debug(NAME " usb_lgtch_polling_callback(%p, %p, %zu)\n",
|
---|
280 | hid_dev, buffer, buffer_size);
|
---|
281 |
|
---|
282 | usb_multimedia_t *multim_dev = (usb_multimedia_t *)data;
|
---|
283 |
|
---|
284 | usb_log_debug(NAME " Calling usb_hid_parse_report() with "
|
---|
285 | "buffer %s\n", usb_debug_str_buffer(buffer, buffer_size, 0));
|
---|
286 |
|
---|
287 | usb_hid_report_path_t *path = usb_hid_report_path();
|
---|
288 | usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_CONSUMER, 0);
|
---|
289 |
|
---|
290 | uint8_t report_id;
|
---|
291 |
|
---|
292 | int rc = usb_hid_parse_report(hid_dev->report, buffer, buffer_size,
|
---|
293 | &report_id);
|
---|
294 |
|
---|
295 | if (rc != EOK) {
|
---|
296 | usb_log_warning(NAME "Error in usb_hid_parse_report(): %s\n",
|
---|
297 | str_error(rc));
|
---|
298 | return true;
|
---|
299 | }
|
---|
300 |
|
---|
301 | usb_hid_report_path_set_report_id(path, report_id);
|
---|
302 |
|
---|
303 | usb_hid_report_field_t *field = usb_hid_report_get_sibling(
|
---|
304 | hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END
|
---|
305 | | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
|
---|
306 | USB_HID_REPORT_TYPE_INPUT);
|
---|
307 |
|
---|
308 | /*! @todo Is this iterating OK if done multiple times?
|
---|
309 | * @todo The parsing is not OK
|
---|
310 | */
|
---|
311 | while (field != NULL) {
|
---|
312 | if(field->value != 0) {
|
---|
313 | usb_log_debug(NAME " KEY VALUE(%X) USAGE(%X)\n",
|
---|
314 | field->value, field->usage);
|
---|
315 | unsigned int key =
|
---|
316 | usb_multimedia_map_usage(field->usage);
|
---|
317 | const char *key_str =
|
---|
318 | usbhid_multimedia_usage_to_str(field->usage);
|
---|
319 | usb_log_info("Pressed key: %s\n", key_str);
|
---|
320 | usb_multimedia_push_ev(hid_dev, multim_dev, KEY_PRESS,
|
---|
321 | key);
|
---|
322 | }
|
---|
323 |
|
---|
324 | field = usb_hid_report_get_sibling(
|
---|
325 | hid_dev->report, field, path, USB_HID_PATH_COMPARE_END
|
---|
326 | | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
|
---|
327 | USB_HID_REPORT_TYPE_INPUT);
|
---|
328 | }
|
---|
329 |
|
---|
330 | usb_hid_report_path_free(path);
|
---|
331 |
|
---|
332 | return true;
|
---|
333 | }
|
---|
334 |
|
---|
335 | /**
|
---|
336 | * @}
|
---|
337 | */
|
---|