tty_ioctl.c 31 KB

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