root/drivers/char/scc.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. InReg
  2. OutReg
  3. wr
  4. or
  5. cl
  6. scc_alloc_buffer_pool
  7. scc_count_used_buffers
  8. scc_get_buffer
  9. scc_return_buffer
  10. scc_free_chain
  11. scc_append_to_chain
  12. scc_enqueue
  13. scc_isr_dispatch
  14. scc_isr
  15. prepare_next_txframe
  16. scc_txint
  17. scc_toss_buffer
  18. flush_FIFO
  19. scc_exint
  20. scc_rxint
  21. kick_rx_timer
  22. scc_spint
  23. set_brg
  24. set_speed
  25. init_brg
  26. init_channel
  27. scc_key_trx
  28. is_grouped
  29. dw_slot_timeout
  30. txdel_timeout
  31. tail_timeout
  32. busy_timeout
  33. maxk_idle_timeout
  34. check_rcv_queue
  35. scc_timer
  36. scc_init_timer
  37. scc_rx_timer
  38. kiss_set_param
  39. kiss_interpret_frame
  40. kiss_store_byte
  41. kiss_decode
  42. kiss_encode
  43. z8530_init
  44. scc_paranoia_check
  45. scc_open
  46. scc_close
  47. scc_change_speed
  48. scc_ioctl
  49. scc_set_termios
  50. check_tx_queue
  51. scc_write
  52. scc_put_char
  53. scc_flush_chars
  54. scc_write_room
  55. scc_chars_in_buffer
  56. scc_flush_buffer
  57. scc_throttle
  58. scc_unthrottle
  59. scc_start
  60. scc_stop
  61. scc_init

   1 #define RCS_ID "$Id: scc.c,v 1.26 1995/09/07 14:46:19 jreuter Exp jreuter $"
   2 
   3 #define BANNER "Z8530 SCC driver v1.9.dl1bke (beta) by dl1bke\n"
   4 
   5 /*
   6 
   7    ********************************************************************
   8    *   SCC.C - Linux driver for Z8530 based HDLC cards for AX.25      *
   9    ********************************************************************
  10 
  11 
  12    ********************************************************************
  13 
  14         (c) 1993 - 1995 by Joerg Reuter DL1BKE
  15 
  16         portions (c) 1994 Hans Alblas PE1AYX 
  17         and      (c) 1993 Guido ten Dolle PE1NNZ
  18 
  19    ********************************************************************
  20    
  21    The driver and the programs in this archive are UNDER CONSTRUCTION.
  22    The code is likely to fail, and so your kernel could --- even 
  23    a whole network. 
  24 
  25    This driver is intended for Amateur Radio use. If you are running it
  26    for commercial purposes, please drop me a note. I am nosy...
  27 
  28    ...BUT:
  29  
  30    ! You  m u s t  recognize the appropriate legislations of your country !
  31    ! before you connect a radio to the SCC board and start to transmit or !
  32    ! receive. The GPL allows you to use the  d r i v e r,  NOT the RADIO! !
  33 
  34    For non-Amateur-Radio use please note that you might need a special
  35    allowance/licence from the designer of the SCC Board and/or the
  36    MODEM. 
  37 
  38    This program is free software; you can redistribute it and/or modify 
  39    it under the terms of the (modified) GNU General Public License 
  40    delivered with the LinuX kernel source.
  41    
  42    This program is distributed in the hope that it will be useful,
  43    but WITHOUT ANY WARRANTY; without even the implied warranty of
  44    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  45    GNU General Public License for more details.
  46 
  47    You should find a copy of the GNU General Public License in 
  48    /usr/src/linux/COPYING; 
  49    
  50    ******************************************************************** 
  51 
  52 
  53    ...If you find any portions of the code that are copyrighted to you,
  54    and you don't want to see in here, please send me a private (!) 
  55    message to my internet site. I will change it as soon as possible. 
  56    Please don't flame to the tcp-group or anywhere else. Thanks!
  57 
  58    Joerg Reuter ampr-net: dl1bke@db0pra.ampr.org
  59                 AX-25   : DL1BKE @ DB0ACH.#NRW.DEU.EU
  60                 Internet: jreuter@lykos.tng.oche.de
  61                 
  62                 
  63    History of z8530drv:
  64    --------------------
  65 
  66    940913       - started to write the driver, rescued most of my own
  67                   code (and Hans Alblas' memory buffer pool concept) from 
  68                   an earlier project "sccdrv" which was initiated by 
  69                   Guido ten Dolle. Not much of the old driver survived, 
  70                   though. The first version I put my hands on was sccdrv1.3
  71                   from August 1993. The memory buffer pool concept
  72                   appeared in an unauthorized sccdrv version (1.5) from
  73                   August 1994.
  74 
  75    950131       - changed copyright notice to GPL without limitations.
  76    
  77    950228       - (hopefully) fixed the reason for kernel panics in
  78                   chk_rcv_queue() [stupid error]
  79                   
  80    950304       - fixed underrun/zcount handling
  81    
  82    950305       - the driver registers port addresses now
  83    
  84    950314       - fixed underrun interrupt handling again
  85    
  86    950512       - (hope to have) fixed hidden re-entrance problem
  87                   in scc_timer()
  88                   
  89    950824       - received frames will be sent to the application
  90                   faster, clean-up of z8530_init()
  91 
  92    Thanks to:
  93    ----------
  94    
  95    PE1CHL Rob   - for a lot of good ideas from his SCC driver for DOS
  96    PE1NNZ Guido - for his port of the original driver to Linux
  97    KA9Q   Phil  - from whom we stole the mbuf-structure
  98    PA3AYX Hans  - who rewrote parts of the memory management and some 
  99                   minor, but nevertheless useful changes
 100    DL8MBT Flori - for support
 101    DG0FT  Rene  - for the BayCom USCC support
 102    PA3AOU Harry - for ESCC testing, information supply and support
 103    
 104    PE1KOX Rob, DG1RTF Thomas, ON5QK Roland, 
 105    
 106    and all who sent me bug reports and ideas... 
 107    
 108    
 109    NB -- if you find errors, change something, please let me know
 110          first before you distribute it... And please don't touch
 111          the version number. Just replace my callsign in
 112          "v1.9.dl1bke" with your own. Just to avoid confusion...
 113          
 114    If you want to add your modification to the linux distribution
 115    please (!) contact me first.
 116           
 117    Jörg Reuter DL1BKE
 118   
 119 */
 120 
 121 #include <linux/errno.h>
 122 #include <linux/signal.h>
 123 #include <linux/sched.h>
 124 #include <linux/timer.h>
 125 #include <linux/tqueue.h>
 126 #include <linux/tty.h>
 127 #include <linux/tty_driver.h>
 128 #include <linux/tty_flip.h>
 129 #include <linux/major.h>
 130 #include <linux/termios.h>
 131 #include <linux/serial.h>
 132 #include <linux/interrupt.h>
 133 #include <linux/ioport.h>
 134 #include <linux/string.h>
 135 #include <linux/fcntl.h>
 136 #include <linux/ptrace.h>
 137 #include <linux/malloc.h>
 138 #include <linux/scc.h>
 139 #include <linux/delay.h>
 140 #include "scc_config.h"
 141 
 142 #include <asm/system.h>
 143 #include <asm/io.h>
 144 #include <asm/segment.h>
 145 #include <asm/bitops.h>
 146 
 147 #include <stdlib.h>
 148 #include <stdio.h>
 149 #include <ctype.h>
 150 #include <time.h>
 151 #include <linux/kernel.h>
 152 
 153 #ifndef Z8530_MAJOR
 154 #define Z8530_MAJOR 34
 155 #endif
 156 
 157 int scc_init(void);
 158 
 159 int scc_open(struct tty_struct *tty, struct file *filp);
 160 static void scc_close(struct tty_struct *tty, struct file *filp);
 161 int scc_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count);
 162 static void scc_put_char(struct tty_struct *tty, unsigned char ch);
 163 static void scc_flush_chars(struct tty_struct *tty);
 164 static int scc_write_room(struct tty_struct *tty);
 165 static int scc_chars_in_buffer(struct tty_struct *tty);
 166 static void scc_flush_buffer(struct tty_struct *tty);
 167 static int scc_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
 168 static void scc_set_termios(struct tty_struct *tty, struct termios *old_termios);
 169 static void scc_throttle(struct tty_struct *tty);
 170 static void scc_unthrottle(struct tty_struct *tty);
 171 static void scc_start(struct tty_struct *tty);
 172 static void scc_stop(struct tty_struct *tty);
 173 
 174 static void z8530_init(void);
 175 
 176 static void scc_change_speed(struct scc_channel *scc);
 177 static void kiss_encode(struct scc_channel *scc);
 178 
 179 static void init_channel(struct scc_channel *scc);
 180 static void scc_key_trx (struct scc_channel *scc, char tx);
 181 static void scc_txint(register struct scc_channel *scc);
 182 static void scc_exint(register struct scc_channel *scc);
 183 static void scc_rxint(register struct scc_channel *scc);
 184 static void scc_spint(register struct scc_channel *scc);
 185 static void scc_isr(int irq, struct pt_regs *regs);
 186 static void scc_timer(void);
 187 static void scc_init_timer(struct scc_channel *scc);
 188 static void scc_rx_timer(void);
 189 
 190 /* from serial.c */
 191 
 192 static int baud_table[] = {
 193         0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
 194         9600, 19200, 38400, 57600, 115200, 0 };
 195 
 196 
 197 struct tty_driver scc_driver;
 198 static int scc_refcount;
 199 static struct tty_struct *scc_table[2*MAXSCC];
 200 static struct termios scc_termios[2 * MAXSCC];
 201 static struct termios scc_termios_locked[2 * MAXSCC];
 202         
 203 struct scc_channel SCC_Info[2 * MAXSCC];         /* information per channel */
 204 
 205 unsigned char Random = 0;               /* random number for p-persist */
 206 unsigned char Driver_Initialized = 0;
 207 static struct sccbuf *sccfreelist[MAX_IBUFS] = {0};
 208 static int allocated_ibufs = 0;
 209 
 210 static struct rx_timer_CB rx_timer_cb;
 211 
 212 /* ******************************************************************** */
 213 /* *                    Port Access Functions                         * */
 214 /* ******************************************************************** */
 215 
 216 static inline unsigned char
 217 InReg(register io_port port, register unsigned char reg)
     /* [previous][next][first][last][top][bottom][index][help] */
 218 {
 219 #ifdef SCC_LDELAY
 220         register unsigned char r;
 221         Outb(port, reg);
 222         udelay(5);
 223         r=Inb(port);
 224         udelay(5);
 225         return r;
 226 #else
 227         Outb(port, reg);
 228         return Inb(port);
 229 #endif
 230 }
 231 
 232 static inline void
 233 OutReg(register io_port port, register unsigned char reg, register unsigned char val)
     /* [previous][next][first][last][top][bottom][index][help] */
 234 {
 235 #ifdef SCC_LDELAY
 236         Outb(port, reg); udelay(5);
 237         Outb(port, val); udelay(5);
 238 #else
 239         Outb(port, reg);
 240         Outb(port, val);
 241 #endif
 242 }
 243 
 244 static inline void
 245 wr(register struct scc_channel *scc, register unsigned char reg, register unsigned char val)
     /* [previous][next][first][last][top][bottom][index][help] */
 246 {
 247         OutReg(scc->ctrl, reg, (scc->wreg[reg] = val));
 248 }
 249 
 250 static inline void
 251 or(register struct scc_channel *scc, register unsigned char reg, register unsigned char val)
     /* [previous][next][first][last][top][bottom][index][help] */
 252 {
 253         OutReg(scc->ctrl, reg, (scc->wreg[reg] |= val));
 254 }
 255 
 256 static inline void
 257 cl(register struct scc_channel *scc, register unsigned char reg, register unsigned char val)
     /* [previous][next][first][last][top][bottom][index][help] */
 258 {
 259         OutReg(scc->ctrl, reg, (scc->wreg[reg] &= ~val));
 260 }
 261 
 262 /* ******************************************************************** */
 263 /* *                    Memory Buffer Management                        */
 264 /* ******************************************************************** */
 265 
 266 /* mbuf concept lent from KA9Q. Tnx PE1AYX for the buffer pool concept  */
 267 /* (sorry, do you have any better ideas?) */
 268 
 269 
 270 /* allocate memory for the interrupt buffer pool */
 271 
 272 void scc_alloc_buffer_pool(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 273 {
 274         int i;
 275         struct sccbuf *sccb;
 276         struct mbuf   *bp;
 277         
 278         for (i = 0 ; i < MAX_IBUFS ; i++)
 279         {
 280                 sccb = (struct sccbuf *)kmalloc(sizeof(struct sccbuf), GFP_ATOMIC);
 281                 bp = (struct mbuf *)kmalloc(sizeof(struct mbuf), GFP_ATOMIC);
 282                 
 283                 if ( !(sccb && bp) )
 284                 {
 285                         allocated_ibufs = --i;
 286                         
 287                         if (allocated_ibufs < 0)
 288                                 panic("scc_alloc_buffer_pool() - can't get buffer space");
 289                         else
 290                                 printk("Warning: scc_alloc_buffer_pool() - allocated only %i buffers\n",i);
 291                                 
 292                         return;
 293                 }
 294                 
 295                 sccfreelist[i] = sccb;
 296                 sccfreelist[i]->bp = bp;
 297                 memset(sccfreelist[i]->bp ,0,sizeof(struct mbuf));
 298                 sccfreelist[i]->inuse = 0;
 299                 sccfreelist[i]->bp->type = 0;
 300                 sccfreelist[i]->bp->refcnt = 0;
 301                 sccfreelist[i]->bp->size = BUFSIZE;
 302         }
 303         allocated_ibufs = MAX_IBUFS;
 304 }
 305 
 306 unsigned int scc_count_used_buffers(unsigned int * rx, unsigned int * tx)
     /* [previous][next][first][last][top][bottom][index][help] */
 307 {
 308         unsigned int i, used = 0;
 309         
 310         if (rx) *rx = 0;
 311         if (tx) *tx = 0;
 312         
 313         for (i = 0 ; i < allocated_ibufs ; i++)
 314         {
 315                 if (sccfreelist[i]->inuse)
 316                 {
 317                         switch (sccfreelist[i]->bp->type)
 318                         {
 319                                 case BT_RECEIVE:
 320                                         if (rx) (*rx)++; break;
 321                                 case BT_TRANSMIT:
 322                                         if (tx) (*tx)++; break;
 323                         }
 324                         
 325                         used++;
 326                 }
 327         }
 328         
 329         return used;
 330 }
 331 
 332 
 333 /* Allocate mbuf */
 334 struct mbuf *
 335 scc_get_buffer(char type)
     /* [previous][next][first][last][top][bottom][index][help] */
 336 {
 337         int i;
 338         unsigned long flags;
 339 
 340         save_flags(flags); cli();       /* just to be sure */
 341 
 342         for (i = 0 ; i < allocated_ibufs ; i++)
 343         {
 344                 if(sccfreelist[i]->inuse == 0)
 345                 {
 346                         sccfreelist[i]->inuse = 1;
 347                         sccfreelist[i]->bp->type = type;
 348                         sccfreelist[i]->bp->next = NULLBUF;
 349                         sccfreelist[i]->bp->anext = NULLBUF;
 350                         sccfreelist[i]->bp->dup = NULLBUF;
 351                         sccfreelist[i]->bp->size = BUFSIZE;
 352                         sccfreelist[i]->bp->refcnt = 1;
 353                         sccfreelist[i]->bp->cnt = 0;
 354                         sccfreelist[i]->bp->in_use = 0;
 355                 
 356                         restore_flags(flags);
 357                         return sccfreelist[i]->bp;
 358                 }
 359         }
 360         
 361         printk("\nSCC scc_get_buffer(): buffer pool empty\n");  /* should never happen */
 362         restore_flags(flags);
 363         return NULLBUF;
 364 }
 365 
 366 
 367 /* Decrement the reference pointer in an mbuf. If it goes to zero,
 368  * free all resources associated with mbuf.
 369  * Return pointer to next mbuf in packet chain
 370  */
 371 struct mbuf *
 372 scc_return_buffer(register struct mbuf *bp, char type)
     /* [previous][next][first][last][top][bottom][index][help] */
 373 {
 374         struct mbuf *bpnext;
 375         int i;
 376         unsigned long flags;
 377         
 378         if(!bp)
 379                 return NULLBUF;
 380                 
 381         save_flags(flags); cli();
 382         bpnext = bp->next;
 383         
 384         if (bp->dup)
 385         {
 386                 for(i = 0 ; i < allocated_ibufs ; i++)
 387                 {
 388                         if(sccfreelist[i]->bp == bp->dup)
 389                         {
 390                               if (sccfreelist[i]->bp->type != type)
 391                               {
 392                                    printk("scc_return_buffer(bp->dup, %i): wrong buffer type %i",
 393                                           type,sccfreelist[i]->bp->type);
 394                               }
 395                               
 396                               sccfreelist[i]->bp->cnt = 0;
 397                               sccfreelist[i]->bp->refcnt = 0;
 398                               sccfreelist[i]->bp->in_use = 0;
 399                               sccfreelist[i]->inuse = 0;
 400                               bp->dup = NULLBUF;
 401                         }
 402                 }
 403         }
 404         
 405         /* Decrement reference count. If it has gone to zero, free it. */
 406         if(--bp->refcnt <= 0)
 407         {
 408                 for(i = 0 ; i < allocated_ibufs ; i++)
 409                 {
 410                         if(sccfreelist[i]->bp == bp)
 411                         {
 412                                 if (sccfreelist[i]->bp->type != type)
 413                                 {
 414                                         printk("scc_return_buffer(bp, %i): wrong buffer type %i",
 415                                                 type,sccfreelist[i]->bp->type);
 416                                 }                               
 417                              
 418                                 sccfreelist[i]->bp->cnt = 0;
 419                                 sccfreelist[i]->bp->refcnt = 0;
 420                                 sccfreelist[i]->inuse = 0;
 421                                 restore_flags(flags);
 422                                 return bpnext;
 423                         }
 424                 }
 425         }
 426                 
 427         printk("\nscc_return_buffer(): bogus pointer %p\n",bp);
 428         restore_flags(flags);
 429         return bpnext;
 430 } 
 431 
 432 
 433 /* Free packet (a chain of mbufs). Return pointer to next packet on queue,
 434  * if any
 435  */
 436 struct mbuf *
 437 scc_free_chain(register struct mbuf *bp, char type)
     /* [previous][next][first][last][top][bottom][index][help] */
 438 {
 439         register struct mbuf *abp;
 440         unsigned long flags;
 441 
 442         if(!bp) 
 443                 return NULLBUF;
 444                 
 445         save_flags(flags); cli();       
 446         
 447         abp = bp->anext;
 448         while (bp) bp = scc_return_buffer(bp, type);
 449                 
 450         restore_flags(flags);
 451         return abp;
 452 }
 453 
 454 
 455 /* Append mbuf to end of mbuf chain */
 456 void
 457 scc_append_to_chain(struct mbuf **bph,struct mbuf *bp)
     /* [previous][next][first][last][top][bottom][index][help] */
 458 {
 459         register struct mbuf *p;
 460         unsigned long flags;
 461 
 462         if(bph == NULLBUFP || bp == NULLBUF)
 463                 return;
 464         
 465         save_flags(flags); cli();
 466         
 467         if(*bph == NULLBUF)
 468         {
 469                 /* First one on chain */
 470                 *bph = bp;
 471         } else {
 472                 for(p = *bph ; p->next != NULLBUF ; p = p->next)
 473                         ;
 474                 p->next = bp;
 475         }
 476         
 477         restore_flags(flags);
 478 }
 479 
 480 
 481 /* Append packet (chain of mbufs) to end of packet queue */
 482 void
 483 scc_enqueue(struct mbuf **queue,struct mbuf *bp)
     /* [previous][next][first][last][top][bottom][index][help] */
 484 {
 485         register struct mbuf *p;
 486         unsigned long flags;
 487 
 488         if(queue == NULLBUFP || bp == NULLBUF)
 489                 return;
 490                 
 491         save_flags(flags); cli();
 492         
 493         if(*queue == NULLBUF)
 494         {
 495                 /* List is empty, stick at front */
 496                 *queue = bp;
 497         } else {
 498                 for(p = *queue ; p->anext != NULLBUF ; p = p->anext)
 499                         ;
 500                 p->anext = bp;
 501         }
 502         restore_flags(flags);
 503 }
 504 
 505 
 506 /* ******************************************************************** */
 507 /* *                    Interrupt Service Routines                    * */
 508 /* ******************************************************************** */
 509 
 510 /* ----> interrupt service routine for the 8530 <---- */
 511 
 512 /* it's recommendet to keep this function "inline" ;-) */
 513 
 514 static inline void
 515 scc_isr_dispatch(register struct scc_channel *scc, register int vector)
     /* [previous][next][first][last][top][bottom][index][help] */
 516 {
 517         switch (vector & VECTOR_MASK)
 518         {
 519                 case TXINT: scc_txint(scc); break;
 520                 case EXINT: scc_exint(scc); break;
 521                 case RXINT: scc_rxint(scc); break;
 522                 case SPINT: scc_spint(scc); break;
 523                 default   : printk("scc_isr(): unknown interrupt status (addr %4.4x, state %2.2x)\n",scc->ctrl,vector);
 524         }
 525 }
 526 
 527 
 528 
 529 
 530 
 531 /* If the card has a latch for the interrupt vector (like the PA0HZP card)
 532    use it to get the number of the chip that generated the int.
 533    If not: poll all defined chips.
 534  */
 535 
 536 static void
 537 scc_isr(int irq, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 538 {
 539         register unsigned char vector;  
 540         register struct scc_channel *scc;
 541         register io_port q;
 542         register io_port *p;
 543         register int k;
 544 
 545 
 546         cli();
 547         
 548         if (Vector_Latch)
 549         {
 550                 while(1)                           /* forever...? */
 551                 { 
 552                         Outb(Vector_Latch, 0);      /* Generate INTACK */
 553         
 554                         /* Read the vector */
 555                         if((vector=Inb(Vector_Latch)) >= 16 * Nchips) break; 
 556                                                 /* ...not forever! */
 557           
 558                         /* Extract channel number and status from vector. */
 559                         /* Isolate channel nummer */
 560                         /* Call handler */
 561                 
 562                         if (vector & 0x01) break; 
 563                  
 564                         scc=&SCC_Info[(((vector>>1)&0x7c)^0x04) >> 2];
 565         
 566                         if (!scc->tty) break;
 567 
 568                         scc_isr_dispatch(scc, vector);
 569                         
 570                         Outb(scc->ctrl,0x38);              /* Reset Highest IUS" opcode to WR0 */
 571                 }  
 572                 
 573                 sti();  
 574                 return;
 575         }
 576         
 577         
 578         /* Find the SCC generating the interrupt by polling all attached SCCs
 579          * reading RR3A (the interrupt pending register)
 580          */
 581 
 582         k = 0;
 583         p = SCC_ctrl;
 584         
 585         while (k++ < Nchips)
 586         {
 587                 if (!(q=*p++)) break;
 588                 
 589                 Outb(q,3);
 590                 
 591                 if (Inb(q))
 592                 {
 593                         if (!(q=*p++)) break;
 594                         
 595                         Outb(q,2);
 596                         vector=Inb(q);                  /* Read the vector */
 597         
 598                         /* Extract channel number and status from vector. */
 599                         /* Isolate channel nummer */
 600                         /* Call handler */
 601         
 602 
 603                         if (vector & 1) break; 
 604         
 605                         scc = &SCC_Info[(((vector >> 1) & 0x7c) ^ 0x04) >> 2];
 606         
 607                         if (!scc->tty) break;
 608 
 609                         /* Isolate status info from vector, call handler */
 610                         
 611                         scc_isr_dispatch(scc, vector);
 612 
 613                         k = 0;
 614                         p = SCC_ctrl;
 615 
 616                 } else p++;
 617         }    
 618         
 619         sti();
 620 }
 621 
 622 
 623 
 624 /* ----> four different interrupt handlers for Tx, Rx, changing of      */
 625 /*       DCD/CTS and Rx/Tx errors                                       */
 626 
 627 
 628 static inline void prepare_next_txframe(register struct scc_channel *scc)
     /* [previous][next][first][last][top][bottom][index][help] */
 629 {
 630         if ((scc->tbp = scc->sndq))
 631         {
 632                 scc->sndq = scc->sndq->anext;
 633                 scc->stat.tx_state = TXS_NEWFRAME;
 634 
 635         } else {
 636                 scc->stat.tx_state = TXS_BUSY;
 637                 scc->t_tail = scc->kiss.tailtime;
 638         }
 639 }
 640 
 641 
 642 /* Transmitter interrupt handler */
 643 static void
 644 scc_txint(register struct scc_channel *scc)
     /* [previous][next][first][last][top][bottom][index][help] */
 645 {
 646         register struct mbuf *bp;
 647 
 648         scc->stat.txints++;
 649 
 650         bp = scc->tbp;
 651                         
 652         while (bp && !bp->cnt)      /* find next buffer */
 653                 bp = scc_return_buffer(bp, BT_TRANSMIT);
 654                                 
 655         if (bp == NULLBUF)                      /* no more buffers in this frame */
 656         {
 657                 if (--scc->stat.tx_queued < 0)
 658                         scc->stat.tx_queued = 0;
 659                         
 660                 Outb(scc->ctrl,RES_Tx_P);       /* reset pending int */                                 
 661                 cl(scc,R10,ABUNDER);            /* frame complete, allow CRC transmit */ 
 662                 prepare_next_txframe(scc);
 663                 
 664         } else {                                /* okay, send byte */
 665         
 666                 if (scc->stat.tx_state == TXS_NEWFRAME)
 667                 {                               /* first byte ? */
 668                         Outb(scc->ctrl, RES_Tx_CRC);    /* reset CRC generator */
 669                         or(scc,R10,ABUNDER);            /* re-install underrun protection */
 670                         Outb(scc->data,bp->data[bp->in_use++]);
 671                                                         /* send byte */
 672                         if (!scc->enhanced)             /* reset EOM latch */
 673                                 Outb(scc->ctrl, RES_EOM_L);
 674                                 
 675                         scc->stat.tx_state = TXS_ACTIVE;/* next byte... */
 676                 } else {
 677                         Outb(scc->data,bp->data[bp->in_use++]);
 678                 }
 679                 
 680                 bp->cnt--;                      /* decrease byte count */
 681                 scc->tbp=bp;                    /* store buffer address */
 682         }
 683 }
 684 
 685 /* Throw away received mbuf(s) when an error occurred */
 686 
 687 static inline void
 688 scc_toss_buffer(register struct scc_channel *scc)
     /* [previous][next][first][last][top][bottom][index][help] */
 689 {
 690         register struct mbuf *bp;
 691         
 692         if((bp = scc->rbp) != NULLBUF)
 693         {
 694                 scc_free_chain(bp->next, BT_RECEIVE);
 695                 bp->next = NULLBUF;
 696                 scc->rbp1 = bp;         /* Don't throw this one away */
 697                 bp->cnt = 0;            /* Simply rewind it */
 698                 bp->in_use = 0;
 699         }
 700 }
 701 
 702 static inline void
 703 flush_FIFO(register struct scc_channel *scc)
     /* [previous][next][first][last][top][bottom][index][help] */
 704 {
 705         register int k;
 706         
 707         for (k=0; k<3; k++)
 708                 Inb(scc->data);
 709                 
 710         if(scc->rbp != NULLBUF) /* did we receive something? */
 711         {
 712                 if(scc->rbp->next != NULLBUF || scc->rbp->cnt > 0)
 713                         scc->stat.rxerrs++;  /* then count it as an error */
 714                         
 715                 scc_toss_buffer(scc);         /* throw away buffer */
 716         }
 717 }
 718 
 719 
 720 
 721 /* External/Status interrupt handler */
 722 static void
 723 scc_exint(register struct scc_channel *scc)
     /* [previous][next][first][last][top][bottom][index][help] */
 724 {
 725         register unsigned char status,changes,chg_and_stat;
 726 
 727         scc->stat.exints++;
 728 
 729         status = InReg(scc->ctrl,R0);
 730         changes = status ^ scc->status;
 731         chg_and_stat = changes & status;
 732         
 733         /* ABORT: generated whenever DCD drops while receiving */
 734 
 735         if (chg_and_stat & BRK_ABRT)            /* Received an ABORT */
 736                 flush_FIFO(scc);
 737                         
 738                 
 739         /* DCD: on = start to receive packet, off = ABORT condition */
 740         /* (a successfully received packet generates a special condition int) */
 741         
 742         if(changes & DCD)                       /* DCD input changed state */
 743         {
 744                 if(status & DCD)                /* DCD is now ON */
 745                 {
 746                         if (scc->modem.clocksrc != CLK_EXTERNAL)
 747                                 OutReg(scc->ctrl,R14,SEARCH|scc->wreg[R14]); /* DPLL: enter search mode */
 748                                 
 749                         or(scc,R3,ENT_HM|RxENABLE); /* enable the receiver, hunt mode */
 750                 } else {                        /* DCD is now OFF */
 751                         cl(scc,R3,ENT_HM|RxENABLE); /* disable the receiver */
 752                         flush_FIFO(scc);
 753                 }
 754         }
 755 
 756 
 757         /* CTS: use external TxDelay (what's that good for?!) */
 758         
 759         if (chg_and_stat & CTS)                 /* CTS is now ON */
 760         {                       
 761                 if (!Running(t_txdel) && scc->kiss.txdelay == 0) /* zero TXDELAY = wait for CTS */
 762                         scc->t_txdel = 0;       /* kick it! */          
 763                 
 764         }
 765         
 766         if ((scc->stat.tx_state == TXS_ACTIVE) && (status & TxEOM))
 767         {
 768                 scc->stat.tx_under++;     /* oops, an underrun! count 'em */
 769                 Outb(scc->ctrl, RES_Tx_P);
 770                 Outb(scc->ctrl, RES_EXT_INT);   /* reset ext/status interrupts */
 771                 scc->t_maxk = 1;
 772                 scc->tbp = scc_free_chain(scc->tbp, BT_TRANSMIT);
 773                 
 774                 if (--scc->stat.tx_queued < 0) scc->stat.tx_queued = 0;
 775                 or(scc,R10,ABUNDER);
 776         }
 777                 
 778         if (status & ZCOUNT)               /* Oops? */
 779         {
 780                 scc->stat.tx_under = 9999;  /* errr... yes. */
 781                 Outb(scc->ctrl, RES_Tx_P); /* just to be sure */
 782                 scc->t_maxk = 1;
 783                 scc->tbp = scc_free_chain(scc->tbp, BT_TRANSMIT);
 784                 if (--scc->stat.tx_queued < 0) scc->stat.tx_queued = 0;
 785                 scc->kiss.tx_inhibit = 1;       /* don't try it again! */
 786         }
 787                 
 788         
 789         scc->status = status;
 790         Outb(scc->ctrl,RES_EXT_INT);
 791 }
 792 
 793 
 794 /* Receiver interrupt handler */
 795 static void
 796 scc_rxint(register struct scc_channel *scc)
     /* [previous][next][first][last][top][bottom][index][help] */
 797 {
 798         register struct mbuf *bp;
 799 
 800         scc->stat.rxints++;
 801 
 802         if( Running(t_maxk) && !(scc->kiss.fulldup))
 803         {
 804                 Inb(scc->data);         /* discard char */
 805                 or(scc,R3,ENT_HM);      /* enter hunt mode for next flag */
 806                 return;
 807         }
 808 
 809         if ((bp = scc->rbp1) == NULLBUF || bp->cnt >= bp->size) 
 810         {                               /* no buffer available or buffer full */
 811                 if (scc->rbp == NULLBUF)
 812                 {
 813                         if ((bp = scc_get_buffer(BT_RECEIVE)) != NULLBUF)
 814                                 scc->rbp = scc->rbp1 = bp;
 815                                 
 816                 }
 817                 else if ((bp = scc_get_buffer(BT_RECEIVE)))
 818                 {
 819                         scc_append_to_chain(&scc->rbp, bp);
 820                         scc->rbp1 = bp;
 821                 }
 822                 
 823                 if (bp == NULLBUF)              /* no buffer available? */
 824                 {
 825                         Inb(scc->data);         /* discard character */
 826                         or(scc,R3,ENT_HM);      /* enter hunt mode */
 827                         scc_toss_buffer(scc);   /* throw away buffers */
 828                         scc->stat.nospace++;    /* and count this error */
 829                         return;
 830                 }
 831         }
 832 
 833         /* now, we have a buffer. read character and store it */
 834         bp->data[bp->cnt++] = Inb(scc->data);
 835 }
 836 
 837 /* kick rx_timer (try to send received frame or part of it ASAP) */
 838 /* !experimental! */
 839 
 840 static inline void
 841 kick_rx_timer(register struct scc_channel *scc)
     /* [previous][next][first][last][top][bottom][index][help] */
 842 {
 843         register unsigned long expires;
 844 
 845         if (!rx_timer_cb.lock)
 846         {
 847                 expires = timer_table[SCC_TIMER].expires - jiffies;
 848 
 849                 rx_timer_cb.expires = (expires > 1)? expires:1;
 850                 rx_timer_cb.scc = scc;
 851                 rx_timer_cb.lock = 1;
 852         
 853                 timer_table[SCC_TIMER].fn = scc_rx_timer;
 854                 timer_table[SCC_TIMER].expires = jiffies + 1;
 855                 timer_active |= 1 << SCC_TIMER;
 856         }
 857 }
 858 
 859 /* Receive Special Condition interrupt handler */
 860 static void
 861 scc_spint(register struct scc_channel *scc)
     /* [previous][next][first][last][top][bottom][index][help] */
 862 {
 863         register unsigned char status;
 864         register struct mbuf *bp;
 865 
 866         scc->stat.spints++;
 867 
 868         status = InReg(scc->ctrl,R1);           /* read receiver status */
 869         
 870         Inb(scc->data);                         /* throw away Rx byte */
 871 
 872         if(status & Rx_OVR)                     /* receiver overrun */
 873         {
 874                 scc->stat.rx_over++;                /* count them */
 875                 or(scc,R3,ENT_HM);              /* enter hunt mode for next flag */
 876                 scc_toss_buffer(scc);                 /* rewind the buffer and toss */
 877         }
 878         
 879         if(status & END_FR && scc->rbp != NULLBUF)      /* end of frame */
 880         {
 881                 /* CRC okay, frame ends on 8 bit boundary and received something ? */
 882                 
 883                 if (!(status & CRC_ERR) && (status & 0xe) == RES8 && scc->rbp->cnt)
 884                 {
 885                         /* ignore last received byte (first of the CRC bytes) */
 886                         
 887                         for (bp = scc->rbp; bp->next != NULLBUF; bp = bp->next) ;
 888                                 bp->cnt--;              /* last byte is first CRC byte */
 889                                 
 890                         scc_enqueue(&scc->rcvq,scc->rbp);
 891                         scc->rbp = scc->rbp1 = NULLBUF;
 892                         scc->stat.rxframes++;
 893                         scc->stat.rx_queued++;
 894                         kick_rx_timer(scc);
 895                 } else {                                /* a bad frame */
 896                         scc_toss_buffer(scc);           /* throw away frame */
 897                         scc->stat.rxerrs++;
 898                 }
 899         }
 900         
 901         Outb(scc->ctrl,ERR_RES);
 902 }
 903 
 904 
 905 /* ******************************************************************** */
 906 /* *                    Init Channel                                    */
 907 /* ******************************************************************** */
 908 
 909 
 910 /* ----> set SCC channel speed <---- */
 911 
 912 static inline void set_brg(register struct scc_channel *scc, unsigned int tc)
     /* [previous][next][first][last][top][bottom][index][help] */
 913 {
 914         unsigned long flags;
 915 
 916         save_flags(flags); cli();       /* 2-step register accesses... */
 917 
 918         cl(scc,R14,BRENABL);            /* disable baudrate generator */
 919         wr(scc,R12,tc & 255);           /* brg rate LOW */
 920         wr(scc,R13,tc >> 8);            /* brg rate HIGH */
 921         or(scc,R14,BRENABL);            /* enable baudrate generator */
 922 
 923         restore_flags(flags);
 924 }
 925 
 926 static inline void set_speed(register struct scc_channel *scc)
     /* [previous][next][first][last][top][bottom][index][help] */
 927 {
 928         set_brg(scc, (unsigned) (Clock / (scc->modem.speed * 64)) - 2);
 929 }
 930 
 931 
 932 /* ----> initialize a SCC channel <---- */
 933 
 934 static inline void init_brg(register struct scc_channel *scc)
     /* [previous][next][first][last][top][bottom][index][help] */
 935 {
 936         wr(scc, R14, BRSRC);                            /* BRG source = PCLK */
 937         OutReg(scc->ctrl, R14, SSBR|scc->wreg[R14]);    /* DPLL source = BRG */
 938         OutReg(scc->ctrl, R14, SNRZI|scc->wreg[R14]);   /* DPLL NRZI mode */
 939 }
 940 
 941 static void
 942 init_channel(register struct scc_channel *scc)
     /* [previous][next][first][last][top][bottom][index][help] */
 943 {
 944         unsigned long flags;
 945 
 946         save_flags(flags); cli();
 947 
 948         wr(scc,R1,0);                   /* no W/REQ operation */
 949         wr(scc,R3,Rx8|RxCRC_ENAB);      /* RX 8 bits/char, CRC, disabled */     
 950         wr(scc,R4,X1CLK|SDLC);          /* *1 clock, SDLC mode */
 951         wr(scc,R5,Tx8|DTR|TxCRC_ENAB);  /* TX 8 bits/char, disabled, DTR */
 952         wr(scc,R6,0);                   /* SDLC address zero (not used) */
 953         wr(scc,R7,FLAG);                /* SDLC flag value */
 954         wr(scc,R10,(scc->modem.nrz? NRZ : NRZI)|CRCPS|ABUNDER); /* abort on underrun, preset CRC generator, NRZ(I) */
 955         wr(scc,R14, 0);
 956 
 957 
 958 /* set clock sources:
 959 
 960    CLK_DPLL: normal halfduplex operation
 961    
 962                 RxClk: use DPLL
 963                 TxClk: use DPLL
 964                 TRxC mode DPLL output
 965                 
 966    CLK_EXTERNAL: external clocking (G3RUH or DF9IC modem)
 967    
 968                 BayCom:                 others:
 969                 
 970                 TxClk = pin RTxC        TxClk = pin TRxC
 971                 RxClk = pin TRxC        RxClk = pin RTxC
 972              
 973 
 974    CLK_DIVIDER:
 975                 RxClk = use DPLL
 976                 TxClk = pin RTxC
 977                 
 978                 BayCom:                 others:
 979                 pin TRxC = DPLL         pin TRxC = BRG
 980                 (RxClk * 1)             (RxClk * 32)
 981 */  
 982 
 983                 
 984         switch(scc->modem.clocksrc)
 985         {
 986                 case CLK_DPLL:
 987                         wr(scc, R11, RCDPLL|TCDPLL|TRxCOI|TRxCDP);
 988                         init_brg(scc);
 989                         break;
 990 
 991                 case CLK_DIVIDER:
 992                         wr(scc, R11, ((Board & BAYCOM)? TRxCDP : TRxCBR) | RCDPLL|TCRTxCP|TRxCOI);
 993                         init_brg(scc);
 994                         break;
 995 
 996                 case CLK_EXTERNAL:
 997                         wr(scc, R11, (Board & BAYCOM)? RCTRxCP|TCRTxCP : RCRTxCP|TCTRxCP);
 998                         OutReg(scc->ctrl, R14, DISDPLL);
 999                         break;
1000 
1001         }
1002         
1003         /* enable CTS (not for Baycom), ABORT & DCD interrupts */
1004         wr(scc,R15,((Board & BAYCOM) ? 0 : CTSIE)|BRKIE|DCDIE|TxUIE);
1005 
1006         if(scc->enhanced)
1007         {
1008                 or(scc,R15,SHDLCE|FIFOE);       /* enable FIFO, SDLC/HDLC Enhancements (From now R7 is R7') */
1009                 wr(scc,R7,AUTOEOM);
1010         }
1011 
1012         if((InReg(scc->ctrl,R0)) & DCD)         /* DCD is now ON */
1013         {
1014                 if (scc->modem.clocksrc != CLK_EXTERNAL)
1015                         or(scc,R14, SEARCH);
1016                         
1017                 or(scc,R3,ENT_HM|RxENABLE);     /* enable the receiver, hunt mode */
1018         }
1019         
1020         Outb(scc->ctrl,RES_EXT_INT);    /* reset ext/status interrupts */
1021         Outb(scc->ctrl,RES_EXT_INT);    /* must be done twice */
1022         
1023         scc->status = InReg(scc->ctrl,R0);      /* read initial status */
1024 
1025         or(scc,R1,INT_ALL_Rx|TxINT_ENAB|EXT_INT_ENAB); /* enable interrupts */
1026         or(scc,R9,MIE);                 /* master interrupt enable */
1027                         
1028         restore_flags(flags);
1029         
1030         set_speed(scc); 
1031 }
1032 
1033 
1034 
1035 
1036 /* ******************************************************************** */
1037 /* *                    SCC timer functions                           * */
1038 /* ******************************************************************** */
1039 
1040 
1041 /* ----> scc_key_trx sets the time constant for the baudrate 
1042          generator and keys the transmitter                  <---- */
1043 
1044 static void
1045 scc_key_trx(struct scc_channel *scc, char tx)
     /* [previous][next][first][last][top][bottom][index][help] */
1046 {
1047         unsigned int time_const;
1048 
1049         if (scc->modem.speed < baud_table[1]) 
1050                 scc->modem.speed = 1200;
1051                 
1052         if (Board & PRIMUS)
1053                 Outb(scc->ctrl + 4, Option | (tx? 0x80 : 0));
1054         
1055         time_const = (unsigned) (Clock / (scc->modem.speed * (tx? 2:64))) - 2;
1056         
1057         if (scc->modem.clocksrc == CLK_DPLL)
1058         {                               /* simplex operation */
1059                 if (tx)
1060                 {
1061                         cl(scc,R3,RxENABLE|ENT_HM);     /* then switch off receiver */
1062                         
1063                         set_brg(scc, time_const);       /* reprogram baudrate generator */
1064 
1065                         /* DPLL -> Rx clk, BRG -> Tx CLK, TRxC mode output, TRxC = BRG */
1066                         wr(scc, R11, RCDPLL|TCBR|TRxCOI|TRxCBR);
1067                         
1068                         or(scc,R5,RTS|TxENAB);          /* set the RTS line and enable TX */
1069                 } else {
1070                         cl(scc,R5,RTS|TxENAB);
1071                         
1072                         set_brg(scc, time_const);       /* reprogram baudrate generator */
1073                         
1074                         /* DPLL -> Rx clk, DPLL -> Tx CLK, TRxC mode output, TRxC = DPLL */
1075                         wr(scc, R11, RCDPLL|TCDPLL|TRxCOI|TRxCDP);
1076                         
1077                         or(scc,R3,RxENABLE|ENT_HM);
1078                 }
1079         } else {
1080                 if (tx)
1081                         or(scc,R5,RTS|TxENAB);          /* enable tx */
1082                 else
1083                         cl(scc,R5,RTS|TxENAB);          /* disable tx */
1084         }
1085 }
1086 
1087 
1088 /* ----> SCC timer interrupt handler and friends. Will be called every 1/TPS s <---- */
1089 
1090 static unsigned char Rand = 17;
1091 
1092 static inline int is_grouped(register struct scc_channel *scc)
     /* [previous][next][first][last][top][bottom][index][help] */
1093 {
1094         int k;
1095         struct scc_channel *scc2;
1096         unsigned char grp1, grp2;
1097 
1098         grp1 = scc->kiss.group;
1099         
1100         for (k = 0; k < (Nchips * 2); k++)
1101         {
1102                 scc2 = &SCC_Info[k];
1103                 grp2 = scc2->kiss.group;
1104                 
1105                 if (scc2 == scc || !(scc2->tty && grp2)) 
1106                         return 0;
1107                 
1108                 if ((grp1 & 0x3f) == (grp2 & 0x3f))
1109                 {
1110                         if ( (grp1 & TXGROUP) && (scc2->wreg[R5] & RTS) )
1111                                 return 1;
1112                         
1113                         if ( (grp1 & RXGROUP) && (scc2->status & DCD) )
1114                                 return 1;
1115                 }
1116         }
1117         return 0;
1118 }
1119                 
1120 
1121 static inline void dw_slot_timeout(register struct scc_channel *scc)
     /* [previous][next][first][last][top][bottom][index][help] */
1122 {
1123         scc->t_dwait = TIMER_STOPPED;
1124         scc->t_slot = TIMER_STOPPED;
1125         
1126         if (!scc->kiss.fulldup)
1127         {
1128                 Rand = Rand * 17 + 31;
1129                 
1130                 if ( (scc->kiss.softdcd? !(scc->status & SYNC_HUNT):(scc->status & DCD))  || (scc->kiss.persist) < Rand || (scc->kiss.group && is_grouped(scc)) )
1131                 {
1132                         if (scc->t_mbusy == TIMER_STOPPED)
1133                                 scc->t_mbusy = TPS * scc->kiss.maxdefer;
1134                                 
1135                         scc->t_slot = scc->kiss.slottime;
1136                         return ;
1137                         
1138                 }
1139         }
1140         
1141         if ( !(scc->wreg[R5] & RTS) )
1142         {
1143                 scc->t_txdel = scc->kiss.txdelay;
1144                 scc_key_trx(scc, TX_ON);
1145         } else {
1146                 scc->t_txdel = 0;
1147         }
1148 }
1149 
1150 
1151 
1152 
1153 static inline void txdel_timeout(register struct scc_channel *scc)
     /* [previous][next][first][last][top][bottom][index][help] */
1154 {
1155         scc->t_txdel = TIMER_STOPPED;
1156         
1157         scc->t_maxk = TPS * scc->kiss.maxkeyup;
1158         prepare_next_txframe(scc);
1159         
1160         if (scc->stat.tx_state != TXS_BUSY)
1161                 scc_txint(scc);         
1162 }
1163         
1164 
1165 
1166 static inline void tail_timeout(register struct scc_channel *scc)
     /* [previous][next][first][last][top][bottom][index][help] */
1167 {
1168         scc->t_tail = TIMER_STOPPED;
1169         
1170         /* when fulldup is 0 or 1, switch off the transmitter.
1171          * when frames are still queued (because of transmit time limit),
1172          * restart the procedure to get the channel after MINTIME.
1173          * when fulldup is 2, the transmitter remains keyed and we
1174          * continue sending after waiting for waittime. IDLETIME is an 
1175          * idle timeout in this case.
1176          */ 
1177          
1178          if (scc->kiss.fulldup < 2)
1179          {
1180                 if (scc->sndq)          /* we had a timeout? */
1181                 {
1182                         scc->stat.tx_state = TXS_BUSY;
1183                         scc->t_dwait = TPS * scc->kiss.mintime; /* try again */
1184                 }
1185                 
1186                 scc->stat.tx_state = TXS_IDLE;
1187                 scc->t_maxk = TIMER_STOPPED;
1188                 scc_key_trx(scc, TX_OFF);
1189                 return;
1190          }
1191          
1192          if (scc->sndq)                 /* maxkeyup expired */ /* ?! */
1193          {
1194                 scc->stat.tx_state = TXS_BUSY;
1195                 scc->t_txdel = TPS * scc->kiss.waittime;
1196          }
1197          else
1198          
1199                 scc->t_idle = TPS * scc->kiss.idletime;
1200 }
1201 
1202 
1203 static inline void busy_timeout(register struct scc_channel *scc)
     /* [previous][next][first][last][top][bottom][index][help] */
1204 {
1205 #ifdef  THROW_AWAY_AFTER_BUSY_TIMEOUT
1206         register struct mbuf *bp;               /* not tested */
1207 
1208         bp = scc->sndq;
1209         
1210         while (bp) bp = scc_free_chain(bp, BT_TRANSMIT);
1211         
1212         scc->sndq = NULLBUF;
1213         scc->stat.tx_state = TXS_IDLE;
1214         
1215 #else
1216         scc->t_txdel = scc->kiss.txdelay;       /* brute force ... */
1217 #endif
1218         scc->t_mbusy = TIMER_STOPPED;
1219         
1220 }
1221 
1222 
1223 static inline void maxk_idle_timeout(register struct scc_channel *scc)
     /* [previous][next][first][last][top][bottom][index][help] */
1224 {
1225         scc->t_maxk = TIMER_STOPPED;
1226         scc->t_idle = TIMER_STOPPED;
1227         
1228         scc->stat.tx_state = TXS_BUSY;
1229         scc->t_tail = scc->kiss.tailtime;
1230 }
1231 
1232 static inline void check_rcv_queue(register struct scc_channel *scc)
     /* [previous][next][first][last][top][bottom][index][help] */
1233 {
1234         register struct mbuf *bp;
1235         
1236         if (scc->stat.rx_queued > QUEUE_THRES)
1237         {
1238                 if (scc->rcvq == NULLBUF)
1239                 {
1240                         printk("z8530drv: Warning - scc->stat.rx_queued shows overflow"
1241                                " (%d) but queue is empty\n", scc->stat.rx_queued);
1242                                
1243                         scc->stat.rx_queued = 0;        /* correct it */
1244                         scc->stat.nospace = 12345;      /* draw attention to it */
1245                         return;
1246                 }
1247                         
1248                 bp = scc->rcvq->anext;  /* don't use the one we currently use */
1249                 
1250                 while (bp && (scc->stat.rx_queued > QUEUE_HYST))
1251                 {
1252                         bp = scc_free_chain(bp, BT_RECEIVE);
1253                         scc->stat.rx_queued--;
1254                         scc->stat.nospace++;
1255                 }
1256                 
1257                 scc->rcvq->anext = bp;
1258         }
1259 }
1260 
1261 static void
1262 scc_timer(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1263 {
1264         register struct scc_channel *scc;
1265         register int chan;
1266         unsigned long flags;
1267                 
1268 
1269         for (chan = 0; chan < (Nchips * 2); chan++)
1270         {
1271                 scc = &SCC_Info[chan];
1272                 
1273                 if (scc->tty && scc->init)
1274                 {
1275                         kiss_encode(scc);
1276                         
1277                         save_flags(flags); cli();
1278                         
1279                         check_rcv_queue(scc);
1280                         
1281                         /* KISS-TNC emulation */
1282                         
1283                         if (Expired(t_dwait)) dw_slot_timeout(scc)      ; else
1284                         if (Expired(t_slot))  dw_slot_timeout(scc)      ; else
1285                         if (Expired(t_txdel)) txdel_timeout(scc)        ; else
1286                         if (Expired(t_tail))  tail_timeout(scc)         ;
1287                         
1288                         /* watchdogs */
1289                         
1290                         if (Expired(t_mbusy)) busy_timeout(scc);
1291                         if (Expired(t_maxk))  maxk_idle_timeout(scc);
1292                         if (Expired(t_idle))  maxk_idle_timeout(scc);
1293                         
1294                         restore_flags(flags);
1295                 }
1296         }
1297         
1298         save_flags(flags); cli();
1299         
1300         timer_table[SCC_TIMER].fn = scc_timer;
1301         timer_table[SCC_TIMER].expires = jiffies + HZ/TPS;
1302         timer_active |= 1 << SCC_TIMER; 
1303         
1304         restore_flags(flags);
1305 }
1306                         
1307 
1308 static void
1309 scc_init_timer(struct scc_channel *scc)
     /* [previous][next][first][last][top][bottom][index][help] */
1310 {
1311         unsigned long flags;
1312         
1313         save_flags(flags); cli();
1314         
1315         Stop_Timer(t_dwait);
1316         Stop_Timer(t_slot);
1317         Stop_Timer(t_txdel);
1318         Stop_Timer(t_tail);
1319         Stop_Timer(t_mbusy);
1320         Stop_Timer(t_maxk);
1321         Stop_Timer(t_idle);
1322         scc->stat.tx_state = TXS_IDLE;
1323         
1324         restore_flags(flags);
1325 }
1326 
1327 
1328 static void
1329 scc_rx_timer(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1330 {
1331         unsigned long flags;
1332         
1333         kiss_encode(rx_timer_cb.scc);
1334         
1335         save_flags(flags); cli();
1336         
1337         timer_table[SCC_TIMER].fn = scc_timer;
1338         timer_table[SCC_TIMER].expires = jiffies + rx_timer_cb.expires;
1339         timer_active |= 1 << SCC_TIMER;
1340         
1341         rx_timer_cb.lock = 0;
1342         
1343         restore_flags(flags);
1344 }
1345 
1346 
1347 /* ******************************************************************** */
1348 /* *                    KISS interpreter                              * */
1349 /* ******************************************************************** */
1350 
1351 
1352 /*
1353  * this will set the "kiss" parameters through kiss itself
1354  */
1355  
1356 static void
1357 kiss_set_param(struct scc_channel *scc,char cmd, unsigned int val)
     /* [previous][next][first][last][top][bottom][index][help] */
1358 {
1359 
1360 #define  VAL val=val*TPS/100
1361 #define SVAL val? val:TIMER_STOPPED
1362 
1363         switch(cmd){
1364         case PARAM_TXDELAY:
1365                 scc->kiss.txdelay = VAL; break;
1366         case PARAM_PERSIST:
1367                 scc->kiss.persist = val; break;
1368         case PARAM_SLOTTIME:
1369                 scc->kiss.slottime = VAL; break;
1370         case PARAM_TXTAIL:
1371                 scc->kiss.tailtime = VAL; break;
1372         case PARAM_FULLDUP:
1373                 scc->kiss.fulldup = val; break;
1374         case PARAM_WAIT:
1375                 scc->kiss.waittime = VAL; break;
1376         case PARAM_MAXKEY:
1377                 scc->kiss.maxkeyup = SVAL; break;
1378         case PARAM_MIN:
1379                 scc->kiss.mintime = SVAL; break;
1380         case PARAM_IDLE:
1381                 scc->kiss.idletime = val; break;
1382         case PARAM_MAXDEFER:
1383                 scc->kiss.maxdefer = SVAL; break;
1384         case PARAM_GROUP:
1385                 scc->kiss.group = val;  break;
1386         case PARAM_TX:
1387                 scc->kiss.tx_inhibit = val;
1388         case PARAM_SOFTDCD:
1389                 scc->kiss.softdcd = val;
1390         }
1391         return;
1392 
1393 #undef  VAL
1394 #undef SVAL
1395 }
1396 
1397 
1398 /* interpret frame: strip CRC and decode KISS */
1399 
1400 static void kiss_interpret_frame(struct scc_channel * scc)
     /* [previous][next][first][last][top][bottom][index][help] */
1401 {
1402         unsigned char kisscmd;
1403         unsigned long flags;
1404 
1405         if (scc->sndq1->cnt < 2)
1406         {
1407                 if (scc->sndq1) 
1408                         scc_free_chain(scc->sndq1, BT_TRANSMIT);
1409                 else
1410                         scc->sndq1 = NULLBUF;
1411                         
1412                 scc->sndq2 = NULLBUF;
1413                 return;
1414         }
1415         
1416         
1417         
1418         if (scc->kiss.not_slip)
1419         {
1420                 kisscmd = scc->sndq1->data[scc->sndq1->in_use++];
1421                 scc->sndq1->cnt--;
1422         } else {
1423                 kisscmd = 0;
1424         }
1425 
1426         if (kisscmd & 0xa0)
1427         {
1428                 if (scc->sndq1->cnt > 2)
1429                         scc->sndq1->cnt -= 2;
1430                 else
1431                 {
1432                         scc_free_chain(scc->sndq1, BT_TRANSMIT);
1433                         scc->sndq2 = NULLBUF;
1434                         return;
1435                 }
1436         }
1437         
1438         
1439         kisscmd &= 0x1f;
1440         
1441                 
1442         if (kisscmd)
1443         {
1444                 kiss_set_param(scc, kisscmd, scc->sndq1->data[scc->sndq1->in_use]);
1445                 scc->sndq1->cnt=0;
1446                 scc->sndq1->in_use=0;
1447                                         
1448                 scc_free_chain(scc->sndq1, BT_TRANSMIT);
1449                 scc->sndq2 = NULLBUF;
1450                 return;
1451         }
1452         
1453         scc_enqueue(&scc->sndq,scc->sndq1); /* scc_enqueue packet */
1454         scc->stat.txframes++;
1455         scc->stat.tx_queued++;
1456         scc->sndq2 = NULLBUF;           /* acquire a new buffer next time */
1457 
1458         save_flags(flags); cli();
1459 
1460         if(scc->stat.tx_state == TXS_IDLE)
1461         {                               /* when transmitter is idle */
1462                 scc_init_timer(scc);
1463                 scc->stat.tx_state = TXS_BUSY;
1464                 scc->t_dwait = (scc->kiss.fulldup? 0:scc->kiss.waittime);
1465         }
1466         
1467         restore_flags(flags);
1468 }
1469 
1470 static void kiss_store_byte(struct scc_channel *scc, unsigned char ch)
     /* [previous][next][first][last][top][bottom][index][help] */
1471 {
1472         if (scc->sndq2 == NULLBUF) return;
1473         
1474         if(scc->sndq2->cnt == scc->sndq2->size)         /* buffer full? */
1475         {
1476                 if((scc->sndq2 = scc_get_buffer(BT_TRANSMIT)) == NULLBUF)
1477                 {
1478                         printk("\nsccdrv: running out of memory\n");
1479                         return;
1480                 }
1481                 scc_append_to_chain(&scc->sndq1,scc->sndq2);         /* add buffer */
1482         }
1483         
1484         scc->sndq2->data[scc->sndq2->cnt++] = ch;
1485 }
1486 
1487 static inline int kiss_decode(struct scc_channel *scc, unsigned char ch)
     /* [previous][next][first][last][top][bottom][index][help] */
1488 {
1489         switch (scc->stat.tx_kiss_state) 
1490         {
1491                 case KISS_IDLE:
1492                         if (ch == FEND)
1493                         {
1494                                 if (!(scc->sndq2 = scc->sndq1 = scc_get_buffer(BT_TRANSMIT)))
1495                                         return 0;
1496                                         
1497                                 scc->stat.tx_kiss_state = KISS_DATA;
1498                         } else scc->stat.txerrs++;
1499                         break;
1500                                         
1501                 case KISS_DATA:
1502                         if (ch == FESC)
1503                                 scc->stat.tx_kiss_state = KISS_ESCAPE;
1504                         else if (ch == FEND)
1505                         {
1506                                 kiss_interpret_frame(scc);      
1507                                 scc->stat.tx_kiss_state = KISS_IDLE;
1508                         }
1509                         else kiss_store_byte(scc, ch);
1510                         break;
1511                                         
1512                 case KISS_ESCAPE:
1513                         if (ch == TFEND)
1514                         {
1515                                 kiss_store_byte(scc, FEND);
1516                                 scc->stat.tx_kiss_state = KISS_DATA;
1517                         }
1518                         else if (ch == TFESC)
1519                         {
1520                                 kiss_store_byte(scc, FESC);
1521                                 scc->stat.tx_kiss_state = KISS_DATA;
1522                         }
1523                         else
1524                         {
1525                                 scc_free_chain(scc->sndq1, BT_TRANSMIT);
1526                                 scc->sndq2 = NULLBUF;
1527                                 scc->stat.txerrs++;
1528                                 scc->stat.tx_kiss_state = KISS_IDLE;
1529                         }
1530                         break;
1531         } /* switch */
1532         
1533         return 0;
1534         
1535 }
1536 
1537 /* ----> Encode received data and write it to the flip-buffer  <---- */
1538 
1539 /* receive raw frame from SCC. used for AX.25 */
1540 static void
1541 kiss_encode(register struct scc_channel *scc)
     /* [previous][next][first][last][top][bottom][index][help] */
1542 {
1543         struct mbuf *bp,*bp2;
1544         struct tty_struct * tty = scc->tty;
1545         unsigned long flags; 
1546         unsigned char ch;
1547 
1548         if(!scc->rcvq)
1549         {
1550                 scc->stat.rx_kiss_state = KISS_IDLE;
1551                 return;
1552         }
1553         
1554         /* worst case: FEND 0 FESC TFEND -> 4 bytes */
1555         
1556         while(tty->flip.count < TTY_FLIPBUF_SIZE-3)
1557         { 
1558                 if (scc->rcvq->cnt)
1559                 {
1560                         bp = scc->rcvq;
1561                         
1562                         if (scc->stat.rx_kiss_state == KISS_IDLE)
1563                         {
1564                                 tty_insert_flip_char(tty, FEND, 0);
1565                                 
1566                                 if (scc->kiss.not_slip)
1567                                         tty_insert_flip_char(tty, 0, 0);
1568                                         
1569                                 scc->stat.rx_kiss_state = KISS_RXFRAME;
1570                         }
1571                                 
1572                         switch(ch = bp->data[bp->in_use++])
1573                         {
1574                                 case FEND:
1575                                         tty_insert_flip_char(tty, FESC, 0);
1576                                         tty_insert_flip_char(tty, TFEND, 0);
1577                                         break;
1578                                 case FESC:
1579                                         tty_insert_flip_char(tty, FESC, 0);
1580                                         tty_insert_flip_char(tty, TFESC, 0);
1581                                         break;
1582                                 default:
1583                                         tty_insert_flip_char(tty, ch, 0);
1584                         }
1585                         
1586                         bp->cnt--;
1587                         
1588                 } else {
1589                         save_flags(flags); cli();
1590                         
1591                         while (!scc->rcvq->cnt)
1592                         {                                /* buffer empty? */
1593                                 bp  = scc->rcvq->next;  /* next buffer */
1594                                 bp2 = scc->rcvq->anext; /* next packet */
1595                                 
1596                                 
1597                                 scc_return_buffer(scc->rcvq, BT_RECEIVE);
1598                                 
1599                                 if (!bp)        /* end of frame ? */
1600                                 {
1601                                         scc->rcvq = bp2;
1602                                         
1603                                         if (--scc->stat.rx_queued < 0)
1604                                                 scc->stat.rx_queued = 0;
1605                                         
1606                                         if (scc->stat.rx_kiss_state == KISS_RXFRAME)    /* new packet? */
1607                                         {
1608                                                 tty_insert_flip_char(tty, FEND, 0); /* send FEND for old frame */
1609                                                 scc->stat.rx_kiss_state = KISS_IDLE; /* generate FEND for new frame */
1610                                         }
1611                                         
1612                                         restore_flags(flags);
1613                                         queue_task(&tty->flip.tqueue, &tq_timer);
1614                                         return;
1615                                         
1616                                 } else scc->rcvq = bp; /* next buffer */
1617                         }
1618                         
1619                         restore_flags(flags);
1620                 }                                               
1621                 
1622         }
1623         
1624         queue_task(&tty->flip.tqueue, &tq_timer); /* kick it... */
1625 }
1626 
1627 
1628 /* ******************************************************************* */
1629 /* *            Init channel structures, special HW, etc...          * */
1630 /* ******************************************************************* */
1631 
1632 
1633 static void
1634 z8530_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1635 {
1636         struct scc_channel *scc;
1637         int chip;
1638         unsigned long flags;
1639 
1640         /* reset and pre-init all chips in the system */
1641         for (chip = 0; chip < Nchips; chip++)
1642         {
1643                 /* Special SCC cards */
1644 
1645                 if(Board & EAGLE)                       /* this is an EAGLE card */
1646                         Outb(Special_Port,0x08);        /* enable interrupt on the board */
1647                         
1648                 if(Board & (PC100 | PRIMUS))            /* this is a PC100/EAGLE card */
1649                         Outb(Special_Port,Option);      /* set the MODEM mode (0x22) */
1650                         
1651                 /* Init SCC */
1652                 
1653                 scc=&SCC_Info[2*chip];
1654                 if (!scc->ctrl) continue;
1655                 
1656                 save_flags(flags); cli();
1657                 
1658                 /* some general init we can do now */
1659                 
1660                 Outb(scc->ctrl, 0);
1661                 OutReg(scc->ctrl,R9,FHWRES);            /* force hardware reset */
1662                 udelay(100);                            /* give it 'a bit' more time than required */
1663                 wr(scc, R2, chip*16);                   /* interrupt vector */
1664                 wr(scc, R9, VIS);                       /* vector includes status */
1665 
1666                 restore_flags(flags);
1667         }
1668 
1669         if (Ivec == 2) Ivec = 9;                        /* this f... IBM AT-design! */
1670         request_irq(Ivec, scc_isr,   SA_INTERRUPT, "AX.25 SCC");
1671  
1672         Driver_Initialized = 1;
1673 }
1674 
1675 
1676 /* ******************************************************************** */
1677 /* *    Filesystem Routines: open, close, ioctl, settermios, etc      * */
1678 /* ******************************************************************** */
1679 
1680 
1681 
1682 /* scc_paranoia_check(): warn user if something went wrong              */
1683 
1684 static inline int scc_paranoia_check(struct scc_channel *scc, kdev_t device,
     /* [previous][next][first][last][top][bottom][index][help] */
1685                                      const char *routine)
1686 {
1687 #ifdef SCC_PARANOIA_CHECK
1688         static const char *badmagic =
1689                 "Warning: bad magic number for Z8530 SCC struct (%s) in %s\n";
1690         static const char *badinfo =
1691                 "Warning: Z8530 not found for (%s) in %s\n";
1692        
1693         if (!scc->init) 
1694         {
1695                 printk(badinfo, kdevname(device), routine);
1696                 return 1;
1697         }
1698         if (scc->magic != SCC_MAGIC) {
1699                 printk(badmagic, kdevname(device), routine);
1700                 return 1;
1701         }
1702 #endif
1703 
1704         return 0;
1705 }
1706 
1707 
1708 /* ----> this one is called whenever you open the device <---- */
1709 
1710 int scc_open(struct tty_struct *tty, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
1711 {
1712         struct scc_channel *scc;
1713         int chan;
1714         
1715         chan = MINOR(tty->device) - tty->driver.minor_start;
1716         if ((chan < 0) || (chan >= (Nchips * 2)))
1717                 return -ENODEV;
1718  
1719         scc = &SCC_Info[chan];
1720         
1721         tty->driver_data = scc;
1722         tty->termios->c_cflag &= ~CBAUD; 
1723         
1724         if (!Driver_Initialized)
1725                 return 0;
1726         
1727         if (scc->magic != SCC_MAGIC)
1728         {
1729                 printk("ERROR: scc_open(): bad magic number for device ("
1730                        "%s)",
1731                        kdevname(tty->device));
1732                 return -ENODEV;
1733         }               
1734         
1735         if(scc->tty != NULL)
1736         {
1737                 scc->tty_opened++;
1738                 return 0;
1739         }
1740 
1741         if(!scc->init) return 0;
1742                          
1743         scc->tty = tty;
1744         init_channel(scc);
1745 
1746         scc->stat.tx_kiss_state = KISS_IDLE;    /* don't change this... */
1747         scc->stat.rx_kiss_state = KISS_IDLE;    /* ...or this */
1748         
1749         scc_init_timer(scc);
1750         
1751         timer_table[SCC_TIMER].fn = scc_timer;
1752         timer_table[SCC_TIMER].expires = 0;     /* now! */
1753         timer_active |= 1 << SCC_TIMER;
1754         
1755         return 0;
1756 }
1757 
1758 
1759 /* ----> and this whenever you close the device <---- */
1760 
1761 static void
1762 scc_close(struct tty_struct *tty, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
1763 {
1764         struct scc_channel *scc = tty->driver_data;
1765         unsigned long flags;
1766 
1767         if (!scc || (scc->magic != SCC_MAGIC))
1768                 return;
1769         
1770         if(scc->tty_opened)
1771         {
1772                 scc->tty_opened--;
1773                 return;
1774         }
1775         
1776         tty->driver_data = NULLBUF;
1777         
1778         save_flags(flags); cli();
1779         
1780         Outb(scc->ctrl,0);              /* Make sure pointer is written */
1781         wr(scc,R1,0);                   /* disable interrupts */
1782         wr(scc,R3,0);
1783         
1784         scc->tty = NULL;
1785         
1786         restore_flags(flags);
1787         tty->stopped = 0;               
1788 }
1789 
1790 
1791 
1792 
1793 /*
1794  * change scc_speed
1795  */
1796  
1797 static void
1798 scc_change_speed(struct scc_channel * scc)
     /* [previous][next][first][last][top][bottom][index][help] */
1799 {
1800         if (scc->tty == NULL)
1801                 return;
1802                 
1803         scc->modem.speed = baud_table[scc->tty->termios->c_cflag & CBAUD];
1804         
1805         if (scc->stat.tx_state == 0)    /* only switch baudrate on rx... ;-) */
1806                 set_speed(scc);
1807 }
1808 
1809 
1810 /* ----> ioctl-routine of the driver <---- */
1811 
1812 /* perform ioctl on SCC (sdlc) channel
1813  * this is used for AX.25 mode, and will set the "kiss" parameters
1814  */
1815  
1816 /* TIOCMGET     - get modem status      arg: (unsigned long *) arg
1817  * TIOCMBIS     - set PTT               arg: ---
1818  * TIOCMBIC     - reset PTT             arg: ---
1819  * TIOCMBIC     - set PTT               arg: ---
1820  * TIOCSCCINI   - initialize driver     arg: ---
1821  * TIOCCHANINI  - initialize channel    arg: (struct scc_modem *) arg
1822  * TIOCGKISS    - get level 1 parameter arg: (struct ioctl_command *) arg
1823  * TIOCSKISS    - set level 1 parameter arg: (struct ioctl_command *) arg
1824  * TIOCSCCSTAT  - get driver status     arg: (struct scc_stat *) arg
1825  */
1826  
1827 
1828 static int
1829 scc_ioctl(struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1830 {
1831         struct scc_channel * scc = tty->driver_data;
1832         unsigned long flags, r;
1833         unsigned int result;
1834         unsigned int value;
1835         struct ioctl_command kiss_cmd;
1836         int error;
1837 
1838         if (scc->magic != SCC_MAGIC) 
1839         {
1840                 printk("ERROR: scc_ioctl(): bad magic number for device %s", 
1841                         kdevname(tty->device));
1842                         
1843                 return -ENODEV;
1844         }
1845                                                          
1846         r = NO_SUCH_PARAM;
1847         
1848         if (!Driver_Initialized)
1849         {
1850                 if (cmd == TIOCSCCINI)
1851                 {
1852                         if (!suser())
1853                                 return -EPERM;
1854                         
1855                         scc_alloc_buffer_pool();
1856                         z8530_init();
1857                         return 0;
1858                 }
1859                 
1860                 return -EINVAL; /* confuse the user */
1861         }
1862         
1863         if (!scc->init)
1864         {
1865 
1866                 if (cmd == TIOCCHANINI)
1867                 {
1868                         if (!arg)
1869                                 return -EFAULT;
1870                                 
1871                         if (!suser())
1872                                 return -EPERM;
1873                         
1874                         memcpy_fromfs(&scc->modem, (void *) arg, sizeof(struct scc_modem));
1875                         
1876                         /* default KISS Params */
1877                 
1878                         if (scc->modem.speed < 4800)
1879                         {
1880                                 scc->kiss.txdelay = 36*TPS/100;    /* 360 ms */
1881                                 scc->kiss.persist = 42;            /* 25% persistence */                        /* was 25 */
1882                                 scc->kiss.slottime = 16*TPS/100;   /* 160 ms */
1883                                 scc->kiss.tailtime = 4;            /* minimal reasonable value */
1884                                 scc->kiss.fulldup = 0;             /* CSMA */
1885                                 scc->kiss.waittime = 50*TPS/100;   /* 500 ms */
1886                                 scc->kiss.maxkeyup = 10;           /* 10 s */
1887                                 scc->kiss.mintime = 3;             /* 3 s */
1888                                 scc->kiss.idletime = 30;           /* 30 s */
1889                                 scc->kiss.maxdefer = 120;          /* 2 min */
1890                                 scc->kiss.not_slip = 1;            /* KISS mode */
1891                                 scc->kiss.softdcd = 0;             /* hardware dcd */
1892                         } else {
1893                                 scc->kiss.txdelay = 10*TPS/100;    /* 100 ms */
1894                                 scc->kiss.persist = 64;            /* 25% persistence */                        /* was 25 */
1895                                 scc->kiss.slottime = 8*TPS/100;    /* 160 ms */
1896                                 scc->kiss.tailtime = 1;            /* minimal reasonable value */
1897                                 scc->kiss.fulldup = 0;             /* CSMA */
1898                                 scc->kiss.waittime = 50*TPS/100;   /* 500 ms */
1899                                 scc->kiss.maxkeyup = 7;            /* 7 s */
1900                                 scc->kiss.mintime = 3;             /* 3 s */
1901                                 scc->kiss.idletime = 30;           /* 30 s */
1902                                 scc->kiss.maxdefer = 120;          /* 2 min */
1903                                 scc->kiss.not_slip = 1;            /* KISS mode */
1904                                 scc->kiss.softdcd = 0;             /* hardware dcd */
1905                         }
1906                         
1907                         scc->init = 1;                  
1908                         
1909                         return 0;
1910                 }
1911                 
1912                 return -EINVAL;
1913         }
1914 
1915         switch(cmd){
1916         case TCSBRK:
1917                 return 0;
1918         case TIOCMGET:
1919                 error = verify_area(VERIFY_WRITE, (void *) arg,sizeof(unsigned int *));
1920                 if (error)
1921                         return error;
1922 
1923                 save_flags(flags); cli();
1924                 
1925                 result =  ((scc->wreg[R5] & RTS) ? TIOCM_RTS : 0)
1926                         | ((scc->wreg[R5] & DTR) ? TIOCM_DTR : 0)
1927                         | ((InReg(scc->ctrl,R0) & DCD)  ? TIOCM_CAR : 0)
1928                         | ((InReg(scc->ctrl,R0) & CTS)  ? TIOCM_CTS : 0);
1929                 
1930                 restore_flags(flags);
1931                         
1932                 put_user_long(result,(unsigned int *) arg);
1933                 return 0;
1934         case TIOCMBIS:
1935         case TIOCMBIC:
1936         case TIOCMSET:
1937                 switch (cmd) {
1938                 case TIOCMBIS:
1939                         scc->wreg[R5] |= DTR;
1940                         scc->wreg[R5] |= RTS;
1941                         break;
1942                 case TIOCMBIC:
1943                         scc->wreg[R5] &= ~DTR;
1944                         scc->wreg[R5] &= ~RTS;
1945                         break;
1946                 case TIOCMSET:
1947                         value = get_user_long((unsigned int *) arg);
1948                         
1949                         if(value & TIOCM_DTR)
1950                                 scc->wreg[R5] |= DTR;
1951                         else
1952                                 scc->wreg[R5] &= ~DTR;
1953                         if(value & TIOCM_RTS)
1954                                 scc->wreg[R5] |= RTS;
1955                         else
1956                                 scc->wreg[R5] &= ~RTS;
1957                         break;
1958                 }
1959                 
1960                 save_flags(flags); cli();
1961                 
1962                 if(scc->stat.tx_state == TXS_IDLE && !Running(t_idle))
1963                         maxk_idle_timeout(scc);
1964                         
1965                 restore_flags(flags);
1966                 
1967                 return 0;
1968                 
1969         case TCGETS:
1970                 error = verify_area(VERIFY_WRITE, (void *) arg, sizeof(struct termios));
1971                 if (error)
1972                         return error;
1973                 if (!arg) 
1974                         return -EFAULT;
1975                         
1976                 memcpy_tofs((void *) arg, scc->tty->termios, sizeof(struct termios));
1977                 return 0;
1978                 
1979         case TCSETS:
1980         case TCSETSF:           /* should flush first, but... */
1981         case TCSETSW:           /* should wait 'till flush, but... */
1982                 if (!suser())
1983                         return -EPERM;
1984                 if (!arg)
1985                         return -EFAULT;
1986                 
1987                 memcpy_fromfs(scc->tty->termios, (void *) arg, sizeof(struct termios));
1988                 scc_change_speed(scc);
1989                 return 0;
1990                 
1991                 
1992         case TIOCSCCSTAT:
1993                 error = verify_area(VERIFY_WRITE, (void *) arg,sizeof(struct scc_stat));
1994                 if (error)
1995                         return error;
1996                 
1997                 if (!arg)
1998                         return -EFAULT;
1999                         
2000                 scc->stat.used_buf = scc_count_used_buffers(&scc->stat.rx_alloc, 
2001                                                             &scc->stat.tx_alloc);
2002                         
2003                 memcpy_tofs((void *) arg, &scc->stat, sizeof(struct scc_stat));
2004                 return 0;
2005                 
2006 #define TICKS (100/TPS)
2007 #define CAST(x) (unsigned long)(x)
2008 #define  Val    kiss_cmd.param
2009 #define  VAL    kiss_cmd.param*TPS/100
2010 #define SVAL    kiss_cmd.param? kiss_cmd.param:TIMER_STOPPED
2011 
2012         case TIOCGKISS:
2013                 error = verify_area(VERIFY_WRITE, (void *) arg,sizeof(struct ioctl_command));
2014                 if (error)
2015                         return error;
2016                         
2017                 if (!arg)
2018                         return -EFAULT;
2019                         
2020                 memcpy_fromfs(&kiss_cmd, (void *) arg, sizeof(struct ioctl_command));
2021 
2022                 switch (kiss_cmd.command)
2023                 {
2024                         case PARAM_TXDELAY:     r = CAST(scc->kiss.txdelay*TICKS);      break;
2025                         case PARAM_PERSIST:     r = CAST(scc->kiss.persist);            break;
2026                         case PARAM_SLOTTIME:    r = CAST(scc->kiss.slottime*TICKS);     break;
2027                         case PARAM_TXTAIL:      r = CAST(scc->kiss.tailtime*TICKS);     break;
2028                         case PARAM_FULLDUP:     r = CAST(scc->kiss.fulldup);            break;
2029                         case PARAM_SOFTDCD:     r = CAST(scc->kiss.softdcd);            break;
2030                         case PARAM_DTR:         r = CAST((scc->wreg[R5] & DTR)? 1:0); break;
2031                         case PARAM_RTS:         r = CAST((scc->wreg[R5] & RTS)? 1:0); break;
2032                         case PARAM_SPEED:       r = CAST(scc->modem.speed);     break;
2033                         case PARAM_GROUP:       r = CAST(scc->kiss.group);              break;
2034                         case PARAM_IDLE:        r = CAST(scc->kiss.idletime);           break;
2035                         case PARAM_MIN:         r = CAST(scc->kiss.mintime);            break;
2036                         case PARAM_MAXKEY:      r = CAST(scc->kiss.maxkeyup);           break;
2037                         case PARAM_WAIT:        r = CAST(scc->kiss.waittime);           break;
2038                         case PARAM_MAXDEFER:    r = CAST(scc->kiss.maxdefer);           break;
2039                         case PARAM_TX:          r = CAST(scc->kiss.tx_inhibit); break;
2040                         case PARAM_SLIP:        r = CAST(!scc->kiss.not_slip);          break;
2041                         default:                r = NO_SUCH_PARAM;
2042                 }
2043                 
2044                 kiss_cmd.param = r;
2045                 
2046                 memcpy_tofs((void *) arg, &kiss_cmd, sizeof(struct ioctl_command));
2047                 return 0;
2048                 break;
2049                 
2050         case TIOCSKISS:
2051                 if (!arg)
2052                         return -EFAULT;
2053 
2054                 if (!suser())
2055                         return -EPERM;
2056                         
2057                 memcpy_fromfs(&kiss_cmd, (void *) arg, sizeof(struct ioctl_command));
2058                 
2059                 switch (kiss_cmd.command)
2060                 {
2061                         case PARAM_TXDELAY:     scc->kiss.txdelay=VAL;          break;
2062                         case PARAM_PERSIST:     scc->kiss.persist=Val;          break;
2063                         case PARAM_SLOTTIME:    scc->kiss.slottime=VAL;         break;
2064                         case PARAM_TXTAIL:      scc->kiss.tailtime=VAL;         break;
2065                         case PARAM_FULLDUP:     scc->kiss.fulldup=Val;          break;
2066                         case PARAM_SOFTDCD:     scc->kiss.softdcd=Val;          break;
2067                         case PARAM_DTR:         break; /* does someone need this? */
2068                         case PARAM_RTS:         break; /* or this? */
2069                         case PARAM_SPEED:       scc->modem.speed=Val;           break;
2070                         case PARAM_GROUP:       scc->kiss.group=Val;            break;
2071                         case PARAM_IDLE:        scc->kiss.idletime=Val;         break;
2072                         case PARAM_MIN:         scc->kiss.mintime=SVAL;         break;
2073                         case PARAM_MAXKEY:      scc->kiss.maxkeyup=SVAL;        break;
2074                         case PARAM_WAIT:        scc->kiss.waittime=Val;         break;
2075                         case PARAM_MAXDEFER:    scc->kiss.maxdefer=SVAL;        break;
2076                         case PARAM_TX:          scc->kiss.tx_inhibit=Val;       break;
2077                         case PARAM_SLIP:        scc->kiss.not_slip=!Val;        break;
2078                         default:                return -ENOIOCTLCMD;
2079                 }
2080                 
2081                 return 0;
2082                 break;
2083 #undef TICKS
2084 #undef CAST
2085 #undef VAL
2086 #undef SVAL
2087 #undef Val
2088                 
2089         default:
2090                 return -ENOIOCTLCMD;
2091     }
2092 }
2093 
2094 
2095 /* ----- TERMIOS function ----- */
2096 
2097 static void
2098 scc_set_termios(struct tty_struct * tty, struct termios * old_termios)
     /* [previous][next][first][last][top][bottom][index][help] */
2099 {
2100         if (tty->termios->c_cflag == old_termios->c_cflag) 
2101                 return;
2102         scc_change_speed(tty->driver_data);
2103 }
2104 
2105 
2106 static inline void check_tx_queue(register struct scc_channel *scc)
     /* [previous][next][first][last][top][bottom][index][help] */
2107 {
2108         register struct mbuf *bp;
2109         
2110         if (scc->stat.tx_queued > QUEUE_THRES)
2111         {
2112                 if (scc->sndq1 == NULLBUF)
2113                 {
2114                         printk("z8530drv: Warning - scc->stat.tx_queued shows overflow"
2115                                " (%d) but queue is empty\n", scc->stat.tx_queued);
2116                                
2117                         scc->stat.tx_queued = 0;        /* correct it */
2118                         scc->stat.nospace = 54321;      /* draw attention to it */
2119                         return;
2120                 }
2121                         
2122                 bp = scc->sndq1->anext; /* don't use the one we currently use */
2123                 
2124                 while (bp && (scc->stat.tx_queued > QUEUE_HYST))
2125                 {
2126                         bp = scc_free_chain(bp, BT_TRANSMIT);
2127                         scc->stat.tx_queued--;
2128                         scc->stat.nospace++;
2129                 }
2130                 
2131                 scc->sndq1->anext = bp;
2132         }
2133 }
2134 
2135 
2136 
2137 /* ----> tx routine: decode KISS data and scc_enqueue it <---- */
2138 
2139 /* send raw frame to SCC. used for AX.25 */
2140 int scc_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
2141 {
2142         struct scc_channel * scc = tty->driver_data;
2143         unsigned char tbuf[BUFSIZE], *p;
2144         int cnt, cnt2;
2145         
2146         if (!tty) return count;
2147         
2148         if (scc_paranoia_check(scc, tty->device, "scc_write"))
2149                 return 0;
2150 
2151         if (scc->kiss.tx_inhibit) return count;
2152         
2153         check_tx_queue(scc);
2154 
2155         cnt2 = count;
2156         
2157         while (cnt2)
2158         {
2159                 cnt   = cnt2 > BUFSIZE? BUFSIZE:cnt2;
2160                 cnt2 -= cnt;
2161                 
2162                 if (from_user)
2163                         memcpy_fromfs(tbuf, buf, cnt);
2164                 else
2165                         memcpy(tbuf, buf, cnt);
2166                 
2167                 buf += cnt;
2168                         
2169                 p=tbuf;
2170                 
2171                 while(cnt--) 
2172                   if (kiss_decode(scc, *p++))
2173                   {
2174                         scc->stat.nospace++;
2175                         return 0;
2176                   }
2177                         
2178         } /* while cnt2 */
2179         
2180         return count;
2181 }
2182                                 
2183 
2184 /* put a single char into the buffer */
2185 
2186 static void scc_put_char(struct tty_struct * tty, unsigned char ch)
     /* [previous][next][first][last][top][bottom][index][help] */
2187 {
2188         struct scc_channel *scc = tty->driver_data;
2189         unsigned char ch2;
2190         
2191         if (scc_paranoia_check(scc, tty->device, "scc_put_char"))
2192                 return;
2193                 
2194         ch2 = ch;
2195         scc_write(tty, 0, &ch2, 1);     /* that's all */
2196 }
2197 
2198 static void scc_flush_chars(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
2199 {
2200         struct scc_channel *scc = tty->driver_data;
2201         
2202         scc_paranoia_check(scc, tty->device, "scc_flush_chars"); /* just to annoy the user... */
2203         
2204         return; /* no flush needed */
2205 }
2206 
2207 /* the kernel does NOT use this routine yet... */
2208 
2209 static int scc_write_room(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
2210 {
2211         struct scc_channel *scc = tty->driver_data;
2212         
2213         if (scc_paranoia_check(scc, tty->device, "scc_write_room"))
2214                 return 0;
2215         
2216         if (scc->stat.tx_alloc >= QUEUE_THRES)
2217         {
2218                 printk("scc_write_room(): buffer full (ignore)\n");
2219                 return 0;
2220         }
2221                 
2222         return BUFSIZE;
2223 }
2224 
2225 static int scc_chars_in_buffer(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
2226 {
2227         struct scc_channel *scc = tty->driver_data;
2228         
2229         if (scc && scc->sndq2)
2230                 return scc->sndq2->cnt;
2231         else
2232                 return 0;
2233 }
2234 
2235 static void scc_flush_buffer(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
2236 {
2237         struct scc_channel *scc = tty->driver_data;
2238         
2239         if (scc_paranoia_check(scc, tty->device, "scc_flush_buffer"))
2240                 return;
2241                 
2242         scc->stat.tx_kiss_state = KISS_IDLE;
2243         
2244         wake_up_interruptible(&tty->write_wait);
2245         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
2246             tty->ldisc.write_wakeup)
2247                 (tty->ldisc.write_wakeup)(tty);
2248 }
2249 
2250 static void scc_throttle(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
2251 {
2252         struct scc_channel *scc = tty->driver_data;
2253         
2254         if (scc_paranoia_check(scc, tty->device, "scc_throttle"))
2255                 return;
2256                 
2257                 
2258         /* dummy */
2259 }
2260 
2261 static void scc_unthrottle(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
2262 {
2263         struct scc_channel *scc = tty->driver_data;
2264         
2265         if (scc_paranoia_check(scc, tty->device, "scc_unthrottle"))
2266                 return;
2267                 
2268         /* dummy */
2269 }
2270 
2271 static void scc_start(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
2272 {
2273         struct scc_channel *scc = tty->driver_data;
2274         
2275         if (scc_paranoia_check(scc, tty->device, "scc_start"))
2276                 return;
2277                 
2278         /* dummy */
2279 }
2280                                                         
2281 
2282 static void scc_stop(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
2283 {
2284         struct scc_channel *scc = tty->driver_data;
2285         
2286         if (scc_paranoia_check(scc, tty->device, "scc_stop"))
2287                 return;
2288                 
2289         /* dummy */
2290 }
2291 
2292 
2293 /* ******************************************************************** */
2294 /* *                    Init SCC driver                               * */
2295 /* ******************************************************************** */
2296 
2297 int scc_init (void)
     /* [previous][next][first][last][top][bottom][index][help] */
2298 {
2299         int chip, chan;
2300         register io_port ctrl;
2301         long flags;
2302         
2303         
2304         memset(&scc_driver, 0, sizeof(struct tty_driver));
2305         scc_driver.magic = TTY_DRIVER_MAGIC;
2306         scc_driver.name = "sc";
2307         scc_driver.major = Z8530_MAJOR;         
2308         scc_driver.minor_start = 0;
2309         scc_driver.num = Nchips*2;
2310         scc_driver.type = TTY_DRIVER_TYPE_SERIAL;
2311         scc_driver.subtype = 0;                 /* not needed */
2312         scc_driver.init_termios = tty_std_termios;
2313         scc_driver.init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2314         scc_driver.flags = TTY_DRIVER_REAL_RAW;
2315         scc_driver.refcount = &scc_refcount;    /* not needed yet */
2316         scc_driver.table = scc_table;
2317         scc_driver.termios = (struct termios **) scc_termios;
2318         scc_driver.termios_locked = (struct termios **) scc_termios_locked;
2319         scc_driver.open = scc_open;
2320         scc_driver.close = scc_close;
2321         scc_driver.write = scc_write;
2322         scc_driver.start = scc_start;
2323         scc_driver.stop = scc_stop;
2324         
2325         scc_driver.put_char = scc_put_char;
2326         scc_driver.flush_chars = scc_flush_chars;        
2327         scc_driver.write_room = scc_write_room;
2328         scc_driver.chars_in_buffer = scc_chars_in_buffer;
2329         scc_driver.flush_buffer = scc_flush_buffer;
2330         
2331         scc_driver.throttle = scc_throttle;
2332         scc_driver.unthrottle = scc_unthrottle;
2333         
2334         scc_driver.ioctl = scc_ioctl;
2335         scc_driver.set_termios = scc_set_termios;
2336         
2337         if (tty_register_driver(&scc_driver))
2338            panic("Couldn't register Z8530 SCC driver\n");
2339                                 
2340         printk (BANNER);
2341         
2342         if (Nchips > MAXSCC) Nchips = MAXSCC;   /* to avoid the "DAU" (duemmster anzunehmender User) */
2343 
2344         /* reset and pre-init all chips in the system */
2345         
2346         for (chip = 0; chip < Nchips; chip++)
2347         {
2348                 memset((char *) &SCC_Info[2*chip  ], 0, sizeof(struct scc_channel));
2349                 memset((char *) &SCC_Info[2*chip+1], 0, sizeof(struct scc_channel));
2350                 
2351                 ctrl = SCC_ctrl[chip * 2];
2352                 if (!ctrl) continue;
2353 
2354                 save_flags(flags); cli();       /* because of 2-step accesses */
2355                 
2356 
2357 /* Hmm... this may fail on fast systems with cards who don't delay the INTACK */
2358 /* If you are sure you specified the right port addresses and the driver doesn't */
2359 /* recognize the chips, define DONT_CHECK in scc_config.h */
2360 
2361 #ifndef DONT_CHECK
2362                 check_region(ctrl, 1);
2363 
2364                 Outb(ctrl, 0);
2365                 OutReg(ctrl,R13,0x55);          /* is this chip realy there? */
2366                 
2367                 if (InReg(ctrl,R13) != 0x55 )
2368                 {
2369                         restore_flags(flags);
2370                         continue;
2371                 }
2372 #endif
2373                 
2374                 SCC_Info[2*chip  ].magic    = SCC_MAGIC;
2375                 SCC_Info[2*chip  ].ctrl     = SCC_ctrl[2*chip];
2376                 SCC_Info[2*chip  ].data     = SCC_data[2*chip];
2377                 SCC_Info[2*chip  ].enhanced = SCC_Enhanced[chip];
2378                         
2379                 SCC_Info[2*chip+1].magic    = SCC_MAGIC;
2380                 SCC_Info[2*chip+1].ctrl     = SCC_ctrl[2*chip+1];
2381                 SCC_Info[2*chip+1].data     = SCC_data[2*chip+1];
2382                 SCC_Info[2*chip+1].enhanced = SCC_Enhanced[chip];
2383 
2384  
2385                 restore_flags(flags);
2386         }
2387 
2388 #ifdef DO_FAST_RX
2389         rx_timer_cb.lock = 0;
2390 #else
2391         rx_timer_cb.lock = 1;
2392 #endif
2393         
2394 #ifdef VERBOSE_BOOTMSG
2395         printk("Init Z8530 driver: %u channels, using irq %u\n",Nchips*2,Ivec);
2396         
2397 
2398         for (chan = 0; chan < Nchips * 2 ; chan++)
2399         {
2400                 printk("/dev/%s%i: data port = 0x%3.3x  control port = 0x%3.3x -- %s\n",
2401                         scc_driver.name, chan, SCC_data[chan], SCC_ctrl[chan],
2402                         SCC_Info[chan].ctrl? "found"   : "missing");
2403                         
2404                 if (SCC_Info[chan].ctrl == 0) 
2405                 {
2406                         SCC_ctrl[chan] = 0;
2407                 } else {
2408                         request_region(SCC_ctrl[chan], 1, "scc ctrl");
2409                         request_region(SCC_data[chan], 1, "scc data");
2410                 }
2411         }
2412 #else
2413         printk("Init Z8530 driver: %u channels\n",Nchips*2);
2414 #endif
2415 
2416         
2417         return 0;
2418 }

/* [previous][next][first][last][top][bottom][index][help] */