Linux Audio

Check our new training course

Open-source upstreaming

Need help get the support for your hardware in upstream Linux?
Loading...
v4.10.11
  1/*****************************************************************************/
  2
  3/*
  4 *	baycom_ser_fdx.c  -- baycom ser12 fullduplex radio modem driver.
  5 *
  6 *	Copyright (C) 1996-2000  Thomas Sailer (sailer@ife.ee.ethz.ch)
  7 *
  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.
 12 *
 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.
 17 *
 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.
 21 *
 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.
 25 *
 26 *
 27 *  Supported modems
 28 *
 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)
 36 *
 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.
 40 *
 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.
 45 *
 46 *  Command line options (insmod command line)
 47 *
 48 *  mode     ser#    hardware DCD
 49 *           ser#*   software 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
 55 *
 56 *
 57 *  History:
 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
 65 *                    reworked receiver
 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
 70 */
 71
 72/*****************************************************************************/
 73
 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>
 84
 85#include <linux/uaccess.h>
 86#include <asm/io.h>
 87#include <asm/irq.h>
 88
 89/* --------------------------------------------------------------------- */
 90
 91#define BAYCOM_DEBUG
 92
 93/* --------------------------------------------------------------------- */
 94
 95static const char bc_drvname[] = "baycom_ser_fdx";
 96static 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";
 98
 99/* --------------------------------------------------------------------- */
