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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [drivers/] [net/] [pi2.c] - Blame information for rev 1777

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

Line No. Rev Author Line
1 1626 jcastillo
/*
2
   pi2.c: Driver for the Ottawa Amateur Radio Club PI and PI2 interface.
3
   Copyright (c) 1994 David Perry
4
 
5
   This program is free software; you can redistribute it and/or modify
6
   it under the terms of the GNU General Public License version 2, as
7
   published by the Free Software Foundation.
8
 
9
   This program is distributed in the hope that it will be useful, but
10
   WITHOUT ANY WARRANTY; without even the implied warranty of
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
   General Public License for more details.
13
 
14
   You should have received a copy of the GNU General Public License
15
   along with this program; if not, write to the Free Software Foundation,
16
   Inc., 675 Mass Ave, Cambridge MA 02139, USA.
17
 
18
   The file skeleton.c by Donald Becker was used as a starting point
19
   for this driver.
20
 
21
   Revision History
22
 
23
   April 6, 1994  (dp) Created
24
                       version 0.0 ALPHA
25
   April 10, 1994 (dp) Included cleanup, suggestions from J. P. Morrison.
26
                       version 0.1 ALPHA
27
   April 13, 1994 (dp) Included address probing from JPM, autoirq
28
                       version 0.2 ALPHA
29
   April 14, 1994 (ac) Sketched in the NET3 changes.
30
   April 17, 1994 (dp) Finished the NET3 changes. Used init_etherdev()
31
                       instead of kmalloc() to ensure that DMA buffers will
32
                       reside under the 16 meg line.
33
                       version 0.4 ALPHA
34
   April 18, 1994 (dp) Now using the kernel provided sk_buff handling functions.
35
                       Fixed a nasty problem with DMA.
36
                       version 0.5 ALPHA
37
   June 6, 1994 (ac)   Fixed to match the buffer locking changes. Added a hack to
38
                       fix a funny I see (search for HACK) and fixed the calls in
39
                       init() so it doesn't migrate module based ethernet cards up
40
                       to eth2 Took out the old module ideas as they are no longer
41
                       relevant to the PI driver.
42
   July 16, 1994 (dp)  Fixed the B channel rx overrun problem ac referred to
43
                       above. Also added a bit of a hack to improve the maximum
44
                       baud rate on the B channel (Search for STUFF2). Included
45
                       ioctl stuff from John Paul Morrison. version 0.6 ALPHA
46
   Feb 9, 1995 (dp)    Updated for 1.1.90 kernel
47
                       version 0.7 ALPHA
48
   Apr 6, 1995 (ac)    Tweaks for NET3 pre snapshot 002 AX.25
49
   April 23, 1995 (dp) Fixed ioctl so it works properly with piconfig program
50
                       when changing the baud rate or clock mode.
51
                       version 0.8 ALPHA
52
   July 17, 1995 (ac)  Finally polishing of AX25.030+ support
53
   Oct  29, 1995 (ac)  A couple of minor fixes before this, and this release changes
54
                       to the proper set_mac_address semantics which will break
55
                       a few programs I suspect.
56
   Aug  18, 1996 (jsn) Converted to be used as a module.
57
*/
58
 
59
/* The following #define invokes a hack that will improve performance (baud)
60
   for the B port. The up side is it makes 9600 baud work ok on the B port.
61
   It may do 38400, depending on the host. The down side is it burns up
62
   CPU cycles with ints locked for up to 1 character time, at the beginning
63
   of each transmitted packet. If this causes you to lose sleep, #undefine it.
64
*/
65
 
66
/*#define STUFF2 1*/
67
 
68
/* The default configuration */
69
#define PI_DMA 3
70
 
71
#define DEF_A_SPEED 0           /* 0 means external clock */
72
#define DEF_A_TXDELAY 15        /* 15 mS transmit delay */
73
#define DEF_A_PERSIST 128       /* 50% persistence */
74
#define DEF_A_SLOTIME 15        /* 15 mS slot time */
75
#define DEF_A_SQUELDELAY 1      /* 1 mS squelch delay - allows fcs and flag */
76
#define DEF_A_CLOCKMODE 0       /* clock mode - 0 is normal */
77
 
78
#define DEF_B_SPEED 1200        /* 1200 baud */
79
#define DEF_B_TXDELAY 40        /* 400 mS */
80
#define DEF_B_PERSIST 128       /* 50% */
81
#define DEF_B_SLOTIME 30        /* 300 mS */
82
#define DEF_B_SQUELDELAY 3      /* 30 mS */
83
#define DEF_B_CLOCKMODE 0       /* Normal clock mode */
84
 
85
/* The following #define is only really required for the PI card, not
86
   the PI2 - but it's safer to leave it in. */
87
#define REALLY_SLOW_IO 1
88
 
89
#include <linux/config.h>
90
#include <linux/module.h>
91
#include <linux/kernel.h>
92
#include <linux/sched.h>
93
#include <linux/types.h>
94
#include <linux/fcntl.h>
95
#include <linux/interrupt.h>
96
#include <linux/ptrace.h>
97
#include <linux/ioport.h>
98
#include <linux/in.h>
99
#include <linux/malloc.h>
100
#include <linux/string.h>
101
#include <linux/errno.h>
102
#include <asm/system.h>
103
#include <asm/bitops.h>
104
#include <asm/io.h>
105
#include <asm/dma.h>
106
#include <asm/segment.h>
107
#include <linux/inet.h>
108
#include <linux/netdevice.h>
109
#include <linux/etherdevice.h>
110
#include <linux/skbuff.h>
111
#include <linux/timer.h>
112
#include <linux/if_arp.h>
113
#include <linux/pi2.h>
114
#include "z8530.h"
115
#include <net/ax25.h>
116
 
117
struct mbuf {
118
    struct mbuf *next;
119
    int cnt;
120
    char data[0];
121
};
122
 
123
/*
124
 *      The actual devices we will use
125
 */
126
 
127
/*
128
 *      PI device declarations.
129
 */
130
 
131
static int pi0_preprobe(struct device *dev){return 0;}   /* Dummy probe function */
132
static struct device pi0a = { "pi0a", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, pi0_preprobe };
133
static struct device pi0b = { "pi0b", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, pi0_preprobe };
134
 
135
 
136
/* The number of low I/O ports used by the card. */
137
#define PI_TOTAL_SIZE   8
138
 
139
 
140
/* Index to functions, as function prototypes. */
141
 
142
static int pi_probe(struct device *dev, int card_type);
143
static int pi_open(struct device *dev);
144
static int pi_send_packet(struct sk_buff *skb, struct device *dev);
145
static void pi_interrupt(int reg_ptr, void *dev_id, struct pt_regs *regs);
146
static int pi_close(struct device *dev);
147
static int pi_ioctl(struct device *dev, struct ifreq *ifr, int cmd);
148
static struct enet_statistics *pi_get_stats(struct device *dev);
149
static void rts(struct pi_local *lp, int x);
150
static void b_rxint(struct device *dev, struct pi_local *lp);
151
static void b_txint(struct pi_local *lp);
152
static void b_exint(struct pi_local *lp);
153
static void a_rxint(struct device *dev, struct pi_local *lp);
154
static void a_txint(struct pi_local *lp);
155
static void a_exint(struct pi_local *lp);
156
static char *get_dma_buffer(unsigned long *mem_ptr);
157
static int valid_dma_page(unsigned long addr, unsigned long dev_buffsize);
158
 
159
static char ax25_bcast[7] =
160
{'Q' << 1, 'S' << 1, 'T' << 1, ' ' << 1, ' ' << 1, ' ' << 1, '0' << 1};
161
static char ax25_test[7] =
162
{'L' << 1, 'I' << 1, 'N' << 1, 'U' << 1, 'X' << 1, ' ' << 1, '1' << 1};
163
 
164
static int ext2_secrm_seed = 152;       /* Random generator base */
165
 
166
static inline unsigned char random(void)
167
{
168
    return (unsigned char) (ext2_secrm_seed = ext2_secrm_seed
169
                            * 69069l + 1);
170
}
171
 
172
static inline void wrtscc(int cbase, int ctl, int sccreg, int val)
173
{
174
    /* assume caller disables interrupts! */
175
    outb_p(0, cbase + DMAEN);    /* Disable DMA while we touch the scc */
176
    outb_p(sccreg, ctl);        /* Select register */
177
    outb_p(val, ctl);           /* Output value */
178
    outb_p(1, cbase + DMAEN);   /* Enable DMA */
179
}
180
 
181
static inline int rdscc(int cbase, int ctl, int sccreg)
182
{
183
    int retval;
184
 
185
    /* assume caller disables interrupts! */
186
    outb_p(0, cbase + DMAEN);    /* Disable DMA while we touch the scc */
187
    outb_p(sccreg, ctl);        /* Select register */
188
    retval = inb_p(ctl);
189
    outb_p(1, cbase + DMAEN);   /* Enable DMA */
190
    return retval;
191
}
192
 
193
static void switchbuffers(struct pi_local *lp)
194
{
195
    if (lp->rcvbuf == lp->rxdmabuf1)
196
        lp->rcvbuf = lp->rxdmabuf2;
197
    else
198
        lp->rcvbuf = lp->rxdmabuf1;
199
}
200
 
