summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/powerpc/mpc55xxevb/console/console-generic.c
blob: ad3b5485e926690117c98f7e4255dc7a744a6d56 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
 * @file
 *
 * @brief Generic console driver implementation.
 */

/*
 * Copyright (c) 2011 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Obere Lagerstr. 30
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * 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 <sys/cdefs.h>

#include <bsp.h>
#include <bsp/console-generic.h>

#include <rtems/console.h>

static const struct termios console_generic_termios = {
  .c_cflag = CS8 | CREAD | CLOCAL | __CONCAT(B, BSP_DEFAULT_BAUD_RATE)
};

static void console_generic_char_out(char c)
{
  int minor = (int) console_generic_minor;
  const console_generic_callbacks *cb =
    console_generic_info_table [minor].callbacks;

  if (c == '\n') {
    (*cb->poll_write)(minor, '\r');
  }

  (*cb->poll_write)(minor, c);
}

static int console_generic_char_in(void)
{
  int minor = (int) console_generic_minor;
  const console_generic_callbacks *cb =
    console_generic_info_table [minor].callbacks;

  return (*cb->poll_read)(minor);
}

static void console_generic_char_out_do_init(void)
{
  int minor = (int) console_generic_minor;
  const console_generic_callbacks *cb =
    console_generic_info_table [minor].callbacks;
  const struct termios *term = &console_generic_termios;

  BSP_output_char = console_generic_char_out;
  (*cb->termios_callbacks.setAttributes)(minor, term);
}

static void console_generic_char_out_init(char c)
{
  console_generic_char_out_do_init();
  console_generic_char_out(c);
}

rtems_device_driver console_initialize(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void *arg
)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  const console_generic_info *info_table = console_generic_info_table;
  rtems_device_minor_number count = console_generic_info_count;
  rtems_device_minor_number console = console_generic_minor;

  if (count <= 0) {
    rtems_fatal_error_occurred(0xdeadbeef);
  }

  rtems_termios_initialize();

  for (minor = 0; minor < count; ++minor) {
    const console_generic_info *info = info_table + minor;

    sc = rtems_io_register_name(info->device_path, major, minor);
    if (sc != RTEMS_SUCCESSFUL) {
      rtems_fatal_error_occurred(0xdeadbeef);
    }
  }

  sc = rtems_io_register_name(CONSOLE_DEVICE_NAME, major, console);
  if (sc != RTEMS_SUCCESSFUL) {
    rtems_fatal_error_occurred(0xdeadbeef);
  }

  console_generic_char_out_do_init();

  return sc;
}

rtems_device_driver console_open(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void *arg
)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  rtems_device_minor_number count = console_generic_info_count;

  if (minor < count) {
    const console_generic_info *info = &console_generic_info_table [minor];

    sc = rtems_termios_open(
      major,
      minor,
      arg,
      &info->callbacks->termios_callbacks
    );
  } else {
    sc = RTEMS_INVALID_ID;
  }

  return sc;
}

rtems_device_driver console_close(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void *arg
)
{
  return rtems_termios_close(arg);
}

rtems_device_driver console_read(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void *arg
)
{
  return rtems_termios_read(arg);
}

rtems_device_driver console_write(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void *arg
)
{
  return rtems_termios_write(arg);
}

rtems_device_driver console_control(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void *arg
)
{
  return rtems_termios_ioctl(arg);
}

BSP_output_char_function_type BSP_output_char = console_generic_char_out_init;

BSP_polling_getchar_function_type BSP_poll_char = console_generic_char_in;