tty_port.c 15 KB

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