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
131 tty_init(struct cyg_devtab_entry *tab)
133 struct tty_private_info *priv = (struct tty_private_info *)tab->priv;
134 #ifdef CYGDBG_IO_INIT
135 diag_printf("Init tty channel: %x\n", tab);
137 priv->dev_info.tty_out_flags = CYG_TTY_OUT_FLAGS_DEFAULT;
138 priv->dev_info.tty_in_flags = CYG_TTY_IN_FLAGS_DEFAULT;
143 tty_lookup(struct cyg_devtab_entry **tab,
144 struct cyg_devtab_entry *sub_tab,
147 cyg_io_handle_t chan = (cyg_io_handle_t)sub_tab;
148 struct tty_private_info *priv = (struct tty_private_info *)(*tab)->priv;
152 priv->dev_handle = chan;
154 len = sizeof(cyg_serial_info_t);
155 // Initialize configuration
156 cyg_io_get_config(chan, CYG_SERIAL_GET_CONFIG,
157 (void *)&priv->dev_info.serial_config, &len);
165 tty_write(cyg_io_handle_t handle, const void *_buf, cyg_uint32 *len)
167 cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
168 struct tty_private_info *priv = (struct tty_private_info *)t->priv;
169 cyg_io_handle_t chan = (cyg_io_handle_t)priv->dev_handle;
171 cyg_int32 bytes_successful, actually_written;
172 cyg_uint8 xbuf[BUFSIZE];
174 cyg_uint8 *buf = (cyg_uint8 *)_buf;
175 Cyg_ErrNo res = -EBADF;
178 bytes_successful = 0;
179 actually_written = 0;
180 while (bytes_successful++ < *len) {
183 (priv->dev_info.tty_out_flags & CYG_TTY_OUT_FLAGS_CRLF)) {
187 // Always leave room for possible CR/LF expansion
188 if ((size >= (BUFSIZE-1)) ||
189 (bytes_successful == *len)) {
190 res = cyg_io_write(chan, xbuf, &size);
192 *len = actually_written;
195 actually_written += size;
203 tty_read(cyg_io_handle_t handle, void *_buf, cyg_uint32 *len)
205 cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
206 struct tty_private_info *priv = (struct tty_private_info *)t->priv;
207 cyg_io_handle_t chan = (cyg_io_handle_t)priv->dev_handle;
212 cyg_uint8 *buf = (cyg_uint8 *)_buf;
215 while (size < *len) {
217 res = cyg_io_read(chan, &c, &clen);
223 if ((priv->dev_info.tty_in_flags & CYG_TTY_IN_FLAGS_BINARY) == 0) {
225 case '\b': /* drop through */
227 size -= 2; // erase one character + 'backspace' char
230 } else if (priv->dev_info.tty_in_flags & CYG_TTY_IN_FLAGS_ECHO) {
232 cyg_io_write(chan, "\b \b", &clen);
236 if (priv->dev_info.tty_in_flags & CYG_TTY_IN_FLAGS_CRLF) {
237 /* Don't do anything because a '\n' will come next */
240 if (priv->dev_info.tty_in_flags & CYG_TTY_IN_FLAGS_CR) {
241 c = '\n'; // Map CR -> LF
245 if (priv->dev_info.tty_in_flags & CYG_TTY_IN_FLAGS_ECHO) {
246 if (priv->dev_info.tty_out_flags & CYG_TTY_OUT_FLAGS_CRLF) {
248 cyg_io_write(chan, "\r\n", &clen);
251 cyg_io_write(chan, &c, &clen);
258 if (priv->dev_info.tty_in_flags & CYG_TTY_IN_FLAGS_ECHO) {
260 cyg_io_write(chan, &c, &clen);
271 tty_select(cyg_io_handle_t handle, cyg_uint32 which, CYG_ADDRWORD info)
273 cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
274 struct tty_private_info *priv = (struct tty_private_info *)t->priv;
275 cyg_io_handle_t chan = (cyg_io_handle_t)priv->dev_handle;
277 // Just pass it on to next driver level
278 return cyg_io_select( chan, which, info );
282 tty_get_config(cyg_io_handle_t handle, cyg_uint32 key, void *buf, cyg_uint32 *len)
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;
287 Cyg_ErrNo res = ENOERR;
289 cyg_int32 current_len;
293 case CYG_IO_GET_CONFIG_TTY_INFO:
294 if (*len < sizeof(cyg_tty_info_t)) {
298 current_len = sizeof(cyg_serial_info_t);
299 res = cyg_io_get_config(chan, CYG_SERIAL_GET_CONFIG,
300 (void *)&priv->dev_info.serial_config, ¤t_len);
305 *(cyg_tty_info_t *)buf = priv->dev_info;
306 *len = sizeof(cyg_tty_info_t);
308 default: // Assume this is a 'serial' driver control
309 res = cyg_io_get_config(chan, key, buf, len);
315 tty_set_config(cyg_io_handle_t handle, cyg_uint32 key, const void *buf, cyg_uint32 *len)
317 cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle;
318 struct tty_private_info *priv = (struct tty_private_info *)t->priv;
319 cyg_io_handle_t chan = (cyg_io_handle_t)priv->dev_handle;
321 cyg_int32 current_len;
323 Cyg_ErrNo res = ENOERR;
326 case CYG_IO_SET_CONFIG_TTY_INFO:
327 if (*len != sizeof(cyg_tty_info_t)) {
330 priv->dev_info = *(cyg_tty_info_t *)buf;
332 default: // Pass on to serial driver
333 res = cyg_io_set_config(chan, key, buf, len);
337 #endif // CYGPKG_IO_SERIAL_TTY