source: rtems/cpukit/libcsupport/src/termios.c @ 93531e9b

5
Last change on this file since 93531e9b was 93531e9b, checked in by Sebastian Huber <sebastian.huber@…>, on 06/06/17 at 06:03:45

Move RTEMS-specific Termios API content

Remove obsolete support for OFILL, OFDEL, NLDLY, CRDLY, BSDLY, VTDLY,
and FFDLY which is not present on FreeBSD and not implemented in Linux.

Update #2833.

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