| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Jiri Svoboda
|
|---|
| 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 kbd_ctl
|
|---|
| 30 | * @ingroup input
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /**
|
|---|
| 34 | * @file
|
|---|
| 35 | * @brief Keyboard device connector controller driver.
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include <async.h>
|
|---|
| 39 | #include <bool.h>
|
|---|
| 40 | #include <errno.h>
|
|---|
| 41 | #include <fcntl.h>
|
|---|
| 42 | #include <gsp.h>
|
|---|
| 43 | #include <io/console.h>
|
|---|
| 44 | #include <io/keycode.h>
|
|---|
| 45 | #include <ipc/kbdev.h>
|
|---|
| 46 | #include <kbd.h>
|
|---|
| 47 | #include <kbd_ctl.h>
|
|---|
| 48 | #include <kbd_port.h>
|
|---|
| 49 | #include <stdlib.h>
|
|---|
| 50 | #include <vfs/vfs_sess.h>
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 | static int kbdev_ctl_init(kbd_dev_t *);
|
|---|
| 54 | static void kbdev_ctl_set_ind(kbd_dev_t *, unsigned);
|
|---|
| 55 |
|
|---|
| 56 | static void kbdev_callback_conn(ipc_callid_t, ipc_call_t *);
|
|---|
| 57 |
|
|---|
| 58 | kbd_ctl_ops_t kbdev_ctl = {
|
|---|
| 59 | .parse_scancode = NULL,
|
|---|
| 60 | .init = kbdev_ctl_init,
|
|---|
| 61 | .set_ind = kbdev_ctl_set_ind
|
|---|
| 62 | };
|
|---|
| 63 |
|
|---|
| 64 | /** Kbdev softstate */
|
|---|
| 65 | typedef struct {
|
|---|
| 66 | /** Link to generic keyboard device */
|
|---|
| 67 | kbd_dev_t *kbd_dev;
|
|---|
| 68 |
|
|---|
| 69 | /** Session with kbdev device */
|
|---|
| 70 | async_sess_t *sess;
|
|---|
| 71 |
|
|---|
| 72 | /** File descriptor of open kbdev device */
|
|---|
| 73 | int fd;
|
|---|
| 74 | } kbdev_t;
|
|---|
| 75 |
|
|---|
| 76 | /** XXX Need to pass data from async_connect_to_me() to connection handler */
|
|---|
| 77 | static kbdev_t *unprotected_kbdev;
|
|---|
| 78 |
|
|---|
| 79 | static kbdev_t *kbdev_new(kbd_dev_t *kdev)
|
|---|
| 80 | {
|
|---|
| 81 | kbdev_t *kbdev;
|
|---|
| 82 |
|
|---|
| 83 | kbdev = calloc(1, sizeof(kbdev_t));
|
|---|
| 84 | if (kbdev == NULL)
|
|---|
| 85 | return NULL;
|
|---|
| 86 |
|
|---|
| 87 | kbdev->kbd_dev = kdev;
|
|---|
| 88 | kbdev->fd = -1;
|
|---|
| 89 |
|
|---|
| 90 | return kbdev;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | static void kbdev_destroy(kbdev_t *kbdev)
|
|---|
| 94 | {
|
|---|
| 95 | if (kbdev->sess != NULL)
|
|---|
| 96 | async_hangup(kbdev->sess);
|
|---|
| 97 | if (kbdev->fd >= 0)
|
|---|
| 98 | close(kbdev->fd);
|
|---|
| 99 | free(kbdev);
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | static int kbdev_ctl_init(kbd_dev_t *kdev)
|
|---|
| 103 | {
|
|---|
| 104 | const char *pathname;
|
|---|
| 105 | async_sess_t *sess;
|
|---|
| 106 | async_exch_t *exch;
|
|---|
| 107 | kbdev_t *kbdev;
|
|---|
| 108 | int fd;
|
|---|
| 109 | int rc;
|
|---|
| 110 |
|
|---|
| 111 | pathname = kdev->dev_path;
|
|---|
| 112 |
|
|---|
| 113 | fd = open(pathname, O_RDWR);
|
|---|
| 114 | if (fd < 0) {
|
|---|
| 115 | return -1;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | sess = fd_session(EXCHANGE_SERIALIZE, fd);
|
|---|
| 119 | if (sess == NULL) {
|
|---|
| 120 | printf(NAME ": Failed starting session with '%s'\n", pathname);
|
|---|
| 121 | close(fd);
|
|---|
| 122 | return -1;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | kbdev = kbdev_new(kdev);
|
|---|
| 126 | if (kbdev == NULL) {
|
|---|
| 127 | printf(NAME ": Failed allocating device structure for '%s'.\n",
|
|---|
| 128 | pathname);
|
|---|
| 129 | return -1;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | kbdev->fd = fd;
|
|---|
| 133 | kbdev->sess = sess;
|
|---|
| 134 |
|
|---|
| 135 | exch = async_exchange_begin(sess);
|
|---|
| 136 | if (exch == NULL) {
|
|---|
| 137 | printf(NAME ": Failed starting exchange with '%s'.\n", pathname);
|
|---|
| 138 | kbdev_destroy(kbdev);
|
|---|
| 139 | return -1;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | /*
|
|---|
| 143 | * XXX We need to pass kbdev to the connection handler. Since the
|
|---|
| 144 | * framework does not support this, use a global variable.
|
|---|
| 145 | * This needs to be fixed ASAP.
|
|---|
| 146 | */
|
|---|
| 147 | unprotected_kbdev = kbdev;
|
|---|
| 148 |
|
|---|
| 149 | rc = async_connect_to_me(exch, 0, 0, 0, kbdev_callback_conn);
|
|---|
| 150 | if (rc != EOK) {
|
|---|
| 151 | printf(NAME ": Failed creating callback connection from '%s'.\n",
|
|---|
| 152 | pathname);
|
|---|
| 153 | async_exchange_end(exch);
|
|---|
| 154 | kbdev_destroy(kbdev);
|
|---|
| 155 | return -1;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | async_exchange_end(exch);
|
|---|
| 159 |
|
|---|
| 160 | kdev->ctl_private = (void *) kbdev;
|
|---|
| 161 | return 0;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | static void kbdev_ctl_set_ind(kbd_dev_t *kdev, unsigned mods)
|
|---|
| 165 | {
|
|---|
| 166 | async_sess_t *sess;
|
|---|
| 167 | async_exch_t *exch;
|
|---|
| 168 |
|
|---|
| 169 | sess = ((kbdev_t *) kdev->ctl_private)->sess;
|
|---|
| 170 |
|
|---|
| 171 | exch = async_exchange_begin(sess);
|
|---|
| 172 | if (!exch)
|
|---|
| 173 | return;
|
|---|
| 174 |
|
|---|
| 175 | async_msg_1(exch, KBDEV_SET_IND, mods);
|
|---|
| 176 | async_exchange_end(exch);
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | static void kbdev_callback_conn(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 180 | {
|
|---|
| 181 | kbdev_t *kbdev;
|
|---|
| 182 | int retval;
|
|---|
| 183 | int type, key;
|
|---|
| 184 |
|
|---|
| 185 | kbdev = unprotected_kbdev;
|
|---|
| 186 |
|
|---|
| 187 | while (true) {
|
|---|
| 188 | ipc_call_t call;
|
|---|
| 189 | ipc_callid_t callid;
|
|---|
| 190 |
|
|---|
| 191 | callid = async_get_call(&call);
|
|---|
| 192 | if (!IPC_GET_IMETHOD(call)) {
|
|---|
| 193 | /* XXX Handle hangup */
|
|---|
| 194 | return;
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | switch (IPC_GET_IMETHOD(call)) {
|
|---|
| 198 | case KBDEV_EVENT:
|
|---|
| 199 | /* Got event from keyboard device */
|
|---|
| 200 | retval = 0;
|
|---|
| 201 | type = IPC_GET_ARG1(call);
|
|---|
| 202 | key = IPC_GET_ARG2(call);
|
|---|
| 203 | kbd_push_ev(kbdev->kbd_dev, type, key);
|
|---|
| 204 | break;
|
|---|
| 205 | default:
|
|---|
| 206 | retval = ENOTSUP;
|
|---|
| 207 | break;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | async_answer_0(callid, retval);
|
|---|
| 211 | }
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | /**
|
|---|
| 215 | * @}
|
|---|
| 216 | */
|
|---|