[6d15572] | 1 | /*
|
---|
| 2 | * Copyright (c) 2005 Jakub Jermar
|
---|
| 3 | * Copyright (c) 2011 Jiri Svoboda
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | /** @file Ski console driver.
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | #include <ddf/driver.h>
|
---|
| 34 | #include <ddf/log.h>
|
---|
| 35 | #include <errno.h>
|
---|
| 36 | #include <ipc/char.h>
|
---|
| 37 | #include <stdint.h>
|
---|
| 38 | #include <stdlib.h>
|
---|
| 39 | #include <thread.h>
|
---|
| 40 | #include <stdbool.h>
|
---|
| 41 |
|
---|
| 42 | #include "ski-con.h"
|
---|
| 43 |
|
---|
| 44 | #define SKI_GETCHAR 21
|
---|
| 45 |
|
---|
| 46 | #define POLL_INTERVAL 10000
|
---|
| 47 |
|
---|
| 48 | static void ski_con_thread_impl(void *arg);
|
---|
| 49 | static int32_t ski_con_getchar(void);
|
---|
| 50 | static void ski_con_connection(ipc_callid_t, ipc_call_t *, void *);
|
---|
| 51 |
|
---|
[676e833] | 52 | /** Add ski console device. */
|
---|
[6d15572] | 53 | int ski_con_add(ski_con_t *con)
|
---|
| 54 | {
|
---|
| 55 | thread_id_t tid;
|
---|
| 56 | ddf_fun_t *fun = NULL;
|
---|
| 57 | bool bound = false;
|
---|
| 58 | int rc;
|
---|
| 59 |
|
---|
| 60 | fun = ddf_fun_create(con->dev, fun_exposed, "a");
|
---|
| 61 | if (fun == NULL) {
|
---|
| 62 | ddf_msg(LVL_ERROR, "Error creating function 'a'.");
|
---|
| 63 | rc = ENOMEM;
|
---|
| 64 | goto error;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | ddf_fun_set_conn_handler(fun, ski_con_connection);
|
---|
| 68 |
|
---|
| 69 | rc = ddf_fun_bind(fun);
|
---|
| 70 | if (rc != EOK) {
|
---|
| 71 | ddf_msg(LVL_ERROR, "Error binding function 'a'.");
|
---|
| 72 | goto error;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | bound = true;
|
---|
| 76 |
|
---|
| 77 | rc = thread_create(ski_con_thread_impl, con, "kbd_poll", &tid);
|
---|
| 78 | if (rc != 0) {
|
---|
| 79 | return rc;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | return EOK;
|
---|
| 83 | error:
|
---|
| 84 | if (bound)
|
---|
| 85 | ddf_fun_unbind(fun);
|
---|
| 86 | if (fun != NULL)
|
---|
| 87 | ddf_fun_destroy(fun);
|
---|
| 88 |
|
---|
| 89 | return rc;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | /** Remove ski console device */
|
---|
| 93 | int ski_con_remove(ski_con_t *con)
|
---|
| 94 | {
|
---|
| 95 | return ENOTSUP;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /** Ski console device gone */
|
---|
| 99 | int ski_con_gone(ski_con_t *con)
|
---|
| 100 | {
|
---|
| 101 | return ENOTSUP;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | /** Thread to poll Ski for keypresses. */
|
---|
| 105 | static void ski_con_thread_impl(void *arg)
|
---|
| 106 | {
|
---|
| 107 | int32_t c;
|
---|
| 108 | ski_con_t *con = (ski_con_t *) arg;
|
---|
| 109 |
|
---|
| 110 | while (1) {
|
---|
| 111 | while (1) {
|
---|
| 112 | c = ski_con_getchar();
|
---|
| 113 | if (c == 0)
|
---|
| 114 | break;
|
---|
| 115 |
|
---|
| 116 | if (con->client_sess != NULL) {
|
---|
| 117 | async_exch_t *exch = async_exchange_begin(con->client_sess);
|
---|
| 118 | async_msg_1(exch, CHAR_NOTIF_BYTE, c);
|
---|
| 119 | async_exchange_end(exch);
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | thread_usleep(POLL_INTERVAL);
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | /** Ask Ski if a key was pressed.
|
---|
| 128 | *
|
---|
| 129 | * Use SSC (Simulator System Call) to get character from the debug console.
|
---|
| 130 | * This call is non-blocking.
|
---|
| 131 | *
|
---|
| 132 | * @return ASCII code of pressed key or 0 if no key pressed.
|
---|
| 133 | */
|
---|
| 134 | static int32_t ski_con_getchar(void)
|
---|
| 135 | {
|
---|
| 136 | uint64_t ch;
|
---|
| 137 |
|
---|
| 138 | #ifdef UARCH_ia64
|
---|
| 139 | asm volatile (
|
---|
| 140 | "mov r15 = %1\n"
|
---|
| 141 | "break 0x80000;;\n" /* modifies r8 */
|
---|
| 142 | "mov %0 = r8;;\n"
|
---|
| 143 |
|
---|
| 144 | : "=r" (ch)
|
---|
| 145 | : "i" (SKI_GETCHAR)
|
---|
| 146 | : "r15", "r8"
|
---|
| 147 | );
|
---|
| 148 | #else
|
---|
| 149 | ch = 0;
|
---|
| 150 | #endif
|
---|
| 151 | return (int32_t) ch;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | static void ski_con_putchar(ski_con_t *con, char ch)
|
---|
| 155 | {
|
---|
| 156 |
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | /** Character device connection handler. */
|
---|
| 160 | static void ski_con_connection(ipc_callid_t iid, ipc_call_t *icall,
|
---|
| 161 | void *arg)
|
---|
| 162 | {
|
---|
| 163 | ski_con_t *con;
|
---|
| 164 |
|
---|
| 165 | /* Answer the IPC_M_CONNECT_ME_TO call. */
|
---|
| 166 | async_answer_0(iid, EOK);
|
---|
| 167 |
|
---|
| 168 | con = (ski_con_t *)ddf_dev_data_get(ddf_fun_get_dev((ddf_fun_t *)arg));
|
---|
| 169 |
|
---|
| 170 | while (true) {
|
---|
| 171 | ipc_call_t call;
|
---|
| 172 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 173 | sysarg_t method = IPC_GET_IMETHOD(call);
|
---|
| 174 |
|
---|
| 175 | if (!method) {
|
---|
| 176 | /* The other side has hung up. */
|
---|
| 177 | async_answer_0(callid, EOK);
|
---|
| 178 | return;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | async_sess_t *sess =
|
---|
| 182 | async_callback_receive_start(EXCHANGE_SERIALIZE, &call);
|
---|
| 183 | if (sess != NULL) {
|
---|
| 184 | if (con->client_sess == NULL) {
|
---|
| 185 | con->client_sess = sess;
|
---|
| 186 | async_answer_0(callid, EOK);
|
---|
| 187 | } else
|
---|
| 188 | async_answer_0(callid, ELIMIT);
|
---|
| 189 | } else {
|
---|
| 190 | switch (method) {
|
---|
| 191 | case CHAR_WRITE_BYTE:
|
---|
| 192 | ddf_msg(LVL_DEBUG, "Write %" PRIun " to device\n",
|
---|
| 193 | IPC_GET_ARG1(call));
|
---|
| 194 | ski_con_putchar(con, (uint8_t) IPC_GET_ARG1(call));
|
---|
| 195 | async_answer_0(callid, EOK);
|
---|
| 196 | break;
|
---|
| 197 | default:
|
---|
| 198 | async_answer_0(callid, EINVAL);
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | /** @}
|
---|
| 205 | */
|
---|