OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [drivers/] [char/] [scc.c] - Blame information for rev 1772

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1626 jcastillo
#define RCS_ID "$Id: scc.c,v 1.1 2005-12-20 10:16:40 jcastillo Exp $"
2
 
3
#define BANNER "Z8530 SCC driver version 2.4c.dl1bke (experimental) 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, 1997 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
   Incomplete history of z8530drv:
53
   -------------------------------
54
 
55
   940913       - started to write the driver, rescued most of my own
56
                  code (and Hans Alblas' memory buffer pool concept) from
57
                  an earlier project "sccdrv" which was initiated by
58
                  Guido ten Dolle. Not much of the old driver survived,
59
                  though. The first version I put my hands on was sccdrv1.3
60
                  from August 1993. The memory buffer pool concept
61
                  appeared in an unauthorized sccdrv version (1.5) from
62
                  August 1994.
63
 
64
   950131       - changed copyright notice to GPL without limitations.
65
 
66
     .
67
     .  <snip!>
68
     .
69
 
70
   951114       - rewrote memory management, took first steps to allow
71
                  compilation as a module. Well, it looks like a whole
72
                  new driver now.
73
 
74
   960413       - Heiko Eissfeld fixed missing verify_area() calls
75
 
76
   960507       - the driver may be used as a network driver now.
77
                  (for kernel AX.25).
78
 
79
   960512       - added scc_net_ioctl(), restructured ioctl routines
80
                  added /proc filesystem support
81
 
82
   960517       - fixed fullduplex mode 2 operation and waittime bug
83
 
84
   960518       - added fullduplex mode 3 to allow direct hardware
85
                  access via protocol layer. Removed kiss.not_slip,
86
                  I do not think someone really needed it.
87
 
88
   960607       - switching off receiver while transmission in halfduplex
89
                  mode with external MODEM clock.
90
 
91
   960715       - rewrote interrupt service routine for "polling" mode,
92
                  fixed bug in grouping algorithm.
93
 
94
   960719       - New transmit timer routines (let's see what it breaks),
95
                  some improvements regarding DCD and SYNC interrupts,
96
                  clean-up of printk().
97
 
98
   960725       - Fixed Maxkeyup problems. Will generate HDLC abort and
99
                  recover now.
100
 
101
   960808       - Maxkeyup will set dev->tbusy until all remaining frames
102
                  are sent.
103
 
104
   970115       - Fixed return values in scc_net_tx(), added missing
105
                  scc_enqueue_buffer()
106
 
107
   970410       - Fixed problem with new *_timer() code in sched.h for
108
                  kernel 2.0.30.
109
 
110
   Thanks to:
111
   ----------
112
 
113
   PE1CHL Rob     - for a lot of good ideas from his SCC driver for DOS
114
   PE1NNZ Guido   - for his port of the original driver to Linux
115
   KA9Q   Phil    - from whom we stole the mbuf-structure
116
   PA3AYX Hans    - for some useful changes
117
   DL8MBT Flori   - for support
118
   DG0FT  Rene    - for the BayCom USCC support
119
   PA3AOU Harry   - for ESCC testing, information supply and support
120
   Heiko Eissfeld - verify_area() clean-up
121
   DL3KHB Klaus   - fullduplex mode 2 bug-hunter
122
   PA3GCU Richard - for support and finding various bugs.
123
 
124
   PE1KOX Rob, DG1RTF Thomas, ON5QK Roland, G4XYW Andy, Linus,
125
   GW4PTS Alan, EI9GL Paul, G0DZB Peter, G8IMB Martin
126
 
127
   and all who sent me bug reports and ideas...
128
 
129
 
130
   NB -- if you find errors, change something, please let me know
131
         first before you distribute it... And please don't touch
132
         the version number. Just replace my callsign in
133
         "v2.4c.dl1bke" with your own. Just to avoid confusion...
134
 
135
   If you want to add your modification to the linux distribution
136
   please (!) contact me first.
137
 
138
   New versions of the driver will be announced on the linux-hams
139
   mailing list on vger.rutgers.edu. To subscribe send an e-mail
140
   to majordomo@vger.rutgers.edu with the following line in
141
   the body of the mail:
142
 
143
           subscribe linux-hams
144
 
145
   The content of the "Subject" field will be ignored.
146
 
147
   vy 73,
148
   Joerg Reuter ampr-net: dl1bke@db0pra.ampr.org
149
                AX-25   : DL1BKE @ DB0ACH.#NRW.DEU.EU
150
                Internet: jreuter@poboxes.com
151
*/
152
 
153
/* ----------------------------------------------------------------------- */
154
 
155
#undef  SCC_DELAY       /* perhaps your ISA bus is a *bit* too fast? */
156
#undef  SCC_LDELAY 1    /* slow it even a bit more down */
157
#undef  DONT_CHECK      /* don't look if the SCCs you specified are available */
158
 
159
#define MAXSCC          4       /* number of max. supported chips */
160
#define RXBUFFERS       8       /* default number of RX buffers */
161
#define TXBUFFERS       8       /* default number of TX buffers */
162
#define BUFSIZE         384     /* must not exceed 4096-sizeof(mbuf) */
163
#undef  DISABLE_ALL_INTS        /* use cli()/sti() in ISR instead of */
164
                                /* enable_irq()/disable_irq()        */
165
#undef  SCC_DEBUG
166
 
167
#define DEFAULT_CLOCK   4915200 /* default pclock if nothing is specified */
168
 
169
/* ----------------------------------------------------------------------- */
170
 
171
#include <linux/config.h> /* CONFIG_INET and CONFIG_SCC_* */
172
#include <linux/module.h>
173
#include <linux/errno.h>
174
#include <linux/signal.h>
175
#include <linux/sched.h>
176
#include <linux/timer.h>
177
#include <linux/tqueue.h>
178
#include <linux/tty.h>
179
#include <linux/tty_driver.h>
180
#include <linux/tty_flip.h>
181
#include <linux/major.h>
182
#include <linux/termios.h>
183
#include <linux/serial.h>
184
#include <linux/interrupt.h>
185
#include <linux/ioport.h>
186
#include <linux/string.h>
187
#include <linux/in.h>
188
#include <linux/fcntl.h>
189
#include <linux/ptrace.h>
190
#include <linux/malloc.h>
191
#include <linux/delay.h>
192
 
193
#include <linux/skbuff.h>
194
#include <linux/netdevice.h>
195
#include <linux/if_ether.h>
196
#include <linux/if_arp.h>
197
#include <linux/socket.h>
198
 
199
#ifdef CONFIG_SCC_STANDALONE
200
#include "scc.h"
201
#else
202
#include <linux/scc.h>
203
#endif
204
 
205
#include <net/ax25.h>
206
#include <asm/irq.h>
207
#include <asm/system.h>
208
#include <asm/io.h>
209
#include <asm/segment.h>
210
#include <asm/bitops.h>
211
 
212
#include <linux/kernel.h>
213
#include <linux/proc_fs.h>
214
 
215
#ifdef MODULE
216
int init_module(void);
217
void cleanup_module(void);
218
#endif
219
 
220
#ifndef Z8530_MAJOR
221
#define Z8530_MAJOR 34
222
#endif
223
 
224
#ifndef PROC_NET_Z8530
225
#define PROC_NET_Z8530 PROC_NET_LAST+10         /* sorry... */
226
#endif
227
 
228
#if !defined(CONFIG_SCC_DEV) && !defined(CONFIG_SCC_TTY)
229
#define CONFIG_SCC_DEV
230
#define CONFIG_SCC_TTY
231
#endif
232
 
233
int scc_init(void);
234
 
235
static struct mbuf * scc_enqueue_buffer(struct mbuf **queue, struct mbuf * buffer);
236
static struct mbuf * scc_dequeue_buffer(struct mbuf **queue);
237
static void alloc_buffer_pool(struct scc_channel *scc);
238
static void free_buffer_pool(struct scc_channel *scc);
239
static struct mbuf * scc_get_buffer(struct scc_channel *scc, char type);
240
 
241
#ifdef CONFIG_SCC_TTY
242
int scc_open(struct tty_struct *tty, struct file *filp);
243
static void scc_close(struct tty_struct *tty, struct file *filp);
244
int scc_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count);
245
static void scc_put_char(struct tty_struct *tty, unsigned char ch);
246
static void scc_flush_chars(struct tty_struct *tty);
247
static int scc_write_room(struct tty_struct *tty);
248
static int scc_chars_in_buffer(struct tty_struct *tty);
249
static void scc_flush_buffer(struct tty_struct *tty);
250
static int scc_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
251
static void scc_set_termios(struct tty_struct *tty, struct termios *old_termios);
252
static void scc_set_ldisc(struct tty_struct *tty);
253
static void scc_throttle(struct tty_struct *tty);
254
static void scc_unthrottle(struct tty_struct *tty);
255
static void scc_start(struct tty_struct *tty);
256
static void scc_stop(struct tty_struct *tty);
257
static void kiss_encode(struct scc_channel *scc);
258
static void scc_rx_timer(unsigned long);
259
static void scc_change_speed(struct scc_channel *scc);
260
#endif
261
 
262
static void t_dwait(unsigned long);
263
static void t_txdelay(unsigned long);
264
static void t_tail(unsigned long);
265
static void t_busy(unsigned long);
266
static void t_maxkeyup(unsigned long);
267
static void t_idle(unsigned long);
268
static void scc_tx_done(struct scc_channel *);
269
static void scc_start_tx_timer(struct scc_channel *, void (*)(unsigned long), unsigned long);
270
static void scc_start_maxkeyup(struct scc_channel *);
271
static void scc_start_defer(struct scc_channel *);
272
 
273
static int scc_ioctl(struct scc_channel *scc, unsigned int cmd, void * arg);
274
 
275
static void z8530_init(void);
276
 
277
static void init_channel(struct scc_channel *scc);
278
static void scc_key_trx (struct scc_channel *scc, char tx);
279
static void scc_isr(int irq, void *dev_id, struct pt_regs *regs);
280
static void scc_init_timer(struct scc_channel *scc);
281
 
282
#ifdef CONFIG_SCC_DEV
283
static int scc_net_setup(struct scc_channel *scc, unsigned char *name);
284
static void scc_net_rx(struct scc_channel *scc, struct mbuf *bp);
285
#endif
286
 
287
static unsigned char *SCC_DriverName = "scc";
288
 
289
#ifdef CONFIG_SCC_TTY
290
static int baud_table[] = {
291
        0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
292
        9600, 19200, 38400, 57600, 115200, 0 };
293
 
294
struct semaphore scc_sem = MUTEX;
295
static struct termios scc_std_termios;
296
unsigned char scc_wbuf[BUFSIZE];
297
 
298
struct tty_driver scc_driver;
299
static struct tty_struct *scc_table[2*MAXSCC];
300
static struct termios scc_termios[2 * MAXSCC];
301
static struct termios scc_termios_locked[2 * MAXSCC];
302
static int scc_refcount;
303
#endif
304
 
305
 
306
static struct irqflags { unsigned char used : 1; } Ivec[16];
307
 
308
static struct scc_channel SCC_Info[2 * MAXSCC]; /* information per channel */
309
 
310
static struct scc_ctrl {
311
        io_port chan_A;
312
        io_port chan_B;
313
        int irq;
314
} SCC_ctrl[MAXSCC+1];
315
 
316
static unsigned char Driver_Initialized = 0;
317
static int Nchips = 0;
318
static io_port Vector_Latch = 0;
319
 
320
#define SCC_DEVNAME (scc->stat.is_netdev? scc->dev->name:kdevname(scc->tty->device))
321
 
322
/* ******************************************************************** */
323
/* *                    Port Access Functions                         * */
324
/* ******************************************************************** */
325
 
326
/* These provide interrupt save 2-step access to the Z8530 registers */
327
 
328
static __inline__ unsigned char
329
InReg(io_port port, unsigned char reg)
330
{
331
        unsigned long flags;
332
        unsigned char r;
333
 
334
        save_flags(flags);
335
        cli();
336
#ifdef SCC_LDELAY
337
        Outb(port, reg);
338
        udelay(SCC_LDELAY);
339
        r=Inb(port);
340
        udelay(SCC_LDELAY);
341
#else
342
        Outb(port, reg);
343
        r=Inb(port);
344
#endif
345
        restore_flags(flags);
346
        return r;
347
}
348
 
349
static __inline__ void
350
OutReg(io_port port, unsigned char reg, unsigned char val)
351
{
352
        unsigned long flags;
353
 
354
        save_flags(flags);
355
        cli();
356
#ifdef SCC_LDELAY
357
        Outb(port, reg); udelay(SCC_LDELAY);
358
        Outb(port, val); udelay(SCC_LDELAY);
359
#else
360
        Outb(port, reg);
361
        Outb(port, val);
362
#endif
363
        restore_flags(flags);
364
}
365
 
366
static __inline__ void
367
wr(struct scc_channel *scc, unsigned char reg, unsigned char val)
368
{
369
        OutReg(scc->ctrl, reg, (scc->wreg[reg] = val));
370
}
371
 
372
static __inline__ void
373
or(struct scc_channel *scc, unsigned char reg, unsigned char val)
374
{
375
        OutReg(scc->ctrl, reg, (scc->wreg[reg] |= val));
376
}
377
 
378
static __inline__ void
379
cl(struct scc_channel *scc, unsigned char reg, unsigned char val)
380
{
381
        OutReg(scc->ctrl, reg, (scc->wreg[reg] &= ~val));
382
}
383
 
384
#ifdef DISABLE_ALL_INTS
385
static __inline__ void scc_cli(int irq)
386
{ cli(); }
387
static __inline__ void scc_sti(int irq)
388
{ sti(); }
389
#else
390
static __inline__ void scc_cli(int irq)
391
{ disable_irq(irq); }
392
static __inline__ void scc_sti(int irq)
393
{ enable_irq(irq); }
394
#endif
395
 
396
 
397
/* ******************************************************************** */
398
/* *                    Memory Buffer Management                        */
399
/* ******************************************************************** */
400
 
401
/*
402
 * The new buffer scheme uses a ring chain of buffers. This has the
403
 * advantage to access both, first and last element of the list, very
404
 * fast. It has the disadvantage to mess things up double if something
405
 * is wrong.
406
 *
407
 * We could have used the socket buffer routines from skbuff.h instead,
408
 * but our own routines keep the driver portable...
409
 *
410
 * Every scc channel has its own buffer pool now with an adjustable
411
 * number of buffers for transmit and receive buffers. The buffer
412
 * size remains the same for each AX.25 frame, but is (as a semi-
413
 * undocumented feature) adjustable within a range of 512 and 4096
414
 * to benefit experiments with higher frame lengths. If you need
415
 * larger frames... Well, you'd better choose a whole new concept
416
 * for that, like a DMA capable I/O card and a block device driver...
417
 *
418
 */
419
 
420
/* ------ Management of buffer queues ------ */
421
 
422
 
423
/* Insert a buffer at the "end" of the chain */
424
 
425
static struct mbuf *
426
scc_enqueue_buffer(struct mbuf **queue, struct mbuf * buffer)
427
{
428
        unsigned long flags;
429
        struct mbuf * anchor;
430
 
431
        save_flags(flags); cli();               /* do not disturb! */
432
 
433
        anchor = *queue;                        /* the anchor is the "start" of the chain */
434
 
435
        if (anchor == NULLBUF)                  /* found an empty list */
436
        {
437
                *queue = buffer;                /* new anchor */
438
                buffer->next = buffer->prev = NULLBUF;
439
        } else
440
        if (anchor->prev == NULLBUF)            /* list has one member only */
441
        {
442
                anchor->next = anchor->prev = buffer;
443
                buffer->next = buffer->prev = anchor;
444
        } else {                                /* every other case */
445
                buffer->prev = anchor->prev;    /* self explaining, isn't it? */
446
                buffer->next = anchor;
447
                anchor->prev->next = buffer;
448
                anchor->prev = buffer;
449
        }
450
 
451
        restore_flags(flags);
452
        return *queue;                          /* return "start" of chain */
453
}
454
 
455
 
456
 
457
/* Remove a buffer from the "start" of the chain an return it */
458
 
459
static struct mbuf *
460
scc_dequeue_buffer(struct mbuf **queue)
461
{
462
        unsigned long flags;
463
        struct mbuf *buffer;
464
 
465
        save_flags(flags); cli();
466
 
467
        buffer = *queue;                        /* head of the chain */
468
 
469
        if (buffer != NULLBUF)                  /* not an empty list? */
470
        {
471
                if (buffer->prev != NULLBUF)    /* not last buffer? */
472
                {
473
                        if (buffer->prev->next == buffer->prev->prev)
474
                        {                       /* only one buffer remaining... */
475
                                buffer->next->prev = NULLBUF;
476
                                buffer->next->next = NULLBUF;
477
                        } else {                /* the one remaining situation... */
478
                                buffer->next->prev = buffer->prev;
479
                                buffer->prev->next = buffer->next;
480
                        }
481
                }
482
 
483
                *queue = buffer->next;          /* new head of chain */
484
 
485
                buffer->next = NULLBUF;         /* for security only... */
486
                buffer->prev = NULLBUF;
487
                buffer->rw_ptr = buffer->data;
488
        }
489
 
490
        restore_flags(flags);
491
        return buffer;                          /* give it away... */
492
}
493
 
494
/* ------ buffer pool management ------ */
495
 
496
 
497
/* allocate buffer pool for a channel */
498
 
499
static void
500
alloc_buffer_pool(struct scc_channel *scc)
501
{
502
        int k;
503
        struct mbuf * bptr;
504
        int  buflen;
505
 
506
        if (scc->mempool)
507
        {
508
                printk(KERN_DEBUG "z8530drv: alloc_buffer_pool() had buffers already allocated\n");
509
                return;
510
        }
511
 
512
        buflen = sizeof(struct mbuf) + scc->stat.bufsize;
513
 
514
        if (scc->stat.bufsize <  336) scc->stat.bufsize = 336;
515
        if (buflen > 4096)
516
        {
517
                scc->stat.bufsize = 4096-sizeof(struct mbuf);
518
                buflen = 4096;
519
        }
520
 
521
        if (scc->stat.rxbuffers < 4)  scc->stat.rxbuffers = 4;
522
        if (scc->stat.txbuffers < 4)  scc->stat.txbuffers = 4;
523
 
524
 
525
        /* allocate receive buffers for this channel */
526
 
527
        for (k = 0; k < scc->stat.rxbuffers ; k++)
528
        {
529
                /* allocate memory for the struct and the buffer */
530
 
531
                bptr = (struct mbuf *) kmalloc(buflen, GFP_ATOMIC);
532
 
533
                /* should not happen, but who knows? */
534
 
535
                if (bptr == NULLBUF)
536
                {
537
                        printk(KERN_WARNING "z8530drv: %s: can't allocate memory for rx buffer pool", SCC_DEVNAME);
538
                        break;
539
                }
540
 
541
                /* clear memory */
542
 
543
                memset(bptr, 0, buflen);
544
 
545
                /* initialize structure */
546
 
547
                bptr->rw_ptr = bptr->data;
548
 
549
                /* and append the buffer to the pool */
550
 
551
                scc_enqueue_buffer(&scc->rx_buffer_pool, bptr);
552
        }
553
 
554
        /* now do the same for the transmit buffers */
555
 
556
        for (k = 0; k < scc->stat.txbuffers ; k++)
557
        {
558
                bptr = (struct mbuf *) kmalloc(buflen, GFP_ATOMIC);
559
 
560
                if (bptr == NULLBUF)
561
                {
562
                        printk(KERN_WARNING "z8530drv: %s: can't allocate memory for tx buffer pool", SCC_DEVNAME);
563
                        break;
564
                }
565
 
566
                memset(bptr, 0, buflen);
567
 
568
                bptr->rw_ptr = bptr->data;
569
 
570
                scc_enqueue_buffer(&scc->tx_buffer_pool, bptr);
571
        }
572
 
573
        scc->mempool = 1;
574
}
575
 
576
 
577
/* remove buffer pool */
578
 
579
static void
580
free_buffer_pool(struct scc_channel *scc)
581
{
582
        unsigned long flags;
583
        int rx_cnt, tx_cnt;
584
 
585
        if (!scc->mempool)
586
                return;
587
 
588
        /* this one is a bit tricky and probably dangerous. */
589
        rx_cnt = tx_cnt = 0;
590
 
591
        save_flags(flags); cli();
592
 
593
        /* esp. to free the buffers currently in use by ISR */
594
 
595
        if (scc->rx_bp != NULLBUF)
596
        {
597
                kfree(scc->rx_bp);
598
                scc->rx_bp = NULLBUF;
599
                rx_cnt++;
600
        }
601
 
602
        if (scc->tx_bp != NULLBUF)
603
        {
604
                kfree(scc->tx_bp);
605
                scc->tx_bp = NULLBUF;
606
                tx_cnt++;
607
        }
608
 
609
        if (scc->kiss_decode_bp != NULLBUF)
610
        {
611
                kfree(scc->kiss_decode_bp);
612
                scc->kiss_decode_bp = NULLBUF;
613
                tx_cnt++;
614
        }
615
 
616
#ifdef CONFIG_SCC_TTY
617
        if (scc->kiss_encode_bp != NULLBUF)
618
        {
619
                kfree(scc->kiss_encode_bp);
620
                scc->kiss_encode_bp = NULLBUF;
621
                rx_cnt++;
622
        }
623
#endif
624
 
625
        restore_flags(flags);
626
 
627
 
628
        while (scc->rx_queue != NULLBUF)
629
        {
630
                kfree(scc_dequeue_buffer(&scc->rx_queue));
631
                rx_cnt++;
632
        }
633
        scc->stat.rx_queued = 0;
634
 
635
        while (scc->tx_queue != NULLBUF)
636
        {
637
                kfree(scc_dequeue_buffer(&scc->tx_queue));
638
                tx_cnt++;
639
        }
640
        scc->stat.tx_queued = 0;
641
 
642
        while (scc->rx_buffer_pool != NULLBUF)
643
        {
644
                kfree(scc_dequeue_buffer(&scc->rx_buffer_pool));
645
                rx_cnt++;
646
        }
647
 
648
        while (scc->tx_buffer_pool != NULLBUF)
649
        {
650
                kfree(scc_dequeue_buffer(&scc->tx_buffer_pool));
651
                tx_cnt++;
652
        }
653
 
654
        if (rx_cnt < scc->stat.rxbuffers)       /* hmm... hmm... :-( */
655
                printk(KERN_ERR "z8530drv: oops, deallocated only %d of %d rx buffers\n", rx_cnt, scc->stat.rxbuffers);
656
        if (rx_cnt > scc->stat.rxbuffers)       /* WHAT?!! */
657
                printk(KERN_ERR "z8530drv: oops, deallocated %d instead of %d rx buffers. Very strange.\n", rx_cnt, scc->stat.rxbuffers);
658
 
659
        if (tx_cnt < scc->stat.txbuffers)
660
                printk(KERN_ERR "z8530drv: oops, deallocated only %d of %d tx buffers\n", tx_cnt, scc->stat.txbuffers);
661
        if (tx_cnt > scc->stat.txbuffers)
662
                printk(KERN_ERR "z8530drv: oops, deallocated %d instead of %d tx buffers. Very strange.\n", tx_cnt, scc->stat.txbuffers);
663
 
664
        scc->mempool = 0;
665
}
666
 
667
 
668
/* ------ rx/tx buffer management ------ */
669
 
670
/*
671
   get a fresh buffer from the pool if possible; if not: get one from
672
   the queue. We will remove the oldest frame from the queue and hope
673
   it was a good idea... ;-)
674
 
675
 */
676
 
677
static struct mbuf *
678
scc_get_buffer(struct scc_channel *scc, char type)
679
{
680
        struct mbuf * bptr;
681
 
682
        if (type == BT_TRANSMIT)
683
        {
684
                bptr = scc_dequeue_buffer(&scc->tx_buffer_pool);
685
 
686
                /* no free buffers in the pool anymore? */
687
 
688
                if (bptr == NULLBUF)
689
                {
690
#if 0
691
                        printk(KERN_DEBUG "z8530drv: tx buffer pool for %s empty\n", SCC_DEVNAME);
692
#endif
693
                        /* use the oldest from the queue instead */
694
 
695
                        bptr = scc_dequeue_buffer(&scc->tx_queue);
696
                        scc->stat.tx_queued--;
697
                        scc->stat.nospace++;
698
 
699
                        /* this should never, ever happen... */
700
 
701
                        if (bptr == NULLBUF)
702
                                printk(KERN_ERR "z8530drv: panic - all tx buffers for %s gone\n", SCC_DEVNAME);
703
                }
704
        } else {
705
                bptr = scc_dequeue_buffer(&scc->rx_buffer_pool);
706
 
707
                if (bptr == NULLBUF)
708
                {
709
                        printk(KERN_DEBUG "z8530drv: rx buffer pool for %s empty\n", SCC_DEVNAME);
710
 
711
                        bptr = scc_dequeue_buffer(&scc->rx_queue);
712
                        scc->stat.rx_queued--;
713
                        scc->stat.nospace++;
714
 
715
                        if (bptr == NULLBUF)
716
                                printk(KERN_ERR "z8530drv: panic - all rx buffers for %s gone\n", SCC_DEVNAME);
717
                }
718
        }
719
 
720
        if (bptr != NULLBUF)
721
        {
722
                bptr->rw_ptr = bptr->data;
723
                bptr->cnt = 0;
724
        }
725
 
726
        return bptr;
727
}
728
 
729
 
730
/* ******************************************************************** */
731
/* *                    Interrupt Service Routines                    * */
732
/* ******************************************************************** */
733
 
734
 
735
 
736
/* ----> subroutines for the interrupt handlers <---- */
737
 
738
 
739
#ifdef CONFIG_SCC_TTY
740
/* kick rx_timer (try to send received frame or a status message ASAP) */
741
 
742
/* of course we could define a "bottom half" routine to do the job,
743
   but since its structures are saved in an array instead of a linked
744
   list we would get in trouble if it clashes with another driver.
745
   IMHO we are fast enough with a timer routine called on the next
746
   timer-INT... Your opinions?
747
 */
748
 
749
static __inline__ void
750
kick_rx_timer(struct scc_channel *scc)
751
{
752
        del_timer(&(scc->rx_t));
753
        scc->rx_t.expires = jiffies + 1;
754
        scc->rx_t.function = scc_rx_timer;
755
        scc->rx_t.data = (unsigned long) scc;
756
        add_timer(&scc->rx_t);
757
}
758
#endif
759
 
760
static __inline__ void
761
scc_notify(struct scc_channel *scc, int event)
762
{
763
        struct mbuf *bp;
764
 
765
        if (scc->kiss.fulldup != KISS_DUPLEX_OPTIMA)
766
                return;
767
 
768
        bp = scc_get_buffer(scc, BT_RECEIVE);
769
 
770
        if (bp == NULLBUF)
771
                return;
772
 
773
        bp->data[0] = PARAM_HWEVENT;
774
        bp->data[1] = event;
775
        bp->cnt += 2;
776
 
777
        if (scc->stat.is_netdev)
778
        {
779
#ifdef CONFIG_SCC_DEV
780
                scc_net_rx(scc, bp);
781
                scc_enqueue_buffer(&scc->rx_buffer_pool, bp);
782
#endif
783
        } else {
784
#ifdef CONFIG_SCC_TTY
785
                scc_enqueue_buffer(&scc->rx_queue, bp);
786
                scc->stat.rx_queued++;
787
                kick_rx_timer(scc);
788
#endif
789
        }
790
}
791
 
792
static __inline__ void
793
flush_rx_FIFO(struct scc_channel *scc)
794
{
795
        int k;
796
 
797
        for (k=0; k<3; k++)
798
                Inb(scc->data);
799
 
800
        if(scc->rx_bp != NULLBUF)       /* did we receive something? */
801
        {
802
                scc->stat.rxerrs++;  /* then count it as an error */
803
                scc_enqueue_buffer(&scc->rx_buffer_pool, scc->rx_bp);
804
 
805
                scc->rx_bp = NULLBUF;
806
        }
807
}
808
 
809
 
810
/* ----> four different interrupt handlers for Tx, Rx, changing of      */
811
/*       DCD/CTS and Rx/Tx errors                                       */
812
 
813
/* Transmitter interrupt handler */
814
static __inline__ void
815
scc_txint(struct scc_channel *scc)
816
{
817
        struct mbuf *bp;
818
 
819
        scc->stat.txints++;
820
 
821
        bp = scc->tx_bp;
822
 
823
        /* send first octet */
824
 
825
        if (bp == NULLBUF)
826
        {
827
                do
828
                {
829
                        if (bp != NULLBUF)
830
                        {
831
                                scc_enqueue_buffer(&scc->tx_buffer_pool, bp);
832
                                scc->stat.tx_queued--;
833
                        }
834
 
835
                        bp = scc_dequeue_buffer(&scc->tx_queue);
836
 
837
                        if (bp == NULLBUF)
838
                        {
839
                                scc_tx_done(scc);
840
                                Outb(scc->ctrl, RES_Tx_P);      /* clear int */
841
                                return;
842
                        }
843
 
844
                        if (bp->cnt > 0)
845
                        {
846
                                bp->rw_ptr++;
847
                                bp->cnt--;
848
                        }
849
 
850
                } while (bp->cnt < 1);
851
 
852
                scc->tx_bp = bp;
853
                scc->stat.tx_state = TXS_ACTIVE;
854
 
855
                OutReg(scc->ctrl, R0, RES_Tx_CRC);
856
                                                /* reset CRC generator */
857
                or(scc,R10,ABUNDER);            /* re-install underrun protection */
858
                Outb(scc->data,*bp->rw_ptr++);  /* send byte */
859
 
860
                if (!scc->enhanced)             /* reset EOM latch */
861
                        Outb(scc->ctrl,RES_EOM_L);
862
 
863
                bp->cnt--;
864
                return;
865
        }
866
 
867
        /* End Of Frame... */
868
 
869
        if (bp->cnt <= 0)
870
        {
871
                Outb(scc->ctrl, RES_Tx_P);      /* reset pending int */
872
                cl(scc, R10, ABUNDER);          /* send CRC */
873
                scc_enqueue_buffer(&scc->tx_buffer_pool, bp);
874
                scc->stat.tx_queued--;
875
                scc->tx_bp = NULLBUF;
876
                scc->stat.tx_state = TXS_NEWFRAME; /* next frame... */
877
                return;
878
        }
879
 
880
        /* send octet */
881
 
882
        Outb(scc->data,*bp->rw_ptr);
883
        bp->rw_ptr++;                   /* increment pointer */
884
        bp->cnt--;                      /* decrease byte count */
885
}
886
 
887
 
888
/* External/Status interrupt handler */
889
static __inline__ void
890
scc_exint(struct scc_channel *scc)
891
{
892
        unsigned char status,changes,chg_and_stat;
893
 
894
        scc->stat.exints++;
895
 
896
        status = InReg(scc->ctrl,R0);
897
        changes = status ^ scc->status;
898
        chg_and_stat = changes & status;
899
 
900
        /* ABORT: generated whenever DCD drops while receiving */
901
 
902
        if (chg_and_stat & BRK_ABRT)            /* Received an ABORT */
903
                flush_rx_FIFO(scc);
904
 
905
 
906
        /* DCD: on = start to receive packet, off = ABORT condition */
907
        /* (a successfully received packet generates a special condition int) */
908
 
909
        if(changes & DCD)                       /* DCD input changed state */
910
        {
911
                if(status & DCD)                /* DCD is now ON */
912
                {
913
                        if (scc->modem.clocksrc != CLK_EXTERNAL)
914
                                OutReg(scc->ctrl,R14,SEARCH|scc->wreg[R14]); /* DPLL: enter search mode */
915
 
916
                        or(scc,R3,ENT_HM|RxENABLE); /* enable the receiver, hunt mode */
917
                } else {                        /* DCD is now OFF */
918
                        cl(scc,R3,ENT_HM|RxENABLE); /* disable the receiver */
919
                        flush_rx_FIFO(scc);
920
                }
921
 
922
                if (!scc->kiss.softdcd)
923
                        scc_notify(scc, (status & DCD)? HWEV_DCD_ON:HWEV_DCD_OFF);
924
        }
925
 
926
        /* HUNT: software DCD; on = waiting for SYNC, off = receiving frame */
927
 
928
        if (changes & SYNC_HUNT)
929
        {
930
                if (scc->kiss.softdcd)
931
                        scc_notify(scc, (status & SYNC_HUNT)? HWEV_DCD_OFF:HWEV_DCD_ON);
932
                else
933
                        cl(scc,R15,SYNCIE);     /* oops, we were too lazy to disable this? */
934
        }
935
 
936
#ifdef notdef
937
        /* CTS: use external TxDelay (what's that good for?!)
938
         * Anyway: If we _could_ use it (BayCom USCC uses CTS for
939
         * own purposes) we _should_ use the "autoenable" feature
940
         * of the Z8530 and not this interrupt...
941
         */
942
 
943
        if (chg_and_stat & CTS)                 /* CTS is now ON */
944
        {
945
                if (scc->kiss.txdelay == 0)      /* zero TXDELAY = wait for CTS */
946
                        scc_start_tx_timer(scc, t_txdelay, 0);
947
        }
948
#endif
949
 
950
        if (scc->stat.tx_state == TXS_ACTIVE && (status & TxEOM))
951
        {
952
                scc->stat.tx_under++;     /* oops, an underrun! count 'em */
953
                Outb(scc->ctrl, RES_EXT_INT);   /* reset ext/status interrupts */
954
 
955
                if (scc->tx_bp != NULLBUF)
956
                {
957
                        scc_enqueue_buffer(&scc->tx_buffer_pool, scc->tx_bp);
958
                        scc->stat.tx_queued--;
959
                        scc->tx_bp = NULLBUF;
960
                }
961
 
962
                or(scc,R10,ABUNDER);
963
                scc_start_tx_timer(scc, t_txdelay, 1);  /* restart transmission */
964
        }
965
 
966
        scc->status = status;
967
        Outb(scc->ctrl,RES_EXT_INT);
968
}
969
 
970
 
971
/* Receiver interrupt handler */
972
static __inline__ void
973
scc_rxint(struct scc_channel *scc)
974
{
975
        struct mbuf *bp;
976
 
977
        scc->stat.rxints++;
978
 
979
        if((scc->wreg[5] & RTS) && scc->kiss.fulldup == KISS_DUPLEX_HALF)
980
        {
981
                Inb(scc->data);         /* discard char */
982
                or(scc,R3,ENT_HM);      /* enter hunt mode for next flag */
983
                return;
984
        }
985
 
986
        bp = scc->rx_bp;
987
 
988
        if (bp == NULLBUF)
989
        {
990
                bp = scc_get_buffer(scc, BT_RECEIVE);
991
                if (bp == NULLBUF)
992
                {
993
                        Inb(scc->data);
994
                        or(scc, R3, ENT_HM);
995
                        return;
996
                }
997
 
998
                scc->rx_bp = bp;
999
 
1000
                *bp->rw_ptr=0;           /* KISS data */
1001
                bp->rw_ptr++;
1002
                bp->cnt++;
1003
        }
1004
 
1005
        if (bp->cnt > scc->stat.bufsize)
1006
        {
1007
#ifdef notdef
1008
                printk(KERN_DEBUG "z8530drv: oops, scc_rxint() received huge frame...\n");
1009
#endif
1010
                scc_enqueue_buffer(&scc->rx_buffer_pool, bp);
1011
                scc->rx_bp = NULLBUF;
1012
                Inb(scc->data);
1013
                or(scc, R3, ENT_HM);
1014
                return;
1015
        }
1016
 
1017
        /* now, we have a buffer. read character and store it */
1018
        *bp->rw_ptr = Inb(scc->data);
1019
        bp->rw_ptr++;
1020
        bp->cnt++;
1021
}
1022
 
1023
 
1024
/* Receive Special Condition interrupt handler */
1025
static __inline__ void
1026
scc_spint(struct scc_channel *scc)
1027
{
1028
        unsigned char status;
1029
        struct mbuf *bp;
1030
 
1031
        scc->stat.spints++;
1032
 
1033
        status = InReg(scc->ctrl,R1);           /* read receiver status */
1034
 
1035
        Inb(scc->data);                         /* throw away Rx byte */
1036
        bp = scc->rx_bp;
1037
 
1038
        if(status & Rx_OVR)                     /* receiver overrun */
1039
        {
1040
                scc->stat.rx_over++;             /* count them */
1041
                or(scc,R3,ENT_HM);               /* enter hunt mode for next flag */
1042
 
1043
                if (bp) scc_enqueue_buffer(&scc->rx_buffer_pool, bp);
1044
                scc->rx_bp = NULLBUF;
1045
        }
1046
 
1047
        if(status & END_FR && bp != NULLBUF)    /* end of frame */
1048
        {
1049
                /* CRC okay, frame ends on 8 bit boundary and received something ? */
1050
 
1051
                if (!(status & CRC_ERR) && (status & 0xe) == RES8 && bp->cnt)
1052
                {
1053
                        /* ignore last received byte (first of the CRC bytes) */
1054
                        bp->cnt--;
1055
 
1056
                        if (scc->stat.is_netdev)
1057
                        {
1058
#ifdef CONFIG_SCC_DEV
1059
                                scc_net_rx(scc, bp);
1060
                                scc_enqueue_buffer(&scc->rx_buffer_pool, bp);
1061
#endif
1062
                        } else {
1063
#ifdef CONFIG_SCC_TTY
1064
                                scc_enqueue_buffer(&scc->rx_queue, bp);
1065
                                scc->stat.rx_queued++;
1066
                                kick_rx_timer(scc);
1067
#endif
1068
                        }
1069
 
1070
                        scc->rx_bp = NULLBUF;
1071
                        scc->stat.rxframes++;
1072
                } else {                                /* a bad frame */
1073
                        scc_enqueue_buffer(&scc->rx_buffer_pool, bp);
1074
                        scc->rx_bp = NULLBUF;
1075
                        scc->stat.rxerrs++;
1076
                }
1077
        }
1078
 
1079
        Outb(scc->ctrl,ERR_RES);
1080
}
1081
 
1082
 
1083
/* ----> interrupt service routine for the Z8530 <---- */
1084
 
1085
static void
1086
scc_isr_dispatch(struct scc_channel *scc, int vector)
1087
{
1088
        switch (vector & VECTOR_MASK)
1089
        {
1090
                case TXINT: scc_txint(scc); break;
1091
                case EXINT: scc_exint(scc); break;
1092
                case RXINT: scc_rxint(scc); break;
1093
                case SPINT: scc_spint(scc); break;
1094
        }
1095
}
1096
 
1097
/* If the card has a latch for the interrupt vector (like the PA0HZP card)
1098
   use it to get the number of the chip that generated the int.
1099
   If not: poll all defined chips.
1100
 */
1101
 
1102
#define SCC_IRQTIMEOUT 30000
1103
 
1104
static void
1105
scc_isr(int irq, void *dev_id, struct pt_regs *regs)
1106
{
1107
        unsigned char vector;
1108
        struct scc_channel *scc;
1109
        struct scc_ctrl *ctrl;
1110
        int k;
1111
 
1112
        scc_cli(irq);
1113
 
1114
        if (Vector_Latch)
1115
        {
1116
                for(k=0; k < SCC_IRQTIMEOUT; k++)
1117
                {
1118
                        Outb(Vector_Latch, 0);      /* Generate INTACK */
1119
 
1120
                        /* Read the vector */
1121
                        if((vector=Inb(Vector_Latch)) >= 16 * Nchips) break;
1122
                        if (vector & 0x01) break;
1123
 
1124
                        scc=&SCC_Info[vector >> 3 ^ 0x01];
1125
                        if (!scc->tty && !scc->dev) break;
1126
 
1127
                        scc_isr_dispatch(scc, vector);
1128
 
1129
                        OutReg(scc->ctrl,R0,RES_H_IUS);              /* Reset Highest IUS */
1130
                }
1131
                scc_sti(irq);
1132
 
1133
                if (k == SCC_IRQTIMEOUT)
1134
                        printk(KERN_WARNING "z8530drv: endless loop in scc_isr()?\n");
1135
 
1136
                return;
1137
        }
1138
 
1139
        /* Find the SCC generating the interrupt by polling all attached SCCs
1140
         * reading RR3A (the interrupt pending register)
1141
         */
1142
 
1143
        ctrl = SCC_ctrl;
1144
        while (ctrl->chan_A)
1145
        {
1146
                if (ctrl->irq != irq)
1147
                {
1148
                        ctrl++;
1149
                        continue;
1150
                }
1151
 
1152
                scc = NULL;
1153
                for (k = 0; InReg(ctrl->chan_A,R3) && k < SCC_IRQTIMEOUT; k++)
1154
                {
1155
                        vector=InReg(ctrl->chan_B,R2);  /* Read the vector */
1156
                        if (vector & 0x01) break;
1157
 
1158
                        scc = &SCC_Info[vector >> 3 ^ 0x01];
1159
                        if (!scc->tty && !scc->dev) break;
1160
 
1161
                        scc_isr_dispatch(scc, vector);
1162
                }
1163
 
1164
                if (k == SCC_IRQTIMEOUT)
1165
                {
1166
                        printk(KERN_WARNING "z8530drv: endless loop in scc_isr()?!\n");
1167
                        break;
1168
                }
1169
 
1170
                /* This looks wierd and it is. At least the BayCom USCC doesn't
1171
                 * use the Interrupt Daisy Chain, thus we'll have to start
1172
                 * all over again to be sure not to miss an interrupt from
1173
                 * (any of) the other chip(s)...
1174
                 * Honestly, the situation *is* braindamaged...
1175
                 */
1176
 
1177
                if (scc != NULL)
1178
                {
1179
                        OutReg(scc->ctrl,R0,RES_H_IUS);
1180
                        ctrl = SCC_ctrl;
1181
                } else
1182
                        ctrl++;
1183
        }
1184
 
1185
        scc_sti(irq);
1186
}
1187
 
1188
 
1189
 
1190
/* ******************************************************************** */
1191
/* *                    Init Channel                                    */
1192
/* ******************************************************************** */
1193
 
1194
 
1195
/* ----> set SCC channel speed <---- */
1196
 
1197
static __inline__ void
1198
set_brg(struct scc_channel *scc, unsigned int tc)
1199
{
1200
        cl(scc,R14,BRENABL);            /* disable baudrate generator */
1201
        wr(scc,R12,tc & 255);           /* brg rate LOW */
1202
        wr(scc,R13,tc >> 8);            /* brg rate HIGH */
1203
        or(scc,R14,BRENABL);            /* enable baudrate generator */
1204
}
1205
 
1206
static __inline__ void
1207
set_speed(struct scc_channel *scc)
1208
{
1209
        disable_irq(scc->irq);
1210
 
1211
        if (scc->modem.speed > 0)        /* paranoia... */
1212
                set_brg(scc, (unsigned) (scc->clock / (scc->modem.speed * 64)) - 2);
1213
 
1214
        enable_irq(scc->irq);
1215
}
1216
 
1217
 
1218
/* ----> initialize a SCC channel <---- */
1219
 
1220
static __inline__ void
1221
init_brg(struct scc_channel *scc)
1222
{
1223
        wr(scc, R14, BRSRC);                            /* BRG source = PCLK */
1224
        OutReg(scc->ctrl, R14, SSBR|scc->wreg[R14]);    /* DPLL source = BRG */
1225
        OutReg(scc->ctrl, R14, SNRZI|scc->wreg[R14]);   /* DPLL NRZI mode */
1226
}
1227
 
1228
/*
1229
 * Initialization according to the Z8530 manual (SGS-Thomson's version):
1230
 *
1231
 * 1. Modes and constants
1232
 *
1233
 * WR9  11000000        chip reset
1234
 * WR4  XXXXXXXX        Tx/Rx control, async or sync mode
1235
 * WR1  0XX00X00        select W/REQ (optional)
1236
 * WR2  XXXXXXXX        program interrupt vector
1237
 * WR3  XXXXXXX0        select Rx control
1238
 * WR5  XXXX0XXX        select Tx control
1239
 * WR6  XXXXXXXX        sync character
1240
 * WR7  XXXXXXXX        sync character
1241
 * WR9  000X0XXX        select interrupt control
1242
 * WR10 XXXXXXXX        miscellaneous control (optional)
1243
 * WR11 XXXXXXXX        clock control
1244
 * WR12 XXXXXXXX        time constant lower byte (optional)
1245
 * WR13 XXXXXXXX        time constant upper byte (optional)
1246
 * WR14 XXXXXXX0        miscellaneous control
1247
 * WR14 XXXSSSSS        commands (optional)
1248
 *
1249
 * 2. Enables
1250
 *
1251
 * WR14 000SSSS1        baud rate enable
1252
 * WR3  SSSSSSS1        Rx enable
1253
 * WR5  SSSS1SSS        Tx enable
1254
 * WR0  10000000        reset Tx CRG (optional)
1255
 * WR1  XSS00S00        DMA enable (optional)
1256
 *
1257
 * 3. Interrupt status
1258
 *
1259
 * WR15 XXXXXXXX        enable external/status
1260
 * WR0  00010000        reset external status
1261
 * WR0  00010000        reset external status twice
1262
 * WR1  SSSXXSXX        enable Rx, Tx and Ext/status
1263
 * WR9  000SXSSS        enable master interrupt enable
1264
 *
1265
 * 1 = set to one, 0 = reset to zero
1266
 * X = user defined, S = same as previous init
1267
 *
1268
 *
1269
 * Note that the implementation differs in some points from above scheme.
1270
 *
1271
 */
1272
 
1273
static void
1274
init_channel(struct scc_channel *scc)
1275
{
1276
        disable_irq(scc->irq);
1277
 
1278
#ifdef CONFIG_SCC_TTY
1279
        del_timer(&scc->rx_t);
1280
#endif
1281
        del_timer(&scc->tx_t);
1282
        del_timer(&scc->tx_wdog);
1283
 
1284
 
1285
 
1286
        wr(scc,R4,X1CLK|SDLC);          /* *1 clock, SDLC mode */
1287
        wr(scc,R1,0);                    /* no W/REQ operation */
1288
        wr(scc,R3,Rx8|RxCRC_ENAB);      /* RX 8 bits/char, CRC, disabled */
1289
        wr(scc,R5,Tx8|DTR|TxCRC_ENAB);  /* TX 8 bits/char, disabled, DTR */
1290
        wr(scc,R6,0);                    /* SDLC address zero (not used) */
1291
        wr(scc,R7,FLAG);                /* SDLC flag value */
1292
        wr(scc,R9,VIS);                 /* vector includes status */
1293
        wr(scc,R10,(scc->modem.nrz? NRZ : NRZI)|CRCPS|ABUNDER); /* abort on underrun, preset CRC generator, NRZ(I) */
1294
        wr(scc,R14, 0);
1295
 
1296
 
1297
/* set clock sources:
1298
 
1299
   CLK_DPLL: normal halfduplex operation
1300
 
1301
                RxClk: use DPLL
1302
                TxClk: use DPLL
1303
                TRxC mode DPLL output
1304
 
1305
   CLK_EXTERNAL: external clocking (G3RUH or DF9IC modem)
1306
 
1307
                BayCom:                 others:
1308
 
1309
                TxClk = pin RTxC        TxClk = pin TRxC
1310
                RxClk = pin TRxC        RxClk = pin RTxC
1311
 
1312
 
1313
   CLK_DIVIDER:
1314
                RxClk = use DPLL
1315
                TxClk = pin RTxC
1316
 
1317
                BayCom:                 others:
1318
                pin TRxC = DPLL         pin TRxC = BRG
1319
                (RxClk * 1)             (RxClk * 32)
1320
*/
1321
 
1322
 
1323
        switch(scc->modem.clocksrc)
1324
        {
1325
                case CLK_DPLL:
1326
                        wr(scc, R11, RCDPLL|TCDPLL|TRxCOI|TRxCDP);
1327
                        init_brg(scc);
1328
                        break;
1329
 
1330
                case CLK_DIVIDER:
1331
                        wr(scc, R11, ((scc->brand & BAYCOM)? TRxCDP : TRxCBR) | RCDPLL|TCRTxCP|TRxCOI);
1332
                        init_brg(scc);
1333
                        break;
1334
 
1335
                case CLK_EXTERNAL:
1336
                        wr(scc, R11, (scc->brand & BAYCOM)? RCTRxCP|TCRTxCP : RCRTxCP|TCTRxCP);
1337
                        OutReg(scc->ctrl, R14, DISDPLL);
1338
                        break;
1339
 
1340
        }
1341
 
1342
        set_speed(scc);                 /* set baudrate */
1343
 
1344
        if(scc->enhanced)
1345
        {
1346
                or(scc,R15,SHDLCE|FIFOE);       /* enable FIFO, SDLC/HDLC Enhancements (From now R7 is R7') */
1347
                wr(scc,R7,AUTOEOM);
1348
        }
1349
 
1350
        if((InReg(scc->ctrl,R0)) & DCD)         /* DCD is now ON */
1351
        {
1352
                if (scc->modem.clocksrc != CLK_EXTERNAL)
1353
                        or(scc,R14, SEARCH);
1354
 
1355
                or(scc,R3,ENT_HM|RxENABLE);     /* enable the receiver, hunt mode */
1356
        }
1357
 
1358
        /* enable ABORT, DCD & SYNC/HUNT interrupts */
1359
 
1360
        wr(scc,R15, BRKIE|TxUIE|DCDIE);
1361
        if (scc->kiss.softdcd)
1362
                or(scc,R15, SYNCIE);
1363
 
1364
        Outb(scc->ctrl,RES_EXT_INT);    /* reset ext/status interrupts */
1365
        Outb(scc->ctrl,RES_EXT_INT);    /* must be done twice */
1366
 
1367
        or(scc,R1,INT_ALL_Rx|TxINT_ENAB|EXT_INT_ENAB); /* enable interrupts */
1368
 
1369
        scc->status = InReg(scc->ctrl,R0);      /* read initial status */
1370
 
1371
        or(scc,R9,MIE);                 /* master interrupt enable */
1372
 
1373
        scc_init_timer(scc);
1374
 
1375
        enable_irq(scc->irq);
1376
}
1377
 
1378
 
1379
 
1380
 
1381
/* ******************************************************************** */
1382
/* *                    SCC timer functions                           * */
1383
/* ******************************************************************** */
1384
 
1385
 
1386
/* ----> scc_key_trx sets the time constant for the baudrate
1387
         generator and keys the transmitter                  <---- */
1388
 
1389
static void
1390
scc_key_trx(struct scc_channel *scc, char tx)
1391
{
1392
        unsigned int time_const;
1393
 
1394
        if (scc->brand & PRIMUS)
1395
                Outb(scc->ctrl + 4, scc->option | (tx? 0x80 : 0));
1396
 
1397
        if (scc->modem.speed < 300)
1398
                scc->modem.speed = 1200;
1399
 
1400
        time_const = (unsigned) (scc->clock / (scc->modem.speed * (tx? 2:64))) - 2;
1401
 
1402
        disable_irq(scc->irq);
1403
 
1404
        if (tx)
1405
        {
1406
                or(scc, R1, TxINT_ENAB);        /* t_maxkeyup may have reset these */
1407
                or(scc, R15, TxUIE);
1408
        }
1409
 
1410
        if (scc->modem.clocksrc == CLK_DPLL)
1411
        {                               /* force simplex operation */
1412
                if (tx)
1413
                {
1414
                        cl(scc, R3, RxENABLE|ENT_HM);   /* switch off receiver */
1415
                        cl(scc, R15, DCDIE);            /* No DCD changes, please */
1416
                        set_brg(scc, time_const);       /* reprogram baudrate generator */
1417
 
1418
                        /* DPLL -> Rx clk, BRG -> Tx CLK, TRxC mode output, TRxC = BRG */
1419
                        wr(scc, R11, RCDPLL|TCBR|TRxCOI|TRxCBR);
1420
 
1421
                        or(scc,R5,RTS|TxENAB);          /* set the RTS line and enable TX */
1422
                } else {
1423
                        cl(scc,R5,RTS|TxENAB);
1424
 
1425
                        set_brg(scc, time_const);       /* reprogram baudrate generator */
1426
 
1427
                        /* DPLL -> Rx clk, DPLL -> Tx CLK, TRxC mode output, TRxC = DPLL */
1428
                        wr(scc, R11, RCDPLL|TCDPLL|TRxCOI|TRxCDP);
1429
 
1430
                        or(scc,R3,RxENABLE|ENT_HM);
1431
                        or(scc,R15, DCDIE);
1432
                }
1433
        } else {
1434
                if (tx)
1435
                {
1436
                        if (scc->kiss.fulldup == KISS_DUPLEX_HALF)
1437
                        {
1438
                                cl(scc, R3, RxENABLE);
1439
                                cl(scc, R15, DCDIE);
1440
                        }
1441
 
1442
 
1443
                        or(scc,R5,RTS|TxENAB);          /* enable tx */
1444
                } else {
1445
                        cl(scc,R5,RTS|TxENAB);          /* disable tx */
1446
 
1447
                        if (scc->kiss.fulldup == KISS_DUPLEX_HALF)
1448
                        {
1449
                                or(scc, R3, RxENABLE|ENT_HM);
1450
                                or(scc, R15, DCDIE);
1451
                        }
1452
                }
1453
        }
1454
 
1455
        enable_irq(scc->irq);
1456
}
1457
 
1458
 
1459
/* ----> SCC timer interrupt handler and friends. <---- */
1460
 
1461
static void
1462
scc_start_tx_timer(struct scc_channel *scc, void (*handler)(unsigned long), unsigned long when)
1463
{
1464
        unsigned long flags;
1465
 
1466
        save_flags(flags);
1467
        cli();
1468
 
1469
        del_timer(&scc->tx_t);
1470
 
1471
        if (when == 0)
1472
        {
1473
                handler((unsigned long) scc);
1474
        } else
1475
        if (when != TIMER_OFF)
1476
        {
1477
                scc->tx_t.data = (unsigned long) scc;
1478
                scc->tx_t.function = handler;
1479
                scc->tx_t.expires = jiffies + (when*HZ)/100;
1480
                add_timer(&scc->tx_t);
1481
        }
1482
 
1483
        restore_flags(flags);
1484
}
1485
 
1486
static void
1487
scc_start_defer(struct scc_channel *scc)
1488
{
1489
        unsigned long flags;
1490
 
1491
        save_flags(flags);
1492
        cli();
1493
 
1494
        del_timer(&scc->tx_wdog);
1495
 
1496
        if (scc->kiss.maxdefer != 0 && scc->kiss.maxdefer != TIMER_OFF)
1497
        {
1498
                scc->tx_wdog.data = (unsigned long) scc;
1499
                scc->tx_wdog.function = t_busy;
1500
                scc->tx_wdog.expires = jiffies + HZ*scc->kiss.maxdefer;
1501
                add_timer(&scc->tx_wdog);
1502
        }
1503
        restore_flags(flags);
1504
}
1505
 
1506
static void
1507
scc_start_maxkeyup(struct scc_channel *scc)
1508
{
1509
        unsigned long flags;
1510
 
1511
        save_flags(flags);
1512
        cli();
1513
 
1514
        del_timer(&scc->tx_wdog);
1515
 
1516
        if (scc->kiss.maxkeyup != 0 && scc->kiss.maxkeyup != TIMER_OFF)
1517
        {
1518
                scc->tx_wdog.data = (unsigned long) scc;
1519
                scc->tx_wdog.function = t_maxkeyup;
1520
                scc->tx_wdog.expires = jiffies + HZ*scc->kiss.maxkeyup;
1521
                add_timer(&scc->tx_wdog);
1522
        }
1523
 
1524
        restore_flags(flags);
1525
}
1526
 
1527
/*
1528
 * This is called from scc_txint() when there are no more frames to send.
1529
 * Not exactly a timer function, but it is a close friend of the family...
1530
 */
1531
 
1532
static void
1533
scc_tx_done(struct scc_channel *scc)
1534
{
1535
        /*
1536
         * trx remains keyed in fulldup mode 2 until t_idle expires.
1537
         */
1538
 
1539
        switch (scc->kiss.fulldup)
1540
        {
1541
                case KISS_DUPLEX_LINK:
1542
                        scc->stat.tx_state = TXS_IDLE2;
1543
                        if (scc->kiss.idletime != TIMER_OFF)
1544
                        scc_start_tx_timer(scc, t_idle, scc->kiss.idletime*100);
1545
                        break;
1546
                case KISS_DUPLEX_OPTIMA:
1547
                        scc_notify(scc, HWEV_ALL_SENT);
1548
                        break;
1549
                default:
1550
                        scc->stat.tx_state = TXS_BUSY;
1551
                        scc_start_tx_timer(scc, t_tail, scc->kiss.tailtime);
1552
        }
1553
 
1554
#ifdef CONFIG_SCC_DEV
1555
        if (scc->stat.is_netdev)
1556
                scc->dev->tbusy = 0;
1557
#endif
1558
}
1559
 
1560
 
1561
static unsigned char Rand = 17;
1562
 
1563
static __inline__ int
1564
is_grouped(struct scc_channel *scc)
1565
{
1566
        int k;
1567
        struct scc_channel *scc2;
1568
        unsigned char grp1, grp2;
1569
 
1570
        grp1 = scc->kiss.group;
1571
 
1572
        for (k = 0; k < (Nchips * 2); k++)
1573
        {
1574
                scc2 = &SCC_Info[k];
1575
                grp2 = scc2->kiss.group;
1576
 
1577
                if (scc2 == scc || !(scc2->tty && scc2->dev && grp2))
1578
                        continue;
1579
 
1580
                if ((grp1 & 0x3f) == (grp2 & 0x3f))
1581
                {
1582
                        if ( (grp1 & TXGROUP) && (scc2->wreg[R5] & RTS) )
1583
                                return 1;
1584
 
1585
                        if ( (grp1 & RXGROUP) && (scc2->status & DCD) )
1586
                                return 1;
1587
                }
1588
        }
1589
        return 0;
1590
}
1591
 
1592
/* DWAIT and SLOTTIME expired
1593
 *
1594
 * fulldup == 0:  DCD is active or Rand > P-persistence: start t_busy timer
1595
 *                else key trx and start txdelay
1596
 * fulldup == 1:  key trx and start txdelay
1597
 * fulldup == 2:  mintime expired, reset status or key trx and start txdelay
1598
 */
1599
 
1600
static void
1601
t_dwait(unsigned long channel)
1602
{
1603
        struct scc_channel *scc = (struct scc_channel *) channel;
1604
 
1605
        if (scc->stat.tx_state == TXS_WAIT)
1606
        {
1607
                if (scc->tx_queue == NULL)
1608
                {
1609
                        scc->stat.tx_state = TXS_IDLE;
1610
                        return;
1611
                }
1612
 
1613
                scc->stat.tx_state = TXS_BUSY;
1614
        }
1615
 
1616
        if (scc->kiss.fulldup == KISS_DUPLEX_HALF)
1617
        {
1618
                Rand = Rand * 17 + 31;
1619
 
1620
                if ( (scc->kiss.softdcd? !(scc->status & SYNC_HUNT):(scc->status & DCD))  || (scc->kiss.persist) < Rand || (scc->kiss.group && is_grouped(scc)) )
1621
                {
1622
                        scc_start_defer(scc);
1623
                        scc_start_tx_timer(scc, t_dwait, scc->kiss.slottime);
1624
                        return ;
1625
                }
1626
        }
1627
 
1628
        if ( !(scc->wreg[R5] & RTS) )
1629
        {
1630
                scc_key_trx(scc, TX_ON);
1631
                scc_start_tx_timer(scc, t_txdelay, scc->kiss.txdelay);
1632
        } else {
1633
                scc_start_tx_timer(scc, t_txdelay, 0);
1634
        }
1635
}
1636
 
1637
 
1638
/* TXDELAY expired
1639
 *
1640
 * kick transmission by a fake scc_txint(scc), start 'maxkeyup' watchdog.
1641
 */
1642
 
1643
static void
1644
t_txdelay(unsigned long channel)
1645
{
1646
        struct scc_channel *scc = (struct scc_channel *) channel;
1647
 
1648
        scc_start_maxkeyup(scc);
1649
 
1650
        if (scc->tx_bp == NULLBUF)
1651
        {
1652
                disable_irq(scc->irq);
1653
                scc_txint(scc);
1654
                enable_irq(scc->irq);
1655
        }
1656
}
1657
 
1658
 
1659
/* TAILTIME expired
1660
 *
1661
 * switch off transmitter. If we were stopped by Maxkeyup restart
1662
 * transmission after 'mintime' seconds
1663
 */
1664
 
1665
static void
1666
t_tail(unsigned long channel)
1667
{
1668
        struct scc_channel *scc = (struct scc_channel *) channel;
1669
 
1670
        del_timer(&scc->tx_wdog);
1671
        scc_key_trx(scc, TX_OFF);
1672
 
1673
        if (scc->stat.tx_state == TXS_TIMEOUT)          /* we had a timeout? */
1674
        {
1675
                scc->stat.tx_state = TXS_WAIT;
1676
 
1677
                if (scc->tx_bp != NULLBUF)
1678
                        scc_enqueue_buffer(&scc->tx_queue, scc->tx_bp);
1679
 
1680
                scc->tx_bp = NULLBUF;
1681
 
1682
                if (scc->kiss.mintime != TIMER_OFF)     /* try it again */
1683
                        scc_start_tx_timer(scc, t_dwait, scc->kiss.mintime*100);
1684
                else
1685
                        scc_start_tx_timer(scc, t_dwait, 0);
1686
                return;
1687
        }
1688
 
1689
        scc->stat.tx_state = TXS_IDLE;
1690
 
1691
#ifdef CONFIG_SCC_DEV
1692
        if (scc->stat.is_netdev)
1693
                scc->dev->tbusy = 0;
1694
#endif
1695
}
1696
 
1697
 
1698
/* BUSY timeout
1699
 *
1700
 * throw away send buffers if DCD remains active too long.
1701
 */
1702
 
1703
static void
1704
t_busy(unsigned long channel)
1705
{
1706
        struct scc_channel *scc = (struct scc_channel *) channel;
1707
        struct mbuf *bp;
1708
 
1709
        del_timer(&scc->tx_t);
1710
 
1711
        while ((bp = scc_dequeue_buffer(&scc->tx_queue)) != NULLBUF)
1712
                scc_enqueue_buffer(&scc->tx_buffer_pool, bp);
1713
 
1714
        scc->stat.tx_queued = 0;
1715
        scc->stat.txerrs++;
1716
 
1717
        scc->tx_queue = NULLBUF;
1718
        scc->stat.tx_state = TXS_IDLE;
1719
 
1720
#ifdef CONFIG_SCC_DEV
1721
        if (scc->stat.is_netdev)
1722
                scc->dev->tbusy = 0;
1723
#endif
1724
}
1725
 
1726
/* MAXKEYUP timeout
1727
 *
1728
 * this is our watchdog.
1729
 */
1730
 
1731
static void
1732
t_maxkeyup(unsigned long channel)
1733
{
1734
        struct scc_channel *scc = (struct scc_channel *) channel;
1735
        unsigned long flags;
1736
 
1737
        save_flags(flags);
1738
        cli();
1739
 
1740
        if (scc->stat.is_netdev)
1741
        {
1742
#ifdef CONFIG_SCC_DEV
1743
                /*
1744
                 * let things settle down before we start to
1745
                 * accept new data.
1746
                 */
1747
                scc->dev->tbusy = 1;
1748
                scc->dev->trans_start = jiffies;
1749
#endif
1750
        }
1751
 
1752
        del_timer(&scc->tx_t);
1753
 
1754
        cl(scc, R1, TxINT_ENAB);        /* force an ABORT, but don't */
1755
        cl(scc, R15, TxUIE);            /* count it. */
1756
        OutReg(scc->ctrl, R0, RES_Tx_P);
1757
 
1758
        restore_flags(flags);
1759
 
1760
        scc->stat.txerrs++;
1761
        scc->stat.tx_state = TXS_TIMEOUT;
1762
        scc_start_tx_timer(scc, t_tail, scc->kiss.tailtime);
1763
}
1764
 
1765
/* IDLE timeout
1766
 *
1767
 * in fulldup mode 2 it keys down the transmitter after 'idle' seconds
1768
 * of inactivity. We will not restart transmission before 'mintime'
1769
 * expires.
1770
 */
1771
 
1772
static void
1773
t_idle(unsigned long channel)
1774
{
1775
        struct scc_channel *scc = (struct scc_channel *) channel;
1776
 
1777
        del_timer(&scc->tx_wdog);
1778
        scc_key_trx(scc, TX_OFF);
1779
 
1780
        if (scc->kiss.mintime != TIMER_OFF)
1781
                scc_start_tx_timer(scc, t_dwait, scc->kiss.mintime*100);
1782
        scc->stat.tx_state = TXS_WAIT;
1783
}
1784
 
1785
 
1786
#ifdef CONFIG_SCC_TTY
1787
static void
1788
scc_rx_timer(unsigned long channel)
1789
{
1790
        struct scc_channel *scc;
1791
 
1792
        scc = (struct scc_channel *) channel;
1793
 
1794
        if (scc->rx_queue && scc->throttled)
1795
        {
1796
                scc->rx_t.expires = jiffies + HZ/25;    /* 40 msec */
1797
                add_timer(&scc->rx_t);
1798
                return;
1799
        }
1800
 
1801
        kiss_encode(scc);
1802
 
1803
        if (scc->rx_queue && !scc->throttled)
1804
        {
1805
 
1806
                printk(KERN_DEBUG "z8530drv: warning, %s should be throttled\n",
1807
                       SCC_DEVNAME);
1808
 
1809
                scc->rx_t.expires = jiffies + HZ/25;    /* 40 msec */
1810
                add_timer(&scc->rx_t);
1811
        }
1812
}
1813
#endif
1814
 
1815
static void
1816
scc_init_timer(struct scc_channel *scc)
1817
{
1818
        unsigned long flags;
1819
 
1820
        save_flags(flags);
1821
        cli();
1822
 
1823
        scc->stat.tx_state = TXS_IDLE;
1824
 
1825
#ifdef CONFIG_SCC_TTY   
1826
        scc->rx_t.data = (unsigned long) scc;
1827
        scc->rx_t.function = scc_rx_timer;
1828
#endif
1829
 
1830
        restore_flags(flags);
1831
}
1832
 
1833
 
1834
/* ******************************************************************** */
1835
/* *                    KISS interpreter                              * */
1836
/* ******************************************************************** */
1837
 
1838
 
1839
/*
1840
 * this will set the "kiss" parameters through KISS commands or ioctl()
1841
 */
1842
 
1843
#define CAST(x) (unsigned long)(x)
1844
 
1845
static unsigned int
1846
kiss_set_param(struct scc_channel *scc, unsigned int cmd, unsigned int arg)
1847
{
1848
        int dcd;
1849
 
1850
        switch (cmd)
1851
        {
1852
                case PARAM_TXDELAY:     scc->kiss.txdelay=arg;          break;
1853
                case PARAM_PERSIST:     scc->kiss.persist=arg;          break;
1854
                case PARAM_SLOTTIME:    scc->kiss.slottime=arg;         break;
1855
                case PARAM_TXTAIL:      scc->kiss.tailtime=arg;         break;
1856
                case PARAM_FULLDUP:     scc->kiss.fulldup=arg;          break;
1857
                case PARAM_DTR:         break; /* does someone need this? */
1858
                case PARAM_GROUP:       scc->kiss.group=arg;            break;
1859
                case PARAM_IDLE:        scc->kiss.idletime=arg;         break;
1860
                case PARAM_MIN:         scc->kiss.mintime=arg;          break;
1861
                case PARAM_MAXKEY:      scc->kiss.maxkeyup=arg;         break;
1862
                case PARAM_WAIT:        scc->kiss.waittime=arg;         break;
1863
                case PARAM_MAXDEFER:    scc->kiss.maxdefer=arg;         break;
1864
                case PARAM_TX:          scc->kiss.tx_inhibit=arg;       break;
1865
 
1866
                case PARAM_SOFTDCD:
1867
                        scc->kiss.softdcd=arg;
1868
                        if (arg)
1869
                                or(scc, R15, SYNCIE);
1870
                        else
1871
                                cl(scc, R15, SYNCIE);
1872
                        break;
1873
 
1874
                case PARAM_SPEED:
1875
                        if (arg < 256)
1876
                                scc->modem.speed=arg*100;
1877
                        else
1878
                                scc->modem.speed=arg;
1879
 
1880
                        if (scc->stat.tx_state == 0)     /* only switch baudrate on rx... ;-) */
1881
                                set_speed(scc);
1882
                        break;
1883
 
1884
                case PARAM_RTS:
1885
                        if ( !(scc->wreg[R5] & RTS) )
1886
                        {
1887
                                if (arg != TX_OFF)
1888
                                        scc_key_trx(scc, TX_ON);
1889
                                        scc_start_tx_timer(scc, t_txdelay, scc->kiss.txdelay);
1890
                        } else {
1891
                                if (arg == TX_OFF)
1892
                                {
1893
                                        scc->stat.tx_state = TXS_BUSY;
1894
                                        scc_start_tx_timer(scc, t_tail, scc->kiss.tailtime);
1895
                                }
1896
                        }
1897
                        break;
1898
 
1899
                case PARAM_HWEVENT:
1900
                        dcd = (scc->kiss.softdcd? !(scc->status & SYNC_HUNT):(scc->status & DCD));
1901
                        scc_notify(scc, dcd? HWEV_DCD_ON:HWEV_DCD_OFF);
1902
                        break;
1903
 
1904
                default:                return -EINVAL;
1905
        }
1906
 
1907
        return 0;
1908
}
1909
 
1910
 
1911
 
1912
static unsigned long
1913
kiss_get_param(struct scc_channel *scc, unsigned int cmd)
1914
{
1915
        switch (cmd)
1916
        {
1917
                case PARAM_TXDELAY:     return CAST(scc->kiss.txdelay);
1918
                case PARAM_PERSIST:     return CAST(scc->kiss.persist);
1919
                case PARAM_SLOTTIME:    return CAST(scc->kiss.slottime);
1920
                case PARAM_TXTAIL:      return CAST(scc->kiss.tailtime);
1921
                case PARAM_FULLDUP:     return CAST(scc->kiss.fulldup);
1922
                case PARAM_SOFTDCD:     return CAST(scc->kiss.softdcd);
1923
                case PARAM_DTR:         return CAST((scc->wreg[R5] & DTR)? 1:0);
1924
                case PARAM_RTS:         return CAST((scc->wreg[R5] & RTS)? 1:0);
1925
                case PARAM_SPEED:       return CAST(scc->modem.speed);
1926
                case PARAM_GROUP:       return CAST(scc->kiss.group);
1927
                case PARAM_IDLE:        return CAST(scc->kiss.idletime);
1928
                case PARAM_MIN:         return CAST(scc->kiss.mintime);
1929
                case PARAM_MAXKEY:      return CAST(scc->kiss.maxkeyup);
1930
                case PARAM_WAIT:        return CAST(scc->kiss.waittime);
1931
                case PARAM_MAXDEFER:    return CAST(scc->kiss.maxdefer);
1932
                case PARAM_TX:          return CAST(scc->kiss.tx_inhibit);
1933
                default:                return NO_SUCH_PARAM;
1934
        }
1935
 
1936
}
1937
 
1938
#undef CAST
1939
#undef SVAL
1940
 
1941
/* interpret frame: strip CRC and decode KISS */
1942
 
1943
static void
1944
kiss_interpret_frame(struct scc_channel * scc)
1945
{
1946
        unsigned char kisscmd;
1947
        unsigned long flags;
1948
        struct mbuf *bp;
1949
 
1950
        bp = scc->kiss_decode_bp;
1951
        bp->rw_ptr = bp->data;
1952
 
1953
        if (bp->cnt < 2)
1954
        {
1955
                scc_enqueue_buffer(&scc->tx_buffer_pool, bp);
1956
                scc->kiss_decode_bp = NULLBUF;
1957
                return;
1958
        }
1959
 
1960
        kisscmd = *bp->rw_ptr;
1961
        bp->rw_ptr++;
1962
 
1963
        if (kisscmd & 0xa0)
1964
        {
1965
                if (bp->cnt > 3)
1966
                        bp->cnt -= 2;
1967
                else
1968
                {
1969
                        scc_enqueue_buffer(&scc->tx_buffer_pool, bp);
1970
                        scc->kiss_decode_bp = NULLBUF;
1971
                        return;
1972
                }
1973
        }
1974
 
1975
        kisscmd &= 0x1f;
1976
 
1977
        if (kisscmd)
1978
        {
1979
                kiss_set_param(scc, kisscmd, *bp->rw_ptr);
1980
                scc_enqueue_buffer(&scc->tx_buffer_pool, bp);
1981
                scc->kiss_decode_bp = NULLBUF;
1982
                return;
1983
        }
1984
 
1985
        scc_enqueue_buffer(&scc->tx_queue, bp); /* enqueue frame */
1986
        scc->kiss_decode_bp = NULLBUF;
1987
 
1988
        scc->stat.txframes++;
1989
        scc->stat.tx_queued++;
1990
 
1991
        save_flags(flags);
1992
        cli();
1993
 
1994
        /*
1995
         * start transmission if the trx state is idle or
1996
         * t_idle hasn't expired yet. Use dwait/persistance/slottime
1997
         * algorithm for normal halfduplex operation.
1998
         */
1999
 
2000
        if(scc->stat.tx_state == TXS_IDLE || scc->stat.tx_state == TXS_IDLE2)
2001
        {
2002
                scc->stat.tx_state = TXS_BUSY;
2003
                if (scc->kiss.fulldup == KISS_DUPLEX_HALF)
2004
                        scc_start_tx_timer(scc, t_dwait, scc->kiss.waittime);
2005
                else
2006
                        scc_start_tx_timer(scc, t_dwait, 0);
2007
        }
2008
 
2009
        restore_flags(flags);
2010
}
2011
 
2012
#ifdef CONFIG_SCC_TTY
2013
static inline void
2014
kiss_store_byte(struct scc_channel *scc, unsigned char ch)
2015
{
2016
        struct mbuf *bp = scc->kiss_decode_bp;
2017
        static int k = 0;
2018
 
2019
        if (bp != NULLBUF)
2020
        {
2021
                if (bp->cnt > scc->stat.bufsize)
2022
                {
2023
                        if (!k++)
2024
                                printk(KERN_NOTICE "z8530drv: KISS frame too long\n");
2025
                } else {
2026
                        k = 0;
2027
                        *bp->rw_ptr = ch;
2028
                        bp->rw_ptr++;
2029
                        bp->cnt++;
2030
                }
2031
        }
2032
}
2033
 
2034
static inline int
2035
kiss_decode(struct scc_channel *scc, unsigned char ch)
2036
{
2037
        switch (scc->stat.tx_kiss_state)
2038
        {
2039
                case KISS_IDLE:
2040
                        if (ch == FEND)
2041
                        {
2042
                                scc->kiss_decode_bp = scc_get_buffer(scc, BT_TRANSMIT);
2043
                                if (scc->kiss_decode_bp == NULLBUF)
2044
                                        return 1;
2045
 
2046
                                scc->stat.tx_kiss_state = KISS_DATA;
2047
                        } else scc->stat.txerrs++;
2048
                        break;
2049
 
2050
                case KISS_DATA:
2051
                        if (ch == FESC)
2052
                                scc->stat.tx_kiss_state = KISS_ESCAPE;
2053
                        else if (ch == FEND)
2054
                        {
2055
                                kiss_interpret_frame(scc);
2056
                                scc->stat.tx_kiss_state = KISS_IDLE;
2057
                        }
2058
                        else kiss_store_byte(scc, ch);
2059
                        break;
2060
 
2061
                case KISS_ESCAPE:
2062
                        if (ch == TFEND)
2063
                        {
2064
                                kiss_store_byte(scc, FEND);
2065
                                scc->stat.tx_kiss_state = KISS_DATA;
2066
                        }
2067
                        else if (ch == TFESC)
2068
                        {
2069
                                kiss_store_byte(scc, FESC);
2070
                                scc->stat.tx_kiss_state = KISS_DATA;
2071
                        }
2072
                        else
2073
                        {
2074
                                scc_enqueue_buffer(&scc->tx_buffer_pool, scc->kiss_decode_bp);
2075
                                scc->kiss_decode_bp = NULLBUF;
2076
                                scc->stat.txerrs++;
2077
                                scc->stat.tx_kiss_state = KISS_IDLE;
2078
                        }
2079
                        break;
2080
        } /* switch */
2081
 
2082
        return 0;
2083
 
2084
}
2085
 
2086
/* ----> Encode received data and write it to the flip-buffer  <---- */
2087
 
2088
static void
2089
kiss_encode(struct scc_channel *scc)
2090
{
2091
        struct mbuf *bp;
2092
        struct tty_struct * tty = scc->tty;
2093
        unsigned char ch;
2094
 
2095
        bp = scc->kiss_encode_bp;
2096
 
2097
        /* worst case: FEND 0 FESC TFEND -> 4 bytes */
2098
 
2099
        while(tty->flip.count < TTY_FLIPBUF_SIZE-4)
2100
        {
2101
                if (bp == NULLBUF)
2102
                {
2103
                        bp = scc_dequeue_buffer(&scc->rx_queue);
2104
                        scc->kiss_encode_bp = bp;
2105
 
2106
                        if (bp == NULLBUF)
2107
                        {
2108
                                scc->stat.rx_kiss_state = KISS_IDLE;
2109
                                break;
2110
                        }
2111
                }
2112
 
2113
 
2114
                if (bp->cnt <= 0)
2115
                {
2116
                        if (--scc->stat.rx_queued < 0)
2117
                                scc->stat.rx_queued = 0;
2118
 
2119
                        if (scc->stat.rx_kiss_state == KISS_RXFRAME)    /* new packet? */
2120
                        {
2121
                                tty_insert_flip_char(tty, FEND, 0);  /* send FEND for old frame */
2122
                                scc->stat.rx_kiss_state = KISS_IDLE; /* generate FEND for new frame */
2123
                        }
2124
 
2125
                        scc_enqueue_buffer(&scc->rx_buffer_pool, bp);
2126
 
2127
                        bp = scc->kiss_encode_bp = NULLBUF;
2128
                        continue;
2129
                }
2130
 
2131
 
2132
                if (scc->stat.rx_kiss_state == KISS_IDLE)
2133
                {
2134
                        tty_insert_flip_char(tty, FEND, 0);
2135
                        scc->stat.rx_kiss_state = KISS_RXFRAME;
2136
                }
2137
 
2138
                switch(ch = *bp->rw_ptr)
2139
                {
2140
                        case FEND:
2141
                                tty_insert_flip_char(tty, FESC, 0);
2142
                                tty_insert_flip_char(tty, TFEND, 0);
2143
                                break;
2144
                        case FESC:
2145
                                tty_insert_flip_char(tty, FESC, 0);
2146
                                tty_insert_flip_char(tty, TFESC, 0);
2147
                                break;
2148
                        default:
2149
                                tty_insert_flip_char(tty, ch, 0);
2150
                }
2151
 
2152
                bp->rw_ptr++;
2153
                bp->cnt--;
2154
        }
2155
 
2156
        queue_task(&tty->flip.tqueue, &tq_timer); /* kick it... */
2157
}
2158
#endif /* CONFIG_SCC_TTY */
2159
 
2160
 
2161
/* ******************************************************************* */
2162
/* *            Init channel structures, special HW, etc...          * */
2163
/* ******************************************************************* */
2164
 
2165
 
2166
static void
2167
z8530_init(void)
2168
{
2169
        struct scc_channel *scc;
2170
        int chip, k;
2171
        unsigned long flags;
2172
        char *flag;
2173
 
2174
 
2175
        printk(KERN_INFO "Init Z8530 driver: %u channels, IRQ", Nchips*2);
2176
 
2177
        flag=" ";
2178
        for (k = 0; k < 16; k++)
2179
                if (Ivec[k].used)
2180
                {
2181
                        printk("%s%d", flag, k);
2182
                        flag=",";
2183
                }
2184
        printk("\n");
2185
 
2186
 
2187
        /* reset and pre-init all chips in the system */
2188
        for (chip = 0; chip < Nchips; chip++)
2189
        {
2190
                scc=&SCC_Info[2*chip];
2191
                if (!scc->ctrl) continue;
2192
 
2193
                /* Special SCC cards */
2194
 
2195
                if(scc->brand & EAGLE)                  /* this is an EAGLE card */
2196
                        Outb(scc->special,0x08);        /* enable interrupt on the board */
2197
 
2198
                if(scc->brand & (PC100 | PRIMUS))       /* this is a PC100/EAGLE card */
2199
                        Outb(scc->special,scc->option); /* set the MODEM mode (0x22) */
2200
 
2201
 
2202
                /* Init SCC */
2203
 
2204
                /* some general init we can do now */
2205
 
2206
                save_flags(flags);
2207
                cli();
2208
 
2209
                Outb(scc->ctrl, 0);
2210
                OutReg(scc->ctrl,R9,FHWRES);            /* force hardware reset */
2211
                udelay(100);                            /* give it 'a bit' more time than required */
2212
                wr(scc, R2, chip*16);                   /* interrupt vector */
2213
                wr(scc, R9, VIS);                       /* vector includes status */
2214
 
2215
                restore_flags(flags);
2216
        }
2217
 
2218
 
2219
        Driver_Initialized = 1;
2220
}
2221
 
2222
 
2223
/* ******************************************************************** */
2224
/* *    Filesystem Routines: open, close, ioctl, settermios, etc      * */
2225
/* ******************************************************************** */
2226
 
2227
 
2228
/* scc_paranoia_check(): warn user if something went wrong              */
2229
 
2230
static __inline__ int
2231
scc_paranoia_check(struct scc_channel *scc, kdev_t device, const char *routine)
2232
{
2233
#ifdef SCC_PARANOIA_CHECK
2234
 
2235
static const char *badmagic =
2236
        KERN_ALERT "Warning: bad magic number for Z8530 SCC struct (%s) in %s\n";
2237
static const char *badinfo =
2238
        KERN_CRIT "Warning: Z8530 not found for (%s) in %s (forgot to run sccinit?)\n";
2239
 
2240
        if (!scc->init)
2241
        {
2242
                printk(badinfo, kdevname(device), routine);
2243
                return 1;
2244
        }
2245
 
2246
        if (scc->magic != SCC_MAGIC)
2247
        {
2248
                printk(badmagic, kdevname(device), routine);
2249
                return 1;
2250
        }
2251
#endif
2252
 
2253
        return 0;
2254
}
2255
 
2256
#ifdef CONFIG_SCC_TTY
2257
/* ----> this one is called whenever you open the device <---- */
2258
 
2259
int
2260
scc_open(struct tty_struct *tty, struct file * filp)
2261
{
2262
        struct scc_channel *scc;
2263
        int chan;
2264
 
2265
        chan = MINOR(tty->device) - tty->driver.minor_start;
2266
 
2267
        if (Driver_Initialized)
2268
        {
2269
                if ( (chan < 0) || (chan >= (Nchips * 2)) )
2270
                        return -ENODEV;
2271
        } else {
2272
                tty->driver_data = &SCC_Info[0];
2273
                MOD_INC_USE_COUNT;
2274
                return 0;
2275
        }
2276
 
2277
        scc = &SCC_Info[chan];
2278
 
2279
        tty->driver_data = scc;
2280
        tty->termios->c_cflag &= ~CBAUD;
2281
 
2282
        if (scc->magic != SCC_MAGIC)
2283
        {
2284
                printk(KERN_ALERT "z8530drv: scc_open() found bad magic number for device (%s)",
2285
                       kdevname(tty->device));
2286
                return -ENODEV;
2287
        }
2288
 
2289
        MOD_INC_USE_COUNT;
2290
 
2291
        if(scc->tty != NULL || scc->stat.is_netdev)
2292
        {
2293
                scc->tty_opened++;
2294
                return 0;
2295
        }
2296
 
2297
        scc->tty = tty;
2298
 
2299
        if(!scc->init)
2300
                return 0;
2301
 
2302
        alloc_buffer_pool(scc);
2303
 
2304
        scc->throttled = 0;
2305
 
2306
        scc->stat.tx_kiss_state = KISS_IDLE;
2307
        scc->stat.rx_kiss_state = KISS_IDLE;
2308
 
2309
        init_channel(scc);
2310
        return 0;
2311
}
2312
 
2313
 
2314
/* ----> and this whenever you close the device <---- */
2315
 
2316
static void
2317
scc_close(struct tty_struct *tty, struct file * filp)
2318
{
2319
        struct scc_channel *scc = tty->driver_data;
2320
        unsigned long flags;
2321
 
2322
        if (!scc || (scc->magic != SCC_MAGIC))
2323
                return;
2324
 
2325
 
2326
        MOD_DEC_USE_COUNT;
2327
 
2328
        if(scc->tty_opened || scc->stat.is_netdev)
2329
        {
2330
                scc->tty_opened--;
2331
                return;
2332
        }
2333
 
2334
        tty->driver_data = NULLBUF;
2335
 
2336
        if (!Driver_Initialized)
2337
                return;
2338
 
2339
        save_flags(flags);
2340
        cli();
2341
 
2342
        Outb(scc->ctrl,0);               /* Make sure pointer is written */
2343
        wr(scc,R1,0);                    /* disable interrupts */
2344
        wr(scc,R3,0);
2345
 
2346
        scc->tty = NULL;
2347
 
2348
        del_timer(&scc->tx_t);
2349
        del_timer(&scc->tx_wdog);
2350
        del_timer(&scc->rx_t);
2351
 
2352
        restore_flags(flags);
2353
 
2354
        if (!scc->init)
2355
                return;
2356
 
2357
        free_buffer_pool(scc);
2358
 
2359
        scc->throttled = 0;
2360
        tty->stopped = 0;
2361
}
2362
 
2363
 
2364
/*
2365
 * change scc_speed
2366
 */
2367
 
2368
static void
2369
scc_change_speed(struct scc_channel * scc)
2370
{
2371
        long speed;
2372
 
2373
        if (scc->tty == NULL)
2374
                return;
2375
 
2376
 
2377
        speed = baud_table[scc->tty->termios->c_cflag & CBAUD];
2378
 
2379
        if (speed > 0) scc->modem.speed = speed;
2380
 
2381
        if (scc->stat.tx_state == 0)     /* only switch baudrate on rx... ;-) */
2382
                set_speed(scc);
2383
}
2384
#endif /* CONFIG_SCC_TTY */
2385
 
2386
/* ----> ioctl-routine of the driver <---- */
2387
 
2388
 
2389
/* sub routines to read/set KISS parameters */
2390
 
2391
/* generic ioctl() functions for both TTY or network device mode:
2392
 *
2393
 * TIOCSCCCFG           - configure driver      arg: (struct scc_hw_config *) arg
2394
 * TIOCSCCINI           - initialize driver     arg: ---
2395
 * TIOCSCCCHANINI       - initialize channel    arg: (struct scc_modem *) arg
2396
 * TIOCSCCGKISS         - get level 1 parameter arg: (struct scc_kiss_cmd *) arg
2397
 * TIOCSCCSKISS         - set level 1 parameter arg: (struct scc_kiss_cmd *) arg
2398
 * TIOCSCCSTAT          - get driver status     arg: (struct scc_stat *) arg
2399
 */
2400
 
2401
static int
2402
scc_ioctl(struct scc_channel *scc, unsigned int cmd, void *arg)
2403
{
2404
        unsigned long flags;
2405
        struct scc_kiss_cmd kiss_cmd;
2406
        struct scc_mem_config memcfg;
2407
        struct scc_hw_config hwcfg;
2408
        int error, chan;
2409
        unsigned char device_name[10];
2410
 
2411
        if (!Driver_Initialized)
2412
        {
2413
                if (cmd == TIOCSCCCFG)
2414
                {
2415
                        int found = 1;
2416
 
2417
                        if (!suser()) return -EPERM;
2418
                        if (!arg) return -EFAULT;
2419
 
2420
                        error = verify_area(VERIFY_READ, arg, sizeof(struct scc_hw_config));
2421
                        if (error) return error;
2422
 
2423
                        if (Nchips >= MAXSCC)
2424
                                return -EINVAL;
2425
 
2426
                        memcpy_fromfs(&hwcfg, arg, sizeof(hwcfg));
2427
 
2428
                        if (hwcfg.irq == 2) hwcfg.irq = 9;
2429
 
2430
                        if (!Ivec[hwcfg.irq].used && hwcfg.irq)
2431
                        {
2432
                                if (request_irq(hwcfg.irq, scc_isr, SA_INTERRUPT, "AX.25 SCC", NULL))
2433
                                        printk(KERN_WARNING "z8530drv: warning, cannot get IRQ %d\n", hwcfg.irq);
2434
                                else
2435
                                        Ivec[hwcfg.irq].used = 1;
2436
                        }
2437
 
2438
                        if (hwcfg.vector_latch)
2439
                                Vector_Latch = hwcfg.vector_latch;
2440
 
2441
                        if (hwcfg.clock == 0)
2442
                                hwcfg.clock = DEFAULT_CLOCK;
2443
 
2444
#ifndef DONT_CHECK
2445
                        disable_irq(hwcfg.irq);
2446
 
2447
                        check_region(scc->ctrl, 1);
2448
                        Outb(hwcfg.ctrl_a, 0);
2449
                        udelay(5);
2450
                        OutReg(hwcfg.ctrl_a,R13,0x55);          /* is this chip really there? */
2451
                        udelay(5);
2452
 
2453
                        if (InReg(hwcfg.ctrl_a,R13) != 0x55)
2454
                                found = 0;
2455
 
2456
                        enable_irq(hwcfg.irq);
2457
#endif
2458
 
2459
                        if (found)
2460
                        {
2461
                                SCC_Info[2*Nchips  ].ctrl = hwcfg.ctrl_a;
2462
                                SCC_Info[2*Nchips  ].data = hwcfg.data_a;
2463
                                SCC_Info[2*Nchips  ].irq  = hwcfg.irq;
2464
                                SCC_Info[2*Nchips+1].ctrl = hwcfg.ctrl_b;
2465
                                SCC_Info[2*Nchips+1].data = hwcfg.data_b;
2466
                                SCC_Info[2*Nchips+1].irq  = hwcfg.irq;
2467
 
2468
                                SCC_ctrl[Nchips].chan_A = hwcfg.ctrl_a;
2469
                                SCC_ctrl[Nchips].chan_B = hwcfg.ctrl_b;
2470
                                SCC_ctrl[Nchips].irq    = hwcfg.irq;
2471
                        }
2472
 
2473
 
2474
                        for (chan = 0; chan < 2; chan++)
2475
                        {
2476
                                sprintf(device_name, "%s%i", SCC_DriverName, 2*Nchips+chan);
2477
 
2478
                                SCC_Info[2*Nchips+chan].special = hwcfg.special;
2479
                                SCC_Info[2*Nchips+chan].clock = hwcfg.clock;
2480
                                SCC_Info[2*Nchips+chan].brand = hwcfg.brand;
2481
                                SCC_Info[2*Nchips+chan].option = hwcfg.option;
2482
                                SCC_Info[2*Nchips+chan].enhanced = hwcfg.escc;
2483
 
2484
#ifdef DONT_CHECK
2485
                                printk(KERN_INFO "%s: data port = 0x%3.3x  control port = 0x%3.3x\n",
2486
                                        device_name,
2487
                                        SCC_Info[2*Nchips+chan].data,
2488
                                        SCC_Info[2*Nchips+chan].ctrl);
2489
 
2490
#else
2491
                                printk(KERN_INFO "%s: data port = 0x%3.3x  control port = 0x%3.3x -- %s\n",
2492
                                        device_name,
2493
                                        chan? hwcfg.data_b : hwcfg.data_a,
2494
                                        chan? hwcfg.ctrl_b : hwcfg.ctrl_a,
2495
                                        found? "found" : "missing");
2496
#endif
2497
 
2498
                                if (found)
2499
                                {
2500
                                        request_region(SCC_Info[2*Nchips+chan].ctrl, 1, "scc ctrl");
2501
                                        request_region(SCC_Info[2*Nchips+chan].data, 1, "scc data");
2502
#ifdef CONFIG_SCC_DEV
2503
                                        if (Nchips+chan != 0)
2504
                                                scc_net_setup(&SCC_Info[2*Nchips+chan], device_name);
2505
#endif
2506
                                }
2507
                        }
2508
 
2509
                        if (found) Nchips++;
2510
 
2511
#ifdef SCC_DEBUG
2512
                        printk(KERN_INFO "Available modes:"
2513
#ifdef CONFIG_SCC_DEV
2514
                        " dev"
2515
#endif
2516
#ifdef CONFIG_SCC_TTY
2517
                        " tty"
2518
#endif
2519
                        "\n");
2520
#endif
2521
 
2522
                        return 0;
2523
                }
2524
 
2525
                if (cmd == TIOCSCCINI)
2526
                {
2527
                        if (!suser())
2528
                                return -EPERM;
2529
 
2530
                        if (Nchips == 0)
2531
                                return -EINVAL;
2532
 
2533
                        z8530_init();
2534
                        return 0;
2535
                }
2536
 
2537
                return -EINVAL; /* confuse the user */
2538
        }
2539
 
2540
        if (!scc->init)
2541
        {
2542
                if (cmd == TIOCSCCCHANINI)
2543
                {
2544
                        if (!suser()) return -EPERM;
2545
                        if (!arg) return -EINVAL;
2546
                        error = verify_area(VERIFY_READ, arg, sizeof(struct scc_modem));
2547
                        if (error) return error;
2548
 
2549
                        scc->stat.rxbuffers = RXBUFFERS;
2550
                        scc->stat.txbuffers = TXBUFFERS;
2551
                        scc->stat.bufsize   = BUFSIZE;
2552
 
2553
                        memcpy_fromfs(&scc->modem, arg, sizeof(struct scc_modem));
2554
 
2555
                        /* default KISS Params */
2556
 
2557
                        if (scc->modem.speed < 4800)
2558
                        {
2559
                                scc->kiss.txdelay = 36;         /* 360 ms */
2560
                                scc->kiss.persist = 42;         /* 25% persistence */                   /* was 25 */
2561
                                scc->kiss.slottime = 16;        /* 160 ms */
2562
                                scc->kiss.tailtime = 4;         /* minimal reasonable value */
2563
                                scc->kiss.fulldup = 0;           /* CSMA */
2564
                                scc->kiss.waittime = 50;        /* 500 ms */
2565
                                scc->kiss.maxkeyup = 10;        /* 10 s */
2566
                                scc->kiss.mintime = 3;          /* 3 s */
2567
                                scc->kiss.idletime = 30;        /* 30 s */
2568
                                scc->kiss.maxdefer = 120;       /* 2 min */
2569
                                scc->kiss.softdcd = 0;           /* hardware dcd */
2570
                        } else {
2571
                                scc->kiss.txdelay = 10;         /* 100 ms */
2572
                                scc->kiss.persist = 64;         /* 25% persistence */                   /* was 25 */
2573
                                scc->kiss.slottime = 8;         /* 160 ms */
2574
                                scc->kiss.tailtime = 1;         /* minimal reasonable value */
2575
                                scc->kiss.fulldup = 0;           /* CSMA */
2576
                                scc->kiss.waittime = 50;        /* 500 ms */
2577
                                scc->kiss.maxkeyup = 7;         /* 7 s */
2578
                                scc->kiss.mintime = 3;          /* 3 s */
2579
                                scc->kiss.idletime = 30;        /* 30 s */
2580
                                scc->kiss.maxdefer = 120;       /* 2 min */
2581
                                scc->kiss.softdcd = 0;           /* hardware dcd */
2582
                        }
2583
 
2584
                        scc->init = 1;
2585
 
2586
                        return 0;
2587
                }
2588
 
2589
                return -EINVAL;
2590
        }
2591
 
2592
        switch(cmd)
2593
        {
2594
                case TIOCSCCSMEM:
2595
                case TIOCCHANMEM_OLD:
2596
                        if (!suser()) return -EPERM;
2597
                        if (!arg) return -EINVAL;
2598
                        error = verify_area(VERIFY_READ, arg, sizeof(struct scc_mem_config));
2599
                        if (error) return error;
2600
 
2601
                        memcpy_fromfs(&memcfg, arg, sizeof(struct scc_mem_config));
2602
 
2603
                        save_flags(flags);
2604
                        cli();
2605
 
2606
                        free_buffer_pool(scc);
2607
                        scc->stat.rxbuffers = memcfg.rxbuffers;
2608
                        scc->stat.txbuffers = memcfg.txbuffers;
2609
                        scc->stat.bufsize   = memcfg.bufsize;
2610
                        if (scc->tty || (scc->dev && (scc->dev->flags & IFF_UP)))
2611
                                alloc_buffer_pool(scc);
2612
 
2613
                        restore_flags(flags);
2614
                        return 0;
2615
 
2616
                case TIOCSCCGSTAT:
2617
                case TIOCSCCSTAT_OLD:
2618
                        if (!arg) return -EINVAL;
2619
                        error = verify_area(VERIFY_WRITE, arg, sizeof(struct scc_mem_config));
2620
                        if (error) return error;
2621
 
2622
                        memcpy_tofs(arg, &scc->stat, sizeof(struct scc_stat));
2623
                        return 0;
2624
 
2625
                case TIOCSCCGKISS:
2626
                case TIOCGKISS_OLD:
2627
                        if (!arg) return -EINVAL;
2628
                        error = verify_area(VERIFY_WRITE, arg, sizeof(struct scc_mem_config));
2629
                        if (error) return error;
2630
 
2631
                        memcpy_fromfs(&kiss_cmd, arg, sizeof(struct scc_kiss_cmd));
2632
                        kiss_cmd.param = kiss_get_param(scc, kiss_cmd.command);
2633
                        memcpy_tofs(arg, &kiss_cmd, sizeof(struct scc_kiss_cmd));
2634
                        return 0;
2635
                        break;
2636
 
2637
                case TIOCSCCSKISS:
2638
                case TIOCSKISS_OLD:
2639
                        if (!suser()) return -EPERM;
2640
                        if (!arg) return -EINVAL;
2641
                        error = verify_area(VERIFY_READ, arg, sizeof(struct scc_mem_config));
2642
                        if (error) return error;
2643
 
2644
                        memcpy_fromfs(&kiss_cmd, arg, sizeof(struct scc_kiss_cmd));
2645
                        return kiss_set_param(scc, kiss_cmd.command, kiss_cmd.param);
2646
                        break;
2647
 
2648
                default:
2649
                        return -ENOIOCTLCMD;
2650
 
2651
        }
2652
 
2653
        return 0;
2654
}
2655
 
2656
#ifdef CONFIG_SCC_TTY
2657
/* addtional ioctl() for TTY driver mode:
2658
 *
2659
 * TIOCMGET     - get modem status      arg: (unsigned long *) arg
2660
 * TIOCMBIS     - set PTT               arg: ---
2661
 * TIOCMBIC     - reset PTT             arg: ---
2662
 * TIOCMBIC     - set PTT               arg: ---
2663
 */
2664
 
2665
static int
2666
scc_tty_ioctl(struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
2667
{
2668
        struct scc_channel * scc = tty->driver_data;
2669
        unsigned long flags;
2670
        unsigned int result;
2671
        unsigned int value;
2672
        int error;
2673
 
2674
        if (scc->magic != SCC_MAGIC)
2675
        {
2676
                printk(KERN_ALERT "z8530drv: scc_ioctl() found bad magic number for device %s",
2677
                        SCC_DEVNAME);
2678
 
2679
                return -ENODEV;
2680
        }
2681
 
2682
        if (!Driver_Initialized && cmd == TIOCSCCINI)
2683
                scc->tty=tty;
2684
 
2685
        switch(cmd)
2686
        {
2687
                case TIOCSCCCFG:
2688
                case TIOCSCCINI:
2689
                case TIOCSCCCHANINI:
2690
                case TIOCSCCSMEM:
2691
                case TIOCSCCSKISS:
2692
                case TIOCSCCGKISS:
2693
 
2694
                case TIOCSCCCFG_OLD:
2695
                case TIOCSCCINI_OLD:
2696
                case TIOCCHANINI_OLD:
2697
                case TIOCCHANMEM_OLD:
2698
                case TIOCSKISS_OLD:
2699
                case TIOCGKISS_OLD:
2700
                case TIOCSCCSTAT_OLD:
2701
                        return scc_ioctl(scc, cmd, (void *) arg);
2702
 
2703
                case TCSBRK:
2704
                        return 0;
2705
 
2706
                case TIOCMGET:
2707
                        error = verify_area(VERIFY_WRITE, (void *) arg,sizeof(unsigned int *));
2708
                        if (error)
2709
                                return error;
2710
 
2711
                        save_flags(flags);
2712
                        cli();
2713
 
2714
                        result =  ((scc->wreg[R5] & RTS) ? TIOCM_RTS : 0)
2715
                                | ((scc->wreg[R5] & DTR) ? TIOCM_DTR : 0)
2716
                                | ((InReg(scc->ctrl,R0) & DCD)  ? TIOCM_CAR : 0)
2717
                                | ((InReg(scc->ctrl,R0) & CTS)  ? TIOCM_CTS : 0);
2718
 
2719
                        restore_flags(flags);
2720
 
2721
                        put_user(result,(unsigned int *) arg);
2722
                        return 0;
2723
 
2724
                case TIOCMBIS:
2725
                case TIOCMBIC:
2726
                case TIOCMSET:
2727
                        switch (cmd)
2728
                        {
2729
                                case TIOCMBIS:
2730
                                        scc->wreg[R5] |= DTR;
2731
                                        scc->wreg[R5] |= RTS;
2732
                                        break;
2733
                                case TIOCMBIC:
2734
                                        scc->wreg[R5] &= ~DTR;
2735
                                        scc->wreg[R5] &= ~RTS;
2736
                                        break;
2737
                                case TIOCMSET:
2738
                                        error = verify_area(VERIFY_READ, (void *) arg,sizeof(unsigned int *));
2739
                                        if (error)
2740
                                                return error;
2741
                                        value = get_user((unsigned int *) arg);
2742
 
2743
                                        if(value & TIOCM_DTR)
2744
                                                scc->wreg[R5] |= DTR;
2745
                                        else
2746
                                                scc->wreg[R5] &= ~DTR;
2747
                                        if(value & TIOCM_RTS)
2748
                                                scc->wreg[R5] |= RTS;
2749
                                        else
2750
                                                scc->wreg[R5] &= ~RTS;
2751
                                        break;
2752
                        }
2753
 
2754
                        OutReg(scc->ctrl, R5, scc->wreg[R5]);
2755
                        return 0;
2756
 
2757
                case TCGETS:
2758
                        error = verify_area(VERIFY_WRITE, (void *) arg, sizeof(struct termios));
2759
                        if (error)
2760
                                return error;
2761
                        if (!arg)
2762
                                return -EFAULT;
2763
 
2764
                        memcpy_tofs((void *) arg, scc->tty->termios, sizeof(struct termios));
2765
                        return 0;
2766
 
2767
                case TCSETS:
2768
                case TCSETSF:           /* should flush first, but... */
2769
                case TCSETSW:           /* should wait 'till flush, but... */
2770
                        if (!arg)
2771
                                return -EFAULT;
2772
 
2773
                        error = verify_area(VERIFY_READ, (void *) arg, sizeof(struct termios));
2774
                        if (error)
2775
                                return error;
2776
                        memcpy_fromfs(scc->tty->termios, (void *) arg, sizeof(struct termios));
2777
                        scc_change_speed(scc);
2778
                        return 0;
2779
 
2780
        }
2781
 
2782
        return -ENOIOCTLCMD;
2783
}
2784
 
2785
 
2786
/* ----> tx routine: decode KISS data and scc_enqueue it <---- */
2787
 
2788
/* send raw frame to SCC. used for AX.25 */
2789
int
2790
scc_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count)
2791
{
2792
        struct scc_channel * scc = tty->driver_data;
2793
        unsigned char *p;
2794
        int cnt, cnt2;
2795
 
2796
        if (!tty)
2797
                return -EINVAL;
2798
 
2799
        if (scc_paranoia_check(scc, tty->device, "scc_write"))
2800
                return -EINVAL;
2801
 
2802
        if (scc->kiss.tx_inhibit) return count;
2803
 
2804
        cnt2 = count;
2805
 
2806
        while (cnt2)
2807
        {
2808
                cnt   = cnt2 > BUFSIZE? BUFSIZE:cnt2;
2809
                cnt2 -= cnt;
2810
 
2811
                if (from_user)
2812
                {
2813
                        down(&scc_sem);
2814
                        memcpy_fromfs(scc_wbuf, buf, cnt);
2815
                        up(&scc_sem);
2816
                }
2817
                else
2818
                        memcpy(scc_wbuf, buf, cnt);
2819
 
2820
                /* The timeout of the slip driver is very small, */
2821
                /* thus we'll wake him up now.                   */
2822
 
2823
                if (cnt2 == 0)
2824
                {
2825
                        wake_up_interruptible(&tty->write_wait);
2826
 
2827
                        if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
2828
                            tty->ldisc.write_wakeup)
2829
                                (tty->ldisc.write_wakeup)(tty);
2830
                } else
2831
                        buf += cnt;
2832
 
2833
                p=scc_wbuf;
2834
 
2835
                while(cnt--)
2836
                  if (kiss_decode(scc, *p++))
2837
                        return -ENOMEM;
2838
        } /* while cnt2 */
2839
 
2840
        return count;
2841
}
2842
 
