[9c0242b] | 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 <io/console.h>
|
---|
| 43 | #include <io/keycode.h>
|
---|
| 44 | #include <ipc/kbdev.h>
|
---|
[cc574511] | 45 | #include <loc.h>
|
---|
[9c0242b] | 46 | #include <stdlib.h>
|
---|
| 47 | #include <vfs/vfs_sess.h>
|
---|
[6d605e6] | 48 | #include <sys/typefmt.h>
|
---|
[b6a088f] | 49 | #include "../gsp.h"
|
---|
| 50 | #include "../input.h"
|
---|
| 51 | #include "../kbd.h"
|
---|
| 52 | #include "../kbd_ctl.h"
|
---|
| 53 | #include "../kbd_port.h"
|
---|
[9c0242b] | 54 |
|
---|
| 55 | static int kbdev_ctl_init(kbd_dev_t *);
|
---|
[1875a0c] | 56 | static void kbdev_ctl_set_ind(kbd_dev_t *, unsigned int);
|
---|
[9c0242b] | 57 |
|
---|
[9934f7d] | 58 | static void kbdev_callback_conn(ipc_callid_t, ipc_call_t *, void *arg);
|
---|
[9c0242b] | 59 |
|
---|
| 60 | kbd_ctl_ops_t kbdev_ctl = {
|
---|
[1875a0c] | 61 | .parse = NULL,
|
---|
[9c0242b] | 62 | .init = kbdev_ctl_init,
|
---|
| 63 | .set_ind = kbdev_ctl_set_ind
|
---|
| 64 | };
|
---|
| 65 |
|
---|
| 66 | /** Kbdev softstate */
|
---|
| 67 | typedef struct {
|
---|
| 68 | /** Link to generic keyboard device */
|
---|
| 69 | kbd_dev_t *kbd_dev;
|
---|
[3123d2a] | 70 |
|
---|
[9c0242b] | 71 | /** Session with kbdev device */
|
---|
| 72 | async_sess_t *sess;
|
---|
| 73 | } kbdev_t;
|
---|
| 74 |
|
---|
| 75 | static kbdev_t *kbdev_new(kbd_dev_t *kdev)
|
---|
| 76 | {
|
---|
[3123d2a] | 77 | kbdev_t *kbdev = calloc(1, sizeof(kbdev_t));
|
---|
[9c0242b] | 78 | if (kbdev == NULL)
|
---|
| 79 | return NULL;
|
---|
[3123d2a] | 80 |
|
---|
[9c0242b] | 81 | kbdev->kbd_dev = kdev;
|
---|
| 82 |
|
---|
| 83 | return kbdev;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | static void kbdev_destroy(kbdev_t *kbdev)
|
---|
| 87 | {
|
---|
| 88 | if (kbdev->sess != NULL)
|
---|
| 89 | async_hangup(kbdev->sess);
|
---|
[3123d2a] | 90 |
|
---|
[9c0242b] | 91 | free(kbdev);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | static int kbdev_ctl_init(kbd_dev_t *kdev)
|
---|
| 95 | {
|
---|
[3123d2a] | 96 | async_sess_t *sess = loc_service_connect(EXCHANGE_SERIALIZE,
|
---|
| 97 | kdev->svc_id, 0);
|
---|
[9c0242b] | 98 | if (sess == NULL) {
|
---|
[cc574511] | 99 | printf("%s: Failed starting session with '%s.'\n", NAME,
|
---|
[cce8a83] | 100 | kdev->svc_name);
|
---|
[3123d2a] | 101 | return ENOENT;
|
---|
[9c0242b] | 102 | }
|
---|
[3123d2a] | 103 |
|
---|
| 104 | kbdev_t *kbdev = kbdev_new(kdev);
|
---|
[9c0242b] | 105 | if (kbdev == NULL) {
|
---|
[1875a0c] | 106 | printf("%s: Failed allocating device structure for '%s'.\n",
|
---|
[cce8a83] | 107 | NAME, kdev->svc_name);
|
---|
[e9563c3] | 108 | async_hangup(sess);
|
---|
[3123d2a] | 109 | return ENOMEM;
|
---|
[9c0242b] | 110 | }
|
---|
[3123d2a] | 111 |
|
---|
[9c0242b] | 112 | kbdev->sess = sess;
|
---|
[3123d2a] | 113 |
|
---|
| 114 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
[9c0242b] | 115 | if (exch == NULL) {
|
---|
[cc574511] | 116 | printf("%s: Failed starting exchange with '%s'.\n", NAME,
|
---|
[cce8a83] | 117 | kdev->svc_name);
|
---|
[9c0242b] | 118 | kbdev_destroy(kbdev);
|
---|
[3123d2a] | 119 | return ENOENT;
|
---|
[9c0242b] | 120 | }
|
---|
[3123d2a] | 121 |
|
---|
| 122 | int rc = async_connect_to_me(exch, 0, 0, 0, kbdev_callback_conn, kbdev);
|
---|
[9c0242b] | 123 | if (rc != EOK) {
|
---|
[1875a0c] | 124 | printf("%s: Failed creating callback connection from '%s'.\n",
|
---|
[cce8a83] | 125 | NAME, kdev->svc_name);
|
---|
[9c0242b] | 126 | async_exchange_end(exch);
|
---|
| 127 | kbdev_destroy(kbdev);
|
---|
[3123d2a] | 128 | return rc;
|
---|
[9c0242b] | 129 | }
|
---|
[3123d2a] | 130 |
|
---|
[9c0242b] | 131 | async_exchange_end(exch);
|
---|
[3123d2a] | 132 |
|
---|
[9c0242b] | 133 | kdev->ctl_private = (void *) kbdev;
|
---|
| 134 | return 0;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | static void kbdev_ctl_set_ind(kbd_dev_t *kdev, unsigned mods)
|
---|
| 138 | {
|
---|
[3123d2a] | 139 | async_sess_t *sess = ((kbdev_t *) kdev->ctl_private)->sess;
|
---|
| 140 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
[9c0242b] | 141 | if (!exch)
|
---|
| 142 | return;
|
---|
[3123d2a] | 143 |
|
---|
[9c0242b] | 144 | async_msg_1(exch, KBDEV_SET_IND, mods);
|
---|
| 145 | async_exchange_end(exch);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[9934f7d] | 148 | static void kbdev_callback_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
[9c0242b] | 149 | {
|
---|
| 150 | kbdev_t *kbdev;
|
---|
| 151 | int retval;
|
---|
| 152 | int type, key;
|
---|
| 153 |
|
---|
[9934f7d] | 154 | /* Kbdev device structure */
|
---|
| 155 | kbdev = arg;
|
---|
[9c0242b] | 156 |
|
---|
| 157 | while (true) {
|
---|
| 158 | ipc_call_t call;
|
---|
| 159 | ipc_callid_t callid;
|
---|
| 160 |
|
---|
| 161 | callid = async_get_call(&call);
|
---|
| 162 | if (!IPC_GET_IMETHOD(call)) {
|
---|
[e9563c3] | 163 | kbdev_destroy(kbdev);
|
---|
[9c0242b] | 164 | return;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | switch (IPC_GET_IMETHOD(call)) {
|
---|
| 168 | case KBDEV_EVENT:
|
---|
| 169 | /* Got event from keyboard device */
|
---|
| 170 | retval = 0;
|
---|
| 171 | type = IPC_GET_ARG1(call);
|
---|
| 172 | key = IPC_GET_ARG2(call);
|
---|
[1875a0c] | 173 | kbd_push_event(kbdev->kbd_dev, type, key);
|
---|
[9c0242b] | 174 | break;
|
---|
| 175 | default:
|
---|
| 176 | retval = ENOTSUP;
|
---|
| 177 | break;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | async_answer_0(callid, retval);
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | /**
|
---|
| 185 | * @}
|
---|
| 186 | */
|
---|