source: rtems/cpukit/libcsupport/include/rtems/termiostypes.h @ 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: 15.3 KB
Line 
1/**
2 * @file rtems/termiostypes.h
3 *
4 * RTEMS termios device support internal data structures
5 */
6
7/*
8 *  COPYRIGHT (c) 1989-2011.
9 *  On-Line Applications Research Corporation (OAR).
10 *
11 *  The license and distribution terms for this file may be
12 *  found in the file LICENSE in this distribution or at
13 *  http://www.rtems.org/license/LICENSE.
14 */
15
16#ifndef  __TERMIOSTYPES_H
17#define  __TERMIOSTYPES_H
18
19#include <rtems.h>
20#include <rtems/libio.h>
21#include <rtems/assoc.h>
22#include <rtems/chain.h>
23#include <sys/ioccom.h>
24#include <stdint.h>
25#include <termios.h>
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31/**
32 *  @defgroup TermiostypesSupport RTEMS Termios Device Support
33 *
34 *  @ingroup libcsupport
35 *
36 *  @brief RTEMS Termios Device Support Internal Data Structures
37 */
38
39/*
40 * Wakeup callback data structure
41 */
42struct ttywakeup {
43  void      (*sw_pfn)(struct termios *tty, void *arg);
44  void      *sw_arg;
45};
46
47/*
48 * Variables associated with the character buffer
49 */
50struct rtems_termios_rawbuf {
51  char *theBuf;
52  volatile unsigned int  Head;
53  volatile unsigned int  Tail;
54  volatile unsigned int  Size;
55  rtems_id    Semaphore;
56};
57
58typedef enum {
59  TERMIOS_POLLED,
60  TERMIOS_IRQ_DRIVEN,
61  TERMIOS_TASK_DRIVEN,
62  TERMIOS_IRQ_SERVER_DRIVEN
63} rtems_termios_device_mode;
64
65struct rtems_termios_tty;
66
67/**
68 * @brief Termios device context.
69 *
70 * @see RTEMS_TERMIOS_DEVICE_CONTEXT_INITIALIZER(),
71 * rtems_termios_device_context_initialize() and
72 * rtems_termios_device_install().
73 */
74typedef struct rtems_termios_device_context {
75  union {
76    /* Used for TERMIOS_POLLED and TERMIOS_IRQ_DRIVEN */
77    rtems_interrupt_lock interrupt;
78
79    /* Used for TERMIOS_IRQ_SERVER_DRIVEN or TERMIOS_TASK_DRIVEN */
80    rtems_id mutex;
81  } lock;
82
83  void ( *lock_acquire )(
84    struct rtems_termios_device_context *,
85    rtems_interrupt_lock_context *
86  );
87
88  void ( *lock_release )(
89    struct rtems_termios_device_context *,
90    rtems_interrupt_lock_context *
91  );
92} rtems_termios_device_context;
93
94void rtems_termios_device_lock_acquire_default(
95  rtems_termios_device_context *ctx,
96  rtems_interrupt_lock_context *lock_context
97);
98
99void rtems_termios_device_lock_release_default(
100  rtems_termios_device_context *ctx,
101  rtems_interrupt_lock_context *lock_context
102);
103
104/**
105 * @brief Initializes a device context.
106 *
107 * @param[in] context The Termios device context.
108 * @param[in] name The name for the interrupt lock.  This name must be a
109 *   string persistent throughout the life time of this lock.  The name is only
110 *   used if profiling is enabled.
111 */
112RTEMS_INLINE_ROUTINE void rtems_termios_device_context_initialize(
113  rtems_termios_device_context *context,
114  const char                   *name
115)
116{
117  rtems_interrupt_lock_initialize( &context->lock.interrupt, name );
118  context->lock_acquire = rtems_termios_device_lock_acquire_default;
119  context->lock_release = rtems_termios_device_lock_release_default;
120}
121
122/**
123 * @brief Initializer for static initialization of Termios device contexts.
124 *
125 * @param name The name for the interrupt lock.  It must be a string.  The name
126 *   is only used if profiling is enabled.
127 */
128#define RTEMS_TERMIOS_DEVICE_CONTEXT_INITIALIZER( name ) \
129  { \
130    { RTEMS_INTERRUPT_LOCK_INITIALIZER( name ) }, \
131    rtems_termios_device_lock_acquire_default, \
132    rtems_termios_device_lock_release_default \
133  }
134
135/**
136 * @brief Termios device handler.
137 *
138 * @see rtems_termios_device_install().
139 */
140typedef struct {
141  /**
142   * @brief First open of this device.
143   *
144   * @param[in] tty The Termios control.  This parameter may be passed to
145   *   interrupt service routines since it must be provided for the
146   *   rtems_termios_enqueue_raw_characters() and
147   *   rtems_termios_dequeue_characters() functions.
148   * @param[in] context The Termios device context.
149   * @param[in] term The current Termios attributes.
150   * @param[in] args The open/close arguments.  This is parameter provided to
151   *   support legacy drivers.  It must not be used by new drivers.
152   *
153   * @retval true Successful operation.
154   * @retval false Cannot open device.
155   *
156   * @see rtems_termios_get_device_context() and rtems_termios_set_best_baud().
157   */
158  bool (*first_open)(
159    struct rtems_termios_tty      *tty,
160    rtems_termios_device_context  *context,
161    struct termios                *term,
162    rtems_libio_open_close_args_t *args
163  );
164
165  /**
166   * @brief Last close of this device.
167   *
168   * @param[in] tty The Termios control.
169   * @param[in] context The Termios device context.
170   * @param[in] args The open/close arguments.  This is parameter provided to
171   *   support legacy drivers.  It must not be used by new drivers.
172   */
173  void (*last_close)(
174    struct rtems_termios_tty      *tty,
175    rtems_termios_device_context  *context,
176    rtems_libio_open_close_args_t *args
177  );
178
179  /**
180   * @brief Polled read.
181   *
182   * In case mode is TERMIOS_IRQ_DRIVEN, TERMIOS_IRQ_SERVER_DRIVEN or
183   * TERMIOS_TASK_DRIVEN, then data is received via
184   * rtems_termios_enqueue_raw_characters().
185   *
186   * @param[in] context The Termios device context.
187   *
188   * @retval char The received data encoded as unsigned char.
189   * @retval -1 No data currently available.
190   */
191  int (*poll_read)(rtems_termios_device_context *context);
192
193  /**
194   * @brief Polled write in case mode is TERMIOS_POLLED or write support
195   * otherwise.
196   *
197   * @param[in] context The Termios device context.
198   * @param[in] buf The output buffer.
199   * @param[in] len The output buffer length in characters.
200   */
201  void (*write)(
202    rtems_termios_device_context *context,
203    const char *buf,
204    size_t len
205  );
206
207  /**
208   * @brief Set attributes after a Termios settings change.
209   *
210   * @param[in] context The Termios device context.
211   * @param[in] term The new Termios attributes.
212   *
213   * @retval true Successful operation.
214   * @retval false Invalid attributes.
215   */
216  bool (*set_attributes)(
217    rtems_termios_device_context *context,
218    const struct termios         *term
219  );
220
221  /**
222   * @brief IO control handler.
223   *
224   * Invoked in case the Termios layer cannot deal with the IO request.
225   *
226   * @param[in] context The Termios device context.
227   * @param[in] request The IO control request.
228   * @param[in] buffer The IO control buffer.
229   */
230  int (*ioctl)(
231    rtems_termios_device_context *context,
232    ioctl_command_t               request,
233    void                         *buffer
234  );
235
236  /**
237   * @brief Termios device mode.
238   */
239  rtems_termios_device_mode mode;
240} rtems_termios_device_handler;
241
242/**
243 * @brief Termios device flow control handler.
244 *
245 * @see rtems_termios_device_install().
246 */
247typedef struct {
248  /**
249   * @brief Indicate to stop remote transmitter.
250   *
251   * @param[in] context The Termios device context.
252   */
253  void (*stop_remote_tx)(rtems_termios_device_context *context);
254
255  /**
256   * @brief Indicate to start remote transmitter.
257   *
258   * @param[in] context The Termios device context.
259   */
260  void (*start_remote_tx)(rtems_termios_device_context *context);
261} rtems_termios_device_flow;
262
263/**
264 * @brief Termios device node for installed devices.
265 *
266 * @see rtems_termios_device_install().
267 */
268typedef struct rtems_termios_device_node {
269  rtems_chain_node                    node;
270  rtems_device_major_number           major;
271  rtems_device_minor_number           minor;
272  const rtems_termios_device_handler *handler;
273  const rtems_termios_device_flow    *flow;
274  rtems_termios_device_context       *context;
275  struct rtems_termios_tty           *tty;
276} rtems_termios_device_node;
277
278/*
279 * Variables associated with each termios instance.
280 * One structure for each hardware I/O device.
281 */
282typedef struct rtems_termios_tty {
283  /*
284   * Linked-list of active TERMIOS devices
285   */
286  struct rtems_termios_tty  *forw;
287  struct rtems_termios_tty  *back;
288
289  /*
290   * How many times has this device been opened
291   */
292  int    refcount;
293
294  /*
295   * This device
296   */
297  rtems_device_major_number  major;
298  rtems_device_minor_number  minor;
299
300  /*
301   * Mutual-exclusion semaphores
302   */
303  rtems_id  isem;
304  rtems_id  osem;
305
306  /*
307   * The canonical (cooked) character buffer
308   */
309  char    *cbuf;
310  int    ccount;
311  int    cindex;
312
313  /*
314   * Keep track of cursor (printhead) position
315   */
316  int    column;
317  int    read_start_column;
318
319  /*
320   * The ioctl settings
321   */
322  struct termios  termios;
323  rtems_interval  vtimeTicks;
324
325  /*
326   * Raw input character buffer
327   */
328  struct rtems_termios_rawbuf rawInBuf;
329  uint32_t                    rawInBufSemaphoreOptions;
330  rtems_interval              rawInBufSemaphoreTimeout;
331  rtems_interval              rawInBufSemaphoreFirstTimeout;
332  unsigned int                rawInBufDropped;  /* Statistics */
333
334  /*
335   * Raw output character buffer
336   */
337  struct rtems_termios_rawbuf rawOutBuf;
338  int  t_dqlen; /* count of characters dequeued from device */
339  enum {rob_idle, rob_busy, rob_wait }  rawOutBufState;
340
341  /*
342   * Callbacks to device-specific routines
343   */
344  rtems_termios_callbacks  device;
345
346  /**
347   * @brief Context for legacy devices using the callbacks.
348   */
349  rtems_termios_device_context legacy_device_context;
350
351  /**
352   * @brief The device handler.
353   */
354  rtems_termios_device_handler handler;
355
356  /**
357   * @brief The device flow control handler.
358   */
359  rtems_termios_device_flow flow;
360
361  volatile unsigned int    flow_ctrl;
362  unsigned int             lowwater,highwater;
363
364  /*
365   * I/O task IDs (for task-driven drivers)
366   */
367  rtems_id                rxTaskId;
368  rtems_id                txTaskId;
369
370  /*
371   * line discipline related stuff
372   */
373  int t_line;   /* id of line discipline                       */
374  void *t_sc;   /* hook for discipline-specific data structure */
375
376  /*
377   * Wakeup callback variables
378   */
379  struct ttywakeup tty_snd;
380  struct ttywakeup tty_rcv;
381  bool             tty_rcvwakeup;
382
383  /**
384   * @brief Corresponding device node.
385   */
386  rtems_termios_device_node *device_node;
387
388  /**
389   * @brief Context for device driver.
390   */
391  rtems_termios_device_context *device_context;
392} rtems_termios_tty;
393
394/**
395 * @brief Installs a Termios device.
396 *
397 * The installed Termios device may be removed via unlink().
398 *
399 * @param[in] device_file The device file path.
400 * @param[in] handler The device handler.  It must be persistent throughout the
401 *   installed time of the device.
402 * @param[in] flow The device flow control handler.  The device flow control
403 *   handler are optional and may be @c NULL.  If present must be persistent
404 *   throughout the installed time of the device.
405 * @param[in] context The device context.  It must be persistent throughout the
406 *   installed time of the device.
407 *
408 * @retval RTEMS_SUCCESSFUL Successful operation.
409 * @retval RTEMS_NO_MEMORY Not enough memory to create a device node.
410 * @retval RTEMS_UNSATISFIED Creation of the device file failed.
411 * @retval RTEMS_INCORRECT_STATE Termios is not initialized.
412 *
413 * @see rtems_termios_get_device_context().
414 */
415rtems_status_code rtems_termios_device_install(
416  const char                         *device_file,
417  const rtems_termios_device_handler *handler,
418  const rtems_termios_device_flow    *flow,
419  rtems_termios_device_context       *context
420);
421
422/**
423 * @brief Returns the device context of an installed Termios device.
424 *
425 * @param[in] tty The Termios control.
426 */
427RTEMS_INLINE_ROUTINE void *rtems_termios_get_device_context(
428  const rtems_termios_tty *tty
429)
430{
431  return tty->device_context;
432}
433
434/**
435 * @brief Acquires the device lock.
436 *
437 * @param[in] context The device context.
438 * @param[in] lock_context The local interrupt lock context for an acquire and
439 *   release pair.
440 */
441RTEMS_INLINE_ROUTINE void rtems_termios_device_lock_acquire(
442  rtems_termios_device_context *context,
443  rtems_interrupt_lock_context *lock_context
444)
445{
446  ( *context->lock_acquire )( context, lock_context );
447}
448
449/**
450 * @brief Releases the device lock.
451 *
452 * @param[in] context The device context.
453 * @param[in] lock_context The local interrupt lock context for an acquire and
454 *   release pair.
455 */
456RTEMS_INLINE_ROUTINE void rtems_termios_device_lock_release(
457  rtems_termios_device_context *context,
458  rtems_interrupt_lock_context *lock_context
459)
460{
461  ( *context->lock_release )( context, lock_context );
462}
463
464/**
465 * @brief Sets the best baud value in the Termios control.
466 *
467 * The valid Termios baud values are between 0 and 460800.  The Termios baud
468 * value is chosen which minimizes the difference to the value specified.
469 *
470 * @param[in] term The Termios attributes.
471 * @param[in] baud The current baud setting of the device.
472 */
473void rtems_termios_set_best_baud(
474  struct termios *term,
475  uint32_t        baud
476);
477
478struct rtems_termios_linesw {
479  int (*l_open) (struct rtems_termios_tty *tp);
480  int (*l_close)(struct rtems_termios_tty *tp);
481  int (*l_read )(struct rtems_termios_tty *tp,rtems_libio_rw_args_t *args);
482  int (*l_write)(struct rtems_termios_tty *tp,rtems_libio_rw_args_t *args);
483  int (*l_rint )(int c,struct rtems_termios_tty *tp);
484  int (*l_start)(struct rtems_termios_tty *tp);
485  int (*l_ioctl)(struct rtems_termios_tty *tp,rtems_libio_ioctl_args_t *args);
486  int (*l_modem)(struct rtems_termios_tty *tp,int flags);
487};
488
489/*
490 * FIXME: this should move to termios.h!
491 */
492void rtems_termios_rxirq_occured(struct rtems_termios_tty *tty);
493
494/*
495 * FIXME: this should move to termios.h!
496 * put a string to output ring buffer
497 */
498void rtems_termios_puts (
499  const void               *buf,
500  size_t                    len,
501  struct rtems_termios_tty *tty
502);
503
504/*
505 * global hooks for line disciplines
506 */
507extern struct rtems_termios_linesw rtems_termios_linesw[];
508extern int   rtems_termios_nlinesw;
509
510#define TTYDISC   0    /* termios tty line discipline */
511#define TABLDISC  3    /* tablet discipline */
512#define SLIPDISC  4    /* serial IP discipline */
513#define PPPDISC   5    /* PPP discipline */
514#define MAXLDISC  8
515
516/* baudrate xxx integer type */
517typedef uint32_t rtems_termios_baud_t;
518
519/**
520 *  @brief RTEMS Termios Baud Table
521 */
522extern const rtems_assoc_t rtems_termios_baud_table [];
523
524/**
525 *  @brief Converts the Integral Baud value @a baud to the Termios Control Flag
526 *  Representation
527 *
528 *  @retval B0 Invalid baud value or a baud value of 0.
529 *  @retval other Baud constant according to @a baud.
530 */
531speed_t rtems_termios_number_to_baud(rtems_termios_baud_t baud);
532
533/**
534 *  @brief Converts the baud flags to an integral baud value.
535 *
536 *  @retval 0 Invalid baud value or a baud value of @c B0.
537 *  @retval other Integral baud value.
538 */
539rtems_termios_baud_t rtems_termios_baud_to_number(speed_t baud);
540
541/**
542 *  @brief Convert Bxxx Constant to Index
543 */
544int  rtems_termios_baud_to_index(rtems_termios_baud_t termios_baud);
545
546/**
547 * @brief Sets the initial @a baud in the Termios context @a tty.
548 *
549 * @retval 0 Successful operation.
550 * @retval -1 Invalid baud value.
551 */
552int rtems_termios_set_initial_baud(
553  struct rtems_termios_tty *tty,
554  rtems_termios_baud_t baud
555);
556
557/**
558 * @brief Termios kqueue() filter filesystem node handler
559 *
560 * Real implementation is provided by libbsd.
561 */
562int rtems_termios_kqfilter(
563  rtems_libio_t *iop,
564  struct knote  *kn
565);
566
567/**
568 * @brief Termios poll() filesystem node handler.
569 *
570 * Real implementation is provided by libbsd.
571 */
572int rtems_termios_poll(
573  rtems_libio_t *iop,
574  int            events
575);
576
577#define RTEMS_IO_SNDWAKEUP _IOW('t', 11, struct ttywakeup ) /* send tty wakeup */
578#define RTEMS_IO_RCVWAKEUP _IOW('t', 12, struct ttywakeup ) /* recv tty wakeup */
579
580#define OLCUC           0x00000100      /* map lower case to upper case on output */
581#define IUCLC           0x00004000      /* map upper case to lower case on input */
582
583#define RTEMS_TERMIOS_NUMBER_BAUD_RATES 25
584
585#ifdef __cplusplus
586}
587#endif
588
589#endif  /* TERMIOSTYPES_H */
Note: See TracBrowser for help on using the repository browser.