tty_ioctl.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  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.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 <asm/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. * Routine which returns the baud rate of the tty
  227. *
  228. * Note that the baud_table needs to be kept in sync with the
  229. * include/asm/termbits.h file.
  230. */
  231. static const speed_t baud_table[] = {
  232. 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
  233. 9600, 19200, 38400, 57600, 115200, 230400, 460800,
  234. #ifdef __sparc__
  235. 76800, 153600, 307200, 614400, 921600
  236. #else
  237. 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
  238. 2500000, 3000000, 3500000, 4000000
  239. #endif
  240. };
  241. #ifndef __sparc__
  242. static const tcflag_t baud_bits[] = {
  243. B0, B50, B75, B110, B134, B150, B200, B300, B600,
  244. B1200, B1800, B2400, B4800, B9600, B19200, B38400,
  245. B57600, B115200, B230400, B460800, B500000, B576000,
  246. B921600, B1000000, B1152000, B1500000, B2000000, B2500000,
  247. B3000000, B3500000, B4000000
  248. };
  249. #else
  250. static const tcflag_t baud_bits[] = {
  251. B0, B50, B75, B110, B134, B150, B200, B300, B600,
  252. B1200, B1800, B2400, B4800, B9600, B19200, B38400,
  253. B57600, B115200, B230400, B460800, B76800, B153600,
  254. B307200, B614400, B921600
  255. };
  256. #endif
  257. static int n_baud_table = ARRAY_SIZE(baud_table);
  258. /**
  259. * tty_termios_baud_rate
  260. * @termios: termios structure
  261. *
  262. * Convert termios baud rate data into a speed. This should be called
  263. * with the termios lock held if this termios is a terminal termios
  264. * structure. May change the termios data. Device drivers can call this
  265. * function but should use ->c_[io]speed directly as they are updated.
  266. *
  267. * Locking: none
  268. */
  269. speed_t tty_termios_baud_rate(struct ktermios *termios)
  270. {
  271. unsigned int cbaud;
  272. cbaud = termios->c_cflag & CBAUD;
  273. #ifdef BOTHER
  274. /* Magic token for arbitrary speed via c_ispeed/c_ospeed */
  275. if (cbaud == BOTHER)
  276. return termios->c_ospeed;
  277. #endif
  278. if (cbaud & CBAUDEX) {
  279. cbaud &= ~CBAUDEX;
  280. if (cbaud < 1 || cbaud + 15 > n_baud_table)
  281. termios->c_cflag &= ~CBAUDEX;
  282. else
  283. cbaud += 15;
  284. }
  285. return cbaud >= n_baud_table ? 0 : baud_table[cbaud];
  286. }
  287. EXPORT_SYMBOL(tty_termios_baud_rate);
  288. /**
  289. * tty_termios_input_baud_rate
  290. * @termios: termios structure
  291. *
  292. * Convert termios baud rate data into a speed. This should be called
  293. * with the termios lock held if this termios is a terminal termios
  294. * structure. May change the termios data. Device drivers can call this
  295. * function but should use ->c_[io]speed directly as they are updated.
  296. *
  297. * Locking: none
  298. */
  299. speed_t tty_termios_input_baud_rate(struct ktermios *termios)
  300. {
  301. #ifdef IBSHIFT
  302. unsigned int cbaud = (termios->c_cflag >> IBSHIFT) & CBAUD;
  303. if (cbaud == B0)
  304. return tty_termios_baud_rate(termios);
  305. /* Magic token for arbitrary speed via c_ispeed*/
  306. if (cbaud == BOTHER)
  307. return termios->c_ispeed;
  308. if (cbaud & CBAUDEX) {
  309. cbaud &= ~CBAUDEX;
  310. if (cbaud < 1 || cbaud + 15 > n_baud_table)
  311. termios->c_cflag &= ~(CBAUDEX << IBSHIFT);
  312. else
  313. cbaud += 15;
  314. }
  315. return cbaud >= n_baud_table ? 0 : baud_table[cbaud];
  316. #else
  317. return tty_termios_baud_rate(termios);
  318. #endif
  319. }
  320. EXPORT_SYMBOL(tty_termios_input_baud_rate);
  321. /**
  322. * tty_termios_encode_baud_rate
  323. * @termios: ktermios structure holding user requested state
  324. * @ispeed: input speed
  325. * @ospeed: output speed
  326. *
  327. * Encode the speeds set into the passed termios structure. This is
  328. * used as a library helper for drivers so that they can report back
  329. * the actual speed selected when it differs from the speed requested
  330. *
  331. * For maximal back compatibility with legacy SYS5/POSIX *nix behaviour
  332. * we need to carefully set the bits when the user does not get the
  333. * desired speed. We allow small margins and preserve as much of possible
  334. * of the input intent to keep compatibility.
  335. *
  336. * Locking: Caller should hold termios lock. This is already held
  337. * when calling this function from the driver termios handler.
  338. *
  339. * The ifdefs deal with platforms whose owners have yet to update them
  340. * and will all go away once this is done.
  341. */
  342. void tty_termios_encode_baud_rate(struct ktermios *termios,
  343. speed_t ibaud, speed_t obaud)
  344. {
  345. int i = 0;
  346. int ifound = -1, ofound = -1;
  347. int iclose = ibaud/50, oclose = obaud/50;
  348. int ibinput = 0;
  349. if (obaud == 0) /* CD dropped */
  350. ibaud = 0; /* Clear ibaud to be sure */
  351. termios->c_ispeed = ibaud;
  352. termios->c_ospeed = obaud;
  353. #ifdef BOTHER
  354. /* If the user asked for a precise weird speed give a precise weird
  355. answer. If they asked for a Bfoo speed they may have problems
  356. digesting non-exact replies so fuzz a bit */
  357. if ((termios->c_cflag & CBAUD) == BOTHER)
  358. oclose = 0;
  359. if (((termios->c_cflag >> IBSHIFT) & CBAUD) == BOTHER)
  360. iclose = 0;
  361. if ((termios->c_cflag >> IBSHIFT) & CBAUD)
  362. ibinput = 1; /* An input speed was specified */
  363. #endif
  364. termios->c_cflag &= ~CBAUD;
  365. /*
  366. * Our goal is to find a close match to the standard baud rate
  367. * returned. Walk the baud rate table and if we get a very close
  368. * match then report back the speed as a POSIX Bxxxx value by
  369. * preference
  370. */
  371. do {
  372. if (obaud - oclose <= baud_table[i] &&
  373. obaud + oclose >= baud_table[i]) {
  374. termios->c_cflag |= baud_bits[i];
  375. ofound = i;
  376. }
  377. if (ibaud - iclose <= baud_table[i] &&
  378. ibaud + iclose >= baud_table[i]) {
  379. /* For the case input == output don't set IBAUD bits
  380. if the user didn't do so */
  381. if (ofound == i && !ibinput)
  382. ifound = i;
  383. #ifdef IBSHIFT
  384. else {
  385. ifound = i;
  386. termios->c_cflag |= (baud_bits[i] << IBSHIFT);
  387. }
  388. #endif
  389. }
  390. } while (++i < n_baud_table);
  391. /*
  392. * If we found no match then use BOTHER if provided or warn
  393. * the user their platform maintainer needs to wake up if not.
  394. */
  395. #ifdef BOTHER
  396. if (ofound == -1)
  397. termios->c_cflag |= BOTHER;
  398. /* Set exact input bits only if the input and output differ or the
  399. user already did */
  400. if (ifound == -1 && (ibaud != obaud || ibinput))
  401. termios->c_cflag |= (BOTHER << IBSHIFT);
  402. #else
  403. if (ifound == -1 || ofound == -1)
  404. pr_warn_once("tty: Unable to return correct speed data as your architecture needs updating.\n");
  405. #endif
  406. }
  407. EXPORT_SYMBOL_GPL(tty_termios_encode_baud_rate);
  408. /**
  409. * tty_encode_baud_rate - set baud rate of the tty
  410. * @ibaud: input baud rate
  411. * @obad: output baud rate
  412. *
  413. * Update the current termios data for the tty with the new speed
  414. * settings. The caller must hold the termios_rwsem for the tty in
  415. * question.
  416. */
  417. void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud, speed_t obaud)
  418. {
  419. tty_termios_encode_baud_rate(&tty->termios, ibaud, obaud);
  420. }
  421. EXPORT_SYMBOL_GPL(tty_encode_baud_rate);
  422. /**
  423. * tty_termios_copy_hw - copy hardware settings
  424. * @new: New termios
  425. * @old: Old termios
  426. *
  427. * Propagate the hardware specific terminal setting bits from
  428. * the old termios structure to the new one. This is used in cases
  429. * where the hardware does not support reconfiguration or as a helper
  430. * in some cases where only minimal reconfiguration is supported
  431. */
  432. void tty_termios_copy_hw(struct ktermios *new, struct ktermios *old)
  433. {
  434. /* The bits a dumb device handles in software. Smart devices need
  435. to always provide a set_termios method */
  436. new->c_cflag &= HUPCL | CREAD | CLOCAL;
  437. new->c_cflag |= old->c_cflag & ~(HUPCL | CREAD | CLOCAL);
  438. new->c_ispeed = old->c_ispeed;
  439. new->c_ospeed = old->c_ospeed;
  440. }
  441. EXPORT_SYMBOL(tty_termios_copy_hw);
  442. /**
  443. * tty_termios_hw_change - check for setting change
  444. * @a: termios
  445. * @b: termios to compare
  446. *
  447. * Check if any of the bits that affect a dumb device have changed
  448. * between the two termios structures, or a speed change is needed.
  449. */
  450. int tty_termios_hw_change(struct ktermios *a, struct ktermios *b)
  451. {
  452. if (a->c_ispeed != b->c_ispeed || a->c_ospeed != b->c_ospeed)
  453. return 1;
  454. if ((a->c_cflag ^ b->c_cflag) & ~(HUPCL | CREAD | CLOCAL))
  455. return 1;
  456. return 0;
  457. }
  458. EXPORT_SYMBOL(tty_termios_hw_change);
  459. /**
  460. * tty_set_termios - update termios values
  461. * @tty: tty to update
  462. * @new_termios: desired new value
  463. *
  464. * Perform updates to the termios values set on this terminal.
  465. * A master pty's termios should never be set.
  466. *
  467. * Locking: termios_rwsem
  468. */
  469. int tty_set_termios(struct tty_struct *tty, struct ktermios *new_termios)
  470. {
  471. struct ktermios old_termios;
  472. struct tty_ldisc *ld;
  473. WARN_ON(tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  474. tty->driver->subtype == PTY_TYPE_MASTER);
  475. /*
  476. * Perform the actual termios internal changes under lock.
  477. */
  478. /* FIXME: we need to decide on some locking/ordering semantics
  479. for the set_termios notification eventually */
  480. down_write(&tty->termios_rwsem);
  481. old_termios = tty->termios;
  482. tty->termios = *new_termios;
  483. unset_locked_termios(tty, &old_termios);
  484. if (tty->ops->set_termios)
  485. tty->ops->set_termios(tty, &old_termios);
  486. else
  487. tty_termios_copy_hw(&tty->termios, &old_termios);
  488. ld = tty_ldisc_ref(tty);
  489. if (ld != NULL) {
  490. if (ld->ops->set_termios)
  491. ld->ops->set_termios(tty, &old_termios);
  492. tty_ldisc_deref(ld);
  493. }
  494. up_write(&tty->termios_rwsem);
  495. return 0;
  496. }
  497. EXPORT_SYMBOL_GPL(tty_set_termios);
  498. /**
  499. * set_termios - set termios values for a tty
  500. * @tty: terminal device
  501. * @arg: user data
  502. * @opt: option information
  503. *
  504. * Helper function to prepare termios data and run necessary other
  505. * functions before using tty_set_termios to do the actual changes.
  506. *
  507. * Locking:
  508. * Called functions take ldisc and termios_rwsem locks
  509. */
  510. static int set_termios(struct tty_struct *tty, void __user *arg, int opt)
  511. {
  512. struct ktermios tmp_termios;
  513. struct tty_ldisc *ld;
  514. int retval = tty_check_change(tty);
  515. if (retval)
  516. return retval;
  517. down_read(&tty->termios_rwsem);
  518. tmp_termios = tty->termios;
  519. up_read(&tty->termios_rwsem);
  520. if (opt & TERMIOS_TERMIO) {
  521. if (user_termio_to_kernel_termios(&tmp_termios,
  522. (struct termio __user *)arg))
  523. return -EFAULT;
  524. #ifdef TCGETS2
  525. } else if (opt & TERMIOS_OLD) {
  526. if (user_termios_to_kernel_termios_1(&tmp_termios,
  527. (struct termios __user *)arg))
  528. return -EFAULT;
  529. } else {
  530. if (user_termios_to_kernel_termios(&tmp_termios,
  531. (struct termios2 __user *)arg))
  532. return -EFAULT;
  533. }
  534. #else
  535. } else if (user_termios_to_kernel_termios(&tmp_termios,
  536. (struct termios __user *)arg))
  537. return -EFAULT;
  538. #endif
  539. /* If old style Bfoo values are used then load c_ispeed/c_ospeed
  540. * with the real speed so its unconditionally usable */
  541. tmp_termios.c_ispeed = tty_termios_input_baud_rate(&tmp_termios);
  542. tmp_termios.c_ospeed = tty_termios_baud_rate(&tmp_termios);
  543. ld = tty_ldisc_ref(tty);
  544. if (ld != NULL) {
  545. if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
  546. ld->ops->flush_buffer(tty);
  547. tty_ldisc_deref(ld);
  548. }
  549. if (opt & TERMIOS_WAIT) {
  550. tty_wait_until_sent(tty, 0);
  551. if (signal_pending(current))
  552. return -ERESTARTSYS;
  553. }
  554. tty_set_termios(tty, &tmp_termios);
  555. /* FIXME: Arguably if tmp_termios == tty->termios AND the
  556. actual requested termios was not tmp_termios then we may
  557. want to return an error as no user requested change has
  558. succeeded */
  559. return 0;
  560. }
  561. static void copy_termios(struct tty_struct *tty, struct ktermios *kterm)
  562. {
  563. down_read(&tty->termios_rwsem);
  564. *kterm = tty->termios;
  565. up_read(&tty->termios_rwsem);
  566. }
  567. static void copy_termios_locked(struct tty_struct *tty, struct ktermios *kterm)
  568. {
  569. down_read(&tty->termios_rwsem);
  570. *kterm = tty->termios_locked;
  571. up_read(&tty->termios_rwsem);
  572. }
  573. static int get_termio(struct tty_struct *tty, struct termio __user *termio)
  574. {
  575. struct ktermios kterm;
  576. copy_termios(tty, &kterm);
  577. if (kernel_termios_to_user_termio(termio, &kterm))
  578. return -EFAULT;
  579. return 0;
  580. }
  581. #ifdef TCGETX
  582. /**
  583. * set_termiox - set termiox fields if possible
  584. * @tty: terminal
  585. * @arg: termiox structure from user
  586. * @opt: option flags for ioctl type
  587. *
  588. * Implement the device calling points for the SYS5 termiox ioctl
  589. * interface in Linux
  590. */
  591. static int set_termiox(struct tty_struct *tty, void __user *arg, int opt)
  592. {
  593. struct termiox tnew;
  594. struct tty_ldisc *ld;
  595. if (tty->termiox == NULL)
  596. return -EINVAL;
  597. if (copy_from_user(&tnew, arg, sizeof(struct termiox)))
  598. return -EFAULT;
  599. ld = tty_ldisc_ref(tty);
  600. if (ld != NULL) {
  601. if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
  602. ld->ops->flush_buffer(tty);
  603. tty_ldisc_deref(ld);
  604. }
  605. if (opt & TERMIOS_WAIT) {
  606. tty_wait_until_sent(tty, 0);
  607. if (signal_pending(current))
  608. return -ERESTARTSYS;
  609. }
  610. down_write(&tty->termios_rwsem);
  611. if (tty->ops->set_termiox)
  612. tty->ops->set_termiox(tty, &tnew);
  613. up_write(&tty->termios_rwsem);
  614. return 0;
  615. }
  616. #endif
  617. #ifdef TIOCGETP
  618. /*
  619. * These are deprecated, but there is limited support..
  620. *
  621. * The "sg_flags" translation is a joke..
  622. */
  623. static int get_sgflags(struct tty_struct *tty)
  624. {
  625. int flags = 0;
  626. if (!L_ICANON(tty)) {
  627. if (L_ISIG(tty))
  628. flags |= 0x02; /* cbreak */
  629. else
  630. flags |= 0x20; /* raw */
  631. }
  632. if (L_ECHO(tty))
  633. flags |= 0x08; /* echo */
  634. if (O_OPOST(tty))
  635. if (O_ONLCR(tty))
  636. flags |= 0x10; /* crmod */
  637. return flags;
  638. }
  639. static int get_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
  640. {
  641. struct sgttyb tmp;
  642. down_read(&tty->termios_rwsem);
  643. tmp.sg_ispeed = tty->termios.c_ispeed;
  644. tmp.sg_ospeed = tty->termios.c_ospeed;
  645. tmp.sg_erase = tty->termios.c_cc[VERASE];
  646. tmp.sg_kill = tty->termios.c_cc[VKILL];
  647. tmp.sg_flags = get_sgflags(tty);
  648. up_read(&tty->termios_rwsem);
  649. return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  650. }
  651. static void set_sgflags(struct ktermios *termios, int flags)
  652. {
  653. termios->c_iflag = ICRNL | IXON;
  654. termios->c_oflag = 0;
  655. termios->c_lflag = ISIG | ICANON;
  656. if (flags & 0x02) { /* cbreak */
  657. termios->c_iflag = 0;
  658. termios->c_lflag &= ~ICANON;
  659. }
  660. if (flags & 0x08) { /* echo */
  661. termios->c_lflag |= ECHO | ECHOE | ECHOK |
  662. ECHOCTL | ECHOKE | IEXTEN;
  663. }
  664. if (flags & 0x10) { /* crmod */
  665. termios->c_oflag |= OPOST | ONLCR;
  666. }
  667. if (flags & 0x20) { /* raw */
  668. termios->c_iflag = 0;
  669. termios->c_lflag &= ~(ISIG | ICANON);
  670. }
  671. if (!(termios->c_lflag & ICANON)) {
  672. termios->c_cc[VMIN] = 1;
  673. termios->c_cc[VTIME] = 0;
  674. }
  675. }
  676. /**
  677. * set_sgttyb - set legacy terminal values
  678. * @tty: tty structure
  679. * @sgttyb: pointer to old style terminal structure
  680. *
  681. * Updates a terminal from the legacy BSD style terminal information
  682. * structure.
  683. *
  684. * Locking: termios_rwsem
  685. */
  686. static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
  687. {
  688. int retval;
  689. struct sgttyb tmp;
  690. struct ktermios termios;
  691. retval = tty_check_change(tty);
  692. if (retval)
  693. return retval;
  694. if (copy_from_user(&tmp, sgttyb, sizeof(tmp)))
  695. return -EFAULT;
  696. down_write(&tty->termios_rwsem);
  697. termios = tty->termios;
  698. termios.c_cc[VERASE] = tmp.sg_erase;
  699. termios.c_cc[VKILL] = tmp.sg_kill;
  700. set_sgflags(&termios, tmp.sg_flags);
  701. /* Try and encode into Bfoo format */
  702. #ifdef BOTHER
  703. tty_termios_encode_baud_rate(&termios, termios.c_ispeed,
  704. termios.c_ospeed);
  705. #endif
  706. up_write(&tty->termios_rwsem);
  707. tty_set_termios(tty, &termios);
  708. return 0;
  709. }
  710. #endif
  711. #ifdef TIOCGETC
  712. static int get_tchars(struct tty_struct *tty, struct tchars __user *tchars)
  713. {
  714. struct tchars tmp;
  715. down_read(&tty->termios_rwsem);
  716. tmp.t_intrc = tty->termios.c_cc[VINTR];
  717. tmp.t_quitc = tty->termios.c_cc[VQUIT];
  718. tmp.t_startc = tty->termios.c_cc[VSTART];
  719. tmp.t_stopc = tty->termios.c_cc[VSTOP];
  720. tmp.t_eofc = tty->termios.c_cc[VEOF];
  721. tmp.t_brkc = tty->termios.c_cc[VEOL2]; /* what is brkc anyway? */
  722. up_read(&tty->termios_rwsem);
  723. return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  724. }
  725. static int set_tchars(struct tty_struct *tty, struct tchars __user *tchars)
  726. {
  727. struct tchars tmp;
  728. if (copy_from_user(&tmp, tchars, sizeof(tmp)))
  729. return -EFAULT;
  730. down_write(&tty->termios_rwsem);
  731. tty->termios.c_cc[VINTR] = tmp.t_intrc;
  732. tty->termios.c_cc[VQUIT] = tmp.t_quitc;
  733. tty->termios.c_cc[VSTART] = tmp.t_startc;
  734. tty->termios.c_cc[VSTOP] = tmp.t_stopc;
  735. tty->termios.c_cc[VEOF] = tmp.t_eofc;
  736. tty->termios.c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */
  737. up_write(&tty->termios_rwsem);
  738. return 0;
  739. }
  740. #endif
  741. #ifdef TIOCGLTC
  742. static int get_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
  743. {
  744. struct ltchars tmp;
  745. down_read(&tty->termios_rwsem);
  746. tmp.t_suspc = tty->termios.c_cc[VSUSP];
  747. /* what is dsuspc anyway? */
  748. tmp.t_dsuspc = tty->termios.c_cc[VSUSP];
  749. tmp.t_rprntc = tty->termios.c_cc[VREPRINT];
  750. /* what is flushc anyway? */
  751. tmp.t_flushc = tty->termios.c_cc[VEOL2];
  752. tmp.t_werasc = tty->termios.c_cc[VWERASE];
  753. tmp.t_lnextc = tty->termios.c_cc[VLNEXT];
  754. up_read(&tty->termios_rwsem);
  755. return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  756. }
  757. static int set_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
  758. {
  759. struct ltchars tmp;
  760. if (copy_from_user(&tmp, ltchars, sizeof(tmp)))
  761. return -EFAULT;
  762. down_write(&tty->termios_rwsem);
  763. tty->termios.c_cc[VSUSP] = tmp.t_suspc;
  764. /* what is dsuspc anyway? */
  765. tty->termios.c_cc[VEOL2] = tmp.t_dsuspc;
  766. tty->termios.c_cc[VREPRINT] = tmp.t_rprntc;
  767. /* what is flushc anyway? */
  768. tty->termios.c_cc[VEOL2] = tmp.t_flushc;
  769. tty->termios.c_cc[VWERASE] = tmp.t_werasc;
  770. tty->termios.c_cc[VLNEXT] = tmp.t_lnextc;
  771. up_write(&tty->termios_rwsem);
  772. return 0;
  773. }
  774. #endif
  775. /**
  776. * tty_change_softcar - carrier change ioctl helper
  777. * @tty: tty to update
  778. * @arg: enable/disable CLOCAL
  779. *
  780. * Perform a change to the CLOCAL state and call into the driver
  781. * layer to make it visible. All done with the termios rwsem
  782. */
  783. static int tty_change_softcar(struct tty_struct *tty, int arg)
  784. {
  785. int ret = 0;
  786. int bit = arg ? CLOCAL : 0;
  787. struct ktermios old;
  788. down_write(&tty->termios_rwsem);
  789. old = tty->termios;
  790. tty->termios.c_cflag &= ~CLOCAL;
  791. tty->termios.c_cflag |= bit;
  792. if (tty->ops->set_termios)
  793. tty->ops->set_termios(tty, &old);
  794. if (C_CLOCAL(tty) != bit)
  795. ret = -EINVAL;
  796. up_write(&tty->termios_rwsem);
  797. return ret;
  798. }
  799. /**
  800. * tty_mode_ioctl - mode related ioctls
  801. * @tty: tty for the ioctl
  802. * @file: file pointer for the tty
  803. * @cmd: command
  804. * @arg: ioctl argument
  805. *
  806. * Perform non line discipline specific mode control ioctls. This
  807. * is designed to be called by line disciplines to ensure they provide
  808. * consistent mode setting.
  809. */
  810. int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
  811. unsigned int cmd, unsigned long arg)
  812. {
  813. struct tty_struct *real_tty;
  814. void __user *p = (void __user *)arg;
  815. int ret = 0;
  816. struct ktermios kterm;
  817. BUG_ON(file == NULL);
  818. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  819. tty->driver->subtype == PTY_TYPE_MASTER)
  820. real_tty = tty->link;
  821. else
  822. real_tty = tty;
  823. switch (cmd) {
  824. #ifdef TIOCGETP
  825. case TIOCGETP:
  826. return get_sgttyb(real_tty, (struct sgttyb __user *) arg);
  827. case TIOCSETP:
  828. case TIOCSETN:
  829. return set_sgttyb(real_tty, (struct sgttyb __user *) arg);
  830. #endif
  831. #ifdef TIOCGETC
  832. case TIOCGETC:
  833. return get_tchars(real_tty, p);
  834. case TIOCSETC:
  835. return set_tchars(real_tty, p);
  836. #endif
  837. #ifdef TIOCGLTC
  838. case TIOCGLTC:
  839. return get_ltchars(real_tty, p);
  840. case TIOCSLTC:
  841. return set_ltchars(real_tty, p);
  842. #endif
  843. case TCSETSF:
  844. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_OLD);
  845. case TCSETSW:
  846. return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_OLD);
  847. case TCSETS:
  848. return set_termios(real_tty, p, TERMIOS_OLD);
  849. #ifndef TCGETS2
  850. case TCGETS:
  851. copy_termios(real_tty, &kterm);
  852. if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
  853. ret = -EFAULT;
  854. return ret;
  855. #else
  856. case TCGETS:
  857. copy_termios(real_tty, &kterm);
  858. if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
  859. ret = -EFAULT;
  860. return ret;
  861. case TCGETS2:
  862. copy_termios(real_tty, &kterm);
  863. if (kernel_termios_to_user_termios((struct termios2 __user *)arg, &kterm))
  864. ret = -EFAULT;
  865. return ret;
  866. case TCSETSF2:
  867. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT);
  868. case TCSETSW2:
  869. return set_termios(real_tty, p, TERMIOS_WAIT);
  870. case TCSETS2:
  871. return set_termios(real_tty, p, 0);
  872. #endif
  873. case TCGETA:
  874. return get_termio(real_tty, p);
  875. case TCSETAF:
  876. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_TERMIO);
  877. case TCSETAW:
  878. return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_TERMIO);
  879. case TCSETA:
  880. return set_termios(real_tty, p, TERMIOS_TERMIO);
  881. #ifndef TCGETS2
  882. case TIOCGLCKTRMIOS:
  883. copy_termios_locked(real_tty, &kterm);
  884. if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
  885. ret = -EFAULT;
  886. return ret;
  887. case TIOCSLCKTRMIOS:
  888. if (!capable(CAP_SYS_ADMIN))
  889. return -EPERM;
  890. copy_termios_locked(real_tty, &kterm);
  891. if (user_termios_to_kernel_termios(&kterm,
  892. (struct termios __user *) arg))
  893. return -EFAULT;
  894. down_write(&real_tty->termios_rwsem);
  895. real_tty->termios_locked = kterm;
  896. up_write(&real_tty->termios_rwsem);
  897. return 0;
  898. #else
  899. case TIOCGLCKTRMIOS:
  900. copy_termios_locked(real_tty, &kterm);
  901. if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
  902. ret = -EFAULT;
  903. return ret;
  904. case TIOCSLCKTRMIOS:
  905. if (!capable(CAP_SYS_ADMIN))
  906. return -EPERM;
  907. copy_termios_locked(real_tty, &kterm);
  908. if (user_termios_to_kernel_termios_1(&kterm,
  909. (struct termios __user *) arg))
  910. return -EFAULT;
  911. down_write(&real_tty->termios_rwsem);
  912. real_tty->termios_locked = kterm;
  913. up_write(&real_tty->termios_rwsem);
  914. return ret;
  915. #endif
  916. #ifdef TCGETX
  917. case TCGETX: {
  918. struct termiox ktermx;
  919. if (real_tty->termiox == NULL)
  920. return -EINVAL;
  921. down_read(&real_tty->termios_rwsem);
  922. memcpy(&ktermx, real_tty->termiox, sizeof(struct termiox));
  923. up_read(&real_tty->termios_rwsem);
  924. if (copy_to_user(p, &ktermx, sizeof(struct termiox)))
  925. ret = -EFAULT;
  926. return ret;
  927. }
  928. case TCSETX:
  929. return set_termiox(real_tty, p, 0);
  930. case TCSETXW:
  931. return set_termiox(real_tty, p, TERMIOS_WAIT);
  932. case TCSETXF:
  933. return set_termiox(real_tty, p, TERMIOS_FLUSH);
  934. #endif
  935. case TIOCGSOFTCAR:
  936. copy_termios(real_tty, &kterm);
  937. ret = put_user((kterm.c_cflag & CLOCAL) ? 1 : 0,
  938. (int __user *)arg);
  939. return ret;
  940. case TIOCSSOFTCAR:
  941. if (get_user(arg, (unsigned int __user *) arg))
  942. return -EFAULT;
  943. return tty_change_softcar(real_tty, arg);
  944. default:
  945. return -ENOIOCTLCMD;
  946. }
  947. }
  948. EXPORT_SYMBOL_GPL(tty_mode_ioctl);
  949. /* Caller guarantees ldisc reference is held */
  950. static int __tty_perform_flush(struct tty_struct *tty, unsigned long arg)
  951. {
  952. struct tty_ldisc *ld = tty->ldisc;
  953. switch (arg) {
  954. case TCIFLUSH:
  955. if (ld && ld->ops->flush_buffer) {
  956. ld->ops->flush_buffer(tty);
  957. tty_unthrottle(tty);
  958. }
  959. break;
  960. case TCIOFLUSH:
  961. if (ld && ld->ops->flush_buffer) {
  962. ld->ops->flush_buffer(tty);
  963. tty_unthrottle(tty);
  964. }
  965. /* fall through */
  966. case TCOFLUSH:
  967. tty_driver_flush_buffer(tty);
  968. break;
  969. default:
  970. return -EINVAL;
  971. }
  972. return 0;
  973. }
  974. int tty_perform_flush(struct tty_struct *tty, unsigned long arg)
  975. {
  976. struct tty_ldisc *ld;
  977. int retval = tty_check_change(tty);
  978. if (retval)
  979. return retval;
  980. ld = tty_ldisc_ref_wait(tty);
  981. retval = __tty_perform_flush(tty, arg);
  982. if (ld)
  983. tty_ldisc_deref(ld);
  984. return retval;
  985. }
  986. EXPORT_SYMBOL_GPL(tty_perform_flush);
  987. int n_tty_ioctl_helper(struct tty_struct *tty, struct file *file,
  988. unsigned int cmd, unsigned long arg)
  989. {
  990. int retval;
  991. switch (cmd) {
  992. case TCXONC:
  993. retval = tty_check_change(tty);
  994. if (retval)
  995. return retval;
  996. switch (arg) {
  997. case TCOOFF:
  998. spin_lock_irq(&tty->flow_lock);
  999. if (!tty->flow_stopped) {
  1000. tty->flow_stopped = 1;
  1001. __stop_tty(tty);
  1002. }
  1003. spin_unlock_irq(&tty->flow_lock);
  1004. break;
  1005. case TCOON:
  1006. spin_lock_irq(&tty->flow_lock);
  1007. if (tty->flow_stopped) {
  1008. tty->flow_stopped = 0;
  1009. __start_tty(tty);
  1010. }
  1011. spin_unlock_irq(&tty->flow_lock);
  1012. break;
  1013. case TCIOFF:
  1014. if (STOP_CHAR(tty) != __DISABLED_CHAR)
  1015. retval = tty_send_xchar(tty, STOP_CHAR(tty));
  1016. break;
  1017. case TCION:
  1018. if (START_CHAR(tty) != __DISABLED_CHAR)
  1019. retval = tty_send_xchar(tty, START_CHAR(tty));
  1020. break;
  1021. default:
  1022. return -EINVAL;
  1023. }
  1024. return retval;
  1025. case TCFLSH:
  1026. retval = tty_check_change(tty);
  1027. if (retval)
  1028. return retval;
  1029. return __tty_perform_flush(tty, arg);
  1030. default:
  1031. /* Try the mode commands */
  1032. return tty_mode_ioctl(tty, file, cmd, arg);
  1033. }
  1034. }
  1035. EXPORT_SYMBOL(n_tty_ioctl_helper);
  1036. #ifdef CONFIG_COMPAT
  1037. long n_tty_compat_ioctl_helper(struct tty_struct *tty, struct file *file,
  1038. unsigned int cmd, unsigned long arg)
  1039. {
  1040. switch (cmd) {
  1041. case TIOCGLCKTRMIOS:
  1042. case TIOCSLCKTRMIOS:
  1043. return tty_mode_ioctl(tty, file, cmd, (unsigned long) compat_ptr(arg));
  1044. default:
  1045. return -ENOIOCTLCMD;
  1046. }
  1047. }
  1048. EXPORT_SYMBOL(n_tty_compat_ioctl_helper);
  1049. #endif