201
static void hardware_send_packet(struct pi_local *lp, struct sk_buff *skb)
202
{
203
    char kickflag;
204
    unsigned long flags;
205
 
206
    lp->stats.tx_packets++;
207
 
208
    save_flags(flags);
209
    cli();
210
    kickflag = (skb_peek(&lp->sndq) == NULL) && (lp->sndbuf == NULL);
211
    restore_flags(flags);
212
 
213
    skb_queue_tail(&lp->sndq, skb);
214
    if (kickflag) {
215
        /* simulate interrupt to xmit */
216
        switch (lp->base & 2) {
217
        case 2:
218
            a_txint(lp);        /* process interrupt */
219
            break;
220
        case 0:
221
            save_flags(flags);
222
            cli();
223
            if (lp->tstate == IDLE)
224
                b_txint(lp);
225
            restore_flags(flags);
226
            break;
227
        }
228
    }
229
}
230
 
231
static void setup_rx_dma(struct pi_local *lp)
232
{
233
    unsigned long flags;
234
    int cmd;
235
    unsigned long dma_abs;
236
    unsigned dmachan;
237
 
238
    save_flags(flags);
239
    cli();
240
 
241
    dma_abs = (unsigned long) (lp->rcvbuf->data);
242
    dmachan = lp->dmachan;
243
    cmd = lp->base + CTL;
244
 
245
    if(!valid_dma_page(dma_abs, DMA_BUFF_SIZE + sizeof(struct mbuf)))
246
        panic("PI: RX buffer violates DMA boundary!");
247
 
248
    /* Get ready for RX DMA */
249
    wrtscc(lp->cardbase, cmd, R1, WT_FN_RDYFN | WT_RDY_RT | INT_ERR_Rx | EXT_INT_ENAB);
250
 
251
    disable_dma(dmachan);
252
    clear_dma_ff(dmachan);
253
 
254
    /* Set DMA mode register to single transfers, incrementing address,
255
     *  auto init, writes
256
     */
257
    set_dma_mode(dmachan, DMA_MODE_READ | 0x10);
258
    set_dma_addr(dmachan, dma_abs);
259
    set_dma_count(dmachan, lp->bufsiz);
260
    enable_dma(dmachan);
261
 
262
    /* If a packet is already coming in, this line is supposed to
263
           avoid receiving a partial packet.
264
     */
265
    wrtscc(lp->cardbase, cmd, R0, RES_Rx_CRC);
266
 
267
    /* Enable RX dma */
268
    wrtscc(lp->cardbase, cmd, R1,
269
      WT_RDY_ENAB | WT_FN_RDYFN | WT_RDY_RT | INT_ERR_Rx | EXT_INT_ENAB);
270
 
271
    restore_flags(flags);
272
}
273
 
274
static void setup_tx_dma(struct pi_local *lp, int length)
275
{
276
    unsigned long dma_abs;
277
    unsigned long flags;
278
    unsigned long dmachan;
279
 
280
    save_flags(flags);
281
    cli();
282
 
283
    dmachan = lp->dmachan;
284
    dma_abs = (unsigned long) (lp->txdmabuf);
285
 
286
    if(!valid_dma_page(dma_abs, DMA_BUFF_SIZE + sizeof(struct mbuf)))
287
        panic("PI: TX buffer violates DMA boundary!");
288
 
289
    disable_dma(dmachan);
290
    /* Set DMA mode register to single transfers, incrementing address,
291
     *  no auto init, reads
292
     */
293
    set_dma_mode(dmachan, DMA_MODE_WRITE);
294
    clear_dma_ff(dmachan);
295
    set_dma_addr(dmachan, dma_abs);
296
    /* output byte count */
297
    set_dma_count(dmachan, length);
298
 
299
    restore_flags(flags);
300
}
301
 
302
static void tdelay(struct pi_local *lp, int time)
303
{
304
    int port;
305
    unsigned int t1;
306
    unsigned char sc;
307
 
308
    if (lp->base & 2) {         /* If A channel */
309
        sc = SC1;
310
        t1 = time;
311
        port = lp->cardbase + TMR1;
312
    } else {
313
        sc = SC2;
314
        t1 = 10 * time;         /* 10s of milliseconds for the B channel */
315
        port = lp->cardbase + TMR2;
316
        wrtscc(lp->cardbase, lp->base + CTL, R1, INT_ALL_Rx | EXT_INT_ENAB);
317
    }
318
 
319
    /* Setup timer sc */
320
    outb_p(sc | LSB_MSB | MODE0, lp->cardbase + TMRCMD);
321
 
322
    /* times 2 to make millisecs */
323
    outb_p((t1 << 1) & 0xFF, port);
324
    outb_p((t1 >> 7) & 0xFF, port);
325
 
326
    /* Enable correct int for timeout */
327
    wrtscc(lp->cardbase, lp->base + CTL, R15, CTSIE);
328
    wrtscc(lp->cardbase, lp->base + CTL, R0, RES_EXT_INT);
329
}
330
 
331
static void free_p(struct sk_buff *skb)
332
{
333
        dev_kfree_skb(skb, FREE_WRITE);
334
}
335
 
336
static void a_txint(struct pi_local *lp)
337
{
338
    int cmd;
339
    unsigned long flags;
340
 
341
    save_flags(flags);
342
    cli();
343
 
344
    cmd = CTL + lp->base;
345
 
346
    switch (lp->tstate) {
347
    case IDLE:
348
        /* Transmitter idle. Find a frame for transmission */
349
        if ((lp->sndbuf = skb_dequeue(&lp->sndq)) == NULL) {
350
            rts(lp, OFF);
351
            restore_flags(flags);
352
            return;
353
        }
354
        /* If a buffer to send, we drop thru here */
355
    case DEFER:
356
        /* we may have deferred prev xmit attempt */
357
        /* Check DCD - debounce it
358
         * See Intel Microcommunications Handbook, p2-308
359
         */
360
        wrtscc(lp->cardbase, cmd, R0, RES_EXT_INT);
361
        wrtscc(lp->cardbase, cmd, R0, RES_EXT_INT);
362
        if ((rdscc(lp->cardbase, cmd, R0) & DCD) != 0) {
363
            lp->tstate = DEFER;
364
            tdelay(lp, 100);
365
            /* defer until DCD transition or timeout */
366
            wrtscc(lp->cardbase, cmd, R15, CTSIE | DCDIE);
367
            restore_flags(flags);
368
            return;
369
        }
370
        if (random() > lp->persist) {
371
            lp->tstate = DEFER;
372
            tdelay(lp, lp->slotime);
373
            restore_flags(flags);
374
            return;
375
        }
376
        /* Assert RTS early minimize collision window */
377
        wrtscc(lp->cardbase, cmd, R5, TxCRC_ENAB | RTS | Tx8);
378
        rts(lp, ON);            /* Transmitter on */
379
        lp->tstate = ST_TXDELAY;
380
        tdelay(lp, lp->txdelay);
381
        restore_flags(flags);
382
        return;
383
    default:
384
        break;
385
    }                           /* end switch(lp->state) */
386
 
387
    restore_flags(flags);
388
}                               /*a_txint */
389
 
