net/mlx4_core: Reduce harmless SRIOV error message to debug level
[linux-2.6-block.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>
174cd4b1 14#include <linux/sched/signal.h>
9e48565d
AC
15#include <linux/wait.h>
16#include <linux/bitops.h>
17#include <linux/delay.h>
18#include <linux/module.h>
8ee3fde0 19#include <linux/serdev.h>
9e48565d 20
c3485ee0
RH
21static int tty_port_default_receive_buf(struct tty_port *port,
22 const unsigned char *p,
23 const unsigned char *f, size_t count)
24{
25 int ret;
26 struct tty_struct *tty;
27 struct tty_ldisc *disc;
28
29 tty = READ_ONCE(port->itty);
30 if (!tty)
31 return 0;
32
33 disc = tty_ldisc_ref(tty);
34 if (!disc)
35 return 0;
36
37 ret = tty_ldisc_receive_buf(disc, p, (char *)f, count);
38
39 tty_ldisc_deref(disc);
40
41 return ret;
42}
43
44static void tty_port_default_wakeup(struct tty_port *port)
45{
46 struct tty_struct *tty = tty_port_tty_get(port);
47
48 if (tty) {
49 tty_wakeup(tty);
50 tty_kref_put(tty);
51 }
52}
53
54static const struct tty_port_client_operations default_client_ops = {
55 .receive_buf = tty_port_default_receive_buf,
56 .write_wakeup = tty_port_default_wakeup,
57};
58
9e48565d
AC
59void tty_port_init(struct tty_port *port)
60{
61 memset(port, 0, sizeof(*port));
ecbbfd44 62 tty_buffer_init(port);
9e48565d 63 init_waitqueue_head(&port->open_wait);
bdc04e31 64 init_waitqueue_head(&port->delta_msr_wait);
9e48565d 65 mutex_init(&port->mutex);
44e4909e 66 mutex_init(&port->buf_mutex);
4a90f09b 67 spin_lock_init(&port->lock);
9e48565d
AC
68 port->close_delay = (50 * HZ) / 100;
69 port->closing_wait = (3000 * HZ) / 100;
c3485ee0 70 port->client_ops = &default_client_ops;
568aafc6 71 kref_init(&port->kref);
9e48565d
AC
72}
73EXPORT_SYMBOL(tty_port_init);
74
2cb4ca02
JS
75/**
76 * tty_port_link_device - link tty and tty_port
77 * @port: tty_port of the device
78 * @driver: tty_driver for this device
79 * @index: index of the tty
80 *
81 * Provide the tty layer wit ha link from a tty (specified by @index) to a
82 * tty_port (@port). Use this only if neither tty_port_register_device nor
83 * tty_port_install is used in the driver. If used, this has to be called before
84 * tty_register_driver.
85 */
86void tty_port_link_device(struct tty_port *port,
87 struct tty_driver *driver, unsigned index)
88{
89 if (WARN_ON(index >= driver->num))
90 return;
91 driver->ports[index] = port;
92}
93EXPORT_SYMBOL_GPL(tty_port_link_device);
94
72a33bf5
JS
95/**
96 * tty_port_register_device - register tty device
97 * @port: tty_port of the device
98 * @driver: tty_driver for this device
99 * @index: index of the tty
100 * @device: parent if exists, otherwise NULL
101 *
102 * It is the same as tty_register_device except the provided @port is linked to
103 * a concrete tty specified by @index. Use this or tty_port_install (or both).
104 * Call tty_port_link_device as a last resort.
105 */
057eb856
JS
106struct device *tty_port_register_device(struct tty_port *port,
107 struct tty_driver *driver, unsigned index,
108 struct device *device)
109{
3086365d 110 return tty_port_register_device_attr(port, driver, index, device, NULL, NULL);
057eb856
JS
111}
112EXPORT_SYMBOL_GPL(tty_port_register_device);
113
b1b79916
TH
114/**
115 * tty_port_register_device_attr - register tty device
116 * @port: tty_port of the device
117 * @driver: tty_driver for this device
118 * @index: index of the tty
119 * @device: parent if exists, otherwise NULL
120 * @drvdata: Driver data to be set to device.
121 * @attr_grp: Attribute group to be set on device.
122 *
123 * It is the same as tty_register_device_attr except the provided @port is
124 * linked to a concrete tty specified by @index. Use this or tty_port_install
125 * (or both). Call tty_port_link_device as a last resort.
126 */
127struct device *tty_port_register_device_attr(struct tty_port *port,
128 struct tty_driver *driver, unsigned index,
129 struct device *device, void *drvdata,
130 const struct attribute_group **attr_grp)
131{
8ee3fde0
RH
132 struct device *dev;
133
b1b79916 134 tty_port_link_device(port, driver, index);
8ee3fde0
RH
135
136 dev = serdev_tty_port_register(port, device, driver, index);
137 if (PTR_ERR(dev) != -ENODEV)
138 /* Skip creating cdev if we registered a serdev device */
139 return dev;
140
b1b79916
TH
141 return tty_register_device_attr(driver, index, device, drvdata,
142 attr_grp);
143}
144EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
145
9e48565d
AC
146int tty_port_alloc_xmit_buf(struct tty_port *port)
147{
148 /* We may sleep in get_zeroed_page() */
44e4909e 149 mutex_lock(&port->buf_mutex);
9e48565d
AC
150 if (port->xmit_buf == NULL)
151 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
44e4909e 152 mutex_unlock(&port->buf_mutex);
9e48565d
AC
153 if (port->xmit_buf == NULL)
154 return -ENOMEM;
155 return 0;
156}
157EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
158
159void tty_port_free_xmit_buf(struct tty_port *port)
160{
44e4909e 161 mutex_lock(&port->buf_mutex);
9e48565d
AC
162 if (port->xmit_buf != NULL) {
163 free_page((unsigned long)port->xmit_buf);
164 port->xmit_buf = NULL;
165 }
44e4909e 166 mutex_unlock(&port->buf_mutex);
9e48565d
AC
167}
168EXPORT_SYMBOL(tty_port_free_xmit_buf);
169
de274bfe
JS
170/**
171 * tty_port_destroy -- destroy inited port
172 * @port: tty port to be doestroyed
173 *
174 * When a port was initialized using tty_port_init, one has to destroy the
175 * port by this function. Either indirectly by using tty_port refcounting
176 * (tty_port_put) or directly if refcounting is not used.
177 */
178void tty_port_destroy(struct tty_port *port)
179{
e176058f 180 tty_buffer_cancel_work(port);
de274bfe
JS
181 tty_buffer_free_all(port);
182}
183EXPORT_SYMBOL(tty_port_destroy);
184
568aafc6
AC
185static void tty_port_destructor(struct kref *kref)
186{
187 struct tty_port *port = container_of(kref, struct tty_port, kref);
e3bfea23
PH
188
189 /* check if last port ref was dropped before tty release */
190 if (WARN_ON(port->itty))
191 return;
8ee3fde0
RH
192
193 serdev_tty_port_unregister(port);
194
568aafc6
AC
195 if (port->xmit_buf)
196 free_page((unsigned long)port->xmit_buf);
de274bfe 197 tty_port_destroy(port);
81c79838 198 if (port->ops && port->ops->destruct)
568aafc6
AC
199 port->ops->destruct(port);
200 else
201 kfree(port);
202}
203
204void tty_port_put(struct tty_port *port)
205{
206 if (port)
207 kref_put(&port->kref, tty_port_destructor);
208}
209EXPORT_SYMBOL(tty_port_put);
9e48565d 210
4a90f09b
AC
211/**
212 * tty_port_tty_get - get a tty reference
213 * @port: tty port
214 *
215 * Return a refcount protected tty instance or NULL if the port is not
216 * associated with a tty (eg due to close or hangup)
217 */
218
219struct tty_struct *tty_port_tty_get(struct tty_port *port)
220{
221 unsigned long flags;
222 struct tty_struct *tty;
223
224 spin_lock_irqsave(&port->lock, flags);
225 tty = tty_kref_get(port->tty);
226 spin_unlock_irqrestore(&port->lock, flags);
227 return tty;
228}
229EXPORT_SYMBOL(tty_port_tty_get);
230
231/**
232 * tty_port_tty_set - set the tty of a port
233 * @port: tty port
234 * @tty: the tty
235 *
236 * Associate the port and tty pair. Manages any internal refcounts.
237 * Pass NULL to deassociate a port
238 */
239
240void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
241{
242 unsigned long flags;
243
244 spin_lock_irqsave(&port->lock, flags);
a211b1af 245 tty_kref_put(port->tty);
cb4bca35 246 port->tty = tty_kref_get(tty);
4a90f09b
AC
247 spin_unlock_irqrestore(&port->lock, flags);
248}
249EXPORT_SYMBOL(tty_port_tty_set);
31f35939 250
957dacae 251static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty)
7ca0ff9a 252{
64bc3979 253 mutex_lock(&port->mutex);
8bde9658
JH
254 if (port->console)
255 goto out;
256
d41861ca
PH
257 if (tty_port_initialized(port)) {
258 tty_port_set_initialized(port, 0);
957dacae
JH
259 /*
260 * Drop DTR/RTS if HUPCL is set. This causes any attached
261 * modem to hang up the line.
262 */
263 if (tty && C_HUPCL(tty))
264 tty_port_lower_dtr_rts(port);
265
8bde9658 266 if (port->ops->shutdown)
7ca0ff9a 267 port->ops->shutdown(port);
8bde9658
JH
268 }
269out:
64bc3979 270 mutex_unlock(&port->mutex);
7ca0ff9a
AC
271}
272
3e61696b
AC
273/**
274 * tty_port_hangup - hangup helper
275 * @port: tty port
276 *
277 * Perform port level tty hangup flag and count changes. Drop the tty
278 * reference.
9c9928bd
PH
279 *
280 * Caller holds tty lock.
3e61696b
AC
281 */
282
283void tty_port_hangup(struct tty_port *port)
284{
957dacae 285 struct tty_struct *tty;
3e61696b
AC
286 unsigned long flags;
287
288 spin_lock_irqsave(&port->lock, flags);
289 port->count = 0;
957dacae
JH
290 tty = port->tty;
291 if (tty)
292 set_bit(TTY_IO_ERROR, &tty->flags);
3e61696b
AC
293 port->tty = NULL;
294 spin_unlock_irqrestore(&port->lock, flags);
807c8d81 295 tty_port_set_active(port, 0);
957dacae
JH
296 tty_port_shutdown(port, tty);
297 tty_kref_put(tty);
3e61696b 298 wake_up_interruptible(&port->open_wait);
bdc04e31 299 wake_up_interruptible(&port->delta_msr_wait);
3e61696b
AC
300}
301EXPORT_SYMBOL(tty_port_hangup);
302
aa27a094
JS
303/**
304 * tty_port_tty_hangup - helper to hang up a tty
305 *
306 * @port: tty port
307 * @check_clocal: hang only ttys with CLOCAL unset?
308 */
309void tty_port_tty_hangup(struct tty_port *port, bool check_clocal)
310{
311 struct tty_struct *tty = tty_port_tty_get(port);
312
1d9e689c 313 if (tty && (!check_clocal || !C_CLOCAL(tty)))
aa27a094 314 tty_hangup(tty);
1d9e689c 315 tty_kref_put(tty);
aa27a094
JS
316}
317EXPORT_SYMBOL_GPL(tty_port_tty_hangup);
318
6aad04f2
JS
319/**
320 * tty_port_tty_wakeup - helper to wake up a tty
321 *
322 * @port: tty port
323 */
324void tty_port_tty_wakeup(struct tty_port *port)
325{
c3485ee0 326 port->client_ops->write_wakeup(port);
6aad04f2
JS
327}
328EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
329
31f35939
AC
330/**
331 * tty_port_carrier_raised - carrier raised check
332 * @port: tty port
333 *
334 * Wrapper for the carrier detect logic. For the moment this is used
335 * to hide some internal details. This will eventually become entirely
336 * internal to the tty port.
337 */
338
339int tty_port_carrier_raised(struct tty_port *port)
340{
341 if (port->ops->carrier_raised == NULL)
342 return 1;
343 return port->ops->carrier_raised(port);
344}
345EXPORT_SYMBOL(tty_port_carrier_raised);
5d951fb4
AC
346
347/**
fcc8ac18 348 * tty_port_raise_dtr_rts - Raise DTR/RTS
5d951fb4
AC
349 * @port: tty port
350 *
351 * Wrapper for the DTR/RTS raise logic. For the moment this is used
352 * to hide some internal details. This will eventually become entirely
353 * internal to the tty port.
354 */
355
356void tty_port_raise_dtr_rts(struct tty_port *port)
357{
fcc8ac18
AC
358 if (port->ops->dtr_rts)
359 port->ops->dtr_rts(port, 1);
5d951fb4
AC
360}
361EXPORT_SYMBOL(tty_port_raise_dtr_rts);
36c621d8 362
fcc8ac18
AC
363/**
364 * tty_port_lower_dtr_rts - Lower DTR/RTS
365 * @port: tty port
366 *
367 * Wrapper for the DTR/RTS raise logic. For the moment this is used
368 * to hide some internal details. This will eventually become entirely
369 * internal to the tty port.
370 */
371
372void tty_port_lower_dtr_rts(struct tty_port *port)
373{
374 if (port->ops->dtr_rts)
375 port->ops->dtr_rts(port, 0);
376}
377EXPORT_SYMBOL(tty_port_lower_dtr_rts);
378
36c621d8
AC
379/**
380 * tty_port_block_til_ready - Waiting logic for tty open
381 * @port: the tty port being opened
382 * @tty: the tty device being bound
ed3f0af8 383 * @filp: the file pointer of the opener or NULL
36c621d8
AC
384 *
385 * Implement the core POSIX/SuS tty behaviour when opening a tty device.
386 * Handles:
387 * - hangup (both before and during)
388 * - non blocking open
389 * - rts/dtr/dcd
390 * - signals
391 * - port flags and counts
392 *
393 * The passed tty_port must implement the carrier_raised method if it can
fcc8ac18 394 * do carrier detect and the dtr_rts method if it supports software
36c621d8
AC
395 * management of these lines. Note that the dtr/rts raise is done each
396 * iteration as a hangup may have previously dropped them while we wait.
c590f6b6
PH
397 *
398 * Caller holds tty lock.
399 *
400 * NB: May drop and reacquire tty lock when blocking, so tty and tty_port
401 * may have changed state (eg., may have been hung up).
36c621d8 402 */
d774a56d 403
36c621d8
AC
404int tty_port_block_til_ready(struct tty_port *port,
405 struct tty_struct *tty, struct file *filp)
406{
407 int do_clocal = 0, retval;
408 unsigned long flags;
6af9a43d 409 DEFINE_WAIT(wait);
36c621d8 410
36c621d8
AC
411 /* if non-blocking mode is set we can pass directly to open unless
412 the port has just hung up or is in another error state */
18900ca6 413 if (tty_io_error(tty)) {
807c8d81 414 tty_port_set_active(port, 1);
8627b96d
AC
415 return 0;
416 }
ed3f0af8 417 if (filp == NULL || (filp->f_flags & O_NONBLOCK)) {
4175f3e3 418 /* Indicate we are open */
9db276f8 419 if (C_BAUD(tty))
4175f3e3 420 tty_port_raise_dtr_rts(port);
807c8d81 421 tty_port_set_active(port, 1);
36c621d8
AC
422 return 0;
423 }
424
425 if (C_CLOCAL(tty))
426 do_clocal = 1;
427
428 /* Block waiting until we can proceed. We may need to wait for the
429 carrier, but we must also wait for any close that is in progress
430 before the next open may complete */
431
432 retval = 0;
36c621d8
AC
433
434 /* The port lock protects the port counts */
435 spin_lock_irqsave(&port->lock, flags);
e359a4e3 436 port->count--;
36c621d8
AC
437 port->blocked_open++;
438 spin_unlock_irqrestore(&port->lock, flags);
439
440 while (1) {
441 /* Indicate we are open */
d41861ca 442 if (C_BAUD(tty) && tty_port_initialized(port))
7834909f 443 tty_port_raise_dtr_rts(port);
36c621d8 444
3e3b5c08 445 prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
d774a56d
AC
446 /* Check for a hangup or uninitialised port.
447 Return accordingly */
d41861ca 448 if (tty_hung_up_p(filp) || !tty_port_initialized(port)) {
36c621d8
AC
449 if (port->flags & ASYNC_HUP_NOTIFY)
450 retval = -EAGAIN;
451 else
452 retval = -ERESTARTSYS;
453 break;
454 }
0eee50af
JS
455 /*
456 * Probe the carrier. For devices with no carrier detect
457 * tty_port_carrier_raised will always return true.
458 * Never ask drivers if CLOCAL is set, this causes troubles
459 * on some hardware.
460 */
fef062cb 461 if (do_clocal || tty_port_carrier_raised(port))
36c621d8
AC
462 break;
463 if (signal_pending(current)) {
464 retval = -ERESTARTSYS;
465 break;
466 }
89c8d91e 467 tty_unlock(tty);
36c621d8 468 schedule();
89c8d91e 469 tty_lock(tty);
36c621d8 470 }
3e3b5c08 471 finish_wait(&port->open_wait, &wait);
36c621d8
AC
472
473 /* Update counts. A parallel hangup will have set count to zero and
474 we must not mess that up further */
475 spin_lock_irqsave(&port->lock, flags);
476 if (!tty_hung_up_p(filp))
477 port->count++;
478 port->blocked_open--;
36c621d8 479 spin_unlock_irqrestore(&port->lock, flags);
807c8d81
PH
480 if (retval == 0)
481 tty_port_set_active(port, 1);
ecc2e05e 482 return retval;
36c621d8
AC
483}
484EXPORT_SYMBOL(tty_port_block_til_ready);
485
b74414f5
JH
486static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty)
487{
488 unsigned int bps = tty_get_baud_rate(tty);
489 long timeout;
490
491 if (bps > 1200) {
492 timeout = (HZ * 10 * port->drain_delay) / bps;
493 timeout = max_t(long, timeout, HZ / 10);
494 } else {
495 timeout = 2 * HZ;
496 }
497 schedule_timeout_interruptible(timeout);
498}
499
79c1faa4 500/* Caller holds tty lock. */
d774a56d
AC
501int tty_port_close_start(struct tty_port *port,
502 struct tty_struct *tty, struct file *filp)
a6614999
AC
503{
504 unsigned long flags;
505
633caba8 506 if (tty_hung_up_p(filp))
a6614999 507 return 0;
a6614999 508
633caba8 509 spin_lock_irqsave(&port->lock, flags);
d774a56d 510 if (tty->count == 1 && port->count != 1) {
339f36ba
PH
511 tty_warn(tty, "%s: tty->count = 1 port count = %d\n", __func__,
512 port->count);
a6614999
AC
513 port->count = 1;
514 }
515 if (--port->count < 0) {
339f36ba
PH
516 tty_warn(tty, "%s: bad port count (%d)\n", __func__,
517 port->count);
a6614999
AC
518 port->count = 0;
519 }
520
521 if (port->count) {
522 spin_unlock_irqrestore(&port->lock, flags);
523 return 0;
524 }
a6614999 525 spin_unlock_irqrestore(&port->lock, flags);
0b2588ca 526
ddc7b758
PH
527 tty->closing = 1;
528
d41861ca 529 if (tty_port_initialized(port)) {
0b2588ca
JH
530 /* Don't block on a stalled port, just pull the chain */
531 if (tty->flow_stopped)
532 tty_driver_flush_buffer(tty);
533 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
79c1faa4 534 tty_wait_until_sent(tty, port->closing_wait);
0b2588ca
JH
535 if (port->drain_delay)
536 tty_port_drain_delay(port, tty);
537 }
e707c35c
AC
538 /* Flush the ldisc buffering */
539 tty_ldisc_flush(tty);
540
469d6d06 541 /* Report to caller this is the last port reference */
a6614999
AC
542 return 1;
543}
544EXPORT_SYMBOL(tty_port_close_start);
545
0733db91 546/* Caller holds tty lock */
a6614999
AC
547void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
548{
549 unsigned long flags;
550
3f40f5b2 551 tty_ldisc_flush(tty);
a6614999
AC
552 tty->closing = 0;
553
ddc7b758
PH
554 spin_lock_irqsave(&port->lock, flags);
555
a6614999
AC
556 if (port->blocked_open) {
557 spin_unlock_irqrestore(&port->lock, flags);
5823323e
PH
558 if (port->close_delay)
559 msleep_interruptible(jiffies_to_msecs(port->close_delay));
a6614999
AC
560 spin_lock_irqsave(&port->lock, flags);
561 wake_up_interruptible(&port->open_wait);
562 }
a6614999 563 spin_unlock_irqrestore(&port->lock, flags);
807c8d81 564 tty_port_set_active(port, 0);
a6614999
AC
565}
566EXPORT_SYMBOL(tty_port_close_end);
7ca0ff9a 567
0733db91
PH
568/**
569 * tty_port_close
570 *
571 * Caller holds tty lock
0733db91 572 */
7ca0ff9a
AC
573void tty_port_close(struct tty_port *port, struct tty_struct *tty,
574 struct file *filp)
575{
576 if (tty_port_close_start(port, tty, filp) == 0)
577 return;
957dacae 578 tty_port_shutdown(port, tty);
d74e8286 579 set_bit(TTY_IO_ERROR, &tty->flags);
7ca0ff9a
AC
580 tty_port_close_end(port, tty);
581 tty_port_tty_set(port, NULL);
582}
583EXPORT_SYMBOL(tty_port_close);
64bc3979 584
72a33bf5
JS
585/**
586 * tty_port_install - generic tty->ops->install handler
587 * @port: tty_port of the device
588 * @driver: tty_driver for this device
589 * @tty: tty to be installed
590 *
591 * It is the same as tty_standard_install except the provided @port is linked
592 * to a concrete tty specified by @tty. Use this or tty_port_register_device
593 * (or both). Call tty_port_link_device as a last resort.
594 */
695586ca
JS
595int tty_port_install(struct tty_port *port, struct tty_driver *driver,
596 struct tty_struct *tty)
597{
598 tty->port = port;
599 return tty_standard_install(driver, tty);
600}
601EXPORT_SYMBOL_GPL(tty_port_install);
602
addd4672
PH
603/**
604 * tty_port_open
605 *
606 * Caller holds tty lock.
607 *
608 * NB: may drop and reacquire tty lock (in tty_port_block_til_ready()) so
609 * tty and tty_port may have changed state (eg., may be hung up now)
610 */
64bc3979 611int tty_port_open(struct tty_port *port, struct tty_struct *tty,
d774a56d 612 struct file *filp)
64bc3979
AC
613{
614 spin_lock_irq(&port->lock);
e359a4e3 615 ++port->count;
64bc3979
AC
616 spin_unlock_irq(&port->lock);
617 tty_port_tty_set(port, tty);
618
619 /*
620 * Do the device-specific open only if the hardware isn't
621 * already initialized. Serialize open and shutdown using the
622 * port mutex.
623 */
624
625 mutex_lock(&port->mutex);
626
d41861ca 627 if (!tty_port_initialized(port)) {
a9a37ec3 628 clear_bit(TTY_IO_ERROR, &tty->flags);
64bc3979
AC
629 if (port->ops->activate) {
630 int retval = port->ops->activate(port, tty);
631 if (retval) {
d774a56d
AC
632 mutex_unlock(&port->mutex);
633 return retval;
634 }
635 }
d41861ca 636 tty_port_set_initialized(port, 1);
64bc3979
AC
637 }
638 mutex_unlock(&port->mutex);
639 return tty_port_block_til_ready(port, tty, filp);
640}
641
642EXPORT_SYMBOL(tty_port_open);