tty_port.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 - Raise 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->dtr_rts)
  133. port->ops->dtr_rts(port, 1);
  134. }
  135. EXPORT_SYMBOL(tty_port_raise_dtr_rts);
  136. /**
  137. * tty_port_lower_dtr_rts - Lower DTR/RTS
  138. * @port: tty port
  139. *
  140. * Wrapper for the DTR/RTS raise logic. For the moment this is used
  141. * to hide some internal details. This will eventually become entirely
  142. * internal to the tty port.
  143. */
  144. void tty_port_lower_dtr_rts(struct tty_port *port)
  145. {
  146. if (port->ops->dtr_rts)
  147. port->ops->dtr_rts(port, 0);
  148. }
  149. EXPORT_SYMBOL(tty_port_lower_dtr_rts);
  150. /**
  151. * tty_port_block_til_ready - Waiting logic for tty open
  152. * @port: the tty port being opened
  153. * @tty: the tty device being bound
  154. * @filp: the file pointer of the opener
  155. *
  156. * Implement the core POSIX/SuS tty behaviour when opening a tty device.
  157. * Handles:
  158. * - hangup (both before and during)
  159. * - non blocking open
  160. * - rts/dtr/dcd
  161. * - signals
  162. * - port flags and counts
  163. *
  164. * The passed tty_port must implement the carrier_raised method if it can
  165. * do carrier detect and the dtr_rts method if it supports software
  166. * management of these lines. Note that the dtr/rts raise is done each
  167. * iteration as a hangup may have previously dropped them while we wait.
  168. */
  169. int tty_port_block_til_ready(struct tty_port *port,
  170. struct tty_struct *tty, struct file *filp)
  171. {
  172. int do_clocal = 0, retval;
  173. unsigned long flags;
  174. DECLARE_WAITQUEUE(wait, current);
  175. int cd;
  176. /* block if port is in the process of being closed */
  177. if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
  178. wait_event_interruptible(port->close_wait,
  179. !(port->flags & ASYNC_CLOSING));
  180. if (port->flags & ASYNC_HUP_NOTIFY)
  181. return -EAGAIN;
  182. else
  183. return -ERESTARTSYS;
  184. }
  185. /* if non-blocking mode is set we can pass directly to open unless
  186. the port has just hung up or is in another error state */
  187. if ((filp->f_flags & O_NONBLOCK) ||
  188. (tty->flags & (1 << TTY_IO_ERROR))) {
  189. port->flags |= ASYNC_NORMAL_ACTIVE;
  190. return 0;
  191. }
  192. if (C_CLOCAL(tty))
  193. do_clocal = 1;
  194. /* Block waiting until we can proceed. We may need to wait for the
  195. carrier, but we must also wait for any close that is in progress
  196. before the next open may complete */
  197. retval = 0;
  198. /* The port lock protects the port counts */
  199. spin_lock_irqsave(&port->lock, flags);
  200. if (!tty_hung_up_p(filp))
  201. port->count--;
  202. port->blocked_open++;
  203. spin_unlock_irqrestore(&port->lock, flags);
  204. while (1) {
  205. /* Indicate we are open */
  206. if (tty->termios->c_cflag & CBAUD)
  207. tty_port_raise_dtr_rts(port);
  208. prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
  209. /* Check for a hangup or uninitialised port. Return accordingly */
  210. if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
  211. if (port->flags & ASYNC_HUP_NOTIFY)
  212. retval = -EAGAIN;
  213. else
  214. retval = -ERESTARTSYS;
  215. break;
  216. }
  217. /* Probe the carrier. For devices with no carrier detect this
  218. will always return true */
  219. cd = tty_port_carrier_raised(port);
  220. if (!(port->flags & ASYNC_CLOSING) &&
  221. (do_clocal || cd))
  222. break;
  223. if (signal_pending(current)) {
  224. retval = -ERESTARTSYS;
  225. break;
  226. }
  227. schedule();
  228. }
  229. finish_wait(&port->open_wait, &wait);
  230. /* Update counts. A parallel hangup will have set count to zero and
  231. we must not mess that up further */
  232. spin_lock_irqsave(&port->lock, flags);
  233. if (!tty_hung_up_p(filp))
  234. port->count++;
  235. port->blocked_open--;
  236. if (retval == 0)
  237. port->flags |= ASYNC_NORMAL_ACTIVE;
  238. spin_unlock_irqrestore(&port->lock, flags);
  239. return 0;
  240. }
  241. EXPORT_SYMBOL(tty_port_block_til_ready);
  242. int tty_port_close_start(struct tty_port *port, struct tty_struct *tty, struct file *filp)
  243. {
  244. unsigned long flags;
  245. spin_lock_irqsave(&port->lock, flags);
  246. if (tty_hung_up_p(filp)) {
  247. spin_unlock_irqrestore(&port->lock, flags);
  248. return 0;
  249. }
  250. if( tty->count == 1 && port->count != 1) {
  251. printk(KERN_WARNING
  252. "tty_port_close_start: tty->count = 1 port count = %d.\n",
  253. port->count);
  254. port->count = 1;
  255. }
  256. if (--port->count < 0) {
  257. printk(KERN_WARNING "tty_port_close_start: count = %d\n",
  258. port->count);
  259. port->count = 0;
  260. }
  261. if (port->count) {
  262. spin_unlock_irqrestore(&port->lock, flags);
  263. return 0;
  264. }
  265. port->flags |= ASYNC_CLOSING;
  266. tty->closing = 1;
  267. spin_unlock_irqrestore(&port->lock, flags);
  268. /* Don't block on a stalled port, just pull the chain */
  269. if (tty->flow_stopped)
  270. tty_driver_flush_buffer(tty);
  271. if (port->flags & ASYNC_INITIALIZED &&
  272. port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  273. tty_wait_until_sent(tty, port->closing_wait);
  274. if (port->drain_delay) {
  275. unsigned int bps = tty_get_baud_rate(tty);
  276. long timeout;
  277. if (bps > 1200)
  278. timeout = max_t(long, (HZ * 10 * port->drain_delay) / bps,
  279. HZ / 10);
  280. else
  281. timeout = 2 * HZ;
  282. schedule_timeout_interruptible(timeout);
  283. }
  284. return 1;
  285. }
  286. EXPORT_SYMBOL(tty_port_close_start);
  287. void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
  288. {
  289. unsigned long flags;
  290. tty_ldisc_flush(tty);
  291. if (tty->termios->c_cflag & HUPCL)
  292. tty_port_lower_dtr_rts(port);
  293. spin_lock_irqsave(&port->lock, flags);
  294. tty->closing = 0;
  295. if (port->blocked_open) {
  296. spin_unlock_irqrestore(&port->lock, flags);
  297. if (port->close_delay) {
  298. msleep_interruptible(
  299. jiffies_to_msecs(port->close_delay));
  300. }
  301. spin_lock_irqsave(&port->lock, flags);
  302. wake_up_interruptible(&port->open_wait);
  303. }
  304. port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
  305. wake_up_interruptible(&port->close_wait);
  306. spin_unlock_irqrestore(&port->lock, flags);
  307. }
  308. EXPORT_SYMBOL(tty_port_close_end);