2843
 
2844
/* put a single char into the buffer */
2845
 
2846
static void
2847
scc_put_char(struct tty_struct * tty, unsigned char ch)
2848
{
2849
        struct scc_channel *scc = tty->driver_data;
2850
 
2851
        if (scc_paranoia_check(scc, tty->device, "scc_put_char"))
2852
                return;
2853
 
2854
        kiss_decode(scc, ch);
2855
}
2856
 
2857
static void
2858
scc_flush_chars(struct tty_struct * tty)
2859
{
2860
        struct scc_channel *scc = tty->driver_data;
2861
 
2862
        scc_paranoia_check(scc, tty->device, "scc_flush_chars"); /* just to annoy the user... */
2863
 
2864
        return; /* no flush needed */
2865
}
2866
 
2867
 
2868
static int
2869
scc_write_room(struct tty_struct *tty)
2870
{
2871
        struct scc_channel *scc = tty->driver_data;
2872
 
2873
        if (scc_paranoia_check(scc, tty->device, "scc_write_room"))
2874
                return 0;
2875
 
2876
        return BUFSIZE;
2877
}
2878
 
2879
static int
2880
scc_chars_in_buffer(struct tty_struct *tty)
2881
{
2882
        struct scc_channel *scc = tty->driver_data;
2883
 
2884
        if (scc && scc->kiss_decode_bp)
2885
                return scc->kiss_decode_bp->cnt;
2886
        else
2887
                return 0;
2888
}
2889
 
