vserver 1.9.3
[linux-2.6.git] / drivers / char / generic_serial.c
1 /*
2  *  generic_serial.c
3  *
4  *  Copyright (C) 1998/1999 R.E.Wolff@BitWizard.nl
5  *
6  *  written for the SX serial driver.
7  *     Contains the code that should be shared over all the serial drivers.
8  *
9  *  Credit for the idea to do it this way might go to Alan Cox. 
10  *
11  *
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.
15  *
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  * */
21
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>
31
32 #define DEBUG 
33
34 static char *                  tmp_buf; 
35 static DECLARE_MUTEX(tmp_buf_sem);
36
37 static int gs_debug;
38
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
44
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__)
47
48 #ifdef 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
57
58 #define RS_EVENT_WRITE_WAKEUP   1
59
60 MODULE_PARM(gs_debug, "i");
61
62
63 void gs_put_char(struct tty_struct * tty, unsigned char ch)
64 {
65         struct gs_port *port;
66         DECL
67
68         func_enter (); 
69
70         if (!tty) return;
71
72         port = tty->driver_data;
73
74         if (!port) return;
75
76         if (! (port->flags & ASYNC_INITIALIZED)) return;
77
78         /* Take a lock on the serial tranmit buffer! */
79         LOCKIT;
80
81         if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
82                 /* Sorry, buffer is full, drop character. Update statistics???? -- REW */
83                 RELEASEIT;
84                 return;
85         }
86
87         port->xmit_buf[port->xmit_head++] = ch;
88         port->xmit_head &= SERIAL_XMIT_SIZE - 1;
89         port->xmit_cnt++;  /* Characters in buffer */
90
91         RELEASEIT;
92         func_exit ();
93 }
94
95
96 #ifdef NEW_WRITE_LOCKING
97
98 /*
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". 
103 */
104
105 int gs_write(struct tty_struct * tty, int from_user, 
106                     const unsigned char *buf, int count)
107 {
108         struct gs_port *port;
109         int c, total = 0;
110         int t;
111
112         func_enter ();
113
114         if (!tty) return 0;
115
116         port = tty->driver_data;
117
118         if (!port) return 0;
119
120         if (! (port->flags & ASYNC_INITIALIZED))
121                 return 0;
122
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);
127
128         while (1) {
129
130                 c = count;
131  
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;
137  
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;
143  
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;
151                         }
152
153                 } else
154                         memcpy (port->xmit_buf + port->xmit_head, buf, c);
155
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;
161         }
162         up (& port->port_write_sem);
163
164         gs_dprintk (GS_DEBUG_WRITE, "write: interrupts are %s\n", 
165                     (port->flags & GS_TX_INTEN)?"enabled": "disabled"); 
166
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);
173         }
174         func_exit ();
175         return total;
176 }
177 #else
178 /*
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". 
183 */
184
185 int gs_write(struct tty_struct * tty, int from_user, 
186                     const unsigned char *buf, int count)
187 {
188         struct gs_port *port;
189         int c, total = 0;
190         int t;
191         unsigned long flags;
192
193         func_enter ();
194
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:
199
200                  for (nwritten = 0;nwritten < n;nwritten += rv) {
201                          rv = write (fd, buf+nwritten, n-nwritten);
202                          if (rv < 0) break; // Error: bail out. //
203                  } 
204
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... 
209          */
210
211         if (!tty) return -EIO;
212
213         port = tty->driver_data;
214         if (!port || !port->xmit_buf || !tmp_buf)
215                 return -EIO;
216
217         save_flags(flags);
218         if (from_user) {
219                 down(&tmp_buf_sem);
220                 while (1) {
221                         c = count;
222
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;
228  
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;        
234
235                         /* Can't copy more? break out! */
236                         if (c <= 0) break;
237
238                         c -= copy_from_user(tmp_buf, buf, c);
239                         if (!c) {
240                                 if (!total)
241                                         total = -EFAULT;
242                                 break;
243                         }
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;
249
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;
258                 }
259                 up(&tmp_buf_sem);
260         } else {
261                 while (1) {
262                         cli();
263                         c = count;
264
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;
270  
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;
276  
277                         /* Can't copy more? break out! */
278                         if (c <= 0) {
279                                 restore_flags(flags);
280                                 break;
281                         }
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;
290                 }
291         }
292
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);
299         }
300         func_exit ();
301         return total;
302 }
303
304 #endif
305
306
307
308 int gs_write_room(struct tty_struct * tty)
309 {
310         struct gs_port *port = tty->driver_data;
311         int ret;
312
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;
319 }
320
321
322 int gs_chars_in_buffer(struct tty_struct *tty)
323 {
324         struct gs_port *port = tty->driver_data;
325         func_enter ();
326
327         func_exit ();
328         return port->xmit_cnt;
329 }
330
331
332 int gs_real_chars_in_buffer(struct tty_struct *tty)
333 {
334         struct gs_port *port;
335         func_enter ();
336
337         if (!tty) return 0;
338         port = tty->driver_data;
339
340         if (!port->rd) return 0;
341         if (!port->rd->chars_in_buffer) return 0;
342
343         func_exit ();
344         return port->xmit_cnt + port->rd->chars_in_buffer (port);
345 }
346
347
348 static int gs_wait_tx_flushed (void * ptr, int timeout) 
349 {
350         struct gs_port *port = ptr;
351         unsigned long end_jiffies;
352         int jiffies_to_transmit, charsleft = 0, rv = 0;
353         int rcib;
354
355         func_enter();
356
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);
361         }
362
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. */
367         }
368
369         rcib = gs_real_chars_in_buffer(port->tty);
370
371         if(rcib <= 0) {
372                 gs_dprintk (GS_DEBUG_FLUSH, "nothing to wait for.\n");
373                 func_exit();
374                 return rv;
375         }
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;
382
383         gs_dprintk (GS_DEBUG_FLUSH, "now=%lx, end=%lx (%ld).\n", 
384                     jiffies, end_jiffies, end_jiffies-jiffies); 
385
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! */
393
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;
398
399                 gs_dprintk (GS_DEBUG_FLUSH, "Expect to finish in %d jiffies "
400                             "(%d chars).\n", jiffies_to_transmit, charsleft); 
401
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;
408                 }
409         }
410
411         gs_dprintk (GS_DEBUG_FLUSH, "charsleft = %d.\n", charsleft); 
412         set_current_state (TASK_RUNNING);
413
414         func_exit();
415         return rv;
416 }
417
418
419
420 void gs_flush_buffer(struct tty_struct *tty)
421 {
422         struct gs_port *port;
423         unsigned long flags;
424
425         func_enter ();
426
427         if (!tty) return;
428
429         port = tty->driver_data;
430
431         if (!port) return;
432
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);
437
438         wake_up_interruptible(&tty->write_wait);
439         tty_wakeup(tty);
440         func_exit ();
441 }
442
443
444 void gs_flush_chars(struct tty_struct * tty)
445 {
446         struct gs_port *port;
447
448         func_enter ();
449
450         if (!tty) return;
451
452         port = tty->driver_data;
453
454         if (!port) return;
455
456         if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
457             !port->xmit_buf) {
458                 func_exit ();
459                 return;
460         }
461
462         /* Beats me -- REW */
463         port->flags |= GS_TX_INTEN;
464         port->rd->enable_tx_interrupts (port);
465         func_exit ();
466 }
467
468
469 void gs_stop(struct tty_struct * tty)
470 {
471         struct gs_port *port;
472
473         func_enter ();
474
475         if (!tty) return;
476
477         port = tty->driver_data;
478
479         if (!port) return;
480
481         if (port->xmit_cnt && 
482             port->xmit_buf && 
483             (port->flags & GS_TX_INTEN) ) {
484                 port->flags &= ~GS_TX_INTEN;
485                 port->rd->disable_tx_interrupts (port);
486         }
487         func_exit ();
488 }
489
490
491 void gs_start(struct tty_struct * tty)
492 {
493         struct gs_port *port;
494
495         if (!tty) return;
496
497         port = tty->driver_data;
498
499         if (!port) return;
500
501         if (port->xmit_cnt && 
502             port->xmit_buf && 
503             !(port->flags & GS_TX_INTEN) ) {
504                 port->flags |= GS_TX_INTEN;
505                 port->rd->enable_tx_interrupts (port);
506         }
507         func_exit ();
508 }
509
510
511 void gs_shutdown_port (struct gs_port *port)
512 {
513         unsigned long flags;
514
515         func_enter();
516         
517         if (!port) return;
518         
519         if (!(port->flags & ASYNC_INITIALIZED))
520                 return;
521
522         save_flags (flags);
523         cli ();
524
525         if (port->xmit_buf) {
526                 free_page((unsigned long) port->xmit_buf);
527                 port->xmit_buf = NULL;
528         }
529
530         if (port->tty)
531                 set_bit(TTY_IO_ERROR, &port->tty->flags);
532
533         port->rd->shutdown_port (port);
534
535         port->flags &= ~ASYNC_INITIALIZED;
536         restore_flags (flags);
537
538         func_exit();
539 }
540
541
542 void gs_hangup(struct tty_struct *tty)
543 {
544         struct gs_port   *port;
545
546         func_enter ();
547
548         if (!tty) return;
549
550         port = tty->driver_data;
551         tty = port->tty;
552         if (!tty) 
553                 return;
554
555         gs_shutdown_port (port);
556         port->flags &= ~(ASYNC_NORMAL_ACTIVE|GS_ACTIVE);
557         port->tty = NULL;
558         port->count = 0;
559
560         wake_up_interruptible(&port->open_wait);
561         func_exit ();
562 }
563
564
565 void gs_do_softint(void *private_)
566 {
567         struct gs_port *port = private_;
568         struct tty_struct *tty;
569
570         func_enter ();
571
572         if (!port) return;
573
574         tty = port->tty;
575
576         if (!tty) return;
577
578         if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &port->event)) {
579                 tty_wakeup(tty);
580                 wake_up_interruptible(&tty->write_wait);
581         }
582         func_exit ();
583 }
584
585
586 int gs_block_til_ready(void *port_, struct file * filp)
587 {
588         struct gs_port *port = port_;
589         DECLARE_WAITQUEUE(wait, current);
590         int    retval;
591         int    do_clocal = 0;
592         int    CD;
593         struct tty_struct *tty;
594
595         func_enter ();
596
597         if (!port) return 0;
598
599         tty = port->tty;
600
601         if (!tty) return 0;
602
603         gs_dprintk (GS_DEBUG_BTR, "Entering gs_block_till_ready.\n"); 
604         /*
605          * If the device is in the middle of being closed, then block
606          * until it's done, and then try again.
607          */
608         if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
609                 interruptible_sleep_on(&port->close_wait);
610                 if (port->flags & ASYNC_HUP_NOTIFY)
611                         return -EAGAIN;
612                 else
613                         return -ERESTARTSYS;
614         }
615
616         gs_dprintk (GS_DEBUG_BTR, "after hung up\n"); 
617
618         /*
619          * If non-blocking mode is set, or the port is not enabled,
620          * then make the check up front and then exit.
621          */
622         if ((filp->f_flags & O_NONBLOCK) ||
623             (tty->flags & (1 << TTY_IO_ERROR))) {
624                 port->flags |= ASYNC_NORMAL_ACTIVE;
625                 return 0;
626         }
627
628         gs_dprintk (GS_DEBUG_BTR, "after nonblock\n"); 
629  
630         if (C_CLOCAL(tty))
631                 do_clocal = 1;
632
633         /*
634          * Block waiting for the carrier detect and the line to become
635          * free (i.e., not in use by the callout).  While we are in
636          * this loop, port->count is dropped by one, so that
637          * rs_close() knows when to free things.  We restore it upon
638          * exit, either normal or abnormal.
639          */
640         retval = 0;
641
642         add_wait_queue(&port->open_wait, &wait);
643
644         gs_dprintk (GS_DEBUG_BTR, "after add waitq.\n"); 
645         cli();
646         if (!tty_hung_up_p(filp))
647                 port->count--;
648         sti();
649         port->blocked_open++;
650         while (1) {
651                 CD = port->rd->get_CD (port);
652                 gs_dprintk (GS_DEBUG_BTR, "CD is now %d.\n", CD);
653                 set_current_state (TASK_INTERRUPTIBLE);
654                 if (tty_hung_up_p(filp) ||
655                     !(port->flags & ASYNC_INITIALIZED)) {
656                         if (port->flags & ASYNC_HUP_NOTIFY)
657                                 retval = -EAGAIN;
658                         else
659                                 retval = -ERESTARTSYS;
660                         break;
661                 }
662                 if (!(port->flags & ASYNC_CLOSING) &&
663                     (do_clocal || CD))
664                         break;
665                 gs_dprintk (GS_DEBUG_BTR, "signal_pending is now: %d (%lx)\n", 
666                 (int)signal_pending (current), *(long*)(&current->blocked)); 
667                 if (signal_pending(current)) {
668                         retval = -ERESTARTSYS;
669                         break;
670                 }
671                 schedule();
672         }
673         gs_dprintk (GS_DEBUG_BTR, "Got out of the loop. (%d)\n",
674                     port->blocked_open);
675         set_current_state (TASK_RUNNING);
676         remove_wait_queue(&port->open_wait, &wait);
677         if (!tty_hung_up_p(filp))
678                 port->count++;
679         port->blocked_open--;
680         if (retval)
681                 return retval;
682
683         port->flags |= ASYNC_NORMAL_ACTIVE;
684         func_exit ();
685         return 0;
686 }                        
687
688
689 void gs_close(struct tty_struct * tty, struct file * filp)
690 {
691         unsigned long flags;
692         struct gs_port *port;
693         
694         func_enter ();
695
696         if (!tty) return;
697
698         port = (struct gs_port *) tty->driver_data;
699
700         if (!port) return;
701
702         if (!port->tty) {
703                 /* This seems to happen when this is called from vhangup. */
704                 gs_dprintk (GS_DEBUG_CLOSE, "gs: Odd: port->tty is NULL\n");
705                 port->tty = tty;
706         }
707
708         save_flags(flags); cli();
709
710         if (tty_hung_up_p(filp)) {
711                 restore_flags(flags);
712                 port->rd->hungup (port);
713                 func_exit ();
714                 return;
715         }
716
717         if ((tty->count == 1) && (port->count != 1)) {
718                 printk(KERN_ERR "gs: gs_close: bad port count;"
719                        " tty->count is 1, port count is %d\n", port->count);
720                 port->count = 1;
721         }
722         if (--port->count < 0) {
723                 printk(KERN_ERR "gs: gs_close: bad port count: %d\n", port->count);
724                 port->count = 0;
725         }
726         if (port->count) {
727                 gs_dprintk(GS_DEBUG_CLOSE, "gs_close: count: %d\n", port->count);
728                 restore_flags(flags);
729                 func_exit ();
730                 return;
731         }
732         port->flags |= ASYNC_CLOSING;
733
734         /*
735          * Now we wait for the transmit buffer to clear; and we notify 
736          * the line discipline to only process XON/XOFF characters.
737          */
738         tty->closing = 1;
739         /* if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
740            tty_wait_until_sent(tty, port->closing_wait); */
741
742         /*
743          * At this point we stop accepting input.  To do this, we
744          * disable the receive line status interrupts, and tell the
745          * interrupt driver to stop checking the data ready bit in the
746          * line status register.
747          */
748
749         port->rd->disable_rx_interrupts (port);
750
751         /* close has no way of returning "EINTR", so discard return value */
752         if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
753                 gs_wait_tx_flushed (port, port->closing_wait); 
754
755         port->flags &= ~GS_ACTIVE;
756
757         if (tty->driver->flush_buffer)
758                 tty->driver->flush_buffer(tty);
759                 
760         tty_ldisc_flush(tty);
761         tty->closing = 0;
762
763         port->event = 0;
764         port->rd->close (port);
765         port->rd->shutdown_port (port);
766         port->tty = NULL;
767
768         if (port->blocked_open) {
769                 if (port->close_delay) {
770                         set_current_state (TASK_INTERRUPTIBLE);
771                         schedule_timeout(port->close_delay);
772                 }
773                 wake_up_interruptible(&port->open_wait);
774         }
775         port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING | ASYNC_INITIALIZED);
776         wake_up_interruptible(&port->close_wait);
777
778         restore_flags(flags);
779         func_exit ();
780 }
781
782
783 static unsigned int     gs_baudrates[] = {
784   0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
785   9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
786 };
787
788
789 void gs_set_termios (struct tty_struct * tty, 
790                      struct termios * old_termios)
791 {
792         struct gs_port *port;
793         int baudrate, tmp, rv;
794         struct termios *tiosp;
795
796         func_enter();
797
798         if (!tty) return;
799
800         port = tty->driver_data;
801
802         if (!port) return;
803
804         tiosp = tty->termios;
805
806         if (gs_debug & GS_DEBUG_TERMIOS) {
807                 gs_dprintk (GS_DEBUG_TERMIOS, "termios structure (%p):\n", tiosp);
808         }
809
810 #if 0
811         /* This is an optimization that is only allowed for dumb cards */
812         /* Smart cards require knowledge of iflags and oflags too: that 
813            might change hardware cooking mode.... */
814 #endif
815         if (old_termios) {
816                 if(   (tiosp->c_iflag == old_termios->c_iflag)
817                    && (tiosp->c_oflag == old_termios->c_oflag)
818                    && (tiosp->c_cflag == old_termios->c_cflag)
819                    && (tiosp->c_lflag == old_termios->c_lflag)
820                    && (tiosp->c_line  == old_termios->c_line)
821                    && (memcmp(tiosp->c_cc, old_termios->c_cc, NCC) == 0)) {
822                         gs_dprintk(GS_DEBUG_TERMIOS, "gs_set_termios: optimized away\n");
823                         return /* 0 */;
824                 }
825         } else 
826                 gs_dprintk(GS_DEBUG_TERMIOS, "gs_set_termios: no old_termios: "
827                            "no optimization\n");
828
829         if(old_termios && (gs_debug & GS_DEBUG_TERMIOS)) {
830                 if(tiosp->c_iflag != old_termios->c_iflag)  printk("c_iflag changed\n");
831                 if(tiosp->c_oflag != old_termios->c_oflag)  printk("c_oflag changed\n");
832                 if(tiosp->c_cflag != old_termios->c_cflag)  printk("c_cflag changed\n");
833                 if(tiosp->c_lflag != old_termios->c_lflag)  printk("c_lflag changed\n");
834                 if(tiosp->c_line  != old_termios->c_line)   printk("c_line changed\n");
835                 if(!memcmp(tiosp->c_cc, old_termios->c_cc, NCC)) printk("c_cc changed\n");
836         }
837
838         baudrate = tiosp->c_cflag & CBAUD;
839         if (baudrate & CBAUDEX) {
840                 baudrate &= ~CBAUDEX;
841                 if ((baudrate < 1) || (baudrate > 4))
842                         tiosp->c_cflag &= ~CBAUDEX;
843                 else
844                         baudrate += 15;
845         }
846
847         baudrate = gs_baudrates[baudrate];
848         if ((tiosp->c_cflag & CBAUD) == B38400) {
849                 if (     (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
850                         baudrate = 57600;
851                 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
852                         baudrate = 115200;
853                 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
854                         baudrate = 230400;
855                 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
856                         baudrate = 460800;
857                 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
858                         baudrate = (port->baud_base / port->custom_divisor);
859         }
860
861         /* I recommend using THIS instead of the mess in termios (and
862            duplicating the above code). Next we should create a clean
863            interface towards this variable. If your card supports arbitrary
864            baud rates, (e.g. CD1400 or 16550 based cards) then everything
865            will be very easy..... */
866         port->baud = baudrate;
867
868         /* Two timer ticks seems enough to wakeup something like SLIP driver */
869         /* Baudrate/10 is cps. Divide by HZ to get chars per tick. */
870         tmp = (baudrate / 10 / HZ) * 2;                  
871
872         if (tmp <                 0) tmp = 0;
873         if (tmp >= SERIAL_XMIT_SIZE) tmp = SERIAL_XMIT_SIZE-1;
874
875         port->wakeup_chars = tmp;
876
877         /* We should really wait for the characters to be all sent before
878            changing the settings. -- CAL */
879         rv = gs_wait_tx_flushed (port, MAX_SCHEDULE_TIMEOUT);
880         if (rv < 0) return /* rv */;
881
882         rv = port->rd->set_real_termios(port);
883         if (rv < 0) return /* rv */;
884
885         if ((!old_termios || 
886              (old_termios->c_cflag & CRTSCTS)) &&
887             !(      tiosp->c_cflag & CRTSCTS)) {
888                 tty->stopped = 0;
889                 gs_start(tty);
890         }
891
892 #ifdef tytso_patch_94Nov25_1726
893         /* This "makes sense", Why is it commented out? */
894
895         if (!(old_termios->c_cflag & CLOCAL) &&
896             (tty->termios->c_cflag & CLOCAL))
897                 wake_up_interruptible(&info->open_wait);
898 #endif
899
900         func_exit();
901         return /* 0 */;
902 }
903
904
905
906 /* Must be called with interrupts enabled */
907 int gs_init_port(struct gs_port *port)
908 {
909         unsigned long flags;
910         unsigned long page;
911
912         save_flags (flags);
913         if (!tmp_buf) {
914                 page = get_zeroed_page(GFP_KERNEL);
915
916                 cli (); /* Don't expect this to make a difference. */ 
917                 if (tmp_buf)
918                         free_page(page);
919                 else
920                         tmp_buf = (unsigned char *) page;
921                 restore_flags (flags);
922
923                 if (!tmp_buf) {
924                         return -ENOMEM;
925                 }
926         }
927
928         if (port->flags & ASYNC_INITIALIZED)
929                 return 0;
930
931         if (!port->xmit_buf) {
932                 /* We may sleep in get_zeroed_page() */
933                 unsigned long tmp;
934
935                 tmp = get_zeroed_page(GFP_KERNEL);
936
937                 /* Spinlock? */
938                 cli ();
939                 if (port->xmit_buf) 
940                         free_page (tmp);
941                 else
942                         port->xmit_buf = (unsigned char *) tmp;
943                 restore_flags (flags);
944
945                 if (!port->xmit_buf)
946                         return -ENOMEM;
947         }
948
949         cli();
950
951         if (port->tty) 
952                 clear_bit(TTY_IO_ERROR, &port->tty->flags);
953
954         port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
955
956         gs_set_termios(port->tty, NULL);
957
958         port->flags |= ASYNC_INITIALIZED;
959         port->flags &= ~GS_TX_INTEN;
960
961         restore_flags(flags);
962         return 0;
963 }
964
965
966 int gs_setserial(struct gs_port *port, struct serial_struct __user *sp)
967 {
968         struct serial_struct sio;
969
970         if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
971                 return(-EFAULT);
972
973         if (!capable(CAP_SYS_ADMIN)) {
974                 if ((sio.baud_base != port->baud_base) ||
975                     (sio.close_delay != port->close_delay) ||
976                     ((sio.flags & ~ASYNC_USR_MASK) !=
977                      (port->flags & ~ASYNC_USR_MASK)))
978                         return(-EPERM);
979         } 
980
981         port->flags = (port->flags & ~ASYNC_USR_MASK) |
982                 (sio.flags & ASYNC_USR_MASK);
983   
984         port->baud_base = sio.baud_base;
985         port->close_delay = sio.close_delay;
986         port->closing_wait = sio.closing_wait;
987         port->custom_divisor = sio.custom_divisor;
988
989         gs_set_termios (port->tty, NULL);
990
991         return 0;
992 }
993
994
995 /*****************************************************************************/
996
997 /*
998  *      Generate the serial struct info.
999  */
1000
1001 int gs_getserial(struct gs_port *port, struct serial_struct __user *sp)
1002 {
1003         struct serial_struct    sio;
1004
1005         memset(&sio, 0, sizeof(struct serial_struct));
1006         sio.flags = port->flags;
1007         sio.baud_base = port->baud_base;
1008         sio.close_delay = port->close_delay;
1009         sio.closing_wait = port->closing_wait;
1010         sio.custom_divisor = port->custom_divisor;
1011         sio.hub6 = 0;
1012
1013         /* If you want you can override these. */
1014         sio.type = PORT_UNKNOWN;
1015         sio.xmit_fifo_size = -1;
1016         sio.line = -1;
1017         sio.port = -1;
1018         sio.irq = -1;
1019
1020         if (port->rd->getserial)
1021                 port->rd->getserial (port, &sio);
1022
1023         if (copy_to_user(sp, &sio, sizeof(struct serial_struct)))
1024                 return -EFAULT;
1025         return 0;
1026
1027 }
1028
1029
1030 void gs_got_break(struct gs_port *port)
1031 {
1032         if (port->flags & ASYNC_SAK) {
1033                 do_SAK (port->tty);
1034         }
1035         *(port->tty->flip.flag_buf_ptr) = TTY_BREAK;
1036         port->tty->flip.flag_buf_ptr++;
1037         port->tty->flip.char_buf_ptr++;
1038         port->tty->flip.count++;
1039 }
1040
1041
1042 EXPORT_SYMBOL(gs_put_char);
1043 EXPORT_SYMBOL(gs_write);
1044 EXPORT_SYMBOL(gs_write_room);
1045 EXPORT_SYMBOL(gs_chars_in_buffer);
1046 EXPORT_SYMBOL(gs_flush_buffer);
1047 EXPORT_SYMBOL(gs_flush_chars);
1048 EXPORT_SYMBOL(gs_stop);
1049 EXPORT_SYMBOL(gs_start);
1050 EXPORT_SYMBOL(gs_hangup);
1051 EXPORT_SYMBOL(gs_do_softint);
1052 EXPORT_SYMBOL(gs_block_til_ready);
1053 EXPORT_SYMBOL(gs_close);
1054 EXPORT_SYMBOL(gs_set_termios);
1055 EXPORT_SYMBOL(gs_init_port);
1056 EXPORT_SYMBOL(gs_setserial);
1057 EXPORT_SYMBOL(gs_getserial);
1058 EXPORT_SYMBOL(gs_got_break);
1059
1060 MODULE_LICENSE("GPL");