VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / net / hamradio / baycom_epp.c
1 /*****************************************************************************/
2
3 /*
4  *      baycom_epp.c  -- baycom epp radio modem driver.
5  *
6  *      Copyright (C) 1998-2000
7  *          Thomas Sailer (sailer@ife.ee.ethz.ch)
8  *
9  *      This program is free software; you can redistribute it and/or modify
10  *      it under the terms of the GNU General Public License as published by
11  *      the Free Software Foundation; either version 2 of the License, or
12  *      (at your option) any later version.
13  *
14  *      This program is distributed in the hope that it will be useful,
15  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *      GNU General Public License for more details.
18  *
19  *      You should have received a copy of the GNU General Public License
20  *      along with this program; if not, write to the Free Software
21  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  *  Please note that the GPL allows you to use the driver, NOT the radio.
24  *  In order to use the radio, you need a license from the communications
25  *  authority of your country.
26  *
27  *
28  *  History:
29  *   0.1  xx.xx.1998  Initial version by Matthias Welwarsky (dg2fef)
30  *   0.2  21.04.1998  Massive rework by Thomas Sailer
31  *                    Integrated FPGA EPP modem configuration routines
32  *   0.3  11.05.1998  Took FPGA config out and moved it into a separate program
33  *   0.4  26.07.1999  Adapted to new lowlevel parport driver interface
34  *   0.5  03.08.1999  adapt to Linus' new __setup/__initcall
35  *                    removed some pre-2.2 kernel compatibility cruft
36  *   0.6  10.08.1999  Check if parport can do SPP and is safe to access during interrupt contexts
37  *   0.7  12.02.2000  adapted to softnet driver interface
38  *
39  */
40
41 /*****************************************************************************/
42
43 #include <linux/config.h>
44 #include <linux/module.h>
45 #include <linux/kernel.h>
46 #include <linux/init.h>
47 #include <linux/string.h>
48 #include <linux/workqueue.h>
49 #include <linux/fs.h>
50 #include <linux/parport.h>
51 #include <linux/smp_lock.h>
52 #include <asm/uaccess.h>
53 #include <linux/if_arp.h>
54 #include <linux/kmod.h>
55 #include <linux/hdlcdrv.h>
56 #include <linux/baycom.h>
57 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
58 /* prototypes for ax25_encapsulate and ax25_rebuild_header */
59 #include <net/ax25.h> 
60 #endif /* CONFIG_AX25 || CONFIG_AX25_MODULE */
61 #include <linux/crc-ccitt.h>
62
63 /* --------------------------------------------------------------------- */
64
65 #define BAYCOM_DEBUG
66 #define BAYCOM_MAGIC 19730510
67
68 /* --------------------------------------------------------------------- */
69
70 static const char paranoia_str[] = KERN_ERR 
71 "baycom_epp: bad magic number for hdlcdrv_state struct in routine %s\n";
72
73 #define baycom_paranoia_check(dev,routine,retval)                                              \
74 ({                                                                                             \
75         if (!dev || !dev->priv || ((struct baycom_state *)dev->priv)->magic != BAYCOM_MAGIC) { \
76                 printk(paranoia_str, routine);                                                 \
77                 return retval;                                                                 \
78         }                                                                                      \
79 })
80
81 #define baycom_paranoia_check_void(dev,routine)                                                \
82 ({                                                                                             \
83         if (!dev || !dev->priv || ((struct baycom_state *)dev->priv)->magic != BAYCOM_MAGIC) { \
84                 printk(paranoia_str, routine);                                                 \
85                 return;                                                                        \
86         }                                                                                      \
87 })
88
89 /* --------------------------------------------------------------------- */
90
91 static const char bc_drvname[] = "baycom_epp";
92 static const char bc_drvinfo[] = KERN_INFO "baycom_epp: (C) 1998-2000 Thomas Sailer, HB9JNX/AE4WA\n"
93 KERN_INFO "baycom_epp: version 0.7 compiled " __TIME__ " " __DATE__ "\n";
94
95 /* --------------------------------------------------------------------- */
96
97 #define NR_PORTS 4
98
99 static struct net_device *baycom_device[NR_PORTS];
100
101 /* --------------------------------------------------------------------- */
102
103 /* EPP status register */
104 #define EPP_DCDBIT      0x80
105 #define EPP_PTTBIT      0x08
106 #define EPP_NREF        0x01
107 #define EPP_NRAEF       0x02
108 #define EPP_NRHF        0x04
109 #define EPP_NTHF        0x20
110 #define EPP_NTAEF       0x10
111 #define EPP_NTEF        EPP_PTTBIT
112
113 /* EPP control register */
114 #define EPP_TX_FIFO_ENABLE 0x10
115 #define EPP_RX_FIFO_ENABLE 0x08
116 #define EPP_MODEM_ENABLE   0x20
117 #define EPP_LEDS           0xC0
118 #define EPP_IRQ_ENABLE     0x10
119
120 /* LPT registers */
121 #define LPTREG_ECONTROL       0x402
122 #define LPTREG_CONFIGB        0x401
123 #define LPTREG_CONFIGA        0x400
124 #define LPTREG_EPPDATA        0x004
125 #define LPTREG_EPPADDR        0x003
126 #define LPTREG_CONTROL        0x002
127 #define LPTREG_STATUS         0x001
128 #define LPTREG_DATA           0x000
129
130 /* LPT control register */
131 #define LPTCTRL_PROGRAM       0x04   /* 0 to reprogram */
132 #define LPTCTRL_WRITE         0x01
133 #define LPTCTRL_ADDRSTB       0x08
134 #define LPTCTRL_DATASTB       0x02
135 #define LPTCTRL_INTEN         0x10
136
137 /* LPT status register */
138 #define LPTSTAT_SHIFT_NINTR   6
139 #define LPTSTAT_WAIT          0x80
140 #define LPTSTAT_NINTR         (1<<LPTSTAT_SHIFT_NINTR)
141 #define LPTSTAT_PE            0x20
142 #define LPTSTAT_DONE          0x10
143 #define LPTSTAT_NERROR        0x08
144 #define LPTSTAT_EPPTIMEOUT    0x01
145
146 /* LPT data register */
147 #define LPTDATA_SHIFT_TDI     0
148 #define LPTDATA_SHIFT_TMS     2
149 #define LPTDATA_TDI           (1<<LPTDATA_SHIFT_TDI)
150 #define LPTDATA_TCK           0x02
151 #define LPTDATA_TMS           (1<<LPTDATA_SHIFT_TMS)
152 #define LPTDATA_INITBIAS      0x80
153
154
155 /* EPP modem config/status bits */
156 #define EPP_DCDBIT            0x80
157 #define EPP_PTTBIT            0x08
158 #define EPP_RXEBIT            0x01
159 #define EPP_RXAEBIT           0x02
160 #define EPP_RXHFULL           0x04
161
162 #define EPP_NTHF              0x20
163 #define EPP_NTAEF             0x10
164 #define EPP_NTEF              EPP_PTTBIT
165
166 #define EPP_TX_FIFO_ENABLE    0x10
167 #define EPP_RX_FIFO_ENABLE    0x08
168 #define EPP_MODEM_ENABLE      0x20
169 #define EPP_LEDS              0xC0
170 #define EPP_IRQ_ENABLE        0x10
171
172 /* Xilinx 4k JTAG instructions */
173 #define XC4K_IRLENGTH   3
174 #define XC4K_EXTEST     0
175 #define XC4K_PRELOAD    1
176 #define XC4K_CONFIGURE  5
177 #define XC4K_BYPASS     7
178
179 #define EPP_CONVENTIONAL  0
180 #define EPP_FPGA          1
181 #define EPP_FPGAEXTSTATUS 2
182
183 #define TXBUFFER_SIZE     ((HDLCDRV_MAXFLEN*6/5)+8)
184
185 /* ---------------------------------------------------------------------- */
186 /*
187  * Information that need to be kept for each board.
188  */
189
190 struct baycom_state {
191         int magic;
192
193         struct pardevice *pdev;
194         unsigned int work_running;
195         struct work_struct run_work;
196         unsigned int modem;
197         unsigned int bitrate;
198         unsigned char stat;
199
200         struct {
201                 unsigned int intclk;
202                 unsigned int fclk;
203                 unsigned int bps;
204                 unsigned int extmodem;
205                 unsigned int loopback;
206         } cfg;
207
208         struct hdlcdrv_channel_params ch_params;
209
210         struct {
211                 unsigned int bitbuf, bitstream, numbits, state;
212                 unsigned char *bufptr;
213                 int bufcnt;
214                 unsigned char buf[TXBUFFER_SIZE];
215         } hdlcrx;
216
217         struct {
218                 int calibrate;
219                 int slotcnt;
220                 int flags;
221                 enum { tx_idle = 0, tx_keyup, tx_data, tx_tail } state;
222                 unsigned char *bufptr;
223                 int bufcnt;
224                 unsigned char buf[TXBUFFER_SIZE];
225         } hdlctx;
226
227         struct net_device_stats stats;
228         unsigned int ptt_keyed;
229         struct sk_buff *skb;  /* next transmit packet  */
230
231 #ifdef BAYCOM_DEBUG
232         struct debug_vals {
233                 unsigned long last_jiffies;
234                 unsigned cur_intcnt;
235                 unsigned last_intcnt;
236                 int cur_pllcorr;
237                 int last_pllcorr;
238                 unsigned int mod_cycles;
239                 unsigned int demod_cycles;
240         } debug_vals;
241 #endif /* BAYCOM_DEBUG */
242 };
243
244 /* --------------------------------------------------------------------- */
245
246 #define KISS_VERBOSE
247
248 /* --------------------------------------------------------------------- */
249
250 #define PARAM_TXDELAY   1
251 #define PARAM_PERSIST   2
252 #define PARAM_SLOTTIME  3
253 #define PARAM_TXTAIL    4
254 #define PARAM_FULLDUP   5
255 #define PARAM_HARDWARE  6
256 #define PARAM_RETURN    255
257
258 /* --------------------------------------------------------------------- */
259 /*
260  * the CRC routines are stolen from WAMPES
261  * by Dieter Deyke
262  */
263
264
265 /*---------------------------------------------------------------------------*/
266
267 #if 0
268 static inline void append_crc_ccitt(unsigned char *buffer, int len)
269 {
270         unsigned int crc = 0xffff;
271
272         for (;len>0;len--)
273                 crc = (crc >> 8) ^ crc_ccitt_table[(crc ^ *buffer++) & 0xff];
274         crc ^= 0xffff;
275         *buffer++ = crc;
276         *buffer++ = crc >> 8;
277 }
278 #endif
279
280 /*---------------------------------------------------------------------------*/
281
282 static inline int check_crc_ccitt(const unsigned char *buf, int cnt)
283 {
284         return (crc_ccitt(0xffff, buf, cnt) & 0xffff) == 0xf0b8;
285 }
286
287 /*---------------------------------------------------------------------------*/
288
289 static inline int calc_crc_ccitt(const unsigned char *buf, int cnt)
290 {
291         return (crc_ccitt(0xffff, buf, cnt) ^ 0xffff) & 0xffff;
292 }
293
294 /* ---------------------------------------------------------------------- */
295
296 #define tenms_to_flags(bc,tenms) ((tenms * bc->bitrate) / 800)
297
298 /* --------------------------------------------------------------------- */
299
300 static inline void baycom_int_freq(struct baycom_state *bc)
301 {
302 #ifdef BAYCOM_DEBUG
303         unsigned long cur_jiffies = jiffies;
304         /*
305          * measure the interrupt frequency
306          */
307         bc->debug_vals.cur_intcnt++;
308         if ((cur_jiffies - bc->debug_vals.last_jiffies) >= HZ) {
309                 bc->debug_vals.last_jiffies = cur_jiffies;
310                 bc->debug_vals.last_intcnt = bc->debug_vals.cur_intcnt;
311                 bc->debug_vals.cur_intcnt = 0;
312                 bc->debug_vals.last_pllcorr = bc->debug_vals.cur_pllcorr;
313                 bc->debug_vals.cur_pllcorr = 0;
314         }
315 #endif /* BAYCOM_DEBUG */
316 }
317
318 /* ---------------------------------------------------------------------- */
319 /*
320  *    eppconfig_path should be setable  via /proc/sys.
321  */
322
323 static char eppconfig_path[256] = "/usr/sbin/eppfpga";
324
325 static char *envp[] = { "HOME=/", "TERM=linux", "PATH=/usr/bin:/bin", NULL };
326
327 /* eppconfig: called during ifconfig up to configure the modem */
328 static int eppconfig(struct baycom_state *bc)
329 {
330         char modearg[256];
331         char portarg[16];
332         char *argv[] = { eppconfig_path, "-s", "-p", portarg, "-m", modearg,
333                          NULL };
334
335         /* set up arguments */
336         sprintf(modearg, "%sclk,%smodem,fclk=%d,bps=%d,divider=%d%s,extstat",
337                 bc->cfg.intclk ? "int" : "ext",
338                 bc->cfg.extmodem ? "ext" : "int", bc->cfg.fclk, bc->cfg.bps,
339                 (bc->cfg.fclk + 8 * bc->cfg.bps) / (16 * bc->cfg.bps),
340                 bc->cfg.loopback ? ",loopback" : "");
341         sprintf(portarg, "%ld", bc->pdev->port->base);
342         printk(KERN_DEBUG "%s: %s -s -p %s -m %s\n", bc_drvname, eppconfig_path, portarg, modearg);
343
344         return call_usermodehelper(eppconfig_path, argv, envp, 1);
345 }
346
347 /* ---------------------------------------------------------------------- */
348
349 static void epp_interrupt(int irq, void *dev_id, struct pt_regs *regs)
350 {
351 }
352
353 /* ---------------------------------------------------------------------- */
354
355 static inline void do_kiss_params(struct baycom_state *bc,
356                                   unsigned char *data, unsigned long len)
357 {
358
359 #ifdef KISS_VERBOSE
360 #define PKP(a,b) printk(KERN_INFO "baycomm_epp: channel params: " a "\n", b)
361 #else /* KISS_VERBOSE */              
362 #define PKP(a,b) 
363 #endif /* KISS_VERBOSE */             
364
365         if (len < 2)
366                 return;
367         switch(data[0]) {
368         case PARAM_TXDELAY:
369                 bc->ch_params.tx_delay = data[1];
370                 PKP("TX delay = %ums", 10 * bc->ch_params.tx_delay);
371                 break;
372         case PARAM_PERSIST:   
373                 bc->ch_params.ppersist = data[1];
374                 PKP("p persistence = %u", bc->ch_params.ppersist);
375                 break;
376         case PARAM_SLOTTIME:  
377                 bc->ch_params.slottime = data[1];
378                 PKP("slot time = %ums", bc->ch_params.slottime);
379                 break;
380         case PARAM_TXTAIL:    
381                 bc->ch_params.tx_tail = data[1];
382                 PKP("TX tail = %ums", bc->ch_params.tx_tail);
383                 break;
384         case PARAM_FULLDUP:   
385                 bc->ch_params.fulldup = !!data[1];
386                 PKP("%s duplex", bc->ch_params.fulldup ? "full" : "half");
387                 break;
388         default:
389                 break;
390         }
391 #undef PKP
392 }
393
394 /* --------------------------------------------------------------------- */
395 /*
396  * high performance HDLC encoder
397  * yes, it's ugly, but generates pretty good code
398  */
399
400 #define ENCODEITERA(j)                         \
401 ({                                             \
402         if (!(notbitstream & (0x1f0 << j)))    \
403                 goto stuff##j;                 \
404   encodeend##j:         ;                      \
405 })
406
407 #define ENCODEITERB(j)                                          \
408 ({                                                              \
409   stuff##j:                                                     \
410         bitstream &= ~(0x100 << j);                             \
411         bitbuf = (bitbuf & (((2 << j) << numbit) - 1)) |        \
412                 ((bitbuf & ~(((2 << j) << numbit) - 1)) << 1);  \
413         numbit++;                                               \
414         notbitstream = ~bitstream;                              \
415         goto encodeend##j;                                      \
416 })
417
418
419 static void encode_hdlc(struct baycom_state *bc)
420 {
421         struct sk_buff *skb;
422         unsigned char *wp, *bp;
423         int pkt_len;
424         unsigned bitstream, notbitstream, bitbuf, numbit, crc;
425         unsigned char crcarr[2];
426         
427         if (bc->hdlctx.bufcnt > 0)
428                 return;
429         skb = bc->skb;
430         if (!skb)
431                 return;
432         bc->skb = NULL;
433         pkt_len = skb->len-1; /* strip KISS byte */
434         wp = bc->hdlctx.buf;
435         bp = skb->data+1;
436         crc = calc_crc_ccitt(bp, pkt_len);
437         crcarr[0] = crc;
438         crcarr[1] = crc >> 8;
439         *wp++ = 0x7e;
440         bitstream = bitbuf = numbit = 0;
441         while (pkt_len > -2) {
442                 bitstream >>= 8;
443                 bitstream |= ((unsigned int)*bp) << 8;
444                 bitbuf |= ((unsigned int)*bp) << numbit;
445                 notbitstream = ~bitstream;
446                 bp++;
447                 pkt_len--;
448                 if (!pkt_len)
449                         bp = crcarr;
450                 ENCODEITERA(0);
451                 ENCODEITERA(1);
452                 ENCODEITERA(2);
453                 ENCODEITERA(3);
454                 ENCODEITERA(4);
455                 ENCODEITERA(5);
456                 ENCODEITERA(6);
457                 ENCODEITERA(7);
458                 goto enditer;
459                 ENCODEITERB(0);
460                 ENCODEITERB(1);
461                 ENCODEITERB(2);
462                 ENCODEITERB(3);
463                 ENCODEITERB(4);
464                 ENCODEITERB(5);
465                 ENCODEITERB(6);
466                 ENCODEITERB(7);
467         enditer:
468                 numbit += 8;
469                 while (numbit >= 8) {
470                         *wp++ = bitbuf;
471                         bitbuf >>= 8;
472                         numbit -= 8;
473                 }
474         }
475         bitbuf |= 0x7e7e << numbit;
476         numbit += 16;
477         while (numbit >= 8) {
478                 *wp++ = bitbuf;
479                 bitbuf >>= 8;
480                 numbit -= 8;
481         }
482         bc->hdlctx.bufptr = bc->hdlctx.buf;
483         bc->hdlctx.bufcnt = wp - bc->hdlctx.buf;
484         dev_kfree_skb(skb);
485         bc->stats.tx_packets++;
486 }
487
488 /* ---------------------------------------------------------------------- */
489
490 static unsigned short random_seed;
491
492 static inline unsigned short random_num(void)
493 {
494         random_seed = 28629 * random_seed + 157;
495         return random_seed;
496 }
497
498 /* ---------------------------------------------------------------------- */
499
500 static int transmit(struct baycom_state *bc, int cnt, unsigned char stat)
501 {
502         struct parport *pp = bc->pdev->port;
503         unsigned char tmp[128];
504         int i, j;
505
506         if (bc->hdlctx.state == tx_tail && !(stat & EPP_PTTBIT))
507                 bc->hdlctx.state = tx_idle;
508         if (bc->hdlctx.state == tx_idle && bc->hdlctx.calibrate <= 0) {
509                 if (bc->hdlctx.bufcnt <= 0)
510                         encode_hdlc(bc);
511                 if (bc->hdlctx.bufcnt <= 0)
512                         return 0;
513                 if (!bc->ch_params.fulldup) {
514                         if (!(stat & EPP_DCDBIT)) {
515                                 bc->hdlctx.slotcnt = bc->ch_params.slottime;
516                                 return 0;
517                         }
518                         if ((--bc->hdlctx.slotcnt) > 0)
519                                 return 0;
520                         bc->hdlctx.slotcnt = bc->ch_params.slottime;
521                         if ((random_num() % 256) > bc->ch_params.ppersist)
522                                 return 0;
523                 }
524         }
525         if (bc->hdlctx.state == tx_idle && bc->hdlctx.bufcnt > 0) {
526                 bc->hdlctx.state = tx_keyup;
527                 bc->hdlctx.flags = tenms_to_flags(bc, bc->ch_params.tx_delay);
528                 bc->ptt_keyed++;
529         }
530         while (cnt > 0) {
531                 switch (bc->hdlctx.state) {
532                 case tx_keyup:
533                         i = min_t(int, cnt, bc->hdlctx.flags);
534                         cnt -= i;
535                         bc->hdlctx.flags -= i;
536                         if (bc->hdlctx.flags <= 0)
537                                 bc->hdlctx.state = tx_data;
538                         memset(tmp, 0x7e, sizeof(tmp));
539                         while (i > 0) {
540                                 j = (i > sizeof(tmp)) ? sizeof(tmp) : i;
541                                 if (j != pp->ops->epp_write_data(pp, tmp, j, 0))
542                                         return -1;
543                                 i -= j;
544                         }
545                         break;
546
547                 case tx_data:
548                         if (bc->hdlctx.bufcnt <= 0) {
549                                 encode_hdlc(bc);
550                                 if (bc->hdlctx.bufcnt <= 0) {
551                                         bc->hdlctx.state = tx_tail;
552                                         bc->hdlctx.flags = tenms_to_flags(bc, bc->ch_params.tx_tail);
553                                         break;
554                                 }
555                         }
556                         i = min_t(int, cnt, bc->hdlctx.bufcnt);
557                         bc->hdlctx.bufcnt -= i;
558                         cnt -= i;
559                         if (i != pp->ops->epp_write_data(pp, bc->hdlctx.bufptr, i, 0))
560                                         return -1;
561                         bc->hdlctx.bufptr += i;
562                         break;
563                         
564                 case tx_tail:
565                         encode_hdlc(bc);
566                         if (bc->hdlctx.bufcnt > 0) {
567                                 bc->hdlctx.state = tx_data;
568                                 break;
569                         }
570                         i = min_t(int, cnt, bc->hdlctx.flags);
571                         if (i) {
572                                 cnt -= i;
573                                 bc->hdlctx.flags -= i;
574                                 memset(tmp, 0x7e, sizeof(tmp));
575                                 while (i > 0) {
576                                         j = (i > sizeof(tmp)) ? sizeof(tmp) : i;
577                                         if (j != pp->ops->epp_write_data(pp, tmp, j, 0))
578                                                 return -1;
579                                         i -= j;
580                                 }
581                                 break;
582                         }
583
584                 default:  /* fall through */
585                         if (bc->hdlctx.calibrate <= 0)
586                                 return 0;
587                         i = min_t(int, cnt, bc->hdlctx.calibrate);
588                         cnt -= i;
589                         bc->hdlctx.calibrate -= i;
590                         memset(tmp, 0, sizeof(tmp));
591                         while (i > 0) {
592                                 j = (i > sizeof(tmp)) ? sizeof(tmp) : i;
593                                 if (j != pp->ops->epp_write_data(pp, tmp, j, 0))
594                                         return -1;
595                                 i -= j;
596                         }
597                         break;
598                 }
599         }
600         return 0;
601 }
602
603 /* ---------------------------------------------------------------------- */
604
605 static void do_rxpacket(struct net_device *dev)
606 {
607         struct baycom_state *bc = netdev_priv(dev);
608         struct sk_buff *skb;
609         unsigned char *cp;
610         unsigned pktlen;
611
612         if (bc->hdlcrx.bufcnt < 4) 
613                 return;
614         if (!check_crc_ccitt(bc->hdlcrx.buf, bc->hdlcrx.bufcnt)) 
615                 return;
616         pktlen = bc->hdlcrx.bufcnt-2+1; /* KISS kludge */
617         if (!(skb = dev_alloc_skb(pktlen))) {
618                 printk("%s: memory squeeze, dropping packet\n", dev->name);
619                 bc->stats.rx_dropped++;
620                 return;
621         }
622         skb->dev = dev;
623         cp = skb_put(skb, pktlen);
624         *cp++ = 0; /* KISS kludge */
625         memcpy(cp, bc->hdlcrx.buf, pktlen - 1);
626         skb->protocol = htons(ETH_P_AX25);
627         skb->mac.raw = skb->data;
628         netif_rx(skb);
629         dev->last_rx = jiffies;
630         bc->stats.rx_packets++;
631 }
632
633 #define DECODEITERA(j)                                                        \
634 ({                                                                            \
635         if (!(notbitstream & (0x0fc << j)))              /* flag or abort */  \
636                 goto flgabrt##j;                                              \
637         if ((bitstream & (0x1f8 << j)) == (0xf8 << j))   /* stuffed bit */    \
638                 goto stuff##j;                                                \
639   enditer##j:      ;                                                           \
640 })
641
642 #define DECODEITERB(j)                                                                 \
643 ({                                                                                     \
644   flgabrt##j:                                                                          \
645         if (!(notbitstream & (0x1fc << j))) {              /* abort received */        \
646                 state = 0;                                                             \
647                 goto enditer##j;                                                       \
648         }                                                                              \
649         if ((bitstream & (0x1fe << j)) != (0x0fc << j))   /* flag received */          \
650                 goto enditer##j;                                                       \
651         if (state)                                                                     \
652                 do_rxpacket(dev);                                                      \
653         bc->hdlcrx.bufcnt = 0;                                                         \
654         bc->hdlcrx.bufptr = bc->hdlcrx.buf;                                            \
655         state = 1;                                                                     \
656         numbits = 7-j;                                                                 \
657         goto enditer##j;                                                               \
658   stuff##j:                                                                            \
659         numbits--;                                                                     \
660         bitbuf = (bitbuf & ((~0xff) << j)) | ((bitbuf & ~((~0xff) << j)) << 1);        \
661         goto enditer##j;                                                               \
662 })
663         
664 static int receive(struct net_device *dev, int cnt)
665 {
666         struct baycom_state *bc = netdev_priv(dev);
667         struct parport *pp = bc->pdev->port;
668         unsigned int bitbuf, notbitstream, bitstream, numbits, state;
669         unsigned char tmp[128];
670         unsigned char *cp;
671         int cnt2, ret = 0;
672         
673         numbits = bc->hdlcrx.numbits;
674         state = bc->hdlcrx.state;
675         bitstream = bc->hdlcrx.bitstream;
676         bitbuf = bc->hdlcrx.bitbuf;
677         while (cnt > 0) {
678                 cnt2 = (cnt > sizeof(tmp)) ? sizeof(tmp) : cnt;
679                 cnt -= cnt2;
680                 if (cnt2 != pp->ops->epp_read_data(pp, tmp, cnt2, 0)) {
681                         ret = -1;
682                         break;
683                 }
684                 cp = tmp;
685                 for (; cnt2 > 0; cnt2--, cp++) {
686                         bitstream >>= 8;
687                         bitstream |= (*cp) << 8;
688                         bitbuf >>= 8;
689                         bitbuf |= (*cp) << 8;
690                         numbits += 8;
691                         notbitstream = ~bitstream;
692                         DECODEITERA(0);
693                         DECODEITERA(1);
694                         DECODEITERA(2);
695                         DECODEITERA(3);
696                         DECODEITERA(4);
697                         DECODEITERA(5);
698                         DECODEITERA(6);
699                         DECODEITERA(7);
700                         goto enddec;
701                         DECODEITERB(0);
702                         DECODEITERB(1);
703                         DECODEITERB(2);
704                         DECODEITERB(3);
705                         DECODEITERB(4);
706                         DECODEITERB(5);
707                         DECODEITERB(6);
708                         DECODEITERB(7);
709                 enddec:
710                         while (state && numbits >= 8) {
711                                 if (bc->hdlcrx.bufcnt >= TXBUFFER_SIZE) {
712                                         state = 0;
713                                 } else {
714                                         *(bc->hdlcrx.bufptr)++ = bitbuf >> (16-numbits);
715                                         bc->hdlcrx.bufcnt++;
716                                         numbits -= 8;
717                                 }
718                         }
719                 }
720         }
721         bc->hdlcrx.numbits = numbits;
722         bc->hdlcrx.state = state;
723         bc->hdlcrx.bitstream = bitstream;
724         bc->hdlcrx.bitbuf = bitbuf;
725         return ret;
726 }
727
728 /* --------------------------------------------------------------------- */
729
730 #ifdef __i386__
731 #include <asm/msr.h>
732 #define GETTICK(x)                                                \
733 ({                                                                \
734         if (cpu_has_tsc)                                          \
735                 rdtscl(x);                                        \
736 })
737 #else /* __i386__ */
738 #define GETTICK(x)
739 #endif /* __i386__ */
740
741 static void epp_bh(struct net_device *dev)
742 {
743         struct baycom_state *bc;
744         struct parport *pp;
745         unsigned char stat;
746         unsigned char tmp[2];
747         unsigned int time1 = 0, time2 = 0, time3 = 0;
748         int cnt, cnt2;
749         
750         baycom_paranoia_check_void(dev, "epp_bh");
751         bc = netdev_priv(dev);
752         if (!bc->work_running)
753                 return;
754         baycom_int_freq(bc);
755         pp = bc->pdev->port;
756         /* update status */
757         if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
758                 goto epptimeout;
759         bc->stat = stat;
760         bc->debug_vals.last_pllcorr = stat;
761         GETTICK(time1);
762         if (bc->modem == EPP_FPGAEXTSTATUS) {
763                 /* get input count */
764                 tmp[0] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE|1;
765                 if (pp->ops->epp_write_addr(pp, tmp, 1, 0) != 1)
766                         goto epptimeout;
767                 if (pp->ops->epp_read_addr(pp, tmp, 2, 0) != 2)
768                         goto epptimeout;
769                 cnt = tmp[0] | (tmp[1] << 8);
770                 cnt &= 0x7fff;
771                 /* get output count */
772                 tmp[0] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE|2;
773                 if (pp->ops->epp_write_addr(pp, tmp, 1, 0) != 1)
774                         goto epptimeout;
775                 if (pp->ops->epp_read_addr(pp, tmp, 2, 0) != 2)
776                         goto epptimeout;
777                 cnt2 = tmp[0] | (tmp[1] << 8);
778                 cnt2 = 16384 - (cnt2 & 0x7fff);
779                 /* return to normal */
780                 tmp[0] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE;
781                 if (pp->ops->epp_write_addr(pp, tmp, 1, 0) != 1)
782                         goto epptimeout;
783                 if (transmit(bc, cnt2, stat))
784                         goto epptimeout;
785                 GETTICK(time2);
786                 if (receive(dev, cnt))
787                         goto epptimeout;
788                 if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
789                         goto epptimeout;
790                 bc->stat = stat;
791         } else {
792                 /* try to tx */
793                 switch (stat & (EPP_NTAEF|EPP_NTHF)) {
794                 case EPP_NTHF:
795                         cnt = 2048 - 256;
796                         break;
797                 
798                 case EPP_NTAEF:
799                         cnt = 2048 - 1793;
800                         break;
801                 
802                 case 0:
803                         cnt = 0;
804                         break;
805                 
806                 default:
807                         cnt = 2048 - 1025;
808                         break;
809                 }
810                 if (transmit(bc, cnt, stat))
811                         goto epptimeout;
812                 GETTICK(time2);
813                 /* do receiver */
814                 while ((stat & (EPP_NRAEF|EPP_NRHF)) != EPP_NRHF) {
815                         switch (stat & (EPP_NRAEF|EPP_NRHF)) {
816                         case EPP_NRAEF:
817                                 cnt = 1025;
818                                 break;
819
820                         case 0:
821                                 cnt = 1793;
822                                 break;
823
824                         default:
825                                 cnt = 256;
826                                 break;
827                         }
828                         if (receive(dev, cnt))
829                                 goto epptimeout;
830                         if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
831                                 goto epptimeout;
832                 }
833                 cnt = 0;
834                 if (bc->bitrate < 50000)
835                         cnt = 256;
836                 else if (bc->bitrate < 100000)
837                         cnt = 128;
838                 while (cnt > 0 && stat & EPP_NREF) {
839                         if (receive(dev, 1))
840                                 goto epptimeout;
841                         cnt--;
842                         if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
843                                 goto epptimeout;
844                 }
845         }
846         GETTICK(time3);
847 #ifdef BAYCOM_DEBUG
848         bc->debug_vals.mod_cycles = time2 - time1;
849         bc->debug_vals.demod_cycles = time3 - time2;
850 #endif /* BAYCOM_DEBUG */
851         schedule_delayed_work(&bc->run_work, 1);
852         if (!bc->skb)
853                 netif_wake_queue(dev);
854         return;
855  epptimeout:
856         printk(KERN_ERR "%s: EPP timeout!\n", bc_drvname);
857 }
858
859 /* ---------------------------------------------------------------------- */
860 /*
861  * ===================== network driver interface =========================
862  */
863
864 static int baycom_send_packet(struct sk_buff *skb, struct net_device *dev)
865 {
866         struct baycom_state *bc;
867
868         baycom_paranoia_check(dev, "baycom_send_packet", 0);
869         bc = netdev_priv(dev);
870         if (skb->data[0] != 0) {
871                 do_kiss_params(bc, skb->data, skb->len);
872                 dev_kfree_skb(skb);
873                 return 0;
874         }
875         if (bc->skb)
876                 return -1;
877         /* strip KISS byte */
878         if (skb->len >= HDLCDRV_MAXFLEN+1 || skb->len < 3) {
879                 dev_kfree_skb(skb);
880                 return 0;
881         }
882         netif_stop_queue(dev);
883         bc->skb = skb;
884         return 0;
885 }
886
887 /* --------------------------------------------------------------------- */
888
889 static int baycom_set_mac_address(struct net_device *dev, void *addr)
890 {
891         struct sockaddr *sa = (struct sockaddr *)addr;
892
893         /* addr is an AX.25 shifted ASCII mac address */
894         memcpy(dev->dev_addr, sa->sa_data, dev->addr_len); 
895         return 0;                                         
896 }
897
898 /* --------------------------------------------------------------------- */
899
900 static struct net_device_stats *baycom_get_stats(struct net_device *dev)
901 {
902         struct baycom_state *bc;
903
904         baycom_paranoia_check(dev, "baycom_get_stats", NULL);
905         bc = netdev_priv(dev);
906         /* 
907          * Get the current statistics.  This may be called with the
908          * card open or closed. 
909          */
910         return &bc->stats;
911 }
912
913 /* --------------------------------------------------------------------- */
914
915 static void epp_wakeup(void *handle)
916 {
917         struct net_device *dev = (struct net_device *)handle;
918         struct baycom_state *bc;
919
920         baycom_paranoia_check_void(dev, "epp_wakeup");
921         bc = netdev_priv(dev);
922         printk(KERN_DEBUG "baycom_epp: %s: why am I being woken up?\n", dev->name);
923         if (!parport_claim(bc->pdev))
924                 printk(KERN_DEBUG "baycom_epp: %s: I'm broken.\n", dev->name);
925 }
926
927 /* --------------------------------------------------------------------- */
928
929 /*
930  * Open/initialize the board. This is called (in the current kernel)
931  * sometime after booting when the 'ifconfig' program is run.
932  *
933  * This routine should set everything up anew at each open, even
934  * registers that "should" only need to be set once at boot, so that
935  * there is non-reboot way to recover if something goes wrong.
936  */
937
938 static int epp_open(struct net_device *dev)
939 {
940         struct baycom_state *bc;
941         struct parport *pp;
942         unsigned int i, j;
943         unsigned char tmp[128];
944         unsigned char stat;
945         unsigned long tstart;
946         
947         baycom_paranoia_check(dev, "epp_open", -ENXIO);
948         bc = netdev_priv(dev);
949         pp = parport_find_base(dev->base_addr);
950         if (!pp) {
951                 printk(KERN_ERR "%s: parport at 0x%lx unknown\n", bc_drvname, dev->base_addr);
952                 return -ENXIO;
953         }
954 #if 0
955         if (pp->irq < 0) {
956                 printk(KERN_ERR "%s: parport at 0x%lx has no irq\n", bc_drvname, pp->base);
957                 parport_put_port(pp);
958                 return -ENXIO;
959         }
960 #endif
961         if ((~pp->modes) & (PARPORT_MODE_TRISTATE | PARPORT_MODE_PCSPP | PARPORT_MODE_SAFEININT)) {
962                 printk(KERN_ERR "%s: parport at 0x%lx cannot be used\n",
963                        bc_drvname, pp->base);
964                 parport_put_port(pp);
965                 return -EIO;
966         }
967         memset(&bc->modem, 0, sizeof(bc->modem));
968         bc->pdev = parport_register_device(pp, dev->name, NULL, epp_wakeup, 
969                                         epp_interrupt, PARPORT_DEV_EXCL, dev);
970         parport_put_port(pp);
971         if (!bc->pdev) {
972                 printk(KERN_ERR "%s: cannot register parport at 0x%lx\n", bc_drvname, pp->base);
973                 return -ENXIO;
974         }
975         if (parport_claim(bc->pdev)) {
976                 printk(KERN_ERR "%s: parport at 0x%lx busy\n", bc_drvname, pp->base);
977                 parport_unregister_device(bc->pdev);
978                 return -EBUSY;
979         }
980         dev->irq = /*pp->irq*/ 0;
981         INIT_WORK(&bc->run_work, (void *)(void *)epp_bh, dev);
982         bc->work_running = 1;
983         bc->modem = EPP_CONVENTIONAL;
984         if (eppconfig(bc))
985                 printk(KERN_INFO "%s: no FPGA detected, assuming conventional EPP modem\n", bc_drvname);
986         else
987                 bc->modem = /*EPP_FPGA*/ EPP_FPGAEXTSTATUS;
988         parport_write_control(pp, LPTCTRL_PROGRAM); /* prepare EPP mode; we aren't using interrupts */
989         /* reset the modem */
990         tmp[0] = 0;
991         tmp[1] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE;
992         if (pp->ops->epp_write_addr(pp, tmp, 2, 0) != 2)
993                 goto epptimeout;
994         /* autoprobe baud rate */
995         tstart = jiffies;
996         i = 0;
997         while ((signed)(jiffies-tstart-HZ/3) < 0) {
998                 if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
999                         goto epptimeout;
1000                 if ((stat & (EPP_NRAEF|EPP_NRHF)) == EPP_NRHF) {
1001                         schedule();
1002                         continue;
1003                 }
1004                 if (pp->ops->epp_read_data(pp, tmp, 128, 0) != 128)
1005                         goto epptimeout;
1006                 if (pp->ops->epp_read_data(pp, tmp, 128, 0) != 128)
1007                         goto epptimeout;
1008                 i += 256;
1009         }
1010         for (j = 0; j < 256; j++) {
1011                 if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
1012                         goto epptimeout;
1013                 if (!(stat & EPP_NREF))
1014                         break;
1015                 if (pp->ops->epp_read_data(pp, tmp, 1, 0) != 1)
1016                         goto epptimeout;
1017                 i++;
1018         }
1019         tstart = jiffies - tstart;
1020         bc->bitrate = i * (8 * HZ) / tstart;
1021         j = 1;
1022         i = bc->bitrate >> 3;
1023         while (j < 7 && i > 150) {
1024                 j++;
1025                 i >>= 1;
1026         }
1027         printk(KERN_INFO "%s: autoprobed bitrate: %d  int divider: %d  int rate: %d\n", 
1028                bc_drvname, bc->bitrate, j, bc->bitrate >> (j+2));
1029         tmp[0] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE/*|j*/;
1030         if (pp->ops->epp_write_addr(pp, tmp, 1, 0) != 1)
1031                 goto epptimeout;
1032         /*
1033          * initialise hdlc variables
1034          */
1035         bc->hdlcrx.state = 0;
1036         bc->hdlcrx.numbits = 0;
1037         bc->hdlctx.state = tx_idle;
1038         bc->hdlctx.bufcnt = 0;
1039         bc->hdlctx.slotcnt = bc->ch_params.slottime;
1040         bc->hdlctx.calibrate = 0;
1041         /* start the bottom half stuff */
1042         schedule_delayed_work(&bc->run_work, 1);
1043         netif_start_queue(dev);
1044         return 0;
1045
1046  epptimeout:
1047         printk(KERN_ERR "%s: epp timeout during bitrate probe\n", bc_drvname);
1048         parport_write_control(pp, 0); /* reset the adapter */
1049         parport_release(bc->pdev);
1050         parport_unregister_device(bc->pdev);
1051         return -EIO;
1052 }
1053
1054 /* --------------------------------------------------------------------- */
1055
1056 static int epp_close(struct net_device *dev)
1057 {
1058         struct baycom_state *bc;
1059         struct parport *pp;
1060         unsigned char tmp[1];
1061
1062         baycom_paranoia_check(dev, "epp_close", -EINVAL);
1063         bc = netdev_priv(dev);
1064         pp = bc->pdev->port;
1065         bc->work_running = 0;
1066         flush_scheduled_work();
1067         bc->stat = EPP_DCDBIT;
1068         tmp[0] = 0;
1069         pp->ops->epp_write_addr(pp, tmp, 1, 0);
1070         parport_write_control(pp, 0); /* reset the adapter */
1071         parport_release(bc->pdev);
1072         parport_unregister_device(bc->pdev);
1073         if (bc->skb)
1074                 dev_kfree_skb(bc->skb);
1075         bc->skb = NULL;
1076         printk(KERN_INFO "%s: close epp at iobase 0x%lx irq %u\n",
1077                bc_drvname, dev->base_addr, dev->irq);
1078         return 0;
1079 }
1080
1081 /* --------------------------------------------------------------------- */
1082
1083 static int baycom_setmode(struct baycom_state *bc, const char *modestr)
1084 {
1085         const char *cp;
1086
1087         if (strstr(modestr,"intclk"))
1088                 bc->cfg.intclk = 1;
1089         if (strstr(modestr,"extclk"))
1090                 bc->cfg.intclk = 0;
1091         if (strstr(modestr,"intmodem"))
1092                 bc->cfg.extmodem = 0;
1093         if (strstr(modestr,"extmodem"))
1094                 bc->cfg.extmodem = 1;
1095         if (strstr(modestr,"noloopback"))
1096                 bc->cfg.loopback = 0;
1097         if (strstr(modestr,"loopback"))
1098                 bc->cfg.loopback = 1;
1099         if ((cp = strstr(modestr,"fclk="))) {
1100                 bc->cfg.fclk = simple_strtoul(cp+5, NULL, 0);
1101                 if (bc->cfg.fclk < 1000000)
1102                         bc->cfg.fclk = 1000000;
1103                 if (bc->cfg.fclk > 25000000)
1104                         bc->cfg.fclk = 25000000;
1105         }
1106         if ((cp = strstr(modestr,"bps="))) {
1107                 bc->cfg.bps = simple_strtoul(cp+4, NULL, 0);
1108                 if (bc->cfg.bps < 1000)
1109                         bc->cfg.bps = 1000;
1110                 if (bc->cfg.bps > 1500000)
1111                         bc->cfg.bps = 1500000;
1112         }
1113         return 0;
1114 }
1115
1116 /* --------------------------------------------------------------------- */
1117
1118 static int baycom_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1119 {
1120         struct baycom_state *bc;
1121         struct hdlcdrv_ioctl hi;
1122
1123         baycom_paranoia_check(dev, "baycom_ioctl", -EINVAL);
1124         bc = netdev_priv(dev);
1125         if (cmd != SIOCDEVPRIVATE)
1126                 return -ENOIOCTLCMD;
1127
1128         if (copy_from_user(&hi, ifr->ifr_data, sizeof(hi)))
1129                 return -EFAULT;
1130         switch (hi.cmd) {
1131         default:
1132                 return -ENOIOCTLCMD;
1133
1134         case HDLCDRVCTL_GETCHANNELPAR:
1135                 hi.data.cp.tx_delay = bc->ch_params.tx_delay;
1136                 hi.data.cp.tx_tail = bc->ch_params.tx_tail;
1137                 hi.data.cp.slottime = bc->ch_params.slottime;
1138                 hi.data.cp.ppersist = bc->ch_params.ppersist;
1139                 hi.data.cp.fulldup = bc->ch_params.fulldup;
1140                 break;
1141
1142         case HDLCDRVCTL_SETCHANNELPAR:
1143                 if (!capable(CAP_NET_ADMIN))
1144                         return -EACCES;
1145                 bc->ch_params.tx_delay = hi.data.cp.tx_delay;
1146                 bc->ch_params.tx_tail = hi.data.cp.tx_tail;
1147                 bc->ch_params.slottime = hi.data.cp.slottime;
1148                 bc->ch_params.ppersist = hi.data.cp.ppersist;
1149                 bc->ch_params.fulldup = hi.data.cp.fulldup;
1150                 bc->hdlctx.slotcnt = 1;
1151                 return 0;
1152                 
1153         case HDLCDRVCTL_GETMODEMPAR:
1154                 hi.data.mp.iobase = dev->base_addr;
1155                 hi.data.mp.irq = dev->irq;
1156                 hi.data.mp.dma = dev->dma;
1157                 hi.data.mp.dma2 = 0;
1158                 hi.data.mp.seriobase = 0;
1159                 hi.data.mp.pariobase = 0;
1160                 hi.data.mp.midiiobase = 0;
1161                 break;
1162
1163         case HDLCDRVCTL_SETMODEMPAR:
1164                 if ((!capable(CAP_SYS_RAWIO)) || netif_running(dev))
1165                         return -EACCES;
1166                 dev->base_addr = hi.data.mp.iobase;
1167                 dev->irq = /*hi.data.mp.irq*/0;
1168                 dev->dma = /*hi.data.mp.dma*/0;
1169                 return 0;       
1170                 
1171         case HDLCDRVCTL_GETSTAT:
1172                 hi.data.cs.ptt = !!(bc->stat & EPP_PTTBIT);
1173                 hi.data.cs.dcd = !(bc->stat & EPP_DCDBIT);
1174                 hi.data.cs.ptt_keyed = bc->ptt_keyed;
1175                 hi.data.cs.tx_packets = bc->stats.tx_packets;
1176                 hi.data.cs.tx_errors = bc->stats.tx_errors;
1177                 hi.data.cs.rx_packets = bc->stats.rx_packets;
1178                 hi.data.cs.rx_errors = bc->stats.rx_errors;
1179                 break;          
1180
1181         case HDLCDRVCTL_OLDGETSTAT:
1182                 hi.data.ocs.ptt = !!(bc->stat & EPP_PTTBIT);
1183                 hi.data.ocs.dcd = !(bc->stat & EPP_DCDBIT);
1184                 hi.data.ocs.ptt_keyed = bc->ptt_keyed;
1185                 break;          
1186
1187         case HDLCDRVCTL_CALIBRATE:
1188                 if (!capable(CAP_SYS_RAWIO))
1189                         return -EACCES;
1190                 bc->hdlctx.calibrate = hi.data.calibrate * bc->bitrate / 8;
1191                 return 0;
1192
1193         case HDLCDRVCTL_DRIVERNAME:
1194                 strncpy(hi.data.drivername, "baycom_epp", sizeof(hi.data.drivername));
1195                 break;
1196                 
1197         case HDLCDRVCTL_GETMODE:
1198                 sprintf(hi.data.modename, "%sclk,%smodem,fclk=%d,bps=%d%s", 
1199                         bc->cfg.intclk ? "int" : "ext",
1200                         bc->cfg.extmodem ? "ext" : "int", bc->cfg.fclk, bc->cfg.bps,
1201                         bc->cfg.loopback ? ",loopback" : "");
1202                 break;
1203
1204         case HDLCDRVCTL_SETMODE:
1205                 if (!capable(CAP_NET_ADMIN) || netif_running(dev))
1206                         return -EACCES;
1207                 hi.data.modename[sizeof(hi.data.modename)-1] = '\0';
1208                 return baycom_setmode(bc, hi.data.modename);
1209
1210         case HDLCDRVCTL_MODELIST:
1211                 strncpy(hi.data.modename, "intclk,extclk,intmodem,extmodem,divider=x",
1212                         sizeof(hi.data.modename));
1213                 break;
1214
1215         case HDLCDRVCTL_MODEMPARMASK:
1216                 return HDLCDRV_PARMASK_IOBASE;
1217
1218         }
1219         if (copy_to_user(ifr->ifr_data, &hi, sizeof(hi)))
1220                 return -EFAULT;
1221         return 0;
1222 }
1223
1224 /* --------------------------------------------------------------------- */
1225
1226 /*
1227  * Check for a network adaptor of this type, and return '0' if one exists.
1228  * If dev->base_addr == 0, probe all likely locations.
1229  * If dev->base_addr == 1, always return failure.
1230  * If dev->base_addr == 2, allocate space for the device and return success
1231  * (detachable devices only).
1232  */
1233 static void baycom_probe(struct net_device *dev)
1234 {
1235         static char ax25_bcast[AX25_ADDR_LEN] = {
1236                 'Q' << 1, 'S' << 1, 'T' << 1, ' ' << 1, ' ' << 1, ' ' << 1, '0' << 1
1237         };
1238         static char ax25_nocall[AX25_ADDR_LEN] = {
1239                 'L' << 1, 'I' << 1, 'N' << 1, 'U' << 1, 'X' << 1, ' ' << 1, '1' << 1
1240         };
1241         const struct hdlcdrv_channel_params dflt_ch_params = { 
1242                 20, 2, 10, 40, 0 
1243         };
1244         struct baycom_state *bc;
1245
1246         /*
1247          * not a real probe! only initialize data structures
1248          */
1249         bc = netdev_priv(dev);
1250         /*
1251          * initialize the baycom_state struct
1252          */
1253         bc->ch_params = dflt_ch_params;
1254         bc->ptt_keyed = 0;
1255
1256         /*
1257          * initialize the device struct
1258          */
1259         dev->open = epp_open;
1260         dev->stop = epp_close;
1261         dev->do_ioctl = baycom_ioctl;
1262         dev->hard_start_xmit = baycom_send_packet;
1263         dev->get_stats = baycom_get_stats;
1264
1265         /* Fill in the fields of the device structure */
1266         bc->skb = NULL;
1267         
1268 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
1269         dev->hard_header = ax25_encapsulate;
1270         dev->rebuild_header = ax25_rebuild_header;
1271 #else /* CONFIG_AX25 || CONFIG_AX25_MODULE */
1272         dev->hard_header = NULL;
1273         dev->rebuild_header = NULL;
1274 #endif /* CONFIG_AX25 || CONFIG_AX25_MODULE */
1275         dev->set_mac_address = baycom_set_mac_address;
1276         
1277         dev->type = ARPHRD_AX25;           /* AF_AX25 device */
1278         dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN;
1279         dev->mtu = AX25_DEF_PACLEN;        /* eth_mtu is the default */
1280         dev->addr_len = AX25_ADDR_LEN;     /* sizeof an ax.25 address */
1281         memcpy(dev->broadcast, ax25_bcast, AX25_ADDR_LEN);
1282         memcpy(dev->dev_addr, ax25_nocall, AX25_ADDR_LEN);
1283         dev->tx_queue_len = 16;
1284
1285         /* New style flags */
1286         dev->flags = 0;
1287 }
1288
1289 /* --------------------------------------------------------------------- */
1290
1291 /*
1292  * command line settable parameters
1293  */
1294 static const char *mode[NR_PORTS] = { "", };
1295 static int iobase[NR_PORTS] = { 0x378, };
1296
1297 MODULE_PARM(mode, "1-" __MODULE_STRING(NR_PORTS) "s");
1298 MODULE_PARM_DESC(mode, "baycom operating mode");
1299 MODULE_PARM(iobase, "1-" __MODULE_STRING(NR_PORTS) "i");
1300 MODULE_PARM_DESC(iobase, "baycom io base address");
1301
1302 MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
1303 MODULE_DESCRIPTION("Baycom epp amateur radio modem driver");
1304 MODULE_LICENSE("GPL");
1305
1306 /* --------------------------------------------------------------------- */
1307
1308 static void __init baycom_epp_dev_setup(struct net_device *dev)
1309 {
1310         struct baycom_state *bc = netdev_priv(dev);
1311
1312         /*
1313          * initialize part of the baycom_state struct
1314          */
1315         bc->magic = BAYCOM_MAGIC;
1316         bc->cfg.fclk = 19666600;
1317         bc->cfg.bps = 9600;
1318         /*
1319          * initialize part of the device struct
1320          */
1321         baycom_probe(dev);
1322 }
1323
1324 static int __init init_baycomepp(void)
1325 {
1326         int i, found = 0;
1327         char set_hw = 1;
1328
1329         printk(bc_drvinfo);
1330         /*
1331          * register net devices
1332          */
1333         for (i = 0; i < NR_PORTS; i++) {
1334                 struct net_device *dev;
1335                 
1336                 dev = alloc_netdev(sizeof(struct baycom_state), "bce%d",
1337                                    baycom_epp_dev_setup);
1338
1339                 if (!dev) {
1340                         printk(KERN_WARNING "bce%d : out of memory\n", i);
1341                         return found ? 0 : -ENOMEM;
1342                 }
1343                         
1344                 sprintf(dev->name, "bce%d", i);
1345                 dev->base_addr = iobase[i];
1346
1347                 if (!mode[i])
1348                         set_hw = 0;
1349                 if (!set_hw)
1350                         iobase[i] = 0;
1351
1352                 if (register_netdev(dev)) {
1353                         printk(KERN_WARNING "%s: cannot register net device %s\n", bc_drvname, dev->name);
1354                         free_netdev(dev);
1355                         break;
1356                 }
1357                 if (set_hw && baycom_setmode(dev->priv, mode[i]))
1358                         set_hw = 0;
1359                 baycom_device[i] = dev;
1360                 found++;
1361         }
1362
1363         return found ? 0 : -ENXIO;
1364 }
1365
1366 static void __exit cleanup_baycomepp(void)
1367 {
1368         int i;
1369
1370         for(i = 0; i < NR_PORTS; i++) {
1371                 struct net_device *dev = baycom_device[i];
1372
1373                 if (dev) {
1374                         struct baycom_state *bc = netdev_priv(dev);
1375                         if (bc->magic == BAYCOM_MAGIC) {
1376                                 unregister_netdev(dev);
1377                                 free_netdev(dev);
1378                         } else
1379                                 printk(paranoia_str, "cleanup_module");
1380                 }
1381         }
1382 }
1383
1384 module_init(init_baycomepp);
1385 module_exit(cleanup_baycomepp);
1386
1387 /* --------------------------------------------------------------------- */
1388
1389 #ifndef MODULE
1390
1391 /*
1392  * format: baycom_epp=io,mode
1393  * mode: fpga config options
1394  */
1395
1396 static int __init baycom_epp_setup(char *str)
1397 {
1398         static unsigned __initdata nr_dev = 0;
1399         int ints[2];
1400
1401         if (nr_dev >= NR_PORTS)
1402                 return 0;
1403         str = get_options(str, 2, ints);
1404         if (ints[0] < 1)
1405                 return 0;
1406         mode[nr_dev] = str;
1407         iobase[nr_dev] = ints[1];
1408         nr_dev++;
1409         return 1;
1410 }
1411
1412 __setup("baycom_epp=", baycom_epp_setup);
1413
1414 #endif /* MODULE */
1415 /* --------------------------------------------------------------------- */