2890
static void
2891
scc_flush_buffer(struct tty_struct *tty)
2892
{
2893
        struct scc_channel *scc = tty->driver_data;
2894
 
2895
        if (scc_paranoia_check(scc, tty->device, "scc_flush_buffer"))
2896
                return;
2897
        wake_up_interruptible(&tty->write_wait);
2898
        if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
2899
            tty->ldisc.write_wakeup)
2900
                (tty->ldisc.write_wakeup)(tty);
2901
}
2902
 
2903
static void
2904
scc_throttle(struct tty_struct *tty)
2905
{
2906
        struct scc_channel *scc = tty->driver_data;
2907
 
2908
        if (scc_paranoia_check(scc, tty->device, "scc_throttle"))
2909
                return;
2910
 
2911
#ifdef SCC_DEBUG                
2912
        printk(KERN_DEBUG "z8530drv: scc_throttle() called for device %d\n", MINOR(tty->device));
2913
#endif
2914
        scc->throttled = 1;
2915
 
2916
        del_timer(&(scc->rx_t));
2917
        scc->rx_t.expires = jiffies + HZ/25;
2918
        add_timer(&scc->rx_t);
2919
}
2920
 
2921
static void
2922
scc_unthrottle(struct tty_struct *tty)
2923
{
2924
        struct scc_channel *scc = tty->driver_data;
2925
 
2926
        if (scc_paranoia_check(scc, tty->device, "scc_unthrottle"))
2927
                return;
2928
 
2929
#ifdef SCC_DEBUG
2930
        printk(KERN_DEBUG "z8350drv: scc_unthrottle() called for device %d\n", MINOR(tty->device));
2931
#endif          
2932
        scc->throttled = 0;
2933
        del_timer(&(scc->rx_t));
2934
}
2935
 
