source: rtems/cpukit/libcsupport/src/termios.c @ 9d1522ed

5
Last change on this file since 9d1522ed was 9d1522ed, checked in by Sebastian Huber <sebastian.huber@…>, on 07/28/17 at 11:25:25

Fix IO control request type

  • Property mode set to 100644
File size: 59.1 KB
Line 
1/**
2 * @file
3 * TERMIOS serial line support
4 */
5
6/*
7 *  Author:
8 *    W. Eric Norum
9 *    Saskatchewan Accelerator Laboratory
10 *    University of Saskatchewan
11 *    Saskatoon, Saskatchewan, CANADA
12 *    eric@skatter.usask.ca
13 *
14 *  The license and distribution terms for this file may be
15 *  found in the file LICENSE in this distribution or at
16 *  http://www.rtems.org/license/LICENSE.
17 */
18
19#if HAVE_CONFIG_H
20#include "config.h"
21#endif
22
23#include <rtems.h>
24#include <rtems/libio.h>
25#include <rtems/imfs.h>
26#include <rtems/score/assert.h>
27#include <ctype.h>
28#include <errno.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <termios.h>
32#include <unistd.h>
33#include <sys/fcntl.h>
34#include <sys/filio.h>
35#include <sys/ttycom.h>
36
37#include <rtems/termiostypes.h>
38
39/*
40 * The size of the cooked buffer
41 */
42#define CBUFSIZE  (rtems_termios_cbufsize)
43
44/*
45 * The sizes of the raw message buffers.
46 * On most architectures it is quite a bit more
47 * efficient if these are powers of two.
48 */
49#define RAW_INPUT_BUFFER_SIZE  (rtems_termios_raw_input_size)
50#define RAW_OUTPUT_BUFFER_SIZE  (rtems_termios_raw_output_size)
51
52/* fields for "flow_ctrl" status */
53#define FL_IREQXOF 1U        /* input queue requests stop of incoming data */
54#define FL_ISNTXOF 2U        /* XOFF has been sent to other side of line   */
55#define FL_IRTSOFF 4U        /* RTS has been turned off for other side..   */
56
57#define FL_ORCVXOF 0x10U     /* XOFF has been received                     */
58#define FL_OSTOP   0x20U     /* output has been stopped due to XOFF        */
59
60#define FL_MDRTS   0x100U    /* input controlled with RTS/CTS handshake    */
61#define FL_MDXON   0x200U    /* input controlled with XON/XOFF protocol    */
62#define FL_MDXOF   0x400U    /* output controlled with XON/XOFF protocol   */
63
64#define NODISC(n) \
65  { NULL,  NULL,  NULL,  NULL, \
66    NULL,  NULL,  NULL,  NULL }
67/*
68 * FIXME: change rtems_termios_linesw entries consistent
69 *        with rtems_termios_linesw entry usage...
70 */
71struct  rtems_termios_linesw rtems_termios_linesw[MAXLDISC] =
72{
73  NODISC(0),    /* 0- termios-built-in */
74  NODISC(1),    /* 1- defunct */
75  NODISC(2),    /* 2- NTTYDISC */
76  NODISC(3),    /* TABLDISC */
77  NODISC(4),    /* SLIPDISC */
78  NODISC(5),    /* PPPDISC */
79  NODISC(6),    /* loadable */
80  NODISC(7),    /* loadable */
81};
82
83int  rtems_termios_nlinesw =
84       sizeof (rtems_termios_linesw) / sizeof (rtems_termios_linesw[0]);
85
86extern rtems_id rtems_termios_ttyMutex;
87
88static size_t rtems_termios_cbufsize = 256;
89static size_t rtems_termios_raw_input_size = 256;
90static size_t rtems_termios_raw_output_size = 64;
91
92static const IMFS_node_control rtems_termios_imfs_node_control;
93
94static struct rtems_termios_tty *rtems_termios_ttyHead;
95static struct rtems_termios_tty *rtems_termios_ttyTail;
96
97static RTEMS_CHAIN_DEFINE_EMPTY(rtems_termios_devices);
98
99static rtems_task rtems_termios_rxdaemon(rtems_task_argument argument);
100static rtems_task rtems_termios_txdaemon(rtems_task_argument argument);
101/*
102 * some constants for I/O daemon task creation
103 */
104#define TERMIOS_TXTASK_PRIO 10
105#define TERMIOS_RXTASK_PRIO 9
106#define TERMIOS_TXTASK_STACKSIZE 1024
107#define TERMIOS_RXTASK_STACKSIZE 1024
108/*
109 * some events to be sent to the I/O tasks
110 */
111#define TERMIOS_TX_START_EVENT     RTEMS_EVENT_1
112#define TERMIOS_TX_TERMINATE_EVENT RTEMS_EVENT_0
113
114#define TERMIOS_RX_PROC_EVENT      RTEMS_EVENT_1
115#define TERMIOS_RX_TERMINATE_EVENT RTEMS_EVENT_0
116
117static rtems_status_code
118rtems_termios_obtain (void)
119{
120  return rtems_semaphore_obtain (rtems_termios_ttyMutex, RTEMS_WAIT,
121    RTEMS_NO_TIMEOUT);
122}
123
124static void
125rtems_termios_release (void)
126{
127  rtems_status_code sc;
128
129  sc = rtems_semaphore_release (rtems_termios_ttyMutex);
130  _Assert (sc == RTEMS_SUCCESSFUL);
131  (void) sc;
132}
133
134rtems_status_code rtems_termios_device_install(
135  const char                         *device_file,
136  const rtems_termios_device_handler *handler,
137  const rtems_termios_device_flow    *flow,
138  rtems_termios_device_context       *context
139)
140{
141  rtems_termios_device_node *new_device_node;
142  int rv;
143
144  new_device_node = calloc (1, sizeof(*new_device_node));
145  if (new_device_node == NULL) {
146    return RTEMS_NO_MEMORY;
147  }
148
149  rtems_chain_initialize_node (&new_device_node->node);
150  new_device_node->handler = handler;
151  new_device_node->flow = flow;
152  new_device_node->context = context;
153  new_device_node->tty = NULL;
154
155  rv = IMFS_make_generic_node(
156    device_file,
157    S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO,
158    &rtems_termios_imfs_node_control,
159    new_device_node
160  );
161  if (rv != 0) {
162    free (new_device_node);
163    return RTEMS_UNSATISFIED;
164  }
165
166  return RTEMS_SUCCESSFUL;
167}
168
169static rtems_termios_tty *
170legacyContextToTTY (rtems_termios_device_context *ctx)
171{
172  return RTEMS_CONTAINER_OF (ctx, rtems_termios_tty, legacy_device_context);
173}
174
175static bool
176rtems_termios_callback_firstOpen(
177  rtems_termios_tty             *tty,
178  rtems_termios_device_context  *ctx,
179  struct termios                *term,
180  rtems_libio_open_close_args_t *args
181)
182{
183  (void) ctx;
184  (void) term;
185
186  (*tty->device.firstOpen) (tty->major, tty->minor, args);
187
188  return true;
189}
190
191static void
192rtems_termios_callback_lastClose(
193  rtems_termios_tty             *tty,
194  rtems_termios_device_context  *ctx,
195  rtems_libio_open_close_args_t *args
196)
197{
198  (void) ctx;
199
200  (*tty->device.lastClose) (tty->major, tty->minor, args);
201}
202
203static int
204rtems_termios_callback_pollRead (rtems_termios_device_context *ctx)
205{
206  rtems_termios_tty *tty = legacyContextToTTY (ctx);
207
208  return (*tty->device.pollRead) (tty->minor);
209}
210
211static void
212rtems_termios_callback_write(
213  rtems_termios_device_context *ctx,
214  const char                   *buf,
215  size_t                        len
216)
217{
218  rtems_termios_tty *tty = legacyContextToTTY (ctx);
219
220  (*tty->device.write) (tty->minor, buf, len);
221}
222
223static bool
224rtems_termios_callback_setAttributes(
225  rtems_termios_device_context *ctx,
226  const struct termios         *term
227)
228{
229  rtems_termios_tty *tty = legacyContextToTTY (ctx);
230
231  (*tty->device.setAttributes) (tty->minor, term);
232
233  return true;
234}
235
236static void
237rtems_termios_callback_stopRemoteTx (rtems_termios_device_context *ctx)
238{
239  rtems_termios_tty *tty = legacyContextToTTY (ctx);
240
241  (*tty->device.stopRemoteTx) (tty->minor);
242}
243
244static void
245rtems_termios_callback_startRemoteTx (rtems_termios_device_context *ctx)
246{
247  rtems_termios_tty *tty = legacyContextToTTY (ctx);
248
249  (*tty->device.startRemoteTx) (tty->minor);
250}
251
252/*
253 * Drain output queue
254 */
255static void
256drainOutput (struct rtems_termios_tty *tty)
257{
258  rtems_termios_device_context *ctx = tty->device_context;
259  rtems_interrupt_lock_context lock_context;
260  rtems_status_code sc;
261
262  if (tty->handler.mode != TERMIOS_POLLED) {
263    rtems_termios_device_lock_acquire (ctx, &lock_context);
264    while (tty->rawOutBuf.Tail != tty->rawOutBuf.Head) {
265      tty->rawOutBufState = rob_wait;
266      rtems_termios_device_lock_release (ctx, &lock_context);
267      sc = rtems_semaphore_obtain(
268        tty->rawOutBuf.Semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
269      if (sc != RTEMS_SUCCESSFUL)
270        rtems_fatal_error_occurred (sc);
271      rtems_termios_device_lock_acquire (ctx, &lock_context);
272    }
273    rtems_termios_device_lock_release (ctx, &lock_context);
274  }
275}
276
277static bool
278needDeviceMutex (rtems_termios_tty *tty)
279{
280  return tty->handler.mode == TERMIOS_IRQ_SERVER_DRIVEN
281    || tty->handler.mode == TERMIOS_TASK_DRIVEN;
282}
283
284static void
285rtems_termios_destroy_tty (rtems_termios_tty *tty, void *arg, bool last_close)
286{
287  rtems_status_code sc;
288
289  if (rtems_termios_linesw[tty->t_line].l_close != NULL) {
290    /*
291     * call discipline-specific close
292     */
293    (void) rtems_termios_linesw[tty->t_line].l_close(tty);
294  } else if (last_close) {
295    /*
296     * default: just flush output buffer
297     */
298    sc = rtems_semaphore_obtain(tty->osem, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
299    if (sc != RTEMS_SUCCESSFUL) {
300      rtems_fatal_error_occurred (sc);
301    }
302    drainOutput (tty);
303    rtems_semaphore_release (tty->osem);
304  }
305
306  if (tty->handler.mode == TERMIOS_TASK_DRIVEN) {
307    /*
308     * send "terminate" to I/O tasks
309     */
310    sc = rtems_event_send( tty->rxTaskId, TERMIOS_RX_TERMINATE_EVENT );
311    if (sc != RTEMS_SUCCESSFUL)
312      rtems_fatal_error_occurred (sc);
313    sc = rtems_event_send( tty->txTaskId, TERMIOS_TX_TERMINATE_EVENT );
314    if (sc != RTEMS_SUCCESSFUL)
315      rtems_fatal_error_occurred (sc);
316  }
317  if (last_close && tty->handler.last_close)
318     (*tty->handler.last_close)(tty, tty->device_context, arg);
319
320  if (tty->device_node != NULL)
321    tty->device_node->tty = NULL;
322
323  rtems_semaphore_delete (tty->isem);
324  rtems_semaphore_delete (tty->osem);
325  rtems_semaphore_delete (tty->rawOutBuf.Semaphore);
326  if ((tty->handler.poll_read == NULL) ||
327      (tty->handler.mode == TERMIOS_TASK_DRIVEN))
328    rtems_semaphore_delete (tty->rawInBuf.Semaphore);
329
330  if (needDeviceMutex (tty)) {
331    rtems_semaphore_delete (tty->device_context->lock.mutex);
332  } else if (tty->device_context == &tty->legacy_device_context) {
333    rtems_interrupt_lock_destroy (&tty->legacy_device_context.lock.interrupt);
334  }
335
336  free (tty->rawInBuf.theBuf);
337  free (tty->rawOutBuf.theBuf);
338  free (tty->cbuf);
339  free (tty);
340}
341
342static void
343deviceAcquireMutex(
344  rtems_termios_device_context *ctx,
345  rtems_interrupt_lock_context *lock_context
346)
347{
348  rtems_status_code sc;
349
350  sc = rtems_semaphore_obtain (ctx->lock.mutex, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
351  _Assert (sc == RTEMS_SUCCESSFUL);
352  (void) sc;
353}
354
355static void
356deviceReleaseMutex(
357  rtems_termios_device_context *ctx,
358  rtems_interrupt_lock_context *lock_context
359)
360{
361  rtems_status_code sc;
362
363  sc = rtems_semaphore_release (ctx->lock.mutex);
364  _Assert (sc == RTEMS_SUCCESSFUL);
365  (void) sc;
366}
367
368void
369rtems_termios_device_lock_acquire_default(
370  rtems_termios_device_context *ctx,
371  rtems_interrupt_lock_context *lock_context
372)
373{
374  rtems_interrupt_lock_acquire (&ctx->lock.interrupt, lock_context);
375}
376
377void
378rtems_termios_device_lock_release_default(
379  rtems_termios_device_context *ctx,
380  rtems_interrupt_lock_context *lock_context
381)
382{
383  rtems_interrupt_lock_release (&ctx->lock.interrupt, lock_context);
384}
385
386static rtems_termios_tty *
387rtems_termios_open_tty(
388  rtems_device_major_number      major,
389  rtems_device_minor_number      minor,
390  rtems_libio_open_close_args_t *args,
391  rtems_termios_tty             *tty,
392  rtems_termios_device_node     *device_node,
393  const rtems_termios_callbacks *callbacks
394)
395{
396  rtems_status_code sc;
397
398  if (tty == NULL) {
399    static char c = 'a';
400    rtems_termios_device_context *ctx;
401
402    /*
403     * Create a new device
404     */
405    tty = calloc (1, sizeof (struct rtems_termios_tty));
406    if (tty == NULL) {
407      return NULL;
408    }
409    /*
410     * allocate raw input buffer
411     */
412    tty->rawInBuf.Size = RAW_INPUT_BUFFER_SIZE;
413    tty->rawInBuf.theBuf = malloc (tty->rawInBuf.Size);
414    if (tty->rawInBuf.theBuf == NULL) {
415            free(tty);
416      return NULL;
417    }
418    /*
419     * allocate raw output buffer
420     */
421    tty->rawOutBuf.Size = RAW_OUTPUT_BUFFER_SIZE;
422    tty->rawOutBuf.theBuf = malloc (tty->rawOutBuf.Size);
423    if (tty->rawOutBuf.theBuf == NULL) {
424            free((void *)(tty->rawInBuf.theBuf));
425            free(tty);
426      return NULL;
427    }
428    /*
429     * allocate cooked buffer
430     */
431    tty->cbuf  = malloc (CBUFSIZE);
432    if (tty->cbuf == NULL) {
433            free((void *)(tty->rawOutBuf.theBuf));
434            free((void *)(tty->rawInBuf.theBuf));
435            free(tty);
436      return NULL;
437    }
438    /*
439     * Initialize wakeup callbacks
440     */
441    tty->tty_snd.sw_pfn = NULL;
442    tty->tty_snd.sw_arg = NULL;
443    tty->tty_rcv.sw_pfn = NULL;
444    tty->tty_rcv.sw_arg = NULL;
445    tty->tty_rcvwakeup  = false;
446
447    tty->minor = minor;
448    tty->major = major;
449
450    /*
451     * Set up mutex semaphores
452     */
453    sc = rtems_semaphore_create (
454      rtems_build_name ('T', 'R', 'i', c),
455      1,
456      RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY,
457      RTEMS_NO_PRIORITY,
458      &tty->isem);
459    if (sc != RTEMS_SUCCESSFUL)
460      rtems_fatal_error_occurred (sc);
461    sc = rtems_semaphore_create (
462      rtems_build_name ('T', 'R', 'o', c),
463      1,
464      RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY,
465      RTEMS_NO_PRIORITY,
466      &tty->osem);
467    if (sc != RTEMS_SUCCESSFUL)
468      rtems_fatal_error_occurred (sc);
469    sc = rtems_semaphore_create (
470      rtems_build_name ('T', 'R', 'x', c),
471      0,
472      RTEMS_SIMPLE_BINARY_SEMAPHORE | RTEMS_FIFO,
473      RTEMS_NO_PRIORITY,
474      &tty->rawOutBuf.Semaphore);
475    if (sc != RTEMS_SUCCESSFUL)
476      rtems_fatal_error_occurred (sc);
477    tty->rawOutBufState = rob_idle;
478
479    /*
480     * Set callbacks
481     */
482    if (device_node != NULL) {
483      device_node->tty = tty;
484      tty->handler = *device_node->handler;
485
486      if (device_node->flow != NULL) {
487        tty->flow = *device_node->flow;
488      }
489
490      tty->device_node = device_node;
491      tty->device_context = device_node->context;
492      memset(&tty->device, 0, sizeof(tty->device));
493    } else {
494      tty->handler.first_open = callbacks->firstOpen != NULL ?
495        rtems_termios_callback_firstOpen : NULL;
496      tty->handler.last_close = callbacks->lastClose != NULL ?
497        rtems_termios_callback_lastClose : NULL;
498      tty->handler.poll_read = callbacks->pollRead != NULL ?
499        rtems_termios_callback_pollRead : NULL;
500      tty->handler.write = callbacks->write != NULL ?
501        rtems_termios_callback_write : NULL;
502      tty->handler.set_attributes = callbacks->setAttributes != NULL ?
503        rtems_termios_callback_setAttributes : NULL;
504      tty->flow.stop_remote_tx = callbacks->stopRemoteTx != NULL ?
505        rtems_termios_callback_stopRemoteTx : NULL;
506      tty->flow.start_remote_tx = callbacks->startRemoteTx != NULL ?
507        rtems_termios_callback_startRemoteTx : NULL;
508      tty->handler.mode = callbacks->outputUsesInterrupts;
509      tty->device_context = NULL;
510      tty->device_node = NULL;
511      tty->device = *callbacks;
512    }
513
514    if (tty->device_context == NULL) {
515      tty->device_context = &tty->legacy_device_context;
516      rtems_termios_device_context_initialize (tty->device_context, "Termios");
517    }
518
519    ctx = tty->device_context;
520
521    if (needDeviceMutex (tty)) {
522      sc = rtems_semaphore_create (
523        rtems_build_name ('T', 'l', 'k', c),
524        1,
525        RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY,
526        0,
527        &ctx->lock.mutex);
528      if (sc != RTEMS_SUCCESSFUL) {
529        rtems_fatal_error_occurred (sc);
530      }
531
532      ctx->lock_acquire = deviceAcquireMutex;
533      ctx->lock_release = deviceReleaseMutex;
534    } else {
535      ctx->lock_acquire = rtems_termios_device_lock_acquire_default;
536      ctx->lock_release = rtems_termios_device_lock_release_default;
537    }
538
539    /*
540     * Create I/O tasks
541     */
542    if (tty->handler.mode == TERMIOS_TASK_DRIVEN) {
543      sc = rtems_task_create (
544                                   rtems_build_name ('T', 'x', 'T', c),
545           TERMIOS_TXTASK_PRIO,
546           TERMIOS_TXTASK_STACKSIZE,
547           RTEMS_NO_PREEMPT | RTEMS_NO_TIMESLICE |
548           RTEMS_NO_ASR,
549           RTEMS_NO_FLOATING_POINT | RTEMS_LOCAL,
550           &tty->txTaskId);
551      if (sc != RTEMS_SUCCESSFUL)
552        rtems_fatal_error_occurred (sc);
553      sc = rtems_task_create (
554                                   rtems_build_name ('R', 'x', 'T', c),
555           TERMIOS_RXTASK_PRIO,
556           TERMIOS_RXTASK_STACKSIZE,
557           RTEMS_NO_PREEMPT | RTEMS_NO_TIMESLICE |
558           RTEMS_NO_ASR,
559           RTEMS_NO_FLOATING_POINT | RTEMS_LOCAL,
560           &tty->rxTaskId);
561      if (sc != RTEMS_SUCCESSFUL)
562        rtems_fatal_error_occurred (sc);
563
564    }
565    if ((tty->handler.poll_read == NULL) ||
566        (tty->handler.mode == TERMIOS_TASK_DRIVEN)){
567      sc = rtems_semaphore_create (
568        rtems_build_name ('T', 'R', 'r', c),
569        0,
570        RTEMS_SIMPLE_BINARY_SEMAPHORE | RTEMS_PRIORITY,
571        RTEMS_NO_PRIORITY,
572        &tty->rawInBuf.Semaphore);
573      if (sc != RTEMS_SUCCESSFUL)
574        rtems_fatal_error_occurred (sc);
575    }
576
577    /*
578     * Set default parameters
579     */
580    tty->termios.c_iflag = BRKINT | ICRNL | IXON | IMAXBEL;
581    tty->termios.c_oflag = OPOST | ONLCR | OXTABS;
582    tty->termios.c_cflag = CS8 | CREAD | CLOCAL;
583    tty->termios.c_lflag =
584       ISIG | ICANON | IEXTEN | ECHO | ECHOK | ECHOE | ECHOCTL;
585
586    tty->termios.c_ispeed = B9600;
587    tty->termios.c_ospeed = B9600;
588
589    tty->termios.c_cc[VINTR] = '\003';
590    tty->termios.c_cc[VQUIT] = '\034';
591    tty->termios.c_cc[VERASE] = '\177';
592    tty->termios.c_cc[VKILL] = '\025';
593    tty->termios.c_cc[VEOF] = '\004';
594    tty->termios.c_cc[VEOL] = '\000';
595    tty->termios.c_cc[VEOL2] = '\000';
596    tty->termios.c_cc[VSTART] = '\021';
597    tty->termios.c_cc[VSTOP] = '\023';
598    tty->termios.c_cc[VSUSP] = '\032';
599    tty->termios.c_cc[VREPRINT] = '\022';
600    tty->termios.c_cc[VDISCARD] = '\017';
601    tty->termios.c_cc[VWERASE] = '\027';
602    tty->termios.c_cc[VLNEXT] = '\026';
603
604    /* start with no flow control, clear flow control flags */
605    tty->flow_ctrl = 0;
606    /*
607     * set low/highwater mark for XON/XOFF support
608     */
609    tty->lowwater  = tty->rawInBuf.Size * 1/2;
610    tty->highwater = tty->rawInBuf.Size * 3/4;
611    /*
612     * Bump name characer
613     */
614    if (c++ == 'z')
615      c = 'a';
616
617  }
618  args->iop->data1 = tty;
619  if (!tty->refcount++) {
620    if (tty->handler.first_open && !(*tty->handler.first_open)(
621        tty, tty->device_context, &tty->termios, args)) {
622      rtems_termios_destroy_tty(tty, args, false);
623      return NULL;
624    }
625
626    /*
627     * start I/O tasks, if needed
628     */
629    if (tty->handler.mode == TERMIOS_TASK_DRIVEN) {
630      sc = rtems_task_start(
631        tty->rxTaskId, rtems_termios_rxdaemon, (rtems_task_argument)tty);
632      if (sc != RTEMS_SUCCESSFUL)
633        rtems_fatal_error_occurred (sc);
634
635      sc = rtems_task_start(
636        tty->txTaskId, rtems_termios_txdaemon, (rtems_task_argument)tty);
637      if (sc != RTEMS_SUCCESSFUL)
638        rtems_fatal_error_occurred (sc);
639    }
640  }
641
642  return tty;
643}
644
645/*
646 * Open a termios device
647 */
648rtems_status_code
649rtems_termios_open (
650  rtems_device_major_number      major,
651  rtems_device_minor_number      minor,
652  void                          *arg,
653  const rtems_termios_callbacks *callbacks
654)
655{
656  rtems_status_code sc;
657  struct rtems_termios_tty *tty;
658
659  /*
660   * See if the device has already been opened
661   */
662  sc = rtems_termios_obtain ();
663  if (sc != RTEMS_SUCCESSFUL)
664    return sc;
665
666  for (tty = rtems_termios_ttyHead ; tty != NULL ; tty = tty->forw) {
667    if ((tty->major == major) && (tty->minor == minor))
668      break;
669  }
670
671  tty = rtems_termios_open_tty(
672    major, minor, arg, tty, NULL, callbacks);
673  if (tty == NULL) {
674    rtems_termios_release ();
675    return RTEMS_NO_MEMORY;
676  }
677
678  if (tty->refcount == 1) {
679    /*
680     * link tty
681     */
682    tty->forw = rtems_termios_ttyHead;
683    tty->back = NULL;
684    if (rtems_termios_ttyHead != NULL)
685      rtems_termios_ttyHead->back = tty;
686    rtems_termios_ttyHead = tty;
687    if (rtems_termios_ttyTail == NULL)
688      rtems_termios_ttyTail = tty;
689  }
690
691  rtems_termios_release ();
692
693  return RTEMS_SUCCESSFUL;
694}
695
696static void
697flushOutput (struct rtems_termios_tty *tty)
698{
699  rtems_termios_device_context *ctx = tty->device_context;
700  rtems_interrupt_lock_context lock_context;
701
702  rtems_termios_device_lock_acquire (ctx, &lock_context);
703  tty->rawOutBuf.Tail = 0;
704  tty->rawOutBuf.Head = 0;
705  tty->rawOutBufState = rob_idle;
706  rtems_termios_device_lock_release (ctx, &lock_context);
707}
708
709static void
710flushInput (struct rtems_termios_tty *tty)
711{
712  rtems_termios_device_context *ctx = tty->device_context;
713  rtems_interrupt_lock_context lock_context;
714
715  rtems_termios_device_lock_acquire (ctx, &lock_context);
716  tty->rawInBuf.Tail = 0;
717  tty->rawInBuf.Head = 0;
718  rtems_termios_device_lock_release (ctx, &lock_context);
719}
720
721static void
722rtems_termios_close_tty (rtems_termios_tty *tty, void *arg)
723{
724  if (--tty->refcount == 0) {
725    rtems_termios_destroy_tty (tty, arg, true);
726  }
727}
728
729rtems_status_code
730rtems_termios_close (void *arg)
731{
732  rtems_status_code sc;
733  rtems_libio_open_close_args_t *args = arg;
734  struct rtems_termios_tty *tty = args->iop->data1;
735
736  sc = rtems_termios_obtain ();
737  if (sc != RTEMS_SUCCESSFUL)
738    rtems_fatal_error_occurred (sc);
739
740  if (tty->refcount == 1) {
741    if (tty->forw == NULL) {
742      rtems_termios_ttyTail = tty->back;
743      if ( rtems_termios_ttyTail != NULL ) {
744        rtems_termios_ttyTail->forw = NULL;
745      }
746    } else {
747      tty->forw->back = tty->back;
748    }
749
750    if (tty->back == NULL) {
751      rtems_termios_ttyHead = tty->forw;
752      if ( rtems_termios_ttyHead != NULL ) {
753        rtems_termios_ttyHead->back = NULL;
754      }
755    } else {
756      tty->back->forw = tty->forw;
757    }
758  }
759
760  rtems_termios_close_tty (tty, arg);
761
762  rtems_termios_release ();
763
764  return RTEMS_SUCCESSFUL;
765}
766
767rtems_status_code rtems_termios_bufsize (
768  size_t cbufsize,
769  size_t raw_input,
770  size_t raw_output
771)
772{
773  rtems_termios_cbufsize        = cbufsize;
774  rtems_termios_raw_input_size  = raw_input;
775  rtems_termios_raw_output_size = raw_output;
776  return RTEMS_SUCCESSFUL;
777}
778
779static void
780termios_set_flowctrl(struct rtems_termios_tty *tty)
781{
782  rtems_termios_device_context *ctx = tty->device_context;
783  rtems_interrupt_lock_context lock_context;
784  /*
785   * check for flow control options to be switched off
786   */
787
788  /* check for outgoing XON/XOFF flow control switched off */
789  if (( tty->flow_ctrl & FL_MDXON) &&
790      !(tty->termios.c_iflag & IXON)) {
791    /* clear related flags in flow_ctrl */
792    tty->flow_ctrl &= ~(FL_MDXON | FL_ORCVXOF);
793
794    /* has output been stopped due to received XOFF? */
795    if (tty->flow_ctrl & FL_OSTOP) {
796      /* disable interrupts    */
797      rtems_termios_device_lock_acquire (ctx, &lock_context);
798      tty->flow_ctrl &= ~FL_OSTOP;
799      /* check for chars in output buffer (or rob_state?) */
800      if (tty->rawOutBufState != rob_idle) {
801        /* if chars available, call write function... */
802        (*tty->handler.write)(
803          ctx, &tty->rawOutBuf.theBuf[tty->rawOutBuf.Tail],1);
804      }
805      /* reenable interrupts */
806      rtems_termios_device_lock_release (ctx, &lock_context);
807    }
808  }
809  /* check for incoming XON/XOFF flow control switched off */
810  if (( tty->flow_ctrl & FL_MDXOF) && !(tty->termios.c_iflag & IXOFF)) {
811    /* clear related flags in flow_ctrl */
812    tty->flow_ctrl &= ~(FL_MDXOF);
813    /* FIXME: what happens, if we had sent XOFF but not yet XON? */
814    tty->flow_ctrl &= ~(FL_ISNTXOF);
815  }
816
817  /* check for incoming RTS/CTS flow control switched off */
818  if (( tty->flow_ctrl & FL_MDRTS) && !(tty->termios.c_cflag & CRTSCTS)) {
819    /* clear related flags in flow_ctrl */
820    tty->flow_ctrl &= ~(FL_MDRTS);
821
822    /* restart remote Tx, if it was stopped */
823    if ((tty->flow_ctrl & FL_IRTSOFF) &&
824        (tty->flow.start_remote_tx != NULL)) {
825      tty->flow.start_remote_tx(ctx);
826    }
827    tty->flow_ctrl &= ~(FL_IRTSOFF);
828  }
829
830  /*
831   * check for flow control options to be switched on
832   */
833  /* check for incoming RTS/CTS flow control switched on */
834  if (tty->termios.c_cflag & CRTSCTS) {
835    tty->flow_ctrl |= FL_MDRTS;
836  }
837  /* check for incoming XON/XOF flow control switched on */
838  if (tty->termios.c_iflag & IXOFF) {
839    tty->flow_ctrl |= FL_MDXOF;
840  }
841  /* check for outgoing XON/XOF flow control switched on */
842  if (tty->termios.c_iflag & IXON) {
843    tty->flow_ctrl |= FL_MDXON;
844  }
845}
846
847rtems_status_code
848rtems_termios_ioctl (void *arg)
849{
850  rtems_libio_ioctl_args_t *args = arg;
851  struct rtems_termios_tty *tty = args->iop->data1;
852  struct ttywakeup         *wakeup = (struct ttywakeup *)args->buffer;
853  rtems_status_code sc;
854  int flags;
855
856  args->ioctl_return = 0;
857  sc = rtems_semaphore_obtain (tty->osem, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
858  if (sc != RTEMS_SUCCESSFUL) {
859    return sc;
860  }
861  switch (args->command) {
862  default:
863    if (rtems_termios_linesw[tty->t_line].l_ioctl != NULL) {
864      sc = rtems_termios_linesw[tty->t_line].l_ioctl(tty,args);
865    } else if (tty->handler.ioctl) {
866      args->ioctl_return = (*tty->handler.ioctl) (tty->device_context,
867        args->command, args->buffer);
868      sc = RTEMS_SUCCESSFUL;
869    } else {
870      sc = RTEMS_INVALID_NUMBER;
871    }
872    break;
873
874  case TIOCGETA:
875    *(struct termios *)args->buffer = tty->termios;
876    break;
877
878  case TIOCSETA:
879  case TIOCSETAW:
880  case TIOCSETAF:
881    tty->termios = *(struct termios *)args->buffer;
882
883    if (args->command == TIOCSETAW || args->command == TIOCSETAF) {
884      drainOutput (tty);
885      if (args->command == TIOCSETAF) {
886        flushInput (tty);
887      }
888    }
889    /* check for and process change in flow control options */
890    termios_set_flowctrl(tty);
891
892    if (tty->termios.c_lflag & ICANON) {
893      tty->rawInBufSemaphoreOptions = RTEMS_WAIT;
894      tty->rawInBufSemaphoreTimeout = RTEMS_NO_TIMEOUT;
895      tty->rawInBufSemaphoreFirstTimeout = RTEMS_NO_TIMEOUT;
896    } else {
897      tty->vtimeTicks = tty->termios.c_cc[VTIME] *
898                    rtems_clock_get_ticks_per_second() / 10;
899      if (tty->termios.c_cc[VTIME]) {
900        tty->rawInBufSemaphoreOptions = RTEMS_WAIT;
901        tty->rawInBufSemaphoreTimeout = tty->vtimeTicks;
902        if (tty->termios.c_cc[VMIN])
903          tty->rawInBufSemaphoreFirstTimeout = RTEMS_NO_TIMEOUT;
904        else
905          tty->rawInBufSemaphoreFirstTimeout = tty->vtimeTicks;
906      } else {
907        if (tty->termios.c_cc[VMIN]) {
908          tty->rawInBufSemaphoreOptions = RTEMS_WAIT;
909          tty->rawInBufSemaphoreTimeout = RTEMS_NO_TIMEOUT;
910          tty->rawInBufSemaphoreFirstTimeout = RTEMS_NO_TIMEOUT;
911        } else {
912          tty->rawInBufSemaphoreOptions = RTEMS_NO_WAIT;
913        }
914      }
915    }
916    if (tty->handler.set_attributes) {
917      sc = (*tty->handler.set_attributes)(tty->device_context, &tty->termios) ?
918        RTEMS_SUCCESSFUL : RTEMS_IO_ERROR;
919    }
920    break;
921
922  case TIOCDRAIN:
923    drainOutput (tty);
924    break;
925
926  case TIOCFLUSH:
927    flags = *((int *)args->buffer);
928
929    if (flags == 0) {
930      flags = FREAD | FWRITE;
931    } else {
932      flags &= FREAD | FWRITE;
933    }
934    if (flags & FWRITE) {
935      flushOutput (tty);
936    }
937    if (flags & FREAD) {
938      flushInput (tty);
939    }
940    break;
941
942  case RTEMS_IO_SNDWAKEUP:
943    tty->tty_snd = *wakeup;
944    break;
945
946  case RTEMS_IO_RCVWAKEUP:
947    tty->tty_rcv = *wakeup;
948    break;
949
950    /*
951     * FIXME: add various ioctl code handlers
952     */
953
954#if 1 /* FIXME */
955  case TIOCSETD:
956    /*
957     * close old line discipline
958     */
959    if (rtems_termios_linesw[tty->t_line].l_close != NULL) {
960      sc = rtems_termios_linesw[tty->t_line].l_close(tty);
961    }
962    tty->t_line=*(int*)(args->buffer);
963    tty->t_sc = NULL; /* ensure that no more valid data */
964    /*
965     * open new line discipline
966     */
967    if (rtems_termios_linesw[tty->t_line].l_open != NULL) {
968      sc = rtems_termios_linesw[tty->t_line].l_open(tty);
969    }
970    break;
971  case TIOCGETD:
972    *(int*)(args->buffer)=tty->t_line;
973    break;
974#endif
975   case FIONREAD: {
976      int rawnc = tty->rawInBuf.Tail - tty->rawInBuf.Head;
977      if ( rawnc < 0 )
978        rawnc += tty->rawInBuf.Size;
979      /* Half guess that this is the right operation */
980      *(int *)args->buffer = tty->ccount - tty->cindex + rawnc;
981    }
982    break;
983  }
984
985  rtems_semaphore_release (tty->osem);
986  return sc;
987}
988
989/*
990 * Send as many chars at once as possible to device-specific code.
991 * If transmitting==true then assume transmission is already running and
992 * an explicit write(0) is needed if output has to stop for flow control.
993 */
994static unsigned int
995startXmit (
996  struct rtems_termios_tty *tty,
997  unsigned int newTail,
998  bool transmitting
999)
1000{
1001  unsigned int nToSend;
1002
1003  tty->rawOutBufState = rob_busy;
1004
1005  /* if XOFF was received, do not (re)start output */
1006  if (tty->flow_ctrl & FL_ORCVXOF) {
1007    /* set flag, that output has been stopped */
1008    tty->flow_ctrl |= FL_OSTOP;
1009    nToSend = 0;
1010    /* stop transmitter */
1011    if (transmitting) {
1012      (*tty->handler.write) (tty->device_context, NULL, 0);
1013    }
1014  } else {
1015    /* when flow control XON or XOF, don't send blocks of data     */
1016    /* to allow fast reaction on incoming flow ctrl and low latency*/
1017    /* for outgoing flow control                                   */
1018    if (tty->flow_ctrl & (FL_MDXON | FL_MDXOF))
1019      nToSend = 1;
1020    else if (newTail > tty->rawOutBuf.Head)
1021      nToSend = tty->rawOutBuf.Size - newTail;
1022    else
1023      nToSend = tty->rawOutBuf.Head - newTail;
1024
1025    (*tty->handler.write)(
1026        tty->device_context, &tty->rawOutBuf.theBuf[newTail], nToSend);
1027  }
1028
1029  return nToSend;
1030}
1031
1032/*
1033 * Send characters to device-specific code
1034 */
1035static size_t
1036doTransmit (const char *buf, size_t len, rtems_termios_tty *tty,
1037            bool wait, bool nextWait)
1038{
1039  unsigned int newHead;
1040  rtems_termios_device_context *ctx = tty->device_context;
1041  rtems_interrupt_lock_context lock_context;
1042  rtems_status_code sc;
1043  size_t todo;
1044
1045  if (tty->handler.mode == TERMIOS_POLLED) {
1046    (*tty->handler.write)(ctx, buf, len);
1047    return len;
1048  }
1049
1050  todo = len;
1051
1052  while (todo > 0) {
1053    size_t nToCopy;
1054    size_t nAvail;
1055
1056    /* Check space for at least one char */
1057    newHead = tty->rawOutBuf.Head + 1;
1058    if (newHead >= tty->rawOutBuf.Size)
1059      newHead -= tty->rawOutBuf.Size;
1060
1061    rtems_termios_device_lock_acquire (ctx, &lock_context);
1062    if (newHead == tty->rawOutBuf.Tail) {
1063      if (wait) {
1064        do {
1065          tty->rawOutBufState = rob_wait;
1066          rtems_termios_device_lock_release (ctx, &lock_context);
1067          sc = rtems_semaphore_obtain(
1068            tty->rawOutBuf.Semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
1069          if (sc != RTEMS_SUCCESSFUL)
1070            rtems_fatal_error_occurred (sc);
1071          rtems_termios_device_lock_acquire (ctx, &lock_context);
1072        } while (newHead == tty->rawOutBuf.Tail);
1073      } else {
1074        rtems_termios_device_lock_release (ctx, &lock_context);
1075        return len - todo;
1076      }
1077    }
1078
1079    /* Determine free space up to current tail or end of ring buffer */
1080    nToCopy = todo;
1081    if (tty->rawOutBuf.Tail > tty->rawOutBuf.Head) {
1082      /* Available space is contiguous from Head to Tail */
1083      nAvail = tty->rawOutBuf.Tail - tty->rawOutBuf.Head - 1;
1084    } else {
1085      /* Available space wraps at buffer end. To keep it simple, utilize
1086         only the free space from Head to end during this iteration */
1087      nAvail = tty->rawOutBuf.Size - tty->rawOutBuf.Head;
1088      /* Head may not touch Tail after wraparound */
1089      if (tty->rawOutBuf.Tail == 0)
1090        nAvail--;
1091    }
1092    if (nToCopy > nAvail)
1093      nToCopy = nAvail;
1094
1095    /* To minimize latency, the memcpy could be done
1096     * with interrupts enabled or with limit on nToCopy (TBD)
1097     */
1098    memcpy(&tty->rawOutBuf.theBuf[tty->rawOutBuf.Head], buf, nToCopy);
1099
1100    newHead = tty->rawOutBuf.Head + nToCopy;
1101    if (newHead >= tty->rawOutBuf.Size)
1102      newHead -= tty->rawOutBuf.Size;
1103    tty->rawOutBuf.Head = newHead;
1104
1105    if (tty->rawOutBufState == rob_idle) {
1106      startXmit (tty, tty->rawOutBuf.Tail, false);
1107    }
1108
1109    rtems_termios_device_lock_release (ctx, &lock_context);
1110
1111    buf += nToCopy;
1112    todo -= nToCopy;
1113    wait = nextWait;
1114  }
1115
1116  return len;
1117}
1118
1119void
1120rtems_termios_puts (
1121  const void *_buf, size_t len, struct rtems_termios_tty *tty)
1122{
1123  doTransmit (_buf, len, tty, true, true);
1124}
1125
1126static bool
1127canTransmit (rtems_termios_tty *tty, bool wait, size_t len)
1128{
1129  rtems_termios_device_context *ctx;
1130  rtems_interrupt_lock_context lock_context;
1131  unsigned int capacity;
1132
1133  if (wait || tty->handler.mode == TERMIOS_POLLED) {
1134    return true;
1135  }
1136
1137  ctx = tty->device_context;
1138  rtems_termios_device_lock_acquire (ctx, &lock_context);
1139  capacity = (tty->rawOutBuf.Tail - tty->rawOutBuf.Head - 1) %
1140    tty->rawOutBuf.Size;
1141  rtems_termios_device_lock_release (ctx, &lock_context);
1142  return capacity >= len;
1143}
1144
1145/*
1146 * Handle output processing
1147 */
1148static bool
1149oproc (unsigned char c, rtems_termios_tty *tty, bool wait)
1150{
1151  char buf[8];
1152  size_t len;
1153
1154  buf[0] = c;
1155  len = 1;
1156
1157  if (tty->termios.c_oflag & OPOST) {
1158    int oldColumn = tty->column;
1159    int columnAdj = 0;
1160
1161    switch (c) {
1162    case '\n':
1163      if (tty->termios.c_oflag & ONLRET)
1164        columnAdj = -oldColumn;
1165      if (tty->termios.c_oflag & ONLCR) {
1166        len = 2;
1167
1168        if (!canTransmit (tty, wait, len)) {
1169          return false;
1170        }
1171
1172        columnAdj = -oldColumn;
1173        buf[0] = '\r';
1174        buf[1] = c;
1175      }
1176      break;
1177
1178    case '\r':
1179      if ((tty->termios.c_oflag & ONOCR) && (oldColumn == 0))
1180        return true;
1181      if (tty->termios.c_oflag & OCRNL) {
1182        buf[0] = '\n';
1183        if (tty->termios.c_oflag & ONLRET)
1184          columnAdj = -oldColumn;
1185      } else {
1186        columnAdj = -oldColumn;
1187      }
1188      break;
1189
1190    case '\t':
1191      columnAdj = 8 - (oldColumn & 7);
1192      if ((tty->termios.c_oflag & TABDLY) == OXTABS) {
1193        int i;
1194
1195        len = (size_t) columnAdj;
1196
1197        if (!canTransmit (tty, wait, len)) {
1198          return false;
1199        }
1200
1201        for (i = 0; i < columnAdj; ++i) {
1202          buf[i] = ' ';
1203        }
1204      }
1205      break;
1206
1207    case '\b':
1208      if (oldColumn > 0)
1209        columnAdj = -1;
1210      break;
1211
1212    default:
1213      if (tty->termios.c_oflag & OLCUC) {
1214        c = toupper(c);
1215        buf[0] = c;
1216      }
1217      if (!iscntrl(c))
1218        columnAdj = 1;
1219      break;
1220    }
1221
1222    tty->column = oldColumn + columnAdj;
1223  }
1224
1225  return doTransmit (buf, len, tty, wait, true) > 0;
1226}
1227
1228static uint32_t
1229rtems_termios_write_tty (rtems_libio_t *iop, rtems_termios_tty *tty,
1230                         const char *buf, uint32_t len)
1231{
1232  bool wait = ((iop->flags & LIBIO_FLAGS_NO_DELAY) == 0);
1233
1234  if (tty->termios.c_oflag & OPOST) {
1235    uint32_t todo = len;
1236
1237    while (todo > 0) {
1238      if (!oproc (*buf, tty, wait)) {
1239        break;
1240      }
1241
1242      ++buf;
1243      --todo;
1244      wait = false;
1245    }
1246
1247    return len - todo;
1248  } else {
1249    return doTransmit (buf, len, tty, wait, false);
1250  }
1251}
1252
1253rtems_status_code
1254rtems_termios_write (void *arg)
1255{
1256  rtems_libio_rw_args_t *args = arg;
1257  struct rtems_termios_tty *tty = args->iop->data1;
1258  rtems_status_code sc;
1259
1260  sc = rtems_semaphore_obtain (tty->osem, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
1261  if (sc != RTEMS_SUCCESSFUL)
1262    return sc;
1263  if (rtems_termios_linesw[tty->t_line].l_write != NULL) {
1264    sc = rtems_termios_linesw[tty->t_line].l_write(tty,args);
1265    rtems_semaphore_release (tty->osem);
1266    return sc;
1267  }
1268  args->bytes_moved = rtems_termios_write_tty (args->iop, tty,
1269                                               args->buffer, args->count);
1270  rtems_semaphore_release (tty->osem);
1271  return sc;
1272}
1273
1274/*
1275 * Echo a typed character
1276 */
1277static void
1278echo (unsigned char c, struct rtems_termios_tty *tty)
1279{
1280  if ((tty->termios.c_lflag & ECHOCTL) &&
1281       iscntrl(c) && (c != '\t') && (c != '\n')) {
1282    char echobuf[2];
1283
1284    echobuf[0] = '^';
1285    echobuf[1] = c ^ 0x40;
1286    doTransmit (echobuf, 2, tty, true, true);
1287    tty->column += 2;
1288  } else {
1289    oproc (c, tty, true);
1290  }
1291}
1292
1293/*
1294 * Erase a character or line
1295 * FIXME: Needs support for WERASE and ECHOPRT.
1296 * FIXME: Some of the tests should check for IEXTEN, too.
1297 */
1298static void
1299erase (struct rtems_termios_tty *tty, int lineFlag)
1300{
1301  if (tty->ccount == 0)
1302    return;
1303  if (lineFlag) {
1304    if (!(tty->termios.c_lflag & ECHO)) {
1305      tty->ccount = 0;
1306      return;
1307    }
1308    if (!(tty->termios.c_lflag & ECHOE)) {
1309      tty->ccount = 0;
1310      echo (tty->termios.c_cc[VKILL], tty);
1311      if (tty->termios.c_lflag & ECHOK)
1312        echo ('\n', tty);
1313      return;
1314    }
1315  }
1316
1317  while (tty->ccount) {
1318    unsigned char c = tty->cbuf[--tty->ccount];
1319
1320    if (tty->termios.c_lflag & ECHO) {
1321      if (!lineFlag && !(tty->termios.c_lflag & ECHOE)) {
1322        echo (tty->termios.c_cc[VERASE], tty);
1323      } else if (c == '\t') {
1324        int col = tty->read_start_column;
1325        int i = 0;
1326
1327        /*
1328         * Find the character before the tab
1329         */
1330        while (i != tty->ccount) {
1331          c = tty->cbuf[i++];
1332          if (c == '\t') {
1333            col = (col | 7) + 1;
1334          } else if (iscntrl (c)) {
1335            if (tty->termios.c_lflag & ECHOCTL)
1336              col += 2;
1337          } else {
1338            col++;
1339          }
1340        }
1341
1342        /*
1343         * Back up over the tab
1344         */
1345        while (tty->column > col) {
1346          doTransmit ("\b", 1, tty, true, true);
1347          tty->column--;
1348        }
1349      }
1350      else {
1351        if (iscntrl (c) && (tty->termios.c_lflag & ECHOCTL)) {
1352          doTransmit ("\b \b", 3, tty, true, true);
1353          if (tty->column)
1354            tty->column--;
1355        }
1356        if (!iscntrl (c) || (tty->termios.c_lflag & ECHOCTL)) {
1357          doTransmit ("\b \b", 3, tty, true, true);
1358          if (tty->column)
1359            tty->column--;
1360        }
1361      }
1362    }
1363    if (!lineFlag)
1364      break;
1365  }
1366}
1367
1368static unsigned char
1369iprocEarly (unsigned char c, rtems_termios_tty *tty)
1370{
1371  if (tty->termios.c_iflag & ISTRIP)
1372    c &= 0x7f;
1373
1374  if (tty->termios.c_iflag & IUCLC)
1375    c = tolower (c);
1376
1377  if (c == '\r') {
1378    if (tty->termios.c_iflag & ICRNL)
1379      c = '\n';
1380  } else if (c == '\n') {
1381    if (tty->termios.c_iflag & INLCR)
1382      c = '\r';
1383  }
1384
1385  return c;
1386}
1387
1388/*
1389 * Process a single input character
1390 */
1391static int
1392iproc (unsigned char c, struct rtems_termios_tty *tty)
1393{
1394  if ((c != '\0') && (tty->termios.c_lflag & ICANON)) {
1395    if (c == tty->termios.c_cc[VERASE]) {
1396      erase (tty, 0);
1397      return 0;
1398    }
1399    else if (c == tty->termios.c_cc[VKILL]) {
1400      erase (tty, 1);
1401      return 0;
1402    }
1403    else if (c == tty->termios.c_cc[VEOF]) {
1404      return 1;
1405    } else if (c == '\n') {
1406      if (tty->termios.c_lflag & (ECHO | ECHONL))
1407        echo (c, tty);
1408      tty->cbuf[tty->ccount++] = c;
1409      return 1;
1410    } else if ((c == tty->termios.c_cc[VEOL]) ||
1411               (c == tty->termios.c_cc[VEOL2])) {
1412      if (tty->termios.c_lflag & ECHO)
1413        echo (c, tty);
1414      tty->cbuf[tty->ccount++] = c;
1415      return 1;
1416    }
1417  }
1418
1419  /*
1420   * FIXME: Should do IMAXBEL handling somehow
1421   */
1422  if (tty->ccount < (CBUFSIZE-1)) {
1423    if (tty->termios.c_lflag & ECHO)
1424      echo (c, tty);
1425    tty->cbuf[tty->ccount++] = c;
1426  }
1427  return 0;
1428}
1429
1430/*
1431 * Process input character, with semaphore.
1432 */
1433static int
1434siproc (unsigned char c, struct rtems_termios_tty *tty)
1435{
1436  int i;
1437
1438  /*
1439   * Obtain output semaphore if character will be echoed
1440   */
1441  if (tty->termios.c_lflag & (ECHO|ECHOE|ECHOK|ECHONL|ECHOPRT|ECHOCTL|ECHOKE)) {
1442    rtems_status_code sc;
1443    sc = rtems_semaphore_obtain (tty->osem, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
1444    if (sc != RTEMS_SUCCESSFUL)
1445      rtems_fatal_error_occurred (sc);
1446    i = iproc (c, tty);
1447    sc = rtems_semaphore_release (tty->osem);
1448    if (sc != RTEMS_SUCCESSFUL)
1449      rtems_fatal_error_occurred (sc);
1450  }
1451  else {
1452    i = iproc (c, tty);
1453  }
1454  return i;
1455}
1456
1457static int
1458siprocPoll (unsigned char c, rtems_termios_tty *tty)
1459{
1460  if (c == '\r' && (tty->termios.c_iflag & IGNCR) != 0) {
1461    return 0;
1462  }
1463
1464  c = iprocEarly (c, tty);
1465  return siproc (c, tty);
1466}
1467
1468/*
1469 * Fill the input buffer by polling the device
1470 */
1471static void
1472fillBufferPoll (struct rtems_termios_tty *tty)
1473{
1474  int n;
1475
1476  if (tty->termios.c_lflag & ICANON) {
1477    for (;;) {
1478      n = (*tty->handler.poll_read)(tty->device_context);
1479      if (n < 0) {
1480        rtems_task_wake_after (1);
1481      } else {
1482        if  (siprocPoll (n, tty))
1483          break;
1484      }
1485    }
1486  } else {
1487    rtems_interval then, now;
1488
1489    then = rtems_clock_get_ticks_since_boot();
1490    for (;;) {
1491      n = (*tty->handler.poll_read)(tty->device_context);
1492      if (n < 0) {
1493        if (tty->termios.c_cc[VMIN]) {
1494          if (tty->termios.c_cc[VTIME] && tty->ccount) {
1495            now = rtems_clock_get_ticks_since_boot();
1496            if ((now - then) > tty->vtimeTicks) {
1497              break;
1498            }
1499          }
1500        } else {
1501          if (!tty->termios.c_cc[VTIME])
1502            break;
1503          now = rtems_clock_get_ticks_since_boot();
1504          if ((now - then) > tty->vtimeTicks) {
1505            break;
1506          }
1507        }
1508        rtems_task_wake_after (1);
1509      } else {
1510        siprocPoll (n, tty);
1511        if (tty->ccount >= tty->termios.c_cc[VMIN])
1512          break;
1513        if (tty->termios.c_cc[VMIN] && tty->termios.c_cc[VTIME])
1514          then = rtems_clock_get_ticks_since_boot();
1515      }
1516    }
1517  }
1518}
1519
1520/*
1521 * Fill the input buffer from the raw input queue
1522 */
1523static void
1524fillBufferQueue (struct rtems_termios_tty *tty)
1525{
1526  rtems_termios_device_context *ctx = tty->device_context;
1527  rtems_interval timeout = tty->rawInBufSemaphoreFirstTimeout;
1528  bool wait = true;
1529
1530  while ( wait ) {
1531    rtems_interrupt_lock_context lock_context;
1532
1533    /*
1534     * Process characters read from raw queue
1535     */
1536
1537    rtems_termios_device_lock_acquire (ctx, &lock_context);
1538
1539    while ((tty->rawInBuf.Head != tty->rawInBuf.Tail) &&
1540                       (tty->ccount < (CBUFSIZE-1))) {
1541      unsigned char c;
1542      unsigned int newHead;
1543
1544      newHead = (tty->rawInBuf.Head + 1) % tty->rawInBuf.Size;
1545      c = tty->rawInBuf.theBuf[newHead];
1546      tty->rawInBuf.Head = newHead;
1547
1548      if(((tty->rawInBuf.Tail - newHead) % tty->rawInBuf.Size)
1549         < tty->lowwater) {
1550        tty->flow_ctrl &= ~FL_IREQXOF;
1551        /* if tx stopped and XON should be sent... */
1552        if (((tty->flow_ctrl & (FL_MDXON | FL_ISNTXOF))
1553             ==                (FL_MDXON | FL_ISNTXOF))
1554            && ((tty->rawOutBufState == rob_idle)
1555          || (tty->flow_ctrl & FL_OSTOP))) {
1556          /* XON should be sent now... */
1557          (*tty->handler.write)(
1558            tty->device_context, (void *)&(tty->termios.c_cc[VSTART]), 1);
1559        } else if (tty->flow_ctrl & FL_MDRTS) {
1560          tty->flow_ctrl &= ~FL_IRTSOFF;
1561          /* activate RTS line */
1562          if (tty->flow.start_remote_tx != NULL) {
1563            tty->flow.start_remote_tx(tty->device_context);
1564          }
1565        }
1566      }
1567
1568      rtems_termios_device_lock_release (ctx, &lock_context);
1569
1570      /* continue processing new character */
1571      if (tty->termios.c_lflag & ICANON) {
1572        if (siproc (c, tty))
1573          wait = false;
1574      } else {
1575        siproc (c, tty);
1576        if (tty->ccount >= tty->termios.c_cc[VMIN])
1577          wait = false;
1578      }
1579      timeout = tty->rawInBufSemaphoreTimeout;
1580
1581      rtems_termios_device_lock_acquire (ctx, &lock_context);
1582    }
1583
1584    rtems_termios_device_lock_release (ctx, &lock_context);
1585
1586    /*
1587     * Wait for characters
1588     */
1589    if (wait) {
1590      if (tty->ccount < CBUFSIZE - 1) {
1591        rtems_status_code sc;
1592
1593        sc = rtems_semaphore_obtain(
1594          tty->rawInBuf.Semaphore, tty->rawInBufSemaphoreOptions, timeout);
1595        if (sc != RTEMS_SUCCESSFUL)
1596          break;
1597      } else {
1598        break;
1599      }
1600    }
1601  }
1602}
1603
1604static uint32_t
1605rtems_termios_read_tty (struct rtems_termios_tty *tty, char *buffer,
1606  uint32_t initial_count)
1607{
1608  uint32_t count;
1609
1610  count = initial_count;
1611
1612  if (tty->cindex == tty->ccount) {
1613    tty->cindex = tty->ccount = 0;
1614    tty->read_start_column = tty->column;
1615    if (tty->handler.poll_read != NULL && tty->handler.mode == TERMIOS_POLLED)
1616      fillBufferPoll (tty);
1617    else
1618      fillBufferQueue (tty);
1619  }
1620  while (count && (tty->cindex < tty->ccount)) {
1621    *buffer++ = tty->cbuf[tty->cindex++];
1622    count--;
1623  }
1624  tty->tty_rcvwakeup = false;
1625  return initial_count - count;
1626}
1627
1628rtems_status_code
1629rtems_termios_read (void *arg)
1630{
1631  rtems_libio_rw_args_t *args = arg;
1632  struct rtems_termios_tty *tty = args->iop->data1;
1633  rtems_status_code sc;
1634
1635  sc = rtems_semaphore_obtain (tty->isem, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
1636  if (sc != RTEMS_SUCCESSFUL)
1637    return sc;
1638
1639  if (rtems_termios_linesw[tty->t_line].l_read != NULL) {
1640    sc = rtems_termios_linesw[tty->t_line].l_read(tty,args);
1641    tty->tty_rcvwakeup = false;
1642    rtems_semaphore_release (tty->isem);
1643    return sc;
1644  }
1645
1646  args->bytes_moved = rtems_termios_read_tty (tty, args->buffer, args->count);
1647  rtems_semaphore_release (tty->isem);
1648  return sc;
1649}
1650
1651/*
1652 * signal receive interrupt to rx daemon
1653 * NOTE: This routine runs in the context of the
1654 *       device receive interrupt handler.
1655 */
1656void rtems_termios_rxirq_occured(struct rtems_termios_tty *tty)
1657{
1658  /*
1659   * send event to rx daemon task
1660   */
1661  rtems_event_send(tty->rxTaskId,TERMIOS_RX_PROC_EVENT);
1662}
1663
1664static bool
1665mustCallReceiveCallback (const rtems_termios_tty *tty, unsigned char c,
1666                         unsigned int newTail, unsigned int head)
1667{
1668  if ((tty->termios.c_lflag & ICANON) != 0) {
1669    return c == '\n' || c == tty->termios.c_cc[VEOF] ||
1670      c == tty->termios.c_cc[VEOL] || c == tty->termios.c_cc[VEOL2];
1671  } else {
1672    unsigned int rawContentSize = (newTail - head) % tty->rawInBuf.Size;
1673
1674    return rawContentSize >= tty->termios.c_cc[VMIN];
1675  }
1676}
1677
1678/*
1679 * Place characters on raw queue.
1680 * NOTE: This routine runs in the context of the
1681 *       device receive interrupt handler.
1682 * Returns the number of characters dropped because of overflow.
1683 */
1684int
1685rtems_termios_enqueue_raw_characters (void *ttyp, const char *buf, int len)
1686{
1687  struct rtems_termios_tty *tty = ttyp;
1688  char c;
1689  int dropped = 0;
1690  bool flow_rcv = false; /* true, if flow control char received */
1691  rtems_termios_device_context *ctx = tty->device_context;
1692  rtems_interrupt_lock_context lock_context;
1693
1694  if (rtems_termios_linesw[tty->t_line].l_rint != NULL) {
1695    while (len--) {
1696      c = *buf++;
1697      rtems_termios_linesw[tty->t_line].l_rint(c,tty);
1698    }
1699
1700    /*
1701     * check to see if rcv wakeup callback was set
1702     */
1703    if (tty->tty_rcv.sw_pfn != NULL && !tty->tty_rcvwakeup) {
1704      tty->tty_rcvwakeup = true;
1705      (*tty->tty_rcv.sw_pfn)(&tty->termios, tty->tty_rcv.sw_arg);
1706    }
1707    return 0;
1708  }
1709
1710  while (len--) {
1711    c = *buf++;
1712    /* FIXME: implement IXANY: any character restarts output */
1713    /* if incoming XON/XOFF controls outgoing stream: */
1714    if (tty->flow_ctrl & FL_MDXON) {
1715      /* if received char is V_STOP and V_START (both are equal value) */
1716      if (c == tty->termios.c_cc[VSTOP]) {
1717        if (c == tty->termios.c_cc[VSTART]) {
1718          /* received VSTOP and VSTART==VSTOP? */
1719          /* then toggle "stop output" status  */
1720          tty->flow_ctrl = tty->flow_ctrl ^ FL_ORCVXOF;
1721        }
1722        else {
1723          /* VSTOP received (other code than VSTART) */
1724          /* stop output                             */
1725          tty->flow_ctrl |= FL_ORCVXOF;
1726        }
1727        flow_rcv = true;
1728      }
1729      else if (c == tty->termios.c_cc[VSTART]) {
1730        /* VSTART received */
1731        /* restart output  */
1732        tty->flow_ctrl &= ~FL_ORCVXOF;
1733        flow_rcv = true;
1734      }
1735    }
1736    if (flow_rcv) {
1737      /* restart output according to FL_ORCVXOF flag */
1738      if ((tty->flow_ctrl & (FL_ORCVXOF | FL_OSTOP)) == FL_OSTOP) {
1739        /* disable interrupts    */
1740        rtems_termios_device_lock_acquire (ctx, &lock_context);
1741        tty->flow_ctrl &= ~FL_OSTOP;
1742        /* check for chars in output buffer (or rob_state?) */
1743        if (tty->rawOutBufState != rob_idle) {
1744          /* if chars available, call write function... */
1745          (*tty->handler.write)(
1746            ctx, &tty->rawOutBuf.theBuf[tty->rawOutBuf.Tail], 1);
1747        }
1748        /* reenable interrupts */
1749        rtems_termios_device_lock_release (ctx, &lock_context);
1750      }
1751    } else {
1752      unsigned int head;
1753      unsigned int oldTail;
1754      unsigned int newTail;
1755      bool callReciveCallback;
1756
1757      if (c == '\r' && (tty->termios.c_iflag & IGNCR) != 0) {
1758        continue;
1759      }
1760
1761      c = iprocEarly (c, tty);
1762
1763      rtems_termios_device_lock_acquire (ctx, &lock_context);
1764
1765      head = tty->rawInBuf.Head;
1766      oldTail = tty->rawInBuf.Tail;
1767      newTail = (oldTail + 1) % tty->rawInBuf.Size;
1768
1769      /* if chars_in_buffer > highwater                */
1770      if ((tty->flow_ctrl & FL_IREQXOF) != 0 && (((newTail - head) %
1771          tty->rawInBuf.Size) > tty->highwater)) {
1772        /* incoming data stream should be stopped */
1773        tty->flow_ctrl |= FL_IREQXOF;
1774        if ((tty->flow_ctrl & (FL_MDXOF | FL_ISNTXOF))
1775            ==                (FL_MDXOF             ) ) {
1776          if ((tty->flow_ctrl & FL_OSTOP) ||
1777              (tty->rawOutBufState == rob_idle)) {
1778            /* if tx is stopped due to XOFF or out of data */
1779            /*    call write function here                 */
1780            tty->flow_ctrl |= FL_ISNTXOF;
1781            (*tty->handler.write)(ctx,
1782                (void *)&(tty->termios.c_cc[VSTOP]), 1);
1783          }
1784        } else if ((tty->flow_ctrl & (FL_MDRTS | FL_IRTSOFF)) == (FL_MDRTS) ) {
1785          tty->flow_ctrl |= FL_IRTSOFF;
1786          /* deactivate RTS line */
1787          if (tty->flow.stop_remote_tx != NULL) {
1788            tty->flow.stop_remote_tx(ctx);
1789          }
1790        }
1791      }
1792
1793      callReciveCallback = false;
1794
1795      if (newTail != head) {
1796        tty->rawInBuf.theBuf[newTail] = c;
1797        tty->rawInBuf.Tail = newTail;
1798
1799        /*
1800         * check to see if rcv wakeup callback was set
1801         */
1802        if (tty->tty_rcv.sw_pfn != NULL && !tty->tty_rcvwakeup) {
1803          if (mustCallReceiveCallback (tty, c, newTail, head)) {
1804            tty->tty_rcvwakeup = true;
1805            callReciveCallback = true;
1806          }
1807        }
1808      } else {
1809        ++dropped;
1810
1811        if (tty->tty_rcv.sw_pfn != NULL && !tty->tty_rcvwakeup) {
1812          tty->tty_rcvwakeup = true;
1813          callReciveCallback = true;
1814        }
1815      }
1816
1817      rtems_termios_device_lock_release (ctx, &lock_context);
1818
1819      if (callReciveCallback) {
1820        (*tty->tty_rcv.sw_pfn)(&tty->termios, tty->tty_rcv.sw_arg);
1821      }
1822    }
1823  }
1824
1825  tty->rawInBufDropped += dropped;
1826  rtems_semaphore_release (tty->rawInBuf.Semaphore);
1827  return dropped;
1828}
1829
1830/*
1831 * in task-driven mode, this function is called in Tx task context
1832 * in interrupt-driven mode, this function is called in TxIRQ context
1833 */
1834static int
1835rtems_termios_refill_transmitter (struct rtems_termios_tty *tty)
1836{
1837  bool wakeUpWriterTask = false;
1838  unsigned int newTail;
1839  int nToSend;
1840  rtems_termios_device_context *ctx = tty->device_context;
1841  rtems_interrupt_lock_context lock_context;
1842  int len;
1843
1844  rtems_termios_device_lock_acquire (ctx, &lock_context);
1845
1846  /* check for XOF/XON to send */
1847  if ((tty->flow_ctrl & (FL_MDXOF | FL_IREQXOF | FL_ISNTXOF))
1848      == (FL_MDXOF | FL_IREQXOF)) {
1849    /* XOFF should be sent now... */
1850    (*tty->handler.write)(ctx, (void *)&(tty->termios.c_cc[VSTOP]), 1);
1851
1852    tty->t_dqlen--;
1853    tty->flow_ctrl |= FL_ISNTXOF;
1854
1855    nToSend = 1;
1856
1857  } else if ((tty->flow_ctrl & (FL_IREQXOF | FL_ISNTXOF)) == FL_ISNTXOF) {
1858    /* NOTE: send XON even, if no longer in XON/XOFF mode... */
1859    /* XON should be sent now... */
1860    /*
1861     * FIXME: this .write call will generate another
1862     * dequeue callback. This will advance the "Tail" in the data
1863     * buffer, although the corresponding data is not yet out!
1864     * Therefore the dequeue "length" should be reduced by 1
1865     */
1866    (*tty->handler.write)(ctx, (void *)&(tty->termios.c_cc[VSTART]), 1);
1867
1868    tty->t_dqlen--;
1869    tty->flow_ctrl &= ~FL_ISNTXOF;
1870
1871    nToSend = 1;
1872  } else if ( tty->rawOutBuf.Head == tty->rawOutBuf.Tail ) {
1873    /*
1874     * buffer was empty
1875     */
1876    if (tty->rawOutBufState == rob_wait) {
1877      /*
1878       * this should never happen...
1879       */
1880      wakeUpWriterTask = true;
1881    }
1882
1883    (*tty->handler.write) (ctx, NULL, 0);
1884    nToSend = 0;
1885  } else {
1886    len = tty->t_dqlen;
1887    tty->t_dqlen = 0;
1888
1889    newTail = (tty->rawOutBuf.Tail + len) % tty->rawOutBuf.Size;
1890    tty->rawOutBuf.Tail = newTail;
1891    if (tty->rawOutBufState == rob_wait) {
1892      /*
1893       * wake up any pending writer task
1894       */
1895      wakeUpWriterTask = true;
1896    }
1897
1898    if (newTail == tty->rawOutBuf.Head) {
1899      /*
1900       * Buffer has become empty
1901       */
1902      tty->rawOutBufState = rob_idle;
1903      (*tty->handler.write) (ctx, NULL, 0);
1904      nToSend = 0;
1905
1906      /*
1907       * check to see if snd wakeup callback was set
1908       */
1909      if ( tty->tty_snd.sw_pfn != NULL) {
1910        (*tty->tty_snd.sw_pfn)(&tty->termios, tty->tty_snd.sw_arg);
1911      }
1912    } else {
1913      /*
1914       * Buffer not empty, check flow control, start transmitter
1915       */
1916      nToSend = startXmit (tty, newTail, true);
1917    }
1918  }
1919
1920  rtems_termios_device_lock_release (ctx, &lock_context);
1921
1922  if (wakeUpWriterTask) {
1923    rtems_semaphore_release (tty->rawOutBuf.Semaphore);
1924  }
1925
1926  return nToSend;
1927}
1928
1929/*
1930 * Characters have been transmitted
1931 * NOTE: This routine runs in the context of the
1932 *       device transmit interrupt handler.
1933 * The second argument is the number of characters transmitted so far.
1934 * This value will always be 1 for devices which generate an interrupt
1935 * for each transmitted character.
1936 * It returns number of characters left to transmit
1937 */
1938int
1939rtems_termios_dequeue_characters (void *ttyp, int len)
1940{
1941  struct rtems_termios_tty *tty = ttyp;
1942  rtems_status_code sc;
1943
1944  /*
1945   * sum up character count already sent
1946   */
1947  tty->t_dqlen += len;
1948
1949  if (tty->handler.mode == TERMIOS_TASK_DRIVEN) {
1950    /*
1951     * send wake up to transmitter task
1952     */
1953    sc = rtems_event_send(tty->txTaskId, TERMIOS_TX_START_EVENT);
1954    if (sc != RTEMS_SUCCESSFUL)
1955      rtems_fatal_error_occurred (sc);
1956    return 0; /* nothing to output in IRQ... */
1957  }
1958
1959  if (tty->t_line == PPPDISC ) {
1960    /*
1961     * call PPP line discipline start function
1962     */
1963    if (rtems_termios_linesw[tty->t_line].l_start != NULL) {
1964      rtems_termios_linesw[tty->t_line].l_start(tty);
1965    }
1966    return 0; /* nothing to output in IRQ... */
1967  }
1968
1969  return rtems_termios_refill_transmitter(tty);
1970}
1971
1972/*
1973 * this task actually processes any transmit events
1974 */
1975static rtems_task rtems_termios_txdaemon(rtems_task_argument argument)
1976{
1977  struct rtems_termios_tty *tty = (struct rtems_termios_tty *)argument;
1978  rtems_event_set the_event;
1979
1980  while (1) {
1981    /*
1982     * wait for rtems event
1983     */
1984    rtems_event_receive(
1985       (TERMIOS_TX_START_EVENT | TERMIOS_TX_TERMINATE_EVENT),
1986       RTEMS_EVENT_ANY | RTEMS_WAIT,
1987       RTEMS_NO_TIMEOUT,
1988       &the_event
1989    );
1990    if ((the_event & TERMIOS_TX_TERMINATE_EVENT) != 0) {
1991      tty->txTaskId = 0;
1992      rtems_task_delete(RTEMS_SELF);
1993    }
1994
1995    /*
1996     * call any line discipline start function
1997     */
1998    if (rtems_termios_linesw[tty->t_line].l_start != NULL) {
1999      rtems_termios_linesw[tty->t_line].l_start(tty);
2000
2001      if (tty->t_line == PPPDISC) {
2002        /*
2003         * Do not call rtems_termios_refill_transmitter() in this case similar
2004         * to rtems_termios_dequeue_characters().
2005         */
2006        continue;
2007      }
2008    }
2009
2010    /*
2011     * try to push further characters to device
2012     */
2013    rtems_termios_refill_transmitter(tty);
2014  }
2015}
2016
2017/*
2018 * this task actually processes any receive events
2019 */
2020static rtems_task rtems_termios_rxdaemon(rtems_task_argument argument)
2021{
2022  struct rtems_termios_tty *tty = (struct rtems_termios_tty *)argument;
2023  rtems_termios_device_context *ctx = tty->device_context;
2024  rtems_event_set the_event;
2025  int c;
2026  char c_buf;
2027
2028  while (1) {
2029    /*
2030     * wait for rtems event
2031     */
2032    rtems_event_receive(
2033      (TERMIOS_RX_PROC_EVENT | TERMIOS_RX_TERMINATE_EVENT),
2034      RTEMS_EVENT_ANY | RTEMS_WAIT,
2035      RTEMS_NO_TIMEOUT,
2036      &the_event
2037    );
2038    if ((the_event & TERMIOS_RX_TERMINATE_EVENT) != 0) {
2039      tty->rxTaskId = 0;
2040      rtems_task_delete(RTEMS_SELF);
2041    }
2042
2043    /*
2044     * do something
2045     */
2046    c = tty->handler.poll_read(ctx);
2047    if (c != EOF) {
2048      /*
2049       * poll_read did call enqueue on its own
2050       */
2051      c_buf = c;
2052      rtems_termios_enqueue_raw_characters ( tty,&c_buf,1);
2053    }
2054  }
2055}
2056
2057static int
2058rtems_termios_imfs_open (rtems_libio_t *iop,
2059  const char *path, int oflag, mode_t mode)
2060{
2061  rtems_termios_device_node *device_node;
2062  rtems_status_code sc;
2063  rtems_libio_open_close_args_t args;
2064  struct rtems_termios_tty *tty;
2065
2066  device_node = IMFS_generic_get_context_by_iop (iop);
2067
2068  memset (&args, 0, sizeof (args));
2069  args.iop = iop;
2070  args.flags = iop->flags;
2071  args.mode = mode;
2072
2073  sc = rtems_termios_obtain ();
2074  if (sc != RTEMS_SUCCESSFUL) {
2075    rtems_set_errno_and_return_minus_one (ENXIO);
2076  }
2077
2078  tty = rtems_termios_open_tty (device_node->major, device_node->minor, &args,
2079      device_node->tty, device_node, NULL);
2080  if (tty == NULL) {
2081    rtems_termios_release ();
2082    rtems_set_errno_and_return_minus_one (ENOMEM);
2083  }
2084
2085  rtems_termios_release ();
2086  return 0;
2087}
2088
2089static int
2090rtems_termios_imfs_close (rtems_libio_t *iop)
2091{
2092  rtems_status_code sc;
2093  rtems_libio_open_close_args_t args;
2094  struct rtems_termios_tty *tty;
2095
2096  memset (&args, 0, sizeof (args));
2097  args.iop = iop;
2098
2099  tty = iop->data1;
2100
2101  sc = rtems_termios_obtain ();
2102  _Assert (sc == RTEMS_SUCCESSFUL);
2103  (void) sc;
2104
2105  rtems_termios_close_tty (tty, &args);
2106  rtems_termios_release ();
2107  return 0;
2108}
2109
2110static ssize_t
2111rtems_termios_imfs_read (rtems_libio_t *iop, void *buffer, size_t count)
2112{
2113  struct rtems_termios_tty *tty;
2114  rtems_status_code sc;
2115  uint32_t bytes_moved;
2116
2117  tty = iop->data1;
2118
2119  sc = rtems_semaphore_obtain (tty->isem, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
2120  _Assert (sc == RTEMS_SUCCESSFUL);
2121
2122  if (rtems_termios_linesw[tty->t_line].l_read != NULL) {
2123    rtems_libio_rw_args_t args;
2124
2125    memset (&args, 0, sizeof (args));
2126    args.iop = iop;
2127    args.buffer = buffer;
2128    args.count = count;
2129    args.flags = iop->flags;
2130
2131    sc = rtems_termios_linesw[tty->t_line].l_read (tty, &args);
2132    tty->tty_rcvwakeup = false;
2133    rtems_semaphore_release (tty->isem);
2134
2135    if (sc != RTEMS_SUCCESSFUL) {
2136      return rtems_status_code_to_errno (sc);
2137    }
2138
2139    return (ssize_t) args.bytes_moved;
2140  }
2141
2142  bytes_moved = rtems_termios_read_tty (tty, buffer, count);
2143  rtems_semaphore_release (tty->isem);
2144  return (ssize_t) bytes_moved;
2145}
2146
2147static ssize_t
2148rtems_termios_imfs_write (rtems_libio_t *iop, const void *buffer, size_t count)
2149{
2150  struct rtems_termios_tty *tty;
2151  rtems_status_code sc;
2152  uint32_t bytes_moved;
2153
2154  tty = iop->data1;
2155
2156  sc = rtems_semaphore_obtain (tty->osem, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
2157  _Assert (sc == RTEMS_SUCCESSFUL);
2158
2159  if (rtems_termios_linesw[tty->t_line].l_write != NULL) {
2160    rtems_libio_rw_args_t args;
2161
2162    memset (&args, 0, sizeof (args));
2163    args.iop = iop;
2164    args.buffer = RTEMS_DECONST (void *, buffer);
2165    args.count = count;
2166    args.flags = iop->flags;
2167
2168    sc = rtems_termios_linesw[tty->t_line].l_write (tty, &args);
2169    rtems_semaphore_release (tty->osem);
2170
2171    if (sc != RTEMS_SUCCESSFUL) {
2172      return rtems_status_code_to_errno (sc);
2173    }
2174
2175    return (ssize_t) args.bytes_moved;
2176  }
2177
2178  bytes_moved = rtems_termios_write_tty (iop, tty, buffer, count);
2179  rtems_semaphore_release (tty->osem);
2180  return (ssize_t) bytes_moved;
2181}
2182
2183static int
2184rtems_termios_imfs_ioctl (rtems_libio_t *iop, ioctl_command_t request,
2185  void *buffer)
2186{
2187  rtems_status_code sc;
2188  rtems_libio_ioctl_args_t args;
2189
2190  memset (&args, 0, sizeof (args));
2191  args.iop = iop;
2192  args.command = request;
2193  args.buffer = buffer;
2194
2195  sc = rtems_termios_ioctl (&args);
2196  if ( sc == RTEMS_SUCCESSFUL ) {
2197    return args.ioctl_return;
2198  } else {
2199    return rtems_status_code_to_errno (sc);
2200  }
2201}
2202
2203static const rtems_filesystem_file_handlers_r rtems_termios_imfs_handler = {
2204  .open_h = rtems_termios_imfs_open,
2205  .close_h = rtems_termios_imfs_close,
2206  .read_h = rtems_termios_imfs_read,
2207  .write_h = rtems_termios_imfs_write,
2208  .ioctl_h = rtems_termios_imfs_ioctl,
2209  .lseek_h = rtems_filesystem_default_lseek,
2210  .fstat_h = IMFS_stat,
2211  .ftruncate_h = rtems_filesystem_default_ftruncate,
2212  .fsync_h = rtems_filesystem_default_fsync_or_fdatasync,
2213  .fdatasync_h = rtems_filesystem_default_fsync_or_fdatasync,
2214  .fcntl_h = rtems_filesystem_default_fcntl,
2215  .kqfilter_h = rtems_termios_kqfilter,
2216  .mmap_h = rtems_termios_mmap,
2217  .poll_h = rtems_termios_poll,
2218  .readv_h = rtems_filesystem_default_readv,
2219  .writev_h = rtems_filesystem_default_writev
2220};
2221
2222static IMFS_jnode_t *
2223rtems_termios_imfs_node_initialize (IMFS_jnode_t *node, void *arg)
2224{
2225  rtems_termios_device_node *device_node;
2226  dev_t dev;
2227
2228  node = IMFS_node_initialize_generic (node, arg);
2229  device_node = IMFS_generic_get_context_by_node (node);
2230  dev = IMFS_generic_get_device_identifier_by_node (node);
2231  device_node->major = rtems_filesystem_dev_major_t (dev);
2232  device_node->minor = rtems_filesystem_dev_minor_t (dev);
2233
2234  return node;
2235}
2236
2237static void
2238rtems_termios_imfs_node_destroy (IMFS_jnode_t *node)
2239{
2240  rtems_termios_device_node *device_node;
2241
2242  device_node = IMFS_generic_get_context_by_node (node);
2243  free (device_node);
2244  IMFS_node_destroy_default (node);
2245}
2246
2247static const IMFS_node_control rtems_termios_imfs_node_control =
2248  IMFS_GENERIC_INITIALIZER(
2249    &rtems_termios_imfs_handler,
2250    rtems_termios_imfs_node_initialize,
2251    rtems_termios_imfs_node_destroy
2252  );
Note: See TracBrowser for help on using the repository browser.