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 init_waitqueue_head(&port->open_wait);
25 init_waitqueue_head(&port->close_wait);
26 init_waitqueue_head(&port->delta_msr_wait);
27 mutex_init(&port->mutex);
28 mutex_init(&port->buf_mutex);
29 spin_lock_init(&port->lock);
30 port->close_delay = (50 * HZ) / 100;
31 port->closing_wait = (3000 * HZ) / 100;
32 kref_init(&port->kref);
34 EXPORT_SYMBOL(tty_port_init);
36 struct device *tty_port_register_device(struct tty_port *port,
37 struct tty_driver *driver, unsigned index,
38 struct device *device)
40 driver->ports[index] = port;
41 return tty_register_device(driver, index, device);
43 EXPORT_SYMBOL_GPL(tty_port_register_device);
45 int tty_port_alloc_xmit_buf(struct tty_port *port)
47 /* We may sleep in get_zeroed_page() */
48 mutex_lock(&port->buf_mutex);
49 if (port->xmit_buf == NULL)
50 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
51 mutex_unlock(&port->buf_mutex);
52 if (port->xmit_buf == NULL)
56 EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
58 void tty_port_free_xmit_buf(struct tty_port *port)
60 mutex_lock(&port->buf_mutex);
61 if (port->xmit_buf != NULL) {
62 free_page((unsigned long)port->xmit_buf);
63 port->xmit_buf = NULL;
65 mutex_unlock(&port->buf_mutex);
67 EXPORT_SYMBOL(tty_port_free_xmit_buf);
69 static void tty_port_destructor(struct kref *kref)
71 struct tty_port *port = container_of(kref, struct tty_port, kref);
73 free_page((unsigned long)port->xmit_buf);
74 if (port->ops->destruct)
75 port->ops->destruct(port);
80 void tty_port_put(struct tty_port *port)
83 kref_put(&port->kref, tty_port_destructor);
85 EXPORT_SYMBOL(tty_port_put);
88 * tty_port_tty_get - get a tty reference
91 * Return a refcount protected tty instance or NULL if the port is not
92 * associated with a tty (eg due to close or hangup)
95 struct tty_struct *tty_port_tty_get(struct tty_port *port)
98 struct tty_struct *tty;
100 spin_lock_irqsave(&port->lock, flags);
101 tty = tty_kref_get(port->tty);
102 spin_unlock_irqrestore(&port->lock, flags);
105 EXPORT_SYMBOL(tty_port_tty_get);
108 * tty_port_tty_set - set the tty of a port
112 * Associate the port and tty pair. Manages any internal refcounts.
113 * Pass NULL to deassociate a port
116 void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
120 spin_lock_irqsave(&port->lock, flags);
122 tty_kref_put(port->tty);
123 port->tty = tty_kref_get(tty);
124 spin_unlock_irqrestore(&port->lock, flags);
126 EXPORT_SYMBOL(tty_port_tty_set);
128 static void tty_port_shutdown(struct tty_port *port)
130 mutex_lock(&port->mutex);
131 if (port->ops->shutdown && !port->console &&
132 test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags))
133 port->ops->shutdown(port);
134 mutex_unlock(&port->mutex);
138 * tty_port_hangup - hangup helper
141 * Perform port level tty hangup flag and count changes. Drop the tty
145 void tty_port_hangup(struct tty_port *port)
149 spin_lock_irqsave(&port->lock, flags);
151 port->flags &= ~ASYNC_NORMAL_ACTIVE;
153 set_bit(TTY_IO_ERROR, &port->tty->flags);
154 tty_kref_put(port->tty);
157 spin_unlock_irqrestore(&port->lock, flags);
158 wake_up_interruptible(&port->open_wait);
159 wake_up_interruptible(&port->delta_msr_wait);
160 tty_port_shutdown(port);
162 EXPORT_SYMBOL(tty_port_hangup);
165 * tty_port_carrier_raised - carrier raised check
168 * Wrapper for the carrier detect logic. For the moment this is used
169 * to hide some internal details. This will eventually become entirely
170 * internal to the tty port.
173 int tty_port_carrier_raised(struct tty_port *port)
175 if (port->ops->carrier_raised == NULL)
177 return port->ops->carrier_raised(port);
179 EXPORT_SYMBOL(tty_port_carrier_raised);
182 * tty_port_raise_dtr_rts - Raise DTR/RTS
185 * Wrapper for the DTR/RTS raise logic. For the moment this is used
186 * to hide some internal details. This will eventually become entirely
187 * internal to the tty port.
190 void tty_port_raise_dtr_rts(struct tty_port *port)
192 if (port->ops->dtr_rts)
193 port->ops->dtr_rts(port, 1);
195 EXPORT_SYMBOL(tty_port_raise_dtr_rts);
198 * tty_port_lower_dtr_rts - Lower DTR/RTS
201 * Wrapper for the DTR/RTS raise logic. For the moment this is used
202 * to hide some internal details. This will eventually become entirely
203 * internal to the tty port.
206 void tty_port_lower_dtr_rts(struct tty_port *port)
208 if (port->ops->dtr_rts)
209 port->ops->dtr_rts(port, 0);
211 EXPORT_SYMBOL(tty_port_lower_dtr_rts);
214 * tty_port_block_til_ready - Waiting logic for tty open
215 * @port: the tty port being opened
216 * @tty: the tty device being bound
217 * @filp: the file pointer of the opener
219 * Implement the core POSIX/SuS tty behaviour when opening a tty device.
221 * - hangup (both before and during)
222 * - non blocking open
225 * - port flags and counts
227 * The passed tty_port must implement the carrier_raised method if it can
228 * do carrier detect and the dtr_rts method if it supports software
229 * management of these lines. Note that the dtr/rts raise is done each
230 * iteration as a hangup may have previously dropped them while we wait.
233 int tty_port_block_til_ready(struct tty_port *port,
234 struct tty_struct *tty, struct file *filp)
236 int do_clocal = 0, retval;
240 /* block if port is in the process of being closed */
241 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
242 wait_event_interruptible_tty(port->close_wait,
243 !(port->flags & ASYNC_CLOSING));
244 if (port->flags & ASYNC_HUP_NOTIFY)
250 /* if non-blocking mode is set we can pass directly to open unless
251 the port has just hung up or is in another error state */
252 if (tty->flags & (1 << TTY_IO_ERROR)) {
253 port->flags |= ASYNC_NORMAL_ACTIVE;
256 if (filp->f_flags & O_NONBLOCK) {
257 /* Indicate we are open */
258 if (tty->termios->c_cflag & CBAUD)
259 tty_port_raise_dtr_rts(port);
260 port->flags |= ASYNC_NORMAL_ACTIVE;
267 /* Block waiting until we can proceed. We may need to wait for the
268 carrier, but we must also wait for any close that is in progress
269 before the next open may complete */
273 /* The port lock protects the port counts */
274 spin_lock_irqsave(&port->lock, flags);
275 if (!tty_hung_up_p(filp))
277 port->blocked_open++;
278 spin_unlock_irqrestore(&port->lock, flags);
281 /* Indicate we are open */
282 if (tty->termios->c_cflag & CBAUD)
283 tty_port_raise_dtr_rts(port);
285 prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
286 /* Check for a hangup or uninitialised port.
287 Return accordingly */
288 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
289 if (port->flags & ASYNC_HUP_NOTIFY)
292 retval = -ERESTARTSYS;
296 * Probe the carrier. For devices with no carrier detect
297 * tty_port_carrier_raised will always return true.
298 * Never ask drivers if CLOCAL is set, this causes troubles
301 if (!(port->flags & ASYNC_CLOSING) &&
302 (do_clocal || tty_port_carrier_raised(port)))
304 if (signal_pending(current)) {
305 retval = -ERESTARTSYS;
312 finish_wait(&port->open_wait, &wait);
314 /* Update counts. A parallel hangup will have set count to zero and
315 we must not mess that up further */
316 spin_lock_irqsave(&port->lock, flags);
317 if (!tty_hung_up_p(filp))
319 port->blocked_open--;
321 port->flags |= ASYNC_NORMAL_ACTIVE;
322 spin_unlock_irqrestore(&port->lock, flags);
325 EXPORT_SYMBOL(tty_port_block_til_ready);
327 int tty_port_close_start(struct tty_port *port,
328 struct tty_struct *tty, struct file *filp)
332 spin_lock_irqsave(&port->lock, flags);
333 if (tty_hung_up_p(filp)) {
334 spin_unlock_irqrestore(&port->lock, flags);
338 if (tty->count == 1 && port->count != 1) {
340 "tty_port_close_start: tty->count = 1 port count = %d.\n",
344 if (--port->count < 0) {
345 printk(KERN_WARNING "tty_port_close_start: count = %d\n",
351 spin_unlock_irqrestore(&port->lock, flags);
353 port->ops->drop(port);
356 set_bit(ASYNCB_CLOSING, &port->flags);
358 spin_unlock_irqrestore(&port->lock, flags);
359 /* Don't block on a stalled port, just pull the chain */
360 if (tty->flow_stopped)
361 tty_driver_flush_buffer(tty);
362 if (test_bit(ASYNCB_INITIALIZED, &port->flags) &&
363 port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
364 tty_wait_until_sent_from_close(tty, port->closing_wait);
365 if (port->drain_delay) {
366 unsigned int bps = tty_get_baud_rate(tty);
370 timeout = max_t(long,
371 (HZ * 10 * port->drain_delay) / bps, HZ / 10);
374 schedule_timeout_interruptible(timeout);
376 /* Flush the ldisc buffering */
377 tty_ldisc_flush(tty);
379 /* Drop DTR/RTS if HUPCL is set. This causes any attached modem to
381 if (tty->termios->c_cflag & HUPCL)
382 tty_port_lower_dtr_rts(port);
384 /* Don't call port->drop for the last reference. Callers will want
385 to drop the last active reference in ->shutdown() or the tty
389 EXPORT_SYMBOL(tty_port_close_start);
391 void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
395 spin_lock_irqsave(&port->lock, flags);
398 if (port->blocked_open) {
399 spin_unlock_irqrestore(&port->lock, flags);
400 if (port->close_delay) {
401 msleep_interruptible(
402 jiffies_to_msecs(port->close_delay));
404 spin_lock_irqsave(&port->lock, flags);
405 wake_up_interruptible(&port->open_wait);
407 port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
408 wake_up_interruptible(&port->close_wait);
409 spin_unlock_irqrestore(&port->lock, flags);
411 EXPORT_SYMBOL(tty_port_close_end);
413 void tty_port_close(struct tty_port *port, struct tty_struct *tty,
416 if (tty_port_close_start(port, tty, filp) == 0)
418 tty_port_shutdown(port);
419 set_bit(TTY_IO_ERROR, &tty->flags);
420 tty_port_close_end(port, tty);
421 tty_port_tty_set(port, NULL);
423 EXPORT_SYMBOL(tty_port_close);
425 int tty_port_install(struct tty_port *port, struct tty_driver *driver,
426 struct tty_struct *tty)
429 return tty_standard_install(driver, tty);
431 EXPORT_SYMBOL_GPL(tty_port_install);
433 int tty_port_open(struct tty_port *port, struct tty_struct *tty,
436 spin_lock_irq(&port->lock);
437 if (!tty_hung_up_p(filp))
439 spin_unlock_irq(&port->lock);
440 tty_port_tty_set(port, tty);
443 * Do the device-specific open only if the hardware isn't
444 * already initialized. Serialize open and shutdown using the
448 mutex_lock(&port->mutex);
450 if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) {
451 clear_bit(TTY_IO_ERROR, &tty->flags);
452 if (port->ops->activate) {
453 int retval = port->ops->activate(port, tty);
455 mutex_unlock(&port->mutex);
459 set_bit(ASYNCB_INITIALIZED, &port->flags);
461 mutex_unlock(&port->mutex);
462 return tty_port_block_til_ready(port, tty, filp);
465 EXPORT_SYMBOL(tty_port_open);