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

4.104.114.84.9
Last change on this file since 6daada6 was 6daada6, checked in by Jennifer Averett <Jennifer.Averett@…>, on May 6, 2005 at 7:53:03 PM

2005-05-06 Jennifer Averett <jennifer.averett@…>

  • 3c509/3c509.c, clock/ckinit.c, console/console.c, console/fb_vga.c, console/inch.c, console/ps2_mouse.c, console/serial_mouse.c, ne2000/ne2000.c, timer/timer.c, wd8003/wd8003.c: Moved irq.h to bsp subdirectory.
  • Property mode set to 100644
File size: 14.8 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.rtems.com/license/LICENSE.
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 <bsp/irq.h>
42#include <rtems/libio.h>
43#include <termios.h>
44#include <uart.h>
45#include <libcpu/cpuModel.h>
46
47#include <rtems/mw_uid.h>
48#include "mouse_parser.h"
49
50/*
51 * Possible value for console input/output :
52 *      BSP_CONSOLE_PORT_CONSOLE
53 *      BSP_UART_COM1
54 *      BSP_UART_COM2
55 *
56 * Note:
57 *   1. Currently BSPPrintkPort, cannot be assigned to COM2,
58 *      it will be fixed soon.
59 *
60 *   2. If both BSPConsolePort and BSPPrintkport are assigned
61 *      to same serial device it does not work that great
62 */
63
64int BSPConsolePort = BSP_CONSOLE_PORT_CONSOLE;
65int BSPPrintkPort  = BSP_CONSOLE_PORT_CONSOLE;
66
67/* int BSPConsolePort = BSP_UART_COM2;  */
68int BSPBaseBaud    = 115200;
69
70extern BSP_polling_getchar_function_type BSP_poll_char;
71extern int getch( void );
72extern void kbd_init( void );
73
74/*-------------------------------------------------------------------------+
75| External Prototypes
76+--------------------------------------------------------------------------*/
77extern void keyboard_interrupt(void );
78extern void keyboard_interrupt_wrapper(void *);
79extern char BSP_wait_polled_input(void);
80extern void _IBMPC_initVideo(void);
81
82static int  conSetAttr(int minor, const struct termios *);
83static void isr_on(const rtems_irq_connect_data *);
84static void isr_off(const rtems_irq_connect_data *);
85static int  isr_is_on(const rtems_irq_connect_data *);
86
87extern int rtems_kbpoll( void );
88
89static rtems_irq_connect_data console_isr_data = {BSP_KEYBOARD,
90                                                  keyboard_interrupt_wrapper,
91                                                  0,
92                                                  isr_on,
93                                                  isr_off,
94                                                  isr_is_on};
95
96static void
97isr_on(const rtems_irq_connect_data *unused)
98{
99  return;
100}
101
102static void
103isr_off(const rtems_irq_connect_data *unused)
104{
105  return;
106}
107
108static int
109isr_is_on(const rtems_irq_connect_data *irq)
110{
111  return BSP_irq_enabled_at_i8259s(irq->name);
112}
113
114extern char _IBMPC_inch(void);
115extern int  rtems_kbpoll( void );
116
117static int
118ibmpc_console_write(int minor, const char *buf, int len)
119{
120  int count;
121  for (count = 0; count < len; count++)
122  {
123    _IBMPC_outch( buf[ count ] );
124    if( buf[ count ] == '\n')
125      _IBMPC_outch( '\r' );            /* LF = LF + CR */
126  }
127  return 0;
128}
129
130int kbd_poll_read( int minor )
131{
132  if( rtems_kbpoll() )
133  {
134     int c = getch();
135     return c;
136  }
137  return -1;
138}
139
140/*
141static void*         termios_ttyp_console         = NULL;
142void enq_key( char key )
143{
144  if( termios_ttyp_console )
145  {
146          rtems_termios_enqueue_raw_characters(termios_ttyp_console, &key,1 );
147  }
148}
149*/
150
151void __assert (const char *file, int line, const char *msg)
152{
153    static   char exit_msg[] = "EXECUTIVE SHUTDOWN! Any key to reboot...";
154  unsigned char  ch;
155
156  /*
157   * Note we cannot call exit or printf from here,
158   * assert can fail inside ISR too
159   */
160
161   /*
162   * Close console
163   */
164  close(2);
165  close(1);
166  close(0);
167
168  printk("\nassert failed: %s: ", file);
169  printk("%d: ", line);
170  printk("%s\n\n", msg);
171  printk(exit_msg);
172  ch = BSP_poll_char();
173  printk("\n\n");
174  rtemsReboot();
175
176}
177
178/*-------------------------------------------------------------------------+
179| Console device driver INITIALIZE entry point.
180+--------------------------------------------------------------------------+
181| Initilizes the I/O console (keyboard + VGA display) driver.
182+--------------------------------------------------------------------------*/
183rtems_device_driver
184console_initialize(rtems_device_major_number major,
185                   rtems_device_minor_number minor,
186                   void                      *arg)
187{
188  rtems_status_code status;
189
190  /* Initialize the KBD interface */
191  kbd_init();
192
193  /*
194   * Set up TERMIOS
195   */
196  rtems_termios_initialize ();
197
198#ifdef RTEMS_RUNTIME_CONSOLE_SELECT
199  /*
200   * If no video card, fall back to serial port console
201   */
202#include <crt.h>
203  if((BSPConsolePort == BSP_CONSOLE_PORT_CONSOLE)
204   && (*(unsigned char*) NB_MAX_ROW_ADDR == 0)
205   && (*(unsigned short*)NB_MAX_COL_ADDR == 0)) {
206    BSPConsolePort = BSP_UART_COM2;
207    BSPPrintkPort  = BSP_UART_COM1;
208  }
209#endif
210
211  /*
212   *  The video was initialized in the start.s code and does not need
213   *  to be reinitialized.
214   */
215
216  if(BSPConsolePort == BSP_CONSOLE_PORT_CONSOLE)
217    {
218      /* Install keyboard interrupt handler */
219      status = BSP_install_rtems_irq_handler(&console_isr_data);
220
221    if (!status)
222        {
223          printk("Error installing keyboard interrupt handler!\n");
224          rtems_fatal_error_occurred(status);
225        }
226
227      status = rtems_io_register_name("/dev/console", major, 0);
228      if (status != RTEMS_SUCCESSFUL)
229        {
230          printk("Error registering console device!\n");
231          rtems_fatal_error_occurred(status);
232        }
233      printk("Initialized console on port CONSOLE\n\n");
234    }
235  else
236    {
237      /*
238       * Do device-specific initialization
239       */
240      /* 9600-8-N-1 */
241      BSP_uart_init(BSPConsolePort, 9600, CHR_8_BITS, 0, 0, 0);
242
243      /* Set interrupt handler */
244      if(BSPConsolePort == BSP_UART_COM1)
245        {
246             console_isr_data.name = BSP_UART_COM1_IRQ;
247        console_isr_data.hdl  = BSP_uart_termios_isr_com1;
248
249        }
250      else
251           {
252          assert(BSPConsolePort == BSP_UART_COM2);
253          console_isr_data.name = BSP_UART_COM2_IRQ;
254          console_isr_data.hdl  = BSP_uart_termios_isr_com2;
255        }
256      status = BSP_install_rtems_irq_handler(&console_isr_data);
257
258      if (!status){
259          printk("Error installing serial console interrupt handler!\n");
260          rtems_fatal_error_occurred(status);
261      }
262      /*
263       * Register the device
264       */
265      status = rtems_io_register_name ("/dev/console", major, 0);
266      if (status != RTEMS_SUCCESSFUL)
267        {
268          printk("Error registering console device!\n");
269          rtems_fatal_error_occurred (status);
270        }
271
272      if(BSPConsolePort == BSP_UART_COM1)
273        {
274          printk("Initialized console on port COM1 9600-8-N-1\n\n");
275        }
276      else
277        {
278          printk("Initialized console on port COM2 9600-8-N-1\n\n");
279        }
280
281      if(BSPPrintkPort == BSP_UART_COM1)
282        {
283          printk("Warning : This will be the last message on console\n");
284
285          /*
286           * FIXME: cast below defeats the very idea of having
287           * function pointer types defined
288           */
289          BSP_output_char = (BSP_output_char_function_type)
290                              BSP_output_char_via_serial;
291          BSP_poll_char   = (BSP_polling_getchar_function_type)
292                              BSP_poll_char_via_serial;
293        }
294      else if(BSPPrintkPort != BSP_CONSOLE_PORT_CONSOLE)
295        {
296           printk("illegal assignement of printk channel");
297         rtems_fatal_error_occurred (status);
298        }
299
300    }
301  return RTEMS_SUCCESSFUL;
302} /* console_initialize */
303
304static int console_open_count = 0;
305
306static int console_last_close(int major, int minor, void *arg)
307{
308  BSP_remove_rtems_irq_handler (&console_isr_data);
309
310  return 0;
311}
312
313/*-------------------------------------------------------------------------+
314| Console device driver OPEN entry point
315+--------------------------------------------------------------------------*/
316rtems_device_driver
317console_open(rtems_device_major_number major,
318                rtems_device_minor_number minor,
319                void                      *arg)
320{
321  rtems_status_code              status;
322  static rtems_termios_callbacks cb =
323  {
324    NULL,                     /* firstOpen */
325    console_last_close,       /* lastClose */
326    NULL,          /* pollRead */
327    BSP_uart_termios_write_com1, /* write */
328    conSetAttr,               /* setAttributes */
329    NULL,                     /* stopRemoteTx */
330    NULL,                     /* startRemoteTx */
331    1                         /* outputUsesInterrupts */
332  };
333
334  if(BSPConsolePort == BSP_CONSOLE_PORT_CONSOLE)
335    {
336
337      /* Let's set the routines for termios to poll the
338       * Kbd queue for data
339       */
340      cb.pollRead = kbd_poll_read;
341      cb.outputUsesInterrupts = 0;
342      /* write the "echo" if it is on */
343      cb.write = ibmpc_console_write;
344
345      cb.setAttributes = NULL;
346      ++console_open_count;
347      status = rtems_termios_open (major, minor, arg, &cb);
348      if(status != RTEMS_SUCCESSFUL)
349      {
350         printk("Error openning console device\n");
351      }
352      return status;
353    }
354
355  if(BSPConsolePort == BSP_UART_COM2)
356    {
357      cb.write = BSP_uart_termios_write_com2;
358    }
359
360  status = rtems_termios_open (major, minor, arg, &cb);
361
362  if(status != RTEMS_SUCCESSFUL)
363    {
364      printk("Error openning console device\n");
365      return status;
366    }
367
368  /*
369   * Pass data area info down to driver
370   */
371  BSP_uart_termios_set(BSPConsolePort,
372                         ((rtems_libio_open_close_args_t *)arg)->iop->data1);
373
374  /* Enable interrupts  on channel */
375  BSP_uart_intr_ctrl(BSPConsolePort, BSP_UART_INTR_CTRL_TERMIOS);
376
377  return RTEMS_SUCCESSFUL;
378}
379
380/*-------------------------------------------------------------------------+
381| Console device driver CLOSE entry point
382+--------------------------------------------------------------------------*/
383rtems_device_driver
384console_close(rtems_device_major_number major,
385              rtems_device_minor_number minor,
386              void                      *arg)
387{
388   return rtems_termios_close (arg);
389} /* console_close */
390
391/*-------------------------------------------------------------------------+
392| Console device driver READ entry point.
393+--------------------------------------------------------------------------+
394| Read characters from the I/O console. We only have stdin.
395+--------------------------------------------------------------------------*/
396rtems_device_driver
397console_read(rtems_device_major_number major,
398             rtems_device_minor_number minor,
399             void                      *arg)
400{
401 return rtems_termios_read( arg );
402} /* console_read */
403
404/*-------------------------------------------------------------------------+
405| Console device driver WRITE entry point.
406+--------------------------------------------------------------------------+
407| Write characters to the I/O console. Stderr and stdout are the same.
408+--------------------------------------------------------------------------*/
409rtems_device_driver
410console_write(rtems_device_major_number major,
411              rtems_device_minor_number minor,
412              void                    * arg)
413{
414  rtems_libio_rw_args_t *rw_args = (rtems_libio_rw_args_t *)arg;
415  char                  *buffer  = rw_args->buffer;
416  int                    maximum  = rw_args->count;
417
418  if(BSPConsolePort != BSP_CONSOLE_PORT_CONSOLE)
419    {
420      return rtems_termios_write (arg);
421    }
422
423  /* write data to VGA */
424  ibmpc_console_write( minor, buffer, maximum );
425  rw_args->bytes_moved = maximum;
426  return RTEMS_SUCCESSFUL;
427} /* console_write */
428
429extern int vt_ioctl( unsigned int cmd, unsigned long arg);
430
431/*
432 * Handle ioctl request.
433 */
434rtems_device_driver
435console_control(rtems_device_major_number major,
436                rtems_device_minor_number minor,
437                void                      * arg
438)
439{
440        rtems_libio_ioctl_args_t *args = arg;
441        switch (args->command)
442        {
443           default:
444      if( vt_ioctl( args->command, (unsigned long)args->buffer ) != 0 )
445          return rtems_termios_ioctl (arg);
446                break;
447
448      case MW_UID_REGISTER_DEVICE:
449      printk( "SerialMouse: reg=%s\n", args->buffer );
450      register_kbd_msg_queue( args->buffer, 0 );
451                break;
452
453      case MW_UID_UNREGISTER_DEVICE:
454      unregister_kbd_msg_queue( 0 );
455                break;
456   }
457        args->ioctl_return = 0;
458   return RTEMS_SUCCESSFUL;
459}
460
461static int
462conSetAttr(int minor, const struct termios *t)
463{
464  unsigned long baud, databits, parity, stopbits;
465
466  switch (t->c_cflag & CBAUD)
467    {
468    case B50:
469      baud = 50;
470      break;
471    case B75:
472      baud = 75;
473      break;
474    case B110:
475      baud = 110;
476      break;
477    case B134:
478      baud = 134;
479      break;
480    case B150:
481      baud = 150;
482      break;
483    case B200:
484      baud = 200;
485      break;
486    case B300:
487      baud = 300;
488      break;
489    case B600:
490      baud = 600;
491      break;
492    case B1200:
493      baud = 1200;
494      break;
495    case B1800:
496      baud = 1800;
497      break;
498    case B2400:
499      baud = 2400;
500      break;
501    case B4800:
502      baud = 4800;
503      break;
504    case B9600:
505      baud = 9600;
506      break;
507    case B19200:
508      baud = 19200;
509      break;
510    case B38400:
511      baud = 38400;
512      break;
513    case B57600:
514      baud = 57600;
515      break;
516    case B115200:
517      baud = 115200;
518      break;
519    default:
520      rtems_fatal_error_occurred (RTEMS_INTERNAL_ERROR);
521      return 0;
522    }
523
524  if (t->c_cflag & PARENB) {
525    /* Parity is enabled */
526    if (t->c_cflag & PARODD) {
527      /* Parity is odd */
528      parity = PEN;
529    }
530    else {
531      /* Parity is even */
532      parity = PEN | EPS;
533    }
534  }
535  else {
536    /* No parity */
537    parity = 0;
538  }
539
540  switch (t->c_cflag & CSIZE) {
541    case CS5: databits = CHR_5_BITS; break;
542    case CS6: databits = CHR_6_BITS; break;
543    case CS7: databits = CHR_7_BITS; break;
544    default: /* just to avoid warnings -- all cases are covered. */
545    case CS8: databits = CHR_8_BITS; break;
546   }
547
548  if (t->c_cflag & CSTOPB) {
549    /* 2 stop bits */
550    stopbits = STB;
551  }
552  else {
553    /* 1 stop bit */
554    stopbits = 0;
555  }
556
557  BSP_uart_set_attributes(BSPConsolePort, baud, databits, parity, stopbits);
558
559  return 0;
560}
561
562void keyboard_interrupt_wrapper(void *unused){
563  keyboard_interrupt();
564}
565
566/*
567 * BSP initialization
568 */
569
570BSP_output_char_function_type BSP_output_char =
571                       (BSP_output_char_function_type) _IBMPC_outch;
572
573BSP_polling_getchar_function_type BSP_poll_char = BSP_wait_polled_input;
Note: See TracBrowser for help on using the repository browser.