tty_ioctl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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: termios structure
  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 now input and output speed must agree.
  204. *
  205. * Locking: Caller should hold termios lock. This is already held
  206. * when calling this function from the driver termios handler.
  207. */
  208. void tty_termios_encode_baud_rate(struct ktermios *termios, speed_t ibaud, speed_t obaud)
  209. {
  210. int i = 0;
  211. int ifound = 0, ofound = 0;
  212. termios->c_ispeed = ibaud;
  213. termios->c_ospeed = obaud;
  214. termios->c_cflag &= ~CBAUD;
  215. /* Identical speed means no input encoding (ie B0 << IBSHIFT)*/
  216. if (termios->c_ispeed == termios->c_ospeed)
  217. ifound = 1;
  218. do {
  219. if (obaud == baud_table[i]) {
  220. termios->c_cflag |= baud_bits[i];
  221. ofound = 1;
  222. /* So that if ibaud == obaud we don't set it */
  223. continue;
  224. }
  225. if (ibaud == baud_table[i]) {
  226. termios->c_cflag |= (baud_bits[i] << IBSHIFT);
  227. ifound = 1;
  228. }
  229. }
  230. while(++i < n_baud_table);
  231. if (!ofound)
  232. termios->c_cflag |= BOTHER;
  233. if (!ifound)
  234. termios->c_cflag |= (BOTHER << IBSHIFT);
  235. }
  236. EXPORT_SYMBOL_GPL(tty_termios_encode_baud_rate);
  237. #endif
  238. /**
  239. * tty_get_baud_rate - get tty bit rates
  240. * @tty: tty to query
  241. *
  242. * Returns the baud rate as an integer for this terminal. The
  243. * termios lock must be held by the caller and the terminal bit
  244. * flags may be updated.
  245. *
  246. * Locking: none
  247. */
  248. speed_t tty_get_baud_rate(struct tty_struct *tty)
  249. {
  250. speed_t baud = tty_termios_baud_rate(tty->termios);
  251. if (baud == 38400 && tty->alt_speed) {
  252. if (!tty->warned) {
  253. printk(KERN_WARNING "Use of setserial/setrocket to "
  254. "set SPD_* flags is deprecated\n");
  255. tty->warned = 1;
  256. }
  257. baud = tty->alt_speed;
  258. }
  259. return baud;
  260. }
  261. EXPORT_SYMBOL(tty_get_baud_rate);
  262. /**
  263. * change_termios - update termios values
  264. * @tty: tty to update
  265. * @new_termios: desired new value
  266. *
  267. * Perform updates to the termios values set on this terminal. There
  268. * is a bit of layering violation here with n_tty in terms of the
  269. * internal knowledge of this function.
  270. *
  271. * Locking: termios_sem
  272. */
  273. static void change_termios(struct tty_struct * tty, struct ktermios * new_termios)
  274. {
  275. int canon_change;
  276. struct ktermios old_termios = *tty->termios;
  277. struct tty_ldisc *ld;
  278. /*
  279. * Perform the actual termios internal changes under lock.
  280. */
  281. /* FIXME: we need to decide on some locking/ordering semantics
  282. for the set_termios notification eventually */
  283. mutex_lock(&tty->termios_mutex);
  284. *tty->termios = *new_termios;
  285. unset_locked_termios(tty->termios, &old_termios, tty->termios_locked);
  286. canon_change = (old_termios.c_lflag ^ tty->termios->c_lflag) & ICANON;
  287. if (canon_change) {
  288. memset(&tty->read_flags, 0, sizeof tty->read_flags);
  289. tty->canon_head = tty->read_tail;
  290. tty->canon_data = 0;
  291. tty->erasing = 0;
  292. }
  293. if (canon_change && !L_ICANON(tty) && tty->read_cnt)
  294. /* Get characters left over from canonical mode. */
  295. wake_up_interruptible(&tty->read_wait);
  296. /* See if packet mode change of state. */
  297. if (tty->link && tty->link->packet) {
  298. int old_flow = ((old_termios.c_iflag & IXON) &&
  299. (old_termios.c_cc[VSTOP] == '\023') &&
  300. (old_termios.c_cc[VSTART] == '\021'));
  301. int new_flow = (I_IXON(tty) &&
  302. STOP_CHAR(tty) == '\023' &&
  303. START_CHAR(tty) == '\021');
  304. if (old_flow != new_flow) {
  305. tty->ctrl_status &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP);
  306. if (new_flow)
  307. tty->ctrl_status |= TIOCPKT_DOSTOP;
  308. else
  309. tty->ctrl_status |= TIOCPKT_NOSTOP;
  310. wake_up_interruptible(&tty->link->read_wait);
  311. }
  312. }
  313. if (tty->driver->set_termios)
  314. (*tty->driver->set_termios)(tty, &old_termios);
  315. ld = tty_ldisc_ref(tty);
  316. if (ld != NULL) {
  317. if (ld->set_termios)
  318. (ld->set_termios)(tty, &old_termios);
  319. tty_ldisc_deref(ld);
  320. }
  321. mutex_unlock(&tty->termios_mutex);
  322. }
  323. /**
  324. * set_termios - set termios values for a tty
  325. * @tty: terminal device
  326. * @arg: user data
  327. * @opt: option information
  328. *
  329. * Helper function to prepare termios data and run neccessary other
  330. * functions before using change_termios to do the actual changes.
  331. *
  332. * Locking:
  333. * Called functions take ldisc and termios_sem locks
  334. */
  335. static int set_termios(struct tty_struct * tty, void __user *arg, int opt)
  336. {
  337. struct ktermios tmp_termios;
  338. struct tty_ldisc *ld;
  339. int retval = tty_check_change(tty);
  340. if (retval)
  341. return retval;
  342. memcpy(&tmp_termios, tty->termios, sizeof(struct ktermios));
  343. if (opt & TERMIOS_TERMIO) {
  344. if (user_termio_to_kernel_termios(&tmp_termios,
  345. (struct termio __user *)arg))
  346. return -EFAULT;
  347. #ifdef TCGETS2
  348. } else if (opt & TERMIOS_OLD) {
  349. if (user_termios_to_kernel_termios_1(&tmp_termios,
  350. (struct termios __user *)arg))
  351. return -EFAULT;
  352. } else {
  353. if (user_termios_to_kernel_termios(&tmp_termios,
  354. (struct termios2 __user *)arg))
  355. return -EFAULT;
  356. }
  357. #else
  358. } else if (user_termios_to_kernel_termios(&tmp_termios,
  359. (struct termios __user *)arg))
  360. return -EFAULT;
  361. #endif
  362. /* If old style Bfoo values are used then load c_ispeed/c_ospeed with the real speed
  363. so its unconditionally usable */
  364. tmp_termios.c_ispeed = tty_termios_input_baud_rate(&tmp_termios);
  365. tmp_termios.c_ospeed = tty_termios_baud_rate(&tmp_termios);
  366. ld = tty_ldisc_ref(tty);
  367. if (ld != NULL) {
  368. if ((opt & TERMIOS_FLUSH) && ld->flush_buffer)
  369. ld->flush_buffer(tty);
  370. tty_ldisc_deref(ld);
  371. }
  372. if (opt & TERMIOS_WAIT) {
  373. tty_wait_until_sent(tty, 0);
  374. if (signal_pending(current))
  375. return -EINTR;
  376. }
  377. change_termios(tty, &tmp_termios);
  378. return 0;
  379. }
  380. static int get_termio(struct tty_struct * tty, struct termio __user * termio)
  381. {
  382. if (kernel_termios_to_user_termio(termio, tty->termios))
  383. return -EFAULT;
  384. return 0;
  385. }
  386. static unsigned long inq_canon(struct tty_struct * tty)
  387. {
  388. int nr, head, tail;
  389. if (!tty->canon_data || !tty->read_buf)
  390. return 0;
  391. head = tty->canon_head;
  392. tail = tty->read_tail;
  393. nr = (head - tail) & (N_TTY_BUF_SIZE-1);
  394. /* Skip EOF-chars.. */
  395. while (head != tail) {
  396. if (test_bit(tail, tty->read_flags) &&
  397. tty->read_buf[tail] == __DISABLED_CHAR)
  398. nr--;
  399. tail = (tail+1) & (N_TTY_BUF_SIZE-1);
  400. }
  401. return nr;
  402. }
  403. #ifdef TIOCGETP
  404. /*
  405. * These are deprecated, but there is limited support..
  406. *
  407. * The "sg_flags" translation is a joke..
  408. */
  409. static int get_sgflags(struct tty_struct * tty)
  410. {
  411. int flags = 0;
  412. if (!(tty->termios->c_lflag & ICANON)) {
  413. if (tty->termios->c_lflag & ISIG)
  414. flags |= 0x02; /* cbreak */
  415. else
  416. flags |= 0x20; /* raw */
  417. }
  418. if (tty->termios->c_lflag & ECHO)
  419. flags |= 0x08; /* echo */
  420. if (tty->termios->c_oflag & OPOST)
  421. if (tty->termios->c_oflag & ONLCR)
  422. flags |= 0x10; /* crmod */
  423. return flags;
  424. }
  425. static int get_sgttyb(struct tty_struct * tty, struct sgttyb __user * sgttyb)
  426. {
  427. struct sgttyb tmp;
  428. mutex_lock(&tty->termios_mutex);
  429. tmp.sg_ispeed = tty->termios->c_ispeed;
  430. tmp.sg_ospeed = tty->termios->c_ospeed;
  431. tmp.sg_erase = tty->termios->c_cc[VERASE];
  432. tmp.sg_kill = tty->termios->c_cc[VKILL];
  433. tmp.sg_flags = get_sgflags(tty);
  434. mutex_unlock(&tty->termios_mutex);
  435. return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  436. }
  437. static void set_sgflags(struct ktermios * termios, int flags)
  438. {
  439. termios->c_iflag = ICRNL | IXON;
  440. termios->c_oflag = 0;
  441. termios->c_lflag = ISIG | ICANON;
  442. if (flags & 0x02) { /* cbreak */
  443. termios->c_iflag = 0;
  444. termios->c_lflag &= ~ICANON;
  445. }
  446. if (flags & 0x08) { /* echo */
  447. termios->c_lflag |= ECHO | ECHOE | ECHOK |
  448. ECHOCTL | ECHOKE | IEXTEN;
  449. }
  450. if (flags & 0x10) { /* crmod */
  451. termios->c_oflag |= OPOST | ONLCR;
  452. }
  453. if (flags & 0x20) { /* raw */
  454. termios->c_iflag = 0;
  455. termios->c_lflag &= ~(ISIG | ICANON);
  456. }
  457. if (!(termios->c_lflag & ICANON)) {
  458. termios->c_cc[VMIN] = 1;
  459. termios->c_cc[VTIME] = 0;
  460. }
  461. }
  462. /**
  463. * set_sgttyb - set legacy terminal values
  464. * @tty: tty structure
  465. * @sgttyb: pointer to old style terminal structure
  466. *
  467. * Updates a terminal from the legacy BSD style terminal information
  468. * structure.
  469. *
  470. * Locking: termios_sem
  471. */
  472. static int set_sgttyb(struct tty_struct * tty, struct sgttyb __user * sgttyb)
  473. {
  474. int retval;
  475. struct sgttyb tmp;
  476. struct ktermios termios;
  477. retval = tty_check_change(tty);
  478. if (retval)
  479. return retval;
  480. if (copy_from_user(&tmp, sgttyb, sizeof(tmp)))
  481. return -EFAULT;
  482. mutex_lock(&tty->termios_mutex);
  483. termios = *tty->termios;
  484. termios.c_cc[VERASE] = tmp.sg_erase;
  485. termios.c_cc[VKILL] = tmp.sg_kill;
  486. set_sgflags(&termios, tmp.sg_flags);
  487. /* Try and encode into Bfoo format */
  488. #ifdef BOTHER
  489. tty_termios_encode_baud_rate(&termios, termios.c_ispeed, termios.c_ospeed);
  490. #endif
  491. mutex_unlock(&tty->termios_mutex);
  492. change_termios(tty, &termios);
  493. return 0;
  494. }
  495. #endif
  496. #ifdef TIOCGETC
  497. static int get_tchars(struct tty_struct * tty, struct tchars __user * tchars)
  498. {
  499. struct tchars tmp;
  500. tmp.t_intrc = tty->termios->c_cc[VINTR];
  501. tmp.t_quitc = tty->termios->c_cc[VQUIT];
  502. tmp.t_startc = tty->termios->c_cc[VSTART];
  503. tmp.t_stopc = tty->termios->c_cc[VSTOP];
  504. tmp.t_eofc = tty->termios->c_cc[VEOF];
  505. tmp.t_brkc = tty->termios->c_cc[VEOL2]; /* what is brkc anyway? */
  506. return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  507. }
  508. static int set_tchars(struct tty_struct * tty, struct tchars __user * tchars)
  509. {
  510. struct tchars tmp;
  511. if (copy_from_user(&tmp, tchars, sizeof(tmp)))
  512. return -EFAULT;
  513. tty->termios->c_cc[VINTR] = tmp.t_intrc;
  514. tty->termios->c_cc[VQUIT] = tmp.t_quitc;
  515. tty->termios->c_cc[VSTART] = tmp.t_startc;
  516. tty->termios->c_cc[VSTOP] = tmp.t_stopc;
  517. tty->termios->c_cc[VEOF] = tmp.t_eofc;
  518. tty->termios->c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */
  519. return 0;
  520. }
  521. #endif
  522. #ifdef TIOCGLTC
  523. static int get_ltchars(struct tty_struct * tty, struct ltchars __user * ltchars)
  524. {
  525. struct ltchars tmp;
  526. tmp.t_suspc = tty->termios->c_cc[VSUSP];
  527. tmp.t_dsuspc = tty->termios->c_cc[VSUSP]; /* what is dsuspc anyway? */
  528. tmp.t_rprntc = tty->termios->c_cc[VREPRINT];
  529. tmp.t_flushc = tty->termios->c_cc[VEOL2]; /* what is flushc anyway? */
  530. tmp.t_werasc = tty->termios->c_cc[VWERASE];
  531. tmp.t_lnextc = tty->termios->c_cc[VLNEXT];
  532. return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  533. }
  534. static int set_ltchars(struct tty_struct * tty, struct ltchars __user * ltchars)
  535. {
  536. struct ltchars tmp;
  537. if (copy_from_user(&tmp, ltchars, sizeof(tmp)))
  538. return -EFAULT;
  539. tty->termios->c_cc[VSUSP] = tmp.t_suspc;
  540. tty->termios->c_cc[VEOL2] = tmp.t_dsuspc; /* what is dsuspc anyway? */
  541. tty->termios->c_cc[VREPRINT] = tmp.t_rprntc;
  542. tty->termios->c_cc[VEOL2] = tmp.t_flushc; /* what is flushc anyway? */
  543. tty->termios->c_cc[VWERASE] = tmp.t_werasc;
  544. tty->termios->c_cc[VLNEXT] = tmp.t_lnextc;
  545. return 0;
  546. }
  547. #endif
  548. /**
  549. * send_prio_char - send priority character
  550. *
  551. * Send a high priority character to the tty even if stopped
  552. *
  553. * Locking: none for xchar method, write ordering for write method.
  554. */
  555. static int send_prio_char(struct tty_struct *tty, char ch)
  556. {
  557. int was_stopped = tty->stopped;
  558. if (tty->driver->send_xchar) {
  559. tty->driver->send_xchar(tty, ch);
  560. return 0;
  561. }
  562. if (mutex_lock_interruptible(&tty->atomic_write_lock))
  563. return -ERESTARTSYS;
  564. if (was_stopped)
  565. start_tty(tty);
  566. tty->driver->write(tty, &ch, 1);
  567. if (was_stopped)
  568. stop_tty(tty);
  569. mutex_unlock(&tty->atomic_write_lock);
  570. return 0;
  571. }
  572. int n_tty_ioctl(struct tty_struct * tty, struct file * file,
  573. unsigned int cmd, unsigned long arg)
  574. {
  575. struct tty_struct * real_tty;
  576. void __user *p = (void __user *)arg;
  577. int retval;
  578. struct tty_ldisc *ld;
  579. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  580. tty->driver->subtype == PTY_TYPE_MASTER)
  581. real_tty = tty->link;
  582. else
  583. real_tty = tty;
  584. switch (cmd) {
  585. #ifdef TIOCGETP
  586. case TIOCGETP:
  587. return get_sgttyb(real_tty, (struct sgttyb __user *) arg);
  588. case TIOCSETP:
  589. case TIOCSETN:
  590. return set_sgttyb(real_tty, (struct sgttyb __user *) arg);
  591. #endif
  592. #ifdef TIOCGETC
  593. case TIOCGETC:
  594. return get_tchars(real_tty, p);
  595. case TIOCSETC:
  596. return set_tchars(real_tty, p);
  597. #endif
  598. #ifdef TIOCGLTC
  599. case TIOCGLTC:
  600. return get_ltchars(real_tty, p);
  601. case TIOCSLTC:
  602. return set_ltchars(real_tty, p);
  603. #endif
  604. case TCSETSF:
  605. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_OLD);
  606. case TCSETSW:
  607. return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_OLD);
  608. case TCSETS:
  609. return set_termios(real_tty, p, TERMIOS_OLD);
  610. #ifndef TCGETS2
  611. case TCGETS:
  612. if (kernel_termios_to_user_termios((struct termios __user *)arg, real_tty->termios))
  613. return -EFAULT;
  614. return 0;
  615. #else
  616. case TCGETS:
  617. if (kernel_termios_to_user_termios_1((struct termios __user *)arg, real_tty->termios))
  618. return -EFAULT;
  619. return 0;
  620. case TCGETS2:
  621. if (kernel_termios_to_user_termios((struct termios2 __user *)arg, real_tty->termios))
  622. return -EFAULT;
  623. return 0;
  624. case TCSETSF2:
  625. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT);
  626. case TCSETSW2:
  627. return set_termios(real_tty, p, TERMIOS_WAIT);
  628. case TCSETS2:
  629. return set_termios(real_tty, p, 0);
  630. #endif
  631. case TCGETA:
  632. return get_termio(real_tty, p);
  633. case TCSETAF:
  634. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_TERMIO);
  635. case TCSETAW:
  636. return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_TERMIO);
  637. case TCSETA:
  638. return set_termios(real_tty, p, TERMIOS_TERMIO);
  639. case TCXONC:
  640. retval = tty_check_change(tty);
  641. if (retval)
  642. return retval;
  643. switch (arg) {
  644. case TCOOFF:
  645. if (!tty->flow_stopped) {
  646. tty->flow_stopped = 1;
  647. stop_tty(tty);
  648. }
  649. break;
  650. case TCOON:
  651. if (tty->flow_stopped) {
  652. tty->flow_stopped = 0;
  653. start_tty(tty);
  654. }
  655. break;
  656. case TCIOFF:
  657. if (STOP_CHAR(tty) != __DISABLED_CHAR)
  658. return send_prio_char(tty, STOP_CHAR(tty));
  659. break;
  660. case TCION:
  661. if (START_CHAR(tty) != __DISABLED_CHAR)
  662. return send_prio_char(tty, START_CHAR(tty));
  663. break;
  664. default:
  665. return -EINVAL;
  666. }
  667. return 0;
  668. case TCFLSH:
  669. retval = tty_check_change(tty);
  670. if (retval)
  671. return retval;
  672. ld = tty_ldisc_ref(tty);
  673. switch (arg) {
  674. case TCIFLUSH:
  675. if (ld && ld->flush_buffer)
  676. ld->flush_buffer(tty);
  677. break;
  678. case TCIOFLUSH:
  679. if (ld && ld->flush_buffer)
  680. ld->flush_buffer(tty);
  681. /* fall through */
  682. case TCOFLUSH:
  683. if (tty->driver->flush_buffer)
  684. tty->driver->flush_buffer(tty);
  685. break;
  686. default:
  687. tty_ldisc_deref(ld);
  688. return -EINVAL;
  689. }
  690. tty_ldisc_deref(ld);
  691. return 0;
  692. case TIOCOUTQ:
  693. return put_user(tty->driver->chars_in_buffer ?
  694. tty->driver->chars_in_buffer(tty) : 0,
  695. (int __user *) arg);
  696. case TIOCINQ:
  697. retval = tty->read_cnt;
  698. if (L_ICANON(tty))
  699. retval = inq_canon(tty);
  700. return put_user(retval, (unsigned int __user *) arg);
  701. case TIOCGLCKTRMIOS:
  702. if (kernel_termios_to_user_termios((struct termios __user *)arg, real_tty->termios_locked))
  703. return -EFAULT;
  704. return 0;
  705. case TIOCSLCKTRMIOS:
  706. if (!capable(CAP_SYS_ADMIN))
  707. return -EPERM;
  708. if (user_termios_to_kernel_termios(real_tty->termios_locked, (struct termios __user *) arg))
  709. return -EFAULT;
  710. return 0;
  711. case TIOCPKT:
  712. {
  713. int pktmode;
  714. if (tty->driver->type != TTY_DRIVER_TYPE_PTY ||
  715. tty->driver->subtype != PTY_TYPE_MASTER)
  716. return -ENOTTY;
  717. if (get_user(pktmode, (int __user *) arg))
  718. return -EFAULT;
  719. if (pktmode) {
  720. if (!tty->packet) {
  721. tty->packet = 1;
  722. tty->link->ctrl_status = 0;
  723. }
  724. } else
  725. tty->packet = 0;
  726. return 0;
  727. }
  728. case TIOCGSOFTCAR:
  729. return put_user(C_CLOCAL(tty) ? 1 : 0, (int __user *)arg);
  730. case TIOCSSOFTCAR:
  731. if (get_user(arg, (unsigned int __user *) arg))
  732. return -EFAULT;
  733. mutex_lock(&tty->termios_mutex);
  734. tty->termios->c_cflag =
  735. ((tty->termios->c_cflag & ~CLOCAL) |
  736. (arg ? CLOCAL : 0));
  737. mutex_unlock(&tty->termios_mutex);
  738. return 0;
  739. default:
  740. return -ENOIOCTLCMD;
  741. }
  742. }
  743. EXPORT_SYMBOL(n_tty_ioctl);