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

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

Use errno_t in all uspace and kernel code.

Change type of every variable, parameter and return value that holds an
<errno.h> constant to either errno_t (the usual case), or sys_errno_t
(some places in kernel). This is for the purpose of self-documentation,
as well as for type-checking with a bit of type definition hackery.

After this commit, HelenOS is free of code that mixes error codes with non-error
values on the assumption that error codes are negative.

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