tty_port.c 15 KB

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