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

4.104.11
Last change on this file since debbc9e was debbc9e, checked in by Joel Sherrill <joel.sherrill@…>, on May 6, 2009 at 4:36:16 PM

2009-05-06 Joel Sherrill <joel.sherrill@…>

  • console/console.c, console/inch.c, ide/ide.c: Fixed warnings.
  • Property mode set to 100644
File size: 14.1 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 int
120ibmpc_console_write(int minor, const char *buf, int len)
121{
122  int 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 0;
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/*-------------------------------------------------------------------------+
143| Console device driver INITIALIZE entry point.
144+--------------------------------------------------------------------------+
145| Initilizes the I/O console (keyboard + VGA display) driver.
146+--------------------------------------------------------------------------*/
147rtems_device_driver
148console_initialize(rtems_device_major_number major,
149                   rtems_device_minor_number minor,
150                   void                      *arg)
151{
152  rtems_status_code status;
153  const char* mode;
154 
155  /*
156   * Check the command line for the type of mode
157   * the consol is.
158   */
159  mode = bsp_cmdline_arg ("--console=");
160
161  if (mode)
162  {
163    mode += sizeof ("--console=") - 1;
164    if (strncmp (mode, "console", sizeof ("console") - 1) == 0)
165    {
166      BSPConsolePort = BSP_CONSOLE_PORT_CONSOLE;
167      BSPPrintkPort  = BSP_CONSOLE_PORT_CONSOLE;
168    }
169    else if (strncmp (mode, "com1", sizeof ("com1") - 1) == 0)
170    {
171      BSPConsolePort = BSP_UART_COM1;
172      BSPPrintkPort  = BSP_UART_COM1;
173    }
174    else if (strncmp (mode, "com2", sizeof ("com2") - 1) == 0)
175    {
176      BSPConsolePort = BSP_UART_COM2;
177      BSPPrintkPort  = BSP_UART_COM2;
178    }
179  }
180 
181  /* Initialize the KBD interface */
182  kbd_init();
183
184  /*
185   * Set up TERMIOS
186   */
187  rtems_termios_initialize ();
188
189#ifdef RTEMS_RUNTIME_CONSOLE_SELECT
190  /*
191   * If no video card, fall back to serial port console
192   */
193#include <crt.h>
194  if((BSPConsolePort == BSP_CONSOLE_PORT_CONSOLE)
195   && (*(unsigned char*) NB_MAX_ROW_ADDR == 0)
196   && (*(unsigned short*)NB_MAX_COL_ADDR == 0)) {
197    BSPConsolePort = BSP_UART_COM2;
198    BSPPrintkPort  = BSP_UART_COM1;
199  }
200#endif
201
202  /*
203   *  The video was initialized in the start.s code and does not need
204   *  to be reinitialized.
205   */
206
207  if(BSPConsolePort == BSP_CONSOLE_PORT_CONSOLE)
208    {
209      /* Install keyboard interrupt handler */
210      status = BSP_install_rtems_irq_handler(&console_isr_data);
211
212    if (!status)
213        {
214          printk("Error installing keyboard interrupt handler!\n");
215          rtems_fatal_error_occurred(status);
216        }
217
218      status = rtems_io_register_name("/dev/console", major, 0);
219      if (status != RTEMS_SUCCESSFUL)
220        {
221          printk("Error registering console device!\n");
222          rtems_fatal_error_occurred(status);
223        }
224      printk("Initialized console on port CONSOLE\n\n");
225    }
226  else
227    {
228      /*
229       * Do device-specific initialization
230       */
231      /* 9600-8-N-1 */
232      BSP_uart_init(BSPConsolePort, 9600, CHR_8_BITS, 0, 0, 0);
233
234      /* Set interrupt handler */
235      if(BSPConsolePort == BSP_UART_COM1)
236        {
237             console_isr_data.name = BSP_UART_COM1_IRQ;
238        console_isr_data.hdl  = BSP_uart_termios_isr_com1;
239
240        }
241      else
242           {
243          assert(BSPConsolePort == BSP_UART_COM2);
244          console_isr_data.name = BSP_UART_COM2_IRQ;
245          console_isr_data.hdl  = BSP_uart_termios_isr_com2;
246        }
247      status = BSP_install_rtems_irq_handler(&console_isr_data);
248
249      if (!status){
250          printk("Error installing serial console interrupt handler!\n");
251          rtems_fatal_error_occurred(status);
252      }
253      /*
254       * Register the device
255       */
256      status = rtems_io_register_name ("/dev/console", major, 0);
257      if (status != RTEMS_SUCCESSFUL)
258        {
259          printk("Error registering console device!\n");
260          rtems_fatal_error_occurred (status);
261        }
262
263      if(BSPConsolePort == BSP_UART_COM1)
264        {
265          printk("Initialized console on port COM1 9600-8-N-1\n\n");
266        }
267      else
268        {
269          printk("Initialized console on port COM2 9600-8-N-1\n\n");
270        }
271
272      if(BSPPrintkPort == BSP_UART_COM1)
273        {
274          printk("Warning : This will be the last message on console\n");
275
276          /*
277           * FIXME: cast below defeats the very idea of having
278           * function pointer types defined
279           */
280          BSP_output_char = (BSP_output_char_function_type)
281                              BSP_output_char_via_serial;
282          BSP_poll_char   = (BSP_polling_getchar_function_type)
283                              BSP_poll_char_via_serial;
284        }
285      else if(BSPPrintkPort != BSP_CONSOLE_PORT_CONSOLE)
286        {
287           printk("illegal assignement of printk channel");
288         rtems_fatal_error_occurred (status);
289        }
290
291    }
292  return RTEMS_SUCCESSFUL;
293} /* console_initialize */
294
295static int console_open_count = 0;
296
297static int console_last_close(int major, int minor, void *arg)
298{
299  BSP_remove_rtems_irq_handler (&console_isr_data);
300
301  return 0;
302}
303
304static int ser_console_first_open(int major, int minor, void *arg)
305{
306  /*
307   * Pass data area info down to driver
308   */
309  BSP_uart_termios_set(BSPConsolePort,
310                         ((rtems_libio_open_close_args_t *)arg)->iop->data1);
311
312  /* Enable interrupts  on channel */
313  BSP_uart_intr_ctrl(BSPConsolePort, BSP_UART_INTR_CTRL_TERMIOS);
314
315  return 0;
316}
317
318/*-------------------------------------------------------------------------+
319| Console device driver OPEN entry point
320+--------------------------------------------------------------------------*/
321rtems_device_driver
322console_open(rtems_device_major_number major,
323                rtems_device_minor_number minor,
324                void                      *arg)
325{
326  rtems_status_code              status;
327  static rtems_termios_callbacks cb =
328  {
329    NULL,                     /* firstOpen */
330    console_last_close,       /* lastClose */
331    NULL,          /* pollRead */
332    BSP_uart_termios_write_com1, /* write */
333    conSetAttr,               /* setAttributes */
334    NULL,                     /* stopRemoteTx */
335    NULL,                     /* startRemoteTx */
336    1                         /* outputUsesInterrupts */
337  };
338
339  if(BSPConsolePort == BSP_CONSOLE_PORT_CONSOLE)
340    {
341
342      /* Let's set the routines for termios to poll the
343       * Kbd queue for data
344       */
345      cb.pollRead = kbd_poll_read;
346      cb.outputUsesInterrupts = 0;
347      /* write the "echo" if it is on */
348      cb.write = ibmpc_console_write;
349
350      cb.setAttributes = NULL;
351      ++console_open_count;
352      status = rtems_termios_open (major, minor, arg, &cb);
353      if(status != RTEMS_SUCCESSFUL)
354      {
355         printk("Error openning console device\n");
356      }
357      return status;
358    }
359
360  if(BSPConsolePort == BSP_UART_COM2)
361    {
362      cb.write = BSP_uart_termios_write_com2;
363    }
364
365  cb.firstOpen = ser_console_first_open;
366
367  status = rtems_termios_open (major, minor, arg, &cb);
368
369  if(status != RTEMS_SUCCESSFUL)
370    {
371      printk("Error openning console device\n");
372      return status;
373    }
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  baud = termios_baud_to_number(t->c_cflag & CBAUD);
465  if ( baud > 115200 )
466    rtems_fatal_error_occurred (RTEMS_INTERNAL_ERROR);
467
468  if (t->c_cflag & PARENB) {
469    /* Parity is enabled */
470    if (t->c_cflag & PARODD) {
471      /* Parity is odd */
472      parity = PEN;
473    }
474    else {
475      /* Parity is even */
476      parity = PEN | EPS;
477    }
478  }
479  else {
480    /* No parity */
481    parity = 0;
482  }
483
484  switch (t->c_cflag & CSIZE) {
485    case CS5: databits = CHR_5_BITS; break;
486    case CS6: databits = CHR_6_BITS; break;
487    case CS7: databits = CHR_7_BITS; break;
488    default: /* just to avoid warnings -- all cases are covered. */
489    case CS8: databits = CHR_8_BITS; break;
490   }
491
492  if (t->c_cflag & CSTOPB) {
493    /* 2 stop bits */
494    stopbits = STB;
495  }
496  else {
497    /* 1 stop bit */
498    stopbits = 0;
499  }
500
501  BSP_uart_set_attributes(BSPConsolePort, baud, databits, parity, stopbits);
502
503  return 0;
504}
505
506void keyboard_interrupt_wrapper(void *unused){
507  keyboard_interrupt();
508}
509
510/*
511 * BSP initialization
512 */
513
514BSP_output_char_function_type BSP_output_char =
515                       (BSP_output_char_function_type) _IBMPC_outch;
516
517BSP_polling_getchar_function_type BSP_poll_char = BSP_wait_polled_input;
Note: See TracBrowser for help on using the repository browser.