tty_ioctl.c 25 KB

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