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

4.104.115
Last change on this file since 2daa19f was 2daa19f, checked in by Ralf Corsepius <ralf.corsepius@…>, on 04/12/10 at 16:34:31

Reflect termios API changes.

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