MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / char / serial_cpe.c
blobf8c996c0f2319e30f3428fb4076e98bb3c8e32ac
1 // --------------------------------------------------------------------
2 // 2002-11-26: lmc83: modified from serial.c
3 // --------------------------------------------------------------------
4 extern void cpe_int_set_irq(unsigned int,int,int);
5 static char *serial_version = "5.05c";
6 static char *serial_revdate = "2001-07-08";
8 #include <linux/config.h>
9 #include <linux/version.h>
10 #include <asm/arch/cpe/cpe.h>
11 #include <asm/arch/cpe_int.h>
12 #include "serial_cpe.h"
14 #undef SERIAL_PARANOIA_CHECK
15 #define CONFIG_SERIAL_NOPAUSE_IO
16 #define SERIAL_DO_RESTART
18 #ifdef CONFIG_SERIAL_ACPI
19 #define ENABLE_SERIAL_ACPI
20 #endif
22 /* Set of debugging defines */
24 #undef SERIAL_DEBUG_INTR
25 //#define SERIAL_DEBUG_OPEN
26 #undef SERIAL_DEBUG_OPEN
27 #define SERIAL_DEBUG_FLOW
28 //#undef SERIAL_DEBUG_FLOW
29 #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
30 #undef SERIAL_DEBUG_PCI
31 #undef SERIAL_DEBUG_AUTOCONF
34 #ifdef MODULE
35 #undef CONFIG_SERIAL_CPE_CONSOLE
36 #endif
39 #define RS_STROBE_TIME (10*HZ)
40 //#define RS_STROBE_TIME HZ
41 #define RS_ISR_PASS_LIMIT 256
45 * End of serial driver configuration section.
48 #include <linux/module.h>
50 #include <linux/types.h>
51 #ifdef LOCAL_HEADERS
52 #include "serial_local.h"
53 #else
54 #include <linux/serial.h>
55 #include <linux/serialP.h>
56 #include <linux/serial_reg.h>
57 #include <asm/serial.h>
58 #define LOCAL_VERSTRING ""
59 #endif
61 #include <linux/errno.h>
62 #include <linux/signal.h>
63 #include <linux/sched.h>
64 #include <linux/timer.h>
65 #include <linux/interrupt.h>
66 #include <linux/tty.h>
67 #include <linux/tty_flip.h>
68 #include <linux/major.h>
69 #include <linux/string.h>
70 #include <linux/fcntl.h>
71 #include <linux/ptrace.h>
72 #include <linux/ioport.h>
73 #include <linux/mm.h>
74 #include <linux/slab.h>
75 #if (LINUX_VERSION_CODE >= 131343)
76 #include <linux/init.h>
77 #endif
78 #if (LINUX_VERSION_CODE >= 131336)
79 #include <asm/uaccess.h>
80 #endif
81 #include <linux/delay.h>
82 #ifdef CONFIG_SERIAL_CPE_CONSOLE
83 #include <linux/console.h>
84 #endif
85 #ifdef CONFIG_MAGIC_SYSRQ
86 #include <linux/sysrq.h>
87 #endif
90 * All of the compatibilty code so we can compile serial.c against
91 * older kernels is hidden in serial_compat.h
93 #if defined(LOCAL_HEADERS) || (LINUX_VERSION_CODE < 0x020317) /* 2.3.23 */
94 #include "serial_compat.h"
95 #endif
97 #include <asm/system.h>
98 #include <asm/io.h>
99 #include <asm/irq.h>
100 #include <asm/bitops.h>
102 #ifdef CONFIG_MAC_SERIAL
103 #define SERIAL_DEV_OFFSET 2
104 #else
105 #define SERIAL_DEV_OFFSET 0
106 #endif
108 #ifdef SERIAL_INLINE
109 #define _INLINE_ inline
110 #else
111 #define _INLINE_
112 #endif
114 static char *serial_name = "Serial driver";
116 #if 0 // mask by Victor Yu. 05-19-2005
117 static DECLARE_TASK_QUEUE(tq_serial);
118 #endif
120 static struct tty_driver serial_driver, callout_driver;
121 static int serial_refcount;
123 static struct timer_list serial_timer;
125 //ivan
126 static int show_once=1;
128 /* serial subtype definitions */
129 #ifndef SERIAL_TYPE_NORMAL
130 #define SERIAL_TYPE_NORMAL 1
131 #define SERIAL_TYPE_CALLOUT 2
132 #endif
134 /* number of characters left in xmit buffer before we ask for more */
135 #define WAKEUP_CHARS 256
138 * IRQ_timeout - How long the timeout should be for each IRQ
139 * should be after the IRQ has been active.
142 static struct async_struct *IRQ_ports[NR_IRQS];
143 static int IRQ_timeout[NR_IRQS];
144 #ifdef CONFIG_SERIAL_CPE_CONSOLE
145 static struct console sercons;
146 static int lsr_break_flag;
147 #endif
148 #if defined(CONFIG_SERIAL_CPE_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
149 static unsigned long break_pressed; /* break, really ... */
150 #endif
152 static unsigned detect_uart_irq (struct serial_state * state);
153 static void autoconfig(struct serial_state * state);
154 static void change_speed(struct async_struct *info, struct termios *old);
155 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
158 * Here we define the default xmit fifo size used for each type of
159 * UART
161 // name, fifosize, flags
162 static struct serial_uart_config uart_config[] = {
163 { "unknown", 1, 0 },
164 { "8250", 1, 0 },
165 { "16450", 1, 0 },
166 { "16550", 1, 0 },
167 { "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
168 { "cirrus", 1, 0 }, /* usurped by cyclades.c */
169 { "ST16650", 1, UART_CLEAR_FIFO | UART_STARTECH },
170 { "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO |
171 UART_STARTECH },
172 { "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO},
173 { "16C950/954", 128, UART_CLEAR_FIFO | UART_USE_FIFO},
174 { "ST16654", 64, UART_CLEAR_FIFO | UART_USE_FIFO |
175 UART_STARTECH },
176 { "XR16850", 128, UART_CLEAR_FIFO | UART_USE_FIFO |
177 UART_STARTECH },
178 { "RSA", 2048, UART_CLEAR_FIFO | UART_USE_FIFO },
179 //{ "FaradayUART", 1, UART_CLEAR_FIFO},
180 { "FaradayUART", 16, UART_CLEAR_FIFO|UART_USE_FIFO },
181 { 0, 0}
186 static struct serial_state rs_table[RS_TABLE_SIZE] = {
187 SERIAL_PORT_DFNS
190 #define NR_PORTS (sizeof(rs_table)/sizeof(struct serial_state))
193 #ifndef PREPARE_FUNC
194 #define PREPARE_FUNC(dev) (dev->prepare)
195 #define ACTIVATE_FUNC(dev) (dev->activate)
196 #define DEACTIVATE_FUNC(dev) (dev->deactivate)
197 #endif
199 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
201 static struct tty_struct *serial_table[NR_PORTS];
202 static struct termios *serial_termios[NR_PORTS];
203 static struct termios *serial_termios_locked[NR_PORTS];
206 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
207 #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
208 kdevname(tty->device), (info->flags), serial_refcount,info->count,tty->count,s)
209 #else
210 #define DBG_CNT(s)
211 #endif
214 * tmp_buf is used as a temporary buffer by serial_write. We need to
215 * lock it in case the copy_from_user blocks while swapping in a page,
216 * and some other program tries to do a serial write at the same time.
217 * Since the lock will only come under contention when the system is
218 * swapping and available memory is low, it makes sense to share one
219 * buffer across all the serial ports, since it significantly saves
220 * memory if large numbers of serial ports are open.
222 static unsigned char *tmp_buf;
223 #ifdef DECLARE_MUTEX
224 static DECLARE_MUTEX(tmp_buf_sem);
225 #else
226 static struct semaphore tmp_buf_sem = MUTEX;
227 #endif
229 #if 1 // mask by Victor Yu. 05-19-2005
230 #ifdef not_complete_yet
231 static inline void wait_for_xmitr(struct async_struct *info);
232 #endif // not_complete_yet
233 #endif
235 #if 0 // mask by Victor Yu. 05-18-2005
236 static inline int serial_paranoia_check(struct async_struct *info,
237 kdev_t device, const char *routine)
238 #else // add by Victor Yu. 05-18-2005
239 static inline int serial_paranoia_check(struct async_struct *info, char *name, const char *routine)
240 #endif
242 #ifdef SERIAL_PARANOIA_CHECK
243 static const char *badmagic =
244 "Warning: bad magic number for serial struct (%s) in %s\n";
245 static const char *badinfo =
246 "Warning: null async_struct for (%s) in %s\n";
248 if (!info) {
249 #if 0 // mask by Victor Yu. 05-18-2005
250 printk(badinfo, kdevname(device), routine);
251 #else // add by Victor Yu. 05-18-2005
252 printk(badinfo, name, routine);
253 #endif
254 return 1;
256 if (info->magic != SERIAL_MAGIC) {
257 #if 0 // mask by Victor Yu. 05-18-2005
258 printk(badmagic, kdevname(device), routine);
259 #else // add by Victor Yu. 05-18-2005
260 printk(badmagic, name, routine);
261 #endif
262 return 1;
264 #endif
265 return 0;
268 static _INLINE_ unsigned int serial_in(struct async_struct *info, int offset)
270 return inb(info->port + offset);
273 static _INLINE_ void serial_out(struct async_struct *info, int offset,
274 int value)
276 outb(value, info->port+offset);
280 * We used to support using pause I/O for certain machines. We
281 * haven't supported this for a while, but just in case it's badly
282 * needed for certain old 386 machines, I've left these #define's
283 * in....
285 #define serial_inp(info, offset) serial_in(info, offset)
286 #define serial_outp(info, offset, value) serial_out(info, offset, value)
290 * For the 16C950
292 void serial_icr_write(struct async_struct *info, int offset, int value)
294 serial_out(info, UART_SCR, offset);
295 serial_out(info, UART_ICR, value);
298 unsigned int serial_icr_read(struct async_struct *info, int offset)
300 int value;
302 serial_icr_write(info, UART_ACR, info->ACR | UART_ACR_ICRRD);
303 serial_out(info, UART_SCR, offset);
304 value = serial_in(info, UART_ICR);
305 serial_icr_write(info, UART_ACR, info->ACR);
306 return value;
310 * ------------------------------------------------------------
311 * rs_stop() and rs_start()
313 * This routines are called before setting or resetting tty->stopped.
314 * They enable or disable transmitter interrupts, as necessary.
315 * ------------------------------------------------------------
317 static void rs_stop(struct tty_struct *tty)
319 struct async_struct *info = (struct async_struct *)tty->driver_data;
320 unsigned long flags;
322 #if 0 // mask by Victor Yu. 05-18-2005
323 if (serial_paranoia_check(info, tty->device, "rs_stop"))
324 #else // add by Victor Yu. 05-18-2005
325 if (serial_paranoia_check(info, tty->name, "rs_stop"))
326 #endif
327 return;
329 save_flags(flags); cli();
330 if (info->IER & SERIAL_IER_TE) {
331 info->IER &= ~SERIAL_IER_TE;
332 serial_out(info, SERIAL_IER, info->IER);
334 if (info->state->type == PORT_16C950) {
335 info->ACR |= UART_ACR_TXDIS;
336 serial_icr_write(info, UART_ACR, info->ACR);
338 restore_flags(flags);
341 static void rs_start(struct tty_struct *tty)
343 struct async_struct *info = (struct async_struct *)tty->driver_data;
344 unsigned long flags;
346 #if 0 // mask by Victor Yu. 05-18-2005
347 if (serial_paranoia_check(info, tty->device, "rs_start"))
348 #else // add by Victor Yu. 05-18-2005
349 if (serial_paranoia_check(info, tty->name, "rs_start"))
350 #endif
351 return;
353 save_flags(flags); cli();
354 if (info->xmit.head != info->xmit.tail
355 && info->xmit.buf
356 && !(info->IER & SERIAL_IER_TE)) {
357 info->IER |= SERIAL_IER_TE;
358 serial_out(info, SERIAL_IER, info->IER);
360 if (info->state->type == PORT_16C950) {
361 info->ACR &= ~UART_ACR_TXDIS;
362 serial_icr_write(info, UART_ACR, info->ACR);
364 restore_flags(flags);
368 * ----------------------------------------------------------------------
370 * Here starts the interrupt handling routines. All of the following
371 * subroutines are declared as inline and are folded into
372 * rs_interrupt(). They were separated out for readability's sake.
374 * Note: rs_interrupt() is a "fast" interrupt, which means that it
375 * runs with interrupts turned off. People who may want to modify
376 * rs_interrupt() should try to keep the interrupt handler as fast as
377 * possible. After you are done making modifications, it is not a bad
378 * idea to do:
380 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
382 * and look at the resulting assemble code in serial.s.
384 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
385 * -----------------------------------------------------------------------
389 * This routine is used by the interrupt handler to schedule
390 * processing in the software interrupt portion of the driver.
392 #if 0 // mask by Victor Yu. 05-19-2005
393 static _INLINE_ void rs_sched_event(struct async_struct *info,
394 int event)
396 info->event |= 1 << event;
397 queue_task(&info->tqueue, &tq_serial);
398 mark_bh(SERIAL_BH);
400 #endif
402 static _INLINE_ void receive_chars(struct async_struct *info,
403 int *status, struct pt_regs * regs)
405 struct tty_struct *tty = info->tty;
406 unsigned char ch;
407 struct async_icount *icount;
408 int max_count = 256;
411 icount = &info->state->icount;
412 do {
413 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
414 #if 0 // mask by Victor Yu. 05-19-2005
415 tty->flip.tqueue.routine((void *) tty);
416 #endif
417 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
418 return; // if TTY_DONT_FLIP is set
420 ch = serial_inp(info, SERIAL_RBR);
421 *tty->flip.char_buf_ptr = ch;
422 icount->rx++;
424 #ifdef SERIAL_DEBUG_INTR
425 printk("DR%02x:%02x...", ch, *status);
426 #endif
427 *tty->flip.flag_buf_ptr = 0;
428 if (*status & (SERIAL_LSR_BI | SERIAL_LSR_PE |
429 SERIAL_LSR_FE | SERIAL_LSR_OE)) {
431 * For statistics only
433 if (*status & SERIAL_LSR_BI) {
434 *status &= ~(SERIAL_LSR_FE | SERIAL_LSR_PE);
435 icount->brk++;
437 * We do the SysRQ and SAK checking
438 * here because otherwise the break
439 * may get masked by ignore_status_mask
440 * or read_status_mask.
442 #if defined(CONFIG_SERIAL_CPE_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
443 if (info->line == sercons.index) {
444 if (!break_pressed) {
445 break_pressed = jiffies;
446 goto ignore_char;
448 break_pressed = 0;
450 #endif
451 if (info->flags & ASYNC_SAK)
452 do_SAK(tty);
453 } else if (*status & SERIAL_LSR_PE)
454 icount->parity++;
455 else if (*status & SERIAL_LSR_FE)
456 icount->frame++;
457 if (*status & SERIAL_LSR_OE)
458 icount->overrun++;
461 * Mask off conditions which should be ignored.
463 *status &= info->read_status_mask;
465 #ifdef CONFIG_SERIAL_CPE_CONSOLE
466 if (info->line == sercons.index) {
467 /* Recover the break flag from console xmit */
468 *status |= lsr_break_flag;
469 lsr_break_flag = 0;
471 #endif
472 if (*status & (SERIAL_LSR_BI)) {
473 #ifdef SERIAL_DEBUG_INTR
474 printk("handling break....");
475 #endif
476 *tty->flip.flag_buf_ptr = TTY_BREAK;
477 } else if (*status & SERIAL_LSR_PE)
478 *tty->flip.flag_buf_ptr = TTY_PARITY;
479 else if (*status & SERIAL_LSR_FE)
480 *tty->flip.flag_buf_ptr = TTY_FRAME;
482 #if defined(CONFIG_SERIAL_CPE_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
483 if (break_pressed && info->line == sercons.index) {
484 if (ch != 0 &&
485 time_before(jiffies, break_pressed + HZ*5)) {
486 handle_sysrq(ch, regs, NULL, NULL);
487 break_pressed = 0;
488 goto ignore_char;
490 break_pressed = 0;
492 #endif
493 if ((*status & info->ignore_status_mask) == 0) {
494 tty->flip.flag_buf_ptr++;
495 tty->flip.char_buf_ptr++;
496 tty->flip.count++;
498 if ((*status & SERIAL_LSR_OE) &&
499 (tty->flip.count < TTY_FLIPBUF_SIZE)) {
501 * Overrun is special, since it's reported
502 * immediately, and doesn't affect the current
503 * character
505 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
506 tty->flip.count++;
507 tty->flip.flag_buf_ptr++;
508 tty->flip.char_buf_ptr++;
510 #if defined(CONFIG_SERIAL_CPE_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
511 ignore_char:
512 #endif
513 *status = serial_inp(info, SERIAL_LSR);
514 } while ((*status & SERIAL_LSR_DR) && (max_count-- > 0));
515 #if (LINUX_VERSION_CODE > 131394) /* 2.1.66 */
516 tty_flip_buffer_push(tty);
517 #else
518 queue_task_irq_off(&tty->flip.tqueue, &tq_timer);
519 #endif
522 static _INLINE_ void transmit_chars(struct async_struct *info, int *intr_done)
524 int count;
526 if (info->x_char)
528 serial_outp(info, SERIAL_THR, info->x_char);
529 info->state->icount.tx++;
530 info->x_char = 0;
531 if (intr_done)
532 *intr_done = 0;
533 return;
535 if (info->xmit.head == info->xmit.tail
536 || info->tty->stopped
537 || info->tty->hw_stopped)
539 info->IER &= ~SERIAL_IER_TE;
540 serial_out(info, SERIAL_IER, info->IER);
541 return;
545 #ifdef not_complete_yet
546 for (; info->xmit.tail != info->xmit.head; )
548 wait_for_xmitr(info);
549 serial_out(info, SERIAL_THR, info->xmit.buf[info->xmit.tail]);
550 info->xmit.tail = (info->xmit.tail + 1) & (SERIAL_XMIT_SIZE-1);
551 info->state->icount.tx++;
553 #endif /* end_of_not */
554 count = info->xmit_fifo_size;
555 do {
556 serial_out(info, SERIAL_THR, info->xmit.buf[info->xmit.tail]);
557 info->xmit.tail = (info->xmit.tail + 1) & (SERIAL_XMIT_SIZE-1);
558 info->state->icount.tx++;
559 if (info->xmit.head == info->xmit.tail)
560 break;
561 } while (--count > 0);
563 if (CIRC_CNT(info->xmit.head,
564 info->xmit.tail,
565 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
567 #if 0 // mask by Victor Yu. 05-19-2005
568 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
569 #endif
572 #ifdef SERIAL_DEBUG_INTR
573 printk("THRE...");
574 #endif
575 if (intr_done)
576 *intr_done = 0;
578 if (info->xmit.head == info->xmit.tail) {
579 info->IER &= ~SERIAL_IER_TE;
580 serial_out(info, SERIAL_IER, info->IER);
584 static _INLINE_ void check_modem_status(struct async_struct *info)
586 int status;
587 struct async_icount *icount;
589 status = serial_in(info, SERIAL_MSR);
591 if (status & SERIAL_MSR_ANY_DELTA) {
592 icount = &info->state->icount;
593 /* update input line counters */
594 if (status & SERIAL_MSR_TERI)
595 icount->rng++;
596 if (status & SERIAL_MSR_DELTADSR)
597 icount->dsr++;
598 if (status & SERIAL_MSR_DELTACD) {
599 icount->dcd++;
600 #ifdef CONFIG_HARD_PPS
601 if ((info->flags & ASYNC_HARDPPS_CD) &&
602 (status & SERIAL_MSR_DCD))
603 hardpps();
604 #endif
606 if (status & SERIAL_MSR_DELTACTS)
607 icount->cts++;
608 wake_up_interruptible(&info->delta_msr_wait);
611 if ((info->flags & ASYNC_CHECK_CD) && (status & SERIAL_MSR_DELTACD)) {
612 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
613 printk("ttys%d CD now %s...", info->line,
614 (status & SERIAL_MSR_DCD) ? "on" : "off");
615 #endif
616 #if 0 // mask by Victor Yu. 05-19-2005
617 if (status & SERIAL_MSR_DCD)
618 wake_up_interruptible(&info->open_wait);
619 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
620 (info->flags & ASYNC_CALLOUT_NOHUP))) {
621 #ifdef SERIAL_DEBUG_OPEN
622 printk("doing serial hangup...");
623 #endif
624 if (info->tty)
625 tty_hangup(info->tty);
627 #else // add by Victor Yu. 05-19-2005
628 if ( status & SERIAL_MSR_DCD ) {
629 wake_up_interruptible(&info->open_wait);
630 if ( info->tty )
631 tty_hangup(info->tty);
633 #endif
635 if (info->flags & ASYNC_CTS_FLOW) {
636 if (info->tty->hw_stopped) {
637 if (status & SERIAL_MSR_CTS) {
638 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
639 printk("CTS tx start...");
640 #endif
641 info->tty->hw_stopped = 0;
642 info->IER |= SERIAL_IER_TE;
643 serial_out(info, SERIAL_IER, info->IER);
644 #if 0 // mask by Victor Yu. 05-19-2005
645 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
646 #endif
647 return;
649 } else {
650 if (!(status & SERIAL_MSR_CTS)) {
651 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
652 printk("CTS tx stop...");
653 #endif
654 info->tty->hw_stopped = 1;
655 info->IER &= ~SERIAL_IER_TE;
656 serial_out(info, SERIAL_IER, info->IER);
662 #ifdef not_complete_yet
663 #ifdef CONFIG_SERIAL_SHARE_IRQ
665 * This is the serial driver's generic interrupt routine
667 #if 0 // mask by Victor Yu. 05-19-2005
668 static void rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
669 #else // add by Victor Yu. 05-19-2005
670 static irqreturn_t rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
671 #endif
673 int status;
674 struct async_struct * info;
675 int pass_counter = 0;
676 struct async_struct *end_mark = 0;
678 #ifdef SERIAL_DEBUG_INTR
679 printk("rs_interrupt(%d)...", irq);
680 #endif
682 info = IRQ_ports[irq];
683 if (!info)
684 #if 0 // mask by Victor Yu. 05-19-2005
685 return;
686 #else // add by Victor Yu. 05-19-2005
687 return IRQ_NONE;
688 #endif
690 do {
691 if (!info->tty ||
692 (serial_in(info, SERIAL_IIR) & SERIAL_IIR_NONE)) {
693 if (!end_mark)
694 end_mark = info;
695 goto next;
697 #ifdef SERIAL_DEBUG_INTR
698 printk("IIR = %x...", serial_in(info, SERIAL_IIR));
699 #endif
700 end_mark = 0;
702 info->last_active = jiffies;
704 status = serial_inp(info, SERIAL_LSR);
705 #ifdef SERIAL_DEBUG_INTR
706 printk("status = %x...", status);
707 #endif
708 if (status & SERIAL_LSR_DR)
709 receive_chars(info, &status, regs);
710 check_modem_status(info);
711 if (status & SERIAL_LSR_THRE)
712 transmit_chars(info, 0);
714 next:
715 info = info->next_port;
716 if (!info) {
717 info = IRQ_ports[irq];
718 if (pass_counter++ > RS_ISR_PASS_LIMIT) {
719 #if 0
720 printk("rs loop break\n");
721 #endif
722 break; /* Prevent infinite loops */
724 continue;
726 } while (end_mark != info);
728 #ifdef SERIAL_DEBUG_INTR
729 printk("end.\n");
730 #endif
731 #if 1 // add by Victor Yu. 05-19-2005
732 return IRQ_RETVAL(1);
733 #endif
735 #endif /* #ifdef CONFIG_SERIAL_SHARE_IRQ */
736 #endif /* end_of_not */
739 * This is the serial driver's interrupt routine for a single port
741 #if 0 // mask by Victor Yu. 05-19-2005
742 static void rs_interrupt_single(int irq, void *dev_id, struct pt_regs * regs)
743 #else // add by Victor Yu. 05-19-2005
744 static irqreturn_t rs_interrupt_single(int irq, void *dev_id, struct pt_regs * regs)
745 #endif
747 int status;
748 int pass_counter = 0;
749 struct async_struct * info;
751 #ifdef SERIAL_DEBUG_INTR
752 printk("rs_interrupt_single(%d)...", irq);
753 #endif
755 info = IRQ_ports[irq];
756 if (!info || !info->tty)
757 #if 0 // mask by Vicor Yu. 05-19-2005
758 return;
759 #else // add by Victor Yu. 05-19-2005
760 return IRQ_NONE;
761 #endif
764 do {
765 status = serial_inp(info, SERIAL_LSR);
766 #ifdef SERIAL_DEBUG_INTR
767 printk("status = %x...", status);
768 #endif
769 if (status & SERIAL_LSR_DR)
770 receive_chars(info, &status, regs);
771 check_modem_status(info);
772 if (status & SERIAL_LSR_THRE)
773 transmit_chars(info, 0);
774 if (pass_counter++ > RS_ISR_PASS_LIMIT) {
775 #if 0
776 printk("rs_single loop break.\n");
777 #endif
778 break;
780 #ifdef SERIAL_DEBUG_INTR
781 printk("IIR = %x...", serial_in(info, SERIAL_IIR));
782 #endif
783 } while (!(serial_in(info, SERIAL_IIR) & SERIAL_IIR_NONE));
784 info->last_active = jiffies;
786 #ifdef SERIAL_DEBUG_INTR
787 printk("end.\n");
788 #endif
789 #if 1 // add by Victor Yu. 05-19-2005
790 return IRQ_RETVAL(1);
791 #endif
797 * -------------------------------------------------------------------
798 * Here ends the serial interrupt routines.
799 * -------------------------------------------------------------------
803 * This routine is used to handle the "bottom half" processing for the
804 * serial driver, known also the "software interrupt" processing.
805 * This processing is done at the kernel interrupt level, after the
806 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
807 * is where time-consuming activities which can not be done in the
808 * interrupt driver proper are done; the interrupt driver schedules
809 * them using rs_sched_event(), and they get done here.
811 #if 0 // mask by Victor Yu. 05-19-2005
812 static void do_serial_bh(void)
814 //printk("do_serial_bh\r\n");
815 run_task_queue(&tq_serial);
817 #endif
819 static void do_softint(void *private_)
821 struct async_struct *info = (struct async_struct *) private_;
822 struct tty_struct *tty;
823 //printk("do_softint\r\n");
824 tty = info->tty;
825 if (!tty)
826 return;
828 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event))
830 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
831 tty->ldisc.write_wakeup)
832 (tty->ldisc.write_wakeup)(tty);
833 wake_up_interruptible(&tty->write_wait);
834 #ifdef SERIAL_HAVE_POLL_WAIT
835 wake_up_interruptible(&tty->poll_wait);
836 #endif
842 * This subroutine is called when the RS_TIMER goes off. It is used
843 * by the serial driver to handle ports that do not have an interrupt
844 * (irq=0). This doesn't work very well for 16450's, but gives barely
845 * passable results for a 16550A. (Although at the expense of much
846 * CPU overhead).
848 static void rs_timer(unsigned long dummy)
850 static unsigned long last_strobe;
851 struct async_struct *info;
852 unsigned int i;
853 unsigned long flags;
854 //printk("rs_timer\r\n");
855 if ((jiffies - last_strobe) >= RS_STROBE_TIME)
857 for (i=0; i < NR_IRQS; i++)
859 info = IRQ_ports[i];
860 if (!info)
861 continue;
862 save_flags(flags); cli();
863 rs_interrupt_single(i, NULL, NULL);
864 restore_flags(flags);
867 last_strobe = jiffies;
868 mod_timer(&serial_timer, jiffies + RS_STROBE_TIME);
870 if (IRQ_ports[0])
872 save_flags(flags); cli();
873 rs_interrupt_single(0, NULL, NULL);
874 restore_flags(flags);
875 mod_timer(&serial_timer, jiffies + IRQ_timeout[0]);
880 * ---------------------------------------------------------------
881 * Low level utility subroutines for the serial driver: routines to
882 * figure out the appropriate timeout for an interrupt chain, routines
883 * to initialize and startup a serial port, and routines to shutdown a
884 * serial port. Useful stuff like that.
885 * ---------------------------------------------------------------
889 * This routine figures out the correct timeout for a particular IRQ.
890 * It uses the smallest timeout of all of the serial ports in a
891 * particular interrupt chain. Now only used for IRQ 0....
893 static void figure_IRQ_timeout(int irq)
895 struct async_struct *info;
896 int timeout = 60*HZ; /* 60 seconds === a long time :-) */
898 info = IRQ_ports[irq];
899 if (!info) {
900 IRQ_timeout[irq] = 60*HZ;
901 return;
903 while (info) {
904 if (info->timeout < timeout)
905 timeout = info->timeout;
906 info = info->next_port;
908 if (!irq)
909 timeout = timeout / 2;
910 IRQ_timeout[irq] = (timeout > 3) ? timeout-2 : 1;
914 static int startup(struct async_struct * info)
916 unsigned long flags;
917 int retval=0;
918 #if 0 // mask by Victor Yu. 05-19-2005
919 void (*handler)(int, void *, struct pt_regs *);
920 #else // add by Victor Yu. 05-19-2005
921 irqreturn_t (*handler)(int, void *, struct pt_regs *);
922 #endif
923 struct serial_state *state= info->state;
924 unsigned long page;
925 //printk("startup\r\n");
926 page = get_zeroed_page(GFP_KERNEL);
927 if (!page)
928 return -ENOMEM;
930 save_flags(flags); cli();
932 if (info->flags & ASYNC_INITIALIZED) {
933 free_page(page);
934 goto errout;
937 if (!CONFIGURED_SERIAL_PORT(state) || !state->type)
939 if (info->tty)
940 set_bit(TTY_IO_ERROR, &info->tty->flags);
941 free_page(page);
942 goto errout;
944 if (info->xmit.buf)
945 free_page(page);
946 else
947 info->xmit.buf = (unsigned char *) page;
949 #ifdef SERIAL_DEBUG_OPEN
950 printk("starting up ttys%d (irq %d)...", info->line, state->irq);
951 #endif
956 * Clear the FIFO buffers and disable them
957 * (they will be reenabled in change_speed())
959 if (uart_config[state->type].flags & UART_CLEAR_FIFO) {
960 serial_outp(info, SERIAL_FCR, SERIAL_FCR_FE);
961 serial_outp(info, SERIAL_FCR, (SERIAL_FCR_FE |
962 SERIAL_FCR_RXFR |
963 SERIAL_FCR_TXFR));
964 serial_outp(info, SERIAL_FCR, 0);
968 * Clear the interrupt registers.
970 (void) serial_inp(info, SERIAL_LSR);
971 (void) serial_inp(info, SERIAL_RBR);
972 (void) serial_inp(info, SERIAL_IIR);
973 (void) serial_inp(info, SERIAL_MSR);
976 * At this point there's no way the LSR could still be 0xFF;
977 * if it is, then bail out, because there's likely no UART
978 * here.
980 if (!(info->flags & ASYNC_BUGGY_UART) && (serial_inp(info, SERIAL_LSR) == 0xff))
982 printk("ttyS%d: LSR safety check engaged!\n", state->line);
983 if (capable(CAP_SYS_ADMIN)) {
984 if (info->tty)
985 set_bit(TTY_IO_ERROR, &info->tty->flags);
986 } else
987 retval = -ENODEV;
988 goto errout;
992 * Allocate the IRQ if necessary
994 if (state->irq && (!IRQ_ports[state->irq] || !IRQ_ports[state->irq]->next_port))
996 if (IRQ_ports[state->irq])
998 #ifdef CONFIG_SERIAL_SHARE_IRQ
999 free_irq(state->irq, &IRQ_ports[state->irq]);
1000 handler = rs_interrupt;
1001 #else
1002 retval = -EBUSY;
1003 goto errout;
1004 #endif /* CONFIG_SERIAL_SHARE_IRQ */
1006 else
1008 handler = rs_interrupt_single;
1010 // printk("IRQ_ports = %x\n", IRQ_ports);
1011 cpe_int_set_irq(state->irq, LEVEL, H_ACTIVE);
1012 if(state->irq <32) //irq
1014 if(show_once++==1)
1015 printk("IRQ UART at interrupt number 0x%x\r\n",state->irq);
1016 #if 0 // mask by Victor Yu. 05-19-2005
1017 retval = request_irq(state->irq, handler, 0,
1018 "serial", &IRQ_ports[state->irq]);
1019 #else // add by Victor Yu. 05-19-2005
1020 retval = request_irq(state->irq, handler, SA_INTERRUPT,
1021 "serial", &IRQ_ports[state->irq]);
1022 #endif
1024 else
1026 if(show_once++==1)
1027 printk("FIQ UART at interrupt number 0x%x\r\n",state->irq);
1028 retval = request_irq(state->irq, handler, SA_INTERRUPT,
1029 "serial", &IRQ_ports[state->irq]);
1032 if (retval) {
1033 if (capable(CAP_SYS_ADMIN)) {
1034 if (info->tty)
1035 set_bit(TTY_IO_ERROR,
1036 &info->tty->flags);
1037 retval = 0;
1039 goto errout;
1044 * Insert serial port into IRQ chain.
1046 info->prev_port = 0;
1047 info->next_port = IRQ_ports[state->irq];
1048 if (info->next_port)
1049 info->next_port->prev_port = info;
1050 IRQ_ports[state->irq] = info;
1051 figure_IRQ_timeout(state->irq);
1054 * Now, initialize the UART
1056 serial_outp(info, SERIAL_LCR, SERIAL_LCR_LEN8); /* reset DLAB */
1058 info->MCR = 0;
1059 if (info->tty->termios->c_cflag & CBAUD)
1060 info->MCR = SERIAL_MCR_DTR | SERIAL_MCR_RTS;
1062 if (state->irq != 0)
1063 info->MCR |= SERIAL_MCR_OUT2;
1065 info->MCR |= ALPHA_KLUDGE_MCR; /* Don't ask */
1066 serial_outp(info, SERIAL_MCR, info->MCR);
1069 * Finally, enable interrupts
1071 info->IER = SERIAL_IER_MS | SERIAL_IER_RLS | SERIAL_IER_DR;
1072 serial_outp(info, SERIAL_IER, info->IER); /* enable interrupts */
1076 * And clear the interrupt registers again for luck.
1078 (void)serial_inp(info, SERIAL_LSR);
1079 (void)serial_inp(info, SERIAL_RBR);
1080 (void)serial_inp(info, SERIAL_IIR);
1081 (void)serial_inp(info, SERIAL_MSR);
1083 if (info->tty)
1084 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1085 info->xmit.head = info->xmit.tail = 0;
1088 * Set up serial timers...
1090 mod_timer(&serial_timer, jiffies + 2*HZ/100);
1093 * Set up the tty->alt_speed kludge
1095 #if (LINUX_VERSION_CODE >= 131394) /* Linux 2.1.66 */
1096 if (info->tty) {
1097 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1098 info->tty->alt_speed = 57600;
1099 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1100 info->tty->alt_speed = 115200;
1101 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1102 info->tty->alt_speed = 230400;
1103 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1104 info->tty->alt_speed = 460800;
1106 #endif
1109 * and set the speed of the serial port
1111 change_speed(info, 0);
1113 info->flags |= ASYNC_INITIALIZED;
1114 restore_flags(flags);
1115 return 0;
1117 errout:
1118 restore_flags(flags);
1119 return retval;
1125 * This routine will shutdown a serial port; interrupts are disabled, and
1126 * DTR is dropped if the hangup on close termio flag is on.
1128 static void shutdown(struct async_struct * info)
1130 unsigned long flags;
1131 struct serial_state *state;
1132 int retval;
1133 //printk("shutdown\r\n");
1134 if (!(info->flags & ASYNC_INITIALIZED))
1135 return;
1137 state = info->state;
1139 #ifdef SERIAL_DEBUG_OPEN
1140 printk("Shutting down serial port %d (irq %d)....", info->line,
1141 state->irq);
1142 #endif
1144 save_flags(flags); cli(); /* Disable interrupts */
1147 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1148 * here so the queue might never be waken up
1150 wake_up_interruptible(&info->delta_msr_wait);
1153 * First unlink the serial port from the IRQ chain...
1155 if (info->next_port)
1156 info->next_port->prev_port = info->prev_port;
1157 if (info->prev_port)
1158 info->prev_port->next_port = info->next_port;
1159 else
1160 IRQ_ports[state->irq] = info->next_port;
1161 figure_IRQ_timeout(state->irq);
1164 * Free the IRQ, if necessary
1166 if (state->irq && (!IRQ_ports[state->irq] ||
1167 !IRQ_ports[state->irq]->next_port)) {
1168 if (IRQ_ports[state->irq]) {
1169 free_irq(state->irq, &IRQ_ports[state->irq]);
1170 retval = request_irq(state->irq, rs_interrupt_single,
1171 SA_SHIRQ, "serial",
1172 &IRQ_ports[state->irq]);
1174 if (retval)
1175 printk("serial shutdown: request_irq: error %d"
1176 " Couldn't reacquire IRQ.\n", retval);
1177 } else
1178 free_irq(state->irq, &IRQ_ports[state->irq]);
1181 if (info->xmit.buf) {
1182 unsigned long pg = (unsigned long) info->xmit.buf;
1183 info->xmit.buf = 0;
1184 free_page(pg);
1187 info->IER = 0;
1188 serial_outp(info, SERIAL_IER, 0x00); /* disable all intrs */
1190 info->MCR &= ~SERIAL_MCR_OUT2;
1191 info->MCR |= ALPHA_KLUDGE_MCR; /* Don't ask */
1193 /* disable break condition */
1194 serial_out(info, SERIAL_LCR, serial_inp(info, SERIAL_LCR) & ~SERIAL_LCR_SETBREAK);
1196 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
1197 info->MCR &= ~(SERIAL_MCR_DTR|SERIAL_MCR_RTS);
1198 serial_outp(info, SERIAL_MCR, info->MCR);
1200 /* disable FIFO's */
1201 serial_outp(info, SERIAL_FCR, (SERIAL_FCR_FE |
1202 SERIAL_FCR_RXFR |
1203 SERIAL_FCR_TXFR));
1204 serial_outp(info, SERIAL_FCR, 0);
1208 (void)serial_in(info, SERIAL_RBR); /* read data port to reset things */
1210 if (info->tty)
1211 set_bit(TTY_IO_ERROR, &info->tty->flags);
1213 #if 0 // mask by Victor Yu. 05-19-2005
1214 #ifdef not_complete_yet
1215 if (uart_config[info->state->type].flags & UART_STARTECH) {
1216 /* Arrange to enter sleep mode */
1217 serial_outp(info, SERIAL_LCR, 0xBF);
1218 serial_outp(info, SERIAL_EFR, SERIAL_EFR_ECB);
1219 serial_outp(info, SERIAL_LCR, 0);
1220 serial_outp(info, SERIAL_IER, SERIAL_IERX_SLEEP);
1221 serial_outp(info, SERIAL_LCR, 0xBF);
1222 serial_outp(info, SERIAL_EFR, 0);
1223 serial_outp(info, SERIAL_LCR, 0);
1225 #endif /* end_of_not */
1227 #ifdef not_complete_yet
1228 if (info->state->type == PORT_16750) {
1229 /* Arrange to enter sleep mode */
1230 serial_outp(info, SERIAL_IER, SERIAL_IERX_SLEEP);
1232 #endif /* end_of_not */
1233 #endif
1234 info->flags &= ~ASYNC_INITIALIZED;
1235 restore_flags(flags);
1238 #if (LINUX_VERSION_CODE < 131394) /* Linux 2.1.66 */
1239 static int baud_table[] = {
1240 0, 50, 75, 110, 134, 150, 200, 300,
1241 600, 1200, 1800, 2400, 4800, 9600, 19200,
1242 38400, 57600, 115200, 230400, 460800, 0 };
1244 static int tty_get_baud_rate(struct tty_struct *tty)
1246 struct async_struct * info = (struct async_struct *)tty->driver_data;
1247 unsigned int cflag, i;
1249 cflag = tty->termios->c_cflag;
1251 i = cflag & CBAUD;
1252 if (i & CBAUDEX) {
1253 i &= ~CBAUDEX;
1254 if (i < 1 || i > 2)
1255 tty->termios->c_cflag &= ~CBAUDEX;
1256 else
1257 i += 15;
1259 if (i == 15) {
1260 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1261 i += 1;
1262 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1263 i += 2;
1265 return baud_table[i];
1267 #endif
1270 * This routine is called to set the UART divisor registers to match
1271 * the specified baud rate for a serial port.
1273 static void change_speed(struct async_struct *info,
1274 struct termios *old_termios)
1276 int quot = 0, baud_base, baud;
1277 unsigned cflag, cval, fcr = 0;
1278 int bits;
1279 unsigned long flags;
1280 //printk("change_speed\r\n");
1281 if (!info->tty || !info->tty->termios)
1282 return;
1283 cflag = info->tty->termios->c_cflag;
1284 if (!CONFIGURED_SERIAL_PORT(info))
1285 return;
1287 /* byte size and parity */
1288 switch (cflag & CSIZE)
1290 case CS5: cval = 0x00; bits = 7; break;
1291 case CS6: cval = 0x01; bits = 8; break;
1292 case CS7: cval = 0x02; bits = 9; break;
1293 case CS8: cval = 0x03; bits = 10; break;
1294 /* Never happens, but GCC is too dumb to figure it out */
1295 default: cval = 0x00; bits = 7; break;
1297 if (cflag & CSTOPB) {
1298 cval |= 0x04;
1299 bits++;
1301 if (cflag & PARENB) {
1302 cval |= SERIAL_LCR_PE;
1303 bits++;
1305 if (!(cflag & PARODD))
1306 cval |= SERIAL_LCR_EVEN;
1307 #ifdef CMSPAR
1308 if (cflag & CMSPAR)
1309 cval |= SERIAL_LCR_STICKPARITY;
1310 #endif
1312 /* Determine divisor based on baud rate */
1313 baud = tty_get_baud_rate(info->tty);
1314 if (!baud)
1315 baud = 9600; /* B0 transition handled in rs_set_termios */
1317 baud_base = info->state->baud_base;
1319 if (baud == 38400 &&
1320 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1321 quot = info->state->custom_divisor;
1322 else {
1323 if (baud == 134)
1324 /* Special case since 134 is really 134.5 */
1325 quot = (2*baud_base / 269);
1326 else if (baud)
1327 quot = baud_base / baud;
1329 /* If the quotient is zero refuse the change */
1330 if (!quot && old_termios) {
1331 info->tty->termios->c_cflag &= ~CBAUD;
1332 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
1333 baud = tty_get_baud_rate(info->tty);
1334 if (!baud)
1335 baud = 9600;
1336 if (baud == 38400 &&
1337 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1338 quot = info->state->custom_divisor;
1339 else {
1340 if (baud == 134)
1341 /* Special case since 134 is really 134.5 */
1342 quot = (2*baud_base / 269);
1343 else if (baud)
1344 quot = baud_base / baud;
1347 /* As a last resort, if the quotient is zero, default to 9600 bps */
1348 if (!quot)
1349 quot = baud_base / 9600;
1351 * Work around a bug in the Oxford Semiconductor 952 rev B
1352 * chip which causes it to seriously miscalculate baud rates
1353 * when DLL is 0.
1355 if (((quot & 0xFF) == 0) && (info->state->type == PORT_16C950) &&
1356 (info->state->revision == 0x5201))
1357 quot++;
1359 info->quot = quot;
1360 info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
1361 info->timeout += HZ/50; /* Add .02 seconds of slop */
1363 /* Set up FIFO's */
1364 if (uart_config[info->state->type].flags & UART_USE_FIFO)
1366 if ((info->state->baud_base / quot) < 2400)
1367 fcr = SERIAL_FCR_FE | SERIAL_FCR_TRIGGER_1;
1369 else
1370 //ivan
1371 //fcr = SERIAL_FCR_FE | SERIAL_FCR_TRIGGER_8;
1372 fcr = SERIAL_FCR_FE | SERIAL_FCR_TRIGGER_14;
1375 /* CTS flow control flag and modem status interrupts */
1376 info->IER &= ~SERIAL_IER_MS;
1377 if (info->flags & ASYNC_HARDPPS_CD)
1378 info->IER |= SERIAL_IER_MS;
1379 if (cflag & CRTSCTS) {
1380 info->flags |= ASYNC_CTS_FLOW;
1381 info->IER |= SERIAL_IER_MS;
1382 } else
1383 info->flags &= ~ASYNC_CTS_FLOW;
1384 if (cflag & CLOCAL)
1385 info->flags &= ~ASYNC_CHECK_CD;
1386 else {
1387 info->flags |= ASYNC_CHECK_CD;
1388 info->IER |= SERIAL_IER_MS;
1390 serial_out(info, SERIAL_IER, info->IER);
1393 * Set up parity check flag
1395 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1397 info->read_status_mask = SERIAL_LSR_OE | SERIAL_LSR_THRE | SERIAL_LSR_DR;
1398 if (I_INPCK(info->tty))
1399 info->read_status_mask |= SERIAL_LSR_FE | SERIAL_LSR_PE;
1400 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1401 info->read_status_mask |= SERIAL_LSR_BI;
1404 * Characters to ignore
1406 info->ignore_status_mask = 0;
1407 if (I_IGNPAR(info->tty))
1408 info->ignore_status_mask |= SERIAL_LSR_PE | SERIAL_LSR_FE;
1409 if (I_IGNBRK(info->tty)) {
1410 info->ignore_status_mask |= SERIAL_LSR_BI;
1412 * If we're ignore parity and break indicators, ignore
1413 * overruns too. (For real raw support).
1415 if (I_IGNPAR(info->tty))
1416 info->ignore_status_mask |= SERIAL_LSR_OE;
1419 * !!! ignore all characters if CREAD is not set
1421 if ((cflag & CREAD) == 0)
1422 info->ignore_status_mask |= SERIAL_LSR_DR;
1423 save_flags(flags); cli();
1425 serial_outp(info, SERIAL_LCR, cval | SERIAL_LCR_DLAB); /* set DLAB */
1426 serial_outp(info, SERIAL_DLL, quot & 0xff); /* LS of divisor */
1427 serial_outp(info, SERIAL_DLM, quot >> 8); /* MS of divisor */
1428 if (info->state->type == PORT_16750)
1429 serial_outp(info, SERIAL_FCR, fcr); /* set fcr */
1430 serial_outp(info, SERIAL_LCR, cval); /* reset DLAB */
1431 info->LCR = cval; /* Save LCR */
1432 if (info->state->type != PORT_16750)
1434 if (fcr & SERIAL_FCR_FE) {
1435 /* emulated UARTs (Lucent Venus 167x) need two steps */
1436 serial_outp(info, SERIAL_FCR, SERIAL_FCR_FE);
1438 serial_outp(info, SERIAL_FCR, fcr); /* set fcr */
1440 restore_flags(flags);
1443 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
1445 struct async_struct *info = (struct async_struct *)tty->driver_data;
1446 unsigned long flags;
1448 #if 0 // mask by Victor Yu. 05-18-2005
1449 if (serial_paranoia_check(info, tty->device, "rs_put_char"))
1450 #else // add by Victor Yu. 05-18-2005
1451 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
1452 #endif
1453 return;
1455 if (!tty || !info->xmit.buf)
1456 return;
1458 save_flags(flags); cli();
1459 if (CIRC_SPACE(info->xmit.head,
1460 info->xmit.tail,
1461 SERIAL_XMIT_SIZE) == 0) {
1462 restore_flags(flags);
1463 return;
1466 info->xmit.buf[info->xmit.head] = ch;
1467 info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
1468 restore_flags(flags);
1471 static void rs_flush_chars(struct tty_struct *tty)
1473 struct async_struct *info = (struct async_struct *)tty->driver_data;
1474 unsigned long flags;
1476 #if 0 // mask by Victor Yu. 05-18-2005
1477 if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
1478 #else // add by Victor Yu. 05-18-2005
1479 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
1480 #endif
1481 return;
1483 if (info->xmit.head == info->xmit.tail
1484 || tty->stopped
1485 || tty->hw_stopped
1486 || !info->xmit.buf)
1487 return;
1489 save_flags(flags); cli();
1490 info->IER |= SERIAL_IER_TE;
1491 serial_out(info, SERIAL_IER, info->IER);
1492 restore_flags(flags);
1495 static int rs_write(struct tty_struct * tty, int from_user, const unsigned char *buf, int count)
1497 int c, ret = 0;
1498 struct async_struct *info = (struct async_struct *)tty->driver_data;
1499 unsigned long flags;
1501 #if 0 // mask by Victor Yu. 05-18-2005
1502 if (serial_paranoia_check(info, tty->device, "rs_write"))
1503 #else // add by Victor Yu. 05-18-2005
1504 if (serial_paranoia_check(info, tty->name, "rs_write"))
1505 #endif
1506 return 0;
1508 if (!tty || !info->xmit.buf || !tmp_buf)
1509 return 0;
1511 // buf[count] = '\0';
1512 // printk("rs_write: %s\n", buf);
1513 save_flags(flags);
1514 if (from_user)
1516 down(&tmp_buf_sem);
1517 while (1) {
1518 int c1;
1519 c = CIRC_SPACE_TO_END(info->xmit.head,
1520 info->xmit.tail,
1521 SERIAL_XMIT_SIZE);
1522 if (count < c)
1523 c = count;
1524 if (c <= 0)
1525 break;
1527 c -= copy_from_user(tmp_buf, buf, c);
1528 if (!c) {
1529 if (!ret)
1530 ret = -EFAULT;
1531 break;
1533 cli();
1534 c1 = CIRC_SPACE_TO_END(info->xmit.head,
1535 info->xmit.tail,
1536 SERIAL_XMIT_SIZE);
1537 if (c1 < c)
1538 c = c1;
1539 memcpy(info->xmit.buf + info->xmit.head, tmp_buf, c);
1540 info->xmit.head = ((info->xmit.head + c) &
1541 (SERIAL_XMIT_SIZE-1));
1542 restore_flags(flags);
1543 buf += c;
1544 count -= c;
1545 ret += c;
1547 up(&tmp_buf_sem);
1549 else
1551 cli();
1552 while (1)
1554 c = CIRC_SPACE_TO_END(info->xmit.head,
1555 info->xmit.tail,
1556 SERIAL_XMIT_SIZE);
1557 if (count < c)
1558 c = count;
1559 if (c <= 0) {
1560 break;
1562 memcpy(info->xmit.buf + info->xmit.head, buf, c);
1563 info->xmit.head = ((info->xmit.head + c) &
1564 (SERIAL_XMIT_SIZE-1));
1565 buf += c;
1566 count -= c;
1567 ret += c;
1569 restore_flags(flags);
1571 if (info->xmit.head != info->xmit.tail
1572 && !tty->stopped
1573 && !tty->hw_stopped
1574 && !(info->IER & SERIAL_IER_TE))
1576 info->IER |= SERIAL_IER_TE;
1577 serial_out(info, SERIAL_IER, info->IER);
1579 return ret;
1582 static int rs_write_room(struct tty_struct *tty)
1584 struct async_struct *info = (struct async_struct *)tty->driver_data;
1586 #if 0 // mask by Victor Yu. 05-18-2005
1587 if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1588 #else // add by Victor Yu. 05-18-2005
1589 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
1590 #endif
1591 return 0;
1592 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1595 static int rs_chars_in_buffer(struct tty_struct *tty)
1597 struct async_struct *info = (struct async_struct *)tty->driver_data;
1599 #if 0 // mask by Victor Yu. 05-18-2005
1600 if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1601 #else // add by Victor Yu. 05-18-2005
1602 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
1603 #endif
1604 return 0;
1605 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1608 static void rs_flush_buffer(struct tty_struct *tty)
1610 struct async_struct *info = (struct async_struct *)tty->driver_data;
1611 unsigned long flags;
1613 #if 0 // mask by Victor Yu. 05-18-2005
1614 if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1615 #else // add by Victor Yu. 05-18-2005
1616 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
1617 #endif
1618 return;
1619 save_flags(flags); cli();
1620 info->xmit.head = info->xmit.tail = 0;
1621 restore_flags(flags);
1622 wake_up_interruptible(&tty->write_wait);
1623 #ifdef SERIAL_HAVE_POLL_WAIT
1624 wake_up_interruptible(&tty->poll_wait);
1625 #endif
1626 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1627 tty->ldisc.write_wakeup)
1628 (tty->ldisc.write_wakeup)(tty);
1632 * This function is used to send a high-priority XON/XOFF character to
1633 * the device
1635 static void rs_send_xchar(struct tty_struct *tty, char ch)
1637 struct async_struct *info = (struct async_struct *)tty->driver_data;
1639 #if 0 // mask by Victor Yu. 05-18-2005
1640 if (serial_paranoia_check(info, tty->device, "rs_send_char"))
1641 #else // add by Victor Yu. 05-18-2005
1642 if (serial_paranoia_check(info, tty->name, "rs_send_char"))
1643 #endif
1644 return;
1646 info->x_char = ch;
1647 if (ch) {
1648 /* Make sure transmit interrupts are on */
1649 info->IER |= SERIAL_IER_TE;
1650 serial_out(info, SERIAL_IER, info->IER);
1655 * ------------------------------------------------------------
1656 * rs_throttle()
1658 * This routine is called by the upper-layer tty layer to signal that
1659 * incoming characters should be throttled.
1660 * ------------------------------------------------------------
1662 static void rs_throttle(struct tty_struct * tty)
1664 struct async_struct *info = (struct async_struct *)tty->driver_data;
1665 unsigned long flags;
1666 #ifdef SERIAL_DEBUG_THROTTLE
1667 char buf[64];
1669 printk("throttle %s: %d....\n", tty_name(tty, buf),
1670 tty->ldisc.chars_in_buffer(tty));
1671 #endif
1673 #if 0 // mask by Victor Yu. 05-18-2005
1674 if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1675 #else // add by Victor Yu. 05-18-2005
1676 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
1677 #endif
1678 return;
1680 if (I_IXOFF(tty))
1681 rs_send_xchar(tty, STOP_CHAR(tty));
1683 if (tty->termios->c_cflag & CRTSCTS)
1684 info->MCR &= ~SERIAL_MCR_RTS;
1686 save_flags(flags); cli();
1687 serial_out(info, SERIAL_MCR, info->MCR);
1688 restore_flags(flags);
1691 static void rs_unthrottle(struct tty_struct * tty)
1693 struct async_struct *info = (struct async_struct *)tty->driver_data;
1694 unsigned long flags;
1695 #ifdef SERIAL_DEBUG_THROTTLE
1696 char buf[64];
1698 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1699 tty->ldisc.chars_in_buffer(tty));
1700 #endif
1702 # if 0 // mask by Victor Yu. 05-18-2005
1703 if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1704 #else // add by Victor yu. 05-18-2005
1705 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1706 #endif
1707 return;
1709 if (I_IXOFF(tty)) {
1710 if (info->x_char)
1711 info->x_char = 0;
1712 else
1713 rs_send_xchar(tty, START_CHAR(tty));
1715 if (tty->termios->c_cflag & CRTSCTS)
1716 info->MCR |= SERIAL_MCR_RTS;
1717 save_flags(flags); cli();
1718 serial_out(info, SERIAL_MCR, info->MCR);
1719 restore_flags(flags);
1723 * ------------------------------------------------------------
1724 * rs_ioctl() and friends
1725 * ------------------------------------------------------------
1728 static int get_serial_info(struct async_struct * info,
1729 struct serial_struct * retinfo)
1731 struct serial_struct tmp;
1732 struct serial_state *state = info->state;
1734 if (!retinfo)
1735 return -EFAULT;
1736 memset(&tmp, 0, sizeof(tmp));
1737 tmp.type = state->type;
1738 tmp.line = state->line;
1739 tmp.port = state->port;
1740 if (HIGH_BITS_OFFSET)
1741 tmp.port_high = state->port >> HIGH_BITS_OFFSET;
1742 else
1743 tmp.port_high = 0;
1744 tmp.irq = state->irq;
1745 tmp.flags = state->flags;
1746 tmp.xmit_fifo_size = state->xmit_fifo_size;
1747 tmp.baud_base = state->baud_base;
1748 tmp.close_delay = state->close_delay;
1749 tmp.closing_wait = state->closing_wait;
1750 tmp.custom_divisor = state->custom_divisor;
1751 tmp.hub6 = state->hub6;
1752 tmp.io_type = state->io_type;
1753 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1754 return -EFAULT;
1755 return 0;
1758 static int set_serial_info(struct async_struct * info,
1759 struct serial_struct * new_info)
1761 struct serial_struct new_serial;
1762 struct serial_state old_state, *state;
1763 unsigned int i,change_irq,change_port;
1764 int retval = 0;
1765 unsigned long new_port;
1767 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1768 return -EFAULT;
1769 state = info->state;
1770 old_state = *state;
1772 new_port = new_serial.port;
1773 if (HIGH_BITS_OFFSET)
1774 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
1776 change_irq = new_serial.irq != state->irq;
1777 change_port = (new_port != ((int) state->port)) ||
1778 (new_serial.hub6 != state->hub6);
1780 if (!capable(CAP_SYS_ADMIN)) {
1781 if (change_irq || change_port ||
1782 (new_serial.baud_base != state->baud_base) ||
1783 (new_serial.type != state->type) ||
1784 (new_serial.close_delay != state->close_delay) ||
1785 (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1786 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1787 (state->flags & ~ASYNC_USR_MASK)))
1788 return -EPERM;
1789 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
1790 (new_serial.flags & ASYNC_USR_MASK));
1791 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1792 (new_serial.flags & ASYNC_USR_MASK));
1793 state->custom_divisor = new_serial.custom_divisor;
1794 goto check_and_exit;
1797 #if 0 // mask by Victor Yu. 05-19-2005
1798 new_serial.irq = irq_cannonicalize(new_serial.irq);
1799 #endif
1801 if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||
1802 (new_serial.baud_base < 9600)|| (new_serial.type < PORT_UNKNOWN) ||
1803 (new_serial.type > PORT_MAX) || (new_serial.type == PORT_CIRRUS) ||
1804 (new_serial.type == PORT_STARTECH)) {
1805 return -EINVAL;
1808 if ((new_serial.type != state->type) ||
1809 (new_serial.xmit_fifo_size <= 0))
1810 new_serial.xmit_fifo_size =
1811 uart_config[new_serial.type].dfl_xmit_fifo_size;
1813 /* Make sure address is not already in use */
1814 if (new_serial.type) {
1815 for (i = 0 ; i < NR_PORTS; i++)
1816 if ((state != &rs_table[i]) &&
1817 (rs_table[i].port == new_port) &&
1818 rs_table[i].type)
1819 return -EADDRINUSE;
1822 if ((change_port || change_irq) && (state->count > 1))
1823 return -EBUSY;
1826 * OK, past this point, all the error checking has been done.
1827 * At this point, we start making changes.....
1830 state->baud_base = new_serial.baud_base;
1831 state->flags = ((state->flags & ~ASYNC_FLAGS) |
1832 (new_serial.flags & ASYNC_FLAGS));
1833 info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
1834 (info->flags & ASYNC_INTERNAL_FLAGS));
1835 state->custom_divisor = new_serial.custom_divisor;
1836 state->close_delay = new_serial.close_delay * HZ/100;
1837 state->closing_wait = new_serial.closing_wait * HZ/100;
1838 #if (LINUX_VERSION_CODE > 0x20100)
1839 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1840 #endif
1841 info->xmit_fifo_size = state->xmit_fifo_size =
1842 new_serial.xmit_fifo_size;
1844 if ((state->type != PORT_UNKNOWN) && state->port) {
1846 release_region(state->port,8);
1848 state->type = new_serial.type;
1849 if (change_port || change_irq) {
1851 * We need to shutdown the serial port at the old
1852 * port/irq combination.
1854 shutdown(info);
1855 state->irq = new_serial.irq;
1856 info->port = state->port = new_port;
1857 info->hub6 = state->hub6 = new_serial.hub6;
1858 if (info->hub6)
1859 info->io_type = state->io_type = SERIAL_IO_HUB6;
1860 else if (info->io_type == SERIAL_IO_HUB6)
1861 info->io_type = state->io_type = SERIAL_IO_PORT;
1863 if ((state->type != PORT_UNKNOWN) && state->port) {
1864 request_region(state->port,8,"serial(set)");
1868 check_and_exit:
1869 if (!state->port || !state->type)
1870 return 0;
1871 if (info->flags & ASYNC_INITIALIZED) {
1872 if (((old_state.flags & ASYNC_SPD_MASK) !=
1873 (state->flags & ASYNC_SPD_MASK)) ||
1874 (old_state.custom_divisor != state->custom_divisor)) {
1875 #if (LINUX_VERSION_CODE >= 131394) /* Linux 2.1.66 */
1876 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1877 info->tty->alt_speed = 57600;
1878 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1879 info->tty->alt_speed = 115200;
1880 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1881 info->tty->alt_speed = 230400;
1882 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1883 info->tty->alt_speed = 460800;
1884 #endif
1885 change_speed(info, 0);
1887 } else
1888 retval = startup(info);
1889 return retval;
1894 * get_lsr_info - get line status register info
1896 * Purpose: Let user call ioctl() to get info when the UART physically
1897 * is emptied. On bus types like RS485, the transmitter must
1898 * release the bus after transmitting. This must be done when
1899 * the transmit shift register is empty, not be done when the
1900 * transmit holding register is empty. This functionality
1901 * allows an RS485 driver to be written in user space.
1903 static int get_lsr_info(struct async_struct * info, unsigned int *value)
1905 unsigned char status;
1906 unsigned int result;
1907 unsigned long flags;
1909 save_flags(flags); cli();
1910 status = serial_in(info, SERIAL_LSR);
1911 restore_flags(flags);
1912 result = ((status & SERIAL_LSR_TE) ? TIOCSER_TEMT : 0);
1915 * If we're about to load something into the transmit
1916 * register, we'll pretend the transmitter isn't empty to
1917 * avoid a race condition (depending on when the transmit
1918 * interrupt happens).
1920 if (info->x_char ||
1921 ((CIRC_CNT(info->xmit.head, info->xmit.tail,
1922 SERIAL_XMIT_SIZE) > 0) &&
1923 !info->tty->stopped && !info->tty->hw_stopped))
1924 result &= ~TIOCSER_TEMT;
1926 if (copy_to_user(value, &result, sizeof(int)))
1927 return -EFAULT;
1928 return 0;
1932 static int get_modem_info(struct async_struct * info, unsigned int *value)
1934 unsigned char control, status;
1935 unsigned int result;
1936 unsigned long flags;
1938 control = info->MCR;
1939 save_flags(flags); cli();
1940 status = serial_in(info, SERIAL_MSR);
1941 restore_flags(flags);
1942 result = ((control & SERIAL_MCR_RTS) ? TIOCM_RTS : 0)
1943 | ((control & SERIAL_MCR_DTR) ? TIOCM_DTR : 0)
1944 #ifdef TIOCM_OUT1
1945 | ((control & SERIAL_MCR_OUT1) ? TIOCM_OUT1 : 0)
1946 | ((control & SERIAL_MCR_OUT2) ? TIOCM_OUT2 : 0)
1947 #endif
1948 | ((status & SERIAL_MSR_DCD) ? TIOCM_CAR : 0)
1949 | ((status & SERIAL_MSR_RI) ? TIOCM_RNG : 0)
1950 | ((status & SERIAL_MSR_DSR) ? TIOCM_DSR : 0)
1951 | ((status & SERIAL_MSR_CTS) ? TIOCM_CTS : 0);
1953 if (copy_to_user(value, &result, sizeof(int)))
1954 return -EFAULT;
1955 return 0;
1958 static int set_modem_info(struct async_struct * info, unsigned int cmd,
1959 unsigned int *value)
1961 unsigned int arg;
1962 unsigned long flags;
1964 if (copy_from_user(&arg, value, sizeof(int)))
1965 return -EFAULT;
1967 switch (cmd) {
1968 case TIOCMBIS:
1969 if (arg & TIOCM_RTS)
1970 info->MCR |= SERIAL_MCR_RTS;
1971 if (arg & TIOCM_DTR)
1972 info->MCR |= SERIAL_MCR_DTR;
1973 #ifdef TIOCM_OUT1
1974 if (arg & TIOCM_OUT1)
1975 info->MCR |= SERIAL_MCR_OUT1;
1976 if (arg & TIOCM_OUT2)
1977 info->MCR |= SERIAL_MCR_OUT2;
1978 #endif
1979 if (arg & TIOCM_LOOP)
1980 info->MCR |= SERIAL_MCR_LPBK;
1981 break;
1982 case TIOCMBIC:
1983 if (arg & TIOCM_RTS)
1984 info->MCR &= ~SERIAL_MCR_RTS;
1985 if (arg & TIOCM_DTR)
1986 info->MCR &= ~SERIAL_MCR_DTR;
1987 #ifdef TIOCM_OUT1
1988 if (arg & TIOCM_OUT1)
1989 info->MCR &= ~SERIAL_MCR_OUT1;
1990 if (arg & TIOCM_OUT2)
1991 info->MCR &= ~SERIAL_MCR_OUT2;
1992 #endif
1993 if (arg & TIOCM_LOOP)
1994 info->MCR &= ~SERIAL_MCR_LPBK;
1995 break;
1996 case TIOCMSET:
1997 info->MCR = ((info->MCR & ~(SERIAL_MCR_RTS |
1998 #ifdef TIOCM_OUT1
1999 SERIAL_MCR_OUT1 |
2000 SERIAL_MCR_OUT2 |
2001 #endif
2002 SERIAL_MCR_LPBK |
2003 SERIAL_MCR_DTR))
2004 | ((arg & TIOCM_RTS) ? SERIAL_MCR_RTS : 0)
2005 #ifdef TIOCM_OUT1
2006 | ((arg & TIOCM_OUT1) ? SERIAL_MCR_OUT1 : 0)
2007 | ((arg & TIOCM_OUT2) ? SERIAL_MCR_OUT2 : 0)
2008 #endif
2009 | ((arg & TIOCM_LOOP) ? SERIAL_MCR_LPBK : 0)
2010 | ((arg & TIOCM_DTR) ? SERIAL_MCR_DTR : 0));
2011 break;
2012 default:
2013 return -EINVAL;
2015 save_flags(flags); cli();
2016 info->MCR |= ALPHA_KLUDGE_MCR; /* Don't ask */
2017 serial_out(info, SERIAL_MCR, info->MCR);
2018 restore_flags(flags);
2019 return 0;
2022 static int do_autoconfig(struct async_struct * info)
2024 int irq, retval;
2026 if (!capable(CAP_SYS_ADMIN))
2027 return -EPERM;
2029 if (info->state->count > 1)
2030 return -EBUSY;
2032 shutdown(info);
2034 autoconfig(info->state);
2035 if ((info->state->flags & ASYNC_AUTO_IRQ) &&
2036 (info->state->port != 0 || info->state->iomem_base != 0) &&
2037 (info->state->type != PORT_UNKNOWN)) {
2038 irq = detect_uart_irq(info->state);
2039 if (irq > 0)
2040 info->state->irq = irq;
2043 retval = startup(info);
2044 if (retval)
2045 return retval;
2046 return 0;
2050 * rs_break() --- routine which turns the break handling on or off
2052 #if (LINUX_VERSION_CODE < 131394) /* Linux 2.1.66 */
2053 static void send_break( struct async_struct * info, int duration)
2055 if (!CONFIGURED_SERIAL_PORT(info))
2056 return;
2057 current->state = TASK_INTERRUPTIBLE;
2058 current->timeout = jiffies + duration;
2059 cli();
2060 info->LCR |= SERIAL_LCR_SETBREAK;
2061 serial_out(info, SERIAL_LCR, info->LCR);
2062 schedule();
2063 info->LCR &= ~SERIAL_LCR_SETBREAK;
2064 serial_out(info, SERIAL_LCR, info->LCR);
2065 sti();
2067 #else
2068 static void rs_break(struct tty_struct *tty, int break_state)
2070 struct async_struct * info = (struct async_struct *)tty->driver_data;
2071 unsigned long flags;
2073 #if 0 // mask by Victor Yu. 05-18-2005
2074 if (serial_paranoia_check(info, tty->device, "rs_break"))
2075 #else // add by Victor Yu. 05-18-2005
2076 if (serial_paranoia_check(info, tty->name, "rs_break"))
2077 #endif
2078 return;
2080 if (!CONFIGURED_SERIAL_PORT(info))
2081 return;
2082 save_flags(flags); cli();
2083 if (break_state == -1)
2084 info->LCR |= SERIAL_LCR_SETBREAK;
2085 else
2086 info->LCR &= ~SERIAL_LCR_SETBREAK;
2087 serial_out(info, SERIAL_LCR, info->LCR);
2088 restore_flags(flags);
2090 #endif
2093 static int rs_ioctl(struct tty_struct *tty, struct file * file,
2094 unsigned int cmd, unsigned long arg)
2096 struct async_struct * info = (struct async_struct *)tty->driver_data;
2097 struct async_icount cprev, cnow; /* kernel counter temps */
2098 struct serial_icounter_struct icount;
2099 unsigned long flags;
2100 #if (LINUX_VERSION_CODE < 131394) /* Linux 2.1.66 */
2101 int retval, tmp;
2102 #endif
2104 #if 0 // mask by Victor Yu. 05-18-2005
2105 if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
2106 #else // add by Victor Yu. 05-18-2005
2107 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
2108 #endif
2109 return -ENODEV;
2111 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2112 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
2113 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
2114 if (tty->flags & (1 << TTY_IO_ERROR))
2115 return -EIO;
2118 switch (cmd) {
2119 #if (LINUX_VERSION_CODE < 131394) /* Linux 2.1.66 */
2120 case TCSBRK: /* SVID version: non-zero arg --> no break */
2121 retval = tty_check_change(tty);
2122 if (retval)
2123 return retval;
2124 tty_wait_until_sent(tty, 0);
2125 if (signal_pending(current))
2126 return -EINTR;
2127 if (!arg) {
2128 send_break(info, HZ/4); /* 1/4 second */
2129 if (signal_pending(current))
2130 return -EINTR;
2132 return 0;
2133 case TCSBRKP: /* support for POSIX tcsendbreak() */
2134 retval = tty_check_change(tty);
2135 if (retval)
2136 return retval;
2137 tty_wait_until_sent(tty, 0);
2138 if (signal_pending(current))
2139 return -EINTR;
2140 send_break(info, arg ? arg*(HZ/10) : HZ/4);
2141 if (signal_pending(current))
2142 return -EINTR;
2143 return 0;
2144 case TIOCGSOFTCAR:
2145 tmp = C_CLOCAL(tty) ? 1 : 0;
2146 if (copy_to_user((void *)arg, &tmp, sizeof(int)))
2147 return -EFAULT;
2148 return 0;
2149 case TIOCSSOFTCAR:
2150 if (copy_from_user(&tmp, (void *)arg, sizeof(int)))
2151 return -EFAULT;
2153 tty->termios->c_cflag =
2154 ((tty->termios->c_cflag & ~CLOCAL) |
2155 (tmp ? CLOCAL : 0));
2156 return 0;
2157 #endif
2158 case TIOCMGET:
2159 return get_modem_info(info, (unsigned int *) arg);
2160 case TIOCMBIS:
2161 case TIOCMBIC:
2162 case TIOCMSET:
2163 return set_modem_info(info, cmd, (unsigned int *) arg);
2164 case TIOCGSERIAL:
2165 return get_serial_info(info,
2166 (struct serial_struct *) arg);
2167 case TIOCSSERIAL:
2168 return set_serial_info(info,
2169 (struct serial_struct *) arg);
2170 case TIOCSERCONFIG:
2171 return do_autoconfig(info);
2173 case TIOCSERGETLSR: /* Get line status register */
2174 return get_lsr_info(info, (unsigned int *) arg);
2176 case TIOCSERGSTRUCT:
2177 if (copy_to_user((struct async_struct *) arg,
2178 info, sizeof(struct async_struct)))
2179 return -EFAULT;
2180 return 0;
2184 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
2185 * - mask passed in arg for lines of interest
2186 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
2187 * Caller should use TIOCGICOUNT to see which one it was
2189 case TIOCMIWAIT:
2190 save_flags(flags); cli();
2191 /* note the counters on entry */
2192 cprev = info->state->icount;
2193 restore_flags(flags);
2194 /* Force modem status interrupts on */
2195 info->IER |= SERIAL_IER_MS;
2196 serial_out(info, SERIAL_IER, info->IER);
2197 while (1) {
2198 interruptible_sleep_on(&info->delta_msr_wait);
2199 /* see if a signal did it */
2200 if (signal_pending(current))
2201 return -ERESTARTSYS;
2202 save_flags(flags); cli();
2203 cnow = info->state->icount; /* atomic copy */
2204 restore_flags(flags);
2205 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2206 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2207 return -EIO; /* no change => error */
2208 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2209 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2210 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2211 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
2212 return 0;
2214 cprev = cnow;
2216 /* NOTREACHED */
2219 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
2220 * Return: write counters to the user passed counter struct
2221 * NB: both 1->0 and 0->1 transitions are counted except for
2222 * RI where only 0->1 is counted.
2224 case TIOCGICOUNT:
2225 save_flags(flags); cli();
2226 cnow = info->state->icount;
2227 restore_flags(flags);
2228 icount.cts = cnow.cts;
2229 icount.dsr = cnow.dsr;
2230 icount.rng = cnow.rng;
2231 icount.dcd = cnow.dcd;
2232 icount.rx = cnow.rx;
2233 icount.tx = cnow.tx;
2234 icount.frame = cnow.frame;
2235 icount.overrun = cnow.overrun;
2236 icount.parity = cnow.parity;
2237 icount.brk = cnow.brk;
2238 icount.buf_overrun = cnow.buf_overrun;
2240 if (copy_to_user((void *)arg, &icount, sizeof(icount)))
2241 return -EFAULT;
2242 return 0;
2243 case TIOCSERGWILD:
2244 case TIOCSERSWILD:
2245 /* "setserial -W" is called in Debian boot */
2246 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
2247 return 0;
2249 default:
2250 return -ENOIOCTLCMD;
2252 return 0;
2255 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
2257 struct async_struct *info = (struct async_struct *)tty->driver_data;
2258 unsigned long flags;
2259 unsigned int cflag = tty->termios->c_cflag;
2261 if ( (cflag == old_termios->c_cflag)
2262 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
2263 == RELEVANT_IFLAG(old_termios->c_iflag)))
2264 return;
2266 change_speed(info, old_termios);
2268 /* Handle transition to B0 status */
2269 if ((old_termios->c_cflag & CBAUD) &&
2270 !(cflag & CBAUD)) {
2271 info->MCR &= ~(SERIAL_MCR_DTR|SERIAL_MCR_RTS);
2272 save_flags(flags); cli();
2273 serial_out(info, SERIAL_MCR, info->MCR);
2274 restore_flags(flags);
2277 /* Handle transition away from B0 status */
2278 if (!(old_termios->c_cflag & CBAUD) &&
2279 (cflag & CBAUD)) {
2280 info->MCR |= SERIAL_MCR_DTR;
2281 if (!(tty->termios->c_cflag & CRTSCTS) ||
2282 !test_bit(TTY_THROTTLED, &tty->flags)) {
2283 info->MCR |= SERIAL_MCR_RTS;
2285 save_flags(flags); cli();
2286 serial_out(info, SERIAL_MCR, info->MCR);
2287 restore_flags(flags);
2290 /* Handle turning off CRTSCTS */
2291 if ((old_termios->c_cflag & CRTSCTS) &&
2292 !(tty->termios->c_cflag & CRTSCTS)) {
2293 tty->hw_stopped = 0;
2294 rs_start(tty);
2297 #if 0
2299 * No need to wake up processes in open wait, since they
2300 * sample the CLOCAL flag once, and don't recheck it.
2301 * XXX It's not clear whether the current behavior is correct
2302 * or not. Hence, this may change.....
2304 if (!(old_termios->c_cflag & CLOCAL) &&
2305 (tty->termios->c_cflag & CLOCAL))
2306 wake_up_interruptible(&info->open_wait);
2307 #endif
2311 * ------------------------------------------------------------
2312 * rs_close()
2314 * This routine is called when the serial port gets closed. First, we
2315 * wait for the last remaining data to be sent. Then, we unlink its
2316 * async structure from the interrupt chain if necessary, and we free
2317 * that IRQ if nothing is left in the chain.
2318 * ------------------------------------------------------------
2320 static void rs_close(struct tty_struct *tty, struct file * filp)
2322 struct async_struct * info = (struct async_struct *)tty->driver_data;
2323 struct serial_state *state;
2324 unsigned long flags;
2326 #if 0 // mask by Victor Yu. 05-18-2005
2327 if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
2328 #else // add by Victor Yu. 05-18-2005
2329 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
2330 #endif
2331 return;
2333 state = info->state;
2335 save_flags(flags); cli();
2337 if (tty_hung_up_p(filp)) {
2338 DBG_CNT("before DEC-hung");
2339 MOD_DEC_USE_COUNT;
2340 restore_flags(flags);
2341 return;
2344 #ifdef SERIAL_DEBUG_OPEN
2345 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
2346 #endif
2347 if ((tty->count == 1) && (state->count != 1)) {
2349 * Uh, oh. tty->count is 1, which means that the tty
2350 * structure will be freed. state->count should always
2351 * be one in these conditions. If it's greater than
2352 * one, we've got real problems, since it means the
2353 * serial port won't be shutdown.
2355 printk("rs_close: bad serial port count; tty->count is 1, "
2356 "state->count is %d\n", state->count);
2357 state->count = 1;
2359 if (--state->count < 0) {
2360 printk("rs_close: bad serial port count for ttys%d: %d\n",
2361 info->line, state->count);
2362 state->count = 0;
2364 if (state->count) {
2365 DBG_CNT("before DEC-2");
2366 MOD_DEC_USE_COUNT;
2367 restore_flags(flags);
2368 return;
2370 info->flags |= ASYNC_CLOSING;
2371 restore_flags(flags);
2373 * Save the termios structure, since this port may have
2374 * separate termios for callout and dialin.
2376 #if 0 // mask by Victor Yu. 05-19-2005
2377 if (info->flags & ASYNC_NORMAL_ACTIVE)
2378 info->state->normal_termios = *tty->termios;
2379 if (info->flags & ASYNC_CALLOUT_ACTIVE)
2380 info->state->callout_termios = *tty->termios;
2381 #endif
2383 * Now we wait for the transmit buffer to clear; and we notify
2384 * the line discipline to only process XON/XOFF characters.
2386 tty->closing = 1;
2387 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
2388 tty_wait_until_sent(tty, info->closing_wait);
2390 * At this point we stop accepting input. To do this, we
2391 * disable the receive line status interrupts, and tell the
2392 * interrupt driver to stop checking the data ready bit in the
2393 * line status register.
2395 info->IER &= ~SERIAL_IER_RLS;
2396 info->read_status_mask &= ~SERIAL_LSR_DR;
2397 if (info->flags & ASYNC_INITIALIZED) {
2398 serial_out(info, SERIAL_IER, info->IER);
2400 * Before we drop DTR, make sure the UART transmitter
2401 * has completely drained; this is especially
2402 * important if there is a transmit FIFO!
2404 rs_wait_until_sent(tty, info->timeout);
2406 shutdown(info);
2407 #if 0 // mask by Victor Yu. 05-19-2005
2408 if (tty->driver.flush_buffer)
2409 tty->driver.flush_buffer(tty);
2410 if (tty->ldisc.flush_buffer)
2411 tty->ldisc.flush_buffer(tty);
2412 #else // add by Victor Yu. 05-19-2005
2413 if ( tty->driver->flush_buffer )
2414 tty->driver->flush_buffer(tty);
2415 tty_ldisc_flush(tty);
2416 #endif
2417 tty->closing = 0;
2418 info->event = 0;
2419 info->tty = 0;
2420 if (info->blocked_open) {
2421 if (info->close_delay) {
2422 set_current_state(TASK_INTERRUPTIBLE);
2423 schedule_timeout(info->close_delay);
2425 wake_up_interruptible(&info->open_wait);
2427 #if 0 // mask by Victor Yu. 05-19-2005
2428 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
2429 ASYNC_CLOSING);
2430 #else // add by Victor Yu. 05-19-2005
2431 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
2432 #endif
2433 wake_up_interruptible(&info->close_wait);
2434 MOD_DEC_USE_COUNT;
2438 * rs_wait_until_sent() --- wait until the transmitter is empty
2440 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
2442 struct async_struct * info = (struct async_struct *)tty->driver_data;
2443 unsigned long orig_jiffies, char_time;
2444 int lsr;
2446 #if 0 // mask by Victor Yu. 05-18-2005
2447 if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
2448 #else // add by Victor Yu. 05-18-2005
2449 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
2450 #endif
2451 return;
2453 if (info->state->type == PORT_UNKNOWN)
2454 return;
2456 if (info->xmit_fifo_size == 0)
2457 return; /* Just in case.... */
2459 orig_jiffies = jiffies;
2461 * Set the check interval to be 1/5 of the estimated time to
2462 * send a single character, and make it at least 1. The check
2463 * interval should also be less than the timeout.
2465 * Note: we have to use pretty tight timings here to satisfy
2466 * the NIST-PCTS.
2468 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
2469 char_time = char_time / 5;
2470 if (char_time == 0)
2471 char_time = 1;
2472 if (timeout && timeout < char_time)
2473 char_time = timeout;
2475 * If the transmitter hasn't cleared in twice the approximate
2476 * amount of time to send the entire FIFO, it probably won't
2477 * ever clear. This assumes the UART isn't doing flow
2478 * control, which is currently the case. Hence, if it ever
2479 * takes longer than info->timeout, this is probably due to a
2480 * UART bug of some kind. So, we clamp the timeout parameter at
2481 * 2*info->timeout.
2483 if (!timeout || timeout > 2*info->timeout)
2484 timeout = 2*info->timeout;
2485 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2486 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
2487 printk("jiff=%lu...", jiffies);
2488 #endif
2489 while (!((lsr = serial_inp(info, SERIAL_LSR)) & SERIAL_LSR_TE)) {
2490 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2491 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2492 #endif
2493 set_current_state(TASK_INTERRUPTIBLE);
2494 schedule_timeout(char_time);
2495 if (signal_pending(current))
2496 break;
2497 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2498 break;
2500 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2501 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2502 #endif
2506 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
2508 static void rs_hangup(struct tty_struct *tty)
2510 struct async_struct * info = (struct async_struct *)tty->driver_data;
2511 struct serial_state *state = info->state;
2513 #if 0 // mask by Victor Yu. 05-18-2005
2514 if (serial_paranoia_check(info, tty->device, "rs_hangup"))
2515 #else // add by Victor Yu. 05-18-2005
2516 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
2517 #endif
2518 return;
2520 state = info->state;
2522 rs_flush_buffer(tty);
2523 if (info->flags & ASYNC_CLOSING)
2524 return;
2525 shutdown(info);
2526 info->event = 0;
2527 state->count = 0;
2528 #if 0 // mask by Victor Yu. 05-19-2005
2529 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
2530 #else // add by Victor Yu. 05-19-2005
2531 info->flags &= ~(ASYNC_NORMAL_ACTIVE);
2532 #endif
2533 info->tty = 0;
2534 wake_up_interruptible(&info->open_wait);
2538 * ------------------------------------------------------------
2539 * rs_open() and friends
2540 * ------------------------------------------------------------
2542 static int block_til_ready(struct tty_struct *tty, struct file * filp,
2543 struct async_struct *info)
2545 DECLARE_WAITQUEUE(wait, current);
2546 struct serial_state *state = info->state;
2547 int retval;
2548 int do_clocal = 0, extra_count = 0;
2549 unsigned long flags;
2552 * If the device is in the middle of being closed, then block
2553 * until it's done, and then try again.
2555 if (tty_hung_up_p(filp) ||
2556 (info->flags & ASYNC_CLOSING)) {
2557 if (info->flags & ASYNC_CLOSING)
2558 interruptible_sleep_on(&info->close_wait);
2559 #ifdef SERIAL_DO_RESTART
2560 return ((info->flags & ASYNC_HUP_NOTIFY) ?
2561 -EAGAIN : -ERESTARTSYS);
2562 #else
2563 return -EAGAIN;
2564 #endif
2568 * If this is a callout device, then just make sure the normal
2569 * device isn't being used.
2571 #if 0 // mask by Victor Yu. 05-19-2005
2572 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
2573 if (info->flags & ASYNC_NORMAL_ACTIVE)
2574 return -EBUSY;
2575 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2576 (info->flags & ASYNC_SESSION_LOCKOUT) &&
2577 (info->session != current->session))
2578 return -EBUSY;
2579 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2580 (info->flags & ASYNC_PGRP_LOCKOUT) &&
2581 (info->pgrp != current->pgrp))
2582 return -EBUSY;
2583 info->flags |= ASYNC_CALLOUT_ACTIVE;
2584 return 0;
2586 #endif
2589 * If non-blocking mode is set, or the port is not enabled,
2590 * then make the check up front and then exit.
2592 if ((filp->f_flags & O_NONBLOCK) ||
2593 (tty->flags & (1 << TTY_IO_ERROR))) {
2594 #if 0 // mask by Victor Yu. 05-19-2005
2595 if (info->flags & ASYNC_CALLOUT_ACTIVE)
2596 return -EBUSY;
2597 #endif
2598 info->flags |= ASYNC_NORMAL_ACTIVE;
2599 return 0;
2602 #if 0 // mask by Victor Yu. 05-19-2005
2603 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
2604 if (state->normal_termios.c_cflag & CLOCAL)
2605 do_clocal = 1;
2606 } else {
2607 if (tty->termios->c_cflag & CLOCAL)
2608 do_clocal = 1;
2610 #endif
2613 * Block waiting for the carrier detect and the line to become
2614 * free (i.e., not in use by the callout). While we are in
2615 * this loop, state->count is dropped by one, so that
2616 * rs_close() knows when to free things. We restore it upon
2617 * exit, either normal or abnormal.
2619 retval = 0;
2620 add_wait_queue(&info->open_wait, &wait);
2621 #ifdef SERIAL_DEBUG_OPEN
2622 printk("block_til_ready before block: ttys%d, count = %d\n",
2623 state->line, state->count);
2624 #endif
2625 save_flags(flags); cli();
2626 if (!tty_hung_up_p(filp)) {
2627 extra_count = 1;
2628 state->count--;
2630 restore_flags(flags);
2631 info->blocked_open++;
2632 while (1) {
2633 #if 0 // mask by Victor Yu. 05-19-2005
2634 save_flags(flags); cli();
2635 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2636 (tty->termios->c_cflag & CBAUD))
2637 serial_out(info, SERIAL_MCR,
2638 serial_inp(info, SERIAL_MCR) |
2639 (SERIAL_MCR_DTR | SERIAL_MCR_RTS));
2640 restore_flags(flags);
2641 #endif
2642 set_current_state(TASK_INTERRUPTIBLE);
2643 if (tty_hung_up_p(filp) ||
2644 !(info->flags & ASYNC_INITIALIZED)) {
2645 #ifdef SERIAL_DO_RESTART
2646 if (info->flags & ASYNC_HUP_NOTIFY)
2647 retval = -EAGAIN;
2648 else
2649 retval = -ERESTARTSYS;
2650 #else
2651 retval = -EAGAIN;
2652 #endif
2653 break;
2655 #if 0 // mask by Victor Yu. 05-19-2005
2656 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2657 !(info->flags & ASYNC_CLOSING) &&
2658 (do_clocal || (serial_in(info, SERIAL_MSR) &
2659 SERIAL_MSR_DCD)))
2660 break;
2661 #endif
2662 if (signal_pending(current)) {
2663 retval = -ERESTARTSYS;
2664 break;
2666 #ifdef SERIAL_DEBUG_OPEN
2667 printk("block_til_ready blocking: ttys%d, count = %d\n",
2668 info->line, state->count);
2669 #endif
2670 schedule();
2672 set_current_state(TASK_RUNNING);
2673 remove_wait_queue(&info->open_wait, &wait);
2674 if (extra_count)
2675 state->count++;
2676 info->blocked_open--;
2677 #ifdef SERIAL_DEBUG_OPEN
2678 printk("block_til_ready after blocking: ttys%d, count = %d\n",
2679 info->line, state->count);
2680 #endif
2681 if (retval)
2682 return retval;
2683 info->flags |= ASYNC_NORMAL_ACTIVE;
2684 return 0;
2687 static int get_async_struct(int line, struct async_struct **ret_info)
2689 struct async_struct *info;
2690 struct serial_state *sstate;
2692 sstate = rs_table + line;
2693 sstate->count++;
2694 if (sstate->info) {
2695 *ret_info = sstate->info;
2696 return 0;
2698 info = kmalloc(sizeof(struct async_struct), GFP_KERNEL);
2699 if (!info) {
2700 sstate->count--;
2701 return -ENOMEM;
2703 memset(info, 0, sizeof(struct async_struct));
2704 init_waitqueue_head(&info->open_wait);
2705 init_waitqueue_head(&info->close_wait);
2706 init_waitqueue_head(&info->delta_msr_wait);
2707 info->magic = SERIAL_MAGIC;
2708 info->port = sstate->port;
2709 info->flags = sstate->flags;
2710 info->io_type = sstate->io_type;
2711 info->iomem_base = sstate->iomem_base;
2712 info->iomem_reg_shift = sstate->iomem_reg_shift;
2713 info->xmit_fifo_size = sstate->xmit_fifo_size;
2714 info->line = line;
2715 #if 0 // mask by Victor Yu. 05-19-2005
2716 info->tqueue.routine = do_softint;
2717 info->tqueue.data = info;
2718 #endif
2719 info->state = sstate;
2720 if (sstate->info) {
2721 kfree(info);
2722 *ret_info = sstate->info;
2723 return 0;
2725 *ret_info = sstate->info = info;
2726 return 0;
2730 * This routine is called whenever a serial port is opened. It
2731 * enables interrupts for a serial port, linking in its async structure into
2732 * the IRQ chain. It also performs the serial-specific
2733 * initialization for the tty structure.
2735 * Note that on failure, we don't decrement the module use count - the tty
2736 * later will call rs_close, which will decrement it for us as long as
2737 * tty->driver_data is set non-NULL. --rmk
2739 static int rs_open(struct tty_struct *tty, struct file * filp)
2741 struct async_struct *info;
2742 int retval, line;
2743 unsigned long page;
2745 MOD_INC_USE_COUNT;
2746 #if 0 // mask by Victor Yu. 05-19-2005
2747 line = MINOR(tty->device) - tty->driver.minor_start;
2748 #else // add by Victor Yu. 05-19-2005
2749 line = tty->index;
2750 #endif
2751 if ((line < 0) || (line >= NR_PORTS)) {
2752 MOD_DEC_USE_COUNT;
2753 return -ENODEV;
2755 retval = get_async_struct(line, &info);
2756 if (retval) {
2757 MOD_DEC_USE_COUNT;
2758 return retval;
2760 tty->driver_data = info;
2761 info->tty = tty;
2762 #if 0 // mask by Victor Yu. 05-18-2005
2763 if (serial_paranoia_check(info, tty->device, "rs_open"))
2764 #else // add by Victor Yu. 05-18-2005
2765 if (serial_paranoia_check(info, tty->name, "rs_open"))
2766 #endif
2767 return -ENODEV;
2769 #ifdef SERIAL_DEBUG_OPEN
2770 printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
2771 info->state->count);
2772 #endif
2773 #if (LINUX_VERSION_CODE > 0x20100)
2774 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2775 #endif
2778 * This relies on lock_kernel() stuff so wants tidying for 2.5
2780 if (!tmp_buf) {
2781 page = get_zeroed_page(GFP_KERNEL);
2782 if (!page)
2783 return -ENOMEM;
2784 if (tmp_buf)
2785 free_page(page);
2786 else
2787 tmp_buf = (unsigned char *) page;
2791 * If the port is the middle of closing, bail out now
2793 if (tty_hung_up_p(filp) ||
2794 (info->flags & ASYNC_CLOSING)) {
2795 if (info->flags & ASYNC_CLOSING)
2796 interruptible_sleep_on(&info->close_wait);
2797 #ifdef SERIAL_DO_RESTART
2798 return ((info->flags & ASYNC_HUP_NOTIFY) ?
2799 -EAGAIN : -ERESTARTSYS);
2800 #else
2801 return -EAGAIN;
2802 #endif
2806 * Start up serial port
2808 retval = startup(info);
2809 if (retval)
2810 return retval;
2812 retval = block_til_ready(tty, filp, info);
2813 if (retval) {
2814 #ifdef SERIAL_DEBUG_OPEN
2815 printk("rs_open returning after block_til_ready with %d\n",
2816 retval);
2817 #endif
2818 return retval;
2821 #if 0 // mask by Victor Yu. 05-19-2005
2822 if ((info->state->count == 1) &&
2823 (info->flags & ASYNC_SPLIT_TERMIOS)) {
2824 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2825 *tty->termios = info->state->normal_termios;
2826 else
2827 *tty->termios = info->state->callout_termios;
2828 change_speed(info, 0);
2830 #else // add by Victor Yu. 05-19-2005
2831 if ( info->state->count == 1 )
2832 change_speed(info, 0);
2833 #endif
2834 #ifdef CONFIG_SERIAL_CPE_CONSOLE
2835 if (sercons.cflag && sercons.index == line) {
2836 tty->termios->c_cflag = sercons.cflag;
2837 sercons.cflag = 0;
2838 change_speed(info, 0);
2840 #endif
2841 #if 0 // mask by Victor Yu. 05-19-2005
2842 info->session = current->session;
2843 info->pgrp = current->pgrp;
2844 #endif
2846 #ifdef SERIAL_DEBUG_OPEN
2847 printk("rs_open ttys%d successful...", info->line);
2848 #endif
2849 return 0;
2853 * /proc fs routines....
2856 static inline int line_info(char *buf, struct serial_state *state)
2858 struct async_struct *info = state->info, scr_info;
2859 char stat_buf[30], control, status;
2860 int ret;
2861 unsigned long flags;
2863 ret = sprintf(buf, "%d: uart:%s port:%lX irq:%d fifo:%d",
2864 state->line, uart_config[state->type].name,
2865 state->port, state->irq, state->xmit_fifo_size);
2867 if (!state->port || (state->type == PORT_UNKNOWN)) {
2868 ret += sprintf(buf+ret, "\n");
2869 return ret;
2873 * Figure out the current RS-232 lines
2875 if (!info) {
2876 info = &scr_info; /* This is just for serial_{in,out} */
2878 info->magic = SERIAL_MAGIC;
2879 info->port = state->port;
2880 info->flags = state->flags;
2881 info->hub6 = state->hub6;
2882 info->io_type = state->io_type;
2883 info->iomem_base = state->iomem_base;
2884 info->iomem_reg_shift = state->iomem_reg_shift;
2885 info->quot = 0;
2886 info->tty = 0;
2888 save_flags(flags); cli();
2889 status = serial_in(info, SERIAL_MSR);
2890 control = info != &scr_info ? info->MCR : serial_in(info, SERIAL_MCR);
2891 restore_flags(flags);
2893 stat_buf[0] = 0;
2894 stat_buf[1] = 0;
2895 if (control & SERIAL_MCR_RTS)
2896 strcat(stat_buf, "|RTS");
2897 if (status & SERIAL_MSR_CTS)
2898 strcat(stat_buf, "|CTS");
2899 if (control & SERIAL_MCR_DTR)
2900 strcat(stat_buf, "|DTR");
2901 if (status & SERIAL_MSR_DSR)
2902 strcat(stat_buf, "|DSR");
2903 if (status & SERIAL_MSR_DCD)
2904 strcat(stat_buf, "|CD");
2905 if (status & SERIAL_MSR_RI)
2906 strcat(stat_buf, "|RI");
2908 if (info->quot) {
2909 ret += sprintf(buf+ret, " baud:%d",
2910 state->baud_base / info->quot);
2913 ret += sprintf(buf+ret, " tx:%d rx:%d",
2914 state->icount.tx, state->icount.rx);
2916 if (state->icount.frame)
2917 ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
2919 if (state->icount.parity)
2920 ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
2922 if (state->icount.brk)
2923 ret += sprintf(buf+ret, " brk:%d", state->icount.brk);
2925 if (state->icount.overrun)
2926 ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
2929 * Last thing is the RS-232 status lines
2931 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2932 return ret;
2936 int rs_read_proc(char *page, char **start, off_t off, int count,
2937 int *eof, void *data)
2939 int i, len = 0, l;
2940 off_t begin = 0;
2942 len += sprintf(page, "serinfo:1.0 driver:%s%s revision:%s\n",
2943 serial_version, LOCAL_VERSTRING, serial_revdate);
2944 for (i = 0; i < NR_PORTS && len < 4000; i++) {
2945 l = line_info(page + len, &rs_table[i]);
2946 len += l;
2947 if (len+begin > off+count)
2948 goto done;
2949 if (len+begin < off) {
2950 begin += len;
2951 len = 0;
2954 *eof = 1;
2955 done:
2956 if (off >= len+begin)
2957 return 0;
2958 *start = page + (off-begin);
2959 return ((count < begin+len-off) ? count : begin+len-off);
2963 * ---------------------------------------------------------------------
2964 * rs_init() and friends
2966 * rs_init() is called at boot-time to initialize the serial driver.
2967 * ---------------------------------------------------------------------
2971 * This routine prints out the appropriate serial driver version
2972 * number, and identifies which options were configured into this
2973 * driver.
2975 static char serial_options[] __initdata =
2977 #ifdef CONFIG_SERIAL_SHARE_IRQ
2978 " SHARE_IRQ"
2979 #define SERIAL_OPT
2980 #endif
2981 #ifdef CONFIG_SERIAL_DETECT_IRQ
2982 " DETECT_IRQ"
2983 #define SERIAL_OPT
2984 #endif
2985 #ifdef ENABLE_SERIAL_ACPI
2986 " SERIAL_ACPI"
2987 #define SERIAL_OPT
2988 #endif
2989 #ifdef SERIAL_OPT
2990 " enabled\n";
2991 #else
2992 " no serial options enabled\n";
2993 #endif
2994 #undef SERIAL_OPT
2996 static _INLINE_ void show_serial_version(void)
2998 printk(KERN_INFO "%s version %s%s (%s) with%s", serial_name,
2999 serial_version, LOCAL_VERSTRING, serial_revdate,
3000 serial_options);
3004 * This routine detect the IRQ of a serial port by clearing OUT2 when
3005 * no UART interrupt are requested (IER = 0) (*GPL*). This seems to work at
3006 * each time, as long as no other device permanently request the IRQ.
3007 * If no IRQ is detected, or multiple IRQ appear, this function returns 0.
3008 * The variable "state" and the field "state->port" should not be null.
3010 static unsigned detect_uart_irq (struct serial_state * state)
3012 int irq;
3013 unsigned long irqs;
3014 unsigned char save_mcr, save_ier;
3015 struct async_struct scr_info; /* serial_{in,out} because HUB6 */
3017 scr_info.magic = SERIAL_MAGIC;
3018 scr_info.state = state;
3019 scr_info.port = state->port;
3020 scr_info.flags = state->flags;
3022 scr_info.io_type = state->io_type;
3023 scr_info.iomem_base = state->iomem_base;
3024 scr_info.iomem_reg_shift = state->iomem_reg_shift;
3026 /* forget possible initially masked and pending IRQ */
3027 probe_irq_off(probe_irq_on());
3028 save_mcr = serial_inp(&scr_info, SERIAL_MCR);
3029 save_ier = serial_inp(&scr_info, SERIAL_IER);
3030 serial_outp(&scr_info, SERIAL_MCR, SERIAL_MCR_OUT1 | SERIAL_MCR_OUT2);
3032 irqs = probe_irq_on();
3033 serial_outp(&scr_info, SERIAL_MCR, 0);
3034 udelay (10);
3035 if (state->flags & ASYNC_FOURPORT) {
3036 serial_outp(&scr_info, SERIAL_MCR,
3037 SERIAL_MCR_DTR | SERIAL_MCR_RTS);
3038 } else {
3039 serial_outp(&scr_info, SERIAL_MCR,
3040 SERIAL_MCR_DTR | SERIAL_MCR_RTS | SERIAL_MCR_OUT2);
3042 serial_outp(&scr_info, SERIAL_IER, 0x0f); /* enable all intrs */
3043 (void)serial_inp(&scr_info, SERIAL_LSR);
3044 (void)serial_inp(&scr_info, SERIAL_RBR);
3045 (void)serial_inp(&scr_info, SERIAL_IIR);
3046 (void)serial_inp(&scr_info, SERIAL_MSR);
3047 serial_outp(&scr_info, SERIAL_THR, 0xFF);
3048 udelay (20);
3049 irq = probe_irq_off(irqs);
3051 serial_outp(&scr_info, SERIAL_MCR, save_mcr);
3052 serial_outp(&scr_info, SERIAL_IER, save_ier);
3054 return (irq > 0)? irq : 0;
3057 #ifdef not_complete_yet
3059 * This is a quickie test to see how big the FIFO is.
3060 * It doesn't work at all the time, more's the pity.
3062 static int size_fifo(struct async_struct *info)
3064 unsigned char old_fcr, old_mcr, old_dll, old_dlm;
3065 int count;
3067 old_fcr = serial_inp(info, SERIAL_FCR);
3068 old_mcr = serial_inp(info, SERIAL_MCR);
3069 serial_outp(info, SERIAL_FCR, SERIAL_FCR_FE |
3070 SERIAL_FCR_RXFR | SERIAL_FCR_TXFR);
3071 serial_outp(info, SERIAL_MCR, SERIAL_MCR_LPBK);
3072 serial_outp(info, SERIAL_LCR, SERIAL_LCR_DLAB);
3073 old_dll = serial_inp(info, SERIAL_DLL);
3074 old_dlm = serial_inp(info, SERIAL_DLM);
3075 serial_outp(info, SERIAL_DLL, 0x01);
3076 serial_outp(info, SERIAL_DLM, 0x00);
3077 serial_outp(info, SERIAL_LCR, 0x03);
3078 for (count = 0; count < 256; count++)
3079 serial_outp(info, SERIAL_THR, count);
3080 mdelay(20);
3081 for (count = 0; (serial_inp(info, SERIAL_LSR) & SERIAL_LSR_DR) &&
3082 (count < 256); count++)
3083 serial_inp(info, SERIAL_RBR);
3084 serial_outp(info, SERIAL_FCR, old_fcr);
3085 serial_outp(info, SERIAL_MCR, old_mcr);
3086 serial_outp(info, SERIAL_LCR, SERIAL_LCR_DLAB);
3087 serial_outp(info, SERIAL_DLL, old_dll);
3088 serial_outp(info, SERIAL_DLM, old_dlm);
3090 return count;
3092 #endif /* end_of_not */
3097 * This routine is called by rs_init() to initialize a specific serial
3098 * port. It determines what type of UART chip this serial port is
3099 * using: 8250, 16450, 16550, 16550A. The important question is
3100 * whether or not this UART is a 16550A or not, since this will
3101 * determine whether or not we can use its FIFO features or not.
3103 static void autoconfig(struct serial_state * state)
3105 unsigned char status1, status2, scratch, scratch2, scratch3;
3106 unsigned char save_lcr, save_mcr;
3107 struct async_struct *info, scr_info;
3108 unsigned long flags;
3110 state->type = PORT_UNKNOWN;
3112 #ifdef SERIAL_DEBUG_AUTOCONF
3113 printk("Testing ttyS%d (0x%04lx, 0x%04x)...\n", state->line,
3114 state->port, (unsigned) state->iomem_base);
3115 #endif
3117 if (!CONFIGURED_SERIAL_PORT(state))
3118 return;
3120 info = &scr_info; /* This is just for serial_{in,out} */
3122 info->magic = SERIAL_MAGIC;
3123 info->state = state;
3124 info->port = state->port;
3125 info->flags = state->flags;
3127 info->io_type = state->io_type;
3128 info->iomem_base = state->iomem_base;
3129 info->iomem_reg_shift = state->iomem_reg_shift;
3131 save_flags(flags); cli();
3133 if (!(state->flags & ASYNC_BUGGY_UART) &&
3134 !state->iomem_base) {
3136 * Do a simple existence test first; if we fail this,
3137 * there's no point trying anything else.
3139 * 0x80 is used as a nonsense port to prevent against
3140 * false positives due to ISA bus float. The
3141 * assumption is that 0x80 is a non-existent port;
3142 * which should be safe since include/asm/io.h also
3143 * makes this assumption.
3145 scratch = serial_inp(info, SERIAL_IER);
3146 serial_outp(info, SERIAL_IER, 0);
3148 scratch2 = serial_inp(info, SERIAL_IER);
3149 serial_outp(info, SERIAL_IER, 0x0F);
3151 scratch3 = serial_inp(info, SERIAL_IER);
3152 serial_outp(info, SERIAL_IER, scratch);
3153 if (scratch2 || scratch3 != 0x0F) {
3154 #ifdef SERIAL_DEBUG_AUTOCONF
3155 printk("serial: ttyS%d: simple autoconfig failed "
3156 "(%02x, %02x)\n", state->line,
3157 scratch2, scratch3);
3158 #endif
3159 restore_flags(flags);
3160 return; /* We failed; there's nothing here */
3164 save_mcr = serial_in(info, SERIAL_MCR);
3165 save_lcr = serial_in(info, SERIAL_LCR);
3168 * Check to see if a UART is really there. Certain broken
3169 * internal modems based on the Rockwell chipset fail this
3170 * test, because they apparently don't implement the loopback
3171 * test mode. So this test is skipped on the COM 1 through
3172 * COM 4 ports. This *should* be safe, since no board
3173 * manufacturer would be stupid enough to design a board
3174 * that conflicts with COM 1-4 --- we hope!
3176 if (!(state->flags & ASYNC_SKIP_TEST)) {
3177 serial_outp(info, SERIAL_MCR, SERIAL_MCR_LPBK | 0x0A);
3178 status1 = serial_inp(info, SERIAL_MSR) & 0xF0;
3179 serial_outp(info, SERIAL_MCR, save_mcr);
3180 if (status1 != 0x90) {
3181 #ifdef SERIAL_DEBUG_AUTOCONF
3182 printk("serial: ttyS%d: no UART loopback failed\n",
3183 state->line);
3184 #endif
3185 restore_flags(flags);
3186 return;
3189 serial_outp(info, SERIAL_LCR, 0xBF); /* set up for StarTech test */
3190 serial_outp(info, SERIAL_EFR, 0); /* EFR is the same as FCR */
3191 serial_outp(info, SERIAL_LCR, 0);
3192 serial_outp(info, SERIAL_FCR, SERIAL_FCR_FE);
3193 scratch = serial_in(info, SERIAL_IIR) >> 6;
3194 switch (scratch) {
3195 case 0:
3196 state->type = PORT_16450;
3197 break;
3198 case 1:
3199 state->type = PORT_UNKNOWN;
3200 break;
3201 case 2:
3202 state->type = PORT_16550;
3203 break;
3204 case 3:
3205 state->type = PORT_16550A;
3206 break;
3211 serial_outp(info, SERIAL_LCR, save_lcr);
3212 if (state->type == PORT_16450) {
3213 scratch = serial_in(info, SERIAL_SPR);
3214 serial_outp(info, SERIAL_SPR, 0xa5);
3215 status1 = serial_in(info, SERIAL_SPR);
3216 serial_outp(info, SERIAL_SPR, 0x5a);
3217 status2 = serial_in(info, SERIAL_SPR);
3218 serial_outp(info, SERIAL_SPR, scratch);
3220 if ((status1 != 0xa5) || (status2 != 0x5a))
3221 state->type = PORT_8250;
3223 state->xmit_fifo_size = uart_config[state->type].dfl_xmit_fifo_size;
3224 //ivan
3225 printk("state->type=%s fifo size=%d\r\n",uart_config[state->type].name,state->xmit_fifo_size);
3227 if (state->type == PORT_UNKNOWN) {
3228 restore_flags(flags);
3229 return;
3232 if (info->port) {
3233 //request_region(info->port,8,"serial(auto)");
3234 request_region(info->port, 0x40, "serial(auto)");
3238 * Reset the UART.
3240 serial_outp(info, SERIAL_MCR, save_mcr);
3241 serial_outp(info, SERIAL_FCR, (SERIAL_FCR_FE |
3242 SERIAL_FCR_RXFR |
3243 SERIAL_FCR_TXFR));
3244 serial_outp(info, SERIAL_FCR, 0);
3245 (void)serial_in(info, SERIAL_RBR);
3246 serial_outp(info, SERIAL_IER, 0);
3248 restore_flags(flags);
3252 int register_serial(struct serial_struct *req);
3253 void unregister_serial(int line);
3255 #if (LINUX_VERSION_CODE > 0x20100)
3256 EXPORT_SYMBOL(register_serial);
3257 EXPORT_SYMBOL(unregister_serial);
3258 #else
3259 static struct symbol_table serial_syms = {
3260 #include <linux/symtab_begin.h>
3261 X(register_serial),
3262 X(unregister_serial),
3263 #include <linux/symtab_end.h>
3265 #endif
3273 * The serial driver boot-time initialization code!
3275 static int __init rs_init(void)
3277 int i;
3278 struct serial_state * state;
3279 //printk("rs_init\r\n");
3280 #if 0 // mask by Victor Yu. 05-19-2005
3281 init_bh(SERIAL_BH, do_serial_bh);
3282 #endif
3283 init_timer(&serial_timer);
3284 serial_timer.function = rs_timer;
3285 mod_timer(&serial_timer, jiffies + RS_STROBE_TIME);
3287 for (i = 0; i < NR_IRQS; i++) {
3288 IRQ_ports[i] = 0;
3289 IRQ_timeout[i] = 0;
3291 #ifdef CONFIG_SERIAL_CPE_CONSOLE
3293 * The interrupt of the serial console port
3294 * can't be shared.
3296 if (sercons.flags & CON_CONSDEV) {
3297 for(i = 0; i < NR_PORTS; i++)
3298 if (i != sercons.index &&
3299 rs_table[i].irq == rs_table[sercons.index].irq)
3300 rs_table[i].irq = 0;
3302 #endif
3303 show_serial_version();
3305 /* Initialize the tty_driver structure */
3307 memset(&serial_driver, 0, sizeof(struct tty_driver));
3308 serial_driver.magic = TTY_DRIVER_MAGIC;
3309 #if (LINUX_VERSION_CODE > 0x20100)
3310 serial_driver.driver_name = "serial";
3311 #endif
3312 #if (LINUX_VERSION_CODE > 0x2032D && defined(CONFIG_DEVFS_FS))
3313 serial_driver.name = "tts/%d";
3314 #else
3315 serial_driver.name = "ttyS";
3316 #endif
3317 serial_driver.major = TTY_MAJOR;
3318 serial_driver.minor_start = 64 + SERIAL_DEV_OFFSET;
3319 serial_driver.num = NR_PORTS;
3320 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
3321 serial_driver.subtype = SERIAL_TYPE_NORMAL;
3322 serial_driver.init_termios = tty_std_termios;
3323 serial_driver.init_termios.c_cflag =
3324 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
3325 serial_driver.flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
3326 #if 0 // mask by Victor Yu. 05-19-2005
3327 serial_driver.refcount = &serial_refcount;
3328 serial_driver.table = serial_table;
3329 #endif
3330 serial_driver.termios = serial_termios;
3331 serial_driver.termios_locked = serial_termios_locked;
3333 serial_driver.open = rs_open;
3334 serial_driver.close = rs_close;
3335 serial_driver.write = rs_write;
3336 serial_driver.put_char = rs_put_char;
3337 serial_driver.flush_chars = rs_flush_chars;
3338 serial_driver.write_room = rs_write_room;
3339 serial_driver.chars_in_buffer = rs_chars_in_buffer;
3340 serial_driver.flush_buffer = rs_flush_buffer;
3341 serial_driver.ioctl = rs_ioctl;
3342 serial_driver.throttle = rs_throttle;
3343 serial_driver.unthrottle = rs_unthrottle;
3344 serial_driver.set_termios = rs_set_termios;
3345 serial_driver.stop = rs_stop;
3346 serial_driver.start = rs_start;
3347 serial_driver.hangup = rs_hangup;
3348 #if (LINUX_VERSION_CODE >= 131394) /* Linux 2.1.66 */
3349 serial_driver.break_ctl = rs_break;
3350 #endif
3351 #if (LINUX_VERSION_CODE >= 131343)
3352 serial_driver.send_xchar = rs_send_xchar;
3353 serial_driver.wait_until_sent = rs_wait_until_sent;
3354 serial_driver.read_proc = rs_read_proc;
3355 #endif
3357 #if 0 // mask by Victor Yu. 05-19-2005
3359 * The callout device is just like normal device except for
3360 * major number and the subtype code.
3362 callout_driver = serial_driver;
3363 #if (LINUX_VERSION_CODE > 0x2032D && defined(CONFIG_DEVFS_FS))
3364 callout_driver.name = "cua/%d";
3365 #else
3366 callout_driver.name = "cua";
3367 #endif
3368 callout_driver.major = TTYAUX_MAJOR;
3369 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
3370 #if (LINUX_VERSION_CODE >= 131343)
3371 callout_driver.read_proc = 0;
3372 callout_driver.proc_entry = 0;
3373 #endif
3374 #endif
3376 if (tty_register_driver(&serial_driver))
3377 panic("Couldn't register serial driver\n");
3378 #if 0 // mask by Victor Yu. 05-19-2005
3379 if (tty_register_driver(&callout_driver))
3380 panic("Couldn't register callout driver\n");
3381 #endif
3384 // ªì©l¤Æ serial_state
3385 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++)
3387 state->magic = SSTATE_MAGIC;
3388 state->line = i;
3389 #if 0 // mask by Victor Yu. 05-19-2005
3390 state->type = PORT_FARADAY; //state->type = PORT_UNKNOWN;
3391 #else // add by Victor Yu. 05-19-2005
3392 state->type = PORT_UNKNOWN;
3393 #endif
3394 state->custom_divisor = 0;
3395 state->close_delay = 5*HZ/10;
3396 state->closing_wait = 30*HZ;
3397 #if 0 // mask by Victor Yu. 05-19-2005
3398 state->callout_termios = callout_driver.init_termios;
3399 state->normal_termios = serial_driver.init_termios;
3400 #endif
3401 state->icount.cts = state->icount.dsr =
3402 state->icount.rng = state->icount.dcd = 0;
3403 state->icount.rx = state->icount.tx = 0;
3404 state->icount.frame = state->icount.parity = 0;
3405 state->icount.overrun = state->icount.brk = 0;
3406 #if 0 // mask by Victor yu. 05-19-2005
3407 state->irq = irq_cannonicalize(state->irq);
3408 #endif
3409 if (state->hub6)
3410 state->io_type = SERIAL_IO_HUB6;
3411 #if 0 // mask by Victor Yu. 05-19-2005
3412 if (state->port && check_region(state->port, 0x40))
3413 continue;
3414 #endif
3416 if (state->flags & ASYNC_BOOT_AUTOCONF)
3417 autoconfig(state);
3420 // µù¥U devfs
3421 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++)
3423 if (state->type == PORT_UNKNOWN)
3424 continue;
3425 if ( (state->flags & ASYNC_BOOT_AUTOCONF)
3426 && (state->flags & ASYNC_AUTO_IRQ)
3427 && (state->port != 0 || state->iomem_base != 0))
3428 state->irq = detect_uart_irq(state);
3429 if (state->io_type == SERIAL_IO_MEM) {
3430 printk(KERN_INFO"ttyS%02d%s at 0x%px (irq = %d) is a %s\n",
3431 //printk(KERN_INFO"ttyAM%d%s at 0x%px (irq = %d) is a %s\n",
3432 state->line + SERIAL_DEV_OFFSET,
3433 (state->flags & ASYNC_FOURPORT) ? " FourPort" : "",
3434 state->iomem_base, state->irq,
3435 uart_config[state->type].name);
3437 else {
3438 printk(KERN_INFO "ttyS%02d%s at 0x%04lx (irq = %d) is a %s\n",
3439 // printk(KERN_INFO "ttyAM%d%s at 0x%04lx (irq = %d) is a %s\n",
3440 state->line + SERIAL_DEV_OFFSET,
3441 (state->flags & ASYNC_FOURPORT) ? " FourPort" : "",
3442 state->port, state->irq,
3443 uart_config[state->type].name);
3445 #if 0 // mask by Victor Yu. 05-19-2005
3446 tty_register_devfs(&serial_driver, 0,
3447 serial_driver.minor_start + state->line);
3448 tty_register_devfs(&callout_driver, 0,
3449 callout_driver.minor_start + state->line);
3450 #endif
3452 return 0;
3455 #if 0 // mask by Victor Yu. 05-19-2005
3456 #ifdef not_complete_yet
3458 * This is for use by architectures that know their serial console
3459 * attributes only at run time. Not to be invoked after rs_init().
3461 int __init early_serial_setup(struct serial_struct *req)
3463 int i = req->line;
3465 if (i >= NR_IRQS)
3466 return(-ENOENT);
3467 rs_table[i].magic = 0;
3468 rs_table[i].baud_base = req->baud_base;
3469 rs_table[i].port = req->port;
3470 if (HIGH_BITS_OFFSET)
3471 rs_table[i].port += (unsigned long) req->port_high <<
3472 HIGH_BITS_OFFSET;
3473 rs_table[i].irq = req->irq;
3474 rs_table[i].flags = req->flags;
3475 rs_table[i].close_delay = req->close_delay;
3476 rs_table[i].io_type = req->io_type;
3477 rs_table[i].hub6 = req->hub6;
3478 rs_table[i].iomem_base = req->iomem_base;
3479 rs_table[i].iomem_reg_shift = req->iomem_reg_shift;
3480 rs_table[i].type = req->type;
3481 rs_table[i].xmit_fifo_size = req->xmit_fifo_size;
3482 rs_table[i].custom_divisor = req->custom_divisor;
3483 rs_table[i].closing_wait = req->closing_wait;
3484 return(0);
3486 #endif /* end_of_not */
3487 #endif
3491 * register_serial and unregister_serial allows for 16x50 serial ports to be
3492 * configured at run-time, to support PCMCIA modems.
3496 * register_serial - configure a 16x50 serial port at runtime
3497 * @req: request structure
3499 * Configure the serial port specified by the request. If the
3500 * port exists and is in use an error is returned. If the port
3501 * is not currently in the table it is added.
3503 * The port is then probed and if neccessary the IRQ is autodetected
3504 * If this fails an error is returned.
3506 * On success the port is ready to use and the line number is returned.
3509 int register_serial(struct serial_struct *req)
3511 int i;
3512 unsigned long flags;
3513 struct serial_state *state;
3514 struct async_struct *info;
3515 unsigned long port;
3517 port = req->port;
3518 if (HIGH_BITS_OFFSET)
3519 port += (unsigned long) req->port_high << HIGH_BITS_OFFSET;
3521 save_flags(flags); cli();
3522 for (i = 0; i < NR_PORTS; i++) {
3523 if ((rs_table[i].port == port) &&
3524 (rs_table[i].iomem_base == req->iomem_base))
3525 break;
3528 if (i == NR_PORTS) {
3529 for (i = 0; i < NR_PORTS; i++)
3530 if ((rs_table[i].type == PORT_UNKNOWN) &&
3531 (rs_table[i].count == 0))
3532 break;
3534 if (i == NR_PORTS) {
3535 restore_flags(flags);
3536 return -1;
3538 state = &rs_table[i];
3539 if (rs_table[i].count) {
3540 restore_flags(flags);
3541 printk("Couldn't configure serial #%d (port=%ld,irq=%d): "
3542 "device already open\n", i, port, req->irq);
3543 return -1;
3545 state->irq = req->irq;
3546 state->port = port;
3547 state->flags = req->flags;
3548 state->io_type = req->io_type;
3549 state->iomem_base = req->iomem_base;
3550 state->iomem_reg_shift = req->iomem_reg_shift;
3551 if (req->baud_base)
3552 state->baud_base = req->baud_base;
3553 if ((info = state->info) != NULL) {
3554 info->port = port;
3555 info->flags = req->flags;
3556 info->io_type = req->io_type;
3557 info->iomem_base = req->iomem_base;
3558 info->iomem_reg_shift = req->iomem_reg_shift;
3560 autoconfig(state);
3561 if (state->type == PORT_UNKNOWN) {
3562 restore_flags(flags);
3563 printk("register_serial(): autoconfig failed\n");
3564 return -1;
3566 restore_flags(flags);
3568 if ((state->flags & ASYNC_AUTO_IRQ) && CONFIGURED_SERIAL_PORT(state))
3569 state->irq = detect_uart_irq(state);
3571 // printk(KERN_INFO "ttyAM%d at %s 0x%04lx (irq = %d) is a %s\n",
3572 printk(KERN_INFO "ttyS%02d at %s 0x%04lx (irq = %d) is a %s\n",
3573 state->line + SERIAL_DEV_OFFSET,
3574 state->iomem_base ? "iomem" : "port",
3575 state->iomem_base ? (unsigned long)state->iomem_base :
3576 state->port, state->irq, uart_config[state->type].name);
3577 #if 0 // mask by Victor Yu. 05-19-2005
3578 tty_register_devfs(&serial_driver, 0,
3579 serial_driver.minor_start + state->line);
3580 tty_register_devfs(&callout_driver, 0,
3581 callout_driver.minor_start + state->line);
3582 #endif
3583 return state->line + SERIAL_DEV_OFFSET;
3587 * unregister_serial - deconfigure a 16x50 serial port
3588 * @line: line to deconfigure
3590 * The port specified is deconfigured and its resources are freed. Any
3591 * user of the port is disconnected as if carrier was dropped. Line is
3592 * the port number returned by register_serial().
3595 void unregister_serial(int line)
3597 unsigned long flags;
3598 struct serial_state *state = &rs_table[line];
3600 save_flags(flags); cli();
3601 if (state->info && state->info->tty)
3602 tty_hangup(state->info->tty);
3603 state->type = PORT_UNKNOWN;
3604 printk(KERN_INFO "tty%02d unloaded\n", state->line);
3605 /* These will be hidden, because they are devices that will no longer
3606 * be available to the system. (ie, PCMCIA modems, once ejected)
3608 #if 0 // mask by Victor Yu. 05-19-2005
3609 tty_unregister_devfs(&serial_driver,
3610 serial_driver.minor_start + state->line);
3611 tty_unregister_devfs(&callout_driver,
3612 callout_driver.minor_start + state->line);
3613 #endif
3614 restore_flags(flags);
3617 static void __exit rs_fini(void)
3619 unsigned long flags;
3620 int e1, e2;
3621 int i;
3622 struct async_struct *info;
3624 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
3625 del_timer_sync(&serial_timer);
3626 save_flags(flags); cli();
3627 #if 0 // mask by Victor Yu. 05-19-2005
3628 remove_bh(SERIAL_BH);
3629 #endif
3630 if ((e1 = tty_unregister_driver(&serial_driver)))
3631 printk("serial: failed to unregister serial driver (%d)\n",
3632 e1);
3633 if ((e2 = tty_unregister_driver(&callout_driver)))
3634 printk("serial: failed to unregister callout driver (%d)\n",
3635 e2);
3636 restore_flags(flags);
3638 for (i = 0; i < NR_PORTS; i++) {
3639 if ((info = rs_table[i].info)) {
3640 rs_table[i].info = NULL;
3641 kfree(info);
3643 if ((rs_table[i].type != PORT_UNKNOWN) && rs_table[i].port) {
3644 release_region(rs_table[i].port, 8);
3649 if (tmp_buf) {
3650 unsigned long pg = (unsigned long) tmp_buf;
3651 tmp_buf = NULL;
3652 free_page(pg);
3657 module_init(rs_init);
3658 module_exit(rs_fini);
3659 MODULE_DESCRIPTION("Standard/generic (dumb) serial driver");
3660 MODULE_AUTHOR("Theodore Ts'o <tytso@mit.edu>");
3661 MODULE_LICENSE("GPL");
3665 * ------------------------------------------------------------
3666 * Serial console driver
3667 * ------------------------------------------------------------
3669 #ifdef CONFIG_SERIAL_CPE_CONSOLE
3671 #define BOTH_EMPTY (SERIAL_LSR_TE | SERIAL_LSR_THRE)
3673 static struct async_struct async_sercons;
3676 * Wait for transmitter & holding register to empty
3678 static inline void wait_for_xmitr(struct async_struct *info)
3680 unsigned int status, tmout = 1000000;
3682 do {
3683 status = serial_in(info, SERIAL_LSR);
3685 if (status & SERIAL_LSR_BI)
3686 lsr_break_flag = SERIAL_LSR_BI;
3688 if (--tmout == 0)
3689 break;
3690 } while((status & BOTH_EMPTY) != BOTH_EMPTY);
3692 /* Wait for flow control if necessary */
3693 if (info->flags & ASYNC_CONS_FLOW) {
3694 tmout = 1000000;
3695 while (--tmout &&
3696 ((serial_in(info, SERIAL_MSR) & SERIAL_MSR_CTS) == 0));
3702 * Print a string to the serial port trying not to disturb
3703 * any possible real use of the port...
3705 * The console must be locked when we get here.
3707 static void serial_console_write(struct console *co, const char *s,
3708 unsigned count)
3710 static struct async_struct *info = &async_sercons;
3711 int ier;
3712 unsigned i;
3715 * First save the IER then disable the interrupts
3717 ier = serial_in(info, SERIAL_IER);
3718 serial_out(info, SERIAL_IER, 0x00);
3721 * Now, do each character
3723 for (i = 0; i < count; i++, s++) {
3724 wait_for_xmitr(info);
3727 * Send the character out.
3728 * If a LF, also do CR...
3730 serial_out(info, SERIAL_THR, *s);
3731 if (*s == 10) {
3732 wait_for_xmitr(info);
3733 serial_out(info, SERIAL_THR, 13);
3738 * Finally, Wait for transmitter & holding register to empty
3739 * and restore the IER
3741 wait_for_xmitr(info);
3742 serial_out(info, SERIAL_IER, ier);
3745 #ifdef not_complete_yet
3747 * Receive character from the serial port
3749 static int serial_console_wait_key(struct console *co)
3751 static struct async_struct *info;
3752 int ier, c;
3754 info = &async_sercons;
3757 * First save the IER then disable the interrupts so
3758 * that the real driver for the port does not get the
3759 * character.
3761 ier = serial_in(info, SERIAL_IER);
3762 serial_out(info, SERIAL_IER, 0x00);
3764 while ((serial_in(info, SERIAL_LSR) & SERIAL_LSR_DR) == 0);
3765 c = serial_in(info, SERIAL_RBR);
3768 * Restore the interrupts
3770 serial_out(info, SERIAL_IER, ier);
3772 return c;
3774 #endif /* end_of_not */
3776 #if 0 // mask by Victor Yu. 05-19-2005
3777 static kdev_t serial_console_device(struct console *c)
3778 #else // add by Victor Yu. 05-19-2005
3779 static dev_t serial_console_device(struct console *c)
3780 #endif
3782 return MKDEV(TTY_MAJOR, 64 + c->index);
3786 * Setup initial baud/bits/parity/flow control. We do two things here:
3787 * - construct a cflag setting for the first rs_open()
3788 * - initialize the serial port
3789 * Return non-zero if we didn't find a serial port.
3791 static int __init serial_console_setup(struct console *co, char *options)
3793 static struct async_struct *info;
3794 struct serial_state *state;
3795 unsigned cval;
3796 int baud = CONFIG_SERIAL_CPE_DEFAULT_BAUD;
3797 int bits = 8;
3798 int parity = 'n';
3799 int doflow = 0;
3800 int cflag = CREAD | HUPCL | CLOCAL;
3801 int quot = 0;
3802 char *s;
3805 if (options)
3807 baud = simple_strtoul(options, NULL, 10);
3808 s = options;
3809 while(*s >= '0' && *s <= '9')
3810 s++;
3811 if (*s) parity = *s++;
3812 if (*s) bits = *s++ - '0';
3813 if (*s) doflow = (*s++ == 'r');
3818 * Now construct a cflag setting.
3820 switch(baud)
3822 case 1200:
3823 cflag |= B1200;
3824 break;
3825 case 2400:
3826 cflag |= B2400;
3827 break;
3828 case 4800:
3829 cflag |= B4800;
3830 break;
3831 case 19200:
3832 cflag |= B19200;
3833 break;
3834 case 38400:
3835 cflag |= B38400;
3836 break;
3837 case 57600:
3838 cflag |= B57600;
3839 break;
3840 case 115200:
3841 cflag |= B115200;
3842 break;
3843 case 9600:
3844 default:
3845 cflag |= B9600;
3847 * Set this to a sane value to prevent a divide error
3849 baud = 9600;
3850 break;
3852 switch(bits) {
3853 case 7:
3854 cflag |= CS7;
3855 break;
3856 default:
3857 case 8:
3858 cflag |= CS8;
3859 break;
3861 switch(parity) {
3862 case 'o': case 'O':
3863 cflag |= PARODD;
3864 break;
3865 case 'e': case 'E':
3866 cflag |= PARENB;
3867 break;
3869 co->cflag = cflag;
3872 * Divisor, bytesize and parity
3874 state = rs_table + co->index;
3875 if (doflow)
3876 state->flags |= ASYNC_CONS_FLOW;
3877 info = &async_sercons;
3878 info->magic = SERIAL_MAGIC;
3879 info->state = state;
3880 info->port = state->port;
3881 info->flags = state->flags;
3883 info->io_type = state->io_type;
3884 info->iomem_base = state->iomem_base;
3885 info->iomem_reg_shift = state->iomem_reg_shift;
3886 quot = state->baud_base / baud;
3887 cval = cflag & (CSIZE | CSTOPB);
3888 cval >>= 4;
3889 if (cflag & PARENB)
3891 cval |= SERIAL_LCR_PE;
3893 if (!(cflag & PARODD))
3895 cval |= SERIAL_LCR_EVEN;
3900 * Disable UART interrupts, set DTR and RTS high
3901 * and set speed.
3903 serial_out(info, SERIAL_LCR, cval | SERIAL_LCR_DLAB); /* set DLAB */
3904 serial_out(info, SERIAL_DLL, quot & 0xff); /* LS of divisor */
3905 serial_out(info, SERIAL_DLM, quot >> 8); /* MS of divisor */
3906 serial_out(info, SERIAL_LCR, cval); /* reset DLAB */
3907 serial_out(info, SERIAL_IER, 0);
3908 serial_out(info, SERIAL_MCR, SERIAL_MCR_DTR | SERIAL_MCR_RTS);
3911 * If we read 0xff from the LSR, there is no UART here.
3913 if (serial_in(info, SERIAL_LSR) == 0xff)
3914 return -1;
3916 return 0;
3919 static struct console sercons = {
3920 name: "ttyS",
3921 write: serial_console_write,
3922 device: serial_console_device,
3923 /// wait_key: serial_console_wait_key,
3924 setup: serial_console_setup,
3925 flags: CON_PRINTBUFFER,
3926 index: -1,
3930 * Register console.
3932 void __init cpe_console_init(void)
3934 register_console(&sercons);
3936 #endif
3939 Local variables:
3940 compile-command: "gcc -D__KERNEL__ -I../../include -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -fno-strict-aliasing -pipe -fno-strength-reduce -march=i586 -DMODULE -DMODVERSIONS -include ../../include/linux/modversions.h -DEXPORT_SYMTAB -c serial.c"
3941 End: