Coarsly sort out 32-bit-only, 64-bit-only and ``portable'' MIPS lib/
[linux-2.6/linux-mips.git] / drivers / char / serial_txx927.c
blob0424420c3a241b68aadf1d1e36e82a9db9b8c6b0
1 /*
2 * drivers/char/serial_txx927.c
3 * driver for TX[34]927 SIO
5 * Copyright 2001 MontaVista Software Inc.
6 * Author: MontaVista Software, Inc.
7 * ahennessy@mvista.com
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
45 #ifdef MODULE
46 #undef CONFIG_TXX927_SERIAL_CONSOLE
47 #endif
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.
58 #ifdef MODVERSIONS
59 #include <linux/modversions.h>
60 #endif
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>
80 #include <linux/mm.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>
87 #endif
88 #ifdef CONFIG_MAGIC_SYSRQ
89 #include <linux/sysrq.h>
90 #endif
92 #include <asm/system.h>
93 #include <asm/serial.h>
94 #include <asm/io.h>
95 #include <asm/irq.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>
102 #endif
104 #define _INLINE_ inline
106 #ifdef CONFIG_MAC_SERIAL
107 #define SERIAL_DEV_OFFSET 2
108 #else
109 #define SERIAL_DEV_OFFSET 0
110 #endif
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
128 #endif
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;
142 #endif
143 #if defined(CONFIG_TXX927_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
144 static unsigned long break_pressed; /* break, really ... */
145 #endif
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);
150 #ifndef PREPARE_FUNC
151 #define PREPARE_FUNC(dev) (dev->prepare)
152 #define ACTIVATE_FUNC(dev) (dev->activate)
153 #define DEACTIVATE_FUNC(dev) (dev->deactivate)
154 #endif
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)
162 #else
163 #define DBG_CNT(s)
164 #endif
166 #define SERIAL_DRIVER_NAME "TXx927SIO"
168 #ifdef CONFIG_SERIAL
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
174 #else
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
180 #endif
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];
196 #ifndef MIN
197 #define MIN(a,b) ((a) < (b) ? (a) : (b))
198 #endif
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;
210 #ifdef DECLARE_MUTEX
211 static DECLARE_MUTEX(tmp_buf_sem);
212 #else
213 static struct semaphore tmp_buf_sem = MUTEX;
214 #endif
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";
225 if (!info) {
226 printk(badinfo, kdevname(device), routine);
227 return 1;
229 if (info->magic != SERIAL_MAGIC) {
230 printk(badmagic, kdevname(device), routine);
231 return 1;
233 #endif
234 return 0;
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;
249 do {
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;
265 unsigned long flags;
267 if (serial_paranoia_check(info, tty->device, "rs_stop"))
268 return;
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;
281 unsigned long flags;
283 if (serial_paranoia_check(info, tty->device, "rs_start"))
284 return;
286 save_flags(flags); cli();
287 if (info->xmit.head != info->xmit.tail
288 && info->xmit.buf
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
307 * idea to do:
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,
322 int event)
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,
330 int *status)
332 struct tty_struct *tty = info->tty;
333 unsigned char ch;
334 int ignored = 0;
335 struct async_icount *icount;
337 icount = &info->state->icount;
338 do {
339 ch = sio_reg(info)->rfifo;
340 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
341 break;
342 *tty->flip.char_buf_ptr = ch;
343 icount->rx++;
345 #ifdef SERIAL_DEBUG_INTR
346 printk("DR%02x:%02x...", ch, *status);
347 #endif
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);
356 icount->brk++;
357 } else if (*status & TXx927_SIDISR_UPER)
358 icount->parity++;
359 else if (*status & TXx927_SIDISR_UFER)
360 icount->frame++;
361 if (*status & TXx927_SIDISR_UOER)
362 icount->overrun++;
365 * Now check to see if character should be
366 * ignored, and mask off conditions which
367 * should be ignored.
369 if (*status & info->ignore_status_mask) {
370 if (++ignored > 100)
371 break;
372 goto ignore_char;
374 *status &= info->read_status_mask;
376 if (*status & (TXx927_SIDISR_UBRK)) {
377 #ifdef SERIAL_DEBUG_INTR
378 printk("handling break....");
379 #endif
380 *tty->flip.flag_buf_ptr = TTY_BREAK;
381 if (info->flags & ASYNC_SAK)
382 do_SAK(tty);
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) {
394 tty->flip.count++;
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++;
403 tty->flip.count++;
404 ignore_char:
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)
413 int count;
415 wait_for_xmitr(info);
417 if (info->x_char) {
418 sio_reg(info)->tfifo = info->x_char;
419 info->state->icount.tx++;
420 info->x_char = 0;
421 if (intr_done)
422 *intr_done = 0;
423 return;
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;
430 return;
433 count = info->xmit_fifo_size;
434 do {
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)
439 break;
440 } while (--count > 0);
442 if (CIRC_CNT(info->xmit.head,
443 info->xmit.tail,
444 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
445 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
447 #ifdef SERIAL_DEBUG_INTR
448 printk("THRE...");
449 #endif
450 if (intr_done)
451 *intr_done = 0;
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)
468 int status;
469 int pass_counter = 0;
470 struct async_struct * info;
472 #ifdef SERIAL_DEBUG_INTR
473 printk("rs_interrupt_single(%d)...", irq);
474 #endif
476 info = IRQ_ports[irq];
477 if (!info || !info->tty)
478 return;
480 do {
481 status = sio_reg(info)->disr;
482 #ifdef SERIAL_DEBUG_INTR
483 printk("status = %x...", status);
484 #endif
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)))
488 break;
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");
501 #endif
502 break;
504 } while (1);
505 info->last_active = jiffies;
506 #ifdef SERIAL_DEBUG_INTR
507 printk("end.\n");
508 #endif
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;
536 tty = info->tty;
537 if (!tty)
538 return;
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);
547 #endif
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
556 * CPU overhead).
558 static void rs_timer(unsigned long dummy)
560 static unsigned long last_strobe;
561 struct async_struct *info;
562 unsigned int i;
563 unsigned long flags;
565 if ((jiffies - last_strobe) >= RS_STROBE_TIME) {
566 for (i=0; i < NR_IRQS; i++) {
567 info = IRQ_ports[i];
568 if (!info)
569 continue;
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);
578 #if 0
579 if (IRQ_ports[0]) {
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]);
586 #endif
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];
609 if (!info) {
610 IRQ_timeout[irq] = 60*HZ;
611 return;
613 while (info) {
614 if (info->timeout < timeout)
615 timeout = info->timeout;
616 info = info->next_port;
618 if (!irq)
619 timeout = timeout / 2;
620 IRQ_timeout[irq] = timeout ? timeout : 1;
623 static int startup(struct async_struct * info)
625 unsigned long flags;
626 int retval=0;
627 struct serial_state *state= info->state;
628 unsigned long page;
630 page = get_zeroed_page(GFP_KERNEL);
631 if (!page)
632 return -ENOMEM;
634 save_flags(flags); cli();
636 if (info->flags & ASYNC_INITIALIZED) {
637 free_page(page);
638 goto errout;
641 if (!state->port) {
642 if (info->tty)
643 set_bit(TTY_IO_ERROR, &info->tty->flags);
644 free_page(page);
645 goto errout;
647 if (info->xmit.buf)
648 free_page(page);
649 else
650 info->xmit.buf = (unsigned char *) page;
652 #ifdef SERIAL_DEBUG_OPEN
653 printk("starting up ttys%d (irq %d)...", info->line, state->irq);
654 #endif
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 |
661 TXx927_SIFCR_FRSTE;
662 /* clear reset */
663 sio_reg(info)->fcr &= ~(TXx927_SIFCR_TFRST | TXx927_SIFCR_RFRST |
664 TXx927_SIFCR_FRSTE);
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]) {
672 retval = -EBUSY;
673 goto errout;
676 retval = request_irq(state->irq, rs_interrupt_single,
677 SA_INTERRUPT,
678 "txx927serial", NULL);
679 if (retval) {
680 if (capable(CAP_SYS_ADMIN)) {
681 if (info->tty)
682 set_bit(TTY_IO_ERROR,
683 &info->tty->flags);
684 retval = 0;
686 goto errout;
691 * Insert serial port into IRQ chain.
693 info->prev_port = 0;
694 info->next_port = IRQ_ports[state->irq];
695 if (info->next_port)
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 */;
712 /* Enable RX/TX */
713 sio_reg(info)->flcr &= ~(TXx927_SIFLCR_RSDE | TXx927_SIFLCR_TSDE);
716 * Finally, enable interrupts
718 sio_reg(info)->dicr = TXx927_SIDICR_RIE;
720 if (info->tty)
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
727 if (info->tty) {
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);
745 return 0;
747 errout:
748 restore_flags(flags);
749 return retval;
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)
758 unsigned long flags;
759 struct serial_state *state;
760 int retval;
762 if (!(info->flags & ASYNC_INITIALIZED))
763 return;
765 state = info->state;
767 #ifdef SERIAL_DEBUG_OPEN
768 printk("Shutting down serial port %d (irq %d)....", info->line,
769 state->irq);
770 #endif
772 save_flags(flags); cli(); /* Disable interrupts */
775 * First unlink the serial port from the IRQ chain...
777 if (info->next_port)
778 info->next_port->prev_port = info->prev_port;
779 if (info->prev_port)
780 info->prev_port->next_port = info->next_port;
781 else
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);
795 if (retval)
796 printk(KERN_WARNING "txx927serial shutdown: request_irq: error %d"
797 " Couldn't reacquire IRQ.\n", retval);
798 } else
799 free_irq(state->irq, NULL);
802 if (info->xmit.buf) {
803 free_page((unsigned long) info->xmit.buf);
804 info->xmit.buf = 0;
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)) {
813 /* drop RTS */
814 sio_reg(info)->flcr |= TXx927_SIFLCR_RTSSC|TXx927_SIFLCR_RSDE;
815 /* TXx927-SIO can not control DTR... */
818 /* reset FIFO's */
819 sio_reg(info)->fcr |= TXx927_SIFCR_TFRST | TXx927_SIFCR_RFRST |
820 TXx927_SIFCR_FRSTE;
821 /* clear reset */
822 sio_reg(info)->fcr &= ~(TXx927_SIFCR_TFRST | TXx927_SIFCR_RFRST |
823 TXx927_SIFCR_FRSTE);
825 /* DON'T disable Rx/Tx here, ie. DON'T set either
826 * TXx927_SIFLCR_RSDE or TXx927_SIFLCR_TSDE in flcr
829 if (info->tty)
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;
845 int bits;
846 unsigned long flags;
848 if (!info->tty || !info->tty->termios)
849 return;
850 cflag = info->tty->termios->c_cflag;
851 if (!info->port)
852 return;
854 cval = sio_reg(info)->lcr;
855 /* byte size and parity */
856 cval &= ~TXx927_SILCR_UMODE_MASK;
857 switch (cflag & CSIZE) {
858 case CS7:
859 cval |= TXx927_SILCR_UMODE_7BIT;
860 bits = 9;
861 break;
862 case CS5: /* not supported */
863 case CS6: /* not supported */
864 case CS8:
865 default:
866 cval |= TXx927_SILCR_UMODE_8BIT;
867 bits = 10;
868 break;
870 cval &= ~TXx927_SILCR_USBL_MASK;
871 if (cflag & CSTOPB) {
872 cval |= TXx927_SILCR_USBL_2BIT;
873 bits++;
874 } else {
875 cval |= TXx927_SILCR_USBL_1BIT;
878 cval &= ~(TXx927_SILCR_UPEN|TXx927_SILCR_UEPS);
879 if (cflag & PARENB) {
880 cval |= TXx927_SILCR_UPEN;
881 bits++;
883 if (!(cflag & PARODD))
884 cval |= TXx927_SILCR_UEPS;
886 /* Determine divisor based on baud rate */
887 baud = tty_get_baud_rate(info->tty);
888 if (!baud)
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);
897 if (!baud)
898 baud = 9600;
899 quot = (baud_base + baud / 2) / baud;
901 /* As a last resort, if the quotient is zero, default to 9600 bps */
902 if (!quot)
903 quot = (baud_base + 9600 / 2) / 9600;
904 info->quot = quot;
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 */;
914 } else {
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;
960 unsigned long flags;
962 if (serial_paranoia_check(info, tty->device, "rs_put_char"))
963 return;
965 if (!tty || !info->xmit.buf)
966 return;
968 save_flags(flags); cli();
969 if (CIRC_SPACE(info->xmit.head,
970 info->xmit.tail,
971 SERIAL_XMIT_SIZE) == 0) {
972 restore_flags(flags);
973 return;
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;
984 unsigned long flags;
986 if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
987 return;
989 if (info->xmit.head == info->xmit.tail
990 || tty->stopped
991 || tty->hw_stopped
992 || !info->xmit.buf)
993 return;
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)
1003 int c, ret = 0;
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"))
1008 return 0;
1010 if (!tty || !info->xmit.buf || !tmp_buf)
1011 return 0;
1013 save_flags(flags);
1015 if (from_user) {
1016 down(&tmp_buf_sem);
1017 while (1) {
1018 int c1;
1019 c = CIRC_SPACE_TO_END(info->xmit.head,
1020 info->xmit.tail,
1021 SERIAL_XMIT_SIZE);
1023 if (count < c)
1024 c = count;
1025 if (c <= 0)
1026 break;
1028 c -= copy_from_user(tmp_buf, buf, c);
1029 if (!c) {
1030 if (!ret)
1031 ret = -EFAULT;
1032 break;
1034 cli();
1035 c1 = CIRC_SPACE_TO_END(info->xmit.head,
1036 info->xmit.tail,
1037 SERIAL_XMIT_SIZE);
1039 if (c1 < c)
1040 c = c1;
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);
1045 buf += c;
1046 count -= c;
1047 ret += c;
1049 up(&tmp_buf_sem);
1050 } else {
1051 cli();
1052 while (1) {
1053 c = CIRC_SPACE_TO_END(info->xmit.head,
1054 info->xmit.tail,
1055 SERIAL_XMIT_SIZE);
1057 if (count < c)
1058 c = count;
1059 if (c <= 0) {
1060 break;
1062 memcpy(info->xmit.buf + info->xmit.head, buf, c);
1063 info->xmit.head = ((info->xmit.head + c) &
1064 (SERIAL_XMIT_SIZE-1));
1065 buf += c;
1066 count -= c;
1067 ret += c;
1069 restore_flags(flags);
1071 if (info->xmit.head != info->xmit.tail
1072 && !tty->stopped
1073 && !tty->hw_stopped
1074 && !(info->IER & UART_IER_THRI))
1075 sio_reg(info)->dicr |= TXx927_SIDICR_TIE;
1077 return ret;
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"))
1085 return 0;
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"))
1095 return 0;
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"))
1106 return;
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);
1113 #endif
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
1121 * the device
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"))
1129 return;
1131 info->x_char = ch;
1132 if (ch) {
1133 /* Make sure transmit interrupts are on */
1134 sio_reg(info)->dicr |= TXx927_SIDICR_TIE;
1139 * ------------------------------------------------------------
1140 * rs_throttle()
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
1151 char buf[64];
1153 printk("throttle %s: %d....\n", tty_name(tty, buf),
1154 tty->ldisc.chars_in_buffer(tty));
1155 #endif
1157 if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1158 return;
1160 if (I_IXOFF(tty))
1161 rs_send_xchar(tty, STOP_CHAR(tty));
1163 if (tty->termios->c_cflag & CRTSCTS) {
1164 save_flags(flags); cli();
1165 /* drop RTS */
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
1176 char buf[64];
1178 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1179 tty->ldisc.chars_in_buffer(tty));
1180 #endif
1182 if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1183 return;
1185 if (I_IXOFF(tty)) {
1186 if (info->x_char)
1187 info->x_char = 0;
1188 else
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)
1219 int error;
1220 unsigned int arg;
1221 unsigned long flags;
1223 error = get_user(arg, value);
1224 if (error)
1225 return error;
1226 save_flags(flags); cli();
1227 switch (cmd) {
1228 case TIOCMBIS:
1229 if (arg & TIOCM_RTS)
1230 sio_reg(info)->flcr &= ~(TXx927_SIFLCR_RTSSC|TXx927_SIFLCR_RSDE);
1231 break;
1232 case TIOCMBIC:
1233 if (arg & TIOCM_RTS)
1234 sio_reg(info)->flcr |= TXx927_SIFLCR_RTSSC|TXx927_SIFLCR_RSDE;
1235 break;
1236 case TIOCMSET:
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);
1240 break;
1241 default:
1242 error = -EINVAL;
1244 restore_flags(flags);
1245 return error;
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"))
1257 return;
1259 if (!info->port)
1260 return;
1261 save_flags(flags); cli();
1262 if (break_state == -1)
1263 sio_reg(info)->flcr |= TXx927_SIFLCR_TBRK;
1264 else
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"))
1275 return -ENODEV;
1277 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1278 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1279 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1280 if (tty->flags & (1 << TTY_IO_ERROR))
1281 return -EIO;
1284 switch (cmd) {
1285 case TIOCMGET:
1286 return get_modem_info(info, (unsigned int *) arg);
1287 case TIOCMBIS:
1288 case TIOCMBIC:
1289 case TIOCMSET:
1290 return set_modem_info(info, cmd, (unsigned int *) arg);
1291 return 0;
1292 case TIOCGSERIAL:
1293 printk("TIOCGSERIAL\n");
1294 return 0;
1295 case TIOCSSERIAL:
1296 printk("TIOCSSERIAL\n");
1297 return 0;
1298 case TIOCSERCONFIG:
1299 printk("TIOCSERCONFIG\n");
1300 return 0;
1302 case TIOCSERGETLSR: /* Get line status register */
1303 printk("TIOCSERGETLSR\n");
1304 return 0;
1306 case TIOCSERGSTRUCT:
1307 printk("TIOCSERGSTRUCT\n");
1308 return 0;
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
1316 case TIOCMIWAIT:
1317 printk("TIOCMIWAIT\n");
1318 return 0;
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.
1326 case TIOCGICOUNT:
1327 printk("TIOCGICOUNT\n");
1328 return 0;
1330 case TIOCSERGWILD:
1331 case TIOCSERSWILD:
1332 /* "setserial -W" is called in Debian boot */
1333 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1334 return 0;
1336 default:
1337 return -ENOIOCTLCMD;
1339 return 0;
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)))
1351 return;
1353 change_speed(info, old_termios);
1355 /* Handle transition to B0 status */
1356 if ((old_termios->c_cflag & CBAUD) &&
1357 !(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) &&
1365 (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;
1378 rs_start(tty);
1383 * ------------------------------------------------------------
1384 * rs_close()
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"))
1399 return;
1401 state = info->state;
1403 save_flags(flags); cli();
1405 if (tty_hung_up_p(filp)) {
1406 restore_flags(flags);
1407 return;
1410 #ifdef SERIAL_DEBUG_OPEN
1411 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
1412 #endif
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);
1423 state->count = 1;
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);
1428 state->count = 0;
1430 if (state->count) {
1431 restore_flags(flags);
1432 return;
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.
1447 tty->closing = 1;
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) {
1458 #if 0
1459 sio_reg(info)->dicr &= ~TXx927_SIDICR_RIE;
1460 #endif
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);
1468 shutdown(info);
1469 if (tty->driver.flush_buffer)
1470 tty->driver.flush_buffer(tty);
1471 if (tty->ldisc.flush_buffer)
1472 tty->ldisc.flush_buffer(tty);
1473 tty->closing = 0;
1474 info->event = 0;
1475 info->tty = 0;
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|
1484 ASYNC_CLOSING);
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;
1496 int cisr;
1498 if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
1499 return;
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
1511 * the NIST-PCTS.
1513 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1514 char_time = char_time / 5;
1515 if (char_time == 0)
1516 char_time = 1;
1517 if (timeout)
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
1526 * 2*info->timeout.
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);
1533 #endif
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);
1537 #endif
1538 __set_current_state(TASK_INTERRUPTIBLE);
1539 schedule_timeout(char_time);
1540 if (signal_pending(current))
1541 break;
1542 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1543 break;
1545 current->state = TASK_RUNNING;
1546 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1547 printk("cisr = %d (jiff=%lu)...done\n", cisr, jiffies);
1548 #endif
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"))
1560 return;
1562 state = info->state;
1564 rs_flush_buffer(tty);
1565 shutdown(info);
1566 info->event = 0;
1567 state->count = 0;
1568 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1569 info->tty = 0;
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;
1583 int retval;
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);
1598 #else
1599 return -EAGAIN;
1600 #endif
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)
1609 return -EBUSY;
1610 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1611 (info->flags & ASYNC_SESSION_LOCKOUT) &&
1612 (info->session != current->session))
1613 return -EBUSY;
1614 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1615 (info->flags & ASYNC_PGRP_LOCKOUT) &&
1616 (info->pgrp != current->pgrp))
1617 return -EBUSY;
1618 info->flags |= ASYNC_CALLOUT_ACTIVE;
1619 return 0;
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)
1629 return -EBUSY;
1630 info->flags |= ASYNC_NORMAL_ACTIVE;
1631 return 0;
1634 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1635 if (state->normal_termios.c_cflag & CLOCAL)
1636 do_clocal = 1;
1637 } else {
1638 if (tty->termios->c_cflag & CLOCAL)
1639 do_clocal = 1;
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.
1649 retval = 0;
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);
1654 #endif
1655 save_flags(flags); cli();
1656 if (!tty_hung_up_p(filp)) {
1657 extra_count = 1;
1658 state->count--;
1660 restore_flags(flags);
1661 info->blocked_open++;
1662 while (1) {
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)
1673 retval = -EAGAIN;
1674 else
1675 retval = -ERESTARTSYS;
1676 #else
1677 retval = -EAGAIN;
1678 #endif
1679 break;
1681 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1682 !(info->flags & ASYNC_CLOSING))
1683 break;
1684 if (signal_pending(current)) {
1685 retval = -ERESTARTSYS;
1686 break;
1688 #ifdef SERIAL_DEBUG_OPEN
1689 printk("block_til_ready blocking: ttys%d, count = %d\n",
1690 info->line, state->count);
1691 #endif
1692 schedule();
1694 current->state = TASK_RUNNING;
1695 remove_wait_queue(&info->open_wait, &wait);
1696 if (extra_count)
1697 state->count++;
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);
1702 #endif
1703 if (retval)
1704 return retval;
1705 info->flags |= ASYNC_NORMAL_ACTIVE;
1706 return 0;
1709 static int get_async_struct(int line, struct async_struct **ret_info)
1711 struct async_struct *info;
1712 struct serial_state *sstate;
1714 #ifdef REMOTE_DEBUG
1715 if (kdb_port_info.state && line == kdb_port_info.line)
1716 return -ENODEV;
1717 #endif
1718 sstate = rs_table + line;
1719 sstate->count++;
1720 if (sstate->info) {
1721 *ret_info = sstate->info;
1722 return 0;
1724 info = kmalloc(sizeof(struct async_struct), GFP_KERNEL);
1725 if (!info) {
1726 sstate->count--;
1727 return -ENOMEM;
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;
1738 info->line = line;
1739 info->tqueue.routine = do_softint;
1740 info->tqueue.data = info;
1741 info->state = sstate;
1742 if (sstate->info) {
1743 kfree(info);
1744 *ret_info = sstate->info;
1745 return 0;
1747 *ret_info = sstate->info = info;
1748 return 0;
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;
1760 int retval, line;
1761 unsigned long page;
1763 line = minor(tty->device) - tty->driver.minor_start;
1764 if ((line < 0) || (line >= NR_PORTS)) {
1765 return -ENODEV;
1767 retval = get_async_struct(line, &info);
1768 if (retval) {
1769 return retval;
1771 tty->driver_data = info;
1772 info->tty = tty;
1774 #ifdef SERIAL_DEBUG_OPEN
1775 printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
1776 info->state->count);
1777 #endif
1778 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1780 if (!tmp_buf) {
1781 page = get_free_page(GFP_KERNEL);
1782 if (!page) {
1783 return -ENOMEM;
1785 if (tmp_buf)
1786 free_page(page);
1787 else
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);
1801 #else
1802 return -EAGAIN;
1803 #endif
1807 * Start up serial port
1809 retval = startup(info);
1810 if (retval) {
1811 return retval;
1814 retval = block_til_ready(tty, filp, info);
1815 if (retval) {
1816 #ifdef SERIAL_DEBUG_OPEN
1817 printk("rs_open returning after block_til_ready with %d\n",
1818 retval);
1819 #endif
1820 return retval;
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;
1827 else
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;
1834 sercons.cflag = 0;
1835 change_speed(info, 0);
1837 #endif
1838 info->session = current->session;
1839 info->pgrp = current->pgrp;
1841 #ifdef SERIAL_DEBUG_OPEN
1842 printk("rs_open ttys%d successful...", info->line);
1843 #endif
1844 return 0;
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;
1854 char stat_buf[30];
1855 int ret;
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);
1862 if (!state->port) {
1863 ret += sprintf(buf+ret, "\n");
1864 return ret;
1868 * Figure out the current RS-232 lines
1870 if (!info) {
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;
1876 info->quot = 0;
1877 info->tty = 0;
1880 stat_buf[0] = 0;
1881 stat_buf[1] = 0;
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);
1889 if (info->quot) {
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);
1913 return ret;
1916 static int rs_read_proc(char *page, char **start, off_t off, int count,
1917 int *eof, void *data)
1919 int i, len = 0, l;
1920 off_t begin = 0;
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]);
1925 len += l;
1926 if (len+begin > off+count)
1927 goto done;
1928 if (len+begin < off) {
1929 begin += len;
1930 len = 0;
1933 *eof = 1;
1934 done:
1935 if (off >= len+begin)
1936 return 0;
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
1952 * driver.
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)
1964 int i;
1965 struct serial_state * state;
1967 if (rs_table[0].port == 0)
1968 return -ENODEV;
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++) {
1976 IRQ_ports[i] = 0;
1977 IRQ_timeout[i] = 0;
1979 #ifdef CONFIG_TXX927_SERIAL_CONSOLE
1981 * The interrupt of the serial console port
1982 * can't be shared.
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;
1990 #endif
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";
2000 #else
2001 serial_driver.name = "ttyS";
2002 #endif
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";
2044 #else
2045 callout_driver.name = "cua";
2046 #endif
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;
2061 state->line = i;
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;
2075 if (state->hub6)
2076 state->io_type = SERIAL_IO_HUB6;
2077 if (state->port) {
2078 continue;
2082 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
2083 if (state->type == PORT_UNKNOWN) {
2084 continue;
2086 printk(KERN_INFO "%s%02d at 0x%04lx (irq = %d) is a %s\n",
2087 TXX927_TTY_NAME,
2088 state->line,
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);
2096 return 0;
2099 static void __exit rs_fini(void)
2101 unsigned long flags;
2102 int e1, e2;
2103 int i;
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",
2111 e1);
2112 if ((e2 = tty_unregister_driver(&callout_driver)))
2113 printk(KERN_WARNING "serial: failed to unregister callout driver (%d)\n",
2114 e2);
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;
2120 kfree(info);
2123 if (tmp_buf) {
2124 unsigned long pg = (unsigned long) tmp_buf;
2125 tmp_buf = NULL;
2126 free_page(pg);
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,
2148 unsigned count)
2150 static struct async_struct *info = &async_sercons;
2151 int ier;
2152 unsigned i;
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;
2172 if (*s == 10) {
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;
2201 unsigned cval;
2202 int baud = 9600;
2203 int bits = 8;
2204 int parity = 'n';
2205 int cflag = CREAD | HUPCL | CLOCAL;
2206 int quot = 0;
2207 char *s;
2209 if (co->index < 0 || co->index >= NR_PORTS) {
2210 return -1;
2212 if (options) {
2213 baud = simple_strtoul(options, NULL, 10);
2214 s = options;
2215 while(*s >= '0' && *s <= '9')
2216 s++;
2217 if (*s) parity = *s++;
2218 if (*s) bits = *s - '0';
2222 * Now construct a cflag setting.
2224 switch(baud) {
2225 case 1200:
2226 cflag |= B1200;
2227 break;
2228 case 2400:
2229 cflag |= B2400;
2230 break;
2231 case 4800:
2232 cflag |= B4800;
2233 break;
2234 case 19200:
2235 cflag |= B19200;
2236 break;
2237 case 38400:
2238 cflag |= B38400;
2239 break;
2240 case 57600:
2241 cflag |= B57600;
2242 break;
2243 case 115200:
2244 cflag |= B115200;
2245 break;
2246 case 9600:
2247 default:
2248 cflag |= B9600;
2249 break;
2251 switch(bits) {
2252 case 7:
2253 cflag |= CS7;
2254 break;
2255 default:
2256 case 8:
2257 cflag |= CS8;
2258 break;
2260 switch(parity) {
2261 case 'o': case 'O':
2262 cflag |= PARODD;
2263 break;
2264 case 'e': case 'E':
2265 cflag |= PARENB;
2266 break;
2268 co->cflag = cflag;
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;
2286 default:
2287 case CS8: cval = TXx927_SILCR_UMODE_8BIT; break;
2289 if (cflag & CSTOPB)
2290 cval |= TXx927_SILCR_USBL_2BIT;
2291 else
2292 cval |= TXx927_SILCR_USBL_1BIT;
2293 if (cflag & PARENB)
2294 cval |= TXx927_SILCR_UPEN;
2295 if (!(cflag & PARODD))
2296 cval |= TXx927_SILCR_UEPS;
2299 * Disable UART interrupts, set DTR and RTS high
2300 * and set speed.
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 */;
2309 /* Enable RX/TX */
2310 sio_reg(info)->flcr &= ~(TXx927_SIFLCR_RSDE | TXx927_SIFLCR_TSDE);
2312 return 0;
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,
2321 index: -1,
2325 * Register console.
2327 void __init txx927_console_init(void)
2329 register_console(&sercons);
2331 #endif