tty_ioctl.c 30 KB

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