tty_port.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. init_waitqueue_head(&port->open_wait);
  23. init_waitqueue_head(&port->close_wait);
  24. mutex_init(&port->mutex);
  25. spin_lock_init(&port->lock);
  26. port->close_delay = (50 * HZ) / 100;
  27. port->closing_wait = (3000 * HZ) / 100;
  28. }
  29. EXPORT_SYMBOL(tty_port_init);
  30. int tty_port_alloc_xmit_buf(struct tty_port *port)
  31. {
  32. /* We may sleep in get_zeroed_page() */
  33. mutex_lock(&port->mutex);
  34. if (port->xmit_buf == NULL)
  35. port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
  36. mutex_unlock(&port->mutex);
  37. if (port->xmit_buf == NULL)
  38. return -ENOMEM;
  39. return 0;
  40. }
  41. EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
  42. void tty_port_free_xmit_buf(struct tty_port *port)
  43. {
  44. mutex_lock(&port->mutex);
  45. if (port->xmit_buf != NULL) {
  46. free_page((unsigned long)port->xmit_buf);
  47. port->xmit_buf = NULL;
  48. }
  49. mutex_unlock(&port->mutex);
  50. }
  51. EXPORT_SYMBOL(tty_port_free_xmit_buf);
  52. /**
  53. * tty_port_tty_get - get a tty reference
  54. * @port: tty port
  55. *
  56. * Return a refcount protected tty instance or NULL if the port is not
  57. * associated with a tty (eg due to close or hangup)
  58. */
  59. struct tty_struct *tty_port_tty_get(struct tty_port *port)
  60. {
  61. unsigned long flags;
  62. struct tty_struct *tty;
  63. spin_lock_irqsave(&port->lock, flags);
  64. tty = tty_kref_get(port->tty);
  65. spin_unlock_irqrestore(&port->lock, flags);
  66. return tty;
  67. }
  68. EXPORT_SYMBOL(tty_port_tty_get);
  69. /**
  70. * tty_port_tty_set - set the tty of a port
  71. * @port: tty port
  72. * @tty: the tty
  73. *
  74. * Associate the port and tty pair. Manages any internal refcounts.
  75. * Pass NULL to deassociate a port
  76. */
  77. void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
  78. {
  79. unsigned long flags;
  80. spin_lock_irqsave(&port->lock, flags);
  81. if (port->tty)
  82. tty_kref_put(port->tty);
  83. port->tty = tty_kref_get(tty);
  84. spin_unlock_irqrestore(&port->lock, flags);
  85. }
  86. EXPORT_SYMBOL(tty_port_tty_set);
  87. static void tty_port_shutdown(struct tty_port *port)
  88. {
  89. if (port->ops->shutdown &&
  90. test_and_clear_bit(ASYNC_INITIALIZED, &port->flags))
  91. port->ops->shutdown(port);
  92. }
  93. /**
  94. * tty_port_hangup - hangup helper
  95. * @port: tty port
  96. *
  97. * Perform port level tty hangup flag and count changes. Drop the tty
  98. * reference.
  99. */
  100. void tty_port_hangup(struct tty_port *port)
  101. {
  102. unsigned long flags;
  103. spin_lock_irqsave(&port->lock, flags);
  104. port->count = 0;
  105. port->flags &= ~ASYNC_NORMAL_ACTIVE;
  106. if (port->tty)
  107. tty_kref_put(port->tty);
  108. port->tty = NULL;
  109. spin_unlock_irqrestore(&port->lock, flags);
  110. wake_up_interruptible(&port->open_wait);
  111. tty_port_shutdown(port);
  112. }
  113. EXPORT_SYMBOL(tty_port_hangup);
  114. /**
  115. * tty_port_carrier_raised - carrier raised check
  116. * @port: tty port
  117. *
  118. * Wrapper for the carrier detect logic. For the moment this is used
  119. * to hide some internal details. This will eventually become entirely
  120. * internal to the tty port.
  121. */
  122. int tty_port_carrier_raised(struct tty_port *port)
  123. {
  124. if (port->ops->carrier_raised == NULL)
  125. return 1;
  126. return port->ops->carrier_raised(port);
  127. }
  128. EXPORT_SYMBOL(tty_port_carrier_raised);
  129. /**
  130. * tty_port_raise_dtr_rts - Raise DTR/RTS
  131. * @port: tty port
  132. *
  133. * Wrapper for the DTR/RTS raise logic. For the moment this is used
  134. * to hide some internal details. This will eventually become entirely
  135. * internal to the tty port.
  136. */
  137. void tty_port_raise_dtr_rts(struct tty_port *port)
  138. {
  139. if (port->ops->dtr_rts)
  140. port->ops->dtr_rts(port, 1);
  141. }
  142. EXPORT_SYMBOL(tty_port_raise_dtr_rts);
  143. /**
  144. * tty_port_lower_dtr_rts - Lower DTR/RTS
  145. * @port: tty port
  146. *
  147. * Wrapper for the DTR/RTS raise logic. For the moment this is used
  148. * to hide some internal details. This will eventually become entirely
  149. * internal to the tty port.
  150. */
  151. void tty_port_lower_dtr_rts(struct tty_port *port)
  152. {
  153. if (port->ops->dtr_rts)
  154. port->ops->dtr_rts(port, 0);
  155. }
  156. EXPORT_SYMBOL(tty_port_lower_dtr_rts);
  157. /**
  158. * tty_port_block_til_ready - Waiting logic for tty open
  159. * @port: the tty port being opened
  160. * @tty: the tty device being bound
  161. * @filp: the file pointer of the opener
  162. *
  163. * Implement the core POSIX/SuS tty behaviour when opening a tty device.
  164. * Handles:
  165. * - hangup (both before and during)
  166. * - non blocking open
  167. * - rts/dtr/dcd
  168. * - signals
  169. * - port flags and counts
  170. *
  171. * The passed tty_port must implement the carrier_raised method if it can
  172. * do carrier detect and the dtr_rts method if it supports software
  173. * management of these lines. Note that the dtr/rts raise is done each
  174. * iteration as a hangup may have previously dropped them while we wait.
  175. */
  176. int tty_port_block_til_ready(struct tty_port *port,
  177. struct tty_struct *tty, struct file *filp)
  178. {
  179. int do_clocal = 0, retval;
  180. unsigned long flags;
  181. DEFINE_WAIT(wait);
  182. int cd;
  183. /* block if port is in the process of being closed */
  184. if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
  185. wait_event_interruptible(port->close_wait,
  186. !(port->flags & ASYNC_CLOSING));
  187. if (port->flags & ASYNC_HUP_NOTIFY)
  188. return -EAGAIN;
  189. else
  190. return -ERESTARTSYS;
  191. }
  192. /* if non-blocking mode is set we can pass directly to open unless
  193. the port has just hung up or is in another error state */
  194. if ((filp->f_flags & O_NONBLOCK) ||
  195. (tty->flags & (1 << TTY_IO_ERROR))) {
  196. port->flags |= ASYNC_NORMAL_ACTIVE;
  197. return 0;
  198. }
  199. if (C_CLOCAL(tty))
  200. do_clocal = 1;
  201. /* Block waiting until we can proceed. We may need to wait for the
  202. carrier, but we must also wait for any close that is in progress
  203. before the next open may complete */
  204. retval = 0;
  205. /* The port lock protects the port counts */
  206. spin_lock_irqsave(&port->lock, flags);
  207. if (!tty_hung_up_p(filp))
  208. port->count--;
  209. port->blocked_open++;
  210. spin_unlock_irqrestore(&port->lock, flags);
  211. while (1) {
  212. /* Indicate we are open */
  213. if (tty->termios->c_cflag & CBAUD)
  214. tty_port_raise_dtr_rts(port);
  215. prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
  216. /* Check for a hangup or uninitialised port. Return accordingly */
  217. if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
  218. if (port->flags & ASYNC_HUP_NOTIFY)
  219. retval = -EAGAIN;
  220. else
  221. retval = -ERESTARTSYS;
  222. break;
  223. }
  224. /* Probe the carrier. For devices with no carrier detect this
  225. will always return true */
  226. cd = tty_port_carrier_raised(port);
  227. if (!(port->flags & ASYNC_CLOSING) &&
  228. (do_clocal || cd))
  229. break;
  230. if (signal_pending(current)) {
  231. retval = -ERESTARTSYS;
  232. break;
  233. }
  234. schedule();
  235. }
  236. finish_wait(&port->open_wait, &wait);
  237. /* Update counts. A parallel hangup will have set count to zero and
  238. we must not mess that up further */
  239. spin_lock_irqsave(&port->lock, flags);
  240. if (!tty_hung_up_p(filp))
  241. port->count++;
  242. port->blocked_open--;
  243. if (retval == 0)
  244. port->flags |= ASYNC_NORMAL_ACTIVE;
  245. spin_unlock_irqrestore(&port->lock, flags);
  246. return retval;
  247. }
  248. EXPORT_SYMBOL(tty_port_block_til_ready);
  249. int tty_port_close_start(struct tty_port *port, struct tty_struct *tty, struct file *filp)
  250. {
  251. unsigned long flags;
  252. spin_lock_irqsave(&port->lock, flags);
  253. if (tty_hung_up_p(filp)) {
  254. spin_unlock_irqrestore(&port->lock, flags);
  255. return 0;
  256. }
  257. if( tty->count == 1 && port->count != 1) {
  258. printk(KERN_WARNING
  259. "tty_port_close_start: tty->count = 1 port count = %d.\n",
  260. port->count);
  261. port->count = 1;
  262. }
  263. if (--port->count < 0) {
  264. printk(KERN_WARNING "tty_port_close_start: count = %d\n",
  265. port->count);
  266. port->count = 0;
  267. }
  268. if (port->count) {
  269. spin_unlock_irqrestore(&port->lock, flags);
  270. if (port->ops->drop)
  271. port->ops->drop(port);
  272. return 0;
  273. }
  274. set_bit(ASYNC_CLOSING, &port->flags);
  275. tty->closing = 1;
  276. spin_unlock_irqrestore(&port->lock, flags);
  277. /* Don't block on a stalled port, just pull the chain */
  278. if (tty->flow_stopped)
  279. tty_driver_flush_buffer(tty);
  280. if (test_bit(ASYNCB_INITIALIZED, &port->flags) &&
  281. port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  282. tty_wait_until_sent(tty, port->closing_wait);
  283. if (port->drain_delay) {
  284. unsigned int bps = tty_get_baud_rate(tty);
  285. long timeout;
  286. if (bps > 1200)
  287. timeout = max_t(long, (HZ * 10 * port->drain_delay) / bps,
  288. HZ / 10);
  289. else
  290. timeout = 2 * HZ;
  291. schedule_timeout_interruptible(timeout);
  292. }
  293. /* Don't call port->drop for the last reference. Callers will want
  294. to drop the last active reference in ->shutdown() or the tty
  295. shutdown path */
  296. return 1;
  297. }
  298. EXPORT_SYMBOL(tty_port_close_start);
  299. void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
  300. {
  301. unsigned long flags;
  302. tty_ldisc_flush(tty);
  303. if (tty->termios->c_cflag & HUPCL)
  304. tty_port_lower_dtr_rts(port);
  305. spin_lock_irqsave(&port->lock, flags);
  306. tty->closing = 0;
  307. if (port->blocked_open) {
  308. spin_unlock_irqrestore(&port->lock, flags);
  309. if (port->close_delay) {
  310. msleep_interruptible(
  311. jiffies_to_msecs(port->close_delay));
  312. }
  313. spin_lock_irqsave(&port->lock, flags);
  314. wake_up_interruptible(&port->open_wait);
  315. }
  316. port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
  317. wake_up_interruptible(&port->close_wait);
  318. spin_unlock_irqrestore(&port->lock, flags);
  319. }
  320. EXPORT_SYMBOL(tty_port_close_end);
  321. void tty_port_close(struct tty_port *port, struct tty_struct *tty,
  322. struct file *filp)
  323. {
  324. if (tty_port_close_start(port, tty, filp) == 0)
  325. return;
  326. tty_port_shutdown(port);
  327. tty_port_close_end(port, tty);
  328. tty_port_tty_set(port, NULL);
  329. }
  330. EXPORT_SYMBOL(tty_port_close);