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