tty_ioctl.c 30 KB

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