tty_port.c 11.5 KB
Newer Older
A
Alan Cox 已提交
1 2 3 4 5 6 7 8 9
/*
 * Tty port functions
 */

#include <linux/types.h>
#include <linux/errno.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
10
#include <linux/serial.h>
A
Alan Cox 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include <linux/timer.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/wait.h>
#include <linux/bitops.h>
#include <linux/delay.h>
#include <linux/module.h>

void tty_port_init(struct tty_port *port)
{
	memset(port, 0, sizeof(*port));
	init_waitqueue_head(&port->open_wait);
	init_waitqueue_head(&port->close_wait);
26
	init_waitqueue_head(&port->delta_msr_wait);
A
Alan Cox 已提交
27
	mutex_init(&port->mutex);
28
	mutex_init(&port->buf_mutex);
A
Alan Cox 已提交
29
	spin_lock_init(&port->lock);
A
Alan Cox 已提交
30 31
	port->close_delay = (50 * HZ) / 100;
	port->closing_wait = (3000 * HZ) / 100;
32
	kref_init(&port->kref);
A
Alan Cox 已提交
33 34 35 36 37 38
}
EXPORT_SYMBOL(tty_port_init);

int tty_port_alloc_xmit_buf(struct tty_port *port)
{
	/* We may sleep in get_zeroed_page() */
39
	mutex_lock(&port->buf_mutex);
A
Alan Cox 已提交
40 41
	if (port->xmit_buf == NULL)
		port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
42
	mutex_unlock(&port->buf_mutex);
A
Alan Cox 已提交
43 44 45 46 47 48 49 50
	if (port->xmit_buf == NULL)
		return -ENOMEM;
	return 0;
}
EXPORT_SYMBOL(tty_port_alloc_xmit_buf);

void tty_port_free_xmit_buf(struct tty_port *port)
{
51
	mutex_lock(&port->buf_mutex);
A
Alan Cox 已提交
52 53 54 55
	if (port->xmit_buf != NULL) {
		free_page((unsigned long)port->xmit_buf);
		port->xmit_buf = NULL;
	}
56
	mutex_unlock(&port->buf_mutex);
A
Alan Cox 已提交
57 58 59
}
EXPORT_SYMBOL(tty_port_free_xmit_buf);

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
static void tty_port_destructor(struct kref *kref)
{
	struct tty_port *port = container_of(kref, struct tty_port, kref);
	if (port->xmit_buf)
		free_page((unsigned long)port->xmit_buf);
	if (port->ops->destruct)
		port->ops->destruct(port);
	else
		kfree(port);
}

void tty_port_put(struct tty_port *port)
{
	if (port)
		kref_put(&port->kref, tty_port_destructor);
}
EXPORT_SYMBOL(tty_port_put);
A
Alan Cox 已提交
77

A
Alan Cox 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
/**
 *	tty_port_tty_get	-	get a tty reference
 *	@port: tty port
 *
 *	Return a refcount protected tty instance or NULL if the port is not
 *	associated with a tty (eg due to close or hangup)
 */

struct tty_struct *tty_port_tty_get(struct tty_port *port)
{
	unsigned long flags;
	struct tty_struct *tty;

	spin_lock_irqsave(&port->lock, flags);
	tty = tty_kref_get(port->tty);
	spin_unlock_irqrestore(&port->lock, flags);
	return tty;
}
EXPORT_SYMBOL(tty_port_tty_get);

/**
 *	tty_port_tty_set	-	set the tty of a port
 *	@port: tty port
 *	@tty: the tty
 *
 *	Associate the port and tty pair. Manages any internal refcounts.
 *	Pass NULL to deassociate a port
 */

void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
{
	unsigned long flags;

	spin_lock_irqsave(&port->lock, flags);
	if (port->tty)
		tty_kref_put(port->tty);
A
Alan Cox 已提交
114
	port->tty = tty_kref_get(tty);
A
Alan Cox 已提交
115 116 117
	spin_unlock_irqrestore(&port->lock, flags);
}
EXPORT_SYMBOL(tty_port_tty_set);
118

A
Alan Cox 已提交
119 120
static void tty_port_shutdown(struct tty_port *port)
{
A
Alan Cox 已提交
121
	mutex_lock(&port->mutex);
122
	if (port->ops->shutdown && !port->console &&
A
Alan Stern 已提交
123
		test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags))
