source: mainline/uspace/drv/bus/usb/usbhid/multimedia/multimedia.c@ 6785b538

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6785b538 was 6785b538, checked in by Jan Vesely <jano.vesely@…>, 13 years ago

usbmid, usbhid, usbhub, usbflbk: Don't access ddf_dev directly, use provided functions.

  • Property mode set to 100644
File size: 8.1 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 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 <async.h>
49#include <str_error.h>
50
51#include <ipc/kbdev.h>
52#include <io/console.h>
53
54#define NAME "multimedia-keys"
55
56
57/**
58 * Logitech UltraX device type.
59 */
60typedef struct usb_multimedia_t {
61 /** Previously pressed keys (not translated to key codes). */
62 //int32_t *keys_old;
63 /** Currently pressed keys (not translated to key codes). */
64 //int32_t *keys;
65 /** Count of stored keys (i.e. number of keys in the report). */
66 //size_t key_count;
67 /** IPC session to the console device (for sending key events). */
68 async_sess_t *console_sess;
69} usb_multimedia_t;
70
71
72
73/**
74 * Default handler for IPC methods not handled by DDF.
75 *
76 * Currently recognizes only one method (IPC_M_CONNECT_TO_ME), in which case it
77 * assumes the caller is the console and thus it stores IPC session to it for
78 * later use by the driver to notify about key events.
79 *
80 * @param fun Device function handling the call.
81 * @param icallid Call id.
82 * @param icall Call data.
83 */
84static void default_connection_handler(ddf_fun_t *fun,
85 ipc_callid_t icallid, ipc_call_t *icall)
86{
87 usb_log_debug(NAME " default_connection_handler()\n");
88
89 usb_multimedia_t *multim_dev = ddf_fun_data_get(fun);
90
91 async_sess_t *sess =
92 async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
93 if (sess != NULL) {
94 if (multim_dev->console_sess == NULL) {
95 multim_dev->console_sess = sess;
96 usb_log_debug(NAME " Saved session to console: %p\n",
97 sess);
98 async_answer_0(icallid, EOK);
99 } else
100 async_answer_0(icallid, ELIMIT);
101 } else
102 async_answer_0(icallid, EINVAL);
103}
104
105static ddf_dev_ops_t multimedia_ops = {
106 .default_handler = default_connection_handler
107};
108
109/**
110 * Processes key events.
111 *
112 * @note This function was copied from AT keyboard driver and modified to suit
113 * USB keyboard.
114 *
115 * @note Lock keys are not sent to the console, as they are completely handled
116 * in the driver. It may, however, be required later that the driver
117 * sends also these keys to application (otherwise it cannot use those
118 * keys at all).
119 *
120 * @param hid_dev
121 * @param multim_dev
122 * @param type Type of the event (press / release). Recognized values:
123 * KEY_PRESS, KEY_RELEASE
124 * @param key Key code of the key according to HID Usage Tables.
125 */
126static void usb_multimedia_push_ev(
127 usb_multimedia_t *multim_dev, int type, unsigned int key)
128{
129 assert(multim_dev != NULL);
130
131 const kbd_event_t ev = {
132 .type = type,
133 .key = key,
134 .mods = 0,
135 .c = 0,
136 };
137
138 usb_log_debug2(NAME " Sending key %d to the console\n", ev.key);
139 if (multim_dev->console_sess == NULL) {
140 usb_log_warning(
141 "Connection to console not ready, key discarded.\n");
142 return;
143 }
144
145 async_exch_t *exch = async_exchange_begin(multim_dev->console_sess);
146 if (exch != NULL) {
147 async_msg_4(exch, KBDEV_EVENT, ev.type, ev.key, ev.mods, ev.c);
148 async_exchange_end(exch);
149 } else {
150 usb_log_warning("Failed to send multimedia key.\n");
151 }
152}
153
154int usb_multimedia_init(struct usb_hid_dev *hid_dev, void **data)
155{
156 if (hid_dev == NULL || hid_dev->usb_dev == NULL) {
157 return EINVAL;
158 }
159
160 usb_log_debug(NAME " Initializing HID/multimedia structure...\n");
161
162 /* Create the exposed function. */
163 ddf_fun_t *fun = usb_device_ddf_fun_create(
164 hid_dev->usb_dev, fun_exposed, NAME);
165 if (fun == NULL) {
166 usb_log_error("Could not create DDF function node.\n");
167 return ENOMEM;
168 }
169
170 ddf_fun_set_ops(fun, &multimedia_ops);
171
172 usb_multimedia_t *multim_dev =
173 ddf_fun_data_alloc(fun, sizeof(usb_multimedia_t));
174 if (multim_dev == NULL) {
175 ddf_fun_destroy(fun);
176 return ENOMEM;
177 }
178
179 multim_dev->console_sess = NULL;
180
181 //todo Autorepeat?
182
183 int rc = ddf_fun_bind(fun);
184 if (rc != EOK) {
185 usb_log_error("Could not bind DDF function: %s.\n",
186 str_error(rc));
187 ddf_fun_destroy(fun);
188 return rc;
189 }
190
191 usb_log_debug(NAME " function created (handle: %" PRIun ").\n",
192 ddf_fun_get_handle(fun));
193
194 rc = ddf_fun_add_to_category(fun, "keyboard");
195 if (rc != EOK) {
196 usb_log_error(
197 "Could not add DDF function to category 'keyboard': %s.\n",
198 str_error(rc));
199 if (ddf_fun_unbind(fun) != EOK) {
200 usb_log_error("Failed to unbind %s, won't destroy.\n",
201 ddf_fun_get_name(fun));
202 } else {
203 ddf_fun_destroy(fun);
204 }
205 return rc;
206 }
207
208 /* Save the KBD device structure into the HID device structure. */
209 *data = fun;
210
211 usb_log_debug(NAME " HID/multimedia structure initialized.\n");
212 return EOK;
213}
214
215void usb_multimedia_deinit(struct usb_hid_dev *hid_dev, void *data)
216{
217 ddf_fun_t *fun = data;
218
219 usb_multimedia_t *multim_dev = ddf_fun_data_get(fun);
220
221 /* Hangup session to the console */
222 if (multim_dev->console_sess)
223 async_hangup(multim_dev->console_sess);
224 if (ddf_fun_unbind(fun) != EOK) {
225 usb_log_error("Failed to unbind %s, won't destroy.\n",
226 ddf_fun_get_name(fun));
227 } else {
228 usb_log_debug2("%s unbound.\n", ddf_fun_get_name(fun));
229 /* This frees multim_dev too as it was stored in
230 * fun->data */
231 ddf_fun_destroy(fun);
232 }
233}
234
235bool usb_multimedia_polling_callback(struct usb_hid_dev *hid_dev, void *data)
236{
237 // TODO: checks
238 ddf_fun_t *fun = data;
239 if (hid_dev == NULL) {
240 return false;
241 }
242
243 usb_multimedia_t *multim_dev = ddf_fun_data_get(fun);
244
245 usb_hid_report_path_t *path = usb_hid_report_path();
246 if (path == NULL)
247 return true; /* This might be a temporary failure. */
248
249 int ret =
250 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_CONSUMER, 0);
251 if (ret != EOK) {
252 usb_hid_report_path_free(path);
253 return true; /* This might be a temporary failure. */
254 }
255
256 usb_hid_report_path_set_report_id(path, hid_dev->report_id);
257
258 usb_hid_report_field_t *field = usb_hid_report_get_sibling(
259 &hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END
260 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
261 USB_HID_REPORT_TYPE_INPUT);
262
263 //FIXME Is this iterating OK if done multiple times?
264 //FIXME The parsing is not OK. (what's wrong?)
265 while (field != NULL) {
266 if (field->value != 0) {
267 usb_log_debug(NAME " KEY VALUE(%X) USAGE(%X)\n",
268 field->value, field->usage);
269 const unsigned key =
270 usb_multimedia_map_usage(field->usage);
271 const char *key_str =
272 usbhid_multimedia_usage_to_str(field->usage);
273 usb_log_info("Pressed key: %s\n", key_str);
274 usb_multimedia_push_ev(multim_dev, KEY_PRESS, key);
275 }
276
277 field = usb_hid_report_get_sibling(
278 &hid_dev->report, field, path, USB_HID_PATH_COMPARE_END
279 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
280 USB_HID_REPORT_TYPE_INPUT);
281 }
282
283 usb_hid_report_path_free(path);
284
285 return true;
286}
287/**
288 * @}
289 */
Note: See TracBrowser for help on using the repository browser.