tty_ioctl.c 26 KB

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