tty_port.c 21 KB

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