A
Alan Cox 已提交
124
			port->ops->shutdown(port);
A
Alan Cox 已提交
125
	mutex_unlock(&port->mutex);
A
Alan Cox 已提交
126 127
}

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
/**
 *	tty_port_hangup		-	hangup helper
 *	@port: tty port
 *
 *	Perform port level tty hangup flag and count changes. Drop the tty
 *	reference.
 */

void tty_port_hangup(struct tty_port *port)
{
	unsigned long flags;

	spin_lock_irqsave(&port->lock, flags);
	port->count = 0;
	port->flags &= ~ASYNC_NORMAL_ACTIVE;
143 144
	if (port->tty) {
		set_bit(TTY_IO_ERROR, &port->tty->flags);
145
		tty_kref_put(port->tty);
146
	}
147 148 149
	port->tty = NULL;
	spin_unlock_irqrestore(&port->lock, flags);
	wake_up_interruptible(&port->open_wait);
150
	wake_up_interruptible(&port->delta_msr_wait);
A
Alan Cox 已提交
151
	tty_port_shutdown(port);
152 153 154
}
EXPORT_SYMBOL(tty_port_hangup);

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
/**
 *	tty_port_carrier_raised	-	carrier raised check
 *	@port: tty port
 *
 *	Wrapper for the carrier detect logic. For the moment this is used
 *	to hide some internal details. This will eventually become entirely
 *	internal to the tty port.
 */

int tty_port_carrier_raised(struct tty_port *port)
{
	if (port->ops->carrier_raised == NULL)
		return 1;
	return port->ops->carrier_raised(port);
}
EXPORT_SYMBOL(tty_port_carrier_raised);
A
Alan Cox 已提交
171 172

/**
173
 *	tty_port_raise_dtr_rts	-	Raise DTR/RTS
A
Alan Cox 已提交
174 175 176 177 178 179 180 181 182
 *	@port: tty port
 *
 *	Wrapper for the DTR/RTS raise logic. For the moment this is used
 *	to hide some internal details. This will eventually become entirely
 *	internal to the tty port.
 */

void tty_port_raise_dtr_rts(struct tty_port *port)
{
183 184
	if (port->ops->dtr_rts)
		port->ops->dtr_rts(port, 1);
A
Alan Cox 已提交
185 186
}
EXPORT_SYMBOL(tty_port_raise_dtr_rts);
187

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
/**
 *	tty_port_lower_dtr_rts	-	Lower DTR/RTS
 *	@port: tty port
 *
 *	Wrapper for the DTR/RTS raise logic. For the moment this is used
 *	to hide some internal details. This will eventually become entirely
 *	internal to the tty port.
 */

void tty_port_lower_dtr_rts(struct tty_port *port)
{
	if (port->ops->dtr_rts)
		port->ops->dtr_rts(port, 0);
}
EXPORT_SYMBOL(tty_port_lower_dtr_rts);

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
/**
 *	tty_port_block_til_ready	-	Waiting logic for tty open
 *	@port: the tty port being opened
 *	@tty: the tty device being bound
 *	@filp: the file pointer of the opener
 *
 *	Implement the core POSIX/SuS tty behaviour when opening a tty device.
 *	Handles:
 *		- hangup (both before and during)
 *		- non blocking open
 *		- rts/dtr/dcd
 *		- signals
 *		- port flags and counts
 *
 *	The passed tty_port must implement the carrier_raised method if it can
219
 *	do carrier detect and the dtr_rts method if it supports software
220 221 222
 *	management of these lines. Note that the dtr/rts raise is done each
 *	iteration as a hangup may have previously dropped them while we wait.
 */
A
Alan Cox 已提交
223

224 225 226 227 228
int tty_port_block_til_ready(struct tty_port *port,
				struct tty_struct *tty, struct file *filp)
{
	int do_clocal = 0, retval;
	unsigned long flags;
229
	DEFINE_WAIT(wait);
230 231 232

	/* block if port is in the process of being closed */
	if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
233
		wait_event_interruptible_tty(port->close_wait,
J
Jiri Slaby 已提交
234
				!(port->flags & ASYNC_CLOSING));
235 236 237 238 239 240 241 242
		if (port->flags & ASYNC_HUP_NOTIFY)
			return -EAGAIN;
		else
			return -ERESTARTSYS;
	}

	/* if non-blocking mode is set we can pass directly to open unless
	   the port has just hung up or is in another error state */
