summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/i386/pc386/console/console_select.c
blob: 0e45ed3d508727bf1553d7e3b30e08eb25856e16 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
 * @file
 *
 * @ingroup Console
 *
 * @brief Generic libchip console select
 *
 * This file contains a routine to select the console based upon a number
 * of criteria.
 */

/*
 *  COPYRIGHT (c) 2011-2012.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 */

#include <bsp.h>
#include <rtems/libio.h>
#include <stdlib.h>
#include <assert.h>
#include <termios.h>

#include <rtems/termiostypes.h>
#include <libchip/serial.h>
#include "../../../shared/console_private.h"
#ifdef RTEMS_RUNTIME_CONSOLE_SELECT
  #include <crt.h>
#endif

/*
 * Method to return true if the device associated with the
 * minor number probs available.
 */
static bool bsp_Is_Available( rtems_device_minor_number minor )
{
  console_tbl  *cptr = Console_Port_Tbl[minor];

  /*
   * First perform the configuration dependent probe, then the
   * device dependent probe
   */
  if ((!cptr->deviceProbe || cptr->deviceProbe(minor)) &&
       cptr->pDeviceFns->deviceProbe(minor)) {
    return true;
  }
  return false;
}

/*
 * Method to return the first available device.
 */
static rtems_device_minor_number bsp_First_Available_Device( void )
{
  rtems_device_minor_number minor;

  for (minor=0; minor < Console_Port_Count ; minor++) {
    console_tbl  *cptr = Console_Port_Tbl[minor];

    /*
     * First perform the configuration dependent probe, then the
     * device dependent probe
     */

    if ((!cptr->deviceProbe || cptr->deviceProbe(minor)) &&
         cptr->pDeviceFns->deviceProbe(minor)) {
      return minor;
    }
  }

  /*
   *  Error No devices were found.  We will want to bail here.
   */
  rtems_fatal_error_occurred(RTEMS_IO_ERROR);
}

void bsp_console_select(void)
{
  static const char* opt;

  /*
   * Check the command line for the type of mode the console is.
   */
  opt = bsp_cmdline_arg ("--console=");

  if (opt) {
    const char* comma;

    opt += sizeof ("--console=") - 1;
    if (strncmp (opt, "console", sizeof ("console") - 1) == 0) {
      Console_Port_Minor = BSP_CONSOLE_VGA;
      BSPPrintkPort      = BSP_CONSOLE_VGA;
    } else if (strncmp (opt, "com1", sizeof ("com1") - 1) == 0) {
      Console_Port_Minor = BSP_CONSOLE_COM1;
      BSPPrintkPort      = BSP_CONSOLE_COM1;
    } else if (strncmp (opt, "com2", sizeof ("com2") - 1) == 0) {
      Console_Port_Minor = BSP_CONSOLE_COM2;
      BSPPrintkPort      = BSP_CONSOLE_COM2;
    }

    comma = strchr (opt, ',');

    if (comma) {
      console_tbl *conscfg;

      comma += 1;
      conscfg = &Console_Configuration_Ports[Console_Port_Minor];
      if (strncmp (opt, "115200", sizeof ("115200") - 1) == 0)
        conscfg->pDeviceParams = (void *)115200;
      else if (strncmp (opt, "57600", sizeof ("57600") - 1) == 0)
        conscfg->pDeviceParams = (void *)57600;
      else if (strncmp (opt, "38400", sizeof ("38400") - 1) == 0)
        conscfg->pDeviceParams = (void *)38400;
      else if (strncmp (opt, "19200", sizeof ("19200") - 1) == 0)
        conscfg->pDeviceParams = (void *)19200;
      else if (strncmp (opt, "9600", sizeof ("9600") - 1) == 0)
        conscfg->pDeviceParams = (void *)9600;
      else if (strncmp (opt, "4800", sizeof ("4800") - 1) == 0)
        conscfg->pDeviceParams = (void *)4800;
    }
  }

  #ifdef RTEMS_RUNTIME_CONSOLE_SELECT
    if ( BSP_runtime_console_select )
      BSP_runtime_console_select(&BSPPrintkPort, &Console_Port_Minor);

    /*
     * If no video card, fall back to serial port console
     */
    if((Console_Port_Minor == BSP_CONSOLE_VGA)
     && (*(unsigned char*) NB_MAX_ROW_ADDR == 0)
     && (*(unsigned short*)NB_MAX_COL_ADDR == 0)) {
      Console_Port_Minor = BSP_CONSOLE_COM2;
      BSPPrintkPort      = BSP_CONSOLE_COM1;
    }
  #endif

  /*
   * If the device that was selected isn't available then
   * let the user know and select the first available device.
   */
  if ( !bsp_Is_Available( Console_Port_Minor ) ) {
    printk(
      "Error finding %s setting console to first available\n",
      Console_Port_Tbl[Console_Port_Minor]->sDeviceName
    );
    Console_Port_Minor = bsp_First_Available_Device();
  }
}