tty_ioctl.c 31 KB

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