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 spin_lock_init(&port->lock);
29 port->close_delay = (50 * HZ) / 100;
30 port->closing_wait = (3000 * HZ) / 100;
32 EXPORT_SYMBOL(tty_port_init);
34 int tty_port_alloc_xmit_buf(struct tty_port *port)
36 /* We may sleep in get_zeroed_page() */
37 mutex_lock(&port->mutex);
38 if (port->xmit_buf == NULL)
39 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
40 mutex_unlock(&port->mutex);
41 if (port->xmit_buf == NULL)
45 EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
47 void tty_port_free_xmit_buf(struct tty_port *port)
49 mutex_lock(&port->mutex);
50 if (port->xmit_buf != NULL) {
51 free_page((unsigned long)port->xmit_buf);
52 port->xmit_buf = NULL;
54 mutex_unlock(&port->mutex);
56 EXPORT_SYMBOL(tty_port_free_xmit_buf);
60 * tty_port_tty_get - get a tty reference
63 * Return a refcount protected tty instance or NULL if the port is not
64 * associated with a tty (eg due to close or hangup)
67 struct tty_struct *tty_port_tty_get(struct tty_port *port)
70 struct tty_struct *tty;
72 spin_lock_irqsave(&port->lock, flags);
73 tty = tty_kref_get(port->tty);
74 spin_unlock_irqrestore(&port->lock, flags);
77 EXPORT_SYMBOL(tty_port_tty_get);
80 * tty_port_tty_set - set the tty of a port
84 * Associate the port and tty pair. Manages any internal refcounts.
85 * Pass NULL to deassociate a port
88 void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
92 spin_lock_irqsave(&port->lock, flags);
94 tty_kref_put(port->tty);
95 port->tty = tty_kref_get(tty);
96 spin_unlock_irqrestore(&port->lock, flags);
98 EXPORT_SYMBOL(tty_port_tty_set);
100 static void tty_port_shutdown(struct tty_port *port)
102 mutex_lock(&port->mutex);
103 if (port->ops->shutdown &&
104 test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags))
105 port->ops->shutdown(port);
106 mutex_unlock(&port->mutex);
110 * tty_port_hangup - hangup helper
113 * Perform port level tty hangup flag and count changes. Drop the tty
117 void tty_port_hangup(struct tty_port *port)
121 spin_lock_irqsave(&port->lock, flags);
123 port->flags &= ~ASYNC_NORMAL_ACTIVE;
125 tty_kref_put(port->tty);
127 spin_unlock_irqrestore(&port->lock, flags);
128 wake_up_interruptible(&port->open_wait);
129 wake_up_interruptible(&port->delta_msr_wait);
130 tty_port_shutdown(port);
132 EXPORT_SYMBOL(tty_port_hangup);
135 * tty_port_carrier_raised - carrier raised check
138 * Wrapper for the carrier detect logic. For the moment this is used
139 * to hide some internal details. This will eventually become entirely
140 * internal to the tty port.
143 int tty_port_carrier_raised(struct tty_port *port)
145 if (port->ops->carrier_raised == NULL)
147 return port->ops->carrier_raised(port);
149 EXPORT_SYMBOL(tty_port_carrier_raised);
152 * tty_port_raise_dtr_rts - Raise DTR/RTS
155 * Wrapper for the DTR/RTS raise logic. For the moment this is used
156 * to hide some internal details. This will eventually become entirely
157 * internal to the tty port.
160 void tty_port_raise_dtr_rts(struct tty_port *port)
162 if (port->ops->dtr_rts)
163 port->ops->dtr_rts(port, 1);
165 EXPORT_SYMBOL(tty_port_raise_dtr_rts);
168 * tty_port_lower_dtr_rts - Lower DTR/RTS
171 * Wrapper for the DTR/RTS raise logic. For the moment this is used
172 * to hide some internal details. This will eventually become entirely
173 * internal to the tty port.
176 void tty_port_lower_dtr_rts(struct tty_port *port)
178 if (port->ops->dtr_rts)
179 port->ops->dtr_rts(port, 0);
181 EXPORT_SYMBOL(tty_port_lower_dtr_rts);
184 * tty_port_block_til_ready - Waiting logic for tty open
185 * @port: the tty port being opened
186 * @tty: the tty device being bound
187 * @filp: the file pointer of the opener
189 * Implement the core POSIX/SuS tty behaviour when opening a tty device.
191 * - hangup (both before and during)
192 * - non blocking open
195 * - port flags and counts
197 * The passed tty_port must implement the carrier_raised method if it can
198 * do carrier detect and the dtr_rts method if it supports software
199 * management of these lines. Note that the dtr/rts raise is done each
200 * iteration as a hangup may have previously dropped them while we wait.
203 int tty_port_block_til_ready(struct tty_port *port,
204 struct tty_struct *tty, struct file *filp)
206 int do_clocal = 0, retval;
211 /* block if port is in the process of being closed */
212 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
213 wait_event_interruptible(port->close_wait,
214 !(port->flags & ASYNC_CLOSING));
215 if (port->flags & ASYNC_HUP_NOTIFY)
221 /* if non-blocking mode is set we can pass directly to open unless
222 the port has just hung up or is in another error state */
223 if (tty->flags & (1 << TTY_IO_ERROR)) {
224 port->flags |= ASYNC_NORMAL_ACTIVE;
227 if (filp->f_flags & O_NONBLOCK) {
228 /* Indicate we are open */
229 if (tty->termios->c_cflag & CBAUD)
230 tty_port_raise_dtr_rts(port);
231 port->flags |= ASYNC_NORMAL_ACTIVE;
238 /* Block waiting until we can proceed. We may need to wait for the
239 carrier, but we must also wait for any close that is in progress
240 before the next open may complete */
244 /* The port lock protects the port counts */
245 spin_lock_irqsave(&port->lock, flags);
246 if (!tty_hung_up_p(filp))
248 port->blocked_open++;
249 spin_unlock_irqrestore(&port->lock, flags);
252 /* Indicate we are open */
253 if (tty->termios->c_cflag & CBAUD)
254 tty_port_raise_dtr_rts(port);
256 prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
257 /* Check for a hangup or uninitialised port. Return accordingly */
258 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
259 if (port->flags & ASYNC_HUP_NOTIFY)
262 retval = -ERESTARTSYS;
265 /* Probe the carrier. For devices with no carrier detect this
266 will always return true */
267 cd = tty_port_carrier_raised(port);
268 if (!(port->flags & ASYNC_CLOSING) &&
271 if (signal_pending(current)) {
272 retval = -ERESTARTSYS;
277 finish_wait(&port->open_wait, &wait);
279 /* Update counts. A parallel hangup will have set count to zero and
280 we must not mess that up further */
281 spin_lock_irqsave(&port->lock, flags);
282 if (!tty_hung_up_p(filp))
284 port->blocked_open--;
286 port->flags |= ASYNC_NORMAL_ACTIVE;
287 spin_unlock_irqrestore(&port->lock, flags);
291 EXPORT_SYMBOL(tty_port_block_til_ready);
293 int tty_port_close_start(struct tty_port *port, struct tty_struct *tty, struct file *filp)
297 spin_lock_irqsave(&port->lock, flags);
298 if (tty_hung_up_p(filp)) {
299 spin_unlock_irqrestore(&port->lock, flags);
303 if( tty->count == 1 && port->count != 1) {
305 "tty_port_close_start: tty->count = 1 port count = %d.\n",
309 if (--port->count < 0) {
310 printk(KERN_WARNING "tty_port_close_start: count = %d\n",
316 spin_unlock_irqrestore(&port->lock, flags);
318 port->ops->drop(port);
321 set_bit(ASYNCB_CLOSING, &port->flags);
323 spin_unlock_irqrestore(&port->lock, flags);
324 /* Don't block on a stalled port, just pull the chain */
325 if (tty->flow_stopped)
326 tty_driver_flush_buffer(tty);
327 if (test_bit(ASYNCB_INITIALIZED, &port->flags) &&
328 port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
329 tty_wait_until_sent(tty, port->closing_wait);
330 if (port->drain_delay) {
331 unsigned int bps = tty_get_baud_rate(tty);
335 timeout = max_t(long, (HZ * 10 * port->drain_delay) / bps,
339 schedule_timeout_interruptible(timeout);
341 /* Don't call port->drop for the last reference. Callers will want
342 to drop the last active reference in ->shutdown() or the tty
346 EXPORT_SYMBOL(tty_port_close_start);
348 void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
352 tty_ldisc_flush(tty);
354 if (tty->termios->c_cflag & HUPCL)
355 tty_port_lower_dtr_rts(port);
357 spin_lock_irqsave(&port->lock, flags);
360 if (port->blocked_open) {
361 spin_unlock_irqrestore(&port->lock, flags);
362 if (port->close_delay) {
363 msleep_interruptible(
364 jiffies_to_msecs(port->close_delay));
366 spin_lock_irqsave(&port->lock, flags);
367 wake_up_interruptible(&port->open_wait);
369 port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
370 wake_up_interruptible(&port->close_wait);
371 spin_unlock_irqrestore(&port->lock, flags);
373 EXPORT_SYMBOL(tty_port_close_end);
375 void tty_port_close(struct tty_port *port, struct tty_struct *tty,
378 if (tty_port_close_start(port, tty, filp) == 0)
380 tty_port_shutdown(port);
381 tty_port_close_end(port, tty);
382 tty_port_tty_set(port, NULL);
384 EXPORT_SYMBOL(tty_port_close);
386 int tty_port_open(struct tty_port *port, struct tty_struct *tty,
389 spin_lock_irq(&port->lock);
390 if (!tty_hung_up_p(filp))
392 spin_unlock_irq(&port->lock);
393 tty_port_tty_set(port, tty);
396 * Do the device-specific open only if the hardware isn't
397 * already initialized. Serialize open and shutdown using the
401 mutex_lock(&port->mutex);
403 if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) {
404 if (port->ops->activate) {
405 int retval = port->ops->activate(port, tty);
407 mutex_unlock(&port->mutex);
411 set_bit(ASYNCB_INITIALIZED, &port->flags);
413 mutex_unlock(&port->mutex);
414 return tty_port_block_til_ready(port, tty, filp);
417 EXPORT_SYMBOL(tty_port_open);