1 /*****************************************************************************/
2 
3 /*
4  *	baycom_par.c  -- baycom par96 and picpar radio modem driver.
5  *
6  *	Copyright (C) 1996-2000  Thomas Sailer (sailer@ife.ee.ethz.ch)
7  *
8  *	This program is free software; you can redistribute it and/or modify
9  *	it under the terms of the GNU General Public License as published by
10  *	the Free Software Foundation; either version 2 of the License, or
11  *	(at your option) any later version.
12  *
13  *	This program is distributed in the hope that it will be useful,
14  *	but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *	GNU General Public License for more details.
17  *
18  *	You should have received a copy of the GNU General Public License
19  *	along with this program; if not, write to the Free Software
20  *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  *  Please note that the GPL allows you to use the driver, NOT the radio.
23  *  In order to use the radio, you need a license from the communications
24  *  authority of your country.
25  *
26  *
27  *  Supported modems
28  *
29  *  par96:  This is a modem for 9600 baud FSK compatible to the G3RUH standard.
30  *          The modem does all the filtering and regenerates the receiver clock.
31  *          Data is transferred from and to the PC via a shift register.
32  *          The shift register is filled with 16 bits and an interrupt is
33  *          signalled. The PC then empties the shift register in a burst. This
34  *          modem connects to the parallel port, hence the name. The modem
35  *          leaves the implementation of the HDLC protocol and the scrambler
36  *          polynomial to the PC. This modem is no longer available (at least
37  *          from Baycom) and has been replaced by the PICPAR modem (see below).
38  *          You may however still build one from the schematics published in
39  *          cq-DL :-).
40  *
41  *  picpar: This is a redesign of the par96 modem by Henning Rech, DF9IC. The
42  *          modem is protocol compatible to par96, but uses only three low
43  *          power ICs and can therefore be fed from the parallel port and
44  *          does not require an additional power supply. It features
45  *          built in DCD circuitry. The driver should therefore be configured
46  *          for hardware DCD.
47  *
48  *
49  *  Command line options (insmod command line)
50  *
51  *  mode     driver mode string. Valid choices are par96 and picpar.
52  *  iobase   base address of the port; common values are 0x378, 0x278, 0x3bc
53  *
54  *
55  *  History:
56  *   0.1  26.06.1996  Adapted from baycom.c and made network driver interface
57  *        18.10.1996  Changed to new user space access routines (copy_{to,from}_user)
58  *   0.3  26.04.1997  init code/data tagged
59  *   0.4  08.07.1997  alternative ser12 decoding algorithm (uses delta CTS ints)
60  *   0.5  11.11.1997  split into separate files for ser12/par96
61  *   0.6  03.08.1999  adapt to Linus' new __setup/__initcall
62  *                    removed some pre-2.2 kernel compatibility cruft
63  *   0.7  10.08.1999  Check if parport can do SPP and is safe to access during interrupt contexts
64  *   0.8  12.02.2000  adapted to softnet driver interface
65  *                    removed direct parport access, uses parport driver methods
66  *   0.9  03.07.2000  fix interface name handling
67  */
68 
69 /*****************************************************************************/
70 
71 #include <linux/version.h>
72 #include <linux/module.h>
73 #include <linux/kernel.h>
74 #include <linux/sched.h>
75 #include <linux/types.h>
76 #include <linux/fcntl.h>
77 #include <linux/interrupt.h>
78 #include <linux/ioport.h>
79 #include <linux/in.h>
80 #include <linux/string.h>
81 #include <asm/system.h>
82 #include <asm/bitops.h>
83 #include <asm/uaccess.h>
84 #include <linux/init.h>
85 #include <linux/delay.h>
86 #include <linux/errno.h>
87 #include <linux/netdevice.h>
88 #include <linux/hdlcdrv.h>
89 #include <linux/baycom.h>
90 #include <linux/parport.h>
91 
92 /* --------------------------------------------------------------------- */
93 
94 #define BAYCOM_DEBUG
95 
96 /*
97  * modem options; bit mask
98  */
99 #define BAYCOM_OPTIONS_SOFTDCD  1
100 
101 /* --------------------------------------------------------------------- */
102 
103 static const char bc_drvname[] = "baycom_par";
104 static const char bc_drvinfo[] = KERN_INFO "baycom_par: (C) 1996-2000 Thomas Sailer, HB9JNX/AE4WA\n"
105 KERN_INFO "baycom_par: version 0.9 compiled " __TIME__ " " __DATE__ "\n";
106 
107 /* --------------------------------------------------------------------- */
108 
109 #define NR_PORTS 4
110 
111 static struct net_device baycom_device[NR_PORTS];
112 
113 /* --------------------------------------------------------------------- */
114 
115 #define PAR96_BURSTBITS 16
116 #define PAR96_BURST     4
117 #define PAR96_PTT       2
118 #define PAR96_TXBIT     1
119 #define PAR96_ACK       0x40
120 #define PAR96_RXBIT     0x20
121 #define PAR96_DCD       0x10
122 #define PAR97_POWER     0xf8
123 
124 /* ---------------------------------------------------------------------- */
125 /*
126  * Information that need to be kept for each board.
127  */
128 
129 struct baycom_state {
130 	struct hdlcdrv_state hdrv;
131 
132 	struct pardevice *pdev;
133 	unsigned int options;
134 
135 	struct modem_state {
136 		short arb_divider;
137 		unsigned char flags;
138 		unsigned int shreg;
139 		struct modem_state_par96 {
140 			int dcd_count;
141 			unsigned int dcd_shreg;
142 			unsigned long descram;
143 			unsigned long scram;
144 		} par96;
145 	} modem;
146 
147 #ifdef BAYCOM_DEBUG
148 	struct debug_vals {
149 		unsigned long last_jiffies;
150 		unsigned cur_intcnt;
151 		unsigned last_intcnt;
152 		int cur_pllcorr;
153 		int last_pllcorr;
154 	} debug_vals;
155 #endif /* BAYCOM_DEBUG */
156 };
157 
158 /* --------------------------------------------------------------------- */
159 
baycom_int_freq(struct baycom_state * bc)160 static void __inline__ baycom_int_freq(struct baycom_state *bc)
161 {
162 #ifdef BAYCOM_DEBUG
163 	unsigned long cur_jiffies = jiffies;
164 	/*
165 	 * measure the interrupt frequency
166 	 */
167 	bc->debug_vals.cur_intcnt++;
168 	if ((cur_jiffies - bc->debug_vals.last_jiffies) >= HZ) {
169 		bc->debug_vals.last_jiffies = cur_jiffies;
170 		bc->debug_vals.last_intcnt = bc->debug_vals.cur_intcnt;
171 		bc->debug_vals.cur_intcnt = 0;
172 		bc->debug_vals.last_pllcorr = bc->debug_vals.cur_pllcorr;
173 		bc->debug_vals.cur_pllcorr = 0;
174 	}
175 #endif /* BAYCOM_DEBUG */
176 }
177 
178 /* --------------------------------------------------------------------- */
179 /*
180  * ===================== PAR96 specific routines =========================
181  */
182 
183 #define PAR96_DESCRAM_TAP1 0x20000
184 #define PAR96_DESCRAM_TAP2 0x01000
185 #define PAR96_DESCRAM_TAP3 0x00001
186 
187 #define PAR96_DESCRAM_TAPSH1 17
188 #define PAR96_DESCRAM_TAPSH2 12
189 #define PAR96_DESCRAM_TAPSH3 0
190 
191 #define PAR96_SCRAM_TAP1 0x20000 /* X^17 */
192 #define PAR96_SCRAM_TAPN 0x00021 /* X^0+X^5 */
193 
194 /* --------------------------------------------------------------------- */
195 
par96_tx(struct net_device * dev,struct baycom_state * bc)196 static __inline__ void par96_tx(struct net_device *dev, struct baycom_state *bc)
197 {
198 	int i;
199 	unsigned int data = hdlcdrv_getbits(&bc->hdrv);
200 	struct parport *pp = bc->pdev->port;
201 
202 	for(i = 0; i < PAR96_BURSTBITS; i++, data >>= 1) {
203 		unsigned char val = PAR97_POWER;
204 		bc->modem.par96.scram = ((bc->modem.par96.scram << 1) |
205 					 (bc->modem.par96.scram & 1));
206 		if (!(data & 1))
207 			bc->modem.par96.scram ^= 1;
208 		if (bc->modem.par96.scram & (PAR96_SCRAM_TAP1 << 1))
209 			bc->modem.par96.scram ^=
210 				(PAR96_SCRAM_TAPN << 1);
211 		if (bc->modem.par96.scram & (PAR96_SCRAM_TAP1 << 2))
212 			val |= PAR96_TXBIT;
213 		pp->ops->write_data(pp, val);
214 		pp->ops->write_data(pp, val | PAR96_BURST);
215 	}
216 }
217 
218 /* --------------------------------------------------------------------- */
219 
par96_rx(struct net_device * dev,struct baycom_state * bc)220 static __inline__ void par96_rx(struct net_device *dev, struct baycom_state *bc)
221 {
222 	int i;
223 	unsigned int data, mask, mask2, descx;
224 	struct parport *pp = bc->pdev->port;
225 
226 	/*
227 	 * do receiver; differential decode and descramble on the fly
228 	 */
229 	for(data = i = 0; i < PAR96_BURSTBITS; i++) {
230 		bc->modem.par96.descram = (bc->modem.par96.descram << 1);
231 		if (pp->ops->read_status(pp) & PAR96_RXBIT)
232 			bc->modem.par96.descram |= 1;
233 		descx = bc->modem.par96.descram ^
234 			(bc->modem.par96.descram >> 1);
235 		/* now the diff decoded data is inverted in descram */
236 		pp->ops->write_data(pp, PAR97_POWER | PAR96_PTT);
237 		descx ^= ((descx >> PAR96_DESCRAM_TAPSH1) ^
238 			  (descx >> PAR96_DESCRAM_TAPSH2));
239 		data >>= 1;
240 		if (!(descx & 1))
241 			data |= 0x8000;
242 		pp->ops->write_data(pp, PAR97_POWER | PAR96_PTT | PAR96_BURST);
243 	}
244 	hdlcdrv_putbits(&bc->hdrv, data);
245 	/*
246 	 * do DCD algorithm
247 	 */
248 	if (bc->options & BAYCOM_OPTIONS_SOFTDCD) {
249 		bc->modem.par96.dcd_shreg = (bc->modem.par96.dcd_shreg >> 16)
250 			| (data << 16);
251 		/* search for flags and set the dcd counter appropriately */
252 		for(mask = 0x1fe00, mask2 = 0xfc00, i = 0;
253 		    i < PAR96_BURSTBITS; i++, mask <<= 1, mask2 <<= 1)
254 			if ((bc->modem.par96.dcd_shreg & mask) == mask2)
255 				bc->modem.par96.dcd_count = HDLCDRV_MAXFLEN+4;
256 		/* check for abort/noise sequences */
257 		for(mask = 0x1fe00, mask2 = 0x1fe00, i = 0;
258 		    i < PAR96_BURSTBITS; i++, mask <<= 1, mask2 <<= 1)
259 			if (((bc->modem.par96.dcd_shreg & mask) == mask2) &&
260 			    (bc->modem.par96.dcd_count >= 0))
261 				bc->modem.par96.dcd_count -= HDLCDRV_MAXFLEN-10;
262 		/* decrement and set the dcd variable */
263 		if (bc->modem.par96.dcd_count >= 0)
264 			bc->modem.par96.dcd_count -= 2;
265 		hdlcdrv_setdcd(&bc->hdrv, bc->modem.par96.dcd_count > 0);
266 	} else {
267 		hdlcdrv_setdcd(&bc->hdrv, !!(pp->ops->read_status(pp) & PAR96_DCD));
268 	}
269 }
270 
271 /* --------------------------------------------------------------------- */
272 
par96_interrupt(int irq,void * dev_id,struct pt_regs * regs)273 static void par96_interrupt(int irq, void *dev_id, struct pt_regs *regs)
274 {
275 	struct net_device *dev = (struct net_device *)dev_id;
276 	struct baycom_state *bc = (struct baycom_state *)dev->priv;
277 
278 	if (!dev || !bc || bc->hdrv.magic != HDLCDRV_MAGIC)
279 		return;
280 
281 	baycom_int_freq(bc);
282 	/*
283 	 * check if transmitter active
284 	 */
285 	if (hdlcdrv_ptt(&bc->hdrv))
286 		par96_tx(dev, bc);
287 	else {
288 		par96_rx(dev, bc);
289 		if (--bc->modem.arb_divider <= 0) {
290 			bc->modem.arb_divider = 6;
291 			__sti();
292 			hdlcdrv_arbitrate(dev, &bc->hdrv);
293 		}
294 	}
295 	__sti();
296 	hdlcdrv_transmitter(dev, &bc->hdrv);
297 	hdlcdrv_receiver(dev, &bc->hdrv);
298         __cli();
299 }
300 
301 /* --------------------------------------------------------------------- */
302 
par96_wakeup(void * handle)303 static void par96_wakeup(void *handle)
304 {
305         struct net_device *dev = (struct net_device *)handle;
306 	struct baycom_state *bc = (struct baycom_state *)dev->priv;
307 
308 	printk(KERN_DEBUG "baycom_par: %s: why am I being woken up?\n", dev->name);
309 	if (!parport_claim(bc->pdev))
310 		printk(KERN_DEBUG "baycom_par: %s: I'm broken.\n", dev->name);
311 }
312 
313 /* --------------------------------------------------------------------- */
314 
par96_open(struct net_device * dev)315 static int par96_open(struct net_device *dev)
316 {
317 	struct baycom_state *bc = (struct baycom_state *)dev->priv;
318 	struct parport *pp = parport_enumerate();
319 
320 	if (!dev || !bc)
321 		return -ENXIO;
322 	while (pp && pp->base != dev->base_addr)
323 		pp = pp->next;
324 	if (!pp) {
325 		printk(KERN_ERR "baycom_par: parport at 0x%lx unknown\n", dev->base_addr);
326 		return -ENXIO;
327 	}
328 	if (pp->irq < 0) {
329 		printk(KERN_ERR "baycom_par: parport at 0x%lx has no irq\n", pp->base);
330 		return -ENXIO;
331 	}
332 	if ((~pp->modes) & (PARPORT_MODE_PCSPP | PARPORT_MODE_SAFEININT)) {
333 		printk(KERN_ERR "baycom_par: parport at 0x%lx cannot be used\n", pp->base);
334 		return -ENXIO;
335 	}
336 	memset(&bc->modem, 0, sizeof(bc->modem));
337 	bc->hdrv.par.bitrate = 9600;
338 	if (!(bc->pdev = parport_register_device(pp, dev->name, NULL, par96_wakeup,
339 						 par96_interrupt, PARPORT_DEV_EXCL, dev))) {
340 		printk(KERN_ERR "baycom_par: cannot register parport at 0x%lx\n", pp->base);
341 		return -ENXIO;
342 	}
343 	if (parport_claim(bc->pdev)) {
344 		printk(KERN_ERR "baycom_par: parport at 0x%lx busy\n", pp->base);
345 		parport_unregister_device(bc->pdev);
346 		return -EBUSY;
347 	}
348 	pp = bc->pdev->port;
349 	dev->irq = pp->irq;
350 	pp->ops->data_forward(pp);
351         bc->hdrv.par.bitrate = 9600;
352 	pp->ops->write_data(pp, PAR96_PTT | PAR97_POWER); /* switch off PTT */
353 	pp->ops->enable_irq(pp);
354 	printk(KERN_INFO "%s: par96 at iobase 0x%lx irq %u options 0x%x\n",
355 	       bc_drvname, dev->base_addr, dev->irq, bc->options);
356 	MOD_INC_USE_COUNT;
357 	return 0;
358 }
359 
360 /* --------------------------------------------------------------------- */
361 
par96_close(struct net_device * dev)362 static int par96_close(struct net_device *dev)
363 {
364 	struct baycom_state *bc = (struct baycom_state *)dev->priv;
365 	struct parport *pp;
366 
367 	if (!dev || !bc)
368 		return -EINVAL;
369 	pp = bc->pdev->port;
370 	/* disable interrupt */
371 	pp->ops->disable_irq(pp);
372 	/* switch off PTT */
373 	pp->ops->write_data(pp, PAR96_PTT | PAR97_POWER);
374 	parport_release(bc->pdev);
375 	parport_unregister_device(bc->pdev);
376 	printk(KERN_INFO "%s: close par96 at iobase 0x%lx irq %u\n",
377 	       bc_drvname, dev->base_addr, dev->irq);
378 	MOD_DEC_USE_COUNT;
379 	return 0;
380 }
381 
382 /* --------------------------------------------------------------------- */
383 /*
384  * ===================== hdlcdrv driver interface =========================
385  */
386 
387 static int baycom_ioctl(struct net_device *dev, struct ifreq *ifr,
388 			struct hdlcdrv_ioctl *hi, int cmd);
389 
390 /* --------------------------------------------------------------------- */
391 
392 static struct hdlcdrv_ops par96_ops = {
393 	bc_drvname,
394 	bc_drvinfo,
395 	par96_open,
396 	par96_close,
397 	baycom_ioctl
398 };
399 
400 /* --------------------------------------------------------------------- */
401 
baycom_setmode(struct baycom_state * bc,const char * modestr)402 static int baycom_setmode(struct baycom_state *bc, const char *modestr)
403 {
404 	if (!strncmp(modestr, "picpar", 6))
405 		bc->options = 0;
406 	else if (!strncmp(modestr, "par96", 5))
407 		bc->options = BAYCOM_OPTIONS_SOFTDCD;
408 	else
409 		bc->options = !!strchr(modestr, '*');
410 	return 0;
411 }
412 
413 /* --------------------------------------------------------------------- */
414 
baycom_ioctl(struct net_device * dev,struct ifreq * ifr,struct hdlcdrv_ioctl * hi,int cmd)415 static int baycom_ioctl(struct net_device *dev, struct ifreq *ifr,
416 			struct hdlcdrv_ioctl *hi, int cmd)
417 {
418 	struct baycom_state *bc;
419 	struct baycom_ioctl bi;
420 	int cmd2;
421 
422 	if (!dev || !dev->priv ||
423 	    ((struct baycom_state *)dev->priv)->hdrv.magic != HDLCDRV_MAGIC) {
424 		printk(KERN_ERR "bc_ioctl: invalid device struct\n");
425 		return -EINVAL;
426 	}
427 	bc = (struct baycom_state *)dev->priv;
428 
429 	if (cmd != SIOCDEVPRIVATE)
430 		return -ENOIOCTLCMD;
431 	if (get_user(cmd2, (int *)ifr->ifr_data))
432 		return -EFAULT;
433 	switch (hi->cmd) {
434 	default:
435 		break;
436 
437 	case HDLCDRVCTL_GETMODE:
438 		strcpy(hi->data.modename, bc->options ? "par96" : "picpar");
439 		if (copy_to_user(ifr->ifr_data, hi, sizeof(struct hdlcdrv_ioctl)))
440 			return -EFAULT;
441 		return 0;
442 
443 	case HDLCDRVCTL_SETMODE:
444 		if (netif_running(dev) || !capable(CAP_NET_ADMIN))
445 			return -EACCES;
446 		hi->data.modename[sizeof(hi->data.modename)-1] = '\0';
447 		return baycom_setmode(bc, hi->data.modename);
448 
449 	case HDLCDRVCTL_MODELIST:
450 		strcpy(hi->data.modename, "par96,picpar");
451 		if (copy_to_user(ifr->ifr_data, hi, sizeof(struct hdlcdrv_ioctl)))
452 			return -EFAULT;
453 		return 0;
454 
455 	case HDLCDRVCTL_MODEMPARMASK:
456 		return HDLCDRV_PARMASK_IOBASE;
457 
458 	}
459 
460 	if (copy_from_user(&bi, ifr->ifr_data, sizeof(bi)))
461 		return -EFAULT;
462 	switch (bi.cmd) {
463 	default:
464 		return -ENOIOCTLCMD;
465 
466 #ifdef BAYCOM_DEBUG
467 	case BAYCOMCTL_GETDEBUG:
468 		bi.data.dbg.debug1 = bc->hdrv.ptt_keyed;
469 		bi.data.dbg.debug2 = bc->debug_vals.last_intcnt;
470 		bi.data.dbg.debug3 = bc->debug_vals.last_pllcorr;
471 		break;
472 #endif /* BAYCOM_DEBUG */
473 
474 	}
475 	if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
476 		return -EFAULT;
477 	return 0;
478 
479 }
480 
481 /* --------------------------------------------------------------------- */
482 
483 /*
484  * command line settable parameters
485  */
486 static const char *mode[NR_PORTS] = { "picpar", };
487 static int iobase[NR_PORTS] = { 0x378, };
488 
489 MODULE_PARM(mode, "1-" __MODULE_STRING(NR_PORTS) "s");
490 MODULE_PARM_DESC(mode, "baycom operating mode; eg. par96 or picpar");
491 MODULE_PARM(iobase, "1-" __MODULE_STRING(NR_PORTS) "i");
492 MODULE_PARM_DESC(iobase, "baycom io base address");
493 
494 MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
495 MODULE_DESCRIPTION("Baycom par96 and picpar amateur radio modem driver");
496 MODULE_LICENSE("GPL");
497 
498 /* --------------------------------------------------------------------- */
499 
init_baycompar(void)500 static int __init init_baycompar(void)
501 {
502 	int i, j, found = 0;
503 	char set_hw = 1;
504 	struct baycom_state *bc;
505 
506 	printk(bc_drvinfo);
507 	/*
508 	 * register net devices
509 	 */
510 	for (i = 0; i < NR_PORTS; i++) {
511 		struct net_device *dev = baycom_device+i;
512 		char ifname[IFNAMSIZ];
513 
514 		sprintf(ifname, "bcp%d", i);
515 		if (!mode[i])
516 			set_hw = 0;
517 		if (!set_hw)
518 			iobase[i] = 0;
519 		j = hdlcdrv_register_hdlcdrv(dev, &par96_ops, sizeof(struct baycom_state),
520 					     ifname, iobase[i], 0, 0);
521 		if (!j) {
522 			bc = (struct baycom_state *)dev->priv;
523 			if (set_hw && baycom_setmode(bc, mode[i]))
524 				set_hw = 0;
525 			found++;
526 		} else {
527 			printk(KERN_WARNING "%s: cannot register net device\n",
528 			       bc_drvname);
529 		}
530 	}
531 	if (!found)
532 		return -ENXIO;
533 	return 0;
534 }
535 
cleanup_baycompar(void)536 static void __exit cleanup_baycompar(void)
537 {
538 	int i;
539 
540 	for(i = 0; i < NR_PORTS; i++) {
541 		struct net_device *dev = baycom_device+i;
542 		struct baycom_state *bc = (struct baycom_state *)dev->priv;
543 
544 		if (bc) {
545 			if (bc->hdrv.magic != HDLCDRV_MAGIC)
546 				printk(KERN_ERR "baycom: invalid magic in "
547 				       "cleanup_module\n");
548 			else
549 				hdlcdrv_unregister_hdlcdrv(dev);
550 		}
551 	}
552 }
553 
554 module_init(init_baycompar);
555 module_exit(cleanup_baycompar);
556 
557 /* --------------------------------------------------------------------- */
558 
559 #ifndef MODULE
560 
561 /*
562  * format: baycom_par=io,mode
563  * mode: par96,picpar
564  */
565 
baycom_par_setup(char * str)566 static int __init baycom_par_setup(char *str)
567 {
568         static unsigned nr_dev;
569 	int ints[2];
570 
571         if (nr_dev >= NR_PORTS)
572                 return 0;
573         str = get_options(str, 2, ints);
574         if (ints[0] < 1)
575                 return 0;
576         mode[nr_dev] = str;
577         iobase[nr_dev] = ints[1];
578 	nr_dev++;
579 	return 1;
580 }
581 
582 __setup("baycom_par=", baycom_par_setup);
583 
584 #endif /* MODULE */
585 /* --------------------------------------------------------------------- */
586