tty_ioctl.c 22 KB

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