390
static void a_exint(struct pi_local *lp)
391
{
392
    unsigned long flags;
393
    int cmd;
394
    char st;
395
    int length;
396
 
397
    save_flags(flags);
398
    cli();                      /* disable interrupts */
399
 
400
    st = rdscc(lp->cardbase, lp->base + CTL, R0);       /* Fetch status */
401
 
402
    /* reset external status latch */
403
    wrtscc(lp->cardbase, CTL + lp->base, R0, RES_EXT_INT);
404
    cmd = lp->base + CTL;
405
 
406
    if ((lp->rstate >= ACTIVE) && (st & BRK_ABRT)) {
407
        setup_rx_dma(lp);
408
        lp->rstate = ACTIVE;
409
    }
410
    switch (lp->tstate) {
411
    case ACTIVE:
412
        free_p(lp->sndbuf);
413
        lp->sndbuf = NULL;
414
        lp->tstate = FLAGOUT;
415
        tdelay(lp, lp->squeldelay);
416
        break;
417
    case FLAGOUT:
418
        if ((lp->sndbuf = skb_dequeue(&lp->sndq)) == NULL) {
419
            /* Nothing to send - return to receive mode */
420
            lp->tstate = IDLE;
421
            rts(lp, OFF);
422
            restore_flags(flags);
423
            return;
424
        }
425
        /* NOTE - fall through if more to send */
426
    case ST_TXDELAY:
427
        /* Disable DMA chan */
428
        disable_dma(lp->dmachan);
429
 
430
        /* Set up for TX dma */
431
        wrtscc(lp->cardbase, cmd, R1, WT_FN_RDYFN | EXT_INT_ENAB);
432
 
433
 
434
        /* Get all chars */
435
        /* Strip KISS control byte */
436
        length = lp->sndbuf->len - 1;
437
        memcpy(lp->txdmabuf, &lp->sndbuf->data[1], length);
438
 
439
 
440
        /* Setup DMA controller for tx */
441
        setup_tx_dma(lp, length);
442
 
443
        /* select transmit interrupts to enable */
444
        /* Allow DMA on chan */
445
        enable_dma(lp->dmachan);
446
 
447
        /* reset CRC, Txint pend*/
448
        wrtscc(lp->cardbase, cmd, R0, RES_Tx_CRC | RES_Tx_P);
449
 
450
        /* allow Underrun int only */
451
        wrtscc(lp->cardbase, cmd, R15, TxUIE);
452
 
453
        /* Enable TX DMA */
454
        wrtscc(lp->cardbase, cmd, R1, WT_RDY_ENAB | WT_FN_RDYFN | EXT_INT_ENAB);
455
 
456
        /* Send CRC on underrun */
457
        wrtscc(lp->cardbase, cmd, R0, RES_EOM_L);
458
 
459
 
460
        /* packet going out now */
461
        lp->tstate = ACTIVE;
462
        break;
463
    case DEFER:
464
        /* we have deferred prev xmit attempt
465
         * See Intel Microcommunications Handbook, p2-308
466
         */
467
        wrtscc(lp->cardbase, cmd, R0, RES_EXT_INT);
468
        wrtscc(lp->cardbase, cmd, R0, RES_EXT_INT);
469
        if ((rdscc(lp->cardbase, cmd, R0) & DCD) != 0) {
470
            lp->tstate = DEFER;
471
            tdelay(lp, 100);
472
            /* Defer until dcd transition or 100mS timeout */
473
            wrtscc(lp->cardbase, CTL + lp->base, R15, CTSIE | DCDIE);
474
            restore_flags(flags);
475
            return;
476
        }
477
        if (random() > lp->persist) {
478
            lp->tstate = DEFER;
479
            tdelay(lp, lp->slotime);
480
            restore_flags(flags);
481
            return;
482
        }
483
        /* Assert RTS early minimize collision window */
484
        wrtscc(lp->cardbase, cmd, R5, TxCRC_ENAB | RTS | Tx8);
485
        rts(lp, ON);            /* Transmitter on */
486
        lp->tstate = ST_TXDELAY;
487
        tdelay(lp, lp->txdelay);
488
        restore_flags(flags);
489
        return;
490
    }                           /* switch(lp->tstate) */
491
 
492
    restore_flags(flags);
493
}                               /* a_exint() */
494
 
495
/* Receive interrupt handler for the A channel
496
 */
497
static void a_rxint(struct device *dev, struct pi_local *lp)
498
{
499
    unsigned long flags;
500
    int cmd;
501
    int bytecount;
502
    char rse;
503
    struct sk_buff *skb;
504
    int sksize, pkt_len;
505
    struct mbuf *cur_buf;
506
    unsigned char *cfix;
507
 
508
    save_flags(flags);
509
    cli();                      /* disable interrupts */
510
    cmd = lp->base + CTL;
511
 
512
    rse = rdscc(lp->cardbase, cmd, R1); /* Get special condition bits from R1 */
513
    if (rse & Rx_OVR)
514
        lp->rstate = RXERROR;
515
 
516
    if (rse & END_FR) {
517
        /* If end of frame */
518
        /* figure length of frame from 8237 */
519
        clear_dma_ff(lp->dmachan);
520
        bytecount = lp->bufsiz - get_dma_residue(lp->dmachan);
521
 
522
        if ((rse & CRC_ERR) || (lp->rstate > ACTIVE) || (bytecount < 10)) {
523
            if ((bytecount >= 10) && (rse & CRC_ERR)) {
524
                lp->stats.rx_crc_errors++;
525
            }
526
            if (lp->rstate == RXERROR) {
527
                lp->stats.rx_errors++;
528
                lp->stats.rx_over_errors++;
529
            }
530
            /* Reset buffer pointers */
531
            lp->rstate = ACTIVE;
532
            setup_rx_dma(lp);
533
        } else {
534
            /* Here we have a valid frame */
535
            /* Toss 2 crc bytes , add one for KISS */
536
            pkt_len = lp->rcvbuf->cnt = bytecount - 2 + 1;
537
 
538
            /* Get buffer for next frame */
539
            cur_buf = lp->rcvbuf;
540
            switchbuffers(lp);
541
            setup_rx_dma(lp);
542
 
543
 
544
            /* Malloc up new buffer. */
545
            sksize = pkt_len;
546
 
547
            skb = dev_alloc_skb(sksize);
548
            if (skb == NULL) {
549
                printk(KERN_ERR "PI: %s: Memory squeeze, dropping packet.\n", dev->name);
550
                lp->stats.rx_dropped++;
551
                restore_flags(flags);
552
                return;
553
            }
554
            skb->dev = dev;
555
 
556
            /* KISS kludge -  prefix with a 0 byte */
557
            cfix=skb_put(skb,pkt_len);
558
            *cfix++=0;
559
            /* 'skb->data' points to the start of sk_buff data area. */
560
            memcpy(cfix, (char *) cur_buf->data,
561
                   pkt_len - 1);
562
            skb->protocol=htons(ETH_P_AX25);
563
            skb->mac.raw=skb->data;
564
            IS_SKB(skb);
565
            netif_rx(skb);
566
            lp->stats.rx_packets++;
567
        }                       /* end good frame */
568
    }                           /* end EOF check */
569
    wrtscc(lp->cardbase, lp->base + CTL, R0, ERR_RES);  /* error reset */
570
    restore_flags(flags);
571
}
572
 
573
static void b_rxint(struct device *dev, struct pi_local *lp)
574
{
575
    unsigned long flags;
576
    int cmd;
577
    char rse;
578
    struct sk_buff *skb;
579
    int sksize;
580
    int pkt_len;
581
    unsigned char *cfix;
582
 
583
    save_flags(flags);
584
    cli();                      /* disable interrupts */
585
    cmd = CTL + lp->base;
586
 
587
    rse = rdscc(lp->cardbase, cmd, R1); /* get status byte from R1 */
588
 
589
    if ((rdscc(lp->cardbase, cmd, R0)) & Rx_CH_AV) {
590
        /* there is a char to be stored
591
         * read special condition bits before reading the data char
592
         */
593
        if (rse & Rx_OVR) {
594
            /* Rx overrun - toss buffer */
595
            /* reset buffer pointers */
596
            lp->rcp = lp->rcvbuf->data;
597
            lp->rcvbuf->cnt = 0;
598
 
599
            lp->rstate = RXERROR;       /* set error flag */
600
            lp->stats.rx_errors++;
601
            lp->stats.rx_over_errors++;
602
        } else if (lp->rcvbuf->cnt >= lp->bufsiz) {
603
            /* Too large -- toss buffer */
604
            /* reset buffer pointers */
605
            lp->rcp = lp->rcvbuf->data;
606
            lp->rcvbuf->cnt = 0;
607
            lp->rstate = TOOBIG;/* when set, chars are not stored */
608
        }
609
        /* ok, we can store the received character now */
610
        if (lp->rstate == ACTIVE) {     /* If no errors... */
611
            *lp->rcp++ = rdscc(lp->cardbase, cmd, R8);  /* char to rcv buff */
612
            lp->rcvbuf->cnt++;  /* bump count */
613
        } else {
614
            /* got to empty FIFO */
615
            (void) rdscc(lp->cardbase, cmd, R8);
616
            wrtscc(lp->cardbase, cmd, R0, ERR_RES);     /* reset err latch */
617
            lp->rstate = ACTIVE;
618
        }
619
    }
620
    if (rse & END_FR) {
621
        /* END OF FRAME -- Make sure Rx was active */
622
        if (lp->rcvbuf->cnt > 0) {
623
            if ((rse & CRC_ERR) || (lp->rstate > ACTIVE) || (lp->rcvbuf->cnt < 10)) {
624
                if ((lp->rcvbuf->cnt >= 10) && (rse & CRC_ERR)) {
625
                    lp->stats.rx_crc_errors++;
626
                }
627
                lp->rcp = lp->rcvbuf->data;
628
                lp->rcvbuf->cnt = 0;
629
            } else {
630
                /* Here we have a valid frame */
631
                pkt_len = lp->rcvbuf->cnt -= 2; /* Toss 2 crc bytes */
632
                pkt_len += 1;   /* Make room for KISS control byte */
633
 
634
                /* Malloc up new buffer. */
635
                sksize = pkt_len;
636
                skb = dev_alloc_skb(sksize);
637
                if (skb == NULL) {
638
                    printk(KERN_ERR "PI: %s: Memory squeeze, dropping packet.\n", dev->name);
639
                    lp->stats.rx_dropped++;
640
                    restore_flags(flags);
641
                    return;
642
                }
643
                skb->dev = dev;
644
 
645
                /* KISS kludge -  prefix with a 0 byte */
646
                cfix=skb_put(skb,pkt_len);
647
                *cfix++=0;
648
                /* 'skb->data' points to the start of sk_buff data area. */
649
                memcpy(cfix, lp->rcvbuf->data, pkt_len - 1);
650
                skb->protocol=ntohs(ETH_P_AX25);
651
                skb->mac.raw=skb->data;
652
                IS_SKB(skb);
653
                netif_rx(skb);
654
                lp->stats.rx_packets++;
655
                /* packet queued - initialize buffer for next frame */
656
                lp->rcp = lp->rcvbuf->data;
657
                lp->rcvbuf->cnt = 0;
658
 
659
            }                   /* end good frame queued */
660
        }                       /* end check for active receive upon EOF */
661
        lp->rstate = ACTIVE;    /* and clear error status */
662
    }                           /* end EOF check */
663
    restore_flags(flags);
664
}
665
 
