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

Last change on this file since c23a1fe was d3109ff, checked in by Jiri Svoboda <jiri@…>, 12 months ago

Cursor and color control in remote console + RGB

Move vt100 module from output server into separate vt library
Add cursor and color control to remcons using libvt
Add RGB color control to serial console and remote console

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