tty_port.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. /**
  88. * tty_port_hangup - hangup helper
  89. * @port: tty port
  90. *
  91. * Perform port level tty hangup flag and count changes. Drop the tty
  92. * reference.
  93. */
  94. void tty_port_hangup(struct tty_port *port)
  95. {
  96. unsigned long flags;
  97. spin_lock_irqsave(&port->lock, flags);
  98. port->count = 0;
  99. port->flags &= ~ASYNC_NORMAL_ACTIVE;
  100. if (port->tty)
  101. tty_kref_put(port->tty);
  102. port->tty = NULL;
  103. spin_unlock_irqrestore(&port->lock, flags);
  104. wake_up_interruptible(&port->open_wait);
  105. }
  106. EXPORT_SYMBOL(tty_port_hangup);
  107. /**
  108. * tty_port_carrier_raised - carrier raised check
  109. * @port: tty port
  110. *
  111. * Wrapper for the carrier detect logic. For the moment this is used
  112. * to hide some internal details. This will eventually become entirely
  113. * internal to the tty port.
  114. */
  115. int tty_port_carrier_raised(struct tty_port *port)
  116. {
  117. if (port->ops->carrier_raised == NULL)
  118. return 1;
  119. return port->ops->carrier_raised(port);
  120. }
  121. EXPORT_SYMBOL(tty_port_carrier_raised);
  122. /**
  123. * tty_port_raise_dtr_rts - Riase DTR/RTS
  124. * @port: tty port
  125. *
  126. * Wrapper for the DTR/RTS raise logic. For the moment this is used
  127. * to hide some internal details. This will eventually become entirely
  128. * internal to the tty port.
  129. */
  130. void tty_port_raise_dtr_rts(struct tty_port *port)
  131. {
  132. if (port->ops->raise_dtr_rts)
  133. port->ops->raise_dtr_rts(port);
  134. }
  135. EXPORT_SYMBOL(tty_port_raise_dtr_rts);
  136. /**
  137. * tty_port_block_til_ready - Waiting logic for tty open
  138. * @port: the tty port being opened
  139. * @tty: the tty device being bound
  140. * @filp: the file pointer of the opener
  141. *
  142. * Implement the core POSIX/SuS tty behaviour when opening a tty device.
  143. * Handles:
  144. * - hangup (both before and during)
  145. * - non blocking open
  146. * - rts/dtr/dcd
  147. * - signals
  148. * - port flags and counts
  149. *
  150. * The passed tty_port must implement the carrier_raised method if it can
  151. * do carrier detect and the raise_dtr_rts method if it supports software
  152. * management of these lines. Note that the dtr/rts raise is done each
  153. * iteration as a hangup may have previously dropped them while we wait.
  154. */
  155. int tty_port_block_til_ready(struct tty_port *port,
  156. struct tty_struct *tty, struct file *filp)
  157. {
  158. int do_clocal = 0, retval;
  159. unsigned long flags;
  160. DECLARE_WAITQUEUE(wait, current);
  161. int cd;
  162. /* block if port is in the process of being closed */
  163. if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
  164. interruptible_sleep_on(&port->close_wait);
  165. if (port->flags & ASYNC_HUP_NOTIFY)
  166. return -EAGAIN;
  167. else
  168. return -ERESTARTSYS;
  169. }
  170. /* if non-blocking mode is set we can pass directly to open unless
  171. the port has just hung up or is in another error state */
  172. if ((filp->f_flags & O_NONBLOCK) ||
  173. (tty->flags & (1 << TTY_IO_ERROR))) {
  174. port->flags |= ASYNC_NORMAL_ACTIVE;
  175. return 0;
  176. }
  177. if (C_CLOCAL(tty))
  178. do_clocal = 1;
  179. /* Block waiting until we can proceed. We may need to wait for the
  180. carrier, but we must also wait for any close that is in progress
  181. before the next open may complete */
  182. retval = 0;
  183. add_wait_queue(&port->open_wait, &wait);
  184. /* The port lock protects the port counts */
  185. spin_lock_irqsave(&port->lock, flags);
  186. if (!tty_hung_up_p(filp))
  187. port->count--;
  188. port->blocked_open++;
  189. spin_unlock_irqrestore(&port->lock, flags);
  190. while (1) {
  191. /* Indicate we are open */
  192. if (tty->termios->c_cflag & CBAUD)
  193. tty_port_raise_dtr_rts(port);
  194. set_current_state(TASK_INTERRUPTIBLE);
  195. /* Check for a hangup or uninitialised port. Return accordingly */
  196. if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
  197. if (port->flags & ASYNC_HUP_NOTIFY)
  198. retval = -EAGAIN;
  199. else
  200. retval = -ERESTARTSYS;
  201. break;
  202. }
  203. /* Probe the carrier. For devices with no carrier detect this
  204. will always return true */
  205. cd = tty_port_carrier_raised(port);
  206. if (!(port->flags & ASYNC_CLOSING) &&
  207. (do_clocal || cd))
  208. break;
  209. if (signal_pending(current)) {
  210. retval = -ERESTARTSYS;
  211. break;
  212. }
  213. schedule();
  214. }
  215. set_current_state(TASK_RUNNING);
  216. remove_wait_queue(&port->open_wait, &wait);
  217. /* Update counts. A parallel hangup will have set count to zero and
  218. we must not mess that up further */
  219. spin_lock_irqsave(&port->lock, flags);
  220. if (!tty_hung_up_p(filp))
  221. port->count++;
  222. port->blocked_open--;
  223. if (retval == 0)
  224. port->flags |= ASYNC_NORMAL_ACTIVE;
  225. spin_unlock_irqrestore(&port->lock, flags);
  226. return 0;
  227. }
  228. EXPORT_SYMBOL(tty_port_block_til_ready);
  229. int tty_port_close_start(struct tty_port *port, struct tty_struct *tty, struct file *filp)
  230. {
  231. unsigned long flags;
  232. spin_lock_irqsave(&port->lock, flags);
  233. if (tty_hung_up_p(filp)) {
  234. spin_unlock_irqrestore(&port->lock, flags);
  235. return 0;
  236. }
  237. if( tty->count == 1 && port->count != 1) {
  238. printk(KERN_WARNING
  239. "tty_port_close_start: tty->count = 1 port count = %d.\n",
  240. port->count);
  241. port->count = 1;
  242. }
  243. if (--port->count < 0) {
  244. printk(KERN_WARNING "tty_port_close_start: count = %d\n",
  245. port->count);
  246. port->count = 0;
  247. }
  248. if (port->count) {
  249. spin_unlock_irqrestore(&port->lock, flags);
  250. return 0;
  251. }
  252. port->flags |= ASYNC_CLOSING;
  253. tty->closing = 1;
  254. spin_unlock_irqrestore(&port->lock, flags);
  255. /* Don't block on a stalled port, just pull the chain */
  256. if (tty->flow_stopped)
  257. tty_driver_flush_buffer(tty);
  258. if (port->flags & ASYNC_INITIALIZED &&
  259. port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  260. tty_wait_until_sent(tty, port->closing_wait);
  261. return 1;
  262. }
  263. EXPORT_SYMBOL(tty_port_close_start);
  264. void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
  265. {
  266. unsigned long flags;
  267. tty_ldisc_flush(tty);
  268. spin_lock_irqsave(&port->lock, flags);
  269. tty->closing = 0;
  270. if (port->blocked_open) {
  271. spin_unlock_irqrestore(&port->lock, flags);
  272. if (port->close_delay) {
  273. msleep_interruptible(
  274. jiffies_to_msecs(port->close_delay));
  275. }
  276. spin_lock_irqsave(&port->lock, flags);
  277. wake_up_interruptible(&port->open_wait);
  278. }
  279. port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
  280. wake_up_interruptible(&port->close_wait);
  281. spin_unlock_irqrestore(&port->lock, flags);
  282. }
  283. EXPORT_SYMBOL(tty_port_close_end);