Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 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> |
| 10 | #include <linux/timer.h> |
| 11 | #include <linux/string.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/sched.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/wait.h> |
| 16 | #include <linux/bitops.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/module.h> |
| 19 | |
| 20 | void tty_port_init(struct tty_port *port) |
| 21 | { |
| 22 | memset(port, 0, sizeof(*port)); |
| 23 | init_waitqueue_head(&port->open_wait); |
| 24 | init_waitqueue_head(&port->close_wait); |
| 25 | mutex_init(&port->mutex); |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 26 | spin_lock_init(&port->lock); |
Alan Cox | 9e48565 | 2008-10-13 10:37:07 +0100 | [diff] [blame] | 27 | port->close_delay = (50 * HZ) / 100; |
| 28 | port->closing_wait = (3000 * HZ) / 100; |
| 29 | } |
| 30 | EXPORT_SYMBOL(tty_port_init); |
| 31 | |
| 32 | int tty_port_alloc_xmit_buf(struct tty_port *port) |
| 33 | { |
| 34 | /* We may sleep in get_zeroed_page() */ |
| 35 | mutex_lock(&port->mutex); |
| 36 | if (port->xmit_buf == NULL) |
| 37 | port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL); |
| 38 | mutex_unlock(&port->mutex); |
| 39 | if (port->xmit_buf == NULL) |
| 40 | return -ENOMEM; |
| 41 | return 0; |
| 42 | } |
| 43 | EXPORT_SYMBOL(tty_port_alloc_xmit_buf); |
| 44 | |
| 45 | void tty_port_free_xmit_buf(struct tty_port *port) |
| 46 | { |
| 47 | mutex_lock(&port->mutex); |
| 48 | if (port->xmit_buf != NULL) { |
| 49 | free_page((unsigned long)port->xmit_buf); |
| 50 | port->xmit_buf = NULL; |
| 51 | } |
| 52 | mutex_unlock(&port->mutex); |
| 53 | } |
| 54 | EXPORT_SYMBOL(tty_port_free_xmit_buf); |
| 55 | |
| 56 | |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 57 | /** |
| 58 | * tty_port_tty_get - get a tty reference |
| 59 | * @port: tty port |
| 60 | * |
| 61 | * Return a refcount protected tty instance or NULL if the port is not |
| 62 | * associated with a tty (eg due to close or hangup) |
| 63 | */ |
| 64 | |
| 65 | struct tty_struct *tty_port_tty_get(struct tty_port *port) |
| 66 | { |
| 67 | unsigned long flags; |
| 68 | struct tty_struct *tty; |
| 69 | |
| 70 | spin_lock_irqsave(&port->lock, flags); |
| 71 | tty = tty_kref_get(port->tty); |
| 72 | spin_unlock_irqrestore(&port->lock, flags); |
| 73 | return tty; |
| 74 | } |
| 75 | EXPORT_SYMBOL(tty_port_tty_get); |
| 76 | |
| 77 | /** |
| 78 | * tty_port_tty_set - set the tty of a port |
| 79 | * @port: tty port |
| 80 | * @tty: the tty |
| 81 | * |
| 82 | * Associate the port and tty pair. Manages any internal refcounts. |
| 83 | * Pass NULL to deassociate a port |
| 84 | */ |
| 85 | |
| 86 | void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty) |
| 87 | { |
| 88 | unsigned long flags; |
| 89 | |
| 90 | spin_lock_irqsave(&port->lock, flags); |
| 91 | if (port->tty) |
| 92 | tty_kref_put(port->tty); |
Alan Cox | cb4bca3 | 2008-10-21 13:47:44 +0100 | [diff] [blame] | 93 | port->tty = tty_kref_get(tty); |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 94 | spin_unlock_irqrestore(&port->lock, flags); |
| 95 | } |
| 96 | EXPORT_SYMBOL(tty_port_tty_set); |