123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597 |
- #include <linux/types.h>
- #include <linux/errno.h>
- #include <linux/tty.h>
- #include <linux/tty_driver.h>
- #include <linux/tty_flip.h>
- #include <linux/serial.h>
- #include <linux/timer.h>
- #include <linux/string.h>
- #include <linux/slab.h>
- #include <linux/sched.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));
- tty_buffer_init(port);
- init_waitqueue_head(&port->open_wait);
- init_waitqueue_head(&port->delta_msr_wait);
- mutex_init(&port->mutex);
- mutex_init(&port->buf_mutex);
- spin_lock_init(&port->lock);
- port->close_delay = (50 * HZ) / 100;
- port->closing_wait = (3000 * HZ) / 100;
- kref_init(&port->kref);
- }
- EXPORT_SYMBOL(tty_port_init);
- void tty_port_link_device(struct tty_port *port,
- struct tty_driver *driver, unsigned index)
- {
- if (WARN_ON(index >= driver->num))
- return;
- driver->ports[index] = port;
- }
- EXPORT_SYMBOL_GPL(tty_port_link_device);
- struct device *tty_port_register_device(struct tty_port *port,
- struct tty_driver *driver, unsigned index,
- struct device *device)
- {
- tty_port_link_device(port, driver, index);
- return tty_register_device(driver, index, device);
- }
- EXPORT_SYMBOL_GPL(tty_port_register_device);
- struct device *tty_port_register_device_attr(struct tty_port *port,
- struct tty_driver *driver, unsigned index,
- struct device *device, void *drvdata,
- const struct attribute_group **attr_grp)
- {
- tty_port_link_device(port, driver, index);
- return tty_register_device_attr(driver, index, device, drvdata,
- attr_grp);
- }
- EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
- int tty_port_alloc_xmit_buf(struct tty_port *port)
- {
-
- mutex_lock(&port->buf_mutex);
- if (port->xmit_buf == NULL)
- port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
- mutex_unlock(&port->buf_mutex);
- 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)
- {
- mutex_lock(&port->buf_mutex);
- if (port->xmit_buf != NULL) {
- free_page((unsigned long)port->xmit_buf);
- port->xmit_buf = NULL;
- }
- mutex_unlock(&port->buf_mutex);
- }
- EXPORT_SYMBOL(tty_port_free_xmit_buf);
- void tty_port_destroy(struct tty_port *port)
- {
- tty_buffer_cancel_work(port);
- tty_buffer_free_all(port);
- }
- EXPORT_SYMBOL(tty_port_destroy);
- static void tty_port_destructor(struct kref *kref)
- {
- struct tty_port *port = container_of(kref, struct tty_port, kref);
-
- if (WARN_ON(port->itty))
- return;
- if (port->xmit_buf)
- free_page((unsigned long)port->xmit_buf);
- tty_port_destroy(port);
- if (port->ops && 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);
- 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);
- void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
- {
- unsigned long flags;
- spin_lock_irqsave(&port->lock, flags);
- tty_kref_put(port->tty);
- port->tty = tty_kref_get(tty);
- spin_unlock_irqrestore(&port->lock, flags);
- }
- EXPORT_SYMBOL(tty_port_tty_set);
- static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty)
- {
- mutex_lock(&port->mutex);
- if (port->console)
- goto out;
- if (tty_port_initialized(port)) {
- tty_port_set_initialized(port, 0);
-
- if (tty && C_HUPCL(tty))
- tty_port_lower_dtr_rts(port);
- if (port->ops->shutdown)
- port->ops->shutdown(port);
- }
- out:
- mutex_unlock(&port->mutex);
- }
- void tty_port_hangup(struct tty_port *port)
- {
- struct tty_struct *tty;
- unsigned long flags;
- spin_lock_irqsave(&port->lock, flags);
- port->count = 0;
- tty = port->tty;
- if (tty)
- set_bit(TTY_IO_ERROR, &tty->flags);
- port->tty = NULL;
- spin_unlock_irqrestore(&port->lock, flags);
- tty_port_set_active(port, 0);
- tty_port_shutdown(port, tty);
- tty_kref_put(tty);
- wake_up_interruptible(&port->open_wait);
- wake_up_interruptible(&port->delta_msr_wait);
- }
- EXPORT_SYMBOL(tty_port_hangup);
- void tty_port_tty_hangup(struct tty_port *port, bool check_clocal)
- {
- struct tty_struct *tty = tty_port_tty_get(port);
- if (tty && (!check_clocal || !C_CLOCAL(tty)))
- tty_hangup(tty);
- tty_kref_put(tty);
- }
- EXPORT_SYMBOL_GPL(tty_port_tty_hangup);
- void tty_port_tty_wakeup(struct tty_port *port)
- {
- struct tty_struct *tty = tty_port_tty_get(port);
- if (tty) {
- tty_wakeup(tty);
- tty_kref_put(tty);
- }
- }
- EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
- 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);
- void tty_port_raise_dtr_rts(struct tty_port *port)
- {
- if (port->ops->dtr_rts)
- port->ops->dtr_rts(port, 1);
- }
- EXPORT_SYMBOL(tty_port_raise_dtr_rts);
- 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);
- 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;
- DEFINE_WAIT(wait);
-
- if (tty_io_error(tty)) {
- tty_port_set_active(port, 1);
- return 0;
- }
- if (filp->f_flags & O_NONBLOCK) {
-
- if (C_BAUD(tty))
- tty_port_raise_dtr_rts(port);
- tty_port_set_active(port, 1);
- return 0;
- }
- if (C_CLOCAL(tty))
- do_clocal = 1;
-
- retval = 0;
-
- spin_lock_irqsave(&port->lock, flags);
- port->count--;
- port->blocked_open++;
- spin_unlock_irqrestore(&port->lock, flags);
- while (1) {
-
- if (C_BAUD(tty) && tty_port_initialized(port))
- tty_port_raise_dtr_rts(port);
- prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
-
- if (tty_hung_up_p(filp) || !tty_port_initialized(port)) {
- if (port->flags & ASYNC_HUP_NOTIFY)
- retval = -EAGAIN;
- else
- retval = -ERESTARTSYS;
- break;
- }
-
- if (do_clocal || tty_port_carrier_raised(port))
- break;
- if (signal_pending(current)) {
- retval = -ERESTARTSYS;
- break;
- }
- tty_unlock(tty);
- schedule();
- tty_lock(tty);
- }
- finish_wait(&port->open_wait, &wait);
-
- spin_lock_irqsave(&port->lock, flags);
- if (!tty_hung_up_p(filp))
- port->count++;
- port->blocked_open--;
- spin_unlock_irqrestore(&port->lock, flags);
- if (retval == 0)
- tty_port_set_active(port, 1);
- return retval;
- }
- EXPORT_SYMBOL(tty_port_block_til_ready);
- static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty)
- {
- unsigned int bps = tty_get_baud_rate(tty);
- long timeout;
- if (bps > 1200) {
- timeout = (HZ * 10 * port->drain_delay) / bps;
- timeout = max_t(long, timeout, HZ / 10);
- } else {
- timeout = 2 * HZ;
- }
- schedule_timeout_interruptible(timeout);
- }
- int tty_port_close_start(struct tty_port *port,
- struct tty_struct *tty, struct file *filp)
- {
- unsigned long flags;
- if (tty_hung_up_p(filp))
- return 0;
- spin_lock_irqsave(&port->lock, flags);
- if (tty->count == 1 && port->count != 1) {
- tty_warn(tty, "%s: tty->count = 1 port count = %d\n", __func__,
- port->count);
- port->count = 1;
- }
- if (--port->count < 0) {
- tty_warn(tty, "%s: bad port count (%d)\n", __func__,
- port->count);
- port->count = 0;
- }
- if (port->count) {
- spin_unlock_irqrestore(&port->lock, flags);
- return 0;
- }
- spin_unlock_irqrestore(&port->lock, flags);
- tty->closing = 1;
- if (tty_port_initialized(port)) {
-
- if (tty->flow_stopped)
- tty_driver_flush_buffer(tty);
- if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
- tty_wait_until_sent(tty, port->closing_wait);
- if (port->drain_delay)
- tty_port_drain_delay(port, tty);
- }
-
- tty_ldisc_flush(tty);
-
- return 1;
- }
- EXPORT_SYMBOL(tty_port_close_start);
- void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
- {
- unsigned long flags;
- tty_ldisc_flush(tty);
- tty->closing = 0;
- spin_lock_irqsave(&port->lock, flags);
- 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);
- }
- spin_unlock_irqrestore(&port->lock, flags);
- tty_port_set_active(port, 0);
- }
- EXPORT_SYMBOL(tty_port_close_end);
- 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, tty);
- set_bit(TTY_IO_ERROR, &tty->flags);
- tty_port_close_end(port, tty);
- tty_port_tty_set(port, NULL);
- }
- EXPORT_SYMBOL(tty_port_close);
- 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);
- int tty_port_open(struct tty_port *port, struct tty_struct *tty,
- struct file *filp)
- {
- spin_lock_irq(&port->lock);
- ++port->count;
- spin_unlock_irq(&port->lock);
- tty_port_tty_set(port, tty);
-
- mutex_lock(&port->mutex);
- if (!tty_port_initialized(port)) {
- clear_bit(TTY_IO_ERROR, &tty->flags);
- if (port->ops->activate) {
- int retval = port->ops->activate(port, tty);
- if (retval) {
- mutex_unlock(&port->mutex);
- return retval;
- }
- }
- tty_port_set_initialized(port, 1);
- }
- mutex_unlock(&port->mutex);
- return tty_port_block_til_ready(port, tty, filp);
- }
- EXPORT_SYMBOL(tty_port_open);
|