2 * drivers/char/serial_txx927.c
3 * driver for TX[34]927 SIO
5 * Copyright 2001 MontaVista Software Inc.
6 * Author: MontaVista Software, Inc.
9 * Based on drivers/char/serial.c
11 * Copyright (C) 2000-2001 Toshiba Corporation
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
21 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * You should have received a copy of the GNU General Public License along
30 * with this program; if not, write to the Free Software Foundation, Inc.,
31 * 675 Mass Ave, Cambridge, MA 02139, USA.
34 #define SERIAL_DO_RESTART
36 /* Set of debugging defines */
38 #undef SERIAL_DEBUG_INTR
39 #undef SERIAL_DEBUG_OPEN
40 #undef SERIAL_DEBUG_FLOW
41 #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
42 #undef SERIAL_DEBUG_PCI
43 #undef SERIAL_DEBUG_AUTOCONF
46 #undef CONFIG_TXX927_SERIAL_CONSOLE
49 #define CONFIG_SERIAL_RSA
51 #define RS_STROBE_TIME (10*HZ)
52 #define RS_ISR_PASS_LIMIT 256
55 * End of serial driver configuration section.
59 #include <linux/modversions.h>
61 #include <linux/module.h>
63 #include <linux/config.h>
64 #include <linux/module.h>
65 #include <linux/errno.h>
66 #include <linux/signal.h>
67 #include <linux/sched.h>
68 #include <linux/timer.h>
69 #include <linux/interrupt.h>
70 #include <linux/tty.h>
71 #include <linux/tty_flip.h>
72 #include <linux/serial.h>
73 #include <linux/circ_buf.h>
74 #include <linux/serial_reg.h>
75 #include <linux/major.h>
76 #include <linux/string.h>
77 #include <linux/fcntl.h>
78 #include <linux/ptrace.h>
79 #include <linux/ioport.h>
81 #include <linux/slab.h>
82 #include <linux/init.h>
83 #include <linux/serialP.h>
84 #include <linux/delay.h>
85 #ifdef CONFIG_TXX927_SERIAL_CONSOLE
86 #include <linux/console.h>
88 #ifdef CONFIG_MAGIC_SYSRQ
89 #include <linux/sysrq.h>
92 #include <asm/system.h>
93 #include <asm/serial.h>
96 #include <asm/uaccess.h>
97 #include <asm/bitops.h>
98 #include <asm/jmr3927/txx927.h>
99 #include <asm/bootinfo.h>
100 #ifdef CONFIG_TOSHIBA_JMR3927
101 #include <asm/jmr3927/jmr3927.h>
104 #define _INLINE_ inline
106 #ifdef CONFIG_MAC_SERIAL
107 #define SERIAL_DEV_OFFSET 2
109 #define SERIAL_DEV_OFFSET 0
112 static char *serial_name
= "TXx927 Serial driver";
113 static char *serial_version
= "0.02";
115 static DECLARE_TASK_QUEUE(tq_serial
);
117 static struct tty_driver serial_driver
, callout_driver
;
118 static int serial_refcount
;
120 static struct timer_list serial_timer
;
122 extern unsigned long get_txx927_uart_baud(void);
124 /* serial subtype definitions */
125 #ifndef SERIAL_TYPE_NORMAL
126 #define SERIAL_TYPE_NORMAL 1
127 #define SERIAL_TYPE_CALLOUT 2
130 /* number of characters left in xmit buffer before we ask for more */
131 #define WAKEUP_CHARS 256
134 * IRQ_timeout - How long the timeout should be for each IRQ
135 * should be after the IRQ has been active.
138 static struct async_struct
*IRQ_ports
[NR_IRQS
];
139 static int IRQ_timeout
[NR_IRQS
];
140 #ifdef CONFIG_TXX927_SERIAL_CONSOLE
141 static struct console sercons
;
143 #if defined(CONFIG_TXX927_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
144 static unsigned long break_pressed
; /* break, really ... */
147 static void change_speed(struct async_struct
*info
, struct termios
*old
);
148 static void rs_wait_until_sent(struct tty_struct
*tty
, int timeout
);
151 #define PREPARE_FUNC(dev) (dev->prepare)
152 #define ACTIVATE_FUNC(dev) (dev->activate)
153 #define DEACTIVATE_FUNC(dev) (dev->deactivate)
156 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
159 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
160 #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
161 kdevname(tty->device), (info->flags), serial_refcount,info->count,tty->count,s)
166 #define SERIAL_DRIVER_NAME "TXx927SIO"
169 /* "ttyS","cua" is used for standard serial driver */
170 #define TXX927_TTY_NAME "ttySC"
171 #define TXX927_TTY_MINOR_START (64 + 16) /* ttySC0(80), ttySC1(81) */
172 #define TXX927_CU_NAME "cuac"
173 #define TXX927_SERIAL_BH TXX927SERIAL_BH
175 /* acts like standard serial driver */
176 #define TXX927_TTY_NAME "ttyS"
177 #define TXX927_TTY_MINOR_START 64
178 #define TXX927_CU_NAME "cua"
179 #define TXX927_SERIAL_BH SERIAL_BH
181 #define TXX927_TTY_MAJOR TTY_MAJOR
182 #define TXX927_TTYAUX_MAJOR TTYAUX_MAJOR
184 #define ASYNC_HAVE_CTS_LINE ASYNC_BOOT_AUTOCONF /* reuse */
186 static struct serial_state rs_table
[RS_TABLE_SIZE
] = {
187 SERIAL_PORT_DFNS
/* Defined in serial.h */
190 #define NR_PORTS (sizeof(rs_table)/sizeof(struct serial_state))
192 static struct tty_struct
*serial_table
[NR_PORTS
];
193 static struct termios
*serial_termios
[NR_PORTS
];
194 static struct termios
*serial_termios_locked
[NR_PORTS
];
197 #define MIN(a,b) ((a) < (b) ? (a) : (b))
201 * tmp_buf is used as a temporary buffer by serial_write. We need to
202 * lock it in case the copy_from_user blocks while swapping in a page,
203 * and some other program tries to do a serial write at the same time.
204 * Since the lock will only come under contention when the system is
205 * swapping and available memory is low, it makes sense to share one
206 * buffer across all the serial ports, since it significantly saves
207 * memory if large numbers of serial ports are open.
209 static unsigned char *tmp_buf
;
211 static DECLARE_MUTEX(tmp_buf_sem
);
213 static struct semaphore tmp_buf_sem
= MUTEX
;
216 static inline int serial_paranoia_check(struct async_struct
*info
,
217 kdev_t device
, const char *routine
)
219 #ifdef SERIAL_PARANOIA_CHECK
220 static const char *badmagic
=
221 "Warning: bad magic number for serial struct (%s) in %s\n";
222 static const char *badinfo
=
223 "Warning: null async_struct for (%s) in %s\n";
226 printk(badinfo
, kdevname(device
), routine
);
229 if (info
->magic
!= SERIAL_MAGIC
) {
230 printk(badmagic
, kdevname(device
), routine
);
237 static inline struct txx927_sio_reg
*sio_reg(struct async_struct
*info
)
239 return (struct txx927_sio_reg
*)info
->port
;
243 * Wait for transmitter & holding register to empty
245 static inline void wait_for_xmitr(struct async_struct
*info
)
247 unsigned int tmout
= 1000000;
250 if (--tmout
== 0) break;
251 } while (!(sio_reg(info
)->cisr
& TXx927_SICISR_TXALS
));
255 * ------------------------------------------------------------
256 * rs_stop() and rs_start()
258 * This routines are called before setting or resetting tty->stopped.
259 * They enable or disable transmitter interrupts, as necessary.
260 * ------------------------------------------------------------
262 static void rs_stop(struct tty_struct
*tty
)
264 struct async_struct
*info
= (struct async_struct
*)tty
->driver_data
;
267 if (serial_paranoia_check(info
, tty
->device
, "rs_stop"))
270 save_flags(flags
); cli();
271 if (info
->IER
& UART_IER_THRI
) {
272 info
->IER
&= ~UART_IER_THRI
;
273 sio_reg(info
)->dicr
&= ~TXx927_SIDICR_TIE
;
275 restore_flags(flags
);
278 static void rs_start(struct tty_struct
*tty
)
280 struct async_struct
*info
= (struct async_struct
*)tty
->driver_data
;
283 if (serial_paranoia_check(info
, tty
->device
, "rs_start"))
286 save_flags(flags
); cli();
287 if (info
->xmit
.head
!= info
->xmit
.tail
289 && !(info
->IER
& UART_IER_THRI
)) {
290 info
->IER
|= UART_IER_THRI
;
291 sio_reg(info
)->dicr
|= TXx927_SIDICR_TIE
;
293 restore_flags(flags
);
297 * ----------------------------------------------------------------------
299 * Here starts the interrupt handling routines. All of the following
300 * subroutines are declared as inline and are folded into
301 * rs_interrupt(). They were separated out for readability's sake.
303 * Note: rs_interrupt() is a "fast" interrupt, which means that it
304 * runs with interrupts turned off. People who may want to modify
305 * rs_interrupt() should try to keep the interrupt handler as fast as
306 * possible. After you are done making modifications, it is not a bad
309 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
311 * and look at the resulting assemble code in serial.s.
313 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
314 * -----------------------------------------------------------------------
318 * This routine is used by the interrupt handler to schedule
319 * processing in the software interrupt portion of the driver.
321 static _INLINE_
void rs_sched_event(struct async_struct
*info
,
324 info
->event
|= 1 << event
;
325 queue_task(&info
->tqueue
, &tq_serial
);
326 mark_bh(TXX927_SERIAL_BH
);
329 static _INLINE_
void receive_chars(struct async_struct
*info
,
332 struct tty_struct
*tty
= info
->tty
;
335 struct async_icount
*icount
;
337 icount
= &info
->state
->icount
;
339 ch
= sio_reg(info
)->rfifo
;
340 if (tty
->flip
.count
>= TTY_FLIPBUF_SIZE
)
342 *tty
->flip
.char_buf_ptr
= ch
;
345 #ifdef SERIAL_DEBUG_INTR
346 printk("DR%02x:%02x...", ch
, *status
);
348 *tty
->flip
.flag_buf_ptr
= 0;
349 if (*status
& (TXx927_SIDISR_UBRK
| TXx927_SIDISR_UPER
|
350 TXx927_SIDISR_UFER
| TXx927_SIDISR_UOER
)) {
352 * For statistics only
354 if (*status
& TXx927_SIDISR_UBRK
) {
355 *status
&= ~(TXx927_SIDISR_UFER
| TXx927_SIDISR_UPER
);
357 } else if (*status
& TXx927_SIDISR_UPER
)
359 else if (*status
& TXx927_SIDISR_UFER
)
361 if (*status
& TXx927_SIDISR_UOER
)
365 * Now check to see if character should be
366 * ignored, and mask off conditions which
369 if (*status
& info
->ignore_status_mask
) {
374 *status
&= info
->read_status_mask
;
376 if (*status
& (TXx927_SIDISR_UBRK
)) {
377 #ifdef SERIAL_DEBUG_INTR
378 printk("handling break....");
380 *tty
->flip
.flag_buf_ptr
= TTY_BREAK
;
381 if (info
->flags
& ASYNC_SAK
)
383 } else if (*status
& TXx927_SIDISR_UPER
)
384 *tty
->flip
.flag_buf_ptr
= TTY_PARITY
;
385 else if (*status
& TXx927_SIDISR_UFER
)
386 *tty
->flip
.flag_buf_ptr
= TTY_FRAME
;
387 if (*status
& TXx927_SIDISR_UOER
) {
389 * Overrun is special, since it's
390 * reported immediately, and doesn't
391 * affect the current character
393 if (tty
->flip
.count
< TTY_FLIPBUF_SIZE
) {
395 tty
->flip
.flag_buf_ptr
++;
396 tty
->flip
.char_buf_ptr
++;
397 *tty
->flip
.flag_buf_ptr
= TTY_OVERRUN
;
401 tty
->flip
.flag_buf_ptr
++;
402 tty
->flip
.char_buf_ptr
++;
405 *status
= sio_reg(info
)->disr
;
406 } while (!(*status
& TXx927_SIDISR_UVALID
));
408 tty_flip_buffer_push(tty
);
411 static _INLINE_
void transmit_chars(struct async_struct
*info
, int *intr_done
)
415 wait_for_xmitr(info
);
418 sio_reg(info
)->tfifo
= info
->x_char
;
419 info
->state
->icount
.tx
++;
426 if (info
->xmit
.head
== info
->xmit
.tail
427 || info
->tty
->stopped
428 || info
->tty
->hw_stopped
) {
429 sio_reg(info
)->dicr
&= ~TXx927_SIDICR_TIE
;
433 count
= info
->xmit_fifo_size
;
435 sio_reg(info
)->tfifo
= info
->xmit
.buf
[info
->xmit
.tail
++];
436 info
->xmit
.tail
= info
->xmit
.tail
& (SERIAL_XMIT_SIZE
-1);
437 info
->state
->icount
.tx
++;
438 if (info
->xmit
.head
== info
->xmit
.tail
)
440 } while (--count
> 0);
442 if (CIRC_CNT(info
->xmit
.head
,
444 SERIAL_XMIT_SIZE
) < WAKEUP_CHARS
)
445 rs_sched_event(info
, RS_EVENT_WRITE_WAKEUP
);
447 #ifdef SERIAL_DEBUG_INTR
453 if (info
->xmit
.head
== info
->xmit
.tail
) {
454 sio_reg(info
)->dicr
&= ~TXx927_SIDICR_TIE
;
458 static _INLINE_
void check_modem_status(struct async_struct
*info
)
460 /* RTS/CTS are controled by HW. (if possible) */
464 * This is the serial driver's interrupt routine for a single port
466 static void rs_interrupt_single(int irq
, void *dev_id
, struct pt_regs
* regs
)
469 int pass_counter
= 0;
470 struct async_struct
* info
;
472 #ifdef SERIAL_DEBUG_INTR
473 printk("rs_interrupt_single(%d)...", irq
);
476 info
= IRQ_ports
[irq
];
477 if (!info
|| !info
->tty
)
481 status
= sio_reg(info
)->disr
;
482 #ifdef SERIAL_DEBUG_INTR
483 printk("status = %x...", status
);
485 if (!(sio_reg(info
)->dicr
& TXx927_SIDICR_TIE
))
486 status
&= ~TXx927_SIDISR_TDIS
;
487 if (!(status
& (TXx927_SIDISR_TDIS
| TXx927_SIDISR_RDIS
| TXx927_SIDISR_TOUT
)))
490 if (status
& TXx927_SIDISR_RDIS
)
491 receive_chars(info
, &status
);
492 check_modem_status(info
);
493 if (status
& TXx927_SIDISR_TDIS
)
494 transmit_chars(info
, 0);
495 /* Clear TX/RX Int. Status */
496 sio_reg(info
)->disr
&= ~(TXx927_SIDISR_TDIS
| TXx927_SIDISR_RDIS
| TXx927_SIDISR_TOUT
);
498 if (pass_counter
++ > RS_ISR_PASS_LIMIT
) {
499 #ifdef SERIAL_DEBUG_INTR
500 printk("rs_single loop break.\n");
505 info
->last_active
= jiffies
;
506 #ifdef SERIAL_DEBUG_INTR
512 * -------------------------------------------------------------------
513 * Here ends the serial interrupt routines.
514 * -------------------------------------------------------------------
518 * This routine is used to handle the "bottom half" processing for the
519 * serial driver, known also the "software interrupt" processing.
520 * This processing is done at the kernel interrupt level, after the
521 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
522 * is where time-consuming activities which can not be done in the
523 * interrupt driver proper are done; the interrupt driver schedules
524 * them using rs_sched_event(), and they get done here.
526 static void do_serial_bh(void)
528 run_task_queue(&tq_serial
);
531 static void do_softint(void *private_
)
533 struct async_struct
*info
= (struct async_struct
*) private_
;
534 struct tty_struct
*tty
;
540 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP
, &info
->event
)) {
541 if ((tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) &&
542 tty
->ldisc
.write_wakeup
)
543 (tty
->ldisc
.write_wakeup
)(tty
);
544 wake_up_interruptible(&tty
->write_wait
);
545 #ifdef SERIAL_HAVE_POLL_WAIT
546 wake_up_interruptible(&tty
->poll_wait
);
552 * This subroutine is called when the RS_TIMER goes off. It is used
553 * by the serial driver to handle ports that do not have an interrupt
554 * (irq=0). This doesn't work very well for 16450's, but gives barely
555 * passable results for a 16550A. (Although at the expense of much
558 static void rs_timer(unsigned long dummy
)
560 static unsigned long last_strobe
;
561 struct async_struct
*info
;
565 if ((jiffies
- last_strobe
) >= RS_STROBE_TIME
) {
566 for (i
=0; i
< NR_IRQS
; i
++) {
570 save_flags(flags
); cli();
571 rs_interrupt_single(i
, NULL
, NULL
);
572 restore_flags(flags
);
575 last_strobe
= jiffies
;
576 mod_timer(&serial_timer
, jiffies
+ RS_STROBE_TIME
);
580 save_flags(flags
); cli();
581 rs_interrupt_single(0, NULL
, NULL
);
582 restore_flags(flags
);
584 mod_timer(&serial_timer
, jiffies
+ IRQ_timeout
[0]);
590 * ---------------------------------------------------------------
591 * Low level utility subroutines for the serial driver: routines to
592 * figure out the appropriate timeout for an interrupt chain, routines
593 * to initialize and startup a serial port, and routines to shutdown a
594 * serial port. Useful stuff like that.
595 * ---------------------------------------------------------------
599 * This routine figures out the correct timeout for a particular IRQ.
600 * It uses the smallest timeout of all of the serial ports in a
601 * particular interrupt chain. Now only used for IRQ 0....
603 static void figure_IRQ_timeout(int irq
)
605 struct async_struct
*info
;
606 int timeout
= 60*HZ
; /* 60 seconds === a long time :-) */
608 info
= IRQ_ports
[irq
];
610 IRQ_timeout
[irq
] = 60*HZ
;
614 if (info
->timeout
< timeout
)
615 timeout
= info
->timeout
;
616 info
= info
->next_port
;
619 timeout
= timeout
/ 2;
620 IRQ_timeout
[irq
] = timeout
? timeout
: 1;
623 static int startup(struct async_struct
* info
)
627 struct serial_state
*state
= info
->state
;
630 page
= get_zeroed_page(GFP_KERNEL
);
634 save_flags(flags
); cli();
636 if (info
->flags
& ASYNC_INITIALIZED
) {
643 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
650 info
->xmit
.buf
= (unsigned char *) page
;
652 #ifdef SERIAL_DEBUG_OPEN
653 printk("starting up ttys%d (irq %d)...", info
->line
, state
->irq
);
657 * Clear the FIFO buffers and disable them
658 * (they will be reenabled in change_speed())
660 sio_reg(info
)->fcr
|= TXx927_SIFCR_TFRST
| TXx927_SIFCR_RFRST
|
663 sio_reg(info
)->fcr
&= ~(TXx927_SIFCR_TFRST
| TXx927_SIFCR_RFRST
|
667 * Allocate the IRQ if necessary
669 if (state
->irq
&& (!IRQ_ports
[state
->irq
] ||
670 !IRQ_ports
[state
->irq
]->next_port
)) {
671 if (IRQ_ports
[state
->irq
]) {
676 retval
= request_irq(state
->irq
, rs_interrupt_single
,
678 "txx927serial", NULL
);
680 if (capable(CAP_SYS_ADMIN
)) {
682 set_bit(TTY_IO_ERROR
,
691 * Insert serial port into IRQ chain.
694 info
->next_port
= IRQ_ports
[state
->irq
];
696 info
->next_port
->prev_port
= info
;
697 IRQ_ports
[state
->irq
] = info
;
698 figure_IRQ_timeout(state
->irq
);
701 * Clear the interrupt registers.
703 sio_reg(info
)->disr
= 0;
706 * Now, initialize the UART
708 /* HW RTS/CTS control */
709 if (state
->flags
& ASYNC_HAVE_CTS_LINE
)
710 sio_reg(info
)->flcr
= TXx927_SIFLCR_RCS
| TXx927_SIFLCR_TES
|
711 TXx927_SIFLCR_RTSTL_MAX
/* 15 */;
713 sio_reg(info
)->flcr
&= ~(TXx927_SIFLCR_RSDE
| TXx927_SIFLCR_TSDE
);
716 * Finally, enable interrupts
718 sio_reg(info
)->dicr
= TXx927_SIDICR_RIE
;
721 clear_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
722 info
->xmit
.head
= info
->xmit
.tail
= 0;
725 * Set up the tty->alt_speed kludge
728 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_HI
)
729 info
->tty
->alt_speed
= 57600;
730 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_VHI
)
731 info
->tty
->alt_speed
= 115200;
732 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_SHI
)
733 info
->tty
->alt_speed
= 230400;
734 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_WARP
)
735 info
->tty
->alt_speed
= 460800;
739 * and set the speed of the serial port
741 change_speed(info
, 0);
743 info
->flags
|= ASYNC_INITIALIZED
;
744 restore_flags(flags
);
748 restore_flags(flags
);
753 * This routine will shutdown a serial port; interrupts are disabled, and
754 * DTR is dropped if the hangup on close termio flag is on.
756 static void shutdown(struct async_struct
* info
)
759 struct serial_state
*state
;
762 if (!(info
->flags
& ASYNC_INITIALIZED
))
767 #ifdef SERIAL_DEBUG_OPEN
768 printk("Shutting down serial port %d (irq %d)....", info
->line
,
772 save_flags(flags
); cli(); /* Disable interrupts */
775 * First unlink the serial port from the IRQ chain...
778 info
->next_port
->prev_port
= info
->prev_port
;
780 info
->prev_port
->next_port
= info
->next_port
;
782 IRQ_ports
[state
->irq
] = info
->next_port
;
783 figure_IRQ_timeout(state
->irq
);
786 * Free the IRQ, if necessary
788 if (state
->irq
&& (!IRQ_ports
[state
->irq
] ||
789 !IRQ_ports
[state
->irq
]->next_port
)) {
790 if (IRQ_ports
[state
->irq
]) {
791 free_irq(state
->irq
, NULL
);
792 retval
= request_irq(state
->irq
, rs_interrupt_single
,
793 SA_INTERRUPT
, "txx927serial", NULL
);
796 printk(KERN_WARNING
"txx927serial shutdown: request_irq: error %d"
797 " Couldn't reacquire IRQ.\n", retval
);
799 free_irq(state
->irq
, NULL
);
802 if (info
->xmit
.buf
) {
803 free_page((unsigned long) info
->xmit
.buf
);
807 sio_reg(info
)->dicr
= 0; /* disable all intrs */
809 /* disable break condition */
810 sio_reg(info
)->flcr
&= ~TXx927_SIFLCR_TBRK
;
812 if (!info
->tty
|| (info
->tty
->termios
->c_cflag
& HUPCL
)) {
814 sio_reg(info
)->flcr
|= TXx927_SIFLCR_RTSSC
|TXx927_SIFLCR_RSDE
;
815 /* TXx927-SIO can not control DTR... */
819 sio_reg(info
)->fcr
|= TXx927_SIFCR_TFRST
| TXx927_SIFCR_RFRST
|
822 sio_reg(info
)->fcr
&= ~(TXx927_SIFCR_TFRST
| TXx927_SIFCR_RFRST
|
825 /* DON'T disable Rx/Tx here, ie. DON'T set either
826 * TXx927_SIFLCR_RSDE or TXx927_SIFLCR_TSDE in flcr
830 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
832 info
->flags
&= ~ASYNC_INITIALIZED
;
833 restore_flags(flags
);
837 * This routine is called to set the UART divisor registers to match
838 * the specified baud rate for a serial port.
840 static void change_speed(struct async_struct
*info
,
841 struct termios
*old_termios
)
843 int quot
= 0, baud_base
, baud
;
844 unsigned cflag
, cval
;
848 if (!info
->tty
|| !info
->tty
->termios
)
850 cflag
= info
->tty
->termios
->c_cflag
;
854 cval
= sio_reg(info
)->lcr
;
855 /* byte size and parity */
856 cval
&= ~TXx927_SILCR_UMODE_MASK
;
857 switch (cflag
& CSIZE
) {
859 cval
|= TXx927_SILCR_UMODE_7BIT
;
862 case CS5
: /* not supported */
863 case CS6
: /* not supported */
866 cval
|= TXx927_SILCR_UMODE_8BIT
;
870 cval
&= ~TXx927_SILCR_USBL_MASK
;
871 if (cflag
& CSTOPB
) {
872 cval
|= TXx927_SILCR_USBL_2BIT
;
875 cval
|= TXx927_SILCR_USBL_1BIT
;
878 cval
&= ~(TXx927_SILCR_UPEN
|TXx927_SILCR_UEPS
);
879 if (cflag
& PARENB
) {
880 cval
|= TXx927_SILCR_UPEN
;
883 if (!(cflag
& PARODD
))
884 cval
|= TXx927_SILCR_UEPS
;
886 /* Determine divisor based on baud rate */
887 baud
= tty_get_baud_rate(info
->tty
);
889 baud
= 9600; /* B0 transition handled in rs_set_termios */
890 baud_base
= info
->state
->baud_base
;
891 quot
= (baud_base
+ baud
/ 2) / baud
;
892 /* If the quotient is zero refuse the change */
893 if (!quot
&& old_termios
) {
894 info
->tty
->termios
->c_cflag
&= ~CBAUD
;
895 info
->tty
->termios
->c_cflag
|= (old_termios
->c_cflag
& CBAUD
);
896 baud
= tty_get_baud_rate(info
->tty
);
899 quot
= (baud_base
+ baud
/ 2) / baud
;
901 /* As a last resort, if the quotient is zero, default to 9600 bps */
903 quot
= (baud_base
+ 9600 / 2) / 9600;
905 info
->timeout
= ((info
->xmit_fifo_size
*HZ
*bits
*quot
) / baud_base
);
906 info
->timeout
+= HZ
/50; /* Add .02 seconds of slop */
908 /* CTS flow control flag */
909 if (cflag
& CRTSCTS
) {
910 info
->flags
|= ASYNC_CTS_FLOW
;
911 if (info
->state
->flags
& ASYNC_HAVE_CTS_LINE
)
912 sio_reg(info
)->flcr
= TXx927_SIFLCR_RCS
| TXx927_SIFLCR_TES
|
913 TXx927_SIFLCR_RTSTL_MAX
/* 15 */;
915 info
->flags
&= ~ASYNC_CTS_FLOW
;
916 sio_reg(info
)->flcr
&= ~(TXx927_SIFLCR_RCS
| TXx927_SIFLCR_TES
| TXx927_SIFLCR_RSDE
| TXx927_SIFLCR_TSDE
);
920 * Set up parity check flag
922 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
924 info
->read_status_mask
= TXx927_SIDISR_UOER
|
925 TXx927_SIDISR_TDIS
| TXx927_SIDISR_RDIS
;
926 if (I_INPCK(info
->tty
))
927 info
->read_status_mask
|= TXx927_SIDISR_UFER
| TXx927_SIDISR_UPER
;
928 if (I_BRKINT(info
->tty
) || I_PARMRK(info
->tty
))
929 info
->read_status_mask
|= TXx927_SIDISR_UBRK
;
932 * Characters to ignore
934 info
->ignore_status_mask
= 0;
935 if (I_IGNPAR(info
->tty
))
936 info
->ignore_status_mask
|= TXx927_SIDISR_UPER
| TXx927_SIDISR_UFER
;
937 if (I_IGNBRK(info
->tty
)) {
938 info
->ignore_status_mask
|= TXx927_SIDISR_UBRK
;
940 * If we're ignore parity and break indicators, ignore
941 * overruns too. (For real raw support).
943 if (I_IGNPAR(info
->tty
))
944 info
->ignore_status_mask
|= TXx927_SIDISR_UOER
;
947 * !!! ignore all characters if CREAD is not set
949 if ((cflag
& CREAD
) == 0)
950 info
->ignore_status_mask
|= TXx927_SIDISR_RDIS
;
951 save_flags(flags
); cli();
952 sio_reg(info
)->lcr
= cval
| TXx927_SILCR_SCS_IMCLK_BG
;
953 sio_reg(info
)->bgr
= quot
| TXx927_SIBGR_BCLK_T0
;
954 restore_flags(flags
);
957 static void rs_put_char(struct tty_struct
*tty
, unsigned char ch
)
959 struct async_struct
*info
= (struct async_struct
*)tty
->driver_data
;
962 if (serial_paranoia_check(info
, tty
->device
, "rs_put_char"))
965 if (!tty
|| !info
->xmit
.buf
)
968 save_flags(flags
); cli();
969 if (CIRC_SPACE(info
->xmit
.head
,
971 SERIAL_XMIT_SIZE
) == 0) {
972 restore_flags(flags
);
976 info
->xmit
.buf
[info
->xmit
.head
++] = ch
;
977 info
->xmit
.head
&= SERIAL_XMIT_SIZE
-1;
978 restore_flags(flags
);
981 static void rs_flush_chars(struct tty_struct
*tty
)
983 struct async_struct
*info
= (struct async_struct
*)tty
->driver_data
;
986 if (serial_paranoia_check(info
, tty
->device
, "rs_flush_chars"))
989 if (info
->xmit
.head
== info
->xmit
.tail
995 save_flags(flags
); cli();
996 sio_reg(info
)->dicr
|= TXx927_SIDICR_TIE
;
997 restore_flags(flags
);
1000 static int rs_write(struct tty_struct
* tty
, int from_user
,
1001 const unsigned char *buf
, int count
)
1004 struct async_struct
*info
= (struct async_struct
*)tty
->driver_data
;
1005 unsigned long flags
;
1007 if (serial_paranoia_check(info
, tty
->device
, "rs_write"))
1010 if (!tty
|| !info
->xmit
.buf
|| !tmp_buf
)
1019 c
= CIRC_SPACE_TO_END(info
->xmit
.head
,
1028 c
-= copy_from_user(tmp_buf
, buf
, c
);
1035 c1
= CIRC_SPACE_TO_END(info
->xmit
.head
,
1041 memcpy(info
->xmit
.buf
+ info
->xmit
.head
, tmp_buf
, c
);
1042 info
->xmit
.head
= ((info
->xmit
.head
+ c
) &
1043 (SERIAL_XMIT_SIZE
-1));
1044 restore_flags(flags
);
1053 c
= CIRC_SPACE_TO_END(info
->xmit
.head
,
1062 memcpy(info
->xmit
.buf
+ info
->xmit
.head
, buf
, c
);
1063 info
->xmit
.head
= ((info
->xmit
.head
+ c
) &
1064 (SERIAL_XMIT_SIZE
-1));
1069 restore_flags(flags
);
1071 if (info
->xmit
.head
!= info
->xmit
.tail
1074 && !(info
->IER
& UART_IER_THRI
))
1075 sio_reg(info
)->dicr
|= TXx927_SIDICR_TIE
;
1080 static int rs_write_room(struct tty_struct
*tty
)
1082 struct async_struct
*info
= (struct async_struct
*)tty
->driver_data
;
1084 if (serial_paranoia_check(info
, tty
->device
, "rs_write_room"))
1087 return CIRC_SPACE(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
);
1090 static int rs_chars_in_buffer(struct tty_struct
*tty
)
1092 struct async_struct
*info
= (struct async_struct
*)tty
->driver_data
;
1094 if (serial_paranoia_check(info
, tty
->device
, "rs_chars_in_buffer"))
1097 return CIRC_CNT(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
);
1100 static void rs_flush_buffer(struct tty_struct
*tty
)
1102 struct async_struct
*info
= (struct async_struct
*)tty
->driver_data
;
1103 unsigned long flags
;
1105 if (serial_paranoia_check(info
, tty
->device
, "rs_flush_buffer"))
1107 save_flags(flags
); cli();
1108 info
->xmit
.head
= info
->xmit
.tail
= 0;
1109 restore_flags(flags
);
1110 wake_up_interruptible(&tty
->write_wait
);
1111 #ifdef SERIAL_HAVE_POLL_WAIT
1112 wake_up_interruptible(&tty
->poll_wait
);
1114 if ((tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) &&
1115 tty
->ldisc
.write_wakeup
)
1116 (tty
->ldisc
.write_wakeup
)(tty
);
1120 * This function is used to send a high-priority XON/XOFF character to
1123 static void rs_send_xchar(struct tty_struct
*tty
, char ch
)
1125 struct async_struct
*info
= (struct async_struct
*)tty
->driver_data
;
1128 if (serial_paranoia_check(info
, tty
->device
, "rs_send_char"))
1133 /* Make sure transmit interrupts are on */
1134 sio_reg(info
)->dicr
|= TXx927_SIDICR_TIE
;
1139 * ------------------------------------------------------------
1142 * This routine is called by the upper-layer tty layer to signal that
1143 * incoming characters should be throttled.
1144 * ------------------------------------------------------------
1146 static void rs_throttle(struct tty_struct
* tty
)
1148 struct async_struct
*info
= (struct async_struct
*)tty
->driver_data
;
1149 unsigned long flags
;
1150 #ifdef SERIAL_DEBUG_THROTTLE
1153 printk("throttle %s: %d....\n", tty_name(tty
, buf
),
1154 tty
->ldisc
.chars_in_buffer(tty
));
1157 if (serial_paranoia_check(info
, tty
->device
, "rs_throttle"))
1161 rs_send_xchar(tty
, STOP_CHAR(tty
));
1163 if (tty
->termios
->c_cflag
& CRTSCTS
) {
1164 save_flags(flags
); cli();
1166 sio_reg(info
)->flcr
|= TXx927_SIFLCR_RTSSC
|TXx927_SIFLCR_RSDE
;
1167 restore_flags(flags
);
1171 static void rs_unthrottle(struct tty_struct
* tty
)
1173 struct async_struct
*info
= (struct async_struct
*)tty
->driver_data
;
1174 unsigned long flags
;
1175 #ifdef SERIAL_DEBUG_THROTTLE
1178 printk("unthrottle %s: %d....\n", tty_name(tty
, buf
),
1179 tty
->ldisc
.chars_in_buffer(tty
));
1182 if (serial_paranoia_check(info
, tty
->device
, "rs_unthrottle"))
1189 rs_send_xchar(tty
, START_CHAR(tty
));
1191 if (tty
->termios
->c_cflag
& CRTSCTS
) {
1192 save_flags(flags
); cli();
1193 sio_reg(info
)->flcr
&= ~(TXx927_SIFLCR_RTSSC
|TXx927_SIFLCR_RSDE
);
1194 restore_flags(flags
);
1199 * ------------------------------------------------------------
1200 * rs_ioctl() and friends
1201 * ------------------------------------------------------------
1204 static int get_modem_info(struct async_struct
* info
, unsigned int *value
)
1206 unsigned int result
;
1207 unsigned long flags
;
1209 save_flags(flags
); cli();
1210 result
= ((sio_reg(info
)->flcr
& TXx927_SIFLCR_RTSSC
) ? 0 : TIOCM_RTS
)
1211 | ((sio_reg(info
)->cisr
& TXx927_SICISR_CTSS
) ? 0 : TIOCM_CTS
);
1212 restore_flags(flags
);
1213 return put_user(result
,value
);
1216 static int set_modem_info(struct async_struct
* info
, unsigned int cmd
,
1217 unsigned int *value
)
1221 unsigned long flags
;
1223 error
= get_user(arg
, value
);
1226 save_flags(flags
); cli();
1229 if (arg
& TIOCM_RTS
)
1230 sio_reg(info
)->flcr
&= ~(TXx927_SIFLCR_RTSSC
|TXx927_SIFLCR_RSDE
);
1233 if (arg
& TIOCM_RTS
)
1234 sio_reg(info
)->flcr
|= TXx927_SIFLCR_RTSSC
|TXx927_SIFLCR_RSDE
;
1237 sio_reg(info
)->flcr
=
1238 (sio_reg(info
)->flcr
& ~(TXx927_SIFLCR_RTSSC
|TXx927_SIFLCR_RSDE
)) |
1239 ((arg
& TIOCM_RTS
) ? 0 : TXx927_SIFLCR_RTSSC
|TXx927_SIFLCR_RSDE
);
1244 restore_flags(flags
);
1249 * rs_break() --- routine which turns the break handling on or off
1251 static void rs_break(struct tty_struct
*tty
, int break_state
)
1253 struct async_struct
* info
= (struct async_struct
*)tty
->driver_data
;
1254 unsigned long flags
;
1256 if (serial_paranoia_check(info
, tty
->device
, "rs_break"))
1261 save_flags(flags
); cli();
1262 if (break_state
== -1)
1263 sio_reg(info
)->flcr
|= TXx927_SIFLCR_TBRK
;
1265 sio_reg(info
)->flcr
&= ~TXx927_SIFLCR_TBRK
;
1266 restore_flags(flags
);
1269 static int rs_ioctl(struct tty_struct
*tty
, struct file
* file
,
1270 unsigned int cmd
, unsigned long arg
)
1272 struct async_struct
* info
= (struct async_struct
*)tty
->driver_data
;
1274 if (serial_paranoia_check(info
, tty
->device
, "rs_ioctl"))
1277 if ((cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCSSERIAL
) &&
1278 (cmd
!= TIOCSERCONFIG
) && (cmd
!= TIOCSERGSTRUCT
) &&
1279 (cmd
!= TIOCMIWAIT
) && (cmd
!= TIOCGICOUNT
)) {
1280 if (tty
->flags
& (1 << TTY_IO_ERROR
))
1286 return get_modem_info(info
, (unsigned int *) arg
);
1290 return set_modem_info(info
, cmd
, (unsigned int *) arg
);
1293 printk("TIOCGSERIAL\n");
1296 printk("TIOCSSERIAL\n");
1299 printk("TIOCSERCONFIG\n");
1302 case TIOCSERGETLSR
: /* Get line status register */
1303 printk("TIOCSERGETLSR\n");
1306 case TIOCSERGSTRUCT
:
1307 printk("TIOCSERGSTRUCT\n");
1311 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1312 * - mask passed in arg for lines of interest
1313 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1314 * Caller should use TIOCGICOUNT to see which one it was
1317 printk("TIOCMIWAIT\n");
1321 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1322 * Return: write counters to the user passed counter struct
1323 * NB: both 1->0 and 0->1 transitions are counted except for
1324 * RI where only 0->1 is counted.
1327 printk("TIOCGICOUNT\n");
1332 /* "setserial -W" is called in Debian boot */
1333 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1337 return -ENOIOCTLCMD
;
1342 static void rs_set_termios(struct tty_struct
*tty
, struct termios
*old_termios
)
1344 struct async_struct
*info
= (struct async_struct
*)tty
->driver_data
;
1345 unsigned long flags
;
1346 unsigned int cflag
= tty
->termios
->c_cflag
;
1348 if ( (cflag
== old_termios
->c_cflag
)
1349 && ( RELEVANT_IFLAG(tty
->termios
->c_iflag
)
1350 == RELEVANT_IFLAG(old_termios
->c_iflag
)))
1353 change_speed(info
, old_termios
);
1355 /* Handle transition to B0 status */
1356 if ((old_termios
->c_cflag
& CBAUD
) &&
1358 save_flags(flags
); cli();
1359 sio_reg(info
)->flcr
|= TXx927_SIFLCR_RTSSC
|TXx927_SIFLCR_RSDE
;
1360 restore_flags(flags
);
1363 /* Handle transition away from B0 status */
1364 if (!(old_termios
->c_cflag
& CBAUD
) &&
1366 if (!(tty
->termios
->c_cflag
& CRTSCTS
) ||
1367 !test_bit(TTY_THROTTLED
, &tty
->flags
)) {
1368 save_flags(flags
); cli();
1369 sio_reg(info
)->flcr
&= ~(TXx927_SIFLCR_RTSSC
|TXx927_SIFLCR_RSDE
);
1370 restore_flags(flags
);
1374 /* Handle turning off CRTSCTS */
1375 if ((old_termios
->c_cflag
& CRTSCTS
) &&
1376 !(tty
->termios
->c_cflag
& CRTSCTS
)) {
1377 tty
->hw_stopped
= 0;
1383 * ------------------------------------------------------------
1386 * This routine is called when the serial port gets closed. First, we
1387 * wait for the last remaining data to be sent. Then, we unlink its
1388 * async structure from the interrupt chain if necessary, and we free
1389 * that IRQ if nothing is left in the chain.
1390 * ------------------------------------------------------------
1392 static void rs_close(struct tty_struct
*tty
, struct file
* filp
)
1394 struct async_struct
* info
= (struct async_struct
*)tty
->driver_data
;
1395 struct serial_state
*state
;
1396 unsigned long flags
;
1398 if (!info
|| serial_paranoia_check(info
, tty
->device
, "rs_close"))
1401 state
= info
->state
;
1403 save_flags(flags
); cli();
1405 if (tty_hung_up_p(filp
)) {
1406 restore_flags(flags
);
1410 #ifdef SERIAL_DEBUG_OPEN
1411 printk("rs_close ttys%d, count = %d\n", info
->line
, state
->count
);
1413 if ((tty
->count
== 1) && (state
->count
!= 1)) {
1415 * Uh, oh. tty->count is 1, which means that the tty
1416 * structure will be freed. state->count should always
1417 * be one in these conditions. If it's greater than
1418 * one, we've got real problems, since it means the
1419 * serial port won't be shutdown.
1421 printk(KERN_WARNING
"rs_close: bad serial port count; tty->count is 1, "
1422 "state->count is %d\n", state
->count
);
1425 if (--state
->count
< 0) {
1426 printk(KERN_WARNING
"rs_close: bad serial port count for ttys%d: %d\n",
1427 info
->line
, state
->count
);
1431 restore_flags(flags
);
1434 info
->flags
|= ASYNC_CLOSING
;
1436 * Save the termios structure, since this port may have
1437 * separate termios for callout and dialin.
1439 if (info
->flags
& ASYNC_NORMAL_ACTIVE
)
1440 info
->state
->normal_termios
= *tty
->termios
;
1441 if (info
->flags
& ASYNC_CALLOUT_ACTIVE
)
1442 info
->state
->callout_termios
= *tty
->termios
;
1444 * Now we wait for the transmit buffer to clear; and we notify
1445 * the line discipline to only process XON/XOFF characters.
1448 if (info
->closing_wait
!= ASYNC_CLOSING_WAIT_NONE
)
1449 tty_wait_until_sent(tty
, info
->closing_wait
);
1451 * At this point we stop accepting input. To do this, we
1452 * disable the receive line status interrupts, and tell the
1453 * interrupt driver to stop checking the data ready bit in the
1454 * line status register.
1456 info
->read_status_mask
&= ~TXx927_SIDISR_RDIS
;
1457 if (info
->flags
& ASYNC_INITIALIZED
) {
1459 sio_reg(info
)->dicr
&= ~TXx927_SIDICR_RIE
;
1462 * Before we drop DTR, make sure the UART transmitter
1463 * has completely drained; this is especially
1464 * important if there is a transmit FIFO!
1466 rs_wait_until_sent(tty
, info
->timeout
);
1469 if (tty
->driver
.flush_buffer
)
1470 tty
->driver
.flush_buffer(tty
);
1471 if (tty
->ldisc
.flush_buffer
)
1472 tty
->ldisc
.flush_buffer(tty
);
1476 if (info
->blocked_open
) {
1477 if (info
->close_delay
) {
1478 current
->state
= TASK_INTERRUPTIBLE
;
1479 schedule_timeout(info
->close_delay
);
1481 wake_up_interruptible(&info
->open_wait
);
1483 info
->flags
&= ~(ASYNC_NORMAL_ACTIVE
|ASYNC_CALLOUT_ACTIVE
|
1485 wake_up_interruptible(&info
->close_wait
);
1486 restore_flags(flags
);
1490 * rs_wait_until_sent() --- wait until the transmitter is empty
1492 static void rs_wait_until_sent(struct tty_struct
*tty
, int timeout
)
1494 struct async_struct
* info
= (struct async_struct
*)tty
->driver_data
;
1495 unsigned long orig_jiffies
, char_time
;
1498 if (serial_paranoia_check(info
, tty
->device
, "rs_wait_until_sent"))
1501 if (info
->xmit_fifo_size
== 0)
1502 return; /* Just in case.... */
1504 orig_jiffies
= jiffies
;
1506 * Set the check interval to be 1/5 of the estimated time to
1507 * send a single character, and make it at least 1. The check
1508 * interval should also be less than the timeout.
1510 * Note: we have to use pretty tight timings here to satisfy
1513 char_time
= (info
->timeout
- HZ
/50) / info
->xmit_fifo_size
;
1514 char_time
= char_time
/ 5;
1518 char_time
= MIN(char_time
, timeout
);
1520 * If the transmitter hasn't cleared in twice the approximate
1521 * amount of time to send the entire FIFO, it probably won't
1522 * ever clear. This assumes the UART isn't doing flow
1523 * control, which is currently the case. Hence, if it ever
1524 * takes longer than info->timeout, this is probably due to a
1525 * UART bug of some kind. So, we clamp the timeout parameter at
1528 if (!timeout
|| timeout
> 2*info
->timeout
)
1529 timeout
= 2*info
->timeout
;
1530 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1531 printk("In rs_wait_until_sent(%d) check=%lu...", timeout
, char_time
);
1532 printk("jiff=%lu...", jiffies
);
1534 while (!((cisr
= sio_reg(info
)->cisr
) & TXx927_SICISR_TXALS
)) {
1535 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1536 printk("cisr = %d (jiff=%lu)...", cisr
, jiffies
);
1538 __set_current_state(TASK_INTERRUPTIBLE
);
1539 schedule_timeout(char_time
);
1540 if (signal_pending(current
))
1542 if (timeout
&& time_after(jiffies
, orig_jiffies
+ timeout
))
1545 current
->state
= TASK_RUNNING
;
1546 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1547 printk("cisr = %d (jiff=%lu)...done\n", cisr
, jiffies
);
1552 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1554 static void rs_hangup(struct tty_struct
*tty
)
1556 struct async_struct
* info
= (struct async_struct
*)tty
->driver_data
;
1557 struct serial_state
*state
= info
->state
;
1559 if (serial_paranoia_check(info
, tty
->device
, "rs_hangup"))
1562 state
= info
->state
;
1564 rs_flush_buffer(tty
);
1568 info
->flags
&= ~(ASYNC_NORMAL_ACTIVE
|ASYNC_CALLOUT_ACTIVE
);
1570 wake_up_interruptible(&info
->open_wait
);
1574 * ------------------------------------------------------------
1575 * rs_open() and friends
1576 * ------------------------------------------------------------
1578 static int block_til_ready(struct tty_struct
*tty
, struct file
* filp
,
1579 struct async_struct
*info
)
1581 DECLARE_WAITQUEUE(wait
, current
);
1582 struct serial_state
*state
= info
->state
;
1584 int do_clocal
= 0, extra_count
= 0;
1585 unsigned long flags
;
1588 * If the device is in the middle of being closed, then block
1589 * until it's done, and then try again.
1591 if (tty_hung_up_p(filp
) ||
1592 (info
->flags
& ASYNC_CLOSING
)) {
1593 if (info
->flags
& ASYNC_CLOSING
)
1594 interruptible_sleep_on(&info
->close_wait
);
1595 #ifdef SERIAL_DO_RESTART
1596 return ((info
->flags
& ASYNC_HUP_NOTIFY
) ?
1597 -EAGAIN
: -ERESTARTSYS
);
1604 * If this is a callout device, then just make sure the normal
1605 * device isn't being used.
1607 if (tty
->driver
.subtype
== SERIAL_TYPE_CALLOUT
) {
1608 if (info
->flags
& ASYNC_NORMAL_ACTIVE
)
1610 if ((info
->flags
& ASYNC_CALLOUT_ACTIVE
) &&
1611 (info
->flags
& ASYNC_SESSION_LOCKOUT
) &&
1612 (info
->session
!= current
->session
))
1614 if ((info
->flags
& ASYNC_CALLOUT_ACTIVE
) &&
1615 (info
->flags
& ASYNC_PGRP_LOCKOUT
) &&
1616 (info
->pgrp
!= current
->pgrp
))
1618 info
->flags
|= ASYNC_CALLOUT_ACTIVE
;
1623 * If non-blocking mode is set, or the port is not enabled,
1624 * then make the check up front and then exit.
1626 if ((filp
->f_flags
& O_NONBLOCK
) ||
1627 (tty
->flags
& (1 << TTY_IO_ERROR
))) {
1628 if (info
->flags
& ASYNC_CALLOUT_ACTIVE
)
1630 info
->flags
|= ASYNC_NORMAL_ACTIVE
;
1634 if (info
->flags
& ASYNC_CALLOUT_ACTIVE
) {
1635 if (state
->normal_termios
.c_cflag
& CLOCAL
)
1638 if (tty
->termios
->c_cflag
& CLOCAL
)
1643 * Block waiting for the carrier detect and the line to become
1644 * free (i.e., not in use by the callout). While we are in
1645 * this loop, state->count is dropped by one, so that
1646 * rs_close() knows when to free things. We restore it upon
1647 * exit, either normal or abnormal.
1650 add_wait_queue(&info
->open_wait
, &wait
);
1651 #ifdef SERIAL_DEBUG_OPEN
1652 printk("block_til_ready before block: ttys%d, count = %d\n",
1653 state
->line
, state
->count
);
1655 save_flags(flags
); cli();
1656 if (!tty_hung_up_p(filp
)) {
1660 restore_flags(flags
);
1661 info
->blocked_open
++;
1663 save_flags(flags
); cli();
1664 if (!(info
->flags
& ASYNC_CALLOUT_ACTIVE
) &&
1665 (tty
->termios
->c_cflag
& CBAUD
))
1666 sio_reg(info
)->flcr
&= ~(TXx927_SIFLCR_RTSSC
|TXx927_SIFLCR_RSDE
);
1667 restore_flags(flags
);
1668 current
->state
= TASK_INTERRUPTIBLE
;
1669 if (tty_hung_up_p(filp
) ||
1670 !(info
->flags
& ASYNC_INITIALIZED
)) {
1671 #ifdef SERIAL_DO_RESTART
1672 if (info
->flags
& ASYNC_HUP_NOTIFY
)
1675 retval
= -ERESTARTSYS
;
1681 if (!(info
->flags
& ASYNC_CALLOUT_ACTIVE
) &&
1682 !(info
->flags
& ASYNC_CLOSING
))
1684 if (signal_pending(current
)) {
1685 retval
= -ERESTARTSYS
;
1688 #ifdef SERIAL_DEBUG_OPEN
1689 printk("block_til_ready blocking: ttys%d, count = %d\n",
1690 info
->line
, state
->count
);
1694 current
->state
= TASK_RUNNING
;
1695 remove_wait_queue(&info
->open_wait
, &wait
);
1698 info
->blocked_open
--;
1699 #ifdef SERIAL_DEBUG_OPEN
1700 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1701 info
->line
, state
->count
);
1705 info
->flags
|= ASYNC_NORMAL_ACTIVE
;
1709 static int get_async_struct(int line
, struct async_struct
**ret_info
)
1711 struct async_struct
*info
;
1712 struct serial_state
*sstate
;
1715 if (kdb_port_info
.state
&& line
== kdb_port_info
.line
)
1718 sstate
= rs_table
+ line
;
1721 *ret_info
= sstate
->info
;
1724 info
= kmalloc(sizeof(struct async_struct
), GFP_KERNEL
);
1729 memset(info
, 0, sizeof(struct async_struct
));
1730 init_waitqueue_head(&info
->open_wait
);
1731 init_waitqueue_head(&info
->close_wait
);
1732 init_waitqueue_head(&info
->delta_msr_wait
);
1733 info
->magic
= SERIAL_MAGIC
;
1734 info
->port
= sstate
->port
;
1735 info
->flags
= sstate
->flags
;
1736 info
->io_type
= sstate
->io_type
;
1737 info
->xmit_fifo_size
= sstate
->xmit_fifo_size
;
1739 info
->tqueue
.routine
= do_softint
;
1740 info
->tqueue
.data
= info
;
1741 info
->state
= sstate
;
1744 *ret_info
= sstate
->info
;
1747 *ret_info
= sstate
->info
= info
;
1752 * This routine is called whenever a serial port is opened. It
1753 * enables interrupts for a serial port, linking in its async structure into
1754 * the IRQ chain. It also performs the serial-specific
1755 * initialization for the tty structure.
1757 static int rs_open(struct tty_struct
*tty
, struct file
* filp
)
1759 struct async_struct
*info
;
1763 line
= minor(tty
->device
) - tty
->driver
.minor_start
;
1764 if ((line
< 0) || (line
>= NR_PORTS
)) {
1767 retval
= get_async_struct(line
, &info
);
1771 tty
->driver_data
= info
;
1774 #ifdef SERIAL_DEBUG_OPEN
1775 printk("rs_open %s%d, count = %d\n", tty
->driver
.name
, info
->line
,
1776 info
->state
->count
);
1778 info
->tty
->low_latency
= (info
->flags
& ASYNC_LOW_LATENCY
) ? 1 : 0;
1781 page
= get_free_page(GFP_KERNEL
);
1788 tmp_buf
= (unsigned char *) page
;
1792 * If the port is the middle of closing, bail out now
1794 if (tty_hung_up_p(filp
) ||
1795 (info
->flags
& ASYNC_CLOSING
)) {
1796 if (info
->flags
& ASYNC_CLOSING
)
1797 interruptible_sleep_on(&info
->close_wait
);
1798 #ifdef SERIAL_DO_RESTART
1799 return ((info
->flags
& ASYNC_HUP_NOTIFY
) ?
1800 -EAGAIN
: -ERESTARTSYS
);
1807 * Start up serial port
1809 retval
= startup(info
);
1814 retval
= block_til_ready(tty
, filp
, info
);
1816 #ifdef SERIAL_DEBUG_OPEN
1817 printk("rs_open returning after block_til_ready with %d\n",
1823 if ((info
->state
->count
== 1) &&
1824 (info
->flags
& ASYNC_SPLIT_TERMIOS
)) {
1825 if (tty
->driver
.subtype
== SERIAL_TYPE_NORMAL
)
1826 *tty
->termios
= info
->state
->normal_termios
;
1828 *tty
->termios
= info
->state
->callout_termios
;
1829 change_speed(info
, 0);
1831 #ifdef CONFIG_TXX927_SERIAL_CONSOLE
1832 if (sercons
.cflag
&& sercons
.index
== line
) {
1833 tty
->termios
->c_cflag
= sercons
.cflag
;
1835 change_speed(info
, 0);
1838 info
->session
= current
->session
;
1839 info
->pgrp
= current
->pgrp
;
1841 #ifdef SERIAL_DEBUG_OPEN
1842 printk("rs_open ttys%d successful...", info
->line
);
1848 * /proc fs routines....
1851 static inline int line_info(char *buf
, struct serial_state
*state
)
1853 struct async_struct
*info
= state
->info
, scr_info
;
1856 unsigned long flags
;
1858 ret
= sprintf(buf
, "%d: uart:%s port:%lX irq:%d",
1859 state
->line
, SERIAL_DRIVER_NAME
,
1860 state
->port
, state
->irq
);
1863 ret
+= sprintf(buf
+ret
, "\n");
1868 * Figure out the current RS-232 lines
1871 info
= &scr_info
; /* This is just for serial_{in,out} */
1873 info
->magic
= SERIAL_MAGIC
;
1874 info
->port
= state
->port
;
1875 info
->flags
= state
->flags
;
1882 save_flags(flags
); cli();
1883 if (!(sio_reg(info
)->flcr
& TXx927_SIFLCR_RTSSC
))
1884 strcat(stat_buf
, "|RTS");
1885 if (!(sio_reg(info
)->cisr
& TXx927_SICISR_CTSS
))
1886 strcat(stat_buf
, "|CTS");
1887 restore_flags(flags
);
1890 ret
+= sprintf(buf
+ret
, " baud:%d",
1891 state
->baud_base
/ info
->quot
);
1894 ret
+= sprintf(buf
+ret
, " tx:%d rx:%d",
1895 state
->icount
.tx
, state
->icount
.rx
);
1897 if (state
->icount
.frame
)
1898 ret
+= sprintf(buf
+ret
, " fe:%d", state
->icount
.frame
);
1900 if (state
->icount
.parity
)
1901 ret
+= sprintf(buf
+ret
, " pe:%d", state
->icount
.parity
);
1903 if (state
->icount
.brk
)
1904 ret
+= sprintf(buf
+ret
, " brk:%d", state
->icount
.brk
);
1906 if (state
->icount
.overrun
)
1907 ret
+= sprintf(buf
+ret
, " oe:%d", state
->icount
.overrun
);
1910 * Last thing is the RS-232 status lines
1912 ret
+= sprintf(buf
+ret
, " %s\n", stat_buf
+1);
1916 static int rs_read_proc(char *page
, char **start
, off_t off
, int count
,
1917 int *eof
, void *data
)
1922 len
+= sprintf(page
, "serinfo:1.0 driver:%s\n", serial_version
);
1923 for (i
= 0; i
< NR_PORTS
&& len
< 4000; i
++) {
1924 l
= line_info(page
+ len
, &rs_table
[i
]);
1926 if (len
+begin
> off
+count
)
1928 if (len
+begin
< off
) {
1935 if (off
>= len
+begin
)
1937 *start
= page
+ (begin
-off
);
1938 return ((count
< begin
+len
-off
) ? count
: begin
+len
-off
);
1942 * ---------------------------------------------------------------------
1943 * rs_init() and friends
1945 * rs_init() is called at boot-time to initialize the serial driver.
1946 * ---------------------------------------------------------------------
1950 * This routine prints out the appropriate serial driver version
1951 * number, and identifies which options were configured into this
1954 static _INLINE_
void show_serial_version(void)
1956 printk(KERN_INFO
"%s version %s\n", serial_name
, serial_version
);
1960 * The serial driver boot-time initialization code!
1962 static int __init
rs_init(void)
1965 struct serial_state
* state
;
1967 if (rs_table
[0].port
== 0)
1970 init_bh(TXX927_SERIAL_BH
, do_serial_bh
);
1971 init_timer(&serial_timer
);
1972 serial_timer
.function
= rs_timer
;
1973 mod_timer(&serial_timer
, jiffies
+ RS_STROBE_TIME
);
1975 for (i
= 0; i
< NR_IRQS
; i
++) {
1979 #ifdef CONFIG_TXX927_SERIAL_CONSOLE
1981 * The interrupt of the serial console port
1984 if (sercons
.flags
& CON_CONSDEV
) {
1985 for(i
= 0; i
< NR_PORTS
; i
++)
1986 if (i
!= sercons
.index
&&
1987 rs_table
[i
].irq
== rs_table
[sercons
.index
].irq
)
1988 rs_table
[i
].irq
= 0;
1991 show_serial_version();
1993 /* Initialize the tty_driver structure */
1995 memset(&serial_driver
, 0, sizeof(struct tty_driver
));
1996 serial_driver
.magic
= TTY_DRIVER_MAGIC
;
1997 serial_driver
.driver_name
= "txx927serial";
1998 #if (LINUX_VERSION_CODE > 0x2032D && defined(CONFIG_DEVFS_FS))
1999 serial_driver
.name
= "tts/%d";
2001 serial_driver
.name
= "ttyS";
2003 serial_driver
.major
= TXX927_TTY_MAJOR
;
2004 serial_driver
.minor_start
= TXX927_TTY_MINOR_START
+ SERIAL_DEV_OFFSET
;
2005 serial_driver
.num
= NR_PORTS
;
2006 serial_driver
.type
= TTY_DRIVER_TYPE_SERIAL
;
2007 serial_driver
.subtype
= SERIAL_TYPE_NORMAL
;
2008 serial_driver
.init_termios
= tty_std_termios
;
2009 serial_driver
.init_termios
.c_cflag
=
2010 B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
2011 serial_driver
.flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_NO_DEVFS
;
2012 serial_driver
.refcount
= &serial_refcount
;
2013 serial_driver
.table
= serial_table
;
2014 serial_driver
.termios
= serial_termios
;
2015 serial_driver
.termios_locked
= serial_termios_locked
;
2017 serial_driver
.open
= rs_open
;
2018 serial_driver
.close
= rs_close
;
2019 serial_driver
.write
= rs_write
;
2020 serial_driver
.put_char
= rs_put_char
;
2021 serial_driver
.flush_chars
= rs_flush_chars
;
2022 serial_driver
.write_room
= rs_write_room
;
2023 serial_driver
.chars_in_buffer
= rs_chars_in_buffer
;
2024 serial_driver
.flush_buffer
= rs_flush_buffer
;
2025 serial_driver
.ioctl
= rs_ioctl
;
2026 serial_driver
.throttle
= rs_throttle
;
2027 serial_driver
.unthrottle
= rs_unthrottle
;
2028 serial_driver
.send_xchar
= rs_send_xchar
;
2029 serial_driver
.set_termios
= rs_set_termios
;
2030 serial_driver
.stop
= rs_stop
;
2031 serial_driver
.start
= rs_start
;
2032 serial_driver
.hangup
= rs_hangup
;
2033 serial_driver
.break_ctl
= rs_break
;
2034 serial_driver
.wait_until_sent
= rs_wait_until_sent
;
2035 serial_driver
.read_proc
= rs_read_proc
;
2038 * The callout device is just like normal device except for
2039 * major number and the subtype code.
2041 callout_driver
= serial_driver
;
2042 #if (LINUX_VERSION_CODE > 0x2032D && defined(CONFIG_DEVFS_FS))
2043 callout_driver
.name
= "cua/%d";
2045 callout_driver
.name
= "cua";
2047 callout_driver
.major
= TTYAUX_MAJOR
;
2048 callout_driver
.subtype
= SERIAL_TYPE_CALLOUT
;
2049 callout_driver
.read_proc
= 0;
2050 callout_driver
.proc_entry
= 0;
2052 if (tty_register_driver(&serial_driver
)){
2053 panic("Couldn't register serial driver\n");
2055 if (tty_register_driver(&callout_driver
)) {
2056 panic("Couldn't register callout driver\n");
2059 for (i
= 0, state
= rs_table
; i
< NR_PORTS
; i
++,state
++) {
2060 state
->magic
= SSTATE_MAGIC
;
2062 state
->type
= PORT_UNKNOWN
;
2063 state
->custom_divisor
= 0;
2064 state
->close_delay
= 5*HZ
/10;
2065 state
->closing_wait
= 30*HZ
;
2066 state
->callout_termios
= callout_driver
.init_termios
;
2067 state
->normal_termios
= serial_driver
.init_termios
;
2068 state
->icount
.cts
= state
->icount
.dsr
=
2069 state
->icount
.rng
= state
->icount
.dcd
= 0;
2070 state
->icount
.rx
= state
->icount
.tx
= 0;
2071 state
->icount
.frame
= state
->icount
.parity
= 0;
2072 state
->icount
.overrun
= state
->icount
.brk
= 0;
2073 state
->irq
= irq_cannonicalize(state
->irq
);
2074 state
->xmit_fifo_size
= TXx927_SIO_TX_FIFO
;
2076 state
->io_type
= SERIAL_IO_HUB6
;
2082 for (i
= 0, state
= rs_table
; i
< NR_PORTS
; i
++,state
++) {
2083 if (state
->type
== PORT_UNKNOWN
) {
2086 printk(KERN_INFO
"%s%02d at 0x%04lx (irq = %d) is a %s\n",
2089 state
->port
, state
->irq
,
2090 SERIAL_DRIVER_NAME
);
2091 tty_register_devfs(&serial_driver
, 0,
2092 serial_driver
.minor_start
+ state
->line
);
2093 tty_register_devfs(&callout_driver
, 0,
2094 callout_driver
.minor_start
+ state
->line
);
2099 static void __exit
rs_fini(void)
2101 unsigned long flags
;
2104 struct async_struct
*info
;
2106 del_timer_sync(&serial_timer
);
2107 save_flags(flags
); cli();
2108 remove_bh(TXX927_SERIAL_BH
);
2109 if ((e1
= tty_unregister_driver(&serial_driver
)))
2110 printk(KERN_WARNING
"serial: failed to unregister serial driver (%d)\n",
2112 if ((e2
= tty_unregister_driver(&callout_driver
)))
2113 printk(KERN_WARNING
"serial: failed to unregister callout driver (%d)\n",
2115 restore_flags(flags
);
2117 for (i
= 0; i
< NR_PORTS
; i
++) {
2118 if ((info
= rs_table
[i
].info
)) {
2119 rs_table
[i
].info
= NULL
;
2124 unsigned long pg
= (unsigned long) tmp_buf
;
2130 module_init(rs_init
);
2131 module_exit(rs_fini
);
2132 MODULE_DESCRIPTION("TXX927 serial driver");
2135 * ------------------------------------------------------------
2136 * Serial console driver
2137 * ------------------------------------------------------------
2139 #ifdef CONFIG_TXX927_SERIAL_CONSOLE
2141 static struct async_struct async_sercons
;
2144 * Print a string to the serial port trying not to disturb
2145 * any possible real use of the port...
2147 static void serial_console_write(struct console
*co
, const char *s
,
2150 static struct async_struct
*info
= &async_sercons
;
2155 * First save the IER then disable the interrupts
2157 ier
= sio_reg(info
)->dicr
;
2158 sio_reg(info
)->dicr
= 0;
2162 * Now, do each character
2164 for (i
= 0; i
< count
; i
++, s
++) {
2165 wait_for_xmitr(info
);
2168 * Send the character out.
2169 * If a LF, also do CR...
2171 sio_reg(info
)->tfifo
= *s
;
2173 wait_for_xmitr(info
);
2174 sio_reg(info
)->tfifo
= 13;
2179 * Finally, Wait for transmitter & holding register to empty
2180 * and restore the IER
2182 wait_for_xmitr(info
);
2183 sio_reg(info
)->dicr
= ier
;
2186 static kdev_t
serial_console_device(struct console
*c
)
2188 return mk_kdev(TXX927_TTY_MAJOR
, TXX927_TTY_MINOR_START
+ c
->index
);
2192 * Setup initial baud/bits/parity. We do two things here:
2193 * - construct a cflag setting for the first rs_open()
2194 * - initialize the serial port
2195 * Return non-zero if we didn't find a serial port.
2197 static int serial_console_setup(struct console
*co
, char *options
)
2199 static struct async_struct
*info
;
2200 struct serial_state
*state
;
2205 int cflag
= CREAD
| HUPCL
| CLOCAL
;
2209 if (co
->index
< 0 || co
->index
>= NR_PORTS
) {
2213 baud
= simple_strtoul(options
, NULL
, 10);
2215 while(*s
>= '0' && *s
<= '9')
2217 if (*s
) parity
= *s
++;
2218 if (*s
) bits
= *s
- '0';
2222 * Now construct a cflag setting.
2271 * Divisor, bytesize and parity
2273 state
= rs_table
+ co
->index
;
2274 info
= &async_sercons
;
2275 info
->magic
= SERIAL_MAGIC
;
2276 info
->state
= state
;
2277 info
->port
= state
->port
;
2278 info
->flags
= state
->flags
;
2279 info
->io_type
= state
->io_type
;
2280 info
->iomem_base
= state
->iomem_base
;
2281 info
->iomem_reg_shift
= state
->iomem_reg_shift
;
2282 quot
= state
->baud_base
/ baud
;
2284 switch (cflag
& CSIZE
) {
2285 case CS7
: cval
= TXx927_SILCR_UMODE_7BIT
; break;
2287 case CS8
: cval
= TXx927_SILCR_UMODE_8BIT
; break;
2290 cval
|= TXx927_SILCR_USBL_2BIT
;
2292 cval
|= TXx927_SILCR_USBL_1BIT
;
2294 cval
|= TXx927_SILCR_UPEN
;
2295 if (!(cflag
& PARODD
))
2296 cval
|= TXx927_SILCR_UEPS
;
2299 * Disable UART interrupts, set DTR and RTS high
2302 sio_reg(info
)->dicr
= 0;
2303 sio_reg(info
)->lcr
= cval
| TXx927_SILCR_SCS_IMCLK_BG
;
2304 sio_reg(info
)->bgr
= quot
| TXx927_SIBGR_BCLK_T0
;
2305 /* HW RTS/CTS control */
2306 if (info
->flags
& ASYNC_HAVE_CTS_LINE
)
2307 sio_reg(info
)->flcr
= TXx927_SIFLCR_RCS
| TXx927_SIFLCR_TES
|
2308 TXx927_SIFLCR_RTSTL_MAX
/* 15 */;
2310 sio_reg(info
)->flcr
&= ~(TXx927_SIFLCR_RSDE
| TXx927_SIFLCR_TSDE
);
2315 static struct console sercons
= {
2316 name
: TXX927_TTY_NAME
,
2317 write
: serial_console_write
,
2318 device
: serial_console_device
,
2319 setup
: serial_console_setup
,
2320 flags
: CON_PRINTBUFFER
,
2327 void __init
txx927_console_init(void)
2329 register_console(&sercons
);