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 |
|
---|
49 | enum {
|
---|
50 | chardev_buf_size = 4096
|
---|
51 | };
|
---|
52 |
|
---|
53 | static char *console;
|
---|
54 |
|
---|
55 | static async_sess_t *sess;
|
---|
56 | static chardev_t *chardev;
|
---|
57 | static service_id_t serial_cat_id;
|
---|
58 | static service_id_t console_cat_id;
|
---|
59 |
|
---|
60 | static uint8_t chardev_buf[chardev_buf_size];
|
---|
61 | static size_t chardev_bused;
|
---|
62 |
|
---|
63 | static FIBRIL_MUTEX_INITIALIZE(discovery_lock);
|
---|
64 | static bool discovery_finished;
|
---|
65 | static FIBRIL_CONDVAR_INITIALIZE(discovery_cv);
|
---|
66 |
|
---|
67 | static 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 |
|
---|
80 | static void chardev_putwchar(wchar_t ch)
|
---|
81 | {
|
---|
82 | if (chardev_bused == chardev_buf_size)
|
---|
83 | chardev_flush();
|
---|
84 | if (!ascii_check(ch))
|
---|
85 | ch = '?';
|
---|
86 | chardev_buf[chardev_bused++] = (uint8_t) ch;
|
---|
87 | }
|
---|
88 |
|
---|
89 | static void chardev_control_puts(const char *str)
|
---|
90 | {
|
---|
91 | const char *p;
|
---|
92 |
|
---|
93 | p = str;
|
---|
94 | while (*p != '\0')
|
---|
95 | chardev_putwchar(*p++);
|
---|
96 | }
|
---|
97 |
|
---|
98 | static bool find_output_dev(service_id_t *svcid)
|
---|
99 | {
|
---|
100 | service_id_t *svc;
|
---|
101 | size_t svcs;
|
---|
102 | errno_t rc;
|
---|
103 |
|
---|
104 | rc = loc_category_get_svcs(serial_cat_id, &svc, &svcs);
|
---|
105 | if (rc != EOK) {
|
---|
106 | fibril_mutex_unlock(&discovery_lock);
|
---|
107 | printf("%s: Failed to get services\n", NAME);
|
---|
108 | return false;
|
---|
109 | }
|
---|
110 |
|
---|
111 | for (size_t i = 0; i < svcs; i++) {
|
---|
112 | char *name;
|
---|
113 |
|
---|
114 | rc = loc_service_get_name(svc[i], &name);
|
---|
115 | if (rc != EOK)
|
---|
116 | continue;
|
---|
117 |
|
---|
118 | if (!str_cmp(console, name)) {
|
---|
119 | /*
|
---|
120 | * This is the serial console service that the user
|
---|
121 | * wanted to use.
|
---|
122 | */
|
---|
123 | *svcid = svc[i];
|
---|
124 | free(svc);
|
---|
125 | return true;
|
---|
126 | }
|
---|
127 |
|
---|
128 | free(name);
|
---|
129 | }
|
---|
130 |
|
---|
131 | free(svc);
|
---|
132 |
|
---|
133 | /* Look for any service in the 'console' category */
|
---|
134 |
|
---|
135 | rc = loc_category_get_svcs(console_cat_id, &svc, &svcs);
|
---|
136 | if (rc != EOK) {
|
---|
137 | fibril_mutex_unlock(&discovery_lock);
|
---|
138 | printf("%s: Failed to get services\n", NAME);
|
---|
139 | return false;
|
---|
140 | }
|
---|
141 |
|
---|
142 | if (svcs > 0) {
|
---|
143 | *svcid = svc[0];
|
---|
144 | free(svc);
|
---|
145 | return true;
|
---|
146 | }
|
---|
147 |
|
---|
148 | free(svc);
|
---|
149 | return false;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /*
|
---|
153 | * This callback scans all the services in the 'serial' category, hoping to see
|
---|
154 | * the single one the user wishes to use as a serial console. If it spots it, it
|
---|
155 | * connects to it and registers it as an output device. Then it unblocks the
|
---|
156 | * fibril blocked in chardev_init().
|
---|
157 | */
|
---|
158 | static void check_for_dev(void *arg)
|
---|
159 | {
|
---|
160 | errno_t rc;
|
---|
161 | bool found;
|
---|
162 | service_id_t sid;
|
---|
163 |
|
---|
164 | fibril_mutex_lock(&discovery_lock);
|
---|
165 | if (discovery_finished) {
|
---|
166 | // TODO: no need to receive these callbacks anymore
|
---|
167 | fibril_mutex_unlock(&discovery_lock);
|
---|
168 | return;
|
---|
169 | }
|
---|
170 |
|
---|
171 | found = find_output_dev(&sid);
|
---|
172 | if (!found) {
|
---|
173 | fibril_mutex_unlock(&discovery_lock);
|
---|
174 | return;
|
---|
175 | }
|
---|
176 |
|
---|
177 | printf("%s: Connecting service %zu\n", NAME, sid);
|
---|
178 | char *name;
|
---|
179 | rc = loc_service_get_name(sid, &name);
|
---|
180 | if (rc != EOK) {
|
---|
181 | fibril_mutex_unlock(&discovery_lock);
|
---|
182 | return;
|
---|
183 | }
|
---|
184 | printf("%s: Service name is %s\n", NAME, name);
|
---|
185 | free(name);
|
---|
186 |
|
---|
187 | sess = loc_service_connect(sid, INTERFACE_DDF, IPC_FLAG_BLOCKING);
|
---|
188 | if (!sess) {
|
---|
189 | fibril_mutex_unlock(&discovery_lock);
|
---|
190 | printf("%s: Failed connecting to device\n", NAME);
|
---|
191 | return;
|
---|
192 | }
|
---|
193 |
|
---|
194 | rc = chardev_open(sess, &chardev);
|
---|
195 | if (rc != EOK) {
|
---|
196 | fibril_mutex_unlock(&discovery_lock);
|
---|
197 | printf("%s: Failed opening character device\n", NAME);
|
---|
198 | return;
|
---|
199 | }
|
---|
200 |
|
---|
201 | serial_init(chardev_putwchar, chardev_control_puts, chardev_flush);
|
---|
202 |
|
---|
203 | discovery_finished = true;
|
---|
204 | fibril_condvar_signal(&discovery_cv);
|
---|
205 | fibril_mutex_unlock(&discovery_lock);
|
---|
206 | }
|
---|
207 |
|
---|
208 | errno_t chardev_init(void)
|
---|
209 | {
|
---|
210 | if (!config_key_exists("console")) {
|
---|
211 | console = NULL;
|
---|
212 | #ifdef MACHINE_ski
|
---|
213 | /* OK */
|
---|
214 | #elif defined(UARCH_sparc64) && defined(PROCESSOR_sun4v)
|
---|
215 | /* OK */
|
---|
216 | #elif defined(MACHINE_msim)
|
---|
217 | /* OK */
|
---|
218 | #else
|
---|
219 | return EOK;
|
---|
220 | #endif
|
---|
221 | } else {
|
---|
222 | console = config_get_value("console");
|
---|
223 | if (!console)
|
---|
224 | return EOK;
|
---|
225 | }
|
---|
226 |
|
---|
227 | errno_t rc = loc_category_get_id("serial", &serial_cat_id, IPC_FLAG_BLOCKING);
|
---|
228 | if (rc != EOK) {
|
---|
229 | printf("%s: Failed to get \"serial\" category ID.\n", NAME);
|
---|
230 | return rc;
|
---|
231 | }
|
---|
232 |
|
---|
233 | rc = loc_category_get_id("console", &console_cat_id, IPC_FLAG_BLOCKING);
|
---|
234 | if (rc != EOK) {
|
---|
235 | printf("%s: Failed to get \"console\" category ID.\n", NAME);
|
---|
236 | return rc;
|
---|
237 | }
|
---|
238 |
|
---|
239 | rc = loc_register_cat_change_cb(check_for_dev, NULL);
|
---|
240 | if (rc != EOK) {
|
---|
241 | printf("%s: Failed to register callback for device discovery.\n",
|
---|
242 | NAME);
|
---|
243 | return rc;
|
---|
244 | }
|
---|
245 |
|
---|
246 | check_for_dev(NULL);
|
---|
247 |
|
---|
248 | fibril_mutex_lock(&discovery_lock);
|
---|
249 | while (!discovery_finished)
|
---|
250 | fibril_condvar_wait(&discovery_cv, &discovery_lock);
|
---|
251 | fibril_mutex_unlock(&discovery_lock);
|
---|
252 |
|
---|
253 | return EOK;
|
---|
254 | }
|
---|
255 |
|
---|
256 | /** @}
|
---|
257 | */
|
---|