Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/hid/output/port/chardev.c

    rbbc6277 r8d2dd7f2  
    11/*
    22 * Copyright (c) 2016 Jakub Jermar
    3  * Copyright (c) 2017 Jiri Svoboda
    43 * All rights reserved.
    54 *
     
    3130 */
    3231
    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>
    3932#include <stddef.h>
    4033#include <stdint.h>
     34#include <char_dev_iface.h>
    4135#include <stdio.h>
    4236#include <stdlib.h>
     37#include <async.h>
     38#include <fibril_synch.h>
     39#include <loc.h>
     40#include <errno.h>
    4341#include <str.h>
     42#include <config.h>
    4443#include "../ctl/serial.h"
    4544#include "../output.h"
    4645#include "chardev.h"
    4746
    48 enum {
    49         chardev_buf_size = 4096
    50 };
    51 
    5247static char *console;
    5348
    5449static async_sess_t *sess;
    55 static chardev_t *chardev;
    5650static 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;
    6151
    6252static FIBRIL_MUTEX_INITIALIZE(discovery_lock);
     
    6454static FIBRIL_CONDVAR_INITIALIZE(discovery_cv);
    6555
    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 
    7956static void chardev_putchar(wchar_t ch)
    8057{
    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;
     58        uint8_t byte = (uint8_t) ch;
     59        char_dev_write(sess, &byte, 1);
    8660}
    8761
    8862static void chardev_control_puts(const char *str)
    8963{
    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;
     64        char_dev_write(sess, (void *) str, str_size(str));
    14965}
    15066
     
    15874{
    15975        int rc;
    160         bool found;
    161         service_id_t sid;
    16276
    16377        fibril_mutex_lock(&discovery_lock);
     
    16882        }
    16983
    170         found = find_output_dev(&sid);
     84        service_id_t *svc;
     85        size_t svcs;
     86        rc = loc_category_get_svcs(serial_cat_id, &svc, &svcs);
     87        if (rc != EOK) {
     88                fibril_mutex_unlock(&discovery_lock);
     89                printf("%s: Failed to get services\n", NAME);
     90                return;
     91        }
     92
     93        service_id_t sid;
     94        bool found = false;
     95
     96        for (size_t i = 0; i < svcs && !found; i++) {
     97                char *name;
     98               
     99                rc = loc_service_get_name(svc[i], &name);
     100                if (rc != EOK)
     101                        continue;
     102
     103                if (!str_cmp(console, name)) {
     104                        /*
     105                         * This is the serial console service that the user
     106                         * wanted to use.
     107                         */
     108                        found = true;
     109                        sid = svc[i];
     110                }
     111                       
     112                free(name);
     113        }
     114
     115        free(svc);
     116
    171117        if (!found) {
    172118                fibril_mutex_unlock(&discovery_lock);
    173119                return;
    174120        }
    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);
    185121
    186122        sess = loc_service_connect(sid, INTERFACE_DDF, IPC_FLAG_BLOCKING);
     
    190126                return;
    191127        }
    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);
     128        serial_init(chardev_putchar, chardev_control_puts);
    201129
    202130        discovery_finished = true;
     
    207135int chardev_init(void)
    208136{
    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
     137        console = config_get_value("console");
     138        if (!console) {
     139                /*
     140                 * The system is not configured to use serial console.
     141                 */
    218142                return EOK;
    219 #endif
    220         } else {
    221                 console = config_get_value("console");
    222                 if (!console)
    223                         return EOK;
    224143        }
    225144
     
    227146        if (rc != EOK) {
    228147                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);
    235148                return rc;
    236149        }
     
    242155                return rc;
    243156        }
    244 
    245         check_for_dev();
    246157
    247158        fibril_mutex_lock(&discovery_lock);
Note: See TracChangeset for help on using the changeset viewer.