root/drivers/char/scc.c

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

DEFINITIONS

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

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

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