243 244 245 246 247
	if (tty->flags & (1 << TTY_IO_ERROR)) {
		port->flags |= ASYNC_NORMAL_ACTIVE;
		return 0;
	}
	if (filp->f_flags & O_NONBLOCK) {
248 249 250
		/* Indicate we are open */
		if (tty->termios->c_cflag & CBAUD)
			tty_port_raise_dtr_rts(port);
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
		port->flags |= ASYNC_NORMAL_ACTIVE;
		return 0;
	}

	if (C_CLOCAL(tty))
		do_clocal = 1;

	/* Block waiting until we can proceed. We may need to wait for the
	   carrier, but we must also wait for any close that is in progress
	   before the next open may complete */

	retval = 0;

	/* The port lock protects the port counts */
	spin_lock_irqsave(&port->lock, flags);
	if (!tty_hung_up_p(filp))
		port->count--;
	port->blocked_open++;
	spin_unlock_irqrestore(&port->lock, flags);

	while (1) {
		/* Indicate we are open */
A
Alan Cox 已提交
273 274
		if (tty->termios->c_cflag & CBAUD)
			tty_port_raise_dtr_rts(port);
275

J
Jiri Slaby 已提交
276
		prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
A
Alan Cox 已提交
277 278
		/* Check for a hangup or uninitialised port.
							Return accordingly */
279 280 281 282 283 284 285
		if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
			if (port->flags & ASYNC_HUP_NOTIFY)
				retval = -EAGAIN;
			else
				retval = -ERESTARTSYS;
			break;
		}
286 287 288 289 290 291
		/*
		 * Probe the carrier. For devices with no carrier detect
		 * tty_port_carrier_raised will always return true.
		 * Never ask drivers if CLOCAL is set, this causes troubles
		 * on some hardware.
		 */
292
		if (!(port->flags & ASYNC_CLOSING) &&
293
				(do_clocal || tty_port_carrier_raised(port)))
294 295 296 297 298
			break;
		if (signal_pending(current)) {
			retval = -ERESTARTSYS;
			break;
		}
299
		tty_unlock();
300
		schedule();
301
		tty_lock();
302
	}
J
Jiri Slaby 已提交
303
	finish_wait(&port->open_wait, &wait);
304 305 306 307 308 309 310 311 312 313

	/* Update counts. A parallel hangup will have set count to zero and
	   we must not mess that up further */
	spin_lock_irqsave(&port->lock, flags);
	if (!tty_hung_up_p(filp))
		port->count++;
	port->blocked_open--;
	if (retval == 0)
		port->flags |= ASYNC_NORMAL_ACTIVE;
	spin_unlock_irqrestore(&port->lock, flags);
314
	return retval;
315 316 317
}
EXPORT_SYMBOL(tty_port_block_til_ready);

A
Alan Cox 已提交
318 319
int tty_port_close_start(struct tty_port *port,
				struct tty_struct *tty, struct file *filp)
