source: mainline/uspace/drv/char/ns8250/ns8250.c@ 6d527cff

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6d527cff was fafb8e5, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Mechanically lowercase IPC_SET_*/IPC_GET_*

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