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

4.104.114.84.95
Last change on this file since df49c60 was df49c60, checked in by Joel Sherrill <joel.sherrill@…>, on Jun 12, 2000 at 3:00:15 PM

Merged from 4.5.0-beta3a

  • 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-1999.
23| *  On-Line Applications Research Corporation (OAR).
24| *
25| *  The license and distribution terms for this file may be
26| *  found in found in the file LICENSE in this distribution or at
27| *  http://www.OARcorp.com/rtems/license.html.
28| **************************************************************************
29|
30|  $Id$
31+--------------------------------------------------------------------------*/
32
33#include <stdio.h>
34#include <stdlib.h>
35#include <assert.h>
36#include <unistd.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 * Note:
54 *   1. Currently BSPPrintkPort, cannot be assigned to COM2,
55 *      it will be fixed soon.
56 *
57 *   2. If both BSPConsolePort and BSPPrintkport are assigned
58 *      to same serial device it does not work that great
59 */
60
61int BSPConsolePort = BSP_CONSOLE_PORT_CONSOLE;
62int BSPPrintkPort  = BSP_CONSOLE_PORT_CONSOLE;
63
64/* int BSPConsolePort = BSP_UART_COM2;  */
65int BSPBaseBaud    = 115200;
66
67extern BSP_polling_getchar_function_type BSP_poll_char;
68
69/*-------------------------------------------------------------------------+
70| External Prototypes
71+--------------------------------------------------------------------------*/
72extern void _IBMPC_keyboard_isr(void);
73extern rtems_boolean _IBMPC_scankey(char *);  /* defined in 'inch.c' */
74extern char BSP_wait_polled_input(void);
75extern void _IBMPC_initVideo(void);
76
77static int  conSetAttr(int minor, const struct termios *);
78static void isr_on(const rtems_irq_connect_data *);
79static void isr_off(const rtems_irq_connect_data *);
80static int  isr_is_on(const rtems_irq_connect_data *);
81
82
83static rtems_irq_connect_data console_isr_data = {BSP_KEYBOARD,
84                                                   _IBMPC_keyboard_isr,
85                                                   isr_on,
86                                                   isr_off,
87                                                   isr_is_on};
88
89static void
90isr_on(const rtems_irq_connect_data *unused)
91{
92  return;
93}
94                                                   
95static void
96isr_off(const rtems_irq_connect_data *unused)
97{
98  return;
99}
100
101static int
102isr_is_on(const rtems_irq_connect_data *irq)
103{
104  return BSP_irq_enabled_at_i8259s(irq->name);
105}
106
107void __assert (const char *file, int line, const char *msg)
108{
109    static   char exit_msg[] = "EXECUTIVE SHUTDOWN! Any key to reboot...";
110  unsigned char  ch;
111   
112  /*
113   * Note we cannot call exit or printf from here,
114   * assert can fail inside ISR too
115   */
116
117   /*
118   * Close console
119   */
120  close(2);
121  close(1);
122  close(0);
123
124  printk("\nassert failed: %s: ", file);
125  printk("%d: ", line);
126  printk("%s\n\n", msg);
127  printk(exit_msg);
128  ch = BSP_poll_char();
129  printk("\n\n");
130  rtemsReboot();
131
132}
133
134
135/*-------------------------------------------------------------------------+
136| Console device driver INITIALIZE entry point.
137+--------------------------------------------------------------------------+
138| Initilizes the I/O console (keyboard + VGA display) driver.
139+--------------------------------------------------------------------------*/
140rtems_device_driver
141console_initialize(rtems_device_major_number major,
142                   rtems_device_minor_number minor,
143                   void                      *arg)
144{
145  rtems_status_code status;
146
147  /*
148   *  The video was initialized in the start.s code and does not need
149   *  to be reinitialized.
150   */
151
152
153  if(BSPConsolePort == BSP_CONSOLE_PORT_CONSOLE)
154    {
155      /* Install keyboard interrupt handler */
156      status = BSP_install_rtems_irq_handler(&console_isr_data);
157 
158      if (!status)
159        {
160          printk("Error installing keyboard interrupt handler!\n");
161          rtems_fatal_error_occurred(status);
162        }
163     
164      status = rtems_io_register_name("/dev/console", major, 0);
165      if (status != RTEMS_SUCCESSFUL)
166        {
167          printk("Error registering console device!\n");
168          rtems_fatal_error_occurred(status);
169        }
170      printk("Initialized console on port CONSOLE\n\n");
171    }
172  else
173    {
174      /*
175       * Set up TERMIOS
176       */
177      rtems_termios_initialize ();
178     
179      /*
180       * Do device-specific initialization
181       */
182     
183      /* 9600-8-N-1 */
184      BSP_uart_init(BSPConsolePort, 9600, 0);
185     
186     
187      /* Set interrupt handler */
188      if(BSPConsolePort == BSP_UART_COM1)
189        {
190          console_isr_data.name = BSP_UART_COM1_IRQ;
191          console_isr_data.hdl  = BSP_uart_termios_isr_com1;
192         
193        }
194      else
195        {
196          assert(BSPConsolePort == BSP_UART_COM2);
197          console_isr_data.name = BSP_UART_COM2_IRQ;
198          console_isr_data.hdl  = BSP_uart_termios_isr_com2;
199        }
200
201      status = BSP_install_rtems_irq_handler(&console_isr_data);
202
203      if (!status){
204          printk("Error installing serial console interrupt handler!\n");
205          rtems_fatal_error_occurred(status);
206      }
207      /*
208       * Register the device
209       */
210      status = rtems_io_register_name ("/dev/console", major, 0);
211      if (status != RTEMS_SUCCESSFUL)
212        {
213          printk("Error registering console device!\n");
214          rtems_fatal_error_occurred (status);
215        }
216
217      if(BSPConsolePort == BSP_UART_COM1)
218        {
219          printk("Initialized console on port COM1 9600-8-N-1\n\n");
220        }
221      else
222        {
223          printk("Initialized console on port COM2 9600-8-N-1\n\n");
224        }
225
226      if(BSPPrintkPort == BSP_UART_COM1)
227        {
228          printk("Warning : This will be the last message on console\n");
229
230          /*
231           * FIXME: cast below defeats the very idea of having
232           * function pointer types defined
233           */
234          BSP_output_char = (BSP_output_char_function_type)
235                              BSP_output_char_via_serial;
236          BSP_poll_char   = (BSP_polling_getchar_function_type)
237                              BSP_poll_char_via_serial;
238        }
239      else if(BSPPrintkPort != BSP_CONSOLE_PORT_CONSOLE)
240        {
241           printk("illegal assignement of projtk channel");
242         rtems_fatal_error_occurred (status);
243        }
244
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.