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

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