tty: fix port buffer locking
[linux/fpc-iii.git] / drivers / tty / tty_port.c
blob88dac3b7936933cc7c6644451c837aa6c6585c9f
1 /*
2 * Tty port functions
3 */
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>
10 #include <linux/serial.h>
11 #include <linux/timer.h>
12 #include <linux/string.h>
13 #include <linux/slab.h>
14 #include <linux/sched/signal.h>
15 #include <linux/wait.h>
16 #include <linux/bitops.h>
17 #include <linux/delay.h>
18 #include <linux/module.h>
20 static int tty_port_default_receive_buf(struct tty_port *port,
21 const unsigned char *p,
22 const unsigned char *f, size_t count)
24 int ret;
25 struct tty_struct *tty;
26 struct tty_ldisc *disc;
28 tty = READ_ONCE(port->itty);
29 if (!tty)
30 return 0;
32 disc = tty_ldisc_ref(tty);
33 if (!disc)
34 return 0;
36 mutex_lock(&tty->atomic_write_lock);
37 ret = tty_ldisc_receive_buf(disc, p, (char *)f, count);
38 mutex_unlock(&tty->atomic_write_lock);
40 tty_ldisc_deref(disc);
42 return ret;
45 static void tty_port_default_wakeup(struct tty_port *port)
47 struct tty_struct *tty = tty_port_tty_get(port);
49 if (tty) {
50 tty_wakeup(tty);
51 tty_kref_put(tty);
55 static const struct tty_port_client_operations default_client_ops = {
56 .receive_buf = tty_port_default_receive_buf,
57 .write_wakeup = tty_port_default_wakeup,
60 void tty_port_init(struct tty_port *port)
62 memset(port, 0, sizeof(*port));
63 tty_buffer_init(port);
64 init_waitqueue_head(&port->open_wait);
65 init_waitqueue_head(&port->delta_msr_wait);
66 mutex_init(&port->mutex);
67 mutex_init(&port->buf_mutex);
68 spin_lock_init(&port->lock);
69 port->close_delay = (50 * HZ) / 100;
70 port->closing_wait = (3000 * HZ) / 100;
71 port->client_ops = &default_client_ops;
72 kref_init(&port->kref);
74 EXPORT_SYMBOL(tty_port_init);
76 /**
77 * tty_port_link_device - link tty and tty_port
78 * @port: tty_port of the device
79 * @driver: tty_driver for this device
80 * @index: index of the tty
82 * Provide the tty layer wit ha link from a tty (specified by @index) to a
83 * tty_port (@port). Use this only if neither tty_port_register_device nor
84 * tty_port_install is used in the driver. If used, this has to be called before
85 * tty_register_driver.
87 void tty_port_link_device(struct tty_port *port,
88 struct tty_driver *driver, unsigned index)
90 if (WARN_ON(index >= driver->num))
91 return;
92 driver->ports[index] = port;
94 EXPORT_SYMBOL_GPL(tty_port_link_device);
96 /**
97 * tty_port_register_device - register tty device
98 * @port: tty_port of the device
99 * @driver: tty_driver for this device
100 * @index: index of the tty
101 * @device: parent if exists, otherwise NULL
103 * It is the same as tty_register_device except the provided @port is linked to
104 * a concrete tty specified by @index. Use this or tty_port_install (or both).
105 * Call tty_port_link_device as a last resort.
107 struct device *tty_port_register_device(struct tty_port *port,
108 struct tty_driver *driver, unsigned index,
109 struct device *device)
111 return tty_port_register_device_attr(port, driver, index, device, NULL, NULL);
113 EXPORT_SYMBOL_GPL(tty_port_register_device);
116 * tty_port_register_device_attr - register tty device
117 * @port: tty_port of the device
118 * @driver: tty_driver for this device
119 * @index: index of the tty
120 * @device: parent if exists, otherwise NULL
121 * @drvdata: Driver data to be set to device.
122 * @attr_grp: Attribute group to be set on device.
124 * It is the same as tty_register_device_attr except the provided @port is
125 * linked to a concrete tty specified by @index. Use this or tty_port_install
126 * (or both). Call tty_port_link_device as a last resort.
128 struct device *tty_port_register_device_attr(struct tty_port *port,
129 struct tty_driver *driver, unsigned index,
130 struct device *device, void *drvdata,
131 const struct attribute_group **attr_grp)
133 tty_port_link_device(port, driver, index);
134 return tty_register_device_attr(driver, index, device, drvdata,
135 attr_grp);
137 EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
139 int tty_port_alloc_xmit_buf(struct tty_port *port)
141 /* We may sleep in get_zeroed_page() */
142 mutex_lock(&port->buf_mutex);
143 if (port->xmit_buf == NULL)
144 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
145 mutex_unlock(&port->buf_mutex);
146 if (port->xmit_buf == NULL)
147 return -ENOMEM;
148 return 0;
150 EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
152 void tty_port_free_xmit_buf(struct tty_port *port)
154 mutex_lock(&port->buf_mutex);
155 if (port->xmit_buf != NULL) {
156 free_page((unsigned long)port->xmit_buf);
157 port->xmit_buf = NULL;
159 mutex_unlock(&port->buf_mutex);
161 EXPORT_SYMBOL(tty_port_free_xmit_buf);
164 * tty_port_destroy -- destroy inited port
165 * @port: tty port to be doestroyed
167 * When a port was initialized using tty_port_init, one has to destroy the
168 * port by this function. Either indirectly by using tty_port refcounting
169 * (tty_port_put) or directly if refcounting is not used.
171 void tty_port_destroy(struct tty_port *port)
173 tty_buffer_cancel_work(port);
174 tty_buffer_free_all(port);
176 EXPORT_SYMBOL(tty_port_destroy);
178 static void tty_port_destructor(struct kref *kref)
180 struct tty_port *port = container_of(kref, struct tty_port, kref);
182 /* check if last port ref was dropped before tty release */
183 if (WARN_ON(port->itty))
184 return;
185 if (port->xmit_buf)
186 free_page((unsigned long)port->xmit_buf);
187 tty_port_destroy(port);
188 if (port->ops && port->ops->destruct)
189 port->ops->destruct(port);
190 else
191 kfree(port);
194 void tty_port_put(struct tty_port *port)
196 if (port)
197 kref_put(&port->kref, tty_port_destructor);
199 EXPORT_SYMBOL(tty_port_put);
202 * tty_port_tty_get - get a tty reference
203 * @port: tty port
205 * Return a refcount protected tty instance or NULL if the port is not
206 * associated with a tty (eg due to close or hangup)
209 struct tty_struct *tty_port_tty_get(struct tty_port *port)
211 unsigned long flags;
212 struct tty_struct *tty;
214 spin_lock_irqsave(&port->lock, flags);
215 tty = tty_kref_get(port->tty);
216 spin_unlock_irqrestore(&port->lock, flags);
217 return tty;
219 EXPORT_SYMBOL(tty_port_tty_get);
222 * tty_port_tty_set - set the tty of a port
223 * @port: tty port
224 * @tty: the tty
226 * Associate the port and tty pair. Manages any internal refcounts.
227 * Pass NULL to deassociate a port
230 void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
232 unsigned long flags;
234 spin_lock_irqsave(&port->lock, flags);
235 tty_kref_put(port->tty);
236 port->tty = tty_kref_get(tty);
237 spin_unlock_irqrestore(&port->lock, flags);
239 EXPORT_SYMBOL(tty_port_tty_set);
241 static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty)
243 mutex_lock(&port->mutex);
244 if (port->console)
245 goto out;
247 if (tty_port_initialized(port)) {
248 tty_port_set_initialized(port, 0);
250 * Drop DTR/RTS if HUPCL is set. This causes any attached
251 * modem to hang up the line.
253 if (tty && C_HUPCL(tty))
254 tty_port_lower_dtr_rts(port);
256 if (port->ops->shutdown)
257 port->ops->shutdown(port);
259 out:
260 mutex_unlock(&port->mutex);
264 * tty_port_hangup - hangup helper
265 * @port: tty port
267 * Perform port level tty hangup flag and count changes. Drop the tty
268 * reference.
270 * Caller holds tty lock.
273 void tty_port_hangup(struct tty_port *port)
275 struct tty_struct *tty;
276 unsigned long flags;
278 spin_lock_irqsave(&port->lock, flags);
279 port->count = 0;
280 tty = port->tty;
281 if (tty)
282 set_bit(TTY_IO_ERROR, &tty->flags);
283 port->tty = NULL;
284 spin_unlock_irqrestore(&port->lock, flags);
285 tty_port_set_active(port, 0);
286 tty_port_shutdown(port, tty);
287 tty_kref_put(tty);
288 wake_up_interruptible(&port->open_wait);
289 wake_up_interruptible(&port->delta_msr_wait);
291 EXPORT_SYMBOL(tty_port_hangup);
294 * tty_port_tty_hangup - helper to hang up a tty
296 * @port: tty port
297 * @check_clocal: hang only ttys with CLOCAL unset?
299 void tty_port_tty_hangup(struct tty_port *port, bool check_clocal)
301 struct tty_struct *tty = tty_port_tty_get(port);
303 if (tty && (!check_clocal || !C_CLOCAL(tty)))
304 tty_hangup(tty);
305 tty_kref_put(tty);
307 EXPORT_SYMBOL_GPL(tty_port_tty_hangup);
310 * tty_port_tty_wakeup - helper to wake up a tty
312 * @port: tty port
314 void tty_port_tty_wakeup(struct tty_port *port)
316 port->client_ops->write_wakeup(port);
318 EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
321 * tty_port_carrier_raised - carrier raised check
322 * @port: tty port
324 * Wrapper for the carrier detect logic. For the moment this is used
325 * to hide some internal details. This will eventually become entirely
326 * internal to the tty port.
329 int tty_port_carrier_raised(struct tty_port *port)
331 if (port->ops->carrier_raised == NULL)
332 return 1;
333 return port->ops->carrier_raised(port);
335 EXPORT_SYMBOL(tty_port_carrier_raised);
338 * tty_port_raise_dtr_rts - Raise DTR/RTS
339 * @port: tty port
341 * Wrapper for the DTR/RTS raise logic. For the moment this is used
342 * to hide some internal details. This will eventually become entirely
343 * internal to the tty port.
346 void tty_port_raise_dtr_rts(struct tty_port *port)
348 if (port->ops->dtr_rts)
349 port->ops->dtr_rts(port, 1);
351 EXPORT_SYMBOL(tty_port_raise_dtr_rts);
354 * tty_port_lower_dtr_rts - Lower DTR/RTS
355 * @port: tty port
357 * Wrapper for the DTR/RTS raise logic. For the moment this is used
358 * to hide some internal details. This will eventually become entirely
359 * internal to the tty port.
362 void tty_port_lower_dtr_rts(struct tty_port *port)
364 if (port->ops->dtr_rts)
365 port->ops->dtr_rts(port, 0);
367 EXPORT_SYMBOL(tty_port_lower_dtr_rts);
370 * tty_port_block_til_ready - Waiting logic for tty open
371 * @port: the tty port being opened
372 * @tty: the tty device being bound
373 * @filp: the file pointer of the opener or NULL
375 * Implement the core POSIX/SuS tty behaviour when opening a tty device.
376 * Handles:
377 * - hangup (both before and during)
378 * - non blocking open
379 * - rts/dtr/dcd
380 * - signals
381 * - port flags and counts
383 * The passed tty_port must implement the carrier_raised method if it can
384 * do carrier detect and the dtr_rts method if it supports software
385 * management of these lines. Note that the dtr/rts raise is done each
386 * iteration as a hangup may have previously dropped them while we wait.
388 * Caller holds tty lock.
390 * NB: May drop and reacquire tty lock when blocking, so tty and tty_port
391 * may have changed state (eg., may have been hung up).
394 int tty_port_block_til_ready(struct tty_port *port,
395 struct tty_struct *tty, struct file *filp)
397 int do_clocal = 0, retval;
398 unsigned long flags;
399 DEFINE_WAIT(wait);
401 /* if non-blocking mode is set we can pass directly to open unless
402 the port has just hung up or is in another error state */
403 if (tty_io_error(tty)) {
404 tty_port_set_active(port, 1);
405 return 0;
407 if (filp == NULL || (filp->f_flags & O_NONBLOCK)) {
408 /* Indicate we are open */
409 if (C_BAUD(tty))
410 tty_port_raise_dtr_rts(port);
411 tty_port_set_active(port, 1);
412 return 0;
415 if (C_CLOCAL(tty))
416 do_clocal = 1;
418 /* Block waiting until we can proceed. We may need to wait for the
419 carrier, but we must also wait for any close that is in progress
420 before the next open may complete */
422 retval = 0;
424 /* The port lock protects the port counts */
425 spin_lock_irqsave(&port->lock, flags);
426 port->count--;
427 port->blocked_open++;
428 spin_unlock_irqrestore(&port->lock, flags);
430 while (1) {
431 /* Indicate we are open */
432 if (C_BAUD(tty) && tty_port_initialized(port))
433 tty_port_raise_dtr_rts(port);
435 prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
436 /* Check for a hangup or uninitialised port.
437 Return accordingly */
438 if (tty_hung_up_p(filp) || !tty_port_initialized(port)) {
439 if (port->flags & ASYNC_HUP_NOTIFY)
440 retval = -EAGAIN;
441 else
442 retval = -ERESTARTSYS;
443 break;
446 * Probe the carrier. For devices with no carrier detect
447 * tty_port_carrier_raised will always return true.
448 * Never ask drivers if CLOCAL is set, this causes troubles
449 * on some hardware.
451 if (do_clocal || tty_port_carrier_raised(port))
452 break;
453 if (signal_pending(current)) {
454 retval = -ERESTARTSYS;
455 break;
457 tty_unlock(tty);
458 schedule();
459 tty_lock(tty);
461 finish_wait(&port->open_wait, &wait);
463 /* Update counts. A parallel hangup will have set count to zero and
464 we must not mess that up further */
465 spin_lock_irqsave(&port->lock, flags);
466 if (!tty_hung_up_p(filp))
467 port->count++;
468 port->blocked_open--;
469 spin_unlock_irqrestore(&port->lock, flags);
470 if (retval == 0)
471 tty_port_set_active(port, 1);
472 return retval;
474 EXPORT_SYMBOL(tty_port_block_til_ready);
476 static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty)
478 unsigned int bps = tty_get_baud_rate(tty);
479 long timeout;
481 if (bps > 1200) {
482 timeout = (HZ * 10 * port->drain_delay) / bps;
483 timeout = max_t(long, timeout, HZ / 10);
484 } else {
485 timeout = 2 * HZ;
487 schedule_timeout_interruptible(timeout);
490 /* Caller holds tty lock. */
491 int tty_port_close_start(struct tty_port *port,
492 struct tty_struct *tty, struct file *filp)
494 unsigned long flags;
496 if (tty_hung_up_p(filp))
497 return 0;
499 spin_lock_irqsave(&port->lock, flags);
500 if (tty->count == 1 && port->count != 1) {
501 tty_warn(tty, "%s: tty->count = 1 port count = %d\n", __func__,
502 port->count);
503 port->count = 1;
505 if (--port->count < 0) {
506 tty_warn(tty, "%s: bad port count (%d)\n", __func__,
507 port->count);
508 port->count = 0;
511 if (port->count) {
512 spin_unlock_irqrestore(&port->lock, flags);
513 return 0;
515 spin_unlock_irqrestore(&port->lock, flags);
517 tty->closing = 1;
519 if (tty_port_initialized(port)) {
520 /* Don't block on a stalled port, just pull the chain */
521 if (tty->flow_stopped)
522 tty_driver_flush_buffer(tty);
523 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
524 tty_wait_until_sent(tty, port->closing_wait);
525 if (port->drain_delay)
526 tty_port_drain_delay(port, tty);
528 /* Flush the ldisc buffering */
529 tty_ldisc_flush(tty);
531 /* Report to caller this is the last port reference */
532 return 1;
534 EXPORT_SYMBOL(tty_port_close_start);
536 /* Caller holds tty lock */
537 void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
539 unsigned long flags;
541 tty_ldisc_flush(tty);
542 tty->closing = 0;
544 spin_lock_irqsave(&port->lock, flags);
546 if (port->blocked_open) {
547 spin_unlock_irqrestore(&port->lock, flags);
548 if (port->close_delay)
549 msleep_interruptible(jiffies_to_msecs(port->close_delay));
550 spin_lock_irqsave(&port->lock, flags);
551 wake_up_interruptible(&port->open_wait);
553 spin_unlock_irqrestore(&port->lock, flags);
554 tty_port_set_active(port, 0);
556 EXPORT_SYMBOL(tty_port_close_end);
559 * tty_port_close
561 * Caller holds tty lock
563 void tty_port_close(struct tty_port *port, struct tty_struct *tty,
564 struct file *filp)
566 if (tty_port_close_start(port, tty, filp) == 0)
567 return;
568 tty_port_shutdown(port, tty);
569 set_bit(TTY_IO_ERROR, &tty->flags);
570 tty_port_close_end(port, tty);
571 tty_port_tty_set(port, NULL);
573 EXPORT_SYMBOL(tty_port_close);
576 * tty_port_install - generic tty->ops->install handler
577 * @port: tty_port of the device
578 * @driver: tty_driver for this device
579 * @tty: tty to be installed
581 * It is the same as tty_standard_install except the provided @port is linked
582 * to a concrete tty specified by @tty. Use this or tty_port_register_device
583 * (or both). Call tty_port_link_device as a last resort.
585 int tty_port_install(struct tty_port *port, struct tty_driver *driver,
586 struct tty_struct *tty)
588 tty->port = port;
589 return tty_standard_install(driver, tty);
591 EXPORT_SYMBOL_GPL(tty_port_install);
594 * tty_port_open
596 * Caller holds tty lock.
598 * NB: may drop and reacquire tty lock (in tty_port_block_til_ready()) so
599 * tty and tty_port may have changed state (eg., may be hung up now)
601 int tty_port_open(struct tty_port *port, struct tty_struct *tty,
602 struct file *filp)
604 spin_lock_irq(&port->lock);
605 ++port->count;
606 spin_unlock_irq(&port->lock);
607 tty_port_tty_set(port, tty);
610 * Do the device-specific open only if the hardware isn't
611 * already initialized. Serialize open and shutdown using the
612 * port mutex.
615 mutex_lock(&port->mutex);
617 if (!tty_port_initialized(port)) {
618 clear_bit(TTY_IO_ERROR, &tty->flags);
619 if (port->ops->activate) {
620 int retval = port->ops->activate(port, tty);
621 if (retval) {
622 mutex_unlock(&port->mutex);
623 return retval;
626 tty_port_set_initialized(port, 1);
628 mutex_unlock(&port->mutex);
629 return tty_port_block_til_ready(port, tty, filp);
632 EXPORT_SYMBOL(tty_port_open);