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