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 found in the file LICENSE in this distribution or at |
---|
20 | * http://www.rtems.com/license/LICENSE. |
---|
21 | * |
---|
22 | * $Id$ |
---|
23 | */ |
---|
24 | |
---|
25 | #include <stdlib.h> |
---|
26 | #include <assert.h> |
---|
27 | #include <stdlib.h> |
---|
28 | |
---|
29 | extern int close(int fd); |
---|
30 | |
---|
31 | #include <bsp.h> |
---|
32 | #include <bsp/irq.h> |
---|
33 | #include <rtems/bspIo.h> |
---|
34 | #include <rtems/libio.h> |
---|
35 | #include <termios.h> |
---|
36 | #include <bsp/uart.h> |
---|
37 | #include <rtems/bspIo.h> /* printk */ |
---|
38 | |
---|
39 | /* Definitions for BSPConsolePort */ |
---|
40 | /* |
---|
41 | * Possible value for console input/output : |
---|
42 | * BSP_CONSOLE_PORT_CONSOLE |
---|
43 | * BSP_UART_COM1 |
---|
44 | * BSP_UART_COM2 |
---|
45 | */ |
---|
46 | int BSPConsolePort = BSP_CONSOLE_PORT; |
---|
47 | |
---|
48 | int BSPBaseBaud = BSP_UART_BAUD_BASE; |
---|
49 | |
---|
50 | /*-------------------------------------------------------------------------+ |
---|
51 | | External Prototypes |
---|
52 | +--------------------------------------------------------------------------*/ |
---|
53 | |
---|
54 | static int conSetAttr(int minor, const struct termios *); |
---|
55 | |
---|
56 | typedef struct TtySTblRec_ { |
---|
57 | char *name; |
---|
58 | rtems_irq_hdl isr; |
---|
59 | } TtySTblRec, *TtySTbl; |
---|
60 | |
---|
61 | static TtySTblRec ttyS[]={ |
---|
62 | { "/dev/ttyS0", |
---|
63 | #ifdef BSP_UART_IOBASE_COM1 |
---|
64 | BSP_uart_termios_isr_com1 |
---|
65 | #else |
---|
66 | 0 |
---|
67 | #endif |
---|
68 | }, |
---|
69 | { "/dev/ttyS1", |
---|
70 | #ifdef BSP_UART_IOBASE_COM2 |
---|
71 | BSP_uart_termios_isr_com2 |
---|
72 | #else |
---|
73 | 0 |
---|
74 | #endif |
---|
75 | }, |
---|
76 | }; |
---|
77 | |
---|
78 | /*-------------------------------------------------------------------------+ |
---|
79 | | Console device driver INITIALIZE entry point. |
---|
80 | +--------------------------------------------------------------------------+ |
---|
81 | | Initilizes the I/O console (keyboard + VGA display) driver. |
---|
82 | +--------------------------------------------------------------------------*/ |
---|
83 | rtems_device_driver |
---|
84 | console_initialize(rtems_device_major_number major, |
---|
85 | rtems_device_minor_number minor, |
---|
86 | void *arg) |
---|
87 | { |
---|
88 | rtems_status_code status; |
---|
89 | |
---|
90 | /* |
---|
91 | * The video was initialized in the start.s code and does not need |
---|
92 | * to be reinitialized. |
---|
93 | */ |
---|
94 | |
---|
95 | /* |
---|
96 | * Set up TERMIOS |
---|
97 | */ |
---|
98 | rtems_termios_initialize (); |
---|
99 | |
---|
100 | /* |
---|
101 | * Do device-specific initialization |
---|
102 | */ |
---|
103 | |
---|
104 | /* RTEMS calls this routine once with 'minor'==0; loop through |
---|
105 | * all known instances... |
---|
106 | */ |
---|
107 | |
---|
108 | for (minor=0; minor < sizeof(ttyS)/sizeof(ttyS[0]); minor++) { |
---|
109 | char *nm; |
---|
110 | /* |
---|
111 | * Skip ports (possibly not supported by BSP...) we have no ISR for |
---|
112 | */ |
---|
113 | if ( ! ttyS[minor].isr ) |
---|
114 | continue; |
---|
115 | /* |
---|
116 | * Register the device |
---|
117 | */ |
---|
118 | status = rtems_io_register_name ((nm=ttyS[minor].name), major, minor); |
---|
119 | if ( RTEMS_SUCCESSFUL==status && BSPConsolePort == minor) |
---|
120 | { |
---|
121 | printk("Registering /dev/console as minor %d (==%s)\n", |
---|
122 | minor, |
---|
123 | ttyS[minor].name); |
---|
124 | /* also register an alias */ |
---|
125 | status = rtems_io_register_name ( |
---|
126 | (nm="/dev/console"), |
---|
127 | major, |
---|
128 | minor); |
---|
129 | } |
---|
130 | if (status != RTEMS_SUCCESSFUL) |
---|
131 | { |
---|
132 | printk("Error registering %s!\n",nm); |
---|
133 | rtems_fatal_error_occurred (status); |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | return RTEMS_SUCCESSFUL; |
---|
138 | } /* console_initialize */ |
---|
139 | |
---|
140 | static int console_first_open(int major, int minor, void *arg) |
---|
141 | { |
---|
142 | rtems_status_code status; |
---|
143 | |
---|
144 | /* must not open a minor device we have no ISR for */ |
---|
145 | assert( minor>=0 && minor < sizeof(ttyS)/sizeof(ttyS[0]) && ttyS[minor].isr ); |
---|
146 | |
---|
147 | /* 9600-8-N-1 */ |
---|
148 | BSP_uart_init(minor, 9600, 0); |
---|
149 | status = BSP_uart_install_isr(minor, ttyS[minor].isr); |
---|
150 | if (!status) |
---|
151 | { |
---|
152 | printk("Error installing serial console interrupt handler for '%s'!\n", |
---|
153 | ttyS[minor].name); |
---|
154 | rtems_fatal_error_occurred(status); |
---|
155 | } |
---|
156 | return 0; |
---|
157 | } |
---|
158 | |
---|
159 | static int console_last_close(int major, int minor, void *arg) |
---|
160 | { |
---|
161 | BSP_uart_remove_isr(minor, ttyS[minor].isr); |
---|
162 | return 0; |
---|
163 | } |
---|
164 | |
---|
165 | /*-------------------------------------------------------------------------+ |
---|
166 | | Console device driver OPEN entry point |
---|
167 | +--------------------------------------------------------------------------*/ |
---|
168 | rtems_device_driver |
---|
169 | console_open(rtems_device_major_number major, |
---|
170 | rtems_device_minor_number minor, |
---|
171 | void *arg) |
---|
172 | { |
---|
173 | rtems_status_code status; |
---|
174 | static rtems_termios_callbacks cb = |
---|
175 | #if defined(USE_POLLED_IO) |
---|
176 | { |
---|
177 | NULL, /* firstOpen */ |
---|
178 | NULL, /* lastClose */ |
---|
179 | NULL, /* pollRead */ |
---|
180 | BSP_uart_termios_write_polled, /* write */ |
---|
181 | conSetAttr, /* setAttributes */ |
---|
182 | NULL, /* stopRemoteTx */ |
---|
183 | NULL, /* startRemoteTx */ |
---|
184 | 0 /* outputUsesInterrupts */ |
---|
185 | }; |
---|
186 | #else |
---|
187 | { |
---|
188 | console_first_open, /* firstOpen */ |
---|
189 | console_last_close, /* lastClose */ |
---|
190 | NULL, /* pollRead */ |
---|
191 | BSP_uart_termios_write_com, /* write */ |
---|
192 | conSetAttr, /* setAttributes */ |
---|
193 | NULL, /* stopRemoteTx */ |
---|
194 | NULL, /* startRemoteTx */ |
---|
195 | 1 /* outputUsesInterrupts */ |
---|
196 | }; |
---|
197 | #endif |
---|
198 | |
---|
199 | status = rtems_termios_open (major, minor, arg, &cb); |
---|
200 | |
---|
201 | if(status != RTEMS_SUCCESSFUL) |
---|
202 | { |
---|
203 | printk("Error opening console device\n"); |
---|
204 | return status; |
---|
205 | } |
---|
206 | |
---|
207 | /* |
---|
208 | * Pass data area info down to driver |
---|
209 | */ |
---|
210 | BSP_uart_termios_set(minor, |
---|
211 | ((rtems_libio_open_close_args_t *)arg)->iop->data1); |
---|
212 | /* Enable interrupts on channel */ |
---|
213 | BSP_uart_intr_ctrl(minor, BSP_UART_INTR_CTRL_TERMIOS); |
---|
214 | |
---|
215 | return RTEMS_SUCCESSFUL; |
---|
216 | } |
---|
217 | |
---|
218 | /*-------------------------------------------------------------------------+ |
---|
219 | | Console device driver CLOSE entry point |
---|
220 | +--------------------------------------------------------------------------*/ |
---|
221 | rtems_device_driver |
---|
222 | console_close(rtems_device_major_number major, |
---|
223 | rtems_device_minor_number minor, |
---|
224 | void *arg) |
---|
225 | { |
---|
226 | rtems_device_driver res = RTEMS_SUCCESSFUL; |
---|
227 | |
---|
228 | res = rtems_termios_close (arg); |
---|
229 | |
---|
230 | return res; |
---|
231 | } /* console_close */ |
---|
232 | |
---|
233 | /*-------------------------------------------------------------------------+ |
---|
234 | | Console device driver READ entry point. |
---|
235 | +--------------------------------------------------------------------------+ |
---|
236 | | Read characters from the I/O console. We only have stdin. |
---|
237 | +--------------------------------------------------------------------------*/ |
---|
238 | rtems_device_driver |
---|
239 | console_read(rtems_device_major_number major, |
---|
240 | rtems_device_minor_number minor, |
---|
241 | void *arg) |
---|
242 | { |
---|
243 | |
---|
244 | return rtems_termios_read (arg); |
---|
245 | } /* console_read */ |
---|
246 | |
---|
247 | /*-------------------------------------------------------------------------+ |
---|
248 | | Console device driver WRITE entry point. |
---|
249 | +--------------------------------------------------------------------------+ |
---|
250 | | Write characters to the I/O console. Stderr and stdout are the same. |
---|
251 | +--------------------------------------------------------------------------*/ |
---|
252 | rtems_device_driver |
---|
253 | console_write(rtems_device_major_number major, |
---|
254 | rtems_device_minor_number minor, |
---|
255 | void * arg) |
---|
256 | { |
---|
257 | |
---|
258 | return rtems_termios_write (arg); |
---|
259 | |
---|
260 | } /* console_write */ |
---|
261 | |
---|
262 | /* |
---|
263 | * Handle ioctl request. |
---|
264 | */ |
---|
265 | rtems_device_driver |
---|
266 | console_control(rtems_device_major_number major, |
---|
267 | rtems_device_minor_number minor, |
---|
268 | void *arg |
---|
269 | ) |
---|
270 | { |
---|
271 | /* does the BSP support break callbacks ? */ |
---|
272 | #if defined(BIOCSETBREAKCB) && defined(BIOCGETBREAKCB) |
---|
273 | rtems_libio_ioctl_args_t *ioa=arg; |
---|
274 | switch (ioa->command) { |
---|
275 | case BIOCSETBREAKCB: |
---|
276 | return BSP_uart_set_break_cb(minor, ioa); |
---|
277 | case BIOCGETBREAKCB: |
---|
278 | return BSP_uart_get_break_cb(minor, ioa); |
---|
279 | |
---|
280 | default: |
---|
281 | break; |
---|
282 | } |
---|
283 | #endif |
---|
284 | return rtems_termios_ioctl (arg); |
---|
285 | } |
---|
286 | |
---|
287 | static int |
---|
288 | conSetAttr(int minor, const struct termios *t) |
---|
289 | { |
---|
290 | int baud; |
---|
291 | |
---|
292 | switch (t->c_cflag & CBAUD) |
---|
293 | { |
---|
294 | case B50: |
---|
295 | baud = 50; |
---|
296 | break; |
---|
297 | case B75: |
---|
298 | baud = 75; |
---|
299 | break; |
---|
300 | case B110: |
---|
301 | baud = 110; |
---|
302 | break; |
---|
303 | case B134: |
---|
304 | baud = 134; |
---|
305 | break; |
---|
306 | case B150: |
---|
307 | baud = 150; |
---|
308 | break; |
---|
309 | case B200: |
---|
310 | baud = 200; |
---|
311 | break; |
---|
312 | case B300: |
---|
313 | baud = 300; |
---|
314 | break; |
---|
315 | case B600: |
---|
316 | baud = 600; |
---|
317 | break; |
---|
318 | case B1200: |
---|
319 | baud = 1200; |
---|
320 | break; |
---|
321 | case B1800: |
---|
322 | baud = 1800; |
---|
323 | break; |
---|
324 | case B2400: |
---|
325 | baud = 2400; |
---|
326 | break; |
---|
327 | case B4800: |
---|
328 | baud = 4800; |
---|
329 | break; |
---|
330 | case B9600: |
---|
331 | baud = 9600; |
---|
332 | break; |
---|
333 | case B19200: |
---|
334 | baud = 19200; |
---|
335 | break; |
---|
336 | case B38400: |
---|
337 | baud = 38400; |
---|
338 | break; |
---|
339 | case B57600: |
---|
340 | baud = 57600; |
---|
341 | break; |
---|
342 | case B115200: |
---|
343 | baud = 115200; |
---|
344 | break; |
---|
345 | default: |
---|
346 | baud = 0; |
---|
347 | rtems_fatal_error_occurred (RTEMS_INTERNAL_ERROR); |
---|
348 | return 0; |
---|
349 | } |
---|
350 | |
---|
351 | BSP_uart_set_baud(minor, baud); |
---|
352 | |
---|
353 | return 0; |
---|
354 | } |
---|