tty_port.c 11 KB

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