tty_ioctl.c 22 KB

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