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

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

usbhid: multimedia; unite init and create_function functions.

  • Property mode set to 100644
File size: 8.4 KB
RevLine 
[e3b5129]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
[d3b6d5e]34 * USB Keyboard multimedia keys subdriver.
[e3b5129]35 */
36
37
[d3b6d5e]38#include "multimedia.h"
[e3b5129]39#include "../usbhid.h"
[b20de1d]40#include "keymap.h"
[e3b5129]41
[faa44e58]42#include <usb/hid/hidparser.h>
[e3b5129]43#include <usb/debug.h>
[faa44e58]44#include <usb/hid/usages/core.h>
[d7c72db]45#include <usb/hid/usages/consumer.h>
[b20de1d]46
[e3b5129]47#include <errno.h>
[79ae36dd]48#include <async.h>
[e3b5129]49#include <str_error.h>
50
[5f88293]51#include <ipc/kbdev.h>
[b20de1d]52#include <io/console.h>
53
[5da7199]54#define NAME "multimedia-keys"
[b20de1d]55
[31cfee16]56/*----------------------------------------------------------------------------*/
57/**
58 * Logitech UltraX device type.
59 */
[d3b6d5e]60typedef struct usb_multimedia_t {
[31cfee16]61 /** Previously pressed keys (not translated to key codes). */
[3e95cd7]62 //int32_t *keys_old;
[31cfee16]63 /** Currently pressed keys (not translated to key codes). */
[3e95cd7]64 //int32_t *keys;
[31cfee16]65 /** Count of stored keys (i.e. number of keys in the report). */
[571ba2a]66 //size_t key_count;
[5da7199]67 /** IPC session to the console device (for sending key events). */
68 async_sess_t *console_sess;
[af8c9b54]69 /** DDF function */
70 ddf_fun_t *fun;
[d3b6d5e]71} usb_multimedia_t;
[31cfee16]72
[b20de1d]73
[e3b5129]74/*----------------------------------------------------------------------------*/
[b20de1d]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
[5da7199]79 * assumes the caller is the console and thus it stores IPC session to it for
[b20de1d]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");
[571ba2a]90
[65b458c4]91 usb_multimedia_t *multim_dev = (usb_multimedia_t *)fun->driver_data;
[571ba2a]92
[65b458c4]93 if (multim_dev == NULL) {
[b20de1d]94 async_answer_0(icallid, EINVAL);
95 return;
96 }
[571ba2a]97
[5da7199]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);
[b20de1d]110}
111
112/*----------------------------------------------------------------------------*/
113
[d3b6d5e]114static ddf_dev_ops_t multimedia_ops = {
[b20de1d]115 .default_handler = default_connection_handler
[e3b5129]116};
117
[b20de1d]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 */
[65b458c4]136static void usb_multimedia_push_ev(usb_hid_dev_t *hid_dev,
137 usb_multimedia_t *multim_dev, int type, unsigned int key)
[e3b5129]138{
[b20de1d]139 assert(hid_dev != NULL);
[65b458c4]140 assert(multim_dev != NULL);
[571ba2a]141
[79ae36dd]142 kbd_event_t ev;
[571ba2a]143
[b20de1d]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);
[5da7199]150 if (multim_dev->console_sess == NULL) {
[b20de1d]151 usb_log_warning(
152 "Connection to console not ready, key discarded.\n");
153 return;
154 }
[571ba2a]155
[5da7199]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);
[b20de1d]159}
160
161/*----------------------------------------------------------------------------*/
162
[ce2a1c2]163int usb_multimedia_init(struct usb_hid_dev *hid_dev, void **data)
[31cfee16]164{
[ce2a1c2]165 if (hid_dev == NULL || hid_dev->usb_dev == NULL) {
166 return EINVAL; /*! @todo Other return code? */
167 }
168
169 usb_log_debug(NAME " Initializing HID/multimedia structure...\n");
170
[15f3c3f]171 /* Create the exposed function. */
[ce2a1c2]172 ddf_fun_t *fun = ddf_fun_create(
173 hid_dev->usb_dev->ddf_dev, fun_exposed, NAME);
[31cfee16]174 if (fun == NULL) {
175 usb_log_error("Could not create DDF function node.\n");
176 return ENOMEM;
177 }
[571ba2a]178
[d3b6d5e]179 fun->ops = &multimedia_ops;
[ce2a1c2]180
181 usb_multimedia_t *multim_dev =
182 ddf_fun_data_alloc(fun, sizeof(usb_multimedia_t));
183 if (multim_dev == NULL) {
184 ddf_fun_destroy(fun);
185 return ENOMEM;
186 }
187
188 multim_dev->console_sess = NULL;
189 multim_dev->fun = fun;
190
191 //todo Autorepeat?
[571ba2a]192
[31cfee16]193 int rc = ddf_fun_bind(fun);
194 if (rc != EOK) {
195 usb_log_error("Could not bind DDF function: %s.\n",
196 str_error(rc));
197 ddf_fun_destroy(fun);
198 return rc;
199 }
[571ba2a]200
[1dc4a5e]201 usb_log_debug("%s function created (handle: %" PRIun ").\n",
[0d59d0e9]202 NAME, fun->handle);
[571ba2a]203
[1dc4a5e]204 rc = ddf_fun_add_to_category(fun, "keyboard");
[31cfee16]205 if (rc != EOK) {
206 usb_log_error(
[1dc4a5e]207 "Could not add DDF function to category 'keyboard': %s.\n",
[31cfee16]208 str_error(rc));
209 ddf_fun_destroy(fun);
210 return rc;
211 }
[571ba2a]212
[ce2a1c2]213 /* Save the KBD device structure into the HID device structure. */
[65b458c4]214 *data = multim_dev;
[571ba2a]215
[d3b6d5e]216 usb_log_debug(NAME " HID/multimedia structure initialized.\n");
[b20de1d]217 return EOK;
218}
219
220/*----------------------------------------------------------------------------*/
221
[65b458c4]222void usb_multimedia_deinit(struct usb_hid_dev *hid_dev, void *data)
[b20de1d]223{
224 if (hid_dev == NULL) {
225 return;
226 }
[571ba2a]227
[65b458c4]228 if (data != NULL) {
229 usb_multimedia_t *multim_dev = (usb_multimedia_t *)data;
[5da7199]230 // hangup session to the console
231 async_hangup(multim_dev->console_sess);
[af8c9b54]232 const int ret = ddf_fun_unbind(multim_dev->fun);
233 if (ret != EOK) {
234 usb_log_error("Failed to unbind multim function.\n");
235 } else {
[2a5b62b]236 usb_log_debug2("%s unbound.\n", multim_dev->fun->name);
[af8c9b54]237 ddf_fun_destroy(multim_dev->fun);
238 }
[b20de1d]239 }
[e3b5129]240}
241
242/*----------------------------------------------------------------------------*/
243
[4d3c13e]244bool usb_multimedia_polling_callback(struct usb_hid_dev *hid_dev, void *data)
[e3b5129]245{
246 // TODO: checks
[4d3c13e]247 if (hid_dev == NULL || data == NULL) {
[19e0560e]248 return false;
249 }
[e3b5129]250
[4d3c13e]251 usb_multimedia_t *multim_dev = (usb_multimedia_t *)data;
[571ba2a]252
[e3b5129]253 usb_hid_report_path_t *path = usb_hid_report_path();
[d3b6d5e]254 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_CONSUMER, 0);
[cfbbe1d3]255
[65c3794]256 usb_hid_report_path_set_report_id(path, hid_dev->report_id);
[e50cd7f]257
[6513110]258 usb_hid_report_field_t *field = usb_hid_report_get_sibling(
[a8c4e871]259 &hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END
260 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
[6513110]261 USB_HID_REPORT_TYPE_INPUT);
[19e0560e]262
[b20de1d]263 /*! @todo Is this iterating OK if done multiple times?
264 * @todo The parsing is not OK
265 */
[6513110]266 while (field != NULL) {
[3a6e423]267 if(field->value != 0) {
[97cb542]268 usb_log_debug(NAME " KEY VALUE(%X) USAGE(%X)\n",
269 field->value, field->usage);
270 unsigned int key =
271 usb_multimedia_map_usage(field->usage);
[d59d0bb]272 const char *key_str =
[d7c72db]273 usbhid_multimedia_usage_to_str(field->usage);
[d59d0bb]274 usb_log_info("Pressed key: %s\n", key_str);
[65b458c4]275 usb_multimedia_push_ev(hid_dev, multim_dev, KEY_PRESS,
276 key);
[3a6e423]277 }
[571ba2a]278
[b20de1d]279 field = usb_hid_report_get_sibling(
[a8c4e871]280 &hid_dev->report, field, path, USB_HID_PATH_COMPARE_END
[b20de1d]281 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
282 USB_HID_REPORT_TYPE_INPUT);
[571ba2a]283 }
[e3b5129]284
285 usb_hid_report_path_free(path);
[571ba2a]286
[e3b5129]287 return true;
288}
289
290/**
291 * @}
292 */
Note: See TracBrowser for help on using the repository browser.