1 //==========================================================================
3 // io/serial/common/tty.c
5 // TTY (terminal-like interface) I/O driver
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //==========================================================================
41 //#####DESCRIPTIONBEGIN####
44 // Contributors: gthomas
46 // Purpose: Device driver for tty I/O, layered on top of serial I/O
49 //####DESCRIPTIONEND####
51 //==========================================================================
53 #include <pkgconf/io.h>
54 #include <pkgconf/io_serial.h>
55 #ifdef CYGPKG_IO_SERIAL_TTY
56 #include <cyg/io/io.h>
57 #include <cyg/io/devtab.h>
58 #include <cyg/io/ttyio.h>
59 #include <cyg/infra/diag.h>
61 static bool tty_init(struct cyg_devtab_entry *tab);
62 static Cyg_ErrNo tty_lookup(struct cyg_devtab_entry **tab,
63 struct cyg_devtab_entry *sub_tab,
65 static Cyg_ErrNo tty_write(cyg_io_handle_t handle, const void *buf, cyg_uint32 *len);
66 static Cyg_ErrNo tty_read(cyg_io_handle_t handle, void *buf, cyg_uint32 *len);
67 static cyg_bool tty_select(cyg_io_handle_t handle, cyg_uint32 which, CYG_ADDRWORD info);
68 static Cyg_ErrNo tty_get_config(cyg_io_handle_t handle, cyg_uint32 key, void *buf, cyg_uint32 *len);
69 static Cyg_ErrNo tty_set_config(cyg_io_handle_t handle, cyg_uint32 key, const void *buf, cyg_uint32 *len);
71 struct tty_private_info {
72 cyg_tty_info_t dev_info;
73 cyg_io_handle_t dev_handle;
76 static DEVIO_TABLE(tty_devio,
84 #ifdef CYGPKG_IO_SERIAL_TTY_TTYDIAG
85 static struct tty_private_info tty_private_info_diag;
86 DEVTAB_ENTRY(tty_io_diag,
88 // CYGDAT_IO_SERIAL_TTY_CONSOLE, // Based on driver for this device
93 tty_lookup, // Execute this when device is being looked up
94 &tty_private_info_diag);
97 #ifdef CYGPKG_IO_SERIAL_TTY_TTY0
98 static struct tty_private_info tty_private_info0;
101 CYGDAT_IO_SERIAL_TTY_TTY0_DEV,
104 tty_lookup, // Execute this when device is being looked up
108 #ifdef CYGPKG_IO_SERIAL_TTY_TTY1
109 static struct tty_private_info tty_private_info1;
110 DEVTAB_ENTRY(tty_io1,
112 CYGDAT_IO_SERIAL_TTY_TTY1_DEV,
115 tty_lookup, // Execute this when device is being looked up
119 #ifdef CYGPKG_IO_SERIAL_TTY_TTY2
120 static struct tty_private_info tty_private_info2;
121 DEVTAB_ENTRY(tty_io2,
123 CYGDAT_IO_SERIAL_TTY_TTY2_DEV,
126 tty_lookup, // Execute this when device is being looked up
130 #ifdef CYGPKG_IO_SERIAL_TTY_TTY3
131 static struct tty_private_info tty_private_info3;
132 DEVTAB_ENTRY(tty_io3,
134 CYGDAT_IO_SERIAL_TTY_TTY3_DEV,
137 tty_lookup, // Execute this when device is being looked up
142 tty_init(struct cyg_devtab_entry *tab)
144 struct tty_private_info *priv = (struct tty_private_info *)tab->priv;
145 #ifdef CYGDBG_IO_INIT
146 diag_printf("Init tty channel: %p\n", tab);
148 priv->dev_info.tty_out_flags = CYG_TTY_OUT_FLAGS_DEFAULT;
149 priv->dev_info.tty_in_flags = CYG_TTY_IN_FLAGS_DEFAULT;
154 tty_lookup(struct cyg_devtab_entry **tab,
155 struct cyg_devtab_entry *sub_tab,
158 cyg_io_handle_t chan = (cyg_io_handle_t)sub_tab;
159 struct tty_private_info *priv = (struct tty_private_info *)(*tab)->priv;
163 priv->dev_handle = chan;
165 len = sizeof(cyg_serial_info_t);
166 // Initialize configuration
167 cyg_io_get_config(chan, CYG_SERIAL_GET_CONFIG,
168 (void *)&priv->dev_info.serial_config, &len);
176 tty_write(cyg_io_handle_t handle, const void *_buf, cyg_uint32 *len)
178 cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
179 struct tty_private_info *priv = (struct tty_private_info *)t->priv;
180 cyg_io_handle_t chan = (cyg_io_handle_t)priv->dev_handle;
182 cyg_int32 bytes_successful, actually_written;
183 cyg_uint8 xbuf[BUFSIZE];
185 cyg_uint8 *buf = (cyg_uint8 *)_buf;
186 Cyg_ErrNo res = -EBADF;
189 bytes_successful = 0;
190 actually_written = 0;
191 while (bytes_successful++ < *len) {
194 (priv->dev_info.tty_out_flags & CYG_TTY_OUT_FLAGS_CRLF)) {
198 // Always leave room for possible CR/LF expansion
199 if ((size >= (BUFSIZE-1)) ||
200 (bytes_successful == *len)) {
201 res = cyg_io_write(chan, xbuf, &size);
203 *len = actually_written;
206 actually_written += size;
214 tty_read(cyg_io_handle_t handle, void *_buf, cyg_uint32 *len)
216 cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
217 struct tty_private_info *priv = (struct tty_private_info *)t->priv;
218 cyg_io_handle_t chan = (cyg_io_handle_t)priv->dev_handle;
223 cyg_uint8 *buf = (cyg_uint8 *)_buf;
226 while (size < *len) {
228 res = cyg_io_read(chan, &c, &clen);
234 if ((priv->dev_info.tty_in_flags & CYG_TTY_IN_FLAGS_BINARY) == 0) {
236 case '\b': /* drop through */
238 size -= 2; // erase one character + 'backspace' char
241 } else if (priv->dev_info.tty_in_flags & CYG_TTY_IN_FLAGS_ECHO) {
243 cyg_io_write(chan, "\b \b", &clen);
247 if (priv->dev_info.tty_in_flags & CYG_TTY_IN_FLAGS_CRLF) {
248 /* Don't do anything because a '\n' will come next */
251 if (priv->dev_info.tty_in_flags & CYG_TTY_IN_FLAGS_CR) {
252 c = '\n'; // Map CR -> LF
256 if (priv->dev_info.tty_in_flags & CYG_TTY_IN_FLAGS_ECHO) {
257 if (priv->dev_info.tty_out_flags & CYG_TTY_OUT_FLAGS_CRLF) {
259 cyg_io_write(chan, "\r\n", &clen);
262 cyg_io_write(chan, &c, &clen);
269 if (priv->dev_info.tty_in_flags & CYG_TTY_IN_FLAGS_ECHO) {
271 cyg_io_write(chan, &c, &clen);
282 tty_select(cyg_io_handle_t handle, cyg_uint32 which, CYG_ADDRWORD info)
284 cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
285 struct tty_private_info *priv = (struct tty_private_info *)t->priv;
286 cyg_io_handle_t chan = (cyg_io_handle_t)priv->dev_handle;
288 // Just pass it on to next driver level
289 return cyg_io_select( chan, which, info );
293 tty_get_config(cyg_io_handle_t handle, cyg_uint32 key, void *buf, cyg_uint32 *len)
295 cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
296 struct tty_private_info *priv = (struct tty_private_info *)t->priv;
297 cyg_io_handle_t chan = (cyg_io_handle_t)priv->dev_handle;
298 Cyg_ErrNo res = ENOERR;
300 cyg_int32 current_len;
304 case CYG_IO_GET_CONFIG_TTY_INFO:
305 if (*len < sizeof(cyg_tty_info_t)) {
309 current_len = sizeof(cyg_serial_info_t);
310 res = cyg_io_get_config(chan, CYG_SERIAL_GET_CONFIG,
311 (void *)&priv->dev_info.serial_config, ¤t_len);
316 *(cyg_tty_info_t *)buf = priv->dev_info;
317 *len = sizeof(cyg_tty_info_t);
319 default: // Assume this is a 'serial' driver control
320 res = cyg_io_get_config(chan, key, buf, len);
326 tty_set_config(cyg_io_handle_t handle, cyg_uint32 key, const void *buf, cyg_uint32 *len)
328 cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
329 struct tty_private_info *priv = (struct tty_private_info *)t->priv;
330 cyg_io_handle_t chan = (cyg_io_handle_t)priv->dev_handle;
332 cyg_int32 current_len;
334 Cyg_ErrNo res = ENOERR;
337 case CYG_IO_SET_CONFIG_TTY_INFO:
338 if (*len != sizeof(cyg_tty_info_t)) {
341 priv->dev_info = *(cyg_tty_info_t *)buf;
343 default: // Pass on to serial driver
344 res = cyg_io_set_config(chan, key, buf, len);
348 #endif // CYGPKG_IO_SERIAL_TTY