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/uaccess.h>
38 #define gs_dprintk(f, str...) if (gs_debug & f) printk (str)
40 #define gs_dprintk(f, str...) /* nothing */
43 #define func_enter() gs_dprintk (GS_DEBUG_FLOW, "gs: enter %s\n", __FUNCTION__)
44 #define func_exit() gs_dprintk (GS_DEBUG_FLOW, "gs: exit %s\n", __FUNCTION__)
46 #define RS_EVENT_WRITE_WAKEUP 1
48 module_param(gs_debug, int, 0644);
51 void gs_put_char(struct tty_struct * tty, unsigned char ch)
59 port = tty->driver_data;
63 if (! (port->flags & ASYNC_INITIALIZED)) return;
65 /* Take a lock on the serial tranmit buffer! */
66 mutex_lock(& port->port_write_mutex);
68 if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
69 /* Sorry, buffer is full, drop character. Update statistics???? -- REW */
70 mutex_unlock(&port->port_write_mutex);
74 port->xmit_buf[port->xmit_head++] = ch;
75 port->xmit_head &= SERIAL_XMIT_SIZE - 1;
76 port->xmit_cnt++; /* Characters in buffer */
78 mutex_unlock(&port->port_write_mutex);
84 > Problems to take into account are:
85 > -1- Interrupts that empty part of the buffer.
86 > -2- page faults on the access to userspace.
87 > -3- Other processes that are also trying to do a "write".
90 int gs_write(struct tty_struct * tty,
91 const unsigned char *buf, int count)
101 port = tty->driver_data;
105 if (! (port->flags & ASYNC_INITIALIZED))
108 /* get exclusive "write" access to this port (problem 3) */
109 /* This is not a spinlock because we can have a disk access (page
110 fault) in copy_from_user */
111 mutex_lock(& port->port_write_mutex);
117 /* This is safe because we "OWN" the "head". Noone else can
118 change the "head": we own the port_write_mutex. */
119 /* Don't overrun the end of the buffer */
120 t = SERIAL_XMIT_SIZE - port->xmit_head;
123 /* This is safe because the xmit_cnt can only decrease. This
124 would increase "t", so we might copy too little chars. */
125 /* Don't copy past the "head" of the buffer */
126 t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
129 /* Can't copy more? break out! */
132 memcpy (port->xmit_buf + port->xmit_head, buf, c);
134 port -> xmit_cnt += c;
135 port -> xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE -1);
140 mutex_unlock(& port->port_write_mutex);
142 gs_dprintk (GS_DEBUG_WRITE, "write: interrupts are %s\n",
143 (port->flags & GS_TX_INTEN)?"enabled": "disabled");
145 if (port->xmit_cnt &&
148 !(port->flags & GS_TX_INTEN)) {
149 port->flags |= GS_TX_INTEN;
150 port->rd->enable_tx_interrupts (port);
158 int gs_write_room(struct tty_struct * tty)
160 struct gs_port *port = tty->driver_data;
164 ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
172 int gs_chars_in_buffer(struct tty_struct *tty)
174 struct gs_port *port = tty->driver_data;
178 return port->xmit_cnt;
182 static int gs_real_chars_in_buffer(struct tty_struct *tty)
184 struct gs_port *port;
188 port = tty->driver_data;
190 if (!port->rd) return 0;
191 if (!port->rd->chars_in_buffer) return 0;
194 return port->xmit_cnt + port->rd->chars_in_buffer (port);
198 static int gs_wait_tx_flushed (void * ptr, unsigned long timeout)
200 struct gs_port *port = ptr;
201 unsigned long end_jiffies;
202 int jiffies_to_transmit, charsleft = 0, rv = 0;
207 gs_dprintk (GS_DEBUG_FLUSH, "port=%p.\n", port);
209 gs_dprintk (GS_DEBUG_FLUSH, "xmit_cnt=%x, xmit_buf=%p, tty=%p.\n",
210 port->xmit_cnt, port->xmit_buf, port->tty);
213 if (!port || port->xmit_cnt < 0 || !port->xmit_buf) {
214 gs_dprintk (GS_DEBUG_FLUSH, "ERROR: !port, !port->xmit_buf or prot->xmit_cnt < 0.\n");
216 return -EINVAL; /* This is an error which we don't know how to handle. */
219 rcib = gs_real_chars_in_buffer(port->tty);
222 gs_dprintk (GS_DEBUG_FLUSH, "nothing to wait for.\n");
226 /* stop trying: now + twice the time it would normally take + seconds */
227 if (timeout == 0) timeout = MAX_SCHEDULE_TIMEOUT;
228 end_jiffies = jiffies;
229 if (timeout != MAX_SCHEDULE_TIMEOUT)
230 end_jiffies += port->baud?(2 * rcib * 10 * HZ / port->baud):0;
231 end_jiffies += timeout;
233 gs_dprintk (GS_DEBUG_FLUSH, "now=%lx, end=%lx (%ld).\n",
234 jiffies, end_jiffies, end_jiffies-jiffies);
236 /* the expression is actually jiffies < end_jiffies, but that won't
237 work around the wraparound. Tricky eh? */
238 while ((charsleft = gs_real_chars_in_buffer (port->tty)) &&
239 time_after (end_jiffies, jiffies)) {
241 chars * (bits/char) * (jiffies /sec) / (bits/sec) = jiffies!
244 charsleft += 16; /* Allow 16 chars more to be transmitted ... */
245 jiffies_to_transmit = port->baud?(1 + charsleft * 10 * HZ / port->baud):0;
246 /* ^^^ Round up.... */
247 if (jiffies_to_transmit <= 0) jiffies_to_transmit = 1;
249 gs_dprintk (GS_DEBUG_FLUSH, "Expect to finish in %d jiffies "
250 "(%d chars).\n", jiffies_to_transmit, charsleft);
252 msleep_interruptible(jiffies_to_msecs(jiffies_to_transmit));
253 if (signal_pending (current)) {
254 gs_dprintk (GS_DEBUG_FLUSH, "Signal pending. Bombing out: ");
260 gs_dprintk (GS_DEBUG_FLUSH, "charsleft = %d.\n", charsleft);
261 set_current_state (TASK_RUNNING);
269 void gs_flush_buffer(struct tty_struct *tty)
271 struct gs_port *port;
278 port = tty->driver_data;
282 /* XXX Would the write semaphore do? */
283 spin_lock_irqsave (&port->driver_lock, flags);
284 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
285 spin_unlock_irqrestore (&port->driver_lock, flags);
292 void gs_flush_chars(struct tty_struct * tty)
294 struct gs_port *port;
300 port = tty->driver_data;
304 if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
310 /* Beats me -- REW */
311 port->flags |= GS_TX_INTEN;
312 port->rd->enable_tx_interrupts (port);
317 void gs_stop(struct tty_struct * tty)
319 struct gs_port *port;
325 port = tty->driver_data;
329 if (port->xmit_cnt &&
331 (port->flags & GS_TX_INTEN) ) {
332 port->flags &= ~GS_TX_INTEN;
333 port->rd->disable_tx_interrupts (port);
339 void gs_start(struct tty_struct * tty)
341 struct gs_port *port;
345 port = tty->driver_data;
349 if (port->xmit_cnt &&
351 !(port->flags & GS_TX_INTEN) ) {
352 port->flags |= GS_TX_INTEN;
353 port->rd->enable_tx_interrupts (port);
359 static void gs_shutdown_port (struct gs_port *port)
367 if (!(port->flags & ASYNC_INITIALIZED))
370 spin_lock_irqsave(&port->driver_lock, flags);
372 if (port->xmit_buf) {
373 free_page((unsigned long) port->xmit_buf);
374 port->xmit_buf = NULL;
378 set_bit(TTY_IO_ERROR, &port->tty->flags);
380 port->rd->shutdown_port (port);
382 port->flags &= ~ASYNC_INITIALIZED;
383 spin_unlock_irqrestore(&port->driver_lock, flags);
389 void gs_hangup(struct tty_struct *tty)
391 struct gs_port *port;
397 port = tty->driver_data;
402 gs_shutdown_port (port);
403 port->flags &= ~(ASYNC_NORMAL_ACTIVE|GS_ACTIVE);
407 wake_up_interruptible(&port->open_wait);
412 int gs_block_til_ready(void *port_, struct file * filp)
414 struct gs_port *port = port_;
415 DECLARE_WAITQUEUE(wait, current);
419 struct tty_struct *tty;
430 gs_dprintk (GS_DEBUG_BTR, "Entering gs_block_till_ready.\n");
432 * If the device is in the middle of being closed, then block
433 * until it's done, and then try again.
435 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
436 interruptible_sleep_on(&port->close_wait);
437 if (port->flags & ASYNC_HUP_NOTIFY)
443 gs_dprintk (GS_DEBUG_BTR, "after hung up\n");
446 * If non-blocking mode is set, or the port is not enabled,
447 * then make the check up front and then exit.
449 if ((filp->f_flags & O_NONBLOCK) ||
450 (tty->flags & (1 << TTY_IO_ERROR))) {
451 port->flags |= ASYNC_NORMAL_ACTIVE;
455 gs_dprintk (GS_DEBUG_BTR, "after nonblock\n");
461 * Block waiting for the carrier detect and the line to become
462 * free (i.e., not in use by the callout). While we are in
463 * this loop, port->count is dropped by one, so that
464 * rs_close() knows when to free things. We restore it upon
465 * exit, either normal or abnormal.
469 add_wait_queue(&port->open_wait, &wait);
471 gs_dprintk (GS_DEBUG_BTR, "after add waitq.\n");
472 spin_lock_irqsave(&port->driver_lock, flags);
473 if (!tty_hung_up_p(filp)) {
476 spin_unlock_irqrestore(&port->driver_lock, flags);
477 port->blocked_open++;
479 CD = port->rd->get_CD (port);
480 gs_dprintk (GS_DEBUG_BTR, "CD is now %d.\n", CD);
481 set_current_state (TASK_INTERRUPTIBLE);
482 if (tty_hung_up_p(filp) ||
483 !(port->flags & ASYNC_INITIALIZED)) {
484 if (port->flags & ASYNC_HUP_NOTIFY)
487 retval = -ERESTARTSYS;
490 if (!(port->flags & ASYNC_CLOSING) &&
493 gs_dprintk (GS_DEBUG_BTR, "signal_pending is now: %d (%lx)\n",
494 (int)signal_pending (current), *(long*)(¤t->blocked));
495 if (signal_pending(current)) {
496 retval = -ERESTARTSYS;
501 gs_dprintk (GS_DEBUG_BTR, "Got out of the loop. (%d)\n",
503 set_current_state (TASK_RUNNING);
504 remove_wait_queue(&port->open_wait, &wait);
505 if (!tty_hung_up_p(filp)) {
508 port->blocked_open--;
512 port->flags |= ASYNC_NORMAL_ACTIVE;
518 void gs_close(struct tty_struct * tty, struct file * filp)
521 struct gs_port *port;
527 port = (struct gs_port *) tty->driver_data;
532 /* This seems to happen when this is called from vhangup. */
533 gs_dprintk (GS_DEBUG_CLOSE, "gs: Odd: port->tty is NULL\n");
537 spin_lock_irqsave(&port->driver_lock, flags);
539 if (tty_hung_up_p(filp)) {
540 spin_unlock_irqrestore(&port->driver_lock, flags);
541 if (port->rd->hungup)
542 port->rd->hungup (port);
547 if ((tty->count == 1) && (port->count != 1)) {
548 printk(KERN_ERR "gs: gs_close port %p: bad port count;"
549 " tty->count is 1, port count is %d\n", port, port->count);
552 if (--port->count < 0) {
553 printk(KERN_ERR "gs: gs_close port %p: bad port count: %d\n", port, port->count);
558 gs_dprintk(GS_DEBUG_CLOSE, "gs_close port %p: count: %d\n", port, port->count);
559 spin_unlock_irqrestore(&port->driver_lock, flags);
563 port->flags |= ASYNC_CLOSING;
566 * Now we wait for the transmit buffer to clear; and we notify
567 * the line discipline to only process XON/XOFF characters.
570 /* if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
571 tty_wait_until_sent(tty, port->closing_wait); */
574 * At this point we stop accepting input. To do this, we
575 * disable the receive line status interrupts, and tell the
576 * interrupt driver to stop checking the data ready bit in the
577 * line status register.
580 port->rd->disable_rx_interrupts (port);
581 spin_unlock_irqrestore(&port->driver_lock, flags);
583 /* close has no way of returning "EINTR", so discard return value */
584 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
585 gs_wait_tx_flushed (port, port->closing_wait);
587 port->flags &= ~GS_ACTIVE;
589 if (tty->driver->flush_buffer)
590 tty->driver->flush_buffer(tty);
592 tty_ldisc_flush(tty);
596 port->rd->close (port);
597 port->rd->shutdown_port (port);
600 if (port->blocked_open) {
601 if (port->close_delay) {
602 spin_unlock_irqrestore(&port->driver_lock, flags);
603 msleep_interruptible(jiffies_to_msecs(port->close_delay));
604 spin_lock_irqsave(&port->driver_lock, flags);
606 wake_up_interruptible(&port->open_wait);
608 port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING | ASYNC_INITIALIZED);
609 wake_up_interruptible(&port->close_wait);
615 void gs_set_termios (struct tty_struct * tty,
616 struct ktermios * old_termios)
618 struct gs_port *port;
619 int baudrate, tmp, rv;
620 struct ktermios *tiosp;
626 port = tty->driver_data;
630 /* This seems to happen when this is called after gs_close. */
631 gs_dprintk (GS_DEBUG_TERMIOS, "gs: Odd: port->tty is NULL\n");
636 tiosp = tty->termios;
638 if (gs_debug & GS_DEBUG_TERMIOS) {
639 gs_dprintk (GS_DEBUG_TERMIOS, "termios structure (%p):\n", tiosp);
642 if(old_termios && (gs_debug & GS_DEBUG_TERMIOS)) {
643 if(tiosp->c_iflag != old_termios->c_iflag) printk("c_iflag changed\n");
644 if(tiosp->c_oflag != old_termios->c_oflag) printk("c_oflag changed\n");
645 if(tiosp->c_cflag != old_termios->c_cflag) printk("c_cflag changed\n");
646 if(tiosp->c_lflag != old_termios->c_lflag) printk("c_lflag changed\n");
647 if(tiosp->c_line != old_termios->c_line) printk("c_line changed\n");
648 if(!memcmp(tiosp->c_cc, old_termios->c_cc, NCC)) printk("c_cc changed\n");
651 baudrate = tty_get_baud_rate(tty);
653 if ((tiosp->c_cflag & CBAUD) == B38400) {
654 if ( (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
656 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
658 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
660 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
662 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
663 baudrate = (port->baud_base / port->custom_divisor);
666 /* I recommend using THIS instead of the mess in termios (and
667 duplicating the above code). Next we should create a clean
668 interface towards this variable. If your card supports arbitrary
669 baud rates, (e.g. CD1400 or 16550 based cards) then everything
670 will be very easy..... */
671 port->baud = baudrate;
673 /* Two timer ticks seems enough to wakeup something like SLIP driver */
674 /* Baudrate/10 is cps. Divide by HZ to get chars per tick. */
675 tmp = (baudrate / 10 / HZ) * 2;
677 if (tmp < 0) tmp = 0;
678 if (tmp >= SERIAL_XMIT_SIZE) tmp = SERIAL_XMIT_SIZE-1;
680 port->wakeup_chars = tmp;
682 /* We should really wait for the characters to be all sent before
683 changing the settings. -- CAL */
684 rv = gs_wait_tx_flushed (port, MAX_SCHEDULE_TIMEOUT);
685 if (rv < 0) return /* rv */;
687 rv = port->rd->set_real_termios(port);
688 if (rv < 0) return /* rv */;
691 (old_termios->c_cflag & CRTSCTS)) &&
692 !( tiosp->c_cflag & CRTSCTS)) {
697 #ifdef tytso_patch_94Nov25_1726
698 /* This "makes sense", Why is it commented out? */
700 if (!(old_termios->c_cflag & CLOCAL) &&
701 (tty->termios->c_cflag & CLOCAL))
702 wake_up_interruptible(&port->gs.open_wait);
711 /* Must be called with interrupts enabled */
712 int gs_init_port(struct gs_port *port)
718 if (port->flags & ASYNC_INITIALIZED) {
722 if (!port->xmit_buf) {
723 /* We may sleep in get_zeroed_page() */
726 tmp = get_zeroed_page(GFP_KERNEL);
727 spin_lock_irqsave (&port->driver_lock, flags);
731 port->xmit_buf = (unsigned char *) tmp;
732 spin_unlock_irqrestore(&port->driver_lock, flags);
733 if (!port->xmit_buf) {
739 spin_lock_irqsave (&port->driver_lock, flags);
741 clear_bit(TTY_IO_ERROR, &port->tty->flags);
742 mutex_init(&port->port_write_mutex);
743 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
744 spin_unlock_irqrestore(&port->driver_lock, flags);
745 gs_set_termios(port->tty, NULL);
746 spin_lock_irqsave (&port->driver_lock, flags);
747 port->flags |= ASYNC_INITIALIZED;
748 port->flags &= ~GS_TX_INTEN;
750 spin_unlock_irqrestore(&port->driver_lock, flags);
756 int gs_setserial(struct gs_port *port, struct serial_struct __user *sp)
758 struct serial_struct sio;
760 if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
763 if (!capable(CAP_SYS_ADMIN)) {
764 if ((sio.baud_base != port->baud_base) ||
765 (sio.close_delay != port->close_delay) ||
766 ((sio.flags & ~ASYNC_USR_MASK) !=
767 (port->flags & ~ASYNC_USR_MASK)))
771 port->flags = (port->flags & ~ASYNC_USR_MASK) |
772 (sio.flags & ASYNC_USR_MASK);
774 port->baud_base = sio.baud_base;
775 port->close_delay = sio.close_delay;
776 port->closing_wait = sio.closing_wait;
777 port->custom_divisor = sio.custom_divisor;
779 gs_set_termios (port->tty, NULL);
785 /*****************************************************************************/
788 * Generate the serial struct info.
791 int gs_getserial(struct gs_port *port, struct serial_struct __user *sp)
793 struct serial_struct sio;
795 memset(&sio, 0, sizeof(struct serial_struct));
796 sio.flags = port->flags;
797 sio.baud_base = port->baud_base;
798 sio.close_delay = port->close_delay;
799 sio.closing_wait = port->closing_wait;
800 sio.custom_divisor = port->custom_divisor;
803 /* If you want you can override these. */
804 sio.type = PORT_UNKNOWN;
805 sio.xmit_fifo_size = -1;
810 if (port->rd->getserial)
811 port->rd->getserial (port, &sio);
813 if (copy_to_user(sp, &sio, sizeof(struct serial_struct)))
820 void gs_got_break(struct gs_port *port)
824 tty_insert_flip_char(port->tty, 0, TTY_BREAK);
825 tty_schedule_flip(port->tty);
826 if (port->flags & ASYNC_SAK) {
834 EXPORT_SYMBOL(gs_put_char);
835 EXPORT_SYMBOL(gs_write);
836 EXPORT_SYMBOL(gs_write_room);
837 EXPORT_SYMBOL(gs_chars_in_buffer);
838 EXPORT_SYMBOL(gs_flush_buffer);
839 EXPORT_SYMBOL(gs_flush_chars);
840 EXPORT_SYMBOL(gs_stop);
841 EXPORT_SYMBOL(gs_start);
842 EXPORT_SYMBOL(gs_hangup);
843 EXPORT_SYMBOL(gs_block_til_ready);
844 EXPORT_SYMBOL(gs_close);
845 EXPORT_SYMBOL(gs_set_termios);
846 EXPORT_SYMBOL(gs_init_port);
847 EXPORT_SYMBOL(gs_setserial);
848 EXPORT_SYMBOL(gs_getserial);
849 EXPORT_SYMBOL(gs_got_break);
851 MODULE_LICENSE("GPL");