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