aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/tty
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2012-05-22 19:12:24 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2012-05-22 19:12:24 -0400
commit94b5aff4c6f72fee6b0f49d49e4fa8b204e8ded9 (patch)
tree39197121b6ef8cddaa0f4057fe24b4ced58e8982 /drivers/tty
parent5d4e2d08e7fdf7339f84a1c670d296a77e02f881 (diff)
parent59bd234b72fc29887839d792b7d6c7e8d2a577a6 (diff)
Merge tag 'tty-3.5-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty
Pull TTY updates from Greg Kroah-Hartman: "Here's the big TTY/serial driver pull request for the 3.5-rc1 merge window. Nothing major in here, just lots of incremental changes from Alan and Jiri reworking some tty core things to behave better and to get a more solid grasp on some of the nasty tty locking issues. There are a few tty and serial driver updates in here as well. All of this has been in the linux-next releases for a while with no problems. Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>" * tag 'tty-3.5-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty: (115 commits) serial: bfin_uart: Make MMR access compatible with 32 bits bf609 style controller. serial: bfin_uart: RTS and CTS MMRs can be either 16-bit width or 32-bit width. serial: bfin_uart: narrow the reboot condition in DMA tx interrupt serial: bfin_uart: Adapt bf5xx serial driver to bf60x serial4 controller. Revert "serial_core: Update buffer overrun statistics." tty: hvc_xen: NULL dereference on allocation failure tty: Fix LED error return tty: Allow uart_register/unregister/register tty: move global ldisc idle waitqueue to the individual ldisc serial8250-em: Add DT support serial8250-em: clk_get() IS_ERR() error handling fix serial_core: Update buffer overrun statistics. tty: drop the pty lock during hangup cris: fix missing tty arg in wait_event_interruptible_tty call tty/amiserial: Add missing argument for tty_unlock() tty_lock: Localise the lock pty: Lock the devpts bits privately tty_lock: undo the old tty_lock use on the ctty serial8250-em: Emma Mobile UART driver V2 Add missing call to uart_update_timeout() ...
Diffstat (limited to 'drivers/tty')
-rw-r--r--drivers/tty/amiserial.c14
-rw-r--r--drivers/tty/bfin_jtag_comm.c44
-rw-r--r--drivers/tty/cyclades.c2
-rw-r--r--drivers/tty/hvc/hvc_console.c96
-rw-r--r--drivers/tty/hvc/hvc_console.h4
-rw-r--r--drivers/tty/hvc/hvc_xen.c4
-rw-r--r--drivers/tty/hvc/hvcs.c74
-rw-r--r--drivers/tty/hvc/hvsi.c128
-rw-r--r--drivers/tty/hvc/hvsi_lib.c2
-rw-r--r--drivers/tty/ipwireless/tty.c81
-rw-r--r--drivers/tty/mxser.c2
-rw-r--r--drivers/tty/n_r3964.c11
-rw-r--r--drivers/tty/n_tty.c9
-rw-r--r--drivers/tty/pty.c40
-rw-r--r--drivers/tty/serial/68328serial.c383
-rw-r--r--drivers/tty/serial/68328serial.h186
-rw-r--r--drivers/tty/serial/8250/8250.c312
-rw-r--r--drivers/tty/serial/8250/8250.h16
-rw-r--r--drivers/tty/serial/8250/8250_em.c186
-rw-r--r--drivers/tty/serial/8250/8250_pci.c63
-rw-r--r--drivers/tty/serial/8250/Kconfig8
-rw-r--r--drivers/tty/serial/8250/Makefile1
-rw-r--r--drivers/tty/serial/amba-pl011.c109
-rw-r--r--drivers/tty/serial/bfin_uart.c74
-rw-r--r--drivers/tty/serial/crisv10.c36
-rw-r--r--drivers/tty/serial/mxs-auart.c2
-rw-r--r--drivers/tty/serial/of_serial.c26
-rw-r--r--drivers/tty/serial/omap-serial.c74
-rw-r--r--drivers/tty/serial/pch_uart.c53
-rw-r--r--drivers/tty/serial/serial_core.c1
-rw-r--r--drivers/tty/synclink.c4
-rw-r--r--drivers/tty/synclink_gt.c4
-rw-r--r--drivers/tty/synclinkmp.c4
-rw-r--r--drivers/tty/tty_buffer.c85
-rw-r--r--drivers/tty/tty_io.c76
-rw-r--r--drivers/tty/tty_ldisc.c37
-rw-r--r--drivers/tty/tty_mutex.c60
-rw-r--r--drivers/tty/tty_port.c6
-rw-r--r--drivers/tty/vt/consolemap.c123
-rw-r--r--drivers/tty/vt/vt.c68
-rw-r--r--drivers/tty/vt/vt_ioctl.c25
41 files changed, 1316 insertions, 1217 deletions
diff --git a/drivers/tty/amiserial.c b/drivers/tty/amiserial.c
index 6cc4358f68c1..35819e312624 100644
--- a/drivers/tty/amiserial.c
+++ b/drivers/tty/amiserial.c
@@ -1033,7 +1033,7 @@ static int get_serial_info(struct tty_struct *tty, struct serial_state *state,
1033 if (!retinfo) 1033 if (!retinfo)
1034 return -EFAULT; 1034 return -EFAULT;
1035 memset(&tmp, 0, sizeof(tmp)); 1035 memset(&tmp, 0, sizeof(tmp));
1036 tty_lock(); 1036 tty_lock(tty);
1037 tmp.line = tty->index; 1037 tmp.line = tty->index;
1038 tmp.port = state->port; 1038 tmp.port = state->port;
1039 tmp.flags = state->tport.flags; 1039 tmp.flags = state->tport.flags;
@@ -1042,7 +1042,7 @@ static int get_serial_info(struct tty_struct *tty, struct serial_state *state,
1042 tmp.close_delay = state->tport.close_delay; 1042 tmp.close_delay = state->tport.close_delay;
1043 tmp.closing_wait = state->tport.closing_wait; 1043 tmp.closing_wait = state->tport.closing_wait;
1044 tmp.custom_divisor = state->custom_divisor; 1044 tmp.custom_divisor = state->custom_divisor;
1045 tty_unlock(); 1045 tty_unlock(tty);
1046 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo))) 1046 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1047 return -EFAULT; 1047 return -EFAULT;
1048 return 0; 1048 return 0;
@@ -1059,12 +1059,12 @@ static int set_serial_info(struct tty_struct *tty, struct serial_state *state,
1059 if (copy_from_user(&new_serial,new_info,sizeof(new_serial))) 1059 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1060 return -EFAULT; 1060 return -EFAULT;
1061 1061
1062 tty_lock(); 1062 tty_lock(tty);
1063 change_spd = ((new_serial.flags ^ port->flags) & ASYNC_SPD_MASK) || 1063 change_spd = ((new_serial.flags ^ port->flags) & ASYNC_SPD_MASK) ||
1064 new_serial.custom_divisor != state->custom_divisor; 1064 new_serial.custom_divisor != state->custom_divisor;
1065 if (new_serial.irq || new_serial.port != state->port || 1065 if (new_serial.irq || new_serial.port != state->port ||
1066 new_serial.xmit_fifo_size != state->xmit_fifo_size) { 1066 new_serial.xmit_fifo_size != state->xmit_fifo_size) {
1067 tty_unlock(); 1067 tty_unlock(tty);
1068 return -EINVAL; 1068 return -EINVAL;
1069 } 1069 }
1070 1070
@@ -1074,7 +1074,7 @@ static int set_serial_info(struct tty_struct *tty, struct serial_state *state,
1074 (new_serial.xmit_fifo_size != state->xmit_fifo_size) || 1074 (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1075 ((new_serial.flags & ~ASYNC_USR_MASK) != 1075 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1076 (port->flags & ~ASYNC_USR_MASK))) { 1076 (port->flags & ~ASYNC_USR_MASK))) {
1077 tty_unlock(); 1077 tty_unlock(tty);
1078 return -EPERM; 1078 return -EPERM;
1079 } 1079 }
1080 port->flags = ((port->flags & ~ASYNC_USR_MASK) | 1080 port->flags = ((port->flags & ~ASYNC_USR_MASK) |
@@ -1084,7 +1084,7 @@ static int set_serial_info(struct tty_struct *tty, struct serial_state *state,
1084 } 1084 }
1085 1085
1086 if (new_serial.baud_base < 9600) { 1086 if (new_serial.baud_base < 9600) {
1087 tty_unlock(); 1087 tty_unlock(tty);
1088 return -EINVAL; 1088 return -EINVAL;
1089 } 1089 }
1090 1090
@@ -1116,7 +1116,7 @@ check_and_exit:
1116 } 1116 }
1117 } else 1117 } else
1118 retval = startup(tty, state); 1118 retval = startup(tty, state);
1119 tty_unlock(); 1119 tty_unlock(tty);
1120 return retval; 1120 return retval;
1121} 1121}
1122 1122
diff --git a/drivers/tty/bfin_jtag_comm.c b/drivers/tty/bfin_jtag_comm.c
index 946f799861f5..61fc74fe1747 100644
--- a/drivers/tty/bfin_jtag_comm.c
+++ b/drivers/tty/bfin_jtag_comm.c
@@ -62,9 +62,7 @@ static inline uint32_t bfin_write_emudat_chars(char a, char b, char c, char d)
62 62
63static struct tty_driver *bfin_jc_driver; 63static struct tty_driver *bfin_jc_driver;
64static struct task_struct *bfin_jc_kthread; 64static struct task_struct *bfin_jc_kthread;
65static struct tty_struct * volatile bfin_jc_tty; 65static struct tty_port port;
66static unsigned long bfin_jc_count;
67static DEFINE_MUTEX(bfin_jc_tty_mutex);
68static volatile struct circ_buf bfin_jc_write_buf; 66static volatile struct circ_buf bfin_jc_write_buf;
69 67
70static int 68static int
@@ -73,18 +71,21 @@ bfin_jc_emudat_manager(void *arg)
73 uint32_t inbound_len = 0, outbound_len = 0; 71 uint32_t inbound_len = 0, outbound_len = 0;
74 72
75 while (!kthread_should_stop()) { 73 while (!kthread_should_stop()) {
74 struct tty_struct *tty = tty_port_tty_get(&port);
76 /* no one left to give data to, so sleep */ 75 /* no one left to give data to, so sleep */
77 if (bfin_jc_tty == NULL && circ_empty(&bfin_jc_write_buf)) { 76 if (tty == NULL && circ_empty(&bfin_jc_write_buf)) {
78 pr_debug("waiting for readers\n"); 77 pr_debug("waiting for readers\n");
79 __set_current_state(TASK_UNINTERRUPTIBLE); 78 __set_current_state(TASK_UNINTERRUPTIBLE);
80 schedule(); 79 schedule();
81 __set_current_state(TASK_RUNNING); 80 __set_current_state(TASK_RUNNING);
81 continue;
82 } 82 }
83 83
84 /* no data available, so just chill */ 84 /* no data available, so just chill */
85 if (!(bfin_read_DBGSTAT() & EMUDIF) && circ_empty(&bfin_jc_write_buf)) { 85 if (!(bfin_read_DBGSTAT() & EMUDIF) && circ_empty(&bfin_jc_write_buf)) {
86 pr_debug("waiting for data (in_len = %i) (circ: %i %i)\n", 86 pr_debug("waiting for data (in_len = %i) (circ: %i %i)\n",
87 inbound_len, bfin_jc_write_buf.tail, bfin_jc_write_buf.head); 87 inbound_len, bfin_jc_write_buf.tail, bfin_jc_write_buf.head);
88 tty_kref_put(tty);
88 if (inbound_len) 89 if (inbound_len)
89 schedule(); 90 schedule();
90 else 91 else
@@ -94,9 +95,6 @@ bfin_jc_emudat_manager(void *arg)
94 95
95 /* if incoming data is ready, eat it */ 96 /* if incoming data is ready, eat it */
96 if (bfin_read_DBGSTAT() & EMUDIF) { 97 if (bfin_read_DBGSTAT() & EMUDIF) {
97 struct tty_struct *tty;
98 mutex_lock(&bfin_jc_tty_mutex);
99 tty = (struct tty_struct *)bfin_jc_tty;
100 if (tty != NULL) { 98 if (tty != NULL) {
101 uint32_t emudat = bfin_read_emudat(); 99 uint32_t emudat = bfin_read_emudat();
102 if (inbound_len == 0) { 100 if (inbound_len == 0) {
@@ -110,7 +108,6 @@ bfin_jc_emudat_manager(void *arg)
110 tty_flip_buffer_push(tty); 108 tty_flip_buffer_push(tty);
111 } 109 }
112 } 110 }
113 mutex_unlock(&bfin_jc_tty_mutex);
114 } 111 }
115 112
116 /* if outgoing data is ready, post it */ 113 /* if outgoing data is ready, post it */
@@ -120,7 +117,6 @@ bfin_jc_emudat_manager(void *arg)
120 bfin_write_emudat(outbound_len); 117 bfin_write_emudat(outbound_len);
121 pr_debug("outgoing length: 0x%08x\n", outbound_len); 118 pr_debug("outgoing length: 0x%08x\n", outbound_len);
122 } else { 119 } else {
123 struct tty_struct *tty;
124 int tail = bfin_jc_write_buf.tail; 120 int tail = bfin_jc_write_buf.tail;
125 size_t ate = (4 <= outbound_len ? 4 : outbound_len); 121 size_t ate = (4 <= outbound_len ? 4 : outbound_len);
126 uint32_t emudat = 122 uint32_t emudat =
@@ -132,14 +128,12 @@ bfin_jc_emudat_manager(void *arg)
132 ); 128 );
133 bfin_jc_write_buf.tail += ate; 129 bfin_jc_write_buf.tail += ate;
134 outbound_len -= ate; 130 outbound_len -= ate;
135 mutex_lock(&bfin_jc_tty_mutex);
136 tty = (struct tty_struct *)bfin_jc_tty;
137 if (tty) 131 if (tty)
138 tty_wakeup(tty); 132 tty_wakeup(tty);
139 mutex_unlock(&bfin_jc_tty_mutex);
140 pr_debug(" outgoing data: 0x%08x (pushing %zu)\n", emudat, ate); 133 pr_debug(" outgoing data: 0x%08x (pushing %zu)\n", emudat, ate);
141 } 134 }
142 } 135 }
136 tty_kref_put(tty);
143 } 137 }
144 138
145 __set_current_state(TASK_RUNNING); 139 __set_current_state(TASK_RUNNING);
@@ -149,24 +143,28 @@ bfin_jc_emudat_manager(void *arg)
149static int 143static int
150bfin_jc_open(struct tty_struct *tty, struct file *filp) 144bfin_jc_open(struct tty_struct *tty, struct file *filp)
151{ 145{
152 mutex_lock(&bfin_jc_tty_mutex); 146 unsigned long flags;
153 pr_debug("open %lu\n", bfin_jc_count); 147
154 ++bfin_jc_count; 148 spin_lock_irqsave(&port.lock, flags);
155 bfin_jc_tty = tty; 149 port.count++;
150 spin_unlock_irqrestore(&port.lock, flags);
151 tty_port_tty_set(&port, tty);
156 wake_up_process(bfin_jc_kthread); 152 wake_up_process(bfin_jc_kthread);
157 mutex_unlock(&bfin_jc_tty_mutex);
158 return 0; 153 return 0;
159} 154}
160 155
161static void 156static void
162bfin_jc_close(struct tty_struct *tty, struct file *filp) 157bfin_jc_close(struct tty_struct *tty, struct file *filp)
163{ 158{
164 mutex_lock(&bfin_jc_tty_mutex); 159 unsigned long flags;
165 pr_debug("close %lu\n", bfin_jc_count); 160 bool last;
166 if (--bfin_jc_count == 0) 161
167 bfin_jc_tty = NULL; 162 spin_lock_irqsave(&port.lock, flags);
163 last = --port.count == 0;
164 spin_unlock_irqrestore(&port.lock, flags);
165 if (last)
166 tty_port_tty_set(&port, NULL);
168 wake_up_process(bfin_jc_kthread); 167 wake_up_process(bfin_jc_kthread);
169 mutex_unlock(&bfin_jc_tty_mutex);
170} 168}
171 169
172/* XXX: we dont handle the put_char() case where we must handle count = 1 */ 170/* XXX: we dont handle the put_char() case where we must handle count = 1 */
@@ -242,6 +240,8 @@ static int __init bfin_jc_init(void)
242{ 240{
243 int ret; 241 int ret;
244 242
243 tty_port_init(&port);
244
245 bfin_jc_kthread = kthread_create(bfin_jc_emudat_manager, NULL, DRV_NAME); 245 bfin_jc_kthread = kthread_create(bfin_jc_emudat_manager, NULL, DRV_NAME);
246 if (IS_ERR(bfin_jc_kthread)) 246 if (IS_ERR(bfin_jc_kthread))
247 return PTR_ERR(bfin_jc_kthread); 247 return PTR_ERR(bfin_jc_kthread);
diff --git a/drivers/tty/cyclades.c b/drivers/tty/cyclades.c
index e61cabdd69df..6984e1a2686a 100644
--- a/drivers/tty/cyclades.c
+++ b/drivers/tty/cyclades.c
@@ -1599,7 +1599,7 @@ static int cy_open(struct tty_struct *tty, struct file *filp)
1599 * If the port is the middle of closing, bail out now 1599 * If the port is the middle of closing, bail out now
1600 */ 1600 */
1601 if (tty_hung_up_p(filp) || (info->port.flags & ASYNC_CLOSING)) { 1601 if (tty_hung_up_p(filp) || (info->port.flags & ASYNC_CLOSING)) {
1602 wait_event_interruptible_tty(info->port.close_wait, 1602 wait_event_interruptible_tty(tty, info->port.close_wait,
1603 !(info->port.flags & ASYNC_CLOSING)); 1603 !(info->port.flags & ASYNC_CLOSING));
1604 return (info->port.flags & ASYNC_HUP_NOTIFY) ? -EAGAIN: -ERESTARTSYS; 1604 return (info->port.flags & ASYNC_HUP_NOTIFY) ? -EAGAIN: -ERESTARTSYS;
1605 } 1605 }
diff --git a/drivers/tty/hvc/hvc_console.c b/drivers/tty/hvc/hvc_console.c
index 8880adf5fc6f..2d691eb7c40a 100644
--- a/drivers/tty/hvc/hvc_console.c
+++ b/drivers/tty/hvc/hvc_console.c
@@ -107,7 +107,7 @@ static struct hvc_struct *hvc_get_by_index(int index)
107 list_for_each_entry(hp, &hvc_structs, next) { 107 list_for_each_entry(hp, &hvc_structs, next) {
108 spin_lock_irqsave(&hp->lock, flags); 108 spin_lock_irqsave(&hp->lock, flags);
109 if (hp->index == index) { 109 if (hp->index == index) {
110 kref_get(&hp->kref); 110 tty_port_get(&hp->port);
111 spin_unlock_irqrestore(&hp->lock, flags); 111 spin_unlock_irqrestore(&hp->lock, flags);
112 spin_unlock(&hvc_structs_lock); 112 spin_unlock(&hvc_structs_lock);
113 return hp; 113 return hp;
@@ -229,9 +229,9 @@ static int __init hvc_console_init(void)
229console_initcall(hvc_console_init); 229console_initcall(hvc_console_init);
230 230
231/* callback when the kboject ref count reaches zero. */ 231/* callback when the kboject ref count reaches zero. */
232static void destroy_hvc_struct(struct kref *kref) 232static void hvc_port_destruct(struct tty_port *port)
233{ 233{
234 struct hvc_struct *hp = container_of(kref, struct hvc_struct, kref); 234 struct hvc_struct *hp = container_of(port, struct hvc_struct, port);
235 unsigned long flags; 235 unsigned long flags;
236 236
237 spin_lock(&hvc_structs_lock); 237 spin_lock(&hvc_structs_lock);
@@ -264,7 +264,7 @@ int hvc_instantiate(uint32_t vtermno, int index, const struct hv_ops *ops)
264 /* make sure no no tty has been registered in this index */ 264 /* make sure no no tty has been registered in this index */
265 hp = hvc_get_by_index(index); 265 hp = hvc_get_by_index(index);
266 if (hp) { 266 if (hp) {
267 kref_put(&hp->kref, destroy_hvc_struct); 267 tty_port_put(&hp->port);
268 return -1; 268 return -1;
269 } 269 }
270 270
@@ -313,20 +313,17 @@ static int hvc_open(struct tty_struct *tty, struct file * filp)
313 if (!(hp = hvc_get_by_index(tty->index))) 313 if (!(hp = hvc_get_by_index(tty->index)))
314 return -ENODEV; 314 return -ENODEV;
315 315
316 spin_lock_irqsave(&hp->lock, flags); 316 spin_lock_irqsave(&hp->port.lock, flags);
317 /* Check and then increment for fast path open. */ 317 /* Check and then increment for fast path open. */
318 if (hp->count++ > 0) { 318 if (hp->port.count++ > 0) {
319 tty_kref_get(tty); 319 spin_unlock_irqrestore(&hp->port.lock, flags);
320 spin_unlock_irqrestore(&hp->lock, flags);
321 hvc_kick(); 320 hvc_kick();
322 return 0; 321 return 0;
323 } /* else count == 0 */ 322 } /* else count == 0 */
323 spin_unlock_irqrestore(&hp->port.lock, flags);
324 324
325 tty->driver_data = hp; 325 tty->driver_data = hp;
326 326 tty_port_tty_set(&hp->port, tty);
327 hp->tty = tty_kref_get(tty);
328
329 spin_unlock_irqrestore(&hp->lock, flags);
330 327
331 if (hp->ops->notifier_add) 328 if (hp->ops->notifier_add)
332 rc = hp->ops->notifier_add(hp, hp->data); 329 rc = hp->ops->notifier_add(hp, hp->data);
@@ -338,12 +335,9 @@ static int hvc_open(struct tty_struct *tty, struct file * filp)
338 * tty fields and return the kref reference. 335 * tty fields and return the kref reference.
339 */ 336 */
340 if (rc) { 337 if (rc) {
341 spin_lock_irqsave(&hp->lock, flags); 338 tty_port_tty_set(&hp->port, NULL);
342 hp->tty = NULL;
343 spin_unlock_irqrestore(&hp->lock, flags);
344 tty_kref_put(tty);
345 tty->driver_data = NULL; 339 tty->driver_data = NULL;
346 kref_put(&hp->kref, destroy_hvc_struct); 340 tty_port_put(&hp->port);
347 printk(KERN_ERR "hvc_open: request_irq failed with rc %d.\n", rc); 341 printk(KERN_ERR "hvc_open: request_irq failed with rc %d.\n", rc);
348 } 342 }
349 /* Force wakeup of the polling thread */ 343 /* Force wakeup of the polling thread */
@@ -370,12 +364,12 @@ static void hvc_close(struct tty_struct *tty, struct file * filp)
370 364
371 hp = tty->driver_data; 365 hp = tty->driver_data;
372 366
373 spin_lock_irqsave(&hp->lock, flags); 367 spin_lock_irqsave(&hp->port.lock, flags);
374 368
375 if (--hp->count == 0) { 369 if (--hp->port.count == 0) {
370 spin_unlock_irqrestore(&hp->port.lock, flags);
376 /* We are done with the tty pointer now. */ 371 /* We are done with the tty pointer now. */
377 hp->tty = NULL; 372 tty_port_tty_set(&hp->port, NULL);
378 spin_unlock_irqrestore(&hp->lock, flags);
379 373
380 if (hp->ops->notifier_del) 374 if (hp->ops->notifier_del)
381 hp->ops->notifier_del(hp, hp->data); 375 hp->ops->notifier_del(hp, hp->data);
@@ -390,14 +384,13 @@ static void hvc_close(struct tty_struct *tty, struct file * filp)
390 */ 384 */
391 tty_wait_until_sent_from_close(tty, HVC_CLOSE_WAIT); 385 tty_wait_until_sent_from_close(tty, HVC_CLOSE_WAIT);
392 } else { 386 } else {
393 if (hp->count < 0) 387 if (hp->port.count < 0)
394 printk(KERN_ERR "hvc_close %X: oops, count is %d\n", 388 printk(KERN_ERR "hvc_close %X: oops, count is %d\n",
395 hp->vtermno, hp->count); 389 hp->vtermno, hp->port.count);
396 spin_unlock_irqrestore(&hp->lock, flags); 390 spin_unlock_irqrestore(&hp->port.lock, flags);
397 } 391 }
398 392
399 tty_kref_put(tty); 393 tty_port_put(&hp->port);
400 kref_put(&hp->kref, destroy_hvc_struct);
401} 394}
402 395
403static void hvc_hangup(struct tty_struct *tty) 396static void hvc_hangup(struct tty_struct *tty)
@@ -412,32 +405,31 @@ static void hvc_hangup(struct tty_struct *tty)
412 /* cancel pending tty resize work */ 405 /* cancel pending tty resize work */
413 cancel_work_sync(&hp->tty_resize); 406 cancel_work_sync(&hp->tty_resize);
414 407
415 spin_lock_irqsave(&hp->lock, flags); 408 spin_lock_irqsave(&hp->port.lock, flags);
416 409
417 /* 410 /*
418 * The N_TTY line discipline has problems such that in a close vs 411 * The N_TTY line discipline has problems such that in a close vs
419 * open->hangup case this can be called after the final close so prevent 412 * open->hangup case this can be called after the final close so prevent
420 * that from happening for now. 413 * that from happening for now.
421 */ 414 */
422 if (hp->count <= 0) { 415 if (hp->port.count <= 0) {
423 spin_unlock_irqrestore(&hp->lock, flags); 416 spin_unlock_irqrestore(&hp->port.lock, flags);
424 return; 417 return;
425 } 418 }
426 419
427 temp_open_count = hp->count; 420 temp_open_count = hp->port.count;
428 hp->count = 0; 421 hp->port.count = 0;
429 hp->n_outbuf = 0; 422 spin_unlock_irqrestore(&hp->port.lock, flags);
430 hp->tty = NULL; 423 tty_port_tty_set(&hp->port, NULL);
431 424
432 spin_unlock_irqrestore(&hp->lock, flags); 425 hp->n_outbuf = 0;
433 426
434 if (hp->ops->notifier_hangup) 427 if (hp->ops->notifier_hangup)
435 hp->ops->notifier_hangup(hp, hp->data); 428 hp->ops->notifier_hangup(hp, hp->data);
436 429
437 while(temp_open_count) { 430 while(temp_open_count) {
438 --temp_open_count; 431 --temp_open_count;
439 tty_kref_put(tty); 432 tty_port_put(&hp->port);
440 kref_put(&hp->kref, destroy_hvc_struct);
441 } 433 }
442} 434}
443 435
@@ -478,7 +470,8 @@ static int hvc_write(struct tty_struct *tty, const unsigned char *buf, int count
478 if (!hp) 470 if (!hp)
479 return -EPIPE; 471 return -EPIPE;
480 472
481 if (hp->count <= 0) 473 /* FIXME what's this (unprotected) check for? */
474 if (hp->port.count <= 0)
482 return -EIO; 475 return -EIO;
483 476
484 spin_lock_irqsave(&hp->lock, flags); 477 spin_lock_irqsave(&hp->lock, flags);
@@ -526,13 +519,12 @@ static void hvc_set_winsz(struct work_struct *work)
526 519
527 hp = container_of(work, struct hvc_struct, tty_resize); 520 hp = container_of(work, struct hvc_struct, tty_resize);
528 521
529 spin_lock_irqsave(&hp->lock, hvc_flags); 522 tty = tty_port_tty_get(&hp->port);
530 if (!hp->tty) { 523 if (!tty)
531 spin_unlock_irqrestore(&hp->lock, hvc_flags);
532 return; 524 return;
533 } 525
534 ws = hp->ws; 526 spin_lock_irqsave(&hp->lock, hvc_flags);
535 tty = tty_kref_get(hp->tty); 527 ws = hp->ws;
536 spin_unlock_irqrestore(&hp->lock, hvc_flags); 528 spin_unlock_irqrestore(&hp->lock, hvc_flags);
537 529
538 tty_do_resize(tty, &ws); 530 tty_do_resize(tty, &ws);
@@ -601,7 +593,7 @@ int hvc_poll(struct hvc_struct *hp)
601 } 593 }
602 594
603 /* No tty attached, just skip */ 595 /* No tty attached, just skip */
604 tty = tty_kref_get(hp->tty); 596 tty = tty_port_tty_get(&hp->port);
605 if (tty == NULL) 597 if (tty == NULL)
606 goto bail; 598 goto bail;
607 599
@@ -681,8 +673,7 @@ int hvc_poll(struct hvc_struct *hp)
681 673
682 tty_flip_buffer_push(tty); 674 tty_flip_buffer_push(tty);
683 } 675 }
684 if (tty) 676 tty_kref_put(tty);
685 tty_kref_put(tty);
686 677
687 return poll_mask; 678 return poll_mask;
688} 679}
@@ -817,6 +808,10 @@ static const struct tty_operations hvc_ops = {
817#endif 808#endif
818}; 809};
819 810
811static const struct tty_port_operations hvc_port_ops = {
812 .destruct = hvc_port_destruct,
813};
814
820struct hvc_struct *hvc_alloc(uint32_t vtermno, int data, 815struct hvc_struct *hvc_alloc(uint32_t vtermno, int data,
821 const struct hv_ops *ops, 816 const struct hv_ops *ops,
822 int outbuf_size) 817 int outbuf_size)
@@ -842,7 +837,8 @@ struct hvc_struct *hvc_alloc(uint32_t vtermno, int data,
842 hp->outbuf_size = outbuf_size; 837 hp->outbuf_size = outbuf_size;
843 hp->outbuf = &((char *)hp)[ALIGN(sizeof(*hp), sizeof(long))]; 838 hp->outbuf = &((char *)hp)[ALIGN(sizeof(*hp), sizeof(long))];
844 839
845 kref_init(&hp->kref); 840 tty_port_init(&hp->port);
841 hp->port.ops = &hvc_port_ops;
846 842
847 INIT_WORK(&hp->tty_resize, hvc_set_winsz); 843 INIT_WORK(&hp->tty_resize, hvc_set_winsz);
848 spin_lock_init(&hp->lock); 844 spin_lock_init(&hp->lock);
@@ -875,9 +871,9 @@ int hvc_remove(struct hvc_struct *hp)
875 unsigned long flags; 871 unsigned long flags;
876 struct tty_struct *tty; 872 struct tty_struct *tty;
877 873
878 spin_lock_irqsave(&hp->lock, flags); 874 tty = tty_port_tty_get(&hp->port);
879 tty = tty_kref_get(hp->tty);
880 875
876 spin_lock_irqsave(&hp->lock, flags);
881 if (hp->index < MAX_NR_HVC_CONSOLES) 877 if (hp->index < MAX_NR_HVC_CONSOLES)
882 vtermnos[hp->index] = -1; 878 vtermnos[hp->index] = -1;
883 879
@@ -891,7 +887,7 @@ int hvc_remove(struct hvc_struct *hp)
891 * kref cause it to be removed, which will probably be the tty_vhangup 887 * kref cause it to be removed, which will probably be the tty_vhangup
892 * below. 888 * below.
893 */ 889 */
894 kref_put(&hp->kref, destroy_hvc_struct); 890 tty_port_put(&hp->port);
895 891
896 /* 892 /*
897 * This function call will auto chain call hvc_hangup. 893 * This function call will auto chain call hvc_hangup.
diff --git a/drivers/tty/hvc/hvc_console.h b/drivers/tty/hvc/hvc_console.h
index c335a1492a54..674d23cb919a 100644
--- a/drivers/tty/hvc/hvc_console.h
+++ b/drivers/tty/hvc/hvc_console.h
@@ -46,10 +46,9 @@
46#define HVC_ALLOC_TTY_ADAPTERS 8 46#define HVC_ALLOC_TTY_ADAPTERS 8
47 47
48struct hvc_struct { 48struct hvc_struct {
49 struct tty_port port;
49 spinlock_t lock; 50 spinlock_t lock;
50 int index; 51 int index;
51 struct tty_struct *tty;
52 int count;
53 int do_wakeup; 52 int do_wakeup;
54 char *outbuf; 53 char *outbuf;
55 int outbuf_size; 54 int outbuf_size;
@@ -61,7 +60,6 @@ struct hvc_struct {
61 struct winsize ws; 60 struct winsize ws;
62 struct work_struct tty_resize; 61 struct work_struct tty_resize;
63 struct list_head next; 62 struct list_head next;
64 struct kref kref; /* ref count & hvc_struct lifetime */
65}; 63};
66 64
67/* implemented by a low level driver */ 65/* implemented by a low level driver */
diff --git a/drivers/tty/hvc/hvc_xen.c b/drivers/tty/hvc/hvc_xen.c
index 83d5c88e7165..d3d91dae065c 100644
--- a/drivers/tty/hvc/hvc_xen.c
+++ b/drivers/tty/hvc/hvc_xen.c
@@ -430,9 +430,9 @@ static int __devinit xencons_probe(struct xenbus_device *dev,
430 if (devid == 0) 430 if (devid == 0)
431 return -ENODEV; 431 return -ENODEV;
432 432
433 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL | __GFP_ZERO); 433 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
434 if (!info) 434 if (!info)
435 goto error_nomem; 435 return -ENOMEM;
436 dev_set_drvdata(&dev->dev, info); 436 dev_set_drvdata(&dev->dev, info);
437 info->xbdev = dev; 437 info->xbdev = dev;
438 info->vtermno = xenbus_devid_to_vtermno(devid); 438 info->vtermno = xenbus_devid_to_vtermno(devid);
diff --git a/drivers/tty/hvc/hvcs.c b/drivers/tty/hvc/hvcs.c
index 3436436fe2d7..d56788c83974 100644
--- a/drivers/tty/hvc/hvcs.c
+++ b/drivers/tty/hvc/hvcs.c
@@ -261,6 +261,7 @@ static DEFINE_SPINLOCK(hvcs_pi_lock);
261 261
262/* One vty-server per hvcs_struct */ 262/* One vty-server per hvcs_struct */
263struct hvcs_struct { 263struct hvcs_struct {
264 struct tty_port port;
264 spinlock_t lock; 265 spinlock_t lock;
265 266
266 /* 267 /*
@@ -269,9 +270,6 @@ struct hvcs_struct {
269 */ 270 */
270 unsigned int index; 271 unsigned int index;
271 272
272 struct tty_struct *tty;
273 int open_count;
274
275 /* 273 /*
276 * Used to tell the driver kernel_thread what operations need to take 274 * Used to tell the driver kernel_thread what operations need to take
277 * place upon this hvcs_struct instance. 275 * place upon this hvcs_struct instance.
@@ -290,12 +288,11 @@ struct hvcs_struct {
290 int chars_in_buffer; 288 int chars_in_buffer;
291 289
292 /* 290 /*
293 * Any variable below the kref is valid before a tty is connected and 291 * Any variable below is valid before a tty is connected and
294 * stays valid after the tty is disconnected. These shouldn't be 292 * stays valid after the tty is disconnected. These shouldn't be
295 * whacked until the kobject refcount reaches zero though some entries 293 * whacked until the kobject refcount reaches zero though some entries
296 * may be changed via sysfs initiatives. 294 * may be changed via sysfs initiatives.
297 */ 295 */
298 struct kref kref; /* ref count & hvcs_struct lifetime */
299 int connected; /* is the vty-server currently connected to a vty? */ 296 int connected; /* is the vty-server currently connected to a vty? */
300 uint32_t p_unit_address; /* partner unit address */ 297 uint32_t p_unit_address; /* partner unit address */
301 uint32_t p_partition_ID; /* partner partition ID */ 298 uint32_t p_partition_ID; /* partner partition ID */
@@ -304,9 +301,6 @@ struct hvcs_struct {
304 struct vio_dev *vdev; 301 struct vio_dev *vdev;
305}; 302};
306 303
307/* Required to back map a kref to its containing object */
308#define from_kref(k) container_of(k, struct hvcs_struct, kref)
309
310static LIST_HEAD(hvcs_structs); 304static LIST_HEAD(hvcs_structs);
311static DEFINE_SPINLOCK(hvcs_structs_lock); 305static DEFINE_SPINLOCK(hvcs_structs_lock);
312static DEFINE_MUTEX(hvcs_init_mutex); 306static DEFINE_MUTEX(hvcs_init_mutex);
@@ -422,7 +416,7 @@ static ssize_t hvcs_vterm_state_store(struct device *dev, struct device_attribut
422 416
423 spin_lock_irqsave(&hvcsd->lock, flags); 417 spin_lock_irqsave(&hvcsd->lock, flags);
424 418
425 if (hvcsd->open_count > 0) { 419 if (hvcsd->port.count > 0) {
426 spin_unlock_irqrestore(&hvcsd->lock, flags); 420 spin_unlock_irqrestore(&hvcsd->lock, flags);
427 printk(KERN_INFO "HVCS: vterm state unchanged. " 421 printk(KERN_INFO "HVCS: vterm state unchanged. "
428 "The hvcs device node is still in use.\n"); 422 "The hvcs device node is still in use.\n");
@@ -564,7 +558,7 @@ static irqreturn_t hvcs_handle_interrupt(int irq, void *dev_instance)
564static void hvcs_try_write(struct hvcs_struct *hvcsd) 558static void hvcs_try_write(struct hvcs_struct *hvcsd)
565{ 559{
566 uint32_t unit_address = hvcsd->vdev->unit_address; 560 uint32_t unit_address = hvcsd->vdev->unit_address;
567 struct tty_struct *tty = hvcsd->tty; 561 struct tty_struct *tty = hvcsd->port.tty;
568 int sent; 562 int sent;
569 563
570 if (hvcsd->todo_mask & HVCS_TRY_WRITE) { 564 if (hvcsd->todo_mask & HVCS_TRY_WRITE) {
@@ -602,7 +596,7 @@ static int hvcs_io(struct hvcs_struct *hvcsd)
602 spin_lock_irqsave(&hvcsd->lock, flags); 596 spin_lock_irqsave(&hvcsd->lock, flags);
603 597
604 unit_address = hvcsd->vdev->unit_address; 598 unit_address = hvcsd->vdev->unit_address;
605 tty = hvcsd->tty; 599 tty = hvcsd->port.tty;
606 600
607 hvcs_try_write(hvcsd); 601 hvcs_try_write(hvcsd);
608 602
@@ -701,10 +695,9 @@ static void hvcs_return_index(int index)
701 hvcs_index_list[index] = -1; 695 hvcs_index_list[index] = -1;
702} 696}
703 697
704/* callback when the kref ref count reaches zero */ 698static void hvcs_destruct_port(struct tty_port *p)
705static void destroy_hvcs_struct(struct kref *kref)
706{ 699{
707 struct hvcs_struct *hvcsd = from_kref(kref); 700 struct hvcs_struct *hvcsd = container_of(p, struct hvcs_struct, port);
708 struct vio_dev *vdev; 701 struct vio_dev *vdev;
709 unsigned long flags; 702 unsigned long flags;
710 703
@@ -741,6 +734,10 @@ static void destroy_hvcs_struct(struct kref *kref)
741 kfree(hvcsd); 734 kfree(hvcsd);
742} 735}
743 736
737static const struct tty_port_operations hvcs_port_ops = {
738 .destruct = hvcs_destruct_port,
739};
740
744static int hvcs_get_index(void) 741static int hvcs_get_index(void)
745{ 742{
746 int i; 743 int i;
@@ -789,10 +786,9 @@ static int __devinit hvcs_probe(
789 if (!hvcsd) 786 if (!hvcsd)
790 return -ENODEV; 787 return -ENODEV;
791 788
792 789 tty_port_init(&hvcsd->port);
790 hvcsd->port.ops = &hvcs_port_ops;
793 spin_lock_init(&hvcsd->lock); 791 spin_lock_init(&hvcsd->lock);
794 /* Automatically incs the refcount the first time */
795 kref_init(&hvcsd->kref);
796 792
797 hvcsd->vdev = dev; 793 hvcsd->vdev = dev;
798 dev_set_drvdata(&dev->dev, hvcsd); 794 dev_set_drvdata(&dev->dev, hvcsd);
@@ -852,7 +848,7 @@ static int __devexit hvcs_remove(struct vio_dev *dev)
852 848
853 spin_lock_irqsave(&hvcsd->lock, flags); 849 spin_lock_irqsave(&hvcsd->lock, flags);
854 850
855 tty = hvcsd->tty; 851 tty = hvcsd->port.tty;
856 852
857 spin_unlock_irqrestore(&hvcsd->lock, flags); 853 spin_unlock_irqrestore(&hvcsd->lock, flags);
858 854
@@ -860,7 +856,7 @@ static int __devexit hvcs_remove(struct vio_dev *dev)
860 * Let the last holder of this object cause it to be removed, which 856 * Let the last holder of this object cause it to be removed, which
861 * would probably be tty_hangup below. 857 * would probably be tty_hangup below.
862 */ 858 */
863 kref_put(&hvcsd->kref, destroy_hvcs_struct); 859 tty_port_put(&hvcsd->port);
864 860
865 /* 861 /*
866 * The hangup is a scheduled function which will auto chain call 862 * The hangup is a scheduled function which will auto chain call
@@ -1094,7 +1090,7 @@ static struct hvcs_struct *hvcs_get_by_index(int index)
1094 list_for_each_entry(hvcsd, &hvcs_structs, next) { 1090 list_for_each_entry(hvcsd, &hvcs_structs, next) {
1095 spin_lock_irqsave(&hvcsd->lock, flags); 1091 spin_lock_irqsave(&hvcsd->lock, flags);
1096 if (hvcsd->index == index) { 1092 if (hvcsd->index == index) {
1097 kref_get(&hvcsd->kref); 1093 tty_port_get(&hvcsd->port);
1098 spin_unlock_irqrestore(&hvcsd->lock, flags); 1094 spin_unlock_irqrestore(&hvcsd->lock, flags);
1099 spin_unlock(&hvcs_structs_lock); 1095 spin_unlock(&hvcs_structs_lock);
1100 return hvcsd; 1096 return hvcsd;
@@ -1138,8 +1134,8 @@ static int hvcs_open(struct tty_struct *tty, struct file *filp)
1138 if ((retval = hvcs_partner_connect(hvcsd))) 1134 if ((retval = hvcs_partner_connect(hvcsd)))
1139 goto error_release; 1135 goto error_release;
1140 1136
1141 hvcsd->open_count = 1; 1137 hvcsd->port.count = 1;
1142 hvcsd->tty = tty; 1138 hvcsd->port.tty = tty;
1143 tty->driver_data = hvcsd; 1139 tty->driver_data = hvcsd;
1144 1140
1145 memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN); 1141 memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
@@ -1160,7 +1156,7 @@ static int hvcs_open(struct tty_struct *tty, struct file *filp)
1160 * and will grab the spinlock and free the connection if it fails. 1156 * and will grab the spinlock and free the connection if it fails.
1161 */ 1157 */
1162 if (((rc = hvcs_enable_device(hvcsd, unit_address, irq, vdev)))) { 1158 if (((rc = hvcs_enable_device(hvcsd, unit_address, irq, vdev)))) {
1163 kref_put(&hvcsd->kref, destroy_hvcs_struct); 1159 tty_port_put(&hvcsd->port);
1164 printk(KERN_WARNING "HVCS: enable device failed.\n"); 1160 printk(KERN_WARNING "HVCS: enable device failed.\n");
1165 return rc; 1161 return rc;
1166 } 1162 }
@@ -1171,8 +1167,8 @@ fast_open:
1171 hvcsd = tty->driver_data; 1167 hvcsd = tty->driver_data;
1172 1168
1173 spin_lock_irqsave(&hvcsd->lock, flags); 1169 spin_lock_irqsave(&hvcsd->lock, flags);
1174 kref_get(&hvcsd->kref); 1170 tty_port_get(&hvcsd->port);
1175 hvcsd->open_count++; 1171 hvcsd->port.count++;
1176 hvcsd->todo_mask |= HVCS_SCHED_READ; 1172 hvcsd->todo_mask |= HVCS_SCHED_READ;
1177 spin_unlock_irqrestore(&hvcsd->lock, flags); 1173 spin_unlock_irqrestore(&hvcsd->lock, flags);
1178 1174
@@ -1186,7 +1182,7 @@ open_success:
1186 1182
1187error_release: 1183error_release:
1188 spin_unlock_irqrestore(&hvcsd->lock, flags); 1184 spin_unlock_irqrestore(&hvcsd->lock, flags);
1189 kref_put(&hvcsd->kref, destroy_hvcs_struct); 1185 tty_port_put(&hvcsd->port);
1190 1186
1191 printk(KERN_WARNING "HVCS: partner connect failed.\n"); 1187 printk(KERN_WARNING "HVCS: partner connect failed.\n");
1192 return retval; 1188 return retval;
@@ -1216,7 +1212,7 @@ static void hvcs_close(struct tty_struct *tty, struct file *filp)
1216 hvcsd = tty->driver_data; 1212 hvcsd = tty->driver_data;
1217 1213
1218 spin_lock_irqsave(&hvcsd->lock, flags); 1214 spin_lock_irqsave(&hvcsd->lock, flags);
1219 if (--hvcsd->open_count == 0) { 1215 if (--hvcsd->port.count == 0) {
1220 1216
1221 vio_disable_interrupts(hvcsd->vdev); 1217 vio_disable_interrupts(hvcsd->vdev);
1222 1218
@@ -1225,7 +1221,7 @@ static void hvcs_close(struct tty_struct *tty, struct file *filp)
1225 * execute any operations on the TTY even though it is obligated 1221 * execute any operations on the TTY even though it is obligated
1226 * to deliver any pending I/O to the hypervisor. 1222 * to deliver any pending I/O to the hypervisor.
1227 */ 1223 */
1228 hvcsd->tty = NULL; 1224 hvcsd->port.tty = NULL;
1229 1225
1230 irq = hvcsd->vdev->irq; 1226 irq = hvcsd->vdev->irq;
1231 spin_unlock_irqrestore(&hvcsd->lock, flags); 1227 spin_unlock_irqrestore(&hvcsd->lock, flags);
@@ -1240,16 +1236,16 @@ static void hvcs_close(struct tty_struct *tty, struct file *filp)
1240 tty->driver_data = NULL; 1236 tty->driver_data = NULL;
1241 1237
1242 free_irq(irq, hvcsd); 1238 free_irq(irq, hvcsd);
1243 kref_put(&hvcsd->kref, destroy_hvcs_struct); 1239 tty_port_put(&hvcsd->port);
1244 return; 1240 return;
1245 } else if (hvcsd->open_count < 0) { 1241 } else if (hvcsd->port.count < 0) {
1246 printk(KERN_ERR "HVCS: vty-server@%X open_count: %d" 1242 printk(KERN_ERR "HVCS: vty-server@%X open_count: %d"
1247 " is missmanaged.\n", 1243 " is missmanaged.\n",
1248 hvcsd->vdev->unit_address, hvcsd->open_count); 1244 hvcsd->vdev->unit_address, hvcsd->port.count);
1249 } 1245 }
1250 1246
1251 spin_unlock_irqrestore(&hvcsd->lock, flags); 1247 spin_unlock_irqrestore(&hvcsd->lock, flags);
1252 kref_put(&hvcsd->kref, destroy_hvcs_struct); 1248 tty_port_put(&hvcsd->port);
1253} 1249}
1254 1250
1255static void hvcs_hangup(struct tty_struct * tty) 1251static void hvcs_hangup(struct tty_struct * tty)
@@ -1261,7 +1257,7 @@ static void hvcs_hangup(struct tty_struct * tty)
1261 1257
1262 spin_lock_irqsave(&hvcsd->lock, flags); 1258 spin_lock_irqsave(&hvcsd->lock, flags);
1263 /* Preserve this so that we know how many kref refs to put */ 1259 /* Preserve this so that we know how many kref refs to put */
1264 temp_open_count = hvcsd->open_count; 1260 temp_open_count = hvcsd->port.count;
1265 1261
1266 /* 1262 /*
1267 * Don't kref put inside the spinlock because the destruction 1263 * Don't kref put inside the spinlock because the destruction
@@ -1273,10 +1269,10 @@ static void hvcs_hangup(struct tty_struct * tty)
1273 hvcsd->todo_mask = 0; 1269 hvcsd->todo_mask = 0;
1274 1270
1275 /* I don't think the tty needs the hvcs_struct pointer after a hangup */ 1271 /* I don't think the tty needs the hvcs_struct pointer after a hangup */
1276 hvcsd->tty->driver_data = NULL; 1272 tty->driver_data = NULL;
1277 hvcsd->tty = NULL; 1273 hvcsd->port.tty = NULL;
1278 1274
1279 hvcsd->open_count = 0; 1275 hvcsd->port.count = 0;
1280 1276
1281 /* This will drop any buffered data on the floor which is OK in a hangup 1277 /* This will drop any buffered data on the floor which is OK in a hangup
1282 * scenario. */ 1278 * scenario. */
@@ -1301,7 +1297,7 @@ static void hvcs_hangup(struct tty_struct * tty)
1301 * NOTE: If this hangup was signaled from user space then the 1297 * NOTE: If this hangup was signaled from user space then the
1302 * final put will never happen. 1298 * final put will never happen.
1303 */ 1299 */
1304 kref_put(&hvcsd->kref, destroy_hvcs_struct); 1300 tty_port_put(&hvcsd->port);
1305 } 1301 }
1306} 1302}
1307 1303
@@ -1347,7 +1343,7 @@ static int hvcs_write(struct tty_struct *tty,
1347 * the middle of a write operation? This is a crummy place to do this 1343 * the middle of a write operation? This is a crummy place to do this
1348 * but we want to keep it all in the spinlock. 1344 * but we want to keep it all in the spinlock.
1349 */ 1345 */
1350 if (hvcsd->open_count <= 0) { 1346 if (hvcsd->port.count <= 0) {
1351 spin_unlock_irqrestore(&hvcsd->lock, flags); 1347 spin_unlock_irqrestore(&hvcsd->lock, flags);
1352 return -ENODEV; 1348 return -ENODEV;
1353 } 1349 }
@@ -1421,7 +1417,7 @@ static int hvcs_write_room(struct tty_struct *tty)
1421{ 1417{
1422 struct hvcs_struct *hvcsd = tty->driver_data; 1418 struct hvcs_struct *hvcsd = tty->driver_data;
1423 1419
1424 if (!hvcsd || hvcsd->open_count <= 0) 1420 if (!hvcsd || hvcsd->port.count <= 0)
1425 return 0; 1421 return 0;
1426 1422
1427 return HVCS_BUFF_LEN - hvcsd->chars_in_buffer; 1423 return HVCS_BUFF_LEN - hvcsd->chars_in_buffer;
diff --git a/drivers/tty/hvc/hvsi.c b/drivers/tty/hvc/hvsi.c
index a7488b748647..6f5bc49c441f 100644
--- a/drivers/tty/hvc/hvsi.c
+++ b/drivers/tty/hvc/hvsi.c
@@ -69,14 +69,13 @@
69#define __ALIGNED__ __attribute__((__aligned__(sizeof(long)))) 69#define __ALIGNED__ __attribute__((__aligned__(sizeof(long))))
70 70
71struct hvsi_struct { 71struct hvsi_struct {
72 struct tty_port port;
72 struct delayed_work writer; 73 struct delayed_work writer;
73 struct work_struct handshaker; 74 struct work_struct handshaker;
74 wait_queue_head_t emptyq; /* woken when outbuf is emptied */ 75 wait_queue_head_t emptyq; /* woken when outbuf is emptied */
75 wait_queue_head_t stateq; /* woken when HVSI state changes */ 76 wait_queue_head_t stateq; /* woken when HVSI state changes */
76 spinlock_t lock; 77 spinlock_t lock;
77 int index; 78 int index;
78 struct tty_struct *tty;
79 int count;
80 uint8_t throttle_buf[128]; 79 uint8_t throttle_buf[128];
81 uint8_t outbuf[N_OUTBUF]; /* to implement write_room and chars_in_buffer */ 80 uint8_t outbuf[N_OUTBUF]; /* to implement write_room and chars_in_buffer */
82 /* inbuf is for packet reassembly. leave a little room for leftovers. */ 81 /* inbuf is for packet reassembly. leave a little room for leftovers. */
@@ -237,7 +236,7 @@ static int hvsi_read(struct hvsi_struct *hp, char *buf, int count)
237} 236}
238 237
239static void hvsi_recv_control(struct hvsi_struct *hp, uint8_t *packet, 238static void hvsi_recv_control(struct hvsi_struct *hp, uint8_t *packet,
240 struct tty_struct **to_hangup, struct hvsi_struct **to_handshake) 239 struct tty_struct *tty, struct hvsi_struct **to_handshake)
241{ 240{
242 struct hvsi_control *header = (struct hvsi_control *)packet; 241 struct hvsi_control *header = (struct hvsi_control *)packet;
243 242
@@ -247,9 +246,8 @@ static void hvsi_recv_control(struct hvsi_struct *hp, uint8_t *packet,
247 /* CD went away; no more connection */ 246 /* CD went away; no more connection */
248 pr_debug("hvsi%i: CD dropped\n", hp->index); 247 pr_debug("hvsi%i: CD dropped\n", hp->index);
249 hp->mctrl &= TIOCM_CD; 248 hp->mctrl &= TIOCM_CD;
250 /* If userland hasn't done an open(2) yet, hp->tty is NULL. */ 249 if (tty && !C_CLOCAL(tty))
251 if (hp->tty && !(hp->tty->flags & CLOCAL)) 250 tty_hangup(tty);
252 *to_hangup = hp->tty;
253 } 251 }
254 break; 252 break;
255 case VSV_CLOSE_PROTOCOL: 253 case VSV_CLOSE_PROTOCOL:
@@ -331,7 +329,8 @@ static void hvsi_recv_query(struct hvsi_struct *hp, uint8_t *packet)
331 } 329 }
332} 330}
333 331
334static void hvsi_insert_chars(struct hvsi_struct *hp, const char *buf, int len) 332static void hvsi_insert_chars(struct hvsi_struct *hp, struct tty_struct *tty,
333 const char *buf, int len)
335{ 334{
336 int i; 335 int i;
337 336
@@ -347,7 +346,7 @@ static void hvsi_insert_chars(struct hvsi_struct *hp, const char *buf, int len)
347 continue; 346 continue;
348 } 347 }
349#endif /* CONFIG_MAGIC_SYSRQ */ 348#endif /* CONFIG_MAGIC_SYSRQ */
350 tty_insert_flip_char(hp->tty, c, 0); 349 tty_insert_flip_char(tty, c, 0);
351 } 350 }
352} 351}
353 352
@@ -360,7 +359,7 @@ static void hvsi_insert_chars(struct hvsi_struct *hp, const char *buf, int len)
360 * revisited. 359 * revisited.
361 */ 360 */
362#define TTY_THRESHOLD_THROTTLE 128 361#define TTY_THRESHOLD_THROTTLE 128
363static struct tty_struct *hvsi_recv_data(struct hvsi_struct *hp, 362static bool hvsi_recv_data(struct hvsi_struct *hp, struct tty_struct *tty,
364 const uint8_t *packet) 363 const uint8_t *packet)
365{ 364{
366 const struct hvsi_header *header = (const struct hvsi_header *)packet; 365 const struct hvsi_header *header = (const struct hvsi_header *)packet;
@@ -371,14 +370,14 @@ static struct tty_struct *hvsi_recv_data(struct hvsi_struct *hp,
371 pr_debug("queueing %i chars '%.*s'\n", datalen, datalen, data); 370 pr_debug("queueing %i chars '%.*s'\n", datalen, datalen, data);
372 371
373 if (datalen == 0) 372 if (datalen == 0)
374 return NULL; 373 return false;
375 374
376 if (overflow > 0) { 375 if (overflow > 0) {
377 pr_debug("%s: got >TTY_THRESHOLD_THROTTLE bytes\n", __func__); 376 pr_debug("%s: got >TTY_THRESHOLD_THROTTLE bytes\n", __func__);
378 datalen = TTY_THRESHOLD_THROTTLE; 377 datalen = TTY_THRESHOLD_THROTTLE;
379 } 378 }
380 379
381 hvsi_insert_chars(hp, data, datalen); 380 hvsi_insert_chars(hp, tty, data, datalen);
382 381
383 if (overflow > 0) { 382 if (overflow > 0) {
384 /* 383 /*
@@ -390,7 +389,7 @@ static struct tty_struct *hvsi_recv_data(struct hvsi_struct *hp,
390 hp->n_throttle = overflow; 389 hp->n_throttle = overflow;
391 } 390 }
392 391
393 return hp->tty; 392 return true;
394} 393}
395 394
396/* 395/*
@@ -399,14 +398,13 @@ static struct tty_struct *hvsi_recv_data(struct hvsi_struct *hp,
399 * machine during console handshaking (in which case tty = NULL and we ignore 398 * machine during console handshaking (in which case tty = NULL and we ignore
400 * incoming data). 399 * incoming data).
401 */ 400 */
402static int hvsi_load_chunk(struct hvsi_struct *hp, struct tty_struct **flip, 401static int hvsi_load_chunk(struct hvsi_struct *hp, struct tty_struct *tty,
403 struct tty_struct **hangup, struct hvsi_struct **handshake) 402 struct hvsi_struct **handshake)
404{ 403{
405 uint8_t *packet = hp->inbuf; 404 uint8_t *packet = hp->inbuf;
406 int chunklen; 405 int chunklen;
406 bool flip = false;
407 407
408 *flip = NULL;
409 *hangup = NULL;
410 *handshake = NULL; 408 *handshake = NULL;
411 409
412 chunklen = hvsi_read(hp, hp->inbuf_end, HVSI_MAX_READ); 410 chunklen = hvsi_read(hp, hp->inbuf_end, HVSI_MAX_READ);
@@ -440,12 +438,12 @@ static int hvsi_load_chunk(struct hvsi_struct *hp, struct tty_struct **flip,
440 case VS_DATA_PACKET_HEADER: 438 case VS_DATA_PACKET_HEADER:
441 if (!is_open(hp)) 439 if (!is_open(hp))
442 break; 440 break;
443 if (hp->tty == NULL) 441 if (tty == NULL)
444 break; /* no tty buffer to put data in */ 442 break; /* no tty buffer to put data in */
445 *flip = hvsi_recv_data(hp, packet); 443 flip = hvsi_recv_data(hp, tty, packet);
446 break; 444 break;
447 case VS_CONTROL_PACKET_HEADER: 445 case VS_CONTROL_PACKET_HEADER:
448 hvsi_recv_control(hp, packet, hangup, handshake); 446 hvsi_recv_control(hp, packet, tty, handshake);
449 break; 447 break;
450 case VS_QUERY_RESPONSE_PACKET_HEADER: 448 case VS_QUERY_RESPONSE_PACKET_HEADER:
451 hvsi_recv_response(hp, packet); 449 hvsi_recv_response(hp, packet);
@@ -462,28 +460,26 @@ static int hvsi_load_chunk(struct hvsi_struct *hp, struct tty_struct **flip,
462 460
463 packet += len_packet(packet); 461 packet += len_packet(packet);
464 462
465 if (*hangup || *handshake) { 463 if (*handshake) {
466 pr_debug("%s: hangup or handshake\n", __func__); 464 pr_debug("%s: handshake\n", __func__);
467 /*
468 * we need to send the hangup now before receiving any more data.
469 * If we get "data, hangup, data", we can't deliver the second
470 * data before the hangup.
471 */
472 break; 465 break;
473 } 466 }
474 } 467 }
475 468
476 compact_inbuf(hp, packet); 469 compact_inbuf(hp, packet);
477 470
471 if (flip)
472 tty_flip_buffer_push(tty);
473
478 return 1; 474 return 1;
479} 475}
480 476
481static void hvsi_send_overflow(struct hvsi_struct *hp) 477static void hvsi_send_overflow(struct hvsi_struct *hp, struct tty_struct *tty)
482{ 478{
483 pr_debug("%s: delivering %i bytes overflow\n", __func__, 479 pr_debug("%s: delivering %i bytes overflow\n", __func__,
484 hp->n_throttle); 480 hp->n_throttle);
485 481
486 hvsi_insert_chars(hp, hp->throttle_buf, hp->n_throttle); 482 hvsi_insert_chars(hp, tty, hp->throttle_buf, hp->n_throttle);
487 hp->n_throttle = 0; 483 hp->n_throttle = 0;
488} 484}
489 485
@@ -494,35 +490,20 @@ static void hvsi_send_overflow(struct hvsi_struct *hp)
494static irqreturn_t hvsi_interrupt(int irq, void *arg) 490static irqreturn_t hvsi_interrupt(int irq, void *arg)
495{ 491{
496 struct hvsi_struct *hp = (struct hvsi_struct *)arg; 492 struct hvsi_struct *hp = (struct hvsi_struct *)arg;
497 struct tty_struct *flip;
498 struct tty_struct *hangup;
499 struct hvsi_struct *handshake; 493 struct hvsi_struct *handshake;
494 struct tty_struct *tty;
500 unsigned long flags; 495 unsigned long flags;
501 int again = 1; 496 int again = 1;
502 497
503 pr_debug("%s\n", __func__); 498 pr_debug("%s\n", __func__);
504 499
500 tty = tty_port_tty_get(&hp->port);
501
505 while (again) { 502 while (again) {
506 spin_lock_irqsave(&hp->lock, flags); 503 spin_lock_irqsave(&hp->lock, flags);
507 again = hvsi_load_chunk(hp, &flip, &hangup, &handshake); 504 again = hvsi_load_chunk(hp, tty, &handshake);
508 spin_unlock_irqrestore(&hp->lock, flags); 505 spin_unlock_irqrestore(&hp->lock, flags);
509 506
510 /*
511 * we have to call tty_flip_buffer_push() and tty_hangup() outside our
512 * spinlock. But we also have to keep going until we've read all the
513 * available data.
514 */
515
516 if (flip) {
517 /* there was data put in the tty flip buffer */
518 tty_flip_buffer_push(flip);
519 flip = NULL;
520 }
521
522 if (hangup) {
523 tty_hangup(hangup);
524 }
525
526 if (handshake) { 507 if (handshake) {
527 pr_debug("hvsi%i: attempting re-handshake\n", handshake->index); 508 pr_debug("hvsi%i: attempting re-handshake\n", handshake->index);
528 schedule_work(&handshake->handshaker); 509 schedule_work(&handshake->handshaker);
@@ -530,18 +511,15 @@ static irqreturn_t hvsi_interrupt(int irq, void *arg)
530 } 511 }
531 512
532 spin_lock_irqsave(&hp->lock, flags); 513 spin_lock_irqsave(&hp->lock, flags);
533 if (hp->tty && hp->n_throttle 514 if (tty && hp->n_throttle && !test_bit(TTY_THROTTLED, &tty->flags)) {
534 && (!test_bit(TTY_THROTTLED, &hp->tty->flags))) { 515 /* we weren't hung up and we weren't throttled, so we can
535 /* we weren't hung up and we weren't throttled, so we can deliver the 516 * deliver the rest now */
536 * rest now */ 517 hvsi_send_overflow(hp, tty);
537 flip = hp->tty; 518 tty_flip_buffer_push(tty);
538 hvsi_send_overflow(hp);
539 } 519 }
540 spin_unlock_irqrestore(&hp->lock, flags); 520 spin_unlock_irqrestore(&hp->lock, flags);
541 521
542 if (flip) { 522 tty_kref_put(tty);
543 tty_flip_buffer_push(flip);
544 }
545 523
546 return IRQ_HANDLED; 524 return IRQ_HANDLED;
547} 525}
@@ -749,9 +727,9 @@ static int hvsi_open(struct tty_struct *tty, struct file *filp)
749 if (hp->state == HVSI_FSP_DIED) 727 if (hp->state == HVSI_FSP_DIED)
750 return -EIO; 728 return -EIO;
751 729
730 tty_port_tty_set(&hp->port, tty);
752 spin_lock_irqsave(&hp->lock, flags); 731 spin_lock_irqsave(&hp->lock, flags);
753 hp->tty = tty; 732 hp->port.count++;
754 hp->count++;
755 atomic_set(&hp->seqno, 0); 733 atomic_set(&hp->seqno, 0);
756 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE); 734 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
757 spin_unlock_irqrestore(&hp->lock, flags); 735 spin_unlock_irqrestore(&hp->lock, flags);
@@ -808,8 +786,8 @@ static void hvsi_close(struct tty_struct *tty, struct file *filp)
808 786
809 spin_lock_irqsave(&hp->lock, flags); 787 spin_lock_irqsave(&hp->lock, flags);
810 788
811 if (--hp->count == 0) { 789 if (--hp->port.count == 0) {
812 hp->tty = NULL; 790 tty_port_tty_set(&hp->port, NULL);
813 hp->inbuf_end = hp->inbuf; /* discard remaining partial packets */ 791 hp->inbuf_end = hp->inbuf; /* discard remaining partial packets */
814 792
815 /* only close down connection if it is not the console */ 793 /* only close down connection if it is not the console */
@@ -841,9 +819,9 @@ static void hvsi_close(struct tty_struct *tty, struct file *filp)
841 819
842 spin_lock_irqsave(&hp->lock, flags); 820 spin_lock_irqsave(&hp->lock, flags);
843 } 821 }
844 } else if (hp->count < 0) 822 } else if (hp->port.count < 0)
845 printk(KERN_ERR "hvsi_close %lu: oops, count is %d\n", 823 printk(KERN_ERR "hvsi_close %lu: oops, count is %d\n",
846 hp - hvsi_ports, hp->count); 824 hp - hvsi_ports, hp->port.count);
847 825
848 spin_unlock_irqrestore(&hp->lock, flags); 826 spin_unlock_irqrestore(&hp->lock, flags);
849} 827}
@@ -855,12 +833,11 @@ static void hvsi_hangup(struct tty_struct *tty)
855 833
856 pr_debug("%s\n", __func__); 834 pr_debug("%s\n", __func__);
857 835
858 spin_lock_irqsave(&hp->lock, flags); 836 tty_port_tty_set(&hp->port, NULL);
859 837
860 hp->count = 0; 838 spin_lock_irqsave(&hp->lock, flags);
839 hp->port.count = 0;
861 hp->n_outbuf = 0; 840 hp->n_outbuf = 0;
862 hp->tty = NULL;
863
864 spin_unlock_irqrestore(&hp->lock, flags); 841 spin_unlock_irqrestore(&hp->lock, flags);
865} 842}
866 843
@@ -888,6 +865,7 @@ static void hvsi_write_worker(struct work_struct *work)
888{ 865{
889 struct hvsi_struct *hp = 866 struct hvsi_struct *hp =
890 container_of(work, struct hvsi_struct, writer.work); 867 container_of(work, struct hvsi_struct, writer.work);
868 struct tty_struct *tty;
891 unsigned long flags; 869 unsigned long flags;
892#ifdef DEBUG 870#ifdef DEBUG
893 static long start_j = 0; 871 static long start_j = 0;
@@ -921,7 +899,11 @@ static void hvsi_write_worker(struct work_struct *work)
921 start_j = 0; 899 start_j = 0;
922#endif /* DEBUG */ 900#endif /* DEBUG */
923 wake_up_all(&hp->emptyq); 901 wake_up_all(&hp->emptyq);
924 tty_wakeup(hp->tty); 902 tty = tty_port_tty_get(&hp->port);
903 if (tty) {
904 tty_wakeup(tty);
905 tty_kref_put(tty);
906 }
925 } 907 }
926 908
927out: 909out:
@@ -966,8 +948,8 @@ static int hvsi_write(struct tty_struct *tty,
966 * and hvsi_write_worker will be scheduled. subsequent hvsi_write() calls 948 * and hvsi_write_worker will be scheduled. subsequent hvsi_write() calls
967 * will see there is no room in outbuf and return. 949 * will see there is no room in outbuf and return.
968 */ 950 */
969 while ((count > 0) && (hvsi_write_room(hp->tty) > 0)) { 951 while ((count > 0) && (hvsi_write_room(tty) > 0)) {
970 int chunksize = min(count, hvsi_write_room(hp->tty)); 952 int chunksize = min(count, hvsi_write_room(tty));
971 953
972 BUG_ON(hp->n_outbuf < 0); 954 BUG_ON(hp->n_outbuf < 0);
973 memcpy(hp->outbuf + hp->n_outbuf, source, chunksize); 955 memcpy(hp->outbuf + hp->n_outbuf, source, chunksize);
@@ -1014,19 +996,16 @@ static void hvsi_unthrottle(struct tty_struct *tty)
1014{ 996{
1015 struct hvsi_struct *hp = tty->driver_data; 997 struct hvsi_struct *hp = tty->driver_data;
1016 unsigned long flags; 998 unsigned long flags;
1017 int shouldflip = 0;
1018 999
1019 pr_debug("%s\n", __func__); 1000 pr_debug("%s\n", __func__);
1020 1001
1021 spin_lock_irqsave(&hp->lock, flags); 1002 spin_lock_irqsave(&hp->lock, flags);
1022 if (hp->n_throttle) { 1003 if (hp->n_throttle) {
1023 hvsi_send_overflow(hp); 1004 hvsi_send_overflow(hp, tty);
1024 shouldflip = 1; 1005 tty_flip_buffer_push(tty);
1025 } 1006 }
1026 spin_unlock_irqrestore(&hp->lock, flags); 1007 spin_unlock_irqrestore(&hp->lock, flags);
1027 1008
1028 if (shouldflip)
1029 tty_flip_buffer_push(hp->tty);
1030 1009
1031 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE); 1010 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
1032} 1011}
@@ -1228,6 +1207,7 @@ static int __init hvsi_console_init(void)
1228 init_waitqueue_head(&hp->emptyq); 1207 init_waitqueue_head(&hp->emptyq);
1229 init_waitqueue_head(&hp->stateq); 1208 init_waitqueue_head(&hp->stateq);
1230 spin_lock_init(&hp->lock); 1209 spin_lock_init(&hp->lock);
1210 tty_port_init(&hp->port);
1231 hp->index = hvsi_count; 1211 hp->index = hvsi_count;
1232 hp->inbuf_end = hp->inbuf; 1212 hp->inbuf_end = hp->inbuf;
1233 hp->state = HVSI_CLOSED; 1213 hp->state = HVSI_CLOSED;
diff --git a/drivers/tty/hvc/hvsi_lib.c b/drivers/tty/hvc/hvsi_lib.c
index 6f4dd83d8695..59c135dd5d20 100644
--- a/drivers/tty/hvc/hvsi_lib.c
+++ b/drivers/tty/hvc/hvsi_lib.c
@@ -377,7 +377,7 @@ int hvsilib_open(struct hvsi_priv *pv, struct hvc_struct *hp)
377 pr_devel("HVSI@%x: open !\n", pv->termno); 377 pr_devel("HVSI@%x: open !\n", pv->termno);
378 378
379 /* Keep track of the tty data structure */ 379 /* Keep track of the tty data structure */
380 pv->tty = tty_kref_get(hp->tty); 380 pv->tty = tty_port_tty_get(&hp->port);
381 381
382 hvsilib_establish(pv); 382 hvsilib_establish(pv);
383 383
diff --git a/drivers/tty/ipwireless/tty.c b/drivers/tty/ipwireless/tty.c
index 4daf962f7055..f8b5fa0093a3 100644
--- a/drivers/tty/ipwireless/tty.c
+++ b/drivers/tty/ipwireless/tty.c
@@ -44,14 +44,13 @@
44#define TTYTYPE_RAS_RAW (2) 44#define TTYTYPE_RAS_RAW (2)
45 45
46struct ipw_tty { 46struct ipw_tty {
47 struct tty_port port;
47 int index; 48 int index;
48 struct ipw_hardware *hardware; 49 struct ipw_hardware *hardware;
49 unsigned int channel_idx; 50 unsigned int channel_idx;
50 unsigned int secondary_channel_idx; 51 unsigned int secondary_channel_idx;
51 int tty_type; 52 int tty_type;
52 struct ipw_network *network; 53 struct ipw_network *network;
53 struct tty_struct *linux_tty;
54 int open_count;
55 unsigned int control_lines; 54 unsigned int control_lines;
56 struct mutex ipw_tty_mutex; 55 struct mutex ipw_tty_mutex;
57 int tx_bytes_queued; 56 int tx_bytes_queued;
@@ -73,23 +72,6 @@ static char *tty_type_name(int tty_type)
73 return channel_names[tty_type]; 72 return channel_names[tty_type];
74} 73}
75 74
76static void report_registering(struct ipw_tty *tty)
77{
78 char *iftype = tty_type_name(tty->tty_type);
79
80 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
81 ": registering %s device ttyIPWp%d\n", iftype, tty->index);
82}
83
84static void report_deregistering(struct ipw_tty *tty)
85{
86 char *iftype = tty_type_name(tty->tty_type);
87
88 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
89 ": deregistering %s device ttyIPWp%d\n", iftype,
90 tty->index);
91}
92
93static struct ipw_tty *get_tty(int index) 75static struct ipw_tty *get_tty(int index)
94{ 76{
95 /* 77 /*
@@ -117,12 +99,12 @@ static int ipw_open(struct tty_struct *linux_tty, struct file *filp)
117 mutex_unlock(&tty->ipw_tty_mutex); 99 mutex_unlock(&tty->ipw_tty_mutex);
118 return -ENODEV; 100 return -ENODEV;
119 } 101 }
120 if (tty->open_count == 0) 102 if (tty->port.count == 0)
121 tty->tx_bytes_queued = 0; 103 tty->tx_bytes_queued = 0;
122 104
123 tty->open_count++; 105 tty->port.count++;
124 106
125 tty->linux_tty = linux_tty; 107 tty->port.tty = linux_tty;
126 linux_tty->driver_data = tty; 108 linux_tty->driver_data = tty;
127 linux_tty->low_latency = 1; 109 linux_tty->low_latency = 1;
128 110
@@ -136,13 +118,13 @@ static int ipw_open(struct tty_struct *linux_tty, struct file *filp)
136 118
137static void do_ipw_close(struct ipw_tty *tty) 119static void do_ipw_close(struct ipw_tty *tty)
138{ 120{
139 tty->open_count--; 121 tty->port.count--;
140 122
141 if (tty->open_count == 0) { 123 if (tty->port.count == 0) {
142 struct tty_struct *linux_tty = tty->linux_tty; 124 struct tty_struct *linux_tty = tty->port.tty;
143 125
144 if (linux_tty != NULL) { 126 if (linux_tty != NULL) {
145 tty->linux_tty = NULL; 127 tty->port.tty = NULL;
146 linux_tty->driver_data = NULL; 128 linux_tty->driver_data = NULL;
147 129
148 if (tty->tty_type == TTYTYPE_MODEM) 130 if (tty->tty_type == TTYTYPE_MODEM)
@@ -159,7 +141,7 @@ static void ipw_hangup(struct tty_struct *linux_tty)
159 return; 141 return;
160 142
161 mutex_lock(&tty->ipw_tty_mutex); 143 mutex_lock(&tty->ipw_tty_mutex);
162 if (tty->open_count == 0) { 144 if (tty->port.count == 0) {
163 mutex_unlock(&tty->ipw_tty_mutex); 145 mutex_unlock(&tty->ipw_tty_mutex);
164 return; 146 return;
165 } 147 }
@@ -182,13 +164,13 @@ void ipwireless_tty_received(struct ipw_tty *tty, unsigned char *data,
182 int work = 0; 164 int work = 0;
183 165
184 mutex_lock(&tty->ipw_tty_mutex); 166 mutex_lock(&tty->ipw_tty_mutex);
185 linux_tty = tty->linux_tty; 167 linux_tty = tty->port.tty;
186 if (linux_tty == NULL) { 168 if (linux_tty == NULL) {
187 mutex_unlock(&tty->ipw_tty_mutex); 169 mutex_unlock(&tty->ipw_tty_mutex);
188 return; 170 return;
189 } 171 }
190 172
191 if (!tty->open_count) { 173 if (!tty->port.count) {
192 mutex_unlock(&tty->ipw_tty_mutex); 174 mutex_unlock(&tty->ipw_tty_mutex);
193 return; 175 return;
194 } 176 }
@@ -230,7 +212,7 @@ static int ipw_write(struct tty_struct *linux_tty,
230 return -ENODEV; 212 return -ENODEV;
231 213
232 mutex_lock(&tty->ipw_tty_mutex); 214 mutex_lock(&tty->ipw_tty_mutex);
233 if (!tty->open_count) { 215 if (!tty->port.count) {
234 mutex_unlock(&tty->ipw_tty_mutex); 216 mutex_unlock(&tty->ipw_tty_mutex);
235 return -EINVAL; 217 return -EINVAL;
236 } 218 }
@@ -270,7 +252,7 @@ static int ipw_write_room(struct tty_struct *linux_tty)
270 if (!tty) 252 if (!tty)
271 return -ENODEV; 253 return -ENODEV;
272 254
273 if (!tty->open_count) 255 if (!tty->port.count)
274 return -EINVAL; 256 return -EINVAL;
275 257
276 room = IPWIRELESS_TX_QUEUE_SIZE - tty->tx_bytes_queued; 258 room = IPWIRELESS_TX_QUEUE_SIZE - tty->tx_bytes_queued;
@@ -312,7 +294,7 @@ static int ipw_chars_in_buffer(struct tty_struct *linux_tty)
312 if (!tty) 294 if (!tty)
313 return 0; 295 return 0;
314 296
315 if (!tty->open_count) 297 if (!tty->port.count)
316 return 0; 298 return 0;
317 299
318 return tty->tx_bytes_queued; 300 return tty->tx_bytes_queued;
@@ -393,7 +375,7 @@ static int ipw_tiocmget(struct tty_struct *linux_tty)
393 if (!tty) 375 if (!tty)
394 return -ENODEV; 376 return -ENODEV;
395 377
396 if (!tty->open_count) 378 if (!tty->port.count)
397 return -EINVAL; 379 return -EINVAL;
398 380
399 return get_control_lines(tty); 381 return get_control_lines(tty);
@@ -409,7 +391,7 @@ ipw_tiocmset(struct tty_struct *linux_tty,
409 if (!tty) 391 if (!tty)
410 return -ENODEV; 392 return -ENODEV;
411 393
412 if (!tty->open_count) 394 if (!tty->port.count)
413 return -EINVAL; 395 return -EINVAL;
414 396
415 return set_control_lines(tty, set, clear); 397 return set_control_lines(tty, set, clear);
@@ -423,7 +405,7 @@ static int ipw_ioctl(struct tty_struct *linux_tty,
423 if (!tty) 405 if (!tty)
424 return -ENODEV; 406 return -ENODEV;
425 407
426 if (!tty->open_count) 408 if (!tty->port.count)
427 return -EINVAL; 409 return -EINVAL;
428 410
429 /* FIXME: Exactly how is the tty object locked here .. */ 411 /* FIXME: Exactly how is the tty object locked here .. */
@@ -492,6 +474,7 @@ static int add_tty(int j,
492 ttys[j]->network = network; 474 ttys[j]->network = network;
493 ttys[j]->tty_type = tty_type; 475 ttys[j]->tty_type = tty_type;
494 mutex_init(&ttys[j]->ipw_tty_mutex); 476 mutex_init(&ttys[j]->ipw_tty_mutex);
477 tty_port_init(&ttys[j]->port);
495 478
496 tty_register_device(ipw_tty_driver, j, NULL); 479 tty_register_device(ipw_tty_driver, j, NULL);
497 ipwireless_associate_network_tty(network, channel_idx, ttys[j]); 480 ipwireless_associate_network_tty(network, channel_idx, ttys[j]);
@@ -500,8 +483,12 @@ static int add_tty(int j,
500 ipwireless_associate_network_tty(network, 483 ipwireless_associate_network_tty(network,
501 secondary_channel_idx, 484 secondary_channel_idx,
502 ttys[j]); 485 ttys[j]);
503 if (get_tty(j) == ttys[j]) 486 /* check if we provide raw device (if loopback is enabled) */
504 report_registering(ttys[j]); 487 if (get_tty(j))
488 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
489 ": registering %s device ttyIPWp%d\n",
490 tty_type_name(tty_type), j);
491
505 return 0; 492 return 0;
506} 493}
507 494
@@ -560,19 +547,21 @@ void ipwireless_tty_free(struct ipw_tty *tty)
560 547
561 if (ttyj) { 548 if (ttyj) {
562 mutex_lock(&ttyj->ipw_tty_mutex); 549 mutex_lock(&ttyj->ipw_tty_mutex);
563 if (get_tty(j) == ttyj) 550 if (get_tty(j))
564 report_deregistering(ttyj); 551 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
552 ": deregistering %s device ttyIPWp%d\n",
553 tty_type_name(ttyj->tty_type), j);
565 ttyj->closing = 1; 554 ttyj->closing = 1;
566 if (ttyj->linux_tty != NULL) { 555 if (ttyj->port.tty != NULL) {
567 mutex_unlock(&ttyj->ipw_tty_mutex); 556 mutex_unlock(&ttyj->ipw_tty_mutex);
568 tty_hangup(ttyj->linux_tty); 557 tty_vhangup(ttyj->port.tty);
569 /* Wait till the tty_hangup has completed */
570 flush_work_sync(&ttyj->linux_tty->hangup_work);
571 /* FIXME: Exactly how is the tty object locked here 558 /* FIXME: Exactly how is the tty object locked here
572 against a parallel ioctl etc */ 559 against a parallel ioctl etc */
560 /* FIXME2: hangup does not mean all processes
561 * are gone */
573 mutex_lock(&ttyj->ipw_tty_mutex); 562 mutex_lock(&ttyj->ipw_tty_mutex);
574 } 563 }
575 while (ttyj->open_count) 564 while (ttyj->port.count)
576 do_ipw_close(ttyj); 565 do_ipw_close(ttyj);
577 ipwireless_disassociate_network_ttys(network, 566 ipwireless_disassociate_network_ttys(network,
578 ttyj->channel_idx); 567 ttyj->channel_idx);
@@ -661,8 +650,8 @@ ipwireless_tty_notify_control_line_change(struct ipw_tty *tty,
661 */ 650 */
662 if ((old_control_lines & IPW_CONTROL_LINE_DCD) 651 if ((old_control_lines & IPW_CONTROL_LINE_DCD)
663 && !(tty->control_lines & IPW_CONTROL_LINE_DCD) 652 && !(tty->control_lines & IPW_CONTROL_LINE_DCD)
664 && tty->linux_tty) { 653 && tty->port.tty) {
665 tty_hangup(tty->linux_tty); 654 tty_hangup(tty->port.tty);
666 } 655 }
667} 656}
668 657
diff --git a/drivers/tty/mxser.c b/drivers/tty/mxser.c
index c6f372dd5623..90cc680c4f0e 100644
--- a/drivers/tty/mxser.c
+++ b/drivers/tty/mxser.c
@@ -2326,7 +2326,7 @@ static const struct tty_operations mxser_ops = {
2326 .get_icount = mxser_get_icount, 2326 .get_icount = mxser_get_icount,
2327}; 2327};
2328 2328
2329struct tty_port_operations mxser_port_ops = { 2329static struct tty_port_operations mxser_port_ops = {
2330 .carrier_raised = mxser_carrier_raised, 2330 .carrier_raised = mxser_carrier_raised,
2331 .dtr_rts = mxser_dtr_rts, 2331 .dtr_rts = mxser_dtr_rts,
2332 .activate = mxser_activate, 2332 .activate = mxser_activate,
diff --git a/drivers/tty/n_r3964.c b/drivers/tty/n_r3964.c
index 5c6c31459a2f..656ad93bbc96 100644
--- a/drivers/tty/n_r3964.c
+++ b/drivers/tty/n_r3964.c
@@ -1065,7 +1065,8 @@ static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
1065 1065
1066 TRACE_L("read()"); 1066 TRACE_L("read()");
1067 1067
1068 tty_lock(); 1068 /* FIXME: should use a private lock */
1069 tty_lock(tty);
1069 1070
1070 pClient = findClient(pInfo, task_pid(current)); 1071 pClient = findClient(pInfo, task_pid(current));
1071 if (pClient) { 1072 if (pClient) {
@@ -1077,7 +1078,7 @@ static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
1077 goto unlock; 1078 goto unlock;
1078 } 1079 }
1079 /* block until there is a message: */ 1080 /* block until there is a message: */
1080 wait_event_interruptible_tty(pInfo->read_wait, 1081 wait_event_interruptible_tty(tty, pInfo->read_wait,
1081 (pMsg = remove_msg(pInfo, pClient))); 1082 (pMsg = remove_msg(pInfo, pClient)));
1082 } 1083 }
1083 1084
@@ -1107,7 +1108,7 @@ static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
1107 } 1108 }
1108 ret = -EPERM; 1109 ret = -EPERM;
1109unlock: 1110unlock:
1110 tty_unlock(); 1111 tty_unlock(tty);
1111 return ret; 1112 return ret;
1112} 1113}
1113 1114
@@ -1156,7 +1157,7 @@ static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
1156 pHeader->locks = 0; 1157 pHeader->locks = 0;
1157 pHeader->owner = NULL; 1158 pHeader->owner = NULL;
1158 1159
1159 tty_lock(); 1160 tty_lock(tty);
1160 1161
1161 pClient = findClient(pInfo, task_pid(current)); 1162 pClient = findClient(pInfo, task_pid(current));
1162 if (pClient) { 1163 if (pClient) {
@@ -1175,7 +1176,7 @@ static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
1175 add_tx_queue(pInfo, pHeader); 1176 add_tx_queue(pInfo, pHeader);
1176 trigger_transmit(pInfo); 1177 trigger_transmit(pInfo);
1177 1178
1178 tty_unlock(); 1179 tty_unlock(tty);
1179 1180
1180 return 0; 1181 return 0;
1181} 1182}
diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c
index 94b6eda87afd..ee1c268f5f9d 100644
--- a/drivers/tty/n_tty.c
+++ b/drivers/tty/n_tty.c
@@ -1630,6 +1630,7 @@ static int copy_from_read_buf(struct tty_struct *tty,
1630 int retval; 1630 int retval;
1631 size_t n; 1631 size_t n;
1632 unsigned long flags; 1632 unsigned long flags;
1633 bool is_eof;
1633 1634
1634 retval = 0; 1635 retval = 0;
1635 spin_lock_irqsave(&tty->read_lock, flags); 1636 spin_lock_irqsave(&tty->read_lock, flags);
@@ -1639,15 +1640,15 @@ static int copy_from_read_buf(struct tty_struct *tty,
1639 if (n) { 1640 if (n) {
1640 retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n); 1641 retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
1641 n -= retval; 1642 n -= retval;
1643 is_eof = n == 1 &&
1644 tty->read_buf[tty->read_tail] == EOF_CHAR(tty);
1642 tty_audit_add_data(tty, &tty->read_buf[tty->read_tail], n); 1645 tty_audit_add_data(tty, &tty->read_buf[tty->read_tail], n);
1643 spin_lock_irqsave(&tty->read_lock, flags); 1646 spin_lock_irqsave(&tty->read_lock, flags);
1644 tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1); 1647 tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
1645 tty->read_cnt -= n; 1648 tty->read_cnt -= n;
1646 /* Turn single EOF into zero-length read */ 1649 /* Turn single EOF into zero-length read */
1647 if (L_EXTPROC(tty) && tty->icanon && n == 1) { 1650 if (L_EXTPROC(tty) && tty->icanon && is_eof && !tty->read_cnt)
1648 if (!tty->read_cnt && (*b)[n-1] == EOF_CHAR(tty)) 1651 n = 0;
1649 n--;
1650 }
1651 spin_unlock_irqrestore(&tty->read_lock, flags); 1652 spin_unlock_irqrestore(&tty->read_lock, flags);
1652 *b += n; 1653 *b += n;
1653 *nr -= n; 1654 *nr -= n;
diff --git a/drivers/tty/pty.c b/drivers/tty/pty.c
index eeae7fafe9a7..59af3945ea85 100644
--- a/drivers/tty/pty.c
+++ b/drivers/tty/pty.c
@@ -26,11 +26,13 @@
26#include <linux/bitops.h> 26#include <linux/bitops.h>
27#include <linux/devpts_fs.h> 27#include <linux/devpts_fs.h>
28#include <linux/slab.h> 28#include <linux/slab.h>
29#include <linux/mutex.h>
29 30
30 31
31#ifdef CONFIG_UNIX98_PTYS 32#ifdef CONFIG_UNIX98_PTYS
32static struct tty_driver *ptm_driver; 33static struct tty_driver *ptm_driver;
33static struct tty_driver *pts_driver; 34static struct tty_driver *pts_driver;
35static DEFINE_MUTEX(devpts_mutex);
34#endif 36#endif
35 37
36static void pty_close(struct tty_struct *tty, struct file *filp) 38static void pty_close(struct tty_struct *tty, struct file *filp)
@@ -45,6 +47,7 @@ static void pty_close(struct tty_struct *tty, struct file *filp)
45 wake_up_interruptible(&tty->read_wait); 47 wake_up_interruptible(&tty->read_wait);
46 wake_up_interruptible(&tty->write_wait); 48 wake_up_interruptible(&tty->write_wait);
47 tty->packet = 0; 49 tty->packet = 0;
50 /* Review - krefs on tty_link ?? */
48 if (!tty->link) 51 if (!tty->link)
49 return; 52 return;
50 tty->link->packet = 0; 53 tty->link->packet = 0;
@@ -54,12 +57,15 @@ static void pty_close(struct tty_struct *tty, struct file *filp)
54 if (tty->driver->subtype == PTY_TYPE_MASTER) { 57 if (tty->driver->subtype == PTY_TYPE_MASTER) {
55 set_bit(TTY_OTHER_CLOSED, &tty->flags); 58 set_bit(TTY_OTHER_CLOSED, &tty->flags);
56#ifdef CONFIG_UNIX98_PTYS 59#ifdef CONFIG_UNIX98_PTYS
57 if (tty->driver == ptm_driver) 60 if (tty->driver == ptm_driver) {
61 mutex_lock(&devpts_mutex);
58 devpts_pty_kill(tty->link); 62 devpts_pty_kill(tty->link);
63 mutex_unlock(&devpts_mutex);
64 }
59#endif 65#endif
60 tty_unlock(); 66 tty_unlock(tty);
61 tty_vhangup(tty->link); 67 tty_vhangup(tty->link);
62 tty_lock(); 68 tty_lock(tty);
63 } 69 }
64} 70}
65 71
@@ -475,13 +481,17 @@ static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
475 * @idx: tty index 481 * @idx: tty index
476 * 482 *
477 * Look up a pty master device. Called under the tty_mutex for now. 483 * Look up a pty master device. Called under the tty_mutex for now.
478 * This provides our locking. 484 * This provides our locking for the tty pointer.
479 */ 485 */
480 486
481static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver, 487static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
482 struct inode *pts_inode, int idx) 488 struct inode *pts_inode, int idx)
483{ 489{
484 struct tty_struct *tty = devpts_get_tty(pts_inode, idx); 490 struct tty_struct *tty;
491
492 mutex_lock(&devpts_mutex);
493 tty = devpts_get_tty(pts_inode, idx);
494 mutex_unlock(&devpts_mutex);
485 /* Master must be open before slave */ 495 /* Master must be open before slave */
486 if (!tty) 496 if (!tty)
487 return ERR_PTR(-EIO); 497 return ERR_PTR(-EIO);
@@ -613,24 +623,29 @@ static int ptmx_open(struct inode *inode, struct file *filp)
613 return retval; 623 return retval;
614 624
615 /* find a device that is not in use. */ 625 /* find a device that is not in use. */
616 tty_lock(); 626 mutex_lock(&devpts_mutex);
617 index = devpts_new_index(inode); 627 index = devpts_new_index(inode);
618 tty_unlock();
619 if (index < 0) { 628 if (index < 0) {
620 retval = index; 629 retval = index;
621 goto err_file; 630 goto err_file;
622 } 631 }
623 632
633 mutex_unlock(&devpts_mutex);
634
624 mutex_lock(&tty_mutex); 635 mutex_lock(&tty_mutex);
625 tty_lock(); 636 mutex_lock(&devpts_mutex);
626 tty = tty_init_dev(ptm_driver, index); 637 tty = tty_init_dev(ptm_driver, index);
627 mutex_unlock(&tty_mutex);
628 638
629 if (IS_ERR(tty)) { 639 if (IS_ERR(tty)) {
630 retval = PTR_ERR(tty); 640 retval = PTR_ERR(tty);
631 goto out; 641 goto out;
632 } 642 }
633 643
644 /* The tty returned here is locked so we can safely
645 drop the mutex */
646 mutex_unlock(&devpts_mutex);
647 mutex_unlock(&tty_mutex);
648
634 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */ 649 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
635 650
636 tty_add_file(tty, filp); 651 tty_add_file(tty, filp);
@@ -643,16 +658,17 @@ static int ptmx_open(struct inode *inode, struct file *filp)
643 if (retval) 658 if (retval)
644 goto err_release; 659 goto err_release;
645 660
646 tty_unlock(); 661 tty_unlock(tty);
647 return 0; 662 return 0;
648err_release: 663err_release:
649 tty_unlock(); 664 tty_unlock(tty);
650 tty_release(inode, filp); 665 tty_release(inode, filp);
651 return retval; 666 return retval;
652out: 667out:
668 mutex_unlock(&tty_mutex);
653 devpts_kill_index(inode, index); 669 devpts_kill_index(inode, index);
654 tty_unlock();
655err_file: 670err_file:
671 mutex_unlock(&devpts_mutex);
656 tty_free_file(filp); 672 tty_free_file(filp);
657 return retval; 673 return retval;
658} 674}
diff --git a/drivers/tty/serial/68328serial.c b/drivers/tty/serial/68328serial.c
index 5ce782529d65..3ed20e435e59 100644
--- a/drivers/tty/serial/68328serial.c
+++ b/drivers/tty/serial/68328serial.c
@@ -17,6 +17,7 @@
17#include <asm/dbg.h> 17#include <asm/dbg.h>
18#include <linux/module.h> 18#include <linux/module.h>
19#include <linux/errno.h> 19#include <linux/errno.h>
20#include <linux/serial.h>
20#include <linux/signal.h> 21#include <linux/signal.h>
21#include <linux/sched.h> 22#include <linux/sched.h>
22#include <linux/timer.h> 23#include <linux/timer.h>
@@ -56,8 +57,6 @@
56#endif /* CONFIG_M68VZ328 */ 57#endif /* CONFIG_M68VZ328 */
57#endif /* CONFIG_M68EZ328 */ 58#endif /* CONFIG_M68EZ328 */
58 59
59#include "68328serial.h"
60
61/* Turn off usage of real serial interrupt code, to "support" Copilot */ 60/* Turn off usage of real serial interrupt code, to "support" Copilot */
62#ifdef CONFIG_XCOPILOT_BUGS 61#ifdef CONFIG_XCOPILOT_BUGS
63#undef USE_INTS 62#undef USE_INTS
@@ -65,33 +64,82 @@
65#define USE_INTS 64#define USE_INTS
66#endif 65#endif
67 66
68static struct m68k_serial m68k_soft[NR_PORTS]; 67/*
68 * I believe this is the optimal setting that reduces the number of interrupts.
69 * At high speeds the output might become a little "bursted" (use USTCNT_TXHE
70 * if that bothers you), but in most cases it will not, since we try to
71 * transmit characters every time rs_interrupt is called. Thus, quite often
72 * you'll see that a receive interrupt occures before the transmit one.
73 * -- Vladimir Gurevich
74 */
75#define USTCNT_TX_INTR_MASK (USTCNT_TXEE)
69 76
70static unsigned int uart_irqs[NR_PORTS] = UART_IRQ_DEFNS; 77/*
78 * 68328 and 68EZ328 UARTS are a little bit different. EZ328 has special
79 * "Old data interrupt" which occures whenever the data stay in the FIFO
80 * longer than 30 bits time. This allows us to use FIFO without compromising
81 * latency. '328 does not have this feature and without the real 328-based
82 * board I would assume that RXRE is the safest setting.
83 *
84 * For EZ328 I use RXHE (Half empty) interrupt to reduce the number of
85 * interrupts. RXFE (receive queue full) causes the system to lose data
86 * at least at 115200 baud
87 *
88 * If your board is busy doing other stuff, you might consider to use
89 * RXRE (data ready intrrupt) instead.
90 *
91 * The other option is to make these INTR masks run-time configurable, so
92 * that people can dynamically adapt them according to the current usage.
93 * -- Vladimir Gurevich
94 */
71 95
72/* multiple ports are contiguous in memory */ 96/* (es) */
73m68328_uart *uart_addr = (m68328_uart *)USTCNT_ADDR; 97#if defined(CONFIG_M68EZ328) || defined(CONFIG_M68VZ328)
98#define USTCNT_RX_INTR_MASK (USTCNT_RXHE | USTCNT_ODEN)
99#elif defined(CONFIG_M68328)
100#define USTCNT_RX_INTR_MASK (USTCNT_RXRE)
101#else
102#error Please, define the Rx interrupt events for your CPU
103#endif
104/* (/es) */
74 105
75struct tty_struct m68k_ttys; 106/*
76struct m68k_serial *m68k_consinfo = 0; 107 * This is our internal structure for each serial port's state.
108 */
109struct m68k_serial {
110 struct tty_port tport;
111 char is_cons; /* Is this our console. */
112 int magic;
113 int baud_base;
114 int port;
115 int irq;
116 int type; /* UART type */
117 int custom_divisor;
118 int x_char; /* xon/xoff character */
119 int line;
120 unsigned char *xmit_buf;
121 int xmit_head;
122 int xmit_tail;
123 int xmit_cnt;
124};
77 125
78#define M68K_CLOCK (16667000) /* FIXME: 16MHz is likely wrong */ 126#define SERIAL_MAGIC 0x5301
79 127
80struct tty_driver *serial_driver; 128/*
129 * Define the number of ports supported and their irqs.
130 */
131#define NR_PORTS 1
81 132
82/* number of characters left in xmit buffer before we ask for more */ 133static struct m68k_serial m68k_soft[NR_PORTS];
83#define WAKEUP_CHARS 256
84 134
85/* Debugging... DEBUG_INTR is bad to use when one of the zs 135static unsigned int uart_irqs[NR_PORTS] = { UART_IRQ_NUM };
86 * lines is your console ;(
87 */
88#undef SERIAL_DEBUG_INTR
89#undef SERIAL_DEBUG_OPEN
90#undef SERIAL_DEBUG_FLOW
91 136
92#define RS_ISR_PASS_LIMIT 256 137/* multiple ports are contiguous in memory */
138m68328_uart *uart_addr = (m68328_uart *)USTCNT_ADDR;
139
140struct tty_driver *serial_driver;
93 141
94static void change_speed(struct m68k_serial *info); 142static void change_speed(struct m68k_serial *info, struct tty_struct *tty);
95 143
96/* 144/*
97 * Setup for console. Argument comes from the boot command line. 145 * Setup for console. Argument comes from the boot command line.
@@ -143,17 +191,6 @@ static int baud_table[] = {
143 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 191 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
144 9600, 19200, 38400, 57600, 115200, 0 }; 192 9600, 19200, 38400, 57600, 115200, 0 };
145 193
146/* Sets or clears DTR/RTS on the requested line */
147static inline void m68k_rtsdtr(struct m68k_serial *ss, int set)
148{
149 if (set) {
150 /* set the RTS/CTS line */
151 } else {
152 /* clear it */
153 }
154 return;
155}
156
157/* Utility routines */ 194/* Utility routines */
158static inline int get_baud(struct m68k_serial *ss) 195static inline int get_baud(struct m68k_serial *ss)
159{ 196{
@@ -189,7 +226,8 @@ static void rs_stop(struct tty_struct *tty)
189 226
190static int rs_put_char(char ch) 227static int rs_put_char(char ch)
191{ 228{
192 int flags, loops = 0; 229 unsigned long flags;
230 int loops = 0;
193 231
194 local_irq_save(flags); 232 local_irq_save(flags);
195 233
@@ -224,28 +262,9 @@ static void rs_start(struct tty_struct *tty)
224 local_irq_restore(flags); 262 local_irq_restore(flags);
225} 263}
226 264
227/* Drop into either the boot monitor or kadb upon receiving a break 265static void receive_chars(struct m68k_serial *info, struct tty_struct *tty,
228 * from keyboard/console input. 266 unsigned short rx)
229 */
230static void batten_down_hatches(void)
231{
232 /* Drop into the debugger */
233}
234
235static void status_handle(struct m68k_serial *info, unsigned short status)
236{ 267{
237 /* If this is console input and this is a
238 * 'break asserted' status change interrupt
239 * see if we can drop into the debugger
240 */
241 if((status & URX_BREAK) && info->break_abort)
242 batten_down_hatches();
243 return;
244}
245
246static void receive_chars(struct m68k_serial *info, unsigned short rx)
247{
248 struct tty_struct *tty = info->tty;
249 m68328_uart *uart = &uart_addr[info->line]; 268 m68328_uart *uart = &uart_addr[info->line];
250 unsigned char ch, flag; 269 unsigned char ch, flag;
251 270
@@ -259,7 +278,6 @@ static void receive_chars(struct m68k_serial *info, unsigned short rx)
259 278
260 if(info->is_cons) { 279 if(info->is_cons) {
261 if(URX_BREAK & rx) { /* whee, break received */ 280 if(URX_BREAK & rx) { /* whee, break received */
262 status_handle(info, rx);
263 return; 281 return;
264#ifdef CONFIG_MAGIC_SYSRQ 282#ifdef CONFIG_MAGIC_SYSRQ
265 } else if (ch == 0x10) { /* ^P */ 283 } else if (ch == 0x10) { /* ^P */
@@ -280,16 +298,13 @@ static void receive_chars(struct m68k_serial *info, unsigned short rx)
280 298
281 flag = TTY_NORMAL; 299 flag = TTY_NORMAL;
282 300
283 if(rx & URX_PARITY_ERROR) { 301 if (rx & URX_PARITY_ERROR)
284 flag = TTY_PARITY; 302 flag = TTY_PARITY;
285 status_handle(info, rx); 303 else if (rx & URX_OVRUN)
286 } else if(rx & URX_OVRUN) {
287 flag = TTY_OVERRUN; 304 flag = TTY_OVERRUN;
288 status_handle(info, rx); 305 else if (rx & URX_FRAME_ERROR)
289 } else if(rx & URX_FRAME_ERROR) {
290 flag = TTY_FRAME; 306 flag = TTY_FRAME;
291 status_handle(info, rx); 307
292 }
293 tty_insert_flip_char(tty, ch, flag); 308 tty_insert_flip_char(tty, ch, flag);
294#ifndef CONFIG_XCOPILOT_BUGS 309#ifndef CONFIG_XCOPILOT_BUGS
295 } while((rx = uart->urx.w) & URX_DATA_READY); 310 } while((rx = uart->urx.w) & URX_DATA_READY);
@@ -301,7 +316,7 @@ clear_and_exit:
301 return; 316 return;
302} 317}
303 318
304static void transmit_chars(struct m68k_serial *info) 319static void transmit_chars(struct m68k_serial *info, struct tty_struct *tty)
305{ 320{
306 m68328_uart *uart = &uart_addr[info->line]; 321 m68328_uart *uart = &uart_addr[info->line];
307 322
@@ -312,7 +327,7 @@ static void transmit_chars(struct m68k_serial *info)
312 goto clear_and_return; 327 goto clear_and_return;
313 } 328 }
314 329
315 if((info->xmit_cnt <= 0) || info->tty->stopped) { 330 if ((info->xmit_cnt <= 0) || !tty || tty->stopped) {
316 /* That's peculiar... TX ints off */ 331 /* That's peculiar... TX ints off */
317 uart->ustcnt &= ~USTCNT_TX_INTR_MASK; 332 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
318 goto clear_and_return; 333 goto clear_and_return;
@@ -340,6 +355,7 @@ clear_and_return:
340irqreturn_t rs_interrupt(int irq, void *dev_id) 355irqreturn_t rs_interrupt(int irq, void *dev_id)
341{ 356{
342 struct m68k_serial *info = dev_id; 357 struct m68k_serial *info = dev_id;
358 struct tty_struct *tty = tty_port_tty_get(&info->tport);
343 m68328_uart *uart; 359 m68328_uart *uart;
344 unsigned short rx; 360 unsigned short rx;
345 unsigned short tx; 361 unsigned short tx;
@@ -350,20 +366,24 @@ irqreturn_t rs_interrupt(int irq, void *dev_id)
350#ifdef USE_INTS 366#ifdef USE_INTS
351 tx = uart->utx.w; 367 tx = uart->utx.w;
352 368
353 if (rx & URX_DATA_READY) receive_chars(info, rx); 369 if (rx & URX_DATA_READY)
354 if (tx & UTX_TX_AVAIL) transmit_chars(info); 370 receive_chars(info, tty, rx);
371 if (tx & UTX_TX_AVAIL)
372 transmit_chars(info, tty);
355#else 373#else
356 receive_chars(info, rx); 374 receive_chars(info, tty, rx);
357#endif 375#endif
376 tty_kref_put(tty);
377
358 return IRQ_HANDLED; 378 return IRQ_HANDLED;
359} 379}
360 380
361static int startup(struct m68k_serial * info) 381static int startup(struct m68k_serial *info, struct tty_struct *tty)
362{ 382{
363 m68328_uart *uart = &uart_addr[info->line]; 383 m68328_uart *uart = &uart_addr[info->line];
364 unsigned long flags; 384 unsigned long flags;
365 385
366 if (info->flags & S_INITIALIZED) 386 if (info->tport.flags & ASYNC_INITIALIZED)
367 return 0; 387 return 0;
368 388
369 if (!info->xmit_buf) { 389 if (!info->xmit_buf) {
@@ -380,7 +400,6 @@ static int startup(struct m68k_serial * info)
380 */ 400 */
381 401
382 uart->ustcnt = USTCNT_UEN; 402 uart->ustcnt = USTCNT_UEN;
383 info->xmit_fifo_size = 1;
384 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN; 403 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
385 (void)uart->urx.w; 404 (void)uart->urx.w;
386 405
@@ -394,17 +413,17 @@ static int startup(struct m68k_serial * info)
394 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK; 413 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
395#endif 414#endif
396 415
397 if (info->tty) 416 if (tty)
398 clear_bit(TTY_IO_ERROR, &info->tty->flags); 417 clear_bit(TTY_IO_ERROR, &tty->flags);
399 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0; 418 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
400 419
401 /* 420 /*
402 * and set the speed of the serial port 421 * and set the speed of the serial port
403 */ 422 */
404 423
405 change_speed(info); 424 change_speed(info, tty);
406 425
407 info->flags |= S_INITIALIZED; 426 info->tport.flags |= ASYNC_INITIALIZED;
408 local_irq_restore(flags); 427 local_irq_restore(flags);
409 return 0; 428 return 0;
410} 429}
@@ -413,13 +432,13 @@ static int startup(struct m68k_serial * info)
413 * This routine will shutdown a serial port; interrupts are disabled, and 432 * This routine will shutdown a serial port; interrupts are disabled, and
414 * DTR is dropped if the hangup on close termio flag is on. 433 * DTR is dropped if the hangup on close termio flag is on.
415 */ 434 */
416static void shutdown(struct m68k_serial * info) 435static void shutdown(struct m68k_serial *info, struct tty_struct *tty)
417{ 436{
418 m68328_uart *uart = &uart_addr[info->line]; 437 m68328_uart *uart = &uart_addr[info->line];
419 unsigned long flags; 438 unsigned long flags;
420 439
421 uart->ustcnt = 0; /* All off! */ 440 uart->ustcnt = 0; /* All off! */
422 if (!(info->flags & S_INITIALIZED)) 441 if (!(info->tport.flags & ASYNC_INITIALIZED))
423 return; 442 return;
424 443
425 local_irq_save(flags); 444 local_irq_save(flags);
@@ -429,10 +448,10 @@ static void shutdown(struct m68k_serial * info)
429 info->xmit_buf = 0; 448 info->xmit_buf = 0;
430 } 449 }
431 450
432 if (info->tty) 451 if (tty)
433 set_bit(TTY_IO_ERROR, &info->tty->flags); 452 set_bit(TTY_IO_ERROR, &tty->flags);
434 453
435 info->flags &= ~S_INITIALIZED; 454 info->tport.flags &= ~ASYNC_INITIALIZED;
436 local_irq_restore(flags); 455 local_irq_restore(flags);
437} 456}
438 457
@@ -488,7 +507,7 @@ struct {
488 * This routine is called to set the UART divisor registers to match 507 * This routine is called to set the UART divisor registers to match
489 * the specified baud rate for a serial port. 508 * the specified baud rate for a serial port.
490 */ 509 */
491static void change_speed(struct m68k_serial *info) 510static void change_speed(struct m68k_serial *info, struct tty_struct *tty)
492{ 511{
493 m68328_uart *uart = &uart_addr[info->line]; 512 m68328_uart *uart = &uart_addr[info->line];
494 unsigned short port; 513 unsigned short port;
@@ -496,9 +515,7 @@ static void change_speed(struct m68k_serial *info)
496 unsigned cflag; 515 unsigned cflag;
497 int i; 516 int i;
498 517
499 if (!info->tty || !info->tty->termios) 518 cflag = tty->termios->c_cflag;
500 return;
501 cflag = info->tty->termios->c_cflag;
502 if (!(port = info->port)) 519 if (!(port = info->port))
503 return; 520 return;
504 521
@@ -510,7 +527,6 @@ static void change_speed(struct m68k_serial *info)
510 i = (i & ~CBAUDEX) + B38400; 527 i = (i & ~CBAUDEX) + B38400;
511 } 528 }
512 529
513 info->baud = baud_table[i];
514 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) | 530 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
515 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale); 531 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
516 532
@@ -807,10 +823,10 @@ static int get_serial_info(struct m68k_serial * info,
807 tmp.line = info->line; 823 tmp.line = info->line;
808 tmp.port = info->port; 824 tmp.port = info->port;
809 tmp.irq = info->irq; 825 tmp.irq = info->irq;
810 tmp.flags = info->flags; 826 tmp.flags = info->tport.flags;
811 tmp.baud_base = info->baud_base; 827 tmp.baud_base = info->baud_base;
812 tmp.close_delay = info->close_delay; 828 tmp.close_delay = info->tport.close_delay;
813 tmp.closing_wait = info->closing_wait; 829 tmp.closing_wait = info->tport.closing_wait;
814 tmp.custom_divisor = info->custom_divisor; 830 tmp.custom_divisor = info->custom_divisor;
815 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) 831 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
816 return -EFAULT; 832 return -EFAULT;
@@ -818,9 +834,10 @@ static int get_serial_info(struct m68k_serial * info,
818 return 0; 834 return 0;
819} 835}
820 836
821static int set_serial_info(struct m68k_serial * info, 837static int set_serial_info(struct m68k_serial *info, struct tty_struct *tty,
822 struct serial_struct * new_info) 838 struct serial_struct * new_info)
823{ 839{
840 struct tty_port *port = &info->tport;
824 struct serial_struct new_serial; 841 struct serial_struct new_serial;
825 struct m68k_serial old_info; 842 struct m68k_serial old_info;
826 int retval = 0; 843 int retval = 0;
@@ -834,17 +851,17 @@ static int set_serial_info(struct m68k_serial * info,
834 if (!capable(CAP_SYS_ADMIN)) { 851 if (!capable(CAP_SYS_ADMIN)) {
835 if ((new_serial.baud_base != info->baud_base) || 852 if ((new_serial.baud_base != info->baud_base) ||
836 (new_serial.type != info->type) || 853 (new_serial.type != info->type) ||
837 (new_serial.close_delay != info->close_delay) || 854 (new_serial.close_delay != port->close_delay) ||
838 ((new_serial.flags & ~S_USR_MASK) != 855 ((new_serial.flags & ~ASYNC_USR_MASK) !=
839 (info->flags & ~S_USR_MASK))) 856 (port->flags & ~ASYNC_USR_MASK)))
840 return -EPERM; 857 return -EPERM;
841 info->flags = ((info->flags & ~S_USR_MASK) | 858 port->flags = ((port->flags & ~ASYNC_USR_MASK) |
842 (new_serial.flags & S_USR_MASK)); 859 (new_serial.flags & ASYNC_USR_MASK));
843 info->custom_divisor = new_serial.custom_divisor; 860 info->custom_divisor = new_serial.custom_divisor;
844 goto check_and_exit; 861 goto check_and_exit;
845 } 862 }
846 863
847 if (info->count > 1) 864 if (port->count > 1)
848 return -EBUSY; 865 return -EBUSY;
849 866
850 /* 867 /*
@@ -853,14 +870,14 @@ static int set_serial_info(struct m68k_serial * info,
853 */ 870 */
854 871
855 info->baud_base = new_serial.baud_base; 872 info->baud_base = new_serial.baud_base;
856 info->flags = ((info->flags & ~S_FLAGS) | 873 port->flags = ((port->flags & ~ASYNC_FLAGS) |
857 (new_serial.flags & S_FLAGS)); 874 (new_serial.flags & ASYNC_FLAGS));
858 info->type = new_serial.type; 875 info->type = new_serial.type;
859 info->close_delay = new_serial.close_delay; 876 port->close_delay = new_serial.close_delay;
860 info->closing_wait = new_serial.closing_wait; 877 port->closing_wait = new_serial.closing_wait;
861 878
862check_and_exit: 879check_and_exit:
863 retval = startup(info); 880 retval = startup(info, tty);
864 return retval; 881 return retval;
865} 882}
866 883
@@ -946,7 +963,7 @@ static int rs_ioctl(struct tty_struct *tty,
946 return get_serial_info(info, 963 return get_serial_info(info,
947 (struct serial_struct *) arg); 964 (struct serial_struct *) arg);
948 case TIOCSSERIAL: 965 case TIOCSSERIAL:
949 return set_serial_info(info, 966 return set_serial_info(info, tty,
950 (struct serial_struct *) arg); 967 (struct serial_struct *) arg);
951 case TIOCSERGETLSR: /* Get line status register */ 968 case TIOCSERGETLSR: /* Get line status register */
952 return get_lsr_info(info, (unsigned int *) arg); 969 return get_lsr_info(info, (unsigned int *) arg);
@@ -965,7 +982,7 @@ static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
965{ 982{
966 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data; 983 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
967 984
968 change_speed(info); 985 change_speed(info, tty);
969 986
970 if ((old_termios->c_cflag & CRTSCTS) && 987 if ((old_termios->c_cflag & CRTSCTS) &&
971 !(tty->termios->c_cflag & CRTSCTS)) { 988 !(tty->termios->c_cflag & CRTSCTS)) {
@@ -988,6 +1005,7 @@ static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
988static void rs_close(struct tty_struct *tty, struct file * filp) 1005static void rs_close(struct tty_struct *tty, struct file * filp)
989{ 1006{
990 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data; 1007 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1008 struct tty_port *port = &info->tport;
991 m68328_uart *uart = &uart_addr[info->line]; 1009 m68328_uart *uart = &uart_addr[info->line];
992 unsigned long flags; 1010 unsigned long flags;
993 1011
@@ -1001,7 +1019,7 @@ static void rs_close(struct tty_struct *tty, struct file * filp)
1001 return; 1019 return;
1002 } 1020 }
1003 1021
1004 if ((tty->count == 1) && (info->count != 1)) { 1022 if ((tty->count == 1) && (port->count != 1)) {
1005 /* 1023 /*
1006 * Uh, oh. tty->count is 1, which means that the tty 1024 * Uh, oh. tty->count is 1, which means that the tty
1007 * structure will be freed. Info->count should always 1025 * structure will be freed. Info->count should always
@@ -1010,26 +1028,26 @@ static void rs_close(struct tty_struct *tty, struct file * filp)
1010 * serial port won't be shutdown. 1028 * serial port won't be shutdown.
1011 */ 1029 */
1012 printk("rs_close: bad serial port count; tty->count is 1, " 1030 printk("rs_close: bad serial port count; tty->count is 1, "
1013 "info->count is %d\n", info->count); 1031 "port->count is %d\n", port->count);
1014 info->count = 1; 1032 port->count = 1;
1015 } 1033 }
1016 if (--info->count < 0) { 1034 if (--port->count < 0) {
1017 printk("rs_close: bad serial port count for ttyS%d: %d\n", 1035 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1018 info->line, info->count); 1036 info->line, port->count);
1019 info->count = 0; 1037 port->count = 0;
1020 } 1038 }
1021 if (info->count) { 1039 if (port->count) {
1022 local_irq_restore(flags); 1040 local_irq_restore(flags);
1023 return; 1041 return;
1024 } 1042 }
1025 info->flags |= S_CLOSING; 1043 port->flags |= ASYNC_CLOSING;
1026 /* 1044 /*
1027 * Now we wait for the transmit buffer to clear; and we notify 1045 * Now we wait for the transmit buffer to clear; and we notify
1028 * the line discipline to only process XON/XOFF characters. 1046 * the line discipline to only process XON/XOFF characters.
1029 */ 1047 */
1030 tty->closing = 1; 1048 tty->closing = 1;
1031 if (info->closing_wait != S_CLOSING_WAIT_NONE) 1049 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1032 tty_wait_until_sent(tty, info->closing_wait); 1050 tty_wait_until_sent(tty, port->closing_wait);
1033 /* 1051 /*
1034 * At this point we stop accepting input. To do this, we 1052 * At this point we stop accepting input. To do this, we
1035 * disable the receive line status interrupts, and tell the 1053 * disable the receive line status interrupts, and tell the
@@ -1040,13 +1058,12 @@ static void rs_close(struct tty_struct *tty, struct file * filp)
1040 uart->ustcnt &= ~USTCNT_RXEN; 1058 uart->ustcnt &= ~USTCNT_RXEN;
1041 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK); 1059 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1042 1060
1043 shutdown(info); 1061 shutdown(info, tty);
1044 rs_flush_buffer(tty); 1062 rs_flush_buffer(tty);
1045 1063
1046 tty_ldisc_flush(tty); 1064 tty_ldisc_flush(tty);
1047 tty->closing = 0; 1065 tty->closing = 0;
1048 info->event = 0; 1066 tty_port_tty_set(&info->tport, NULL);
1049 info->tty = NULL;
1050#warning "This is not and has never been valid so fix it" 1067#warning "This is not and has never been valid so fix it"
1051#if 0 1068#if 0
1052 if (tty->ldisc.num != ldiscs[N_TTY].num) { 1069 if (tty->ldisc.num != ldiscs[N_TTY].num) {
@@ -1058,14 +1075,13 @@ static void rs_close(struct tty_struct *tty, struct file * filp)
1058 (tty->ldisc.open)(tty); 1075 (tty->ldisc.open)(tty);
1059 } 1076 }
1060#endif 1077#endif
1061 if (info->blocked_open) { 1078 if (port->blocked_open) {
1062 if (info->close_delay) { 1079 if (port->close_delay)
1063 msleep_interruptible(jiffies_to_msecs(info->close_delay)); 1080 msleep_interruptible(jiffies_to_msecs(port->close_delay));
1064 } 1081 wake_up_interruptible(&port->open_wait);
1065 wake_up_interruptible(&info->open_wait);
1066 } 1082 }
1067 info->flags &= ~(S_NORMAL_ACTIVE|S_CLOSING); 1083 port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1068 wake_up_interruptible(&info->close_wait); 1084 wake_up_interruptible(&port->close_wait);
1069 local_irq_restore(flags); 1085 local_irq_restore(flags);
1070} 1086}
1071 1087
@@ -1080,107 +1096,14 @@ void rs_hangup(struct tty_struct *tty)
1080 return; 1096 return;
1081 1097
1082 rs_flush_buffer(tty); 1098 rs_flush_buffer(tty);
1083 shutdown(info); 1099 shutdown(info, tty);
1084 info->event = 0; 1100 info->tport.count = 0;
1085 info->count = 0; 1101 info->tport.flags &= ~ASYNC_NORMAL_ACTIVE;
1086 info->flags &= ~S_NORMAL_ACTIVE; 1102 tty_port_tty_set(&info->tport, NULL);
1087 info->tty = NULL; 1103 wake_up_interruptible(&info->tport.open_wait);
1088 wake_up_interruptible(&info->open_wait);
1089} 1104}
1090 1105
1091/* 1106/*
1092 * ------------------------------------------------------------
1093 * rs_open() and friends
1094 * ------------------------------------------------------------
1095 */
1096static int block_til_ready(struct tty_struct *tty, struct file * filp,
1097 struct m68k_serial *info)
1098{
1099 DECLARE_WAITQUEUE(wait, current);
1100 int retval;
1101 int do_clocal = 0;
1102
1103 /*
1104 * If the device is in the middle of being closed, then block
1105 * until it's done, and then try again.
1106 */
1107 if (info->flags & S_CLOSING) {
1108 interruptible_sleep_on(&info->close_wait);
1109#ifdef SERIAL_DO_RESTART
1110 if (info->flags & S_HUP_NOTIFY)
1111 return -EAGAIN;
1112 else
1113 return -ERESTARTSYS;
1114#else
1115 return -EAGAIN;
1116#endif
1117 }
1118
1119 /*
1120 * If non-blocking mode is set, or the port is not enabled,
1121 * then make the check up front and then exit.
1122 */
1123 if ((filp->f_flags & O_NONBLOCK) ||
1124 (tty->flags & (1 << TTY_IO_ERROR))) {
1125 info->flags |= S_NORMAL_ACTIVE;
1126 return 0;
1127 }
1128
1129 if (tty->termios->c_cflag & CLOCAL)
1130 do_clocal = 1;
1131
1132 /*
1133 * Block waiting for the carrier detect and the line to become
1134 * free (i.e., not in use by the callout). While we are in
1135 * this loop, info->count is dropped by one, so that
1136 * rs_close() knows when to free things. We restore it upon
1137 * exit, either normal or abnormal.
1138 */
1139 retval = 0;
1140 add_wait_queue(&info->open_wait, &wait);
1141
1142 info->count--;
1143 info->blocked_open++;
1144 while (1) {
1145 local_irq_disable();
1146 m68k_rtsdtr(info, 1);
1147 local_irq_enable();
1148 current->state = TASK_INTERRUPTIBLE;
1149 if (tty_hung_up_p(filp) ||
1150 !(info->flags & S_INITIALIZED)) {
1151#ifdef SERIAL_DO_RESTART
1152 if (info->flags & S_HUP_NOTIFY)
1153 retval = -EAGAIN;
1154 else
1155 retval = -ERESTARTSYS;
1156#else
1157 retval = -EAGAIN;
1158#endif
1159 break;
1160 }
1161 if (!(info->flags & S_CLOSING) && do_clocal)
1162 break;
1163 if (signal_pending(current)) {
1164 retval = -ERESTARTSYS;
1165 break;
1166 }
1167 tty_unlock();
1168 schedule();
1169 tty_lock();
1170 }
1171 current->state = TASK_RUNNING;
1172 remove_wait_queue(&info->open_wait, &wait);
1173 if (!tty_hung_up_p(filp))
1174 info->count++;
1175 info->blocked_open--;
1176
1177 if (retval)
1178 return retval;
1179 info->flags |= S_NORMAL_ACTIVE;
1180 return 0;
1181}
1182
1183/*
1184 * This routine is called whenever a serial port is opened. It 1107 * This routine is called whenever a serial port is opened. It
1185 * enables interrupts for a serial port, linking in its S structure into 1108 * enables interrupts for a serial port, linking in its S structure into
1186 * the IRQ chain. It also performs the serial-specific 1109 * the IRQ chain. It also performs the serial-specific
@@ -1196,18 +1119,18 @@ int rs_open(struct tty_struct *tty, struct file * filp)
1196 if (serial_paranoia_check(info, tty->name, "rs_open")) 1119 if (serial_paranoia_check(info, tty->name, "rs_open"))
1197 return -ENODEV; 1120 return -ENODEV;
1198 1121
1199 info->count++; 1122 info->tport.count++;
1200 tty->driver_data = info; 1123 tty->driver_data = info;
1201 info->tty = tty; 1124 tty_port_tty_set(&info->tport, tty);
1202 1125
1203 /* 1126 /*
1204 * Start up serial port 1127 * Start up serial port
1205 */ 1128 */
1206 retval = startup(info); 1129 retval = startup(info, tty);
1207 if (retval) 1130 if (retval)
1208 return retval; 1131 return retval;
1209 1132
1210 return block_til_ready(tty, filp, info); 1133 return tty_port_block_til_ready(&info->tport, tty, filp);
1211} 1134}
1212 1135
1213/* Finally, routines used to initialize the serial driver. */ 1136/* Finally, routines used to initialize the serial driver. */
@@ -1235,11 +1158,15 @@ static const struct tty_operations rs_ops = {
1235 .set_ldisc = rs_set_ldisc, 1158 .set_ldisc = rs_set_ldisc,
1236}; 1159};
1237 1160
1161static const struct tty_port_operations rs_port_ops = {
1162};
1163
1238/* rs_init inits the driver */ 1164/* rs_init inits the driver */
1239static int __init 1165static int __init
1240rs68328_init(void) 1166rs68328_init(void)
1241{ 1167{
1242 int flags, i; 1168 unsigned long flags;
1169 int i;
1243 struct m68k_serial *info; 1170 struct m68k_serial *info;
1244 1171
1245 serial_driver = alloc_tty_driver(NR_PORTS); 1172 serial_driver = alloc_tty_driver(NR_PORTS);
@@ -1273,19 +1200,13 @@ rs68328_init(void)
1273 for(i=0;i<NR_PORTS;i++) { 1200 for(i=0;i<NR_PORTS;i++) {
1274 1201
1275 info = &m68k_soft[i]; 1202 info = &m68k_soft[i];
1203 tty_port_init(&info->tport);
1204 info->tport.ops = &rs_port_ops;
1276 info->magic = SERIAL_MAGIC; 1205 info->magic = SERIAL_MAGIC;
1277 info->port = (int) &uart_addr[i]; 1206 info->port = (int) &uart_addr[i];
1278 info->tty = NULL;
1279 info->irq = uart_irqs[i]; 1207 info->irq = uart_irqs[i];
1280 info->custom_divisor = 16; 1208 info->custom_divisor = 16;
1281 info->close_delay = 50;
1282 info->closing_wait = 3000;
1283 info->x_char = 0; 1209 info->x_char = 0;
1284 info->event = 0;
1285 info->count = 0;
1286 info->blocked_open = 0;
1287 init_waitqueue_head(&info->open_wait);
1288 init_waitqueue_head(&info->close_wait);
1289 info->line = i; 1210 info->line = i;
1290 info->is_cons = 1; /* Means shortcuts work */ 1211 info->is_cons = 1; /* Means shortcuts work */
1291 1212
diff --git a/drivers/tty/serial/68328serial.h b/drivers/tty/serial/68328serial.h
deleted file mode 100644
index 3d2faabd766f..000000000000
--- a/drivers/tty/serial/68328serial.h
+++ /dev/null
@@ -1,186 +0,0 @@
1/* 68328serial.h: Definitions for the mc68328 serial driver.
2 *
3 * Copyright (C) 1995 David S. Miller <davem@caip.rutgers.edu>
4 * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>
5 * Copyright (C) 1998, 1999 D. Jeff Dionne <jeff@uclinux.org>
6 * Copyright (C) 1999 Vladimir Gurevich <vgurevic@cisco.com>
7 *
8 * VZ Support/Fixes Evan Stawnyczy <e@lineo.ca>
9 */
10
11#ifndef _MC683XX_SERIAL_H
12#define _MC683XX_SERIAL_H
13
14
15struct serial_struct {
16 int type;
17 int line;
18 int port;
19 int irq;
20 int flags;
21 int xmit_fifo_size;
22 int custom_divisor;
23 int baud_base;
24 unsigned short close_delay;
25 char reserved_char[2];
26 int hub6; /* FIXME: We don't have AT&T Hub6 boards! */
27 unsigned short closing_wait; /* time to wait before closing */
28 unsigned short closing_wait2; /* no longer used... */
29 int reserved[4];
30};
31
32/*
33 * For the close wait times, 0 means wait forever for serial port to
34 * flush its output. 65535 means don't wait at all.
35 */
36#define S_CLOSING_WAIT_INF 0
37#define S_CLOSING_WAIT_NONE 65535
38
39/*
40 * Definitions for S_struct (and serial_struct) flags field
41 */
42#define S_HUP_NOTIFY 0x0001 /* Notify getty on hangups and closes
43 on the callout port */
44#define S_FOURPORT 0x0002 /* Set OU1, OUT2 per AST Fourport settings */
45#define S_SAK 0x0004 /* Secure Attention Key (Orange book) */
46#define S_SPLIT_TERMIOS 0x0008 /* Separate termios for dialin/callout */
47
48#define S_SPD_MASK 0x0030
49#define S_SPD_HI 0x0010 /* Use 56000 instead of 38400 bps */
50
51#define S_SPD_VHI 0x0020 /* Use 115200 instead of 38400 bps */
52#define S_SPD_CUST 0x0030 /* Use user-specified divisor */
53
54#define S_SKIP_TEST 0x0040 /* Skip UART test during autoconfiguration */
55#define S_AUTO_IRQ 0x0080 /* Do automatic IRQ during autoconfiguration */
56#define S_SESSION_LOCKOUT 0x0100 /* Lock out cua opens based on session */
57#define S_PGRP_LOCKOUT 0x0200 /* Lock out cua opens based on pgrp */
58#define S_CALLOUT_NOHUP 0x0400 /* Don't do hangups for cua device */
59
60#define S_FLAGS 0x0FFF /* Possible legal S flags */
61#define S_USR_MASK 0x0430 /* Legal flags that non-privileged
62 * users can set or reset */
63
64/* Internal flags used only by kernel/chr_drv/serial.c */
65#define S_INITIALIZED 0x80000000 /* Serial port was initialized */
66#define S_CALLOUT_ACTIVE 0x40000000 /* Call out device is active */
67#define S_NORMAL_ACTIVE 0x20000000 /* Normal device is active */
68#define S_BOOT_AUTOCONF 0x10000000 /* Autoconfigure port on bootup */
69#define S_CLOSING 0x08000000 /* Serial port is closing */
70#define S_CTS_FLOW 0x04000000 /* Do CTS flow control */
71#define S_CHECK_CD 0x02000000 /* i.e., CLOCAL */
72
73/* Software state per channel */
74
75#ifdef __KERNEL__
76
77/*
78 * I believe this is the optimal setting that reduces the number of interrupts.
79 * At high speeds the output might become a little "bursted" (use USTCNT_TXHE
80 * if that bothers you), but in most cases it will not, since we try to
81 * transmit characters every time rs_interrupt is called. Thus, quite often
82 * you'll see that a receive interrupt occures before the transmit one.
83 * -- Vladimir Gurevich
84 */
85#define USTCNT_TX_INTR_MASK (USTCNT_TXEE)
86
87/*
88 * 68328 and 68EZ328 UARTS are a little bit different. EZ328 has special
89 * "Old data interrupt" which occures whenever the data stay in the FIFO
90 * longer than 30 bits time. This allows us to use FIFO without compromising
91 * latency. '328 does not have this feature and without the real 328-based
92 * board I would assume that RXRE is the safest setting.
93 *
94 * For EZ328 I use RXHE (Half empty) interrupt to reduce the number of
95 * interrupts. RXFE (receive queue full) causes the system to lose data
96 * at least at 115200 baud
97 *
98 * If your board is busy doing other stuff, you might consider to use
99 * RXRE (data ready intrrupt) instead.
100 *
101 * The other option is to make these INTR masks run-time configurable, so
102 * that people can dynamically adapt them according to the current usage.
103 * -- Vladimir Gurevich
104 */
105
106/* (es) */
107#if defined(CONFIG_M68EZ328) || defined(CONFIG_M68VZ328)
108#define USTCNT_RX_INTR_MASK (USTCNT_RXHE | USTCNT_ODEN)
109#elif defined(CONFIG_M68328)
110#define USTCNT_RX_INTR_MASK (USTCNT_RXRE)
111#else
112#error Please, define the Rx interrupt events for your CPU
113#endif
114/* (/es) */
115
116/*
117 * This is our internal structure for each serial port's state.
118 *
119 * Many fields are paralleled by the structure used by the serial_struct
120 * structure.
121 *
122 * For definitions of the flags field, see tty.h
123 */
124
125struct m68k_serial {
126 char soft_carrier; /* Use soft carrier on this channel */
127 char break_abort; /* Is serial console in, so process brk/abrt */
128 char is_cons; /* Is this our console. */
129
130 /* We need to know the current clock divisor
131 * to read the bps rate the chip has currently
132 * loaded.
133 */
134 unsigned char clk_divisor; /* May be 1, 16, 32, or 64 */
135 int baud;
136 int magic;
137 int baud_base;
138 int port;
139 int irq;
140 int flags; /* defined in tty.h */
141 int type; /* UART type */
142 struct tty_struct *tty;
143 int read_status_mask;
144 int ignore_status_mask;
145 int timeout;
146 int xmit_fifo_size;
147 int custom_divisor;
148 int x_char; /* xon/xoff character */
149 int close_delay;
150 unsigned short closing_wait;
151 unsigned short closing_wait2;
152 unsigned long event;
153 unsigned long last_active;
154 int line;
155 int count; /* # of fd on device */
156 int blocked_open; /* # of blocked opens */
157 unsigned char *xmit_buf;
158 int xmit_head;
159 int xmit_tail;
160 int xmit_cnt;
161 wait_queue_head_t open_wait;
162 wait_queue_head_t close_wait;
163};
164
165
166#define SERIAL_MAGIC 0x5301
167
168/*
169 * The size of the serial xmit buffer is 1 page, or 4096 bytes
170 */
171#define SERIAL_XMIT_SIZE 4096
172
173/*
174 * Events are used to schedule things to happen at timer-interrupt
175 * time, instead of at rs interrupt time.
176 */
177#define RS_EVENT_WRITE_WAKEUP 0
178
179/*
180 * Define the number of ports supported and their irqs.
181 */
182#define NR_PORTS 1
183#define UART_IRQ_DEFNS {UART_IRQ_NUM}
184
185#endif /* __KERNEL__ */
186#endif /* !(_MC683XX_SERIAL_H) */
diff --git a/drivers/tty/serial/8250/8250.c b/drivers/tty/serial/8250/8250.c
index 5c27f7e6c9f1..47d061b9ad4d 100644
--- a/drivers/tty/serial/8250/8250.c
+++ b/drivers/tty/serial/8250/8250.c
@@ -284,7 +284,20 @@ static const struct serial8250_config uart_config[] = {
284 }, 284 },
285}; 285};
286 286
287#if defined(CONFIG_MIPS_ALCHEMY) 287/* Uart divisor latch read */
288static int default_serial_dl_read(struct uart_8250_port *up)
289{
290 return serial_in(up, UART_DLL) | serial_in(up, UART_DLM) << 8;
291}
292
293/* Uart divisor latch write */
294static void default_serial_dl_write(struct uart_8250_port *up, int value)
295{
296 serial_out(up, UART_DLL, value & 0xff);
297 serial_out(up, UART_DLM, value >> 8 & 0xff);
298}
299
300#ifdef CONFIG_MIPS_ALCHEMY
288 301
289/* Au1x00 UART hardware has a weird register layout */ 302/* Au1x00 UART hardware has a weird register layout */
290static const u8 au_io_in_map[] = { 303static const u8 au_io_in_map[] = {
@@ -305,22 +318,32 @@ static const u8 au_io_out_map[] = {
305 [UART_MCR] = 6, 318 [UART_MCR] = 6,
306}; 319};
307 320
308/* sane hardware needs no mapping */ 321static unsigned int au_serial_in(struct uart_port *p, int offset)
309static inline int map_8250_in_reg(struct uart_port *p, int offset) 322{
323 offset = au_io_in_map[offset] << p->regshift;
324 return __raw_readl(p->membase + offset);
325}
326
327static void au_serial_out(struct uart_port *p, int offset, int value)
328{
329 offset = au_io_out_map[offset] << p->regshift;
330 __raw_writel(value, p->membase + offset);
331}
332
333/* Au1x00 haven't got a standard divisor latch */
334static int au_serial_dl_read(struct uart_8250_port *up)
310{ 335{
311 if (p->iotype != UPIO_AU) 336 return __raw_readl(up->port.membase + 0x28);
312 return offset;
313 return au_io_in_map[offset];
314} 337}
315 338
316static inline int map_8250_out_reg(struct uart_port *p, int offset) 339static void au_serial_dl_write(struct uart_8250_port *up, int value)
317{ 340{
318 if (p->iotype != UPIO_AU) 341 __raw_writel(value, up->port.membase + 0x28);
319 return offset;
320 return au_io_out_map[offset];
321} 342}
322 343
323#elif defined(CONFIG_SERIAL_8250_RM9K) 344#endif
345
346#ifdef CONFIG_SERIAL_8250_RM9K
324 347
325static const u8 348static const u8
326 regmap_in[8] = { 349 regmap_in[8] = {
@@ -344,87 +367,79 @@ static const u8
344 [UART_SCR] = 0x2c 367 [UART_SCR] = 0x2c
345 }; 368 };
346 369
347static inline int map_8250_in_reg(struct uart_port *p, int offset) 370static unsigned int rm9k_serial_in(struct uart_port *p, int offset)
348{ 371{
349 if (p->iotype != UPIO_RM9000) 372 offset = regmap_in[offset] << p->regshift;
350 return offset; 373 return readl(p->membase + offset);
351 return regmap_in[offset];
352} 374}
353 375
354static inline int map_8250_out_reg(struct uart_port *p, int offset) 376static void rm9k_serial_out(struct uart_port *p, int offset, int value)
355{ 377{
356 if (p->iotype != UPIO_RM9000) 378 offset = regmap_out[offset] << p->regshift;
357 return offset; 379 writel(value, p->membase + offset);
358 return regmap_out[offset];
359} 380}
360 381
361#else 382static int rm9k_serial_dl_read(struct uart_8250_port *up)
383{
384 return ((__raw_readl(up->port.membase + 0x10) << 8) |
385 (__raw_readl(up->port.membase + 0x08) & 0xff)) & 0xffff;
386}
362 387
363/* sane hardware needs no mapping */ 388static void rm9k_serial_dl_write(struct uart_8250_port *up, int value)
364#define map_8250_in_reg(up, offset) (offset) 389{
365#define map_8250_out_reg(up, offset) (offset) 390 __raw_writel(value, up->port.membase + 0x08);
391 __raw_writel(value >> 8, up->port.membase + 0x10);
392}
366 393
367#endif 394#endif
368 395
369static unsigned int hub6_serial_in(struct uart_port *p, int offset) 396static unsigned int hub6_serial_in(struct uart_port *p, int offset)
370{ 397{
371 offset = map_8250_in_reg(p, offset) << p->regshift; 398 offset = offset << p->regshift;
372 outb(p->hub6 - 1 + offset, p->iobase); 399 outb(p->hub6 - 1 + offset, p->iobase);
373 return inb(p->iobase + 1); 400 return inb(p->iobase + 1);
374} 401}
375 402
376static void hub6_serial_out(struct uart_port *p, int offset, int value) 403static void hub6_serial_out(struct uart_port *p, int offset, int value)
377{ 404{
378 offset = map_8250_out_reg(p, offset) << p->regshift; 405 offset = offset << p->regshift;
379 outb(p->hub6 - 1 + offset, p->iobase); 406 outb(p->hub6 - 1 + offset, p->iobase);
380 outb(value, p->iobase + 1); 407 outb(value, p->iobase + 1);
381} 408}
382 409
383static unsigned int mem_serial_in(struct uart_port *p, int offset) 410static unsigned int mem_serial_in(struct uart_port *p, int offset)
384{ 411{
385 offset = map_8250_in_reg(p, offset) << p->regshift; 412 offset = offset << p->regshift;
386 return readb(p->membase + offset); 413 return readb(p->membase + offset);
387} 414}
388 415
389static void mem_serial_out(struct uart_port *p, int offset, int value) 416static void mem_serial_out(struct uart_port *p, int offset, int value)
390{ 417{
391 offset = map_8250_out_reg(p, offset) << p->regshift; 418 offset = offset << p->regshift;
392 writeb(value, p->membase + offset); 419 writeb(value, p->membase + offset);
393} 420}
394 421
395static void mem32_serial_out(struct uart_port *p, int offset, int value) 422static void mem32_serial_out(struct uart_port *p, int offset, int value)
396{ 423{
397 offset = map_8250_out_reg(p, offset) << p->regshift; 424 offset = offset << p->regshift;
398 writel(value, p->membase + offset); 425 writel(value, p->membase + offset);
399} 426}
400 427
401static unsigned int mem32_serial_in(struct uart_port *p, int offset) 428static unsigned int mem32_serial_in(struct uart_port *p, int offset)
402{ 429{
403 offset = map_8250_in_reg(p, offset) << p->regshift; 430 offset = offset << p->regshift;
404 return readl(p->membase + offset); 431 return readl(p->membase + offset);
405} 432}
406 433
407static unsigned int au_serial_in(struct uart_port *p, int offset)
408{
409 offset = map_8250_in_reg(p, offset) << p->regshift;
410 return __raw_readl(p->membase + offset);
411}
412
413static void au_serial_out(struct uart_port *p, int offset, int value)
414{
415 offset = map_8250_out_reg(p, offset) << p->regshift;
416 __raw_writel(value, p->membase + offset);
417}
418
419static unsigned int io_serial_in(struct uart_port *p, int offset) 434static unsigned int io_serial_in(struct uart_port *p, int offset)
420{ 435{
421 offset = map_8250_in_reg(p, offset) << p->regshift; 436 offset = offset << p->regshift;
422 return inb(p->iobase + offset); 437 return inb(p->iobase + offset);
423} 438}
424 439
425static void io_serial_out(struct uart_port *p, int offset, int value) 440static void io_serial_out(struct uart_port *p, int offset, int value)
426{ 441{
427 offset = map_8250_out_reg(p, offset) << p->regshift; 442 offset = offset << p->regshift;
428 outb(value, p->iobase + offset); 443 outb(value, p->iobase + offset);
429} 444}
430 445
@@ -434,6 +449,10 @@ static void set_io_from_upio(struct uart_port *p)
434{ 449{
435 struct uart_8250_port *up = 450 struct uart_8250_port *up =
436 container_of(p, struct uart_8250_port, port); 451 container_of(p, struct uart_8250_port, port);
452
453 up->dl_read = default_serial_dl_read;
454 up->dl_write = default_serial_dl_write;
455
437 switch (p->iotype) { 456 switch (p->iotype) {
438 case UPIO_HUB6: 457 case UPIO_HUB6:
439 p->serial_in = hub6_serial_in; 458 p->serial_in = hub6_serial_in;
@@ -445,16 +464,28 @@ static void set_io_from_upio(struct uart_port *p)
445 p->serial_out = mem_serial_out; 464 p->serial_out = mem_serial_out;
446 break; 465 break;
447 466
448 case UPIO_RM9000:
449 case UPIO_MEM32: 467 case UPIO_MEM32:
450 p->serial_in = mem32_serial_in; 468 p->serial_in = mem32_serial_in;
451 p->serial_out = mem32_serial_out; 469 p->serial_out = mem32_serial_out;
452 break; 470 break;
453 471
472#ifdef CONFIG_SERIAL_8250_RM9K
473 case UPIO_RM9000:
474 p->serial_in = rm9k_serial_in;
475 p->serial_out = rm9k_serial_out;
476 up->dl_read = rm9k_serial_dl_read;
477 up->dl_write = rm9k_serial_dl_write;
478 break;
479#endif
480
481#ifdef CONFIG_MIPS_ALCHEMY
454 case UPIO_AU: 482 case UPIO_AU:
455 p->serial_in = au_serial_in; 483 p->serial_in = au_serial_in;
456 p->serial_out = au_serial_out; 484 p->serial_out = au_serial_out;
485 up->dl_read = au_serial_dl_read;
486 up->dl_write = au_serial_dl_write;
457 break; 487 break;
488#endif
458 489
459 default: 490 default:
460 p->serial_in = io_serial_in; 491 p->serial_in = io_serial_in;
@@ -481,59 +512,6 @@ serial_port_out_sync(struct uart_port *p, int offset, int value)
481 } 512 }
482} 513}
483 514
484/* Uart divisor latch read */
485static inline int _serial_dl_read(struct uart_8250_port *up)
486{
487 return serial_in(up, UART_DLL) | serial_in(up, UART_DLM) << 8;
488}
489
490/* Uart divisor latch write */
491static inline void _serial_dl_write(struct uart_8250_port *up, int value)
492{
493 serial_out(up, UART_DLL, value & 0xff);
494 serial_out(up, UART_DLM, value >> 8 & 0xff);
495}
496
497#if defined(CONFIG_MIPS_ALCHEMY)
498/* Au1x00 haven't got a standard divisor latch */
499static int serial_dl_read(struct uart_8250_port *up)
500{
501 if (up->port.iotype == UPIO_AU)
502 return __raw_readl(up->port.membase + 0x28);
503 else
504 return _serial_dl_read(up);
505}
506
507static void serial_dl_write(struct uart_8250_port *up, int value)
508{
509 if (up->port.iotype == UPIO_AU)
510 __raw_writel(value, up->port.membase + 0x28);
511 else
512 _serial_dl_write(up, value);
513}
514#elif defined(CONFIG_SERIAL_8250_RM9K)
515static int serial_dl_read(struct uart_8250_port *up)
516{
517 return (up->port.iotype == UPIO_RM9000) ?
518 (((__raw_readl(up->port.membase + 0x10) << 8) |
519 (__raw_readl(up->port.membase + 0x08) & 0xff)) & 0xffff) :
520 _serial_dl_read(up);
521}
522
523static void serial_dl_write(struct uart_8250_port *up, int value)
524{
525 if (up->port.iotype == UPIO_RM9000) {
526 __raw_writel(value, up->port.membase + 0x08);
527 __raw_writel(value >> 8, up->port.membase + 0x10);
528 } else {
529 _serial_dl_write(up, value);
530 }
531}
532#else
533#define serial_dl_read(up) _serial_dl_read(up)
534#define serial_dl_write(up, value) _serial_dl_write(up, value)
535#endif
536
537/* 515/*
538 * For the 16C950 516 * For the 16C950
539 */ 517 */
@@ -568,6 +546,16 @@ static void serial8250_clear_fifos(struct uart_8250_port *p)
568 } 546 }
569} 547}
570 548
549void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p)
550{
551 unsigned char fcr;
552
553 serial8250_clear_fifos(p);
554 fcr = uart_config[p->port.type].fcr;
555 serial_out(p, UART_FCR, fcr);
556}
557EXPORT_SYMBOL_GPL(serial8250_clear_and_reinit_fifos);
558
571/* 559/*
572 * IER sleep support. UARTs which have EFRs need the "extended 560 * IER sleep support. UARTs which have EFRs need the "extended
573 * capability" bit enabled. Note that on XR16C850s, we need to 561 * capability" bit enabled. Note that on XR16C850s, we need to
@@ -1332,27 +1320,6 @@ static void serial8250_enable_ms(struct uart_port *port)
1332} 1320}
1333 1321
1334/* 1322/*
1335 * Clear the Tegra rx fifo after a break
1336 *
1337 * FIXME: This needs to become a port specific callback once we have a
1338 * framework for this
1339 */
1340static void clear_rx_fifo(struct uart_8250_port *up)
1341{
1342 unsigned int status, tmout = 10000;
1343 do {
1344 status = serial_in(up, UART_LSR);
1345 if (status & (UART_LSR_FIFOE | UART_LSR_BRK_ERROR_BITS))
1346 status = serial_in(up, UART_RX);
1347 else
1348 break;
1349 if (--tmout == 0)
1350 break;
1351 udelay(1);
1352 } while (1);
1353}
1354
1355/*
1356 * serial8250_rx_chars: processes according to the passed in LSR 1323 * serial8250_rx_chars: processes according to the passed in LSR
1357 * value, and returns the remaining LSR bits not handled 1324 * value, and returns the remaining LSR bits not handled
1358 * by this Rx routine. 1325 * by this Rx routine.
@@ -1386,20 +1353,10 @@ serial8250_rx_chars(struct uart_8250_port *up, unsigned char lsr)
1386 up->lsr_saved_flags = 0; 1353 up->lsr_saved_flags = 0;
1387 1354
1388 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) { 1355 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
1389 /*
1390 * For statistics only
1391 */
1392 if (lsr & UART_LSR_BI) { 1356 if (lsr & UART_LSR_BI) {
1393 lsr &= ~(UART_LSR_FE | UART_LSR_PE); 1357 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1394 port->icount.brk++; 1358 port->icount.brk++;
1395 /* 1359 /*
1396 * If tegra port then clear the rx fifo to
1397 * accept another break/character.
1398 */
1399 if (port->type == PORT_TEGRA)
1400 clear_rx_fifo(up);
1401
1402 /*
1403 * We do the SysRQ and SAK checking 1360 * We do the SysRQ and SAK checking
1404 * here because otherwise the break 1361 * here because otherwise the break
1405 * may get masked by ignore_status_mask 1362 * may get masked by ignore_status_mask
@@ -2280,10 +2237,11 @@ serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
2280 quot++; 2237 quot++;
2281 2238
2282 if (up->capabilities & UART_CAP_FIFO && port->fifosize > 1) { 2239 if (up->capabilities & UART_CAP_FIFO && port->fifosize > 1) {
2283 if (baud < 2400) 2240 fcr = uart_config[port->type].fcr;
2284 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1; 2241 if (baud < 2400) {
2285 else 2242 fcr &= ~UART_FCR_TRIGGER_MASK;
2286 fcr = uart_config[port->type].fcr; 2243 fcr |= UART_FCR_TRIGGER_1;
2244 }
2287 } 2245 }
2288 2246
2289 /* 2247 /*
@@ -3037,6 +2995,7 @@ static int __devinit serial8250_probe(struct platform_device *dev)
3037 port.serial_in = p->serial_in; 2995 port.serial_in = p->serial_in;
3038 port.serial_out = p->serial_out; 2996 port.serial_out = p->serial_out;
3039 port.handle_irq = p->handle_irq; 2997 port.handle_irq = p->handle_irq;
2998 port.handle_break = p->handle_break;
3040 port.set_termios = p->set_termios; 2999 port.set_termios = p->set_termios;
3041 port.pm = p->pm; 3000 port.pm = p->pm;
3042 port.dev = &dev->dev; 3001 port.dev = &dev->dev;
@@ -3153,7 +3112,7 @@ static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *
3153} 3112}
3154 3113
3155/** 3114/**
3156 * serial8250_register_port - register a serial port 3115 * serial8250_register_8250_port - register a serial port
3157 * @port: serial port template 3116 * @port: serial port template
3158 * 3117 *
3159 * Configure the serial port specified by the request. If the 3118 * Configure the serial port specified by the request. If the
@@ -3165,50 +3124,56 @@ static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *
3165 * 3124 *
3166 * On success the port is ready to use and the line number is returned. 3125 * On success the port is ready to use and the line number is returned.
3167 */ 3126 */
3168int serial8250_register_port(struct uart_port *port) 3127int serial8250_register_8250_port(struct uart_8250_port *up)
3169{ 3128{
3170 struct uart_8250_port *uart; 3129 struct uart_8250_port *uart;
3171 int ret = -ENOSPC; 3130 int ret = -ENOSPC;
3172 3131
3173 if (port->uartclk == 0) 3132 if (up->port.uartclk == 0)
3174 return -EINVAL; 3133 return -EINVAL;
3175 3134
3176 mutex_lock(&serial_mutex); 3135 mutex_lock(&serial_mutex);
3177 3136
3178 uart = serial8250_find_match_or_unused(port); 3137 uart = serial8250_find_match_or_unused(&up->port);
3179 if (uart) { 3138 if (uart) {
3180 uart_remove_one_port(&serial8250_reg, &uart->port); 3139 uart_remove_one_port(&serial8250_reg, &uart->port);
3181 3140
3182 uart->port.iobase = port->iobase; 3141 uart->port.iobase = up->port.iobase;
3183 uart->port.membase = port->membase; 3142 uart->port.membase = up->port.membase;
3184 uart->port.irq = port->irq; 3143 uart->port.irq = up->port.irq;
3185 uart->port.irqflags = port->irqflags; 3144 uart->port.irqflags = up->port.irqflags;
3186 uart->port.uartclk = port->uartclk; 3145 uart->port.uartclk = up->port.uartclk;
3187 uart->port.fifosize = port->fifosize; 3146 uart->port.fifosize = up->port.fifosize;
3188 uart->port.regshift = port->regshift; 3147 uart->port.regshift = up->port.regshift;
3189 uart->port.iotype = port->iotype; 3148 uart->port.iotype = up->port.iotype;
3190 uart->port.flags = port->flags | UPF_BOOT_AUTOCONF; 3149 uart->port.flags = up->port.flags | UPF_BOOT_AUTOCONF;
3191 uart->port.mapbase = port->mapbase; 3150 uart->port.mapbase = up->port.mapbase;
3192 uart->port.private_data = port->private_data; 3151 uart->port.private_data = up->port.private_data;
3193 if (port->dev) 3152 if (up->port.dev)
3194 uart->port.dev = port->dev; 3153 uart->port.dev = up->port.dev;
3195 3154
3196 if (port->flags & UPF_FIXED_TYPE) 3155 if (up->port.flags & UPF_FIXED_TYPE)
3197 serial8250_init_fixed_type_port(uart, port->type); 3156 serial8250_init_fixed_type_port(uart, up->port.type);
3198 3157
3199 set_io_from_upio(&uart->port); 3158 set_io_from_upio(&uart->port);
3200 /* Possibly override default I/O functions. */ 3159 /* Possibly override default I/O functions. */
3201 if (port->serial_in) 3160 if (up->port.serial_in)
3202 uart->port.serial_in = port->serial_in; 3161 uart->port.serial_in = up->port.serial_in;
3203 if (port->serial_out) 3162 if (up->port.serial_out)
3204 uart->port.serial_out = port->serial_out; 3163 uart->port.serial_out = up->port.serial_out;
3205 if (port->handle_irq) 3164 if (up->port.handle_irq)
3206 uart->port.handle_irq = port->handle_irq; 3165 uart->port.handle_irq = up->port.handle_irq;
3207 /* Possibly override set_termios call */ 3166 /* Possibly override set_termios call */
3208 if (port->set_termios) 3167 if (up->port.set_termios)
3209 uart->port.set_termios = port->set_termios; 3168 uart->port.set_termios = up->port.set_termios;
3210 if (port->pm) 3169 if (up->port.pm)
3211 uart->port.pm = port->pm; 3170 uart->port.pm = up->port.pm;
3171 if (up->port.handle_break)
3172 uart->port.handle_break = up->port.handle_break;
3173 if (up->dl_read)
3174 uart->dl_read = up->dl_read;
3175 if (up->dl_write)
3176 uart->dl_write = up->dl_write;
3212 3177
3213 if (serial8250_isa_config != NULL) 3178 if (serial8250_isa_config != NULL)
3214 serial8250_isa_config(0, &uart->port, 3179 serial8250_isa_config(0, &uart->port,
@@ -3222,6 +3187,29 @@ int serial8250_register_port(struct uart_port *port)
3222 3187
3223 return ret; 3188 return ret;
3224} 3189}
3190EXPORT_SYMBOL(serial8250_register_8250_port);
3191
3192/**
3193 * serial8250_register_port - register a serial port
3194 * @port: serial port template
3195 *
3196 * Configure the serial port specified by the request. If the
3197 * port exists and is in use, it is hung up and unregistered
3198 * first.
3199 *
3200 * The port is then probed and if necessary the IRQ is autodetected
3201 * If this fails an error is returned.
3202 *
3203 * On success the port is ready to use and the line number is returned.
3204 */
3205int serial8250_register_port(struct uart_port *port)
3206{
3207 struct uart_8250_port up;
3208
3209 memset(&up, 0, sizeof(up));
3210 memcpy(&up.port, port, sizeof(*port));
3211 return serial8250_register_8250_port(&up);
3212}
3225EXPORT_SYMBOL(serial8250_register_port); 3213EXPORT_SYMBOL(serial8250_register_port);
3226 3214
3227/** 3215/**
diff --git a/drivers/tty/serial/8250/8250.h b/drivers/tty/serial/8250/8250.h
index 2868a1da254d..f9719d167c8d 100644
--- a/drivers/tty/serial/8250/8250.h
+++ b/drivers/tty/serial/8250/8250.h
@@ -37,6 +37,10 @@ struct uart_8250_port {
37 unsigned char lsr_saved_flags; 37 unsigned char lsr_saved_flags;
38#define MSR_SAVE_FLAGS UART_MSR_ANY_DELTA 38#define MSR_SAVE_FLAGS UART_MSR_ANY_DELTA
39 unsigned char msr_saved_flags; 39 unsigned char msr_saved_flags;
40
41 /* 8250 specific callbacks */
42 int (*dl_read)(struct uart_8250_port *);
43 void (*dl_write)(struct uart_8250_port *, int);
40}; 44};
41 45
42struct old_serial_port { 46struct old_serial_port {
@@ -96,6 +100,18 @@ static inline void serial_out(struct uart_8250_port *up, int offset, int value)
96 up->port.serial_out(&up->port, offset, value); 100 up->port.serial_out(&up->port, offset, value);
97} 101}
98 102
103void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p);
104
105static inline int serial_dl_read(struct uart_8250_port *up)
106{
107 return up->dl_read(up);
108}
109
110static inline void serial_dl_write(struct uart_8250_port *up, int value)
111{
112 up->dl_write(up, value);
113}
114
99#if defined(__alpha__) && !defined(CONFIG_PCI) 115#if defined(__alpha__) && !defined(CONFIG_PCI)
100/* 116/*
101 * Digital did something really horribly wrong with the OUT1 and OUT2 117 * Digital did something really horribly wrong with the OUT1 and OUT2
diff --git a/drivers/tty/serial/8250/8250_em.c b/drivers/tty/serial/8250/8250_em.c
new file mode 100644
index 000000000000..3a0363e7f3a7
--- /dev/null
+++ b/drivers/tty/serial/8250/8250_em.c
@@ -0,0 +1,186 @@
1/*
2 * Renesas Emma Mobile 8250 driver
3 *
4 * Copyright (C) 2012 Magnus Damm
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/device.h>
21#include <linux/init.h>
22#include <linux/io.h>
23#include <linux/module.h>
24#include <linux/serial_8250.h>
25#include <linux/serial_core.h>
26#include <linux/serial_reg.h>
27#include <linux/platform_device.h>
28#include <linux/clk.h>
29#include <linux/slab.h>
30
31#include "8250.h"
32
33#define UART_DLL_EM 9
34#define UART_DLM_EM 10
35
36struct serial8250_em_priv {
37 struct clk *sclk;
38 int line;
39};
40
41static void serial8250_em_serial_out(struct uart_port *p, int offset, int value)
42{
43 switch (offset) {
44 case UART_TX: /* TX @ 0x00 */
45 writeb(value, p->membase);
46 break;
47 case UART_FCR: /* FCR @ 0x0c (+1) */
48 case UART_LCR: /* LCR @ 0x10 (+1) */
49 case UART_MCR: /* MCR @ 0x14 (+1) */
50 case UART_SCR: /* SCR @ 0x20 (+1) */
51 writel(value, p->membase + ((offset + 1) << 2));
52 break;
53 case UART_IER: /* IER @ 0x04 */
54 value &= 0x0f; /* only 4 valid bits - not Xscale */
55 /* fall-through */
56 case UART_DLL_EM: /* DLL @ 0x24 (+9) */
57 case UART_DLM_EM: /* DLM @ 0x28 (+9) */
58 writel(value, p->membase + (offset << 2));
59 }
60}
61
62static unsigned int serial8250_em_serial_in(struct uart_port *p, int offset)
63{
64 switch (offset) {
65 case UART_RX: /* RX @ 0x00 */
66 return readb(p->membase);
67 case UART_MCR: /* MCR @ 0x14 (+1) */
68 case UART_LSR: /* LSR @ 0x18 (+1) */
69 case UART_MSR: /* MSR @ 0x1c (+1) */
70 case UART_SCR: /* SCR @ 0x20 (+1) */
71 return readl(p->membase + ((offset + 1) << 2));
72 case UART_IER: /* IER @ 0x04 */
73 case UART_IIR: /* IIR @ 0x08 */
74 case UART_DLL_EM: /* DLL @ 0x24 (+9) */
75 case UART_DLM_EM: /* DLM @ 0x28 (+9) */
76 return readl(p->membase + (offset << 2));
77 }
78 return 0;
79}
80
81static int serial8250_em_serial_dl_read(struct uart_8250_port *up)
82{
83 return serial_in(up, UART_DLL_EM) | serial_in(up, UART_DLM_EM) << 8;
84}
85
86static void serial8250_em_serial_dl_write(struct uart_8250_port *up, int value)
87{
88 serial_out(up, UART_DLL_EM, value & 0xff);
89 serial_out(up, UART_DLM_EM, value >> 8 & 0xff);
90}
91
92static int __devinit serial8250_em_probe(struct platform_device *pdev)
93{
94 struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
95 struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
96 struct serial8250_em_priv *priv;
97 struct uart_8250_port up;
98 int ret = -EINVAL;
99
100 if (!regs || !irq) {
101 dev_err(&pdev->dev, "missing registers or irq\n");
102 goto err0;
103 }
104
105 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
106 if (!priv) {
107 dev_err(&pdev->dev, "unable to allocate private data\n");
108 ret = -ENOMEM;
109 goto err0;
110 }
111
112 priv->sclk = clk_get(&pdev->dev, "sclk");
113 if (IS_ERR(priv->sclk)) {
114 dev_err(&pdev->dev, "unable to get clock\n");
115 ret = PTR_ERR(priv->sclk);
116 goto err1;
117 }
118
119 memset(&up, 0, sizeof(up));
120 up.port.mapbase = regs->start;
121 up.port.irq = irq->start;
122 up.port.type = PORT_UNKNOWN;
123 up.port.flags = UPF_BOOT_AUTOCONF | UPF_FIXED_PORT | UPF_IOREMAP;
124 up.port.dev = &pdev->dev;
125 up.port.private_data = priv;
126
127 clk_enable(priv->sclk);
128 up.port.uartclk = clk_get_rate(priv->sclk);
129
130 up.port.iotype = UPIO_MEM32;
131 up.port.serial_in = serial8250_em_serial_in;
132 up.port.serial_out = serial8250_em_serial_out;
133 up.dl_read = serial8250_em_serial_dl_read;
134 up.dl_write = serial8250_em_serial_dl_write;
135
136 ret = serial8250_register_8250_port(&up);
137 if (ret < 0) {
138 dev_err(&pdev->dev, "unable to register 8250 port\n");
139 goto err2;
140 }
141
142 priv->line = ret;
143 platform_set_drvdata(pdev, priv);
144 return 0;
145
146 err2:
147 clk_disable(priv->sclk);
148 clk_put(priv->sclk);
149 err1:
150 kfree(priv);
151 err0:
152 return ret;
153}
154
155static int __devexit serial8250_em_remove(struct platform_device *pdev)
156{
157 struct serial8250_em_priv *priv = platform_get_drvdata(pdev);
158
159 serial8250_unregister_port(priv->line);
160 clk_disable(priv->sclk);
161 clk_put(priv->sclk);
162 kfree(priv);
163 return 0;
164}
165
166static const struct of_device_id serial8250_em_dt_ids[] __devinitconst = {
167 { .compatible = "renesas,em-uart", },
168 {},
169};
170MODULE_DEVICE_TABLE(of, serial8250_em_dt_ids);
171
172static struct platform_driver serial8250_em_platform_driver = {
173 .driver = {
174 .name = "serial8250-em",
175 .of_match_table = serial8250_em_dt_ids,
176 .owner = THIS_MODULE,
177 },
178 .probe = serial8250_em_probe,
179 .remove = __devexit_p(serial8250_em_remove),
180};
181
182module_platform_driver(serial8250_em_platform_driver);
183
184MODULE_AUTHOR("Magnus Damm");
185MODULE_DESCRIPTION("Renesas Emma Mobile 8250 Driver");
186MODULE_LICENSE("GPL v2");
diff --git a/drivers/tty/serial/8250/8250_pci.c b/drivers/tty/serial/8250/8250_pci.c
index 858dca865d6a..28e7c7cce893 100644
--- a/drivers/tty/serial/8250/8250_pci.c
+++ b/drivers/tty/serial/8250/8250_pci.c
@@ -17,6 +17,7 @@
17#include <linux/slab.h> 17#include <linux/slab.h>
18#include <linux/delay.h> 18#include <linux/delay.h>
19#include <linux/tty.h> 19#include <linux/tty.h>
20#include <linux/serial_reg.h>
20#include <linux/serial_core.h> 21#include <linux/serial_core.h>
21#include <linux/8250_pci.h> 22#include <linux/8250_pci.h>
22#include <linux/bitops.h> 23#include <linux/bitops.h>
@@ -1092,11 +1093,49 @@ static int skip_tx_en_setup(struct serial_private *priv,
1092 return pci_default_setup(priv, board, port, idx); 1093 return pci_default_setup(priv, board, port, idx);
1093} 1094}
1094 1095
1096static void kt_handle_break(struct uart_port *p)
1097{
1098 struct uart_8250_port *up =
1099 container_of(p, struct uart_8250_port, port);
1100 /*
1101 * On receipt of a BI, serial device in Intel ME (Intel
1102 * management engine) needs to have its fifos cleared for sane
1103 * SOL (Serial Over Lan) output.
1104 */
1105 serial8250_clear_and_reinit_fifos(up);
1106}
1107
1108static unsigned int kt_serial_in(struct uart_port *p, int offset)
1109{
1110 struct uart_8250_port *up =
1111 container_of(p, struct uart_8250_port, port);
1112 unsigned int val;
1113
1114 /*
1115 * When the Intel ME (management engine) gets reset its serial
1116 * port registers could return 0 momentarily. Functions like
1117 * serial8250_console_write, read and save the IER, perform
1118 * some operation and then restore it. In order to avoid
1119 * setting IER register inadvertently to 0, if the value read
1120 * is 0, double check with ier value in uart_8250_port and use
1121 * that instead. up->ier should be the same value as what is
1122 * currently configured.
1123 */
1124 val = inb(p->iobase + offset);
1125 if (offset == UART_IER) {
1126 if (val == 0)
1127 val = up->ier;
1128 }
1129 return val;
1130}
1131
1095static int kt_serial_setup(struct serial_private *priv, 1132static int kt_serial_setup(struct serial_private *priv,
1096 const struct pciserial_board *board, 1133 const struct pciserial_board *board,
1097 struct uart_port *port, int idx) 1134 struct uart_port *port, int idx)
1098{ 1135{
1099 port->flags |= UPF_BUG_THRE; 1136 port->flags |= UPF_BUG_THRE;
1137 port->serial_in = kt_serial_in;
1138 port->handle_break = kt_handle_break;
1100 return skip_tx_en_setup(priv, board, port, idx); 1139 return skip_tx_en_setup(priv, board, port, idx);
1101} 1140}
1102 1141
@@ -1609,54 +1648,72 @@ static struct pci_serial_quirk pci_serial_quirks[] __refdata = {
1609 { 1648 {
1610 .vendor = PCI_VENDOR_ID_INTEL, 1649 .vendor = PCI_VENDOR_ID_INTEL,
1611 .device = 0x8811, 1650 .device = 0x8811,
1651 .subvendor = PCI_ANY_ID,
1652 .subdevice = PCI_ANY_ID,
1612 .init = pci_eg20t_init, 1653 .init = pci_eg20t_init,
1613 .setup = pci_default_setup, 1654 .setup = pci_default_setup,
1614 }, 1655 },
1615 { 1656 {
1616 .vendor = PCI_VENDOR_ID_INTEL, 1657 .vendor = PCI_VENDOR_ID_INTEL,
1617 .device = 0x8812, 1658 .device = 0x8812,
1659 .subvendor = PCI_ANY_ID,
1660 .subdevice = PCI_ANY_ID,
1618 .init = pci_eg20t_init, 1661 .init = pci_eg20t_init,
1619 .setup = pci_default_setup, 1662 .setup = pci_default_setup,
1620 }, 1663 },
1621 { 1664 {
1622 .vendor = PCI_VENDOR_ID_INTEL, 1665 .vendor = PCI_VENDOR_ID_INTEL,
1623 .device = 0x8813, 1666 .device = 0x8813,
1667 .subvendor = PCI_ANY_ID,
1668 .subdevice = PCI_ANY_ID,
1624 .init = pci_eg20t_init, 1669 .init = pci_eg20t_init,
1625 .setup = pci_default_setup, 1670 .setup = pci_default_setup,
1626 }, 1671 },
1627 { 1672 {
1628 .vendor = PCI_VENDOR_ID_INTEL, 1673 .vendor = PCI_VENDOR_ID_INTEL,
1629 .device = 0x8814, 1674 .device = 0x8814,
1675 .subvendor = PCI_ANY_ID,
1676 .subdevice = PCI_ANY_ID,
1630 .init = pci_eg20t_init, 1677 .init = pci_eg20t_init,
1631 .setup = pci_default_setup, 1678 .setup = pci_default_setup,
1632 }, 1679 },
1633 { 1680 {
1634 .vendor = 0x10DB, 1681 .vendor = 0x10DB,
1635 .device = 0x8027, 1682 .device = 0x8027,
1683 .subvendor = PCI_ANY_ID,
1684 .subdevice = PCI_ANY_ID,
1636 .init = pci_eg20t_init, 1685 .init = pci_eg20t_init,
1637 .setup = pci_default_setup, 1686 .setup = pci_default_setup,
1638 }, 1687 },
1639 { 1688 {
1640 .vendor = 0x10DB, 1689 .vendor = 0x10DB,
1641 .device = 0x8028, 1690 .device = 0x8028,
1691 .subvendor = PCI_ANY_ID,
1692 .subdevice = PCI_ANY_ID,
1642 .init = pci_eg20t_init, 1693 .init = pci_eg20t_init,
1643 .setup = pci_default_setup, 1694 .setup = pci_default_setup,
1644 }, 1695 },
1645 { 1696 {
1646 .vendor = 0x10DB, 1697 .vendor = 0x10DB,
1647 .device = 0x8029, 1698 .device = 0x8029,
1699 .subvendor = PCI_ANY_ID,
1700 .subdevice = PCI_ANY_ID,
1648 .init = pci_eg20t_init, 1701 .init = pci_eg20t_init,
1649 .setup = pci_default_setup, 1702 .setup = pci_default_setup,
1650 }, 1703 },
1651 { 1704 {
1652 .vendor = 0x10DB, 1705 .vendor = 0x10DB,
1653 .device = 0x800C, 1706 .device = 0x800C,
1707 .subvendor = PCI_ANY_ID,
1708 .subdevice = PCI_ANY_ID,
1654 .init = pci_eg20t_init, 1709 .init = pci_eg20t_init,
1655 .setup = pci_default_setup, 1710 .setup = pci_default_setup,
1656 }, 1711 },
1657 { 1712 {
1658 .vendor = 0x10DB, 1713 .vendor = 0x10DB,
1659 .device = 0x800D, 1714 .device = 0x800D,
1715 .subvendor = PCI_ANY_ID,
1716 .subdevice = PCI_ANY_ID,
1660 .init = pci_eg20t_init, 1717 .init = pci_eg20t_init,
1661 .setup = pci_default_setup, 1718 .setup = pci_default_setup,
1662 }, 1719 },
@@ -2775,6 +2832,12 @@ void pciserial_suspend_ports(struct serial_private *priv)
2775 for (i = 0; i < priv->nr; i++) 2832 for (i = 0; i < priv->nr; i++)
2776 if (priv->line[i] >= 0) 2833 if (priv->line[i] >= 0)
2777 serial8250_suspend_port(priv->line[i]); 2834 serial8250_suspend_port(priv->line[i]);
2835
2836 /*
2837 * Ensure that every init quirk is properly torn down
2838 */
2839 if (priv->quirk->exit)
2840 priv->quirk->exit(priv->dev);
2778} 2841}
2779EXPORT_SYMBOL_GPL(pciserial_suspend_ports); 2842EXPORT_SYMBOL_GPL(pciserial_suspend_ports);
2780 2843
diff --git a/drivers/tty/serial/8250/Kconfig b/drivers/tty/serial/8250/Kconfig
index 591f8018e7dd..8bc7ecbf6bea 100644
--- a/drivers/tty/serial/8250/Kconfig
+++ b/drivers/tty/serial/8250/Kconfig
@@ -278,3 +278,11 @@ config SERIAL_8250_DW
278 help 278 help
279 Selecting this option will enable handling of the extra features 279 Selecting this option will enable handling of the extra features
280 present in the Synopsys DesignWare APB UART. 280 present in the Synopsys DesignWare APB UART.
281
282config SERIAL_8250_EM
283 tristate "Support for Emma Mobile intergrated serial port"
284 depends on SERIAL_8250 && ARM && HAVE_CLK
285 help
286 Selecting this option will add support for the integrated serial
287 port hardware found on the Emma Mobile line of processors.
288 If unsure, say N.
diff --git a/drivers/tty/serial/8250/Makefile b/drivers/tty/serial/8250/Makefile
index 867bba738908..3f35eacdf673 100644
--- a/drivers/tty/serial/8250/Makefile
+++ b/drivers/tty/serial/8250/Makefile
@@ -18,3 +18,4 @@ obj-$(CONFIG_SERIAL_8250_HUB6) += 8250_hub6.o
18obj-$(CONFIG_SERIAL_8250_MCA) += 8250_mca.o 18obj-$(CONFIG_SERIAL_8250_MCA) += 8250_mca.o
19obj-$(CONFIG_SERIAL_8250_FSL) += 8250_fsl.o 19obj-$(CONFIG_SERIAL_8250_FSL) += 8250_fsl.o
20obj-$(CONFIG_SERIAL_8250_DW) += 8250_dw.o 20obj-$(CONFIG_SERIAL_8250_DW) += 8250_dw.o
21obj-$(CONFIG_SERIAL_8250_EM) += 8250_em.o
diff --git a/drivers/tty/serial/amba-pl011.c b/drivers/tty/serial/amba-pl011.c
index 062ef8c2b3cb..4ad721fb8405 100644
--- a/drivers/tty/serial/amba-pl011.c
+++ b/drivers/tty/serial/amba-pl011.c
@@ -68,30 +68,6 @@
68#define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE) 68#define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
69#define UART_DUMMY_DR_RX (1 << 16) 69#define UART_DUMMY_DR_RX (1 << 16)
70 70
71
72#define UART_WA_SAVE_NR 14
73
74static void pl011_lockup_wa(unsigned long data);
75static const u32 uart_wa_reg[UART_WA_SAVE_NR] = {
76 ST_UART011_DMAWM,
77 ST_UART011_TIMEOUT,
78 ST_UART011_LCRH_RX,
79 UART011_IBRD,
80 UART011_FBRD,
81 ST_UART011_LCRH_TX,
82 UART011_IFLS,
83 ST_UART011_XFCR,
84 ST_UART011_XON1,
85 ST_UART011_XON2,
86 ST_UART011_XOFF1,
87 ST_UART011_XOFF2,
88 UART011_CR,
89 UART011_IMSC
90};
91
92static u32 uart_wa_regdata[UART_WA_SAVE_NR];
93static DECLARE_TASKLET(pl011_lockup_tlet, pl011_lockup_wa, 0);
94
95/* There is by now at least one vendor with differing details, so handle it */ 71/* There is by now at least one vendor with differing details, so handle it */
96struct vendor_data { 72struct vendor_data {
97 unsigned int ifls; 73 unsigned int ifls;
@@ -101,6 +77,7 @@ struct vendor_data {
101 bool oversampling; 77 bool oversampling;
102 bool interrupt_may_hang; /* vendor-specific */ 78 bool interrupt_may_hang; /* vendor-specific */
103 bool dma_threshold; 79 bool dma_threshold;
80 bool cts_event_workaround;
104}; 81};
105 82
106static struct vendor_data vendor_arm = { 83static struct vendor_data vendor_arm = {
@@ -110,6 +87,7 @@ static struct vendor_data vendor_arm = {
110 .lcrh_rx = UART011_LCRH, 87 .lcrh_rx = UART011_LCRH,
111 .oversampling = false, 88 .oversampling = false,
112 .dma_threshold = false, 89 .dma_threshold = false,
90 .cts_event_workaround = false,
113}; 91};
114 92
115static struct vendor_data vendor_st = { 93static struct vendor_data vendor_st = {
@@ -120,6 +98,7 @@ static struct vendor_data vendor_st = {
120 .oversampling = true, 98 .oversampling = true,
121 .interrupt_may_hang = true, 99 .interrupt_may_hang = true,
122 .dma_threshold = true, 100 .dma_threshold = true,
101 .cts_event_workaround = true,
123}; 102};
124 103
125static struct uart_amba_port *amba_ports[UART_NR]; 104static struct uart_amba_port *amba_ports[UART_NR];
@@ -1055,69 +1034,6 @@ static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1055#define pl011_dma_flush_buffer NULL 1034#define pl011_dma_flush_buffer NULL
1056#endif 1035#endif
1057 1036
1058
1059/*
1060 * pl011_lockup_wa
1061 * This workaround aims to break the deadlock situation
1062 * when after long transfer over uart in hardware flow
1063 * control, uart interrupt registers cannot be cleared.
1064 * Hence uart transfer gets blocked.
1065 *
1066 * It is seen that during such deadlock condition ICR
1067 * don't get cleared even on multiple write. This leads
1068 * pass_counter to decrease and finally reach zero. This
1069 * can be taken as trigger point to run this UART_BT_WA.
1070 *
1071 */
1072static void pl011_lockup_wa(unsigned long data)
1073{
1074 struct uart_amba_port *uap = amba_ports[0];
1075 void __iomem *base = uap->port.membase;
1076 struct circ_buf *xmit = &uap->port.state->xmit;
1077 struct tty_struct *tty = uap->port.state->port.tty;
1078 int buf_empty_retries = 200;
1079 int loop;
1080
1081 /* Stop HCI layer from submitting data for tx */
1082 tty->hw_stopped = 1;
1083 while (!uart_circ_empty(xmit)) {
1084 if (buf_empty_retries-- == 0)
1085 break;
1086 udelay(100);
1087 }
1088
1089 /* Backup registers */
1090 for (loop = 0; loop < UART_WA_SAVE_NR; loop++)
1091 uart_wa_regdata[loop] = readl(base + uart_wa_reg[loop]);
1092
1093 /* Disable UART so that FIFO data is flushed out */
1094 writew(0x00, uap->port.membase + UART011_CR);
1095
1096 /* Soft reset UART module */
1097 if (uap->port.dev->platform_data) {
1098 struct amba_pl011_data *plat;
1099
1100 plat = uap->port.dev->platform_data;
1101 if (plat->reset)
1102 plat->reset();
1103 }
1104
1105 /* Restore registers */
1106 for (loop = 0; loop < UART_WA_SAVE_NR; loop++)
1107 writew(uart_wa_regdata[loop] ,
1108 uap->port.membase + uart_wa_reg[loop]);
1109
1110 /* Initialise the old status of the modem signals */
1111 uap->old_status = readw(uap->port.membase + UART01x_FR) &
1112 UART01x_FR_MODEM_ANY;
1113
1114 if (readl(base + UART011_MIS) & 0x2)
1115 printk(KERN_EMERG "UART_BT_WA: ***FAILED***\n");
1116
1117 /* Start Tx/Rx */
1118 tty->hw_stopped = 0;
1119}
1120
1121static void pl011_stop_tx(struct uart_port *port) 1037static void pl011_stop_tx(struct uart_port *port)
1122{ 1038{
1123 struct uart_amba_port *uap = (struct uart_amba_port *)port; 1039 struct uart_amba_port *uap = (struct uart_amba_port *)port;
@@ -1246,12 +1162,26 @@ static irqreturn_t pl011_int(int irq, void *dev_id)
1246 unsigned long flags; 1162 unsigned long flags;
1247 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT; 1163 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
1248 int handled = 0; 1164 int handled = 0;
1165 unsigned int dummy_read;
1249 1166
1250 spin_lock_irqsave(&uap->port.lock, flags); 1167 spin_lock_irqsave(&uap->port.lock, flags);
1251 1168
1252 status = readw(uap->port.membase + UART011_MIS); 1169 status = readw(uap->port.membase + UART011_MIS);
1253 if (status) { 1170 if (status) {
1254 do { 1171 do {
1172 if (uap->vendor->cts_event_workaround) {
1173 /* workaround to make sure that all bits are unlocked.. */
1174 writew(0x00, uap->port.membase + UART011_ICR);
1175
1176 /*
1177 * WA: introduce 26ns(1 uart clk) delay before W1C;
1178 * single apb access will incur 2 pclk(133.12Mhz) delay,
1179 * so add 2 dummy reads
1180 */
1181 dummy_read = readw(uap->port.membase + UART011_ICR);
1182 dummy_read = readw(uap->port.membase + UART011_ICR);
1183 }
1184
1255 writew(status & ~(UART011_TXIS|UART011_RTIS| 1185 writew(status & ~(UART011_TXIS|UART011_RTIS|
1256 UART011_RXIS), 1186 UART011_RXIS),
1257 uap->port.membase + UART011_ICR); 1187 uap->port.membase + UART011_ICR);
@@ -1268,11 +1198,8 @@ static irqreturn_t pl011_int(int irq, void *dev_id)
1268 if (status & UART011_TXIS) 1198 if (status & UART011_TXIS)
1269 pl011_tx_chars(uap); 1199 pl011_tx_chars(uap);
1270 1200
1271 if (pass_counter-- == 0) { 1201 if (pass_counter-- == 0)
1272 if (uap->interrupt_may_hang)
1273 tasklet_schedule(&pl011_lockup_tlet);
1274 break; 1202 break;
1275 }
1276 1203
1277 status = readw(uap->port.membase + UART011_MIS); 1204 status = readw(uap->port.membase + UART011_MIS);
1278 } while (status != 0); 1205 } while (status != 0);
diff --git a/drivers/tty/serial/bfin_uart.c b/drivers/tty/serial/bfin_uart.c
index 5832fdef11e9..bd97db23985b 100644
--- a/drivers/tty/serial/bfin_uart.c
+++ b/drivers/tty/serial/bfin_uart.c
@@ -1,7 +1,7 @@
1/* 1/*
2 * Blackfin On-Chip Serial Driver 2 * Blackfin On-Chip Serial Driver
3 * 3 *
4 * Copyright 2006-2010 Analog Devices Inc. 4 * Copyright 2006-2011 Analog Devices Inc.
5 * 5 *
6 * Enter bugs at http://blackfin.uclinux.org/ 6 * Enter bugs at http://blackfin.uclinux.org/
7 * 7 *
@@ -35,10 +35,6 @@
35#include <asm/portmux.h> 35#include <asm/portmux.h>
36#include <asm/cacheflush.h> 36#include <asm/cacheflush.h>
37#include <asm/dma.h> 37#include <asm/dma.h>
38
39#define port_membase(uart) (((struct bfin_serial_port *)(uart))->port.membase)
40#define get_lsr_cache(uart) (((struct bfin_serial_port *)(uart))->lsr)
41#define put_lsr_cache(uart, v) (((struct bfin_serial_port *)(uart))->lsr = (v))
42#include <asm/bfin_serial.h> 38#include <asm/bfin_serial.h>
43 39
44#ifdef CONFIG_SERIAL_BFIN_MODULE 40#ifdef CONFIG_SERIAL_BFIN_MODULE
@@ -166,7 +162,7 @@ static void bfin_serial_stop_tx(struct uart_port *port)
166 uart->tx_count = 0; 162 uart->tx_count = 0;
167 uart->tx_done = 1; 163 uart->tx_done = 1;
168#else 164#else
169#ifdef CONFIG_BF54x 165#if defined(CONFIG_BF54x) || defined(CONFIG_BF60x)
170 /* Clear TFI bit */ 166 /* Clear TFI bit */
171 UART_PUT_LSR(uart, TFI); 167 UART_PUT_LSR(uart, TFI);
172#endif 168#endif
@@ -337,7 +333,7 @@ static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
337 struct circ_buf *xmit = &uart->port.state->xmit; 333 struct circ_buf *xmit = &uart->port.state->xmit;
338 334
339 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) { 335 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
340#ifdef CONFIG_BF54x 336#if defined(CONFIG_BF54x) || defined(CONFIG_BF60x)
341 /* Clear TFI bit */ 337 /* Clear TFI bit */
342 UART_PUT_LSR(uart, TFI); 338 UART_PUT_LSR(uart, TFI);
343#endif 339#endif
@@ -536,7 +532,7 @@ static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
536 */ 532 */
537 UART_CLEAR_IER(uart, ETBEI); 533 UART_CLEAR_IER(uart, ETBEI);
538 uart->port.icount.tx += uart->tx_count; 534 uart->port.icount.tx += uart->tx_count;
539 if (!uart_circ_empty(xmit)) { 535 if (!(xmit->tail == 0 && xmit->head == 0)) {
540 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1); 536 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
541 537
542 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 538 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
@@ -553,7 +549,7 @@ static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
553static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id) 549static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
554{ 550{
555 struct bfin_serial_port *uart = dev_id; 551 struct bfin_serial_port *uart = dev_id;
556 unsigned short irqstat; 552 unsigned int irqstat;
557 int x_pos, pos; 553 int x_pos, pos;
558 554
559 spin_lock(&uart->rx_lock); 555 spin_lock(&uart->rx_lock);
@@ -586,7 +582,7 @@ static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
586static unsigned int bfin_serial_tx_empty(struct uart_port *port) 582static unsigned int bfin_serial_tx_empty(struct uart_port *port)
587{ 583{
588 struct bfin_serial_port *uart = (struct bfin_serial_port *)port; 584 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
589 unsigned short lsr; 585 unsigned int lsr;
590 586
591 lsr = UART_GET_LSR(uart); 587 lsr = UART_GET_LSR(uart);
592 if (lsr & TEMT) 588 if (lsr & TEMT)
@@ -598,7 +594,7 @@ static unsigned int bfin_serial_tx_empty(struct uart_port *port)
598static void bfin_serial_break_ctl(struct uart_port *port, int break_state) 594static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
599{ 595{
600 struct bfin_serial_port *uart = (struct bfin_serial_port *)port; 596 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
601 u16 lcr = UART_GET_LCR(uart); 597 u32 lcr = UART_GET_LCR(uart);
602 if (break_state) 598 if (break_state)
603 lcr |= SB; 599 lcr |= SB;
604 else 600 else
@@ -745,7 +741,7 @@ static int bfin_serial_startup(struct uart_port *port)
745 } 741 }
746 742
747 /* CTS RTS PINs are negative assertive. */ 743 /* CTS RTS PINs are negative assertive. */
748 UART_PUT_MCR(uart, ACTS); 744 UART_PUT_MCR(uart, UART_GET_MCR(uart) | ACTS);
749 UART_SET_IER(uart, EDSSI); 745 UART_SET_IER(uart, EDSSI);
750 } 746 }
751#endif 747#endif
@@ -803,7 +799,7 @@ bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
803 struct bfin_serial_port *uart = (struct bfin_serial_port *)port; 799 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
804 unsigned long flags; 800 unsigned long flags;
805 unsigned int baud, quot; 801 unsigned int baud, quot;
806 unsigned short val, ier, lcr = 0; 802 unsigned int ier, lcr = 0;
807 803
808 switch (termios->c_cflag & CSIZE) { 804 switch (termios->c_cflag & CSIZE) {
809 case CS8: 805 case CS8:
@@ -875,26 +871,23 @@ bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
875 871
876 /* Disable UART */ 872 /* Disable UART */
877 ier = UART_GET_IER(uart); 873 ier = UART_GET_IER(uart);
874 UART_PUT_GCTL(uart, UART_GET_GCTL(uart) & ~UCEN);
878 UART_DISABLE_INTS(uart); 875 UART_DISABLE_INTS(uart);
879 876
880 /* Set DLAB in LCR to Access DLL and DLH */ 877 /* Set DLAB in LCR to Access CLK */
881 UART_SET_DLAB(uart); 878 UART_SET_DLAB(uart);
882 879
883 UART_PUT_DLL(uart, quot & 0xFF); 880 UART_PUT_CLK(uart, quot);
884 UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
885 SSYNC(); 881 SSYNC();
886 882
887 /* Clear DLAB in LCR to Access THR RBR IER */ 883 /* Clear DLAB in LCR to Access THR RBR IER */
888 UART_CLEAR_DLAB(uart); 884 UART_CLEAR_DLAB(uart);
889 885
890 UART_PUT_LCR(uart, lcr); 886 UART_PUT_LCR(uart, (UART_GET_LCR(uart) & ~LCR_MASK) | lcr);
891 887
892 /* Enable UART */ 888 /* Enable UART */
893 UART_ENABLE_INTS(uart, ier); 889 UART_ENABLE_INTS(uart, ier);
894 890 UART_PUT_GCTL(uart, UART_GET_GCTL(uart) | UCEN);
895 val = UART_GET_GCTL(uart);
896 val |= UCEN;
897 UART_PUT_GCTL(uart, val);
898 891
899 /* Port speed changed, update the per-port timeout. */ 892 /* Port speed changed, update the per-port timeout. */
900 uart_update_timeout(port, termios->c_cflag, baud); 893 uart_update_timeout(port, termios->c_cflag, baud);
@@ -954,17 +947,17 @@ bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
954static void bfin_serial_set_ldisc(struct uart_port *port, int ld) 947static void bfin_serial_set_ldisc(struct uart_port *port, int ld)
955{ 948{
956 struct bfin_serial_port *uart = (struct bfin_serial_port *)port; 949 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
957 unsigned short val; 950 unsigned int val;
958 951
959 switch (ld) { 952 switch (ld) {
960 case N_IRDA: 953 case N_IRDA:
961 val = UART_GET_GCTL(uart); 954 val = UART_GET_GCTL(uart);
962 val |= (IREN | RPOLC); 955 val |= (UMOD_IRDA | RPOLC);
963 UART_PUT_GCTL(uart, val); 956 UART_PUT_GCTL(uart, val);
964 break; 957 break;
965 default: 958 default:
966 val = UART_GET_GCTL(uart); 959 val = UART_GET_GCTL(uart);
967 val &= ~(IREN | RPOLC); 960 val &= ~(UMOD_MASK | RPOLC);
968 UART_PUT_GCTL(uart, val); 961 UART_PUT_GCTL(uart, val);
969 } 962 }
970} 963}
@@ -972,13 +965,13 @@ static void bfin_serial_set_ldisc(struct uart_port *port, int ld)
972static void bfin_serial_reset_irda(struct uart_port *port) 965static void bfin_serial_reset_irda(struct uart_port *port)
973{ 966{
974 struct bfin_serial_port *uart = (struct bfin_serial_port *)port; 967 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
975 unsigned short val; 968 unsigned int val;
976 969
977 val = UART_GET_GCTL(uart); 970 val = UART_GET_GCTL(uart);
978 val &= ~(IREN | RPOLC); 971 val &= ~(UMOD_MASK | RPOLC);
979 UART_PUT_GCTL(uart, val); 972 UART_PUT_GCTL(uart, val);
980 SSYNC(); 973 SSYNC();
981 val |= (IREN | RPOLC); 974 val |= (UMOD_IRDA | RPOLC);
982 UART_PUT_GCTL(uart, val); 975 UART_PUT_GCTL(uart, val);
983 SSYNC(); 976 SSYNC();
984} 977}
@@ -1070,12 +1063,12 @@ static void __init
1070bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud, 1063bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
1071 int *parity, int *bits) 1064 int *parity, int *bits)
1072{ 1065{
1073 unsigned short status; 1066 unsigned int status;
1074 1067
1075 status = UART_GET_IER(uart) & (ERBFI | ETBEI); 1068 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
1076 if (status == (ERBFI | ETBEI)) { 1069 if (status == (ERBFI | ETBEI)) {
1077 /* ok, the port was enabled */ 1070 /* ok, the port was enabled */
1078 u16 lcr, dlh, dll; 1071 u32 lcr, clk;
1079 1072
1080 lcr = UART_GET_LCR(uart); 1073 lcr = UART_GET_LCR(uart);
1081 1074
@@ -1086,30 +1079,17 @@ bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
1086 else 1079 else
1087 *parity = 'o'; 1080 *parity = 'o';
1088 } 1081 }
1089 switch (lcr & 0x03) { 1082 *bits = ((lcr & WLS_MASK) >> WLS_OFFSET) + 5;
1090 case 0: 1083
1091 *bits = 5; 1084 /* Set DLAB in LCR to Access CLK */
1092 break;
1093 case 1:
1094 *bits = 6;
1095 break;
1096 case 2:
1097 *bits = 7;
1098 break;
1099 case 3:
1100 *bits = 8;
1101 break;
1102 }
1103 /* Set DLAB in LCR to Access DLL and DLH */
1104 UART_SET_DLAB(uart); 1085 UART_SET_DLAB(uart);
1105 1086
1106 dll = UART_GET_DLL(uart); 1087 clk = UART_GET_CLK(uart);
1107 dlh = UART_GET_DLH(uart);
1108 1088
1109 /* Clear DLAB in LCR to Access THR RBR IER */ 1089 /* Clear DLAB in LCR to Access THR RBR IER */
1110 UART_CLEAR_DLAB(uart); 1090 UART_CLEAR_DLAB(uart);
1111 1091
1112 *baud = get_sclk() / (16*(dll | dlh << 8)); 1092 *baud = get_sclk() / (16*clk);
1113 } 1093 }
1114 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits); 1094 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
1115} 1095}
diff --git a/drivers/tty/serial/crisv10.c b/drivers/tty/serial/crisv10.c
index 5b07c0c3a10c..7264d4d26717 100644
--- a/drivers/tty/serial/crisv10.c
+++ b/drivers/tty/serial/crisv10.c
@@ -952,19 +952,6 @@ static const struct control_pins e100_modem_pins[NR_PORTS] =
952/* Input */ 952/* Input */
953#define E100_DSR_GET(info) ((*e100_modem_pins[(info)->line].dsr_port) & e100_modem_pins[(info)->line].dsr_mask) 953#define E100_DSR_GET(info) ((*e100_modem_pins[(info)->line].dsr_port) & e100_modem_pins[(info)->line].dsr_mask)
954 954
955
956/*
957 * tmp_buf is used as a temporary buffer by serial_write. We need to
958 * lock it in case the memcpy_fromfs blocks while swapping in a page,
959 * and some other program tries to do a serial write at the same time.
960 * Since the lock will only come under contention when the system is
961 * swapping and available memory is low, it makes sense to share one
962 * buffer across all the serial ports, since it significantly saves
963 * memory if large numbers of serial ports are open.
964 */
965static unsigned char *tmp_buf;
966static DEFINE_MUTEX(tmp_buf_mutex);
967
968/* Calculate the chartime depending on baudrate, numbor of bits etc. */ 955/* Calculate the chartime depending on baudrate, numbor of bits etc. */
969static void update_char_time(struct e100_serial * info) 956static void update_char_time(struct e100_serial * info)
970{ 957{
@@ -3150,7 +3137,7 @@ static int rs_raw_write(struct tty_struct *tty,
3150 3137
3151 /* first some sanity checks */ 3138 /* first some sanity checks */
3152 3139
3153 if (!tty || !info->xmit.buf || !tmp_buf) 3140 if (!tty || !info->xmit.buf)
3154 return 0; 3141 return 0;
3155 3142
3156#ifdef SERIAL_DEBUG_DATA 3143#ifdef SERIAL_DEBUG_DATA
@@ -3989,7 +3976,7 @@ block_til_ready(struct tty_struct *tty, struct file * filp,
3989 */ 3976 */
3990 if (tty_hung_up_p(filp) || 3977 if (tty_hung_up_p(filp) ||
3991 (info->flags & ASYNC_CLOSING)) { 3978 (info->flags & ASYNC_CLOSING)) {
3992 wait_event_interruptible_tty(info->close_wait, 3979 wait_event_interruptible_tty(tty, info->close_wait,
3993 !(info->flags & ASYNC_CLOSING)); 3980 !(info->flags & ASYNC_CLOSING));
3994#ifdef SERIAL_DO_RESTART 3981#ifdef SERIAL_DO_RESTART
3995 if (info->flags & ASYNC_HUP_NOTIFY) 3982 if (info->flags & ASYNC_HUP_NOTIFY)
@@ -4065,9 +4052,9 @@ block_til_ready(struct tty_struct *tty, struct file * filp,
4065 printk("block_til_ready blocking: ttyS%d, count = %d\n", 4052 printk("block_til_ready blocking: ttyS%d, count = %d\n",
4066 info->line, info->count); 4053 info->line, info->count);
4067#endif 4054#endif
4068 tty_unlock(); 4055 tty_unlock(tty);
4069 schedule(); 4056 schedule();
4070 tty_lock(); 4057 tty_lock(tty);
4071 } 4058 }
4072 set_current_state(TASK_RUNNING); 4059 set_current_state(TASK_RUNNING);
4073 remove_wait_queue(&info->open_wait, &wait); 4060 remove_wait_queue(&info->open_wait, &wait);
@@ -4106,7 +4093,6 @@ rs_open(struct tty_struct *tty, struct file * filp)
4106{ 4093{
4107 struct e100_serial *info; 4094 struct e100_serial *info;
4108 int retval; 4095 int retval;
4109 unsigned long page;
4110 int allocated_resources = 0; 4096 int allocated_resources = 0;
4111 4097
4112 info = rs_table + tty->index; 4098 info = rs_table + tty->index;
@@ -4124,23 +4110,12 @@ rs_open(struct tty_struct *tty, struct file * filp)
4124 4110
4125 tty->low_latency = !!(info->flags & ASYNC_LOW_LATENCY); 4111 tty->low_latency = !!(info->flags & ASYNC_LOW_LATENCY);
4126 4112
4127 if (!tmp_buf) {
4128 page = get_zeroed_page(GFP_KERNEL);
4129 if (!page) {
4130 return -ENOMEM;
4131 }
4132 if (tmp_buf)
4133 free_page(page);
4134 else
4135 tmp_buf = (unsigned char *) page;
4136 }
4137
4138 /* 4113 /*
4139 * If the port is in the middle of closing, bail out now 4114 * If the port is in the middle of closing, bail out now
4140 */ 4115 */
4141 if (tty_hung_up_p(filp) || 4116 if (tty_hung_up_p(filp) ||
4142 (info->flags & ASYNC_CLOSING)) { 4117 (info->flags & ASYNC_CLOSING)) {
4143 wait_event_interruptible_tty(info->close_wait, 4118 wait_event_interruptible_tty(tty, info->close_wait,
4144 !(info->flags & ASYNC_CLOSING)); 4119 !(info->flags & ASYNC_CLOSING));
4145#ifdef SERIAL_DO_RESTART 4120#ifdef SERIAL_DO_RESTART
4146 return ((info->flags & ASYNC_HUP_NOTIFY) ? 4121 return ((info->flags & ASYNC_HUP_NOTIFY) ?
@@ -4487,6 +4462,7 @@ static int __init rs_init(void)
4487 info->enabled = 0; 4462 info->enabled = 0;
4488 } 4463 }
4489 } 4464 }
4465 tty_port_init(&info->port);
4490 info->uses_dma_in = 0; 4466 info->uses_dma_in = 0;
4491 info->uses_dma_out = 0; 4467 info->uses_dma_out = 0;
4492 info->line = i; 4468 info->line = i;
diff --git a/drivers/tty/serial/mxs-auart.c b/drivers/tty/serial/mxs-auart.c
index 7081600bede4..ec56d8397aae 100644
--- a/drivers/tty/serial/mxs-auart.c
+++ b/drivers/tty/serial/mxs-auart.c
@@ -370,6 +370,8 @@ static void mxs_auart_settermios(struct uart_port *u,
370 370
371 writel(ctrl, u->membase + AUART_LINECTRL); 371 writel(ctrl, u->membase + AUART_LINECTRL);
372 writel(ctrl2, u->membase + AUART_CTRL2); 372 writel(ctrl2, u->membase + AUART_CTRL2);
373
374 uart_update_timeout(u, termios->c_cflag, baud);
373} 375}
374 376
375static irqreturn_t mxs_auart_irq_handle(int irq, void *context) 377static irqreturn_t mxs_auart_irq_handle(int irq, void *context)
diff --git a/drivers/tty/serial/of_serial.c b/drivers/tty/serial/of_serial.c
index e8c9cee07d00..5410c0637266 100644
--- a/drivers/tty/serial/of_serial.c
+++ b/drivers/tty/serial/of_serial.c
@@ -12,10 +12,13 @@
12#include <linux/init.h> 12#include <linux/init.h>
13#include <linux/module.h> 13#include <linux/module.h>
14#include <linux/slab.h> 14#include <linux/slab.h>
15#include <linux/delay.h>
15#include <linux/serial_core.h> 16#include <linux/serial_core.h>
16#include <linux/serial_8250.h> 17#include <linux/serial_8250.h>
18#include <linux/serial_reg.h>
17#include <linux/of_address.h> 19#include <linux/of_address.h>
18#include <linux/of_irq.h> 20#include <linux/of_irq.h>
21#include <linux/of_serial.h>
19#include <linux/of_platform.h> 22#include <linux/of_platform.h>
20#include <linux/nwpserial.h> 23#include <linux/nwpserial.h>
21 24
@@ -24,6 +27,26 @@ struct of_serial_info {
24 int line; 27 int line;
25}; 28};
26 29
30#ifdef CONFIG_ARCH_TEGRA
31void tegra_serial_handle_break(struct uart_port *p)
32{
33 unsigned int status, tmout = 10000;
34
35 do {
36 status = p->serial_in(p, UART_LSR);
37 if (status & (UART_LSR_FIFOE | UART_LSR_BRK_ERROR_BITS))
38 status = p->serial_in(p, UART_RX);
39 else
40 break;
41 if (--tmout == 0)
42 break;
43 udelay(1);
44 } while (1);
45}
46/* FIXME remove this export when tegra finishes conversion to open firmware */
47EXPORT_SYMBOL_GPL(tegra_serial_handle_break);
48#endif
49
27/* 50/*
28 * Fill a struct uart_port for a given device node 51 * Fill a struct uart_port for a given device node
29 */ 52 */
@@ -84,6 +107,9 @@ static int __devinit of_platform_serial_setup(struct platform_device *ofdev,
84 | UPF_FIXED_PORT | UPF_FIXED_TYPE; 107 | UPF_FIXED_PORT | UPF_FIXED_TYPE;
85 port->dev = &ofdev->dev; 108 port->dev = &ofdev->dev;
86 109
110 if (type == PORT_TEGRA)
111 port->handle_break = tegra_serial_handle_break;
112
87 return 0; 113 return 0;
88} 114}
89 115
diff --git a/drivers/tty/serial/omap-serial.c b/drivers/tty/serial/omap-serial.c
index d00b38eb268e..d3cda0cb2df0 100644
--- a/drivers/tty/serial/omap-serial.c
+++ b/drivers/tty/serial/omap-serial.c
@@ -44,6 +44,13 @@
44#include <plat/dmtimer.h> 44#include <plat/dmtimer.h>
45#include <plat/omap-serial.h> 45#include <plat/omap-serial.h>
46 46
47#define UART_BUILD_REVISION(x, y) (((x) << 8) | (y))
48
49#define OMAP_UART_REV_42 0x0402
50#define OMAP_UART_REV_46 0x0406
51#define OMAP_UART_REV_52 0x0502
52#define OMAP_UART_REV_63 0x0603
53
47#define DEFAULT_CLK_SPEED 48000000 /* 48Mhz*/ 54#define DEFAULT_CLK_SPEED 48000000 /* 48Mhz*/
48 55
49/* SCR register bitmasks */ 56/* SCR register bitmasks */
@@ -53,6 +60,17 @@
53#define OMAP_UART_FCR_RX_FIFO_TRIG_SHIFT 6 60#define OMAP_UART_FCR_RX_FIFO_TRIG_SHIFT 6
54#define OMAP_UART_FCR_RX_FIFO_TRIG_MASK (0x3 << 6) 61#define OMAP_UART_FCR_RX_FIFO_TRIG_MASK (0x3 << 6)
55 62
63/* MVR register bitmasks */
64#define OMAP_UART_MVR_SCHEME_SHIFT 30
65
66#define OMAP_UART_LEGACY_MVR_MAJ_MASK 0xf0
67#define OMAP_UART_LEGACY_MVR_MAJ_SHIFT 4
68#define OMAP_UART_LEGACY_MVR_MIN_MASK 0x0f
69
70#define OMAP_UART_MVR_MAJ_MASK 0x700
71#define OMAP_UART_MVR_MAJ_SHIFT 8
72#define OMAP_UART_MVR_MIN_MASK 0x3f
73
56static struct uart_omap_port *ui[OMAP_MAX_HSUART_PORTS]; 74static struct uart_omap_port *ui[OMAP_MAX_HSUART_PORTS];
57 75
58/* Forward declaration of functions */ 76/* Forward declaration of functions */
@@ -1346,6 +1364,59 @@ static void uart_tx_dma_callback(int lch, u16 ch_status, void *data)
1346 return; 1364 return;
1347} 1365}
1348 1366
1367static void omap_serial_fill_features_erratas(struct uart_omap_port *up)
1368{
1369 u32 mvr, scheme;
1370 u16 revision, major, minor;
1371
1372 mvr = serial_in(up, UART_OMAP_MVER);
1373
1374 /* Check revision register scheme */
1375 scheme = mvr >> OMAP_UART_MVR_SCHEME_SHIFT;
1376
1377 switch (scheme) {
1378 case 0: /* Legacy Scheme: OMAP2/3 */
1379 /* MINOR_REV[0:4], MAJOR_REV[4:7] */
1380 major = (mvr & OMAP_UART_LEGACY_MVR_MAJ_MASK) >>
1381 OMAP_UART_LEGACY_MVR_MAJ_SHIFT;
1382 minor = (mvr & OMAP_UART_LEGACY_MVR_MIN_MASK);
1383 break;
1384 case 1:
1385 /* New Scheme: OMAP4+ */
1386 /* MINOR_REV[0:5], MAJOR_REV[8:10] */
1387 major = (mvr & OMAP_UART_MVR_MAJ_MASK) >>
1388 OMAP_UART_MVR_MAJ_SHIFT;
1389 minor = (mvr & OMAP_UART_MVR_MIN_MASK);
1390 break;
1391 default:
1392 dev_warn(&up->pdev->dev,
1393 "Unknown %s revision, defaulting to highest\n",
1394 up->name);
1395 /* highest possible revision */
1396 major = 0xff;
1397 minor = 0xff;
1398 }
1399
1400 /* normalize revision for the driver */
1401 revision = UART_BUILD_REVISION(major, minor);
1402
1403 switch (revision) {
1404 case OMAP_UART_REV_46:
1405 up->errata |= (UART_ERRATA_i202_MDR1_ACCESS |
1406 UART_ERRATA_i291_DMA_FORCEIDLE);
1407 break;
1408 case OMAP_UART_REV_52:
1409 up->errata |= (UART_ERRATA_i202_MDR1_ACCESS |
1410 UART_ERRATA_i291_DMA_FORCEIDLE);
1411 break;
1412 case OMAP_UART_REV_63:
1413 up->errata |= UART_ERRATA_i202_MDR1_ACCESS;
1414 break;
1415 default:
1416 break;
1417 }
1418}
1419
1349static struct omap_uart_port_info *of_get_uart_port_info(struct device *dev) 1420static struct omap_uart_port_info *of_get_uart_port_info(struct device *dev)
1350{ 1421{
1351 struct omap_uart_port_info *omap_up_info; 1422 struct omap_uart_port_info *omap_up_info;
@@ -1439,7 +1510,6 @@ static int serial_omap_probe(struct platform_device *pdev)
1439 "%d\n", DEFAULT_CLK_SPEED); 1510 "%d\n", DEFAULT_CLK_SPEED);
1440 } 1511 }
1441 up->uart_dma.uart_base = mem->start; 1512 up->uart_dma.uart_base = mem->start;
1442 up->errata = omap_up_info->errata;
1443 1513
1444 if (omap_up_info->dma_enabled) { 1514 if (omap_up_info->dma_enabled) {
1445 up->uart_dma.uart_dma_tx = dma_tx->start; 1515 up->uart_dma.uart_dma_tx = dma_tx->start;
@@ -1469,6 +1539,8 @@ static int serial_omap_probe(struct platform_device *pdev)
1469 pm_runtime_enable(&pdev->dev); 1539 pm_runtime_enable(&pdev->dev);
1470 pm_runtime_get_sync(&pdev->dev); 1540 pm_runtime_get_sync(&pdev->dev);
1471 1541
1542 omap_serial_fill_features_erratas(up);
1543
1472 ui[up->port.line] = up; 1544 ui[up->port.line] = up;
1473 serial_omap_add_console_port(up); 1545 serial_omap_add_console_port(up);
1474 1546
diff --git a/drivers/tty/serial/pch_uart.c b/drivers/tty/serial/pch_uart.c
index c2816f494807..4fdec6a6b758 100644
--- a/drivers/tty/serial/pch_uart.c
+++ b/drivers/tty/serial/pch_uart.c
@@ -39,6 +39,7 @@ enum {
39 PCH_UART_HANDLED_RX_ERR_INT_SHIFT, 39 PCH_UART_HANDLED_RX_ERR_INT_SHIFT,
40 PCH_UART_HANDLED_RX_TRG_INT_SHIFT, 40 PCH_UART_HANDLED_RX_TRG_INT_SHIFT,
41 PCH_UART_HANDLED_MS_INT_SHIFT, 41 PCH_UART_HANDLED_MS_INT_SHIFT,
42 PCH_UART_HANDLED_LS_INT_SHIFT,
42}; 43};
43 44
44enum { 45enum {
@@ -63,6 +64,8 @@ enum {
63 PCH_UART_HANDLED_RX_TRG_INT_SHIFT)<<1)) 64 PCH_UART_HANDLED_RX_TRG_INT_SHIFT)<<1))
64#define PCH_UART_HANDLED_MS_INT (1<<((PCH_UART_HANDLED_MS_INT_SHIFT)<<1)) 65#define PCH_UART_HANDLED_MS_INT (1<<((PCH_UART_HANDLED_MS_INT_SHIFT)<<1))
65 66
67#define PCH_UART_HANDLED_LS_INT (1<<((PCH_UART_HANDLED_LS_INT_SHIFT)<<1))
68
66#define PCH_UART_RBR 0x00 69#define PCH_UART_RBR 0x00
67#define PCH_UART_THR 0x00 70#define PCH_UART_THR 0x00
68 71
@@ -229,7 +232,6 @@ struct eg20t_port {
229 int start_tx; 232 int start_tx;
230 int start_rx; 233 int start_rx;
231 int tx_empty; 234 int tx_empty;
232 int int_dis_flag;
233 int trigger; 235 int trigger;
234 int trigger_level; 236 int trigger_level;
235 struct pch_uart_buffer rxbuf; 237 struct pch_uart_buffer rxbuf;
@@ -237,7 +239,6 @@ struct eg20t_port {
237 unsigned int fcr; 239 unsigned int fcr;
238 unsigned int mcr; 240 unsigned int mcr;
239 unsigned int use_dma; 241 unsigned int use_dma;
240 unsigned int use_dma_flag;
241 struct dma_async_tx_descriptor *desc_tx; 242 struct dma_async_tx_descriptor *desc_tx;
242 struct dma_async_tx_descriptor *desc_rx; 243 struct dma_async_tx_descriptor *desc_rx;
243 struct pch_dma_slave param_tx; 244 struct pch_dma_slave param_tx;
@@ -560,14 +561,10 @@ static int pch_uart_hal_read(struct eg20t_port *priv, unsigned char *buf,
560 return i; 561 return i;
561} 562}
562 563
563static unsigned int pch_uart_hal_get_iid(struct eg20t_port *priv) 564static unsigned char pch_uart_hal_get_iid(struct eg20t_port *priv)
564{ 565{
565 unsigned int iir; 566 return ioread8(priv->membase + UART_IIR) &\
566 int ret; 567 (PCH_UART_IIR_IID | PCH_UART_IIR_TOI | PCH_UART_IIR_IP);
567
568 iir = ioread8(priv->membase + UART_IIR);
569 ret = (iir & (PCH_UART_IIR_IID | PCH_UART_IIR_TOI | PCH_UART_IIR_IP));
570 return ret;
571} 568}
572 569
573static u8 pch_uart_hal_get_line_status(struct eg20t_port *priv) 570static u8 pch_uart_hal_get_line_status(struct eg20t_port *priv)
@@ -666,10 +663,13 @@ static void pch_free_dma(struct uart_port *port)
666 dma_release_channel(priv->chan_rx); 663 dma_release_channel(priv->chan_rx);
667 priv->chan_rx = NULL; 664 priv->chan_rx = NULL;
668 } 665 }
669 if (sg_dma_address(&priv->sg_rx)) 666
670 dma_free_coherent(port->dev, port->fifosize, 667 if (priv->rx_buf_dma) {
671 sg_virt(&priv->sg_rx), 668 dma_free_coherent(port->dev, port->fifosize, priv->rx_buf_virt,
672 sg_dma_address(&priv->sg_rx)); 669 priv->rx_buf_dma);
670 priv->rx_buf_virt = NULL;
671 priv->rx_buf_dma = 0;
672 }
673 673
674 return; 674 return;
675} 675}
@@ -1053,12 +1053,17 @@ static irqreturn_t pch_uart_interrupt(int irq, void *dev_id)
1053 unsigned int handled; 1053 unsigned int handled;
1054 u8 lsr; 1054 u8 lsr;
1055 int ret = 0; 1055 int ret = 0;
1056 unsigned int iid; 1056 unsigned char iid;
1057 unsigned long flags; 1057 unsigned long flags;
1058 int next = 1;
1059 u8 msr;
1058 1060
1059 spin_lock_irqsave(&priv->port.lock, flags); 1061 spin_lock_irqsave(&priv->port.lock, flags);
1060 handled = 0; 1062 handled = 0;
1061 while ((iid = pch_uart_hal_get_iid(priv)) > 1) { 1063 while (next) {
1064 iid = pch_uart_hal_get_iid(priv);
1065 if (iid & PCH_UART_IIR_IP) /* No Interrupt */
1066 break;
1062 switch (iid) { 1067 switch (iid) {
1063 case PCH_UART_IID_RLS: /* Receiver Line Status */ 1068 case PCH_UART_IID_RLS: /* Receiver Line Status */
1064 lsr = pch_uart_hal_get_line_status(priv); 1069 lsr = pch_uart_hal_get_line_status(priv);
@@ -1066,6 +1071,8 @@ static irqreturn_t pch_uart_interrupt(int irq, void *dev_id)
1066 UART_LSR_PE | UART_LSR_OE)) { 1071 UART_LSR_PE | UART_LSR_OE)) {
1067 pch_uart_err_ir(priv, lsr); 1072 pch_uart_err_ir(priv, lsr);
1068 ret = PCH_UART_HANDLED_RX_ERR_INT; 1073 ret = PCH_UART_HANDLED_RX_ERR_INT;
1074 } else {
1075 ret = PCH_UART_HANDLED_LS_INT;
1069 } 1076 }
1070 break; 1077 break;
1071 case PCH_UART_IID_RDR: /* Received Data Ready */ 1078 case PCH_UART_IID_RDR: /* Received Data Ready */
@@ -1092,20 +1099,22 @@ static irqreturn_t pch_uart_interrupt(int irq, void *dev_id)
1092 ret = handle_tx(priv); 1099 ret = handle_tx(priv);
1093 break; 1100 break;
1094 case PCH_UART_IID_MS: /* Modem Status */ 1101 case PCH_UART_IID_MS: /* Modem Status */
1095 ret = PCH_UART_HANDLED_MS_INT; 1102 msr = pch_uart_hal_get_modem(priv);
1103 next = 0; /* MS ir prioirty is the lowest. So, MS ir
1104 means final interrupt */
1105 if ((msr & UART_MSR_ANY_DELTA) == 0)
1106 break;
1107 ret |= PCH_UART_HANDLED_MS_INT;
1096 break; 1108 break;
1097 default: /* Never junp to this label */ 1109 default: /* Never junp to this label */
1098 dev_err(priv->port.dev, "%s:iid=%d (%lu)\n", __func__, 1110 dev_err(priv->port.dev, "%s:iid=%02x (%lu)\n", __func__,
1099 iid, jiffies); 1111 iid, jiffies);
1100 ret = -1; 1112 ret = -1;
1113 next = 0;
1101 break; 1114 break;
1102 } 1115 }
1103 handled |= (unsigned int)ret; 1116 handled |= (unsigned int)ret;
1104 } 1117 }
1105 if (handled == 0 && iid <= 1) {
1106 if (priv->int_dis_flag)
1107 priv->int_dis_flag = 0;
1108 }
1109 1118
1110 spin_unlock_irqrestore(&priv->port.lock, flags); 1119 spin_unlock_irqrestore(&priv->port.lock, flags);
1111 return IRQ_RETVAL(handled); 1120 return IRQ_RETVAL(handled);
@@ -1200,7 +1209,6 @@ static void pch_uart_stop_rx(struct uart_port *port)
1200 priv = container_of(port, struct eg20t_port, port); 1209 priv = container_of(port, struct eg20t_port, port);
1201 priv->start_rx = 0; 1210 priv->start_rx = 0;
1202 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT); 1211 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
1203 priv->int_dis_flag = 1;
1204} 1212}
1205 1213
1206/* Enable the modem status interrupts. */ 1214/* Enable the modem status interrupts. */
@@ -1447,7 +1455,6 @@ static int pch_uart_verify_port(struct uart_port *port,
1447 __func__); 1455 __func__);
1448 return -EOPNOTSUPP; 1456 return -EOPNOTSUPP;
1449#endif 1457#endif
1450 priv->use_dma_flag = 1;
1451 dev_info(priv->port.dev, "PCH UART : Use DMA Mode\n"); 1458 dev_info(priv->port.dev, "PCH UART : Use DMA Mode\n");
1452 if (!priv->use_dma) 1459 if (!priv->use_dma)
1453 pch_request_dma(port); 1460 pch_request_dma(port);
diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index 9c4c05b2825b..246b823c1b27 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -2282,6 +2282,7 @@ void uart_unregister_driver(struct uart_driver *drv)
2282 tty_unregister_driver(p); 2282 tty_unregister_driver(p);
2283 put_tty_driver(p); 2283 put_tty_driver(p);
2284 kfree(drv->state); 2284 kfree(drv->state);
2285 drv->state = NULL;
2285 drv->tty_driver = NULL; 2286 drv->tty_driver = NULL;
2286} 2287}
2287 2288
diff --git a/drivers/tty/synclink.c b/drivers/tty/synclink.c
index 593d40ad0a6b..5ed0daae6564 100644
--- a/drivers/tty/synclink.c
+++ b/drivers/tty/synclink.c
@@ -3338,9 +3338,9 @@ static int block_til_ready(struct tty_struct *tty, struct file * filp,
3338 printk("%s(%d):block_til_ready blocking on %s count=%d\n", 3338 printk("%s(%d):block_til_ready blocking on %s count=%d\n",
3339 __FILE__,__LINE__, tty->driver->name, port->count ); 3339 __FILE__,__LINE__, tty->driver->name, port->count );
3340 3340
3341 tty_unlock(); 3341 tty_unlock(tty);
3342 schedule(); 3342 schedule();
3343 tty_lock(); 3343 tty_lock(tty);
3344 } 3344 }
3345 3345
3346 set_current_state(TASK_RUNNING); 3346 set_current_state(TASK_RUNNING);
diff --git a/drivers/tty/synclink_gt.c b/drivers/tty/synclink_gt.c
index aa1debf97cc7..45b43f11ca39 100644
--- a/drivers/tty/synclink_gt.c
+++ b/drivers/tty/synclink_gt.c
@@ -3336,9 +3336,9 @@ static int block_til_ready(struct tty_struct *tty, struct file *filp,
3336 } 3336 }
3337 3337
3338 DBGINFO(("%s block_til_ready wait\n", tty->driver->name)); 3338 DBGINFO(("%s block_til_ready wait\n", tty->driver->name));
3339 tty_unlock(); 3339 tty_unlock(tty);
3340 schedule(); 3340 schedule();
3341 tty_lock(); 3341 tty_lock(tty);
3342 } 3342 }
3343 3343
3344 set_current_state(TASK_RUNNING); 3344 set_current_state(TASK_RUNNING);
diff --git a/drivers/tty/synclinkmp.c b/drivers/tty/synclinkmp.c
index a3dddc12d2fe..4a1e4f07765b 100644
--- a/drivers/tty/synclinkmp.c
+++ b/drivers/tty/synclinkmp.c
@@ -3357,9 +3357,9 @@ static int block_til_ready(struct tty_struct *tty, struct file *filp,
3357 printk("%s(%d):%s block_til_ready() count=%d\n", 3357 printk("%s(%d):%s block_til_ready() count=%d\n",
3358 __FILE__,__LINE__, tty->driver->name, port->count ); 3358 __FILE__,__LINE__, tty->driver->name, port->count );
3359 3359
3360 tty_unlock(); 3360 tty_unlock(tty);
3361 schedule(); 3361 schedule();
3362 tty_lock(); 3362 tty_lock(tty);
3363 } 3363 }
3364 3364
3365 set_current_state(TASK_RUNNING); 3365 set_current_state(TASK_RUNNING);
diff --git a/drivers/tty/tty_buffer.c b/drivers/tty/tty_buffer.c
index 6c9b7cd6778a..91e326ffe7db 100644
--- a/drivers/tty/tty_buffer.c
+++ b/drivers/tty/tty_buffer.c
@@ -185,25 +185,19 @@ static struct tty_buffer *tty_buffer_find(struct tty_struct *tty, size_t size)
185 /* Should possibly check if this fails for the largest buffer we 185 /* Should possibly check if this fails for the largest buffer we
186 have queued and recycle that ? */ 186 have queued and recycle that ? */
187} 187}
188
189/** 188/**
190 * tty_buffer_request_room - grow tty buffer if needed 189 * __tty_buffer_request_room - grow tty buffer if needed
191 * @tty: tty structure 190 * @tty: tty structure
192 * @size: size desired 191 * @size: size desired
193 * 192 *
194 * Make at least size bytes of linear space available for the tty 193 * Make at least size bytes of linear space available for the tty
195 * buffer. If we fail return the size we managed to find. 194 * buffer. If we fail return the size we managed to find.
196 * 195 * Locking: Caller must hold tty->buf.lock
197 * Locking: Takes tty->buf.lock
198 */ 196 */
199int tty_buffer_request_room(struct tty_struct *tty, size_t size) 197static int __tty_buffer_request_room(struct tty_struct *tty, size_t size)
200{ 198{
201 struct tty_buffer *b, *n; 199 struct tty_buffer *b, *n;
202 int left; 200 int left;
203 unsigned long flags;
204
205 spin_lock_irqsave(&tty->buf.lock, flags);
206
207 /* OPTIMISATION: We could keep a per tty "zero" sized buffer to 201 /* OPTIMISATION: We could keep a per tty "zero" sized buffer to
208 remove this conditional if its worth it. This would be invisible 202 remove this conditional if its worth it. This would be invisible
209 to the callers */ 203 to the callers */
@@ -225,9 +219,30 @@ int tty_buffer_request_room(struct tty_struct *tty, size_t size)
225 size = left; 219 size = left;
226 } 220 }
227 221
228 spin_unlock_irqrestore(&tty->buf.lock, flags);
229 return size; 222 return size;
230} 223}
224
225
226/**
227 * tty_buffer_request_room - grow tty buffer if needed
228 * @tty: tty structure
229 * @size: size desired
230 *
231 * Make at least size bytes of linear space available for the tty
232 * buffer. If we fail return the size we managed to find.
233 *
234 * Locking: Takes tty->buf.lock
235 */
236int tty_buffer_request_room(struct tty_struct *tty, size_t size)
237{
238 unsigned long flags;
239 int length;
240
241 spin_lock_irqsave(&tty->buf.lock, flags);
242 length = __tty_buffer_request_room(tty, size);
243 spin_unlock_irqrestore(&tty->buf.lock, flags);
244 return length;
245}
231EXPORT_SYMBOL_GPL(tty_buffer_request_room); 246EXPORT_SYMBOL_GPL(tty_buffer_request_room);
232 247
233/** 248/**
@@ -249,14 +264,22 @@ int tty_insert_flip_string_fixed_flag(struct tty_struct *tty,
249 int copied = 0; 264 int copied = 0;
250 do { 265 do {
251 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE); 266 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
252 int space = tty_buffer_request_room(tty, goal); 267 int space;
253 struct tty_buffer *tb = tty->buf.tail; 268 unsigned long flags;
269 struct tty_buffer *tb;
270
271 spin_lock_irqsave(&tty->buf.lock, flags);
272 space = __tty_buffer_request_room(tty, goal);
273 tb = tty->buf.tail;
254 /* If there is no space then tb may be NULL */ 274 /* If there is no space then tb may be NULL */
255 if (unlikely(space == 0)) 275 if (unlikely(space == 0)) {
276 spin_unlock_irqrestore(&tty->buf.lock, flags);
256 break; 277 break;
278 }
257 memcpy(tb->char_buf_ptr + tb->used, chars, space); 279 memcpy(tb->char_buf_ptr + tb->used, chars, space);
258 memset(tb->flag_buf_ptr + tb->used, flag, space); 280 memset(tb->flag_buf_ptr + tb->used, flag, space);
259 tb->used += space; 281 tb->used += space;
282 spin_unlock_irqrestore(&tty->buf.lock, flags);
260 copied += space; 283 copied += space;
261 chars += space; 284 chars += space;
262 /* There is a small chance that we need to split the data over 285 /* There is a small chance that we need to split the data over
@@ -286,14 +309,22 @@ int tty_insert_flip_string_flags(struct tty_struct *tty,
286 int copied = 0; 309 int copied = 0;
287 do { 310 do {
288 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE); 311 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
289 int space = tty_buffer_request_room(tty, goal); 312 int space;
290 struct tty_buffer *tb = tty->buf.tail; 313 unsigned long __flags;
314 struct tty_buffer *tb;
315
316 spin_lock_irqsave(&tty->buf.lock, __flags);
317 space = __tty_buffer_request_room(tty, goal);
318 tb = tty->buf.tail;
291 /* If there is no space then tb may be NULL */ 319 /* If there is no space then tb may be NULL */
292 if (unlikely(space == 0)) 320 if (unlikely(space == 0)) {
321 spin_unlock_irqrestore(&tty->buf.lock, __flags);
293 break; 322 break;
323 }
294 memcpy(tb->char_buf_ptr + tb->used, chars, space); 324 memcpy(tb->char_buf_ptr + tb->used, chars, space);
295 memcpy(tb->flag_buf_ptr + tb->used, flags, space); 325 memcpy(tb->flag_buf_ptr + tb->used, flags, space);
296 tb->used += space; 326 tb->used += space;
327 spin_unlock_irqrestore(&tty->buf.lock, __flags);
297 copied += space; 328 copied += space;
298 chars += space; 329 chars += space;
299 flags += space; 330 flags += space;
@@ -344,13 +375,20 @@ EXPORT_SYMBOL(tty_schedule_flip);
344int tty_prepare_flip_string(struct tty_struct *tty, unsigned char **chars, 375int tty_prepare_flip_string(struct tty_struct *tty, unsigned char **chars,
345 size_t size) 376 size_t size)
346{ 377{
347 int space = tty_buffer_request_room(tty, size); 378 int space;
379 unsigned long flags;
380 struct tty_buffer *tb;
381
382 spin_lock_irqsave(&tty->buf.lock, flags);
383 space = __tty_buffer_request_room(tty, size);
384
385 tb = tty->buf.tail;
348 if (likely(space)) { 386 if (likely(space)) {
349 struct tty_buffer *tb = tty->buf.tail;
350 *chars = tb->char_buf_ptr + tb->used; 387 *chars = tb->char_buf_ptr + tb->used;
351 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space); 388 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
352 tb->used += space; 389 tb->used += space;
353 } 390 }
391 spin_unlock_irqrestore(&tty->buf.lock, flags);
354 return space; 392 return space;
355} 393}
356EXPORT_SYMBOL_GPL(tty_prepare_flip_string); 394EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
@@ -374,13 +412,20 @@ EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
374int tty_prepare_flip_string_flags(struct tty_struct *tty, 412int tty_prepare_flip_string_flags(struct tty_struct *tty,
375 unsigned char **chars, char **flags, size_t size) 413 unsigned char **chars, char **flags, size_t size)
376{ 414{
377 int space = tty_buffer_request_room(tty, size); 415 int space;
416 unsigned long __flags;
417 struct tty_buffer *tb;
418
419 spin_lock_irqsave(&tty->buf.lock, __flags);
420 space = __tty_buffer_request_room(tty, size);
421
422 tb = tty->buf.tail;
378 if (likely(space)) { 423 if (likely(space)) {
379 struct tty_buffer *tb = tty->buf.tail;
380 *chars = tb->char_buf_ptr + tb->used; 424 *chars = tb->char_buf_ptr + tb->used;
381 *flags = tb->flag_buf_ptr + tb->used; 425 *flags = tb->flag_buf_ptr + tb->used;
382 tb->used += space; 426 tb->used += space;
383 } 427 }
428 spin_unlock_irqrestore(&tty->buf.lock, __flags);
384 return space; 429 return space;
385} 430}
386EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags); 431EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
index d939bd705c71..9e930c009bf2 100644
--- a/drivers/tty/tty_io.c
+++ b/drivers/tty/tty_io.c
@@ -185,6 +185,7 @@ void free_tty_struct(struct tty_struct *tty)
185 put_device(tty->dev); 185 put_device(tty->dev);
186 kfree(tty->write_buf); 186 kfree(tty->write_buf);
187 tty_buffer_free_all(tty); 187 tty_buffer_free_all(tty);
188 tty->magic = 0xDEADDEAD;
188 kfree(tty); 189 kfree(tty);
189} 190}
190 191
@@ -573,7 +574,7 @@ void __tty_hangup(struct tty_struct *tty)
573 } 574 }
574 spin_unlock(&redirect_lock); 575 spin_unlock(&redirect_lock);
575 576
576 tty_lock(); 577 tty_lock(tty);
577 578
578 /* some functions below drop BTM, so we need this bit */ 579 /* some functions below drop BTM, so we need this bit */
579 set_bit(TTY_HUPPING, &tty->flags); 580 set_bit(TTY_HUPPING, &tty->flags);
@@ -666,7 +667,7 @@ void __tty_hangup(struct tty_struct *tty)
666 clear_bit(TTY_HUPPING, &tty->flags); 667 clear_bit(TTY_HUPPING, &tty->flags);
667 tty_ldisc_enable(tty); 668 tty_ldisc_enable(tty);
668 669
669 tty_unlock(); 670 tty_unlock(tty);
670 671
671 if (f) 672 if (f)
672 fput(f); 673 fput(f);
@@ -855,10 +856,11 @@ void disassociate_ctty(int on_exit)
855 */ 856 */
856void no_tty(void) 857void no_tty(void)
857{ 858{
859 /* FIXME: Review locking here. The tty_lock never covered any race
860 between a new association and proc_clear_tty but possible we need
861 to protect against this anyway */
858 struct task_struct *tsk = current; 862 struct task_struct *tsk = current;
859 tty_lock();
860 disassociate_ctty(0); 863 disassociate_ctty(0);
861 tty_unlock();
862 proc_clear_tty(tsk); 864 proc_clear_tty(tsk);
863} 865}
864 866
@@ -1102,12 +1104,12 @@ void tty_write_message(struct tty_struct *tty, char *msg)
1102{ 1104{
1103 if (tty) { 1105 if (tty) {
1104 mutex_lock(&tty->atomic_write_lock); 1106 mutex_lock(&tty->atomic_write_lock);
1105 tty_lock(); 1107 tty_lock(tty);
1106 if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) { 1108 if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) {
1107 tty_unlock(); 1109 tty_unlock(tty);
1108 tty->ops->write(tty, msg, strlen(msg)); 1110 tty->ops->write(tty, msg, strlen(msg));
1109 } else 1111 } else
1110 tty_unlock(); 1112 tty_unlock(tty);
1111 tty_write_unlock(tty); 1113 tty_write_unlock(tty);
1112 } 1114 }
1113 return; 1115 return;
@@ -1402,6 +1404,7 @@ struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx)
1402 } 1404 }
1403 initialize_tty_struct(tty, driver, idx); 1405 initialize_tty_struct(tty, driver, idx);
1404 1406
1407 tty_lock(tty);
1405 retval = tty_driver_install_tty(driver, tty); 1408 retval = tty_driver_install_tty(driver, tty);
1406 if (retval < 0) 1409 if (retval < 0)
1407 goto err_deinit_tty; 1410 goto err_deinit_tty;
@@ -1414,9 +1417,11 @@ struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx)
1414 retval = tty_ldisc_setup(tty, tty->link); 1417 retval = tty_ldisc_setup(tty, tty->link);
1415 if (retval) 1418 if (retval)
1416 goto err_release_tty; 1419 goto err_release_tty;
1420 /* Return the tty locked so that it cannot vanish under the caller */
1417 return tty; 1421 return tty;
1418 1422
1419err_deinit_tty: 1423err_deinit_tty:
1424 tty_unlock(tty);
1420 deinitialize_tty_struct(tty); 1425 deinitialize_tty_struct(tty);
1421 free_tty_struct(tty); 1426 free_tty_struct(tty);
1422err_module_put: 1427err_module_put:
@@ -1425,6 +1430,7 @@ err_module_put:
1425 1430
1426 /* call the tty release_tty routine to clean out this slot */ 1431 /* call the tty release_tty routine to clean out this slot */
1427err_release_tty: 1432err_release_tty:
1433 tty_unlock(tty);
1428 printk_ratelimited(KERN_INFO "tty_init_dev: ldisc open failed, " 1434 printk_ratelimited(KERN_INFO "tty_init_dev: ldisc open failed, "
1429 "clearing slot %d\n", idx); 1435 "clearing slot %d\n", idx);
1430 release_tty(tty, idx); 1436 release_tty(tty, idx);
@@ -1627,7 +1633,7 @@ int tty_release(struct inode *inode, struct file *filp)
1627 if (tty_paranoia_check(tty, inode, __func__)) 1633 if (tty_paranoia_check(tty, inode, __func__))
1628 return 0; 1634 return 0;
1629 1635
1630 tty_lock(); 1636 tty_lock(tty);
1631 check_tty_count(tty, __func__); 1637 check_tty_count(tty, __func__);
1632 1638
1633 __tty_fasync(-1, filp, 0); 1639 __tty_fasync(-1, filp, 0);
@@ -1636,10 +1642,11 @@ int tty_release(struct inode *inode, struct file *filp)
1636 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY && 1642 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1637 tty->driver->subtype == PTY_TYPE_MASTER); 1643 tty->driver->subtype == PTY_TYPE_MASTER);
1638 devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0; 1644 devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
1645 /* Review: parallel close */
1639 o_tty = tty->link; 1646 o_tty = tty->link;
1640 1647
1641 if (tty_release_checks(tty, o_tty, idx)) { 1648 if (tty_release_checks(tty, o_tty, idx)) {
1642 tty_unlock(); 1649 tty_unlock(tty);
1643 return 0; 1650 return 0;
1644 } 1651 }
1645 1652
@@ -1651,7 +1658,7 @@ int tty_release(struct inode *inode, struct file *filp)
1651 if (tty->ops->close) 1658 if (tty->ops->close)
1652 tty->ops->close(tty, filp); 1659 tty->ops->close(tty, filp);
1653 1660
1654 tty_unlock(); 1661 tty_unlock(tty);
1655 /* 1662 /*
1656 * Sanity check: if tty->count is going to zero, there shouldn't be 1663 * Sanity check: if tty->count is going to zero, there shouldn't be
1657 * any waiters on tty->read_wait or tty->write_wait. We test the 1664 * any waiters on tty->read_wait or tty->write_wait. We test the
@@ -1674,7 +1681,7 @@ int tty_release(struct inode *inode, struct file *filp)
1674 opens on /dev/tty */ 1681 opens on /dev/tty */
1675 1682
1676 mutex_lock(&tty_mutex); 1683 mutex_lock(&tty_mutex);
1677 tty_lock(); 1684 tty_lock_pair(tty, o_tty);
1678 tty_closing = tty->count <= 1; 1685 tty_closing = tty->count <= 1;
1679 o_tty_closing = o_tty && 1686 o_tty_closing = o_tty &&
1680 (o_tty->count <= (pty_master ? 1 : 0)); 1687 (o_tty->count <= (pty_master ? 1 : 0));
@@ -1705,7 +1712,7 @@ int tty_release(struct inode *inode, struct file *filp)
1705 1712
1706 printk(KERN_WARNING "%s: %s: read/write wait queue active!\n", 1713 printk(KERN_WARNING "%s: %s: read/write wait queue active!\n",
1707 __func__, tty_name(tty, buf)); 1714 __func__, tty_name(tty, buf));
1708 tty_unlock(); 1715 tty_unlock_pair(tty, o_tty);
1709 mutex_unlock(&tty_mutex); 1716 mutex_unlock(&tty_mutex);
1710 schedule(); 1717 schedule();
1711 } 1718 }
@@ -1768,7 +1775,7 @@ int tty_release(struct inode *inode, struct file *filp)
1768 1775
1769 /* check whether both sides are closing ... */ 1776 /* check whether both sides are closing ... */
1770 if (!tty_closing || (o_tty && !o_tty_closing)) { 1777 if (!tty_closing || (o_tty && !o_tty_closing)) {
1771 tty_unlock(); 1778 tty_unlock_pair(tty, o_tty);
1772 return 0; 1779 return 0;
1773 } 1780 }
1774 1781
@@ -1781,14 +1788,16 @@ int tty_release(struct inode *inode, struct file *filp)
1781 tty_ldisc_release(tty, o_tty); 1788 tty_ldisc_release(tty, o_tty);
1782 /* 1789 /*
1783 * The release_tty function takes care of the details of clearing 1790 * The release_tty function takes care of the details of clearing
1784 * the slots and preserving the termios structure. 1791 * the slots and preserving the termios structure. The tty_unlock_pair
1792 * should be safe as we keep a kref while the tty is locked (so the
1793 * unlock never unlocks a freed tty).
1785 */ 1794 */
1786 release_tty(tty, idx); 1795 release_tty(tty, idx);
1796 tty_unlock_pair(tty, o_tty);
1787 1797
1788 /* Make this pty number available for reallocation */ 1798 /* Make this pty number available for reallocation */
1789 if (devpts) 1799 if (devpts)
1790 devpts_kill_index(inode, idx); 1800 devpts_kill_index(inode, idx);
1791 tty_unlock();
1792 return 0; 1801 return 0;
1793} 1802}
1794 1803
@@ -1800,6 +1809,9 @@ int tty_release(struct inode *inode, struct file *filp)
1800 * 1809 *
1801 * We cannot return driver and index like for the other nodes because 1810 * We cannot return driver and index like for the other nodes because
1802 * devpts will not work then. It expects inodes to be from devpts FS. 1811 * devpts will not work then. It expects inodes to be from devpts FS.
1812 *
1813 * We need to move to returning a refcounted object from all the lookup
1814 * paths including this one.
1803 */ 1815 */
1804static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp) 1816static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp)
1805{ 1817{
@@ -1816,6 +1828,7 @@ static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp)
1816 /* noctty = 1; */ 1828 /* noctty = 1; */
1817 tty_kref_put(tty); 1829 tty_kref_put(tty);
1818 /* FIXME: we put a reference and return a TTY! */ 1830 /* FIXME: we put a reference and return a TTY! */
1831 /* This is only safe because the caller holds tty_mutex */
1819 return tty; 1832 return tty;
1820} 1833}
1821 1834
@@ -1888,6 +1901,9 @@ static struct tty_driver *tty_lookup_driver(dev_t device, struct file *filp,
1888 * Locking: tty_mutex protects tty, tty_lookup_driver and tty_init_dev. 1901 * Locking: tty_mutex protects tty, tty_lookup_driver and tty_init_dev.
1889 * tty->count should protect the rest. 1902 * tty->count should protect the rest.
1890 * ->siglock protects ->signal/->sighand 1903 * ->siglock protects ->signal/->sighand
1904 *
1905 * Note: the tty_unlock/lock cases without a ref are only safe due to
1906 * tty_mutex
1891 */ 1907 */
1892 1908
1893static int tty_open(struct inode *inode, struct file *filp) 1909static int tty_open(struct inode *inode, struct file *filp)
@@ -1911,8 +1927,7 @@ retry_open:
1911 retval = 0; 1927 retval = 0;
1912 1928
1913 mutex_lock(&tty_mutex); 1929 mutex_lock(&tty_mutex);
1914 tty_lock(); 1930 /* This is protected by the tty_mutex */
1915
1916 tty = tty_open_current_tty(device, filp); 1931 tty = tty_open_current_tty(device, filp);
1917 if (IS_ERR(tty)) { 1932 if (IS_ERR(tty)) {
1918 retval = PTR_ERR(tty); 1933 retval = PTR_ERR(tty);
@@ -1933,17 +1948,19 @@ retry_open:
1933 } 1948 }
1934 1949
1935 if (tty) { 1950 if (tty) {
1951 tty_lock(tty);
1936 retval = tty_reopen(tty); 1952 retval = tty_reopen(tty);
1937 if (retval) 1953 if (retval < 0) {
1954 tty_unlock(tty);
1938 tty = ERR_PTR(retval); 1955 tty = ERR_PTR(retval);
1939 } else 1956 }
1957 } else /* Returns with the tty_lock held for now */
1940 tty = tty_init_dev(driver, index); 1958 tty = tty_init_dev(driver, index);
1941 1959
1942 mutex_unlock(&tty_mutex); 1960 mutex_unlock(&tty_mutex);
1943 if (driver) 1961 if (driver)
1944 tty_driver_kref_put(driver); 1962 tty_driver_kref_put(driver);
1945 if (IS_ERR(tty)) { 1963 if (IS_ERR(tty)) {
1946 tty_unlock();
1947 retval = PTR_ERR(tty); 1964 retval = PTR_ERR(tty);
1948 goto err_file; 1965 goto err_file;
1949 } 1966 }
@@ -1972,7 +1989,7 @@ retry_open:
1972 printk(KERN_DEBUG "%s: error %d in opening %s...\n", __func__, 1989 printk(KERN_DEBUG "%s: error %d in opening %s...\n", __func__,
1973 retval, tty->name); 1990 retval, tty->name);
1974#endif 1991#endif
1975 tty_unlock(); /* need to call tty_release without BTM */ 1992 tty_unlock(tty); /* need to call tty_release without BTM */
1976 tty_release(inode, filp); 1993 tty_release(inode, filp);
1977 if (retval != -ERESTARTSYS) 1994 if (retval != -ERESTARTSYS)
1978 return retval; 1995 return retval;
@@ -1984,17 +2001,15 @@ retry_open:
1984 /* 2001 /*
1985 * Need to reset f_op in case a hangup happened. 2002 * Need to reset f_op in case a hangup happened.
1986 */ 2003 */
1987 tty_lock();
1988 if (filp->f_op == &hung_up_tty_fops) 2004 if (filp->f_op == &hung_up_tty_fops)
1989 filp->f_op = &tty_fops; 2005 filp->f_op = &tty_fops;
1990 tty_unlock();
1991 goto retry_open; 2006 goto retry_open;
1992 } 2007 }
1993 tty_unlock(); 2008 tty_unlock(tty);
1994 2009
1995 2010
1996 mutex_lock(&tty_mutex); 2011 mutex_lock(&tty_mutex);
1997 tty_lock(); 2012 tty_lock(tty);
1998 spin_lock_irq(&current->sighand->siglock); 2013 spin_lock_irq(&current->sighand->siglock);
1999 if (!noctty && 2014 if (!noctty &&
2000 current->signal->leader && 2015 current->signal->leader &&
@@ -2002,11 +2017,10 @@ retry_open:
2002 tty->session == NULL) 2017 tty->session == NULL)
2003 __proc_set_tty(current, tty); 2018 __proc_set_tty(current, tty);
2004 spin_unlock_irq(&current->sighand->siglock); 2019 spin_unlock_irq(&current->sighand->siglock);
2005 tty_unlock(); 2020 tty_unlock(tty);
2006 mutex_unlock(&tty_mutex); 2021 mutex_unlock(&tty_mutex);
2007 return 0; 2022 return 0;
2008err_unlock: 2023err_unlock:
2009 tty_unlock();
2010 mutex_unlock(&tty_mutex); 2024 mutex_unlock(&tty_mutex);
2011 /* after locks to avoid deadlock */ 2025 /* after locks to avoid deadlock */
2012 if (!IS_ERR_OR_NULL(driver)) 2026 if (!IS_ERR_OR_NULL(driver))
@@ -2089,10 +2103,13 @@ out:
2089 2103
2090static int tty_fasync(int fd, struct file *filp, int on) 2104static int tty_fasync(int fd, struct file *filp, int on)
2091{ 2105{
2106 struct tty_struct *tty = file_tty(filp);
2092 int retval; 2107 int retval;
2093 tty_lock(); 2108
2109 tty_lock(tty);
2094 retval = __tty_fasync(fd, filp, on); 2110 retval = __tty_fasync(fd, filp, on);
2095 tty_unlock(); 2111 tty_unlock(tty);
2112
2096 return retval; 2113 return retval;
2097} 2114}
2098 2115
@@ -2929,6 +2946,7 @@ void initialize_tty_struct(struct tty_struct *tty,
2929 tty->pgrp = NULL; 2946 tty->pgrp = NULL;
2930 tty->overrun_time = jiffies; 2947 tty->overrun_time = jiffies;
2931 tty_buffer_init(tty); 2948 tty_buffer_init(tty);
2949 mutex_init(&tty->legacy_mutex);
2932 mutex_init(&tty->termios_mutex); 2950 mutex_init(&tty->termios_mutex);
2933 mutex_init(&tty->ldisc_mutex); 2951 mutex_init(&tty->ldisc_mutex);
2934 init_waitqueue_head(&tty->write_wait); 2952 init_waitqueue_head(&tty->write_wait);
diff --git a/drivers/tty/tty_ldisc.c b/drivers/tty/tty_ldisc.c
index 24b95db75d84..173a9000a6cb 100644
--- a/drivers/tty/tty_ldisc.c
+++ b/drivers/tty/tty_ldisc.c
@@ -28,7 +28,6 @@
28 28
29static DEFINE_SPINLOCK(tty_ldisc_lock); 29static DEFINE_SPINLOCK(tty_ldisc_lock);
30static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait); 30static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
31static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_idle);
32/* Line disc dispatch table */ 31/* Line disc dispatch table */
33static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS]; 32static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
34 33
@@ -65,7 +64,7 @@ static void put_ldisc(struct tty_ldisc *ld)
65 return; 64 return;
66 } 65 }
67 local_irq_restore(flags); 66 local_irq_restore(flags);
68 wake_up(&tty_ldisc_idle); 67 wake_up(&ld->wq_idle);
69} 68}
70 69
71/** 70/**
@@ -200,6 +199,8 @@ static struct tty_ldisc *tty_ldisc_get(int disc)
200 199
201 ld->ops = ldops; 200 ld->ops = ldops;
202 atomic_set(&ld->users, 1); 201 atomic_set(&ld->users, 1);
202 init_waitqueue_head(&ld->wq_idle);
203
203 return ld; 204 return ld;
204} 205}
205 206
@@ -538,7 +539,7 @@ static void tty_ldisc_flush_works(struct tty_struct *tty)
538static int tty_ldisc_wait_idle(struct tty_struct *tty, long timeout) 539static int tty_ldisc_wait_idle(struct tty_struct *tty, long timeout)
539{ 540{
540 long ret; 541 long ret;
541 ret = wait_event_timeout(tty_ldisc_idle, 542 ret = wait_event_timeout(tty->ldisc->wq_idle,
542 atomic_read(&tty->ldisc->users) == 1, timeout); 543 atomic_read(&tty->ldisc->users) == 1, timeout);
543 return ret > 0 ? 0 : -EBUSY; 544 return ret > 0 ? 0 : -EBUSY;
544} 545}
@@ -567,7 +568,7 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
567 if (IS_ERR(new_ldisc)) 568 if (IS_ERR(new_ldisc))
568 return PTR_ERR(new_ldisc); 569 return PTR_ERR(new_ldisc);
569 570
570 tty_lock(); 571 tty_lock(tty);
571 /* 572 /*
572 * We need to look at the tty locking here for pty/tty pairs 573 * We need to look at the tty locking here for pty/tty pairs
573 * when both sides try to change in parallel. 574 * when both sides try to change in parallel.
@@ -581,12 +582,12 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
581 */ 582 */
582 583
583 if (tty->ldisc->ops->num == ldisc) { 584 if (tty->ldisc->ops->num == ldisc) {
584 tty_unlock(); 585 tty_unlock(tty);
585 tty_ldisc_put(new_ldisc); 586 tty_ldisc_put(new_ldisc);
586 return 0; 587 return 0;
587 } 588 }
588 589
589 tty_unlock(); 590 tty_unlock(tty);
590 /* 591 /*
591 * Problem: What do we do if this blocks ? 592 * Problem: What do we do if this blocks ?
592 * We could deadlock here 593 * We could deadlock here
@@ -594,7 +595,7 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
594 595
595 tty_wait_until_sent(tty, 0); 596 tty_wait_until_sent(tty, 0);
596 597
597 tty_lock(); 598 tty_lock(tty);
598 mutex_lock(&tty->ldisc_mutex); 599 mutex_lock(&tty->ldisc_mutex);
599 600
600 /* 601 /*
@@ -604,10 +605,10 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
604 605
605 while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) { 606 while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
606 mutex_unlock(&tty->ldisc_mutex); 607 mutex_unlock(&tty->ldisc_mutex);
607 tty_unlock(); 608 tty_unlock(tty);
608 wait_event(tty_ldisc_wait, 609 wait_event(tty_ldisc_wait,
609 test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0); 610 test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
610 tty_lock(); 611 tty_lock(tty);
611 mutex_lock(&tty->ldisc_mutex); 612 mutex_lock(&tty->ldisc_mutex);
612 } 613 }
613 614
@@ -622,7 +623,7 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
622 623
623 o_ldisc = tty->ldisc; 624 o_ldisc = tty->ldisc;
624 625
625 tty_unlock(); 626 tty_unlock(tty);
626 /* 627 /*
627 * Make sure we don't change while someone holds a 628 * Make sure we don't change while someone holds a
628 * reference to the line discipline. The TTY_LDISC bit 629 * reference to the line discipline. The TTY_LDISC bit
@@ -649,7 +650,7 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
649 650
650 retval = tty_ldisc_wait_idle(tty, 5 * HZ); 651 retval = tty_ldisc_wait_idle(tty, 5 * HZ);
651 652
652 tty_lock(); 653 tty_lock(tty);
653 mutex_lock(&tty->ldisc_mutex); 654 mutex_lock(&tty->ldisc_mutex);
654 655
655 /* handle wait idle failure locked */ 656 /* handle wait idle failure locked */
@@ -664,7 +665,7 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
664 clear_bit(TTY_LDISC_CHANGING, &tty->flags); 665 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
665 mutex_unlock(&tty->ldisc_mutex); 666 mutex_unlock(&tty->ldisc_mutex);
666 tty_ldisc_put(new_ldisc); 667 tty_ldisc_put(new_ldisc);
667 tty_unlock(); 668 tty_unlock(tty);
668 return -EIO; 669 return -EIO;
669 } 670 }
670 671
@@ -707,7 +708,7 @@ enable:
707 if (o_work) 708 if (o_work)
708 schedule_work(&o_tty->buf.work); 709 schedule_work(&o_tty->buf.work);
709 mutex_unlock(&tty->ldisc_mutex); 710 mutex_unlock(&tty->ldisc_mutex);
710 tty_unlock(); 711 tty_unlock(tty);
711 return retval; 712 return retval;
712} 713}
713 714
@@ -815,11 +816,11 @@ void tty_ldisc_hangup(struct tty_struct *tty)
815 * need to wait for another function taking the BTM 816 * need to wait for another function taking the BTM
816 */ 817 */
817 clear_bit(TTY_LDISC, &tty->flags); 818 clear_bit(TTY_LDISC, &tty->flags);
818 tty_unlock(); 819 tty_unlock(tty);
819 cancel_work_sync(&tty->buf.work); 820 cancel_work_sync(&tty->buf.work);
820 mutex_unlock(&tty->ldisc_mutex); 821 mutex_unlock(&tty->ldisc_mutex);
821retry: 822retry:
822 tty_lock(); 823 tty_lock(tty);
823 mutex_lock(&tty->ldisc_mutex); 824 mutex_lock(&tty->ldisc_mutex);
824 825
825 /* At this point we have a closed ldisc and we want to 826 /* At this point we have a closed ldisc and we want to
@@ -830,7 +831,7 @@ retry:
830 if (atomic_read(&tty->ldisc->users) != 1) { 831 if (atomic_read(&tty->ldisc->users) != 1) {
831 char cur_n[TASK_COMM_LEN], tty_n[64]; 832 char cur_n[TASK_COMM_LEN], tty_n[64];
832 long timeout = 3 * HZ; 833 long timeout = 3 * HZ;
833 tty_unlock(); 834 tty_unlock(tty);
834 835
835 while (tty_ldisc_wait_idle(tty, timeout) == -EBUSY) { 836 while (tty_ldisc_wait_idle(tty, timeout) == -EBUSY) {
836 timeout = MAX_SCHEDULE_TIMEOUT; 837 timeout = MAX_SCHEDULE_TIMEOUT;
@@ -911,10 +912,10 @@ void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
911 * race with the set_ldisc code path. 912 * race with the set_ldisc code path.
912 */ 913 */
913 914
914 tty_unlock(); 915 tty_unlock(tty);
915 tty_ldisc_halt(tty); 916 tty_ldisc_halt(tty);
916 tty_ldisc_flush_works(tty); 917 tty_ldisc_flush_works(tty);
917 tty_lock(); 918 tty_lock(tty);
918 919
919 mutex_lock(&tty->ldisc_mutex); 920 mutex_lock(&tty->ldisc_mutex);
920 /* 921 /*
diff --git a/drivers/tty/tty_mutex.c b/drivers/tty/tty_mutex.c
index 9ff986c32a21..69adc80c98cd 100644
--- a/drivers/tty/tty_mutex.c
+++ b/drivers/tty/tty_mutex.c
@@ -4,29 +4,59 @@
4#include <linux/semaphore.h> 4#include <linux/semaphore.h>
5#include <linux/sched.h> 5#include <linux/sched.h>
6 6
7/* 7/* Legacy tty mutex glue */
8 * The 'big tty mutex'
9 *
10 * This mutex is taken and released by tty_lock() and tty_unlock(),
11 * replacing the older big kernel lock.
12 * It can no longer be taken recursively, and does not get
13 * released implicitly while sleeping.
14 *
15 * Don't use in new code.
16 */
17static DEFINE_MUTEX(big_tty_mutex);
18 8
19/* 9/*
20 * Getting the big tty mutex. 10 * Getting the big tty mutex.
21 */ 11 */
22void __lockfunc tty_lock(void) 12
13void __lockfunc tty_lock(struct tty_struct *tty)
23{ 14{
24 mutex_lock(&big_tty_mutex); 15 if (tty->magic != TTY_MAGIC) {
16 printk(KERN_ERR "L Bad %p\n", tty);
17 WARN_ON(1);
18 return;
19 }
20 tty_kref_get(tty);
21 mutex_lock(&tty->legacy_mutex);
25} 22}
26EXPORT_SYMBOL(tty_lock); 23EXPORT_SYMBOL(tty_lock);
27 24
28void __lockfunc tty_unlock(void) 25void __lockfunc tty_unlock(struct tty_struct *tty)
29{ 26{
30 mutex_unlock(&big_tty_mutex); 27 if (tty->magic != TTY_MAGIC) {
28 printk(KERN_ERR "U Bad %p\n", tty);
29 WARN_ON(1);
30 return;
31 }
32 mutex_unlock(&tty->legacy_mutex);
33 tty_kref_put(tty);
31} 34}
32EXPORT_SYMBOL(tty_unlock); 35EXPORT_SYMBOL(tty_unlock);
36
37/*
38 * Getting the big tty mutex for a pair of ttys with lock ordering
39 * On a non pty/tty pair tty2 can be NULL which is just fine.
40 */
41void __lockfunc tty_lock_pair(struct tty_struct *tty,
42 struct tty_struct *tty2)
43{
44 if (tty < tty2) {
45 tty_lock(tty);
46 tty_lock(tty2);
47 } else {
48 if (tty2 && tty2 != tty)
49 tty_lock(tty2);
50 tty_lock(tty);
51 }
52}
53EXPORT_SYMBOL(tty_lock_pair);
54
55void __lockfunc tty_unlock_pair(struct tty_struct *tty,
56 struct tty_struct *tty2)
57{
58 tty_unlock(tty);
59 if (tty2 && tty2 != tty)
60 tty_unlock(tty2);
61}
62EXPORT_SYMBOL(tty_unlock_pair);
diff --git a/drivers/tty/tty_port.c b/drivers/tty/tty_port.c
index bf6e238146ae..d9cca95a5452 100644
--- a/drivers/tty/tty_port.c
+++ b/drivers/tty/tty_port.c
@@ -230,7 +230,7 @@ int tty_port_block_til_ready(struct tty_port *port,
230 230
231 /* block if port is in the process of being closed */ 231 /* block if port is in the process of being closed */
232 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) { 232 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
233 wait_event_interruptible_tty(port->close_wait, 233 wait_event_interruptible_tty(tty, port->close_wait,
234 !(port->flags & ASYNC_CLOSING)); 234 !(port->flags & ASYNC_CLOSING));
235 if (port->flags & ASYNC_HUP_NOTIFY) 235 if (port->flags & ASYNC_HUP_NOTIFY)
236 return -EAGAIN; 236 return -EAGAIN;
@@ -296,9 +296,9 @@ int tty_port_block_til_ready(struct tty_port *port,
296 retval = -ERESTARTSYS; 296 retval = -ERESTARTSYS;
297 break; 297 break;
298 } 298 }
299 tty_unlock(); 299 tty_unlock(tty);
300 schedule(); 300 schedule();
301 tty_lock(); 301 tty_lock(tty);
302 } 302 }
303 finish_wait(&port->open_wait, &wait); 303 finish_wait(&port->open_wait, &wait);
304 304
diff --git a/drivers/tty/vt/consolemap.c b/drivers/tty/vt/consolemap.c
index 8308fc7cdc26..2aaa0c228409 100644
--- a/drivers/tty/vt/consolemap.c
+++ b/drivers/tty/vt/consolemap.c
@@ -19,6 +19,7 @@
19#include <linux/init.h> 19#include <linux/init.h>
20#include <linux/tty.h> 20#include <linux/tty.h>
21#include <asm/uaccess.h> 21#include <asm/uaccess.h>
22#include <linux/console.h>
22#include <linux/consolemap.h> 23#include <linux/consolemap.h>
23#include <linux/vt_kern.h> 24#include <linux/vt_kern.h>
24 25
@@ -312,6 +313,7 @@ int con_set_trans_old(unsigned char __user * arg)
312 if (!access_ok(VERIFY_READ, arg, E_TABSZ)) 313 if (!access_ok(VERIFY_READ, arg, E_TABSZ))
313 return -EFAULT; 314 return -EFAULT;
314 315
316 console_lock();
315 for (i=0; i<E_TABSZ ; i++) { 317 for (i=0; i<E_TABSZ ; i++) {
316 unsigned char uc; 318 unsigned char uc;
317 __get_user(uc, arg+i); 319 __get_user(uc, arg+i);
@@ -319,6 +321,7 @@ int con_set_trans_old(unsigned char __user * arg)
319 } 321 }
320 322
321 update_user_maps(); 323 update_user_maps();
324 console_unlock();
322 return 0; 325 return 0;
323} 326}
324 327
@@ -330,11 +333,13 @@ int con_get_trans_old(unsigned char __user * arg)
330 if (!access_ok(VERIFY_WRITE, arg, E_TABSZ)) 333 if (!access_ok(VERIFY_WRITE, arg, E_TABSZ))
331 return -EFAULT; 334 return -EFAULT;
332 335
336 console_lock();
333 for (i=0; i<E_TABSZ ; i++) 337 for (i=0; i<E_TABSZ ; i++)
334 { 338 {
335 ch = conv_uni_to_pc(vc_cons[fg_console].d, p[i]); 339 ch = conv_uni_to_pc(vc_cons[fg_console].d, p[i]);
336 __put_user((ch & ~0xff) ? 0 : ch, arg+i); 340 __put_user((ch & ~0xff) ? 0 : ch, arg+i);
337 } 341 }
342 console_unlock();
338 return 0; 343 return 0;
339} 344}
340 345
@@ -346,6 +351,7 @@ int con_set_trans_new(ushort __user * arg)
346 if (!access_ok(VERIFY_READ, arg, E_TABSZ*sizeof(unsigned short))) 351 if (!access_ok(VERIFY_READ, arg, E_TABSZ*sizeof(unsigned short)))
347 return -EFAULT; 352 return -EFAULT;
348 353
354 console_lock();
349 for (i=0; i<E_TABSZ ; i++) { 355 for (i=0; i<E_TABSZ ; i++) {
350 unsigned short us; 356 unsigned short us;
351 __get_user(us, arg+i); 357 __get_user(us, arg+i);
@@ -353,6 +359,7 @@ int con_set_trans_new(ushort __user * arg)
353 } 359 }
354 360
355 update_user_maps(); 361 update_user_maps();
362 console_unlock();
356 return 0; 363 return 0;
357} 364}
358 365
@@ -364,8 +371,10 @@ int con_get_trans_new(ushort __user * arg)
364 if (!access_ok(VERIFY_WRITE, arg, E_TABSZ*sizeof(unsigned short))) 371 if (!access_ok(VERIFY_WRITE, arg, E_TABSZ*sizeof(unsigned short)))
365 return -EFAULT; 372 return -EFAULT;
366 373
374 console_lock();
367 for (i=0; i<E_TABSZ ; i++) 375 for (i=0; i<E_TABSZ ; i++)
368 __put_user(p[i], arg+i); 376 __put_user(p[i], arg+i);
377 console_unlock();
369 378
370 return 0; 379 return 0;
371} 380}
@@ -407,6 +416,7 @@ static void con_release_unimap(struct uni_pagedir *p)
407 } 416 }
408} 417}
409 418
419/* Caller must hold the console lock */
410void con_free_unimap(struct vc_data *vc) 420void con_free_unimap(struct vc_data *vc)
411{ 421{
412 struct uni_pagedir *p; 422 struct uni_pagedir *p;
@@ -487,17 +497,21 @@ con_insert_unipair(struct uni_pagedir *p, u_short unicode, u_short fontpos)
487 return 0; 497 return 0;
488} 498}
489 499
490/* ui is a leftover from using a hashtable, but might be used again */ 500/* ui is a leftover from using a hashtable, but might be used again
491int con_clear_unimap(struct vc_data *vc, struct unimapinit *ui) 501 Caller must hold the lock */
502static int con_do_clear_unimap(struct vc_data *vc, struct unimapinit *ui)
492{ 503{
493 struct uni_pagedir *p, *q; 504 struct uni_pagedir *p, *q;
494 505
495 p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc; 506 p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc;
496 if (p && p->readonly) return -EIO; 507 if (p && p->readonly)
508 return -EIO;
509
497 if (!p || --p->refcount) { 510 if (!p || --p->refcount) {
498 q = kzalloc(sizeof(*p), GFP_KERNEL); 511 q = kzalloc(sizeof(*p), GFP_KERNEL);
499 if (!q) { 512 if (!q) {
500 if (p) p->refcount++; 513 if (p)
514 p->refcount++;
501 return -ENOMEM; 515 return -ENOMEM;
502 } 516 }
503 q->refcount=1; 517 q->refcount=1;
@@ -511,23 +525,43 @@ int con_clear_unimap(struct vc_data *vc, struct unimapinit *ui)
511 return 0; 525 return 0;
512} 526}
513 527
528int con_clear_unimap(struct vc_data *vc, struct unimapinit *ui)
529{
530 int ret;
531 console_lock();
532 ret = con_do_clear_unimap(vc, ui);
533 console_unlock();
534 return ret;
535}
536
514int con_set_unimap(struct vc_data *vc, ushort ct, struct unipair __user *list) 537int con_set_unimap(struct vc_data *vc, ushort ct, struct unipair __user *list)
515{ 538{
516 int err = 0, err1, i; 539 int err = 0, err1, i;
517 struct uni_pagedir *p, *q; 540 struct uni_pagedir *p, *q;
518 541
542 console_lock();
543
519 /* Save original vc_unipagdir_loc in case we allocate a new one */ 544 /* Save original vc_unipagdir_loc in case we allocate a new one */
520 p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc; 545 p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc;
521 if (p->readonly) return -EIO; 546 if (p->readonly) {
547 console_unlock();
548 return -EIO;
549 }
522 550
523 if (!ct) return 0; 551 if (!ct) {
552 console_unlock();
553 return 0;
554 }
524 555
525 if (p->refcount > 1) { 556 if (p->refcount > 1) {
526 int j, k; 557 int j, k;
527 u16 **p1, *p2, l; 558 u16 **p1, *p2, l;
528 559
529 err1 = con_clear_unimap(vc, NULL); 560 err1 = con_do_clear_unimap(vc, NULL);
530 if (err1) return err1; 561 if (err1) {
562 console_unlock();
563 return err1;
564 }
531 565
532 /* 566 /*
533 * Since refcount was > 1, con_clear_unimap() allocated a 567 * Since refcount was > 1, con_clear_unimap() allocated a
@@ -558,7 +592,8 @@ int con_set_unimap(struct vc_data *vc, ushort ct, struct unipair __user *list)
558 *vc->vc_uni_pagedir_loc = (unsigned long)p; 592 *vc->vc_uni_pagedir_loc = (unsigned long)p;
559 con_release_unimap(q); 593 con_release_unimap(q);
560 kfree(q); 594 kfree(q);
561 return err1; 595 console_unlock();
596 return err1;
562 } 597 }
563 } 598 }
564 } else { 599 } else {
@@ -592,21 +627,30 @@ int con_set_unimap(struct vc_data *vc, ushort ct, struct unipair __user *list)
592 /* 627 /*
593 * Merge with fontmaps of any other virtual consoles. 628 * Merge with fontmaps of any other virtual consoles.
594 */ 629 */
595 if (con_unify_unimap(vc, p)) 630 if (con_unify_unimap(vc, p)) {
631 console_unlock();
596 return err; 632 return err;
633 }
597 634
598 for (i = 0; i <= 3; i++) 635 for (i = 0; i <= 3; i++)
599 set_inverse_transl(vc, p, i); /* Update inverse translations */ 636 set_inverse_transl(vc, p, i); /* Update inverse translations */
600 set_inverse_trans_unicode(vc, p); 637 set_inverse_trans_unicode(vc, p);
601 638
639 console_unlock();
602 return err; 640 return err;
603} 641}
604 642
605/* Loads the unimap for the hardware font, as defined in uni_hash.tbl. 643/**
606 The representation used was the most compact I could come up 644 * con_set_default_unimap - set default unicode map
607 with. This routine is executed at sys_setup time, and when the 645 * @vc: the console we are updating
608 PIO_FONTRESET ioctl is called. */ 646 *
609 647 * Loads the unimap for the hardware font, as defined in uni_hash.tbl.
648 * The representation used was the most compact I could come up
649 * with. This routine is executed at video setup, and when the
650 * PIO_FONTRESET ioctl is called.
651 *
652 * The caller must hold the console lock
653 */
610int con_set_default_unimap(struct vc_data *vc) 654int con_set_default_unimap(struct vc_data *vc)
611{ 655{
612 int i, j, err = 0, err1; 656 int i, j, err = 0, err1;
@@ -617,6 +661,7 @@ int con_set_default_unimap(struct vc_data *vc)
617 p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc; 661 p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc;
618 if (p == dflt) 662 if (p == dflt)
619 return 0; 663 return 0;
664
620 dflt->refcount++; 665 dflt->refcount++;
621 *vc->vc_uni_pagedir_loc = (unsigned long)dflt; 666 *vc->vc_uni_pagedir_loc = (unsigned long)dflt;
622 if (p && !--p->refcount) { 667 if (p && !--p->refcount) {
@@ -628,8 +673,9 @@ int con_set_default_unimap(struct vc_data *vc)
628 673
629 /* The default font is always 256 characters */ 674 /* The default font is always 256 characters */
630 675
631 err = con_clear_unimap(vc, NULL); 676 err = con_do_clear_unimap(vc, NULL);
632 if (err) return err; 677 if (err)
678 return err;
633 679
634 p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc; 680 p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc;
635 q = dfont_unitable; 681 q = dfont_unitable;
@@ -654,6 +700,13 @@ int con_set_default_unimap(struct vc_data *vc)
654} 700}
655EXPORT_SYMBOL(con_set_default_unimap); 701EXPORT_SYMBOL(con_set_default_unimap);
656 702
703/**
704 * con_copy_unimap - copy unimap between two vts
705 * @dst_vc: target
706 * @src_vt: source
707 *
708 * The caller must hold the console lock when invoking this method
709 */
657int con_copy_unimap(struct vc_data *dst_vc, struct vc_data *src_vc) 710int con_copy_unimap(struct vc_data *dst_vc, struct vc_data *src_vc)
658{ 711{
659 struct uni_pagedir *q; 712 struct uni_pagedir *q;
@@ -668,13 +721,23 @@ int con_copy_unimap(struct vc_data *dst_vc, struct vc_data *src_vc)
668 *dst_vc->vc_uni_pagedir_loc = (long)q; 721 *dst_vc->vc_uni_pagedir_loc = (long)q;
669 return 0; 722 return 0;
670} 723}
724EXPORT_SYMBOL(con_copy_unimap);
671 725
726/**
727 * con_get_unimap - get the unicode map
728 * @vc: the console to read from
729 *
730 * Read the console unicode data for this console. Called from the ioctl
731 * handlers.
732 */
672int con_get_unimap(struct vc_data *vc, ushort ct, ushort __user *uct, struct unipair __user *list) 733int con_get_unimap(struct vc_data *vc, ushort ct, ushort __user *uct, struct unipair __user *list)
673{ 734{
674 int i, j, k, ect; 735 int i, j, k, ect;
675 u16 **p1, *p2; 736 u16 **p1, *p2;
676 struct uni_pagedir *p; 737 struct uni_pagedir *p;
677 738
739 console_lock();
740
678 ect = 0; 741 ect = 0;
679 if (*vc->vc_uni_pagedir_loc) { 742 if (*vc->vc_uni_pagedir_loc) {
680 p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc; 743 p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc;
@@ -694,22 +757,19 @@ int con_get_unimap(struct vc_data *vc, ushort ct, ushort __user *uct, struct uni
694 } 757 }
695 } 758 }
696 __put_user(ect, uct); 759 __put_user(ect, uct);
760 console_unlock();
697 return ((ect <= ct) ? 0 : -ENOMEM); 761 return ((ect <= ct) ? 0 : -ENOMEM);
698} 762}
699 763
700void con_protect_unimap(struct vc_data *vc, int rdonly)
701{
702 struct uni_pagedir *p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc;
703
704 if (p)
705 p->readonly = rdonly;
706}
707
708/* 764/*
709 * Always use USER_MAP. These functions are used by the keyboard, 765 * Always use USER_MAP. These functions are used by the keyboard,
710 * which shouldn't be affected by G0/G1 switching, etc. 766 * which shouldn't be affected by G0/G1 switching, etc.
711 * If the user map still contains default values, i.e. the 767 * If the user map still contains default values, i.e. the
712 * direct-to-font mapping, then assume user is using Latin1. 768 * direct-to-font mapping, then assume user is using Latin1.
769 *
770 * FIXME: at some point we need to decide if we want to lock the table
771 * update element itself via the keyboard_event_lock for consistency with the
772 * keyboard driver as well as the consoles
713 */ 773 */
714/* may be called during an interrupt */ 774/* may be called during an interrupt */
715u32 conv_8bit_to_uni(unsigned char c) 775u32 conv_8bit_to_uni(unsigned char c)
@@ -777,4 +837,3 @@ console_map_init(void)
777 con_set_default_unimap(vc_cons[i].d); 837 con_set_default_unimap(vc_cons[i].d);
778} 838}
779 839
780EXPORT_SYMBOL(con_copy_unimap);
diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c
index 2156188db4a6..84cbf298c094 100644
--- a/drivers/tty/vt/vt.c
+++ b/drivers/tty/vt/vt.c
@@ -3892,36 +3892,6 @@ static void set_palette(struct vc_data *vc)
3892 vc->vc_sw->con_set_palette(vc, color_table); 3892 vc->vc_sw->con_set_palette(vc, color_table);
3893} 3893}
3894 3894
3895static int set_get_cmap(unsigned char __user *arg, int set)
3896{
3897 int i, j, k;
3898
3899 WARN_CONSOLE_UNLOCKED();
3900
3901 for (i = 0; i < 16; i++)
3902 if (set) {
3903 get_user(default_red[i], arg++);
3904 get_user(default_grn[i], arg++);
3905 get_user(default_blu[i], arg++);
3906 } else {
3907 put_user(default_red[i], arg++);
3908 put_user(default_grn[i], arg++);
3909 put_user(default_blu[i], arg++);
3910 }
3911 if (set) {
3912 for (i = 0; i < MAX_NR_CONSOLES; i++)
3913 if (vc_cons_allocated(i)) {
3914 for (j = k = 0; j < 16; j++) {
3915 vc_cons[i].d->vc_palette[k++] = default_red[j];
3916 vc_cons[i].d->vc_palette[k++] = default_grn[j];
3917 vc_cons[i].d->vc_palette[k++] = default_blu[j];
3918 }
3919 set_palette(vc_cons[i].d);
3920 }
3921 }
3922 return 0;
3923}
3924
3925/* 3895/*
3926 * Load palette into the DAC registers. arg points to a colour 3896 * Load palette into the DAC registers. arg points to a colour
3927 * map, 3 bytes per colour, 16 colours, range from 0 to 255. 3897 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
@@ -3929,24 +3899,50 @@ static int set_get_cmap(unsigned char __user *arg, int set)
3929 3899
3930int con_set_cmap(unsigned char __user *arg) 3900int con_set_cmap(unsigned char __user *arg)
3931{ 3901{
3932 int rc; 3902 int i, j, k;
3903 unsigned char colormap[3*16];
3904
3905 if (copy_from_user(colormap, arg, sizeof(colormap)))
3906 return -EFAULT;
3933 3907
3934 console_lock(); 3908 console_lock();
3935 rc = set_get_cmap (arg,1); 3909 for (i = k = 0; i < 16; i++) {
3910 default_red[i] = colormap[k++];
3911 default_grn[i] = colormap[k++];
3912 default_blu[i] = colormap[k++];
3913 }
3914 for (i = 0; i < MAX_NR_CONSOLES; i++) {
3915 if (!vc_cons_allocated(i))
3916 continue;
3917 for (j = k = 0; j < 16; j++) {
3918 vc_cons[i].d->vc_palette[k++] = default_red[j];
3919 vc_cons[i].d->vc_palette[k++] = default_grn[j];
3920 vc_cons[i].d->vc_palette[k++] = default_blu[j];
3921 }
3922 set_palette(vc_cons[i].d);
3923 }
3936 console_unlock(); 3924 console_unlock();
3937 3925
3938 return rc; 3926 return 0;
3939} 3927}
3940 3928
3941int con_get_cmap(unsigned char __user *arg) 3929int con_get_cmap(unsigned char __user *arg)
3942{ 3930{
3943 int rc; 3931 int i, k;
3932 unsigned char colormap[3*16];
3944 3933
3945 console_lock(); 3934 console_lock();
3946 rc = set_get_cmap (arg,0); 3935 for (i = k = 0; i < 16; i++) {
3936 colormap[k++] = default_red[i];
3937 colormap[k++] = default_grn[i];
3938 colormap[k++] = default_blu[i];
3939 }
3947 console_unlock(); 3940 console_unlock();
3948 3941
3949 return rc; 3942 if (copy_to_user(arg, colormap, sizeof(colormap)))
3943 return -EFAULT;
3944
3945 return 0;
3950} 3946}
3951 3947
3952void reset_palette(struct vc_data *vc) 3948void reset_palette(struct vc_data *vc)
diff --git a/drivers/tty/vt/vt_ioctl.c b/drivers/tty/vt/vt_ioctl.c
index ede2ef18d2fb..64618547be11 100644
--- a/drivers/tty/vt/vt_ioctl.c
+++ b/drivers/tty/vt/vt_ioctl.c
@@ -910,7 +910,9 @@ int vt_ioctl(struct tty_struct *tty,
910 ret = con_font_op(vc_cons[fg_console].d, &op); 910 ret = con_font_op(vc_cons[fg_console].d, &op);
911 if (ret) 911 if (ret)
912 break; 912 break;
913 console_lock();
913 con_set_default_unimap(vc_cons[fg_console].d); 914 con_set_default_unimap(vc_cons[fg_console].d);
915 console_unlock();
914 break; 916 break;
915 } 917 }
916#endif 918#endif
@@ -934,33 +936,23 @@ int vt_ioctl(struct tty_struct *tty,
934 case PIO_SCRNMAP: 936 case PIO_SCRNMAP:
935 if (!perm) 937 if (!perm)
936 ret = -EPERM; 938 ret = -EPERM;
937 else { 939 else
938 tty_lock();
939 ret = con_set_trans_old(up); 940 ret = con_set_trans_old(up);
940 tty_unlock();
941 }
942 break; 941 break;
943 942
944 case GIO_SCRNMAP: 943 case GIO_SCRNMAP:
945 tty_lock();
946 ret = con_get_trans_old(up); 944 ret = con_get_trans_old(up);
947 tty_unlock();
948 break; 945 break;
949 946
950 case PIO_UNISCRNMAP: 947 case PIO_UNISCRNMAP:
951 if (!perm) 948 if (!perm)
952 ret = -EPERM; 949 ret = -EPERM;
953 else { 950 else
954 tty_lock();
955 ret = con_set_trans_new(up); 951 ret = con_set_trans_new(up);
956 tty_unlock();
957 }
958 break; 952 break;
959 953
960 case GIO_UNISCRNMAP: 954 case GIO_UNISCRNMAP:
961 tty_lock();
962 ret = con_get_trans_new(up); 955 ret = con_get_trans_new(up);
963 tty_unlock();
964 break; 956 break;
965 957
966 case PIO_UNIMAPCLR: 958 case PIO_UNIMAPCLR:
@@ -970,19 +962,14 @@ int vt_ioctl(struct tty_struct *tty,
970 ret = copy_from_user(&ui, up, sizeof(struct unimapinit)); 962 ret = copy_from_user(&ui, up, sizeof(struct unimapinit));
971 if (ret) 963 if (ret)
972 ret = -EFAULT; 964 ret = -EFAULT;
973 else { 965 else
974 tty_lock();
975 con_clear_unimap(vc, &ui); 966 con_clear_unimap(vc, &ui);
976 tty_unlock();
977 }
978 break; 967 break;
979 } 968 }
980 969
981 case PIO_UNIMAP: 970 case PIO_UNIMAP:
982 case GIO_UNIMAP: 971 case GIO_UNIMAP:
983 tty_lock();
984 ret = do_unimap_ioctl(cmd, up, perm, vc); 972 ret = do_unimap_ioctl(cmd, up, perm, vc);
985 tty_unlock();
986 break; 973 break;
987 974
988 case VT_LOCKSWITCH: 975 case VT_LOCKSWITCH:
@@ -1196,9 +1183,7 @@ long vt_compat_ioctl(struct tty_struct *tty,
1196 1183
1197 case PIO_UNIMAP: 1184 case PIO_UNIMAP:
1198 case GIO_UNIMAP: 1185 case GIO_UNIMAP:
1199 tty_lock();
1200 ret = compat_unimap_ioctl(cmd, up, perm, vc); 1186 ret = compat_unimap_ioctl(cmd, up, perm, vc);
1201 tty_unlock();
1202 break; 1187 break;
1203 1188
1204 /* 1189 /*