666
 
667
static void b_txint(struct pi_local *lp)
668
{
669
    unsigned long flags;
670
    int cmd;
671
    unsigned char c;
672
 
673
    save_flags(flags);
674
    cli();
675
    cmd = CTL + lp->base;
676
 
677
    switch (lp->tstate) {
678
    case CRCOUT:
679
        lp->tstate = FLAGOUT;
680
        tdelay(lp, lp->squeldelay);
681
        restore_flags(flags);
682
        return;
683
    case IDLE:
684
        /* Transmitter idle. Find a frame for transmission */
685
        if ((lp->sndbuf = skb_dequeue(&lp->sndq)) == NULL) {
686
            /* Nothing to send - return to receive mode
687
             * Tx OFF now - flag should have gone
688
             */
689
            rts(lp, OFF);
690
 
691
            restore_flags(flags);
692
            return;
693
        }
694
        lp->txptr = lp->sndbuf->data;
695
        lp->txptr++;            /* Ignore KISS control byte */
696
        lp->txcnt = (int) lp->sndbuf->len - 1;
697
        /* If a buffer to send, we drop thru here */
698
    case DEFER:         /* we may have deferred prev xmit attempt */
699
        /* Check DCD - debounce it */
700
        /* See Intel Microcommunications Handbook, p2-308 */
701
        wrtscc(lp->cardbase, cmd, R0, RES_EXT_INT);
702
        wrtscc(lp->cardbase, cmd, R0, RES_EXT_INT);
703
        if ((rdscc(lp->cardbase, cmd, R0) & DCD) != 0) {
704
            lp->tstate = DEFER;
705
            tdelay(lp, 100);
706
            /* defer until DCD transition or timeout */
707
            wrtscc(lp->cardbase, cmd, R15, CTSIE | DCDIE);
708
            restore_flags(flags);
709
            return;
710
        }
711
        if (random() > lp->persist) {
712
            lp->tstate = DEFER;
713
            tdelay(lp, lp->slotime);
714
            restore_flags(flags);
715
            return;
716
        }
717
        rts(lp, ON);            /* Transmitter on */
718
        lp->tstate = ST_TXDELAY;
719
        tdelay(lp, lp->txdelay);
720
        restore_flags(flags);
721
        return;
722
 
723
    case ACTIVE:
724
        /* Here we are actively sending a frame */
725
        if (lp->txcnt--) {
726
            c = *lp->txptr++;
727
            /* next char is gone */
728
            wrtscc(lp->cardbase, cmd, R8, c);
729
            /* stuffing a char satisfies Interrupt condition */
730
        } else {
731
            /* No more to send */
732
            free_p(lp->sndbuf);
733
            lp->sndbuf = NULL;
734
            if ((rdscc(lp->cardbase, cmd, R0) & 0x40)) {
735
                /* Did we underrun? */
736
                /* unexpected underrun */
737
                lp->stats.tx_errors++;
738
                lp->stats.tx_fifo_errors++;
739
                wrtscc(lp->cardbase, cmd, R0, SEND_ABORT);
740
                lp->tstate = FLAGOUT;
741
                tdelay(lp, lp->squeldelay);
742
                restore_flags(flags);
743
                return;
744
            }
745
            lp->tstate = UNDERRUN;      /* Now we expect to underrun */
746
            /* Send flags on underrun */
747
            if (lp->speed) {    /* If internally clocked */
748
                wrtscc(lp->cardbase, cmd, R10, CRCPS | NRZI);
749
            } else {
750
                wrtscc(lp->cardbase, cmd, R10, CRCPS);
751
            }
752
            wrtscc(lp->cardbase, cmd, R0, RES_Tx_P);    /* reset Tx Int Pend */
753
        }
754
        restore_flags(flags);
755
        return;                 /* back to wait for interrupt */
756
    }                           /* end switch */
757
    restore_flags(flags);
758
}
759
 
760
/* Pi SIO External/Status interrupts (for the B channel)
761
 * This can be caused by a receiver abort, or a Tx UNDERRUN/EOM.
762
 * Receiver automatically goes to Hunt on an abort.
763
 *
764
 * If the Tx Underrun interrupt hits, change state and
765
 * issue a reset command for it, and return.
766
 */
767
static void b_exint(struct pi_local *lp)
768
{
769
    unsigned long flags;
770
    char st;
771
    int cmd;
772
    char c;
773
 
774
    cmd = CTL + lp->base;
775
    save_flags(flags);
776
    cli();                      /* disable interrupts */
777
    st = rdscc(lp->cardbase, cmd, R0);  /* Fetch status */
778
    /* reset external status latch */
779
    wrtscc(lp->cardbase, cmd, R0, RES_EXT_INT);
780
 
781
 
782
    switch (lp->tstate) {
783
    case ACTIVE:                /* Unexpected underrun */
784
        free_p(lp->sndbuf);
785
        lp->sndbuf = NULL;
786
        wrtscc(lp->cardbase, cmd, R0, SEND_ABORT);
787
        lp->tstate = FLAGOUT;
788
        lp->stats.tx_errors++;
789
        lp->stats.tx_fifo_errors++;
790
        tdelay(lp, lp->squeldelay);
791
        restore_flags(flags);
792
        return;
793
    case UNDERRUN:
794
        lp->tstate = CRCOUT;
795
        restore_flags(flags);
796
        return;
797
    case FLAGOUT:
798
        /* Find a frame for transmission */
799
        if ((lp->sndbuf = skb_dequeue(&lp->sndq)) == NULL) {
800
            /* Nothing to send - return to receive mode
801
             * Tx OFF now - flag should have gone
802
             */
803
            rts(lp, OFF);
804
            lp->tstate = IDLE;
805
            restore_flags(flags);
806
            return;
807
        }
808
        lp->txptr = lp->sndbuf->data;
809
        lp->txptr++;            /* Ignore KISS control byte */
810
        lp->txcnt = (int) lp->sndbuf->len - 1;
811
        /* Get first char to send */
812
        lp->txcnt--;
813
        c = *lp->txptr++;
814
        wrtscc(lp->cardbase, cmd, R0, RES_Tx_CRC);      /* reset for next frame */
815
 
816
        /* Send abort on underrun */
817
        if (lp->speed) {        /* If internally clocked */
818
            wrtscc(lp->cardbase, cmd, R10, CRCPS | NRZI | ABUNDER);
819
        } else {
820
            wrtscc(lp->cardbase, cmd, R10, CRCPS | ABUNDER);
821
        }
822
 
823
        wrtscc(lp->cardbase, cmd, R8, c);       /* First char out now */
824
        wrtscc(lp->cardbase, cmd, R0, RES_EOM_L);       /* Reset end of message latch */
825
 
826
#ifdef STUFF2
827
        /* stuff an extra one if we can */
828
        if (lp->txcnt) {
829
            lp->txcnt--;
830
            c = *lp->txptr++;
831
            /* Wait for tx buffer empty */
832
            while((rdscc(lp->cardbase, cmd, R0) & 0x04) == 0)
833
                ;
834
            wrtscc(lp->cardbase, cmd, R8, c);
835
        }
836
#endif
837
 
838
        /* select transmit interrupts to enable */
839
 
840
        wrtscc(lp->cardbase, cmd, R15, TxUIE);  /* allow Underrun int only */
841
        wrtscc(lp->cardbase, cmd, R0, RES_EXT_INT);
842
        wrtscc(lp->cardbase, cmd, R1, TxINT_ENAB | EXT_INT_ENAB);       /* Tx/Ext ints */
843
 
844
        lp->tstate = ACTIVE;    /* char going out now */
845
        restore_flags(flags);
846
        return;
847
 
848
    case DEFER:
849
        /* Check DCD - debounce it
850
         * See Intel Microcommunications Handbook, p2-308
851
         */
852
        wrtscc(lp->cardbase, cmd, R0, RES_EXT_INT);
853
        wrtscc(lp->cardbase, cmd, R0, RES_EXT_INT);
854
        if ((rdscc(lp->cardbase, cmd, R0) & DCD) != 0) {
855
            lp->tstate = DEFER;
856
            tdelay(lp, 100);
857
            /* defer until DCD transition or timeout */
858
            wrtscc(lp->cardbase, cmd, R15, CTSIE | DCDIE);
859
            restore_flags(flags);
860
            return;
861
        }
862
        if (random() > lp->persist) {
863
            lp->tstate = DEFER;
864
            tdelay(lp, lp->slotime);
865
            restore_flags(flags);
866
            return;
867
        }
868
        rts(lp, ON);            /* Transmitter on */
869
        lp->tstate = ST_TXDELAY;
870
        tdelay(lp, lp->txdelay);
871
        restore_flags(flags);
872
        return;
873
 
874
    case ST_TXDELAY:
875
 
876
        /* Get first char to send */
877
        lp->txcnt--;
878
        c = *lp->txptr++;
879
        wrtscc(lp->cardbase, cmd, R0, RES_Tx_CRC);      /* reset for next frame */
880
 
881
        /* Send abort on underrun */
882
        if (lp->speed) {        /* If internally clocked */
883
            wrtscc(lp->cardbase, cmd, R10, CRCPS | NRZI | ABUNDER);
884
        } else {
885
            wrtscc(lp->cardbase, cmd, R10, CRCPS | ABUNDER);
886
        }
887
 
888
        wrtscc(lp->cardbase, cmd, R8, c);       /* First char out now */
889
        wrtscc(lp->cardbase, cmd, R0, RES_EOM_L);       /* Reset end of message latch */
890
 
891
#ifdef STUFF2
892
        /* stuff an extra one if we can */
893
        if (lp->txcnt) {
894
            lp->txcnt--;
895
            c = *lp->txptr++;
896
            /* Wait for tx buffer empty */
897
            while((rdscc(lp->cardbase, cmd, R0) & 0x04) == 0)
898
                ;
899
            wrtscc(lp->cardbase, cmd, R8, c);
900
        }
901
#endif
902
 
903
        /* select transmit interrupts to enable */
904
 
905
        wrtscc(lp->cardbase, cmd, R15, TxUIE);  /* allow Underrun int only */
906
        wrtscc(lp->cardbase, cmd, R0, RES_EXT_INT);
907
        /* Tx/Extern ints on */
908
        wrtscc(lp->cardbase, cmd, R1, TxINT_ENAB | EXT_INT_ENAB);
909
 
910
        lp->tstate = ACTIVE;    /* char going out now */
911
        restore_flags(flags);
912
        return;
913
    }
914
 
915
    /* Receive Mode only
916
     * This triggers when hunt mode is entered, & since an ABORT
917
     * automatically enters hunt mode, we use that to clean up
918
     * any waiting garbage
919
     */
920
    if ((lp->rstate == ACTIVE) && (st & BRK_ABRT)) {
921
        (void) rdscc(lp->cardbase, cmd, R8);
922
        (void) rdscc(lp->cardbase, cmd, R8);
923
        (void) rdscc(lp->cardbase, cmd, R8);
924
        lp->rcp = lp->rcvbuf->data;
925
        lp->rcvbuf->cnt = 0;     /* rewind on DCD transition */
926
    }
927
    restore_flags(flags);
928
}
929
 
