tty_ioctl.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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 <linux/smp_lock.h>
  24. #include <asm/io.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/system.h>
  27. #undef TTY_DEBUG_WAIT_UNTIL_SENT
  28. #undef DEBUG
  29. /*
  30. * Internal flag options for termios setting behavior
  31. */
  32. #define TERMIOS_FLUSH 1
  33. #define TERMIOS_WAIT 2
  34. #define TERMIOS_TERMIO 4
  35. #define TERMIOS_OLD 8
  36. int tty_chars_in_buffer(struct tty_struct *tty)
  37. {
  38. if (tty->ops->chars_in_buffer)
  39. return tty->ops->chars_in_buffer(tty);
  40. else
  41. return 0;
  42. }
  43. EXPORT_SYMBOL(tty_chars_in_buffer);
  44. int tty_write_room(struct tty_struct *tty)
  45. {
  46. if (tty->ops->write_room)
  47. return tty->ops->write_room(tty);
  48. return 2048;
  49. }
  50. EXPORT_SYMBOL(tty_write_room);
  51. void tty_driver_flush_buffer(struct tty_struct *tty)
  52. {
  53. if (tty->ops->flush_buffer)
  54. tty->ops->flush_buffer(tty);
  55. }
  56. EXPORT_SYMBOL(tty_driver_flush_buffer);
  57. void tty_throttle(struct tty_struct *tty)
  58. {
  59. /* check TTY_THROTTLED first so it indicates our state */
  60. if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
  61. tty->ops->throttle)
  62. tty->ops->throttle(tty);
  63. }
  64. EXPORT_SYMBOL(tty_throttle);
  65. void tty_unthrottle(struct tty_struct *tty)
  66. {
  67. if (test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
  68. tty->ops->unthrottle)
  69. tty->ops->unthrottle(tty);
  70. }
  71. EXPORT_SYMBOL(tty_unthrottle);
  72. /**
  73. * tty_wait_until_sent - wait for I/O to finish
  74. * @tty: tty we are waiting for
  75. * @timeout: how long we will wait
  76. *
  77. * Wait for characters pending in a tty driver to hit the wire, or
  78. * for a timeout to occur (eg due to flow control)
  79. *
  80. * Locking: none
  81. */
  82. void tty_wait_until_sent(struct tty_struct *tty, long timeout)
  83. {
  84. #ifdef TTY_DEBUG_WAIT_UNTIL_SENT
  85. char buf[64];
  86. printk(KERN_DEBUG "%s wait until sent...\n", tty_name(tty, buf));
  87. #endif
  88. if (!timeout)
  89. timeout = MAX_SCHEDULE_TIMEOUT;
  90. if (wait_event_interruptible_timeout(tty->write_wait,
  91. !tty_chars_in_buffer(tty), timeout) >= 0) {
  92. if (tty->ops->wait_until_sent)
  93. tty->ops->wait_until_sent(tty, timeout);
  94. }
  95. }
  96. EXPORT_SYMBOL(tty_wait_until_sent);
  97. static void unset_locked_termios(struct ktermios *termios,
  98. struct ktermios *old,
  99. struct ktermios *locked)
  100. {
  101. int i;
  102. #define NOSET_MASK(x, y, z) (x = ((x) & ~(z)) | ((y) & (z)))
  103. if (!locked) {
  104. printk(KERN_WARNING "Warning?!? termios_locked is NULL.\n");
  105. return;
  106. }
  107. NOSET_MASK(termios->c_iflag, old->c_iflag, locked->c_iflag);
  108. NOSET_MASK(termios->c_oflag, old->c_oflag, locked->c_oflag);
  109. NOSET_MASK(termios->c_cflag, old->c_cflag, locked->c_cflag);
  110. NOSET_MASK(termios->c_lflag, old->c_lflag, locked->c_lflag);
  111. termios->c_line = locked->c_line ? old->c_line : termios->c_line;
  112. for (i = 0; i < NCCS; i++)
  113. termios->c_cc[i] = locked->c_cc[i] ?
  114. old->c_cc[i] : termios->c_cc[i];
  115. /* FIXME: What should we do for i/ospeed */
  116. }
  117. /*
  118. * Routine which returns the baud rate of the tty
  119. *
  120. * Note that the baud_table needs to be kept in sync with the
  121. * include/asm/termbits.h file.
  122. */
  123. static const speed_t baud_table[] = {
  124. 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
  125. 9600, 19200, 38400, 57600, 115200, 230400, 460800,
  126. #ifdef __sparc__
  127. 76800, 153600, 307200, 614400, 921600
  128. #else
  129. 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
  130. 2500000, 3000000, 3500000, 4000000
  131. #endif
  132. };
  133. #ifndef __sparc__
  134. static const tcflag_t baud_bits[] = {
  135. B0, B50, B75, B110, B134, B150, B200, B300, B600,
  136. B1200, B1800, B2400, B4800, B9600, B19200, B38400,
  137. B57600, B115200, B230400, B460800, B500000, B576000,
  138. B921600, B1000000, B1152000, B1500000, B2000000, B2500000,
  139. B3000000, B3500000, B4000000
  140. };
  141. #else
  142. static const tcflag_t baud_bits[] = {
  143. B0, B50, B75, B110, B134, B150, B200, B300, B600,
  144. B1200, B1800, B2400, B4800, B9600, B19200, B38400,
  145. B57600, B115200, B230400, B460800, B76800, B153600,
  146. B307200, B614400, B921600
  147. };
  148. #endif
  149. static int n_baud_table = ARRAY_SIZE(baud_table);
  150. /**
  151. * tty_termios_baud_rate
  152. * @termios: termios structure
  153. *
  154. * Convert termios baud rate data into a speed. This should be called
  155. * with the termios lock held if this termios is a terminal termios
  156. * structure. May change the termios data. Device drivers can call this
  157. * function but should use ->c_[io]speed directly as they are updated.
  158. *
  159. * Locking: none
  160. */
  161. speed_t tty_termios_baud_rate(struct ktermios *termios)
  162. {
  163. unsigned int cbaud;
  164. cbaud = termios->c_cflag & CBAUD;
  165. #ifdef BOTHER
  166. /* Magic token for arbitary speed via c_ispeed/c_ospeed */
  167. if (cbaud == BOTHER)
  168. return termios->c_ospeed;
  169. #endif
  170. if (cbaud & CBAUDEX) {
  171. cbaud &= ~CBAUDEX;
  172. if (cbaud < 1 || cbaud + 15 > n_baud_table)
  173. termios->c_cflag &= ~CBAUDEX;
  174. else
  175. cbaud += 15;
  176. }
  177. return baud_table[cbaud];
  178. }
  179. EXPORT_SYMBOL(tty_termios_baud_rate);
  180. /**
  181. * tty_termios_input_baud_rate
  182. * @termios: termios structure
  183. *
  184. * Convert termios baud rate data into a speed. This should be called
  185. * with the termios lock held if this termios is a terminal termios
  186. * structure. May change the termios data. Device drivers can call this
  187. * function but should use ->c_[io]speed directly as they are updated.
  188. *
  189. * Locking: none
  190. */
  191. speed_t tty_termios_input_baud_rate(struct ktermios *termios)
  192. {
  193. #ifdef IBSHIFT
  194. unsigned int cbaud = (termios->c_cflag >> IBSHIFT) & CBAUD;
  195. if (cbaud == B0)
  196. return tty_termios_baud_rate(termios);
  197. /* Magic token for arbitary speed via c_ispeed*/
  198. if (cbaud == BOTHER)
  199. return termios->c_ispeed;
  200. if (cbaud & CBAUDEX) {
  201. cbaud &= ~CBAUDEX;
  202. if (cbaud < 1 || cbaud + 15 > n_baud_table)
  203. termios->c_cflag &= ~(CBAUDEX << IBSHIFT);
  204. else
  205. cbaud += 15;
  206. }
  207. return baud_table[cbaud];
  208. #else
  209. return tty_termios_baud_rate(termios);
  210. #endif
  211. }
  212. EXPORT_SYMBOL(tty_termios_input_baud_rate);
  213. /**
  214. * tty_termios_encode_baud_rate
  215. * @termios: ktermios structure holding user requested state
  216. * @ispeed: input speed
  217. * @ospeed: output speed
  218. *
  219. * Encode the speeds set into the passed termios structure. This is
  220. * used as a library helper for drivers os that they can report back
  221. * the actual speed selected when it differs from the speed requested
  222. *
  223. * For maximal back compatibility with legacy SYS5/POSIX *nix behaviour
  224. * we need to carefully set the bits when the user does not get the
  225. * desired speed. We allow small margins and preserve as much of possible
  226. * of the input intent to keep compatiblity.
  227. *
  228. * Locking: Caller should hold termios lock. This is already held
  229. * when calling this function from the driver termios handler.
  230. *
  231. * The ifdefs deal with platforms whose owners have yet to update them
  232. * and will all go away once this is done.
  233. */
  234. void tty_termios_encode_baud_rate(struct ktermios *termios,
  235. speed_t ibaud, speed_t obaud)
  236. {
  237. int i = 0;
  238. int ifound = -1, ofound = -1;
  239. int iclose = ibaud/50, oclose = obaud/50;
  240. int ibinput = 0;
  241. if (obaud == 0) /* CD dropped */
  242. ibaud = 0; /* Clear ibaud to be sure */
  243. termios->c_ispeed = ibaud;
  244. termios->c_ospeed = obaud;
  245. #ifdef BOTHER
  246. /* If the user asked for a precise weird speed give a precise weird
  247. answer. If they asked for a Bfoo speed they many have problems
  248. digesting non-exact replies so fuzz a bit */
  249. if ((termios->c_cflag & CBAUD) == BOTHER)
  250. oclose = 0;
  251. if (((termios->c_cflag >> IBSHIFT) & CBAUD) == BOTHER)
  252. iclose = 0;
  253. if ((termios->c_cflag >> IBSHIFT) & CBAUD)
  254. ibinput = 1; /* An input speed was specified */
  255. #endif
  256. termios->c_cflag &= ~CBAUD;
  257. /*
  258. * Our goal is to find a close match to the standard baud rate
  259. * returned. Walk the baud rate table and if we get a very close
  260. * match then report back the speed as a POSIX Bxxxx value by
  261. * preference
  262. */
  263. do {
  264. if (obaud - oclose <= baud_table[i] &&
  265. obaud + oclose >= baud_table[i]) {
  266. termios->c_cflag |= baud_bits[i];
  267. ofound = i;
  268. }
  269. if (ibaud - iclose <= baud_table[i] &&
  270. ibaud + iclose >= baud_table[i]) {
  271. /* For the case input == output don't set IBAUD bits
  272. if the user didn't do so */
  273. if (ofound == i && !ibinput)
  274. ifound = i;
  275. #ifdef IBSHIFT
  276. else {
  277. ifound = i;
  278. termios->c_cflag |= (baud_bits[i] << IBSHIFT);
  279. }
  280. #endif
  281. }
  282. } while (++i < n_baud_table);
  283. /*
  284. * If we found no match then use BOTHER if provided or warn
  285. * the user their platform maintainer needs to wake up if not.
  286. */
  287. #ifdef BOTHER
  288. if (ofound == -1)
  289. termios->c_cflag |= BOTHER;
  290. /* Set exact input bits only if the input and output differ or the
  291. user already did */
  292. if (ifound == -1 && (ibaud != obaud || ibinput))
  293. termios->c_cflag |= (BOTHER << IBSHIFT);
  294. #else
  295. if (ifound == -1 || ofound == -1) {
  296. static int warned;
  297. if (!warned++)
  298. printk(KERN_WARNING "tty: Unable to return correct "
  299. "speed data as your architecture needs updating.\n");
  300. }
  301. #endif
  302. }
  303. EXPORT_SYMBOL_GPL(tty_termios_encode_baud_rate);
  304. void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud, speed_t obaud)
  305. {
  306. tty_termios_encode_baud_rate(tty->termios, ibaud, obaud);
  307. }
  308. EXPORT_SYMBOL_GPL(tty_encode_baud_rate);
  309. /**
  310. * tty_get_baud_rate - get tty bit rates
  311. * @tty: tty to query
  312. *
  313. * Returns the baud rate as an integer for this terminal. The
  314. * termios lock must be held by the caller and the terminal bit
  315. * flags may be updated.
  316. *
  317. * Locking: none
  318. */
  319. speed_t tty_get_baud_rate(struct tty_struct *tty)
  320. {
  321. speed_t baud = tty_termios_baud_rate(tty->termios);
  322. if (baud == 38400 && tty->alt_speed) {
  323. if (!tty->warned) {
  324. printk(KERN_WARNING "Use of setserial/setrocket to "
  325. "set SPD_* flags is deprecated\n");
  326. tty->warned = 1;
  327. }
  328. baud = tty->alt_speed;
  329. }
  330. return baud;
  331. }
  332. EXPORT_SYMBOL(tty_get_baud_rate);
  333. /**
  334. * tty_termios_copy_hw - copy hardware settings
  335. * @new: New termios
  336. * @old: Old termios
  337. *
  338. * Propogate the hardware specific terminal setting bits from
  339. * the old termios structure to the new one. This is used in cases
  340. * where the hardware does not support reconfiguration or as a helper
  341. * in some cases where only minimal reconfiguration is supported
  342. */
  343. void tty_termios_copy_hw(struct ktermios *new, struct ktermios *old)
  344. {
  345. /* The bits a dumb device handles in software. Smart devices need
  346. to always provide a set_termios method */
  347. new->c_cflag &= HUPCL | CREAD | CLOCAL;
  348. new->c_cflag |= old->c_cflag & ~(HUPCL | CREAD | CLOCAL);
  349. new->c_ispeed = old->c_ispeed;
  350. new->c_ospeed = old->c_ospeed;
  351. }
  352. EXPORT_SYMBOL(tty_termios_copy_hw);
  353. /**
  354. * tty_termios_hw_change - check for setting change
  355. * @a: termios
  356. * @b: termios to compare
  357. *
  358. * Check if any of the bits that affect a dumb device have changed
  359. * between the two termios structures, or a speed change is needed.
  360. */
  361. int tty_termios_hw_change(struct ktermios *a, struct ktermios *b)
  362. {
  363. if (a->c_ispeed != b->c_ispeed || a->c_ospeed != b->c_ospeed)
  364. return 1;
  365. if ((a->c_cflag ^ b->c_cflag) & ~(HUPCL | CREAD | CLOCAL))
  366. return 1;
  367. return 0;
  368. }
  369. EXPORT_SYMBOL(tty_termios_hw_change);
  370. /**
  371. * change_termios - update termios values
  372. * @tty: tty to update
  373. * @new_termios: desired new value
  374. *
  375. * Perform updates to the termios values set on this terminal. There
  376. * is a bit of layering violation here with n_tty in terms of the
  377. * internal knowledge of this function.
  378. *
  379. * Locking: termios_sem
  380. */
  381. static void change_termios(struct tty_struct *tty, struct ktermios *new_termios)
  382. {
  383. int canon_change;
  384. struct ktermios old_termios;
  385. struct tty_ldisc *ld;
  386. unsigned long flags;
  387. /*
  388. * Perform the actual termios internal changes under lock.
  389. */
  390. /* FIXME: we need to decide on some locking/ordering semantics
  391. for the set_termios notification eventually */
  392. mutex_lock(&tty->termios_mutex);
  393. old_termios = *tty->termios;
  394. *tty->termios = *new_termios;
  395. unset_locked_termios(tty->termios, &old_termios, tty->termios_locked);
  396. canon_change = (old_termios.c_lflag ^ tty->termios->c_lflag) & ICANON;
  397. if (canon_change) {
  398. memset(&tty->read_flags, 0, sizeof tty->read_flags);
  399. tty->canon_head = tty->read_tail;
  400. tty->canon_data = 0;
  401. tty->erasing = 0;
  402. }
  403. /* This bit should be in the ldisc code */
  404. if (canon_change && !L_ICANON(tty) && tty->read_cnt)
  405. /* Get characters left over from canonical mode. */
  406. wake_up_interruptible(&tty->read_wait);
  407. /* See if packet mode change of state. */
  408. if (tty->link && tty->link->packet) {
  409. int old_flow = ((old_termios.c_iflag & IXON) &&
  410. (old_termios.c_cc[VSTOP] == '\023') &&
  411. (old_termios.c_cc[VSTART] == '\021'));
  412. int new_flow = (I_IXON(tty) &&
  413. STOP_CHAR(tty) == '\023' &&
  414. START_CHAR(tty) == '\021');
  415. if (old_flow != new_flow) {
  416. spin_lock_irqsave(&tty->ctrl_lock, flags);
  417. tty->ctrl_status &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP);
  418. if (new_flow)
  419. tty->ctrl_status |= TIOCPKT_DOSTOP;
  420. else
  421. tty->ctrl_status |= TIOCPKT_NOSTOP;
  422. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  423. wake_up_interruptible(&tty->link->read_wait);
  424. }
  425. }
  426. if (tty->ops->set_termios)
  427. (*tty->ops->set_termios)(tty, &old_termios);
  428. else
  429. tty_termios_copy_hw(tty->termios, &old_termios);
  430. ld = tty_ldisc_ref(tty);
  431. if (ld != NULL) {
  432. if (ld->set_termios)
  433. (ld->set_termios)(tty, &old_termios);
  434. tty_ldisc_deref(ld);
  435. }
  436. mutex_unlock(&tty->termios_mutex);
  437. }
  438. /**
  439. * set_termios - set termios values for a tty
  440. * @tty: terminal device
  441. * @arg: user data
  442. * @opt: option information
  443. *
  444. * Helper function to prepare termios data and run necessary other
  445. * functions before using change_termios to do the actual changes.
  446. *
  447. * Locking:
  448. * Called functions take ldisc and termios_sem locks
  449. */
  450. static int set_termios(struct tty_struct *tty, void __user *arg, int opt)
  451. {
  452. struct ktermios tmp_termios;
  453. struct tty_ldisc *ld;
  454. int retval = tty_check_change(tty);
  455. if (retval)
  456. return retval;
  457. mutex_lock(&tty->termios_mutex);
  458. memcpy(&tmp_termios, tty->termios, sizeof(struct ktermios));
  459. mutex_unlock(&tty->termios_mutex);
  460. if (opt & TERMIOS_TERMIO) {
  461. if (user_termio_to_kernel_termios(&tmp_termios,
  462. (struct termio __user *)arg))
  463. return -EFAULT;
  464. #ifdef TCGETS2
  465. } else if (opt & TERMIOS_OLD) {
  466. if (user_termios_to_kernel_termios_1(&tmp_termios,
  467. (struct termios __user *)arg))
  468. return -EFAULT;
  469. } else {
  470. if (user_termios_to_kernel_termios(&tmp_termios,
  471. (struct termios2 __user *)arg))
  472. return -EFAULT;
  473. }
  474. #else
  475. } else if (user_termios_to_kernel_termios(&tmp_termios,
  476. (struct termios __user *)arg))
  477. return -EFAULT;
  478. #endif
  479. /* If old style Bfoo values are used then load c_ispeed/c_ospeed
  480. * with the real speed so its unconditionally usable */
  481. tmp_termios.c_ispeed = tty_termios_input_baud_rate(&tmp_termios);
  482. tmp_termios.c_ospeed = tty_termios_baud_rate(&tmp_termios);
  483. ld = tty_ldisc_ref(tty);
  484. if (ld != NULL) {
  485. if ((opt & TERMIOS_FLUSH) && ld->flush_buffer)
  486. ld->flush_buffer(tty);
  487. tty_ldisc_deref(ld);
  488. }
  489. if (opt & TERMIOS_WAIT) {
  490. tty_wait_until_sent(tty, 0);
  491. if (signal_pending(current))
  492. return -EINTR;
  493. }
  494. change_termios(tty, &tmp_termios);
  495. /* FIXME: Arguably if tmp_termios == tty->termios AND the
  496. actual requested termios was not tmp_termios then we may
  497. want to return an error as no user requested change has
  498. succeeded */
  499. return 0;
  500. }
  501. static int get_termio(struct tty_struct *tty, struct termio __user *termio)
  502. {
  503. if (kernel_termios_to_user_termio(termio, tty->termios))
  504. return -EFAULT;
  505. return 0;
  506. }
  507. static unsigned long inq_canon(struct tty_struct *tty)
  508. {
  509. int nr, head, tail;
  510. if (!tty->canon_data || !tty->read_buf)
  511. return 0;
  512. head = tty->canon_head;
  513. tail = tty->read_tail;
  514. nr = (head - tail) & (N_TTY_BUF_SIZE-1);
  515. /* Skip EOF-chars.. */
  516. while (head != tail) {
  517. if (test_bit(tail, tty->read_flags) &&
  518. tty->read_buf[tail] == __DISABLED_CHAR)
  519. nr--;
  520. tail = (tail+1) & (N_TTY_BUF_SIZE-1);
  521. }
  522. return nr;
  523. }
  524. #ifdef TIOCGETP
  525. /*
  526. * These are deprecated, but there is limited support..
  527. *
  528. * The "sg_flags" translation is a joke..
  529. */
  530. static int get_sgflags(struct tty_struct *tty)
  531. {
  532. int flags = 0;
  533. if (!(tty->termios->c_lflag & ICANON)) {
  534. if (tty->termios->c_lflag & ISIG)
  535. flags |= 0x02; /* cbreak */
  536. else
  537. flags |= 0x20; /* raw */
  538. }
  539. if (tty->termios->c_lflag & ECHO)
  540. flags |= 0x08; /* echo */
  541. if (tty->termios->c_oflag & OPOST)
  542. if (tty->termios->c_oflag & ONLCR)
  543. flags |= 0x10; /* crmod */
  544. return flags;
  545. }
  546. static int get_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
  547. {
  548. struct sgttyb tmp;
  549. mutex_lock(&tty->termios_mutex);
  550. tmp.sg_ispeed = tty->termios->c_ispeed;
  551. tmp.sg_ospeed = tty->termios->c_ospeed;
  552. tmp.sg_erase = tty->termios->c_cc[VERASE];
  553. tmp.sg_kill = tty->termios->c_cc[VKILL];
  554. tmp.sg_flags = get_sgflags(tty);
  555. mutex_unlock(&tty->termios_mutex);
  556. return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  557. }
  558. static void set_sgflags(struct ktermios *termios, int flags)
  559. {
  560. termios->c_iflag = ICRNL | IXON;
  561. termios->c_oflag = 0;
  562. termios->c_lflag = ISIG | ICANON;
  563. if (flags & 0x02) { /* cbreak */
  564. termios->c_iflag = 0;
  565. termios->c_lflag &= ~ICANON;
  566. }
  567. if (flags & 0x08) { /* echo */
  568. termios->c_lflag |= ECHO | ECHOE | ECHOK |
  569. ECHOCTL | ECHOKE | IEXTEN;
  570. }
  571. if (flags & 0x10) { /* crmod */
  572. termios->c_oflag |= OPOST | ONLCR;
  573. }
  574. if (flags & 0x20) { /* raw */
  575. termios->c_iflag = 0;
  576. termios->c_lflag &= ~(ISIG | ICANON);
  577. }
  578. if (!(termios->c_lflag & ICANON)) {
  579. termios->c_cc[VMIN] = 1;
  580. termios->c_cc[VTIME] = 0;
  581. }
  582. }
  583. /**
  584. * set_sgttyb - set legacy terminal values
  585. * @tty: tty structure
  586. * @sgttyb: pointer to old style terminal structure
  587. *
  588. * Updates a terminal from the legacy BSD style terminal information
  589. * structure.
  590. *
  591. * Locking: termios_sem
  592. */
  593. static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
  594. {
  595. int retval;
  596. struct sgttyb tmp;
  597. struct ktermios termios;
  598. retval = tty_check_change(tty);
  599. if (retval)
  600. return retval;
  601. if (copy_from_user(&tmp, sgttyb, sizeof(tmp)))
  602. return -EFAULT;
  603. mutex_lock(&tty->termios_mutex);
  604. termios = *tty->termios;
  605. termios.c_cc[VERASE] = tmp.sg_erase;
  606. termios.c_cc[VKILL] = tmp.sg_kill;
  607. set_sgflags(&termios, tmp.sg_flags);
  608. /* Try and encode into Bfoo format */
  609. #ifdef BOTHER
  610. tty_termios_encode_baud_rate(&termios, termios.c_ispeed,
  611. termios.c_ospeed);
  612. #endif
  613. mutex_unlock(&tty->termios_mutex);
  614. change_termios(tty, &termios);
  615. return 0;
  616. }
  617. #endif
  618. #ifdef TIOCGETC
  619. static int get_tchars(struct tty_struct *tty, struct tchars __user *tchars)
  620. {
  621. struct tchars tmp;
  622. mutex_lock(&tty->termios_mutex);
  623. tmp.t_intrc = tty->termios->c_cc[VINTR];
  624. tmp.t_quitc = tty->termios->c_cc[VQUIT];
  625. tmp.t_startc = tty->termios->c_cc[VSTART];
  626. tmp.t_stopc = tty->termios->c_cc[VSTOP];
  627. tmp.t_eofc = tty->termios->c_cc[VEOF];
  628. tmp.t_brkc = tty->termios->c_cc[VEOL2]; /* what is brkc anyway? */
  629. mutex_unlock(&tty->termios_mutex);
  630. return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  631. }
  632. static int set_tchars(struct tty_struct *tty, struct tchars __user *tchars)
  633. {
  634. struct tchars tmp;
  635. if (copy_from_user(&tmp, tchars, sizeof(tmp)))
  636. return -EFAULT;
  637. mutex_lock(&tty->termios_mutex);
  638. tty->termios->c_cc[VINTR] = tmp.t_intrc;
  639. tty->termios->c_cc[VQUIT] = tmp.t_quitc;
  640. tty->termios->c_cc[VSTART] = tmp.t_startc;
  641. tty->termios->c_cc[VSTOP] = tmp.t_stopc;
  642. tty->termios->c_cc[VEOF] = tmp.t_eofc;
  643. tty->termios->c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */
  644. mutex_unlock(&tty->termios_mutex);
  645. return 0;
  646. }
  647. #endif
  648. #ifdef TIOCGLTC
  649. static int get_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
  650. {
  651. struct ltchars tmp;
  652. mutex_lock(&tty->termios_mutex);
  653. tmp.t_suspc = tty->termios->c_cc[VSUSP];
  654. /* what is dsuspc anyway? */
  655. tmp.t_dsuspc = tty->termios->c_cc[VSUSP];
  656. tmp.t_rprntc = tty->termios->c_cc[VREPRINT];
  657. /* what is flushc anyway? */
  658. tmp.t_flushc = tty->termios->c_cc[VEOL2];
  659. tmp.t_werasc = tty->termios->c_cc[VWERASE];
  660. tmp.t_lnextc = tty->termios->c_cc[VLNEXT];
  661. mutex_unlock(&tty->termios_mutex);
  662. return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  663. }
  664. static int set_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
  665. {
  666. struct ltchars tmp;
  667. if (copy_from_user(&tmp, ltchars, sizeof(tmp)))
  668. return -EFAULT;
  669. mutex_lock(&tty->termios_mutex);
  670. tty->termios->c_cc[VSUSP] = tmp.t_suspc;
  671. /* what is dsuspc anyway? */
  672. tty->termios->c_cc[VEOL2] = tmp.t_dsuspc;
  673. tty->termios->c_cc[VREPRINT] = tmp.t_rprntc;
  674. /* what is flushc anyway? */
  675. tty->termios->c_cc[VEOL2] = tmp.t_flushc;
  676. tty->termios->c_cc[VWERASE] = tmp.t_werasc;
  677. tty->termios->c_cc[VLNEXT] = tmp.t_lnextc;
  678. mutex_unlock(&tty->termios_mutex);
  679. return 0;
  680. }
  681. #endif
  682. /**
  683. * send_prio_char - send priority character
  684. *
  685. * Send a high priority character to the tty even if stopped
  686. *
  687. * Locking: none for xchar method, write ordering for write method.
  688. */
  689. static int send_prio_char(struct tty_struct *tty, char ch)
  690. {
  691. int was_stopped = tty->stopped;
  692. if (tty->ops->send_xchar) {
  693. tty->ops->send_xchar(tty, ch);
  694. return 0;
  695. }
  696. if (tty_write_lock(tty, 0) < 0)
  697. return -ERESTARTSYS;
  698. if (was_stopped)
  699. start_tty(tty);
  700. tty->ops->write(tty, &ch, 1);
  701. if (was_stopped)
  702. stop_tty(tty);
  703. tty_write_unlock(tty);
  704. return 0;
  705. }
  706. /**
  707. * tty_change_softcar - carrier change ioctl helper
  708. * @tty: tty to update
  709. * @arg: enable/disable CLOCAL
  710. *
  711. * Perform a change to the CLOCAL state and call into the driver
  712. * layer to make it visible. All done with the termios mutex
  713. */
  714. static int tty_change_softcar(struct tty_struct *tty, int arg)
  715. {
  716. int ret = 0;
  717. int bit = arg ? CLOCAL : 0;
  718. struct ktermios old;
  719. mutex_lock(&tty->termios_mutex);
  720. old = *tty->termios;
  721. tty->termios->c_cflag &= ~CLOCAL;
  722. tty->termios->c_cflag |= bit;
  723. if (tty->ops->set_termios)
  724. tty->ops->set_termios(tty, &old);
  725. if ((tty->termios->c_cflag & CLOCAL) != bit)
  726. ret = -EINVAL;
  727. mutex_unlock(&tty->termios_mutex);
  728. return ret;
  729. }
  730. /**
  731. * tty_mode_ioctl - mode related ioctls
  732. * @tty: tty for the ioctl
  733. * @file: file pointer for the tty
  734. * @cmd: command
  735. * @arg: ioctl argument
  736. *
  737. * Perform non line discipline specific mode control ioctls. This
  738. * is designed to be called by line disciplines to ensure they provide
  739. * consistent mode setting.
  740. */
  741. int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
  742. unsigned int cmd, unsigned long arg)
  743. {
  744. struct tty_struct *real_tty;
  745. void __user *p = (void __user *)arg;
  746. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  747. tty->driver->subtype == PTY_TYPE_MASTER)
  748. real_tty = tty->link;
  749. else
  750. real_tty = tty;
  751. switch (cmd) {
  752. #ifdef TIOCGETP
  753. case TIOCGETP:
  754. return get_sgttyb(real_tty, (struct sgttyb __user *) arg);
  755. case TIOCSETP:
  756. case TIOCSETN:
  757. return set_sgttyb(real_tty, (struct sgttyb __user *) arg);
  758. #endif
  759. #ifdef TIOCGETC
  760. case TIOCGETC:
  761. return get_tchars(real_tty, p);
  762. case TIOCSETC:
  763. return set_tchars(real_tty, p);
  764. #endif
  765. #ifdef TIOCGLTC
  766. case TIOCGLTC:
  767. return get_ltchars(real_tty, p);
  768. case TIOCSLTC:
  769. return set_ltchars(real_tty, p);
  770. #endif
  771. case TCSETSF:
  772. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_OLD);
  773. case TCSETSW:
  774. return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_OLD);
  775. case TCSETS:
  776. return set_termios(real_tty, p, TERMIOS_OLD);
  777. #ifndef TCGETS2
  778. case TCGETS:
  779. if (kernel_termios_to_user_termios((struct termios __user *)arg, real_tty->termios))
  780. return -EFAULT;
  781. return 0;
  782. #else
  783. case TCGETS:
  784. if (kernel_termios_to_user_termios_1((struct termios __user *)arg, real_tty->termios))
  785. return -EFAULT;
  786. return 0;
  787. case TCGETS2:
  788. if (kernel_termios_to_user_termios((struct termios2 __user *)arg, real_tty->termios))
  789. return -EFAULT;
  790. return 0;
  791. case TCSETSF2:
  792. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT);
  793. case TCSETSW2:
  794. return set_termios(real_tty, p, TERMIOS_WAIT);
  795. case TCSETS2:
  796. return set_termios(real_tty, p, 0);
  797. #endif
  798. case TCGETA:
  799. return get_termio(real_tty, p);
  800. case TCSETAF:
  801. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_TERMIO);
  802. case TCSETAW:
  803. return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_TERMIO);
  804. case TCSETA:
  805. return set_termios(real_tty, p, TERMIOS_TERMIO);
  806. #ifndef TCGETS2
  807. case TIOCGLCKTRMIOS:
  808. if (kernel_termios_to_user_termios((struct termios __user *)arg, real_tty->termios_locked))
  809. return -EFAULT;
  810. return 0;
  811. case TIOCSLCKTRMIOS:
  812. if (!capable(CAP_SYS_ADMIN))
  813. return -EPERM;
  814. if (user_termios_to_kernel_termios(real_tty->termios_locked,
  815. (struct termios __user *) arg))
  816. return -EFAULT;
  817. return 0;
  818. #else
  819. case TIOCGLCKTRMIOS:
  820. if (kernel_termios_to_user_termios_1((struct termios __user *)arg, real_tty->termios_locked))
  821. return -EFAULT;
  822. return 0;
  823. case TIOCSLCKTRMIOS:
  824. if (!capable(CAP_SYS_ADMIN))
  825. return -EPERM;
  826. if (user_termios_to_kernel_termios_1(real_tty->termios_locked,
  827. (struct termios __user *) arg))
  828. return -EFAULT;
  829. return 0;
  830. #endif
  831. case TIOCGSOFTCAR:
  832. return put_user(C_CLOCAL(tty) ? 1 : 0,
  833. (int __user *)arg);
  834. case TIOCSSOFTCAR:
  835. if (get_user(arg, (unsigned int __user *) arg))
  836. return -EFAULT;
  837. return tty_change_softcar(tty, arg);
  838. default:
  839. return -ENOIOCTLCMD;
  840. }
  841. }
  842. EXPORT_SYMBOL_GPL(tty_mode_ioctl);
  843. int tty_perform_flush(struct tty_struct *tty, unsigned long arg)
  844. {
  845. struct tty_ldisc *ld;
  846. int retval = tty_check_change(tty);
  847. if (retval)
  848. return retval;
  849. ld = tty_ldisc_ref(tty);
  850. switch (arg) {
  851. case TCIFLUSH:
  852. if (ld && ld->flush_buffer)
  853. ld->flush_buffer(tty);
  854. break;
  855. case TCIOFLUSH:
  856. if (ld && ld->flush_buffer)
  857. ld->flush_buffer(tty);
  858. /* fall through */
  859. case TCOFLUSH:
  860. tty_driver_flush_buffer(tty);
  861. break;
  862. default:
  863. tty_ldisc_deref(ld);
  864. return -EINVAL;
  865. }
  866. tty_ldisc_deref(ld);
  867. return 0;
  868. }
  869. EXPORT_SYMBOL_GPL(tty_perform_flush);
  870. int n_tty_ioctl(struct tty_struct *tty, struct file *file,
  871. unsigned int cmd, unsigned long arg)
  872. {
  873. unsigned long flags;
  874. int retval;
  875. switch (cmd) {
  876. case TCXONC:
  877. retval = tty_check_change(tty);
  878. if (retval)
  879. return retval;
  880. switch (arg) {
  881. case TCOOFF:
  882. if (!tty->flow_stopped) {
  883. tty->flow_stopped = 1;
  884. stop_tty(tty);
  885. }
  886. break;
  887. case TCOON:
  888. if (tty->flow_stopped) {
  889. tty->flow_stopped = 0;
  890. start_tty(tty);
  891. }
  892. break;
  893. case TCIOFF:
  894. if (STOP_CHAR(tty) != __DISABLED_CHAR)
  895. return send_prio_char(tty, STOP_CHAR(tty));
  896. break;
  897. case TCION:
  898. if (START_CHAR(tty) != __DISABLED_CHAR)
  899. return send_prio_char(tty, START_CHAR(tty));
  900. break;
  901. default:
  902. return -EINVAL;
  903. }
  904. return 0;
  905. case TCFLSH:
  906. return tty_perform_flush(tty, arg);
  907. case TIOCOUTQ:
  908. return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
  909. case TIOCINQ:
  910. retval = tty->read_cnt;
  911. if (L_ICANON(tty))
  912. retval = inq_canon(tty);
  913. return put_user(retval, (unsigned int __user *) arg);
  914. case TIOCPKT:
  915. {
  916. int pktmode;
  917. if (tty->driver->type != TTY_DRIVER_TYPE_PTY ||
  918. tty->driver->subtype != PTY_TYPE_MASTER)
  919. return -ENOTTY;
  920. if (get_user(pktmode, (int __user *) arg))
  921. return -EFAULT;
  922. spin_lock_irqsave(&tty->ctrl_lock, flags);
  923. if (pktmode) {
  924. if (!tty->packet) {
  925. tty->packet = 1;
  926. tty->link->ctrl_status = 0;
  927. }
  928. } else
  929. tty->packet = 0;
  930. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  931. return 0;
  932. }
  933. default:
  934. /* Try the mode commands */
  935. return tty_mode_ioctl(tty, file, cmd, arg);
  936. }
  937. }
  938. EXPORT_SYMBOL(n_tty_ioctl);