Linux Audio

Check our new training course

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