source: rtems/c/src/lib/libbsp/i386/pc386/console/console.c @ 97d6366

4.104.114.84.95
Last change on this file since 97d6366 was 97d6366, checked in by Joel Sherrill <joel.sherrill@…>, on 01/20/99 at 21:59:09

Changed call to rtems_close() to close().

  • Property mode set to 100644
File size: 13.0 KB
Line 
1/*-------------------------------------------------------------------------+
2| console.c v1.1 - PC386 BSP - 1997/08/07
3+--------------------------------------------------------------------------+
4| This file contains the PC386 console I/O package.
5+--------------------------------------------------------------------------+
6| (C) Copyright 1997 -
7| - NavIST Group - Real-Time Distributed Systems and Industrial Automation
8|
9| http://pandora.ist.utl.pt
10|
11| Instituto Superior Tecnico * Lisboa * PORTUGAL
12+--------------------------------------------------------------------------+
13| Disclaimer:
14|
15| This file is provided "AS IS" without warranty of any kind, either
16| expressed or implied.
17+--------------------------------------------------------------------------+
18| This code is based on:
19|   console.c,v 1.4 1995/12/19 20:07:23 joel Exp - go32 BSP
20| With the following copyright notice:
21| **************************************************************************
22| *  COPYRIGHT (c) 1989-1998.
23| *  On-Line Applications Research Corporation (OAR).
24| *  Copyright assigned to U.S. Government, 1994.
25| *
26| *  The license and distribution terms for this file may be
27| *  found in found in the file LICENSE in this distribution or at
28| *  http://www.OARcorp.com/rtems/license.html.
29| **************************************************************************
30|
31|  $Id$
32+--------------------------------------------------------------------------*/
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <assert.h>
37#undef __assert
38void __assert (const char *file, int line, const char *msg);
39
40#include <bsp.h>
41#include <irq.h>
42#include <rtems/libio.h>
43#include <termios.h>
44#include <uart.h>
45#include <libcpu/cpuModel.h>
46
47/*
48 * Possible value for console input/output :
49 *      BSP_CONSOLE_PORT_CONSOLE
50 *      BSP_UART_COM1
51 *      BSP_UART_COM2
52 */
53
54/*
55 * Possible value for console input/output :
56 *      BSP_CONSOLE_PORT_CONSOLE
57 *      BSP_UART_COM1
58 *      BSP_UART_COM2
59 */
60
61int BSPConsolePort = BSP_CONSOLE_PORT_CONSOLE;
62
63/* int BSPConsolePort = BSP_UART_COM2;  */
64int BSPBaseBaud    = 115200;
65
66extern BSP_polling_getchar_function_type BSP_poll_char;
67
68/*-------------------------------------------------------------------------+
69| External Prototypes
70+--------------------------------------------------------------------------*/
71extern void _IBMPC_keyboard_isr(void);
72extern rtems_boolean _IBMPC_scankey(char *);  /* defined in 'inch.c' */
73extern char BSP_wait_polled_input(void);
74extern void _IBMPC_initVideo(void);
75
76static int  conSetAttr(int minor, const struct termios *);
77static void isr_on(const rtems_irq_connect_data *);
78static void isr_off(const rtems_irq_connect_data *);
79static int  isr_is_on(const rtems_irq_connect_data *);
80
81
82static rtems_irq_connect_data console_isr_data = {BSP_KEYBOARD,
83                                                   _IBMPC_keyboard_isr,
84                                                   isr_on,
85                                                   isr_off,
86                                                   isr_is_on};
87
88static void
89isr_on(const rtems_irq_connect_data *unused)
90{
91  return;
92}
93                                                   
94static void
95isr_off(const rtems_irq_connect_data *unused)
96{
97  return;
98}
99
100static int
101isr_is_on(const rtems_irq_connect_data *irq)
102{
103  return BSP_irq_enabled_at_i8259s(irq->name);
104}
105
106void console_reserve_resources(rtems_configuration_table *conf)
107{
108    if(BSPConsolePort != BSP_CONSOLE_PORT_CONSOLE)
109    {
110      rtems_termios_reserve_resources(conf, 1);
111    }
112   
113  return;
114}
115
116void __assert (const char *file, int line, const char *msg)
117{
118    static   char exit_msg[] = "EXECUTIVE SHUTDOWN! Any key to reboot...";
119  unsigned char  ch;
120   
121  /*
122   * Note we cannot call exit or printf from here,
123   * assert can fail inside ISR too
124   */
125
126   /*
127   * Close console
128   */
129  close(2);
130  close(1);
131  close(0);
132
133  printk("\nassert failed: %s: ", file);
134  printk("%d: ", line);
135  printk("%s\n\n", msg);
136  printk(exit_msg);
137  ch = BSP_poll_char();
138  printk("\n\n");
139  rtemsReboot();
140
141}
142
143
144/*-------------------------------------------------------------------------+
145| Console device driver INITIALIZE entry point.
146+--------------------------------------------------------------------------+
147| Initilizes the I/O console (keyboard + VGA display) driver.
148+--------------------------------------------------------------------------*/
149rtems_device_driver
150console_initialize(rtems_device_major_number major,
151                   rtems_device_minor_number minor,
152                   void                      *arg)
153{
154  rtems_status_code status;
155
156  /*
157   *  The video was initialized in the start.s code and does not need
158   *  to be reinitialized.
159   */
160
161
162  if(BSPConsolePort == BSP_CONSOLE_PORT_CONSOLE)
163    {
164      /* Install keyboard interrupt handler */
165      status = BSP_install_rtems_irq_handler(&console_isr_data);
166 
167      if (!status)
168        {
169          printk("Error installing keyboard interrupt handler!\n");
170          rtems_fatal_error_occurred(status);
171        }
172     
173      status = rtems_io_register_name("/dev/console", major, 0);
174      if (status != RTEMS_SUCCESSFUL)
175        {
176          printk("Error registering console device!\n");
177          rtems_fatal_error_occurred(status);
178        }
179      printk("Initialized console on port CONSOLE\n\n");
180    }
181  else
182    {
183      /*
184       * Set up TERMIOS
185       */
186      rtems_termios_initialize ();
187     
188      /*
189       * Do device-specific initialization
190       */
191     
192      /* 9600-8-N-1 */
193      BSP_uart_init(BSPConsolePort, 9600, 0);
194     
195     
196      /* Set interrupt handler */
197      if(BSPConsolePort == BSP_UART_COM1)
198        {
199          console_isr_data.name = BSP_UART_COM1_IRQ;
200          console_isr_data.hdl  = BSP_uart_termios_isr_com1;
201         
202        }
203      else
204        {
205          assert(BSPConsolePort == BSP_UART_COM2);
206          console_isr_data.name = BSP_UART_COM2_IRQ;
207          console_isr_data.hdl  = BSP_uart_termios_isr_com2;
208        }
209
210      status = BSP_install_rtems_irq_handler(&console_isr_data);
211
212      if (!status){
213          printk("Error installing serial console interrupt handler!\n");
214          rtems_fatal_error_occurred(status);
215      }
216      /*
217       * Register the device
218       */
219      status = rtems_io_register_name ("/dev/console", major, 0);
220      if (status != RTEMS_SUCCESSFUL)
221        {
222          printk("Error registering console device!\n");
223          rtems_fatal_error_occurred (status);
224        }
225
226      if(BSPConsolePort == BSP_UART_COM1)
227        {
228          printk("Initialized console on port COM1 9600-8-N-1\n\n");
229        }
230      else
231        {
232          printk("Initialized console on port COM2 9600-8-N-1\n\n");
233        }
234#define  PRINTK_ON_SERIAL
235#ifdef PRINTK_ON_SERIAL
236      /*
237       * You can remove the follwoing tree lines if you want to have printk
238       * using the video console for output while printf use serial line.
239       * This may be convenient to debug the serial line driver itself...
240       */
241      /*      printk("Warning : This will be the last message displayed on console\n");*/
242      BSP_output_char = (BSP_output_char_function_type) BSP_output_char_via_serial;
243      BSP_poll_char   = (BSP_polling_getchar_function_type) BSP_poll_char_via_serial;
244#endif 
245    }
246  return RTEMS_SUCCESSFUL;
247} /* console_initialize */
248
249
250static int console_open_count = 0;
251
252static int console_last_close(int major, int minor, void *arg)
253{
254  BSP_remove_rtems_irq_handler (&console_isr_data);
255
256  return 0;
257}
258
259/*-------------------------------------------------------------------------+
260| Console device driver OPEN entry point
261+--------------------------------------------------------------------------*/
262rtems_device_driver
263console_open(rtems_device_major_number major,
264                rtems_device_minor_number minor,
265                void                      *arg)
266{
267  rtems_status_code              status;
268  static rtems_termios_callbacks cb =
269  {
270    NULL,                     /* firstOpen */
271    console_last_close,       /* lastClose */
272    NULL,                     /* pollRead */
273    BSP_uart_termios_write_com1, /* write */
274    conSetAttr,               /* setAttributes */
275    NULL,                     /* stopRemoteTx */
276    NULL,                     /* startRemoteTx */
277    1                         /* outputUsesInterrupts */
278  };
279
280  if(BSPConsolePort == BSP_CONSOLE_PORT_CONSOLE)
281    {
282      ++console_open_count;
283      return RTEMS_SUCCESSFUL;
284    }
285
286  if(BSPConsolePort == BSP_UART_COM2)
287    {
288      cb.write = BSP_uart_termios_write_com2;
289    }
290
291  status = rtems_termios_open (major, minor, arg, &cb);
292
293  if(status != RTEMS_SUCCESSFUL)
294    {
295      printk("Error openning console device\n");
296      return status;
297    }
298
299  /*
300   * Pass data area info down to driver
301   */
302  BSP_uart_termios_set(BSPConsolePort,
303                         ((rtems_libio_open_close_args_t *)arg)->iop->data1);
304 
305  /* Enable interrupts  on channel */
306  BSP_uart_intr_ctrl(BSPConsolePort, BSP_UART_INTR_CTRL_TERMIOS);
307
308  return RTEMS_SUCCESSFUL;
309}
310
311/*-------------------------------------------------------------------------+
312| Console device driver CLOSE entry point
313+--------------------------------------------------------------------------*/
314rtems_device_driver
315console_close(rtems_device_major_number major,
316              rtems_device_minor_number minor,
317              void                      *arg)
318{
319  rtems_device_driver res = RTEMS_SUCCESSFUL;
320
321  if(BSPConsolePort != BSP_CONSOLE_PORT_CONSOLE)
322    {
323      res =  rtems_termios_close (arg);
324    }
325  else {
326    if (--console_open_count == 0) {
327      console_last_close(major, minor, arg);
328    }
329  }
330 
331  return res;
332} /* console_close */
333
334 
335/*-------------------------------------------------------------------------+
336| Console device driver READ entry point.
337+--------------------------------------------------------------------------+
338| Read characters from the I/O console. We only have stdin.
339+--------------------------------------------------------------------------*/
340rtems_device_driver
341console_read(rtems_device_major_number major,
342             rtems_device_minor_number minor,
343             void                      *arg)
344{
345  rtems_libio_rw_args_t *rw_args = (rtems_libio_rw_args_t *)arg;
346  char                  *buffer  = rw_args->buffer;
347  int            count, maximum  = rw_args->count;
348
349  if(BSPConsolePort != BSP_CONSOLE_PORT_CONSOLE)
350    {
351      return rtems_termios_read (arg);
352    }
353 
354  for (count = 0; count < maximum; count++)
355  {
356    /* Get character */
357    buffer[count] = _IBMPC_inch_sleep();
358
359    /* Echo character to screen */
360    _IBMPC_outch(buffer[count]);
361    if (buffer[count] == '\r')
362      {
363        _IBMPC_outch('\n');  /* CR = CR + LF */
364      }
365
366    if (buffer[count] == '\n' || buffer[count] == '\r')
367    {
368      /* What if this goes past the end of the buffer?  We're hosed. [bhc] */
369      buffer[count++]  = '\n';
370      buffer[count]    = '\0';
371      break;
372    }
373  }
374 
375  rw_args->bytes_moved = count;
376  return ((count >= 0) ? RTEMS_SUCCESSFUL : RTEMS_UNSATISFIED);
377} /* console_read */
378 
379
380/*-------------------------------------------------------------------------+
381| Console device driver WRITE entry point.
382+--------------------------------------------------------------------------+
383| Write characters to the I/O console. Stderr and stdout are the same.
384+--------------------------------------------------------------------------*/
385rtems_device_driver
386console_write(rtems_device_major_number major,
387              rtems_device_minor_number minor,
388              void                    * arg)
389{
390  rtems_libio_rw_args_t *rw_args = (rtems_libio_rw_args_t *)arg;
391  char                  *buffer  = rw_args->buffer;
392  int            count, maximum  = rw_args->count;
393
394  if(BSPConsolePort != BSP_CONSOLE_PORT_CONSOLE)
395    {
396      return rtems_termios_write (arg);
397    }
398 
399  for (count = 0; count < maximum; count++)
400  {
401    _IBMPC_outch(buffer[count]);
402    if (buffer[count] == '\n')
403      _IBMPC_outch('\r');            /* LF = LF + CR */
404  }
405
406  rw_args->bytes_moved = maximum;
407  return RTEMS_SUCCESSFUL;
408} /* console_write */
409
410
411 
412/*
413 * Handle ioctl request.
414 */
415rtems_device_driver
416console_control(rtems_device_major_number major,
417                rtems_device_minor_number minor,
418                void                      * arg
419)
420{
421  if(BSPConsolePort != BSP_CONSOLE_PORT_CONSOLE)
422    {
423      return rtems_termios_ioctl (arg);
424    }
425
426  return RTEMS_SUCCESSFUL;
427}
428
429static int
430conSetAttr(int minor, const struct termios *t)
431{
432  int baud;
433
434  switch (t->c_cflag & CBAUD)
435    {
436    case B50:   
437      baud = 50;
438      break;
439    case B75:   
440      baud = 75;       
441      break;
442    case B110: 
443      baud = 110;       
444      break;
445    case B134: 
446      baud = 134;       
447      break;
448    case B150: 
449      baud = 150;       
450      break;
451    case B200:
452      baud = 200;       
453      break;
454    case B300: 
455      baud = 300;
456      break;
457    case B600: 
458      baud = 600;       
459      break;
460    case B1200:
461      baud = 1200;
462      break;
463    case B1800:
464      baud = 1800;     
465      break;
466    case B2400:
467      baud = 2400;
468      break;
469    case B4800:
470      baud = 4800;
471      break;
472    case B9600:
473      baud = 9600;
474      break;
475    case B19200:
476      baud = 19200;
477      break;
478    case B38400:
479      baud = 38400;
480      break;
481    case B57600:       
482      baud = 57600;
483      break;
484    case B115200:
485      baud = 115200;
486      break;
487    default:
488      baud = 0;
489      rtems_fatal_error_occurred (RTEMS_INTERNAL_ERROR);
490      return 0;
491    }
492
493  BSP_uart_set_baud(BSPConsolePort, baud);
494
495  return 0;
496}
497
498/*
499 * BSP initialization
500 */
501
502BSP_output_char_function_type BSP_output_char =
503                       (BSP_output_char_function_type) _IBMPC_outch;
504
505BSP_polling_getchar_function_type BSP_poll_char = BSP_wait_polled_input;
506
507
Note: See TracBrowser for help on using the repository browser.