[676e833] | 1 | /*
|
---|
| 2 | * Copyright (c) 2006 Josef Cejka
|
---|
| 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
|
---|
| 31 | * @brief Msim console driver.
|
---|
| 32 | */
|
---|
| 33 |
|
---|
| 34 | #include <async.h>
|
---|
| 35 | #include <ddf/driver.h>
|
---|
| 36 | #include <ddf/log.h>
|
---|
| 37 | #include <ddi.h>
|
---|
| 38 | #include <errno.h>
|
---|
[7a6065c] | 39 | #include <io/chardev_srv.h>
|
---|
[676e833] | 40 |
|
---|
| 41 | #include "msim-con.h"
|
---|
| 42 |
|
---|
[984a9ba] | 43 | static void msim_con_connection(ipc_call_t *, void *);
|
---|
[676e833] | 44 |
|
---|
[f2d88f3] | 45 | static errno_t msim_con_read(chardev_srv_t *, void *, size_t, size_t *,
|
---|
| 46 | chardev_flags_t);
|
---|
[b7fd2a0] | 47 | static errno_t msim_con_write(chardev_srv_t *, const void *, size_t, size_t *);
|
---|
[7a6065c] | 48 |
|
---|
| 49 | static chardev_ops_t msim_con_chardev_ops = {
|
---|
| 50 | .read = msim_con_read,
|
---|
| 51 | .write = msim_con_write
|
---|
| 52 | };
|
---|
| 53 |
|
---|
[92232331] | 54 | static irq_cmd_t msim_cmds_proto[] = {
|
---|
[676e833] | 55 | {
|
---|
| 56 | .cmd = CMD_PIO_READ_8,
|
---|
| 57 | .addr = (void *) 0, /* will be patched in run-time */
|
---|
| 58 | .dstarg = 2
|
---|
| 59 | },
|
---|
| 60 | {
|
---|
| 61 | .cmd = CMD_ACCEPT
|
---|
| 62 | }
|
---|
| 63 | };
|
---|
| 64 |
|
---|
[01c3bb4] | 65 | static void msim_irq_handler(ipc_call_t *call, void *arg)
|
---|
[676e833] | 66 | {
|
---|
| 67 | msim_con_t *con = (msim_con_t *) arg;
|
---|
| 68 | uint8_t c;
|
---|
[b7fd2a0] | 69 | errno_t rc;
|
---|
[7a6065c] | 70 |
|
---|
| 71 | fibril_mutex_lock(&con->buf_lock);
|
---|
[676e833] | 72 |
|
---|
[fafb8e5] | 73 | c = ipc_get_arg2(call);
|
---|
[7a6065c] | 74 | rc = circ_buf_push(&con->cbuf, &c);
|
---|
| 75 | if (rc != EOK)
|
---|
| 76 | ddf_msg(LVL_ERROR, "Buffer overrun");
|
---|
[7aa94304] | 77 |
|
---|
[7a6065c] | 78 | fibril_mutex_unlock(&con->buf_lock);
|
---|
| 79 | fibril_condvar_broadcast(&con->buf_cv);
|
---|
[676e833] | 80 | }
|
---|
| 81 |
|
---|
| 82 | /** Add msim console device. */
|
---|
[b7fd2a0] | 83 | errno_t msim_con_add(msim_con_t *con, msim_con_res_t *res)
|
---|
[676e833] | 84 | {
|
---|
| 85 | ddf_fun_t *fun = NULL;
|
---|
[92232331] | 86 | irq_cmd_t *msim_cmds = NULL;
|
---|
[b7fd2a0] | 87 | errno_t rc;
|
---|
[4f87a85a] | 88 | bool bound = false;
|
---|
[676e833] | 89 |
|
---|
[7a6065c] | 90 | circ_buf_init(&con->cbuf, con->buf, msim_con_buf_size, 1);
|
---|
| 91 | fibril_mutex_initialize(&con->buf_lock);
|
---|
| 92 | fibril_condvar_initialize(&con->buf_cv);
|
---|
| 93 |
|
---|
[eadaeae8] | 94 | con->irq_handle = CAP_NIL;
|
---|
| 95 |
|
---|
[92232331] | 96 | msim_cmds = malloc(sizeof(msim_cmds_proto));
|
---|
| 97 | if (msim_cmds == NULL) {
|
---|
| 98 | rc = ENOMEM;
|
---|
| 99 | goto error;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[7de5f12] | 102 | con->res = *res;
|
---|
| 103 |
|
---|
[676e833] | 104 | fun = ddf_fun_create(con->dev, fun_exposed, "a");
|
---|
| 105 | if (fun == NULL) {
|
---|
| 106 | ddf_msg(LVL_ERROR, "Error creating function 'a'.");
|
---|
| 107 | rc = ENOMEM;
|
---|
| 108 | goto error;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[c309b18] | 111 | rc = pio_enable((void *)res->base, 1, (void **) &con->out_reg);
|
---|
| 112 | if (rc != EOK) {
|
---|
| 113 | ddf_msg(LVL_ERROR, "Error enabling I/O");
|
---|
| 114 | goto error;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[676e833] | 117 | ddf_fun_set_conn_handler(fun, msim_con_connection);
|
---|
| 118 |
|
---|
[92232331] | 119 | con->irq_range[0].base = res->base;
|
---|
| 120 | con->irq_range[0].size = 1;
|
---|
| 121 |
|
---|
| 122 | memcpy(msim_cmds, msim_cmds_proto, sizeof(msim_cmds_proto));
|
---|
[7de5f12] | 123 | msim_cmds[0].addr = (void *) res->base;
|
---|
[92232331] | 124 |
|
---|
| 125 | con->irq_code.rangecount = 1;
|
---|
| 126 | con->irq_code.ranges = con->irq_range;
|
---|
| 127 | con->irq_code.cmdcount = sizeof(msim_cmds_proto) / sizeof(irq_cmd_t);
|
---|
| 128 | con->irq_code.cmds = msim_cmds;
|
---|
| 129 |
|
---|
[eadaeae8] | 130 | rc = async_irq_subscribe(res->irq, msim_irq_handler, con,
|
---|
| 131 | &con->irq_code, &con->irq_handle);
|
---|
| 132 | if (rc != EOK) {
|
---|
| 133 | ddf_msg(LVL_ERROR, "Error registering IRQ code.");
|
---|
| 134 | goto error;
|
---|
| 135 | }
|
---|
[676e833] | 136 |
|
---|
[7a6065c] | 137 | chardev_srvs_init(&con->cds);
|
---|
| 138 | con->cds.ops = &msim_con_chardev_ops;
|
---|
| 139 | con->cds.sarg = con;
|
---|
| 140 |
|
---|
[676e833] | 141 | rc = ddf_fun_bind(fun);
|
---|
| 142 | if (rc != EOK) {
|
---|
| 143 | ddf_msg(LVL_ERROR, "Error binding function 'a'.");
|
---|
| 144 | goto error;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[4f87a85a] | 147 | bound = true;
|
---|
| 148 |
|
---|
| 149 | rc = ddf_fun_add_to_category(fun, "console");
|
---|
| 150 | if (rc != EOK) {
|
---|
| 151 | ddf_msg(LVL_ERROR, "Error adding function 'a' to category "
|
---|
| 152 | "'console'.");
|
---|
| 153 | goto error;
|
---|
| 154 | }
|
---|
[c309b18] | 155 |
|
---|
[676e833] | 156 | return EOK;
|
---|
| 157 | error:
|
---|
[bb97118] | 158 | if (cap_handle_valid(con->irq_handle))
|
---|
[eadaeae8] | 159 | async_irq_unsubscribe(con->irq_handle);
|
---|
[4f87a85a] | 160 | if (bound)
|
---|
| 161 | ddf_fun_unbind(fun);
|
---|
[676e833] | 162 | if (fun != NULL)
|
---|
| 163 | ddf_fun_destroy(fun);
|
---|
[92232331] | 164 | free(msim_cmds);
|
---|
[676e833] | 165 |
|
---|
| 166 | return rc;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | /** Remove msim console device */
|
---|
[b7fd2a0] | 170 | errno_t msim_con_remove(msim_con_t *con)
|
---|
[676e833] | 171 | {
|
---|
| 172 | return ENOTSUP;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | /** Msim console device gone */
|
---|
[b7fd2a0] | 176 | errno_t msim_con_gone(msim_con_t *con)
|
---|
[676e833] | 177 | {
|
---|
| 178 | return ENOTSUP;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | static void msim_con_putchar(msim_con_t *con, uint8_t ch)
|
---|
| 182 | {
|
---|
[c309b18] | 183 | pio_write_8(con->out_reg, ch);
|
---|
[676e833] | 184 | }
|
---|
| 185 |
|
---|
[7a6065c] | 186 | /** Read from msim console device */
|
---|
[b7fd2a0] | 187 | static errno_t msim_con_read(chardev_srv_t *srv, void *buf, size_t size,
|
---|
[f2d88f3] | 188 | size_t *nread, chardev_flags_t flags)
|
---|
[7a6065c] | 189 | {
|
---|
| 190 | msim_con_t *con = (msim_con_t *) srv->srvs->sarg;
|
---|
| 191 | size_t p;
|
---|
| 192 | uint8_t *bp = (uint8_t *) buf;
|
---|
[b7fd2a0] | 193 | errno_t rc;
|
---|
[7a6065c] | 194 |
|
---|
| 195 | fibril_mutex_lock(&con->buf_lock);
|
---|
| 196 |
|
---|
[f2d88f3] | 197 | while ((flags & chardev_f_nonblock) == 0 &&
|
---|
| 198 | circ_buf_nused(&con->cbuf) == 0)
|
---|
[7a6065c] | 199 | fibril_condvar_wait(&con->buf_cv, &con->buf_lock);
|
---|
| 200 |
|
---|
| 201 | p = 0;
|
---|
| 202 | while (p < size) {
|
---|
| 203 | rc = circ_buf_pop(&con->cbuf, &bp[p]);
|
---|
| 204 | if (rc != EOK)
|
---|
| 205 | break;
|
---|
| 206 | ++p;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | fibril_mutex_unlock(&con->buf_lock);
|
---|
| 210 |
|
---|
| 211 | *nread = p;
|
---|
| 212 | return EOK;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | /** Write to msim console device */
|
---|
[b7fd2a0] | 216 | static errno_t msim_con_write(chardev_srv_t *srv, const void *data, size_t size,
|
---|
[7a6065c] | 217 | size_t *nwr)
|
---|
| 218 | {
|
---|
| 219 | msim_con_t *con = (msim_con_t *) srv->srvs->sarg;
|
---|
| 220 | size_t i;
|
---|
| 221 | uint8_t *dp = (uint8_t *) data;
|
---|
| 222 |
|
---|
| 223 | for (i = 0; i < size; i++)
|
---|
[1b20da0] | 224 | msim_con_putchar(con, dp[i]);
|
---|
[7a6065c] | 225 |
|
---|
| 226 | *nwr = size;
|
---|
| 227 | return EOK;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[676e833] | 230 | /** Character device connection handler. */
|
---|
[984a9ba] | 231 | static void msim_con_connection(ipc_call_t *icall, void *arg)
|
---|
[676e833] | 232 | {
|
---|
[7a6065c] | 233 | msim_con_t *con = (msim_con_t *) ddf_dev_data_get(
|
---|
| 234 | ddf_fun_get_dev((ddf_fun_t *) arg));
|
---|
| 235 |
|
---|
[984a9ba] | 236 | chardev_conn(icall, &con->cds);
|
---|
[676e833] | 237 | }
|
---|
| 238 |
|
---|
| 239 | /** @}
|
---|
| 240 | */
|
---|