2936
/* experimental, the easiest way to stop output is a fake scc_throttle */
2937
 
2938
static void
2939
scc_start(struct tty_struct *tty)
2940
{
2941
        struct scc_channel *scc = tty->driver_data;
2942
 
2943
        if (scc_paranoia_check(scc, tty->device, "scc_start"))
2944
                return;
2945
 
2946
        scc_unthrottle(tty);
2947
}
2948
 
2949
static void
2950
scc_stop(struct tty_struct *tty)
2951
{
2952
        struct scc_channel *scc = tty->driver_data;
2953
 
2954
        if (scc_paranoia_check(scc, tty->device, "scc_stop"))
2955
                return;
2956
 
2957
        scc_throttle(tty);
2958
}
2959
 
2960
static void
2961
scc_set_termios(struct tty_struct * tty, struct termios * old_termios)
2962
{
2963
        struct scc_channel *scc = tty->driver_data;
2964
 
2965
        if (scc_paranoia_check(scc, tty->device, "scc_set_termios"))
2966
                return;
2967
 
2968
        if (old_termios && (tty->termios->c_cflag == old_termios->c_cflag))
2969
                return;
2970
 
2971
        scc_change_speed(scc);
2972
}
2973
 
2974
static void
2975
scc_set_ldisc(struct tty_struct * tty)
2976
{
2977
        struct scc_channel *scc = tty->driver_data;
2978
 
2979
        if (scc_paranoia_check(scc, tty->device, "scc_set_ldisc"))
2980
                return;
2981
 
2982
        scc_change_speed(scc);
2983
}
2984
#endif /* CONFIG_SCC_TTY */
2985
 
