tty_ioctl.c 30 KB

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