tty_port.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /*
  2. * Tty port functions
  3. */
  4. #include <linux/types.h>
  5. #include <linux/errno.h>
  6. #include <linux/tty.h>
  7. #include <linux/tty_driver.h>
  8. #include <linux/tty_flip.h>
  9. #include <linux/serial.h>
  10. #include <linux/timer.h>
  11. #include <linux/string.h>
  12. #include <linux/slab.h>
  13. #include <linux/sched.h>
  14. #include <linux/wait.h>
  15. #include <linux/bitops.h>
  16. #include <linux/delay.h>
  17. #include <linux/module.h>
  18. void tty_port_init(struct tty_port *port)
  19. {
  20. memset(port, 0, sizeof(*port));
  21. tty_buffer_init(port);
  22. init_waitqueue_head(&port->open_wait);
  23. init_waitqueue_head(&port->close_wait);
  24. init_waitqueue_head(&port->delta_msr_wait);
  25. mutex_init(&port->mutex);
  26. mutex_init(&port->buf_mutex);
  27. spin_lock_init(&port->lock);
  28. port->close_delay = (50 * HZ) / 100;
  29. port->closing_wait = (3000 * HZ) / 100;
  30. kref_init(&port->kref);
  31. }
  32. EXPORT_SYMBOL(tty_port_init);
  33. /**
  34. * tty_port_link_device - link tty and tty_port
  35. * @port: tty_port of the device
  36. * @driver: tty_driver for this device
  37. * @index: index of the tty
  38. *
  39. * Provide the tty layer wit ha link from a tty (specified by @index) to a
  40. * tty_port (@port). Use this only if neither tty_port_register_device nor
  41. * tty_port_install is used in the driver. If used, this has to be called before
  42. * tty_register_driver.
  43. */
  44. void tty_port_link_device(struct tty_port *port,
  45. struct tty_driver *driver, unsigned index)
  46. {
  47. if (WARN_ON(index >= driver->num))
  48. return;
  49. driver->ports[index] = port;
  50. }
  51. EXPORT_SYMBOL_GPL(tty_port_link_device);
  52. /**
  53. * tty_port_register_device - register tty device
  54. * @port: tty_port of the device
  55. * @driver: tty_driver for this device
  56. * @index: index of the tty
  57. * @device: parent if exists, otherwise NULL
  58. *
  59. * It is the same as tty_register_device except the provided @port is linked to
  60. * a concrete tty specified by @index. Use this or tty_port_install (or both).
  61. * Call tty_port_link_device as a last resort.
  62. */
  63. struct device *tty_port_register_device(struct tty_port *port,
  64. struct tty_driver *driver, unsigned index,
  65. struct device *device)
  66. {
  67. tty_port_link_device(port, driver, index);
  68. return tty_register_device(driver, index, device);
  69. }
  70. EXPORT_SYMBOL_GPL(tty_port_register_device);
  71. /**
  72. * tty_port_register_device_attr - register tty device
  73. * @port: tty_port of the device
  74. * @driver: tty_driver for this device
  75. * @index: index of the tty
  76. * @device: parent if exists, otherwise NULL
  77. * @drvdata: Driver data to be set to device.
  78. * @attr_grp: Attribute group to be set on device.
  79. *
  80. * It is the same as tty_register_device_attr except the provided @port is
  81. * linked to a concrete tty specified by @index. Use this or tty_port_install
  82. * (or both). Call tty_port_link_device as a last resort.
  83. */
  84. struct device *tty_port_register_device_attr(struct tty_port *port,
  85. struct tty_driver *driver, unsigned index,
  86. struct device *device, void *drvdata,
  87. const struct attribute_group **attr_grp)
  88. {
  89. tty_port_link_device(port, driver, index);
  90. return tty_register_device_attr(driver, index, device, drvdata,
  91. attr_grp);
  92. }
  93. EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
  94. int tty_port_alloc_xmit_buf(struct tty_port *port)
  95. {
  96. /* We may sleep in get_zeroed_page() */
  97. mutex_lock(&port->buf_mutex);
  98. if (port->xmit_buf == NULL)
  99. port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
  100. mutex_unlock(&port->buf_mutex);
  101. if (port->xmit_buf == NULL)
  102. return -ENOMEM;
  103. return 0;
  104. }
  105. EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
  106. void tty_port_free_xmit_buf(struct tty_port *port)
  107. {
  108. mutex_lock(&port->buf_mutex);
  109. if (port->xmit_buf != NULL) {
  110. free_page((unsigned long)port->xmit_buf);
  111. port->xmit_buf = NULL;
  112. }
  113. mutex_unlock(&port->buf_mutex);
  114. }
  115. EXPORT_SYMBOL(tty_port_free_xmit_buf);
  116. /**
  117. * tty_port_destroy -- destroy inited port
  118. * @port: tty port to be doestroyed
  119. *
  120. * When a port was initialized using tty_port_init, one has to destroy the
  121. * port by this function. Either indirectly by using tty_port refcounting
  122. * (tty_port_put) or directly if refcounting is not used.
  123. */
  124. void tty_port_destroy(struct tty_port *port)
  125. {
  126. cancel_work_sync(&port->buf.work);
  127. tty_buffer_free_all(port);
  128. }
  129. EXPORT_SYMBOL(tty_port_destroy);
  130. static void tty_port_destructor(struct kref *kref)
  131. {
  132. struct tty_port *port = container_of(kref, struct tty_port, kref);
  133. /* check if last port ref was dropped before tty release */
  134. if (WARN_ON(port->itty))
  135. return;
  136. if (port->xmit_buf)
  137. free_page((unsigned long)port->xmit_buf);
  138. tty_port_destroy(port);
  139. if (port->ops && port->ops->destruct)
  140. port->ops->destruct(port);
  141. else
  142. kfree(port);
  143. }
  144. void tty_port_put(struct tty_port *port)
  145. {
  146. if (port)
  147. kref_put(&port->kref, tty_port_destructor);
  148. }
  149. EXPORT_SYMBOL(tty_port_put);
  150. /**
  151. * tty_port_tty_get - get a tty reference
  152. * @port: tty port
  153. *
  154. * Return a refcount protected tty instance or NULL if the port is not
  155. * associated with a tty (eg due to close or hangup)
  156. */
  157. struct tty_struct *tty_port_tty_get(struct tty_port *port)
  158. {
  159. unsigned long flags;
  160. struct tty_struct *tty;
  161. spin_lock_irqsave(&port->lock, flags);
  162. tty = tty_kref_get(port->tty);
  163. spin_unlock_irqrestore(&port->lock, flags);
  164. return tty;
  165. }
  166. EXPORT_SYMBOL(tty_port_tty_get);
  167. /**
  168. * tty_port_tty_set - set the tty of a port
  169. * @port: tty port
  170. * @tty: the tty
  171. *
  172. * Associate the port and tty pair. Manages any internal refcounts.
  173. * Pass NULL to deassociate a port
  174. */
  175. void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
  176. {
  177. unsigned long flags;
  178. spin_lock_irqsave(&port->lock, flags);
  179. tty_kref_put(port->tty);
  180. port->tty = tty_kref_get(tty);
  181. spin_unlock_irqrestore(&port->lock, flags);
  182. }
  183. EXPORT_SYMBOL(tty_port_tty_set);
  184. static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty)
  185. {
  186. mutex_lock(&port->mutex);
  187. if (port->console)
  188. goto out;
  189. if (test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) {
  190. /*
  191. * Drop DTR/RTS if HUPCL is set. This causes any attached
  192. * modem to hang up the line.
  193. */
  194. if (tty && C_HUPCL(tty))
  195. tty_port_lower_dtr_rts(port);
  196. if (port->ops->shutdown)
  197. port->ops->shutdown(port);
  198. }
  199. out:
  200. mutex_unlock(&port->mutex);
  201. }
  202. /**
  203. * tty_port_hangup - hangup helper
  204. * @port: tty port
  205. *
  206. * Perform port level tty hangup flag and count changes. Drop the tty
  207. * reference.
  208. *
  209. * Caller holds tty lock.
  210. */
  211. void tty_port_hangup(struct tty_port *port)
  212. {
  213. struct tty_struct *tty;
  214. unsigned long flags;
  215. spin_lock_irqsave(&port->lock, flags);
  216. port->count = 0;
  217. port->flags &= ~ASYNC_NORMAL_ACTIVE;
  218. tty = port->tty;
  219. if (tty)
  220. set_bit(TTY_IO_ERROR, &tty->flags);
  221. port->tty = NULL;
  222. spin_unlock_irqrestore(&port->lock, flags);
  223. tty_port_shutdown(port, tty);
  224. tty_kref_put(tty);
  225. wake_up_interruptible(&port->open_wait);
  226. wake_up_interruptible(&port->delta_msr_wait);
  227. }
  228. EXPORT_SYMBOL(tty_port_hangup);
  229. /**
  230. * tty_port_tty_hangup - helper to hang up a tty
  231. *
  232. * @port: tty port
  233. * @check_clocal: hang only ttys with CLOCAL unset?
  234. */
  235. void tty_port_tty_hangup(struct tty_port *port, bool check_clocal)
  236. {
  237. struct tty_struct *tty = tty_port_tty_get(port);
  238. if (tty && (!check_clocal || !C_CLOCAL(tty)))
  239. tty_hangup(tty);
  240. tty_kref_put(tty);
  241. }
  242. EXPORT_SYMBOL_GPL(tty_port_tty_hangup);
  243. /**
  244. * tty_port_tty_wakeup - helper to wake up a tty
  245. *
  246. * @port: tty port
  247. */
  248. void tty_port_tty_wakeup(struct tty_port *port)
  249. {
  250. struct tty_struct *tty = tty_port_tty_get(port);
  251. if (tty) {
  252. tty_wakeup(tty);
  253. tty_kref_put(tty);
  254. }
  255. }
  256. EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
  257. /**
  258. * tty_port_carrier_raised - carrier raised check
  259. * @port: tty port
  260. *
  261. * Wrapper for the carrier detect logic. For the moment this is used
  262. * to hide some internal details. This will eventually become entirely
  263. * internal to the tty port.
  264. */
  265. int tty_port_carrier_raised(struct tty_port *port)
  266. {
  267. if (port->ops->carrier_raised == NULL)
  268. return 1;
  269. return port->ops->carrier_raised(port);
  270. }
  271. EXPORT_SYMBOL(tty_port_carrier_raised);
  272. /**
  273. * tty_port_raise_dtr_rts - Raise DTR/RTS
  274. * @port: tty port
  275. *
  276. * Wrapper for the DTR/RTS raise logic. For the moment this is used
  277. * to hide some internal details. This will eventually become entirely
  278. * internal to the tty port.
  279. */
  280. void tty_port_raise_dtr_rts(struct tty_port *port)
  281. {
  282. if (port->ops->dtr_rts)
  283. port->ops->dtr_rts(port, 1);
  284. }
  285. EXPORT_SYMBOL(tty_port_raise_dtr_rts);
  286. /**
  287. * tty_port_lower_dtr_rts - Lower DTR/RTS
  288. * @port: tty port
  289. *
  290. * Wrapper for the DTR/RTS raise logic. For the moment this is used
  291. * to hide some internal details. This will eventually become entirely
  292. * internal to the tty port.
  293. */
  294. void tty_port_lower_dtr_rts(struct tty_port *port)
  295. {
  296. if (port->ops->dtr_rts)
  297. port->ops->dtr_rts(port, 0);
  298. }
  299. EXPORT_SYMBOL(tty_port_lower_dtr_rts);
  300. /**
  301. * tty_port_block_til_ready - Waiting logic for tty open
  302. * @port: the tty port being opened
  303. * @tty: the tty device being bound
  304. * @filp: the file pointer of the opener
  305. *
  306. * Implement the core POSIX/SuS tty behaviour when opening a tty device.
  307. * Handles:
  308. * - hangup (both before and during)
  309. * - non blocking open
  310. * - rts/dtr/dcd
  311. * - signals
  312. * - port flags and counts
  313. *
  314. * The passed tty_port must implement the carrier_raised method if it can
  315. * do carrier detect and the dtr_rts method if it supports software
  316. * management of these lines. Note that the dtr/rts raise is done each
  317. * iteration as a hangup may have previously dropped them while we wait.
  318. *
  319. * Caller holds tty lock.
  320. *
  321. * NB: May drop and reacquire tty lock when blocking, so tty and tty_port
  322. * may have changed state (eg., may have been hung up).
  323. */
  324. int tty_port_block_til_ready(struct tty_port *port,
  325. struct tty_struct *tty, struct file *filp)
  326. {
  327. int do_clocal = 0, retval;
  328. unsigned long flags;
  329. DEFINE_WAIT(wait);
  330. /* block if port is in the process of being closed */
  331. if (port->flags & ASYNC_CLOSING) {
  332. wait_event_interruptible_tty(tty, port->close_wait,
  333. !(port->flags & ASYNC_CLOSING));
  334. if (port->flags & ASYNC_HUP_NOTIFY)
  335. return -EAGAIN;
  336. else
  337. return -ERESTARTSYS;
  338. }
  339. /* if non-blocking mode is set we can pass directly to open unless
  340. the port has just hung up or is in another error state */
  341. if (tty->flags & (1 << TTY_IO_ERROR)) {
  342. port->flags |= ASYNC_NORMAL_ACTIVE;
  343. return 0;
  344. }
  345. if (filp->f_flags & O_NONBLOCK) {
  346. /* Indicate we are open */
  347. if (tty->termios.c_cflag & CBAUD)
  348. tty_port_raise_dtr_rts(port);
  349. port->flags |= ASYNC_NORMAL_ACTIVE;
  350. return 0;
  351. }
  352. if (C_CLOCAL(tty))
  353. do_clocal = 1;
  354. /* Block waiting until we can proceed. We may need to wait for the
  355. carrier, but we must also wait for any close that is in progress
  356. before the next open may complete */
  357. retval = 0;
  358. /* The port lock protects the port counts */
  359. spin_lock_irqsave(&port->lock, flags);
  360. port->count--;
  361. port->blocked_open++;
  362. spin_unlock_irqrestore(&port->lock, flags);
  363. while (1) {
  364. /* Indicate we are open */
  365. if (C_BAUD(tty) && test_bit(ASYNCB_INITIALIZED, &port->flags))
  366. tty_port_raise_dtr_rts(port);
  367. prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
  368. /* Check for a hangup or uninitialised port.
  369. Return accordingly */
  370. if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
  371. if (port->flags & ASYNC_HUP_NOTIFY)
  372. retval = -EAGAIN;
  373. else
  374. retval = -ERESTARTSYS;
  375. break;
  376. }
  377. /*
  378. * Probe the carrier. For devices with no carrier detect
  379. * tty_port_carrier_raised will always return true.
  380. * Never ask drivers if CLOCAL is set, this causes troubles
  381. * on some hardware.
  382. */
  383. if (!(port->flags & ASYNC_CLOSING) &&
  384. (do_clocal || tty_port_carrier_raised(port)))
  385. break;
  386. if (signal_pending(current)) {
  387. retval = -ERESTARTSYS;
  388. break;
  389. }
  390. tty_unlock(tty);
  391. schedule();
  392. tty_lock(tty);
  393. }
  394. finish_wait(&port->open_wait, &wait);
  395. /* Update counts. A parallel hangup will have set count to zero and
  396. we must not mess that up further */
  397. spin_lock_irqsave(&port->lock, flags);
  398. if (!tty_hung_up_p(filp))
  399. port->count++;
  400. port->blocked_open--;
  401. if (retval == 0)
  402. port->flags |= ASYNC_NORMAL_ACTIVE;
  403. spin_unlock_irqrestore(&port->lock, flags);
  404. return retval;
  405. }
  406. EXPORT_SYMBOL(tty_port_block_til_ready);
  407. static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty)
  408. {
  409. unsigned int bps = tty_get_baud_rate(tty);
  410. long timeout;
  411. if (bps > 1200) {
  412. timeout = (HZ * 10 * port->drain_delay) / bps;
  413. timeout = max_t(long, timeout, HZ / 10);
  414. } else {
  415. timeout = 2 * HZ;
  416. }
  417. schedule_timeout_interruptible(timeout);
  418. }
  419. /* Caller holds tty lock.
  420. * NB: may drop and reacquire tty lock (in tty_wait_until_sent_from_close())
  421. * so tty and tty port may have changed state (but not hung up or reopened).
  422. */
  423. int tty_port_close_start(struct tty_port *port,
  424. struct tty_struct *tty, struct file *filp)
  425. {
  426. unsigned long flags;
  427. if (tty_hung_up_p(filp))
  428. return 0;
  429. spin_lock_irqsave(&port->lock, flags);
  430. if (tty->count == 1 && port->count != 1) {
  431. printk(KERN_WARNING
  432. "tty_port_close_start: tty->count = 1 port count = %d.\n",
  433. port->count);
  434. port->count = 1;
  435. }
  436. if (--port->count < 0) {
  437. printk(KERN_WARNING "tty_port_close_start: count = %d\n",
  438. port->count);
  439. port->count = 0;
  440. }
  441. if (port->count) {
  442. spin_unlock_irqrestore(&port->lock, flags);
  443. return 0;
  444. }
  445. set_bit(ASYNCB_CLOSING, &port->flags);
  446. spin_unlock_irqrestore(&port->lock, flags);
  447. tty->closing = 1;
  448. if (test_bit(ASYNCB_INITIALIZED, &port->flags)) {
  449. /* Don't block on a stalled port, just pull the chain */
  450. if (tty->flow_stopped)
  451. tty_driver_flush_buffer(tty);
  452. if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  453. tty_wait_until_sent_from_close(tty, port->closing_wait);
  454. if (port->drain_delay)
  455. tty_port_drain_delay(port, tty);
  456. }
  457. /* Flush the ldisc buffering */
  458. tty_ldisc_flush(tty);
  459. /* Report to caller this is the last port reference */
  460. return 1;
  461. }
  462. EXPORT_SYMBOL(tty_port_close_start);
  463. /* Caller holds tty lock */
  464. void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
  465. {
  466. unsigned long flags;
  467. tty_ldisc_flush(tty);
  468. tty->closing = 0;
  469. spin_lock_irqsave(&port->lock, flags);
  470. if (port->blocked_open) {
  471. spin_unlock_irqrestore(&port->lock, flags);
  472. if (port->close_delay) {
  473. msleep_interruptible(
  474. jiffies_to_msecs(port->close_delay));
  475. }
  476. spin_lock_irqsave(&port->lock, flags);
  477. wake_up_interruptible(&port->open_wait);
  478. }
  479. port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
  480. wake_up_interruptible(&port->close_wait);
  481. spin_unlock_irqrestore(&port->lock, flags);
  482. }
  483. EXPORT_SYMBOL(tty_port_close_end);
  484. /**
  485. * tty_port_close
  486. *
  487. * Caller holds tty lock
  488. *
  489. * NB: may drop and reacquire tty lock (in tty_port_close_start()->
  490. * tty_wait_until_sent_from_close()) so tty and tty_port may have changed
  491. * state (but not hung up or reopened).
  492. */
  493. void tty_port_close(struct tty_port *port, struct tty_struct *tty,
  494. struct file *filp)
  495. {
  496. if (tty_port_close_start(port, tty, filp) == 0)
  497. return;
  498. tty_port_shutdown(port, tty);
  499. set_bit(TTY_IO_ERROR, &tty->flags);
  500. tty_port_close_end(port, tty);
  501. tty_port_tty_set(port, NULL);
  502. }
  503. EXPORT_SYMBOL(tty_port_close);
  504. /**
  505. * tty_port_install - generic tty->ops->install handler
  506. * @port: tty_port of the device
  507. * @driver: tty_driver for this device
  508. * @tty: tty to be installed
  509. *
  510. * It is the same as tty_standard_install except the provided @port is linked
  511. * to a concrete tty specified by @tty. Use this or tty_port_register_device
  512. * (or both). Call tty_port_link_device as a last resort.
  513. */
  514. int tty_port_install(struct tty_port *port, struct tty_driver *driver,
  515. struct tty_struct *tty)
  516. {
  517. tty->port = port;
  518. return tty_standard_install(driver, tty);
  519. }
  520. EXPORT_SYMBOL_GPL(tty_port_install);
  521. /**
  522. * tty_port_open
  523. *
  524. * Caller holds tty lock.
  525. *
  526. * NB: may drop and reacquire tty lock (in tty_port_block_til_ready()) so
  527. * tty and tty_port may have changed state (eg., may be hung up now)
  528. */
  529. int tty_port_open(struct tty_port *port, struct tty_struct *tty,
  530. struct file *filp)
  531. {
  532. spin_lock_irq(&port->lock);
  533. ++port->count;
  534. spin_unlock_irq(&port->lock);
  535. tty_port_tty_set(port, tty);
  536. /*
  537. * Do the device-specific open only if the hardware isn't
  538. * already initialized. Serialize open and shutdown using the
  539. * port mutex.
  540. */
  541. mutex_lock(&port->mutex);
  542. if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) {
  543. clear_bit(TTY_IO_ERROR, &tty->flags);
  544. if (port->ops->activate) {
  545. int retval = port->ops->activate(port, tty);
  546. if (retval) {
  547. mutex_unlock(&port->mutex);
  548. return retval;
  549. }
  550. }
  551. set_bit(ASYNCB_INITIALIZED, &port->flags);
  552. }
  553. mutex_unlock(&port->mutex);
  554. return tty_port_block_til_ready(port, tty, filp);
  555. }
  556. EXPORT_SYMBOL(tty_port_open);