1 /*****************************************************************************/
4 * baycom_epp.c -- baycom epp radio modem driver.
6 * Copyright (C) 1998-2000
7 * Thomas Sailer (sailer@ife.ee.ethz.ch)
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * Please note that the GPL allows you to use the driver, NOT the radio.
24 * In order to use the radio, you need a license from the communications
25 * authority of your country.
29 * 0.1 xx.xx.1998 Initial version by Matthias Welwarsky (dg2fef)
30 * 0.2 21.04.1998 Massive rework by Thomas Sailer
31 * Integrated FPGA EPP modem configuration routines
32 * 0.3 11.05.1998 Took FPGA config out and moved it into a separate program
33 * 0.4 26.07.1999 Adapted to new lowlevel parport driver interface
34 * 0.5 03.08.1999 adapt to Linus' new __setup/__initcall
35 * removed some pre-2.2 kernel compatibility cruft
36 * 0.6 10.08.1999 Check if parport can do SPP and is safe to access during interrupt contexts
37 * 0.7 12.02.2000 adapted to softnet driver interface
41 /*****************************************************************************/
43 #include <linux/config.h>
44 #include <linux/module.h>
45 #include <linux/kernel.h>
46 #include <linux/init.h>
47 #include <linux/string.h>
48 #include <linux/workqueue.h>
50 #include <linux/parport.h>
51 #include <linux/smp_lock.h>
52 #include <asm/uaccess.h>
53 #include <linux/if_arp.h>
54 #include <linux/kmod.h>
55 #include <linux/hdlcdrv.h>
56 #include <linux/baycom.h>
57 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
58 /* prototypes for ax25_encapsulate and ax25_rebuild_header */
60 #endif /* CONFIG_AX25 || CONFIG_AX25_MODULE */
62 /* --------------------------------------------------------------------- */
65 #define BAYCOM_MAGIC 19730510
67 /* --------------------------------------------------------------------- */
69 static const char paranoia_str[] = KERN_ERR
70 "baycom_epp: bad magic number for hdlcdrv_state struct in routine %s\n";
72 #define baycom_paranoia_check(dev,routine,retval) \
74 if (!dev || !dev->priv || ((struct baycom_state *)dev->priv)->magic != BAYCOM_MAGIC) { \
75 printk(paranoia_str, routine); \
80 #define baycom_paranoia_check_void(dev,routine) \
82 if (!dev || !dev->priv || ((struct baycom_state *)dev->priv)->magic != BAYCOM_MAGIC) { \
83 printk(paranoia_str, routine); \
88 /* --------------------------------------------------------------------- */
90 static const char bc_drvname[] = "baycom_epp";
91 static const char bc_drvinfo[] = KERN_INFO "baycom_epp: (C) 1998-2000 Thomas Sailer, HB9JNX/AE4WA\n"
92 KERN_INFO "baycom_epp: version 0.7 compiled " __TIME__ " " __DATE__ "\n";
94 /* --------------------------------------------------------------------- */
98 static struct net_device *baycom_device[NR_PORTS];
100 /* --------------------------------------------------------------------- */
102 /* EPP status register */
103 #define EPP_DCDBIT 0x80
104 #define EPP_PTTBIT 0x08
105 #define EPP_NREF 0x01
106 #define EPP_NRAEF 0x02
107 #define EPP_NRHF 0x04
108 #define EPP_NTHF 0x20
109 #define EPP_NTAEF 0x10
110 #define EPP_NTEF EPP_PTTBIT
112 /* EPP control register */
113 #define EPP_TX_FIFO_ENABLE 0x10
114 #define EPP_RX_FIFO_ENABLE 0x08
115 #define EPP_MODEM_ENABLE 0x20
116 #define EPP_LEDS 0xC0
117 #define EPP_IRQ_ENABLE 0x10
120 #define LPTREG_ECONTROL 0x402
121 #define LPTREG_CONFIGB 0x401
122 #define LPTREG_CONFIGA 0x400
123 #define LPTREG_EPPDATA 0x004
124 #define LPTREG_EPPADDR 0x003
125 #define LPTREG_CONTROL 0x002
126 #define LPTREG_STATUS 0x001
127 #define LPTREG_DATA 0x000
129 /* LPT control register */
130 #define LPTCTRL_PROGRAM 0x04 /* 0 to reprogram */
131 #define LPTCTRL_WRITE 0x01
132 #define LPTCTRL_ADDRSTB 0x08
133 #define LPTCTRL_DATASTB 0x02
134 #define LPTCTRL_INTEN 0x10
136 /* LPT status register */
137 #define LPTSTAT_SHIFT_NINTR 6
138 #define LPTSTAT_WAIT 0x80
139 #define LPTSTAT_NINTR (1<<LPTSTAT_SHIFT_NINTR)
140 #define LPTSTAT_PE 0x20
141 #define LPTSTAT_DONE 0x10
142 #define LPTSTAT_NERROR 0x08
143 #define LPTSTAT_EPPTIMEOUT 0x01
145 /* LPT data register */
146 #define LPTDATA_SHIFT_TDI 0
147 #define LPTDATA_SHIFT_TMS 2
148 #define LPTDATA_TDI (1<<LPTDATA_SHIFT_TDI)
149 #define LPTDATA_TCK 0x02
150 #define LPTDATA_TMS (1<<LPTDATA_SHIFT_TMS)
151 #define LPTDATA_INITBIAS 0x80
154 /* EPP modem config/status bits */
155 #define EPP_DCDBIT 0x80
156 #define EPP_PTTBIT 0x08
157 #define EPP_RXEBIT 0x01
158 #define EPP_RXAEBIT 0x02
159 #define EPP_RXHFULL 0x04
161 #define EPP_NTHF 0x20
162 #define EPP_NTAEF 0x10
163 #define EPP_NTEF EPP_PTTBIT
165 #define EPP_TX_FIFO_ENABLE 0x10
166 #define EPP_RX_FIFO_ENABLE 0x08
167 #define EPP_MODEM_ENABLE 0x20
168 #define EPP_LEDS 0xC0
169 #define EPP_IRQ_ENABLE 0x10
171 /* Xilinx 4k JTAG instructions */
172 #define XC4K_IRLENGTH 3
173 #define XC4K_EXTEST 0
174 #define XC4K_PRELOAD 1
175 #define XC4K_CONFIGURE 5
176 #define XC4K_BYPASS 7
178 #define EPP_CONVENTIONAL 0
180 #define EPP_FPGAEXTSTATUS 2
182 #define TXBUFFER_SIZE ((HDLCDRV_MAXFLEN*6/5)+8)
184 /* ---------------------------------------------------------------------- */
186 * Information that need to be kept for each board.
189 struct baycom_state {
192 struct pardevice *pdev;
193 unsigned int work_running;
194 struct work_struct run_work;
196 unsigned int bitrate;
203 unsigned int extmodem;
204 unsigned int loopback;
207 struct hdlcdrv_channel_params ch_params;
210 unsigned int bitbuf, bitstream, numbits, state;
211 unsigned char *bufptr;
213 unsigned char buf[TXBUFFER_SIZE];
220 enum { tx_idle = 0, tx_keyup, tx_data, tx_tail } state;
221 unsigned char *bufptr;
223 unsigned char buf[TXBUFFER_SIZE];
226 struct net_device_stats stats;
227 unsigned int ptt_keyed;
228 struct sk_buff *skb; /* next transmit packet */
232 unsigned long last_jiffies;
234 unsigned last_intcnt;
237 unsigned int mod_cycles;
238 unsigned int demod_cycles;
240 #endif /* BAYCOM_DEBUG */
243 /* --------------------------------------------------------------------- */
247 /* --------------------------------------------------------------------- */
249 #define PARAM_TXDELAY 1
250 #define PARAM_PERSIST 2
251 #define PARAM_SLOTTIME 3
252 #define PARAM_TXTAIL 4
253 #define PARAM_FULLDUP 5
254 #define PARAM_HARDWARE 6
255 #define PARAM_RETURN 255
257 /* --------------------------------------------------------------------- */
259 * the CRC routines are stolen from WAMPES
263 static const unsigned short crc_ccitt_table[] = {
264 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
265 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
266 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
267 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
268 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
269 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
270 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
271 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
272 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
273 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
274 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
275 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
276 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
277 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
278 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
279 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
280 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
281 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
282 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
283 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
284 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
285 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
286 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
287 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
288 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
289 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
290 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
291 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
292 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
293 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
294 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
295 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
298 /*---------------------------------------------------------------------------*/
301 static inline void append_crc_ccitt(unsigned char *buffer, int len)
303 unsigned int crc = 0xffff;
306 crc = (crc >> 8) ^ crc_ccitt_table[(crc ^ *buffer++) & 0xff];
309 *buffer++ = crc >> 8;
313 /*---------------------------------------------------------------------------*/
315 static inline int check_crc_ccitt(const unsigned char *buf, int cnt)
317 unsigned int crc = 0xffff;
319 for (; cnt > 0; cnt--)
320 crc = (crc >> 8) ^ crc_ccitt_table[(crc ^ *buf++) & 0xff];
321 return (crc & 0xffff) == 0xf0b8;
324 /*---------------------------------------------------------------------------*/
326 static inline int calc_crc_ccitt(const unsigned char *buf, int cnt)
328 unsigned int crc = 0xffff;
330 for (; cnt > 0; cnt--)
331 crc = (crc >> 8) ^ crc_ccitt_table[(crc ^ *buf++) & 0xff];
333 return (crc & 0xffff);
336 /* ---------------------------------------------------------------------- */
338 #define tenms_to_flags(bc,tenms) ((tenms * bc->bitrate) / 800)
340 /* --------------------------------------------------------------------- */
342 static inline void baycom_int_freq(struct baycom_state *bc)
345 unsigned long cur_jiffies = jiffies;
347 * measure the interrupt frequency
349 bc->debug_vals.cur_intcnt++;
350 if ((cur_jiffies - bc->debug_vals.last_jiffies) >= HZ) {
351 bc->debug_vals.last_jiffies = cur_jiffies;
352 bc->debug_vals.last_intcnt = bc->debug_vals.cur_intcnt;
353 bc->debug_vals.cur_intcnt = 0;
354 bc->debug_vals.last_pllcorr = bc->debug_vals.cur_pllcorr;
355 bc->debug_vals.cur_pllcorr = 0;
357 #endif /* BAYCOM_DEBUG */
360 /* ---------------------------------------------------------------------- */
362 * eppconfig_path should be setable via /proc/sys.
365 static char eppconfig_path[256] = "/usr/sbin/eppfpga";
367 static char *envp[] = { "HOME=/", "TERM=linux", "PATH=/usr/bin:/bin", NULL };
369 /* eppconfig: called during ifconfig up to configure the modem */
370 static int eppconfig(struct baycom_state *bc)
374 char *argv[] = { eppconfig_path, "-s", "-p", portarg, "-m", modearg,
377 /* set up arguments */
378 sprintf(modearg, "%sclk,%smodem,fclk=%d,bps=%d,divider=%d%s,extstat",
379 bc->cfg.intclk ? "int" : "ext",
380 bc->cfg.extmodem ? "ext" : "int", bc->cfg.fclk, bc->cfg.bps,
381 (bc->cfg.fclk + 8 * bc->cfg.bps) / (16 * bc->cfg.bps),
382 bc->cfg.loopback ? ",loopback" : "");
383 sprintf(portarg, "%ld", bc->pdev->port->base);
384 printk(KERN_DEBUG "%s: %s -s -p %s -m %s\n", bc_drvname, eppconfig_path, portarg, modearg);
386 return call_usermodehelper(eppconfig_path, argv, envp, 1);
389 /* ---------------------------------------------------------------------- */
391 static void epp_interrupt(int irq, void *dev_id, struct pt_regs *regs)
395 /* ---------------------------------------------------------------------- */
397 static inline void do_kiss_params(struct baycom_state *bc,
398 unsigned char *data, unsigned long len)
402 #define PKP(a,b) printk(KERN_INFO "baycomm_epp: channel params: " a "\n", b)
403 #else /* KISS_VERBOSE */
405 #endif /* KISS_VERBOSE */
411 bc->ch_params.tx_delay = data[1];
412 PKP("TX delay = %ums", 10 * bc->ch_params.tx_delay);
415 bc->ch_params.ppersist = data[1];
416 PKP("p persistence = %u", bc->ch_params.ppersist);
419 bc->ch_params.slottime = data[1];
420 PKP("slot time = %ums", bc->ch_params.slottime);
423 bc->ch_params.tx_tail = data[1];
424 PKP("TX tail = %ums", bc->ch_params.tx_tail);
427 bc->ch_params.fulldup = !!data[1];
428 PKP("%s duplex", bc->ch_params.fulldup ? "full" : "half");
436 /* --------------------------------------------------------------------- */
438 * high performance HDLC encoder
439 * yes, it's ugly, but generates pretty good code
442 #define ENCODEITERA(j) \
444 if (!(notbitstream & (0x1f0 << j))) \
449 #define ENCODEITERB(j) \
452 bitstream &= ~(0x100 << j); \
453 bitbuf = (bitbuf & (((2 << j) << numbit) - 1)) | \
454 ((bitbuf & ~(((2 << j) << numbit) - 1)) << 1); \
456 notbitstream = ~bitstream; \
461 static void encode_hdlc(struct baycom_state *bc)
464 unsigned char *wp, *bp;
466 unsigned bitstream, notbitstream, bitbuf, numbit, crc;
467 unsigned char crcarr[2];
469 if (bc->hdlctx.bufcnt > 0)
475 pkt_len = skb->len-1; /* strip KISS byte */
478 crc = calc_crc_ccitt(bp, pkt_len);
480 crcarr[1] = crc >> 8;
482 bitstream = bitbuf = numbit = 0;
483 while (pkt_len > -2) {
485 bitstream |= ((unsigned int)*bp) << 8;
486 bitbuf |= ((unsigned int)*bp) << numbit;
487 notbitstream = ~bitstream;
511 while (numbit >= 8) {
517 bitbuf |= 0x7e7e << numbit;
519 while (numbit >= 8) {
524 bc->hdlctx.bufptr = bc->hdlctx.buf;
525 bc->hdlctx.bufcnt = wp - bc->hdlctx.buf;
527 bc->stats.tx_packets++;
530 /* ---------------------------------------------------------------------- */
532 static unsigned short random_seed;
534 static inline unsigned short random_num(void)
536 random_seed = 28629 * random_seed + 157;
540 /* ---------------------------------------------------------------------- */
542 static int transmit(struct baycom_state *bc, int cnt, unsigned char stat)
544 struct parport *pp = bc->pdev->port;
545 unsigned char tmp[128];
548 if (bc->hdlctx.state == tx_tail && !(stat & EPP_PTTBIT))
549 bc->hdlctx.state = tx_idle;
550 if (bc->hdlctx.state == tx_idle && bc->hdlctx.calibrate <= 0) {
551 if (bc->hdlctx.bufcnt <= 0)
553 if (bc->hdlctx.bufcnt <= 0)
555 if (!bc->ch_params.fulldup) {
556 if (!(stat & EPP_DCDBIT)) {
557 bc->hdlctx.slotcnt = bc->ch_params.slottime;
560 if ((--bc->hdlctx.slotcnt) > 0)
562 bc->hdlctx.slotcnt = bc->ch_params.slottime;
563 if ((random_num() % 256) > bc->ch_params.ppersist)
567 if (bc->hdlctx.state == tx_idle && bc->hdlctx.bufcnt > 0) {
568 bc->hdlctx.state = tx_keyup;
569 bc->hdlctx.flags = tenms_to_flags(bc, bc->ch_params.tx_delay);
573 switch (bc->hdlctx.state) {
575 i = min_t(int, cnt, bc->hdlctx.flags);
577 bc->hdlctx.flags -= i;
578 if (bc->hdlctx.flags <= 0)
579 bc->hdlctx.state = tx_data;
580 memset(tmp, 0x7e, sizeof(tmp));
582 j = (i > sizeof(tmp)) ? sizeof(tmp) : i;
583 if (j != pp->ops->epp_write_data(pp, tmp, j, 0))
590 if (bc->hdlctx.bufcnt <= 0) {
592 if (bc->hdlctx.bufcnt <= 0) {
593 bc->hdlctx.state = tx_tail;
594 bc->hdlctx.flags = tenms_to_flags(bc, bc->ch_params.tx_tail);
598 i = min_t(int, cnt, bc->hdlctx.bufcnt);
599 bc->hdlctx.bufcnt -= i;
601 if (i != pp->ops->epp_write_data(pp, bc->hdlctx.bufptr, i, 0))
603 bc->hdlctx.bufptr += i;
608 if (bc->hdlctx.bufcnt > 0) {
609 bc->hdlctx.state = tx_data;
612 i = min_t(int, cnt, bc->hdlctx.flags);
615 bc->hdlctx.flags -= i;
616 memset(tmp, 0x7e, sizeof(tmp));
618 j = (i > sizeof(tmp)) ? sizeof(tmp) : i;
619 if (j != pp->ops->epp_write_data(pp, tmp, j, 0))
626 default: /* fall through */
627 if (bc->hdlctx.calibrate <= 0)
629 i = min_t(int, cnt, bc->hdlctx.calibrate);
631 bc->hdlctx.calibrate -= i;
632 memset(tmp, 0, sizeof(tmp));
634 j = (i > sizeof(tmp)) ? sizeof(tmp) : i;
635 if (j != pp->ops->epp_write_data(pp, tmp, j, 0))
645 /* ---------------------------------------------------------------------- */
647 static void do_rxpacket(struct net_device *dev)
649 struct baycom_state *bc = netdev_priv(dev);
654 if (bc->hdlcrx.bufcnt < 4)
656 if (!check_crc_ccitt(bc->hdlcrx.buf, bc->hdlcrx.bufcnt))
658 pktlen = bc->hdlcrx.bufcnt-2+1; /* KISS kludge */
659 if (!(skb = dev_alloc_skb(pktlen))) {
660 printk("%s: memory squeeze, dropping packet\n", dev->name);
661 bc->stats.rx_dropped++;
665 cp = skb_put(skb, pktlen);
666 *cp++ = 0; /* KISS kludge */
667 memcpy(cp, bc->hdlcrx.buf, pktlen - 1);
668 skb->protocol = htons(ETH_P_AX25);
669 skb->mac.raw = skb->data;
671 dev->last_rx = jiffies;
672 bc->stats.rx_packets++;
675 #define DECODEITERA(j) \
677 if (!(notbitstream & (0x0fc << j))) /* flag or abort */ \
679 if ((bitstream & (0x1f8 << j)) == (0xf8 << j)) /* stuffed bit */ \
684 #define DECODEITERB(j) \
687 if (!(notbitstream & (0x1fc << j))) { /* abort received */ \
691 if ((bitstream & (0x1fe << j)) != (0x0fc << j)) /* flag received */ \
695 bc->hdlcrx.bufcnt = 0; \
696 bc->hdlcrx.bufptr = bc->hdlcrx.buf; \
702 bitbuf = (bitbuf & ((~0xff) << j)) | ((bitbuf & ~((~0xff) << j)) << 1); \
706 static int receive(struct net_device *dev, int cnt)
708 struct baycom_state *bc = netdev_priv(dev);
709 struct parport *pp = bc->pdev->port;
710 unsigned int bitbuf, notbitstream, bitstream, numbits, state;
711 unsigned char tmp[128];
715 numbits = bc->hdlcrx.numbits;
716 state = bc->hdlcrx.state;
717 bitstream = bc->hdlcrx.bitstream;
718 bitbuf = bc->hdlcrx.bitbuf;
720 cnt2 = (cnt > sizeof(tmp)) ? sizeof(tmp) : cnt;
722 if (cnt2 != pp->ops->epp_read_data(pp, tmp, cnt2, 0)) {
727 for (; cnt2 > 0; cnt2--, cp++) {
729 bitstream |= (*cp) << 8;
731 bitbuf |= (*cp) << 8;
733 notbitstream = ~bitstream;
752 while (state && numbits >= 8) {
753 if (bc->hdlcrx.bufcnt >= TXBUFFER_SIZE) {
756 *(bc->hdlcrx.bufptr)++ = bitbuf >> (16-numbits);
763 bc->hdlcrx.numbits = numbits;
764 bc->hdlcrx.state = state;
765 bc->hdlcrx.bitstream = bitstream;
766 bc->hdlcrx.bitbuf = bitbuf;
770 /* --------------------------------------------------------------------- */
781 #endif /* __i386__ */
783 static void epp_bh(struct net_device *dev)
785 struct baycom_state *bc;
788 unsigned char tmp[2];
789 unsigned int time1 = 0, time2 = 0, time3 = 0;
792 baycom_paranoia_check_void(dev, "epp_bh");
793 bc = netdev_priv(dev);
794 if (!bc->work_running)
799 if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
802 bc->debug_vals.last_pllcorr = stat;
804 if (bc->modem == EPP_FPGAEXTSTATUS) {
805 /* get input count */
806 tmp[0] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE|1;
807 if (pp->ops->epp_write_addr(pp, tmp, 1, 0) != 1)
809 if (pp->ops->epp_read_addr(pp, tmp, 2, 0) != 2)
811 cnt = tmp[0] | (tmp[1] << 8);
813 /* get output count */
814 tmp[0] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE|2;
815 if (pp->ops->epp_write_addr(pp, tmp, 1, 0) != 1)
817 if (pp->ops->epp_read_addr(pp, tmp, 2, 0) != 2)
819 cnt2 = tmp[0] | (tmp[1] << 8);
820 cnt2 = 16384 - (cnt2 & 0x7fff);
821 /* return to normal */
822 tmp[0] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE;
823 if (pp->ops->epp_write_addr(pp, tmp, 1, 0) != 1)
825 if (transmit(bc, cnt2, stat))
828 if (receive(dev, cnt))
830 if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
835 switch (stat & (EPP_NTAEF|EPP_NTHF)) {
852 if (transmit(bc, cnt, stat))
856 while ((stat & (EPP_NRAEF|EPP_NRHF)) != EPP_NRHF) {
857 switch (stat & (EPP_NRAEF|EPP_NRHF)) {
870 if (receive(dev, cnt))
872 if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
876 if (bc->bitrate < 50000)
878 else if (bc->bitrate < 100000)
880 while (cnt > 0 && stat & EPP_NREF) {
884 if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
890 bc->debug_vals.mod_cycles = time2 - time1;
891 bc->debug_vals.demod_cycles = time3 - time2;
892 #endif /* BAYCOM_DEBUG */
893 schedule_delayed_work(&bc->run_work, 1);
895 netif_wake_queue(dev);
898 printk(KERN_ERR "%s: EPP timeout!\n", bc_drvname);
901 /* ---------------------------------------------------------------------- */
903 * ===================== network driver interface =========================
906 static int baycom_send_packet(struct sk_buff *skb, struct net_device *dev)
908 struct baycom_state *bc;
910 baycom_paranoia_check(dev, "baycom_send_packet", 0);
911 bc = netdev_priv(dev);
912 if (skb->data[0] != 0) {
913 do_kiss_params(bc, skb->data, skb->len);
919 /* strip KISS byte */
920 if (skb->len >= HDLCDRV_MAXFLEN+1 || skb->len < 3) {
924 netif_stop_queue(dev);
929 /* --------------------------------------------------------------------- */
931 static int baycom_set_mac_address(struct net_device *dev, void *addr)
933 struct sockaddr *sa = (struct sockaddr *)addr;
935 /* addr is an AX.25 shifted ASCII mac address */
936 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
940 /* --------------------------------------------------------------------- */
942 static struct net_device_stats *baycom_get_stats(struct net_device *dev)
944 struct baycom_state *bc;
946 baycom_paranoia_check(dev, "baycom_get_stats", NULL);
947 bc = netdev_priv(dev);
949 * Get the current statistics. This may be called with the
950 * card open or closed.
955 /* --------------------------------------------------------------------- */
957 static void epp_wakeup(void *handle)
959 struct net_device *dev = (struct net_device *)handle;
960 struct baycom_state *bc;
962 baycom_paranoia_check_void(dev, "epp_wakeup");
963 bc = netdev_priv(dev);
964 printk(KERN_DEBUG "baycom_epp: %s: why am I being woken up?\n", dev->name);
965 if (!parport_claim(bc->pdev))
966 printk(KERN_DEBUG "baycom_epp: %s: I'm broken.\n", dev->name);
969 /* --------------------------------------------------------------------- */
972 * Open/initialize the board. This is called (in the current kernel)
973 * sometime after booting when the 'ifconfig' program is run.
975 * This routine should set everything up anew at each open, even
976 * registers that "should" only need to be set once at boot, so that
977 * there is non-reboot way to recover if something goes wrong.
980 static int epp_open(struct net_device *dev)
982 struct baycom_state *bc;
985 unsigned char tmp[128];
987 unsigned long tstart;
989 baycom_paranoia_check(dev, "epp_open", -ENXIO);
990 bc = netdev_priv(dev);
991 pp = parport_find_base(dev->base_addr);
993 printk(KERN_ERR "%s: parport at 0x%lx unknown\n", bc_drvname, dev->base_addr);
998 printk(KERN_ERR "%s: parport at 0x%lx has no irq\n", bc_drvname, pp->base);
999 parport_put_port(pp);
1003 if ((~pp->modes) & (PARPORT_MODE_TRISTATE | PARPORT_MODE_PCSPP | PARPORT_MODE_SAFEININT)) {
1004 printk(KERN_ERR "%s: parport at 0x%lx cannot be used\n",
1005 bc_drvname, pp->base);
1006 parport_put_port(pp);
1009 memset(&bc->modem, 0, sizeof(bc->modem));
1010 bc->pdev = parport_register_device(pp, dev->name, NULL, epp_wakeup,
1011 epp_interrupt, PARPORT_DEV_EXCL, dev);
1012 parport_put_port(pp);
1014 printk(KERN_ERR "%s: cannot register parport at 0x%lx\n", bc_drvname, pp->base);
1017 if (parport_claim(bc->pdev)) {
1018 printk(KERN_ERR "%s: parport at 0x%lx busy\n", bc_drvname, pp->base);
1019 parport_unregister_device(bc->pdev);
1022 dev->irq = /*pp->irq*/ 0;
1023 INIT_WORK(&bc->run_work, (void *)(void *)epp_bh, dev);
1024 bc->work_running = 1;
1025 bc->modem = EPP_CONVENTIONAL;
1027 printk(KERN_INFO "%s: no FPGA detected, assuming conventional EPP modem\n", bc_drvname);
1029 bc->modem = /*EPP_FPGA*/ EPP_FPGAEXTSTATUS;
1030 parport_write_control(pp, LPTCTRL_PROGRAM); /* prepare EPP mode; we aren't using interrupts */
1031 /* reset the modem */
1033 tmp[1] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE;
1034 if (pp->ops->epp_write_addr(pp, tmp, 2, 0) != 2)
1036 /* autoprobe baud rate */
1039 while ((signed)(jiffies-tstart-HZ/3) < 0) {
1040 if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
1042 if ((stat & (EPP_NRAEF|EPP_NRHF)) == EPP_NRHF) {
1046 if (pp->ops->epp_read_data(pp, tmp, 128, 0) != 128)
1048 if (pp->ops->epp_read_data(pp, tmp, 128, 0) != 128)
1052 for (j = 0; j < 256; j++) {
1053 if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1)
1055 if (!(stat & EPP_NREF))
1057 if (pp->ops->epp_read_data(pp, tmp, 1, 0) != 1)
1061 tstart = jiffies - tstart;
1062 bc->bitrate = i * (8 * HZ) / tstart;
1064 i = bc->bitrate >> 3;
1065 while (j < 7 && i > 150) {
1069 printk(KERN_INFO "%s: autoprobed bitrate: %d int divider: %d int rate: %d\n",
1070 bc_drvname, bc->bitrate, j, bc->bitrate >> (j+2));
1071 tmp[0] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE/*|j*/;
1072 if (pp->ops->epp_write_addr(pp, tmp, 1, 0) != 1)
1075 * initialise hdlc variables
1077 bc->hdlcrx.state = 0;
1078 bc->hdlcrx.numbits = 0;
1079 bc->hdlctx.state = tx_idle;
1080 bc->hdlctx.bufcnt = 0;
1081 bc->hdlctx.slotcnt = bc->ch_params.slottime;
1082 bc->hdlctx.calibrate = 0;
1083 /* start the bottom half stuff */
1084 schedule_delayed_work(&bc->run_work, 1);
1085 netif_start_queue(dev);
1089 printk(KERN_ERR "%s: epp timeout during bitrate probe\n", bc_drvname);
1090 parport_write_control(pp, 0); /* reset the adapter */
1091 parport_release(bc->pdev);
1092 parport_unregister_device(bc->pdev);
1096 /* --------------------------------------------------------------------- */
1098 static int epp_close(struct net_device *dev)
1100 struct baycom_state *bc;
1102 unsigned char tmp[1];
1104 baycom_paranoia_check(dev, "epp_close", -EINVAL);
1105 bc = netdev_priv(dev);
1106 pp = bc->pdev->port;
1107 bc->work_running = 0;
1108 flush_scheduled_work();
1109 bc->stat = EPP_DCDBIT;
1111 pp->ops->epp_write_addr(pp, tmp, 1, 0);
1112 parport_write_control(pp, 0); /* reset the adapter */
1113 parport_release(bc->pdev);
1114 parport_unregister_device(bc->pdev);
1116 dev_kfree_skb(bc->skb);
1118 printk(KERN_INFO "%s: close epp at iobase 0x%lx irq %u\n",
1119 bc_drvname, dev->base_addr, dev->irq);
1123 /* --------------------------------------------------------------------- */
1125 static int baycom_setmode(struct baycom_state *bc, const char *modestr)
1129 if (strstr(modestr,"intclk"))
1131 if (strstr(modestr,"extclk"))
1133 if (strstr(modestr,"intmodem"))
1134 bc->cfg.extmodem = 0;
1135 if (strstr(modestr,"extmodem"))
1136 bc->cfg.extmodem = 1;
1137 if (strstr(modestr,"noloopback"))
1138 bc->cfg.loopback = 0;
1139 if (strstr(modestr,"loopback"))
1140 bc->cfg.loopback = 1;
1141 if ((cp = strstr(modestr,"fclk="))) {
1142 bc->cfg.fclk = simple_strtoul(cp+5, NULL, 0);
1143 if (bc->cfg.fclk < 1000000)
1144 bc->cfg.fclk = 1000000;
1145 if (bc->cfg.fclk > 25000000)
1146 bc->cfg.fclk = 25000000;
1148 if ((cp = strstr(modestr,"bps="))) {
1149 bc->cfg.bps = simple_strtoul(cp+4, NULL, 0);
1150 if (bc->cfg.bps < 1000)
1152 if (bc->cfg.bps > 1500000)
1153 bc->cfg.bps = 1500000;
1158 /* --------------------------------------------------------------------- */
1160 static int baycom_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1162 struct baycom_state *bc;
1163 struct hdlcdrv_ioctl hi;
1165 baycom_paranoia_check(dev, "baycom_ioctl", -EINVAL);
1166 bc = netdev_priv(dev);
1167 if (cmd != SIOCDEVPRIVATE)
1168 return -ENOIOCTLCMD;
1169 if (get_user(cmd, (int *)ifr->ifr_data))
1172 if (copy_from_user(&hi, ifr->ifr_data, sizeof(hi)))
1176 return -ENOIOCTLCMD;
1178 case HDLCDRVCTL_GETCHANNELPAR:
1179 hi.data.cp.tx_delay = bc->ch_params.tx_delay;
1180 hi.data.cp.tx_tail = bc->ch_params.tx_tail;
1181 hi.data.cp.slottime = bc->ch_params.slottime;
1182 hi.data.cp.ppersist = bc->ch_params.ppersist;
1183 hi.data.cp.fulldup = bc->ch_params.fulldup;
1186 case HDLCDRVCTL_SETCHANNELPAR:
1187 if (!capable(CAP_NET_ADMIN))
1189 bc->ch_params.tx_delay = hi.data.cp.tx_delay;
1190 bc->ch_params.tx_tail = hi.data.cp.tx_tail;
1191 bc->ch_params.slottime = hi.data.cp.slottime;
1192 bc->ch_params.ppersist = hi.data.cp.ppersist;
1193 bc->ch_params.fulldup = hi.data.cp.fulldup;
1194 bc->hdlctx.slotcnt = 1;
1197 case HDLCDRVCTL_GETMODEMPAR:
1198 hi.data.mp.iobase = dev->base_addr;
1199 hi.data.mp.irq = dev->irq;
1200 hi.data.mp.dma = dev->dma;
1201 hi.data.mp.dma2 = 0;
1202 hi.data.mp.seriobase = 0;
1203 hi.data.mp.pariobase = 0;
1204 hi.data.mp.midiiobase = 0;
1207 case HDLCDRVCTL_SETMODEMPAR:
1208 if ((!capable(CAP_SYS_RAWIO)) || netif_running(dev))
1210 dev->base_addr = hi.data.mp.iobase;
1211 dev->irq = /*hi.data.mp.irq*/0;
1212 dev->dma = /*hi.data.mp.dma*/0;
1215 case HDLCDRVCTL_GETSTAT:
1216 hi.data.cs.ptt = !!(bc->stat & EPP_PTTBIT);
1217 hi.data.cs.dcd = !(bc->stat & EPP_DCDBIT);
1218 hi.data.cs.ptt_keyed = bc->ptt_keyed;
1219 hi.data.cs.tx_packets = bc->stats.tx_packets;
1220 hi.data.cs.tx_errors = bc->stats.tx_errors;
1221 hi.data.cs.rx_packets = bc->stats.rx_packets;
1222 hi.data.cs.rx_errors = bc->stats.rx_errors;
1225 case HDLCDRVCTL_OLDGETSTAT:
1226 hi.data.ocs.ptt = !!(bc->stat & EPP_PTTBIT);
1227 hi.data.ocs.dcd = !(bc->stat & EPP_DCDBIT);
1228 hi.data.ocs.ptt_keyed = bc->ptt_keyed;
1231 case HDLCDRVCTL_CALIBRATE:
1232 if (!capable(CAP_SYS_RAWIO))
1234 bc->hdlctx.calibrate = hi.data.calibrate * bc->bitrate / 8;
1237 case HDLCDRVCTL_DRIVERNAME:
1238 strncpy(hi.data.drivername, "baycom_epp", sizeof(hi.data.drivername));
1241 case HDLCDRVCTL_GETMODE:
1242 sprintf(hi.data.modename, "%sclk,%smodem,fclk=%d,bps=%d%s",
1243 bc->cfg.intclk ? "int" : "ext",
1244 bc->cfg.extmodem ? "ext" : "int", bc->cfg.fclk, bc->cfg.bps,
1245 bc->cfg.loopback ? ",loopback" : "");
1248 case HDLCDRVCTL_SETMODE:
1249 if (!capable(CAP_NET_ADMIN) || netif_running(dev))
1251 hi.data.modename[sizeof(hi.data.modename)-1] = '\0';
1252 return baycom_setmode(bc, hi.data.modename);
1254 case HDLCDRVCTL_MODELIST:
1255 strncpy(hi.data.modename, "intclk,extclk,intmodem,extmodem,divider=x",
1256 sizeof(hi.data.modename));
1259 case HDLCDRVCTL_MODEMPARMASK:
1260 return HDLCDRV_PARMASK_IOBASE;
1263 if (copy_to_user(ifr->ifr_data, &hi, sizeof(hi)))
1268 /* --------------------------------------------------------------------- */
1271 * Check for a network adaptor of this type, and return '0' if one exists.
1272 * If dev->base_addr == 0, probe all likely locations.
1273 * If dev->base_addr == 1, always return failure.
1274 * If dev->base_addr == 2, allocate space for the device and return success
1275 * (detachable devices only).
1277 static void baycom_probe(struct net_device *dev)
1279 static char ax25_bcast[AX25_ADDR_LEN] = {
1280 'Q' << 1, 'S' << 1, 'T' << 1, ' ' << 1, ' ' << 1, ' ' << 1, '0' << 1
1282 static char ax25_nocall[AX25_ADDR_LEN] = {
1283 'L' << 1, 'I' << 1, 'N' << 1, 'U' << 1, 'X' << 1, ' ' << 1, '1' << 1
1285 const struct hdlcdrv_channel_params dflt_ch_params = {
1288 struct baycom_state *bc;
1291 * not a real probe! only initialize data structures
1293 bc = netdev_priv(dev);
1295 * initialize the baycom_state struct
1297 bc->ch_params = dflt_ch_params;
1301 * initialize the device struct
1303 dev->open = epp_open;
1304 dev->stop = epp_close;
1305 dev->do_ioctl = baycom_ioctl;
1306 dev->hard_start_xmit = baycom_send_packet;
1307 dev->get_stats = baycom_get_stats;
1309 /* Fill in the fields of the device structure */
1312 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
1313 dev->hard_header = ax25_encapsulate;
1314 dev->rebuild_header = ax25_rebuild_header;
1315 #else /* CONFIG_AX25 || CONFIG_AX25_MODULE */
1316 dev->hard_header = NULL;
1317 dev->rebuild_header = NULL;
1318 #endif /* CONFIG_AX25 || CONFIG_AX25_MODULE */
1319 dev->set_mac_address = baycom_set_mac_address;
1321 dev->type = ARPHRD_AX25; /* AF_AX25 device */
1322 dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN;
1323 dev->mtu = AX25_DEF_PACLEN; /* eth_mtu is the default */
1324 dev->addr_len = AX25_ADDR_LEN; /* sizeof an ax.25 address */
1325 memcpy(dev->broadcast, ax25_bcast, AX25_ADDR_LEN);
1326 memcpy(dev->dev_addr, ax25_nocall, AX25_ADDR_LEN);
1327 dev->tx_queue_len = 16;
1329 /* New style flags */
1333 /* --------------------------------------------------------------------- */
1336 * command line settable parameters
1338 static const char *mode[NR_PORTS] = { "", };
1339 static int iobase[NR_PORTS] = { 0x378, };
1341 MODULE_PARM(mode, "1-" __MODULE_STRING(NR_PORTS) "s");
1342 MODULE_PARM_DESC(mode, "baycom operating mode");
1343 MODULE_PARM(iobase, "1-" __MODULE_STRING(NR_PORTS) "i");
1344 MODULE_PARM_DESC(iobase, "baycom io base address");
1346 MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
1347 MODULE_DESCRIPTION("Baycom epp amateur radio modem driver");
1348 MODULE_LICENSE("GPL");
1350 /* --------------------------------------------------------------------- */
1352 static void __init baycom_epp_dev_setup(struct net_device *dev)
1354 struct baycom_state *bc = netdev_priv(dev);
1357 * initialize part of the baycom_state struct
1359 bc->magic = BAYCOM_MAGIC;
1360 bc->cfg.fclk = 19666600;
1363 * initialize part of the device struct
1368 static int __init init_baycomepp(void)
1375 * register net devices
1377 for (i = 0; i < NR_PORTS; i++) {
1378 struct net_device *dev;
1380 dev = alloc_netdev(sizeof(struct baycom_state), "bce%d",
1381 baycom_epp_dev_setup);
1384 printk(KERN_WARNING "bce%d : out of memory\n", i);
1385 return found ? 0 : -ENOMEM;
1388 sprintf(dev->name, "bce%d", i);
1389 dev->base_addr = iobase[i];
1396 if (register_netdev(dev)) {
1397 printk(KERN_WARNING "%s: cannot register net device %s\n", bc_drvname, dev->name);
1401 if (set_hw && baycom_setmode(dev->priv, mode[i]))
1403 baycom_device[i] = dev;
1407 return found ? 0 : -ENXIO;
1410 static void __exit cleanup_baycomepp(void)
1414 for(i = 0; i < NR_PORTS; i++) {
1415 struct net_device *dev = baycom_device[i];
1418 struct baycom_state *bc = netdev_priv(dev);
1419 if (bc->magic == BAYCOM_MAGIC) {
1420 unregister_netdev(dev);
1423 printk(paranoia_str, "cleanup_module");
1428 module_init(init_baycomepp);
1429 module_exit(cleanup_baycomepp);
1431 /* --------------------------------------------------------------------- */
1436 * format: baycom_epp=io,mode
1437 * mode: fpga config options
1440 static int __init baycom_epp_setup(char *str)
1442 static unsigned __initdata nr_dev = 0;
1445 if (nr_dev >= NR_PORTS)
1447 str = get_options(str, 2, ints);
1451 iobase[nr_dev] = ints[1];
1456 __setup("baycom_epp=", baycom_epp_setup);
1459 /* --------------------------------------------------------------------- */