source: rtems/c/src/lib/libc/termios.c @ 119bced

4.104.114.84.95
Last change on this file since 119bced was 119bced, checked in by Joel Sherrill <joel.sherrill@…>, on 05/22/98 at 14:51:11

Added tcdrain(), cfgetospeed(0, cfsetospeed(), cfgetispeed(), and
cfsetispeed().

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