• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
3  */
4 
5 #include <linux/types.h>
6 #include <linux/kernel.h>
7 #include <linux/termios.h>
8 #include <linux/tty.h>
9 #include <linux/export.h>
10 
11 
12 /*
13  * Routine which returns the baud rate of the tty
14  *
15  * Note that the baud_table needs to be kept in sync with the
16  * include/asm/termbits.h file.
17  */
18 static const speed_t baud_table[] = {
19 	0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
20 	9600, 19200, 38400, 57600, 115200, 230400, 460800,
21 #ifdef __sparc__
22 	76800, 153600, 307200, 614400, 921600
23 #else
24 	500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
25 	2500000, 3000000, 3500000, 4000000
26 #endif
27 };
28 
29 #ifndef __sparc__
30 static const tcflag_t baud_bits[] = {
31 	B0, B50, B75, B110, B134, B150, B200, B300, B600,
32 	B1200, B1800, B2400, B4800, B9600, B19200, B38400,
33 	B57600, B115200, B230400, B460800, B500000, B576000,
34 	B921600, B1000000, B1152000, B1500000, B2000000, B2500000,
35 	B3000000, B3500000, B4000000
36 };
37 #else
38 static const tcflag_t baud_bits[] = {
39 	B0, B50, B75, B110, B134, B150, B200, B300, B600,
40 	B1200, B1800, B2400, B4800, B9600, B19200, B38400,
41 	B57600, B115200, B230400, B460800, B76800, B153600,
42 	B307200, B614400, B921600
43 };
44 #endif
45 
46 static int n_baud_table = ARRAY_SIZE(baud_table);
47 
48 /**
49  *	tty_termios_baud_rate
50  *	@termios: termios structure
51  *
52  *	Convert termios baud rate data into a speed. This should be called
53  *	with the termios lock held if this termios is a terminal termios
54  *	structure. May change the termios data. Device drivers can call this
55  *	function but should use ->c_[io]speed directly as they are updated.
56  *
57  *	Locking: none
58  */
59 
tty_termios_baud_rate(struct ktermios * termios)60 speed_t tty_termios_baud_rate(struct ktermios *termios)
61 {
62 	unsigned int cbaud;
63 
64 	cbaud = termios->c_cflag & CBAUD;
65 
66 #ifdef BOTHER
67 	/* Magic token for arbitrary speed via c_ispeed/c_ospeed */
68 	if (cbaud == BOTHER)
69 		return termios->c_ospeed;
70 #endif
71 	if (cbaud & CBAUDEX) {
72 		cbaud &= ~CBAUDEX;
73 
74 		if (cbaud < 1 || cbaud + 15 > n_baud_table)
75 			termios->c_cflag &= ~CBAUDEX;
76 		else
77 			cbaud += 15;
78 	}
79 	return cbaud >= n_baud_table ? 0 : baud_table[cbaud];
80 }
81 EXPORT_SYMBOL(tty_termios_baud_rate);
82 
83 /**
84  *	tty_termios_input_baud_rate
85  *	@termios: termios structure
86  *
87  *	Convert termios baud rate data into a speed. This should be called
88  *	with the termios lock held if this termios is a terminal termios
89  *	structure. May change the termios data. Device drivers can call this
90  *	function but should use ->c_[io]speed directly as they are updated.
91  *
92  *	Locking: none
93  */
94 
tty_termios_input_baud_rate(struct ktermios * termios)95 speed_t tty_termios_input_baud_rate(struct ktermios *termios)
96 {
97 #ifdef IBSHIFT
98 	unsigned int cbaud = (termios->c_cflag >> IBSHIFT) & CBAUD;
99 
100 	if (cbaud == B0)
101 		return tty_termios_baud_rate(termios);
102 
103 	/* Magic token for arbitrary speed via c_ispeed*/
104 	if (cbaud == BOTHER)
105 		return termios->c_ispeed;
106 
107 	if (cbaud & CBAUDEX) {
108 		cbaud &= ~CBAUDEX;
109 
110 		if (cbaud < 1 || cbaud + 15 > n_baud_table)
111 			termios->c_cflag &= ~(CBAUDEX << IBSHIFT);
112 		else
113 			cbaud += 15;
114 	}
115 	return cbaud >= n_baud_table ? 0 : baud_table[cbaud];
116 #else
117 	return tty_termios_baud_rate(termios);
118 #endif
119 }
120 EXPORT_SYMBOL(tty_termios_input_baud_rate);
121 
122 /**
123  *	tty_termios_encode_baud_rate
124  *	@termios: ktermios structure holding user requested state
125  *	@ispeed: input speed
126  *	@ospeed: output speed
127  *
128  *	Encode the speeds set into the passed termios structure. This is
129  *	used as a library helper for drivers so that they can report back
130  *	the actual speed selected when it differs from the speed requested
131  *
132  *	For maximal back compatibility with legacy SYS5/POSIX *nix behaviour
133  *	we need to carefully set the bits when the user does not get the
134  *	desired speed. We allow small margins and preserve as much of possible
135  *	of the input intent to keep compatibility.
136  *
137  *	Locking: Caller should hold termios lock. This is already held
138  *	when calling this function from the driver termios handler.
139  *
140  *	The ifdefs deal with platforms whose owners have yet to update them
141  *	and will all go away once this is done.
142  */
143 
tty_termios_encode_baud_rate(struct ktermios * termios,speed_t ibaud,speed_t obaud)144 void tty_termios_encode_baud_rate(struct ktermios *termios,
145 				  speed_t ibaud, speed_t obaud)
146 {
147 	int i = 0;
148 	int ifound = -1, ofound = -1;
149 	int iclose = ibaud/50, oclose = obaud/50;
150 	int ibinput = 0;
151 
152 	if (obaud == 0)			/* CD dropped 		  */
153 		ibaud = 0;		/* Clear ibaud to be sure */
154 
155 	termios->c_ispeed = ibaud;
156 	termios->c_ospeed = obaud;
157 
158 #ifdef BOTHER
159 	if ((termios->c_cflag >> IBSHIFT) & CBAUD)
160 		ibinput = 1;	/* An input speed was specified */
161 
162 	/* If the user asked for a precise weird speed give a precise weird
163 	   answer. If they asked for a Bfoo speed they may have problems
164 	   digesting non-exact replies so fuzz a bit */
165 
166 	if ((termios->c_cflag & CBAUD) == BOTHER) {
167 		oclose = 0;
168 		if (!ibinput)
169 			iclose = 0;
170 	}
171 	if (((termios->c_cflag >> IBSHIFT) & CBAUD) == BOTHER)
172 		iclose = 0;
173 #endif
174 	termios->c_cflag &= ~CBAUD;
175 #ifdef IBSHIFT
176 	termios->c_cflag &= ~(CBAUD << IBSHIFT);
177 #endif
178 
179 	/*
180 	 *	Our goal is to find a close match to the standard baud rate
181 	 *	returned. Walk the baud rate table and if we get a very close
182 	 *	match then report back the speed as a POSIX Bxxxx value by
183 	 *	preference
184 	 */
185 
186 	do {
187 		if (obaud - oclose <= baud_table[i] &&
188 		    obaud + oclose >= baud_table[i]) {
189 			termios->c_cflag |= baud_bits[i];
190 			ofound = i;
191 		}
192 		if (ibaud - iclose <= baud_table[i] &&
193 		    ibaud + iclose >= baud_table[i]) {
194 			/* For the case input == output don't set IBAUD bits
195 			   if the user didn't do so */
196 			if (ofound == i && !ibinput)
197 				ifound  = i;
198 #ifdef IBSHIFT
199 			else {
200 				ifound = i;
201 				termios->c_cflag |= (baud_bits[i] << IBSHIFT);
202 			}
203 #endif
204 		}
205 	} while (++i < n_baud_table);
206 
207 	/*
208 	 *	If we found no match then use BOTHER if provided or warn
209 	 *	the user their platform maintainer needs to wake up if not.
210 	 */
211 #ifdef BOTHER
212 	if (ofound == -1)
213 		termios->c_cflag |= BOTHER;
214 	/* Set exact input bits only if the input and output differ or the
215 	   user already did */
216 	if (ifound == -1 && (ibaud != obaud || ibinput))
217 		termios->c_cflag |= (BOTHER << IBSHIFT);
218 #else
219 	if (ifound == -1 || ofound == -1)
220 		pr_warn_once("tty: Unable to return correct speed data as your architecture needs updating.\n");
221 #endif
222 }
223 EXPORT_SYMBOL_GPL(tty_termios_encode_baud_rate);
224 
225 /**
226  *	tty_encode_baud_rate		-	set baud rate of the tty
227  *	@ibaud: input baud rate
228  *	@obad: output baud rate
229  *
230  *	Update the current termios data for the tty with the new speed
231  *	settings. The caller must hold the termios_rwsem for the tty in
232  *	question.
233  */
234 
tty_encode_baud_rate(struct tty_struct * tty,speed_t ibaud,speed_t obaud)235 void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud, speed_t obaud)
236 {
237 	tty_termios_encode_baud_rate(&tty->termios, ibaud, obaud);
238 }
239 EXPORT_SYMBOL_GPL(tty_encode_baud_rate);
240