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_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 | rc = pio_enable((void *)res->base, 1, (void **) &con->out_reg);
|
---|
109 | if (rc != EOK) {
|
---|
110 | ddf_msg(LVL_ERROR, "Error enabling I/O");
|
---|
111 | goto error;
|
---|
112 | }
|
---|
113 |
|
---|
114 | ddf_fun_set_conn_handler(fun, msim_con_connection);
|
---|
115 |
|
---|
116 | con->irq_range[0].base = res->base;
|
---|
117 | con->irq_range[0].size = 1;
|
---|
118 |
|
---|
119 | memcpy(msim_cmds, msim_cmds_proto, sizeof(msim_cmds_proto));
|
---|
120 | msim_cmds[0].addr = (void *) res->base;
|
---|
121 |
|
---|
122 | con->irq_code.rangecount = 1;
|
---|
123 | con->irq_code.ranges = con->irq_range;
|
---|
124 | con->irq_code.cmdcount = sizeof(msim_cmds_proto) / sizeof(irq_cmd_t);
|
---|
125 | con->irq_code.cmds = msim_cmds;
|
---|
126 |
|
---|
127 | async_irq_subscribe(res->irq, msim_irq_handler, con, &con->irq_code, NULL);
|
---|
128 | subscribed = true;
|
---|
129 |
|
---|
130 | chardev_srvs_init(&con->cds);
|
---|
131 | con->cds.ops = &msim_con_chardev_ops;
|
---|
132 | con->cds.sarg = con;
|
---|
133 |
|
---|
134 | rc = ddf_fun_bind(fun);
|
---|
135 | if (rc != EOK) {
|
---|
136 | ddf_msg(LVL_ERROR, "Error binding function 'a'.");
|
---|
137 | goto error;
|
---|
138 | }
|
---|
139 |
|
---|
140 | ddf_fun_add_to_category(fun, "console");
|
---|
141 |
|
---|
142 | return EOK;
|
---|
143 | error:
|
---|
144 | if (subscribed)
|
---|
145 | async_irq_unsubscribe(res->irq);
|
---|
146 | if (fun != NULL)
|
---|
147 | ddf_fun_destroy(fun);
|
---|
148 | free(msim_cmds);
|
---|
149 |
|
---|
150 | return rc;
|
---|
151 | }
|
---|
152 |
|
---|
153 | /** Remove msim console device */
|
---|
154 | int msim_con_remove(msim_con_t *con)
|
---|
155 | {
|
---|
156 | return ENOTSUP;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /** Msim console device gone */
|
---|
160 | int msim_con_gone(msim_con_t *con)
|
---|
161 | {
|
---|
162 | return ENOTSUP;
|
---|
163 | }
|
---|
164 |
|
---|
165 | static void msim_con_putchar(msim_con_t *con, uint8_t ch)
|
---|
166 | {
|
---|
167 | pio_write_8(con->out_reg, ch);
|
---|
168 | }
|
---|
169 |
|
---|
170 | /** Read from msim console device */
|
---|
171 | static int msim_con_read(chardev_srv_t *srv, void *buf, size_t size,
|
---|
172 | size_t *nread)
|
---|
173 | {
|
---|
174 | msim_con_t *con = (msim_con_t *) srv->srvs->sarg;
|
---|
175 | size_t p;
|
---|
176 | uint8_t *bp = (uint8_t *) buf;
|
---|
177 | int rc;
|
---|
178 |
|
---|
179 | fibril_mutex_lock(&con->buf_lock);
|
---|
180 |
|
---|
181 | while (circ_buf_nused(&con->cbuf) == 0)
|
---|
182 | fibril_condvar_wait(&con->buf_cv, &con->buf_lock);
|
---|
183 |
|
---|
184 | p = 0;
|
---|
185 | while (p < size) {
|
---|
186 | rc = circ_buf_pop(&con->cbuf, &bp[p]);
|
---|
187 | if (rc != EOK)
|
---|
188 | break;
|
---|
189 | ++p;
|
---|
190 | }
|
---|
191 |
|
---|
192 | fibril_mutex_unlock(&con->buf_lock);
|
---|
193 |
|
---|
194 | *nread = p;
|
---|
195 | return EOK;
|
---|
196 | }
|
---|
197 |
|
---|
198 | /** Write to msim console device */
|
---|
199 | static int msim_con_write(chardev_srv_t *srv, const void *data, size_t size,
|
---|
200 | size_t *nwr)
|
---|
201 | {
|
---|
202 | msim_con_t *con = (msim_con_t *) srv->srvs->sarg;
|
---|
203 | size_t i;
|
---|
204 | uint8_t *dp = (uint8_t *) data;
|
---|
205 |
|
---|
206 | for (i = 0; i < size; i++)
|
---|
207 | msim_con_putchar(con, dp[i]);
|
---|
208 |
|
---|
209 | *nwr = size;
|
---|
210 | return EOK;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /** Character device connection handler. */
|
---|
214 | static void msim_con_connection(ipc_callid_t iid, ipc_call_t *icall,
|
---|
215 | void *arg)
|
---|
216 | {
|
---|
217 | msim_con_t *con = (msim_con_t *) ddf_dev_data_get(
|
---|
218 | ddf_fun_get_dev((ddf_fun_t *) arg));
|
---|
219 |
|
---|
220 | chardev_conn(iid, icall, &con->cds);
|
---|
221 | }
|
---|
222 |
|
---|
223 | /** @}
|
---|
224 | */
|
---|