tty_ioctl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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. if (opt & TERMIOS_TERMIO) {
  343. memcpy(&tmp_termios, tty->termios, sizeof(struct ktermios));
  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. memcpy(&tmp_termios, tty->termios, sizeof(struct termios));
  350. if (user_termios_to_kernel_termios_1(&tmp_termios,
  351. (struct termios_v1 __user *)arg))
  352. return -EFAULT;
  353. #endif
  354. } else {
  355. if (user_termios_to_kernel_termios(&tmp_termios,
  356. (struct termios __user *)arg))
  357. return -EFAULT;
  358. }
  359. /* If old style Bfoo values are used then load c_ispeed/c_ospeed with the real speed
  360. so its unconditionally usable */
  361. tmp_termios.c_ispeed = tty_termios_input_baud_rate(&tmp_termios);
  362. tmp_termios.c_ospeed = tty_termios_baud_rate(&tmp_termios);
  363. ld = tty_ldisc_ref(tty);
  364. if (ld != NULL) {
  365. if ((opt & TERMIOS_FLUSH) && ld->flush_buffer)
  366. ld->flush_buffer(tty);
  367. tty_ldisc_deref(ld);
  368. }
  369. if (opt & TERMIOS_WAIT) {
  370. tty_wait_until_sent(tty, 0);
  371. if (signal_pending(current))
  372. return -EINTR;
  373. }
  374. change_termios(tty, &tmp_termios);
  375. return 0;
  376. }
  377. static int get_termio(struct tty_struct * tty, struct termio __user * termio)
  378. {
  379. if (kernel_termios_to_user_termio(termio, tty->termios))
  380. return -EFAULT;
  381. return 0;
  382. }
  383. static unsigned long inq_canon(struct tty_struct * tty)
  384. {
  385. int nr, head, tail;
  386. if (!tty->canon_data || !tty->read_buf)
  387. return 0;
  388. head = tty->canon_head;
  389. tail = tty->read_tail;
  390. nr = (head - tail) & (N_TTY_BUF_SIZE-1);
  391. /* Skip EOF-chars.. */
  392. while (head != tail) {
  393. if (test_bit(tail, tty->read_flags) &&
  394. tty->read_buf[tail] == __DISABLED_CHAR)
  395. nr--;
  396. tail = (tail+1) & (N_TTY_BUF_SIZE-1);
  397. }
  398. return nr;
  399. }
  400. #ifdef TIOCGETP
  401. /*
  402. * These are deprecated, but there is limited support..
  403. *
  404. * The "sg_flags" translation is a joke..
  405. */
  406. static int get_sgflags(struct tty_struct * tty)
  407. {
  408. int flags = 0;
  409. if (!(tty->termios->c_lflag & ICANON)) {
  410. if (tty->termios->c_lflag & ISIG)
  411. flags |= 0x02; /* cbreak */
  412. else
  413. flags |= 0x20; /* raw */
  414. }
  415. if (tty->termios->c_lflag & ECHO)
  416. flags |= 0x08; /* echo */
  417. if (tty->termios->c_oflag & OPOST)
  418. if (tty->termios->c_oflag & ONLCR)
  419. flags |= 0x10; /* crmod */
  420. return flags;
  421. }
  422. static int get_sgttyb(struct tty_struct * tty, struct sgttyb __user * sgttyb)
  423. {
  424. struct sgttyb tmp;
  425. mutex_lock(&tty->termios_mutex);
  426. tmp.sg_ispeed = tty->termios->c_ispeed;
  427. tmp.sg_ospeed = tty->termios->c_ospeed;
  428. tmp.sg_erase = tty->termios->c_cc[VERASE];
  429. tmp.sg_kill = tty->termios->c_cc[VKILL];
  430. tmp.sg_flags = get_sgflags(tty);
  431. mutex_unlock(&tty->termios_mutex);
  432. return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  433. }
  434. static void set_sgflags(struct ktermios * termios, int flags)
  435. {
  436. termios->c_iflag = ICRNL | IXON;
  437. termios->c_oflag = 0;
  438. termios->c_lflag = ISIG | ICANON;
  439. if (flags & 0x02) { /* cbreak */
  440. termios->c_iflag = 0;
  441. termios->c_lflag &= ~ICANON;
  442. }
  443. if (flags & 0x08) { /* echo */
  444. termios->c_lflag |= ECHO | ECHOE | ECHOK |
  445. ECHOCTL | ECHOKE | IEXTEN;
  446. }
  447. if (flags & 0x10) { /* crmod */
  448. termios->c_oflag |= OPOST | ONLCR;
  449. }
  450. if (flags & 0x20) { /* raw */
  451. termios->c_iflag = 0;
  452. termios->c_lflag &= ~(ISIG | ICANON);
  453. }
  454. if (!(termios->c_lflag & ICANON)) {
  455. termios->c_cc[VMIN] = 1;
  456. termios->c_cc[VTIME] = 0;
  457. }
  458. }
  459. /**
  460. * set_sgttyb - set legacy terminal values
  461. * @tty: tty structure
  462. * @sgttyb: pointer to old style terminal structure
  463. *
  464. * Updates a terminal from the legacy BSD style terminal information
  465. * structure.
  466. *
  467. * Locking: termios_sem
  468. */
  469. static int set_sgttyb(struct tty_struct * tty, struct sgttyb __user * sgttyb)
  470. {
  471. int retval;
  472. struct sgttyb tmp;
  473. struct ktermios termios;
  474. retval = tty_check_change(tty);
  475. if (retval)
  476. return retval;
  477. if (copy_from_user(&tmp, sgttyb, sizeof(tmp)))
  478. return -EFAULT;
  479. mutex_lock(&tty->termios_mutex);
  480. termios = *tty->termios;
  481. termios.c_cc[VERASE] = tmp.sg_erase;
  482. termios.c_cc[VKILL] = tmp.sg_kill;
  483. set_sgflags(&termios, tmp.sg_flags);
  484. /* Try and encode into Bfoo format */
  485. #ifdef BOTHER
  486. tty_termios_encode_baud_rate(&termios, termios.c_ispeed, termios.c_ospeed);
  487. #endif
  488. mutex_unlock(&tty->termios_mutex);
  489. change_termios(tty, &termios);
  490. return 0;
  491. }
  492. #endif
  493. #ifdef TIOCGETC
  494. static int get_tchars(struct tty_struct * tty, struct tchars __user * tchars)
  495. {
  496. struct tchars tmp;
  497. tmp.t_intrc = tty->termios->c_cc[VINTR];
  498. tmp.t_quitc = tty->termios->c_cc[VQUIT];
  499. tmp.t_startc = tty->termios->c_cc[VSTART];
  500. tmp.t_stopc = tty->termios->c_cc[VSTOP];
  501. tmp.t_eofc = tty->termios->c_cc[VEOF];
  502. tmp.t_brkc = tty->termios->c_cc[VEOL2]; /* what is brkc anyway? */
  503. return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  504. }
  505. static int set_tchars(struct tty_struct * tty, struct tchars __user * tchars)
  506. {
  507. struct tchars tmp;
  508. if (copy_from_user(&tmp, tchars, sizeof(tmp)))
  509. return -EFAULT;
  510. tty->termios->c_cc[VINTR] = tmp.t_intrc;
  511. tty->termios->c_cc[VQUIT] = tmp.t_quitc;
  512. tty->termios->c_cc[VSTART] = tmp.t_startc;
  513. tty->termios->c_cc[VSTOP] = tmp.t_stopc;
  514. tty->termios->c_cc[VEOF] = tmp.t_eofc;
  515. tty->termios->c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */
  516. return 0;
  517. }
  518. #endif
  519. #ifdef TIOCGLTC
  520. static int get_ltchars(struct tty_struct * tty, struct ltchars __user * ltchars)
  521. {
  522. struct ltchars tmp;
  523. tmp.t_suspc = tty->termios->c_cc[VSUSP];
  524. tmp.t_dsuspc = tty->termios->c_cc[VSUSP]; /* what is dsuspc anyway? */
  525. tmp.t_rprntc = tty->termios->c_cc[VREPRINT];
  526. tmp.t_flushc = tty->termios->c_cc[VEOL2]; /* what is flushc anyway? */
  527. tmp.t_werasc = tty->termios->c_cc[VWERASE];
  528. tmp.t_lnextc = tty->termios->c_cc[VLNEXT];
  529. return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  530. }
  531. static int set_ltchars(struct tty_struct * tty, struct ltchars __user * ltchars)
  532. {
  533. struct ltchars tmp;
  534. if (copy_from_user(&tmp, ltchars, sizeof(tmp)))
  535. return -EFAULT;
  536. tty->termios->c_cc[VSUSP] = tmp.t_suspc;
  537. tty->termios->c_cc[VEOL2] = tmp.t_dsuspc; /* what is dsuspc anyway? */
  538. tty->termios->c_cc[VREPRINT] = tmp.t_rprntc;
  539. tty->termios->c_cc[VEOL2] = tmp.t_flushc; /* what is flushc anyway? */
  540. tty->termios->c_cc[VWERASE] = tmp.t_werasc;
  541. tty->termios->c_cc[VLNEXT] = tmp.t_lnextc;
  542. return 0;
  543. }
  544. #endif
  545. /**
  546. * send_prio_char - send priority character
  547. *
  548. * Send a high priority character to the tty even if stopped
  549. *
  550. * Locking: none for xchar method, write ordering for write method.
  551. */
  552. static int send_prio_char(struct tty_struct *tty, char ch)
  553. {
  554. int was_stopped = tty->stopped;
  555. if (tty->driver->send_xchar) {
  556. tty->driver->send_xchar(tty, ch);
  557. return 0;
  558. }
  559. if (mutex_lock_interruptible(&tty->atomic_write_lock))
  560. return -ERESTARTSYS;
  561. if (was_stopped)
  562. start_tty(tty);
  563. tty->driver->write(tty, &ch, 1);
  564. if (was_stopped)
  565. stop_tty(tty);
  566. mutex_unlock(&tty->atomic_write_lock);
  567. return 0;
  568. }
  569. int n_tty_ioctl(struct tty_struct * tty, struct file * file,
  570. unsigned int cmd, unsigned long arg)
  571. {
  572. struct tty_struct * real_tty;
  573. void __user *p = (void __user *)arg;
  574. int retval;
  575. struct tty_ldisc *ld;
  576. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  577. tty->driver->subtype == PTY_TYPE_MASTER)
  578. real_tty = tty->link;
  579. else
  580. real_tty = tty;
  581. switch (cmd) {
  582. #ifdef TIOCGETP
  583. case TIOCGETP:
  584. return get_sgttyb(real_tty, (struct sgttyb __user *) arg);
  585. case TIOCSETP:
  586. case TIOCSETN:
  587. return set_sgttyb(real_tty, (struct sgttyb __user *) arg);
  588. #endif
  589. #ifdef TIOCGETC
  590. case TIOCGETC:
  591. return get_tchars(real_tty, p);
  592. case TIOCSETC:
  593. return set_tchars(real_tty, p);
  594. #endif
  595. #ifdef TIOCGLTC
  596. case TIOCGLTC:
  597. return get_ltchars(real_tty, p);
  598. case TIOCSLTC:
  599. return set_ltchars(real_tty, p);
  600. #endif
  601. case TCSETSF:
  602. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_OLD);
  603. case TCSETSW:
  604. return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_OLD);
  605. case TCSETS:
  606. return set_termios(real_tty, p, TERMIOS_OLD);
  607. #ifndef TCGETS2
  608. case TCGETS:
  609. if (kernel_termios_to_user_termios((struct termios __user *)arg, real_tty->termios))
  610. return -EFAULT;
  611. return 0;
  612. #else
  613. case TCGETS:
  614. if (kernel_termios_to_user_termios_1((struct termios_v1 __user *)arg, real_tty->termios))
  615. return -EFAULT;
  616. return 0;
  617. case TCGETS2:
  618. if (kernel_termios_to_user_termios((struct termios __user *)arg, real_tty->termios))
  619. return -EFAULT;
  620. return 0;
  621. case TCSETSF2:
  622. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT);
  623. case TCSETSW2:
  624. return set_termios(real_tty, p, TERMIOS_WAIT);
  625. case TCSETS2:
  626. return set_termios(real_tty, p, 0);
  627. #endif
  628. case TCGETA:
  629. return get_termio(real_tty, p);
  630. case TCSETAF:
  631. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_TERMIO);
  632. case TCSETAW:
  633. return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_TERMIO);
  634. case TCSETA:
  635. return set_termios(real_tty, p, TERMIOS_TERMIO);
  636. case TCXONC:
  637. retval = tty_check_change(tty);
  638. if (retval)
  639. return retval;
  640. switch (arg) {
  641. case TCOOFF:
  642. if (!tty->flow_stopped) {
  643. tty->flow_stopped = 1;
  644. stop_tty(tty);
  645. }
  646. break;
  647. case TCOON:
  648. if (tty->flow_stopped) {
  649. tty->flow_stopped = 0;
  650. start_tty(tty);
  651. }
  652. break;
  653. case TCIOFF:
  654. if (STOP_CHAR(tty) != __DISABLED_CHAR)
  655. return send_prio_char(tty, STOP_CHAR(tty));
  656. break;
  657. case TCION:
  658. if (START_CHAR(tty) != __DISABLED_CHAR)
  659. return send_prio_char(tty, START_CHAR(tty));
  660. break;
  661. default:
  662. return -EINVAL;
  663. }
  664. return 0;
  665. case TCFLSH:
  666. retval = tty_check_change(tty);
  667. if (retval)
  668. return retval;
  669. ld = tty_ldisc_ref(tty);
  670. switch (arg) {
  671. case TCIFLUSH:
  672. if (ld && ld->flush_buffer)
  673. ld->flush_buffer(tty);
  674. break;
  675. case TCIOFLUSH:
  676. if (ld && ld->flush_buffer)
  677. ld->flush_buffer(tty);
  678. /* fall through */
  679. case TCOFLUSH:
  680. if (tty->driver->flush_buffer)
  681. tty->driver->flush_buffer(tty);
  682. break;
  683. default:
  684. tty_ldisc_deref(ld);
  685. return -EINVAL;
  686. }
  687. tty_ldisc_deref(ld);
  688. return 0;
  689. case TIOCOUTQ:
  690. return put_user(tty->driver->chars_in_buffer ?
  691. tty->driver->chars_in_buffer(tty) : 0,
  692. (int __user *) arg);
  693. case TIOCINQ:
  694. retval = tty->read_cnt;
  695. if (L_ICANON(tty))
  696. retval = inq_canon(tty);
  697. return put_user(retval, (unsigned int __user *) arg);
  698. case TIOCGLCKTRMIOS:
  699. if (kernel_termios_to_user_termios((struct termios __user *)arg, real_tty->termios_locked))
  700. return -EFAULT;
  701. return 0;
  702. case TIOCSLCKTRMIOS:
  703. if (!capable(CAP_SYS_ADMIN))
  704. return -EPERM;
  705. if (user_termios_to_kernel_termios(real_tty->termios_locked, (struct termios __user *) arg))
  706. return -EFAULT;
  707. return 0;
  708. case TIOCPKT:
  709. {
  710. int pktmode;
  711. if (tty->driver->type != TTY_DRIVER_TYPE_PTY ||
  712. tty->driver->subtype != PTY_TYPE_MASTER)
  713. return -ENOTTY;
  714. if (get_user(pktmode, (int __user *) arg))
  715. return -EFAULT;
  716. if (pktmode) {
  717. if (!tty->packet) {
  718. tty->packet = 1;
  719. tty->link->ctrl_status = 0;
  720. }
  721. } else
  722. tty->packet = 0;
  723. return 0;
  724. }
  725. case TIOCGSOFTCAR:
  726. return put_user(C_CLOCAL(tty) ? 1 : 0, (int __user *)arg);
  727. case TIOCSSOFTCAR:
  728. if (get_user(arg, (unsigned int __user *) arg))
  729. return -EFAULT;
  730. mutex_lock(&tty->termios_mutex);
  731. tty->termios->c_cflag =
  732. ((tty->termios->c_cflag & ~CLOCAL) |
  733. (arg ? CLOCAL : 0));
  734. mutex_unlock(&tty->termios_mutex);
  735. return 0;
  736. default:
  737. return -ENOIOCTLCMD;
  738. }
  739. }
  740. EXPORT_SYMBOL(n_tty_ioctl);