source: rtems/cpukit/libcsupport/src/termios.c @ 28779c7

4.115
Last change on this file since 28779c7 was 28779c7, checked in by Sebastian Huber <sebastian.huber@…>, on 03/07/14 at 11:53:41

score: Add function to destroy SMP locks

  • Property mode set to 100644
File size: 42.4 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.rtems.com/license/LICENSE.
14 */
15
16#if HAVE_CONFIG_H
17#include "config.h"
18#endif
19
20#include <rtems.h>
21#include <rtems/libio.h>
22#include <ctype.h>
23#include <errno.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <termios.h>
27#include <unistd.h>
28#include <sys/ttycom.h>
29
30#include <rtems/termiostypes.h>
31
32/*
33 * The size of the cooked buffer
34 */
35#define CBUFSIZE  (rtems_termios_cbufsize)
36
37/*
38 * The sizes of the raw message buffers.
39 * On most architectures it is quite a bit more
40 * efficient if these are powers of two.
41 */
42#define RAW_INPUT_BUFFER_SIZE  (rtems_termios_raw_input_size)
43#define RAW_OUTPUT_BUFFER_SIZE  (rtems_termios_raw_output_size)
44
45/* fields for "flow_ctrl" status */
46#define FL_IREQXOF 1U        /* input queue requests stop of incoming data */
47#define FL_ISNTXOF 2U        /* XOFF has been sent to other side of line   */
48#define FL_IRTSOFF 4U        /* RTS has been turned off for other side..   */
49
50#define FL_ORCVXOF 0x10U     /* XOFF has been received                     */
51#define FL_OSTOP   0x20U     /* output has been stopped due to XOFF        */
52
53#define FL_MDRTS   0x100U    /* input controlled with RTS/CTS handshake    */
54#define FL_MDXON   0x200U    /* input controlled with XON/XOFF protocol    */
55#define FL_MDXOF   0x400U    /* output controlled with XON/XOFF protocol   */
56
57#define NODISC(n) \
58  { NULL,  NULL,  NULL,  NULL, \
59    NULL,  NULL,  NULL,  NULL }
60/*
61 * FIXME: change rtems_termios_linesw entries consistent
62 *        with rtems_termios_linesw entry usage...
63 */
64struct  rtems_termios_linesw rtems_termios_linesw[MAXLDISC] =
65{
66  NODISC(0),    /* 0- termios-built-in */
67  NODISC(1),    /* 1- defunct */
68  NODISC(2),    /* 2- NTTYDISC */
69  NODISC(3),    /* TABLDISC */
70  NODISC(4),    /* SLIPDISC */
71  NODISC(5),    /* PPPDISC */
72  NODISC(6),    /* loadable */
73  NODISC(7),    /* loadable */
74};
75
76int  rtems_termios_nlinesw =
77       sizeof (rtems_termios_linesw) / sizeof (rtems_termios_linesw[0]);
78
79extern struct rtems_termios_tty *rtems_termios_ttyHead;
80extern struct rtems_termios_tty *rtems_termios_ttyTail;
81extern rtems_id rtems_termios_ttyMutex;
82
83static size_t rtems_termios_cbufsize = 256;
84static size_t rtems_termios_raw_input_size = 128;
85static size_t rtems_termios_raw_output_size = 64;
86
87static rtems_task rtems_termios_rxdaemon(rtems_task_argument argument);
88static rtems_task rtems_termios_txdaemon(rtems_task_argument argument);
89/*
90 * some constants for I/O daemon task creation
91 */
92#define TERMIOS_TXTASK_PRIO 10
93#define TERMIOS_RXTASK_PRIO 9
94#define TERMIOS_TXTASK_STACKSIZE 1024
95#define TERMIOS_RXTASK_STACKSIZE 1024
96/*
97 * some events to be sent to the I/O tasks
98 */
99#define TERMIOS_TX_START_EVENT     RTEMS_EVENT_1
100#define TERMIOS_TX_TERMINATE_EVENT RTEMS_EVENT_0
101
102#define TERMIOS_RX_PROC_EVENT      RTEMS_EVENT_1
103#define TERMIOS_RX_TERMINATE_EVENT RTEMS_EVENT_0
104
105/*
106 * Open a termios device
107 */
108rtems_status_code
109rtems_termios_open (
110  rtems_device_major_number      major,
111  rtems_device_minor_number      minor,
112  void                          *arg,
113  const rtems_termios_callbacks *callbacks
114)
115{
116  rtems_status_code sc;
117  rtems_libio_open_close_args_t *args = arg;
118  struct rtems_termios_tty *tty;
119
120  /*
121   * See if the device has already been opened
122   */
123  sc = rtems_semaphore_obtain(
124    rtems_termios_ttyMutex, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
125  if (sc != RTEMS_SUCCESSFUL)
126    return sc;
127
128  for (tty = rtems_termios_ttyHead ; tty != NULL ; tty = tty->forw) {
129    if ((tty->major == major) && (tty->minor == minor))
130      break;
131  }
132
133  if (tty == NULL) {
134    static char c = 'a';
135
136    /*
137     * Create a new device
138     */
139    tty = calloc (1, sizeof (struct rtems_termios_tty));
140    if (tty == NULL) {
141      rtems_semaphore_release (rtems_termios_ttyMutex);
142      return RTEMS_NO_MEMORY;
143    }
144    /*
145     * allocate raw input buffer
146     */
147    tty->rawInBuf.Size = RAW_INPUT_BUFFER_SIZE;
148    tty->rawInBuf.theBuf = malloc (tty->rawInBuf.Size);
149    if (tty->rawInBuf.theBuf == NULL) {
150            free(tty);
151      rtems_semaphore_release (rtems_termios_ttyMutex);
152      return RTEMS_NO_MEMORY;
153    }
154    /*
155     * allocate raw output buffer
156     */
157    tty->rawOutBuf.Size = RAW_OUTPUT_BUFFER_SIZE;
158    tty->rawOutBuf.theBuf = malloc (tty->rawOutBuf.Size);
159    if (tty->rawOutBuf.theBuf == NULL) {
160            free((void *)(tty->rawInBuf.theBuf));
161            free(tty);
162      rtems_semaphore_release (rtems_termios_ttyMutex);
163      return RTEMS_NO_MEMORY;
164    }
165    /*
166     * allocate cooked buffer
167     */
168    tty->cbuf  = malloc (CBUFSIZE);
169    if (tty->cbuf == NULL) {
170            free((void *)(tty->rawOutBuf.theBuf));
171            free((void *)(tty->rawInBuf.theBuf));
172            free(tty);
173      rtems_semaphore_release (rtems_termios_ttyMutex);
174      return RTEMS_NO_MEMORY;
175    }
176    /*
177     * Initialize wakeup callbacks
178     */
179    tty->tty_snd.sw_pfn = NULL;
180    tty->tty_snd.sw_arg = NULL;
181    tty->tty_rcv.sw_pfn = NULL;
182    tty->tty_rcv.sw_arg = NULL;
183    tty->tty_rcvwakeup  = 0;
184
185    /*
186     * link tty
187     */
188    tty->forw = rtems_termios_ttyHead;
189    tty->back = NULL;
190    if (rtems_termios_ttyHead != NULL)
191      rtems_termios_ttyHead->back = tty;
192    rtems_termios_ttyHead = tty;
193    if (rtems_termios_ttyTail == NULL)
194      rtems_termios_ttyTail = tty;
195
196    tty->minor = minor;
197    tty->major = major;
198
199    /*
200     * Set up mutex semaphores
201     */
202    sc = rtems_semaphore_create (
203      rtems_build_name ('T', 'R', 'i', c),
204      1,
205      RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY,
206      RTEMS_NO_PRIORITY,
207      &tty->isem);
208    if (sc != RTEMS_SUCCESSFUL)
209      rtems_fatal_error_occurred (sc);
210    sc = rtems_semaphore_create (
211      rtems_build_name ('T', 'R', 'o', c),
212      1,
213      RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY,
214      RTEMS_NO_PRIORITY,
215      &tty->osem);
216    if (sc != RTEMS_SUCCESSFUL)
217      rtems_fatal_error_occurred (sc);
218    sc = rtems_semaphore_create (
219      rtems_build_name ('T', 'R', 'x', c),
220      0,
221      RTEMS_SIMPLE_BINARY_SEMAPHORE | RTEMS_FIFO,
222      RTEMS_NO_PRIORITY,
223      &tty->rawOutBuf.Semaphore);
224    if (sc != RTEMS_SUCCESSFUL)
225      rtems_fatal_error_occurred (sc);
226    tty->rawOutBufState = rob_idle;
227
228    /*
229     * Set callbacks
230     */
231    tty->device = *callbacks;
232
233    rtems_interrupt_lock_initialize (&tty->interrupt_lock);
234
235    /*
236     * Create I/O tasks
237     */
238    if (tty->device.outputUsesInterrupts == TERMIOS_TASK_DRIVEN) {
239      sc = rtems_task_create (
240                                   rtems_build_name ('T', 'x', 'T', c),
241           TERMIOS_TXTASK_PRIO,
242           TERMIOS_TXTASK_STACKSIZE,
243           RTEMS_NO_PREEMPT | RTEMS_NO_TIMESLICE |
244           RTEMS_NO_ASR,
245           RTEMS_NO_FLOATING_POINT | RTEMS_LOCAL,
246           &tty->txTaskId);
247      if (sc != RTEMS_SUCCESSFUL)
248        rtems_fatal_error_occurred (sc);
249      sc = rtems_task_create (
250                                   rtems_build_name ('R', 'x', 'T', c),
251           TERMIOS_RXTASK_PRIO,
252           TERMIOS_RXTASK_STACKSIZE,
253           RTEMS_NO_PREEMPT | RTEMS_NO_TIMESLICE |
254           RTEMS_NO_ASR,
255           RTEMS_NO_FLOATING_POINT | RTEMS_LOCAL,
256           &tty->rxTaskId);
257      if (sc != RTEMS_SUCCESSFUL)
258        rtems_fatal_error_occurred (sc);
259
260    }
261    if ((tty->device.pollRead == NULL) ||
262        (tty->device.outputUsesInterrupts == TERMIOS_TASK_DRIVEN)){
263      sc = rtems_semaphore_create (
264        rtems_build_name ('T', 'R', 'r', c),
265        0,
266        RTEMS_SIMPLE_BINARY_SEMAPHORE | RTEMS_PRIORITY,
267        RTEMS_NO_PRIORITY,
268        &tty->rawInBuf.Semaphore);
269      if (sc != RTEMS_SUCCESSFUL)
270        rtems_fatal_error_occurred (sc);
271    }
272
273    /*
274     * Set default parameters
275     */
276    tty->termios.c_iflag = BRKINT | ICRNL | IXON | IMAXBEL;
277    tty->termios.c_oflag = OPOST | ONLCR | XTABS;
278    tty->termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL;
279    tty->termios.c_lflag =
280       ISIG | ICANON | IEXTEN | ECHO | ECHOK | ECHOE | ECHOCTL;
281
282    tty->termios.c_cc[VINTR] = '\003';
283    tty->termios.c_cc[VQUIT] = '\034';
284    tty->termios.c_cc[VERASE] = '\177';
285    tty->termios.c_cc[VKILL] = '\025';
286    tty->termios.c_cc[VEOF] = '\004';
287    tty->termios.c_cc[VEOL] = '\000';
288    tty->termios.c_cc[VEOL2] = '\000';
289    tty->termios.c_cc[VSTART] = '\021';
290    tty->termios.c_cc[VSTOP] = '\023';
291    tty->termios.c_cc[VSUSP] = '\032';
292    tty->termios.c_cc[VREPRINT] = '\022';
293    tty->termios.c_cc[VDISCARD] = '\017';
294    tty->termios.c_cc[VWERASE] = '\027';
295    tty->termios.c_cc[VLNEXT] = '\026';
296
297    /* start with no flow control, clear flow control flags */
298    tty->flow_ctrl = 0;
299    /*
300     * set low/highwater mark for XON/XOFF support
301     */
302    tty->lowwater  = tty->rawInBuf.Size * 1/2;
303    tty->highwater = tty->rawInBuf.Size * 3/4;
304    /*
305     * Bump name characer
306     */
307    if (c++ == 'z')
308      c = 'a';
309
310  }
311  args->iop->data1 = tty;
312  if (!tty->refcount++) {
313    if (tty->device.firstOpen)
314      (*tty->device.firstOpen)(major, minor, arg);
315
316    /*
317     * start I/O tasks, if needed
318     */
319    if (tty->device.outputUsesInterrupts == TERMIOS_TASK_DRIVEN) {
320      sc = rtems_task_start(
321        tty->rxTaskId, rtems_termios_rxdaemon, (rtems_task_argument)tty);
322      if (sc != RTEMS_SUCCESSFUL)
323        rtems_fatal_error_occurred (sc);
324
325      sc = rtems_task_start(
326        tty->txTaskId, rtems_termios_txdaemon, (rtems_task_argument)tty);
327      if (sc != RTEMS_SUCCESSFUL)
328        rtems_fatal_error_occurred (sc);
329    }
330  }
331  rtems_semaphore_release (rtems_termios_ttyMutex);
332  return RTEMS_SUCCESSFUL;
333}
334
335/*
336 * Drain output queue
337 */
338static void
339drainOutput (struct rtems_termios_tty *tty)
340{
341  rtems_interrupt_lock_context lock_context;
342  rtems_status_code sc;
343
344  if (tty->device.outputUsesInterrupts != TERMIOS_POLLED) {
345    rtems_termios_interrupt_lock_acquire (tty, &lock_context);
346    while (tty->rawOutBuf.Tail != tty->rawOutBuf.Head) {
347      tty->rawOutBufState = rob_wait;
348      rtems_termios_interrupt_lock_release (tty, &lock_context);
349      sc = rtems_semaphore_obtain(
350        tty->rawOutBuf.Semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
351      if (sc != RTEMS_SUCCESSFUL)
352        rtems_fatal_error_occurred (sc);
353      rtems_termios_interrupt_lock_acquire (tty, &lock_context);
354    }
355    rtems_termios_interrupt_lock_release (tty, &lock_context);
356  }
357}
358
359static void
360flushOutput (struct rtems_termios_tty *tty)
361{
362  rtems_interrupt_lock_context lock_context;
363
364  rtems_termios_interrupt_lock_acquire (tty, &lock_context);
365  tty->rawOutBuf.Tail = 0;
366  tty->rawOutBuf.Head = 0;
367  tty->rawOutBufState = rob_idle;
368  rtems_termios_interrupt_lock_release (tty, &lock_context);
369}
370
371static void
372flushInput (struct rtems_termios_tty *tty)
373{
374  rtems_interrupt_lock_context lock_context;
375
376  rtems_termios_interrupt_lock_acquire (tty, &lock_context);
377  tty->rawInBuf.Tail = 0;
378  tty->rawInBuf.Head = 0;
379  rtems_termios_interrupt_lock_release (tty, &lock_context);
380}
381
382rtems_status_code
383rtems_termios_close (void *arg)
384{
385  rtems_libio_open_close_args_t *args = arg;
386  struct rtems_termios_tty *tty = args->iop->data1;
387  rtems_status_code sc;
388
389  sc = rtems_semaphore_obtain(
390    rtems_termios_ttyMutex, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
391  if (sc != RTEMS_SUCCESSFUL)
392    rtems_fatal_error_occurred (sc);
393  if (--tty->refcount == 0) {
394    if (rtems_termios_linesw[tty->t_line].l_close != NULL) {
395      /*
396       * call discipline-specific close
397       */
398      sc = rtems_termios_linesw[tty->t_line].l_close(tty);
399    } else {
400      /*
401       * default: just flush output buffer
402       */
403      sc = rtems_semaphore_obtain(tty->osem, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
404      if (sc != RTEMS_SUCCESSFUL) {
405        rtems_fatal_error_occurred (sc);
406      }
407      drainOutput (tty);
408      rtems_semaphore_release (tty->osem);
409    }
410
411    if (tty->device.outputUsesInterrupts == TERMIOS_TASK_DRIVEN) {
412      /*
413       * send "terminate" to I/O tasks
414       */
415      sc = rtems_event_send( tty->rxTaskId, TERMIOS_RX_TERMINATE_EVENT );
416      if (sc != RTEMS_SUCCESSFUL)
417        rtems_fatal_error_occurred (sc);
418      sc = rtems_event_send( tty->txTaskId, TERMIOS_TX_TERMINATE_EVENT );
419      if (sc != RTEMS_SUCCESSFUL)
420        rtems_fatal_error_occurred (sc);
421    }
422    if (tty->device.lastClose)
423       (*tty->device.lastClose)(tty->major, tty->minor, arg);
424    if (tty->forw == NULL) {
425      rtems_termios_ttyTail = tty->back;
426      if ( rtems_termios_ttyTail != NULL ) {
427        rtems_termios_ttyTail->forw = NULL;
428      }
429    } else {
430      tty->forw->back = tty->back;
431    }
432
433    if (tty->back == NULL) {
434      rtems_termios_ttyHead = tty->forw;
435      if ( rtems_termios_ttyHead != NULL ) {
436        rtems_termios_ttyHead->back = NULL;
437      }
438    } else {
439      tty->back->forw = tty->forw;
440    }
441
442    rtems_semaphore_delete (tty->isem);
443    rtems_semaphore_delete (tty->osem);
444    rtems_semaphore_delete (tty->rawOutBuf.Semaphore);
445    if ((tty->device.pollRead == NULL) ||
446        (tty->device.outputUsesInterrupts == TERMIOS_TASK_DRIVEN))
447      rtems_semaphore_delete (tty->rawInBuf.Semaphore);
448    rtems_interrupt_lock_destroy (&tty->interrupt_lock);
449    free (tty->rawInBuf.theBuf);
450    free (tty->rawOutBuf.theBuf);
451    free (tty->cbuf);
452    free (tty);
453  }
454  rtems_semaphore_release (rtems_termios_ttyMutex);
455  return RTEMS_SUCCESSFUL;
456}
457
458rtems_status_code rtems_termios_bufsize (
459  size_t cbufsize,
460  size_t raw_input,
461  size_t raw_output
462)
463{
464  rtems_termios_cbufsize        = cbufsize;
465  rtems_termios_raw_input_size  = raw_input;
466  rtems_termios_raw_output_size = raw_output;
467  return RTEMS_SUCCESSFUL;
468}
469
470static void
471termios_set_flowctrl(struct rtems_termios_tty *tty)
472{
473  rtems_interrupt_lock_context lock_context;
474  /*
475   * check for flow control options to be switched off
476   */
477
478  /* check for outgoing XON/XOFF flow control switched off */
479  if (( tty->flow_ctrl & FL_MDXON) &&
480      !(tty->termios.c_iflag & IXON)) {
481    /* clear related flags in flow_ctrl */
482    tty->flow_ctrl &= ~(FL_MDXON | FL_ORCVXOF);
483
484    /* has output been stopped due to received XOFF? */
485    if (tty->flow_ctrl & FL_OSTOP) {
486      /* disable interrupts    */
487      rtems_termios_interrupt_lock_acquire (tty, &lock_context);
488      tty->flow_ctrl &= ~FL_OSTOP;
489      /* check for chars in output buffer (or rob_state?) */
490      if (tty->rawOutBufState != rob_idle) {
491        /* if chars available, call write function... */
492        (*tty->device.write)(
493          tty->minor, &tty->rawOutBuf.theBuf[tty->rawOutBuf.Tail],1);
494      }
495      /* reenable interrupts */
496      rtems_termios_interrupt_lock_release (tty, &lock_context);
497    }
498  }
499  /* check for incoming XON/XOFF flow control switched off */
500  if (( tty->flow_ctrl & FL_MDXOF) && !(tty->termios.c_iflag & IXOFF)) {
501    /* clear related flags in flow_ctrl */
502    tty->flow_ctrl &= ~(FL_MDXOF);
503    /* FIXME: what happens, if we had sent XOFF but not yet XON? */
504    tty->flow_ctrl &= ~(FL_ISNTXOF);
505  }
506
507  /* check for incoming RTS/CTS flow control switched off */
508  if (( tty->flow_ctrl & FL_MDRTS) && !(tty->termios.c_cflag & CRTSCTS)) {
509    /* clear related flags in flow_ctrl */
510    tty->flow_ctrl &= ~(FL_MDRTS);
511
512    /* restart remote Tx, if it was stopped */
513    if ((tty->flow_ctrl & FL_IRTSOFF) && (tty->device.startRemoteTx != NULL)) {
514      tty->device.startRemoteTx(tty->minor);
515    }
516    tty->flow_ctrl &= ~(FL_IRTSOFF);
517  }
518
519  /*
520   * check for flow control options to be switched on
521   */
522  /* check for incoming RTS/CTS flow control switched on */
523  if (tty->termios.c_cflag & CRTSCTS) {
524    tty->flow_ctrl |= FL_MDRTS;
525  }
526  /* check for incoming XON/XOF flow control switched on */
527  if (tty->termios.c_iflag & IXOFF) {
528    tty->flow_ctrl |= FL_MDXOF;
529  }
530  /* check for outgoing XON/XOF flow control switched on */
531  if (tty->termios.c_iflag & IXON) {
532    tty->flow_ctrl |= FL_MDXON;
533  }
534}
535
536rtems_status_code
537rtems_termios_ioctl (void *arg)
538{
539  rtems_libio_ioctl_args_t *args = arg;
540  struct rtems_termios_tty *tty = args->iop->data1;
541  struct ttywakeup         *wakeup = (struct ttywakeup *)args->buffer;
542  rtems_status_code sc;
543
544  args->ioctl_return = 0;
545  sc = rtems_semaphore_obtain (tty->osem, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
546  if (sc != RTEMS_SUCCESSFUL) {
547    return sc;
548  }
549  switch (args->command) {
550  default:
551    if (rtems_termios_linesw[tty->t_line].l_ioctl != NULL) {
552      sc = rtems_termios_linesw[tty->t_line].l_ioctl(tty,args);
553    }
554    else {
555      sc = RTEMS_INVALID_NUMBER;
556    }
557    break;
558
559  case RTEMS_IO_GET_ATTRIBUTES:
560    *(struct termios *)args->buffer = tty->termios;
561    break;
562
563  case RTEMS_IO_SET_ATTRIBUTES:
564    tty->termios = *(struct termios *)args->buffer;
565
566    /* check for and process change in flow control options */
567    termios_set_flowctrl(tty);
568
569    if (tty->termios.c_lflag & ICANON) {
570      tty->rawInBufSemaphoreOptions = RTEMS_WAIT;
571      tty->rawInBufSemaphoreTimeout = RTEMS_NO_TIMEOUT;
572      tty->rawInBufSemaphoreFirstTimeout = RTEMS_NO_TIMEOUT;
573    } else {
574      tty->vtimeTicks = tty->termios.c_cc[VTIME] *
575                    rtems_clock_get_ticks_per_second() / 10;
576      if (tty->termios.c_cc[VTIME]) {
577        tty->rawInBufSemaphoreOptions = RTEMS_WAIT;
578        tty->rawInBufSemaphoreTimeout = tty->vtimeTicks;
579        if (tty->termios.c_cc[VMIN])
580          tty->rawInBufSemaphoreFirstTimeout = RTEMS_NO_TIMEOUT;
581        else
582          tty->rawInBufSemaphoreFirstTimeout = tty->vtimeTicks;
583      } else {
584        if (tty->termios.c_cc[VMIN]) {
585          tty->rawInBufSemaphoreOptions = RTEMS_WAIT;
586          tty->rawInBufSemaphoreTimeout = RTEMS_NO_TIMEOUT;
587          tty->rawInBufSemaphoreFirstTimeout = RTEMS_NO_TIMEOUT;
588        } else {
589          tty->rawInBufSemaphoreOptions = RTEMS_NO_WAIT;
590        }
591      }
592    }
593    if (tty->device.setAttributes)
594      (*tty->device.setAttributes)(tty->minor, &tty->termios);
595    break;
596
597  case RTEMS_IO_TCDRAIN:
598    drainOutput (tty);
599    break;
600
601  case RTEMS_IO_TCFLUSH:
602    switch ((intptr_t) args->buffer) {
603      case TCIFLUSH:
604        flushInput (tty);
605        break;
606      case TCOFLUSH:
607        flushOutput (tty);
608        break;
609      case TCIOFLUSH:
610        flushOutput (tty);
611        flushInput (tty);
612        break;
613      default:
614        sc = RTEMS_INVALID_NAME;
615        break;
616    }
617    break;
618
619  case RTEMS_IO_SNDWAKEUP:
620    tty->tty_snd = *wakeup;
621    break;
622
623  case RTEMS_IO_RCVWAKEUP:
624    tty->tty_rcv = *wakeup;
625    break;
626
627    /*
628     * FIXME: add various ioctl code handlers
629     */
630
631#if 1 /* FIXME */
632  case TIOCSETD:
633    /*
634     * close old line discipline
635     */
636    if (rtems_termios_linesw[tty->t_line].l_close != NULL) {
637      sc = rtems_termios_linesw[tty->t_line].l_close(tty);
638    }
639    tty->t_line=*(int*)(args->buffer);
640    tty->t_sc = NULL; /* ensure that no more valid data */
641    /*
642     * open new line discipline
643     */
644    if (rtems_termios_linesw[tty->t_line].l_open != NULL) {
645      sc = rtems_termios_linesw[tty->t_line].l_open(tty);
646    }
647    break;
648  case TIOCGETD:
649    *(int*)(args->buffer)=tty->t_line;
650    break;
651#endif
652   case FIONREAD: {
653      int rawnc = tty->rawInBuf.Tail - tty->rawInBuf.Head;
654      if ( rawnc < 0 )
655        rawnc += tty->rawInBuf.Size;
656      /* Half guess that this is the right operation */
657      *(int *)args->buffer = tty->ccount - tty->cindex + rawnc;
658    }
659    break;
660  }
661
662  rtems_semaphore_release (tty->osem);
663  return sc;
664}
665
666/*
667 * Send characters to device-specific code
668 */
669void
670rtems_termios_puts (
671  const void *_buf, size_t len, struct rtems_termios_tty *tty)
672{
673  const char *buf = _buf;
674  unsigned int newHead;
675  rtems_interrupt_lock_context lock_context;
676  rtems_status_code sc;
677
678  if (tty->device.outputUsesInterrupts == TERMIOS_POLLED) {
679    (*tty->device.write)(tty->minor, buf, len);
680    return;
681  }
682  newHead = tty->rawOutBuf.Head;
683  while (len) {
684    /*
685     * Performance improvement could be made here.
686     * Copy multiple bytes to raw buffer:
687     * if (len > 1) && (space to buffer end, or tail > 1)
688     *  ncopy = MIN (len, space to buffer end or tail)
689     *  memcpy (raw buffer, buf, ncopy)
690     *  buf += ncopy
691     *  len -= ncopy
692     *
693     * To minimize latency, the memcpy should be done
694     * with interrupts enabled.
695     */
696    newHead = (newHead + 1) % tty->rawOutBuf.Size;
697    rtems_termios_interrupt_lock_acquire (tty, &lock_context);
698    while (newHead == tty->rawOutBuf.Tail) {
699      tty->rawOutBufState = rob_wait;
700      rtems_termios_interrupt_lock_release (tty, &lock_context);
701      sc = rtems_semaphore_obtain(
702        tty->rawOutBuf.Semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
703      if (sc != RTEMS_SUCCESSFUL)
704        rtems_fatal_error_occurred (sc);
705      rtems_termios_interrupt_lock_acquire (tty, &lock_context);
706    }
707    tty->rawOutBuf.theBuf[tty->rawOutBuf.Head] = *buf++;
708    tty->rawOutBuf.Head = newHead;
709    if (tty->rawOutBufState == rob_idle) {
710      /* check, whether XOFF has been received */
711      if (!(tty->flow_ctrl & FL_ORCVXOF)) {
712        (*tty->device.write)(
713          tty->minor, &tty->rawOutBuf.theBuf[tty->rawOutBuf.Tail],1);
714      } else {
715        /* remember that output has been stopped due to flow ctrl*/
716        tty->flow_ctrl |= FL_OSTOP;
717      }
718      tty->rawOutBufState = rob_busy;
719    }
720    rtems_termios_interrupt_lock_release (tty, &lock_context);
721    len--;
722  }
723}
724
725/*
726 * Handle output processing
727 */
728static void
729oproc (unsigned char c, struct rtems_termios_tty *tty)
730{
731  int  i;
732
733  if (tty->termios.c_oflag & OPOST) {
734    switch (c) {
735    case '\n':
736      if (tty->termios.c_oflag & ONLRET)
737        tty->column = 0;
738      if (tty->termios.c_oflag & ONLCR) {
739        rtems_termios_puts ("\r", 1, tty);
740        tty->column = 0;
741      }
742      break;
743
744    case '\r':
745      if ((tty->termios.c_oflag & ONOCR) && (tty->column == 0))
746        return;
747      if (tty->termios.c_oflag & OCRNL) {
748        c = '\n';
749        if (tty->termios.c_oflag & ONLRET)
750          tty->column = 0;
751        break;
752      }
753      tty->column = 0;
754      break;
755
756    case '\t':
757      i = 8 - (tty->column & 7);
758      if ((tty->termios.c_oflag & TABDLY) == XTABS) {
759        tty->column += i;
760        rtems_termios_puts ( "        ",  i, tty);
761        return;
762      }
763      tty->column += i;
764      break;
765
766    case '\b':
767      if (tty->column > 0)
768        tty->column--;
769      break;
770
771    default:
772      if (tty->termios.c_oflag & OLCUC)
773        c = toupper(c);
774      if (!iscntrl(c))
775        tty->column++;
776      break;
777    }
778  }
779  rtems_termios_puts (&c, 1, tty);
780}
781
782rtems_status_code
783rtems_termios_write (void *arg)
784{
785  rtems_libio_rw_args_t *args = arg;
786  struct rtems_termios_tty *tty = args->iop->data1;
787  rtems_status_code sc;
788
789  sc = rtems_semaphore_obtain (tty->osem, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
790  if (sc != RTEMS_SUCCESSFUL)
791    return sc;
792  if (rtems_termios_linesw[tty->t_line].l_write != NULL) {
793    sc = rtems_termios_linesw[tty->t_line].l_write(tty,args);
794    rtems_semaphore_release (tty->osem);
795    return sc;
796  }
797  if (tty->termios.c_oflag & OPOST) {
798    uint32_t   count = args->count;
799    char      *buffer = args->buffer;
800    while (count--)
801      oproc (*buffer++, tty);
802    args->bytes_moved = args->count;
803  } else {
804    rtems_termios_puts (args->buffer, args->count, tty);
805    args->bytes_moved = args->count;
806  }
807  rtems_semaphore_release (tty->osem);
808  return sc;
809}
810
811/*
812 * Echo a typed character
813 */
814static void
815echo (unsigned char c, struct rtems_termios_tty *tty)
816{
817  if ((tty->termios.c_lflag & ECHOCTL) &&
818       iscntrl(c) && (c != '\t') && (c != '\n')) {
819    char echobuf[2];
820
821    echobuf[0] = '^';
822    echobuf[1] = c ^ 0x40;
823    rtems_termios_puts (echobuf, 2, tty);
824    tty->column += 2;
825  } else {
826    oproc (c, tty);
827  }
828}
829
830/*
831 * Erase a character or line
832 * FIXME: Needs support for WERASE and ECHOPRT.
833 * FIXME: Some of the tests should check for IEXTEN, too.
834 */
835static void
836erase (struct rtems_termios_tty *tty, int lineFlag)
837{
838  if (tty->ccount == 0)
839    return;
840  if (lineFlag) {
841    if (!(tty->termios.c_lflag & ECHO)) {
842      tty->ccount = 0;
843      return;
844    }
845    if (!(tty->termios.c_lflag & ECHOE)) {
846      tty->ccount = 0;
847      echo (tty->termios.c_cc[VKILL], tty);
848      if (tty->termios.c_lflag & ECHOK)
849        echo ('\n', tty);
850      return;
851    }
852  }
853
854  while (tty->ccount) {
855    unsigned char c = tty->cbuf[--tty->ccount];
856
857    if (tty->termios.c_lflag & ECHO) {
858      if (!lineFlag && !(tty->termios.c_lflag & ECHOE)) {
859        echo (tty->termios.c_cc[VERASE], tty);
860      } else if (c == '\t') {
861        int col = tty->read_start_column;
862        int i = 0;
863
864        /*
865         * Find the character before the tab
866         */
867        while (i != tty->ccount) {
868          c = tty->cbuf[i++];
869          if (c == '\t') {
870            col = (col | 7) + 1;
871          } else if (iscntrl (c)) {
872            if (tty->termios.c_lflag & ECHOCTL)
873              col += 2;
874          } else {
875            col++;
876          }
877        }
878
879        /*
880         * Back up over the tab
881         */
882        while (tty->column > col) {
883          rtems_termios_puts ("\b", 1, tty);
884          tty->column--;
885        }
886      }
887      else {
888        if (iscntrl (c) && (tty->termios.c_lflag & ECHOCTL)) {
889          rtems_termios_puts ("\b \b", 3, tty);
890          if (tty->column)
891            tty->column--;
892        }
893        if (!iscntrl (c) || (tty->termios.c_lflag & ECHOCTL)) {
894          rtems_termios_puts ("\b \b", 3, tty);
895          if (tty->column)
896            tty->column--;
897        }
898      }
899    }
900    if (!lineFlag)
901      break;
902  }
903}
904
905/*
906 * Process a single input character
907 */
908static int
909iproc (unsigned char c, struct rtems_termios_tty *tty)
910{
911  if (tty->termios.c_iflag & ISTRIP)
912    c &= 0x7f;
913
914  if (tty->termios.c_iflag & IUCLC)
915    c = tolower (c);
916
917  if (c == '\r') {
918    if (tty->termios.c_iflag & IGNCR)
919      return 0;
920    if (tty->termios.c_iflag & ICRNL)
921      c = '\n';
922  } else if ((c == '\n') && (tty->termios.c_iflag & INLCR)) {
923    c = '\r';
924  }
925
926  if ((c != '\0') && (tty->termios.c_lflag & ICANON)) {
927    if (c == tty->termios.c_cc[VERASE]) {
928      erase (tty, 0);
929      return 0;
930    }
931    else if (c == tty->termios.c_cc[VKILL]) {
932      erase (tty, 1);
933      return 0;
934    }
935    else if (c == tty->termios.c_cc[VEOF]) {
936      return 1;
937    } else if (c == '\n') {
938      if (tty->termios.c_lflag & (ECHO | ECHONL))
939        echo (c, tty);
940      tty->cbuf[tty->ccount++] = c;
941      return 1;
942    } else if ((c == tty->termios.c_cc[VEOL]) ||
943               (c == tty->termios.c_cc[VEOL2])) {
944      if (tty->termios.c_lflag & ECHO)
945        echo (c, tty);
946      tty->cbuf[tty->ccount++] = c;
947      return 1;
948    }
949  }
950
951  /*
952   * FIXME: Should do IMAXBEL handling somehow
953   */
954  if (tty->ccount < (CBUFSIZE-1)) {
955    if (tty->termios.c_lflag & ECHO)
956      echo (c, tty);
957    tty->cbuf[tty->ccount++] = c;
958  }
959  return 0;
960}
961
962/*
963 * Process input character, with semaphore.
964 */
965static int
966siproc (unsigned char c, struct rtems_termios_tty *tty)
967{
968  int i;
969
970  /*
971   * Obtain output semaphore if character will be echoed
972   */
973  if (tty->termios.c_lflag & (ECHO|ECHOE|ECHOK|ECHONL|ECHOPRT|ECHOCTL|ECHOKE)) {
974    rtems_status_code sc;
975    sc = rtems_semaphore_obtain (tty->osem, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
976    if (sc != RTEMS_SUCCESSFUL)
977      rtems_fatal_error_occurred (sc);
978    i = iproc (c, tty);
979    sc = rtems_semaphore_release (tty->osem);
980    if (sc != RTEMS_SUCCESSFUL)
981      rtems_fatal_error_occurred (sc);
982  }
983  else {
984    i = iproc (c, tty);
985  }
986  return i;
987}
988
989/*
990 * Fill the input buffer by polling the device
991 */
992static rtems_status_code
993fillBufferPoll (struct rtems_termios_tty *tty)
994{
995  int n;
996
997  if (tty->termios.c_lflag & ICANON) {
998    for (;;) {
999      n = (*tty->device.pollRead)(tty->minor);
1000      if (n < 0) {
1001        rtems_task_wake_after (1);
1002      } else {
1003        if  (siproc (n, tty))
1004          break;
1005      }
1006    }
1007  } else {
1008    rtems_interval then, now;
1009
1010    then = rtems_clock_get_ticks_since_boot();
1011    for (;;) {
1012      n = (*tty->device.pollRead)(tty->minor);
1013      if (n < 0) {
1014        if (tty->termios.c_cc[VMIN]) {
1015          if (tty->termios.c_cc[VTIME] && tty->ccount) {
1016            now = rtems_clock_get_ticks_since_boot();
1017            if ((now - then) > tty->vtimeTicks) {
1018              break;
1019            }
1020          }
1021        } else {
1022          if (!tty->termios.c_cc[VTIME])
1023            break;
1024          now = rtems_clock_get_ticks_since_boot();
1025          if ((now - then) > tty->vtimeTicks) {
1026            break;
1027          }
1028        }
1029        rtems_task_wake_after (1);
1030      } else {
1031        siproc (n, tty);
1032        if (tty->ccount >= tty->termios.c_cc[VMIN])
1033          break;
1034        if (tty->termios.c_cc[VMIN] && tty->termios.c_cc[VTIME])
1035          then = rtems_clock_get_ticks_since_boot();
1036      }
1037    }
1038  }
1039  return RTEMS_SUCCESSFUL;
1040}
1041
1042/*
1043 * Fill the input buffer from the raw input queue
1044 */
1045static rtems_status_code
1046fillBufferQueue (struct rtems_termios_tty *tty)
1047{
1048  rtems_interval timeout = tty->rawInBufSemaphoreFirstTimeout;
1049  rtems_status_code sc;
1050  int               wait = 1;
1051
1052  while ( wait ) {
1053    /*
1054     * Process characters read from raw queue
1055     */
1056    while ((tty->rawInBuf.Head != tty->rawInBuf.Tail) &&
1057                       (tty->ccount < (CBUFSIZE-1))) {
1058      unsigned char c;
1059      unsigned int newHead;
1060
1061      newHead = (tty->rawInBuf.Head + 1) % tty->rawInBuf.Size;
1062      c = tty->rawInBuf.theBuf[newHead];
1063      tty->rawInBuf.Head = newHead;
1064      if(((tty->rawInBuf.Tail-newHead+tty->rawInBuf.Size)
1065          % tty->rawInBuf.Size)
1066         < tty->lowwater) {
1067        tty->flow_ctrl &= ~FL_IREQXOF;
1068        /* if tx stopped and XON should be sent... */
1069        if (((tty->flow_ctrl & (FL_MDXON | FL_ISNTXOF))
1070             ==                (FL_MDXON | FL_ISNTXOF))
1071            && ((tty->rawOutBufState == rob_idle)
1072          || (tty->flow_ctrl & FL_OSTOP))) {
1073          /* XON should be sent now... */
1074          (*tty->device.write)(
1075            tty->minor, (void *)&(tty->termios.c_cc[VSTART]), 1);
1076        } else if (tty->flow_ctrl & FL_MDRTS) {
1077          tty->flow_ctrl &= ~FL_IRTSOFF;
1078          /* activate RTS line */
1079          if (tty->device.startRemoteTx != NULL) {
1080            tty->device.startRemoteTx(tty->minor);
1081          }
1082        }
1083      }
1084
1085      /* continue processing new character */
1086      if (tty->termios.c_lflag & ICANON) {
1087        if (siproc (c, tty))
1088          wait = 0;
1089      } else {
1090        siproc (c, tty);
1091        if (tty->ccount >= tty->termios.c_cc[VMIN])
1092          wait = 0;
1093      }
1094      timeout = tty->rawInBufSemaphoreTimeout;
1095    }
1096
1097    /*
1098     * Wait for characters
1099     */
1100    if ( wait ) {
1101      sc = rtems_semaphore_obtain(
1102        tty->rawInBuf.Semaphore, tty->rawInBufSemaphoreOptions, timeout);
1103      if (sc != RTEMS_SUCCESSFUL)
1104        break;
1105    }
1106  }
1107  return RTEMS_SUCCESSFUL;
1108}
1109
1110rtems_status_code
1111rtems_termios_read (void *arg)
1112{
1113  rtems_libio_rw_args_t *args = arg;
1114  struct rtems_termios_tty *tty = args->iop->data1;
1115  uint32_t   count = args->count;
1116  char      *buffer = args->buffer;
1117  rtems_status_code sc;
1118
1119  sc = rtems_semaphore_obtain (tty->isem, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
1120  if (sc != RTEMS_SUCCESSFUL)
1121    return sc;
1122
1123  if (rtems_termios_linesw[tty->t_line].l_read != NULL) {
1124    sc = rtems_termios_linesw[tty->t_line].l_read(tty,args);
1125    tty->tty_rcvwakeup = 0;
1126    rtems_semaphore_release (tty->isem);
1127    return sc;
1128  }
1129
1130  if (tty->cindex == tty->ccount) {
1131    tty->cindex = tty->ccount = 0;
1132    tty->read_start_column = tty->column;
1133    if (tty->device.pollRead != NULL &&
1134        tty->device.outputUsesInterrupts == TERMIOS_POLLED)
1135      sc = fillBufferPoll (tty);
1136    else
1137      sc = fillBufferQueue (tty);
1138
1139    if (sc != RTEMS_SUCCESSFUL)
1140      tty->cindex = tty->ccount = 0;
1141  }
1142  while (count && (tty->cindex < tty->ccount)) {
1143    *buffer++ = tty->cbuf[tty->cindex++];
1144    count--;
1145  }
1146  args->bytes_moved = args->count - count;
1147  tty->tty_rcvwakeup = 0;
1148  rtems_semaphore_release (tty->isem);
1149  return sc;
1150}
1151
1152/*
1153 * signal receive interrupt to rx daemon
1154 * NOTE: This routine runs in the context of the
1155 *       device receive interrupt handler.
1156 */
1157void rtems_termios_rxirq_occured(struct rtems_termios_tty *tty)
1158{
1159  /*
1160   * send event to rx daemon task
1161   */
1162  rtems_event_send(tty->rxTaskId,TERMIOS_RX_PROC_EVENT);
1163}
1164
1165/*
1166 * Place characters on raw queue.
1167 * NOTE: This routine runs in the context of the
1168 *       device receive interrupt handler.
1169 * Returns the number of characters dropped because of overflow.
1170 */
1171int
1172rtems_termios_enqueue_raw_characters (void *ttyp, const char *buf, int len)
1173{
1174  struct rtems_termios_tty *tty = ttyp;
1175  unsigned int newTail;
1176  char c;
1177  int dropped = 0;
1178  bool flow_rcv = false; /* true, if flow control char received */
1179  rtems_interrupt_lock_context lock_context;
1180
1181  if (rtems_termios_linesw[tty->t_line].l_rint != NULL) {
1182    while (len--) {
1183      c = *buf++;
1184      rtems_termios_linesw[tty->t_line].l_rint(c,tty);
1185    }
1186
1187    /*
1188     * check to see if rcv wakeup callback was set
1189     */
1190    if (( !tty->tty_rcvwakeup ) && ( tty->tty_rcv.sw_pfn != NULL )) {
1191      (*tty->tty_rcv.sw_pfn)(&tty->termios, tty->tty_rcv.sw_arg);
1192      tty->tty_rcvwakeup = 1;
1193        }
1194    return 0;
1195  }
1196
1197  while (len--) {
1198    c = *buf++;
1199    /* FIXME: implement IXANY: any character restarts output */
1200    /* if incoming XON/XOFF controls outgoing stream: */
1201    if (tty->flow_ctrl & FL_MDXON) {
1202      /* if received char is V_STOP and V_START (both are equal value) */
1203      if (c == tty->termios.c_cc[VSTOP]) {
1204        if (c == tty->termios.c_cc[VSTART]) {
1205          /* received VSTOP and VSTART==VSTOP? */
1206          /* then toggle "stop output" status  */
1207          tty->flow_ctrl = tty->flow_ctrl ^ FL_ORCVXOF;
1208        }
1209        else {
1210          /* VSTOP received (other code than VSTART) */
1211          /* stop output                             */
1212          tty->flow_ctrl |= FL_ORCVXOF;
1213        }
1214        flow_rcv = true;
1215      }
1216      else if (c == tty->termios.c_cc[VSTART]) {
1217        /* VSTART received */
1218        /* restart output  */
1219        tty->flow_ctrl &= ~FL_ORCVXOF;
1220        flow_rcv = true;
1221      }
1222    }
1223    if (flow_rcv) {
1224      /* restart output according to FL_ORCVXOF flag */
1225      if ((tty->flow_ctrl & (FL_ORCVXOF | FL_OSTOP)) == FL_OSTOP) {
1226        /* disable interrupts    */
1227        rtems_termios_interrupt_lock_acquire (tty, &lock_context);
1228        tty->flow_ctrl &= ~FL_OSTOP;
1229        /* check for chars in output buffer (or rob_state?) */
1230        if (tty->rawOutBufState != rob_idle) {
1231          /* if chars available, call write function... */
1232          (*tty->device.write)(
1233            tty->minor, &tty->rawOutBuf.theBuf[tty->rawOutBuf.Tail], 1);
1234        }
1235        /* reenable interrupts */
1236        rtems_termios_interrupt_lock_release (tty, &lock_context);
1237      }
1238    } else {
1239      newTail = (tty->rawInBuf.Tail + 1) % tty->rawInBuf.Size;
1240      /* if chars_in_buffer > highwater                */
1241      rtems_termios_interrupt_lock_acquire (tty, &lock_context);
1242      if ((((newTail - tty->rawInBuf.Head + tty->rawInBuf.Size)
1243            % tty->rawInBuf.Size) > tty->highwater) &&
1244          !(tty->flow_ctrl & FL_IREQXOF)) {
1245        /* incoming data stream should be stopped */
1246        tty->flow_ctrl |= FL_IREQXOF;
1247        if ((tty->flow_ctrl & (FL_MDXOF | FL_ISNTXOF))
1248            ==                (FL_MDXOF             ) ) {
1249          if ((tty->flow_ctrl & FL_OSTOP) ||
1250              (tty->rawOutBufState == rob_idle)) {
1251            /* if tx is stopped due to XOFF or out of data */
1252            /*    call write function here                 */
1253            tty->flow_ctrl |= FL_ISNTXOF;
1254            (*tty->device.write)(tty->minor,
1255                (void *)&(tty->termios.c_cc[VSTOP]), 1);
1256          }
1257        } else if ((tty->flow_ctrl & (FL_MDRTS | FL_IRTSOFF)) == (FL_MDRTS) ) {
1258          tty->flow_ctrl |= FL_IRTSOFF;
1259          /* deactivate RTS line */
1260          if (tty->device.stopRemoteTx != NULL) {
1261            tty->device.stopRemoteTx(tty->minor);
1262          }
1263        }
1264      }
1265
1266      /* reenable interrupts */
1267      rtems_termios_interrupt_lock_release (tty, &lock_context);
1268
1269      if (newTail == tty->rawInBuf.Head) {
1270        dropped++;
1271      } else {
1272        tty->rawInBuf.theBuf[newTail] = c;
1273        tty->rawInBuf.Tail = newTail;
1274
1275        /*
1276         * check to see if rcv wakeup callback was set
1277         */
1278        if (( !tty->tty_rcvwakeup ) && ( tty->tty_rcv.sw_pfn != NULL )) {
1279          (*tty->tty_rcv.sw_pfn)(&tty->termios, tty->tty_rcv.sw_arg);
1280          tty->tty_rcvwakeup = 1;
1281        }
1282      }
1283    }
1284  }
1285
1286  tty->rawInBufDropped += dropped;
1287  rtems_semaphore_release (tty->rawInBuf.Semaphore);
1288  return dropped;
1289}
1290
1291/*
1292 * in task-driven mode, this function is called in Tx task context
1293 * in interrupt-driven mode, this function is called in TxIRQ context
1294 */
1295static int
1296rtems_termios_refill_transmitter (struct rtems_termios_tty *tty)
1297{
1298  bool wakeUpWriterTask = false;
1299  unsigned int newTail;
1300  int nToSend;
1301  rtems_interrupt_lock_context lock_context;
1302  int len;
1303
1304  rtems_termios_interrupt_lock_acquire (tty, &lock_context);
1305
1306  /* check for XOF/XON to send */
1307  if ((tty->flow_ctrl & (FL_MDXOF | FL_IREQXOF | FL_ISNTXOF))
1308      == (FL_MDXOF | FL_IREQXOF)) {
1309    /* XOFF should be sent now... */
1310    (*tty->device.write)(tty->minor, (void *)&(tty->termios.c_cc[VSTOP]), 1);
1311
1312    tty->t_dqlen--;
1313    tty->flow_ctrl |= FL_ISNTXOF;
1314
1315    nToSend = 1;
1316
1317  } else if ((tty->flow_ctrl & (FL_IREQXOF | FL_ISNTXOF)) == FL_ISNTXOF) {
1318    /* NOTE: send XON even, if no longer in XON/XOFF mode... */
1319    /* XON should be sent now... */
1320    /*
1321     * FIXME: this .write call will generate another
1322     * dequeue callback. This will advance the "Tail" in the data
1323     * buffer, although the corresponding data is not yet out!
1324     * Therefore the dequeue "length" should be reduced by 1
1325     */
1326    (*tty->device.write)(tty->minor, (void *)&(tty->termios.c_cc[VSTART]), 1);
1327
1328    tty->t_dqlen--;
1329    tty->flow_ctrl &= ~FL_ISNTXOF;
1330
1331    nToSend = 1;
1332  } else if ( tty->rawOutBuf.Head == tty->rawOutBuf.Tail ) {
1333    /*
1334     * buffer was empty
1335     */
1336    if (tty->rawOutBufState == rob_wait) {
1337      /*
1338       * this should never happen...
1339       */
1340      wakeUpWriterTask = true;
1341    }
1342
1343    (*tty->device.write) (tty->minor, NULL, 0);
1344    nToSend = 0;
1345  } else {
1346    len = tty->t_dqlen;
1347    tty->t_dqlen = 0;
1348
1349    newTail = (tty->rawOutBuf.Tail + len) % tty->rawOutBuf.Size;
1350    tty->rawOutBuf.Tail = newTail;
1351    if (tty->rawOutBufState == rob_wait) {
1352      /*
1353       * wake up any pending writer task
1354       */
1355      wakeUpWriterTask = true;
1356    }
1357
1358    if (newTail == tty->rawOutBuf.Head) {
1359      /*
1360       * Buffer has become empty
1361       */
1362      tty->rawOutBufState = rob_idle;
1363      (*tty->device.write) (tty->minor, NULL, 0);
1364      nToSend = 0;
1365
1366      /*
1367       * check to see if snd wakeup callback was set
1368       */
1369      if ( tty->tty_snd.sw_pfn != NULL) {
1370        (*tty->tty_snd.sw_pfn)(&tty->termios, tty->tty_snd.sw_arg);
1371      }
1372    }
1373    /* check, whether output should stop due to received XOFF */
1374    else if ((tty->flow_ctrl & (FL_MDXON | FL_ORCVXOF))
1375       ==                (FL_MDXON | FL_ORCVXOF)) {
1376      /* Buffer not empty, but output stops due to XOFF */
1377      /* set flag, that output has been stopped */
1378      tty->flow_ctrl |= FL_OSTOP;
1379      tty->rawOutBufState = rob_busy; /*apm*/
1380      (*tty->device.write) (tty->minor, NULL, 0);
1381      nToSend = 0;
1382    } else {
1383      /*
1384       * Buffer not empty, start tranmitter
1385       */
1386      if (newTail > tty->rawOutBuf.Head)
1387        nToSend = tty->rawOutBuf.Size - newTail;
1388      else
1389        nToSend = tty->rawOutBuf.Head - newTail;
1390      /* when flow control XON or XOF, don't send blocks of data     */
1391      /* to allow fast reaction on incoming flow ctrl and low latency*/
1392      /* for outgoing flow control                                   */
1393      if (tty->flow_ctrl & (FL_MDXON | FL_MDXOF)) {
1394        nToSend = 1;
1395      }
1396      tty->rawOutBufState = rob_busy; /*apm*/
1397      (*tty->device.write)(
1398        tty->minor, &tty->rawOutBuf.theBuf[newTail], nToSend);
1399    }
1400    tty->rawOutBuf.Tail = newTail; /*apm*/
1401  }
1402
1403  rtems_termios_interrupt_lock_release (tty, &lock_context);
1404
1405  if (wakeUpWriterTask) {
1406    rtems_semaphore_release (tty->rawOutBuf.Semaphore);
1407  }
1408
1409  return nToSend;
1410}
1411
1412/*
1413 * Characters have been transmitted
1414 * NOTE: This routine runs in the context of the
1415 *       device transmit interrupt handler.
1416 * The second argument is the number of characters transmitted so far.
1417 * This value will always be 1 for devices which generate an interrupt
1418 * for each transmitted character.
1419 * It returns number of characters left to transmit
1420 */
1421int
1422rtems_termios_dequeue_characters (void *ttyp, int len)
1423{
1424  struct rtems_termios_tty *tty = ttyp;
1425  rtems_status_code sc;
1426
1427  /*
1428   * sum up character count already sent
1429   */
1430  tty->t_dqlen += len;
1431
1432  if (tty->device.outputUsesInterrupts == TERMIOS_TASK_DRIVEN) {
1433    /*
1434     * send wake up to transmitter task
1435     */
1436    sc = rtems_event_send(tty->txTaskId, TERMIOS_TX_START_EVENT);
1437    if (sc != RTEMS_SUCCESSFUL)
1438      rtems_fatal_error_occurred (sc);
1439    return 0; /* nothing to output in IRQ... */
1440  }
1441
1442  if (tty->t_line == PPPDISC ) {
1443    /*
1444     * call any line discipline start function
1445     */
1446    if (rtems_termios_linesw[tty->t_line].l_start != NULL) {
1447      rtems_termios_linesw[tty->t_line].l_start(tty);
1448    }
1449    return 0; /* nothing to output in IRQ... */
1450  }
1451
1452  return rtems_termios_refill_transmitter(tty);
1453}
1454
1455/*
1456 * this task actually processes any transmit events
1457 */
1458static rtems_task rtems_termios_txdaemon(rtems_task_argument argument)
1459{
1460  struct rtems_termios_tty *tty = (struct rtems_termios_tty *)argument;
1461  rtems_event_set the_event;
1462
1463  while (1) {
1464    /*
1465     * wait for rtems event
1466     */
1467    rtems_event_receive(
1468       (TERMIOS_TX_START_EVENT | TERMIOS_TX_TERMINATE_EVENT),
1469       RTEMS_EVENT_ANY | RTEMS_WAIT,
1470       RTEMS_NO_TIMEOUT,
1471       &the_event
1472    );
1473    if ((the_event & TERMIOS_TX_TERMINATE_EVENT) != 0) {
1474      tty->txTaskId = 0;
1475      rtems_task_delete(RTEMS_SELF);
1476    }
1477
1478    /*
1479     * call any line discipline start function
1480     */
1481    if (rtems_termios_linesw[tty->t_line].l_start != NULL) {
1482      rtems_termios_linesw[tty->t_line].l_start(tty);
1483    }
1484
1485    /*
1486     * try to push further characters to device
1487     */
1488    rtems_termios_refill_transmitter(tty);
1489  }
1490}
1491
1492/*
1493 * this task actually processes any receive events
1494 */
1495static rtems_task rtems_termios_rxdaemon(rtems_task_argument argument)
1496{
1497  struct rtems_termios_tty *tty = (struct rtems_termios_tty *)argument;
1498  rtems_event_set the_event;
1499  int c;
1500  char c_buf;
1501
1502  while (1) {
1503    /*
1504     * wait for rtems event
1505     */
1506    rtems_event_receive(
1507      (TERMIOS_RX_PROC_EVENT | TERMIOS_RX_TERMINATE_EVENT),
1508      RTEMS_EVENT_ANY | RTEMS_WAIT,
1509      RTEMS_NO_TIMEOUT,
1510      &the_event
1511    );
1512    if ((the_event & TERMIOS_RX_TERMINATE_EVENT) != 0) {
1513      tty->rxTaskId = 0;
1514      rtems_task_delete(RTEMS_SELF);
1515    }
1516
1517    /*
1518     * do something
1519     */
1520    c = tty->device.pollRead(tty->minor);
1521    if (c != EOF) {
1522      /*
1523       * pollRead did call enqueue on its own
1524       */
1525      c_buf = c;
1526      rtems_termios_enqueue_raw_characters ( tty,&c_buf,1);
1527    }
1528  }
1529}
Note: See TracBrowser for help on using the repository browser.