tty_ioctl.c 30 KB

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