1 /* ====================================================================
5 // POSIX termios API implementation
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####
43 // Author(s): jlarmour
46 // Purpose: POSIX termios API support
47 // Description: Most of the real work happens in the POSIX termios tty
50 //####DESCRIPTIONEND####
52 // ==================================================================*/
56 #include <pkgconf/io_serial.h>
58 #ifdef CYGPKG_IO_SERIAL_TERMIOS
62 #include <termios.h> // Header for this file
63 #include <cyg/infra/cyg_type.h> // Common stuff
64 #include <cyg/infra/cyg_ass.h> // Assertion support
65 #include <cyg/infra/cyg_trac.h> // Tracing support
66 #include <cyg/io/serialio.h> // eCos serial implementation
67 #include <cyg/fileio/fileio.h> // file operations
68 #include <cyg/io/io.h>
69 #include <errno.h> // errno
70 #include <unistd.h> // isatty()
75 const struct termios *termios_p;
82 cfgetospeed( const struct termios *termios_p )
84 CYG_REPORT_FUNCTYPE( "returning speed code %d" );
85 CYG_CHECK_DATA_PTRC( termios_p );
86 CYG_REPORT_FUNCARG1XV( termios_p );
87 CYG_REPORT_RETVAL( termios_p->c_ospeed );
88 return termios_p->c_ospeed;
93 cfsetospeed( struct termios *termios_p, speed_t speed )
95 CYG_REPORT_FUNCTYPE( "returning %d" );
96 CYG_CHECK_DATA_PTRC( termios_p );
97 CYG_REPORT_FUNCARG2( "termios_p=%08x, speed=%d", termios_p, speed );
98 CYG_REPORT_RETVAL( termios_p->c_ospeed );
99 if ( speed > B4000000 ) {
101 CYG_REPORT_RETVAL( -1 );
104 termios_p->c_ospeed = speed;
105 CYG_REPORT_RETVAL( 0 );
111 cfgetispeed( const struct termios *termios_p )
113 CYG_REPORT_FUNCTYPE( "returning speed code %d" );
114 CYG_CHECK_DATA_PTRC( termios_p );
115 CYG_REPORT_FUNCARG1XV( termios_p );
116 CYG_REPORT_RETVAL( termios_p->c_ispeed );
117 return termios_p->c_ispeed;
122 cfsetispeed( struct termios *termios_p, speed_t speed )
124 CYG_REPORT_FUNCTYPE( "returning %d" );
125 CYG_CHECK_DATA_PTRC( termios_p );
126 CYG_REPORT_FUNCARG2( "termios_p=%08x, speed=%d", termios_p, speed );
127 if ( speed > B115200 ) {
129 CYG_REPORT_RETVAL( -1 );
132 termios_p->c_ispeed = speed;
133 CYG_REPORT_RETVAL( 0 );
139 cyg_fp_get( int fd );
142 cyg_fp_free( cyg_file *fp );
145 tcgetattr( int fildes, struct termios *termios_p )
149 int len = sizeof( *termios_p );
151 CYG_REPORT_FUNCTYPE( "returning %d" );
152 CYG_REPORT_FUNCARG2( "fildes=%d, termios_p=%08x", fildes, termios_p );
153 CYG_CHECK_DATA_PTRC( termios_p );
155 if ( !isatty(fildes) ) {
157 CYG_REPORT_RETVAL( -1 );
161 fp = cyg_fp_get( fildes );
165 CYG_REPORT_RETVAL( -1 );
169 ret = fp->f_ops->fo_getinfo( fp, CYG_IO_GET_CONFIG_TERMIOS, termios_p,
175 CYG_REPORT_RETVAL( -1 );
178 CYG_REPORT_RETVAL( 0 );
184 tcsetattr( int fildes, int optact, const struct termios *termios_p )
189 int len = sizeof( attr );
191 CYG_REPORT_FUNCTYPE( "returning %d" );
192 CYG_REPORT_FUNCARG3( "fildes=%d, optact=%d, termios_p=%08x",
193 fildes, optact, termios_p );
194 CYG_CHECK_DATA_PTRC( termios_p );
196 if ( !isatty(fildes) ) {
198 CYG_REPORT_RETVAL( -1 );
202 if ( (optact != TCSANOW) && (optact != TCSADRAIN) &&
203 (optact != TCSAFLUSH) ) {
205 CYG_REPORT_RETVAL( -1 );
209 fp = cyg_fp_get( fildes );
213 CYG_REPORT_RETVAL( -1 );
217 attr.termios_p = termios_p;
218 attr.optact = optact;
220 ret = fp->f_ops->fo_setinfo( fp, CYG_IO_SET_CONFIG_TERMIOS, &attr,
227 CYG_REPORT_RETVAL( -1 );
230 CYG_REPORT_RETVAL( 0 );
236 tcsendbreak( int fildes, int duration )
243 tcdrain( int fildes )
248 CYG_REPORT_FUNCTYPE( "returning %d" );
249 CYG_REPORT_FUNCARG1DV( fildes );
251 if ( !isatty(fildes) ) {
253 CYG_REPORT_RETVAL( -1 );
257 fp = cyg_fp_get( fildes );
261 CYG_REPORT_RETVAL( -1 );
265 ret = fp->f_ops->fo_getinfo( fp,
266 CYG_IO_GET_CONFIG_SERIAL_OUTPUT_DRAIN,
272 CYG_REPORT_RETVAL( -1 );
275 CYG_REPORT_RETVAL( 0 );
280 tcflush( int fildes, int queue_sel )
285 CYG_REPORT_FUNCTYPE( "returning %d" );
286 CYG_REPORT_FUNCARG2DV( fildes, queue_sel );
288 if ( !isatty(fildes) ) {
290 CYG_REPORT_RETVAL( -1 );
294 fp = cyg_fp_get( fildes );
298 CYG_REPORT_RETVAL( -1 );
302 switch( queue_sel ) {
304 ret = fp->f_ops->fo_getinfo( fp,
305 CYG_IO_GET_CONFIG_SERIAL_OUTPUT_FLUSH,
309 ret = fp->f_ops->fo_getinfo( fp,
310 CYG_IO_GET_CONFIG_SERIAL_INPUT_FLUSH,
314 ret = fp->f_ops->fo_getinfo( fp,
315 CYG_IO_GET_CONFIG_SERIAL_OUTPUT_FLUSH,
327 CYG_REPORT_RETVAL( -1 );
330 CYG_REPORT_RETVAL( 0 );
335 tcflow( int fildes, int action )
340 int len = sizeof(forcearg);
342 CYG_REPORT_FUNCTYPE( "returning %d" );
343 CYG_REPORT_FUNCARG2DV( fildes, action );
345 if ( !isatty(fildes) ) {
347 CYG_REPORT_RETVAL( -1 );
351 fp = cyg_fp_get( fildes );
355 CYG_REPORT_RETVAL( -1 );
361 forcearg = CYGNUM_SERIAL_FLOW_THROTTLE_TX;
362 ret = fp->f_ops->fo_setinfo( fp,
363 CYG_IO_SET_CONFIG_SERIAL_FLOW_CONTROL_FORCE,
367 forcearg = CYGNUM_SERIAL_FLOW_RESTART_TX;
368 ret = fp->f_ops->fo_setinfo( fp,
369 CYG_IO_SET_CONFIG_SERIAL_FLOW_CONTROL_FORCE,
373 forcearg = CYGNUM_SERIAL_FLOW_THROTTLE_RX;
374 ret = fp->f_ops->fo_setinfo( fp,
375 CYG_IO_SET_CONFIG_SERIAL_FLOW_CONTROL_FORCE,
379 forcearg = CYGNUM_SERIAL_FLOW_RESTART_RX;
380 ret = fp->f_ops->fo_setinfo( fp,
381 CYG_IO_SET_CONFIG_SERIAL_FLOW_CONTROL_FORCE,
393 CYG_REPORT_RETVAL( -1 );
396 CYG_REPORT_RETVAL( 0 );
400 #endif // ifdef CYGPKG_IO_SERIAL_TERMIOS