tty_ioctl.c 31 KB

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