[PATCH] Fix up 'linux-dvb' maintainers entry
[linux-2.6/history.git] / drivers / char / generic_serial.c
blob1f278a42e3cbddbcc7d69003ea9b5f2344465150
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 <asm/semaphore.h>
30 #include <asm/uaccess.h>
32 #define DEBUG
34 static char * tmp_buf;
35 static DECLARE_MUTEX(tmp_buf_sem);
37 static int gs_debug;
39 #ifdef DEBUG
40 #define gs_dprintk(f, str...) if (gs_debug & f) printk (str)
41 #else
42 #define gs_dprintk(f, str...) /* nothing */
43 #endif
45 #define func_enter() gs_dprintk (GS_DEBUG_FLOW, "gs: enter %s\n", __FUNCTION__)
46 #define func_exit() gs_dprintk (GS_DEBUG_FLOW, "gs: exit %s\n", __FUNCTION__)
48 #if NEW_WRITE_LOCKING
49 #define DECL /* Nothing */
50 #define LOCKIT down (& port->port_write_sem);
51 #define RELEASEIT up (&port->port_write_sem);
52 #else
53 #define DECL unsigned long flags;
54 #define LOCKIT save_flags (flags);cli ()
55 #define RELEASEIT restore_flags (flags)
56 #endif
58 #define RS_EVENT_WRITE_WAKEUP 1
60 MODULE_PARM(gs_debug, "i");
63 void gs_put_char(struct tty_struct * tty, unsigned char ch)
65 struct gs_port *port;
66 DECL
68 func_enter ();
70 if (!tty) return;
72 port = tty->driver_data;
74 if (!port) return;
76 if (! (port->flags & ASYNC_INITIALIZED)) return;
78 /* Take a lock on the serial tranmit buffer! */
79 LOCKIT;
81 if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
82 /* Sorry, buffer is full, drop character. Update statistics???? -- REW */
83 RELEASEIT;
84 return;
87 port->xmit_buf[port->xmit_head++] = ch;
88 port->xmit_head &= SERIAL_XMIT_SIZE - 1;
89 port->xmit_cnt++; /* Characters in buffer */
91 RELEASEIT;
92 func_exit ();
96 #ifdef NEW_WRITE_LOCKING
99 > Problems to take into account are:
100 > -1- Interrupts that empty part of the buffer.
101 > -2- page faults on the access to userspace.
102 > -3- Other processes that are also trying to do a "write".
105 int gs_write(struct tty_struct * tty, int from_user,
106 const unsigned char *buf, int count)
108 struct gs_port *port;
109 int c, total = 0;
110 int t;
112 func_enter ();
114 if (!tty) return 0;
116 port = tty->driver_data;
118 if (!port) return 0;
120 if (! (port->flags & ASYNC_INITIALIZED))
121 return 0;
123 /* get exclusive "write" access to this port (problem 3) */
124 /* This is not a spinlock because we can have a disk access (page
125 fault) in copy_from_user */
126 down (& port->port_write_sem);
128 while (1) {
130 c = count;
132 /* This is safe because we "OWN" the "head". Noone else can
133 change the "head": we own the port_write_sem. */
134 /* Don't overrun the end of the buffer */
135 t = SERIAL_XMIT_SIZE - port->xmit_head;
136 if (t < c) c = t;
138 /* This is safe because the xmit_cnt can only decrease. This
139 would increase "t", so we might copy too little chars. */
140 /* Don't copy past the "head" of the buffer */
141 t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
142 if (t < c) c = t;
144 /* Can't copy more? break out! */
145 if (c <= 0) break;
146 if (from_user) {
147 if (copy_from_user (port->xmit_buf + port->xmit_head,
148 buf, c)) {
149 up (& port->port_write_sem);
150 return -EFAULT;
153 } else
154 memcpy (port->xmit_buf + port->xmit_head, buf, c);
156 port -> xmit_cnt += c;
157 port -> xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE -1);
158 buf += c;
159 count -= c;
160 total += c;
162 up (& port->port_write_sem);
164 gs_dprintk (GS_DEBUG_WRITE, "write: interrupts are %s\n",
165 (port->flags & GS_TX_INTEN)?"enabled": "disabled");
167 if (port->xmit_cnt &&
168 !tty->stopped &&
169 !tty->hw_stopped &&
170 !(port->flags & GS_TX_INTEN)) {
171 port->flags |= GS_TX_INTEN;
172 port->rd->enable_tx_interrupts (port);
174 func_exit ();
175 return total;
177 #else
179 > Problems to take into account are:
180 > -1- Interrupts that empty part of the buffer.
181 > -2- page faults on the access to userspace.
182 > -3- Other processes that are also trying to do a "write".
185 int gs_write(struct tty_struct * tty, int from_user,
186 const unsigned char *buf, int count)
188 struct gs_port *port;
189 int c, total = 0;
190 int t;
191 unsigned long flags;
193 func_enter ();
195 /* The standard serial driver returns 0 in this case.
196 That sounds to me as "No error, I just didn't get to writing any
197 bytes. Feel free to try again."
198 The "official" way to write n bytes from buf is:
200 for (nwritten = 0;nwritten < n;nwritten += rv) {
201 rv = write (fd, buf+nwritten, n-nwritten);
202 if (rv < 0) break; // Error: bail out. //
205 which will loop endlessly in this case. The manual page for write
206 agrees with me. In practise almost everybody writes
207 "write (fd, buf,n);" but some people might have had to deal with
208 incomplete writes in the past and correctly implemented it by now...
211 if (!tty) return -EIO;
213 port = tty->driver_data;
214 if (!port || !port->xmit_buf || !tmp_buf)
215 return -EIO;
217 save_flags(flags);
218 if (from_user) {
219 down(&tmp_buf_sem);
220 while (1) {
221 c = count;
223 /* This is safe because we "OWN" the "head". Noone else can
224 change the "head": we own the port_write_sem. */
225 /* Don't overrun the end of the buffer */
226 t = SERIAL_XMIT_SIZE - port->xmit_head;
227 if (t < c) c = t;
229 /* This is safe because the xmit_cnt can only decrease. This
230 would increase "t", so we might copy too little chars. */
231 /* Don't copy past the "head" of the buffer */
232 t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
233 if (t < c) c = t;
235 /* Can't copy more? break out! */
236 if (c <= 0) break;
238 c -= copy_from_user(tmp_buf, buf, c);
239 if (!c) {
240 if (!total)
241 total = -EFAULT;
242 break;
244 cli();
245 t = SERIAL_XMIT_SIZE - port->xmit_head;
246 if (t < c) c = t;
247 t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
248 if (t < c) c = t;
250 memcpy(port->xmit_buf + port->xmit_head, tmp_buf, c);
251 port->xmit_head = ((port->xmit_head + c) &
252 (SERIAL_XMIT_SIZE-1));
253 port->xmit_cnt += c;
254 restore_flags(flags);
255 buf += c;
256 count -= c;
257 total += c;
259 up(&tmp_buf_sem);
260 } else {
261 while (1) {
262 cli();
263 c = count;
265 /* This is safe because we "OWN" the "head". Noone else can
266 change the "head": we own the port_write_sem. */
267 /* Don't overrun the end of the buffer */
268 t = SERIAL_XMIT_SIZE - port->xmit_head;
269 if (t < c) c = t;
271 /* This is safe because the xmit_cnt can only decrease. This
272 would increase "t", so we might copy too little chars. */
273 /* Don't copy past the "head" of the buffer */
274 t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
275 if (t < c) c = t;
277 /* Can't copy more? break out! */
278 if (c <= 0) {
279 restore_flags(flags);
280 break;
282 memcpy(port->xmit_buf + port->xmit_head, buf, c);
283 port->xmit_head = ((port->xmit_head + c) &
284 (SERIAL_XMIT_SIZE-1));
285 port->xmit_cnt += c;
286 restore_flags(flags);
287 buf += c;
288 count -= c;
289 total += c;
293 if (port->xmit_cnt &&
294 !tty->stopped &&
295 !tty->hw_stopped &&
296 !(port->flags & GS_TX_INTEN)) {
297 port->flags |= GS_TX_INTEN;
298 port->rd->enable_tx_interrupts (port);
300 func_exit ();
301 return total;
304 #endif
308 int gs_write_room(struct tty_struct * tty)
310 struct gs_port *port = tty->driver_data;
311 int ret;
313 func_enter ();
314 ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
315 if (ret < 0)
316 ret = 0;
317 func_exit ();
318 return ret;
322 int gs_chars_in_buffer(struct tty_struct *tty)
324 struct gs_port *port = tty->driver_data;
325 func_enter ();
327 func_exit ();
328 return port->xmit_cnt;
332 int gs_real_chars_in_buffer(struct tty_struct *tty)
334 struct gs_port *port;
335 func_enter ();
337 if (!tty) return 0;
338 port = tty->driver_data;
340 if (!port->rd) return 0;
341 if (!port->rd->chars_in_buffer) return 0;
343 func_exit ();
344 return port->xmit_cnt + port->rd->chars_in_buffer (port);
348 static int gs_wait_tx_flushed (void * ptr, int timeout)
350 struct gs_port *port = ptr;
351 unsigned long end_jiffies;
352 int jiffies_to_transmit, charsleft = 0, rv = 0;
353 int rcib;
355 func_enter();
357 gs_dprintk (GS_DEBUG_FLUSH, "port=%p.\n", port);
358 if (port) {
359 gs_dprintk (GS_DEBUG_FLUSH, "xmit_cnt=%x, xmit_buf=%p, tty=%p.\n",
360 port->xmit_cnt, port->xmit_buf, port->tty);
363 if (!port || port->xmit_cnt < 0 || !port->xmit_buf) {
364 gs_dprintk (GS_DEBUG_FLUSH, "ERROR: !port, !port->xmit_buf or prot->xmit_cnt < 0.\n");
365 func_exit();
366 return -EINVAL; /* This is an error which we don't know how to handle. */
369 rcib = gs_real_chars_in_buffer(port->tty);
371 if(rcib <= 0) {
372 gs_dprintk (GS_DEBUG_FLUSH, "nothing to wait for.\n");
373 func_exit();
374 return rv;
376 /* stop trying: now + twice the time it would normally take + seconds */
377 if (timeout == 0) timeout = MAX_SCHEDULE_TIMEOUT;
378 end_jiffies = jiffies;
379 if (timeout != MAX_SCHEDULE_TIMEOUT)
380 end_jiffies += port->baud?(2 * rcib * 10 * HZ / port->baud):0;
381 end_jiffies += timeout;
383 gs_dprintk (GS_DEBUG_FLUSH, "now=%lx, end=%lx (%ld).\n",
384 jiffies, end_jiffies, end_jiffies-jiffies);
386 /* the expression is actually jiffies < end_jiffies, but that won't
387 work around the wraparound. Tricky eh? */
388 while ((charsleft = gs_real_chars_in_buffer (port->tty)) &&
389 time_after (end_jiffies, jiffies)) {
390 /* Units check:
391 chars * (bits/char) * (jiffies /sec) / (bits/sec) = jiffies!
392 check! */
394 charsleft += 16; /* Allow 16 chars more to be transmitted ... */
395 jiffies_to_transmit = port->baud?(1 + charsleft * 10 * HZ / port->baud):0;
396 /* ^^^ Round up.... */
397 if (jiffies_to_transmit <= 0) jiffies_to_transmit = 1;
399 gs_dprintk (GS_DEBUG_FLUSH, "Expect to finish in %d jiffies "
400 "(%d chars).\n", jiffies_to_transmit, charsleft);
402 set_current_state (TASK_INTERRUPTIBLE);
403 schedule_timeout(jiffies_to_transmit);
404 if (signal_pending (current)) {
405 gs_dprintk (GS_DEBUG_FLUSH, "Signal pending. Bombing out: ");
406 rv = -EINTR;
407 break;
411 gs_dprintk (GS_DEBUG_FLUSH, "charsleft = %d.\n", charsleft);
412 set_current_state (TASK_RUNNING);
414 func_exit();
415 return rv;
420 void gs_flush_buffer(struct tty_struct *tty)
422 struct gs_port *port;
423 unsigned long flags;
425 func_enter ();
427 if (!tty) return;
429 port = tty->driver_data;
431 if (!port) return;
433 /* XXX Would the write semaphore do? */
434 save_flags(flags); cli();
435 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
436 restore_flags(flags);
438 wake_up_interruptible(&tty->write_wait);
439 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
440 tty->ldisc.write_wakeup)
441 (tty->ldisc.write_wakeup)(tty);
442 func_exit ();
446 void gs_flush_chars(struct tty_struct * tty)
448 struct gs_port *port;
450 func_enter ();
452 if (!tty) return;
454 port = tty->driver_data;
456 if (!port) return;
458 if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
459 !port->xmit_buf) {
460 func_exit ();
461 return;
464 /* Beats me -- REW */
465 port->flags |= GS_TX_INTEN;
466 port->rd->enable_tx_interrupts (port);
467 func_exit ();
471 void gs_stop(struct tty_struct * tty)
473 struct gs_port *port;
475 func_enter ();
477 if (!tty) return;
479 port = tty->driver_data;
481 if (!port) return;
483 if (port->xmit_cnt &&
484 port->xmit_buf &&
485 (port->flags & GS_TX_INTEN) ) {
486 port->flags &= ~GS_TX_INTEN;
487 port->rd->disable_tx_interrupts (port);
489 func_exit ();
493 void gs_start(struct tty_struct * tty)
495 struct gs_port *port;
497 if (!tty) return;
499 port = tty->driver_data;
501 if (!port) return;
503 if (port->xmit_cnt &&
504 port->xmit_buf &&
505 !(port->flags & GS_TX_INTEN) ) {
506 port->flags |= GS_TX_INTEN;
507 port->rd->enable_tx_interrupts (port);
509 func_exit ();
513 void gs_shutdown_port (struct gs_port *port)
515 unsigned long flags;
517 func_enter();
519 if (!port) return;
521 if (!(port->flags & ASYNC_INITIALIZED))
522 return;
524 save_flags (flags);
525 cli ();
527 if (port->xmit_buf) {
528 free_page((unsigned long) port->xmit_buf);
529 port->xmit_buf = 0;
532 if (port->tty)
533 set_bit(TTY_IO_ERROR, &port->tty->flags);
535 port->rd->shutdown_port (port);
537 port->flags &= ~ASYNC_INITIALIZED;
538 restore_flags (flags);
540 func_exit();
544 void gs_hangup(struct tty_struct *tty)
546 struct gs_port *port;
548 func_enter ();
550 if (!tty) return;
552 port = tty->driver_data;
553 tty = port->tty;
554 if (!tty)
555 return;
557 gs_shutdown_port (port);
558 port->flags &= ~(ASYNC_NORMAL_ACTIVE|GS_ACTIVE);
559 port->tty = NULL;
560 port->count = 0;
562 wake_up_interruptible(&port->open_wait);
563 func_exit ();
567 void gs_do_softint(void *private_)
569 struct gs_port *port = private_;
570 struct tty_struct *tty;
572 func_enter ();
574 if (!port) return;
576 tty = port->tty;
578 if (!tty) return;
580 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &port->event)) {
581 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
582 tty->ldisc.write_wakeup)
583 (tty->ldisc.write_wakeup)(tty);
584 wake_up_interruptible(&tty->write_wait);
586 func_exit ();
590 int gs_block_til_ready(void *port_, struct file * filp)
592 struct gs_port *port = port_;
593 DECLARE_WAITQUEUE(wait, current);
594 int retval;
595 int do_clocal = 0;
596 int CD;
597 struct tty_struct *tty;
599 func_enter ();
601 if (!port) return 0;
603 tty = port->tty;
605 if (!tty) return 0;
607 gs_dprintk (GS_DEBUG_BTR, "Entering gs_block_till_ready.\n");
609 * If the device is in the middle of being closed, then block
610 * until it's done, and then try again.
612 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
613 interruptible_sleep_on(&port->close_wait);
614 if (port->flags & ASYNC_HUP_NOTIFY)
615 return -EAGAIN;
616 else
617 return -ERESTARTSYS;
620 gs_dprintk (GS_DEBUG_BTR, "after hung up\n");
623 * If non-blocking mode is set, or the port is not enabled,
624 * then make the check up front and then exit.
626 if ((filp->f_flags & O_NONBLOCK) ||
627 (tty->flags & (1 << TTY_IO_ERROR))) {
628 port->flags |= ASYNC_NORMAL_ACTIVE;
629 return 0;
632 gs_dprintk (GS_DEBUG_BTR, "after nonblock\n");
634 if (C_CLOCAL(tty))
635 do_clocal = 1;
638 * Block waiting for the carrier detect and the line to become
639 * free (i.e., not in use by the callout). While we are in
640 * this loop, port->count is dropped by one, so that
641 * rs_close() knows when to free things. We restore it upon
642 * exit, either normal or abnormal.
644 retval = 0;
646 add_wait_queue(&port->open_wait, &wait);
648 gs_dprintk (GS_DEBUG_BTR, "after add waitq.\n");
649 cli();
650 if (!tty_hung_up_p(filp))
651 port->count--;
652 sti();
653 port->blocked_open++;
654 while (1) {
655 CD = port->rd->get_CD (port);
656 gs_dprintk (GS_DEBUG_BTR, "CD is now %d.\n", CD);
657 set_current_state (TASK_INTERRUPTIBLE);
658 if (tty_hung_up_p(filp) ||
659 !(port->flags & ASYNC_INITIALIZED)) {
660 if (port->flags & ASYNC_HUP_NOTIFY)
661 retval = -EAGAIN;
662 else
663 retval = -ERESTARTSYS;
664 break;
666 if (!(port->flags & ASYNC_CLOSING) &&
667 (do_clocal || CD))
668 break;
669 gs_dprintk (GS_DEBUG_BTR, "signal_pending is now: %d (%lx)\n",
670 (int)signal_pending (current), *(long*)(&current->blocked));
671 if (signal_pending(current)) {
672 retval = -ERESTARTSYS;
673 break;
675 schedule();
677 gs_dprintk (GS_DEBUG_BTR, "Got out of the loop. (%d)\n",
678 port->blocked_open);
679 set_current_state (TASK_RUNNING);
680 remove_wait_queue(&port->open_wait, &wait);
681 if (!tty_hung_up_p(filp))
682 port->count++;
683 port->blocked_open--;
684 if (retval)
685 return retval;
687 port->flags |= ASYNC_NORMAL_ACTIVE;
688 func_exit ();
689 return 0;
693 void gs_close(struct tty_struct * tty, struct file * filp)
695 unsigned long flags;
696 struct gs_port *port;
698 func_enter ();
700 if (!tty) return;
702 port = (struct gs_port *) tty->driver_data;
704 if (!port) return;
706 if (!port->tty) {
707 /* This seems to happen when this is called from vhangup. */
708 gs_dprintk (GS_DEBUG_CLOSE, "gs: Odd: port->tty is NULL\n");
709 port->tty = tty;
712 save_flags(flags); cli();
714 if (tty_hung_up_p(filp)) {
715 restore_flags(flags);
716 port->rd->hungup (port);
717 func_exit ();
718 return;
721 if ((tty->count == 1) && (port->count != 1)) {
722 printk(KERN_ERR "gs: gs_close: bad port count;"
723 " tty->count is 1, port count is %d\n", port->count);
724 port->count = 1;
726 if (--port->count < 0) {
727 printk(KERN_ERR "gs: gs_close: bad port count: %d\n", port->count);
728 port->count = 0;
730 if (port->count) {
731 gs_dprintk(GS_DEBUG_CLOSE, "gs_close: count: %d\n", port->count);
732 restore_flags(flags);
733 func_exit ();
734 return;
736 port->flags |= ASYNC_CLOSING;
739 * Now we wait for the transmit buffer to clear; and we notify
740 * the line discipline to only process XON/XOFF characters.
742 tty->closing = 1;
743 /* if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
744 tty_wait_until_sent(tty, port->closing_wait); */
747 * At this point we stop accepting input. To do this, we
748 * disable the receive line status interrupts, and tell the
749 * interrupt driver to stop checking the data ready bit in the
750 * line status register.
753 port->rd->disable_rx_interrupts (port);
755 /* close has no way of returning "EINTR", so discard return value */
756 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
757 gs_wait_tx_flushed (port, port->closing_wait);
759 port->flags &= ~GS_ACTIVE;
761 if (tty->driver->flush_buffer)
762 tty->driver->flush_buffer(tty);
763 if (tty->ldisc.flush_buffer)
764 tty->ldisc.flush_buffer(tty);
765 tty->closing = 0;
767 port->event = 0;
768 port->rd->close (port);
769 port->rd->shutdown_port (port);
770 port->tty = 0;
772 if (port->blocked_open) {
773 if (port->close_delay) {
774 set_current_state (TASK_INTERRUPTIBLE);
775 schedule_timeout(port->close_delay);
777 wake_up_interruptible(&port->open_wait);
779 port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING | ASYNC_INITIALIZED);
780 wake_up_interruptible(&port->close_wait);
782 restore_flags(flags);
783 func_exit ();
787 static unsigned int gs_baudrates[] = {
788 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
789 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
793 void gs_set_termios (struct tty_struct * tty,
794 struct termios * old_termios)
796 struct gs_port *port;
797 int baudrate, tmp, rv;
798 struct termios *tiosp;
800 func_enter();
802 if (!tty) return;
804 port = tty->driver_data;
806 if (!port) return;
808 tiosp = tty->termios;
810 if (gs_debug & GS_DEBUG_TERMIOS) {
811 gs_dprintk (GS_DEBUG_TERMIOS, "termios structure (%p):\n", tiosp);
814 #if 0
815 /* This is an optimization that is only allowed for dumb cards */
816 /* Smart cards require knowledge of iflags and oflags too: that
817 might change hardware cooking mode.... */
818 #endif
819 if (old_termios) {
820 if( (tiosp->c_iflag == old_termios->c_iflag)
821 && (tiosp->c_oflag == old_termios->c_oflag)
822 && (tiosp->c_cflag == old_termios->c_cflag)
823 && (tiosp->c_lflag == old_termios->c_lflag)
824 && (tiosp->c_line == old_termios->c_line)
825 && (memcmp(tiosp->c_cc, old_termios->c_cc, NCC) == 0)) {
826 gs_dprintk(GS_DEBUG_TERMIOS, "gs_set_termios: optimized away\n");
827 return /* 0 */;
829 } else
830 gs_dprintk(GS_DEBUG_TERMIOS, "gs_set_termios: no old_termios: "
831 "no optimization\n");
833 if(old_termios && (gs_debug & GS_DEBUG_TERMIOS)) {
834 if(tiosp->c_iflag != old_termios->c_iflag) printk("c_iflag changed\n");
835 if(tiosp->c_oflag != old_termios->c_oflag) printk("c_oflag changed\n");
836 if(tiosp->c_cflag != old_termios->c_cflag) printk("c_cflag changed\n");
837 if(tiosp->c_lflag != old_termios->c_lflag) printk("c_lflag changed\n");
838 if(tiosp->c_line != old_termios->c_line) printk("c_line changed\n");
839 if(!memcmp(tiosp->c_cc, old_termios->c_cc, NCC)) printk("c_cc changed\n");
842 baudrate = tiosp->c_cflag & CBAUD;
843 if (baudrate & CBAUDEX) {
844 baudrate &= ~CBAUDEX;
845 if ((baudrate < 1) || (baudrate > 4))
846 tiosp->c_cflag &= ~CBAUDEX;
847 else
848 baudrate += 15;
851 baudrate = gs_baudrates[baudrate];
852 if ((tiosp->c_cflag & CBAUD) == B38400) {
853 if ( (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
854 baudrate = 57600;
855 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
856 baudrate = 115200;
857 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
858 baudrate = 230400;
859 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
860 baudrate = 460800;
861 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
862 baudrate = (port->baud_base / port->custom_divisor);
865 /* I recommend using THIS instead of the mess in termios (and
866 duplicating the above code). Next we should create a clean
867 interface towards this variable. If your card supports arbitrary
868 baud rates, (e.g. CD1400 or 16550 based cards) then everything
869 will be very easy..... */
870 port->baud = baudrate;
872 /* Two timer ticks seems enough to wakeup something like SLIP driver */
873 /* Baudrate/10 is cps. Divide by HZ to get chars per tick. */
874 tmp = (baudrate / 10 / HZ) * 2;
876 if (tmp < 0) tmp = 0;
877 if (tmp >= SERIAL_XMIT_SIZE) tmp = SERIAL_XMIT_SIZE-1;
879 port->wakeup_chars = tmp;
881 /* We should really wait for the characters to be all sent before
882 changing the settings. -- CAL */
883 rv = gs_wait_tx_flushed (port, MAX_SCHEDULE_TIMEOUT);
884 if (rv < 0) return /* rv */;
886 rv = port->rd->set_real_termios(port);
887 if (rv < 0) return /* rv */;
889 if ((!old_termios ||
890 (old_termios->c_cflag & CRTSCTS)) &&
891 !( tiosp->c_cflag & CRTSCTS)) {
892 tty->stopped = 0;
893 gs_start(tty);
896 #ifdef tytso_patch_94Nov25_1726
897 /* This "makes sense", Why is it commented out? */
899 if (!(old_termios->c_cflag & CLOCAL) &&
900 (tty->termios->c_cflag & CLOCAL))
901 wake_up_interruptible(&info->open_wait);
902 #endif
904 func_exit();
905 return /* 0 */;
910 /* Must be called with interrupts enabled */
911 int gs_init_port(struct gs_port *port)
913 unsigned long flags;
914 unsigned long page;
916 save_flags (flags);
917 if (!tmp_buf) {
918 page = get_zeroed_page(GFP_KERNEL);
920 cli (); /* Don't expect this to make a difference. */
921 if (tmp_buf)
922 free_page(page);
923 else
924 tmp_buf = (unsigned char *) page;
925 restore_flags (flags);
927 if (!tmp_buf) {
928 return -ENOMEM;
932 if (port->flags & ASYNC_INITIALIZED)
933 return 0;
935 if (!port->xmit_buf) {
936 /* We may sleep in get_zeroed_page() */
937 unsigned long tmp;
939 tmp = get_zeroed_page(GFP_KERNEL);
941 /* Spinlock? */
942 cli ();
943 if (port->xmit_buf)
944 free_page (tmp);
945 else
946 port->xmit_buf = (unsigned char *) tmp;
947 restore_flags (flags);
949 if (!port->xmit_buf)
950 return -ENOMEM;
953 cli();
955 if (port->tty)
956 clear_bit(TTY_IO_ERROR, &port->tty->flags);
958 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
960 gs_set_termios(port->tty, NULL);
962 port->flags |= ASYNC_INITIALIZED;
963 port->flags &= ~GS_TX_INTEN;
965 restore_flags(flags);
966 return 0;
970 int gs_setserial(struct gs_port *port, struct serial_struct *sp)
972 struct serial_struct sio;
974 if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
975 return(-EFAULT);
977 if (!capable(CAP_SYS_ADMIN)) {
978 if ((sio.baud_base != port->baud_base) ||
979 (sio.close_delay != port->close_delay) ||
980 ((sio.flags & ~ASYNC_USR_MASK) !=
981 (port->flags & ~ASYNC_USR_MASK)))
982 return(-EPERM);
985 port->flags = (port->flags & ~ASYNC_USR_MASK) |
986 (sio.flags & ASYNC_USR_MASK);
988 port->baud_base = sio.baud_base;
989 port->close_delay = sio.close_delay;
990 port->closing_wait = sio.closing_wait;
991 port->custom_divisor = sio.custom_divisor;
993 gs_set_termios (port->tty, NULL);
995 return 0;
999 /*****************************************************************************/
1002 * Generate the serial struct info.
1005 int gs_getserial(struct gs_port *port, struct serial_struct *sp)
1007 struct serial_struct sio;
1009 memset(&sio, 0, sizeof(struct serial_struct));
1010 sio.flags = port->flags;
1011 sio.baud_base = port->baud_base;
1012 sio.close_delay = port->close_delay;
1013 sio.closing_wait = port->closing_wait;
1014 sio.custom_divisor = port->custom_divisor;
1015 sio.hub6 = 0;
1017 /* If you want you can override these. */
1018 sio.type = PORT_UNKNOWN;
1019 sio.xmit_fifo_size = -1;
1020 sio.line = -1;
1021 sio.port = -1;
1022 sio.irq = -1;
1024 if (port->rd->getserial)
1025 port->rd->getserial (port, &sio);
1027 if (copy_to_user(sp, &sio, sizeof(struct serial_struct)))
1028 return -EFAULT;
1029 return 0;
1034 void gs_got_break(struct gs_port *port)
1036 if (port->flags & ASYNC_SAK) {
1037 do_SAK (port->tty);
1039 *(port->tty->flip.flag_buf_ptr) = TTY_BREAK;
1040 port->tty->flip.flag_buf_ptr++;
1041 port->tty->flip.char_buf_ptr++;
1042 port->tty->flip.count++;
1046 EXPORT_SYMBOL(gs_put_char);
1047 EXPORT_SYMBOL(gs_write);
1048 EXPORT_SYMBOL(gs_write_room);
1049 EXPORT_SYMBOL(gs_chars_in_buffer);
1050 EXPORT_SYMBOL(gs_flush_buffer);
1051 EXPORT_SYMBOL(gs_flush_chars);
1052 EXPORT_SYMBOL(gs_stop);
1053 EXPORT_SYMBOL(gs_start);
1054 EXPORT_SYMBOL(gs_hangup);
1055 EXPORT_SYMBOL(gs_do_softint);
1056 EXPORT_SYMBOL(gs_block_til_ready);
1057 EXPORT_SYMBOL(gs_close);
1058 EXPORT_SYMBOL(gs_set_termios);
1059 EXPORT_SYMBOL(gs_init_port);
1060 EXPORT_SYMBOL(gs_setserial);
1061 EXPORT_SYMBOL(gs_getserial);
1062 EXPORT_SYMBOL(gs_got_break);
1064 MODULE_LICENSE("GPL");