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 <net/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 
 584         return 0;
 585 }
 586 
 587 #ifndef MODULE
 588 
 589 int arcnet_ioprobe(struct device *dev, short ioaddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 590 {
 591         int delayval,airq;
 592 
 593         BUGLVL(D_INIT)
 594                 printk("arcnet: probing address %Xh\n",ioaddr);
 595 
 596         BUGLVL(D_INIT)
 597                 printk("arcnet:  status1=%Xh\n",inb(STATUS));
 598 
 599                 
 600         /* very simple - all we have to do is reset the card, and if there's
 601          * no irq, it's not an ARCnet.  We can also kill two birds with
 602          * one stone because we detect the IRQ at the same time :)
 603          */
 604          
 605         /* reset the card by reading the reset port */
 606         inb(RESET);
 607         JIFFER(RESETtime);
 608 
 609         /* if status port is FF, there's certainly no arcnet... give up. */
 610         if (inb(STATUS)==0xFF)
 611         {
 612                 BUGLVL(D_INIT)
 613                         printk("arcnet:  probe failed.  Status port empty.\n");
 614                 return ENODEV;
 615         }
 616 
 617         /* we'll try to be reasonably sure it's an arcnet by making sure
 618          * the value of the COMMAND port changes automatically once in a
 619          * while.  I have no idea what those values ARE, but at least
 620          * they work.
 621          */
 622         {
 623                 int initval,curval;
 624                 
 625                 curval=initval=inb(COMMAND);
 626                 delayval=jiffies+5;
 627                 while (delayval>=jiffies && curval==initval)
 628                         curval=inb(COMMAND);
 629                         
 630                 if (curval==initval)
 631                 {
 632                         printk("arcnet:  probe failed.  never-changing command port (%02Xh).\n",
 633                                 initval);
 634                         return ENODEV;
 635                 }
 636         }
 637 
 638         BUGLVL(D_INIT)
 639                 printk("arcnet:  status2=%Xh\n",inb(STATUS));
 640 
 641         /* now we turn the reset bit off so we can IRQ next reset... */
 642         outb(CFLAGScmd|RESETclear|CONFIGclear,COMMAND);
 643         XJIFFER(ACKtime);
 644         if (inb(STATUS) & RESETflag) /* reset flag STILL on */
 645         {
 646                 BUGLVL(D_INIT)
 647                         printk("arcnet:  probe failed.  eternal reset flag1...(status=%Xh)\n",
 648                                 inb(STATUS));
 649                 return ENODEV;
 650         }
 651 
 652         /* set up automatic IRQ detection */
 653         autoirq_setup(0);
 654         
 655         /* enable reset IRQ's (shouldn't be necessary, but worth a try) */
 656         outb(RESETflag,INTMASK);
 657 
 658         /* now reset it again to generate an IRQ */
 659         inb(RESET);
 660         JIFFER(RESETtime);
 661 
 662         BUGLVL(D_INIT)
 663                 printk("arcnet:  status3=%Xh\n",inb(STATUS));
 664 
 665         /* and turn the reset flag back off */
 666         outb(CFLAGScmd|RESETclear|CONFIGclear,COMMAND);
 667         XJIFFER(ACKtime);
 668 
 669         BUGLVL(D_INIT)
 670                 printk("arcnet:  status4=%Xh\n",inb(STATUS));
 671 
 672         /* enable reset IRQ's again */
 673         outb(RESETflag,INTMASK);
 674 
 675         /* now reset it again to generate an IRQ */
 676         inb(RESET);
 677         JIFFER(RESETtime);
 678         
 679         BUGLVL(D_INIT)
 680                 printk("arcnet:  status5=%Xh\n",inb(STATUS));
 681 
 682         /* if we do this, we're sure to get an IRQ since the card has
 683          * just reset and the NORXflag is on until we tell it to start
 684          * receiving.
 685          *
 686          * However, this could, theoretically, cause a lockup.  Maybe I'm just
 687          * not very good at theory! :)
 688          */
 689 #ifdef DANGER_PROBE
 690         outb(NORXflag,INTMASK);
 691         JIFFER(RESETtime);
 692         outb(0,INTMASK);
 693 #endif
 694 
 695         /* and turn the reset flag back off */
 696         outb(CFLAGScmd|RESETclear|CONFIGclear,COMMAND);
 697         XJIFFER(ACKtime);
 698 
 699         airq = autoirq_report(0);
 700         if (net_debug>=D_INIT && airq)
 701                 printk("arcnet:  autoirq is %d\n", airq);
 702 
 703         /* if there was no autoirq AND the user hasn't set any defaults,
 704          * give up.
 705          */
 706         if (!airq && !(dev->base_addr && dev->irq))
 707         {
 708                 BUGLVL(D_INIT)
 709                         printk("arcnet:  probe failed.  no autoirq...\n");
 710                 return ENODEV;
 711         }
 712 
 713         /* otherwise we probably have a card.  Let's make sure. */
 714         
 715         if (inb(STATUS) & RESETflag) /* reset flag on */
 716         {
 717                 /* now we turn the reset bit off */
 718                 outb(CFLAGScmd|RESETclear|CONFIGclear,COMMAND);
 719                 XJIFFER(ACKtime);
 720         }
 721         
 722         if (inb(STATUS) & RESETflag) /* reset flag STILL on */
 723         {
 724                 BUGLVL(D_INIT)
 725                         printk("arcnet:  probe failed.  eternal reset flag...(status=%Xh)\n",
 726                                 inb(STATUS));
 727                 return ENODEV;
 728         }
 729         
 730         /* okay, we've got a real, live ARCnet on our hands. */
 731         if (!dev->base_addr) dev->base_addr=ioaddr;
 732         
 733         if (dev->irq < 2)               /* "Auto-IRQ" */
 734         {
 735                 /* we already did the autoirq above, so store the values */
 736                 dev->irq=airq;
 737         }
 738         else if (dev->irq == 2)
 739         {
 740                 if (net_debug)
 741                         printk("arcnet: IRQ2 == IRQ9, don't worry.\n");
 742                 dev->irq = 9;
 743         }
 744 
 745         BUGLVL(D_INIT)
 746                 printk("arcnet: irq and base address seem okay. (%lXh, IRQ %d)\n",
 747                         dev->base_addr,dev->irq);
 748         return 0;
 749 }
 750 
 751 
 752 /* A memory probe that is called after the card is reset.
 753  * It checks for the official TESTvalue in byte 0 and makes sure the buffer
 754  * has certain characteristics of an ARCnet...
 755  */
 756 int arcnet_memprobe(struct device *dev,u_char *addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 757 {
 758         BUGLVL(D_INIT)
 759                 printk("arcnet: probing memory at %lXh\n",(u_long)addr);
 760                 
 761         dev->mem_start=0;
 762 
 763 #ifdef STRICT_MEM_DETECT /* probably better. */
 764         /* ARCnet memory byte 0 is TESTvalue */
 765         if (addr[0]!=TESTvalue)
 766         {
 767                 BUGLVL(D_INIT)
 768                         printk("arcnet:  probe failed.  addr=%lXh, addr[0]=%Xh (not %Xh)\n",
 769                                 (unsigned long)addr,addr[0],TESTvalue);
 770                 return ENODEV;
 771         }
 772         
 773         /* now verify the shared memory writability */
 774         addr[0]=0x42;
 775         if (addr[0]!=0x42)
 776         {
 777                 BUGLVL(D_INIT)
 778                         printk("arcnet:  probe failed.  addr=%lXh, addr[0]=%Xh (not 42h)\n",
 779                                 (unsigned long)addr,addr[0]);
 780                 return ENODEV;
 781         }
 782 #else
 783         if (addr[0]!=TESTvalue)
 784         {
 785                 BUGLVL(D_INIT)
 786                         printk("arcnet:  probe failed.  addr=%lXh, addr[0]=%Xh (not %Xh)\n",
 787                                 (unsigned long)addr,addr[0],TESTvalue);
 788                 return ENODEV;
 789         }
 790 #endif
 791 
 792         /* got it!  fill in dev */
 793         dev->mem_start=(unsigned long)addr;
 794         dev->mem_end=dev->mem_start+512*4-1;
 795         dev->rmem_start=dev->mem_start+512*0;
 796         dev->rmem_end=dev->mem_start+512*2-1;
 797 
 798         return 0;
 799 }
 800 
 801 #endif /* MODULE */
 802 
 803 
 804 /* Open/initialize the board.  This is called (in the current kernel)
 805    sometime after booting when the 'ifconfig' program is run.
 806 
 807    This routine should set everything up anew at each open, even
 808    registers that "should" only need to be set once at boot, so that
 809    there is non-reboot way to recover if something goes wrong.
 810    */
 811 static int
 812 arcnet_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 813 {
 814         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
 815 /*      int ioaddr = dev->base_addr;*/
 816 
 817         if (dev->metric>=10)
 818         {
 819                 net_debug=dev->metric-10;
 820                 dev->metric=1;
 821         }
 822 
 823         if (net_debug) printk(version);
 824 
 825 #if 0   /* Yup, they're hardwired in arcnets */
 826         /* This is used if the interrupt line can turned off (shared).
 827            See 3c503.c for an example of selecting the IRQ at config-time. */
 828         if (request_irq(dev->irq, &arcnet_interrupt, 0, "arcnet")) {
 829                 return -EAGAIN;
 830         }
 831 #endif
 832 
 833         irq2dev_map[dev->irq] = dev;
 834 
 835         /* Reset the hardware here. */
 836         BUGLVL(D_EXTRA) printk("arcnet: arcnet_open: resetting card.\n");
 837         
 838         /* try to reset - twice if it fails the first time */
 839         if (arcnet_reset(dev) && arcnet_reset(dev))
 840                 return -ENODEV;
 841         
 842 /*      chipset_init(dev, 1);*/
 843 /*      outb(0x00, ioaddr);*/
 844 
 845 /*      lp->open_time = jiffies;*/
 846 
 847         dev->tbusy=0;
 848         dev->interrupt=0;
 849         dev->start=1;
 850         lp->intx=0;
 851         lp->in_txhandler=0;
 852 
 853 #ifdef USE_TIMER_HANDLER
 854         /* grab a timer handler to recover from any missed IRQ's */
 855         init_timer(&lp->timer);
 856         lp->timer.expires = TIMERval;         /* length of time */
 857         lp->timer.data = (unsigned long)dev;  /* pointer to "dev" structure */
 858         lp->timer.function = &arcnet_timer;    /* timer handler */
 859         add_timer(&lp->timer);
 860 #endif
 861 
 862 #ifdef MODULE
 863         MOD_INC_USE_COUNT;
 864 #endif
 865                                         
 866         return 0;
 867 }
 868 
 869 
 870 /* The inverse routine to arcnet_open(). */
 871 static int
 872 arcnet_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 873 {
 874         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
 875         int ioaddr = dev->base_addr;
 876 #ifdef EXTRA_DELAYS
 877         int delayval;
 878 #endif
 879 
 880 /*      lp->open_time = 0;*/
 881 
 882         dev->tbusy = 1;
 883         dev->start = 0;
 884         
 885         /* release the timer */
 886         del_timer(&lp->timer);
 887 
 888         /* Flush the Tx and disable Rx here.     */
 889         /* resetting the card should do the job. */
 890         /*inb(RESET);*/
 891 
 892         outb(0,INTMASK);        /* no IRQ's */
 893         outb(NOTXcmd,COMMAND);  /* disable transmit */
 894         XJIFFER(ACKtime);
 895         outb(NORXcmd,COMMAND);  /* disable receive */
 896 
 897         /* Update the statistics here. */
 898         
 899 #ifdef MODULE
 900         MOD_DEC_USE_COUNT;
 901 #endif
 902 
 903         return 0;
 904 }
 905 
 906 
 907 static int
 908 arcnet_send_packet(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 909 {
 910         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
 911         int ioaddr=dev->base_addr;
 912 /*      short daddr;*/
 913 
 914         lp->intx++;
 915 
 916         BUGLVL(D_DURING)
 917                 printk("arcnet: transmit requested (status=%Xh, inTX=%d)\n",
 918                         inb(STATUS),lp->intx);
 919 
 920         if (dev->tbusy || lp->in_txhandler)
 921         {
 922                 /* If we get here, some higher level has decided we are broken.
 923                    There should really be a "kick me" function call instead. */
 924                 int tickssofar = jiffies - dev->trans_start;
 925                 int recbuf=lp->recbuf;
 926                 int status=inb(STATUS);
 927                 
 928                 /* resume any stopped tx's */
 929 #if 0
 930                 if (lp->txready && (inb(STATUS)&TXFREEflag))
 931                 {
 932                         printk("arcnet: kickme: starting a TX (status=%Xh)\n",
 933                                 inb(STATUS));
 934                         arcnet_go_tx(dev);
 935                         lp->intx--;
 936                         return 1;
 937                 }
 938 #endif
 939                 
 940                 if (tickssofar < 5) 
 941                 {
 942                         BUGLVL(D_DURING)
 943                                 printk("arcnet: premature kickme! (status=%Xh ticks=%d o.skb=%ph numsegs=%d segnum=%d\n",
 944                                         status,tickssofar,lp->outgoing.skb,
 945                                         lp->outgoing.numsegs,
 946                                         lp->outgoing.segnum);
 947                         lp->intx--;
 948                         return 1;
 949                 }
 950 
 951                 BUGLVL(D_INIT)
 952                         printk("arcnet: transmit timed out (status=%Xh, inTX=%d, tickssofar=%d)\n",
 953                                 status,lp->intx,tickssofar);
 954 
 955                 /* Try to restart the adaptor. */
 956                 /*arcnet_reset(dev);*/
 957                 
 958                 if (status&NORXflag) EnableReceiver();
 959                 if (!(status&TXFREEflag)) outb(NOTXcmd,COMMAND);
 960                 dev->trans_start = jiffies;
 961 
 962                 if (lp->outgoing.skb)
 963                         dev_kfree_skb(lp->outgoing.skb,FREE_WRITE);
 964                 lp->outgoing.skb=NULL;
 965 
 966                 dev->tbusy=0;
 967                 mark_bh(NET_BH);
 968                 lp->intx=0;
 969                 lp->in_txhandler=0;
 970                 lp->txready=0;
 971                 lp->sending=0;
 972 
 973                 return 1;
 974         }
 975 
 976         /* If some higher layer thinks we've missed a tx-done interrupt
 977            we are passed NULL. Caution: dev_tint() handles the cli()/sti()
 978            itself. */
 979         if (skb == NULL) {
 980                 BUGLVL(D_INIT)
 981                         printk("arcnet: tx passed null skb (status=%Xh, inTX=%d, tickssofar=%ld)\n",
 982                                 inb(STATUS),lp->intx,jiffies-dev->trans_start);
 983                 dev_tint(dev);
 984                 lp->intx--;
 985                 return 0;
 986         }
 987         
 988         if (lp->txready)        /* transmit already in progress! */
 989         {
 990                 printk("arcnet: trying to start new packet while busy!\n");
 991                 printk("arcnet: marking as not ready.\n");
 992                 lp->txready=0;
 993                 return 1;
 994         }
 995 
 996         /* Block a timer-based transmit from overlapping.  This could better be
 997            done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
 998         if (set_bit(0, (void*)&dev->tbusy) != 0)
 999         {
1000             printk("arcnet: transmitter called with busy bit set! (status=%Xh, inTX=%d, tickssofar=%ld)\n",
1001                         inb(STATUS),lp->intx,jiffies-dev->trans_start);
1002             lp->intx--;
1003             return -EBUSY;
1004         }
1005         else {
1006                 struct Outgoing *out=&(lp->outgoing);
1007                 out->length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
1008                 out->hdr=(struct ClientData*)skb->data;
1009                 out->skb=skb;
1010                 BUGLVL( D_DATA ) {
1011                         short i;
1012                         for( i=0; i< skb->len; i++)
1013                         {
1014                                 if( i%16 == 0 ) printk("\n[%04hX] ",i);
1015                                 printk("%02hX ",((unsigned char*)skb->data)[i]);
1016                         }
1017                         printk("\n");
1018                 }
1019 
1020 #ifdef IRQ_XMIT
1021                 if (lp->txready && inb(STATUS)&TXFREEflag)
1022                         arcnet_go_tx(dev);
1023 #endif
1024 
1025                 
1026                 if (out->length<=XMTU)  /* fits in one packet? */
1027                 {
1028                         BUGLVL(D_TX) printk("arcnet: not splitting %d-byte packet. (split_flag=%d)\n",
1029                                         out->length,out->hdr->split_flag);
1030                         BUGLVL(D_INIT) if (out->hdr->split_flag)
1031                                 printk("arcnet: short packet has split_flag set?! (split_flag=%d)\n",
1032                                         out->hdr->split_flag);
1033                         out->numsegs=1;
1034                         out->segnum=1;
1035                         arcnet_prepare_tx(dev,out->hdr,
1036                                 out->length-sizeof(struct ClientData),
1037                                 ((char *)skb->data)+sizeof(struct ClientData));
1038                         careful_xmit_wait(dev);
1039 
1040                         /* done right away */
1041                         dev_kfree_skb(out->skb,FREE_WRITE);
1042                         out->skb=NULL;
1043                                         
1044                         if (!lp->sending)
1045                         {
1046                                 arcnet_go_tx(dev);
1047                                 
1048                                 /* inform upper layers */
1049                                 dev->tbusy=0;
1050                                 mark_bh(NET_BH);
1051                         }
1052                 }
1053                 else                    /* too big for one - split it */
1054                 {
1055                         int maxsegsize=XMTU-sizeof(struct ClientData);
1056 
1057                         out->data=(u_char *)skb->data
1058                                         + sizeof(struct ClientData);
1059                         out->dataleft=out->length-sizeof(struct ClientData);
1060                         out->numsegs=(out->dataleft+maxsegsize-1)/maxsegsize;
1061                                 
1062                         out->segnum=0;
1063                                 
1064                         BUGLVL(D_TX) printk("arcnet: packet (%d bytes) split into %d fragments:\n",
1065                                 out->length,out->numsegs);
1066 
1067 #ifdef IRQ_XMIT
1068                         /* if a packet waiting, launch it */
1069                         if (lp->txready && inb(STATUS)&TXFREEflag)
1070                                 arcnet_go_tx(dev);
1071 
1072                         if (!lp->txready)
1073                         {
1074                                 /* prepare a packet, launch it and prepare
1075                                  * another.
1076                                  */
1077                                 arcnet_continue_tx(dev);
1078                                 if (!lp->sending)
1079                                 {
1080                                         arcnet_go_tx(dev);
1081                                         arcnet_continue_tx(dev);
1082                                         if (!lp->sending)
1083                                                 arcnet_go_tx(dev);
1084                                 }
1085                         }
1086                         
1087                         /* if segnum==numsegs, the transmission is finished;
1088                          * free the skb right away.
1089                          */
1090                         if (out->segnum==out->numsegs)
1091                         {
1092                                 /* transmit completed */
1093                                 out->segnum++;
1094                                 if (out->skb)
1095                                         dev_kfree_skb(out->skb,FREE_WRITE);
1096                                 out->skb=NULL;
1097 #if 0
1098                                 /* inform upper layers */
1099                                 dev->tbusy=0;
1100                                 mark_bh(NET_BH);
1101 #endif
1102                         }
1103                         
1104 #else /* non-irq xmit */
1105                         while (out->segnum<out->numsegs)
1106                         {
1107                                 arcnet_continue_tx(dev);
1108                                 careful_xmit_wait(dev);
1109                                 arcnet_go_tx(dev);
1110                                 dev->trans_start=jiffies;
1111                         }
1112 
1113                         dev_kfree_skb(out->skb,FREE_WRITE);
1114                         out->skb=NULL;
1115 
1116                         /* inform upper layers */
1117                         dev->tbusy = 0;
1118                         mark_bh(NET_BH);
1119 #endif
1120                 }
1121         }
1122 
1123         lp->intx--;
1124         lp->stats.tx_packets++;
1125         dev->trans_start=jiffies;
1126         return 0;
1127 }
1128 
1129 static void arcnet_continue_tx(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1130 {
1131         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1132         int maxsegsize=XMTU-sizeof(struct ClientData);
1133         struct Outgoing *out=&(lp->outgoing);
1134         
1135         if (lp->txready)
1136         {
1137                 printk("arcnet: continue_tx: called with packet in buffer!\n");
1138                 return;
1139         }
1140 
1141         if (out->segnum>=out->numsegs)
1142         {
1143                 printk("arcnet: continue_tx: building segment %d of %d!\n",
1144                         out->segnum+1,out->numsegs);
1145         }
1146 
1147         if (!out->segnum)       /* first packet */
1148                 out->hdr->split_flag=((out->numsegs-2)<<1)+1;
1149         else
1150                 out->hdr->split_flag=out->segnum<<1;
1151 
1152         out->seglen=maxsegsize;
1153         if (out->seglen>out->dataleft) out->seglen=out->dataleft;
1154                         
1155         BUGLVL(D_TX) printk("arcnet: building packet #%d (%d bytes) of %d (%d total), splitflag=%d\n",
1156                 out->segnum+1,out->seglen,out->numsegs,
1157                 out->length,out->hdr->split_flag);
1158 
1159         arcnet_prepare_tx(dev,out->hdr,out->seglen,out->data);
1160                 
1161         out->dataleft-=out->seglen;
1162         out->data+=out->seglen;
1163         out->segnum++;
1164 }
1165 
1166 #ifdef CAREFUL_XMIT
1167 static void careful_xmit_wait(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1168 {
1169         int ioaddr=dev->base_addr;
1170         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1171 
1172         /* wait patiently for tx to become available again */
1173         while ( !(inb(STATUS)&TXFREEflag) )
1174         {
1175                 if (jiffies-dev->trans_start > 20 || !dev->tbusy)
1176                 {
1177                         BUGLVL(D_INIT)
1178                                 printk("arcnet: CAREFUL_XMIT timeout. (busy=%d, status=%Xh)\n",
1179                                         dev->tbusy,inb(STATUS));
1180                         lp->stats.tx_errors++;
1181                         
1182                         outb(NOTXcmd,COMMAND);
1183                         return;
1184                 }
1185         }
1186         BUGLVL(D_TX) printk("arcnet: transmit completed successfully. (status=%Xh)\n",
1187                                 inb(STATUS));
1188 }
1189 #endif
1190 
1191 static void
1192 arcnet_prepare_tx(struct device *dev,struct ClientData *hdr,short length,
     /* [previous][next][first][last][top][bottom][index][help] */
1193                 char *data)
1194 {
1195 /*      int ioaddr = dev->base_addr;*/
1196         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1197         struct ClientData *arcsoft;
1198         union ArcPacket *arcpacket = 
1199                 (union ArcPacket *)(dev->mem_start+512*(lp->txbuf^1));
1200         u_char pkttype;
1201         int offset;
1202         short daddr;
1203         
1204         lp->txbuf=lp->txbuf^1;  /* XOR with 1 to alternate between 2 and 3 */
1205         
1206         length+=sizeof(struct ClientData);
1207 
1208         BUGLVL(D_TX)
1209                 printk("arcnet: arcnet_prep_tx: hdr:%ph, length:%d, data:%ph\n",
1210                         hdr,length,data);
1211 
1212         /* clean out the page to make debugging make more sense :) */
1213         BUGLVL(D_DURING)
1214                 memset((void *)dev->mem_start+lp->txbuf*512,0x42,512);
1215 
1216         daddr=arcpacket->hardheader.destination=hdr->daddr;
1217 
1218         /* load packet into shared memory */
1219         if (length<=MTU)        /* Normal (256-byte) Packet */
1220         {
1221                 pkttype=NORMAL;
1222                         
1223                 arcpacket->hardheader.offset1=offset=256-length
1224                                 + EXTRA_CLIENTDATA;
1225                 arcsoft=(struct ClientData *)
1226                         (&arcpacket->raw[offset-EXTRA_CLIENTDATA]);
1227         }
1228         else if (length>=MinTU) /* Extended (512-byte) Packet */
1229         {
1230                 pkttype=EXTENDED;
1231                 
1232                 arcpacket->hardheader.offset1=0;
1233                 arcpacket->hardheader.offset2=offset=512-length
1234                         + EXTRA_CLIENTDATA;
1235                 arcsoft=(struct ClientData *)
1236                         (&arcpacket->raw[offset-EXTRA_CLIENTDATA]);
1237         }
1238         else                            /* Exception Packet */
1239         {
1240                 pkttype=EXCEPTION;
1241                 
1242                 arcpacket->hardheader.offset1=0;
1243                 arcpacket->hardheader.offset2=offset=512-length-4
1244                         + EXTRA_CLIENTDATA;
1245                 arcsoft=(struct ClientData *)
1246                         (&arcpacket->raw[offset+4-EXTRA_CLIENTDATA]);
1247                 
1248                 /* exception-specific stuff - these four bytes
1249                  * make the packet long enough to fit in a 512-byte
1250                  * frame.
1251                  */
1252                 arcpacket->raw[offset+0]=arcsoft->protocol_id;
1253                 arcpacket->raw[offset+1]=0xFF; /* FF flag */
1254                         arcpacket->raw[offset+2]=0xFF; /* FF padding */
1255                         arcpacket->raw[offset+3]=0xFF; /* FF padding */
1256         }
1257 
1258 
1259         /* copy the packet into ARCnet shmem
1260          *  - the first bytes of ClientData header are skipped
1261          */
1262         memcpy((u_char*)arcsoft+EXTRA_CLIENTDATA,
1263                 (u_char*)hdr+EXTRA_CLIENTDATA,
1264                 sizeof(struct ClientData)-EXTRA_CLIENTDATA);
1265         memcpy((u_char*)arcsoft+sizeof(struct ClientData),
1266                 data,
1267                 length-sizeof(struct ClientData));
1268                 
1269         BUGLVL(D_DURING) printk("arcnet: transmitting packet to station %02Xh (%d bytes, type=%d)\n",
1270                         daddr,length,pkttype);
1271                         
1272         BUGLVL(D_TX)
1273         {
1274                 int countx,county;
1275                 
1276                 printk("arcnet: packet dump [tx] follows:");
1277 
1278                 for (county=0; county<16+(pkttype!=NORMAL)*16; county++)
1279                 {
1280                         printk("\n[%04X] ",county*16);
1281                         for (countx=0; countx<16; countx++)
1282                                 printk("%02X ",
1283                                         arcpacket->raw[county*16+countx]);
1284                 }
1285                 
1286                 printk("\n");
1287         }
1288 #ifdef CAREFUL_XMIT
1289  #if 0
1290         careful_xmit_wait(dev);
1291 
1292         /* if we're not broadcasting, make sure the xmit was ack'd.
1293          * if it wasn't, there is probably no card with that
1294          * address... or else it missed our tx somehow.
1295          */
1296         if (daddr && !(inb(STATUS)&TXACKflag))
1297         {       
1298                 BUGLVL(D_INIT)
1299                         printk("arcnet: transmit not acknowledged. (status=%Xh, daddr=%02Xh)\n",
1300                                 inb(STATUS),daddr);
1301                 lp->stats.tx_errors++;
1302                 return -ENONET; /* "machine is not on the network" */
1303         }
1304  #endif
1305 #endif
1306         lp->txready=lp->txbuf;  /* packet is ready for sending */
1307 
1308 #if 0
1309 #ifdef IRQ_XMIT
1310         if (inb(STATUS)&TXFREEflag) arcnet_go_tx(dev);
1311 #endif
1312 #endif
1313 }
1314 
1315 static void
1316 arcnet_go_tx(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1317 {
1318         struct arcnet_local *lp=(struct arcnet_local *)dev->priv;
1319         int ioaddr=dev->base_addr;
1320 
1321         BUGLVL(D_DURING)
1322                 printk("arcnet: go_tx: status=%Xh\n",
1323                         inb(STATUS));
1324         
1325         if (!(inb(STATUS)&TXFREEflag) || !lp->txready) return;
1326 
1327         /* start sending */
1328         outb(TXcmd|(lp->txready<<3),COMMAND);
1329 
1330 #ifdef IRQ_XMIT
1331         outb(TXFREEflag|NORXflag,INTMASK);
1332 #endif
1333 
1334         dev->trans_start = jiffies;
1335         lp->txready=0;
1336         lp->sending++;
1337 }
1338 
1339 
1340 /* The typical workload of the driver:
1341    Handle the network interface interrupts. */
1342 static void
1343 arcnet_interrupt(int irq,struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
1344 {
1345         struct device *dev = (struct device *)(irq2dev_map[irq]);
1346 
1347         if (dev == NULL) {
1348                 if (net_debug >= D_DURING)
1349                         printk("arcnet: irq %d for unknown device.\n", irq);
1350                 return;
1351         }
1352         
1353         arcnet_inthandler(dev);
1354 }
1355 
1356 static void
1357 arcnet_inthandler(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1358 {       
1359         struct arcnet_local *lp;
1360         int ioaddr, status, boguscount = 3, didsomething;
1361         
1362         dev->interrupt = 1;
1363         sti();
1364 
1365         ioaddr = dev->base_addr;
1366         lp = (struct arcnet_local *)dev->priv;
1367 
1368 #ifdef IRQ_XMIT
1369         outb(0,INTMASK);
1370 #endif
1371         
1372         BUGLVL(D_DURING)
1373                 printk("arcnet: in net_interrupt (status=%Xh)\n",inb(STATUS));
1374 
1375         do
1376         {
1377                 status = inb(STATUS);
1378                 didsomething=0;
1379         
1380                 if (!dev->start)
1381                 {
1382                         BUGLVL(D_EXTRA)
1383                                 printk("arcnet: ARCnet not yet initialized.  irq ignored. (status=%Xh)\n",
1384                                         status);
1385 #ifdef IRQ_XMIT
1386                         if (!(status&NORXflag))
1387                                 outb(NORXflag,INTMASK);
1388 #endif
1389                         dev->interrupt=0;
1390                         return;
1391                 }
1392         
1393                 /* RESET flag was enabled - card is resetting and if RX
1394                  * is disabled, it's NOT because we just got a packet.
1395                  */
1396                 if (status & RESETflag)
1397                 {
1398                         BUGLVL(D_INIT)
1399                                 printk("arcnet: reset irq (status=%Xh)\n",
1400                                         status);
1401                         dev->interrupt=0;
1402                         return;
1403                 }
1404 
1405 #if 1   /* yes, it's silly to disable this part but it makes good testing */
1406                 /* RX is inhibited - we must have received something. */
1407                 if (status & NORXflag)
1408                 {
1409                         int recbuf=lp->recbuf=!lp->recbuf;
1410 
1411                         BUGLVL(D_DURING)
1412                                 printk("arcnet: receive irq (status=%Xh)\n",
1413                                         status);
1414 
1415                         /* enable receive of our next packet */
1416                         EnableReceiver();
1417                 
1418                         /* Got a packet. */
1419                         arcnet_rx(dev,!recbuf);
1420                         
1421                         didsomething++;
1422                 }
1423 #endif
1424 #ifdef IRQ_XMIT
1425                 /* it can only be an xmit-done irq if we're xmitting :) */
1426                 if (status&TXFREEflag && !lp->in_txhandler && lp->sending)
1427                 {
1428                         struct Outgoing *out=&(lp->outgoing);
1429                         
1430                         lp->in_txhandler++;
1431                         lp->sending--;
1432                         
1433                         BUGLVL(D_DURING)
1434                                 printk("arcnet: TX IRQ (stat=%Xh, numsegs=%d, segnum=%d, skb=%ph)\n",
1435                                         status,out->numsegs,out->segnum,out->skb);
1436                                         
1437                         /* send packet if there is one */
1438                         if (lp->txready)
1439                         {
1440                                 arcnet_go_tx(dev);
1441                                 didsomething++;
1442                         }
1443                         
1444                         if (lp->intx)
1445                         {
1446                                 lp->in_txhandler--;
1447                                 continue;
1448                         }
1449 
1450                         if (!lp->outgoing.skb)
1451                         {
1452                                 BUGLVL(D_DURING)
1453                                         printk("arcnet: TX IRQ done: no split to continue.\n");
1454                                 
1455                                 /* inform upper layers */
1456                                 if (!lp->txready && dev->tbusy)
1457                                 {
1458                                         dev->tbusy=0;
1459                                         mark_bh(NET_BH);
1460                                 }
1461                                 
1462                                 lp->in_txhandler--;
1463                                 continue;
1464                         }
1465                         
1466                         /*lp->stats.tx_packets++;*/
1467                         
1468                         /* if more than one segment, and not all segments
1469                          * are done, then continue xmit.
1470                          */
1471                         if (out->segnum<out->numsegs)
1472                                 arcnet_continue_tx(dev);
1473                         if (lp->txready && !lp->sending)
1474                                 arcnet_go_tx(dev);
1475 
1476                         /* if segnum==numsegs, the transmission is finished;
1477                          * free the skb.
1478                          */
1479                         if (out->segnum>=out->numsegs)
1480                         {
1481                                 /* transmit completed */
1482                                 out->segnum++;
1483                                 if (out->skb)
1484                                         dev_kfree_skb(out->skb,FREE_WRITE);
1485                                 out->skb=NULL;
1486 
1487                                 /* inform upper layers */
1488                                 if (!lp->txready && dev->tbusy)
1489                                 {
1490                                         dev->tbusy=0;
1491                                         mark_bh(NET_BH);
1492                                 }
1493                         }
1494                         didsomething++;
1495                         
1496                         lp->in_txhandler--;
1497                 }
1498 #endif /* IRQ_XMIT */
1499         } while (--boguscount && didsomething);
1500 
1501         BUGLVL(D_DURING)
1502                 printk("arcnet: net_interrupt complete (status=%Xh)\n",
1503                         inb(STATUS));
1504 
1505 #ifdef IRQ_XMIT
1506         if (dev->start && lp->sending )
1507                 outb(NORXflag|TXFREEflag,INTMASK);
1508         else
1509                 outb(NORXflag,INTMASK);
1510 #endif
1511 
1512         dev->interrupt=0;
1513 }
1514 
1515 /* A packet has arrived; grab it from the buffers and possibly unsplit it.
1516  */
1517 static void
1518 arcnet_rx(struct device *dev,int recbuf)
     /* [previous][next][first][last][top][bottom][index][help] */
1519 {
1520         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1521         int ioaddr = dev->base_addr;
1522 /*      int status = inb(STATUS);*/
1523 
1524         struct sk_buff *skb;
1525 
1526         union ArcPacket *arcpacket=
1527                 (union ArcPacket *)(dev->mem_start+recbuf*512);
1528         struct ClientData *soft,*arcsoft;
1529         short length,offset;
1530         u_char pkttype,daddr,saddr;
1531 
1532         daddr=arcpacket->hardheader.destination;
1533         saddr=arcpacket->hardheader.source;
1534         
1535         /* if source is 0, it's not a "used" packet! */
1536         if (saddr==0)
1537         {
1538                 /*BUGLVL(D_DURING)*/
1539                         printk("arcnet: discarding old packet. (status=%Xh)\n",
1540                                 inb(STATUS));
1541                 lp->stats.rx_errors++;
1542                 return;
1543         }
1544         arcpacket->hardheader.source=0;
1545         
1546         if (arcpacket->hardheader.offset1) /* Normal Packet */
1547         {
1548                 offset=arcpacket->hardheader.offset1;
1549                 arcsoft=(struct ClientData *)
1550                         (&arcpacket->raw[offset-EXTRA_CLIENTDATA]);
1551                 length=256-offset+EXTRA_CLIENTDATA;
1552                 pkttype=NORMAL;
1553         }
1554         else            /* ExtendedPacket or ExceptionPacket */
1555         {
1556                 offset=arcpacket->hardheader.offset2;
1557                 arcsoft=(struct ClientData *)
1558                         (&arcpacket->raw[offset-EXTRA_CLIENTDATA]);
1559 
1560                 if (arcsoft->split_flag!=0xFF)  /* Extended Packet */
1561                 {
1562                         length=512-offset+EXTRA_CLIENTDATA;
1563                         pkttype=EXTENDED;
1564                 }
1565                 else                            /* Exception Packet */
1566                 {
1567                         /* skip over 4-byte junkola */
1568                         arcsoft=(struct ClientData *)
1569                                 ((u_char *)arcsoft + 4);
1570                         length=512-offset+EXTRA_CLIENTDATA-4;
1571                         pkttype=EXCEPTION;
1572                 }
1573         }
1574         
1575         if (!arcsoft->split_flag)               /* not split */
1576         {
1577                 struct Incoming *in=&lp->incoming[saddr];
1578 
1579                 BUGLVL(D_RX) printk("arcnet: incoming is not split (splitflag=%d)\n",
1580                         arcsoft->split_flag);
1581                         
1582         if (in->skb)    /* already assembling one! */
1583                 {
1584                         BUGLVL(D_INIT) printk("arcnet: aborting assembly (seq=%d) for unsplit packet (splitflag=%d, seq=%d)\n",
1585                                 in->sequence,arcsoft->split_flag,
1586                                 arcsoft->sequence);
1587                         kfree_skb(in->skb,FREE_WRITE);
1588                         in->skb=NULL;
1589                 }
1590                 
1591                 in->sequence=arcsoft->sequence;
1592 
1593                 skb = alloc_skb(length, GFP_ATOMIC);
1594                 if (skb == NULL) {
1595                         printk("%s: Memory squeeze, dropping packet.\n", 
1596                                 dev->name);
1597                         lp->stats.rx_dropped++;
1598                         return;
1599                 }
1600                 soft=(struct ClientData *)skb->data;
1601                 
1602                 skb->len = length;
1603                 skb->dev = dev;
1604                 
1605                 memcpy((u_char *)soft+EXTRA_CLIENTDATA,
1606                         (u_char *)arcsoft+EXTRA_CLIENTDATA,
1607                         length-EXTRA_CLIENTDATA);
1608                 soft->daddr=daddr;
1609                 soft->saddr=saddr;
1610                 
1611                 BUGLVL(D_DURING)
1612                         printk("arcnet: received packet from %02Xh to %02Xh (%d bytes, type=%d)\n",
1613                                 saddr,daddr,length,pkttype);
1614                 BUGLVL(D_RX)
1615                 {
1616                         int countx,county;
1617                         
1618                         printk("arcnet: packet dump [rx-unsplit] follows:");
1619 
1620                         for (county=0; county<16+(pkttype!=NORMAL)*16; county++)
1621                         {
1622                                 printk("\n[%04X] ",county*16);
1623                                 for (countx=0; countx<16; countx++)
1624                                         printk("%02X ",
1625                                                 arcpacket->raw[county*16+countx]);
1626                         }
1627                         
1628                         printk("\n");
1629                 }
1630 
1631                 /* ARP packets have problems when sent from DOS.
1632                  * source address is always 0!  So we take the hardware
1633                  * source addr (which is impossible to fumble) and insert
1634                  * it ourselves.
1635                  */
1636                 if (soft->protocol_id == ARC_P_ARP)
1637                 {
1638                         struct arphdr *arp=(struct arphdr *)
1639                                         ((char *)soft+sizeof(struct ClientData));
1640 
1641                         /* make sure addresses are the right length */
1642                         if (arp->ar_hln==1 && arp->ar_pln==4)
1643                         {
1644                                 char *cptr=(char *)(arp)+sizeof(struct arphdr);
1645                                 
1646                                 if (!*cptr)     /* is saddr = 00? */
1647                                 {
1648                                         BUGLVL(D_DURING)
1649                                                 printk("arcnet: ARP source address was 00h, set to %02Xh.\n",
1650                                                         saddr);
1651                                         *cptr=saddr;
1652                                 }
1653                                 else BUGLVL(D_DURING) 
1654                                 {
1655                                         printk("arcnet: ARP source address (%Xh) is fine.\n",
1656                                                 *cptr);
1657                                 }
1658                         }
1659                         else
1660                         {
1661                                 printk("arcnet: funny-shaped ARP packet. (%Xh, %Xh)\n",
1662                                         arp->ar_hln,arp->ar_pln);
1663                         }
1664                 }
1665                 skb->protocol=arc_type_trans(skb,dev);
1666                 netif_rx(skb);
1667                 lp->stats.rx_packets++;
1668          }
1669          else                   /* split packet */
1670          {
1671                  /* NOTE:  MSDOS ARP packet correction should only need to
1672                   * apply to unsplit packets, since ARP packets are so short.
1673                   *
1674                   * My interpretation of the RFC1201 (ARCnet) document is that
1675                   * if a packet is received out of order, the entire assembly
1676                   * process should be aborted.
1677                   *
1678                   * The RFC also mentions "it is possible for successfully
1679                   * received packets to be retransmitted."  As of 0.40 all
1680                   * previously received packets are allowed, not just the
1681                   * most recent one.
1682                   *
1683                   * We allow multiple assembly processes, one for each
1684                   * ARCnet card possible on the network.  Seems rather like
1685                   * a waste of memory.  Necessary?
1686                   */
1687                   
1688                 struct Incoming *in=&lp->incoming[saddr];
1689                 
1690                 BUGLVL(D_RX) printk("arcnet: packet is split (splitflag=%d, seq=%d)\n",
1691                         arcsoft->split_flag,in->sequence);
1692 
1693                 if (in->skb && in->sequence!=arcsoft->sequence)
1694                 {
1695                         BUGLVL(D_INIT) printk("arcnet: wrong seq number, aborting assembly (expected=%d, seq=%d, splitflag=%d)\n",      
1696                                 in->sequence,arcsoft->sequence,
1697                                 arcsoft->split_flag);
1698                         kfree_skb(in->skb,FREE_WRITE);
1699                         in->skb=NULL;
1700                         in->lastpacket=in->numpackets=0;
1701                 }
1702                   
1703                 if (arcsoft->split_flag & 1)    /* first packet in split */
1704                 {
1705                         BUGLVL(D_RX) printk("arcnet: brand new splitpacket (splitflag=%d)\n",
1706                                 arcsoft->split_flag);
1707                         if (in->skb)    /* already assembling one! */
1708                         {
1709                                 BUGLVL(D_INIT) printk("arcnet: aborting previous (seq=%d) assembly (splitflag=%d, seq=%d)\n",
1710                                         in->sequence,arcsoft->split_flag,
1711                                         arcsoft->sequence);
1712                                 kfree_skb(in->skb,FREE_WRITE);
1713                         }
1714 
1715                         in->sequence=arcsoft->sequence;
1716                         in->numpackets=((unsigned)arcsoft->split_flag>>1)+2;
1717                         in->lastpacket=1;
1718                         
1719                         if (in->numpackets>16)
1720                         {
1721                                 printk("arcnet: incoming packet more than 16 segments; dropping. (splitflag=%d)\n",
1722                                         arcsoft->split_flag);
1723                                 lp->stats.rx_dropped++;
1724                                 return;
1725                         }
1726                 
1727                         in->skb=skb=alloc_skb(508*in->numpackets
1728                                         + sizeof(struct ClientData),
1729                                         GFP_ATOMIC);
1730                         if (skb == NULL) {
1731                                 printk("%s: (split) memory squeeze, dropping packet.\n", 
1732                                         dev->name);
1733                                 lp->stats.rx_dropped++;
1734                                 return;
1735                         }
1736                                         
1737                         /* I don't know what this is for, but it DOES avoid
1738                          * warnings...
1739                          */
1740                         skb->free=1;
1741                         
1742                         soft=(struct ClientData *)skb->data;
1743                         
1744                         skb->len=sizeof(struct ClientData);
1745                         skb->dev=dev;
1746 
1747                         memcpy((u_char *)soft+EXTRA_CLIENTDATA,
1748                                 (u_char *)arcsoft+EXTRA_CLIENTDATA,
1749                                 sizeof(struct ClientData)-EXTRA_CLIENTDATA);
1750                         soft->split_flag=0; /* final packet won't be split */
1751                 }
1752                 else                    /* not first packet */
1753                 {
1754                         int packetnum=((unsigned)arcsoft->split_flag>>1) + 1;
1755 
1756                         /* if we're not assembling, there's no point
1757                          * trying to continue.
1758                          */                     
1759                         if (!in->skb)
1760                         {
1761                                 BUGLVL(D_INIT) printk("arcnet: can't continue split without starting first! (splitflag=%d, seq=%d)\n",
1762                                         arcsoft->split_flag,arcsoft->sequence);
1763                                 return;
1764                         }
1765 
1766                         in->lastpacket++;
1767                         if (packetnum!=in->lastpacket) /* not the right flag! */
1768                         {
1769                                 /* harmless duplicate? ignore. */
1770                                 if (packetnum<=in->lastpacket-1)
1771                                 {
1772                                         BUGLVL(D_INIT) printk("arcnet: duplicate splitpacket ignored! (splitflag=%d)\n",
1773                                                 arcsoft->split_flag);
1774                                         return;
1775                                 }
1776                                 
1777                                 /* "bad" duplicate, kill reassembly */
1778                                 BUGLVL(D_INIT) printk("arcnet: out-of-order splitpacket, reassembly (seq=%d) aborted (splitflag=%d, seq=%d)\n", 
1779                                         in->sequence,arcsoft->split_flag,
1780                                         arcsoft->sequence);
1781                                 kfree_skb(in->skb,FREE_WRITE);
1782                                 in->skb=NULL;
1783                                 in->lastpacket=in->numpackets=0;
1784                                 return;
1785                         }
1786 
1787                         soft=(struct ClientData *)in->skb->data;
1788                 }
1789                 
1790                 skb=in->skb;
1791                 
1792                 memcpy(skb->data+skb->len,
1793                        (u_char *)arcsoft+sizeof(struct ClientData),
1794                        length-sizeof(struct ClientData));
1795 
1796                 skb->len+=length-sizeof(struct ClientData);
1797                 
1798                 soft->daddr=daddr;
1799                 soft->saddr=saddr;
1800                 
1801                 BUGLVL(D_DURING)
1802                         printk("arcnet: received packet from %02Xh to %02Xh (%d bytes, type=%d)\n",
1803                                 saddr,daddr,length,pkttype);
1804                 BUGLVL(D_RX)
1805                 {
1806                         int countx,county;
1807                         
1808                         printk("arcnet: packet dump [rx-split] follows:");
1809 
1810                         for (county=0; county<16+(pkttype!=NORMAL)*16; county++)
1811                         {
1812                                 printk("\n[%04X] ",county*16);
1813                                 for (countx=0; countx<16; countx++)
1814                                         printk("%02X ",
1815                                                 arcpacket->raw[county*16+countx]);
1816                         }
1817                         
1818                         printk("\n");
1819                 }
1820                 
1821                 /* are we done? */
1822                 if (in->lastpacket == in->numpackets)
1823                 {
1824                         if (!skb || !in->skb)
1825                                 printk("arcnet: ?!? done reassembling packet, no skb? (skb=%ph, in->skb=%ph)\n",
1826                                         skb,in->skb);
1827                         in->skb=NULL;
1828                         in->lastpacket=in->numpackets=0;
1829                         skb->protocol=arc_type_trans(skb,dev);                  
1830                         netif_rx(skb);
1831                         lp->stats.rx_packets++;
1832                 }
1833          }
1834         
1835         /* If any worth-while packets have been received, netif_rx()
1836            has done a mark_bh(NET_BH) for us and will work on them
1837            when we get to the bottom-half routine. */
1838 }
1839 
1840 
1841 #ifdef USE_TIMER_HANDLER
1842 /* this function is called every once in a while to make sure the ARCnet
1843  * isn't stuck.
1844  *
1845  * If we miss a receive IRQ, the receiver (and IRQ) is permanently disabled
1846  * and we might never receive a packet again!  This will check if this
1847  * is the case, and if so, re-enable the receiver.
1848  */
1849 static void
1850 arcnet_timer(unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1851 {
1852         struct device *dev=(struct device *)arg;
1853         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1854         short ioaddr=dev->base_addr;
1855         int status=inb(STATUS);
1856 
1857         /* if we didn't interrupt the IRQ handler, and RX's are still
1858          * disabled, and we're not resetting the card... then we're stuck!
1859          */
1860         if (!dev->interrupt && dev->start
1861           && status&NORXflag && !status&RESETflag)
1862         {
1863                 BUGLVL(D_INIT)
1864                         printk("arcnet: timer: ARCnet was stuck!  (status=%Xh)\n",
1865                                 status);
1866                 
1867                 arcnet_inthandler(dev);
1868         }
1869 
1870         /* requeue ourselves */
1871         init_timer(&lp->timer);
1872         lp->timer.expires=TIMERval;
1873         add_timer(&lp->timer);
1874 }
1875 #endif
1876 
1877 /* Get the current statistics.  This may be called with the card open or
1878    closed. */
1879 static struct enet_statistics *
1880 arcnet_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1881 {
1882         struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
1883 /*      short ioaddr = dev->base_addr;*/
1884 
1885         return &lp->stats;
1886 }
1887 
1888 /* Set or clear the multicast filter for this adaptor.
1889    num_addrs == -1      Promiscuous mode, receive all packets
1890    num_addrs == 0       Normal mode, clear multicast list
1891    num_addrs > 0        Multicast mode, receive normal and MC packets, and do
1892                         best-effort filtering.
1893  */
1894 static void
1895 set_multicast_list(struct device *dev, int num_addrs, void *addrs)
     /* [previous][next][first][last][top][bottom][index][help] */
1896 {
1897 #if 0     /* no promiscuous mode at all */
1898         struct arcnet_local *lp=(struct arcnet_local *)(dev->priv);
1899 
1900         short ioaddr = dev->base_addr;
1901         if (num_addrs) {
1902                 outw(69, ioaddr);               /* Enable promiscuous mode */
1903         } else
1904                 outw(99, ioaddr);               /* Disable promiscuous mode, use normal mode */
1905 #endif
1906 }
1907 
1908 int arcnet_reset(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1909 {
1910         struct arcnet_local *lp=(struct arcnet_local *)dev->priv;
1911         short ioaddr=dev->base_addr;
1912         int delayval,recbuf=lp->recbuf;
1913         
1914         outb(0,INTMASK);        /* no IRQ's, please! */
1915         
1916         BUGLVL(D_INIT)
1917                 printk("arcnet: Resetting %s (status=%Xh)\n",
1918                         dev->name,inb(STATUS));
1919 
1920         inb(RESET);             /* Reset by reading this port */
1921         JIFFER(RESETtime);
1922 
1923         outb(CFLAGScmd|RESETclear, COMMAND); /* clear flags & end reset */
1924         outb(CFLAGScmd|CONFIGclear,COMMAND);
1925 
1926         /* after a reset, the first byte of shared mem is TESTvalue and the
1927          * second byte is our 8-bit ARCnet address
1928          */
1929         {
1930                 u_char *cardmem = (u_char *) dev->mem_start;
1931                 if (cardmem[0] != TESTvalue)
1932                 {
1933                         BUGLVL(D_INIT)
1934                                 printk("arcnet: reset failed: TESTvalue not present.\n");
1935                         return 1;
1936                 }
1937                 lp->arcnum=cardmem[1];  /* save address for later use */
1938         }
1939         
1940         /* clear out status variables */
1941         recbuf=lp->recbuf=0;
1942         lp->txbuf=2;
1943         /*dev->tbusy=0;*/
1944 
1945         /* enable extended (512-byte) packets */
1946         outb(CONFIGcmd|EXTconf,COMMAND);
1947         XJIFFER(ACKtime);
1948         
1949         /* clean out all the memory to make debugging make more sense :) */
1950         BUGLVL(D_DURING)
1951                 memset((void *)dev->mem_start,0x42,2048);
1952         
1953         /* and enable receive of our first packet to the first buffer */
1954         EnableReceiver();
1955 
1956         /* re-enable interrupts */
1957         outb(NORXflag,INTMASK);
1958         
1959         /* done!  return success. */
1960         return 0;
1961 }
1962 
1963 
1964 /*
1965  *       Create the ARCnet ClientData header for an arbitrary protocol layer
1966  *
1967  *      saddr=NULL      means use device source address (always will anyway)
1968  *      daddr=NULL      means leave destination address (eg unresolved arp)
1969  */
1970 int arc_header(unsigned char *buff,struct device *dev,unsigned short type,
     /* [previous][next][first][last][top][bottom][index][help] */
1971                 void *daddr,void *saddr,unsigned len,struct sk_buff *skb)
1972 {
1973         struct ClientData *head = (struct ClientData *)buff;
1974         struct arcnet_local *lp=(struct arcnet_local *)(dev->priv);
1975 
1976         /* set the protocol ID according to RFC-1201 */
1977         switch(type)
1978         {
1979         case ETH_P_IP:
1980                 head->protocol_id=ARC_P_IP;
1981                 break;
1982         case ETH_P_ARP:
1983                 head->protocol_id=ARC_P_ARP;
1984                 break;
1985         case ETH_P_RARP:
1986                 head->protocol_id=ARC_P_RARP;
1987                 break;
1988         case ETH_P_IPX:
1989                 head->protocol_id=ARC_P_IPX;
1990                 break;
1991         case ETH_P_ATALK:
1992                 head->protocol_id=ARC_P_ATALK;
1993                 break;
1994         default:
1995                 printk("arcnet: I don't understand protocol %d (%Xh)\n",
1996                         type,type);
1997                 return 0;
1998         }       
1999 
2000 #if 1
2001         /*
2002          *      Set the source hardware address.
2003          *      AVE: we can't do this, so we don't.  Code below is directly
2004          *           stolen from eth.c driver and won't work.
2005          ** TM: but for debugging I would like to have saddr in the header
2006          */
2007         if(saddr)
2008                 head->saddr=((u_char*)saddr)[0];
2009         else
2010                 head->saddr=((u_char*)(dev->dev_addr))[0];
2011 #endif
2012 
2013 #if 0
2014         /*
2015          *      Anyway, the loopback-device should never use this function... 
2016          *
2017          *      And the chances of it using the ARCnet version of it are so
2018          *      tiny that I don't think we have to worry :)
2019          */
2020         if (dev->flags & IFF_LOOPBACK) 
2021         {
2022                 head->daddr=0;
2023                 return(dev->hard_header_len);
2024         }
2025 #endif
2026 
2027         head->split_flag=0;     /* split packets are done elsewhere */
2028         head->sequence=(lp->sequence++);
2029 
2030         /* supposedly if daddr is NULL, we should ignore it... */
2031         if(daddr)
2032         {
2033                 head->daddr=((u_char*)daddr)[0];
2034                 return dev->hard_header_len;
2035         }
2036         else
2037                 head->daddr=0;  /* better fill one in anyway */
2038                 
2039         return -dev->hard_header_len;
2040 }
2041 
2042 
2043 /*
2044  *      Rebuild the ARCnet ClientData header. This is called after an ARP
2045  *      (or in future other address resolution) has completed on this
2046  *      sk_buff. We now let ARP fill in the other fields.
2047  */
2048 int arc_rebuild_header(void *buff,struct device *dev,unsigned long dst,
     /* [previous][next][first][last][top][bottom][index][help] */
2049                 struct sk_buff *skb)
2050 {
2051         struct ClientData *head = (struct ClientData *)buff;
2052 
2053         /*
2054          *      Only ARP/IP is currently supported
2055          */
2056          
2057         if(head->protocol_id != ARC_P_IP) 
2058         {
2059                 printk("arcnet: I don't understand resolve type %d (%Xh) addresses!\n",
2060                         head->protocol_id,head->protocol_id);
2061                 head->daddr=0;
2062                 /*memcpy(eth->h_source, dev->dev_addr, dev->addr_len);*/
2063                 return 0;
2064         }
2065 
2066         /*
2067          *      Try and get ARP to resolve the header.
2068          */
2069 #ifdef CONFIG_INET       
2070         return arp_find(&(head->daddr), dst, dev, dev->pa_addr, skb)? 1 : 0;
2071 #else
2072         return 0;       
2073 #endif  
2074 }
2075                 
2076 /*
2077  *      Determine the packet's protocol ID.
2078  *
2079  *      With ARCnet we have to convert everything to Ethernet-style stuff.
2080  */
2081 unsigned short arc_type_trans(struct sk_buff *skb,struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
2082 {
2083         struct ClientData *head = (struct ClientData *) skb->data;
2084         
2085         if (head->daddr==0)
2086                 skb->pkt_type=PACKET_BROADCAST;
2087         else if(dev->flags&IFF_PROMISC)
2088         {
2089                 /* if we're not sending to ourselves :) */
2090                 if (head->daddr != dev->dev_addr[0])
2091                         skb->pkt_type=PACKET_OTHERHOST;
2092         }
2093         
2094         /* now return the protocol number */
2095         switch (head->protocol_id)
2096         {
2097         case ARC_P_IP:          return htons(ETH_P_IP);
2098         case ARC_P_ARP:         return htons(ETH_P_ARP);
2099         case ARC_P_RARP:        return htons(ETH_P_RARP);
2100         case ARC_P_IPX:         return htons(ETH_P_IPX);
2101         case ARC_P_ATALK:       return htons(ETH_P_ATALK);      /* Doesn't work yet */
2102         case ARC_P_LANSOFT: /* don't understand.  fall through. */
2103         default:
2104                 BUGLVL(D_DURING)
2105                         printk("arcnet: received packet of unknown protocol id %d (%Xh)\n",
2106                                 head->protocol_id,head->protocol_id);
2107                 return 0;
2108         }
2109         return htons(ETH_P_IP);
2110 }
2111 
2112 #ifdef MODULE
2113 char kernel_version[] = UTS_RELEASE;
2114 static struct device thisARCnet = {
2115   "      ",/* if blank, device name inserted by /linux/drivers/net/net_init.c */
2116   0, 0, 0, 0,
2117   0, 0,  /* I/O address, IRQ */
2118   0, 0, 0, NULL, arcnet_probe };
2119         
2120         
2121 int io=0x0;     /* <--- EDIT THESE LINES FOR YOUR CONFIGURATION */
2122 int irqnum=0;   /* or use the insmod io= irq= shmem= options */
2123 int shmem=0;
2124 int num=0;      /* number of device (ie for arc0, arc1, arc2...) */
2125 
2126 int
2127 init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2128 {
2129         sprintf(thisARCnet.name,"arc%d",num);
2130 
2131         thisARCnet.base_addr=io;
2132 
2133         thisARCnet.irq=irqnum;
2134         if (thisARCnet.irq==2) thisARCnet.irq=9;
2135 
2136         if (shmem)
2137         {
2138                 thisARCnet.mem_start=shmem;
2139                 thisARCnet.mem_end=thisARCnet.mem_start+512*4-1;
2140                 thisARCnet.rmem_start=thisARCnet.mem_start+512*0;
2141                 thisARCnet.rmem_end=thisARCnet.mem_start+512*2-1;
2142         }
2143 
2144         if (register_netdev(&thisARCnet) != 0)
2145                 return -EIO;
2146         return 0;
2147 }
2148 
2149 void
2150 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2151 {
2152   if (MOD_IN_USE) {
2153     printk("%s: device busy, remove delayed\n",thisARCnet.name);
2154   } else {
2155     if (thisARCnet.start) arcnet_close(&thisARCnet);
2156     if (thisARCnet.irq) free_irq(thisARCnet.irq);
2157     if (thisARCnet.base_addr) release_region(thisARCnet.base_addr,
2158                                                 ETHERCARD_TOTAL_SIZE);
2159     unregister_netdev(&thisARCnet);
2160   }
2161 }
2162 
2163 #endif /* MODULE */
2164 
2165 
2166 
2167 /*
2168  * Local variables:
2169  *  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c skeleton.c"
2170  *  version-control: t
2171  *  kept-new-versions: 5
2172  *  tab-width: 4
2173  * End:
2174  */
2175 

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