source: mainline/uspace/drv/char/ns8250/ns8250.c@ ca4c5596

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ca4c5596 was 4f87a85a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Check return code from ddf_add_fun_to_category

  • Property mode set to 100644
File size: 28.6 KB
RevLine 
[68414f4a]1/*
[5fe1c32]2 * Copyright (c) 2010 Lenka Trochtova
[74017ce]3 * Copyright (c) 2017 Jiri Svoboda
[5fe1c32]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
30/**
[4122410]31 * @addtogroup ns8250
[5fe1c32]32 * @{
33 */
34
35/** @file
36 */
37
38#include <assert.h>
39#include <stdio.h>
40#include <errno.h>
[dd8ab1c]41#include <str_error.h>
[3e6a98c5]42#include <stdbool.h>
[5fe1c32]43#include <fibril_synch.h>
44#include <stdlib.h>
[c47e1a8]45#include <str.h>
[5fe1c32]46#include <ctype.h>
47#include <macros.h>
[38d150e]48#include <stdlib.h>
[5fe1c32]49#include <dirent.h>
[1f8657b]50#include <ddi.h>
[5fe1c32]51
[af6b5157]52#include <ddf/driver.h>
53#include <ddf/interrupt.h>
[fc51296]54#include <ddf/log.h>
[74017ce]55#include <io/chardev_srv.h>
[5fe1c32]56
57#include <device/hw_res.h>
[f4ef3c2]58#include <ipc/serial_ctl.h>
[5fe1c32]59
[1f8657b]60#include "cyclic_buffer.h"
61
[04c7003f]62#define NAME "ns8250"
[5fe1c32]63
[1f8657b]64#define REG_COUNT 7
[f4ef3c2]65#define MAX_BAUD_RATE 115200
[cf8cc36]66#define DLAB_MASK (1 << 7)
67
[c7235d40]68/** Interrupt Enable Register definition. */
69#define NS8250_IER_RXREADY (1 << 0)
70#define NS8250_IER_THRE (1 << 1)
71#define NS8250_IER_RXSTATUS (1 << 2)
72#define NS8250_IER_MODEM_STATUS (1 << 3)
73
74/** Interrupt ID Register definition. */
75#define NS8250_IID_ACTIVE (1 << 0)
[e0cd9042]76#define NS8250_IID_CAUSE_MASK 0x0e
77#define NS8250_IID_CAUSE_RXSTATUS 0x06
[c7235d40]78
79/** FIFO Control Register definition. */
80#define NS8250_FCR_FIFOENABLE (1 << 0)
81#define NS8250_FCR_RXFIFORESET (1 << 1)
82#define NS8250_FCR_TXFIFORESET (1 << 2)
83#define NS8250_FCR_DMAMODE (1 << 3)
84#define NS8250_FCR_RXTRIGGERLOW (1 << 6)
85#define NS8250_FCR_RXTRIGGERHI (1 << 7)
86
87/** Line Control Register definition. */
88#define NS8250_LCR_STOPBITS (1 << 2)
89#define NS8250_LCR_PARITY (1 << 3)
90#define NS8250_LCR_SENDBREAK (1 << 6)
91#define NS8250_LCR_DLAB (1 << 7)
92
93/** Modem Control Register definition. */
94#define NS8250_MCR_DTR (1 << 0)
95#define NS8250_MCR_RTS (1 << 1)
96#define NS8250_MCR_OUT1 (1 << 2)
97#define NS8250_MCR_OUT2 (1 << 3)
98#define NS8250_MCR_LOOPBACK (1 << 4)
99#define NS8250_MCR_ALL (0x1f)
100
101/** Line Status Register definition. */
102#define NS8250_LSR_RXREADY (1 << 0)
103#define NS8250_LSR_OE (1 << 1)
104#define NS8250_LSR_PE (1 << 2)
105#define NS8250_LSR_FE (1 << 3)
106#define NS8250_LSR_BREAK (1 << 4)
107#define NS8250_LSR_THRE (1 << 5)
108#define NS8250_LSR_TSE (1 << 6)
109
110/** Modem Status Register definition. */
111#define NS8250_MSR_DELTACTS (1 << 0)
112#define NS8250_MSR_DELTADSR (1 << 1)
113#define NS8250_MSR_RITRAILING (1 << 2)
114#define NS8250_MSR_DELTADCD (1 << 3)
115#define NS8250_MSR_CTS (1 << 4)
116#define NS8250_MSR_DSR (1 << 5)
117#define NS8250_MSR_RI (1 << 6)
118#define NS8250_MSR_DCD (1 << 7)
119#define NS8250_MSR_SIGNALS (NS8250_MSR_CTS | NS8250_MSR_DSR \
120 | NS8250_MSR_RI | NS8250_MSR_DCD)
121
[49698fa]122/** The number of bits of one data unit send by the serial port. */
[cf8cc36]123typedef enum {
124 WORD_LENGTH_5,
125 WORD_LENGTH_6,
126 WORD_LENGTH_7,
[49698fa]127 WORD_LENGTH_8
[cf8cc36]128} word_length_t;
129
130/** The number of stop bits used by the serial port. */
131typedef enum {
132 /** Use one stop bit. */
133 ONE_STOP_BIT,
134 /** 1.5 stop bits for word length 5, 2 stop bits otherwise. */
[49698fa]135 TWO_STOP_BITS
[cf8cc36]136} stop_bit_t;
137
[c7235d40]138/** 8250 UART registers layout. */
139typedef struct {
140 ioport8_t data; /**< Data register. */
141 ioport8_t ier; /**< Interrupt Enable Reg. */
142 ioport8_t iid; /**< Interrupt ID Reg. */
143 ioport8_t lcr; /**< Line Control Reg. */
144 ioport8_t mcr; /**< Modem Control Reg. */
145 ioport8_t lsr; /**< Line Status Reg. */
146 ioport8_t msr; /**< Modem Status Reg. */
147} ns8250_regs_t;
148
[49698fa]149/** The driver data for the serial port devices. */
[68414f4a]150typedef struct ns8250 {
151 /** DDF device node */
[83a2f43]152 ddf_dev_t *dev;
[68414f4a]153 /** DDF function node */
[83a2f43]154 ddf_fun_t *fun;
[74017ce]155 /** Character device service */
156 chardev_srvs_t cds;
[d51838f]157 /** Parent session */
158 async_sess_t *parent_sess;
[c7235d40]159 /** I/O registers **/
160 ns8250_regs_t *regs;
[4510e06]161 /** Are there any clients connected to the device? */
162 unsigned client_connections;
[cf8cc36]163 /** The irq assigned to this device. */
[1f8657b]164 int irq;
[3f74275]165 /** IRQ capability handle */
[eadaeae8]166 cap_irq_handle_t irq_handle;
[cf8cc36]167 /** The base i/o address of the devices registers. */
[340513c]168 uintptr_t io_addr;
[cf8cc36]169 /** The i/o port used to access the serial ports registers. */
[1f8657b]170 ioport8_t *port;
[381ff2f]171 /** The buffer for incoming data. */
[1f8657b]172 cyclic_buffer_t input_buffer;
[49698fa]173 /** The fibril mutex for synchronizing the access to the device. */
174 fibril_mutex_t mutex;
[91ecaa10]175 /** Indicates that some data has become available */
176 fibril_condvar_t input_buffer_available;
[5b68e0c]177 /** True if device is removed. */
178 bool removed;
[68414f4a]179} ns8250_t;
[5fe1c32]180
[56fd7cf]181/** Obtain soft-state structure from device node */
182static ns8250_t *dev_ns8250(ddf_dev_t *dev)
183{
184 return ddf_dev_data_get(dev);
185}
186
187/** Obtain soft-state structure from function node */
188static ns8250_t *fun_ns8250(ddf_fun_t *fun)
189{
190 return dev_ns8250(ddf_fun_get_dev(fun));
191}
192
[74017ce]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
[381ff2f]199/** Find out if there is some incoming data available on the serial port.
[49698fa]200 *
201 * @param port The base address of the serial port device's ports.
202 * @return True if there are data waiting to be read, false
203 * otherwise.
[cf8cc36]204 */
[c7235d40]205static bool ns8250_received(ns8250_regs_t *regs)
[ca97cad]206{
[c7235d40]207 return (pio_read_8(&regs->lsr) & NS8250_LSR_RXREADY) != 0;
[ca97cad]208}
209
[cf8cc36]210/** Read one byte from the serial port.
[49698fa]211 *
212 * @param port The base address of the serial port device's ports.
213 * @return The data read.
[cf8cc36]214 */
[c7235d40]215static uint8_t ns8250_read_8(ns8250_regs_t *regs)
[ca97cad]216{
[c7235d40]217 return pio_read_8(&regs->data);
[ca97cad]218}
219
[cf8cc36]220/** Find out wheter it is possible to send data.
[49698fa]221 *
222 * @param port The base address of the serial port device's ports.
[cf8cc36]223 */
[c7235d40]224static bool is_transmit_empty(ns8250_regs_t *regs)
[ca97cad]225{
[c7235d40]226 return (pio_read_8(&regs->lsr) & NS8250_LSR_THRE) != 0;
[ca97cad]227}
228
[cf8cc36]229/** Write one character on the serial port.
[49698fa]230 *
231 * @param port The base address of the serial port device's ports.
232 * @param c The character to be written to the serial port device.
[cf8cc36]233 */
[c7235d40]234static void ns8250_write_8(ns8250_regs_t *regs, uint8_t c)
[ca97cad]235{
[c7235d40]236 while (!is_transmit_empty(regs))
[ca97cad]237 ;
[a35b458]238
[c7235d40]239 pio_write_8(&regs->data, c);
[ca97cad]240}
241
[cf8cc36]242/** Read data from the serial port device.
[49698fa]243 *
[74017ce]244 * @param srv Server-side connection data
[956d4281]245 * @param buf The output buffer for read data.
[49698fa]246 * @param count The number of bytes to be read.
[74017ce]247 * @param nread Place to store number of bytes actually read
[49698fa]248 *
[74017ce]249 * @return EOK on success or non-zero error code
[cf8cc36]250 */
[b7fd2a0]251static errno_t ns8250_read(chardev_srv_t *srv, void *buf, size_t count, size_t *nread)
[f658458]252{
[74017ce]253 ns8250_t *ns = srv_ns8250(srv);
254 char *bp = (char *) buf;
255 size_t pos = 0;
[a35b458]256
[74017ce]257 if (count == 0) {
258 *nread = 0;
259 return EOK;
260 }
[a35b458]261
[68414f4a]262 fibril_mutex_lock(&ns->mutex);
[91ecaa10]263 while (buf_is_empty(&ns->input_buffer))
264 fibril_condvar_wait(&ns->input_buffer_available, &ns->mutex);
[74017ce]265 while (!buf_is_empty(&ns->input_buffer) && pos < count) {
266 bp[pos] = (char)buf_pop_front(&ns->input_buffer);
267 pos++;
[bb864a0]268 }
[68414f4a]269 fibril_mutex_unlock(&ns->mutex);
[a35b458]270
[74017ce]271 *nread = pos;
272 return EOK;
[f658458]273}
274
[cf8cc36]275/** Write a character to the serial port.
[49698fa]276 *
[68414f4a]277 * @param ns Serial port device
278 * @param c The character to be written
[cf8cc36]279 */
[68414f4a]280static inline void ns8250_putchar(ns8250_t *ns, uint8_t c)
[49698fa]281{
[68414f4a]282 fibril_mutex_lock(&ns->mutex);
[c7235d40]283 ns8250_write_8(ns->regs, c);
[68414f4a]284 fibril_mutex_unlock(&ns->mutex);
[ca97cad]285}
286
[cf8cc36]287/** Write data to the serial port.
[49698fa]288 *
[74017ce]289 * @param srv Server-side connection data
[68414f4a]290 * @param buf The data to be written
291 * @param count The number of bytes to be written
[74017ce]292 * @param nwritten Place to store number of bytes successfully written
293 * @return EOK on success or non-zero error code
[cf8cc36]294 */
[b7fd2a0]295static errno_t ns8250_write(chardev_srv_t *srv, const void *buf, size_t count,
[74017ce]296 size_t *nwritten)
[f658458]297{
[74017ce]298 ns8250_t *ns = srv_ns8250(srv);
[ca97cad]299 size_t idx;
[74017ce]300 uint8_t *bp = (uint8_t *) buf;
[a35b458]301
[49698fa]302 for (idx = 0; idx < count; idx++)
[74017ce]303 ns8250_putchar(ns, bp[idx]);
[a35b458]304
[74017ce]305 *nwritten = count;
306 return EOK;
[f658458]307}
308
[b7fd2a0]309static errno_t ns8250_open(chardev_srvs_t *, chardev_srv_t *);
310static errno_t ns8250_close(chardev_srv_t *);
[984a9ba]311static void ns8250_default_handler(chardev_srv_t *, ipc_call_t *);
[5fe1c32]312
[49698fa]313/** The character interface's callbacks. */
[74017ce]314static chardev_ops_t ns8250_chardev_ops = {
315 .open = ns8250_open,
316 .close = ns8250_close,
317 .read = ns8250_read,
318 .write = ns8250_write,
319 .def_handler = ns8250_default_handler
[f658458]320};
321
[984a9ba]322static void ns8250_char_conn(ipc_call_t *, void *);
[74017ce]323
[b7fd2a0]324static errno_t ns8250_dev_add(ddf_dev_t *dev);
325static errno_t ns8250_dev_remove(ddf_dev_t *dev);
[5fe1c32]326
[49698fa]327/** The serial port device driver's standard operations. */
[04c7003f]328static driver_ops_t ns8250_ops = {
[0c0f823b]329 .dev_add = &ns8250_dev_add,
[5b68e0c]330 .dev_remove = &ns8250_dev_remove
[5fe1c32]331};
332
[49698fa]333/** The serial port device driver structure. */
[04c7003f]334static driver_t ns8250_driver = {
[5fe1c32]335 .name = NAME,
[04c7003f]336 .driver_ops = &ns8250_ops
[5fe1c32]337};
338
[68414f4a]339/** Clean up the serial port soft-state
[49698fa]340 *
[68414f4a]341 * @param ns Serial port device
[cf8cc36]342 */
[68414f4a]343static void ns8250_dev_cleanup(ns8250_t *ns)
[1f8657b]344{
345}
346
[cf8cc36]347/** Enable the i/o ports of the device.
[49698fa]348 *
[68414f4a]349 * @param ns Serial port device
350 * @return True on success, false otherwise
[cf8cc36]351 */
[68414f4a]352static bool ns8250_pio_enable(ns8250_t *ns)
[1f8657b]353{
[56fd7cf]354 ddf_msg(LVL_DEBUG, "ns8250_pio_enable %s", ddf_dev_get_name(ns->dev));
[a35b458]355
[49698fa]356 /* Gain control over port's registers. */
[340513c]357 if (pio_enable((void *) ns->io_addr, REG_COUNT,
[68414f4a]358 (void **) &ns->port)) {
[340513c]359 ddf_msg(LVL_ERROR, "Cannot map the port %#" PRIxn
[56fd7cf]360 " for device %s.", ns->io_addr, ddf_dev_get_name(ns->dev));
[1f8657b]361 return false;
362 }
[c7235d40]363
364 ns->regs = (ns8250_regs_t *)ns->port;
[a35b458]365
[1f8657b]366 return true;
367}
368
[cf8cc36]369/** Probe the serial port device for its presence.
[49698fa]370 *
[68414f4a]371 * @param ns Serial port device
372 * @return True if the device is present, false otherwise
[cf8cc36]373 */
[68414f4a]374static bool ns8250_dev_probe(ns8250_t *ns)
[1f8657b]375{
[56fd7cf]376 ddf_msg(LVL_DEBUG, "ns8250_dev_probe %s", ddf_dev_get_name(ns->dev));
[a35b458]377
[1f8657b]378 bool res = true;
379 uint8_t olddata;
[a35b458]380
[c7235d40]381 olddata = pio_read_8(&ns->regs->mcr);
[a35b458]382
[c7235d40]383 pio_write_8(&ns->regs->mcr, NS8250_MCR_LOOPBACK);
384 if (pio_read_8(&ns->regs->msr) & NS8250_MSR_SIGNALS)
[1f8657b]385 res = false;
[a35b458]386
[c7235d40]387 pio_write_8(&ns->regs->mcr, NS8250_MCR_ALL);
[3bacee1]388 if ((pio_read_8(&ns->regs->msr) & NS8250_MSR_SIGNALS) !=
389 NS8250_MSR_SIGNALS)
[1f8657b]390 res = false;
[a35b458]391
[c7235d40]392 pio_write_8(&ns->regs->mcr, olddata);
[a35b458]393
[fc51296]394 if (!res) {
[ebcb05a]395 ddf_msg(LVL_DEBUG, "Device %s is not present.",
[56fd7cf]396 ddf_dev_get_name(ns->dev));
[fc51296]397 }
[a35b458]398
[49698fa]399 return res;
[1f8657b]400}
401
[cf8cc36]402/** Initialize serial port device.
[49698fa]403 *
[68414f4a]404 * @param ns Serial port device
[cde999a]405 * @return Zero on success, error number otherwise
[cf8cc36]406 */
[b7fd2a0]407static errno_t ns8250_dev_initialize(ns8250_t *ns)
[1f8657b]408{
[b7fd2a0]409 errno_t ret = EOK;
[a35b458]410
[56fd7cf]411 ddf_msg(LVL_DEBUG, "ns8250_dev_initialize %s", ddf_dev_get_name(ns->dev));
[a35b458]412
[1f8657b]413 hw_resource_list_t hw_resources;
414 memset(&hw_resources, 0, sizeof(hw_resource_list_t));
[a35b458]415
[49698fa]416 /* Get hw resources. */
[d51838f]417 ret = hw_res_get_resource_list(ns->parent_sess, &hw_resources);
[be942bc]418 if (ret != EOK) {
[fc51296]419 ddf_msg(LVL_ERROR, "Failed to get HW resources for device "
[56fd7cf]420 "%s.", ddf_dev_get_name(ns->dev));
[1f8657b]421 goto failed;
[49698fa]422 }
[a35b458]423
[1f8657b]424 size_t i;
425 hw_resource_t *res;
426 bool irq = false;
427 bool ioport = false;
[a35b458]428
[1f8657b]429 for (i = 0; i < hw_resources.count; i++) {
430 res = &hw_resources.resources[i];
431 switch (res->type) {
432 case INTERRUPT:
[68414f4a]433 ns->irq = res->res.interrupt.irq;
[1f8657b]434 irq = true;
[3a67d63]435 ddf_msg(LVL_NOTE, "Device %s was assigned irq = 0x%x.",
[56fd7cf]436 ddf_dev_get_name(ns->dev), ns->irq);
[1f8657b]437 break;
[a35b458]438
[1f8657b]439 case IO_RANGE:
[68414f4a]440 ns->io_addr = res->res.io_range.address;
[1f8657b]441 if (res->res.io_range.size < REG_COUNT) {
[fc51296]442 ddf_msg(LVL_ERROR, "I/O range assigned to "
[56fd7cf]443 "device %s is too small.", ddf_dev_get_name(ns->dev));
[be942bc]444 ret = ELIMIT;
[1f8657b]445 goto failed;
446 }
447 ioport = true;
[3a67d63]448 ddf_msg(LVL_NOTE, "Device %s was assigned I/O address = "
[340513c]449 "0x%#" PRIxn ".", ddf_dev_get_name(ns->dev), ns->io_addr);
[3bacee1]450 break;
[a35b458]451
[dafe675]452 default:
453 break;
[1f8657b]454 }
455 }
[a35b458]456
[1f8657b]457 if (!irq || !ioport) {
[ebcb05a]458 ddf_msg(LVL_ERROR, "Missing HW resource(s) for device %s.",
[56fd7cf]459 ddf_dev_get_name(ns->dev));
[be942bc]460 ret = ENOENT;
[1f8657b]461 goto failed;
[49698fa]462 }
[a35b458]463
[f724e82]464 hw_res_clean_resource_list(&hw_resources);
[df747b9c]465 return ret;
[a35b458]466
[1f8657b]467failed:
[68414f4a]468 ns8250_dev_cleanup(ns);
[f724e82]469 hw_res_clean_resource_list(&hw_resources);
[49698fa]470 return ret;
[1f8657b]471}
472
[cf8cc36]473/** Enable interrupts on the serial port device.
[49698fa]474 *
[68414f4a]475 * Interrupt when data is received
[49698fa]476 *
477 * @param port The base address of the serial port device's ports.
[cf8cc36]478 */
[c7235d40]479static inline void ns8250_port_interrupts_enable(ns8250_regs_t *regs)
[bab6388]480{
[c7235d40]481 /* Interrupt when data received. */
[e0cd9042]482 pio_write_8(&regs->ier, NS8250_IER_RXREADY | NS8250_IER_RXSTATUS);
[3bacee1]483 pio_write_8(&regs->mcr, NS8250_MCR_DTR | NS8250_MCR_RTS |
484 NS8250_MCR_OUT2);
[f4ef3c2]485}
486
[cf8cc36]487/** Disable interrupts on the serial port device.
[49698fa]488 *
[68414f4a]489 * @param port The base address of the serial port device's ports
[cf8cc36]490 */
[c7235d40]491static inline void ns8250_port_interrupts_disable(ns8250_regs_t *regs)
[f4ef3c2]492{
[c7235d40]493 pio_write_8(&regs->ier, 0x0); /* Disable all interrupts. */
[f4ef3c2]494}
495
[cf8cc36]496/** Enable interrupts for the serial port device.
[49698fa]497 *
[68414f4a]498 * @param ns Serial port device
[cde999a]499 * @return Zero on success, error number otherwise
[cf8cc36]500 */
[b7fd2a0]501static errno_t ns8250_interrupt_enable(ns8250_t *ns)
[cfe7716]502{
[ebc9c2c]503 /* Enable interrupt using IRC service. */
[b7fd2a0]504 errno_t rc = hw_res_enable_interrupt(ns->parent_sess, ns->irq);
[ebc9c2c]505 if (rc != EOK)
[128c78b]506 return EIO;
[a35b458]507
[e0cd9042]508 /* Read LSR to clear possible previous LSR interrupt */
509 pio_read_8(&ns->regs->lsr);
[a35b458]510
[49698fa]511 /* Enable interrupt on the serial port. */
[c7235d40]512 ns8250_port_interrupts_enable(ns->regs);
[a35b458]513
[cfe7716]514 return EOK;
515}
516
[cf8cc36]517/** Set Divisor Latch Access Bit.
[49698fa]518 *
519 * When the Divisor Latch Access Bit is set, it is possible to set baud rate of
520 * the serial port device.
521 *
522 * @param port The base address of the serial port device's ports.
[cf8cc36]523 */
[c7235d40]524static inline void enable_dlab(ns8250_regs_t *regs)
[cf8cc36]525{
[c7235d40]526 uint8_t val = pio_read_8(&regs->lcr);
527 pio_write_8(&regs->lcr, val | NS8250_LCR_DLAB);
[cf8cc36]528}
529
530/** Clear Divisor Latch Access Bit.
[49698fa]531 *
532 * @param port The base address of the serial port device's ports.
[cf8cc36]533 */
[c7235d40]534static inline void clear_dlab(ns8250_regs_t *regs)
[cf8cc36]535{
[c7235d40]536 uint8_t val = pio_read_8(&regs->lcr);
537 pio_write_8(&regs->lcr, val & (~NS8250_LCR_DLAB));
[cf8cc36]538}
539
540/** Set baud rate of the serial communication on the serial device.
[49698fa]541 *
542 * @param port The base address of the serial port device's ports.
543 * @param baud_rate The baud rate to be used by the device.
[cde999a]544 * @return Zero on success, error number otherwise (EINVAL
[49698fa]545 * if the specified baud_rate is not valid).
[cf8cc36]546 */
[b7fd2a0]547static errno_t ns8250_port_set_baud_rate(ns8250_regs_t *regs, unsigned int baud_rate)
[f4ef3c2]548{
549 uint16_t divisor;
550 uint8_t div_low, div_high;
[a35b458]551
[33dbbd2]552 if (baud_rate < 50 || MAX_BAUD_RATE % baud_rate != 0) {
[ebcb05a]553 ddf_msg(LVL_ERROR, "Invalid baud rate %d requested.",
[fc51296]554 baud_rate);
[49698fa]555 return EINVAL;
[f4ef3c2]556 }
[a35b458]557
[f4ef3c2]558 divisor = MAX_BAUD_RATE / baud_rate;
559 div_low = (uint8_t)divisor;
[49698fa]560 div_high = (uint8_t)(divisor >> 8);
[a35b458]561
[49698fa]562 /* Enable DLAB to be able to access baud rate divisor. */
[c7235d40]563 enable_dlab(regs);
[a35b458]564
[49698fa]565 /* Set divisor low byte. */
[c7235d40]566 pio_write_8(&regs->data, div_low);
[49698fa]567 /* Set divisor high byte. */
[c7235d40]568 pio_write_8(&regs->ier, div_high);
[a35b458]569
[c7235d40]570 clear_dlab(regs);
[a35b458]571
[49698fa]572 return EOK;
[f4ef3c2]573}
574
[cf8cc36]575/** Get baud rate used by the serial port device.
[49698fa]576 *
577 * @param port The base address of the serial port device's ports.
578 * @param baud_rate The ouput parameter to which the baud rate is stored.
[cf8cc36]579 */
[c7235d40]580static unsigned int ns8250_port_get_baud_rate(ns8250_regs_t *regs)
[f4ef3c2]581{
[cf8cc36]582 uint16_t divisor;
583 uint8_t div_low, div_high;
[a35b458]584
[49698fa]585 /* Enable DLAB to be able to access baud rate divisor. */
[c7235d40]586 enable_dlab(regs);
[a35b458]587
[49698fa]588 /* Get divisor low byte. */
[c7235d40]589 div_low = pio_read_8(&regs->data);
[49698fa]590 /* Get divisor high byte. */
[c7235d40]591 div_high = pio_read_8(&regs->ier);
[a35b458]592
[c7235d40]593 clear_dlab(regs);
[a35b458]594
[cf8cc36]595 divisor = (div_high << 8) | div_low;
596 return MAX_BAUD_RATE / divisor;
597}
598
[49698fa]599/** Get the parameters of the serial communication set on the serial port
600 * device.
601 *
602 * @param parity The parity used.
603 * @param word_length The length of one data unit in bits.
604 * @param stop_bits The number of stop bits used (one or two).
[cf8cc36]605 */
[c7235d40]606static void ns8250_port_get_com_props(ns8250_regs_t *regs, unsigned int *parity,
[49698fa]607 unsigned int *word_length, unsigned int *stop_bits)
[cf8cc36]608{
609 uint8_t val;
[a35b458]610
[c7235d40]611 val = pio_read_8(&regs->lcr);
612 *parity = ((val >> NS8250_LCR_PARITY) & 7);
[a35b458]613
[56fd7cf]614 /* Silence warnings */
615 *word_length = 0;
616
[cf8cc36]617 switch (val & 3) {
[49698fa]618 case WORD_LENGTH_5:
619 *word_length = 5;
620 break;
621 case WORD_LENGTH_6:
622 *word_length = 6;
623 break;
624 case WORD_LENGTH_7:
625 *word_length = 7;
626 break;
627 case WORD_LENGTH_8:
628 *word_length = 8;
629 break;
[cf8cc36]630 }
[a35b458]631
[c7235d40]632 if ((val >> NS8250_LCR_STOPBITS) & 1)
[cf8cc36]633 *stop_bits = 2;
[49698fa]634 else
[cf8cc36]635 *stop_bits = 1;
[f4ef3c2]636}
637
[cf8cc36]638/** Set the parameters of the serial communication on the serial port device.
[49698fa]639 *
640 * @param parity The parity to be used.
641 * @param word_length The length of one data unit in bits.
642 * @param stop_bits The number of stop bits used (one or two).
643 * @return Zero on success, EINVAL if some of the specified values
644 * is invalid.
[cf8cc36]645 */
[b7fd2a0]646static errno_t ns8250_port_set_com_props(ns8250_regs_t *regs, unsigned int parity,
[49698fa]647 unsigned int word_length, unsigned int stop_bits)
[cf8cc36]648{
649 uint8_t val;
[a35b458]650
[cf8cc36]651 switch (word_length) {
[49698fa]652 case 5:
653 val = WORD_LENGTH_5;
654 break;
655 case 6:
656 val = WORD_LENGTH_6;
657 break;
658 case 7:
659 val = WORD_LENGTH_7;
660 break;
661 case 8:
662 val = WORD_LENGTH_8;
663 break;
664 default:
665 return EINVAL;
[cf8cc36]666 }
[a35b458]667
[cf8cc36]668 switch (stop_bits) {
[49698fa]669 case 1:
[c7235d40]670 val |= ONE_STOP_BIT << NS8250_LCR_STOPBITS;
[49698fa]671 break;
672 case 2:
[c7235d40]673 val |= TWO_STOP_BITS << NS8250_LCR_STOPBITS;
[49698fa]674 break;
675 default:
676 return EINVAL;
[cf8cc36]677 }
[a35b458]678
[cf8cc36]679 switch (parity) {
[49698fa]680 case SERIAL_NO_PARITY:
681 case SERIAL_ODD_PARITY:
682 case SERIAL_EVEN_PARITY:
683 case SERIAL_MARK_PARITY:
684 case SERIAL_SPACE_PARITY:
[c7235d40]685 val |= parity << NS8250_LCR_PARITY;
[49698fa]686 break;
687 default:
688 return EINVAL;
[cf8cc36]689 }
[a35b458]690
[c7235d40]691 pio_write_8(&regs->lcr, val);
[a35b458]692
[cf8cc36]693 return EOK;
694}
695
696/** Initialize the serial port device.
[49698fa]697 *
[cf8cc36]698 * Set the default parameters of the serial communication.
[49698fa]699 *
[68414f4a]700 * @param ns Serial port device
[cf8cc36]701 */
[68414f4a]702static void ns8250_initialize_port(ns8250_t *ns)
[cfe7716]703{
[49698fa]704 /* Disable interrupts. */
[c7235d40]705 ns8250_port_interrupts_disable(ns->regs);
[49698fa]706 /* Set baud rate. */
[c7235d40]707 ns8250_port_set_baud_rate(ns->regs, 38400);
[49698fa]708 /* 8 bits, no parity, two stop bits. */
[c7235d40]709 ns8250_port_set_com_props(ns->regs, SERIAL_NO_PARITY, 8, 2);
[188c76c]710 /*
711 * Enable FIFO, clear them, with 4-byte threshold for greater
712 * reliability.
713 */
[3bacee1]714 pio_write_8(&ns->regs->iid, NS8250_FCR_FIFOENABLE |
715 NS8250_FCR_RXFIFORESET | NS8250_FCR_TXFIFORESET |
716 NS8250_FCR_RXTRIGGERLOW);
[49698fa]717 /*
718 * RTS/DSR set (Request to Send and Data Terminal Ready lines enabled),
719 * Aux Output2 set - needed for interrupts.
720 */
[3bacee1]721 pio_write_8(&ns->regs->mcr, NS8250_MCR_DTR | NS8250_MCR_RTS |
722 NS8250_MCR_OUT2);
[49698fa]723}
724
[5b68e0c]725/** Deinitialize the serial port device.
726 *
727 * @param ns Serial port device
728 */
729static void ns8250_port_cleanup(ns8250_t *ns)
730{
731 /* Disable FIFO */
[c7235d40]732 pio_write_8(&ns->regs->iid, 0x00);
[5b68e0c]733 /* Disable DTR, RTS, OUT1, OUT2 (int. enable) */
[c7235d40]734 pio_write_8(&ns->regs->mcr, 0x00);
[5b68e0c]735 /* Disable all interrupts from the port */
[c7235d40]736 ns8250_port_interrupts_disable(ns->regs);
[5b68e0c]737}
738
[49698fa]739/** Read the data from the serial port device and store them to the input
740 * buffer.
741 *
[68414f4a]742 * @param ns Serial port device
[cf8cc36]743 */
[68414f4a]744static void ns8250_read_from_device(ns8250_t *ns)
[7f8b581]745{
[c7235d40]746 ns8250_regs_t *regs = ns->regs;
[dafe675]747 bool cont = true;
[a35b458]748
[c92e30f]749 fibril_mutex_lock(&ns->mutex);
[49698fa]750 while (cont) {
[c7235d40]751 cont = ns8250_received(regs);
[cf8cc36]752 if (cont) {
[c7235d40]753 uint8_t val = ns8250_read_8(regs);
[a35b458]754
[4510e06]755 if (ns->client_connections > 0) {
[91ecaa10]756 bool buf_was_empty = buf_is_empty(&ns->input_buffer);
[68414f4a]757 if (!buf_push_back(&ns->input_buffer, val)) {
[fc51296]758 ddf_msg(LVL_WARN, "Buffer overflow on "
[56fd7cf]759 "%s.", ddf_dev_get_name(ns->dev));
[c92e30f]760 break;
[dafe675]761 } else {
[fc51296]762 ddf_msg(LVL_DEBUG2, "Character %c saved "
[ebcb05a]763 "to the buffer of %s.",
[56fd7cf]764 val, ddf_dev_get_name(ns->dev));
[91ecaa10]765 if (buf_was_empty)
766 fibril_condvar_broadcast(&ns->input_buffer_available);
[dafe675]767 }
[49698fa]768 }
[dafe675]769 }
[49698fa]770 }
[c92e30f]771 fibril_mutex_unlock(&ns->mutex);
772 fibril_yield();
[2300b9d]773}
774
[cf8cc36]775/** The interrupt handler.
[49698fa]776 *
[e0cd9042]777 * The serial port is initialized to interrupt when some data come or line
778 * status register changes, so the interrupt is handled by reading the incoming
779 * data and reading the line status register.
[49698fa]780 *
[8820544]781 * @param dev The serial port device.
782 *
[cf8cc36]783 */
[01c3bb4]784static inline void ns8250_interrupt_handler(ipc_call_t *icall, ddf_dev_t *dev)
[2300b9d]785{
[56fd7cf]786 ns8250_t *ns = dev_ns8250(dev);
[e0cd9042]787 uint8_t iir = pio_read_8(&ns->regs->iid);
788 if ((iir & NS8250_IID_CAUSE_MASK) == NS8250_IID_CAUSE_RXSTATUS) {
789 uint8_t lsr = pio_read_8(&ns->regs->lsr);
790 if (lsr & NS8250_LSR_OE) {
[56fd7cf]791 ddf_msg(LVL_WARN, "Overrun error on %s", ddf_dev_get_name(ns->dev));
[e0cd9042]792 }
793 }
[a35b458]794
[e0cd9042]795 ns8250_read_from_device(ns);
[d51838f]796 hw_res_clear_interrupt(ns->parent_sess, ns->irq);
[7f8b581]797}
798
[cf8cc36]799/** Register the interrupt handler for the device.
[49698fa]800 *
[68414f4a]801 * @param ns Serial port device
[cf8cc36]802 */
[b7fd2a0]803static inline errno_t ns8250_register_interrupt_handler(ns8250_t *ns,
[eadaeae8]804 cap_irq_handle_t *ihandle)
[7f8b581]805{
[68414f4a]806 return register_interrupt_handler(ns->dev, ns->irq,
[eadaeae8]807 ns8250_interrupt_handler, NULL, ihandle);
[2300b9d]808}
809
[cf8cc36]810/** Unregister the interrupt handler for the device.
[49698fa]811 *
[68414f4a]812 * @param ns Serial port device
[cf8cc36]813 */
[b7fd2a0]814static inline errno_t ns8250_unregister_interrupt_handler(ns8250_t *ns)
[2300b9d]815{
[eadaeae8]816 return unregister_interrupt_handler(ns->dev, ns->irq_handle);
[7f8b581]817}
818
[0c0f823b]819/** The dev_add callback method of the serial port driver.
[49698fa]820 *
[cf8cc36]821 * Probe and initialize the newly added device.
[49698fa]822 *
823 * @param dev The serial port device.
[cf8cc36]824 */
[b7fd2a0]825static errno_t ns8250_dev_add(ddf_dev_t *dev)
[5fe1c32]826{
[68414f4a]827 ns8250_t *ns = NULL;
[83a2f43]828 ddf_fun_t *fun = NULL;
[68414f4a]829 bool need_cleanup = false;
[dad0d2f]830 bool need_unreg_intr_handler = false;
[4f87a85a]831 bool bound = false;
[b7fd2a0]832 errno_t rc;
[a35b458]833
[0c0f823b]834 ddf_msg(LVL_DEBUG, "ns8250_dev_add %s (handle = %d)",
[56fd7cf]835 ddf_dev_get_name(dev), (int) ddf_dev_get_handle(dev));
[a35b458]836
[68414f4a]837 /* Allocate soft-state for the device */
[5f6e25e]838 ns = ddf_dev_data_alloc(dev, sizeof(ns8250_t));
[68414f4a]839 if (ns == NULL) {
840 rc = ENOMEM;
841 goto fail;
842 }
[a35b458]843
[5f6e25e]844 fibril_mutex_initialize(&ns->mutex);
[91ecaa10]845 fibril_condvar_initialize(&ns->input_buffer_available);
[68414f4a]846 ns->dev = dev;
[a35b458]847
[d51838f]848 ns->parent_sess = ddf_dev_parent_sess_get(ns->dev);
849 if (ns->parent_sess == NULL) {
850 ddf_msg(LVL_ERROR, "Failed to connect to parent driver of "
851 "device %s.", ddf_dev_get_name(ns->dev));
852 rc = EIO;
853 goto fail;
854 }
[a35b458]855
[68414f4a]856 rc = ns8250_dev_initialize(ns);
857 if (rc != EOK)
858 goto fail;
[a35b458]859
[68414f4a]860 need_cleanup = true;
[a35b458]861
[68414f4a]862 if (!ns8250_pio_enable(ns)) {
863 rc = EADDRNOTAVAIL;
864 goto fail;
[49698fa]865 }
[a35b458]866
[49698fa]867 /* Find out whether the device is present. */
[68414f4a]868 if (!ns8250_dev_probe(ns)) {
869 rc = ENOENT;
870 goto fail;
[49698fa]871 }
[a35b458]872
[49698fa]873 /* Serial port initialization (baud rate etc.). */
[68414f4a]874 ns8250_initialize_port(ns);
[a35b458]875
[49698fa]876 /* Register interrupt handler. */
[eadaeae8]877 ns->irq_handle = CAP_NIL;
878 rc = ns8250_register_interrupt_handler(ns, &ns->irq_handle);
[071a1ddb]879 if (rc != EOK) {
[ebcb05a]880 ddf_msg(LVL_ERROR, "Failed to register interrupt handler.");
[68414f4a]881 rc = EADDRNOTAVAIL;
882 goto fail;
[7f8b581]883 }
[dad0d2f]884 need_unreg_intr_handler = true;
[c4e30607]885
[49698fa]886 /* Enable interrupt. */
[68414f4a]887 rc = ns8250_interrupt_enable(ns);
888 if (rc != EOK) {
[fc51296]889 ddf_msg(LVL_ERROR, "Failed to enable the interrupt. Error code = "
[dd8ab1c]890 "%s.", str_error_name(rc));
[68414f4a]891 goto fail;
[49698fa]892 }
[a35b458]893
[97a62fe]894 fun = ddf_fun_create(dev, fun_exposed, "a");
895 if (fun == NULL) {
[ebcb05a]896 ddf_msg(LVL_ERROR, "Failed creating function.");
[97a62fe]897 goto fail;
898 }
[a35b458]899
[74017ce]900 ddf_fun_set_conn_handler(fun, ns8250_char_conn);
[a35b458]901
[74017ce]902 chardev_srvs_init(&ns->cds);
903 ns->cds.ops = &ns8250_chardev_ops;
904 ns->cds.sarg = ns;
[a35b458]905
[97a62fe]906 rc = ddf_fun_bind(fun);
907 if (rc != EOK) {
[ebcb05a]908 ddf_msg(LVL_ERROR, "Failed binding function.");
[97a62fe]909 goto fail;
910 }
911
[4f87a85a]912 bound = true;
[68414f4a]913 ns->fun = fun;
[a35b458]914
[4f87a85a]915 rc = ddf_fun_add_to_category(fun, "serial");
916 if (rc != EOK) {
917 ddf_msg(LVL_ERROR, "Error adding function to category 'serial'.");
918 goto fail;
919 }
[a35b458]920
[ebcb05a]921 ddf_msg(LVL_NOTE, "Device %s successfully initialized.",
[56fd7cf]922 ddf_dev_get_name(dev));
[a35b458]923
[df747b9c]924 return EOK;
[68414f4a]925fail:
[4f87a85a]926 if (bound)
927 ddf_fun_unbind(fun);
[97a62fe]928 if (fun != NULL)
929 ddf_fun_destroy(fun);
[dad0d2f]930 if (need_unreg_intr_handler)
931 ns8250_unregister_interrupt_handler(ns);
[68414f4a]932 if (need_cleanup)
933 ns8250_dev_cleanup(ns);
934 return rc;
[5fe1c32]935}
936
[b7fd2a0]937static errno_t ns8250_dev_remove(ddf_dev_t *dev)
[5b68e0c]938{
[56fd7cf]939 ns8250_t *ns = dev_ns8250(dev);
[b7fd2a0]940 errno_t rc;
[a35b458]941
[5b68e0c]942 fibril_mutex_lock(&ns->mutex);
[956d4281]943 if (ns->client_connections > 0) {
[5b68e0c]944 fibril_mutex_unlock(&ns->mutex);
945 return EBUSY;
946 }
947 ns->removed = true;
948 fibril_mutex_unlock(&ns->mutex);
[a35b458]949
[5b68e0c]950 rc = ddf_fun_unbind(ns->fun);
951 if (rc != EOK) {
952 ddf_msg(LVL_ERROR, "Failed to unbind function.");
953 return rc;
954 }
[a35b458]955
[5b68e0c]956 ddf_fun_destroy(ns->fun);
[a35b458]957
[5b68e0c]958 ns8250_port_cleanup(ns);
959 ns8250_unregister_interrupt_handler(ns);
960 ns8250_dev_cleanup(ns);
961 return EOK;
962}
963
[25a7e11d]964/** Open the device.
[49698fa]965 *
966 * This is a callback function called when a client tries to connect to the
967 * device.
968 *
[74017ce]969 * @param srvs Service structure
970 * @param srv Server-side connection structure
[25a7e11d]971 */
[b7fd2a0]972static errno_t ns8250_open(chardev_srvs_t *srvs, chardev_srv_t *srv)
[25a7e11d]973{
[74017ce]974 ns8250_t *ns = srv_ns8250(srv);
[b7fd2a0]975 errno_t res;
[a35b458]976
[5b68e0c]977 fibril_mutex_lock(&ns->mutex);
[4510e06]978 if (ns->removed) {
[5b68e0c]979 res = ENXIO;
[25a7e11d]980 } else {
981 res = EOK;
[4510e06]982 ns->client_connections++;
[25a7e11d]983 }
[5b68e0c]984 fibril_mutex_unlock(&ns->mutex);
[a35b458]985
[25a7e11d]986 return res;
987}
988
989/** Close the device.
[49698fa]990 *
991 * This is a callback function called when a client tries to disconnect from
992 * the device.
993 *
[74017ce]994 * @param srv Server-side connection structure
[25a7e11d]995 */
[b7fd2a0]996static errno_t ns8250_close(chardev_srv_t *srv)
[25a7e11d]997{
[74017ce]998 ns8250_t *data = srv_ns8250(srv);
[a35b458]999
[25a7e11d]1000 fibril_mutex_lock(&data->mutex);
[a35b458]1001
[4510e06]1002 assert(data->client_connections > 0);
[a35b458]1003
[4510e06]1004 if (!(--data->client_connections))
1005 buf_clear(&data->input_buffer);
[a35b458]1006
[49698fa]1007 fibril_mutex_unlock(&data->mutex);
[a35b458]1008
[74017ce]1009 return EOK;
[25a7e11d]1010}
1011
[49698fa]1012/** Get parameters of the serial communication which are set to the specified
1013 * device.
1014 *
1015 * @param dev The serial port device.
1016 * @param baud_rate The baud rate used by the device.
1017 * @param parity The type of parity used by the device.
1018 * @param word_length The size of one data unit in bits.
1019 * @param stop_bits The number of stop bits used.
[cf8cc36]1020 */
[18b6a88]1021static void ns8250_get_props(ddf_dev_t *dev, unsigned int *baud_rate,
1022 unsigned int *parity, unsigned int *word_length, unsigned int *stop_bits)
[49698fa]1023{
[56fd7cf]1024 ns8250_t *data = dev_ns8250(dev);
[c7235d40]1025 ns8250_regs_t *regs = data->regs;
[a35b458]1026
[49698fa]1027 fibril_mutex_lock(&data->mutex);
[c7235d40]1028 ns8250_port_interrupts_disable(regs);
1029 *baud_rate = ns8250_port_get_baud_rate(regs);
1030 ns8250_port_get_com_props(regs, parity, word_length, stop_bits);
1031 ns8250_port_interrupts_enable(regs);
[49698fa]1032 fibril_mutex_unlock(&data->mutex);
[a35b458]1033
[fc51296]1034 ddf_msg(LVL_DEBUG, "ns8250_get_props: baud rate %d, parity 0x%x, word "
[ebcb05a]1035 "length %d, stop bits %d", *baud_rate, *parity, *word_length,
[49698fa]1036 *stop_bits);
[cf8cc36]1037}
1038
[49698fa]1039/** Set parameters of the serial communication to the specified serial port
1040 * device.
1041 *
1042 * @param dev The serial port device.
1043 * @param baud_rate The baud rate to be used by the device.
1044 * @param parity The type of parity to be used by the device.
1045 * @param word_length The size of one data unit in bits.
1046 * @param stop_bits The number of stop bits to be used.
[cf8cc36]1047 */
[b7fd2a0]1048static errno_t ns8250_set_props(ddf_dev_t *dev, unsigned int baud_rate,
[33dbbd2]1049 unsigned int parity, unsigned int word_length, unsigned int stop_bits)
[cf8cc36]1050{
[fc51296]1051 ddf_msg(LVL_DEBUG, "ns8250_set_props: baud rate %d, parity 0x%x, word "
[ebcb05a]1052 "length %d, stop bits %d", baud_rate, parity, word_length,
[49698fa]1053 stop_bits);
[a35b458]1054
[56fd7cf]1055 ns8250_t *data = dev_ns8250(dev);
[c7235d40]1056 ns8250_regs_t *regs = data->regs;
[b7fd2a0]1057 errno_t ret;
[a35b458]1058
[49698fa]1059 fibril_mutex_lock(&data->mutex);
[c7235d40]1060 ns8250_port_interrupts_disable(regs);
1061 ret = ns8250_port_set_baud_rate(regs, baud_rate);
[33dbbd2]1062 if (ret == EOK)
[c7235d40]1063 ret = ns8250_port_set_com_props(regs, parity, word_length, stop_bits);
1064 ns8250_port_interrupts_enable(regs);
[49698fa]1065 fibril_mutex_unlock(&data->mutex);
[a35b458]1066
[49698fa]1067 return ret;
[cf8cc36]1068}
1069
[49698fa]1070/** Default handler for client requests which are not handled by the standard
1071 * interfaces.
1072 *
[f4ef3c2]1073 * Configure the parameters of the serial communication.
1074 */
[984a9ba]1075static void ns8250_default_handler(chardev_srv_t *srv, ipc_call_t *call)
[f4ef3c2]1076{
[74017ce]1077 ns8250_t *ns8250 = srv_ns8250(srv);
[228e490]1078 sysarg_t method = IPC_GET_IMETHOD(*call);
[b7fd2a0]1079 errno_t ret;
[49698fa]1080 unsigned int baud_rate, parity, word_length, stop_bits;
[a35b458]1081
[49698fa]1082 switch (method) {
1083 case SERIAL_GET_COM_PROPS:
[74017ce]1084 ns8250_get_props(ns8250->dev, &baud_rate, &parity, &word_length,
[49698fa]1085 &stop_bits);
[984a9ba]1086 async_answer_4(call, EOK, baud_rate, parity, word_length,
[49698fa]1087 stop_bits);
1088 break;
[a35b458]1089
[49698fa]1090 case SERIAL_SET_COM_PROPS:
[3bacee1]1091 baud_rate = IPC_GET_ARG1(*call);
[49698fa]1092 parity = IPC_GET_ARG2(*call);
1093 word_length = IPC_GET_ARG3(*call);
1094 stop_bits = IPC_GET_ARG4(*call);
[74017ce]1095 ret = ns8250_set_props(ns8250->dev, baud_rate, parity, word_length,
[49698fa]1096 stop_bits);
[984a9ba]1097 async_answer_0(call, ret);
[49698fa]1098 break;
[a35b458]1099
[49698fa]1100 default:
[984a9ba]1101 async_answer_0(call, ENOTSUP);
[49698fa]1102 }
[f4ef3c2]1103}
1104
[984a9ba]1105void ns8250_char_conn(ipc_call_t *icall, void *arg)
[74017ce]1106{
1107 ns8250_t *ns8250 = fun_ns8250((ddf_fun_t *)arg);
1108
[984a9ba]1109 chardev_conn(icall, &ns8250->cds);
[74017ce]1110}
1111
[25a7e11d]1112/** Initialize the serial port driver.
[49698fa]1113 *
1114 * Initialize device operations structures with callback methods for handling
[25a7e11d]1115 * client requests to the serial port devices.
1116 */
[49698fa]1117static void ns8250_init(void)
[5fe1c32]1118{
[267f235]1119 ddf_log_init(NAME);
[5fe1c32]1120}
1121
1122int main(int argc, char *argv[])
1123{
[49698fa]1124 printf(NAME ": HelenOS serial port driver\n");
[04c7003f]1125 ns8250_init();
[83a2f43]1126 return ddf_driver_main(&ns8250_driver);
[5fe1c32]1127}
1128
1129/**
1130 * @}
[49698fa]1131 */
Note: See TracBrowser for help on using the repository browser.