ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 #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
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         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
440             tty->ldisc.write_wakeup)
441                 (tty->ldisc.write_wakeup)(tty);
442         func_exit ();
443 }
444
445
446 void gs_flush_chars(struct tty_struct * tty)
447 {
448         struct gs_port *port;
449
450         func_enter ();
451
452         if (!tty) return;
453
454         port = tty->driver_data;
455
456         if (!port) return;
457
458         if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
459             !port->xmit_buf) {
460                 func_exit ();
461                 return;
462         }
463
464         /* Beats me -- REW */
465         port->flags |= GS_TX_INTEN;
466         port->rd->enable_tx_interrupts (port);
467         func_exit ();
468 }
469
470
471 void gs_stop(struct tty_struct * tty)
472 {
473         struct gs_port *port;
474
475         func_enter ();
476
477         if (!tty) return;
478
479         port = tty->driver_data;
480
481         if (!port) return;
482
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);
488         }
489         func_exit ();
490 }
491
492
493 void gs_start(struct tty_struct * tty)
494 {
495         struct gs_port *port;
496
497         if (!tty) return;
498
499         port = tty->driver_data;
500
501         if (!port) return;
502
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);
508         }
509         func_exit ();
510 }
511
512
513 void gs_shutdown_port (struct gs_port *port)
514 {
515         unsigned long flags;
516
517         func_enter();
518         
519         if (!port) return;
520         
521         if (!(port->flags & ASYNC_INITIALIZED))
522                 return;
523
524         save_flags (flags);
525         cli ();
526
527         if (port->xmit_buf) {
528                 free_page((unsigned long) port->xmit_buf);
529                 port->xmit_buf = 0;
530         }
531
532         if (port->tty)
533                 set_bit(TTY_IO_ERROR, &port->tty->flags);
534
535         port->rd->shutdown_port (port);
536
537         port->flags &= ~ASYNC_INITIALIZED;
538         restore_flags (flags);
539
540         func_exit();
541 }
542
543
544 void gs_hangup(struct tty_struct *tty)
545 {
546         struct gs_port   *port;
547
548         func_enter ();
549
550         if (!tty) return;
551
552         port = tty->driver_data;
553         tty = port->tty;
554         if (!tty) 
555                 return;
556
557         gs_shutdown_port (port);
558         port->flags &= ~(ASYNC_NORMAL_ACTIVE|GS_ACTIVE);
559         port->tty = NULL;
560         port->count = 0;
561
562         wake_up_interruptible(&port->open_wait);
563         func_exit ();
564 }
565
566
567 void gs_do_softint(void *private_)
568 {
569         struct gs_port *port = private_;
570         struct tty_struct *tty;
571
572         func_enter ();
573
574         if (!port) return;
575
576         tty = port->tty;
577
578         if (!tty) return;
579
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);
585         }
586         func_exit ();
587 }
588
589
590 int gs_block_til_ready(void *port_, struct file * filp)
591 {
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;
598
599         func_enter ();
600
601         if (!port) return 0;
602
603         tty = port->tty;
604
605         if (!tty) return 0;
606
607         gs_dprintk (GS_DEBUG_BTR, "Entering gs_block_till_ready.\n"); 
608         /*
609          * If the device is in the middle of being closed, then block
610          * until it's done, and then try again.
611          */
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;
618         }
619
620         gs_dprintk (GS_DEBUG_BTR, "after hung up\n"); 
621
622         /*
623          * If non-blocking mode is set, or the port is not enabled,
624          * then make the check up front and then exit.
625          */
626         if ((filp->f_flags & O_NONBLOCK) ||
627             (tty->flags & (1 << TTY_IO_ERROR))) {
628                 port->flags |= ASYNC_NORMAL_ACTIVE;
629                 return 0;
630         }
631
632         gs_dprintk (GS_DEBUG_BTR, "after nonblock\n"); 
633  
634         if (C_CLOCAL(tty))
635                 do_clocal = 1;
636
637         /*
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.
643          */
644         retval = 0;
645
646         add_wait_queue(&port->open_wait, &wait);
647
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;
665                 }
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;
674                 }
675                 schedule();
676         }
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;
686
687         port->flags |= ASYNC_NORMAL_ACTIVE;
688         func_exit ();
689         return 0;
690 }                        
691
692
693 void gs_close(struct tty_struct * tty, struct file * filp)
694 {
695         unsigned long flags;
696         struct gs_port *port;
697
698         func_enter ();
699
700         if (!tty) return;
701
702         port = (struct gs_port *) tty->driver_data;
703
704         if (!port) return;
705
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;
710         }
711
712         save_flags(flags); cli();
713
714         if (tty_hung_up_p(filp)) {
715                 restore_flags(flags);
716                 port->rd->hungup (port);
717                 func_exit ();
718                 return;
719         }
720
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;
725         }
726         if (--port->count < 0) {
727                 printk(KERN_ERR "gs: gs_close: bad port count: %d\n", port->count);
728                 port->count = 0;
729         }
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;
735         }
736         port->flags |= ASYNC_CLOSING;
737
738         /*
739          * Now we wait for the transmit buffer to clear; and we notify 
740          * the line discipline to only process XON/XOFF characters.
741          */
742         tty->closing = 1;
743         /* if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
744            tty_wait_until_sent(tty, port->closing_wait); */
745
746         /*
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.
751          */
752
753         port->rd->disable_rx_interrupts (port);
754
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); 
758
759         port->flags &= ~GS_ACTIVE;
760
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;
766
767         port->event = 0;
768         port->rd->close (port);
769         port->rd->shutdown_port (port);
770         port->tty = 0;
771
772         if (port->blocked_open) {
773                 if (port->close_delay) {
774                         set_current_state (TASK_INTERRUPTIBLE);
775                         schedule_timeout(port->close_delay);
776                 }
777                 wake_up_interruptible(&port->open_wait);
778         }
779         port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING | ASYNC_INITIALIZED);
780         wake_up_interruptible(&port->close_wait);
781
782         restore_flags(flags);
783         func_exit ();
784 }
785
786
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
790 };
791
792
793 void gs_set_termios (struct tty_struct * tty, 
794                      struct termios * old_termios)
795 {
796         struct gs_port *port;
797         int baudrate, tmp, rv;
798         struct termios *tiosp;
799
800         func_enter();
801
802         if (!tty) return;
803
804         port = tty->driver_data;
805
806         if (!port) return;
807
808         tiosp = tty->termios;
809
810         if (gs_debug & GS_DEBUG_TERMIOS) {
811                 gs_dprintk (GS_DEBUG_TERMIOS, "termios structure (%p):\n", tiosp);
812         }
813
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 */;
828                 }
829         } else 
830                 gs_dprintk(GS_DEBUG_TERMIOS, "gs_set_termios: no old_termios: "
831                            "no optimization\n");
832
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");
840         }
841
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;
849         }
850
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);
863         }
864
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;
871
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;                  
875
876         if (tmp <                 0) tmp = 0;
877         if (tmp >= SERIAL_XMIT_SIZE) tmp = SERIAL_XMIT_SIZE-1;
878
879         port->wakeup_chars = tmp;
880
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 */;
885
886         rv = port->rd->set_real_termios(port);
887         if (rv < 0) return /* rv */;
888
889         if ((!old_termios || 
890              (old_termios->c_cflag & CRTSCTS)) &&
891             !(      tiosp->c_cflag & CRTSCTS)) {
892                 tty->stopped = 0;
893                 gs_start(tty);
894         }
895
896 #ifdef tytso_patch_94Nov25_1726
897         /* This "makes sense", Why is it commented out? */
898
899         if (!(old_termios->c_cflag & CLOCAL) &&
900             (tty->termios->c_cflag & CLOCAL))
901                 wake_up_interruptible(&info->open_wait);
902 #endif
903
904         func_exit();
905         return /* 0 */;
906 }
907
908
909
910 /* Must be called with interrupts enabled */
911 int gs_init_port(struct gs_port *port)
912 {
913         unsigned long flags;
914         unsigned long page;
915
916         save_flags (flags);
917         if (!tmp_buf) {
918                 page = get_zeroed_page(GFP_KERNEL);
919
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);
926
927                 if (!tmp_buf) {
928                         return -ENOMEM;
929                 }
930         }
931
932         if (port->flags & ASYNC_INITIALIZED)
933                 return 0;
934
935         if (!port->xmit_buf) {
936                 /* We may sleep in get_zeroed_page() */
937                 unsigned long tmp;
938
939                 tmp = get_zeroed_page(GFP_KERNEL);
940
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);
948
949                 if (!port->xmit_buf)
950                         return -ENOMEM;
951         }
952
953         cli();
954
955         if (port->tty) 
956                 clear_bit(TTY_IO_ERROR, &port->tty->flags);
957
958         port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
959
960         gs_set_termios(port->tty, NULL);
961
962         port->flags |= ASYNC_INITIALIZED;
963         port->flags &= ~GS_TX_INTEN;
964
965         restore_flags(flags);
966         return 0;
967 }
968
969
970 int gs_setserial(struct gs_port *port, struct serial_struct *sp)
971 {
972         struct serial_struct sio;
973
974         if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
975                 return(-EFAULT);
976
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);
983         } 
984
985         port->flags = (port->flags & ~ASYNC_USR_MASK) |
986                 (sio.flags & ASYNC_USR_MASK);
987   
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;
992
993         gs_set_termios (port->tty, NULL);
994
995         return 0;
996 }
997
998
999 /*****************************************************************************/
1000
1001 /*
1002  *      Generate the serial struct info.
1003  */
1004
1005 int gs_getserial(struct gs_port *port, struct serial_struct *sp)
1006 {
1007         struct serial_struct    sio;
1008
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;
1016
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;
1023
1024         if (port->rd->getserial)
1025                 port->rd->getserial (port, &sio);
1026
1027         if (copy_to_user(sp, &sio, sizeof(struct serial_struct)))
1028                 return -EFAULT;
1029         return 0;
1030
1031 }
1032
1033
1034 void gs_got_break(struct gs_port *port)
1035 {
1036         if (port->flags & ASYNC_SAK) {
1037                 do_SAK (port->tty);
1038         }
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++;
1043 }
1044
1045
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);
1063
1064 MODULE_LICENSE("GPL");