source: mainline/uspace/srv/hid/output/port/chardev.c@ bdf06ad

Last change on this file since bdf06ad was b79c91cc, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Enable UTF-8 on user-space serial console output

  • Property mode set to 100644
File size: 6.3 KB
Line 
1/*
2 * Copyright (c) 2016 Jakub Jermar
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/** @addtogropu output
31 * @{
32 */
33
34#include <async.h>
35#include <config.h>
36#include <errno.h>
37#include <fibril_synch.h>
38#include <io/chardev.h>
39#include <loc.h>
40#include <stddef.h>
41#include <stdint.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <str.h>
45#include "../ctl/serial.h"
46#include "../output.h"
47#include "chardev.h"
48
49enum {
50 chardev_buf_size = 4096
51};
52
53static char *console;
54
55static async_sess_t *sess;
56static chardev_t *chardev;
57static service_id_t serial_cat_id;
58static service_id_t console_cat_id;
59
60static uint8_t chardev_buf[chardev_buf_size];
61static size_t chardev_bused;
62
63static FIBRIL_MUTEX_INITIALIZE(discovery_lock);
64static bool discovery_finished;
65static FIBRIL_CONDVAR_INITIALIZE(discovery_cv);
66
67static void chardev_flush(void)
68{
69 size_t nwr;
70
71 if (chardev_bused == 0)
72 return;
73
74 chardev_write(chardev, chardev_buf, chardev_bused, &nwr);
75 /* XXX Handle error */
76
77 chardev_bused = 0;
78}
79
80static void chardev_putchar(char ch)
81{
82 if (chardev_bused == chardev_buf_size)
83 chardev_flush();
84 chardev_buf[chardev_bused++] = (uint8_t) ch;
85}
86
87static void chardev_putuchar(char32_t ch)
88{
89 char buf[STR_BOUNDS(1)];
90 size_t off;
91 size_t i;
92 errno_t rc;
93
94 off = 0;
95 rc = chr_encode(ch, buf, &off, sizeof(buf));
96 if (rc != EOK)
97 return;
98
99 for (i = 0; i < off; i++)
100 chardev_putchar(buf[i]);
101}
102
103static void chardev_control_puts(const char *str)
104{
105 const char *p;
106
107 p = str;
108 while (*p != '\0')
109 chardev_putuchar(*p++);
110}
111
112static bool find_output_dev(service_id_t *svcid)
113{
114 service_id_t *svc;
115 size_t svcs;
116 errno_t rc;
117
118 rc = loc_category_get_svcs(serial_cat_id, &svc, &svcs);
119 if (rc != EOK) {
120 fibril_mutex_unlock(&discovery_lock);
121 printf("%s: Failed to get services\n", NAME);
122 return false;
123 }
124
125 for (size_t i = 0; i < svcs; i++) {
126 char *name;
127
128 rc = loc_service_get_name(svc[i], &name);
129 if (rc != EOK)
130 continue;
131
132 if (!str_cmp(console, name)) {
133 /*
134 * This is the serial console service that the user
135 * wanted to use.
136 */
137 *svcid = svc[i];
138 free(svc);
139 return true;
140 }
141
142 free(name);
143 }
144
145 free(svc);
146
147 /* Look for any service in the 'console' category */
148
149 rc = loc_category_get_svcs(console_cat_id, &svc, &svcs);
150 if (rc != EOK) {
151 fibril_mutex_unlock(&discovery_lock);
152 printf("%s: Failed to get services\n", NAME);
153 return false;
154 }
155
156 if (svcs > 0) {
157 *svcid = svc[0];
158 free(svc);
159 return true;
160 }
161
162 free(svc);
163 return false;
164}
165
166/*
167 * This callback scans all the services in the 'serial' category, hoping to see
168 * the single one the user wishes to use as a serial console. If it spots it, it
169 * connects to it and registers it as an output device. Then it unblocks the
170 * fibril blocked in chardev_init().
171 */
172static void check_for_dev(void *arg)
173{
174 errno_t rc;
175 bool found;
176 service_id_t sid;
177
178 fibril_mutex_lock(&discovery_lock);
179 if (discovery_finished) {
180 // TODO: no need to receive these callbacks anymore
181 fibril_mutex_unlock(&discovery_lock);
182 return;
183 }
184
185 found = find_output_dev(&sid);
186 if (!found) {
187 fibril_mutex_unlock(&discovery_lock);
188 return;
189 }
190
191 printf("%s: Connecting service %zu\n", NAME, sid);
192 char *name;
193 rc = loc_service_get_name(sid, &name);
194 if (rc != EOK) {
195 fibril_mutex_unlock(&discovery_lock);
196 return;
197 }
198 printf("%s: Service name is %s\n", NAME, name);
199 free(name);
200
201 sess = loc_service_connect(sid, INTERFACE_DDF, IPC_FLAG_BLOCKING);
202 if (!sess) {
203 fibril_mutex_unlock(&discovery_lock);
204 printf("%s: Failed connecting to device\n", NAME);
205 return;
206 }
207
208 rc = chardev_open(sess, &chardev);
209 if (rc != EOK) {
210 fibril_mutex_unlock(&discovery_lock);
211 printf("%s: Failed opening character device\n", NAME);
212 return;
213 }
214
215 serial_init(chardev_putuchar, chardev_control_puts, chardev_flush);
216
217 discovery_finished = true;
218 fibril_condvar_signal(&discovery_cv);
219 fibril_mutex_unlock(&discovery_lock);
220}
221
222errno_t chardev_init(void)
223{
224 if (!config_key_exists("console")) {
225 console = NULL;
226#ifdef MACHINE_ski
227 /* OK */
228#elif defined(UARCH_sparc64) && defined(PROCESSOR_sun4v)
229 /* OK */
230#elif defined(MACHINE_msim)
231 /* OK */
232#elif defined(UARCH_arm64) && defined(MACHINE_virt)
233 /* OK */
234#elif defined(UARCH_arm64) && defined(MACHINE_hikey960)
235 /* OK */
236#else
237 return EOK;
238#endif
239 } else {
240 console = config_get_value("console");
241 if (!console)
242 return EOK;
243 }
244
245 errno_t rc = loc_category_get_id("serial", &serial_cat_id, IPC_FLAG_BLOCKING);
246 if (rc != EOK) {
247 printf("%s: Failed to get \"serial\" category ID.\n", NAME);
248 return rc;
249 }
250
251 rc = loc_category_get_id("console", &console_cat_id, IPC_FLAG_BLOCKING);
252 if (rc != EOK) {
253 printf("%s: Failed to get \"console\" category ID.\n", NAME);
254 return rc;
255 }
256
257 rc = loc_register_cat_change_cb(check_for_dev, NULL);
258 if (rc != EOK) {
259 printf("%s: Failed to register callback for device discovery.\n",
260 NAME);
261 return rc;
262 }
263
264 check_for_dev(NULL);
265
266 fibril_mutex_lock(&discovery_lock);
267 while (!discovery_finished)
268 fibril_condvar_wait(&discovery_cv, &discovery_lock);
269 fibril_mutex_unlock(&discovery_lock);
270
271 return EOK;
272}
273
274/** @}
275 */
Note: See TracBrowser for help on using the repository browser.