tty_port.c 12 KB

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