| [9f51afc] | 1 | /*
|
|---|
| [9be360ee] | 2 | * Copyright (c) 2011 Jiri Svoboda
|
|---|
| [9f51afc] | 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_port
|
|---|
| 30 | * @ingroup kbd
|
|---|
| 31 | * @{
|
|---|
| [79ae36dd] | 32 | */
|
|---|
| [9f51afc] | 33 | /** @file
|
|---|
| 34 | * @brief Chardev keyboard port driver.
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <ipc/char.h>
|
|---|
| 38 | #include <async.h>
|
|---|
| [854eddd6] | 39 | #include <input.h>
|
|---|
| [9f51afc] | 40 | #include <kbd_port.h>
|
|---|
| 41 | #include <kbd.h>
|
|---|
| [15f3c3f] | 42 | #include <loc.h>
|
|---|
| [9f51afc] | 43 | #include <errno.h>
|
|---|
| [79ae36dd] | 44 | #include <stdio.h>
|
|---|
| 45 |
|
|---|
| [9934f7d] | 46 | static void kbd_port_events(ipc_callid_t iid, ipc_call_t *icall, void *arg);
|
|---|
| [9f51afc] | 47 |
|
|---|
| [9be360ee] | 48 | static int chardev_port_init(kbd_dev_t *);
|
|---|
| [b1bdc7a4] | 49 | static void chardev_port_yield(void);
|
|---|
| 50 | static void chardev_port_reclaim(void);
|
|---|
| 51 | static void chardev_port_write(uint8_t data);
|
|---|
| 52 |
|
|---|
| 53 | kbd_port_ops_t chardev_port = {
|
|---|
| 54 | .init = chardev_port_init,
|
|---|
| 55 | .yield = chardev_port_yield,
|
|---|
| 56 | .reclaim = chardev_port_reclaim,
|
|---|
| 57 | .write = chardev_port_write
|
|---|
| 58 | };
|
|---|
| 59 |
|
|---|
| [9be360ee] | 60 | static kbd_dev_t *kbd_dev;
|
|---|
| [a40dea3] | 61 | static async_sess_t *dev_sess;
|
|---|
| [9f51afc] | 62 |
|
|---|
| [a9b5b5f] | 63 | /** List of devices to try connecting to. */
|
|---|
| 64 | static const char *in_devs[] = {
|
|---|
| [79ae36dd] | 65 | "char/s3c24ser"
|
|---|
| [a9b5b5f] | 66 | };
|
|---|
| 67 |
|
|---|
| [79ae36dd] | 68 | static const unsigned int num_devs = sizeof(in_devs) / sizeof(in_devs[0]);
|
|---|
| [a9b5b5f] | 69 |
|
|---|
| [9be360ee] | 70 | static int chardev_port_init(kbd_dev_t *kdev)
|
|---|
| [9f51afc] | 71 | {
|
|---|
| [15f3c3f] | 72 | service_id_t service_id;
|
|---|
| [a40dea3] | 73 | async_exch_t *exch;
|
|---|
| [79ae36dd] | 74 | unsigned int i;
|
|---|
| 75 | int rc;
|
|---|
| 76 |
|
|---|
| [9be360ee] | 77 | kbd_dev = kdev;
|
|---|
| 78 |
|
|---|
| [a9b5b5f] | 79 | for (i = 0; i < num_devs; i++) {
|
|---|
| [15f3c3f] | 80 | rc = loc_service_get_id(in_devs[i], &service_id, 0);
|
|---|
| [79ae36dd] | 81 | if (rc == EOK)
|
|---|
| [a9b5b5f] | 82 | break;
|
|---|
| 83 | }
|
|---|
| [79ae36dd] | 84 |
|
|---|
| [a9b5b5f] | 85 | if (i >= num_devs) {
|
|---|
| [79ae36dd] | 86 | printf("%s: Could not find any suitable input device\n", NAME);
|
|---|
| [a9b5b5f] | 87 | return -1;
|
|---|
| [9f51afc] | 88 | }
|
|---|
| [79ae36dd] | 89 |
|
|---|
| [15f3c3f] | 90 | dev_sess = loc_service_connect(EXCHANGE_ATOMIC, service_id,
|
|---|
| [a40dea3] | 91 | IPC_FLAG_BLOCKING);
|
|---|
| 92 | if (dev_sess == NULL) {
|
|---|
| [79ae36dd] | 93 | printf("%s: Failed connecting to device\n", NAME);
|
|---|
| 94 | return ENOENT;
|
|---|
| [9f51afc] | 95 | }
|
|---|
| [79ae36dd] | 96 |
|
|---|
| [a40dea3] | 97 | exch = async_exchange_begin(dev_sess);
|
|---|
| 98 | if (exch == NULL) {
|
|---|
| 99 | printf("%s: Failed starting exchange with device\n", NAME);
|
|---|
| 100 | async_hangup(dev_sess);
|
|---|
| 101 | return ENOMEM;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| [9f51afc] | 104 | /* NB: The callback connection is slotted for removal */
|
|---|
| [a40dea3] | 105 | rc = async_connect_to_me(exch, 0, 0, 0, kbd_port_events, NULL);
|
|---|
| 106 | async_exchange_end(exch);
|
|---|
| 107 |
|
|---|
| 108 | if (rc != 0) {
|
|---|
| [1875a0c] | 109 | printf("%s: Failed to create callback from device\n", NAME);
|
|---|
| [a40dea3] | 110 | async_hangup(dev_sess);
|
|---|
| [a9b5b5f] | 111 | return -1;
|
|---|
| [9f51afc] | 112 | }
|
|---|
| [a40dea3] | 113 |
|
|---|
| [9f51afc] | 114 | return 0;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| [b1bdc7a4] | 117 | static void chardev_port_yield(void)
|
|---|
| [9f51afc] | 118 | {
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| [b1bdc7a4] | 121 | static void chardev_port_reclaim(void)
|
|---|
| [9f51afc] | 122 | {
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| [b1bdc7a4] | 125 | static void chardev_port_write(uint8_t data)
|
|---|
| [9f51afc] | 126 | {
|
|---|
| [a40dea3] | 127 | async_exch_t *exch = async_exchange_begin(dev_sess);
|
|---|
| 128 | if (exch == NULL) {
|
|---|
| 129 | printf("%s: Failed starting exchange with device\n", NAME);
|
|---|
| 130 | return;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | async_msg_1(exch, CHAR_WRITE_BYTE, data);
|
|---|
| 134 | async_exchange_end(exch);
|
|---|
| [9f51afc] | 135 | }
|
|---|
| 136 |
|
|---|
| [9934f7d] | 137 | static void kbd_port_events(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
|---|
| [9f51afc] | 138 | {
|
|---|
| 139 | /* Ignore parameters, the connection is already opened */
|
|---|
| 140 | while (true) {
|
|---|
| 141 |
|
|---|
| 142 | ipc_call_t call;
|
|---|
| 143 | ipc_callid_t callid = async_get_call(&call);
|
|---|
| [79ae36dd] | 144 |
|
|---|
| 145 | if (!IPC_GET_IMETHOD(call)) {
|
|---|
| 146 | /* TODO: Handle hangup */
|
|---|
| 147 | return;
|
|---|
| 148 | }
|
|---|
| [9f51afc] | 149 |
|
|---|
| 150 | int retval;
|
|---|
| 151 |
|
|---|
| [228e490] | 152 | switch (IPC_GET_IMETHOD(call)) {
|
|---|
| [9f51afc] | 153 | case CHAR_NOTIF_BYTE:
|
|---|
| [1875a0c] | 154 | kbd_push_data(kbd_dev, IPC_GET_ARG1(call));
|
|---|
| [9f51afc] | 155 | break;
|
|---|
| 156 | default:
|
|---|
| 157 | retval = ENOENT;
|
|---|
| 158 | }
|
|---|
| [ffa2c8ef] | 159 | async_answer_0(callid, retval);
|
|---|
| [9f51afc] | 160 | }
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 | /**
|
|---|
| 165 | * @}
|
|---|
| 166 | */
|
|---|