]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/LibC/Uefi/InteractiveIO/TerminalFunctions.c
StdLib: Fix some build problems and obscure bugs.
[mirror_edk2.git] / StdLib / LibC / Uefi / InteractiveIO / TerminalFunctions.c
CommitLineData
6c6c850a 1/** @file\r
2 "Terminal" Control functions for Interactive IO.\r
3\r
4 Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>\r
5 This program and the accompanying materials are licensed and made available under\r
6 the terms and conditions of the BSD License that accompanies this distribution.\r
7 The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12**/\r
13#include <Uefi.h>\r
14#include <Library/BaseMemoryLib.h>\r
15\r
16#include <LibConfig.h>\r
17\r
18#include <errno.h>\r
19#include <sys/termios.h>\r
20#include <Device/IIO.h>\r
21#include <MainData.h>\r
22\r
23/** Get input baud rate.\r
24\r
25 Extracts the input baud rate from the termios structure pointed to by the\r
26 pTermios argument.\r
27\r
28 @param[in] pTermios A pointer to the termios structure from which to extract\r
29 the input baud rate.\r
30\r
31 @return The value of the input speed is returned exactly as it is contained\r
32 in the termios structure, without interpretation.\r
33**/\r
34speed_t\r
35cfgetispeed (\r
36 const struct termios *pTermios\r
37 )\r
38{\r
39 return pTermios->c_ispeed;\r
40}\r
41\r
42/** Get output baud rate.\r
43\r
44 Extracts the output baud rate from the termios structure pointed to by the\r
45 pTermios argument.\r
46\r
47 @param[in] pTermios A pointer to the termios structure from which to extract\r
48 the output baud rate.\r
49\r
50 @return The value of the output speed is returned exactly as it is contained\r
51 in the termios structure, without interpretation.\r
52**/\r
53speed_t\r
54cfgetospeed (\r
55 const struct termios *pTermios\r
56 )\r
57{\r
58 return pTermios->c_ospeed;\r
59}\r
60\r
61/** Set input baud rate.\r
62\r
63 Replaces the input baud rate, in the termios structure pointed to by the\r
64 pTermios argument, with the value of NewSpeed.\r
65\r
66 @param[out] pTermios A pointer to the termios structure into which to set\r
67 the input baud rate.\r
68 @param[in] NewSpeed The new input baud rate.\r
69\r
70 @retval 0 The operation completed successfully.\r
71 @retval -1 An error occured and errno is set to indicate the error.\r
72 * EINVAL - The value of NewSpeed is outside the range of\r
73 possible speed values as specified in <sys/termios.h>.\r
74**/\r
75int\r
76cfsetispeed (\r
77 struct termios *pTermios,\r
78 speed_t NewSpeed\r
79 )\r
80{\r
81 int RetVal;\r
82\r
83 if(NewSpeed < B921600) {\r
84 pTermios->c_ispeed = NewSpeed;\r
85 RetVal = 0;\r
86 }\r
87 else {\r
88 RetVal = -1;\r
89 errno = EINVAL;\r
90 }\r
91 return RetVal;\r
92}\r
93\r
94/** Set output baud rate.\r
95\r
96 Replaces the output baud rate, in the termios structure pointed to by the\r
97 pTermios argument, with the value of NewSpeed.\r
98\r
99 @param[out] pTermios A pointer to the termios structure into which to set\r
100 the output baud rate.\r
101 @param[in] NewSpeed The new output baud rate.\r
102\r
103 @retval 0 The operation completed successfully.\r
104 @retval -1 An error occured and errno is set to indicate the error.\r
105 * EINVAL - The value of NewSpeed is outside the range of\r
106 possible speed values as specified in <sys/termios.h>.\r
107**/\r
108int\r
109cfsetospeed (\r
110 struct termios *pTermios,\r
111 speed_t NewSpeed\r
112 )\r
113{\r
114 int RetVal;\r
115\r
116 if(NewSpeed < B921600) {\r
117 pTermios->c_ospeed = NewSpeed;\r
118 RetVal = 0;\r
119 }\r
120 else {\r
121 RetVal = -1;\r
122 errno = EINVAL;\r
123 }\r
124 return RetVal;\r
125}\r
126\r
127/** Get the parameters associated with an interactive IO device.\r
128\r
129 Get the parameters associated with the device referred to by\r
130 fd and store them into the termios structure referenced by pTermios.\r
131\r
132 @param[in] fd The file descriptor for an open interactive IO device.\r
133 @param[out] pTermios A pointer to a termios structure into which to store\r
134 attributes of the interactive IO device.\r
135\r
136 @retval 0 The operation completed successfully.\r
137 @retval -1 An error occured and errno is set to indicate the error.\r
138 * EBADF - The fd argument is not a valid file descriptor.\r
139 * ENOTTY - The file associated with fd is not an interactive IO device.\r
140**/\r
141int\r
142tcgetattr (\r
143 int fd,\r
144 struct termios *pTermios\r
145 )\r
146{\r
147 cIIO *IIO;\r
148 int RetVal;\r
149 struct __filedes *filp;\r
150 struct termios *Termio;\r
151\r
152 RetVal = 0;\r
153 if(ValidateFD( fd, VALID_OPEN)) {\r
154 filp = &gMD->fdarray[fd];\r
155\r
156 if((filp->f_iflags & _S_ITTY) != 0) {\r
157 // fd is for a TTY or "Interactive IO" device\r
158 IIO = (cIIO *)filp->devdata;\r
159 Termio = &IIO->Termio;\r
160 (void)CopyMem((void *)pTermios, (const void *)Termio, sizeof(struct termios));\r
161 }\r
162 else {\r
163 errno = ENOTTY;\r
164 RetVal = -1;\r
165 }\r
166 }\r
167 else {\r
168 errno = EBADF;\r
169 RetVal = -1;\r
170 }\r
171 return RetVal;\r
172}\r
173\r
174/** Set the parameters associated with an interactive IO device.\r
175\r
176 Set the parameters associated with the device referred to by\r
177 fd to the values in the termios structure referenced by pTermios.\r
178\r
179 Behavior is modified by the value of the OptAct parameter:\r
180 * TCSANOW: The change shall occur immediately.\r
181 * TCSADRAIN: The change shall occur after all output written to fd is\r
182 transmitted. This action should be used when changing parameters which\r
183 affect output.\r
184 * TCSAFLUSH: The change shall occur after all output written to fd is\r
185 transmitted, and all input so far received but not read shall be\r
186 discarded before the change is made.\r
187\r
188 @param[in] fd The file descriptor for an open interactive IO device.\r
189 @param[in] OptAct Currently has no effect.\r
190 @param[in] pTermios A pointer to a termios structure into which to retrieve\r
191 attributes to set in the interactive IO device.\r
192\r
193 @retval 0 The operation completed successfully.\r
194 @retval -1 An error occured and errno is set to indicate the error.\r
195 * EBADF - The fd argument is not a valid file descriptor.\r
196 * ENOTTY - The file associated with fd is not an interactive IO device.\r
197**/\r
198int\r
199tcsetattr (\r
200 int fd,\r
201 int OptAct, // Currently ignored\r
202 const struct termios *pTermios\r
203 )\r
204{\r
205 cIIO *IIO;\r
206 int RetVal;\r
207 struct __filedes *filp;\r
208 struct termios *Termio;\r
209\r
210 RetVal = 0;\r
211 if(ValidateFD( fd, VALID_OPEN)) {\r
212 filp = &gMD->fdarray[fd];\r
213\r
214 if((filp->f_iflags & _S_ITTY) != 0) {\r
215 // fd is for a TTY or "Interactive IO" device\r
216 IIO = (cIIO *)filp->devdata;\r
217 Termio = &IIO->Termio;\r
218 (void)CopyMem((void *)Termio, (const void *)pTermios, sizeof(struct termios));\r
219 }\r
220 else {\r
221 errno = ENOTTY;\r
222 RetVal = -1;\r
223 }\r
224 }\r
225 else {\r
226 errno = EBADF;\r
227 RetVal = -1;\r
228 }\r
229 return RetVal;\r
230}\r
231\r
232/** Transmit pending output.\r
233\r
234 Function is not yet implemented for UEFI.\r
235\r
236 @param[in] fd Ignored\r
237\r
238 @retval -1 This function is not yet supported. errno is set to ENOTSUP.\r
239**/\r
240int\r
241tcdrain (int fd)\r
242{\r
243 errno = ENOTSUP;\r
244 return -1;\r
245}\r
246\r
247/** Suspend or restart the transmission or reception of data.\r
248\r
249 This function will suspend or resume transmission or reception of data on\r
250 the file referred to by fd, depending on the value of Action.\r
251\r
252 Function is not yet implemented for UEFI.\r
253\r
254 @param[in] fd Ignored\r
255 @param[in] Action Ignored\r
256\r
257 @retval -1 This function is not yet supported. errno is set to ENOTSUP.\r
258**/\r
259int\r
260tcflow (\r
261 int fd,\r
262 int Action)\r
263{\r
264 errno = ENOTSUP;\r
265 return -1;\r
266}\r
267\r
268/** Discard non-transmitted output data, non-read input data, or both.\r
269\r
270 Function is not yet implemented for UEFI.\r
271\r
272 @param[in] fd Ignored\r
273 @param[in] QueueSelector Ignored\r
274\r
275 @retval -1 This function is not yet supported. errno is set to ENOTSUP.\r
276**/\r
277int\r
278tcflush (\r
279 int fd,\r
280 int QueueSelector)\r
281{\r
282 errno = ENOTSUP;\r
283 return -1;\r
284}\r
285\r