930
/* Probe for a PI card. */
931
/* This routine also initializes the timer chip */
932
 
933
static int hw_probe(int ioaddr)
934
{
935
    int time = 1000;            /* Number of milliseconds for test */
936
    unsigned long start_time, end_time;
937
 
938
    int base, tmr0, tmr1, tmrcmd;
939
    int a = 1;
940
    int b = 1;
941
 
942
    base = ioaddr & 0x3f0;
943
    tmr0 = TMR0 + base;
944
    tmr1 = TMR1 + base;
945
    tmrcmd = TMRCMD + base;
946
 
947
    /* Set up counter chip timer 0 for 500 uS period square wave */
948
    /* assuming a 3.68 mhz clock for now */
949
    outb_p(SC0 | LSB_MSB | MODE3, tmrcmd);
950
    outb_p(922 & 0xFF, tmr0);
951
    outb_p(922 >> 8, tmr0);
952
 
953
    /* Setup timer control word for timer 1*/
954
    outb_p(SC1 | LSB_MSB | MODE0, tmrcmd);
955
    outb_p((time << 1) & 0xFF, tmr1);
956
    outb_p((time >> 7) & 0XFF, tmr1);
957
 
958
    /* wait until counter reg is loaded */
959
    do {
960
        /* Latch count for reading */
961
        outb_p(SC1, tmrcmd);
962
        a = inb_p(tmr1);
963
        b = inb_p(tmr1);
964
    } while (b == 0);
965
    start_time = jiffies;
966
    while (b != 0) {
967
        /* Latch count for reading */
968
        outb_p(SC1, tmrcmd);
969
        a = inb_p(tmr1);
970
        b = inb_p(tmr1);
971
        end_time = jiffies;
972
        /* Don't wait forever - there may be no card here */
973
        if ((end_time - start_time) > 200)
974
            return 0;            /* No card found */
975
    }
976
    end_time = jiffies;
977
    /* 87 jiffies, for a 3.68 mhz clock, half that for a double speed clock */
978
    if ((end_time - start_time) > 65) {
979
        return (1);             /* PI card found */
980
    } else {
981
        /* Faster crystal - tmr0 needs adjusting */
982
        /* Set up counter chip */
983
        /* 500 uS square wave */
984
        outb_p(SC0 | LSB_MSB | MODE3, tmrcmd);
985
        outb_p(1844 & 0xFF, tmr0);
986
        outb_p(1844 >> 8, tmr0);
987
        return (2);             /* PI2 card found */
988
    }
989
}
990
 
991
static void rts(struct pi_local *lp, int x)
992
{
993
    int tc;
994
    long br;
995
    int cmd;
996
    int dummy;
997
 
998
    /* assumes interrupts are off */
999
    cmd = CTL + lp->base;
1000
 
1001
    /* Reprogram BRG and turn on transmitter to send flags */
1002
    if (x == ON) {              /* Turn Tx ON and Receive OFF */
1003
        /* Exints off first to avoid abort int */
1004
        wrtscc(lp->cardbase, cmd, R15, 0);
1005
        wrtscc(lp->cardbase, cmd, R3, Rx8);     /* Rx off */
1006
        lp->rstate = IDLE;
1007
        if (cmd & 2) {          /* if channel a */
1008
            /* Set up for TX dma */
1009
            wrtscc(lp->cardbase, cmd, R1, WT_FN_RDYFN | EXT_INT_ENAB);
1010
        } else {
1011
            wrtscc(lp->cardbase, cmd, R1, 0);    /* No interrupts */
1012
        }
1013
 
1014
        if (!lp->clockmode) {
1015
            if (lp->speed) {    /* if internally clocked */
1016
                br = lp->speed; /* get desired speed */
1017
                tc = (lp->xtal / br) - 2;       /* calc 1X BRG divisor */
1018
                wrtscc(lp->cardbase, cmd, R12, tc & 0xFF);      /* lower byte */
1019
                wrtscc(lp->cardbase, cmd, R13, (tc >> 8) & 0xFF);       /* upper byte */
1020
            }
1021
        }
1022
        wrtscc(lp->cardbase, cmd, R5, TxCRC_ENAB | RTS | TxENAB | Tx8 | DTR);
1023
        /* Transmitter now on */
1024
    } else {                    /* Tx OFF and Rx ON */
1025
        lp->tstate = IDLE;
1026
        wrtscc(lp->cardbase, cmd, R5, Tx8 | DTR);       /*  TX off */
1027
 
1028
        if (!lp->clockmode) {
1029
            if (lp->speed) {    /* if internally clocked */
1030
                /* Reprogram BRG for 32x clock for receive DPLL */
1031
                /* BRG off, keep Pclk source */
1032
                wrtscc(lp->cardbase, cmd, R14, BRSRC);
1033
                br = lp->speed; /* get desired speed */
1034
                /* calc 32X BRG divisor */
1035
                tc = ((lp->xtal / 32) / br) - 2;
1036
                wrtscc(lp->cardbase, cmd, R12, tc & 0xFF);      /* lower byte */
1037
                wrtscc(lp->cardbase, cmd, R13, (tc >> 8) & 0xFF);       /* upper byte */
1038
                /* SEARCH mode, BRG source */
1039
                wrtscc(lp->cardbase, cmd, R14, BRSRC | SEARCH);
1040
                /* Enable the BRG */
1041
                wrtscc(lp->cardbase, cmd, R14, BRSRC | BRENABL);
1042
            }
1043
        }
1044
        /* Flush rx fifo */
1045
        wrtscc(lp->cardbase, cmd, R3, Rx8);     /* Make sure rx is off */
1046
        wrtscc(lp->cardbase, cmd, R0, ERR_RES); /* reset err latch */
1047
        dummy = rdscc(lp->cardbase, cmd, R1);   /* get status byte from R1 */
1048
        (void) rdscc(lp->cardbase, cmd, R8);
1049
        (void) rdscc(lp->cardbase, cmd, R8);
1050
 
1051
        (void) rdscc(lp->cardbase, cmd, R8);
1052
 
1053
        /* Now, turn on the receiver and hunt for a flag */
1054
        wrtscc(lp->cardbase, cmd, R3, RxENABLE | Rx8);
1055
        lp->rstate = ACTIVE;    /* Normal state */
1056
 
1057
        if (cmd & 2) {          /* if channel a */
1058
            setup_rx_dma(lp);
1059
        } else {
1060
            /* reset buffer pointers */
1061
            lp->rcp = lp->rcvbuf->data;
1062
            lp->rcvbuf->cnt = 0;
1063
            wrtscc(lp->cardbase, cmd, R1, (INT_ALL_Rx | EXT_INT_ENAB));
1064
        }
1065
        wrtscc(lp->cardbase, cmd, R15, BRKIE);  /* allow ABORT int */
1066
    }
1067
}
1068
 
1069
/* Fill in the MAC-level header. */
1070
static int pi_header(struct sk_buff *skb, struct device *dev, unsigned short type,
1071
             void *daddr, void *saddr, unsigned len)