2986
#ifdef CONFIG_SCC_DEV
2987
/* ******************************************************************** */
2988
/* *                        Network driver routines                   * */
2989
/* ******************************************************************** */
2990
 
2991
static unsigned char ax25_bcast[AX25_ADDR_LEN] =
2992
{'Q' << 1, 'S' << 1, 'T' << 1, ' ' << 1, ' ' << 1, ' ' << 1, '0' << 1};
2993
static unsigned char ax25_nocall[AX25_ADDR_LEN] =
2994
{'L' << 1, 'I' << 1, 'N' << 1, 'U' << 1, 'X' << 1, ' ' << 1, '1' << 1};
2995
 
2996
static int
2997
scc_net_init(struct device *dev)
2998
{
2999
        return 0;        /* dummy */
3000
}
3001
 
3002
/* ----> open network device <---- */
3003
 
3004
static int
3005
scc_net_open(struct device *dev)
3006
{
3007
        struct scc_channel *scc = (struct scc_channel *) dev->priv;
3008
 
3009
        if (scc == NULL || scc->magic != SCC_MAGIC)
3010
                return -ENODEV;
3011
 
3012
        if (scc->tty != NULL)
3013
                return -EBUSY;
3014
 
3015
        if (!scc->init)
3016
                return -EINVAL;
3017
 
3018
        scc->stat.is_netdev = 1;
3019
 
3020
        MOD_INC_USE_COUNT;
3021
 
3022
        alloc_buffer_pool(scc);
3023
 
3024
        scc->stat.tx_kiss_state = KISS_IDLE;    /* unused */
3025
        scc->stat.rx_kiss_state = KISS_IDLE;    /* unused */
3026
 
3027
        init_channel(scc);
3028
 
3029
        dev->tbusy = 0;
3030
        dev->start = 1;
3031
 
3032
        return 0;
3033
}
3034
 
