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