44 #include <linux/module.h>
45 #include <linux/kernel.h>
47 #include <linux/sched.h>
48 #include <linux/string.h>
51 #include <linux/parport.h>
52 #include <linux/if_arp.h>
53 #include <linux/hdlcdrv.h>
56 #include <linux/random.h>
58 #include <asm/uaccess.h>
63 #define BAYCOM_MAGIC 19730510
67 static const char paranoia_str[] =
KERN_ERR
68 "baycom_epp: bad magic number for hdlcdrv_state struct in routine %s\n";
70 static const char bc_drvname[] =
"baycom_epp";
71 static const char bc_drvinfo[] =
KERN_INFO "baycom_epp: (C) 1998-2000 Thomas Sailer, HB9JNX/AE4WA\n"
72 "baycom_epp: version 0.7\n";
83 #define EPP_DCDBIT 0x80
84 #define EPP_PTTBIT 0x08
86 #define EPP_NRAEF 0x02
89 #define EPP_NTAEF 0x10
90 #define EPP_NTEF EPP_PTTBIT
93 #define EPP_TX_FIFO_ENABLE 0x10
94 #define EPP_RX_FIFO_ENABLE 0x08
95 #define EPP_MODEM_ENABLE 0x20
97 #define EPP_IRQ_ENABLE 0x10
100 #define LPTREG_ECONTROL 0x402
101 #define LPTREG_CONFIGB 0x401
102 #define LPTREG_CONFIGA 0x400
103 #define LPTREG_EPPDATA 0x004
104 #define LPTREG_EPPADDR 0x003
105 #define LPTREG_CONTROL 0x002
106 #define LPTREG_STATUS 0x001
107 #define LPTREG_DATA 0x000
110 #define LPTCTRL_PROGRAM 0x04
111 #define LPTCTRL_WRITE 0x01
112 #define LPTCTRL_ADDRSTB 0x08
113 #define LPTCTRL_DATASTB 0x02
114 #define LPTCTRL_INTEN 0x10
117 #define LPTSTAT_SHIFT_NINTR 6
118 #define LPTSTAT_WAIT 0x80
119 #define LPTSTAT_NINTR (1<<LPTSTAT_SHIFT_NINTR)
120 #define LPTSTAT_PE 0x20
121 #define LPTSTAT_DONE 0x10
122 #define LPTSTAT_NERROR 0x08
123 #define LPTSTAT_EPPTIMEOUT 0x01
126 #define LPTDATA_SHIFT_TDI 0
127 #define LPTDATA_SHIFT_TMS 2
128 #define LPTDATA_TDI (1<<LPTDATA_SHIFT_TDI)
129 #define LPTDATA_TCK 0x02
130 #define LPTDATA_TMS (1<<LPTDATA_SHIFT_TMS)
131 #define LPTDATA_INITBIAS 0x80
135 #define EPP_DCDBIT 0x80
136 #define EPP_PTTBIT 0x08
137 #define EPP_RXEBIT 0x01
138 #define EPP_RXAEBIT 0x02
139 #define EPP_RXHFULL 0x04
141 #define EPP_NTHF 0x20
142 #define EPP_NTAEF 0x10
143 #define EPP_NTEF EPP_PTTBIT
145 #define EPP_TX_FIFO_ENABLE 0x10
146 #define EPP_RX_FIFO_ENABLE 0x08
147 #define EPP_MODEM_ENABLE 0x20
148 #define EPP_LEDS 0xC0
149 #define EPP_IRQ_ENABLE 0x10
152 #define XC4K_IRLENGTH 3
153 #define XC4K_EXTEST 0
154 #define XC4K_PRELOAD 1
155 #define XC4K_CONFIGURE 5
156 #define XC4K_BYPASS 7
158 #define EPP_CONVENTIONAL 0
160 #define EPP_FPGAEXTSTATUS 2
162 #define TXBUFFER_SIZE ((HDLCDRV_MAXFLEN*6/5)+8)
229 #define PARAM_TXDELAY 1
230 #define PARAM_PERSIST 2
231 #define PARAM_SLOTTIME 3
232 #define PARAM_TXTAIL 4
233 #define PARAM_FULLDUP 5
234 #define PARAM_HARDWARE 6
235 #define PARAM_RETURN 255
247 static inline void append_crc_ccitt(
unsigned char *
buffer,
int len)
249 unsigned int crc = 0xffff;
255 *buffer++ = crc >> 8;
261 static inline int check_crc_ccitt(
const unsigned char *
buf,
int cnt)
263 return (
crc_ccitt(0xffff, buf, cnt) & 0xffff) == 0xf0b8;
268 static inline int calc_crc_ccitt(
const unsigned char *buf,
int cnt)
270 return (
crc_ccitt(0xffff, buf, cnt) ^ 0xffff) & 0xffff;
275 #define tenms_to_flags(bc,tenms) ((tenms * bc->bitrate) / 800)
282 unsigned long cur_jiffies =
jiffies;
302 static char eppconfig_path[256] =
"/usr/sbin/eppfpga";
304 static char *envp[] = {
"HOME=/",
"TERM=linux",
"PATH=/usr/bin:/bin",
NULL };
311 char *argv[] = { eppconfig_path,
"-s",
"-p", portarg,
"-m", modearg,
315 sprintf(modearg,
"%sclk,%smodem,fclk=%d,bps=%d,divider=%d%s,extstat",
316 bc->
cfg.intclk ?
"int" :
"ext",
317 bc->
cfg.extmodem ?
"ext" :
"int", bc->
cfg.fclk, bc->
cfg.bps,
318 (bc->
cfg.fclk + 8 * bc->
cfg.bps) / (16 * bc->
cfg.bps),
319 bc->
cfg.loopback ?
",loopback" :
"");
321 printk(
KERN_DEBUG "%s: %s -s -p %s -m %s\n", bc_drvname, eppconfig_path, portarg, modearg);
323 return call_usermodehelper(eppconfig_path, argv, envp,
UMH_WAIT_PROC);
329 unsigned char *
data,
unsigned long len)
333 #define PKP(a,b) printk(KERN_INFO "baycomm_epp: channel params: " a "\n", b)
359 PKP(
"%s duplex", bc->
ch_params.fulldup ?
"full" :
"half");
372 unsigned char *
wp, *bp;
375 unsigned char crcarr[2];
378 if (bc->
hdlctx.bufcnt > 0)
384 pkt_len = skb->
len-1;
387 crc = calc_crc_ccitt(bp, pkt_len);
389 crcarr[1] = crc >> 8;
391 bitstream = bitbuf = numbit = 0;
392 while (pkt_len > -2) {
394 bitstream |= ((
unsigned int)*bp) << 8;
395 bitbuf |= ((
unsigned int)*bp) << numbit;
396 notbitstream = ~bitstream;
401 for (j = 0; j < 8; j++)
402 if (
unlikely(!(notbitstream & (0x1f0 << j)))) {
403 bitstream &= ~(0x100 <<
j);
404 bitbuf = (bitbuf & (((2 <<
j) << numbit) - 1)) |
405 ((bitbuf & ~(((2 <<
j) << numbit) - 1)) << 1);
407 notbitstream = ~bitstream;
410 while (numbit >= 8) {
416 bitbuf |= 0x7e7e << numbit;
418 while (numbit >= 8) {
426 bc->
dev->stats.tx_packets++;
434 unsigned char tmp[128];
438 bc->
hdlctx.state = tx_idle;
439 if (bc->
hdlctx.state == tx_idle && bc->
hdlctx.calibrate <= 0) {
440 if (bc->
hdlctx.bufcnt <= 0)
442 if (bc->
hdlctx.bufcnt <= 0)
449 if ((--bc->
hdlctx.slotcnt) > 0)
456 if (bc->
hdlctx.state == tx_idle && bc->
hdlctx.bufcnt > 0) {
457 bc->
hdlctx.state = tx_keyup;
462 switch (bc->
hdlctx.state) {
467 if (bc->
hdlctx.flags <= 0)
469 memset(tmp, 0x7e,
sizeof(tmp));
471 j = (i >
sizeof(
tmp)) ?
sizeof(
tmp) : i;
472 if (j != pp->
ops->epp_write_data(pp, tmp, j, 0))
479 if (bc->
hdlctx.bufcnt <= 0) {
481 if (bc->
hdlctx.bufcnt <= 0) {
482 bc->
hdlctx.state = tx_tail;
490 if (i != pp->
ops->epp_write_data(pp, bc->
hdlctx.bufptr, i, 0))
497 if (bc->
hdlctx.bufcnt > 0) {
505 memset(tmp, 0x7e,
sizeof(tmp));
507 j = (i >
sizeof(
tmp)) ?
sizeof(
tmp) : i;
508 if (j != pp->
ops->epp_write_data(pp, tmp, j, 0))
516 if (bc->
hdlctx.calibrate <= 0)
521 memset(tmp, 0,
sizeof(tmp));
523 j = (i >
sizeof(
tmp)) ?
sizeof(
tmp) : i;
524 if (j != pp->
ops->epp_write_data(pp, tmp, j, 0))
543 if (bc->
hdlcrx.bufcnt < 4)
545 if (!check_crc_ccitt(bc->
hdlcrx.buf, bc->
hdlcrx.bufcnt))
547 pktlen = bc->
hdlcrx.bufcnt-2+1;
548 if (!(skb = dev_alloc_skb(pktlen))) {
549 printk(
"%s: memory squeeze, dropping packet\n", dev->
name);
550 dev->
stats.rx_dropped++;
556 skb->
protocol = ax25_type_trans(skb, dev);
558 dev->
stats.rx_packets++;
561 static int receive(
struct net_device *dev,
int cnt)
565 unsigned int bitbuf, notbitstream, bitstream, numbits,
state;
566 unsigned char tmp[128];
571 numbits = bc->
hdlcrx.numbits;
573 bitstream = bc->
hdlcrx.bitstream;
574 bitbuf = bc->
hdlcrx.bitbuf;
576 cnt2 = (cnt >
sizeof(
tmp)) ?
sizeof(
tmp) : cnt;
578 if (cnt2 != pp->
ops->epp_read_data(pp, tmp, cnt2, 0)) {
583 for (; cnt2 > 0; cnt2--, cp++) {
585 bitstream |= (*cp) << 8;
587 bitbuf |= (*cp) << 8;
589 notbitstream = ~bitstream;
590 for (j = 0; j < 8; j++) {
593 if (
unlikely(!(notbitstream & (0x0fc << j)))) {
596 if (!(notbitstream & (0x1fc << j)))
600 else if ((bitstream & (0x1fe << j)) == (0x0fc <<
j)) {
611 else if (
unlikely((bitstream & (0x1f8 << j)) == (0xf8 << j))) {
613 bitbuf = (bitbuf & ((~0xff) << j)) | ((bitbuf & ~((~0xff) <<
j)) << 1);
616 while (state && numbits >= 8) {
620 *(bc->
hdlcrx.bufptr)++ = bitbuf >> (16-numbits);
627 bc->
hdlcrx.numbits = numbits;
629 bc->
hdlcrx.bitstream = bitstream;
630 bc->
hdlcrx.bitbuf = bitbuf;
653 unsigned char tmp[2];
654 unsigned int time1 = 0, time2 = 0, time3 = 0;
664 if (pp->
ops->epp_read_addr(pp, &stat, 1, 0) != 1)
672 if (pp->
ops->epp_write_addr(pp, tmp, 1, 0) != 1)
674 if (pp->
ops->epp_read_addr(pp, tmp, 2, 0) != 2)
676 cnt = tmp[0] | (tmp[1] << 8);
680 if (pp->
ops->epp_write_addr(pp, tmp, 1, 0) != 1)
682 if (pp->
ops->epp_read_addr(pp, tmp, 2, 0) != 2)
684 cnt2 = tmp[0] | (tmp[1] << 8);
685 cnt2 = 16384 - (cnt2 & 0x7fff);
688 if (pp->
ops->epp_write_addr(pp, tmp, 1, 0) != 1)
690 if (transmit(bc, cnt2, stat))
693 if (receive(dev, cnt))
695 if (pp->
ops->epp_read_addr(pp, &stat, 1, 0) != 1)
717 if (transmit(bc, cnt, stat))
735 if (receive(dev, cnt))
737 if (pp->
ops->epp_read_addr(pp, &stat, 1, 0) != 1)
745 while (cnt > 0 && stat &
EPP_NREF) {
749 if (pp->
ops->epp_read_addr(pp, &stat, 1, 0) != 1)
760 netif_wake_queue(dev);
775 if (skb->
data[0] != 0) {
776 do_kiss_params(bc, skb->
data, skb->
len);
787 netif_stop_queue(dev);
794 static int baycom_set_mac_address(
struct net_device *dev,
void *
addr)
805 static void epp_wakeup(
void *
handle)
831 unsigned char tmp[128];
848 bc_drvname, pp->
base);
870 printk(
KERN_INFO "%s: no FPGA detected, assuming conventional EPP modem\n", bc_drvname);
877 if (pp->
ops->epp_write_addr(pp, tmp, 2, 0) != 2)
883 if (pp->
ops->epp_read_addr(pp, &stat, 1, 0) != 1)
889 if (pp->
ops->epp_read_data(pp, tmp, 128, 0) != 128)
891 if (pp->
ops->epp_read_data(pp, tmp, 128, 0) != 128)
895 for (j = 0; j < 256; j++) {
896 if (pp->
ops->epp_read_addr(pp, &stat, 1, 0) != 1)
900 if (pp->
ops->epp_read_data(pp, tmp, 1, 0) != 1)
908 while (j < 7 && i > 150) {
912 printk(
KERN_INFO "%s: autoprobed bitrate: %d int divider: %d int rate: %d\n",
915 if (pp->
ops->epp_write_addr(pp, tmp, 1, 0) != 1)
922 bc->
hdlctx.state = tx_idle;
928 netif_start_queue(dev);
932 printk(
KERN_ERR "%s: epp timeout during bitrate probe\n", bc_drvname);
945 unsigned char tmp[1];
951 pp->
ops->epp_write_addr(pp, tmp, 1, 0);
956 dev_kfree_skb(bc->
skb);
965 static int baycom_setmode(
struct baycom_state *bc,
const char *modestr)
969 if (
strstr(modestr,
"intclk"))
971 if (
strstr(modestr,
"extclk"))
973 if (
strstr(modestr,
"intmodem"))
974 bc->
cfg.extmodem = 0;
975 if (
strstr(modestr,
"extmodem"))
976 bc->
cfg.extmodem = 1;
977 if (
strstr(modestr,
"noloopback"))
978 bc->
cfg.loopback = 0;
979 if (
strstr(modestr,
"loopback"))
980 bc->
cfg.loopback = 1;
981 if ((cp =
strstr(modestr,
"fclk="))) {
983 if (bc->
cfg.fclk < 1000000)
984 bc->
cfg.fclk = 1000000;
985 if (bc->
cfg.fclk > 25000000)
986 bc->
cfg.fclk = 25000000;
988 if ((cp =
strstr(modestr,
"bps="))) {
990 if (bc->
cfg.bps < 1000)
992 if (bc->
cfg.bps > 1500000)
993 bc->
cfg.bps = 1500000;
1035 hi.data.mp.irq = dev->
irq;
1036 hi.data.mp.dma = dev->
dma;
1037 hi.data.mp.dma2 = 0;
1038 hi.data.mp.seriobase = 0;
1039 hi.data.mp.pariobase = 0;
1040 hi.data.mp.midiiobase = 0;
1055 hi.data.cs.tx_packets = dev->
stats.tx_packets;
1056 hi.data.cs.tx_errors = dev->
stats.tx_errors;
1057 hi.data.cs.rx_packets = dev->
stats.rx_packets;
1058 hi.data.cs.rx_errors = dev->
stats.rx_errors;
1074 strncpy(
hi.data.drivername,
"baycom_epp",
sizeof(
hi.data.drivername));
1078 sprintf(
hi.data.modename,
"%sclk,%smodem,fclk=%d,bps=%d%s",
1079 bc->
cfg.intclk ?
"int" :
"ext",
1080 bc->
cfg.extmodem ?
"ext" :
"int", bc->
cfg.fclk, bc->
cfg.bps,
1081 bc->
cfg.loopback ?
",loopback" :
"");
1087 hi.data.modename[
sizeof(
hi.data.modename)-1] =
'\0';
1088 return baycom_setmode(bc,
hi.data.modename);
1091 strncpy(
hi.data.modename,
"intclk,extclk,intmodem,extmodem,divider=x",
1092 sizeof(
hi.data.modename));
1107 .ndo_open = epp_open,
1108 .ndo_stop = epp_close,
1110 .ndo_start_xmit = baycom_send_packet,
1111 .ndo_set_mac_address = baycom_set_mac_address,
1121 static void baycom_probe(
struct net_device *dev)
1131 bc = netdev_priv(dev);
1188 bc->
cfg.fclk = 19666600;
1196 static int __init init_baycomepp(
void)
1209 baycom_epp_dev_setup);
1213 return found ? 0 : -
ENOMEM;
1229 if (set_hw && baycom_setmode(netdev_priv(dev), mode[i]))
1231 baycom_device[
i] =
dev;
1235 return found ? 0 : -
ENXIO;
1238 static void __exit cleanup_baycomepp(
void)
1251 printk(paranoia_str,
"cleanup_module");
1268 static int __init baycom_epp_setup(
char *
str)
1273 if (nr_dev >= NR_PORTS)
1279 iobase[nr_dev] = ints[1];
1284 __setup(
"baycom_epp=", baycom_epp_setup);