1 /*****************************************************************************/
4 * baycom_par.c -- baycom par96 and picpar radio modem driver.
6 * Copyright (C) 1997 Thomas Sailer (sailer@ife.ee.ethz.ch)
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.
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.
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.
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.
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
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
49 * Command line options (insmod command line)
51 * mode driver mode string. Valid choices are par96 and picpar.
52 * iobase base address of the port; common values are 0x378, 0x278, 0x3bc
56 * 0.1 26.06.96 Adapted from baycom.c and made network driver interface
57 * 18.10.96 Changed to new user space access routines (copy_{to,from}_user)
58 * 0.3 26.04.97 init code/data tagged
59 * 0.4 08.07.97 alternative ser12 decoding algorithm (uses delta CTS ints)
60 * 0.5 11.11.97 split into separate files for ser12/par96
63 /*****************************************************************************/
65 #include <linux/module.h>
66 #include <linux/kernel.h>
67 #include <linux/sched.h>
68 #include <linux/types.h>
69 #include <linux/fcntl.h>
70 #include <linux/interrupt.h>
71 #include <linux/ioport.h>
73 #include <linux/string.h>
74 #include <asm/system.h>
75 #include <asm/bitops.h>
77 #include <linux/delay.h>
78 #include <linux/errno.h>
79 #include <linux/netdevice.h>
80 #include <linux/hdlcdrv.h>
81 #include <linux/baycom.h>
82 #include <linux/parport.h>
84 /* --------------------------------------------------------------------- */
87 * currently this module is supposed to support both module styles, i.e.
88 * the old one present up to about 2.1.9, and the new one functioning
89 * starting with 2.1.21. The reason is I have a kit allowing to compile
90 * this module also under 2.0.x which was requested by several people.
93 #include <linux/version.h>
95 #if LINUX_VERSION_CODE >= 0x20100
96 #include <asm/uaccess.h>
98 #include <asm/segment.h>
104 #define put_user(x,ptr) ({ __put_user((unsigned long)(x),(ptr),sizeof(*(ptr))); 0; })
105 #define get_user(x,ptr) ({ x = ((__typeof__(*(ptr)))__get_user((ptr),sizeof(*(ptr)))); 0; })
107 extern __inline__
int copy_from_user(void *to
, const void *from
, unsigned long n
)
109 int i
= verify_area(VERIFY_READ
, from
, n
);
112 memcpy_fromfs(to
, from
, n
);
116 extern __inline__
int copy_to_user(void *to
, const void *from
, unsigned long n
)
118 int i
= verify_area(VERIFY_WRITE
, to
, n
);
121 memcpy_tofs(to
, from
, n
);
126 #if LINUX_VERSION_CODE >= 0x20123
127 #include <linux/init.h>
131 #define __initfunc(x) x
134 /* --------------------------------------------------------------------- */
139 * modem options; bit mask
141 #define BAYCOM_OPTIONS_SOFTDCD 1
143 /* --------------------------------------------------------------------- */
145 static const char bc_drvname
[] = "baycom_par";
146 static const char bc_drvinfo
[] = KERN_INFO
"baycom_par: (C) 1997 Thomas Sailer, HB9JNX/AE4WA\n"
147 KERN_INFO
"baycom_par: version 0.5 compiled " __TIME__
" " __DATE__
"\n";
149 /* --------------------------------------------------------------------- */
153 static struct device baycom_device
[NR_PORTS
];
158 } baycom_ports
[NR_PORTS
] = { { NULL
, 0 }, };
160 /* --------------------------------------------------------------------- */
162 #define SER12_EXTENT 8
164 #define LPT_DATA(dev) ((dev)->base_addr+0)
165 #define LPT_STATUS(dev) ((dev)->base_addr+1)
166 #define LPT_CONTROL(dev) ((dev)->base_addr+2)
167 #define LPT_IRQ_ENABLE 0x10
169 #define PAR96_BURSTBITS 16
170 #define PAR96_BURST 4
172 #define PAR96_TXBIT 1
173 #define PAR96_ACK 0x40
174 #define PAR96_RXBIT 0x20
175 #define PAR96_DCD 0x10
176 #define PAR97_POWER 0xf8
178 /* ---------------------------------------------------------------------- */
180 * Information that need to be kept for each board.
183 struct baycom_state
{
184 struct hdlcdrv_state hdrv
;
186 struct pardevice
*pdev
;
187 unsigned int options
;
193 struct modem_state_par96
{
195 unsigned int dcd_shreg
;
196 unsigned long descram
;
203 unsigned long last_jiffies
;
205 unsigned last_intcnt
;
209 #endif /* BAYCOM_DEBUG */
212 /* --------------------------------------------------------------------- */
214 #define min(a, b) (((a) < (b)) ? (a) : (b))
215 #define max(a, b) (((a) > (b)) ? (a) : (b))
217 /* --------------------------------------------------------------------- */
219 static void __inline__
baycom_int_freq(struct baycom_state
*bc
)
222 unsigned long cur_jiffies
= jiffies
;
224 * measure the interrupt frequency
226 bc
->debug_vals
.cur_intcnt
++;
227 if ((cur_jiffies
- bc
->debug_vals
.last_jiffies
) >= HZ
) {
228 bc
->debug_vals
.last_jiffies
= cur_jiffies
;
229 bc
->debug_vals
.last_intcnt
= bc
->debug_vals
.cur_intcnt
;
230 bc
->debug_vals
.cur_intcnt
= 0;
231 bc
->debug_vals
.last_pllcorr
= bc
->debug_vals
.cur_pllcorr
;
232 bc
->debug_vals
.cur_pllcorr
= 0;
234 #endif /* BAYCOM_DEBUG */
237 /* --------------------------------------------------------------------- */
239 * ===================== PAR96 specific routines =========================
242 #define PAR96_DESCRAM_TAP1 0x20000
243 #define PAR96_DESCRAM_TAP2 0x01000
244 #define PAR96_DESCRAM_TAP3 0x00001
246 #define PAR96_DESCRAM_TAPSH1 17
247 #define PAR96_DESCRAM_TAPSH2 12
248 #define PAR96_DESCRAM_TAPSH3 0
250 #define PAR96_SCRAM_TAP1 0x20000 /* X^17 */
251 #define PAR96_SCRAM_TAPN 0x00021 /* X^0+X^5 */
253 /* --------------------------------------------------------------------- */
255 static __inline__
void par96_tx(struct device
*dev
, struct baycom_state
*bc
)
258 unsigned int data
= hdlcdrv_getbits(&bc
->hdrv
);
260 for(i
= 0; i
< PAR96_BURSTBITS
; i
++, data
>>= 1) {
261 unsigned char val
= PAR97_POWER
;
262 bc
->modem
.par96
.scram
= ((bc
->modem
.par96
.scram
<< 1) |
263 (bc
->modem
.par96
.scram
& 1));
265 bc
->modem
.par96
.scram
^= 1;
266 if (bc
->modem
.par96
.scram
& (PAR96_SCRAM_TAP1
<< 1))
267 bc
->modem
.par96
.scram
^=
268 (PAR96_SCRAM_TAPN
<< 1);
269 if (bc
->modem
.par96
.scram
& (PAR96_SCRAM_TAP1
<< 2))
271 outb(val
, LPT_DATA(dev
));
272 outb(val
| PAR96_BURST
, LPT_DATA(dev
));
276 /* --------------------------------------------------------------------- */
278 static __inline__
void par96_rx(struct device
*dev
, struct baycom_state
*bc
)
281 unsigned int data
, mask
, mask2
, descx
;
284 * do receiver; differential decode and descramble on the fly
286 for(data
= i
= 0; i
< PAR96_BURSTBITS
; i
++) {
287 bc
->modem
.par96
.descram
= (bc
->modem
.par96
.descram
<< 1);
288 if (inb(LPT_STATUS(dev
)) & PAR96_RXBIT
)
289 bc
->modem
.par96
.descram
|= 1;
290 descx
= bc
->modem
.par96
.descram
^
291 (bc
->modem
.par96
.descram
>> 1);
292 /* now the diff decoded data is inverted in descram */
293 outb(PAR97_POWER
| PAR96_PTT
, LPT_DATA(dev
));
294 descx
^= ((descx
>> PAR96_DESCRAM_TAPSH1
) ^
295 (descx
>> PAR96_DESCRAM_TAPSH2
));
299 outb(PAR97_POWER
| PAR96_PTT
| PAR96_BURST
, LPT_DATA(dev
));
301 hdlcdrv_putbits(&bc
->hdrv
, data
);
305 if (bc
->options
& BAYCOM_OPTIONS_SOFTDCD
) {
306 bc
->modem
.par96
.dcd_shreg
= (bc
->modem
.par96
.dcd_shreg
>> 16)
308 /* search for flags and set the dcd counter appropriately */
309 for(mask
= 0x1fe00, mask2
= 0xfc00, i
= 0;
310 i
< PAR96_BURSTBITS
; i
++, mask
<<= 1, mask2
<<= 1)
311 if ((bc
->modem
.par96
.dcd_shreg
& mask
) == mask2
)
312 bc
->modem
.par96
.dcd_count
= HDLCDRV_MAXFLEN
+4;
313 /* check for abort/noise sequences */
314 for(mask
= 0x1fe00, mask2
= 0x1fe00, i
= 0;
315 i
< PAR96_BURSTBITS
; i
++, mask
<<= 1, mask2
<<= 1)
316 if (((bc
->modem
.par96
.dcd_shreg
& mask
) == mask2
) &&
317 (bc
->modem
.par96
.dcd_count
>= 0))
318 bc
->modem
.par96
.dcd_count
-= HDLCDRV_MAXFLEN
-10;
319 /* decrement and set the dcd variable */
320 if (bc
->modem
.par96
.dcd_count
>= 0)
321 bc
->modem
.par96
.dcd_count
-= 2;
322 hdlcdrv_setdcd(&bc
->hdrv
, bc
->modem
.par96
.dcd_count
> 0);
324 hdlcdrv_setdcd(&bc
->hdrv
, !!(inb(LPT_STATUS(dev
)) & PAR96_DCD
));
328 /* --------------------------------------------------------------------- */
330 static void par96_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
332 struct device
*dev
= (struct device
*)dev_id
;
333 struct baycom_state
*bc
= (struct baycom_state
*)dev
->priv
;
335 if (!dev
|| !bc
|| bc
->hdrv
.magic
!= HDLCDRV_MAGIC
)
340 * check if transmitter active
342 if (hdlcdrv_ptt(&bc
->hdrv
))
346 if (--bc
->modem
.arb_divider
<= 0) {
347 bc
->modem
.arb_divider
= 6;
349 hdlcdrv_arbitrate(dev
, &bc
->hdrv
);
353 hdlcdrv_transmitter(dev
, &bc
->hdrv
);
354 hdlcdrv_receiver(dev
, &bc
->hdrv
);
358 /* --------------------------------------------------------------------- */
360 static void par96_wakeup(void *handle
)
362 struct device
*dev
= (struct device
*)handle
;
363 struct baycom_state
*bc
= (struct baycom_state
*)dev
->priv
;
365 printk(KERN_DEBUG
"baycom_par: %s: why am I being woken up?\n", dev
->name
);
366 if (!parport_claim(bc
->pdev
))
367 printk(KERN_DEBUG
"baycom_par: %s: I'm broken.\n", dev
->name
);
370 /* --------------------------------------------------------------------- */
372 static int par96_open(struct device
*dev
)
374 struct baycom_state
*bc
= (struct baycom_state
*)dev
->priv
;
375 struct parport
*pp
= parport_enumerate();
379 while (pp
&& pp
->base
!= dev
->base_addr
)
382 printk(KERN_ERR
"baycom_par: parport at 0x%lx unknown\n", dev
->base_addr
);
386 printk(KERN_ERR
"baycom_par: parport at 0x%lx has no irq\n", pp
->base
);
389 memset(&bc
->modem
, 0, sizeof(bc
->modem
));
390 bc
->hdrv
.par
.bitrate
= 9600;
391 if (!(bc
->pdev
= parport_register_device(pp
, dev
->name
, NULL
, par96_wakeup
,
392 par96_interrupt
, PARPORT_DEV_EXCL
, dev
))) {
393 printk(KERN_ERR
"baycom_par: cannot register parport at 0x%lx\n", pp
->base
);
396 if (parport_claim(bc
->pdev
)) {
397 printk(KERN_ERR
"baycom_par: parport at 0x%lx busy\n", pp
->base
);
398 parport_unregister_device(bc
->pdev
);
402 /* bc->pdev->port->ops->change_mode(bc->pdev->port, PARPORT_MODE_PCSPP); not yet implemented */
403 bc
->hdrv
.par
.bitrate
= 9600;
405 outb(PAR96_PTT
| PAR97_POWER
, LPT_DATA(dev
));
406 /*bc->pdev->port->ops->enable_irq(bc->pdev->port); not yet implemented */
407 outb(LPT_IRQ_ENABLE
, LPT_CONTROL(dev
));
408 printk(KERN_INFO
"%s: par96 at iobase 0x%lx irq %u options 0x%x\n",
409 bc_drvname
, dev
->base_addr
, dev
->irq
, bc
->options
);
414 /* --------------------------------------------------------------------- */
416 static int par96_close(struct device
*dev
)
418 struct baycom_state
*bc
= (struct baycom_state
*)dev
->priv
;
422 /* disable interrupt */
423 outb(0, LPT_CONTROL(dev
));
424 /*bc->pdev->port->ops->disable_irq(bc->pdev->port); not yet implemented */
426 outb(PAR96_PTT
| PAR97_POWER
, LPT_DATA(dev
));
427 parport_release(bc
->pdev
);
428 parport_unregister_device(bc
->pdev
);
429 printk(KERN_INFO
"%s: close par96 at iobase 0x%lx irq %u\n",
430 bc_drvname
, dev
->base_addr
, dev
->irq
);
435 /* --------------------------------------------------------------------- */
437 * ===================== hdlcdrv driver interface =========================
440 static int baycom_ioctl(struct device
*dev
, struct ifreq
*ifr
,
441 struct hdlcdrv_ioctl
*hi
, int cmd
);
443 /* --------------------------------------------------------------------- */
445 static struct hdlcdrv_ops par96_ops
= {
453 /* --------------------------------------------------------------------- */
455 static int baycom_setmode(struct baycom_state
*bc
, const char *modestr
)
457 if (!strncmp(modestr
, "picpar", 6))
459 else if (!strncmp(modestr
, "par96", 5))
460 bc
->options
= BAYCOM_OPTIONS_SOFTDCD
;
462 bc
->options
= !!strchr(modestr
, '*');
466 /* --------------------------------------------------------------------- */
468 static int baycom_ioctl(struct device
*dev
, struct ifreq
*ifr
,
469 struct hdlcdrv_ioctl
*hi
, int cmd
)
471 struct baycom_state
*bc
;
472 struct baycom_ioctl bi
;
475 if (!dev
|| !dev
->priv
||
476 ((struct baycom_state
*)dev
->priv
)->hdrv
.magic
!= HDLCDRV_MAGIC
) {
477 printk(KERN_ERR
"bc_ioctl: invalid device struct\n");
480 bc
= (struct baycom_state
*)dev
->priv
;
482 if (cmd
!= SIOCDEVPRIVATE
)
484 if (get_user(cmd2
, (int *)ifr
->ifr_data
))
490 case HDLCDRVCTL_GETMODE
:
491 strcpy(hi
->data
.modename
, bc
->options
? "par96" : "picpar");
492 if (copy_to_user(ifr
->ifr_data
, hi
, sizeof(struct hdlcdrv_ioctl
)))
496 case HDLCDRVCTL_SETMODE
:
497 if (dev
->start
|| !suser())
499 hi
->data
.modename
[sizeof(hi
->data
.modename
)-1] = '\0';
500 return baycom_setmode(bc
, hi
->data
.modename
);
502 case HDLCDRVCTL_MODELIST
:
503 strcpy(hi
->data
.modename
, "par96,picpar");
504 if (copy_to_user(ifr
->ifr_data
, hi
, sizeof(struct hdlcdrv_ioctl
)))
508 case HDLCDRVCTL_MODEMPARMASK
:
509 return HDLCDRV_PARMASK_IOBASE
;
513 if (copy_from_user(&bi
, ifr
->ifr_data
, sizeof(bi
)))
520 case BAYCOMCTL_GETDEBUG
:
521 bi
.data
.dbg
.debug1
= bc
->hdrv
.ptt_keyed
;
522 bi
.data
.dbg
.debug2
= bc
->debug_vals
.last_intcnt
;
523 bi
.data
.dbg
.debug3
= bc
->debug_vals
.last_pllcorr
;
525 #endif /* BAYCOM_DEBUG */
528 if (copy_to_user(ifr
->ifr_data
, &bi
, sizeof(bi
)))
534 /* --------------------------------------------------------------------- */
536 int __init
baycom_par_init(void)
540 struct baycom_state
*bc
;
541 char ifname
[HDLCDRV_IFNAMELEN
];
546 * register net devices
548 for (i
= 0; i
< NR_PORTS
; i
++) {
549 struct device
*dev
= baycom_device
+i
;
550 sprintf(ifname
, "bcp%d", i
);
552 if (!baycom_ports
[i
].mode
)
555 baycom_ports
[i
].iobase
= 0;
556 j
= hdlcdrv_register_hdlcdrv(dev
, &par96_ops
,
557 sizeof(struct baycom_state
),
558 ifname
, baycom_ports
[i
].iobase
, 0, 0);
560 bc
= (struct baycom_state
*)dev
->priv
;
561 if (set_hw
&& baycom_setmode(bc
, baycom_ports
[i
].mode
))
565 printk(KERN_WARNING
"%s: cannot register net device\n",
574 /* --------------------------------------------------------------------- */
579 * command line settable parameters
581 static const char *mode
[NR_PORTS
] = { "picpar", };
582 static int iobase
[NR_PORTS
] = { 0x378, };
584 #if LINUX_VERSION_CODE >= 0x20115
586 MODULE_PARM(mode
, "1-" __MODULE_STRING(NR_PORTS
) "s");
587 MODULE_PARM_DESC(mode
, "baycom operating mode; eg. par96 or picpar");
588 MODULE_PARM(iobase
, "1-" __MODULE_STRING(NR_PORTS
) "i");
589 MODULE_PARM_DESC(iobase
, "baycom io base address");
591 MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
592 MODULE_DESCRIPTION("Baycom par96 and picpar amateur radio modem driver");
596 int __init
init_module(void)
600 for (i
= 0; (i
< NR_PORTS
) && (mode
[i
]); i
++) {
601 baycom_ports
[i
].mode
= mode
[i
];
602 baycom_ports
[i
].iobase
= iobase
[i
];
605 baycom_ports
[i
+1].mode
= NULL
;
606 return baycom_par_init();
609 /* --------------------------------------------------------------------- */
611 void cleanup_module(void)
615 for(i
= 0; i
< NR_PORTS
; i
++) {
616 struct device
*dev
= baycom_device
+i
;
617 struct baycom_state
*bc
= (struct baycom_state
*)dev
->priv
;
620 if (bc
->hdrv
.magic
!= HDLCDRV_MAGIC
)
621 printk(KERN_ERR
"baycom: invalid magic in "
624 hdlcdrv_unregister_hdlcdrv(dev
);
630 /* --------------------------------------------------------------------- */
632 * format: baycom_par=io,mode
636 void __init
baycom_par_setup(char *str
, int *ints
)
640 for (i
= 0; (i
< NR_PORTS
) && (baycom_ports
[i
].mode
); i
++);
641 if ((i
>= NR_PORTS
) || (ints
[0] < 1)) {
642 printk(KERN_INFO
"%s: too many or invalid interface "
643 "specifications\n", bc_drvname
);
646 baycom_ports
[i
].mode
= str
;
647 baycom_ports
[i
].iobase
= ints
[1];
649 baycom_ports
[i
+1].mode
= NULL
;
653 /* --------------------------------------------------------------------- */