tty_ioctl.c 31 KB

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