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 <ipc/char.h>
|
---|
40 |
|
---|
41 | #include "msim-con.h"
|
---|
42 |
|
---|
43 | static void msim_con_connection(ipc_callid_t, ipc_call_t *, void *);
|
---|
44 |
|
---|
45 | static irq_cmd_t msim_cmds_proto[] = {
|
---|
46 | {
|
---|
47 | .cmd = CMD_PIO_READ_8,
|
---|
48 | .addr = (void *) 0, /* will be patched in run-time */
|
---|
49 | .dstarg = 2
|
---|
50 | },
|
---|
51 | {
|
---|
52 | .cmd = CMD_ACCEPT
|
---|
53 | }
|
---|
54 | };
|
---|
55 |
|
---|
56 | static void msim_irq_handler(ipc_callid_t iid, ipc_call_t *call, void *arg)
|
---|
57 | {
|
---|
58 | msim_con_t *con = (msim_con_t *) arg;
|
---|
59 | uint8_t c;
|
---|
60 |
|
---|
61 | c = IPC_GET_ARG2(*call);
|
---|
62 |
|
---|
63 | if (con->client_sess != NULL) {
|
---|
64 | async_exch_t *exch = async_exchange_begin(con->client_sess);
|
---|
65 | async_msg_1(exch, CHAR_NOTIF_BYTE, c);
|
---|
66 | async_exchange_end(exch);
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | /** Add msim console device. */
|
---|
71 | int msim_con_add(msim_con_t *con, msim_con_res_t *res)
|
---|
72 | {
|
---|
73 | ddf_fun_t *fun = NULL;
|
---|
74 | bool subscribed = false;
|
---|
75 | irq_cmd_t *msim_cmds = NULL;
|
---|
76 | int rc;
|
---|
77 |
|
---|
78 | msim_cmds = malloc(sizeof(msim_cmds_proto));
|
---|
79 | if (msim_cmds == NULL) {
|
---|
80 | rc = ENOMEM;
|
---|
81 | goto error;
|
---|
82 | }
|
---|
83 |
|
---|
84 | con->res = *res;
|
---|
85 |
|
---|
86 | fun = ddf_fun_create(con->dev, fun_exposed, "a");
|
---|
87 | if (fun == NULL) {
|
---|
88 | ddf_msg(LVL_ERROR, "Error creating function 'a'.");
|
---|
89 | rc = ENOMEM;
|
---|
90 | goto error;
|
---|
91 | }
|
---|
92 |
|
---|
93 | ddf_fun_set_conn_handler(fun, msim_con_connection);
|
---|
94 |
|
---|
95 | con->irq_range[0].base = res->base;
|
---|
96 | con->irq_range[0].size = 1;
|
---|
97 |
|
---|
98 | memcpy(msim_cmds, msim_cmds_proto, sizeof(msim_cmds_proto));
|
---|
99 | msim_cmds[0].addr = (void *) res->base;
|
---|
100 |
|
---|
101 | con->irq_code.rangecount = 1;
|
---|
102 | con->irq_code.ranges = con->irq_range;
|
---|
103 | con->irq_code.cmdcount = sizeof(msim_cmds_proto) / sizeof(irq_cmd_t);
|
---|
104 | con->irq_code.cmds = msim_cmds;
|
---|
105 |
|
---|
106 | async_irq_subscribe(res->irq, msim_irq_handler, con, &con->irq_code);
|
---|
107 | subscribed = true;
|
---|
108 |
|
---|
109 | rc = ddf_fun_bind(fun);
|
---|
110 | if (rc != EOK) {
|
---|
111 | ddf_msg(LVL_ERROR, "Error binding function 'a'.");
|
---|
112 | goto error;
|
---|
113 | }
|
---|
114 |
|
---|
115 | return EOK;
|
---|
116 | error:
|
---|
117 | if (subscribed)
|
---|
118 | async_irq_unsubscribe(res->irq);
|
---|
119 | if (fun != NULL)
|
---|
120 | ddf_fun_destroy(fun);
|
---|
121 | free(msim_cmds);
|
---|
122 |
|
---|
123 | return rc;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /** Remove msim console device */
|
---|
127 | int msim_con_remove(msim_con_t *con)
|
---|
128 | {
|
---|
129 | return ENOTSUP;
|
---|
130 | }
|
---|
131 |
|
---|
132 | /** Msim console device gone */
|
---|
133 | int msim_con_gone(msim_con_t *con)
|
---|
134 | {
|
---|
135 | return ENOTSUP;
|
---|
136 | }
|
---|
137 |
|
---|
138 | static void msim_con_putchar(msim_con_t *con, uint8_t ch)
|
---|
139 | {
|
---|
140 | }
|
---|
141 |
|
---|
142 | /** Character device connection handler. */
|
---|
143 | static void msim_con_connection(ipc_callid_t iid, ipc_call_t *icall,
|
---|
144 | void *arg)
|
---|
145 | {
|
---|
146 | msim_con_t *con;
|
---|
147 |
|
---|
148 | /* Answer the IPC_M_CONNECT_ME_TO call. */
|
---|
149 | async_answer_0(iid, EOK);
|
---|
150 |
|
---|
151 | con = (msim_con_t *)ddf_dev_data_get(ddf_fun_get_dev((ddf_fun_t *)arg));
|
---|
152 |
|
---|
153 | while (true) {
|
---|
154 | ipc_call_t call;
|
---|
155 | ipc_callid_t callid = async_get_call(&call);
|
---|
156 | sysarg_t method = IPC_GET_IMETHOD(call);
|
---|
157 |
|
---|
158 | if (!method) {
|
---|
159 | /* The other side has hung up. */
|
---|
160 | async_answer_0(callid, EOK);
|
---|
161 | return;
|
---|
162 | }
|
---|
163 |
|
---|
164 | async_sess_t *sess =
|
---|
165 | async_callback_receive_start(EXCHANGE_SERIALIZE, &call);
|
---|
166 | if (sess != NULL) {
|
---|
167 | if (con->client_sess == NULL) {
|
---|
168 | con->client_sess = sess;
|
---|
169 | async_answer_0(callid, EOK);
|
---|
170 | } else
|
---|
171 | async_answer_0(callid, ELIMIT);
|
---|
172 | } else {
|
---|
173 | switch (method) {
|
---|
174 | case CHAR_WRITE_BYTE:
|
---|
175 | ddf_msg(LVL_DEBUG, "Write %" PRIun " to device\n",
|
---|
176 | IPC_GET_ARG1(call));
|
---|
177 | msim_con_putchar(con, (uint8_t) IPC_GET_ARG1(call));
|
---|
178 | async_answer_0(callid, EOK);
|
---|
179 | break;
|
---|
180 | default:
|
---|
181 | async_answer_0(callid, EINVAL);
|
---|
182 | }
|
---|
183 | }
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | /** @}
|
---|
188 | */
|
---|