tty_ioctl.c 24 KB

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