Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

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