tty_port.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. }
  240. EXPORT_SYMBOL_GPL(tty_port_tty_hangup);
  241. /**
  242. * tty_port_tty_wakeup - helper to wake up a tty
  243. *
  244. * @port: tty port
  245. */
  246. void tty_port_tty_wakeup(struct tty_port *port)
  247. {
  248. struct tty_struct *tty = tty_port_tty_get(port);
  249. if (tty) {
  250. tty_wakeup(tty);
  251. tty_kref_put(tty);
  252. }
  253. }
  254. EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
  255. /**
  256. * tty_port_carrier_raised - carrier raised check
  257. * @port: tty port
  258. *
  259. * Wrapper for the carrier detect logic. For the moment this is used
  260. * to hide some internal details. This will eventually become entirely
  261. * internal to the tty port.
  262. */
  263. int tty_port_carrier_raised(struct tty_port *port)
  264. {
  265. if (port->ops->carrier_raised == NULL)
  266. return 1;
  267. return port->ops->carrier_raised(port);
  268. }
  269. EXPORT_SYMBOL(tty_port_carrier_raised);
  270. /**
  271. * tty_port_raise_dtr_rts - Raise DTR/RTS
  272. * @port: tty port
  273. *
  274. * Wrapper for the DTR/RTS raise logic. For the moment this is used
  275. * to hide some internal details. This will eventually become entirely
  276. * internal to the tty port.
  277. */
  278. void tty_port_raise_dtr_rts(struct tty_port *port)
  279. {
  280. if (port->ops->dtr_rts)
  281. port->ops->dtr_rts(port, 1);
  282. }
  283. EXPORT_SYMBOL(tty_port_raise_dtr_rts);
  284. /**
  285. * tty_port_lower_dtr_rts - Lower DTR/RTS
  286. * @port: tty port
  287. *
  288. * Wrapper for the DTR/RTS raise logic. For the moment this is used
  289. * to hide some internal details. This will eventually become entirely
  290. * internal to the tty port.
  291. */
  292. void tty_port_lower_dtr_rts(struct tty_port *port)
  293. {
  294. if (port->ops->dtr_rts)
  295. port->ops->dtr_rts(port, 0);
  296. }
  297. EXPORT_SYMBOL(tty_port_lower_dtr_rts);
  298. /**
  299. * tty_port_block_til_ready - Waiting logic for tty open
  300. * @port: the tty port being opened
  301. * @tty: the tty device being bound
  302. * @filp: the file pointer of the opener
  303. *
  304. * Implement the core POSIX/SuS tty behaviour when opening a tty device.
  305. * Handles:
  306. * - hangup (both before and during)
  307. * - non blocking open
  308. * - rts/dtr/dcd
  309. * - signals
  310. * - port flags and counts
  311. *
  312. * The passed tty_port must implement the carrier_raised method if it can
  313. * do carrier detect and the dtr_rts method if it supports software
  314. * management of these lines. Note that the dtr/rts raise is done each
  315. * iteration as a hangup may have previously dropped them while we wait.
  316. */
  317. int tty_port_block_til_ready(struct tty_port *port,
  318. struct tty_struct *tty, struct file *filp)
  319. {
  320. int do_clocal = 0, retval;
  321. unsigned long flags;
  322. DEFINE_WAIT(wait);
  323. /* block if port is in the process of being closed */
  324. if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
  325. wait_event_interruptible_tty(tty, port->close_wait,
  326. !(port->flags & ASYNC_CLOSING));
  327. if (port->flags & ASYNC_HUP_NOTIFY)
  328. return -EAGAIN;
  329. else
  330. return -ERESTARTSYS;
  331. }
  332. /* if non-blocking mode is set we can pass directly to open unless
  333. the port has just hung up or is in another error state */
  334. if (tty->flags & (1 << TTY_IO_ERROR)) {
  335. port->flags |= ASYNC_NORMAL_ACTIVE;
  336. return 0;
  337. }
  338. if (filp->f_flags & O_NONBLOCK) {
  339. /* Indicate we are open */
  340. if (tty->termios.c_cflag & CBAUD)
  341. tty_port_raise_dtr_rts(port);
  342. port->flags |= ASYNC_NORMAL_ACTIVE;
  343. return 0;
  344. }
  345. if (C_CLOCAL(tty))
  346. do_clocal = 1;
  347. /* Block waiting until we can proceed. We may need to wait for the
  348. carrier, but we must also wait for any close that is in progress
  349. before the next open may complete */
  350. retval = 0;
  351. /* The port lock protects the port counts */
  352. spin_lock_irqsave(&port->lock, flags);
  353. if (!tty_hung_up_p(filp))
  354. port->count--;
  355. port->blocked_open++;
  356. spin_unlock_irqrestore(&port->lock, flags);
  357. while (1) {
  358. /* Indicate we are open */
  359. if (C_BAUD(tty) && test_bit(ASYNCB_INITIALIZED, &port->flags))
  360. tty_port_raise_dtr_rts(port);
  361. prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
  362. /* Check for a hangup or uninitialised port.
  363. Return accordingly */
  364. if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
  365. if (port->flags & ASYNC_HUP_NOTIFY)
  366. retval = -EAGAIN;
  367. else
  368. retval = -ERESTARTSYS;
  369. break;
  370. }
  371. /*
  372. * Probe the carrier. For devices with no carrier detect
  373. * tty_port_carrier_raised will always return true.
  374. * Never ask drivers if CLOCAL is set, this causes troubles
  375. * on some hardware.
  376. */
  377. if (!(port->flags & ASYNC_CLOSING) &&
  378. (do_clocal || tty_port_carrier_raised(port)))
  379. break;
  380. if (signal_pending(current)) {
  381. retval = -ERESTARTSYS;
  382. break;
  383. }
  384. tty_unlock(tty);
  385. schedule();
  386. tty_lock(tty);
  387. }
  388. finish_wait(&port->open_wait, &wait);
  389. /* Update counts. A parallel hangup will have set count to zero and
  390. we must not mess that up further */
  391. spin_lock_irqsave(&port->lock, flags);
  392. if (!tty_hung_up_p(filp))
  393. port->count++;
  394. port->blocked_open--;
  395. if (retval == 0)
  396. port->flags |= ASYNC_NORMAL_ACTIVE;
  397. spin_unlock_irqrestore(&port->lock, flags);
  398. return retval;
  399. }
  400. EXPORT_SYMBOL(tty_port_block_til_ready);
  401. static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty)
  402. {
  403. unsigned int bps = tty_get_baud_rate(tty);
  404. long timeout;
  405. if (bps > 1200) {
  406. timeout = (HZ * 10 * port->drain_delay) / bps;
  407. timeout = max_t(long, timeout, HZ / 10);
  408. } else {
  409. timeout = 2 * HZ;
  410. }
  411. schedule_timeout_interruptible(timeout);
  412. }
  413. int tty_port_close_start(struct tty_port *port,
  414. struct tty_struct *tty, struct file *filp)
  415. {
  416. unsigned long flags;
  417. spin_lock_irqsave(&port->lock, flags);
  418. if (tty_hung_up_p(filp)) {
  419. spin_unlock_irqrestore(&port->lock, flags);
  420. return 0;
  421. }
  422. if (tty->count == 1 && port->count != 1) {
  423. printk(KERN_WARNING
  424. "tty_port_close_start: tty->count = 1 port count = %d.\n",
  425. port->count);
  426. port->count = 1;
  427. }
  428. if (--port->count < 0) {
  429. printk(KERN_WARNING "tty_port_close_start: count = %d\n",
  430. port->count);
  431. port->count = 0;
  432. }
  433. if (port->count) {
  434. spin_unlock_irqrestore(&port->lock, flags);
  435. if (port->ops->drop)
  436. port->ops->drop(port);
  437. return 0;
  438. }
  439. set_bit(ASYNCB_CLOSING, &port->flags);
  440. tty->closing = 1;
  441. spin_unlock_irqrestore(&port->lock, flags);
  442. if (test_bit(ASYNCB_INITIALIZED, &port->flags)) {
  443. /* Don't block on a stalled port, just pull the chain */
  444. if (tty->flow_stopped)
  445. tty_driver_flush_buffer(tty);
  446. if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  447. tty_wait_until_sent_from_close(tty, port->closing_wait);
  448. if (port->drain_delay)
  449. tty_port_drain_delay(port, tty);
  450. }
  451. /* Flush the ldisc buffering */
  452. tty_ldisc_flush(tty);
  453. /* Don't call port->drop for the last reference. Callers will want
  454. to drop the last active reference in ->shutdown() or the tty
  455. shutdown path */
  456. return 1;
  457. }
  458. EXPORT_SYMBOL(tty_port_close_start);
  459. void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
  460. {
  461. unsigned long flags;
  462. spin_lock_irqsave(&port->lock, flags);
  463. tty->closing = 0;
  464. if (port->blocked_open) {
  465. spin_unlock_irqrestore(&port->lock, flags);
  466. if (port->close_delay) {
  467. msleep_interruptible(
  468. jiffies_to_msecs(port->close_delay));
  469. }
  470. spin_lock_irqsave(&port->lock, flags);
  471. wake_up_interruptible(&port->open_wait);
  472. }
  473. port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
  474. wake_up_interruptible(&port->close_wait);
  475. spin_unlock_irqrestore(&port->lock, flags);
  476. }
  477. EXPORT_SYMBOL(tty_port_close_end);
  478. void tty_port_close(struct tty_port *port, struct tty_struct *tty,
  479. struct file *filp)
  480. {
  481. if (tty_port_close_start(port, tty, filp) == 0)
  482. return;
  483. tty_port_shutdown(port, tty);
  484. set_bit(TTY_IO_ERROR, &tty->flags);
  485. tty_port_close_end(port, tty);
  486. tty_port_tty_set(port, NULL);
  487. }
  488. EXPORT_SYMBOL(tty_port_close);
  489. /**
  490. * tty_port_install - generic tty->ops->install handler
  491. * @port: tty_port of the device
  492. * @driver: tty_driver for this device
  493. * @tty: tty to be installed
  494. *
  495. * It is the same as tty_standard_install except the provided @port is linked
  496. * to a concrete tty specified by @tty. Use this or tty_port_register_device
  497. * (or both). Call tty_port_link_device as a last resort.
  498. */
  499. int tty_port_install(struct tty_port *port, struct tty_driver *driver,
  500. struct tty_struct *tty)
  501. {
  502. tty->port = port;
  503. return tty_standard_install(driver, tty);
  504. }
  505. EXPORT_SYMBOL_GPL(tty_port_install);
  506. int tty_port_open(struct tty_port *port, struct tty_struct *tty,
  507. struct file *filp)
  508. {
  509. spin_lock_irq(&port->lock);
  510. if (!tty_hung_up_p(filp))
  511. ++port->count;
  512. spin_unlock_irq(&port->lock);
  513. tty_port_tty_set(port, tty);
  514. /*
  515. * Do the device-specific open only if the hardware isn't
  516. * already initialized. Serialize open and shutdown using the
  517. * port mutex.
  518. */
  519. mutex_lock(&port->mutex);
  520. if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) {
  521. clear_bit(TTY_IO_ERROR, &tty->flags);
  522. if (port->ops->activate) {
  523. int retval = port->ops->activate(port, tty);
  524. if (retval) {
  525. mutex_unlock(&port->mutex);
  526. return retval;
  527. }
  528. }
  529. set_bit(ASYNCB_INITIALIZED, &port->flags);
  530. }
  531. mutex_unlock(&port->mutex);
  532. return tty_port_block_til_ready(port, tty, filp);
  533. }
  534. EXPORT_SYMBOL(tty_port_open);