tty_ioctl.c 30 KB

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