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