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_enqueue_buffer
  7. scc_dequeue_buffer
  8. alloc_buffer_pool
  9. free_buffer_pool
  10. scc_get_buffer
  11. scc_isr_dispatch
  12. scc_isr
  13. scc_txint
  14. flush_FIFO
  15. scc_exint
  16. scc_rxint
  17. kick_rx_timer
  18. scc_spint
  19. set_brg
  20. set_speed
  21. init_brg
  22. init_channel
  23. scc_key_trx
  24. is_grouped
  25. dw_slot_timeout
  26. txdel_timeout
  27. tail_timeout
  28. busy_timeout
  29. maxk_idle_timeout
  30. scc_tx_timer
  31. scc_rx_timer
  32. scc_init_timer
  33. kiss_set_param
  34. kiss_interpret_frame
  35. kiss_store_byte
  36. kiss_decode
  37. kiss_encode
  38. z8530_init
  39. scc_paranoia_check
  40. scc_open
  41. scc_close
  42. scc_change_speed
  43. scc_ioctl
  44. scc_write
  45. scc_put_char
  46. scc_flush_chars
  47. scc_write_room
  48. scc_chars_in_buffer
  49. scc_flush_buffer
  50. scc_throttle
  51. scc_unthrottle
  52. scc_start
  53. scc_stop
  54. scc_set_termios
  55. scc_set_ldisc
  56. scc_init
  57. init_module
  58. cleanup_module

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

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