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

4.104.11
Last change on this file since 2daa19f was 2daa19f, checked in by Ralf Corsepius <ralf.corsepius@…>, on Apr 12, 2010 at 4:34:31 PM

Reflect termios API changes.

  • Property mode set to 100644
File size: 14.7 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
38#include <bsp.h>
39#include <bsp/irq.h>
40#include <rtems/libio.h>
41#include <termios.h>
42#include <rtems/termiostypes.h>
43#include <uart.h>
44#include <libcpu/cpuModel.h>
45
46#include <rtems/mw_uid.h>
47#include "mouse_parser.h"
48
49/*
50 * Possible value for console input/output :
51 *      BSP_CONSOLE_PORT_CONSOLE
52 *      BSP_UART_COM1
53 *      BSP_UART_COM2
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
61 */
62
63#if (USE_COM1_AS_CONSOLE == 1)
64int BSPConsolePort = BSP_UART_COM1;
65int BSPPrintkPort  = BSP_UART_COM1;
66#else
67int BSPConsolePort = BSP_CONSOLE_PORT_CONSOLE;
68int BSPPrintkPort  = BSP_CONSOLE_PORT_CONSOLE;
69#endif
70
71int BSPBaseBaud    = 115200;
72
73extern BSP_polling_getchar_function_type BSP_poll_char;
74extern int getch( void );
75extern void kbd_init( void );
76
77/*-------------------------------------------------------------------------+
78| External Prototypes
79+--------------------------------------------------------------------------*/
80extern void keyboard_interrupt(void );
81extern void keyboard_interrupt_wrapper(void *);
82extern int BSP_wait_polled_input(void);
83extern void _IBMPC_initVideo(void);
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
90extern int rtems_kbpoll( void );
91
92static rtems_irq_connect_data console_isr_data = {BSP_KEYBOARD,
93                                                  keyboard_interrupt_wrapper,
94                                                  0,
95                                                  isr_on,
96                                                  isr_off,
97                                                  isr_is_on};
98
99static void
100isr_on(const rtems_irq_connect_data *unused)
101{
102  return;
103}
104
105static void
106isr_off(const rtems_irq_connect_data *unused)
107{
108  return;
109}
110
111static int
112isr_is_on(const rtems_irq_connect_data *irq)
113{
114  return BSP_irq_enabled_at_i8259s(irq->name);
115}
116
117extern int  rtems_kbpoll( void );
118
119static ssize_t
120ibmpc_console_write(int minor, const char *buf, size_t len)
121{
122  size_t count;
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  }
129  return count;
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
142/* provide default that does nothing */
143extern void
144BSP_runtime_console_select(int *, int *) __attribute__((weak));
145
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
152BSP_console_select(void)
153{
154  const char* mode;
155
156  /*
157   * Check the command line for the type of mode
158   * the console is.
159   */
160  mode = bsp_cmdline_arg ("--console=");
161
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  }
181
182  if ( BSP_runtime_console_select )
183    BSP_runtime_console_select(&BSPPrintkPort, &BSPConsolePort);
184
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
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;
231
232
233  /* Initialize the KBD interface */
234  kbd_init();
235
236  /*
237   * Set up TERMIOS
238   */
239  rtems_termios_initialize ();
240
241  /*
242   *  The video was initialized in the start.s code and does not need
243   *  to be reinitialized.
244   */
245
246  if(BSPConsolePort == BSP_CONSOLE_PORT_CONSOLE)
247    {
248      /* Install keyboard interrupt handler */
249      status = BSP_install_rtems_irq_handler(&console_isr_data);
250
251    if (!status)
252        {
253          printk("Error installing keyboard interrupt handler!\n");
254          rtems_fatal_error_occurred(status);
255        }
256
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 */
271      BSP_uart_init(BSPConsolePort, 9600, CHR_8_BITS, 0, 0, 0);
272
273      /* Set interrupt handler */
274      if(BSPConsolePort == BSP_UART_COM1)
275        {
276             console_isr_data.name = BSP_UART_COM1_IRQ;
277        console_isr_data.hdl  = BSP_uart_termios_isr_com1;
278
279        }
280      else
281           {
282          assert(BSPConsolePort == BSP_UART_COM2);
283          console_isr_data.name = BSP_UART_COM2_IRQ;
284          console_isr_data.hdl  = BSP_uart_termios_isr_com2;
285        }
286      status = BSP_install_rtems_irq_handler(&console_isr_data);
287
288      if (!status){
289          printk("Error installing serial console interrupt handler!\n");
290          rtems_fatal_error_occurred(status);
291      }
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
302      if(BSPConsolePort == BSP_UART_COM1)
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        }
310  }
311
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);
316    }
317
318  return RTEMS_SUCCESSFUL;
319} /* console_initialize */
320
321static int console_open_count = 0;
322
323static int console_last_close(int major, int minor, void *arg)
324{
325  BSP_remove_rtems_irq_handler (&console_isr_data);
326
327  return 0;
328}
329
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
344/*-------------------------------------------------------------------------+
345| Console device driver OPEN entry point
346+--------------------------------------------------------------------------*/
347rtems_device_driver
348console_open(rtems_device_major_number major,
349                rtems_device_minor_number minor,
350                void                      *arg)
351{
352  rtems_status_code              status;
353  static rtems_termios_callbacks cb =
354  {
355    NULL,                     /* firstOpen */
356    console_last_close,       /* lastClose */
357    NULL,          /* pollRead */
358    BSP_uart_termios_write_com1, /* write */
359    conSetAttr,               /* setAttributes */
360    NULL,                     /* stopRemoteTx */
361    NULL,                     /* startRemoteTx */
362    1                         /* outputUsesInterrupts */
363  };
364
365  if(BSPConsolePort == BSP_CONSOLE_PORT_CONSOLE)
366    {
367
368      /* Let's set the routines for termios to poll the
369       * Kbd queue for data
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;
377      ++console_open_count;
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;
384    }
385
386  if(BSPConsolePort == BSP_UART_COM2)
387    {
388      cb.write = BSP_uart_termios_write_com2;
389    }
390
391  cb.firstOpen = ser_console_first_open;
392
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}
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{
412   return rtems_termios_close (arg);
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{
425 return rtems_termios_read( arg );
426} /* console_read */
427
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;
440  int                    maximum  = rw_args->count;
441
442  if(BSPConsolePort != BSP_CONSOLE_PORT_CONSOLE)
443    {
444      return rtems_termios_write (arg);
445    }
446
447  /* write data to VGA */
448  ibmpc_console_write( minor, buffer, maximum );
449  rw_args->bytes_moved = maximum;
450  return RTEMS_SUCCESSFUL;
451} /* console_write */
452
453extern int vt_ioctl( unsigned int cmd, unsigned long arg);
454
455/*
456 * Handle ioctl request.
457 */
458rtems_device_driver
459console_control(rtems_device_major_number major,
460                rtems_device_minor_number minor,
461                void                      * arg
462)
463{
464        rtems_libio_ioctl_args_t *args = arg;
465        switch (args->command)
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;
483}
484
485static int
486conSetAttr(int minor, const struct termios *t)
487{
488  unsigned long baud, databits, parity, stopbits;
489
490  baud = rtems_termios_baud_to_number(t->c_cflag & CBAUD);
491  if ( baud > 115200 )
492    rtems_fatal_error_occurred (RTEMS_INTERNAL_ERROR);
493
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  }
509
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;
514    default: /* just to avoid warnings -- all cases are covered. */
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);
528
529  return 0;
530}
531
532void keyboard_interrupt_wrapper(void *unused){
533  keyboard_interrupt();
534}
535
536/*
537 * BSP initialization
538 */
539
540BSP_output_char_function_type BSP_output_char =
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.