1072
{
1073
    return ax25_encapsulate(skb, dev, type, daddr, saddr, len);
1074
}
1075
 
1076
/* Rebuild the MAC-level header. */
1077
static int pi_rebuild_header(void *buff, struct device *dev, unsigned long raddr,
1078
                             struct sk_buff *skb)
1079
{
1080
    return ax25_rebuild_header(buff, dev, raddr, skb);
1081
}
1082
 
1083
static void scc_init(struct device *dev)
1084
{
1085
    unsigned long flags;
1086
    struct pi_local *lp = (struct pi_local *) dev->priv;
1087
 
1088
    int tc;
1089
    long br;
1090
    register int cmd;
1091
 
1092
    /* Initialize 8530 channel for SDLC operation */
1093
 
1094
    cmd = CTL + lp->base;
1095
    save_flags(flags);
1096
    cli();
1097
 
1098
    switch (cmd & CHANA) {
1099
    case CHANA:
1100
        wrtscc(lp->cardbase, cmd, R9, CHRA);    /* Reset channel A */
1101
        wrtscc(lp->cardbase, cmd, R2, 0xff);    /* Initialize interrupt vector */
1102
        break;
1103
    default:
1104
        wrtscc(lp->cardbase, cmd, R9, CHRB);    /* Reset channel B */
1105
        break;
1106
    }
1107
 
1108
    /* Deselect all Rx and Tx interrupts */
1109
    wrtscc(lp->cardbase, cmd, R1, 0);
1110
 
1111
    /* Turn off external interrupts (like CTS/CD) */
1112
    wrtscc(lp->cardbase, cmd, R15, 0);
1113
 
1114
    /* X1 clock, SDLC mode */
1115
    wrtscc(lp->cardbase, cmd, R4, SDLC | X1CLK);
1116
 
1117
    /* Tx/Rx parameters */
1118
    if (lp->speed) {            /* Use internal clocking */
1119
        wrtscc(lp->cardbase, cmd, R10, CRCPS | NRZI);
1120
        if (!lp->clockmode)
1121
            /* Tx Clk from BRG. Rcv Clk from DPLL, TRxC pin outputs DPLL */
1122
            wrtscc(lp->cardbase, cmd, R11, TCBR | RCDPLL | TRxCDP | TRxCOI);
1123
        else
1124
            /* Tx Clk from DPLL, Rcv Clk from DPLL, TRxC Outputs BRG */
1125
            wrtscc(lp->cardbase, cmd, R11, TCDPLL | RCDPLL | TRxCBR | TRxCOI);
1126
    } else {                    /* Use external clocking */
1127
        wrtscc(lp->cardbase, cmd, R10, CRCPS);
1128
        /* Tx Clk from Trxcl. Rcv Clk from Rtxcl, TRxC pin is input */
1129
        wrtscc(lp->cardbase, cmd, R11, TCTRxCP);
1130
    }
1131
 
1132
    /* Null out SDLC start address */
1133
    wrtscc(lp->cardbase, cmd, R6, 0);
1134
 
1135
    /* SDLC flag */
1136
    wrtscc(lp->cardbase, cmd, R7, FLAG);
1137
 
1138
    /* Set up the Transmitter but don't enable it
1139
     *  DTR, 8 bit TX chars only
1140
     */
1141
    wrtscc(lp->cardbase, cmd, R5, Tx8 | DTR);
1142
 
1143
    /* Receiver initial setup */
1144
    wrtscc(lp->cardbase, cmd, R3, Rx8); /* 8 bits/char */
1145
 
1146
    /* Setting up BRG now - turn it off first */
1147
    wrtscc(lp->cardbase, cmd, R14, BRSRC);      /* BRG off, keep Pclk source */
1148
 
1149
    /* set the 32x time constant for the BRG in Receive mode */
1150
 
1151
    if (lp->speed) {
1152
        br = lp->speed;         /* get desired speed */
1153
        tc = ((lp->xtal / 32) / br) - 2;        /* calc 32X BRG divisor */
1154
    } else {
1155
        tc = 14;
1156
    }
1157
 
1158
    wrtscc(lp->cardbase, cmd, R12, tc & 0xFF);  /* lower byte */
1159
    wrtscc(lp->cardbase, cmd, R13, (tc >> 8) & 0xFF);   /* upper byte */
1160
 
1161
    /* Following subroutine sets up and ENABLES the receiver */
1162
    rts(lp, OFF);               /* TX OFF and RX ON */
1163
 
1164
    if (lp->speed) {
1165
        /* DPLL frm BRG, BRG src PCLK */
1166
        wrtscc(lp->cardbase, cmd, R14, BRSRC | SSBR);
1167
    } else {
1168
        /* DPLL frm rtxc,BRG src PCLK */
1169
        wrtscc(lp->cardbase, cmd, R14, BRSRC | SSRTxC);
1170
    }
1171
    wrtscc(lp->cardbase, cmd, R14, BRSRC | SEARCH);     /* SEARCH mode, keep BRG src */
1172
    wrtscc(lp->cardbase, cmd, R14, BRSRC | BRENABL);    /* Enable the BRG */
1173
 
1174
    if (!(cmd & 2))             /* if channel b */
1175
        wrtscc(lp->cardbase, cmd, R1, (INT_ALL_Rx | EXT_INT_ENAB));
1176
 
1177
    wrtscc(lp->cardbase, cmd, R15, BRKIE);      /* ABORT int */
1178
 
1179
    /* Now, turn on the receiver and hunt for a flag */
1180
    wrtscc(lp->cardbase, cmd, R3, RxENABLE | RxCRC_ENAB | Rx8);
1181
 
1182
    restore_flags(flags);
1183
}
1184
 
1185
static void chipset_init(struct device *dev)
1186
{
1187
    int cardbase;
1188
    unsigned long flags;
1189
 
1190
    cardbase = dev->base_addr & 0x3f0;
1191
 
1192
    save_flags(flags);
1193
    cli();
1194
    wrtscc(cardbase, dev->base_addr + CTL, R9, FHWRES); /* Hardware reset */
1195
    /* Disable interrupts with master interrupt ctrl reg */
1196
    wrtscc(cardbase, dev->base_addr + CTL, R9, 0);
1197
    restore_flags(flags);
1198
 
1199
}
1200
 
1201
 
1202
int pi_init(void)
1203
{
1204
    int *port;
1205
    int ioaddr = 0;
1206
    int card_type = 0;
1207
    int ports[] =
1208
    {0x380, 0x300, 0x320, 0x340, 0x360, 0x3a0, 0};
1209
 
1210
    printk(KERN_INFO "PI: V0.8 ALPHA April 23 1995 David Perry (dp@hydra.carleton.ca)\n");
1211
 
1212
    /* Only one card supported for now */
1213
    for (port = &ports[0]; *port && !card_type; port++) {
1214
        ioaddr = *port;
1215
 
1216
        if (check_region(ioaddr, PI_TOTAL_SIZE) == 0) {
1217
            printk(KERN_INFO "PI: Probing for card at address %#3x\n",ioaddr);
1218
            card_type = hw_probe(ioaddr);
1219
        }
1220
    }
1221
 
1222
    switch (card_type) {
1223
    case 1:
1224
        printk(KERN_INFO "PI: Found a PI card at address %#3x\n", ioaddr);
1225
        break;
1226
    case 2:
1227
        printk(KERN_INFO "PI: Found a PI2 card at address %#3x\n", ioaddr);
1228
        break;
1229
    default:
1230
        printk(KERN_ERR "PI: ERROR: No card found\n");
1231
        return -EIO;
1232
    }
1233
 
1234
    /* Link a couple of device structures into the chain */
1235
    /* For the A port */
1236
    /* Allocate space for 4 buffers even though we only need 3,
1237
       because one of them may cross a DMA page boundary and
1238
       be rejected by get_dma_buffer().
1239
    */
1240
    register_netdev(&pi0a);
1241
 
1242
    pi0a.priv = kmalloc(sizeof(struct pi_local) + (DMA_BUFF_SIZE + sizeof(struct mbuf)) * 4, GFP_KERNEL | GFP_DMA);
1243
 
1244
    pi0a.dma = PI_DMA;
1245
    pi0a.base_addr = ioaddr + 2;
1246
    pi0a.irq = 0;
1247
 
1248
    /* And the B port */
1249
    register_netdev(&pi0b);
1250
    pi0b.base_addr = ioaddr;
1251
    pi0b.irq = 0;
1252
 
1253
    pi0b.priv = kmalloc(sizeof(struct pi_local) + (DMA_BUFF_SIZE + sizeof(struct mbuf)) * 4, GFP_KERNEL | GFP_DMA);
1254
 
1255
    /* Now initialize them */
1256
    pi_probe(&pi0a, card_type);
1257
    pi_probe(&pi0b, card_type);
1258
 
1259
    pi0b.irq = pi0a.irq;        /* IRQ is shared */
1260
 
1261
    return 0;
1262
}
1263
 
1264
static int valid_dma_page(unsigned long addr, unsigned long dev_buffsize)
1265
{
1266
    if (((addr & 0xffff) + dev_buffsize) <= 0x10000)
1267
        return 1;
1268
    else
1269
        return 0;
1270
}
1271
 