100
101#define NR_PORTS 4
102
103static struct net_device *baycom_device[NR_PORTS];
104
105/* --------------------------------------------------------------------- */
106
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)
119
120#define SER12_EXTENT 8
121
122/* ---------------------------------------------------------------------- */
123/*
124 * Information that need to be kept for each board.
125 */
126
127struct baycom_state {
128	struct hdlcdrv_state hdrv;
129
130	unsigned int baud, baud_us, baud_arbdiv, baud_uartdiv, baud_dcdtimeout;
131	int opt_dcd;
132
133	struct modem_state {
134		unsigned char flags;
135		unsigned char ptt;
136		unsigned int shreg;
137		struct modem_state_ser12 {
138			unsigned char tx_bit;
139			unsigned char last_rxbit;
140			int dcd_sum0, dcd_sum1, dcd_sum2;
141			int dcd_time;
142			unsigned int pll_time;
143			unsigned int txshreg;
144		} ser12;
145	} modem;
146
147#ifdef BAYCOM_DEBUG
148	struct debug_vals {
149		unsigned long last_jiffies;
150		unsigned cur_intcnt;
151		unsigned last_intcnt;
152		int cur_pllcorr;
153		int last_pllcorr;
154	} debug_vals;
155#endif /* BAYCOM_DEBUG */
156};
157
158/* --------------------------------------------------------------------- */
159
160static inline void baycom_int_freq(struct baycom_state *bc)
161{
162#ifdef BAYCOM_DEBUG
163	unsigned long cur_jiffies = jiffies;
164	/*
165	 * measure the interrupt frequency
166	 */
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;
174	}
175#endif /* BAYCOM_DEBUG */
176}
177
178/* --------------------------------------------------------------------- */
179/*
180 * ===================== SER12 specific routines =========================
181 */
182
183/* --------------------------------------------------------------------- */
184
185static inline void ser12_set_divisor(struct net_device *dev,
186                                     unsigned int divisor)
187{
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 */
192        /*
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
196         */
197        outb(0x00, THR(dev->base_addr));
198        /*
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)
203         */
204}
205
206/* --------------------------------------------------------------------- */
207
208#if 0
209static inline unsigned int hweight16(unsigned int w)
210        __attribute__ ((unused));
211static inline unsigned int hweight8(unsigned int w)
212        __attribute__ ((unused));
213
214static inline unsigned int hweight16(unsigned int w)
215{
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);
220}
221
222static inline unsigned int hweight8(unsigned int w)
223{
224        unsigned short res = (w & 0x55) + ((w >> 1) & 0x55);
225        res = (res & 0x33) + ((res >> 2) & 0x33);
226        return (res & 0x0F) + ((res >> 4) & 0x0F);
227}
228#endif
229
230/* --------------------------------------------------------------------- */
231
232static __inline__ void ser12_rx(struct net_device *dev, struct baycom_state *bc, struct timespec64 *ts, unsigned char curs)
233{
234	int timediff;
235	int bdus8 = bc->baud_us >> 3;
236	int bdus4 = bc->baud_us >> 2;
237	int bdus2 = bc->baud_us >> 1;
238
239	timediff = 1000000 + ts->tv_nsec / NSEC_PER_USEC -
240					bc->modem.ser12.pll_time;
241	while (timediff >= 500000)
242		timediff -= 1000000;
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;
251		}
252		/* add a one bit */
253		bc->modem.shreg >>= 1;
254	}
255	if (bc->modem.ser12.dcd_time <= 0) {
256		if (!bc->opt_dcd)
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;
264	}
265	if (bc->modem.ser12.last_rxbit != curs) {
266		bc->modem.ser12.last_rxbit = curs;
267		bc->modem.shreg |= 0x10000;
268		/* adjust the PLL */
269		if (timediff > 0)
270			bc->modem.ser12.pll_time += bdus8;
271		else
272			bc->modem.ser12.pll_time += 1000000 - bdus8;
273		/* update DCD */
274		if (abs(timediff) > bdus4)
275			bc->modem.ser12.dcd_sum0 += 4;
276		else
277			bc->modem.ser12.dcd_sum0--;
278#ifdef BAYCOM_DEBUG
279		bc->debug_vals.cur_pllcorr = timediff;
280#endif /* BAYCOM_DEBUG */
281	}
282	while (bc->modem.ser12.pll_time >= 1000000)
283		bc->modem.ser12.pll_time -= 1000000;
284}
285
286/* --------------------------------------------------------------------- */
287
288static irqreturn_t ser12_interrupt(int irq, void *dev_id)
289{
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;
295
296	if (!bc || bc->hdrv.magic != HDLCDRV_MAGIC)
297		return IRQ_NONE;
298	/* fast way out for shared irq */
299	if ((iir = inb(IIR(dev->base_addr))) & 1) 	
300		return IRQ_NONE;
301	/* get current time */
302	ktime_get_ts64(&ts);
303	msr = inb(MSR(dev->base_addr));
304	/* delta DCD */
305	if ((msr & 8) && bc->opt_dcd)
306		hdlcdrv_setdcd(&bc->hdrv, !((msr ^ bc->opt_dcd) & 0x80));
307	do {
308		switch (iir & 6) {
309		case 6:
310			inb(LSR(dev->base_addr));
311			break;
312			
313		case 4:
314			inb(RBR(dev->base_addr));
315			break;
316			
317		case 2:
318			/*
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
322			 */
323			outb(0x00, THR(dev->base_addr));
324			baycom_int_freq(bc);
325			txcount++;
326			/*
327			 * first output the last bit (!) then call HDLC transmitter,
328			 * since this may take quite long
329			 */
330			if (bc->modem.ptt)
331				outb(0x0e | (!!bc->modem.ser12.tx_bit), MCR(dev->base_addr));
332			else
333				outb(0x0d, MCR(dev->base_addr));       /* transmitter off */
334			break;
335			
336		default:
337			msr = inb(MSR(dev->base_addr));
338			/* delta DCD */
339			if ((msr & 8) && bc->opt_dcd) 
340				hdlcdrv_setdcd(&bc->hdrv, !((msr ^ bc->opt_dcd) & 0x80));
341			break;
342		}
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);
351				bc->modem.ptt = 0;
352				goto end_transmit;
353			}
354		}
355		bc->modem.ser12.tx_bit = !(bc->modem.ser12.tx_bit ^ (bc->modem.ser12.txshreg & 1));
356		bc->modem.ser12.txshreg >>= 1;
357	}
358 end_transmit:
359	local_irq_enable();
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;
365			bc->modem.ptt = 1;
366		}
367	}
368	hdlcdrv_transmitter(dev, &bc->hdrv);
369	hdlcdrv_receiver(dev, &bc->hdrv);
370	local_irq_disable();
371	return IRQ_HANDLED;
372}
373
374/* --------------------------------------------------------------------- */
375
376enum uart { c_uart_unknown, c_uart_8250,
377	    c_uart_16450, c_uart_16550, c_uart_16550A};
378static const char *uart_str[] = { 
379	"unknown", "8250", "16450", "16550", "16550A" 
380};
381
382static enum uart ser12_check_uart(unsigned int iobase)
383{
384	unsigned char b1,b2,b3;
385	enum uart u;
386	enum uart uart_tab[] =
387		{ c_uart_16450, c_uart_unknown, c_uart_16550, c_uart_16550A };
388
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));
396	if (b3 != 0x90)
397		return c_uart_unknown;
398	inb(RBR(iobase));
399	inb(RBR(iobase));
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))
408			u = c_uart_8250;
409	}
410	return u;
411}
412
413/* --------------------------------------------------------------------- */
414
415static int ser12_open(struct net_device *dev)
416{
417	struct baycom_state *bc = netdev_priv(dev);
418	enum uart u;
419
420	if (!dev || !bc)
421		return -ENXIO;
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);
427		return -ENXIO;
428	}
429	if (bc->baud < 300 || bc->baud > 4800) {
430		printk(KERN_INFO "baycom_ser_fdx: invalid baudrate "
431				"(300...4800)\n");
432		return -EINVAL;
433	}
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",
436		       dev->base_addr);
437		return -EACCES;
438	}
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);
445		return -EIO;
446	}
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);
453		return -EBUSY;
454	}
455	/*
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
460	 * the transmitter
461	 */
462	ser12_set_divisor(dev, 115200/100/8);
463	/*
464	 * enable transmitter empty interrupt and modem status interrupt
465	 */
466	outb(0x0a, IER(dev->base_addr));
467	/*
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
471	 */
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]);
476	return 0;
477}
478
479/* --------------------------------------------------------------------- */
480
481static int ser12_close(struct net_device *dev)
482{
483	struct baycom_state *bc = netdev_priv(dev);
484
485	if (!dev || !bc)
486		return -EINVAL;
487	/*
488	 * disable interrupts
489	 */
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);
496	return 0;
497}
498
499/* --------------------------------------------------------------------- */
500/*
501 * ===================== hdlcdrv driver interface =========================
502 */
503
504/* --------------------------------------------------------------------- */
505
506static int baycom_ioctl(struct net_device *dev, struct ifreq *ifr,
507			struct hdlcdrv_ioctl *hi, int cmd);
508
509/* --------------------------------------------------------------------- */
510
511static struct hdlcdrv_ops ser12_ops = {
512	.drvname = bc_drvname,
513	.drvinfo = bc_drvinfo,
514	.open    = ser12_open,
515	.close   = ser12_close,
516	.ioctl   = baycom_ioctl,
517};
518
519/* --------------------------------------------------------------------- */
520
521static int baycom_setmode(struct baycom_state *bc, const char *modestr)
522{
523	unsigned int baud;
524
525	if (!strncmp(modestr, "ser", 3)) {
526		baud = simple_strtoul(modestr+3, NULL, 10);
527		if (baud >= 3 && baud <= 48)
528			bc->baud = baud*100;
529	}
530	if (strchr(modestr, '*'))
531		bc->opt_dcd = 0;
532	else if (strchr(modestr, '+'))
533		bc->opt_dcd = -1;
534	else
535		bc->opt_dcd = 1;
536	return 0;
537}
538
539/* --------------------------------------------------------------------- */
540
541static int baycom_ioctl(struct net_device *dev, struct ifreq *ifr,
542			struct hdlcdrv_ioctl *hi, int cmd)
543{
544	struct baycom_state *bc;
545	struct baycom_ioctl bi;
546
547	if (!dev)
548		return -EINVAL;
549
550	bc = netdev_priv(dev);
551	BUG_ON(bc->hdrv.magic != HDLCDRV_MAGIC);
552
553	if (cmd != SIOCDEVPRIVATE)
554		return -ENOIOCTLCMD;
555	switch (hi->cmd) {
556	default:
557		break;
558
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)))
564			return -EFAULT;
565		return 0;
566
567	case HDLCDRVCTL_SETMODE:
568		if (netif_running(dev) || !capable(CAP_NET_ADMIN))
569			return -EACCES;
570		hi->data.modename[sizeof(hi->data.modename)-1] = '\0';
571		return baycom_setmode(bc, hi->data.modename);
572
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)))
576			return -EFAULT;
577		return 0;
578
579	case HDLCDRVCTL_MODEMPARMASK:
580		return HDLCDRV_PARMASK_IOBASE | HDLCDRV_PARMASK_IRQ;
581
582	}
583
584	if (copy_from_user(&bi, ifr->ifr_data, sizeof(bi)))
585		return -EFAULT;
586	switch (bi.cmd) {
587	default:
588		return -ENOIOCTLCMD;
589
590#ifdef BAYCOM_DEBUG
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;
595		break;
596#endif /* BAYCOM_DEBUG */
597
598	}
599	if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
600		return -EFAULT;
601	return 0;
602
603}
604
605/* --------------------------------------------------------------------- */
606
607/*
608 * command line settable parameters
609 */
610static char *mode[NR_PORTS] = { "ser12*", };
611static int iobase[NR_PORTS] = { 0x3f8, };
612static int irq[NR_PORTS] = { 4, };
613static int baud[NR_PORTS] = { [0 ... NR_PORTS-1] = 1200 };
614
615module_param_array(mode, charp, NULL, 0);
616MODULE_PARM_DESC(mode, "baycom operating mode; * for software DCD");
617module_param_array(iobase, int, NULL, 0);
618MODULE_PARM_DESC(iobase, "baycom io base address");
619module_param_array(irq, int, NULL, 0);
620MODULE_PARM_DESC(irq, "baycom irq number");
621module_param_array(baud, int, NULL, 0);
622MODULE_PARM_DESC(baud, "baycom baud rate (300 to 4800)");
623
624MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
625MODULE_DESCRIPTION("Baycom ser12 full duplex amateur radio modem driver");
626MODULE_LICENSE("GPL");
627
628/* --------------------------------------------------------------------- */
629
630static int __init init_baycomserfdx(void)
631{
632	int i, found = 0;
633	char set_hw = 1;
634
635	printk(bc_drvinfo);
636	/*
637	 * register net devices
638	 */
639	for (i = 0; i < NR_PORTS; i++) {
640		struct net_device *dev;
641		struct baycom_state *bc;
642		char ifname[IFNAMSIZ];
643
644		sprintf(ifname, "bcsf%d", i);
645
646		if (!mode[i])
647			set_hw = 0;
648		if (!set_hw)
649			iobase[i] = irq[i] = 0;
650
651		dev = hdlcdrv_register(&ser12_ops, 
652				       sizeof(struct baycom_state),
653				       ifname, iobase[i], irq[i], 0);
654		if (IS_ERR(dev)) 
655			break;
656
657		bc = netdev_priv(dev);
658		if (set_hw && baycom_setmode(bc, mode[i]))
659			set_hw = 0;
660		bc->baud = baud[i];
661		found++;
662		baycom_device[i] = dev;
663	}
664
665	if (!found)
666		return -ENXIO;
667	return 0;
668}
669
670static void __exit cleanup_baycomserfdx(void)
671{
672	int i;
673
674	for(i = 0; i < NR_PORTS; i++) {
675		struct net_device *dev = baycom_device[i];
676		if (dev) 
677			hdlcdrv_unregister(dev);
678	}
679}
680
681module_init(init_baycomserfdx);
682module_exit(cleanup_baycomserfdx);
683
684/* --------------------------------------------------------------------- */
685
686#ifndef MODULE
687
688/*
689 * format: baycom_ser_fdx=io,irq,mode
690 * mode: ser#    hardware DCD
691 *       ser#*   software DCD
692 *       ser#+   hardware DCD, inverted signal at DCD pin
693 * '#' denotes the baud rate / 100, eg. ser12* is '1200 baud, soft DCD'
694 */
695
696static int __init baycom_ser_fdx_setup(char *str)
697{
698        static unsigned nr_dev;
699        int ints[4];
700
701        if (nr_dev >= NR_PORTS)
702                return 0;
703        str = get_options(str, 4, ints);
704        if (ints[0] < 2)
705                return 0;
706        mode[nr_dev] = str;
707        iobase[nr_dev] = ints[1];
708        irq[nr_dev] = ints[2];
709	if (ints[0] >= 3)
710		baud[nr_dev] = ints[3];
711	nr_dev++;
712	return 1;
713}
714
715__setup("baycom_ser_fdx=", baycom_ser_fdx_setup);
716
717#endif /* MODULE */
718/* --------------------------------------------------------------------- */
v3.1
  1/*****************************************************************************/
  2
  3/*
  4 *	baycom_ser_fdx.c  -- baycom ser12 fullduplex radio modem driver.
  5 *
  6 *	Copyright (C) 1996-2000  Thomas Sailer (sailer@ife.ee.ethz.ch)
  7 *
  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.
 12 *
 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.
 17 *
 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.
 21 *
 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.
 25 *
 26 *
 27 *  Supported modems
 28 *
 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)
 36 *
 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.
 40 *
 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.
 45 *
 46 *  Command line options (insmod command line)
 47 *
 48 *  mode     ser#    hardware DCD
 49 *           ser#*   software 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
 55 *
 56 *
 57 *  History:
 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
 65 *                    reworked receiver
 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
 70 */
 71
 72/*****************************************************************************/
 73
 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
 84#include <asm/uaccess.h>
 85#include <asm/io.h>
 86#include <asm/irq.h>
 87
 88/* --------------------------------------------------------------------- */
 89
 90#define BAYCOM_DEBUG
 91
 92/* --------------------------------------------------------------------- */
 93
 94static const char bc_drvname[] = "baycom_ser_fdx";
 95static const char bc_drvinfo[] = KERN_INFO "baycom_ser_fdx: (C) 1996-2000 Thomas Sailer, HB9JNX/AE4WA\n"
 96"baycom_ser_fdx: version 0.10\n";
 97
 98/* --------------------------------------------------------------------- */
 99
