[e53794c] | 1 | /*
|
---|
[d3109ff] | 2 | * Copyright (c) 2024 Jiri Svoboda
|
---|
[e53794c] | 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 |
|
---|
[4122410] | 30 | /** @addtogropu output
|
---|
| 31 | * @{
|
---|
[e53794c] | 32 | */
|
---|
| 33 |
|
---|
[74017ce] | 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>
|
---|
[8d2dd7f2] | 40 | #include <stddef.h>
|
---|
| 41 | #include <stdint.h>
|
---|
[e53794c] | 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 |
|
---|
[bbc6277] | 49 | enum {
|
---|
| 50 | chardev_buf_size = 4096
|
---|
| 51 | };
|
---|
| 52 |
|
---|
[7259317] | 53 | static char *console;
|
---|
| 54 |
|
---|
[e53794c] | 55 | static async_sess_t *sess;
|
---|
[74017ce] | 56 | static chardev_t *chardev;
|
---|
[e53794c] | 57 | static service_id_t serial_cat_id;
|
---|
[9940ce0] | 58 | static service_id_t console_cat_id;
|
---|
[e53794c] | 59 |
|
---|
[bbc6277] | 60 | static uint8_t chardev_buf[chardev_buf_size];
|
---|
| 61 | static size_t chardev_bused;
|
---|
| 62 |
|
---|
[e53794c] | 63 | static FIBRIL_MUTEX_INITIALIZE(discovery_lock);
|
---|
| 64 | static bool discovery_finished;
|
---|
| 65 | static FIBRIL_CONDVAR_INITIALIZE(discovery_cv);
|
---|
| 66 |
|
---|
[d3109ff] | 67 | static void chardev_flush(void *arg)
|
---|
[e53794c] | 68 | {
|
---|
[74017ce] | 69 | size_t nwr;
|
---|
[bbc6277] | 70 |
|
---|
[d3109ff] | 71 | (void)arg;
|
---|
| 72 |
|
---|
[bbc6277] | 73 | if (chardev_bused == 0)
|
---|
| 74 | return;
|
---|
| 75 |
|
---|
| 76 | chardev_write(chardev, chardev_buf, chardev_bused, &nwr);
|
---|
[74017ce] | 77 | /* XXX Handle error */
|
---|
[bbc6277] | 78 |
|
---|
| 79 | chardev_bused = 0;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[b79c91cc] | 82 | static void chardev_putchar(char ch)
|
---|
[bbc6277] | 83 | {
|
---|
| 84 | if (chardev_bused == chardev_buf_size)
|
---|
[d3109ff] | 85 | chardev_flush(NULL);
|
---|
[bbc6277] | 86 | chardev_buf[chardev_bused++] = (uint8_t) ch;
|
---|
[e53794c] | 87 | }
|
---|
| 88 |
|
---|
[d3109ff] | 89 | static void chardev_putuchar(void *arg, char32_t ch)
|
---|
[b79c91cc] | 90 | {
|
---|
| 91 | char buf[STR_BOUNDS(1)];
|
---|
| 92 | size_t off;
|
---|
| 93 | size_t i;
|
---|
| 94 | errno_t rc;
|
---|
| 95 |
|
---|
[d3109ff] | 96 | (void)arg;
|
---|
| 97 |
|
---|
[b79c91cc] | 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 |
|
---|
[d3109ff] | 107 | static void chardev_control_puts(void *arg, const char *str)
|
---|
[e53794c] | 108 | {
|
---|
[bbc6277] | 109 | const char *p;
|
---|
| 110 |
|
---|
| 111 | p = str;
|
---|
| 112 | while (*p != '\0')
|
---|
[d3109ff] | 113 | chardev_putuchar(arg, *p++);
|
---|
[e53794c] | 114 | }
|
---|
| 115 |
|
---|
[9940ce0] | 116 | static bool find_output_dev(service_id_t *svcid)
|
---|
[e53794c] | 117 | {
|
---|
| 118 | service_id_t *svc;
|
---|
| 119 | size_t svcs;
|
---|
[b7fd2a0] | 120 | errno_t rc;
|
---|
[9940ce0] | 121 |
|
---|
[e53794c] | 122 | rc = loc_category_get_svcs(serial_cat_id, &svc, &svcs);
|
---|
| 123 | if (rc != EOK) {
|
---|
[7259317] | 124 | fibril_mutex_unlock(&discovery_lock);
|
---|
[e53794c] | 125 | printf("%s: Failed to get services\n", NAME);
|
---|
[9940ce0] | 126 | return false;
|
---|
[e53794c] | 127 | }
|
---|
| 128 |
|
---|
[9940ce0] | 129 | for (size_t i = 0; i < svcs; i++) {
|
---|
[7259317] | 130 | char *name;
|
---|
[9940ce0] | 131 |
|
---|
[7259317] | 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 | */
|
---|
[9940ce0] | 141 | *svcid = svc[i];
|
---|
| 142 | free(svc);
|
---|
| 143 | return true;
|
---|
[7259317] | 144 | }
|
---|
[9940ce0] | 145 |
|
---|
[7259317] | 146 | free(name);
|
---|
[e53794c] | 147 | }
|
---|
| 148 |
|
---|
| 149 | free(svc);
|
---|
| 150 |
|
---|
[9940ce0] | 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 | */
|
---|
[e89a06a] | 176 | static void check_for_dev(void *arg)
|
---|
[9940ce0] | 177 | {
|
---|
[b7fd2a0] | 178 | errno_t rc;
|
---|
[9940ce0] | 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);
|
---|
[7259317] | 190 | if (!found) {
|
---|
| 191 | fibril_mutex_unlock(&discovery_lock);
|
---|
| 192 | return;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[5f4c41b2] | 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 |
|
---|
[e53794c] | 205 | sess = loc_service_connect(sid, INTERFACE_DDF, IPC_FLAG_BLOCKING);
|
---|
| 206 | if (!sess) {
|
---|
[7259317] | 207 | fibril_mutex_unlock(&discovery_lock);
|
---|
[e53794c] | 208 | printf("%s: Failed connecting to device\n", NAME);
|
---|
| 209 | return;
|
---|
| 210 | }
|
---|
[74017ce] | 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 |
|
---|
[28a5ebd] | 219 | serial_init(chardev_putuchar, chardev_control_puts, chardev_flush);
|
---|
[e53794c] | 220 |
|
---|
| 221 | discovery_finished = true;
|
---|
| 222 | fibril_condvar_signal(&discovery_cv);
|
---|
| 223 | fibril_mutex_unlock(&discovery_lock);
|
---|
| 224 | }
|
---|
| 225 |
|
---|
[b7fd2a0] | 226 | errno_t chardev_init(void)
|
---|
[e53794c] | 227 | {
|
---|
[9940ce0] | 228 | if (!config_key_exists("console")) {
|
---|
| 229 | console = NULL;
|
---|
[5f4c41b2] | 230 | #ifdef MACHINE_ski
|
---|
| 231 | /* OK */
|
---|
| 232 | #elif defined(UARCH_sparc64) && defined(PROCESSOR_sun4v)
|
---|
| 233 | /* OK */
|
---|
[c309b18] | 234 | #elif defined(MACHINE_msim)
|
---|
| 235 | /* OK */
|
---|
[7348c4b] | 236 | #elif defined(UARCH_arm64) && defined(MACHINE_virt)
|
---|
| 237 | /* OK */
|
---|
[06f10ac] | 238 | #elif defined(UARCH_arm64) && defined(MACHINE_hikey960)
|
---|
| 239 | /* OK */
|
---|
[5f4c41b2] | 240 | #else
|
---|
[00130656] | 241 | return EOK;
|
---|
[9940ce0] | 242 | #endif
|
---|
| 243 | } else {
|
---|
| 244 | console = config_get_value("console");
|
---|
| 245 | if (!console)
|
---|
| 246 | return EOK;
|
---|
[00130656] | 247 | }
|
---|
| 248 |
|
---|
[b7fd2a0] | 249 | errno_t rc = loc_category_get_id("serial", &serial_cat_id, IPC_FLAG_BLOCKING);
|
---|
[e53794c] | 250 | if (rc != EOK) {
|
---|
| 251 | printf("%s: Failed to get \"serial\" category ID.\n", NAME);
|
---|
| 252 | return rc;
|
---|
[1b20da0] | 253 | }
|
---|
[e53794c] | 254 |
|
---|
[9940ce0] | 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;
|
---|
[1b20da0] | 259 | }
|
---|
[9940ce0] | 260 |
|
---|
[e89a06a] | 261 | rc = loc_register_cat_change_cb(check_for_dev, NULL);
|
---|
[e53794c] | 262 | if (rc != EOK) {
|
---|
| 263 | printf("%s: Failed to register callback for device discovery.\n",
|
---|
| 264 | NAME);
|
---|
| 265 | return rc;
|
---|
| 266 | }
|
---|
| 267 |
|
---|
[e89a06a] | 268 | check_for_dev(NULL);
|
---|
[9940ce0] | 269 |
|
---|
[e53794c] | 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 | */
|
---|