1272
static int pi_set_mac_address(struct device *dev, void *addr)
1273
{
1274
    struct sockaddr *sa = (struct sockaddr *)addr;
1275
    memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);  /* addr is an AX.25 shifted ASCII */
1276
    return 0;                                            /* mac address */
1277
}
1278
 
1279
/* Allocate a buffer which does not cross a DMA page boundary */
1280
static char *
1281
get_dma_buffer(unsigned long *mem_ptr)
1282
{
1283
    char *ret;
1284
 
1285
    ret = (char *)*mem_ptr;
1286
 
1287
    if(!valid_dma_page(*mem_ptr, DMA_BUFF_SIZE + sizeof(struct mbuf))){
1288
        *mem_ptr += (DMA_BUFF_SIZE + sizeof(struct mbuf));
1289
        ret = (char *)*mem_ptr;
1290
    }
1291
    *mem_ptr += (DMA_BUFF_SIZE + sizeof(struct mbuf));
1292
    return (ret);
1293
}
1294
 
1295
static int pi_probe(struct device *dev, int card_type)
1296
{
1297
    short ioaddr;
1298
    struct pi_local *lp;
1299
    int i;
1300
    unsigned long flags;
1301
    unsigned long mem_ptr;
1302
 
1303
    ioaddr = dev->base_addr;
1304
 
1305
    /* Initialize the device structure. */
1306
    /* Must be done before chipset_init */
1307
    /* Make certain the data structures used by the PI2 are aligned. */
1308
    dev->priv = (void *) (((int) dev->priv + 7) & ~7);
1309
    lp = (struct pi_local *) dev->priv;
1310
 
1311
    memset(dev->priv, 0, sizeof(struct pi_local));
1312
 
1313
    /* Allocate some buffers which do not cross DMA page boundaries */
1314
    mem_ptr = (unsigned long) dev->priv + sizeof(struct pi_local);
1315
    lp->txdmabuf = get_dma_buffer(&mem_ptr);
1316
    lp->rxdmabuf1 = (struct mbuf *) get_dma_buffer(&mem_ptr);
1317
    lp->rxdmabuf2 = (struct mbuf *) get_dma_buffer(&mem_ptr);
1318
 
1319
    /* Initialize rx buffer */
1320
    lp->rcvbuf = lp->rxdmabuf1;
1321
    lp->rcp = lp->rcvbuf->data;
1322
    lp->rcvbuf->cnt = 0;
1323
 
1324
    /* Initialize the transmit queue head structure */
1325
    skb_queue_head_init(&lp->sndq);
1326
 
1327
    /* These need to be initialized before scc_init is called. */
1328
    if (card_type == 1)
1329
        lp->xtal = (unsigned long) SINGLE / 2;
1330
    else
1331
        lp->xtal = (unsigned long) DOUBLE / 2;
1332
    lp->base = dev->base_addr;
1333
    lp->cardbase = dev->base_addr & 0x3f0;
1334
    if (dev->base_addr & CHANA) {
1335
        lp->speed = DEF_A_SPEED;
1336
        /* default channel access Params */
1337
        lp->txdelay = DEF_A_TXDELAY;
1338
        lp->persist = DEF_A_PERSIST;
1339
        lp->slotime = DEF_A_SLOTIME;
1340
        lp->squeldelay = DEF_A_SQUELDELAY;
1341
        lp->clockmode = DEF_A_CLOCKMODE;
1342
 
1343
    } else {
1344
        lp->speed = DEF_B_SPEED;
1345
        /* default channel access Params */
1346
        lp->txdelay = DEF_B_TXDELAY;
1347
        lp->persist = DEF_B_PERSIST;
1348
        lp->slotime = DEF_B_SLOTIME;
1349
        lp->squeldelay = DEF_B_SQUELDELAY;
1350
        lp->clockmode = DEF_B_CLOCKMODE;
1351
    }
1352
    lp->bufsiz = DMA_BUFF_SIZE;
1353
    lp->tstate = IDLE;
1354
 
1355
    chipset_init(dev);
1356
 
1357
    if (dev->base_addr & CHANA) {       /* Do these things only for the A port */
1358
        /* Note that a single IRQ services 2 devices (A and B channels) */
1359
 
1360
        lp->dmachan = dev->dma;
1361
        if (lp->dmachan < 1 || lp->dmachan > 3)
1362
            printk(KERN_ERR "PI: DMA channel %d out of range\n", lp->dmachan);
1363
 
1364
        /* chipset_init() was already called */
1365
 
1366
        if (dev->irq < 2) {
1367
            autoirq_setup(0);
1368
            save_flags(flags);
1369
            cli();
1370
            wrtscc(lp->cardbase, CTL + lp->base, R1, EXT_INT_ENAB);
1371
            /* enable PI card interrupts */
1372
            wrtscc(lp->cardbase, CTL + lp->base, R9, MIE | NV);
1373
            restore_flags(flags);
1374
            /* request a timer interrupt for 1 mS hence */
1375
            tdelay(lp, 1);
1376
            /* 20 "jiffies" should be plenty of time... */
1377
            dev->irq = autoirq_report(20);
1378
            if (!dev->irq) {
1379
                printk(KERN_ERR "PI: Failed to detect IRQ line.\n");
1380
            }
1381
            save_flags(flags);
1382
            cli();
1383
            wrtscc(lp->cardbase, dev->base_addr + CTL, R9, FHWRES);     /* Hardware reset */
1384
            /* Disable interrupts with master interrupt ctrl reg */
1385
            wrtscc(lp->cardbase, dev->base_addr + CTL, R9, 0);
1386
            restore_flags(flags);
1387
        }
1388
 
1389
        printk(KERN_INFO "PI: Autodetected IRQ %d, assuming DMA %d.\n",
1390
               dev->irq, dev->dma);
1391
 
1392
        /* This board has jumpered interrupts. Snarf the interrupt vector
1393
                   now.  There is no point in waiting since no other device can use
1394
                   the interrupt, and this marks the 'irqaction' as busy. */
1395
        {
1396
            int irqval = request_irq(dev->irq, &pi_interrupt,0, "pi2", NULL);
1397
            if (irqval) {
1398
                printk(KERN_ERR "PI: unable to get IRQ %d (irqval=%d).\n",
1399
                       dev->irq, irqval);
1400
                return EAGAIN;
1401
            }
1402
        }
1403
 
1404
        /* Grab the region */
1405
        request_region(ioaddr & 0x3f0, PI_TOTAL_SIZE, "pi2" );
1406
 
1407
 
1408
    }                           /* Only for A port */
1409
    dev->open = pi_open;
1410
    dev->stop = pi_close;
1411
    dev->do_ioctl = pi_ioctl;
1412
    dev->hard_start_xmit = pi_send_packet;
1413
    dev->get_stats = pi_get_stats;
1414
 
1415
    /* Fill in the fields of the device structure */
1416
    for (i = 0; i < DEV_NUMBUFFS; i++)
1417
        skb_queue_head_init(&dev->buffs[i]);
1418
 
1419
 
1420
    dev->hard_header = pi_header;
1421
    dev->rebuild_header = pi_rebuild_header;
1422
    dev->set_mac_address = pi_set_mac_address;
1423
 
1424
    dev->type = ARPHRD_AX25;                    /* AF_AX25 device */
1425
    dev->hard_header_len = 73;                  /* We do digipeaters now */
1426
    dev->mtu = 1500;                            /* eth_mtu is the default */
1427
    dev->addr_len = 7;                          /* sizeof an ax.25 address */
1428
    memcpy(dev->broadcast, ax25_bcast, 7);
1429
    memcpy(dev->dev_addr, ax25_test, 7);
1430
 
1431
    /* New-style flags. */
1432
    dev->flags = 0;
1433
    dev->family = AF_INET;
1434
 
1435
#ifdef CONFIG_INET
1436
    dev->pa_addr    = in_aton("192.168.0.1");
1437
    dev->pa_brdaddr = in_aton("192.168.0.255");
1438
    dev->pa_mask    = in_aton("255.255.255.0");
1439
    dev->pa_alen    = 4;
1440
#endif
1441
 
1442
    return 0;
1443
}
1444
 
1445
/* Open/initialize the board.  This is called (in the current kernel)
1446
   sometime after booting when the 'ifconfig' program is run.
1447
 
1448
   This routine should set everything up anew at each open, even
1449
   registers that "should" only need to be set once at boot, so that
1450
   there is non-reboot way to recover if something goes wrong.
1451
   */
1452
static int pi_open(struct device *dev)
1453
{
1454
    unsigned long flags;
1455
    static int first_time = 1;
1456
 
1457
    struct pi_local *lp = (struct pi_local *) dev->priv;
1458
 
1459
    if (dev->base_addr & 2) {   /* if A channel */
1460
        if (first_time) {
1461
            if (request_dma(dev->dma,"pi2")) {
1462
                free_irq(dev->irq, NULL);
1463
                return -EAGAIN;
1464
            }
1465
            irq2dev_map[dev->irq] = dev;
1466
        }
1467
        /* Reset the hardware here. */
1468
        chipset_init(dev);
1469
    }
1470
    lp->tstate = IDLE;
1471
 
1472
    if (dev->base_addr & 2) {   /* if A channel */
1473
        scc_init(dev);          /* Called once for each channel */
1474
        scc_init(dev->next);
1475
    }
1476
    /* master interrupt enable */
1477
    save_flags(flags);
1478
    cli();
1479
    wrtscc(lp->cardbase, CTL + lp->base, R9, MIE | NV);
1480
    restore_flags(flags);
1481
 
1482
    lp->open_time = jiffies;
1483
 
1484
    dev->tbusy = 0;
1485
    dev->interrupt = 0;
1486
    dev->start = 1;
1487
    first_time = 0;
1488
 
1489
    MOD_INC_USE_COUNT;
1490
 
1491
    return 0;
1492
}
1493
 
