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