tty_ioctl.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  1. /*
  2. * linux/drivers/char/tty_ioctl.c
  3. *
  4. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  5. *
  6. * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
  7. * which can be dynamically activated and de-activated by the line
  8. * discipline handling modules (like SLIP).
  9. */
  10. #include <linux/types.h>
  11. #include <linux/termios.h>
  12. #include <linux/errno.h>
  13. #include <linux/sched.h>
  14. #include <linux/kernel.h>
  15. #include <linux/major.h>
  16. #include <linux/tty.h>
  17. #include <linux/fcntl.h>
  18. #include <linux/string.h>
  19. #include <linux/mm.h>
  20. #include <linux/module.h>
  21. #include <linux/bitops.h>
  22. #include <linux/mutex.h>
  23. #include <linux/smp_lock.h>
  24. #include <asm/io.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/system.h>
  27. #undef TTY_DEBUG_WAIT_UNTIL_SENT
  28. #undef DEBUG
  29. /*
  30. * Internal flag options for termios setting behavior
  31. */
  32. #define TERMIOS_FLUSH 1
  33. #define TERMIOS_WAIT 2
  34. #define TERMIOS_TERMIO 4
  35. #define TERMIOS_OLD 8
  36. /**
  37. * tty_chars_in_buffer - characters pending
  38. * @tty: terminal
  39. *
  40. * Return the number of bytes of data in the device private
  41. * output queue. If no private method is supplied there is assumed
  42. * to be no queue on the device.
  43. */
  44. int tty_chars_in_buffer(struct tty_struct *tty)
  45. {
  46. if (tty->ops->chars_in_buffer)
  47. return tty->ops->chars_in_buffer(tty);
  48. else
  49. return 0;
  50. }
  51. EXPORT_SYMBOL(tty_chars_in_buffer);
  52. /**
  53. * tty_write_room - write queue space
  54. * @tty: terminal
  55. *
  56. * Return the number of bytes that can be queued to this device
  57. * at the present time. The result should be treated as a guarantee
  58. * and the driver cannot offer a value it later shrinks by more than
  59. * the number of bytes written. If no method is provided 2K is always
  60. * returned and data may be lost as there will be no flow control.
  61. */
  62. int tty_write_room(struct tty_struct *tty)
  63. {
  64. if (tty->ops->write_room)
  65. return tty->ops->write_room(tty);
  66. return 2048;
  67. }
  68. EXPORT_SYMBOL(tty_write_room);
  69. /**
  70. * tty_driver_flush_buffer - discard internal buffer
  71. * @tty: terminal
  72. *
  73. * Discard the internal output buffer for this device. If no method
  74. * is provided then either the buffer cannot be hardware flushed or
  75. * there is no buffer driver side.
  76. */
  77. void tty_driver_flush_buffer(struct tty_struct *tty)
  78. {
  79. if (tty->ops->flush_buffer)
  80. tty->ops->flush_buffer(tty);
  81. }
  82. EXPORT_SYMBOL(tty_driver_flush_buffer);
  83. /**
  84. * tty_throttle - flow control
  85. * @tty: terminal
  86. *
  87. * Indicate that a tty should stop transmitting data down the stack.
  88. * Takes the termios mutex to protect against parallel throttle/unthrottle
  89. * and also to ensure the driver can consistently reference its own
  90. * termios data at this point when implementing software flow control.
  91. */
  92. void tty_throttle(struct tty_struct *tty)
  93. {
  94. mutex_lock(&tty->termios_mutex);
  95. /* check TTY_THROTTLED first so it indicates our state */
  96. if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
  97. tty->ops->throttle)
  98. tty->ops->throttle(tty);
  99. mutex_unlock(&tty->termios_mutex);
  100. }
  101. EXPORT_SYMBOL(tty_throttle);
  102. /**
  103. * tty_unthrottle - flow control
  104. * @tty: terminal
  105. *
  106. * Indicate that a tty may continue transmitting data down the stack.
  107. * Takes the termios mutex to protect against parallel throttle/unthrottle
  108. * and also to ensure the driver can consistently reference its own
  109. * termios data at this point when implementing software flow control.
  110. *
  111. * Drivers should however remember that the stack can issue a throttle,
  112. * then change flow control method, then unthrottle.
  113. */
  114. void tty_unthrottle(struct tty_struct *tty)
  115. {
  116. mutex_lock(&tty->termios_mutex);
  117. if (test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
  118. tty->ops->unthrottle)
  119. tty->ops->unthrottle(tty);
  120. mutex_unlock(&tty->termios_mutex);
  121. }
  122. EXPORT_SYMBOL(tty_unthrottle);
  123. /**
  124. * tty_wait_until_sent - wait for I/O to finish
  125. * @tty: tty we are waiting for
  126. * @timeout: how long we will wait
  127. *
  128. * Wait for characters pending in a tty driver to hit the wire, or
  129. * for a timeout to occur (eg due to flow control)
  130. *
  131. * Locking: none
  132. */
  133. void tty_wait_until_sent(struct tty_struct *tty, long timeout)
  134. {
  135. #ifdef TTY_DEBUG_WAIT_UNTIL_SENT
  136. char buf[64];
  137. printk(KERN_DEBUG "%s wait until sent...\n", tty_name(tty, buf));
  138. #endif
  139. if (!timeout)
  140. timeout = MAX_SCHEDULE_TIMEOUT;
  141. if (wait_event_interruptible_timeout(tty->write_wait,
  142. !tty_chars_in_buffer(tty), timeout) >= 0) {
  143. if (tty->ops->wait_until_sent)
  144. tty->ops->wait_until_sent(tty, timeout);
  145. }
  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 arbitary 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 arbitary 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 os 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 compatiblity.
  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. static int warned;
  351. if (!warned++)
  352. printk(KERN_WARNING "tty: Unable to return correct "
  353. "speed data as your architecture needs updating.\n");
  354. }
  355. #endif
  356. }
  357. EXPORT_SYMBOL_GPL(tty_termios_encode_baud_rate);
  358. /**
  359. * tty_encode_baud_rate - set baud rate of the tty
  360. * @ibaud: input baud rate
  361. * @obad: output baud rate
  362. *
  363. * Update the current termios data for the tty with the new speed
  364. * settings. The caller must hold the termios_mutex for the tty in
  365. * question.
  366. */
  367. void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud, speed_t obaud)
  368. {
  369. tty_termios_encode_baud_rate(tty->termios, ibaud, obaud);
  370. }
  371. EXPORT_SYMBOL_GPL(tty_encode_baud_rate);
  372. /**
  373. * tty_get_baud_rate - get tty bit rates
  374. * @tty: tty to query
  375. *
  376. * Returns the baud rate as an integer for this terminal. The
  377. * termios lock must be held by the caller and the terminal bit
  378. * flags may be updated.
  379. *
  380. * Locking: none
  381. */
  382. speed_t tty_get_baud_rate(struct tty_struct *tty)
  383. {
  384. speed_t baud = tty_termios_baud_rate(tty->termios);
  385. if (baud == 38400 && tty->alt_speed) {
  386. if (!tty->warned) {
  387. printk(KERN_WARNING "Use of setserial/setrocket to "
  388. "set SPD_* flags is deprecated\n");
  389. tty->warned = 1;
  390. }
  391. baud = tty->alt_speed;
  392. }
  393. return baud;
  394. }
  395. EXPORT_SYMBOL(tty_get_baud_rate);
  396. /**
  397. * tty_termios_copy_hw - copy hardware settings
  398. * @new: New termios
  399. * @old: Old termios
  400. *
  401. * Propogate the hardware specific terminal setting bits from
  402. * the old termios structure to the new one. This is used in cases
  403. * where the hardware does not support reconfiguration or as a helper
  404. * in some cases where only minimal reconfiguration is supported
  405. */
  406. void tty_termios_copy_hw(struct ktermios *new, struct ktermios *old)
  407. {
  408. /* The bits a dumb device handles in software. Smart devices need
  409. to always provide a set_termios method */
  410. new->c_cflag &= HUPCL | CREAD | CLOCAL;
  411. new->c_cflag |= old->c_cflag & ~(HUPCL | CREAD | CLOCAL);
  412. new->c_ispeed = old->c_ispeed;
  413. new->c_ospeed = old->c_ospeed;
  414. }
  415. EXPORT_SYMBOL(tty_termios_copy_hw);
  416. /**
  417. * tty_termios_hw_change - check for setting change
  418. * @a: termios
  419. * @b: termios to compare
  420. *
  421. * Check if any of the bits that affect a dumb device have changed
  422. * between the two termios structures, or a speed change is needed.
  423. */
  424. int tty_termios_hw_change(struct ktermios *a, struct ktermios *b)
  425. {
  426. if (a->c_ispeed != b->c_ispeed || a->c_ospeed != b->c_ospeed)
  427. return 1;
  428. if ((a->c_cflag ^ b->c_cflag) & ~(HUPCL | CREAD | CLOCAL))
  429. return 1;
  430. return 0;
  431. }
  432. EXPORT_SYMBOL(tty_termios_hw_change);
  433. /**
  434. * change_termios - update termios values
  435. * @tty: tty to update
  436. * @new_termios: desired new value
  437. *
  438. * Perform updates to the termios values set on this terminal. There
  439. * is a bit of layering violation here with n_tty in terms of the
  440. * internal knowledge of this function.
  441. *
  442. * Locking: termios_mutex
  443. */
  444. static void change_termios(struct tty_struct *tty, struct ktermios *new_termios)
  445. {
  446. struct ktermios old_termios;
  447. struct tty_ldisc *ld;
  448. unsigned long flags;
  449. /*
  450. * Perform the actual termios internal changes under lock.
  451. */
  452. /* FIXME: we need to decide on some locking/ordering semantics
  453. for the set_termios notification eventually */
  454. mutex_lock(&tty->termios_mutex);
  455. old_termios = *tty->termios;
  456. *tty->termios = *new_termios;
  457. unset_locked_termios(tty->termios, &old_termios, tty->termios_locked);
  458. /* See if packet mode change of state. */
  459. if (tty->link && tty->link->packet) {
  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) {
  467. spin_lock_irqsave(&tty->ctrl_lock, flags);
  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. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  474. wake_up_interruptible(&tty->link->read_wait);
  475. }
  476. }
  477. if (tty->ops->set_termios)
  478. (*tty->ops->set_termios)(tty, &old_termios);
  479. else
  480. tty_termios_copy_hw(tty->termios, &old_termios);
  481. ld = tty_ldisc_ref(tty);
  482. if (ld != NULL) {
  483. if (ld->ops->set_termios)
  484. (ld->ops->set_termios)(tty, &old_termios);
  485. tty_ldisc_deref(ld);
  486. }
  487. mutex_unlock(&tty->termios_mutex);
  488. }
  489. /**
  490. * set_termios - set termios values for a tty
  491. * @tty: terminal device
  492. * @arg: user data
  493. * @opt: option information
  494. *
  495. * Helper function to prepare termios data and run necessary other
  496. * functions before using change_termios to do the actual changes.
  497. *
  498. * Locking:
  499. * Called functions take ldisc and termios_mutex locks
  500. */
  501. static int set_termios(struct tty_struct *tty, void __user *arg, int opt)
  502. {
  503. struct ktermios tmp_termios;
  504. struct tty_ldisc *ld;
  505. int retval = tty_check_change(tty);
  506. if (retval)
  507. return retval;
  508. mutex_lock(&tty->termios_mutex);
  509. memcpy(&tmp_termios, tty->termios, sizeof(struct ktermios));
  510. mutex_unlock(&tty->termios_mutex);
  511. if (opt & TERMIOS_TERMIO) {
  512. if (user_termio_to_kernel_termios(&tmp_termios,
  513. (struct termio __user *)arg))
  514. return -EFAULT;
  515. #ifdef TCGETS2
  516. } else if (opt & TERMIOS_OLD) {
  517. if (user_termios_to_kernel_termios_1(&tmp_termios,
  518. (struct termios __user *)arg))
  519. return -EFAULT;
  520. } else {
  521. if (user_termios_to_kernel_termios(&tmp_termios,
  522. (struct termios2 __user *)arg))
  523. return -EFAULT;
  524. }
  525. #else
  526. } else if (user_termios_to_kernel_termios(&tmp_termios,
  527. (struct termios __user *)arg))
  528. return -EFAULT;
  529. #endif
  530. /* If old style Bfoo values are used then load c_ispeed/c_ospeed
  531. * with the real speed so its unconditionally usable */
  532. tmp_termios.c_ispeed = tty_termios_input_baud_rate(&tmp_termios);
  533. tmp_termios.c_ospeed = tty_termios_baud_rate(&tmp_termios);
  534. ld = tty_ldisc_ref(tty);
  535. if (ld != NULL) {
  536. if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
  537. ld->ops->flush_buffer(tty);
  538. tty_ldisc_deref(ld);
  539. }
  540. if (opt & TERMIOS_WAIT) {
  541. tty_wait_until_sent(tty, 0);
  542. if (signal_pending(current))
  543. return -EINTR;
  544. }
  545. change_termios(tty, &tmp_termios);
  546. /* FIXME: Arguably if tmp_termios == tty->termios AND the
  547. actual requested termios was not tmp_termios then we may
  548. want to return an error as no user requested change has
  549. succeeded */
  550. return 0;
  551. }
  552. static void copy_termios(struct tty_struct *tty, struct ktermios *kterm)
  553. {
  554. mutex_lock(&tty->termios_mutex);
  555. memcpy(kterm, tty->termios, sizeof(struct ktermios));
  556. mutex_unlock(&tty->termios_mutex);
  557. }
  558. static void copy_termios_locked(struct tty_struct *tty, struct ktermios *kterm)
  559. {
  560. mutex_lock(&tty->termios_mutex);
  561. memcpy(kterm, tty->termios_locked, sizeof(struct ktermios));
  562. mutex_unlock(&tty->termios_mutex);
  563. }
  564. static int get_termio(struct tty_struct *tty, struct termio __user *termio)
  565. {
  566. struct ktermios kterm;
  567. copy_termios(tty, &kterm);
  568. if (kernel_termios_to_user_termio(termio, &kterm))
  569. return -EFAULT;
  570. return 0;
  571. }
  572. #ifdef TCGETX
  573. /**
  574. * set_termiox - set termiox fields if possible
  575. * @tty: terminal
  576. * @arg: termiox structure from user
  577. * @opt: option flags for ioctl type
  578. *
  579. * Implement the device calling points for the SYS5 termiox ioctl
  580. * interface in Linux
  581. */
  582. static int set_termiox(struct tty_struct *tty, void __user *arg, int opt)
  583. {
  584. struct termiox tnew;
  585. struct tty_ldisc *ld;
  586. if (tty->termiox == NULL)
  587. return -EINVAL;
  588. if (copy_from_user(&tnew, arg, sizeof(struct termiox)))
  589. return -EFAULT;
  590. ld = tty_ldisc_ref(tty);
  591. if (ld != NULL) {
  592. if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
  593. ld->ops->flush_buffer(tty);
  594. tty_ldisc_deref(ld);
  595. }
  596. if (opt & TERMIOS_WAIT) {
  597. tty_wait_until_sent(tty, 0);
  598. if (signal_pending(current))
  599. return -EINTR;
  600. }
  601. mutex_lock(&tty->termios_mutex);
  602. if (tty->ops->set_termiox)
  603. tty->ops->set_termiox(tty, &tnew);
  604. mutex_unlock(&tty->termios_mutex);
  605. return 0;
  606. }
  607. #endif
  608. #ifdef TIOCGETP
  609. /*
  610. * These are deprecated, but there is limited support..
  611. *
  612. * The "sg_flags" translation is a joke..
  613. */
  614. static int get_sgflags(struct tty_struct *tty)
  615. {
  616. int flags = 0;
  617. if (!(tty->termios->c_lflag & ICANON)) {
  618. if (tty->termios->c_lflag & ISIG)
  619. flags |= 0x02; /* cbreak */
  620. else
  621. flags |= 0x20; /* raw */
  622. }
  623. if (tty->termios->c_lflag & ECHO)
  624. flags |= 0x08; /* echo */
  625. if (tty->termios->c_oflag & OPOST)
  626. if (tty->termios->c_oflag & ONLCR)
  627. flags |= 0x10; /* crmod */
  628. return flags;
  629. }
  630. static int get_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
  631. {
  632. struct sgttyb tmp;
  633. mutex_lock(&tty->termios_mutex);
  634. tmp.sg_ispeed = tty->termios->c_ispeed;
  635. tmp.sg_ospeed = tty->termios->c_ospeed;
  636. tmp.sg_erase = tty->termios->c_cc[VERASE];
  637. tmp.sg_kill = tty->termios->c_cc[VKILL];
  638. tmp.sg_flags = get_sgflags(tty);
  639. mutex_unlock(&tty->termios_mutex);
  640. return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  641. }
  642. static void set_sgflags(struct ktermios *termios, int flags)
  643. {
  644. termios->c_iflag = ICRNL | IXON;
  645. termios->c_oflag = 0;
  646. termios->c_lflag = ISIG | ICANON;
  647. if (flags & 0x02) { /* cbreak */
  648. termios->c_iflag = 0;
  649. termios->c_lflag &= ~ICANON;
  650. }
  651. if (flags & 0x08) { /* echo */
  652. termios->c_lflag |= ECHO | ECHOE | ECHOK |
  653. ECHOCTL | ECHOKE | IEXTEN;
  654. }
  655. if (flags & 0x10) { /* crmod */
  656. termios->c_oflag |= OPOST | ONLCR;
  657. }
  658. if (flags & 0x20) { /* raw */
  659. termios->c_iflag = 0;
  660. termios->c_lflag &= ~(ISIG | ICANON);
  661. }
  662. if (!(termios->c_lflag & ICANON)) {
  663. termios->c_cc[VMIN] = 1;
  664. termios->c_cc[VTIME] = 0;
  665. }
  666. }
  667. /**
  668. * set_sgttyb - set legacy terminal values
  669. * @tty: tty structure
  670. * @sgttyb: pointer to old style terminal structure
  671. *
  672. * Updates a terminal from the legacy BSD style terminal information
  673. * structure.
  674. *
  675. * Locking: termios_mutex
  676. */
  677. static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
  678. {
  679. int retval;
  680. struct sgttyb tmp;
  681. struct ktermios termios;
  682. retval = tty_check_change(tty);
  683. if (retval)
  684. return retval;
  685. if (copy_from_user(&tmp, sgttyb, sizeof(tmp)))
  686. return -EFAULT;
  687. mutex_lock(&tty->termios_mutex);
  688. termios = *tty->termios;
  689. termios.c_cc[VERASE] = tmp.sg_erase;
  690. termios.c_cc[VKILL] = tmp.sg_kill;
  691. set_sgflags(&termios, tmp.sg_flags);
  692. /* Try and encode into Bfoo format */
  693. #ifdef BOTHER
  694. tty_termios_encode_baud_rate(&termios, termios.c_ispeed,
  695. termios.c_ospeed);
  696. #endif
  697. mutex_unlock(&tty->termios_mutex);
  698. change_termios(tty, &termios);
  699. return 0;
  700. }
  701. #endif
  702. #ifdef TIOCGETC
  703. static int get_tchars(struct tty_struct *tty, struct tchars __user *tchars)
  704. {
  705. struct tchars tmp;
  706. mutex_lock(&tty->termios_mutex);
  707. tmp.t_intrc = tty->termios->c_cc[VINTR];
  708. tmp.t_quitc = tty->termios->c_cc[VQUIT];
  709. tmp.t_startc = tty->termios->c_cc[VSTART];
  710. tmp.t_stopc = tty->termios->c_cc[VSTOP];
  711. tmp.t_eofc = tty->termios->c_cc[VEOF];
  712. tmp.t_brkc = tty->termios->c_cc[VEOL2]; /* what is brkc anyway? */
  713. mutex_unlock(&tty->termios_mutex);
  714. return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  715. }
  716. static int set_tchars(struct tty_struct *tty, struct tchars __user *tchars)
  717. {
  718. struct tchars tmp;
  719. if (copy_from_user(&tmp, tchars, sizeof(tmp)))
  720. return -EFAULT;
  721. mutex_lock(&tty->termios_mutex);
  722. tty->termios->c_cc[VINTR] = tmp.t_intrc;
  723. tty->termios->c_cc[VQUIT] = tmp.t_quitc;
  724. tty->termios->c_cc[VSTART] = tmp.t_startc;
  725. tty->termios->c_cc[VSTOP] = tmp.t_stopc;
  726. tty->termios->c_cc[VEOF] = tmp.t_eofc;
  727. tty->termios->c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */
  728. mutex_unlock(&tty->termios_mutex);
  729. return 0;
  730. }
  731. #endif
  732. #ifdef TIOCGLTC
  733. static int get_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
  734. {
  735. struct ltchars tmp;
  736. mutex_lock(&tty->termios_mutex);
  737. tmp.t_suspc = tty->termios->c_cc[VSUSP];
  738. /* what is dsuspc anyway? */
  739. tmp.t_dsuspc = tty->termios->c_cc[VSUSP];
  740. tmp.t_rprntc = tty->termios->c_cc[VREPRINT];
  741. /* what is flushc anyway? */
  742. tmp.t_flushc = tty->termios->c_cc[VEOL2];
  743. tmp.t_werasc = tty->termios->c_cc[VWERASE];
  744. tmp.t_lnextc = tty->termios->c_cc[VLNEXT];
  745. mutex_unlock(&tty->termios_mutex);
  746. return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  747. }
  748. static int set_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
  749. {
  750. struct ltchars tmp;
  751. if (copy_from_user(&tmp, ltchars, sizeof(tmp)))
  752. return -EFAULT;
  753. mutex_lock(&tty->termios_mutex);
  754. tty->termios->c_cc[VSUSP] = tmp.t_suspc;
  755. /* what is dsuspc anyway? */
  756. tty->termios->c_cc[VEOL2] = tmp.t_dsuspc;
  757. tty->termios->c_cc[VREPRINT] = tmp.t_rprntc;
  758. /* what is flushc anyway? */
  759. tty->termios->c_cc[VEOL2] = tmp.t_flushc;
  760. tty->termios->c_cc[VWERASE] = tmp.t_werasc;
  761. tty->termios->c_cc[VLNEXT] = tmp.t_lnextc;
  762. mutex_unlock(&tty->termios_mutex);
  763. return 0;
  764. }
  765. #endif
  766. /**
  767. * send_prio_char - send priority character
  768. *
  769. * Send a high priority character to the tty even if stopped
  770. *
  771. * Locking: none for xchar method, write ordering for write method.
  772. */
  773. static int send_prio_char(struct tty_struct *tty, char ch)
  774. {
  775. int was_stopped = tty->stopped;
  776. if (tty->ops->send_xchar) {
  777. tty->ops->send_xchar(tty, ch);
  778. return 0;
  779. }
  780. if (tty_write_lock(tty, 0) < 0)
  781. return -ERESTARTSYS;
  782. if (was_stopped)
  783. start_tty(tty);
  784. tty->ops->write(tty, &ch, 1);
  785. if (was_stopped)
  786. stop_tty(tty);
  787. tty_write_unlock(tty);
  788. return 0;
  789. }
  790. /**
  791. * tty_change_softcar - carrier change ioctl helper
  792. * @tty: tty to update
  793. * @arg: enable/disable CLOCAL
  794. *
  795. * Perform a change to the CLOCAL state and call into the driver
  796. * layer to make it visible. All done with the termios mutex
  797. */
  798. static int tty_change_softcar(struct tty_struct *tty, int arg)
  799. {
  800. int ret = 0;
  801. int bit = arg ? CLOCAL : 0;
  802. struct ktermios old;
  803. mutex_lock(&tty->termios_mutex);
  804. old = *tty->termios;
  805. tty->termios->c_cflag &= ~CLOCAL;
  806. tty->termios->c_cflag |= bit;
  807. if (tty->ops->set_termios)
  808. tty->ops->set_termios(tty, &old);
  809. if ((tty->termios->c_cflag & CLOCAL) != bit)
  810. ret = -EINVAL;
  811. mutex_unlock(&tty->termios_mutex);
  812. return ret;
  813. }
  814. /**
  815. * tty_mode_ioctl - mode related ioctls
  816. * @tty: tty for the ioctl
  817. * @file: file pointer for the tty
  818. * @cmd: command
  819. * @arg: ioctl argument
  820. *
  821. * Perform non line discipline specific mode control ioctls. This
  822. * is designed to be called by line disciplines to ensure they provide
  823. * consistent mode setting.
  824. */
  825. int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
  826. unsigned int cmd, unsigned long arg)
  827. {
  828. struct tty_struct *real_tty;
  829. void __user *p = (void __user *)arg;
  830. int ret = 0;
  831. struct ktermios kterm;
  832. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  833. tty->driver->subtype == PTY_TYPE_MASTER)
  834. real_tty = tty->link;
  835. else
  836. real_tty = tty;
  837. switch (cmd) {
  838. #ifdef TIOCGETP
  839. case TIOCGETP:
  840. return get_sgttyb(real_tty, (struct sgttyb __user *) arg);
  841. case TIOCSETP:
  842. case TIOCSETN:
  843. return set_sgttyb(real_tty, (struct sgttyb __user *) arg);
  844. #endif
  845. #ifdef TIOCGETC
  846. case TIOCGETC:
  847. return get_tchars(real_tty, p);
  848. case TIOCSETC:
  849. return set_tchars(real_tty, p);
  850. #endif
  851. #ifdef TIOCGLTC
  852. case TIOCGLTC:
  853. return get_ltchars(real_tty, p);
  854. case TIOCSLTC:
  855. return set_ltchars(real_tty, p);
  856. #endif
  857. case TCSETSF:
  858. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_OLD);
  859. case TCSETSW:
  860. return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_OLD);
  861. case TCSETS:
  862. return set_termios(real_tty, p, TERMIOS_OLD);
  863. #ifndef TCGETS2
  864. case TCGETS:
  865. copy_termios(real_tty, &kterm);
  866. if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
  867. ret = -EFAULT;
  868. return ret;
  869. #else
  870. case TCGETS:
  871. copy_termios(real_tty, &kterm);
  872. if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
  873. ret = -EFAULT;
  874. return ret;
  875. case TCGETS2:
  876. copy_termios(real_tty, &kterm);
  877. if (kernel_termios_to_user_termios((struct termios2 __user *)arg, &kterm))
  878. ret = -EFAULT;
  879. return ret;
  880. case TCSETSF2:
  881. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT);
  882. case TCSETSW2:
  883. return set_termios(real_tty, p, TERMIOS_WAIT);
  884. case TCSETS2:
  885. return set_termios(real_tty, p, 0);
  886. #endif
  887. case TCGETA:
  888. return get_termio(real_tty, p);
  889. case TCSETAF:
  890. return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_TERMIO);
  891. case TCSETAW:
  892. return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_TERMIO);
  893. case TCSETA:
  894. return set_termios(real_tty, p, TERMIOS_TERMIO);
  895. #ifndef TCGETS2
  896. case TIOCGLCKTRMIOS:
  897. copy_termios_locked(real_tty, &kterm);
  898. if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
  899. ret = -EFAULT;
  900. return ret;
  901. case TIOCSLCKTRMIOS:
  902. if (!capable(CAP_SYS_ADMIN))
  903. return -EPERM;
  904. copy_termios_locked(real_tty, &kterm);
  905. if (user_termios_to_kernel_termios(&kterm,
  906. (struct termios __user *) arg))
  907. return -EFAULT;
  908. mutex_lock(&real_tty->termios_mutex);
  909. memcpy(real_tty->termios_locked, &kterm, sizeof(struct ktermios));
  910. mutex_unlock(&real_tty->termios_mutex);
  911. return 0;
  912. #else
  913. case TIOCGLCKTRMIOS:
  914. copy_termios_locked(real_tty, &kterm);
  915. if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
  916. ret = -EFAULT;
  917. return ret;
  918. case TIOCSLCKTRMIOS:
  919. if (!capable(CAP_SYS_ADMIN))
  920. return -EPERM;
  921. copy_termios_locked(real_tty, &kterm);
  922. if (user_termios_to_kernel_termios_1(&kterm,
  923. (struct termios __user *) arg))
  924. return -EFAULT;
  925. mutex_lock(&real_tty->termios_mutex);
  926. memcpy(real_tty->termios_locked, &kterm, sizeof(struct ktermios));
  927. mutex_unlock(&real_tty->termios_mutex);
  928. return ret;
  929. #endif
  930. #ifdef TCGETX
  931. case TCGETX: {
  932. struct termiox ktermx;
  933. if (real_tty->termiox == NULL)
  934. return -EINVAL;
  935. mutex_lock(&real_tty->termios_mutex);
  936. memcpy(&ktermx, real_tty->termiox, sizeof(struct termiox));
  937. mutex_unlock(&real_tty->termios_mutex);
  938. if (copy_to_user(p, &ktermx, sizeof(struct termiox)))
  939. ret = -EFAULT;
  940. return ret;
  941. }
  942. case TCSETX:
  943. return set_termiox(real_tty, p, 0);
  944. case TCSETXW:
  945. return set_termiox(real_tty, p, TERMIOS_WAIT);
  946. case TCSETXF:
  947. return set_termiox(real_tty, p, TERMIOS_FLUSH);
  948. #endif
  949. case TIOCGSOFTCAR:
  950. copy_termios(real_tty, &kterm);
  951. ret = put_user((kterm.c_cflag & CLOCAL) ? 1 : 0,
  952. (int __user *)arg);
  953. return ret;
  954. case TIOCSSOFTCAR:
  955. if (get_user(arg, (unsigned int __user *) arg))
  956. return -EFAULT;
  957. return tty_change_softcar(real_tty, arg);
  958. default:
  959. return -ENOIOCTLCMD;
  960. }
  961. }
  962. EXPORT_SYMBOL_GPL(tty_mode_ioctl);
  963. int tty_perform_flush(struct tty_struct *tty, unsigned long arg)
  964. {
  965. struct tty_ldisc *ld;
  966. int retval = tty_check_change(tty);
  967. if (retval)
  968. return retval;
  969. ld = tty_ldisc_ref_wait(tty);
  970. switch (arg) {
  971. case TCIFLUSH:
  972. if (ld && ld->ops->flush_buffer)
  973. ld->ops->flush_buffer(tty);
  974. break;
  975. case TCIOFLUSH:
  976. if (ld && ld->ops->flush_buffer)
  977. ld->ops->flush_buffer(tty);
  978. /* fall through */
  979. case TCOFLUSH:
  980. tty_driver_flush_buffer(tty);
  981. break;
  982. default:
  983. tty_ldisc_deref(ld);
  984. return -EINVAL;
  985. }
  986. tty_ldisc_deref(ld);
  987. return 0;
  988. }
  989. EXPORT_SYMBOL_GPL(tty_perform_flush);
  990. int n_tty_ioctl_helper(struct tty_struct *tty, struct file *file,
  991. unsigned int cmd, unsigned long arg)
  992. {
  993. unsigned long flags;
  994. int retval;
  995. switch (cmd) {
  996. case TCXONC:
  997. retval = tty_check_change(tty);
  998. if (retval)
  999. return retval;
  1000. switch (arg) {
  1001. case TCOOFF:
  1002. if (!tty->flow_stopped) {
  1003. tty->flow_stopped = 1;
  1004. stop_tty(tty);
  1005. }
  1006. break;
  1007. case TCOON:
  1008. if (tty->flow_stopped) {
  1009. tty->flow_stopped = 0;
  1010. start_tty(tty);
  1011. }
  1012. break;
  1013. case TCIOFF:
  1014. if (STOP_CHAR(tty) != __DISABLED_CHAR)
  1015. return send_prio_char(tty, STOP_CHAR(tty));
  1016. break;
  1017. case TCION:
  1018. if (START_CHAR(tty) != __DISABLED_CHAR)
  1019. return send_prio_char(tty, START_CHAR(tty));
  1020. break;
  1021. default:
  1022. return -EINVAL;
  1023. }
  1024. return 0;
  1025. case TCFLSH:
  1026. return tty_perform_flush(tty, arg);
  1027. case TIOCPKT:
  1028. {
  1029. int pktmode;
  1030. if (tty->driver->type != TTY_DRIVER_TYPE_PTY ||
  1031. tty->driver->subtype != PTY_TYPE_MASTER)
  1032. return -ENOTTY;
  1033. if (get_user(pktmode, (int __user *) arg))
  1034. return -EFAULT;
  1035. spin_lock_irqsave(&tty->ctrl_lock, flags);
  1036. if (pktmode) {
  1037. if (!tty->packet) {
  1038. tty->packet = 1;
  1039. tty->link->ctrl_status = 0;
  1040. }
  1041. } else
  1042. tty->packet = 0;
  1043. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  1044. return 0;
  1045. }
  1046. default:
  1047. /* Try the mode commands */
  1048. return tty_mode_ioctl(tty, file, cmd, arg);
  1049. }
  1050. }
  1051. EXPORT_SYMBOL(n_tty_ioctl_helper);