source: mainline/uspace/drv/char/msim-con/msim-con.c@ 676e833

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 676e833 was 676e833, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Move receiving side of Msim console to a separate driver. Work around possibility that console character device is not available at the moment input server starts.

  • Property mode set to 100644
File size: 4.9 KB
Line 
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#include <sysinfo.h>
41
42#include "msim-con.h"
43
44static void msim_con_connection(ipc_callid_t, ipc_call_t *, void *);
45
46static irq_pio_range_t msim_ranges[] = {
47 {
48 .base = 0,
49 .size = 1
50 }
51};
52
53static irq_cmd_t msim_cmds[] = {
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
64static irq_code_t msim_kbd = {
65 sizeof(msim_ranges) / sizeof(irq_pio_range_t),
66 msim_ranges,
67 sizeof(msim_cmds) / sizeof(irq_cmd_t),
68 msim_cmds
69};
70#include <stdio.h>
71static void msim_irq_handler(ipc_callid_t iid, ipc_call_t *call, void *arg)
72{
73 msim_con_t *con = (msim_con_t *) arg;
74 uint8_t c;
75
76 c = IPC_GET_ARG2(*call);
77 printf("key=%d\n", c);
78 if (con->client_sess != NULL) {
79 async_exch_t *exch = async_exchange_begin(con->client_sess);
80 async_msg_1(exch, CHAR_NOTIF_BYTE, c);
81 async_exchange_end(exch);
82 }
83}
84
85/** Add msim console device. */
86int msim_con_add(msim_con_t *con)
87{
88 ddf_fun_t *fun = NULL;
89 bool subscribed = false;
90 int rc;
91
92 printf("msim_con_add\n");
93 fun = ddf_fun_create(con->dev, fun_exposed, "a");
94 if (fun == NULL) {
95 ddf_msg(LVL_ERROR, "Error creating function 'a'.");
96 rc = ENOMEM;
97 goto error;
98 }
99
100 ddf_fun_set_conn_handler(fun, msim_con_connection);
101
102 sysarg_t paddr;
103 if (sysinfo_get_value("kbd.address.physical", &paddr) != EOK) {
104 rc = ENOENT;
105 goto error;
106 }
107
108 sysarg_t inr;
109 if (sysinfo_get_value("kbd.inr", &inr) != EOK) {
110 rc = ENOENT;
111 goto error;
112 }
113
114 printf("msim_con_add: irq subscribe\n");
115
116 msim_ranges[0].base = paddr;
117 msim_cmds[0].addr = (void *) paddr;
118 async_irq_subscribe(inr, msim_irq_handler, con, &msim_kbd);
119 subscribed = true;
120
121 printf("msim_con_add: bind\n");
122 rc = ddf_fun_bind(fun);
123 if (rc != EOK) {
124 ddf_msg(LVL_ERROR, "Error binding function 'a'.");
125 goto error;
126 }
127
128 printf("msim_con_add: DONE\n");
129 return EOK;
130error:
131 if (subscribed)
132 async_irq_unsubscribe(inr);
133 if (fun != NULL)
134 ddf_fun_destroy(fun);
135
136 return rc;
137}
138
139/** Remove msim console device */
140int msim_con_remove(msim_con_t *con)
141{
142 return ENOTSUP;
143}
144
145/** Msim console device gone */
146int msim_con_gone(msim_con_t *con)
147{
148 return ENOTSUP;
149}
150
151static void msim_con_putchar(msim_con_t *con, uint8_t ch)
152{
153}
154
155/** Character device connection handler. */
156static void msim_con_connection(ipc_callid_t iid, ipc_call_t *icall,
157 void *arg)
158{
159 msim_con_t *con;
160
161 /* Answer the IPC_M_CONNECT_ME_TO call. */
162 async_answer_0(iid, EOK);
163
164 printf("msim_con_connection\n");
165
166 con = (msim_con_t *)ddf_dev_data_get(ddf_fun_get_dev((ddf_fun_t *)arg));
167
168 while (true) {
169 ipc_call_t call;
170 ipc_callid_t callid = async_get_call(&call);
171 sysarg_t method = IPC_GET_IMETHOD(call);
172
173 if (!method) {
174 /* The other side has hung up. */
175 async_answer_0(callid, EOK);
176 return;
177 }
178
179 async_sess_t *sess =
180 async_callback_receive_start(EXCHANGE_SERIALIZE, &call);
181 if (sess != NULL) {
182 if (con->client_sess == NULL) {
183 con->client_sess = sess;
184 async_answer_0(callid, EOK);
185 } else
186 async_answer_0(callid, ELIMIT);
187 printf("msim_con_connection: set client_sess\n");
188 } else {
189 switch (method) {
190 case CHAR_WRITE_BYTE:
191 ddf_msg(LVL_DEBUG, "Write %" PRIun " to device\n",
192 IPC_GET_ARG1(call));
193 msim_con_putchar(con, (uint8_t) IPC_GET_ARG1(call));
194 async_answer_0(callid, EOK);
195 break;
196 default:
197 async_answer_0(callid, EINVAL);
198 }
199 }
200 }
201}
202
203/** @}
204 */
Note: See TracBrowser for help on using the repository browser.