1 | /*-------------------------------------------------------------------------+ |
---|
2 | | console.c v1.1 - PC386 BSP - 1997/08/07 |
---|
3 | +--------------------------------------------------------------------------+ |
---|
4 | | This file contains the PC386 console I/O package. |
---|
5 | +--------------------------------------------------------------------------+ |
---|
6 | | (C) Copyright 1997 - |
---|
7 | | - NavIST Group - Real-Time Distributed Systems and Industrial Automation |
---|
8 | | |
---|
9 | | http://pandora.ist.utl.pt |
---|
10 | | |
---|
11 | | Instituto Superior Tecnico * Lisboa * PORTUGAL |
---|
12 | +--------------------------------------------------------------------------+ |
---|
13 | | Disclaimer: |
---|
14 | | |
---|
15 | | This file is provided "AS IS" without warranty of any kind, either |
---|
16 | | expressed or implied. |
---|
17 | +--------------------------------------------------------------------------+ |
---|
18 | | This code is based on: |
---|
19 | | console.c,v 1.4 1995/12/19 20:07:23 joel Exp - go32 BSP |
---|
20 | | With the following copyright notice: |
---|
21 | | ************************************************************************** |
---|
22 | | * COPYRIGHT (c) 1989-1999. |
---|
23 | | * On-Line Applications Research Corporation (OAR). |
---|
24 | | * |
---|
25 | | * The license and distribution terms for this file may be |
---|
26 | | * found in found in the file LICENSE in this distribution or at |
---|
27 | | * http://www.OARcorp.com/rtems/license.html. |
---|
28 | | ************************************************************************** |
---|
29 | | |
---|
30 | | $Id$ |
---|
31 | +--------------------------------------------------------------------------*/ |
---|
32 | |
---|
33 | #include <stdio.h> |
---|
34 | #include <stdlib.h> |
---|
35 | #include <assert.h> |
---|
36 | #undef __assert |
---|
37 | void __assert (const char *file, int line, const char *msg); |
---|
38 | |
---|
39 | #include <bsp.h> |
---|
40 | #include <irq.h> |
---|
41 | #include <rtems/libio.h> |
---|
42 | #include <termios.h> |
---|
43 | #include <uart.h> |
---|
44 | #include <libcpu/cpuModel.h> |
---|
45 | |
---|
46 | /* |
---|
47 | * Possible value for console input/output : |
---|
48 | * BSP_CONSOLE_PORT_CONSOLE |
---|
49 | * BSP_UART_COM1 |
---|
50 | * BSP_UART_COM2 |
---|
51 | * |
---|
52 | * Note: |
---|
53 | * 1. Currently BSPPrintkPort, cannot be assigned to COM2, |
---|
54 | * it will be fixed soon. |
---|
55 | * |
---|
56 | * 2. If both BSPConsolePort and BSPPrintkport are assigned |
---|
57 | * to same serial device it does not work that great |
---|
58 | */ |
---|
59 | |
---|
60 | int BSPConsolePort = BSP_CONSOLE_PORT_CONSOLE; |
---|
61 | int BSPPrintkPort = BSP_CONSOLE_PORT_CONSOLE; |
---|
62 | |
---|
63 | /* int BSPConsolePort = BSP_UART_COM2; */ |
---|
64 | int BSPBaseBaud = 115200; |
---|
65 | |
---|
66 | extern BSP_polling_getchar_function_type BSP_poll_char; |
---|
67 | |
---|
68 | /*-------------------------------------------------------------------------+ |
---|
69 | | External Prototypes |
---|
70 | +--------------------------------------------------------------------------*/ |
---|
71 | extern void _IBMPC_keyboard_isr(rtems_vector_number); |
---|
72 | extern rtems_boolean _IBMPC_scankey(char *); /* defined in 'inch.c' */ |
---|
73 | extern char BSP_wait_polled_input(void); |
---|
74 | extern void _IBMPC_initVideo(void); |
---|
75 | |
---|
76 | static int conSetAttr(int minor, const struct termios *); |
---|
77 | static void isr_on(const rtems_irq_connect_data *); |
---|
78 | static void isr_off(const rtems_irq_connect_data *); |
---|
79 | static int isr_is_on(const rtems_irq_connect_data *); |
---|
80 | |
---|
81 | |
---|
82 | static rtems_irq_connect_data console_isr_data = {BSP_KEYBOARD, |
---|
83 | _IBMPC_keyboard_isr, |
---|
84 | isr_on, |
---|
85 | isr_off, |
---|
86 | isr_is_on}; |
---|
87 | |
---|
88 | static void |
---|
89 | isr_on(const rtems_irq_connect_data *unused) |
---|
90 | { |
---|
91 | return; |
---|
92 | } |
---|
93 | |
---|
94 | static void |
---|
95 | isr_off(const rtems_irq_connect_data *unused) |
---|
96 | { |
---|
97 | return; |
---|
98 | } |
---|
99 | |
---|
100 | static int |
---|
101 | isr_is_on(const rtems_irq_connect_data *irq) |
---|
102 | { |
---|
103 | return BSP_irq_enabled_at_i8259s(irq->name); |
---|
104 | } |
---|
105 | |
---|
106 | void console_reserve_resources(rtems_configuration_table *conf) |
---|
107 | { |
---|
108 | if(BSPConsolePort != BSP_CONSOLE_PORT_CONSOLE) |
---|
109 | { |
---|
110 | rtems_termios_reserve_resources(conf, 1); |
---|
111 | } |
---|
112 | |
---|
113 | return; |
---|
114 | } |
---|
115 | |
---|
116 | void __assert (const char *file, int line, const char *msg) |
---|
117 | { |
---|
118 | static char exit_msg[] = "EXECUTIVE SHUTDOWN! Any key to reboot..."; |
---|
119 | unsigned char ch; |
---|
120 | |
---|
121 | /* |
---|
122 | * Note we cannot call exit or printf from here, |
---|
123 | * assert can fail inside ISR too |
---|
124 | */ |
---|
125 | |
---|
126 | /* |
---|
127 | * Close console |
---|
128 | */ |
---|
129 | close(2); |
---|
130 | close(1); |
---|
131 | close(0); |
---|
132 | |
---|
133 | printk("\nassert failed: %s: ", file); |
---|
134 | printk("%d: ", line); |
---|
135 | printk("%s\n\n", msg); |
---|
136 | printk(exit_msg); |
---|
137 | ch = BSP_poll_char(); |
---|
138 | printk("\n\n"); |
---|
139 | rtemsReboot(); |
---|
140 | |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | /*-------------------------------------------------------------------------+ |
---|
145 | | Console device driver INITIALIZE entry point. |
---|
146 | +--------------------------------------------------------------------------+ |
---|
147 | | Initilizes the I/O console (keyboard + VGA display) driver. |
---|
148 | +--------------------------------------------------------------------------*/ |
---|
149 | rtems_device_driver |
---|
150 | console_initialize(rtems_device_major_number major, |
---|
151 | rtems_device_minor_number minor, |
---|
152 | void *arg) |
---|
153 | { |
---|
154 | rtems_status_code status; |
---|
155 | |
---|
156 | /* |
---|
157 | * The video was initialized in the start.s code and does not need |
---|
158 | * to be reinitialized. |
---|
159 | */ |
---|
160 | |
---|
161 | |
---|
162 | if(BSPConsolePort == BSP_CONSOLE_PORT_CONSOLE) |
---|
163 | { |
---|
164 | /* Install keyboard interrupt handler */ |
---|
165 | status = BSP_install_rtems_irq_handler(&console_isr_data); |
---|
166 | |
---|
167 | if (!status) |
---|
168 | { |
---|
169 | printk("Error installing keyboard interrupt handler!\n"); |
---|
170 | rtems_fatal_error_occurred(status); |
---|
171 | } |
---|
172 | |
---|
173 | status = rtems_io_register_name("/dev/console", major, 0); |
---|
174 | if (status != RTEMS_SUCCESSFUL) |
---|
175 | { |
---|
176 | printk("Error registering console device!\n"); |
---|
177 | rtems_fatal_error_occurred(status); |
---|
178 | } |
---|
179 | printk("Initialized console on port CONSOLE\n\n"); |
---|
180 | } |
---|
181 | else |
---|
182 | { |
---|
183 | /* |
---|
184 | * Set up TERMIOS |
---|
185 | */ |
---|
186 | rtems_termios_initialize (); |
---|
187 | |
---|
188 | /* |
---|
189 | * Do device-specific initialization |
---|
190 | */ |
---|
191 | |
---|
192 | /* 9600-8-N-1 */ |
---|
193 | BSP_uart_init(BSPConsolePort, 9600, 0); |
---|
194 | |
---|
195 | |
---|
196 | /* Set interrupt handler */ |
---|
197 | if(BSPConsolePort == BSP_UART_COM1) |
---|
198 | { |
---|
199 | console_isr_data.name = BSP_UART_COM1_IRQ; |
---|
200 | console_isr_data.hdl = BSP_uart_termios_isr_com1; |
---|
201 | |
---|
202 | } |
---|
203 | else |
---|
204 | { |
---|
205 | assert(BSPConsolePort == BSP_UART_COM2); |
---|
206 | console_isr_data.name = BSP_UART_COM2_IRQ; |
---|
207 | console_isr_data.hdl = BSP_uart_termios_isr_com2; |
---|
208 | } |
---|
209 | |
---|
210 | status = BSP_install_rtems_irq_handler(&console_isr_data); |
---|
211 | |
---|
212 | if (!status){ |
---|
213 | printk("Error installing serial console interrupt handler!\n"); |
---|
214 | rtems_fatal_error_occurred(status); |
---|
215 | } |
---|
216 | /* |
---|
217 | * Register the device |
---|
218 | */ |
---|
219 | status = rtems_io_register_name ("/dev/console", major, 0); |
---|
220 | if (status != RTEMS_SUCCESSFUL) |
---|
221 | { |
---|
222 | printk("Error registering console device!\n"); |
---|
223 | rtems_fatal_error_occurred (status); |
---|
224 | } |
---|
225 | |
---|
226 | if(BSPConsolePort == BSP_UART_COM1) |
---|
227 | { |
---|
228 | printk("Initialized console on port COM1 9600-8-N-1\n\n"); |
---|
229 | } |
---|
230 | else |
---|
231 | { |
---|
232 | printk("Initialized console on port COM2 9600-8-N-1\n\n"); |
---|
233 | } |
---|
234 | |
---|
235 | if(BSPPrintkPort == BSP_UART_COM1) |
---|
236 | { |
---|
237 | printk("Warning : This will be the last message on console\n"); |
---|
238 | |
---|
239 | /* |
---|
240 | * FIXME: cast below defeats the very idea of having |
---|
241 | * function pointer types defined |
---|
242 | */ |
---|
243 | BSP_output_char = (BSP_output_char_function_type) |
---|
244 | BSP_output_char_via_serial; |
---|
245 | BSP_poll_char = (BSP_polling_getchar_function_type) |
---|
246 | BSP_poll_char_via_serial; |
---|
247 | } |
---|
248 | else if(BSPPrintkPort != BSP_CONSOLE_PORT_CONSOLE) |
---|
249 | { |
---|
250 | printk("illegal assignement of projtk channel"); |
---|
251 | rtems_fatal_error_occurred (status); |
---|
252 | } |
---|
253 | |
---|
254 | } |
---|
255 | return RTEMS_SUCCESSFUL; |
---|
256 | } /* console_initialize */ |
---|
257 | |
---|
258 | |
---|
259 | static int console_open_count = 0; |
---|
260 | |
---|
261 | static int console_last_close(int major, int minor, void *arg) |
---|
262 | { |
---|
263 | BSP_remove_rtems_irq_handler (&console_isr_data); |
---|
264 | |
---|
265 | return 0; |
---|
266 | } |
---|
267 | |
---|
268 | /*-------------------------------------------------------------------------+ |
---|
269 | | Console device driver OPEN entry point |
---|
270 | +--------------------------------------------------------------------------*/ |
---|
271 | rtems_device_driver |
---|
272 | console_open(rtems_device_major_number major, |
---|
273 | rtems_device_minor_number minor, |
---|
274 | void *arg) |
---|
275 | { |
---|
276 | rtems_status_code status; |
---|
277 | static rtems_termios_callbacks cb = |
---|
278 | { |
---|
279 | NULL, /* firstOpen */ |
---|
280 | console_last_close, /* lastClose */ |
---|
281 | NULL, /* pollRead */ |
---|
282 | BSP_uart_termios_write_com1, /* write */ |
---|
283 | conSetAttr, /* setAttributes */ |
---|
284 | NULL, /* stopRemoteTx */ |
---|
285 | NULL, /* startRemoteTx */ |
---|
286 | 1 /* outputUsesInterrupts */ |
---|
287 | }; |
---|
288 | |
---|
289 | if(BSPConsolePort == BSP_CONSOLE_PORT_CONSOLE) |
---|
290 | { |
---|
291 | ++console_open_count; |
---|
292 | return RTEMS_SUCCESSFUL; |
---|
293 | } |
---|
294 | |
---|
295 | if(BSPConsolePort == BSP_UART_COM2) |
---|
296 | { |
---|
297 | cb.write = BSP_uart_termios_write_com2; |
---|
298 | } |
---|
299 | |
---|
300 | status = rtems_termios_open (major, minor, arg, &cb); |
---|
301 | |
---|
302 | if(status != RTEMS_SUCCESSFUL) |
---|
303 | { |
---|
304 | printk("Error openning console device\n"); |
---|
305 | return status; |
---|
306 | } |
---|
307 | |
---|
308 | /* |
---|
309 | * Pass data area info down to driver |
---|
310 | */ |
---|
311 | BSP_uart_termios_set(BSPConsolePort, |
---|
312 | ((rtems_libio_open_close_args_t *)arg)->iop->data1); |
---|
313 | |
---|
314 | /* Enable interrupts on channel */ |
---|
315 | BSP_uart_intr_ctrl(BSPConsolePort, BSP_UART_INTR_CTRL_TERMIOS); |
---|
316 | |
---|
317 | return RTEMS_SUCCESSFUL; |
---|
318 | } |
---|
319 | |
---|
320 | /*-------------------------------------------------------------------------+ |
---|
321 | | Console device driver CLOSE entry point |
---|
322 | +--------------------------------------------------------------------------*/ |
---|
323 | rtems_device_driver |
---|
324 | console_close(rtems_device_major_number major, |
---|
325 | rtems_device_minor_number minor, |
---|
326 | void *arg) |
---|
327 | { |
---|
328 | rtems_device_driver res = RTEMS_SUCCESSFUL; |
---|
329 | |
---|
330 | if(BSPConsolePort != BSP_CONSOLE_PORT_CONSOLE) |
---|
331 | { |
---|
332 | res = rtems_termios_close (arg); |
---|
333 | } |
---|
334 | else { |
---|
335 | if (--console_open_count == 0) { |
---|
336 | console_last_close(major, minor, arg); |
---|
337 | } |
---|
338 | } |
---|
339 | |
---|
340 | return res; |
---|
341 | } /* console_close */ |
---|
342 | |
---|
343 | |
---|
344 | /*-------------------------------------------------------------------------+ |
---|
345 | | Console device driver READ entry point. |
---|
346 | +--------------------------------------------------------------------------+ |
---|
347 | | Read characters from the I/O console. We only have stdin. |
---|
348 | +--------------------------------------------------------------------------*/ |
---|
349 | rtems_device_driver |
---|
350 | console_read(rtems_device_major_number major, |
---|
351 | rtems_device_minor_number minor, |
---|
352 | void *arg) |
---|
353 | { |
---|
354 | rtems_libio_rw_args_t *rw_args = (rtems_libio_rw_args_t *)arg; |
---|
355 | char *buffer = rw_args->buffer; |
---|
356 | int count, maximum = rw_args->count; |
---|
357 | |
---|
358 | if(BSPConsolePort != BSP_CONSOLE_PORT_CONSOLE) |
---|
359 | { |
---|
360 | return rtems_termios_read (arg); |
---|
361 | } |
---|
362 | |
---|
363 | for (count = 0; count < maximum; count++) |
---|
364 | { |
---|
365 | /* Get character */ |
---|
366 | buffer[count] = _IBMPC_inch_sleep(); |
---|
367 | |
---|
368 | /* Echo character to screen */ |
---|
369 | _IBMPC_outch(buffer[count]); |
---|
370 | if (buffer[count] == '\r') |
---|
371 | { |
---|
372 | _IBMPC_outch('\n'); /* CR = CR + LF */ |
---|
373 | } |
---|
374 | |
---|
375 | if (buffer[count] == '\n' || buffer[count] == '\r') |
---|
376 | { |
---|
377 | /* What if this goes past the end of the buffer? We're hosed. [bhc] */ |
---|
378 | buffer[count++] = '\n'; |
---|
379 | buffer[count] = '\0'; |
---|
380 | break; |
---|
381 | } |
---|
382 | } |
---|
383 | |
---|
384 | rw_args->bytes_moved = count; |
---|
385 | return ((count >= 0) ? RTEMS_SUCCESSFUL : RTEMS_UNSATISFIED); |
---|
386 | } /* console_read */ |
---|
387 | |
---|
388 | |
---|
389 | /*-------------------------------------------------------------------------+ |
---|
390 | | Console device driver WRITE entry point. |
---|
391 | +--------------------------------------------------------------------------+ |
---|
392 | | Write characters to the I/O console. Stderr and stdout are the same. |
---|
393 | +--------------------------------------------------------------------------*/ |
---|
394 | rtems_device_driver |
---|
395 | console_write(rtems_device_major_number major, |
---|
396 | rtems_device_minor_number minor, |
---|
397 | void * arg) |
---|
398 | { |
---|
399 | rtems_libio_rw_args_t *rw_args = (rtems_libio_rw_args_t *)arg; |
---|
400 | char *buffer = rw_args->buffer; |
---|
401 | int count, maximum = rw_args->count; |
---|
402 | |
---|
403 | if(BSPConsolePort != BSP_CONSOLE_PORT_CONSOLE) |
---|
404 | { |
---|
405 | return rtems_termios_write (arg); |
---|
406 | } |
---|
407 | |
---|
408 | for (count = 0; count < maximum; count++) |
---|
409 | { |
---|
410 | _IBMPC_outch(buffer[count]); |
---|
411 | if (buffer[count] == '\n') |
---|
412 | _IBMPC_outch('\r'); /* LF = LF + CR */ |
---|
413 | } |
---|
414 | |
---|
415 | rw_args->bytes_moved = maximum; |
---|
416 | return RTEMS_SUCCESSFUL; |
---|
417 | } /* console_write */ |
---|
418 | |
---|
419 | |
---|
420 | |
---|
421 | /* |
---|
422 | * Handle ioctl request. |
---|
423 | */ |
---|
424 | rtems_device_driver |
---|
425 | console_control(rtems_device_major_number major, |
---|
426 | rtems_device_minor_number minor, |
---|
427 | void * arg |
---|
428 | ) |
---|
429 | { |
---|
430 | if(BSPConsolePort != BSP_CONSOLE_PORT_CONSOLE) |
---|
431 | { |
---|
432 | return rtems_termios_ioctl (arg); |
---|
433 | } |
---|
434 | |
---|
435 | return RTEMS_SUCCESSFUL; |
---|
436 | } |
---|
437 | |
---|
438 | static int |
---|
439 | conSetAttr(int minor, const struct termios *t) |
---|
440 | { |
---|
441 | int baud; |
---|
442 | |
---|
443 | switch (t->c_cflag & CBAUD) |
---|
444 | { |
---|
445 | case B50: |
---|
446 | baud = 50; |
---|
447 | break; |
---|
448 | case B75: |
---|
449 | baud = 75; |
---|
450 | break; |
---|
451 | case B110: |
---|
452 | baud = 110; |
---|
453 | break; |
---|
454 | case B134: |
---|
455 | baud = 134; |
---|
456 | break; |
---|
457 | case B150: |
---|
458 | baud = 150; |
---|
459 | break; |
---|
460 | case B200: |
---|
461 | baud = 200; |
---|
462 | break; |
---|
463 | case B300: |
---|
464 | baud = 300; |
---|
465 | break; |
---|
466 | case B600: |
---|
467 | baud = 600; |
---|
468 | break; |
---|
469 | case B1200: |
---|
470 | baud = 1200; |
---|
471 | break; |
---|
472 | case B1800: |
---|
473 | baud = 1800; |
---|
474 | break; |
---|
475 | case B2400: |
---|
476 | baud = 2400; |
---|
477 | break; |
---|
478 | case B4800: |
---|
479 | baud = 4800; |
---|
480 | break; |
---|
481 | case B9600: |
---|
482 | baud = 9600; |
---|
483 | break; |
---|
484 | case B19200: |
---|
485 | baud = 19200; |
---|
486 | break; |
---|
487 | case B38400: |
---|
488 | baud = 38400; |
---|
489 | break; |
---|
490 | case B57600: |
---|
491 | baud = 57600; |
---|
492 | break; |
---|
493 | case B115200: |
---|
494 | baud = 115200; |
---|
495 | break; |
---|
496 | default: |
---|
497 | baud = 0; |
---|
498 | rtems_fatal_error_occurred (RTEMS_INTERNAL_ERROR); |
---|
499 | return 0; |
---|
500 | } |
---|
501 | |
---|
502 | BSP_uart_set_baud(BSPConsolePort, baud); |
---|
503 | |
---|
504 | return 0; |
---|
505 | } |
---|
506 | |
---|
507 | /* |
---|
508 | * BSP initialization |
---|
509 | */ |
---|
510 | |
---|
511 | BSP_output_char_function_type BSP_output_char = |
---|
512 | (BSP_output_char_function_type) _IBMPC_outch; |
---|
513 | |
---|
514 | BSP_polling_getchar_function_type BSP_poll_char = BSP_wait_polled_input; |
---|
515 | |
---|
516 | |
---|