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
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
35 #undef CONFIG_SERIAL_CPE_CONSOLE
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>
52 #include "serial_local.h"
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 ""
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>
74 #include <linux/slab.h>
75 #if (LINUX_VERSION_CODE >= 131343)
76 #include <linux/init.h>
78 #if (LINUX_VERSION_CODE >= 131336)
79 #include <asm/uaccess.h>
81 #include <linux/delay.h>
82 #ifdef CONFIG_SERIAL_CPE_CONSOLE
83 #include <linux/console.h>
85 #ifdef CONFIG_MAGIC_SYSRQ
86 #include <linux/sysrq.h>
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"
97 #include <asm/system.h>
100 #include <asm/bitops.h>
102 #ifdef CONFIG_MAC_SERIAL
103 #define SERIAL_DEV_OFFSET 2
105 #define SERIAL_DEV_OFFSET 0
109 #define _INLINE_ inline
114 static char *serial_name
= "Serial driver";
116 #if 0 // mask by Victor Yu. 05-19-2005
117 static DECLARE_TASK_QUEUE(tq_serial
);
120 static struct tty_driver serial_driver
, callout_driver
;
121 static int serial_refcount
;
123 static struct timer_list serial_timer
;
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
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
;
148 #if defined(CONFIG_SERIAL_CPE_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
149 static unsigned long break_pressed
; /* break, really ... */
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
161 // name, fifosize, flags
162 static struct serial_uart_config uart_config
[] = {
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
|
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
|
176 { "XR16850", 128, UART_CLEAR_FIFO
| UART_USE_FIFO
|
178 { "RSA", 2048, UART_CLEAR_FIFO
| UART_USE_FIFO
},
179 //{ "FaradayUART", 1, UART_CLEAR_FIFO},
180 { "FaradayUART", 16, UART_CLEAR_FIFO
|UART_USE_FIFO
},
186 static struct serial_state rs_table
[RS_TABLE_SIZE
] = {
190 #define NR_PORTS (sizeof(rs_table)/sizeof(struct serial_state))
194 #define PREPARE_FUNC(dev) (dev->prepare)
195 #define ACTIVATE_FUNC(dev) (dev->activate)
196 #define DEACTIVATE_FUNC(dev) (dev->deactivate)
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)
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
;
224 static DECLARE_MUTEX(tmp_buf_sem
);
226 static struct semaphore tmp_buf_sem
= MUTEX
;
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
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
)
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";
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
);
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
);
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
,
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
285 #define serial_inp(info, offset) serial_in(info, offset)
286 #define serial_outp(info, offset, value) serial_out(info, offset, value)
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
)
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
);
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
;
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"))
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
;
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"))
353 save_flags(flags
); cli();
354 if (info
->xmit
.head
!= info
->xmit
.tail
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
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
,
396 info
->event
|= 1 << event
;
397 queue_task(&info
->tqueue
, &tq_serial
);
402 static _INLINE_
void receive_chars(struct async_struct
*info
,
403 int *status
, struct pt_regs
* regs
)
405 struct tty_struct
*tty
= info
->tty
;
407 struct async_icount
*icount
;
411 icount
= &info
->state
->icount
;
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
);
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
;
424 #ifdef SERIAL_DEBUG_INTR
425 printk("DR%02x:%02x...", ch
, *status
);
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
);
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
;
451 if (info
->flags
& ASYNC_SAK
)
453 } else if (*status
& SERIAL_LSR_PE
)
455 else if (*status
& SERIAL_LSR_FE
)
457 if (*status
& SERIAL_LSR_OE
)
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
;
472 if (*status
& (SERIAL_LSR_BI
)) {
473 #ifdef SERIAL_DEBUG_INTR
474 printk("handling break....");
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
) {
485 time_before(jiffies
, break_pressed
+ HZ
*5)) {
486 handle_sysrq(ch
, regs
, NULL
, NULL
);
493 if ((*status
& info
->ignore_status_mask
) == 0) {
494 tty
->flip
.flag_buf_ptr
++;
495 tty
->flip
.char_buf_ptr
++;
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
505 *tty
->flip
.flag_buf_ptr
= TTY_OVERRUN
;
507 tty
->flip
.flag_buf_ptr
++;
508 tty
->flip
.char_buf_ptr
++;
510 #if defined(CONFIG_SERIAL_CPE_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
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
);
518 queue_task_irq_off(&tty
->flip
.tqueue
, &tq_timer
);
522 static _INLINE_
void transmit_chars(struct async_struct
*info
, int *intr_done
)
528 serial_outp(info
, SERIAL_THR
, info
->x_char
);
529 info
->state
->icount
.tx
++;
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
);
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
;
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
)
561 } while (--count
> 0);
563 if (CIRC_CNT(info
->xmit
.head
,
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
);
572 #ifdef SERIAL_DEBUG_INTR
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
)
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
)
596 if (status
& SERIAL_MSR_DELTADSR
)
598 if (status
& SERIAL_MSR_DELTACD
) {
600 #ifdef CONFIG_HARD_PPS
601 if ((info
->flags
& ASYNC_HARDPPS_CD
) &&
602 (status
& SERIAL_MSR_DCD
))
606 if (status
& SERIAL_MSR_DELTACTS
)
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");
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...");
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
);
631 tty_hangup(info
->tty
);
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...");
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
);
650 if (!(status
& SERIAL_MSR_CTS
)) {
651 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
652 printk("CTS tx stop...");
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
)
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
);
682 info
= IRQ_ports
[irq
];
684 #if 0 // mask by Victor Yu. 05-19-2005
686 #else // add by Victor Yu. 05-19-2005
692 (serial_in(info
, SERIAL_IIR
) & SERIAL_IIR_NONE
)) {
697 #ifdef SERIAL_DEBUG_INTR
698 printk("IIR = %x...", serial_in(info
, SERIAL_IIR
));
702 info
->last_active
= jiffies
;
704 status
= serial_inp(info
, SERIAL_LSR
);
705 #ifdef SERIAL_DEBUG_INTR
706 printk("status = %x...", status
);
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);
715 info
= info
->next_port
;
717 info
= IRQ_ports
[irq
];
718 if (pass_counter
++ > RS_ISR_PASS_LIMIT
) {
720 printk("rs loop break\n");
722 break; /* Prevent infinite loops */
726 } while (end_mark
!= info
);
728 #ifdef SERIAL_DEBUG_INTR
731 #if 1 // add by Victor Yu. 05-19-2005
732 return IRQ_RETVAL(1);
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
)
748 int pass_counter
= 0;
749 struct async_struct
* info
;
751 #ifdef SERIAL_DEBUG_INTR
752 printk("rs_interrupt_single(%d)...", irq
);
755 info
= IRQ_ports
[irq
];
756 if (!info
|| !info
->tty
)
757 #if 0 // mask by Vicor Yu. 05-19-2005
759 #else // add by Victor Yu. 05-19-2005
765 status
= serial_inp(info
, SERIAL_LSR
);
766 #ifdef SERIAL_DEBUG_INTR
767 printk("status = %x...", status
);
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
) {
776 printk("rs_single loop break.\n");
780 #ifdef SERIAL_DEBUG_INTR
781 printk("IIR = %x...", serial_in(info
, SERIAL_IIR
));
783 } while (!(serial_in(info
, SERIAL_IIR
) & SERIAL_IIR_NONE
));
784 info
->last_active
= jiffies
;
786 #ifdef SERIAL_DEBUG_INTR
789 #if 1 // add by Victor Yu. 05-19-2005
790 return IRQ_RETVAL(1);
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
);
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");
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
);
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
848 static void rs_timer(unsigned long dummy
)
850 static unsigned long last_strobe
;
851 struct async_struct
*info
;
854 //printk("rs_timer\r\n");
855 if ((jiffies
- last_strobe
) >= RS_STROBE_TIME
)
857 for (i
=0; i
< NR_IRQS
; i
++)
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
);
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
];
900 IRQ_timeout
[irq
] = 60*HZ
;
904 if (info
->timeout
< timeout
)
905 timeout
= info
->timeout
;
906 info
= info
->next_port
;
909 timeout
= timeout
/ 2;
910 IRQ_timeout
[irq
] = (timeout
> 3) ? timeout
-2 : 1;
914 static int startup(struct async_struct
* info
)
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
*);
923 struct serial_state
*state
= info
->state
;
925 //printk("startup\r\n");
926 page
= get_zeroed_page(GFP_KERNEL
);
930 save_flags(flags
); cli();
932 if (info
->flags
& ASYNC_INITIALIZED
) {
937 if (!CONFIGURED_SERIAL_PORT(state
) || !state
->type
)
940 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
947 info
->xmit
.buf
= (unsigned char *) page
;
949 #ifdef SERIAL_DEBUG_OPEN
950 printk("starting up ttys%d (irq %d)...", info
->line
, state
->irq
);
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
|
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
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
)) {
985 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
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
;
1004 #endif /* CONFIG_SERIAL_SHARE_IRQ */
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
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
]);
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
]);
1033 if (capable(CAP_SYS_ADMIN
)) {
1035 set_bit(TTY_IO_ERROR
,
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 */
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
);
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 */
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;
1109 * and set the speed of the serial port
1111 change_speed(info
, 0);
1113 info
->flags
|= ASYNC_INITIALIZED
;
1114 restore_flags(flags
);
1118 restore_flags(flags
);
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
;
1133 //printk("shutdown\r\n");
1134 if (!(info
->flags
& ASYNC_INITIALIZED
))
1137 state
= info
->state
;
1139 #ifdef SERIAL_DEBUG_OPEN
1140 printk("Shutting down serial port %d (irq %d)....", info
->line
,
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
;
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
,
1172 &IRQ_ports
[state
->irq
]);
1175 printk("serial shutdown: request_irq: error %d"
1176 " Couldn't reacquire IRQ.\n", retval
);
1178 free_irq(state
->irq
, &IRQ_ports
[state
->irq
]);
1181 if (info
->xmit
.buf
) {
1182 unsigned long pg
= (unsigned long) info
->xmit
.buf
;
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
|
1204 serial_outp(info
, SERIAL_FCR
, 0);
1208 (void)serial_in(info
, SERIAL_RBR
); /* read data port to reset things */
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 */
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
;
1255 tty
->termios
->c_cflag
&= ~CBAUDEX
;
1260 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_HI
)
1262 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_VHI
)
1265 return baud_table
[i
];
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;
1279 unsigned long flags
;
1280 //printk("change_speed\r\n");
1281 if (!info
->tty
|| !info
->tty
->termios
)
1283 cflag
= info
->tty
->termios
->c_cflag
;
1284 if (!CONFIGURED_SERIAL_PORT(info
))
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
) {
1301 if (cflag
& PARENB
) {
1302 cval
|= SERIAL_LCR_PE
;
1305 if (!(cflag
& PARODD
))
1306 cval
|= SERIAL_LCR_EVEN
;
1309 cval
|= SERIAL_LCR_STICKPARITY
;
1312 /* Determine divisor based on baud rate */
1313 baud
= tty_get_baud_rate(info
->tty
);
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
;
1324 /* Special case since 134 is really 134.5 */
1325 quot
= (2*baud_base
/ 269);
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
);
1336 if (baud
== 38400 &&
1337 ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_CUST
))
1338 quot
= info
->state
->custom_divisor
;
1341 /* Special case since 134 is really 134.5 */
1342 quot
= (2*baud_base
/ 269);
1344 quot
= baud_base
/ baud
;
1347 /* As a last resort, if the quotient is zero, default to 9600 bps */
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
1355 if (((quot
& 0xFF) == 0) && (info
->state
->type
== PORT_16C950
) &&
1356 (info
->state
->revision
== 0x5201))
1360 info
->timeout
= ((info
->xmit_fifo_size
*HZ
*bits
*quot
) / baud_base
);
1361 info
->timeout
+= HZ
/50; /* Add .02 seconds of slop */
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
;
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
;
1383 info
->flags
&= ~ASYNC_CTS_FLOW
;
1385 info
->flags
&= ~ASYNC_CHECK_CD
;
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"))
1455 if (!tty
|| !info
->xmit
.buf
)
1458 save_flags(flags
); cli();
1459 if (CIRC_SPACE(info
->xmit
.head
,
1461 SERIAL_XMIT_SIZE
) == 0) {
1462 restore_flags(flags
);
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"))
1483 if (info
->xmit
.head
== info
->xmit
.tail
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
)
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"))
1508 if (!tty
|| !info
->xmit
.buf
|| !tmp_buf
)
1511 // buf[count] = '\0';
1512 // printk("rs_write: %s\n", buf);
1519 c
= CIRC_SPACE_TO_END(info
->xmit
.head
,
1527 c
-= copy_from_user(tmp_buf
, buf
, c
);
1534 c1
= CIRC_SPACE_TO_END(info
->xmit
.head
,
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
);
1554 c
= CIRC_SPACE_TO_END(info
->xmit
.head
,
1562 memcpy(info
->xmit
.buf
+ info
->xmit
.head
, buf
, c
);
1563 info
->xmit
.head
= ((info
->xmit
.head
+ c
) &
1564 (SERIAL_XMIT_SIZE
-1));
1569 restore_flags(flags
);
1571 if (info
->xmit
.head
!= info
->xmit
.tail
1574 && !(info
->IER
& SERIAL_IER_TE
))
1576 info
->IER
|= SERIAL_IER_TE
;
1577 serial_out(info
, SERIAL_IER
, info
->IER
);
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"))
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"))
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"))
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
);
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
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"))
1648 /* Make sure transmit interrupts are on */
1649 info
->IER
|= SERIAL_IER_TE
;
1650 serial_out(info
, SERIAL_IER
, info
->IER
);
1655 * ------------------------------------------------------------
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
1669 printk("throttle %s: %d....\n", tty_name(tty
, buf
),
1670 tty
->ldisc
.chars_in_buffer(tty
));
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"))
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
1698 printk("unthrottle %s: %d....\n", tty_name(tty
, buf
),
1699 tty
->ldisc
.chars_in_buffer(tty
));
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"))
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
;
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
;
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
)))
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
;
1765 unsigned long new_port
;
1767 if (copy_from_user(&new_serial
,new_info
,sizeof(new_serial
)))
1769 state
= info
->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
)))
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
);
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
)) {
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
) &&
1822 if ((change_port
|| change_irq
) && (state
->count
> 1))
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;
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.
1855 state
->irq
= new_serial
.irq
;
1856 info
->port
= state
->port
= new_port
;
1857 info
->hub6
= state
->hub6
= new_serial
.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)");
1869 if (!state
->port
|| !state
->type
)
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;
1885 change_speed(info
, 0);
1888 retval
= startup(info
);
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).
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)))
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)
1945 | ((control
& SERIAL_MCR_OUT1
) ? TIOCM_OUT1
: 0)
1946 | ((control
& SERIAL_MCR_OUT2
) ? TIOCM_OUT2
: 0)
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)))
1958 static int set_modem_info(struct async_struct
* info
, unsigned int cmd
,
1959 unsigned int *value
)
1962 unsigned long flags
;
1964 if (copy_from_user(&arg
, value
, sizeof(int)))
1969 if (arg
& TIOCM_RTS
)
1970 info
->MCR
|= SERIAL_MCR_RTS
;
1971 if (arg
& TIOCM_DTR
)
1972 info
->MCR
|= SERIAL_MCR_DTR
;
1974 if (arg
& TIOCM_OUT1
)
1975 info
->MCR
|= SERIAL_MCR_OUT1
;
1976 if (arg
& TIOCM_OUT2
)
1977 info
->MCR
|= SERIAL_MCR_OUT2
;
1979 if (arg
& TIOCM_LOOP
)
1980 info
->MCR
|= SERIAL_MCR_LPBK
;
1983 if (arg
& TIOCM_RTS
)
1984 info
->MCR
&= ~SERIAL_MCR_RTS
;
1985 if (arg
& TIOCM_DTR
)
1986 info
->MCR
&= ~SERIAL_MCR_DTR
;
1988 if (arg
& TIOCM_OUT1
)
1989 info
->MCR
&= ~SERIAL_MCR_OUT1
;
1990 if (arg
& TIOCM_OUT2
)
1991 info
->MCR
&= ~SERIAL_MCR_OUT2
;
1993 if (arg
& TIOCM_LOOP
)
1994 info
->MCR
&= ~SERIAL_MCR_LPBK
;
1997 info
->MCR
= ((info
->MCR
& ~(SERIAL_MCR_RTS
|
2004 | ((arg
& TIOCM_RTS
) ? SERIAL_MCR_RTS
: 0)
2006 | ((arg
& TIOCM_OUT1
) ? SERIAL_MCR_OUT1
: 0)
2007 | ((arg
& TIOCM_OUT2
) ? SERIAL_MCR_OUT2
: 0)
2009 | ((arg
& TIOCM_LOOP
) ? SERIAL_MCR_LPBK
: 0)
2010 | ((arg
& TIOCM_DTR
) ? SERIAL_MCR_DTR
: 0));
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
);
2022 static int do_autoconfig(struct async_struct
* info
)
2026 if (!capable(CAP_SYS_ADMIN
))
2029 if (info
->state
->count
> 1)
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
);
2040 info
->state
->irq
= irq
;
2043 retval
= startup(info
);
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
))
2057 current
->state
= TASK_INTERRUPTIBLE
;
2058 current
->timeout
= jiffies
+ duration
;
2060 info
->LCR
|= SERIAL_LCR_SETBREAK
;
2061 serial_out(info
, SERIAL_LCR
, info
->LCR
);
2063 info
->LCR
&= ~SERIAL_LCR_SETBREAK
;
2064 serial_out(info
, SERIAL_LCR
, info
->LCR
);
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"))
2080 if (!CONFIGURED_SERIAL_PORT(info
))
2082 save_flags(flags
); cli();
2083 if (break_state
== -1)
2084 info
->LCR
|= SERIAL_LCR_SETBREAK
;
2086 info
->LCR
&= ~SERIAL_LCR_SETBREAK
;
2087 serial_out(info
, SERIAL_LCR
, info
->LCR
);
2088 restore_flags(flags
);
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 */
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"))
2111 if ((cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCSSERIAL
) &&
2112 (cmd
!= TIOCSERCONFIG
) && (cmd
!= TIOCSERGSTRUCT
) &&
2113 (cmd
!= TIOCMIWAIT
) && (cmd
!= TIOCGICOUNT
)) {
2114 if (tty
->flags
& (1 << TTY_IO_ERROR
))
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
);
2124 tty_wait_until_sent(tty
, 0);
2125 if (signal_pending(current
))
2128 send_break(info
, HZ
/4); /* 1/4 second */
2129 if (signal_pending(current
))
2133 case TCSBRKP
: /* support for POSIX tcsendbreak() */
2134 retval
= tty_check_change(tty
);
2137 tty_wait_until_sent(tty
, 0);
2138 if (signal_pending(current
))
2140 send_break(info
, arg
? arg
*(HZ
/10) : HZ
/4);
2141 if (signal_pending(current
))
2145 tmp
= C_CLOCAL(tty
) ? 1 : 0;
2146 if (copy_to_user((void *)arg
, &tmp
, sizeof(int)))
2150 if (copy_from_user(&tmp
, (void *)arg
, sizeof(int)))
2153 tty
->termios
->c_cflag
=
2154 ((tty
->termios
->c_cflag
& ~CLOCAL
) |
2155 (tmp
? CLOCAL
: 0));
2159 return get_modem_info(info
, (unsigned int *) arg
);
2163 return set_modem_info(info
, cmd
, (unsigned int *) arg
);
2165 return get_serial_info(info
,
2166 (struct serial_struct
*) arg
);
2168 return set_serial_info(info
,
2169 (struct serial_struct
*) arg
);
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
)))
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
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
);
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
)) ) {
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.
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
)))
2245 /* "setserial -W" is called in Debian boot */
2246 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
2250 return -ENOIOCTLCMD
;
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
)))
2266 change_speed(info
, old_termios
);
2268 /* Handle transition to B0 status */
2269 if ((old_termios
->c_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
) &&
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;
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
);
2311 * ------------------------------------------------------------
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"))
2333 state
= info
->state
;
2335 save_flags(flags
); cli();
2337 if (tty_hung_up_p(filp
)) {
2338 DBG_CNT("before DEC-hung");
2340 restore_flags(flags
);
2344 #ifdef SERIAL_DEBUG_OPEN
2345 printk("rs_close ttys%d, count = %d\n", info
->line
, state
->count
);
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
);
2359 if (--state
->count
< 0) {
2360 printk("rs_close: bad serial port count for ttys%d: %d\n",
2361 info
->line
, state
->count
);
2365 DBG_CNT("before DEC-2");
2367 restore_flags(flags
);
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
;
2383 * Now we wait for the transmit buffer to clear; and we notify
2384 * the line discipline to only process XON/XOFF characters.
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
);
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
);
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
|
2430 #else // add by Victor Yu. 05-19-2005
2431 info
->flags
&= ~(ASYNC_NORMAL_ACTIVE
|ASYNC_CLOSING
);
2433 wake_up_interruptible(&info
->close_wait
);
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
;
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"))
2453 if (info
->state
->type
== PORT_UNKNOWN
)
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
2468 char_time
= (info
->timeout
- HZ
/50) / info
->xmit_fifo_size
;
2469 char_time
= char_time
/ 5;
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
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
);
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
);
2493 set_current_state(TASK_INTERRUPTIBLE
);
2494 schedule_timeout(char_time
);
2495 if (signal_pending(current
))
2497 if (timeout
&& time_after(jiffies
, orig_jiffies
+ timeout
))
2500 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2501 printk("lsr = %d (jiff=%lu)...done\n", lsr
, jiffies
);
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"))
2520 state
= info
->state
;
2522 rs_flush_buffer(tty
);
2523 if (info
->flags
& ASYNC_CLOSING
)
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
);
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
;
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
);
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
)
2575 if ((info
->flags
& ASYNC_CALLOUT_ACTIVE
) &&
2576 (info
->flags
& ASYNC_SESSION_LOCKOUT
) &&
2577 (info
->session
!= current
->session
))
2579 if ((info
->flags
& ASYNC_CALLOUT_ACTIVE
) &&
2580 (info
->flags
& ASYNC_PGRP_LOCKOUT
) &&
2581 (info
->pgrp
!= current
->pgrp
))
2583 info
->flags
|= ASYNC_CALLOUT_ACTIVE
;
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
)
2598 info
->flags
|= ASYNC_NORMAL_ACTIVE
;
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
)
2607 if (tty
->termios
->c_cflag
& CLOCAL
)
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.
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
);
2625 save_flags(flags
); cli();
2626 if (!tty_hung_up_p(filp
)) {
2630 restore_flags(flags
);
2631 info
->blocked_open
++;
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
);
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
)
2649 retval
= -ERESTARTSYS
;
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
) &
2662 if (signal_pending(current
)) {
2663 retval
= -ERESTARTSYS
;
2666 #ifdef SERIAL_DEBUG_OPEN
2667 printk("block_til_ready blocking: ttys%d, count = %d\n",
2668 info
->line
, state
->count
);
2672 set_current_state(TASK_RUNNING
);
2673 remove_wait_queue(&info
->open_wait
, &wait
);
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
);
2683 info
->flags
|= ASYNC_NORMAL_ACTIVE
;
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
;
2695 *ret_info
= sstate
->info
;
2698 info
= kmalloc(sizeof(struct async_struct
), GFP_KERNEL
);
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
;
2715 #if 0 // mask by Victor Yu. 05-19-2005
2716 info
->tqueue
.routine
= do_softint
;
2717 info
->tqueue
.data
= info
;
2719 info
->state
= sstate
;
2722 *ret_info
= sstate
->info
;
2725 *ret_info
= sstate
->info
= info
;
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
;
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
2751 if ((line
< 0) || (line
>= NR_PORTS
)) {
2755 retval
= get_async_struct(line
, &info
);
2760 tty
->driver_data
= info
;
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"))
2769 #ifdef SERIAL_DEBUG_OPEN
2770 printk("rs_open %s%d, count = %d\n", tty
->driver
.name
, info
->line
,
2771 info
->state
->count
);
2773 #if (LINUX_VERSION_CODE > 0x20100)
2774 info
->tty
->low_latency
= (info
->flags
& ASYNC_LOW_LATENCY
) ? 1 : 0;
2778 * This relies on lock_kernel() stuff so wants tidying for 2.5
2781 page
= get_zeroed_page(GFP_KERNEL
);
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
);
2806 * Start up serial port
2808 retval
= startup(info
);
2812 retval
= block_til_ready(tty
, filp
, info
);
2814 #ifdef SERIAL_DEBUG_OPEN
2815 printk("rs_open returning after block_til_ready with %d\n",
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
;
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);
2834 #ifdef CONFIG_SERIAL_CPE_CONSOLE
2835 if (sercons
.cflag
&& sercons
.index
== line
) {
2836 tty
->termios
->c_cflag
= sercons
.cflag
;
2838 change_speed(info
, 0);
2841 #if 0 // mask by Victor Yu. 05-19-2005
2842 info
->session
= current
->session
;
2843 info
->pgrp
= current
->pgrp
;
2846 #ifdef SERIAL_DEBUG_OPEN
2847 printk("rs_open ttys%d successful...", info
->line
);
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
;
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");
2873 * Figure out the current RS-232 lines
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
;
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
);
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");
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);
2936 int rs_read_proc(char *page
, char **start
, off_t off
, int count
,
2937 int *eof
, void *data
)
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
]);
2947 if (len
+begin
> off
+count
)
2949 if (len
+begin
< off
) {
2956 if (off
>= len
+begin
)
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
2975 static char serial_options
[] __initdata
=
2977 #ifdef CONFIG_SERIAL_SHARE_IRQ
2981 #ifdef CONFIG_SERIAL_DETECT_IRQ
2985 #ifdef ENABLE_SERIAL_ACPI
2992 " no serial options enabled\n";
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
,
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
)
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);
3035 if (state
->flags
& ASYNC_FOURPORT
) {
3036 serial_outp(&scr_info
, SERIAL_MCR
,
3037 SERIAL_MCR_DTR
| SERIAL_MCR_RTS
);
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);
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
;
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
);
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
);
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
);
3117 if (!CONFIGURED_SERIAL_PORT(state
))
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
);
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",
3185 restore_flags(flags
);
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;
3196 state
->type
= PORT_16450
;
3199 state
->type
= PORT_UNKNOWN
;
3202 state
->type
= PORT_16550
;
3205 state
->type
= PORT_16550A
;
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
;
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
);
3233 //request_region(info->port,8,"serial(auto)");
3234 request_region(info
->port
, 0x40, "serial(auto)");
3240 serial_outp(info
, SERIAL_MCR
, save_mcr
);
3241 serial_outp(info
, SERIAL_FCR
, (SERIAL_FCR_FE
|
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
);
3259 static struct symbol_table serial_syms
= {
3260 #include <linux/symtab_begin.h>
3262 X(unregister_serial
),
3263 #include <linux/symtab_end.h>
3273 * The serial driver boot-time initialization code!
3275 static int __init
rs_init(void)
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
);
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
++) {
3291 #ifdef CONFIG_SERIAL_CPE_CONSOLE
3293 * The interrupt of the serial console port
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;
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";
3312 #if (LINUX_VERSION_CODE > 0x2032D && defined(CONFIG_DEVFS_FS))
3313 serial_driver
.name
= "tts/%d";
3315 serial_driver
.name
= "ttyS";
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
;
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
;
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
;
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";
3366 callout_driver
.name
= "cua";
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;
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");
3384 // ªì©l¤Æ serial_state
3385 for (i
= 0, state
= rs_table
; i
< NR_PORTS
; i
++,state
++)
3387 state
->magic
= SSTATE_MAGIC
;
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
;
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
;
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
);
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))
3416 if (state
->flags
& ASYNC_BOOT_AUTOCONF
)
3421 for (i
= 0, state
= rs_table
; i
< NR_PORTS
; i
++,state
++)
3423 if (state
->type
== PORT_UNKNOWN
)
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
);
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
);
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
)
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
<<
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
;
3486 #endif /* end_of_not */
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
)
3512 unsigned long flags
;
3513 struct serial_state
*state
;
3514 struct async_struct
*info
;
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
))
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))
3534 if (i
== NR_PORTS
) {
3535 restore_flags(flags
);
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
);
3545 state
->irq
= req
->irq
;
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
;
3552 state
->baud_base
= req
->baud_base
;
3553 if ((info
= state
->info
) != NULL
) {
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
;
3561 if (state
->type
== PORT_UNKNOWN
) {
3562 restore_flags(flags
);
3563 printk("register_serial(): autoconfig failed\n");
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
);
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
);
3614 restore_flags(flags
);
3617 static void __exit
rs_fini(void)
3619 unsigned long flags
;
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
);
3630 if ((e1
= tty_unregister_driver(&serial_driver
)))
3631 printk("serial: failed to unregister serial driver (%d)\n",
3633 if ((e2
= tty_unregister_driver(&callout_driver
)))
3634 printk("serial: failed to unregister callout driver (%d)\n",
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
;
3643 if ((rs_table
[i
].type
!= PORT_UNKNOWN
) && rs_table
[i
].port
) {
3644 release_region(rs_table
[i
].port
, 8);
3650 unsigned long pg
= (unsigned long) tmp_buf
;
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;
3683 status
= serial_in(info
, SERIAL_LSR
);
3685 if (status
& SERIAL_LSR_BI
)
3686 lsr_break_flag
= SERIAL_LSR_BI
;
3690 } while((status
& BOTH_EMPTY
) != BOTH_EMPTY
);
3692 /* Wait for flow control if necessary */
3693 if (info
->flags
& ASYNC_CONS_FLOW
) {
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
,
3710 static struct async_struct
*info
= &async_sercons
;
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
);
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
;
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
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
);
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
)
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
;
3796 int baud
= CONFIG_SERIAL_CPE_DEFAULT_BAUD
;
3800 int cflag
= CREAD
| HUPCL
| CLOCAL
;
3807 baud
= simple_strtoul(options
, NULL
, 10);
3809 while(*s
>= '0' && *s
<= '9')
3811 if (*s
) parity
= *s
++;
3812 if (*s
) bits
= *s
++ - '0';
3813 if (*s
) doflow
= (*s
++ == 'r');
3818 * Now construct a cflag setting.
3847 * Set this to a sane value to prevent a divide error
3872 * Divisor, bytesize and parity
3874 state
= rs_table
+ co
->index
;
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
);
3891 cval
|= SERIAL_LCR_PE
;
3893 if (!(cflag
& PARODD
))
3895 cval
|= SERIAL_LCR_EVEN
;
3900 * Disable UART interrupts, set DTR and RTS high
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)
3919 static struct console sercons
= {
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
,
3932 void __init
cpe_console_init(void)
3934 register_console(&sercons
);
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"