tty_port.c 15 KB

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