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

Last change on this file was f2d88f3, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Add forgotten changes to enable non-blocking chardev read

  • Property mode set to 100644
File size: 5.5 KB
Line 
1/*
2 * Copyright (c) 2008 Pavel Rimsky
3 * Copyright (c) 2017 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 Sun4v console driver
31 */
32
33#include <as.h>
34#include <async.h>
35#include <ddf/log.h>
36#include <ddi.h>
37#include <errno.h>
38#include <str_error.h>
39#include <io/chardev_srv.h>
40#include <stdbool.h>
41
42#include "sun4v-con.h"
43
44static void sun4v_con_connection(ipc_call_t *, void *);
45
46#define POLL_INTERVAL 10000
47
48static errno_t sun4v_con_read(chardev_srv_t *, void *, size_t, size_t *,
49 chardev_flags_t);
50static errno_t sun4v_con_write(chardev_srv_t *, const void *, size_t, size_t *);
51
52static chardev_ops_t sun4v_con_chardev_ops = {
53 .read = sun4v_con_read,
54 .write = sun4v_con_write
55};
56
57static void sun4v_con_putchar(sun4v_con_t *con, uint8_t data)
58{
59 if (data == '\n')
60 sun4v_con_putchar(con, '\r');
61
62 while (con->output_buffer->write_ptr ==
63 (con->output_buffer->read_ptr + OUTPUT_BUFFER_SIZE - 1) %
64 OUTPUT_BUFFER_SIZE)
65 ;
66
67 con->output_buffer->data[con->output_buffer->write_ptr] = data;
68 con->output_buffer->write_ptr =
69 ((con->output_buffer->write_ptr) + 1) % OUTPUT_BUFFER_SIZE;
70}
71
72/** Add sun4v console device. */
73errno_t sun4v_con_add(sun4v_con_t *con, sun4v_con_res_t *res)
74{
75 ddf_fun_t *fun = NULL;
76 errno_t rc;
77 bool bound = false;
78
79 con->res = *res;
80 con->input_buffer = (niagara_input_buffer_t *) AS_AREA_ANY;
81
82 fun = ddf_fun_create(con->dev, fun_exposed, "a");
83 if (fun == NULL) {
84 ddf_msg(LVL_ERROR, "Error creating function 'a'.");
85 rc = ENOMEM;
86 goto error;
87 }
88
89 chardev_srvs_init(&con->cds);
90 con->cds.ops = &sun4v_con_chardev_ops;
91 con->cds.sarg = con;
92
93 ddf_fun_set_conn_handler(fun, sun4v_con_connection);
94
95 rc = physmem_map(res->in_base, 1, AS_AREA_READ | AS_AREA_WRITE,
96 (void *) &con->input_buffer);
97 if (rc != EOK) {
98 ddf_msg(LVL_ERROR, "Error mapping memory: %s", str_error_name(rc));
99 goto error;
100 }
101
102 con->output_buffer = (niagara_output_buffer_t *) AS_AREA_ANY;
103
104 rc = physmem_map(res->out_base, 1, AS_AREA_READ | AS_AREA_WRITE,
105 (void *) &con->output_buffer);
106 if (rc != EOK) {
107 ddf_msg(LVL_ERROR, "Error mapping memory: %s", str_error_name(rc));
108 return rc;
109 }
110
111 rc = ddf_fun_bind(fun);
112 if (rc != EOK) {
113 ddf_msg(LVL_ERROR, "Error binding function 'a'.");
114 goto error;
115 }
116
117 bound = true;
118
119 rc = ddf_fun_add_to_category(fun, "console");
120 if (rc != EOK) {
121 ddf_msg(LVL_ERROR, "Error adding function 'a' to category "
122 "'console'.");
123 goto error;
124 }
125
126 return EOK;
127error:
128 if (con->input_buffer != (niagara_input_buffer_t *) AS_AREA_ANY)
129 physmem_unmap((void *) con->input_buffer);
130
131 if (con->output_buffer != (niagara_output_buffer_t *) AS_AREA_ANY)
132 physmem_unmap((void *) con->output_buffer);
133
134 if (bound)
135 ddf_fun_unbind(fun);
136
137 if (fun != NULL)
138 ddf_fun_destroy(fun);
139
140 return rc;
141}
142
143/** Remove sun4v console device */
144errno_t sun4v_con_remove(sun4v_con_t *con)
145{
146 return ENOTSUP;
147}
148
149/** Msim console device gone */
150errno_t sun4v_con_gone(sun4v_con_t *con)
151{
152 return ENOTSUP;
153}
154
155/** Read from Sun4v console device */
156static errno_t sun4v_con_read(chardev_srv_t *srv, void *buf, size_t size,
157 size_t *nread, chardev_flags_t flags)
158{
159 sun4v_con_t *con = (sun4v_con_t *) srv->srvs->sarg;
160 size_t p;
161 uint8_t *bp = (uint8_t *) buf;
162 char c;
163
164 while ((flags & chardev_f_nonblock) == 0 &&
165 con->input_buffer->read_ptr == con->input_buffer->write_ptr)
166 fibril_usleep(POLL_INTERVAL);
167
168 p = 0;
169 while (p < size && con->input_buffer->read_ptr != con->input_buffer->write_ptr) {
170 c = con->input_buffer->data[con->input_buffer->read_ptr];
171 con->input_buffer->read_ptr =
172 ((con->input_buffer->read_ptr) + 1) % INPUT_BUFFER_SIZE;
173 bp[p++] = c;
174 }
175
176 *nread = p;
177 return EOK;
178}
179
180/** Write to Sun4v console device */
181static errno_t sun4v_con_write(chardev_srv_t *srv, const void *data, size_t size,
182 size_t *nwr)
183{
184 sun4v_con_t *con = (sun4v_con_t *) srv->srvs->sarg;
185 size_t i;
186 uint8_t *dp = (uint8_t *) data;
187
188 for (i = 0; i < size; i++)
189 sun4v_con_putchar(con, dp[i]);
190
191 *nwr = size;
192 return EOK;
193}
194
195/** Character device connection handler. */
196static void sun4v_con_connection(ipc_call_t *icall, void *arg)
197{
198 sun4v_con_t *con = (sun4v_con_t *) ddf_dev_data_get(
199 ddf_fun_get_dev((ddf_fun_t *) arg));
200
201 chardev_conn(icall, &con->cds);
202}
203
204/** @}
205 */
Note: See TracBrowser for help on using the repository browser.