source: rtems/c/src/lib/libbsp/i386/i386ex/console/console.c @ 8f35817

4.104.114.84.95
Last change on this file since 8f35817 was 8f35817, checked in by Joel Sherrill <joel.sherrill@…>, on Sep 15, 1997 at 6:52:26 PM

eliminated potential for overfilling buffer on read

  • Property mode set to 100644
File size: 4.8 KB
Line 
1/*
2 *  This file contains the i386ex console IO package.
3 *
4 *  COPYRIGHT (c) 1989-1997.
5 *  On-Line Applications Research Corporation (OAR).
6 *  Copyright assigned to U.S. Government, 1994.
7 *
8 *  The license and distribution terms for this file may in
9 *  the file LICENSE in this distribution or at
10 *  http://www.OARcorp.com/rtems/license.html.
11 *
12 *  $Id$
13 */
14
15#define F386_INIT
16
17#include <bsp.h>
18#include <rtems/libio.h>
19 
20#include <stdlib.h>
21
22#include "../startup/80386ex.h"
23
24/*  console_cleanup
25 *
26 *  This routine is called at exit to clean up the console hardware.
27 *
28 *  Input parameters: NONE
29 *
30 *  Output parameters:  NONE
31 *
32 *  Return values:
33 */
34
35void console_cleanup( void )
36{
37  register rtems_unsigned8 ignored;
38
39/* clear the read buffer */
40
41   inport_byte( RBR0, ignored );
42
43}
44
45/*  console_initialize
46 *
47 *  This routine initializes the console IO driver.
48 *
49 *  Input parameters: NONE
50 *
51 *  Output parameters:  NONE
52 *
53 *  Return values:
54 */
55
56rtems_device_driver console_initialize(
57  rtems_device_major_number  major,
58  rtems_device_minor_number  minor,
59  void                      *arg
60)
61{
62  rtems_status_code status;
63
64  /*
65   *  flush the console now and at exit.  Just in case.
66   */
67
68  console_cleanup();
69
70  status = rtems_io_register_name(
71    "/dev/console",
72    major,
73    (rtems_device_minor_number) 0
74  );
75 
76  if (status != RTEMS_SUCCESSFUL)
77    rtems_fatal_error_occurred(status);
78 
79  atexit( console_cleanup );
80
81  return RTEMS_SUCCESSFUL;
82}
83
84
85/*  is_character_ready
86 *
87 *  This routine returns TRUE if a character is available.
88 *
89 *  Input parameters: NONE
90 *
91 *  Output parameters:  NONE
92 *
93 *  Return values:
94 */
95
96rtems_boolean is_character_ready(
97  char *ch
98)
99{
100  register rtems_unsigned8 status;
101
102  inport_byte( LSR1, status );
103
104  if ( Is_rx_ready( status ) ) {
105     inport_byte( RBR1,  status );
106     *ch = status;
107     return TRUE;
108  }
109  return FALSE;
110}
111
112/*  inbyte
113 *
114 *  This routine reads a character from the UART.
115 *
116 *  Input parameters: NONE
117 *
118 *  Output parameters:  NONE
119 *
120 *  Return values:
121 *    character read from UART
122 */
123
124char inbyte( void )
125{
126  register rtems_unsigned8 status; 
127  char                     ch;
128
129  do {
130    inport_byte( LSR1, status );
131  } while ( !( status & 0x01 ));/* s/b while ( !( Is_rx_ready( status ) ) ); */
132
133  inport_byte( RBR1, ch );
134
135  return ch;
136}
137
138/*  outbyte
139 *
140 *  This routine transmits a character out the port.  It supports
141 *  XON/XOFF flow control.
142 *
143 *  Input parameters:
144 *    ch  - character to be transmitted
145 *
146 *  Output parameters:  NONE
147 */
148
149void outbyte(
150  char ch
151)
152{
153  rtems_unsigned8 status;
154
155  do {
156    inport_byte( LSR1, status );
157  } while ( ! ( 0x40 & status ) ); /* ( Is_tx_ready( status ) ) );*/
158
159/*
160 *  GDB will NOT use XON/XOFF protocol
161 */
162
163
164#ifdef USE_XON
165 
166  while ( is_character_ready( &status ) == TRUE ) { 
167    if ( status == XOFF )
168      do {
169        while ( is_character_ready( &status ) == FALSE ) ;
170      } while ( status != XON );
171  }
172
173#endif
174
175  outport_byte( TBR1, ch );
176
177}
178
179/*
180 *  Open entry point
181 */
182 
183rtems_device_driver console_open(
184  rtems_device_major_number major,
185  rtems_device_minor_number minor,
186  void                    * arg
187)
188{
189  return RTEMS_SUCCESSFUL;
190}
191 
192/*
193 *  Close entry point
194 */
195 
196rtems_device_driver console_close(
197  rtems_device_major_number major,
198  rtems_device_minor_number minor,
199  void                    * arg
200)
201{
202  return RTEMS_SUCCESSFUL;
203}
204 
205/*
206 * read bytes from the serial port. We only have stdin.
207 */
208 
209rtems_device_driver console_read(
210  rtems_device_major_number major,
211  rtems_device_minor_number minor,
212  void                    * arg
213)
214{
215  rtems_libio_rw_args_t *rw_args;
216  char *buffer;
217  int maximum;
218  int count = 0;
219 
220  rw_args = (rtems_libio_rw_args_t *) arg;
221 
222  buffer = rw_args->buffer;
223  maximum = rw_args->count;
224 
225  for (count = 0; count < maximum; count++) {
226    buffer[ count ] = inbyte();
227    if (buffer[ count ] == '\n' || buffer[ count ] == '\r') {
228      buffer[ count++ ]  = '\n';
229      break;
230    }
231  }
232 
233  rw_args->bytes_moved = count;
234  return (count >= 0) ? RTEMS_SUCCESSFUL : RTEMS_UNSATISFIED;
235}
236
237/*
238 * write bytes to the serial port. Stdout and stderr are the same.
239 */
240 
241rtems_device_driver console_write(
242  rtems_device_major_number major,
243  rtems_device_minor_number minor,
244  void                    * arg
245)
246{
247  int count;
248  int maximum;
249  rtems_libio_rw_args_t *rw_args;
250  char *buffer;
251 
252  rw_args = (rtems_libio_rw_args_t *) arg;
253 
254  buffer = rw_args->buffer;
255  maximum = rw_args->count;
256 
257  for (count = 0; count < maximum; count++) {
258    if ( buffer[ count ] == '\n') {
259      outbyte('\r');
260    }
261    outbyte( buffer[ count ] );
262  }
263 
264  rw_args->bytes_moved = maximum;
265  return 0;
266}
267 
268/*
269 *  IO Control entry point
270 */
271 
272rtems_device_driver console_control(
273  rtems_device_major_number major,
274  rtems_device_minor_number minor,
275  void                    * arg
276)
277{
278  return RTEMS_SUCCESSFUL;
279}
Note: See TracBrowser for help on using the repository browser.