root/drivers/net/wic.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. wic_init
  2. wic_kick_bh
  3. wic_set_multicast_list
  4. wic_bh
  5. wic_bh_timeout_error
  6. wic_none
  7. wic_receive
  8. wic_receive_packet
  9. wic_send
  10. wic_send_packet
  11. wic_connection_close
  12. wic_error
  13. wic_interrupt
  14. wic_rebuild_header
  15. wic_tx_packet
  16. wic_open
  17. wic_close
  18. wic_get_stats
  19. wic_config
  20. wic_ioctl
  21. get_byte
  22. ack_resp
  23. wic_reset
  24. check_bfr
  25. recv_cmd_resp
  26. send_byte
  27. send_cmd
  28. init_module
  29. cleanup_module

   1 /* $Id: wic.c,v 1.0 1995/02/11 10:26:05 hayes Exp $ */
   2 /* WIC: A parallel port "network" driver for Linux. */
   3 /* based on the plip network driver */
   4 /* Modified for Linux 1.3.x by Alan Cox <Alan.Cox@linux.org> */
   5 
   6 char *version = "NET3 WIC version 0.9 hayes@netplumbing.com";
   7 
   8 /*
   9   Sources:
  10         Ideas and protocols came from Russ Nelson's <nelson@crynwr.com>
  11         "parallel.asm" parallel port packet driver and from the plip.c
  12         parallel networking linux driver from the 1.2.13 Linux
  13         distribution.
  14 
  15   The packet is encapsulated as if it were ethernet.
  16 
  17 */
  18 
  19 #ifdef MODULE
  20 #include <linux/module.h>
  21 #include <linux/version.h>
  22 #else
  23 #define MOD_INC_USE_COUNT
  24 #define MOD_DEC_USE_COUNT
  25 #endif
  26 
  27 #include <linux/kernel.h>
  28 #include <linux/sched.h>
  29 #include <linux/types.h>
  30 #include <linux/fcntl.h>
  31 #include <linux/interrupt.h>
  32 #include <linux/string.h>
  33 #include <linux/ptrace.h>
  34 #include <linux/if_ether.h>
  35 #include <asm/system.h>
  36 #include <asm/io.h>
  37 #include <linux/in.h>
  38 #include <linux/errno.h>
  39 #include <linux/delay.h>
  40 #include <linux/lp.h>
  41 
  42 #include <linux/netdevice.h>
  43 #include <linux/etherdevice.h>
  44 #include <linux/skbuff.h>
  45 #include <linux/if_wic.h>
  46 
  47 #include <linux/tqueue.h>
  48 #include <linux/ioport.h>
  49 #include <asm/bitops.h>
  50 #include <asm/irq.h>
  51 #include <asm/byteorder.h>
  52 #include <string.h>
  53 
  54 #define NET_DEBUG 1
  55 /* Use 0 for production, 1 for verification, >2 for debug */
  56 #ifndef NET_DEBUG
  57 #define NET_DEBUG 1
  58 #endif
  59 unsigned int net_debug = NET_DEBUG;
  60 
  61 /* Connection time out = WIC_TRIGGER_WAIT * WIC_DELAY_UNIT usec */
  62 #define WIC_TRIGGER_WAIT         500
  63 
  64 /* Nibble time out = WIC_NIBBLE_WAIT * WIC_DELAY_UNIT usec */
  65 #define WIC_NIBBLE_WAIT        3000
  66 
  67 #define PAR_DATA(dev)           ((dev)->base_addr+0)
  68 #define PAR_STATUS(dev)         ((dev)->base_addr+1)
  69 #define PAR_CONTROL(dev)        ((dev)->base_addr+2)
  70 
  71 /* Bottom halfs */
  72 void wic_kick_bh(struct device *dev);
  73 void wic_bh(struct device *dev);
  74 
  75 /* Interrupt handler */
  76 void wic_interrupt(int irq, void *dev_ptr, struct pt_regs *regs);
  77 
  78 /* Functions for DEV methods */
  79 int wic_rebuild_header(void *buff, struct device *dev,
  80                                unsigned long raddr, struct sk_buff *skb);
  81 int wic_tx_packet(struct sk_buff *skb, struct device *dev);
  82 int wic_open(struct device *dev);
  83 int wic_close(struct device *dev);
  84 struct enet_statistics *wic_get_stats(struct device *dev);
  85 int wic_config(struct device *dev, struct ifmap *map);
  86 int wic_ioctl(struct device *dev, struct ifreq *ifr, int cmd);
  87 int send_cmd(struct device *dev, unsigned char *cmd, char len);
  88 int recv_cmd_resp(struct device *dev, unsigned char *cmd);
  89 int send_byte(struct device *dev, unsigned char c);
  90 int get_byte(struct device *dev, unsigned char *c);
  91 int ack_resp(struct device *dev);
  92 int check_bfr(struct device *dev);
  93 void wic_reset(struct device *dev);
  94 void wic_set_multicast_list(struct device *dev);
  95 
  96 #define LOOPCNT 30000   
  97 unsigned char tog = 3;
  98 unsigned char save = 0;
  99 
 100 enum wic_connection_state {
 101         WIC_CN_NONE=0,
 102         WIC_CN_RECEIVE,
 103         WIC_CN_SEND,
 104         WIC_CN_CLOSING,
 105         WIC_CN_ERROR
 106 };
 107 
 108 enum wic_packet_state {
 109         WIC_PK_DONE=0,
 110         WIC_PK_TRIGGER,
 111         WIC_PK_LENGTH_LSB,
 112         WIC_PK_LENGTH_MSB,
 113         WIC_PK_DATA,
 114         WIC_PK_CHECKSUM
 115 };
 116 
 117 enum wic_nibble_state {
 118         WIC_NB_BEGIN,
 119         WIC_NB_1,
 120         WIC_NB_2,
 121 };
 122 
 123 struct wic_local {
 124         enum wic_packet_state state;
 125         enum wic_nibble_state nibble;
 126         union {
 127                 struct {
 128 #if defined(__LITTLE_ENDIAN)
 129                         unsigned char lsb;
 130                         unsigned char msb;
 131 #elif defined(__BIG_ENDIAN)
 132                         unsigned char msb;
 133                         unsigned char lsb;
 134 #else
 135 #error  "Please fix the endianness defines in <asm/byteorder.h>"
 136 #endif                                          
 137                 } b;
 138                 unsigned short h;
 139         } length;
 140         unsigned short byte;
 141         unsigned char  checksum;
 142         unsigned char  data;
 143         struct sk_buff *skb;
 144 };
 145 
 146 struct net_local {
 147         struct enet_statistics enet_stats;
 148         struct tq_struct immediate;
 149         struct tq_struct deferred;
 150         struct wic_local snd_data;
 151         struct wic_local rcv_data;
 152         unsigned long  trigger;
 153         unsigned long  nibble;
 154         enum wic_connection_state connection;
 155         unsigned short timeout_count;
 156         char is_deferred;
 157         int (*orig_rebuild_header)(void *eth, struct device *dev,
 158                                    unsigned long raddr, struct sk_buff *skb);
 159 };
 160 
 161 /* Entry point of WIC driver.
 162    Probe the hardware, and register/initialize the driver. */
 163 int
 164 wic_init(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 165 {
 166         struct net_local *nl;
 167         struct wicconf wc;
 168         int i;
 169 
 170         /* Check region before the probe */
 171         if (check_region(PAR_DATA(dev), 3) < 0)
 172                 return -ENODEV;
 173         
 174         /* Check that there is something at base_addr. */
 175         outb(0, PAR_DATA(dev));
 176         udelay(1000);
 177         if (inb(PAR_DATA(dev)) != 0)
 178                 return -ENODEV;
 179 
 180         printk("%s\n",version);
 181         printk("%s: Parallel port at %#3lx, ", dev->name, dev->base_addr);
 182         if (dev->irq) {
 183                 printk("using assigned IRQ %d.\n", dev->irq);
 184         } else {
 185                 int irq = 0;
 186 #ifdef MODULE
 187                 /* dev->irq==0 means autoprobe, but we don't try to do so
 188                    with module.  We can change it by ifconfig */
 189 #else
 190                 unsigned int irqs = probe_irq_on();
 191 
 192                 outb(0x00, PAR_CONTROL(dev));
 193                 udelay(1000);
 194                 udelay(1000);
 195                 irq = probe_irq_off(irqs);
 196 #endif
 197                 if (irq > 0) {
 198                         dev->irq = irq;
 199                         printk("using probed IRQ %d.\n", dev->irq);
 200                 } else
 201                         printk("failed to detect IRQ(%d) --"
 202                                " Please set IRQ by ifconfig.\n", irq);
 203         }
 204 
 205         request_region(PAR_DATA(dev), 3, dev->name);
 206 
 207         /* Fill in the generic fields of the device structure. */
 208         ether_setup(dev);
 209 
 210         /* Then, override parts of it */
 211         dev->hard_start_xmit    = wic_tx_packet;
 212         dev->open               = wic_open;
 213         dev->stop               = wic_close;
 214         dev->get_stats          = wic_get_stats;
 215         dev->set_config         = wic_config;
 216         dev->do_ioctl           = wic_ioctl;
 217         dev->mtu                = 1514;
 218         dev->set_multicast_list = wic_set_multicast_list;
 219         dev->flags              = IFF_BROADCAST | IFF_RUNNING | IFF_NOTRAILERS;
 220 
 221         /* get the MAC address from the controller */
 222         wc.len = 1;
 223         wc.pcmd = WIC_GETNET;
 224         check_bfr(dev);
 225         send_cmd(dev, (unsigned char *)&wc, 1);
 226         wc.len = recv_cmd_resp(dev, (unsigned char *)&wc.data);
 227         while ((wc.len == 1) && (wc.data[0] == 0x7)) /* controller int */
 228                 wc.len = recv_cmd_resp(dev, (unsigned char *)&wc.data);
 229         
 230         printk("%s:MAC address: ",dev->name);   
 231         for (i=0; i < ETH_ALEN ; i++) {
 232                 dev->dev_addr[i] = wc.data[i];
 233                 printk("%2x ",dev->dev_addr[i]);
 234         }
 235         printk("\n");
 236 
 237         /* Set the private structure */
 238         dev->priv = kmalloc(sizeof (struct net_local), GFP_KERNEL);
 239         if (dev->priv == NULL)
 240                 return EAGAIN;
 241         memset(dev->priv, 0, sizeof(struct net_local));
 242         nl = (struct net_local *) dev->priv;
 243 
 244         nl->orig_rebuild_header = dev->rebuild_header;
 245         dev->rebuild_header     = wic_rebuild_header;
 246 
 247         /* Initialize constants */
 248         nl->trigger     = WIC_TRIGGER_WAIT;
 249         nl->nibble      = WIC_NIBBLE_WAIT;
 250 
 251         /* Initialize task queue structures */
 252         nl->immediate.next = NULL;
 253         nl->immediate.sync = 0;
 254         nl->immediate.routine = (void *)(void *)wic_bh;
 255         nl->immediate.data = dev;
 256 
 257         nl->deferred.next = NULL;
 258         nl->deferred.sync = 0;
 259         nl->deferred.routine = (void *)(void *)wic_kick_bh;
 260         nl->deferred.data = dev;
 261 
 262         return 0;
 263 }
 264 
 265 /* Bottom half handler for the delayed request.
 266    This routine is kicked by do_timer().
 267    Request `wic_bh' to be invoked. */
 268 void
 269 wic_kick_bh(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 270 {
 271         struct net_local *nl = (struct net_local *)dev->priv;
 272 
 273         if (nl->is_deferred) {
 274                 queue_task(&nl->immediate, &tq_immediate);
 275                 mark_bh(IMMEDIATE_BH);
 276         }
 277 }
 278 
 279 /* Forward declarations of internal routines */
 280 int wic_none(struct device *, struct net_local *,
 281                      struct wic_local *, struct wic_local *);
 282 int wic_receive_packet(struct device *, struct net_local *,
 283                                struct wic_local *, struct wic_local *);
 284 int wic_send_packet(struct device *, struct net_local *,
 285                             struct wic_local *, struct wic_local *);
 286 int wic_connection_close(struct device *, struct net_local *,
 287                                  struct wic_local *, struct wic_local *);
 288 int wic_error(struct device *, struct net_local *,
 289                       struct wic_local *, struct wic_local *);
 290 int wic_bh_timeout_error(struct device *dev, struct net_local *nl,
 291                                  struct wic_local *snd,
 292                                  struct wic_local *rcv,
 293                                  int error);
 294 
 295 #define OK        0
 296 #define TIMEOUT   1
 297 #define ERROR     2
 298 
 299 typedef int (*wic_func)(struct device *dev, struct net_local *nl,
 300                          struct wic_local *snd, struct wic_local *rcv);
 301 
 302 wic_func connection_state_table[] =
 303 {
 304         wic_none,
 305         wic_receive_packet,
 306         wic_send_packet,
 307         wic_connection_close,
 308         wic_error
 309 };
 310 
 311 void 
 312 wic_set_multicast_list(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 313 {
 314         struct wicconf wc;
 315         struct wic_net *wn;
 316         
 317         disable_irq(dev->irq);
 318         save &= 0xef; /* disable */
 319         outb(save, PAR_CONTROL(dev));
 320         
 321         wc.len = 1;
 322         wc.pcmd = WIC_GETNET;
 323         check_bfr(dev);
 324         tog = 3;
 325         send_cmd(dev, (unsigned char *)&wc, 1);
 326         wc.len = recv_cmd_resp(dev, (unsigned char *)&wc.data);
 327         while ((wc.len == 1) && (wc.data[0] == 0x7)) /* controller int */
 328                 wc.len = recv_cmd_resp(dev, (unsigned char *)&wc.data);
 329         wn = (struct wic_net *)&wc.data;
 330         if(dev->flags&IFF_PROMISC)
 331         {
 332                 /* promiscuous mode */
 333                 wn->mode |= (NET_MODE_ME | NET_MODE_BCAST | 
 334                         NET_MODE_MCAST | NET_MODE_PROM);
 335                 printk("%s: Setting promiscuous mode\n", dev->name);
 336         }
 337         else if((dev->flags&IFF_ALLMULTI) || dev->mc_count)
 338         {
 339                 wn->mode &= ~NET_MODE_PROM;
 340                 wn->mode |= (NET_MODE_MCAST | NET_MODE_ME | NET_MODE_BCAST);
 341         }
 342         else
 343         {
 344                 wn->mode &= ~(NET_MODE_PROM | NET_MODE_MCAST);
 345                 wn->mode |= (NET_MODE_ME | NET_MODE_BCAST);
 346         }
 347         wc.len = 23;
 348         wc.pcmd = WIC_SETNET;
 349         check_bfr(dev);
 350         tog = 3;
 351         send_cmd(dev, (unsigned char *)&wc, wc.len);
 352 
 353         save |= 0x10; /* enable */
 354         outb(save, PAR_CONTROL(dev));
 355         enable_irq(dev->irq);
 356         return;
 357 }
 358 
 359 /* Bottom half handler of WIC. */
 360 void
 361 wic_bh(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 362 {
 363         struct net_local *nl = (struct net_local *)dev->priv;
 364         struct wic_local *snd = &nl->snd_data;
 365         struct wic_local *rcv = &nl->rcv_data;
 366         wic_func f;
 367         int r;
 368 
 369         nl->is_deferred = 0;
 370         f = connection_state_table[nl->connection];
 371         if ((r = (*f)(dev, nl, snd, rcv)) != OK
 372             && (r = wic_bh_timeout_error(dev, nl, snd, rcv, r)) != OK) {
 373                 nl->is_deferred = 1;
 374                 queue_task(&nl->deferred, &tq_timer);
 375         }
 376 }
 377 
 378 int
 379 wic_bh_timeout_error(struct device *dev, struct net_local *nl,
     /* [previous][next][first][last][top][bottom][index][help] */
 380                       struct wic_local *snd, struct wic_local *rcv,
 381                       int error)
 382 {
 383         unsigned char c0;
 384         unsigned long flags;
 385 
 386         save_flags(flags);
 387         cli();
 388         if (nl->connection == WIC_CN_SEND) {
 389 
 390                 if (error != ERROR) { /* Timeout */
 391                         nl->timeout_count++;
 392                         if ((snd->state == WIC_PK_TRIGGER
 393                              && nl->timeout_count <= 10)
 394                             || nl->timeout_count <= 3) {
 395                                 restore_flags(flags);
 396                                 /* Try again later */
 397                                 return TIMEOUT;
 398                         }
 399                         c0 = inb(PAR_STATUS(dev));
 400                         printk("%s: transmit timeout(%d,%02x)\n",
 401                                dev->name, snd->state, c0);
 402                 }
 403                 nl->enet_stats.tx_errors++;
 404                 nl->enet_stats.tx_aborted_errors++;
 405         } else if (nl->connection == WIC_CN_RECEIVE) {
 406                 if (rcv->state == WIC_PK_TRIGGER) {
 407                         /* Transmission was interrupted. */
 408                         restore_flags(flags);
 409                         return OK;
 410                 }
 411                 if (error != ERROR) { /* Timeout */
 412                         if (++nl->timeout_count <= 3) {
 413                                 restore_flags(flags);
 414                                 /* Try again later */
 415                                 return TIMEOUT;
 416                         }
 417                         c0 = inb(PAR_STATUS(dev));
 418                         printk("%s: receive timeout(%d,%02x)\n",
 419                                dev->name, rcv->state, c0);
 420                 }
 421                 nl->enet_stats.rx_dropped++;
 422         }
 423         rcv->state = WIC_PK_DONE;
 424         if (rcv->skb) {
 425                 rcv->skb->free = 1;
 426                 kfree_skb(rcv->skb, FREE_READ);
 427                 rcv->skb = NULL;
 428         }
 429         snd->state = WIC_PK_DONE;
 430         if (snd->skb) {
 431                 snd->skb->free = 1;
 432                 dev_kfree_skb(snd->skb, FREE_WRITE);
 433                 snd->skb = NULL;
 434         }
 435 #if (0)
 436         disable_irq(dev->irq);
 437         save &= 0xef; /* disable */
 438         outb(save, PAR_CONTROL(dev));
 439         dev->tbusy = 1;
 440         outb(0x00, PAR_DATA(dev));
 441 #endif /* (0) */
 442         nl->connection = WIC_CN_ERROR;
 443         restore_flags(flags);
 444 
 445         return TIMEOUT;
 446 }
 447 
 448 int
 449 wic_none(struct device *dev, struct net_local *nl,
     /* [previous][next][first][last][top][bottom][index][help] */
 450           struct wic_local *snd, struct wic_local *rcv)
 451 {
 452         return OK;
 453 }
 454 
 455 /* WIC_RECEIVE --- receive a byte(two nibbles)
 456    Returns OK on success, TIMEOUT on timeout */
 457 inline int
 458 wic_receive(unsigned short nibble_timeout, unsigned short status_addr,
     /* [previous][next][first][last][top][bottom][index][help] */
 459              enum wic_nibble_state *ns_p, unsigned char *data_p)
 460 {
 461 unsigned int cx;
 462 
 463         cx = LOOPCNT;
 464         while ((inb(status_addr) & 0x08) != ((tog<<3) & 0x08)) {
 465                 if (--cx == 0) {
 466                         return TIMEOUT;
 467                 }
 468         }
 469         *data_p = inb(status_addr-1);
 470         tog ^= 0x01;
 471         outb(tog| save, status_addr+1);
 472         return OK;
 473 }
 474 
 475 /* WIC_RECEIVE_PACKET --- receive a packet */
 476 int
 477 wic_receive_packet(struct device *dev, struct net_local *nl,
     /* [previous][next][first][last][top][bottom][index][help] */
 478                     struct wic_local *snd, struct wic_local *rcv)
 479 {
 480         unsigned short status_addr = PAR_STATUS(dev);
 481         unsigned short nibble_timeout = nl->nibble;
 482         unsigned char *lbuf;
 483         unsigned char junk;
 484         unsigned long flags;
 485 
 486         save_flags(flags);
 487         cli();
 488         switch (rcv->state) {
 489         case WIC_PK_TRIGGER:
 490                 disable_irq(dev->irq);
 491                 save &= 0xef; /* disable */
 492                 outb(save, PAR_CONTROL(dev));
 493                 
 494                 dev->interrupt = 0;
 495 
 496                 tog &= 0xfe;
 497                 ack_resp(dev);
 498                 if (net_debug > 2)
 499                         printk("%s: receive start\n", dev->name);
 500                 rcv->state = WIC_PK_LENGTH_LSB;
 501                 rcv->nibble = WIC_NB_BEGIN;
 502 
 503         case WIC_PK_LENGTH_LSB:
 504                 if (net_debug > 2)
 505                         printk("%s: WIC_PK_LENGTH_LSB\n", dev->name);
 506                 if (snd->state != WIC_PK_DONE) {
 507                         if (wic_receive(nl->trigger, status_addr,
 508                                          &rcv->nibble, &rcv->length.b.lsb)) {
 509                                 /* collision, here dev->tbusy == 1 */
 510                                 rcv->state = WIC_PK_DONE;
 511                                 nl->is_deferred = 1;
 512                                 nl->connection = WIC_CN_SEND;
 513                                 restore_flags(flags);
 514                                 queue_task(&nl->deferred, &tq_timer);
 515                                 save |= 0x10; /* enable */
 516                                 outb(save, PAR_CONTROL(dev));
 517                                 enable_irq(dev->irq);
 518                                 return OK;
 519                         }
 520                 } else {
 521                         if (wic_receive(nibble_timeout, status_addr,
 522                                          &rcv->nibble, &rcv->length.b.lsb)) {
 523                                 restore_flags(flags);
 524                                 return TIMEOUT;
 525                         }
 526                 }
 527                 rcv->state = WIC_PK_LENGTH_MSB;
 528 
 529         case WIC_PK_LENGTH_MSB:
 530                 if (net_debug > 2)
 531                         printk("%s: WIC_PK_LENGTH_MSB\n", dev->name);
 532                 if (wic_receive(nibble_timeout, status_addr,
 533                                  &rcv->nibble, &rcv->length.b.msb)) {
 534                         restore_flags(flags);
 535                         return TIMEOUT;
 536                 }
 537                 if (rcv->length.h > dev->mtu || rcv->length.h < 8) {
 538                         printk("%s: bad packet size %d.\n", dev->name, rcv->length.h);
 539                         restore_flags(flags);
 540                         return ERROR;
 541                 }
 542                 /* Malloc up new buffer. */
 543                 rcv->skb = dev_alloc_skb(rcv->length.h);
 544                 if (rcv->skb == NULL) {
 545                         printk("%s: Memory squeeze.\n", dev->name);
 546                         restore_flags(flags);
 547                         return ERROR;
 548                 }
 549                 skb_put(rcv->skb,rcv->length.h);
 550                 rcv->skb->dev = dev;
 551                 
 552                 rcv->state = WIC_PK_DATA;
 553                 rcv->byte = 0;
 554                 rcv->checksum = 0;
 555                 
 556                 /* sequence numbers */
 557                 if (net_debug > 2)
 558                         printk("%s: WIC_PK_SEQ\n", dev->name);
 559                 if (wic_receive(nibble_timeout, status_addr,
 560                                  &rcv->nibble, &junk)) {
 561                         restore_flags(flags);
 562                         return TIMEOUT;
 563                 }
 564                 if (wic_receive(nibble_timeout, status_addr,
 565                                  &rcv->nibble, &junk)) {
 566                         restore_flags(flags);
 567                         return TIMEOUT;
 568                 }
 569 
 570         case WIC_PK_DATA:
 571                 if (net_debug > 2)
 572                         printk("%s: WIC_PK_DATA: length %i\n", dev->name, 
 573                                 rcv->length.h);
 574                 lbuf = rcv->skb->data;
 575                 do {
 576                         if (wic_receive(nibble_timeout, status_addr, 
 577                                          &rcv->nibble, &lbuf[rcv->byte])) {
 578                                 restore_flags(flags);
 579                                 return TIMEOUT;
 580                         }
 581                 } while (++rcv->byte < (rcv->length.h - 4));
 582 
 583                 /* receive pad byte */
 584                 if (rcv->length.h & 0x01)
 585                         wic_receive(nibble_timeout, status_addr, 
 586                                          &rcv->nibble, &lbuf[rcv->byte]);
 587                 
 588                 do {
 589                         rcv->checksum += lbuf[--rcv->byte];
 590                 } while (rcv->byte);
 591 
 592                 rcv->state = WIC_PK_CHECKSUM;
 593 
 594         case WIC_PK_CHECKSUM:
 595                 if (net_debug > 2)
 596                         printk("%s: WIC_PK_CHECKSUM\n", dev->name);
 597                 if (wic_receive(nibble_timeout, status_addr,
 598                                  &rcv->nibble, &junk)) {
 599                         restore_flags(flags);
 600                         return TIMEOUT;
 601                 }
 602                 outb(0, PAR_DATA(dev));
 603                 rcv->state = WIC_PK_DONE;
 604 
 605         case WIC_PK_DONE:
 606                 if (net_debug > 2)
 607                         printk("%s: WIC_PK_DONE\n", dev->name);
 608                 /* Inform the upper layer for the arrival of a packet. */
 609                 netif_rx(rcv->skb);
 610                 nl->enet_stats.rx_packets++;
 611                 rcv->skb = NULL;
 612                 if (net_debug > 2)
 613                         printk("%s: receive end\n", dev->name);
 614 
 615                 /* Close the connection. */
 616                 if (snd->state != WIC_PK_DONE) {
 617                         nl->connection = WIC_CN_SEND;
 618                         restore_flags(flags);
 619                         queue_task(&nl->immediate, &tq_immediate);
 620                         save |= 0x10; /* enable */
 621                         outb(save, PAR_CONTROL(dev));
 622                         enable_irq(dev->irq);
 623                         return OK;
 624                 } else {
 625                         nl->connection = WIC_CN_NONE;
 626                         restore_flags(flags);
 627                         save |= 0x10; /* enable */
 628                         outb(save, PAR_CONTROL(dev));
 629                         enable_irq(dev->irq);
 630                         return OK;
 631                 }
 632         }
 633         restore_flags(flags);
 634         return OK;
 635 }
 636 
 637 /* WIC_SEND --- send a byte (two nibbles) 
 638    Returns OK on success, TIMEOUT when timeout    */
 639 inline int
 640 wic_send(unsigned short nibble_timeout, unsigned short data_addr,
     /* [previous][next][first][last][top][bottom][index][help] */
 641           enum wic_nibble_state *ns_p, unsigned char data)
 642 {
 643 unsigned int cx;
 644 
 645         cx = LOOPCNT;
 646         while ((inb(data_addr+1) & 0x80) == ((tog<<7) & 0x80)) {
 647                 if (--cx == 0) {
 648                         return -TIMEOUT;
 649                 }
 650         }
 651         outb(data, data_addr);
 652         outb(tog | save, data_addr+2);
 653         tog ^= 0x01;
 654         return OK;
 655 }
 656 
 657 /* WIC_SEND_PACKET --- send a packet */
 658 int
 659 wic_send_packet(struct device *dev, struct net_local *nl,
     /* [previous][next][first][last][top][bottom][index][help] */
 660                  struct wic_local *snd, struct wic_local *rcv)
 661 {
 662         unsigned short data_addr = PAR_DATA(dev);
 663         unsigned short nibble_timeout = nl->nibble;
 664         unsigned char *lbuf;
 665         unsigned int cx;
 666         unsigned int pad = 2;
 667         unsigned long flags;
 668 
 669         if (snd->skb == NULL || (lbuf = snd->skb->data) == NULL) {
 670                 printk("%s: send skb lost\n", dev->name);
 671                 snd->state = WIC_PK_DONE;
 672                 snd->skb = NULL;
 673                 save |= 0x10; /* enable */
 674                 outb(save, PAR_CONTROL(dev));
 675                 enable_irq(dev->irq);
 676                 return ERROR;
 677         }
 678 
 679         save_flags(flags);      
 680         cli();
 681         switch (snd->state) {
 682         case WIC_PK_TRIGGER:
 683         
 684                 if (nl->connection == WIC_CN_RECEIVE) {
 685                         /* interrupted */
 686                         nl->enet_stats.collisions++;
 687                         restore_flags(flags);
 688                         if (net_debug > 1)
 689                                 printk("%s: collision.\n", dev->name);
 690                         save |= 0x10; /* enable */
 691                         outb(save, PAR_CONTROL(dev));
 692                         enable_irq(dev->irq);
 693                         return OK;
 694                 }
 695                 
 696                 disable_irq(dev->irq);
 697                 save &= 0xef; /* disable */
 698                 outb(save, PAR_CONTROL(dev));
 699                 
 700                 /* interrupt controller */
 701                 tog = 3;
 702                 outb(0x06 | save, PAR_CONTROL(dev));
 703                         
 704                 cx = LOOPCNT;
 705                 while ((inb(PAR_STATUS(dev)) & 0xe8) != 0xc0) {
 706                         if (--cx == 0) {
 707                                 restore_flags(flags);
 708                                 return TIMEOUT;
 709                         }
 710                         if (cx == 10)
 711                                 outb(0x02, PAR_CONTROL(dev));
 712                 }
 713                 
 714                 if (net_debug > 2)
 715                         printk("%s: send start\n", dev->name);
 716                 snd->state = WIC_PK_LENGTH_LSB;
 717                 snd->nibble = WIC_NB_BEGIN;
 718                 nl->timeout_count = 0;
 719 
 720         case WIC_PK_LENGTH_LSB:
 721                 if (snd->length.h & 0x01)
 722                         pad = 3;
 723                 else
 724                         pad = 2;
 725                 snd->length.h += (4 + pad); /* len + seq + data + pad */
 726                 if (net_debug > 2)
 727                         printk("%s: WIC_PK_LENGTH_LSB: length = %i\n", 
 728                                 dev->name, snd->length.h);
 729 
 730                 if (wic_send(nibble_timeout, data_addr,
 731                               &snd->nibble, snd->length.b.lsb)) {
 732                         restore_flags(flags);
 733                         return TIMEOUT;
 734                 }
 735                 snd->state = WIC_PK_LENGTH_MSB;
 736 
 737         case WIC_PK_LENGTH_MSB:
 738                 if (net_debug > 2)
 739                         printk("%s: WIC_PK_LENGTH_MSB\n", dev->name);
 740                 if (wic_send(nibble_timeout, data_addr,
 741                               &snd->nibble, snd->length.b.msb)) {
 742                         restore_flags(flags);
 743                         return TIMEOUT;
 744                 }
 745                 snd->state = WIC_PK_DATA;
 746                 snd->byte = 0;
 747                 snd->checksum = 0;
 748 
 749         case WIC_PK_DATA:
 750                 /* adjust length back to data only */
 751                 snd->length.h -= (4 + pad); /* len + seq + data + pad */
 752                 /* send 2 byte sequence number */
 753                 if (net_debug > 2)
 754                         printk("%s: WIC_SEQ\n", dev->name);
 755                 if (wic_send(nibble_timeout, data_addr,
 756                               &snd->nibble, 0)) {
 757                         restore_flags(flags);
 758                         return TIMEOUT;
 759                 }
 760                 if (wic_send(nibble_timeout, data_addr,
 761                               &snd->nibble, 0)) {
 762                         restore_flags(flags);
 763                         return TIMEOUT;
 764                 }       
 765                 if (net_debug > 2)
 766                         printk("%s: WIC_PK_DATA\n", dev->name);
 767 
 768                 do {
 769                         if (wic_send(nibble_timeout, data_addr,
 770                                       &snd->nibble, lbuf[snd->byte])) {
 771                                 restore_flags(flags);
 772                                 return TIMEOUT;
 773                         }
 774                 }
 775                 while (++snd->byte < snd->length.h);
 776                 
 777                 do
 778                         snd->checksum += lbuf[--snd->byte];
 779                 while (snd->byte);
 780 
 781                 snd->state = WIC_PK_CHECKSUM;
 782 
 783         case WIC_PK_CHECKSUM:
 784                 /* send pad bytes */
 785                 if (net_debug > 2)
 786                         printk("%s: WIC_PK_PAD: %i bytes\n", 
 787                                 dev->name, pad);
 788                 while(pad--)
 789                         if (wic_send(nibble_timeout, data_addr,
 790                                 &snd->nibble, 0)) {
 791                                 restore_flags(flags);
 792                                 return TIMEOUT;
 793                         }
 794                 dev_kfree_skb(snd->skb, FREE_WRITE);
 795                 nl->enet_stats.tx_packets++;
 796                 snd->state = WIC_PK_DONE;
 797 
 798         case WIC_PK_DONE:
 799                 if (net_debug > 2)
 800                         printk("%s: WIC_PK_DONE\n", dev->name);
 801                 /* Close the connection */
 802                 outb (0x00, PAR_DATA(dev));
 803                 outb(save, PAR_CONTROL(dev));
 804                 
 805                 snd->skb = NULL;
 806                 if (net_debug > 2)
 807                         printk("%s: send end\n", dev->name);
 808                 nl->connection = WIC_CN_CLOSING;
 809                 nl->is_deferred = 1;
 810                 restore_flags(flags);
 811                 queue_task(&nl->deferred, &tq_timer);
 812                 save |= 0x10; /* enable */
 813                 outb(save, PAR_CONTROL(dev));
 814                 enable_irq(dev->irq);
 815                 return OK;
 816         }
 817         restore_flags(flags);
 818         return OK;
 819 }
 820 
 821 int
 822 wic_connection_close(struct device *dev, struct net_local *nl,
     /* [previous][next][first][last][top][bottom][index][help] */
 823                       struct wic_local *snd, struct wic_local *rcv)
 824 {
 825 unsigned long flags;
 826 
 827         save_flags(flags);
 828         cli();
 829         if (nl->connection == WIC_CN_CLOSING) {
 830                 nl->connection = WIC_CN_NONE;
 831                 dev->tbusy = 0;
 832                 mark_bh(NET_BH);
 833         }
 834         restore_flags(flags);
 835         return OK;
 836 }
 837 
 838 /* WIC_ERROR --- wait till other end settled */
 839 int
 840 wic_error(struct device *dev, struct net_local *nl,
     /* [previous][next][first][last][top][bottom][index][help] */
 841            struct wic_local *snd, struct wic_local *rcv)
 842 {
 843         unsigned char status;
 844 
 845         status = inb(PAR_STATUS(dev));
 846         if ((status & 0xf8) == 0x80) {
 847                 if (net_debug > 2)
 848                         printk("%s: reset interface.\n", dev->name);
 849                 nl->connection = WIC_CN_NONE;
 850                 dev->tbusy = 0;
 851                 dev->interrupt = 0;
 852                 save |= 0x10; /* enable */
 853                 outb(save, PAR_CONTROL(dev));
 854                 enable_irq(dev->irq);
 855                 mark_bh(NET_BH);
 856         } else {
 857                 nl->is_deferred = 1;
 858                 queue_task(&nl->deferred, &tq_timer);
 859         }
 860 
 861         return OK;
 862 }
 863 
 864 /* Handle the parallel port interrupts. */
 865 void
 866 wic_interrupt(int irq, void *dev_ptr, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 867 {
 868         struct device *dev = (struct device *) irq2dev_map[irq];
 869         struct net_local *nl = (struct net_local *)dev->priv;
 870         struct wic_local *rcv = &nl->rcv_data;
 871         unsigned long flags;
 872 
 873         if (dev == NULL) {
 874                 printk ("wic_interrupt: irq %d for unknown device.\n", irq);
 875                 return;
 876         }
 877 
 878         if (dev->interrupt) {
 879                 return;
 880         }
 881 
 882         if (check_bfr(dev) < 0) {
 883                 return;
 884         }
 885         
 886         dev->interrupt = 1;
 887         if (net_debug > 3)
 888                 printk("%s: interrupt.\n", dev->name);
 889 
 890         save_flags(flags);
 891         cli();
 892         switch (nl->connection) {
 893         case WIC_CN_CLOSING:
 894                 dev->tbusy = 0;
 895         case WIC_CN_NONE:
 896         case WIC_CN_SEND:
 897                 dev->last_rx = jiffies;
 898                 rcv->state = WIC_PK_TRIGGER;
 899                 nl->connection = WIC_CN_RECEIVE;
 900                 nl->timeout_count = 0;
 901                 restore_flags(flags);
 902                 queue_task(&nl->immediate, &tq_immediate);
 903                 mark_bh(IMMEDIATE_BH);
 904                 break;
 905 
 906         case WIC_CN_RECEIVE:
 907                 printk("%s: receive interrupt when receiving packet\n", dev->name);
 908                 restore_flags(flags);
 909                 break;
 910 
 911         case WIC_CN_ERROR:
 912                 printk("%s: receive interrupt in error state\n", dev->name);
 913                 restore_flags(flags);
 914                 break;
 915         }
 916 }
 917 
 918 int
 919 wic_rebuild_header(void *buff, struct device *dev, unsigned long dst,
     /* [previous][next][first][last][top][bottom][index][help] */
 920                     struct sk_buff *skb)
 921 {
 922         struct net_local *nl = (struct net_local *)dev->priv;
 923         struct ethhdr *eth = (struct ethhdr *)buff;
 924         int i;
 925 
 926         if ((dev->flags & IFF_NOARP)==0)
 927                 return nl->orig_rebuild_header(buff, dev, dst, skb);
 928 
 929         if (eth->h_proto != htons(ETH_P_IP)) {
 930                 printk("wic_rebuild_header: Don't know how to resolve type %d addresses?\n", (int)eth->h_proto);
 931                 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
 932                 return 0;
 933         }
 934 
 935         for (i=0; i < ETH_ALEN - sizeof(unsigned long); i++)
 936                 eth->h_dest[i] = 0xfc;
 937         memcpy(&(eth->h_dest[i]), &dst, sizeof(unsigned long));
 938         return 0;
 939 }
 940 
 941 int
 942 wic_tx_packet(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 943 {
 944         struct net_local *nl = (struct net_local *)dev->priv;
 945         struct wic_local *snd = &nl->snd_data;
 946         unsigned long flags;
 947 
 948         if (dev->tbusy)
 949                 return 1;
 950 
 951         /* If some higher layer thinks we've missed an tx-done interrupt
 952            we are passed NULL. Caution: dev_tint() handles the cli()/sti()
 953            itself. */
 954         if (skb == NULL) {
 955                 dev_tint(dev);
 956                 return 0;
 957         }
 958 
 959         if (set_bit(0, (void*)&dev->tbusy) != 0) {
 960                 printk("%s: Transmitter access conflict.\n", dev->name);
 961                 return 1;
 962         }
 963 
 964         if (skb->len > dev->mtu) {
 965                 printk("%s: packet too big, %d.\n", dev->name, (int)skb->len);
 966                 dev->tbusy = 0;
 967                 return 0;
 968         }
 969 
 970         if (net_debug > 2)
 971                 printk("%s: send request\n", dev->name);
 972 
 973         save_flags(flags);
 974         cli();
 975         dev->trans_start = jiffies;
 976         snd->skb = skb;
 977         snd->length.h = skb->len;
 978         snd->state = WIC_PK_TRIGGER;
 979         if (nl->connection == WIC_CN_NONE) {
 980                 nl->connection = WIC_CN_SEND;
 981                 nl->timeout_count = 0;
 982         }
 983         restore_flags(flags);
 984         queue_task(&nl->immediate, &tq_immediate);
 985         mark_bh(IMMEDIATE_BH);
 986 
 987         return 0;
 988 }
 989 
 990 /* Open/initialize the board.  This is called (in the current kernel)
 991    sometime after booting when the 'ifconfig' program is run.
 992 
 993    This routine gets exclusive access to the parallel port by allocating
 994    its IRQ line.
 995  */
 996 int
 997 wic_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 998 {
 999         struct net_local *nl = (struct net_local *)dev->priv;
1000         unsigned long flags;
1001 
1002         if (dev->irq == 0) {
1003                 printk("%s: IRQ is not set.  Please set it by ifconfig.\n", dev->name);
1004                 return -EAGAIN;
1005         }
1006         save_flags(flags);
1007         cli();
1008         check_bfr(dev);
1009         if (request_irq(dev->irq , wic_interrupt, 0, dev->name, NULL) != 0) {
1010                 sti();
1011                 printk("%s: couldn't get IRQ %d.\n", dev->name, dev->irq);
1012                 return -EAGAIN;
1013         }
1014         irq2dev_map[dev->irq] = dev;
1015         restore_flags(flags);
1016 
1017         save |= 0x10; /* enable */
1018         outb(save, PAR_CONTROL(dev));
1019         /* Initialize the state machine. */
1020         nl->rcv_data.state = nl->snd_data.state = WIC_PK_DONE;
1021         nl->rcv_data.skb = nl->snd_data.skb = NULL;
1022         nl->connection = WIC_CN_NONE;
1023         nl->is_deferred = 0;
1024 
1025         dev->interrupt = 0;
1026         dev->start = 1;
1027         dev->tbusy = 0;
1028         MOD_INC_USE_COUNT;
1029         return 0;
1030 }
1031 
1032 /* The inverse routine to wic_open (). */
1033 int
1034 wic_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1035 {
1036         struct net_local *nl = (struct net_local *)dev->priv;
1037         struct wic_local *snd = &nl->snd_data;
1038         struct wic_local *rcv = &nl->rcv_data;
1039 
1040         dev->tbusy = 1;
1041         dev->start = 0;
1042         cli();
1043         free_irq(dev->irq, NULL);
1044         irq2dev_map[dev->irq] = NULL;
1045         nl->is_deferred = 0;
1046         nl->connection = WIC_CN_NONE;
1047         sti();
1048         outb(0x00, PAR_DATA(dev));
1049 
1050         snd->state = WIC_PK_DONE;
1051         if (snd->skb) {
1052                 snd->skb->free = 1;
1053                 dev_kfree_skb(snd->skb, FREE_WRITE);
1054                 snd->skb = NULL;
1055         }
1056         rcv->state = WIC_PK_DONE;
1057         if (rcv->skb) {
1058                 rcv->skb->free = 1;
1059                 kfree_skb(rcv->skb, FREE_READ);
1060                 rcv->skb = NULL;
1061         }
1062 
1063         MOD_DEC_USE_COUNT;
1064         return 0;
1065 }
1066 
1067 struct enet_statistics *
1068 wic_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1069 {
1070         struct net_local *nl = (struct net_local *)dev->priv;
1071         struct enet_statistics *r = &nl->enet_stats;
1072 
1073         return r;
1074 }
1075 
1076 int
1077 wic_config(struct device *dev, struct ifmap *map)
     /* [previous][next][first][last][top][bottom][index][help] */
1078 {
1079         if (dev->flags & IFF_UP)
1080                 return -EBUSY;
1081 
1082         if (map->base_addr != (unsigned long)-1
1083             && map->base_addr != dev->base_addr)
1084                 printk("%s: You cannot change base_addr of this interface (ignored).\n", dev->name);
1085 
1086         if (map->irq != (unsigned char)-1)
1087                 dev->irq = map->irq;
1088         return 0;
1089 }
1090 
1091 int
1092 wic_ioctl(struct device *dev, struct ifreq *rq, int cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
1093 {
1094         struct wicconf wc;
1095         int err;
1096         char len = 0;
1097         unsigned long flags;
1098 
1099         err=verify_area(VERIFY_WRITE, rq->ifr_data, sizeof(struct wicconf));
1100         if (err)
1101                 return err;
1102         memcpy_fromfs(&wc, rq->ifr_data, sizeof(struct wicconf));
1103         switch(wc.pcmd) {
1104                 case WIC_AYT:
1105                         strcpy(wc.data, version);
1106                         wc.len = strlen(wc.data);
1107                         memcpy_tofs(rq->ifr_data, &wc, sizeof(struct wicconf));
1108                         /* return 0; */
1109                         break;
1110                 case WIC_RESET:
1111                         wic_reset(dev);
1112                         return(0);
1113                         /* break; */
1114                 case WIC_SETSN:
1115                         len = 17;
1116                         break;
1117                 case WIC_SETPS:
1118                         len = 3;
1119                         break;
1120                 case WIC_SETAF:
1121                 case WIC_SETGPF:
1122                         len = 2;
1123                         break;
1124                 case WIC_SETNET:
1125                         len = 23;
1126                         break;
1127                 case WIC_SETSYS:
1128                         len = 15;
1129                         break;
1130                 case WIC_GETVERH:
1131                 case WIC_GETNL:
1132                 case WIC_GETSN:
1133                 case WIC_CLRSTATS:
1134                 case WIC_GETSTATS:
1135                 case WIC_GETVERM:
1136                 case WIC_GETNET:
1137                 case WIC_GETSYS:
1138                         len = 1;
1139                         break;  
1140                 default:
1141                         return -EOPNOTSUPP;
1142         }
1143 
1144         /* Wait for lock to free */
1145         while (set_bit(0, (void *)&dev->tbusy) != 0); 
1146         save_flags(flags);
1147         cli();
1148 
1149         disable_irq(dev->irq);
1150         save &= 0xef; /* disable */
1151         outb(save, PAR_CONTROL(dev));
1152         err = check_bfr(dev);
1153         tog = 3;
1154         err = send_cmd(dev, (unsigned char *)&wc, len);
1155 
1156         if (wc.pcmd & 0x40) {   /* response */
1157                 len = (char)recv_cmd_resp(dev, wc.data);
1158                 while ((len == 1) && (wc.data[0] == 0x7)) { /* controller int */
1159                         len = (char)recv_cmd_resp(dev, wc.data);
1160                 }
1161                 save |= 0x10; /* enable */
1162                 outb(save, PAR_CONTROL(dev));
1163                 enable_irq(dev->irq);
1164                 wc.len = (len <0) ? 0 : len;
1165                 memcpy_tofs(rq->ifr_data, &wc, sizeof(struct wicconf));
1166         } else {
1167                 save |= 0x10; /* enable */
1168                 outb(save, PAR_CONTROL(dev));
1169                 enable_irq(dev->irq);
1170         }
1171         restore_flags(flags);
1172 
1173         outb(0, PAR_DATA(dev));
1174         dev->tbusy = 0;
1175         return 0;
1176 }
1177 
1178 int
1179 get_byte(struct device *dev, unsigned char *c)
     /* [previous][next][first][last][top][bottom][index][help] */
1180 {
1181 unsigned int cx;
1182 
1183         cx = LOOPCNT;
1184         while ((inb(PAR_STATUS(dev)) & 0x08) != ((tog << 3)&0x08)) {
1185                 if (--cx == 0) {
1186                         return(-TIMEOUT);
1187                 }
1188         }
1189         /* receive a byte of data */
1190         *c = inb(PAR_DATA(dev));
1191         tog ^= 0x01;
1192         /* ack reception of data */
1193         outb(tog| save, PAR_CONTROL(dev));
1194         return OK;
1195 }
1196 
1197 int
1198 ack_resp(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1199 {
1200 unsigned int cx;
1201         
1202         outb(save | 0x27, PAR_CONTROL(dev));
1203 
1204         /* wait for controller to remove interrupt [Ack(low), Busy(low)] */
1205         cx = LOOPCNT;
1206         while ((inb(PAR_STATUS(dev)) & 0xc0) != 0x80) {
1207                 if (--cx == 0) {
1208                         return -TIMEOUT;
1209                 }
1210         }
1211         
1212         outb(save | 0x22, PAR_CONTROL(dev));
1213         cx = LOOPCNT;
1214         while ((inb(PAR_STATUS(dev)) & 0x08) == 0x08) {
1215                 if (--cx == 0) {
1216                         return TIMEOUT;
1217                 }
1218         }
1219         tog |= 0x20;
1220         tog &= 0xfe;
1221         return OK;
1222 }
1223 
1224 void
1225 wic_reset(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1226 {
1227 unsigned char stat;
1228 
1229         stat = inb(PAR_CONTROL(dev));
1230         outb(0, PAR_DATA(dev));
1231         outb(stat | 0x08, PAR_CONTROL(dev));
1232         outb(stat & 0xf7, PAR_CONTROL(dev));
1233         dev->tbusy = 0;
1234         dev->interrupt = 0;
1235         tog = 3;
1236         save = 0;
1237         return;
1238 }
1239 
1240 int
1241 check_bfr(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1242 {
1243 unsigned char c0, l;
1244 
1245         if ((inb(PAR_STATUS(dev)) & 0xc8) == 0x48) {
1246                 save |= 0x80;
1247                 outb(0x23| save, PAR_CONTROL(dev));
1248                 ack_resp(dev);
1249                 get_byte(dev, &l);      /* len */
1250                 while (l--) {
1251                         get_byte(dev, &c0);
1252                 }
1253                 get_byte(dev, &c0);
1254                 save &=0x7f;
1255                 outb(0, PAR_DATA(dev));
1256                 return -l;
1257         } else
1258         return (0);
1259 }
1260 
1261 
1262 int
1263 recv_cmd_resp(struct device *dev, unsigned char *buf)
     /* [previous][next][first][last][top][bottom][index][help] */
1264 {
1265 unsigned char cksum = 0;
1266 int err;
1267 unsigned char c0 = 0;
1268 int len;
1269 int savelen;
1270 unsigned int cx;
1271 int i;
1272 
1273         tog &= 0xfe;
1274         cx = LOOPCNT;
1275         while ((inb(PAR_STATUS(dev)) & 0xc8) != 0x48) {
1276                 if (--cx == 0) {
1277                         /* clear Busy */
1278                         outb(0, PAR_DATA(dev));
1279                         printk("rcv_cmd_resp: timeout\n");
1280                         return -TIMEOUT;
1281                 }
1282         }
1283         
1284         /* acknowledge the interrupt */
1285         i = ack_resp(dev);
1286 
1287         /* get length */
1288         err = get_byte(dev, &c0);
1289         if (err < 0) {
1290                 printk("get_byte1: failed\n");
1291                 return(err);
1292         }
1293         len = c0;
1294         savelen = len;
1295 
1296         /* get data */
1297         while(len--) {
1298                 err = get_byte(dev, &c0);
1299                 if (err < 0) {
1300                         printk("get_byte2: failed\n");
1301                         return(err);
1302                 }
1303                 outb(0, PAR_DATA(dev)); 
1304                 *buf = c0;
1305                 cksum += c0;
1306                 buf++;
1307         }       
1308         /* get cksum */
1309         err = get_byte(dev, &c0);
1310         if (err < 0) {
1311                 printk("get_byte3: failed\n");
1312                 return(err);
1313         }
1314         if (cksum != c0) {
1315                 printk("cksum failed\n");
1316                 return(-3);
1317         }
1318         /* get trailing byte, if any... */
1319         get_byte(dev, &c0);
1320         return(savelen);
1321 }       
1322 
1323 int
1324 send_byte(struct device *dev, unsigned char c)
     /* [previous][next][first][last][top][bottom][index][help] */
1325 {
1326 unsigned int cx;
1327 
1328         cx = LOOPCNT;
1329         while ((inb(PAR_STATUS(dev)) & 0x80) == ((tog<<7) & 0x80)) {
1330                 if (--cx == 0) {
1331                         return(-TIMEOUT);
1332                 }
1333         }
1334         outb(c, PAR_DATA(dev));
1335         outb(save |tog, PAR_CONTROL(dev));
1336         tog ^= 0x01;
1337         return OK;
1338 }
1339 
1340 
1341 int
1342 send_cmd(struct device *dev, unsigned char *cmd, char len)
     /* [previous][next][first][last][top][bottom][index][help] */
1343 {
1344 unsigned char cksum = 0;
1345 int err = 0;
1346 unsigned int cx;
1347 
1348         /* interrupt controller */
1349         outb(save | 0x04, PAR_CONTROL(dev));
1350         /* wait for ACK */
1351         cx = LOOPCNT;
1352         while ((inb(PAR_STATUS(dev)) & 0xe8) != 0xc0) {
1353                 if (--cx == 0) 
1354                         return -TIMEOUT;
1355                 if (cx == 10)
1356                         outb(0x02, PAR_CONTROL(dev));
1357         }
1358         /* cmd coming... */
1359         outb(save | 0x02, PAR_CONTROL(dev));
1360         /* send length byte */
1361         err = send_byte(dev, (unsigned char)len);
1362         
1363         /* send data */
1364         while (len--) {
1365                 err = send_byte(dev, *cmd);     
1366                 if (err < 0) {
1367                         return err;
1368                 }
1369                 cksum += *cmd;
1370                 cmd++;
1371         }
1372         
1373         /* send cksum byte */
1374         err = send_byte(dev, cksum);    
1375         if (err < 0)
1376                 return err;
1377 
1378         cx = LOOPCNT;
1379         while ((inb(PAR_STATUS(dev)) & 0x80) == ((tog <<7)&0x80)) {
1380                 if (--cx == 0) 
1381                         return -TIMEOUT;
1382         }
1383         save |= 0x80;
1384         outb(save | 0x23, PAR_CONTROL(dev));
1385         outb(0, PAR_DATA(dev));
1386         return OK;      
1387 }
1388 
1389 #ifdef MODULE
1390 struct device dev_wic0 = 
1391 {
1392         "wic0" /*"wic"*/,
1393         0, 0, 0, 0,             /* memory */
1394         0x3BC, 5,               /* base, irq */
1395         0, 0, 0, NULL, wic_init 
1396 };
1397 
1398 struct device dev_wic1 = 
1399 {
1400         "wic1" /*"wic"*/,
1401         0, 0, 0, 0,             /* memory */
1402         0x378, 7,               /* base, irq */
1403         0, 0, 0, NULL, wic_init 
1404 };
1405 
1406 struct device dev_wic2 = 
1407 {
1408         "wic2" /*"wic"*/,
1409         0, 0, 0, 0,             /* memory */
1410         0x278, 2,               /* base, irq */
1411         0, 0, 0, NULL, wic_init 
1412 };
1413 
1414 int
1415 init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1416 {
1417         int devices=0;
1418 
1419         if (register_netdev(&dev_wic0) != 0)
1420                 devices++;
1421         if (register_netdev(&dev_wic1) != 0)
1422                 devices++;
1423         if (register_netdev(&dev_wic2) != 0)
1424                 devices++;
1425         if (devices == 0)
1426                 return -EIO;
1427         return 0;
1428 }
1429 
1430 void
1431 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1432 {
1433         if (dev_wic0.priv) {
1434                 unregister_netdev(&dev_wic0);
1435                 release_region(PAR_DATA(&dev_wic0), 3);
1436                 kfree_s(dev_wic0.priv, sizeof(struct net_local));
1437                 dev_wic0.priv = NULL;
1438         }
1439         if (dev_wic1.priv) {
1440                 unregister_netdev(&dev_wic1);
1441                 release_region(PAR_DATA(&dev_wic1), 3);
1442                 kfree_s(dev_wic1.priv, sizeof(struct net_local));
1443                 dev_wic1.priv = NULL;
1444         }
1445         if (dev_wic2.priv) {
1446                 unregister_netdev(&dev_wic2);
1447                 release_region(PAR_DATA(&dev_wic2), 3);
1448                 kfree_s(dev_wic2.priv, sizeof(struct net_local));
1449                 dev_wic2.priv = NULL;
1450         }
1451 }
1452 #endif /* MODULE */
1453 
1454 /*
1455  * Local variables:
1456  * compile-command: "gcc -DMODULE -DCONFIG_MODVERSIONS -D__KERNEL__ -Wall -Wstrict-prototypes -O2 -g -fomit-frame-pointer -pipe -m486 -c wic.c"
1457  * End:
1458  */

/* [previous][next][first][last][top][bottom][index][help] */