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 contignuous 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/module.h>
75 #include <linux/ioport.h>
76 #include <linux/string.h>
77 #include <linux/init.h>
78 #include <linux/hdlcdrv.h>
79 #include <linux/baycom.h>
80 #include <linux/jiffies.h>
82 #include <asm/uaccess.h>
86 /* --------------------------------------------------------------------- */
90 /* --------------------------------------------------------------------- */
92 static const char bc_drvname[] = "baycom_ser_fdx";
93 static const char bc_drvinfo[] = KERN_INFO "baycom_ser_fdx: (C) 1996-2000 Thomas Sailer, HB9JNX/AE4WA\n"
94 KERN_INFO "baycom_ser_fdx: version 0.10 compiled " __TIME__ " " __DATE__ "\n";
96 /* --------------------------------------------------------------------- */
100 static struct net_device *baycom_device[NR_PORTS];
102 /* --------------------------------------------------------------------- */
104 #define RBR(iobase) (iobase+0)
105 #define THR(iobase) (iobase+0)
106 #define IER(iobase) (iobase+1)
107 #define IIR(iobase) (iobase+2)
108 #define FCR(iobase) (iobase+2)
109 #define LCR(iobase) (iobase+3)
110 #define MCR(iobase) (iobase+4)
111 #define LSR(iobase) (iobase+5)
112 #define MSR(iobase) (iobase+6)
113 #define SCR(iobase) (iobase+7)
114 #define DLL(iobase) (iobase+0)
115 #define DLM(iobase) (iobase+1)
117 #define SER12_EXTENT 8
119 /* ---------------------------------------------------------------------- */
121 * Information that need to be kept for each board.
124 struct baycom_state {
125 struct hdlcdrv_state hdrv;
127 unsigned int baud, baud_us, baud_arbdiv, baud_uartdiv, baud_dcdtimeout;
134 struct modem_state_ser12 {
135 unsigned char tx_bit;
136 unsigned char last_rxbit;
137 int dcd_sum0, dcd_sum1, dcd_sum2;
139 unsigned int pll_time;
140 unsigned int txshreg;
146 unsigned long last_jiffies;
148 unsigned last_intcnt;
152 #endif /* BAYCOM_DEBUG */
155 /* --------------------------------------------------------------------- */
157 static inline void baycom_int_freq(struct baycom_state *bc)
160 unsigned long cur_jiffies = jiffies;
162 * measure the interrupt frequency
164 bc->debug_vals.cur_intcnt++;
165 if (time_after_eq(cur_jiffies, bc->debug_vals.last_jiffies + HZ)) {
166 bc->debug_vals.last_jiffies = cur_jiffies;
167 bc->debug_vals.last_intcnt = bc->debug_vals.cur_intcnt;
168 bc->debug_vals.cur_intcnt = 0;
169 bc->debug_vals.last_pllcorr = bc->debug_vals.cur_pllcorr;
170 bc->debug_vals.cur_pllcorr = 0;
172 #endif /* BAYCOM_DEBUG */
175 /* --------------------------------------------------------------------- */
177 * ===================== SER12 specific routines =========================
180 /* --------------------------------------------------------------------- */
182 static inline void ser12_set_divisor(struct net_device *dev,
183 unsigned int divisor)
185 outb(0x81, LCR(dev->base_addr)); /* DLAB = 1 */
186 outb(divisor, DLL(dev->base_addr));
187 outb(divisor >> 8, DLM(dev->base_addr));
188 outb(0x01, LCR(dev->base_addr)); /* word length = 6 */
190 * make sure the next interrupt is generated;
191 * 0 must be used to power the modem; the modem draws its
192 * power from the TxD line
194 outb(0x00, THR(dev->base_addr));
196 * it is important not to set the divider while transmitting;
197 * this reportedly makes some UARTs generating interrupts
198 * in the hundredthousands per second region
199 * Reported by: Ignacio.Arenaza@studi.epfl.ch (Ignacio Arenaza Nuno)
203 /* --------------------------------------------------------------------- */
206 static inline unsigned int hweight16(unsigned int w)
207 __attribute__ ((unused));
208 static inline unsigned int hweight8(unsigned int w)
209 __attribute__ ((unused));
211 static inline unsigned int hweight16(unsigned int w)
213 unsigned short res = (w & 0x5555) + ((w >> 1) & 0x5555);
214 res = (res & 0x3333) + ((res >> 2) & 0x3333);
215 res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F);
216 return (res & 0x00FF) + ((res >> 8) & 0x00FF);
219 static inline unsigned int hweight8(unsigned int w)
221 unsigned short res = (w & 0x55) + ((w >> 1) & 0x55);
222 res = (res & 0x33) + ((res >> 2) & 0x33);
223 return (res & 0x0F) + ((res >> 4) & 0x0F);
227 /* --------------------------------------------------------------------- */
229 static __inline__ void ser12_rx(struct net_device *dev, struct baycom_state *bc, struct timeval *tv, unsigned char curs)
232 int bdus8 = bc->baud_us >> 3;
233 int bdus4 = bc->baud_us >> 2;
234 int bdus2 = bc->baud_us >> 1;
236 timediff = 1000000 + tv->tv_usec - bc->modem.ser12.pll_time;
237 while (timediff >= 500000)
239 while (timediff >= bdus2) {
240 timediff -= bc->baud_us;
241 bc->modem.ser12.pll_time += bc->baud_us;
242 bc->modem.ser12.dcd_time--;
243 /* first check if there is room to add a bit */
244 if (bc->modem.shreg & 1) {
245 hdlcdrv_putbits(&bc->hdrv, (bc->modem.shreg >> 1) ^ 0xffff);
246 bc->modem.shreg = 0x10000;
249 bc->modem.shreg >>= 1;
251 if (bc->modem.ser12.dcd_time <= 0) {
253 hdlcdrv_setdcd(&bc->hdrv, (bc->modem.ser12.dcd_sum0 +
254 bc->modem.ser12.dcd_sum1 +
255 bc->modem.ser12.dcd_sum2) < 0);
256 bc->modem.ser12.dcd_sum2 = bc->modem.ser12.dcd_sum1;
257 bc->modem.ser12.dcd_sum1 = bc->modem.ser12.dcd_sum0;
258 bc->modem.ser12.dcd_sum0 = 2; /* slight bias */
259 bc->modem.ser12.dcd_time += 120;
261 if (bc->modem.ser12.last_rxbit != curs) {
262 bc->modem.ser12.last_rxbit = curs;
263 bc->modem.shreg |= 0x10000;
266 bc->modem.ser12.pll_time += bdus8;
268 bc->modem.ser12.pll_time += 1000000 - bdus8;
270 if (abs(timediff) > bdus4)
271 bc->modem.ser12.dcd_sum0 += 4;
273 bc->modem.ser12.dcd_sum0--;
275 bc->debug_vals.cur_pllcorr = timediff;
276 #endif /* BAYCOM_DEBUG */
278 while (bc->modem.ser12.pll_time >= 1000000)
279 bc->modem.ser12.pll_time -= 1000000;
282 /* --------------------------------------------------------------------- */
284 static irqreturn_t ser12_interrupt(int irq, void *dev_id)
286 struct net_device *dev = (struct net_device *)dev_id;
287 struct baycom_state *bc = netdev_priv(dev);
289 unsigned char iir, msr;
290 unsigned int txcount = 0;
292 if (!bc || bc->hdrv.magic != HDLCDRV_MAGIC)
294 /* fast way out for shared irq */
295 if ((iir = inb(IIR(dev->base_addr))) & 1)
297 /* get current time */
298 do_gettimeofday(&tv);
299 msr = inb(MSR(dev->base_addr));
301 if ((msr & 8) && bc->opt_dcd)
302 hdlcdrv_setdcd(&bc->hdrv, !((msr ^ bc->opt_dcd) & 0x80));
306 inb(LSR(dev->base_addr));
310 inb(RBR(dev->base_addr));
315 * make sure the next interrupt is generated;
316 * 0 must be used to power the modem; the modem draws its
317 * power from the TxD line
319 outb(0x00, THR(dev->base_addr));
323 * first output the last bit (!) then call HDLC transmitter,
324 * since this may take quite long
327 outb(0x0e | (!!bc->modem.ser12.tx_bit), MCR(dev->base_addr));
329 outb(0x0d, MCR(dev->base_addr)); /* transmitter off */
333 msr = inb(MSR(dev->base_addr));
335 if ((msr & 8) && bc->opt_dcd)
336 hdlcdrv_setdcd(&bc->hdrv, !((msr ^ bc->opt_dcd) & 0x80));
339 iir = inb(IIR(dev->base_addr));
340 } while (!(iir & 1));
341 ser12_rx(dev, bc, &tv, msr & 0x10); /* CTS */
342 if (bc->modem.ptt && txcount) {
343 if (bc->modem.ser12.txshreg <= 1) {
344 bc->modem.ser12.txshreg = 0x10000 | hdlcdrv_getbits(&bc->hdrv);
345 if (!hdlcdrv_ptt(&bc->hdrv)) {
346 ser12_set_divisor(dev, 115200/100/8);
351 bc->modem.ser12.tx_bit = !(bc->modem.ser12.tx_bit ^ (bc->modem.ser12.txshreg & 1));
352 bc->modem.ser12.txshreg >>= 1;
356 if (!bc->modem.ptt && txcount) {
357 hdlcdrv_arbitrate(dev, &bc->hdrv);
358 if (hdlcdrv_ptt(&bc->hdrv)) {
359 ser12_set_divisor(dev, bc->baud_uartdiv);
360 bc->modem.ser12.txshreg = 1;
364 hdlcdrv_transmitter(dev, &bc->hdrv);
365 hdlcdrv_receiver(dev, &bc->hdrv);
370 /* --------------------------------------------------------------------- */
372 enum uart { c_uart_unknown, c_uart_8250,
373 c_uart_16450, c_uart_16550, c_uart_16550A};
374 static const char *uart_str[] = {
375 "unknown", "8250", "16450", "16550", "16550A"
378 static enum uart ser12_check_uart(unsigned int iobase)
380 unsigned char b1,b2,b3;
382 enum uart uart_tab[] =
383 { c_uart_16450, c_uart_unknown, c_uart_16550, c_uart_16550A };
385 b1 = inb(MCR(iobase));
386 outb(b1 | 0x10, MCR(iobase)); /* loopback mode */
387 b2 = inb(MSR(iobase));
388 outb(0x1a, MCR(iobase));
389 b3 = inb(MSR(iobase)) & 0xf0;
390 outb(b1, MCR(iobase)); /* restore old values */
391 outb(b2, MSR(iobase));
393 return c_uart_unknown;
396 outb(0x01, FCR(iobase)); /* enable FIFOs */
397 u = uart_tab[(inb(IIR(iobase)) >> 6) & 3];
398 if (u == c_uart_16450) {
399 outb(0x5a, SCR(iobase));
400 b1 = inb(SCR(iobase));
401 outb(0xa5, SCR(iobase));
402 b2 = inb(SCR(iobase));
403 if ((b1 != 0x5a) || (b2 != 0xa5))
409 /* --------------------------------------------------------------------- */
411 static int ser12_open(struct net_device *dev)
413 struct baycom_state *bc = netdev_priv(dev);
418 if (!dev->base_addr || dev->base_addr > 0xffff-SER12_EXTENT ||
419 dev->irq < 2 || dev->irq > NR_IRQS) {
420 printk(KERN_INFO "baycom_ser_fdx: invalid portnumber (max %u) "
421 "or irq (2 <= irq <= %d)\n",
422 0xffff-SER12_EXTENT, NR_IRQS);
425 if (bc->baud < 300 || bc->baud > 4800) {
426 printk(KERN_INFO "baycom_ser_fdx: invalid baudrate "
430 if (!request_region(dev->base_addr, SER12_EXTENT, "baycom_ser_fdx")) {
431 printk(KERN_WARNING "BAYCOM_SER_FSX: I/O port 0x%04lx busy \n",
435 memset(&bc->modem, 0, sizeof(bc->modem));
436 bc->hdrv.par.bitrate = bc->baud;
437 bc->baud_us = 1000000/bc->baud;
438 bc->baud_uartdiv = (115200/8)/bc->baud;
439 if ((u = ser12_check_uart(dev->base_addr)) == c_uart_unknown){
440 release_region(dev->base_addr, SER12_EXTENT);
443 outb(0, FCR(dev->base_addr)); /* disable FIFOs */
444 outb(0x0d, MCR(dev->base_addr));
445 outb(0, IER(dev->base_addr));
446 if (request_irq(dev->irq, ser12_interrupt, IRQF_DISABLED | IRQF_SHARED,
447 "baycom_ser_fdx", dev)) {
448 release_region(dev->base_addr, SER12_EXTENT);
452 * set the SIO to 6 Bits/character; during receive,
453 * the baud rate is set to produce 100 ints/sec
454 * to feed the channel arbitration process,
455 * during transmit to baud ints/sec to run
458 ser12_set_divisor(dev, 115200/100/8);
460 * enable transmitter empty interrupt and modem status interrupt
462 outb(0x0a, IER(dev->base_addr));
464 * make sure the next interrupt is generated;
465 * 0 must be used to power the modem; the modem draws its
466 * power from the TxD line
468 outb(0x00, THR(dev->base_addr));
469 hdlcdrv_setdcd(&bc->hdrv, 0);
470 printk(KERN_INFO "%s: ser_fdx at iobase 0x%lx irq %u baud %u uart %s\n",
471 bc_drvname, dev->base_addr, dev->irq, bc->baud, uart_str[u]);
475 /* --------------------------------------------------------------------- */
477 static int ser12_close(struct net_device *dev)
479 struct baycom_state *bc = netdev_priv(dev);
486 outb(0, IER(dev->base_addr));
487 outb(1, MCR(dev->base_addr));
488 free_irq(dev->irq, dev);
489 release_region(dev->base_addr, SER12_EXTENT);
490 printk(KERN_INFO "%s: close ser_fdx at iobase 0x%lx irq %u\n",
491 bc_drvname, dev->base_addr, dev->irq);
495 /* --------------------------------------------------------------------- */
497 * ===================== hdlcdrv driver interface =========================
500 /* --------------------------------------------------------------------- */
502 static int baycom_ioctl(struct net_device *dev, struct ifreq *ifr,
503 struct hdlcdrv_ioctl *hi, int cmd);
505 /* --------------------------------------------------------------------- */
507 static struct hdlcdrv_ops ser12_ops = {
508 .drvname = bc_drvname,
509 .drvinfo = bc_drvinfo,
511 .close = ser12_close,
512 .ioctl = baycom_ioctl,
515 /* --------------------------------------------------------------------- */
517 static int baycom_setmode(struct baycom_state *bc, const char *modestr)
521 if (!strncmp(modestr, "ser", 3)) {
522 baud = simple_strtoul(modestr+3, NULL, 10);
523 if (baud >= 3 && baud <= 48)
526 if (strchr(modestr, '*'))
528 else if (strchr(modestr, '+'))
535 /* --------------------------------------------------------------------- */
537 static int baycom_ioctl(struct net_device *dev, struct ifreq *ifr,
538 struct hdlcdrv_ioctl *hi, int cmd)
540 struct baycom_state *bc;
541 struct baycom_ioctl bi;
546 bc = netdev_priv(dev);
547 BUG_ON(bc->hdrv.magic != HDLCDRV_MAGIC);
549 if (cmd != SIOCDEVPRIVATE)
555 case HDLCDRVCTL_GETMODE:
556 sprintf(hi->data.modename, "ser%u", bc->baud / 100);
557 if (bc->opt_dcd <= 0)
558 strcat(hi->data.modename, (!bc->opt_dcd) ? "*" : "+");
559 if (copy_to_user(ifr->ifr_data, hi, sizeof(struct hdlcdrv_ioctl)))
563 case HDLCDRVCTL_SETMODE:
564 if (netif_running(dev) || !capable(CAP_NET_ADMIN))
566 hi->data.modename[sizeof(hi->data.modename)-1] = '\0';
567 return baycom_setmode(bc, hi->data.modename);
569 case HDLCDRVCTL_MODELIST:
570 strcpy(hi->data.modename, "ser12,ser3,ser24");
571 if (copy_to_user(ifr->ifr_data, hi, sizeof(struct hdlcdrv_ioctl)))
575 case HDLCDRVCTL_MODEMPARMASK:
576 return HDLCDRV_PARMASK_IOBASE | HDLCDRV_PARMASK_IRQ;
580 if (copy_from_user(&bi, ifr->ifr_data, sizeof(bi)))
587 case BAYCOMCTL_GETDEBUG:
588 bi.data.dbg.debug1 = bc->hdrv.ptt_keyed;
589 bi.data.dbg.debug2 = bc->debug_vals.last_intcnt;
590 bi.data.dbg.debug3 = bc->debug_vals.last_pllcorr;
592 #endif /* BAYCOM_DEBUG */
595 if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
601 /* --------------------------------------------------------------------- */
604 * command line settable parameters
606 static char *mode[NR_PORTS] = { "ser12*", };
607 static int iobase[NR_PORTS] = { 0x3f8, };
608 static int irq[NR_PORTS] = { 4, };
609 static int baud[NR_PORTS] = { [0 ... NR_PORTS-1] = 1200 };
611 module_param_array(mode, charp, NULL, 0);
612 MODULE_PARM_DESC(mode, "baycom operating mode; * for software DCD");
613 module_param_array(iobase, int, NULL, 0);
614 MODULE_PARM_DESC(iobase, "baycom io base address");
615 module_param_array(irq, int, NULL, 0);
616 MODULE_PARM_DESC(irq, "baycom irq number");
617 module_param_array(baud, int, NULL, 0);
618 MODULE_PARM_DESC(baud, "baycom baud rate (300 to 4800)");
620 MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
621 MODULE_DESCRIPTION("Baycom ser12 full duplex amateur radio modem driver");
622 MODULE_LICENSE("GPL");
624 /* --------------------------------------------------------------------- */
626 static int __init init_baycomserfdx(void)
633 * register net devices
635 for (i = 0; i < NR_PORTS; i++) {
636 struct net_device *dev;
637 struct baycom_state *bc;
638 char ifname[IFNAMSIZ];
640 sprintf(ifname, "bcsf%d", i);
645 iobase[i] = irq[i] = 0;
647 dev = hdlcdrv_register(&ser12_ops,
648 sizeof(struct baycom_state),
649 ifname, iobase[i], irq[i], 0);
653 bc = netdev_priv(dev);
654 if (set_hw && baycom_setmode(bc, mode[i]))
658 baycom_device[i] = dev;
666 static void __exit cleanup_baycomserfdx(void)
670 for(i = 0; i < NR_PORTS; i++) {
671 struct net_device *dev = baycom_device[i];
673 hdlcdrv_unregister(dev);
677 module_init(init_baycomserfdx);
678 module_exit(cleanup_baycomserfdx);
680 /* --------------------------------------------------------------------- */
685 * format: baycom_ser_fdx=io,irq,mode
686 * mode: ser# hardware DCD
688 * ser#+ hardware DCD, inverted signal at DCD pin
689 * '#' denotes the baud rate / 100, eg. ser12* is '1200 baud, soft DCD'
692 static int __init baycom_ser_fdx_setup(char *str)
694 static unsigned nr_dev;
697 if (nr_dev >= NR_PORTS)
699 str = get_options(str, 4, ints);
703 iobase[nr_dev] = ints[1];
704 irq[nr_dev] = ints[2];
706 baud[nr_dev] = ints[3];
711 __setup("baycom_ser_fdx=", baycom_ser_fdx_setup);
714 /* --------------------------------------------------------------------- */