tty_ioctl.c 31 KB

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