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