home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / KERNEL-S / V1.2 / LINUX-1.2 / LINUX-1 / linux / drivers / net / arcnet.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-23  |  58.7 KB  |  2,191 lines

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