tty_port.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Tty port functions
  4. */
  5. #include <linux/types.h>
  6. #include <linux/errno.h>
  7. #include <linux/tty.h>
  8. #include <linux/tty_driver.h>
  9. #include <linux/tty_flip.h>
  10. #include <linux/serial.h>
  11. #include <linux/timer.h>
  12. #include <linux/string.h>
  13. #include <linux/slab.h>
  14. #include <linux/sched/signal.h>
  15. #include <linux/wait.h>
  16. #include <linux/bitops.h>
  17. #include <linux/delay.h>
  18. #include <linux/module.h>
  19. #include <linux/serdev.h>
  20. #include "tty.h"
  21. static size_t tty_port_default_receive_buf(struct tty_port *port, const u8 *p,
  22. const u8 *f, size_t count)
  23. {
  24. struct tty_struct *tty;
  25. struct tty_ldisc *ld;
  26. tty = READ_ONCE(port->itty);
  27. if (!tty)
  28. return 0;
  29. ld = tty_ldisc_ref(tty);
  30. if (!ld)
  31. return 0;
  32. count = tty_ldisc_receive_buf(ld, p, f, count);
  33. tty_ldisc_deref(ld);
  34. return count;
  35. }
  36. static void tty_port_default_lookahead_buf(struct tty_port *port, const u8 *p,
  37. const u8 *f, size_t count)
  38. {
  39. struct tty_struct *tty;
  40. struct tty_ldisc *ld;
  41. tty = READ_ONCE(port->itty);
  42. if (!tty)
  43. return;
  44. ld = tty_ldisc_ref(tty);
  45. if (!ld)
  46. return;
  47. if (ld->ops->lookahead_buf)
  48. ld->ops->lookahead_buf(ld->tty, p, f, count);
  49. tty_ldisc_deref(ld);
  50. }
  51. static void tty_port_default_wakeup(struct tty_port *port)
  52. {
  53. scoped_guard(tty_port_tty, port)
  54. tty_wakeup(scoped_tty());
  55. }
  56. const struct tty_port_client_operations tty_port_default_client_ops = {
  57. .receive_buf = tty_port_default_receive_buf,
  58. .lookahead_buf = tty_port_default_lookahead_buf,
  59. .write_wakeup = tty_port_default_wakeup,
  60. };
  61. EXPORT_SYMBOL_GPL(tty_port_default_client_ops);
  62. /**
  63. * tty_port_init - initialize tty_port
  64. * @port: tty_port to initialize
  65. *
  66. * Initializes the state of struct tty_port. When a port was initialized using
  67. * this function, one has to destroy the port by tty_port_destroy(). Either
  68. * indirectly by using &tty_port refcounting (tty_port_put()) or directly if
  69. * refcounting is not used.
  70. */
  71. void tty_port_init(struct tty_port *port)
  72. {
  73. memset(port, 0, sizeof(*port));
  74. tty_buffer_init(port);
  75. init_waitqueue_head(&port->open_wait);
  76. init_waitqueue_head(&port->delta_msr_wait);
  77. mutex_init(&port->mutex);
  78. mutex_init(&port->buf_mutex);
  79. spin_lock_init(&port->lock);
  80. port->close_delay = (50 * HZ) / 100;
  81. port->closing_wait = (3000 * HZ) / 100;
  82. port->client_ops = &tty_port_default_client_ops;
  83. kref_init(&port->kref);
  84. }
  85. EXPORT_SYMBOL(tty_port_init);
  86. /**
  87. * tty_port_link_device - link tty and tty_port
  88. * @port: tty_port of the device
  89. * @driver: tty_driver for this device
  90. * @index: index of the tty
  91. *
  92. * Provide the tty layer with a link from a tty (specified by @index) to a
  93. * tty_port (@port). Use this only if neither tty_port_register_device() nor
  94. * tty_port_install() is used in the driver. If used, this has to be called
  95. * before tty_register_driver().
  96. */
  97. void tty_port_link_device(struct tty_port *port,
  98. struct tty_driver *driver, unsigned index)
  99. {
  100. if (WARN_ON(index >= driver->num))
  101. return;
  102. driver->ports[index] = port;
  103. }
  104. EXPORT_SYMBOL_GPL(tty_port_link_device);
  105. /**
  106. * tty_port_register_device - register tty device
  107. * @port: tty_port of the device
  108. * @driver: tty_driver for this device
  109. * @index: index of the tty
  110. * @device: parent if exists, otherwise NULL
  111. *
  112. * It is the same as tty_register_device() except the provided @port is linked
  113. * to a concrete tty specified by @index. Use this or tty_port_install() (or
  114. * both). Call tty_port_link_device() as a last resort.
  115. */
  116. struct device *tty_port_register_device(struct tty_port *port,
  117. struct tty_driver *driver, unsigned index,
  118. struct device *device)
  119. {
  120. return tty_port_register_device_attr(port, driver, index, device, NULL, NULL);
  121. }
  122. EXPORT_SYMBOL_GPL(tty_port_register_device);
  123. /**
  124. * tty_port_register_device_attr - register tty device
  125. * @port: tty_port of the device
  126. * @driver: tty_driver for this device
  127. * @index: index of the tty
  128. * @device: parent if exists, otherwise NULL
  129. * @drvdata: Driver data to be set to device.
  130. * @attr_grp: Attribute group to be set on device.
  131. *
  132. * It is the same as tty_register_device_attr() except the provided @port is
  133. * linked to a concrete tty specified by @index. Use this or tty_port_install()
  134. * (or both). Call tty_port_link_device() as a last resort.
  135. */
  136. struct device *tty_port_register_device_attr(struct tty_port *port,
  137. struct tty_driver *driver, unsigned index,
  138. struct device *device, void *drvdata,
  139. const struct attribute_group **attr_grp)
  140. {
  141. tty_port_link_device(port, driver, index);
  142. return tty_register_device_attr(driver, index, device, drvdata,
  143. attr_grp);
  144. }
  145. EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
  146. /**
  147. * tty_port_register_device_attr_serdev - register tty or serdev device
  148. * @port: tty_port of the device
  149. * @driver: tty_driver for this device
  150. * @index: index of the tty
  151. * @host: serial port hardware device
  152. * @parent: parent if exists, otherwise NULL
  153. * @drvdata: driver data for the device
  154. * @attr_grp: attribute group for the device
  155. *
  156. * Register a serdev or tty device depending on if the parent device has any
  157. * defined serdev clients or not.
  158. */
  159. struct device *tty_port_register_device_attr_serdev(struct tty_port *port,
  160. struct tty_driver *driver, unsigned index,
  161. struct device *host, struct device *parent, void *drvdata,
  162. const struct attribute_group **attr_grp)
  163. {
  164. struct device *dev;
  165. tty_port_link_device(port, driver, index);
  166. dev = serdev_tty_port_register(port, host, parent, driver, index);
  167. if (PTR_ERR(dev) != -ENODEV) {
  168. /* Skip creating cdev if we registered a serdev device */
  169. return dev;
  170. }
  171. return tty_register_device_attr(driver, index, parent, drvdata,
  172. attr_grp);
  173. }
  174. EXPORT_SYMBOL_GPL(tty_port_register_device_attr_serdev);
  175. /**
  176. * tty_port_unregister_device - deregister a tty or serdev device
  177. * @port: tty_port of the device
  178. * @driver: tty_driver for this device
  179. * @index: index of the tty
  180. *
  181. * If a tty or serdev device is registered with a call to
  182. * tty_port_register_device_serdev() then this function must be called when
  183. * the device is gone.
  184. */
  185. void tty_port_unregister_device(struct tty_port *port,
  186. struct tty_driver *driver, unsigned index)
  187. {
  188. int ret;
  189. ret = serdev_tty_port_unregister(port);
  190. if (ret == 0)
  191. return;
  192. tty_unregister_device(driver, index);
  193. }
  194. EXPORT_SYMBOL_GPL(tty_port_unregister_device);
  195. int tty_port_alloc_xmit_buf(struct tty_port *port)
  196. {
  197. /* We may sleep in get_zeroed_page() */
  198. guard(mutex)(&port->buf_mutex);
  199. if (port->xmit_buf)
  200. return 0;
  201. port->xmit_buf = (u8 *)get_zeroed_page(GFP_KERNEL);
  202. if (port->xmit_buf == NULL)
  203. return -ENOMEM;
  204. kfifo_init(&port->xmit_fifo, port->xmit_buf, PAGE_SIZE);
  205. return 0;
  206. }
  207. EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
  208. void tty_port_free_xmit_buf(struct tty_port *port)
  209. {
  210. guard(mutex)(&port->buf_mutex);
  211. free_page((unsigned long)port->xmit_buf);
  212. port->xmit_buf = NULL;
  213. INIT_KFIFO(port->xmit_fifo);
  214. }
  215. EXPORT_SYMBOL(tty_port_free_xmit_buf);
  216. /**
  217. * tty_port_destroy - destroy inited port
  218. * @port: tty port to be destroyed
  219. *
  220. * When a port was initialized using tty_port_init(), one has to destroy the
  221. * port by this function. Either indirectly by using &tty_port refcounting
  222. * (tty_port_put()) or directly if refcounting is not used.
  223. */
  224. void tty_port_destroy(struct tty_port *port)
  225. {
  226. tty_buffer_cancel_work(port);
  227. tty_buffer_free_all(port);
  228. }
  229. EXPORT_SYMBOL(tty_port_destroy);
  230. static void tty_port_destructor(struct kref *kref)
  231. {
  232. struct tty_port *port = container_of(kref, struct tty_port, kref);
  233. /* check if last port ref was dropped before tty release */
  234. if (WARN_ON(port->itty))
  235. return;
  236. free_page((unsigned long)port->xmit_buf);
  237. tty_port_destroy(port);
  238. if (port->ops && port->ops->destruct)
  239. port->ops->destruct(port);
  240. else
  241. kfree(port);
  242. }
  243. /**
  244. * tty_port_put - drop a reference to tty_port
  245. * @port: port to drop a reference of (can be NULL)
  246. *
  247. * The final put will destroy and free up the @port using
  248. * @port->ops->destruct() hook, or using kfree() if not provided.
  249. */
  250. void tty_port_put(struct tty_port *port)
  251. {
  252. if (port)
  253. kref_put(&port->kref, tty_port_destructor);
  254. }
  255. EXPORT_SYMBOL(tty_port_put);
  256. /**
  257. * tty_port_tty_get - get a tty reference
  258. * @port: tty port
  259. *
  260. * Return a refcount protected tty instance or %NULL if the port is not
  261. * associated with a tty (eg due to close or hangup).
  262. */
  263. struct tty_struct *tty_port_tty_get(struct tty_port *port)
  264. {
  265. guard(spinlock_irqsave)(&port->lock);
  266. return tty_kref_get(port->tty);
  267. }
  268. EXPORT_SYMBOL(tty_port_tty_get);
  269. /**
  270. * tty_port_tty_set - set the tty of a port
  271. * @port: tty port
  272. * @tty: the tty
  273. *
  274. * Associate the port and tty pair. Manages any internal refcounts. Pass %NULL
  275. * to deassociate a port.
  276. */
  277. void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
  278. {
  279. guard(spinlock_irqsave)(&port->lock);
  280. tty_kref_put(port->tty);
  281. port->tty = tty_kref_get(tty);
  282. }
  283. EXPORT_SYMBOL(tty_port_tty_set);
  284. /**
  285. * tty_port_shutdown - internal helper to shutdown the device
  286. * @port: tty port to be shut down
  287. * @tty: the associated tty
  288. *
  289. * It is used by tty_port_hangup() and tty_port_close(). Its task is to
  290. * shutdown the device if it was initialized (note consoles remain
  291. * functioning). It lowers DTR/RTS (if @tty has HUPCL set) and invokes
  292. * @port->ops->shutdown().
  293. */
  294. static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty)
  295. {
  296. guard(mutex)(&port->mutex);
  297. if (port->console)
  298. return;
  299. if (!tty_port_initialized(port))
  300. return;
  301. tty_port_set_initialized(port, false);
  302. /*
  303. * Drop DTR/RTS if HUPCL is set. This causes any attached
  304. * modem to hang up the line.
  305. */
  306. if (tty && C_HUPCL(tty))
  307. tty_port_lower_dtr_rts(port);
  308. if (port->ops->shutdown)
  309. port->ops->shutdown(port);
  310. }
  311. /**
  312. * tty_port_hangup - hangup helper
  313. * @port: tty port
  314. *
  315. * Perform port level tty hangup flag and count changes. Drop the tty
  316. * reference.
  317. *
  318. * Caller holds tty lock.
  319. */
  320. void tty_port_hangup(struct tty_port *port)
  321. {
  322. struct tty_struct *tty;
  323. scoped_guard(spinlock_irqsave, &port->lock) {
  324. port->count = 0;
  325. tty = port->tty;
  326. if (tty)
  327. set_bit(TTY_IO_ERROR, &tty->flags);
  328. port->tty = NULL;
  329. }
  330. tty_port_set_active(port, false);
  331. tty_port_shutdown(port, tty);
  332. tty_kref_put(tty);
  333. wake_up_interruptible(&port->open_wait);
  334. wake_up_interruptible(&port->delta_msr_wait);
  335. }
  336. EXPORT_SYMBOL(tty_port_hangup);
  337. void __tty_port_tty_hangup(struct tty_port *port, bool check_clocal, bool async)
  338. {
  339. scoped_guard(tty_port_tty, port) {
  340. struct tty_struct *tty = scoped_tty();
  341. if (!check_clocal || !C_CLOCAL(tty)) {
  342. if (async)
  343. tty_hangup(tty);
  344. else
  345. tty_vhangup(tty);
  346. }
  347. }
  348. }
  349. EXPORT_SYMBOL_GPL(__tty_port_tty_hangup);
  350. /**
  351. * tty_port_tty_wakeup - helper to wake up a tty
  352. * @port: tty port
  353. */
  354. void tty_port_tty_wakeup(struct tty_port *port)
  355. {
  356. port->client_ops->write_wakeup(port);
  357. }
  358. EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
  359. /**
  360. * tty_port_carrier_raised - carrier raised check
  361. * @port: tty port
  362. *
  363. * Wrapper for the carrier detect logic. For the moment this is used
  364. * to hide some internal details. This will eventually become entirely
  365. * internal to the tty port.
  366. */
  367. bool tty_port_carrier_raised(struct tty_port *port)
  368. {
  369. if (port->ops->carrier_raised == NULL)
  370. return true;
  371. return port->ops->carrier_raised(port);
  372. }
  373. EXPORT_SYMBOL(tty_port_carrier_raised);
  374. /**
  375. * tty_port_raise_dtr_rts - Raise DTR/RTS
  376. * @port: tty port
  377. *
  378. * Wrapper for the DTR/RTS raise logic. For the moment this is used to hide
  379. * some internal details. This will eventually become entirely internal to the
  380. * tty port.
  381. */
  382. void tty_port_raise_dtr_rts(struct tty_port *port)
  383. {
  384. if (port->ops->dtr_rts)
  385. port->ops->dtr_rts(port, true);
  386. }
  387. EXPORT_SYMBOL(tty_port_raise_dtr_rts);
  388. /**
  389. * tty_port_lower_dtr_rts - Lower DTR/RTS
  390. * @port: tty port
  391. *
  392. * Wrapper for the DTR/RTS raise logic. For the moment this is used to hide
  393. * some internal details. This will eventually become entirely internal to the
  394. * tty port.
  395. */
  396. void tty_port_lower_dtr_rts(struct tty_port *port)
  397. {
  398. if (port->ops->dtr_rts)
  399. port->ops->dtr_rts(port, false);
  400. }
  401. EXPORT_SYMBOL(tty_port_lower_dtr_rts);
  402. /**
  403. * tty_port_block_til_ready - Waiting logic for tty open
  404. * @port: the tty port being opened
  405. * @tty: the tty device being bound
  406. * @filp: the file pointer of the opener or %NULL
  407. *
  408. * Implement the core POSIX/SuS tty behaviour when opening a tty device.
  409. * Handles:
  410. *
  411. * - hangup (both before and during)
  412. * - non blocking open
  413. * - rts/dtr/dcd
  414. * - signals
  415. * - port flags and counts
  416. *
  417. * The passed @port must implement the @port->ops->carrier_raised method if it
  418. * can do carrier detect and the @port->ops->dtr_rts method if it supports
  419. * software management of these lines. Note that the dtr/rts raise is done each
  420. * iteration as a hangup may have previously dropped them while we wait.
  421. *
  422. * Caller holds tty lock.
  423. *
  424. * Note: May drop and reacquire tty lock when blocking, so @tty and @port may
  425. * have changed state (eg., may have been hung up).
  426. */
  427. int tty_port_block_til_ready(struct tty_port *port,
  428. struct tty_struct *tty, struct file *filp)
  429. {
  430. int do_clocal = 0, retval;
  431. DEFINE_WAIT(wait);
  432. /* if non-blocking mode is set we can pass directly to open unless
  433. * the port has just hung up or is in another error state.
  434. */
  435. if (tty_io_error(tty)) {
  436. tty_port_set_active(port, true);
  437. return 0;
  438. }
  439. if (filp == NULL || (filp->f_flags & O_NONBLOCK)) {
  440. /* Indicate we are open */
  441. if (C_BAUD(tty))
  442. tty_port_raise_dtr_rts(port);
  443. tty_port_set_active(port, true);
  444. return 0;
  445. }
  446. if (C_CLOCAL(tty))
  447. do_clocal = 1;
  448. /* Block waiting until we can proceed. We may need to wait for the
  449. * carrier, but we must also wait for any close that is in progress
  450. * before the next open may complete.
  451. */
  452. retval = 0;
  453. /* The port lock protects the port counts */
  454. scoped_guard(spinlock_irqsave, &port->lock) {
  455. port->count--;
  456. port->blocked_open++;
  457. }
  458. while (1) {
  459. /* Indicate we are open */
  460. if (C_BAUD(tty) && tty_port_initialized(port))
  461. tty_port_raise_dtr_rts(port);
  462. prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
  463. /* Check for a hangup or uninitialised port.
  464. * Return accordingly.
  465. */
  466. if (tty_hung_up_p(filp) || !tty_port_initialized(port)) {
  467. if (port->flags & ASYNC_HUP_NOTIFY)
  468. retval = -EAGAIN;
  469. else
  470. retval = -ERESTARTSYS;
  471. break;
  472. }
  473. /*
  474. * Probe the carrier. For devices with no carrier detect
  475. * tty_port_carrier_raised will always return true.
  476. * Never ask drivers if CLOCAL is set, this causes troubles
  477. * on some hardware.
  478. */
  479. if (do_clocal || tty_port_carrier_raised(port))
  480. break;
  481. if (signal_pending(current)) {
  482. retval = -ERESTARTSYS;
  483. break;
  484. }
  485. tty_unlock(tty);
  486. schedule();
  487. tty_lock(tty);
  488. }
  489. finish_wait(&port->open_wait, &wait);
  490. /* Update counts. A parallel hangup will have set count to zero and
  491. * we must not mess that up further.
  492. */
  493. scoped_guard(spinlock_irqsave, &port->lock) {
  494. if (!tty_hung_up_p(filp))
  495. port->count++;
  496. port->blocked_open--;
  497. }
  498. if (retval == 0)
  499. tty_port_set_active(port, true);
  500. return retval;
  501. }
  502. EXPORT_SYMBOL(tty_port_block_til_ready);
  503. static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty)
  504. {
  505. unsigned int bps = tty_get_baud_rate(tty);
  506. long timeout;
  507. if (bps > 1200) {
  508. timeout = (HZ * 10 * port->drain_delay) / bps;
  509. timeout = max_t(long, timeout, HZ / 10);
  510. } else {
  511. timeout = 2 * HZ;
  512. }
  513. schedule_timeout_interruptible(timeout);
  514. }
  515. /**
  516. * tty_port_close_start - helper for tty->ops->close, part 1/2
  517. * @port: tty_port of the device
  518. * @tty: tty being closed
  519. * @filp: passed file pointer
  520. *
  521. * Decrements and checks open count. Flushes the port if this is the last
  522. * close. That means, dropping the data from the outpu buffer on the device and
  523. * waiting for sending logic to finish. The rest of close handling is performed
  524. * in tty_port_close_end().
  525. *
  526. * Locking: Caller holds tty lock.
  527. *
  528. * Return: 1 if this is the last close, otherwise 0
  529. */
  530. int tty_port_close_start(struct tty_port *port,
  531. struct tty_struct *tty, struct file *filp)
  532. {
  533. if (tty_hung_up_p(filp))
  534. return 0;
  535. scoped_guard(spinlock_irqsave, &port->lock) {
  536. if (tty->count == 1 && port->count != 1) {
  537. tty_warn(tty, "%s: tty->count = 1 port count = %d\n", __func__,
  538. port->count);
  539. port->count = 1;
  540. }
  541. if (--port->count < 0) {
  542. tty_warn(tty, "%s: bad port count (%d)\n", __func__,
  543. port->count);
  544. port->count = 0;
  545. }
  546. if (port->count)
  547. return 0;
  548. }
  549. tty->closing = 1;
  550. if (tty_port_initialized(port)) {
  551. /* Don't block on a stalled port, just pull the chain */
  552. if (tty->flow.tco_stopped)
  553. tty_driver_flush_buffer(tty);
  554. if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  555. tty_wait_until_sent(tty, port->closing_wait);
  556. if (port->drain_delay)
  557. tty_port_drain_delay(port, tty);
  558. }
  559. /* Flush the ldisc buffering */
  560. tty_ldisc_flush(tty);
  561. /* Report to caller this is the last port reference */
  562. return 1;
  563. }
  564. EXPORT_SYMBOL(tty_port_close_start);
  565. /**
  566. * tty_port_close_end - helper for tty->ops->close, part 2/2
  567. * @port: tty_port of the device
  568. * @tty: tty being closed
  569. *
  570. * This is a continuation of the first part: tty_port_close_start(). This
  571. * should be called after turning off the device. It flushes the data from the
  572. * line discipline and delays the close by @port->close_delay.
  573. *
  574. * Locking: Caller holds tty lock.
  575. */
  576. void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
  577. {
  578. unsigned long flags;
  579. tty_ldisc_flush(tty);
  580. tty->closing = 0;
  581. spin_lock_irqsave(&port->lock, flags);
  582. if (port->blocked_open) {
  583. spin_unlock_irqrestore(&port->lock, flags);
  584. if (port->close_delay)
  585. msleep_interruptible(jiffies_to_msecs(port->close_delay));
  586. spin_lock_irqsave(&port->lock, flags);
  587. wake_up_interruptible(&port->open_wait);
  588. }
  589. spin_unlock_irqrestore(&port->lock, flags);
  590. tty_port_set_active(port, false);
  591. }
  592. EXPORT_SYMBOL(tty_port_close_end);
  593. /**
  594. * tty_port_close - generic tty->ops->close handler
  595. * @port: tty_port of the device
  596. * @tty: tty being closed
  597. * @filp: passed file pointer
  598. *
  599. * It is a generic helper to be used in driver's @tty->ops->close. It wraps a
  600. * sequence of tty_port_close_start(), tty_port_shutdown(), and
  601. * tty_port_close_end(). The latter two are called only if this is the last
  602. * close. See the respective functions for the details.
  603. *
  604. * Locking: Caller holds tty lock
  605. */
  606. void tty_port_close(struct tty_port *port, struct tty_struct *tty,
  607. struct file *filp)
  608. {
  609. if (tty_port_close_start(port, tty, filp) == 0)
  610. return;
  611. tty_port_shutdown(port, tty);
  612. if (!port->console)
  613. set_bit(TTY_IO_ERROR, &tty->flags);
  614. tty_port_close_end(port, tty);
  615. tty_port_tty_set(port, NULL);
  616. }
  617. EXPORT_SYMBOL(tty_port_close);
  618. /**
  619. * tty_port_install - generic tty->ops->install handler
  620. * @port: tty_port of the device
  621. * @driver: tty_driver for this device
  622. * @tty: tty to be installed
  623. *
  624. * It is the same as tty_standard_install() except the provided @port is linked
  625. * to a concrete tty specified by @tty. Use this or tty_port_register_device()
  626. * (or both). Call tty_port_link_device() as a last resort.
  627. */
  628. int tty_port_install(struct tty_port *port, struct tty_driver *driver,
  629. struct tty_struct *tty)
  630. {
  631. tty->port = port;
  632. return tty_standard_install(driver, tty);
  633. }
  634. EXPORT_SYMBOL_GPL(tty_port_install);
  635. /**
  636. * tty_port_open - generic tty->ops->open handler
  637. * @port: tty_port of the device
  638. * @tty: tty to be opened
  639. * @filp: passed file pointer
  640. *
  641. * It is a generic helper to be used in driver's @tty->ops->open. It activates
  642. * the devices using @port->ops->activate if not active already. And waits for
  643. * the device to be ready using tty_port_block_til_ready() (e.g. raises
  644. * DTR/CTS and waits for carrier).
  645. *
  646. * Note that @port->ops->shutdown is not called when @port->ops->activate
  647. * returns an error (on the contrary, @tty->ops->close is).
  648. *
  649. * Locking: Caller holds tty lock.
  650. *
  651. * Note: may drop and reacquire tty lock (in tty_port_block_til_ready()) so
  652. * @tty and @port may have changed state (eg., may be hung up now).
  653. */
  654. int tty_port_open(struct tty_port *port, struct tty_struct *tty,
  655. struct file *filp)
  656. {
  657. scoped_guard(spinlock_irq, &port->lock)
  658. ++port->count;
  659. tty_port_tty_set(port, tty);
  660. /*
  661. * Do the device-specific open only if the hardware isn't
  662. * already initialized. Serialize open and shutdown using the
  663. * port mutex.
  664. */
  665. scoped_guard(mutex, &port->mutex) {
  666. if (tty_port_initialized(port))
  667. break;
  668. clear_bit(TTY_IO_ERROR, &tty->flags);
  669. if (port->ops->activate) {
  670. int retval = port->ops->activate(port, tty);
  671. if (retval)
  672. return retval;
  673. }
  674. tty_port_set_initialized(port, true);
  675. }
  676. return tty_port_block_til_ready(port, tty, filp);
  677. }
  678. EXPORT_SYMBOL(tty_port_open);