aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/char
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2009-12-11 18:34:40 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2009-12-11 18:34:40 -0500
commit0f4974c439dd7826c85bae4e6a8088ce2db0f498 (patch)
treefdabc7d9bb7d7bc49aad547c0aac3a633ce01f09 /drivers/char
parent3126c136bc30225d7a43af741778aa50e95e467a (diff)
parent36ba782e9674cdc29ec7003757df0b375e99fa96 (diff)
Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty-2.6
* git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty-2.6: (58 commits) tty: split the lock up a bit further tty: Move the leader test in disassociate tty: Push the bkl down a bit in the hangup code tty: Push the lock down further into the ldisc code tty: push the BKL down into the handlers a bit tty: moxa: split open lock tty: moxa: Kill the use of lock_kernel tty: moxa: Fix modem op locking tty: moxa: Kill off the throttle method tty: moxa: Locking clean up tty: moxa: rework the locking a bit tty: moxa: Use more tty_port ops tty: isicom: fix deadlock on shutdown tty: mxser: Use the new locking rules to fix setserial properly tty: mxser: use the tty_port_open method tty: isicom: sort out the board init logic tty: isicom: switch to the new tty_port_open helper tty: tty_port: Add a kref object to the tty port tty: istallion: tty port open/close methods tty: stallion: Convert to the tty_port_open/close methods ...
Diffstat (limited to 'drivers/char')
-rw-r--r--drivers/char/Kconfig13
-rw-r--r--drivers/char/Makefile1
-rw-r--r--drivers/char/bfin_jtag_comm.c2
-rw-r--r--drivers/char/epca.c2
-rw-r--r--drivers/char/esp.c2533
-rw-r--r--drivers/char/isicom.c115
-rw-r--r--drivers/char/istallion.c185
-rw-r--r--drivers/char/moxa.c289
-rw-r--r--drivers/char/mxser.c248
-rw-r--r--drivers/char/pcmcia/ipwireless/tty.c2
-rw-r--r--drivers/char/pty.c2
-rw-r--r--drivers/char/riscom8.c89
-rw-r--r--drivers/char/stallion.c129
-rw-r--r--drivers/char/tty_io.c151
-rw-r--r--drivers/char/tty_ldisc.c23
-rw-r--r--drivers/char/tty_port.c97
16 files changed, 554 insertions, 3327 deletions
diff --git a/drivers/char/Kconfig b/drivers/char/Kconfig
index 6aad99ec4e0f..6f31c9472100 100644
--- a/drivers/char/Kconfig
+++ b/drivers/char/Kconfig
@@ -201,19 +201,6 @@ config DIGIEPCA
201 To compile this driver as a module, choose M here: the 201 To compile this driver as a module, choose M here: the
202 module will be called epca. 202 module will be called epca.
203 203
204config ESPSERIAL
205 tristate "Hayes ESP serial port support"
206 depends on SERIAL_NONSTANDARD && ISA && ISA_DMA_API && BROKEN
207 help
208 This is a driver which supports Hayes ESP serial ports. Both single
209 port cards and multiport cards are supported. Make sure to read
210 <file:Documentation/hayes-esp.txt>.
211
212 To compile this driver as a module, choose M here: the
213 module will be called esp.
214
215 If unsure, say N.
216
217config MOXA_INTELLIO 204config MOXA_INTELLIO
218 tristate "Moxa Intellio support" 205 tristate "Moxa Intellio support"
219 depends on SERIAL_NONSTANDARD && (ISA || EISA || PCI) 206 depends on SERIAL_NONSTANDARD && (ISA || EISA || PCI)
diff --git a/drivers/char/Makefile b/drivers/char/Makefile
index 19a79dd79eee..f957edf7e45d 100644
--- a/drivers/char/Makefile
+++ b/drivers/char/Makefile
@@ -18,7 +18,6 @@ obj-$(CONFIG_CONSOLE_TRANSLATIONS) += consolemap.o consolemap_deftbl.o
18obj-$(CONFIG_HW_CONSOLE) += vt.o defkeymap.o 18obj-$(CONFIG_HW_CONSOLE) += vt.o defkeymap.o
19obj-$(CONFIG_AUDIT) += tty_audit.o 19obj-$(CONFIG_AUDIT) += tty_audit.o
20obj-$(CONFIG_MAGIC_SYSRQ) += sysrq.o 20obj-$(CONFIG_MAGIC_SYSRQ) += sysrq.o
21obj-$(CONFIG_ESPSERIAL) += esp.o
22obj-$(CONFIG_MVME147_SCC) += generic_serial.o vme_scc.o 21obj-$(CONFIG_MVME147_SCC) += generic_serial.o vme_scc.o
23obj-$(CONFIG_MVME162_SCC) += generic_serial.o vme_scc.o 22obj-$(CONFIG_MVME162_SCC) += generic_serial.o vme_scc.o
24obj-$(CONFIG_BVME6000_SCC) += generic_serial.o vme_scc.o 23obj-$(CONFIG_BVME6000_SCC) += generic_serial.o vme_scc.o
diff --git a/drivers/char/bfin_jtag_comm.c b/drivers/char/bfin_jtag_comm.c
index 1d7c34c73b20..2628c7415ea8 100644
--- a/drivers/char/bfin_jtag_comm.c
+++ b/drivers/char/bfin_jtag_comm.c
@@ -226,7 +226,7 @@ bfin_jc_wait_until_sent(struct tty_struct *tty, int timeout)
226 } 226 }
227} 227}
228 228
229static struct tty_operations bfin_jc_ops = { 229static const struct tty_operations bfin_jc_ops = {
230 .open = bfin_jc_open, 230 .open = bfin_jc_open,
231 .close = bfin_jc_close, 231 .close = bfin_jc_close,
232 .write = bfin_jc_write, 232 .write = bfin_jc_write,
diff --git a/drivers/char/epca.c b/drivers/char/epca.c
index dde5134713e2..17b044a71e02 100644
--- a/drivers/char/epca.c
+++ b/drivers/char/epca.c
@@ -935,7 +935,7 @@ static int info_open(struct tty_struct *tty, struct file *filp)
935 return 0; 935 return 0;
936} 936}
937 937
938static struct tty_operations info_ops = { 938static const struct tty_operations info_ops = {
939 .open = info_open, 939 .open = info_open,
940 .ioctl = info_ioctl, 940 .ioctl = info_ioctl,
941}; 941};
diff --git a/drivers/char/esp.c b/drivers/char/esp.c
deleted file mode 100644
index b19d43cd9542..000000000000
--- a/drivers/char/esp.c
+++ /dev/null
@@ -1,2533 +0,0 @@
1/*
2 * esp.c - driver for Hayes ESP serial cards
3 *
4 * --- Notices from serial.c, upon which this driver is based ---
5 *
6 * Copyright (C) 1991, 1992 Linus Torvalds
7 *
8 * Extensively rewritten by Theodore Ts'o, 8/16/92 -- 9/14/92. Now
9 * much more extensible to support other serial cards based on the
10 * 16450/16550A UART's. Added support for the AST FourPort and the
11 * Accent Async board.
12 *
13 * set_serial_info fixed to set the flags, custom divisor, and uart
14 * type fields. Fix suggested by Michael K. Johnson 12/12/92.
15 *
16 * 11/95: TIOCMIWAIT, TIOCGICOUNT by Angelo Haritsis <ah@doc.ic.ac.uk>
17 *
18 * 03/96: Modularised by Angelo Haritsis <ah@doc.ic.ac.uk>
19 *
20 * rs_set_termios fixed to look also for changes of the input
21 * flags INPCK, BRKINT, PARMRK, IGNPAR and IGNBRK.
22 * Bernd Anhäupl 05/17/96.
23 *
24 * --- End of notices from serial.c ---
25 *
26 * Support for the ESP serial card by Andrew J. Robinson
27 * <arobinso@nyx.net> (Card detection routine taken from a patch
28 * by Dennis J. Boylan). Patches to allow use with 2.1.x contributed
29 * by Chris Faylor.
30 *
31 * Most recent changes: (Andrew J. Robinson)
32 * Support for PIO mode. This allows the driver to work properly with
33 * multiport cards.
34 *
35 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> -
36 * several cleanups, use module_init/module_exit, etc
37 *
38 * This module exports the following rs232 io functions:
39 *
40 * int espserial_init(void);
41 */
42
43#include <linux/module.h>
44#include <linux/errno.h>
45#include <linux/signal.h>
46#include <linux/sched.h>
47#include <linux/interrupt.h>
48#include <linux/tty.h>
49#include <linux/tty_flip.h>
50#include <linux/serial.h>
51#include <linux/serialP.h>
52#include <linux/serial_reg.h>
53#include <linux/major.h>
54#include <linux/string.h>
55#include <linux/fcntl.h>
56#include <linux/ptrace.h>
57#include <linux/ioport.h>
58#include <linux/mm.h>
59#include <linux/init.h>
60#include <linux/delay.h>
61#include <linux/bitops.h>
62
63#include <asm/system.h>
64#include <linux/io.h>
65
66#include <asm/dma.h>
67#include <linux/slab.h>
68#include <linux/uaccess.h>
69
70#include <linux/hayesesp.h>
71
72#define NR_PORTS 64 /* maximum number of ports */
73#define NR_PRIMARY 8 /* maximum number of primary ports */
74#define REGION_SIZE 8 /* size of io region to request */
75
76/* The following variables can be set by giving module options */
77static int irq[NR_PRIMARY]; /* IRQ for each base port */
78static unsigned int divisor[NR_PRIMARY]; /* custom divisor for each port */
79static unsigned int dma = ESP_DMA_CHANNEL; /* DMA channel */
80static unsigned int rx_trigger = ESP_RX_TRIGGER;
81static unsigned int tx_trigger = ESP_TX_TRIGGER;
82static unsigned int flow_off = ESP_FLOW_OFF;
83static unsigned int flow_on = ESP_FLOW_ON;
84static unsigned int rx_timeout = ESP_RX_TMOUT;
85static unsigned int pio_threshold = ESP_PIO_THRESHOLD;
86
87MODULE_LICENSE("GPL");
88
89module_param_array(irq, int, NULL, 0);
90module_param_array(divisor, uint, NULL, 0);
91module_param(dma, uint, 0);
92module_param(rx_trigger, uint, 0);
93module_param(tx_trigger, uint, 0);
94module_param(flow_off, uint, 0);
95module_param(flow_on, uint, 0);
96module_param(rx_timeout, uint, 0);
97module_param(pio_threshold, uint, 0);
98
99/* END */
100
101static char *dma_buffer;
102static int dma_bytes;
103static struct esp_pio_buffer *free_pio_buf;
104
105#define DMA_BUFFER_SZ 1024
106
107#define WAKEUP_CHARS 1024
108
109static char serial_name[] __initdata = "ESP serial driver";
110static char serial_version[] __initdata = "2.2";
111
112static struct tty_driver *esp_driver;
113
114/*
115 * Serial driver configuration section. Here are the various options:
116 *
117 * SERIAL_PARANOIA_CHECK
118 * Check the magic number for the esp_structure where
119 * ever possible.
120 */
121
122#undef SERIAL_PARANOIA_CHECK
123#define SERIAL_DO_RESTART
124
125#undef SERIAL_DEBUG_INTR
126#undef SERIAL_DEBUG_OPEN
127#undef SERIAL_DEBUG_FLOW
128
129#if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
130#define DBG_CNT(s) printk(KERN_DEBUG "(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
131 tty->name, info->port.flags, \
132 serial_driver.refcount, \
133 info->port.count, tty->count, s)
134#else
135#define DBG_CNT(s)
136#endif
137
138static struct esp_struct *ports;
139
140static void change_speed(struct esp_struct *info);
141static void rs_wait_until_sent(struct tty_struct *, int);
142
143/*
144 * The ESP card has a clock rate of 14.7456 MHz (that is, 2**ESPC_SCALE
145 * times the normal 1.8432 Mhz clock of most serial boards).
146 */
147#define BASE_BAUD ((1843200 / 16) * (1 << ESPC_SCALE))
148
149/* Standard COM flags (except for COM4, because of the 8514 problem) */
150#define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
151
152static inline int serial_paranoia_check(struct esp_struct *info,
153 char *name, const char *routine)
154{
155#ifdef SERIAL_PARANOIA_CHECK
156 static const char badmagic[] = KERN_WARNING
157 "Warning: bad magic number for serial struct (%s) in %s\n";
158 static const char badinfo[] = KERN_WARNING
159 "Warning: null esp_struct for (%s) in %s\n";
160
161 if (!info) {
162 printk(badinfo, name, routine);
163 return 1;
164 }
165 if (info->magic != ESP_MAGIC) {
166 printk(badmagic, name, routine);
167 return 1;
168 }
169#endif
170 return 0;
171}
172
173static inline unsigned int serial_in(struct esp_struct *info, int offset)
174{
175 return inb(info->io_port + offset);
176}
177
178static inline void serial_out(struct esp_struct *info, int offset,
179 unsigned char value)
180{
181 outb(value, info->io_port+offset);
182}
183
184/*
185 * ------------------------------------------------------------
186 * rs_stop() and rs_start()
187 *
188 * This routines are called before setting or resetting tty->stopped.
189 * They enable or disable transmitter interrupts, as necessary.
190 * ------------------------------------------------------------
191 */
192static void rs_stop(struct tty_struct *tty)
193{
194 struct esp_struct *info = tty->driver_data;
195 unsigned long flags;
196
197 if (serial_paranoia_check(info, tty->name, "rs_stop"))
198 return;
199
200 spin_lock_irqsave(&info->lock, flags);
201 if (info->IER & UART_IER_THRI) {
202 info->IER &= ~UART_IER_THRI;
203 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
204 serial_out(info, UART_ESI_CMD2, info->IER);
205 }
206 spin_unlock_irqrestore(&info->lock, flags);
207}
208
209static void rs_start(struct tty_struct *tty)
210{
211 struct esp_struct *info = tty->driver_data;
212 unsigned long flags;
213
214 if (serial_paranoia_check(info, tty->name, "rs_start"))
215 return;
216
217 spin_lock_irqsave(&info->lock, flags);
218 if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
219 info->IER |= UART_IER_THRI;
220 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
221 serial_out(info, UART_ESI_CMD2, info->IER);
222 }
223 spin_unlock_irqrestore(&info->lock, flags);
224}
225
226/*
227 * ----------------------------------------------------------------------
228 *
229 * Here starts the interrupt handling routines. All of the following
230 * subroutines are declared as inline and are folded into
231 * rs_interrupt(). They were separated out for readability's sake.
232 *
233 * Note: rs_interrupt() is a "fast" interrupt, which means that it
234 * runs with interrupts turned off. People who may want to modify
235 * rs_interrupt() should try to keep the interrupt handler as fast as
236 * possible. After you are done making modifications, it is not a bad
237 * idea to do:
238 *
239 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
240 *
241 * and look at the resulting assemble code in serial.s.
242 *
243 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
244 * -----------------------------------------------------------------------
245 */
246
247static DEFINE_SPINLOCK(pio_lock);
248
249static inline struct esp_pio_buffer *get_pio_buffer(void)
250{
251 struct esp_pio_buffer *buf;
252 unsigned long flags;
253
254 spin_lock_irqsave(&pio_lock, flags);
255 if (free_pio_buf) {
256 buf = free_pio_buf;
257 free_pio_buf = buf->next;
258 } else {
259 buf = kmalloc(sizeof(struct esp_pio_buffer), GFP_ATOMIC);
260 }
261 spin_unlock_irqrestore(&pio_lock, flags);
262 return buf;
263}
264
265static inline void release_pio_buffer(struct esp_pio_buffer *buf)
266{
267 unsigned long flags;
268 spin_lock_irqsave(&pio_lock, flags);
269 buf->next = free_pio_buf;
270 free_pio_buf = buf;
271 spin_unlock_irqrestore(&pio_lock, flags);
272}
273
274static inline void receive_chars_pio(struct esp_struct *info, int num_bytes)
275{
276 struct tty_struct *tty = info->port.tty;
277 int i;
278 struct esp_pio_buffer *pio_buf;
279 struct esp_pio_buffer *err_buf;
280 unsigned char status_mask;
281
282 pio_buf = get_pio_buffer();
283
284 if (!pio_buf)
285 return;
286
287 err_buf = get_pio_buffer();
288
289 if (!err_buf) {
290 release_pio_buffer(pio_buf);
291 return;
292 }
293
294 status_mask = (info->read_status_mask >> 2) & 0x07;
295
296 for (i = 0; i < num_bytes - 1; i += 2) {
297 *((unsigned short *)(pio_buf->data + i)) =
298 inw(info->io_port + UART_ESI_RX);
299 err_buf->data[i] = serial_in(info, UART_ESI_RWS);
300 err_buf->data[i + 1] = (err_buf->data[i] >> 3) & status_mask;
301 err_buf->data[i] &= status_mask;
302 }
303
304 if (num_bytes & 0x0001) {
305 pio_buf->data[num_bytes - 1] = serial_in(info, UART_ESI_RX);
306 err_buf->data[num_bytes - 1] =
307 (serial_in(info, UART_ESI_RWS) >> 3) & status_mask;
308 }
309
310 /* make sure everything is still ok since interrupts were enabled */
311 tty = info->port.tty;
312
313 if (!tty) {
314 release_pio_buffer(pio_buf);
315 release_pio_buffer(err_buf);
316 info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
317 return;
318 }
319
320 status_mask = (info->ignore_status_mask >> 2) & 0x07;
321
322 for (i = 0; i < num_bytes; i++) {
323 if (!(err_buf->data[i] & status_mask)) {
324 int flag = 0;
325
326 if (err_buf->data[i] & 0x04) {
327 flag = TTY_BREAK;
328 if (info->port.flags & ASYNC_SAK)
329 do_SAK(tty);
330 } else if (err_buf->data[i] & 0x02)
331 flag = TTY_FRAME;
332 else if (err_buf->data[i] & 0x01)
333 flag = TTY_PARITY;
334 tty_insert_flip_char(tty, pio_buf->data[i], flag);
335 }
336 }
337
338 tty_schedule_flip(tty);
339
340 info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
341 release_pio_buffer(pio_buf);
342 release_pio_buffer(err_buf);
343}
344
345static void program_isa_dma(int dma, int dir, unsigned long addr, int len)
346{
347 unsigned long flags;
348
349 flags = claim_dma_lock();
350 disable_dma(dma);
351 clear_dma_ff(dma);
352 set_dma_mode(dma, dir);
353 set_dma_addr(dma, addr);
354 set_dma_count(dma, len);
355 enable_dma(dma);
356 release_dma_lock(flags);
357}
358
359static void receive_chars_dma(struct esp_struct *info, int num_bytes)
360{
361 info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
362 dma_bytes = num_bytes;
363 info->stat_flags |= ESP_STAT_DMA_RX;
364
365 program_isa_dma(dma, DMA_MODE_READ, isa_virt_to_bus(dma_buffer),
366 dma_bytes);
367 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_RX);
368}
369
370static inline void receive_chars_dma_done(struct esp_struct *info,
371 int status)
372{
373 struct tty_struct *tty = info->port.tty;
374 int num_bytes;
375 unsigned long flags;
376
377 flags = claim_dma_lock();
378 disable_dma(dma);
379 clear_dma_ff(dma);
380
381 info->stat_flags &= ~ESP_STAT_DMA_RX;
382 num_bytes = dma_bytes - get_dma_residue(dma);
383 release_dma_lock(flags);
384
385 info->icount.rx += num_bytes;
386
387 if (num_bytes > 0) {
388 tty_insert_flip_string(tty, dma_buffer, num_bytes - 1);
389
390 status &= (0x1c & info->read_status_mask);
391
392 /* Is the status significant or do we throw the last byte ? */
393 if (!(status & info->ignore_status_mask)) {
394 int statflag = 0;
395
396 if (status & 0x10) {
397 statflag = TTY_BREAK;
398 (info->icount.brk)++;
399 if (info->port.flags & ASYNC_SAK)
400 do_SAK(tty);
401 } else if (status & 0x08) {
402 statflag = TTY_FRAME;
403 info->icount.frame++;
404 } else if (status & 0x04) {
405 statflag = TTY_PARITY;
406 info->icount.parity++;
407 }
408 tty_insert_flip_char(tty, dma_buffer[num_bytes - 1],
409 statflag);
410 }
411 tty_schedule_flip(tty);
412 }
413
414 if (dma_bytes != num_bytes) {
415 num_bytes = dma_bytes - num_bytes;
416 dma_bytes = 0;
417 receive_chars_dma(info, num_bytes);
418 } else
419 dma_bytes = 0;
420}
421
422/* Caller must hold info->lock */
423
424static inline void transmit_chars_pio(struct esp_struct *info,
425 int space_avail)
426{
427 int i;
428 struct esp_pio_buffer *pio_buf;
429
430 pio_buf = get_pio_buffer();
431
432 if (!pio_buf)
433 return;
434
435 while (space_avail && info->xmit_cnt) {
436 if (info->xmit_tail + space_avail <= ESP_XMIT_SIZE) {
437 memcpy(pio_buf->data,
438 &(info->xmit_buf[info->xmit_tail]),
439 space_avail);
440 } else {
441 i = ESP_XMIT_SIZE - info->xmit_tail;
442 memcpy(pio_buf->data,
443 &(info->xmit_buf[info->xmit_tail]), i);
444 memcpy(&(pio_buf->data[i]), info->xmit_buf,
445 space_avail - i);
446 }
447
448 info->xmit_cnt -= space_avail;
449 info->xmit_tail = (info->xmit_tail + space_avail) &
450 (ESP_XMIT_SIZE - 1);
451
452 for (i = 0; i < space_avail - 1; i += 2) {
453 outw(*((unsigned short *)(pio_buf->data + i)),
454 info->io_port + UART_ESI_TX);
455 }
456
457 if (space_avail & 0x0001)
458 serial_out(info, UART_ESI_TX,
459 pio_buf->data[space_avail - 1]);
460
461 if (info->xmit_cnt) {
462 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
463 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
464 space_avail = serial_in(info, UART_ESI_STAT1) << 8;
465 space_avail |= serial_in(info, UART_ESI_STAT2);
466
467 if (space_avail > info->xmit_cnt)
468 space_avail = info->xmit_cnt;
469 }
470 }
471
472 if (info->xmit_cnt < WAKEUP_CHARS) {
473 if (info->port.tty)
474 tty_wakeup(info->port.tty);
475
476#ifdef SERIAL_DEBUG_INTR
477 printk("THRE...");
478#endif
479
480 if (info->xmit_cnt <= 0) {
481 info->IER &= ~UART_IER_THRI;
482 serial_out(info, UART_ESI_CMD1,
483 ESI_SET_SRV_MASK);
484 serial_out(info, UART_ESI_CMD2, info->IER);
485 }
486 }
487
488 release_pio_buffer(pio_buf);
489}
490
491/* Caller must hold info->lock */
492static inline void transmit_chars_dma(struct esp_struct *info, int num_bytes)
493{
494 dma_bytes = num_bytes;
495
496 if (info->xmit_tail + dma_bytes <= ESP_XMIT_SIZE) {
497 memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
498 dma_bytes);
499 } else {
500 int i = ESP_XMIT_SIZE - info->xmit_tail;
501 memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
502 i);
503 memcpy(&(dma_buffer[i]), info->xmit_buf, dma_bytes - i);
504 }
505
506 info->xmit_cnt -= dma_bytes;
507 info->xmit_tail = (info->xmit_tail + dma_bytes) & (ESP_XMIT_SIZE - 1);
508
509 if (info->xmit_cnt < WAKEUP_CHARS) {
510 if (info->port.tty)
511 tty_wakeup(info->port.tty);
512
513#ifdef SERIAL_DEBUG_INTR
514 printk("THRE...");
515#endif
516
517 if (info->xmit_cnt <= 0) {
518 info->IER &= ~UART_IER_THRI;
519 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
520 serial_out(info, UART_ESI_CMD2, info->IER);
521 }
522 }
523
524 info->stat_flags |= ESP_STAT_DMA_TX;
525
526 program_isa_dma(dma, DMA_MODE_WRITE, isa_virt_to_bus(dma_buffer),
527 dma_bytes);
528 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
529}
530
531static inline void transmit_chars_dma_done(struct esp_struct *info)
532{
533 int num_bytes;
534 unsigned long flags;
535
536 flags = claim_dma_lock();
537 disable_dma(dma);
538 clear_dma_ff(dma);
539
540 num_bytes = dma_bytes - get_dma_residue(dma);
541 info->icount.tx += dma_bytes;
542 release_dma_lock(flags);
543
544 if (dma_bytes != num_bytes) {
545 dma_bytes -= num_bytes;
546 memmove(dma_buffer, dma_buffer + num_bytes, dma_bytes);
547
548 program_isa_dma(dma, DMA_MODE_WRITE,
549 isa_virt_to_bus(dma_buffer), dma_bytes);
550
551 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
552 } else {
553 dma_bytes = 0;
554 info->stat_flags &= ~ESP_STAT_DMA_TX;
555 }
556}
557
558static void check_modem_status(struct esp_struct *info)
559{
560 int status;
561
562 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
563 status = serial_in(info, UART_ESI_STAT2);
564
565 if (status & UART_MSR_ANY_DELTA) {
566 /* update input line counters */
567 if (status & UART_MSR_TERI)
568 info->icount.rng++;
569 if (status & UART_MSR_DDSR)
570 info->icount.dsr++;
571 if (status & UART_MSR_DDCD)
572 info->icount.dcd++;
573 if (status & UART_MSR_DCTS)
574 info->icount.cts++;
575 wake_up_interruptible(&info->port.delta_msr_wait);
576 }
577
578 if ((info->port.flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
579#if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
580 printk("ttys%d CD now %s...", info->line,
581 (status & UART_MSR_DCD) ? "on" : "off");
582#endif
583 if (status & UART_MSR_DCD)
584 wake_up_interruptible(&info->port.open_wait);
585 else {
586#ifdef SERIAL_DEBUG_OPEN
587 printk("scheduling hangup...");
588#endif
589 tty_hangup(info->port.tty);
590 }
591 }
592}
593
594/*
595 * This is the serial driver's interrupt routine
596 */
597static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
598{
599 struct esp_struct *info;
600 unsigned err_status;
601 unsigned int scratch;
602
603#ifdef SERIAL_DEBUG_INTR
604 printk("rs_interrupt_single(%d)...", irq);
605#endif
606 info = (struct esp_struct *)dev_id;
607 err_status = 0;
608 scratch = serial_in(info, UART_ESI_SID);
609
610 spin_lock(&info->lock);
611
612 if (!info->port.tty) {
613 spin_unlock(&info->lock);
614 return IRQ_NONE;
615 }
616
617 if (scratch & 0x04) { /* error */
618 serial_out(info, UART_ESI_CMD1, ESI_GET_ERR_STAT);
619 err_status = serial_in(info, UART_ESI_STAT1);
620 serial_in(info, UART_ESI_STAT2);
621
622 if (err_status & 0x01)
623 info->stat_flags |= ESP_STAT_RX_TIMEOUT;
624
625 if (err_status & 0x20) /* UART status */
626 check_modem_status(info);
627
628 if (err_status & 0x80) /* Start break */
629 wake_up_interruptible(&info->break_wait);
630 }
631
632 if ((scratch & 0x88) || /* DMA completed or timed out */
633 (err_status & 0x1c) /* receive error */) {
634 if (info->stat_flags & ESP_STAT_DMA_RX)
635 receive_chars_dma_done(info, err_status);
636 else if (info->stat_flags & ESP_STAT_DMA_TX)
637 transmit_chars_dma_done(info);
638 }
639
640 if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
641 ((scratch & 0x01) || (info->stat_flags & ESP_STAT_RX_TIMEOUT)) &&
642 (info->IER & UART_IER_RDI)) {
643 int num_bytes;
644
645 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
646 serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
647 num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
648 num_bytes |= serial_in(info, UART_ESI_STAT2);
649
650 num_bytes = tty_buffer_request_room(info->port.tty, num_bytes);
651
652 if (num_bytes) {
653 if (dma_bytes ||
654 (info->stat_flags & ESP_STAT_USE_PIO) ||
655 (num_bytes <= info->config.pio_threshold))
656 receive_chars_pio(info, num_bytes);
657 else
658 receive_chars_dma(info, num_bytes);
659 }
660 }
661
662 if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
663 (scratch & 0x02) && (info->IER & UART_IER_THRI)) {
664 if ((info->xmit_cnt <= 0) || info->port.tty->stopped) {
665 info->IER &= ~UART_IER_THRI;
666 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
667 serial_out(info, UART_ESI_CMD2, info->IER);
668 } else {
669 int num_bytes;
670
671 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
672 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
673 num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
674 num_bytes |= serial_in(info, UART_ESI_STAT2);
675
676 if (num_bytes > info->xmit_cnt)
677 num_bytes = info->xmit_cnt;
678
679 if (num_bytes) {
680 if (dma_bytes ||
681 (info->stat_flags & ESP_STAT_USE_PIO) ||
682 (num_bytes <= info->config.pio_threshold))
683 transmit_chars_pio(info, num_bytes);
684 else
685 transmit_chars_dma(info, num_bytes);
686 }
687 }
688 }
689
690 info->last_active = jiffies;
691
692#ifdef SERIAL_DEBUG_INTR
693 printk("end.\n");
694#endif
695 spin_unlock(&info->lock);
696 return IRQ_HANDLED;
697}
698
699/*
700 * -------------------------------------------------------------------
701 * Here ends the serial interrupt routines.
702 * -------------------------------------------------------------------
703 */
704
705/*
706 * ---------------------------------------------------------------
707 * Low level utility subroutines for the serial driver: routines to
708 * figure out the appropriate timeout for an interrupt chain, routines
709 * to initialize and startup a serial port, and routines to shutdown a
710 * serial port. Useful stuff like that.
711 *
712 * Caller should hold lock
713 * ---------------------------------------------------------------
714 */
715
716static void esp_basic_init(struct esp_struct *info)
717{
718 /* put ESPC in enhanced mode */
719 serial_out(info, UART_ESI_CMD1, ESI_SET_MODE);
720
721 if (info->stat_flags & ESP_STAT_NEVER_DMA)
722 serial_out(info, UART_ESI_CMD2, 0x01);
723 else
724 serial_out(info, UART_ESI_CMD2, 0x31);
725
726 /* disable interrupts for now */
727 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
728 serial_out(info, UART_ESI_CMD2, 0x00);
729
730 /* set interrupt and DMA channel */
731 serial_out(info, UART_ESI_CMD1, ESI_SET_IRQ);
732
733 if (info->stat_flags & ESP_STAT_NEVER_DMA)
734 serial_out(info, UART_ESI_CMD2, 0x01);
735 else
736 serial_out(info, UART_ESI_CMD2, (dma << 4) | 0x01);
737
738 serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
739
740 if (info->line % 8) /* secondary port */
741 serial_out(info, UART_ESI_CMD2, 0x0d); /* shared */
742 else if (info->irq == 9)
743 serial_out(info, UART_ESI_CMD2, 0x02);
744 else
745 serial_out(info, UART_ESI_CMD2, info->irq);
746
747 /* set error status mask (check this) */
748 serial_out(info, UART_ESI_CMD1, ESI_SET_ERR_MASK);
749
750 if (info->stat_flags & ESP_STAT_NEVER_DMA)
751 serial_out(info, UART_ESI_CMD2, 0xa1);
752 else
753 serial_out(info, UART_ESI_CMD2, 0xbd);
754
755 serial_out(info, UART_ESI_CMD2, 0x00);
756
757 /* set DMA timeout */
758 serial_out(info, UART_ESI_CMD1, ESI_SET_DMA_TMOUT);
759 serial_out(info, UART_ESI_CMD2, 0xff);
760
761 /* set FIFO trigger levels */
762 serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
763 serial_out(info, UART_ESI_CMD2, info->config.rx_trigger >> 8);
764 serial_out(info, UART_ESI_CMD2, info->config.rx_trigger);
765 serial_out(info, UART_ESI_CMD2, info->config.tx_trigger >> 8);
766 serial_out(info, UART_ESI_CMD2, info->config.tx_trigger);
767
768 /* Set clock scaling and wait states */
769 serial_out(info, UART_ESI_CMD1, ESI_SET_PRESCALAR);
770 serial_out(info, UART_ESI_CMD2, 0x04 | ESPC_SCALE);
771
772 /* set reinterrupt pacing */
773 serial_out(info, UART_ESI_CMD1, ESI_SET_REINTR);
774 serial_out(info, UART_ESI_CMD2, 0xff);
775}
776
777static int startup(struct esp_struct *info)
778{
779 unsigned long flags;
780 int retval = 0;
781 unsigned int num_chars;
782
783 spin_lock_irqsave(&info->lock, flags);
784
785 if (info->port.flags & ASYNC_INITIALIZED)
786 goto out;
787
788 if (!info->xmit_buf) {
789 info->xmit_buf = (unsigned char *)get_zeroed_page(GFP_ATOMIC);
790 retval = -ENOMEM;
791 if (!info->xmit_buf)
792 goto out;
793 }
794
795#ifdef SERIAL_DEBUG_OPEN
796 printk(KERN_DEBUG "starting up ttys%d (irq %d)...",
797 info->line, info->irq);
798#endif
799
800 /* Flush the RX buffer. Using the ESI flush command may cause */
801 /* wild interrupts, so read all the data instead. */
802
803 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
804 serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
805 num_chars = serial_in(info, UART_ESI_STAT1) << 8;
806 num_chars |= serial_in(info, UART_ESI_STAT2);
807
808 while (num_chars > 1) {
809 inw(info->io_port + UART_ESI_RX);
810 num_chars -= 2;
811 }
812
813 if (num_chars)
814 serial_in(info, UART_ESI_RX);
815
816 /* set receive character timeout */
817 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
818 serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
819
820 /* clear all flags except the "never DMA" flag */
821 info->stat_flags &= ESP_STAT_NEVER_DMA;
822
823 if (info->stat_flags & ESP_STAT_NEVER_DMA)
824 info->stat_flags |= ESP_STAT_USE_PIO;
825
826 spin_unlock_irqrestore(&info->lock, flags);
827
828 /*
829 * Allocate the IRQ
830 */
831
832 retval = request_irq(info->irq, rs_interrupt_single, IRQF_SHARED,
833 "esp serial", info);
834
835 if (retval) {
836 if (capable(CAP_SYS_ADMIN)) {
837 if (info->port.tty)
838 set_bit(TTY_IO_ERROR,
839 &info->port.tty->flags);
840 retval = 0;
841 }
842 goto out_unlocked;
843 }
844
845 if (!(info->stat_flags & ESP_STAT_USE_PIO) && !dma_buffer) {
846 dma_buffer = (char *)__get_dma_pages(
847 GFP_KERNEL, get_order(DMA_BUFFER_SZ));
848
849 /* use PIO mode if DMA buf/chan cannot be allocated */
850 if (!dma_buffer)
851 info->stat_flags |= ESP_STAT_USE_PIO;
852 else if (request_dma(dma, "esp serial")) {
853 free_pages((unsigned long)dma_buffer,
854 get_order(DMA_BUFFER_SZ));
855 dma_buffer = NULL;
856 info->stat_flags |= ESP_STAT_USE_PIO;
857 }
858
859 }
860
861 info->MCR = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
862
863 spin_lock_irqsave(&info->lock, flags);
864 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
865 serial_out(info, UART_ESI_CMD2, UART_MCR);
866 serial_out(info, UART_ESI_CMD2, info->MCR);
867
868 /*
869 * Finally, enable interrupts
870 */
871 /* info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI; */
872 info->IER = UART_IER_RLSI | UART_IER_RDI | UART_IER_DMA_TMOUT |
873 UART_IER_DMA_TC;
874 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
875 serial_out(info, UART_ESI_CMD2, info->IER);
876
877 if (info->port.tty)
878 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
879 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
880 spin_unlock_irqrestore(&info->lock, flags);
881
882 /*
883 * Set up the tty->alt_speed kludge
884 */
885 if (info->port.tty) {
886 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
887 info->port.tty->alt_speed = 57600;
888 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
889 info->port.tty->alt_speed = 115200;
890 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
891 info->port.tty->alt_speed = 230400;
892 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
893 info->port.tty->alt_speed = 460800;
894 }
895
896 /*
897 * set the speed of the serial port
898 */
899 change_speed(info);
900 info->port.flags |= ASYNC_INITIALIZED;
901 return 0;
902
903out:
904 spin_unlock_irqrestore(&info->lock, flags);
905out_unlocked:
906 return retval;
907}
908
909/*
910 * This routine will shutdown a serial port; interrupts are disabled, and
911 * DTR is dropped if the hangup on close termio flag is on.
912 */
913static void shutdown(struct esp_struct *info)
914{
915 unsigned long flags, f;
916
917 if (!(info->port.flags & ASYNC_INITIALIZED))
918 return;
919
920#ifdef SERIAL_DEBUG_OPEN
921 printk("Shutting down serial port %d (irq %d)....", info->line,
922 info->irq);
923#endif
924
925 spin_lock_irqsave(&info->lock, flags);
926 /*
927 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
928 * here so the queue might never be waken up
929 */
930 wake_up_interruptible(&info->port.delta_msr_wait);
931 wake_up_interruptible(&info->break_wait);
932
933 /* stop a DMA transfer on the port being closed */
934 /* DMA lock is higher priority always */
935 if (info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) {
936 f = claim_dma_lock();
937 disable_dma(dma);
938 clear_dma_ff(dma);
939 release_dma_lock(f);
940
941 dma_bytes = 0;
942 }
943
944 /*
945 * Free the IRQ
946 */
947 free_irq(info->irq, info);
948
949 if (dma_buffer) {
950 struct esp_struct *current_port = ports;
951
952 while (current_port) {
953 if ((current_port != info) &&
954 (current_port->port.flags & ASYNC_INITIALIZED))
955 break;
956
957 current_port = current_port->next_port;
958 }
959
960 if (!current_port) {
961 free_dma(dma);
962 free_pages((unsigned long)dma_buffer,
963 get_order(DMA_BUFFER_SZ));
964 dma_buffer = NULL;
965 }
966 }
967
968 if (info->xmit_buf) {
969 free_page((unsigned long) info->xmit_buf);
970 info->xmit_buf = NULL;
971 }
972
973 info->IER = 0;
974 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
975 serial_out(info, UART_ESI_CMD2, 0x00);
976
977 if (!info->port.tty || (info->port.tty->termios->c_cflag & HUPCL))
978 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
979
980 info->MCR &= ~UART_MCR_OUT2;
981 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
982 serial_out(info, UART_ESI_CMD2, UART_MCR);
983 serial_out(info, UART_ESI_CMD2, info->MCR);
984
985 if (info->port.tty)
986 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
987
988 info->port.flags &= ~ASYNC_INITIALIZED;
989 spin_unlock_irqrestore(&info->lock, flags);
990}
991
992/*
993 * This routine is called to set the UART divisor registers to match
994 * the specified baud rate for a serial port.
995 */
996static void change_speed(struct esp_struct *info)
997{
998 unsigned short port;
999 int quot = 0;
1000 unsigned cflag, cval;
1001 int baud, bits;
1002 unsigned char flow1 = 0, flow2 = 0;
1003 unsigned long flags;
1004
1005 if (!info->port.tty || !info->port.tty->termios)
1006 return;
1007 cflag = info->port.tty->termios->c_cflag;
1008 port = info->io_port;
1009
1010 /* byte size and parity */
1011 switch (cflag & CSIZE) {
1012 case CS5: cval = 0x00; bits = 7; break;
1013 case CS6: cval = 0x01; bits = 8; break;
1014 case CS7: cval = 0x02; bits = 9; break;
1015 case CS8: cval = 0x03; bits = 10; break;
1016 default: cval = 0x00; bits = 7; break;
1017 }
1018 if (cflag & CSTOPB) {
1019 cval |= 0x04;
1020 bits++;
1021 }
1022 if (cflag & PARENB) {
1023 cval |= UART_LCR_PARITY;
1024 bits++;
1025 }
1026 if (!(cflag & PARODD))
1027 cval |= UART_LCR_EPAR;
1028#ifdef CMSPAR
1029 if (cflag & CMSPAR)
1030 cval |= UART_LCR_SPAR;
1031#endif
1032 baud = tty_get_baud_rate(info->port.tty);
1033 if (baud == 38400 &&
1034 ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1035 quot = info->custom_divisor;
1036 else {
1037 if (baud == 134) /* Special case since 134 is really 134.5 */
1038 quot = (2*BASE_BAUD / 269);
1039 else if (baud)
1040 quot = BASE_BAUD / baud;
1041 }
1042 /* If the quotient is ever zero, default to 9600 bps */
1043 if (!quot)
1044 quot = BASE_BAUD / 9600;
1045
1046 if (baud) {
1047 /* Actual rate */
1048 baud = BASE_BAUD/quot;
1049 tty_encode_baud_rate(info->port.tty, baud, baud);
1050 }
1051 info->timeout = ((1024 * HZ * bits * quot) / BASE_BAUD) + (HZ / 50);
1052
1053 /* CTS flow control flag and modem status interrupts */
1054 /* info->IER &= ~UART_IER_MSI; */
1055 if (cflag & CRTSCTS) {
1056 info->port.flags |= ASYNC_CTS_FLOW;
1057 /* info->IER |= UART_IER_MSI; */
1058 flow1 = 0x04;
1059 flow2 = 0x10;
1060 } else
1061 info->port.flags &= ~ASYNC_CTS_FLOW;
1062 if (cflag & CLOCAL)
1063 info->port.flags &= ~ASYNC_CHECK_CD;
1064 else
1065 info->port.flags |= ASYNC_CHECK_CD;
1066
1067 /*
1068 * Set up parity check flag
1069 */
1070 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1071 if (I_INPCK(info->port.tty))
1072 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1073 if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
1074 info->read_status_mask |= UART_LSR_BI;
1075
1076 info->ignore_status_mask = 0;
1077#if 0
1078 /* This should be safe, but for some broken bits of hardware... */
1079 if (I_IGNPAR(info->port.tty)) {
1080 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1081 info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
1082 }
1083#endif
1084 if (I_IGNBRK(info->port.tty)) {
1085 info->ignore_status_mask |= UART_LSR_BI;
1086 info->read_status_mask |= UART_LSR_BI;
1087 /*
1088 * If we're ignore parity and break indicators, ignore
1089 * overruns too. (For real raw support).
1090 */
1091 if (I_IGNPAR(info->port.tty)) {
1092 info->ignore_status_mask |= UART_LSR_OE | \
1093 UART_LSR_PE | UART_LSR_FE;
1094 info->read_status_mask |= UART_LSR_OE | \
1095 UART_LSR_PE | UART_LSR_FE;
1096 }
1097 }
1098
1099 if (I_IXOFF(info->port.tty))
1100 flow1 |= 0x81;
1101
1102 spin_lock_irqsave(&info->lock, flags);
1103 /* set baud */
1104 serial_out(info, UART_ESI_CMD1, ESI_SET_BAUD);
1105 serial_out(info, UART_ESI_CMD2, quot >> 8);
1106 serial_out(info, UART_ESI_CMD2, quot & 0xff);
1107
1108 /* set data bits, parity, etc. */
1109 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1110 serial_out(info, UART_ESI_CMD2, UART_LCR);
1111 serial_out(info, UART_ESI_CMD2, cval);
1112
1113 /* Enable flow control */
1114 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CNTL);
1115 serial_out(info, UART_ESI_CMD2, flow1);
1116 serial_out(info, UART_ESI_CMD2, flow2);
1117
1118 /* set flow control characters (XON/XOFF only) */
1119 if (I_IXOFF(info->port.tty)) {
1120 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CHARS);
1121 serial_out(info, UART_ESI_CMD2, START_CHAR(info->port.tty));
1122 serial_out(info, UART_ESI_CMD2, STOP_CHAR(info->port.tty));
1123 serial_out(info, UART_ESI_CMD2, 0x10);
1124 serial_out(info, UART_ESI_CMD2, 0x21);
1125 switch (cflag & CSIZE) {
1126 case CS5:
1127 serial_out(info, UART_ESI_CMD2, 0x1f);
1128 break;
1129 case CS6:
1130 serial_out(info, UART_ESI_CMD2, 0x3f);
1131 break;
1132 case CS7:
1133 case CS8:
1134 serial_out(info, UART_ESI_CMD2, 0x7f);
1135 break;
1136 default:
1137 serial_out(info, UART_ESI_CMD2, 0xff);
1138 break;
1139 }
1140 }
1141
1142 /* Set high/low water */
1143 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1144 serial_out(info, UART_ESI_CMD2, info->config.flow_off >> 8);
1145 serial_out(info, UART_ESI_CMD2, info->config.flow_off);
1146 serial_out(info, UART_ESI_CMD2, info->config.flow_on >> 8);
1147 serial_out(info, UART_ESI_CMD2, info->config.flow_on);
1148
1149 spin_unlock_irqrestore(&info->lock, flags);
1150}
1151
1152static int rs_put_char(struct tty_struct *tty, unsigned char ch)
1153{
1154 struct esp_struct *info = tty->driver_data;
1155 unsigned long flags;
1156 int ret = 0;
1157
1158 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
1159 return 0;
1160
1161 if (!info->xmit_buf)
1162 return 0;
1163
1164 spin_lock_irqsave(&info->lock, flags);
1165 if (info->xmit_cnt < ESP_XMIT_SIZE - 1) {
1166 info->xmit_buf[info->xmit_head++] = ch;
1167 info->xmit_head &= ESP_XMIT_SIZE-1;
1168 info->xmit_cnt++;
1169 ret = 1;
1170 }
1171 spin_unlock_irqrestore(&info->lock, flags);
1172 return ret;
1173}
1174
1175static void rs_flush_chars(struct tty_struct *tty)
1176{
1177 struct esp_struct *info = tty->driver_data;
1178 unsigned long flags;
1179
1180 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
1181 return;
1182
1183 spin_lock_irqsave(&info->lock, flags);
1184
1185 if (info->xmit_cnt <= 0 || tty->stopped || !info->xmit_buf)
1186 goto out;
1187
1188 if (!(info->IER & UART_IER_THRI)) {
1189 info->IER |= UART_IER_THRI;
1190 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1191 serial_out(info, UART_ESI_CMD2, info->IER);
1192 }
1193out:
1194 spin_unlock_irqrestore(&info->lock, flags);
1195}
1196
1197static int rs_write(struct tty_struct *tty,
1198 const unsigned char *buf, int count)
1199{
1200 int c, t, ret = 0;
1201 struct esp_struct *info = tty->driver_data;
1202 unsigned long flags;
1203
1204 if (serial_paranoia_check(info, tty->name, "rs_write"))
1205 return 0;
1206
1207 if (!info->xmit_buf)
1208 return 0;
1209
1210 while (1) {
1211 /* Thanks to R. Wolff for suggesting how to do this with */
1212 /* interrupts enabled */
1213
1214 c = count;
1215 t = ESP_XMIT_SIZE - info->xmit_cnt - 1;
1216
1217 if (t < c)
1218 c = t;
1219
1220 t = ESP_XMIT_SIZE - info->xmit_head;
1221
1222 if (t < c)
1223 c = t;
1224
1225 if (c <= 0)
1226 break;
1227
1228 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1229
1230 info->xmit_head = (info->xmit_head + c) & (ESP_XMIT_SIZE-1);
1231 info->xmit_cnt += c;
1232 buf += c;
1233 count -= c;
1234 ret += c;
1235 }
1236
1237 spin_lock_irqsave(&info->lock, flags);
1238
1239 if (info->xmit_cnt && !tty->stopped && !(info->IER & UART_IER_THRI)) {
1240 info->IER |= UART_IER_THRI;
1241 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1242 serial_out(info, UART_ESI_CMD2, info->IER);
1243 }
1244
1245 spin_unlock_irqrestore(&info->lock, flags);
1246 return ret;
1247}
1248
1249static int rs_write_room(struct tty_struct *tty)
1250{
1251 struct esp_struct *info = tty->driver_data;
1252 int ret;
1253 unsigned long flags;
1254
1255 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
1256 return 0;
1257
1258 spin_lock_irqsave(&info->lock, flags);
1259
1260 ret = ESP_XMIT_SIZE - info->xmit_cnt - 1;
1261 if (ret < 0)
1262 ret = 0;
1263 spin_unlock_irqrestore(&info->lock, flags);
1264 return ret;
1265}
1266
1267static int rs_chars_in_buffer(struct tty_struct *tty)
1268{
1269 struct esp_struct *info = tty->driver_data;
1270
1271 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
1272 return 0;
1273 return info->xmit_cnt;
1274}
1275
1276static void rs_flush_buffer(struct tty_struct *tty)
1277{
1278 struct esp_struct *info = tty->driver_data;
1279 unsigned long flags;
1280
1281 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
1282 return;
1283 spin_lock_irqsave(&info->lock, flags);
1284 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1285 spin_unlock_irqrestore(&info->lock, flags);
1286 tty_wakeup(tty);
1287}
1288
1289/*
1290 * ------------------------------------------------------------
1291 * rs_throttle()
1292 *
1293 * This routine is called by the upper-layer tty layer to signal that
1294 * incoming characters should be throttled.
1295 * ------------------------------------------------------------
1296 */
1297static void rs_throttle(struct tty_struct *tty)
1298{
1299 struct esp_struct *info = tty->driver_data;
1300 unsigned long flags;
1301#ifdef SERIAL_DEBUG_THROTTLE
1302 char buf[64];
1303
1304 printk("throttle %s: %d....\n", tty_name(tty, buf),
1305 tty_chars_in_buffer(tty));
1306#endif
1307
1308 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
1309 return;
1310
1311 spin_lock_irqsave(&info->lock, flags);
1312 info->IER &= ~UART_IER_RDI;
1313 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1314 serial_out(info, UART_ESI_CMD2, info->IER);
1315 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1316 serial_out(info, UART_ESI_CMD2, 0x00);
1317 spin_unlock_irqrestore(&info->lock, flags);
1318}
1319
1320static void rs_unthrottle(struct tty_struct *tty)
1321{
1322 struct esp_struct *info = tty->driver_data;
1323 unsigned long flags;
1324#ifdef SERIAL_DEBUG_THROTTLE
1325 char buf[64];
1326
1327 printk(KERN_DEBUG "unthrottle %s: %d....\n", tty_name(tty, buf),
1328 tty_chars_in_buffer(tty));
1329#endif
1330
1331 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1332 return;
1333
1334 spin_lock_irqsave(&info->lock, flags);
1335 info->IER |= UART_IER_RDI;
1336 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1337 serial_out(info, UART_ESI_CMD2, info->IER);
1338 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1339 serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
1340 spin_unlock_irqrestore(&info->lock, flags);
1341}
1342
1343/*
1344 * ------------------------------------------------------------
1345 * rs_ioctl() and friends
1346 * ------------------------------------------------------------
1347 */
1348
1349static int get_serial_info(struct esp_struct *info,
1350 struct serial_struct __user *retinfo)
1351{
1352 struct serial_struct tmp;
1353
1354 lock_kernel();
1355 memset(&tmp, 0, sizeof(tmp));
1356 tmp.type = PORT_16550A;
1357 tmp.line = info->line;
1358 tmp.port = info->io_port;
1359 tmp.irq = info->irq;
1360 tmp.flags = info->port.flags;
1361 tmp.xmit_fifo_size = 1024;
1362 tmp.baud_base = BASE_BAUD;
1363 tmp.close_delay = info->close_delay;
1364 tmp.closing_wait = info->closing_wait;
1365 tmp.custom_divisor = info->custom_divisor;
1366 tmp.hub6 = 0;
1367 unlock_kernel();
1368 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1369 return -EFAULT;
1370 return 0;
1371}
1372
1373static int get_esp_config(struct esp_struct *info,
1374 struct hayes_esp_config __user *retinfo)
1375{
1376 struct hayes_esp_config tmp;
1377
1378 if (!retinfo)
1379 return -EFAULT;
1380
1381 memset(&tmp, 0, sizeof(tmp));
1382 lock_kernel();
1383 tmp.rx_timeout = info->config.rx_timeout;
1384 tmp.rx_trigger = info->config.rx_trigger;
1385 tmp.tx_trigger = info->config.tx_trigger;
1386 tmp.flow_off = info->config.flow_off;
1387 tmp.flow_on = info->config.flow_on;
1388 tmp.pio_threshold = info->config.pio_threshold;
1389 tmp.dma_channel = (info->stat_flags & ESP_STAT_NEVER_DMA ? 0 : dma);
1390 unlock_kernel();
1391
1392 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
1393}
1394
1395static int set_serial_info(struct esp_struct *info,
1396 struct serial_struct __user *new_info)
1397{
1398 struct serial_struct new_serial;
1399 struct esp_struct old_info;
1400 unsigned int change_irq;
1401 int retval = 0;
1402 struct esp_struct *current_async;
1403
1404 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1405 return -EFAULT;
1406 old_info = *info;
1407
1408 if ((new_serial.type != PORT_16550A) ||
1409 (new_serial.hub6) ||
1410 (info->io_port != new_serial.port) ||
1411 (new_serial.baud_base != BASE_BAUD) ||
1412 (new_serial.irq > 15) ||
1413 (new_serial.irq < 2) ||
1414 (new_serial.irq == 6) ||
1415 (new_serial.irq == 8) ||
1416 (new_serial.irq == 13))
1417 return -EINVAL;
1418
1419 change_irq = new_serial.irq != info->irq;
1420
1421 if (change_irq && (info->line % 8))
1422 return -EINVAL;
1423
1424 if (!capable(CAP_SYS_ADMIN)) {
1425 if (change_irq ||
1426 (new_serial.close_delay != info->close_delay) ||
1427 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1428 (info->port.flags & ~ASYNC_USR_MASK)))
1429 return -EPERM;
1430 info->port.flags = ((info->port.flags & ~ASYNC_USR_MASK) |
1431 (new_serial.flags & ASYNC_USR_MASK));
1432 info->custom_divisor = new_serial.custom_divisor;
1433 } else {
1434 if (new_serial.irq == 2)
1435 new_serial.irq = 9;
1436
1437 if (change_irq) {
1438 current_async = ports;
1439
1440 while (current_async) {
1441 if ((current_async->line >= info->line) &&
1442 (current_async->line < (info->line + 8))) {
1443 if (current_async == info) {
1444 if (current_async->port.count > 1)
1445 return -EBUSY;
1446 } else if (current_async->port.count)
1447 return -EBUSY;
1448 }
1449
1450 current_async = current_async->next_port;
1451 }
1452 }
1453
1454 /*
1455 * OK, past this point, all the error checking has been done.
1456 * At this point, we start making changes.....
1457 */
1458
1459 info->port.flags = ((info->port.flags & ~ASYNC_FLAGS) |
1460 (new_serial.flags & ASYNC_FLAGS));
1461 info->custom_divisor = new_serial.custom_divisor;
1462 info->close_delay = new_serial.close_delay * HZ/100;
1463 info->closing_wait = new_serial.closing_wait * HZ/100;
1464
1465 if (change_irq) {
1466 /*
1467 * We need to shutdown the serial port at the old
1468 * port/irq combination.
1469 */
1470 shutdown(info);
1471
1472 current_async = ports;
1473
1474 while (current_async) {
1475 if ((current_async->line >= info->line) &&
1476 (current_async->line < (info->line + 8)))
1477 current_async->irq = new_serial.irq;
1478
1479 current_async = current_async->next_port;
1480 }
1481
1482 serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
1483 if (info->irq == 9)
1484 serial_out(info, UART_ESI_CMD2, 0x02);
1485 else
1486 serial_out(info, UART_ESI_CMD2, info->irq);
1487 }
1488 }
1489
1490 if (info->port.flags & ASYNC_INITIALIZED) {
1491 if (((old_info.port.flags & ASYNC_SPD_MASK) !=
1492 (info->port.flags & ASYNC_SPD_MASK)) ||
1493 (old_info.custom_divisor != info->custom_divisor)) {
1494 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1495 info->port.tty->alt_speed = 57600;
1496 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1497 info->port.tty->alt_speed = 115200;
1498 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1499 info->port.tty->alt_speed = 230400;
1500 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1501 info->port.tty->alt_speed = 460800;
1502 change_speed(info);
1503 }
1504 } else
1505 retval = startup(info);
1506
1507 return retval;
1508}
1509
1510static int set_esp_config(struct esp_struct *info,
1511 struct hayes_esp_config __user *new_info)
1512{
1513 struct hayes_esp_config new_config;
1514 unsigned int change_dma;
1515 int retval = 0;
1516 struct esp_struct *current_async;
1517 unsigned long flags;
1518
1519 /* Perhaps a non-sysadmin user should be able to do some of these */
1520 /* operations. I haven't decided yet. */
1521
1522 if (!capable(CAP_SYS_ADMIN))
1523 return -EPERM;
1524
1525 if (copy_from_user(&new_config, new_info, sizeof(new_config)))
1526 return -EFAULT;
1527
1528 if ((new_config.flow_on >= new_config.flow_off) ||
1529 (new_config.rx_trigger < 1) ||
1530 (new_config.tx_trigger < 1) ||
1531 (new_config.flow_off < 1) ||
1532 (new_config.flow_on < 1) ||
1533 (new_config.rx_trigger > 1023) ||
1534 (new_config.tx_trigger > 1023) ||
1535 (new_config.flow_off > 1023) ||
1536 (new_config.flow_on > 1023) ||
1537 (new_config.pio_threshold < 0) ||
1538 (new_config.pio_threshold > 1024))
1539 return -EINVAL;
1540
1541 if ((new_config.dma_channel != 1) && (new_config.dma_channel != 3))
1542 new_config.dma_channel = 0;
1543
1544 if (info->stat_flags & ESP_STAT_NEVER_DMA)
1545 change_dma = new_config.dma_channel;
1546 else
1547 change_dma = (new_config.dma_channel != dma);
1548
1549 if (change_dma) {
1550 if (new_config.dma_channel) {
1551 /* PIO mode to DMA mode transition OR */
1552 /* change current DMA channel */
1553 current_async = ports;
1554
1555 while (current_async) {
1556 if (current_async == info) {
1557 if (current_async->port.count > 1)
1558 return -EBUSY;
1559 } else if (current_async->port.count)
1560 return -EBUSY;
1561
1562 current_async = current_async->next_port;
1563 }
1564
1565 shutdown(info);
1566 dma = new_config.dma_channel;
1567 info->stat_flags &= ~ESP_STAT_NEVER_DMA;
1568
1569 /* all ports must use the same DMA channel */
1570
1571 spin_lock_irqsave(&info->lock, flags);
1572 current_async = ports;
1573
1574 while (current_async) {
1575 esp_basic_init(current_async);
1576 current_async = current_async->next_port;
1577 }
1578 spin_unlock_irqrestore(&info->lock, flags);
1579 } else {
1580 /* DMA mode to PIO mode only */
1581 if (info->port.count > 1)
1582 return -EBUSY;
1583
1584 shutdown(info);
1585 spin_lock_irqsave(&info->lock, flags);
1586 info->stat_flags |= ESP_STAT_NEVER_DMA;
1587 esp_basic_init(info);
1588 spin_unlock_irqrestore(&info->lock, flags);
1589 }
1590 }
1591
1592 info->config.pio_threshold = new_config.pio_threshold;
1593
1594 if ((new_config.flow_off != info->config.flow_off) ||
1595 (new_config.flow_on != info->config.flow_on)) {
1596 info->config.flow_off = new_config.flow_off;
1597 info->config.flow_on = new_config.flow_on;
1598
1599 spin_lock_irqsave(&info->lock, flags);
1600 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1601 serial_out(info, UART_ESI_CMD2, new_config.flow_off >> 8);
1602 serial_out(info, UART_ESI_CMD2, new_config.flow_off);
1603 serial_out(info, UART_ESI_CMD2, new_config.flow_on >> 8);
1604 serial_out(info, UART_ESI_CMD2, new_config.flow_on);
1605 spin_unlock_irqrestore(&info->lock, flags);
1606 }
1607
1608 if ((new_config.rx_trigger != info->config.rx_trigger) ||
1609 (new_config.tx_trigger != info->config.tx_trigger)) {
1610 info->config.rx_trigger = new_config.rx_trigger;
1611 info->config.tx_trigger = new_config.tx_trigger;
1612 spin_lock_irqsave(&info->lock, flags);
1613 serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
1614 serial_out(info, UART_ESI_CMD2,
1615 new_config.rx_trigger >> 8);
1616 serial_out(info, UART_ESI_CMD2, new_config.rx_trigger);
1617 serial_out(info, UART_ESI_CMD2,
1618 new_config.tx_trigger >> 8);
1619 serial_out(info, UART_ESI_CMD2, new_config.tx_trigger);
1620 spin_unlock_irqrestore(&info->lock, flags);
1621 }
1622
1623 if (new_config.rx_timeout != info->config.rx_timeout) {
1624 info->config.rx_timeout = new_config.rx_timeout;
1625 spin_lock_irqsave(&info->lock, flags);
1626
1627 if (info->IER & UART_IER_RDI) {
1628 serial_out(info, UART_ESI_CMD1,
1629 ESI_SET_RX_TIMEOUT);
1630 serial_out(info, UART_ESI_CMD2,
1631 new_config.rx_timeout);
1632 }
1633
1634 spin_unlock_irqrestore(&info->lock, flags);
1635 }
1636
1637 if (!(info->port.flags & ASYNC_INITIALIZED))
1638 retval = startup(info);
1639
1640 return retval;
1641}
1642
1643/*
1644 * get_lsr_info - get line status register info
1645 *
1646 * Purpose: Let user call ioctl() to get info when the UART physically
1647 * is emptied. On bus types like RS485, the transmitter must
1648 * release the bus after transmitting. This must be done when
1649 * the transmit shift register is empty, not be done when the
1650 * transmit holding register is empty. This functionality
1651 * allows an RS485 driver to be written in user space.
1652 */
1653static int get_lsr_info(struct esp_struct *info, unsigned int __user *value)
1654{
1655 unsigned char status;
1656 unsigned int result;
1657 unsigned long flags;
1658
1659 spin_lock_irqsave(&info->lock, flags);
1660 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1661 status = serial_in(info, UART_ESI_STAT1);
1662 spin_unlock_irqrestore(&info->lock, flags);
1663 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1664 return put_user(result, value);
1665}
1666
1667
1668static int esp_tiocmget(struct tty_struct *tty, struct file *file)
1669{
1670 struct esp_struct *info = tty->driver_data;
1671 unsigned char control, status;
1672 unsigned long flags;
1673
1674 if (serial_paranoia_check(info, tty->name, __func__))
1675 return -ENODEV;
1676 if (tty->flags & (1 << TTY_IO_ERROR))
1677 return -EIO;
1678
1679 control = info->MCR;
1680
1681 spin_lock_irqsave(&info->lock, flags);
1682 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1683 status = serial_in(info, UART_ESI_STAT2);
1684 spin_unlock_irqrestore(&info->lock, flags);
1685
1686 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1687 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1688 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1689 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1690 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1691 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1692}
1693
1694static int esp_tiocmset(struct tty_struct *tty, struct file *file,
1695 unsigned int set, unsigned int clear)
1696{
1697 struct esp_struct *info = tty->driver_data;
1698 unsigned long flags;
1699
1700 if (serial_paranoia_check(info, tty->name, __func__))
1701 return -ENODEV;
1702 if (tty->flags & (1 << TTY_IO_ERROR))
1703 return -EIO;
1704
1705 spin_lock_irqsave(&info->lock, flags);
1706
1707 if (set & TIOCM_RTS)
1708 info->MCR |= UART_MCR_RTS;
1709 if (set & TIOCM_DTR)
1710 info->MCR |= UART_MCR_DTR;
1711
1712 if (clear & TIOCM_RTS)
1713 info->MCR &= ~UART_MCR_RTS;
1714 if (clear & TIOCM_DTR)
1715 info->MCR &= ~UART_MCR_DTR;
1716
1717 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1718 serial_out(info, UART_ESI_CMD2, UART_MCR);
1719 serial_out(info, UART_ESI_CMD2, info->MCR);
1720
1721 spin_unlock_irqrestore(&info->lock, flags);
1722 return 0;
1723}
1724
1725/*
1726 * rs_break() --- routine which turns the break handling on or off
1727 */
1728static int esp_break(struct tty_struct *tty, int break_state)
1729{
1730 struct esp_struct *info = tty->driver_data;
1731 unsigned long flags;
1732
1733 if (serial_paranoia_check(info, tty->name, "esp_break"))
1734 return -EINVAL;
1735
1736 if (break_state == -1) {
1737 spin_lock_irqsave(&info->lock, flags);
1738 serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1739 serial_out(info, UART_ESI_CMD2, 0x01);
1740 spin_unlock_irqrestore(&info->lock, flags);
1741
1742 /* FIXME - new style wait needed here */
1743 interruptible_sleep_on(&info->break_wait);
1744 } else {
1745 spin_lock_irqsave(&info->lock, flags);
1746 serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1747 serial_out(info, UART_ESI_CMD2, 0x00);
1748 spin_unlock_irqrestore(&info->lock, flags);
1749 }
1750 return 0;
1751}
1752
1753static int rs_ioctl(struct tty_struct *tty, struct file *file,
1754 unsigned int cmd, unsigned long arg)
1755{
1756 struct esp_struct *info = tty->driver_data;
1757 struct async_icount cprev, cnow; /* kernel counter temps */
1758 struct serial_icounter_struct __user *p_cuser; /* user space */
1759 void __user *argp = (void __user *)arg;
1760 unsigned long flags;
1761 int ret;
1762
1763 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1764 return -ENODEV;
1765
1766 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1767 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1768 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT) &&
1769 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT) &&
1770 (cmd != TIOCGHAYESESP) && (cmd != TIOCSHAYESESP)) {
1771 if (tty->flags & (1 << TTY_IO_ERROR))
1772 return -EIO;
1773 }
1774
1775 switch (cmd) {
1776 case TIOCGSERIAL:
1777 return get_serial_info(info, argp);
1778 case TIOCSSERIAL:
1779 lock_kernel();
1780 ret = set_serial_info(info, argp);
1781 unlock_kernel();
1782 return ret;
1783 case TIOCSERGWILD:
1784 return put_user(0L, (unsigned long __user *)argp);
1785 case TIOCSERGETLSR: /* Get line status register */
1786 return get_lsr_info(info, argp);
1787 case TIOCSERSWILD:
1788 if (!capable(CAP_SYS_ADMIN))
1789 return -EPERM;
1790 return 0;
1791 /*
1792 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1793 * - mask passed in arg for lines of interest
1794 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1795 * Caller should use TIOCGICOUNT to see which one it was
1796 */
1797 case TIOCMIWAIT:
1798 spin_lock_irqsave(&info->lock, flags);
1799 cprev = info->icount; /* note the counters on entry */
1800 spin_unlock_irqrestore(&info->lock, flags);
1801 while (1) {
1802 /* FIXME: convert to new style wakeup */
1803 interruptible_sleep_on(&info->port.delta_msr_wait);
1804 /* see if a signal did it */
1805 if (signal_pending(current))
1806 return -ERESTARTSYS;
1807 spin_lock_irqsave(&info->lock, flags);
1808 cnow = info->icount; /* atomic copy */
1809 spin_unlock_irqrestore(&info->lock, flags);
1810 if (cnow.rng == cprev.rng &&
1811 cnow.dsr == cprev.dsr &&
1812 cnow.dcd == cprev.dcd &&
1813 cnow.cts == cprev.cts)
1814 return -EIO; /* no change => error */
1815 if (((arg & TIOCM_RNG) &&
1816 (cnow.rng != cprev.rng)) ||
1817 ((arg & TIOCM_DSR) &&
1818 (cnow.dsr != cprev.dsr)) ||
1819 ((arg & TIOCM_CD) &&
1820 (cnow.dcd != cprev.dcd)) ||
1821 ((arg & TIOCM_CTS) &&
1822 (cnow.cts != cprev.cts))) {
1823 return 0;
1824 }
1825 cprev = cnow;
1826 }
1827 /* NOTREACHED */
1828 /*
1829 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1830 * Return: write counters to the user passed counter struct
1831 * NB: both 1->0 and 0->1 transitions are counted except for
1832 * RI where only 0->1 is counted.
1833 */
1834 case TIOCGICOUNT:
1835 spin_lock_irqsave(&info->lock, flags);
1836 cnow = info->icount;
1837 spin_unlock_irqrestore(&info->lock, flags);
1838 p_cuser = argp;
1839 if (put_user(cnow.cts, &p_cuser->cts) ||
1840 put_user(cnow.dsr, &p_cuser->dsr) ||
1841 put_user(cnow.rng, &p_cuser->rng) ||
1842 put_user(cnow.dcd, &p_cuser->dcd))
1843 return -EFAULT;
1844 return 0;
1845 case TIOCGHAYESESP:
1846 return get_esp_config(info, argp);
1847 case TIOCSHAYESESP:
1848 lock_kernel();
1849 ret = set_esp_config(info, argp);
1850 unlock_kernel();
1851 return ret;
1852 default:
1853 return -ENOIOCTLCMD;
1854 }
1855 return 0;
1856}
1857
1858static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1859{
1860 struct esp_struct *info = tty->driver_data;
1861 unsigned long flags;
1862
1863 change_speed(info);
1864
1865 spin_lock_irqsave(&info->lock, flags);
1866
1867 /* Handle transition to B0 status */
1868 if ((old_termios->c_cflag & CBAUD) &&
1869 !(tty->termios->c_cflag & CBAUD)) {
1870 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1871 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1872 serial_out(info, UART_ESI_CMD2, UART_MCR);
1873 serial_out(info, UART_ESI_CMD2, info->MCR);
1874 }
1875
1876 /* Handle transition away from B0 status */
1877 if (!(old_termios->c_cflag & CBAUD) &&
1878 (tty->termios->c_cflag & CBAUD)) {
1879 info->MCR |= (UART_MCR_DTR | UART_MCR_RTS);
1880 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1881 serial_out(info, UART_ESI_CMD2, UART_MCR);
1882 serial_out(info, UART_ESI_CMD2, info->MCR);
1883 }
1884
1885 spin_unlock_irqrestore(&info->lock, flags);
1886
1887 /* Handle turning of CRTSCTS */
1888 if ((old_termios->c_cflag & CRTSCTS) &&
1889 !(tty->termios->c_cflag & CRTSCTS)) {
1890 rs_start(tty);
1891 }
1892}
1893
1894/*
1895 * ------------------------------------------------------------
1896 * rs_close()
1897 *
1898 * This routine is called when the serial port gets closed. First, we
1899 * wait for the last remaining data to be sent. Then, we unlink its
1900 * async structure from the interrupt chain if necessary, and we free
1901 * that IRQ if nothing is left in the chain.
1902 * ------------------------------------------------------------
1903 */
1904static void rs_close(struct tty_struct *tty, struct file *filp)
1905{
1906 struct esp_struct *info = tty->driver_data;
1907 unsigned long flags;
1908
1909 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1910 return;
1911
1912 spin_lock_irqsave(&info->lock, flags);
1913
1914 if (tty_hung_up_p(filp)) {
1915 DBG_CNT("before DEC-hung");
1916 goto out;
1917 }
1918
1919#ifdef SERIAL_DEBUG_OPEN
1920 printk(KERN_DEBUG "rs_close ttys%d, count = %d\n",
1921 info->line, info->port.count);
1922#endif
1923 if (tty->count == 1 && info->port.count != 1) {
1924 /*
1925 * Uh, oh. tty->count is 1, which means that the tty
1926 * structure will be freed. Info->count should always
1927 * be one in these conditions. If it's greater than
1928 * one, we've got real problems, since it means the
1929 * serial port won't be shutdown.
1930 */
1931 printk(KERN_DEBUG "rs_close: bad serial port count; tty->count is 1, info->port.count is %d\n", info->port.count);
1932 info->port.count = 1;
1933 }
1934 if (--info->port.count < 0) {
1935 printk(KERN_ERR "rs_close: bad serial port count for ttys%d: %d\n",
1936 info->line, info->port.count);
1937 info->port.count = 0;
1938 }
1939 if (info->port.count) {
1940 DBG_CNT("before DEC-2");
1941 goto out;
1942 }
1943 info->port.flags |= ASYNC_CLOSING;
1944
1945 spin_unlock_irqrestore(&info->lock, flags);
1946 /*
1947 * Now we wait for the transmit buffer to clear; and we notify
1948 * the line discipline to only process XON/XOFF characters.
1949 */
1950 tty->closing = 1;
1951 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1952 tty_wait_until_sent(tty, info->closing_wait);
1953 /*
1954 * At this point we stop accepting input. To do this, we
1955 * disable the receive line status interrupts, and tell the
1956 * interrupt driver to stop checking the data ready bit in the
1957 * line status register.
1958 */
1959 /* info->IER &= ~UART_IER_RLSI; */
1960 info->IER &= ~UART_IER_RDI;
1961 info->read_status_mask &= ~UART_LSR_DR;
1962 if (info->port.flags & ASYNC_INITIALIZED) {
1963
1964 spin_lock_irqsave(&info->lock, flags);
1965 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1966 serial_out(info, UART_ESI_CMD2, info->IER);
1967
1968 /* disable receive timeout */
1969 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1970 serial_out(info, UART_ESI_CMD2, 0x00);
1971
1972 spin_unlock_irqrestore(&info->lock, flags);
1973
1974 /*
1975 * Before we drop DTR, make sure the UART transmitter
1976 * has completely drained; this is especially
1977 * important if there is a transmit FIFO!
1978 */
1979 rs_wait_until_sent(tty, info->timeout);
1980 }
1981 shutdown(info);
1982 rs_flush_buffer(tty);
1983 tty_ldisc_flush(tty);
1984 tty->closing = 0;
1985 info->port.tty = NULL;
1986
1987 if (info->port.blocked_open) {
1988 if (info->close_delay)
1989 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1990 wake_up_interruptible(&info->port.open_wait);
1991 }
1992 info->port.flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1993 wake_up_interruptible(&info->port.close_wait);
1994 return;
1995
1996out:
1997 spin_unlock_irqrestore(&info->lock, flags);
1998}
1999
2000static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
2001{
2002 struct esp_struct *info = tty->driver_data;
2003 unsigned long orig_jiffies, char_time;
2004 unsigned long flags;
2005
2006 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
2007 return;
2008
2009 orig_jiffies = jiffies;
2010 char_time = ((info->timeout - HZ / 50) / 1024) / 5;
2011
2012 if (!char_time)
2013 char_time = 1;
2014
2015 spin_lock_irqsave(&info->lock, flags);
2016 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2017 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2018
2019 while ((serial_in(info, UART_ESI_STAT1) != 0x03) ||
2020 (serial_in(info, UART_ESI_STAT2) != 0xff)) {
2021
2022 spin_unlock_irqrestore(&info->lock, flags);
2023 msleep_interruptible(jiffies_to_msecs(char_time));
2024
2025 if (signal_pending(current))
2026 return;
2027
2028 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2029 return;
2030
2031 spin_lock_irqsave(&info->lock, flags);
2032 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2033 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2034 }
2035 spin_unlock_irqrestore(&info->lock, flags);
2036 set_current_state(TASK_RUNNING);
2037}
2038
2039/*
2040 * esp_hangup() --- called by tty_hangup() when a hangup is signaled.
2041 */
2042static void esp_hangup(struct tty_struct *tty)
2043{
2044 struct esp_struct *info = tty->driver_data;
2045
2046 if (serial_paranoia_check(info, tty->name, "esp_hangup"))
2047 return;
2048
2049 rs_flush_buffer(tty);
2050 shutdown(info);
2051 info->port.count = 0;
2052 info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
2053 info->port.tty = NULL;
2054 wake_up_interruptible(&info->port.open_wait);
2055}
2056
2057static int esp_carrier_raised(struct tty_port *port)
2058{
2059 struct esp_struct *info = container_of(port, struct esp_struct, port);
2060 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
2061 if (serial_in(info, UART_ESI_STAT2) & UART_MSR_DCD)
2062 return 1;
2063 return 0;
2064}
2065
2066/*
2067 * ------------------------------------------------------------
2068 * esp_open() and friends
2069 * ------------------------------------------------------------
2070 */
2071static int block_til_ready(struct tty_struct *tty, struct file *filp,
2072 struct esp_struct *info)
2073{
2074 DECLARE_WAITQUEUE(wait, current);
2075 int retval;
2076 int do_clocal = 0;
2077 unsigned long flags;
2078 int cd;
2079 struct tty_port *port = &info->port;
2080
2081 /*
2082 * If the device is in the middle of being closed, then block
2083 * until it's done, and then try again.
2084 */
2085 if (tty_hung_up_p(filp) ||
2086 (port->flags & ASYNC_CLOSING)) {
2087 if (port->flags & ASYNC_CLOSING)
2088 interruptible_sleep_on(&port->close_wait);
2089#ifdef SERIAL_DO_RESTART
2090 if (port->flags & ASYNC_HUP_NOTIFY)
2091 return -EAGAIN;
2092 else
2093 return -ERESTARTSYS;
2094#else
2095 return -EAGAIN;
2096#endif
2097 }
2098
2099 /*
2100 * If non-blocking mode is set, or the port is not enabled,
2101 * then make the check up front and then exit.
2102 */
2103 if ((filp->f_flags & O_NONBLOCK) ||
2104 (tty->flags & (1 << TTY_IO_ERROR))) {
2105 port->flags |= ASYNC_NORMAL_ACTIVE;
2106 return 0;
2107 }
2108
2109 if (tty->termios->c_cflag & CLOCAL)
2110 do_clocal = 1;
2111
2112 /*
2113 * Block waiting for the carrier detect and the line to become
2114 * free (i.e., not in use by the callout). While we are in
2115 * this loop, port->count is dropped by one, so that
2116 * rs_close() knows when to free things. We restore it upon
2117 * exit, either normal or abnormal.
2118 */
2119 retval = 0;
2120 add_wait_queue(&port->open_wait, &wait);
2121#ifdef SERIAL_DEBUG_OPEN
2122 printk(KERN_DEBUG "block_til_ready before block: ttys%d, count = %d\n",
2123 info->line, port->count);
2124#endif
2125 spin_lock_irqsave(&info->lock, flags);
2126 if (!tty_hung_up_p(filp))
2127 port->count--;
2128 port->blocked_open++;
2129 while (1) {
2130 if ((tty->termios->c_cflag & CBAUD)) {
2131 unsigned int scratch;
2132
2133 serial_out(info, UART_ESI_CMD1, ESI_READ_UART);
2134 serial_out(info, UART_ESI_CMD2, UART_MCR);
2135 scratch = serial_in(info, UART_ESI_STAT1);
2136 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2137 serial_out(info, UART_ESI_CMD2, UART_MCR);
2138 serial_out(info, UART_ESI_CMD2,
2139 scratch | UART_MCR_DTR | UART_MCR_RTS);
2140 }
2141 set_current_state(TASK_INTERRUPTIBLE);
2142 if (tty_hung_up_p(filp) ||
2143 !(port->flags & ASYNC_INITIALIZED)) {
2144#ifdef SERIAL_DO_RESTART
2145 if (port->flags & ASYNC_HUP_NOTIFY)
2146 retval = -EAGAIN;
2147 else
2148 retval = -ERESTARTSYS;
2149#else
2150 retval = -EAGAIN;
2151#endif
2152 break;
2153 }
2154
2155 cd = tty_port_carrier_raised(port);
2156
2157 if (!(port->flags & ASYNC_CLOSING) &&
2158 (do_clocal))
2159 break;
2160 if (signal_pending(current)) {
2161 retval = -ERESTARTSYS;
2162 break;
2163 }
2164#ifdef SERIAL_DEBUG_OPEN
2165 printk(KERN_DEBUG "block_til_ready blocking: ttys%d, count = %d\n",
2166 info->line, port->count);
2167#endif
2168 spin_unlock_irqrestore(&info->lock, flags);
2169 schedule();
2170 spin_lock_irqsave(&info->lock, flags);
2171 }
2172 set_current_state(TASK_RUNNING);
2173 remove_wait_queue(&port->open_wait, &wait);
2174 if (!tty_hung_up_p(filp))
2175 port->count++;
2176 port->blocked_open--;
2177 spin_unlock_irqrestore(&info->lock, flags);
2178#ifdef SERIAL_DEBUG_OPEN
2179 printk(KERN_DEBUG "block_til_ready after blocking: ttys%d, count = %d\n",
2180 info->line, port->count);
2181#endif
2182 if (retval)
2183 return retval;
2184 port->flags |= ASYNC_NORMAL_ACTIVE;
2185 return 0;
2186}
2187
2188/*
2189 * This routine is called whenever a serial port is opened. It
2190 * enables interrupts for a serial port, linking in its async structure into
2191 * the IRQ chain. It also performs the serial-specific
2192 * initialization for the tty structure.
2193 */
2194static int esp_open(struct tty_struct *tty, struct file *filp)
2195{
2196 struct esp_struct *info;
2197 int retval, line;
2198 unsigned long flags;
2199
2200 line = tty->index;
2201 if ((line < 0) || (line >= NR_PORTS))
2202 return -ENODEV;
2203
2204 /* find the port in the chain */
2205
2206 info = ports;
2207
2208 while (info && (info->line != line))
2209 info = info->next_port;
2210
2211 if (!info) {
2212 serial_paranoia_check(info, tty->name, "esp_open");
2213 return -ENODEV;
2214 }
2215
2216#ifdef SERIAL_DEBUG_OPEN
2217 printk(KERN_DEBUG "esp_open %s, count = %d\n", tty->name, info->port.count);
2218#endif
2219 spin_lock_irqsave(&info->lock, flags);
2220 info->port.count++;
2221 tty->driver_data = info;
2222 info->port.tty = tty;
2223
2224 spin_unlock_irqrestore(&info->lock, flags);
2225
2226 /*
2227 * Start up serial port
2228 */
2229 retval = startup(info);
2230 if (retval)
2231 return retval;
2232
2233 retval = block_til_ready(tty, filp, info);
2234 if (retval) {
2235#ifdef SERIAL_DEBUG_OPEN
2236 printk(KERN_DEBUG "esp_open returning after block_til_ready with %d\n",
2237 retval);
2238#endif
2239 return retval;
2240 }
2241#ifdef SERIAL_DEBUG_OPEN
2242 printk(KERN_DEBUG "esp_open %s successful...", tty->name);
2243#endif
2244 return 0;
2245}
2246
2247/*
2248 * ---------------------------------------------------------------------
2249 * espserial_init() and friends
2250 *
2251 * espserial_init() is called at boot-time to initialize the serial driver.
2252 * ---------------------------------------------------------------------
2253 */
2254
2255/*
2256 * This routine prints out the appropriate serial driver version
2257 * number, and identifies which options were configured into this
2258 * driver.
2259 */
2260
2261static void __init show_serial_version(void)
2262{
2263 printk(KERN_INFO "%s version %s (DMA %u)\n",
2264 serial_name, serial_version, dma);
2265}
2266
2267/*
2268 * This routine is called by espserial_init() to initialize a specific serial
2269 * port.
2270 */
2271static int autoconfig(struct esp_struct *info)
2272{
2273 int port_detected = 0;
2274 unsigned long flags;
2275
2276 if (!request_region(info->io_port, REGION_SIZE, "esp serial"))
2277 return -EIO;
2278
2279 spin_lock_irqsave(&info->lock, flags);
2280 /*
2281 * Check for ESP card
2282 */
2283
2284 if (serial_in(info, UART_ESI_BASE) == 0xf3) {
2285 serial_out(info, UART_ESI_CMD1, 0x00);
2286 serial_out(info, UART_ESI_CMD1, 0x01);
2287
2288 if ((serial_in(info, UART_ESI_STAT2) & 0x70) == 0x20) {
2289 port_detected = 1;
2290
2291 if (!(info->irq)) {
2292 serial_out(info, UART_ESI_CMD1, 0x02);
2293
2294 if (serial_in(info, UART_ESI_STAT1) & 0x01)
2295 info->irq = 3;
2296 else
2297 info->irq = 4;
2298 }
2299
2300
2301 /* put card in enhanced mode */
2302 /* this prevents access through */
2303 /* the "old" IO ports */
2304 esp_basic_init(info);
2305
2306 /* clear out MCR */
2307 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2308 serial_out(info, UART_ESI_CMD2, UART_MCR);
2309 serial_out(info, UART_ESI_CMD2, 0x00);
2310 }
2311 }
2312 if (!port_detected)
2313 release_region(info->io_port, REGION_SIZE);
2314
2315 spin_unlock_irqrestore(&info->lock, flags);
2316 return (port_detected);
2317}
2318
2319static const struct tty_operations esp_ops = {
2320 .open = esp_open,
2321 .close = rs_close,
2322 .write = rs_write,
2323 .put_char = rs_put_char,
2324 .flush_chars = rs_flush_chars,
2325 .write_room = rs_write_room,
2326 .chars_in_buffer = rs_chars_in_buffer,
2327 .flush_buffer = rs_flush_buffer,
2328 .ioctl = rs_ioctl,
2329 .throttle = rs_throttle,
2330 .unthrottle = rs_unthrottle,
2331 .set_termios = rs_set_termios,
2332 .stop = rs_stop,
2333 .start = rs_start,
2334 .hangup = esp_hangup,
2335 .break_ctl = esp_break,
2336 .wait_until_sent = rs_wait_until_sent,
2337 .tiocmget = esp_tiocmget,
2338 .tiocmset = esp_tiocmset,
2339};
2340
2341static const struct tty_port_operations esp_port_ops = {
2342 .esp_carrier_raised,
2343};
2344
2345/*
2346 * The serial driver boot-time initialization code!
2347 */
2348static int __init espserial_init(void)
2349{
2350 int i, offset;
2351 struct esp_struct *info;
2352 struct esp_struct *last_primary = NULL;
2353 int esp[] = { 0x100, 0x140, 0x180, 0x200, 0x240, 0x280, 0x300, 0x380 };
2354
2355 esp_driver = alloc_tty_driver(NR_PORTS);
2356 if (!esp_driver)
2357 return -ENOMEM;
2358
2359 for (i = 0; i < NR_PRIMARY; i++) {
2360 if (irq[i] != 0) {
2361 if ((irq[i] < 2) || (irq[i] > 15) || (irq[i] == 6) ||
2362 (irq[i] == 8) || (irq[i] == 13))
2363 irq[i] = 0;
2364 else if (irq[i] == 2)
2365 irq[i] = 9;
2366 }
2367 }
2368
2369 if ((dma != 1) && (dma != 3))
2370 dma = 0;
2371
2372 if ((rx_trigger < 1) || (rx_trigger > 1023))
2373 rx_trigger = 768;
2374
2375 if ((tx_trigger < 1) || (tx_trigger > 1023))
2376 tx_trigger = 768;
2377
2378 if ((flow_off < 1) || (flow_off > 1023))
2379 flow_off = 1016;
2380
2381 if ((flow_on < 1) || (flow_on > 1023))
2382 flow_on = 944;
2383
2384 if ((rx_timeout < 0) || (rx_timeout > 255))
2385 rx_timeout = 128;
2386
2387 if (flow_on >= flow_off)
2388 flow_on = flow_off - 1;
2389
2390 show_serial_version();
2391
2392 /* Initialize the tty_driver structure */
2393
2394 esp_driver->owner = THIS_MODULE;
2395 esp_driver->name = "ttyP";
2396 esp_driver->major = ESP_IN_MAJOR;
2397 esp_driver->minor_start = 0;
2398 esp_driver->type = TTY_DRIVER_TYPE_SERIAL;
2399 esp_driver->subtype = SERIAL_TYPE_NORMAL;
2400 esp_driver->init_termios = tty_std_termios;
2401 esp_driver->init_termios.c_cflag =
2402 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2403 esp_driver->init_termios.c_ispeed = 9600;
2404 esp_driver->init_termios.c_ospeed = 9600;
2405 esp_driver->flags = TTY_DRIVER_REAL_RAW;
2406 tty_set_operations(esp_driver, &esp_ops);
2407 if (tty_register_driver(esp_driver)) {
2408 printk(KERN_ERR "Couldn't register esp serial driver");
2409 put_tty_driver(esp_driver);
2410 return 1;
2411 }
2412
2413 info = kzalloc(sizeof(struct esp_struct), GFP_KERNEL);
2414
2415 if (!info) {
2416 printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
2417 tty_unregister_driver(esp_driver);
2418 put_tty_driver(esp_driver);
2419 return 1;
2420 }
2421
2422 spin_lock_init(&info->lock);
2423 /* rx_trigger, tx_trigger are needed by autoconfig */
2424 info->config.rx_trigger = rx_trigger;
2425 info->config.tx_trigger = tx_trigger;
2426
2427 i = 0;
2428 offset = 0;
2429
2430 do {
2431 tty_port_init(&info->port);
2432 info->port.ops = &esp_port_ops;
2433 info->io_port = esp[i] + offset;
2434 info->irq = irq[i];
2435 info->line = (i * 8) + (offset / 8);
2436
2437 if (!autoconfig(info)) {
2438 i++;
2439 offset = 0;
2440 continue;
2441 }
2442
2443 info->custom_divisor = (divisor[i] >> (offset / 2)) & 0xf;
2444 info->port.flags = STD_COM_FLAGS;
2445 if (info->custom_divisor)
2446 info->port.flags |= ASYNC_SPD_CUST;
2447 info->magic = ESP_MAGIC;
2448 info->close_delay = 5*HZ/10;
2449 info->closing_wait = 30*HZ;
2450 info->config.rx_timeout = rx_timeout;
2451 info->config.flow_on = flow_on;
2452 info->config.flow_off = flow_off;
2453 info->config.pio_threshold = pio_threshold;
2454 info->next_port = ports;
2455 init_waitqueue_head(&info->break_wait);
2456 ports = info;
2457 printk(KERN_INFO "ttyP%d at 0x%04x (irq = %d) is an ESP ",
2458 info->line, info->io_port, info->irq);
2459
2460 if (info->line % 8) {
2461 printk("secondary port\n");
2462 /* 8 port cards can't do DMA */
2463 info->stat_flags |= ESP_STAT_NEVER_DMA;
2464
2465 if (last_primary)
2466 last_primary->stat_flags |= ESP_STAT_NEVER_DMA;
2467 } else {
2468 printk("primary port\n");
2469 last_primary = info;
2470 irq[i] = info->irq;
2471 }
2472
2473 if (!dma)
2474 info->stat_flags |= ESP_STAT_NEVER_DMA;
2475
2476 info = kzalloc(sizeof(struct esp_struct), GFP_KERNEL);
2477 if (!info) {
2478 printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
2479 /* allow use of the already detected ports */
2480 return 0;
2481 }
2482
2483 spin_lock_init(&info->lock);
2484 /* rx_trigger, tx_trigger are needed by autoconfig */
2485 info->config.rx_trigger = rx_trigger;
2486 info->config.tx_trigger = tx_trigger;
2487
2488 if (offset == 56) {
2489 i++;
2490 offset = 0;
2491 } else {
2492 offset += 8;
2493 }
2494 } while (i < NR_PRIMARY);
2495
2496 /* free the last port memory allocation */
2497 kfree(info);
2498
2499 return 0;
2500}
2501
2502static void __exit espserial_exit(void)
2503{
2504 int e1;
2505 struct esp_struct *temp_async;
2506 struct esp_pio_buffer *pio_buf;
2507
2508 e1 = tty_unregister_driver(esp_driver);
2509 if (e1)
2510 printk(KERN_ERR "esp: failed to unregister driver (%d)\n", e1);
2511 put_tty_driver(esp_driver);
2512
2513 while (ports) {
2514 if (ports->io_port)
2515 release_region(ports->io_port, REGION_SIZE);
2516 temp_async = ports->next_port;
2517 kfree(ports);
2518 ports = temp_async;
2519 }
2520
2521 if (dma_buffer)
2522 free_pages((unsigned long)dma_buffer,
2523 get_order(DMA_BUFFER_SZ));
2524
2525 while (free_pio_buf) {
2526 pio_buf = free_pio_buf->next;
2527 kfree(free_pio_buf);
2528 free_pio_buf = pio_buf;
2529 }
2530}
2531
2532module_init(espserial_init);
2533module_exit(espserial_exit);
diff --git a/drivers/char/isicom.c b/drivers/char/isicom.c
index 426bfdd7f3e0..300d5bd6cd06 100644
--- a/drivers/char/isicom.c
+++ b/drivers/char/isicom.c
@@ -793,35 +793,30 @@ static inline void isicom_setup_board(struct isi_board *bp)
793{ 793{
794 int channel; 794 int channel;
795 struct isi_port *port; 795 struct isi_port *port;
796 unsigned long flags;
797 796
798 spin_lock_irqsave(&bp->card_lock, flags); 797 bp->count++;
799 if (bp->status & BOARD_ACTIVE) { 798 if (!(bp->status & BOARD_INIT)) {
800 spin_unlock_irqrestore(&bp->card_lock, flags); 799 port = bp->ports;
801 return; 800 for (channel = 0; channel < bp->port_count; channel++, port++)
801 drop_dtr_rts(port);
802 } 802 }
803 port = bp->ports; 803 bp->status |= BOARD_ACTIVE | BOARD_INIT;
804 bp->status |= BOARD_ACTIVE;
805 for (channel = 0; channel < bp->port_count; channel++, port++)
806 drop_dtr_rts(port);
807 spin_unlock_irqrestore(&bp->card_lock, flags);
808} 804}
809 805
810static int isicom_setup_port(struct tty_struct *tty) 806/* Activate and thus setup board are protected from races against shutdown
807 by the tty_port mutex */
808
809static int isicom_activate(struct tty_port *tport, struct tty_struct *tty)
811{ 810{
812 struct isi_port *port = tty->driver_data; 811 struct isi_port *port = container_of(tport, struct isi_port, port);
813 struct isi_board *card = port->card; 812 struct isi_board *card = port->card;
814 unsigned long flags; 813 unsigned long flags;
815 814
816 if (port->port.flags & ASYNC_INITIALIZED) 815 if (tty_port_alloc_xmit_buf(tport) < 0)
817 return 0;
818 if (tty_port_alloc_xmit_buf(&port->port) < 0)
819 return -ENOMEM; 816 return -ENOMEM;
820 817
821 spin_lock_irqsave(&card->card_lock, flags); 818 spin_lock_irqsave(&card->card_lock, flags);
822 clear_bit(TTY_IO_ERROR, &tty->flags); 819 isicom_setup_board(card);
823 if (port->port.count == 1)
824 card->count++;
825 820
826 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0; 821 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
827 822
@@ -832,9 +827,7 @@ static int isicom_setup_port(struct tty_struct *tty)
832 outw(((ISICOM_KILLTX | ISICOM_KILLRX) << 8) | 0x06, card->base); 827 outw(((ISICOM_KILLTX | ISICOM_KILLRX) << 8) | 0x06, card->base);
833 InterruptTheCard(card->base); 828 InterruptTheCard(card->base);
834 } 829 }
835
836 isicom_config_port(tty); 830 isicom_config_port(tty);
837 port->port.flags |= ASYNC_INITIALIZED;
838 spin_unlock_irqrestore(&card->card_lock, flags); 831 spin_unlock_irqrestore(&card->card_lock, flags);
839 832
840 return 0; 833 return 0;
@@ -871,85 +864,37 @@ static struct tty_port *isicom_find_port(struct tty_struct *tty)
871 864
872 return &port->port; 865 return &port->port;
873} 866}
874 867
875static int isicom_open(struct tty_struct *tty, struct file *filp) 868static int isicom_open(struct tty_struct *tty, struct file *filp)
876{ 869{
877 struct isi_port *port; 870 struct isi_port *port;
878 struct isi_board *card; 871 struct isi_board *card;
879 struct tty_port *tport; 872 struct tty_port *tport;
880 int error = 0;
881 873
882 tport = isicom_find_port(tty); 874 tport = isicom_find_port(tty);
883 if (tport == NULL) 875 if (tport == NULL)
884 return -ENODEV; 876 return -ENODEV;
885 port = container_of(tport, struct isi_port, port); 877 port = container_of(tport, struct isi_port, port);
886 card = &isi_card[BOARD(tty->index)]; 878 card = &isi_card[BOARD(tty->index)];
887 isicom_setup_board(card);
888 879
889 /* FIXME: locking on port.count etc */ 880 return tty_port_open(tport, tty, filp);
890 port->port.count++;
891 tty->driver_data = port;
892 tty_port_tty_set(&port->port, tty);
893 /* FIXME: Locking on Initialized flag */
894 if (!test_bit(ASYNCB_INITIALIZED, &tport->flags))
895 error = isicom_setup_port(tty);
896 if (error == 0)
897 error = tty_port_block_til_ready(&port->port, tty, filp);
898 return error;
899} 881}
900 882
901/* close et all */ 883/* close et all */
902 884
903static inline void isicom_shutdown_board(struct isi_board *bp)
904{
905 if (bp->status & BOARD_ACTIVE)
906 bp->status &= ~BOARD_ACTIVE;
907}
908
909/* card->lock HAS to be held */ 885/* card->lock HAS to be held */
910static void isicom_shutdown_port(struct isi_port *port) 886static void isicom_shutdown_port(struct isi_port *port)
911{ 887{
912 struct isi_board *card = port->card; 888 struct isi_board *card = port->card;
913 struct tty_struct *tty;
914
915 tty = tty_port_tty_get(&port->port);
916
917 if (!(port->port.flags & ASYNC_INITIALIZED)) {
918 tty_kref_put(tty);
919 return;
920 }
921
922 tty_port_free_xmit_buf(&port->port);
923 port->port.flags &= ~ASYNC_INITIALIZED;
924 /* 3rd October 2000 : Vinayak P Risbud */
925 tty_port_tty_set(&port->port, NULL);
926
927 /*Fix done by Anil .S on 30-04-2001
928 remote login through isi port has dtr toggle problem
929 due to which the carrier drops before the password prompt
930 appears on the remote end. Now we drop the dtr only if the
931 HUPCL(Hangup on close) flag is set for the tty*/
932
933 if (C_HUPCL(tty))
934 /* drop dtr on this port */
935 drop_dtr(port);
936
937 /* any other port uninits */
938 if (tty)
939 set_bit(TTY_IO_ERROR, &tty->flags);
940 889
941 if (--card->count < 0) { 890 if (--card->count < 0) {
942 pr_dbg("isicom_shutdown_port: bad board(0x%lx) count %d.\n", 891 pr_dbg("isicom_shutdown_port: bad board(0x%lx) count %d.\n",
943 card->base, card->count); 892 card->base, card->count);
944 card->count = 0; 893 card->count = 0;
945 } 894 }
946 895 /* last port was closed, shutdown that board too */
947 /* last port was closed, shutdown that boad too */ 896 if (!card->count)
948 if (C_HUPCL(tty)) { 897 card->status &= BOARD_ACTIVE;
949 if (!card->count)
950 isicom_shutdown_board(card);
951 }
952 tty_kref_put(tty);
953} 898}
954 899
955static void isicom_flush_buffer(struct tty_struct *tty) 900static void isicom_flush_buffer(struct tty_struct *tty)
@@ -968,7 +913,7 @@ static void isicom_flush_buffer(struct tty_struct *tty)
968 tty_wakeup(tty); 913 tty_wakeup(tty);
969} 914}
970 915
971static void isicom_close_port(struct tty_port *port) 916static void isicom_shutdown(struct tty_port *port)
972{ 917{
973 struct isi_port *ip = container_of(port, struct isi_port, port); 918 struct isi_port *ip = container_of(port, struct isi_port, port);
974 struct isi_board *card = ip->card; 919 struct isi_board *card = ip->card;
@@ -977,12 +922,11 @@ static void isicom_close_port(struct tty_port *port)
977 /* indicate to the card that no more data can be received 922 /* indicate to the card that no more data can be received
978 on this port */ 923 on this port */
979 spin_lock_irqsave(&card->card_lock, flags); 924 spin_lock_irqsave(&card->card_lock, flags);
980 if (port->flags & ASYNC_INITIALIZED) { 925 card->port_status &= ~(1 << ip->channel);
981 card->port_status &= ~(1 << ip->channel); 926 outw(card->port_status, card->base + 0x02);
982 outw(card->port_status, card->base + 0x02);
983 }
984 isicom_shutdown_port(ip); 927 isicom_shutdown_port(ip);
985 spin_unlock_irqrestore(&card->card_lock, flags); 928 spin_unlock_irqrestore(&card->card_lock, flags);
929 tty_port_free_xmit_buf(port);
986} 930}
987 931
988static void isicom_close(struct tty_struct *tty, struct file *filp) 932static void isicom_close(struct tty_struct *tty, struct file *filp)
@@ -991,12 +935,7 @@ static void isicom_close(struct tty_struct *tty, struct file *filp)
991 struct tty_port *port = &ip->port; 935 struct tty_port *port = &ip->port;
992 if (isicom_paranoia_check(ip, tty->name, "isicom_close")) 936 if (isicom_paranoia_check(ip, tty->name, "isicom_close"))
993 return; 937 return;
994 938 tty_port_close(port, tty, filp);
995 if (tty_port_close_start(port, tty, filp) == 0)
996 return;
997 isicom_close_port(port);
998 isicom_flush_buffer(tty);
999 tty_port_close_end(port, tty);
1000} 939}
1001 940
1002/* write et all */ 941/* write et all */
@@ -1326,15 +1265,9 @@ static void isicom_start(struct tty_struct *tty)
1326static void isicom_hangup(struct tty_struct *tty) 1265static void isicom_hangup(struct tty_struct *tty)
1327{ 1266{
1328 struct isi_port *port = tty->driver_data; 1267 struct isi_port *port = tty->driver_data;
1329 unsigned long flags;
1330 1268
1331 if (isicom_paranoia_check(port, tty->name, "isicom_hangup")) 1269 if (isicom_paranoia_check(port, tty->name, "isicom_hangup"))
1332 return; 1270 return;
1333
1334 spin_lock_irqsave(&port->card->card_lock, flags);
1335 isicom_shutdown_port(port);
1336 spin_unlock_irqrestore(&port->card->card_lock, flags);
1337
1338 tty_port_hangup(&port->port); 1271 tty_port_hangup(&port->port);
1339} 1272}
1340 1273
@@ -1367,6 +1300,8 @@ static const struct tty_operations isicom_ops = {
1367static const struct tty_port_operations isicom_port_ops = { 1300static const struct tty_port_operations isicom_port_ops = {
1368 .carrier_raised = isicom_carrier_raised, 1301 .carrier_raised = isicom_carrier_raised,
1369 .dtr_rts = isicom_dtr_rts, 1302 .dtr_rts = isicom_dtr_rts,
1303 .activate = isicom_activate,
1304 .shutdown = isicom_shutdown,
1370}; 1305};
1371 1306
1372static int __devinit reset_card(struct pci_dev *pdev, 1307static int __devinit reset_card(struct pci_dev *pdev,
diff --git a/drivers/char/istallion.c b/drivers/char/istallion.c
index 402838f4083e..4cd6c527ee41 100644
--- a/drivers/char/istallion.c
+++ b/drivers/char/istallion.c
@@ -213,7 +213,6 @@ static int stli_shared;
213 * with the slave. Most of them need to be updated atomically, so always 213 * with the slave. Most of them need to be updated atomically, so always
214 * use the bit setting operations (unless protected by cli/sti). 214 * use the bit setting operations (unless protected by cli/sti).
215 */ 215 */
216#define ST_INITIALIZING 1
217#define ST_OPENING 2 216#define ST_OPENING 2
218#define ST_CLOSING 3 217#define ST_CLOSING 3
219#define ST_CMDING 4 218#define ST_CMDING 4
@@ -621,7 +620,7 @@ static int stli_brdinit(struct stlibrd *brdp);
621static int stli_startbrd(struct stlibrd *brdp); 620static int stli_startbrd(struct stlibrd *brdp);
622static ssize_t stli_memread(struct file *fp, char __user *buf, size_t count, loff_t *offp); 621static ssize_t stli_memread(struct file *fp, char __user *buf, size_t count, loff_t *offp);
623static ssize_t stli_memwrite(struct file *fp, const char __user *buf, size_t count, loff_t *offp); 622static ssize_t stli_memwrite(struct file *fp, const char __user *buf, size_t count, loff_t *offp);
624static int stli_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg); 623static long stli_memioctl(struct file *fp, unsigned int cmd, unsigned long arg);
625static void stli_brdpoll(struct stlibrd *brdp, cdkhdr_t __iomem *hdrp); 624static void stli_brdpoll(struct stlibrd *brdp, cdkhdr_t __iomem *hdrp);
626static void stli_poll(unsigned long arg); 625static void stli_poll(unsigned long arg);
627static int stli_hostcmd(struct stlibrd *brdp, struct stliport *portp); 626static int stli_hostcmd(struct stlibrd *brdp, struct stliport *portp);
@@ -704,7 +703,7 @@ static const struct file_operations stli_fsiomem = {
704 .owner = THIS_MODULE, 703 .owner = THIS_MODULE,
705 .read = stli_memread, 704 .read = stli_memread,
706 .write = stli_memwrite, 705 .write = stli_memwrite,
707 .ioctl = stli_memioctl, 706 .unlocked_ioctl = stli_memioctl,
708}; 707};
709 708
710/*****************************************************************************/ 709/*****************************************************************************/
@@ -783,13 +782,32 @@ static int stli_parsebrd(struct stlconf *confp, char **argp)
783 782
784/*****************************************************************************/ 783/*****************************************************************************/
785 784
785/*
786 * On the first open of the device setup the port hardware, and
787 * initialize the per port data structure. Since initializing the port
788 * requires several commands to the board we will need to wait for any
789 * other open that is already initializing the port.
790 *
791 * Locking: protected by the port mutex.
792 */
793
794static int stli_activate(struct tty_port *port, struct tty_struct *tty)
795{
796 struct stliport *portp = container_of(port, struct stliport, port);
797 struct stlibrd *brdp = stli_brds[portp->brdnr];
798 int rc;
799
800 if ((rc = stli_initopen(tty, brdp, portp)) >= 0)
801 clear_bit(TTY_IO_ERROR, &tty->flags);
802 wake_up_interruptible(&portp->raw_wait);
803 return rc;
804}
805
786static int stli_open(struct tty_struct *tty, struct file *filp) 806static int stli_open(struct tty_struct *tty, struct file *filp)
787{ 807{
788 struct stlibrd *brdp; 808 struct stlibrd *brdp;
789 struct stliport *portp; 809 struct stliport *portp;
790 struct tty_port *port;
791 unsigned int minordev, brdnr, portnr; 810 unsigned int minordev, brdnr, portnr;
792 int rc;
793 811
794 minordev = tty->index; 812 minordev = tty->index;
795 brdnr = MINOR2BRD(minordev); 813 brdnr = MINOR2BRD(minordev);
@@ -809,95 +827,56 @@ static int stli_open(struct tty_struct *tty, struct file *filp)
809 return -ENODEV; 827 return -ENODEV;
810 if (portp->devnr < 1) 828 if (portp->devnr < 1)
811 return -ENODEV; 829 return -ENODEV;
812 port = &portp->port; 830 return tty_port_open(&portp->port, tty, filp);
813
814/*
815 * On the first open of the device setup the port hardware, and
816 * initialize the per port data structure. Since initializing the port
817 * requires several commands to the board we will need to wait for any
818 * other open that is already initializing the port.
819 *
820 * Review - locking
821 */
822 tty_port_tty_set(port, tty);
823 tty->driver_data = portp;
824 port->count++;
825
826 wait_event_interruptible(portp->raw_wait,
827 !test_bit(ST_INITIALIZING, &portp->state));
828 if (signal_pending(current))
829 return -ERESTARTSYS;
830
831 if ((portp->port.flags & ASYNC_INITIALIZED) == 0) {
832 set_bit(ST_INITIALIZING, &portp->state);
833 if ((rc = stli_initopen(tty, brdp, portp)) >= 0) {
834 /* Locking */
835 port->flags |= ASYNC_INITIALIZED;
836 clear_bit(TTY_IO_ERROR, &tty->flags);
837 }
838 clear_bit(ST_INITIALIZING, &portp->state);
839 wake_up_interruptible(&portp->raw_wait);
840 if (rc < 0)
841 return rc;
842 }
843 return tty_port_block_til_ready(&portp->port, tty, filp);
844} 831}
845 832
833
846/*****************************************************************************/ 834/*****************************************************************************/
847 835
848static void stli_close(struct tty_struct *tty, struct file *filp) 836static void stli_shutdown(struct tty_port *port)
849{ 837{
850 struct stlibrd *brdp; 838 struct stlibrd *brdp;
851 struct stliport *portp; 839 unsigned long ftype;
852 struct tty_port *port;
853 unsigned long flags; 840 unsigned long flags;
841 struct stliport *portp = container_of(port, struct stliport, port);
854 842
855 portp = tty->driver_data; 843 if (portp->brdnr >= stli_nrbrds)
856 if (portp == NULL)
857 return; 844 return;
858 port = &portp->port; 845 brdp = stli_brds[portp->brdnr];
859 846 if (brdp == NULL)
860 if (tty_port_close_start(port, tty, filp) == 0)
861 return; 847 return;
862 848
863/* 849 /*
864 * May want to wait for data to drain before closing. The BUSY flag 850 * May want to wait for data to drain before closing. The BUSY
865 * keeps track of whether we are still transmitting or not. It is 851 * flag keeps track of whether we are still transmitting or not.
866 * updated by messages from the slave - indicating when all chars 852 * It is updated by messages from the slave - indicating when all
867 * really have drained. 853 * chars really have drained.
868 */ 854 */
869 spin_lock_irqsave(&stli_lock, flags);
870 if (tty == stli_txcooktty)
871 stli_flushchars(tty);
872 spin_unlock_irqrestore(&stli_lock, flags);
873
874 /* We end up doing this twice for the moment. This needs looking at
875 eventually. Note we still use portp->closing_wait as a result */
876 if (portp->closing_wait != ASYNC_CLOSING_WAIT_NONE)
877 tty_wait_until_sent(tty, portp->closing_wait);
878 855
879 /* FIXME: port locking here needs attending to */ 856 if (!test_bit(ST_CLOSING, &portp->state))
880 port->flags &= ~ASYNC_INITIALIZED; 857 stli_rawclose(brdp, portp, 0, 0);
881 858
882 brdp = stli_brds[portp->brdnr]; 859 spin_lock_irqsave(&stli_lock, flags);
883 stli_rawclose(brdp, portp, 0, 0);
884 if (tty->termios->c_cflag & HUPCL) {
885 stli_mkasysigs(&portp->asig, 0, 0);
886 if (test_bit(ST_CMDING, &portp->state))
887 set_bit(ST_DOSIGS, &portp->state);
888 else
889 stli_sendcmd(brdp, portp, A_SETSIGNALS, &portp->asig,
890 sizeof(asysigs_t), 0);
891 }
892 clear_bit(ST_TXBUSY, &portp->state); 860 clear_bit(ST_TXBUSY, &portp->state);
893 clear_bit(ST_RXSTOP, &portp->state); 861 clear_bit(ST_RXSTOP, &portp->state);
894 set_bit(TTY_IO_ERROR, &tty->flags); 862 spin_unlock_irqrestore(&stli_lock, flags);
895 tty_ldisc_flush(tty);
896 set_bit(ST_DOFLUSHRX, &portp->state);
897 stli_flushbuffer(tty);
898 863
899 tty_port_close_end(port, tty); 864 ftype = FLUSHTX | FLUSHRX;
900 tty_port_tty_set(port, NULL); 865 stli_cmdwait(brdp, portp, A_FLUSH, &ftype, sizeof(u32), 0);
866}
867
868static void stli_close(struct tty_struct *tty, struct file *filp)
869{
870 struct stliport *portp = tty->driver_data;
871 unsigned long flags;
872 if (portp == NULL)
873 return;
874 spin_lock_irqsave(&stli_lock, flags);
875 /* Flush any internal buffering out first */
876 if (tty == stli_txcooktty)
877 stli_flushchars(tty);
878 spin_unlock_irqrestore(&stli_lock, flags);
879 tty_port_close(&portp->port, tty, filp);
901} 880}
902 881
903/*****************************************************************************/ 882/*****************************************************************************/
@@ -1724,6 +1703,7 @@ static void stli_start(struct tty_struct *tty)
1724 1703
1725/*****************************************************************************/ 1704/*****************************************************************************/
1726 1705
1706
1727/* 1707/*
1728 * Hangup this port. This is pretty much like closing the port, only 1708 * Hangup this port. This is pretty much like closing the port, only
1729 * a little more brutal. No waiting for data to drain. Shutdown the 1709 * a little more brutal. No waiting for data to drain. Shutdown the
@@ -1733,47 +1713,8 @@ static void stli_start(struct tty_struct *tty)
1733 1713
1734static void stli_hangup(struct tty_struct *tty) 1714static void stli_hangup(struct tty_struct *tty)
1735{ 1715{
1736 struct stliport *portp; 1716 struct stliport *portp = tty->driver_data;
1737 struct stlibrd *brdp; 1717 tty_port_hangup(&portp->port);
1738 struct tty_port *port;
1739 unsigned long flags;
1740
1741 portp = tty->driver_data;
1742 if (portp == NULL)
1743 return;
1744 if (portp->brdnr >= stli_nrbrds)
1745 return;
1746 brdp = stli_brds[portp->brdnr];
1747 if (brdp == NULL)
1748 return;
1749 port = &portp->port;
1750
1751 spin_lock_irqsave(&port->lock, flags);
1752 port->flags &= ~ASYNC_INITIALIZED;
1753 spin_unlock_irqrestore(&port->lock, flags);
1754
1755 if (!test_bit(ST_CLOSING, &portp->state))
1756 stli_rawclose(brdp, portp, 0, 0);
1757
1758 spin_lock_irqsave(&stli_lock, flags);
1759 if (tty->termios->c_cflag & HUPCL) {
1760 stli_mkasysigs(&portp->asig, 0, 0);
1761 if (test_bit(ST_CMDING, &portp->state)) {
1762 set_bit(ST_DOSIGS, &portp->state);
1763 set_bit(ST_DOFLUSHTX, &portp->state);
1764 set_bit(ST_DOFLUSHRX, &portp->state);
1765 } else {
1766 stli_sendcmd(brdp, portp, A_SETSIGNALSF,
1767 &portp->asig, sizeof(asysigs_t), 0);
1768 }
1769 }
1770
1771 clear_bit(ST_TXBUSY, &portp->state);
1772 clear_bit(ST_RXSTOP, &portp->state);
1773 set_bit(TTY_IO_ERROR, &tty->flags);
1774 spin_unlock_irqrestore(&stli_lock, flags);
1775
1776 tty_port_hangup(port);
1777} 1718}
1778 1719
1779/*****************************************************************************/ 1720/*****************************************************************************/
@@ -4311,7 +4252,7 @@ static int stli_getbrdstruct(struct stlibrd __user *arg)
4311 * reset it, and start/stop it. 4252 * reset it, and start/stop it.
4312 */ 4253 */
4313 4254
4314static int stli_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg) 4255static long stli_memioctl(struct file *fp, unsigned int cmd, unsigned long arg)
4315{ 4256{
4316 struct stlibrd *brdp; 4257 struct stlibrd *brdp;
4317 int brdnr, rc, done; 4258 int brdnr, rc, done;
@@ -4356,7 +4297,7 @@ static int stli_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, un
4356 * Now handle the board specific ioctls. These all depend on the 4297 * Now handle the board specific ioctls. These all depend on the
4357 * minor number of the device they were called from. 4298 * minor number of the device they were called from.
4358 */ 4299 */
4359 brdnr = iminor(ip); 4300 brdnr = iminor(fp->f_dentry->d_inode);
4360 if (brdnr >= STL_MAXBRDS) 4301 if (brdnr >= STL_MAXBRDS)
4361 return -ENODEV; 4302 return -ENODEV;
4362 brdp = stli_brds[brdnr]; 4303 brdp = stli_brds[brdnr];
@@ -4420,6 +4361,8 @@ static const struct tty_operations stli_ops = {
4420static const struct tty_port_operations stli_port_ops = { 4361static const struct tty_port_operations stli_port_ops = {
4421 .carrier_raised = stli_carrier_raised, 4362 .carrier_raised = stli_carrier_raised,
4422 .dtr_rts = stli_dtr_rts, 4363 .dtr_rts = stli_dtr_rts,
4364 .activate = stli_activate,
4365 .shutdown = stli_shutdown,
4423}; 4366};
4424 4367
4425/*****************************************************************************/ 4368/*****************************************************************************/
diff --git a/drivers/char/moxa.c b/drivers/char/moxa.c
index dd0083bbb64a..63ee3bbc1ce4 100644
--- a/drivers/char/moxa.c
+++ b/drivers/char/moxa.c
@@ -34,7 +34,6 @@
34#include <linux/tty.h> 34#include <linux/tty.h>
35#include <linux/tty_flip.h> 35#include <linux/tty_flip.h>
36#include <linux/major.h> 36#include <linux/major.h>
37#include <linux/smp_lock.h>
38#include <linux/string.h> 37#include <linux/string.h>
39#include <linux/fcntl.h> 38#include <linux/fcntl.h>
40#include <linux/ptrace.h> 39#include <linux/ptrace.h>
@@ -139,7 +138,7 @@ struct moxa_port {
139 int cflag; 138 int cflag;
140 unsigned long statusflags; 139 unsigned long statusflags;
141 140
142 u8 DCDState; 141 u8 DCDState; /* Protected by the port lock */
143 u8 lineCtrl; 142 u8 lineCtrl;
144 u8 lowChkFlag; 143 u8 lowChkFlag;
145}; 144};
@@ -151,10 +150,9 @@ struct mon_str {
151}; 150};
152 151
153/* statusflags */ 152/* statusflags */
154#define TXSTOPPED 0x1 153#define TXSTOPPED 1
155#define LOWWAIT 0x2 154#define LOWWAIT 2
156#define EMPTYWAIT 0x4 155#define EMPTYWAIT 3
157#define THROTTLE 0x8
158 156
159#define SERIAL_DO_RESTART 157#define SERIAL_DO_RESTART
160 158
@@ -165,6 +163,7 @@ static struct mon_str moxaLog;
165static unsigned int moxaFuncTout = HZ / 2; 163static unsigned int moxaFuncTout = HZ / 2;
166static unsigned int moxaLowWaterChk; 164static unsigned int moxaLowWaterChk;
167static DEFINE_MUTEX(moxa_openlock); 165static DEFINE_MUTEX(moxa_openlock);
166static DEFINE_SPINLOCK(moxa_lock);
168/* Variables for insmod */ 167/* Variables for insmod */
169#ifdef MODULE 168#ifdef MODULE
170static unsigned long baseaddr[MAX_BOARDS]; 169static unsigned long baseaddr[MAX_BOARDS];
@@ -194,8 +193,6 @@ static int moxa_write(struct tty_struct *, const unsigned char *, int);
194static int moxa_write_room(struct tty_struct *); 193static int moxa_write_room(struct tty_struct *);
195static void moxa_flush_buffer(struct tty_struct *); 194static void moxa_flush_buffer(struct tty_struct *);
196static int moxa_chars_in_buffer(struct tty_struct *); 195static int moxa_chars_in_buffer(struct tty_struct *);
197static void moxa_throttle(struct tty_struct *);
198static void moxa_unthrottle(struct tty_struct *);
199static void moxa_set_termios(struct tty_struct *, struct ktermios *); 196static void moxa_set_termios(struct tty_struct *, struct ktermios *);
200static void moxa_stop(struct tty_struct *); 197static void moxa_stop(struct tty_struct *);
201static void moxa_start(struct tty_struct *); 198static void moxa_start(struct tty_struct *);
@@ -205,9 +202,9 @@ static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
205 unsigned int set, unsigned int clear); 202 unsigned int set, unsigned int clear);
206static void moxa_poll(unsigned long); 203static void moxa_poll(unsigned long);
207static void moxa_set_tty_param(struct tty_struct *, struct ktermios *); 204static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
208static void moxa_setup_empty_event(struct tty_struct *); 205static void moxa_shutdown(struct tty_port *);
209static void moxa_shut_down(struct tty_struct *);
210static int moxa_carrier_raised(struct tty_port *); 206static int moxa_carrier_raised(struct tty_port *);
207static void moxa_dtr_rts(struct tty_port *, int);
211/* 208/*
212 * moxa board interface functions: 209 * moxa board interface functions:
213 */ 210 */
@@ -234,6 +231,8 @@ static void MoxaSetFifo(struct moxa_port *port, int enable);
234 * I/O functions 231 * I/O functions
235 */ 232 */
236 233
234static DEFINE_SPINLOCK(moxafunc_lock);
235
237static void moxa_wait_finish(void __iomem *ofsAddr) 236static void moxa_wait_finish(void __iomem *ofsAddr)
238{ 237{
239 unsigned long end = jiffies + moxaFuncTout; 238 unsigned long end = jiffies + moxaFuncTout;
@@ -247,9 +246,25 @@ static void moxa_wait_finish(void __iomem *ofsAddr)
247 246
248static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg) 247static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
249{ 248{
249 unsigned long flags;
250 spin_lock_irqsave(&moxafunc_lock, flags);
250 writew(arg, ofsAddr + FuncArg); 251 writew(arg, ofsAddr + FuncArg);
251 writew(cmd, ofsAddr + FuncCode); 252 writew(cmd, ofsAddr + FuncCode);
252 moxa_wait_finish(ofsAddr); 253 moxa_wait_finish(ofsAddr);
254 spin_unlock_irqrestore(&moxafunc_lock, flags);
255}
256
257static int moxafuncret(void __iomem *ofsAddr, u16 cmd, u16 arg)
258{
259 unsigned long flags;
260 u16 ret;
261 spin_lock_irqsave(&moxafunc_lock, flags);
262 writew(arg, ofsAddr + FuncArg);
263 writew(cmd, ofsAddr + FuncCode);
264 moxa_wait_finish(ofsAddr);
265 ret = readw(ofsAddr + FuncArg);
266 spin_unlock_irqrestore(&moxafunc_lock, flags);
267 return ret;
253} 268}
254 269
255static void moxa_low_water_check(void __iomem *ofsAddr) 270static void moxa_low_water_check(void __iomem *ofsAddr)
@@ -299,22 +314,20 @@ static int moxa_ioctl(struct tty_struct *tty, struct file *file,
299 struct moxa_port *p; 314 struct moxa_port *p;
300 unsigned int i, j; 315 unsigned int i, j;
301 316
302 mutex_lock(&moxa_openlock);
303 for (i = 0; i < MAX_BOARDS; i++) { 317 for (i = 0; i < MAX_BOARDS; i++) {
304 p = moxa_boards[i].ports; 318 p = moxa_boards[i].ports;
305 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) { 319 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
306 memset(&tmp, 0, sizeof(tmp)); 320 memset(&tmp, 0, sizeof(tmp));
321 spin_lock_bh(&moxa_lock);
307 if (moxa_boards[i].ready) { 322 if (moxa_boards[i].ready) {
308 tmp.inq = MoxaPortRxQueue(p); 323 tmp.inq = MoxaPortRxQueue(p);
309 tmp.outq = MoxaPortTxQueue(p); 324 tmp.outq = MoxaPortTxQueue(p);
310 } 325 }
311 if (copy_to_user(argm, &tmp, sizeof(tmp))) { 326 spin_unlock_bh(&moxa_lock);
312 mutex_unlock(&moxa_openlock); 327 if (copy_to_user(argm, &tmp, sizeof(tmp)))
313 return -EFAULT; 328 return -EFAULT;
314 }
315 } 329 }
316 } 330 }
317 mutex_unlock(&moxa_openlock);
318 break; 331 break;
319 } case MOXA_GET_OQUEUE: 332 } case MOXA_GET_OQUEUE:
320 status = MoxaPortTxQueue(ch); 333 status = MoxaPortTxQueue(ch);
@@ -330,16 +343,20 @@ static int moxa_ioctl(struct tty_struct *tty, struct file *file,
330 struct moxa_port *p; 343 struct moxa_port *p;
331 unsigned int i, j; 344 unsigned int i, j;
332 345
333 mutex_lock(&moxa_openlock);
334 for (i = 0; i < MAX_BOARDS; i++) { 346 for (i = 0; i < MAX_BOARDS; i++) {
335 p = moxa_boards[i].ports; 347 p = moxa_boards[i].ports;
336 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) { 348 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
337 struct tty_struct *ttyp; 349 struct tty_struct *ttyp;
338 memset(&tmp, 0, sizeof(tmp)); 350 memset(&tmp, 0, sizeof(tmp));
339 if (!moxa_boards[i].ready) 351 spin_lock_bh(&moxa_lock);
352 if (!moxa_boards[i].ready) {
353 spin_unlock_bh(&moxa_lock);
340 goto copy; 354 goto copy;
355 }
341 356
342 status = MoxaPortLineStatus(p); 357 status = MoxaPortLineStatus(p);
358 spin_unlock_bh(&moxa_lock);
359
343 if (status & 1) 360 if (status & 1)
344 tmp.cts = 1; 361 tmp.cts = 1;
345 if (status & 2) 362 if (status & 2)
@@ -354,24 +371,21 @@ static int moxa_ioctl(struct tty_struct *tty, struct file *file,
354 tmp.cflag = ttyp->termios->c_cflag; 371 tmp.cflag = ttyp->termios->c_cflag;
355 tty_kref_put(tty); 372 tty_kref_put(tty);
356copy: 373copy:
357 if (copy_to_user(argm, &tmp, sizeof(tmp))) { 374 if (copy_to_user(argm, &tmp, sizeof(tmp)))
358 mutex_unlock(&moxa_openlock);
359 return -EFAULT; 375 return -EFAULT;
360 }
361 } 376 }
362 } 377 }
363 mutex_unlock(&moxa_openlock);
364 break; 378 break;
365 } 379 }
366 case TIOCGSERIAL: 380 case TIOCGSERIAL:
367 mutex_lock(&moxa_openlock); 381 mutex_lock(&ch->port.mutex);
368 ret = moxa_get_serial_info(ch, argp); 382 ret = moxa_get_serial_info(ch, argp);
369 mutex_unlock(&moxa_openlock); 383 mutex_unlock(&ch->port.mutex);
370 break; 384 break;
371 case TIOCSSERIAL: 385 case TIOCSSERIAL:
372 mutex_lock(&moxa_openlock); 386 mutex_lock(&ch->port.mutex);
373 ret = moxa_set_serial_info(ch, argp); 387 ret = moxa_set_serial_info(ch, argp);
374 mutex_unlock(&moxa_openlock); 388 mutex_unlock(&ch->port.mutex);
375 break; 389 break;
376 default: 390 default:
377 ret = -ENOIOCTLCMD; 391 ret = -ENOIOCTLCMD;
@@ -396,8 +410,6 @@ static const struct tty_operations moxa_ops = {
396 .flush_buffer = moxa_flush_buffer, 410 .flush_buffer = moxa_flush_buffer,
397 .chars_in_buffer = moxa_chars_in_buffer, 411 .chars_in_buffer = moxa_chars_in_buffer,
398 .ioctl = moxa_ioctl, 412 .ioctl = moxa_ioctl,
399 .throttle = moxa_throttle,
400 .unthrottle = moxa_unthrottle,
401 .set_termios = moxa_set_termios, 413 .set_termios = moxa_set_termios,
402 .stop = moxa_stop, 414 .stop = moxa_stop,
403 .start = moxa_start, 415 .start = moxa_start,
@@ -409,11 +421,12 @@ static const struct tty_operations moxa_ops = {
409 421
410static const struct tty_port_operations moxa_port_ops = { 422static const struct tty_port_operations moxa_port_ops = {
411 .carrier_raised = moxa_carrier_raised, 423 .carrier_raised = moxa_carrier_raised,
424 .dtr_rts = moxa_dtr_rts,
425 .shutdown = moxa_shutdown,
412}; 426};
413 427
414static struct tty_driver *moxaDriver; 428static struct tty_driver *moxaDriver;
415static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0); 429static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
416static DEFINE_SPINLOCK(moxa_lock);
417 430
418/* 431/*
419 * HW init 432 * HW init
@@ -1112,14 +1125,12 @@ static void __exit moxa_exit(void)
1112module_init(moxa_init); 1125module_init(moxa_init);
1113module_exit(moxa_exit); 1126module_exit(moxa_exit);
1114 1127
1115static void moxa_close_port(struct tty_struct *tty) 1128static void moxa_shutdown(struct tty_port *port)
1116{ 1129{
1117 struct moxa_port *ch = tty->driver_data; 1130 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1118 moxa_shut_down(tty); 1131 MoxaPortDisable(ch);
1119 MoxaPortFlushData(ch, 2); 1132 MoxaPortFlushData(ch, 2);
1120 ch->port.flags &= ~ASYNC_NORMAL_ACTIVE; 1133 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
1121 tty->driver_data = NULL;
1122 tty_port_tty_set(&ch->port, NULL);
1123} 1134}
1124 1135
1125static int moxa_carrier_raised(struct tty_port *port) 1136static int moxa_carrier_raised(struct tty_port *port)
@@ -1127,45 +1138,19 @@ static int moxa_carrier_raised(struct tty_port *port)
1127 struct moxa_port *ch = container_of(port, struct moxa_port, port); 1138 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1128 int dcd; 1139 int dcd;
1129 1140
1130 spin_lock_bh(&moxa_lock); 1141 spin_lock_irq(&port->lock);
1131 dcd = ch->DCDState; 1142 dcd = ch->DCDState;
1132 spin_unlock_bh(&moxa_lock); 1143 spin_unlock_irq(&port->lock);
1133 return dcd; 1144 return dcd;
1134} 1145}
1135 1146
1136static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp, 1147static void moxa_dtr_rts(struct tty_port *port, int onoff)
1137 struct moxa_port *ch)
1138{ 1148{
1139 struct tty_port *port = &ch->port; 1149 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1140 DEFINE_WAIT(wait); 1150 MoxaPortLineCtrl(ch, onoff, onoff);
1141 int retval = 0;
1142 u8 dcd;
1143
1144 while (1) {
1145 prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
1146 if (tty_hung_up_p(filp)) {
1147#ifdef SERIAL_DO_RESTART
1148 retval = -ERESTARTSYS;
1149#else
1150 retval = -EAGAIN;
1151#endif
1152 break;
1153 }
1154 dcd = tty_port_carrier_raised(port);
1155 if (dcd)
1156 break;
1157
1158 if (signal_pending(current)) {
1159 retval = -ERESTARTSYS;
1160 break;
1161 }
1162 schedule();
1163 }
1164 finish_wait(&port->open_wait, &wait);
1165
1166 return retval;
1167} 1151}
1168 1152
1153
1169static int moxa_open(struct tty_struct *tty, struct file *filp) 1154static int moxa_open(struct tty_struct *tty, struct file *filp)
1170{ 1155{
1171 struct moxa_board_conf *brd; 1156 struct moxa_board_conf *brd;
@@ -1194,6 +1179,7 @@ static int moxa_open(struct tty_struct *tty, struct file *filp)
1194 ch->port.count++; 1179 ch->port.count++;
1195 tty->driver_data = ch; 1180 tty->driver_data = ch;
1196 tty_port_tty_set(&ch->port, tty); 1181 tty_port_tty_set(&ch->port, tty);
1182 mutex_lock(&ch->port.mutex);
1197 if (!(ch->port.flags & ASYNC_INITIALIZED)) { 1183 if (!(ch->port.flags & ASYNC_INITIALIZED)) {
1198 ch->statusflags = 0; 1184 ch->statusflags = 0;
1199 moxa_set_tty_param(tty, tty->termios); 1185 moxa_set_tty_param(tty, tty->termios);
@@ -1202,58 +1188,20 @@ static int moxa_open(struct tty_struct *tty, struct file *filp)
1202 MoxaSetFifo(ch, ch->type == PORT_16550A); 1188 MoxaSetFifo(ch, ch->type == PORT_16550A);
1203 ch->port.flags |= ASYNC_INITIALIZED; 1189 ch->port.flags |= ASYNC_INITIALIZED;
1204 } 1190 }
1191 mutex_unlock(&ch->port.mutex);
1205 mutex_unlock(&moxa_openlock); 1192 mutex_unlock(&moxa_openlock);
1206 1193
1207 retval = 0; 1194 retval = tty_port_block_til_ready(&ch->port, tty, filp);
1208 if (!(filp->f_flags & O_NONBLOCK) && !C_CLOCAL(tty)) 1195 if (retval == 0)
1209 retval = moxa_block_till_ready(tty, filp, ch); 1196 set_bit(ASYNCB_NORMAL_ACTIVE, &ch->port.flags);
1210 mutex_lock(&moxa_openlock);
1211 if (retval) {
1212 if (ch->port.count) /* 0 means already hung up... */
1213 if (--ch->port.count == 0)
1214 moxa_close_port(tty);
1215 } else
1216 ch->port.flags |= ASYNC_NORMAL_ACTIVE;
1217 mutex_unlock(&moxa_openlock);
1218
1219 return retval; 1197 return retval;
1220} 1198}
1221 1199
1222static void moxa_close(struct tty_struct *tty, struct file *filp) 1200static void moxa_close(struct tty_struct *tty, struct file *filp)
1223{ 1201{
1224 struct moxa_port *ch; 1202 struct moxa_port *ch = tty->driver_data;
1225 int port;
1226
1227 port = tty->index;
1228 if (port == MAX_PORTS || tty_hung_up_p(filp))
1229 return;
1230
1231 mutex_lock(&moxa_openlock);
1232 ch = tty->driver_data;
1233 if (ch == NULL)
1234 goto unlock;
1235 if (tty->count == 1 && ch->port.count != 1) {
1236 printk(KERN_WARNING "moxa_close: bad serial port count; "
1237 "tty->count is 1, ch->port.count is %d\n", ch->port.count);
1238 ch->port.count = 1;
1239 }
1240 if (--ch->port.count < 0) {
1241 printk(KERN_WARNING "moxa_close: bad serial port count, "
1242 "device=%s\n", tty->name);
1243 ch->port.count = 0;
1244 }
1245 if (ch->port.count)
1246 goto unlock;
1247
1248 ch->cflag = tty->termios->c_cflag; 1203 ch->cflag = tty->termios->c_cflag;
1249 if (ch->port.flags & ASYNC_INITIALIZED) { 1204 tty_port_close(&ch->port, tty, filp);
1250 moxa_setup_empty_event(tty);
1251 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
1252 }
1253
1254 moxa_close_port(tty);
1255unlock:
1256 mutex_unlock(&moxa_openlock);
1257} 1205}
1258 1206
1259static int moxa_write(struct tty_struct *tty, 1207static int moxa_write(struct tty_struct *tty,
@@ -1269,7 +1217,7 @@ static int moxa_write(struct tty_struct *tty,
1269 len = MoxaPortWriteData(tty, buf, count); 1217 len = MoxaPortWriteData(tty, buf, count);
1270 spin_unlock_bh(&moxa_lock); 1218 spin_unlock_bh(&moxa_lock);
1271 1219
1272 ch->statusflags |= LOWWAIT; 1220 set_bit(LOWWAIT, &ch->statusflags);
1273 return len; 1221 return len;
1274} 1222}
1275 1223
@@ -1300,40 +1248,21 @@ static int moxa_chars_in_buffer(struct tty_struct *tty)
1300 struct moxa_port *ch = tty->driver_data; 1248 struct moxa_port *ch = tty->driver_data;
1301 int chars; 1249 int chars;
1302 1250
1303 /*
1304 * Sigh...I have to check if driver_data is NULL here, because
1305 * if an open() fails, the TTY subsystem eventually calls
1306 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1307 * routine. And since the open() failed, we return 0 here. TDJ
1308 */
1309 if (ch == NULL)
1310 return 0;
1311 lock_kernel();
1312 chars = MoxaPortTxQueue(ch); 1251 chars = MoxaPortTxQueue(ch);
1313 if (chars) { 1252 if (chars)
1314 /* 1253 /*
1315 * Make it possible to wakeup anything waiting for output 1254 * Make it possible to wakeup anything waiting for output
1316 * in tty_ioctl.c, etc. 1255 * in tty_ioctl.c, etc.
1317 */ 1256 */
1318 if (!(ch->statusflags & EMPTYWAIT)) 1257 set_bit(EMPTYWAIT, &ch->statusflags);
1319 moxa_setup_empty_event(tty);
1320 }
1321 unlock_kernel();
1322 return chars; 1258 return chars;
1323} 1259}
1324 1260
1325static int moxa_tiocmget(struct tty_struct *tty, struct file *file) 1261static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1326{ 1262{
1327 struct moxa_port *ch; 1263 struct moxa_port *ch = tty->driver_data;
1328 int flag = 0, dtr, rts; 1264 int flag = 0, dtr, rts;
1329 1265
1330 mutex_lock(&moxa_openlock);
1331 ch = tty->driver_data;
1332 if (!ch) {
1333 mutex_unlock(&moxa_openlock);
1334 return -EINVAL;
1335 }
1336
1337 MoxaPortGetLineOut(ch, &dtr, &rts); 1266 MoxaPortGetLineOut(ch, &dtr, &rts);
1338 if (dtr) 1267 if (dtr)
1339 flag |= TIOCM_DTR; 1268 flag |= TIOCM_DTR;
@@ -1346,7 +1275,6 @@ static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1346 flag |= TIOCM_DSR; 1275 flag |= TIOCM_DSR;
1347 if (dtr & 4) 1276 if (dtr & 4)
1348 flag |= TIOCM_CD; 1277 flag |= TIOCM_CD;
1349 mutex_unlock(&moxa_openlock);
1350 return flag; 1278 return flag;
1351} 1279}
1352 1280
@@ -1379,20 +1307,6 @@ static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1379 return 0; 1307 return 0;
1380} 1308}
1381 1309
1382static void moxa_throttle(struct tty_struct *tty)
1383{
1384 struct moxa_port *ch = tty->driver_data;
1385
1386 ch->statusflags |= THROTTLE;
1387}
1388
1389static void moxa_unthrottle(struct tty_struct *tty)
1390{
1391 struct moxa_port *ch = tty->driver_data;
1392
1393 ch->statusflags &= ~THROTTLE;
1394}
1395
1396static void moxa_set_termios(struct tty_struct *tty, 1310static void moxa_set_termios(struct tty_struct *tty,
1397 struct ktermios *old_termios) 1311 struct ktermios *old_termios)
1398{ 1312{
@@ -1412,7 +1326,7 @@ static void moxa_stop(struct tty_struct *tty)
1412 if (ch == NULL) 1326 if (ch == NULL)
1413 return; 1327 return;
1414 MoxaPortTxDisable(ch); 1328 MoxaPortTxDisable(ch);
1415 ch->statusflags |= TXSTOPPED; 1329 set_bit(TXSTOPPED, &ch->statusflags);
1416} 1330}
1417 1331
1418 1332
@@ -1427,38 +1341,32 @@ static void moxa_start(struct tty_struct *tty)
1427 return; 1341 return;
1428 1342
1429 MoxaPortTxEnable(ch); 1343 MoxaPortTxEnable(ch);
1430 ch->statusflags &= ~TXSTOPPED; 1344 clear_bit(TXSTOPPED, &ch->statusflags);
1431} 1345}
1432 1346
1433static void moxa_hangup(struct tty_struct *tty) 1347static void moxa_hangup(struct tty_struct *tty)
1434{ 1348{
1435 struct moxa_port *ch; 1349 struct moxa_port *ch = tty->driver_data;
1436 1350 tty_port_hangup(&ch->port);
1437 mutex_lock(&moxa_openlock);
1438 ch = tty->driver_data;
1439 if (ch == NULL) {
1440 mutex_unlock(&moxa_openlock);
1441 return;
1442 }
1443 ch->port.count = 0;
1444 moxa_close_port(tty);
1445 mutex_unlock(&moxa_openlock);
1446
1447 wake_up_interruptible(&ch->port.open_wait);
1448} 1351}
1449 1352
1450static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd) 1353static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1451{ 1354{
1452 struct tty_struct *tty; 1355 struct tty_struct *tty;
1356 unsigned long flags;
1453 dcd = !!dcd; 1357 dcd = !!dcd;
1454 1358
1359 spin_lock_irqsave(&p->port.lock, flags);
1455 if (dcd != p->DCDState) { 1360 if (dcd != p->DCDState) {
1361 p->DCDState = dcd;
1362 spin_unlock_irqrestore(&p->port.lock, flags);
1456 tty = tty_port_tty_get(&p->port); 1363 tty = tty_port_tty_get(&p->port);
1457 if (tty && C_CLOCAL(tty) && !dcd) 1364 if (tty && C_CLOCAL(tty) && !dcd)
1458 tty_hangup(tty); 1365 tty_hangup(tty);
1459 tty_kref_put(tty); 1366 tty_kref_put(tty);
1460 } 1367 }
1461 p->DCDState = dcd; 1368 else
1369 spin_unlock_irqrestore(&p->port.lock, flags);
1462} 1370}
1463 1371
1464static int moxa_poll_port(struct moxa_port *p, unsigned int handle, 1372static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
@@ -1470,24 +1378,24 @@ static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1470 u16 intr; 1378 u16 intr;
1471 1379
1472 if (tty) { 1380 if (tty) {
1473 if ((p->statusflags & EMPTYWAIT) && 1381 if (test_bit(EMPTYWAIT, &p->statusflags) &&
1474 MoxaPortTxQueue(p) == 0) { 1382 MoxaPortTxQueue(p) == 0) {
1475 p->statusflags &= ~EMPTYWAIT; 1383 clear_bit(EMPTYWAIT, &p->statusflags);
1476 tty_wakeup(tty); 1384 tty_wakeup(tty);
1477 } 1385 }
1478 if ((p->statusflags & LOWWAIT) && !tty->stopped && 1386 if (test_bit(LOWWAIT, &p->statusflags) && !tty->stopped &&
1479 MoxaPortTxQueue(p) <= WAKEUP_CHARS) { 1387 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1480 p->statusflags &= ~LOWWAIT; 1388 clear_bit(LOWWAIT, &p->statusflags);
1481 tty_wakeup(tty); 1389 tty_wakeup(tty);
1482 } 1390 }
1483 1391
1484 if (inited && !(p->statusflags & THROTTLE) && 1392 if (inited && !test_bit(TTY_THROTTLED, &tty->flags) &&
1485 MoxaPortRxQueue(p) > 0) { /* RX */ 1393 MoxaPortRxQueue(p) > 0) { /* RX */
1486 MoxaPortReadData(p); 1394 MoxaPortReadData(p);
1487 tty_schedule_flip(tty); 1395 tty_schedule_flip(tty);
1488 } 1396 }
1489 } else { 1397 } else {
1490 p->statusflags &= ~EMPTYWAIT; 1398 clear_bit(EMPTYWAIT, &p->statusflags);
1491 MoxaPortFlushData(p, 0); /* flush RX */ 1399 MoxaPortFlushData(p, 0); /* flush RX */
1492 } 1400 }
1493 1401
@@ -1588,35 +1496,6 @@ static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_term
1588 tty_encode_baud_rate(tty, baud, baud); 1496 tty_encode_baud_rate(tty, baud, baud);
1589} 1497}
1590 1498
1591static void moxa_setup_empty_event(struct tty_struct *tty)
1592{
1593 struct moxa_port *ch = tty->driver_data;
1594
1595 spin_lock_bh(&moxa_lock);
1596 ch->statusflags |= EMPTYWAIT;
1597 spin_unlock_bh(&moxa_lock);
1598}
1599
1600static void moxa_shut_down(struct tty_struct *tty)
1601{
1602 struct moxa_port *ch = tty->driver_data;
1603
1604 if (!(ch->port.flags & ASYNC_INITIALIZED))
1605 return;
1606
1607 MoxaPortDisable(ch);
1608
1609 /*
1610 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1611 */
1612 if (C_HUPCL(tty))
1613 MoxaPortLineCtrl(ch, 0, 0);
1614
1615 spin_lock_bh(&moxa_lock);
1616 ch->port.flags &= ~ASYNC_INITIALIZED;
1617 spin_unlock_bh(&moxa_lock);
1618}
1619
1620/***************************************************************************** 1499/*****************************************************************************
1621 * Driver level functions: * 1500 * Driver level functions: *
1622 *****************************************************************************/ 1501 *****************************************************************************/
@@ -1918,10 +1797,12 @@ static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1918 baud = MoxaPortSetBaud(port, baud); 1797 baud = MoxaPortSetBaud(port, baud);
1919 1798
1920 if (termio->c_iflag & (IXON | IXOFF | IXANY)) { 1799 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1800 spin_lock_irq(&moxafunc_lock);
1921 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg); 1801 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1922 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1); 1802 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1923 writeb(FC_SetXonXoff, ofsAddr + FuncCode); 1803 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
1924 moxa_wait_finish(ofsAddr); 1804 moxa_wait_finish(ofsAddr);
1805 spin_unlock_irq(&moxafunc_lock);
1925 1806
1926 } 1807 }
1927 return baud; 1808 return baud;
@@ -1974,18 +1855,14 @@ static int MoxaPortLineStatus(struct moxa_port *port)
1974 int val; 1855 int val;
1975 1856
1976 ofsAddr = port->tableAddr; 1857 ofsAddr = port->tableAddr;
1977 if (MOXA_IS_320(port->board)) { 1858 if (MOXA_IS_320(port->board))
1978 moxafunc(ofsAddr, FC_LineStatus, 0); 1859 val = moxafuncret(ofsAddr, FC_LineStatus, 0);
1979 val = readw(ofsAddr + FuncArg); 1860 else
1980 } else {
1981 val = readw(ofsAddr + FlagStat) >> 4; 1861 val = readw(ofsAddr + FlagStat) >> 4;
1982 }
1983 val &= 0x0B; 1862 val &= 0x0B;
1984 if (val & 8) 1863 if (val & 8)
1985 val |= 4; 1864 val |= 4;
1986 spin_lock_bh(&moxa_lock);
1987 moxa_new_dcdstate(port, val & 8); 1865 moxa_new_dcdstate(port, val & 8);
1988 spin_unlock_bh(&moxa_lock);
1989 val &= 7; 1866 val &= 7;
1990 return val; 1867 return val;
1991} 1868}
diff --git a/drivers/char/mxser.c b/drivers/char/mxser.c
index 5e28d39b9e81..3d923065d9a2 100644
--- a/drivers/char/mxser.c
+++ b/drivers/char/mxser.c
@@ -23,7 +23,6 @@
23#include <linux/errno.h> 23#include <linux/errno.h>
24#include <linux/signal.h> 24#include <linux/signal.h>
25#include <linux/sched.h> 25#include <linux/sched.h>
26#include <linux/smp_lock.h>
27#include <linux/timer.h> 26#include <linux/timer.h>
28#include <linux/interrupt.h> 27#include <linux/interrupt.h>
29#include <linux/tty.h> 28#include <linux/tty.h>
@@ -856,9 +855,9 @@ static void mxser_check_modem_status(struct tty_struct *tty,
856 } 855 }
857} 856}
858 857
859static int mxser_startup(struct tty_struct *tty) 858static int mxser_activate(struct tty_port *port, struct tty_struct *tty)
860{ 859{
861 struct mxser_port *info = tty->driver_data; 860 struct mxser_port *info = container_of(port, struct mxser_port, port);
862 unsigned long page; 861 unsigned long page;
863 unsigned long flags; 862 unsigned long flags;
864 863
@@ -868,22 +867,13 @@ static int mxser_startup(struct tty_struct *tty)
868 867
869 spin_lock_irqsave(&info->slock, flags); 868 spin_lock_irqsave(&info->slock, flags);
870 869
871 if (info->port.flags & ASYNC_INITIALIZED) {
872 free_page(page);
873 spin_unlock_irqrestore(&info->slock, flags);
874 return 0;
875 }
876
877 if (!info->ioaddr || !info->type) { 870 if (!info->ioaddr || !info->type) {
878 set_bit(TTY_IO_ERROR, &tty->flags); 871 set_bit(TTY_IO_ERROR, &tty->flags);
879 free_page(page); 872 free_page(page);
880 spin_unlock_irqrestore(&info->slock, flags); 873 spin_unlock_irqrestore(&info->slock, flags);
881 return 0; 874 return 0;
882 } 875 }
883 if (info->port.xmit_buf) 876 info->port.xmit_buf = (unsigned char *) page;
884 free_page(page);
885 else
886 info->port.xmit_buf = (unsigned char *) page;
887 877
888 /* 878 /*
889 * Clear the FIFO buffers and disable them 879 * Clear the FIFO buffers and disable them
@@ -951,24 +941,19 @@ static int mxser_startup(struct tty_struct *tty)
951 * and set the speed of the serial port 941 * and set the speed of the serial port
952 */ 942 */
953 mxser_change_speed(tty, NULL); 943 mxser_change_speed(tty, NULL);
954 info->port.flags |= ASYNC_INITIALIZED;
955 spin_unlock_irqrestore(&info->slock, flags); 944 spin_unlock_irqrestore(&info->slock, flags);
956 945
957 return 0; 946 return 0;
958} 947}
959 948
960/* 949/*
961 * This routine will shutdown a serial port; interrupts maybe disabled, and 950 * This routine will shutdown a serial port
962 * DTR is dropped if the hangup on close termio flag is on.
963 */ 951 */
964static void mxser_shutdown(struct tty_struct *tty) 952static void mxser_shutdown_port(struct tty_port *port)
965{ 953{
966 struct mxser_port *info = tty->driver_data; 954 struct mxser_port *info = container_of(port, struct mxser_port, port);
967 unsigned long flags; 955 unsigned long flags;
968 956
969 if (!(info->port.flags & ASYNC_INITIALIZED))
970 return;
971
972 spin_lock_irqsave(&info->slock, flags); 957 spin_lock_irqsave(&info->slock, flags);
973 958
974 /* 959 /*
@@ -978,7 +963,7 @@ static void mxser_shutdown(struct tty_struct *tty)
978 wake_up_interruptible(&info->port.delta_msr_wait); 963 wake_up_interruptible(&info->port.delta_msr_wait);
979 964
980 /* 965 /*
981 * Free the IRQ, if necessary 966 * Free the xmit buffer, if necessary
982 */ 967 */
983 if (info->port.xmit_buf) { 968 if (info->port.xmit_buf) {
984 free_page((unsigned long) info->port.xmit_buf); 969 free_page((unsigned long) info->port.xmit_buf);
@@ -988,10 +973,6 @@ static void mxser_shutdown(struct tty_struct *tty)
988 info->IER = 0; 973 info->IER = 0;
989 outb(0x00, info->ioaddr + UART_IER); 974 outb(0x00, info->ioaddr + UART_IER);
990 975
991 if (tty->termios->c_cflag & HUPCL)
992 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
993 outb(info->MCR, info->ioaddr + UART_MCR);
994
995 /* clear Rx/Tx FIFO's */ 976 /* clear Rx/Tx FIFO's */
996 if (info->board->chip_flag) 977 if (info->board->chip_flag)
997 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT | 978 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
@@ -1004,9 +985,6 @@ static void mxser_shutdown(struct tty_struct *tty)
1004 /* read data port to reset things */ 985 /* read data port to reset things */
1005 (void) inb(info->ioaddr + UART_RX); 986 (void) inb(info->ioaddr + UART_RX);
1006 987
1007 set_bit(TTY_IO_ERROR, &tty->flags);
1008
1009 info->port.flags &= ~ASYNC_INITIALIZED;
1010 988
1011 if (info->board->chip_flag) 989 if (info->board->chip_flag)
1012 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr); 990 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
@@ -1023,8 +1001,7 @@ static void mxser_shutdown(struct tty_struct *tty)
1023static int mxser_open(struct tty_struct *tty, struct file *filp) 1001static int mxser_open(struct tty_struct *tty, struct file *filp)
1024{ 1002{
1025 struct mxser_port *info; 1003 struct mxser_port *info;
1026 unsigned long flags; 1004 int line;
1027 int retval, line;
1028 1005
1029 line = tty->index; 1006 line = tty->index;
1030 if (line == MXSER_PORTS) 1007 if (line == MXSER_PORTS)
@@ -1035,23 +1012,7 @@ static int mxser_open(struct tty_struct *tty, struct file *filp)
1035 if (!info->ioaddr) 1012 if (!info->ioaddr)
1036 return -ENODEV; 1013 return -ENODEV;
1037 1014
1038 tty->driver_data = info; 1015 return tty_port_open(&info->port, tty, filp);
1039 tty_port_tty_set(&info->port, tty);
1040 /*
1041 * Start up serial port
1042 */
1043 spin_lock_irqsave(&info->port.lock, flags);
1044 info->port.count++;
1045 spin_unlock_irqrestore(&info->port.lock, flags);
1046 retval = mxser_startup(tty);
1047 if (retval)
1048 return retval;
1049
1050 retval = tty_port_block_til_ready(&info->port, tty, filp);
1051 if (retval)
1052 return retval;
1053
1054 return 0;
1055} 1016}
1056 1017
1057static void mxser_flush_buffer(struct tty_struct *tty) 1018static void mxser_flush_buffer(struct tty_struct *tty)
@@ -1075,19 +1036,11 @@ static void mxser_flush_buffer(struct tty_struct *tty)
1075} 1036}
1076 1037
1077 1038
1078static void mxser_close_port(struct tty_struct *tty, struct tty_port *port) 1039static void mxser_close_port(struct tty_port *port)
1079{ 1040{
1080 struct mxser_port *info = container_of(port, struct mxser_port, port); 1041 struct mxser_port *info = container_of(port, struct mxser_port, port);
1081 unsigned long timeout; 1042 unsigned long timeout;
1082 /* 1043 /*
1083 * Save the termios structure, since this port may have
1084 * separate termios for callout and dialin.
1085 *
1086 * FIXME: Can this go ?
1087 */
1088 if (port->flags & ASYNC_NORMAL_ACTIVE)
1089 info->normal_termios = *tty->termios;
1090 /*
1091 * At this point we stop accepting input. To do this, we 1044 * At this point we stop accepting input. To do this, we
1092 * disable the receive line status interrupts, and tell the 1045 * disable the receive line status interrupts, and tell the
1093 * interrupt driver to stop checking the data ready bit in the 1046 * interrupt driver to stop checking the data ready bit in the
@@ -1097,22 +1050,18 @@ static void mxser_close_port(struct tty_struct *tty, struct tty_port *port)
1097 if (info->board->chip_flag) 1050 if (info->board->chip_flag)
1098 info->IER &= ~MOXA_MUST_RECV_ISR; 1051 info->IER &= ~MOXA_MUST_RECV_ISR;
1099 1052
1100 if (port->flags & ASYNC_INITIALIZED) { 1053 outb(info->IER, info->ioaddr + UART_IER);
1101 outb(info->IER, info->ioaddr + UART_IER); 1054 /*
1102 /* 1055 * Before we drop DTR, make sure the UART transmitter
1103 * Before we drop DTR, make sure the UART transmitter 1056 * has completely drained; this is especially
1104 * has completely drained; this is especially 1057 * important if there is a transmit FIFO!
1105 * important if there is a transmit FIFO! 1058 */
1106 */ 1059 timeout = jiffies + HZ;
1107 timeout = jiffies + HZ; 1060 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
1108 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) { 1061 schedule_timeout_interruptible(5);
1109 schedule_timeout_interruptible(5); 1062 if (time_after(jiffies, timeout))
1110 if (time_after(jiffies, timeout)) 1063 break;
1111 break;
1112 }
1113 } 1064 }
1114 mxser_shutdown(tty);
1115
1116} 1065}
1117 1066
1118/* 1067/*
@@ -1130,8 +1079,12 @@ static void mxser_close(struct tty_struct *tty, struct file *filp)
1130 return; 1079 return;
1131 if (tty_port_close_start(port, tty, filp) == 0) 1080 if (tty_port_close_start(port, tty, filp) == 0)
1132 return; 1081 return;
1133 mxser_close_port(tty, port); 1082 mutex_lock(&port->mutex);
1083 mxser_close_port(port);
1134 mxser_flush_buffer(tty); 1084 mxser_flush_buffer(tty);
1085 mxser_shutdown_port(port);
1086 clear_bit(ASYNCB_INITIALIZED, &port->flags);
1087 mutex_unlock(&port->mutex);
1135 /* Right now the tty_port set is done outside of the close_end helper 1088 /* Right now the tty_port set is done outside of the close_end helper
1136 as we don't yet have everyone using refcounts */ 1089 as we don't yet have everyone using refcounts */
1137 tty_port_close_end(port, tty); 1090 tty_port_close_end(port, tty);
@@ -1275,6 +1228,7 @@ static int mxser_set_serial_info(struct tty_struct *tty,
1275 struct serial_struct __user *new_info) 1228 struct serial_struct __user *new_info)
1276{ 1229{
1277 struct mxser_port *info = tty->driver_data; 1230 struct mxser_port *info = tty->driver_data;
1231 struct tty_port *port = &info->port;
1278 struct serial_struct new_serial; 1232 struct serial_struct new_serial;
1279 speed_t baud; 1233 speed_t baud;
1280 unsigned long sl_flags; 1234 unsigned long sl_flags;
@@ -1290,7 +1244,7 @@ static int mxser_set_serial_info(struct tty_struct *tty,
1290 new_serial.port != info->ioaddr) 1244 new_serial.port != info->ioaddr)
1291 return -EINVAL; 1245 return -EINVAL;
1292 1246
1293 flags = info->port.flags & ASYNC_SPD_MASK; 1247 flags = port->flags & ASYNC_SPD_MASK;
1294 1248
1295 if (!capable(CAP_SYS_ADMIN)) { 1249 if (!capable(CAP_SYS_ADMIN)) {
1296 if ((new_serial.baud_base != info->baud_base) || 1250 if ((new_serial.baud_base != info->baud_base) ||
@@ -1304,16 +1258,17 @@ static int mxser_set_serial_info(struct tty_struct *tty,
1304 * OK, past this point, all the error checking has been done. 1258 * OK, past this point, all the error checking has been done.
1305 * At this point, we start making changes..... 1259 * At this point, we start making changes.....
1306 */ 1260 */
1307 info->port.flags = ((info->port.flags & ~ASYNC_FLAGS) | 1261 port->flags = ((port->flags & ~ASYNC_FLAGS) |
1308 (new_serial.flags & ASYNC_FLAGS)); 1262 (new_serial.flags & ASYNC_FLAGS));
1309 info->port.close_delay = new_serial.close_delay * HZ / 100; 1263 port->close_delay = new_serial.close_delay * HZ / 100;
1310 info->port.closing_wait = new_serial.closing_wait * HZ / 100; 1264 port->closing_wait = new_serial.closing_wait * HZ / 100;
1311 tty->low_latency = (info->port.flags & ASYNC_LOW_LATENCY) 1265 tty->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1312 ? 1 : 0; 1266 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
1313 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
1314 (new_serial.baud_base != info->baud_base || 1267 (new_serial.baud_base != info->baud_base ||
1315 new_serial.custom_divisor != 1268 new_serial.custom_divisor !=
1316 info->custom_divisor)) { 1269 info->custom_divisor)) {
1270 if (new_serial.custom_divisor == 0)
1271 return -EINVAL;
1317 baud = new_serial.baud_base / new_serial.custom_divisor; 1272 baud = new_serial.baud_base / new_serial.custom_divisor;
1318 tty_encode_baud_rate(tty, baud, baud); 1273 tty_encode_baud_rate(tty, baud, baud);
1319 } 1274 }
@@ -1323,15 +1278,17 @@ static int mxser_set_serial_info(struct tty_struct *tty,
1323 1278
1324 process_txrx_fifo(info); 1279 process_txrx_fifo(info);
1325 1280
1326 if (info->port.flags & ASYNC_INITIALIZED) { 1281 if (test_bit(ASYNCB_INITIALIZED, &port->flags)) {
1327 if (flags != (info->port.flags & ASYNC_SPD_MASK)) { 1282 if (flags != (port->flags & ASYNC_SPD_MASK)) {
1328 spin_lock_irqsave(&info->slock, sl_flags); 1283 spin_lock_irqsave(&info->slock, sl_flags);
1329 mxser_change_speed(tty, NULL); 1284 mxser_change_speed(tty, NULL);
1330 spin_unlock_irqrestore(&info->slock, sl_flags); 1285 spin_unlock_irqrestore(&info->slock, sl_flags);
1331 } 1286 }
1332 } else 1287 } else {
1333 retval = mxser_startup(tty); 1288 retval = mxser_activate(port, tty);
1334 1289 if (retval == 0)
1290 set_bit(ASYNCB_INITIALIZED, &port->flags);
1291 }
1335 return retval; 1292 return retval;
1336} 1293}
1337 1294
@@ -1520,7 +1477,8 @@ static int __init mxser_read_register(int port, unsigned short *regs)
1520 1477
1521static int mxser_ioctl_special(unsigned int cmd, void __user *argp) 1478static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1522{ 1479{
1523 struct mxser_port *port; 1480 struct mxser_port *ip;
1481 struct tty_port *port;
1524 struct tty_struct *tty; 1482 struct tty_struct *tty;
1525 int result, status; 1483 int result, status;
1526 unsigned int i, j; 1484 unsigned int i, j;
@@ -1536,38 +1494,39 @@ static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1536 1494
1537 case MOXA_CHKPORTENABLE: 1495 case MOXA_CHKPORTENABLE:
1538 result = 0; 1496 result = 0;
1539 lock_kernel();
1540 for (i = 0; i < MXSER_BOARDS; i++) 1497 for (i = 0; i < MXSER_BOARDS; i++)
1541 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) 1498 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1542 if (mxser_boards[i].ports[j].ioaddr) 1499 if (mxser_boards[i].ports[j].ioaddr)
1543 result |= (1 << i); 1500 result |= (1 << i);
1544 unlock_kernel();
1545 return put_user(result, (unsigned long __user *)argp); 1501 return put_user(result, (unsigned long __user *)argp);
1546 case MOXA_GETDATACOUNT: 1502 case MOXA_GETDATACOUNT:
1547 lock_kernel(); 1503 /* The receive side is locked by port->slock but it isn't
1504 clear that an exact snapshot is worth copying here */
1548 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log))) 1505 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1549 ret = -EFAULT; 1506 ret = -EFAULT;
1550 unlock_kernel();
1551 return ret; 1507 return ret;
1552 case MOXA_GETMSTATUS: { 1508 case MOXA_GETMSTATUS: {
1553 struct mxser_mstatus ms, __user *msu = argp; 1509 struct mxser_mstatus ms, __user *msu = argp;
1554 lock_kernel();
1555 for (i = 0; i < MXSER_BOARDS; i++) 1510 for (i = 0; i < MXSER_BOARDS; i++)
1556 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) { 1511 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1557 port = &mxser_boards[i].ports[j]; 1512 ip = &mxser_boards[i].ports[j];
1513 port = &ip->port;
1558 memset(&ms, 0, sizeof(ms)); 1514 memset(&ms, 0, sizeof(ms));
1559 1515
1560 if (!port->ioaddr) 1516 mutex_lock(&port->mutex);
1517 if (!ip->ioaddr)
1561 goto copy; 1518 goto copy;
1562 1519
1563 tty = tty_port_tty_get(&port->port); 1520 tty = tty_port_tty_get(port);
1564 1521
1565 if (!tty || !tty->termios) 1522 if (!tty || !tty->termios)
1566 ms.cflag = port->normal_termios.c_cflag; 1523 ms.cflag = ip->normal_termios.c_cflag;
1567 else 1524 else
1568 ms.cflag = tty->termios->c_cflag; 1525 ms.cflag = tty->termios->c_cflag;
1569 tty_kref_put(tty); 1526 tty_kref_put(tty);
1570 status = inb(port->ioaddr + UART_MSR); 1527 spin_lock_irq(&ip->slock);
1528 status = inb(ip->ioaddr + UART_MSR);
1529 spin_unlock_irq(&ip->slock);
1571 if (status & UART_MSR_DCD) 1530 if (status & UART_MSR_DCD)
1572 ms.dcd = 1; 1531 ms.dcd = 1;
1573 if (status & UART_MSR_DSR) 1532 if (status & UART_MSR_DSR)
@@ -1575,13 +1534,11 @@ static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1575 if (status & UART_MSR_CTS) 1534 if (status & UART_MSR_CTS)
1576 ms.cts = 1; 1535 ms.cts = 1;
1577 copy: 1536 copy:
1578 if (copy_to_user(msu, &ms, sizeof(ms))) { 1537 mutex_unlock(&port->mutex);
1579 unlock_kernel(); 1538 if (copy_to_user(msu, &ms, sizeof(ms)))
1580 return -EFAULT; 1539 return -EFAULT;
1581 }
1582 msu++; 1540 msu++;
1583 } 1541 }
1584 unlock_kernel();
1585 return 0; 1542 return 0;
1586 } 1543 }
1587 case MOXA_ASPP_MON_EXT: { 1544 case MOXA_ASPP_MON_EXT: {
@@ -1593,41 +1550,48 @@ static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1593 if (!me) 1550 if (!me)
1594 return -ENOMEM; 1551 return -ENOMEM;
1595 1552
1596 lock_kernel();
1597 for (i = 0, p = 0; i < MXSER_BOARDS; i++) { 1553 for (i = 0, p = 0; i < MXSER_BOARDS; i++) {
1598 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++, p++) { 1554 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++, p++) {
1599 if (p >= ARRAY_SIZE(me->rx_cnt)) { 1555 if (p >= ARRAY_SIZE(me->rx_cnt)) {
1600 i = MXSER_BOARDS; 1556 i = MXSER_BOARDS;
1601 break; 1557 break;
1602 } 1558 }
1603 port = &mxser_boards[i].ports[j]; 1559 ip = &mxser_boards[i].ports[j];
1604 if (!port->ioaddr) 1560 port = &ip->port;
1561
1562 mutex_lock(&port->mutex);
1563 if (!ip->ioaddr) {
1564 mutex_unlock(&port->mutex);
1605 continue; 1565 continue;
1566 }
1606 1567
1607 status = mxser_get_msr(port->ioaddr, 0, p); 1568 spin_lock_irq(&ip->slock);
1569 status = mxser_get_msr(ip->ioaddr, 0, p);
1608 1570
1609 if (status & UART_MSR_TERI) 1571 if (status & UART_MSR_TERI)
1610 port->icount.rng++; 1572 ip->icount.rng++;
1611 if (status & UART_MSR_DDSR) 1573 if (status & UART_MSR_DDSR)
1612 port->icount.dsr++; 1574 ip->icount.dsr++;
1613 if (status & UART_MSR_DDCD) 1575 if (status & UART_MSR_DDCD)
1614 port->icount.dcd++; 1576 ip->icount.dcd++;
1615 if (status & UART_MSR_DCTS) 1577 if (status & UART_MSR_DCTS)
1616 port->icount.cts++; 1578 ip->icount.cts++;
1617 1579
1618 port->mon_data.modem_status = status; 1580 ip->mon_data.modem_status = status;
1619 me->rx_cnt[p] = port->mon_data.rxcnt; 1581 me->rx_cnt[p] = ip->mon_data.rxcnt;
1620 me->tx_cnt[p] = port->mon_data.txcnt; 1582 me->tx_cnt[p] = ip->mon_data.txcnt;
1621 me->up_rxcnt[p] = port->mon_data.up_rxcnt; 1583 me->up_rxcnt[p] = ip->mon_data.up_rxcnt;
1622 me->up_txcnt[p] = port->mon_data.up_txcnt; 1584 me->up_txcnt[p] = ip->mon_data.up_txcnt;
1623 me->modem_status[p] = 1585 me->modem_status[p] =
1624 port->mon_data.modem_status; 1586 ip->mon_data.modem_status;
1625 tty = tty_port_tty_get(&port->port); 1587 spin_unlock_irq(&ip->slock);
1588
1589 tty = tty_port_tty_get(&ip->port);
1626 1590
1627 if (!tty || !tty->termios) { 1591 if (!tty || !tty->termios) {
1628 cflag = port->normal_termios.c_cflag; 1592 cflag = ip->normal_termios.c_cflag;
1629 iflag = port->normal_termios.c_iflag; 1593 iflag = ip->normal_termios.c_iflag;
1630 me->baudrate[p] = tty_termios_baud_rate(&port->normal_termios); 1594 me->baudrate[p] = tty_termios_baud_rate(&ip->normal_termios);
1631 } else { 1595 } else {
1632 cflag = tty->termios->c_cflag; 1596 cflag = tty->termios->c_cflag;
1633 iflag = tty->termios->c_iflag; 1597 iflag = tty->termios->c_iflag;
@@ -1646,16 +1610,15 @@ static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1646 if (iflag & (IXON | IXOFF)) 1610 if (iflag & (IXON | IXOFF))
1647 me->flowctrl[p] |= 0x0C; 1611 me->flowctrl[p] |= 0x0C;
1648 1612
1649 if (port->type == PORT_16550A) 1613 if (ip->type == PORT_16550A)
1650 me->fifo[p] = 1; 1614 me->fifo[p] = 1;
1651 1615
1652 opmode = inb(port->opmode_ioaddr) >> 1616 opmode = inb(ip->opmode_ioaddr)>>((p % 4) * 2);
1653 ((p % 4) * 2);
1654 opmode &= OP_MODE_MASK; 1617 opmode &= OP_MODE_MASK;
1655 me->iftype[p] = opmode; 1618 me->iftype[p] = opmode;
1619 mutex_unlock(&port->mutex);
1656 } 1620 }
1657 } 1621 }
1658 unlock_kernel();
1659 if (copy_to_user(argp, me, sizeof(*me))) 1622 if (copy_to_user(argp, me, sizeof(*me)))
1660 ret = -EFAULT; 1623 ret = -EFAULT;
1661 kfree(me); 1624 kfree(me);
@@ -1692,6 +1655,7 @@ static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1692 unsigned int cmd, unsigned long arg) 1655 unsigned int cmd, unsigned long arg)
1693{ 1656{
1694 struct mxser_port *info = tty->driver_data; 1657 struct mxser_port *info = tty->driver_data;
1658 struct tty_port *port = &info->port;
1695 struct async_icount cnow; 1659 struct async_icount cnow;
1696 unsigned long flags; 1660 unsigned long flags;
1697 void __user *argp = (void __user *)arg; 1661 void __user *argp = (void __user *)arg;
@@ -1716,20 +1680,20 @@ static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1716 opmode != RS422_MODE && 1680 opmode != RS422_MODE &&
1717 opmode != RS485_4WIRE_MODE) 1681 opmode != RS485_4WIRE_MODE)
1718 return -EFAULT; 1682 return -EFAULT;
1719 lock_kernel();
1720 mask = ModeMask[p]; 1683 mask = ModeMask[p];
1721 shiftbit = p * 2; 1684 shiftbit = p * 2;
1685 spin_lock_irq(&info->slock);
1722 val = inb(info->opmode_ioaddr); 1686 val = inb(info->opmode_ioaddr);
1723 val &= mask; 1687 val &= mask;
1724 val |= (opmode << shiftbit); 1688 val |= (opmode << shiftbit);
1725 outb(val, info->opmode_ioaddr); 1689 outb(val, info->opmode_ioaddr);
1726 unlock_kernel(); 1690 spin_unlock_irq(&info->slock);
1727 } else { 1691 } else {
1728 lock_kernel();
1729 shiftbit = p * 2; 1692 shiftbit = p * 2;
1693 spin_lock_irq(&info->slock);
1730 opmode = inb(info->opmode_ioaddr) >> shiftbit; 1694 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1695 spin_unlock_irq(&info->slock);
1731 opmode &= OP_MODE_MASK; 1696 opmode &= OP_MODE_MASK;
1732 unlock_kernel();
1733 if (put_user(opmode, (int __user *)argp)) 1697 if (put_user(opmode, (int __user *)argp))
1734 return -EFAULT; 1698 return -EFAULT;
1735 } 1699 }
@@ -1742,14 +1706,14 @@ static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1742 1706
1743 switch (cmd) { 1707 switch (cmd) {
1744 case TIOCGSERIAL: 1708 case TIOCGSERIAL:
1745 lock_kernel(); 1709 mutex_lock(&port->mutex);
1746 retval = mxser_get_serial_info(tty, argp); 1710 retval = mxser_get_serial_info(tty, argp);
1747 unlock_kernel(); 1711 mutex_unlock(&port->mutex);
1748 return retval; 1712 return retval;
1749 case TIOCSSERIAL: 1713 case TIOCSSERIAL:
1750 lock_kernel(); 1714 mutex_lock(&port->mutex);
1751 retval = mxser_set_serial_info(tty, argp); 1715 retval = mxser_set_serial_info(tty, argp);
1752 unlock_kernel(); 1716 mutex_unlock(&port->mutex);
1753 return retval; 1717 return retval;
1754 case TIOCSERGETLSR: /* Get line status register */ 1718 case TIOCSERGETLSR: /* Get line status register */
1755 return mxser_get_lsr_info(info, argp); 1719 return mxser_get_lsr_info(info, argp);
@@ -1795,31 +1759,33 @@ static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1795 case MOXA_HighSpeedOn: 1759 case MOXA_HighSpeedOn:
1796 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp); 1760 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1797 case MOXA_SDS_RSTICOUNTER: 1761 case MOXA_SDS_RSTICOUNTER:
1798 lock_kernel(); 1762 spin_lock_irq(&info->slock);
1799 info->mon_data.rxcnt = 0; 1763 info->mon_data.rxcnt = 0;
1800 info->mon_data.txcnt = 0; 1764 info->mon_data.txcnt = 0;
1801 unlock_kernel(); 1765 spin_unlock_irq(&info->slock);
1802 return 0; 1766 return 0;
1803 1767
1804 case MOXA_ASPP_OQUEUE:{ 1768 case MOXA_ASPP_OQUEUE:{
1805 int len, lsr; 1769 int len, lsr;
1806 1770
1807 lock_kernel();
1808 len = mxser_chars_in_buffer(tty); 1771 len = mxser_chars_in_buffer(tty);
1772 spin_lock(&info->slock);
1809 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_THRE; 1773 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_THRE;
1774 spin_unlock_irq(&info->slock);
1810 len += (lsr ? 0 : 1); 1775 len += (lsr ? 0 : 1);
1811 unlock_kernel();
1812 1776
1813 return put_user(len, (int __user *)argp); 1777 return put_user(len, (int __user *)argp);
1814 } 1778 }
1815 case MOXA_ASPP_MON: { 1779 case MOXA_ASPP_MON: {
1816 int mcr, status; 1780 int mcr, status;
1817 1781
1818 lock_kernel(); 1782 spin_lock(&info->slock);
1819 status = mxser_get_msr(info->ioaddr, 1, tty->index); 1783 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1820 mxser_check_modem_status(tty, info, status); 1784 mxser_check_modem_status(tty, info, status);
1821 1785
1822 mcr = inb(info->ioaddr + UART_MCR); 1786 mcr = inb(info->ioaddr + UART_MCR);
1787 spin_unlock(&info->slock);
1788
1823 if (mcr & MOXA_MUST_MCR_XON_FLAG) 1789 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1824 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD; 1790 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1825 else 1791 else
@@ -1834,7 +1800,7 @@ static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1834 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD; 1800 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1835 else 1801 else
1836 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD; 1802 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1837 unlock_kernel(); 1803
1838 if (copy_to_user(argp, &info->mon_data, 1804 if (copy_to_user(argp, &info->mon_data,
1839 sizeof(struct mxser_mon))) 1805 sizeof(struct mxser_mon)))
1840 return -EFAULT; 1806 return -EFAULT;
@@ -1993,6 +1959,7 @@ static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1993{ 1959{
1994 struct mxser_port *info = tty->driver_data; 1960 struct mxser_port *info = tty->driver_data;
1995 unsigned long orig_jiffies, char_time; 1961 unsigned long orig_jiffies, char_time;
1962 unsigned long flags;
1996 int lsr; 1963 int lsr;
1997 1964
1998 if (info->type == PORT_UNKNOWN) 1965 if (info->type == PORT_UNKNOWN)
@@ -2032,19 +1999,21 @@ static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2032 timeout, char_time); 1999 timeout, char_time);
2033 printk("jiff=%lu...", jiffies); 2000 printk("jiff=%lu...", jiffies);
2034#endif 2001#endif
2035 lock_kernel(); 2002 spin_lock_irqsave(&info->slock, flags);
2036 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) { 2003 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
2037#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT 2004#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2038 printk("lsr = %d (jiff=%lu)...", lsr, jiffies); 2005 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2039#endif 2006#endif
2007 spin_unlock_irqrestore(&info->slock, flags);
2040 schedule_timeout_interruptible(char_time); 2008 schedule_timeout_interruptible(char_time);
2009 spin_lock_irqsave(&info->slock, flags);
2041 if (signal_pending(current)) 2010 if (signal_pending(current))
2042 break; 2011 break;
2043 if (timeout && time_after(jiffies, orig_jiffies + timeout)) 2012 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2044 break; 2013 break;
2045 } 2014 }
2015 spin_unlock_irqrestore(&info->slock, flags);
2046 set_current_state(TASK_RUNNING); 2016 set_current_state(TASK_RUNNING);
2047 unlock_kernel();
2048 2017
2049#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT 2018#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2050 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies); 2019 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
@@ -2059,7 +2028,6 @@ static void mxser_hangup(struct tty_struct *tty)
2059 struct mxser_port *info = tty->driver_data; 2028 struct mxser_port *info = tty->driver_data;
2060 2029
2061 mxser_flush_buffer(tty); 2030 mxser_flush_buffer(tty);
2062 mxser_shutdown(tty);
2063 tty_port_hangup(&info->port); 2031 tty_port_hangup(&info->port);
2064} 2032}
2065 2033
@@ -2363,6 +2331,8 @@ static const struct tty_operations mxser_ops = {
2363struct tty_port_operations mxser_port_ops = { 2331struct tty_port_operations mxser_port_ops = {
2364 .carrier_raised = mxser_carrier_raised, 2332 .carrier_raised = mxser_carrier_raised,
2365 .dtr_rts = mxser_dtr_rts, 2333 .dtr_rts = mxser_dtr_rts,
2334 .activate = mxser_activate,
2335 .shutdown = mxser_shutdown_port,
2366}; 2336};
2367 2337
2368/* 2338/*
diff --git a/drivers/char/pcmcia/ipwireless/tty.c b/drivers/char/pcmcia/ipwireless/tty.c
index 674b3ab3587d..2bb7874a6899 100644
--- a/drivers/char/pcmcia/ipwireless/tty.c
+++ b/drivers/char/pcmcia/ipwireless/tty.c
@@ -603,7 +603,7 @@ void ipwireless_tty_free(struct ipw_tty *tty)
603 } 603 }
604} 604}
605 605
606static struct tty_operations tty_ops = { 606static const struct tty_operations tty_ops = {
607 .open = ipw_open, 607 .open = ipw_open,
608 .close = ipw_close, 608 .close = ipw_close,
609 .hangup = ipw_hangup, 609 .hangup = ipw_hangup,
diff --git a/drivers/char/pty.c b/drivers/char/pty.c
index d86c0bc05c1c..385c44b3034f 100644
--- a/drivers/char/pty.c
+++ b/drivers/char/pty.c
@@ -659,7 +659,7 @@ static int __ptmx_open(struct inode *inode, struct file *filp)
659 if (!retval) 659 if (!retval)
660 return 0; 660 return 0;
661out1: 661out1:
662 tty_release_dev(filp); 662 tty_release(inode, filp);
663 return retval; 663 return retval;
664out: 664out:
665 devpts_kill_index(inode, index); 665 devpts_kill_index(inode, index);
diff --git a/drivers/char/riscom8.c b/drivers/char/riscom8.c
index 3cfa22d469e0..0a8d1e56c993 100644
--- a/drivers/char/riscom8.c
+++ b/drivers/char/riscom8.c
@@ -793,26 +793,21 @@ static void rc_change_speed(struct tty_struct *tty, struct riscom_board *bp,
793} 793}
794 794
795/* Must be called with interrupts enabled */ 795/* Must be called with interrupts enabled */
796static int rc_setup_port(struct tty_struct *tty, struct riscom_board *bp, 796static int rc_activate_port(struct tty_port *port, struct tty_struct *tty)
797 struct riscom_port *port)
798{ 797{
798 struct riscom_port *rp = container_of(port, struct riscom_port, port);
799 struct riscom_board *bp = port_Board(rp);
799 unsigned long flags; 800 unsigned long flags;
800 801
801 if (port->port.flags & ASYNC_INITIALIZED) 802 if (tty_port_alloc_xmit_buf(port) < 0)
802 return 0;
803
804 if (tty_port_alloc_xmit_buf(&port->port) < 0)
805 return -ENOMEM; 803 return -ENOMEM;
806 804
807 spin_lock_irqsave(&riscom_lock, flags); 805 spin_lock_irqsave(&riscom_lock, flags);
808 806
809 clear_bit(TTY_IO_ERROR, &tty->flags); 807 clear_bit(TTY_IO_ERROR, &tty->flags);
810 if (port->port.count == 1) 808 bp->count++;
811 bp->count++; 809 rp->xmit_cnt = rp->xmit_head = rp->xmit_tail = 0;
812 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0; 810 rc_change_speed(tty, bp, rp);
813 rc_change_speed(tty, bp, port);
814 port->port.flags |= ASYNC_INITIALIZED;
815
816 spin_unlock_irqrestore(&riscom_lock, flags); 811 spin_unlock_irqrestore(&riscom_lock, flags);
817 return 0; 812 return 0;
818} 813}
@@ -821,9 +816,6 @@ static int rc_setup_port(struct tty_struct *tty, struct riscom_board *bp,
821static void rc_shutdown_port(struct tty_struct *tty, 816static void rc_shutdown_port(struct tty_struct *tty,
822 struct riscom_board *bp, struct riscom_port *port) 817 struct riscom_board *bp, struct riscom_port *port)
823{ 818{
824 if (!(port->port.flags & ASYNC_INITIALIZED))
825 return;
826
827#ifdef RC_REPORT_OVERRUN 819#ifdef RC_REPORT_OVERRUN
828 printk(KERN_INFO "rc%d: port %d: Total %ld overruns were detected.\n", 820 printk(KERN_INFO "rc%d: port %d: Total %ld overruns were detected.\n",
829 board_No(bp), port_No(port), port->overrun); 821 board_No(bp), port_No(port), port->overrun);
@@ -840,11 +832,6 @@ static void rc_shutdown_port(struct tty_struct *tty,
840 } 832 }
841#endif 833#endif
842 tty_port_free_xmit_buf(&port->port); 834 tty_port_free_xmit_buf(&port->port);
843 if (C_HUPCL(tty)) {
844 /* Drop DTR */
845 bp->DTR |= (1u << port_No(port));
846 rc_out(bp, RC_DTR, bp->DTR);
847 }
848 835
849 /* Select port */ 836 /* Select port */
850 rc_out(bp, CD180_CAR, port_No(port)); 837 rc_out(bp, CD180_CAR, port_No(port));
@@ -856,7 +843,6 @@ static void rc_shutdown_port(struct tty_struct *tty,
856 rc_out(bp, CD180_IER, port->IER); 843 rc_out(bp, CD180_IER, port->IER);
857 844
858 set_bit(TTY_IO_ERROR, &tty->flags); 845 set_bit(TTY_IO_ERROR, &tty->flags);
859 port->port.flags &= ~ASYNC_INITIALIZED;
860 846
861 if (--bp->count < 0) { 847 if (--bp->count < 0) {
862 printk(KERN_INFO "rc%d: rc_shutdown_port: " 848 printk(KERN_INFO "rc%d: rc_shutdown_port: "
@@ -889,6 +875,20 @@ static int carrier_raised(struct tty_port *port)
889 return CD; 875 return CD;
890} 876}
891 877
878static void dtr_rts(struct tty_port *port, int onoff)
879{
880 struct riscom_port *p = container_of(port, struct riscom_port, port);
881 struct riscom_board *bp = port_Board(p);
882 unsigned long flags;
883
884 spin_lock_irqsave(&riscom_lock, flags);
885 bp->DTR &= ~(1u << port_No(p));
886 if (onoff == 0)
887 bp->DTR |= (1u << port_No(p));
888 rc_out(bp, RC_DTR, bp->DTR);
889 spin_unlock_irqrestore(&riscom_lock, flags);
890}
891
892static int rc_open(struct tty_struct *tty, struct file *filp) 892static int rc_open(struct tty_struct *tty, struct file *filp)
893{ 893{
894 int board; 894 int board;
@@ -909,14 +909,7 @@ static int rc_open(struct tty_struct *tty, struct file *filp)
909 if (error) 909 if (error)
910 return error; 910 return error;
911 911
912 port->port.count++; 912 return tty_port_open(&port->port, tty, filp);
913 tty->driver_data = port;
914 tty_port_tty_set(&port->port, tty);
915
916 error = rc_setup_port(tty, bp, port);
917 if (error == 0)
918 error = tty_port_block_til_ready(&port->port, tty, filp);
919 return error;
920} 913}
921 914
922static void rc_flush_buffer(struct tty_struct *tty) 915static void rc_flush_buffer(struct tty_struct *tty)
@@ -950,24 +943,23 @@ static void rc_close_port(struct tty_port *port)
950 943
951 spin_lock_irqsave(&riscom_lock, flags); 944 spin_lock_irqsave(&riscom_lock, flags);
952 rp->IER &= ~IER_RXD; 945 rp->IER &= ~IER_RXD;
953 if (port->flags & ASYNC_INITIALIZED) { 946
954 rp->IER &= ~IER_TXRDY; 947 rp->IER &= ~IER_TXRDY;
955 rp->IER |= IER_TXEMPTY; 948 rp->IER |= IER_TXEMPTY;
956 rc_out(bp, CD180_CAR, port_No(rp)); 949 rc_out(bp, CD180_CAR, port_No(rp));
957 rc_out(bp, CD180_IER, rp->IER); 950 rc_out(bp, CD180_IER, rp->IER);
958 /* 951 /*
959 * Before we drop DTR, make sure the UART transmitter 952 * Before we drop DTR, make sure the UART transmitter
960 * has completely drained; this is especially 953 * has completely drained; this is especially
961 * important if there is a transmit FIFO! 954 * important if there is a transmit FIFO!
962 */ 955 */
963 timeout = jiffies + HZ; 956 timeout = jiffies + HZ;
964 while (rp->IER & IER_TXEMPTY) { 957 while (rp->IER & IER_TXEMPTY) {
965 spin_unlock_irqrestore(&riscom_lock, flags); 958 spin_unlock_irqrestore(&riscom_lock, flags);
966 msleep_interruptible(jiffies_to_msecs(rp->timeout)); 959 msleep_interruptible(jiffies_to_msecs(rp->timeout));
967 spin_lock_irqsave(&riscom_lock, flags); 960 spin_lock_irqsave(&riscom_lock, flags);
968 if (time_after(jiffies, timeout)) 961 if (time_after(jiffies, timeout))
969 break; 962 break;
970 }
971 } 963 }
972 rc_shutdown_port(port->tty, bp, rp); 964 rc_shutdown_port(port->tty, bp, rp);
973 spin_unlock_irqrestore(&riscom_lock, flags); 965 spin_unlock_irqrestore(&riscom_lock, flags);
@@ -1354,7 +1346,6 @@ static void rc_hangup(struct tty_struct *tty)
1354 if (rc_paranoia_check(port, tty->name, "rc_hangup")) 1346 if (rc_paranoia_check(port, tty->name, "rc_hangup"))
1355 return; 1347 return;
1356 1348
1357 rc_shutdown_port(tty, port_Board(port), port);
1358 tty_port_hangup(&port->port); 1349 tty_port_hangup(&port->port);
1359} 1350}
1360 1351
@@ -1401,7 +1392,9 @@ static const struct tty_operations riscom_ops = {
1401 1392
1402static const struct tty_port_operations riscom_port_ops = { 1393static const struct tty_port_operations riscom_port_ops = {
1403 .carrier_raised = carrier_raised, 1394 .carrier_raised = carrier_raised,
1395 .dtr_rts = dtr_rts,
1404 .shutdown = rc_close_port, 1396 .shutdown = rc_close_port,
1397 .activate = rc_activate_port,
1405}; 1398};
1406 1399
1407 1400
diff --git a/drivers/char/stallion.c b/drivers/char/stallion.c
index db6dcfa35ba0..0e511d61f544 100644
--- a/drivers/char/stallion.c
+++ b/drivers/char/stallion.c
@@ -407,7 +407,7 @@ static unsigned int stl_baudrates[] = {
407 * Declare all those functions in this driver! 407 * Declare all those functions in this driver!
408 */ 408 */
409 409
410static int stl_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg); 410static long stl_memioctl(struct file *fp, unsigned int cmd, unsigned long arg);
411static int stl_brdinit(struct stlbrd *brdp); 411static int stl_brdinit(struct stlbrd *brdp);
412static int stl_getportstats(struct tty_struct *tty, struct stlport *portp, comstats_t __user *cp); 412static int stl_getportstats(struct tty_struct *tty, struct stlport *portp, comstats_t __user *cp);
413static int stl_clrportstats(struct stlport *portp, comstats_t __user *cp); 413static int stl_clrportstats(struct stlport *portp, comstats_t __user *cp);
@@ -607,7 +607,7 @@ static unsigned int sc26198_baudtable[] = {
607 */ 607 */
608static const struct file_operations stl_fsiomem = { 608static const struct file_operations stl_fsiomem = {
609 .owner = THIS_MODULE, 609 .owner = THIS_MODULE,
610 .ioctl = stl_memioctl, 610 .unlocked_ioctl = stl_memioctl,
611}; 611};
612 612
613static struct class *stallion_class; 613static struct class *stallion_class;
@@ -702,6 +702,24 @@ static struct stlbrd *stl_allocbrd(void)
702 702
703/*****************************************************************************/ 703/*****************************************************************************/
704 704
705static int stl_activate(struct tty_port *port, struct tty_struct *tty)
706{
707 struct stlport *portp = container_of(port, struct stlport, port);
708 if (!portp->tx.buf) {
709 portp->tx.buf = kmalloc(STL_TXBUFSIZE, GFP_KERNEL);
710 if (!portp->tx.buf)
711 return -ENOMEM;
712 portp->tx.head = portp->tx.buf;
713 portp->tx.tail = portp->tx.buf;
714 }
715 stl_setport(portp, tty->termios);
716 portp->sigs = stl_getsignals(portp);
717 stl_setsignals(portp, 1, 1);
718 stl_enablerxtx(portp, 1, 1);
719 stl_startrxtx(portp, 1, 0);
720 return 0;
721}
722
705static int stl_open(struct tty_struct *tty, struct file *filp) 723static int stl_open(struct tty_struct *tty, struct file *filp)
706{ 724{
707 struct stlport *portp; 725 struct stlport *portp;
@@ -737,32 +755,8 @@ static int stl_open(struct tty_struct *tty, struct file *filp)
737 if (portp == NULL) 755 if (portp == NULL)
738 return -ENODEV; 756 return -ENODEV;
739 port = &portp->port; 757 port = &portp->port;
758 return tty_port_open(&portp->port, tty, filp);
740 759
741/*
742 * On the first open of the device setup the port hardware, and
743 * initialize the per port data structure.
744 */
745 tty_port_tty_set(port, tty);
746 tty->driver_data = portp;
747 port->count++;
748
749 if ((port->flags & ASYNC_INITIALIZED) == 0) {
750 if (!portp->tx.buf) {
751 portp->tx.buf = kmalloc(STL_TXBUFSIZE, GFP_KERNEL);
752 if (!portp->tx.buf)
753 return -ENOMEM;
754 portp->tx.head = portp->tx.buf;
755 portp->tx.tail = portp->tx.buf;
756 }
757 stl_setport(portp, tty->termios);
758 portp->sigs = stl_getsignals(portp);
759 stl_setsignals(portp, 1, 1);
760 stl_enablerxtx(portp, 1, 1);
761 stl_startrxtx(portp, 1, 0);
762 clear_bit(TTY_IO_ERROR, &tty->flags);
763 port->flags |= ASYNC_INITIALIZED;
764 }
765 return tty_port_block_til_ready(port, tty, filp);
766} 760}
767 761
768/*****************************************************************************/ 762/*****************************************************************************/
@@ -826,38 +820,12 @@ static void stl_waituntilsent(struct tty_struct *tty, int timeout)
826 820
827/*****************************************************************************/ 821/*****************************************************************************/
828 822
829static void stl_close(struct tty_struct *tty, struct file *filp) 823static void stl_shutdown(struct tty_port *port)
830{ 824{
831 struct stlport *portp; 825 struct stlport *portp = container_of(port, struct stlport, port);
832 struct tty_port *port;
833 unsigned long flags;
834
835 pr_debug("stl_close(tty=%p,filp=%p)\n", tty, filp);
836
837 portp = tty->driver_data;
838 BUG_ON(portp == NULL);
839
840 port = &portp->port;
841
842 if (tty_port_close_start(port, tty, filp) == 0)
843 return;
844/*
845 * May want to wait for any data to drain before closing. The BUSY
846 * flag keeps track of whether we are still sending or not - it is
847 * very accurate for the cd1400, not quite so for the sc26198.
848 * (The sc26198 has no "end-of-data" interrupt only empty FIFO)
849 */
850 stl_waituntilsent(tty, (HZ / 2));
851
852 spin_lock_irqsave(&port->lock, flags);
853 portp->port.flags &= ~ASYNC_INITIALIZED;
854 spin_unlock_irqrestore(&port->lock, flags);
855
856 stl_disableintrs(portp); 826 stl_disableintrs(portp);
857 if (tty->termios->c_cflag & HUPCL)
858 stl_setsignals(portp, 0, 0);
859 stl_enablerxtx(portp, 0, 0); 827 stl_enablerxtx(portp, 0, 0);
860 stl_flushbuffer(tty); 828 stl_flush(portp);
861 portp->istate = 0; 829 portp->istate = 0;
862 if (portp->tx.buf != NULL) { 830 if (portp->tx.buf != NULL) {
863 kfree(portp->tx.buf); 831 kfree(portp->tx.buf);
@@ -865,9 +833,16 @@ static void stl_close(struct tty_struct *tty, struct file *filp)
865 portp->tx.head = NULL; 833 portp->tx.head = NULL;
866 portp->tx.tail = NULL; 834 portp->tx.tail = NULL;
867 } 835 }
836}
837
838static void stl_close(struct tty_struct *tty, struct file *filp)
839{
840 struct stlport*portp;
841 pr_debug("stl_close(tty=%p,filp=%p)\n", tty, filp);
868 842
869 tty_port_close_end(port, tty); 843 portp = tty->driver_data;
870 tty_port_tty_set(port, NULL); 844 BUG_ON(portp == NULL);
845 tty_port_close(&portp->port, tty, filp);
871} 846}
872 847
873/*****************************************************************************/ 848/*****************************************************************************/
@@ -1314,35 +1289,12 @@ static void stl_stop(struct tty_struct *tty)
1314 1289
1315static void stl_hangup(struct tty_struct *tty) 1290static void stl_hangup(struct tty_struct *tty)
1316{ 1291{
1317 struct stlport *portp; 1292 struct stlport *portp = tty->driver_data;
1318 struct tty_port *port;
1319 unsigned long flags;
1320
1321 pr_debug("stl_hangup(tty=%p)\n", tty); 1293 pr_debug("stl_hangup(tty=%p)\n", tty);
1322 1294
1323 portp = tty->driver_data;
1324 if (portp == NULL) 1295 if (portp == NULL)
1325 return; 1296 return;
1326 port = &portp->port; 1297 tty_port_hangup(&portp->port);
1327
1328 spin_lock_irqsave(&port->lock, flags);
1329 port->flags &= ~ASYNC_INITIALIZED;
1330 spin_unlock_irqrestore(&port->lock, flags);
1331
1332 stl_disableintrs(portp);
1333 if (tty->termios->c_cflag & HUPCL)
1334 stl_setsignals(portp, 0, 0);
1335 stl_enablerxtx(portp, 0, 0);
1336 stl_flushbuffer(tty);
1337 portp->istate = 0;
1338 set_bit(TTY_IO_ERROR, &tty->flags);
1339 if (portp->tx.buf != NULL) {
1340 kfree(portp->tx.buf);
1341 portp->tx.buf = NULL;
1342 portp->tx.head = NULL;
1343 portp->tx.tail = NULL;
1344 }
1345 tty_port_hangup(port);
1346} 1298}
1347 1299
1348/*****************************************************************************/ 1300/*****************************************************************************/
@@ -2486,18 +2438,19 @@ static int stl_getbrdstruct(struct stlbrd __user *arg)
2486 * collection. 2438 * collection.
2487 */ 2439 */
2488 2440
2489static int stl_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg) 2441static long stl_memioctl(struct file *fp, unsigned int cmd, unsigned long arg)
2490{ 2442{
2491 int brdnr, rc; 2443 int brdnr, rc;
2492 void __user *argp = (void __user *)arg; 2444 void __user *argp = (void __user *)arg;
2493 2445
2494 pr_debug("stl_memioctl(ip=%p,fp=%p,cmd=%x,arg=%lx)\n", ip, fp, cmd,arg); 2446 pr_debug("stl_memioctl(fp=%p,cmd=%x,arg=%lx)\n", fp, cmd,arg);
2495 2447
2496 brdnr = iminor(ip); 2448 brdnr = iminor(fp->f_dentry->d_inode);
2497 if (brdnr >= STL_MAXBRDS) 2449 if (brdnr >= STL_MAXBRDS)
2498 return -ENODEV; 2450 return -ENODEV;
2499 rc = 0; 2451 rc = 0;
2500 2452
2453 lock_kernel();
2501 switch (cmd) { 2454 switch (cmd) {
2502 case COM_GETPORTSTATS: 2455 case COM_GETPORTSTATS:
2503 rc = stl_getportstats(NULL, NULL, argp); 2456 rc = stl_getportstats(NULL, NULL, argp);
@@ -2518,7 +2471,7 @@ static int stl_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, uns
2518 rc = -ENOIOCTLCMD; 2471 rc = -ENOIOCTLCMD;
2519 break; 2472 break;
2520 } 2473 }
2521 2474 unlock_kernel();
2522 return rc; 2475 return rc;
2523} 2476}
2524 2477
@@ -2549,6 +2502,8 @@ static const struct tty_operations stl_ops = {
2549static const struct tty_port_operations stl_port_ops = { 2502static const struct tty_port_operations stl_port_ops = {
2550 .carrier_raised = stl_carrier_raised, 2503 .carrier_raised = stl_carrier_raised,
2551 .dtr_rts = stl_dtr_rts, 2504 .dtr_rts = stl_dtr_rts,
2505 .activate = stl_activate,
2506 .shutdown = stl_shutdown,
2552}; 2507};
2553 2508
2554/*****************************************************************************/ 2509/*****************************************************************************/
diff --git a/drivers/char/tty_io.c b/drivers/char/tty_io.c
index 59499ee0fe6a..684f0e0b175e 100644
--- a/drivers/char/tty_io.c
+++ b/drivers/char/tty_io.c
@@ -142,7 +142,6 @@ ssize_t redirected_tty_write(struct file *, const char __user *,
142 size_t, loff_t *); 142 size_t, loff_t *);
143static unsigned int tty_poll(struct file *, poll_table *); 143static unsigned int tty_poll(struct file *, poll_table *);
144static int tty_open(struct inode *, struct file *); 144static int tty_open(struct inode *, struct file *);
145static int tty_release(struct inode *, struct file *);
146long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 145long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
147#ifdef CONFIG_COMPAT 146#ifdef CONFIG_COMPAT
148static long tty_compat_ioctl(struct file *file, unsigned int cmd, 147static long tty_compat_ioctl(struct file *file, unsigned int cmd,
@@ -506,8 +505,6 @@ static void do_tty_hangup(struct work_struct *work)
506 if (!tty) 505 if (!tty)
507 return; 506 return;
508 507
509 /* inuse_filps is protected by the single kernel lock */
510 lock_kernel();
511 508
512 spin_lock(&redirect_lock); 509 spin_lock(&redirect_lock);
513 if (redirect && redirect->private_data == tty) { 510 if (redirect && redirect->private_data == tty) {
@@ -516,7 +513,11 @@ static void do_tty_hangup(struct work_struct *work)
516 } 513 }
517 spin_unlock(&redirect_lock); 514 spin_unlock(&redirect_lock);
518 515
516 /* inuse_filps is protected by the single kernel lock */
517 lock_kernel();
519 check_tty_count(tty, "do_tty_hangup"); 518 check_tty_count(tty, "do_tty_hangup");
519 unlock_kernel();
520
520 file_list_lock(); 521 file_list_lock();
521 /* This breaks for file handles being sent over AF_UNIX sockets ? */ 522 /* This breaks for file handles being sent over AF_UNIX sockets ? */
522 list_for_each_entry(filp, &tty->tty_files, f_u.fu_list) { 523 list_for_each_entry(filp, &tty->tty_files, f_u.fu_list) {
@@ -530,6 +531,7 @@ static void do_tty_hangup(struct work_struct *work)
530 } 531 }
531 file_list_unlock(); 532 file_list_unlock();
532 533
534 lock_kernel();
533 tty_ldisc_hangup(tty); 535 tty_ldisc_hangup(tty);
534 536
535 read_lock(&tasklist_lock); 537 read_lock(&tasklist_lock);
@@ -708,6 +710,8 @@ void disassociate_ctty(int on_exit)
708 struct tty_struct *tty; 710 struct tty_struct *tty;
709 struct pid *tty_pgrp = NULL; 711 struct pid *tty_pgrp = NULL;
710 712
713 if (!current->signal->leader)
714 return;
711 715
712 tty = get_current_tty(); 716 tty = get_current_tty();
713 if (tty) { 717 if (tty) {
@@ -773,8 +777,7 @@ void no_tty(void)
773{ 777{
774 struct task_struct *tsk = current; 778 struct task_struct *tsk = current;
775 lock_kernel(); 779 lock_kernel();
776 if (tsk->signal->leader) 780 disassociate_ctty(0);
777 disassociate_ctty(0);
778 unlock_kernel(); 781 unlock_kernel();
779 proc_clear_tty(tsk); 782 proc_clear_tty(tsk);
780} 783}
@@ -1017,14 +1020,16 @@ out:
1017 1020
1018void tty_write_message(struct tty_struct *tty, char *msg) 1021void tty_write_message(struct tty_struct *tty, char *msg)
1019{ 1022{
1020 lock_kernel();
1021 if (tty) { 1023 if (tty) {
1022 mutex_lock(&tty->atomic_write_lock); 1024 mutex_lock(&tty->atomic_write_lock);
1023 if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) 1025 lock_kernel();
1026 if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) {
1027 unlock_kernel();
1024 tty->ops->write(tty, msg, strlen(msg)); 1028 tty->ops->write(tty, msg, strlen(msg));
1029 } else
1030 unlock_kernel();
1025 tty_write_unlock(tty); 1031 tty_write_unlock(tty);
1026 } 1032 }
1027 unlock_kernel();
1028 return; 1033 return;
1029} 1034}
1030 1035
@@ -1202,14 +1207,21 @@ static int tty_driver_install_tty(struct tty_driver *driver,
1202 struct tty_struct *tty) 1207 struct tty_struct *tty)
1203{ 1208{
1204 int idx = tty->index; 1209 int idx = tty->index;
1210 int ret;
1205 1211
1206 if (driver->ops->install) 1212 if (driver->ops->install) {
1207 return driver->ops->install(driver, tty); 1213 lock_kernel();
1214 ret = driver->ops->install(driver, tty);
1215 unlock_kernel();
1216 return ret;
1217 }
1208 1218
1209 if (tty_init_termios(tty) == 0) { 1219 if (tty_init_termios(tty) == 0) {
1220 lock_kernel();
1210 tty_driver_kref_get(driver); 1221 tty_driver_kref_get(driver);
1211 tty->count++; 1222 tty->count++;
1212 driver->ttys[idx] = tty; 1223 driver->ttys[idx] = tty;
1224 unlock_kernel();
1213 return 0; 1225 return 0;
1214 } 1226 }
1215 return -ENOMEM; 1227 return -ENOMEM;
@@ -1302,10 +1314,14 @@ struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx,
1302 struct tty_struct *tty; 1314 struct tty_struct *tty;
1303 int retval; 1315 int retval;
1304 1316
1317 lock_kernel();
1305 /* Check if pty master is being opened multiple times */ 1318 /* Check if pty master is being opened multiple times */
1306 if (driver->subtype == PTY_TYPE_MASTER && 1319 if (driver->subtype == PTY_TYPE_MASTER &&
1307 (driver->flags & TTY_DRIVER_DEVPTS_MEM) && !first_ok) 1320 (driver->flags & TTY_DRIVER_DEVPTS_MEM) && !first_ok) {
1321 unlock_kernel();
1308 return ERR_PTR(-EIO); 1322 return ERR_PTR(-EIO);
1323 }
1324 unlock_kernel();
1309 1325
1310 /* 1326 /*
1311 * First time open is complex, especially for PTY devices. 1327 * First time open is complex, especially for PTY devices.
@@ -1335,7 +1351,6 @@ struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx,
1335 * If we fail here just call release_tty to clean up. No need 1351 * If we fail here just call release_tty to clean up. No need
1336 * to decrement the use counts, as release_tty doesn't care. 1352 * to decrement the use counts, as release_tty doesn't care.
1337 */ 1353 */
1338
1339 retval = tty_ldisc_setup(tty, tty->link); 1354 retval = tty_ldisc_setup(tty, tty->link);
1340 if (retval) 1355 if (retval)
1341 goto release_mem_out; 1356 goto release_mem_out;
@@ -1350,7 +1365,9 @@ release_mem_out:
1350 if (printk_ratelimit()) 1365 if (printk_ratelimit())
1351 printk(KERN_INFO "tty_init_dev: ldisc open failed, " 1366 printk(KERN_INFO "tty_init_dev: ldisc open failed, "
1352 "clearing slot %d\n", idx); 1367 "clearing slot %d\n", idx);
1368 lock_kernel();
1353 release_tty(tty, idx); 1369 release_tty(tty, idx);
1370 unlock_kernel();
1354 return ERR_PTR(retval); 1371 return ERR_PTR(retval);
1355} 1372}
1356 1373
@@ -1464,7 +1481,17 @@ static void release_tty(struct tty_struct *tty, int idx)
1464 tty_kref_put(tty); 1481 tty_kref_put(tty);
1465} 1482}
1466 1483
1467/* 1484/**
1485 * tty_release - vfs callback for close
1486 * @inode: inode of tty
1487 * @filp: file pointer for handle to tty
1488 *
1489 * Called the last time each file handle is closed that references
1490 * this tty. There may however be several such references.
1491 *
1492 * Locking:
1493 * Takes bkl. See tty_release_dev
1494 *
1468 * Even releasing the tty structures is a tricky business.. We have 1495 * Even releasing the tty structures is a tricky business.. We have
1469 * to be very careful that the structures are all released at the 1496 * to be very careful that the structures are all released at the
1470 * same time, as interrupts might otherwise get the wrong pointers. 1497 * same time, as interrupts might otherwise get the wrong pointers.
@@ -1472,20 +1499,20 @@ static void release_tty(struct tty_struct *tty, int idx)
1472 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could 1499 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1473 * lead to double frees or releasing memory still in use. 1500 * lead to double frees or releasing memory still in use.
1474 */ 1501 */
1475void tty_release_dev(struct file *filp) 1502
1503int tty_release(struct inode *inode, struct file *filp)
1476{ 1504{
1477 struct tty_struct *tty, *o_tty; 1505 struct tty_struct *tty, *o_tty;
1478 int pty_master, tty_closing, o_tty_closing, do_sleep; 1506 int pty_master, tty_closing, o_tty_closing, do_sleep;
1479 int devpts; 1507 int devpts;
1480 int idx; 1508 int idx;
1481 char buf[64]; 1509 char buf[64];
1482 struct inode *inode;
1483 1510
1484 inode = filp->f_path.dentry->d_inode;
1485 tty = (struct tty_struct *)filp->private_data; 1511 tty = (struct tty_struct *)filp->private_data;
1486 if (tty_paranoia_check(tty, inode, "tty_release_dev")) 1512 if (tty_paranoia_check(tty, inode, "tty_release_dev"))
1487 return; 1513 return 0;
1488 1514
1515 lock_kernel();
1489 check_tty_count(tty, "tty_release_dev"); 1516 check_tty_count(tty, "tty_release_dev");
1490 1517
1491 tty_fasync(-1, filp, 0); 1518 tty_fasync(-1, filp, 0);
@@ -1500,19 +1527,22 @@ void tty_release_dev(struct file *filp)
1500 if (idx < 0 || idx >= tty->driver->num) { 1527 if (idx < 0 || idx >= tty->driver->num) {
1501 printk(KERN_DEBUG "tty_release_dev: bad idx when trying to " 1528 printk(KERN_DEBUG "tty_release_dev: bad idx when trying to "
1502 "free (%s)\n", tty->name); 1529 "free (%s)\n", tty->name);
1503 return; 1530 unlock_kernel();
1531 return 0;
1504 } 1532 }
1505 if (!devpts) { 1533 if (!devpts) {
1506 if (tty != tty->driver->ttys[idx]) { 1534 if (tty != tty->driver->ttys[idx]) {
1535 unlock_kernel();
1507 printk(KERN_DEBUG "tty_release_dev: driver.table[%d] not tty " 1536 printk(KERN_DEBUG "tty_release_dev: driver.table[%d] not tty "
1508 "for (%s)\n", idx, tty->name); 1537 "for (%s)\n", idx, tty->name);
1509 return; 1538 return 0;
1510 } 1539 }
1511 if (tty->termios != tty->driver->termios[idx]) { 1540 if (tty->termios != tty->driver->termios[idx]) {
1541 unlock_kernel();
1512 printk(KERN_DEBUG "tty_release_dev: driver.termios[%d] not termios " 1542 printk(KERN_DEBUG "tty_release_dev: driver.termios[%d] not termios "
1513 "for (%s)\n", 1543 "for (%s)\n",
1514 idx, tty->name); 1544 idx, tty->name);
1515 return; 1545 return 0;
1516 } 1546 }
1517 } 1547 }
1518#endif 1548#endif
@@ -1526,26 +1556,30 @@ void tty_release_dev(struct file *filp)
1526 if (tty->driver->other && 1556 if (tty->driver->other &&
1527 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) { 1557 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1528 if (o_tty != tty->driver->other->ttys[idx]) { 1558 if (o_tty != tty->driver->other->ttys[idx]) {
1559 unlock_kernel();
1529 printk(KERN_DEBUG "tty_release_dev: other->table[%d] " 1560 printk(KERN_DEBUG "tty_release_dev: other->table[%d] "
1530 "not o_tty for (%s)\n", 1561 "not o_tty for (%s)\n",
1531 idx, tty->name); 1562 idx, tty->name);
1532 return; 1563 return 0 ;
1533 } 1564 }
1534 if (o_tty->termios != tty->driver->other->termios[idx]) { 1565 if (o_tty->termios != tty->driver->other->termios[idx]) {
1566 unlock_kernel();
1535 printk(KERN_DEBUG "tty_release_dev: other->termios[%d] " 1567 printk(KERN_DEBUG "tty_release_dev: other->termios[%d] "
1536 "not o_termios for (%s)\n", 1568 "not o_termios for (%s)\n",
1537 idx, tty->name); 1569 idx, tty->name);
1538 return; 1570 return 0;
1539 } 1571 }
1540 if (o_tty->link != tty) { 1572 if (o_tty->link != tty) {
1573 unlock_kernel();
1541 printk(KERN_DEBUG "tty_release_dev: bad pty pointers\n"); 1574 printk(KERN_DEBUG "tty_release_dev: bad pty pointers\n");
1542 return; 1575 return 0;
1543 } 1576 }
1544 } 1577 }
1545#endif 1578#endif
1546 if (tty->ops->close) 1579 if (tty->ops->close)
1547 tty->ops->close(tty, filp); 1580 tty->ops->close(tty, filp);
1548 1581
1582 unlock_kernel();
1549 /* 1583 /*
1550 * Sanity check: if tty->count is going to zero, there shouldn't be 1584 * Sanity check: if tty->count is going to zero, there shouldn't be
1551 * any waiters on tty->read_wait or tty->write_wait. We test the 1585 * any waiters on tty->read_wait or tty->write_wait. We test the
@@ -1568,6 +1602,7 @@ void tty_release_dev(struct file *filp)
1568 opens on /dev/tty */ 1602 opens on /dev/tty */
1569 1603
1570 mutex_lock(&tty_mutex); 1604 mutex_lock(&tty_mutex);
1605 lock_kernel();
1571 tty_closing = tty->count <= 1; 1606 tty_closing = tty->count <= 1;
1572 o_tty_closing = o_tty && 1607 o_tty_closing = o_tty &&
1573 (o_tty->count <= (pty_master ? 1 : 0)); 1608 (o_tty->count <= (pty_master ? 1 : 0));
@@ -1598,6 +1633,7 @@ void tty_release_dev(struct file *filp)
1598 1633
1599 printk(KERN_WARNING "tty_release_dev: %s: read/write wait queue " 1634 printk(KERN_WARNING "tty_release_dev: %s: read/write wait queue "
1600 "active!\n", tty_name(tty, buf)); 1635 "active!\n", tty_name(tty, buf));
1636 unlock_kernel();
1601 mutex_unlock(&tty_mutex); 1637 mutex_unlock(&tty_mutex);
1602 schedule(); 1638 schedule();
1603 } 1639 }
@@ -1661,8 +1697,10 @@ void tty_release_dev(struct file *filp)
1661 mutex_unlock(&tty_mutex); 1697 mutex_unlock(&tty_mutex);
1662 1698
1663 /* check whether both sides are closing ... */ 1699 /* check whether both sides are closing ... */
1664 if (!tty_closing || (o_tty && !o_tty_closing)) 1700 if (!tty_closing || (o_tty && !o_tty_closing)) {
1665 return; 1701 unlock_kernel();
1702 return 0;
1703 }
1666 1704
1667#ifdef TTY_DEBUG_HANGUP 1705#ifdef TTY_DEBUG_HANGUP
1668 printk(KERN_DEBUG "freeing tty structure..."); 1706 printk(KERN_DEBUG "freeing tty structure...");
@@ -1680,10 +1718,12 @@ void tty_release_dev(struct file *filp)
1680 /* Make this pty number available for reallocation */ 1718 /* Make this pty number available for reallocation */
1681 if (devpts) 1719 if (devpts)
1682 devpts_kill_index(inode, idx); 1720 devpts_kill_index(inode, idx);
1721 unlock_kernel();
1722 return 0;
1683} 1723}
1684 1724
1685/** 1725/**
1686 * __tty_open - open a tty device 1726 * tty_open - open a tty device
1687 * @inode: inode of device file 1727 * @inode: inode of device file
1688 * @filp: file pointer to tty 1728 * @filp: file pointer to tty
1689 * 1729 *
@@ -1703,7 +1743,7 @@ void tty_release_dev(struct file *filp)
1703 * ->siglock protects ->signal/->sighand 1743 * ->siglock protects ->signal/->sighand
1704 */ 1744 */
1705 1745
1706static int __tty_open(struct inode *inode, struct file *filp) 1746static int tty_open(struct inode *inode, struct file *filp)
1707{ 1747{
1708 struct tty_struct *tty = NULL; 1748 struct tty_struct *tty = NULL;
1709 int noctty, retval; 1749 int noctty, retval;
@@ -1720,10 +1760,12 @@ retry_open:
1720 retval = 0; 1760 retval = 0;
1721 1761
1722 mutex_lock(&tty_mutex); 1762 mutex_lock(&tty_mutex);
1763 lock_kernel();
1723 1764
1724 if (device == MKDEV(TTYAUX_MAJOR, 0)) { 1765 if (device == MKDEV(TTYAUX_MAJOR, 0)) {
1725 tty = get_current_tty(); 1766 tty = get_current_tty();
1726 if (!tty) { 1767 if (!tty) {
1768 unlock_kernel();
1727 mutex_unlock(&tty_mutex); 1769 mutex_unlock(&tty_mutex);
1728 return -ENXIO; 1770 return -ENXIO;
1729 } 1771 }
@@ -1755,12 +1797,14 @@ retry_open:
1755 goto got_driver; 1797 goto got_driver;
1756 } 1798 }
1757 } 1799 }
1800 unlock_kernel();
1758 mutex_unlock(&tty_mutex); 1801 mutex_unlock(&tty_mutex);
1759 return -ENODEV; 1802 return -ENODEV;
1760 } 1803 }
1761 1804
1762 driver = get_tty_driver(device, &index); 1805 driver = get_tty_driver(device, &index);
1763 if (!driver) { 1806 if (!driver) {
1807 unlock_kernel();
1764 mutex_unlock(&tty_mutex); 1808 mutex_unlock(&tty_mutex);
1765 return -ENODEV; 1809 return -ENODEV;
1766 } 1810 }
@@ -1770,6 +1814,7 @@ got_driver:
1770 tty = tty_driver_lookup_tty(driver, inode, index); 1814 tty = tty_driver_lookup_tty(driver, inode, index);
1771 1815
1772 if (IS_ERR(tty)) { 1816 if (IS_ERR(tty)) {
1817 unlock_kernel();
1773 mutex_unlock(&tty_mutex); 1818 mutex_unlock(&tty_mutex);
1774 return PTR_ERR(tty); 1819 return PTR_ERR(tty);
1775 } 1820 }
@@ -1784,8 +1829,10 @@ got_driver:
1784 1829
1785 mutex_unlock(&tty_mutex); 1830 mutex_unlock(&tty_mutex);
1786 tty_driver_kref_put(driver); 1831 tty_driver_kref_put(driver);
1787 if (IS_ERR(tty)) 1832 if (IS_ERR(tty)) {
1833 unlock_kernel();
1788 return PTR_ERR(tty); 1834 return PTR_ERR(tty);
1835 }
1789 1836
1790 filp->private_data = tty; 1837 filp->private_data = tty;
1791 file_move(filp, &tty->tty_files); 1838 file_move(filp, &tty->tty_files);
@@ -1813,11 +1860,15 @@ got_driver:
1813 printk(KERN_DEBUG "error %d in opening %s...", retval, 1860 printk(KERN_DEBUG "error %d in opening %s...", retval,
1814 tty->name); 1861 tty->name);
1815#endif 1862#endif
1816 tty_release_dev(filp); 1863 tty_release(inode, filp);
1817 if (retval != -ERESTARTSYS) 1864 if (retval != -ERESTARTSYS) {
1865 unlock_kernel();
1818 return retval; 1866 return retval;
1819 if (signal_pending(current)) 1867 }
1868 if (signal_pending(current)) {
1869 unlock_kernel();
1820 return retval; 1870 return retval;
1871 }
1821 schedule(); 1872 schedule();
1822 /* 1873 /*
1823 * Need to reset f_op in case a hangup happened. 1874 * Need to reset f_op in case a hangup happened.
@@ -1826,8 +1877,11 @@ got_driver:
1826 filp->f_op = &tty_fops; 1877 filp->f_op = &tty_fops;
1827 goto retry_open; 1878 goto retry_open;
1828 } 1879 }
1880 unlock_kernel();
1881
1829 1882
1830 mutex_lock(&tty_mutex); 1883 mutex_lock(&tty_mutex);
1884 lock_kernel();
1831 spin_lock_irq(&current->sighand->siglock); 1885 spin_lock_irq(&current->sighand->siglock);
1832 if (!noctty && 1886 if (!noctty &&
1833 current->signal->leader && 1887 current->signal->leader &&
@@ -1835,43 +1889,12 @@ got_driver:
1835 tty->session == NULL) 1889 tty->session == NULL)
1836 __proc_set_tty(current, tty); 1890 __proc_set_tty(current, tty);
1837 spin_unlock_irq(&current->sighand->siglock); 1891 spin_unlock_irq(&current->sighand->siglock);
1892 unlock_kernel();
1838 mutex_unlock(&tty_mutex); 1893 mutex_unlock(&tty_mutex);
1839 return 0; 1894 return 0;
1840} 1895}
1841 1896
1842/* BKL pushdown: scary code avoidance wrapper */
1843static int tty_open(struct inode *inode, struct file *filp)
1844{
1845 int ret;
1846
1847 lock_kernel();
1848 ret = __tty_open(inode, filp);
1849 unlock_kernel();
1850 return ret;
1851}
1852
1853
1854
1855
1856/**
1857 * tty_release - vfs callback for close
1858 * @inode: inode of tty
1859 * @filp: file pointer for handle to tty
1860 *
1861 * Called the last time each file handle is closed that references
1862 * this tty. There may however be several such references.
1863 *
1864 * Locking:
1865 * Takes bkl. See tty_release_dev
1866 */
1867 1897
1868static int tty_release(struct inode *inode, struct file *filp)
1869{
1870 lock_kernel();
1871 tty_release_dev(filp);
1872 unlock_kernel();
1873 return 0;
1874}
1875 1898
1876/** 1899/**
1877 * tty_poll - check tty status 1900 * tty_poll - check tty status
@@ -2317,9 +2340,7 @@ static int tiocsetd(struct tty_struct *tty, int __user *p)
2317 if (get_user(ldisc, p)) 2340 if (get_user(ldisc, p))
2318 return -EFAULT; 2341 return -EFAULT;
2319 2342
2320 lock_kernel();
2321 ret = tty_set_ldisc(tty, ldisc); 2343 ret = tty_set_ldisc(tty, ldisc);
2322 unlock_kernel();
2323 2344
2324 return ret; 2345 return ret;
2325} 2346}
diff --git a/drivers/char/tty_ldisc.c b/drivers/char/tty_ldisc.c
index feb55075819b..3f653f7d849f 100644
--- a/drivers/char/tty_ldisc.c
+++ b/drivers/char/tty_ldisc.c
@@ -34,6 +34,8 @@
34#include <linux/vt_kern.h> 34#include <linux/vt_kern.h>
35#include <linux/selection.h> 35#include <linux/selection.h>
36 36
37#include <linux/smp_lock.h> /* For the moment */
38
37#include <linux/kmod.h> 39#include <linux/kmod.h>
38#include <linux/nsproxy.h> 40#include <linux/nsproxy.h>
39 41
@@ -443,8 +445,14 @@ static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
443static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld) 445static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
444{ 446{
445 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags)); 447 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
446 if (ld->ops->open) 448 if (ld->ops->open) {
447 return ld->ops->open(tty); 449 int ret;
450 /* BKL here locks verus a hangup event */
451 lock_kernel();
452 ret = ld->ops->open(tty);
453 unlock_kernel();
454 return ret;
455 }
448 return 0; 456 return 0;
449} 457}
450 458
@@ -545,6 +553,7 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
545 if (IS_ERR(new_ldisc)) 553 if (IS_ERR(new_ldisc))
546 return PTR_ERR(new_ldisc); 554 return PTR_ERR(new_ldisc);
547 555
556 lock_kernel();
548 /* 557 /*
549 * We need to look at the tty locking here for pty/tty pairs 558 * We need to look at the tty locking here for pty/tty pairs
550 * when both sides try to change in parallel. 559 * when both sides try to change in parallel.
@@ -558,10 +567,12 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
558 */ 567 */
559 568
560 if (tty->ldisc->ops->num == ldisc) { 569 if (tty->ldisc->ops->num == ldisc) {
570 unlock_kernel();
561 tty_ldisc_put(new_ldisc); 571 tty_ldisc_put(new_ldisc);
562 return 0; 572 return 0;
563 } 573 }
564 574
575 unlock_kernel();
565 /* 576 /*
566 * Problem: What do we do if this blocks ? 577 * Problem: What do we do if this blocks ?
567 * We could deadlock here 578 * We could deadlock here
@@ -582,6 +593,9 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
582 test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0); 593 test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
583 mutex_lock(&tty->ldisc_mutex); 594 mutex_lock(&tty->ldisc_mutex);
584 } 595 }
596
597 lock_kernel();
598
585 set_bit(TTY_LDISC_CHANGING, &tty->flags); 599 set_bit(TTY_LDISC_CHANGING, &tty->flags);
586 600
587 /* 601 /*
@@ -592,6 +606,8 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
592 tty->receive_room = 0; 606 tty->receive_room = 0;
593 607
594 o_ldisc = tty->ldisc; 608 o_ldisc = tty->ldisc;
609
610 unlock_kernel();
595 /* 611 /*
596 * Make sure we don't change while someone holds a 612 * Make sure we don't change while someone holds a
597 * reference to the line discipline. The TTY_LDISC bit 613 * reference to the line discipline. The TTY_LDISC bit
@@ -617,12 +633,14 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
617 flush_scheduled_work(); 633 flush_scheduled_work();
618 634
619 mutex_lock(&tty->ldisc_mutex); 635 mutex_lock(&tty->ldisc_mutex);
636 lock_kernel();
620 if (test_bit(TTY_HUPPED, &tty->flags)) { 637 if (test_bit(TTY_HUPPED, &tty->flags)) {
621 /* We were raced by the hangup method. It will have stomped 638 /* We were raced by the hangup method. It will have stomped
622 the ldisc data and closed the ldisc down */ 639 the ldisc data and closed the ldisc down */
623 clear_bit(TTY_LDISC_CHANGING, &tty->flags); 640 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
624 mutex_unlock(&tty->ldisc_mutex); 641 mutex_unlock(&tty->ldisc_mutex);
625 tty_ldisc_put(new_ldisc); 642 tty_ldisc_put(new_ldisc);
643 unlock_kernel();
626 return -EIO; 644 return -EIO;
627 } 645 }
628 646
@@ -664,6 +682,7 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
664 if (o_work) 682 if (o_work)
665 schedule_delayed_work(&o_tty->buf.work, 1); 683 schedule_delayed_work(&o_tty->buf.work, 1);
666 mutex_unlock(&tty->ldisc_mutex); 684 mutex_unlock(&tty->ldisc_mutex);
685 unlock_kernel();
667 return retval; 686 return retval;
668} 687}
669 688
diff --git a/drivers/char/tty_port.c b/drivers/char/tty_port.c
index c63f3d33914a..be492dd66437 100644
--- a/drivers/char/tty_port.c
+++ b/drivers/char/tty_port.c
@@ -25,19 +25,21 @@ void tty_port_init(struct tty_port *port)
25 init_waitqueue_head(&port->close_wait); 25 init_waitqueue_head(&port->close_wait);
26 init_waitqueue_head(&port->delta_msr_wait); 26 init_waitqueue_head(&port->delta_msr_wait);
27 mutex_init(&port->mutex); 27 mutex_init(&port->mutex);
28 mutex_init(&port->buf_mutex);
28 spin_lock_init(&port->lock); 29 spin_lock_init(&port->lock);
29 port->close_delay = (50 * HZ) / 100; 30 port->close_delay = (50 * HZ) / 100;
30 port->closing_wait = (3000 * HZ) / 100; 31 port->closing_wait = (3000 * HZ) / 100;
32 kref_init(&port->kref);
31} 33}
32EXPORT_SYMBOL(tty_port_init); 34EXPORT_SYMBOL(tty_port_init);
33 35
34int tty_port_alloc_xmit_buf(struct tty_port *port) 36int tty_port_alloc_xmit_buf(struct tty_port *port)
35{ 37{
36 /* We may sleep in get_zeroed_page() */ 38 /* We may sleep in get_zeroed_page() */
37 mutex_lock(&port->mutex); 39 mutex_lock(&port->buf_mutex);
38 if (port->xmit_buf == NULL) 40 if (port->xmit_buf == NULL)
39 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL); 41 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
40 mutex_unlock(&port->mutex); 42 mutex_unlock(&port->buf_mutex);
41 if (port->xmit_buf == NULL) 43 if (port->xmit_buf == NULL)
42 return -ENOMEM; 44 return -ENOMEM;
43 return 0; 45 return 0;
@@ -46,15 +48,32 @@ EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
46 48
47void tty_port_free_xmit_buf(struct tty_port *port) 49void tty_port_free_xmit_buf(struct tty_port *port)
48{ 50{
49 mutex_lock(&port->mutex); 51 mutex_lock(&port->buf_mutex);
50 if (port->xmit_buf != NULL) { 52 if (port->xmit_buf != NULL) {
51 free_page((unsigned long)port->xmit_buf); 53 free_page((unsigned long)port->xmit_buf);
52 port->xmit_buf = NULL; 54 port->xmit_buf = NULL;
53 } 55 }
54 mutex_unlock(&port->mutex); 56 mutex_unlock(&port->buf_mutex);
55} 57}
56EXPORT_SYMBOL(tty_port_free_xmit_buf); 58EXPORT_SYMBOL(tty_port_free_xmit_buf);
57 59
60static void tty_port_destructor(struct kref *kref)
61{
62 struct tty_port *port = container_of(kref, struct tty_port, kref);
63 if (port->xmit_buf)
64 free_page((unsigned long)port->xmit_buf);
65 if (port->ops->destruct)
66 port->ops->destruct(port);
67 else
68 kfree(port);
69}
70
71void tty_port_put(struct tty_port *port)
72{
73 if (port)
74 kref_put(&port->kref, tty_port_destructor);
75}
76EXPORT_SYMBOL(tty_port_put);
58 77
59/** 78/**
60 * tty_port_tty_get - get a tty reference 79 * tty_port_tty_get - get a tty reference
@@ -99,10 +118,11 @@ EXPORT_SYMBOL(tty_port_tty_set);
99 118
100static void tty_port_shutdown(struct tty_port *port) 119static void tty_port_shutdown(struct tty_port *port)
101{ 120{
121 mutex_lock(&port->mutex);
102 if (port->ops->shutdown && 122 if (port->ops->shutdown &&
103 test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) 123 test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags))
104 port->ops->shutdown(port); 124 port->ops->shutdown(port);
105 125 mutex_unlock(&port->mutex);
106} 126}
107 127
108/** 128/**
@@ -120,8 +140,10 @@ void tty_port_hangup(struct tty_port *port)
120 spin_lock_irqsave(&port->lock, flags); 140 spin_lock_irqsave(&port->lock, flags);
121 port->count = 0; 141 port->count = 0;
122 port->flags &= ~ASYNC_NORMAL_ACTIVE; 142 port->flags &= ~ASYNC_NORMAL_ACTIVE;
123 if (port->tty) 143 if (port->tty) {
144 set_bit(TTY_IO_ERROR, &port->tty->flags);
124 tty_kref_put(port->tty); 145 tty_kref_put(port->tty);
146 }
125 port->tty = NULL; 147 port->tty = NULL;
126 spin_unlock_irqrestore(&port->lock, flags); 148 spin_unlock_irqrestore(&port->lock, flags);
127 wake_up_interruptible(&port->open_wait); 149 wake_up_interruptible(&port->open_wait);
@@ -198,7 +220,7 @@ EXPORT_SYMBOL(tty_port_lower_dtr_rts);
198 * management of these lines. Note that the dtr/rts raise is done each 220 * management of these lines. Note that the dtr/rts raise is done each
199 * iteration as a hangup may have previously dropped them while we wait. 221 * iteration as a hangup may have previously dropped them while we wait.
200 */ 222 */
201 223
202int tty_port_block_til_ready(struct tty_port *port, 224int tty_port_block_til_ready(struct tty_port *port,
203 struct tty_struct *tty, struct file *filp) 225 struct tty_struct *tty, struct file *filp)
204{ 226{
@@ -253,7 +275,8 @@ int tty_port_block_til_ready(struct tty_port *port,
253 tty_port_raise_dtr_rts(port); 275 tty_port_raise_dtr_rts(port);
254 276
255 prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE); 277 prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
256 /* Check for a hangup or uninitialised port. Return accordingly */ 278 /* Check for a hangup or uninitialised port.
279 Return accordingly */
257 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) { 280 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
258 if (port->flags & ASYNC_HUP_NOTIFY) 281 if (port->flags & ASYNC_HUP_NOTIFY)
259 retval = -EAGAIN; 282 retval = -EAGAIN;
@@ -285,11 +308,11 @@ int tty_port_block_til_ready(struct tty_port *port,
285 port->flags |= ASYNC_NORMAL_ACTIVE; 308 port->flags |= ASYNC_NORMAL_ACTIVE;
286 spin_unlock_irqrestore(&port->lock, flags); 309 spin_unlock_irqrestore(&port->lock, flags);
287 return retval; 310 return retval;
288
289} 311}
290EXPORT_SYMBOL(tty_port_block_til_ready); 312EXPORT_SYMBOL(tty_port_block_til_ready);
291 313
292int tty_port_close_start(struct tty_port *port, struct tty_struct *tty, struct file *filp) 314int tty_port_close_start(struct tty_port *port,
315 struct tty_struct *tty, struct file *filp)
293{ 316{
294 unsigned long flags; 317 unsigned long flags;
295 318
@@ -299,7 +322,7 @@ int tty_port_close_start(struct tty_port *port, struct tty_struct *tty, struct f
299 return 0; 322 return 0;
300 } 323 }
301 324
302 if( tty->count == 1 && port->count != 1) { 325 if (tty->count == 1 && port->count != 1) {
303 printk(KERN_WARNING 326 printk(KERN_WARNING
304 "tty_port_close_start: tty->count = 1 port count = %d.\n", 327 "tty_port_close_start: tty->count = 1 port count = %d.\n",
305 port->count); 328 port->count);
@@ -331,12 +354,20 @@ int tty_port_close_start(struct tty_port *port, struct tty_struct *tty, struct f
331 long timeout; 354 long timeout;
332 355
333 if (bps > 1200) 356 if (bps > 1200)
334 timeout = max_t(long, (HZ * 10 * port->drain_delay) / bps, 357 timeout = max_t(long,
335 HZ / 10); 358 (HZ * 10 * port->drain_delay) / bps, HZ / 10);
336 else 359 else
337 timeout = 2 * HZ; 360 timeout = 2 * HZ;
338 schedule_timeout_interruptible(timeout); 361 schedule_timeout_interruptible(timeout);
339 } 362 }
363 /* Flush the ldisc buffering */
364 tty_ldisc_flush(tty);
365
366 /* Drop DTR/RTS if HUPCL is set. This causes any attached modem to
367 hang up the line */
368 if (tty->termios->c_cflag & HUPCL)
369 tty_port_lower_dtr_rts(port);
370
340 /* Don't call port->drop for the last reference. Callers will want 371 /* Don't call port->drop for the last reference. Callers will want
341 to drop the last active reference in ->shutdown() or the tty 372 to drop the last active reference in ->shutdown() or the tty
342 shutdown path */ 373 shutdown path */
@@ -348,11 +379,6 @@ void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
348{ 379{
349 unsigned long flags; 380 unsigned long flags;
350 381
351 tty_ldisc_flush(tty);
352
353 if (tty->termios->c_cflag & HUPCL)
354 tty_port_lower_dtr_rts(port);
355
356 spin_lock_irqsave(&port->lock, flags); 382 spin_lock_irqsave(&port->lock, flags);
357 tty->closing = 0; 383 tty->closing = 0;
358 384
@@ -377,7 +403,42 @@ void tty_port_close(struct tty_port *port, struct tty_struct *tty,
377 if (tty_port_close_start(port, tty, filp) == 0) 403 if (tty_port_close_start(port, tty, filp) == 0)
378 return; 404 return;
379 tty_port_shutdown(port); 405 tty_port_shutdown(port);
406 set_bit(TTY_IO_ERROR, &tty->flags);
380 tty_port_close_end(port, tty); 407 tty_port_close_end(port, tty);
381 tty_port_tty_set(port, NULL); 408 tty_port_tty_set(port, NULL);
382} 409}
383EXPORT_SYMBOL(tty_port_close); 410EXPORT_SYMBOL(tty_port_close);
411
412int tty_port_open(struct tty_port *port, struct tty_struct *tty,
413 struct file *filp)
414{
415 spin_lock_irq(&port->lock);
416 if (!tty_hung_up_p(filp))
417 ++port->count;
418 spin_unlock_irq(&port->lock);
419 tty_port_tty_set(port, tty);
420
421 /*
422 * Do the device-specific open only if the hardware isn't
423 * already initialized. Serialize open and shutdown using the
424 * port mutex.
425 */
426
427 mutex_lock(&port->mutex);
428
429 if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) {
430 clear_bit(TTY_IO_ERROR, &tty->flags);
431 if (port->ops->activate) {
432 int retval = port->ops->activate(port, tty);
433 if (retval) {
434 mutex_unlock(&port->mutex);
435 return retval;
436 }
437 }
438 set_bit(ASYNCB_INITIALIZED, &port->flags);
439 }
440 mutex_unlock(&port->mutex);
441 return tty_port_block_til_ready(port, tty, filp);
442}
443
444EXPORT_SYMBOL(tty_port_open);