1 /*****************************************************************************/
4 * baycom_ser_fdx.c -- baycom ser12 fullduplex radio modem driver.
6 * Copyright (C) 1996-2000 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 * ser12: This is a very simple 1200 baud AFSK modem. The modem consists only
30 * of a modulator/demodulator chip, usually a TI TCM3105. The computer
31 * is responsible for regenerating the receiver bit clock, as well as
32 * for handling the HDLC protocol. The modem connects to a serial port,
33 * hence the name. Since the serial port is not used as an async serial
34 * port, the kernel driver for serial ports cannot be used, and this
35 * driver only supports standard serial hardware (8250, 16450, 16550A)
37 * This modem usually draws its supply current out of the otherwise unused
38 * TXD pin of the serial port. Thus a contiguous stream of 0x00-bytes
39 * is transmitted to achieve a positive supply voltage.
41 * hsk: This is a 4800 baud FSK modem, designed for TNC use. It works fine
42 * in 'baycom-mode' :-) In contrast to the TCM3105 modem, power is
43 * externally supplied. So there's no need to provide the 0x00-byte-stream
44 * when receiving or idle, which drastically reduces interrupt load.
46 * Command line options (insmod command line)
48 * mode ser# hardware DCD
50 * ser#+ hardware DCD, inverted signal at DCD pin
51 * '#' denotes the baud rate / 100, eg. ser12* is '1200 baud, soft DCD'
52 * iobase base address of the port; common values are 0x3f8, 0x2f8, 0x3e8, 0x2e8
53 * baud baud rate (between 300 and 4800)
54 * irq interrupt line of the port; common values are 4,3
58 * 0.1 26.06.1996 Adapted from baycom.c and made network driver interface
59 * 18.10.1996 Changed to new user space access routines (copy_{to,from}_user)
60 * 0.3 26.04.1997 init code/data tagged
61 * 0.4 08.07.1997 alternative ser12 decoding algorithm (uses delta CTS ints)
62 * 0.5 11.11.1997 ser12/par96 split into separate files
63 * 0.6 24.01.1998 Thorsten Kranzkowski, dl8bcu and Thomas Sailer:
64 * reduced interrupt load in transmit case
66 * 0.7 03.08.1999 adapt to Linus' new __setup/__initcall
67 * 0.8 10.08.1999 use module_init/module_exit
68 * 0.9 12.02.2000 adapted to softnet driver interface
69 * 0.10 03.07.2000 fix interface name handling
72 /*****************************************************************************/
74 #include <linux/capability.h>
75 #include <linux/module.h>
76 #include <linux/ioport.h>
77 #include <linux/string.h>
78 #include <linux/init.h>
79 #include <linux/interrupt.h>
80 #include <linux/hdlcdrv.h>
81 #include <linux/baycom.h>
82 #include <linux/jiffies.h>
83 #include <linux/time64.h>
85 #include <linux/uaccess.h>
89 /* --------------------------------------------------------------------- */
93 /* --------------------------------------------------------------------- */
95 static const char bc_drvname
[] = "baycom_ser_fdx";
96 static const char bc_drvinfo
[] = KERN_INFO
"baycom_ser_fdx: (C) 1996-2000 Thomas Sailer, HB9JNX/AE4WA\n"
97 "baycom_ser_fdx: version 0.10\n";
99 /* --------------------------------------------------------------------- */
103 static struct net_device
*baycom_device
[NR_PORTS
];
105 /* --------------------------------------------------------------------- */
107 #define RBR(iobase) (iobase+0)
108 #define THR(iobase) (iobase+0)
109 #define IER(iobase) (iobase+1)
110 #define IIR(iobase) (iobase+2)
111 #define FCR(iobase) (iobase+2)
112 #define LCR(iobase) (iobase+3)
113 #define MCR(iobase) (iobase+4)
114 #define LSR(iobase) (iobase+5)
115 #define MSR(iobase) (iobase+6)
116 #define SCR(iobase) (iobase+7)
117 #define DLL(iobase) (iobase+0)
118 #define DLM(iobase) (iobase+1)
120 #define SER12_EXTENT 8
122 /* ---------------------------------------------------------------------- */
124 * Information that need to be kept for each board.
127 struct baycom_state
{
128 struct hdlcdrv_state hdrv
;
130 unsigned int baud
, baud_us
, baud_arbdiv
, baud_uartdiv
, baud_dcdtimeout
;
137 struct modem_state_ser12
{
138 unsigned char tx_bit
;
139 unsigned char last_rxbit
;
140 int dcd_sum0
, dcd_sum1
, dcd_sum2
;
142 unsigned int pll_time
;
143 unsigned int txshreg
;
149 unsigned long last_jiffies
;
151 unsigned last_intcnt
;
155 #endif /* BAYCOM_DEBUG */
158 /* --------------------------------------------------------------------- */
160 static inline void baycom_int_freq(struct baycom_state
*bc
)
163 unsigned long cur_jiffies
= jiffies
;
165 * measure the interrupt frequency
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;
175 #endif /* BAYCOM_DEBUG */
178 /* --------------------------------------------------------------------- */
180 * ===================== SER12 specific routines =========================
183 /* --------------------------------------------------------------------- */
185 static inline void ser12_set_divisor(struct net_device
*dev
,
186 unsigned int divisor
)
188 outb(0x81, LCR(dev
->base_addr
)); /* DLAB = 1 */
189 outb(divisor
, DLL(dev
->base_addr
));
190 outb(divisor
>> 8, DLM(dev
->base_addr
));
191 outb(0x01, LCR(dev
->base_addr
)); /* word length = 6 */
193 * make sure the next interrupt is generated;
194 * 0 must be used to power the modem; the modem draws its
195 * power from the TxD line
197 outb(0x00, THR(dev
->base_addr
));
199 * it is important not to set the divider while transmitting;
200 * this reportedly makes some UARTs generating interrupts
201 * in the hundredthousands per second region
202 * Reported by: Ignacio.Arenaza@studi.epfl.ch (Ignacio Arenaza Nuno)
206 /* --------------------------------------------------------------------- */
209 static inline unsigned int hweight16(unsigned int w
)
210 __attribute__ ((unused
));
211 static inline unsigned int hweight8(unsigned int w
)
212 __attribute__ ((unused
));
214 static inline unsigned int hweight16(unsigned int w
)
216 unsigned short res
= (w
& 0x5555) + ((w
>> 1) & 0x5555);
217 res
= (res
& 0x3333) + ((res
>> 2) & 0x3333);
218 res
= (res
& 0x0F0F) + ((res
>> 4) & 0x0F0F);
219 return (res
& 0x00FF) + ((res
>> 8) & 0x00FF);
222 static inline unsigned int hweight8(unsigned int w
)
224 unsigned short res
= (w
& 0x55) + ((w
>> 1) & 0x55);
225 res
= (res
& 0x33) + ((res
>> 2) & 0x33);
226 return (res
& 0x0F) + ((res
>> 4) & 0x0F);
230 /* --------------------------------------------------------------------- */
232 static __inline__
void ser12_rx(struct net_device
*dev
, struct baycom_state
*bc
, struct timespec64
*ts
, unsigned char curs
)
235 int bdus8
= bc
->baud_us
>> 3;
236 int bdus4
= bc
->baud_us
>> 2;
237 int bdus2
= bc
->baud_us
>> 1;
239 timediff
= 1000000 + ts
->tv_nsec
/ NSEC_PER_USEC
-
240 bc
->modem
.ser12
.pll_time
;
241 while (timediff
>= 500000)
243 while (timediff
>= bdus2
) {
244 timediff
-= bc
->baud_us
;
245 bc
->modem
.ser12
.pll_time
+= bc
->baud_us
;
246 bc
->modem
.ser12
.dcd_time
--;
247 /* first check if there is room to add a bit */
248 if (bc
->modem
.shreg
& 1) {
249 hdlcdrv_putbits(&bc
->hdrv
, (bc
->modem
.shreg
>> 1) ^ 0xffff);
250 bc
->modem
.shreg
= 0x10000;
253 bc
->modem
.shreg
>>= 1;
255 if (bc
->modem
.ser12
.dcd_time
<= 0) {
257 hdlcdrv_setdcd(&bc
->hdrv
, (bc
->modem
.ser12
.dcd_sum0
+
258 bc
->modem
.ser12
.dcd_sum1
+
259 bc
->modem
.ser12
.dcd_sum2
) < 0);
260 bc
->modem
.ser12
.dcd_sum2
= bc
->modem
.ser12
.dcd_sum1
;
261 bc
->modem
.ser12
.dcd_sum1
= bc
->modem
.ser12
.dcd_sum0
;
262 bc
->modem
.ser12
.dcd_sum0
= 2; /* slight bias */
263 bc
->modem
.ser12
.dcd_time
+= 120;
265 if (bc
->modem
.ser12
.last_rxbit
!= curs
) {
266 bc
->modem
.ser12
.last_rxbit
= curs
;
267 bc
->modem
.shreg
|= 0x10000;
270 bc
->modem
.ser12
.pll_time
+= bdus8
;
272 bc
->modem
.ser12
.pll_time
+= 1000000 - bdus8
;
274 if (abs(timediff
) > bdus4
)
275 bc
->modem
.ser12
.dcd_sum0
+= 4;
277 bc
->modem
.ser12
.dcd_sum0
--;
279 bc
->debug_vals
.cur_pllcorr
= timediff
;
280 #endif /* BAYCOM_DEBUG */
282 while (bc
->modem
.ser12
.pll_time
>= 1000000)
283 bc
->modem
.ser12
.pll_time
-= 1000000;
286 /* --------------------------------------------------------------------- */
288 static irqreturn_t
ser12_interrupt(int irq
, void *dev_id
)
290 struct net_device
*dev
= (struct net_device
*)dev_id
;
291 struct baycom_state
*bc
= netdev_priv(dev
);
292 struct timespec64 ts
;
293 unsigned char iir
, msr
;
294 unsigned int txcount
= 0;
296 if (!bc
|| bc
->hdrv
.magic
!= HDLCDRV_MAGIC
)
298 /* fast way out for shared irq */
299 if ((iir
= inb(IIR(dev
->base_addr
))) & 1)
301 /* get current time */
303 msr
= inb(MSR(dev
->base_addr
));
305 if ((msr
& 8) && bc
->opt_dcd
)
306 hdlcdrv_setdcd(&bc
->hdrv
, !((msr
^ bc
->opt_dcd
) & 0x80));
310 inb(LSR(dev
->base_addr
));
314 inb(RBR(dev
->base_addr
));
319 * make sure the next interrupt is generated;
320 * 0 must be used to power the modem; the modem draws its
321 * power from the TxD line
323 outb(0x00, THR(dev
->base_addr
));
327 * first output the last bit (!) then call HDLC transmitter,
328 * since this may take quite long
331 outb(0x0e | (!!bc
->modem
.ser12
.tx_bit
), MCR(dev
->base_addr
));
333 outb(0x0d, MCR(dev
->base_addr
)); /* transmitter off */
337 msr
= inb(MSR(dev
->base_addr
));
339 if ((msr
& 8) && bc
->opt_dcd
)
340 hdlcdrv_setdcd(&bc
->hdrv
, !((msr
^ bc
->opt_dcd
) & 0x80));
343 iir
= inb(IIR(dev
->base_addr
));
344 } while (!(iir
& 1));
345 ser12_rx(dev
, bc
, &ts
, msr
& 0x10); /* CTS */
346 if (bc
->modem
.ptt
&& txcount
) {
347 if (bc
->modem
.ser12
.txshreg
<= 1) {
348 bc
->modem
.ser12
.txshreg
= 0x10000 | hdlcdrv_getbits(&bc
->hdrv
);
349 if (!hdlcdrv_ptt(&bc
->hdrv
)) {
350 ser12_set_divisor(dev
, 115200/100/8);
355 bc
->modem
.ser12
.tx_bit
= !(bc
->modem
.ser12
.tx_bit
^ (bc
->modem
.ser12
.txshreg
& 1));
356 bc
->modem
.ser12
.txshreg
>>= 1;
360 if (!bc
->modem
.ptt
&& txcount
) {
361 hdlcdrv_arbitrate(dev
, &bc
->hdrv
);
362 if (hdlcdrv_ptt(&bc
->hdrv
)) {
363 ser12_set_divisor(dev
, bc
->baud_uartdiv
);
364 bc
->modem
.ser12
.txshreg
= 1;
368 hdlcdrv_transmitter(dev
, &bc
->hdrv
);
369 hdlcdrv_receiver(dev
, &bc
->hdrv
);
374 /* --------------------------------------------------------------------- */
376 enum uart
{ c_uart_unknown
, c_uart_8250
,
377 c_uart_16450
, c_uart_16550
, c_uart_16550A
};
378 static const char *uart_str
[] = {
379 "unknown", "8250", "16450", "16550", "16550A"
382 static enum uart
ser12_check_uart(unsigned int iobase
)
384 unsigned char b1
,b2
,b3
;
386 enum uart uart_tab
[] =
387 { c_uart_16450
, c_uart_unknown
, c_uart_16550
, c_uart_16550A
};
389 b1
= inb(MCR(iobase
));
390 outb(b1
| 0x10, MCR(iobase
)); /* loopback mode */
391 b2
= inb(MSR(iobase
));
392 outb(0x1a, MCR(iobase
));
393 b3
= inb(MSR(iobase
)) & 0xf0;
394 outb(b1
, MCR(iobase
)); /* restore old values */
395 outb(b2
, MSR(iobase
));
397 return c_uart_unknown
;
400 outb(0x01, FCR(iobase
)); /* enable FIFOs */
401 u
= uart_tab
[(inb(IIR(iobase
)) >> 6) & 3];
402 if (u
== c_uart_16450
) {
403 outb(0x5a, SCR(iobase
));
404 b1
= inb(SCR(iobase
));
405 outb(0xa5, SCR(iobase
));
406 b2
= inb(SCR(iobase
));
407 if ((b1
!= 0x5a) || (b2
!= 0xa5))
413 /* --------------------------------------------------------------------- */
415 static int ser12_open(struct net_device
*dev
)
417 struct baycom_state
*bc
= netdev_priv(dev
);
422 if (!dev
->base_addr
|| dev
->base_addr
> 0xffff-SER12_EXTENT
||
423 dev
->irq
< 2 || dev
->irq
> nr_irqs
) {
424 printk(KERN_INFO
"baycom_ser_fdx: invalid portnumber (max %u) "
425 "or irq (2 <= irq <= %d)\n",
426 0xffff-SER12_EXTENT
, nr_irqs
);
429 if (bc
->baud
< 300 || bc
->baud
> 4800) {
430 printk(KERN_INFO
"baycom_ser_fdx: invalid baudrate "
434 if (!request_region(dev
->base_addr
, SER12_EXTENT
, "baycom_ser_fdx")) {
435 printk(KERN_WARNING
"BAYCOM_SER_FSX: I/O port 0x%04lx busy\n",
439 memset(&bc
->modem
, 0, sizeof(bc
->modem
));
440 bc
->hdrv
.par
.bitrate
= bc
->baud
;
441 bc
->baud_us
= 1000000/bc
->baud
;
442 bc
->baud_uartdiv
= (115200/8)/bc
->baud
;
443 if ((u
= ser12_check_uart(dev
->base_addr
)) == c_uart_unknown
){
444 release_region(dev
->base_addr
, SER12_EXTENT
);
447 outb(0, FCR(dev
->base_addr
)); /* disable FIFOs */
448 outb(0x0d, MCR(dev
->base_addr
));
449 outb(0, IER(dev
->base_addr
));
450 if (request_irq(dev
->irq
, ser12_interrupt
, IRQF_SHARED
,
451 "baycom_ser_fdx", dev
)) {
452 release_region(dev
->base_addr
, SER12_EXTENT
);
456 * set the SIO to 6 Bits/character; during receive,
457 * the baud rate is set to produce 100 ints/sec
458 * to feed the channel arbitration process,
459 * during transmit to baud ints/sec to run
462 ser12_set_divisor(dev
, 115200/100/8);
464 * enable transmitter empty interrupt and modem status interrupt
466 outb(0x0a, IER(dev
->base_addr
));
468 * make sure the next interrupt is generated;
469 * 0 must be used to power the modem; the modem draws its
470 * power from the TxD line
472 outb(0x00, THR(dev
->base_addr
));
473 hdlcdrv_setdcd(&bc
->hdrv
, 0);
474 printk(KERN_INFO
"%s: ser_fdx at iobase 0x%lx irq %u baud %u uart %s\n",
475 bc_drvname
, dev
->base_addr
, dev
->irq
, bc
->baud
, uart_str
[u
]);
479 /* --------------------------------------------------------------------- */
481 static int ser12_close(struct net_device
*dev
)
483 struct baycom_state
*bc
= netdev_priv(dev
);
490 outb(0, IER(dev
->base_addr
));
491 outb(1, MCR(dev
->base_addr
));
492 free_irq(dev
->irq
, dev
);
493 release_region(dev
->base_addr
, SER12_EXTENT
);
494 printk(KERN_INFO
"%s: close ser_fdx at iobase 0x%lx irq %u\n",
495 bc_drvname
, dev
->base_addr
, dev
->irq
);
499 /* --------------------------------------------------------------------- */
501 * ===================== hdlcdrv driver interface =========================
504 /* --------------------------------------------------------------------- */
506 static int baycom_ioctl(struct net_device
*dev
, struct ifreq
*ifr
,
507 struct hdlcdrv_ioctl
*hi
, int cmd
);
509 /* --------------------------------------------------------------------- */
511 static const struct hdlcdrv_ops ser12_ops
= {
512 .drvname
= bc_drvname
,
513 .drvinfo
= bc_drvinfo
,
515 .close
= ser12_close
,
516 .ioctl
= baycom_ioctl
,
519 /* --------------------------------------------------------------------- */
521 static int baycom_setmode(struct baycom_state
*bc
, const char *modestr
)
525 if (!strncmp(modestr
, "ser", 3)) {
526 baud
= simple_strtoul(modestr
+3, NULL
, 10);
527 if (baud
>= 3 && baud
<= 48)
530 if (strchr(modestr
, '*'))
532 else if (strchr(modestr
, '+'))
539 /* --------------------------------------------------------------------- */
541 static int baycom_ioctl(struct net_device
*dev
, struct ifreq
*ifr
,
542 struct hdlcdrv_ioctl
*hi
, int cmd
)
544 struct baycom_state
*bc
;
545 struct baycom_ioctl bi
;
550 bc
= netdev_priv(dev
);
551 BUG_ON(bc
->hdrv
.magic
!= HDLCDRV_MAGIC
);
553 if (cmd
!= SIOCDEVPRIVATE
)
559 case HDLCDRVCTL_GETMODE
:
560 sprintf(hi
->data
.modename
, "ser%u", bc
->baud
/ 100);
561 if (bc
->opt_dcd
<= 0)
562 strcat(hi
->data
.modename
, (!bc
->opt_dcd
) ? "*" : "+");
563 if (copy_to_user(ifr
->ifr_data
, hi
, sizeof(struct hdlcdrv_ioctl
)))
567 case HDLCDRVCTL_SETMODE
:
568 if (netif_running(dev
) || !capable(CAP_NET_ADMIN
))
570 hi
->data
.modename
[sizeof(hi
->data
.modename
)-1] = '\0';
571 return baycom_setmode(bc
, hi
->data
.modename
);
573 case HDLCDRVCTL_MODELIST
:
574 strcpy(hi
->data
.modename
, "ser12,ser3,ser24");
575 if (copy_to_user(ifr
->ifr_data
, hi
, sizeof(struct hdlcdrv_ioctl
)))
579 case HDLCDRVCTL_MODEMPARMASK
:
580 return HDLCDRV_PARMASK_IOBASE
| HDLCDRV_PARMASK_IRQ
;
584 if (copy_from_user(&bi
, ifr
->ifr_data
, sizeof(bi
)))
591 case BAYCOMCTL_GETDEBUG
:
592 bi
.data
.dbg
.debug1
= bc
->hdrv
.ptt_keyed
;
593 bi
.data
.dbg
.debug2
= bc
->debug_vals
.last_intcnt
;
594 bi
.data
.dbg
.debug3
= bc
->debug_vals
.last_pllcorr
;
596 #endif /* BAYCOM_DEBUG */
599 if (copy_to_user(ifr
->ifr_data
, &bi
, sizeof(bi
)))
605 /* --------------------------------------------------------------------- */
608 * command line settable parameters
610 static char *mode
[NR_PORTS
] = { "ser12*", };
611 static int iobase
[NR_PORTS
] = { 0x3f8, };
612 static int irq
[NR_PORTS
] = { 4, };
613 static int baud
[NR_PORTS
] = { [0 ... NR_PORTS
-1] = 1200 };
615 module_param_array(mode
, charp
, NULL
, 0);
616 MODULE_PARM_DESC(mode
, "baycom operating mode; * for software DCD");
617 module_param_hw_array(iobase
, int, ioport
, NULL
, 0);
618 MODULE_PARM_DESC(iobase
, "baycom io base address");
619 module_param_hw_array(irq
, int, irq
, NULL
, 0);
620 MODULE_PARM_DESC(irq
, "baycom irq number");
621 module_param_array(baud
, int, NULL
, 0);
622 MODULE_PARM_DESC(baud
, "baycom baud rate (300 to 4800)");
624 MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
625 MODULE_DESCRIPTION("Baycom ser12 full duplex amateur radio modem driver");
626 MODULE_LICENSE("GPL");
628 /* --------------------------------------------------------------------- */
630 static int __init
init_baycomserfdx(void)
637 * register net devices
639 for (i
= 0; i
< NR_PORTS
; i
++) {
640 struct net_device
*dev
;
641 struct baycom_state
*bc
;
642 char ifname
[IFNAMSIZ
];
644 sprintf(ifname
, "bcsf%d", i
);
649 iobase
[i
] = irq
[i
] = 0;
651 dev
= hdlcdrv_register(&ser12_ops
,
652 sizeof(struct baycom_state
),
653 ifname
, iobase
[i
], irq
[i
], 0);
657 bc
= netdev_priv(dev
);
658 if (set_hw
&& baycom_setmode(bc
, mode
[i
]))
662 baycom_device
[i
] = dev
;
670 static void __exit
cleanup_baycomserfdx(void)
674 for(i
= 0; i
< NR_PORTS
; i
++) {
675 struct net_device
*dev
= baycom_device
[i
];
677 hdlcdrv_unregister(dev
);
681 module_init(init_baycomserfdx
);
682 module_exit(cleanup_baycomserfdx
);
684 /* --------------------------------------------------------------------- */
689 * format: baycom_ser_fdx=io,irq,mode
690 * mode: ser# hardware DCD
692 * ser#+ hardware DCD, inverted signal at DCD pin
693 * '#' denotes the baud rate / 100, eg. ser12* is '1200 baud, soft DCD'
696 static int __init
baycom_ser_fdx_setup(char *str
)
698 static unsigned nr_dev
;
701 if (nr_dev
>= NR_PORTS
)
703 str
= get_options(str
, 4, ints
);
707 iobase
[nr_dev
] = ints
[1];
708 irq
[nr_dev
] = ints
[2];
710 baud
[nr_dev
] = ints
[3];
715 __setup("baycom_ser_fdx=", baycom_ser_fdx_setup
);
718 /* --------------------------------------------------------------------- */