tty_ioctl.c 25 KB

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