1 | /* This file contains the TTY driver for the serial ports. The driver |
---|
2 | * is layered so that different UART hardware can be used. It is implemented |
---|
3 | * using the Driver Manager. |
---|
4 | * |
---|
5 | * This driver uses the termios pseudo driver. |
---|
6 | * |
---|
7 | * COPYRIGHT (c) 2010. |
---|
8 | * Cobham Gaisler AB. |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.com/license/LICENSE. |
---|
13 | */ |
---|
14 | |
---|
15 | #include <bsp.h> |
---|
16 | #include <stdlib.h> |
---|
17 | #include <rtems/libio.h> |
---|
18 | #include <rtems/bspIo.h> |
---|
19 | #include <cons.h> |
---|
20 | |
---|
21 | #ifdef RTEMS_DRVMGR_STARTUP |
---|
22 | |
---|
23 | /* Note that it is not possible to use the interrupt mode of the driver |
---|
24 | * together with the "old" APBUART and -u to GRMON. However the new |
---|
25 | * APBUART core (from 1.0.17-b2710) has the GRMON debug bit and can |
---|
26 | * handle interrupts. |
---|
27 | */ |
---|
28 | |
---|
29 | int console_initialized = 0; |
---|
30 | rtems_device_major_number console_major = 0; |
---|
31 | |
---|
32 | #define FLAG_SYSCON 0x01 |
---|
33 | struct console_priv { |
---|
34 | unsigned char flags; /* 0x1=SystemConsole */ |
---|
35 | unsigned char minor; |
---|
36 | struct console_dev *dev; |
---|
37 | }; |
---|
38 | |
---|
39 | #define CONSOLE_MAX BSP_NUMBER_OF_TERMIOS_PORTS |
---|
40 | struct console_priv cons[CONSOLE_MAX] = {{0,0},}; |
---|
41 | |
---|
42 | /* Register Console to TERMIOS layer and initialize it */ |
---|
43 | static void console_dev_init(struct console_priv *con, int minor) |
---|
44 | { |
---|
45 | char name[16], *fsname; |
---|
46 | rtems_status_code status; |
---|
47 | |
---|
48 | if (!con->dev->fsname) { |
---|
49 | strcpy(name, "/dev/console_a"); |
---|
50 | /* Special console name and MINOR for SYSTEM CONSOLE */ |
---|
51 | if (minor == 0) |
---|
52 | name[12] = '\0'; /* /dev/console */ |
---|
53 | name[13] += minor; /* when minor=0, this has no effect... */ |
---|
54 | fsname = name; |
---|
55 | } else { |
---|
56 | fsname = con->dev->fsname; |
---|
57 | } |
---|
58 | status = rtems_io_register_name(fsname, console_major, minor); |
---|
59 | if ((minor == 0) && (status != RTEMS_SUCCESSFUL)) |
---|
60 | rtems_fatal_error_occurred(status); |
---|
61 | } |
---|
62 | |
---|
63 | void console_dev_register(struct console_dev *dev) |
---|
64 | { |
---|
65 | int i, minor = 0; |
---|
66 | struct console_priv *con = NULL; |
---|
67 | |
---|
68 | if ((dev->flags & CONSOLE_FLAG_SYSCON) && !cons[0].dev) { |
---|
69 | con = &cons[0]; |
---|
70 | con->flags = FLAG_SYSCON; |
---|
71 | } else { |
---|
72 | for (i=1; i<CONSOLE_MAX; i++) { |
---|
73 | if (!cons[i].dev) { |
---|
74 | con = &cons[i]; |
---|
75 | con->flags = 0; |
---|
76 | minor = i; |
---|
77 | break; |
---|
78 | } |
---|
79 | } |
---|
80 | } |
---|
81 | if (con == NULL) { |
---|
82 | /* Not enough console structures */ |
---|
83 | return; |
---|
84 | } |
---|
85 | |
---|
86 | /* Assign Console */ |
---|
87 | con->dev = dev; |
---|
88 | con->minor = minor; |
---|
89 | |
---|
90 | /* Console layer is already initialized, that means that we can |
---|
91 | * register termios interface directly. |
---|
92 | */ |
---|
93 | if (console_initialized) |
---|
94 | console_dev_init(con, minor); |
---|
95 | } |
---|
96 | |
---|
97 | #if 0 |
---|
98 | void console_dev_unregister(struct console_dev *dev) |
---|
99 | { |
---|
100 | |
---|
101 | } |
---|
102 | #endif |
---|
103 | |
---|
104 | rtems_device_driver console_initialize( |
---|
105 | rtems_device_major_number major, |
---|
106 | rtems_device_minor_number minor, |
---|
107 | void *arg) |
---|
108 | { |
---|
109 | int i; |
---|
110 | |
---|
111 | console_major = major; |
---|
112 | |
---|
113 | rtems_termios_initialize(); |
---|
114 | |
---|
115 | /* Register all Console a file system device node */ |
---|
116 | for (i=0; i<CONSOLE_MAX; i++) { |
---|
117 | if (cons[i].dev) |
---|
118 | console_dev_init(&cons[i], i); |
---|
119 | } |
---|
120 | |
---|
121 | console_initialized = 1; |
---|
122 | |
---|
123 | return RTEMS_SUCCESSFUL; |
---|
124 | } |
---|
125 | |
---|
126 | rtems_device_driver console_open( |
---|
127 | rtems_device_major_number major, |
---|
128 | rtems_device_minor_number minor, |
---|
129 | void *arg) |
---|
130 | { |
---|
131 | rtems_status_code status; |
---|
132 | struct termios term; |
---|
133 | |
---|
134 | if ((minor >= CONSOLE_MAX) || !cons[minor].dev) |
---|
135 | return RTEMS_INVALID_NUMBER; |
---|
136 | |
---|
137 | status = rtems_termios_open( |
---|
138 | major, |
---|
139 | (int)cons[minor].dev, |
---|
140 | arg, |
---|
141 | cons[minor].dev->callbacks); |
---|
142 | |
---|
143 | /* Inherit UART hardware parameters from bootloader on system console */ |
---|
144 | if ((status == RTEMS_SUCCESSFUL) && (cons[minor].flags & FLAG_SYSCON) && |
---|
145 | (cons[minor].dev->ops.get_uart_attrs != NULL)) { |
---|
146 | if (tcgetattr(STDIN_FILENO, &term) >= 0) { |
---|
147 | cons[minor].dev->ops.get_uart_attrs(cons[minor].dev, |
---|
148 | &term); |
---|
149 | term.c_oflag |= ONLCR; |
---|
150 | tcsetattr(STDIN_FILENO, TCSANOW, &term); |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | return status; |
---|
155 | } |
---|
156 | |
---|
157 | rtems_device_driver console_close( |
---|
158 | rtems_device_major_number major, |
---|
159 | rtems_device_minor_number minor, |
---|
160 | void *arg) |
---|
161 | { |
---|
162 | return rtems_termios_close(arg); |
---|
163 | } |
---|
164 | |
---|
165 | rtems_device_driver console_read( |
---|
166 | rtems_device_major_number major, |
---|
167 | rtems_device_minor_number minor, |
---|
168 | void *arg) |
---|
169 | { |
---|
170 | return rtems_termios_read(arg); |
---|
171 | } |
---|
172 | |
---|
173 | rtems_device_driver console_write( |
---|
174 | rtems_device_major_number major, |
---|
175 | rtems_device_minor_number minor, |
---|
176 | void *arg) |
---|
177 | { |
---|
178 | return rtems_termios_write(arg); |
---|
179 | } |
---|
180 | |
---|
181 | rtems_device_driver console_control( |
---|
182 | rtems_device_major_number major, |
---|
183 | rtems_device_minor_number minor, |
---|
184 | void *arg) |
---|
185 | { |
---|
186 | return rtems_termios_ioctl(arg); |
---|
187 | } |
---|
188 | |
---|
189 | #endif |
---|