Changeset ac307b2 in mainline for uspace/drv


Ignore:
Timestamp:
2017-11-25T11:12:23Z (8 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
98cb5e0d
Parents:
f571ca49 (diff), 0851a3d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into callcaps

Location:
uspace/drv
Files:
1 deleted
13 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/char/i8042/i8042.c

    rf571ca49 rac307b2  
    22 * Copyright (c) 2001-2004 Jakub Jermar
    33 * Copyright (c) 2006 Josef Cejka
    4  * Copyright (c) 2014 Jiri Svoboda
     4 * Copyright (c) 2017 Jiri Svoboda
    55 * Copyright (c) 2011 Jan Vesely
    66 * All rights reserved.
     
    3939 */
    4040
     41#include <adt/circ_buf.h>
    4142#include <ddf/log.h>
    4243#include <ddf/interrupt.h>
     
    130131{
    131132        i8042_t *controller = ddf_dev_data_get(dev);
     133        int rc;
    132134       
    133135        const uint8_t status = IPC_GET_ARG1(*call);
    134136        const uint8_t data = IPC_GET_ARG2(*call);
    135137       
    136         buffer_t *buffer = (status & i8042_AUX_DATA) ?
    137             &controller->aux_buffer : &controller->kbd_buffer;
    138        
    139         buffer_write(buffer, data);
     138        i8042_port_t *port = (status & i8042_AUX_DATA) ?
     139            controller->aux : controller->kbd;
     140       
     141        fibril_mutex_lock(&port->buf_lock);
     142       
     143        rc = circ_buf_push(&port->cbuf, &data);
     144        if (rc != EOK)
     145                ddf_msg(LVL_ERROR, "Buffer overrun");
     146
     147        fibril_mutex_unlock(&port->buf_lock);
     148        fibril_condvar_broadcast(&port->buf_cv);
    140149}
    141150
     
    159168        const size_t cmd_count = sizeof(i8042_cmds) / sizeof(irq_cmd_t);
    160169        irq_cmd_t cmds[cmd_count];
     170        ddf_fun_t *kbd_fun;
     171        ddf_fun_t *aux_fun;
    161172        i8042_regs_t *ar;
    162 
     173       
    163174        int rc;
    164175        bool kbd_bound = false;
    165176        bool aux_bound = false;
    166 
    167         dev->kbd_fun = NULL;
    168         dev->aux_fun = NULL;
    169177       
    170178        if (regs->size < sizeof(i8042_regs_t)) {
     
    178186        }
    179187       
    180         dev->kbd_fun = ddf_fun_create(ddf_dev, fun_inner, "ps2a");
    181         if (dev->kbd_fun == NULL) {
     188        kbd_fun = ddf_fun_create(ddf_dev, fun_inner, "ps2a");
     189        if (kbd_fun == NULL) {
    182190                rc = ENOMEM;
    183191                goto error;
    184192        };
    185193       
    186         dev->kbd = ddf_fun_data_alloc(dev->kbd_fun, sizeof(i8042_port_t));
     194        dev->kbd = ddf_fun_data_alloc(kbd_fun, sizeof(i8042_port_t));
    187195        if (dev->kbd == NULL) {
    188196                rc = ENOMEM;
     
    190198        }
    191199       
     200        dev->kbd->fun = kbd_fun;
    192201        dev->kbd->ctl = dev;
    193202        chardev_srvs_init(&dev->kbd->cds);
    194203        dev->kbd->cds.ops = &i8042_chardev_ops;
    195204        dev->kbd->cds.sarg = dev->kbd;
    196        
    197         rc = ddf_fun_add_match_id(dev->kbd_fun, "char/xtkbd", 90);
     205        fibril_mutex_initialize(&dev->kbd->buf_lock);
     206        fibril_condvar_initialize(&dev->kbd->buf_cv);
     207       
     208        rc = ddf_fun_add_match_id(dev->kbd->fun, "char/xtkbd", 90);
    198209        if (rc != EOK)
    199210                goto error;
    200211       
    201         dev->aux_fun = ddf_fun_create(ddf_dev, fun_inner, "ps2b");
    202         if (dev->aux_fun == NULL) {
     212        aux_fun = ddf_fun_create(ddf_dev, fun_inner, "ps2b");
     213        if (aux_fun == NULL) {
    203214                rc = ENOMEM;
    204215                goto error;
    205216        }
    206217       
    207         dev->aux = ddf_fun_data_alloc(dev->aux_fun, sizeof(i8042_port_t));
     218        dev->aux = ddf_fun_data_alloc(aux_fun, sizeof(i8042_port_t));
    208219        if (dev->aux == NULL) {
    209220                rc = ENOMEM;
     
    211222        }
    212223       
     224        dev->aux->fun = aux_fun;
    213225        dev->aux->ctl = dev;
    214226        chardev_srvs_init(&dev->aux->cds);
    215227        dev->aux->cds.ops = &i8042_chardev_ops;
    216228        dev->aux->cds.sarg = dev->aux;
    217        
    218         rc = ddf_fun_add_match_id(dev->aux_fun, "char/ps2mouse", 90);
     229        fibril_mutex_initialize(&dev->aux->buf_lock);
     230        fibril_condvar_initialize(&dev->aux->buf_cv);
     231       
     232        rc = ddf_fun_add_match_id(dev->aux->fun, "char/ps2mouse", 90);
    219233        if (rc != EOK)
    220234                goto error;
    221235       
    222         ddf_fun_set_conn_handler(dev->kbd_fun, i8042_char_conn);
    223         ddf_fun_set_conn_handler(dev->aux_fun, i8042_char_conn);
    224        
    225         buffer_init(&dev->kbd_buffer, dev->kbd_data, BUFFER_SIZE);
    226         buffer_init(&dev->aux_buffer, dev->aux_data, BUFFER_SIZE);
     236        ddf_fun_set_conn_handler(dev->kbd->fun, i8042_char_conn);
     237        ddf_fun_set_conn_handler(dev->aux->fun, i8042_char_conn);
     238       
     239        circ_buf_init(&dev->kbd->cbuf, dev->kbd->buf_data, BUFFER_SIZE, 1);
     240        circ_buf_init(&dev->aux->cbuf, dev->aux->buf_data, BUFFER_SIZE, 1);
    227241        fibril_mutex_initialize(&dev->write_guard);
    228242       
    229         rc = ddf_fun_bind(dev->kbd_fun);
     243        rc = ddf_fun_bind(dev->kbd->fun);
    230244        if (rc != EOK) {
    231245                ddf_msg(LVL_ERROR, "Failed to bind keyboard function: %s.",
    232                     ddf_fun_get_name(dev->kbd_fun));
     246                    ddf_fun_get_name(dev->kbd->fun));
    233247                goto error;
    234248        }
    235249        kbd_bound = true;
    236250       
    237         rc = ddf_fun_bind(dev->aux_fun);
     251        rc = ddf_fun_bind(dev->aux->fun);
    238252        if (rc != EOK) {
    239253                ddf_msg(LVL_ERROR, "Failed to bind aux function: %s.",
    240                     ddf_fun_get_name(dev->aux_fun));
     254                    ddf_fun_get_name(dev->aux->fun));
    241255                goto error;
    242256        }
     
    317331error:
    318332        if (kbd_bound)
    319                 ddf_fun_unbind(dev->kbd_fun);
     333                ddf_fun_unbind(dev->kbd->fun);
    320334        if (aux_bound)
    321                 ddf_fun_unbind(dev->aux_fun);
    322         if (dev->kbd_fun != NULL)
    323                 ddf_fun_destroy(dev->kbd_fun);
    324         if (dev->aux_fun != NULL)
    325                 ddf_fun_destroy(dev->aux_fun);
     335                ddf_fun_unbind(dev->aux->fun);
     336        if (dev->kbd->fun != NULL)
     337                ddf_fun_destroy(dev->kbd->fun);
     338        if (dev->aux->fun != NULL)
     339                ddf_fun_destroy(dev->aux->fun);
    326340
    327341        return rc;
     
    377391{
    378392        i8042_port_t *port = (i8042_port_t *)srv->srvs->sarg;
    379         i8042_t *i8042 = port->ctl;
     393        size_t p;
    380394        uint8_t *destp = (uint8_t *)dest;
    381395        int rc;
    382         size_t i;
    383        
    384         buffer_t *buffer = (port == i8042->aux) ?
    385             &i8042->aux_buffer : &i8042->kbd_buffer;
    386        
    387         for (i = 0; i < size; ++i) {
    388                 rc = buffer_read(buffer, destp, i == 0);
     396       
     397        fibril_mutex_lock(&port->buf_lock);
     398       
     399        while (circ_buf_nused(&port->cbuf) == 0)
     400                fibril_condvar_wait(&port->buf_cv, &port->buf_lock);
     401
     402        p = 0;
     403        while (p < size) {
     404                rc = circ_buf_pop(&port->cbuf, &destp[p]);
    389405                if (rc != EOK)
    390406                        break;
    391                 ++destp;
    392         }
    393        
    394         *nread = i;
     407                ++p;
     408        }
     409
     410        fibril_mutex_unlock(&port->buf_lock);
     411
     412        *nread = p;
    395413        return EOK;
    396414}
  • uspace/drv/char/i8042/i8042.h

    rf571ca49 rac307b2  
    22 * Copyright (c) 2006 Josef Cejka
    33 * Copyright (c) 2011 Jan Vesely
     4 * Copyright (c) 2017 Jiri Svoboda
    45 * All rights reserved.
    56 *
     
    4041#define i8042_H_
    4142
     43#include <adt/circ_buf.h>
    4244#include <io/chardev_srv.h>
    4345#include <ddi.h>
    4446#include <fibril_synch.h>
    4547#include <ddf/driver.h>
    46 #include "buffer.h"
    4748
    4849#define NAME  "i8042"
     
    5960/** i8042 Port. */
    6061typedef struct {
    61         struct i8042 *ctl;              /**< Controller */
    62         chardev_srvs_t cds;             /**< Character device server data */
     62        /** Controller */
     63        struct i8042 *ctl;
     64        /** Device function */
     65        ddf_fun_t *fun;
     66        /** Character device server data */
     67        chardev_srvs_t cds;
     68        /** Circular buffer */
     69        circ_buf_t cbuf;
     70        /** Buffer data space */
     71        uint8_t buf_data[BUFFER_SIZE];
     72        /** Protect buffer */
     73        fibril_mutex_t buf_lock;
     74        /** Signal new data in buffer */
     75        fibril_condvar_t buf_cv;
    6376} i8042_port_t;
    6477
    6578/** i8042 Controller. */
    6679typedef struct i8042 {
    67         i8042_regs_t *regs;             /**< I/O registers. */
    68         ddf_fun_t *kbd_fun;             /**< Pirmary port device function. */
    69         ddf_fun_t *aux_fun;             /**< Auxiliary port device function. */
    70         buffer_t kbd_buffer;            /**< Primary port buffer. */
    71         buffer_t aux_buffer;            /**< Aux. port buffer. */
    72         uint8_t aux_data[BUFFER_SIZE];  /**< Primary port buffer space. */
    73         uint8_t kbd_data[BUFFER_SIZE];  /**< Aux. port buffer space. */
     80        /**< I/O registers. */
     81        i8042_regs_t *regs;
     82        /** Keyboard port */
    7483        i8042_port_t *kbd;
     84        /** AUX port */
    7585        i8042_port_t *aux;
    76         fibril_mutex_t write_guard;     /**< Prevents simultanous port writes.*/
     86        /** Prevents simultanous port writes.*/
     87        fibril_mutex_t write_guard;
    7788} i8042_t;
    78 
    7989
    8090extern int i8042_init(i8042_t *, addr_range_t *, int, int, ddf_dev_t *);
  • uspace/drv/char/msim-con/msim-con.c

    rf571ca49 rac307b2  
    3737#include <ddi.h>
    3838#include <errno.h>
    39 #include <ipc/char.h>
     39#include <io/chardev_srv.h>
    4040
    4141#include "msim-con.h"
    4242
    4343static void msim_con_connection(ipc_callid_t, ipc_call_t *, void *);
     44
     45static int msim_con_read(chardev_srv_t *, void *, size_t, size_t *);
     46static int msim_con_write(chardev_srv_t *, const void *, size_t, size_t *);
     47
     48static chardev_ops_t msim_con_chardev_ops = {
     49        .read = msim_con_read,
     50        .write = msim_con_write
     51};
    4452
    4553static irq_cmd_t msim_cmds_proto[] = {
     
    5866        msim_con_t *con = (msim_con_t *) arg;
    5967        uint8_t c;
     68        int rc;
     69
     70        fibril_mutex_lock(&con->buf_lock);
    6071
    6172        c = IPC_GET_ARG2(*call);
    62 
    63         if (con->client_sess != NULL) {
    64                 async_exch_t *exch = async_exchange_begin(con->client_sess);
    65                 async_msg_1(exch, CHAR_NOTIF_BYTE, c);
    66                 async_exchange_end(exch);
    67         }
     73        rc = circ_buf_push(&con->cbuf, &c);
     74        if (rc != EOK)
     75                ddf_msg(LVL_ERROR, "Buffer overrun");
     76
     77        fibril_mutex_unlock(&con->buf_lock);
     78        fibril_condvar_broadcast(&con->buf_cv);
    6879}
    6980
     
    7586        irq_cmd_t *msim_cmds = NULL;
    7687        int rc;
     88
     89        circ_buf_init(&con->cbuf, con->buf, msim_con_buf_size, 1);
     90        fibril_mutex_initialize(&con->buf_lock);
     91        fibril_condvar_initialize(&con->buf_cv);
    7792
    7893        msim_cmds = malloc(sizeof(msim_cmds_proto));
     
    106121        async_irq_subscribe(res->irq, msim_irq_handler, con, &con->irq_code);
    107122        subscribed = true;
     123
     124        chardev_srvs_init(&con->cds);
     125        con->cds.ops = &msim_con_chardev_ops;
     126        con->cds.sarg = con;
    108127
    109128        rc = ddf_fun_bind(fun);
     
    140159}
    141160
     161/** Read from msim console device */
     162static int msim_con_read(chardev_srv_t *srv, void *buf, size_t size,
     163    size_t *nread)
     164{
     165        msim_con_t *con = (msim_con_t *) srv->srvs->sarg;
     166        size_t p;
     167        uint8_t *bp = (uint8_t *) buf;
     168        int rc;
     169
     170        fibril_mutex_lock(&con->buf_lock);
     171
     172        while (circ_buf_nused(&con->cbuf) == 0)
     173                fibril_condvar_wait(&con->buf_cv, &con->buf_lock);
     174
     175        p = 0;
     176        while (p < size) {
     177                rc = circ_buf_pop(&con->cbuf, &bp[p]);
     178                if (rc != EOK)
     179                        break;
     180                ++p;
     181        }
     182
     183        fibril_mutex_unlock(&con->buf_lock);
     184
     185        *nread = p;
     186        return EOK;
     187}
     188
     189/** Write to msim console device */
     190static int msim_con_write(chardev_srv_t *srv, const void *data, size_t size,
     191    size_t *nwr)
     192{
     193        msim_con_t *con = (msim_con_t *) srv->srvs->sarg;
     194        size_t i;
     195        uint8_t *dp = (uint8_t *) data;
     196
     197        for (i = 0; i < size; i++)
     198                msim_con_putchar(con, dp[i]);
     199
     200        *nwr = size;
     201        return EOK;
     202}
     203
    142204/** Character device connection handler. */
    143205static void msim_con_connection(ipc_callid_t iid, ipc_call_t *icall,
    144206    void *arg)
    145207{
    146         msim_con_t *con;
    147 
    148         /* Answer the IPC_M_CONNECT_ME_TO call. */
    149         async_answer_0(iid, EOK);
    150 
    151         con = (msim_con_t *)ddf_dev_data_get(ddf_fun_get_dev((ddf_fun_t *)arg));
    152 
    153         while (true) {
    154                 ipc_call_t call;
    155                 ipc_callid_t callid = async_get_call(&call);
    156                 sysarg_t method = IPC_GET_IMETHOD(call);
    157 
    158                 if (!method) {
    159                         /* The other side has hung up. */
    160                         async_answer_0(callid, EOK);
    161                         return;
    162                 }
    163 
    164                 async_sess_t *sess =
    165                     async_callback_receive_start(EXCHANGE_SERIALIZE, &call);
    166                 if (sess != NULL) {
    167                         if (con->client_sess == NULL) {
    168                                 con->client_sess = sess;
    169                                 async_answer_0(callid, EOK);
    170                         } else
    171                                 async_answer_0(callid, ELIMIT);
    172                 } else {
    173                         switch (method) {
    174                         case CHAR_WRITE_BYTE:
    175                                 ddf_msg(LVL_DEBUG, "Write %" PRIun " to device\n",
    176                                     IPC_GET_ARG1(call));
    177                                 msim_con_putchar(con, (uint8_t) IPC_GET_ARG1(call));
    178                                 async_answer_0(callid, EOK);
    179                                 break;
    180                         default:
    181                                 async_answer_0(callid, EINVAL);
    182                         }
    183                 }
    184         }
     208        msim_con_t *con = (msim_con_t *) ddf_dev_data_get(
     209            ddf_fun_get_dev((ddf_fun_t *) arg));
     210
     211        chardev_conn(iid, icall, &con->cds);
    185212}
    186213
  • uspace/drv/char/msim-con/msim-con.h

    rf571ca49 rac307b2  
    3636#define MSIM_CON_H
    3737
     38#include <adt/circ_buf.h>
    3839#include <async.h>
    3940#include <ddf/driver.h>
     41#include <fibril_synch.h>
     42#include <io/chardev_srv.h>
    4043#include <loc.h>
    4144#include <stdint.h>
     45
     46enum {
     47        msim_con_buf_size = 64
     48};
    4249
    4350/** MSIM console resources */
     
    5158        async_sess_t *client_sess;
    5259        ddf_dev_t *dev;
     60        chardev_srvs_t cds;
    5361        msim_con_res_t res;
    5462        irq_pio_range_t irq_range[1];
    5563        irq_code_t irq_code;
     64        circ_buf_t cbuf;
     65        uint8_t buf[msim_con_buf_size];
     66        fibril_mutex_t buf_lock;
     67        fibril_condvar_t buf_cv;
    5668} msim_con_t;
    5769
  • uspace/drv/char/ns8250/ns8250.c

    rf571ca49 rac307b2  
    11/*
    22 * Copyright (c) 2010 Lenka Trochtova
    3  * Copyright (c) 2011 Jiri Svoboda
     3 * Copyright (c) 2017 Jiri Svoboda
    44 * All rights reserved.
    55 *
     
    5353#include <ddf/interrupt.h>
    5454#include <ddf/log.h>
    55 #include <ops/char_dev.h>
     55#include <io/chardev_srv.h>
    5656
    5757#include <device/hw_res.h>
     
    153153        /** DDF function node */
    154154        ddf_fun_t *fun;
     155        /** Character device service */
     156        chardev_srvs_t cds;
    155157        /** Parent session */
    156158        async_sess_t *parent_sess;
     
    189191}
    190192
     193/** Obtain soft-state structure from chardev srv */
     194static ns8250_t *srv_ns8250(chardev_srv_t *srv)
     195{
     196        return (ns8250_t *)srv->srvs->sarg;
     197}
     198
     199
    191200/** Find out if there is some incoming data available on the serial port.
    192201 *
     
    234243/** Read data from the serial port device.
    235244 *
    236  * @param fun           The serial port function
     245 * @param srv           Server-side connection data
    237246 * @param buf           The output buffer for read data.
    238247 * @param count         The number of bytes to be read.
    239  *
    240  * @return              The number of bytes actually read on success, negative
    241  *                      error number otherwise.
    242  */
    243 static int ns8250_read(ddf_fun_t *fun, char *buf, size_t count)
    244 {
    245         ns8250_t *ns = fun_ns8250(fun);
    246         int ret = 0;
    247        
    248         if (count == 0) return 0;
     248 * @param nread         Place to store number of bytes actually read
     249 *
     250 * @return              EOK on success or non-zero error code
     251 */
     252static int ns8250_read(chardev_srv_t *srv, void *buf, size_t count, size_t *nread)
     253{
     254        ns8250_t *ns = srv_ns8250(srv);
     255        char *bp = (char *) buf;
     256        size_t pos = 0;
     257       
     258        if (count == 0) {
     259                *nread = 0;
     260                return EOK;
     261        }
    249262       
    250263        fibril_mutex_lock(&ns->mutex);
    251264        while (buf_is_empty(&ns->input_buffer))
    252265                fibril_condvar_wait(&ns->input_buffer_available, &ns->mutex);
    253         while (!buf_is_empty(&ns->input_buffer) && (size_t)ret < count) {
    254                 buf[ret] = (char)buf_pop_front(&ns->input_buffer);
    255                 ret++;
     266        while (!buf_is_empty(&ns->input_buffer) && pos < count) {
     267                bp[pos] = (char)buf_pop_front(&ns->input_buffer);
     268                pos++;
    256269        }
    257270        fibril_mutex_unlock(&ns->mutex);
    258271       
    259         return ret;
     272        *nread = pos;
     273        return EOK;
    260274}
    261275
     
    274288/** Write data to the serial port.
    275289 *
    276  * @param fun           The serial port function
     290 * @param srv           Server-side connection data
    277291 * @param buf           The data to be written
    278292 * @param count         The number of bytes to be written
    279  * @return              Zero on success
    280  */
    281 static int ns8250_write(ddf_fun_t *fun, char *buf, size_t count)
    282 {
    283         ns8250_t *ns = fun_ns8250(fun);
     293 * @param nwritten      Place to store number of bytes successfully written
     294 * @return              EOK on success or non-zero error code
     295 */
     296static int ns8250_write(chardev_srv_t *srv, const void *buf, size_t count,
     297    size_t *nwritten)
     298{
     299        ns8250_t *ns = srv_ns8250(srv);
    284300        size_t idx;
     301        uint8_t *bp = (uint8_t *) buf;
    285302       
    286303        for (idx = 0; idx < count; idx++)
    287                 ns8250_putchar(ns, (uint8_t) buf[idx]);
    288        
    289         return count;
    290 }
    291 
    292 static ddf_dev_ops_t ns8250_dev_ops;
     304                ns8250_putchar(ns, bp[idx]);
     305       
     306        *nwritten = count;
     307        return EOK;
     308}
     309
     310static int ns8250_open(chardev_srvs_t *, chardev_srv_t *);
     311static int ns8250_close(chardev_srv_t *);
     312static void ns8250_default_handler(chardev_srv_t *, ipc_callid_t, ipc_call_t *);
    293313
    294314/** The character interface's callbacks. */
    295 static char_dev_ops_t ns8250_char_dev_ops = {
    296         .read = &ns8250_read,
    297         .write = &ns8250_write
     315static chardev_ops_t ns8250_chardev_ops = {
     316        .open = ns8250_open,
     317        .close = ns8250_close,
     318        .read = ns8250_read,
     319        .write = ns8250_write,
     320        .def_handler = ns8250_default_handler
    298321};
     322
     323static void ns8250_char_conn(ipc_callid_t, ipc_call_t *, void *);
    299324
    300325static int ns8250_dev_add(ddf_dev_t *dev);
     
    872897        }
    873898       
    874         /* Set device operations. */
    875         ddf_fun_set_ops(fun, &ns8250_dev_ops);
     899        ddf_fun_set_conn_handler(fun, ns8250_char_conn);
     900       
     901        chardev_srvs_init(&ns->cds);
     902        ns->cds.ops = &ns8250_chardev_ops;
     903        ns->cds.sarg = ns;
     904       
    876905        rc = ddf_fun_bind(fun);
    877906        if (rc != EOK) {
     
    930959 * device.
    931960 *
    932  * @param dev           The device.
    933  */
    934 static int ns8250_open(ddf_fun_t *fun)
    935 {
    936         ns8250_t *ns = fun_ns8250(fun);
     961 * @param srvs          Service structure
     962 * @param srv           Server-side connection structure
     963 */
     964static int ns8250_open(chardev_srvs_t *srvs, chardev_srv_t *srv)
     965{
     966        ns8250_t *ns = srv_ns8250(srv);
    937967        int res;
    938968       
     
    954984 * the device.
    955985 *
    956  * @param dev           The device.
    957  */
    958 static void ns8250_close(ddf_fun_t *fun)
    959 {
    960         ns8250_t *data = fun_ns8250(fun);
     986 * @param srv           Server-side connection structure
     987 */
     988static int ns8250_close(chardev_srv_t *srv)
     989{
     990        ns8250_t *data = srv_ns8250(srv);
    961991       
    962992        fibril_mutex_lock(&data->mutex);
     
    968998       
    969999        fibril_mutex_unlock(&data->mutex);
     1000       
     1001        return EOK;
    9701002}
    9711003
     
    10341066 * Configure the parameters of the serial communication.
    10351067 */
    1036 static void ns8250_default_handler(ddf_fun_t *fun, ipc_callid_t callid,
     1068static void ns8250_default_handler(chardev_srv_t *srv, ipc_callid_t callid,
    10371069    ipc_call_t *call)
    10381070{
     1071        ns8250_t *ns8250 = srv_ns8250(srv);
    10391072        sysarg_t method = IPC_GET_IMETHOD(*call);
    10401073        int ret;
     
    10431076        switch (method) {
    10441077        case SERIAL_GET_COM_PROPS:
    1045                 ns8250_get_props(ddf_fun_get_dev(fun), &baud_rate, &parity, &word_length,
     1078                ns8250_get_props(ns8250->dev, &baud_rate, &parity, &word_length,
    10461079                    &stop_bits);
    10471080                async_answer_4(callid, EOK, baud_rate, parity, word_length,
     
    10541087                word_length = IPC_GET_ARG3(*call);
    10551088                stop_bits = IPC_GET_ARG4(*call);
    1056                 ret = ns8250_set_props(ddf_fun_get_dev(fun), baud_rate, parity, word_length,
     1089                ret = ns8250_set_props(ns8250->dev, baud_rate, parity, word_length,
    10571090                    stop_bits);
    10581091                async_answer_0(callid, ret);
     
    10641097}
    10651098
     1099void ns8250_char_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
     1100{
     1101        ns8250_t *ns8250 = fun_ns8250((ddf_fun_t *)arg);
     1102
     1103        chardev_conn(iid, icall, &ns8250->cds);
     1104}
     1105
    10661106/** Initialize the serial port driver.
    10671107 *
     
    10721112{
    10731113        ddf_log_init(NAME);
    1074        
    1075         ns8250_dev_ops.open = &ns8250_open;
    1076         ns8250_dev_ops.close = &ns8250_close;
    1077        
    1078         ns8250_dev_ops.interfaces[CHAR_DEV_IFACE] = &ns8250_char_dev_ops;
    1079         ns8250_dev_ops.default_handler = &ns8250_default_handler;
    10801114}
    10811115
  • uspace/drv/char/ski-con/ski-con.c

    rf571ca49 rac307b2  
    11/*
    22 * Copyright (c) 2005 Jakub Jermar
    3  * Copyright (c) 2011 Jiri Svoboda
     3 * Copyright (c) 2017 Jiri Svoboda
    44 * All rights reserved.
    55 *
     
    3434#include <ddf/log.h>
    3535#include <errno.h>
    36 #include <ipc/char.h>
     36#include <fibril.h>
     37#include <io/chardev.h>
    3738#include <stdint.h>
    3839#include <stdlib.h>
    39 #include <thread.h>
    4040#include <stdbool.h>
    4141
     
    4646#define POLL_INTERVAL           10000
    4747
    48 static void ski_con_thread_impl(void *arg);
     48static int ski_con_fibril(void *arg);
    4949static int32_t ski_con_getchar(void);
    5050static void ski_con_connection(ipc_callid_t, ipc_call_t *, void *);
    5151
     52static int ski_con_read(chardev_srv_t *, void *, size_t, size_t *);
     53static int ski_con_write(chardev_srv_t *, const void *, size_t, size_t *);
     54
     55static chardev_ops_t ski_con_chardev_ops = {
     56        .read = ski_con_read,
     57        .write = ski_con_write
     58};
     59
    5260/** Add ski console device. */
    5361int ski_con_add(ski_con_t *con)
    5462{
    55         thread_id_t tid;
     63        fid_t fid;
    5664        ddf_fun_t *fun = NULL;
    5765        bool bound = false;
    5866        int rc;
     67
     68        circ_buf_init(&con->cbuf, con->buf, ski_con_buf_size, 1);
     69        fibril_mutex_initialize(&con->buf_lock);
     70        fibril_condvar_initialize(&con->buf_cv);
    5971
    6072        fun = ddf_fun_create(con->dev, fun_exposed, "a");
     
    6779        ddf_fun_set_conn_handler(fun, ski_con_connection);
    6880
     81        chardev_srvs_init(&con->cds);
     82        con->cds.ops = &ski_con_chardev_ops;
     83        con->cds.sarg = con;
     84
    6985        rc = ddf_fun_bind(fun);
    7086        if (rc != EOK) {
     
    7591        bound = true;
    7692
    77         rc = thread_create(ski_con_thread_impl, con, "kbd_poll", &tid);
    78         if (rc != 0) {
    79                 return rc;
    80         }
    81 
     93        fid = fibril_create(ski_con_fibril, con);
     94        if (fid == 0) {
     95                ddf_msg(LVL_ERROR, "Error creating fibril.");
     96                rc = ENOMEM;
     97                goto error;
     98        }
     99
     100        fibril_add_ready(fid);
    82101        return EOK;
    83102error:
     
    102121}
    103122
    104 /** Thread to poll Ski for keypresses. */
    105 static void ski_con_thread_impl(void *arg)
     123/** Poll Ski for keypresses. */
     124static int ski_con_fibril(void *arg)
    106125{
    107126        int32_t c;
    108127        ski_con_t *con = (ski_con_t *) arg;
     128        int rc;
    109129
    110130        while (1) {
     
    114134                                break;
    115135
    116                         if (con->client_sess != NULL) {
    117                                 async_exch_t *exch = async_exchange_begin(con->client_sess);
    118                                 async_msg_1(exch, CHAR_NOTIF_BYTE, c);
    119                                 async_exchange_end(exch);
    120                         }
     136                        fibril_mutex_lock(&con->buf_lock);
     137
     138                        rc = circ_buf_push(&con->cbuf, &c);
     139                        if (rc != EOK)
     140                                ddf_msg(LVL_ERROR, "Buffer overrun");
     141
     142                        fibril_mutex_unlock(&con->buf_lock);
     143                        fibril_condvar_broadcast(&con->buf_cv);
    121144                }
    122145
    123                 thread_usleep(POLL_INTERVAL);
    124         }
     146                fibril_usleep(POLL_INTERVAL);
     147        }
     148
     149        return 0;
    125150}
    126151
     
    157182}
    158183
     184/** Read from Ski console device */
     185static int ski_con_read(chardev_srv_t *srv, void *buf, size_t size,
     186    size_t *nread)
     187{
     188        ski_con_t *con = (ski_con_t *) srv->srvs->sarg;
     189        size_t p;
     190        uint8_t *bp = (uint8_t *) buf;
     191        int rc;
     192
     193        fibril_mutex_lock(&con->buf_lock);
     194
     195        while (circ_buf_nused(&con->cbuf) == 0)
     196                fibril_condvar_wait(&con->buf_cv, &con->buf_lock);
     197
     198        p = 0;
     199        while (p < size) {
     200                rc = circ_buf_pop(&con->cbuf, &bp[p]);
     201                if (rc != EOK)
     202                        break;
     203                ++p;
     204        }
     205
     206        fibril_mutex_unlock(&con->buf_lock);
     207
     208        *nread = p;
     209        return EOK;
     210}
     211
     212/** Write to Ski console device */
     213static int ski_con_write(chardev_srv_t *srv, const void *data, size_t size,
     214    size_t *nwr)
     215{
     216        ski_con_t *con = (ski_con_t *) srv->srvs->sarg;
     217        size_t i;
     218        uint8_t *dp = (uint8_t *) data;
     219
     220        for (i = 0; i < size; i++)
     221                ski_con_putchar(con, dp[i]);
     222
     223        *nwr = size;
     224        return EOK;
     225}
     226
    159227/** Character device connection handler. */
    160228static void ski_con_connection(ipc_callid_t iid, ipc_call_t *icall,
    161229    void *arg)
    162230{
    163         ski_con_t *con;
    164 
    165         /* Answer the IPC_M_CONNECT_ME_TO call. */
    166         async_answer_0(iid, EOK);
    167 
    168         con = (ski_con_t *)ddf_dev_data_get(ddf_fun_get_dev((ddf_fun_t *)arg));
    169 
    170         while (true) {
    171                 ipc_call_t call;
    172                 ipc_callid_t callid = async_get_call(&call);
    173                 sysarg_t method = IPC_GET_IMETHOD(call);
    174 
    175                 if (!method) {
    176                         /* The other side has hung up. */
    177                         async_answer_0(callid, EOK);
    178                         return;
    179                 }
    180 
    181                 async_sess_t *sess =
    182                     async_callback_receive_start(EXCHANGE_SERIALIZE, &call);
    183                 if (sess != NULL) {
    184                         if (con->client_sess == NULL) {
    185                                 con->client_sess = sess;
    186                                 async_answer_0(callid, EOK);
    187                         } else
    188                                 async_answer_0(callid, ELIMIT);
    189                 } else {
    190                         switch (method) {
    191                         case CHAR_WRITE_BYTE:
    192                                 ddf_msg(LVL_DEBUG, "Write %" PRIun " to device\n",
    193                                     IPC_GET_ARG1(call));
    194                                 ski_con_putchar(con, (uint8_t) IPC_GET_ARG1(call));
    195                                 async_answer_0(callid, EOK);
    196                                 break;
    197                         default:
    198                                 async_answer_0(callid, EINVAL);
    199                         }
    200                 }
    201         }
     231        ski_con_t *con = (ski_con_t *) ddf_dev_data_get(
     232            ddf_fun_get_dev((ddf_fun_t *) arg));
     233
     234        chardev_conn(iid, icall, &con->cds);
    202235}
    203236
  • uspace/drv/char/ski-con/ski-con.h

    rf571ca49 rac307b2  
    3636#define SKI_CON_H
    3737
     38#include <adt/circ_buf.h>
    3839#include <async.h>
    3940#include <ddf/driver.h>
     41#include <io/chardev_srv.h>
    4042#include <loc.h>
    4143#include <stdint.h>
     44
     45enum {
     46        ski_con_buf_size = 64
     47};
    4248
    4349/** Ski console */
     
    4551        async_sess_t *client_sess;
    4652        ddf_dev_t *dev;
     53        chardev_srvs_t cds;
     54        circ_buf_t cbuf;
     55        uint8_t buf[ski_con_buf_size];
     56        fibril_mutex_t buf_lock;
     57        fibril_condvar_t buf_cv;
    4758} ski_con_t;
    4859
  • uspace/drv/char/sun4v-con/sun4v-con.c

    rf571ca49 rac307b2  
    11/*
    22 * Copyright (c) 2008 Pavel Rimsky
    3  * Copyright (c) 2011 Jiri Svoboda
     3 * Copyright (c) 2017 Jiri Svoboda
    44 * All rights reserved.
    55 *
     
    3636#include <ddi.h>
    3737#include <errno.h>
    38 #include <ipc/char.h>
     38#include <io/chardev_srv.h>
    3939#include <stdbool.h>
    4040#include <thread.h>
     
    6262static input_buffer_t input_buffer;
    6363
    64 static void sun4v_thread_impl(void *arg);
     64static int sun4v_con_read(chardev_srv_t *, void *, size_t, size_t *);
     65static int sun4v_con_write(chardev_srv_t *, const void *, size_t, size_t *);
     66
     67static chardev_ops_t sun4v_con_chardev_ops = {
     68        .read = sun4v_con_read,
     69        .write = sun4v_con_write
     70};
    6571
    6672static void sun4v_con_putchar(sun4v_con_t *con, uint8_t data)
     
    8692        }
    8793
     94        chardev_srvs_init(&con->cds);
     95        con->cds.ops = &sun4v_con_chardev_ops;
     96        con->cds.sarg = con;
     97
    8898        ddf_fun_set_conn_handler(fun, sun4v_con_connection);
    8999
     
    94104                goto error;
    95105        }
    96 
    97         thread_id_t tid;
    98         rc = thread_create(sun4v_thread_impl, con, "kbd_poll", &tid);
    99         if (rc != EOK)
    100                 goto error;
    101106
    102107        rc = ddf_fun_bind(fun);
     
    131136}
    132137
    133 /**
    134  * Called regularly by the polling thread. Reads codes of all the
    135  * pressed keys from the buffer.
    136  */
    137 static void sun4v_key_pressed(sun4v_con_t *con)
     138/** Read from Sun4v console device */
     139static int sun4v_con_read(chardev_srv_t *srv, void *buf, size_t size,
     140    size_t *nread)
    138141{
     142        size_t p;
     143        uint8_t *bp = (uint8_t *) buf;
    139144        char c;
    140145
    141         while (input_buffer->read_ptr != input_buffer->write_ptr) {
     146        while (input_buffer->read_ptr == input_buffer->write_ptr)
     147                fibril_usleep(POLL_INTERVAL);
     148
     149        p = 0;
     150        while (p < size && input_buffer->read_ptr != input_buffer->write_ptr) {
    142151                c = input_buffer->data[input_buffer->read_ptr];
    143152                input_buffer->read_ptr =
    144153                    ((input_buffer->read_ptr) + 1) % INPUT_BUFFER_SIZE;
    145                 if (con->client_sess != NULL) {
    146                         async_exch_t *exch = async_exchange_begin(con->client_sess);
    147                         async_msg_1(exch, CHAR_NOTIF_BYTE, c);
    148                         async_exchange_end(exch);
    149                 }
    150                 (void) c;
     154                bp[p++] = c;
    151155        }
     156
     157        *nread = p;
     158        return EOK;
    152159}
    153160
    154 /**
    155  * Thread to poll Sun4v console for keypresses.
    156  */
    157 static void sun4v_thread_impl(void *arg)
     161/** Write to Sun4v console device */
     162static int sun4v_con_write(chardev_srv_t *srv, const void *data, size_t size,
     163    size_t *nwr)
    158164{
    159         sun4v_con_t *con = (sun4v_con_t *) arg;
     165        sun4v_con_t *con = (sun4v_con_t *) srv->srvs->sarg;
     166        size_t i;
     167        uint8_t *dp = (uint8_t *) data;
    160168
    161         while (true) {
    162                 sun4v_key_pressed(con);
    163                 thread_usleep(POLL_INTERVAL);
    164         }
     169        for (i = 0; i < size; i++)
     170                sun4v_con_putchar(con, dp[i]);
     171
     172        *nwr = size;
     173        return EOK;
    165174}
    166175
     
    169178    void *arg)
    170179{
    171         sun4v_con_t *con;
     180        sun4v_con_t *con = (sun4v_con_t *) ddf_dev_data_get(
     181            ddf_fun_get_dev((ddf_fun_t *) arg));
    172182
    173         /* Answer the IPC_M_CONNECT_ME_TO call. */
    174         async_answer_0(iid, EOK);
    175 
    176         con = (sun4v_con_t *)ddf_dev_data_get(ddf_fun_get_dev((ddf_fun_t *)arg));
    177 
    178         while (true) {
    179                 ipc_call_t call;
    180                 ipc_callid_t callid = async_get_call(&call);
    181                 sysarg_t method = IPC_GET_IMETHOD(call);
    182 
    183                 if (!method) {
    184                         /* The other side has hung up. */
    185                         async_answer_0(callid, EOK);
    186                         return;
    187                 }
    188 
    189                 async_sess_t *sess =
    190                     async_callback_receive_start(EXCHANGE_SERIALIZE, &call);
    191                 if (sess != NULL) {
    192                         if (con->client_sess == NULL) {
    193                                 con->client_sess = sess;
    194                                 async_answer_0(callid, EOK);
    195                         } else
    196                                 async_answer_0(callid, ELIMIT);
    197                 } else {
    198                         switch (method) {
    199                         case CHAR_WRITE_BYTE:
    200                                 ddf_msg(LVL_DEBUG, "Write %" PRIun " to device\n",
    201                                     IPC_GET_ARG1(call));
    202                                 sun4v_con_putchar(con, (uint8_t) IPC_GET_ARG1(call));
    203                                 async_answer_0(callid, EOK);
    204                                 break;
    205                         default:
    206                                 async_answer_0(callid, EINVAL);
    207                         }
    208                 }
    209         }
     183        chardev_conn(iid, icall, &con->cds);
    210184}
    211185
  • uspace/drv/char/sun4v-con/sun4v-con.h

    rf571ca49 rac307b2  
    3838#include <async.h>
    3939#include <ddf/driver.h>
     40#include <io/chardev_srv.h>
    4041#include <loc.h>
    4142#include <stdint.h>
     
    5051        async_sess_t *client_sess;
    5152        ddf_dev_t *dev;
     53        chardev_srvs_t cds;
    5254        sun4v_con_res_t res;
    5355} sun4v_con_t;
  • uspace/drv/root/virt/devices.def

    rf571ca49 rac307b2  
    2626},
    2727{
    28         .name = "null",
    29         .match_id = "virtual&test1"
    30 },
    31 {
    3228        .name = "test3",
    3329        .match_id = "virtual&test3"
  • uspace/drv/test/test1/Makefile

    rf571ca49 rac307b2  
    3232
    3333SOURCES = \
    34         char.c \
    3534        test1.c
    3635
  • uspace/drv/test/test1/test1.c

    rf571ca49 rac307b2  
    177177        ddf_fun_add_to_category(fun_a, "virtual");
    178178
    179         if (str_cmp(dev_name, "null") == 0) {
    180                 ddf_fun_set_ops(fun_a,  &char_device_ops);
    181                 ddf_fun_add_to_category(fun_a, "virt-null");
    182         } else if (str_cmp(dev_name, "test1") == 0) {
     179        if (str_cmp(dev_name, "test1") == 0) {
    183180                (void) register_fun_verbose(dev,
    184181                    "cloning myself ;-)", "clone",
  • uspace/drv/test/test1/test1.h

    rf571ca49 rac307b2  
    3636#define NAME "test1"
    3737
    38 extern ddf_dev_ops_t char_device_ops;
    39 
    4038#endif
Note: See TracChangeset for help on using the changeset viewer.