3035
/* ----> close network device <---- */
3036
 
3037
static int
3038
scc_net_close(struct device *dev)
3039
{
3040
        struct scc_channel *scc = (struct scc_channel *) dev->priv;
3041
        unsigned long flags;
3042
 
3043
        if (scc == NULL || scc->magic != SCC_MAGIC)
3044
                return -ENODEV;
3045
 
3046
        if (!scc->stat.is_netdev)
3047
                return -ENXIO;
3048
 
3049
        MOD_DEC_USE_COUNT;
3050
 
3051
        scc->stat.is_netdev = 0;
3052
 
3053
        save_flags(flags);
3054
        cli();
3055
 
3056
        Outb(scc->ctrl,0);               /* Make sure pointer is written */
3057
        wr(scc,R1,0);                    /* disable interrupts */
3058
        wr(scc,R3,0);
3059
 
3060
        del_timer(&scc->tx_t);
3061
        del_timer(&scc->tx_wdog);
3062
#ifdef CONFIG_SCC_TTY
3063
        del_timer(&scc->rx_t);
3064
#endif
3065
        restore_flags(flags);
3066
 
3067
        free_buffer_pool(scc);
3068
 
3069
        dev->tbusy = 1;
3070
        dev->start = 0;
3071
 
3072
        return 0;
3073
}
3074
 
