~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/char/generic_serial.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 <asm/semaphore.h>
 29 #include <asm/uaccess.h>
 30 
 31 #define DEBUG 
 32 
 33 static char *                  tmp_buf; 
 34 static DECLARE_MUTEX(tmp_buf_sem);
 35 
 36 static int gs_debug;
 37 
 38 #ifdef DEBUG
 39 #define gs_dprintk(f, str...) if (gs_debug & f) printk (str)
 40 #else
 41 #define gs_dprintk(f, str...) /* nothing */
 42 #endif
 43 
 44 #define func_enter() gs_dprintk (GS_DEBUG_FLOW, "gs: enter " __FUNCTION__ "\n")
 45 #define func_exit()  gs_dprintk (GS_DEBUG_FLOW, "gs: exit  " __FUNCTION__ "\n")
 46 
 47 #if NEW_WRITE_LOCKING
 48 #define DECL      /* Nothing */
 49 #define LOCKIT    down (& port->port_write_sem);
 50 #define RELEASEIT up (&port->port_write_sem);
 51 #else
 52 #define DECL      unsigned long flags;
 53 #define LOCKIT    save_flags (flags);cli ()
 54 #define RELEASEIT restore_flags (flags)
 55 #endif
 56 
 57 #define RS_EVENT_WRITE_WAKEUP   1
 58 
 59 MODULE_PARM(gs_debug, "i");
 60 
 61 
 62 void gs_put_char(struct tty_struct * tty, unsigned char ch)
 63 {
 64         struct gs_port *port;
 65         DECL
 66 
 67         func_enter (); 
 68 
 69         if (!tty) return;
 70 
 71         port = tty->driver_data;
 72 
 73         if (!port) return;
 74 
 75         if (! (port->flags & ASYNC_INITIALIZED)) return;
 76 
 77         /* Take a lock on the serial tranmit buffer! */
 78         LOCKIT;
 79 
 80         if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
 81                 /* Sorry, buffer is full, drop character. Update statistics???? -- REW */
 82                 RELEASEIT;
 83                 return;
 84         }
 85 
 86         port->xmit_buf[port->xmit_head++] = ch;
 87         port->xmit_head &= SERIAL_XMIT_SIZE - 1;
 88         port->xmit_cnt++;  /* Characters in buffer */
 89 
 90         RELEASEIT;
 91         func_exit ();
 92 }
 93 
 94 
 95 #ifdef NEW_WRITE_LOCKING
 96 
 97 /*
 98 > Problems to take into account are:
 99 >       -1- Interrupts that empty part of the buffer.
100 >       -2- page faults on the access to userspace. 
101 >       -3- Other processes that are also trying to do a "write". 
102 */
103 
104 int gs_write(struct tty_struct * tty, int from_user, 
105                     const unsigned char *buf, int count)
106 {
107         struct gs_port *port;
108         int c, total = 0;
109         int t;
110 
111         func_enter ();
112 
113         if (!tty) return 0;
114 
115         port = tty->driver;
116 
117         if (!port) return 0;
118 
119         if (! (port->flags & ASYNC_INITIALIZED))
120                 return 0;
121 
122         /* get exclusive "write" access to this port (problem 3) */
123         /* This is not a spinlock because we can have a disk access (page 
124                  fault) in copy_from_user */
125         down (& port->port_write_sem);
126 
127         while (1) {
128 
129                 c = count;
130  
131                 /* This is safe because we "OWN" the "head". Noone else can 
132                    change the "head": we own the port_write_sem. */
133                 /* Don't overrun the end of the buffer */
134                 t = SERIAL_XMIT_SIZE - port->xmit_head;
135                 if (t < c) c = t;
136  
137                 /* This is safe because the xmit_cnt can only decrease. This 
138                    would increase "t", so we might copy too little chars. */
139                 /* Don't copy past the "head" of the buffer */
140                 t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
141                 if (t < c) c = t;
142  
143                 /* Can't copy more? break out! */
144                 if (c <= 0) break;
145                 if (from_user)
146                         copy_from_user (port->xmit_buf + port->xmit_head, buf, c);
147                 else
148                         memcpy         (port->xmit_buf + port->xmit_head, buf, c);
149 
150                 port -> xmit_cnt += c;
151                 port -> xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE -1);
152                 buf += c;
153                 count -= c;
154                 total += c;
155         }
156         up (& port->port_write_sem);
157 
158         gs_dprintk (GS_DEBUG_WRITE, "write: interrupts are %s\n", 
159                     (port->flags & GS_TX_INTEN)?"enabled": "disabled"); 
160 
161         if (port->xmit_cnt && 
162             !tty->stopped && 
163             !tty->hw_stopped &&
164             !(port->flags & GS_TX_INTEN)) {
165                 port->flags |= GS_TX_INTEN;
166                 port->rd->enable_tx_interrupts (port);
167         }
168         func_exit ();
169         return total;
170 }
171 #else
172 /*
173 > Problems to take into account are:
174 >       -1- Interrupts that empty part of the buffer.
175 >       -2- page faults on the access to userspace. 
176 >       -3- Other processes that are also trying to do a "write". 
177 */
178 
179 int gs_write(struct tty_struct * tty, int from_user, 
180                     const unsigned char *buf, int count)
181 {
182         struct gs_port *port;
183         int c, total = 0;
184         int t;
185         unsigned long flags;
186 
187         func_enter ();
188 
189         /* The standard serial driver returns 0 in this case. 
190            That sounds to me as "No error, I just didn't get to writing any
191            bytes. Feel free to try again." 
192            The "official" way to write n bytes from buf is:
193 
194                  for (nwritten = 0;nwritten < n;nwritten += rv) {
195                          rv = write (fd, buf+nwritten, n-nwritten);
196                          if (rv < 0) break; // Error: bail out. //
197                  } 
198 
199            which will loop endlessly in this case. The manual page for write
200            agrees with me. In practise almost everybody writes 
201            "write (fd, buf,n);" but some people might have had to deal with 
202            incomplete writes in the past and correctly implemented it by now... 
203          */
204 
205         if (!tty) return -EIO;
206 
207         port = tty->driver_data;
208         if (!port || !port->xmit_buf || !tmp_buf)
209                 return -EIO;
210 
211         save_flags(flags);
212         if (from_user) {
213                 down(&tmp_buf_sem);
214                 while (1) {
215                         c = count;
216 
217                         /* This is safe because we "OWN" the "head". Noone else can 
218                            change the "head": we own the port_write_sem. */
219                         /* Don't overrun the end of the buffer */
220                         t = SERIAL_XMIT_SIZE - port->xmit_head;
221                         if (t < c) c = t;
222  
223                         /* This is safe because the xmit_cnt can only decrease. This 
224                            would increase "t", so we might copy too little chars. */
225                         /* Don't copy past the "head" of the buffer */
226                         t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
227                         if (t < c) c = t;        
228 
229                         /* Can't copy more? break out! */
230                         if (c <= 0) break;
231 
232                         c -= copy_from_user(tmp_buf, buf, c);
233                         if (!c) {
234                                 if (!total)
235                                         total = -EFAULT;
236                                 break;
237                         }
238                         cli();
239                         t = SERIAL_XMIT_SIZE - port->xmit_head;
240                         if (t < c) c = t;
241                         t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
242                         if (t < c) c = t;
243 
244                         memcpy(port->xmit_buf + port->xmit_head, tmp_buf, c);
245                         port->xmit_head = ((port->xmit_head + c) &
246                                            (SERIAL_XMIT_SIZE-1));
247                         port->xmit_cnt += c;
248                         restore_flags(flags);
249                         buf += c;
250                         count -= c;
251                         total += c;
252                 }
253                 up(&tmp_buf_sem);
254         } else {
255                 while (1) {
256                         cli();
257                         c = count;
258 
259                         /* This is safe because we "OWN" the "head". Noone else can 
260                            change the "head": we own the port_write_sem. */
261                         /* Don't overrun the end of the buffer */
262                         t = SERIAL_XMIT_SIZE - port->xmit_head;
263                         if (t < c) c = t;
264  
265                         /* This is safe because the xmit_cnt can only decrease. This 
266                            would increase "t", so we might copy too little chars. */
267                         /* Don't copy past the "head" of the buffer */
268                         t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
269                         if (t < c) c = t;
270  
271                         /* Can't copy more? break out! */
272                         if (c <= 0) {
273                                 restore_flags(flags);
274                                 break;
275                         }
276                         memcpy(port->xmit_buf + port->xmit_head, buf, c);
277                         port->xmit_head = ((port->xmit_head + c) &
278                                            (SERIAL_XMIT_SIZE-1));
279                         port->xmit_cnt += c;
280                         restore_flags(flags);
281                         buf += c;
282                         count -= c;
283                         total += c;
284                 }
285         }
286 
287         if (port->xmit_cnt && 
288             !tty->stopped && 
289             !tty->hw_stopped &&
290             !(port->flags & GS_TX_INTEN)) {
291                 port->flags |= GS_TX_INTEN;
292                 port->rd->enable_tx_interrupts (port);
293         }
294         func_exit ();
295         return total;
296 }
297 
298 #endif
299 
300 
301 
302 int gs_write_room(struct tty_struct * tty)
303 {
304         struct gs_port *port = tty->driver_data;
305         int ret;
306 
307         func_enter ();
308         ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
309         if (ret < 0)
310                 ret = 0;
311         func_exit ();
312         return ret;
313 }
314 
315 
316 int gs_chars_in_buffer(struct tty_struct *tty)
317 {
318         struct gs_port *port = tty->driver_data;
319         func_enter ();
320 
321         func_exit ();
322         return port->xmit_cnt;
323 }
324 
325 
326 int gs_real_chars_in_buffer(struct tty_struct *tty)
327 {
328         struct gs_port *port;
329         func_enter ();
330 
331         if (!tty) return 0;
332         port = tty->driver_data;
333 
334         if (!port->rd) return 0;
335         if (!port->rd->chars_in_buffer) return 0;
336 
337         func_exit ();
338         return port->xmit_cnt + port->rd->chars_in_buffer (port);
339 }
340 
341 
342 static int gs_wait_tx_flushed (void * ptr, int timeout) 
343 {
344         struct gs_port *port = ptr;
345         long end_jiffies;
346         int jiffies_to_transmit, charsleft = 0, rv = 0;
347         int to, rcib;
348 
349         func_enter();
350 
351         gs_dprintk (GS_DEBUG_FLUSH, "port=%p.\n", port);
352         if (port) {
353                 gs_dprintk (GS_DEBUG_FLUSH, "xmit_cnt=%x, xmit_buf=%p, tty=%p.\n", 
354                 port->xmit_cnt, port->xmit_buf, port->tty);
355         }
356 
357         if (!port || port->xmit_cnt < 0 || !port->xmit_buf) {
358                 gs_dprintk (GS_DEBUG_FLUSH, "ERROR: !port, !port->xmit_buf or prot->xmit_cnt < 0.\n");
359                 func_exit();
360                 return -EINVAL;  /* This is an error which we don't know how to handle. */
361         }
362 
363         rcib = gs_real_chars_in_buffer(port->tty);
364 
365         if(rcib <= 0) {
366                 gs_dprintk (GS_DEBUG_FLUSH, "nothing to wait for.\n");
367                 func_exit();
368                 return rv;
369         }
370         /* stop trying: now + twice the time it would normally take +  seconds */
371         end_jiffies  = jiffies; 
372         if (timeout !=  MAX_SCHEDULE_TIMEOUT)
373                 end_jiffies += port->baud?(2 * rcib * 10 * HZ / port->baud):0;
374         end_jiffies += timeout;
375 
376         gs_dprintk (GS_DEBUG_FLUSH, "now=%lx, end=%lx (%ld).\n", 
377                     jiffies, end_jiffies, end_jiffies-jiffies); 
378 
379         to = 100;
380         /* the expression is actually jiffies < end_jiffies, but that won't
381            work around the wraparound. Tricky eh? */
382         while (to-- &&
383                (charsleft = gs_real_chars_in_buffer (port->tty)) &&
384                 time_after (end_jiffies, jiffies)) {
385                 /* Units check: 
386                    chars * (bits/char) * (jiffies /sec) / (bits/sec) = jiffies!
387                    check! */
388 
389                 charsleft += 16; /* Allow 16 chars more to be transmitted ... */
390                 jiffies_to_transmit = port->baud?(1 + charsleft * 10 * HZ / port->baud):0;
391                 /*                                ^^^ Round up.... */
392                 if (jiffies_to_transmit <= 0) jiffies_to_transmit = 1;
393 
394                 gs_dprintk (GS_DEBUG_FLUSH, "Expect to finish in %d jiffies "
395                             "(%d chars).\n", jiffies_to_transmit, charsleft); 
396 
397                 set_current_state (TASK_INTERRUPTIBLE);
398                 schedule_timeout(jiffies_to_transmit);
399                 if (signal_pending (current)) {
400                         gs_dprintk (GS_DEBUG_FLUSH, "Signal pending. Bombing out: "); 
401                         rv = -EINTR;
402                         break;
403                 }
404         }
405 
406         gs_dprintk (GS_DEBUG_FLUSH, "charsleft = %d.\n", charsleft); 
407         set_current_state (TASK_RUNNING);
408 
409         func_exit();
410         return rv;
411 }
412 
413 
414 
415 void gs_flush_buffer(struct tty_struct *tty)
416 {
417         struct gs_port *port;
418         unsigned long flags;
419 
420         func_enter ();
421 
422         if (!tty) return;
423 
424         port = tty->driver_data;
425 
426         if (!port) return;
427 
428         /* XXX Would the write semaphore do? */
429         save_flags(flags); cli();
430         port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
431         restore_flags(flags);
432 
433         wake_up_interruptible(&tty->write_wait);
434         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
435             tty->ldisc.write_wakeup)
436                 (tty->ldisc.write_wakeup)(tty);
437         func_exit ();
438 }
439 
440 
441 void gs_flush_chars(struct tty_struct * tty)
442 {
443         struct gs_port *port;
444 
445         func_enter ();
446 
447         if (!tty) return;
448 
449         port = tty->driver_data;
450 
451         if (!port) return;
452 
453         if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
454             !port->xmit_buf) {
455                 func_exit ();
456                 return;
457         }
458 
459         /* Beats me -- REW */
460         port->flags |= GS_TX_INTEN;
461         port->rd->enable_tx_interrupts (port);
462         func_exit ();
463 }
464 
465 
466 void gs_stop(struct tty_struct * tty)
467 {
468         struct gs_port *port;
469 
470         func_enter ();
471 
472         if (!tty) return;
473 
474         port = tty->driver_data;
475 
476         if (!port) return;
477 
478         if (port->xmit_cnt && 
479             port->xmit_buf && 
480             (port->flags & GS_TX_INTEN) ) {
481                 port->flags &= ~GS_TX_INTEN;
482                 port->rd->disable_tx_interrupts (port);
483         }
484         func_exit ();
485 }
486 
487 
488 void gs_start(struct tty_struct * tty)
489 {
490         struct gs_port *port;
491 
492         if (!tty) return;
493 
494         port = tty->driver_data;
495 
496         if (!port) return;
497 
498         if (port->xmit_cnt && 
499             port->xmit_buf && 
500             !(port->flags & GS_TX_INTEN) ) {
501                 port->flags |= GS_TX_INTEN;
502                 port->rd->enable_tx_interrupts (port);
503         }
504         func_exit ();
505 }
506 
507 
508 void gs_shutdown_port (struct gs_port *port)
509 {
510         long flags;
511 
512         func_enter();
513         
514         if (!port) return;
515         
516         if (!(port->flags & ASYNC_INITIALIZED))
517                 return;
518 
519         save_flags (flags);
520         cli ();
521 
522         if (port->xmit_buf) {
523                 free_page((unsigned long) port->xmit_buf);
524                 port->xmit_buf = 0;
525         }
526 
527         if (port->tty)
528                 set_bit(TTY_IO_ERROR, &port->tty->flags);
529 
530         port->rd->shutdown_port (port);
531 
532         port->flags &= ~ASYNC_INITIALIZED;
533         restore_flags (flags);
534 
535         func_exit();
536 }
537 
538 
539 void gs_hangup(struct tty_struct *tty)
540 {
541         struct gs_port   *port;
542 
543         func_enter ();
544 
545         if (!tty) return;
546 
547         port = tty->driver_data;
548         tty = port->tty;
549         if (!tty) 
550                 return;
551 
552         gs_shutdown_port (port);
553         port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE |GS_ACTIVE);
554         port->tty = NULL;
555         port->count = 0;
556 
557         wake_up_interruptible(&port->open_wait);
558         func_exit ();
559 }
560 
561 
562 void gs_do_softint(void *private_)
563 {
564         struct gs_port *port = private_;
565         struct tty_struct *tty;
566 
567         func_enter ();
568 
569         if (!port) return;
570 
571         tty = port->tty;
572 
573         if (!tty) return;
574 
575         if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &port->event)) {
576                 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
577                     tty->ldisc.write_wakeup)
578                         (tty->ldisc.write_wakeup)(tty);
579                 wake_up_interruptible(&tty->write_wait);
580         }
581         func_exit ();
582 }
583 
584 
585 int gs_block_til_ready(void *port_, struct file * filp)
586 {
587         struct gs_port *port = port_;
588         DECLARE_WAITQUEUE(wait, current);
589         int    retval;
590         int    do_clocal = 0;
591         int    CD;
592         struct tty_struct *tty;
593 
594         func_enter ();
595 
596         if (!port) return 0;
597 
598         tty = port->tty;
599 
600         if (!tty) return 0;
601 
602         gs_dprintk (GS_DEBUG_BTR, "Entering gs_block_till_ready.\n"); 
603         /*
604          * If the device is in the middle of being closed, then block
605          * until it's done, and then try again.
606          */
607         if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
608           interruptible_sleep_on(&port->close_wait);
609                 if (port->flags & ASYNC_HUP_NOTIFY)
610                         return -EAGAIN;
611                 else
612                         return -ERESTARTSYS;
613         }
614 
615         gs_dprintk (GS_DEBUG_BTR, "after hung up\n"); 
616 
617         /*
618          * If this is a callout device, then just make sure the normal
619          * device isn't being used.
620          */
621         if (tty->driver.subtype == GS_TYPE_CALLOUT) {
622                 if (port->flags & ASYNC_NORMAL_ACTIVE)
623                         return -EBUSY;
624                 if ((port->flags & ASYNC_CALLOUT_ACTIVE) &&
625                     (port->flags & ASYNC_SESSION_LOCKOUT) &&
626                     (port->session != current->session))
627                         return -EBUSY;
628                 if ((port->flags & ASYNC_CALLOUT_ACTIVE) &&
629                     (port->flags & ASYNC_PGRP_LOCKOUT) &&
630                     (port->pgrp != current->pgrp))
631                         return -EBUSY;
632                 port->flags |= ASYNC_CALLOUT_ACTIVE;
633                 return 0;
634         }
635 
636         gs_dprintk (GS_DEBUG_BTR, "after subtype\n");
637 
638         /*
639          * If non-blocking mode is set, or the port is not enabled,
640          * then make the check up front and then exit.
641          */
642         if ((filp->f_flags & O_NONBLOCK) ||
643             (tty->flags & (1 << TTY_IO_ERROR))) {
644                 if (port->flags & ASYNC_CALLOUT_ACTIVE)
645                         return -EBUSY;
646                 port->flags |= ASYNC_NORMAL_ACTIVE;
647                 return 0;
648         }
649 
650         gs_dprintk (GS_DEBUG_BTR, "after nonblock\n"); 
651  
652         if (port->flags & ASYNC_CALLOUT_ACTIVE) {
653                 if (port->normal_termios.c_cflag & CLOCAL) 
654                         do_clocal = 1;
655         } else {
656                 if (C_CLOCAL(tty))
657                         do_clocal = 1;
658         }
659 
660         /*
661          * Block waiting for the carrier detect and the line to become
662          * free (i.e., not in use by the callout).  While we are in
663          * this loop, port->count is dropped by one, so that
664          * rs_close() knows when to free things.  We restore it upon
665          * exit, either normal or abnormal.
666          */
667         retval = 0;
668 
669         add_wait_queue(&port->open_wait, &wait);
670 
671         gs_dprintk (GS_DEBUG_BTR, "after add waitq.\n"); 
672         cli();
673         if (!tty_hung_up_p(filp))
674                 port->count--;
675         sti();
676         port->blocked_open++;
677         while (1) {
678                 CD = port->rd->get_CD (port);
679                 gs_dprintk (GS_DEBUG_BTR, "CD is now %d.\n", CD);
680                 set_current_state (TASK_INTERRUPTIBLE);
681                 if (tty_hung_up_p(filp) ||
682                     !(port->flags & ASYNC_INITIALIZED)) {
683                         if (port->flags & ASYNC_HUP_NOTIFY)
684                                 retval = -EAGAIN;
685                         else
686                                 retval = -ERESTARTSYS;
687                         break;
688                 }
689                 if (!(port->flags & ASYNC_CALLOUT_ACTIVE) &&
690                     !(port->flags & ASYNC_CLOSING) &&
691                     (do_clocal || CD))
692                         break;
693                 gs_dprintk (GS_DEBUG_BTR, "signal_pending is now: %d (%lx)\n", 
694                 (int)signal_pending (current), *(long*)(&current->blocked)); 
695                 if (signal_pending(current)) {
696                         retval = -ERESTARTSYS;
697                         break;
698                 }
699                 schedule();
700         }
701         gs_dprintk (GS_DEBUG_BTR, "Got out of the loop. (%d)\n",
702                     port->blocked_open);
703         set_current_state (TASK_RUNNING);
704         remove_wait_queue(&port->open_wait, &wait);
705         if (!tty_hung_up_p(filp))
706                 port->count++;
707         port->blocked_open--;
708         if (retval)
709                 return retval;
710 
711         port->flags |= ASYNC_NORMAL_ACTIVE;
712         func_exit ();
713         return 0;
714 }                        
715 
716 
717 void gs_close(struct tty_struct * tty, struct file * filp)
718 {
719         unsigned long flags;
720         struct gs_port *port;
721 
722         func_enter ();
723 
724         if (!tty) return;
725 
726         port = (struct gs_port *) tty->driver_data;
727 
728         if (!port) return;
729 
730         if (!port->tty) {
731                 /* This seems to happen when this is called from vhangup. */
732                 gs_dprintk (GS_DEBUG_CLOSE, "gs: Odd: port->tty is NULL\n");
733                 port->tty = tty;
734         }
735 
736         save_flags(flags); cli();
737 
738         if (tty_hung_up_p(filp)) {
739                 restore_flags(flags);
740                 port->rd->hungup (port);
741                 func_exit ();
742                 return;
743         }
744 
745         if ((tty->count == 1) && (port->count != 1)) {
746                 printk(KERN_ERR "gs: gs_close: bad port count;"
747                        " tty->count is 1, port count is %d\n", port->count);
748                 port->count = 1;
749         }
750         if (--port->count < 0) {
751                 printk(KERN_ERR "gs: gs_close: bad port count: %d\n", port->count);
752                 port->count = 0;
753         }
754         if (port->count) {
755                 gs_dprintk(GS_DEBUG_CLOSE, "gs_close: count: %d\n", port->count);
756                 restore_flags(flags);
757                 func_exit ();
758                 return;
759         }
760         port->flags |= ASYNC_CLOSING;
761 
762         /*
763          * Save the termios structure, since this port may have
764          * separate termios for callout and dialin.
765          */
766         if (port->flags & ASYNC_NORMAL_ACTIVE)
767                 port->normal_termios = *tty->termios;
768         if (port->flags & ASYNC_CALLOUT_ACTIVE)
769                 port->callout_termios = *tty->termios;
770         /*
771          * Now we wait for the transmit buffer to clear; and we notify 
772          * the line discipline to only process XON/XOFF characters.
773          */
774         tty->closing = 1;
775         /* if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
776            tty_wait_until_sent(tty, port->closing_wait); */
777 
778         /*
779          * At this point we stop accepting input.  To do this, we
780          * disable the receive line status interrupts, and tell the
781          * interrupt driver to stop checking the data ready bit in the
782          * line status register.
783          */
784 
785         port->rd->disable_rx_interrupts (port);
786 
787         /* close has no way of returning "EINTR", so discard return value */
788         if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
789                 gs_wait_tx_flushed (port, port->closing_wait); 
790 
791         port->flags &= ~GS_ACTIVE;
792 
793         if (tty->driver.flush_buffer)
794                 tty->driver.flush_buffer(tty);
795         if (tty->ldisc.flush_buffer)
796                 tty->ldisc.flush_buffer(tty);
797         tty->closing = 0;
798 
799         port->event = 0;
800         port->rd->close (port);
801         port->rd->shutdown_port (port);
802         port->tty = 0;
803 
804         if (port->blocked_open) {
805                 if (port->close_delay) {
806                         set_current_state (TASK_INTERRUPTIBLE);
807                         schedule_timeout(port->close_delay);
808                 }
809                 wake_up_interruptible(&port->open_wait);
810         }
811         port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
812                          ASYNC_CLOSING | ASYNC_INITIALIZED);
813         wake_up_interruptible(&port->close_wait);
814 
815         restore_flags(flags);
816         func_exit ();
817 }
818 
819 
820 static unsigned int     gs_baudrates[] = {
821   0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
822   9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
823 };
824 
825 
826 void gs_set_termios (struct tty_struct * tty, 
827                      struct termios * old_termios)
828 {
829         struct gs_port *port;
830         int baudrate, tmp, rv;
831         struct termios *tiosp;
832 
833         func_enter();
834 
835         if (!tty) return;
836 
837         port = tty->driver_data;
838 
839         if (!port) return;
840 
841         tiosp = tty->termios;
842 
843         if (gs_debug & GS_DEBUG_TERMIOS) {
844                 gs_dprintk (GS_DEBUG_TERMIOS, "termios structure (%p):\n", tiosp);
845         }
846 
847 #if 0
848         /* This is an optimization that is only allowed for dumb cards */
849         /* Smart cards require knowledge of iflags and oflags too: that 
850            might change hardware cooking mode.... */
851 #endif
852         if (old_termios) {
853                 if(   (tiosp->c_iflag == old_termios->c_iflag)
854                    && (tiosp->c_oflag == old_termios->c_oflag)
855                    && (tiosp->c_cflag == old_termios->c_cflag)
856                    && (tiosp->c_lflag == old_termios->c_lflag)
857                    && (tiosp->c_line  == old_termios->c_line)
858                    && (memcmp(tiosp->c_cc, old_termios->c_cc, NCC) == 0)) {
859                         gs_dprintk(GS_DEBUG_TERMIOS, "gs_set_termios: optimized away\n");
860                         return /* 0 */;
861                 }
862         } else 
863                 gs_dprintk(GS_DEBUG_TERMIOS, "gs_set_termios: no old_termios: "
864                            "no optimization\n");
865 
866         if(old_termios && (gs_debug & GS_DEBUG_TERMIOS)) {
867                 if(tiosp->c_iflag != old_termios->c_iflag)  printk("c_iflag changed\n");
868                 if(tiosp->c_oflag != old_termios->c_oflag)  printk("c_oflag changed\n");
869                 if(tiosp->c_cflag != old_termios->c_cflag)  printk("c_cflag changed\n");
870                 if(tiosp->c_lflag != old_termios->c_lflag)  printk("c_lflag changed\n");
871                 if(tiosp->c_line  != old_termios->c_line)   printk("c_line changed\n");
872                 if(!memcmp(tiosp->c_cc, old_termios->c_cc, NCC)) printk("c_cc changed\n");
873         }
874 
875         baudrate = tiosp->c_cflag & CBAUD;
876         if (baudrate & CBAUDEX) {
877                 baudrate &= ~CBAUDEX;
878                 if ((baudrate < 1) || (baudrate > 4))
879                         tiosp->c_cflag &= ~CBAUDEX;
880                 else
881                         baudrate += 15;
882         }
883 
884         baudrate = gs_baudrates[baudrate];
885         if ((tiosp->c_cflag & CBAUD) == B38400) {
886                 if (     (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
887                         baudrate = 57600;
888                 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
889                         baudrate = 115200;
890                 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
891                         baudrate = 230400;
892                 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
893                         baudrate = 460800;
894                 else if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
895                         baudrate = (port->baud_base / port->custom_divisor);
896         }
897 
898         /* I recommend using THIS instead of the mess in termios (and
899            duplicating the above code). Next we should create a clean
900            interface towards this variable. If your card supports arbitrary
901            baud rates, (e.g. CD1400 or 16550 based cards) then everything
902            will be very easy..... */
903         port->baud = baudrate;
904 
905         /* Two timer ticks seems enough to wakeup something like SLIP driver */
906         /* Baudrate/10 is cps. Divide by HZ to get chars per tick. */
907         tmp = (baudrate / 10 / HZ) * 2;                  
908 
909         if (tmp <                 0) tmp = 0;
910         if (tmp >= SERIAL_XMIT_SIZE) tmp = SERIAL_XMIT_SIZE-1;
911 
912         port->wakeup_chars = tmp;
913 
914         /* We should really wait for the characters to be all sent before
915            changing the settings. -- CAL */
916         rv = gs_wait_tx_flushed (port, MAX_SCHEDULE_TIMEOUT);
917         if (rv < 0) return /* rv */;
918 
919         rv = port->rd->set_real_termios(port);
920         if (rv < 0) return /* rv */;
921 
922         if ((!old_termios || 
923              (old_termios->c_cflag & CRTSCTS)) &&
924             !(      tiosp->c_cflag & CRTSCTS)) {
925                 tty->stopped = 0;
926                 gs_start(tty);
927         }
928 
929 #ifdef tytso_patch_94Nov25_1726
930         /* This "makes sense", Why is it commented out? */
931 
932         if (!(old_termios->c_cflag & CLOCAL) &&
933             (tty->termios->c_cflag & CLOCAL))
934                 wake_up_interruptible(&info->open_wait);
935 #endif
936 
937         func_exit();
938         return /* 0 */;
939 }
940 
941 
942 
943 /* Must be called with interrupts enabled */
944 int gs_init_port(struct gs_port *port)
945 {
946         unsigned long flags;
947         unsigned long page;
948 
949         save_flags (flags);
950         if (!tmp_buf) {
951                 page = get_free_page(GFP_KERNEL);
952 
953                 cli (); /* Don't expect this to make a difference. */ 
954                 if (tmp_buf)
955                         free_page(page);
956                 else
957                         tmp_buf = (unsigned char *) page;
958                 restore_flags (flags);
959 
960                 if (!tmp_buf) {
961                         return -ENOMEM;
962                 }
963         }
964 
965         if (port->flags & ASYNC_INITIALIZED)
966                 return 0;
967 
968         if (!port->xmit_buf) {
969                 /* We may sleep in get_free_page() */
970                 unsigned long tmp;
971 
972                 tmp = get_free_page(GFP_KERNEL);
973 
974                 /* Spinlock? */
975                 cli ();
976                 if (port->xmit_buf) 
977                         free_page (tmp);
978                 else
979                         port->xmit_buf = (unsigned char *) tmp;
980                 restore_flags (flags);
981 
982                 if (!port->xmit_buf)
983                         return -ENOMEM;
984         }
985 
986         cli();
987 
988         if (port->tty) 
989                 clear_bit(TTY_IO_ERROR, &port->tty->flags);
990 
991         port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
992 
993         gs_set_termios(port->tty, NULL);
994 
995         port->flags |= ASYNC_INITIALIZED;
996         port->flags &= ~GS_TX_INTEN;
997 
998         restore_flags(flags);
999         return 0;
1000 }
1001 
1002 
1003 int gs_setserial(struct gs_port *port, struct serial_struct *sp)
1004 {
1005         struct serial_struct sio;
1006 
1007         copy_from_user(&sio, sp, sizeof(struct serial_struct));
1008 
1009         if (!capable(CAP_SYS_ADMIN)) {
1010                 if ((sio.baud_base != port->baud_base) ||
1011                     (sio.close_delay != port->close_delay) ||
1012                     ((sio.flags & ~ASYNC_USR_MASK) !=
1013                      (port->flags & ~ASYNC_USR_MASK)))
1014                         return(-EPERM);
1015         } 
1016 
1017         port->flags = (port->flags & ~ASYNC_USR_MASK) |
1018                 (sio.flags & ASYNC_USR_MASK);
1019   
1020         port->baud_base = sio.baud_base;
1021         port->close_delay = sio.close_delay;
1022         port->closing_wait = sio.closing_wait;
1023         port->custom_divisor = sio.custom_divisor;
1024 
1025         gs_set_termios (port->tty, NULL);
1026 
1027         return 0;
1028 }
1029 
1030 
1031 /*****************************************************************************/
1032 
1033 /*
1034  *      Generate the serial struct info.
1035  */
1036 
1037 void gs_getserial(struct gs_port *port, struct serial_struct *sp)
1038 {
1039         struct serial_struct    sio;
1040 
1041         memset(&sio, 0, sizeof(struct serial_struct));
1042         sio.flags = port->flags;
1043         sio.baud_base = port->baud_base;
1044         sio.close_delay = port->close_delay;
1045         sio.closing_wait = port->closing_wait;
1046         sio.custom_divisor = port->custom_divisor;
1047         sio.hub6 = 0;
1048 
1049         /* If you want you can override these. */
1050         sio.type = PORT_UNKNOWN;
1051         sio.xmit_fifo_size = -1;
1052         sio.line = -1;
1053         sio.port = -1;
1054         sio.irq = -1;
1055 
1056         if (port->rd->getserial)
1057                 port->rd->getserial (port, &sio);
1058 
1059         copy_to_user(sp, &sio, sizeof(struct serial_struct));
1060 }
1061 
1062 EXPORT_SYMBOL(gs_put_char);
1063 EXPORT_SYMBOL(gs_write);
1064 EXPORT_SYMBOL(gs_write_room);
1065 EXPORT_SYMBOL(gs_chars_in_buffer);
1066 EXPORT_SYMBOL(gs_flush_buffer);
1067 EXPORT_SYMBOL(gs_flush_chars);
1068 EXPORT_SYMBOL(gs_stop);
1069 EXPORT_SYMBOL(gs_start);
1070 EXPORT_SYMBOL(gs_hangup);
1071 EXPORT_SYMBOL(gs_do_softint);
1072 EXPORT_SYMBOL(gs_block_til_ready);
1073 EXPORT_SYMBOL(gs_close);
1074 EXPORT_SYMBOL(gs_set_termios);
1075 EXPORT_SYMBOL(gs_init_port);
1076 EXPORT_SYMBOL(gs_setserial);
1077 EXPORT_SYMBOL(gs_getserial);
1078 
1079 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.