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 */
61 #include <linux/crc-ccitt.h>
63 /* --------------------------------------------------------------------- */
66 #define BAYCOM_MAGIC 19730510
68 /* --------------------------------------------------------------------- */
70 static const char paranoia_str
[] = KERN_ERR
71 "baycom_epp: bad magic number for hdlcdrv_state struct in routine %s\n";
73 static const char bc_drvname
[] = "baycom_epp";
74 static const char bc_drvinfo
[] = KERN_INFO
"baycom_epp: (C) 1998-2000 Thomas Sailer, HB9JNX/AE4WA\n"
75 KERN_INFO
"baycom_epp: version 0.7 compiled " __TIME__
" " __DATE__
"\n";
77 /* --------------------------------------------------------------------- */
81 static struct net_device
*baycom_device
[NR_PORTS
];
83 /* --------------------------------------------------------------------- */
85 /* EPP status register */
86 #define EPP_DCDBIT 0x80
87 #define EPP_PTTBIT 0x08
89 #define EPP_NRAEF 0x02
92 #define EPP_NTAEF 0x10
93 #define EPP_NTEF EPP_PTTBIT
95 /* EPP control register */
96 #define EPP_TX_FIFO_ENABLE 0x10
97 #define EPP_RX_FIFO_ENABLE 0x08
98 #define EPP_MODEM_ENABLE 0x20
100 #define EPP_IRQ_ENABLE 0x10
103 #define LPTREG_ECONTROL 0x402
104 #define LPTREG_CONFIGB 0x401
105 #define LPTREG_CONFIGA 0x400
106 #define LPTREG_EPPDATA 0x004
107 #define LPTREG_EPPADDR 0x003
108 #define LPTREG_CONTROL 0x002
109 #define LPTREG_STATUS 0x001
110 #define LPTREG_DATA 0x000
112 /* LPT control register */
113 #define LPTCTRL_PROGRAM 0x04 /* 0 to reprogram */
114 #define LPTCTRL_WRITE 0x01
115 #define LPTCTRL_ADDRSTB 0x08
116 #define LPTCTRL_DATASTB 0x02
117 #define LPTCTRL_INTEN 0x10
119 /* LPT status register */
120 #define LPTSTAT_SHIFT_NINTR 6
121 #define LPTSTAT_WAIT 0x80
122 #define LPTSTAT_NINTR (1<<LPTSTAT_SHIFT_NINTR)
123 #define LPTSTAT_PE 0x20
124 #define LPTSTAT_DONE 0x10
125 #define LPTSTAT_NERROR 0x08
126 #define LPTSTAT_EPPTIMEOUT 0x01
128 /* LPT data register */
129 #define LPTDATA_SHIFT_TDI 0
130 #define LPTDATA_SHIFT_TMS 2
131 #define LPTDATA_TDI (1<<LPTDATA_SHIFT_TDI)
132 #define LPTDATA_TCK 0x02
133 #define LPTDATA_TMS (1<<LPTDATA_SHIFT_TMS)
134 #define LPTDATA_INITBIAS 0x80
137 /* EPP modem config/status bits */
138 #define EPP_DCDBIT 0x80
139 #define EPP_PTTBIT 0x08
140 #define EPP_RXEBIT 0x01
141 #define EPP_RXAEBIT 0x02
142 #define EPP_RXHFULL 0x04
144 #define EPP_NTHF 0x20
145 #define EPP_NTAEF 0x10
146 #define EPP_NTEF EPP_PTTBIT
148 #define EPP_TX_FIFO_ENABLE 0x10
149 #define EPP_RX_FIFO_ENABLE 0x08
150 #define EPP_MODEM_ENABLE 0x20
151 #define EPP_LEDS 0xC0
152 #define EPP_IRQ_ENABLE 0x10
154 /* Xilinx 4k JTAG instructions */
155 #define XC4K_IRLENGTH 3
156 #define XC4K_EXTEST 0
157 #define XC4K_PRELOAD 1
158 #define XC4K_CONFIGURE 5
159 #define XC4K_BYPASS 7
161 #define EPP_CONVENTIONAL 0
163 #define EPP_FPGAEXTSTATUS 2
165 #define TXBUFFER_SIZE ((HDLCDRV_MAXFLEN*6/5)+8)
167 /* ---------------------------------------------------------------------- */
169 * Information that need to be kept for each board.
172 struct baycom_state
{
175 struct pardevice
*pdev
;
176 unsigned int work_running
;
177 struct work_struct run_work
;
179 unsigned int bitrate
;
186 unsigned int extmodem
;
187 unsigned int loopback
;
190 struct hdlcdrv_channel_params ch_params
;
193 unsigned int bitbuf
, bitstream
, numbits
, state
;
194 unsigned char *bufptr
;
196 unsigned char buf
[TXBUFFER_SIZE
];
203 enum { tx_idle
= 0, tx_keyup
, tx_data
, tx_tail
} state
;
204 unsigned char *bufptr
;
206 unsigned char buf
[TXBUFFER_SIZE
];
209 struct net_device_stats stats
;
210 unsigned int ptt_keyed
;
211 struct sk_buff
*skb
; /* next transmit packet */
215 unsigned long last_jiffies
;
217 unsigned last_intcnt
;
220 unsigned int mod_cycles
;
221 unsigned int demod_cycles
;
223 #endif /* BAYCOM_DEBUG */
226 /* --------------------------------------------------------------------- */
230 /* --------------------------------------------------------------------- */
232 #define PARAM_TXDELAY 1
233 #define PARAM_PERSIST 2
234 #define PARAM_SLOTTIME 3
235 #define PARAM_TXTAIL 4
236 #define PARAM_FULLDUP 5
237 #define PARAM_HARDWARE 6
238 #define PARAM_RETURN 255
240 /* --------------------------------------------------------------------- */
242 * the CRC routines are stolen from WAMPES
247 /*---------------------------------------------------------------------------*/
250 static inline void append_crc_ccitt(unsigned char *buffer
, int len
)
252 unsigned int crc
= 0xffff;
255 crc
= (crc
>> 8) ^ crc_ccitt_table
[(crc
^ *buffer
++) & 0xff];
258 *buffer
++ = crc
>> 8;
262 /*---------------------------------------------------------------------------*/
264 static inline int check_crc_ccitt(const unsigned char *buf
, int cnt
)
266 return (crc_ccitt(0xffff, buf
, cnt
) & 0xffff) == 0xf0b8;
269 /*---------------------------------------------------------------------------*/
271 static inline int calc_crc_ccitt(const unsigned char *buf
, int cnt
)
273 return (crc_ccitt(0xffff, buf
, cnt
) ^ 0xffff) & 0xffff;
276 /* ---------------------------------------------------------------------- */
278 #define tenms_to_flags(bc,tenms) ((tenms * bc->bitrate) / 800)
280 /* --------------------------------------------------------------------- */
282 static inline void baycom_int_freq(struct baycom_state
*bc
)
285 unsigned long cur_jiffies
= jiffies
;
287 * measure the interrupt frequency
289 bc
->debug_vals
.cur_intcnt
++;
290 if ((cur_jiffies
- bc
->debug_vals
.last_jiffies
) >= HZ
) {
291 bc
->debug_vals
.last_jiffies
= cur_jiffies
;
292 bc
->debug_vals
.last_intcnt
= bc
->debug_vals
.cur_intcnt
;
293 bc
->debug_vals
.cur_intcnt
= 0;
294 bc
->debug_vals
.last_pllcorr
= bc
->debug_vals
.cur_pllcorr
;
295 bc
->debug_vals
.cur_pllcorr
= 0;
297 #endif /* BAYCOM_DEBUG */
300 /* ---------------------------------------------------------------------- */
302 * eppconfig_path should be setable via /proc/sys.
305 static char eppconfig_path
[256] = "/usr/sbin/eppfpga";
307 static char *envp
[] = { "HOME=/", "TERM=linux", "PATH=/usr/bin:/bin", NULL
};
309 /* eppconfig: called during ifconfig up to configure the modem */
310 static int eppconfig(struct baycom_state
*bc
)
314 char *argv
[] = { eppconfig_path
, "-s", "-p", portarg
, "-m", modearg
,
317 /* set up arguments */
318 sprintf(modearg
, "%sclk,%smodem,fclk=%d,bps=%d,divider=%d%s,extstat",
319 bc
->cfg
.intclk
? "int" : "ext",
320 bc
->cfg
.extmodem
? "ext" : "int", bc
->cfg
.fclk
, bc
->cfg
.bps
,
321 (bc
->cfg
.fclk
+ 8 * bc
->cfg
.bps
) / (16 * bc
->cfg
.bps
),
322 bc
->cfg
.loopback
? ",loopback" : "");
323 sprintf(portarg
, "%ld", bc
->pdev
->port
->base
);
324 printk(KERN_DEBUG
"%s: %s -s -p %s -m %s\n", bc_drvname
, eppconfig_path
, portarg
, modearg
);
326 return call_usermodehelper(eppconfig_path
, argv
, envp
, 1);
329 /* ---------------------------------------------------------------------- */
331 static void epp_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
335 /* ---------------------------------------------------------------------- */
337 static inline void do_kiss_params(struct baycom_state
*bc
,
338 unsigned char *data
, unsigned long len
)
342 #define PKP(a,b) printk(KERN_INFO "baycomm_epp: channel params: " a "\n", b)
343 #else /* KISS_VERBOSE */
345 #endif /* KISS_VERBOSE */
351 bc
->ch_params
.tx_delay
= data
[1];
352 PKP("TX delay = %ums", 10 * bc
->ch_params
.tx_delay
);
355 bc
->ch_params
.ppersist
= data
[1];
356 PKP("p persistence = %u", bc
->ch_params
.ppersist
);
359 bc
->ch_params
.slottime
= data
[1];
360 PKP("slot time = %ums", bc
->ch_params
.slottime
);
363 bc
->ch_params
.tx_tail
= data
[1];
364 PKP("TX tail = %ums", bc
->ch_params
.tx_tail
);
367 bc
->ch_params
.fulldup
= !!data
[1];
368 PKP("%s duplex", bc
->ch_params
.fulldup
? "full" : "half");
376 /* --------------------------------------------------------------------- */
378 static void encode_hdlc(struct baycom_state
*bc
)
381 unsigned char *wp
, *bp
;
383 unsigned bitstream
, notbitstream
, bitbuf
, numbit
, crc
;
384 unsigned char crcarr
[2];
387 if (bc
->hdlctx
.bufcnt
> 0)
393 pkt_len
= skb
->len
-1; /* strip KISS byte */
396 crc
= calc_crc_ccitt(bp
, pkt_len
);
398 crcarr
[1] = crc
>> 8;
400 bitstream
= bitbuf
= numbit
= 0;
401 while (pkt_len
> -2) {
403 bitstream
|= ((unsigned int)*bp
) << 8;
404 bitbuf
|= ((unsigned int)*bp
) << numbit
;
405 notbitstream
= ~bitstream
;
410 for (j
= 0; j
< 8; j
++)
411 if (unlikely(!(notbitstream
& (0x1f0 << j
)))) {
412 bitstream
&= ~(0x100 << j
);
413 bitbuf
= (bitbuf
& (((2 << j
) << numbit
) - 1)) |
414 ((bitbuf
& ~(((2 << j
) << numbit
) - 1)) << 1);
416 notbitstream
= ~bitstream
;
419 while (numbit
>= 8) {
425 bitbuf
|= 0x7e7e << numbit
;
427 while (numbit
>= 8) {
432 bc
->hdlctx
.bufptr
= bc
->hdlctx
.buf
;
433 bc
->hdlctx
.bufcnt
= wp
- bc
->hdlctx
.buf
;
435 bc
->stats
.tx_packets
++;
438 /* ---------------------------------------------------------------------- */
440 static unsigned short random_seed
;
442 static inline unsigned short random_num(void)
444 random_seed
= 28629 * random_seed
+ 157;
448 /* ---------------------------------------------------------------------- */
450 static int transmit(struct baycom_state
*bc
, int cnt
, unsigned char stat
)
452 struct parport
*pp
= bc
->pdev
->port
;
453 unsigned char tmp
[128];
456 if (bc
->hdlctx
.state
== tx_tail
&& !(stat
& EPP_PTTBIT
))
457 bc
->hdlctx
.state
= tx_idle
;
458 if (bc
->hdlctx
.state
== tx_idle
&& bc
->hdlctx
.calibrate
<= 0) {
459 if (bc
->hdlctx
.bufcnt
<= 0)
461 if (bc
->hdlctx
.bufcnt
<= 0)
463 if (!bc
->ch_params
.fulldup
) {
464 if (!(stat
& EPP_DCDBIT
)) {
465 bc
->hdlctx
.slotcnt
= bc
->ch_params
.slottime
;
468 if ((--bc
->hdlctx
.slotcnt
) > 0)
470 bc
->hdlctx
.slotcnt
= bc
->ch_params
.slottime
;
471 if ((random_num() % 256) > bc
->ch_params
.ppersist
)
475 if (bc
->hdlctx
.state
== tx_idle
&& bc
->hdlctx
.bufcnt
> 0) {
476 bc
->hdlctx
.state
= tx_keyup
;
477 bc
->hdlctx
.flags
= tenms_to_flags(bc
, bc
->ch_params
.tx_delay
);
481 switch (bc
->hdlctx
.state
) {
483 i
= min_t(int, cnt
, bc
->hdlctx
.flags
);
485 bc
->hdlctx
.flags
-= i
;
486 if (bc
->hdlctx
.flags
<= 0)
487 bc
->hdlctx
.state
= tx_data
;
488 memset(tmp
, 0x7e, sizeof(tmp
));
490 j
= (i
> sizeof(tmp
)) ? sizeof(tmp
) : i
;
491 if (j
!= pp
->ops
->epp_write_data(pp
, tmp
, j
, 0))
498 if (bc
->hdlctx
.bufcnt
<= 0) {
500 if (bc
->hdlctx
.bufcnt
<= 0) {
501 bc
->hdlctx
.state
= tx_tail
;
502 bc
->hdlctx
.flags
= tenms_to_flags(bc
, bc
->ch_params
.tx_tail
);
506 i
= min_t(int, cnt
, bc
->hdlctx
.bufcnt
);
507 bc
->hdlctx
.bufcnt
-= i
;
509 if (i
!= pp
->ops
->epp_write_data(pp
, bc
->hdlctx
.bufptr
, i
, 0))
511 bc
->hdlctx
.bufptr
+= i
;
516 if (bc
->hdlctx
.bufcnt
> 0) {
517 bc
->hdlctx
.state
= tx_data
;
520 i
= min_t(int, cnt
, bc
->hdlctx
.flags
);
523 bc
->hdlctx
.flags
-= i
;
524 memset(tmp
, 0x7e, sizeof(tmp
));
526 j
= (i
> sizeof(tmp
)) ? sizeof(tmp
) : i
;
527 if (j
!= pp
->ops
->epp_write_data(pp
, tmp
, j
, 0))
534 default: /* fall through */
535 if (bc
->hdlctx
.calibrate
<= 0)
537 i
= min_t(int, cnt
, bc
->hdlctx
.calibrate
);
539 bc
->hdlctx
.calibrate
-= i
;
540 memset(tmp
, 0, sizeof(tmp
));
542 j
= (i
> sizeof(tmp
)) ? sizeof(tmp
) : i
;
543 if (j
!= pp
->ops
->epp_write_data(pp
, tmp
, j
, 0))
553 /* ---------------------------------------------------------------------- */
555 static void do_rxpacket(struct net_device
*dev
)
557 struct baycom_state
*bc
= netdev_priv(dev
);
562 if (bc
->hdlcrx
.bufcnt
< 4)
564 if (!check_crc_ccitt(bc
->hdlcrx
.buf
, bc
->hdlcrx
.bufcnt
))
566 pktlen
= bc
->hdlcrx
.bufcnt
-2+1; /* KISS kludge */
567 if (!(skb
= dev_alloc_skb(pktlen
))) {
568 printk("%s: memory squeeze, dropping packet\n", dev
->name
);
569 bc
->stats
.rx_dropped
++;
572 cp
= skb_put(skb
, pktlen
);
573 *cp
++ = 0; /* KISS kludge */
574 memcpy(cp
, bc
->hdlcrx
.buf
, pktlen
- 1);
575 skb
->protocol
= ax25_type_trans(skb
, dev
);
577 dev
->last_rx
= jiffies
;
578 bc
->stats
.rx_packets
++;
581 static int receive(struct net_device
*dev
, int cnt
)
583 struct baycom_state
*bc
= netdev_priv(dev
);
584 struct parport
*pp
= bc
->pdev
->port
;
585 unsigned int bitbuf
, notbitstream
, bitstream
, numbits
, state
;
586 unsigned char tmp
[128];
591 numbits
= bc
->hdlcrx
.numbits
;
592 state
= bc
->hdlcrx
.state
;
593 bitstream
= bc
->hdlcrx
.bitstream
;
594 bitbuf
= bc
->hdlcrx
.bitbuf
;
596 cnt2
= (cnt
> sizeof(tmp
)) ? sizeof(tmp
) : cnt
;
598 if (cnt2
!= pp
->ops
->epp_read_data(pp
, tmp
, cnt2
, 0)) {
603 for (; cnt2
> 0; cnt2
--, cp
++) {
605 bitstream
|= (*cp
) << 8;
607 bitbuf
|= (*cp
) << 8;
609 notbitstream
= ~bitstream
;
610 for (j
= 0; j
< 8; j
++) {
613 if (unlikely(!(notbitstream
& (0x0fc << j
)))) {
616 if (!(notbitstream
& (0x1fc << j
)))
619 /* not flag received */
620 else if (!(bitstream
& (0x1fe << j
)) != (0x0fc << j
)) {
623 bc
->hdlcrx
.bufcnt
= 0;
624 bc
->hdlcrx
.bufptr
= bc
->hdlcrx
.buf
;
631 else if (unlikely((bitstream
& (0x1f8 << j
)) == (0xf8 << j
))) {
633 bitbuf
= (bitbuf
& ((~0xff) << j
)) | ((bitbuf
& ~((~0xff) << j
)) << 1);
636 while (state
&& numbits
>= 8) {
637 if (bc
->hdlcrx
.bufcnt
>= TXBUFFER_SIZE
) {
640 *(bc
->hdlcrx
.bufptr
)++ = bitbuf
>> (16-numbits
);
647 bc
->hdlcrx
.numbits
= numbits
;
648 bc
->hdlcrx
.state
= state
;
649 bc
->hdlcrx
.bitstream
= bitstream
;
650 bc
->hdlcrx
.bitbuf
= bitbuf
;
654 /* --------------------------------------------------------------------- */
665 #endif /* __i386__ */
667 static void epp_bh(struct net_device
*dev
)
669 struct baycom_state
*bc
;
672 unsigned char tmp
[2];
673 unsigned int time1
= 0, time2
= 0, time3
= 0;
676 bc
= netdev_priv(dev
);
677 if (!bc
->work_running
)
682 if (pp
->ops
->epp_read_addr(pp
, &stat
, 1, 0) != 1)
685 bc
->debug_vals
.last_pllcorr
= stat
;
687 if (bc
->modem
== EPP_FPGAEXTSTATUS
) {
688 /* get input count */
689 tmp
[0] = EPP_TX_FIFO_ENABLE
|EPP_RX_FIFO_ENABLE
|EPP_MODEM_ENABLE
|1;
690 if (pp
->ops
->epp_write_addr(pp
, tmp
, 1, 0) != 1)
692 if (pp
->ops
->epp_read_addr(pp
, tmp
, 2, 0) != 2)
694 cnt
= tmp
[0] | (tmp
[1] << 8);
696 /* get output count */
697 tmp
[0] = EPP_TX_FIFO_ENABLE
|EPP_RX_FIFO_ENABLE
|EPP_MODEM_ENABLE
|2;
698 if (pp
->ops
->epp_write_addr(pp
, tmp
, 1, 0) != 1)
700 if (pp
->ops
->epp_read_addr(pp
, tmp
, 2, 0) != 2)
702 cnt2
= tmp
[0] | (tmp
[1] << 8);
703 cnt2
= 16384 - (cnt2
& 0x7fff);
704 /* return to normal */
705 tmp
[0] = EPP_TX_FIFO_ENABLE
|EPP_RX_FIFO_ENABLE
|EPP_MODEM_ENABLE
;
706 if (pp
->ops
->epp_write_addr(pp
, tmp
, 1, 0) != 1)
708 if (transmit(bc
, cnt2
, stat
))
711 if (receive(dev
, cnt
))
713 if (pp
->ops
->epp_read_addr(pp
, &stat
, 1, 0) != 1)
718 switch (stat
& (EPP_NTAEF
|EPP_NTHF
)) {
735 if (transmit(bc
, cnt
, stat
))
739 while ((stat
& (EPP_NRAEF
|EPP_NRHF
)) != EPP_NRHF
) {
740 switch (stat
& (EPP_NRAEF
|EPP_NRHF
)) {
753 if (receive(dev
, cnt
))
755 if (pp
->ops
->epp_read_addr(pp
, &stat
, 1, 0) != 1)
759 if (bc
->bitrate
< 50000)
761 else if (bc
->bitrate
< 100000)
763 while (cnt
> 0 && stat
& EPP_NREF
) {
767 if (pp
->ops
->epp_read_addr(pp
, &stat
, 1, 0) != 1)
773 bc
->debug_vals
.mod_cycles
= time2
- time1
;
774 bc
->debug_vals
.demod_cycles
= time3
- time2
;
775 #endif /* BAYCOM_DEBUG */
776 schedule_delayed_work(&bc
->run_work
, 1);
778 netif_wake_queue(dev
);
781 printk(KERN_ERR
"%s: EPP timeout!\n", bc_drvname
);
784 /* ---------------------------------------------------------------------- */
786 * ===================== network driver interface =========================
789 static int baycom_send_packet(struct sk_buff
*skb
, struct net_device
*dev
)
791 struct baycom_state
*bc
= netdev_priv(dev
);
793 if (skb
->data
[0] != 0) {
794 do_kiss_params(bc
, skb
->data
, skb
->len
);
800 /* strip KISS byte */
801 if (skb
->len
>= HDLCDRV_MAXFLEN
+1 || skb
->len
< 3) {
805 netif_stop_queue(dev
);
810 /* --------------------------------------------------------------------- */
812 static int baycom_set_mac_address(struct net_device
*dev
, void *addr
)
814 struct sockaddr
*sa
= (struct sockaddr
*)addr
;
816 /* addr is an AX.25 shifted ASCII mac address */
817 memcpy(dev
->dev_addr
, sa
->sa_data
, dev
->addr_len
);
821 /* --------------------------------------------------------------------- */
823 static struct net_device_stats
*baycom_get_stats(struct net_device
*dev
)
825 struct baycom_state
*bc
= netdev_priv(dev
);
828 * Get the current statistics. This may be called with the
829 * card open or closed.
834 /* --------------------------------------------------------------------- */
836 static void epp_wakeup(void *handle
)
838 struct net_device
*dev
= (struct net_device
*)handle
;
839 struct baycom_state
*bc
= netdev_priv(dev
);
841 printk(KERN_DEBUG
"baycom_epp: %s: why am I being woken up?\n", dev
->name
);
842 if (!parport_claim(bc
->pdev
))
843 printk(KERN_DEBUG
"baycom_epp: %s: I'm broken.\n", dev
->name
);
846 /* --------------------------------------------------------------------- */
849 * Open/initialize the board. This is called (in the current kernel)
850 * sometime after booting when the 'ifconfig' program is run.
852 * This routine should set everything up anew at each open, even
853 * registers that "should" only need to be set once at boot, so that
854 * there is non-reboot way to recover if something goes wrong.
857 static int epp_open(struct net_device
*dev
)
859 struct baycom_state
*bc
= netdev_priv(dev
);
860 struct parport
*pp
= parport_find_base(dev
->base_addr
);
862 unsigned char tmp
[128];
864 unsigned long tstart
;
867 printk(KERN_ERR
"%s: parport at 0x%lx unknown\n", bc_drvname
, dev
->base_addr
);
872 printk(KERN_ERR
"%s: parport at 0x%lx has no irq\n", bc_drvname
, pp
->base
);
873 parport_put_port(pp
);
877 if ((~pp
->modes
) & (PARPORT_MODE_TRISTATE
| PARPORT_MODE_PCSPP
| PARPORT_MODE_SAFEININT
)) {
878 printk(KERN_ERR
"%s: parport at 0x%lx cannot be used\n",
879 bc_drvname
, pp
->base
);
880 parport_put_port(pp
);
883 memset(&bc
->modem
, 0, sizeof(bc
->modem
));
884 bc
->pdev
= parport_register_device(pp
, dev
->name
, NULL
, epp_wakeup
,
885 epp_interrupt
, PARPORT_DEV_EXCL
, dev
);
886 parport_put_port(pp
);
888 printk(KERN_ERR
"%s: cannot register parport at 0x%lx\n", bc_drvname
, pp
->base
);
891 if (parport_claim(bc
->pdev
)) {
892 printk(KERN_ERR
"%s: parport at 0x%lx busy\n", bc_drvname
, pp
->base
);
893 parport_unregister_device(bc
->pdev
);
896 dev
->irq
= /*pp->irq*/ 0;
897 INIT_WORK(&bc
->run_work
, (void *)(void *)epp_bh
, dev
);
898 bc
->work_running
= 1;
899 bc
->modem
= EPP_CONVENTIONAL
;
901 printk(KERN_INFO
"%s: no FPGA detected, assuming conventional EPP modem\n", bc_drvname
);
903 bc
->modem
= /*EPP_FPGA*/ EPP_FPGAEXTSTATUS
;
904 parport_write_control(pp
, LPTCTRL_PROGRAM
); /* prepare EPP mode; we aren't using interrupts */
905 /* reset the modem */
907 tmp
[1] = EPP_TX_FIFO_ENABLE
|EPP_RX_FIFO_ENABLE
|EPP_MODEM_ENABLE
;
908 if (pp
->ops
->epp_write_addr(pp
, tmp
, 2, 0) != 2)
910 /* autoprobe baud rate */
913 while ((signed)(jiffies
-tstart
-HZ
/3) < 0) {
914 if (pp
->ops
->epp_read_addr(pp
, &stat
, 1, 0) != 1)
916 if ((stat
& (EPP_NRAEF
|EPP_NRHF
)) == EPP_NRHF
) {
920 if (pp
->ops
->epp_read_data(pp
, tmp
, 128, 0) != 128)
922 if (pp
->ops
->epp_read_data(pp
, tmp
, 128, 0) != 128)
926 for (j
= 0; j
< 256; j
++) {
927 if (pp
->ops
->epp_read_addr(pp
, &stat
, 1, 0) != 1)
929 if (!(stat
& EPP_NREF
))
931 if (pp
->ops
->epp_read_data(pp
, tmp
, 1, 0) != 1)
935 tstart
= jiffies
- tstart
;
936 bc
->bitrate
= i
* (8 * HZ
) / tstart
;
938 i
= bc
->bitrate
>> 3;
939 while (j
< 7 && i
> 150) {
943 printk(KERN_INFO
"%s: autoprobed bitrate: %d int divider: %d int rate: %d\n",
944 bc_drvname
, bc
->bitrate
, j
, bc
->bitrate
>> (j
+2));
945 tmp
[0] = EPP_TX_FIFO_ENABLE
|EPP_RX_FIFO_ENABLE
|EPP_MODEM_ENABLE
/*|j*/;
946 if (pp
->ops
->epp_write_addr(pp
, tmp
, 1, 0) != 1)
949 * initialise hdlc variables
951 bc
->hdlcrx
.state
= 0;
952 bc
->hdlcrx
.numbits
= 0;
953 bc
->hdlctx
.state
= tx_idle
;
954 bc
->hdlctx
.bufcnt
= 0;
955 bc
->hdlctx
.slotcnt
= bc
->ch_params
.slottime
;
956 bc
->hdlctx
.calibrate
= 0;
957 /* start the bottom half stuff */
958 schedule_delayed_work(&bc
->run_work
, 1);
959 netif_start_queue(dev
);
963 printk(KERN_ERR
"%s: epp timeout during bitrate probe\n", bc_drvname
);
964 parport_write_control(pp
, 0); /* reset the adapter */
965 parport_release(bc
->pdev
);
966 parport_unregister_device(bc
->pdev
);
970 /* --------------------------------------------------------------------- */
972 static int epp_close(struct net_device
*dev
)
974 struct baycom_state
*bc
= netdev_priv(dev
);
975 struct parport
*pp
= bc
->pdev
->port
;
976 unsigned char tmp
[1];
978 bc
->work_running
= 0;
979 flush_scheduled_work();
980 bc
->stat
= EPP_DCDBIT
;
982 pp
->ops
->epp_write_addr(pp
, tmp
, 1, 0);
983 parport_write_control(pp
, 0); /* reset the adapter */
984 parport_release(bc
->pdev
);
985 parport_unregister_device(bc
->pdev
);
987 dev_kfree_skb(bc
->skb
);
989 printk(KERN_INFO
"%s: close epp at iobase 0x%lx irq %u\n",
990 bc_drvname
, dev
->base_addr
, dev
->irq
);
994 /* --------------------------------------------------------------------- */
996 static int baycom_setmode(struct baycom_state
*bc
, const char *modestr
)
1000 if (strstr(modestr
,"intclk"))
1002 if (strstr(modestr
,"extclk"))
1004 if (strstr(modestr
,"intmodem"))
1005 bc
->cfg
.extmodem
= 0;
1006 if (strstr(modestr
,"extmodem"))
1007 bc
->cfg
.extmodem
= 1;
1008 if (strstr(modestr
,"noloopback"))
1009 bc
->cfg
.loopback
= 0;
1010 if (strstr(modestr
,"loopback"))
1011 bc
->cfg
.loopback
= 1;
1012 if ((cp
= strstr(modestr
,"fclk="))) {
1013 bc
->cfg
.fclk
= simple_strtoul(cp
+5, NULL
, 0);
1014 if (bc
->cfg
.fclk
< 1000000)
1015 bc
->cfg
.fclk
= 1000000;
1016 if (bc
->cfg
.fclk
> 25000000)
1017 bc
->cfg
.fclk
= 25000000;
1019 if ((cp
= strstr(modestr
,"bps="))) {
1020 bc
->cfg
.bps
= simple_strtoul(cp
+4, NULL
, 0);
1021 if (bc
->cfg
.bps
< 1000)
1023 if (bc
->cfg
.bps
> 1500000)
1024 bc
->cfg
.bps
= 1500000;
1029 /* --------------------------------------------------------------------- */
1031 static int baycom_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
1033 struct baycom_state
*bc
= netdev_priv(dev
);
1034 struct hdlcdrv_ioctl hi
;
1036 if (cmd
!= SIOCDEVPRIVATE
)
1037 return -ENOIOCTLCMD
;
1039 if (copy_from_user(&hi
, ifr
->ifr_data
, sizeof(hi
)))
1043 return -ENOIOCTLCMD
;
1045 case HDLCDRVCTL_GETCHANNELPAR
:
1046 hi
.data
.cp
.tx_delay
= bc
->ch_params
.tx_delay
;
1047 hi
.data
.cp
.tx_tail
= bc
->ch_params
.tx_tail
;
1048 hi
.data
.cp
.slottime
= bc
->ch_params
.slottime
;
1049 hi
.data
.cp
.ppersist
= bc
->ch_params
.ppersist
;
1050 hi
.data
.cp
.fulldup
= bc
->ch_params
.fulldup
;
1053 case HDLCDRVCTL_SETCHANNELPAR
:
1054 if (!capable(CAP_NET_ADMIN
))
1056 bc
->ch_params
.tx_delay
= hi
.data
.cp
.tx_delay
;
1057 bc
->ch_params
.tx_tail
= hi
.data
.cp
.tx_tail
;
1058 bc
->ch_params
.slottime
= hi
.data
.cp
.slottime
;
1059 bc
->ch_params
.ppersist
= hi
.data
.cp
.ppersist
;
1060 bc
->ch_params
.fulldup
= hi
.data
.cp
.fulldup
;
1061 bc
->hdlctx
.slotcnt
= 1;
1064 case HDLCDRVCTL_GETMODEMPAR
:
1065 hi
.data
.mp
.iobase
= dev
->base_addr
;
1066 hi
.data
.mp
.irq
= dev
->irq
;
1067 hi
.data
.mp
.dma
= dev
->dma
;
1068 hi
.data
.mp
.dma2
= 0;
1069 hi
.data
.mp
.seriobase
= 0;
1070 hi
.data
.mp
.pariobase
= 0;
1071 hi
.data
.mp
.midiiobase
= 0;
1074 case HDLCDRVCTL_SETMODEMPAR
:
1075 if ((!capable(CAP_SYS_RAWIO
)) || netif_running(dev
))
1077 dev
->base_addr
= hi
.data
.mp
.iobase
;
1078 dev
->irq
= /*hi.data.mp.irq*/0;
1079 dev
->dma
= /*hi.data.mp.dma*/0;
1082 case HDLCDRVCTL_GETSTAT
:
1083 hi
.data
.cs
.ptt
= !!(bc
->stat
& EPP_PTTBIT
);
1084 hi
.data
.cs
.dcd
= !(bc
->stat
& EPP_DCDBIT
);
1085 hi
.data
.cs
.ptt_keyed
= bc
->ptt_keyed
;
1086 hi
.data
.cs
.tx_packets
= bc
->stats
.tx_packets
;
1087 hi
.data
.cs
.tx_errors
= bc
->stats
.tx_errors
;
1088 hi
.data
.cs
.rx_packets
= bc
->stats
.rx_packets
;
1089 hi
.data
.cs
.rx_errors
= bc
->stats
.rx_errors
;
1092 case HDLCDRVCTL_OLDGETSTAT
:
1093 hi
.data
.ocs
.ptt
= !!(bc
->stat
& EPP_PTTBIT
);
1094 hi
.data
.ocs
.dcd
= !(bc
->stat
& EPP_DCDBIT
);
1095 hi
.data
.ocs
.ptt_keyed
= bc
->ptt_keyed
;
1098 case HDLCDRVCTL_CALIBRATE
:
1099 if (!capable(CAP_SYS_RAWIO
))
1101 bc
->hdlctx
.calibrate
= hi
.data
.calibrate
* bc
->bitrate
/ 8;
1104 case HDLCDRVCTL_DRIVERNAME
:
1105 strncpy(hi
.data
.drivername
, "baycom_epp", sizeof(hi
.data
.drivername
));
1108 case HDLCDRVCTL_GETMODE
:
1109 sprintf(hi
.data
.modename
, "%sclk,%smodem,fclk=%d,bps=%d%s",
1110 bc
->cfg
.intclk
? "int" : "ext",
1111 bc
->cfg
.extmodem
? "ext" : "int", bc
->cfg
.fclk
, bc
->cfg
.bps
,
1112 bc
->cfg
.loopback
? ",loopback" : "");
1115 case HDLCDRVCTL_SETMODE
:
1116 if (!capable(CAP_NET_ADMIN
) || netif_running(dev
))
1118 hi
.data
.modename
[sizeof(hi
.data
.modename
)-1] = '\0';
1119 return baycom_setmode(bc
, hi
.data
.modename
);
1121 case HDLCDRVCTL_MODELIST
:
1122 strncpy(hi
.data
.modename
, "intclk,extclk,intmodem,extmodem,divider=x",
1123 sizeof(hi
.data
.modename
));
1126 case HDLCDRVCTL_MODEMPARMASK
:
1127 return HDLCDRV_PARMASK_IOBASE
;
1130 if (copy_to_user(ifr
->ifr_data
, &hi
, sizeof(hi
)))
1135 /* --------------------------------------------------------------------- */
1138 * Check for a network adaptor of this type, and return '0' if one exists.
1139 * If dev->base_addr == 0, probe all likely locations.
1140 * If dev->base_addr == 1, always return failure.
1141 * If dev->base_addr == 2, allocate space for the device and return success
1142 * (detachable devices only).
1144 static void baycom_probe(struct net_device
*dev
)
1146 static char ax25_bcast
[AX25_ADDR_LEN
] = {
1147 'Q' << 1, 'S' << 1, 'T' << 1, ' ' << 1, ' ' << 1, ' ' << 1, '0' << 1
1149 static char ax25_nocall
[AX25_ADDR_LEN
] = {
1150 'L' << 1, 'I' << 1, 'N' << 1, 'U' << 1, 'X' << 1, ' ' << 1, '1' << 1
1152 const struct hdlcdrv_channel_params dflt_ch_params
= {
1155 struct baycom_state
*bc
;
1158 * not a real probe! only initialize data structures
1160 bc
= netdev_priv(dev
);
1162 * initialize the baycom_state struct
1164 bc
->ch_params
= dflt_ch_params
;
1168 * initialize the device struct
1170 dev
->open
= epp_open
;
1171 dev
->stop
= epp_close
;
1172 dev
->do_ioctl
= baycom_ioctl
;
1173 dev
->hard_start_xmit
= baycom_send_packet
;
1174 dev
->get_stats
= baycom_get_stats
;
1176 /* Fill in the fields of the device structure */
1179 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
1180 dev
->hard_header
= ax25_encapsulate
;
1181 dev
->rebuild_header
= ax25_rebuild_header
;
1182 #else /* CONFIG_AX25 || CONFIG_AX25_MODULE */
1183 dev
->hard_header
= NULL
;
1184 dev
->rebuild_header
= NULL
;
1185 #endif /* CONFIG_AX25 || CONFIG_AX25_MODULE */
1186 dev
->set_mac_address
= baycom_set_mac_address
;
1188 dev
->type
= ARPHRD_AX25
; /* AF_AX25 device */
1189 dev
->hard_header_len
= AX25_MAX_HEADER_LEN
+ AX25_BPQ_HEADER_LEN
;
1190 dev
->mtu
= AX25_DEF_PACLEN
; /* eth_mtu is the default */
1191 dev
->addr_len
= AX25_ADDR_LEN
; /* sizeof an ax.25 address */
1192 memcpy(dev
->broadcast
, ax25_bcast
, AX25_ADDR_LEN
);
1193 memcpy(dev
->dev_addr
, ax25_nocall
, AX25_ADDR_LEN
);
1194 dev
->tx_queue_len
= 16;
1196 /* New style flags */
1200 /* --------------------------------------------------------------------- */
1203 * command line settable parameters
1205 static const char *mode
[NR_PORTS
] = { "", };
1206 static int iobase
[NR_PORTS
] = { 0x378, };
1208 module_param_array(mode
, charp
, NULL
, 0);
1209 MODULE_PARM_DESC(mode
, "baycom operating mode");
1210 module_param_array(iobase
, int, NULL
, 0);
1211 MODULE_PARM_DESC(iobase
, "baycom io base address");
1213 MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
1214 MODULE_DESCRIPTION("Baycom epp amateur radio modem driver");
1215 MODULE_LICENSE("GPL");
1217 /* --------------------------------------------------------------------- */
1219 static void __init
baycom_epp_dev_setup(struct net_device
*dev
)
1221 struct baycom_state
*bc
= netdev_priv(dev
);
1224 * initialize part of the baycom_state struct
1226 bc
->magic
= BAYCOM_MAGIC
;
1227 bc
->cfg
.fclk
= 19666600;
1230 * initialize part of the device struct
1235 static int __init
init_baycomepp(void)
1242 * register net devices
1244 for (i
= 0; i
< NR_PORTS
; i
++) {
1245 struct net_device
*dev
;
1247 dev
= alloc_netdev(sizeof(struct baycom_state
), "bce%d",
1248 baycom_epp_dev_setup
);
1251 printk(KERN_WARNING
"bce%d : out of memory\n", i
);
1252 return found
? 0 : -ENOMEM
;
1255 sprintf(dev
->name
, "bce%d", i
);
1256 dev
->base_addr
= iobase
[i
];
1263 if (register_netdev(dev
)) {
1264 printk(KERN_WARNING
"%s: cannot register net device %s\n", bc_drvname
, dev
->name
);
1268 if (set_hw
&& baycom_setmode(netdev_priv(dev
), mode
[i
]))
1270 baycom_device
[i
] = dev
;
1274 return found
? 0 : -ENXIO
;
1277 static void __exit
cleanup_baycomepp(void)
1281 for(i
= 0; i
< NR_PORTS
; i
++) {
1282 struct net_device
*dev
= baycom_device
[i
];
1285 struct baycom_state
*bc
= netdev_priv(dev
);
1286 if (bc
->magic
== BAYCOM_MAGIC
) {
1287 unregister_netdev(dev
);
1290 printk(paranoia_str
, "cleanup_module");
1295 module_init(init_baycomepp
);
1296 module_exit(cleanup_baycomepp
);
1298 /* --------------------------------------------------------------------- */
1303 * format: baycom_epp=io,mode
1304 * mode: fpga config options
1307 static int __init
baycom_epp_setup(char *str
)
1309 static unsigned __initdata nr_dev
= 0;
1312 if (nr_dev
>= NR_PORTS
)
1314 str
= get_options(str
, 2, ints
);
1318 iobase
[nr_dev
] = ints
[1];
1323 __setup("baycom_epp=", baycom_epp_setup
);
1326 /* --------------------------------------------------------------------- */