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