tty_ioctl.c 27 KB

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