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

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

Replace fibril_usleep() with async_usleep().

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