root/drivers/net/arcnet.c

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

DEFINITIONS

This source file includes following definitions.
  1. arcnet_probe
  2. arcnet_ioprobe
  3. arcnet_memprobe
  4. arcnet_open
  5. arcnet_close
  6. arcnet_send_packet
  7. arcnet_continue_tx
  8. careful_xmit_wait
  9. arcnet_prepare_tx
  10. arcnet_go_tx
  11. arcnet_interrupt
  12. arcnet_inthandler
  13. arcnet_rx
  14. arcnet_timer
  15. arcnet_get_stats
  16. set_multicast_list
  17. arcnet_reset
  18. arc_header
  19. arc_rebuild_header
  20. arc_type_trans
  21. init_module
  22. cleanup_module

   1 /* arcnet.c
   2         Written 1994-95 by Avery Pennarun, derived from skeleton.c by
   3         Donald Becker.
   4 
   5         Contact Avery at: apenwarr@tourism.807-city.on.ca or
   6         RR #5 Pole Line Road, Thunder Bay, ON, Canada P7C 5M9
   7         
   8         **********************
   9 
  10         skeleton.c Written 1993 by Donald Becker.
  11         Copyright 1993 United States Government as represented by the
  12         Director, National Security Agency.  This software may only be used
  13         and distributed according to the terms of the GNU Public License as
  14         modified by SRC, incorporated herein by reference.
  15          
  16         **********************
  17 
  18         v1.01 (95/03/24)
  19           - Fixed some IPX-related bugs. (Thanks to Tomasz Motylewski
  20             <motyl@tichy.ch.uj.edu.pl> for the patches to make arcnet work
  21             with dosemu!)
  22         v1.0 (95/02/15)
  23           - Initial non-alpha release.
  24         
  25          
  26         TO DO:
  27         
  28          - Test in systems with NON-ARCnet network cards, just to see if
  29            autoprobe kills anything.  With any luck, it won't.  (It's pretty
  30            careful.)
  31                 - Except some unfriendly NE2000's die. (as of 0.40-ALPHA)
  32          - cards with shared memory that can be "turned off?"
  33          - NFS mount freezes after several megabytes to SOSS for DOS. 
  34            unmount/remount works.  Is this arcnet-specific?  I don't know.
  35          - Add support for the various stupid bugs ("I didn't read the RFC"
  36            syndrome) in Windows for Workgroups and LanMan.
  37  */
  38  
  39 /**************************************************************************/
  40 
  41 /* define this if you want to use the new but possibly dangerous ioprobe
  42  * If you get lockups right after status5, you probably need
  43  * to undefine this.  It should make more cards probe correctly,
  44  * I hope.
  45  */
  46 #define DANGER_PROBE
  47 
  48 /* define this if you want to use the "extra delays" which were removed
  49  * in 0.41 since they seemed needless.
  50  */
  51 #undef EXTRA_DELAYS
  52 
  53 /* undefine this if you want to use the non-IRQ-driven transmitter. (possibly
  54  * safer, although it takes more CPU time and IRQ_XMIT seems fine right now)
  55  */
  56 #define IRQ_XMIT
  57  
  58 /* define this for "careful" transmitting.  Try with and without if you have
  59  * problems.  If you use IRQ_XMIT, do NOT define this.
  60  */
  61 #undef CAREFUL_XMIT
  62 
  63 /* define this for an extra-careful memory detect.  This should work all
  64  * the time now, but you never know.
  65  */
  66 #define STRICT_MEM_DETECT
  67 
  68 /* define this to use the "old-style" limited MTU by default.  It basically
  69  * disables packet splitting.  ifconfig can still be used to reset the MTU.
  70  *
  71  * leave this disabled if possible, so it will use ethernet defaults,
  72  * which is our goal.
  73  */
  74 #undef LIMIT_MTU
  75 
  76 /* define this if you have a problem with the card getting "stuck" now and
  77  * then, which can only be fixed by a reboot or resetting the card manually
  78  * via ifconfig up/down.  ARCnet will set a timer function which is called
  79  * 8 times every second.
  80  *
  81  * This should no longer be necessary.  if you experience "stuck" ARCnet
  82  * drivers, please email apenwarr@tourism.807-city.on.ca or I will remove
  83  * this feature in a future release.
  84  */
  85 #undef USE_TIMER_HANDLER
  86 
  87 /**************************************************************************/
  88  
  89 static char *version =
  90  "arcnet.c:v1.01 95/03/24 Avery Pennarun <apenwarr@tourism.807-city.on.ca>\n";
  91 
  92 /*
  93   Sources:
  94         Crynwr arcnet.com/arcether.com packet drivers.
  95         arcnet.c v0.00 dated 1/1/94 and apparently by
  96                 Donald Becker - it didn't work :)
  97         skeleton.c v0.05 dated 11/16/93 by Donald Becker
  98                 (from Linux Kernel 1.1.45)
  99         ...I sure wish I had the ARCnet data sheets right about now!
 100         RFC's 1201 and 1051 (mostly 1201) - re: ARCnet IP packets
 101         net/inet/eth.c (from kernel 1.1.50) for header-building info...
 102         Alternate Linux ARCnet source by V.Shergin <vsher@sao.stavropol.su>
 103         Textual information and more alternate source from Joachim Koenig
 104                 <jojo@repas.de>
 105 */
 106 
 107 #include <linux/config.h>
 108 #ifdef MODULE
 109 #include <linux/module.h>
 110 #include <linux/version.h>
 111 #endif /* MODULE */
 112 
 113 #include <linux/kernel.h>
 114 #include <linux/sched.h>
 115 #include <linux/types.h>
 116 #include <linux/fcntl.h>
 117 #include <linux/interrupt.h>
 118 #include <linux/ptrace.h>
 119 #include <linux/ioport.h>
 120 #include <linux/in.h>
 121 #include <linux/malloc.h>
 122 #include <linux/string.h>
 123 #include <linux/timer.h>
 124 #include <linux/errno.h>
 125 #include <linux/delay.h>
 126 
 127 #include <asm/system.h>
 128 #include <asm/bitops.h>
 129 #include <asm/io.h>
 130 #include <asm/dma.h>
 131 
 132 #include <linux/netdevice.h>
 133 #include <linux/etherdevice.h>
 134 #include <linux/skbuff.h>
 135 #include "arp.h"
 136 
 137 
 138 /* debug levels:
 139  * D_OFF        production
 140  * D_NORMAL     verification
 141  * D_INIT       show init/detect messages
 142  * D_DURING     show messages during normal use (ie interrupts)
 143  * D_DATA   show packets data from skb's, not on Arcnet card
 144  * D_TX         show tx packets
 145  * D_RX         show tx+rx packets
 146  */
 147 #define D_OFF           0
 148 #define D_NORMAL        1
 149 #define D_INIT          2
 150 #define D_EXTRA         3
 151 #define D_DURING        4
 152 #define D_DATA          6
 153 #define D_TX            8
 154 #define D_RX            9
 155 
 156 #ifndef NET_DEBUG
 157 #define NET_DEBUG       D_INIT
 158 #endif
 159 static unsigned int net_debug = NET_DEBUG;
 160 
 161 #ifndef HAVE_AUTOIRQ
 162 /* From auto_irq.c, in ioport.h for later versions. */
 163 extern void autoirq_setup(int waittime);
 164 extern int autoirq_report(int waittime);
 165 /* The map from IRQ number (as passed to the interrupt handler) to
 166    'struct device'. */
 167 extern struct device *irq2dev_map[16];
 168 #endif
 169 
 170 #ifndef HAVE_PORTRESERVE
 171 #define check_region(ioaddr, size)              0
 172 #define request_region(ioaddr, size)            do ; while (0)
 173 #endif
 174 
 175 /* macro to simplify debug checking */
 176 #define BUGLVL(x) if (net_debug>=x)
 177 
 178 /* The number of low I/O ports used by the ethercard. */
 179 #define ETHERCARD_TOTAL_SIZE    16
 180 
 181 
 182 /* Handy defines for ARCnet specific stuff */
 183         /* COM 9026 (?) --> ARCnet register addresses */
 184 #define INTMASK (ioaddr+0)              /* writable */
 185 #define STATUS  (ioaddr+0)              /* readable */
 186 #define COMMAND (ioaddr+1)      /* writable, returns random vals on read (?) */
 187 #define RESET  (ioaddr+8)               /* software reset writable */
 188 
 189         /* time needed for various things (in clock ticks, 1/100 sec) */
 190 #define RESETtime 40            /* reset */
 191 #define XMITtime 10             /* send (?) */
 192 #define ACKtime 10              /* acknowledge (?) */
 193 
 194         /* these are the max/min lengths of packet data. (including
 195          * ClientData header)
 196          * note: packet sizes 250, 251, 252 are impossible (God knows why)
 197          *  so exception packets become necessary.
 198          *
 199          * These numbers are compared with the length of the full packet,
 200          * including ClientData header.
 201          */
 202 #define MTU     (253+EXTRA_CLIENTDATA)  /* normal packet max size */
 203 #define MinTU   (257+EXTRA_CLIENTDATA)  /* extended packet min size */
 204 #define XMTU    (508+EXTRA_CLIENTDATA)  /* extended packet max size */
 205 
 206         /* status/interrupt mask bit fields */
 207 #define TXFREEflag      0x001            /* transmitter available */
 208 #define TXACKflag       0x002            /* transmitted msg. ackd */
 209 #define RECONflag       0x004            /* system reconfigured */
 210 #define TESTflag        0x008            /* test flag */
 211 #define RESETflag       0x010            /* power-on-reset */
 212 #define RES1flag        0x020            /* unused */
 213 #define RES2flag        0x040            /* unused */
 214 #define NORXflag        0x080            /* receiver inhibited */
 215 
 216        /* in the command register, the following bits have these meanings:
 217         *                0-2     command
 218         *                3-4     page number (for enable rcv/xmt command)
 219         *                 7      receive broadcasts
 220         */
 221 #define NOTXcmd         0x001            /* disable transmitter */
 222 #define NORXcmd         0x002            /* disable receiver */
 223 #define TXcmd           0x003            /* enable transmitter */
 224 #define RXcmd           0x004            /* enable receiver */
 225 #define CONFIGcmd       0x005            /* define configuration */
 226 #define CFLAGScmd       0x006            /* clear flags */
 227 #define TESTcmd         0x007            /* load test flags */
 228 
 229        /* flags for "clear flags" command */
 230 #define RESETclear      0x008            /* power-on-reset */
 231 #define CONFIGclear     0x010            /* system reconfigured */
 232 
 233       /* flags for "load test flags" command */
 234 #define TESTload        0x008            /* test flag (diagnostic) */
 235 
 236       /* byte deposited into first address of buffers on reset */
 237 #define TESTvalue       0321             /* that's octal for 0xD1 :) */
 238 
 239       /* for "enable receiver" command */
 240 #define RXbcasts        0x080            /* receive broadcasts */
 241 
 242       /* flags for "define configuration" command */
 243 #define NORMALconf      0x000            /* 1-249 byte packets */
 244 #define EXTconf         0x008            /* 250-504 byte packets */
 245 
 246         /* buffers (4 total) used for receive and xmit.
 247          */
 248 #define EnableReceiver()        outb(RXcmd|(recbuf<<3)|RXbcasts,COMMAND)
 249 /*#define TXbuf         2 (Obsoleted by ping-pong xmits) */
 250 
 251         /* Protocol ID's */
 252 #define ARC_P_IP        212             /* 0xD4 */
 253 #define ARC_P_ARP       213             /* 0xD5 */
 254 #define ARC_P_RARP      214             /* 0xD6 */
 255 #define ARC_P_IPX       250             /* 0xFA */
 256 #define ARC_P_LANSOFT   251             /* 0xFB */
 257 #define ARC_P_ATALK     0xDD
 258 
 259         /* Length of time between "stuck" checks */
 260 #define TIMERval        (HZ/8)          /* about 1/8 second */
 261 
 262         /* these structures define the format of an arcnet packet. */
 263 #define NORMAL          0
 264 #define EXTENDED        1
 265 #define EXCEPTION       2
 266 
 267         /* the header required by the card itself */
 268 struct HardHeader
 269 {
 270         u_char  source,         /* source ARCnet - filled in automagically */
 271                 destination,    /* destination ARCnet - 0 for broadcast */
 272                 offset1,        /* offset of ClientData (256-byte packets) */
 273                 offset2;        /* offset of ClientData (512-byte packets) */
 274 };
 275 
 276         /* a complete ARCnet packet */
 277 union ArcPacket
 278 {
 279         struct HardHeader hardheader;   /* the hardware header */
 280         u_char raw[512];                /* raw packet info, incl ClientData */
 281 };
 282 
 283         /* the "client data" header - RFC-1201 information
 284          * notice that this screws up if it's not an even number of bytes
 285          * <sigh>
 286          */
 287 struct ClientData
 288 {
 289         /* data that's NOT part of real packet */
 290         u_char  daddr;          /* Destination address - stored here,
 291                                  *   but WE MUST GET RID OF IT BEFORE SENDING A
 292                                  *   PACKET!!
 293                                  */
 294         u_char  saddr;          /* Source address - necessary for IPX protocol */
 295         
 296         /* data that IS part of real packet */
 297         u_char  protocol_id,    /* ARC_P_IP, ARC_P_ARP, or ARC_P_RARP */
 298                 split_flag;     /* for use with split packets */
 299         u_short sequence;       /* sequence number (?) */
 300 };
 301 #define EXTRA_CLIENTDATA (sizeof(struct ClientData)-4)
 302 
 303 
 304 /* "Incoming" is information needed for each address that could be sending
 305  * to us.  Mostly for partially-received split packets.
 306  */
 307 struct Incoming
 308 {
 309         struct sk_buff *skb;            /* packet data buffer             */
 310         unsigned char lastpacket,       /* number of last packet (from 1) */
 311                       numpackets;       /* number of packets in split     */
 312         u_short sequence;               /* sequence number of assembly    */
 313 };
 314 
 315 struct Outgoing
 316 {
 317         struct sk_buff *skb;            /* buffer from upper levels */
 318         struct ClientData *hdr;         /* clientdata of last packet */
 319         u_char *data;                   /* pointer to data in packet */
 320         short length,                   /* bytes total */
 321               dataleft,                 /* bytes left */
 322               segnum,                   /* segment being sent */
 323               numsegs,                  /* number of segments */
 324               seglen;                   /* length of segment */
 325 };
 326 
 327 
 328 /* Information that needs to be kept for each board. */
 329 struct arcnet_local {
 330         struct enet_statistics stats;
 331         u_char arcnum;          /* arcnet number - our 8-bit address */
 332         u_short sequence;       /* sequence number (incs with each packet) */
 333         u_char recbuf,          /* receive buffer # (0 or 1) */
 334                txbuf,           /* transmit buffer # (2 or 3) */
 335                txready;         /* buffer where a packet is ready to send */
 336         short intx,             /* in TX routine? */
 337               in_txhandler,     /* in TX_IRQ handler? */
 338               sending;          /* transmit in progress? */
 339         short tx_left;          /* segments of split packet left to TX */
 340         struct timer_list timer; /* the timer interrupt struct */
 341         struct Incoming incoming[256];  /* one from each address */
 342         struct Outgoing outgoing; /* packet currently being sent */
 343 };
 344 
 345 
 346 /* Index to functions, as function prototypes. */
 347 extern int arcnet_probe(struct device *dev);
 348 #ifndef MODULE
 349 static int arcnet_memprobe(struct device *dev,u_char *addr);
 350 static int arcnet_ioprobe(struct device *dev, short ioaddr);
 351 #endif
 352 
 353 static int arcnet_open(struct device *dev);
 354 static int arcnet_close(struct device *dev);
 355 
 356 static int arcnet_send_packet(struct sk_buff *skb, struct device *dev);
 357 #ifdef CAREFUL_XMIT
 358  static void careful_xmit_wait(struct device *dev);
 359 #else
 360  #define careful_xmit_wait(dev)
 361 #endif
 362 static void arcnet_continue_tx(struct device *dev);
 363 static void arcnet_prepare_tx(struct device *dev,struct ClientData *hdr,
 364                 short length,char *data);
 365 static void arcnet_go_tx(struct device *dev);
 366 
 367 static void arcnet_interrupt(int irq,struct pt_regs *regs);
 368 static void arcnet_inthandler(struct device *dev);
 369 static void arcnet_rx(struct device *dev,int recbuf);
 370 
 371 #ifdef USE_TIMER_HANDLER
 372 static void arcnet_timer(unsigned long arg);
 373 #endif
 374 
 375 static struct enet_statistics *arcnet_get_stats(struct device *dev);
 376 static void set_multicast_list(struct device *dev, int num_addrs, void *addrs);
 377 
 378         /* annoying functions for header/arp/etc building */
 379 int arc_header(unsigned char *buff,struct device *dev,unsigned short type,
 380                 void *daddr,void *saddr,unsigned len,struct sk_buff *skb);
 381 int arc_rebuild_header(void *eth,struct device *dev,unsigned long raddr,
 382                 struct sk_buff *skb);
 383 unsigned short arc_type_trans(struct sk_buff *skb,struct device *dev);
 384 
 385 static int arcnet_reset(struct device *dev);
 386 
 387 #ifdef MODULE
 388 int  init_module(void);
 389 void cleanup_module(void);
 390 #endif
 391 
 392 #define tx_done(dev) 1
 393 
 394 /*
 395 #define JIFFER(time) for (delayval=jiffies+(time); delayval>jiffies;);
 396 */
 397 #define JIFFER(time) for (delayval=0; delayval<(time*10); delayval++) \
 398                 udelay(1000);
 399 
 400 #ifdef EXTRA_DELAYS
 401  #define XJIFFER(time) JIFFER(time)
 402 #else
 403  #define XJIFFER(time)
 404 #endif
 405 
 406 
 407 /* Check for a network adaptor of this type, and return '0' if one exists.
 408  *  If dev->base_addr == 0, probe all likely locations.
 409  *  If dev->base_addr == 1, always return failure.
 410  *  If dev->base_addr == 2, allocate space for the device and return success
 411  *  (detachable devices only).
 412  */
 413 int
 414 arcnet_probe(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 415 {
 416 #ifndef MODULE
 417         /* I refuse to probe anything less than 0x200, because anyone using
 418          * an address like that should probably be shot.
 419          */
 420         int *port, ports[] = {/* first the suggested values! */
 421                               0x300,0x2E0,0x2F0,0x2D0,
 422                               /* ...now everything else possible. */
 423                               0x200,0x210,0x220,0x230,0x240,0x250,0x260,0x270,
 424                               0x280,0x290,0x2a0,0x2b0,0x2c0,
 425                                     0x310,0x320,0x330,0x340,0x350,0x360,0x370,
 426                               0x380,0x390,0x3a0,/* video ports, */0x3e0,0x3f0,
 427                               /* a null ends the list */
 428                               0};
 429         /* I'm not going to probe below 0xA0000 either, for similar reasons.
 430          */
 431         unsigned long *addr, addrs[] = {0xD0000,0xE0000,0xA0000,0xB0000,
 432                                         0xC0000,0xF0000,
 433                                         /* from <mdrejhon@magi.com> */
 434                                         0xE1000,
 435                                         0xDD000,0xDC000,
 436                                         0xD9000,0xD8000,0xD5000,0xD4000,0xD1000,
 437                                         0xCD000,0xCC000,
 438                                         0xC9000,0xC8000,0xC5000,0xC4000,
 439                                         /* terminator */
 440                                         0};
 441         int base_addr=dev->base_addr, status=0;
 442 #endif /* MODULE */
 443         int delayval;
 444         struct arcnet_local *lp;
 445 
 446         if (net_debug)
 447         {
 448                 printk(version);
 449                 printk("arcnet: ***\n");
 450                 printk("arcnet: * Read linux/drivers/net/README.arcnet for important release notes!\n");
 451                 printk("arcnet: *\n");
 452                 printk("arcnet: * This version should be stable, but e-mail me if you have any\n");
 453                 printk("arcnet: * questions, comments, or bug reports!\n");
 454                 printk("arcnet: ***\n");
 455         }
 456 
 457         BUGLVL(D_INIT)
 458                 printk("arcnet: given: base %lXh, IRQ %Xh, shmem %lXh\n",
 459                         dev->base_addr,dev->irq,dev->mem_start);
 460                         
 461 #ifndef MODULE
 462         if (base_addr > 0x1ff)          /* Check a single specified location. */
 463                 status=arcnet_ioprobe(dev, base_addr);
 464         else if (base_addr > 0)         /* Don't probe at all. */
 465                 return ENXIO;
 466         else for (port = &ports[0]; *port; port++)
 467         {
 468                 int ioaddr = *port;
 469                 if (check_region(ioaddr, ETHERCARD_TOTAL_SIZE))
 470                 {
 471                         BUGLVL(D_INIT)
 472                                 printk("arcnet: Skipping %Xh because of check_region...\n",
 473                                         ioaddr);
 474                         continue;
 475                 }
 476 
 477                 status=arcnet_ioprobe(dev, ioaddr);
 478                 if (!status) break;
 479         }
 480         
 481         if (status) return status;
 482 
 483         /* ioprobe turned out okay.  Now give it a couple seconds to finish
 484          * initializing...
 485          */
 486         BUGLVL(D_INIT)
 487                 printk("arcnet: ioprobe okay!  Waiting for reset...\n");
 488         JIFFER(100);
 489 
 490         /* okay, now we have to find the shared memory area. */
 491         BUGLVL(D_INIT)
 492                 printk("arcnet: starting memory probe, given %lXh\n",
 493                         dev->mem_start);
 494         if (dev->mem_start)     /* value given - probe just that one */
 495         {
 496                 status=arcnet_memprobe(dev,(u_char *)dev->mem_start);
 497                 if (status) return status;
 498         }
 499         else                    /* no value given - probe everything */
 500         {
 501                 for (addr = &addrs[0]; *addr; addr++) {
 502                         status=arcnet_memprobe(dev,(u_char *)(*addr));
 503                         if (!status) break;
 504                 }
 505                 
 506                 if (status) return status;
 507         }
 508 #else /* MODULE */
 509         if (!dev->base_addr || !dev->irq || !dev->mem_start 
 510                 || !dev->rmem_start)
 511         {
 512                 printk("arcnet: loadable modules can't autoprobe!\n");
 513                 printk("arcnet:  try using io=, irqnum=, and shmem= on the insmod line.\n");
 514                 printk("arcnet:  you may also need num= to change the device name. (ie. num=1 for arc1)\n");
 515                 return ENODEV;
 516         }
 517 #endif
 518         /* now reserve the irq... */
 519         {       
 520                 int irqval = request_irq(dev->irq, &arcnet_interrupt, 0,
 521                         "arcnet");
 522                 if (irqval) {
 523                         printk("%s: unable to get IRQ %d (irqval=%d).\n",
 524                                 dev->name,dev->irq, irqval);
 525                         return EAGAIN;
 526                  }
 527          }
 528          
 529         /* Grab the region so we can find another board if autoIRQ fails. */
 530         request_region(dev->base_addr, ETHERCARD_TOTAL_SIZE,"arcnet");
 531         
 532         printk("%s: ARCnet card found at %03lXh, IRQ %d, ShMem at %lXh.\n", 
 533                 dev->name, dev->base_addr, dev->irq, dev->mem_start);
 534 
 535         /* Initialize the device structure. */
 536         dev->priv = kmalloc(sizeof(struct arcnet_local), GFP_KERNEL);
 537         memset(dev->priv, 0, sizeof(struct arcnet_local));
 538         lp=(struct arcnet_local *)(dev->priv);
 539 
 540         dev->open               = arcnet_open;
 541         dev->stop               = arcnet_close;
 542         dev->hard_start_xmit = arcnet_send_packet;
 543         dev->get_stats  = arcnet_get_stats;
 544 #ifdef HAVE_MULTICAST
 545         dev->set_multicast_list = &set_multicast_list;
 546 #endif
 547 
 548         /* Fill in the fields of the device structure with ethernet-generic values. */
 549         ether_setup(dev);
 550 
 551         /* And now fill particular ones with arcnet values :) */
 552         
 553         dev->type=ARPHRD_ARCNET;
 554         dev->hard_header_len=sizeof(struct ClientData);
 555         BUGLVL(D_EXTRA)
 556                 printk("arcnet: ClientData header size is %d.\narcnet: HardHeader size is %d.\n",
 557                         sizeof(struct ClientData),sizeof(struct HardHeader));
 558 #if LIMIT_MTU   /* the old way - normally, now use ethernet default */
 559         dev->mtu=512-sizeof(struct HardHeader)+EXTRA_CLIENTDATA;
 560 #endif
 561                 /* since we strip EXTRA_CLIENTDATA bytes off before sending,
 562                  * we let Linux add that many bytes to the packet data...
 563                  */
 564         dev->addr_len=1;
 565         dev->broadcast[0]=0x00;
 566         
 567         BUGLVL(D_INIT) printk("arcnet: arcnet_probe: resetting card.\n");
 568         arcnet_reset(dev);
 569         JIFFER(50);
 570         BUGLVL(D_NORMAL)
 571                 printk("arcnet: We appear to be station %d (%02Xh)\n",
 572                         lp->arcnum,lp->arcnum);
 573         if (lp->arcnum==0)
 574                 printk("arcnet: WARNING!  Station address 0 is reserved for broadcasts!\n");
 575         if (lp->arcnum==255)
 576                 printk("arcnet: WARNING!  Station address 255 may confuse DOS networking programs!\n");
 577         dev->dev_addr[0]=lp->arcnum;
 578         lp->sequence=1;
 579         lp->recbuf=0;
 580 
 581         dev->hard_header        = arc_header;
 582         dev->rebuild_header     = arc_rebuild_header;
 583         dev->type_trans         = arc_type_trans;
 584 
 585         return 0;
 586 }
 587 
 588 #ifndef MODULE
 589 
 590 int arcnet_ioprobe(struct device *dev, short ioaddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 591 {
 592         int delayval,airq;
 593 
 594         BUGLVL(D_INIT)
 595                 printk("arcnet: probing address %Xh\n",ioaddr);
 596 
 597         BUGLVL(D_INIT)
 598                 printk("arcnet:  status1=%Xh\n",inb(STATUS));
 599 
 600                 
 601         /* very simple - all we have to do is reset the card, and if there's
 602          * no irq, it's not an ARCnet.  We can also kill two birds with
 603          * one stone because we detect the IRQ at the same time :)
 604          */
 605          
 606         /* reset the card by reading the reset port */
 607         inb(RESET);
 608         JIFFER(RESETtime);
 609 
 610         /* if status port is FF, there's certainly no arcnet... give up. */
 611         if (inb(STATUS)==0xFF)
 612         {
 613                 BUGLVL(D_INIT)
 614                         printk("arcnet:  probe failed.  Status port empty.\n");
 615                 return ENODEV;
 616         }
 617 
 618         /* we'll try to be reasonably sure it's an arcnet by making sure
 619          * the value of the COMMAND port changes automatically once in a
 620          * while.  I have no idea what those values ARE, but at least
 621          * they work.
 622          */
 623         {
 624                 int initval,curval;
 625                 
 626                 curval=initval=inb(COMMAND);
 627                 delayval=jiffies+5;
 628                 while (delayval>=jiffies && curval==initval)
 629                         curval=inb(COMMAND);
 630                         
 631                 if (curval==initval)
 632                 {
 633                         printk("arcnet:  probe failed.  never-changing command port (%02Xh).\n",
 634                                 initval);
 635                         return ENODEV;
 636                 }
 637         }
 638 
 639         BUGLVL(D_INIT)
 640                 printk("arcnet:  status2=%Xh\n",inb(STATUS));
 641 
 642         /* now we turn the reset bit off so we can IRQ next reset... */
 643         outb(CFLAGScmd|RESETclear|CONFIGclear,COMMAND);
 644         XJIFFER(ACKtime);
 645         if (inb(STATUS) & RESETflag) /* reset flag STILL on */
 646         {
 647                 BUGLVL(D_INIT)
 648                         printk("arcnet:  probe failed.  eternal reset flag1...(status=%Xh)\n",
 649                                 inb(STATUS));
 650                 return ENODEV;
 651         }
 652 
 653         /* set up automatic IRQ detection */
 654         autoirq_setup(0);
 655         
 656         /* enable reset IRQ's (shouldn't be necessary, but worth a try) */
 657         outb(RESETflag,INTMASK);
 658 
 659         /* now reset it again to generate an IRQ */
 660         inb(RESET);
 661         JIFFER(RESETtime);
 662 
 663         BUGLVL(D_INIT)
 664                 printk("arcnet:  status3=%Xh\n",inb(STATUS));
 665 
 666         /* and turn the reset flag back off */
 667         outb(CFLAGScmd|RESETclear|CONFIGclear,COMMAND);
 668         XJIFFER(ACKtime);
 669 
 670         BUGLVL(D_INIT)
 671                 printk("arcnet:  status4=%Xh\n",inb(STATUS));
 672 
 673         /* enable reset IRQ's again */
 674         outb(RESETflag,INTMASK);
 675 
 676         /* now reset it again to generate an IRQ */
 677         inb(RESET);
 678         JIFFER(RESETtime);
 679         
 680         BUGLVL(D_INIT)
 681                 printk("arcnet:  status5=%Xh\n",inb(STATUS));
 682 
 683         /* if we do this, we're sure to get an IRQ since the card has
 684          * just reset and the NORXflag is on until we tell it to start
 685          * receiving.
 686          *
 687          * However, this could, theoretically, cause a lockup.  Maybe I'm just
 688          * not very good at theory! :)
 689          */
 690 #ifdef DANGER_PROBE
 691         outb(NORXflag,INTMASK);
 692         JIFFER(RESETtime);
 693         outb(0,INTMASK);
 694 #endif
 695 
 696         /* and turn the reset flag back off */
 697         outb(CFLAGScmd|RESETclear|CONFIGclear,COMMAND);
 698         XJIFFER(ACKtime);
 699 
 700         airq = autoirq_report(0);
 701         if (net_debug>=D_INIT && airq)
 702                 printk("arcnet:  autoirq is %d\n", airq);
 703 
 704         /* if there was no autoirq AND the user hasn't set any defaults,
 705          * give up.
 706          */
 707         if (!airq && !(dev->base_addr && dev->irq))
 708         {
 709                 BUGLVL(D_INIT)
 710                         printk("arcnet:  probe failed.  no autoirq...\n");
 711                 return ENODEV;
 712         }
 713 
 714         /* otherwise we probably have a card.  Let's make sure. */
 715         
 716         if (inb(STATUS) & RESETflag) /* reset flag on */
 717         {
 718                 /* now we turn the reset bit off */
 719                 outb(CFLAGScmd|RESETclear|CONFIGclear,COMMAND);
 720                 XJIFFER(ACKtime);
 721         }
 722         
 723         if (inb(STATUS) & RESETflag) /* reset flag STILL on */
 724         {
 725                 BUGLVL(D_INIT)
 726                         printk("arcnet:  probe failed.  eternal reset flag...(status=%Xh)\n",
 727                                 inb(STATUS));
 728                 return ENODEV;
 729         }
 730         
 731         /* okay, we've got a real, live ARCnet on our hands. */
 732         if (!dev->base_addr) dev->base_addr=ioaddr;
 733         
 734         if (dev->irq < 2)               /* "Auto-IRQ" */
 735         {
 736                 /* we already did the autoirq above, so store the values */
 737                 dev->irq=airq;
 738         }
 739         else if (dev->irq == 2)
 740         {
 741                 if (net_debug)
 742                         printk("arcnet: IRQ2 == IRQ9, don't worry.\n");
 743                 dev->irq = 9;
 744         }
 745 
 746         BUGLVL(D_INIT)
 747                 printk("arcnet: irq and base address seem okay. (%lXh, IRQ %d)\n",
 748                         dev->base_addr,dev->irq);
 749         return 0;
 750 }
 751 
 752 
 753 /* A memory probe that is called after the card is reset.
 754  * It checks for the official TESTvalue in byte 0 and makes sure the buffer
 755  * has certain characteristics of an ARCnet...
 756  */
 757 int arcnet_memprobe(struct device *dev,u_char *addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 758 {
 759         BUGLVL(D_INIT)
 760                 printk("arcnet: probing memory at %lXh\n",(u_long)addr);
 761                 
 762         dev->mem_start=0;
 763 
 764 #ifdef STRICT_MEM_DETECT /* probably better. */
 765         /* ARCnet memory byte 0 is TESTvalue */
 766         if (addr[0]!=TESTvalue)
 767         {
 768                 BUGLVL(D_INIT)
 769                         printk("arcnet:  probe failed.  addr=%lXh, addr[0]=%Xh (not %Xh)\n",
 770                                 (unsigned long)addr,addr[0],TESTvalue);
 771                 return ENODEV;
 772         }
 773         
 774         /* now verify the shared memory writability */
 775         addr[0]=0x42;
 776         if (addr[0]!=0x42)
 777         {
 778                 BUGLVL(D_INIT)
 779                         printk("arcnet:  probe failed.  addr=%lXh, addr[0]=%Xh (not 42h)\n",
 780                                 (unsigned long)addr,addr[0]);
 781                 return ENODEV;
 782         }
 783 #else
 784         if (addr[0]!=TESTvalue)
 785         {
 786                 BUGLVL(D_INIT)
 787                         printk("arcnet:  probe failed.  addr=%lXh, addr[0]=%Xh (not %Xh)\n",
 788                                 (unsigned long)addr,addr[0],TESTvalue);
 789                 return ENODEV;
 790         }
 791 #endif
 792 
 793         /* got it!  fill in dev */
 794         dev->mem_start=(unsigned long)addr;
 795         dev->mem_end=dev->mem_start+512*4-1;
 796         dev->rmem_start=dev->mem_start+512*0;
 797         dev->rmem_end=dev->mem_start+512*2-1;
 798 
 799         return 0;
 800 }
 801 
 802 #endif /* MODULE */
 803 
 804 
 805 /* Open/initialize the board.  This is called (in the current kernel)
 806    sometime after booting when the 'ifconfig' program is run.
 807 
 808    This routine should set everything up anew at each open, even
 809    registers that "should" only need to be set once at boot, so that
 810    there is non-reboot way to recover if something goes wrong.
 811    */
 812 static int
 813 arcnet_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 814 {
 815         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
 816 /*      int ioaddr = dev->base_addr;*/
 817 
 818         if (dev->metric>=10)
 819         {
 820                 net_debug=dev->metric-10;
 821                 dev->metric=1;
 822         }
 823 
 824         if (net_debug) printk(version);
 825 
 826 #if 0   /* Yup, they're hardwired in arcnets */
 827         /* This is used if the interrupt line can turned off (shared).
 828            See 3c503.c for an example of selecting the IRQ at config-time. */
 829         if (request_irq(dev->irq, &arcnet_interrupt, 0, "arcnet")) {
 830                 return -EAGAIN;
 831         }
 832 #endif
 833 
 834         irq2dev_map[dev->irq] = dev;
 835 
 836         /* Reset the hardware here. */
 837         BUGLVL(D_EXTRA) printk("arcnet: arcnet_open: resetting card.\n");
 838         
 839         /* try to reset - twice if it fails the first time */
 840         if (arcnet_reset(dev) && arcnet_reset(dev))
 841                 return -ENODEV;
 842         
 843 /*      chipset_init(dev, 1);*/
 844 /*      outb(0x00, ioaddr);*/
 845 
 846 /*      lp->open_time = jiffies;*/
 847 
 848         dev->tbusy=0;
 849         dev->interrupt=0;
 850         dev->start=1;
 851         lp->intx=0;
 852         lp->in_txhandler=0;
 853 
 854 #ifdef USE_TIMER_HANDLER
 855         /* grab a timer handler to recover from any missed IRQ's */
 856         init_timer(&lp->timer);
 857         lp->timer.expires = TIMERval;         /* length of time */
 858         lp->timer.data = (unsigned long)dev;  /* pointer to "dev" structure */
 859         lp->timer.function = &arcnet_timer;    /* timer handler */
 860         add_timer(&lp->timer);
 861 #endif
 862 
 863 #ifdef MODULE
 864         MOD_INC_USE_COUNT;
 865 #endif
 866                                         
 867         return 0;
 868 }
 869 
 870 
 871 /* The inverse routine to arcnet_open(). */
 872 static int
 873 arcnet_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 874 {
 875         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
 876         int ioaddr = dev->base_addr;
 877 #ifdef EXTRA_DELAYS
 878         int delayval;
 879 #endif
 880 
 881 /*      lp->open_time = 0;*/
 882 
 883         dev->tbusy = 1;
 884         dev->start = 0;
 885         
 886         /* release the timer */
 887         del_timer(&lp->timer);
 888 
 889         /* Flush the Tx and disable Rx here.     */
 890         /* resetting the card should do the job. */
 891         /*inb(RESET);*/
 892 
 893         outb(0,INTMASK);        /* no IRQ's */
 894         outb(NOTXcmd,COMMAND);  /* disable transmit */
 895         XJIFFER(ACKtime);
 896         outb(NORXcmd,COMMAND);  /* disable receive */
 897 
 898         /* Update the statistics here. */
 899         
 900 #ifdef MODULE
 901         MOD_DEC_USE_COUNT;
 902 #endif
 903 
 904         return 0;
 905 }
 906 
 907 
 908 static int
 909 arcnet_send_packet(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 910 {
 911         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
 912         int ioaddr=dev->base_addr;
 913 /*      short daddr;*/
 914 
 915         lp->intx++;
 916 
 917         BUGLVL(D_DURING)
 918                 printk("arcnet: transmit requested (status=%Xh, inTX=%d)\n",
 919                         inb(STATUS),lp->intx);
 920 
 921         if (dev->tbusy || lp->in_txhandler)
 922         {
 923                 /* If we get here, some higher level has decided we are broken.
 924                    There should really be a "kick me" function call instead. */
 925                 int tickssofar = jiffies - dev->trans_start;
 926                 int recbuf=lp->recbuf;
 927                 int status=inb(STATUS);
 928                 
 929                 /* resume any stopped tx's */
 930 #if 0
 931                 if (lp->txready && (inb(STATUS)&TXFREEflag))
 932                 {
 933                         printk("arcnet: kickme: starting a TX (status=%Xh)\n",
 934                                 inb(STATUS));
 935                         arcnet_go_tx(dev);
 936                         lp->intx--;
 937                         return 1;
 938                 }
 939 #endif
 940                 
 941                 if (tickssofar < 5) 
 942                 {
 943                         BUGLVL(D_DURING)
 944                                 printk("arcnet: premature kickme! (status=%Xh ticks=%d o.skb=%ph numsegs=%d segnum=%d\n",
 945                                         status,tickssofar,lp->outgoing.skb,
 946                                         lp->outgoing.numsegs,
 947                                         lp->outgoing.segnum);
 948                         lp->intx--;
 949                         return 1;
 950                 }
 951 
 952                 BUGLVL(D_INIT)
 953                         printk("arcnet: transmit timed out (status=%Xh, inTX=%d, tickssofar=%d)\n",
 954                                 status,lp->intx,tickssofar);
 955 
 956                 /* Try to restart the adaptor. */
 957                 /*arcnet_reset(dev);*/
 958                 
 959                 if (status&NORXflag) EnableReceiver();
 960                 if (!(status&TXFREEflag)) outb(NOTXcmd,COMMAND);
 961                 dev->trans_start = jiffies;
 962 
 963                 if (lp->outgoing.skb)
 964                         dev_kfree_skb(lp->outgoing.skb,FREE_WRITE);
 965                 lp->outgoing.skb=NULL;
 966 
 967                 dev->tbusy=0;
 968                 mark_bh(NET_BH);
 969                 lp->intx=0;
 970                 lp->in_txhandler=0;
 971                 lp->txready=0;
 972                 lp->sending=0;
 973 
 974                 return 1;
 975         }
 976 
 977         /* If some higher layer thinks we've missed a tx-done interrupt
 978            we are passed NULL. Caution: dev_tint() handles the cli()/sti()
 979            itself. */
 980         if (skb == NULL) {
 981                 BUGLVL(D_INIT)
 982                         printk("arcnet: tx passed null skb (status=%Xh, inTX=%d, tickssofar=%ld)\n",
 983                                 inb(STATUS),lp->intx,jiffies-dev->trans_start);
 984                 dev_tint(dev);
 985                 lp->intx--;
 986                 return 0;
 987         }
 988         
 989         if (lp->txready)        /* transmit already in progress! */
 990         {
 991                 printk("arcnet: trying to start new packet while busy!\n");
 992                 printk("arcnet: marking as not ready.\n");
 993                 lp->txready=0;
 994                 return 1;
 995         }
 996 
 997         /* Block a timer-based transmit from overlapping.  This could better be
 998            done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
 999         if (set_bit(0, (void*)&dev->tbusy) != 0)
1000         {
1001             printk("arcnet: transmitter called with busy bit set! (status=%Xh, inTX=%d, tickssofar=%ld)\n",
1002                         inb(STATUS),lp->intx,jiffies-dev->trans_start);
1003             lp->intx--;
1004             return -EBUSY;
1005         }
1006         else {
1007                 struct Outgoing *out=&(lp->outgoing);
1008                 out->length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
1009                 out->hdr=(struct ClientData*)skb->data;
1010                 out->skb=skb;
1011                 BUGLVL( D_DATA ) {
1012                         short i;
1013                         for( i=0; i< skb->len; i++)
1014                         {
1015                                 if( i%16 == 0 ) printk("\n[%04hX] ",i);
1016                                 printk("%02hX ",((unsigned char*)skb->data)[i]);
1017                         }
1018                         printk("\n");
1019                 }
1020 
1021 #ifdef IRQ_XMIT
1022                 if (lp->txready && inb(STATUS)&TXFREEflag)
1023                         arcnet_go_tx(dev);
1024 #endif
1025 
1026                 
1027                 if (out->length<=XMTU)  /* fits in one packet? */
1028                 {
1029                         BUGLVL(D_TX) printk("arcnet: not splitting %d-byte packet. (split_flag=%d)\n",
1030                                         out->length,out->hdr->split_flag);
1031                         BUGLVL(D_INIT) if (out->hdr->split_flag)
1032                                 printk("arcnet: short packet has split_flag set?! (split_flag=%d)\n",
1033                                         out->hdr->split_flag);
1034                         out->numsegs=1;
1035                         out->segnum=1;
1036                         arcnet_prepare_tx(dev,out->hdr,
1037                                 out->length-sizeof(struct ClientData),
1038                                 ((char *)skb->data)+sizeof(struct ClientData));
1039                         careful_xmit_wait(dev);
1040 
1041                         /* done right away */
1042                         dev_kfree_skb(out->skb,FREE_WRITE);
1043                         out->skb=NULL;
1044                                         
1045                         if (!lp->sending)
1046                         {
1047                                 arcnet_go_tx(dev);
1048                                 
1049                                 /* inform upper layers */
1050                                 dev->tbusy=0;
1051                                 mark_bh(NET_BH);
1052                         }
1053                 }
1054                 else                    /* too big for one - split it */
1055                 {
1056                         int maxsegsize=XMTU-sizeof(struct ClientData);
1057 
1058                         out->data=(u_char *)skb->data
1059                                         + sizeof(struct ClientData);
1060                         out->dataleft=out->length-sizeof(struct ClientData);
1061                         out->numsegs=(out->dataleft+maxsegsize-1)/maxsegsize;
1062                                 
1063                         out->segnum=0;
1064                                 
1065                         BUGLVL(D_TX) printk("arcnet: packet (%d bytes) split into %d fragments:\n",
1066                                 out->length,out->numsegs);
1067 
1068 #ifdef IRQ_XMIT
1069                         /* if a packet waiting, launch it */
1070                         if (lp->txready && inb(STATUS)&TXFREEflag)
1071                                 arcnet_go_tx(dev);
1072 
1073                         if (!lp->txready)
1074                         {
1075                                 /* prepare a packet, launch it and prepare
1076                                  * another.
1077                                  */
1078                                 arcnet_continue_tx(dev);
1079                                 if (!lp->sending)
1080                                 {
1081                                         arcnet_go_tx(dev);
1082                                         arcnet_continue_tx(dev);
1083                                         if (!lp->sending)
1084                                                 arcnet_go_tx(dev);
1085                                 }
1086                         }
1087                         
1088                         /* if segnum==numsegs, the transmission is finished;
1089                          * free the skb right away.
1090                          */
1091                         if (out->segnum==out->numsegs)
1092                         {
1093                                 /* transmit completed */
1094                                 out->segnum++;
1095                                 if (out->skb)
1096                                         dev_kfree_skb(out->skb,FREE_WRITE);
1097                                 out->skb=NULL;
1098 #if 0
1099                                 /* inform upper layers */
1100                                 dev->tbusy=0;
1101                                 mark_bh(NET_BH);
1102 #endif
1103                         }
1104                         
1105 #else /* non-irq xmit */
1106                         while (out->segnum<out->numsegs)
1107                         {
1108                                 arcnet_continue_tx(dev);
1109                                 careful_xmit_wait(dev);
1110                                 arcnet_go_tx(dev);
1111                                 dev->trans_start=jiffies;
1112                         }
1113 
1114                         dev_kfree_skb(out->skb,FREE_WRITE);
1115                         out->skb=NULL;
1116 
1117                         /* inform upper layers */
1118                         dev->tbusy = 0;
1119                         mark_bh(NET_BH);
1120 #endif
1121                 }
1122         }
1123 
1124         lp->intx--;
1125         lp->stats.tx_packets++;
1126         dev->trans_start=jiffies;
1127         return 0;
1128 }
1129 
1130 static void arcnet_continue_tx(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1131 {
1132         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1133         int maxsegsize=XMTU-sizeof(struct ClientData);
1134         struct Outgoing *out=&(lp->outgoing);
1135         
1136         if (lp->txready)
1137         {
1138                 printk("arcnet: continue_tx: called with packet in buffer!\n");
1139                 return;
1140         }
1141 
1142         if (out->segnum>=out->numsegs)
1143         {
1144                 printk("arcnet: continue_tx: building segment %d of %d!\n",
1145                         out->segnum+1,out->numsegs);
1146         }
1147 
1148         if (!out->segnum)       /* first packet */
1149                 out->hdr->split_flag=((out->numsegs-2)<<1)+1;
1150         else
1151                 out->hdr->split_flag=out->segnum<<1;
1152 
1153         out->seglen=maxsegsize;
1154         if (out->seglen>out->dataleft) out->seglen=out->dataleft;
1155                         
1156         BUGLVL(D_TX) printk("arcnet: building packet #%d (%d bytes) of %d (%d total), splitflag=%d\n",
1157                 out->segnum+1,out->seglen,out->numsegs,
1158                 out->length,out->hdr->split_flag);
1159 
1160         arcnet_prepare_tx(dev,out->hdr,out->seglen,out->data);
1161                 
1162         out->dataleft-=out->seglen;
1163         out->data+=out->seglen;
1164         out->segnum++;
1165 }
1166 
1167 #ifdef CAREFUL_XMIT
1168 static void careful_xmit_wait(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1169 {
1170         int ioaddr=dev->base_addr;
1171         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1172 
1173         /* wait patiently for tx to become available again */
1174         while ( !(inb(STATUS)&TXFREEflag) )
1175         {
1176                 if (jiffies-dev->trans_start > 20 || !dev->tbusy)
1177                 {
1178                         BUGLVL(D_INIT)
1179                                 printk("arcnet: CAREFUL_XMIT timeout. (busy=%d, status=%Xh)\n",
1180                                         dev->tbusy,inb(STATUS));
1181                         lp->stats.tx_errors++;
1182                         
1183                         outb(NOTXcmd,COMMAND);
1184                         return;
1185                 }
1186         }
1187         BUGLVL(D_TX) printk("arcnet: transmit completed successfully. (status=%Xh)\n",
1188                                 inb(STATUS));
1189 }
1190 #endif
1191 
1192 static void
1193 arcnet_prepare_tx(struct device *dev,struct ClientData *hdr,short length,
     /* [previous][next][first][last][top][bottom][index][help] */
1194                 char *data)
1195 {
1196 /*      int ioaddr = dev->base_addr;*/
1197         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1198         struct ClientData *arcsoft;
1199         union ArcPacket *arcpacket = 
1200                 (union ArcPacket *)(dev->mem_start+512*(lp->txbuf^1));
1201         u_char pkttype;
1202         int offset;
1203         short daddr;
1204         
1205         lp->txbuf=lp->txbuf^1;  /* XOR with 1 to alternate between 2 and 3 */
1206         
1207         length+=sizeof(struct ClientData);
1208 
1209         BUGLVL(D_TX)
1210                 printk("arcnet: arcnet_prep_tx: hdr:%ph, length:%d, data:%ph\n",
1211                         hdr,length,data);
1212 
1213         /* clean out the page to make debugging make more sense :) */
1214         BUGLVL(D_DURING)
1215                 memset((void *)dev->mem_start+lp->txbuf*512,0x42,512);
1216 
1217         daddr=arcpacket->hardheader.destination=hdr->daddr;
1218 
1219         /* load packet into shared memory */
1220         if (length<=MTU)        /* Normal (256-byte) Packet */
1221         {
1222                 pkttype=NORMAL;
1223                         
1224                 arcpacket->hardheader.offset1=offset=256-length
1225                                 + EXTRA_CLIENTDATA;
1226                 arcsoft=(struct ClientData *)
1227                         (&arcpacket->raw[offset-EXTRA_CLIENTDATA]);
1228         }
1229         else if (length>=MinTU) /* Extended (512-byte) Packet */
1230         {
1231                 pkttype=EXTENDED;
1232                 
1233                 arcpacket->hardheader.offset1=0;
1234                 arcpacket->hardheader.offset2=offset=512-length
1235                         + EXTRA_CLIENTDATA;
1236                 arcsoft=(struct ClientData *)
1237                         (&arcpacket->raw[offset-EXTRA_CLIENTDATA]);
1238         }
1239         else                            /* Exception Packet */
1240         {
1241                 pkttype=EXCEPTION;
1242                 
1243                 arcpacket->hardheader.offset1=0;
1244                 arcpacket->hardheader.offset2=offset=512-length-4
1245                         + EXTRA_CLIENTDATA;
1246                 arcsoft=(struct ClientData *)
1247                         (&arcpacket->raw[offset+4-EXTRA_CLIENTDATA]);
1248                 
1249                 /* exception-specific stuff - these four bytes
1250                  * make the packet long enough to fit in a 512-byte
1251                  * frame.
1252                  */
1253                 arcpacket->raw[offset+0]=arcsoft->protocol_id;
1254                 arcpacket->raw[offset+1]=0xFF; /* FF flag */
1255                         arcpacket->raw[offset+2]=0xFF; /* FF padding */
1256                         arcpacket->raw[offset+3]=0xFF; /* FF padding */
1257         }
1258 
1259 
1260         /* copy the packet into ARCnet shmem
1261          *  - the first bytes of ClientData header are skipped
1262          */
1263         memcpy((u_char*)arcsoft+EXTRA_CLIENTDATA,
1264                 (u_char*)hdr+EXTRA_CLIENTDATA,
1265                 sizeof(struct ClientData)-EXTRA_CLIENTDATA);
1266         memcpy((u_char*)arcsoft+sizeof(struct ClientData),
1267                 data,
1268                 length-sizeof(struct ClientData));
1269                 
1270         BUGLVL(D_DURING) printk("arcnet: transmitting packet to station %02Xh (%d bytes, type=%d)\n",
1271                         daddr,length,pkttype);
1272                         
1273         BUGLVL(D_TX)
1274         {
1275                 int countx,county;
1276                 
1277                 printk("arcnet: packet dump [tx] follows:");
1278 
1279                 for (county=0; county<16+(pkttype!=NORMAL)*16; county++)
1280                 {
1281                         printk("\n[%04X] ",county*16);
1282                         for (countx=0; countx<16; countx++)
1283                                 printk("%02X ",
1284                                         arcpacket->raw[county*16+countx]);
1285                 }
1286                 
1287                 printk("\n");
1288         }
1289 #ifdef CAREFUL_XMIT
1290  #if 0
1291         careful_xmit_wait(dev);
1292 
1293         /* if we're not broadcasting, make sure the xmit was ack'd.
1294          * if it wasn't, there is probably no card with that
1295          * address... or else it missed our tx somehow.
1296          */
1297         if (daddr && !(inb(STATUS)&TXACKflag))
1298         {       
1299                 BUGLVL(D_INIT)
1300                         printk("arcnet: transmit not acknowledged. (status=%Xh, daddr=%02Xh)\n",
1301                                 inb(STATUS),daddr);
1302                 lp->stats.tx_errors++;
1303                 return -ENONET; /* "machine is not on the network" */
1304         }
1305  #endif
1306 #endif
1307         lp->txready=lp->txbuf;  /* packet is ready for sending */
1308 
1309 #if 0
1310 #ifdef IRQ_XMIT
1311         if (inb(STATUS)&TXFREEflag) arcnet_go_tx(dev);
1312 #endif
1313 #endif
1314 }
1315 
1316 static void
1317 arcnet_go_tx(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1318 {
1319         struct arcnet_local *lp=(struct arcnet_local *)dev->priv;
1320         int ioaddr=dev->base_addr;
1321 
1322         BUGLVL(D_DURING)
1323                 printk("arcnet: go_tx: status=%Xh\n",
1324                         inb(STATUS));
1325         
1326         if (!(inb(STATUS)&TXFREEflag) || !lp->txready) return;
1327 
1328         /* start sending */
1329         outb(TXcmd|(lp->txready<<3),COMMAND);
1330 
1331 #ifdef IRQ_XMIT
1332         outb(TXFREEflag|NORXflag,INTMASK);
1333 #endif
1334 
1335         dev->trans_start = jiffies;
1336         lp->txready=0;
1337         lp->sending++;
1338 }
1339 
1340 
1341 /* The typical workload of the driver:
1342    Handle the network interface interrupts. */
1343 static void
1344 arcnet_interrupt(int irq,struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
1345 {
1346         struct device *dev = (struct device *)(irq2dev_map[irq]);
1347 
1348         if (dev == NULL) {
1349                 if (net_debug >= D_DURING)
1350                         printk("arcnet: irq %d for unknown device.\n", irq);
1351                 return;
1352         }
1353         
1354         arcnet_inthandler(dev);
1355 }
1356 
1357 static void
1358 arcnet_inthandler(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1359 {       
1360         struct arcnet_local *lp;
1361         int ioaddr, status, boguscount = 3, didsomething;
1362         
1363         dev->interrupt = 1;
1364         sti();
1365 
1366         ioaddr = dev->base_addr;
1367         lp = (struct arcnet_local *)dev->priv;
1368 
1369 #ifdef IRQ_XMIT
1370         outb(0,INTMASK);
1371 #endif
1372         
1373         BUGLVL(D_DURING)
1374                 printk("arcnet: in net_interrupt (status=%Xh)\n",inb(STATUS));
1375 
1376         do
1377         {
1378                 status = inb(STATUS);
1379                 didsomething=0;
1380         
1381                 if (!dev->start)
1382                 {
1383                         BUGLVL(D_EXTRA)
1384                                 printk("arcnet: ARCnet not yet initialized.  irq ignored. (status=%Xh)\n",
1385                                         status);
1386 #ifdef IRQ_XMIT
1387                         if (!(status&NORXflag))
1388                                 outb(NORXflag,INTMASK);
1389 #endif
1390                         dev->interrupt=0;
1391                         return;
1392                 }
1393         
1394                 /* RESET flag was enabled - card is resetting and if RX
1395                  * is disabled, it's NOT because we just got a packet.
1396                  */
1397                 if (status & RESETflag)
1398                 {
1399                         BUGLVL(D_INIT)
1400                                 printk("arcnet: reset irq (status=%Xh)\n",
1401                                         status);
1402                         dev->interrupt=0;
1403                         return;
1404                 }
1405 
1406 #if 1   /* yes, it's silly to disable this part but it makes good testing */
1407                 /* RX is inhibited - we must have received something. */
1408                 if (status & NORXflag)
1409                 {
1410                         int recbuf=lp->recbuf=!lp->recbuf;
1411 
1412                         BUGLVL(D_DURING)
1413                                 printk("arcnet: receive irq (status=%Xh)\n",
1414                                         status);
1415 
1416                         /* enable receive of our next packet */
1417                         EnableReceiver();
1418                 
1419                         /* Got a packet. */
1420                         arcnet_rx(dev,!recbuf);
1421                         
1422                         didsomething++;
1423                 }
1424 #endif
1425 #ifdef IRQ_XMIT
1426                 /* it can only be an xmit-done irq if we're xmitting :) */
1427                 if (status&TXFREEflag && !lp->in_txhandler && lp->sending)
1428                 {
1429                         struct Outgoing *out=&(lp->outgoing);
1430                         
1431                         lp->in_txhandler++;
1432                         lp->sending--;
1433                         
1434                         BUGLVL(D_DURING)
1435                                 printk("arcnet: TX IRQ (stat=%Xh, numsegs=%d, segnum=%d, skb=%ph)\n",
1436                                         status,out->numsegs,out->segnum,out->skb);
1437                                         
1438                         /* send packet if there is one */
1439                         if (lp->txready)
1440                         {
1441                                 arcnet_go_tx(dev);
1442                                 didsomething++;
1443                         }
1444                         
1445                         if (lp->intx)
1446                         {
1447                                 lp->in_txhandler--;
1448                                 continue;
1449                         }
1450 
1451                         if (!lp->outgoing.skb)
1452                         {
1453                                 BUGLVL(D_DURING)
1454                                         printk("arcnet: TX IRQ done: no split to continue.\n");
1455                                 
1456                                 /* inform upper layers */
1457                                 if (!lp->txready && dev->tbusy)
1458                                 {
1459                                         dev->tbusy=0;
1460                                         mark_bh(NET_BH);
1461                                 }
1462                                 
1463                                 lp->in_txhandler--;
1464                                 continue;
1465                         }
1466                         
1467                         /*lp->stats.tx_packets++;*/
1468                         
1469                         /* if more than one segment, and not all segments
1470                          * are done, then continue xmit.
1471                          */
1472                         if (out->segnum<out->numsegs)
1473                                 arcnet_continue_tx(dev);
1474                         if (lp->txready && !lp->sending)
1475                                 arcnet_go_tx(dev);
1476 
1477                         /* if segnum==numsegs, the transmission is finished;
1478                          * free the skb.
1479                          */
1480                         if (out->segnum>=out->numsegs)
1481                         {
1482                                 /* transmit completed */
1483                                 out->segnum++;
1484                                 if (out->skb)
1485                                         dev_kfree_skb(out->skb,FREE_WRITE);
1486                                 out->skb=NULL;
1487 
1488                                 /* inform upper layers */
1489                                 if (!lp->txready && dev->tbusy)
1490                                 {
1491                                         dev->tbusy=0;
1492                                         mark_bh(NET_BH);
1493                                 }
1494                         }
1495                         didsomething++;
1496                         
1497                         lp->in_txhandler--;
1498                 }
1499 #endif /* IRQ_XMIT */
1500         } while (--boguscount && didsomething);
1501 
1502         BUGLVL(D_DURING)
1503                 printk("arcnet: net_interrupt complete (status=%Xh)\n",
1504                         inb(STATUS));
1505 
1506 #ifdef IRQ_XMIT
1507         if (dev->start && lp->sending )
1508                 outb(NORXflag|TXFREEflag,INTMASK);
1509         else
1510                 outb(NORXflag,INTMASK);
1511 #endif
1512 
1513         dev->interrupt=0;
1514 }
1515 
1516 /* A packet has arrived; grab it from the buffers and possibly unsplit it.
1517  */
1518 static void
1519 arcnet_rx(struct device *dev,int recbuf)
     /* [previous][next][first][last][top][bottom][index][help] */
1520 {
1521         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1522         int ioaddr = dev->base_addr;
1523 /*      int status = inb(STATUS);*/
1524 
1525         struct sk_buff *skb;
1526 
1527         union ArcPacket *arcpacket=
1528                 (union ArcPacket *)(dev->mem_start+recbuf*512);
1529         struct ClientData *soft,*arcsoft;
1530         short length,offset;
1531         u_char pkttype,daddr,saddr;
1532 
1533         daddr=arcpacket->hardheader.destination;
1534         saddr=arcpacket->hardheader.source;
1535         
1536         /* if source is 0, it's not a "used" packet! */
1537         if (saddr==0)
1538         {
1539                 /*BUGLVL(D_DURING)*/
1540                         printk("arcnet: discarding old packet. (status=%Xh)\n",
1541                                 inb(STATUS));
1542                 lp->stats.rx_errors++;
1543                 return;
1544         }
1545         arcpacket->hardheader.source=0;
1546         
1547         if (arcpacket->hardheader.offset1) /* Normal Packet */
1548         {
1549                 offset=arcpacket->hardheader.offset1;
1550                 arcsoft=(struct ClientData *)
1551                         (&arcpacket->raw[offset-EXTRA_CLIENTDATA]);
1552                 length=256-offset+EXTRA_CLIENTDATA;
1553                 pkttype=NORMAL;
1554         }
1555         else            /* ExtendedPacket or ExceptionPacket */
1556         {
1557                 offset=arcpacket->hardheader.offset2;
1558                 arcsoft=(struct ClientData *)
1559                         (&arcpacket->raw[offset-EXTRA_CLIENTDATA]);
1560 
1561                 if (arcsoft->split_flag!=0xFF)  /* Extended Packet */
1562                 {
1563                         length=512-offset+EXTRA_CLIENTDATA;
1564                         pkttype=EXTENDED;
1565                 }
1566                 else                            /* Exception Packet */
1567                 {
1568                         /* skip over 4-byte junkola */
1569                         arcsoft=(struct ClientData *)
1570                                 ((u_char *)arcsoft + 4);
1571                         length=512-offset+EXTRA_CLIENTDATA-4;
1572                         pkttype=EXCEPTION;
1573                 }
1574         }
1575         
1576         if (!arcsoft->split_flag)               /* not split */
1577         {
1578                 struct Incoming *in=&lp->incoming[saddr];
1579 
1580                 BUGLVL(D_RX) printk("arcnet: incoming is not split (splitflag=%d)\n",
1581                         arcsoft->split_flag);
1582                         
1583         if (in->skb)    /* already assembling one! */
1584                 {
1585                         BUGLVL(D_INIT) printk("arcnet: aborting assembly (seq=%d) for unsplit packet (splitflag=%d, seq=%d)\n",
1586                                 in->sequence,arcsoft->split_flag,
1587                                 arcsoft->sequence);
1588                         kfree_skb(in->skb,FREE_WRITE);
1589                         in->skb=NULL;
1590                 }
1591                 
1592                 in->sequence=arcsoft->sequence;
1593 
1594                 skb = alloc_skb(length, GFP_ATOMIC);
1595                 if (skb == NULL) {
1596                         printk("%s: Memory squeeze, dropping packet.\n", 
1597                                 dev->name);
1598                         lp->stats.rx_dropped++;
1599                         return;
1600                 }
1601                 soft=(struct ClientData *)skb->data;
1602                 
1603                 skb->len = length;
1604                 skb->dev = dev;
1605                 
1606                 memcpy((u_char *)soft+EXTRA_CLIENTDATA,
1607                         (u_char *)arcsoft+EXTRA_CLIENTDATA,
1608                         length-EXTRA_CLIENTDATA);
1609                 soft->daddr=daddr;
1610                 soft->saddr=saddr;
1611                 
1612                 BUGLVL(D_DURING)
1613                         printk("arcnet: received packet from %02Xh to %02Xh (%d bytes, type=%d)\n",
1614                                 saddr,daddr,length,pkttype);
1615                 BUGLVL(D_RX)
1616                 {
1617                         int countx,county;
1618                         
1619                         printk("arcnet: packet dump [rx-unsplit] follows:");
1620 
1621                         for (county=0; county<16+(pkttype!=NORMAL)*16; county++)
1622                         {
1623                                 printk("\n[%04X] ",county*16);
1624                                 for (countx=0; countx<16; countx++)
1625                                         printk("%02X ",
1626                                                 arcpacket->raw[county*16+countx]);
1627                         }
1628                         
1629                         printk("\n");
1630                 }
1631 
1632                 /* ARP packets have problems when sent from DOS.
1633                  * source address is always 0!  So we take the hardware
1634                  * source addr (which is impossible to fumble) and insert
1635                  * it ourselves.
1636                  */
1637                 if (soft->protocol_id == ARC_P_ARP)
1638                 {
1639                         struct arphdr *arp=(struct arphdr *)
1640                                         ((char *)soft+sizeof(struct ClientData));
1641 
1642                         /* make sure addresses are the right length */
1643                         if (arp->ar_hln==1 && arp->ar_pln==4)
1644                         {
1645                                 char *cptr=(char *)(arp)+sizeof(struct arphdr);
1646                                 
1647                                 if (!*cptr)     /* is saddr = 00? */
1648                                 {
1649                                         BUGLVL(D_DURING)
1650                                                 printk("arcnet: ARP source address was 00h, set to %02Xh.\n",
1651                                                         saddr);
1652                                         *cptr=saddr;
1653                                 }
1654                                 else BUGLVL(D_DURING) 
1655                                 {
1656                                         printk("arcnet: ARP source address (%Xh) is fine.\n",
1657                                                 *cptr);
1658                                 }
1659                         }
1660                         else
1661                         {
1662                                 printk("arcnet: funny-shaped ARP packet. (%Xh, %Xh)\n",
1663                                         arp->ar_hln,arp->ar_pln);
1664                         }
1665                 }
1666                 BUGLVL( D_DATA ) {
1667                     short i;
1668                         for( i=0; i< skb->len; i++)
1669                         {
1670                                 if( i%16 == 0 ) printk("\n[%04hX] ",i);
1671                                 printk("%02hX ",((unsigned char*)skb->data)[i]);
1672                         }
1673                         printk("\n");
1674                 }
1675                 netif_rx(skb);
1676                 lp->stats.rx_packets++;
1677          }
1678          else                   /* split packet */
1679          {
1680                  /* NOTE:  MSDOS ARP packet correction should only need to
1681                   * apply to unsplit packets, since ARP packets are so short.
1682                   *
1683                   * My interpretation of the RFC1201 (ARCnet) document is that
1684                   * if a packet is received out of order, the entire assembly
1685                   * process should be aborted.
1686                   *
1687                   * The RFC also mentions "it is possible for successfully
1688                   * received packets to be retransmitted."  As of 0.40 all
1689                   * previously received packets are allowed, not just the
1690                   * most recent one.
1691                   *
1692                   * We allow multiple assembly processes, one for each
1693                   * ARCnet card possible on the network.  Seems rather like
1694                   * a waste of memory.  Necessary?
1695                   */
1696                   
1697                 struct Incoming *in=&lp->incoming[saddr];
1698                 
1699                 BUGLVL(D_RX) printk("arcnet: packet is split (splitflag=%d, seq=%d)\n",
1700                         arcsoft->split_flag,in->sequence);
1701 
1702                 if (in->skb && in->sequence!=arcsoft->sequence)
1703                 {
1704                         BUGLVL(D_INIT) printk("arcnet: wrong seq number, aborting assembly (expected=%d, seq=%d, splitflag=%d)\n",      
1705                                 in->sequence,arcsoft->sequence,
1706                                 arcsoft->split_flag);
1707                         kfree_skb(in->skb,FREE_WRITE);
1708                         in->skb=NULL;
1709                         in->lastpacket=in->numpackets=0;
1710                 }
1711                   
1712                 if (arcsoft->split_flag & 1)    /* first packet in split */
1713                 {
1714                         BUGLVL(D_RX) printk("arcnet: brand new splitpacket (splitflag=%d)\n",
1715                                 arcsoft->split_flag);
1716                         if (in->skb)    /* already assembling one! */
1717                         {
1718                                 BUGLVL(D_INIT) printk("arcnet: aborting previous (seq=%d) assembly (splitflag=%d, seq=%d)\n",
1719                                         in->sequence,arcsoft->split_flag,
1720                                         arcsoft->sequence);
1721                                 kfree_skb(in->skb,FREE_WRITE);
1722                         }
1723 
1724                         in->sequence=arcsoft->sequence;
1725                         in->numpackets=((unsigned)arcsoft->split_flag>>1)+2;
1726                         in->lastpacket=1;
1727                         
1728                         if (in->numpackets>16)
1729                         {
1730                                 printk("arcnet: incoming packet more than 16 segments; dropping. (splitflag=%d)\n",
1731                                         arcsoft->split_flag);
1732                                 lp->stats.rx_dropped++;
1733                                 return;
1734                         }
1735                 
1736                         in->skb=skb=alloc_skb(508*in->numpackets
1737                                         + sizeof(struct ClientData),
1738                                         GFP_ATOMIC);
1739                         if (skb == NULL) {
1740                                 printk("%s: (split) memory squeeze, dropping packet.\n", 
1741                                         dev->name);
1742                                 lp->stats.rx_dropped++;
1743                                 return;
1744                         }
1745                                         
1746                         /* I don't know what this is for, but it DOES avoid
1747                          * warnings...
1748                          */
1749                         skb->free=1;
1750                         
1751                         soft=(struct ClientData *)skb->data;
1752                         
1753                         skb->len=sizeof(struct ClientData);
1754                         skb->dev=dev;
1755 
1756                         memcpy((u_char *)soft+EXTRA_CLIENTDATA,
1757                                 (u_char *)arcsoft+EXTRA_CLIENTDATA,
1758                                 sizeof(struct ClientData)-EXTRA_CLIENTDATA);
1759                         soft->split_flag=0; /* final packet won't be split */
1760                 }
1761                 else                    /* not first packet */
1762                 {
1763                         int packetnum=((unsigned)arcsoft->split_flag>>1) + 1;
1764 
1765                         /* if we're not assembling, there's no point
1766                          * trying to continue.
1767                          */                     
1768                         if (!in->skb)
1769                         {
1770                                 BUGLVL(D_INIT) printk("arcnet: can't continue split without starting first! (splitflag=%d, seq=%d)\n",
1771                                         arcsoft->split_flag,arcsoft->sequence);
1772                                 return;
1773                         }
1774 
1775                         in->lastpacket++;
1776                         if (packetnum!=in->lastpacket) /* not the right flag! */
1777                         {
1778                                 /* harmless duplicate? ignore. */
1779                                 if (packetnum<=in->lastpacket-1)
1780                                 {
1781                                         BUGLVL(D_INIT) printk("arcnet: duplicate splitpacket ignored! (splitflag=%d)\n",
1782                                                 arcsoft->split_flag);
1783                                         return;
1784                                 }
1785                                 
1786                                 /* "bad" duplicate, kill reassembly */
1787                                 BUGLVL(D_INIT) printk("arcnet: out-of-order splitpacket, reassembly (seq=%d) aborted (splitflag=%d, seq=%d)\n", 
1788                                         in->sequence,arcsoft->split_flag,
1789                                         arcsoft->sequence);
1790                                 kfree_skb(in->skb,FREE_WRITE);
1791                                 in->skb=NULL;
1792                                 in->lastpacket=in->numpackets=0;
1793                                 return;
1794                         }
1795 
1796                         soft=(struct ClientData *)in->skb->data;
1797                 }
1798                 
1799                 skb=in->skb;
1800                 
1801                 memcpy(skb->data+skb->len,
1802                        (u_char *)arcsoft+sizeof(struct ClientData),
1803                        length-sizeof(struct ClientData));
1804 
1805                 skb->len+=length-sizeof(struct ClientData);
1806                 
1807                 soft->daddr=daddr;
1808                 soft->saddr=saddr;
1809                 
1810                 BUGLVL(D_DURING)
1811                         printk("arcnet: received packet from %02Xh to %02Xh (%d bytes, type=%d)\n",
1812                                 saddr,daddr,length,pkttype);
1813                 BUGLVL(D_RX)
1814                 {
1815                         int countx,county;
1816                         
1817                         printk("arcnet: packet dump [rx-split] follows:");
1818 
1819                         for (county=0; county<16+(pkttype!=NORMAL)*16; county++)
1820                         {
1821                                 printk("\n[%04X] ",county*16);
1822                                 for (countx=0; countx<16; countx++)
1823                                         printk("%02X ",
1824                                                 arcpacket->raw[county*16+countx]);
1825                         }
1826                         
1827                         printk("\n");
1828                 }
1829                 
1830                 /* are we done? */
1831                 if (in->lastpacket == in->numpackets)
1832                 {
1833                         if (!skb || !in->skb)
1834                                 printk("arcnet: ?!? done reassembling packet, no skb? (skb=%ph, in->skb=%ph)\n",
1835                                         skb,in->skb);
1836                         in->skb=NULL;
1837                         in->lastpacket=in->numpackets=0;
1838                         BUGLVL( D_DATA ) {
1839                             short i;
1840                                 for( i=0; i< skb->len; i++)
1841                                 {
1842                                         if( i%16 == 0 ) printk("\n[%04hX] ",i);
1843                                         printk("%02hX ",((unsigned char*)skb->data)[i]);
1844                                 }
1845                                 printk("\n");
1846                         }
1847                         netif_rx(skb);
1848                         lp->stats.rx_packets++;
1849                 }
1850          }
1851         
1852         /* If any worth-while packets have been received, dev_rint()
1853            has done a mark_bh(NET_BH) for us and will work on them
1854            when we get to the bottom-half routine. */
1855         /* arcnet: pardon? */
1856 }
1857 
1858 
1859 #ifdef USE_TIMER_HANDLER
1860 /* this function is called every once in a while to make sure the ARCnet
1861  * isn't stuck.
1862  *
1863  * If we miss a receive IRQ, the receiver (and IRQ) is permanently disabled
1864  * and we might never receive a packet again!  This will check if this
1865  * is the case, and if so, re-enable the receiver.
1866  */
1867 static void
1868 arcnet_timer(unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1869 {
1870         struct device *dev=(struct device *)arg;
1871         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1872         short ioaddr=dev->base_addr;
1873         int status=inb(STATUS);
1874 
1875         /* if we didn't interrupt the IRQ handler, and RX's are still
1876          * disabled, and we're not resetting the card... then we're stuck!
1877          */
1878         if (!dev->interrupt && dev->start
1879           && status&NORXflag && !status&RESETflag)
1880         {
1881                 BUGLVL(D_INIT)
1882                         printk("arcnet: timer: ARCnet was stuck!  (status=%Xh)\n",
1883                                 status);
1884                 
1885                 arcnet_inthandler(dev);
1886         }
1887 
1888         /* requeue ourselves */
1889         init_timer(&lp->timer);
1890         lp->timer.expires=TIMERval;
1891         add_timer(&lp->timer);
1892 }
1893 #endif
1894 
1895 /* Get the current statistics.  This may be called with the card open or
1896    closed. */
1897 static struct enet_statistics *
1898 arcnet_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1899 {
1900         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1901 /*      short ioaddr = dev->base_addr;*/
1902 
1903         return &lp->stats;
1904 }
1905 
1906 /* Set or clear the multicast filter for this adaptor.
1907    num_addrs == -1      Promiscuous mode, receive all packets
1908    num_addrs == 0       Normal mode, clear multicast list
1909    num_addrs > 0        Multicast mode, receive normal and MC packets, and do
1910                         best-effort filtering.
1911  */
1912 static void
1913 set_multicast_list(struct device *dev, int num_addrs, void *addrs)
     /* [previous][next][first][last][top][bottom][index][help] */
1914 {
1915 #if 0     /* no promiscuous mode at all */
1916         struct arcnet_local *lp=(struct arcnet_local *)(dev->priv);
1917 
1918         short ioaddr = dev->base_addr;
1919         if (num_addrs) {
1920                 outw(69, ioaddr);               /* Enable promiscuous mode */
1921         } else
1922                 outw(99, ioaddr);               /* Disable promiscuous mode, use normal mode */
1923 #endif
1924 }
1925 
1926 int arcnet_reset(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1927 {
1928         struct arcnet_local *lp=(struct arcnet_local *)dev->priv;
1929         short ioaddr=dev->base_addr;
1930         int delayval,recbuf=lp->recbuf;
1931         
1932         outb(0,INTMASK);        /* no IRQ's, please! */
1933         
1934         BUGLVL(D_INIT)
1935                 printk("arcnet: Resetting %s (status=%Xh)\n",
1936                         dev->name,inb(STATUS));
1937 
1938         inb(RESET);             /* Reset by reading this port */
1939         JIFFER(RESETtime);
1940 
1941         outb(CFLAGScmd|RESETclear, COMMAND); /* clear flags & end reset */
1942         outb(CFLAGScmd|CONFIGclear,COMMAND);
1943 
1944         /* after a reset, the first byte of shared mem is TESTvalue and the
1945          * second byte is our 8-bit ARCnet address
1946          */
1947         {
1948                 u_char *cardmem = (u_char *) dev->mem_start;
1949                 if (cardmem[0] != TESTvalue)
1950                 {
1951                         BUGLVL(D_INIT)
1952                                 printk("arcnet: reset failed: TESTvalue not present.\n");
1953                         return 1;
1954                 }
1955                 lp->arcnum=cardmem[1];  /* save address for later use */
1956         }
1957         
1958         /* clear out status variables */
1959         recbuf=lp->recbuf=0;
1960         lp->txbuf=2;
1961         /*dev->tbusy=0;*/
1962 
1963         /* enable extended (512-byte) packets */
1964         outb(CONFIGcmd|EXTconf,COMMAND);
1965         XJIFFER(ACKtime);
1966         
1967         /* clean out all the memory to make debugging make more sense :) */
1968         BUGLVL(D_DURING)
1969                 memset((void *)dev->mem_start,0x42,2048);
1970         
1971         /* and enable receive of our first packet to the first buffer */
1972         EnableReceiver();
1973 
1974         /* re-enable interrupts */
1975         outb(NORXflag,INTMASK);
1976         
1977         /* done!  return success. */
1978         return 0;
1979 }
1980 
1981 
1982 /*
1983  *       Create the ARCnet ClientData header for an arbitrary protocol layer
1984  *
1985  *      saddr=NULL      means use device source address (always will anyway)
1986  *      daddr=NULL      means leave destination address (eg unresolved arp)
1987  */
1988 int arc_header(unsigned char *buff,struct device *dev,unsigned short type,
     /* [previous][next][first][last][top][bottom][index][help] */
1989                 void *daddr,void *saddr,unsigned len,struct sk_buff *skb)
1990 {
1991         struct ClientData *head = (struct ClientData *)buff;
1992         struct arcnet_local *lp=(struct arcnet_local *)(dev->priv);
1993 
1994         /* set the protocol ID according to RFC-1201 */
1995         switch(type)
1996         {
1997         case ETH_P_IP:
1998                 head->protocol_id=ARC_P_IP;
1999                 break;
2000         case ETH_P_ARP:
2001                 head->protocol_id=ARC_P_ARP;
2002                 break;
2003         case ETH_P_RARP:
2004                 head->protocol_id=ARC_P_RARP;
2005                 break;
2006         case ETH_P_IPX:
2007                 head->protocol_id=ARC_P_IPX;
2008                 break;
2009         case ETH_P_ATALK:
2010                 head->protocol_id=ARC_P_ATALK;
2011                 break;
2012         default:
2013                 printk("arcnet: I don't understand protocol %d (%Xh)\n",
2014                         type,type);
2015                 return 0;
2016         }       
2017 
2018 #if 1
2019         /*
2020          *      Set the source hardware address.
2021          *      AVE: we can't do this, so we don't.  Code below is directly
2022          *           stolen from eth.c driver and won't work.
2023          ** TM: but for debugging I would like to have saddr in the header
2024          */
2025         if(saddr)
2026                 head->saddr=((u_char*)saddr)[0];
2027         else
2028                 head->saddr=((u_char*)(dev->dev_addr))[0];
2029 #endif
2030 
2031 #if 0
2032         /*
2033          *      Anyway, the loopback-device should never use this function... 
2034          *
2035          *      And the chances of it using the ARCnet version of it are so
2036          *      tiny that I don't think we have to worry :)
2037          */
2038         if (dev->flags & IFF_LOOPBACK) 
2039         {
2040                 head->daddr=0;
2041                 return(dev->hard_header_len);
2042         }
2043 #endif
2044 
2045         head->split_flag=0;     /* split packets are done elsewhere */
2046         head->sequence=(lp->sequence++);
2047 
2048         /* supposedly if daddr is NULL, we should ignore it... */
2049         if(daddr)
2050         {
2051                 head->daddr=((u_char*)daddr)[0];
2052                 return dev->hard_header_len;
2053         }
2054         else
2055                 head->daddr=0;  /* better fill one in anyway */
2056                 
2057         return -dev->hard_header_len;
2058 }
2059 
2060 
2061 /*
2062  *      Rebuild the ARCnet ClientData header. This is called after an ARP
2063  *      (or in future other address resolution) has completed on this
2064  *      sk_buff. We now let ARP fill in the other fields.
2065  */
2066 int arc_rebuild_header(void *buff,struct device *dev,unsigned long dst,
     /* [previous][next][first][last][top][bottom][index][help] */
2067                 struct sk_buff *skb)
2068 {
2069         struct ClientData *head = (struct ClientData *)buff;
2070 
2071         /*
2072          *      Only ARP/IP is currently supported
2073          */
2074          
2075         if(head->protocol_id != ARC_P_IP) 
2076         {
2077                 printk("arcnet: I don't understand resolve type %d (%Xh) addresses!\n",
2078                         head->protocol_id,head->protocol_id);
2079                 head->daddr=0;
2080                 /*memcpy(eth->h_source, dev->dev_addr, dev->addr_len);*/
2081                 return 0;
2082         }
2083 
2084         /*
2085          *      Try and get ARP to resolve the header.
2086          */
2087 #ifdef CONFIG_INET       
2088         return arp_find(&(head->daddr), dst, dev, dev->pa_addr, skb)? 1 : 0;
2089 #else
2090         return 0;       
2091 #endif  
2092 }
2093                 
2094 /*
2095  *      Determine the packet's protocol ID.
2096  *
2097  *      With ARCnet we have to convert everything to Ethernet-style stuff.
2098  */
2099 unsigned short arc_type_trans(struct sk_buff *skb,struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
2100 {
2101         struct ClientData *head = (struct ClientData *) skb->data;
2102         /*unsigned char *rawp;*/
2103         
2104         if (head->daddr==0)
2105                 skb->pkt_type=PACKET_BROADCAST;
2106                 
2107 #if 0 /* code for ethernet with multicast */
2108         if(*eth->h_dest&1)
2109         {
2110                 if(memcmp(eth->h_dest,dev->broadcast, ETH_ALEN)==0)
2111                         skb->pkt_type=PACKET_BROADCAST;
2112                 else
2113                         skb->pkt_type=PACKET_MULTICAST;
2114         }
2115 #endif
2116         
2117         if(dev->flags&IFF_PROMISC)
2118         {
2119                 /* if we're not sending to ourselves :) */
2120                 if (head->daddr != dev->dev_addr[0])
2121                         skb->pkt_type=PACKET_OTHERHOST;
2122         }
2123         
2124         /* now return the protocol number */
2125         switch (head->protocol_id)
2126         {
2127         case ARC_P_IP:          return htons(ETH_P_IP);
2128         case ARC_P_ARP:         return htons(ETH_P_ARP);
2129         case ARC_P_RARP:        return htons(ETH_P_RARP);
2130         case ARC_P_IPX:         return htons(ETH_P_IPX);
2131         case ARC_P_ATALK:   return htons(ETH_P_ATALK); /* appletalk, not tested */
2132         case ARC_P_LANSOFT: /* don't understand.  fall through. */
2133         default:
2134                 BUGLVL(D_DURING)
2135                         printk("arcnet: received packet of unknown protocol id %d (%Xh)\n",
2136                                 head->protocol_id,head->protocol_id);
2137                 return 0;
2138         }
2139 
2140 #if 0 /* more ethernet-specific junk */
2141         if (ntohs(eth->h_proto) >= 1536)
2142                 return eth->h_proto;
2143                 
2144         rawp = (unsigned char *)(eth + 1);
2145         
2146         if (*(unsigned short *)rawp == 0xFFFF)
2147                 return htons(ETH_P_802_3);
2148         if (*(unsigned short *)rawp == 0xAAAA)
2149                 return htons(ETH_P_SNAP);
2150                 
2151         return htons(ETH_P_802_2);
2152 #endif
2153 
2154         return htons(ETH_P_IP);
2155 }
2156 
2157 #ifdef MODULE
2158 char kernel_version[] = UTS_RELEASE;
2159 static struct device thisARCnet = {
2160   "      ",/* if blank, device name inserted by /linux/drivers/net/net_init.c */
2161   0, 0, 0, 0,
2162   0, 0,  /* I/O address, IRQ */
2163   0, 0, 0, NULL, arcnet_probe };
2164         
2165         
2166 int io=0x0;     /* <--- EDIT THESE LINES FOR YOUR CONFIGURATION */
2167 int irqnum=0;   /* or use the insmod io= irq= shmem= options */
2168 int shmem=0;
2169 int num=0;      /* number of device (ie for arc0, arc1, arc2...) */
2170 
2171 int
2172 init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2173 {
2174         sprintf(thisARCnet.name,"arc%d",num);
2175 
2176         thisARCnet.base_addr=io;
2177 
2178         thisARCnet.irq=irqnum;
2179         if (thisARCnet.irq==2) thisARCnet.irq=9;
2180 
2181         if (shmem)
2182         {
2183                 thisARCnet.mem_start=shmem;
2184                 thisARCnet.mem_end=thisARCnet.mem_start+512*4-1;
2185                 thisARCnet.rmem_start=thisARCnet.mem_start+512*0;
2186                 thisARCnet.rmem_end=thisARCnet.mem_start+512*2-1;
2187         }
2188 
2189         if (register_netdev(&thisARCnet) != 0)
2190                 return -EIO;
2191         return 0;
2192 }
2193 
2194 void
2195 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2196 {
2197   if (MOD_IN_USE) {
2198     printk("%s: device busy, remove delayed\n",thisARCnet.name);
2199   } else {
2200     if (thisARCnet.start) arcnet_close(&thisARCnet);
2201     if (thisARCnet.irq) free_irq(thisARCnet.irq);
2202     if (thisARCnet.base_addr) release_region(thisARCnet.base_addr,
2203                                                 ETHERCARD_TOTAL_SIZE);
2204     unregister_netdev(&thisARCnet);
2205   }
2206 }
2207 
2208 #endif /* MODULE */
2209 
2210 
2211 
2212 /*
2213  * Local variables:
2214  *  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c skeleton.c"
2215  *  version-control: t
2216  *  kept-new-versions: 5
2217  *  tab-width: 4
2218  * End:
2219  */
2220 

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