tty_port.c 16 KB

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