TTY: quatech2, remove unneeded is_open
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / tty / tty_port.c
CommitLineData
9e48565d
AC
1/*
2 * Tty port functions
3 */
4
5#include <linux/types.h>
6#include <linux/errno.h>
7#include <linux/tty.h>
8#include <linux/tty_driver.h>
9#include <linux/tty_flip.h>
3e61696b 10#include <linux/serial.h>
9e48565d
AC
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>
20
21void tty_port_init(struct tty_port *port)
22{
23 memset(port, 0, sizeof(*port));
ecbbfd44 24 tty_buffer_init(port);
9e48565d
AC
25 init_waitqueue_head(&port->open_wait);
26 init_waitqueue_head(&port->close_wait);
bdc04e31 27 init_waitqueue_head(&port->delta_msr_wait);
9e48565d 28 mutex_init(&port->mutex);
44e4909e 29 mutex_init(&port->buf_mutex);
4a90f09b 30 spin_lock_init(&port->lock);
9e48565d
AC
31 port->close_delay = (50 * HZ) / 100;
32 port->closing_wait = (3000 * HZ) / 100;
568aafc6 33 kref_init(&port->kref);
9e48565d
AC
34}
35EXPORT_SYMBOL(tty_port_init);
36
2cb4ca02
JS
37/**
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
42 *
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.
47 */
48void tty_port_link_device(struct tty_port *port,
49 struct tty_driver *driver, unsigned index)
50{
51 if (WARN_ON(index >= driver->num))
52 return;
53 driver->ports[index] = port;
54}
55EXPORT_SYMBOL_GPL(tty_port_link_device);
56
72a33bf5
JS
57/**
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
63 *
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.
67 */
057eb856
JS
68struct device *tty_port_register_device(struct tty_port *port,
69 struct tty_driver *driver, unsigned index,
70 struct device *device)
71{
2cb4ca02 72 tty_port_link_device(port, driver, index);
057eb856
JS
73 return tty_register_device(driver, index, device);
74}
75EXPORT_SYMBOL_GPL(tty_port_register_device);
76
b1b79916
TH
77/**
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.
85 *
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.
89 */
90struct 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)
94{
95 tty_port_link_device(port, driver, index);
96 return tty_register_device_attr(driver, index, device, drvdata,
97 attr_grp);
98}
99EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
100
9e48565d
AC
101int tty_port_alloc_xmit_buf(struct tty_port *port)
102{
103 /* We may sleep in get_zeroed_page() */
44e4909e 104 mutex_lock(&port->buf_mutex);
9e48565d
AC
105 if (port->xmit_buf == NULL)
106 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
44e4909e 107 mutex_unlock(&port->buf_mutex);
9e48565d
AC
108 if (port->xmit_buf == NULL)
109 return -ENOMEM;
110 return 0;
111}
112EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
113
114void tty_port_free_xmit_buf(struct tty_port *port)
115{
44e4909e 116 mutex_lock(&port->buf_mutex);
9e48565d
AC
117 if (port->xmit_buf != NULL) {
118 free_page((unsigned long)port->xmit_buf);
119 port->xmit_buf = NULL;
120 }
44e4909e 121 mutex_unlock(&port->buf_mutex);
9e48565d
AC
122}
123EXPORT_SYMBOL(tty_port_free_xmit_buf);
124
de274bfe
JS
125/**
126 * tty_port_destroy -- destroy inited port
127 * @port: tty port to be doestroyed
128 *
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.
132 */
133void tty_port_destroy(struct tty_port *port)
134{
135 tty_buffer_free_all(port);
136}
137EXPORT_SYMBOL(tty_port_destroy);
138
568aafc6
AC
139static void tty_port_destructor(struct kref *kref)
140{
141 struct tty_port *port = container_of(kref, struct tty_port, kref);
142 if (port->xmit_buf)
143 free_page((unsigned long)port->xmit_buf);
de274bfe 144 tty_port_destroy(port);
81c79838 145 if (port->ops && port->ops->destruct)
568aafc6
AC
146 port->ops->destruct(port);
147 else
148 kfree(port);
149}
150
151void tty_port_put(struct tty_port *port)
152{
153 if (port)
154 kref_put(&port->kref, tty_port_destructor);
155}
156EXPORT_SYMBOL(tty_port_put);
9e48565d 157
4a90f09b
AC
158/**
159 * tty_port_tty_get - get a tty reference
160 * @port: tty port
161 *
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)
164 */
165
166struct tty_struct *tty_port_tty_get(struct tty_port *port)
167{
168 unsigned long flags;
169 struct tty_struct *tty;
170
171 spin_lock_irqsave(&port->lock, flags);
172 tty = tty_kref_get(port->tty);
173 spin_unlock_irqrestore(&port->lock, flags);
174 return tty;
175}
176EXPORT_SYMBOL(tty_port_tty_get);
177
178/**
179 * tty_port_tty_set - set the tty of a port
180 * @port: tty port
181 * @tty: the tty
182 *
183 * Associate the port and tty pair. Manages any internal refcounts.
184 * Pass NULL to deassociate a port
185 */
186
187void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
188{
189 unsigned long flags;
190
191 spin_lock_irqsave(&port->lock, flags);
192 if (port->tty)
193 tty_kref_put(port->tty);
cb4bca35 194 port->tty = tty_kref_get(tty);
4a90f09b
AC
195 spin_unlock_irqrestore(&port->lock, flags);
196}
197EXPORT_SYMBOL(tty_port_tty_set);
31f35939 198
7ca0ff9a
AC
199static void tty_port_shutdown(struct tty_port *port)
200{
64bc3979 201 mutex_lock(&port->mutex);
336cee42 202 if (port->ops->shutdown && !port->console &&
1f5c13fa 203 test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags))
7ca0ff9a 204 port->ops->shutdown(port);
64bc3979 205 mutex_unlock(&port->mutex);
7ca0ff9a
AC
206}
207
3e61696b
AC
208/**
209 * tty_port_hangup - hangup helper
210 * @port: tty port
211 *
212 * Perform port level tty hangup flag and count changes. Drop the tty
213 * reference.
214 */
215
216void tty_port_hangup(struct tty_port *port)
217{
218 unsigned long flags;
219
220 spin_lock_irqsave(&port->lock, flags);
221 port->count = 0;
222 port->flags &= ~ASYNC_NORMAL_ACTIVE;
d74e8286
AC
223 if (port->tty) {
224 set_bit(TTY_IO_ERROR, &port->tty->flags);
3e61696b 225 tty_kref_put(port->tty);
d74e8286 226 }
3e61696b
AC
227 port->tty = NULL;
228 spin_unlock_irqrestore(&port->lock, flags);
229 wake_up_interruptible(&port->open_wait);
bdc04e31 230 wake_up_interruptible(&port->delta_msr_wait);
7ca0ff9a 231 tty_port_shutdown(port);
3e61696b
AC
232}
233EXPORT_SYMBOL(tty_port_hangup);
234
6aad04f2
JS
235/**
236 * tty_port_tty_wakeup - helper to wake up a tty
237 *
238 * @port: tty port
239 */
240void tty_port_tty_wakeup(struct tty_port *port)
241{
242 struct tty_struct *tty = tty_port_tty_get(port);
243
244 if (tty) {
245 tty_wakeup(tty);
246 tty_kref_put(tty);
247 }
248}
249EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
250
31f35939
AC
251/**
252 * tty_port_carrier_raised - carrier raised check
253 * @port: tty port
254 *
255 * Wrapper for the carrier detect logic. For the moment this is used
256 * to hide some internal details. This will eventually become entirely
257 * internal to the tty port.
258 */
259
260int tty_port_carrier_raised(struct tty_port *port)
261{
262 if (port->ops->carrier_raised == NULL)
263 return 1;
264 return port->ops->carrier_raised(port);
265}
266EXPORT_SYMBOL(tty_port_carrier_raised);
5d951fb4
AC
267
268/**
fcc8ac18 269 * tty_port_raise_dtr_rts - Raise DTR/RTS
5d951fb4
AC
270 * @port: tty port
271 *
272 * Wrapper for the DTR/RTS raise logic. For the moment this is used
273 * to hide some internal details. This will eventually become entirely
274 * internal to the tty port.
275 */
276
277void tty_port_raise_dtr_rts(struct tty_port *port)
278{
fcc8ac18
AC
279 if (port->ops->dtr_rts)
280 port->ops->dtr_rts(port, 1);
5d951fb4
AC
281}
282EXPORT_SYMBOL(tty_port_raise_dtr_rts);
36c621d8 283
fcc8ac18
AC
284/**
285 * tty_port_lower_dtr_rts - Lower DTR/RTS
286 * @port: tty port
287 *
288 * Wrapper for the DTR/RTS raise logic. For the moment this is used
289 * to hide some internal details. This will eventually become entirely
290 * internal to the tty port.
291 */
292
293void tty_port_lower_dtr_rts(struct tty_port *port)
294{
295 if (port->ops->dtr_rts)
296 port->ops->dtr_rts(port, 0);
297}
298EXPORT_SYMBOL(tty_port_lower_dtr_rts);
299
36c621d8
AC
300/**
301 * tty_port_block_til_ready - Waiting logic for tty open
302 * @port: the tty port being opened
303 * @tty: the tty device being bound
304 * @filp: the file pointer of the opener
305 *
306 * Implement the core POSIX/SuS tty behaviour when opening a tty device.
307 * Handles:
308 * - hangup (both before and during)
309 * - non blocking open
310 * - rts/dtr/dcd
311 * - signals
312 * - port flags and counts
313 *
314 * The passed tty_port must implement the carrier_raised method if it can
fcc8ac18 315 * do carrier detect and the dtr_rts method if it supports software
36c621d8
AC
316 * management of these lines. Note that the dtr/rts raise is done each
317 * iteration as a hangup may have previously dropped them while we wait.
318 */
d774a56d 319
36c621d8
AC
320int tty_port_block_til_ready(struct tty_port *port,
321 struct tty_struct *tty, struct file *filp)
322{
323 int do_clocal = 0, retval;
324 unsigned long flags;
6af9a43d 325 DEFINE_WAIT(wait);
36c621d8
AC
326
327 /* block if port is in the process of being closed */
328 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
89c8d91e 329 wait_event_interruptible_tty(tty, port->close_wait,
5fc5b42a 330 !(port->flags & ASYNC_CLOSING));
36c621d8
AC
331 if (port->flags & ASYNC_HUP_NOTIFY)
332 return -EAGAIN;
333 else
334 return -ERESTARTSYS;
335 }
336
337 /* if non-blocking mode is set we can pass directly to open unless
338 the port has just hung up or is in another error state */
8627b96d
AC
339 if (tty->flags & (1 << TTY_IO_ERROR)) {
340 port->flags |= ASYNC_NORMAL_ACTIVE;
341 return 0;
342 }
343 if (filp->f_flags & O_NONBLOCK) {
4175f3e3 344 /* Indicate we are open */
adc8d746 345 if (tty->termios.c_cflag & CBAUD)
4175f3e3 346 tty_port_raise_dtr_rts(port);
36c621d8
AC
347 port->flags |= ASYNC_NORMAL_ACTIVE;
348 return 0;
349 }
350
351 if (C_CLOCAL(tty))
352 do_clocal = 1;
353
354 /* Block waiting until we can proceed. We may need to wait for the
355 carrier, but we must also wait for any close that is in progress
356 before the next open may complete */
357
358 retval = 0;
36c621d8
AC
359
360 /* The port lock protects the port counts */
361 spin_lock_irqsave(&port->lock, flags);
362 if (!tty_hung_up_p(filp))
363 port->count--;
364 port->blocked_open++;
365 spin_unlock_irqrestore(&port->lock, flags);
366
367 while (1) {
368 /* Indicate we are open */
adc8d746 369 if (tty->termios.c_cflag & CBAUD)
7834909f 370 tty_port_raise_dtr_rts(port);
36c621d8 371
3e3b5c08 372 prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
d774a56d
AC
373 /* Check for a hangup or uninitialised port.
374 Return accordingly */
36c621d8
AC
375 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
376 if (port->flags & ASYNC_HUP_NOTIFY)
377 retval = -EAGAIN;
378 else
379 retval = -ERESTARTSYS;
380 break;
381 }
0eee50af
JS
382 /*
383 * Probe the carrier. For devices with no carrier detect
384 * tty_port_carrier_raised will always return true.
385 * Never ask drivers if CLOCAL is set, this causes troubles
386 * on some hardware.
387 */
36c621d8 388 if (!(port->flags & ASYNC_CLOSING) &&
0eee50af 389 (do_clocal || tty_port_carrier_raised(port)))
36c621d8
AC
390 break;
391 if (signal_pending(current)) {
392 retval = -ERESTARTSYS;
393 break;
394 }
89c8d91e 395 tty_unlock(tty);
36c621d8 396 schedule();
89c8d91e 397 tty_lock(tty);
36c621d8 398 }
3e3b5c08 399 finish_wait(&port->open_wait, &wait);
36c621d8
AC
400
401 /* Update counts. A parallel hangup will have set count to zero and
402 we must not mess that up further */
403 spin_lock_irqsave(&port->lock, flags);
404 if (!tty_hung_up_p(filp))
405 port->count++;
406 port->blocked_open--;
407 if (retval == 0)
408 port->flags |= ASYNC_NORMAL_ACTIVE;
409 spin_unlock_irqrestore(&port->lock, flags);
ecc2e05e 410 return retval;
36c621d8
AC
411}
412EXPORT_SYMBOL(tty_port_block_til_ready);
413
d774a56d
AC
414int tty_port_close_start(struct tty_port *port,
415 struct tty_struct *tty, struct file *filp)
a6614999
AC
416{
417 unsigned long flags;
418
419 spin_lock_irqsave(&port->lock, flags);
420 if (tty_hung_up_p(filp)) {
421 spin_unlock_irqrestore(&port->lock, flags);
422 return 0;
423 }
424
d774a56d 425 if (tty->count == 1 && port->count != 1) {
a6614999
AC
426 printk(KERN_WARNING
427 "tty_port_close_start: tty->count = 1 port count = %d.\n",
428 port->count);
429 port->count = 1;
430 }
431 if (--port->count < 0) {
432 printk(KERN_WARNING "tty_port_close_start: count = %d\n",
433 port->count);
434 port->count = 0;
435 }
436
437 if (port->count) {
438 spin_unlock_irqrestore(&port->lock, flags);
7ca0ff9a
AC
439 if (port->ops->drop)
440 port->ops->drop(port);
a6614999
AC
441 return 0;
442 }
1f5c13fa 443 set_bit(ASYNCB_CLOSING, &port->flags);
a6614999
AC
444 tty->closing = 1;
445 spin_unlock_irqrestore(&port->lock, flags);
fba85e01
AC
446 /* Don't block on a stalled port, just pull the chain */
447 if (tty->flow_stopped)
448 tty_driver_flush_buffer(tty);
7ca0ff9a 449 if (test_bit(ASYNCB_INITIALIZED, &port->flags) &&
6ed1dbae 450 port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
424cc039 451 tty_wait_until_sent_from_close(tty, port->closing_wait);
1ec739be
AC
452 if (port->drain_delay) {
453 unsigned int bps = tty_get_baud_rate(tty);
454 long timeout;
455
456 if (bps > 1200)
d774a56d
AC
457 timeout = max_t(long,
458 (HZ * 10 * port->drain_delay) / bps, HZ / 10);
1ec739be
AC
459 else
460 timeout = 2 * HZ;
461 schedule_timeout_interruptible(timeout);
462 }
e707c35c
AC
463 /* Flush the ldisc buffering */
464 tty_ldisc_flush(tty);
465
466 /* Drop DTR/RTS if HUPCL is set. This causes any attached modem to
467 hang up the line */
adc8d746 468 if (tty->termios.c_cflag & HUPCL)
e707c35c
AC
469 tty_port_lower_dtr_rts(port);
470
7ca0ff9a
AC
471 /* Don't call port->drop for the last reference. Callers will want
472 to drop the last active reference in ->shutdown() or the tty
473 shutdown path */
a6614999
AC
474 return 1;
475}
476EXPORT_SYMBOL(tty_port_close_start);
477
478void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
479{
480 unsigned long flags;
481
a6614999
AC
482 spin_lock_irqsave(&port->lock, flags);
483 tty->closing = 0;
484
485 if (port->blocked_open) {
486 spin_unlock_irqrestore(&port->lock, flags);
487 if (port->close_delay) {
488 msleep_interruptible(
489 jiffies_to_msecs(port->close_delay));
490 }
491 spin_lock_irqsave(&port->lock, flags);
492 wake_up_interruptible(&port->open_wait);
493 }
494 port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
495 wake_up_interruptible(&port->close_wait);
496 spin_unlock_irqrestore(&port->lock, flags);
497}
498EXPORT_SYMBOL(tty_port_close_end);
7ca0ff9a
AC
499
500void tty_port_close(struct tty_port *port, struct tty_struct *tty,
501 struct file *filp)
502{
503 if (tty_port_close_start(port, tty, filp) == 0)
504 return;
505 tty_port_shutdown(port);
d74e8286 506 set_bit(TTY_IO_ERROR, &tty->flags);
7ca0ff9a
AC
507 tty_port_close_end(port, tty);
508 tty_port_tty_set(port, NULL);
509}
510EXPORT_SYMBOL(tty_port_close);
64bc3979 511
72a33bf5
JS
512/**
513 * tty_port_install - generic tty->ops->install handler
514 * @port: tty_port of the device
515 * @driver: tty_driver for this device
516 * @tty: tty to be installed
517 *
518 * It is the same as tty_standard_install except the provided @port is linked
519 * to a concrete tty specified by @tty. Use this or tty_port_register_device
520 * (or both). Call tty_port_link_device as a last resort.
521 */
695586ca
JS
522int tty_port_install(struct tty_port *port, struct tty_driver *driver,
523 struct tty_struct *tty)
524{
525 tty->port = port;
526 return tty_standard_install(driver, tty);
527}
528EXPORT_SYMBOL_GPL(tty_port_install);
529
64bc3979 530int tty_port_open(struct tty_port *port, struct tty_struct *tty,
d774a56d 531 struct file *filp)
64bc3979
AC
532{
533 spin_lock_irq(&port->lock);
534 if (!tty_hung_up_p(filp))
535 ++port->count;
536 spin_unlock_irq(&port->lock);
537 tty_port_tty_set(port, tty);
538
539 /*
540 * Do the device-specific open only if the hardware isn't
541 * already initialized. Serialize open and shutdown using the
542 * port mutex.
543 */
544
545 mutex_lock(&port->mutex);
546
547 if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) {
a9a37ec3 548 clear_bit(TTY_IO_ERROR, &tty->flags);
64bc3979
AC
549 if (port->ops->activate) {
550 int retval = port->ops->activate(port, tty);
551 if (retval) {
d774a56d
AC
552 mutex_unlock(&port->mutex);
553 return retval;
554 }
555 }
64bc3979
AC
556 set_bit(ASYNCB_INITIALIZED, &port->flags);
557 }
558 mutex_unlock(&port->mutex);
559 return tty_port_block_til_ready(port, tty, filp);
560}
561
562EXPORT_SYMBOL(tty_port_open);