tty_port.c 14 KB

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