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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c8ea6eca was 4122410, checked in by Jakub Jermar <jakub@…>, 7 years ago

Improve Doxygen documentaion

This is stil WiP. A number of libraries, drivers and services were
converted to using a more hierarchical and decentralized scheme when it
comes to specifying to which doxygen group they belong.

  • Property mode set to 100644
File size: 6.0 KB
RevLine 
[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]49enum {
50 chardev_buf_size = 4096
51};
52
[7259317]53static char *console;
54
[e53794c]55static async_sess_t *sess;
[74017ce]56static chardev_t *chardev;
[e53794c]57static service_id_t serial_cat_id;
[9940ce0]58static service_id_t console_cat_id;
[e53794c]59
[bbc6277]60static uint8_t chardev_buf[chardev_buf_size];
61static size_t chardev_bused;
62
[e53794c]63static FIBRIL_MUTEX_INITIALIZE(discovery_lock);
64static bool discovery_finished;
65static FIBRIL_CONDVAR_INITIALIZE(discovery_cv);
66
[bbc6277]67static 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
[ed88c8e]80static void chardev_putwchar(wchar_t ch)
[bbc6277]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;
[e53794c]87}
88
89static void chardev_control_puts(const char *str)
90{
[bbc6277]91 const char *p;
92
93 p = str;
94 while (*p != '\0')
[ed88c8e]95 chardev_putwchar(*p++);
[e53794c]96}
97
[9940ce0]98static bool find_output_dev(service_id_t *svcid)
[e53794c]99{
100 service_id_t *svc;
101 size_t svcs;
[b7fd2a0]102 errno_t rc;
[9940ce0]103
[e53794c]104 rc = loc_category_get_svcs(serial_cat_id, &svc, &svcs);
105 if (rc != EOK) {
[7259317]106 fibril_mutex_unlock(&discovery_lock);
[e53794c]107 printf("%s: Failed to get services\n", NAME);
[9940ce0]108 return false;
[e53794c]109 }
110
[9940ce0]111 for (size_t i = 0; i < svcs; i++) {
[7259317]112 char *name;
[9940ce0]113
[7259317]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 */
[9940ce0]123 *svcid = svc[i];
124 free(svc);
125 return true;
[7259317]126 }
[9940ce0]127
[7259317]128 free(name);
[e53794c]129 }
130
131 free(svc);
132
[9940ce0]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 */
[e89a06a]158static void check_for_dev(void *arg)
[9940ce0]159{
[b7fd2a0]160 errno_t rc;
[9940ce0]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);
[7259317]172 if (!found) {
173 fibril_mutex_unlock(&discovery_lock);
174 return;
175 }
176
[5f4c41b2]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
[e53794c]187 sess = loc_service_connect(sid, INTERFACE_DDF, IPC_FLAG_BLOCKING);
188 if (!sess) {
[7259317]189 fibril_mutex_unlock(&discovery_lock);
[e53794c]190 printf("%s: Failed connecting to device\n", NAME);
191 return;
192 }
[74017ce]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
[ed88c8e]201 serial_init(chardev_putwchar, chardev_control_puts, chardev_flush);
[e53794c]202
203 discovery_finished = true;
204 fibril_condvar_signal(&discovery_cv);
205 fibril_mutex_unlock(&discovery_lock);
206}
207
[b7fd2a0]208errno_t chardev_init(void)
[e53794c]209{
[9940ce0]210 if (!config_key_exists("console")) {
211 console = NULL;
[5f4c41b2]212#ifdef MACHINE_ski
213 /* OK */
214#elif defined(UARCH_sparc64) && defined(PROCESSOR_sun4v)
215 /* OK */
[c309b18]216#elif defined(MACHINE_msim)
217 /* OK */
[5f4c41b2]218#else
[00130656]219 return EOK;
[9940ce0]220#endif
221 } else {
222 console = config_get_value("console");
223 if (!console)
224 return EOK;
[00130656]225 }
226
[b7fd2a0]227 errno_t rc = loc_category_get_id("serial", &serial_cat_id, IPC_FLAG_BLOCKING);
[e53794c]228 if (rc != EOK) {
229 printf("%s: Failed to get \"serial\" category ID.\n", NAME);
230 return rc;
[1b20da0]231 }
[e53794c]232
[9940ce0]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;
[1b20da0]237 }
[9940ce0]238
[e89a06a]239 rc = loc_register_cat_change_cb(check_for_dev, NULL);
[e53794c]240 if (rc != EOK) {
241 printf("%s: Failed to register callback for device discovery.\n",
242 NAME);
243 return rc;
244 }
245
[e89a06a]246 check_for_dev(NULL);
[9940ce0]247
[e53794c]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 */
Note: See TracBrowser for help on using the repository browser.