4 * Copyright (C) 1998/1999 R.E.Wolff@BitWizard.nl
6 * written for the SX serial driver.
7 * Contains the code that should be shared over all the serial drivers.
9 * Credit for the idea to do it this way might go to Alan Cox.
12 * Version 0.1 -- December, 1998. Initial version.
13 * Version 0.2 -- March, 1999. Some more routines. Bugfixes. Etc.
14 * Version 0.5 -- August, 1999. Some more fixes. Reformat for Linus.
16 * BitWizard is actively maintaining this file. We sometimes find
17 * that someone submitted changes to this file. We really appreciate
18 * your help, but please submit changes through us. We're doing our
19 * best to be responsive. -- REW
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/tty.h>
25 #include <linux/serial.h>
27 #include <linux/generic_serial.h>
28 #include <linux/interrupt.h>
29 #include <linux/tty_flip.h>
30 #include <linux/delay.h>
31 #include <asm/semaphore.h>
32 #include <asm/uaccess.h>
39 #define gs_dprintk(f, str...) if (gs_debug & f) printk (str)
41 #define gs_dprintk(f, str...) /* nothing */
44 #define func_enter() gs_dprintk (GS_DEBUG_FLOW, "gs: enter %s\n", __FUNCTION__)
45 #define func_exit() gs_dprintk (GS_DEBUG_FLOW, "gs: exit %s\n", __FUNCTION__)
47 #define RS_EVENT_WRITE_WAKEUP 1
49 module_param(gs_debug
, int, 0644);
52 void gs_put_char(struct tty_struct
* tty
, unsigned char ch
)
60 port
= tty
->driver_data
;
64 if (! (port
->flags
& ASYNC_INITIALIZED
)) return;
66 /* Take a lock on the serial tranmit buffer! */
67 mutex_lock(& port
->port_write_mutex
);
69 if (port
->xmit_cnt
>= SERIAL_XMIT_SIZE
- 1) {
70 /* Sorry, buffer is full, drop character. Update statistics???? -- REW */
71 mutex_unlock(&port
->port_write_mutex
);
75 port
->xmit_buf
[port
->xmit_head
++] = ch
;
76 port
->xmit_head
&= SERIAL_XMIT_SIZE
- 1;
77 port
->xmit_cnt
++; /* Characters in buffer */
79 mutex_unlock(&port
->port_write_mutex
);
85 > Problems to take into account are:
86 > -1- Interrupts that empty part of the buffer.
87 > -2- page faults on the access to userspace.
88 > -3- Other processes that are also trying to do a "write".
91 int gs_write(struct tty_struct
* tty
,
92 const unsigned char *buf
, int count
)
102 port
= tty
->driver_data
;
106 if (! (port
->flags
& ASYNC_INITIALIZED
))
109 /* get exclusive "write" access to this port (problem 3) */
110 /* This is not a spinlock because we can have a disk access (page
111 fault) in copy_from_user */
112 mutex_lock(& port
->port_write_mutex
);
118 /* This is safe because we "OWN" the "head". Noone else can
119 change the "head": we own the port_write_mutex. */
120 /* Don't overrun the end of the buffer */
121 t
= SERIAL_XMIT_SIZE
- port
->xmit_head
;
124 /* This is safe because the xmit_cnt can only decrease. This
125 would increase "t", so we might copy too little chars. */
126 /* Don't copy past the "head" of the buffer */
127 t
= SERIAL_XMIT_SIZE
- 1 - port
->xmit_cnt
;
130 /* Can't copy more? break out! */
133 memcpy (port
->xmit_buf
+ port
->xmit_head
, buf
, c
);
135 port
-> xmit_cnt
+= c
;
136 port
-> xmit_head
= (port
->xmit_head
+ c
) & (SERIAL_XMIT_SIZE
-1);
141 mutex_unlock(& port
->port_write_mutex
);
143 gs_dprintk (GS_DEBUG_WRITE
, "write: interrupts are %s\n",
144 (port
->flags
& GS_TX_INTEN
)?"enabled": "disabled");
146 if (port
->xmit_cnt
&&
149 !(port
->flags
& GS_TX_INTEN
)) {
150 port
->flags
|= GS_TX_INTEN
;
151 port
->rd
->enable_tx_interrupts (port
);
159 int gs_write_room(struct tty_struct
* tty
)
161 struct gs_port
*port
= tty
->driver_data
;
165 ret
= SERIAL_XMIT_SIZE
- port
->xmit_cnt
- 1;
173 int gs_chars_in_buffer(struct tty_struct
*tty
)
175 struct gs_port
*port
= tty
->driver_data
;
179 return port
->xmit_cnt
;
183 static int gs_real_chars_in_buffer(struct tty_struct
*tty
)
185 struct gs_port
*port
;
189 port
= tty
->driver_data
;
191 if (!port
->rd
) return 0;
192 if (!port
->rd
->chars_in_buffer
) return 0;
195 return port
->xmit_cnt
+ port
->rd
->chars_in_buffer (port
);
199 static int gs_wait_tx_flushed (void * ptr
, unsigned long timeout
)
201 struct gs_port
*port
= ptr
;
202 unsigned long end_jiffies
;
203 int jiffies_to_transmit
, charsleft
= 0, rv
= 0;
208 gs_dprintk (GS_DEBUG_FLUSH
, "port=%p.\n", port
);
210 gs_dprintk (GS_DEBUG_FLUSH
, "xmit_cnt=%x, xmit_buf=%p, tty=%p.\n",
211 port
->xmit_cnt
, port
->xmit_buf
, port
->tty
);
214 if (!port
|| port
->xmit_cnt
< 0 || !port
->xmit_buf
) {
215 gs_dprintk (GS_DEBUG_FLUSH
, "ERROR: !port, !port->xmit_buf or prot->xmit_cnt < 0.\n");
217 return -EINVAL
; /* This is an error which we don't know how to handle. */
220 rcib
= gs_real_chars_in_buffer(port
->tty
);
223 gs_dprintk (GS_DEBUG_FLUSH
, "nothing to wait for.\n");
227 /* stop trying: now + twice the time it would normally take + seconds */
228 if (timeout
== 0) timeout
= MAX_SCHEDULE_TIMEOUT
;
229 end_jiffies
= jiffies
;
230 if (timeout
!= MAX_SCHEDULE_TIMEOUT
)
231 end_jiffies
+= port
->baud
?(2 * rcib
* 10 * HZ
/ port
->baud
):0;
232 end_jiffies
+= timeout
;
234 gs_dprintk (GS_DEBUG_FLUSH
, "now=%lx, end=%lx (%ld).\n",
235 jiffies
, end_jiffies
, end_jiffies
-jiffies
);
237 /* the expression is actually jiffies < end_jiffies, but that won't
238 work around the wraparound. Tricky eh? */
239 while ((charsleft
= gs_real_chars_in_buffer (port
->tty
)) &&
240 time_after (end_jiffies
, jiffies
)) {
242 chars * (bits/char) * (jiffies /sec) / (bits/sec) = jiffies!
245 charsleft
+= 16; /* Allow 16 chars more to be transmitted ... */
246 jiffies_to_transmit
= port
->baud
?(1 + charsleft
* 10 * HZ
/ port
->baud
):0;
247 /* ^^^ Round up.... */
248 if (jiffies_to_transmit
<= 0) jiffies_to_transmit
= 1;
250 gs_dprintk (GS_DEBUG_FLUSH
, "Expect to finish in %d jiffies "
251 "(%d chars).\n", jiffies_to_transmit
, charsleft
);
253 msleep_interruptible(jiffies_to_msecs(jiffies_to_transmit
));
254 if (signal_pending (current
)) {
255 gs_dprintk (GS_DEBUG_FLUSH
, "Signal pending. Bombing out: ");
261 gs_dprintk (GS_DEBUG_FLUSH
, "charsleft = %d.\n", charsleft
);
262 set_current_state (TASK_RUNNING
);
270 void gs_flush_buffer(struct tty_struct
*tty
)
272 struct gs_port
*port
;
279 port
= tty
->driver_data
;
283 /* XXX Would the write semaphore do? */
284 spin_lock_irqsave (&port
->driver_lock
, flags
);
285 port
->xmit_cnt
= port
->xmit_head
= port
->xmit_tail
= 0;
286 spin_unlock_irqrestore (&port
->driver_lock
, flags
);
293 void gs_flush_chars(struct tty_struct
* tty
)
295 struct gs_port
*port
;
301 port
= tty
->driver_data
;
305 if (port
->xmit_cnt
<= 0 || tty
->stopped
|| tty
->hw_stopped
||
311 /* Beats me -- REW */
312 port
->flags
|= GS_TX_INTEN
;
313 port
->rd
->enable_tx_interrupts (port
);
318 void gs_stop(struct tty_struct
* tty
)
320 struct gs_port
*port
;
326 port
= tty
->driver_data
;
330 if (port
->xmit_cnt
&&
332 (port
->flags
& GS_TX_INTEN
) ) {
333 port
->flags
&= ~GS_TX_INTEN
;
334 port
->rd
->disable_tx_interrupts (port
);
340 void gs_start(struct tty_struct
* tty
)
342 struct gs_port
*port
;
346 port
= tty
->driver_data
;
350 if (port
->xmit_cnt
&&
352 !(port
->flags
& GS_TX_INTEN
) ) {
353 port
->flags
|= GS_TX_INTEN
;
354 port
->rd
->enable_tx_interrupts (port
);
360 static void gs_shutdown_port (struct gs_port
*port
)
368 if (!(port
->flags
& ASYNC_INITIALIZED
))
371 spin_lock_irqsave(&port
->driver_lock
, flags
);
373 if (port
->xmit_buf
) {
374 free_page((unsigned long) port
->xmit_buf
);
375 port
->xmit_buf
= NULL
;
379 set_bit(TTY_IO_ERROR
, &port
->tty
->flags
);
381 port
->rd
->shutdown_port (port
);
383 port
->flags
&= ~ASYNC_INITIALIZED
;
384 spin_unlock_irqrestore(&port
->driver_lock
, flags
);
390 void gs_hangup(struct tty_struct
*tty
)
392 struct gs_port
*port
;
398 port
= tty
->driver_data
;
403 gs_shutdown_port (port
);
404 port
->flags
&= ~(ASYNC_NORMAL_ACTIVE
|GS_ACTIVE
);
408 wake_up_interruptible(&port
->open_wait
);
413 int gs_block_til_ready(void *port_
, struct file
* filp
)
415 struct gs_port
*port
= port_
;
416 DECLARE_WAITQUEUE(wait
, current
);
420 struct tty_struct
*tty
;
431 gs_dprintk (GS_DEBUG_BTR
, "Entering gs_block_till_ready.\n");
433 * If the device is in the middle of being closed, then block
434 * until it's done, and then try again.
436 if (tty_hung_up_p(filp
) || port
->flags
& ASYNC_CLOSING
) {
437 interruptible_sleep_on(&port
->close_wait
);
438 if (port
->flags
& ASYNC_HUP_NOTIFY
)
444 gs_dprintk (GS_DEBUG_BTR
, "after hung up\n");
447 * If non-blocking mode is set, or the port is not enabled,
448 * then make the check up front and then exit.
450 if ((filp
->f_flags
& O_NONBLOCK
) ||
451 (tty
->flags
& (1 << TTY_IO_ERROR
))) {
452 port
->flags
|= ASYNC_NORMAL_ACTIVE
;
456 gs_dprintk (GS_DEBUG_BTR
, "after nonblock\n");
462 * Block waiting for the carrier detect and the line to become
463 * free (i.e., not in use by the callout). While we are in
464 * this loop, port->count is dropped by one, so that
465 * rs_close() knows when to free things. We restore it upon
466 * exit, either normal or abnormal.
470 add_wait_queue(&port
->open_wait
, &wait
);
472 gs_dprintk (GS_DEBUG_BTR
, "after add waitq.\n");
473 spin_lock_irqsave(&port
->driver_lock
, flags
);
474 if (!tty_hung_up_p(filp
)) {
477 spin_unlock_irqrestore(&port
->driver_lock
, flags
);
478 port
->blocked_open
++;
480 CD
= port
->rd
->get_CD (port
);
481 gs_dprintk (GS_DEBUG_BTR
, "CD is now %d.\n", CD
);
482 set_current_state (TASK_INTERRUPTIBLE
);
483 if (tty_hung_up_p(filp
) ||
484 !(port
->flags
& ASYNC_INITIALIZED
)) {
485 if (port
->flags
& ASYNC_HUP_NOTIFY
)
488 retval
= -ERESTARTSYS
;
491 if (!(port
->flags
& ASYNC_CLOSING
) &&
494 gs_dprintk (GS_DEBUG_BTR
, "signal_pending is now: %d (%lx)\n",
495 (int)signal_pending (current
), *(long*)(¤t
->blocked
));
496 if (signal_pending(current
)) {
497 retval
= -ERESTARTSYS
;
502 gs_dprintk (GS_DEBUG_BTR
, "Got out of the loop. (%d)\n",
504 set_current_state (TASK_RUNNING
);
505 remove_wait_queue(&port
->open_wait
, &wait
);
506 if (!tty_hung_up_p(filp
)) {
509 port
->blocked_open
--;
513 port
->flags
|= ASYNC_NORMAL_ACTIVE
;
519 void gs_close(struct tty_struct
* tty
, struct file
* filp
)
522 struct gs_port
*port
;
528 port
= (struct gs_port
*) tty
->driver_data
;
533 /* This seems to happen when this is called from vhangup. */
534 gs_dprintk (GS_DEBUG_CLOSE
, "gs: Odd: port->tty is NULL\n");
538 spin_lock_irqsave(&port
->driver_lock
, flags
);
540 if (tty_hung_up_p(filp
)) {
541 spin_unlock_irqrestore(&port
->driver_lock
, flags
);
542 if (port
->rd
->hungup
)
543 port
->rd
->hungup (port
);
548 if ((tty
->count
== 1) && (port
->count
!= 1)) {
549 printk(KERN_ERR
"gs: gs_close port %p: bad port count;"
550 " tty->count is 1, port count is %d\n", port
, port
->count
);
553 if (--port
->count
< 0) {
554 printk(KERN_ERR
"gs: gs_close port %p: bad port count: %d\n", port
, port
->count
);
559 gs_dprintk(GS_DEBUG_CLOSE
, "gs_close port %p: count: %d\n", port
, port
->count
);
560 spin_unlock_irqrestore(&port
->driver_lock
, flags
);
564 port
->flags
|= ASYNC_CLOSING
;
567 * Now we wait for the transmit buffer to clear; and we notify
568 * the line discipline to only process XON/XOFF characters.
571 /* if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
572 tty_wait_until_sent(tty, port->closing_wait); */
575 * At this point we stop accepting input. To do this, we
576 * disable the receive line status interrupts, and tell the
577 * interrupt driver to stop checking the data ready bit in the
578 * line status register.
581 port
->rd
->disable_rx_interrupts (port
);
582 spin_unlock_irqrestore(&port
->driver_lock
, flags
);
584 /* close has no way of returning "EINTR", so discard return value */
585 if (port
->closing_wait
!= ASYNC_CLOSING_WAIT_NONE
)
586 gs_wait_tx_flushed (port
, port
->closing_wait
);
588 port
->flags
&= ~GS_ACTIVE
;
590 if (tty
->driver
->flush_buffer
)
591 tty
->driver
->flush_buffer(tty
);
593 tty_ldisc_flush(tty
);
597 port
->rd
->close (port
);
598 port
->rd
->shutdown_port (port
);
601 if (port
->blocked_open
) {
602 if (port
->close_delay
) {
603 spin_unlock_irqrestore(&port
->driver_lock
, flags
);
604 msleep_interruptible(jiffies_to_msecs(port
->close_delay
));
605 spin_lock_irqsave(&port
->driver_lock
, flags
);
607 wake_up_interruptible(&port
->open_wait
);
609 port
->flags
&= ~(ASYNC_NORMAL_ACTIVE
|ASYNC_CLOSING
| ASYNC_INITIALIZED
);
610 wake_up_interruptible(&port
->close_wait
);
616 void gs_set_termios (struct tty_struct
* tty
,
617 struct ktermios
* old_termios
)
619 struct gs_port
*port
;
620 int baudrate
, tmp
, rv
;
621 struct ktermios
*tiosp
;
627 port
= tty
->driver_data
;
631 /* This seems to happen when this is called after gs_close. */
632 gs_dprintk (GS_DEBUG_TERMIOS
, "gs: Odd: port->tty is NULL\n");
637 tiosp
= tty
->termios
;
639 if (gs_debug
& GS_DEBUG_TERMIOS
) {
640 gs_dprintk (GS_DEBUG_TERMIOS
, "termios structure (%p):\n", tiosp
);
643 if(old_termios
&& (gs_debug
& GS_DEBUG_TERMIOS
)) {
644 if(tiosp
->c_iflag
!= old_termios
->c_iflag
) printk("c_iflag changed\n");
645 if(tiosp
->c_oflag
!= old_termios
->c_oflag
) printk("c_oflag changed\n");
646 if(tiosp
->c_cflag
!= old_termios
->c_cflag
) printk("c_cflag changed\n");
647 if(tiosp
->c_lflag
!= old_termios
->c_lflag
) printk("c_lflag changed\n");
648 if(tiosp
->c_line
!= old_termios
->c_line
) printk("c_line changed\n");
649 if(!memcmp(tiosp
->c_cc
, old_termios
->c_cc
, NCC
)) printk("c_cc changed\n");
652 baudrate
= tty_get_baud_rate(tty
);
654 if ((tiosp
->c_cflag
& CBAUD
) == B38400
) {
655 if ( (port
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_HI
)
657 else if ((port
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_VHI
)
659 else if ((port
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_SHI
)
661 else if ((port
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_WARP
)
663 else if ((port
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_CUST
)
664 baudrate
= (port
->baud_base
/ port
->custom_divisor
);
667 /* I recommend using THIS instead of the mess in termios (and
668 duplicating the above code). Next we should create a clean
669 interface towards this variable. If your card supports arbitrary
670 baud rates, (e.g. CD1400 or 16550 based cards) then everything
671 will be very easy..... */
672 port
->baud
= baudrate
;
674 /* Two timer ticks seems enough to wakeup something like SLIP driver */
675 /* Baudrate/10 is cps. Divide by HZ to get chars per tick. */
676 tmp
= (baudrate
/ 10 / HZ
) * 2;
678 if (tmp
< 0) tmp
= 0;
679 if (tmp
>= SERIAL_XMIT_SIZE
) tmp
= SERIAL_XMIT_SIZE
-1;
681 port
->wakeup_chars
= tmp
;
683 /* We should really wait for the characters to be all sent before
684 changing the settings. -- CAL */
685 rv
= gs_wait_tx_flushed (port
, MAX_SCHEDULE_TIMEOUT
);
686 if (rv
< 0) return /* rv */;
688 rv
= port
->rd
->set_real_termios(port
);
689 if (rv
< 0) return /* rv */;
692 (old_termios
->c_cflag
& CRTSCTS
)) &&
693 !( tiosp
->c_cflag
& CRTSCTS
)) {
698 #ifdef tytso_patch_94Nov25_1726
699 /* This "makes sense", Why is it commented out? */
701 if (!(old_termios
->c_cflag
& CLOCAL
) &&
702 (tty
->termios
->c_cflag
& CLOCAL
))
703 wake_up_interruptible(&port
->gs
.open_wait
);
712 /* Must be called with interrupts enabled */
713 int gs_init_port(struct gs_port
*port
)
719 if (port
->flags
& ASYNC_INITIALIZED
) {
723 if (!port
->xmit_buf
) {
724 /* We may sleep in get_zeroed_page() */
727 tmp
= get_zeroed_page(GFP_KERNEL
);
728 spin_lock_irqsave (&port
->driver_lock
, flags
);
732 port
->xmit_buf
= (unsigned char *) tmp
;
733 spin_unlock_irqrestore(&port
->driver_lock
, flags
);
734 if (!port
->xmit_buf
) {
740 spin_lock_irqsave (&port
->driver_lock
, flags
);
742 clear_bit(TTY_IO_ERROR
, &port
->tty
->flags
);
743 mutex_init(&port
->port_write_mutex
);
744 port
->xmit_cnt
= port
->xmit_head
= port
->xmit_tail
= 0;
745 spin_unlock_irqrestore(&port
->driver_lock
, flags
);
746 gs_set_termios(port
->tty
, NULL
);
747 spin_lock_irqsave (&port
->driver_lock
, flags
);
748 port
->flags
|= ASYNC_INITIALIZED
;
749 port
->flags
&= ~GS_TX_INTEN
;
751 spin_unlock_irqrestore(&port
->driver_lock
, flags
);
757 int gs_setserial(struct gs_port
*port
, struct serial_struct __user
*sp
)
759 struct serial_struct sio
;
761 if (copy_from_user(&sio
, sp
, sizeof(struct serial_struct
)))
764 if (!capable(CAP_SYS_ADMIN
)) {
765 if ((sio
.baud_base
!= port
->baud_base
) ||
766 (sio
.close_delay
!= port
->close_delay
) ||
767 ((sio
.flags
& ~ASYNC_USR_MASK
) !=
768 (port
->flags
& ~ASYNC_USR_MASK
)))
772 port
->flags
= (port
->flags
& ~ASYNC_USR_MASK
) |
773 (sio
.flags
& ASYNC_USR_MASK
);
775 port
->baud_base
= sio
.baud_base
;
776 port
->close_delay
= sio
.close_delay
;
777 port
->closing_wait
= sio
.closing_wait
;
778 port
->custom_divisor
= sio
.custom_divisor
;
780 gs_set_termios (port
->tty
, NULL
);
786 /*****************************************************************************/
789 * Generate the serial struct info.
792 int gs_getserial(struct gs_port
*port
, struct serial_struct __user
*sp
)
794 struct serial_struct sio
;
796 memset(&sio
, 0, sizeof(struct serial_struct
));
797 sio
.flags
= port
->flags
;
798 sio
.baud_base
= port
->baud_base
;
799 sio
.close_delay
= port
->close_delay
;
800 sio
.closing_wait
= port
->closing_wait
;
801 sio
.custom_divisor
= port
->custom_divisor
;
804 /* If you want you can override these. */
805 sio
.type
= PORT_UNKNOWN
;
806 sio
.xmit_fifo_size
= -1;
811 if (port
->rd
->getserial
)
812 port
->rd
->getserial (port
, &sio
);
814 if (copy_to_user(sp
, &sio
, sizeof(struct serial_struct
)))
821 void gs_got_break(struct gs_port
*port
)
825 tty_insert_flip_char(port
->tty
, 0, TTY_BREAK
);
826 tty_schedule_flip(port
->tty
);
827 if (port
->flags
& ASYNC_SAK
) {
835 EXPORT_SYMBOL(gs_put_char
);
836 EXPORT_SYMBOL(gs_write
);
837 EXPORT_SYMBOL(gs_write_room
);
838 EXPORT_SYMBOL(gs_chars_in_buffer
);
839 EXPORT_SYMBOL(gs_flush_buffer
);
840 EXPORT_SYMBOL(gs_flush_chars
);
841 EXPORT_SYMBOL(gs_stop
);
842 EXPORT_SYMBOL(gs_start
);
843 EXPORT_SYMBOL(gs_hangup
);
844 EXPORT_SYMBOL(gs_block_til_ready
);
845 EXPORT_SYMBOL(gs_close
);
846 EXPORT_SYMBOL(gs_set_termios
);
847 EXPORT_SYMBOL(gs_init_port
);
848 EXPORT_SYMBOL(gs_setserial
);
849 EXPORT_SYMBOL(gs_getserial
);
850 EXPORT_SYMBOL(gs_got_break
);
852 MODULE_LICENSE("GPL");