1/*****************************************************************************/ 2 3/* 4 * baycom_epp.c -- baycom epp radio modem driver. 5 * 6 * Copyright (C) 1998-2000 7 * Thomas Sailer (sailer@ife.ee.ethz.ch) 8 * 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. 13 * 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. 18 * 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. 22 * 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. 26 * 27 * 28 * History: 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 38 * 39 */ 40 41/*****************************************************************************/ 42 43#include <linux/crc-ccitt.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> 49#include <linux/fs.h> 50#include <linux/parport.h> 51#include <linux/if_arp.h> 52#include <linux/hdlcdrv.h> 53#include <linux/baycom.h> 54#include <linux/jiffies.h> 55#include <linux/random.h> 56#include <net/ax25.h> 57#include <asm/uaccess.h> 58 59/* --------------------------------------------------------------------- */ 60 61#define BAYCOM_DEBUG 62#define BAYCOM_MAGIC 19730510 63 64/* --------------------------------------------------------------------- */ 65 66static const char paranoia_str[] = KERN_ERR 67 "baycom_epp: bad magic number for hdlcdrv_state struct in routine %s\n"; 68 69static const char bc_drvname[] = "baycom_epp"; 70static const char bc_drvinfo[] = KERN_INFO "baycom_epp: (C) 1998-2000 Thomas Sailer, HB9JNX/AE4WA\n" 71KERN_INFO "baycom_epp: version 0.7 compiled " __TIME__ " " __DATE__ "\n"; 72 73/* --------------------------------------------------------------------- */ 74 75#define NR_PORTS 4 76 77static struct net_device *baycom_device[NR_PORTS]; 78 79/* --------------------------------------------------------------------- */ 80 81/* EPP status register */ 82#define EPP_DCDBIT 0x80 83#define EPP_PTTBIT 0x08 84#define EPP_NREF 0x01 85#define EPP_NRAEF 0x02 86#define EPP_NRHF 0x04 87#define EPP_NTHF 0x20 88#define EPP_NTAEF 0x10 89#define EPP_NTEF EPP_PTTBIT 90 91/* EPP control register */ 92#define EPP_TX_FIFO_ENABLE 0x10 93#define EPP_RX_FIFO_ENABLE 0x08 94#define EPP_MODEM_ENABLE 0x20 95#define EPP_LEDS 0xC0 96#define EPP_IRQ_ENABLE 0x10 97 98/* LPT registers */ 99#define LPTREG_ECONTROL 0x402 100#define LPTREG_CONFIGB 0x401 101#define LPTREG_CONFIGA 0x400 102#define LPTREG_EPPDATA 0x004 103#define LPTREG_EPPADDR 0x003 104#define LPTREG_CONTROL 0x002 105#define LPTREG_STATUS 0x001 106#define LPTREG_DATA 0x000 107 108/* LPT control register */ 109#define LPTCTRL_PROGRAM 0x04 /* 0 to reprogram */ 110#define LPTCTRL_WRITE 0x01 111#define LPTCTRL_ADDRSTB 0x08 112#define LPTCTRL_DATASTB 0x02 113#define LPTCTRL_INTEN 0x10 114 115/* LPT status register */ 116#define LPTSTAT_SHIFT_NINTR 6 117#define LPTSTAT_WAIT 0x80 118#define LPTSTAT_NINTR (1<<LPTSTAT_SHIFT_NINTR) 119#define LPTSTAT_PE 0x20 120#define LPTSTAT_DONE 0x10 121#define LPTSTAT_NERROR 0x08 122#define LPTSTAT_EPPTIMEOUT 0x01 123 124/* LPT data register */ 125#define LPTDATA_SHIFT_TDI 0 126#define LPTDATA_SHIFT_TMS 2 127#define LPTDATA_TDI (1<<LPTDATA_SHIFT_TDI) 128#define LPTDATA_TCK 0x02 129#define LPTDATA_TMS (1<<LPTDATA_SHIFT_TMS) 130#define LPTDATA_INITBIAS 0x80 131 132 133/* EPP modem config/status bits */ 134#define EPP_DCDBIT 0x80 135#define EPP_PTTBIT 0x08 136#define EPP_RXEBIT 0x01 137#define EPP_RXAEBIT 0x02 138#define EPP_RXHFULL 0x04 139 140#define EPP_NTHF 0x20 141#define EPP_NTAEF 0x10 142#define EPP_NTEF EPP_PTTBIT 143 144#define EPP_TX_FIFO_ENABLE 0x10 145#define EPP_RX_FIFO_ENABLE 0x08 146#define EPP_MODEM_ENABLE 0x20 147#define EPP_LEDS 0xC0 148#define EPP_IRQ_ENABLE 0x10 149 150/* Xilinx 4k JTAG instructions */ 151#define XC4K_IRLENGTH 3 152#define XC4K_EXTEST 0 153#define XC4K_PRELOAD 1 154#define XC4K_CONFIGURE 5 155#define XC4K_BYPASS 7 156 157#define EPP_CONVENTIONAL 0 158#define EPP_FPGA 1 159#define EPP_FPGAEXTSTATUS 2 160 161#define TXBUFFER_SIZE ((HDLCDRV_MAXFLEN*6/5)+8) 162 163/* ---------------------------------------------------------------------- */ 164/* 165 * Information that need to be kept for each board. 166 */ 167 168struct baycom_state { 169 int magic; 170 171 struct pardevice *pdev; 172 struct net_device *dev; 173 unsigned int work_running; 174 struct delayed_work run_work; 175 unsigned int modem; 176 unsigned int bitrate; 177 unsigned char stat; 178 179 struct { 180 unsigned int intclk; 181 unsigned int fclk; 182 unsigned int bps; 183 unsigned int extmodem; 184 unsigned int loopback; 185 } cfg; 186 187 struct hdlcdrv_channel_params ch_params; 188 189 struct { 190 unsigned int bitbuf, bitstream, numbits, state; 191 unsigned char *bufptr; 192 int bufcnt; 193 unsigned char buf[TXBUFFER_SIZE]; 194 } hdlcrx; 195 196 struct { 197 int calibrate; 198 int slotcnt; 199 int flags; 200 enum { tx_idle = 0, tx_keyup, tx_data, tx_tail } state; 201 unsigned char *bufptr; 202 int bufcnt; 203 unsigned char buf[TXBUFFER_SIZE]; 204 } hdlctx; 205 206 struct net_device_stats stats; 207 unsigned int ptt_keyed; 208 struct sk_buff *skb; /* next transmit packet */ 209 210#ifdef BAYCOM_DEBUG 211 struct debug_vals { 212 unsigned long last_jiffies; 213 unsigned cur_intcnt; 214 unsigned last_intcnt; 215 int cur_pllcorr; 216 int last_pllcorr; 217 unsigned int mod_cycles; 218 unsigned int demod_cycles; 219 } debug_vals; 220#endif /* BAYCOM_DEBUG */ 221}; 222 223/* --------------------------------------------------------------------- */ 224 225#define KISS_VERBOSE 226 227/* --------------------------------------------------------------------- */ 228 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 236 237/* --------------------------------------------------------------------- */ 238/* 239 * the CRC routines are stolen from WAMPES 240 * by Dieter Deyke 241 */ 242 243 244/*---------------------------------------------------------------------------*/ 245 246 247/*---------------------------------------------------------------------------*/ 248 249static inline int check_crc_ccitt(const unsigned char *buf, int cnt) 250{ 251 return (crc_ccitt(0xffff, buf, cnt) & 0xffff) == 0xf0b8; 252} 253 254/*---------------------------------------------------------------------------*/ 255 256static inline int calc_crc_ccitt(const unsigned char *buf, int cnt) 257{ 258 return (crc_ccitt(0xffff, buf, cnt) ^ 0xffff) & 0xffff; 259} 260 261/* ---------------------------------------------------------------------- */ 262 263#define tenms_to_flags(bc,tenms) ((tenms * bc->bitrate) / 800) 264 265/* --------------------------------------------------------------------- */ 266 267static inline void baycom_int_freq(struct baycom_state *bc) 268{ 269#ifdef BAYCOM_DEBUG 270 unsigned long cur_jiffies = jiffies; 271 /* 272 * measure the interrupt frequency 273 */ 274 bc->debug_vals.cur_intcnt++; 275 if (time_after_eq(cur_jiffies, bc->debug_vals.last_jiffies + HZ)) { 276 bc->debug_vals.last_jiffies = cur_jiffies; 277 bc->debug_vals.last_intcnt = bc->debug_vals.cur_intcnt; 278 bc->debug_vals.cur_intcnt = 0; 279 bc->debug_vals.last_pllcorr = bc->debug_vals.cur_pllcorr; 280 bc->debug_vals.cur_pllcorr = 0; 281 } 282#endif /* BAYCOM_DEBUG */ 283} 284 285/* ---------------------------------------------------------------------- */ 286/* 287 * eppconfig_path should be setable via /proc/sys. 288 */ 289 290static char eppconfig_path[256] = "/usr/sbin/eppfpga"; 291 292static char *envp[] = { "HOME=/", "TERM=linux", "PATH=/usr/bin:/bin", NULL }; 293 294/* eppconfig: called during ifconfig up to configure the modem */ 295static int eppconfig(struct baycom_state *bc) 296{ 297 char modearg[256]; 298 char portarg[16]; 299 char *argv[] = { eppconfig_path, "-s", "-p", portarg, "-m", modearg, 300 NULL }; 301 302 /* set up arguments */ 303 sprintf(modearg, "%sclk,%smodem,fclk=%d,bps=%d,divider=%d%s,extstat", 304 bc->cfg.intclk ? "int" : "ext", 305 bc->cfg.extmodem ? "ext" : "int", bc->cfg.fclk, bc->cfg.bps, 306 (bc->cfg.fclk + 8 * bc->cfg.bps) / (16 * bc->cfg.bps), 307 bc->cfg.loopback ? ",loopback" : ""); 308 sprintf(portarg, "%ld", bc->pdev->port->base); 309 printk(KERN_DEBUG "%s: %s -s -p %s -m %s\n", bc_drvname, eppconfig_path, portarg, modearg); 310 311 return call_usermodehelper(eppconfig_path, argv, envp, 1); 312} 313 314/* ---------------------------------------------------------------------- */ 315 316static void epp_interrupt(int irq, void *dev_id) 317{ 318} 319 320/* ---------------------------------------------------------------------- */ 321 322static inline void do_kiss_params(struct baycom_state *bc, 323 unsigned char *data, unsigned long len) 324{ 325 326#ifdef KISS_VERBOSE 327#define PKP(a,b) printk(KERN_INFO "baycomm_epp: channel params: " a "\n", b) 328#else /* KISS_VERBOSE */ 329#define PKP(a,b) 330#endif /* KISS_VERBOSE */ 331 332 if (len < 2) 333 return; 334 switch(data[0]) { 335 case PARAM_TXDELAY: 336 bc->ch_params.tx_delay = data[1]; 337 PKP("TX delay = %ums", 10 * bc->ch_params.tx_delay); 338 break; 339 case PARAM_PERSIST: 340 bc->ch_params.ppersist = data[1]; 341 PKP("p persistence = %u", bc->ch_params.ppersist); 342 break; 343 case PARAM_SLOTTIME: 344 bc->ch_params.slottime = data[1]; 345 PKP("slot time = %ums", bc->ch_params.slottime); 346 break; 347 case PARAM_TXTAIL: 348 bc->ch_params.tx_tail = data[1]; 349 PKP("TX tail = %ums", bc->ch_params.tx_tail); 350 break; 351 case PARAM_FULLDUP: 352 bc->ch_params.fulldup = !!data[1]; 353 PKP("%s duplex", bc->ch_params.fulldup ? "full" : "half"); 354 break; 355 default: 356 break; 357 } 358#undef PKP 359} 360 361/* --------------------------------------------------------------------- */ 362 363static void encode_hdlc(struct baycom_state *bc) 364{ 365 struct sk_buff *skb; 366 unsigned char *wp, *bp; 367 int pkt_len; 368 unsigned bitstream, notbitstream, bitbuf, numbit, crc; 369 unsigned char crcarr[2]; 370 int j; 371 372 if (bc->hdlctx.bufcnt > 0) 373 return; 374 skb = bc->skb; 375 if (!skb) 376 return; 377 bc->skb = NULL; 378 pkt_len = skb->len-1; /* strip KISS byte */ 379 wp = bc->hdlctx.buf; 380 bp = skb->data+1; 381 crc = calc_crc_ccitt(bp, pkt_len); 382 crcarr[0] = crc; 383 crcarr[1] = crc >> 8; 384 *wp++ = 0x7e; 385 bitstream = bitbuf = numbit = 0; 386 while (pkt_len > -2) { 387 bitstream >>= 8; 388 bitstream |= ((unsigned int)*bp) << 8; 389 bitbuf |= ((unsigned int)*bp) << numbit; 390 notbitstream = ~bitstream; 391 bp++; 392 pkt_len--; 393 if (!pkt_len) 394 bp = crcarr; 395 for (j = 0; j < 8; j++) 396 if (unlikely(!(notbitstream & (0x1f0 << j)))) { 397 bitstream &= ~(0x100 << j); 398 bitbuf = (bitbuf & (((2 << j) << numbit) - 1)) | 399 ((bitbuf & ~(((2 << j) << numbit) - 1)) << 1); 400 numbit++; 401 notbitstream = ~bitstream; 402 } 403 numbit += 8; 404 while (numbit >= 8) { 405 *wp++ = bitbuf; 406 bitbuf >>= 8; 407 numbit -= 8; 408 } 409 } 410 bitbuf |= 0x7e7e << numbit; 411 numbit += 16; 412 while (numbit >= 8) { 413 *wp++ = bitbuf; 414 bitbuf >>= 8; 415 numbit -= 8; 416 } 417 bc->hdlctx.bufptr = bc->hdlctx.buf; 418 bc->hdlctx.bufcnt = wp - bc->hdlctx.buf; 419 dev_kfree_skb(skb); 420 bc->stats.tx_packets++; 421} 422 423/* ---------------------------------------------------------------------- */ 424 425static int transmit(struct baycom_state *bc, int cnt, unsigned char stat) 426{ 427 struct parport *pp = bc->pdev->port; 428 unsigned char tmp[128]; 429 int i, j; 430 431 if (bc->hdlctx.state == tx_tail && !(stat & EPP_PTTBIT)) 432 bc->hdlctx.state = tx_idle; 433 if (bc->hdlctx.state == tx_idle && bc->hdlctx.calibrate <= 0) { 434 if (bc->hdlctx.bufcnt <= 0) 435 encode_hdlc(bc); 436 if (bc->hdlctx.bufcnt <= 0) 437 return 0; 438 if (!bc->ch_params.fulldup) { 439 if (!(stat & EPP_DCDBIT)) { 440 bc->hdlctx.slotcnt = bc->ch_params.slottime; 441 return 0; 442 } 443 if ((--bc->hdlctx.slotcnt) > 0) 444 return 0; 445 bc->hdlctx.slotcnt = bc->ch_params.slottime; 446 if ((random32() % 256) > bc->ch_params.ppersist) 447 return 0; 448 } 449 } 450 if (bc->hdlctx.state == tx_idle && bc->hdlctx.bufcnt > 0) { 451 bc->hdlctx.state = tx_keyup; 452 bc->hdlctx.flags = tenms_to_flags(bc, bc->ch_params.tx_delay); 453 bc->ptt_keyed++; 454 } 455 while (cnt > 0) { 456 switch (bc->hdlctx.state) { 457 case tx_keyup: 458 i = min_t(int, cnt, bc->hdlctx.flags); 459 cnt -= i; 460 bc->hdlctx.flags -= i; 461 if (bc->hdlctx.flags <= 0) 462 bc->hdlctx.state = tx_data; 463 memset(tmp, 0x7e, sizeof(tmp)); 464 while (i > 0) { 465 j = (i > sizeof(tmp)) ? sizeof(tmp) : i; 466 if (j != pp->ops->epp_write_data(pp, tmp, j, 0)) 467 return -1; 468 i -= j; 469 } 470 break; 471 472 case tx_data: 473 if (bc->hdlctx.bufcnt <= 0) { 474 encode_hdlc(bc); 475 if (bc->hdlctx.bufcnt <= 0) { 476 bc->hdlctx.state = tx_tail; 477 bc->hdlctx.flags = tenms_to_flags(bc, bc->ch_params.tx_tail); 478 break; 479 } 480 } 481 i = min_t(int, cnt, bc->hdlctx.bufcnt); 482 bc->hdlctx.bufcnt -= i; 483 cnt -= i; 484 if (i != pp->ops->epp_write_data(pp, bc->hdlctx.bufptr, i, 0)) 485 return -1; 486 bc->hdlctx.bufptr += i; 487 break; 488 489 case tx_tail: 490 encode_hdlc(bc); 491 if (bc->hdlctx.bufcnt > 0) { 492 bc->hdlctx.state = tx_data; 493 break; 494 } 495 i = min_t(int, cnt, bc->hdlctx.flags); 496 if (i) { 497 cnt -= i; 498 bc->hdlctx.flags -= i; 499 memset(tmp, 0x7e, sizeof(tmp)); 500 while (i > 0) { 501 j = (i > sizeof(tmp)) ? sizeof(tmp) : i; 502 if (j != pp->ops->epp_write_data(pp, tmp, j, 0)) 503 return -1; 504 i -= j; 505 } 506 break; 507 } 508 509 default: /* fall through */ 510 if (bc->hdlctx.calibrate <= 0) 511 return 0; 512 i = min_t(int, cnt, bc->hdlctx.calibrate); 513 cnt -= i; 514 bc->hdlctx.calibrate -= i; 515 memset(tmp, 0, sizeof(tmp)); 516 while (i > 0) { 517 j = (i > sizeof(tmp)) ? sizeof(tmp) : i; 518 if (j != pp->ops->epp_write_data(pp, tmp, j, 0)) 519 return -1; 520 i -= j; 521 } 522 break; 523 } 524 } 525 return 0; 526} 527 528/* ---------------------------------------------------------------------- */ 529 530static void do_rxpacket(struct net_device *dev) 531{ 532 struct baycom_state *bc = netdev_priv(dev); 533 struct sk_buff *skb; 534 unsigned char *cp; 535 unsigned pktlen; 536 537 if (bc->hdlcrx.bufcnt < 4) 538 return; 539 if (!check_crc_ccitt(bc->hdlcrx.buf, bc->hdlcrx.bufcnt)) 540 return; 541 pktlen = bc->hdlcrx.bufcnt-2+1; /* KISS kludge */ 542 if (!(skb = dev_alloc_skb(pktlen))) { 543 printk("%s: memory squeeze, dropping packet\n", dev->name); 544 bc->stats.rx_dropped++; 545 return; 546 } 547 cp = skb_put(skb, pktlen); 548 *cp++ = 0; /* KISS kludge */ 549 memcpy(cp, bc->hdlcrx.buf, pktlen - 1); 550 skb->protocol = ax25_type_trans(skb, dev); 551 netif_rx(skb); 552 dev->last_rx = jiffies; 553 bc->stats.rx_packets++; 554} 555 556static int receive(struct net_device *dev, int cnt) 557{ 558 struct baycom_state *bc = netdev_priv(dev); 559 struct parport *pp = bc->pdev->port; 560 unsigned int bitbuf, notbitstream, bitstream, numbits, state; 561 unsigned char tmp[128]; 562 unsigned char *cp; 563 int cnt2, ret = 0; 564 int j; 565 566 numbits = bc->hdlcrx.numbits; 567 state = bc->hdlcrx.state; 568 bitstream = bc->hdlcrx.bitstream; 569 bitbuf = bc->hdlcrx.bitbuf; 570 while (cnt > 0) { 571 cnt2 = (cnt > sizeof(tmp)) ? sizeof(tmp) : cnt; 572 cnt -= cnt2; 573 if (cnt2 != pp->ops->epp_read_data(pp, tmp, cnt2, 0)) { 574 ret = -1; 575 break; 576 } 577 cp = tmp; 578 for (; cnt2 > 0; cnt2--, cp++) { 579 bitstream >>= 8; 580 bitstream |= (*cp) << 8; 581 bitbuf >>= 8; 582 bitbuf |= (*cp) << 8; 583 numbits += 8; 584 notbitstream = ~bitstream; 585 for (j = 0; j < 8; j++) { 586 587 /* flag or abort */ 588 if (unlikely(!(notbitstream & (0x0fc << j)))) { 589 590 /* abort received */ 591 if (!(notbitstream & (0x1fc << j))) 592 state = 0; 593 594 /* not flag received */ 595 else if (!(bitstream & (0x1fe << j)) != (0x0fc << j)) { 596 if (state) 597 do_rxpacket(dev); 598 bc->hdlcrx.bufcnt = 0; 599 bc->hdlcrx.bufptr = bc->hdlcrx.buf; 600 state = 1; 601 numbits = 7-j; 602 } 603 } 604 605 /* stuffed bit */ 606 else if (unlikely((bitstream & (0x1f8 << j)) == (0xf8 << j))) { 607 numbits--; 608 bitbuf = (bitbuf & ((~0xff) << j)) | ((bitbuf & ~((~0xff) << j)) << 1); 609 } 610 } 611 while (state && numbits >= 8) { 612 if (bc->hdlcrx.bufcnt >= TXBUFFER_SIZE) { 613 state = 0; 614 } else { 615 *(bc->hdlcrx.bufptr)++ = bitbuf >> (16-numbits); 616 bc->hdlcrx.bufcnt++; 617 numbits -= 8; 618 } 619 } 620 } 621 } 622 bc->hdlcrx.numbits = numbits; 623 bc->hdlcrx.state = state; 624 bc->hdlcrx.bitstream = bitstream; 625 bc->hdlcrx.bitbuf = bitbuf; 626 return ret; 627} 628 629/* --------------------------------------------------------------------- */ 630 631#ifdef __i386__ 632#include <asm/msr.h> 633#define GETTICK(x) \ 634({ \ 635 if (cpu_has_tsc) \ 636 rdtscl(x); \ 637}) 638#else /* __i386__ */ 639#define GETTICK(x) 640#endif /* __i386__ */ 641 642static void epp_bh(struct work_struct *work) 643{ 644 struct net_device *dev; 645 struct baycom_state *bc; 646 struct parport *pp; 647 unsigned char stat; 648 unsigned char tmp[2]; 649 unsigned int time1 = 0, time2 = 0, time3 = 0; 650 int cnt, cnt2; 651 652 bc = container_of(work, struct baycom_state, run_work.work); 653 dev = bc->dev; 654 if (!bc->work_running) 655 return; 656 baycom_int_freq(bc); 657 pp = bc->pdev->port; 658 /* update status */ 659 if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1) 660 goto epptimeout; 661 bc->stat = stat; 662 bc->debug_vals.last_pllcorr = stat; 663 GETTICK(time1); 664 if (bc->modem == EPP_FPGAEXTSTATUS) { 665 /* get input count */ 666 tmp[0] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE|1; 667 if (pp->ops->epp_write_addr(pp, tmp, 1, 0) != 1) 668 goto epptimeout; 669 if (pp->ops->epp_read_addr(pp, tmp, 2, 0) != 2) 670 goto epptimeout; 671 cnt = tmp[0] | (tmp[1] << 8); 672 cnt &= 0x7fff; 673 /* get output count */ 674 tmp[0] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE|2; 675 if (pp->ops->epp_write_addr(pp, tmp, 1, 0) != 1) 676 goto epptimeout; 677 if (pp->ops->epp_read_addr(pp, tmp, 2, 0) != 2) 678 goto epptimeout; 679 cnt2 = tmp[0] | (tmp[1] << 8); 680 cnt2 = 16384 - (cnt2 & 0x7fff); 681 /* return to normal */ 682 tmp[0] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE; 683 if (pp->ops->epp_write_addr(pp, tmp, 1, 0) != 1) 684 goto epptimeout; 685 if (transmit(bc, cnt2, stat)) 686 goto epptimeout; 687 GETTICK(time2); 688 if (receive(dev, cnt)) 689 goto epptimeout; 690 if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1) 691 goto epptimeout; 692 bc->stat = stat; 693 } else { 694 /* try to tx */ 695 switch (stat & (EPP_NTAEF|EPP_NTHF)) { 696 case EPP_NTHF: 697 cnt = 2048 - 256; 698 break; 699 700 case EPP_NTAEF: 701 cnt = 2048 - 1793; 702 break; 703 704 case 0: 705 cnt = 0; 706 break; 707 708 default: 709 cnt = 2048 - 1025; 710 break; 711 } 712 if (transmit(bc, cnt, stat)) 713 goto epptimeout; 714 GETTICK(time2); 715 /* do receiver */ 716 while ((stat & (EPP_NRAEF|EPP_NRHF)) != EPP_NRHF) { 717 switch (stat & (EPP_NRAEF|EPP_NRHF)) { 718 case EPP_NRAEF: 719 cnt = 1025; 720 break; 721 722 case 0: 723 cnt = 1793; 724 break; 725 726 default: 727 cnt = 256; 728 break; 729 } 730 if (receive(dev, cnt)) 731 goto epptimeout; 732 if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1) 733 goto epptimeout; 734 } 735 cnt = 0; 736 if (bc->bitrate < 50000) 737 cnt = 256; 738 else if (bc->bitrate < 100000) 739 cnt = 128; 740 while (cnt > 0 && stat & EPP_NREF) { 741 if (receive(dev, 1)) 742 goto epptimeout; 743 cnt--; 744 if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1) 745 goto epptimeout; 746 } 747 } 748 GETTICK(time3); 749#ifdef BAYCOM_DEBUG 750 bc->debug_vals.mod_cycles = time2 - time1; 751 bc->debug_vals.demod_cycles = time3 - time2; 752#endif /* BAYCOM_DEBUG */ 753 schedule_delayed_work(&bc->run_work, 1); 754 if (!bc->skb) 755 netif_wake_queue(dev); 756 return; 757 epptimeout: 758 printk(KERN_ERR "%s: EPP timeout!\n", bc_drvname); 759} 760 761/* ---------------------------------------------------------------------- */ 762/* 763 * ===================== network driver interface ========================= 764 */ 765 766static int baycom_send_packet(struct sk_buff *skb, struct net_device *dev) 767{ 768 struct baycom_state *bc = netdev_priv(dev); 769 770 if (skb->data[0] != 0) { 771 do_kiss_params(bc, skb->data, skb->len); 772 dev_kfree_skb(skb); 773 return 0; 774 } 775 if (bc->skb) 776 return -1; 777 /* strip KISS byte */ 778 if (skb->len >= HDLCDRV_MAXFLEN+1 || skb->len < 3) { 779 dev_kfree_skb(skb); 780 return 0; 781 } 782 netif_stop_queue(dev); 783 bc->skb = skb; 784 return 0; 785} 786 787/* --------------------------------------------------------------------- */ 788 789static int baycom_set_mac_address(struct net_device *dev, void *addr) 790{ 791 struct sockaddr *sa = (struct sockaddr *)addr; 792 793 /* addr is an AX.25 shifted ASCII mac address */ 794 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len); 795 return 0; 796} 797 798/* --------------------------------------------------------------------- */ 799 800static struct net_device_stats *baycom_get_stats(struct net_device *dev) 801{ 802 struct baycom_state *bc = netdev_priv(dev); 803 804 /* 805 * Get the current statistics. This may be called with the 806 * card open or closed. 807 */ 808 return &bc->stats; 809} 810 811/* --------------------------------------------------------------------- */ 812 813static void epp_wakeup(void *handle) 814{ 815 struct net_device *dev = (struct net_device *)handle; 816 struct baycom_state *bc = netdev_priv(dev); 817 818 printk(KERN_DEBUG "baycom_epp: %s: why am I being woken up?\n", dev->name); 819 if (!parport_claim(bc->pdev)) 820 printk(KERN_DEBUG "baycom_epp: %s: I'm broken.\n", dev->name); 821} 822 823/* --------------------------------------------------------------------- */ 824 825/* 826 * Open/initialize the board. This is called (in the current kernel) 827 * sometime after booting when the 'ifconfig' program is run. 828 * 829 * This routine should set everything up anew at each open, even 830 * registers that "should" only need to be set once at boot, so that 831 * there is non-reboot way to recover if something goes wrong. 832 */ 833 834static int epp_open(struct net_device *dev) 835{ 836 struct baycom_state *bc = netdev_priv(dev); 837 struct parport *pp = parport_find_base(dev->base_addr); 838 unsigned int i, j; 839 unsigned char tmp[128]; 840 unsigned char stat; 841 unsigned long tstart; 842 843 if (!pp) { 844 printk(KERN_ERR "%s: parport at 0x%lx unknown\n", bc_drvname, dev->base_addr); 845 return -ENXIO; 846 } 847 if ((~pp->modes) & (PARPORT_MODE_TRISTATE | PARPORT_MODE_PCSPP | PARPORT_MODE_SAFEININT)) { 848 printk(KERN_ERR "%s: parport at 0x%lx cannot be used\n", 849 bc_drvname, pp->base); 850 parport_put_port(pp); 851 return -EIO; 852 } 853 memset(&bc->modem, 0, sizeof(bc->modem)); 854 bc->pdev = parport_register_device(pp, dev->name, NULL, epp_wakeup, 855 epp_interrupt, PARPORT_DEV_EXCL, dev); 856 parport_put_port(pp); 857 if (!bc->pdev) { 858 printk(KERN_ERR "%s: cannot register parport at 0x%lx\n", bc_drvname, pp->base); 859 return -ENXIO; 860 } 861 if (parport_claim(bc->pdev)) { 862 printk(KERN_ERR "%s: parport at 0x%lx busy\n", bc_drvname, pp->base); 863 parport_unregister_device(bc->pdev); 864 return -EBUSY; 865 } 866 dev->irq = /*pp->irq*/ 0; 867 INIT_DELAYED_WORK(&bc->run_work, epp_bh); 868 bc->work_running = 1; 869 bc->modem = EPP_CONVENTIONAL; 870 if (eppconfig(bc)) 871 printk(KERN_INFO "%s: no FPGA detected, assuming conventional EPP modem\n", bc_drvname); 872 else 873 bc->modem = /*EPP_FPGA*/ EPP_FPGAEXTSTATUS; 874 parport_write_control(pp, LPTCTRL_PROGRAM); /* prepare EPP mode; we aren't using interrupts */ 875 /* reset the modem */ 876 tmp[0] = 0; 877 tmp[1] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE; 878 if (pp->ops->epp_write_addr(pp, tmp, 2, 0) != 2) 879 goto epptimeout; 880 /* autoprobe baud rate */ 881 tstart = jiffies; 882 i = 0; 883 while (time_before(jiffies, tstart + HZ/3)) { 884 if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1) 885 goto epptimeout; 886 if ((stat & (EPP_NRAEF|EPP_NRHF)) == EPP_NRHF) { 887 schedule(); 888 continue; 889 } 890 if (pp->ops->epp_read_data(pp, tmp, 128, 0) != 128) 891 goto epptimeout; 892 if (pp->ops->epp_read_data(pp, tmp, 128, 0) != 128) 893 goto epptimeout; 894 i += 256; 895 } 896 for (j = 0; j < 256; j++) { 897 if (pp->ops->epp_read_addr(pp, &stat, 1, 0) != 1) 898 goto epptimeout; 899 if (!(stat & EPP_NREF)) 900 break; 901 if (pp->ops->epp_read_data(pp, tmp, 1, 0) != 1) 902 goto epptimeout; 903 i++; 904 } 905 tstart = jiffies - tstart; 906 bc->bitrate = i * (8 * HZ) / tstart; 907 j = 1; 908 i = bc->bitrate >> 3; 909 while (j < 7 && i > 150) { 910 j++; 911 i >>= 1; 912 } 913 printk(KERN_INFO "%s: autoprobed bitrate: %d int divider: %d int rate: %d\n", 914 bc_drvname, bc->bitrate, j, bc->bitrate >> (j+2)); 915 tmp[0] = EPP_TX_FIFO_ENABLE|EPP_RX_FIFO_ENABLE|EPP_MODEM_ENABLE/*|j*/; 916 if (pp->ops->epp_write_addr(pp, tmp, 1, 0) != 1) 917 goto epptimeout; 918 /* 919 * initialise hdlc variables 920 */ 921 bc->hdlcrx.state = 0; 922 bc->hdlcrx.numbits = 0; 923 bc->hdlctx.state = tx_idle; 924 bc->hdlctx.bufcnt = 0; 925 bc->hdlctx.slotcnt = bc->ch_params.slottime; 926 bc->hdlctx.calibrate = 0; 927 /* start the bottom half stuff */ 928 schedule_delayed_work(&bc->run_work, 1); 929 netif_start_queue(dev); 930 return 0; 931 932 epptimeout: 933 printk(KERN_ERR "%s: epp timeout during bitrate probe\n", bc_drvname); 934 parport_write_control(pp, 0); /* reset the adapter */ 935 parport_release(bc->pdev); 936 parport_unregister_device(bc->pdev); 937 return -EIO; 938} 939 940/* --------------------------------------------------------------------- */ 941 942static int epp_close(struct net_device *dev) 943{ 944 struct baycom_state *bc = netdev_priv(dev); 945 struct parport *pp = bc->pdev->port; 946 unsigned char tmp[1]; 947 948 bc->work_running = 0; 949 flush_scheduled_work(); 950 bc->stat = EPP_DCDBIT; 951 tmp[0] = 0; 952 pp->ops->epp_write_addr(pp, tmp, 1, 0); 953 parport_write_control(pp, 0); /* reset the adapter */ 954 parport_release(bc->pdev); 955 parport_unregister_device(bc->pdev); 956 if (bc->skb) 957 dev_kfree_skb(bc->skb); 958 bc->skb = NULL; 959 printk(KERN_INFO "%s: close epp at iobase 0x%lx irq %u\n", 960 bc_drvname, dev->base_addr, dev->irq); 961 return 0; 962} 963 964/* --------------------------------------------------------------------- */ 965 966static int baycom_setmode(struct baycom_state *bc, const char *modestr) 967{ 968 const char *cp; 969 970 if (strstr(modestr,"intclk")) 971 bc->cfg.intclk = 1; 972 if (strstr(modestr,"extclk")) 973 bc->cfg.intclk = 0; 974 if (strstr(modestr,"intmodem")) 975 bc->cfg.extmodem = 0; 976 if (strstr(modestr,"extmodem")) 977 bc->cfg.extmodem = 1; 978 if (strstr(modestr,"noloopback")) 979 bc->cfg.loopback = 0; 980 if (strstr(modestr,"loopback")) 981 bc->cfg.loopback = 1; 982 if ((cp = strstr(modestr,"fclk="))) { 983 bc->cfg.fclk = simple_strtoul(cp+5, NULL, 0); 984 if (bc->cfg.fclk < 1000000) 985 bc->cfg.fclk = 1000000; 986 if (bc->cfg.fclk > 25000000) 987 bc->cfg.fclk = 25000000; 988 } 989 if ((cp = strstr(modestr,"bps="))) { 990 bc->cfg.bps = simple_strtoul(cp+4, NULL, 0); 991 if (bc->cfg.bps < 1000) 992 bc->cfg.bps = 1000; 993 if (bc->cfg.bps > 1500000) 994 bc->cfg.bps = 1500000; 995 } 996 return 0; 997} 998 999/* --------------------------------------------------------------------- */ 1000 1001static int baycom_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 1002{ 1003 struct baycom_state *bc = netdev_priv(dev); 1004 struct hdlcdrv_ioctl hi; 1005 1006 if (cmd != SIOCDEVPRIVATE) 1007 return -ENOIOCTLCMD; 1008 1009 if (copy_from_user(&hi, ifr->ifr_data, sizeof(hi))) 1010 return -EFAULT; 1011 switch (hi.cmd) { 1012 default: 1013 return -ENOIOCTLCMD; 1014 1015 case HDLCDRVCTL_GETCHANNELPAR: 1016 hi.data.cp.tx_delay = bc->ch_params.tx_delay; 1017 hi.data.cp.tx_tail = bc->ch_params.tx_tail; 1018 hi.data.cp.slottime = bc->ch_params.slottime; 1019 hi.data.cp.ppersist = bc->ch_params.ppersist; 1020 hi.data.cp.fulldup = bc->ch_params.fulldup; 1021 break; 1022 1023 case HDLCDRVCTL_SETCHANNELPAR: 1024 if (!capable(CAP_NET_ADMIN)) 1025 return -EACCES; 1026 bc->ch_params.tx_delay = hi.data.cp.tx_delay; 1027 bc->ch_params.tx_tail = hi.data.cp.tx_tail; 1028 bc->ch_params.slottime = hi.data.cp.slottime; 1029 bc->ch_params.ppersist = hi.data.cp.ppersist; 1030 bc->ch_params.fulldup = hi.data.cp.fulldup; 1031 bc->hdlctx.slotcnt = 1; 1032 return 0; 1033 1034 case HDLCDRVCTL_GETMODEMPAR: 1035 hi.data.mp.iobase = dev->base_addr; 1036 hi.data.mp.irq = dev->irq; 1037 hi.data.mp.dma = dev->dma; 1038 hi.data.mp.dma2 = 0; 1039 hi.data.mp.seriobase = 0; 1040 hi.data.mp.pariobase = 0; 1041 hi.data.mp.midiiobase = 0; 1042 break; 1043 1044 case HDLCDRVCTL_SETMODEMPAR: 1045 if ((!capable(CAP_SYS_RAWIO)) || netif_running(dev)) 1046 return -EACCES; 1047 dev->base_addr = hi.data.mp.iobase; 1048 dev->irq = /*hi.data.mp.irq*/0; 1049 dev->dma = /*hi.data.mp.dma*/0; 1050 return 0; 1051 1052 case HDLCDRVCTL_GETSTAT: 1053 hi.data.cs.ptt = !!(bc->stat & EPP_PTTBIT); 1054 hi.data.cs.dcd = !(bc->stat & EPP_DCDBIT); 1055 hi.data.cs.ptt_keyed = bc->ptt_keyed; 1056 hi.data.cs.tx_packets = bc->stats.tx_packets; 1057 hi.data.cs.tx_errors = bc->stats.tx_errors; 1058 hi.data.cs.rx_packets = bc->stats.rx_packets; 1059 hi.data.cs.rx_errors = bc->stats.rx_errors; 1060 break; 1061 1062 case HDLCDRVCTL_OLDGETSTAT: 1063 hi.data.ocs.ptt = !!(bc->stat & EPP_PTTBIT); 1064 hi.data.ocs.dcd = !(bc->stat & EPP_DCDBIT); 1065 hi.data.ocs.ptt_keyed = bc->ptt_keyed; 1066 break; 1067 1068 case HDLCDRVCTL_CALIBRATE: 1069 if (!capable(CAP_SYS_RAWIO)) 1070 return -EACCES; 1071 bc->hdlctx.calibrate = hi.data.calibrate * bc->bitrate / 8; 1072 return 0; 1073 1074 case HDLCDRVCTL_DRIVERNAME: 1075 strncpy(hi.data.drivername, "baycom_epp", sizeof(hi.data.drivername)); 1076 break; 1077 1078 case HDLCDRVCTL_GETMODE: 1079 sprintf(hi.data.modename, "%sclk,%smodem,fclk=%d,bps=%d%s", 1080 bc->cfg.intclk ? "int" : "ext", 1081 bc->cfg.extmodem ? "ext" : "int", bc->cfg.fclk, bc->cfg.bps, 1082 bc->cfg.loopback ? ",loopback" : ""); 1083 break; 1084 1085 case HDLCDRVCTL_SETMODE: 1086 if (!capable(CAP_NET_ADMIN) || netif_running(dev)) 1087 return -EACCES; 1088 hi.data.modename[sizeof(hi.data.modename)-1] = '\0'; 1089 return baycom_setmode(bc, hi.data.modename); 1090 1091 case HDLCDRVCTL_MODELIST: 1092 strncpy(hi.data.modename, "intclk,extclk,intmodem,extmodem,divider=x", 1093 sizeof(hi.data.modename)); 1094 break; 1095 1096 case HDLCDRVCTL_MODEMPARMASK: 1097 return HDLCDRV_PARMASK_IOBASE; 1098 1099 } 1100 if (copy_to_user(ifr->ifr_data, &hi, sizeof(hi))) 1101 return -EFAULT; 1102 return 0; 1103} 1104 1105/* --------------------------------------------------------------------- */ 1106 1107/* 1108 * Check for a network adaptor of this type, and return '0' if one exists. 1109 * If dev->base_addr == 0, probe all likely locations. 1110 * If dev->base_addr == 1, always return failure. 1111 * If dev->base_addr == 2, allocate space for the device and return success 1112 * (detachable devices only). 1113 */ 1114static void baycom_probe(struct net_device *dev) 1115{ 1116 const struct hdlcdrv_channel_params dflt_ch_params = { 1117 20, 2, 10, 40, 0 1118 }; 1119 struct baycom_state *bc; 1120 1121 /* 1122 * not a real probe! only initialize data structures 1123 */ 1124 bc = netdev_priv(dev); 1125 /* 1126 * initialize the baycom_state struct 1127 */ 1128 bc->ch_params = dflt_ch_params; 1129 bc->ptt_keyed = 0; 1130 1131 /* 1132 * initialize the device struct 1133 */ 1134 dev->open = epp_open; 1135 dev->stop = epp_close; 1136 dev->do_ioctl = baycom_ioctl; 1137 dev->hard_start_xmit = baycom_send_packet; 1138 dev->get_stats = baycom_get_stats; 1139 1140 /* Fill in the fields of the device structure */ 1141 bc->skb = NULL; 1142 1143 dev->hard_header = ax25_hard_header; 1144 dev->rebuild_header = ax25_rebuild_header; 1145 dev->set_mac_address = baycom_set_mac_address; 1146 1147 dev->type = ARPHRD_AX25; /* AF_AX25 device */ 1148 dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN; 1149 dev->mtu = AX25_DEF_PACLEN; /* eth_mtu is the default */ 1150 dev->addr_len = AX25_ADDR_LEN; /* sizeof an ax.25 address */ 1151 memcpy(dev->broadcast, &ax25_bcast, AX25_ADDR_LEN); 1152 memcpy(dev->dev_addr, &null_ax25_address, AX25_ADDR_LEN); 1153 dev->tx_queue_len = 16; 1154 1155 /* New style flags */ 1156 dev->flags = 0; 1157} 1158 1159/* --------------------------------------------------------------------- */ 1160 1161/* 1162 * command line settable parameters 1163 */ 1164static const char *mode[NR_PORTS] = { "", }; 1165static int iobase[NR_PORTS] = { 0x378, }; 1166 1167module_param_array(mode, charp, NULL, 0); 1168MODULE_PARM_DESC(mode, "baycom operating mode"); 1169module_param_array(iobase, int, NULL, 0); 1170MODULE_PARM_DESC(iobase, "baycom io base address"); 1171 1172MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu"); 1173MODULE_DESCRIPTION("Baycom epp amateur radio modem driver"); 1174MODULE_LICENSE("GPL"); 1175 1176/* --------------------------------------------------------------------- */ 1177 1178static void __init baycom_epp_dev_setup(struct net_device *dev) 1179{ 1180 struct baycom_state *bc = netdev_priv(dev); 1181 1182 /* 1183 * initialize part of the baycom_state struct 1184 */ 1185 bc->dev = dev; 1186 bc->magic = BAYCOM_MAGIC; 1187 bc->cfg.fclk = 19666600; 1188 bc->cfg.bps = 9600; 1189 /* 1190 * initialize part of the device struct 1191 */ 1192 baycom_probe(dev); 1193} 1194 1195static int __init init_baycomepp(void) 1196{ 1197 int i, found = 0; 1198 char set_hw = 1; 1199 1200 printk(bc_drvinfo); 1201 /* 1202 * register net devices 1203 */ 1204 for (i = 0; i < NR_PORTS; i++) { 1205 struct net_device *dev; 1206 1207 dev = alloc_netdev(sizeof(struct baycom_state), "bce%d", 1208 baycom_epp_dev_setup); 1209 1210 if (!dev) { 1211 printk(KERN_WARNING "bce%d : out of memory\n", i); 1212 return found ? 0 : -ENOMEM; 1213 } 1214 1215 sprintf(dev->name, "bce%d", i); 1216 dev->base_addr = iobase[i]; 1217 1218 if (!mode[i]) 1219 set_hw = 0; 1220 if (!set_hw) 1221 iobase[i] = 0; 1222 1223 if (register_netdev(dev)) { 1224 printk(KERN_WARNING "%s: cannot register net device %s\n", bc_drvname, dev->name); 1225 free_netdev(dev); 1226 break; 1227 } 1228 if (set_hw && baycom_setmode(netdev_priv(dev), mode[i])) 1229 set_hw = 0; 1230 baycom_device[i] = dev; 1231 found++; 1232 } 1233 1234 return found ? 0 : -ENXIO; 1235} 1236 1237static void __exit cleanup_baycomepp(void) 1238{ 1239 int i; 1240 1241 for(i = 0; i < NR_PORTS; i++) { 1242 struct net_device *dev = baycom_device[i]; 1243 1244 if (dev) { 1245 struct baycom_state *bc = netdev_priv(dev); 1246 if (bc->magic == BAYCOM_MAGIC) { 1247 unregister_netdev(dev); 1248 free_netdev(dev); 1249 } else 1250 printk(paranoia_str, "cleanup_module"); 1251 } 1252 } 1253} 1254 1255module_init(init_baycomepp); 1256module_exit(cleanup_baycomepp); 1257 1258/* --------------------------------------------------------------------- */ 1259 1260#ifndef MODULE 1261 1262/* 1263 * format: baycom_epp=io,mode 1264 * mode: fpga config options 1265 */ 1266 1267static int __init baycom_epp_setup(char *str) 1268{ 1269 static unsigned __initdata nr_dev = 0; 1270 int ints[2]; 1271 1272 if (nr_dev >= NR_PORTS) 1273 return 0; 1274 str = get_options(str, 2, ints); 1275 if (ints[0] < 1) 1276 return 0; 1277 mode[nr_dev] = str; 1278 iobase[nr_dev] = ints[1]; 1279 nr_dev++; 1280 return 1; 1281} 1282 1283__setup("baycom_epp=", baycom_epp_setup); 1284 1285#endif /* MODULE */ 1286/* --------------------------------------------------------------------- */ 1287