tty_port.c 13 KB

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