tty_port.c 18 KB

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