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.rtems.com/license/LICENSE. |
---|
28 | | ************************************************************************** |
---|
29 | | |
---|
30 | | $Id$ |
---|
31 | +--------------------------------------------------------------------------*/ |
---|
32 | |
---|
33 | #include <stdio.h> |
---|
34 | #include <stdlib.h> |
---|
35 | #include <assert.h> |
---|
36 | #include <unistd.h> |
---|
37 | |
---|
38 | #include <bsp.h> |
---|
39 | #include <bsp/irq.h> |
---|
40 | #include <rtems/libio.h> |
---|
41 | #include <termios.h> |
---|
42 | #include <uart.h> |
---|
43 | #include <libcpu/cpuModel.h> |
---|
44 | |
---|
45 | #include <rtems/mw_uid.h> |
---|
46 | #include "mouse_parser.h" |
---|
47 | |
---|
48 | /* |
---|
49 | * Possible value for console input/output : |
---|
50 | * BSP_CONSOLE_PORT_CONSOLE |
---|
51 | * BSP_UART_COM1 |
---|
52 | * BSP_UART_COM2 |
---|
53 | * |
---|
54 | * Note: |
---|
55 | * 1. Currently BSPPrintkPort, cannot be assigned to COM2, |
---|
56 | * it will be fixed soon. |
---|
57 | * |
---|
58 | * 2. If both BSPConsolePort and BSPPrintkport are assigned |
---|
59 | * to same serial device it does not work that great |
---|
60 | */ |
---|
61 | |
---|
62 | #if (USE_COM1_AS_CONSOLE == 1) |
---|
63 | int BSPConsolePort = BSP_UART_COM1; |
---|
64 | int BSPPrintkPort = BSP_UART_COM1; |
---|
65 | #else |
---|
66 | int BSPConsolePort = BSP_CONSOLE_PORT_CONSOLE; |
---|
67 | int BSPPrintkPort = BSP_CONSOLE_PORT_CONSOLE; |
---|
68 | #endif |
---|
69 | |
---|
70 | int BSPBaseBaud = 115200; |
---|
71 | |
---|
72 | extern BSP_polling_getchar_function_type BSP_poll_char; |
---|
73 | extern int getch( void ); |
---|
74 | extern void kbd_init( void ); |
---|
75 | |
---|
76 | /*-------------------------------------------------------------------------+ |
---|
77 | | External Prototypes |
---|
78 | +--------------------------------------------------------------------------*/ |
---|
79 | extern void keyboard_interrupt(void ); |
---|
80 | extern void keyboard_interrupt_wrapper(void *); |
---|
81 | extern char BSP_wait_polled_input(void); |
---|
82 | extern void _IBMPC_initVideo(void); |
---|
83 | |
---|
84 | static int conSetAttr(int minor, const struct termios *); |
---|
85 | static void isr_on(const rtems_irq_connect_data *); |
---|
86 | static void isr_off(const rtems_irq_connect_data *); |
---|
87 | static int isr_is_on(const rtems_irq_connect_data *); |
---|
88 | |
---|
89 | extern int rtems_kbpoll( void ); |
---|
90 | |
---|
91 | static rtems_irq_connect_data console_isr_data = {BSP_KEYBOARD, |
---|
92 | keyboard_interrupt_wrapper, |
---|
93 | 0, |
---|
94 | isr_on, |
---|
95 | isr_off, |
---|
96 | isr_is_on}; |
---|
97 | |
---|
98 | static void |
---|
99 | isr_on(const rtems_irq_connect_data *unused) |
---|
100 | { |
---|
101 | return; |
---|
102 | } |
---|
103 | |
---|
104 | static void |
---|
105 | isr_off(const rtems_irq_connect_data *unused) |
---|
106 | { |
---|
107 | return; |
---|
108 | } |
---|
109 | |
---|
110 | static int |
---|
111 | isr_is_on(const rtems_irq_connect_data *irq) |
---|
112 | { |
---|
113 | return BSP_irq_enabled_at_i8259s(irq->name); |
---|
114 | } |
---|
115 | |
---|
116 | extern char _IBMPC_inch(void); |
---|
117 | extern int rtems_kbpoll( void ); |
---|
118 | |
---|
119 | static int |
---|
120 | ibmpc_console_write(int minor, const char *buf, int len) |
---|
121 | { |
---|
122 | int count; |
---|
123 | for (count = 0; count < len; count++) |
---|
124 | { |
---|
125 | _IBMPC_outch( buf[ count ] ); |
---|
126 | if( buf[ count ] == '\n') |
---|
127 | _IBMPC_outch( '\r' ); /* LF = LF + CR */ |
---|
128 | } |
---|
129 | return 0; |
---|
130 | } |
---|
131 | |
---|
132 | int kbd_poll_read( int minor ) |
---|
133 | { |
---|
134 | if( rtems_kbpoll() ) |
---|
135 | { |
---|
136 | int c = getch(); |
---|
137 | return c; |
---|
138 | } |
---|
139 | return -1; |
---|
140 | } |
---|
141 | |
---|
142 | /*-------------------------------------------------------------------------+ |
---|
143 | | Console device driver INITIALIZE entry point. |
---|
144 | +--------------------------------------------------------------------------+ |
---|
145 | | Initilizes the I/O console (keyboard + VGA display) driver. |
---|
146 | +--------------------------------------------------------------------------*/ |
---|
147 | rtems_device_driver |
---|
148 | console_initialize(rtems_device_major_number major, |
---|
149 | rtems_device_minor_number minor, |
---|
150 | void *arg) |
---|
151 | { |
---|
152 | rtems_status_code status; |
---|
153 | |
---|
154 | /* Initialize the KBD interface */ |
---|
155 | kbd_init(); |
---|
156 | |
---|
157 | /* |
---|
158 | * Set up TERMIOS |
---|
159 | */ |
---|
160 | rtems_termios_initialize (); |
---|
161 | |
---|
162 | #ifdef RTEMS_RUNTIME_CONSOLE_SELECT |
---|
163 | /* |
---|
164 | * If no video card, fall back to serial port console |
---|
165 | */ |
---|
166 | #include <crt.h> |
---|
167 | if((BSPConsolePort == BSP_CONSOLE_PORT_CONSOLE) |
---|
168 | && (*(unsigned char*) NB_MAX_ROW_ADDR == 0) |
---|
169 | && (*(unsigned short*)NB_MAX_COL_ADDR == 0)) { |
---|
170 | BSPConsolePort = BSP_UART_COM2; |
---|
171 | BSPPrintkPort = BSP_UART_COM1; |
---|
172 | } |
---|
173 | #endif |
---|
174 | |
---|
175 | /* |
---|
176 | * The video was initialized in the start.s code and does not need |
---|
177 | * to be reinitialized. |
---|
178 | */ |
---|
179 | |
---|
180 | if(BSPConsolePort == BSP_CONSOLE_PORT_CONSOLE) |
---|
181 | { |
---|
182 | /* Install keyboard interrupt handler */ |
---|
183 | status = BSP_install_rtems_irq_handler(&console_isr_data); |
---|
184 | |
---|
185 | if (!status) |
---|
186 | { |
---|
187 | printk("Error installing keyboard interrupt handler!\n"); |
---|
188 | rtems_fatal_error_occurred(status); |
---|
189 | } |
---|
190 | |
---|
191 | status = rtems_io_register_name("/dev/console", major, 0); |
---|
192 | if (status != RTEMS_SUCCESSFUL) |
---|
193 | { |
---|
194 | printk("Error registering console device!\n"); |
---|
195 | rtems_fatal_error_occurred(status); |
---|
196 | } |
---|
197 | printk("Initialized console on port CONSOLE\n\n"); |
---|
198 | } |
---|
199 | else |
---|
200 | { |
---|
201 | /* |
---|
202 | * Do device-specific initialization |
---|
203 | */ |
---|
204 | /* 9600-8-N-1 */ |
---|
205 | BSP_uart_init(BSPConsolePort, 9600, CHR_8_BITS, 0, 0, 0); |
---|
206 | |
---|
207 | /* Set interrupt handler */ |
---|
208 | if(BSPConsolePort == BSP_UART_COM1) |
---|
209 | { |
---|
210 | console_isr_data.name = BSP_UART_COM1_IRQ; |
---|
211 | console_isr_data.hdl = BSP_uart_termios_isr_com1; |
---|
212 | |
---|
213 | } |
---|
214 | else |
---|
215 | { |
---|
216 | assert(BSPConsolePort == BSP_UART_COM2); |
---|
217 | console_isr_data.name = BSP_UART_COM2_IRQ; |
---|
218 | console_isr_data.hdl = BSP_uart_termios_isr_com2; |
---|
219 | } |
---|
220 | status = BSP_install_rtems_irq_handler(&console_isr_data); |
---|
221 | |
---|
222 | if (!status){ |
---|
223 | printk("Error installing serial console interrupt handler!\n"); |
---|
224 | rtems_fatal_error_occurred(status); |
---|
225 | } |
---|
226 | /* |
---|
227 | * Register the device |
---|
228 | */ |
---|
229 | status = rtems_io_register_name ("/dev/console", major, 0); |
---|
230 | if (status != RTEMS_SUCCESSFUL) |
---|
231 | { |
---|
232 | printk("Error registering console device!\n"); |
---|
233 | rtems_fatal_error_occurred (status); |
---|
234 | } |
---|
235 | |
---|
236 | if(BSPConsolePort == BSP_UART_COM1) |
---|
237 | { |
---|
238 | printk("Initialized console on port COM1 9600-8-N-1\n\n"); |
---|
239 | } |
---|
240 | else |
---|
241 | { |
---|
242 | printk("Initialized console on port COM2 9600-8-N-1\n\n"); |
---|
243 | } |
---|
244 | |
---|
245 | if(BSPPrintkPort == BSP_UART_COM1) |
---|
246 | { |
---|
247 | printk("Warning : This will be the last message on console\n"); |
---|
248 | |
---|
249 | /* |
---|
250 | * FIXME: cast below defeats the very idea of having |
---|
251 | * function pointer types defined |
---|
252 | */ |
---|
253 | BSP_output_char = (BSP_output_char_function_type) |
---|
254 | BSP_output_char_via_serial; |
---|
255 | BSP_poll_char = (BSP_polling_getchar_function_type) |
---|
256 | BSP_poll_char_via_serial; |
---|
257 | } |
---|
258 | else if(BSPPrintkPort != BSP_CONSOLE_PORT_CONSOLE) |
---|
259 | { |
---|
260 | printk("illegal assignement of printk channel"); |
---|
261 | rtems_fatal_error_occurred (status); |
---|
262 | } |
---|
263 | |
---|
264 | } |
---|
265 | return RTEMS_SUCCESSFUL; |
---|
266 | } /* console_initialize */ |
---|
267 | |
---|
268 | static int console_open_count = 0; |
---|
269 | |
---|
270 | static int console_last_close(int major, int minor, void *arg) |
---|
271 | { |
---|
272 | BSP_remove_rtems_irq_handler (&console_isr_data); |
---|
273 | |
---|
274 | return 0; |
---|
275 | } |
---|
276 | |
---|
277 | /*-------------------------------------------------------------------------+ |
---|
278 | | Console device driver OPEN entry point |
---|
279 | +--------------------------------------------------------------------------*/ |
---|
280 | rtems_device_driver |
---|
281 | console_open(rtems_device_major_number major, |
---|
282 | rtems_device_minor_number minor, |
---|
283 | void *arg) |
---|
284 | { |
---|
285 | rtems_status_code status; |
---|
286 | static rtems_termios_callbacks cb = |
---|
287 | { |
---|
288 | NULL, /* firstOpen */ |
---|
289 | console_last_close, /* lastClose */ |
---|
290 | NULL, /* pollRead */ |
---|
291 | BSP_uart_termios_write_com1, /* write */ |
---|
292 | conSetAttr, /* setAttributes */ |
---|
293 | NULL, /* stopRemoteTx */ |
---|
294 | NULL, /* startRemoteTx */ |
---|
295 | 1 /* outputUsesInterrupts */ |
---|
296 | }; |
---|
297 | |
---|
298 | if(BSPConsolePort == BSP_CONSOLE_PORT_CONSOLE) |
---|
299 | { |
---|
300 | |
---|
301 | /* Let's set the routines for termios to poll the |
---|
302 | * Kbd queue for data |
---|
303 | */ |
---|
304 | cb.pollRead = kbd_poll_read; |
---|
305 | cb.outputUsesInterrupts = 0; |
---|
306 | /* write the "echo" if it is on */ |
---|
307 | cb.write = ibmpc_console_write; |
---|
308 | |
---|
309 | cb.setAttributes = NULL; |
---|
310 | ++console_open_count; |
---|
311 | status = rtems_termios_open (major, minor, arg, &cb); |
---|
312 | if(status != RTEMS_SUCCESSFUL) |
---|
313 | { |
---|
314 | printk("Error openning console device\n"); |
---|
315 | } |
---|
316 | return status; |
---|
317 | } |
---|
318 | |
---|
319 | if(BSPConsolePort == BSP_UART_COM2) |
---|
320 | { |
---|
321 | cb.write = BSP_uart_termios_write_com2; |
---|
322 | } |
---|
323 | |
---|
324 | status = rtems_termios_open (major, minor, arg, &cb); |
---|
325 | |
---|
326 | if(status != RTEMS_SUCCESSFUL) |
---|
327 | { |
---|
328 | printk("Error openning console device\n"); |
---|
329 | return status; |
---|
330 | } |
---|
331 | |
---|
332 | /* |
---|
333 | * Pass data area info down to driver |
---|
334 | */ |
---|
335 | BSP_uart_termios_set(BSPConsolePort, |
---|
336 | ((rtems_libio_open_close_args_t *)arg)->iop->data1); |
---|
337 | |
---|
338 | /* Enable interrupts on channel */ |
---|
339 | BSP_uart_intr_ctrl(BSPConsolePort, BSP_UART_INTR_CTRL_TERMIOS); |
---|
340 | |
---|
341 | return RTEMS_SUCCESSFUL; |
---|
342 | } |
---|
343 | |
---|
344 | /*-------------------------------------------------------------------------+ |
---|
345 | | Console device driver CLOSE entry point |
---|
346 | +--------------------------------------------------------------------------*/ |
---|
347 | rtems_device_driver |
---|
348 | console_close(rtems_device_major_number major, |
---|
349 | rtems_device_minor_number minor, |
---|
350 | void *arg) |
---|
351 | { |
---|
352 | return rtems_termios_close (arg); |
---|
353 | } /* console_close */ |
---|
354 | |
---|
355 | /*-------------------------------------------------------------------------+ |
---|
356 | | Console device driver READ entry point. |
---|
357 | +--------------------------------------------------------------------------+ |
---|
358 | | Read characters from the I/O console. We only have stdin. |
---|
359 | +--------------------------------------------------------------------------*/ |
---|
360 | rtems_device_driver |
---|
361 | console_read(rtems_device_major_number major, |
---|
362 | rtems_device_minor_number minor, |
---|
363 | void *arg) |
---|
364 | { |
---|
365 | return rtems_termios_read( arg ); |
---|
366 | } /* console_read */ |
---|
367 | |
---|
368 | /*-------------------------------------------------------------------------+ |
---|
369 | | Console device driver WRITE entry point. |
---|
370 | +--------------------------------------------------------------------------+ |
---|
371 | | Write characters to the I/O console. Stderr and stdout are the same. |
---|
372 | +--------------------------------------------------------------------------*/ |
---|
373 | rtems_device_driver |
---|
374 | console_write(rtems_device_major_number major, |
---|
375 | rtems_device_minor_number minor, |
---|
376 | void * arg) |
---|
377 | { |
---|
378 | rtems_libio_rw_args_t *rw_args = (rtems_libio_rw_args_t *)arg; |
---|
379 | char *buffer = rw_args->buffer; |
---|
380 | int maximum = rw_args->count; |
---|
381 | |
---|
382 | if(BSPConsolePort != BSP_CONSOLE_PORT_CONSOLE) |
---|
383 | { |
---|
384 | return rtems_termios_write (arg); |
---|
385 | } |
---|
386 | |
---|
387 | /* write data to VGA */ |
---|
388 | ibmpc_console_write( minor, buffer, maximum ); |
---|
389 | rw_args->bytes_moved = maximum; |
---|
390 | return RTEMS_SUCCESSFUL; |
---|
391 | } /* console_write */ |
---|
392 | |
---|
393 | extern int vt_ioctl( unsigned int cmd, unsigned long arg); |
---|
394 | |
---|
395 | /* |
---|
396 | * Handle ioctl request. |
---|
397 | */ |
---|
398 | rtems_device_driver |
---|
399 | console_control(rtems_device_major_number major, |
---|
400 | rtems_device_minor_number minor, |
---|
401 | void * arg |
---|
402 | ) |
---|
403 | { |
---|
404 | rtems_libio_ioctl_args_t *args = arg; |
---|
405 | switch (args->command) |
---|
406 | { |
---|
407 | default: |
---|
408 | if( vt_ioctl( args->command, (unsigned long)args->buffer ) != 0 ) |
---|
409 | return rtems_termios_ioctl (arg); |
---|
410 | break; |
---|
411 | |
---|
412 | case MW_UID_REGISTER_DEVICE: |
---|
413 | printk( "SerialMouse: reg=%s\n", args->buffer ); |
---|
414 | register_kbd_msg_queue( args->buffer, 0 ); |
---|
415 | break; |
---|
416 | |
---|
417 | case MW_UID_UNREGISTER_DEVICE: |
---|
418 | unregister_kbd_msg_queue( 0 ); |
---|
419 | break; |
---|
420 | } |
---|
421 | args->ioctl_return = 0; |
---|
422 | return RTEMS_SUCCESSFUL; |
---|
423 | } |
---|
424 | |
---|
425 | static int |
---|
426 | conSetAttr(int minor, const struct termios *t) |
---|
427 | { |
---|
428 | unsigned long baud, databits, parity, stopbits; |
---|
429 | |
---|
430 | baud = termios_baud_to_number(t->c_cflag & CBAUD); |
---|
431 | if ( baud > 115200 ) |
---|
432 | rtems_fatal_error_occurred (RTEMS_INTERNAL_ERROR); |
---|
433 | |
---|
434 | if (t->c_cflag & PARENB) { |
---|
435 | /* Parity is enabled */ |
---|
436 | if (t->c_cflag & PARODD) { |
---|
437 | /* Parity is odd */ |
---|
438 | parity = PEN; |
---|
439 | } |
---|
440 | else { |
---|
441 | /* Parity is even */ |
---|
442 | parity = PEN | EPS; |
---|
443 | } |
---|
444 | } |
---|
445 | else { |
---|
446 | /* No parity */ |
---|
447 | parity = 0; |
---|
448 | } |
---|
449 | |
---|
450 | switch (t->c_cflag & CSIZE) { |
---|
451 | case CS5: databits = CHR_5_BITS; break; |
---|
452 | case CS6: databits = CHR_6_BITS; break; |
---|
453 | case CS7: databits = CHR_7_BITS; break; |
---|
454 | default: /* just to avoid warnings -- all cases are covered. */ |
---|
455 | case CS8: databits = CHR_8_BITS; break; |
---|
456 | } |
---|
457 | |
---|
458 | if (t->c_cflag & CSTOPB) { |
---|
459 | /* 2 stop bits */ |
---|
460 | stopbits = STB; |
---|
461 | } |
---|
462 | else { |
---|
463 | /* 1 stop bit */ |
---|
464 | stopbits = 0; |
---|
465 | } |
---|
466 | |
---|
467 | BSP_uart_set_attributes(BSPConsolePort, baud, databits, parity, stopbits); |
---|
468 | |
---|
469 | return 0; |
---|
470 | } |
---|
471 | |
---|
472 | void keyboard_interrupt_wrapper(void *unused){ |
---|
473 | keyboard_interrupt(); |
---|
474 | } |
---|
475 | |
---|
476 | /* |
---|
477 | * BSP initialization |
---|
478 | */ |
---|
479 | |
---|
480 | BSP_output_char_function_type BSP_output_char = |
---|
481 | (BSP_output_char_function_type) _IBMPC_outch; |
---|
482 | |
---|
483 | BSP_polling_getchar_function_type BSP_poll_char = BSP_wait_polled_input; |
---|