1494
static int pi_send_packet(struct sk_buff *skb, struct device *dev)
1495
{
1496
    struct pi_local *lp = (struct pi_local *) dev->priv;
1497
 
1498
    /* If some higher layer thinks we've missed an tx-done interrupt
1499
           we are passed NULL. Caution: dev_tint() handles the cli()/sti()
1500
           itself. */
1501
    if (skb == NULL) {
1502
        dev_tint(dev);
1503
        return 0;
1504
    }
1505
    hardware_send_packet(lp, skb);
1506
    dev->trans_start = jiffies;
1507
 
1508
    return 0;
1509
}
1510
 
1511
/* The typical workload of the driver:
1512
   Handle the network interface interrupts. */
1513
static void pi_interrupt(int reg_ptr, void *dev_id, struct pt_regs *regs)
1514
{
1515
/*    int irq = -(((struct pt_regs *) reg_ptr)->orig_eax + 2);*/
1516
    struct pi_local *lp;
1517
    int st;
1518
    unsigned long flags;
1519
 
1520
/*    dev_b = dev_a->next;       Relies on the order defined in Space.c */
1521
 
1522
#if 0
1523
    if (dev_a == NULL) {
1524
        printk(KERN_ERR "PI: pi_interrupt(): irq %d for unknown device.\n", irq);
1525
        return;
1526
    }
1527
#endif    
1528
    /* Read interrupt status register (only valid from channel A)
1529
     * Process all pending interrupts in while loop
1530
     */
1531
    lp = (struct pi_local *) pi0a.priv; /* Assume channel A */
1532
    while ((st = rdscc(lp->cardbase, pi0a.base_addr | CHANA | CTL, R3)) != 0) {
1533
        if (st & CHBTxIP) {
1534
            /* Channel B Transmit Int Pending */
1535
            lp = (struct pi_local *) pi0b.priv;
1536
            b_txint(lp);
1537
        } else if (st & CHARxIP) {
1538
            /* Channel A Rcv Interrupt Pending */
1539
            lp = (struct pi_local *) pi0a.priv;
1540
            a_rxint(&pi0a, lp);
1541
        } else if (st & CHATxIP) {
1542
            /* Channel A Transmit Int Pending */
1543
            lp = (struct pi_local *) pi0a.priv;
1544
            a_txint(lp);
1545
        } else if (st & CHAEXT) {
1546
            /* Channel A External Status Int */
1547
            lp = (struct pi_local *) pi0a.priv;
1548
            a_exint(lp);
1549
        } else if (st & CHBRxIP) {
1550
            /* Channel B Rcv Interrupt Pending */
1551
            lp = (struct pi_local *) pi0b.priv;
1552
            b_rxint(&pi0b, lp);
1553
        } else if (st & CHBEXT) {
1554
            /* Channel B External Status Int */
1555
            lp = (struct pi_local *) pi0b.priv;
1556
            b_exint(lp);
1557
        }
1558
        /* Reset highest interrupt under service */
1559
        save_flags(flags);
1560
        cli();
1561
        wrtscc(lp->cardbase, lp->base + CTL, R0, RES_H_IUS);
1562
        restore_flags(flags);
1563
    }                           /* End of while loop on int processing */
1564
    return;
1565
}
1566
 
1567
/* The inverse routine to pi_open(). */
1568
static int pi_close(struct device *dev)
1569
{
1570
    unsigned long flags;
1571
    struct pi_local *lp;
1572
    struct sk_buff *ptr;
1573
 
1574
    save_flags(flags);
1575
    cli();
1576
 
1577
    lp = (struct pi_local *) dev->priv;
1578
    ptr = NULL;
1579
 
1580
    chipset_init(dev);          /* reset the scc */
1581
    disable_dma(lp->dmachan);
1582
 
1583
    lp->open_time = 0;
1584
 
1585
    dev->tbusy = 1;
1586
    dev->start = 0;
1587
 
1588
    /* Free any buffers left in the hardware transmit queue */
1589
    while ((ptr = skb_dequeue(&lp->sndq)) != NULL)
1590
        free_p(ptr);
1591
 
1592
    restore_flags(flags);
1593
 
1594
    MOD_DEC_USE_COUNT;
1595
 
1596
    return 0;
1597
}
1598
 
1599
static int pi_ioctl(struct device *dev, struct ifreq *ifr, int cmd)
1600
{
1601
    unsigned long flags;
1602
    struct pi_req rq;
1603
    struct pi_local *lp = (struct pi_local *) dev->priv;
1604
 
1605
    int ret = verify_area(VERIFY_WRITE, ifr->ifr_data, sizeof(struct pi_req));
1606
    if (ret)
1607
        return ret;
1608
 
1609
    if(cmd!=SIOCDEVPRIVATE)
1610
        return -EINVAL;
1611
 
1612
    memcpy_fromfs(&rq, ifr->ifr_data, sizeof(struct pi_req));
1613
 
1614
    switch (rq.cmd) {
1615
    case SIOCSPIPARAM:
1616
 
1617
        if (!suser())
1618
            return -EPERM;
1619
        save_flags(flags);
1620
        cli();
1621
        lp->txdelay = rq.txdelay;
1622
        lp->persist = rq.persist;
1623
        lp->slotime = rq.slotime;
1624
        lp->squeldelay = rq.squeldelay;
1625
        lp->clockmode = rq.clockmode;
1626
        lp->speed = rq.speed;
1627
        pi_open(&pi0a); /* both channels get reset %%% */
1628
        restore_flags(flags);
1629
        ret = 0;
1630
        break;
1631
 
1632
    case SIOCSPIDMA:
1633
 
1634
        if (!suser())
1635
            return -EPERM;
1636
        ret = 0;
1637
        if (dev->base_addr & 2) {   /* if A channel */
1638
           if (rq.dmachan < 1 || rq.dmachan > 3)
1639
                return -EINVAL;
1640
           save_flags(flags);
1641
           cli();
1642
           pi_close(dev);
1643
           free_dma(lp->dmachan);
1644
           dev->dma = lp->dmachan = rq.dmachan;
1645
           if (request_dma(lp->dmachan,"pi2"))
1646
                ret = -EAGAIN;
1647
           pi_open(dev);
1648
           restore_flags(flags);
1649
        }
1650
        break;
1651
 
1652
    case SIOCSPIIRQ:
1653
        ret = -EINVAL;      /* add this later */
1654
        break;
1655
 
1656
    case SIOCGPIPARAM:
1657
    case SIOCGPIDMA:
1658
    case SIOCGPIIRQ:
1659
 
1660
        rq.speed = lp->speed;
1661
        rq.txdelay = lp->txdelay;
1662
        rq.persist = lp->persist;
1663
        rq.slotime = lp->slotime;
1664
        rq.squeldelay = lp->squeldelay;
1665
        rq.clockmode = lp->clockmode;
1666
        rq.dmachan = lp->dmachan;
1667
        rq.irq = dev->irq;
1668
        memcpy_tofs(ifr->ifr_data, &rq, sizeof(struct pi_req));
1669
        ret = 0;
1670
        break;
1671
 
1672
    default:
1673
        ret = -EINVAL;
1674
    }
1675
    return ret;
1676
}
1677
 
1678
/* Get the current statistics.  This may be called with the card open or
1679
   closed. */
1680
static struct netstats *
1681
 pi_get_stats(struct device *dev)
1682
{
1683
    struct pi_local *lp = (struct pi_local *) dev->priv;
1684
 
1685
    return &lp->stats;
1686
}
1687
 
1688
#ifdef MODULE
1689
int init_module(void)
1690
{
1691
    register_symtab(NULL);
1692
    return pi_init();
1693
}
1694
 
1695
void cleanup_module(void)
1696
{
1697
    free_irq(pi0a.irq, NULL);   /* IRQs and IO Ports are shared */
1698
    release_region(pi0a.base_addr & 0x3f0, PI_TOTAL_SIZE);
1699
    irq2dev_map[pi0a.irq] = NULL;
1700
 
1701
    kfree(pi0a.priv);
1702
    pi0a.priv = NULL;
1703
    unregister_netdev(&pi0a);
1704
 
1705
    kfree(pi0b.priv);
1706
    pi0b.priv = NULL;
1707
    unregister_netdev(&pi0b);
1708
}
1709
#endif
1710
 
1711
/*
1712
 * Local variables:
1713
 *  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c skeleton.c"
1714
 *  version-control: t
1715
 *  kept-new-versions: 5
1716
 *  tab-width: 4
1717
 * End:
1718
 */

powered by: WebSVN 2.1.0

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