tty_ioctl.c 29 KB

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