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