tty_ioctl.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  4. *
  5. * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
  6. * which can be dynamically activated and de-activated by the line
  7. * discipline handling modules (like SLIP).
  8. */
  9. #include <linux/bits.h>
  10. #include <linux/types.h>
  11. #include <linux/termios.h>
  12. #include <linux/errno.h>
  13. #include <linux/sched/signal.h>
  14. #include <linux/kernel.h>
  15. #include <linux/major.h>
  16. #include <linux/tty.h>
  17. #include <linux/fcntl.h>
  18. #include <linux/string.h>
  19. #include <linux/mm.h>
  20. #include <linux/module.h>
  21. #include <linux/bitops.h>
  22. #include <linux/mutex.h>
  23. #include <linux/compat.h>
  24. #include <linux/termios_internal.h>
  25. #include "tty.h"
  26. #include <asm/io.h>
  27. #include <linux/uaccess.h>
  28. #undef DEBUG
  29. /*
  30. * Internal flag options for termios setting behavior
  31. */
  32. #define TERMIOS_FLUSH BIT(0)
  33. #define TERMIOS_WAIT BIT(1)
  34. #define TERMIOS_TERMIO BIT(2)
  35. #define TERMIOS_OLD BIT(3)
  36. /**
  37. * tty_chars_in_buffer - characters pending
  38. * @tty: terminal
  39. *
  40. * Returns: the number of bytes of data in the device private output queue. If
  41. * no private method is supplied there is assumed to be no queue on the device.
  42. */
  43. unsigned int tty_chars_in_buffer(struct tty_struct *tty)
  44. {
  45. if (tty->ops->chars_in_buffer)
  46. return tty->ops->chars_in_buffer(tty);
  47. return 0;
  48. }
  49. EXPORT_SYMBOL(tty_chars_in_buffer);
  50. /**
  51. * tty_write_room - write queue space
  52. * @tty: terminal
  53. *
  54. * Returns: the number of bytes that can be queued to this device at the present
  55. * time. The result should be treated as a guarantee and the driver cannot
  56. * offer a value it later shrinks by more than the number of bytes written. If
  57. * no method is provided, 2K is always returned and data may be lost as there
  58. * will be no flow control.
  59. */
  60. unsigned int tty_write_room(struct tty_struct *tty)
  61. {
  62. if (tty->ops->write_room)
  63. return tty->ops->write_room(tty);
  64. return 2048;
  65. }
  66. EXPORT_SYMBOL(tty_write_room);
  67. /**
  68. * tty_driver_flush_buffer - discard internal buffer
  69. * @tty: terminal
  70. *
  71. * Discard the internal output buffer for this device. If no method is provided,
  72. * then either the buffer cannot be hardware flushed or there is no buffer
  73. * driver side.
  74. */
  75. void tty_driver_flush_buffer(struct tty_struct *tty)
  76. {
  77. if (tty->ops->flush_buffer)
  78. tty->ops->flush_buffer(tty);
  79. }
  80. EXPORT_SYMBOL(tty_driver_flush_buffer);
  81. /**
  82. * tty_unthrottle - flow control
  83. * @tty: terminal
  84. *
  85. * Indicate that a @tty may continue transmitting data down the stack. Takes
  86. * the &tty_struct->termios_rwsem to protect against parallel
  87. * throttle/unthrottle and also to ensure the driver can consistently reference
  88. * its own termios data at this point when implementing software flow control.
  89. *
  90. * Drivers should however remember that the stack can issue a throttle, then
  91. * change flow control method, then unthrottle.
  92. */
  93. void tty_unthrottle(struct tty_struct *tty)
  94. {
  95. down_write(&tty->termios_rwsem);
  96. if (test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
  97. tty->ops->unthrottle)
  98. tty->ops->unthrottle(tty);
  99. tty->flow_change = TTY_FLOW_NO_CHANGE;
  100. up_write(&tty->termios_rwsem);
  101. }
  102. EXPORT_SYMBOL(tty_unthrottle);
  103. /**
  104. * tty_throttle_safe - flow control
  105. * @tty: terminal
  106. *
  107. * Indicate that a @tty should stop transmitting data down the stack.
  108. * tty_throttle_safe() will only attempt throttle if @tty->flow_change is
  109. * %TTY_THROTTLE_SAFE. Prevents an accidental throttle due to race conditions
  110. * when throttling is conditional on factors evaluated prior to throttling.
  111. *
  112. * Returns: %true if @tty is throttled (or was already throttled)
  113. */
  114. bool tty_throttle_safe(struct tty_struct *tty)
  115. {
  116. bool ret = true;
  117. mutex_lock(&tty->throttle_mutex);
  118. if (!tty_throttled(tty)) {
  119. if (tty->flow_change != TTY_THROTTLE_SAFE)
  120. ret = false;
  121. else {
  122. set_bit(TTY_THROTTLED, &tty->flags);
  123. if (tty->ops->throttle)
  124. tty->ops->throttle(tty);
  125. }
  126. }
  127. mutex_unlock(&tty->throttle_mutex);
  128. return ret;
  129. }
  130. /**
  131. * tty_unthrottle_safe - flow control
  132. * @tty: terminal
  133. *
  134. * Similar to tty_unthrottle() but will only attempt unthrottle if
  135. * @tty->flow_change is %TTY_UNTHROTTLE_SAFE. Prevents an accidental unthrottle
  136. * due to race conditions when unthrottling is conditional on factors evaluated
  137. * prior to unthrottling.
  138. *
  139. * Returns: %true if @tty is unthrottled (or was already unthrottled)
  140. */
  141. bool tty_unthrottle_safe(struct tty_struct *tty)
  142. {
  143. bool ret = true;
  144. mutex_lock(&tty->throttle_mutex);
  145. if (tty_throttled(tty)) {
  146. if (tty->flow_change != TTY_UNTHROTTLE_SAFE)
  147. ret = false;
  148. else {
  149. clear_bit(TTY_THROTTLED, &tty->flags);
  150. if (tty->ops->unthrottle)
  151. tty->ops->unthrottle(tty);
  152. }
  153. }
  154. mutex_unlock(&tty->throttle_mutex);
  155. return ret;
  156. }
  157. /**
  158. * tty_wait_until_sent - wait for I/O to finish
  159. * @tty: tty we are waiting for
  160. * @timeout: how long we will wait
  161. *
  162. * Wait for characters pending in a tty driver to hit the wire, or for a
  163. * timeout to occur (eg due to flow control).
  164. *
  165. * Locking: none
  166. */
  167. void tty_wait_until_sent(struct tty_struct *tty, long timeout)
  168. {
  169. if (!timeout)
  170. timeout = MAX_SCHEDULE_TIMEOUT;
  171. timeout = wait_event_interruptible_timeout(tty->write_wait,
  172. !tty_chars_in_buffer(tty), timeout);
  173. if (timeout <= 0)
  174. return;
  175. if (timeout == MAX_SCHEDULE_TIMEOUT)
  176. timeout = 0;
  177. if (tty->ops->wait_until_sent)
  178. tty->ops->wait_until_sent(tty, timeout);
  179. }
  180. EXPORT_SYMBOL(tty_wait_until_sent);
  181. /*
  182. * Termios Helper Methods
  183. */
  184. static void unset_locked_termios(struct tty_struct *tty, const struct ktermios *old)
  185. {
  186. struct ktermios *termios = &tty->termios;
  187. struct ktermios *locked = &tty->termios_locked;
  188. int i;
  189. #define NOSET_MASK(x, y, z) (x = ((x) & ~(z)) | ((y) & (z)))
  190. NOSET_MASK(termios->c_iflag, old->c_iflag, locked->c_iflag);
  191. NOSET_MASK(termios->c_oflag, old->c_oflag, locked->c_oflag);
  192. NOSET_MASK(termios->c_cflag, old->c_cflag, locked->c_cflag);
  193. NOSET_MASK(termios->c_lflag, old->c_lflag, locked->c_lflag);
  194. termios->c_line = locked->c_line ? old->c_line : termios->c_line;
  195. for (i = 0; i < NCCS; i++)
  196. termios->c_cc[i] = locked->c_cc[i] ?
  197. old->c_cc[i] : termios->c_cc[i];
  198. /* FIXME: What should we do for i/ospeed */
  199. }
  200. /**
  201. * tty_termios_copy_hw - copy hardware settings
  202. * @new: new termios
  203. * @old: old termios
  204. *
  205. * Propagate the hardware specific terminal setting bits from the @old termios
  206. * structure to the @new one. This is used in cases where the hardware does not
  207. * support reconfiguration or as a helper in some cases where only minimal
  208. * reconfiguration is supported.
  209. */
  210. void tty_termios_copy_hw(struct ktermios *new, const struct ktermios *old)
  211. {
  212. /* The bits a dumb device handles in software. Smart devices need
  213. to always provide a set_termios method */
  214. new->c_cflag &= HUPCL | CREAD | CLOCAL;
  215. new->c_cflag |= old->c_cflag & ~(HUPCL | CREAD | CLOCAL);
  216. new->c_ispeed = old->c_ispeed;
  217. new->c_ospeed = old->c_ospeed;
  218. }
  219. EXPORT_SYMBOL(tty_termios_copy_hw);
  220. /**
  221. * tty_termios_hw_change - check for setting change
  222. * @a: termios
  223. * @b: termios to compare
  224. *
  225. * Check if any of the bits that affect a dumb device have changed between the
  226. * two termios structures, or a speed change is needed.
  227. *
  228. * Returns: %true if change is needed
  229. */
  230. bool tty_termios_hw_change(const struct ktermios *a, const struct ktermios *b)
  231. {
  232. if (a->c_ispeed != b->c_ispeed || a->c_ospeed != b->c_ospeed)
  233. return true;
  234. if ((a->c_cflag ^ b->c_cflag) & ~(HUPCL | CREAD | CLOCAL))
  235. return true;
  236. return false;
  237. }
  238. EXPORT_SYMBOL(tty_termios_hw_change);
  239. /**
  240. * tty_get_char_size - get size of a character
  241. * @cflag: termios cflag value
  242. *
  243. * Returns: size (in bits) of a character depending on @cflag's %CSIZE setting
  244. */
  245. unsigned char tty_get_char_size(unsigned int cflag)
  246. {
  247. switch (cflag & CSIZE) {
  248. case CS5:
  249. return 5;
  250. case CS6:
  251. return 6;
  252. case CS7:
  253. return 7;
  254. case CS8:
  255. default:
  256. return 8;
  257. }
  258. }
  259. EXPORT_SYMBOL_GPL(tty_get_char_size);
  260. /**
  261. * tty_get_frame_size - get size of a frame
  262. * @cflag: termios cflag value
  263. *
  264. * Get the size (in bits) of a frame depending on @cflag's %CSIZE, %CSTOPB, and
  265. * %PARENB setting. The result is a sum of character size, start and stop bits
  266. * -- one bit each -- second stop bit (if set), and parity bit (if set).
  267. *
  268. * Returns: size (in bits) of a frame depending on @cflag's setting.
  269. */
  270. unsigned char tty_get_frame_size(unsigned int cflag)
  271. {
  272. unsigned char bits = 2 + tty_get_char_size(cflag);
  273. if (cflag & CSTOPB)
  274. bits++;
  275. if (cflag & PARENB)
  276. bits++;
  277. if (cflag & ADDRB)
  278. bits++;
  279. return bits;
  280. }
  281. EXPORT_SYMBOL_GPL(tty_get_frame_size);
  282. /**
  283. * tty_set_termios - update termios values
  284. * @tty: tty to update
  285. * @new_termios: desired new value
  286. *
  287. * Perform updates to the termios values set on this @tty. A master pty's
  288. * termios should never be set.
  289. *
  290. * Locking: &tty_struct->termios_rwsem
  291. */
  292. int tty_set_termios(struct tty_struct *tty, struct ktermios *new_termios)
  293. {
  294. struct ktermios old_termios;
  295. struct tty_ldisc *ld;
  296. WARN_ON(tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  297. tty->driver->subtype == PTY_TYPE_MASTER);
  298. /*
  299. * Perform the actual termios internal changes under lock.
  300. */
  301. /* FIXME: we need to decide on some locking/ordering semantics
  302. for the set_termios notification eventually */
  303. down_write(&tty->termios_rwsem);
  304. old_termios = tty->termios;
  305. tty->termios = *new_termios;
  306. unset_locked_termios(tty, &old_termios);
  307. /* Reset any ADDRB changes, ADDRB is changed through ->rs485_config() */
  308. tty->termios.c_cflag ^= (tty->termios.c_cflag ^ old_termios.c_cflag) & ADDRB;
  309. if (tty->ops->set_termios)
  310. tty->ops->set_termios(tty, &old_termios);
  311. else
  312. tty_termios_copy_hw(&tty->termios, &old_termios);
  313. ld = tty_ldisc_ref(tty);
  314. if (ld != NULL) {
  315. if (ld->ops->set_termios)
  316. ld->ops->set_termios(tty, &old_termios);
  317. tty_ldisc_deref(ld);
  318. }
  319. up_write(&tty->termios_rwsem);
  320. return 0;
  321. }
  322. EXPORT_SYMBOL_GPL(tty_set_termios);
  323. /*
  324. * Translate a "termio" structure into a "termios". Ugh.
  325. */
  326. __weak int user_termio_to_kernel_termios(struct ktermios *termios,
  327. struct termio __user *termio)
  328. {
  329. struct termio v;
  330. if (copy_from_user(&v, termio, sizeof(struct termio)))
  331. return -EFAULT;
  332. termios->c_iflag = (0xffff0000 & termios->c_iflag) | v.c_iflag;
  333. termios->c_oflag = (0xffff0000 & termios->c_oflag) | v.c_oflag;
  334. termios->c_cflag = (0xffff0000 & termios->c_cflag) | v.c_cflag;
  335. termios->c_lflag = (0xffff0000 & termios->c_lflag) | v.c_lflag;
  336. termios->c_line = (0xffff0000 & termios->c_lflag) | v.c_line;
  337. memcpy(termios->c_cc, v.c_cc, NCC);
  338. return 0;
  339. }
  340. /*
  341. * Translate a "termios" structure into a "termio". Ugh.
  342. */
  343. __weak int kernel_termios_to_user_termio(struct termio __user *termio,
  344. struct ktermios *termios)
  345. {
  346. struct termio v;
  347. memset(&v, 0, sizeof(struct termio));
  348. v.c_iflag = termios->c_iflag;
  349. v.c_oflag = termios->c_oflag;
  350. v.c_cflag = termios->c_cflag;
  351. v.c_lflag = termios->c_lflag;
  352. v.c_line = termios->c_line;
  353. memcpy(v.c_cc, termios->c_cc, NCC);
  354. return copy_to_user(termio, &v, sizeof(struct termio));
  355. }
  356. #ifdef TCGETS2
  357. __weak int user_termios_to_kernel_termios(struct ktermios *k,
  358. struct termios2 __user *u)
  359. {
  360. return copy_from_user(k, u, sizeof(struct termios2));
  361. }
  362. __weak int kernel_termios_to_user_termios(struct termios2 __user *u,
  363. struct ktermios *k)
  364. {
  365. return copy_to_user(u, k, sizeof(struct termios2));
  366. }
  367. __weak int user_termios_to_kernel_termios_1(struct ktermios *k,
  368. struct termios __user *u)
  369. {
  370. return copy_from_user(k, u, sizeof(struct termios));
  371. }
  372. __weak int kernel_termios_to_user_termios_1(struct termios __user *u,
  373. struct ktermios *k)
  374. {
  375. return copy_to_user(u, k, sizeof(struct termios));
  376. }
  377. #else
  378. __weak int user_termios_to_kernel_termios(struct ktermios *k,
  379. struct termios __user *u)
  380. {
  381. return copy_from_user(k, u, sizeof(struct termios));
  382. }
  383. __weak int kernel_termios_to_user_termios(struct termios __user *u,
  384. struct ktermios *k)
  385. {
  386. return copy_to_user(u, k, sizeof(struct termios));
  387. }
  388. #endif /* TCGETS2 */
  389. /**
  390. * set_termios - set termios values for a tty
  391. * @tty: terminal device
  392. * @arg: user data
  393. * @opt: option information
  394. *
  395. * Helper function to prepare termios data and run necessary other functions
  396. * before using tty_set_termios() to do the actual changes.
  397. *
  398. * Locking: called functions take &tty_struct->ldisc_sem and
  399. * &tty_struct->termios_rwsem locks
  400. *
  401. * Returns: 0 on success, an error otherwise
  402. */
  403. static int set_termios(struct tty_struct *tty, void __user *arg, int opt)
  404. {
  405. struct ktermios tmp_termios;
  406. struct tty_ldisc *ld;
  407. int retval = tty_check_change(tty);
  408. if (retval)
  409. return retval;
  410. down_read(&tty->termios_rwsem);
  411. tmp_termios = tty->termios;
  412. up_read(&tty->termios_rwsem);
  413. if (opt & TERMIOS_TERMIO) {
  414. if (user_termio_to_kernel_termios(&tmp_termios,
  415. (struct termio __user *)arg))
  416. return -EFAULT;
  417. #ifdef TCGETS2
  418. } else if (opt & TERMIOS_OLD) {
  419. if (user_termios_to_kernel_termios_1(&tmp_termios,
  420. (struct termios __user *)arg))
  421. return -EFAULT;
  422. } else {
  423. if (user_termios_to_kernel_termios(&tmp_termios,
  424. (struct termios2 __user *)arg))
  425. return -EFAULT;
  426. }
  427. #else
  428. } else if (user_termios_to_kernel_termios(&tmp_termios,
  429. (struct termios __user *)arg))
  430. return -EFAULT;
  431. #endif
  432. /* If old style Bfoo values are used then load c_ispeed/c_ospeed
  433. * with the real speed so its unconditionally usable */
  434. tmp_termios.c_ispeed = tty_termios_input_baud_rate(&tmp_termios);
  435. tmp_termios.c_ospeed = tty_termios_baud_rate(&tmp_termios);
  436. if (opt & (TERMIOS_FLUSH|TERMIOS_WAIT)) {
  437. retry_write_wait:
  438. retval = wait_event_interruptible(tty->write_wait, !tty_chars_in_buffer(tty));
  439. if (retval < 0)
  440. return retval;
  441. if (tty_write_lock(tty, false) < 0)
  442. goto retry_write_wait;
  443. /* Racing writer? */
  444. if (tty_chars_in_buffer(tty)) {
  445. tty_write_unlock(tty);
  446. goto retry_write_wait;
  447. }
  448. ld = tty_ldisc_ref(tty);
  449. if (ld != NULL) {
  450. if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
  451. ld->ops->flush_buffer(tty);
  452. tty_ldisc_deref(ld);
  453. }
  454. if ((opt & TERMIOS_WAIT) && tty->ops->wait_until_sent) {
  455. tty->ops->wait_until_sent(tty, 0);
  456. if (signal_pending(current)) {
  457. tty_write_unlock(tty);
  458. return -ERESTARTSYS;
  459. }
  460. }
  461. tty_set_termios(tty, &tmp_termios);
  462. tty_write_unlock(tty);
  463. } else {
  464. tty_set_termios(tty, &tmp_termios);
  465. }
  466. /* FIXME: Arguably if tmp_termios == tty->termios AND the
  467. actual requested termios was not tmp_termios then we may
  468. want to return an error as no user requested change has
  469. succeeded */
  470. return 0;
  471. }
  472. static void copy_termios(struct tty_struct *tty, struct ktermios *kterm)
  473. {
  474. down_read(&tty->termios_rwsem);
  475. *kterm = tty->termios;
  476. up_read(&tty->termios_rwsem);
  477. }
  478. static void copy_termios_locked(struct tty_struct *tty, struct ktermios *kterm)
  479. {
  480. down_read(&tty->termios_rwsem);
  481. *kterm = tty->termios_locked;
  482. up_read(&tty->termios_rwsem);
  483. }
  484. static int get_termio(struct tty_struct *tty, struct termio __user *termio)
  485. {
  486. struct ktermios kterm;
  487. copy_termios(tty, &kterm);
  488. if (kernel_termios_to_user_termio(termio, &kterm))
  489. return -EFAULT;
  490. return 0;
  491. }
  492. #ifdef TIOCGETP
  493. /*
  494. * These are deprecated, but there is limited support..
  495. *
  496. * The "sg_flags" translation is a joke..
  497. */
  498. static int get_sgflags(struct tty_struct *tty)
  499. {
  500. int flags = 0;
  501. if (!L_ICANON(tty)) {
  502. if (L_ISIG(tty))
  503. flags |= 0x02; /* cbreak */
  504. else
  505. flags |= 0x20; /* raw */
  506. }
  507. if (L_ECHO(tty))
  508. flags |= 0x08; /* echo */
  509. if (O_OPOST(tty))
  510. if (O_ONLCR(tty))
  511. flags |= 0x10; /* crmod */
  512. return flags;
  513. }
  514. static int get_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
  515. {
  516. struct sgttyb tmp;
  517. down_read(&tty->termios_rwsem);
  518. tmp.sg_ispeed = tty->termios.c_ispeed;
  519. tmp.sg_ospeed = tty->termios.c_ospeed;
  520. tmp.sg_erase = tty->termios.c_cc[VERASE];
  521. tmp.sg_kill = tty->termios.c_cc[VKILL];
  522. tmp.sg_flags = get_sgflags(tty);
  523. up_read(&tty->termios_rwsem);
  524. return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  525. }
  526. static void set_sgflags(struct ktermios *termios, int flags)
  527. {
  528. termios->c_iflag = ICRNL | IXON;
  529. termios->c_oflag = 0;
  530. termios->c_lflag = ISIG | ICANON;
  531. if (flags & 0x02) { /* cbreak */
  532. termios->c_iflag = 0;
  533. termios->c_lflag &= ~ICANON;
  534. }
  535. if (flags & 0x08) { /* echo */
  536. termios->c_lflag |= ECHO | ECHOE | ECHOK |
  537. ECHOCTL | ECHOKE | IEXTEN;
  538. }
  539. if (flags & 0x10) { /* crmod */
  540. termios->c_oflag |= OPOST | ONLCR;
  541. }
  542. if (flags & 0x20) { /* raw */
  543. termios->c_iflag = 0;
  544. termios->c_lflag &= ~(ISIG | ICANON);
  545. }
  546. if (!(termios->c_lflag & ICANON)) {
  547. termios->c_cc[VMIN] = 1;
  548. termios->c_cc[VTIME] = 0;
  549. }
  550. }
  551. /**
  552. * set_sgttyb - set legacy terminal values
  553. * @tty: tty structure
  554. * @sgttyb: pointer to old style terminal structure
  555. *
  556. * Updates a terminal from the legacy BSD style terminal information structure.
  557. *
  558. * Locking: &tty_struct->termios_rwsem
  559. *
  560. * Returns: 0 on success, an error otherwise
  561. */
  562. static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
  563. {
  564. int retval;
  565. struct sgttyb tmp;
  566. struct ktermios termios;
  567. retval = tty_check_change(tty);
  568. if (retval)
  569. return retval;
  570. if (copy_from_user(&tmp, sgttyb, sizeof(tmp)))
  571. return -EFAULT;
  572. down_write(&tty->termios_rwsem);
  573. termios = tty->termios;
  574. termios.c_cc[VERASE] = tmp.sg_erase;
  575. termios.c_cc[VKILL] = tmp.sg_kill;
  576. set_sgflags(&termios, tmp.sg_flags);
  577. /* Try and encode into Bfoo format */
  578. tty_termios_encode_baud_rate(&termios, termios.c_ispeed,
  579. termios.c_ospeed);
  580. up_write(&tty->termios_rwsem);
  581. tty_set_termios(tty, &termios);
  582. return 0;
  583. }
  584. #endif
  585. #ifdef TIOCGETC
  586. static int get_tchars(struct tty_struct *tty, struct tchars __user *tchars)
  587. {
  588. struct tchars tmp;
  589. down_read(&tty->termios_rwsem);
  590. tmp.t_intrc = tty->termios.c_cc[VINTR];
  591. tmp.t_quitc = tty->termios.c_cc[VQUIT];
  592. tmp.t_startc = tty->termios.c_cc[VSTART];
  593. tmp.t_stopc = tty->termios.c_cc[VSTOP];
  594. tmp.t_eofc = tty->termios.c_cc[VEOF];
  595. tmp.t_brkc = tty->termios.c_cc[VEOL2]; /* what is brkc anyway? */
  596. up_read(&tty->termios_rwsem);
  597. return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  598. }
  599. static int set_tchars(struct tty_struct *tty, struct tchars __user *tchars)
  600. {
  601. struct tchars tmp;
  602. if (copy_from_user(&tmp, tchars, sizeof(tmp)))
  603. return -EFAULT;
  604. down_write(&tty->termios_rwsem);
  605. tty->termios.c_cc[VINTR] = tmp.t_intrc;
  606. tty->termios.c_cc[VQUIT] = tmp.t_quitc;
  607. tty->termios.c_cc[VSTART] = tmp.t_startc;
  608. tty->termios.c_cc[VSTOP] = tmp.t_stopc;
  609. tty->termios.c_cc[VEOF] = tmp.t_eofc;
  610. tty->termios.c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */
  611. up_write(&tty->termios_rwsem);
  612. return 0;
  613. }
  614. #endif
  615. #ifdef TIOCGLTC
  616. static int get_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
  617. {
  618. struct ltchars tmp;
  619. down_read(&tty->termios_rwsem);
  620. tmp.t_suspc = tty->termios.c_cc[VSUSP];
  621. /* what is dsuspc anyway? */
  622. tmp.t_dsuspc = tty->termios.c_cc[VSUSP];
  623. tmp.t_rprntc = tty->termios.c_cc[VREPRINT];
  624. /* what is flushc anyway? */
  625. tmp.t_flushc = tty->termios.c_cc[VEOL2];
  626. tmp.t_werasc = tty->termios.c_cc[VWERASE];
  627. tmp.t_lnextc = tty->termios.c_cc[VLNEXT];
  628. up_read(&tty->termios_rwsem);
  629. return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  630. }
  631. static int set_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
  632. {
  633. struct ltchars tmp;
  634. if (copy_from_user(&tmp, ltchars, sizeof(tmp)))
  635. return -EFAULT;
  636. down_write(&tty->termios_rwsem);
  637. tty->termios.c_cc[VSUSP] = tmp.t_suspc;
  638. /* what is dsuspc anyway? */
  639. tty->termios.c_cc[VEOL2] = tmp.t_dsuspc;
  640. tty->termios.c_cc[VREPRINT] = tmp.t_rprntc;
  641. /* what is flushc anyway? */
  642. tty->termios.c_cc[VEOL2] = tmp.t_flushc;
  643. tty->termios.c_cc[VWERASE] = tmp.t_werasc;
  644. tty->termios.c_cc[VLNEXT] = tmp.t_lnextc;
  645. up_write(&tty->termios_rwsem);
  646. return 0;
  647. }
  648. #endif
  649. /**
  650. * tty_change_softcar - carrier change ioctl helper
  651. * @tty: tty to update
  652. * @enable: enable/disable %CLOCAL
  653. *
  654. * Perform a change to the %CLOCAL state and call into the driver layer to make
  655. * it visible.
  656. *
  657. * Locking: &tty_struct->termios_rwsem.
  658. *
  659. * Returns: 0 on success, an error otherwise
  660. */
  661. static int tty_change_softcar(struct tty_struct *tty, bool enable)
  662. {
  663. int ret = 0;
  664. struct ktermios old;
  665. tcflag_t bit = enable ? CLOCAL : 0;
  666. down_write(&tty->termios_rwsem);
  667. old = tty->termios;
  668. tty->termios.c_cflag &= ~CLOCAL;
  669. tty->termios.c_cflag |= bit;
  670. if (tty->ops->set_termios)
  671. tty->ops->set_termios(tty, &old);
  672. if (C_CLOCAL(tty) != bit)
  673. ret = -EINVAL;
  674. up_write(&tty->termios_rwsem);
  675. return ret;
  676. }
  677. /**
  678. * tty_mode_ioctl - mode related ioctls
  679. * @tty: tty for the ioctl
  680. * @cmd: command
  681. * @arg: ioctl argument
  682. *
  683. * Perform non-line discipline specific mode control ioctls. This is designed
  684. * to be called by line disciplines to ensure they provide consistent mode
  685. * setting.
  686. */
  687. int tty_mode_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
  688. {
  689. struct tty_struct *real_tty;
  690. void __user *p = (void __user *)arg;
  691. int ret = 0;
  692. struct ktermios kterm;
  693. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  694. tty->driver->subtype == PTY_TYPE_MASTER)
  695. real_tty = tty->link;
  696. else
  697. real_tty = tty;
  698. switch (cmd) {
  699. #ifdef TIOCGETP
  700. case TIOCGETP:
  701. return get_sgttyb(real_tty, (struct sgttyb __user *) arg);
  702. case TIOCSETP:
  703. case TIOCSETN:
  704. return set_sgttyb(real_tty, (struct sgttyb __user *) arg);
  705. #endif
  706. #ifdef TIOCGETC
  707. case TIOCGETC:
  708. return get_tchars(real_tty, p);
  709. case TIOCSETC:
  710. return set_tchars(real_tty, p);
  711. #endif
  712. #ifdef TIOCGLTC
  713. case TIOCGLTC:
  714. return get_ltchars(real_tty, p);
  715. case TIOCSLTC:
  716. return set_ltchars(real_tty, p);
  717. #endif
  718. case TCSETSF:
  719. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_OLD);
  720. case TCSETSW:
  721. return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_OLD);
  722. case TCSETS:
  723. return set_termios(real_tty, p, TERMIOS_OLD);
  724. #ifndef TCGETS2
  725. case TCGETS:
  726. copy_termios(real_tty, &kterm);
  727. if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
  728. ret = -EFAULT;
  729. return ret;
  730. #else
  731. case TCGETS:
  732. copy_termios(real_tty, &kterm);
  733. if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
  734. ret = -EFAULT;
  735. return ret;
  736. case TCGETS2:
  737. copy_termios(real_tty, &kterm);
  738. if (kernel_termios_to_user_termios((struct termios2 __user *)arg, &kterm))
  739. ret = -EFAULT;
  740. return ret;
  741. case TCSETSF2:
  742. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT);
  743. case TCSETSW2:
  744. return set_termios(real_tty, p, TERMIOS_WAIT);
  745. case TCSETS2:
  746. return set_termios(real_tty, p, 0);
  747. #endif
  748. case TCGETA:
  749. return get_termio(real_tty, p);
  750. case TCSETAF:
  751. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_TERMIO);
  752. case TCSETAW:
  753. return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_TERMIO);
  754. case TCSETA:
  755. return set_termios(real_tty, p, TERMIOS_TERMIO);
  756. #ifndef TCGETS2
  757. case TIOCGLCKTRMIOS:
  758. copy_termios_locked(real_tty, &kterm);
  759. if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
  760. ret = -EFAULT;
  761. return ret;
  762. case TIOCSLCKTRMIOS:
  763. if (!checkpoint_restore_ns_capable(&init_user_ns))
  764. return -EPERM;
  765. copy_termios_locked(real_tty, &kterm);
  766. if (user_termios_to_kernel_termios(&kterm,
  767. (struct termios __user *) arg))
  768. return -EFAULT;
  769. down_write(&real_tty->termios_rwsem);
  770. real_tty->termios_locked = kterm;
  771. up_write(&real_tty->termios_rwsem);
  772. return 0;
  773. #else
  774. case TIOCGLCKTRMIOS:
  775. copy_termios_locked(real_tty, &kterm);
  776. if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
  777. ret = -EFAULT;
  778. return ret;
  779. case TIOCSLCKTRMIOS:
  780. if (!checkpoint_restore_ns_capable(&init_user_ns))
  781. return -EPERM;
  782. copy_termios_locked(real_tty, &kterm);
  783. if (user_termios_to_kernel_termios_1(&kterm,
  784. (struct termios __user *) arg))
  785. return -EFAULT;
  786. down_write(&real_tty->termios_rwsem);
  787. real_tty->termios_locked = kterm;
  788. up_write(&real_tty->termios_rwsem);
  789. return ret;
  790. #endif
  791. #ifdef TCGETX
  792. case TCGETX:
  793. case TCSETX:
  794. case TCSETXW:
  795. case TCSETXF:
  796. return -ENOTTY;
  797. #endif
  798. case TIOCGSOFTCAR:
  799. copy_termios(real_tty, &kterm);
  800. ret = put_user((kterm.c_cflag & CLOCAL) ? 1 : 0,
  801. (int __user *)arg);
  802. return ret;
  803. case TIOCSSOFTCAR:
  804. if (get_user(arg, (unsigned int __user *) arg))
  805. return -EFAULT;
  806. return tty_change_softcar(real_tty, arg);
  807. default:
  808. return -ENOIOCTLCMD;
  809. }
  810. }
  811. EXPORT_SYMBOL_GPL(tty_mode_ioctl);
  812. /* Caller guarantees ldisc reference is held */
  813. static int __tty_perform_flush(struct tty_struct *tty, unsigned long arg)
  814. {
  815. struct tty_ldisc *ld = tty->ldisc;
  816. switch (arg) {
  817. case TCIFLUSH:
  818. if (ld && ld->ops->flush_buffer) {
  819. ld->ops->flush_buffer(tty);
  820. tty_unthrottle(tty);
  821. }
  822. break;
  823. case TCIOFLUSH:
  824. if (ld && ld->ops->flush_buffer) {
  825. ld->ops->flush_buffer(tty);
  826. tty_unthrottle(tty);
  827. }
  828. fallthrough;
  829. case TCOFLUSH:
  830. tty_driver_flush_buffer(tty);
  831. break;
  832. default:
  833. return -EINVAL;
  834. }
  835. return 0;
  836. }
  837. int tty_perform_flush(struct tty_struct *tty, unsigned long arg)
  838. {
  839. struct tty_ldisc *ld;
  840. int retval = tty_check_change(tty);
  841. if (retval)
  842. return retval;
  843. ld = tty_ldisc_ref_wait(tty);
  844. retval = __tty_perform_flush(tty, arg);
  845. if (ld)
  846. tty_ldisc_deref(ld);
  847. return retval;
  848. }
  849. EXPORT_SYMBOL_GPL(tty_perform_flush);
  850. int n_tty_ioctl_helper(struct tty_struct *tty, unsigned int cmd,
  851. unsigned long arg)
  852. {
  853. int retval;
  854. switch (cmd) {
  855. case TCXONC:
  856. retval = tty_check_change(tty);
  857. if (retval)
  858. return retval;
  859. switch (arg) {
  860. case TCOOFF:
  861. spin_lock_irq(&tty->flow.lock);
  862. if (!tty->flow.tco_stopped) {
  863. tty->flow.tco_stopped = true;
  864. __stop_tty(tty);
  865. }
  866. spin_unlock_irq(&tty->flow.lock);
  867. break;
  868. case TCOON:
  869. spin_lock_irq(&tty->flow.lock);
  870. if (tty->flow.tco_stopped) {
  871. tty->flow.tco_stopped = false;
  872. __start_tty(tty);
  873. }
  874. spin_unlock_irq(&tty->flow.lock);
  875. break;
  876. case TCIOFF:
  877. if (STOP_CHAR(tty) != __DISABLED_CHAR)
  878. retval = tty_send_xchar(tty, STOP_CHAR(tty));
  879. break;
  880. case TCION:
  881. if (START_CHAR(tty) != __DISABLED_CHAR)
  882. retval = tty_send_xchar(tty, START_CHAR(tty));
  883. break;
  884. default:
  885. return -EINVAL;
  886. }
  887. return retval;
  888. case TCFLSH:
  889. retval = tty_check_change(tty);
  890. if (retval)
  891. return retval;
  892. return __tty_perform_flush(tty, arg);
  893. default:
  894. /* Try the mode commands */
  895. return tty_mode_ioctl(tty, cmd, arg);
  896. }
  897. }
  898. EXPORT_SYMBOL(n_tty_ioctl_helper);