320 321 322 323 324 325 326 327 328
{
	unsigned long flags;

	spin_lock_irqsave(&port->lock, flags);
	if (tty_hung_up_p(filp)) {
		spin_unlock_irqrestore(&port->lock, flags);
		return 0;
	}

A
Alan Cox 已提交
329
	if (tty->count == 1 && port->count != 1) {
330 331 332 333 334 335 336 337 338 339 340 341 342
		printk(KERN_WARNING
		    "tty_port_close_start: tty->count = 1 port count = %d.\n",
								port->count);
		port->count = 1;
	}
	if (--port->count < 0) {
		printk(KERN_WARNING "tty_port_close_start: count = %d\n",
								port->count);
		port->count = 0;
	}

	if (port->count) {
		spin_unlock_irqrestore(&port->lock, flags);
A
Alan Cox 已提交
343 344
		if (port->ops->drop)
			port->ops->drop(port);
345 346
		return 0;
	}
A
Alan Stern 已提交
347
	set_bit(ASYNCB_CLOSING, &port->flags);
348 349
	tty->closing = 1;
	spin_unlock_irqrestore(&port->lock, flags);
350 351 352
	/* Don't block on a stalled port, just pull the chain */
	if (tty->flow_stopped)
		tty_driver_flush_buffer(tty);
A
Alan Cox 已提交
353
	if (test_bit(ASYNCB_INITIALIZED, &port->flags) &&
A
Alan Cox 已提交
354
			port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
355
		tty_wait_until_sent_from_close(tty, port->closing_wait);
356 357 358 359 360
	if (port->drain_delay) {
		unsigned int bps = tty_get_baud_rate(tty);
		long timeout;

		if (bps > 1200)
A
Alan Cox 已提交
361 362
			timeout = max_t(long,
				(HZ * 10 * port->drain_delay) / bps, HZ / 10);
363 364 365 366
		else
			timeout = 2 * HZ;
		schedule_timeout_interruptible(timeout);
	}
A
Alan Cox 已提交
367 368 369 370 371 372 373 374
	/* Flush the ldisc buffering */
	tty_ldisc_flush(tty);

	/* Drop DTR/RTS if HUPCL is set. This causes any attached modem to
	   hang up the line */
	if (tty->termios->c_cflag & HUPCL)
		tty_port_lower_dtr_rts(port);

A
Alan Cox 已提交
375 376 377
	/* Don't call port->drop for the last reference. Callers will want
	   to drop the last active reference in ->shutdown() or the tty
	   shutdown path */
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
	return 1;
}
EXPORT_SYMBOL(tty_port_close_start);

void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
{
	unsigned long flags;

	spin_lock_irqsave(&port->lock, flags);
	tty->closing = 0;

	if (port->blocked_open) {
		spin_unlock_irqrestore(&port->lock, flags);
		if (port->close_delay) {
			msleep_interruptible(
				jiffies_to_msecs(port->close_delay));
		}
		spin_lock_irqsave(&port->lock, flags);
		wake_up_interruptible(&port->open_wait);
	}
	port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
	wake_up_interruptible(&port->close_wait);
	spin_unlock_irqrestore(&port->lock, flags);
}
EXPORT_SYMBOL(tty_port_close_end);
A
Alan Cox 已提交
403 404 405 406 407 408 409

void tty_port_close(struct tty_port *port, struct tty_struct *tty,
							struct file *filp)
{
	if (tty_port_close_start(port, tty, filp) == 0)
		return;
	tty_port_shutdown(port);
410
	set_bit(TTY_IO_ERROR, &tty->flags);
A
Alan Cox 已提交
411 412 413 414
	tty_port_close_end(port, tty);
	tty_port_tty_set(port, NULL);
}
EXPORT_SYMBOL(tty_port_close);
A
Alan Cox 已提交
415

416 417 418 419 420 421 422 423
int tty_port_install(struct tty_port *port, struct tty_driver *driver,
		struct tty_struct *tty)
{
	tty->port = port;
	return tty_standard_install(driver, tty);
}
EXPORT_SYMBOL_GPL(tty_port_install);

A
Alan Cox 已提交
424
int tty_port_open(struct tty_port *port, struct tty_struct *tty,
A
Alan Cox 已提交
425
							struct file *filp)
A
Alan Cox 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
{
	spin_lock_irq(&port->lock);
	if (!tty_hung_up_p(filp))
		++port->count;
	spin_unlock_irq(&port->lock);
	tty_port_tty_set(port, tty);

	/*
	 * Do the device-specific open only if the hardware isn't
	 * already initialized. Serialize open and shutdown using the
	 * port mutex.
	 */

	mutex_lock(&port->mutex);

	if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) {
A
Alan Cox 已提交
442
		clear_bit(TTY_IO_ERROR, &tty->flags);
A
Alan Cox 已提交
443 444 445
		if (port->ops->activate) {
			int retval = port->ops->activate(port, tty);
			if (retval) {
A
Alan Cox 已提交
446 447 448 449
				mutex_unlock(&port->mutex);
				return retval;
			}
		}
A
Alan Cox 已提交
450 451 452 453 454 455 456
		set_bit(ASYNCB_INITIALIZED, &port->flags);
	}
	mutex_unlock(&port->mutex);
	return tty_port_block_til_ready(port, tty, filp);
}

EXPORT_SYMBOL(tty_port_open);