5 #include <linux/types.h>
6 #include <linux/errno.h>
8 #include <linux/tty_driver.h>
9 #include <linux/tty_flip.h>
10 #include <linux/serial.h>
11 #include <linux/timer.h>
12 #include <linux/string.h>
13 #include <linux/slab.h>
14 #include <linux/sched.h>
15 #include <linux/init.h>
16 #include <linux/wait.h>
17 #include <linux/bitops.h>
18 #include <linux/delay.h>
19 #include <linux/module.h>
21 void tty_port_init(struct tty_port *port)
23 memset(port, 0, sizeof(*port));
24 tty_buffer_init(port);
25 init_waitqueue_head(&port->open_wait);
26 init_waitqueue_head(&port->close_wait);
27 init_waitqueue_head(&port->delta_msr_wait);
28 mutex_init(&port->mutex);
29 mutex_init(&port->buf_mutex);
30 spin_lock_init(&port->lock);
31 port->close_delay = (50 * HZ) / 100;
32 port->closing_wait = (3000 * HZ) / 100;
33 kref_init(&port->kref);
35 EXPORT_SYMBOL(tty_port_init);
38 * tty_port_link_device - link tty and tty_port
39 * @port: tty_port of the device
40 * @driver: tty_driver for this device
41 * @index: index of the tty
43 * Provide the tty layer wit ha link from a tty (specified by @index) to a
44 * tty_port (@port). Use this only if neither tty_port_register_device nor
45 * tty_port_install is used in the driver. If used, this has to be called before
46 * tty_register_driver.
48 void tty_port_link_device(struct tty_port *port,
49 struct tty_driver *driver, unsigned index)
51 if (WARN_ON(index >= driver->num))
53 driver->ports[index] = port;
55 EXPORT_SYMBOL_GPL(tty_port_link_device);
58 * tty_port_register_device - register tty device
59 * @port: tty_port of the device
60 * @driver: tty_driver for this device
61 * @index: index of the tty
62 * @device: parent if exists, otherwise NULL
64 * It is the same as tty_register_device except the provided @port is linked to
65 * a concrete tty specified by @index. Use this or tty_port_install (or both).
66 * Call tty_port_link_device as a last resort.
68 struct device *tty_port_register_device(struct tty_port *port,
69 struct tty_driver *driver, unsigned index,
70 struct device *device)
72 tty_port_link_device(port, driver, index);
73 return tty_register_device(driver, index, device);
75 EXPORT_SYMBOL_GPL(tty_port_register_device);
78 * tty_port_register_device_attr - register tty device
79 * @port: tty_port of the device
80 * @driver: tty_driver for this device
81 * @index: index of the tty
82 * @device: parent if exists, otherwise NULL
83 * @drvdata: Driver data to be set to device.
84 * @attr_grp: Attribute group to be set on device.
86 * It is the same as tty_register_device_attr except the provided @port is
87 * linked to a concrete tty specified by @index. Use this or tty_port_install
88 * (or both). Call tty_port_link_device as a last resort.
90 struct device *tty_port_register_device_attr(struct tty_port *port,
91 struct tty_driver *driver, unsigned index,
92 struct device *device, void *drvdata,
93 const struct attribute_group **attr_grp)
95 tty_port_link_device(port, driver, index);
96 return tty_register_device_attr(driver, index, device, drvdata,
99 EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
101 int tty_port_alloc_xmit_buf(struct tty_port *port)
103 /* We may sleep in get_zeroed_page() */
104 mutex_lock(&port->buf_mutex);
105 if (port->xmit_buf == NULL)
106 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
107 mutex_unlock(&port->buf_mutex);
108 if (port->xmit_buf == NULL)
112 EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
114 void tty_port_free_xmit_buf(struct tty_port *port)
116 mutex_lock(&port->buf_mutex);
117 if (port->xmit_buf != NULL) {
118 free_page((unsigned long)port->xmit_buf);
119 port->xmit_buf = NULL;
121 mutex_unlock(&port->buf_mutex);
123 EXPORT_SYMBOL(tty_port_free_xmit_buf);
126 * tty_port_destroy -- destroy inited port
127 * @port: tty port to be doestroyed
129 * When a port was initialized using tty_port_init, one has to destroy the
130 * port by this function. Either indirectly by using tty_port refcounting
131 * (tty_port_put) or directly if refcounting is not used.
133 void tty_port_destroy(struct tty_port *port)
135 tty_buffer_free_all(port);
137 EXPORT_SYMBOL(tty_port_destroy);
139 static void tty_port_destructor(struct kref *kref)
141 struct tty_port *port = container_of(kref, struct tty_port, kref);
143 free_page((unsigned long)port->xmit_buf);
144 tty_port_destroy(port);
145 if (port->ops && port->ops->destruct)
146 port->ops->destruct(port);
151 void tty_port_put(struct tty_port *port)
154 kref_put(&port->kref, tty_port_destructor);
156 EXPORT_SYMBOL(tty_port_put);
159 * tty_port_tty_get - get a tty reference
162 * Return a refcount protected tty instance or NULL if the port is not
163 * associated with a tty (eg due to close or hangup)
166 struct tty_struct *tty_port_tty_get(struct tty_port *port)
169 struct tty_struct *tty;
171 spin_lock_irqsave(&port->lock, flags);
172 tty = tty_kref_get(port->tty);
173 spin_unlock_irqrestore(&port->lock, flags);
176 EXPORT_SYMBOL(tty_port_tty_get);
179 * tty_port_tty_set - set the tty of a port
183 * Associate the port and tty pair. Manages any internal refcounts.
184 * Pass NULL to deassociate a port
187 void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
191 spin_lock_irqsave(&port->lock, flags);
193 tty_kref_put(port->tty);
194 port->tty = tty_kref_get(tty);
195 spin_unlock_irqrestore(&port->lock, flags);
197 EXPORT_SYMBOL(tty_port_tty_set);
199 static void tty_port_shutdown(struct tty_port *port)
201 mutex_lock(&port->mutex);
202 if (port->ops->shutdown && !port->console &&
203 test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags))
204 port->ops->shutdown(port);
205 mutex_unlock(&port->mutex);
209 * tty_port_hangup - hangup helper
212 * Perform port level tty hangup flag and count changes. Drop the tty
216 void tty_port_hangup(struct tty_port *port)
220 spin_lock_irqsave(&port->lock, flags);
222 port->flags &= ~ASYNC_NORMAL_ACTIVE;
224 set_bit(TTY_IO_ERROR, &port->tty->flags);
225 tty_kref_put(port->tty);
228 spin_unlock_irqrestore(&port->lock, flags);
229 wake_up_interruptible(&port->open_wait);
230 wake_up_interruptible(&port->delta_msr_wait);
231 tty_port_shutdown(port);
233 EXPORT_SYMBOL(tty_port_hangup);
236 * tty_port_tty_hangup - helper to hang up a tty
239 * @check_clocal: hang only ttys with CLOCAL unset?
241 void tty_port_tty_hangup(struct tty_port *port, bool check_clocal)
243 struct tty_struct *tty = tty_port_tty_get(port);
245 if (tty && (!check_clocal || !C_CLOCAL(tty))) {
250 EXPORT_SYMBOL_GPL(tty_port_tty_hangup);
253 * tty_port_tty_wakeup - helper to wake up a tty
257 void tty_port_tty_wakeup(struct tty_port *port)
259 struct tty_struct *tty = tty_port_tty_get(port);
266 EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
269 * tty_port_carrier_raised - carrier raised check
272 * Wrapper for the carrier detect logic. For the moment this is used
273 * to hide some internal details. This will eventually become entirely
274 * internal to the tty port.
277 int tty_port_carrier_raised(struct tty_port *port)
279 if (port->ops->carrier_raised == NULL)
281 return port->ops->carrier_raised(port);
283 EXPORT_SYMBOL(tty_port_carrier_raised);
286 * tty_port_raise_dtr_rts - Raise DTR/RTS
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.
294 void tty_port_raise_dtr_rts(struct tty_port *port)
296 if (port->ops->dtr_rts)
297 port->ops->dtr_rts(port, 1);
299 EXPORT_SYMBOL(tty_port_raise_dtr_rts);
302 * tty_port_lower_dtr_rts - Lower DTR/RTS
305 * Wrapper for the DTR/RTS raise logic. For the moment this is used
306 * to hide some internal details. This will eventually become entirely
307 * internal to the tty port.
310 void tty_port_lower_dtr_rts(struct tty_port *port)
312 if (port->ops->dtr_rts)
313 port->ops->dtr_rts(port, 0);
315 EXPORT_SYMBOL(tty_port_lower_dtr_rts);
318 * tty_port_block_til_ready - Waiting logic for tty open
319 * @port: the tty port being opened
320 * @tty: the tty device being bound
321 * @filp: the file pointer of the opener
323 * Implement the core POSIX/SuS tty behaviour when opening a tty device.
325 * - hangup (both before and during)
326 * - non blocking open
329 * - port flags and counts
331 * The passed tty_port must implement the carrier_raised method if it can
332 * do carrier detect and the dtr_rts method if it supports software
333 * management of these lines. Note that the dtr/rts raise is done each
334 * iteration as a hangup may have previously dropped them while we wait.
337 int tty_port_block_til_ready(struct tty_port *port,
338 struct tty_struct *tty, struct file *filp)
340 int do_clocal = 0, retval;
344 /* block if port is in the process of being closed */
345 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
346 wait_event_interruptible_tty(tty, port->close_wait,
347 !(port->flags & ASYNC_CLOSING));
348 if (port->flags & ASYNC_HUP_NOTIFY)
354 /* if non-blocking mode is set we can pass directly to open unless
355 the port has just hung up or is in another error state */
356 if (tty->flags & (1 << TTY_IO_ERROR)) {
357 port->flags |= ASYNC_NORMAL_ACTIVE;
360 if (filp->f_flags & O_NONBLOCK) {
361 /* Indicate we are open */
362 if (tty->termios.c_cflag & CBAUD)
363 tty_port_raise_dtr_rts(port);
364 port->flags |= ASYNC_NORMAL_ACTIVE;
371 /* Block waiting until we can proceed. We may need to wait for the
372 carrier, but we must also wait for any close that is in progress
373 before the next open may complete */
377 /* The port lock protects the port counts */
378 spin_lock_irqsave(&port->lock, flags);
379 if (!tty_hung_up_p(filp))
381 port->blocked_open++;
382 spin_unlock_irqrestore(&port->lock, flags);
385 /* Indicate we are open */
386 if (tty->termios.c_cflag & CBAUD)
387 tty_port_raise_dtr_rts(port);
389 prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
390 /* Check for a hangup or uninitialised port.
391 Return accordingly */
392 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
393 if (port->flags & ASYNC_HUP_NOTIFY)
396 retval = -ERESTARTSYS;
400 * Probe the carrier. For devices with no carrier detect
401 * tty_port_carrier_raised will always return true.
402 * Never ask drivers if CLOCAL is set, this causes troubles
405 if (!(port->flags & ASYNC_CLOSING) &&
406 (do_clocal || tty_port_carrier_raised(port)))
408 if (signal_pending(current)) {
409 retval = -ERESTARTSYS;
416 finish_wait(&port->open_wait, &wait);
418 /* Update counts. A parallel hangup will have set count to zero and
419 we must not mess that up further */
420 spin_lock_irqsave(&port->lock, flags);
421 if (!tty_hung_up_p(filp))
423 port->blocked_open--;
425 port->flags |= ASYNC_NORMAL_ACTIVE;
426 spin_unlock_irqrestore(&port->lock, flags);
429 EXPORT_SYMBOL(tty_port_block_til_ready);
431 int tty_port_close_start(struct tty_port *port,
432 struct tty_struct *tty, struct file *filp)
436 spin_lock_irqsave(&port->lock, flags);
437 if (tty_hung_up_p(filp)) {
438 spin_unlock_irqrestore(&port->lock, flags);
442 if (tty->count == 1 && port->count != 1) {
444 "tty_port_close_start: tty->count = 1 port count = %d.\n",
448 if (--port->count < 0) {
449 printk(KERN_WARNING "tty_port_close_start: count = %d\n",
455 spin_unlock_irqrestore(&port->lock, flags);
457 port->ops->drop(port);
460 set_bit(ASYNCB_CLOSING, &port->flags);
462 spin_unlock_irqrestore(&port->lock, flags);
463 /* Don't block on a stalled port, just pull the chain */
464 if (tty->flow_stopped)
465 tty_driver_flush_buffer(tty);
466 if (test_bit(ASYNCB_INITIALIZED, &port->flags) &&
467 port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
468 tty_wait_until_sent_from_close(tty, port->closing_wait);
469 if (port->drain_delay) {
470 unsigned int bps = tty_get_baud_rate(tty);
474 timeout = max_t(long,
475 (HZ * 10 * port->drain_delay) / bps, HZ / 10);
478 schedule_timeout_interruptible(timeout);
480 /* Flush the ldisc buffering */
481 tty_ldisc_flush(tty);
483 /* Drop DTR/RTS if HUPCL is set. This causes any attached modem to
485 if (tty->termios.c_cflag & HUPCL)
486 tty_port_lower_dtr_rts(port);
488 /* Don't call port->drop for the last reference. Callers will want
489 to drop the last active reference in ->shutdown() or the tty
493 EXPORT_SYMBOL(tty_port_close_start);
495 void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
499 spin_lock_irqsave(&port->lock, flags);
502 if (port->blocked_open) {
503 spin_unlock_irqrestore(&port->lock, flags);
504 if (port->close_delay) {
505 msleep_interruptible(
506 jiffies_to_msecs(port->close_delay));
508 spin_lock_irqsave(&port->lock, flags);
509 wake_up_interruptible(&port->open_wait);
511 port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
512 wake_up_interruptible(&port->close_wait);
513 spin_unlock_irqrestore(&port->lock, flags);
515 EXPORT_SYMBOL(tty_port_close_end);
517 void tty_port_close(struct tty_port *port, struct tty_struct *tty,
520 if (tty_port_close_start(port, tty, filp) == 0)
522 tty_port_shutdown(port);
523 set_bit(TTY_IO_ERROR, &tty->flags);
524 tty_port_close_end(port, tty);
525 tty_port_tty_set(port, NULL);
527 EXPORT_SYMBOL(tty_port_close);
530 * tty_port_install - generic tty->ops->install handler
531 * @port: tty_port of the device
532 * @driver: tty_driver for this device
533 * @tty: tty to be installed
535 * It is the same as tty_standard_install except the provided @port is linked
536 * to a concrete tty specified by @tty. Use this or tty_port_register_device
537 * (or both). Call tty_port_link_device as a last resort.
539 int tty_port_install(struct tty_port *port, struct tty_driver *driver,
540 struct tty_struct *tty)
543 return tty_standard_install(driver, tty);
545 EXPORT_SYMBOL_GPL(tty_port_install);
547 int tty_port_open(struct tty_port *port, struct tty_struct *tty,
550 spin_lock_irq(&port->lock);
551 if (!tty_hung_up_p(filp))
553 spin_unlock_irq(&port->lock);
554 tty_port_tty_set(port, tty);
557 * Do the device-specific open only if the hardware isn't
558 * already initialized. Serialize open and shutdown using the
562 mutex_lock(&port->mutex);
564 if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) {
565 clear_bit(TTY_IO_ERROR, &tty->flags);
566 if (port->ops->activate) {
567 int retval = port->ops->activate(port, tty);
569 mutex_unlock(&port->mutex);
573 set_bit(ASYNCB_INITIALIZED, &port->flags);
575 mutex_unlock(&port->mutex);
576 return tty_port_block_til_ready(port, tty, filp);
579 EXPORT_SYMBOL(tty_port_open);