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