[55e1322] | 1 | /* |
---|
| 2 | * TERMIOS serial line support |
---|
| 3 | * |
---|
[d24ceb3] | 4 | * Author: |
---|
[55e1322] | 5 | * W. Eric Norum |
---|
| 6 | * Saskatchewan Accelerator Laboratory |
---|
| 7 | * University of Saskatchewan |
---|
| 8 | * Saskatoon, Saskatchewan, CANADA |
---|
| 9 | * eric@skatter.usask.ca |
---|
| 10 | * |
---|
| 11 | * The license and distribution terms for this file may be |
---|
| 12 | * found in the file LICENSE in this distribution or at |
---|
| 13 | * http://www.OARcorp.com/rtems/license.html. |
---|
| 14 | * |
---|
| 15 | * $Id$ |
---|
| 16 | */ |
---|
| 17 | |
---|
| 18 | #include <rtems.h> |
---|
| 19 | #include <rtems/libio.h> |
---|
| 20 | #include <ctype.h> |
---|
| 21 | #include <errno.h> |
---|
| 22 | #include <stdio.h> |
---|
| 23 | #include <stdlib.h> |
---|
| 24 | #include <termios.h> |
---|
| 25 | #include <unistd.h> |
---|
| 26 | |
---|
| 27 | /* |
---|
| 28 | * The size of the cooked buffer |
---|
| 29 | */ |
---|
[118a812] | 30 | #define CBUFSIZE 256 |
---|
| 31 | |
---|
| 32 | /* |
---|
[d24ceb3] | 33 | * The sizes of the raw message buffers. |
---|
| 34 | * On most architectures it is quite a bit more |
---|
| 35 | * efficient if these are powers of two. |
---|
[118a812] | 36 | */ |
---|
[d24ceb3] | 37 | #define RAW_INPUT_BUFFER_SIZE 128 |
---|
| 38 | #define RAW_OUTPUT_BUFFER_SIZE 64 |
---|
[55e1322] | 39 | |
---|
| 40 | /* |
---|
| 41 | * Variables associated with each termios instance. |
---|
| 42 | * One structure for each hardware I/O device. |
---|
| 43 | */ |
---|
| 44 | struct rtems_termios_tty { |
---|
[118a812] | 45 | /* |
---|
| 46 | * Linked-list of active TERMIOS devices |
---|
| 47 | */ |
---|
| 48 | struct rtems_termios_tty *forw; |
---|
| 49 | struct rtems_termios_tty *back; |
---|
| 50 | |
---|
| 51 | /* |
---|
| 52 | * How many times has this device been opened |
---|
| 53 | */ |
---|
| 54 | int refcount; |
---|
| 55 | |
---|
| 56 | /* |
---|
| 57 | * This device |
---|
| 58 | */ |
---|
| 59 | rtems_device_major_number major; |
---|
| 60 | rtems_device_major_number minor; |
---|
| 61 | |
---|
| 62 | /* |
---|
| 63 | * Mutual-exclusion semaphores |
---|
| 64 | */ |
---|
| 65 | rtems_id isem; |
---|
| 66 | rtems_id osem; |
---|
| 67 | |
---|
| 68 | /* |
---|
| 69 | * The canonical (cooked) character buffer |
---|
| 70 | */ |
---|
| 71 | char cbuf[CBUFSIZE]; |
---|
| 72 | int ccount; |
---|
| 73 | int cindex; |
---|
| 74 | |
---|
| 75 | /* |
---|
| 76 | * Keep track of cursor (printhead) position |
---|
| 77 | */ |
---|
| 78 | int column; |
---|
| 79 | int read_start_column; |
---|
| 80 | |
---|
| 81 | /* |
---|
| 82 | * The ioctl settings |
---|
| 83 | */ |
---|
| 84 | struct termios termios; |
---|
| 85 | rtems_interval vtimeTicks; |
---|
| 86 | |
---|
| 87 | /* |
---|
[d24ceb3] | 88 | * Raw input character buffer |
---|
| 89 | */ |
---|
| 90 | volatile char rawInBuf[RAW_INPUT_BUFFER_SIZE]; |
---|
| 91 | volatile unsigned int rawInBufHead; |
---|
| 92 | volatile unsigned int rawInBufTail; |
---|
| 93 | rtems_id rawInBufSemaphore; |
---|
| 94 | rtems_unsigned32 rawInBufSemaphoreOptions; |
---|
| 95 | rtems_interval rawInBufSemaphoreTimeout; |
---|
| 96 | rtems_interval rawInBufSemaphoreFirstTimeout; |
---|
| 97 | unsigned int rawInBufDropped; /* Statistics */ |
---|
| 98 | |
---|
| 99 | /* |
---|
| 100 | * Raw output character buffer |
---|
[118a812] | 101 | */ |
---|
[d24ceb3] | 102 | char outputUsesInterrupts; |
---|
| 103 | volatile char rawOutBuf[RAW_OUTPUT_BUFFER_SIZE]; |
---|
| 104 | volatile unsigned int rawOutBufHead; |
---|
| 105 | volatile unsigned int rawOutBufTail; |
---|
| 106 | rtems_id rawOutBufSemaphore; |
---|
| 107 | enum {rob_idle, rob_busy, rob_wait } rawOutBufState; |
---|
[118a812] | 108 | |
---|
| 109 | /* |
---|
| 110 | * Callbacks to device-specific routines |
---|
| 111 | */ |
---|
| 112 | int (*lastClose)(int major, int minor, void *arg); |
---|
[51eb8d5] | 113 | int (*read)(int minor); |
---|
[608641e] | 114 | int (*write)(int minor, const char *buf, int len); |
---|
[55e1322] | 115 | }; |
---|
| 116 | static struct rtems_termios_tty *ttyHead, *ttyTail; |
---|
| 117 | static rtems_id ttyMutex; |
---|
| 118 | |
---|
[d24ceb3] | 119 | /* |
---|
| 120 | * Reserve enough resources to open every physical device once. |
---|
| 121 | */ |
---|
[2872e0b] | 122 | |
---|
| 123 | static int first_time; /* assumed to be zeroed by BSS initialization */ |
---|
| 124 | |
---|
[d24ceb3] | 125 | void |
---|
| 126 | rtems_termios_reserve_resources ( |
---|
| 127 | rtems_configuration_table *configuration, |
---|
| 128 | rtems_unsigned32 number_of_devices |
---|
| 129 | ) |
---|
| 130 | { |
---|
| 131 | rtems_api_configuration_table *rtems_config; |
---|
| 132 | |
---|
| 133 | if (!configuration) |
---|
| 134 | rtems_fatal_error_occurred (0xFFF0F001); |
---|
| 135 | rtems_config = configuration->RTEMS_api_configuration; |
---|
| 136 | if (!rtems_config) |
---|
| 137 | rtems_fatal_error_occurred (0xFFF0F002); |
---|
[2872e0b] | 138 | if (!first_time) |
---|
[d24ceb3] | 139 | rtems_config->maximum_semaphores += 1; |
---|
[2872e0b] | 140 | first_time = 1; |
---|
[d24ceb3] | 141 | rtems_config->maximum_semaphores += (4 * number_of_devices); |
---|
| 142 | } |
---|
| 143 | |
---|
[55e1322] | 144 | void |
---|
| 145 | rtems_termios_initialize (void) |
---|
| 146 | { |
---|
[118a812] | 147 | rtems_status_code sc; |
---|
| 148 | |
---|
| 149 | /* |
---|
| 150 | * Create the mutex semaphore for the tty list |
---|
| 151 | */ |
---|
| 152 | if (!ttyMutex) { |
---|
| 153 | sc = rtems_semaphore_create ( |
---|
| 154 | rtems_build_name ('T', 'R', 'm', 'i'), |
---|
| 155 | 1, |
---|
| 156 | RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY, |
---|
| 157 | RTEMS_NO_PRIORITY, |
---|
| 158 | &ttyMutex); |
---|
| 159 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 160 | rtems_fatal_error_occurred (sc); |
---|
| 161 | } |
---|
[55e1322] | 162 | } |
---|
[118a812] | 163 | |
---|
[55e1322] | 164 | /* |
---|
| 165 | * Open a termios device |
---|
| 166 | */ |
---|
| 167 | rtems_status_code |
---|
| 168 | rtems_termios_open ( |
---|
| 169 | rtems_device_major_number major, |
---|
| 170 | rtems_device_minor_number minor, |
---|
| 171 | void *arg, |
---|
| 172 | int (*deviceFirstOpen)(int major, int minor, void *arg), |
---|
| 173 | int (*deviceLastClose)(int major, int minor, void *arg), |
---|
[51eb8d5] | 174 | int (*deviceRead)(int minor), |
---|
[608641e] | 175 | int (*deviceWrite)(int minor, const char *buf, int len), |
---|
[d24ceb3] | 176 | int deviceOutputUsesInterrupts |
---|
[55e1322] | 177 | ) |
---|
| 178 | { |
---|
[118a812] | 179 | rtems_status_code sc; |
---|
| 180 | rtems_libio_open_close_args_t *args = arg; |
---|
| 181 | struct rtems_termios_tty *tty; |
---|
| 182 | |
---|
| 183 | /* |
---|
| 184 | * See if the device has already been opened |
---|
| 185 | */ |
---|
| 186 | sc = rtems_semaphore_obtain (ttyMutex, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
| 187 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 188 | return sc; |
---|
| 189 | for (tty = ttyHead ; tty != NULL ; tty = tty->forw) { |
---|
| 190 | if ((tty->major == major) && (tty->minor == minor)) |
---|
| 191 | break; |
---|
| 192 | } |
---|
| 193 | if (tty == NULL) { |
---|
| 194 | static char c = 'a'; |
---|
| 195 | |
---|
| 196 | /* |
---|
| 197 | * Create a new device |
---|
| 198 | */ |
---|
| 199 | tty = malloc (sizeof (struct rtems_termios_tty)); |
---|
| 200 | if (tty == NULL) { |
---|
| 201 | rtems_semaphore_release (ttyMutex); |
---|
| 202 | return RTEMS_NO_MEMORY; |
---|
| 203 | } |
---|
| 204 | tty->forw = ttyHead; |
---|
| 205 | ttyHead = tty; |
---|
| 206 | tty->back = NULL; |
---|
| 207 | if (ttyTail == NULL) |
---|
| 208 | ttyTail = tty; |
---|
| 209 | |
---|
[3a7782b] | 210 | tty->minor = minor; |
---|
| 211 | tty->major = major; |
---|
| 212 | |
---|
[118a812] | 213 | /* |
---|
| 214 | * Set up mutex semaphores |
---|
| 215 | */ |
---|
| 216 | sc = rtems_semaphore_create ( |
---|
| 217 | rtems_build_name ('T', 'R', 'i', c), |
---|
| 218 | 1, |
---|
| 219 | RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY, |
---|
| 220 | RTEMS_NO_PRIORITY, |
---|
| 221 | &tty->isem); |
---|
| 222 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 223 | rtems_fatal_error_occurred (sc); |
---|
| 224 | sc = rtems_semaphore_create ( |
---|
| 225 | rtems_build_name ('T', 'R', 'o', c), |
---|
| 226 | 1, |
---|
| 227 | RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY, |
---|
| 228 | RTEMS_NO_PRIORITY, |
---|
| 229 | &tty->osem); |
---|
| 230 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 231 | rtems_fatal_error_occurred (sc); |
---|
[d24ceb3] | 232 | sc = rtems_semaphore_create ( |
---|
| 233 | rtems_build_name ('T', 'R', 'x', c), |
---|
| 234 | 0, |
---|
| 235 | RTEMS_COUNTING_SEMAPHORE | RTEMS_PRIORITY, |
---|
| 236 | RTEMS_NO_PRIORITY, |
---|
| 237 | &tty->rawOutBufSemaphore); |
---|
| 238 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 239 | rtems_fatal_error_occurred (sc); |
---|
| 240 | tty->rawOutBufHead = 0; |
---|
| 241 | tty->rawOutBufTail = 0; |
---|
[118a812] | 242 | |
---|
| 243 | /* |
---|
| 244 | * Set callbacks |
---|
| 245 | */ |
---|
| 246 | tty->write = deviceWrite; |
---|
| 247 | tty->lastClose = deviceLastClose; |
---|
| 248 | if ((tty->read = deviceRead) == NULL) { |
---|
| 249 | sc = rtems_semaphore_create ( |
---|
| 250 | rtems_build_name ('T', 'R', 'r', c), |
---|
| 251 | 0, |
---|
| 252 | RTEMS_COUNTING_SEMAPHORE | RTEMS_PRIORITY, |
---|
| 253 | RTEMS_NO_PRIORITY, |
---|
[d24ceb3] | 254 | &tty->rawInBufSemaphore); |
---|
[118a812] | 255 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 256 | rtems_fatal_error_occurred (sc); |
---|
[d24ceb3] | 257 | tty->rawInBufHead = 0; |
---|
| 258 | tty->rawInBufTail = 0; |
---|
[118a812] | 259 | } |
---|
| 260 | |
---|
| 261 | /* |
---|
| 262 | * Initialize variables |
---|
| 263 | */ |
---|
| 264 | tty->column = 0; |
---|
| 265 | tty->cindex = tty->ccount = 0; |
---|
[d24ceb3] | 266 | tty->outputUsesInterrupts = deviceOutputUsesInterrupts; |
---|
[118a812] | 267 | |
---|
| 268 | /* |
---|
| 269 | * Set default parameters |
---|
| 270 | */ |
---|
| 271 | tty->termios.c_iflag = BRKINT | ICRNL | IXON | IMAXBEL; |
---|
| 272 | tty->termios.c_oflag = OPOST | ONLCR | XTABS; |
---|
| 273 | tty->termios.c_cflag = B9600 | CS8 | CREAD; |
---|
| 274 | tty->termios.c_lflag = ISIG | ICANON | IEXTEN | ECHO | ECHOK | ECHOE | ECHOCTL; |
---|
| 275 | tty->termios.c_cc[VINTR] = '\003'; |
---|
| 276 | tty->termios.c_cc[VQUIT] = '\034'; |
---|
| 277 | tty->termios.c_cc[VERASE] = '\177'; |
---|
| 278 | tty->termios.c_cc[VKILL] = '\025'; |
---|
| 279 | tty->termios.c_cc[VEOF] = '\004'; |
---|
| 280 | tty->termios.c_cc[VEOL] = '\000'; |
---|
| 281 | tty->termios.c_cc[VEOL2] = '\000'; |
---|
| 282 | tty->termios.c_cc[VSTART] = '\021'; |
---|
| 283 | tty->termios.c_cc[VSTOP] = '\023'; |
---|
| 284 | tty->termios.c_cc[VSUSP] = '\032'; |
---|
| 285 | tty->termios.c_cc[VREPRINT] = '\022'; |
---|
| 286 | tty->termios.c_cc[VDISCARD] = '\017'; |
---|
| 287 | tty->termios.c_cc[VWERASE] = '\027'; |
---|
| 288 | tty->termios.c_cc[VLNEXT] = '\026'; |
---|
| 289 | |
---|
| 290 | /* |
---|
| 291 | * Device-specific open |
---|
| 292 | */ |
---|
| 293 | if (deviceFirstOpen) |
---|
| 294 | (*deviceFirstOpen) (major, minor, arg); |
---|
| 295 | |
---|
| 296 | /* |
---|
| 297 | * Bump name characer |
---|
| 298 | */ |
---|
| 299 | if (c++ == 'z') |
---|
| 300 | c = 'a'; |
---|
| 301 | } |
---|
| 302 | tty->refcount++; |
---|
| 303 | args->iop->data1 = tty; |
---|
| 304 | rtems_semaphore_release (ttyMutex); |
---|
| 305 | return RTEMS_SUCCESSFUL; |
---|
[55e1322] | 306 | } |
---|
| 307 | |
---|
| 308 | rtems_status_code |
---|
| 309 | rtems_termios_close (void *arg) |
---|
| 310 | { |
---|
[118a812] | 311 | rtems_libio_open_close_args_t *args = arg; |
---|
| 312 | struct rtems_termios_tty *tty = args->iop->data1; |
---|
| 313 | rtems_status_code sc; |
---|
| 314 | |
---|
| 315 | sc = rtems_semaphore_obtain (ttyMutex, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
| 316 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 317 | rtems_fatal_error_occurred (sc); |
---|
| 318 | if (--tty->refcount == 0) { |
---|
| 319 | if (tty->lastClose) |
---|
| 320 | (*tty->lastClose) (tty->major, tty->minor, arg); |
---|
| 321 | if (tty->forw == NULL) |
---|
| 322 | ttyTail = tty->back; |
---|
| 323 | else |
---|
| 324 | tty->forw->back = tty->back; |
---|
| 325 | if (tty->back == NULL) |
---|
| 326 | ttyHead = tty->forw; |
---|
| 327 | else |
---|
| 328 | tty->back->forw = tty->forw; |
---|
| 329 | rtems_semaphore_delete (tty->isem); |
---|
| 330 | rtems_semaphore_delete (tty->osem); |
---|
[d24ceb3] | 331 | rtems_semaphore_delete (tty->rawOutBufSemaphore); |
---|
[118a812] | 332 | if (tty->read == NULL) |
---|
[d24ceb3] | 333 | rtems_semaphore_delete (tty->rawInBufSemaphore); |
---|
[118a812] | 334 | free (tty); |
---|
| 335 | } |
---|
| 336 | rtems_semaphore_release (ttyMutex); |
---|
| 337 | return RTEMS_SUCCESSFUL; |
---|
[55e1322] | 338 | } |
---|
| 339 | |
---|
| 340 | rtems_status_code |
---|
| 341 | rtems_termios_ioctl (void *arg) |
---|
| 342 | { |
---|
[118a812] | 343 | rtems_libio_ioctl_args_t *args = arg; |
---|
| 344 | struct rtems_termios_tty *tty = args->iop->data1; |
---|
| 345 | rtems_status_code sc; |
---|
| 346 | |
---|
[d24ceb3] | 347 | args->ioctl_return = 0; |
---|
[118a812] | 348 | sc = rtems_semaphore_obtain (tty->osem, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
[3a7782b] | 349 | if (sc != RTEMS_SUCCESSFUL) { |
---|
| 350 | args->ioctl_return = sc; |
---|
[118a812] | 351 | return sc; |
---|
[3a7782b] | 352 | } |
---|
[118a812] | 353 | switch (args->command) { |
---|
| 354 | default: |
---|
| 355 | sc = RTEMS_INVALID_NUMBER; |
---|
| 356 | break; |
---|
| 357 | |
---|
| 358 | case RTEMS_IO_GET_ATTRIBUTES: |
---|
| 359 | *(struct termios *)args->buffer = tty->termios; |
---|
| 360 | break; |
---|
| 361 | |
---|
| 362 | case RTEMS_IO_SET_ATTRIBUTES: |
---|
| 363 | tty->termios = *(struct termios *)args->buffer; |
---|
| 364 | if (tty->termios.c_lflag & ICANON) { |
---|
[d24ceb3] | 365 | tty->rawInBufSemaphoreOptions = RTEMS_WAIT; |
---|
| 366 | tty->rawInBufSemaphoreTimeout = RTEMS_NO_TIMEOUT; |
---|
| 367 | tty->rawInBufSemaphoreFirstTimeout = RTEMS_NO_TIMEOUT; |
---|
[118a812] | 368 | } |
---|
| 369 | else { |
---|
| 370 | rtems_interval ticksPerSecond; |
---|
| 371 | rtems_clock_get (RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticksPerSecond); |
---|
| 372 | tty->vtimeTicks = tty->termios.c_cc[VTIME] * ticksPerSecond / 10; |
---|
| 373 | if (tty->termios.c_cc[VTIME]) { |
---|
[d24ceb3] | 374 | tty->rawInBufSemaphoreOptions = RTEMS_WAIT; |
---|
| 375 | tty->rawInBufSemaphoreTimeout = tty->vtimeTicks; |
---|
[118a812] | 376 | if (tty->termios.c_cc[VMIN]) |
---|
[d24ceb3] | 377 | tty->rawInBufSemaphoreFirstTimeout = RTEMS_NO_TIMEOUT; |
---|
[118a812] | 378 | else |
---|
[d24ceb3] | 379 | tty->rawInBufSemaphoreFirstTimeout = tty->vtimeTicks; |
---|
[118a812] | 380 | } |
---|
| 381 | else { |
---|
| 382 | if (tty->termios.c_cc[VMIN]) { |
---|
[d24ceb3] | 383 | tty->rawInBufSemaphoreOptions = RTEMS_WAIT; |
---|
| 384 | tty->rawInBufSemaphoreTimeout = RTEMS_NO_TIMEOUT; |
---|
| 385 | tty->rawInBufSemaphoreFirstTimeout = RTEMS_NO_TIMEOUT; |
---|
[118a812] | 386 | } |
---|
| 387 | else { |
---|
[d24ceb3] | 388 | tty->rawInBufSemaphoreOptions = RTEMS_NO_WAIT; |
---|
[118a812] | 389 | } |
---|
| 390 | } |
---|
| 391 | } |
---|
| 392 | break; |
---|
| 393 | } |
---|
| 394 | rtems_semaphore_release (tty->osem); |
---|
[3a7782b] | 395 | args->ioctl_return = sc; |
---|
[118a812] | 396 | return sc; |
---|
[55e1322] | 397 | } |
---|
| 398 | |
---|
[d24ceb3] | 399 | /* |
---|
| 400 | * Send characters to device-specific code |
---|
| 401 | */ |
---|
| 402 | static void |
---|
| 403 | osend (const char *buf, int len, struct rtems_termios_tty *tty) |
---|
| 404 | { |
---|
| 405 | unsigned int newHead; |
---|
| 406 | rtems_interrupt_level level; |
---|
| 407 | rtems_status_code sc; |
---|
| 408 | |
---|
| 409 | if (!tty->outputUsesInterrupts) { |
---|
| 410 | (*tty->write)(tty->minor, buf, len); |
---|
| 411 | return; |
---|
| 412 | } |
---|
| 413 | newHead = tty->rawOutBufHead; |
---|
| 414 | while (len) { |
---|
| 415 | /* |
---|
| 416 | * Performance improvement could be made here. |
---|
| 417 | * Copy multiple bytes to raw buffer: |
---|
| 418 | * if (len > 1) && (space to buffer end, or tail > 1) |
---|
| 419 | * ncopy = MIN (len, space to buffer end or tail) |
---|
| 420 | * memcpy (raw buffer, buf, ncopy) |
---|
| 421 | * buf += ncopy |
---|
| 422 | * len -= ncopy |
---|
| 423 | * |
---|
| 424 | * To minimize latency, the memcpy should be done |
---|
| 425 | * with interrupts enabled. |
---|
| 426 | */ |
---|
| 427 | newHead = (newHead + 1) % RAW_OUTPUT_BUFFER_SIZE; |
---|
| 428 | rtems_interrupt_disable (level); |
---|
| 429 | while (newHead == tty->rawOutBufTail) { |
---|
| 430 | tty->rawOutBufState = rob_wait; |
---|
| 431 | rtems_interrupt_enable (level); |
---|
| 432 | sc = rtems_semaphore_obtain (tty->rawOutBufSemaphore, |
---|
| 433 | RTEMS_WAIT, |
---|
| 434 | RTEMS_NO_TIMEOUT); |
---|
| 435 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 436 | rtems_fatal_error_occurred (sc); |
---|
| 437 | rtems_interrupt_disable (level); |
---|
| 438 | } |
---|
| 439 | tty->rawOutBuf[tty->rawOutBufHead] = *buf++; |
---|
| 440 | tty->rawOutBufHead = newHead; |
---|
| 441 | if (tty->rawOutBufState == rob_idle) { |
---|
| 442 | rtems_interrupt_enable (level); |
---|
| 443 | tty->rawOutBufState = rob_busy; |
---|
| 444 | (*tty->write)(tty->minor, (char *)&tty->rawOutBuf[tty->rawOutBufTail], 1); |
---|
| 445 | } |
---|
| 446 | else { |
---|
| 447 | rtems_interrupt_enable (level); |
---|
| 448 | } |
---|
| 449 | len--; |
---|
| 450 | } |
---|
| 451 | } |
---|
| 452 | |
---|
[55e1322] | 453 | /* |
---|
| 454 | * Handle output processing |
---|
| 455 | */ |
---|
| 456 | static void |
---|
| 457 | oproc (unsigned char c, struct rtems_termios_tty *tty) |
---|
| 458 | { |
---|
[118a812] | 459 | int i; |
---|
| 460 | |
---|
| 461 | if (tty->termios.c_oflag & OPOST) { |
---|
| 462 | switch (c) { |
---|
| 463 | case '\n': |
---|
| 464 | if (tty->termios.c_oflag & ONLRET) |
---|
| 465 | tty->column = 0; |
---|
| 466 | if (tty->termios.c_oflag & ONLCR) { |
---|
[d24ceb3] | 467 | osend ("\r", 1, tty); |
---|
[118a812] | 468 | tty->column = 0; |
---|
| 469 | } |
---|
| 470 | break; |
---|
| 471 | |
---|
| 472 | case '\r': |
---|
| 473 | if ((tty->termios.c_oflag & ONOCR) && (tty->column == 0)) |
---|
| 474 | return; |
---|
| 475 | if (tty->termios.c_oflag & OCRNL) { |
---|
| 476 | c = '\n'; |
---|
| 477 | if (tty->termios.c_oflag & ONLRET) |
---|
| 478 | tty->column = 0; |
---|
| 479 | break; |
---|
| 480 | } |
---|
| 481 | tty->column = 0; |
---|
| 482 | break; |
---|
| 483 | |
---|
| 484 | case '\t': |
---|
| 485 | i = 8 - (tty->column & 7); |
---|
| 486 | if ((tty->termios.c_oflag & TABDLY) == XTABS) { |
---|
| 487 | tty->column += i; |
---|
[d24ceb3] | 488 | osend ( " ", i, tty); |
---|
[118a812] | 489 | return; |
---|
| 490 | } |
---|
| 491 | tty->column += i; |
---|
| 492 | break; |
---|
| 493 | |
---|
| 494 | case '\b': |
---|
| 495 | if (tty->column > 0) |
---|
| 496 | tty->column--; |
---|
| 497 | break; |
---|
| 498 | |
---|
| 499 | default: |
---|
| 500 | if (tty->termios.c_oflag & OLCUC) |
---|
| 501 | c = toupper(c); |
---|
| 502 | if (!iscntrl(c)) |
---|
| 503 | tty->column++; |
---|
| 504 | break; |
---|
| 505 | } |
---|
| 506 | } |
---|
[d24ceb3] | 507 | osend (&c, 1, tty); |
---|
[55e1322] | 508 | } |
---|
| 509 | |
---|
| 510 | rtems_status_code |
---|
| 511 | rtems_termios_write (void *arg) |
---|
| 512 | { |
---|
[118a812] | 513 | rtems_libio_rw_args_t *args = arg; |
---|
| 514 | struct rtems_termios_tty *tty = args->iop->data1; |
---|
| 515 | rtems_status_code sc; |
---|
| 516 | |
---|
| 517 | sc = rtems_semaphore_obtain (tty->osem, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
| 518 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 519 | return sc; |
---|
| 520 | if (tty->termios.c_oflag & OPOST) { |
---|
| 521 | unsigned32 count = args->count; |
---|
| 522 | unsigned8 *buffer = args->buffer; |
---|
| 523 | while (count--) |
---|
| 524 | oproc (*buffer++, tty); |
---|
| 525 | args->bytes_moved = args->count; |
---|
| 526 | } |
---|
| 527 | else { |
---|
[d24ceb3] | 528 | osend (args->buffer, args->count, tty); |
---|
| 529 | args->bytes_moved = args->count; |
---|
[118a812] | 530 | } |
---|
| 531 | rtems_semaphore_release (tty->osem); |
---|
| 532 | return sc; |
---|
[55e1322] | 533 | } |
---|
| 534 | |
---|
| 535 | /* |
---|
| 536 | * Echo a typed character |
---|
| 537 | */ |
---|
| 538 | static void |
---|
| 539 | echo (unsigned char c, struct rtems_termios_tty *tty) |
---|
| 540 | { |
---|
[118a812] | 541 | if ((tty->termios.c_lflag & ECHOCTL) && iscntrl(c) && (c != '\t') && (c != '\n')) { |
---|
| 542 | char echobuf[2]; |
---|
| 543 | |
---|
| 544 | echobuf[0] = '^'; |
---|
| 545 | echobuf[1] = c ^ 0x40; |
---|
[d24ceb3] | 546 | osend (echobuf, 2, tty); |
---|
[118a812] | 547 | tty->column += 2; |
---|
| 548 | } |
---|
| 549 | else { |
---|
| 550 | oproc (c, tty); |
---|
| 551 | } |
---|
[55e1322] | 552 | } |
---|
| 553 | |
---|
| 554 | /* |
---|
| 555 | * Erase a character or line |
---|
| 556 | * FIXME: Needs support for WERASE and ECHOPRT. |
---|
| 557 | * FIXME: Some of the tests should check for IEXTEN, too. |
---|
| 558 | */ |
---|
| 559 | static void |
---|
| 560 | erase (struct rtems_termios_tty *tty, int lineFlag) |
---|
| 561 | { |
---|
[118a812] | 562 | if (tty->ccount == 0) |
---|
| 563 | return; |
---|
| 564 | if (lineFlag) { |
---|
| 565 | if (!(tty->termios.c_lflag & ECHO)) { |
---|
| 566 | tty->ccount = 0; |
---|
| 567 | return; |
---|
| 568 | } |
---|
| 569 | if (!(tty->termios.c_lflag & ECHOE)) { |
---|
| 570 | tty->ccount = 0; |
---|
| 571 | echo (tty->termios.c_cc[VKILL], tty); |
---|
| 572 | if (tty->termios.c_lflag & ECHOK) |
---|
| 573 | echo ('\n', tty); |
---|
| 574 | return; |
---|
| 575 | } |
---|
| 576 | } |
---|
| 577 | while (tty->ccount) { |
---|
| 578 | unsigned char c = tty->cbuf[--tty->ccount]; |
---|
| 579 | |
---|
| 580 | if (tty->termios.c_lflag & ECHO) { |
---|
| 581 | if (!lineFlag && !(tty->termios.c_lflag & ECHOE)) { |
---|
| 582 | echo (tty->termios.c_cc[VERASE], tty); |
---|
| 583 | } |
---|
| 584 | else if (c == '\t') { |
---|
| 585 | int col = tty->read_start_column; |
---|
| 586 | int i = 0; |
---|
| 587 | |
---|
| 588 | /* |
---|
| 589 | * Find the character before the tab |
---|
| 590 | */ |
---|
| 591 | while (i != tty->ccount) { |
---|
| 592 | c = tty->cbuf[i++]; |
---|
| 593 | if (c == '\t') { |
---|
| 594 | col = (col | 7) + 1; |
---|
| 595 | } |
---|
| 596 | else if (iscntrl (c)) { |
---|
| 597 | if (tty->termios.c_lflag & ECHOCTL) |
---|
| 598 | col += 2; |
---|
| 599 | } |
---|
| 600 | else { |
---|
| 601 | col++; |
---|
| 602 | } |
---|
| 603 | } |
---|
| 604 | |
---|
| 605 | /* |
---|
| 606 | * Back up over the tab |
---|
| 607 | */ |
---|
| 608 | while (tty->column > col) { |
---|
[d24ceb3] | 609 | osend ("\b", 1, tty); |
---|
[118a812] | 610 | tty->column--; |
---|
| 611 | } |
---|
| 612 | } |
---|
| 613 | else { |
---|
| 614 | if (iscntrl (c) && (tty->termios.c_lflag & ECHOCTL)) { |
---|
[d24ceb3] | 615 | osend ("\b \b", 3, tty); |
---|
[118a812] | 616 | if (tty->column) |
---|
| 617 | tty->column--; |
---|
| 618 | } |
---|
| 619 | if (!iscntrl (c) || (tty->termios.c_lflag & ECHOCTL)) { |
---|
[d24ceb3] | 620 | osend ("\b \b", 3, tty); |
---|
[118a812] | 621 | if (tty->column) |
---|
| 622 | tty->column--; |
---|
| 623 | } |
---|
| 624 | } |
---|
| 625 | } |
---|
| 626 | if (!lineFlag) |
---|
| 627 | break; |
---|
| 628 | } |
---|
[55e1322] | 629 | } |
---|
| 630 | |
---|
| 631 | /* |
---|
| 632 | * Process a single input character |
---|
| 633 | */ |
---|
| 634 | static int |
---|
| 635 | iproc (unsigned char c, struct rtems_termios_tty *tty) |
---|
| 636 | { |
---|
[118a812] | 637 | if (tty->termios.c_iflag & ISTRIP) |
---|
| 638 | c &= 0x7f; |
---|
| 639 | if (tty->termios.c_iflag & IUCLC) |
---|
| 640 | c = tolower (c); |
---|
| 641 | if (c == '\r') { |
---|
| 642 | if (tty->termios.c_iflag & IGNCR) |
---|
| 643 | return 0; |
---|
| 644 | if (tty->termios.c_iflag & ICRNL) |
---|
| 645 | c = '\n'; |
---|
| 646 | } |
---|
| 647 | else if ((c == '\n') && (tty->termios.c_iflag & INLCR)) { |
---|
| 648 | c = '\r'; |
---|
| 649 | } |
---|
| 650 | if ((c != '\0') && (tty->termios.c_lflag & ICANON)) { |
---|
| 651 | if (c == tty->termios.c_cc[VERASE]) { |
---|
| 652 | erase (tty, 0); |
---|
| 653 | return 0; |
---|
| 654 | } |
---|
| 655 | else if (c == tty->termios.c_cc[VKILL]) { |
---|
| 656 | erase (tty, 1); |
---|
| 657 | return 0; |
---|
| 658 | } |
---|
| 659 | else if (c == tty->termios.c_cc[VEOF]) { |
---|
| 660 | return 1; |
---|
| 661 | } |
---|
| 662 | else if (c == '\n') { |
---|
| 663 | if (tty->termios.c_lflag & (ECHO | ECHONL)) |
---|
| 664 | echo (c, tty); |
---|
| 665 | tty->cbuf[tty->ccount++] = c; |
---|
| 666 | return 1; |
---|
| 667 | } |
---|
| 668 | else if ((c == tty->termios.c_cc[VEOL]) |
---|
| 669 | || (c == tty->termios.c_cc[VEOL2])) { |
---|
| 670 | if (tty->termios.c_lflag & ECHO) |
---|
| 671 | echo (c, tty); |
---|
| 672 | tty->cbuf[tty->ccount++] = c; |
---|
| 673 | return 1; |
---|
| 674 | } |
---|
| 675 | } |
---|
| 676 | |
---|
| 677 | /* |
---|
| 678 | * FIXME: Should do IMAXBEL handling somehow |
---|
| 679 | */ |
---|
| 680 | if (tty->ccount < (CBUFSIZE-1)) { |
---|
| 681 | if (tty->termios.c_lflag & ECHO) |
---|
| 682 | echo (c, tty); |
---|
| 683 | tty->cbuf[tty->ccount++] = c; |
---|
| 684 | } |
---|
| 685 | return 0; |
---|
[55e1322] | 686 | } |
---|
| 687 | |
---|
| 688 | /* |
---|
| 689 | * Process input character, with semaphore. |
---|
| 690 | */ |
---|
| 691 | static int |
---|
| 692 | siproc (unsigned char c, struct rtems_termios_tty *tty) |
---|
| 693 | { |
---|
[118a812] | 694 | int i; |
---|
| 695 | |
---|
| 696 | /* |
---|
| 697 | * Obtain output semaphore if character will be echoed |
---|
| 698 | */ |
---|
| 699 | if (tty->termios.c_lflag & (ECHO|ECHOE|ECHOK|ECHONL|ECHOPRT|ECHOCTL|ECHOKE)) { |
---|
| 700 | rtems_semaphore_obtain (tty->osem, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
| 701 | i = iproc (c, tty); |
---|
| 702 | rtems_semaphore_release (tty->osem); |
---|
| 703 | } |
---|
| 704 | else { |
---|
| 705 | i = iproc (c, tty); |
---|
| 706 | } |
---|
| 707 | return i; |
---|
[55e1322] | 708 | } |
---|
| 709 | |
---|
| 710 | /* |
---|
| 711 | * Fill the input buffer by polling the device |
---|
| 712 | */ |
---|
| 713 | static rtems_status_code |
---|
| 714 | fillBufferPoll (struct rtems_termios_tty *tty) |
---|
| 715 | { |
---|
[118a812] | 716 | int n; |
---|
| 717 | |
---|
| 718 | if (tty->termios.c_lflag & ICANON) { |
---|
| 719 | for (;;) { |
---|
[51eb8d5] | 720 | n = (*tty->read)(tty->minor); |
---|
[118a812] | 721 | if (n < 0) { |
---|
| 722 | rtems_task_wake_after (1); |
---|
| 723 | } |
---|
| 724 | else { |
---|
[51eb8d5] | 725 | if (siproc (n, tty)) |
---|
[118a812] | 726 | break; |
---|
| 727 | } |
---|
| 728 | } |
---|
| 729 | } |
---|
| 730 | else { |
---|
| 731 | rtems_interval then, now; |
---|
| 732 | if (!tty->termios.c_cc[VMIN] && tty->termios.c_cc[VTIME]) |
---|
| 733 | rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &then); |
---|
| 734 | for (;;) { |
---|
[51eb8d5] | 735 | n = (*tty->read)(tty->minor); |
---|
[118a812] | 736 | if (n < 0) { |
---|
| 737 | if (tty->termios.c_cc[VMIN]) { |
---|
| 738 | if (tty->termios.c_cc[VTIME] && tty->ccount) { |
---|
| 739 | rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &now); |
---|
| 740 | if ((now - then) > tty->vtimeTicks) { |
---|
| 741 | break; |
---|
| 742 | } |
---|
| 743 | } |
---|
| 744 | } |
---|
| 745 | else { |
---|
| 746 | if (!tty->termios.c_cc[VTIME]) |
---|
| 747 | break; |
---|
| 748 | rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &now); |
---|
| 749 | if ((now - then) > tty->vtimeTicks) { |
---|
| 750 | break; |
---|
| 751 | } |
---|
| 752 | } |
---|
| 753 | rtems_task_wake_after (1); |
---|
| 754 | } |
---|
| 755 | else { |
---|
[51eb8d5] | 756 | siproc (n, tty); |
---|
[118a812] | 757 | if (tty->ccount >= tty->termios.c_cc[VMIN]) |
---|
| 758 | break; |
---|
| 759 | if (tty->termios.c_cc[VMIN] && tty->termios.c_cc[VTIME]) |
---|
| 760 | rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &then); |
---|
| 761 | } |
---|
| 762 | } |
---|
| 763 | } |
---|
| 764 | return RTEMS_SUCCESSFUL; |
---|
[55e1322] | 765 | } |
---|
| 766 | |
---|
| 767 | /* |
---|
| 768 | * Fill the input buffer from the raw input queue |
---|
| 769 | */ |
---|
| 770 | static rtems_status_code |
---|
| 771 | fillBufferQueue (struct rtems_termios_tty *tty) |
---|
| 772 | { |
---|
[d24ceb3] | 773 | rtems_interval timeout = tty->rawInBufSemaphoreFirstTimeout; |
---|
[118a812] | 774 | rtems_status_code sc; |
---|
| 775 | |
---|
| 776 | for (;;) { |
---|
| 777 | /* |
---|
| 778 | * Process characters read from raw queue |
---|
| 779 | */ |
---|
[d24ceb3] | 780 | while (tty->rawInBufHead != tty->rawInBufTail) { |
---|
[118a812] | 781 | unsigned char c; |
---|
| 782 | unsigned int newHead; |
---|
| 783 | |
---|
[d24ceb3] | 784 | newHead = (tty->rawInBufHead + 1) % RAW_INPUT_BUFFER_SIZE; |
---|
| 785 | c = tty->rawInBuf[newHead]; |
---|
| 786 | tty->rawInBufHead = newHead; |
---|
[118a812] | 787 | if (tty->termios.c_lflag & ICANON) { |
---|
| 788 | if (siproc (c, tty)) |
---|
| 789 | return RTEMS_SUCCESSFUL; |
---|
| 790 | } |
---|
| 791 | else { |
---|
| 792 | siproc (c, tty); |
---|
| 793 | if (tty->ccount >= tty->termios.c_cc[VMIN]) |
---|
| 794 | return RTEMS_SUCCESSFUL; |
---|
| 795 | } |
---|
[d24ceb3] | 796 | timeout = tty->rawInBufSemaphoreTimeout; |
---|
[118a812] | 797 | } |
---|
| 798 | |
---|
| 799 | /* |
---|
| 800 | * Wait for characters |
---|
| 801 | */ |
---|
[d24ceb3] | 802 | sc = rtems_semaphore_obtain (tty->rawInBufSemaphore, |
---|
| 803 | tty->rawInBufSemaphoreOptions, |
---|
[118a812] | 804 | timeout); |
---|
| 805 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 806 | break; |
---|
| 807 | } |
---|
| 808 | return RTEMS_SUCCESSFUL; |
---|
[55e1322] | 809 | } |
---|
| 810 | |
---|
| 811 | rtems_status_code |
---|
| 812 | rtems_termios_read (void *arg) |
---|
| 813 | { |
---|
[118a812] | 814 | rtems_libio_rw_args_t *args = arg; |
---|
| 815 | struct rtems_termios_tty *tty = args->iop->data1; |
---|
| 816 | unsigned32 count = args->count; |
---|
| 817 | unsigned8 *buffer = args->buffer; |
---|
| 818 | rtems_status_code sc; |
---|
| 819 | |
---|
| 820 | sc = rtems_semaphore_obtain (tty->isem, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
| 821 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 822 | return sc; |
---|
| 823 | if (tty->cindex == tty->ccount) { |
---|
| 824 | tty->cindex = tty->ccount = 0; |
---|
| 825 | tty->read_start_column = tty->column; |
---|
| 826 | if (tty->read) |
---|
| 827 | sc = fillBufferPoll (tty); |
---|
| 828 | else |
---|
| 829 | sc = fillBufferQueue (tty); |
---|
| 830 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 831 | tty->cindex = tty->ccount = 0; |
---|
| 832 | } |
---|
| 833 | while (count && (tty->cindex < tty->ccount)) { |
---|
| 834 | *buffer++ = tty->cbuf[tty->cindex++]; |
---|
| 835 | count--; |
---|
| 836 | } |
---|
| 837 | args->bytes_moved = args->count - count; |
---|
| 838 | rtems_semaphore_release (tty->isem); |
---|
| 839 | return sc; |
---|
[55e1322] | 840 | } |
---|
| 841 | |
---|
| 842 | /* |
---|
| 843 | * Place characters on raw queue. |
---|
[d24ceb3] | 844 | * NOTE: This routine runs in the context of the |
---|
| 845 | * device receive interrupt handler. |
---|
[55e1322] | 846 | */ |
---|
| 847 | void |
---|
| 848 | rtems_termios_enqueue_raw_characters (void *ttyp, char *buf, int len) |
---|
| 849 | { |
---|
[118a812] | 850 | struct rtems_termios_tty *tty = ttyp; |
---|
| 851 | unsigned int newTail; |
---|
| 852 | |
---|
| 853 | while (len) { |
---|
[d24ceb3] | 854 | newTail = (tty->rawInBufTail + 1) % RAW_INPUT_BUFFER_SIZE; |
---|
| 855 | if (newTail == tty->rawInBufHead) { |
---|
| 856 | tty->rawInBufDropped += len; |
---|
[118a812] | 857 | break; |
---|
| 858 | } |
---|
[d24ceb3] | 859 | tty->rawInBuf[newTail] = *buf++; |
---|
[118a812] | 860 | len--; |
---|
[d24ceb3] | 861 | tty->rawInBufTail = newTail; |
---|
[118a812] | 862 | } |
---|
[d24ceb3] | 863 | rtems_semaphore_release (tty->rawInBufSemaphore); |
---|
[55e1322] | 864 | } |
---|
| 865 | |
---|
[118a812] | 866 | /* |
---|
[d24ceb3] | 867 | * Characters have been transmitted |
---|
| 868 | * NOTE: This routine runs in the context of the |
---|
| 869 | * device transmit interrupt handler. |
---|
| 870 | * The second argument is the number of characters transmitted so far. |
---|
| 871 | * This value will always be 1 for devices which generate an interrupt |
---|
| 872 | * for each transmitted character. |
---|
[118a812] | 873 | */ |
---|
[d24ceb3] | 874 | void |
---|
| 875 | rtems_termios_dequeue_characters (void *ttyp, int len) |
---|
[118a812] | 876 | { |
---|
[d24ceb3] | 877 | struct rtems_termios_tty *tty = ttyp; |
---|
| 878 | unsigned int newTail; |
---|
| 879 | int nToSend; |
---|
[118a812] | 880 | |
---|
[d24ceb3] | 881 | if (tty->rawOutBufState == rob_wait) |
---|
| 882 | rtems_semaphore_release (tty->rawOutBufSemaphore); |
---|
| 883 | newTail = (tty->rawOutBufTail + len) % RAW_OUTPUT_BUFFER_SIZE; |
---|
| 884 | if (newTail == tty->rawOutBufHead) { |
---|
| 885 | /* |
---|
| 886 | * Buffer empty |
---|
| 887 | */ |
---|
| 888 | tty->rawOutBufState = rob_idle; |
---|
| 889 | } |
---|
| 890 | else { |
---|
| 891 | /* |
---|
| 892 | * Buffer not empty, start tranmitter |
---|
| 893 | */ |
---|
| 894 | tty->rawOutBufState = rob_busy; |
---|
| 895 | if (newTail > tty->rawOutBufHead) |
---|
| 896 | nToSend = RAW_OUTPUT_BUFFER_SIZE - newTail; |
---|
| 897 | else |
---|
| 898 | nToSend = tty->rawOutBufHead - newTail; |
---|
| 899 | (*tty->write)(tty->minor, (char *)&tty->rawOutBuf[newTail], nToSend); |
---|
| 900 | } |
---|
| 901 | tty->rawOutBufTail = newTail; |
---|
[118a812] | 902 | } |
---|