Ignore:
File:
1 edited

Legend:

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

    r74017ce rd51838f  
    11/*
    22 * Copyright (c) 2010 Lenka Trochtova
    3  * Copyright (c) 2017 Jiri Svoboda
     3 * Copyright (c) 2011 Jiri Svoboda
    44 * All rights reserved.
    55 *
     
    5353#include <ddf/interrupt.h>
    5454#include <ddf/log.h>
    55 #include <io/chardev_srv.h>
     55#include <ops/char_dev.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;
    157155        /** Parent session */
    158156        async_sess_t *parent_sess;
     
    191189}
    192190
    193 /** Obtain soft-state structure from chardev srv */
    194 static ns8250_t *srv_ns8250(chardev_srv_t *srv)
    195 {
    196         return (ns8250_t *)srv->srvs->sarg;
    197 }
    198 
    199 
    200191/** Find out if there is some incoming data available on the serial port.
    201192 *
     
    243234/** Read data from the serial port device.
    244235 *
    245  * @param srv           Server-side connection data
     236 * @param fun           The serial port function
    246237 * @param buf           The output buffer for read data.
    247238 * @param count         The number of bytes to be read.
    248  * @param nread         Place to store number of bytes actually read
    249  *
    250  * @return              EOK on success or non-zero error code
    251  */
    252 static 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         }
     239 *
     240 * @return              The number of bytes actually read on success, negative
     241 *                      error number otherwise.
     242 */
     243static 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;
    262249       
    263250        fibril_mutex_lock(&ns->mutex);
    264251        while (buf_is_empty(&ns->input_buffer))
    265252                fibril_condvar_wait(&ns->input_buffer_available, &ns->mutex);
    266         while (!buf_is_empty(&ns->input_buffer) && pos < count) {
    267                 bp[pos] = (char)buf_pop_front(&ns->input_buffer);
    268                 pos++;
     253        while (!buf_is_empty(&ns->input_buffer) && (size_t)ret < count) {
     254                buf[ret] = (char)buf_pop_front(&ns->input_buffer);
     255                ret++;
    269256        }
    270257        fibril_mutex_unlock(&ns->mutex);
    271258       
    272         *nread = pos;
    273         return EOK;
     259        return ret;
    274260}
    275261
     
    288274/** Write data to the serial port.
    289275 *
    290  * @param srv           Server-side connection data
     276 * @param fun           The serial port function
    291277 * @param buf           The data to be written
    292278 * @param count         The number of bytes to be written
    293  * @param nwritten      Place to store number of bytes successfully written
    294  * @return              EOK on success or non-zero error code
    295  */
    296 static 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);
     279 * @return              Zero on success
     280 */
     281static int ns8250_write(ddf_fun_t *fun, char *buf, size_t count)
     282{
     283        ns8250_t *ns = fun_ns8250(fun);
    300284        size_t idx;
    301         uint8_t *bp = (uint8_t *) buf;
    302285       
    303286        for (idx = 0; idx < count; idx++)
    304                 ns8250_putchar(ns, bp[idx]);
    305        
    306         *nwritten = count;
    307         return EOK;
    308 }
    309 
    310 static int ns8250_open(chardev_srvs_t *, chardev_srv_t *);
    311 static int ns8250_close(chardev_srv_t *);
    312 static void ns8250_default_handler(chardev_srv_t *, ipc_callid_t, ipc_call_t *);
     287                ns8250_putchar(ns, (uint8_t) buf[idx]);
     288       
     289        return count;
     290}
     291
     292static ddf_dev_ops_t ns8250_dev_ops;
    313293
    314294/** The character interface's callbacks. */
    315 static 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
     295static char_dev_ops_t ns8250_char_dev_ops = {
     296        .read = &ns8250_read,
     297        .write = &ns8250_write
    321298};
    322 
    323 static void ns8250_char_conn(ipc_callid_t, ipc_call_t *, void *);
    324299
    325300static int ns8250_dev_add(ddf_dev_t *dev);
     
    897872        }
    898873       
    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        
     874        /* Set device operations. */
     875        ddf_fun_set_ops(fun, &ns8250_dev_ops);
    905876        rc = ddf_fun_bind(fun);
    906877        if (rc != EOK) {
     
    959930 * device.
    960931 *
    961  * @param srvs          Service structure
    962  * @param srv           Server-side connection structure
    963  */
    964 static int ns8250_open(chardev_srvs_t *srvs, chardev_srv_t *srv)
    965 {
    966         ns8250_t *ns = srv_ns8250(srv);
     932 * @param dev           The device.
     933 */
     934static int ns8250_open(ddf_fun_t *fun)
     935{
     936        ns8250_t *ns = fun_ns8250(fun);
    967937        int res;
    968938       
     
    984954 * the device.
    985955 *
    986  * @param srv           Server-side connection structure
    987  */
    988 static int ns8250_close(chardev_srv_t *srv)
    989 {
    990         ns8250_t *data = srv_ns8250(srv);
     956 * @param dev           The device.
     957 */
     958static void ns8250_close(ddf_fun_t *fun)
     959{
     960        ns8250_t *data = fun_ns8250(fun);
    991961       
    992962        fibril_mutex_lock(&data->mutex);
     
    998968       
    999969        fibril_mutex_unlock(&data->mutex);
    1000        
    1001         return EOK;
    1002970}
    1003971
     
    10661034 * Configure the parameters of the serial communication.
    10671035 */
    1068 static void ns8250_default_handler(chardev_srv_t *srv, ipc_callid_t callid,
     1036static void ns8250_default_handler(ddf_fun_t *fun, ipc_callid_t callid,
    10691037    ipc_call_t *call)
    10701038{
    1071         ns8250_t *ns8250 = srv_ns8250(srv);
    10721039        sysarg_t method = IPC_GET_IMETHOD(*call);
    10731040        int ret;
     
    10761043        switch (method) {
    10771044        case SERIAL_GET_COM_PROPS:
    1078                 ns8250_get_props(ns8250->dev, &baud_rate, &parity, &word_length,
     1045                ns8250_get_props(ddf_fun_get_dev(fun), &baud_rate, &parity, &word_length,
    10791046                    &stop_bits);
    10801047                async_answer_4(callid, EOK, baud_rate, parity, word_length,
     
    10871054                word_length = IPC_GET_ARG3(*call);
    10881055                stop_bits = IPC_GET_ARG4(*call);
    1089                 ret = ns8250_set_props(ns8250->dev, baud_rate, parity, word_length,
     1056                ret = ns8250_set_props(ddf_fun_get_dev(fun), baud_rate, parity, word_length,
    10901057                    stop_bits);
    10911058                async_answer_0(callid, ret);
     
    10971064}
    10981065
    1099 void 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 
    11061066/** Initialize the serial port driver.
    11071067 *
     
    11121072{
    11131073        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;
    11141080}
    11151081
Note: See TracChangeset for help on using the changeset viewer.