[e53794c] | 1 | /*
|
---|
| 2 | * Copyright (c) 2016 Jakub Jermar
|
---|
[9940ce0] | 3 | * Copyright (c) 2017 Jiri Svoboda
|
---|
[e53794c] | 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 |
|
---|
[bbc6277] | 67 | static void chardev_flush(void)
|
---|
[e53794c] | 68 | {
|
---|
[74017ce] | 69 | size_t nwr;
|
---|
[bbc6277] | 70 |
|
---|
| 71 | if (chardev_bused == 0)
|
---|
| 72 | return;
|
---|
| 73 |
|
---|
| 74 | chardev_write(chardev, chardev_buf, chardev_bused, &nwr);
|
---|
[74017ce] | 75 | /* XXX Handle error */
|
---|
[bbc6277] | 76 |
|
---|
| 77 | chardev_bused = 0;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[b79c91cc] | 80 | static void chardev_putchar(char ch)
|
---|
[bbc6277] | 81 | {
|
---|
| 82 | if (chardev_bused == chardev_buf_size)
|
---|
| 83 | chardev_flush();
|
---|
| 84 | chardev_buf[chardev_bused++] = (uint8_t) ch;
|
---|
[e53794c] | 85 | }
|
---|
| 86 |
|
---|
[b79c91cc] | 87 | static 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 |
|
---|
[e53794c] | 103 | static void chardev_control_puts(const char *str)
|
---|
| 104 | {
|
---|
[bbc6277] | 105 | const char *p;
|
---|
| 106 |
|
---|
| 107 | p = str;
|
---|
| 108 | while (*p != '\0')
|
---|
[28a5ebd] | 109 | chardev_putuchar(*p++);
|
---|
[e53794c] | 110 | }
|
---|
| 111 |
|
---|
[9940ce0] | 112 | static bool find_output_dev(service_id_t *svcid)
|
---|
[e53794c] | 113 | {
|
---|
| 114 | service_id_t *svc;
|
---|
| 115 | size_t svcs;
|
---|
[b7fd2a0] | 116 | errno_t rc;
|
---|
[9940ce0] | 117 |
|
---|
[e53794c] | 118 | rc = loc_category_get_svcs(serial_cat_id, &svc, &svcs);
|
---|
| 119 | if (rc != EOK) {
|
---|
[7259317] | 120 | fibril_mutex_unlock(&discovery_lock);
|
---|
[e53794c] | 121 | printf("%s: Failed to get services\n", NAME);
|
---|
[9940ce0] | 122 | return false;
|
---|
[e53794c] | 123 | }
|
---|
| 124 |
|
---|
[9940ce0] | 125 | for (size_t i = 0; i < svcs; i++) {
|
---|
[7259317] | 126 | char *name;
|
---|
[9940ce0] | 127 |
|
---|
[7259317] | 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 | */
|
---|
[9940ce0] | 137 | *svcid = svc[i];
|
---|
| 138 | free(svc);
|
---|
| 139 | return true;
|
---|
[7259317] | 140 | }
|
---|
[9940ce0] | 141 |
|
---|
[7259317] | 142 | free(name);
|
---|
[e53794c] | 143 | }
|
---|
| 144 |
|
---|
| 145 | free(svc);
|
---|
| 146 |
|
---|
[9940ce0] | 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 | */
|
---|
[e89a06a] | 172 | static void check_for_dev(void *arg)
|
---|
[9940ce0] | 173 | {
|
---|
[b7fd2a0] | 174 | errno_t rc;
|
---|
[9940ce0] | 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);
|
---|
[7259317] | 186 | if (!found) {
|
---|
| 187 | fibril_mutex_unlock(&discovery_lock);
|
---|
| 188 | return;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[5f4c41b2] | 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 |
|
---|
[e53794c] | 201 | sess = loc_service_connect(sid, INTERFACE_DDF, IPC_FLAG_BLOCKING);
|
---|
| 202 | if (!sess) {
|
---|
[7259317] | 203 | fibril_mutex_unlock(&discovery_lock);
|
---|
[e53794c] | 204 | printf("%s: Failed connecting to device\n", NAME);
|
---|
| 205 | return;
|
---|
| 206 | }
|
---|
[74017ce] | 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 |
|
---|
[28a5ebd] | 215 | serial_init(chardev_putuchar, chardev_control_puts, chardev_flush);
|
---|
[e53794c] | 216 |
|
---|
| 217 | discovery_finished = true;
|
---|
| 218 | fibril_condvar_signal(&discovery_cv);
|
---|
| 219 | fibril_mutex_unlock(&discovery_lock);
|
---|
| 220 | }
|
---|
| 221 |
|
---|
[b7fd2a0] | 222 | errno_t chardev_init(void)
|
---|
[e53794c] | 223 | {
|
---|
[9940ce0] | 224 | if (!config_key_exists("console")) {
|
---|
| 225 | console = NULL;
|
---|
[5f4c41b2] | 226 | #ifdef MACHINE_ski
|
---|
| 227 | /* OK */
|
---|
| 228 | #elif defined(UARCH_sparc64) && defined(PROCESSOR_sun4v)
|
---|
| 229 | /* OK */
|
---|
[c309b18] | 230 | #elif defined(MACHINE_msim)
|
---|
| 231 | /* OK */
|
---|
[7348c4b] | 232 | #elif defined(UARCH_arm64) && defined(MACHINE_virt)
|
---|
| 233 | /* OK */
|
---|
[06f10ac] | 234 | #elif defined(UARCH_arm64) && defined(MACHINE_hikey960)
|
---|
| 235 | /* OK */
|
---|
[5f4c41b2] | 236 | #else
|
---|
[00130656] | 237 | return EOK;
|
---|
[9940ce0] | 238 | #endif
|
---|
| 239 | } else {
|
---|
| 240 | console = config_get_value("console");
|
---|
| 241 | if (!console)
|
---|
| 242 | return EOK;
|
---|
[00130656] | 243 | }
|
---|
| 244 |
|
---|
[b7fd2a0] | 245 | errno_t rc = loc_category_get_id("serial", &serial_cat_id, IPC_FLAG_BLOCKING);
|
---|
[e53794c] | 246 | if (rc != EOK) {
|
---|
| 247 | printf("%s: Failed to get \"serial\" category ID.\n", NAME);
|
---|
| 248 | return rc;
|
---|
[1b20da0] | 249 | }
|
---|
[e53794c] | 250 |
|
---|
[9940ce0] | 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;
|
---|
[1b20da0] | 255 | }
|
---|
[9940ce0] | 256 |
|
---|
[e89a06a] | 257 | rc = loc_register_cat_change_cb(check_for_dev, NULL);
|
---|
[e53794c] | 258 | if (rc != EOK) {
|
---|
| 259 | printf("%s: Failed to register callback for device discovery.\n",
|
---|
| 260 | NAME);
|
---|
| 261 | return rc;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
[e89a06a] | 264 | check_for_dev(NULL);
|
---|
[9940ce0] | 265 |
|
---|
[e53794c] | 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 | */
|
---|