| [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] | 60 | typedef 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;
|
|---|
| [d3b6d5e] | 69 | } usb_multimedia_t;
|
|---|
| [31cfee16] | 70 |
|
|---|
| [b20de1d] | 71 |
|
|---|
| [e3b5129] | 72 | /*----------------------------------------------------------------------------*/
|
|---|
| [05920d23] | 73 | /**
|
|---|
| [b20de1d] | 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
|
|---|
| [5da7199] | 77 | * assumes the caller is the console and thus it stores IPC session to it for
|
|---|
| [b20de1d] | 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 | */
|
|---|
| 84 | static 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");
|
|---|
| [05920d23] | 88 | if (fun == NULL || fun->driver_data == NULL) {
|
|---|
| [b20de1d] | 89 | async_answer_0(icallid, EINVAL);
|
|---|
| 90 | return;
|
|---|
| 91 | }
|
|---|
| [571ba2a] | 92 |
|
|---|
| [05920d23] | 93 | usb_multimedia_t *multim_dev = fun->driver_data;
|
|---|
| 94 |
|
|---|
| [5da7199] | 95 | async_sess_t *sess =
|
|---|
| 96 | async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
|
|---|
| 97 | if (sess != NULL) {
|
|---|
| 98 | if (multim_dev->console_sess == NULL) {
|
|---|
| 99 | multim_dev->console_sess = sess;
|
|---|
| 100 | usb_log_debug(NAME " Saved session to console: %p\n",
|
|---|
| 101 | sess);
|
|---|
| 102 | async_answer_0(icallid, EOK);
|
|---|
| 103 | } else
|
|---|
| 104 | async_answer_0(icallid, ELIMIT);
|
|---|
| 105 | } else
|
|---|
| 106 | async_answer_0(icallid, EINVAL);
|
|---|
| [b20de1d] | 107 | }
|
|---|
| 108 | /*----------------------------------------------------------------------------*/
|
|---|
| [d3b6d5e] | 109 | static ddf_dev_ops_t multimedia_ops = {
|
|---|
| [b20de1d] | 110 | .default_handler = default_connection_handler
|
|---|
| [e3b5129] | 111 | };
|
|---|
| [b20de1d] | 112 | /*----------------------------------------------------------------------------*/
|
|---|
| 113 | /**
|
|---|
| 114 | * Processes key events.
|
|---|
| 115 | *
|
|---|
| 116 | * @note This function was copied from AT keyboard driver and modified to suit
|
|---|
| 117 | * USB keyboard.
|
|---|
| 118 | *
|
|---|
| 119 | * @note Lock keys are not sent to the console, as they are completely handled
|
|---|
| 120 | * in the driver. It may, however, be required later that the driver
|
|---|
| 121 | * sends also these keys to application (otherwise it cannot use those
|
|---|
| 122 | * keys at all).
|
|---|
| [cf99c4c] | 123 | *
|
|---|
| 124 | * @param hid_dev
|
|---|
| 125 | * @param multim_dev
|
|---|
| 126 | * @param type Type of the event (press / release). Recognized values:
|
|---|
| [b20de1d] | 127 | * KEY_PRESS, KEY_RELEASE
|
|---|
| 128 | * @param key Key code of the key according to HID Usage Tables.
|
|---|
| 129 | */
|
|---|
| [05920d23] | 130 | static void usb_multimedia_push_ev(
|
|---|
| [65b458c4] | 131 | usb_multimedia_t *multim_dev, int type, unsigned int key)
|
|---|
| [e3b5129] | 132 | {
|
|---|
| [65b458c4] | 133 | assert(multim_dev != NULL);
|
|---|
| [571ba2a] | 134 |
|
|---|
| [cf99c4c] | 135 | const kbd_event_t ev = {
|
|---|
| 136 | .type = type,
|
|---|
| 137 | .key = key,
|
|---|
| 138 | .mods = 0,
|
|---|
| 139 | .c = 0,
|
|---|
| 140 | };
|
|---|
| [b20de1d] | 141 |
|
|---|
| 142 | usb_log_debug2(NAME " Sending key %d to the console\n", ev.key);
|
|---|
| [5da7199] | 143 | if (multim_dev->console_sess == NULL) {
|
|---|
| [b20de1d] | 144 | usb_log_warning(
|
|---|
| 145 | "Connection to console not ready, key discarded.\n");
|
|---|
| 146 | return;
|
|---|
| 147 | }
|
|---|
| [571ba2a] | 148 |
|
|---|
| [5da7199] | 149 | async_exch_t *exch = async_exchange_begin(multim_dev->console_sess);
|
|---|
| [05920d23] | 150 | if (exch != NULL) {
|
|---|
| 151 | async_msg_4(exch, KBDEV_EVENT, ev.type, ev.key, ev.mods, ev.c);
|
|---|
| 152 | async_exchange_end(exch);
|
|---|
| 153 | } else {
|
|---|
| 154 | usb_log_warning("Failed to send multimedia key.\n");
|
|---|
| 155 | }
|
|---|
| [b20de1d] | 156 | }
|
|---|
| 157 | /*----------------------------------------------------------------------------*/
|
|---|
| [ce2a1c2] | 158 | int usb_multimedia_init(struct usb_hid_dev *hid_dev, void **data)
|
|---|
| [31cfee16] | 159 | {
|
|---|
| [ce2a1c2] | 160 | if (hid_dev == NULL || hid_dev->usb_dev == NULL) {
|
|---|
| [cf99c4c] | 161 | return EINVAL;
|
|---|
| [ce2a1c2] | 162 | }
|
|---|
| 163 |
|
|---|
| 164 | usb_log_debug(NAME " Initializing HID/multimedia structure...\n");
|
|---|
| 165 |
|
|---|
| [15f3c3f] | 166 | /* Create the exposed function. */
|
|---|
| [ce2a1c2] | 167 | ddf_fun_t *fun = ddf_fun_create(
|
|---|
| 168 | hid_dev->usb_dev->ddf_dev, fun_exposed, NAME);
|
|---|
| [31cfee16] | 169 | if (fun == NULL) {
|
|---|
| 170 | usb_log_error("Could not create DDF function node.\n");
|
|---|
| 171 | return ENOMEM;
|
|---|
| 172 | }
|
|---|
| [571ba2a] | 173 |
|
|---|
| [d3b6d5e] | 174 | fun->ops = &multimedia_ops;
|
|---|
| [ce2a1c2] | 175 |
|
|---|
| 176 | usb_multimedia_t *multim_dev =
|
|---|
| 177 | ddf_fun_data_alloc(fun, sizeof(usb_multimedia_t));
|
|---|
| 178 | if (multim_dev == NULL) {
|
|---|
| 179 | ddf_fun_destroy(fun);
|
|---|
| 180 | return ENOMEM;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | multim_dev->console_sess = NULL;
|
|---|
| 184 |
|
|---|
| 185 | //todo Autorepeat?
|
|---|
| [571ba2a] | 186 |
|
|---|
| [31cfee16] | 187 | int rc = ddf_fun_bind(fun);
|
|---|
| 188 | if (rc != EOK) {
|
|---|
| 189 | usb_log_error("Could not bind DDF function: %s.\n",
|
|---|
| 190 | str_error(rc));
|
|---|
| 191 | ddf_fun_destroy(fun);
|
|---|
| 192 | return rc;
|
|---|
| 193 | }
|
|---|
| [571ba2a] | 194 |
|
|---|
| [cf99c4c] | 195 | usb_log_debug(NAME " function created (handle: %" PRIun ").\n",
|
|---|
| 196 | fun->handle);
|
|---|
| [571ba2a] | 197 |
|
|---|
| [1dc4a5e] | 198 | rc = ddf_fun_add_to_category(fun, "keyboard");
|
|---|
| [31cfee16] | 199 | if (rc != EOK) {
|
|---|
| 200 | usb_log_error(
|
|---|
| [1dc4a5e] | 201 | "Could not add DDF function to category 'keyboard': %s.\n",
|
|---|
| [31cfee16] | 202 | str_error(rc));
|
|---|
| [a0c05e7] | 203 | if (ddf_fun_unbind(fun) != EOK) {
|
|---|
| 204 | usb_log_error("Failed to unbind %s, won't destroy.\n",
|
|---|
| 205 | fun->name);
|
|---|
| 206 | } else {
|
|---|
| 207 | ddf_fun_destroy(fun);
|
|---|
| 208 | }
|
|---|
| [31cfee16] | 209 | return rc;
|
|---|
| 210 | }
|
|---|
| [571ba2a] | 211 |
|
|---|
| [ce2a1c2] | 212 | /* Save the KBD device structure into the HID device structure. */
|
|---|
| [05920d23] | 213 | *data = fun;
|
|---|
| [571ba2a] | 214 |
|
|---|
| [d3b6d5e] | 215 | usb_log_debug(NAME " HID/multimedia structure initialized.\n");
|
|---|
| [b20de1d] | 216 | return EOK;
|
|---|
| 217 | }
|
|---|
| 218 | /*----------------------------------------------------------------------------*/
|
|---|
| [65b458c4] | 219 | void usb_multimedia_deinit(struct usb_hid_dev *hid_dev, void *data)
|
|---|
| [b20de1d] | 220 | {
|
|---|
| [05920d23] | 221 | ddf_fun_t *fun = data;
|
|---|
| 222 | if (fun != NULL && fun->driver_data != NULL) {
|
|---|
| 223 | usb_multimedia_t *multim_dev = fun->driver_data;
|
|---|
| [cf99c4c] | 224 | /* Hangup session to the console */
|
|---|
| 225 | if (multim_dev->console_sess)
|
|---|
| 226 | async_hangup(multim_dev->console_sess);
|
|---|
| [a0c05e7] | 227 | if (ddf_fun_unbind(fun) != EOK) {
|
|---|
| 228 | usb_log_error("Failed to unbind %s, won't destroy.\n",
|
|---|
| [05920d23] | 229 | fun->name);
|
|---|
| [af8c9b54] | 230 | } else {
|
|---|
| [05920d23] | 231 | usb_log_debug2("%s unbound.\n", fun->name);
|
|---|
| [cf99c4c] | 232 | /* This frees multim_dev too as it was stored in
|
|---|
| 233 | * fun->data */
|
|---|
| [05920d23] | 234 | ddf_fun_destroy(fun);
|
|---|
| [af8c9b54] | 235 | }
|
|---|
| [05920d23] | 236 | } else {
|
|---|
| 237 | usb_log_error(
|
|---|
| 238 | "Failed to deinit multimedia subdriver, data missing.\n");
|
|---|
| [b20de1d] | 239 | }
|
|---|
| [e3b5129] | 240 | }
|
|---|
| 241 | /*----------------------------------------------------------------------------*/
|
|---|
| [4d3c13e] | 242 | bool usb_multimedia_polling_callback(struct usb_hid_dev *hid_dev, void *data)
|
|---|
| [e3b5129] | 243 | {
|
|---|
| 244 | // TODO: checks
|
|---|
| [05920d23] | 245 | ddf_fun_t *fun = data;
|
|---|
| 246 | if (hid_dev == NULL || fun == NULL || fun->driver_data == NULL) {
|
|---|
| [19e0560e] | 247 | return false;
|
|---|
| 248 | }
|
|---|
| [e3b5129] | 249 |
|
|---|
| [05920d23] | 250 | usb_multimedia_t *multim_dev = fun->driver_data;
|
|---|
| [571ba2a] | 251 |
|
|---|
| [e3b5129] | 252 | usb_hid_report_path_t *path = usb_hid_report_path();
|
|---|
| [05920d23] | 253 | if (path == NULL)
|
|---|
| 254 | return true; /* This might be a temporary failure. */
|
|---|
| 255 |
|
|---|
| [cf99c4c] | 256 | int ret =
|
|---|
| 257 | usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_CONSUMER, 0);
|
|---|
| [05920d23] | 258 | if (ret != EOK) {
|
|---|
| 259 | usb_hid_report_path_free(path);
|
|---|
| [cf99c4c] | 260 | return true; /* This might be a temporary failure. */
|
|---|
| [05920d23] | 261 | }
|
|---|
| [cfbbe1d3] | 262 |
|
|---|
| [65c3794] | 263 | usb_hid_report_path_set_report_id(path, hid_dev->report_id);
|
|---|
| [e50cd7f] | 264 |
|
|---|
| [6513110] | 265 | usb_hid_report_field_t *field = usb_hid_report_get_sibling(
|
|---|
| [a8c4e871] | 266 | &hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END
|
|---|
| 267 | | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
|
|---|
| [6513110] | 268 | USB_HID_REPORT_TYPE_INPUT);
|
|---|
| [19e0560e] | 269 |
|
|---|
| [05920d23] | 270 | //FIXME Is this iterating OK if done multiple times?
|
|---|
| 271 | //FIXME The parsing is not OK. (what's wrong?)
|
|---|
| [6513110] | 272 | while (field != NULL) {
|
|---|
| [05920d23] | 273 | if (field->value != 0) {
|
|---|
| 274 | usb_log_debug(NAME " KEY VALUE(%X) USAGE(%X)\n",
|
|---|
| [97cb542] | 275 | field->value, field->usage);
|
|---|
| [cf99c4c] | 276 | const unsigned key =
|
|---|
| [97cb542] | 277 | usb_multimedia_map_usage(field->usage);
|
|---|
| [cf99c4c] | 278 | const char *key_str =
|
|---|
| [d7c72db] | 279 | usbhid_multimedia_usage_to_str(field->usage);
|
|---|
| [d59d0bb] | 280 | usb_log_info("Pressed key: %s\n", key_str);
|
|---|
| [05920d23] | 281 | usb_multimedia_push_ev(multim_dev, KEY_PRESS, key);
|
|---|
| [3a6e423] | 282 | }
|
|---|
| [571ba2a] | 283 |
|
|---|
| [b20de1d] | 284 | field = usb_hid_report_get_sibling(
|
|---|
| [a8c4e871] | 285 | &hid_dev->report, field, path, USB_HID_PATH_COMPARE_END
|
|---|
| [cf99c4c] | 286 | | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
|
|---|
| [b20de1d] | 287 | USB_HID_REPORT_TYPE_INPUT);
|
|---|
| [571ba2a] | 288 | }
|
|---|
| [e3b5129] | 289 |
|
|---|
| 290 | usb_hid_report_path_free(path);
|
|---|
| [571ba2a] | 291 |
|
|---|
| [e3b5129] | 292 | return true;
|
|---|
| 293 | }
|
|---|
| 294 | /**
|
|---|
| 295 | * @}
|
|---|
| 296 | */
|
|---|