[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> |
---|
| 35 | #include <termios.h> |
---|
| 36 | #include <bsp/uart.h> |
---|
[69ed59f] | 37 | #include <rtems/bspIo.h> /* printk */ |
---|
[ba46ffa6] | 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 | */ |
---|
[69ed59f] | 46 | int BSPConsolePort = BSP_CONSOLE_PORT; |
---|
[ba46ffa6] | 47 | |
---|
[69ed59f] | 48 | int BSPBaseBaud = BSP_UART_BAUD_BASE; |
---|
[ba46ffa6] | 49 | |
---|
| 50 | /*-------------------------------------------------------------------------+ |
---|
| 51 | | External Prototypes |
---|
| 52 | +--------------------------------------------------------------------------*/ |
---|
| 53 | |
---|
| 54 | static int conSetAttr(int minor, const struct termios *); |
---|
| 55 | |
---|
[69ed59f] | 56 | typedef struct TtySTblRec_ { |
---|
[2d0d029] | 57 | char *name; |
---|
| 58 | rtems_irq_hdl isr; |
---|
| 59 | } TtySTblRec, *TtySTbl; |
---|
[69ed59f] | 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 | |
---|
[ba46ffa6] | 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 (); |
---|
[6128a4a] | 99 | |
---|
[ba46ffa6] | 100 | /* |
---|
| 101 | * Do device-specific initialization |
---|
| 102 | */ |
---|
| 103 | |
---|
[69ed59f] | 104 | /* RTEMS calls this routine once with 'minor'==0; loop through |
---|
| 105 | * all known instances... |
---|
[ba46ffa6] | 106 | */ |
---|
[6128a4a] | 107 | |
---|
[69ed59f] | 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 | { |
---|
[0d6849e7] | 121 | printk("Registering /dev/console as minor %d (==%s)\n", |
---|
[69ed59f] | 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 | } |
---|
[e79a1947] | 136 | |
---|
[ba46ffa6] | 137 | return RTEMS_SUCCESSFUL; |
---|
| 138 | } /* console_initialize */ |
---|
| 139 | |
---|
[69ed59f] | 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 | |
---|
[ba46ffa6] | 159 | static int console_last_close(int major, int minor, void *arg) |
---|
| 160 | { |
---|
[69ed59f] | 161 | BSP_uart_remove_isr(minor, ttyS[minor].isr); |
---|
[ba46ffa6] | 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; |
---|
[6128a4a] | 174 | static rtems_termios_callbacks cb = |
---|
[e79a1947] | 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 */ |
---|
[ba46ffa6] | 185 | }; |
---|
[e79a1947] | 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 |
---|
[ba46ffa6] | 198 | |
---|
| 199 | status = rtems_termios_open (major, minor, arg, &cb); |
---|
| 200 | |
---|
| 201 | if(status != RTEMS_SUCCESSFUL) |
---|
| 202 | { |
---|
[e79a1947] | 203 | printk("Error opening console device\n"); |
---|
[ba46ffa6] | 204 | return status; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | /* |
---|
| 208 | * Pass data area info down to driver |
---|
| 209 | */ |
---|
[6128a4a] | 210 | BSP_uart_termios_set(minor, |
---|
[ba46ffa6] | 211 | ((rtems_libio_open_close_args_t *)arg)->iop->data1); |
---|
| 212 | /* Enable interrupts on channel */ |
---|
[69ed59f] | 213 | BSP_uart_intr_ctrl(minor, BSP_UART_INTR_CTRL_TERMIOS); |
---|
[ba46ffa6] | 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); |
---|
[6128a4a] | 229 | |
---|
[ba46ffa6] | 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 */ |
---|
[6128a4a] | 246 | |
---|
[ba46ffa6] | 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); |
---|
[6128a4a] | 259 | |
---|
[ba46ffa6] | 260 | } /* console_write */ |
---|
| 261 | |
---|
| 262 | /* |
---|
| 263 | * Handle ioctl request. |
---|
| 264 | */ |
---|
[6128a4a] | 265 | rtems_device_driver |
---|
[4f3e4f33] | 266 | console_control(rtems_device_major_number major, |
---|
| 267 | rtems_device_minor_number minor, |
---|
| 268 | void *arg |
---|
[ba46ffa6] | 269 | ) |
---|
[6128a4a] | 270 | { |
---|
[4f3e4f33] | 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); |
---|
[6128a4a] | 279 | |
---|
[4f3e4f33] | 280 | default: |
---|
| 281 | break; |
---|
| 282 | } |
---|
| 283 | #endif |
---|
[ba46ffa6] | 284 | return rtems_termios_ioctl (arg); |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | static int |
---|
| 288 | conSetAttr(int minor, const struct termios *t) |
---|
| 289 | { |
---|
| 290 | int baud; |
---|
| 291 | |
---|
[6128a4a] | 292 | switch (t->c_cflag & CBAUD) |
---|
[ba46ffa6] | 293 | { |
---|
[6128a4a] | 294 | case B50: |
---|
[ba46ffa6] | 295 | baud = 50; |
---|
| 296 | break; |
---|
[6128a4a] | 297 | case B75: |
---|
| 298 | baud = 75; |
---|
[ba46ffa6] | 299 | break; |
---|
[6128a4a] | 300 | case B110: |
---|
| 301 | baud = 110; |
---|
[ba46ffa6] | 302 | break; |
---|
[6128a4a] | 303 | case B134: |
---|
| 304 | baud = 134; |
---|
[ba46ffa6] | 305 | break; |
---|
[6128a4a] | 306 | case B150: |
---|
| 307 | baud = 150; |
---|
[ba46ffa6] | 308 | break; |
---|
| 309 | case B200: |
---|
[6128a4a] | 310 | baud = 200; |
---|
[ba46ffa6] | 311 | break; |
---|
[6128a4a] | 312 | case B300: |
---|
[ba46ffa6] | 313 | baud = 300; |
---|
| 314 | break; |
---|
[6128a4a] | 315 | case B600: |
---|
| 316 | baud = 600; |
---|
[ba46ffa6] | 317 | break; |
---|
[6128a4a] | 318 | case B1200: |
---|
[ba46ffa6] | 319 | baud = 1200; |
---|
| 320 | break; |
---|
[6128a4a] | 321 | case B1800: |
---|
| 322 | baud = 1800; |
---|
[ba46ffa6] | 323 | break; |
---|
[6128a4a] | 324 | case B2400: |
---|
[ba46ffa6] | 325 | baud = 2400; |
---|
| 326 | break; |
---|
[6128a4a] | 327 | case B4800: |
---|
[ba46ffa6] | 328 | baud = 4800; |
---|
| 329 | break; |
---|
[6128a4a] | 330 | case B9600: |
---|
[ba46ffa6] | 331 | baud = 9600; |
---|
| 332 | break; |
---|
| 333 | case B19200: |
---|
| 334 | baud = 19200; |
---|
| 335 | break; |
---|
| 336 | case B38400: |
---|
| 337 | baud = 38400; |
---|
| 338 | break; |
---|
[6128a4a] | 339 | case B57600: |
---|
[ba46ffa6] | 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 | |
---|
[69ed59f] | 351 | BSP_uart_set_baud(minor, baud); |
---|
[ba46ffa6] | 352 | |
---|
| 353 | return 0; |
---|
| 354 | } |
---|