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

Last change on this file was fafb8e5, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 6 years ago

Mechanically lowercase IPC_SET_*/IPC_GET_*

  • Property mode set to 100644
File size: 5.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 <io/chardev_srv.h>
40
41#include "msim-con.h"
42
43static void msim_con_connection(ipc_call_t *, void *);
44
45static errno_t msim_con_read(chardev_srv_t *, void *, size_t, size_t *,
46 chardev_flags_t);
47static errno_t msim_con_write(chardev_srv_t *, const void *, size_t, size_t *);
48
49static chardev_ops_t msim_con_chardev_ops = {
50 .read = msim_con_read,
51 .write = msim_con_write
52};
53
54static irq_cmd_t msim_cmds_proto[] = {
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
65static void msim_irq_handler(ipc_call_t *call, void *arg)
66{
67 msim_con_t *con = (msim_con_t *) arg;
68 uint8_t c;
69 errno_t rc;
70
71 fibril_mutex_lock(&con->buf_lock);
72
73 c = ipc_get_arg2(call);
74 rc = circ_buf_push(&con->cbuf, &c);
75 if (rc != EOK)
76 ddf_msg(LVL_ERROR, "Buffer overrun");
77
78 fibril_mutex_unlock(&con->buf_lock);
79 fibril_condvar_broadcast(&con->buf_cv);
80}
81
82/** Add msim console device. */
83errno_t msim_con_add(msim_con_t *con, msim_con_res_t *res)
84{
85 ddf_fun_t *fun = NULL;
86 irq_cmd_t *msim_cmds = NULL;
87 errno_t rc;
88 bool bound = false;
89
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
94 con->irq_handle = CAP_NIL;
95
96 msim_cmds = malloc(sizeof(msim_cmds_proto));
97 if (msim_cmds == NULL) {
98 rc = ENOMEM;
99 goto error;
100 }
101
102 con->res = *res;
103
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
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
117 ddf_fun_set_conn_handler(fun, msim_con_connection);
118
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));
123 msim_cmds[0].addr = (void *) res->base;
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
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 }
136
137 chardev_srvs_init(&con->cds);
138 con->cds.ops = &msim_con_chardev_ops;
139 con->cds.sarg = con;
140
141 rc = ddf_fun_bind(fun);
142 if (rc != EOK) {
143 ddf_msg(LVL_ERROR, "Error binding function 'a'.");
144 goto error;
145 }
146
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 }
155
156 return EOK;
157error:
158 if (cap_handle_valid(con->irq_handle))
159 async_irq_unsubscribe(con->irq_handle);
160 if (bound)
161 ddf_fun_unbind(fun);
162 if (fun != NULL)
163 ddf_fun_destroy(fun);
164 free(msim_cmds);
165
166 return rc;
167}
168
169/** Remove msim console device */
170errno_t msim_con_remove(msim_con_t *con)
171{
172 return ENOTSUP;
173}
174
175/** Msim console device gone */
176errno_t msim_con_gone(msim_con_t *con)
177{
178 return ENOTSUP;
179}
180
181static void msim_con_putchar(msim_con_t *con, uint8_t ch)
182{
183 pio_write_8(con->out_reg, ch);
184}
185
186/** Read from msim console device */
187static errno_t msim_con_read(chardev_srv_t *srv, void *buf, size_t size,
188 size_t *nread, chardev_flags_t flags)
189{
190 msim_con_t *con = (msim_con_t *) srv->srvs->sarg;
191 size_t p;
192 uint8_t *bp = (uint8_t *) buf;
193 errno_t rc;
194
195 fibril_mutex_lock(&con->buf_lock);
196
197 while ((flags & chardev_f_nonblock) == 0 &&
198 circ_buf_nused(&con->cbuf) == 0)
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 */
216static errno_t msim_con_write(chardev_srv_t *srv, const void *data, size_t size,
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++)
224 msim_con_putchar(con, dp[i]);
225
226 *nwr = size;
227 return EOK;
228}
229
230/** Character device connection handler. */
231static void msim_con_connection(ipc_call_t *icall, void *arg)
232{
233 msim_con_t *con = (msim_con_t *) ddf_dev_data_get(
234 ddf_fun_get_dev((ddf_fun_t *) arg));
235
236 chardev_conn(icall, &con->cds);
237}
238
239/** @}
240 */
Note: See TracBrowser for help on using the repository browser.