3075
/* ----> receive frame, called from scc_rxint() <---- */
3076
 
3077
static void
3078
scc_net_rx(struct scc_channel *scc, struct mbuf *bp)
3079
{
3080
        struct sk_buff *skb;
3081
 
3082
        if ( bp == NULL || bp->cnt == 0 ||
3083
            scc == NULL || scc->magic != SCC_MAGIC)
3084
                return;
3085
 
3086
        skb = dev_alloc_skb(bp->cnt+1);
3087
 
3088
        if (skb == NULL)
3089
        {
3090
                scc->dev_stat.rx_dropped++;
3091
                return;
3092
        }
3093
 
3094
        scc->dev_stat.rx_packets++;
3095
 
3096
        skb->dev = scc->dev;
3097
        skb->protocol = htons(ETH_P_AX25);
3098
 
3099
        memcpy(skb_put(skb, bp->cnt), bp->data, bp->cnt);
3100
 
3101
        skb->mac.raw = skb->data;
3102
        netif_rx(skb);
3103
 
3104
        return;
3105
}
3106
 
3107
/* ----> transmit frame <---- */
3108
 
3109
static int
3110
scc_net_tx(struct sk_buff *skb, struct device *dev)
3111
{
3112
        struct scc_channel *scc = (struct scc_channel *) dev->priv;
3113
        struct mbuf *bp;
3114
        long ticks;
3115
 
3116
        /*
3117
         * We have our own queues, dev->tbusy is set only by t_maxkeyup
3118
         * to avoid running the driver into maxkeup again and again.
3119
         *
3120
         * Hope I havn't outsmart me _again_ ;-)
3121
         */
3122
 
3123
        if (dev->tbusy)
3124
        {
3125
                ticks = (signed) (jiffies - dev->trans_start);
3126
 
3127
                if (ticks < scc->kiss.maxdefer*HZ || scc == NULL)
3128
                        return 1;
3129
 
3130
                /*
3131
                 * Arrgh... Seems to be a _very_ busy channel.
3132
                 * throw away transmission queue.
3133
                 */
3134
 
3135
                del_timer(&scc->tx_wdog);
3136
                t_busy((unsigned long) scc);
3137
 
3138
                dev->tbusy=0;
3139
                dev->trans_start = jiffies;
3140
        }
3141
 
3142
        if (skb == NULL)
3143
        {
3144
                dev_tint(dev);
3145
                return 0;
3146
        }
3147
 
3148
        if (scc == NULL || scc->magic != SCC_MAGIC)
3149
        {
3150
                dev_kfree_skb(skb, FREE_WRITE);
3151
                return 0;
3152
        }
3153
 
3154
        scc->dev_stat.tx_packets++;
3155
        bp = scc_get_buffer(scc, BT_TRANSMIT);
3156
 
3157
        if (bp == NULLBUF || skb->len > scc->stat.bufsize || skb->len == 0)
3158
        {
3159
                scc->dev_stat.tx_dropped++;
3160
                if (bp) scc_enqueue_buffer(&scc->tx_buffer_pool, bp);
3161
                dev_kfree_skb(skb, FREE_WRITE);
3162
                return 0;
3163
        }
3164
 
3165
        memcpy(bp->data, skb->data, skb->len);
3166
        bp->cnt = skb->len;
3167
 
3168
        dev_kfree_skb(skb, FREE_WRITE);
3169
 
3170
        scc->kiss_decode_bp = bp;
3171
        kiss_interpret_frame(scc);
3172
        dev->trans_start = jiffies;
3173
        return 0;
3174
}
3175
 
3176
/* ----> set interface callsign <---- */
3177
 
3178
static int
3179
scc_net_set_mac_address(struct device *dev, void *addr)
3180
{
3181
        struct sockaddr *sa = (struct sockaddr *) addr;
3182
        memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
3183
        return 0;
3184
}
3185
 
3186
/* ----> rebuild header <---- */
3187
 
3188
static int
3189
scc_net_rebuild_header(void *buff, struct device *dev, unsigned long raddr, struct sk_buff *skb)
3190
{
3191
    return ax25_rebuild_header(buff, dev, raddr, skb);
3192
}
3193
 
3194
/* ----> "hard" header <---- */
3195
 
3196
static int
3197
scc_net_header(struct sk_buff *skb, struct device *dev, unsigned short type,
3198
               void *daddr, void *saddr, unsigned len)
3199
{
3200
    return ax25_encapsulate(skb, dev, type, daddr, saddr, len);
3201
}
3202
 
3203
/* ----> get statistics <---- */
3204
 
3205
static struct enet_statistics *
3206
scc_net_get_stats(struct device *dev)
3207
{
3208
        struct scc_channel *scc = (struct scc_channel *) dev->priv;
3209
 
3210
        if (scc == NULL || scc->magic != SCC_MAGIC)
3211
                return NULL;
3212
 
3213
        scc->dev_stat.rx_errors = scc->stat.rxerrs + scc->stat.rx_over;
3214
        scc->dev_stat.tx_errors = scc->stat.txerrs + scc->stat.tx_under;
3215
        scc->dev_stat.rx_fifo_errors = scc->stat.rx_over;
3216
        scc->dev_stat.tx_fifo_errors = scc->stat.tx_under;
3217
 
3218
        return &scc->dev_stat;
3219
}
3220
 
3221
/* ----> ioctl for network devices <---- */
3222
 
