source: rtems/cpukit/libcsupport/include/rtems/termiostypes.h @ 93726e5

4.115
Last change on this file since 93726e5 was 93726e5, checked in by Sebastian Huber <sebastian.huber@…>, on Jul 9, 2014 at 1:44:13 PM

termios: Add rtems_termios_set_best_baud()

  • Property mode set to 100644
File size: 12.7 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 <stdint.h>
24#include <termios.h>
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30/**
31 *  @defgroup TermiostypesSupport RTEMS Termios Device Support
32 *
33 *  @ingroup libcsupport
34 *
35 *  @brief RTEMS Termios Device Support Internal Data Structures
36 */
37
38/*
39 * Wakeup callback data structure
40 */
41struct ttywakeup {
42  void      (*sw_pfn)(struct termios *tty, void *arg);
43  void      *sw_arg;
44};
45
46/*
47 * Variables associated with the character buffer
48 */
49struct rtems_termios_rawbuf {
50  char *theBuf;
51  volatile unsigned int  Head;
52  volatile unsigned int  Tail;
53  volatile unsigned int  Size;
54  rtems_id    Semaphore;
55};
56
57typedef enum {
58  TERMIOS_POLLED,
59  TERMIOS_IRQ_DRIVEN,
60  TERMIOS_TASK_DRIVEN
61} rtems_termios_device_mode;
62
63struct rtems_termios_tty;
64
65/**
66 * @brief Termios device handler.
67 *
68 * @see rtems_termios_device_install().
69 */
70typedef struct {
71  /**
72   * @brief First open of this device.
73   *
74   * @param[in] tty The Termios control.
75   * @param[in] args The open/close arguments.  This is parameter provided to
76   *   support legacy drivers.  It must not be used by new drivers.
77   *
78   * @retval true Successful operation.
79   * @retval false Cannot open device.
80   *
81   * @see rtems_termios_get_device_context(), rtems_termios_set_best_baud() and
82   *   rtems_termios_get_termios().
83   */
84  bool (*first_open)(
85    struct rtems_termios_tty      *tty,
86    rtems_libio_open_close_args_t *args
87  );
88
89  /**
90   * @brief Last close of this device.
91   *
92   * @param[in] tty The Termios control.
93   * @param[in] args The open/close arguments.  This is parameter provided to
94   *   support legacy drivers.  It must not be used by new drivers.
95   *
96   * @see rtems_termios_get_device_context().
97   */
98  void (*last_close)(
99    struct rtems_termios_tty      *tty,
100    rtems_libio_open_close_args_t *args
101  );
102
103  /**
104   * @brief Polled read.
105   *
106   * In case mode is TERMIOS_IRQ_DRIVEN or TERMIOS_TASK_DRIVEN, then data is
107   * received via rtems_termios_enqueue_raw_characters().
108   *
109   * @param[in] tty The Termios control.
110   *
111   * @retval char The received data encoded as unsigned char.
112   * @retval -1 No data currently available.
113   *
114   * @see rtems_termios_get_device_context().
115   */
116  int (*poll_read)(struct rtems_termios_tty *tty);
117
118  /**
119   * @brief Polled write in case mode is TERMIOS_POLLED or write support
120   * otherwise.
121   *
122   * @param[in] tty The Termios control.
123   * @param[in] buf The output buffer.
124   * @param[in] len The output buffer length in characters.
125   *
126   * @see rtems_termios_get_device_context().
127   */
128  void (*write)(struct rtems_termios_tty *tty, const char *buf, size_t len);
129
130  /**
131   * @brief Set attributes after a Termios settings change.
132   *
133   * @param[in] tty The Termios control.
134   * @param[in] term The new Termios attributes.
135   *
136   * @retval true Successful operation.
137   * @retval false Invalid attributes.
138   *
139   * @see rtems_termios_get_device_context().
140   */
141  bool (*set_attributes)(
142    struct rtems_termios_tty *tty,
143    const struct termios     *term
144  );
145
146  /**
147   * @brief Indicate to stop remote transmitter.
148   *
149   * @param[in] tty The Termios control.
150   *
151   * @see rtems_termios_get_device_context().
152   */
153  void (*stop_remote_tx)(struct rtems_termios_tty *tty);
154
155  /**
156   * @brief Indicate to start remote transmitter.
157   *
158   * @param[in] tty The Termios control.
159   *
160   * @see rtems_termios_get_device_context().
161   */
162  void (*start_remote_tx)(struct rtems_termios_tty *tty);
163
164  /**
165   * @brief Termios device mode.
166   */
167  rtems_termios_device_mode mode;
168} rtems_termios_device_handler;
169
170/**
171 * @brief Termios device node for installed devices.
172 *
173 * @see rtems_termios_device_install().
174 */
175typedef struct rtems_termios_device_node {
176  rtems_chain_node                    node;
177  rtems_device_major_number           major;
178  rtems_device_minor_number           minor;
179  const rtems_termios_device_handler *handler;
180  void                               *context;
181  struct rtems_termios_tty           *tty;
182} rtems_termios_device_node;
183
184/*
185 * Variables associated with each termios instance.
186 * One structure for each hardware I/O device.
187 */
188typedef struct rtems_termios_tty {
189  /*
190   * Linked-list of active TERMIOS devices
191   */
192  struct rtems_termios_tty  *forw;
193  struct rtems_termios_tty  *back;
194
195  /*
196   * How many times has this device been opened
197   */
198  int    refcount;
199
200  /*
201   * This device
202   */
203  rtems_device_major_number  major;
204  rtems_device_minor_number  minor;
205
206  /*
207   * Mutual-exclusion semaphores
208   */
209  rtems_id  isem;
210  rtems_id  osem;
211
212  /*
213   * The canonical (cooked) character buffer
214   */
215  char    *cbuf;
216  int    ccount;
217  int    cindex;
218
219  /*
220   * Keep track of cursor (printhead) position
221   */
222  int    column;
223  int    read_start_column;
224
225  /*
226   * The ioctl settings
227   */
228  struct termios  termios;
229  rtems_interval  vtimeTicks;
230
231  /*
232   * Raw input character buffer
233   */
234  struct rtems_termios_rawbuf rawInBuf;
235  uint32_t                    rawInBufSemaphoreOptions;
236  rtems_interval              rawInBufSemaphoreTimeout;
237  rtems_interval              rawInBufSemaphoreFirstTimeout;
238  unsigned int                rawInBufDropped;  /* Statistics */
239
240  /*
241   * Raw output character buffer
242   */
243  struct rtems_termios_rawbuf rawOutBuf;
244  int  t_dqlen; /* count of characters dequeued from device */
245  enum {rob_idle, rob_busy, rob_wait }  rawOutBufState;
246
247  /*
248   * Callbacks to device-specific routines
249   */
250  rtems_termios_callbacks  device;
251
252  /**
253   * @brief The device handler.
254   */
255  rtems_termios_device_handler handler;
256
257  volatile unsigned int    flow_ctrl;
258  unsigned int             lowwater,highwater;
259
260  /*
261   * I/O task IDs (for task-driven drivers)
262   */
263  rtems_id                rxTaskId;
264  rtems_id                txTaskId;
265
266  /*
267   * line discipline related stuff
268   */
269  int t_line;   /* id of line discipline                       */
270  void *t_sc;   /* hook for discipline-specific data structure */
271
272  /*
273   * Wakeup callback variables
274   */
275  struct ttywakeup tty_snd;
276  struct ttywakeup tty_rcv;
277  int              tty_rcvwakeup;
278
279  rtems_interrupt_lock interrupt_lock;
280
281  /**
282   * @brief Corresponding device node.
283   */
284  rtems_termios_device_node *device_node;
285
286  /**
287   * @brief Context for device driver.
288   *
289   * @see rtems_termios_get_device_context().
290   */
291  void *device_context;
292} rtems_termios_tty;
293
294/**
295 * @brief Installs a Termios device.
296 *
297 * @param[in] device_file If not @c NULL, then a device file for the specified
298 * major and minor number will be created.
299 * @param[in] major The device major number of the corresponding device driver.
300 * @param[in] minor The device minor number of the corresponding device driver.
301 * @param[in] handler The device handler.  It must be persistent throughout the
302 *   installed time of the device.
303 * @param[in] context The device context.  It must be persistent throughout the
304 *   installed time of the device.
305 *
306 * @retval RTEMS_SUCCESSFUL Successful operation.
307 * @retval RTEMS_NO_MEMORY Not enough memory to create a device node.
308 * @retval RTEMS_UNSATISFIED Creation of the device file failed.
309 * @retval RTEMS_RESOURCE_IN_USE There exists a device node for this major and
310 * minor number pair.
311 * @retval RTEMS_INCORRECT_STATE Termios is not initialized.
312 *
313 * @see rtems_termios_device_remove(), rtems_termios_device_open(),
314 * rtems_termios_device_close() and rtems_termios_get_device_context().
315 */
316rtems_status_code rtems_termios_device_install(
317  const char                         *device_file,
318  rtems_device_major_number           major,
319  rtems_device_minor_number           minor,
320  const rtems_termios_device_handler *handler,
321  void                               *context
322);
323
324/**
325 * @brief Removes a Termios device.
326 *
327 * @param[in] device_file If not @c NULL, then the device file to remove.
328 * @param[in] major The device major number of the corresponding device driver.
329 * @param[in] minor The device minor number of the corresponding device driver.
330 *
331 * @retval RTEMS_SUCCESSFUL Successful operation.
332 * @retval RTEMS_INVALID_ID There is no device installed with this major and
333 * minor number pair.
334 * @retval RTEMS_RESOURCE_IN_USE This device is currently in use.
335 * @retval RTEMS_UNSATISFIED Removal of the device file failed.
336 * @retval RTEMS_INCORRECT_STATE Termios is not initialized.
337 *
338 * @see rtems_termios_device_install().
339 */
340rtems_status_code rtems_termios_device_remove(
341  const char                *device_file,
342  rtems_device_major_number  major,
343  rtems_device_minor_number  minor
344);
345
346/**
347 * @brief Opens an installed Termios device.
348 *
349 * @see rtems_termios_device_install().
350 */
351rtems_status_code rtems_termios_device_open(
352  rtems_device_major_number  major,
353  rtems_device_minor_number  minor,
354  void                      *arg
355);
356
357/**
358 * @brief Closes an installed Termios device.
359 *
360 * @retval RTEMS_SUCCESSFUL Successful operation.
361 *
362 * @see rtems_termios_device_install().
363 */
364rtems_status_code rtems_termios_device_close(void *arg);
365
366/**
367 * @brief Returns the device context of an installed Termios device.
368 */
369RTEMS_INLINE_ROUTINE void *rtems_termios_get_device_context(
370  const rtems_termios_tty *tty
371)
372{
373  return tty->device_context;
374}
375
376/**
377 * @brief Returns the Termios structure.
378 *
379 * It can be used for example in the first open handler to adjust or obtain the
380 * initial attributes.
381 */
382RTEMS_INLINE_ROUTINE struct termios *rtems_termios_get_termios(
383  rtems_termios_tty *tty
384)
385{
386  return &tty->termios;
387}
388
389/**
390 * @brief Sets the best baud value in the Termios control.
391 *
392 * The valid Termios baud values are between 0 and 460800.  The Termios baud
393 * value is chosen which minimizes the difference to the value specified.
394 *
395 * @param[in] tty The Termios control.
396 * @param[in] baud The current baud setting of the device.
397 */
398void rtems_termios_set_best_baud(
399  rtems_termios_tty *tty,
400  uint32_t           baud
401);
402
403struct rtems_termios_linesw {
404  int (*l_open) (struct rtems_termios_tty *tp);
405  int (*l_close)(struct rtems_termios_tty *tp);
406  int (*l_read )(struct rtems_termios_tty *tp,rtems_libio_rw_args_t *args);
407  int (*l_write)(struct rtems_termios_tty *tp,rtems_libio_rw_args_t *args);
408  int (*l_rint )(int c,struct rtems_termios_tty *tp);
409  int (*l_start)(struct rtems_termios_tty *tp);
410  int (*l_ioctl)(struct rtems_termios_tty *tp,rtems_libio_ioctl_args_t *args);
411  int (*l_modem)(struct rtems_termios_tty *tp,int flags);
412};
413
414/*
415 * FIXME: this should move to termios.h!
416 */
417void rtems_termios_rxirq_occured(struct rtems_termios_tty *tty);
418
419/*
420 * FIXME: this should move to termios.h!
421 * put a string to output ring buffer
422 */
423void rtems_termios_puts (
424  const void               *buf,
425  size_t                    len,
426  struct rtems_termios_tty *tty
427);
428
429/*
430 * global hooks for line disciplines
431 */
432extern struct rtems_termios_linesw rtems_termios_linesw[];
433extern int   rtems_termios_nlinesw;
434
435#define TTYDISC   0    /* termios tty line discipline */
436#define TABLDISC  3    /* tablet discipline */
437#define SLIPDISC  4    /* serial IP discipline */
438#define PPPDISC   5    /* PPP discipline */
439#define MAXLDISC  8
440
441/* baudrate xxx integer type */
442typedef uint32_t rtems_termios_baud_t;
443
444/**
445 *  @brief RTEMS Termios Baud Table
446 */
447extern const rtems_assoc_t rtems_termios_baud_table [];
448
449/**
450 *  @brief Converts the Integral Baud value @a baud to the Termios Control Flag
451 *  Representation
452 *
453 *  @retval B0 Invalid baud value or a baud value of 0.
454 *  @retval other Baud constant according to @a baud.
455 */
456tcflag_t rtems_termios_number_to_baud(rtems_termios_baud_t baud);
457
458/**
459 *  @brief Convert Baud Part of Termios control flags to an integral Baud Value
460 *
461 *  There is no need to mask the @a c_cflag with @c CBAUD.
462 *
463 *  @retval 0 Invalid baud value or a baud value of @c B0.
464 *  @retval other Integral baud value.
465 */
466rtems_termios_baud_t rtems_termios_baud_to_number(tcflag_t c_cflag);
467
468/**
469 *  @brief Convert Bxxx Constant to Index
470 */
471int  rtems_termios_baud_to_index(rtems_termios_baud_t termios_baud);
472
473/**
474 * @brief Sets the initial @a baud in the Termios context @a tty.
475 *
476 * @retval 0 Successful operation.
477 * @retval -1 Invalid baud value.
478 */
479int rtems_termios_set_initial_baud(
480  struct rtems_termios_tty *tty,
481  rtems_termios_baud_t baud
482);
483
484#define rtems_termios_interrupt_lock_acquire(tty, level) \
485  rtems_interrupt_lock_acquire(&tty->interrupt_lock, level)
486
487#define rtems_termios_interrupt_lock_release(tty, level) \
488  rtems_interrupt_lock_release(&tty->interrupt_lock, level)
489
490#ifdef __cplusplus
491}
492#endif
493
494#endif  /* TERMIOSTYPES_H */
Note: See TracBrowser for help on using the repository browser.