1 /*****************************************************************************/
4 * baycom_par.c -- baycom par96 and picpar radio modem driver.
6 * Copyright (C) 1996-1999 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
61 * 0.6 03.08.99 adapt to Linus' new __setup/__initcall
62 * removed some pre-2.2 kernel compatibility cruft
63 * 0.7 10.08.99 Check if parport can do SPP and is safe to access during interrupt contexts
66 /*****************************************************************************/
68 #include <linux/version.h>
69 #include <linux/module.h>
70 #include <linux/kernel.h>
71 #include <linux/sched.h>
72 #include <linux/types.h>
73 #include <linux/fcntl.h>
74 #include <linux/interrupt.h>
75 #include <linux/ioport.h>
77 #include <linux/string.h>
78 #include <asm/system.h>
79 #include <asm/bitops.h>
81 #include <asm/uaccess.h>
82 #include <linux/init.h>
83 #include <linux/delay.h>
84 #include <linux/errno.h>
85 #include <linux/netdevice.h>
86 #include <linux/hdlcdrv.h>
87 #include <linux/baycom.h>
88 #include <linux/parport.h>
90 /* --------------------------------------------------------------------- */
95 * modem options; bit mask
97 #define BAYCOM_OPTIONS_SOFTDCD 1
99 /* --------------------------------------------------------------------- */
101 static const char bc_drvname
[] = "baycom_par";
102 static const char bc_drvinfo
[] = KERN_INFO
"baycom_par: (C) 1996-1999 Thomas Sailer, HB9JNX/AE4WA\n"
103 KERN_INFO
"baycom_par: version 0.6 compiled " __TIME__
" " __DATE__
"\n";
105 /* --------------------------------------------------------------------- */
109 static struct net_device baycom_device
[NR_PORTS
];
111 /* --------------------------------------------------------------------- */
113 #define SER12_EXTENT 8
115 #define LPT_DATA(dev) ((dev)->base_addr+0)
116 #define LPT_STATUS(dev) ((dev)->base_addr+1)
117 #define LPT_CONTROL(dev) ((dev)->base_addr+2)
118 #define LPT_IRQ_ENABLE 0x10
120 #define PAR96_BURSTBITS 16
121 #define PAR96_BURST 4
123 #define PAR96_TXBIT 1
124 #define PAR96_ACK 0x40
125 #define PAR96_RXBIT 0x20
126 #define PAR96_DCD 0x10
127 #define PAR97_POWER 0xf8
129 /* ---------------------------------------------------------------------- */
131 * Information that need to be kept for each board.
134 struct baycom_state
{
135 struct hdlcdrv_state hdrv
;
137 struct pardevice
*pdev
;
138 unsigned int options
;
144 struct modem_state_par96
{
146 unsigned int dcd_shreg
;
147 unsigned long descram
;
154 unsigned long last_jiffies
;
156 unsigned last_intcnt
;
160 #endif /* BAYCOM_DEBUG */
163 /* --------------------------------------------------------------------- */
165 #define min(a, b) (((a) < (b)) ? (a) : (b))
166 #define max(a, b) (((a) > (b)) ? (a) : (b))
168 /* --------------------------------------------------------------------- */
170 static void __inline__
baycom_int_freq(struct baycom_state
*bc
)
173 unsigned long cur_jiffies
= jiffies
;
175 * measure the interrupt frequency
177 bc
->debug_vals
.cur_intcnt
++;
178 if ((cur_jiffies
- bc
->debug_vals
.last_jiffies
) >= HZ
) {
179 bc
->debug_vals
.last_jiffies
= cur_jiffies
;
180 bc
->debug_vals
.last_intcnt
= bc
->debug_vals
.cur_intcnt
;
181 bc
->debug_vals
.cur_intcnt
= 0;
182 bc
->debug_vals
.last_pllcorr
= bc
->debug_vals
.cur_pllcorr
;
183 bc
->debug_vals
.cur_pllcorr
= 0;
185 #endif /* BAYCOM_DEBUG */
188 /* --------------------------------------------------------------------- */
190 * ===================== PAR96 specific routines =========================
193 #define PAR96_DESCRAM_TAP1 0x20000
194 #define PAR96_DESCRAM_TAP2 0x01000
195 #define PAR96_DESCRAM_TAP3 0x00001
197 #define PAR96_DESCRAM_TAPSH1 17
198 #define PAR96_DESCRAM_TAPSH2 12
199 #define PAR96_DESCRAM_TAPSH3 0
201 #define PAR96_SCRAM_TAP1 0x20000 /* X^17 */
202 #define PAR96_SCRAM_TAPN 0x00021 /* X^0+X^5 */
204 /* --------------------------------------------------------------------- */
206 static __inline__
void par96_tx(struct net_device
*dev
, struct baycom_state
*bc
)
209 unsigned int data
= hdlcdrv_getbits(&bc
->hdrv
);
211 for(i
= 0; i
< PAR96_BURSTBITS
; i
++, data
>>= 1) {
212 unsigned char val
= PAR97_POWER
;
213 bc
->modem
.par96
.scram
= ((bc
->modem
.par96
.scram
<< 1) |
214 (bc
->modem
.par96
.scram
& 1));
216 bc
->modem
.par96
.scram
^= 1;
217 if (bc
->modem
.par96
.scram
& (PAR96_SCRAM_TAP1
<< 1))
218 bc
->modem
.par96
.scram
^=
219 (PAR96_SCRAM_TAPN
<< 1);
220 if (bc
->modem
.par96
.scram
& (PAR96_SCRAM_TAP1
<< 2))
222 outb(val
, LPT_DATA(dev
));
223 outb(val
| PAR96_BURST
, LPT_DATA(dev
));
227 /* --------------------------------------------------------------------- */
229 static __inline__
void par96_rx(struct net_device
*dev
, struct baycom_state
*bc
)
232 unsigned int data
, mask
, mask2
, descx
;
235 * do receiver; differential decode and descramble on the fly
237 for(data
= i
= 0; i
< PAR96_BURSTBITS
; i
++) {
238 bc
->modem
.par96
.descram
= (bc
->modem
.par96
.descram
<< 1);
239 if (inb(LPT_STATUS(dev
)) & PAR96_RXBIT
)
240 bc
->modem
.par96
.descram
|= 1;
241 descx
= bc
->modem
.par96
.descram
^
242 (bc
->modem
.par96
.descram
>> 1);
243 /* now the diff decoded data is inverted in descram */
244 outb(PAR97_POWER
| PAR96_PTT
, LPT_DATA(dev
));
245 descx
^= ((descx
>> PAR96_DESCRAM_TAPSH1
) ^
246 (descx
>> PAR96_DESCRAM_TAPSH2
));
250 outb(PAR97_POWER
| PAR96_PTT
| PAR96_BURST
, LPT_DATA(dev
));
252 hdlcdrv_putbits(&bc
->hdrv
, data
);
256 if (bc
->options
& BAYCOM_OPTIONS_SOFTDCD
) {
257 bc
->modem
.par96
.dcd_shreg
= (bc
->modem
.par96
.dcd_shreg
>> 16)
259 /* search for flags and set the dcd counter appropriately */
260 for(mask
= 0x1fe00, mask2
= 0xfc00, i
= 0;
261 i
< PAR96_BURSTBITS
; i
++, mask
<<= 1, mask2
<<= 1)
262 if ((bc
->modem
.par96
.dcd_shreg
& mask
) == mask2
)
263 bc
->modem
.par96
.dcd_count
= HDLCDRV_MAXFLEN
+4;
264 /* check for abort/noise sequences */
265 for(mask
= 0x1fe00, mask2
= 0x1fe00, i
= 0;
266 i
< PAR96_BURSTBITS
; i
++, mask
<<= 1, mask2
<<= 1)
267 if (((bc
->modem
.par96
.dcd_shreg
& mask
) == mask2
) &&
268 (bc
->modem
.par96
.dcd_count
>= 0))
269 bc
->modem
.par96
.dcd_count
-= HDLCDRV_MAXFLEN
-10;
270 /* decrement and set the dcd variable */
271 if (bc
->modem
.par96
.dcd_count
>= 0)
272 bc
->modem
.par96
.dcd_count
-= 2;
273 hdlcdrv_setdcd(&bc
->hdrv
, bc
->modem
.par96
.dcd_count
> 0);
275 hdlcdrv_setdcd(&bc
->hdrv
, !!(inb(LPT_STATUS(dev
)) & PAR96_DCD
));
279 /* --------------------------------------------------------------------- */
281 static void par96_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
283 struct net_device
*dev
= (struct net_device
*)dev_id
;
284 struct baycom_state
*bc
= (struct baycom_state
*)dev
->priv
;
286 if (!dev
|| !bc
|| bc
->hdrv
.magic
!= HDLCDRV_MAGIC
)
291 * check if transmitter active
293 if (hdlcdrv_ptt(&bc
->hdrv
))
297 if (--bc
->modem
.arb_divider
<= 0) {
298 bc
->modem
.arb_divider
= 6;
300 hdlcdrv_arbitrate(dev
, &bc
->hdrv
);
304 hdlcdrv_transmitter(dev
, &bc
->hdrv
);
305 hdlcdrv_receiver(dev
, &bc
->hdrv
);
309 /* --------------------------------------------------------------------- */
311 static void par96_wakeup(void *handle
)
313 struct net_device
*dev
= (struct net_device
*)handle
;
314 struct baycom_state
*bc
= (struct baycom_state
*)dev
->priv
;
316 printk(KERN_DEBUG
"baycom_par: %s: why am I being woken up?\n", dev
->name
);
317 if (!parport_claim(bc
->pdev
))
318 printk(KERN_DEBUG
"baycom_par: %s: I'm broken.\n", dev
->name
);
321 /* --------------------------------------------------------------------- */
323 static int par96_open(struct net_device
*dev
)
325 struct baycom_state
*bc
= (struct baycom_state
*)dev
->priv
;
326 struct parport
*pp
= parport_enumerate();
330 while (pp
&& pp
->base
!= dev
->base_addr
)
333 printk(KERN_ERR
"baycom_par: parport at 0x%lx unknown\n", dev
->base_addr
);
337 printk(KERN_ERR
"baycom_par: parport at 0x%lx has no irq\n", pp
->base
);
340 if ((~pp
->modes
) & (PARPORT_MODE_PCSPP
| PARPORT_MODE_SAFEININT
)) {
341 printk(KERN_ERR
"baycom_par: parport at 0x%lx cannot be used\n", pp
->base
);
344 memset(&bc
->modem
, 0, sizeof(bc
->modem
));
345 bc
->hdrv
.par
.bitrate
= 9600;
346 if (!(bc
->pdev
= parport_register_device(pp
, dev
->name
, NULL
, par96_wakeup
,
347 par96_interrupt
, PARPORT_DEV_EXCL
, dev
))) {
348 printk(KERN_ERR
"baycom_par: cannot register parport at 0x%lx\n", pp
->base
);
351 if (parport_claim(bc
->pdev
)) {
352 printk(KERN_ERR
"baycom_par: parport at 0x%lx busy\n", pp
->base
);
353 parport_unregister_device(bc
->pdev
);
357 /* bc->pdev->port->ops->change_mode(bc->pdev->port, PARPORT_MODE_PCSPP); not yet implemented */
358 bc
->hdrv
.par
.bitrate
= 9600;
360 outb(PAR96_PTT
| PAR97_POWER
, LPT_DATA(dev
));
361 /*bc->pdev->port->ops->enable_irq(bc->pdev->port); not yet implemented */
362 outb(LPT_IRQ_ENABLE
, LPT_CONTROL(dev
));
363 printk(KERN_INFO
"%s: par96 at iobase 0x%lx irq %u options 0x%x\n",
364 bc_drvname
, dev
->base_addr
, dev
->irq
, bc
->options
);
369 /* --------------------------------------------------------------------- */
371 static int par96_close(struct net_device
*dev
)
373 struct baycom_state
*bc
= (struct baycom_state
*)dev
->priv
;
377 /* disable interrupt */
378 outb(0, LPT_CONTROL(dev
));
379 /*bc->pdev->port->ops->disable_irq(bc->pdev->port); not yet implemented */
381 outb(PAR96_PTT
| PAR97_POWER
, LPT_DATA(dev
));
382 parport_release(bc
->pdev
);
383 parport_unregister_device(bc
->pdev
);
384 printk(KERN_INFO
"%s: close par96 at iobase 0x%lx irq %u\n",
385 bc_drvname
, dev
->base_addr
, dev
->irq
);
390 /* --------------------------------------------------------------------- */
392 * ===================== hdlcdrv driver interface =========================
395 static int baycom_ioctl(struct net_device
*dev
, struct ifreq
*ifr
,
396 struct hdlcdrv_ioctl
*hi
, int cmd
);
398 /* --------------------------------------------------------------------- */
400 static struct hdlcdrv_ops par96_ops
= {
408 /* --------------------------------------------------------------------- */
410 static int baycom_setmode(struct baycom_state
*bc
, const char *modestr
)
412 if (!strncmp(modestr
, "picpar", 6))
414 else if (!strncmp(modestr
, "par96", 5))
415 bc
->options
= BAYCOM_OPTIONS_SOFTDCD
;
417 bc
->options
= !!strchr(modestr
, '*');
421 /* --------------------------------------------------------------------- */
423 static int baycom_ioctl(struct net_device
*dev
, struct ifreq
*ifr
,
424 struct hdlcdrv_ioctl
*hi
, int cmd
)
426 struct baycom_state
*bc
;
427 struct baycom_ioctl bi
;
430 if (!dev
|| !dev
->priv
||
431 ((struct baycom_state
*)dev
->priv
)->hdrv
.magic
!= HDLCDRV_MAGIC
) {
432 printk(KERN_ERR
"bc_ioctl: invalid device struct\n");
435 bc
= (struct baycom_state
*)dev
->priv
;
437 if (cmd
!= SIOCDEVPRIVATE
)
439 if (get_user(cmd2
, (int *)ifr
->ifr_data
))
445 case HDLCDRVCTL_GETMODE
:
446 strcpy(hi
->data
.modename
, bc
->options
? "par96" : "picpar");
447 if (copy_to_user(ifr
->ifr_data
, hi
, sizeof(struct hdlcdrv_ioctl
)))
451 case HDLCDRVCTL_SETMODE
:
452 if (dev
->start
|| !suser())
454 hi
->data
.modename
[sizeof(hi
->data
.modename
)-1] = '\0';
455 return baycom_setmode(bc
, hi
->data
.modename
);
457 case HDLCDRVCTL_MODELIST
:
458 strcpy(hi
->data
.modename
, "par96,picpar");
459 if (copy_to_user(ifr
->ifr_data
, hi
, sizeof(struct hdlcdrv_ioctl
)))
463 case HDLCDRVCTL_MODEMPARMASK
:
464 return HDLCDRV_PARMASK_IOBASE
;
468 if (copy_from_user(&bi
, ifr
->ifr_data
, sizeof(bi
)))
475 case BAYCOMCTL_GETDEBUG
:
476 bi
.data
.dbg
.debug1
= bc
->hdrv
.ptt_keyed
;
477 bi
.data
.dbg
.debug2
= bc
->debug_vals
.last_intcnt
;
478 bi
.data
.dbg
.debug3
= bc
->debug_vals
.last_pllcorr
;
480 #endif /* BAYCOM_DEBUG */
483 if (copy_to_user(ifr
->ifr_data
, &bi
, sizeof(bi
)))
489 /* --------------------------------------------------------------------- */
492 * command line settable parameters
494 static const char *mode
[NR_PORTS
] = { "picpar", };
495 static int iobase
[NR_PORTS
] = { 0x378, };
497 MODULE_PARM(mode
, "1-" __MODULE_STRING(NR_PORTS
) "s");
498 MODULE_PARM_DESC(mode
, "baycom operating mode; eg. par96 or picpar");
499 MODULE_PARM(iobase
, "1-" __MODULE_STRING(NR_PORTS
) "i");
500 MODULE_PARM_DESC(iobase
, "baycom io base address");
502 MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
503 MODULE_DESCRIPTION("Baycom par96 and picpar amateur radio modem driver");
505 /* --------------------------------------------------------------------- */
507 static int __init
init_baycompar(void)
511 struct baycom_state
*bc
;
512 char ifname
[HDLCDRV_IFNAMELEN
];
516 * register net devices
518 for (i
= 0; i
< NR_PORTS
; i
++) {
519 struct net_device
*dev
= baycom_device
+i
;
520 sprintf(ifname
, "bcp%d", i
);
526 j
= hdlcdrv_register_hdlcdrv(dev
, &par96_ops
, sizeof(struct baycom_state
),
527 ifname
, iobase
[i
], 0, 0);
529 bc
= (struct baycom_state
*)dev
->priv
;
530 if (set_hw
&& baycom_setmode(bc
, mode
[i
]))
534 printk(KERN_WARNING
"%s: cannot register net device\n",
543 static void __exit
cleanup_baycompar(void)
547 for(i
= 0; i
< NR_PORTS
; i
++) {
548 struct net_device
*dev
= baycom_device
+i
;
549 struct baycom_state
*bc
= (struct baycom_state
*)dev
->priv
;
552 if (bc
->hdrv
.magic
!= HDLCDRV_MAGIC
)
553 printk(KERN_ERR
"baycom: invalid magic in "
556 hdlcdrv_unregister_hdlcdrv(dev
);
561 module_init(init_baycompar
);
562 module_exit(cleanup_baycompar
);
564 /* --------------------------------------------------------------------- */
569 * format: baycom_par=io,mode
573 static int __init
baycom_par_setup(char *str
)
575 static unsigned __initdata nr_dev
= 0;
578 if (nr_dev
>= NR_PORTS
)
580 str
= get_options(str
, 2, ints
);
584 iobase
[nr_dev
] = ints
[1];
589 __setup("baycom_par=", baycom_par_setup
);
592 /* --------------------------------------------------------------------- */