tty_port.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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/init.h>
  15. #include <linux/wait.h>
  16. #include <linux/bitops.h>
  17. #include <linux/delay.h>
  18. #include <linux/module.h>
  19. void tty_port_init(struct tty_port *port)
  20. {
  21. memset(port, 0, sizeof(*port));
  22. tty_buffer_init(port);
  23. init_waitqueue_head(&port->open_wait);
  24. init_waitqueue_head(&port->close_wait);
  25. init_waitqueue_head(&port->delta_msr_wait);
  26. mutex_init(&port->mutex);
  27. mutex_init(&port->buf_mutex);
  28. spin_lock_init(&port->lock);
  29. port->close_delay = (50 * HZ) / 100;
  30. port->closing_wait = (3000 * HZ) / 100;
  31. kref_init(&port->kref);
  32. }
  33. EXPORT_SYMBOL(tty_port_init);
  34. /**
  35. * tty_port_link_device - link tty and tty_port
  36. * @port: tty_port of the device
  37. * @driver: tty_driver for this device
  38. * @index: index of the tty
  39. *
  40. * Provide the tty layer wit ha link from a tty (specified by @index) to a
  41. * tty_port (@port). Use this only if neither tty_port_register_device nor
  42. * tty_port_install is used in the driver. If used, this has to be called before
  43. * tty_register_driver.
  44. */
  45. void tty_port_link_device(struct tty_port *port,
  46. struct tty_driver *driver, unsigned index)
  47. {
  48. if (WARN_ON(index >= driver->num))
  49. return;
  50. driver->ports[index] = port;
  51. }
  52. EXPORT_SYMBOL_GPL(tty_port_link_device);
  53. /**
  54. * tty_port_register_device - register tty device
  55. * @port: tty_port of the device
  56. * @driver: tty_driver for this device
  57. * @index: index of the tty
  58. * @device: parent if exists, otherwise NULL
  59. *
  60. * It is the same as tty_register_device except the provided @port is linked to
  61. * a concrete tty specified by @index. Use this or tty_port_install (or both).
  62. * Call tty_port_link_device as a last resort.
  63. */
  64. struct device *tty_port_register_device(struct tty_port *port,
  65. struct tty_driver *driver, unsigned index,
  66. struct device *device)
  67. {
  68. tty_port_link_device(port, driver, index);
  69. return tty_register_device(driver, index, device);
  70. }
  71. EXPORT_SYMBOL_GPL(tty_port_register_device);
  72. /**
  73. * tty_port_register_device_attr - register tty device
  74. * @port: tty_port of the device
  75. * @driver: tty_driver for this device
  76. * @index: index of the tty
  77. * @device: parent if exists, otherwise NULL
  78. * @drvdata: Driver data to be set to device.
  79. * @attr_grp: Attribute group to be set on device.
  80. *
  81. * It is the same as tty_register_device_attr except the provided @port is
  82. * linked to a concrete tty specified by @index. Use this or tty_port_install
  83. * (or both). Call tty_port_link_device as a last resort.
  84. */
  85. struct device *tty_port_register_device_attr(struct tty_port *port,
  86. struct tty_driver *driver, unsigned index,
  87. struct device *device, void *drvdata,
  88. const struct attribute_group **attr_grp)
  89. {
  90. tty_port_link_device(port, driver, index);
  91. return tty_register_device_attr(driver, index, device, drvdata,
  92. attr_grp);
  93. }
  94. EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
  95. int tty_port_alloc_xmit_buf(struct tty_port *port)
  96. {
  97. /* We may sleep in get_zeroed_page() */
  98. mutex_lock(&port->buf_mutex);
  99. if (port->xmit_buf == NULL)
  100. port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
  101. mutex_unlock(&port->buf_mutex);
  102. if (port->xmit_buf == NULL)
  103. return -ENOMEM;
  104. return 0;
  105. }
  106. EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
  107. void tty_port_free_xmit_buf(struct tty_port *port)
  108. {
  109. mutex_lock(&port->buf_mutex);
  110. if (port->xmit_buf != NULL) {
  111. free_page((unsigned long)port->xmit_buf);
  112. port->xmit_buf = NULL;
  113. }
  114. mutex_unlock(&port->buf_mutex);
  115. }
  116. EXPORT_SYMBOL(tty_port_free_xmit_buf);
  117. /**
  118. * tty_port_destroy -- destroy inited port
  119. * @port: tty port to be doestroyed
  120. *
  121. * When a port was initialized using tty_port_init, one has to destroy the
  122. * port by this function. Either indirectly by using tty_port refcounting
  123. * (tty_port_put) or directly if refcounting is not used.
  124. */
  125. void tty_port_destroy(struct tty_port *port)
  126. {
  127. cancel_work_sync(&port->buf.work);
  128. tty_buffer_free_all(port);
  129. }
  130. EXPORT_SYMBOL(tty_port_destroy);
  131. static void tty_port_destructor(struct kref *kref)
  132. {
  133. struct tty_port *port = container_of(kref, struct tty_port, kref);
  134. /* check if last port ref was dropped before tty release */
  135. if (WARN_ON(port->itty))
  136. return;
  137. if (port->xmit_buf)
  138. free_page((unsigned long)port->xmit_buf);
  139. tty_port_destroy(port);
  140. if (port->ops && port->ops->destruct)
  141. port->ops->destruct(port);
  142. else
  143. kfree(port);
  144. }
  145. void tty_port_put(struct tty_port *port)
  146. {
  147. if (port)
  148. kref_put(&port->kref, tty_port_destructor);
  149. }
  150. EXPORT_SYMBOL(tty_port_put);
  151. /**
  152. * tty_port_tty_get - get a tty reference
  153. * @port: tty port
  154. *
  155. * Return a refcount protected tty instance or NULL if the port is not
  156. * associated with a tty (eg due to close or hangup)
  157. */
  158. struct tty_struct *tty_port_tty_get(struct tty_port *port)
  159. {
  160. unsigned long flags;
  161. struct tty_struct *tty;
  162. spin_lock_irqsave(&port->lock, flags);
  163. tty = tty_kref_get(port->tty);
  164. spin_unlock_irqrestore(&port->lock, flags);
  165. return tty;
  166. }
  167. EXPORT_SYMBOL(tty_port_tty_get);
  168. /**
  169. * tty_port_tty_set - set the tty of a port
  170. * @port: tty port
  171. * @tty: the tty
  172. *
  173. * Associate the port and tty pair. Manages any internal refcounts.
  174. * Pass NULL to deassociate a port
  175. */
  176. void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
  177. {
  178. unsigned long flags;
  179. spin_lock_irqsave(&port->lock, flags);
  180. if (port->tty)
  181. tty_kref_put(port->tty);
  182. port->tty = tty_kref_get(tty);
  183. spin_unlock_irqrestore(&port->lock, flags);
  184. }
  185. EXPORT_SYMBOL(tty_port_tty_set);
  186. static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty)
  187. {
  188. mutex_lock(&port->mutex);
  189. if (port->console)
  190. goto out;
  191. if (test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) {
  192. /*
  193. * Drop DTR/RTS if HUPCL is set. This causes any attached
  194. * modem to hang up the line.
  195. */
  196. if (tty && C_HUPCL(tty))
  197. tty_port_lower_dtr_rts(port);
  198. if (port->ops->shutdown)
  199. port->ops->shutdown(port);
  200. }
  201. out:
  202. mutex_unlock(&port->mutex);
  203. }
  204. /**
  205. * tty_port_hangup - hangup helper
  206. * @port: tty port
  207. *
  208. * Perform port level tty hangup flag and count changes. Drop the tty
  209. * reference.
  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. port->flags &= ~ASYNC_NORMAL_ACTIVE;
  218. tty = port->tty;
  219. if (tty)
  220. set_bit(TTY_IO_ERROR, &tty->flags);
  221. port->tty = NULL;
  222. spin_unlock_irqrestore(&port->lock, flags);
  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. int tty_port_block_til_ready(struct tty_port *port,
  320. struct tty_struct *tty, struct file *filp)
  321. {
  322. int do_clocal = 0, retval;
  323. unsigned long flags;
  324. DEFINE_WAIT(wait);
  325. /* block if port is in the process of being closed */
  326. if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
  327. wait_event_interruptible_tty(tty, port->close_wait,
  328. !(port->flags & ASYNC_CLOSING));
  329. if (port->flags & ASYNC_HUP_NOTIFY)
  330. return -EAGAIN;
  331. else
  332. return -ERESTARTSYS;
  333. }
  334. /* if non-blocking mode is set we can pass directly to open unless
  335. the port has just hung up or is in another error state */
  336. if (tty->flags & (1 << TTY_IO_ERROR)) {
  337. port->flags |= ASYNC_NORMAL_ACTIVE;
  338. return 0;
  339. }
  340. if (filp->f_flags & O_NONBLOCK) {
  341. /* Indicate we are open */
  342. if (tty->termios.c_cflag & CBAUD)
  343. tty_port_raise_dtr_rts(port);
  344. port->flags |= ASYNC_NORMAL_ACTIVE;
  345. return 0;
  346. }
  347. if (C_CLOCAL(tty))
  348. do_clocal = 1;
  349. /* Block waiting until we can proceed. We may need to wait for the
  350. carrier, but we must also wait for any close that is in progress
  351. before the next open may complete */
  352. retval = 0;
  353. /* The port lock protects the port counts */
  354. spin_lock_irqsave(&port->lock, flags);
  355. if (!tty_hung_up_p(filp))
  356. port->count--;
  357. port->blocked_open++;
  358. spin_unlock_irqrestore(&port->lock, flags);
  359. while (1) {
  360. /* Indicate we are open */
  361. if (C_BAUD(tty) && test_bit(ASYNCB_INITIALIZED, &port->flags))
  362. tty_port_raise_dtr_rts(port);
  363. prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
  364. /* Check for a hangup or uninitialised port.
  365. Return accordingly */
  366. if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
  367. if (port->flags & ASYNC_HUP_NOTIFY)
  368. retval = -EAGAIN;
  369. else
  370. retval = -ERESTARTSYS;
  371. break;
  372. }
  373. /*
  374. * Probe the carrier. For devices with no carrier detect
  375. * tty_port_carrier_raised will always return true.
  376. * Never ask drivers if CLOCAL is set, this causes troubles
  377. * on some hardware.
  378. */
  379. if (!(port->flags & ASYNC_CLOSING) &&
  380. (do_clocal || tty_port_carrier_raised(port)))
  381. break;
  382. if (signal_pending(current)) {
  383. retval = -ERESTARTSYS;
  384. break;
  385. }
  386. tty_unlock(tty);
  387. schedule();
  388. tty_lock(tty);
  389. }
  390. finish_wait(&port->open_wait, &wait);
  391. /* Update counts. A parallel hangup will have set count to zero and
  392. we must not mess that up further */
  393. spin_lock_irqsave(&port->lock, flags);
  394. if (!tty_hung_up_p(filp))
  395. port->count++;
  396. port->blocked_open--;
  397. if (retval == 0)
  398. port->flags |= ASYNC_NORMAL_ACTIVE;
  399. spin_unlock_irqrestore(&port->lock, flags);
  400. return retval;
  401. }
  402. EXPORT_SYMBOL(tty_port_block_til_ready);
  403. static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty)
  404. {
  405. unsigned int bps = tty_get_baud_rate(tty);
  406. long timeout;
  407. if (bps > 1200) {
  408. timeout = (HZ * 10 * port->drain_delay) / bps;
  409. timeout = max_t(long, timeout, HZ / 10);
  410. } else {
  411. timeout = 2 * HZ;
  412. }
  413. schedule_timeout_interruptible(timeout);
  414. }
  415. int tty_port_close_start(struct tty_port *port,
  416. struct tty_struct *tty, struct file *filp)
  417. {
  418. unsigned long flags;
  419. spin_lock_irqsave(&port->lock, flags);
  420. if (tty_hung_up_p(filp)) {
  421. spin_unlock_irqrestore(&port->lock, flags);
  422. return 0;
  423. }
  424. if (tty->count == 1 && port->count != 1) {
  425. printk(KERN_WARNING
  426. "tty_port_close_start: tty->count = 1 port count = %d.\n",
  427. port->count);
  428. port->count = 1;
  429. }
  430. if (--port->count < 0) {
  431. printk(KERN_WARNING "tty_port_close_start: count = %d\n",
  432. port->count);
  433. port->count = 0;
  434. }
  435. if (port->count) {
  436. spin_unlock_irqrestore(&port->lock, flags);
  437. return 0;
  438. }
  439. set_bit(ASYNCB_CLOSING, &port->flags);
  440. tty->closing = 1;
  441. spin_unlock_irqrestore(&port->lock, flags);
  442. if (test_bit(ASYNCB_INITIALIZED, &port->flags)) {
  443. /* Don't block on a stalled port, just pull the chain */
  444. if (tty->flow_stopped)
  445. tty_driver_flush_buffer(tty);
  446. if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  447. tty_wait_until_sent_from_close(tty, port->closing_wait);
  448. if (port->drain_delay)
  449. tty_port_drain_delay(port, tty);
  450. }
  451. /* Flush the ldisc buffering */
  452. tty_ldisc_flush(tty);
  453. /* Report to caller this is the last port reference */
  454. return 1;
  455. }
  456. EXPORT_SYMBOL(tty_port_close_start);
  457. void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
  458. {
  459. unsigned long flags;
  460. spin_lock_irqsave(&port->lock, flags);
  461. tty->closing = 0;
  462. if (port->blocked_open) {
  463. spin_unlock_irqrestore(&port->lock, flags);
  464. if (port->close_delay) {
  465. msleep_interruptible(
  466. jiffies_to_msecs(port->close_delay));
  467. }
  468. spin_lock_irqsave(&port->lock, flags);
  469. wake_up_interruptible(&port->open_wait);
  470. }
  471. port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
  472. wake_up_interruptible(&port->close_wait);
  473. spin_unlock_irqrestore(&port->lock, flags);
  474. }
  475. EXPORT_SYMBOL(tty_port_close_end);
  476. void tty_port_close(struct tty_port *port, struct tty_struct *tty,
  477. struct file *filp)
  478. {
  479. if (tty_port_close_start(port, tty, filp) == 0)
  480. return;
  481. tty_port_shutdown(port, tty);
  482. set_bit(TTY_IO_ERROR, &tty->flags);
  483. tty_port_close_end(port, tty);
  484. tty_port_tty_set(port, NULL);
  485. }
  486. EXPORT_SYMBOL(tty_port_close);
  487. /**
  488. * tty_port_install - generic tty->ops->install handler
  489. * @port: tty_port of the device
  490. * @driver: tty_driver for this device
  491. * @tty: tty to be installed
  492. *
  493. * It is the same as tty_standard_install except the provided @port is linked
  494. * to a concrete tty specified by @tty. Use this or tty_port_register_device
  495. * (or both). Call tty_port_link_device as a last resort.
  496. */
  497. int tty_port_install(struct tty_port *port, struct tty_driver *driver,
  498. struct tty_struct *tty)
  499. {
  500. tty->port = port;
  501. return tty_standard_install(driver, tty);
  502. }
  503. EXPORT_SYMBOL_GPL(tty_port_install);
  504. int tty_port_open(struct tty_port *port, struct tty_struct *tty,
  505. struct file *filp)
  506. {
  507. spin_lock_irq(&port->lock);
  508. if (!tty_hung_up_p(filp))
  509. ++port->count;
  510. spin_unlock_irq(&port->lock);
  511. tty_port_tty_set(port, tty);
  512. /*
  513. * Do the device-specific open only if the hardware isn't
  514. * already initialized. Serialize open and shutdown using the
  515. * port mutex.
  516. */
  517. mutex_lock(&port->mutex);
  518. if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) {
  519. clear_bit(TTY_IO_ERROR, &tty->flags);
  520. if (port->ops->activate) {
  521. int retval = port->ops->activate(port, tty);
  522. if (retval) {
  523. mutex_unlock(&port->mutex);
  524. return retval;
  525. }
  526. }
  527. set_bit(ASYNCB_INITIALIZED, &port->flags);
  528. }
  529. mutex_unlock(&port->mutex);
  530. return tty_port_block_til_ready(port, tty, filp);
  531. }
  532. EXPORT_SYMBOL(tty_port_open);