tty_ioctl.c 31 KB

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