2 * Serial driver for the amiga builtin port.
4 * This code was created by taking serial.c version 4.30 from kernel
5 * release 2.3.22, replacing all hardware related stuff with the
6 * corresponding amiga hardware actions, and removing all irrelevant
7 * code. As a consequence, it uses many of the constants and names
8 * associated with the registers and bits of 16550 compatible UARTS -
9 * but only to keep track of status, etc in the state variables. It
10 * was done this was to make it easier to keep the code in line with
11 * (non hardware specific) changes to serial.c.
13 * The port is registered with the tty driver as minor device 64, and
14 * therefore other ports should should only use 65 upwards.
16 * Richard Lucock 28/12/99
18 * Copyright (C) 1991, 1992 Linus Torvalds
19 * Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997,
20 * 1998, 1999 Theodore Ts'o
25 * Serial driver configuration section. Here are the various options:
27 * SERIAL_PARANOIA_CHECK
28 * Check the magic number for the async_structure where
32 #include <linux/delay.h>
34 #undef SERIAL_PARANOIA_CHECK
35 #define SERIAL_DO_RESTART
37 /* Set of debugging defines */
39 #undef SERIAL_DEBUG_INTR
40 #undef SERIAL_DEBUG_OPEN
41 #undef SERIAL_DEBUG_FLOW
42 #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
46 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
47 #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
48 tty->name, (info->state->flags), serial_driver->refcount,info->count,tty->count,s)
54 * End of serial driver configuration section.
57 #include <linux/module.h>
59 #include <linux/types.h>
60 #include <linux/serial.h>
61 #include <linux/serialP.h>
62 #include <linux/serial_reg.h>
63 static char *serial_version
= "4.30";
65 #include <linux/errno.h>
66 #include <linux/signal.h>
67 #include <linux/sched.h>
68 #include <linux/kernel.h>
69 #include <linux/timer.h>
70 #include <linux/interrupt.h>
71 #include <linux/tty.h>
72 #include <linux/tty_flip.h>
73 #include <linux/console.h>
74 #include <linux/major.h>
75 #include <linux/string.h>
76 #include <linux/fcntl.h>
77 #include <linux/ptrace.h>
78 #include <linux/ioport.h>
80 #include <linux/seq_file.h>
81 #include <linux/slab.h>
82 #include <linux/init.h>
83 #include <linux/bitops.h>
84 #include <linux/platform_device.h>
86 #include <asm/setup.h>
88 #include <asm/system.h>
92 #include <asm/amigahw.h>
93 #include <asm/amigaints.h>
95 #define custom amiga_custom
96 static char *serial_name
= "Amiga-builtin serial driver";
98 static struct tty_driver
*serial_driver
;
100 /* number of characters left in xmit buffer before we ask for more */
101 #define WAKEUP_CHARS 256
103 static struct async_struct
*IRQ_ports
;
105 static unsigned char current_ctl_bits
;
107 static void change_speed(struct async_struct
*info
, struct ktermios
*old
);
108 static void rs_wait_until_sent(struct tty_struct
*tty
, int timeout
);
111 static struct serial_state rs_table
[1];
113 #define NR_PORTS ARRAY_SIZE(rs_table)
115 #include <asm/uaccess.h>
117 #define serial_isroot() (capable(CAP_SYS_ADMIN))
120 static inline int serial_paranoia_check(struct async_struct
*info
,
121 char *name
, const char *routine
)
123 #ifdef SERIAL_PARANOIA_CHECK
124 static const char *badmagic
=
125 "Warning: bad magic number for serial struct (%s) in %s\n";
126 static const char *badinfo
=
127 "Warning: null async_struct for (%s) in %s\n";
130 printk(badinfo
, name
, routine
);
133 if (info
->magic
!= SERIAL_MAGIC
) {
134 printk(badmagic
, name
, routine
);
141 /* some serial hardware definitions */
142 #define SDR_OVRUN (1<<15)
143 #define SDR_RBF (1<<14)
144 #define SDR_TBE (1<<13)
145 #define SDR_TSRE (1<<12)
147 #define SERPER_PARENB (1<<15)
149 #define AC_SETCLR (1<<15)
150 #define AC_UARTBRK (1<<11)
152 #define SER_DTR (1<<7)
153 #define SER_RTS (1<<6)
154 #define SER_DCD (1<<5)
155 #define SER_CTS (1<<4)
156 #define SER_DSR (1<<3)
158 static __inline__
void rtsdtr_ctrl(int bits
)
160 ciab
.pra
= ((bits
& (SER_RTS
| SER_DTR
)) ^ (SER_RTS
| SER_DTR
)) | (ciab
.pra
& ~(SER_RTS
| SER_DTR
));
164 * ------------------------------------------------------------
165 * rs_stop() and rs_start()
167 * This routines are called before setting or resetting tty->stopped.
168 * They enable or disable transmitter interrupts, as necessary.
169 * ------------------------------------------------------------
171 static void rs_stop(struct tty_struct
*tty
)
173 struct async_struct
*info
= tty
->driver_data
;
176 if (serial_paranoia_check(info
, tty
->name
, "rs_stop"))
179 local_irq_save(flags
);
180 if (info
->IER
& UART_IER_THRI
) {
181 info
->IER
&= ~UART_IER_THRI
;
182 /* disable Tx interrupt and remove any pending interrupts */
183 custom
.intena
= IF_TBE
;
185 custom
.intreq
= IF_TBE
;
188 local_irq_restore(flags
);
191 static void rs_start(struct tty_struct
*tty
)
193 struct async_struct
*info
= tty
->driver_data
;
196 if (serial_paranoia_check(info
, tty
->name
, "rs_start"))
199 local_irq_save(flags
);
200 if (info
->xmit
.head
!= info
->xmit
.tail
202 && !(info
->IER
& UART_IER_THRI
)) {
203 info
->IER
|= UART_IER_THRI
;
204 custom
.intena
= IF_SETCLR
| IF_TBE
;
206 /* set a pending Tx Interrupt, transmitter should restart now */
207 custom
.intreq
= IF_SETCLR
| IF_TBE
;
210 local_irq_restore(flags
);
214 * ----------------------------------------------------------------------
216 * Here starts the interrupt handling routines. All of the following
217 * subroutines are declared as inline and are folded into
218 * rs_interrupt(). They were separated out for readability's sake.
220 * Note: rs_interrupt() is a "fast" interrupt, which means that it
221 * runs with interrupts turned off. People who may want to modify
222 * rs_interrupt() should try to keep the interrupt handler as fast as
223 * possible. After you are done making modifications, it is not a bad
226 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
228 * and look at the resulting assemble code in serial.s.
230 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
231 * -----------------------------------------------------------------------
234 static void receive_chars(struct async_struct
*info
)
238 struct tty_struct
*tty
= info
->tty
;
239 unsigned char ch
, flag
;
240 struct async_icount
*icount
;
243 icount
= &info
->state
->icount
;
245 status
= UART_LSR_DR
; /* We obviously have a character! */
246 serdatr
= custom
.serdatr
;
248 custom
.intreq
= IF_RBF
;
251 if((serdatr
& 0x1ff) == 0)
252 status
|= UART_LSR_BI
;
253 if(serdatr
& SDR_OVRUN
)
254 status
|= UART_LSR_OE
;
259 #ifdef SERIAL_DEBUG_INTR
260 printk("DR%02x:%02x...", ch
, status
);
265 * We don't handle parity or frame errors - but I have left
266 * the code in, since I'm not sure that the errors can't be
270 if (status
& (UART_LSR_BI
| UART_LSR_PE
|
271 UART_LSR_FE
| UART_LSR_OE
)) {
273 * For statistics only
275 if (status
& UART_LSR_BI
) {
276 status
&= ~(UART_LSR_FE
| UART_LSR_PE
);
278 } else if (status
& UART_LSR_PE
)
280 else if (status
& UART_LSR_FE
)
282 if (status
& UART_LSR_OE
)
286 * Now check to see if character should be
287 * ignored, and mask off conditions which
290 if (status
& info
->ignore_status_mask
)
293 status
&= info
->read_status_mask
;
295 if (status
& (UART_LSR_BI
)) {
296 #ifdef SERIAL_DEBUG_INTR
297 printk("handling break....");
300 if (info
->state
->flags
& ASYNC_SAK
)
302 } else if (status
& UART_LSR_PE
)
304 else if (status
& UART_LSR_FE
)
306 if (status
& UART_LSR_OE
) {
308 * Overrun is special, since it's
309 * reported immediately, and doesn't
310 * affect the current character
315 tty_insert_flip_char(tty
, ch
, flag
);
317 tty_insert_flip_char(tty
, 0, TTY_OVERRUN
);
318 tty_flip_buffer_push(tty
);
323 static void transmit_chars(struct async_struct
*info
)
325 custom
.intreq
= IF_TBE
;
328 custom
.serdat
= info
->x_char
| 0x100;
330 info
->state
->icount
.tx
++;
334 if (info
->xmit
.head
== info
->xmit
.tail
335 || info
->tty
->stopped
336 || info
->tty
->hw_stopped
) {
337 info
->IER
&= ~UART_IER_THRI
;
338 custom
.intena
= IF_TBE
;
343 custom
.serdat
= info
->xmit
.buf
[info
->xmit
.tail
++] | 0x100;
345 info
->xmit
.tail
= info
->xmit
.tail
& (SERIAL_XMIT_SIZE
-1);
346 info
->state
->icount
.tx
++;
348 if (CIRC_CNT(info
->xmit
.head
,
350 SERIAL_XMIT_SIZE
) < WAKEUP_CHARS
)
351 tty_wakeup(info
->tty
);
353 #ifdef SERIAL_DEBUG_INTR
356 if (info
->xmit
.head
== info
->xmit
.tail
) {
357 custom
.intena
= IF_TBE
;
359 info
->IER
&= ~UART_IER_THRI
;
363 static void check_modem_status(struct async_struct
*info
)
365 unsigned char status
= ciab
.pra
& (SER_DCD
| SER_CTS
| SER_DSR
);
366 unsigned char dstatus
;
367 struct async_icount
*icount
;
369 /* Determine bits that have changed */
370 dstatus
= status
^ current_ctl_bits
;
371 current_ctl_bits
= status
;
374 icount
= &info
->state
->icount
;
375 /* update input line counters */
376 if (dstatus
& SER_DSR
)
378 if (dstatus
& SER_DCD
) {
380 #ifdef CONFIG_HARD_PPS
381 if ((info
->state
->flags
& ASYNC_HARDPPS_CD
) &&
386 if (dstatus
& SER_CTS
)
388 wake_up_interruptible(&info
->delta_msr_wait
);
391 if ((info
->state
->flags
& ASYNC_CHECK_CD
) && (dstatus
& SER_DCD
)) {
392 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
393 printk("ttyS%d CD now %s...", info
->line
,
394 (!(status
& SER_DCD
)) ? "on" : "off");
396 if (!(status
& SER_DCD
))
397 wake_up_interruptible(&info
->open_wait
);
399 #ifdef SERIAL_DEBUG_OPEN
400 printk("doing serial hangup...");
403 tty_hangup(info
->tty
);
406 if (info
->state
->flags
& ASYNC_CTS_FLOW
) {
407 if (info
->tty
->hw_stopped
) {
408 if (!(status
& SER_CTS
)) {
409 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
410 printk("CTS tx start...");
412 info
->tty
->hw_stopped
= 0;
413 info
->IER
|= UART_IER_THRI
;
414 custom
.intena
= IF_SETCLR
| IF_TBE
;
416 /* set a pending Tx Interrupt, transmitter should restart now */
417 custom
.intreq
= IF_SETCLR
| IF_TBE
;
419 tty_wakeup(info
->tty
);
423 if ((status
& SER_CTS
)) {
424 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
425 printk("CTS tx stop...");
427 info
->tty
->hw_stopped
= 1;
428 info
->IER
&= ~UART_IER_THRI
;
429 /* disable Tx interrupt and remove any pending interrupts */
430 custom
.intena
= IF_TBE
;
432 custom
.intreq
= IF_TBE
;
439 static irqreturn_t
ser_vbl_int( int irq
, void *data
)
441 /* vbl is just a periodic interrupt we tie into to update modem status */
442 struct async_struct
* info
= IRQ_ports
;
444 * TBD - is it better to unregister from this interrupt or to
445 * ignore it if MSI is clear ?
447 if(info
->IER
& UART_IER_MSI
)
448 check_modem_status(info
);
452 static irqreturn_t
ser_rx_int(int irq
, void *dev_id
)
454 struct async_struct
* info
;
456 #ifdef SERIAL_DEBUG_INTR
457 printk("ser_rx_int...");
461 if (!info
|| !info
->tty
)
465 #ifdef SERIAL_DEBUG_INTR
471 static irqreturn_t
ser_tx_int(int irq
, void *dev_id
)
473 struct async_struct
* info
;
475 if (custom
.serdatr
& SDR_TBE
) {
476 #ifdef SERIAL_DEBUG_INTR
477 printk("ser_tx_int...");
481 if (!info
|| !info
->tty
)
484 transmit_chars(info
);
485 #ifdef SERIAL_DEBUG_INTR
493 * -------------------------------------------------------------------
494 * Here ends the serial interrupt routines.
495 * -------------------------------------------------------------------
499 * ---------------------------------------------------------------
500 * Low level utility subroutines for the serial driver: routines to
501 * figure out the appropriate timeout for an interrupt chain, routines
502 * to initialize and startup a serial port, and routines to shutdown a
503 * serial port. Useful stuff like that.
504 * ---------------------------------------------------------------
507 static int startup(struct async_struct
* info
)
513 page
= get_zeroed_page(GFP_KERNEL
);
517 local_irq_save(flags
);
519 if (info
->state
->flags
& ASYNC_INITIALIZED
) {
527 info
->xmit
.buf
= (unsigned char *) page
;
529 #ifdef SERIAL_DEBUG_OPEN
530 printk("starting up ttys%d ...", info
->line
);
533 /* Clear anything in the input buffer */
535 custom
.intreq
= IF_RBF
;
538 retval
= request_irq(IRQ_AMIGA_VERTB
, ser_vbl_int
, 0, "serial status", info
);
540 if (serial_isroot()) {
542 set_bit(TTY_IO_ERROR
,
549 /* enable both Rx and Tx interrupts */
550 custom
.intena
= IF_SETCLR
| IF_RBF
| IF_TBE
;
552 info
->IER
= UART_IER_MSI
;
554 /* remember current state of the DCD and CTS bits */
555 current_ctl_bits
= ciab
.pra
& (SER_DCD
| SER_CTS
| SER_DSR
);
560 if (info
->tty
->termios
->c_cflag
& CBAUD
)
561 info
->MCR
= SER_DTR
| SER_RTS
;
562 rtsdtr_ctrl(info
->MCR
);
565 clear_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
566 info
->xmit
.head
= info
->xmit
.tail
= 0;
569 * Set up the tty->alt_speed kludge
572 if ((info
->state
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_HI
)
573 info
->tty
->alt_speed
= 57600;
574 if ((info
->state
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_VHI
)
575 info
->tty
->alt_speed
= 115200;
576 if ((info
->state
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_SHI
)
577 info
->tty
->alt_speed
= 230400;
578 if ((info
->state
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_WARP
)
579 info
->tty
->alt_speed
= 460800;
583 * and set the speed of the serial port
585 change_speed(info
, NULL
);
587 info
->state
->flags
|= ASYNC_INITIALIZED
;
588 local_irq_restore(flags
);
592 local_irq_restore(flags
);
597 * This routine will shutdown a serial port; interrupts are disabled, and
598 * DTR is dropped if the hangup on close termio flag is on.
600 static void shutdown(struct async_struct
* info
)
603 struct serial_state
*state
;
605 if (!(info
->state
->flags
& ASYNC_INITIALIZED
))
610 #ifdef SERIAL_DEBUG_OPEN
611 printk("Shutting down serial port %d ....\n", info
->line
);
614 local_irq_save(flags
); /* Disable interrupts */
617 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
618 * here so the queue might never be waken up
620 wake_up_interruptible(&info
->delta_msr_wait
);
625 * Free the IRQ, if necessary
627 free_irq(IRQ_AMIGA_VERTB
, info
);
629 if (info
->xmit
.buf
) {
630 free_page((unsigned long) info
->xmit
.buf
);
631 info
->xmit
.buf
= NULL
;
635 custom
.intena
= IF_RBF
| IF_TBE
;
638 /* disable break condition */
639 custom
.adkcon
= AC_UARTBRK
;
642 if (!info
->tty
|| (info
->tty
->termios
->c_cflag
& HUPCL
))
643 info
->MCR
&= ~(SER_DTR
|SER_RTS
);
644 rtsdtr_ctrl(info
->MCR
);
647 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
649 info
->state
->flags
&= ~ASYNC_INITIALIZED
;
650 local_irq_restore(flags
);
655 * This routine is called to set the UART divisor registers to match
656 * the specified baud rate for a serial port.
658 static void change_speed(struct async_struct
*info
,
659 struct ktermios
*old_termios
)
661 int quot
= 0, baud_base
, baud
;
662 unsigned cflag
, cval
= 0;
666 if (!info
->tty
|| !info
->tty
->termios
)
668 cflag
= info
->tty
->termios
->c_cflag
;
670 /* Byte size is always 8 bits plus parity bit if requested */
673 if (cflag
& CSTOPB
) {
677 if (cflag
& PARENB
) {
678 cval
|= UART_LCR_PARITY
;
681 if (!(cflag
& PARODD
))
682 cval
|= UART_LCR_EPAR
;
685 cval
|= UART_LCR_SPAR
;
688 /* Determine divisor based on baud rate */
689 baud
= tty_get_baud_rate(info
->tty
);
691 baud
= 9600; /* B0 transition handled in rs_set_termios */
692 baud_base
= info
->state
->baud_base
;
694 ((info
->state
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_CUST
))
695 quot
= info
->state
->custom_divisor
;
698 /* Special case since 134 is really 134.5 */
699 quot
= (2*baud_base
/ 269);
701 quot
= baud_base
/ baud
;
703 /* If the quotient is zero refuse the change */
704 if (!quot
&& old_termios
) {
705 /* FIXME: Will need updating for new tty in the end */
706 info
->tty
->termios
->c_cflag
&= ~CBAUD
;
707 info
->tty
->termios
->c_cflag
|= (old_termios
->c_cflag
& CBAUD
);
708 baud
= tty_get_baud_rate(info
->tty
);
712 ((info
->state
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_CUST
))
713 quot
= info
->state
->custom_divisor
;
716 /* Special case since 134 is really 134.5 */
717 quot
= (2*baud_base
/ 269);
719 quot
= baud_base
/ baud
;
722 /* As a last resort, if the quotient is zero, default to 9600 bps */
724 quot
= baud_base
/ 9600;
726 info
->timeout
= ((info
->xmit_fifo_size
*HZ
*bits
*quot
) / baud_base
);
727 info
->timeout
+= HZ
/50; /* Add .02 seconds of slop */
729 /* CTS flow control flag and modem status interrupts */
730 info
->IER
&= ~UART_IER_MSI
;
731 if (info
->state
->flags
& ASYNC_HARDPPS_CD
)
732 info
->IER
|= UART_IER_MSI
;
733 if (cflag
& CRTSCTS
) {
734 info
->state
->flags
|= ASYNC_CTS_FLOW
;
735 info
->IER
|= UART_IER_MSI
;
737 info
->state
->flags
&= ~ASYNC_CTS_FLOW
;
739 info
->state
->flags
&= ~ASYNC_CHECK_CD
;
741 info
->state
->flags
|= ASYNC_CHECK_CD
;
742 info
->IER
|= UART_IER_MSI
;
745 * Does clearing IER_MSI imply that we should disable the VBL interrupt ?
749 * Set up parity check flag
752 info
->read_status_mask
= UART_LSR_OE
| UART_LSR_DR
;
753 if (I_INPCK(info
->tty
))
754 info
->read_status_mask
|= UART_LSR_FE
| UART_LSR_PE
;
755 if (I_BRKINT(info
->tty
) || I_PARMRK(info
->tty
))
756 info
->read_status_mask
|= UART_LSR_BI
;
759 * Characters to ignore
761 info
->ignore_status_mask
= 0;
762 if (I_IGNPAR(info
->tty
))
763 info
->ignore_status_mask
|= UART_LSR_PE
| UART_LSR_FE
;
764 if (I_IGNBRK(info
->tty
)) {
765 info
->ignore_status_mask
|= UART_LSR_BI
;
767 * If we're ignore parity and break indicators, ignore
768 * overruns too. (For real raw support).
770 if (I_IGNPAR(info
->tty
))
771 info
->ignore_status_mask
|= UART_LSR_OE
;
774 * !!! ignore all characters if CREAD is not set
776 if ((cflag
& CREAD
) == 0)
777 info
->ignore_status_mask
|= UART_LSR_DR
;
778 local_irq_save(flags
);
783 /* Set up the baud rate */
786 /* Enable or disable parity bit */
788 if(cval
& UART_LCR_PARITY
)
789 serper
|= (SERPER_PARENB
);
791 custom
.serper
= serper
;
795 local_irq_restore(flags
);
798 static int rs_put_char(struct tty_struct
*tty
, unsigned char ch
)
800 struct async_struct
*info
;
803 info
= tty
->driver_data
;
805 if (serial_paranoia_check(info
, tty
->name
, "rs_put_char"))
811 local_irq_save(flags
);
812 if (CIRC_SPACE(info
->xmit
.head
,
814 SERIAL_XMIT_SIZE
) == 0) {
815 local_irq_restore(flags
);
819 info
->xmit
.buf
[info
->xmit
.head
++] = ch
;
820 info
->xmit
.head
&= SERIAL_XMIT_SIZE
-1;
821 local_irq_restore(flags
);
825 static void rs_flush_chars(struct tty_struct
*tty
)
827 struct async_struct
*info
= tty
->driver_data
;
830 if (serial_paranoia_check(info
, tty
->name
, "rs_flush_chars"))
833 if (info
->xmit
.head
== info
->xmit
.tail
839 local_irq_save(flags
);
840 info
->IER
|= UART_IER_THRI
;
841 custom
.intena
= IF_SETCLR
| IF_TBE
;
843 /* set a pending Tx Interrupt, transmitter should restart now */
844 custom
.intreq
= IF_SETCLR
| IF_TBE
;
846 local_irq_restore(flags
);
849 static int rs_write(struct tty_struct
* tty
, const unsigned char *buf
, int count
)
852 struct async_struct
*info
;
855 info
= tty
->driver_data
;
857 if (serial_paranoia_check(info
, tty
->name
, "rs_write"))
863 local_irq_save(flags
);
865 c
= CIRC_SPACE_TO_END(info
->xmit
.head
,
873 memcpy(info
->xmit
.buf
+ info
->xmit
.head
, buf
, c
);
874 info
->xmit
.head
= ((info
->xmit
.head
+ c
) &
875 (SERIAL_XMIT_SIZE
-1));
880 local_irq_restore(flags
);
882 if (info
->xmit
.head
!= info
->xmit
.tail
885 && !(info
->IER
& UART_IER_THRI
)) {
886 info
->IER
|= UART_IER_THRI
;
888 custom
.intena
= IF_SETCLR
| IF_TBE
;
890 /* set a pending Tx Interrupt, transmitter should restart now */
891 custom
.intreq
= IF_SETCLR
| IF_TBE
;
893 local_irq_restore(flags
);
898 static int rs_write_room(struct tty_struct
*tty
)
900 struct async_struct
*info
= tty
->driver_data
;
902 if (serial_paranoia_check(info
, tty
->name
, "rs_write_room"))
904 return CIRC_SPACE(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
);
907 static int rs_chars_in_buffer(struct tty_struct
*tty
)
909 struct async_struct
*info
= tty
->driver_data
;
911 if (serial_paranoia_check(info
, tty
->name
, "rs_chars_in_buffer"))
913 return CIRC_CNT(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
);
916 static void rs_flush_buffer(struct tty_struct
*tty
)
918 struct async_struct
*info
= tty
->driver_data
;
921 if (serial_paranoia_check(info
, tty
->name
, "rs_flush_buffer"))
923 local_irq_save(flags
);
924 info
->xmit
.head
= info
->xmit
.tail
= 0;
925 local_irq_restore(flags
);
930 * This function is used to send a high-priority XON/XOFF character to
933 static void rs_send_xchar(struct tty_struct
*tty
, char ch
)
935 struct async_struct
*info
= tty
->driver_data
;
938 if (serial_paranoia_check(info
, tty
->name
, "rs_send_char"))
943 /* Make sure transmit interrupts are on */
946 local_irq_save(flags
);
947 if(!(custom
.intenar
& IF_TBE
)) {
948 custom
.intena
= IF_SETCLR
| IF_TBE
;
950 /* set a pending Tx Interrupt, transmitter should restart now */
951 custom
.intreq
= IF_SETCLR
| IF_TBE
;
954 local_irq_restore(flags
);
956 info
->IER
|= UART_IER_THRI
;
961 * ------------------------------------------------------------
964 * This routine is called by the upper-layer tty layer to signal that
965 * incoming characters should be throttled.
966 * ------------------------------------------------------------
968 static void rs_throttle(struct tty_struct
* tty
)
970 struct async_struct
*info
= tty
->driver_data
;
972 #ifdef SERIAL_DEBUG_THROTTLE
975 printk("throttle %s: %d....\n", tty_name(tty
, buf
),
976 tty
->ldisc
.chars_in_buffer(tty
));
979 if (serial_paranoia_check(info
, tty
->name
, "rs_throttle"))
983 rs_send_xchar(tty
, STOP_CHAR(tty
));
985 if (tty
->termios
->c_cflag
& CRTSCTS
)
986 info
->MCR
&= ~SER_RTS
;
988 local_irq_save(flags
);
989 rtsdtr_ctrl(info
->MCR
);
990 local_irq_restore(flags
);
993 static void rs_unthrottle(struct tty_struct
* tty
)
995 struct async_struct
*info
= tty
->driver_data
;
997 #ifdef SERIAL_DEBUG_THROTTLE
1000 printk("unthrottle %s: %d....\n", tty_name(tty
, buf
),
1001 tty
->ldisc
.chars_in_buffer(tty
));
1004 if (serial_paranoia_check(info
, tty
->name
, "rs_unthrottle"))
1011 rs_send_xchar(tty
, START_CHAR(tty
));
1013 if (tty
->termios
->c_cflag
& CRTSCTS
)
1014 info
->MCR
|= SER_RTS
;
1015 local_irq_save(flags
);
1016 rtsdtr_ctrl(info
->MCR
);
1017 local_irq_restore(flags
);
1021 * ------------------------------------------------------------
1022 * rs_ioctl() and friends
1023 * ------------------------------------------------------------
1026 static int get_serial_info(struct async_struct
* info
,
1027 struct serial_struct __user
* retinfo
)
1029 struct serial_struct tmp
;
1030 struct serial_state
*state
= info
->state
;
1034 memset(&tmp
, 0, sizeof(tmp
));
1036 tmp
.type
= state
->type
;
1037 tmp
.line
= state
->line
;
1038 tmp
.port
= state
->port
;
1039 tmp
.irq
= state
->irq
;
1040 tmp
.flags
= state
->flags
;
1041 tmp
.xmit_fifo_size
= state
->xmit_fifo_size
;
1042 tmp
.baud_base
= state
->baud_base
;
1043 tmp
.close_delay
= state
->close_delay
;
1044 tmp
.closing_wait
= state
->closing_wait
;
1045 tmp
.custom_divisor
= state
->custom_divisor
;
1047 if (copy_to_user(retinfo
,&tmp
,sizeof(*retinfo
)))
1052 static int set_serial_info(struct async_struct
* info
,
1053 struct serial_struct __user
* new_info
)
1055 struct serial_struct new_serial
;
1056 struct serial_state old_state
, *state
;
1057 unsigned int change_irq
,change_port
;
1060 if (copy_from_user(&new_serial
,new_info
,sizeof(new_serial
)))
1064 state
= info
->state
;
1067 change_irq
= new_serial
.irq
!= state
->irq
;
1068 change_port
= (new_serial
.port
!= state
->port
);
1069 if(change_irq
|| change_port
|| (new_serial
.xmit_fifo_size
!= state
->xmit_fifo_size
)) {
1074 if (!serial_isroot()) {
1075 if ((new_serial
.baud_base
!= state
->baud_base
) ||
1076 (new_serial
.close_delay
!= state
->close_delay
) ||
1077 (new_serial
.xmit_fifo_size
!= state
->xmit_fifo_size
) ||
1078 ((new_serial
.flags
& ~ASYNC_USR_MASK
) !=
1079 (state
->flags
& ~ASYNC_USR_MASK
)))
1081 state
->flags
= ((state
->flags
& ~ASYNC_USR_MASK
) |
1082 (new_serial
.flags
& ASYNC_USR_MASK
));
1083 state
->custom_divisor
= new_serial
.custom_divisor
;
1084 goto check_and_exit
;
1087 if (new_serial
.baud_base
< 9600) {
1093 * OK, past this point, all the error checking has been done.
1094 * At this point, we start making changes.....
1097 state
->baud_base
= new_serial
.baud_base
;
1098 state
->flags
= ((state
->flags
& ~ASYNC_FLAGS
) |
1099 (new_serial
.flags
& ASYNC_FLAGS
));
1100 state
->custom_divisor
= new_serial
.custom_divisor
;
1101 state
->close_delay
= new_serial
.close_delay
* HZ
/100;
1102 state
->closing_wait
= new_serial
.closing_wait
* HZ
/100;
1103 info
->tty
->low_latency
= (state
->flags
& ASYNC_LOW_LATENCY
) ? 1 : 0;
1106 if (state
->flags
& ASYNC_INITIALIZED
) {
1107 if (((old_state
.flags
& ASYNC_SPD_MASK
) !=
1108 (state
->flags
& ASYNC_SPD_MASK
)) ||
1109 (old_state
.custom_divisor
!= state
->custom_divisor
)) {
1110 if ((state
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_HI
)
1111 info
->tty
->alt_speed
= 57600;
1112 if ((state
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_VHI
)
1113 info
->tty
->alt_speed
= 115200;
1114 if ((state
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_SHI
)
1115 info
->tty
->alt_speed
= 230400;
1116 if ((state
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_WARP
)
1117 info
->tty
->alt_speed
= 460800;
1118 change_speed(info
, NULL
);
1121 retval
= startup(info
);
1128 * get_lsr_info - get line status register info
1130 * Purpose: Let user call ioctl() to get info when the UART physically
1131 * is emptied. On bus types like RS485, the transmitter must
1132 * release the bus after transmitting. This must be done when
1133 * the transmit shift register is empty, not be done when the
1134 * transmit holding register is empty. This functionality
1135 * allows an RS485 driver to be written in user space.
1137 static int get_lsr_info(struct async_struct
* info
, unsigned int __user
*value
)
1139 unsigned char status
;
1140 unsigned int result
;
1141 unsigned long flags
;
1143 local_irq_save(flags
);
1144 status
= custom
.serdatr
;
1146 local_irq_restore(flags
);
1147 result
= ((status
& SDR_TSRE
) ? TIOCSER_TEMT
: 0);
1148 if (copy_to_user(value
, &result
, sizeof(int)))
1154 static int rs_tiocmget(struct tty_struct
*tty
)
1156 struct async_struct
* info
= tty
->driver_data
;
1157 unsigned char control
, status
;
1158 unsigned long flags
;
1160 if (serial_paranoia_check(info
, tty
->name
, "rs_ioctl"))
1162 if (tty
->flags
& (1 << TTY_IO_ERROR
))
1165 control
= info
->MCR
;
1166 local_irq_save(flags
);
1168 local_irq_restore(flags
);
1169 return ((control
& SER_RTS
) ? TIOCM_RTS
: 0)
1170 | ((control
& SER_DTR
) ? TIOCM_DTR
: 0)
1171 | (!(status
& SER_DCD
) ? TIOCM_CAR
: 0)
1172 | (!(status
& SER_DSR
) ? TIOCM_DSR
: 0)
1173 | (!(status
& SER_CTS
) ? TIOCM_CTS
: 0);
1176 static int rs_tiocmset(struct tty_struct
*tty
, unsigned int set
,
1179 struct async_struct
* info
= tty
->driver_data
;
1180 unsigned long flags
;
1182 if (serial_paranoia_check(info
, tty
->name
, "rs_ioctl"))
1184 if (tty
->flags
& (1 << TTY_IO_ERROR
))
1187 local_irq_save(flags
);
1188 if (set
& TIOCM_RTS
)
1189 info
->MCR
|= SER_RTS
;
1190 if (set
& TIOCM_DTR
)
1191 info
->MCR
|= SER_DTR
;
1192 if (clear
& TIOCM_RTS
)
1193 info
->MCR
&= ~SER_RTS
;
1194 if (clear
& TIOCM_DTR
)
1195 info
->MCR
&= ~SER_DTR
;
1196 rtsdtr_ctrl(info
->MCR
);
1197 local_irq_restore(flags
);
1202 * rs_break() --- routine which turns the break handling on or off
1204 static int rs_break(struct tty_struct
*tty
, int break_state
)
1206 struct async_struct
* info
= tty
->driver_data
;
1207 unsigned long flags
;
1209 if (serial_paranoia_check(info
, tty
->name
, "rs_break"))
1212 local_irq_save(flags
);
1213 if (break_state
== -1)
1214 custom
.adkcon
= AC_SETCLR
| AC_UARTBRK
;
1216 custom
.adkcon
= AC_UARTBRK
;
1218 local_irq_restore(flags
);
1223 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1224 * Return: write counters to the user passed counter struct
1225 * NB: both 1->0 and 0->1 transitions are counted except for
1226 * RI where only 0->1 is counted.
1228 static int rs_get_icount(struct tty_struct
*tty
,
1229 struct serial_icounter_struct
*icount
)
1231 struct async_struct
*info
= tty
->driver_data
;
1232 struct async_icount cnow
;
1233 unsigned long flags
;
1235 local_irq_save(flags
);
1236 cnow
= info
->state
->icount
;
1237 local_irq_restore(flags
);
1238 icount
->cts
= cnow
.cts
;
1239 icount
->dsr
= cnow
.dsr
;
1240 icount
->rng
= cnow
.rng
;
1241 icount
->dcd
= cnow
.dcd
;
1242 icount
->rx
= cnow
.rx
;
1243 icount
->tx
= cnow
.tx
;
1244 icount
->frame
= cnow
.frame
;
1245 icount
->overrun
= cnow
.overrun
;
1246 icount
->parity
= cnow
.parity
;
1247 icount
->brk
= cnow
.brk
;
1248 icount
->buf_overrun
= cnow
.buf_overrun
;
1253 static int rs_ioctl(struct tty_struct
*tty
,
1254 unsigned int cmd
, unsigned long arg
)
1256 struct async_struct
* info
= tty
->driver_data
;
1257 struct async_icount cprev
, cnow
; /* kernel counter temps */
1258 void __user
*argp
= (void __user
*)arg
;
1259 unsigned long flags
;
1261 if (serial_paranoia_check(info
, tty
->name
, "rs_ioctl"))
1264 if ((cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCSSERIAL
) &&
1265 (cmd
!= TIOCSERCONFIG
) && (cmd
!= TIOCSERGSTRUCT
) &&
1266 (cmd
!= TIOCMIWAIT
) && (cmd
!= TIOCGICOUNT
)) {
1267 if (tty
->flags
& (1 << TTY_IO_ERROR
))
1273 return get_serial_info(info
, argp
);
1275 return set_serial_info(info
, argp
);
1279 case TIOCSERGETLSR
: /* Get line status register */
1280 return get_lsr_info(info
, argp
);
1282 case TIOCSERGSTRUCT
:
1283 if (copy_to_user(argp
,
1284 info
, sizeof(struct async_struct
)))
1289 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1290 * - mask passed in arg for lines of interest
1291 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1292 * Caller should use TIOCGICOUNT to see which one it was
1295 local_irq_save(flags
);
1296 /* note the counters on entry */
1297 cprev
= info
->state
->icount
;
1298 local_irq_restore(flags
);
1300 interruptible_sleep_on(&info
->delta_msr_wait
);
1301 /* see if a signal did it */
1302 if (signal_pending(current
))
1303 return -ERESTARTSYS
;
1304 local_irq_save(flags
);
1305 cnow
= info
->state
->icount
; /* atomic copy */
1306 local_irq_restore(flags
);
1307 if (cnow
.rng
== cprev
.rng
&& cnow
.dsr
== cprev
.dsr
&&
1308 cnow
.dcd
== cprev
.dcd
&& cnow
.cts
== cprev
.cts
)
1309 return -EIO
; /* no change => error */
1310 if ( ((arg
& TIOCM_RNG
) && (cnow
.rng
!= cprev
.rng
)) ||
1311 ((arg
& TIOCM_DSR
) && (cnow
.dsr
!= cprev
.dsr
)) ||
1312 ((arg
& TIOCM_CD
) && (cnow
.dcd
!= cprev
.dcd
)) ||
1313 ((arg
& TIOCM_CTS
) && (cnow
.cts
!= cprev
.cts
)) ) {
1322 /* "setserial -W" is called in Debian boot */
1323 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1327 return -ENOIOCTLCMD
;
1332 static void rs_set_termios(struct tty_struct
*tty
, struct ktermios
*old_termios
)
1334 struct async_struct
*info
= tty
->driver_data
;
1335 unsigned long flags
;
1336 unsigned int cflag
= tty
->termios
->c_cflag
;
1338 change_speed(info
, old_termios
);
1340 /* Handle transition to B0 status */
1341 if ((old_termios
->c_cflag
& CBAUD
) &&
1343 info
->MCR
&= ~(SER_DTR
|SER_RTS
);
1344 local_irq_save(flags
);
1345 rtsdtr_ctrl(info
->MCR
);
1346 local_irq_restore(flags
);
1349 /* Handle transition away from B0 status */
1350 if (!(old_termios
->c_cflag
& CBAUD
) &&
1352 info
->MCR
|= SER_DTR
;
1353 if (!(tty
->termios
->c_cflag
& CRTSCTS
) ||
1354 !test_bit(TTY_THROTTLED
, &tty
->flags
)) {
1355 info
->MCR
|= SER_RTS
;
1357 local_irq_save(flags
);
1358 rtsdtr_ctrl(info
->MCR
);
1359 local_irq_restore(flags
);
1362 /* Handle turning off CRTSCTS */
1363 if ((old_termios
->c_cflag
& CRTSCTS
) &&
1364 !(tty
->termios
->c_cflag
& CRTSCTS
)) {
1365 tty
->hw_stopped
= 0;
1371 * No need to wake up processes in open wait, since they
1372 * sample the CLOCAL flag once, and don't recheck it.
1373 * XXX It's not clear whether the current behavior is correct
1374 * or not. Hence, this may change.....
1376 if (!(old_termios
->c_cflag
& CLOCAL
) &&
1377 (tty
->termios
->c_cflag
& CLOCAL
))
1378 wake_up_interruptible(&info
->open_wait
);
1383 * ------------------------------------------------------------
1386 * This routine is called when the serial port gets closed. First, we
1387 * wait for the last remaining data to be sent. Then, we unlink its
1388 * async structure from the interrupt chain if necessary, and we free
1389 * that IRQ if nothing is left in the chain.
1390 * ------------------------------------------------------------
1392 static void rs_close(struct tty_struct
*tty
, struct file
* filp
)
1394 struct async_struct
* info
= tty
->driver_data
;
1395 struct serial_state
*state
;
1396 unsigned long flags
;
1398 if (!info
|| serial_paranoia_check(info
, tty
->name
, "rs_close"))
1401 state
= info
->state
;
1403 local_irq_save(flags
);
1405 if (tty_hung_up_p(filp
)) {
1406 DBG_CNT("before DEC-hung");
1407 local_irq_restore(flags
);
1411 #ifdef SERIAL_DEBUG_OPEN
1412 printk("rs_close ttys%d, count = %d\n", info
->line
, state
->count
);
1414 if ((tty
->count
== 1) && (state
->count
!= 1)) {
1416 * Uh, oh. tty->count is 1, which means that the tty
1417 * structure will be freed. state->count should always
1418 * be one in these conditions. If it's greater than
1419 * one, we've got real problems, since it means the
1420 * serial port won't be shutdown.
1422 printk("rs_close: bad serial port count; tty->count is 1, "
1423 "state->count is %d\n", state
->count
);
1426 if (--state
->count
< 0) {
1427 printk("rs_close: bad serial port count for ttys%d: %d\n",
1428 info
->line
, state
->count
);
1432 DBG_CNT("before DEC-2");
1433 local_irq_restore(flags
);
1436 state
->flags
|= ASYNC_CLOSING
;
1438 * Now we wait for the transmit buffer to clear; and we notify
1439 * the line discipline to only process XON/XOFF characters.
1442 if (info
->closing_wait
!= ASYNC_CLOSING_WAIT_NONE
)
1443 tty_wait_until_sent(tty
, info
->closing_wait
);
1445 * At this point we stop accepting input. To do this, we
1446 * disable the receive line status interrupts, and tell the
1447 * interrupt driver to stop checking the data ready bit in the
1448 * line status register.
1450 info
->read_status_mask
&= ~UART_LSR_DR
;
1451 if (state
->flags
& ASYNC_INITIALIZED
) {
1452 /* disable receive interrupts */
1453 custom
.intena
= IF_RBF
;
1455 /* clear any pending receive interrupt */
1456 custom
.intreq
= IF_RBF
;
1460 * Before we drop DTR, make sure the UART transmitter
1461 * has completely drained; this is especially
1462 * important if there is a transmit FIFO!
1464 rs_wait_until_sent(tty
, info
->timeout
);
1467 rs_flush_buffer(tty
);
1469 tty_ldisc_flush(tty
);
1472 if (info
->blocked_open
) {
1473 if (info
->close_delay
) {
1474 msleep_interruptible(jiffies_to_msecs(info
->close_delay
));
1476 wake_up_interruptible(&info
->open_wait
);
1478 state
->flags
&= ~(ASYNC_NORMAL_ACTIVE
|ASYNC_CLOSING
);
1479 wake_up_interruptible(&info
->close_wait
);
1480 local_irq_restore(flags
);
1484 * rs_wait_until_sent() --- wait until the transmitter is empty
1486 static void rs_wait_until_sent(struct tty_struct
*tty
, int timeout
)
1488 struct async_struct
* info
= tty
->driver_data
;
1489 unsigned long orig_jiffies
, char_time
;
1492 if (serial_paranoia_check(info
, tty
->name
, "rs_wait_until_sent"))
1495 if (info
->xmit_fifo_size
== 0)
1496 return; /* Just in case.... */
1498 orig_jiffies
= jiffies
;
1501 * Set the check interval to be 1/5 of the estimated time to
1502 * send a single character, and make it at least 1. The check
1503 * interval should also be less than the timeout.
1505 * Note: we have to use pretty tight timings here to satisfy
1508 char_time
= (info
->timeout
- HZ
/50) / info
->xmit_fifo_size
;
1509 char_time
= char_time
/ 5;
1513 char_time
= min_t(unsigned long, char_time
, timeout
);
1515 * If the transmitter hasn't cleared in twice the approximate
1516 * amount of time to send the entire FIFO, it probably won't
1517 * ever clear. This assumes the UART isn't doing flow
1518 * control, which is currently the case. Hence, if it ever
1519 * takes longer than info->timeout, this is probably due to a
1520 * UART bug of some kind. So, we clamp the timeout parameter at
1523 if (!timeout
|| timeout
> 2*info
->timeout
)
1524 timeout
= 2*info
->timeout
;
1525 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1526 printk("In rs_wait_until_sent(%d) check=%lu...", timeout
, char_time
);
1527 printk("jiff=%lu...", jiffies
);
1529 while(!((lsr
= custom
.serdatr
) & SDR_TSRE
)) {
1530 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1531 printk("serdatr = %d (jiff=%lu)...", lsr
, jiffies
);
1533 msleep_interruptible(jiffies_to_msecs(char_time
));
1534 if (signal_pending(current
))
1536 if (timeout
&& time_after(jiffies
, orig_jiffies
+ timeout
))
1539 __set_current_state(TASK_RUNNING
);
1541 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1542 printk("lsr = %d (jiff=%lu)...done\n", lsr
, jiffies
);
1547 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1549 static void rs_hangup(struct tty_struct
*tty
)
1551 struct async_struct
* info
= tty
->driver_data
;
1552 struct serial_state
*state
= info
->state
;
1554 if (serial_paranoia_check(info
, tty
->name
, "rs_hangup"))
1557 state
= info
->state
;
1559 rs_flush_buffer(tty
);
1562 state
->flags
&= ~ASYNC_NORMAL_ACTIVE
;
1564 wake_up_interruptible(&info
->open_wait
);
1568 * ------------------------------------------------------------
1569 * rs_open() and friends
1570 * ------------------------------------------------------------
1572 static int block_til_ready(struct tty_struct
*tty
, struct file
* filp
,
1573 struct async_struct
*info
)
1575 #ifdef DECLARE_WAITQUEUE
1576 DECLARE_WAITQUEUE(wait
, current
);
1578 struct wait_queue wait
= { current
, NULL
};
1580 struct serial_state
*state
= info
->state
;
1582 int do_clocal
= 0, extra_count
= 0;
1583 unsigned long flags
;
1586 * If the device is in the middle of being closed, then block
1587 * until it's done, and then try again.
1589 if (tty_hung_up_p(filp
) ||
1590 (state
->flags
& ASYNC_CLOSING
)) {
1591 if (state
->flags
& ASYNC_CLOSING
)
1592 interruptible_sleep_on(&info
->close_wait
);
1593 #ifdef SERIAL_DO_RESTART
1594 return ((state
->flags
& ASYNC_HUP_NOTIFY
) ?
1595 -EAGAIN
: -ERESTARTSYS
);
1602 * If non-blocking mode is set, or the port is not enabled,
1603 * then make the check up front and then exit.
1605 if ((filp
->f_flags
& O_NONBLOCK
) ||
1606 (tty
->flags
& (1 << TTY_IO_ERROR
))) {
1607 state
->flags
|= ASYNC_NORMAL_ACTIVE
;
1611 if (tty
->termios
->c_cflag
& CLOCAL
)
1615 * Block waiting for the carrier detect and the line to become
1616 * free (i.e., not in use by the callout). While we are in
1617 * this loop, state->count is dropped by one, so that
1618 * rs_close() knows when to free things. We restore it upon
1619 * exit, either normal or abnormal.
1622 add_wait_queue(&info
->open_wait
, &wait
);
1623 #ifdef SERIAL_DEBUG_OPEN
1624 printk("block_til_ready before block: ttys%d, count = %d\n",
1625 state
->line
, state
->count
);
1627 local_irq_save(flags
);
1628 if (!tty_hung_up_p(filp
)) {
1632 local_irq_restore(flags
);
1633 info
->blocked_open
++;
1635 local_irq_save(flags
);
1636 if (tty
->termios
->c_cflag
& CBAUD
)
1637 rtsdtr_ctrl(SER_DTR
|SER_RTS
);
1638 local_irq_restore(flags
);
1639 set_current_state(TASK_INTERRUPTIBLE
);
1640 if (tty_hung_up_p(filp
) ||
1641 !(state
->flags
& ASYNC_INITIALIZED
)) {
1642 #ifdef SERIAL_DO_RESTART
1643 if (state
->flags
& ASYNC_HUP_NOTIFY
)
1646 retval
= -ERESTARTSYS
;
1652 if (!(state
->flags
& ASYNC_CLOSING
) &&
1653 (do_clocal
|| (!(ciab
.pra
& SER_DCD
)) ))
1655 if (signal_pending(current
)) {
1656 retval
= -ERESTARTSYS
;
1659 #ifdef SERIAL_DEBUG_OPEN
1660 printk("block_til_ready blocking: ttys%d, count = %d\n",
1661 info
->line
, state
->count
);
1667 __set_current_state(TASK_RUNNING
);
1668 remove_wait_queue(&info
->open_wait
, &wait
);
1671 info
->blocked_open
--;
1672 #ifdef SERIAL_DEBUG_OPEN
1673 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1674 info
->line
, state
->count
);
1678 state
->flags
|= ASYNC_NORMAL_ACTIVE
;
1682 static int get_async_struct(int line
, struct async_struct
**ret_info
)
1684 struct async_struct
*info
;
1685 struct serial_state
*sstate
;
1687 sstate
= rs_table
+ line
;
1690 *ret_info
= sstate
->info
;
1693 info
= kzalloc(sizeof(struct async_struct
), GFP_KERNEL
);
1698 #ifdef DECLARE_WAITQUEUE
1699 init_waitqueue_head(&info
->open_wait
);
1700 init_waitqueue_head(&info
->close_wait
);
1701 init_waitqueue_head(&info
->delta_msr_wait
);
1703 info
->port
= sstate
->port
;
1704 info
->xmit_fifo_size
= sstate
->xmit_fifo_size
;
1706 info
->state
= sstate
;
1709 *ret_info
= sstate
->info
;
1712 *ret_info
= sstate
->info
= info
;
1717 * This routine is called whenever a serial port is opened. It
1718 * enables interrupts for a serial port, linking in its async structure into
1719 * the IRQ chain. It also performs the serial-specific
1720 * initialization for the tty structure.
1722 static int rs_open(struct tty_struct
*tty
, struct file
* filp
)
1724 struct async_struct
*info
;
1727 retval
= get_async_struct(tty
->index
, &info
);
1731 tty
->driver_data
= info
;
1733 if (serial_paranoia_check(info
, tty
->name
, "rs_open"))
1736 #ifdef SERIAL_DEBUG_OPEN
1737 printk("rs_open %s, count = %d\n", tty
->name
, info
->state
->count
);
1739 info
->tty
->low_latency
= (info
->state
->flags
& ASYNC_LOW_LATENCY
) ? 1 : 0;
1742 * If the port is the middle of closing, bail out now
1744 if (tty_hung_up_p(filp
) ||
1745 (info
->state
->flags
& ASYNC_CLOSING
)) {
1746 if (info
->state
->flags
& ASYNC_CLOSING
)
1747 interruptible_sleep_on(&info
->close_wait
);
1748 #ifdef SERIAL_DO_RESTART
1749 return ((info
->state
->flags
& ASYNC_HUP_NOTIFY
) ?
1750 -EAGAIN
: -ERESTARTSYS
);
1757 * Start up serial port
1759 retval
= startup(info
);
1764 retval
= block_til_ready(tty
, filp
, info
);
1766 #ifdef SERIAL_DEBUG_OPEN
1767 printk("rs_open returning after block_til_ready with %d\n",
1773 #ifdef SERIAL_DEBUG_OPEN
1774 printk("rs_open %s successful...", tty
->name
);
1780 * /proc fs routines....
1783 static inline void line_info(struct seq_file
*m
, struct serial_state
*state
)
1785 struct async_struct
*info
= state
->info
, scr_info
;
1786 char stat_buf
[30], control
, status
;
1787 unsigned long flags
;
1789 seq_printf(m
, "%d: uart:amiga_builtin",state
->line
);
1792 * Figure out the current RS-232 lines
1795 info
= &scr_info
; /* This is just for serial_{in,out} */
1800 local_irq_save(flags
);
1802 control
= info
? info
->MCR
: status
;
1803 local_irq_restore(flags
);
1807 if(!(control
& SER_RTS
))
1808 strcat(stat_buf
, "|RTS");
1809 if(!(status
& SER_CTS
))
1810 strcat(stat_buf
, "|CTS");
1811 if(!(control
& SER_DTR
))
1812 strcat(stat_buf
, "|DTR");
1813 if(!(status
& SER_DSR
))
1814 strcat(stat_buf
, "|DSR");
1815 if(!(status
& SER_DCD
))
1816 strcat(stat_buf
, "|CD");
1819 seq_printf(m
, " baud:%d", state
->baud_base
/ info
->quot
);
1822 seq_printf(m
, " tx:%d rx:%d", state
->icount
.tx
, state
->icount
.rx
);
1824 if (state
->icount
.frame
)
1825 seq_printf(m
, " fe:%d", state
->icount
.frame
);
1827 if (state
->icount
.parity
)
1828 seq_printf(m
, " pe:%d", state
->icount
.parity
);
1830 if (state
->icount
.brk
)
1831 seq_printf(m
, " brk:%d", state
->icount
.brk
);
1833 if (state
->icount
.overrun
)
1834 seq_printf(m
, " oe:%d", state
->icount
.overrun
);
1837 * Last thing is the RS-232 status lines
1839 seq_printf(m
, " %s\n", stat_buf
+1);
1842 static int rs_proc_show(struct seq_file
*m
, void *v
)
1844 seq_printf(m
, "serinfo:1.0 driver:%s\n", serial_version
);
1845 line_info(m
, &rs_table
[0]);
1849 static int rs_proc_open(struct inode
*inode
, struct file
*file
)
1851 return single_open(file
, rs_proc_show
, NULL
);
1854 static const struct file_operations rs_proc_fops
= {
1855 .owner
= THIS_MODULE
,
1856 .open
= rs_proc_open
,
1858 .llseek
= seq_lseek
,
1859 .release
= single_release
,
1863 * ---------------------------------------------------------------------
1864 * rs_init() and friends
1866 * rs_init() is called at boot-time to initialize the serial driver.
1867 * ---------------------------------------------------------------------
1871 * This routine prints out the appropriate serial driver version
1872 * number, and identifies which options were configured into this
1875 static void show_serial_version(void)
1877 printk(KERN_INFO
"%s version %s\n", serial_name
, serial_version
);
1881 static const struct tty_operations serial_ops
= {
1885 .put_char
= rs_put_char
,
1886 .flush_chars
= rs_flush_chars
,
1887 .write_room
= rs_write_room
,
1888 .chars_in_buffer
= rs_chars_in_buffer
,
1889 .flush_buffer
= rs_flush_buffer
,
1891 .throttle
= rs_throttle
,
1892 .unthrottle
= rs_unthrottle
,
1893 .set_termios
= rs_set_termios
,
1896 .hangup
= rs_hangup
,
1897 .break_ctl
= rs_break
,
1898 .send_xchar
= rs_send_xchar
,
1899 .wait_until_sent
= rs_wait_until_sent
,
1900 .tiocmget
= rs_tiocmget
,
1901 .tiocmset
= rs_tiocmset
,
1902 .get_icount
= rs_get_icount
,
1903 .proc_fops
= &rs_proc_fops
,
1907 * The serial driver boot-time initialization code!
1909 static int __init
amiga_serial_probe(struct platform_device
*pdev
)
1911 unsigned long flags
;
1912 struct serial_state
* state
;
1915 serial_driver
= alloc_tty_driver(NR_PORTS
);
1921 show_serial_version();
1923 /* Initialize the tty_driver structure */
1925 serial_driver
->driver_name
= "amiserial";
1926 serial_driver
->name
= "ttyS";
1927 serial_driver
->major
= TTY_MAJOR
;
1928 serial_driver
->minor_start
= 64;
1929 serial_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
1930 serial_driver
->subtype
= SERIAL_TYPE_NORMAL
;
1931 serial_driver
->init_termios
= tty_std_termios
;
1932 serial_driver
->init_termios
.c_cflag
=
1933 B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1934 serial_driver
->flags
= TTY_DRIVER_REAL_RAW
;
1935 tty_set_operations(serial_driver
, &serial_ops
);
1937 error
= tty_register_driver(serial_driver
);
1939 goto fail_put_tty_driver
;
1942 state
->port
= (int)&custom
.serdatr
; /* Just to give it a value */
1944 state
->custom_divisor
= 0;
1945 state
->close_delay
= 5*HZ
/10;
1946 state
->closing_wait
= 30*HZ
;
1947 state
->icount
.cts
= state
->icount
.dsr
=
1948 state
->icount
.rng
= state
->icount
.dcd
= 0;
1949 state
->icount
.rx
= state
->icount
.tx
= 0;
1950 state
->icount
.frame
= state
->icount
.parity
= 0;
1951 state
->icount
.overrun
= state
->icount
.brk
= 0;
1953 printk(KERN_INFO
"ttyS%d is the amiga builtin serial port\n",
1956 /* Hardware set up */
1958 state
->baud_base
= amiga_colorclock
;
1959 state
->xmit_fifo_size
= 1;
1961 /* set ISRs, and then disable the rx interrupts */
1962 error
= request_irq(IRQ_AMIGA_TBE
, ser_tx_int
, 0, "serial TX", state
);
1964 goto fail_unregister
;
1966 error
= request_irq(IRQ_AMIGA_RBF
, ser_rx_int
, 0,
1967 "serial RX", state
);
1971 local_irq_save(flags
);
1973 /* turn off Rx and Tx interrupts */
1974 custom
.intena
= IF_RBF
| IF_TBE
;
1977 /* clear any pending interrupt */
1978 custom
.intreq
= IF_RBF
| IF_TBE
;
1981 local_irq_restore(flags
);
1984 * set the appropriate directions for the modem control flags,
1985 * and clear RTS and DTR
1987 ciab
.ddra
|= (SER_DTR
| SER_RTS
); /* outputs */
1988 ciab
.ddra
&= ~(SER_DCD
| SER_CTS
| SER_DSR
); /* inputs */
1990 platform_set_drvdata(pdev
, state
);
1995 free_irq(IRQ_AMIGA_TBE
, state
);
1997 tty_unregister_driver(serial_driver
);
1998 fail_put_tty_driver
:
1999 put_tty_driver(serial_driver
);
2003 static int __exit
amiga_serial_remove(struct platform_device
*pdev
)
2006 struct serial_state
*state
= platform_get_drvdata(pdev
);
2007 struct async_struct
*info
= state
->info
;
2009 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2010 if ((error
= tty_unregister_driver(serial_driver
)))
2011 printk("SERIAL: failed to unregister serial driver (%d)\n",
2013 put_tty_driver(serial_driver
);
2015 rs_table
[0].info
= NULL
;
2018 free_irq(IRQ_AMIGA_TBE
, rs_table
);
2019 free_irq(IRQ_AMIGA_RBF
, rs_table
);
2021 platform_set_drvdata(pdev
, NULL
);
2026 static struct platform_driver amiga_serial_driver
= {
2027 .remove
= __exit_p(amiga_serial_remove
),
2029 .name
= "amiga-serial",
2030 .owner
= THIS_MODULE
,
2034 static int __init
amiga_serial_init(void)
2036 return platform_driver_probe(&amiga_serial_driver
, amiga_serial_probe
);
2039 module_init(amiga_serial_init
);
2041 static void __exit
amiga_serial_exit(void)
2043 platform_driver_unregister(&amiga_serial_driver
);
2046 module_exit(amiga_serial_exit
);
2049 #if defined(CONFIG_SERIAL_CONSOLE) && !defined(MODULE)
2052 * ------------------------------------------------------------
2053 * Serial console driver
2054 * ------------------------------------------------------------
2057 static void amiga_serial_putc(char c
)
2059 custom
.serdat
= (unsigned char)c
| 0x100;
2060 while (!(custom
.serdatr
& 0x2000))
2065 * Print a string to the serial port trying not to disturb
2066 * any possible real use of the port...
2068 * The console must be locked when we get here.
2070 static void serial_console_write(struct console
*co
, const char *s
,
2073 unsigned short intena
= custom
.intenar
;
2075 custom
.intena
= IF_TBE
;
2079 amiga_serial_putc('\r');
2080 amiga_serial_putc(*s
++);
2083 custom
.intena
= IF_SETCLR
| (intena
& IF_TBE
);
2086 static struct tty_driver
*serial_console_device(struct console
*c
, int *index
)
2089 return serial_driver
;
2092 static struct console sercons
= {
2094 .write
= serial_console_write
,
2095 .device
= serial_console_device
,
2096 .flags
= CON_PRINTBUFFER
,
2103 static int __init
amiserial_console_init(void)
2105 register_console(&sercons
);
2108 console_initcall(amiserial_console_init
);
2110 #endif /* CONFIG_SERIAL_CONSOLE && !MODULE */
2112 MODULE_LICENSE("GPL");
2113 MODULE_ALIAS("platform:amiga-serial");