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

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

usbhid: multim unbind ddf function during destruction.

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