1 /** @file
2     Macros and declarations for terminal oriented ioctls and
3     I/O discipline.
4 
5     Copyright (c) 2016, Daryl McDaniel. All rights reserved.<BR>
6     Copyright (c) 2012 - 2014, Intel Corporation. All rights reserved.<BR>
7     This program and the accompanying materials are licensed and made available under
8     the terms and conditions of the BSD License that accompanies this distribution.
9     The full text of the license may be found at
10     http://opensource.org/licenses/bsd-license.
11 
12     THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13     WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 
15  * Copyright (c) 1988, 1989, 1993, 1994
16  *  The Regents of the University of California.  All rights reserved.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  * 3. Neither the name of the University nor the names of its contributors
27  *    may be used to endorse or promote products derived from this software
28  *    without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  *
42  *  @(#)termios.h 8.3 (Berkeley) 3/28/94
43     NetBSD: termios.h,v 1.29 2005/12/11 12:25:21 christos Exp
44 **/
45 #ifndef _SYS_TERMIOS_H_
46 #define _SYS_TERMIOS_H_
47 
48 #include <sys/ansi.h>
49 #include <sys/featuretest.h>
50 
51 /*  Special Control Characters
52  *
53  * Index into c_cc[] character array.
54  */
55 typedef enum {
56 /* Name     Enabled by */
57   VTABLEN,  /* OXTABS - Length between TAB stops. */
58   VEOF,     /* ICANON */
59   VEOL,     /* ICANON */
60   VERASE,   /* ICANON */
61   VKILL,    /* ICANON */
62   VINTR,    /* ISIG */
63   VQUIT,    /* ISIG */
64   VMIN,     /* !ICANON */
65   VTIME,    /* !ICANON */
66 
67   /* Extensions from BSD and POSIX -- Not yet implemented for UEFI */
68   VWERASE,   /* IEXTEN, ICANON -- Erase the WORD to the left of the cursor */
69   VREPRINT,  /* IEXTEN, ICANON -- Re-draw the current line (input buffer) */
70   VLNEXT,    /* IEXTEN, ICANON -- Input the next character literally */
71   VDISCARD,  /* IEXTEN -- Toggle.  Discards output display until toggled. */
72 
73   /* NCCS must always be the last member of this enum. */
74   NCCS      /* Number of control characters in c_cc[]  */
75 } CCC_INDEX;
76 
77 #define _POSIX_VDISABLE ((unsigned char)'\377')
78 
79 #define CCEQ(val, c)  (c == val ? val != _POSIX_VDISABLE : 0)
80 
81 /*
82  * Input flags - software input processing
83   c_iflag
84 */
85 #define   INLCR     0x0001  /* map NL into CR */
86 #define   IGNCR     0x0002  /* ignore CR */
87 #define   ICRNL     0x0004  /* map CR to NL (ala CRMOD) */
88 #define   IGNSPEC   0x0008  /* Ignore function, control, and other non-printing special keys. */
89 #ifdef  HAVE_DA_SERIAL
90   #define ISTRIP    0x0010  /* strip 8th bit off chars */
91   #define IGNBRK    0x0020  /* ignore BREAK condition */
92   #define BRKINT    0x0040  /* map BREAK to SIGINTR */
93   #define IRESRV1   0x0080
94   #define IGNPAR    0x0100  /* ignore (discard) parity errors */
95   #define PARMRK    0x0200  /* mark parity and framing errors */
96   #define INPCK     0x0400  /* enable checking of parity errors */
97   #define IXON      0x0800  /* enable output flow control */
98   #define IXOFF     0x1000  /* enable input flow control */
99   #define IXANY     0x2000  /* any char will restart after stop */
100 #endif  /* HAVE_DA_SERIAL */
101 
102 /*
103  * Output flags - software output processing
104   c_oflag
105  */
106 #define OPOST     0x0001  /* enable following output processing */
107 #define ONLCR     0x0002  /* map NL to CR-NL (ala CRMOD) */
108 #define OXTABS    0x0004  /* expand tabs to spaces */
109 #define ONOEOT    0x0008  /* discard EOT's (^D) on output */
110 #define OCRNL     0x0010  /* map CR to NL */
111 #define ONOCR     0x0020  /* discard CR's when on column 0 */
112 #define ONLRET    0x0040  /* move to column 0 on CR */
113 #define OCTRL     0x0080  /* Map control characters to the sequence ^C */
114 
115 /*
116  * Control flags - hardware control of terminal
117   c_cflag
118  */
119 #ifdef  HAVE_DA_SERIAL
120   #define CIGNORE   0x0001      /* ignore control flags */
121   #define CSIZE     0x0300      /* character size mask */
122   #define     CS5       0x0000      /* 5 bits (pseudo) */
123   #define     CS6       0x0100      /* 6 bits */
124   #define     CS7       0x0200      /* 7 bits */
125   #define     CS8       0x0300      /* 8 bits */
126   #define CSTOPB    0x0400      /* send 2 stop bits, else 1 */
127   #define CREAD     0x0800      /* enable receiver */
128   #define PARENB    0x1000      /* parity enable */
129   #define PARODD    0x2000      /* odd parity, else even */
130   #define HUPCL     0x4000      /* hang up on last close */
131   #define CLOCAL    0x8000      /* ignore modem status lines */
132 #endif
133 
134 
135 /*
136  * "Local" flags - dumping ground for other state
137  *
138  * Warning: some flags in this structure begin with
139  * the letter "I" and look like they belong in the
140  * input flag.
141  */
142 #define ECHO      0x0001    /* enable echoing */
143 #define ECHOE     0x0002    /* visually erase chars */
144 #define ECHOK     0x0004    /* echo NL after line kill */
145 #define ECHONL    0x0008    /* echo NL even if ECHO is off */
146 #define ISIG      0x0010    /* enable signals INTR, QUIT, [D]SUSP */
147 #define ICANON    0x0020    /* canonicalize input lines */
148 #define IEXTEN    0x0040    /* enable Extensions */
149 #define SKIP_1    0x0100    /* Currently unused */
150 #define TOSTOP    0x0200    /* stop background jobs on output */
151 #define PENDIN    0x0400    /* re-echo input buffer at next read */
152 #define NOFLSH    0x0800    /* don't flush output on signal */
153 #define FLUSHO    0x1000    /* output being flushed (state) */
154 
155 typedef INT8    cc_t;
156 typedef UINT16  tcflag_t;
157 typedef UINT32  speed_t;
158 
159 struct termios {
160   INT32     c_ispeed;   /* input speed    - Use a signed type instead of speed_t */
161   INT32     c_ospeed;   /* output speed   - to ease integer promotion when used. */
162   tcflag_t  c_iflag;    /* input flags */
163   tcflag_t  c_oflag;    /* output flags */
164   tcflag_t  c_cflag;    /* control flags */
165   tcflag_t  c_lflag;    /* local flags */
166   cc_t      c_cc[NCCS]; /* control chars */
167 };
168 
169 /*
170  * Commands passed to tcsetattr() for setting the termios structure.
171  */
172 #define TCSANOW     0       /* make change immediate */
173 #define TCSADRAIN   1       /* drain output, then change */
174 #define TCSAFLUSH   2       /* drain output, flush input */
175 #define TCSASOFT    0x10    /* flag - don't alter h.w. state */
176 
177 /*
178  * Standard speeds
179  */
180 #define B0            0
181 #define B50          50
182 #define B75          75
183 #define B110        110
184 #define B134        134
185 #define B150        150
186 #define B200        200
187 #define B300        300
188 #define B600        600
189 #define B1200      1200
190 #define B1800      1800
191 #define B2400      2400
192 #define B4800      4800
193 #define B9600      9600
194 #define B19200    19200
195 #define B38400    38400
196 
197 // Extended speed definitions
198 #define B7200      7200
199 #define B14400    14400
200 #define B28800    28800
201 #define B57600    57600
202 #define B76800    76800
203 #define B115200  115200
204 #define B230400  230400
205 #define B460800  460800
206 #define B921600  921600
207 
208 #define TCIFLUSH  1
209 #define TCOFLUSH  2
210 #define TCIOFLUSH 3
211 #define TCOOFF    1
212 #define TCOON     2
213 #define TCIOFF    3
214 #define TCION     4
215 
216 #include <sys/EfiCdefs.h>
217 
218 __BEGIN_DECLS
219 
220 /** Get input baud rate.
221 
222     Extracts the input baud rate from the termios structure pointed to by the
223     pTermios argument.
224 
225     @param[in]  pTermios  A pointer to the termios structure from which to extract
226                           the input baud rate.
227 
228     @return The value of the input speed is returned exactly as it is contained
229             in the termios structure, without interpretation.
230 **/
231 speed_t cfgetispeed (const struct termios *);
232 
233 /** Get output baud rate.
234 
235     Extracts the output baud rate from the termios structure pointed to by the
236     pTermios argument.
237 
238     @param[in]  pTermios  A pointer to the termios structure from which to extract
239                           the output baud rate.
240 
241     @return The value of the output speed is returned exactly as it is contained
242             in the termios structure, without interpretation.
243 **/
244 speed_t cfgetospeed (const struct termios *);
245 
246 /** Set input baud rate.
247 
248     Replaces the input baud rate, in the termios structure pointed to by the
249     pTermios argument, with the value of NewSpeed.
250 
251     @param[out]   pTermios  A pointer to the termios structure into which to set
252                             the input baud rate.
253     @param[in]    NewSpeed  The new input baud rate.
254 
255     @retval 0     The operation completed successfully.
256     @retval -1    An error occured and errno is set to indicate the error.
257                     * EINVAL - The value of NewSpeed is outside the range of
258                       possible speed values as specified in <sys/termios.h>.
259 **/
260 int     cfsetispeed (struct termios *, speed_t);
261 
262 /** Set output baud rate.
263 
264     Replaces the output baud rate, in the termios structure pointed to by the
265     pTermios argument, with the value of NewSpeed.
266 
267     @param[out]   pTermios  A pointer to the termios structure into which to set
268                             the output baud rate.
269     @param[in]    NewSpeed  The new output baud rate.
270 
271     @retval 0     The operation completed successfully.
272     @retval -1    An error occured and errno is set to indicate the error.
273                     * EINVAL - The value of NewSpeed is outside the range of
274                       possible speed values as specified in <sys/termios.h>.
275 **/
276 int     cfsetospeed (struct termios *, speed_t);
277 
278 /** Get the parameters associated with an interactive IO device.
279 
280     Get the parameters associated with the device referred to by
281     fd and store them into the termios structure referenced by pTermios.
282 
283     @param[in]    fd        The file descriptor for an open interactive IO device.
284     @param[out]   pTermios  A pointer to a termios structure into which to store
285                             attributes of the interactive IO device.
286 
287     @retval 0     The operation completed successfully.
288     @retval -1    An error occured and errno is set to indicate the error.
289                     * EBADF - The fd argument is not a valid file descriptor.
290                     * ENOTTY - The file associated with fd is not an interactive IO device.
291 **/
292 int     tcgetattr   (int fd, struct termios *pTermios);
293 
294 /** Set the parameters associated with an interactive IO device.
295 
296     Set the parameters associated with the device referred to by
297     fd to the values in the termios structure referenced by pTermios.
298 
299     Behavior is modified by the value of the OptAct parameter:
300       * TCSANOW: The change shall occur immediately.
301       * TCSADRAIN: The change shall occur after all output written to fd is
302         transmitted. This action should be used when changing parameters which
303         affect output.
304       * TCSAFLUSH: The change shall occur after all output written to fd is
305         transmitted, and all input so far received but not read shall be
306         discarded before the change is made.
307 
308     @param[in]  fd        The file descriptor for an open interactive IO device.
309     @param[in]  OptAct    Currently has no effect.
310     @param[in]  pTermios  A pointer to a termios structure into which to retrieve
311                           attributes to set in the interactive IO device.
312 
313     @retval 0     The operation completed successfully.
314     @retval -1    An error occured and errno is set to indicate the error.
315                     * EBADF - The fd argument is not a valid file descriptor.
316                     * ENOTTY - The file associated with fd is not an interactive IO device.
317 **/
318 int     tcsetattr   (int fd, int OptAct, const struct termios *pTermios);
319 
320 /** Transmit pending output.
321 
322 
323     @param[in]  fd        The file descriptor for an open interactive IO device.
324 
325     @retval 0     The operation completed successfully.
326     @retval -1    An error occured and errno is set to indicate the error.
327                     * EBADF - The fd argument is not a valid file descriptor.
328                     * ENOTTY - The file associated with fd is not an interactive IO device.
329                     * EINTR - A signal interrupted tcdrain().
330                     * ENOTSUP - This function is not supported.
331 **/
332 int     tcdrain     (int fd);
333 
334 /** Suspend or restart the transmission or reception of data.
335 
336     This function will suspend or resume transmission or reception of data on
337     the file referred to by fd, depending on the value of Action.
338 
339     @param[in]  fd        The file descriptor of an open interactive IO device (terminal).
340     @param[in]  Action    The action to be performed:
341                             * TCOOFF - Suspend output.
342                             * TCOON - Resume suspended output.
343                             * TCIOFF - If fd refers to an IIO device, transmit a
344                                       STOP character, which is intended to cause the
345                                       terminal device to stop transmitting data.
346                             * TCION - If fd refers to an IIO device, transmit a
347                                       START character, which is intended to cause the
348                                       terminal device to start transmitting data.
349 
350     @retval 0     The operation completed successfully.
351     @retval -1    An error occured and errno is set to indicate the error.
352                     * EBADF - The fd argument is not a valid file descriptor.
353                     * ENOTTY - The file associated with fd is not an interactive IO device.
354                     * EINVAL - The Action argument is not a supported value.
355                     * ENOTSUP - This function is not supported.
356 **/
357 int     tcflow      (int fd, int Action);
358 
359 /** Discard non-transmitted output data, non-read input data, or both.
360 
361 
362     @param[in]  fd              The file descriptor for an open interactive IO device.
363     @param[in]  QueueSelector   The IO queue to be affected:
364                                   * TCIFLUSH - If fd refers to a device open for input, flush
365                                     pending input.  Otherwise error EINVAL.
366                                   * TCOFLUSH - If fd refers to a device open for output,
367                                     flush pending output.  Otherwise error EINVAL.
368                                   * TCIOFLUSH - If fd refers to a device open for both
369                                     input and output, flush pending input and output.
370                                     Otherwise error EINVAL.
371 
372     @retval 0     The operation completed successfully.
373     @retval -1    An error occured and errno is set to indicate the error.
374                     * EBADF - The fd argument is not a valid file descriptor.
375                     * ENOTTY - The file associated with fd is not an interactive IO device.
376                     * EINVAL - The QueueSelector argument is not a supported value.
377                     * ENOTSUP - This function is not supported.
378 **/
379 int     tcflush     (int fd, int QueueSelector);
380 
381 //int     tcsendbreak (int, int);
382 //pid_t   tcgetsid    (int);
383 
384 //void    cfmakeraw   (struct termios *);
385 //int     cfsetspeed  (struct termios *, speed_t);
386 __END_DECLS
387 
388 /*  Input values for UEFI Keyboard Scan Codes.
389 
390     The UEFI Keyboard Scan Codes are mapped into the upper range of the Unicode
391     Private Use Area so that the characters can be inserted into the input stream
392     and treated the same as any other character.
393 
394     These values are only used for input.  If these codes are output to the
395     console, or another interactive I/O device, the behavior will depend upon
396     the current locale and UEFI character set loaded.
397 */
398 typedef enum {
399   TtySpecKeyMin = 0xF7F0,
400   /* This area is reserved for use by internal I/O software.
401       At least 4 values must exist between TtySpecKeyMin and TtyFunKeyMin.
402   */
403   TtyFunKeyMin  = 0xF7FA,
404   TtyKeyEject   = 0xF7FA,
405   TtyRecovery,         TtyToggleDisplay,    TtyHibernate,
406   TtySuspend,          TtyBrightnessDown,   TtyBrightnessUp,
407   TtyVolumeDown = 0xF87F,
408   TtyVolumeUp,         TtyMute,
409   TtyF24        = 0xF88D,
410   TtyF23,              TtyF22,              TtyF21,              TtyF20,
411   TtyF19,              TtyF18,              TtyF17,              TtyF16,
412   TtyF15,              TtyF14,              TtyF13,
413   TtyEscape     = 0xF8E9,
414   TtyF12,              TtyF11,              TtyF10,              TtyF9,
415   TtyF8,               TtyF7,               TtyF6,               TtyF5,
416   TtyF4,               TtyF3,               TtyF2,               TtyF1,
417   TtyPageDown,         TtyPageUp,           TtyDelete,           TtyInsert,
418   TtyEnd,              TtyHome,             TtyLeftArrow,        TtyRightArrow,
419   TtyDownArrow,
420   TtyUpArrow    = 0xF8FF,
421   TtyFunKeyMax  = 0xF900
422 } TtyFunKey;
423 
424 // Non-UEFI character definitions
425 #define CHAR_EOT    0x0004        /* End of Text (EOT) character -- Unix End-of-File character */
426 #define CHAR_SUB    0x001a        /* MSDOS End-of-File character */
427 #define CHAR_ESC    0x001b        /* Escape (ESC) character */
428 
429 #endif /* !_SYS_TERMIOS_H_ */
430