Back to home page

OSCL-LXR

 
 

    


0001 /*      cops.c: LocalTalk driver for Linux.
0002  *
0003  *  Authors:
0004  *      - Jay Schulist <jschlst@samba.org>
0005  *
0006  *  With more than a little help from;
0007  *  - Alan Cox <alan@lxorguk.ukuu.org.uk>
0008  *
0009  *      Derived from:
0010  *      - skeleton.c: A network driver outline for linux.
0011  *        Written 1993-94 by Donald Becker.
0012  *  - ltpc.c: A driver for the LocalTalk PC card.
0013  *    Written by Bradford W. Johnson.
0014  *
0015  *      Copyright 1993 United States Government as represented by the
0016  *      Director, National Security Agency.
0017  *
0018  *      This software may be used and distributed according to the terms
0019  *      of the GNU General Public License, incorporated herein by reference.
0020  *
0021  *  Changes:
0022  *  19970608    Alan Cox    Allowed dual card type support
0023  *                  Can set board type in insmod
0024  *                  Hooks for cops_setup routine
0025  *                  (not yet implemented).
0026  *  19971101    Jay Schulist    Fixes for multiple lt* devices.
0027  *  19980607    Steven Hirsch   Fixed the badly broken support
0028  *                  for Tangent type cards. Only
0029  *                                      tested on Daystar LT200. Some
0030  *                                      cleanup of formatting and program
0031  *                                      logic.  Added emacs 'local-vars'
0032  *                                      setup for Jay's brace style.
0033  *  20000211    Alan Cox    Cleaned up for softnet
0034  */
0035 
0036 static const char *version =
0037 "cops.c:v0.04 6/7/98 Jay Schulist <jschlst@samba.org>\n";
0038 /*
0039  *  Sources:
0040  *      COPS Localtalk SDK. This provides almost all of the information
0041  *      needed.
0042  */
0043 
0044 /*
0045  * insmod/modprobe configurable stuff.
0046  *  - IO Port, choose one your card supports or 0 if you dare.
0047  *  - IRQ, also choose one your card supports or nothing and let
0048  *    the driver figure it out.
0049  */
0050 
0051 #include <linux/module.h>
0052 #include <linux/kernel.h>
0053 #include <linux/types.h>
0054 #include <linux/fcntl.h>
0055 #include <linux/interrupt.h>
0056 #include <linux/ptrace.h>
0057 #include <linux/ioport.h>
0058 #include <linux/in.h>
0059 #include <linux/string.h>
0060 #include <linux/errno.h>
0061 #include <linux/init.h>
0062 #include <linux/netdevice.h>
0063 #include <linux/etherdevice.h>
0064 #include <linux/skbuff.h>
0065 #include <linux/if_arp.h>
0066 #include <linux/if_ltalk.h>
0067 #include <linux/delay.h>    /* For udelay() */
0068 #include <linux/atalk.h>
0069 #include <linux/spinlock.h>
0070 #include <linux/bitops.h>
0071 #include <linux/jiffies.h>
0072 
0073 #include <net/Space.h>
0074 
0075 #include <asm/io.h>
0076 #include <asm/dma.h>
0077 
0078 #include "cops.h"       /* Our Stuff */
0079 #include "cops_ltdrv.h"     /* Firmware code for Tangent type cards. */
0080 #include "cops_ffdrv.h"     /* Firmware code for Dayna type cards. */
0081 
0082 /*
0083  *      The name of the card. Is used for messages and in the requests for
0084  *      io regions, irqs and dma channels
0085  */
0086 
0087 static const char *cardname = "cops";
0088 
0089 #ifdef CONFIG_COPS_DAYNA
0090 static int board_type = DAYNA;  /* Module exported */
0091 #else
0092 static int board_type = TANGENT;
0093 #endif
0094 
0095 static int io = 0x240;      /* Default IO for Dayna */
0096 static int irq = 5;     /* Default IRQ */
0097 
0098 /*
0099  *  COPS Autoprobe information.
0100  *  Right now if port address is right but IRQ is not 5 this will
0101  *      return a 5 no matter what since we will still get a status response.
0102  *      Need one more additional check to narrow down after we have gotten
0103  *      the ioaddr. But since only other possible IRQs is 3 and 4 so no real
0104  *  hurry on this. I *STRONGLY* recommend using IRQ 5 for your card with
0105  *  this driver.
0106  * 
0107  *  This driver has 2 modes and they are: Dayna mode and Tangent mode.
0108  *  Each mode corresponds with the type of card. It has been found
0109  *  that there are 2 main types of cards and all other cards are
0110  *  the same and just have different names or only have minor differences
0111  *  such as more IO ports. As this driver is tested it will
0112  *  become more clear on exactly what cards are supported. The driver
0113  *  defaults to using Dayna mode. To change the drivers mode, simply
0114  *  select Dayna or Tangent mode when configuring the kernel.
0115  *
0116  *      This driver should support:
0117  *      TANGENT driver mode:
0118  *              Tangent ATB-II, Novell NL-1000, Daystar Digital LT-200,
0119  *      COPS LT-1
0120  *      DAYNA driver mode:
0121  *              Dayna DL2000/DaynaTalk PC (Half Length), COPS LT-95, 
0122  *      Farallon PhoneNET PC III, Farallon PhoneNET PC II
0123  *  Other cards possibly supported mode unknown though:
0124  *      Dayna DL2000 (Full length), COPS LT/M (Micro-Channel)
0125  *
0126  *  Cards NOT supported by this driver but supported by the ltpc.c
0127  *  driver written by Bradford W. Johnson <johns393@maroon.tc.umn.edu>
0128  *      Farallon PhoneNET PC
0129  *      Original Apple LocalTalk PC card
0130  * 
0131  *      N.B.
0132  *
0133  *      The Daystar Digital LT200 boards do not support interrupt-driven
0134  *      IO.  You must specify 'irq=0xff' as a module parameter to invoke
0135  *      polled mode.  I also believe that the port probing logic is quite
0136  *      dangerous at best and certainly hopeless for a polled card.  Best to 
0137  *      specify both. - Steve H.
0138  *
0139  */
0140 
0141 /*
0142  * Zero terminated list of IO ports to probe.
0143  */
0144 
0145 static unsigned int ports[] = { 
0146     0x240, 0x340, 0x200, 0x210, 0x220, 0x230, 0x260, 
0147     0x2A0, 0x300, 0x310, 0x320, 0x330, 0x350, 0x360,
0148     0
0149 };
0150 
0151 /*
0152  * Zero terminated list of IRQ ports to probe.
0153  */
0154 
0155 static int cops_irqlist[] = {
0156     5, 4, 3, 0 
0157 };
0158 
0159 static struct timer_list cops_timer;
0160 static struct net_device *cops_timer_dev;
0161 
0162 /* use 0 for production, 1 for verification, 2 for debug, 3 for verbose debug */
0163 #ifndef COPS_DEBUG
0164 #define COPS_DEBUG 1 
0165 #endif
0166 static unsigned int cops_debug = COPS_DEBUG;
0167 
0168 /* The number of low I/O ports used by the card. */
0169 #define COPS_IO_EXTENT       8
0170 
0171 /* Information that needs to be kept for each board. */
0172 
0173 struct cops_local
0174 {
0175         int board;          /* Holds what board type is. */
0176     int nodeid;         /* Set to 1 once have nodeid. */
0177         unsigned char node_acquire; /* Node ID when acquired. */
0178         struct atalk_addr node_addr;    /* Full node address */
0179     spinlock_t lock;        /* RX/TX lock */
0180 };
0181 
0182 /* Index to functions, as function prototypes. */
0183 static int  cops_probe1 (struct net_device *dev, int ioaddr);
0184 static int  cops_irq (int ioaddr, int board);
0185 
0186 static int  cops_open (struct net_device *dev);
0187 static int  cops_jumpstart (struct net_device *dev);
0188 static void cops_reset (struct net_device *dev, int sleep);
0189 static void cops_load (struct net_device *dev);
0190 static int  cops_nodeid (struct net_device *dev, int nodeid);
0191 
0192 static irqreturn_t cops_interrupt (int irq, void *dev_id);
0193 static void cops_poll(struct timer_list *t);
0194 static void cops_timeout(struct net_device *dev, unsigned int txqueue);
0195 static void cops_rx (struct net_device *dev);
0196 static netdev_tx_t  cops_send_packet (struct sk_buff *skb,
0197                         struct net_device *dev);
0198 static void set_multicast_list (struct net_device *dev);
0199 static int  cops_ioctl (struct net_device *dev, struct ifreq *rq, int cmd);
0200 static int  cops_close (struct net_device *dev);
0201 
0202 static void cleanup_card(struct net_device *dev)
0203 {
0204     if (dev->irq)
0205         free_irq(dev->irq, dev);
0206     release_region(dev->base_addr, COPS_IO_EXTENT);
0207 }
0208 
0209 /*
0210  *      Check for a network adaptor of this type, and return '0' iff one exists.
0211  *      If dev->base_addr == 0, probe all likely locations.
0212  *      If dev->base_addr in [1..0x1ff], always return failure.
0213  *        otherwise go with what we pass in.
0214  */
0215 struct net_device * __init cops_probe(int unit)
0216 {
0217     struct net_device *dev;
0218     unsigned *port;
0219     int base_addr;
0220     int err = 0;
0221 
0222     dev = alloc_ltalkdev(sizeof(struct cops_local));
0223     if (!dev)
0224         return ERR_PTR(-ENOMEM);
0225 
0226     if (unit >= 0) {
0227         sprintf(dev->name, "lt%d", unit);
0228         netdev_boot_setup_check(dev);
0229         irq = dev->irq;
0230         base_addr = dev->base_addr;
0231     } else {
0232         base_addr = dev->base_addr = io;
0233     }
0234 
0235     if (base_addr > 0x1ff) {    /* Check a single specified location. */
0236         err = cops_probe1(dev, base_addr);
0237     } else if (base_addr != 0) { /* Don't probe at all. */
0238         err = -ENXIO;
0239     } else {
0240         /* FIXME  Does this really work for cards which generate irq?
0241          * It's definitely N.G. for polled Tangent. sh
0242          * Dayna cards don't autoprobe well at all, but if your card is
0243          * at IRQ 5 & IO 0x240 we find it every time. ;) JS
0244          */
0245         for (port = ports; *port && cops_probe1(dev, *port) < 0; port++)
0246             ;
0247         if (!*port)
0248             err = -ENODEV;
0249     }
0250     if (err)
0251         goto out;
0252     err = register_netdev(dev);
0253     if (err)
0254         goto out1;
0255     return dev;
0256 out1:
0257     cleanup_card(dev);
0258 out:
0259     free_netdev(dev);
0260     return ERR_PTR(err);
0261 }
0262 
0263 static const struct net_device_ops cops_netdev_ops = {
0264     .ndo_open               = cops_open,
0265         .ndo_stop               = cops_close,
0266     .ndo_start_xmit     = cops_send_packet,
0267     .ndo_tx_timeout     = cops_timeout,
0268         .ndo_do_ioctl           = cops_ioctl,
0269     .ndo_set_rx_mode    = set_multicast_list,
0270 };
0271 
0272 /*
0273  *      This is the real probe routine. Linux has a history of friendly device
0274  *      probes on the ISA bus. A good device probes avoids doing writes, and
0275  *      verifies that the correct device exists and functions.
0276  */
0277 static int __init cops_probe1(struct net_device *dev, int ioaddr)
0278 {
0279         struct cops_local *lp;
0280     static unsigned version_printed;
0281     int board = board_type;
0282     int retval;
0283     
0284         if(cops_debug && version_printed++ == 0)
0285         printk("%s", version);
0286 
0287     /* Grab the region so no one else tries to probe our ioports. */
0288     if (!request_region(ioaddr, COPS_IO_EXTENT, dev->name))
0289         return -EBUSY;
0290 
0291         /*
0292          * Since this board has jumpered interrupts, allocate the interrupt
0293          * vector now. There is no point in waiting since no other device
0294          * can use the interrupt, and this marks the irq as busy. Jumpered
0295          * interrupts are typically not reported by the boards, and we must
0296          * used AutoIRQ to find them.
0297      */
0298     dev->irq = irq;
0299     switch (dev->irq)
0300     {
0301         case 0:
0302             /* COPS AutoIRQ routine */
0303             dev->irq = cops_irq(ioaddr, board);
0304             if (dev->irq)
0305                 break;
0306             fallthrough;    /* Once no IRQ found on this port */
0307         case 1:
0308             retval = -EINVAL;
0309             goto err_out;
0310 
0311         /* Fixup for users that don't know that IRQ 2 is really
0312          * IRQ 9, or don't know which one to set.
0313          */
0314         case 2:
0315             dev->irq = 9;
0316             break;
0317 
0318         /* Polled operation requested. Although irq of zero passed as
0319          * a parameter tells the init routines to probe, we'll
0320          * overload it to denote polled operation at runtime.
0321          */
0322         case 0xff:
0323             dev->irq = 0;
0324             break;
0325 
0326         default:
0327             break;
0328     }
0329 
0330     dev->base_addr = ioaddr;
0331 
0332     /* Reserve any actual interrupt. */
0333     if (dev->irq) {
0334         retval = request_irq(dev->irq, cops_interrupt, 0, dev->name, dev);
0335         if (retval)
0336             goto err_out;
0337     }
0338 
0339         lp = netdev_priv(dev);
0340         spin_lock_init(&lp->lock);
0341 
0342     /* Copy local board variable to lp struct. */
0343     lp->board               = board;
0344 
0345     dev->netdev_ops     = &cops_netdev_ops;
0346     dev->watchdog_timeo = HZ * 2;
0347 
0348 
0349     /* Tell the user where the card is and what mode we're in. */
0350     if(board==DAYNA)
0351         printk("%s: %s at %#3x, using IRQ %d, in Dayna mode.\n", 
0352             dev->name, cardname, ioaddr, dev->irq);
0353     if(board==TANGENT) {
0354         if(dev->irq)
0355             printk("%s: %s at %#3x, IRQ %d, in Tangent mode\n", 
0356                 dev->name, cardname, ioaddr, dev->irq);
0357         else
0358             printk("%s: %s at %#3x, using polled IO, in Tangent mode.\n", 
0359                 dev->name, cardname, ioaddr);
0360 
0361     }
0362         return 0;
0363 
0364 err_out:
0365     release_region(ioaddr, COPS_IO_EXTENT);
0366     return retval;
0367 }
0368 
0369 static int __init cops_irq (int ioaddr, int board)
0370 {       /*
0371          * This does not use the IRQ to determine where the IRQ is. We just
0372          * assume that when we get a correct status response that it's the IRQ.
0373          * This really just verifies the IO port but since we only have access
0374          * to such a small number of IRQs (5, 4, 3) this is not bad.
0375          * This will probably not work for more than one card.
0376          */
0377         int irqaddr=0;
0378         int i, x, status;
0379 
0380         if(board==DAYNA)
0381         {
0382                 outb(0, ioaddr+DAYNA_RESET);
0383                 inb(ioaddr+DAYNA_RESET);
0384                 mdelay(333);
0385         }
0386         if(board==TANGENT)
0387         {
0388                 inb(ioaddr);
0389                 outb(0, ioaddr);
0390                 outb(0, ioaddr+TANG_RESET);
0391         }
0392 
0393         for(i=0; cops_irqlist[i] !=0; i++)
0394         {
0395                 irqaddr = cops_irqlist[i];
0396                 for(x = 0xFFFF; x>0; x --)    /* wait for response */
0397                 {
0398                         if(board==DAYNA)
0399                         {
0400                                 status = (inb(ioaddr+DAYNA_CARD_STATUS)&3);
0401                                 if(status == 1)
0402                                         return irqaddr;
0403                         }
0404                         if(board==TANGENT)
0405                         {
0406                                 if((inb(ioaddr+TANG_CARD_STATUS)& TANG_TX_READY) !=0)
0407                                         return irqaddr;
0408                         }
0409                 }
0410         }
0411         return 0;       /* no IRQ found */
0412 }
0413 
0414 /*
0415  * Open/initialize the board. This is called (in the current kernel)
0416  * sometime after booting when the 'ifconfig' program is run.
0417  */
0418 static int cops_open(struct net_device *dev)
0419 {
0420     struct cops_local *lp = netdev_priv(dev);
0421 
0422     if(dev->irq==0)
0423     {
0424         /*
0425          * I don't know if the Dayna-style boards support polled 
0426          * operation.  For now, only allow it for Tangent.
0427          */
0428         if(lp->board==TANGENT)  /* Poll 20 times per second */
0429         {
0430             cops_timer_dev = dev;
0431             timer_setup(&cops_timer, cops_poll, 0);
0432             cops_timer.expires  = jiffies + HZ/20;
0433             add_timer(&cops_timer);
0434         } 
0435         else 
0436         {
0437             printk(KERN_WARNING "%s: No irq line set\n", dev->name);
0438             return -EAGAIN;
0439         }
0440     }
0441 
0442     cops_jumpstart(dev);    /* Start the card up. */
0443 
0444     netif_start_queue(dev);
0445         return 0;
0446 }
0447 
0448 /*
0449  *  This allows for a dynamic start/restart of the entire card.
0450  */
0451 static int cops_jumpstart(struct net_device *dev)
0452 {
0453     struct cops_local *lp = netdev_priv(dev);
0454 
0455     /*
0456          *      Once the card has the firmware loaded and has acquired
0457          *      the nodeid, if it is reset it will lose it all.
0458          */
0459         cops_reset(dev,1);  /* Need to reset card before load firmware. */
0460         cops_load(dev);     /* Load the firmware. */
0461 
0462     /*
0463      *  If atalkd already gave us a nodeid we will use that
0464      *  one again, else we wait for atalkd to give us a nodeid
0465      *  in cops_ioctl. This may cause a problem if someone steals
0466      *  our nodeid while we are resetting.
0467      */ 
0468     if(lp->nodeid == 1)
0469         cops_nodeid(dev,lp->node_acquire);
0470 
0471     return 0;
0472 }
0473 
0474 static void tangent_wait_reset(int ioaddr)
0475 {
0476     int timeout=0;
0477 
0478     while(timeout++ < 5 && (inb(ioaddr+TANG_CARD_STATUS)&TANG_TX_READY)==0)
0479         mdelay(1);   /* Wait 1 second */
0480 }
0481 
0482 /*
0483  *      Reset the LocalTalk board.
0484  */
0485 static void cops_reset(struct net_device *dev, int sleep)
0486 {
0487         struct cops_local *lp = netdev_priv(dev);
0488         int ioaddr=dev->base_addr;
0489 
0490         if(lp->board==TANGENT)
0491         {
0492                 inb(ioaddr);        /* Clear request latch. */
0493                 outb(0,ioaddr);     /* Clear the TANG_TX_READY flop. */
0494                 outb(0, ioaddr+TANG_RESET); /* Reset the adapter. */
0495 
0496         tangent_wait_reset(ioaddr);
0497                 outb(0, ioaddr+TANG_CLEAR_INT);
0498         }
0499         if(lp->board==DAYNA)
0500         {
0501                 outb(0, ioaddr+DAYNA_RESET);    /* Assert the reset port */
0502                 inb(ioaddr+DAYNA_RESET);    /* Clear the reset */
0503         if (sleep)
0504             msleep(333);
0505         else
0506             mdelay(333);
0507         }
0508 
0509     netif_wake_queue(dev);
0510 }
0511 
0512 static void cops_load (struct net_device *dev)
0513 {
0514         struct ifreq ifr;
0515         struct ltfirmware *ltf= (struct ltfirmware *)&ifr.ifr_ifru;
0516         struct cops_local *lp = netdev_priv(dev);
0517         int ioaddr=dev->base_addr;
0518     int length, i = 0;
0519 
0520         strcpy(ifr.ifr_name,"lt0");
0521 
0522         /* Get card's firmware code and do some checks on it. */
0523 #ifdef CONFIG_COPS_DAYNA        
0524         if(lp->board==DAYNA)
0525         {
0526                 ltf->length=sizeof(ffdrv_code);
0527                 ltf->data=ffdrv_code;
0528         }
0529         else
0530 #endif        
0531 #ifdef CONFIG_COPS_TANGENT
0532         if(lp->board==TANGENT)
0533         {
0534                 ltf->length=sizeof(ltdrv_code);
0535                 ltf->data=ltdrv_code;
0536         }
0537         else
0538 #endif
0539     {
0540         printk(KERN_INFO "%s; unsupported board type.\n", dev->name);
0541         return;
0542     }
0543     
0544         /* Check to make sure firmware is correct length. */
0545         if(lp->board==DAYNA && ltf->length!=5983)
0546         {
0547                 printk(KERN_WARNING "%s: Firmware is not length of FFDRV.BIN.\n", dev->name);
0548                 return;
0549         }
0550         if(lp->board==TANGENT && ltf->length!=2501)
0551         {
0552                 printk(KERN_WARNING "%s: Firmware is not length of DRVCODE.BIN.\n", dev->name);
0553                 return;
0554         }
0555 
0556         if(lp->board==DAYNA)
0557         {
0558                 /*
0559                  *      We must wait for a status response
0560                  *      with the DAYNA board.
0561                  */
0562                 while(++i<65536)
0563                 {
0564                        if((inb(ioaddr+DAYNA_CARD_STATUS)&3)==1)
0565                                 break;
0566                 }
0567 
0568                 if(i==65536)
0569                         return;
0570         }
0571 
0572         /*
0573          *      Upload the firmware and kick. Byte-by-byte works nicely here.
0574          */
0575     i=0;
0576         length = ltf->length;
0577         while(length--)
0578         {
0579                 outb(ltf->data[i], ioaddr);
0580                 i++;
0581         }
0582 
0583     if(cops_debug > 1)
0584         printk("%s: Uploaded firmware - %d bytes of %d bytes.\n", 
0585             dev->name, i, ltf->length);
0586 
0587         if(lp->board==DAYNA)    /* Tell Dayna to run the firmware code. */
0588                 outb(1, ioaddr+DAYNA_INT_CARD);
0589     else            /* Tell Tang to run the firmware code. */
0590         inb(ioaddr);
0591 
0592         if(lp->board==TANGENT)
0593         {
0594                 tangent_wait_reset(ioaddr);
0595                 inb(ioaddr);    /* Clear initial ready signal. */
0596         }
0597 }
0598 
0599 /*
0600  *  Get the LocalTalk Nodeid from the card. We can suggest
0601  *  any nodeid 1-254. The card will try and get that exact
0602  *  address else we can specify 0 as the nodeid and the card
0603  *  will autoprobe for a nodeid.
0604  */
0605 static int cops_nodeid (struct net_device *dev, int nodeid)
0606 {
0607     struct cops_local *lp = netdev_priv(dev);
0608     int ioaddr = dev->base_addr;
0609 
0610     if(lp->board == DAYNA)
0611         {
0612         /* Empty any pending adapter responses. */
0613                 while((inb(ioaddr+DAYNA_CARD_STATUS)&DAYNA_TX_READY)==0)
0614                 {
0615             outb(0, ioaddr+COPS_CLEAR_INT); /* Clear interrupts. */
0616             if((inb(ioaddr+DAYNA_CARD_STATUS)&0x03)==DAYNA_RX_REQUEST)
0617                 cops_rx(dev);   /* Kick any packets waiting. */
0618             schedule();
0619                 }
0620 
0621                 outb(2, ioaddr);        /* Output command packet length as 2. */
0622                 outb(0, ioaddr);
0623                 outb(LAP_INIT, ioaddr); /* Send LAP_INIT command byte. */
0624                 outb(nodeid, ioaddr);   /* Suggest node address. */
0625         }
0626 
0627     if(lp->board == TANGENT)
0628         {
0629                 /* Empty any pending adapter responses. */
0630                 while(inb(ioaddr+TANG_CARD_STATUS)&TANG_RX_READY)
0631                 {
0632             outb(0, ioaddr+COPS_CLEAR_INT); /* Clear interrupt. */
0633             cops_rx(dev);           /* Kick out packets waiting. */
0634             schedule();
0635                 }
0636 
0637         /* Not sure what Tangent does if nodeid picked is used. */
0638                 if(nodeid == 0)                 /* Seed. */
0639             nodeid = jiffies&0xFF;      /* Get a random try */
0640                 outb(2, ioaddr);                /* Command length LSB */
0641                 outb(0, ioaddr);                /* Command length MSB */
0642                 outb(LAP_INIT, ioaddr);         /* Send LAP_INIT byte */
0643                 outb(nodeid, ioaddr);           /* LAP address hint. */
0644                 outb(0xFF, ioaddr);             /* Int. level to use */
0645         }
0646 
0647     lp->node_acquire=0;     /* Set nodeid holder to 0. */
0648         while(lp->node_acquire==0)  /* Get *True* nodeid finally. */
0649     {
0650         outb(0, ioaddr+COPS_CLEAR_INT); /* Clear any interrupt. */
0651 
0652         if(lp->board == DAYNA)
0653         {
0654             if((inb(ioaddr+DAYNA_CARD_STATUS)&0x03)==DAYNA_RX_REQUEST)
0655                 cops_rx(dev);   /* Grab the nodeid put in lp->node_acquire. */
0656         }
0657         if(lp->board == TANGENT)
0658         {   
0659             if(inb(ioaddr+TANG_CARD_STATUS)&TANG_RX_READY)
0660                 cops_rx(dev);   /* Grab the nodeid put in lp->node_acquire. */
0661         }
0662         schedule();
0663     }
0664 
0665     if(cops_debug > 1)
0666         printk(KERN_DEBUG "%s: Node ID %d has been acquired.\n", 
0667             dev->name, lp->node_acquire);
0668 
0669     lp->nodeid=1;   /* Set got nodeid to 1. */
0670 
0671         return 0;
0672 }
0673 
0674 /*
0675  *  Poll the Tangent type cards to see if we have work.
0676  */
0677  
0678 static void cops_poll(struct timer_list *unused)
0679 {
0680     int ioaddr, status;
0681     int boguscount = 0;
0682     struct net_device *dev = cops_timer_dev;
0683 
0684     del_timer(&cops_timer);
0685 
0686     if(dev == NULL)
0687         return; /* We've been downed */
0688 
0689     ioaddr = dev->base_addr;
0690     do {
0691         status=inb(ioaddr+TANG_CARD_STATUS);
0692         if(status & TANG_RX_READY)
0693             cops_rx(dev);
0694         if(status & TANG_TX_READY)
0695             netif_wake_queue(dev);
0696         status = inb(ioaddr+TANG_CARD_STATUS);
0697     } while((++boguscount < 20) && (status&(TANG_RX_READY|TANG_TX_READY)));
0698 
0699     /* poll 20 times per second */
0700     cops_timer.expires = jiffies + HZ/20;
0701     add_timer(&cops_timer);
0702 }
0703 
0704 /*
0705  *      The typical workload of the driver:
0706  *      Handle the network interface interrupts.
0707  */
0708 static irqreturn_t cops_interrupt(int irq, void *dev_id)
0709 {
0710         struct net_device *dev = dev_id;
0711         struct cops_local *lp;
0712         int ioaddr, status;
0713         int boguscount = 0;
0714 
0715         ioaddr = dev->base_addr;
0716         lp = netdev_priv(dev);
0717 
0718     if(lp->board==DAYNA)
0719     {
0720         do {
0721             outb(0, ioaddr + COPS_CLEAR_INT);
0722             status=inb(ioaddr+DAYNA_CARD_STATUS);
0723             if((status&0x03)==DAYNA_RX_REQUEST)
0724                 cops_rx(dev);
0725             netif_wake_queue(dev);
0726         } while(++boguscount < 20);
0727     }
0728     else
0729     {
0730         do {
0731             status=inb(ioaddr+TANG_CARD_STATUS);
0732             if(status & TANG_RX_READY)
0733                 cops_rx(dev);
0734             if(status & TANG_TX_READY)
0735                 netif_wake_queue(dev);
0736             status=inb(ioaddr+TANG_CARD_STATUS);
0737         } while((++boguscount < 20) && (status&(TANG_RX_READY|TANG_TX_READY)));
0738     }
0739 
0740         return IRQ_HANDLED;
0741 }
0742 
0743 /*
0744  *      We have a good packet(s), get it/them out of the buffers.
0745  */
0746 static void cops_rx(struct net_device *dev)
0747 {
0748         int pkt_len = 0;
0749         int rsp_type = 0;
0750         struct sk_buff *skb = NULL;
0751         struct cops_local *lp = netdev_priv(dev);
0752         int ioaddr = dev->base_addr;
0753         int boguscount = 0;
0754         unsigned long flags;
0755 
0756 
0757     spin_lock_irqsave(&lp->lock, flags);
0758     
0759         if(lp->board==DAYNA)
0760         {
0761                 outb(0, ioaddr);                /* Send out Zero length. */
0762                 outb(0, ioaddr);
0763                 outb(DATA_READ, ioaddr);        /* Send read command out. */
0764 
0765                 /* Wait for DMA to turn around. */
0766                 while(++boguscount<1000000)
0767                 {
0768             barrier();
0769                         if((inb(ioaddr+DAYNA_CARD_STATUS)&0x03)==DAYNA_RX_READY)
0770                                 break;
0771                 }
0772 
0773                 if(boguscount==1000000)
0774                 {
0775                         printk(KERN_WARNING "%s: DMA timed out.\n",dev->name);
0776             spin_unlock_irqrestore(&lp->lock, flags);
0777                         return;
0778                 }
0779         }
0780 
0781         /* Get response length. */
0782     pkt_len = inb(ioaddr);
0783         pkt_len |= (inb(ioaddr) << 8);
0784         /* Input IO code. */
0785         rsp_type=inb(ioaddr);
0786 
0787         /* Malloc up new buffer. */
0788         skb = dev_alloc_skb(pkt_len);
0789         if(skb == NULL)
0790         {
0791                 printk(KERN_WARNING "%s: Memory squeeze, dropping packet.\n",
0792             dev->name);
0793                 dev->stats.rx_dropped++;
0794                 while(pkt_len--)        /* Discard packet */
0795                         inb(ioaddr);
0796                 spin_unlock_irqrestore(&lp->lock, flags);
0797                 return;
0798         }
0799         skb->dev = dev;
0800         skb_put(skb, pkt_len);
0801         skb->protocol = htons(ETH_P_LOCALTALK);
0802 
0803         insb(ioaddr, skb->data, pkt_len);               /* Eat the Data */
0804 
0805         if(lp->board==DAYNA)
0806                 outb(1, ioaddr+DAYNA_INT_CARD);         /* Interrupt the card */
0807 
0808         spin_unlock_irqrestore(&lp->lock, flags);  /* Restore interrupts. */
0809 
0810         /* Check for bad response length */
0811         if(pkt_len < 0 || pkt_len > MAX_LLAP_SIZE)
0812         {
0813         printk(KERN_WARNING "%s: Bad packet length of %d bytes.\n", 
0814             dev->name, pkt_len);
0815                 dev->stats.tx_errors++;
0816                 dev_kfree_skb_any(skb);
0817                 return;
0818         }
0819 
0820         /* Set nodeid and then get out. */
0821         if(rsp_type == LAP_INIT_RSP)
0822         {   /* Nodeid taken from received packet. */
0823                 lp->node_acquire = skb->data[0];
0824                 dev_kfree_skb_any(skb);
0825                 return;
0826         }
0827 
0828         /* One last check to make sure we have a good packet. */
0829         if(rsp_type != LAP_RESPONSE)
0830         {
0831                 printk(KERN_WARNING "%s: Bad packet type %d.\n", dev->name, rsp_type);
0832                 dev->stats.tx_errors++;
0833                 dev_kfree_skb_any(skb);
0834                 return;
0835         }
0836 
0837         skb_reset_mac_header(skb);    /* Point to entire packet. */
0838         skb_pull(skb,3);
0839         skb_reset_transport_header(skb);    /* Point to data (Skip header). */
0840 
0841         /* Update the counters. */
0842         dev->stats.rx_packets++;
0843         dev->stats.rx_bytes += skb->len;
0844 
0845         /* Send packet to a higher place. */
0846         netif_rx(skb);
0847 }
0848 
0849 static void cops_timeout(struct net_device *dev, unsigned int txqueue)
0850 {
0851         struct cops_local *lp = netdev_priv(dev);
0852         int ioaddr = dev->base_addr;
0853 
0854     dev->stats.tx_errors++;
0855         if(lp->board==TANGENT)
0856         {
0857         if((inb(ioaddr+TANG_CARD_STATUS)&TANG_TX_READY)==0)
0858             printk(KERN_WARNING "%s: No TX complete interrupt.\n", dev->name);
0859     }
0860     printk(KERN_WARNING "%s: Transmit timed out.\n", dev->name);
0861     cops_jumpstart(dev);    /* Restart the card. */
0862     netif_trans_update(dev); /* prevent tx timeout */
0863     netif_wake_queue(dev);
0864 }
0865 
0866 
0867 /*
0868  *  Make the card transmit a LocalTalk packet.
0869  */
0870 
0871 static netdev_tx_t cops_send_packet(struct sk_buff *skb,
0872                       struct net_device *dev)
0873 {
0874         struct cops_local *lp = netdev_priv(dev);
0875         int ioaddr = dev->base_addr;
0876         unsigned long flags;
0877 
0878         /*
0879          * Block a timer-based transmit from overlapping. 
0880      */
0881      
0882     netif_stop_queue(dev);
0883 
0884     spin_lock_irqsave(&lp->lock, flags);
0885     if(lp->board == DAYNA)   /* Wait for adapter transmit buffer. */
0886         while((inb(ioaddr+DAYNA_CARD_STATUS)&DAYNA_TX_READY)==0)
0887             cpu_relax();
0888     if(lp->board == TANGENT) /* Wait for adapter transmit buffer. */
0889         while((inb(ioaddr+TANG_CARD_STATUS)&TANG_TX_READY)==0)
0890             cpu_relax();
0891 
0892     /* Output IO length. */
0893     outb(skb->len, ioaddr);
0894     outb(skb->len >> 8, ioaddr);
0895 
0896     /* Output IO code. */
0897     outb(LAP_WRITE, ioaddr);
0898 
0899     if(lp->board == DAYNA)  /* Check the transmit buffer again. */
0900         while((inb(ioaddr+DAYNA_CARD_STATUS)&DAYNA_TX_READY)==0);
0901 
0902     outsb(ioaddr, skb->data, skb->len); /* Send out the data. */
0903 
0904     if(lp->board==DAYNA)    /* Dayna requires you kick the card */
0905         outb(1, ioaddr+DAYNA_INT_CARD);
0906 
0907     spin_unlock_irqrestore(&lp->lock, flags);   /* Restore interrupts. */
0908 
0909     /* Done sending packet, update counters and cleanup. */
0910     dev->stats.tx_packets++;
0911     dev->stats.tx_bytes += skb->len;
0912     dev_kfree_skb (skb);
0913     return NETDEV_TX_OK;
0914 }
0915 
0916 /*
0917  *  Dummy function to keep the Appletalk layer happy.
0918  */
0919  
0920 static void set_multicast_list(struct net_device *dev)
0921 {
0922         if(cops_debug >= 3)
0923         printk("%s: set_multicast_list executed\n", dev->name);
0924 }
0925 
0926 /*
0927  *      System ioctls for the COPS LocalTalk card.
0928  */
0929  
0930 static int cops_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
0931 {
0932         struct cops_local *lp = netdev_priv(dev);
0933         struct sockaddr_at *sa = (struct sockaddr_at *)&ifr->ifr_addr;
0934         struct atalk_addr *aa = &lp->node_addr;
0935 
0936         switch(cmd)
0937         {
0938                 case SIOCSIFADDR:
0939             /* Get and set the nodeid and network # atalkd wants. */
0940             cops_nodeid(dev, sa->sat_addr.s_node);
0941             aa->s_net               = sa->sat_addr.s_net;
0942                         aa->s_node              = lp->node_acquire;
0943 
0944             /* Set broardcast address. */
0945                         dev->broadcast[0]       = 0xFF;
0946             
0947             /* Set hardware address. */
0948                         dev->addr_len           = 1;
0949             dev_addr_set(dev, &aa->s_node);
0950                         return 0;
0951 
0952                 case SIOCGIFADDR:
0953                         sa->sat_addr.s_net      = aa->s_net;
0954                         sa->sat_addr.s_node     = aa->s_node;
0955                         return 0;
0956 
0957                 default:
0958                         return -EOPNOTSUPP;
0959         }
0960 }
0961 
0962 /*
0963  *  The inverse routine to cops_open().
0964  */
0965  
0966 static int cops_close(struct net_device *dev)
0967 {
0968     struct cops_local *lp = netdev_priv(dev);
0969 
0970     /* If we were running polled, yank the timer.
0971      */
0972     if(lp->board==TANGENT && dev->irq==0)
0973         del_timer(&cops_timer);
0974 
0975     netif_stop_queue(dev);
0976         return 0;
0977 }
0978 
0979 
0980 #ifdef MODULE
0981 static struct net_device *cops_dev;
0982 
0983 MODULE_LICENSE("GPL");
0984 module_param_hw(io, int, ioport, 0);
0985 module_param_hw(irq, int, irq, 0);
0986 module_param_hw(board_type, int, other, 0);
0987 
0988 static int __init cops_module_init(void)
0989 {
0990     if (io == 0)
0991         printk(KERN_WARNING "%s: You shouldn't autoprobe with insmod\n",
0992             cardname);
0993     cops_dev = cops_probe(-1);
0994     return PTR_ERR_OR_ZERO(cops_dev);
0995 }
0996 
0997 static void __exit cops_module_exit(void)
0998 {
0999     unregister_netdev(cops_dev);
1000     cleanup_card(cops_dev);
1001     free_netdev(cops_dev);
1002 }
1003 module_init(cops_module_init);
1004 module_exit(cops_module_exit);
1005 #endif /* MODULE */