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