tty_ioctl.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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. * change_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. static void change_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. }
  492. /**
  493. * set_termios - set termios values for a tty
  494. * @tty: terminal device
  495. * @arg: user data
  496. * @opt: option information
  497. *
  498. * Helper function to prepare termios data and run necessary other
  499. * functions before using change_termios to do the actual changes.
  500. *
  501. * Locking:
  502. * Called functions take ldisc and termios_mutex locks
  503. */
  504. static int set_termios(struct tty_struct *tty, void __user *arg, int opt)
  505. {
  506. struct ktermios tmp_termios;
  507. struct tty_ldisc *ld;
  508. int retval = tty_check_change(tty);
  509. if (retval)
  510. return retval;
  511. mutex_lock(&tty->termios_mutex);
  512. memcpy(&tmp_termios, tty->termios, sizeof(struct ktermios));
  513. mutex_unlock(&tty->termios_mutex);
  514. if (opt & TERMIOS_TERMIO) {
  515. if (user_termio_to_kernel_termios(&tmp_termios,
  516. (struct termio __user *)arg))
  517. return -EFAULT;
  518. #ifdef TCGETS2
  519. } else if (opt & TERMIOS_OLD) {
  520. if (user_termios_to_kernel_termios_1(&tmp_termios,
  521. (struct termios __user *)arg))
  522. return -EFAULT;
  523. } else {
  524. if (user_termios_to_kernel_termios(&tmp_termios,
  525. (struct termios2 __user *)arg))
  526. return -EFAULT;
  527. }
  528. #else
  529. } else if (user_termios_to_kernel_termios(&tmp_termios,
  530. (struct termios __user *)arg))
  531. return -EFAULT;
  532. #endif
  533. /* If old style Bfoo values are used then load c_ispeed/c_ospeed
  534. * with the real speed so its unconditionally usable */
  535. tmp_termios.c_ispeed = tty_termios_input_baud_rate(&tmp_termios);
  536. tmp_termios.c_ospeed = tty_termios_baud_rate(&tmp_termios);
  537. ld = tty_ldisc_ref(tty);
  538. if (ld != NULL) {
  539. if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
  540. ld->ops->flush_buffer(tty);
  541. tty_ldisc_deref(ld);
  542. }
  543. if (opt & TERMIOS_WAIT) {
  544. tty_wait_until_sent(tty, 0);
  545. if (signal_pending(current))
  546. return -EINTR;
  547. }
  548. change_termios(tty, &tmp_termios);
  549. /* FIXME: Arguably if tmp_termios == tty->termios AND the
  550. actual requested termios was not tmp_termios then we may
  551. want to return an error as no user requested change has
  552. succeeded */
  553. return 0;
  554. }
  555. static void copy_termios(struct tty_struct *tty, struct ktermios *kterm)
  556. {
  557. mutex_lock(&tty->termios_mutex);
  558. memcpy(kterm, tty->termios, sizeof(struct ktermios));
  559. mutex_unlock(&tty->termios_mutex);
  560. }
  561. static void copy_termios_locked(struct tty_struct *tty, struct ktermios *kterm)
  562. {
  563. mutex_lock(&tty->termios_mutex);
  564. memcpy(kterm, tty->termios_locked, sizeof(struct ktermios));
  565. mutex_unlock(&tty->termios_mutex);
  566. }
  567. static int get_termio(struct tty_struct *tty, struct termio __user *termio)
  568. {
  569. struct ktermios kterm;
  570. copy_termios(tty, &kterm);
  571. if (kernel_termios_to_user_termio(termio, &kterm))
  572. return -EFAULT;
  573. return 0;
  574. }
  575. #ifdef TCGETX
  576. /**
  577. * set_termiox - set termiox fields if possible
  578. * @tty: terminal
  579. * @arg: termiox structure from user
  580. * @opt: option flags for ioctl type
  581. *
  582. * Implement the device calling points for the SYS5 termiox ioctl
  583. * interface in Linux
  584. */
  585. static int set_termiox(struct tty_struct *tty, void __user *arg, int opt)
  586. {
  587. struct termiox tnew;
  588. struct tty_ldisc *ld;
  589. if (tty->termiox == NULL)
  590. return -EINVAL;
  591. if (copy_from_user(&tnew, arg, sizeof(struct termiox)))
  592. return -EFAULT;
  593. ld = tty_ldisc_ref(tty);
  594. if (ld != NULL) {
  595. if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
  596. ld->ops->flush_buffer(tty);
  597. tty_ldisc_deref(ld);
  598. }
  599. if (opt & TERMIOS_WAIT) {
  600. tty_wait_until_sent(tty, 0);
  601. if (signal_pending(current))
  602. return -EINTR;
  603. }
  604. mutex_lock(&tty->termios_mutex);
  605. if (tty->ops->set_termiox)
  606. tty->ops->set_termiox(tty, &tnew);
  607. mutex_unlock(&tty->termios_mutex);
  608. return 0;
  609. }
  610. #endif
  611. #ifdef TIOCGETP
  612. /*
  613. * These are deprecated, but there is limited support..
  614. *
  615. * The "sg_flags" translation is a joke..
  616. */
  617. static int get_sgflags(struct tty_struct *tty)
  618. {
  619. int flags = 0;
  620. if (!(tty->termios->c_lflag & ICANON)) {
  621. if (tty->termios->c_lflag & ISIG)
  622. flags |= 0x02; /* cbreak */
  623. else
  624. flags |= 0x20; /* raw */
  625. }
  626. if (tty->termios->c_lflag & ECHO)
  627. flags |= 0x08; /* echo */
  628. if (tty->termios->c_oflag & OPOST)
  629. if (tty->termios->c_oflag & ONLCR)
  630. flags |= 0x10; /* crmod */
  631. return flags;
  632. }
  633. static int get_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
  634. {
  635. struct sgttyb tmp;
  636. mutex_lock(&tty->termios_mutex);
  637. tmp.sg_ispeed = tty->termios->c_ispeed;
  638. tmp.sg_ospeed = tty->termios->c_ospeed;
  639. tmp.sg_erase = tty->termios->c_cc[VERASE];
  640. tmp.sg_kill = tty->termios->c_cc[VKILL];
  641. tmp.sg_flags = get_sgflags(tty);
  642. mutex_unlock(&tty->termios_mutex);
  643. return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  644. }
  645. static void set_sgflags(struct ktermios *termios, int flags)
  646. {
  647. termios->c_iflag = ICRNL | IXON;
  648. termios->c_oflag = 0;
  649. termios->c_lflag = ISIG | ICANON;
  650. if (flags & 0x02) { /* cbreak */
  651. termios->c_iflag = 0;
  652. termios->c_lflag &= ~ICANON;
  653. }
  654. if (flags & 0x08) { /* echo */
  655. termios->c_lflag |= ECHO | ECHOE | ECHOK |
  656. ECHOCTL | ECHOKE | IEXTEN;
  657. }
  658. if (flags & 0x10) { /* crmod */
  659. termios->c_oflag |= OPOST | ONLCR;
  660. }
  661. if (flags & 0x20) { /* raw */
  662. termios->c_iflag = 0;
  663. termios->c_lflag &= ~(ISIG | ICANON);
  664. }
  665. if (!(termios->c_lflag & ICANON)) {
  666. termios->c_cc[VMIN] = 1;
  667. termios->c_cc[VTIME] = 0;
  668. }
  669. }
  670. /**
  671. * set_sgttyb - set legacy terminal values
  672. * @tty: tty structure
  673. * @sgttyb: pointer to old style terminal structure
  674. *
  675. * Updates a terminal from the legacy BSD style terminal information
  676. * structure.
  677. *
  678. * Locking: termios_mutex
  679. */
  680. static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
  681. {
  682. int retval;
  683. struct sgttyb tmp;
  684. struct ktermios termios;
  685. retval = tty_check_change(tty);
  686. if (retval)
  687. return retval;
  688. if (copy_from_user(&tmp, sgttyb, sizeof(tmp)))
  689. return -EFAULT;
  690. mutex_lock(&tty->termios_mutex);
  691. termios = *tty->termios;
  692. termios.c_cc[VERASE] = tmp.sg_erase;
  693. termios.c_cc[VKILL] = tmp.sg_kill;
  694. set_sgflags(&termios, tmp.sg_flags);
  695. /* Try and encode into Bfoo format */
  696. #ifdef BOTHER
  697. tty_termios_encode_baud_rate(&termios, termios.c_ispeed,
  698. termios.c_ospeed);
  699. #endif
  700. mutex_unlock(&tty->termios_mutex);
  701. change_termios(tty, &termios);
  702. return 0;
  703. }
  704. #endif
  705. #ifdef TIOCGETC
  706. static int get_tchars(struct tty_struct *tty, struct tchars __user *tchars)
  707. {
  708. struct tchars tmp;
  709. mutex_lock(&tty->termios_mutex);
  710. tmp.t_intrc = tty->termios->c_cc[VINTR];
  711. tmp.t_quitc = tty->termios->c_cc[VQUIT];
  712. tmp.t_startc = tty->termios->c_cc[VSTART];
  713. tmp.t_stopc = tty->termios->c_cc[VSTOP];
  714. tmp.t_eofc = tty->termios->c_cc[VEOF];
  715. tmp.t_brkc = tty->termios->c_cc[VEOL2]; /* what is brkc anyway? */
  716. mutex_unlock(&tty->termios_mutex);
  717. return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  718. }
  719. static int set_tchars(struct tty_struct *tty, struct tchars __user *tchars)
  720. {
  721. struct tchars tmp;
  722. if (copy_from_user(&tmp, tchars, sizeof(tmp)))
  723. return -EFAULT;
  724. mutex_lock(&tty->termios_mutex);
  725. tty->termios->c_cc[VINTR] = tmp.t_intrc;
  726. tty->termios->c_cc[VQUIT] = tmp.t_quitc;
  727. tty->termios->c_cc[VSTART] = tmp.t_startc;
  728. tty->termios->c_cc[VSTOP] = tmp.t_stopc;
  729. tty->termios->c_cc[VEOF] = tmp.t_eofc;
  730. tty->termios->c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */
  731. mutex_unlock(&tty->termios_mutex);
  732. return 0;
  733. }
  734. #endif
  735. #ifdef TIOCGLTC
  736. static int get_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
  737. {
  738. struct ltchars tmp;
  739. mutex_lock(&tty->termios_mutex);
  740. tmp.t_suspc = tty->termios->c_cc[VSUSP];
  741. /* what is dsuspc anyway? */
  742. tmp.t_dsuspc = tty->termios->c_cc[VSUSP];
  743. tmp.t_rprntc = tty->termios->c_cc[VREPRINT];
  744. /* what is flushc anyway? */
  745. tmp.t_flushc = tty->termios->c_cc[VEOL2];
  746. tmp.t_werasc = tty->termios->c_cc[VWERASE];
  747. tmp.t_lnextc = tty->termios->c_cc[VLNEXT];
  748. mutex_unlock(&tty->termios_mutex);
  749. return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  750. }
  751. static int set_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
  752. {
  753. struct ltchars tmp;
  754. if (copy_from_user(&tmp, ltchars, sizeof(tmp)))
  755. return -EFAULT;
  756. mutex_lock(&tty->termios_mutex);
  757. tty->termios->c_cc[VSUSP] = tmp.t_suspc;
  758. /* what is dsuspc anyway? */
  759. tty->termios->c_cc[VEOL2] = tmp.t_dsuspc;
  760. tty->termios->c_cc[VREPRINT] = tmp.t_rprntc;
  761. /* what is flushc anyway? */
  762. tty->termios->c_cc[VEOL2] = tmp.t_flushc;
  763. tty->termios->c_cc[VWERASE] = tmp.t_werasc;
  764. tty->termios->c_cc[VLNEXT] = tmp.t_lnextc;
  765. mutex_unlock(&tty->termios_mutex);
  766. return 0;
  767. }
  768. #endif
  769. /**
  770. * send_prio_char - send priority character
  771. *
  772. * Send a high priority character to the tty even if stopped
  773. *
  774. * Locking: none for xchar method, write ordering for write method.
  775. */
  776. static int send_prio_char(struct tty_struct *tty, char ch)
  777. {
  778. int was_stopped = tty->stopped;
  779. if (tty->ops->send_xchar) {
  780. tty->ops->send_xchar(tty, ch);
  781. return 0;
  782. }
  783. if (tty_write_lock(tty, 0) < 0)
  784. return -ERESTARTSYS;
  785. if (was_stopped)
  786. start_tty(tty);
  787. tty->ops->write(tty, &ch, 1);
  788. if (was_stopped)
  789. stop_tty(tty);
  790. tty_write_unlock(tty);
  791. return 0;
  792. }
  793. /**
  794. * tty_change_softcar - carrier change ioctl helper
  795. * @tty: tty to update
  796. * @arg: enable/disable CLOCAL
  797. *
  798. * Perform a change to the CLOCAL state and call into the driver
  799. * layer to make it visible. All done with the termios mutex
  800. */
  801. static int tty_change_softcar(struct tty_struct *tty, int arg)
  802. {
  803. int ret = 0;
  804. int bit = arg ? CLOCAL : 0;
  805. struct ktermios old;
  806. mutex_lock(&tty->termios_mutex);
  807. old = *tty->termios;
  808. tty->termios->c_cflag &= ~CLOCAL;
  809. tty->termios->c_cflag |= bit;
  810. if (tty->ops->set_termios)
  811. tty->ops->set_termios(tty, &old);
  812. if ((tty->termios->c_cflag & CLOCAL) != bit)
  813. ret = -EINVAL;
  814. mutex_unlock(&tty->termios_mutex);
  815. return ret;
  816. }
  817. /**
  818. * tty_mode_ioctl - mode related ioctls
  819. * @tty: tty for the ioctl
  820. * @file: file pointer for the tty
  821. * @cmd: command
  822. * @arg: ioctl argument
  823. *
  824. * Perform non line discipline specific mode control ioctls. This
  825. * is designed to be called by line disciplines to ensure they provide
  826. * consistent mode setting.
  827. */
  828. int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
  829. unsigned int cmd, unsigned long arg)
  830. {
  831. struct tty_struct *real_tty;
  832. void __user *p = (void __user *)arg;
  833. int ret = 0;
  834. struct ktermios kterm;
  835. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  836. tty->driver->subtype == PTY_TYPE_MASTER)
  837. real_tty = tty->link;
  838. else
  839. real_tty = tty;
  840. switch (cmd) {
  841. #ifdef TIOCGETP
  842. case TIOCGETP:
  843. return get_sgttyb(real_tty, (struct sgttyb __user *) arg);
  844. case TIOCSETP:
  845. case TIOCSETN:
  846. return set_sgttyb(real_tty, (struct sgttyb __user *) arg);
  847. #endif
  848. #ifdef TIOCGETC
  849. case TIOCGETC:
  850. return get_tchars(real_tty, p);
  851. case TIOCSETC:
  852. return set_tchars(real_tty, p);
  853. #endif
  854. #ifdef TIOCGLTC
  855. case TIOCGLTC:
  856. return get_ltchars(real_tty, p);
  857. case TIOCSLTC:
  858. return set_ltchars(real_tty, p);
  859. #endif
  860. case TCSETSF:
  861. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_OLD);
  862. case TCSETSW:
  863. return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_OLD);
  864. case TCSETS:
  865. return set_termios(real_tty, p, TERMIOS_OLD);
  866. #ifndef TCGETS2
  867. case TCGETS:
  868. copy_termios(real_tty, &kterm);
  869. if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
  870. ret = -EFAULT;
  871. return ret;
  872. #else
  873. case TCGETS:
  874. copy_termios(real_tty, &kterm);
  875. if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
  876. ret = -EFAULT;
  877. return ret;
  878. case TCGETS2:
  879. copy_termios(real_tty, &kterm);
  880. if (kernel_termios_to_user_termios((struct termios2 __user *)arg, &kterm))
  881. ret = -EFAULT;
  882. return ret;
  883. case TCSETSF2:
  884. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT);
  885. case TCSETSW2:
  886. return set_termios(real_tty, p, TERMIOS_WAIT);
  887. case TCSETS2:
  888. return set_termios(real_tty, p, 0);
  889. #endif
  890. case TCGETA:
  891. return get_termio(real_tty, p);
  892. case TCSETAF:
  893. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_TERMIO);
  894. case TCSETAW:
  895. return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_TERMIO);
  896. case TCSETA:
  897. return set_termios(real_tty, p, TERMIOS_TERMIO);
  898. #ifndef TCGETS2
  899. case TIOCGLCKTRMIOS:
  900. copy_termios_locked(real_tty, &kterm);
  901. if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
  902. ret = -EFAULT;
  903. return ret;
  904. case TIOCSLCKTRMIOS:
  905. if (!capable(CAP_SYS_ADMIN))
  906. return -EPERM;
  907. copy_termios_locked(real_tty, &kterm);
  908. if (user_termios_to_kernel_termios(&kterm,
  909. (struct termios __user *) arg))
  910. return -EFAULT;
  911. mutex_lock(&real_tty->termios_mutex);
  912. memcpy(real_tty->termios_locked, &kterm, sizeof(struct ktermios));
  913. mutex_unlock(&real_tty->termios_mutex);
  914. return 0;
  915. #else
  916. case TIOCGLCKTRMIOS:
  917. copy_termios_locked(real_tty, &kterm);
  918. if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
  919. ret = -EFAULT;
  920. return ret;
  921. case TIOCSLCKTRMIOS:
  922. if (!capable(CAP_SYS_ADMIN))
  923. return -EPERM;
  924. copy_termios_locked(real_tty, &kterm);
  925. if (user_termios_to_kernel_termios_1(&kterm,
  926. (struct termios __user *) arg))
  927. return -EFAULT;
  928. mutex_lock(&real_tty->termios_mutex);
  929. memcpy(real_tty->termios_locked, &kterm, sizeof(struct ktermios));
  930. mutex_unlock(&real_tty->termios_mutex);
  931. return ret;
  932. #endif
  933. #ifdef TCGETX
  934. case TCGETX: {
  935. struct termiox ktermx;
  936. if (real_tty->termiox == NULL)
  937. return -EINVAL;
  938. mutex_lock(&real_tty->termios_mutex);
  939. memcpy(&ktermx, real_tty->termiox, sizeof(struct termiox));
  940. mutex_unlock(&real_tty->termios_mutex);
  941. if (copy_to_user(p, &ktermx, sizeof(struct termiox)))
  942. ret = -EFAULT;
  943. return ret;
  944. }
  945. case TCSETX:
  946. return set_termiox(real_tty, p, 0);
  947. case TCSETXW:
  948. return set_termiox(real_tty, p, TERMIOS_WAIT);
  949. case TCSETXF:
  950. return set_termiox(real_tty, p, TERMIOS_FLUSH);
  951. #endif
  952. case TIOCGSOFTCAR:
  953. copy_termios(real_tty, &kterm);
  954. ret = put_user((kterm.c_cflag & CLOCAL) ? 1 : 0,
  955. (int __user *)arg);
  956. return ret;
  957. case TIOCSSOFTCAR:
  958. if (get_user(arg, (unsigned int __user *) arg))
  959. return -EFAULT;
  960. return tty_change_softcar(real_tty, arg);
  961. default:
  962. return -ENOIOCTLCMD;
  963. }
  964. }
  965. EXPORT_SYMBOL_GPL(tty_mode_ioctl);
  966. int tty_perform_flush(struct tty_struct *tty, unsigned long arg)
  967. {
  968. struct tty_ldisc *ld;
  969. int retval = tty_check_change(tty);
  970. if (retval)
  971. return retval;
  972. ld = tty_ldisc_ref_wait(tty);
  973. switch (arg) {
  974. case TCIFLUSH:
  975. if (ld && ld->ops->flush_buffer)
  976. ld->ops->flush_buffer(tty);
  977. break;
  978. case TCIOFLUSH:
  979. if (ld && ld->ops->flush_buffer)
  980. ld->ops->flush_buffer(tty);
  981. /* fall through */
  982. case TCOFLUSH:
  983. tty_driver_flush_buffer(tty);
  984. break;
  985. default:
  986. tty_ldisc_deref(ld);
  987. return -EINVAL;
  988. }
  989. tty_ldisc_deref(ld);
  990. return 0;
  991. }
  992. EXPORT_SYMBOL_GPL(tty_perform_flush);
  993. int n_tty_ioctl_helper(struct tty_struct *tty, struct file *file,
  994. unsigned int cmd, unsigned long arg)
  995. {
  996. unsigned long flags;
  997. int retval;
  998. switch (cmd) {
  999. case TCXONC:
  1000. retval = tty_check_change(tty);
  1001. if (retval)
  1002. return retval;
  1003. switch (arg) {
  1004. case TCOOFF:
  1005. if (!tty->flow_stopped) {
  1006. tty->flow_stopped = 1;
  1007. stop_tty(tty);
  1008. }
  1009. break;
  1010. case TCOON:
  1011. if (tty->flow_stopped) {
  1012. tty->flow_stopped = 0;
  1013. start_tty(tty);
  1014. }
  1015. break;
  1016. case TCIOFF:
  1017. if (STOP_CHAR(tty) != __DISABLED_CHAR)
  1018. return send_prio_char(tty, STOP_CHAR(tty));
  1019. break;
  1020. case TCION:
  1021. if (START_CHAR(tty) != __DISABLED_CHAR)
  1022. return send_prio_char(tty, START_CHAR(tty));
  1023. break;
  1024. default:
  1025. return -EINVAL;
  1026. }
  1027. return 0;
  1028. case TCFLSH:
  1029. return tty_perform_flush(tty, arg);
  1030. case TIOCPKT:
  1031. {
  1032. int pktmode;
  1033. if (tty->driver->type != TTY_DRIVER_TYPE_PTY ||
  1034. tty->driver->subtype != PTY_TYPE_MASTER)
  1035. return -ENOTTY;
  1036. if (get_user(pktmode, (int __user *) arg))
  1037. return -EFAULT;
  1038. spin_lock_irqsave(&tty->ctrl_lock, flags);
  1039. if (pktmode) {
  1040. if (!tty->packet) {
  1041. tty->packet = 1;
  1042. tty->link->ctrl_status = 0;
  1043. }
  1044. } else
  1045. tty->packet = 0;
  1046. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  1047. return 0;
  1048. }
  1049. default:
  1050. /* Try the mode commands */
  1051. return tty_mode_ioctl(tty, file, cmd, arg);
  1052. }
  1053. }
  1054. EXPORT_SYMBOL(n_tty_ioctl_helper);