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