source: rtems/cpukit/include/rtems/termiostypes.h @ 0c3d6f58

Last change on this file since 0c3d6f58 was 0c3d6f58, checked in by Sebastian Huber <sebastian.huber@…>, on 05/23/23 at 08:44:04

termios: Add <rtems/termiosdevice.h>

Add <rtems/termiosdevice.h> which does not depend on <rtems/libio.h> to
provide rtems_termios_device_context and rtems_termios_device_handler.
For polled serial device drivers, this removes a header file dependency
to the full file system support.

  • Property mode set to 100644
File size: 10.2 KB
Line 
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/**
4 * @file
5 *
6 * RTEMS termios device support internal data structures
7 */
8
9/*
10 *  COPYRIGHT (c) 1989-2011.
11 *  On-Line Applications Research Corporation (OAR).
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35#ifndef  __TERMIOSTYPES_H
36#define  __TERMIOSTYPES_H
37
38#include <rtems.h>
39#include <rtems/libio.h>
40#include <rtems/assoc.h>
41#include <rtems/chain.h>
42#include <rtems/termiosdevice.h>
43#include <stdint.h>
44#include <termios.h>
45
46#ifdef __cplusplus
47extern "C" {
48#endif
49
50/**
51 * @addtogroup TermiostypesSupport
52 *
53 * @{
54 */
55
56/*
57 * Wakeup callback data structure
58 */
59struct ttywakeup {
60  void      (*sw_pfn)(struct termios *tty, void *arg);
61  void      *sw_arg;
62};
63
64/*
65 * Variables associated with the character buffer
66 */
67struct rtems_termios_rawbuf {
68  char *theBuf;
69  volatile unsigned int  Head;
70  volatile unsigned int  Tail;
71  volatile unsigned int  Size;
72  rtems_binary_semaphore Semaphore;
73};
74
75/**
76 * @brief Termios device node for installed devices.
77 *
78 * @see rtems_termios_device_install().
79 */
80typedef struct rtems_termios_device_node {
81  rtems_chain_node                    node;
82  rtems_device_major_number           major;
83  rtems_device_minor_number           minor;
84  const rtems_termios_device_handler *handler;
85  const rtems_termios_device_flow    *flow;
86  rtems_termios_device_context       *context;
87  struct rtems_termios_tty           *tty;
88} rtems_termios_device_node;
89
90/*
91 * Variables associated with each termios instance.
92 * One structure for each hardware I/O device.
93 */
94typedef struct rtems_termios_tty {
95  /*
96   * Linked-list of active TERMIOS devices
97   */
98  struct rtems_termios_tty  *forw;
99  struct rtems_termios_tty  *back;
100
101  /*
102   * How many times has this device been opened
103   */
104  int    refcount;
105
106  /*
107   * This device
108   */
109  rtems_device_major_number  major;
110  rtems_device_minor_number  minor;
111
112  /*
113   * Mutual-exclusion semaphores
114   */
115  rtems_mutex isem;
116  rtems_mutex osem;
117
118  /*
119   * The canonical (cooked) character buffer
120   */
121  char    *cbuf;
122  int    ccount;
123  int    cindex;
124
125  /*
126   * Keep track of cursor (printhead) position
127   */
128  int    column;
129  int    read_start_column;
130
131  /*
132   * The ioctl settings
133   */
134  struct termios  termios;
135  rtems_interval  vtimeTicks;
136
137  /*
138   * Raw input character buffer
139   */
140  struct rtems_termios_rawbuf rawInBuf;
141  bool                        rawInBufSemaphoreWait;
142  rtems_interval              rawInBufSemaphoreTimeout;
143  rtems_interval              rawInBufSemaphoreFirstTimeout;
144  unsigned int                rawInBufDropped;  /* Statistics */
145
146  /*
147   * Raw output character buffer
148   */
149  struct rtems_termios_rawbuf rawOutBuf;
150  int  t_dqlen; /* count of characters dequeued from device */
151  enum {rob_idle, rob_busy, rob_wait }  rawOutBufState;
152
153  /*
154   * Callbacks to device-specific routines
155   */
156  rtems_termios_callbacks  device;
157
158  /**
159   * @brief Context for legacy devices using the callbacks.
160   */
161  rtems_termios_device_context legacy_device_context;
162
163  /**
164   * @brief The device handler.
165   */
166  rtems_termios_device_handler handler;
167
168  /**
169   * @brief The device flow control handler.
170   */
171  rtems_termios_device_flow flow;
172
173  volatile unsigned int    flow_ctrl;
174  unsigned int             lowwater,highwater;
175
176  /*
177   * I/O task IDs (for task-driven drivers)
178   */
179  rtems_id                rxTaskId;
180  rtems_id                txTaskId;
181  /*
182   * Information for the tx task how many characters have been dequeued.
183   */
184  int                     txTaskCharsDequeued;
185
186  /*
187   * line discipline related stuff
188   */
189  int t_line;   /* id of line discipline                       */
190  void *t_sc;   /* hook for discipline-specific data structure */
191
192  /*
193   * Wakeup callback variables
194   */
195  struct ttywakeup tty_snd;
196  struct ttywakeup tty_rcv;
197  bool             tty_rcvwakeup;
198
199  /**
200   * @brief Corresponding device node.
201   */
202  rtems_termios_device_node *device_node;
203
204  /**
205   * @brief Context for device driver.
206   */
207  rtems_termios_device_context *device_context;
208} rtems_termios_tty;
209
210/**
211 * @brief Installs a Termios device.
212 *
213 * The installed Termios device may be removed via unlink().
214 *
215 * @param[in] device_file The device file path.
216 * @param[in] handler The device handler.  It must be persistent throughout the
217 *   installed time of the device.
218 * @param[in] flow The device flow control handler.  The device flow control
219 *   handler are optional and may be @c NULL.  If present must be persistent
220 *   throughout the installed time of the device.
221 * @param[in] context The device context.  It must be persistent throughout the
222 *   installed time of the device.
223 *
224 * @retval RTEMS_SUCCESSFUL Successful operation.
225 * @retval RTEMS_NO_MEMORY Not enough memory to create a device node.
226 * @retval RTEMS_UNSATISFIED Creation of the device file failed.
227 * @retval RTEMS_INCORRECT_STATE Termios is not initialized.
228 *
229 * @see rtems_termios_get_device_context().
230 */
231rtems_status_code rtems_termios_device_install(
232  const char                         *device_file,
233  const rtems_termios_device_handler *handler,
234  const rtems_termios_device_flow    *flow,
235  rtems_termios_device_context       *context
236);
237
238/**
239 * @brief Returns the device context of an installed Termios device.
240 *
241 * @param[in] tty The Termios control.
242 */
243static inline void *rtems_termios_get_device_context(
244  const rtems_termios_tty *tty
245)
246{
247  return tty->device_context;
248}
249
250/**
251 * @brief Sets the best baud value in the Termios control.
252 *
253 * The valid Termios baud values are between 0 and 460800.  The Termios baud
254 * value is chosen which minimizes the difference to the value specified.
255 *
256 * @param[in] term The Termios attributes.
257 * @param[in] baud The current baud setting of the device.
258 */
259void rtems_termios_set_best_baud(
260  struct termios *term,
261  uint32_t        baud
262);
263
264struct rtems_termios_linesw {
265  int (*l_open) (struct rtems_termios_tty *tp);
266  int (*l_close)(struct rtems_termios_tty *tp);
267  int (*l_read )(struct rtems_termios_tty *tp,rtems_libio_rw_args_t *args);
268  int (*l_write)(struct rtems_termios_tty *tp,rtems_libio_rw_args_t *args);
269  int (*l_rint )(int c,struct rtems_termios_tty *tp);
270  int (*l_start)(struct rtems_termios_tty *tp,int len);
271  int (*l_ioctl)(struct rtems_termios_tty *tp,rtems_libio_ioctl_args_t *args);
272  int (*l_modem)(struct rtems_termios_tty *tp,int flags);
273};
274
275/*
276 * FIXME: this should move to termios.h!
277 */
278void rtems_termios_rxirq_occured(struct rtems_termios_tty *tty);
279
280/*
281 * FIXME: this should move to termios.h!
282 * put a string to output ring buffer
283 */
284void rtems_termios_puts (
285  const void               *buf,
286  size_t                    len,
287  struct rtems_termios_tty *tty
288);
289
290/*
291 * global hooks for line disciplines
292 */
293extern struct rtems_termios_linesw rtems_termios_linesw[];
294extern int   rtems_termios_nlinesw;
295
296#define TTYDISC   0    /* termios tty line discipline */
297#define TABLDISC  3    /* tablet discipline */
298#define SLIPDISC  4    /* serial IP discipline */
299#define PPPDISC   5    /* PPP discipline */
300#define MAXLDISC  8
301
302/* baudrate xxx integer type */
303typedef uint32_t rtems_termios_baud_t;
304
305/**
306 *  @brief RTEMS Termios Baud Table
307 */
308extern const rtems_assoc_t rtems_termios_baud_table [];
309
310/**
311 *  @brief Converts the Integral Baud value @a baud to the Termios Control Flag
312 *  Representation
313 *
314 *  @retval B0 Invalid baud value or a baud value of 0.
315 *  @retval other Baud constant according to @a baud.
316 */
317speed_t rtems_termios_number_to_baud(rtems_termios_baud_t baud);
318
319/**
320 *  @brief Converts the baud flags to an integral baud value.
321 *
322 *  @retval 0 Invalid baud value or a baud value of @c B0.
323 *  @retval other Integral baud value.
324 */
325rtems_termios_baud_t rtems_termios_baud_to_number(speed_t baud);
326
327/**
328 *  @brief Convert Bxxx Constant to Index
329 */
330int  rtems_termios_baud_to_index(rtems_termios_baud_t termios_baud);
331
332/**
333 * @brief Sets the initial @a baud in the Termios context @a tty.
334 *
335 * @retval 0 Successful operation.
336 * @retval -1 Invalid baud value.
337 */
338int rtems_termios_set_initial_baud(
339  struct rtems_termios_tty *tty,
340  rtems_termios_baud_t baud
341);
342
343/**
344 * @brief Termios kqueue() filter filesystem node handler
345 *
346 * Real implementation is provided by libbsd.
347 */
348int rtems_termios_kqfilter(
349  rtems_libio_t *iop,
350  struct knote  *kn
351);
352
353/**
354 * @brief Termios mmap() filter filesystem node handler
355 *
356 * Real implementation is provided by libbsd.
357 */
358int rtems_termios_mmap(
359  rtems_libio_t *iop,
360  void         **addr,
361  size_t         len,
362  int            prot,
363  off_t          off
364);
365
366/**
367 * @brief Termios poll() filesystem node handler.
368 *
369 * Real implementation is provided by libbsd.
370 */
371int rtems_termios_poll(
372  rtems_libio_t *iop,
373  int            events
374);
375
376#define RTEMS_IO_SNDWAKEUP _IOW('t', 11, struct ttywakeup ) /* send tty wakeup */
377#define RTEMS_IO_RCVWAKEUP _IOW('t', 12, struct ttywakeup ) /* recv tty wakeup */
378
379#define OLCUC           0x00000100      /* map lower case to upper case on output */
380#define IUCLC           0x00004000      /* map upper case to lower case on input */
381
382#define RTEMS_TERMIOS_NUMBER_BAUD_RATES 25
383
384#ifdef __cplusplus
385}
386#endif
387
388#endif  /* TERMIOSTYPES_H */
Note: See TracBrowser for help on using the repository browser.