| 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>
|
|---|
| 39 | #include <io/chardev_srv.h>
|
|---|
| 40 |
|
|---|
| 41 | #include "msim-con.h"
|
|---|
| 42 |
|
|---|
| 43 | static void msim_con_connection(ipc_callid_t, ipc_call_t *, void *);
|
|---|
| 44 |
|
|---|
| 45 | static int msim_con_read(chardev_srv_t *, void *, size_t, size_t *);
|
|---|
| 46 | static int msim_con_write(chardev_srv_t *, const void *, size_t, size_t *);
|
|---|
| 47 |
|
|---|
| 48 | static chardev_ops_t msim_con_chardev_ops = {
|
|---|
| 49 | .read = msim_con_read,
|
|---|
| 50 | .write = msim_con_write
|
|---|
| 51 | };
|
|---|
| 52 |
|
|---|
| 53 | static irq_cmd_t msim_cmds_proto[] = {
|
|---|
| 54 | {
|
|---|
| 55 | .cmd = CMD_PIO_READ_8,
|
|---|
| 56 | .addr = (void *) 0, /* will be patched in run-time */
|
|---|
| 57 | .dstarg = 2
|
|---|
| 58 | },
|
|---|
| 59 | {
|
|---|
| 60 | .cmd = CMD_ACCEPT
|
|---|
| 61 | }
|
|---|
| 62 | };
|
|---|
| 63 |
|
|---|
| 64 | static void msim_irq_handler(ipc_callid_t iid, ipc_call_t *call, void *arg)
|
|---|
| 65 | {
|
|---|
| 66 | msim_con_t *con = (msim_con_t *) arg;
|
|---|
| 67 | uint8_t c;
|
|---|
| 68 | int rc;
|
|---|
| 69 |
|
|---|
| 70 | fibril_mutex_lock(&con->buf_lock);
|
|---|
| 71 |
|
|---|
| 72 | c = IPC_GET_ARG2(*call);
|
|---|
| 73 | rc = circ_buf_push(&con->cbuf, &c);
|
|---|
| 74 | if (rc != EOK)
|
|---|
| 75 | ddf_msg(LVL_ERROR, "Buffer overrun");
|
|---|
| 76 |
|
|---|
| 77 | fibril_mutex_unlock(&con->buf_lock);
|
|---|
| 78 | fibril_condvar_broadcast(&con->buf_cv);
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | /** Add msim console device. */
|
|---|
| 82 | int msim_con_add(msim_con_t *con, msim_con_res_t *res)
|
|---|
| 83 | {
|
|---|
| 84 | ddf_fun_t *fun = NULL;
|
|---|
| 85 | bool subscribed = false;
|
|---|
| 86 | irq_cmd_t *msim_cmds = NULL;
|
|---|
| 87 | int rc;
|
|---|
| 88 |
|
|---|
| 89 | circ_buf_init(&con->cbuf, con->buf, msim_con_buf_size, 1);
|
|---|
| 90 | fibril_mutex_initialize(&con->buf_lock);
|
|---|
| 91 | fibril_condvar_initialize(&con->buf_cv);
|
|---|
| 92 |
|
|---|
| 93 | msim_cmds = malloc(sizeof(msim_cmds_proto));
|
|---|
| 94 | if (msim_cmds == NULL) {
|
|---|
| 95 | rc = ENOMEM;
|
|---|
| 96 | goto error;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | con->res = *res;
|
|---|
| 100 |
|
|---|
| 101 | fun = ddf_fun_create(con->dev, fun_exposed, "a");
|
|---|
| 102 | if (fun == NULL) {
|
|---|
| 103 | ddf_msg(LVL_ERROR, "Error creating function 'a'.");
|
|---|
| 104 | rc = ENOMEM;
|
|---|
| 105 | goto error;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | ddf_fun_set_conn_handler(fun, msim_con_connection);
|
|---|
| 109 |
|
|---|
| 110 | con->irq_range[0].base = res->base;
|
|---|
| 111 | con->irq_range[0].size = 1;
|
|---|
| 112 |
|
|---|
| 113 | memcpy(msim_cmds, msim_cmds_proto, sizeof(msim_cmds_proto));
|
|---|
| 114 | msim_cmds[0].addr = (void *) res->base;
|
|---|
| 115 |
|
|---|
| 116 | con->irq_code.rangecount = 1;
|
|---|
| 117 | con->irq_code.ranges = con->irq_range;
|
|---|
| 118 | con->irq_code.cmdcount = sizeof(msim_cmds_proto) / sizeof(irq_cmd_t);
|
|---|
| 119 | con->irq_code.cmds = msim_cmds;
|
|---|
| 120 |
|
|---|
| 121 | async_irq_subscribe(res->irq, msim_irq_handler, con, &con->irq_code);
|
|---|
| 122 | subscribed = true;
|
|---|
| 123 |
|
|---|
| 124 | chardev_srvs_init(&con->cds);
|
|---|
| 125 | con->cds.ops = &msim_con_chardev_ops;
|
|---|
| 126 | con->cds.sarg = con;
|
|---|
| 127 |
|
|---|
| 128 | rc = ddf_fun_bind(fun);
|
|---|
| 129 | if (rc != EOK) {
|
|---|
| 130 | ddf_msg(LVL_ERROR, "Error binding function 'a'.");
|
|---|
| 131 | goto error;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | return EOK;
|
|---|
| 135 | error:
|
|---|
| 136 | if (subscribed)
|
|---|
| 137 | async_irq_unsubscribe(res->irq);
|
|---|
| 138 | if (fun != NULL)
|
|---|
| 139 | ddf_fun_destroy(fun);
|
|---|
| 140 | free(msim_cmds);
|
|---|
| 141 |
|
|---|
| 142 | return rc;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | /** Remove msim console device */
|
|---|
| 146 | int msim_con_remove(msim_con_t *con)
|
|---|
| 147 | {
|
|---|
| 148 | return ENOTSUP;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | /** Msim console device gone */
|
|---|
| 152 | int msim_con_gone(msim_con_t *con)
|
|---|
| 153 | {
|
|---|
| 154 | return ENOTSUP;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | static void msim_con_putchar(msim_con_t *con, uint8_t ch)
|
|---|
| 158 | {
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /** Read from msim console device */
|
|---|
| 162 | static int msim_con_read(chardev_srv_t *srv, void *buf, size_t size,
|
|---|
| 163 | size_t *nread)
|
|---|
| 164 | {
|
|---|
| 165 | msim_con_t *con = (msim_con_t *) srv->srvs->sarg;
|
|---|
| 166 | size_t p;
|
|---|
| 167 | uint8_t *bp = (uint8_t *) buf;
|
|---|
| 168 | int rc;
|
|---|
| 169 |
|
|---|
| 170 | fibril_mutex_lock(&con->buf_lock);
|
|---|
| 171 |
|
|---|
| 172 | while (circ_buf_nused(&con->cbuf) == 0)
|
|---|
| 173 | fibril_condvar_wait(&con->buf_cv, &con->buf_lock);
|
|---|
| 174 |
|
|---|
| 175 | p = 0;
|
|---|
| 176 | while (p < size) {
|
|---|
| 177 | rc = circ_buf_pop(&con->cbuf, &bp[p]);
|
|---|
| 178 | if (rc != EOK)
|
|---|
| 179 | break;
|
|---|
| 180 | ++p;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | fibril_mutex_unlock(&con->buf_lock);
|
|---|
| 184 |
|
|---|
| 185 | *nread = p;
|
|---|
| 186 | return EOK;
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | /** Write to msim console device */
|
|---|
| 190 | static int msim_con_write(chardev_srv_t *srv, const void *data, size_t size,
|
|---|
| 191 | size_t *nwr)
|
|---|
| 192 | {
|
|---|
| 193 | msim_con_t *con = (msim_con_t *) srv->srvs->sarg;
|
|---|
| 194 | size_t i;
|
|---|
| 195 | uint8_t *dp = (uint8_t *) data;
|
|---|
| 196 |
|
|---|
| 197 | for (i = 0; i < size; i++)
|
|---|
| 198 | msim_con_putchar(con, dp[i]);
|
|---|
| 199 |
|
|---|
| 200 | *nwr = size;
|
|---|
| 201 | return EOK;
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | /** Character device connection handler. */
|
|---|
| 205 | static void msim_con_connection(ipc_callid_t iid, ipc_call_t *icall,
|
|---|
| 206 | void *arg)
|
|---|
| 207 | {
|
|---|
| 208 | msim_con_t *con = (msim_con_t *) ddf_dev_data_get(
|
|---|
| 209 | ddf_fun_get_dev((ddf_fun_t *) arg));
|
|---|
| 210 |
|
|---|
| 211 | chardev_conn(iid, icall, &con->cds);
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | /** @}
|
|---|
| 215 | */
|
|---|