tty_port.c 18 KB

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