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