tty_port.c 15 KB

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