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. flush_FIFO
  18. scc_exint
  19. scc_rxint
  20. scc_spint
  21. set_brg
  22. set_speed
  23. init_brg
  24. init_channel
  25. scc_key_trx
  26. is_grouped
  27. dw_slot_timeout
  28. txdel_timeout
  29. tail_timeout
  30. busy_timeout
  31. maxk_idle_timeout
  32. scc_timer
  33. scc_init_timer
  34. kiss_set_param
  35. kiss_interpret_frame
  36. kiss_store_byte
  37. kiss_decode
  38. z8530_init
  39. scc_paranoia_check
  40. scc_open
  41. scc_close
  42. scc_change_speed
  43. scc_ioctl
  44. scc_set_termios
  45. check_tx_queue
  46. scc_write
  47. scc_put_char
  48. scc_flush_chars
  49. scc_write_room
  50. scc_chars_in_buffer
  51. scc_flush_buffer
  52. scc_throttle
  53. scc_unthrottle
  54. scc_start
  55. scc_stop
  56. scc_hangup
  57. scc_init

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

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