tty_ioctl.c 15 KB

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