tty_ioctl.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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 TCGETX
  386. /**
  387. * set_termiox - set termiox fields if possible
  388. * @tty: terminal
  389. * @arg: termiox structure from user
  390. * @opt: option flags for ioctl type
  391. *
  392. * Implement the device calling points for the SYS5 termiox ioctl
  393. * interface in Linux
  394. */
  395. static int set_termiox(struct tty_struct *tty, void __user *arg, int opt)
  396. {
  397. struct termiox tnew;
  398. struct tty_ldisc *ld;
  399. if (tty->termiox == NULL)
  400. return -EINVAL;
  401. if (copy_from_user(&tnew, arg, sizeof(struct termiox)))
  402. return -EFAULT;
  403. ld = tty_ldisc_ref(tty);
  404. if (ld != NULL) {
  405. if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
  406. ld->ops->flush_buffer(tty);
  407. tty_ldisc_deref(ld);
  408. }
  409. if (opt & TERMIOS_WAIT) {
  410. tty_wait_until_sent(tty, 0);
  411. if (signal_pending(current))
  412. return -ERESTARTSYS;
  413. }
  414. down_write(&tty->termios_rwsem);
  415. if (tty->ops->set_termiox)
  416. tty->ops->set_termiox(tty, &tnew);
  417. up_write(&tty->termios_rwsem);
  418. return 0;
  419. }
  420. #endif
  421. #ifdef TIOCGETP
  422. /*
  423. * These are deprecated, but there is limited support..
  424. *
  425. * The "sg_flags" translation is a joke..
  426. */
  427. static int get_sgflags(struct tty_struct *tty)
  428. {
  429. int flags = 0;
  430. if (!L_ICANON(tty)) {
  431. if (L_ISIG(tty))
  432. flags |= 0x02; /* cbreak */
  433. else
  434. flags |= 0x20; /* raw */
  435. }
  436. if (L_ECHO(tty))
  437. flags |= 0x08; /* echo */
  438. if (O_OPOST(tty))
  439. if (O_ONLCR(tty))
  440. flags |= 0x10; /* crmod */
  441. return flags;
  442. }
  443. static int get_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
  444. {
  445. struct sgttyb tmp;
  446. down_read(&tty->termios_rwsem);
  447. tmp.sg_ispeed = tty->termios.c_ispeed;
  448. tmp.sg_ospeed = tty->termios.c_ospeed;
  449. tmp.sg_erase = tty->termios.c_cc[VERASE];
  450. tmp.sg_kill = tty->termios.c_cc[VKILL];
  451. tmp.sg_flags = get_sgflags(tty);
  452. up_read(&tty->termios_rwsem);
  453. return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  454. }
  455. static void set_sgflags(struct ktermios *termios, int flags)
  456. {
  457. termios->c_iflag = ICRNL | IXON;
  458. termios->c_oflag = 0;
  459. termios->c_lflag = ISIG | ICANON;
  460. if (flags & 0x02) { /* cbreak */
  461. termios->c_iflag = 0;
  462. termios->c_lflag &= ~ICANON;
  463. }
  464. if (flags & 0x08) { /* echo */
  465. termios->c_lflag |= ECHO | ECHOE | ECHOK |
  466. ECHOCTL | ECHOKE | IEXTEN;
  467. }
  468. if (flags & 0x10) { /* crmod */
  469. termios->c_oflag |= OPOST | ONLCR;
  470. }
  471. if (flags & 0x20) { /* raw */
  472. termios->c_iflag = 0;
  473. termios->c_lflag &= ~(ISIG | ICANON);
  474. }
  475. if (!(termios->c_lflag & ICANON)) {
  476. termios->c_cc[VMIN] = 1;
  477. termios->c_cc[VTIME] = 0;
  478. }
  479. }
  480. /**
  481. * set_sgttyb - set legacy terminal values
  482. * @tty: tty structure
  483. * @sgttyb: pointer to old style terminal structure
  484. *
  485. * Updates a terminal from the legacy BSD style terminal information
  486. * structure.
  487. *
  488. * Locking: termios_rwsem
  489. */
  490. static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
  491. {
  492. int retval;
  493. struct sgttyb tmp;
  494. struct ktermios termios;
  495. retval = tty_check_change(tty);
  496. if (retval)
  497. return retval;
  498. if (copy_from_user(&tmp, sgttyb, sizeof(tmp)))
  499. return -EFAULT;
  500. down_write(&tty->termios_rwsem);
  501. termios = tty->termios;
  502. termios.c_cc[VERASE] = tmp.sg_erase;
  503. termios.c_cc[VKILL] = tmp.sg_kill;
  504. set_sgflags(&termios, tmp.sg_flags);
  505. /* Try and encode into Bfoo format */
  506. #ifdef BOTHER
  507. tty_termios_encode_baud_rate(&termios, termios.c_ispeed,
  508. termios.c_ospeed);
  509. #endif
  510. up_write(&tty->termios_rwsem);
  511. tty_set_termios(tty, &termios);
  512. return 0;
  513. }
  514. #endif
  515. #ifdef TIOCGETC
  516. static int get_tchars(struct tty_struct *tty, struct tchars __user *tchars)
  517. {
  518. struct tchars tmp;
  519. down_read(&tty->termios_rwsem);
  520. tmp.t_intrc = tty->termios.c_cc[VINTR];
  521. tmp.t_quitc = tty->termios.c_cc[VQUIT];
  522. tmp.t_startc = tty->termios.c_cc[VSTART];
  523. tmp.t_stopc = tty->termios.c_cc[VSTOP];
  524. tmp.t_eofc = tty->termios.c_cc[VEOF];
  525. tmp.t_brkc = tty->termios.c_cc[VEOL2]; /* what is brkc anyway? */
  526. up_read(&tty->termios_rwsem);
  527. return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  528. }
  529. static int set_tchars(struct tty_struct *tty, struct tchars __user *tchars)
  530. {
  531. struct tchars tmp;
  532. if (copy_from_user(&tmp, tchars, sizeof(tmp)))
  533. return -EFAULT;
  534. down_write(&tty->termios_rwsem);
  535. tty->termios.c_cc[VINTR] = tmp.t_intrc;
  536. tty->termios.c_cc[VQUIT] = tmp.t_quitc;
  537. tty->termios.c_cc[VSTART] = tmp.t_startc;
  538. tty->termios.c_cc[VSTOP] = tmp.t_stopc;
  539. tty->termios.c_cc[VEOF] = tmp.t_eofc;
  540. tty->termios.c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */
  541. up_write(&tty->termios_rwsem);
  542. return 0;
  543. }
  544. #endif
  545. #ifdef TIOCGLTC
  546. static int get_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
  547. {
  548. struct ltchars tmp;
  549. down_read(&tty->termios_rwsem);
  550. tmp.t_suspc = tty->termios.c_cc[VSUSP];
  551. /* what is dsuspc anyway? */
  552. tmp.t_dsuspc = tty->termios.c_cc[VSUSP];
  553. tmp.t_rprntc = tty->termios.c_cc[VREPRINT];
  554. /* what is flushc anyway? */
  555. tmp.t_flushc = tty->termios.c_cc[VEOL2];
  556. tmp.t_werasc = tty->termios.c_cc[VWERASE];
  557. tmp.t_lnextc = tty->termios.c_cc[VLNEXT];
  558. up_read(&tty->termios_rwsem);
  559. return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  560. }
  561. static int set_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
  562. {
  563. struct ltchars tmp;
  564. if (copy_from_user(&tmp, ltchars, sizeof(tmp)))
  565. return -EFAULT;
  566. down_write(&tty->termios_rwsem);
  567. tty->termios.c_cc[VSUSP] = tmp.t_suspc;
  568. /* what is dsuspc anyway? */
  569. tty->termios.c_cc[VEOL2] = tmp.t_dsuspc;
  570. tty->termios.c_cc[VREPRINT] = tmp.t_rprntc;
  571. /* what is flushc anyway? */
  572. tty->termios.c_cc[VEOL2] = tmp.t_flushc;
  573. tty->termios.c_cc[VWERASE] = tmp.t_werasc;
  574. tty->termios.c_cc[VLNEXT] = tmp.t_lnextc;
  575. up_write(&tty->termios_rwsem);
  576. return 0;
  577. }
  578. #endif
  579. /**
  580. * tty_change_softcar - carrier change ioctl helper
  581. * @tty: tty to update
  582. * @arg: enable/disable CLOCAL
  583. *
  584. * Perform a change to the CLOCAL state and call into the driver
  585. * layer to make it visible. All done with the termios rwsem
  586. */
  587. static int tty_change_softcar(struct tty_struct *tty, int arg)
  588. {
  589. int ret = 0;
  590. int bit = arg ? CLOCAL : 0;
  591. struct ktermios old;
  592. down_write(&tty->termios_rwsem);
  593. old = tty->termios;
  594. tty->termios.c_cflag &= ~CLOCAL;
  595. tty->termios.c_cflag |= bit;
  596. if (tty->ops->set_termios)
  597. tty->ops->set_termios(tty, &old);
  598. if (C_CLOCAL(tty) != bit)
  599. ret = -EINVAL;
  600. up_write(&tty->termios_rwsem);
  601. return ret;
  602. }
  603. /**
  604. * tty_mode_ioctl - mode related ioctls
  605. * @tty: tty for the ioctl
  606. * @file: file pointer for the tty
  607. * @cmd: command
  608. * @arg: ioctl argument
  609. *
  610. * Perform non line discipline specific mode control ioctls. This
  611. * is designed to be called by line disciplines to ensure they provide
  612. * consistent mode setting.
  613. */
  614. int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
  615. unsigned int cmd, unsigned long arg)
  616. {
  617. struct tty_struct *real_tty;
  618. void __user *p = (void __user *)arg;
  619. int ret = 0;
  620. struct ktermios kterm;
  621. BUG_ON(file == NULL);
  622. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  623. tty->driver->subtype == PTY_TYPE_MASTER)
  624. real_tty = tty->link;
  625. else
  626. real_tty = tty;
  627. switch (cmd) {
  628. #ifdef TIOCGETP
  629. case TIOCGETP:
  630. return get_sgttyb(real_tty, (struct sgttyb __user *) arg);
  631. case TIOCSETP:
  632. case TIOCSETN:
  633. return set_sgttyb(real_tty, (struct sgttyb __user *) arg);
  634. #endif
  635. #ifdef TIOCGETC
  636. case TIOCGETC:
  637. return get_tchars(real_tty, p);
  638. case TIOCSETC:
  639. return set_tchars(real_tty, p);
  640. #endif
  641. #ifdef TIOCGLTC
  642. case TIOCGLTC:
  643. return get_ltchars(real_tty, p);
  644. case TIOCSLTC:
  645. return set_ltchars(real_tty, p);
  646. #endif
  647. case TCSETSF:
  648. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_OLD);
  649. case TCSETSW:
  650. return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_OLD);
  651. case TCSETS:
  652. return set_termios(real_tty, p, TERMIOS_OLD);
  653. #ifndef TCGETS2
  654. case TCGETS:
  655. copy_termios(real_tty, &kterm);
  656. if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
  657. ret = -EFAULT;
  658. return ret;
  659. #else
  660. case TCGETS:
  661. copy_termios(real_tty, &kterm);
  662. if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
  663. ret = -EFAULT;
  664. return ret;
  665. case TCGETS2:
  666. copy_termios(real_tty, &kterm);
  667. if (kernel_termios_to_user_termios((struct termios2 __user *)arg, &kterm))
  668. ret = -EFAULT;
  669. return ret;
  670. case TCSETSF2:
  671. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT);
  672. case TCSETSW2:
  673. return set_termios(real_tty, p, TERMIOS_WAIT);
  674. case TCSETS2:
  675. return set_termios(real_tty, p, 0);
  676. #endif
  677. case TCGETA:
  678. return get_termio(real_tty, p);
  679. case TCSETAF:
  680. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_TERMIO);
  681. case TCSETAW:
  682. return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_TERMIO);
  683. case TCSETA:
  684. return set_termios(real_tty, p, TERMIOS_TERMIO);
  685. #ifndef TCGETS2
  686. case TIOCGLCKTRMIOS:
  687. copy_termios_locked(real_tty, &kterm);
  688. if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
  689. ret = -EFAULT;
  690. return ret;
  691. case TIOCSLCKTRMIOS:
  692. if (!capable(CAP_SYS_ADMIN))
  693. return -EPERM;
  694. copy_termios_locked(real_tty, &kterm);
  695. if (user_termios_to_kernel_termios(&kterm,
  696. (struct termios __user *) arg))
  697. return -EFAULT;
  698. down_write(&real_tty->termios_rwsem);
  699. real_tty->termios_locked = kterm;
  700. up_write(&real_tty->termios_rwsem);
  701. return 0;
  702. #else
  703. case TIOCGLCKTRMIOS:
  704. copy_termios_locked(real_tty, &kterm);
  705. if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
  706. ret = -EFAULT;
  707. return ret;
  708. case TIOCSLCKTRMIOS:
  709. if (!capable(CAP_SYS_ADMIN))
  710. return -EPERM;
  711. copy_termios_locked(real_tty, &kterm);
  712. if (user_termios_to_kernel_termios_1(&kterm,
  713. (struct termios __user *) arg))
  714. return -EFAULT;
  715. down_write(&real_tty->termios_rwsem);
  716. real_tty->termios_locked = kterm;
  717. up_write(&real_tty->termios_rwsem);
  718. return ret;
  719. #endif
  720. #ifdef TCGETX
  721. case TCGETX: {
  722. struct termiox ktermx;
  723. if (real_tty->termiox == NULL)
  724. return -EINVAL;
  725. down_read(&real_tty->termios_rwsem);
  726. memcpy(&ktermx, real_tty->termiox, sizeof(struct termiox));
  727. up_read(&real_tty->termios_rwsem);
  728. if (copy_to_user(p, &ktermx, sizeof(struct termiox)))
  729. ret = -EFAULT;
  730. return ret;
  731. }
  732. case TCSETX:
  733. return set_termiox(real_tty, p, 0);
  734. case TCSETXW:
  735. return set_termiox(real_tty, p, TERMIOS_WAIT);
  736. case TCSETXF:
  737. return set_termiox(real_tty, p, TERMIOS_FLUSH);
  738. #endif
  739. case TIOCGSOFTCAR:
  740. copy_termios(real_tty, &kterm);
  741. ret = put_user((kterm.c_cflag & CLOCAL) ? 1 : 0,
  742. (int __user *)arg);
  743. return ret;
  744. case TIOCSSOFTCAR:
  745. if (get_user(arg, (unsigned int __user *) arg))
  746. return -EFAULT;
  747. return tty_change_softcar(real_tty, arg);
  748. default:
  749. return -ENOIOCTLCMD;
  750. }
  751. }
  752. EXPORT_SYMBOL_GPL(tty_mode_ioctl);
  753. /* Caller guarantees ldisc reference is held */
  754. static int __tty_perform_flush(struct tty_struct *tty, unsigned long arg)
  755. {
  756. struct tty_ldisc *ld = tty->ldisc;
  757. switch (arg) {
  758. case TCIFLUSH:
  759. if (ld && ld->ops->flush_buffer) {
  760. ld->ops->flush_buffer(tty);
  761. tty_unthrottle(tty);
  762. }
  763. break;
  764. case TCIOFLUSH:
  765. if (ld && ld->ops->flush_buffer) {
  766. ld->ops->flush_buffer(tty);
  767. tty_unthrottle(tty);
  768. }
  769. /* fall through */
  770. case TCOFLUSH:
  771. tty_driver_flush_buffer(tty);
  772. break;
  773. default:
  774. return -EINVAL;
  775. }
  776. return 0;
  777. }
  778. int tty_perform_flush(struct tty_struct *tty, unsigned long arg)
  779. {
  780. struct tty_ldisc *ld;
  781. int retval = tty_check_change(tty);
  782. if (retval)
  783. return retval;
  784. ld = tty_ldisc_ref_wait(tty);
  785. retval = __tty_perform_flush(tty, arg);
  786. if (ld)
  787. tty_ldisc_deref(ld);
  788. return retval;
  789. }
  790. EXPORT_SYMBOL_GPL(tty_perform_flush);
  791. int n_tty_ioctl_helper(struct tty_struct *tty, struct file *file,
  792. unsigned int cmd, unsigned long arg)
  793. {
  794. int retval;
  795. switch (cmd) {
  796. case TCXONC:
  797. retval = tty_check_change(tty);
  798. if (retval)
  799. return retval;
  800. switch (arg) {
  801. case TCOOFF:
  802. spin_lock_irq(&tty->flow_lock);
  803. if (!tty->flow_stopped) {
  804. tty->flow_stopped = 1;
  805. __stop_tty(tty);
  806. }
  807. spin_unlock_irq(&tty->flow_lock);
  808. break;
  809. case TCOON:
  810. spin_lock_irq(&tty->flow_lock);
  811. if (tty->flow_stopped) {
  812. tty->flow_stopped = 0;
  813. __start_tty(tty);
  814. }
  815. spin_unlock_irq(&tty->flow_lock);
  816. break;
  817. case TCIOFF:
  818. if (STOP_CHAR(tty) != __DISABLED_CHAR)
  819. retval = tty_send_xchar(tty, STOP_CHAR(tty));
  820. break;
  821. case TCION:
  822. if (START_CHAR(tty) != __DISABLED_CHAR)
  823. retval = tty_send_xchar(tty, START_CHAR(tty));
  824. break;
  825. default:
  826. return -EINVAL;
  827. }
  828. return retval;
  829. case TCFLSH:
  830. retval = tty_check_change(tty);
  831. if (retval)
  832. return retval;
  833. return __tty_perform_flush(tty, arg);
  834. default:
  835. /* Try the mode commands */
  836. return tty_mode_ioctl(tty, file, cmd, arg);
  837. }
  838. }
  839. EXPORT_SYMBOL(n_tty_ioctl_helper);