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

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