tty_ioctl.c 25 KB

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