tty_ioctl.c 30 KB

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