Linux 2.6.20.7
[linux/fpc-iii.git] / drivers / char / generic_serial.c
blobe2a0b6f3d336317ccc842e1ea88a4f3743f08c95
1 /*
2 * generic_serial.c
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
20 * */
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/tty.h>
25 #include <linux/serial.h>
26 #include <linux/mm.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>
34 #define DEBUG
36 static int gs_debug;
38 #ifdef DEBUG
39 #define gs_dprintk(f, str...) if (gs_debug & f) printk (str)
40 #else
41 #define gs_dprintk(f, str...) /* nothing */
42 #endif
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__)
46 #define NEW_WRITE_LOCKING 1
47 #if NEW_WRITE_LOCKING
48 #define DECL /* Nothing */
49 #define LOCKIT mutex_lock(& port->port_write_mutex);
50 #define RELEASEIT mutex_unlock(&port->port_write_mutex);
51 #else
52 #define DECL unsigned long flags;
53 #define LOCKIT save_flags (flags);cli ()
54 #define RELEASEIT restore_flags (flags)
55 #endif
57 #define RS_EVENT_WRITE_WAKEUP 1
59 module_param(gs_debug, int, 0644);
62 void gs_put_char(struct tty_struct * tty, unsigned char ch)
64 struct gs_port *port;
65 DECL
67 func_enter ();
69 if (!tty) return;
71 port = tty->driver_data;
73 if (!port) return;
75 if (! (port->flags & ASYNC_INITIALIZED)) return;
77 /* Take a lock on the serial tranmit buffer! */
78 LOCKIT;
80 if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
81 /* Sorry, buffer is full, drop character. Update statistics???? -- REW */
82 RELEASEIT;
83 return;
86 port->xmit_buf[port->xmit_head++] = ch;
87 port->xmit_head &= SERIAL_XMIT_SIZE - 1;
88 port->xmit_cnt++; /* Characters in buffer */
90 RELEASEIT;
91 func_exit ();
95 #ifdef NEW_WRITE_LOCKING
98 > Problems to take into account are:
99 > -1- Interrupts that empty part of the buffer.
100 > -2- page faults on the access to userspace.
101 > -3- Other processes that are also trying to do a "write".
104 int gs_write(struct tty_struct * tty,
105 const unsigned char *buf, int count)
107 struct gs_port *port;
108 int c, total = 0;
109 int t;
111 func_enter ();
113 if (!tty) return 0;
115 port = tty->driver_data;
117 if (!port) return 0;
119 if (! (port->flags & ASYNC_INITIALIZED))
120 return 0;
122 /* get exclusive "write" access to this port (problem 3) */
123 /* This is not a spinlock because we can have a disk access (page
124 fault) in copy_from_user */
125 mutex_lock(& port->port_write_mutex);
127 while (1) {
129 c = count;
131 /* This is safe because we "OWN" the "head". Noone else can
132 change the "head": we own the port_write_mutex. */
133 /* Don't overrun the end of the buffer */
134 t = SERIAL_XMIT_SIZE - port->xmit_head;
135 if (t < c) c = t;
137 /* This is safe because the xmit_cnt can only decrease. This
138 would increase "t", so we might copy too little chars. */
139 /* Don't copy past the "head" of the buffer */
140 t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
141 if (t < c) c = t;
143 /* Can't copy more? break out! */
144 if (c <= 0) break;
146 memcpy (port->xmit_buf + port->xmit_head, buf, c);
148 port -> xmit_cnt += c;
149 port -> xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE -1);
150 buf += c;
151 count -= c;
152 total += c;
154 mutex_unlock(& port->port_write_mutex);
156 gs_dprintk (GS_DEBUG_WRITE, "write: interrupts are %s\n",
157 (port->flags & GS_TX_INTEN)?"enabled": "disabled");
159 if (port->xmit_cnt &&
160 !tty->stopped &&
161 !tty->hw_stopped &&
162 !(port->flags & GS_TX_INTEN)) {
163 port->flags |= GS_TX_INTEN;
164 port->rd->enable_tx_interrupts (port);
166 func_exit ();
167 return total;
169 #else
171 > Problems to take into account are:
172 > -1- Interrupts that empty part of the buffer.
173 > -2- page faults on the access to userspace.
174 > -3- Other processes that are also trying to do a "write".
177 int gs_write(struct tty_struct * tty,
178 const unsigned char *buf, int count)
180 struct gs_port *port;
181 int c, total = 0;
182 int t;
183 unsigned long flags;
185 func_enter ();
187 /* The standard serial driver returns 0 in this case.
188 That sounds to me as "No error, I just didn't get to writing any
189 bytes. Feel free to try again."
190 The "official" way to write n bytes from buf is:
192 for (nwritten = 0;nwritten < n;nwritten += rv) {
193 rv = write (fd, buf+nwritten, n-nwritten);
194 if (rv < 0) break; // Error: bail out. //
197 which will loop endlessly in this case. The manual page for write
198 agrees with me. In practise almost everybody writes
199 "write (fd, buf,n);" but some people might have had to deal with
200 incomplete writes in the past and correctly implemented it by now...
203 if (!tty) return -EIO;
205 port = tty->driver_data;
206 if (!port || !port->xmit_buf)
207 return -EIO;
209 local_save_flags(flags);
210 while (1) {
211 cli();
212 c = count;
214 /* This is safe because we "OWN" the "head". Noone else can
215 change the "head": we own the port_write_mutex. */
216 /* Don't overrun the end of the buffer */
217 t = SERIAL_XMIT_SIZE - port->xmit_head;
218 if (t < c) c = t;
220 /* This is safe because the xmit_cnt can only decrease. This
221 would increase "t", so we might copy too little chars. */
222 /* Don't copy past the "head" of the buffer */
223 t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
224 if (t < c) c = t;
226 /* Can't copy more? break out! */
227 if (c <= 0) {
228 local_restore_flags(flags);
229 break;
231 memcpy(port->xmit_buf + port->xmit_head, buf, c);
232 port->xmit_head = ((port->xmit_head + c) &
233 (SERIAL_XMIT_SIZE-1));
234 port->xmit_cnt += c;
235 local_restore_flags(flags);
236 buf += c;
237 count -= c;
238 total += c;
241 if (port->xmit_cnt &&
242 !tty->stopped &&
243 !tty->hw_stopped &&
244 !(port->flags & GS_TX_INTEN)) {
245 port->flags |= GS_TX_INTEN;
246 port->rd->enable_tx_interrupts (port);
248 func_exit ();
249 return total;
252 #endif
256 int gs_write_room(struct tty_struct * tty)
258 struct gs_port *port = tty->driver_data;
259 int ret;
261 func_enter ();
262 ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
263 if (ret < 0)
264 ret = 0;
265 func_exit ();
266 return ret;
270 int gs_chars_in_buffer(struct tty_struct *tty)
272 struct gs_port *port = tty->driver_data;
273 func_enter ();
275 func_exit ();
276 return port->xmit_cnt;
280 static int gs_real_chars_in_buffer(struct tty_struct *tty)
282 struct gs_port *port;
283 func_enter ();
285 if (!tty) return 0;
286 port = tty->driver_data;
288 if (!port->rd) return 0;
289 if (!port->rd->chars_in_buffer) return 0;
291 func_exit ();
292 return port->xmit_cnt + port->rd->chars_in_buffer (port);
296 static int gs_wait_tx_flushed (void * ptr, unsigned long timeout)
298 struct gs_port *port = ptr;
299 unsigned long end_jiffies;
300 int jiffies_to_transmit, charsleft = 0, rv = 0;
301 int rcib;
303 func_enter();
305 gs_dprintk (GS_DEBUG_FLUSH, "port=%p.\n", port);
306 if (port) {
307 gs_dprintk (GS_DEBUG_FLUSH, "xmit_cnt=%x, xmit_buf=%p, tty=%p.\n",
308 port->xmit_cnt, port->xmit_buf, port->tty);
311 if (!port || port->xmit_cnt < 0 || !port->xmit_buf) {
312 gs_dprintk (GS_DEBUG_FLUSH, "ERROR: !port, !port->xmit_buf or prot->xmit_cnt < 0.\n");
313 func_exit();
314 return -EINVAL; /* This is an error which we don't know how to handle. */
317 rcib = gs_real_chars_in_buffer(port->tty);
319 if(rcib <= 0) {
320 gs_dprintk (GS_DEBUG_FLUSH, "nothing to wait for.\n");
321 func_exit();
322 return rv;
324 /* stop trying: now + twice the time it would normally take + seconds */
325 if (timeout == 0) timeout = MAX_SCHEDULE_TIMEOUT;
326 end_jiffies = jiffies;
327 if (timeout != MAX_SCHEDULE_TIMEOUT)
328 end_jiffies += port->baud?(2 * rcib * 10 * HZ / port->baud):0;
329 end_jiffies += timeout;
331 gs_dprintk (GS_DEBUG_FLUSH, "now=%lx, end=%lx (%ld).\n",
332 jiffies, end_jiffies, end_jiffies-jiffies);
334 /* the expression is actually jiffies < end_jiffies, but that won't
335 work around the wraparound. Tricky eh? */
336 while ((charsleft = gs_real_chars_in_buffer (port->tty)) &&
337 time_after (end_jiffies, jiffies)) {
338 /* Units check:
339 chars * (bits/char) * (jiffies /sec) / (bits/sec) = jiffies!
340 check! */
342 charsleft += 16; /* Allow 16 chars more to be transmitted ... */
343 jiffies_to_transmit = port->baud?(1 + charsleft * 10 * HZ / port->baud):0;
344 /* ^^^ Round up.... */
345 if (jiffies_to_transmit <= 0) jiffies_to_transmit = 1;
347 gs_dprintk (GS_DEBUG_FLUSH, "Expect to finish in %d jiffies "
348 "(%d chars).\n", jiffies_to_transmit, charsleft);
350 msleep_interruptible(jiffies_to_msecs(jiffies_to_transmit));
351 if (signal_pending (current)) {
352 gs_dprintk (GS_DEBUG_FLUSH, "Signal pending. Bombing out: ");
353 rv = -EINTR;
354 break;
358 gs_dprintk (GS_DEBUG_FLUSH, "charsleft = %d.\n", charsleft);
359 set_current_state (TASK_RUNNING);
361 func_exit();
362 return rv;
367 void gs_flush_buffer(struct tty_struct *tty)
369 struct gs_port *port;
370 unsigned long flags;
372 func_enter ();
374 if (!tty) return;
376 port = tty->driver_data;
378 if (!port) return;
380 /* XXX Would the write semaphore do? */
381 spin_lock_irqsave (&port->driver_lock, flags);
382 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
383 spin_unlock_irqrestore (&port->driver_lock, flags);
385 wake_up_interruptible(&tty->write_wait);
386 tty_wakeup(tty);
387 func_exit ();
391 void gs_flush_chars(struct tty_struct * tty)
393 struct gs_port *port;
395 func_enter ();
397 if (!tty) return;
399 port = tty->driver_data;
401 if (!port) return;
403 if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
404 !port->xmit_buf) {
405 func_exit ();
406 return;
409 /* Beats me -- REW */
410 port->flags |= GS_TX_INTEN;
411 port->rd->enable_tx_interrupts (port);
412 func_exit ();
416 void gs_stop(struct tty_struct * tty)
418 struct gs_port *port;
420 func_enter ();
422 if (!tty) return;
424 port = tty->driver_data;
426 if (!port) return;
428 if (port->xmit_cnt &&
429 port->xmit_buf &&
430 (port->flags & GS_TX_INTEN) ) {
431 port->flags &= ~GS_TX_INTEN;
432 port->rd->disable_tx_interrupts (port);
434 func_exit ();
438 void gs_start(struct tty_struct * tty)
440 struct gs_port *port;
442 if (!tty) return;
444 port = tty->driver_data;
446 if (!port) return;
448 if (port->xmit_cnt &&
449 port->xmit_buf &&
450 !(port->flags & GS_TX_INTEN) ) {
451 port->flags |= GS_TX_INTEN;
452 port->rd->enable_tx_interrupts (port);
454 func_exit ();
458 static void gs_shutdown_port (struct gs_port *port)
460 unsigned long flags;
462 func_enter();
464 if (!port) return;
466 if (!(port->flags & ASYNC_INITIALIZED))
467 return;
469 spin_lock_irqsave(&port->driver_lock, flags);
471 if (port->xmit_buf) {
472 free_page((unsigned long) port->xmit_buf);
473 port->xmit_buf = NULL;
476 if (port->tty)
477 set_bit(TTY_IO_ERROR, &port->tty->flags);
479 port->rd->shutdown_port (port);
481 port->flags &= ~ASYNC_INITIALIZED;
482 spin_unlock_irqrestore(&port->driver_lock, flags);
484 func_exit();
488 void gs_hangup(struct tty_struct *tty)
490 struct gs_port *port;
492 func_enter ();
494 if (!tty) return;
496 port = tty->driver_data;
497 tty = port->tty;
498 if (!tty)
499 return;
501 gs_shutdown_port (port);
502 port->flags &= ~(ASYNC_NORMAL_ACTIVE|GS_ACTIVE);
503 port->tty = NULL;
504 port->count = 0;
506 wake_up_interruptible(&port->open_wait);
507 func_exit ();
511 int gs_block_til_ready(void *port_, struct file * filp)
513 struct gs_port *port = port_;
514 DECLARE_WAITQUEUE(wait, current);
515 int retval;
516 int do_clocal = 0;
517 int CD;
518 struct tty_struct *tty;
519 unsigned long flags;
521 func_enter ();
523 if (!port) return 0;
525 tty = port->tty;
527 if (!tty) return 0;
529 gs_dprintk (GS_DEBUG_BTR, "Entering gs_block_till_ready.\n");
531 * If the device is in the middle of being closed, then block
532 * until it's done, and then try again.
534 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
535 interruptible_sleep_on(&port->close_wait);
536 if (port->flags & ASYNC_HUP_NOTIFY)
537 return -EAGAIN;
538 else
539 return -ERESTARTSYS;
542 gs_dprintk (GS_DEBUG_BTR, "after hung up\n");
545 * If non-blocking mode is set, or the port is not enabled,
546 * then make the check up front and then exit.
548 if ((filp->f_flags & O_NONBLOCK) ||
549 (tty->flags & (1 << TTY_IO_ERROR))) {
550 port->flags |= ASYNC_NORMAL_ACTIVE;
551 return 0;
554 gs_dprintk (GS_DEBUG_BTR, "after nonblock\n");
556 if (C_CLOCAL(tty))
557 do_clocal = 1;
560 * Block waiting for the carrier detect and the line to become
561 * free (i.e., not in use by the callout). While we are in
562 * this loop, port->count is dropped by one, so that
563 * rs_close() knows when to free things. We restore it upon
564 * exit, either normal or abnormal.
566 retval = 0;
568 add_wait_queue(&port->open_wait, &wait);
570 gs_dprintk (GS_DEBUG_BTR, "after add waitq.\n");
571 spin_lock_irqsave(&port->driver_lock, flags);
572 if (!tty_hung_up_p(filp)) {
573 port->count--;
575 spin_unlock_irqrestore(&port->driver_lock, flags);
576 port->blocked_open++;
577 while (1) {
578 CD = port->rd->get_CD (port);
579 gs_dprintk (GS_DEBUG_BTR, "CD is now %d.\n", CD);
580 set_current_state (TASK_INTERRUPTIBLE);
581 if (tty_hung_up_p(filp) ||
582 !(port->flags & ASYNC_INITIALIZED)) {
583 if (port->flags & ASYNC_HUP_NOTIFY)
584 retval = -EAGAIN;
585 else
586 retval = -ERESTARTSYS;
587 break;
589 if (!(port->flags & ASYNC_CLOSING) &&
590 (do_clocal || CD))
591 break;
592 gs_dprintk (GS_DEBUG_BTR, "signal_pending is now: %d (%lx)\n",
593 (int)signal_pending (current), *(long*)(&current->blocked));
594 if (signal_pending(current)) {
595 retval = -ERESTARTSYS;
596 break;
598 schedule();
600 gs_dprintk (GS_DEBUG_BTR, "Got out of the loop. (%d)\n",
601 port->blocked_open);
602 set_current_state (TASK_RUNNING);
603 remove_wait_queue(&port->open_wait, &wait);
604 if (!tty_hung_up_p(filp)) {
605 port->count++;
607 port->blocked_open--;
608 if (retval)
609 return retval;
611 port->flags |= ASYNC_NORMAL_ACTIVE;
612 func_exit ();
613 return 0;
617 void gs_close(struct tty_struct * tty, struct file * filp)
619 unsigned long flags;
620 struct gs_port *port;
622 func_enter ();
624 if (!tty) return;
626 port = (struct gs_port *) tty->driver_data;
628 if (!port) return;
630 if (!port->tty) {
631 /* This seems to happen when this is called from vhangup. */
632 gs_dprintk (GS_DEBUG_CLOSE, "gs: Odd: port->tty is NULL\n");
633 port->tty = tty;
636 spin_lock_irqsave(&port->driver_lock, flags);
638 if (tty_hung_up_p(filp)) {
639 spin_unlock_irqrestore(&port->driver_lock, flags);
640 if (port->rd->hungup)
641 port->rd->hungup (port);
642 func_exit ();
643 return;
646 if ((tty->count == 1) && (port->count != 1)) {
647 printk(KERN_ERR "gs: gs_close port %p: bad port count;"
648 " tty->count is 1, port count is %d\n", port, port->count);
649 port->count = 1;
651 if (--port->count < 0) {
652 printk(KERN_ERR "gs: gs_close port %p: bad port count: %d\n", port, port->count);
653 port->count = 0;
656 if (port->count) {
657 gs_dprintk(GS_DEBUG_CLOSE, "gs_close port %p: count: %d\n", port, port->count);
658 spin_unlock_irqrestore(&port->driver_lock, flags);
659 func_exit ();
660 return;
662 port->flags |= ASYNC_CLOSING;
665 * Now we wait for the transmit buffer to clear; and we notify
666 * the line discipline to only process XON/XOFF characters.
668 tty->closing = 1;
669 /* if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
670 tty_wait_until_sent(tty, port->closing_wait); */
673 * At this point we stop accepting input. To do this, we
674 * disable the receive line status interrupts, and tell the
675 * interrupt driver to stop checking the data ready bit in the
676 * line status register.
679 port->rd->disable_rx_interrupts (port);
680 spin_unlock_irqrestore(&port->driver_lock, flags);
682 /* close has no way of returning "EINTR", so discard return value */
683 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
684 gs_wait_tx_flushed (port, port->closing_wait);
686 port->flags &= ~GS_ACTIVE;
688 if (tty->driver->flush_buffer)
689 tty->driver->flush_buffer(tty);
691 tty_ldisc_flush(tty);
692 tty->closing = 0;
694 port->event = 0;
695 port->rd->close (port);
696 port->rd->shutdown_port (port);
697 port->tty = NULL;
699 if (port->blocked_open) {
700 if (port->close_delay) {
701 spin_unlock_irqrestore(&port->driver_lock, flags);
702 msleep_interruptible(jiffies_to_msecs(port->close_delay));
703 spin_lock_irqsave(&port->driver_lock, flags);
705 wake_up_interruptible(&port->open_wait);
707 port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING | ASYNC_INITIALIZED);
708 wake_up_interruptible(&port->close_wait);
710 func_exit ();
714 void gs_set_termios (struct tty_struct * tty,
715 struct ktermios * old_termios)
717 struct gs_port *port;
718 int baudrate, tmp, rv;
719 struct ktermios *tiosp;
721 func_enter();
723 if (!tty) return;
725 port = tty->driver_data;
727 if (!port) return;
728 if (!port->tty) {
729 /* This seems to happen when this is called after gs_close. */
730 gs_dprintk (GS_DEBUG_TERMIOS, "gs: Odd: port->tty is NULL\n");
731 port->tty = tty;
735 tiosp = tty->termios;
737 if (gs_debug & GS_DEBUG_TERMIOS) {
738 gs_dprintk (GS_DEBUG_TERMIOS, "termios structure (%p):\n", tiosp);
741 /* This is an optimization that is only allowed for dumb cards */
742 /* Smart cards require knowledge of iflags and oflags too: that
743 might change hardware cooking mode.... */
744 if (old_termios) {
745 if( (tiosp->c_iflag == old_termios->c_iflag)
746 && (tiosp->c_oflag == old_termios->c_oflag)
747 && (tiosp->c_cflag == old_termios->c_cflag)
748 && (tiosp->c_lflag == old_termios->c_lflag)
749 && (tiosp->c_line == old_termios->c_line)
750 && (memcmp(tiosp->c_cc, old_termios->c_cc, NCC) == 0)) {
751 gs_dprintk(GS_DEBUG_TERMIOS, "gs_set_termios: optimized away\n");
752 return /* 0 */;
754 } else
755 gs_dprintk(GS_DEBUG_TERMIOS, "gs_set_termios: no old_termios: "
756 "no optimization\n");
758 if(old_termios && (gs_debug & GS_DEBUG_TERMIOS)) {
759 if(tiosp->c_iflag != old_termios->c_iflag) printk("c_iflag changed\n");
760 if(tiosp->c_oflag != old_termios->c_oflag) printk("c_oflag changed\n");
761 if(tiosp->c_cflag != old_termios->c_cflag) printk("c_cflag changed\n");
762 if(tiosp->c_lflag != old_termios->c_lflag) printk("c_lflag changed\n");
763 if(tiosp->c_line != old_termios->c_line) printk("c_line changed\n");
764 if(!memcmp(tiosp->c_cc, old_termios->c_cc, NCC)) printk("c_cc changed\n");
767 baudrate = tty_get_baud_rate(tty);
769 if ((tiosp->c_cflag & CBAUD) == B38400) {
770 if ( (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
771 baudrate = 57600;
772 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
773 baudrate = 115200;
774 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
775 baudrate = 230400;
776 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
777 baudrate = 460800;
778 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
779 baudrate = (port->baud_base / port->custom_divisor);
782 /* I recommend using THIS instead of the mess in termios (and
783 duplicating the above code). Next we should create a clean
784 interface towards this variable. If your card supports arbitrary
785 baud rates, (e.g. CD1400 or 16550 based cards) then everything
786 will be very easy..... */
787 port->baud = baudrate;
789 /* Two timer ticks seems enough to wakeup something like SLIP driver */
790 /* Baudrate/10 is cps. Divide by HZ to get chars per tick. */
791 tmp = (baudrate / 10 / HZ) * 2;
793 if (tmp < 0) tmp = 0;
794 if (tmp >= SERIAL_XMIT_SIZE) tmp = SERIAL_XMIT_SIZE-1;
796 port->wakeup_chars = tmp;
798 /* We should really wait for the characters to be all sent before
799 changing the settings. -- CAL */
800 rv = gs_wait_tx_flushed (port, MAX_SCHEDULE_TIMEOUT);
801 if (rv < 0) return /* rv */;
803 rv = port->rd->set_real_termios(port);
804 if (rv < 0) return /* rv */;
806 if ((!old_termios ||
807 (old_termios->c_cflag & CRTSCTS)) &&
808 !( tiosp->c_cflag & CRTSCTS)) {
809 tty->stopped = 0;
810 gs_start(tty);
813 #ifdef tytso_patch_94Nov25_1726
814 /* This "makes sense", Why is it commented out? */
816 if (!(old_termios->c_cflag & CLOCAL) &&
817 (tty->termios->c_cflag & CLOCAL))
818 wake_up_interruptible(&port->gs.open_wait);
819 #endif
821 func_exit();
822 return /* 0 */;
827 /* Must be called with interrupts enabled */
828 int gs_init_port(struct gs_port *port)
830 unsigned long flags;
832 func_enter ();
834 if (port->flags & ASYNC_INITIALIZED) {
835 func_exit ();
836 return 0;
838 if (!port->xmit_buf) {
839 /* We may sleep in get_zeroed_page() */
840 unsigned long tmp;
842 tmp = get_zeroed_page(GFP_KERNEL);
843 spin_lock_irqsave (&port->driver_lock, flags);
844 if (port->xmit_buf)
845 free_page (tmp);
846 else
847 port->xmit_buf = (unsigned char *) tmp;
848 spin_unlock_irqrestore(&port->driver_lock, flags);
849 if (!port->xmit_buf) {
850 func_exit ();
851 return -ENOMEM;
855 spin_lock_irqsave (&port->driver_lock, flags);
856 if (port->tty)
857 clear_bit(TTY_IO_ERROR, &port->tty->flags);
858 mutex_init(&port->port_write_mutex);
859 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
860 spin_unlock_irqrestore(&port->driver_lock, flags);
861 gs_set_termios(port->tty, NULL);
862 spin_lock_irqsave (&port->driver_lock, flags);
863 port->flags |= ASYNC_INITIALIZED;
864 port->flags &= ~GS_TX_INTEN;
866 spin_unlock_irqrestore(&port->driver_lock, flags);
867 func_exit ();
868 return 0;
872 int gs_setserial(struct gs_port *port, struct serial_struct __user *sp)
874 struct serial_struct sio;
876 if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
877 return(-EFAULT);
879 if (!capable(CAP_SYS_ADMIN)) {
880 if ((sio.baud_base != port->baud_base) ||
881 (sio.close_delay != port->close_delay) ||
882 ((sio.flags & ~ASYNC_USR_MASK) !=
883 (port->flags & ~ASYNC_USR_MASK)))
884 return(-EPERM);
887 port->flags = (port->flags & ~ASYNC_USR_MASK) |
888 (sio.flags & ASYNC_USR_MASK);
890 port->baud_base = sio.baud_base;
891 port->close_delay = sio.close_delay;
892 port->closing_wait = sio.closing_wait;
893 port->custom_divisor = sio.custom_divisor;
895 gs_set_termios (port->tty, NULL);
897 return 0;
901 /*****************************************************************************/
904 * Generate the serial struct info.
907 int gs_getserial(struct gs_port *port, struct serial_struct __user *sp)
909 struct serial_struct sio;
911 memset(&sio, 0, sizeof(struct serial_struct));
912 sio.flags = port->flags;
913 sio.baud_base = port->baud_base;
914 sio.close_delay = port->close_delay;
915 sio.closing_wait = port->closing_wait;
916 sio.custom_divisor = port->custom_divisor;
917 sio.hub6 = 0;
919 /* If you want you can override these. */
920 sio.type = PORT_UNKNOWN;
921 sio.xmit_fifo_size = -1;
922 sio.line = -1;
923 sio.port = -1;
924 sio.irq = -1;
926 if (port->rd->getserial)
927 port->rd->getserial (port, &sio);
929 if (copy_to_user(sp, &sio, sizeof(struct serial_struct)))
930 return -EFAULT;
931 return 0;
936 void gs_got_break(struct gs_port *port)
938 func_enter ();
940 tty_insert_flip_char(port->tty, 0, TTY_BREAK);
941 tty_schedule_flip(port->tty);
942 if (port->flags & ASYNC_SAK) {
943 do_SAK (port->tty);
946 func_exit ();
950 EXPORT_SYMBOL(gs_put_char);
951 EXPORT_SYMBOL(gs_write);
952 EXPORT_SYMBOL(gs_write_room);
953 EXPORT_SYMBOL(gs_chars_in_buffer);
954 EXPORT_SYMBOL(gs_flush_buffer);
955 EXPORT_SYMBOL(gs_flush_chars);
956 EXPORT_SYMBOL(gs_stop);
957 EXPORT_SYMBOL(gs_start);
958 EXPORT_SYMBOL(gs_hangup);
959 EXPORT_SYMBOL(gs_block_til_ready);
960 EXPORT_SYMBOL(gs_close);
961 EXPORT_SYMBOL(gs_set_termios);
962 EXPORT_SYMBOL(gs_init_port);
963 EXPORT_SYMBOL(gs_setserial);
964 EXPORT_SYMBOL(gs_getserial);
965 EXPORT_SYMBOL(gs_got_break);
967 MODULE_LICENSE("GPL");