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

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

Check return code from ddf_add_fun_to_category

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