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

4.104.114.84.95
Last change on this file since f05b2ac was f05b2ac, checked in by Ralf Corsepius <ralf.corsepius@…>, on 04/21/04 at 16:01:48

Remove duplicate white lines.

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