tty_port.c 11 KB

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