xref: /openbmc/linux/drivers/net/appletalk/cops.c (revision 87c2ce3b)
1 /*      cops.c: LocalTalk driver for Linux.
2  *
3  *	Authors:
4  *      - Jay Schulist <jschlst@samba.org>
5  *
6  *	With more than a little help from;
7  *	- Alan Cox <Alan.Cox@linux.org>
8  *
9  *      Derived from:
10  *      - skeleton.c: A network driver outline for linux.
11  *        Written 1993-94 by Donald Becker.
12  *	- ltpc.c: A driver for the LocalTalk PC card.
13  *	  Written by Bradford W. Johnson.
14  *
15  *      Copyright 1993 United States Government as represented by the
16  *      Director, National Security Agency.
17  *
18  *      This software may be used and distributed according to the terms
19  *      of the GNU General Public License, incorporated herein by reference.
20  *
21  *	Changes:
22  *	19970608	Alan Cox	Allowed dual card type support
23  *					Can set board type in insmod
24  *					Hooks for cops_setup routine
25  *					(not yet implemented).
26  *	19971101	Jay Schulist	Fixes for multiple lt* devices.
27  *	19980607	Steven Hirsch	Fixed the badly broken support
28  *					for Tangent type cards. Only
29  *                                      tested on Daystar LT200. Some
30  *                                      cleanup of formatting and program
31  *                                      logic.  Added emacs 'local-vars'
32  *                                      setup for Jay's brace style.
33  *	20000211	Alan Cox	Cleaned up for softnet
34  */
35 
36 static const char *version =
37 "cops.c:v0.04 6/7/98 Jay Schulist <jschlst@samba.org>\n";
38 /*
39  *  Sources:
40  *      COPS Localtalk SDK. This provides almost all of the information
41  *      needed.
42  */
43 
44 /*
45  * insmod/modprobe configurable stuff.
46  *	- IO Port, choose one your card supports or 0 if you dare.
47  *	- IRQ, also choose one your card supports or nothing and let
48  *	  the driver figure it out.
49  */
50 
51 #include <linux/config.h>
52 #include <linux/module.h>
53 #include <linux/kernel.h>
54 #include <linux/types.h>
55 #include <linux/fcntl.h>
56 #include <linux/interrupt.h>
57 #include <linux/ptrace.h>
58 #include <linux/ioport.h>
59 #include <linux/in.h>
60 #include <linux/slab.h>
61 #include <linux/string.h>
62 #include <linux/errno.h>
63 #include <linux/init.h>
64 #include <linux/netdevice.h>
65 #include <linux/etherdevice.h>
66 #include <linux/skbuff.h>
67 #include <linux/if_arp.h>
68 #include <linux/if_ltalk.h>
69 #include <linux/delay.h>	/* For udelay() */
70 #include <linux/atalk.h>
71 #include <linux/spinlock.h>
72 #include <linux/bitops.h>
73 
74 #include <asm/system.h>
75 #include <asm/io.h>
76 #include <asm/dma.h>
77 
78 #include "cops.h"		/* Our Stuff */
79 #include "cops_ltdrv.h"		/* Firmware code for Tangent type cards. */
80 #include "cops_ffdrv.h"		/* Firmware code for Dayna type cards. */
81 
82 /*
83  *      The name of the card. Is used for messages and in the requests for
84  *      io regions, irqs and dma channels
85  */
86 
87 static const char *cardname = "cops";
88 
89 #ifdef CONFIG_COPS_DAYNA
90 static int board_type = DAYNA;	/* Module exported */
91 #else
92 static int board_type = TANGENT;
93 #endif
94 
95 static int io = 0x240;		/* Default IO for Dayna */
96 static int irq = 5;		/* Default IRQ */
97 
98 /*
99  *	COPS Autoprobe information.
100  *	Right now if port address is right but IRQ is not 5 this will
101  *      return a 5 no matter what since we will still get a status response.
102  *      Need one more additional check to narrow down after we have gotten
103  *      the ioaddr. But since only other possible IRQs is 3 and 4 so no real
104  *	hurry on this. I *STRONGLY* recommend using IRQ 5 for your card with
105  *	this driver.
106  *
107  *	This driver has 2 modes and they are: Dayna mode and Tangent mode.
108  *	Each mode corresponds with the type of card. It has been found
109  *	that there are 2 main types of cards and all other cards are
110  *	the same and just have different names or only have minor differences
111  *	such as more IO ports. As this driver is tested it will
112  *	become more clear on exactly what cards are supported. The driver
113  *	defaults to using Dayna mode. To change the drivers mode, simply
114  *	select Dayna or Tangent mode when configuring the kernel.
115  *
116  *      This driver should support:
117  *      TANGENT driver mode:
118  *              Tangent ATB-II, Novell NL-1000, Daystar Digital LT-200,
119  *		COPS LT-1
120  *      DAYNA driver mode:
121  *              Dayna DL2000/DaynaTalk PC (Half Length), COPS LT-95,
122  *		Farallon PhoneNET PC III, Farallon PhoneNET PC II
123  *	Other cards possibly supported mode unkown though:
124  *		Dayna DL2000 (Full length), COPS LT/M (Micro-Channel)
125  *
126  *	Cards NOT supported by this driver but supported by the ltpc.c
127  *	driver written by Bradford W. Johnson <johns393@maroon.tc.umn.edu>
128  *		Farallon PhoneNET PC
129  *		Original Apple LocalTalk PC card
130  *
131  *      N.B.
132  *
133  *      The Daystar Digital LT200 boards do not support interrupt-driven
134  *      IO.  You must specify 'irq=0xff' as a module parameter to invoke
135  *      polled mode.  I also believe that the port probing logic is quite
136  *      dangerous at best and certainly hopeless for a polled card.  Best to
137  *      specify both. - Steve H.
138  *
139  */
140 
141 /*
142  * Zero terminated list of IO ports to probe.
143  */
144 
145 static unsigned int ports[] = {
146 	0x240, 0x340, 0x200, 0x210, 0x220, 0x230, 0x260,
147 	0x2A0, 0x300, 0x310, 0x320, 0x330, 0x350, 0x360,
148 	0
149 };
150 
151 /*
152  * Zero terminated list of IRQ ports to probe.
153  */
154 
155 static int cops_irqlist[] = {
156 	5, 4, 3, 0
157 };
158 
159 static struct timer_list cops_timer;
160 
161 /* use 0 for production, 1 for verification, 2 for debug, 3 for verbose debug */
162 #ifndef COPS_DEBUG
163 #define COPS_DEBUG 1
164 #endif
165 static unsigned int cops_debug = COPS_DEBUG;
166 
167 /* The number of low I/O ports used by the card. */
168 #define COPS_IO_EXTENT       8
169 
170 /* Information that needs to be kept for each board. */
171 
172 struct cops_local
173 {
174         struct net_device_stats stats;
175         int board;			/* Holds what board type is. */
176 	int nodeid;			/* Set to 1 once have nodeid. */
177         unsigned char node_acquire;	/* Node ID when acquired. */
178         struct atalk_addr node_addr;	/* Full node address */
179 	spinlock_t lock;		/* RX/TX lock */
180 };
181 
182 /* Index to functions, as function prototypes. */
183 static int  cops_probe1 (struct net_device *dev, int ioaddr);
184 static int  cops_irq (int ioaddr, int board);
185 
186 static int  cops_open (struct net_device *dev);
187 static int  cops_jumpstart (struct net_device *dev);
188 static void cops_reset (struct net_device *dev, int sleep);
189 static void cops_load (struct net_device *dev);
190 static int  cops_nodeid (struct net_device *dev, int nodeid);
191 
192 static irqreturn_t cops_interrupt (int irq, void *dev_id, struct pt_regs *regs);
193 static void cops_poll (unsigned long ltdev);
194 static void cops_timeout(struct net_device *dev);
195 static void cops_rx (struct net_device *dev);
196 static int  cops_send_packet (struct sk_buff *skb, struct net_device *dev);
197 static void set_multicast_list (struct net_device *dev);
198 static int  cops_hard_header (struct sk_buff *skb, struct net_device *dev,
199 			      unsigned short type, void *daddr, void *saddr,
200 			      unsigned len);
201 
202 static int  cops_ioctl (struct net_device *dev, struct ifreq *rq, int cmd);
203 static int  cops_close (struct net_device *dev);
204 static struct net_device_stats *cops_get_stats (struct net_device *dev);
205 
206 static void cleanup_card(struct net_device *dev)
207 {
208 	if (dev->irq)
209 		free_irq(dev->irq, dev);
210 	release_region(dev->base_addr, COPS_IO_EXTENT);
211 }
212 
213 /*
214  *      Check for a network adaptor of this type, and return '0' iff one exists.
215  *      If dev->base_addr == 0, probe all likely locations.
216  *      If dev->base_addr in [1..0x1ff], always return failure.
217  *        otherwise go with what we pass in.
218  */
219 struct net_device * __init cops_probe(int unit)
220 {
221 	struct net_device *dev;
222 	unsigned *port;
223 	int base_addr;
224 	int err = 0;
225 
226 	dev = alloc_ltalkdev(sizeof(struct cops_local));
227 	if (!dev)
228 		return ERR_PTR(-ENOMEM);
229 
230 	if (unit >= 0) {
231 		sprintf(dev->name, "lt%d", unit);
232 		netdev_boot_setup_check(dev);
233 		irq = dev->irq;
234 		base_addr = dev->base_addr;
235 	} else {
236 		base_addr = dev->base_addr = io;
237 	}
238 
239 	SET_MODULE_OWNER(dev);
240 
241 	if (base_addr > 0x1ff) {    /* Check a single specified location. */
242 		err = cops_probe1(dev, base_addr);
243 	} else if (base_addr != 0) { /* Don't probe at all. */
244 		err = -ENXIO;
245 	} else {
246 		/* FIXME  Does this really work for cards which generate irq?
247 		 * It's definitely N.G. for polled Tangent. sh
248 		 * Dayna cards don't autoprobe well at all, but if your card is
249 		 * at IRQ 5 & IO 0x240 we find it every time. ;) JS
250 		 */
251 		for (port = ports; *port && cops_probe1(dev, *port) < 0; port++)
252 			;
253 		if (!*port)
254 			err = -ENODEV;
255 	}
256 	if (err)
257 		goto out;
258 	err = register_netdev(dev);
259 	if (err)
260 		goto out1;
261 	return dev;
262 out1:
263 	cleanup_card(dev);
264 out:
265 	free_netdev(dev);
266 	return ERR_PTR(err);
267 }
268 
269 /*
270  *      This is the real probe routine. Linux has a history of friendly device
271  *      probes on the ISA bus. A good device probes avoids doing writes, and
272  *      verifies that the correct device exists and functions.
273  */
274 static int __init cops_probe1(struct net_device *dev, int ioaddr)
275 {
276         struct cops_local *lp;
277 	static unsigned version_printed;
278 	int board = board_type;
279 	int retval;
280 
281         if(cops_debug && version_printed++ == 0)
282 		printk("%s", version);
283 
284 	/* Grab the region so no one else tries to probe our ioports. */
285 	if (!request_region(ioaddr, COPS_IO_EXTENT, dev->name))
286 		return -EBUSY;
287 
288         /*
289          * Since this board has jumpered interrupts, allocate the interrupt
290          * vector now. There is no point in waiting since no other device
291          * can use the interrupt, and this marks the irq as busy. Jumpered
292          * interrupts are typically not reported by the boards, and we must
293          * used AutoIRQ to find them.
294 	 */
295 	dev->irq = irq;
296 	switch (dev->irq)
297 	{
298 		case 0:
299 			/* COPS AutoIRQ routine */
300 			dev->irq = cops_irq(ioaddr, board);
301 			if (dev->irq)
302 				break;
303 			/* No IRQ found on this port, fallthrough */
304 		case 1:
305 			retval = -EINVAL;
306 			goto err_out;
307 
308 		/* Fixup for users that don't know that IRQ 2 is really
309 		 * IRQ 9, or don't know which one to set.
310 		 */
311 		case 2:
312 			dev->irq = 9;
313 			break;
314 
315 		/* Polled operation requested. Although irq of zero passed as
316 		 * a parameter tells the init routines to probe, we'll
317 		 * overload it to denote polled operation at runtime.
318 		 */
319 		case 0xff:
320 			dev->irq = 0;
321 			break;
322 
323 		default:
324 			break;
325 	}
326 
327 	/* Reserve any actual interrupt. */
328 	if (dev->irq) {
329 		retval = request_irq(dev->irq, &cops_interrupt, 0, dev->name, dev);
330 		if (retval)
331 			goto err_out;
332 	}
333 
334 	dev->base_addr = ioaddr;
335 
336         lp = netdev_priv(dev);
337         memset(lp, 0, sizeof(struct cops_local));
338         spin_lock_init(&lp->lock);
339 
340 	/* Copy local board variable to lp struct. */
341 	lp->board               = board;
342 
343 	dev->hard_start_xmit    = cops_send_packet;
344 	dev->tx_timeout		= cops_timeout;
345 	dev->watchdog_timeo	= HZ * 2;
346 	dev->hard_header	= cops_hard_header;
347         dev->get_stats          = cops_get_stats;
348 	dev->open               = cops_open;
349         dev->stop               = cops_close;
350         dev->do_ioctl           = cops_ioctl;
351 	dev->set_multicast_list = set_multicast_list;
352         dev->mc_list            = NULL;
353 
354 	/* Tell the user where the card is and what mode we're in. */
355 	if(board==DAYNA)
356 		printk("%s: %s at %#3x, using IRQ %d, in Dayna mode.\n",
357 			dev->name, cardname, ioaddr, dev->irq);
358 	if(board==TANGENT) {
359 		if(dev->irq)
360 			printk("%s: %s at %#3x, IRQ %d, in Tangent mode\n",
361 				dev->name, cardname, ioaddr, dev->irq);
362 		else
363 			printk("%s: %s at %#3x, using polled IO, in Tangent mode.\n",
364 				dev->name, cardname, ioaddr);
365 
366 	}
367         return 0;
368 
369 err_out:
370 	release_region(ioaddr, COPS_IO_EXTENT);
371 	return retval;
372 }
373 
374 static int __init cops_irq (int ioaddr, int board)
375 {       /*
376          * This does not use the IRQ to determine where the IRQ is. We just
377          * assume that when we get a correct status response that it's the IRQ.
378          * This really just verifies the IO port but since we only have access
379          * to such a small number of IRQs (5, 4, 3) this is not bad.
380          * This will probably not work for more than one card.
381          */
382         int irqaddr=0;
383         int i, x, status;
384 
385         if(board==DAYNA)
386         {
387                 outb(0, ioaddr+DAYNA_RESET);
388                 inb(ioaddr+DAYNA_RESET);
389                 mdelay(333);
390         }
391         if(board==TANGENT)
392         {
393                 inb(ioaddr);
394                 outb(0, ioaddr);
395                 outb(0, ioaddr+TANG_RESET);
396         }
397 
398         for(i=0; cops_irqlist[i] !=0; i++)
399         {
400                 irqaddr = cops_irqlist[i];
401                 for(x = 0xFFFF; x>0; x --)    /* wait for response */
402                 {
403                         if(board==DAYNA)
404                         {
405                                 status = (inb(ioaddr+DAYNA_CARD_STATUS)&3);
406                                 if(status == 1)
407                                         return irqaddr;
408                         }
409                         if(board==TANGENT)
410                         {
411                                 if((inb(ioaddr+TANG_CARD_STATUS)& TANG_TX_READY) !=0)
412                                         return irqaddr;
413                         }
414                 }
415         }
416         return 0;       /* no IRQ found */
417 }
418 
419 /*
420  * Open/initialize the board. This is called (in the current kernel)
421  * sometime after booting when the 'ifconfig' program is run.
422  */
423 static int cops_open(struct net_device *dev)
424 {
425     struct cops_local *lp = netdev_priv(dev);
426 
427 	if(dev->irq==0)
428 	{
429 		/*
430 		 * I don't know if the Dayna-style boards support polled
431 		 * operation.  For now, only allow it for Tangent.
432 		 */
433 		if(lp->board==TANGENT)	/* Poll 20 times per second */
434 		{
435 		    init_timer(&cops_timer);
436 		    cops_timer.function = cops_poll;
437 		    cops_timer.data 	= (unsigned long)dev;
438 		    cops_timer.expires 	= jiffies + HZ/20;
439 		    add_timer(&cops_timer);
440 		}
441 		else
442 		{
443 			printk(KERN_WARNING "%s: No irq line set\n", dev->name);
444 			return -EAGAIN;
445 		}
446 	}
447 
448 	cops_jumpstart(dev);	/* Start the card up. */
449 
450 	netif_start_queue(dev);
451         return 0;
452 }
453 
454 /*
455  *	This allows for a dynamic start/restart of the entire card.
456  */
457 static int cops_jumpstart(struct net_device *dev)
458 {
459 	struct cops_local *lp = netdev_priv(dev);
460 
461 	/*
462          *      Once the card has the firmware loaded and has acquired
463          *      the nodeid, if it is reset it will lose it all.
464          */
465         cops_reset(dev,1);	/* Need to reset card before load firmware. */
466         cops_load(dev);		/* Load the firmware. */
467 
468 	/*
469 	 *	If atalkd already gave us a nodeid we will use that
470 	 *	one again, else we wait for atalkd to give us a nodeid
471 	 *	in cops_ioctl. This may cause a problem if someone steals
472 	 *	our nodeid while we are resetting.
473 	 */
474 	if(lp->nodeid == 1)
475 		cops_nodeid(dev,lp->node_acquire);
476 
477 	return 0;
478 }
479 
480 static void tangent_wait_reset(int ioaddr)
481 {
482 	int timeout=0;
483 
484 	while(timeout++ < 5 && (inb(ioaddr+TANG_CARD_STATUS)&TANG_TX_READY)==0)
485 		mdelay(1);   /* Wait 1 second */
486 }
487 
488 /*
489  *      Reset the LocalTalk board.
490  */
491 static void cops_reset(struct net_device *dev, int sleep)
492 {
493         struct cops_local *lp = netdev_priv(dev);
494         int ioaddr=dev->base_addr;
495 
496         if(lp->board==TANGENT)
497         {
498                 inb(ioaddr);		/* Clear request latch. */
499                 outb(0,ioaddr);		/* Clear the TANG_TX_READY flop. */
500                 outb(0, ioaddr+TANG_RESET);	/* Reset the adapter. */
501 
502 		tangent_wait_reset(ioaddr);
503                 outb(0, ioaddr+TANG_CLEAR_INT);
504         }
505         if(lp->board==DAYNA)
506         {
507                 outb(0, ioaddr+DAYNA_RESET);	/* Assert the reset port */
508                 inb(ioaddr+DAYNA_RESET);	/* Clear the reset */
509                 if(sleep)
510                 {
511                         long snap=jiffies;
512 
513 			/* Let card finish initializing, about 1/3 second */
514 	                while(jiffies-snap<HZ/3)
515                                 schedule();
516                 }
517                 else
518                         mdelay(333);
519         }
520 	netif_wake_queue(dev);
521 	return;
522 }
523 
524 static void cops_load (struct net_device *dev)
525 {
526         struct ifreq ifr;
527         struct ltfirmware *ltf= (struct ltfirmware *)&ifr.ifr_ifru;
528         struct cops_local *lp = netdev_priv(dev);
529         int ioaddr=dev->base_addr;
530 	int length, i = 0;
531 
532         strcpy(ifr.ifr_name,"lt0");
533 
534         /* Get card's firmware code and do some checks on it. */
535 #ifdef CONFIG_COPS_DAYNA
536         if(lp->board==DAYNA)
537         {
538                 ltf->length=sizeof(ffdrv_code);
539                 ltf->data=ffdrv_code;
540         }
541         else
542 #endif
543 #ifdef CONFIG_COPS_TANGENT
544         if(lp->board==TANGENT)
545         {
546                 ltf->length=sizeof(ltdrv_code);
547                 ltf->data=ltdrv_code;
548         }
549         else
550 #endif
551 	{
552 		printk(KERN_INFO "%s; unsupported board type.\n", dev->name);
553 		return;
554 	}
555 
556         /* Check to make sure firmware is correct length. */
557         if(lp->board==DAYNA && ltf->length!=5983)
558         {
559                 printk(KERN_WARNING "%s: Firmware is not length of FFDRV.BIN.\n", dev->name);
560                 return;
561         }
562         if(lp->board==TANGENT && ltf->length!=2501)
563         {
564                 printk(KERN_WARNING "%s: Firmware is not length of DRVCODE.BIN.\n", dev->name);
565                 return;
566         }
567 
568         if(lp->board==DAYNA)
569         {
570                 /*
571                  *      We must wait for a status response
572                  *      with the DAYNA board.
573                  */
574                 while(++i<65536)
575                 {
576                        if((inb(ioaddr+DAYNA_CARD_STATUS)&3)==1)
577                                 break;
578                 }
579 
580                 if(i==65536)
581                         return;
582         }
583 
584         /*
585          *      Upload the firmware and kick. Byte-by-byte works nicely here.
586          */
587 	i=0;
588         length = ltf->length;
589         while(length--)
590         {
591                 outb(ltf->data[i], ioaddr);
592                 i++;
593         }
594 
595 	if(cops_debug > 1)
596 		printk("%s: Uploaded firmware - %d bytes of %d bytes.\n",
597 			dev->name, i, ltf->length);
598 
599         if(lp->board==DAYNA) 	/* Tell Dayna to run the firmware code. */
600                 outb(1, ioaddr+DAYNA_INT_CARD);
601 	else			/* Tell Tang to run the firmware code. */
602 		inb(ioaddr);
603 
604         if(lp->board==TANGENT)
605         {
606                 tangent_wait_reset(ioaddr);
607                 inb(ioaddr);	/* Clear initial ready signal. */
608         }
609 
610         return;
611 }
612 
613 /*
614  * 	Get the LocalTalk Nodeid from the card. We can suggest
615  *	any nodeid 1-254. The card will try and get that exact
616  *	address else we can specify 0 as the nodeid and the card
617  *	will autoprobe for a nodeid.
618  */
619 static int cops_nodeid (struct net_device *dev, int nodeid)
620 {
621 	struct cops_local *lp = netdev_priv(dev);
622 	int ioaddr = dev->base_addr;
623 
624 	if(lp->board == DAYNA)
625         {
626         	/* Empty any pending adapter responses. */
627                 while((inb(ioaddr+DAYNA_CARD_STATUS)&DAYNA_TX_READY)==0)
628                 {
629 			outb(0, ioaddr+COPS_CLEAR_INT);	/* Clear interrupts. */
630         		if((inb(ioaddr+DAYNA_CARD_STATUS)&0x03)==DAYNA_RX_REQUEST)
631                 		cops_rx(dev);	/* Kick any packets waiting. */
632 			schedule();
633                 }
634 
635                 outb(2, ioaddr);       	/* Output command packet length as 2. */
636                 outb(0, ioaddr);
637                 outb(LAP_INIT, ioaddr);	/* Send LAP_INIT command byte. */
638                 outb(nodeid, ioaddr);  	/* Suggest node address. */
639         }
640 
641 	if(lp->board == TANGENT)
642         {
643                 /* Empty any pending adapter responses. */
644                 while(inb(ioaddr+TANG_CARD_STATUS)&TANG_RX_READY)
645                 {
646 			outb(0, ioaddr+COPS_CLEAR_INT);	/* Clear interrupt. */
647                 	cops_rx(dev);          	/* Kick out packets waiting. */
648 			schedule();
649                 }
650 
651 		/* Not sure what Tangent does if nodeid picked is used. */
652                 if(nodeid == 0)	         		/* Seed. */
653                 	nodeid = jiffies&0xFF;		/* Get a random try */
654                 outb(2, ioaddr);        		/* Command length LSB */
655                 outb(0, ioaddr);       			/* Command length MSB */
656                 outb(LAP_INIT, ioaddr); 		/* Send LAP_INIT byte */
657                 outb(nodeid, ioaddr); 		  	/* LAP address hint. */
658                 outb(0xFF, ioaddr);     		/* Int. level to use */
659         }
660 
661 	lp->node_acquire=0;		/* Set nodeid holder to 0. */
662         while(lp->node_acquire==0)	/* Get *True* nodeid finally. */
663 	{
664 		outb(0, ioaddr+COPS_CLEAR_INT);	/* Clear any interrupt. */
665 
666 		if(lp->board == DAYNA)
667 		{
668                 	if((inb(ioaddr+DAYNA_CARD_STATUS)&0x03)==DAYNA_RX_REQUEST)
669                 		cops_rx(dev);	/* Grab the nodeid put in lp->node_acquire. */
670 		}
671 		if(lp->board == TANGENT)
672 		{
673 			if(inb(ioaddr+TANG_CARD_STATUS)&TANG_RX_READY)
674                                 cops_rx(dev);   /* Grab the nodeid put in lp->node_acquire. */
675 		}
676 		schedule();
677 	}
678 
679 	if(cops_debug > 1)
680 		printk(KERN_DEBUG "%s: Node ID %d has been acquired.\n",
681 			dev->name, lp->node_acquire);
682 
683 	lp->nodeid=1;	/* Set got nodeid to 1. */
684 
685         return 0;
686 }
687 
688 /*
689  *	Poll the Tangent type cards to see if we have work.
690  */
691 
692 static void cops_poll(unsigned long ltdev)
693 {
694 	int ioaddr, status;
695 	int boguscount = 0;
696 
697 	struct net_device *dev = (struct net_device *)ltdev;
698 
699 	del_timer(&cops_timer);
700 
701 	if(dev == NULL)
702 		return;	/* We've been downed */
703 
704 	ioaddr = dev->base_addr;
705 	do {
706 		status=inb(ioaddr+TANG_CARD_STATUS);
707 		if(status & TANG_RX_READY)
708 			cops_rx(dev);
709 		if(status & TANG_TX_READY)
710 			netif_wake_queue(dev);
711 		status = inb(ioaddr+TANG_CARD_STATUS);
712 	} while((++boguscount < 20) && (status&(TANG_RX_READY|TANG_TX_READY)));
713 
714 	/* poll 20 times per second */
715 	cops_timer.expires = jiffies + HZ/20;
716 	add_timer(&cops_timer);
717 
718 	return;
719 }
720 
721 /*
722  *      The typical workload of the driver:
723  *      Handle the network interface interrupts.
724  */
725 static irqreturn_t cops_interrupt(int irq, void *dev_id, struct pt_regs * regs)
726 {
727         struct net_device *dev = dev_id;
728         struct cops_local *lp;
729         int ioaddr, status;
730         int boguscount = 0;
731 
732         ioaddr = dev->base_addr;
733         lp = netdev_priv(dev);
734 
735 	if(lp->board==DAYNA)
736 	{
737 		do {
738 			outb(0, ioaddr + COPS_CLEAR_INT);
739                        	status=inb(ioaddr+DAYNA_CARD_STATUS);
740                        	if((status&0x03)==DAYNA_RX_REQUEST)
741                        	        cops_rx(dev);
742                 	netif_wake_queue(dev);
743 		} while(++boguscount < 20);
744 	}
745 	else
746 	{
747 		do {
748                        	status=inb(ioaddr+TANG_CARD_STATUS);
749 			if(status & TANG_RX_READY)
750 				cops_rx(dev);
751 			if(status & TANG_TX_READY)
752 				netif_wake_queue(dev);
753 			status=inb(ioaddr+TANG_CARD_STATUS);
754 		} while((++boguscount < 20) && (status&(TANG_RX_READY|TANG_TX_READY)));
755 	}
756 
757         return IRQ_HANDLED;
758 }
759 
760 /*
761  *      We have a good packet(s), get it/them out of the buffers.
762  */
763 static void cops_rx(struct net_device *dev)
764 {
765         int pkt_len = 0;
766         int rsp_type = 0;
767         struct sk_buff *skb = NULL;
768         struct cops_local *lp = netdev_priv(dev);
769         int ioaddr = dev->base_addr;
770         int boguscount = 0;
771         unsigned long flags;
772 
773 
774 	spin_lock_irqsave(&lp->lock, flags);
775 
776         if(lp->board==DAYNA)
777         {
778                 outb(0, ioaddr);                /* Send out Zero length. */
779                 outb(0, ioaddr);
780                 outb(DATA_READ, ioaddr);        /* Send read command out. */
781 
782                 /* Wait for DMA to turn around. */
783                 while(++boguscount<1000000)
784                 {
785 			barrier();
786                         if((inb(ioaddr+DAYNA_CARD_STATUS)&0x03)==DAYNA_RX_READY)
787                                 break;
788                 }
789 
790                 if(boguscount==1000000)
791                 {
792                         printk(KERN_WARNING "%s: DMA timed out.\n",dev->name);
793 			spin_unlock_irqrestore(&lp->lock, flags);
794                         return;
795                 }
796         }
797 
798         /* Get response length. */
799 	if(lp->board==DAYNA)
800         	pkt_len = inb(ioaddr) & 0xFF;
801 	else
802 		pkt_len = inb(ioaddr) & 0x00FF;
803         pkt_len |= (inb(ioaddr) << 8);
804         /* Input IO code. */
805         rsp_type=inb(ioaddr);
806 
807         /* Malloc up new buffer. */
808         skb = dev_alloc_skb(pkt_len);
809         if(skb == NULL)
810         {
811                 printk(KERN_WARNING "%s: Memory squeeze, dropping packet.\n",
812 			dev->name);
813                 lp->stats.rx_dropped++;
814                 while(pkt_len--)        /* Discard packet */
815                         inb(ioaddr);
816                 spin_unlock_irqrestore(&lp->lock, flags);
817                 return;
818         }
819         skb->dev = dev;
820         skb_put(skb, pkt_len);
821         skb->protocol = htons(ETH_P_LOCALTALK);
822 
823         insb(ioaddr, skb->data, pkt_len);               /* Eat the Data */
824 
825         if(lp->board==DAYNA)
826                 outb(1, ioaddr+DAYNA_INT_CARD);         /* Interrupt the card */
827 
828         spin_unlock_irqrestore(&lp->lock, flags);  /* Restore interrupts. */
829 
830         /* Check for bad response length */
831         if(pkt_len < 0 || pkt_len > MAX_LLAP_SIZE)
832         {
833 		printk(KERN_WARNING "%s: Bad packet length of %d bytes.\n",
834 			dev->name, pkt_len);
835                 lp->stats.tx_errors++;
836                 dev_kfree_skb_any(skb);
837                 return;
838         }
839 
840         /* Set nodeid and then get out. */
841         if(rsp_type == LAP_INIT_RSP)
842         {	/* Nodeid taken from received packet. */
843                 lp->node_acquire = skb->data[0];
844                 dev_kfree_skb_any(skb);
845                 return;
846         }
847 
848         /* One last check to make sure we have a good packet. */
849         if(rsp_type != LAP_RESPONSE)
850         {
851                 printk(KERN_WARNING "%s: Bad packet type %d.\n", dev->name, rsp_type);
852                 lp->stats.tx_errors++;
853                 dev_kfree_skb_any(skb);
854                 return;
855         }
856 
857         skb->mac.raw    = skb->data;    /* Point to entire packet. */
858         skb_pull(skb,3);
859         skb->h.raw      = skb->data;    /* Point to data (Skip header). */
860 
861         /* Update the counters. */
862         lp->stats.rx_packets++;
863         lp->stats.rx_bytes += skb->len;
864 
865         /* Send packet to a higher place. */
866         netif_rx(skb);
867 	dev->last_rx = jiffies;
868 }
869 
870 static void cops_timeout(struct net_device *dev)
871 {
872         struct cops_local *lp = netdev_priv(dev);
873         int ioaddr = dev->base_addr;
874 
875 	lp->stats.tx_errors++;
876         if(lp->board==TANGENT)
877         {
878 		if((inb(ioaddr+TANG_CARD_STATUS)&TANG_TX_READY)==0)
879                		printk(KERN_WARNING "%s: No TX complete interrupt.\n", dev->name);
880 	}
881 	printk(KERN_WARNING "%s: Transmit timed out.\n", dev->name);
882 	cops_jumpstart(dev);	/* Restart the card. */
883 	dev->trans_start = jiffies;
884 	netif_wake_queue(dev);
885 }
886 
887 
888 /*
889  *	Make the card transmit a LocalTalk packet.
890  */
891 
892 static int cops_send_packet(struct sk_buff *skb, struct net_device *dev)
893 {
894         struct cops_local *lp = netdev_priv(dev);
895         int ioaddr = dev->base_addr;
896         unsigned long flags;
897 
898         /*
899          * Block a timer-based transmit from overlapping.
900 	 */
901 
902 	netif_stop_queue(dev);
903 
904 	spin_lock_irqsave(&lp->lock, flags);
905 	if(lp->board == DAYNA)	 /* Wait for adapter transmit buffer. */
906 		while((inb(ioaddr+DAYNA_CARD_STATUS)&DAYNA_TX_READY)==0)
907 			cpu_relax();
908 	if(lp->board == TANGENT) /* Wait for adapter transmit buffer. */
909 		while((inb(ioaddr+TANG_CARD_STATUS)&TANG_TX_READY)==0)
910 			cpu_relax();
911 
912 	/* Output IO length. */
913 	outb(skb->len, ioaddr);
914 	if(lp->board == DAYNA)
915                	outb(skb->len >> 8, ioaddr);
916 	else
917 		outb((skb->len >> 8)&0x0FF, ioaddr);
918 
919 	/* Output IO code. */
920 	outb(LAP_WRITE, ioaddr);
921 
922 	if(lp->board == DAYNA)	/* Check the transmit buffer again. */
923         	while((inb(ioaddr+DAYNA_CARD_STATUS)&DAYNA_TX_READY)==0);
924 
925 	outsb(ioaddr, skb->data, skb->len);	/* Send out the data. */
926 
927 	if(lp->board==DAYNA)	/* Dayna requires you kick the card */
928 		outb(1, ioaddr+DAYNA_INT_CARD);
929 
930 	spin_unlock_irqrestore(&lp->lock, flags);	/* Restore interrupts. */
931 
932 	/* Done sending packet, update counters and cleanup. */
933 	lp->stats.tx_packets++;
934 	lp->stats.tx_bytes += skb->len;
935 	dev->trans_start = jiffies;
936 	dev_kfree_skb (skb);
937         return 0;
938 }
939 
940 /*
941  *	Dummy function to keep the Appletalk layer happy.
942  */
943 
944 static void set_multicast_list(struct net_device *dev)
945 {
946         if(cops_debug >= 3)
947 		printk("%s: set_multicast_list executed\n", dev->name);
948 }
949 
950 /*
951  *      Another Dummy function to keep the Appletalk layer happy.
952  */
953 
954 static int cops_hard_header(struct sk_buff *skb, struct net_device *dev,
955 			    unsigned short type, void *daddr, void *saddr,
956 			    unsigned len)
957 {
958         if(cops_debug >= 3)
959                 printk("%s: cops_hard_header executed. Wow!\n", dev->name);
960         return 0;
961 }
962 
963 /*
964  *      System ioctls for the COPS LocalTalk card.
965  */
966 
967 static int cops_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
968 {
969         struct cops_local *lp = netdev_priv(dev);
970         struct sockaddr_at *sa = (struct sockaddr_at *)&ifr->ifr_addr;
971         struct atalk_addr *aa = (struct atalk_addr *)&lp->node_addr;
972 
973         switch(cmd)
974         {
975                 case SIOCSIFADDR:
976 			/* Get and set the nodeid and network # atalkd wants. */
977 			cops_nodeid(dev, sa->sat_addr.s_node);
978 			aa->s_net               = sa->sat_addr.s_net;
979                         aa->s_node              = lp->node_acquire;
980 
981 			/* Set broardcast address. */
982                         dev->broadcast[0]       = 0xFF;
983 
984 			/* Set hardware address. */
985                         dev->dev_addr[0]        = aa->s_node;
986                         dev->addr_len           = 1;
987                         return 0;
988 
989                 case SIOCGIFADDR:
990                         sa->sat_addr.s_net      = aa->s_net;
991                         sa->sat_addr.s_node     = aa->s_node;
992                         return 0;
993 
994                 default:
995                         return -EOPNOTSUPP;
996         }
997 }
998 
999 /*
1000  *	The inverse routine to cops_open().
1001  */
1002 
1003 static int cops_close(struct net_device *dev)
1004 {
1005 	struct cops_local *lp = netdev_priv(dev);
1006 
1007 	/* If we were running polled, yank the timer.
1008 	 */
1009 	if(lp->board==TANGENT && dev->irq==0)
1010 		del_timer(&cops_timer);
1011 
1012 	netif_stop_queue(dev);
1013         return 0;
1014 }
1015 
1016 /*
1017  *      Get the current statistics.
1018  *      This may be called with the card open or closed.
1019  */
1020 static struct net_device_stats *cops_get_stats(struct net_device *dev)
1021 {
1022         struct cops_local *lp = netdev_priv(dev);
1023         return &lp->stats;
1024 }
1025 
1026 #ifdef MODULE
1027 static struct net_device *cops_dev;
1028 
1029 MODULE_LICENSE("GPL");
1030 module_param(io, int, 0);
1031 module_param(irq, int, 0);
1032 module_param(board_type, int, 0);
1033 
1034 int init_module(void)
1035 {
1036 	if (io == 0)
1037 		printk(KERN_WARNING "%s: You shouldn't autoprobe with insmod\n",
1038 			cardname);
1039 	cops_dev = cops_probe(-1);
1040 	if (IS_ERR(cops_dev))
1041 		return PTR_ERR(cops_dev);
1042         return 0;
1043 }
1044 
1045 void cleanup_module(void)
1046 {
1047 	unregister_netdev(cops_dev);
1048 	cleanup_card(cops_dev);
1049 	free_netdev(cops_dev);
1050 }
1051 #endif /* MODULE */
1052 
1053 /*
1054  * Local variables:
1055  *  compile-command: "gcc -DMODVERSIONS -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O2 -c cops.c"
1056  *  c-basic-offset: 4
1057  *  c-file-offsets: ((substatement-open . 0))
1058  * End:
1059  */
1060