tty_ioctl.c 14 KB

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