tty_ioctl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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 <asm/io.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/system.h>
  25. #undef TTY_DEBUG_WAIT_UNTIL_SENT
  26. #undef DEBUG
  27. /*
  28. * Internal flag options for termios setting behavior
  29. */
  30. #define TERMIOS_FLUSH 1
  31. #define TERMIOS_WAIT 2
  32. #define TERMIOS_TERMIO 4
  33. /**
  34. * tty_wait_until_sent - wait for I/O to finish
  35. * @tty: tty we are waiting for
  36. * @timeout: how long we will wait
  37. *
  38. * Wait for characters pending in a tty driver to hit the wire, or
  39. * for a timeout to occur (eg due to flow control)
  40. *
  41. * Locking: none
  42. */
  43. void tty_wait_until_sent(struct tty_struct * tty, long timeout)
  44. {
  45. DECLARE_WAITQUEUE(wait, current);
  46. #ifdef TTY_DEBUG_WAIT_UNTIL_SENT
  47. char buf[64];
  48. printk(KERN_DEBUG "%s wait until sent...\n", tty_name(tty, buf));
  49. #endif
  50. if (!tty->driver->chars_in_buffer)
  51. return;
  52. add_wait_queue(&tty->write_wait, &wait);
  53. if (!timeout)
  54. timeout = MAX_SCHEDULE_TIMEOUT;
  55. do {
  56. #ifdef TTY_DEBUG_WAIT_UNTIL_SENT
  57. printk(KERN_DEBUG "waiting %s...(%d)\n", tty_name(tty, buf),
  58. tty->driver->chars_in_buffer(tty));
  59. #endif
  60. set_current_state(TASK_INTERRUPTIBLE);
  61. if (signal_pending(current))
  62. goto stop_waiting;
  63. if (!tty->driver->chars_in_buffer(tty))
  64. break;
  65. timeout = schedule_timeout(timeout);
  66. } while (timeout);
  67. if (tty->driver->wait_until_sent)
  68. tty->driver->wait_until_sent(tty, timeout);
  69. stop_waiting:
  70. set_current_state(TASK_RUNNING);
  71. remove_wait_queue(&tty->write_wait, &wait);
  72. }
  73. EXPORT_SYMBOL(tty_wait_until_sent);
  74. static void unset_locked_termios(struct termios *termios,
  75. struct termios *old,
  76. struct termios *locked)
  77. {
  78. int i;
  79. #define NOSET_MASK(x,y,z) (x = ((x) & ~(z)) | ((y) & (z)))
  80. if (!locked) {
  81. printk(KERN_WARNING "Warning?!? termios_locked is NULL.\n");
  82. return;
  83. }
  84. NOSET_MASK(termios->c_iflag, old->c_iflag, locked->c_iflag);
  85. NOSET_MASK(termios->c_oflag, old->c_oflag, locked->c_oflag);
  86. NOSET_MASK(termios->c_cflag, old->c_cflag, locked->c_cflag);
  87. NOSET_MASK(termios->c_lflag, old->c_lflag, locked->c_lflag);
  88. termios->c_line = locked->c_line ? old->c_line : termios->c_line;
  89. for (i=0; i < NCCS; i++)
  90. termios->c_cc[i] = locked->c_cc[i] ?
  91. old->c_cc[i] : termios->c_cc[i];
  92. }
  93. /**
  94. * change_termios - update termios values
  95. * @tty: tty to update
  96. * @new_termios: desired new value
  97. *
  98. * Perform updates to the termios values set on this terminal. There
  99. * is a bit of layering violation here with n_tty in terms of the
  100. * internal knowledge of this function.
  101. *
  102. * Locking: termios_sem
  103. */
  104. static void change_termios(struct tty_struct * tty, struct termios * new_termios)
  105. {
  106. int canon_change;
  107. struct termios old_termios = *tty->termios;
  108. struct tty_ldisc *ld;
  109. /*
  110. * Perform the actual termios internal changes under lock.
  111. */
  112. /* FIXME: we need to decide on some locking/ordering semantics
  113. for the set_termios notification eventually */
  114. down(&tty->termios_sem);
  115. *tty->termios = *new_termios;
  116. unset_locked_termios(tty->termios, &old_termios, tty->termios_locked);
  117. canon_change = (old_termios.c_lflag ^ tty->termios->c_lflag) & ICANON;
  118. if (canon_change) {
  119. memset(&tty->read_flags, 0, sizeof tty->read_flags);
  120. tty->canon_head = tty->read_tail;
  121. tty->canon_data = 0;
  122. tty->erasing = 0;
  123. }
  124. if (canon_change && !L_ICANON(tty) && tty->read_cnt)
  125. /* Get characters left over from canonical mode. */
  126. wake_up_interruptible(&tty->read_wait);
  127. /* See if packet mode change of state. */
  128. if (tty->link && tty->link->packet) {
  129. int old_flow = ((old_termios.c_iflag & IXON) &&
  130. (old_termios.c_cc[VSTOP] == '\023') &&
  131. (old_termios.c_cc[VSTART] == '\021'));
  132. int new_flow = (I_IXON(tty) &&
  133. STOP_CHAR(tty) == '\023' &&
  134. START_CHAR(tty) == '\021');
  135. if (old_flow != new_flow) {
  136. tty->ctrl_status &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP);
  137. if (new_flow)
  138. tty->ctrl_status |= TIOCPKT_DOSTOP;
  139. else
  140. tty->ctrl_status |= TIOCPKT_NOSTOP;
  141. wake_up_interruptible(&tty->link->read_wait);
  142. }
  143. }
  144. if (tty->driver->set_termios)
  145. (*tty->driver->set_termios)(tty, &old_termios);
  146. ld = tty_ldisc_ref(tty);
  147. if (ld != NULL) {
  148. if (ld->set_termios)
  149. (ld->set_termios)(tty, &old_termios);
  150. tty_ldisc_deref(ld);
  151. }
  152. up(&tty->termios_sem);
  153. }
  154. /**
  155. * set_termios - set termios values for a tty
  156. * @tty: terminal device
  157. * @arg: user data
  158. * @opt: option information
  159. *
  160. * Helper function to prepare termios data and run neccessary other
  161. * functions before using change_termios to do the actual changes.
  162. *
  163. * Locking:
  164. * Called functions take ldisc and termios_sem locks
  165. */
  166. static int set_termios(struct tty_struct * tty, void __user *arg, int opt)
  167. {
  168. struct termios tmp_termios;
  169. struct tty_ldisc *ld;
  170. int retval = tty_check_change(tty);
  171. if (retval)
  172. return retval;
  173. if (opt & TERMIOS_TERMIO) {
  174. memcpy(&tmp_termios, tty->termios, sizeof(struct termios));
  175. if (user_termio_to_kernel_termios(&tmp_termios,
  176. (struct termio __user *)arg))
  177. return -EFAULT;
  178. } else {
  179. if (user_termios_to_kernel_termios(&tmp_termios,
  180. (struct termios __user *)arg))
  181. return -EFAULT;
  182. }
  183. ld = tty_ldisc_ref(tty);
  184. if (ld != NULL) {
  185. if ((opt & TERMIOS_FLUSH) && ld->flush_buffer)
  186. ld->flush_buffer(tty);
  187. tty_ldisc_deref(ld);
  188. }
  189. if (opt & TERMIOS_WAIT) {
  190. tty_wait_until_sent(tty, 0);
  191. if (signal_pending(current))
  192. return -EINTR;
  193. }
  194. change_termios(tty, &tmp_termios);
  195. return 0;
  196. }
  197. static int get_termio(struct tty_struct * tty, struct termio __user * termio)
  198. {
  199. if (kernel_termios_to_user_termio(termio, tty->termios))
  200. return -EFAULT;
  201. return 0;
  202. }
  203. static unsigned long inq_canon(struct tty_struct * tty)
  204. {
  205. int nr, head, tail;
  206. if (!tty->canon_data || !tty->read_buf)
  207. return 0;
  208. head = tty->canon_head;
  209. tail = tty->read_tail;
  210. nr = (head - tail) & (N_TTY_BUF_SIZE-1);
  211. /* Skip EOF-chars.. */
  212. while (head != tail) {
  213. if (test_bit(tail, tty->read_flags) &&
  214. tty->read_buf[tail] == __DISABLED_CHAR)
  215. nr--;
  216. tail = (tail+1) & (N_TTY_BUF_SIZE-1);
  217. }
  218. return nr;
  219. }
  220. #ifdef TIOCGETP
  221. /*
  222. * These are deprecated, but there is limited support..
  223. *
  224. * The "sg_flags" translation is a joke..
  225. */
  226. static int get_sgflags(struct tty_struct * tty)
  227. {
  228. int flags = 0;
  229. if (!(tty->termios->c_lflag & ICANON)) {
  230. if (tty->termios->c_lflag & ISIG)
  231. flags |= 0x02; /* cbreak */
  232. else
  233. flags |= 0x20; /* raw */
  234. }
  235. if (tty->termios->c_lflag & ECHO)
  236. flags |= 0x08; /* echo */
  237. if (tty->termios->c_oflag & OPOST)
  238. if (tty->termios->c_oflag & ONLCR)
  239. flags |= 0x10; /* crmod */
  240. return flags;
  241. }
  242. static int get_sgttyb(struct tty_struct * tty, struct sgttyb __user * sgttyb)
  243. {
  244. struct sgttyb tmp;
  245. down(&tty->termios_sem);
  246. tmp.sg_ispeed = 0;
  247. tmp.sg_ospeed = 0;
  248. tmp.sg_erase = tty->termios->c_cc[VERASE];
  249. tmp.sg_kill = tty->termios->c_cc[VKILL];
  250. tmp.sg_flags = get_sgflags(tty);
  251. up(&tty->termios_sem);
  252. return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  253. }
  254. static void set_sgflags(struct termios * termios, int flags)
  255. {
  256. termios->c_iflag = ICRNL | IXON;
  257. termios->c_oflag = 0;
  258. termios->c_lflag = ISIG | ICANON;
  259. if (flags & 0x02) { /* cbreak */
  260. termios->c_iflag = 0;
  261. termios->c_lflag &= ~ICANON;
  262. }
  263. if (flags & 0x08) { /* echo */
  264. termios->c_lflag |= ECHO | ECHOE | ECHOK |
  265. ECHOCTL | ECHOKE | IEXTEN;
  266. }
  267. if (flags & 0x10) { /* crmod */
  268. termios->c_oflag |= OPOST | ONLCR;
  269. }
  270. if (flags & 0x20) { /* raw */
  271. termios->c_iflag = 0;
  272. termios->c_lflag &= ~(ISIG | ICANON);
  273. }
  274. if (!(termios->c_lflag & ICANON)) {
  275. termios->c_cc[VMIN] = 1;
  276. termios->c_cc[VTIME] = 0;
  277. }
  278. }
  279. /**
  280. * set_sgttyb - set legacy terminal values
  281. * @tty: tty structure
  282. * @sgttyb: pointer to old style terminal structure
  283. *
  284. * Updates a terminal from the legacy BSD style terminal information
  285. * structure.
  286. *
  287. * Locking: termios_sem
  288. */
  289. static int set_sgttyb(struct tty_struct * tty, struct sgttyb __user * sgttyb)
  290. {
  291. int retval;
  292. struct sgttyb tmp;
  293. struct termios termios;
  294. retval = tty_check_change(tty);
  295. if (retval)
  296. return retval;
  297. if (copy_from_user(&tmp, sgttyb, sizeof(tmp)))
  298. return -EFAULT;
  299. down(&tty->termios_sem);
  300. termios = *tty->termios;
  301. termios.c_cc[VERASE] = tmp.sg_erase;
  302. termios.c_cc[VKILL] = tmp.sg_kill;
  303. set_sgflags(&termios, tmp.sg_flags);
  304. up(&tty->termios_sem);
  305. change_termios(tty, &termios);
  306. return 0;
  307. }
  308. #endif
  309. #ifdef TIOCGETC
  310. static int get_tchars(struct tty_struct * tty, struct tchars __user * tchars)
  311. {
  312. struct tchars tmp;
  313. tmp.t_intrc = tty->termios->c_cc[VINTR];
  314. tmp.t_quitc = tty->termios->c_cc[VQUIT];
  315. tmp.t_startc = tty->termios->c_cc[VSTART];
  316. tmp.t_stopc = tty->termios->c_cc[VSTOP];
  317. tmp.t_eofc = tty->termios->c_cc[VEOF];
  318. tmp.t_brkc = tty->termios->c_cc[VEOL2]; /* what is brkc anyway? */
  319. return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  320. }
  321. static int set_tchars(struct tty_struct * tty, struct tchars __user * tchars)
  322. {
  323. struct tchars tmp;
  324. if (copy_from_user(&tmp, tchars, sizeof(tmp)))
  325. return -EFAULT;
  326. tty->termios->c_cc[VINTR] = tmp.t_intrc;
  327. tty->termios->c_cc[VQUIT] = tmp.t_quitc;
  328. tty->termios->c_cc[VSTART] = tmp.t_startc;
  329. tty->termios->c_cc[VSTOP] = tmp.t_stopc;
  330. tty->termios->c_cc[VEOF] = tmp.t_eofc;
  331. tty->termios->c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */
  332. return 0;
  333. }
  334. #endif
  335. #ifdef TIOCGLTC
  336. static int get_ltchars(struct tty_struct * tty, struct ltchars __user * ltchars)
  337. {
  338. struct ltchars tmp;
  339. tmp.t_suspc = tty->termios->c_cc[VSUSP];
  340. tmp.t_dsuspc = tty->termios->c_cc[VSUSP]; /* what is dsuspc anyway? */
  341. tmp.t_rprntc = tty->termios->c_cc[VREPRINT];
  342. tmp.t_flushc = tty->termios->c_cc[VEOL2]; /* what is flushc anyway? */
  343. tmp.t_werasc = tty->termios->c_cc[VWERASE];
  344. tmp.t_lnextc = tty->termios->c_cc[VLNEXT];
  345. return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  346. }
  347. static int set_ltchars(struct tty_struct * tty, struct ltchars __user * ltchars)
  348. {
  349. struct ltchars tmp;
  350. if (copy_from_user(&tmp, ltchars, sizeof(tmp)))
  351. return -EFAULT;
  352. tty->termios->c_cc[VSUSP] = tmp.t_suspc;
  353. tty->termios->c_cc[VEOL2] = tmp.t_dsuspc; /* what is dsuspc anyway? */
  354. tty->termios->c_cc[VREPRINT] = tmp.t_rprntc;
  355. tty->termios->c_cc[VEOL2] = tmp.t_flushc; /* what is flushc anyway? */
  356. tty->termios->c_cc[VWERASE] = tmp.t_werasc;
  357. tty->termios->c_cc[VLNEXT] = tmp.t_lnextc;
  358. return 0;
  359. }
  360. #endif
  361. /**
  362. * send_prio_char - send priority character
  363. *
  364. * Send a high priority character to the tty even if stopped
  365. *
  366. * Locking: none
  367. *
  368. * FIXME: overlapping calls with start/stop tty lose state of tty
  369. */
  370. static void send_prio_char(struct tty_struct *tty, char ch)
  371. {
  372. int was_stopped = tty->stopped;
  373. if (tty->driver->send_xchar) {
  374. tty->driver->send_xchar(tty, ch);
  375. return;
  376. }
  377. if (was_stopped)
  378. start_tty(tty);
  379. tty->driver->write(tty, &ch, 1);
  380. if (was_stopped)
  381. stop_tty(tty);
  382. }
  383. int n_tty_ioctl(struct tty_struct * tty, struct file * file,
  384. unsigned int cmd, unsigned long arg)
  385. {
  386. struct tty_struct * real_tty;
  387. void __user *p = (void __user *)arg;
  388. int retval;
  389. struct tty_ldisc *ld;
  390. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  391. tty->driver->subtype == PTY_TYPE_MASTER)
  392. real_tty = tty->link;
  393. else
  394. real_tty = tty;
  395. switch (cmd) {
  396. #ifdef TIOCGETP
  397. case TIOCGETP:
  398. return get_sgttyb(real_tty, (struct sgttyb __user *) arg);
  399. case TIOCSETP:
  400. case TIOCSETN:
  401. return set_sgttyb(real_tty, (struct sgttyb __user *) arg);
  402. #endif
  403. #ifdef TIOCGETC
  404. case TIOCGETC:
  405. return get_tchars(real_tty, p);
  406. case TIOCSETC:
  407. return set_tchars(real_tty, p);
  408. #endif
  409. #ifdef TIOCGLTC
  410. case TIOCGLTC:
  411. return get_ltchars(real_tty, p);
  412. case TIOCSLTC:
  413. return set_ltchars(real_tty, p);
  414. #endif
  415. case TCGETS:
  416. if (kernel_termios_to_user_termios((struct termios __user *)arg, real_tty->termios))
  417. return -EFAULT;
  418. return 0;
  419. case TCSETSF:
  420. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT);
  421. case TCSETSW:
  422. return set_termios(real_tty, p, TERMIOS_WAIT);
  423. case TCSETS:
  424. return set_termios(real_tty, p, 0);
  425. case TCGETA:
  426. return get_termio(real_tty, p);
  427. case TCSETAF:
  428. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_TERMIO);
  429. case TCSETAW:
  430. return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_TERMIO);
  431. case TCSETA:
  432. return set_termios(real_tty, p, TERMIOS_TERMIO);
  433. case TCXONC:
  434. retval = tty_check_change(tty);
  435. if (retval)
  436. return retval;
  437. switch (arg) {
  438. case TCOOFF:
  439. if (!tty->flow_stopped) {
  440. tty->flow_stopped = 1;
  441. stop_tty(tty);
  442. }
  443. break;
  444. case TCOON:
  445. if (tty->flow_stopped) {
  446. tty->flow_stopped = 0;
  447. start_tty(tty);
  448. }
  449. break;
  450. case TCIOFF:
  451. if (STOP_CHAR(tty) != __DISABLED_CHAR)
  452. send_prio_char(tty, STOP_CHAR(tty));
  453. break;
  454. case TCION:
  455. if (START_CHAR(tty) != __DISABLED_CHAR)
  456. send_prio_char(tty, START_CHAR(tty));
  457. break;
  458. default:
  459. return -EINVAL;
  460. }
  461. return 0;
  462. case TCFLSH:
  463. retval = tty_check_change(tty);
  464. if (retval)
  465. return retval;
  466. ld = tty_ldisc_ref(tty);
  467. switch (arg) {
  468. case TCIFLUSH:
  469. if (ld && ld->flush_buffer)
  470. ld->flush_buffer(tty);
  471. break;
  472. case TCIOFLUSH:
  473. if (ld && ld->flush_buffer)
  474. ld->flush_buffer(tty);
  475. /* fall through */
  476. case TCOFLUSH:
  477. if (tty->driver->flush_buffer)
  478. tty->driver->flush_buffer(tty);
  479. break;
  480. default:
  481. tty_ldisc_deref(ld);
  482. return -EINVAL;
  483. }
  484. tty_ldisc_deref(ld);
  485. return 0;
  486. case TIOCOUTQ:
  487. return put_user(tty->driver->chars_in_buffer ?
  488. tty->driver->chars_in_buffer(tty) : 0,
  489. (int __user *) arg);
  490. case TIOCINQ:
  491. retval = tty->read_cnt;
  492. if (L_ICANON(tty))
  493. retval = inq_canon(tty);
  494. return put_user(retval, (unsigned int __user *) arg);
  495. case TIOCGLCKTRMIOS:
  496. if (kernel_termios_to_user_termios((struct termios __user *)arg, real_tty->termios_locked))
  497. return -EFAULT;
  498. return 0;
  499. case TIOCSLCKTRMIOS:
  500. if (!capable(CAP_SYS_ADMIN))
  501. return -EPERM;
  502. if (user_termios_to_kernel_termios(real_tty->termios_locked, (struct termios __user *) arg))
  503. return -EFAULT;
  504. return 0;
  505. case TIOCPKT:
  506. {
  507. int pktmode;
  508. if (tty->driver->type != TTY_DRIVER_TYPE_PTY ||
  509. tty->driver->subtype != PTY_TYPE_MASTER)
  510. return -ENOTTY;
  511. if (get_user(pktmode, (int __user *) arg))
  512. return -EFAULT;
  513. if (pktmode) {
  514. if (!tty->packet) {
  515. tty->packet = 1;
  516. tty->link->ctrl_status = 0;
  517. }
  518. } else
  519. tty->packet = 0;
  520. return 0;
  521. }
  522. case TIOCGSOFTCAR:
  523. return put_user(C_CLOCAL(tty) ? 1 : 0, (int __user *)arg);
  524. case TIOCSSOFTCAR:
  525. if (get_user(arg, (unsigned int __user *) arg))
  526. return -EFAULT;
  527. down(&tty->termios_sem);
  528. tty->termios->c_cflag =
  529. ((tty->termios->c_cflag & ~CLOCAL) |
  530. (arg ? CLOCAL : 0));
  531. up(&tty->termios_sem);
  532. return 0;
  533. default:
  534. return -ENOIOCTLCMD;
  535. }
  536. }
  537. EXPORT_SYMBOL(n_tty_ioctl);