1 | /*
|
---|
2 | * Copyright (c) 2011 Lubos Slovak
|
---|
3 | * Copyright (c) 2011 Vojtech Horky
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup drvusbhid
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /**
|
---|
34 | * @file
|
---|
35 | * USB Keyboard multimedia keys subdriver.
|
---|
36 | */
|
---|
37 |
|
---|
38 |
|
---|
39 | #include "multimedia.h"
|
---|
40 | #include "../usbhid.h"
|
---|
41 | #include "keymap.h"
|
---|
42 |
|
---|
43 | #include <usb/hid/hidparser.h>
|
---|
44 | #include <usb/debug.h>
|
---|
45 | #include <usb/hid/usages/core.h>
|
---|
46 | #include <usb/hid/usages/consumer.h>
|
---|
47 |
|
---|
48 | #include <errno.h>
|
---|
49 | #include <async.h>
|
---|
50 | #include <str_error.h>
|
---|
51 |
|
---|
52 | #include <ipc/kbdev.h>
|
---|
53 | #include <io/console.h>
|
---|
54 |
|
---|
55 | #define NAME "multimedia-keys"
|
---|
56 |
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Logitech UltraX device type.
|
---|
60 | */
|
---|
61 | typedef struct usb_multimedia_t {
|
---|
62 | /** Previously pressed keys (not translated to key codes). */
|
---|
63 | //int32_t *keys_old;
|
---|
64 | /** Currently pressed keys (not translated to key codes). */
|
---|
65 | //int32_t *keys;
|
---|
66 | /** Count of stored keys (i.e. number of keys in the report). */
|
---|
67 | //size_t key_count;
|
---|
68 | /** IPC session to the console device (for sending key events). */
|
---|
69 | async_sess_t *console_sess;
|
---|
70 | } usb_multimedia_t;
|
---|
71 |
|
---|
72 |
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Default handler for IPC methods not handled by DDF.
|
---|
76 | *
|
---|
77 | * Currently recognizes only one method (IPC_M_CONNECT_TO_ME), in which case it
|
---|
78 | * assumes the caller is the console and thus it stores IPC session to it for
|
---|
79 | * later use by the driver to notify about key events.
|
---|
80 | *
|
---|
81 | * @param fun Device function handling the call.
|
---|
82 | * @param icallid Call id.
|
---|
83 | * @param icall Call data.
|
---|
84 | */
|
---|
85 | static void default_connection_handler(ddf_fun_t *fun,
|
---|
86 | ipc_callid_t icallid, ipc_call_t *icall)
|
---|
87 | {
|
---|
88 | usb_log_debug(NAME " default_connection_handler()");
|
---|
89 |
|
---|
90 | usb_multimedia_t *multim_dev = ddf_fun_data_get(fun);
|
---|
91 |
|
---|
92 | async_sess_t *sess =
|
---|
93 | async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
|
---|
94 | if (sess != NULL) {
|
---|
95 | if (multim_dev->console_sess == NULL) {
|
---|
96 | multim_dev->console_sess = sess;
|
---|
97 | usb_log_debug(NAME " Saved session to console: %p",
|
---|
98 | sess);
|
---|
99 | async_answer_0(icallid, EOK);
|
---|
100 | } else
|
---|
101 | async_answer_0(icallid, ELIMIT);
|
---|
102 | } else
|
---|
103 | async_answer_0(icallid, EINVAL);
|
---|
104 | }
|
---|
105 |
|
---|
106 | static ddf_dev_ops_t multimedia_ops = {
|
---|
107 | .default_handler = default_connection_handler
|
---|
108 | };
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Processes key events.
|
---|
112 | *
|
---|
113 | * @note This function was copied from AT keyboard driver and modified to suit
|
---|
114 | * USB keyboard.
|
---|
115 | *
|
---|
116 | * @note Lock keys are not sent to the console, as they are completely handled
|
---|
117 | * in the driver. It may, however, be required later that the driver
|
---|
118 | * sends also these keys to application (otherwise it cannot use those
|
---|
119 | * keys at all).
|
---|
120 | *
|
---|
121 | * @param hid_dev
|
---|
122 | * @param multim_dev
|
---|
123 | * @param type Type of the event (press / release). Recognized values:
|
---|
124 | * KEY_PRESS, KEY_RELEASE
|
---|
125 | * @param key Key code of the key according to HID Usage Tables.
|
---|
126 | */
|
---|
127 | static void usb_multimedia_push_ev(
|
---|
128 | usb_multimedia_t *multim_dev, int type, unsigned int key)
|
---|
129 | {
|
---|
130 | assert(multim_dev != NULL);
|
---|
131 |
|
---|
132 | const kbd_event_t ev = {
|
---|
133 | .type = type,
|
---|
134 | .key = key,
|
---|
135 | .mods = 0,
|
---|
136 | .c = 0,
|
---|
137 | };
|
---|
138 |
|
---|
139 | usb_log_debug2(NAME " Sending key %d to the console", ev.key);
|
---|
140 | if (multim_dev->console_sess == NULL) {
|
---|
141 | usb_log_warning(
|
---|
142 | "Connection to console not ready, key discarded.\n");
|
---|
143 | return;
|
---|
144 | }
|
---|
145 |
|
---|
146 | async_exch_t *exch = async_exchange_begin(multim_dev->console_sess);
|
---|
147 | if (exch != NULL) {
|
---|
148 | async_msg_4(exch, KBDEV_EVENT, ev.type, ev.key, ev.mods, ev.c);
|
---|
149 | async_exchange_end(exch);
|
---|
150 | } else {
|
---|
151 | usb_log_warning("Failed to send multimedia key.");
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | int usb_multimedia_init(struct usb_hid_dev *hid_dev, void **data)
|
---|
156 | {
|
---|
157 | if (hid_dev == NULL || hid_dev->usb_dev == NULL) {
|
---|
158 | return EINVAL;
|
---|
159 | }
|
---|
160 |
|
---|
161 | usb_log_debug(NAME " Initializing HID/multimedia structure...");
|
---|
162 |
|
---|
163 | /* Create the exposed function. */
|
---|
164 | ddf_fun_t *fun = usb_device_ddf_fun_create(
|
---|
165 | hid_dev->usb_dev, fun_exposed, NAME);
|
---|
166 | if (fun == NULL) {
|
---|
167 | usb_log_error("Could not create DDF function node.");
|
---|
168 | return ENOMEM;
|
---|
169 | }
|
---|
170 |
|
---|
171 | ddf_fun_set_ops(fun, &multimedia_ops);
|
---|
172 |
|
---|
173 | usb_multimedia_t *multim_dev =
|
---|
174 | ddf_fun_data_alloc(fun, sizeof(usb_multimedia_t));
|
---|
175 | if (multim_dev == NULL) {
|
---|
176 | ddf_fun_destroy(fun);
|
---|
177 | return ENOMEM;
|
---|
178 | }
|
---|
179 |
|
---|
180 | multim_dev->console_sess = NULL;
|
---|
181 |
|
---|
182 | //todo Autorepeat?
|
---|
183 |
|
---|
184 | int rc = ddf_fun_bind(fun);
|
---|
185 | if (rc != EOK) {
|
---|
186 | usb_log_error("Could not bind DDF function: %s.",
|
---|
187 | str_error(rc));
|
---|
188 | ddf_fun_destroy(fun);
|
---|
189 | return rc;
|
---|
190 | }
|
---|
191 |
|
---|
192 | usb_log_debug(NAME " function created (handle: %" PRIun ").",
|
---|
193 | ddf_fun_get_handle(fun));
|
---|
194 |
|
---|
195 | rc = ddf_fun_add_to_category(fun, "keyboard");
|
---|
196 | if (rc != EOK) {
|
---|
197 | usb_log_error(
|
---|
198 | "Could not add DDF function to category 'keyboard': %s.\n",
|
---|
199 | str_error(rc));
|
---|
200 | if (ddf_fun_unbind(fun) != EOK) {
|
---|
201 | usb_log_error("Failed to unbind %s, won't destroy.",
|
---|
202 | ddf_fun_get_name(fun));
|
---|
203 | } else {
|
---|
204 | ddf_fun_destroy(fun);
|
---|
205 | }
|
---|
206 | return rc;
|
---|
207 | }
|
---|
208 |
|
---|
209 | /* Save the KBD device structure into the HID device structure. */
|
---|
210 | *data = fun;
|
---|
211 |
|
---|
212 | usb_log_debug(NAME " HID/multimedia structure initialized.");
|
---|
213 | return EOK;
|
---|
214 | }
|
---|
215 |
|
---|
216 | void usb_multimedia_deinit(struct usb_hid_dev *hid_dev, void *data)
|
---|
217 | {
|
---|
218 | ddf_fun_t *fun = data;
|
---|
219 |
|
---|
220 | usb_multimedia_t *multim_dev = ddf_fun_data_get(fun);
|
---|
221 |
|
---|
222 | /* Hangup session to the console */
|
---|
223 | if (multim_dev->console_sess)
|
---|
224 | async_hangup(multim_dev->console_sess);
|
---|
225 | if (ddf_fun_unbind(fun) != EOK) {
|
---|
226 | usb_log_error("Failed to unbind %s, won't destroy.",
|
---|
227 | ddf_fun_get_name(fun));
|
---|
228 | } else {
|
---|
229 | usb_log_debug2("%s unbound.", ddf_fun_get_name(fun));
|
---|
230 | /* This frees multim_dev too as it was stored in
|
---|
231 | * fun->data */
|
---|
232 | ddf_fun_destroy(fun);
|
---|
233 | }
|
---|
234 | }
|
---|
235 |
|
---|
236 | bool usb_multimedia_polling_callback(struct usb_hid_dev *hid_dev, void *data)
|
---|
237 | {
|
---|
238 | // TODO: checks
|
---|
239 | ddf_fun_t *fun = data;
|
---|
240 | if (hid_dev == NULL) {
|
---|
241 | return false;
|
---|
242 | }
|
---|
243 |
|
---|
244 | usb_multimedia_t *multim_dev = ddf_fun_data_get(fun);
|
---|
245 |
|
---|
246 | usb_hid_report_path_t *path = usb_hid_report_path();
|
---|
247 | if (path == NULL)
|
---|
248 | return true; /* This might be a temporary failure. */
|
---|
249 |
|
---|
250 | int ret =
|
---|
251 | usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_CONSUMER, 0);
|
---|
252 | if (ret != EOK) {
|
---|
253 | usb_hid_report_path_free(path);
|
---|
254 | return true; /* This might be a temporary failure. */
|
---|
255 | }
|
---|
256 |
|
---|
257 | usb_hid_report_path_set_report_id(path, hid_dev->report_id);
|
---|
258 |
|
---|
259 | usb_hid_report_field_t *field = usb_hid_report_get_sibling(
|
---|
260 | &hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END
|
---|
261 | | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
|
---|
262 | USB_HID_REPORT_TYPE_INPUT);
|
---|
263 |
|
---|
264 | //FIXME Is this iterating OK if done multiple times?
|
---|
265 | //FIXME The parsing is not OK. (what's wrong?)
|
---|
266 | while (field != NULL) {
|
---|
267 | if (field->value != 0) {
|
---|
268 | usb_log_debug(NAME " KEY VALUE(%X) USAGE(%X)",
|
---|
269 | field->value, field->usage);
|
---|
270 | const unsigned key =
|
---|
271 | usb_multimedia_map_usage(field->usage);
|
---|
272 | const char *key_str =
|
---|
273 | usbhid_multimedia_usage_to_str(field->usage);
|
---|
274 | usb_log_info("Pressed key: %s", key_str);
|
---|
275 | usb_multimedia_push_ev(multim_dev, KEY_PRESS, key);
|
---|
276 | }
|
---|
277 |
|
---|
278 | field = usb_hid_report_get_sibling(
|
---|
279 | &hid_dev->report, field, path, USB_HID_PATH_COMPARE_END
|
---|
280 | | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
|
---|
281 | USB_HID_REPORT_TYPE_INPUT);
|
---|
282 | }
|
---|
283 |
|
---|
284 | usb_hid_report_path_free(path);
|
---|
285 |
|
---|
286 | return true;
|
---|
287 | }
|
---|
288 | /**
|
---|
289 | * @}
|
---|
290 | */
|
---|