Portability cleanup as required by Linus.
[linux-2.6/linux-mips.git] / drivers / char / generic_serial.c
blob01eb20e9202fb067131df974f967bdf635b6936f
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.
17 #include <linux/tty.h>
18 #include <linux/serial.h>
19 #include <linux/mm.h>
20 #include <asm/semaphore.h>
21 #include <asm/uaccess.h>
22 #include <linux/version.h>
23 #include <linux/module.h>
24 #include <linux/generic_serial.h>
26 #define DEBUG
28 static char * tmp_buf;
29 static DECLARE_MUTEX(tmp_buf_sem);
31 int gs_debug = 0;
34 #ifdef DEBUG
35 #define gs_dprintk(f, str...) if (gs_debug & f) printk (str)
36 #else
37 #define gs_dprintk(f, str...) /* nothing */
38 #endif
40 #define func_enter() gs_dprintk (GS_DEBUG_FLOW, "gs: enter " __FUNCTION__ "\n")
41 #define func_exit() gs_dprintk (GS_DEBUG_FLOW, "gs: exit " __FUNCTION__ "\n")
43 #if NEW_WRITE_LOCKING
44 #define DECL /* Nothing */
45 #define LOCKIT down (& port->port_write_sem);
46 #define RELEASEIT up (&port->port_write_sem);
47 #else
48 #define DECL unsigned long flags;
49 #define LOCKIT save_flags (flags);cli ()
50 #define RELEASEIT restore_flags (flags)
51 #endif
53 #define RS_EVENT_WRITE_WAKEUP 1
55 #ifdef MODULE
56 MODULE_PARM(gs_debug, "i");
57 #endif
59 #ifdef DEBUG
60 static void my_hd (unsigned char *addr, int len)
62 int i, j, ch;
64 for (i=0;i<len;i+=16) {
65 printk ("%08x ", (int) addr+i);
66 for (j=0;j<16;j++) {
67 printk ("%02x %s", addr[j+i], (j==7)?" ":"");
69 for (j=0;j<16;j++) {
70 ch = addr[j+i];
71 printk ("%c", (ch < 0x20)?'.':((ch > 0x7f)?'.':ch));
73 printk ("\n");
76 #else
77 #define my_hd(addr,len)
78 #endif
81 void gs_put_char(struct tty_struct * tty, unsigned char ch)
83 struct gs_port *port = tty->driver_data;
84 DECL
86 /* func_enter (); */
88 /* Take a lock on the serial tranmit buffer! */
89 LOCKIT;
91 if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
92 /* Sorry, buffer is full, drop character. Update statistics???? -- REW */
93 RELEASEIT;
94 return;
97 port->xmit_buf[port->xmit_head++] = ch;
98 port->xmit_head &= SERIAL_XMIT_SIZE - 1;
99 port->xmit_cnt++; /* Characters in buffer */
101 RELEASEIT;
102 /* func_exit ();*/
106 #ifdef NEW_WRITE_LOCKING
109 > Problems to take into account are:
110 > -1- Interrupts that empty part of the buffer.
111 > -2- page faults on the access to userspace.
112 > -3- Other processes that are also trying to do a "write".
115 int gs_write(struct tty_struct * tty, int from_user,
116 const unsigned char *buf, int count)
118 struct gs_port *port = tty->driver_data;
119 int c, total = 0;
120 int t;
122 /* func_enter (); */
124 if (! (port->flags & ASYNC_INITIALIZED))
125 return 0;
127 /* get exclusive "write" access to this port (problem 3) */
128 /* This is not a spinlock because we can have a disk access (page
129 fault) in copy_from_user */
130 down (& port->port_write_sem);
132 while (1) {
134 c = count;
136 /* This is safe because we "OWN" the "head". Noone else can
137 change the "head": we own the port_write_sem. */
138 /* Don't overrun the end of the buffer */
139 t = SERIAL_XMIT_SIZE - port->xmit_head;
140 if (t < c) c = t;
142 /* This is safe because the xmit_cnt can only decrease. This
143 would increase "t", so we might copy too little chars. */
144 /* Don't copy past the "head" of the buffer */
145 t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
146 if (t < c) c = t;
148 /* Can't copy more? break out! */
149 if (c <= 0) break;
150 if (from_user)
151 copy_from_user (port->xmit_buf + port->xmit_head, buf, c);
152 else
153 memcpy (port->xmit_buf + port->xmit_head, buf, c);
155 port -> xmit_cnt += c;
156 port -> xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE -1);
157 buf += c;
158 count -= c;
159 total += c;
161 up (& port->port_write_sem);
163 gs_dprintk (GS_DEBUG_WRITE, "write: interrupts are %s\n",
164 (port->flags & GS_TX_INTEN)?"enabled": "disabled");
166 if (port->xmit_cnt &&
167 !tty->stopped &&
168 !tty->hw_stopped &&
169 !(port->flags & GS_TX_INTEN)) {
170 port->flags |= GS_TX_INTEN;
171 port->rd->enable_tx_interrupts (port);
173 /* func_exit (); */
174 return total;
176 #else
178 > Problems to take into account are:
179 > -1- Interrupts that empty part of the buffer.
180 > -2- page faults on the access to userspace.
181 > -3- Other processes that are also trying to do a "write".
184 int gs_write(struct tty_struct * tty, int from_user,
185 const unsigned char *buf, int count)
187 struct gs_port *port;
188 int c, total = 0;
189 int t;
190 unsigned long flags;
192 func_enter ();
194 /* The standard serial driver returns 0 in this case.
195 That sounds to me as "No error, I just didn't get to writing any
196 bytes. Feel free to try again."
197 The "official" way to write n bytes from buf is:
199 for (nwritten = 0;nwritten < n;nwritten += rv) {
200 rv = write (fd, buf+nwritten, n-nwritten);
201 if (rv < 0) break; // Error: bail out. //
204 which will loop endlessly in this case. The manual page for write
205 agrees with me. In practise almost everybody writes
206 "write (fd, buf,n);" but some people might have had to deal with
207 incomplete writes in the past and correctly implemented it by now...
210 if (!tty) return -EIO;
212 port = tty->driver_data;
213 if (!port || !port->xmit_buf || !tmp_buf)
214 return -EIO;
216 save_flags(flags);
217 if (from_user) {
218 down(&tmp_buf_sem);
219 while (1) {
220 c = count;
222 /* This is safe because we "OWN" the "head". Noone else can
223 change the "head": we own the port_write_sem. */
224 /* Don't overrun the end of the buffer */
225 t = SERIAL_XMIT_SIZE - port->xmit_head;
226 if (t < c) c = t;
228 /* This is safe because the xmit_cnt can only decrease. This
229 would increase "t", so we might copy too little chars. */
230 /* Don't copy past the "head" of the buffer */
231 t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
232 if (t < c) c = t;
234 /* Can't copy more? break out! */
235 if (c <= 0) break;
237 c -= copy_from_user(tmp_buf, buf, c);
238 if (!c) {
239 if (!total)
240 total = -EFAULT;
241 break;
243 cli();
244 t = SERIAL_XMIT_SIZE - port->xmit_head;
245 if (t < c) c = t;
246 t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
247 if (t < c) c = t;
249 memcpy(port->xmit_buf + port->xmit_head, tmp_buf, c);
250 port->xmit_head = ((port->xmit_head + c) &
251 (SERIAL_XMIT_SIZE-1));
252 port->xmit_cnt += c;
253 restore_flags(flags);
254 buf += c;
255 count -= c;
256 total += c;
258 up(&tmp_buf_sem);
259 } else {
260 while (1) {
261 cli();
262 c = count;
264 /* This is safe because we "OWN" the "head". Noone else can
265 change the "head": we own the port_write_sem. */
266 /* Don't overrun the end of the buffer */
267 t = SERIAL_XMIT_SIZE - port->xmit_head;
268 if (t < c) c = t;
270 /* This is safe because the xmit_cnt can only decrease. This
271 would increase "t", so we might copy too little chars. */
272 /* Don't copy past the "head" of the buffer */
273 t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
274 if (t < c) c = t;
276 /* Can't copy more? break out! */
277 if (c <= 0) {
278 restore_flags(flags);
279 break;
281 memcpy(port->xmit_buf + port->xmit_head, buf, c);
282 port->xmit_head = ((port->xmit_head + c) &
283 (SERIAL_XMIT_SIZE-1));
284 port->xmit_cnt += c;
285 restore_flags(flags);
286 buf += c;
287 count -= c;
288 total += c;
292 if (port->xmit_cnt &&
293 !tty->stopped &&
294 !tty->hw_stopped &&
295 !(port->flags & GS_TX_INTEN)) {
296 port->flags |= GS_TX_INTEN;
297 port->rd->enable_tx_interrupts (port);
299 func_exit ();
300 return total;
303 #endif
307 int gs_write_room(struct tty_struct * tty)
309 struct gs_port *port = tty->driver_data;
310 int ret;
312 /* func_enter (); */
313 ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
314 if (ret < 0)
315 ret = 0;
316 /* func_exit (); */
317 return ret;
321 int gs_chars_in_buffer(struct tty_struct *tty)
323 struct gs_port *port = tty->driver_data;
324 func_enter ();
326 func_exit ();
327 return port->xmit_cnt;
331 int gs_real_chars_in_buffer(struct tty_struct *tty)
333 struct gs_port *port;
334 func_enter ();
336 if (!tty) return 0;
337 port = tty->driver_data;
339 if (!port->rd) return 0;
340 if (!port->rd->chars_in_buffer) return 0;
342 func_exit ();
343 return port->xmit_cnt + port->rd->chars_in_buffer (port);
347 static int gs_wait_tx_flushed (void * ptr, int timeout)
349 struct gs_port *port = ptr;
350 long end_jiffies;
351 int jiffies_to_transmit, charsleft = 0, rv = 0;
352 int to, rcib;
354 func_enter();
356 gs_dprintk (GS_DEBUG_FLUSH, "port=%p.\n", port);
357 if (port) {
358 gs_dprintk (GS_DEBUG_FLUSH, "xmit_cnt=%x, xmit_buf=%p, tty=%p.\n",
359 port->xmit_cnt, port->xmit_buf, port->tty);
362 if (!port || port->xmit_cnt < 0 || !port->xmit_buf) {
363 gs_dprintk (GS_DEBUG_FLUSH, "ERROR: !port, !port->xmit_buf or prot->xmit_cnt < 0.\n");
364 func_exit();
365 return -EINVAL; /* This is an error which we don't know how to handle. */
368 rcib = gs_real_chars_in_buffer(port->tty);
370 if(rcib <= 0) {
371 gs_dprintk (GS_DEBUG_FLUSH, "nothing to wait for.\n");
372 func_exit();
373 return rv;
375 /* stop trying: now + twice the time it would normally take + seconds */
376 end_jiffies = jiffies;
377 if (timeout != MAX_SCHEDULE_TIMEOUT)
378 end_jiffies += port->baud?(2 * rcib * 10 * HZ / port->baud):0;
379 end_jiffies += timeout;
381 gs_dprintk (GS_DEBUG_FLUSH, "now=%lx, end=%lx (%ld).\n",
382 jiffies, end_jiffies, end_jiffies-jiffies);
384 to = 100;
385 /* the expression is actually jiffies < end_jiffies, but that won't
386 work around the wraparound. Tricky eh? */
387 while (to-- &&
388 (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 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 current->state = TASK_RUNNING;
414 func_exit();
415 return rv;
420 void gs_flush_buffer(struct tty_struct *tty)
422 struct gs_port *port = tty->driver_data;
423 unsigned long flags;
425 func_enter ();
426 /* XXX Would the write semaphore do? */
427 save_flags(flags); cli();
428 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
429 restore_flags(flags);
431 wake_up_interruptible(&tty->write_wait);
432 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
433 tty->ldisc.write_wakeup)
434 (tty->ldisc.write_wakeup)(tty);
435 func_exit ();
439 void gs_flush_chars(struct tty_struct * tty)
441 struct gs_port *port = tty->driver_data;
443 func_enter ();
444 if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
445 !port->xmit_buf) {
446 func_exit ();
447 return;
450 /* Beats me -- REW */
451 port->flags |= GS_TX_INTEN;
452 port->rd->enable_tx_interrupts (port);
453 func_exit ();
457 void gs_stop(struct tty_struct * tty)
459 struct gs_port *port = tty->driver_data;
461 func_enter ();
462 if (port->xmit_cnt &&
463 port->xmit_buf &&
464 (port->flags & GS_TX_INTEN) ) {
465 port->flags &= ~GS_TX_INTEN;
466 port->rd->disable_tx_interrupts (port);
468 func_exit ();
472 void gs_start(struct tty_struct * tty)
474 struct gs_port *port = tty->driver_data;
476 if (port->xmit_cnt &&
477 port->xmit_buf &&
478 !(port->flags & GS_TX_INTEN) ) {
479 port->flags |= GS_TX_INTEN;
480 port->rd->enable_tx_interrupts (port);
482 func_exit ();
486 void gs_shutdown_port (struct gs_port *port)
488 long flags;
489 func_enter();
490 if (!(port->flags & ASYNC_INITIALIZED))
491 return;
493 save_flags (flags);
494 cli ();
496 if (port->xmit_buf) {
497 free_page((unsigned long) port->xmit_buf);
498 port->xmit_buf = 0;
501 if (port->tty)
502 set_bit(TTY_IO_ERROR, &port->tty->flags);
504 port->rd->shutdown_port (port);
506 port->flags &= ~ASYNC_INITIALIZED;
507 restore_flags (flags);
508 func_exit();
512 void gs_hangup(struct tty_struct *tty)
514 struct gs_port *port = tty->driver_data;
516 func_enter ();
518 tty = port->tty;
519 if (!tty)
520 return;
522 gs_shutdown_port (port);
523 port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE |GS_ACTIVE);
524 port->tty = NULL;
525 port->count = 0;
527 wake_up_interruptible(&port->open_wait);
528 func_exit ();
532 void gs_do_softint(void *private_)
534 struct gs_port *port = private_;
535 struct tty_struct *tty;
537 tty = port->tty;
538 if(!tty) return;
540 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &port->event)) {
541 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
542 tty->ldisc.write_wakeup)
543 (tty->ldisc.write_wakeup)(tty);
544 wake_up_interruptible(&tty->write_wait);
546 func_exit ();
550 int block_til_ready(void *port_, struct file * filp)
552 struct gs_port *port = port_;
553 DECLARE_WAITQUEUE(wait, current);
554 int retval;
555 int do_clocal = 0;
556 int CD;
557 struct tty_struct *tty;
559 func_enter ();
560 tty = port->tty;
562 gs_dprintk (GS_DEBUG_BTR, "Entering block_till_ready.\n");
564 * If the device is in the middle of being closed, then block
565 * until it's done, and then try again.
567 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
568 interruptible_sleep_on(&port->close_wait);
569 if (port->flags & ASYNC_HUP_NOTIFY)
570 return -EAGAIN;
571 else
572 return -ERESTARTSYS;
574 gs_dprintk (GS_DEBUG_BTR, "after hung up\n");
577 * If this is a callout device, then just make sure the normal
578 * device isn't being used.
580 if (tty->driver.subtype == GS_TYPE_CALLOUT) {
581 if (port->flags & ASYNC_NORMAL_ACTIVE)
582 return -EBUSY;
583 if ((port->flags & ASYNC_CALLOUT_ACTIVE) &&
584 (port->flags & ASYNC_SESSION_LOCKOUT) &&
585 (port->session != current->session))
586 return -EBUSY;
587 if ((port->flags & ASYNC_CALLOUT_ACTIVE) &&
588 (port->flags & ASYNC_PGRP_LOCKOUT) &&
589 (port->pgrp != current->pgrp))
590 return -EBUSY;
591 port->flags |= ASYNC_CALLOUT_ACTIVE;
592 return 0;
595 gs_dprintk (GS_DEBUG_BTR, "after subtype\n");
597 * If non-blocking mode is set, or the port is not enabled,
598 * then make the check up front and then exit.
600 if ((filp->f_flags & O_NONBLOCK) ||
601 (tty->flags & (1 << TTY_IO_ERROR))) {
602 if (port->flags & ASYNC_CALLOUT_ACTIVE)
603 return -EBUSY;
604 port->flags |= ASYNC_NORMAL_ACTIVE;
605 return 0;
608 gs_dprintk (GS_DEBUG_BTR, "after nonblock\n");
609 if (port->flags & ASYNC_CALLOUT_ACTIVE) {
610 if (port->normal_termios.c_cflag & CLOCAL)
611 do_clocal = 1;
612 } else {
613 if (C_CLOCAL(tty))
614 do_clocal = 1;
617 gs_dprintk (GS_DEBUG_BTR, "after clocal check.\n");
619 * Block waiting for the carrier detect and the line to become
620 * free (i.e., not in use by the callout). While we are in
621 * this loop, port->count is dropped by one, so that
622 * rs_close() knows when to free things. We restore it upon
623 * exit, either normal or abnormal.
625 retval = 0;
627 add_wait_queue(&port->open_wait, &wait);
629 gs_dprintk (GS_DEBUG_BTR, "after add waitq.\n");
630 cli();
631 if (!tty_hung_up_p(filp))
632 port->count--;
633 sti();
634 port->blocked_open++;
635 while (1) {
636 CD = port->rd->get_CD (port);
637 gs_dprintk (GS_DEBUG_BTR, "CD is now %d.\n", CD);
638 set_current_state(TASK_INTERRUPTIBLE);
639 if (tty_hung_up_p(filp) ||
640 !(port->flags & ASYNC_INITIALIZED)) {
641 if (port->flags & ASYNC_HUP_NOTIFY)
642 retval = -EAGAIN;
643 else
644 retval = -ERESTARTSYS;
645 break;
647 if (!(port->flags & ASYNC_CALLOUT_ACTIVE) &&
648 !(port->flags & ASYNC_CLOSING) &&
649 (do_clocal || CD))
650 break;
651 gs_dprintk (GS_DEBUG_BTR, "signal_pending is now: %d (%lx)\n",
652 (int)signal_pending (current), *(long*)(&current->blocked));
653 if (signal_pending(current)) {
654 retval = -ERESTARTSYS;
655 break;
657 schedule();
659 gs_dprintk (GS_DEBUG_BTR, "Got out of the loop. (%d)\n",
660 port->blocked_open);
661 set_current_state (TASK_RUNNING);
662 remove_wait_queue(&port->open_wait, &wait);
663 if (!tty_hung_up_p(filp))
664 port->count++;
665 port->blocked_open--;
666 if (retval)
667 return retval;
669 port->flags |= ASYNC_NORMAL_ACTIVE;
670 func_exit ();
671 return 0;
675 void gs_close(struct tty_struct * tty, struct file * filp)
677 unsigned long flags;
678 struct gs_port *port;
680 func_enter ();
682 port = (struct gs_port *) tty->driver_data;
684 if(! port) {
685 func_exit();
686 return;
689 if (!port->tty) {
690 /* This seems to happen when this is called from vhangup. */
691 gs_dprintk (GS_DEBUG_CLOSE, "gs: Odd: port->tty is NULL\n");
692 port->tty = tty;
695 save_flags(flags); cli();
696 if (tty_hung_up_p(filp)) {
697 restore_flags(flags);
698 port->rd->hungup (port);
699 func_exit ();
700 return;
703 if ((tty->count == 1) && (port->count != 1)) {
704 printk(KERN_ERR "gs: gs_close: bad port count;"
705 " tty->count is 1, port count is %d\n", port->count);
706 port->count = 1;
708 if (--port->count < 0) {
709 printk(KERN_ERR "gs: gs_close: bad port count: %d\n", port->count);
710 port->count = 0;
712 if (port->count) {
713 gs_dprintk(GS_DEBUG_CLOSE, "gs_close: count: %d\n", port->count);
714 restore_flags(flags);
715 func_exit ();
716 return;
718 port->flags |= ASYNC_CLOSING;
721 * Save the termios structure, since this port may have
722 * separate termios for callout and dialin.
724 if (port->flags & ASYNC_NORMAL_ACTIVE)
725 port->normal_termios = *tty->termios;
726 if (port->flags & ASYNC_CALLOUT_ACTIVE)
727 port->callout_termios = *tty->termios;
729 * Now we wait for the transmit buffer to clear; and we notify
730 * the line discipline to only process XON/XOFF characters.
732 tty->closing = 1;
733 /* if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
734 tty_wait_until_sent(tty, port->closing_wait); */
737 * At this point we stop accepting input. To do this, we
738 * disable the receive line status interrupts, and tell the
739 * interrupt driver to stop checking the data ready bit in the
740 * line status register.
743 port->rd->disable_rx_interrupts (port);
745 /* close has no way of returning "EINTR", so discard return value */
746 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
747 gs_wait_tx_flushed (port, port->closing_wait);
749 port->flags &= ~GS_ACTIVE;
751 if (tty->driver.flush_buffer)
752 tty->driver.flush_buffer(tty);
753 if (tty->ldisc.flush_buffer)
754 tty->ldisc.flush_buffer(tty);
755 tty->closing = 0;
757 port->event = 0;
758 port->rd->close (port);
759 port->rd->shutdown_port (port);
760 port->tty = 0;
762 if (port->blocked_open) {
763 if (port->close_delay) {
764 current->state = TASK_INTERRUPTIBLE;
765 schedule_timeout(port->close_delay);
767 wake_up_interruptible(&port->open_wait);
769 port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
770 ASYNC_CLOSING | ASYNC_INITIALIZED);
771 wake_up_interruptible(&port->close_wait);
773 restore_flags(flags);
774 func_exit ();
778 static unsigned int gs_baudrates[] = {
779 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
780 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
784 void gs_set_termios (struct tty_struct * tty,
785 struct termios * old_termios)
787 struct gs_port *port = tty->driver_data;
788 int baudrate, tmp, rv;
789 struct termios *tiosp;
791 func_enter();
793 tiosp = tty->termios;
796 if (gs_debug & GS_DEBUG_TERMIOS) {
797 gs_dprintk (GS_DEBUG_TERMIOS, "termios structure (%p):\n", tiosp);
800 #if 0
801 /* This is an optimization that is only allowed for dumb cards */
802 /* Smart cards require knowledge of iflags and oflags too: that
803 might change hardware cooking mode.... */
804 #endif
805 if (old_termios) {
806 if( (tiosp->c_iflag == old_termios->c_iflag)
807 && (tiosp->c_oflag == old_termios->c_oflag)
808 && (tiosp->c_cflag == old_termios->c_cflag)
809 && (tiosp->c_lflag == old_termios->c_lflag)
810 && (tiosp->c_line == old_termios->c_line)
811 && (memcmp(tiosp->c_cc, old_termios->c_cc, NCC) == 0)) {
812 gs_dprintk(GS_DEBUG_TERMIOS, "gs_set_termios: optimized away\n");
813 return /* 0 */;
815 } else
816 gs_dprintk(GS_DEBUG_TERMIOS, "gs_set_termios: no old_termios: "
817 "no optimization\n");
819 if(old_termios && (gs_debug & GS_DEBUG_TERMIOS)) {
820 if(tiosp->c_iflag != old_termios->c_iflag) printk("c_iflag changed\n");
821 if(tiosp->c_oflag != old_termios->c_oflag) printk("c_oflag changed\n");
822 if(tiosp->c_cflag != old_termios->c_cflag) printk("c_cflag changed\n");
823 if(tiosp->c_lflag != old_termios->c_lflag) printk("c_lflag changed\n");
824 if(tiosp->c_line != old_termios->c_line) printk("c_line changed\n");
825 if(!memcmp(tiosp->c_cc, old_termios->c_cc, NCC)) printk("c_cc changed\n");
828 baudrate = tiosp->c_cflag & CBAUD;
829 if (baudrate & CBAUDEX) {
830 baudrate &= ~CBAUDEX;
831 if ((baudrate < 1) || (baudrate > 4))
832 tiosp->c_cflag &= ~CBAUDEX;
833 else
834 baudrate += 15;
837 baudrate = gs_baudrates[baudrate];
838 if ((tiosp->c_cflag & CBAUD) == B38400) {
839 if ( (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
840 baudrate = 57600;
841 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
842 baudrate = 115200;
843 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
844 baudrate = 230400;
845 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
846 baudrate = 460800;
847 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
848 baudrate = (port->baud_base / port->custom_divisor);
851 /* I recommend using THIS instead of the mess in termios (and
852 duplicating the above code). Next we should create a clean
853 interface towards this variable. If your card supports arbitrary
854 baud rates, (e.g. CD1400 or 16550 based cards) then everything
855 will be very easy..... */
856 port->baud = baudrate;
858 /* Two timer ticks seems enough to wakeup something like SLIP driver */
859 /* Baudrate/10 is cps. Divide by HZ to get chars per tick. */
860 tmp = (baudrate / 10 / HZ) * 2;
862 if (tmp < 0) tmp = 0;
863 if (tmp >= SERIAL_XMIT_SIZE) tmp = SERIAL_XMIT_SIZE-1;
865 port->wakeup_chars = tmp;
867 /* We should really wait for the characters to be all sent before
868 changing the settings. -- CAL */
869 rv = gs_wait_tx_flushed (port, MAX_SCHEDULE_TIMEOUT);
870 if (rv < 0) return /* rv */;
872 rv = port->rd->set_real_termios(port);
873 if (rv < 0) return /* rv */;
875 if ((!old_termios ||
876 (old_termios->c_cflag & CRTSCTS)) &&
877 !( tiosp->c_cflag & CRTSCTS)) {
878 tty->stopped = 0;
879 gs_start(tty);
882 #ifdef tytso_patch_94Nov25_1726
883 /* This "makes sense", Why is it commented out? */
885 if (!(old_termios->c_cflag & CLOCAL) &&
886 (tty->termios->c_cflag & CLOCAL))
887 wake_up_interruptible(&info->open_wait);
888 #endif
890 func_exit();
891 return /* 0 */;
896 /* Must be called with interrupts enabled */
897 int gs_init_port(struct gs_port *port)
899 unsigned long flags;
900 unsigned long page;
902 save_flags (flags);
903 if (!tmp_buf) {
904 page = get_free_page(GFP_KERNEL);
906 cli (); /* Don't expect this to make a difference. */
907 if (tmp_buf)
908 free_page(page);
909 else
910 tmp_buf = (unsigned char *) page;
911 restore_flags (flags);
913 if (!tmp_buf) {
914 return -ENOMEM;
918 if (port->flags & ASYNC_INITIALIZED)
919 return 0;
921 if (!port->xmit_buf) {
922 /* We may sleep in get_free_page() */
923 unsigned long tmp;
925 tmp = get_free_page(GFP_KERNEL);
927 /* Spinlock? */
928 cli ();
929 if (port->xmit_buf)
930 free_page (tmp);
931 else
932 port->xmit_buf = (unsigned char *) tmp;
933 restore_flags (flags);
935 if (!port->xmit_buf)
936 return -ENOMEM;
939 cli();
941 if (port->tty)
942 clear_bit(TTY_IO_ERROR, &port->tty->flags);
944 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
946 gs_set_termios(port->tty, NULL);
948 port->flags |= ASYNC_INITIALIZED;
949 port->flags &= ~GS_TX_INTEN;
951 restore_flags(flags);
952 return 0;
956 int gs_setserial(struct gs_port *port, struct serial_struct *sp)
958 struct serial_struct sio;
960 copy_from_user(&sio, sp, sizeof(struct serial_struct));
962 if (!capable(CAP_SYS_ADMIN)) {
963 if ((sio.baud_base != port->baud_base) ||
964 (sio.close_delay != port->close_delay) ||
965 ((sio.flags & ~ASYNC_USR_MASK) !=
966 (port->flags & ~ASYNC_USR_MASK)))
967 return(-EPERM);
970 port->flags = (port->flags & ~ASYNC_USR_MASK) |
971 (sio.flags & ASYNC_USR_MASK);
973 port->baud_base = sio.baud_base;
974 port->close_delay = sio.close_delay;
975 port->closing_wait = sio.closing_wait;
976 port->custom_divisor = sio.custom_divisor;
978 gs_set_termios (port->tty, NULL);
980 return 0;
984 /*****************************************************************************/
987 * Generate the serial struct info.
990 void gs_getserial(struct gs_port *port, struct serial_struct *sp)
992 struct serial_struct sio;
994 memset(&sio, 0, sizeof(struct serial_struct));
995 sio.flags = port->flags;
996 sio.baud_base = port->baud_base;
997 sio.close_delay = port->close_delay;
998 sio.closing_wait = port->closing_wait;
999 sio.custom_divisor = port->custom_divisor;
1000 sio.hub6 = 0;
1002 /* If you want you can override these. */
1003 sio.type = PORT_UNKNOWN;
1004 sio.xmit_fifo_size = -1;
1005 sio.line = -1;
1006 sio.port = -1;
1007 sio.irq = -1;
1009 if (port->rd->getserial)
1010 port->rd->getserial (port, &sio);
1012 copy_to_user(sp, &sio, sizeof(struct serial_struct));
1016 #ifdef MODULE
1017 int init_module (void)
1019 return 0;
1022 int cleanup_module (void)
1024 return 0;
1026 #endif