tty_port.c 15 KB

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