Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:14

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * RTEMS termios device support internal data structures
0007  */
0008 
0009 /*
0010  *  COPYRIGHT (c) 1989-2011.
0011  *  On-Line Applications Research Corporation (OAR).
0012  *
0013  * Redistribution and use in source and binary forms, with or without
0014  * modification, are permitted provided that the following conditions
0015  * are met:
0016  * 1. Redistributions of source code must retain the above copyright
0017  *    notice, this list of conditions and the following disclaimer.
0018  * 2. Redistributions in binary form must reproduce the above copyright
0019  *    notice, this list of conditions and the following disclaimer in the
0020  *    documentation and/or other materials provided with the distribution.
0021  *
0022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0023  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0025  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0026  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0027  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0028  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0029  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0030  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0032  * POSSIBILITY OF SUCH DAMAGE.
0033  */
0034 
0035 #ifndef  __TERMIOSTYPES_H
0036 #define  __TERMIOSTYPES_H
0037 
0038 #include <rtems.h>
0039 #include <rtems/libio.h>
0040 #include <rtems/assoc.h>
0041 #include <rtems/chain.h>
0042 #include <rtems/termiosdevice.h>
0043 #include <stdint.h>
0044 #include <termios.h>
0045 
0046 #ifdef __cplusplus
0047 extern "C" {
0048 #endif
0049 
0050 /**
0051  * @addtogroup TermiostypesSupport
0052  *
0053  * @{
0054  */
0055 
0056 /*
0057  * Wakeup callback data structure
0058  */
0059 struct ttywakeup {
0060   void      (*sw_pfn)(struct termios *tty, void *arg);
0061   void      *sw_arg;
0062 };
0063 
0064 /*
0065  * Variables associated with the character buffer
0066  */
0067 struct rtems_termios_rawbuf {
0068   char *theBuf;
0069   volatile unsigned int  Head;
0070   volatile unsigned int  Tail;
0071   volatile unsigned int  Size;
0072   rtems_binary_semaphore Semaphore;
0073 };
0074 
0075 /**
0076  * @brief Termios device node for installed devices.
0077  *
0078  * @see rtems_termios_device_install().
0079  */
0080 typedef struct rtems_termios_device_node {
0081   rtems_chain_node                    node;
0082   rtems_device_major_number           major;
0083   rtems_device_minor_number           minor;
0084   const rtems_termios_device_handler *handler;
0085   const rtems_termios_device_flow    *flow;
0086   rtems_termios_device_context       *context;
0087   struct rtems_termios_tty           *tty;
0088 } rtems_termios_device_node;
0089 
0090 /*
0091  * Variables associated with each termios instance.
0092  * One structure for each hardware I/O device.
0093  */
0094 typedef struct rtems_termios_tty {
0095   /*
0096    * Linked-list of active TERMIOS devices
0097    */
0098   struct rtems_termios_tty  *forw;
0099   struct rtems_termios_tty  *back;
0100 
0101   /*
0102    * How many times has this device been opened
0103    */
0104   int    refcount;
0105 
0106   /*
0107    * This device
0108    */
0109   rtems_device_major_number  major;
0110   rtems_device_minor_number  minor;
0111 
0112   /*
0113    * Mutual-exclusion semaphores
0114    */
0115   rtems_mutex isem;
0116   rtems_mutex osem;
0117 
0118   /*
0119    * The canonical (cooked) character buffer
0120    */
0121   char    *cbuf;
0122   int    ccount;
0123   int    cindex;
0124 
0125   /*
0126    * Keep track of cursor (printhead) position
0127    */
0128   int    column;
0129   int    read_start_column;
0130 
0131   /*
0132    * The ioctl settings
0133    */
0134   struct termios  termios;
0135   rtems_interval  vtimeTicks;
0136 
0137   /*
0138    * Raw input character buffer
0139    */
0140   struct rtems_termios_rawbuf rawInBuf;
0141   bool                        rawInBufSemaphoreWait;
0142   rtems_interval              rawInBufSemaphoreTimeout;
0143   rtems_interval              rawInBufSemaphoreFirstTimeout;
0144   unsigned int                rawInBufDropped;  /* Statistics */
0145 
0146   /*
0147    * Raw output character buffer
0148    */
0149   struct rtems_termios_rawbuf rawOutBuf;
0150   int  t_dqlen; /* count of characters dequeued from device */
0151   enum {rob_idle, rob_busy, rob_wait }  rawOutBufState;
0152 
0153   /*
0154    * Callbacks to device-specific routines
0155    */
0156   rtems_termios_callbacks  device;
0157 
0158   /**
0159    * @brief Context for legacy devices using the callbacks.
0160    */
0161   rtems_termios_device_context legacy_device_context;
0162 
0163   /**
0164    * @brief The device handler.
0165    */
0166   rtems_termios_device_handler handler;
0167 
0168   /**
0169    * @brief The device flow control handler.
0170    */
0171   rtems_termios_device_flow flow;
0172 
0173   volatile unsigned int    flow_ctrl;
0174   unsigned int             lowwater,highwater;
0175 
0176   /*
0177    * I/O task IDs (for task-driven drivers)
0178    */
0179   rtems_id                rxTaskId;
0180   rtems_id                txTaskId;
0181   /*
0182    * Information for the tx task how many characters have been dequeued.
0183    */
0184   int                     txTaskCharsDequeued;
0185 
0186   /*
0187    * line discipline related stuff
0188    */
0189   int t_line;   /* id of line discipline                       */
0190   void *t_sc;   /* hook for discipline-specific data structure */
0191 
0192   /*
0193    * Wakeup callback variables
0194    */
0195   struct ttywakeup tty_snd;
0196   struct ttywakeup tty_rcv;
0197   bool             tty_rcvwakeup;
0198 
0199   /**
0200    * @brief Corresponding device node.
0201    */
0202   rtems_termios_device_node *device_node;
0203 
0204   /**
0205    * @brief Context for device driver.
0206    */
0207   rtems_termios_device_context *device_context;
0208 } rtems_termios_tty;
0209 
0210 /**
0211  * @brief Installs a Termios device.
0212  *
0213  * The installed Termios device may be removed via unlink().
0214  *
0215  * @param[in] device_file The device file path.
0216  * @param[in] handler The device handler.  It must be persistent throughout the
0217  *   installed time of the device.
0218  * @param[in] flow The device flow control handler.  The device flow control
0219  *   handler are optional and may be @c NULL.  If present must be persistent
0220  *   throughout the installed time of the device.
0221  * @param[in] context The device context.  It must be persistent throughout the
0222  *   installed time of the device.
0223  *
0224  * @retval RTEMS_SUCCESSFUL Successful operation.
0225  * @retval RTEMS_NO_MEMORY Not enough memory to create a device node.
0226  * @retval RTEMS_UNSATISFIED Creation of the device file failed.
0227  * @retval RTEMS_INCORRECT_STATE Termios is not initialized.
0228  *
0229  * @see rtems_termios_get_device_context().
0230  */
0231 rtems_status_code rtems_termios_device_install(
0232   const char                         *device_file,
0233   const rtems_termios_device_handler *handler,
0234   const rtems_termios_device_flow    *flow,
0235   rtems_termios_device_context       *context
0236 );
0237 
0238 /**
0239  * @brief Returns the device context of an installed Termios device.
0240  *
0241  * @param[in] tty The Termios control.
0242  */
0243 static inline void *rtems_termios_get_device_context(
0244   const rtems_termios_tty *tty
0245 )
0246 {
0247   return tty->device_context;
0248 }
0249 
0250 /**
0251  * @brief Sets the best baud value in the Termios control.
0252  *
0253  * The valid Termios baud values are between 0 and 460800.  The Termios baud
0254  * value is chosen which minimizes the difference to the value specified.
0255  *
0256  * @param[in] term The Termios attributes.
0257  * @param[in] baud The current baud setting of the device.
0258  */
0259 void rtems_termios_set_best_baud(
0260   struct termios *term,
0261   uint32_t        baud
0262 );
0263 
0264 struct rtems_termios_linesw {
0265   int (*l_open) (struct rtems_termios_tty *tp);
0266   int (*l_close)(struct rtems_termios_tty *tp);
0267   int (*l_read )(struct rtems_termios_tty *tp,rtems_libio_rw_args_t *args);
0268   int (*l_write)(struct rtems_termios_tty *tp,rtems_libio_rw_args_t *args);
0269   int (*l_rint )(int c,struct rtems_termios_tty *tp);
0270   int (*l_start)(struct rtems_termios_tty *tp,int len);
0271   int (*l_ioctl)(struct rtems_termios_tty *tp,rtems_libio_ioctl_args_t *args);
0272   int (*l_modem)(struct rtems_termios_tty *tp,int flags);
0273 };
0274 
0275 /*
0276  * FIXME: this should move to termios.h!
0277  */
0278 void rtems_termios_rxirq_occured(struct rtems_termios_tty *tty);
0279 
0280 /*
0281  * FIXME: this should move to termios.h!
0282  * put a string to output ring buffer
0283  */
0284 void rtems_termios_puts (
0285   const void               *buf,
0286   size_t                    len,
0287   struct rtems_termios_tty *tty
0288 );
0289 
0290 /*
0291  * global hooks for line disciplines
0292  */
0293 extern struct rtems_termios_linesw rtems_termios_linesw[];
0294 extern int   rtems_termios_nlinesw;
0295 
0296 #define TTYDISC   0    /* termios tty line discipline */
0297 #define TABLDISC  3    /* tablet discipline */
0298 #define SLIPDISC  4    /* serial IP discipline */
0299 #define PPPDISC   5    /* PPP discipline */
0300 #define MAXLDISC  8
0301 
0302 /* baudrate xxx integer type */
0303 typedef uint32_t rtems_termios_baud_t;
0304 
0305 /**
0306  *  @brief RTEMS Termios Baud Table
0307  */
0308 extern const rtems_assoc_t rtems_termios_baud_table [];
0309 
0310 /**
0311  *  @brief Converts the Integral Baud value @a baud to the Termios Control Flag
0312  *  Representation
0313  *
0314  *  @retval B0 Invalid baud value or a baud value of 0.
0315  *  @retval other Baud constant according to @a baud.
0316  */
0317 speed_t rtems_termios_number_to_baud(rtems_termios_baud_t baud);
0318 
0319 /**
0320  *  @brief Converts the baud flags to an integral baud value.
0321  *
0322  *  @retval 0 Invalid baud value or a baud value of @c B0.
0323  *  @retval other Integral baud value.
0324  */
0325 rtems_termios_baud_t rtems_termios_baud_to_number(speed_t baud);
0326 
0327 /**
0328  *  @brief Convert Bxxx Constant to Index
0329  */
0330 int  rtems_termios_baud_to_index(rtems_termios_baud_t termios_baud);
0331 
0332 /**
0333  * @brief Sets the initial @a baud in the Termios context @a tty.
0334  *
0335  * @retval 0 Successful operation.
0336  * @retval -1 Invalid baud value.
0337  */
0338 int rtems_termios_set_initial_baud(
0339   struct rtems_termios_tty *tty,
0340   rtems_termios_baud_t baud
0341 );
0342 
0343 /**
0344  * @brief Termios kqueue() filter filesystem node handler
0345  *
0346  * Real implementation is provided by libbsd.
0347  */
0348 int rtems_termios_kqfilter(
0349   rtems_libio_t *iop,
0350   struct knote  *kn
0351 );
0352 
0353 /**
0354  * @brief Termios mmap() filter filesystem node handler
0355  *
0356  * Real implementation is provided by libbsd.
0357  */
0358 int rtems_termios_mmap(
0359   rtems_libio_t *iop,
0360   void         **addr,
0361   size_t         len,
0362   int            prot,
0363   off_t          off
0364 );
0365 
0366 /**
0367  * @brief Termios poll() filesystem node handler.
0368  *
0369  * Real implementation is provided by libbsd.
0370  */
0371 int rtems_termios_poll(
0372   rtems_libio_t *iop,
0373   int            events
0374 );
0375 
0376 #define RTEMS_IO_SNDWAKEUP _IOW('t', 11, struct ttywakeup ) /* send tty wakeup */
0377 #define RTEMS_IO_RCVWAKEUP _IOW('t', 12, struct ttywakeup ) /* recv tty wakeup */
0378 
0379 #define OLCUC       0x00000100  /* map lower case to upper case on output */
0380 #define IUCLC       0x00004000  /* map upper case to lower case on input */
0381 
0382 #define RTEMS_TERMIOS_NUMBER_BAUD_RATES 25
0383 
0384 #ifdef __cplusplus
0385 }
0386 #endif
0387 
0388 #endif  /* TERMIOSTYPES_H */