1 | /* |
---|
2 | * console.c -- console I/O package |
---|
3 | * |
---|
4 | * Copyright (C) 1999 Eric Valette. valette@crf.canon.fr |
---|
5 | * |
---|
6 | * This code is based on the pc386 BSP console.c so the following |
---|
7 | * copyright also applies : |
---|
8 | * |
---|
9 | * (C) Copyright 1997 - |
---|
10 | * - NavIST Group - Real-Time Distributed Systems and Industrial Automation |
---|
11 | * |
---|
12 | * Till Straumann, <strauman@slac.stanford.edu>, 12/20/2001 |
---|
13 | * separate BSP specific stuff from generics... |
---|
14 | * |
---|
15 | * http://pandora.ist.utl.pt |
---|
16 | * |
---|
17 | * Instituto Superior Tecnico * Lisboa * PORTUGAL |
---|
18 | * The license and distribution terms for this file may be |
---|
19 | * found in the file LICENSE in this distribution or at |
---|
20 | * http://www.rtems.org/license/LICENSE. |
---|
21 | */ |
---|
22 | |
---|
23 | #include <stdlib.h> |
---|
24 | #include <assert.h> |
---|
25 | #include <inttypes.h> |
---|
26 | |
---|
27 | #include <bsp.h> |
---|
28 | #include <bsp/irq.h> |
---|
29 | #include <rtems/bspIo.h> |
---|
30 | #include <rtems/libio.h> |
---|
31 | #include <rtems/console.h> |
---|
32 | #include <rtems/termiostypes.h> |
---|
33 | #include <termios.h> |
---|
34 | #include <bsp/uart.h> |
---|
35 | #include <rtems/bspIo.h> /* printk */ |
---|
36 | |
---|
37 | /* Definitions for BSPConsolePort */ |
---|
38 | /* |
---|
39 | * Possible value for console input/output : |
---|
40 | * BSP_CONSOLE_PORT_CONSOLE |
---|
41 | * BSP_UART_COM1 |
---|
42 | * BSP_UART_COM2 |
---|
43 | */ |
---|
44 | int BSPConsolePort = BSP_CONSOLE_PORT; |
---|
45 | |
---|
46 | int BSPBaseBaud = BSP_UART_BAUD_BASE; |
---|
47 | |
---|
48 | /* |
---|
49 | * TERMIOS_OUTPUT_MODE should be a 'bspopts.h/configure'-able option; |
---|
50 | * we could even make it a link-time option (but that would require |
---|
51 | * small changes)... |
---|
52 | */ |
---|
53 | #if defined(USE_POLLED_IO) |
---|
54 | #define TERMIOS_OUTPUT_MODE TERMIOS_POLLED |
---|
55 | #elif defined(USE_TASK_DRIVEN_IO) |
---|
56 | #define TERMIOS_OUTPUT_MODE TERMIOS_TASK_DRIVEN |
---|
57 | #else |
---|
58 | #define TERMIOS_OUTPUT_MODE TERMIOS_IRQ_DRIVEN |
---|
59 | #endif |
---|
60 | |
---|
61 | /*-------------------------------------------------------------------------+ |
---|
62 | | External Prototypes |
---|
63 | +--------------------------------------------------------------------------*/ |
---|
64 | |
---|
65 | static int conSetAttr(int minor, const struct termios *); |
---|
66 | |
---|
67 | typedef struct TtySTblRec_ { |
---|
68 | char *name; |
---|
69 | rtems_irq_hdl isr; |
---|
70 | } TtySTblRec, *TtySTbl; |
---|
71 | |
---|
72 | static TtySTblRec ttyS[]={ |
---|
73 | { "/dev/ttyS0", |
---|
74 | #ifdef BSP_UART_IOBASE_COM1 |
---|
75 | BSP_uart_termios_isr_com1 |
---|
76 | #else |
---|
77 | 0 |
---|
78 | #endif |
---|
79 | }, |
---|
80 | { "/dev/ttyS1", |
---|
81 | #ifdef BSP_UART_IOBASE_COM2 |
---|
82 | BSP_uart_termios_isr_com2 |
---|
83 | #else |
---|
84 | 0 |
---|
85 | #endif |
---|
86 | }, |
---|
87 | }; |
---|
88 | |
---|
89 | /*-------------------------------------------------------------------------+ |
---|
90 | | Console device driver INITIALIZE entry point. |
---|
91 | +--------------------------------------------------------------------------+ |
---|
92 | | Initilizes the I/O console (keyboard + VGA display) driver. |
---|
93 | +--------------------------------------------------------------------------*/ |
---|
94 | rtems_device_driver console_initialize( |
---|
95 | rtems_device_major_number major, |
---|
96 | rtems_device_minor_number minor, |
---|
97 | void *arg |
---|
98 | ) |
---|
99 | { |
---|
100 | rtems_status_code status; |
---|
101 | |
---|
102 | /* |
---|
103 | * The video was initialized in the start.s code and does not need |
---|
104 | * to be reinitialized. |
---|
105 | */ |
---|
106 | |
---|
107 | /* |
---|
108 | * Set up TERMIOS |
---|
109 | */ |
---|
110 | rtems_termios_initialize(); |
---|
111 | |
---|
112 | /* |
---|
113 | * Do device-specific initialization |
---|
114 | */ |
---|
115 | |
---|
116 | /* RTEMS calls this routine once with 'minor'==0; loop through |
---|
117 | * all known instances... |
---|
118 | */ |
---|
119 | |
---|
120 | for (minor=0; minor < sizeof(ttyS)/sizeof(ttyS[0]); minor++) { |
---|
121 | char *nm; |
---|
122 | /* |
---|
123 | * Skip ports (possibly not supported by BSP...) we have no ISR for |
---|
124 | */ |
---|
125 | if ( ! ttyS[minor].isr ) |
---|
126 | continue; |
---|
127 | /* |
---|
128 | * Register the device |
---|
129 | */ |
---|
130 | status = rtems_io_register_name ((nm=ttyS[minor].name), major, minor); |
---|
131 | if ( RTEMS_SUCCESSFUL==status && BSPConsolePort == minor) { |
---|
132 | printk("Registering /dev/console as minor %" PRIu32 " (==%s)\n", |
---|
133 | minor, |
---|
134 | ttyS[minor].name); |
---|
135 | /* also register an alias */ |
---|
136 | status = rtems_io_register_name ( (nm="/dev/console"), major, minor); |
---|
137 | } |
---|
138 | |
---|
139 | if (status != RTEMS_SUCCESSFUL) { |
---|
140 | printk("Error registering %s!\n",nm); |
---|
141 | rtems_fatal_error_occurred (status); |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | return RTEMS_SUCCESSFUL; |
---|
146 | } /* console_initialize */ |
---|
147 | |
---|
148 | #if !defined(USE_POLLED_IO) |
---|
149 | static int console_first_open(int major, int minor, void *arg) |
---|
150 | { |
---|
151 | rtems_status_code status; |
---|
152 | |
---|
153 | /* must not open a minor device we have no ISR for */ |
---|
154 | assert( minor>=0 && minor < sizeof(ttyS)/sizeof(ttyS[0]) && ttyS[minor].isr ); |
---|
155 | |
---|
156 | /* 9600-8-N-1 */ |
---|
157 | BSP_uart_init(minor, 9600, 0); |
---|
158 | status = BSP_uart_install_isr(minor, ttyS[minor].isr); |
---|
159 | if (!status) { |
---|
160 | printk("Error installing serial console interrupt handler for '%s'!\n", |
---|
161 | ttyS[minor].name); |
---|
162 | rtems_fatal_error_occurred(status); |
---|
163 | } |
---|
164 | |
---|
165 | /* |
---|
166 | * Pass data area info down to driver |
---|
167 | */ |
---|
168 | BSP_uart_termios_set(minor, ((rtems_libio_open_close_args_t *)arg)->iop->data1); |
---|
169 | |
---|
170 | /* Enable interrupts on channel */ |
---|
171 | BSP_uart_intr_ctrl(minor, BSP_UART_INTR_CTRL_TERMIOS); |
---|
172 | |
---|
173 | return 0; |
---|
174 | } |
---|
175 | #endif |
---|
176 | |
---|
177 | #if !defined(USE_POLLED_IO) |
---|
178 | static int console_last_close(int major, int minor, void *arg) |
---|
179 | { |
---|
180 | BSP_uart_remove_isr(minor, ttyS[minor].isr); |
---|
181 | return 0; |
---|
182 | } |
---|
183 | #endif |
---|
184 | |
---|
185 | /*-------------------------------------------------------------------------+ |
---|
186 | | Console device driver OPEN entry point |
---|
187 | +--------------------------------------------------------------------------*/ |
---|
188 | rtems_device_driver console_open( |
---|
189 | rtems_device_major_number major, |
---|
190 | rtems_device_minor_number minor, |
---|
191 | void *arg |
---|
192 | ) |
---|
193 | { |
---|
194 | rtems_status_code status; |
---|
195 | static rtems_termios_callbacks cb = |
---|
196 | #if defined(USE_POLLED_IO) |
---|
197 | { |
---|
198 | NULL, /* firstOpen */ |
---|
199 | NULL, /* lastClose */ |
---|
200 | NULL, /* pollRead */ |
---|
201 | BSP_uart_termios_write_polled, /* write */ |
---|
202 | conSetAttr, /* setAttributes */ |
---|
203 | NULL, /* stopRemoteTx */ |
---|
204 | NULL, /* startRemoteTx */ |
---|
205 | TERMIOS_POLLED /* outputUsesInterrupts */ |
---|
206 | }; |
---|
207 | #else |
---|
208 | { |
---|
209 | console_first_open, /* firstOpen */ |
---|
210 | console_last_close, /* lastClose */ |
---|
211 | #ifdef USE_TASK_DRIVEN_IO |
---|
212 | BSP_uart_termios_read_com, /* pollRead */ |
---|
213 | #else |
---|
214 | NULL, /* pollRead */ |
---|
215 | #endif |
---|
216 | BSP_uart_termios_write_com, /* write */ |
---|
217 | conSetAttr, /* setAttributes */ |
---|
218 | NULL, /* stopRemoteTx */ |
---|
219 | NULL, /* startRemoteTx */ |
---|
220 | TERMIOS_OUTPUT_MODE /* outputUsesInterrupts */ |
---|
221 | }; |
---|
222 | #endif |
---|
223 | |
---|
224 | status = rtems_termios_open (major, minor, arg, &cb); |
---|
225 | |
---|
226 | if (status != RTEMS_SUCCESSFUL) { |
---|
227 | printk("Error opening console device\n"); |
---|
228 | return status; |
---|
229 | } |
---|
230 | |
---|
231 | return RTEMS_SUCCESSFUL; |
---|
232 | } |
---|
233 | |
---|
234 | /*-------------------------------------------------------------------------+ |
---|
235 | | Console device driver CLOSE entry point |
---|
236 | +--------------------------------------------------------------------------*/ |
---|
237 | rtems_device_driver |
---|
238 | console_close( |
---|
239 | rtems_device_major_number major, |
---|
240 | rtems_device_minor_number minor, |
---|
241 | void *arg |
---|
242 | ) |
---|
243 | { |
---|
244 | rtems_device_driver res = RTEMS_SUCCESSFUL; |
---|
245 | |
---|
246 | res = rtems_termios_close (arg); |
---|
247 | |
---|
248 | return res; |
---|
249 | } /* console_close */ |
---|
250 | |
---|
251 | /*-------------------------------------------------------------------------+ |
---|
252 | | Console device driver READ entry point. |
---|
253 | +--------------------------------------------------------------------------+ |
---|
254 | | Read characters from the I/O console. We only have stdin. |
---|
255 | +--------------------------------------------------------------------------*/ |
---|
256 | rtems_device_driver console_read( |
---|
257 | rtems_device_major_number major, |
---|
258 | rtems_device_minor_number minor, |
---|
259 | void *arg |
---|
260 | ) |
---|
261 | { |
---|
262 | return rtems_termios_read (arg); |
---|
263 | } /* console_read */ |
---|
264 | |
---|
265 | /*-------------------------------------------------------------------------+ |
---|
266 | | Console device driver WRITE entry point. |
---|
267 | +--------------------------------------------------------------------------+ |
---|
268 | | Write characters to the I/O console. Stderr and stdout are the same. |
---|
269 | +--------------------------------------------------------------------------*/ |
---|
270 | rtems_device_driver console_write( |
---|
271 | rtems_device_major_number major, |
---|
272 | rtems_device_minor_number minor, |
---|
273 | void *arg |
---|
274 | ) |
---|
275 | { |
---|
276 | return rtems_termios_write (arg); |
---|
277 | } /* console_write */ |
---|
278 | |
---|
279 | /* |
---|
280 | * Handle ioctl request. |
---|
281 | */ |
---|
282 | rtems_device_driver console_control( |
---|
283 | rtems_device_major_number major, |
---|
284 | rtems_device_minor_number minor, |
---|
285 | void *arg |
---|
286 | ) |
---|
287 | { |
---|
288 | /* does the BSP support break callbacks ? */ |
---|
289 | #if defined(BIOCSETBREAKCB) && defined(BIOCGETBREAKCB) |
---|
290 | rtems_libio_ioctl_args_t *ioa=arg; |
---|
291 | switch (ioa->command) { |
---|
292 | case BIOCSETBREAKCB: return BSP_uart_set_break_cb(minor, ioa); |
---|
293 | case BIOCGETBREAKCB: return BSP_uart_get_break_cb(minor, ioa); |
---|
294 | default: break; |
---|
295 | } |
---|
296 | #endif |
---|
297 | return rtems_termios_ioctl (arg); |
---|
298 | } |
---|
299 | |
---|
300 | static int conSetAttr( |
---|
301 | int minor, |
---|
302 | const struct termios *t |
---|
303 | ) |
---|
304 | { |
---|
305 | rtems_termios_baud_t baud; |
---|
306 | |
---|
307 | baud = rtems_termios_baud_to_number(t->c_cflag & CBAUD); |
---|
308 | if ( baud > 115200 ) |
---|
309 | rtems_fatal_error_occurred (RTEMS_INTERNAL_ERROR); |
---|
310 | |
---|
311 | BSP_uart_set_baud(minor, baud); |
---|
312 | |
---|
313 | return 0; |
---|
314 | } |
---|