100#define NR_PORTS 4
101
102static struct net_device *baycom_device[NR_PORTS];
103
104/* --------------------------------------------------------------------- */
105
106#define RBR(iobase) (iobase+0)
107#define THR(iobase) (iobase+0)
108#define IER(iobase) (iobase+1)
109#define IIR(iobase) (iobase+2)
110#define FCR(iobase) (iobase+2)
111#define LCR(iobase) (iobase+3)
112#define MCR(iobase) (iobase+4)
113#define LSR(iobase) (iobase+5)
114#define MSR(iobase) (iobase+6)
115#define SCR(iobase) (iobase+7)
116#define DLL(iobase) (iobase+0)
117#define DLM(iobase) (iobase+1)
118
119#define SER12_EXTENT 8
120
121/* ---------------------------------------------------------------------- */
122/*
123 * Information that need to be kept for each board.
124 */
125
126struct baycom_state {
127	struct hdlcdrv_state hdrv;
128
129	unsigned int baud, baud_us, baud_arbdiv, baud_uartdiv, baud_dcdtimeout;
130	int opt_dcd;
131
132	struct modem_state {
133		unsigned char flags;
134		unsigned char ptt;
135		unsigned int shreg;
136		struct modem_state_ser12 {
137			unsigned char tx_bit;
138			unsigned char last_rxbit;
139			int dcd_sum0, dcd_sum1, dcd_sum2;
140			int dcd_time;
141			unsigned int pll_time;
142			unsigned int txshreg;
143		} ser12;
144	} modem;
145
146#ifdef BAYCOM_DEBUG
147	struct debug_vals {
148		unsigned long last_jiffies;
149		unsigned cur_intcnt;
150		unsigned last_intcnt;
151		int cur_pllcorr;
152		int last_pllcorr;
153	} debug_vals;
154#endif /* BAYCOM_DEBUG */
155};
156
157/* --------------------------------------------------------------------- */
158
159static inline void baycom_int_freq(struct baycom_state *bc)
160{
161#ifdef BAYCOM_DEBUG
162	unsigned long cur_jiffies = jiffies;
163	/*
164	 * measure the interrupt frequency
165	 */
166	bc->debug_vals.cur_intcnt++;
167	if (time_after_eq(cur_jiffies, bc->debug_vals.last_jiffies + HZ)) {
168		bc->debug_vals.last_jiffies = cur_jiffies;
169		bc->debug_vals.last_intcnt = bc->debug_vals.cur_intcnt;
170		bc->debug_vals.cur_intcnt = 0;
171		bc->debug_vals.last_pllcorr = bc->debug_vals.cur_pllcorr;
172		bc->debug_vals.cur_pllcorr = 0;
173	}
174#endif /* BAYCOM_DEBUG */
175}
176
177/* --------------------------------------------------------------------- */
178/*
179 * ===================== SER12 specific routines =========================
180 */
181
182/* --------------------------------------------------------------------- */
183
184static inline void ser12_set_divisor(struct net_device *dev,
185                                     unsigned int divisor)
186{
187        outb(0x81, LCR(dev->base_addr));        /* DLAB = 1 */
188        outb(divisor, DLL(dev->base_addr));
189        outb(divisor >> 8, DLM(dev->base_addr));
190        outb(0x01, LCR(dev->base_addr));        /* word length = 6 */
191        /*
192         * make sure the next interrupt is generated;
193         * 0 must be used to power the modem; the modem draws its
194         * power from the TxD line
195         */
196        outb(0x00, THR(dev->base_addr));
197        /*
198         * it is important not to set the divider while transmitting;
199         * this reportedly makes some UARTs generating interrupts
200         * in the hundredthousands per second region
201         * Reported by: Ignacio.Arenaza@studi.epfl.ch (Ignacio Arenaza Nuno)
202         */
203}
204
205/* --------------------------------------------------------------------- */
206
207#if 0
208static inline unsigned int hweight16(unsigned int w)
209        __attribute__ ((unused));
210static inline unsigned int hweight8(unsigned int w)
211        __attribute__ ((unused));
212
213static inline unsigned int hweight16(unsigned int w)
214{
215        unsigned short res = (w & 0x5555) + ((w >> 1) & 0x5555);
216        res = (res & 0x3333) + ((res >> 2) & 0x3333);
217        res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F);
218        return (res & 0x00FF) + ((res >> 8) & 0x00FF);
219}
220
221static inline unsigned int hweight8(unsigned int w)
222{
223        unsigned short res = (w & 0x55) + ((w >> 1) & 0x55);
224        res = (res & 0x33) + ((res >> 2) & 0x33);
225        return (res & 0x0F) + ((res >> 4) & 0x0F);
226}
227#endif
228
229/* --------------------------------------------------------------------- */
230
231static __inline__ void ser12_rx(struct net_device *dev, struct baycom_state *bc, struct timeval *tv, unsigned char curs)
232{
233	int timediff;
234	int bdus8 = bc->baud_us >> 3;
235	int bdus4 = bc->baud_us >> 2;
236	int bdus2 = bc->baud_us >> 1;
237
238	timediff = 1000000 + tv->tv_usec - bc->modem.ser12.pll_time;
 
239	while (timediff >= 500000)
240		timediff -= 1000000;
241	while (timediff >= bdus2) {
242		timediff -= bc->baud_us;
243		bc->modem.ser12.pll_time += bc->baud_us;
244		bc->modem.ser12.dcd_time--;
245		/* first check if there is room to add a bit */
246		if (bc->modem.shreg & 1) {
247			hdlcdrv_putbits(&bc->hdrv, (bc->modem.shreg >> 1) ^ 0xffff);
248			bc->modem.shreg = 0x10000;
249		}
250		/* add a one bit */
251		bc->modem.shreg >>= 1;
252	}
253	if (bc->modem.ser12.dcd_time <= 0) {
254		if (!bc->opt_dcd)
255			hdlcdrv_setdcd(&bc->hdrv, (bc->modem.ser12.dcd_sum0 + 
256						   bc->modem.ser12.dcd_sum1 + 
257						   bc->modem.ser12.dcd_sum2) < 0);
258		bc->modem.ser12.dcd_sum2 = bc->modem.ser12.dcd_sum1;
259		bc->modem.ser12.dcd_sum1 = bc->modem.ser12.dcd_sum0;
260		bc->modem.ser12.dcd_sum0 = 2; /* slight bias */
261		bc->modem.ser12.dcd_time += 120;
262	}
263	if (bc->modem.ser12.last_rxbit != curs) {
264		bc->modem.ser12.last_rxbit = curs;
265		bc->modem.shreg |= 0x10000;
266		/* adjust the PLL */
267		if (timediff > 0)
268			bc->modem.ser12.pll_time += bdus8;
269		else
270			bc->modem.ser12.pll_time += 1000000 - bdus8;
271		/* update DCD */
272		if (abs(timediff) > bdus4)
273			bc->modem.ser12.dcd_sum0 += 4;
274		else
275			bc->modem.ser12.dcd_sum0--;
276#ifdef BAYCOM_DEBUG
277		bc->debug_vals.cur_pllcorr = timediff;
278#endif /* BAYCOM_DEBUG */
279	}
280	while (bc->modem.ser12.pll_time >= 1000000)
281		bc->modem.ser12.pll_time -= 1000000;
282}
283
284/* --------------------------------------------------------------------- */
285
286static irqreturn_t ser12_interrupt(int irq, void *dev_id)
287{
288	struct net_device *dev = (struct net_device *)dev_id;
289	struct baycom_state *bc = netdev_priv(dev);
290	struct timeval tv;
291	unsigned char iir, msr;
292	unsigned int txcount = 0;
293
294	if (!bc || bc->hdrv.magic != HDLCDRV_MAGIC)
295		return IRQ_NONE;
296	/* fast way out for shared irq */
297	if ((iir = inb(IIR(dev->base_addr))) & 1) 	
298		return IRQ_NONE;
299	/* get current time */
300	do_gettimeofday(&tv);
301	msr = inb(MSR(dev->base_addr));
302	/* delta DCD */
303	if ((msr & 8) && bc->opt_dcd)
304		hdlcdrv_setdcd(&bc->hdrv, !((msr ^ bc->opt_dcd) & 0x80));
305	do {
306		switch (iir & 6) {
307		case 6:
308			inb(LSR(dev->base_addr));
309			break;
310			
311		case 4:
312			inb(RBR(dev->base_addr));
313			break;
314			
315		case 2:
316			/*
317			 * make sure the next interrupt is generated;
318			 * 0 must be used to power the modem; the modem draws its
319			 * power from the TxD line
320			 */
321			outb(0x00, THR(dev->base_addr));
322			baycom_int_freq(bc);
323			txcount++;
324			/*
325			 * first output the last bit (!) then call HDLC transmitter,
326			 * since this may take quite long
327			 */
328			if (bc->modem.ptt)
329				outb(0x0e | (!!bc->modem.ser12.tx_bit), MCR(dev->base_addr));
330			else
331				outb(0x0d, MCR(dev->base_addr));       /* transmitter off */
332			break;
333			
334		default:
335			msr = inb(MSR(dev->base_addr));
336			/* delta DCD */
337			if ((msr & 8) && bc->opt_dcd) 
338				hdlcdrv_setdcd(&bc->hdrv, !((msr ^ bc->opt_dcd) & 0x80));
339			break;
340		}
341		iir = inb(IIR(dev->base_addr));
342	} while (!(iir & 1));
343	ser12_rx(dev, bc, &tv, msr & 0x10); /* CTS */
344	if (bc->modem.ptt && txcount) {
345		if (bc->modem.ser12.txshreg <= 1) {
346			bc->modem.ser12.txshreg = 0x10000 | hdlcdrv_getbits(&bc->hdrv);
347			if (!hdlcdrv_ptt(&bc->hdrv)) {
348				ser12_set_divisor(dev, 115200/100/8);
349				bc->modem.ptt = 0;
350				goto end_transmit;
351			}
352		}
353		bc->modem.ser12.tx_bit = !(bc->modem.ser12.tx_bit ^ (bc->modem.ser12.txshreg & 1));
354		bc->modem.ser12.txshreg >>= 1;
355	}
356 end_transmit:
357	local_irq_enable();
358	if (!bc->modem.ptt && txcount) {
359		hdlcdrv_arbitrate(dev, &bc->hdrv);
360		if (hdlcdrv_ptt(&bc->hdrv)) {
361			ser12_set_divisor(dev, bc->baud_uartdiv);
362			bc->modem.ser12.txshreg = 1;
363			bc->modem.ptt = 1;
364		}
365	}
366	hdlcdrv_transmitter(dev, &bc->hdrv);
367	hdlcdrv_receiver(dev, &bc->hdrv);
368	local_irq_disable();
369	return IRQ_HANDLED;
370}
371
372/* --------------------------------------------------------------------- */
373
374enum uart { c_uart_unknown, c_uart_8250,
375	    c_uart_16450, c_uart_16550, c_uart_16550A};
376static const char *uart_str[] = { 
377	"unknown", "8250", "16450", "16550", "16550A" 
378};
379
380static enum uart ser12_check_uart(unsigned int iobase)
381{
382	unsigned char b1,b2,b3;
383	enum uart u;
384	enum uart uart_tab[] =
385		{ c_uart_16450, c_uart_unknown, c_uart_16550, c_uart_16550A };
386
387	b1 = inb(MCR(iobase));
388	outb(b1 | 0x10, MCR(iobase));	/* loopback mode */
389	b2 = inb(MSR(iobase));
390	outb(0x1a, MCR(iobase));
391	b3 = inb(MSR(iobase)) & 0xf0;
392	outb(b1, MCR(iobase));			/* restore old values */
393	outb(b2, MSR(iobase));
394	if (b3 != 0x90)
395		return c_uart_unknown;
396	inb(RBR(iobase));
397	inb(RBR(iobase));
398	outb(0x01, FCR(iobase));		/* enable FIFOs */
399	u = uart_tab[(inb(IIR(iobase)) >> 6) & 3];
400	if (u == c_uart_16450) {
401		outb(0x5a, SCR(iobase));
402		b1 = inb(SCR(iobase));
403		outb(0xa5, SCR(iobase));
404		b2 = inb(SCR(iobase));
405		if ((b1 != 0x5a) || (b2 != 0xa5))
406			u = c_uart_8250;
407	}
408	return u;
409}
410
411/* --------------------------------------------------------------------- */
412
413static int ser12_open(struct net_device *dev)
414{
415	struct baycom_state *bc = netdev_priv(dev);
416	enum uart u;
417
418	if (!dev || !bc)
419		return -ENXIO;
420	if (!dev->base_addr || dev->base_addr > 0xffff-SER12_EXTENT ||
421	    dev->irq < 2 || dev->irq > nr_irqs) {
422		printk(KERN_INFO "baycom_ser_fdx: invalid portnumber (max %u) "
423				"or irq (2 <= irq <= %d)\n",
424				0xffff-SER12_EXTENT, nr_irqs);
425		return -ENXIO;
426	}
427	if (bc->baud < 300 || bc->baud > 4800) {
428		printk(KERN_INFO "baycom_ser_fdx: invalid baudrate "
429				"(300...4800)\n");
430		return -EINVAL;
431	}
432	if (!request_region(dev->base_addr, SER12_EXTENT, "baycom_ser_fdx")) {
433		printk(KERN_WARNING "BAYCOM_SER_FSX: I/O port 0x%04lx busy\n",
434		       dev->base_addr);
435		return -EACCES;
436	}
437	memset(&bc->modem, 0, sizeof(bc->modem));
438	bc->hdrv.par.bitrate = bc->baud;
439	bc->baud_us = 1000000/bc->baud;
440	bc->baud_uartdiv = (115200/8)/bc->baud;
441	if ((u = ser12_check_uart(dev->base_addr)) == c_uart_unknown){
442		release_region(dev->base_addr, SER12_EXTENT);
443		return -EIO;
444	}
445	outb(0, FCR(dev->base_addr));  /* disable FIFOs */
446	outb(0x0d, MCR(dev->base_addr));
447	outb(0, IER(dev->base_addr));
448	if (request_irq(dev->irq, ser12_interrupt, IRQF_DISABLED | IRQF_SHARED,
449			"baycom_ser_fdx", dev)) {
450		release_region(dev->base_addr, SER12_EXTENT);
451		return -EBUSY;
452	}
453	/*
454	 * set the SIO to 6 Bits/character; during receive,
455	 * the baud rate is set to produce 100 ints/sec
456	 * to feed the channel arbitration process,
457	 * during transmit to baud ints/sec to run
458	 * the transmitter
459	 */
460	ser12_set_divisor(dev, 115200/100/8);
461	/*
462	 * enable transmitter empty interrupt and modem status interrupt
463	 */
464	outb(0x0a, IER(dev->base_addr));
465	/*
466	 * make sure the next interrupt is generated;
467	 * 0 must be used to power the modem; the modem draws its
468	 * power from the TxD line
469	 */
470	outb(0x00, THR(dev->base_addr));
471	hdlcdrv_setdcd(&bc->hdrv, 0);
472	printk(KERN_INFO "%s: ser_fdx at iobase 0x%lx irq %u baud %u uart %s\n",
473	       bc_drvname, dev->base_addr, dev->irq, bc->baud, uart_str[u]);
474	return 0;
475}
476
477/* --------------------------------------------------------------------- */
478
479static int ser12_close(struct net_device *dev)
480{
481	struct baycom_state *bc = netdev_priv(dev);
482
483	if (!dev || !bc)
484		return -EINVAL;
485	/*
486	 * disable interrupts
487	 */
488	outb(0, IER(dev->base_addr));
489	outb(1, MCR(dev->base_addr));
490	free_irq(dev->irq, dev);
491	release_region(dev->base_addr, SER12_EXTENT);
492	printk(KERN_INFO "%s: close ser_fdx at iobase 0x%lx irq %u\n",
493	       bc_drvname, dev->base_addr, dev->irq);
494	return 0;
495}
496
497/* --------------------------------------------------------------------- */
498/*
499 * ===================== hdlcdrv driver interface =========================
500 */
501
502/* --------------------------------------------------------------------- */
503
504static int baycom_ioctl(struct net_device *dev, struct ifreq *ifr,
505			struct hdlcdrv_ioctl *hi, int cmd);
506
507/* --------------------------------------------------------------------- */
508
509static struct hdlcdrv_ops ser12_ops = {
510	.drvname = bc_drvname,
511	.drvinfo = bc_drvinfo,
512	.open    = ser12_open,
513	.close   = ser12_close,
514	.ioctl   = baycom_ioctl,
515};
516
517/* --------------------------------------------------------------------- */
518
519static int baycom_setmode(struct baycom_state *bc, const char *modestr)
520{
521	unsigned int baud;
522
523	if (!strncmp(modestr, "ser", 3)) {
524		baud = simple_strtoul(modestr+3, NULL, 10);
525		if (baud >= 3 && baud <= 48)
526			bc->baud = baud*100;
527	}
528	if (strchr(modestr, '*'))
529		bc->opt_dcd = 0;
530	else if (strchr(modestr, '+'))
531		bc->opt_dcd = -1;
532	else
533		bc->opt_dcd = 1;
534	return 0;
535}
536
537/* --------------------------------------------------------------------- */
538
539static int baycom_ioctl(struct net_device *dev, struct ifreq *ifr,
540			struct hdlcdrv_ioctl *hi, int cmd)
541{
542	struct baycom_state *bc;
543	struct baycom_ioctl bi;
544
545	if (!dev)
546		return -EINVAL;
547
548	bc = netdev_priv(dev);
549	BUG_ON(bc->hdrv.magic != HDLCDRV_MAGIC);
550
551	if (cmd != SIOCDEVPRIVATE)
552		return -ENOIOCTLCMD;
553	switch (hi->cmd) {
554	default:
555		break;
556
557	case HDLCDRVCTL_GETMODE:
558		sprintf(hi->data.modename, "ser%u", bc->baud / 100);
559		if (bc->opt_dcd <= 0)
560			strcat(hi->data.modename, (!bc->opt_dcd) ? "*" : "+");
561		if (copy_to_user(ifr->ifr_data, hi, sizeof(struct hdlcdrv_ioctl)))
562			return -EFAULT;
563		return 0;
564
565	case HDLCDRVCTL_SETMODE:
566		if (netif_running(dev) || !capable(CAP_NET_ADMIN))
567			return -EACCES;
568		hi->data.modename[sizeof(hi->data.modename)-1] = '\0';
569		return baycom_setmode(bc, hi->data.modename);
570
571	case HDLCDRVCTL_MODELIST:
572		strcpy(hi->data.modename, "ser12,ser3,ser24");
573		if (copy_to_user(ifr->ifr_data, hi, sizeof(struct hdlcdrv_ioctl)))
574			return -EFAULT;
575		return 0;
576
577	case HDLCDRVCTL_MODEMPARMASK:
578		return HDLCDRV_PARMASK_IOBASE | HDLCDRV_PARMASK_IRQ;
579
580	}
581
582	if (copy_from_user(&bi, ifr->ifr_data, sizeof(bi)))
583		return -EFAULT;
584	switch (bi.cmd) {
585	default:
586		return -ENOIOCTLCMD;
587
588#ifdef BAYCOM_DEBUG
589	case BAYCOMCTL_GETDEBUG:
590		bi.data.dbg.debug1 = bc->hdrv.ptt_keyed;
591		bi.data.dbg.debug2 = bc->debug_vals.last_intcnt;
592		bi.data.dbg.debug3 = bc->debug_vals.last_pllcorr;
593		break;
594#endif /* BAYCOM_DEBUG */
595
596	}
597	if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
598		return -EFAULT;
599	return 0;
600
601}
602
603/* --------------------------------------------------------------------- */
604
605/*
606 * command line settable parameters
607 */
608static char *mode[NR_PORTS] = { "ser12*", };
609static int iobase[NR_PORTS] = { 0x3f8, };
610static int irq[NR_PORTS] = { 4, };
611static int baud[NR_PORTS] = { [0 ... NR_PORTS-1] = 1200 };
612
613module_param_array(mode, charp, NULL, 0);
614MODULE_PARM_DESC(mode, "baycom operating mode; * for software DCD");
615module_param_array(iobase, int, NULL, 0);
616MODULE_PARM_DESC(iobase, "baycom io base address");
617module_param_array(irq, int, NULL, 0);
618MODULE_PARM_DESC(irq, "baycom irq number");
619module_param_array(baud, int, NULL, 0);
620MODULE_PARM_DESC(baud, "baycom baud rate (300 to 4800)");
621
622MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
623MODULE_DESCRIPTION("Baycom ser12 full duplex amateur radio modem driver");
624MODULE_LICENSE("GPL");
625
626/* --------------------------------------------------------------------- */
627
628static int __init init_baycomserfdx(void)
629{
630	int i, found = 0;
631	char set_hw = 1;
632
633	printk(bc_drvinfo);
634	/*
635	 * register net devices
636	 */
637	for (i = 0; i < NR_PORTS; i++) {
638		struct net_device *dev;
639		struct baycom_state *bc;
640		char ifname[IFNAMSIZ];
641
642		sprintf(ifname, "bcsf%d", i);
643
644		if (!mode[i])
645			set_hw = 0;
646		if (!set_hw)
647			iobase[i] = irq[i] = 0;
648
649		dev = hdlcdrv_register(&ser12_ops, 
650				       sizeof(struct baycom_state),
651				       ifname, iobase[i], irq[i], 0);
652		if (IS_ERR(dev)) 
653			break;
654
655		bc = netdev_priv(dev);
656		if (set_hw && baycom_setmode(bc, mode[i]))
657			set_hw = 0;
658		bc->baud = baud[i];
659		found++;
660		baycom_device[i] = dev;
661	}
662
663	if (!found)
664		return -ENXIO;
665	return 0;
666}
667
668static void __exit cleanup_baycomserfdx(void)
669{
670	int i;
671
672	for(i = 0; i < NR_PORTS; i++) {
673		struct net_device *dev = baycom_device[i];
674		if (dev) 
675			hdlcdrv_unregister(dev);
676	}
677}
678
679module_init(init_baycomserfdx);
680module_exit(cleanup_baycomserfdx);
681
682/* --------------------------------------------------------------------- */
683
684#ifndef MODULE
685
686/*
687 * format: baycom_ser_fdx=io,irq,mode
688 * mode: ser#    hardware DCD
689 *       ser#*   software DCD
690 *       ser#+   hardware DCD, inverted signal at DCD pin
691 * '#' denotes the baud rate / 100, eg. ser12* is '1200 baud, soft DCD'
692 */
693
694static int __init baycom_ser_fdx_setup(char *str)
695{
696        static unsigned nr_dev;
697        int ints[4];
698
699        if (nr_dev >= NR_PORTS)
700                return 0;
701        str = get_options(str, 4, ints);
702        if (ints[0] < 2)
703                return 0;
704        mode[nr_dev] = str;
705        iobase[nr_dev] = ints[1];
706        irq[nr_dev] = ints[2];
707	if (ints[0] >= 3)
708		baud[nr_dev] = ints[3];
709	nr_dev++;
710	return 1;
711}
712
713__setup("baycom_ser_fdx=", baycom_ser_fdx_setup);
714
715#endif /* MODULE */
716/* --------------------------------------------------------------------- */