1 /*****************************************************************************/
4 * baycom_ser_hdx.c -- baycom ser12 halfduplex 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)
38 * Command line options (insmod command line)
40 * mode ser12 hardware DCD
42 * ser12@ hardware/software DCD, i.e. no explicit DCD signal but hardware
43 * mutes audio input to the modem
44 * ser12+ hardware DCD, inverted signal at DCD pin
45 * iobase base address of the port; common values are 0x3f8, 0x2f8, 0x3e8, 0x2e8
46 * irq interrupt line of the port; common values are 4,3
50 * 0.1 26.06.1996 Adapted from baycom.c and made network driver interface
51 * 18.10.1996 Changed to new user space access routines (copy_{to,from}_user)
52 * 0.3 26.04.1997 init code/data tagged
53 * 0.4 08.07.1997 alternative ser12 decoding algorithm (uses delta CTS ints)
54 * 0.5 11.11.1997 ser12/par96 split into separate files
55 * 0.6 14.04.1998 cleanups
56 * 0.7 03.08.1999 adapt to Linus' new __setup/__initcall
57 * 0.8 10.08.1999 use module_init/module_exit
58 * 0.9 12.02.2000 adapted to softnet driver interface
59 * 0.10 03.07.2000 fix interface name handling
62 /*****************************************************************************/
64 #include <linux/module.h>
65 #include <linux/ioport.h>
66 #include <linux/string.h>
67 #include <linux/init.h>
68 #include <asm/uaccess.h>
70 #include <linux/hdlcdrv.h>
71 #include <linux/baycom.h>
73 /* --------------------------------------------------------------------- */
77 /* --------------------------------------------------------------------- */
79 static const char bc_drvname[] = "baycom_ser_hdx";
80 static const char bc_drvinfo[] = KERN_INFO "baycom_ser_hdx: (C) 1996-2000 Thomas Sailer, HB9JNX/AE4WA\n"
81 KERN_INFO "baycom_ser_hdx: version 0.10 compiled " __TIME__ " " __DATE__ "\n";
83 /* --------------------------------------------------------------------- */
87 static struct net_device *baycom_device[NR_PORTS];
89 /* --------------------------------------------------------------------- */
91 #define RBR(iobase) (iobase+0)
92 #define THR(iobase) (iobase+0)
93 #define IER(iobase) (iobase+1)
94 #define IIR(iobase) (iobase+2)
95 #define FCR(iobase) (iobase+2)
96 #define LCR(iobase) (iobase+3)
97 #define MCR(iobase) (iobase+4)
98 #define LSR(iobase) (iobase+5)
99 #define MSR(iobase) (iobase+6)
100 #define SCR(iobase) (iobase+7)
101 #define DLL(iobase) (iobase+0)
102 #define DLM(iobase) (iobase+1)
104 #define SER12_EXTENT 8
106 /* ---------------------------------------------------------------------- */
108 * Information that need to be kept for each board.
111 struct baycom_state {
112 struct hdlcdrv_state hdrv;
120 struct modem_state_ser12 {
121 unsigned char tx_bit;
122 int dcd_sum0, dcd_sum1, dcd_sum2;
123 unsigned char last_sample;
124 unsigned char last_rxbit;
125 unsigned int dcd_shreg;
126 unsigned int dcd_time;
127 unsigned int bit_pll;
128 unsigned char interm_sample;
134 unsigned long last_jiffies;
136 unsigned last_intcnt;
140 #endif /* BAYCOM_DEBUG */
143 /* --------------------------------------------------------------------- */
145 static inline void baycom_int_freq(struct baycom_state *bc)
148 unsigned long cur_jiffies = jiffies;
150 * measure the interrupt frequency
152 bc->debug_vals.cur_intcnt++;
153 if ((cur_jiffies - bc->debug_vals.last_jiffies) >= HZ) {
154 bc->debug_vals.last_jiffies = cur_jiffies;
155 bc->debug_vals.last_intcnt = bc->debug_vals.cur_intcnt;
156 bc->debug_vals.cur_intcnt = 0;
157 bc->debug_vals.last_pllcorr = bc->debug_vals.cur_pllcorr;
158 bc->debug_vals.cur_pllcorr = 0;
160 #endif /* BAYCOM_DEBUG */
163 /* --------------------------------------------------------------------- */
165 * ===================== SER12 specific routines =========================
168 static inline void ser12_set_divisor(struct net_device *dev,
169 unsigned char divisor)
171 outb(0x81, LCR(dev->base_addr)); /* DLAB = 1 */
172 outb(divisor, DLL(dev->base_addr));
173 outb(0, DLM(dev->base_addr));
174 outb(0x01, LCR(dev->base_addr)); /* word length = 6 */
176 * make sure the next interrupt is generated;
177 * 0 must be used to power the modem; the modem draws its
178 * power from the TxD line
180 outb(0x00, THR(dev->base_addr));
182 * it is important not to set the divider while transmitting;
183 * this reportedly makes some UARTs generating interrupts
184 * in the hundredthousands per second region
185 * Reported by: Ignacio.Arenaza@studi.epfl.ch (Ignacio Arenaza Nuno)
189 /* --------------------------------------------------------------------- */
192 * must call the TX arbitrator every 10ms
194 #define SER12_ARB_DIVIDER(bc) (bc->opt_dcd ? 24 : 36)
196 #define SER12_DCD_INTERVAL(bc) (bc->opt_dcd ? 12 : 240)
198 static inline void ser12_tx(struct net_device *dev, struct baycom_state *bc)
200 /* one interrupt per channel bit */
201 ser12_set_divisor(dev, 12);
203 * first output the last bit (!) then call HDLC transmitter,
204 * since this may take quite long
206 outb(0x0e | (!!bc->modem.ser12.tx_bit), MCR(dev->base_addr));
207 if (bc->modem.shreg <= 1)
208 bc->modem.shreg = 0x10000 | hdlcdrv_getbits(&bc->hdrv);
209 bc->modem.ser12.tx_bit = !(bc->modem.ser12.tx_bit ^
210 (bc->modem.shreg & 1));
211 bc->modem.shreg >>= 1;
214 /* --------------------------------------------------------------------- */
216 static inline void ser12_rx(struct net_device *dev, struct baycom_state *bc)
222 cur_s = inb(MSR(dev->base_addr)) & 0x10; /* the CTS line */
223 hdlcdrv_channelbit(&bc->hdrv, cur_s);
224 bc->modem.ser12.dcd_shreg = (bc->modem.ser12.dcd_shreg << 1) |
225 (cur_s != bc->modem.ser12.last_sample);
226 bc->modem.ser12.last_sample = cur_s;
227 if(bc->modem.ser12.dcd_shreg & 1) {
229 unsigned int dcdspos, dcdsneg;
231 dcdspos = dcdsneg = 0;
232 dcdspos += ((bc->modem.ser12.dcd_shreg >> 1) & 1);
233 if (!(bc->modem.ser12.dcd_shreg & 0x7ffffffe))
235 dcdsneg += ((bc->modem.ser12.dcd_shreg >> 2) & 1);
236 dcdsneg += ((bc->modem.ser12.dcd_shreg >> 3) & 1);
237 dcdsneg += ((bc->modem.ser12.dcd_shreg >> 4) & 1);
239 bc->modem.ser12.dcd_sum0 += 16*dcdspos - dcdsneg;
241 bc->modem.ser12.dcd_sum0--;
243 if(!bc->modem.ser12.dcd_time) {
244 hdlcdrv_setdcd(&bc->hdrv, (bc->modem.ser12.dcd_sum0 +
245 bc->modem.ser12.dcd_sum1 +
246 bc->modem.ser12.dcd_sum2) < 0);
247 bc->modem.ser12.dcd_sum2 = bc->modem.ser12.dcd_sum1;
248 bc->modem.ser12.dcd_sum1 = bc->modem.ser12.dcd_sum0;
249 /* offset to ensure DCD off on silent input */
250 bc->modem.ser12.dcd_sum0 = 2;
251 bc->modem.ser12.dcd_time = SER12_DCD_INTERVAL(bc);
253 bc->modem.ser12.dcd_time--;
256 * PLL code for the improved software DCD algorithm
258 if (bc->modem.ser12.interm_sample) {
260 * intermediate sample; set timing correction to normal
262 ser12_set_divisor(dev, 4);
265 * do PLL correction and call HDLC receiver
267 switch (bc->modem.ser12.dcd_shreg & 7) {
268 case 1: /* transition too late */
269 ser12_set_divisor(dev, 5);
271 bc->debug_vals.cur_pllcorr++;
272 #endif /* BAYCOM_DEBUG */
274 case 4: /* transition too early */
275 ser12_set_divisor(dev, 3);
277 bc->debug_vals.cur_pllcorr--;
278 #endif /* BAYCOM_DEBUG */
281 ser12_set_divisor(dev, 4);
284 bc->modem.shreg >>= 1;
285 if (bc->modem.ser12.last_sample ==
286 bc->modem.ser12.last_rxbit)
287 bc->modem.shreg |= 0x10000;
288 bc->modem.ser12.last_rxbit =
289 bc->modem.ser12.last_sample;
291 if (++bc->modem.ser12.interm_sample >= 3)
292 bc->modem.ser12.interm_sample = 0;
296 if (bc->modem.ser12.dcd_shreg & 1) {
297 unsigned int dcdspos, dcdsneg;
299 dcdspos = dcdsneg = 0;
300 dcdspos += ((bc->modem.ser12.dcd_shreg >> 1) & 1);
301 dcdspos += (!(bc->modem.ser12.dcd_shreg & 0x7ffffffe))
303 dcdsneg += ((bc->modem.ser12.dcd_shreg >> 2) & 1);
304 dcdsneg += ((bc->modem.ser12.dcd_shreg >> 3) & 1);
305 dcdsneg += ((bc->modem.ser12.dcd_shreg >> 4) & 1);
307 bc->modem.ser12.dcd_sum0 += 16*dcdspos - dcdsneg;
311 * PLL algorithm for the hardware squelch DCD algorithm
313 if (bc->modem.ser12.interm_sample) {
315 * intermediate sample; set timing correction to normal
317 ser12_set_divisor(dev, 6);
320 * do PLL correction and call HDLC receiver
322 switch (bc->modem.ser12.dcd_shreg & 3) {
323 case 1: /* transition too late */
324 ser12_set_divisor(dev, 7);
326 bc->debug_vals.cur_pllcorr++;
327 #endif /* BAYCOM_DEBUG */
329 case 2: /* transition too early */
330 ser12_set_divisor(dev, 5);
332 bc->debug_vals.cur_pllcorr--;
333 #endif /* BAYCOM_DEBUG */
336 ser12_set_divisor(dev, 6);
339 bc->modem.shreg >>= 1;
340 if (bc->modem.ser12.last_sample ==
341 bc->modem.ser12.last_rxbit)
342 bc->modem.shreg |= 0x10000;
343 bc->modem.ser12.last_rxbit =
344 bc->modem.ser12.last_sample;
346 bc->modem.ser12.interm_sample = !bc->modem.ser12.interm_sample;
350 bc->modem.ser12.dcd_sum0 -= (bc->modem.ser12.dcd_shreg & 1);
352 outb(0x0d, MCR(dev->base_addr)); /* transmitter off */
353 if (bc->modem.shreg & 1) {
354 hdlcdrv_putbits(&bc->hdrv, bc->modem.shreg >> 1);
355 bc->modem.shreg = 0x10000;
357 if(!bc->modem.ser12.dcd_time) {
359 hdlcdrv_setdcd(&bc->hdrv, !((inb(MSR(dev->base_addr)) ^ bc->opt_dcd) & 0x80));
361 hdlcdrv_setdcd(&bc->hdrv, (bc->modem.ser12.dcd_sum0 +
362 bc->modem.ser12.dcd_sum1 +
363 bc->modem.ser12.dcd_sum2) < 0);
364 bc->modem.ser12.dcd_sum2 = bc->modem.ser12.dcd_sum1;
365 bc->modem.ser12.dcd_sum1 = bc->modem.ser12.dcd_sum0;
366 /* offset to ensure DCD off on silent input */
367 bc->modem.ser12.dcd_sum0 = 2;
368 bc->modem.ser12.dcd_time = SER12_DCD_INTERVAL(bc);
370 bc->modem.ser12.dcd_time--;
373 /* --------------------------------------------------------------------- */
375 static irqreturn_t ser12_interrupt(int irq, void *dev_id, struct pt_regs *regs)
377 struct net_device *dev = (struct net_device *)dev_id;
378 struct baycom_state *bc = netdev_priv(dev);
381 if (!dev || !bc || bc->hdrv.magic != HDLCDRV_MAGIC)
384 if ((iir = inb(IIR(dev->base_addr))) & 1)
390 inb(LSR(dev->base_addr));
394 inb(RBR(dev->base_addr));
399 * check if transmitter active
401 if (hdlcdrv_ptt(&bc->hdrv))
405 bc->modem.arb_divider--;
407 outb(0x00, THR(dev->base_addr));
411 inb(MSR(dev->base_addr));
414 iir = inb(IIR(dev->base_addr));
415 } while (!(iir & 1));
416 if (bc->modem.arb_divider <= 0) {
417 bc->modem.arb_divider = SER12_ARB_DIVIDER(bc);
419 hdlcdrv_arbitrate(dev, &bc->hdrv);
422 hdlcdrv_transmitter(dev, &bc->hdrv);
423 hdlcdrv_receiver(dev, &bc->hdrv);
428 /* --------------------------------------------------------------------- */
430 enum uart { c_uart_unknown, c_uart_8250,
431 c_uart_16450, c_uart_16550, c_uart_16550A};
432 static const char *uart_str[] = {
433 "unknown", "8250", "16450", "16550", "16550A"
436 static enum uart ser12_check_uart(unsigned int iobase)
438 unsigned char b1,b2,b3;
440 enum uart uart_tab[] =
441 { c_uart_16450, c_uart_unknown, c_uart_16550, c_uart_16550A };
443 b1 = inb(MCR(iobase));
444 outb(b1 | 0x10, MCR(iobase)); /* loopback mode */
445 b2 = inb(MSR(iobase));
446 outb(0x1a, MCR(iobase));
447 b3 = inb(MSR(iobase)) & 0xf0;
448 outb(b1, MCR(iobase)); /* restore old values */
449 outb(b2, MSR(iobase));
451 return c_uart_unknown;
454 outb(0x01, FCR(iobase)); /* enable FIFOs */
455 u = uart_tab[(inb(IIR(iobase)) >> 6) & 3];
456 if (u == c_uart_16450) {
457 outb(0x5a, SCR(iobase));
458 b1 = inb(SCR(iobase));
459 outb(0xa5, SCR(iobase));
460 b2 = inb(SCR(iobase));
461 if ((b1 != 0x5a) || (b2 != 0xa5))
467 /* --------------------------------------------------------------------- */
469 static int ser12_open(struct net_device *dev)
471 struct baycom_state *bc = netdev_priv(dev);
476 if (!dev->base_addr || dev->base_addr > 0x1000-SER12_EXTENT ||
477 dev->irq < 2 || dev->irq > 15)
479 if (!request_region(dev->base_addr, SER12_EXTENT, "baycom_ser12"))
481 memset(&bc->modem, 0, sizeof(bc->modem));
482 bc->hdrv.par.bitrate = 1200;
483 if ((u = ser12_check_uart(dev->base_addr)) == c_uart_unknown) {
484 release_region(dev->base_addr, SER12_EXTENT);
487 outb(0, FCR(dev->base_addr)); /* disable FIFOs */
488 outb(0x0d, MCR(dev->base_addr));
489 outb(0, IER(dev->base_addr));
490 if (request_irq(dev->irq, ser12_interrupt, SA_INTERRUPT | SA_SHIRQ,
491 "baycom_ser12", dev)) {
492 release_region(dev->base_addr, SER12_EXTENT);
496 * enable transmitter empty interrupt
498 outb(2, IER(dev->base_addr));
500 * set the SIO to 6 Bits/character and 19200 or 28800 baud, so that
501 * we get exactly (hopefully) 2 or 3 interrupts per radio symbol,
502 * depending on the usage of the software DCD routine
504 ser12_set_divisor(dev, bc->opt_dcd ? 6 : 4);
505 printk(KERN_INFO "%s: ser12 at iobase 0x%lx irq %u uart %s\n",
506 bc_drvname, dev->base_addr, dev->irq, uart_str[u]);
510 /* --------------------------------------------------------------------- */
512 static int ser12_close(struct net_device *dev)
514 struct baycom_state *bc = netdev_priv(dev);
521 outb(0, IER(dev->base_addr));
522 outb(1, MCR(dev->base_addr));
523 free_irq(dev->irq, dev);
524 release_region(dev->base_addr, SER12_EXTENT);
525 printk(KERN_INFO "%s: close ser12 at iobase 0x%lx irq %u\n",
526 bc_drvname, dev->base_addr, dev->irq);
530 /* --------------------------------------------------------------------- */
532 * ===================== hdlcdrv driver interface =========================
535 /* --------------------------------------------------------------------- */
537 static int baycom_ioctl(struct net_device *dev, struct ifreq *ifr,
538 struct hdlcdrv_ioctl *hi, int cmd);
540 /* --------------------------------------------------------------------- */
542 static struct hdlcdrv_ops ser12_ops = {
543 .drvname = bc_drvname,
544 .drvinfo = bc_drvinfo,
546 .close = ser12_close,
547 .ioctl = baycom_ioctl,
550 /* --------------------------------------------------------------------- */
552 static int baycom_setmode(struct baycom_state *bc, const char *modestr)
554 if (strchr(modestr, '*'))
556 else if (strchr(modestr, '+'))
558 else if (strchr(modestr, '@'))
565 /* --------------------------------------------------------------------- */
567 static int baycom_ioctl(struct net_device *dev, struct ifreq *ifr,
568 struct hdlcdrv_ioctl *hi, int cmd)
570 struct baycom_state *bc;
571 struct baycom_ioctl bi;
576 bc = netdev_priv(dev);
577 BUG_ON(bc->hdrv.magic != HDLCDRV_MAGIC);
579 if (cmd != SIOCDEVPRIVATE)
585 case HDLCDRVCTL_GETMODE:
586 strcpy(hi->data.modename, "ser12");
587 if (bc->opt_dcd <= 0)
588 strcat(hi->data.modename, (!bc->opt_dcd) ? "*" : (bc->opt_dcd == -2) ? "@" : "+");
589 if (copy_to_user(ifr->ifr_data, hi, sizeof(struct hdlcdrv_ioctl)))
593 case HDLCDRVCTL_SETMODE:
594 if (netif_running(dev) || !capable(CAP_NET_ADMIN))
596 hi->data.modename[sizeof(hi->data.modename)-1] = '\0';
597 return baycom_setmode(bc, hi->data.modename);
599 case HDLCDRVCTL_MODELIST:
600 strcpy(hi->data.modename, "ser12");
601 if (copy_to_user(ifr->ifr_data, hi, sizeof(struct hdlcdrv_ioctl)))
605 case HDLCDRVCTL_MODEMPARMASK:
606 return HDLCDRV_PARMASK_IOBASE | HDLCDRV_PARMASK_IRQ;
610 if (copy_from_user(&bi, ifr->ifr_data, sizeof(bi)))
617 case BAYCOMCTL_GETDEBUG:
618 bi.data.dbg.debug1 = bc->hdrv.ptt_keyed;
619 bi.data.dbg.debug2 = bc->debug_vals.last_intcnt;
620 bi.data.dbg.debug3 = bc->debug_vals.last_pllcorr;
622 #endif /* BAYCOM_DEBUG */
625 if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
631 /* --------------------------------------------------------------------- */
634 * command line settable parameters
636 static char *mode[NR_PORTS] = { "ser12*", };
637 static int iobase[NR_PORTS] = { 0x3f8, };
638 static int irq[NR_PORTS] = { 4, };
640 module_param_array(mode, charp, NULL, 0);
641 MODULE_PARM_DESC(mode, "baycom operating mode; * for software DCD");
642 module_param_array(iobase, int, NULL, 0);
643 MODULE_PARM_DESC(iobase, "baycom io base address");
644 module_param_array(irq, int, NULL, 0);
645 MODULE_PARM_DESC(irq, "baycom irq number");
647 MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
648 MODULE_DESCRIPTION("Baycom ser12 half duplex amateur radio modem driver");
649 MODULE_LICENSE("GPL");
651 /* --------------------------------------------------------------------- */
653 static int __init init_baycomserhdx(void)
660 * register net devices
662 for (i = 0; i < NR_PORTS; i++) {
663 struct net_device *dev;
664 struct baycom_state *bc;
665 char ifname[IFNAMSIZ];
667 sprintf(ifname, "bcsh%d", i);
672 iobase[i] = irq[i] = 0;
674 dev = hdlcdrv_register(&ser12_ops,
675 sizeof(struct baycom_state),
676 ifname, iobase[i], irq[i], 0);
680 bc = netdev_priv(dev);
681 if (set_hw && baycom_setmode(bc, mode[i]))
684 baycom_device[i] = dev;
692 static void __exit cleanup_baycomserhdx(void)
696 for(i = 0; i < NR_PORTS; i++) {
697 struct net_device *dev = baycom_device[i];
700 hdlcdrv_unregister(dev);
704 module_init(init_baycomserhdx);
705 module_exit(cleanup_baycomserhdx);
707 /* --------------------------------------------------------------------- */
712 * format: baycom_ser_hdx=io,irq,mode
713 * mode: ser12 hardware DCD
714 * ser12* software DCD
715 * ser12@ hardware/software DCD, i.e. no explicit DCD signal but hardware
716 * mutes audio input to the modem
717 * ser12+ hardware DCD, inverted signal at DCD pin
720 static int __init baycom_ser_hdx_setup(char *str)
722 static unsigned nr_dev;
725 if (nr_dev >= NR_PORTS)
727 str = get_options(str, 3, ints);
731 iobase[nr_dev] = ints[1];
732 irq[nr_dev] = ints[2];
737 __setup("baycom_ser_hdx=", baycom_ser_hdx_setup);
740 /* --------------------------------------------------------------------- */