3223
static int
3224
scc_net_ioctl(struct device *dev, struct ifreq *ifr, int cmd)
3225
{
3226
        struct scc_channel *scc = (struct scc_channel *) dev->priv;
3227
        unsigned int ncmd = 0;
3228
        int res;
3229
 
3230
        if (scc == NULL || scc->magic != SCC_MAGIC)
3231
                return -EINVAL;
3232
 
3233
        if (!Driver_Initialized && cmd == SIOCSCCINI)
3234
        {
3235
                scc->dev = dev;
3236
                scc->stat.is_netdev = 1;
3237
        }
3238
 
3239
        switch (cmd)
3240
        {
3241
                case SIOCSCCRESERVED:   return -EINVAL;      /* unused */
3242
                case SIOCSCCCFG:        ncmd = TIOCSCCCFG;      break;
3243
                case SIOCSCCINI:        ncmd = TIOCSCCINI;      break;
3244
                case SIOCSCCCHANINI:    ncmd = TIOCSCCCHANINI;  break;
3245
                case SIOCSCCSMEM:       ncmd = TIOCSCCSMEM;     break;
3246
                case SIOCSCCGKISS:      ncmd = TIOCSCCGKISS;    break;
3247
                case SIOCSCCSKISS:      ncmd = TIOCSCCSKISS;    break;
3248
                case SIOCSCCGSTAT:      ncmd = TIOCSCCGSTAT;    break;
3249
                default:                return -EINVAL;
3250
        }
3251
 
3252
        res = scc_ioctl(scc, ncmd, (void *) ifr->ifr_data);
3253
 
3254
#ifdef CONFIG_SCC_TTY
3255
        if (!(dev->flags & IFF_UP))
3256
                scc->stat.is_netdev = 0;
3257
#endif
3258
 
3259
        return res;
3260
 
3261
}
3262
 
3263
 
3264
/* ----> initialize interface <---- */
3265
 
3266
static int
3267
scc_net_setup(struct scc_channel *scc, unsigned char *name)
3268
{
3269
        int k;
3270
        unsigned char *buf;
3271
        struct device *dev;
3272
 
3273
        if (dev_get(name) != NULL)
3274
        {
3275
                printk(KERN_INFO "Z8530drv: device %s already exists.\n", name);
3276
                return -EEXIST;
3277
        }
3278
 
3279
        if ((scc->dev = (struct device *) kmalloc(sizeof(struct device), GFP_KERNEL)) == NULL)
3280
                return -ENOMEM;
3281
 
3282
#ifndef CONFIG_SCC_TTY
3283
        scc->stat.is_netdev = 1;
3284
#endif
3285
        dev = scc->dev;
3286
        memset(dev, 0, sizeof(struct device));
3287
 
3288
        buf = (unsigned char *) kmalloc(10, GFP_KERNEL);
3289
        strcpy(buf, name);
3290
 
3291
        dev->priv = (void *) scc;
3292
        dev->name = buf;
3293
        dev->init = scc_net_init;
3294
 
3295
        if (register_netdev(dev) != 0)
3296
        {
3297
                kfree(dev);
3298
                return -EIO;
3299
        }
3300
 
3301
        for (k=0; k < DEV_NUMBUFFS; k++)
3302
                skb_queue_head_init(&dev->buffs[k]);
3303
 
3304
        dev->open            = scc_net_open;
3305
        dev->stop            = scc_net_close;
3306
 
3307
        dev->hard_start_xmit = scc_net_tx;
3308
        dev->hard_header     = scc_net_header;
3309
        dev->rebuild_header  = scc_net_rebuild_header;
3310
        dev->set_mac_address = scc_net_set_mac_address;
3311
        dev->get_stats       = scc_net_get_stats;
3312
        dev->do_ioctl        = scc_net_ioctl;
3313
 
3314
        memcpy(dev->broadcast, ax25_bcast,  AX25_ADDR_LEN);
3315
        memcpy(dev->dev_addr,  ax25_nocall, AX25_ADDR_LEN);
3316
 
3317
        dev->flags      = 0;
3318
        dev->family     = AF_INET;
3319
        dev->pa_addr    = 0;
3320
        dev->pa_brdaddr = 0;
3321
        dev->pa_mask    = 0;
3322
        dev->pa_alen    = 4;
3323
 
3324
        dev->type = ARPHRD_AX25;
3325
        dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN;
3326
        dev->mtu = AX25_DEF_PACLEN;
3327
        dev->addr_len = AX25_ADDR_LEN;
3328
 
3329
        return 0;
3330
}
3331
#endif /* CONFIG_SCC_DEV */
3332
 
3333
/* ******************************************************************** */
3334
/* *            dump statistics to /proc/net/z8530drv                 * */
3335
/* ******************************************************************** */
3336
 
3337
 
3338
static int
3339
scc_net_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
3340
{
3341
        struct scc_channel *scc;
3342
        char *txstate, *clksrc, *brand;
3343
        struct scc_kiss *kiss;
3344
        struct scc_stat *stat;
3345
        int len = 0;
3346
        off_t pos = 0;
3347
        off_t begin = 0;
3348
        int k;
3349
 
3350
        if (!Driver_Initialized)
3351
        {
3352
                len += sprintf(buffer, "Driver not initialized.\n");
3353
                goto done;
3354
        }
3355
 
3356
        if (!Nchips)
3357
        {
3358
                len += sprintf(buffer, "Z8530 chips not found\n");
3359
                goto done;
3360
        }
3361
 
3362
        for (k = 0; k < Nchips*2; k++)
3363
        {
3364
                scc = &SCC_Info[k];
3365
                stat = &scc->stat;
3366
                kiss = &scc->kiss;
3367
 
3368
                if (!scc->init)
3369
                        continue;
3370
 
3371
                switch(stat->tx_state)
3372
                {
3373
                        case TXS_IDLE:          txstate = "idle";       break;
3374
                        case TXS_BUSY:          txstate = "busy";       break;
3375
                        case TXS_ACTIVE:        txstate = "active";     break;
3376
                        case TXS_NEWFRAME:      txstate = "new";        break;
3377
                        case TXS_IDLE2:         txstate = "keyed";      break;
3378
                        case TXS_WAIT:          txstate = "wait";       break;
3379
                        case TXS_TIMEOUT:       txstate = "timeout";    break;
3380
                        default:                txstate = "???";
3381
                }
3382
                switch(scc->modem.clocksrc)
3383
                {
3384
                        case CLK_DPLL:          clksrc = "dpll";        break;
3385
                        case CLK_EXTERNAL:      clksrc = "ext";         break;
3386
                        case CLK_DIVIDER:       clksrc = "div";         break;
3387
                        default:                clksrc = "???";
3388
                }
3389
                switch(scc->brand)
3390
                {
3391
                        case PA0HZP:    brand="pa0hzp"; break;
3392
                        case EAGLE:     brand="eagle";  break;
3393
                        case PC100:     brand="pc1100"; break;
3394
                        case PRIMUS:    brand="primus"; break;
3395
                        case DRSI:      brand="drsi";   break;
3396
                        case BAYCOM:    brand="baycom"; break;
3397
                        default:        brand="???";
3398
                }
3399
 
3400
                len += sprintf(buffer+len, "Device     : %s%d\n", SCC_DriverName, k);
3401
                len += sprintf(buffer+len, "Hardware   : %3.3x %3.3x %d %lu %s %s %3.3x %3.3x %d\n",
3402
                                scc->data, scc->ctrl, scc->irq, scc->clock, brand,
3403
                                scc->enhanced? "enh":"norm", Vector_Latch, scc->special,
3404
                                scc->option);
3405
                len += sprintf(buffer+len, "Received   : %lu %lu %d\n",
3406
                                stat->rxframes, stat->rxerrs, stat->rx_over);
3407
                len += sprintf(buffer+len, "Transmitted: %lu %lu %d %s\n",
3408
                                stat->txframes, stat->txerrs, stat->tx_under, txstate);
3409
                len += sprintf(buffer+len, "Interrupts : %lu %lu %lu %lu\n",
3410
                                stat->rxints, stat->txints, stat->exints, stat->spints);
3411
                len += sprintf(buffer+len, "Buffers    : %d %d/%d %d/%d %d\n",
3412
                                stat->bufsize, stat->rx_queued, stat->rxbuffers,
3413
                                stat->tx_queued, stat->txbuffers, stat->nospace);
3414
                len += sprintf(buffer+len, "MODEM      : %lu %s %s %s\n",
3415
                                scc->modem.speed, scc->modem.nrz? "nrz":"nrzi",
3416
                                clksrc, kiss->softdcd? "soft":"hard");
3417
                len += sprintf(buffer+len, "Mode       : %s\n",
3418
                                stat->is_netdev? "dev":"tty");
3419
#define K(x) kiss->x
3420
                len += sprintf(buffer+len, "KISS params: %d %d %d %d %d %d %d %d %d %d %d %d\n",
3421
                                K(txdelay), K(persist), K(slottime), K(tailtime),
3422
                                K(fulldup), K(waittime), K(mintime), K(maxkeyup),
3423
                                K(idletime), K(maxdefer), K(tx_inhibit), K(group));
3424
#undef K
3425
#ifdef SCC_DEBUG
3426
                {
3427
                        int reg;
3428
 
3429
                        len += sprintf(buffer+len, "Z8530 wregs: ");
3430
                        for (reg = 0; reg < 16; reg++)
3431
                                len += sprintf(buffer+len, "%2.2x ", scc->wreg[reg]);
3432
                        len += sprintf(buffer+len, "\n");
3433
 
3434
                        len += sprintf(buffer+len, "Z8530 rregs: %2.2x %2.2x XX ", InReg(scc->ctrl,R0), InReg(scc->ctrl,R1));
3435
                        for (reg = 3; reg < 8; reg++)
3436
                                len += sprintf(buffer+len, "%2.2x ", InReg(scc->ctrl, reg));
3437
                        len += sprintf(buffer+len, "XX ");
3438
                        for (reg = 9; reg < 16; reg++)
3439
                                len += sprintf(buffer+len, "%2.2x ", InReg(scc->ctrl, reg));
3440
                        len += sprintf(buffer+len, "\n");
3441
                }
3442
#endif
3443
                len += sprintf(buffer+len, "\n");
3444
 
3445
                pos = begin + len;
3446
 
3447
                if (pos < offset) {
3448
                        len   = 0;
3449
                        begin = pos;
3450
                }
3451
 
3452
                if (pos > offset + length)
3453
                        break;
3454
        }
3455
 
3456
done:
3457
 
3458
        *start = buffer + (offset - begin);
3459
        len   -= (offset - begin);
3460
 
3461
        if (len > length) len = length;
3462
 
3463
        return len;
3464
}
3465
 
3466
#ifdef CONFIG_INET
3467
struct proc_dir_entry scc_proc_dir_entry =
3468
{
3469
  PROC_NET_Z8530, 8, "z8530drv", S_IFREG | S_IRUGO, 1, 0, 0, 0,
3470
  &proc_net_inode_operations, scc_net_get_info
3471
};
3472
 
3473
#define scc_net_procfs_init()   proc_net_register(&scc_proc_dir_entry);
3474
#define scc_net_procfs_remove() proc_net_unregister(PROC_NET_Z8530);
3475
#else
3476
#define scc_net_procfs_init()
3477
#define scc_net_procfs_remove()
3478
#endif
3479
 
3480
 
3481
/* ******************************************************************** */
3482
/* *                    Init SCC driver                               * */
3483
/* ******************************************************************** */
3484
 
3485
int scc_init (void)
3486
{
3487
        int chip, chan, k;
3488
#ifdef CONFIG_SCC_DEV
3489
        char devname[10];
3490
#endif
3491
 
3492
        printk(KERN_INFO BANNER);
3493
 
3494
#ifdef CONFIG_SCC_TTY
3495
        memset(&scc_std_termios, 0, sizeof(struct termios));
3496
        memset(&scc_driver, 0, sizeof(struct tty_driver));
3497
        scc_driver.magic = TTY_DRIVER_MAGIC;
3498
        scc_driver.name = SCC_DriverName;
3499
        scc_driver.major = Z8530_MAJOR;
3500
        scc_driver.minor_start = 0;
3501
        scc_driver.num = MAXSCC*2;
3502
        scc_driver.type = TTY_DRIVER_TYPE_SERIAL;
3503
        scc_driver.subtype = 1;                 /* not needed */
3504
        scc_driver.init_termios = scc_std_termios;
3505
        scc_driver.init_termios.c_cflag = B9600  | CREAD | CS8 | HUPCL | CLOCAL;
3506
        scc_driver.init_termios.c_iflag = IGNBRK | IGNPAR;
3507
        scc_driver.flags = TTY_DRIVER_REAL_RAW;
3508
        scc_driver.refcount = &scc_refcount;
3509
        scc_driver.table = scc_table;
3510
        scc_driver.termios = (struct termios **) scc_termios;
3511
        scc_driver.termios_locked = (struct termios **) scc_termios_locked;
3512
        scc_driver.open = scc_open;
3513
        scc_driver.close = scc_close;
3514
        scc_driver.write = scc_write;
3515
        scc_driver.start = scc_start;
3516
        scc_driver.stop = scc_stop;
3517
 
3518
        scc_driver.put_char = scc_put_char;
3519
        scc_driver.flush_chars = scc_flush_chars;
3520
        scc_driver.write_room = scc_write_room;
3521
        scc_driver.chars_in_buffer = scc_chars_in_buffer;
3522
        scc_driver.flush_buffer = scc_flush_buffer;
3523
 
3524
        scc_driver.throttle = scc_throttle;
3525
        scc_driver.unthrottle = scc_unthrottle;
3526
 
3527
        scc_driver.ioctl = scc_tty_ioctl;
3528
        scc_driver.set_termios = scc_set_termios;
3529
        scc_driver.set_ldisc = scc_set_ldisc;
3530
 
3531
 
3532
        if (tty_register_driver(&scc_driver))
3533
        {
3534
                printk(KERN_ERR "Failed to register Z8530 SCC driver\n");
3535
                return -EIO;
3536
        }
3537
#endif
3538
 
3539
        memset(&SCC_ctrl, 0, sizeof(SCC_ctrl));
3540
 
3541
        /* pre-init channel information */
3542
 
3543
        for (chip = 0; chip < MAXSCC; chip++)
3544
        {
3545
                memset((char *) &SCC_Info[2*chip  ], 0, sizeof(struct scc_channel));
3546
                memset((char *) &SCC_Info[2*chip+1], 0, sizeof(struct scc_channel));
3547
 
3548
                for (chan = 0; chan < 2; chan++)
3549
                        SCC_Info[2*chip+chan].magic    = SCC_MAGIC;
3550
        }
3551
 
3552
        for (k = 0; k < 16; k++) Ivec[k].used = 0;
3553
 
3554
#ifdef CONFIG_SCC_DEV
3555
        sprintf(devname,"%s0", SCC_DriverName);
3556
 
3557
        if (scc_net_setup(SCC_Info, devname))
3558
                printk(KERN_WARNING "z8530drv: cannot setup network device\n");
3559
#endif
3560
        scc_net_procfs_init();
3561
 
3562
        return 0;
3563
}
3564
 
3565
/* ******************************************************************** */
3566
/* *                        Module support                            * */
3567
/* ******************************************************************** */
3568
 
3569
 
3570
#ifdef MODULE
3571
int init_module(void)
3572
{
3573
        int result = 0;
3574
 
3575
        result = scc_init();
3576
 
3577
        if (result == 0)
3578
                printk(KERN_INFO "Copyright 1993,1996 Joerg Reuter DL1BKE (jreuter@lykos.tng.oche.de)\n");
3579
 
3580
        return result;
3581
}
3582
 
3583
void cleanup_module(void)
3584
{
3585
        long flags;
3586
        io_port ctrl;
3587
        int k;
3588
        struct scc_channel *scc;
3589
 
3590
        save_flags(flags);
3591
        cli();
3592
 
3593
#ifdef CONFIG_SCC_TTY
3594
        if ( (k = tty_unregister_driver(&scc_driver)) )
3595
        {
3596
                printk(KERN_ERR "z8530drv: failed to unregister tty driver: (%d)\n", -k);
3597
                restore_flags(flags);
3598
                return;
3599
        }
3600
#endif
3601
 
3602
        if (Nchips == 0)
3603
                unregister_netdev(SCC_Info[0].dev);
3604
 
3605
        for (k = 0; k < Nchips; k++)
3606
                if ( (ctrl = SCC_ctrl[k].chan_A) )
3607
                {
3608
                        Outb(ctrl, 0);
3609
                        OutReg(ctrl,R9,FHWRES); /* force hardware reset */
3610
                        udelay(50);
3611
                }
3612
 
3613
        for (k = 0; k < Nchips*2; k++)
3614
        {
3615
                scc = &SCC_Info[k];
3616
                if (scc)
3617
                {
3618
                        release_region(scc->ctrl, 1);
3619
                        release_region(scc->data, 1);
3620
                        if (scc->dev)
3621
                        {
3622
                                unregister_netdev(scc->dev);
3623
                                kfree(scc->dev);
3624
                        }
3625
                }
3626
        }
3627
 
3628
        for (k=0; k < 16 ; k++)
3629
                if (Ivec[k].used) free_irq(k, NULL);
3630
 
3631
        restore_flags(flags);
3632
 
3633
        scc_net_procfs_remove();
3634
}
3635
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.