Mercurial > flash_v2
annotate packages/io/serial/current/src/common/tty.c @ 66:bf00f99aec69 ecos-sw-2000-02-02
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
| author | jlarmour |
|---|---|
| date | Wed, 02 Feb 2000 19:57:02 +0000 |
| parents | c38311975d4f |
| children | 2085233a121a |
| rev | line source |
|---|---|
| 2 | 1 //========================================================================== |
| 2 // | |
| 3 // io/serial/common/tty.c | |
| 4 // | |
| 5 // TTY (terminal-like interface) I/O driver | |
| 6 // | |
| 7 //========================================================================== | |
| 8 //####COPYRIGHTBEGIN#### | |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
62
diff
changeset
|
9 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
62
diff
changeset
|
10 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
62
diff
changeset
|
11 // The contents of this file are subject to the Red Hat eCos Public License |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
12 // Version 1.1 (the "License"); you may not use this file except in |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
62
diff
changeset
|
13 // compliance with the License. You may obtain a copy of the License at |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
14 // http://www.redhat.com/ |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
62
diff
changeset
|
15 // |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
16 // Software distributed under the License is distributed on an "AS IS" |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
62
diff
changeset
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
62
diff
changeset
|
18 // License for the specific language governing rights and limitations under |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
62
diff
changeset
|
19 // the License. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
62
diff
changeset
|
20 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
62
diff
changeset
|
21 // The Original Code is eCos - Embedded Configurable Operating System, |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
62
diff
changeset
|
22 // released September 30, 1998. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
62
diff
changeset
|
23 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
62
diff
changeset
|
24 // The Initial Developer of the Original Code is Red Hat. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
62
diff
changeset
|
25 // Portions created by Red Hat are |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
62
diff
changeset
|
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
62
diff
changeset
|
27 // All Rights Reserved. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
62
diff
changeset
|
28 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
62
diff
changeset
|
29 // |
| 2 | 30 //####COPYRIGHTEND#### |
| 31 //========================================================================== | |
| 32 //#####DESCRIPTIONBEGIN#### | |
| 33 // | |
| 34 // Author(s): gthomas | |
| 35 // Contributors: gthomas | |
| 36 // Date: 1999-02-04 | |
| 37 // Purpose: Device driver for tty I/O, layered on top of serial I/O | |
| 38 // Description: | |
| 39 // | |
| 40 //####DESCRIPTIONEND#### | |
| 41 // | |
| 42 //========================================================================== | |
| 43 | |
| 44 #include <pkgconf/io.h> | |
| 45 #include <pkgconf/io_serial.h> | |
| 46 #ifdef CYGPKG_IO_SERIAL_TTY | |
| 47 #include <cyg/io/io.h> | |
| 48 #include <cyg/io/devtab.h> | |
| 49 #include <cyg/io/ttyio.h> | |
| 50 #include <cyg/infra/diag.h> | |
| 51 | |
| 52 static bool tty_init(struct cyg_devtab_entry *tab); | |
| 53 static Cyg_ErrNo tty_lookup(struct cyg_devtab_entry **tab, | |
| 54 struct cyg_devtab_entry *sub_tab, | |
| 55 const char *name); | |
| 56 static Cyg_ErrNo tty_write(cyg_io_handle_t handle, const void *buf, cyg_uint32 *len); | |
| 57 static Cyg_ErrNo tty_read(cyg_io_handle_t handle, void *buf, cyg_uint32 *len); | |
| 58 static Cyg_ErrNo tty_get_config(cyg_io_handle_t handle, cyg_uint32 key, void *buf, cyg_uint32 *len); | |
| 59 static Cyg_ErrNo tty_set_config(cyg_io_handle_t handle, cyg_uint32 key, const void *buf, cyg_uint32 *len); | |
| 60 | |
| 61 struct tty_private_info { | |
| 62 cyg_tty_info_t dev_info; | |
| 63 cyg_io_handle_t dev_handle; | |
| 64 }; | |
| 65 | |
| 66 static DEVIO_TABLE(tty_devio, | |
| 67 tty_write, | |
| 68 tty_read, | |
| 69 tty_get_config, | |
| 70 tty_set_config | |
| 71 ); | |
| 72 | |
| 73 #ifdef CYGPKG_IO_SERIAL_TTY_TTYDIAG | |
| 74 static struct tty_private_info tty_private_info_diag; | |
| 75 DEVTAB_ENTRY(tty_io_diag, | |
| 76 // "/dev/console", | |
| 77 // CYGDAT_IO_SERIAL_TTY_CONSOLE, // Based on driver for this device | |
| 78 "/dev/ttydiag", | |
| 79 "/dev/haldiag", | |
| 80 &tty_devio, | |
| 81 tty_init, | |
| 82 tty_lookup, // Execute this when device is being looked up | |
| 83 &tty_private_info_diag); | |
| 84 #endif | |
| 85 | |
| 86 #ifdef CYGPKG_IO_SERIAL_TTY_TTY0 | |
| 87 static struct tty_private_info tty_private_info0; | |
| 88 DEVTAB_ENTRY(tty_io0, | |
| 89 "/dev/tty0", | |
| 90 CYGDAT_IO_SERIAL_TTY_TTY0_DEV, | |
| 91 &tty_devio, | |
| 92 tty_init, | |
| 93 tty_lookup, // Execute this when device is being looked up | |
| 94 &tty_private_info0); | |
| 95 #endif | |
| 96 | |
| 97 #ifdef CYGPKG_IO_SERIAL_TTY_TTY1 | |
| 98 static struct tty_private_info tty_private_info1; | |
| 99 DEVTAB_ENTRY(tty_io1, | |
| 100 "/dev/tty1", | |
| 101 CYGDAT_IO_SERIAL_TTY_TTY1_DEV, | |
| 102 &tty_devio, | |
| 103 tty_init, | |
| 104 tty_lookup, // Execute this when device is being looked up | |
| 105 &tty_private_info1); | |
| 106 #endif | |
| 107 | |
| 108 #ifdef CYGPKG_IO_SERIAL_TTY_TTY2 | |
| 109 static struct tty_private_info tty_private_info2; | |
| 110 DEVTAB_ENTRY(tty_io2, | |
| 111 "/dev/tty2", | |
| 112 CYGDAT_IO_SERIAL_TTY_TTY2_DEV, | |
| 113 &tty_devio, | |
| 114 tty_init, | |
| 115 tty_lookup, // Execute this when device is being looked up | |
| 116 &tty_private_info2); | |
| 117 #endif | |
| 118 | |
| 119 static bool | |
| 120 tty_init(struct cyg_devtab_entry *tab) | |
| 121 { | |
| 122 struct tty_private_info *priv = (struct tty_private_info *)tab->priv; | |
| 123 #ifdef CYGDBG_IO_INIT | |
| 124 diag_printf("Init tty channel: %x\n", tab); | |
| 125 #endif | |
| 126 priv->dev_info.tty_out_flags = CYG_TTY_OUT_FLAGS_DEFAULT; | |
| 127 priv->dev_info.tty_in_flags = CYG_TTY_IN_FLAGS_DEFAULT; | |
| 128 return true; | |
| 129 } | |
| 130 | |
| 131 static Cyg_ErrNo | |
| 132 tty_lookup(struct cyg_devtab_entry **tab, | |
| 133 struct cyg_devtab_entry *sub_tab, | |
| 134 const char *name) | |
| 135 { | |
| 136 cyg_io_handle_t chan = (cyg_io_handle_t)sub_tab; | |
| 137 struct tty_private_info *priv = (struct tty_private_info *)(*tab)->priv; | |
| 138 #if 0 | |
| 139 cyg_int32 len; | |
| 140 #endif | |
| 141 priv->dev_handle = chan; | |
| 142 #if 0 | |
| 143 len = sizeof(cyg_serial_info_t); | |
| 144 // Initialize configuration | |
| 145 cyg_io_get_config(chan, CYG_SERIAL_GET_CONFIG, | |
| 146 (void *)&priv->dev_info.serial_config, &len); | |
| 147 #endif | |
| 148 return ENOERR; | |
| 149 } | |
| 150 | |
| 151 #define BUFSIZE 64 | |
| 152 | |
| 153 static Cyg_ErrNo | |
| 154 tty_write(cyg_io_handle_t handle, const void *_buf, cyg_uint32 *len) | |
| 155 { | |
| 156 cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle; | |
| 157 struct tty_private_info *priv = (struct tty_private_info *)t->priv; | |
| 158 cyg_io_handle_t chan = (cyg_io_handle_t)priv->dev_handle; | |
| 159 cyg_int32 size, bytes_successful, actually_written; | |
| 160 cyg_uint8 xbuf[BUFSIZE]; | |
| 161 cyg_uint8 c; | |
| 162 cyg_uint8 *buf = (cyg_uint8 *)_buf; | |
| 163 Cyg_ErrNo res = -EBADF; | |
| 164 // assert(chan) | |
| 165 size = 0; | |
| 166 bytes_successful = 0; | |
| 167 actually_written = 0; | |
| 168 while (bytes_successful++ < *len) { | |
| 169 xbuf[size++] = (c = *buf++); | |
| 170 if ((c == '\n') && | |
| 171 (priv->dev_info.tty_out_flags & CYG_TTY_OUT_FLAGS_CRLF)) { | |
| 172 xbuf[size++] = '\r'; | |
| 173 } | |
| 174 // Always leave room for possible CR/LF expansion | |
| 175 if ((size == (BUFSIZE-1)) || | |
| 176 (bytes_successful == *len)) { | |
| 177 res = cyg_io_write(chan, xbuf, &size); | |
| 178 if (res != ENOERR) { | |
| 179 *len = actually_written; | |
| 180 return res; | |
| 181 } | |
| 182 actually_written += size; | |
| 183 size = 0; | |
| 184 } | |
| 185 } | |
| 186 *len = actually_written; | |
| 187 return res; | |
| 188 } | |
| 189 | |
| 190 static Cyg_ErrNo | |
| 191 tty_read(cyg_io_handle_t handle, void *_buf, cyg_uint32 *len) | |
| 192 { | |
| 193 cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle; | |
| 194 struct tty_private_info *priv = (struct tty_private_info *)t->priv; | |
| 195 cyg_io_handle_t chan = (cyg_io_handle_t)priv->dev_handle; | |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
196 cyg_uint32 clen; |
|
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
197 cyg_int32 size; |
| 2 | 198 Cyg_ErrNo res; |
| 199 cyg_uint8 c; | |
| 200 cyg_uint8 *buf = (cyg_uint8 *)_buf; | |
| 201 // assert(chan) | |
| 202 size = 0; | |
| 203 while (size < *len) { | |
| 204 clen = 1; | |
| 205 res = cyg_io_read(chan, &c, &clen); | |
| 206 if (res != ENOERR) { | |
| 207 *len = size; | |
| 208 return res; | |
| 209 } | |
| 210 buf[size++] = c; | |
| 211 if ((priv->dev_info.tty_in_flags & CYG_TTY_IN_FLAGS_BINARY) == 0) { | |
| 212 if ((c == '\b') || (c == 0x7F)) { | |
| 213 size -= 2; // erase one character + 'backspace' char | |
|
62
7a6ac9edc838
Merge from eCos master repository on 2000-01-24-19:43:49-GMT
jlarmour
parents:
2
diff
changeset
|
214 if (size < 0) { |
|
7a6ac9edc838
Merge from eCos master repository on 2000-01-24-19:43:49-GMT
jlarmour
parents:
2
diff
changeset
|
215 size = 0; |
|
7a6ac9edc838
Merge from eCos master repository on 2000-01-24-19:43:49-GMT
jlarmour
parents:
2
diff
changeset
|
216 } else if (priv->dev_info.tty_in_flags & CYG_TTY_IN_FLAGS_ECHO) { |
| 2 | 217 clen = 3; |
| 218 cyg_io_write(chan, "\b \b", &clen); | |
| 219 } | |
| 220 } else if ((c == '\n') || (c == '\r')) { | |
| 221 clen = 2; | |
| 222 if (priv->dev_info.tty_in_flags & CYG_TTY_IN_FLAGS_ECHO) { | |
| 223 cyg_io_write(chan, "\n\r", &clen); | |
| 224 } | |
| 225 if (priv->dev_info.tty_in_flags & CYG_TTY_IN_FLAGS_CRLF) { | |
| 226 c = '\n'; // Map CR -> LF | |
| 227 } | |
| 228 buf[size-1] = c; | |
| 229 break; | |
| 230 } else { | |
| 231 if (priv->dev_info.tty_in_flags & CYG_TTY_IN_FLAGS_ECHO) { | |
| 232 clen = 1; | |
| 233 cyg_io_write(chan, &c, &clen); | |
| 234 } | |
| 235 } | |
| 236 } | |
| 237 } | |
| 238 *len = size; | |
| 239 return ENOERR; | |
| 240 } | |
| 241 | |
| 242 static Cyg_ErrNo | |
| 243 tty_get_config(cyg_io_handle_t handle, cyg_uint32 key, void *buf, cyg_uint32 *len) | |
| 244 { | |
| 245 cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle; | |
| 246 struct tty_private_info *priv = (struct tty_private_info *)t->priv; | |
| 247 cyg_io_handle_t chan = (cyg_io_handle_t)priv->dev_handle; | |
| 248 Cyg_ErrNo res = ENOERR; | |
| 249 #if 0 | |
| 250 cyg_int32 current_len; | |
| 251 #endif | |
| 252 // assert(chan) | |
| 253 switch (key) { | |
| 254 case CYG_IO_GET_CONFIG_TTY_INFO: | |
| 255 if (*len < sizeof(cyg_tty_info_t)) { | |
| 256 return -EINVAL; | |
| 257 } | |
| 258 #if 0 | |
| 259 current_len = sizeof(cyg_serial_info_t); | |
| 260 res = cyg_io_get_config(chan, CYG_SERIAL_GET_CONFIG, | |
| 261 (void *)&priv->dev_info.serial_config, ¤t_len); | |
| 262 if (res != ENOERR) { | |
| 263 return res; | |
| 264 } | |
| 265 #endif | |
| 266 *(cyg_tty_info_t *)buf = priv->dev_info; | |
| 267 *len = sizeof(cyg_tty_info_t); | |
| 268 break; | |
| 269 default: // Assume this is a 'serial' driver control | |
| 270 res = cyg_io_get_config(chan, key, buf, len); | |
| 271 } | |
| 272 return res; | |
| 273 } | |
| 274 | |
| 275 static Cyg_ErrNo | |
| 276 tty_set_config(cyg_io_handle_t handle, cyg_uint32 key, const void *buf, cyg_uint32 *len) | |
| 277 { | |
| 278 cyg_devtab_entry_t *t = (cyg_devtab_entry_t *)handle; | |
| 279 struct tty_private_info *priv = (struct tty_private_info *)t->priv; | |
| 280 cyg_io_handle_t chan = (cyg_io_handle_t)priv->dev_handle; | |
| 281 #if 0 | |
| 282 cyg_int32 current_len; | |
| 283 #endif | |
| 284 Cyg_ErrNo res = ENOERR; | |
| 285 // assert(chan) | |
| 286 switch (key) { | |
| 287 case CYG_IO_SET_CONFIG_TTY_INFO: | |
| 288 if (*len != sizeof(cyg_tty_info_t)) { | |
| 289 return -EINVAL; | |
| 290 } | |
| 291 priv->dev_info = *(cyg_tty_info_t *)buf; | |
| 292 break; | |
| 293 default: // Pass on to serial driver | |
| 294 res = cyg_io_set_config(chan, key, buf, len); | |
| 295 } | |
| 296 return res; | |
| 297 } | |
| 298 #endif // CYGPKG_IO_SERIAL_TTY |
