2 * linux/drivers/char/amiserial.c
4 * Serial driver for the amiga builtin port.
6 * This code was created by taking serial.c version 4.30 from kernel
7 * release 2.3.22, replacing all hardware related stuff with the
8 * corresponding amiga hardware actions, and removing all irrelevant
9 * code. As a consequence, it uses many of the constants and names
10 * associated with the registers and bits of 16550 compatible UARTS -
11 * but only to keep track of status, etc in the state variables. It
12 * was done this was to make it easier to keep the code in line with
13 * (non hardware specific) changes to serial.c.
15 * The port is registered with the tty driver as minor device 64, and
16 * therefore other ports should should only use 65 upwards.
18 * Richard Lucock 28/12/99
20 * Copyright (C) 1991, 1992 Linus Torvalds
21 * Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997,
22 * 1998, 1999 Theodore Ts'o
27 * Serial driver configuration section. Here are the various options:
29 * SERIAL_PARANOIA_CHECK
30 * Check the magic number for the async_structure where
34 #include <linux/config.h>
35 #include <linux/delay.h>
37 #undef SERIAL_PARANOIA_CHECK
38 #define SERIAL_DO_RESTART
40 /* Set of debugging defines */
42 #undef SERIAL_DEBUG_INTR
43 #undef SERIAL_DEBUG_OPEN
44 #undef SERIAL_DEBUG_FLOW
45 #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
51 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
52 #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
53 tty->name, (info->flags), serial_driver->refcount,info->count,tty->count,s)
59 * End of serial driver configuration section.
62 #include <linux/module.h>
64 #include <linux/types.h>
65 #include <linux/serial.h>
66 #include <linux/serialP.h>
67 #include <linux/serial_reg.h>
68 static char *serial_version
= "4.30";
70 #include <linux/errno.h>
71 #include <linux/signal.h>
72 #include <linux/sched.h>
73 #include <linux/kernel.h>
74 #include <linux/timer.h>
75 #include <linux/interrupt.h>
76 #include <linux/tty.h>
77 #include <linux/tty_flip.h>
78 #include <linux/console.h>
79 #include <linux/major.h>
80 #include <linux/string.h>
81 #include <linux/fcntl.h>
82 #include <linux/ptrace.h>
83 #include <linux/ioport.h>
85 #include <linux/slab.h>
86 #include <linux/init.h>
87 #include <linux/bitops.h>
89 #include <asm/setup.h>
91 #include <asm/system.h>
95 #include <asm/amigahw.h>
96 #include <asm/amigaints.h>
99 #define _INLINE_ inline
102 static char *serial_name
= "Amiga-builtin serial driver";
104 static struct tty_driver
*serial_driver
;
106 /* number of characters left in xmit buffer before we ask for more */
107 #define WAKEUP_CHARS 256
109 static struct async_struct
*IRQ_ports
;
111 static unsigned char current_ctl_bits
;
113 static void change_speed(struct async_struct
*info
, struct termios
*old
);
114 static void rs_wait_until_sent(struct tty_struct
*tty
, int timeout
);
117 static struct serial_state rs_table
[1];
119 #define NR_PORTS (sizeof(rs_table)/sizeof(struct serial_state))
122 * tmp_buf is used as a temporary buffer by serial_write. We need to
123 * lock it in case the copy_from_user blocks while swapping in a page,
124 * and some other program tries to do a serial write at the same time.
125 * Since the lock will only come under contention when the system is
126 * swapping and available memory is low, it makes sense to share one
127 * buffer across all the serial ports, since it significantly saves
128 * memory if large numbers of serial ports are open.
130 static unsigned char *tmp_buf
;
131 static DECLARE_MUTEX(tmp_buf_sem
);
133 #include <asm/uaccess.h>
135 #define serial_isroot() (capable(CAP_SYS_ADMIN))
138 static inline int serial_paranoia_check(struct async_struct
*info
,
139 char *name
, const char *routine
)
141 #ifdef SERIAL_PARANOIA_CHECK
142 static const char *badmagic
=
143 "Warning: bad magic number for serial struct (%s) in %s\n";
144 static const char *badinfo
=
145 "Warning: null async_struct for (%s) in %s\n";
148 printk(badinfo
, name
, routine
);
151 if (info
->magic
!= SERIAL_MAGIC
) {
152 printk(badmagic
, name
, routine
);
159 /* some serial hardware definitions */
160 #define SDR_OVRUN (1<<15)
161 #define SDR_RBF (1<<14)
162 #define SDR_TBE (1<<13)
163 #define SDR_TSRE (1<<12)
165 #define SERPER_PARENB (1<<15)
167 #define AC_SETCLR (1<<15)
168 #define AC_UARTBRK (1<<11)
170 #define SER_DTR (1<<7)
171 #define SER_RTS (1<<6)
172 #define SER_DCD (1<<5)
173 #define SER_CTS (1<<4)
174 #define SER_DSR (1<<3)
176 static __inline__
void rtsdtr_ctrl(int bits
)
178 ciab
.pra
= ((bits
& (SER_RTS
| SER_DTR
)) ^ (SER_RTS
| SER_DTR
)) | (ciab
.pra
& ~(SER_RTS
| SER_DTR
));
182 * ------------------------------------------------------------
183 * rs_stop() and rs_start()
185 * This routines are called before setting or resetting tty->stopped.
186 * They enable or disable transmitter interrupts, as necessary.
187 * ------------------------------------------------------------
189 static void rs_stop(struct tty_struct
*tty
)
191 struct async_struct
*info
= (struct async_struct
*)tty
->driver_data
;
194 if (serial_paranoia_check(info
, tty
->name
, "rs_stop"))
197 local_irq_save(flags
);
198 if (info
->IER
& UART_IER_THRI
) {
199 info
->IER
&= ~UART_IER_THRI
;
200 /* disable Tx interrupt and remove any pending interrupts */
201 custom
.intena
= IF_TBE
;
203 custom
.intreq
= IF_TBE
;
206 local_irq_restore(flags
);
209 static void rs_start(struct tty_struct
*tty
)
211 struct async_struct
*info
= (struct async_struct
*)tty
->driver_data
;
214 if (serial_paranoia_check(info
, tty
->name
, "rs_start"))
217 local_irq_save(flags
);
218 if (info
->xmit
.head
!= info
->xmit
.tail
220 && !(info
->IER
& UART_IER_THRI
)) {
221 info
->IER
|= UART_IER_THRI
;
222 custom
.intena
= IF_SETCLR
| IF_TBE
;
224 /* set a pending Tx Interrupt, transmitter should restart now */
225 custom
.intreq
= IF_SETCLR
| IF_TBE
;
228 local_irq_restore(flags
);
232 * ----------------------------------------------------------------------
234 * Here starts the interrupt handling routines. All of the following
235 * subroutines are declared as inline and are folded into
236 * rs_interrupt(). They were separated out for readability's sake.
238 * Note: rs_interrupt() is a "fast" interrupt, which means that it
239 * runs with interrupts turned off. People who may want to modify
240 * rs_interrupt() should try to keep the interrupt handler as fast as
241 * possible. After you are done making modifications, it is not a bad
244 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
246 * and look at the resulting assemble code in serial.s.
248 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
249 * -----------------------------------------------------------------------
253 * This routine is used by the interrupt handler to schedule
254 * processing in the software interrupt portion of the driver.
256 static _INLINE_
void rs_sched_event(struct async_struct
*info
,
259 info
->event
|= 1 << event
;
260 tasklet_schedule(&info
->tlet
);
263 static _INLINE_
void receive_chars(struct async_struct
*info
)
267 struct tty_struct
*tty
= info
->tty
;
269 struct async_icount
*icount
;
271 icount
= &info
->state
->icount
;
273 status
= UART_LSR_DR
; /* We obviously have a character! */
274 serdatr
= custom
.serdatr
;
276 custom
.intreq
= IF_RBF
;
279 if((serdatr
& 0x1ff) == 0)
280 status
|= UART_LSR_BI
;
281 if(serdatr
& SDR_OVRUN
)
282 status
|= UART_LSR_OE
;
285 if (tty
->flip
.count
>= TTY_FLIPBUF_SIZE
)
287 *tty
->flip
.char_buf_ptr
= ch
;
290 #ifdef SERIAL_DEBUG_INTR
291 printk("DR%02x:%02x...", ch
, status
);
293 *tty
->flip
.flag_buf_ptr
= 0;
296 * We don't handle parity or frame errors - but I have left
297 * the code in, since I'm not sure that the errors can't be
301 if (status
& (UART_LSR_BI
| UART_LSR_PE
|
302 UART_LSR_FE
| UART_LSR_OE
)) {
304 * For statistics only
306 if (status
& UART_LSR_BI
) {
307 status
&= ~(UART_LSR_FE
| UART_LSR_PE
);
309 } else if (status
& UART_LSR_PE
)
311 else if (status
& UART_LSR_FE
)
313 if (status
& UART_LSR_OE
)
317 * Now check to see if character should be
318 * ignored, and mask off conditions which
321 if (status
& info
->ignore_status_mask
)
324 status
&= info
->read_status_mask
;
326 if (status
& (UART_LSR_BI
)) {
327 #ifdef SERIAL_DEBUG_INTR
328 printk("handling break....");
330 *tty
->flip
.flag_buf_ptr
= TTY_BREAK
;
331 if (info
->flags
& ASYNC_SAK
)
333 } else if (status
& UART_LSR_PE
)
334 *tty
->flip
.flag_buf_ptr
= TTY_PARITY
;
335 else if (status
& UART_LSR_FE
)
336 *tty
->flip
.flag_buf_ptr
= TTY_FRAME
;
337 if (status
& UART_LSR_OE
) {
339 * Overrun is special, since it's
340 * reported immediately, and doesn't
341 * affect the current character
343 if (tty
->flip
.count
< TTY_FLIPBUF_SIZE
) {
345 tty
->flip
.flag_buf_ptr
++;
346 tty
->flip
.char_buf_ptr
++;
347 *tty
->flip
.flag_buf_ptr
= TTY_OVERRUN
;
351 tty
->flip
.flag_buf_ptr
++;
352 tty
->flip
.char_buf_ptr
++;
356 tty_flip_buffer_push(tty
);
359 static _INLINE_
void transmit_chars(struct async_struct
*info
)
361 custom
.intreq
= IF_TBE
;
364 custom
.serdat
= info
->x_char
| 0x100;
366 info
->state
->icount
.tx
++;
370 if (info
->xmit
.head
== info
->xmit
.tail
371 || info
->tty
->stopped
372 || info
->tty
->hw_stopped
) {
373 info
->IER
&= ~UART_IER_THRI
;
374 custom
.intena
= IF_TBE
;
379 custom
.serdat
= info
->xmit
.buf
[info
->xmit
.tail
++] | 0x100;
381 info
->xmit
.tail
= info
->xmit
.tail
& (SERIAL_XMIT_SIZE
-1);
382 info
->state
->icount
.tx
++;
384 if (CIRC_CNT(info
->xmit
.head
,
386 SERIAL_XMIT_SIZE
) < WAKEUP_CHARS
)
387 rs_sched_event(info
, RS_EVENT_WRITE_WAKEUP
);
389 #ifdef SERIAL_DEBUG_INTR
392 if (info
->xmit
.head
== info
->xmit
.tail
) {
393 custom
.intena
= IF_TBE
;
395 info
->IER
&= ~UART_IER_THRI
;
399 static _INLINE_
void check_modem_status(struct async_struct
*info
)
401 unsigned char status
= ciab
.pra
& (SER_DCD
| SER_CTS
| SER_DSR
);
402 unsigned char dstatus
;
403 struct async_icount
*icount
;
405 /* Determine bits that have changed */
406 dstatus
= status
^ current_ctl_bits
;
407 current_ctl_bits
= status
;
410 icount
= &info
->state
->icount
;
411 /* update input line counters */
412 if (dstatus
& SER_DSR
)
414 if (dstatus
& SER_DCD
) {
416 #ifdef CONFIG_HARD_PPS
417 if ((info
->flags
& ASYNC_HARDPPS_CD
) &&
422 if (dstatus
& SER_CTS
)
424 wake_up_interruptible(&info
->delta_msr_wait
);
427 if ((info
->flags
& ASYNC_CHECK_CD
) && (dstatus
& SER_DCD
)) {
428 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
429 printk("ttyS%d CD now %s...", info
->line
,
430 (!(status
& SER_DCD
)) ? "on" : "off");
432 if (!(status
& SER_DCD
))
433 wake_up_interruptible(&info
->open_wait
);
435 #ifdef SERIAL_DEBUG_OPEN
436 printk("doing serial hangup...");
439 tty_hangup(info
->tty
);
442 if (info
->flags
& ASYNC_CTS_FLOW
) {
443 if (info
->tty
->hw_stopped
) {
444 if (!(status
& SER_CTS
)) {
445 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
446 printk("CTS tx start...");
448 info
->tty
->hw_stopped
= 0;
449 info
->IER
|= UART_IER_THRI
;
450 custom
.intena
= IF_SETCLR
| IF_TBE
;
452 /* set a pending Tx Interrupt, transmitter should restart now */
453 custom
.intreq
= IF_SETCLR
| IF_TBE
;
455 rs_sched_event(info
, RS_EVENT_WRITE_WAKEUP
);
459 if ((status
& SER_CTS
)) {
460 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
461 printk("CTS tx stop...");
463 info
->tty
->hw_stopped
= 1;
464 info
->IER
&= ~UART_IER_THRI
;
465 /* disable Tx interrupt and remove any pending interrupts */
466 custom
.intena
= IF_TBE
;
468 custom
.intreq
= IF_TBE
;
475 static irqreturn_t
ser_vbl_int( int irq
, void *data
, struct pt_regs
*regs
)
477 /* vbl is just a periodic interrupt we tie into to update modem status */
478 struct async_struct
* info
= IRQ_ports
;
480 * TBD - is it better to unregister from this interrupt or to
481 * ignore it if MSI is clear ?
483 if(info
->IER
& UART_IER_MSI
)
484 check_modem_status(info
);
488 static irqreturn_t
ser_rx_int(int irq
, void *dev_id
, struct pt_regs
* regs
)
490 struct async_struct
* info
;
492 #ifdef SERIAL_DEBUG_INTR
493 printk("ser_rx_int...");
497 if (!info
|| !info
->tty
)
501 info
->last_active
= jiffies
;
502 #ifdef SERIAL_DEBUG_INTR
508 static irqreturn_t
ser_tx_int(int irq
, void *dev_id
, struct pt_regs
* regs
)
510 struct async_struct
* info
;
512 if (custom
.serdatr
& SDR_TBE
) {
513 #ifdef SERIAL_DEBUG_INTR
514 printk("ser_tx_int...");
518 if (!info
|| !info
->tty
)
521 transmit_chars(info
);
522 info
->last_active
= jiffies
;
523 #ifdef SERIAL_DEBUG_INTR
531 * -------------------------------------------------------------------
532 * Here ends the serial interrupt routines.
533 * -------------------------------------------------------------------
537 * This routine is used to handle the "bottom half" processing for the
538 * serial driver, known also the "software interrupt" processing.
539 * This processing is done at the kernel interrupt level, after the
540 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
541 * is where time-consuming activities which can not be done in the
542 * interrupt driver proper are done; the interrupt driver schedules
543 * them using rs_sched_event(), and they get done here.
546 static void do_softint(unsigned long private_
)
548 struct async_struct
*info
= (struct async_struct
*) private_
;
549 struct tty_struct
*tty
;
555 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP
, &info
->event
)) {
557 wake_up_interruptible(&tty
->write_wait
);
562 * ---------------------------------------------------------------
563 * Low level utility subroutines for the serial driver: routines to
564 * figure out the appropriate timeout for an interrupt chain, routines
565 * to initialize and startup a serial port, and routines to shutdown a
566 * serial port. Useful stuff like that.
567 * ---------------------------------------------------------------
570 static int startup(struct async_struct
* info
)
576 page
= get_zeroed_page(GFP_KERNEL
);
580 local_irq_save(flags
);
582 if (info
->flags
& ASYNC_INITIALIZED
) {
590 info
->xmit
.buf
= (unsigned char *) page
;
592 #ifdef SERIAL_DEBUG_OPEN
593 printk("starting up ttys%d ...", info
->line
);
596 /* Clear anything in the input buffer */
598 custom
.intreq
= IF_RBF
;
601 retval
= request_irq(IRQ_AMIGA_VERTB
, ser_vbl_int
, 0, "serial status", info
);
603 if (serial_isroot()) {
605 set_bit(TTY_IO_ERROR
,
612 /* enable both Rx and Tx interrupts */
613 custom
.intena
= IF_SETCLR
| IF_RBF
| IF_TBE
;
615 info
->IER
= UART_IER_MSI
;
617 /* remember current state of the DCD and CTS bits */
618 current_ctl_bits
= ciab
.pra
& (SER_DCD
| SER_CTS
| SER_DSR
);
623 if (info
->tty
->termios
->c_cflag
& CBAUD
)
624 info
->MCR
= SER_DTR
| SER_RTS
;
625 rtsdtr_ctrl(info
->MCR
);
628 clear_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
629 info
->xmit
.head
= info
->xmit
.tail
= 0;
632 * Set up the tty->alt_speed kludge
635 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_HI
)
636 info
->tty
->alt_speed
= 57600;
637 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_VHI
)
638 info
->tty
->alt_speed
= 115200;
639 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_SHI
)
640 info
->tty
->alt_speed
= 230400;
641 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_WARP
)
642 info
->tty
->alt_speed
= 460800;
646 * and set the speed of the serial port
648 change_speed(info
, NULL
);
650 info
->flags
|= ASYNC_INITIALIZED
;
651 local_irq_restore(flags
);
655 local_irq_restore(flags
);
660 * This routine will shutdown a serial port; interrupts are disabled, and
661 * DTR is dropped if the hangup on close termio flag is on.
663 static void shutdown(struct async_struct
* info
)
666 struct serial_state
*state
;
668 if (!(info
->flags
& ASYNC_INITIALIZED
))
673 #ifdef SERIAL_DEBUG_OPEN
674 printk("Shutting down serial port %d ....\n", info
->line
);
677 local_irq_save(flags
); /* Disable interrupts */
680 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
681 * here so the queue might never be waken up
683 wake_up_interruptible(&info
->delta_msr_wait
);
688 * Free the IRQ, if necessary
690 free_irq(IRQ_AMIGA_VERTB
, info
);
692 if (info
->xmit
.buf
) {
693 free_page((unsigned long) info
->xmit
.buf
);
694 info
->xmit
.buf
= NULL
;
698 custom
.intena
= IF_RBF
| IF_TBE
;
701 /* disable break condition */
702 custom
.adkcon
= AC_UARTBRK
;
705 if (!info
->tty
|| (info
->tty
->termios
->c_cflag
& HUPCL
))
706 info
->MCR
&= ~(SER_DTR
|SER_RTS
);
707 rtsdtr_ctrl(info
->MCR
);
710 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
712 info
->flags
&= ~ASYNC_INITIALIZED
;
713 local_irq_restore(flags
);
718 * This routine is called to set the UART divisor registers to match
719 * the specified baud rate for a serial port.
721 static void change_speed(struct async_struct
*info
,
722 struct termios
*old_termios
)
724 int quot
= 0, baud_base
, baud
;
725 unsigned cflag
, cval
= 0;
729 if (!info
->tty
|| !info
->tty
->termios
)
731 cflag
= info
->tty
->termios
->c_cflag
;
733 /* Byte size is always 8 bits plus parity bit if requested */
736 if (cflag
& CSTOPB
) {
740 if (cflag
& PARENB
) {
741 cval
|= UART_LCR_PARITY
;
744 if (!(cflag
& PARODD
))
745 cval
|= UART_LCR_EPAR
;
748 cval
|= UART_LCR_SPAR
;
751 /* Determine divisor based on baud rate */
752 baud
= tty_get_baud_rate(info
->tty
);
754 baud
= 9600; /* B0 transition handled in rs_set_termios */
755 baud_base
= info
->state
->baud_base
;
757 ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_CUST
))
758 quot
= info
->state
->custom_divisor
;
761 /* Special case since 134 is really 134.5 */
762 quot
= (2*baud_base
/ 269);
764 quot
= baud_base
/ baud
;
766 /* If the quotient is zero refuse the change */
767 if (!quot
&& old_termios
) {
768 info
->tty
->termios
->c_cflag
&= ~CBAUD
;
769 info
->tty
->termios
->c_cflag
|= (old_termios
->c_cflag
& CBAUD
);
770 baud
= tty_get_baud_rate(info
->tty
);
774 ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_CUST
))
775 quot
= info
->state
->custom_divisor
;
778 /* Special case since 134 is really 134.5 */
779 quot
= (2*baud_base
/ 269);
781 quot
= baud_base
/ baud
;
784 /* As a last resort, if the quotient is zero, default to 9600 bps */
786 quot
= baud_base
/ 9600;
788 info
->timeout
= ((info
->xmit_fifo_size
*HZ
*bits
*quot
) / baud_base
);
789 info
->timeout
+= HZ
/50; /* Add .02 seconds of slop */
791 /* CTS flow control flag and modem status interrupts */
792 info
->IER
&= ~UART_IER_MSI
;
793 if (info
->flags
& ASYNC_HARDPPS_CD
)
794 info
->IER
|= UART_IER_MSI
;
795 if (cflag
& CRTSCTS
) {
796 info
->flags
|= ASYNC_CTS_FLOW
;
797 info
->IER
|= UART_IER_MSI
;
799 info
->flags
&= ~ASYNC_CTS_FLOW
;
801 info
->flags
&= ~ASYNC_CHECK_CD
;
803 info
->flags
|= ASYNC_CHECK_CD
;
804 info
->IER
|= UART_IER_MSI
;
807 * Does clearing IER_MSI imply that we should disbale the VBL interrupt ?
811 * Set up parity check flag
813 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
815 info
->read_status_mask
= UART_LSR_OE
| UART_LSR_DR
;
816 if (I_INPCK(info
->tty
))
817 info
->read_status_mask
|= UART_LSR_FE
| UART_LSR_PE
;
818 if (I_BRKINT(info
->tty
) || I_PARMRK(info
->tty
))
819 info
->read_status_mask
|= UART_LSR_BI
;
822 * Characters to ignore
824 info
->ignore_status_mask
= 0;
825 if (I_IGNPAR(info
->tty
))
826 info
->ignore_status_mask
|= UART_LSR_PE
| UART_LSR_FE
;
827 if (I_IGNBRK(info
->tty
)) {
828 info
->ignore_status_mask
|= UART_LSR_BI
;
830 * If we're ignore parity and break indicators, ignore
831 * overruns too. (For real raw support).
833 if (I_IGNPAR(info
->tty
))
834 info
->ignore_status_mask
|= UART_LSR_OE
;
837 * !!! ignore all characters if CREAD is not set
839 if ((cflag
& CREAD
) == 0)
840 info
->ignore_status_mask
|= UART_LSR_DR
;
841 local_irq_save(flags
);
846 /* Set up the baud rate */
849 /* Enable or disable parity bit */
851 if(cval
& UART_LCR_PARITY
)
852 serper
|= (SERPER_PARENB
);
854 custom
.serper
= serper
;
858 info
->LCR
= cval
; /* Save LCR */
859 local_irq_restore(flags
);
862 static void rs_put_char(struct tty_struct
*tty
, unsigned char ch
)
864 struct async_struct
*info
;
870 info
= tty
->driver_data
;
872 if (serial_paranoia_check(info
, tty
->name
, "rs_put_char"))
878 local_irq_save(flags
);
879 if (CIRC_SPACE(info
->xmit
.head
,
881 SERIAL_XMIT_SIZE
) == 0) {
882 local_irq_restore(flags
);
886 info
->xmit
.buf
[info
->xmit
.head
++] = ch
;
887 info
->xmit
.head
&= SERIAL_XMIT_SIZE
-1;
888 local_irq_restore(flags
);
891 static void rs_flush_chars(struct tty_struct
*tty
)
893 struct async_struct
*info
= (struct async_struct
*)tty
->driver_data
;
896 if (serial_paranoia_check(info
, tty
->name
, "rs_flush_chars"))
899 if (info
->xmit
.head
== info
->xmit
.tail
905 local_irq_save(flags
);
906 info
->IER
|= UART_IER_THRI
;
907 custom
.intena
= IF_SETCLR
| IF_TBE
;
909 /* set a pending Tx Interrupt, transmitter should restart now */
910 custom
.intreq
= IF_SETCLR
| IF_TBE
;
912 local_irq_restore(flags
);
915 static int rs_write(struct tty_struct
* tty
, const unsigned char *buf
, int count
)
918 struct async_struct
*info
;
924 info
= tty
->driver_data
;
926 if (serial_paranoia_check(info
, tty
->name
, "rs_write"))
929 if (!info
->xmit
.buf
|| !tmp_buf
)
932 local_save_flags(flags
);
935 c
= CIRC_SPACE_TO_END(info
->xmit
.head
,
943 memcpy(info
->xmit
.buf
+ info
->xmit
.head
, buf
, c
);
944 info
->xmit
.head
= ((info
->xmit
.head
+ c
) &
945 (SERIAL_XMIT_SIZE
-1));
950 local_irq_restore(flags
);
952 if (info
->xmit
.head
!= info
->xmit
.tail
955 && !(info
->IER
& UART_IER_THRI
)) {
956 info
->IER
|= UART_IER_THRI
;
958 custom
.intena
= IF_SETCLR
| IF_TBE
;
960 /* set a pending Tx Interrupt, transmitter should restart now */
961 custom
.intreq
= IF_SETCLR
| IF_TBE
;
963 local_irq_restore(flags
);
968 static int rs_write_room(struct tty_struct
*tty
)
970 struct async_struct
*info
= (struct async_struct
*)tty
->driver_data
;
972 if (serial_paranoia_check(info
, tty
->name
, "rs_write_room"))
974 return CIRC_SPACE(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
);
977 static int rs_chars_in_buffer(struct tty_struct
*tty
)
979 struct async_struct
*info
= (struct async_struct
*)tty
->driver_data
;
981 if (serial_paranoia_check(info
, tty
->name
, "rs_chars_in_buffer"))
983 return CIRC_CNT(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
);
986 static void rs_flush_buffer(struct tty_struct
*tty
)
988 struct async_struct
*info
= (struct async_struct
*)tty
->driver_data
;
991 if (serial_paranoia_check(info
, tty
->name
, "rs_flush_buffer"))
993 local_irq_save(flags
);
994 info
->xmit
.head
= info
->xmit
.tail
= 0;
995 local_irq_restore(flags
);
996 wake_up_interruptible(&tty
->write_wait
);
1001 * This function is used to send a high-priority XON/XOFF character to
1004 static void rs_send_xchar(struct tty_struct
*tty
, char ch
)
1006 struct async_struct
*info
= (struct async_struct
*)tty
->driver_data
;
1007 unsigned long flags
;
1009 if (serial_paranoia_check(info
, tty
->name
, "rs_send_char"))
1014 /* Make sure transmit interrupts are on */
1017 local_irq_save(flags
);
1018 if(!(custom
.intenar
& IF_TBE
)) {
1019 custom
.intena
= IF_SETCLR
| IF_TBE
;
1021 /* set a pending Tx Interrupt, transmitter should restart now */
1022 custom
.intreq
= IF_SETCLR
| IF_TBE
;
1025 local_irq_restore(flags
);
1027 info
->IER
|= UART_IER_THRI
;
1032 * ------------------------------------------------------------
1035 * This routine is called by the upper-layer tty layer to signal that
1036 * incoming characters should be throttled.
1037 * ------------------------------------------------------------
1039 static void rs_throttle(struct tty_struct
* tty
)
1041 struct async_struct
*info
= (struct async_struct
*)tty
->driver_data
;
1042 unsigned long flags
;
1043 #ifdef SERIAL_DEBUG_THROTTLE
1046 printk("throttle %s: %d....\n", tty_name(tty
, buf
),
1047 tty
->ldisc
.chars_in_buffer(tty
));
1050 if (serial_paranoia_check(info
, tty
->name
, "rs_throttle"))
1054 rs_send_xchar(tty
, STOP_CHAR(tty
));
1056 if (tty
->termios
->c_cflag
& CRTSCTS
)
1057 info
->MCR
&= ~SER_RTS
;
1059 local_irq_save(flags
);
1060 rtsdtr_ctrl(info
->MCR
);
1061 local_irq_restore(flags
);
1064 static void rs_unthrottle(struct tty_struct
* tty
)
1066 struct async_struct
*info
= (struct async_struct
*)tty
->driver_data
;
1067 unsigned long flags
;
1068 #ifdef SERIAL_DEBUG_THROTTLE
1071 printk("unthrottle %s: %d....\n", tty_name(tty
, buf
),
1072 tty
->ldisc
.chars_in_buffer(tty
));
1075 if (serial_paranoia_check(info
, tty
->name
, "rs_unthrottle"))
1082 rs_send_xchar(tty
, START_CHAR(tty
));
1084 if (tty
->termios
->c_cflag
& CRTSCTS
)
1085 info
->MCR
|= SER_RTS
;
1086 local_irq_save(flags
);
1087 rtsdtr_ctrl(info
->MCR
);
1088 local_irq_restore(flags
);
1092 * ------------------------------------------------------------
1093 * rs_ioctl() and friends
1094 * ------------------------------------------------------------
1097 static int get_serial_info(struct async_struct
* info
,
1098 struct serial_struct
* retinfo
)
1100 struct serial_struct tmp
;
1101 struct serial_state
*state
= info
->state
;
1105 memset(&tmp
, 0, sizeof(tmp
));
1106 tmp
.type
= state
->type
;
1107 tmp
.line
= state
->line
;
1108 tmp
.port
= state
->port
;
1109 tmp
.irq
= state
->irq
;
1110 tmp
.flags
= state
->flags
;
1111 tmp
.xmit_fifo_size
= state
->xmit_fifo_size
;
1112 tmp
.baud_base
= state
->baud_base
;
1113 tmp
.close_delay
= state
->close_delay
;
1114 tmp
.closing_wait
= state
->closing_wait
;
1115 tmp
.custom_divisor
= state
->custom_divisor
;
1116 if (copy_to_user(retinfo
,&tmp
,sizeof(*retinfo
)))
1121 static int set_serial_info(struct async_struct
* info
,
1122 struct serial_struct
* new_info
)
1124 struct serial_struct new_serial
;
1125 struct serial_state old_state
, *state
;
1126 unsigned int change_irq
,change_port
;
1129 if (copy_from_user(&new_serial
,new_info
,sizeof(new_serial
)))
1131 state
= info
->state
;
1134 change_irq
= new_serial
.irq
!= state
->irq
;
1135 change_port
= (new_serial
.port
!= state
->port
);
1136 if(change_irq
|| change_port
|| (new_serial
.xmit_fifo_size
!= state
->xmit_fifo_size
))
1139 if (!serial_isroot()) {
1140 if ((new_serial
.baud_base
!= state
->baud_base
) ||
1141 (new_serial
.close_delay
!= state
->close_delay
) ||
1142 (new_serial
.xmit_fifo_size
!= state
->xmit_fifo_size
) ||
1143 ((new_serial
.flags
& ~ASYNC_USR_MASK
) !=
1144 (state
->flags
& ~ASYNC_USR_MASK
)))
1146 state
->flags
= ((state
->flags
& ~ASYNC_USR_MASK
) |
1147 (new_serial
.flags
& ASYNC_USR_MASK
));
1148 info
->flags
= ((info
->flags
& ~ASYNC_USR_MASK
) |
1149 (new_serial
.flags
& ASYNC_USR_MASK
));
1150 state
->custom_divisor
= new_serial
.custom_divisor
;
1151 goto check_and_exit
;
1154 if (new_serial
.baud_base
< 9600)
1158 * OK, past this point, all the error checking has been done.
1159 * At this point, we start making changes.....
1162 state
->baud_base
= new_serial
.baud_base
;
1163 state
->flags
= ((state
->flags
& ~ASYNC_FLAGS
) |
1164 (new_serial
.flags
& ASYNC_FLAGS
));
1165 info
->flags
= ((state
->flags
& ~ASYNC_INTERNAL_FLAGS
) |
1166 (info
->flags
& ASYNC_INTERNAL_FLAGS
));
1167 state
->custom_divisor
= new_serial
.custom_divisor
;
1168 state
->close_delay
= new_serial
.close_delay
* HZ
/100;
1169 state
->closing_wait
= new_serial
.closing_wait
* HZ
/100;
1170 info
->tty
->low_latency
= (info
->flags
& ASYNC_LOW_LATENCY
) ? 1 : 0;
1173 if (info
->flags
& ASYNC_INITIALIZED
) {
1174 if (((old_state
.flags
& ASYNC_SPD_MASK
) !=
1175 (state
->flags
& ASYNC_SPD_MASK
)) ||
1176 (old_state
.custom_divisor
!= state
->custom_divisor
)) {
1177 if ((state
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_HI
)
1178 info
->tty
->alt_speed
= 57600;
1179 if ((state
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_VHI
)
1180 info
->tty
->alt_speed
= 115200;
1181 if ((state
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_SHI
)
1182 info
->tty
->alt_speed
= 230400;
1183 if ((state
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_WARP
)
1184 info
->tty
->alt_speed
= 460800;
1185 change_speed(info
, NULL
);
1188 retval
= startup(info
);
1194 * get_lsr_info - get line status register info
1196 * Purpose: Let user call ioctl() to get info when the UART physically
1197 * is emptied. On bus types like RS485, the transmitter must
1198 * release the bus after transmitting. This must be done when
1199 * the transmit shift register is empty, not be done when the
1200 * transmit holding register is empty. This functionality
1201 * allows an RS485 driver to be written in user space.
1203 static int get_lsr_info(struct async_struct
* info
, unsigned int *value
)
1205 unsigned char status
;
1206 unsigned int result
;
1207 unsigned long flags
;
1209 local_irq_save(flags
);
1210 status
= custom
.serdatr
;
1212 local_irq_restore(flags
);
1213 result
= ((status
& SDR_TSRE
) ? TIOCSER_TEMT
: 0);
1214 if (copy_to_user(value
, &result
, sizeof(int)))
1220 static int rs_tiocmget(struct tty_struct
*tty
, struct file
*file
)
1222 struct async_struct
* info
= (struct async_struct
*)tty
->driver_data
;
1223 unsigned char control
, status
;
1224 unsigned long flags
;
1226 if (serial_paranoia_check(info
, tty
->name
, "rs_ioctl"))
1228 if (tty
->flags
& (1 << TTY_IO_ERROR
))
1231 control
= info
->MCR
;
1232 local_irq_save(flags
);
1234 local_irq_restore(flags
);
1235 return ((control
& SER_RTS
) ? TIOCM_RTS
: 0)
1236 | ((control
& SER_DTR
) ? TIOCM_DTR
: 0)
1237 | (!(status
& SER_DCD
) ? TIOCM_CAR
: 0)
1238 | (!(status
& SER_DSR
) ? TIOCM_DSR
: 0)
1239 | (!(status
& SER_CTS
) ? TIOCM_CTS
: 0);
1242 static int rs_tiocmset(struct tty_struct
*tty
, struct file
*file
,
1243 unsigned int set
, unsigned int clear
)
1245 struct async_struct
* info
= (struct async_struct
*)tty
->driver_data
;
1246 unsigned long flags
;
1248 if (serial_paranoia_check(info
, tty
->name
, "rs_ioctl"))
1250 if (tty
->flags
& (1 << TTY_IO_ERROR
))
1253 local_irq_save(flags
);
1254 if (set
& TIOCM_RTS
)
1255 info
->MCR
|= SER_RTS
;
1256 if (set
& TIOCM_DTR
)
1257 info
->MCR
|= SER_DTR
;
1258 if (clear
& TIOCM_RTS
)
1259 info
->MCR
&= ~SER_RTS
;
1260 if (clear
& TIOCM_DTR
)
1261 info
->MCR
&= ~SER_DTR
;
1262 rtsdtr_ctrl(info
->MCR
);
1263 local_irq_restore(flags
);
1268 * rs_break() --- routine which turns the break handling on or off
1270 static void rs_break(struct tty_struct
*tty
, int break_state
)
1272 struct async_struct
* info
= (struct async_struct
*)tty
->driver_data
;
1273 unsigned long flags
;
1275 if (serial_paranoia_check(info
, tty
->name
, "rs_break"))
1278 local_irq_save(flags
);
1279 if (break_state
== -1)
1280 custom
.adkcon
= AC_SETCLR
| AC_UARTBRK
;
1282 custom
.adkcon
= AC_UARTBRK
;
1284 local_irq_restore(flags
);
1288 static int rs_ioctl(struct tty_struct
*tty
, struct file
* file
,
1289 unsigned int cmd
, unsigned long arg
)
1291 struct async_struct
* info
= (struct async_struct
*)tty
->driver_data
;
1292 struct async_icount cprev
, cnow
; /* kernel counter temps */
1293 struct serial_icounter_struct icount
;
1294 unsigned long flags
;
1296 if (serial_paranoia_check(info
, tty
->name
, "rs_ioctl"))
1299 if ((cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCSSERIAL
) &&
1300 (cmd
!= TIOCSERCONFIG
) && (cmd
!= TIOCSERGSTRUCT
) &&
1301 (cmd
!= TIOCMIWAIT
) && (cmd
!= TIOCGICOUNT
)) {
1302 if (tty
->flags
& (1 << TTY_IO_ERROR
))
1308 return get_serial_info(info
,
1309 (struct serial_struct
*) arg
);
1311 return set_serial_info(info
,
1312 (struct serial_struct
*) arg
);
1316 case TIOCSERGETLSR
: /* Get line status register */
1317 return get_lsr_info(info
, (unsigned int *) arg
);
1319 case TIOCSERGSTRUCT
:
1320 if (copy_to_user((struct async_struct
*) arg
,
1321 info
, sizeof(struct async_struct
)))
1326 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1327 * - mask passed in arg for lines of interest
1328 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1329 * Caller should use TIOCGICOUNT to see which one it was
1332 local_irq_save(flags
);
1333 /* note the counters on entry */
1334 cprev
= info
->state
->icount
;
1335 local_irq_restore(flags
);
1337 interruptible_sleep_on(&info
->delta_msr_wait
);
1338 /* see if a signal did it */
1339 if (signal_pending(current
))
1340 return -ERESTARTSYS
;
1341 local_irq_save(flags
);
1342 cnow
= info
->state
->icount
; /* atomic copy */
1343 local_irq_restore(flags
);
1344 if (cnow
.rng
== cprev
.rng
&& cnow
.dsr
== cprev
.dsr
&&
1345 cnow
.dcd
== cprev
.dcd
&& cnow
.cts
== cprev
.cts
)
1346 return -EIO
; /* no change => error */
1347 if ( ((arg
& TIOCM_RNG
) && (cnow
.rng
!= cprev
.rng
)) ||
1348 ((arg
& TIOCM_DSR
) && (cnow
.dsr
!= cprev
.dsr
)) ||
1349 ((arg
& TIOCM_CD
) && (cnow
.dcd
!= cprev
.dcd
)) ||
1350 ((arg
& TIOCM_CTS
) && (cnow
.cts
!= cprev
.cts
)) ) {
1358 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1359 * Return: write counters to the user passed counter struct
1360 * NB: both 1->0 and 0->1 transitions are counted except for
1361 * RI where only 0->1 is counted.
1364 local_irq_save(flags
);
1365 cnow
= info
->state
->icount
;
1366 local_irq_restore(flags
);
1367 icount
.cts
= cnow
.cts
;
1368 icount
.dsr
= cnow
.dsr
;
1369 icount
.rng
= cnow
.rng
;
1370 icount
.dcd
= cnow
.dcd
;
1371 icount
.rx
= cnow
.rx
;
1372 icount
.tx
= cnow
.tx
;
1373 icount
.frame
= cnow
.frame
;
1374 icount
.overrun
= cnow
.overrun
;
1375 icount
.parity
= cnow
.parity
;
1376 icount
.brk
= cnow
.brk
;
1377 icount
.buf_overrun
= cnow
.buf_overrun
;
1379 if (copy_to_user((void *)arg
, &icount
, sizeof(icount
)))
1384 /* "setserial -W" is called in Debian boot */
1385 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1389 return -ENOIOCTLCMD
;
1394 static void rs_set_termios(struct tty_struct
*tty
, struct termios
*old_termios
)
1396 struct async_struct
*info
= (struct async_struct
*)tty
->driver_data
;
1397 unsigned long flags
;
1398 unsigned int cflag
= tty
->termios
->c_cflag
;
1400 if ( (cflag
== old_termios
->c_cflag
)
1401 && ( RELEVANT_IFLAG(tty
->termios
->c_iflag
)
1402 == RELEVANT_IFLAG(old_termios
->c_iflag
)))
1405 change_speed(info
, old_termios
);
1407 /* Handle transition to B0 status */
1408 if ((old_termios
->c_cflag
& CBAUD
) &&
1410 info
->MCR
&= ~(SER_DTR
|SER_RTS
);
1411 local_irq_save(flags
);
1412 rtsdtr_ctrl(info
->MCR
);
1413 local_irq_restore(flags
);
1416 /* Handle transition away from B0 status */
1417 if (!(old_termios
->c_cflag
& CBAUD
) &&
1419 info
->MCR
|= SER_DTR
;
1420 if (!(tty
->termios
->c_cflag
& CRTSCTS
) ||
1421 !test_bit(TTY_THROTTLED
, &tty
->flags
)) {
1422 info
->MCR
|= SER_RTS
;
1424 local_irq_save(flags
);
1425 rtsdtr_ctrl(info
->MCR
);
1426 local_irq_restore(flags
);
1429 /* Handle turning off CRTSCTS */
1430 if ((old_termios
->c_cflag
& CRTSCTS
) &&
1431 !(tty
->termios
->c_cflag
& CRTSCTS
)) {
1432 tty
->hw_stopped
= 0;
1438 * No need to wake up processes in open wait, since they
1439 * sample the CLOCAL flag once, and don't recheck it.
1440 * XXX It's not clear whether the current behavior is correct
1441 * or not. Hence, this may change.....
1443 if (!(old_termios
->c_cflag
& CLOCAL
) &&
1444 (tty
->termios
->c_cflag
& CLOCAL
))
1445 wake_up_interruptible(&info
->open_wait
);
1450 * ------------------------------------------------------------
1453 * This routine is called when the serial port gets closed. First, we
1454 * wait for the last remaining data to be sent. Then, we unlink its
1455 * async structure from the interrupt chain if necessary, and we free
1456 * that IRQ if nothing is left in the chain.
1457 * ------------------------------------------------------------
1459 static void rs_close(struct tty_struct
*tty
, struct file
* filp
)
1461 struct async_struct
* info
= (struct async_struct
*)tty
->driver_data
;
1462 struct serial_state
*state
;
1463 unsigned long flags
;
1465 if (!info
|| serial_paranoia_check(info
, tty
->name
, "rs_close"))
1468 state
= info
->state
;
1470 local_irq_save(flags
);
1472 if (tty_hung_up_p(filp
)) {
1473 DBG_CNT("before DEC-hung");
1474 local_irq_restore(flags
);
1478 #ifdef SERIAL_DEBUG_OPEN
1479 printk("rs_close ttys%d, count = %d\n", info
->line
, state
->count
);
1481 if ((tty
->count
== 1) && (state
->count
!= 1)) {
1483 * Uh, oh. tty->count is 1, which means that the tty
1484 * structure will be freed. state->count should always
1485 * be one in these conditions. If it's greater than
1486 * one, we've got real problems, since it means the
1487 * serial port won't be shutdown.
1489 printk("rs_close: bad serial port count; tty->count is 1, "
1490 "state->count is %d\n", state
->count
);
1493 if (--state
->count
< 0) {
1494 printk("rs_close: bad serial port count for ttys%d: %d\n",
1495 info
->line
, state
->count
);
1499 DBG_CNT("before DEC-2");
1500 local_irq_restore(flags
);
1503 info
->flags
|= ASYNC_CLOSING
;
1505 * Now we wait for the transmit buffer to clear; and we notify
1506 * the line discipline to only process XON/XOFF characters.
1509 if (info
->closing_wait
!= ASYNC_CLOSING_WAIT_NONE
)
1510 tty_wait_until_sent(tty
, info
->closing_wait
);
1512 * At this point we stop accepting input. To do this, we
1513 * disable the receive line status interrupts, and tell the
1514 * interrupt driver to stop checking the data ready bit in the
1515 * line status register.
1517 info
->read_status_mask
&= ~UART_LSR_DR
;
1518 if (info
->flags
& ASYNC_INITIALIZED
) {
1519 /* disable receive interrupts */
1520 custom
.intena
= IF_RBF
;
1522 /* clear any pending receive interrupt */
1523 custom
.intreq
= IF_RBF
;
1527 * Before we drop DTR, make sure the UART transmitter
1528 * has completely drained; this is especially
1529 * important if there is a transmit FIFO!
1531 rs_wait_until_sent(tty
, info
->timeout
);
1534 if (tty
->driver
->flush_buffer
)
1535 tty
->driver
->flush_buffer(tty
);
1537 tty_ldisc_flush(tty
);
1541 if (info
->blocked_open
) {
1542 if (info
->close_delay
) {
1543 msleep_interruptible(jiffies_to_msecs(info
->close_delay
));
1545 wake_up_interruptible(&info
->open_wait
);
1547 info
->flags
&= ~(ASYNC_NORMAL_ACTIVE
|ASYNC_CLOSING
);
1548 wake_up_interruptible(&info
->close_wait
);
1549 local_irq_restore(flags
);
1553 * rs_wait_until_sent() --- wait until the transmitter is empty
1555 static void rs_wait_until_sent(struct tty_struct
*tty
, int timeout
)
1557 struct async_struct
* info
= (struct async_struct
*)tty
->driver_data
;
1558 unsigned long orig_jiffies
, char_time
;
1561 if (serial_paranoia_check(info
, tty
->name
, "rs_wait_until_sent"))
1564 if (info
->xmit_fifo_size
== 0)
1565 return; /* Just in case.... */
1567 orig_jiffies
= jiffies
;
1569 * Set the check interval to be 1/5 of the estimated time to
1570 * send a single character, and make it at least 1. The check
1571 * interval should also be less than the timeout.
1573 * Note: we have to use pretty tight timings here to satisfy
1576 char_time
= (info
->timeout
- HZ
/50) / info
->xmit_fifo_size
;
1577 char_time
= char_time
/ 5;
1581 char_time
= min_t(unsigned long, char_time
, timeout
);
1583 * If the transmitter hasn't cleared in twice the approximate
1584 * amount of time to send the entire FIFO, it probably won't
1585 * ever clear. This assumes the UART isn't doing flow
1586 * control, which is currently the case. Hence, if it ever
1587 * takes longer than info->timeout, this is probably due to a
1588 * UART bug of some kind. So, we clamp the timeout parameter at
1591 if (!timeout
|| timeout
> 2*info
->timeout
)
1592 timeout
= 2*info
->timeout
;
1593 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1594 printk("In rs_wait_until_sent(%d) check=%lu...", timeout
, char_time
);
1595 printk("jiff=%lu...", jiffies
);
1597 while(!((lsr
= custom
.serdatr
) & SDR_TSRE
)) {
1598 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1599 printk("serdatr = %d (jiff=%lu)...", lsr
, jiffies
);
1601 msleep_interruptible(jiffies_to_msecs(char_time
));
1602 if (signal_pending(current
))
1604 if (timeout
&& time_after(jiffies
, orig_jiffies
+ timeout
))
1607 current
->state
= TASK_RUNNING
;
1608 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1609 printk("lsr = %d (jiff=%lu)...done\n", lsr
, jiffies
);
1614 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1616 static void rs_hangup(struct tty_struct
*tty
)
1618 struct async_struct
* info
= (struct async_struct
*)tty
->driver_data
;
1619 struct serial_state
*state
= info
->state
;
1621 if (serial_paranoia_check(info
, tty
->name
, "rs_hangup"))
1624 state
= info
->state
;
1626 rs_flush_buffer(tty
);
1630 info
->flags
&= ~ASYNC_NORMAL_ACTIVE
;
1632 wake_up_interruptible(&info
->open_wait
);
1636 * ------------------------------------------------------------
1637 * rs_open() and friends
1638 * ------------------------------------------------------------
1640 static int block_til_ready(struct tty_struct
*tty
, struct file
* filp
,
1641 struct async_struct
*info
)
1643 #ifdef DECLARE_WAITQUEUE
1644 DECLARE_WAITQUEUE(wait
, current
);
1646 struct wait_queue wait
= { current
, NULL
};
1648 struct serial_state
*state
= info
->state
;
1650 int do_clocal
= 0, extra_count
= 0;
1651 unsigned long flags
;
1654 * If the device is in the middle of being closed, then block
1655 * until it's done, and then try again.
1657 if (tty_hung_up_p(filp
) ||
1658 (info
->flags
& ASYNC_CLOSING
)) {
1659 if (info
->flags
& ASYNC_CLOSING
)
1660 interruptible_sleep_on(&info
->close_wait
);
1661 #ifdef SERIAL_DO_RESTART
1662 return ((info
->flags
& ASYNC_HUP_NOTIFY
) ?
1663 -EAGAIN
: -ERESTARTSYS
);
1670 * If non-blocking mode is set, or the port is not enabled,
1671 * then make the check up front and then exit.
1673 if ((filp
->f_flags
& O_NONBLOCK
) ||
1674 (tty
->flags
& (1 << TTY_IO_ERROR
))) {
1675 info
->flags
|= ASYNC_NORMAL_ACTIVE
;
1679 if (tty
->termios
->c_cflag
& CLOCAL
)
1683 * Block waiting for the carrier detect and the line to become
1684 * free (i.e., not in use by the callout). While we are in
1685 * this loop, state->count is dropped by one, so that
1686 * rs_close() knows when to free things. We restore it upon
1687 * exit, either normal or abnormal.
1690 add_wait_queue(&info
->open_wait
, &wait
);
1691 #ifdef SERIAL_DEBUG_OPEN
1692 printk("block_til_ready before block: ttys%d, count = %d\n",
1693 state
->line
, state
->count
);
1695 local_irq_save(flags
);
1696 if (!tty_hung_up_p(filp
)) {
1700 local_irq_restore(flags
);
1701 info
->blocked_open
++;
1703 local_irq_save(flags
);
1704 if (tty
->termios
->c_cflag
& CBAUD
)
1705 rtsdtr_ctrl(SER_DTR
|SER_RTS
);
1706 local_irq_restore(flags
);
1707 set_current_state(TASK_INTERRUPTIBLE
);
1708 if (tty_hung_up_p(filp
) ||
1709 !(info
->flags
& ASYNC_INITIALIZED
)) {
1710 #ifdef SERIAL_DO_RESTART
1711 if (info
->flags
& ASYNC_HUP_NOTIFY
)
1714 retval
= -ERESTARTSYS
;
1720 if (!(info
->flags
& ASYNC_CLOSING
) &&
1721 (do_clocal
|| (!(ciab
.pra
& SER_DCD
)) ))
1723 if (signal_pending(current
)) {
1724 retval
= -ERESTARTSYS
;
1727 #ifdef SERIAL_DEBUG_OPEN
1728 printk("block_til_ready blocking: ttys%d, count = %d\n",
1729 info
->line
, state
->count
);
1733 current
->state
= TASK_RUNNING
;
1734 remove_wait_queue(&info
->open_wait
, &wait
);
1737 info
->blocked_open
--;
1738 #ifdef SERIAL_DEBUG_OPEN
1739 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1740 info
->line
, state
->count
);
1744 info
->flags
|= ASYNC_NORMAL_ACTIVE
;
1748 static int get_async_struct(int line
, struct async_struct
**ret_info
)
1750 struct async_struct
*info
;
1751 struct serial_state
*sstate
;
1753 sstate
= rs_table
+ line
;
1756 *ret_info
= sstate
->info
;
1759 info
= kmalloc(sizeof(struct async_struct
), GFP_KERNEL
);
1764 memset(info
, 0, sizeof(struct async_struct
));
1765 #ifdef DECLARE_WAITQUEUE
1766 init_waitqueue_head(&info
->open_wait
);
1767 init_waitqueue_head(&info
->close_wait
);
1768 init_waitqueue_head(&info
->delta_msr_wait
);
1770 info
->magic
= SERIAL_MAGIC
;
1771 info
->port
= sstate
->port
;
1772 info
->flags
= sstate
->flags
;
1773 info
->xmit_fifo_size
= sstate
->xmit_fifo_size
;
1775 tasklet_init(&info
->tlet
, do_softint
, (unsigned long)info
);
1776 info
->state
= sstate
;
1779 *ret_info
= sstate
->info
;
1782 *ret_info
= sstate
->info
= info
;
1787 * This routine is called whenever a serial port is opened. It
1788 * enables interrupts for a serial port, linking in its async structure into
1789 * the IRQ chain. It also performs the serial-specific
1790 * initialization for the tty structure.
1792 static int rs_open(struct tty_struct
*tty
, struct file
* filp
)
1794 struct async_struct
*info
;
1799 if ((line
< 0) || (line
>= NR_PORTS
)) {
1802 retval
= get_async_struct(line
, &info
);
1806 tty
->driver_data
= info
;
1808 if (serial_paranoia_check(info
, tty
->name
, "rs_open"))
1811 #ifdef SERIAL_DEBUG_OPEN
1812 printk("rs_open %s, count = %d\n", tty
->name
, info
->state
->count
);
1814 info
->tty
->low_latency
= (info
->flags
& ASYNC_LOW_LATENCY
) ? 1 : 0;
1817 page
= get_zeroed_page(GFP_KERNEL
);
1824 tmp_buf
= (unsigned char *) page
;
1828 * If the port is the middle of closing, bail out now
1830 if (tty_hung_up_p(filp
) ||
1831 (info
->flags
& ASYNC_CLOSING
)) {
1832 if (info
->flags
& ASYNC_CLOSING
)
1833 interruptible_sleep_on(&info
->close_wait
);
1834 #ifdef SERIAL_DO_RESTART
1835 return ((info
->flags
& ASYNC_HUP_NOTIFY
) ?
1836 -EAGAIN
: -ERESTARTSYS
);
1843 * Start up serial port
1845 retval
= startup(info
);
1850 retval
= block_til_ready(tty
, filp
, info
);
1852 #ifdef SERIAL_DEBUG_OPEN
1853 printk("rs_open returning after block_til_ready with %d\n",
1859 #ifdef SERIAL_DEBUG_OPEN
1860 printk("rs_open %s successful...", tty
->name
);
1866 * /proc fs routines....
1869 static inline int line_info(char *buf
, struct serial_state
*state
)
1871 struct async_struct
*info
= state
->info
, scr_info
;
1872 char stat_buf
[30], control
, status
;
1874 unsigned long flags
;
1876 ret
= sprintf(buf
, "%d: uart:amiga_builtin",state
->line
);
1879 * Figure out the current RS-232 lines
1882 info
= &scr_info
; /* This is just for serial_{in,out} */
1884 info
->magic
= SERIAL_MAGIC
;
1885 info
->flags
= state
->flags
;
1889 local_irq_save(flags
);
1891 control
= info
? info
->MCR
: status
;
1892 local_irq_restore(flags
);
1896 if(!(control
& SER_RTS
))
1897 strcat(stat_buf
, "|RTS");
1898 if(!(status
& SER_CTS
))
1899 strcat(stat_buf
, "|CTS");
1900 if(!(control
& SER_DTR
))
1901 strcat(stat_buf
, "|DTR");
1902 if(!(status
& SER_DSR
))
1903 strcat(stat_buf
, "|DSR");
1904 if(!(status
& SER_DCD
))
1905 strcat(stat_buf
, "|CD");
1908 ret
+= sprintf(buf
+ret
, " baud:%d",
1909 state
->baud_base
/ info
->quot
);
1912 ret
+= sprintf(buf
+ret
, " tx:%d rx:%d",
1913 state
->icount
.tx
, state
->icount
.rx
);
1915 if (state
->icount
.frame
)
1916 ret
+= sprintf(buf
+ret
, " fe:%d", state
->icount
.frame
);
1918 if (state
->icount
.parity
)
1919 ret
+= sprintf(buf
+ret
, " pe:%d", state
->icount
.parity
);
1921 if (state
->icount
.brk
)
1922 ret
+= sprintf(buf
+ret
, " brk:%d", state
->icount
.brk
);
1924 if (state
->icount
.overrun
)
1925 ret
+= sprintf(buf
+ret
, " oe:%d", state
->icount
.overrun
);
1928 * Last thing is the RS-232 status lines
1930 ret
+= sprintf(buf
+ret
, " %s\n", stat_buf
+1);
1934 static int rs_read_proc(char *page
, char **start
, off_t off
, int count
,
1935 int *eof
, void *data
)
1940 len
+= sprintf(page
, "serinfo:1.0 driver:%s\n", serial_version
);
1941 l
= line_info(page
+ len
, &rs_table
[0]);
1943 if (len
+begin
> off
+count
)
1945 if (len
+begin
< off
) {
1951 if (off
>= len
+begin
)
1953 *start
= page
+ (off
-begin
);
1954 return ((count
< begin
+len
-off
) ? count
: begin
+len
-off
);
1958 * ---------------------------------------------------------------------
1959 * rs_init() and friends
1961 * rs_init() is called at boot-time to initialize the serial driver.
1962 * ---------------------------------------------------------------------
1966 * This routine prints out the appropriate serial driver version
1967 * number, and identifies which options were configured into this
1970 static _INLINE_
void show_serial_version(void)
1972 printk(KERN_INFO
"%s version %s\n", serial_name
, serial_version
);
1976 static struct tty_operations serial_ops
= {
1980 .put_char
= rs_put_char
,
1981 .flush_chars
= rs_flush_chars
,
1982 .write_room
= rs_write_room
,
1983 .chars_in_buffer
= rs_chars_in_buffer
,
1984 .flush_buffer
= rs_flush_buffer
,
1986 .throttle
= rs_throttle
,
1987 .unthrottle
= rs_unthrottle
,
1988 .set_termios
= rs_set_termios
,
1991 .hangup
= rs_hangup
,
1992 .break_ctl
= rs_break
,
1993 .send_xchar
= rs_send_xchar
,
1994 .wait_until_sent
= rs_wait_until_sent
,
1995 .read_proc
= rs_read_proc
,
1996 .tiocmget
= rs_tiocmget
,
1997 .tiocmset
= rs_tiocmset
,
2001 * The serial driver boot-time initialization code!
2003 static int __init
rs_init(void)
2005 unsigned long flags
;
2006 struct serial_state
* state
;
2008 if (!MACH_IS_AMIGA
|| !AMIGAHW_PRESENT(AMI_SERIAL
))
2011 serial_driver
= alloc_tty_driver(1);
2016 * We request SERDAT and SERPER only, because the serial registers are
2017 * too spreaded over the custom register space
2019 if (!request_mem_region(CUSTOM_PHYSADDR
+0x30, 4, "amiserial [Paula]"))
2024 show_serial_version();
2026 /* Initialize the tty_driver structure */
2028 serial_driver
->owner
= THIS_MODULE
;
2029 serial_driver
->driver_name
= "amiserial";
2030 serial_driver
->name
= "ttyS";
2031 serial_driver
->major
= TTY_MAJOR
;
2032 serial_driver
->minor_start
= 64;
2033 serial_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
2034 serial_driver
->subtype
= SERIAL_TYPE_NORMAL
;
2035 serial_driver
->init_termios
= tty_std_termios
;
2036 serial_driver
->init_termios
.c_cflag
=
2037 B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
2038 serial_driver
->flags
= TTY_DRIVER_REAL_RAW
;
2039 tty_set_operations(serial_driver
, &serial_ops
);
2041 if (tty_register_driver(serial_driver
))
2042 panic("Couldn't register serial driver\n");
2045 state
->magic
= SSTATE_MAGIC
;
2046 state
->port
= (int)&custom
.serdatr
; /* Just to give it a value */
2048 state
->custom_divisor
= 0;
2049 state
->close_delay
= 5*HZ
/10;
2050 state
->closing_wait
= 30*HZ
;
2051 state
->icount
.cts
= state
->icount
.dsr
=
2052 state
->icount
.rng
= state
->icount
.dcd
= 0;
2053 state
->icount
.rx
= state
->icount
.tx
= 0;
2054 state
->icount
.frame
= state
->icount
.parity
= 0;
2055 state
->icount
.overrun
= state
->icount
.brk
= 0;
2057 printk(KERN_INFO
"ttyS%d is the amiga builtin serial port\n",
2060 /* Hardware set up */
2062 state
->baud_base
= amiga_colorclock
;
2063 state
->xmit_fifo_size
= 1;
2065 local_irq_save(flags
);
2067 /* set ISRs, and then disable the rx interrupts */
2068 request_irq(IRQ_AMIGA_TBE
, ser_tx_int
, 0, "serial TX", state
);
2069 request_irq(IRQ_AMIGA_RBF
, ser_rx_int
, SA_INTERRUPT
, "serial RX", state
);
2071 /* turn off Rx and Tx interrupts */
2072 custom
.intena
= IF_RBF
| IF_TBE
;
2075 /* clear any pending interrupt */
2076 custom
.intreq
= IF_RBF
| IF_TBE
;
2079 local_irq_restore(flags
);
2082 * set the appropriate directions for the modem control flags,
2083 * and clear RTS and DTR
2085 ciab
.ddra
|= (SER_DTR
| SER_RTS
); /* outputs */
2086 ciab
.ddra
&= ~(SER_DCD
| SER_CTS
| SER_DSR
); /* inputs */
2091 static __exit
void rs_exit(void)
2094 struct async_struct
*info
= rs_table
[0].info
;
2096 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2097 tasklet_kill(&info
->tlet
);
2098 if ((error
= tty_unregister_driver(serial_driver
)))
2099 printk("SERIAL: failed to unregister serial driver (%d)\n",
2101 put_tty_driver(serial_driver
);
2104 rs_table
[0].info
= NULL
;
2109 free_page((unsigned long) tmp_buf
);
2113 release_mem_region(CUSTOM_PHYSADDR
+0x30, 4);
2116 module_init(rs_init
)
2117 module_exit(rs_exit
)
2121 * ------------------------------------------------------------
2122 * Serial console driver
2123 * ------------------------------------------------------------
2125 #ifdef CONFIG_SERIAL_CONSOLE
2127 static void amiga_serial_putc(char c
)
2129 custom
.serdat
= (unsigned char)c
| 0x100;
2130 while (!(custom
.serdatr
& 0x2000))
2135 * Print a string to the serial port trying not to disturb
2136 * any possible real use of the port...
2138 * The console must be locked when we get here.
2140 static void serial_console_write(struct console
*co
, const char *s
,
2143 unsigned short intena
= custom
.intenar
;
2145 custom
.intena
= IF_TBE
;
2149 amiga_serial_putc('\r');
2150 amiga_serial_putc(*s
++);
2153 custom
.intena
= IF_SETCLR
| (intena
& IF_TBE
);
2156 static struct tty_driver
*serial_console_device(struct console
*c
, int *index
)
2159 return serial_driver
;
2162 static struct console sercons
= {
2164 .write
= serial_console_write
,
2165 .device
= serial_console_device
,
2166 .flags
= CON_PRINTBUFFER
,
2173 static int __init
amiserial_console_init(void)
2175 register_console(&sercons
);
2178 console_initcall(amiserial_console_init
);
2181 MODULE_LICENSE("GPL");