tty_port.c 15 KB

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