Linux Audio

Check our new training course

Loading...
v4.17
   1/***    ltpc.c -- a driver for the LocalTalk PC card.
   2 *
   3 *      Copyright (c) 1995,1996 Bradford W. Johnson <johns393@maroon.tc.umn.edu>
   4 *
   5 *      This software may be used and distributed according to the terms
   6 *      of the GNU General Public License, incorporated herein by reference.
   7 *
   8 *      This is ALPHA code at best.  It may not work for you.  It may
   9 *      damage your equipment.  It may damage your relations with other
  10 *      users of your network.  Use it at your own risk!
  11 *
  12 *      Based in part on:
  13 *      skeleton.c      by Donald Becker
  14 *      dummy.c         by Nick Holloway and Alan Cox
  15 *      loopback.c      by Ross Biro, Fred van Kampen, Donald Becker
  16 *      the netatalk source code (UMICH)
  17 *      lots of work on the card...
  18 *
  19 *      I do not have access to the (proprietary) SDK that goes with the card.
  20 *      If you do, I don't want to know about it, and you can probably write
  21 *      a better driver yourself anyway.  This does mean that the pieces that
  22 *      talk to the card are guesswork on my part, so use at your own risk!
  23 *
  24 *      This is my first try at writing Linux networking code, and is also
  25 *      guesswork.  Again, use at your own risk!  (Although on this part, I'd
  26 *      welcome suggestions)
  27 *
  28 *      This is a loadable kernel module which seems to work at my site
  29 *      consisting of a 1.2.13 linux box running netatalk 1.3.3, and with
  30 *      the kernel support from 1.3.3b2 including patches routing.patch
  31 *      and ddp.disappears.from.chooser.  In order to run it, you will need
  32 *      to patch ddp.c and aarp.c in the kernel, but only a little...
  33 *
  34 *      I'm fairly confident that while this is arguably badly written, the
  35 *      problems that people experience will be "higher level", that is, with
  36 *      complications in the netatalk code.  The driver itself doesn't do
  37 *      anything terribly complicated -- it pretends to be an ether device
  38 *      as far as netatalk is concerned, strips the DDP data out of the ether
  39 *      frame and builds a LLAP packet to send out the card.  In the other
  40 *      direction, it receives LLAP frames from the card and builds a fake
  41 *      ether packet that it then tosses up to the networking code.  You can
  42 *      argue (correctly) that this is an ugly way to do things, but it
  43 *      requires a minimal amount of fooling with the code in ddp.c and aarp.c.
  44 *
  45 *      The card will do a lot more than is used here -- I *think* it has the
  46 *      layers up through ATP.  Even if you knew how that part works (which I
  47 *      don't) it would be a big job to carve up the kernel ddp code to insert
  48 *      things at a higher level, and probably a bad idea...
  49 *
  50 *      There are a number of other cards that do LocalTalk on the PC.  If
  51 *      nobody finds any insurmountable (at the netatalk level) problems
  52 *      here, this driver should encourage people to put some work into the
  53 *      other cards (some of which I gather are still commercially available)
  54 *      and also to put hooks for LocalTalk into the official ddp code.
  55 *
  56 *      I welcome comments and suggestions.  This is my first try at Linux
  57 *      networking stuff, and there are probably lots of things that I did
  58 *      suboptimally.  
  59 *
  60 ***/
  61
  62/***
  63 *
  64 * $Log: ltpc.c,v $
  65 * Revision 1.1.2.1  2000/03/01 05:35:07  jgarzik
  66 * at and tr cleanup
  67 *
  68 * Revision 1.8  1997/01/28 05:44:54  bradford
  69 * Clean up for non-module a little.
  70 * Hacked about a bit to clean things up - Alan Cox 
  71 * Probably broken it from the origina 1.8
  72 *
  73
  74 * 1998/11/09: David Huggins-Daines <dhd@debian.org>
  75 * Cleaned up the initialization code to use the standard autoirq methods,
  76   and to probe for things in the standard order of i/o, irq, dma.  This
  77   removes the "reset the reset" hack, because I couldn't figure out an
  78   easy way to get the card to trigger an interrupt after it.
  79 * Added support for passing configuration parameters on the kernel command
  80   line and through insmod
  81 * Changed the device name from "ltalk0" to "lt0", both to conform with the
  82   other localtalk driver, and to clear up the inconsistency between the
  83   module and the non-module versions of the driver :-)
  84 * Added a bunch of comments (I was going to make some enums for the state
  85   codes and the register offsets, but I'm still not sure exactly what their
  86   semantics are)
  87 * Don't poll anymore in interrupt-driven mode
  88 * It seems to work as a module now (as of 2.1.127), but I don't think
  89   I'm responsible for that...
  90
  91 *
  92 * Revision 1.7  1996/12/12 03:42:33  bradford
  93 * DMA alloc cribbed from 3c505.c.
  94 *
  95 * Revision 1.6  1996/12/12 03:18:58  bradford
  96 * Added virt_to_bus; works in 2.1.13.
  97 *
  98 * Revision 1.5  1996/12/12 03:13:22  root
  99 * xmitQel initialization -- think through better though.
 100 *
 101 * Revision 1.4  1996/06/18 14:55:55  root
 102 * Change names to ltpc. Tabs. Took a shot at dma alloc,
 103 * although more needs to be done eventually.
 104 *
 105 * Revision 1.3  1996/05/22 14:59:39  root
 106 * Change dev->open, dev->close to track dummy.c in 1.99.(around 7)
 107 *
 108 * Revision 1.2  1996/05/22 14:58:24  root
 109 * Change tabs mostly.
 110 *
 111 * Revision 1.1  1996/04/23 04:45:09  root
 112 * Initial revision
 113 *
 114 * Revision 0.16  1996/03/05 15:59:56  root
 115 * Change ARPHRD_LOCALTLK definition to the "real" one.
 116 *
 117 * Revision 0.15  1996/03/05 06:28:30  root
 118 * Changes for kernel 1.3.70.  Still need a few patches to kernel, but
 119 * it's getting closer.
 120 *
 121 * Revision 0.14  1996/02/25 17:38:32  root
 122 * More cleanups.  Removed query to card on get_stats.
 123 *
 124 * Revision 0.13  1996/02/21  16:27:40  root
 125 * Refix debug_print_skb.  Fix mac.raw gotcha that appeared in 1.3.65.
 126 * Clean up receive code a little.
 127 *
 128 * Revision 0.12  1996/02/19  16:34:53  root
 129 * Fix debug_print_skb.  Kludge outgoing snet to 0 when using startup
 130 * range.  Change debug to mask: 1 for verbose, 2 for higher level stuff
 131 * including packet printing, 4 for lower level (card i/o) stuff.
 132 *
 133 * Revision 0.11  1996/02/12  15:53:38  root
 134 * Added router sends (requires new aarp.c patch)
 135 *
 136 * Revision 0.10  1996/02/11  00:19:35  root
 137 * Change source LTALK_LOGGING debug switch to insmod ... debug=2.
 138 *
 139 * Revision 0.9  1996/02/10  23:59:35  root
 140 * Fixed those fixes for 1.2 -- DANGER!  The at.h that comes with netatalk
 141 * has a *different* definition of struct sockaddr_at than the Linux kernel
 142 * does.  This is an "insidious and invidious" bug...
 143 * (Actually the preceding comment is false -- it's the atalk.h in the
 144 * ancient atalk-0.06 that's the problem)
 145 *
 146 * Revision 0.8  1996/02/10 19:09:00  root
 147 * Merge 1.3 changes.  Tested OK under 1.3.60.
 148 *
 149 * Revision 0.7  1996/02/10 17:56:56  root
 150 * Added debug=1 parameter on insmod for debugging prints.  Tried
 151 * to fix timer unload on rmmod, but I don't think that's the problem.
 152 *
 153 * Revision 0.6  1995/12/31  19:01:09  root
 154 * Clean up rmmod, irq comments per feedback from Corin Anderson (Thanks Corey!)
 155 * Clean up initial probing -- sometimes the card wakes up latched in reset.
 156 *
 157 * Revision 0.5  1995/12/22  06:03:44  root
 158 * Added comments in front and cleaned up a bit.
 159 * This version sent out to people.
 160 *
 161 * Revision 0.4  1995/12/18  03:46:44  root
 162 * Return shortDDP to longDDP fake to 0/0.  Added command structs.
 163 *
 164 ***/
 165
 166/* ltpc jumpers are:
 167*
 168*	Interrupts -- set at most one.  If none are set, the driver uses
 169*	polled mode.  Because the card was developed in the XT era, the
 170*	original documentation refers to IRQ2.  Since you'll be running
 171*	this on an AT (or later) class machine, that really means IRQ9.
 172*
 173*	SW1	IRQ 4
 174*	SW2	IRQ 3
 175*	SW3	IRQ 9 (2 in original card documentation only applies to XT)
 176*
 177*
 178*	DMA -- choose DMA 1 or 3, and set both corresponding switches.
 179*
 180*	SW4	DMA 3
 181*	SW5	DMA 1
 182*	SW6	DMA 3
 183*	SW7	DMA 1
 184*
 185*
 186*	I/O address -- choose one.  
 187*
 188*	SW8	220 / 240
 189*/
 190
 191/*	To have some stuff logged, do 
 192*	insmod ltpc.o debug=1
 193*
 194*	For a whole bunch of stuff, use higher numbers.
 195*
 196*	The default is 0, i.e. no messages except for the probe results.
 197*/
 198
 199/* insmod-tweakable variables */
 200static int debug;
 201#define DEBUG_VERBOSE 1
 202#define DEBUG_UPPER 2
 203#define DEBUG_LOWER 4
 204
 205static int io;
 206static int irq;
 207static int dma;
 208
 209#include <linux/module.h>
 210#include <linux/kernel.h>
 211#include <linux/types.h>
 212#include <linux/fcntl.h>
 213#include <linux/interrupt.h>
 214#include <linux/ptrace.h>
 215#include <linux/ioport.h>
 216#include <linux/spinlock.h>
 217#include <linux/in.h>
 218#include <linux/string.h>
 219#include <linux/errno.h>
 220#include <linux/init.h>
 221#include <linux/netdevice.h>
 222#include <linux/etherdevice.h>
 223#include <linux/skbuff.h>
 224#include <linux/if_arp.h>
 225#include <linux/if_ltalk.h>
 226#include <linux/delay.h>
 227#include <linux/timer.h>
 228#include <linux/atalk.h>
 229#include <linux/bitops.h>
 230#include <linux/gfp.h>
 231
 
 
 232#include <asm/dma.h>
 233#include <asm/io.h>
 234
 235/* our stuff */
 236#include "ltpc.h"
 237
 238static DEFINE_SPINLOCK(txqueue_lock);
 239static DEFINE_SPINLOCK(mbox_lock);
 240
 241/* function prototypes */
 242static int do_read(struct net_device *dev, void *cbuf, int cbuflen,
 243	void *dbuf, int dbuflen);
 244static int sendup_buffer (struct net_device *dev);
 245
 246/* Dma Memory related stuff, cribbed directly from 3c505.c */
 247
 248static unsigned long dma_mem_alloc(int size)
 249{
 250        int order = get_order(size);
 251
 252        return __get_dma_pages(GFP_KERNEL, order);
 253}
 254
 255/* DMA data buffer, DMA command buffer */
 256static unsigned char *ltdmabuf;
 257static unsigned char *ltdmacbuf;
 258
 259/* private struct, holds our appletalk address */
 260
 261struct ltpc_private
 262{
 263	struct atalk_addr my_addr;
 264};
 265
 266/* transmit queue element struct */
 267
 268struct xmitQel {
 269	struct xmitQel *next;
 270	/* command buffer */
 271	unsigned char *cbuf;
 272	short cbuflen;
 273	/* data buffer */
 274	unsigned char *dbuf;
 275	short dbuflen;
 276	unsigned char QWrite;	/* read or write data */
 277	unsigned char mailbox;
 278};
 279
 280/* the transmit queue itself */
 281
 282static struct xmitQel *xmQhd, *xmQtl;
 283
 284static void enQ(struct xmitQel *qel)
 285{
 286	unsigned long flags;
 287	qel->next = NULL;
 288	
 289	spin_lock_irqsave(&txqueue_lock, flags);
 290	if (xmQtl) {
 291		xmQtl->next = qel;
 292	} else {
 293		xmQhd = qel;
 294	}
 295	xmQtl = qel;
 296	spin_unlock_irqrestore(&txqueue_lock, flags);
 297
 298	if (debug & DEBUG_LOWER)
 299		printk("enqueued a 0x%02x command\n",qel->cbuf[0]);
 300}
 301
 302static struct xmitQel *deQ(void)
 303{
 304	unsigned long flags;
 305	int i;
 306	struct xmitQel *qel=NULL;
 307	
 308	spin_lock_irqsave(&txqueue_lock, flags);
 309	if (xmQhd) {
 310		qel = xmQhd;
 311		xmQhd = qel->next;
 312		if(!xmQhd) xmQtl = NULL;
 313	}
 314	spin_unlock_irqrestore(&txqueue_lock, flags);
 315
 316	if ((debug & DEBUG_LOWER) && qel) {
 317		int n;
 318		printk(KERN_DEBUG "ltpc: dequeued command ");
 319		n = qel->cbuflen;
 320		if (n>100) n=100;
 321		for(i=0;i<n;i++) printk("%02x ",qel->cbuf[i]);
 322		printk("\n");
 323	}
 324
 325	return qel;
 326}
 327
 328/* and... the queue elements we'll be using */
 329static struct xmitQel qels[16];
 330
 331/* and their corresponding mailboxes */
 332static unsigned char mailbox[16];
 333static unsigned char mboxinuse[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
 334
 335static int wait_timeout(struct net_device *dev, int c)
 336{
 337	/* returns true if it stayed c */
 338	/* this uses base+6, but it's ok */
 339	int i;
 340
 341	/* twenty second or so total */
 342
 343	for(i=0;i<200000;i++) {
 344		if ( c != inb_p(dev->base_addr+6) ) return 0;
 345		udelay(100);
 346	}
 347	return 1; /* timed out */
 348}
 349
 350/* get the first free mailbox */
 351
 352static int getmbox(void)
 353{
 354	unsigned long flags;
 355	int i;
 356
 357	spin_lock_irqsave(&mbox_lock, flags);
 358	for(i=1;i<16;i++) if(!mboxinuse[i]) {
 359		mboxinuse[i]=1;
 360		spin_unlock_irqrestore(&mbox_lock, flags);
 361		return i;
 362	}
 363	spin_unlock_irqrestore(&mbox_lock, flags);
 364	return 0;
 365}
 366
 367/* read a command from the card */
 368static void handlefc(struct net_device *dev)
 369{
 370	/* called *only* from idle, non-reentrant */
 371	int dma = dev->dma;
 372	int base = dev->base_addr;
 373	unsigned long flags;
 374
 375
 376	flags=claim_dma_lock();
 377	disable_dma(dma);
 378	clear_dma_ff(dma);
 379	set_dma_mode(dma,DMA_MODE_READ);
 380	set_dma_addr(dma,virt_to_bus(ltdmacbuf));
 381	set_dma_count(dma,50);
 382	enable_dma(dma);
 383	release_dma_lock(flags);
 384
 385	inb_p(base+3);
 386	inb_p(base+2);
 387
 388	if ( wait_timeout(dev,0xfc) ) printk("timed out in handlefc\n");
 389}
 390
 391/* read data from the card */
 392static void handlefd(struct net_device *dev)
 393{
 394	int dma = dev->dma;
 395	int base = dev->base_addr;
 396	unsigned long flags;
 397
 398	flags=claim_dma_lock();
 399	disable_dma(dma);
 400	clear_dma_ff(dma);
 401	set_dma_mode(dma,DMA_MODE_READ);
 402	set_dma_addr(dma,virt_to_bus(ltdmabuf));
 403	set_dma_count(dma,800);
 404	enable_dma(dma);
 405	release_dma_lock(flags);
 406
 407	inb_p(base+3);
 408	inb_p(base+2);
 409
 410	if ( wait_timeout(dev,0xfd) ) printk("timed out in handlefd\n");
 411	sendup_buffer(dev);
 412} 
 413
 414static void handlewrite(struct net_device *dev)
 415{
 416	/* called *only* from idle, non-reentrant */
 417	/* on entry, 0xfb and ltdmabuf holds data */
 418	int dma = dev->dma;
 419	int base = dev->base_addr;
 420	unsigned long flags;
 421	
 422	flags=claim_dma_lock();
 423	disable_dma(dma);
 424	clear_dma_ff(dma);
 425	set_dma_mode(dma,DMA_MODE_WRITE);
 426	set_dma_addr(dma,virt_to_bus(ltdmabuf));
 427	set_dma_count(dma,800);
 428	enable_dma(dma);
 429	release_dma_lock(flags);
 430	
 431	inb_p(base+3);
 432	inb_p(base+2);
 433
 434	if ( wait_timeout(dev,0xfb) ) {
 435		flags=claim_dma_lock();
 436		printk("timed out in handlewrite, dma res %d\n",
 437			get_dma_residue(dev->dma) );
 438		release_dma_lock(flags);
 439	}
 440}
 441
 442static void handleread(struct net_device *dev)
 443{
 444	/* on entry, 0xfb */
 445	/* on exit, ltdmabuf holds data */
 446	int dma = dev->dma;
 447	int base = dev->base_addr;
 448	unsigned long flags;
 449
 450	
 451	flags=claim_dma_lock();
 452	disable_dma(dma);
 453	clear_dma_ff(dma);
 454	set_dma_mode(dma,DMA_MODE_READ);
 455	set_dma_addr(dma,virt_to_bus(ltdmabuf));
 456	set_dma_count(dma,800);
 457	enable_dma(dma);
 458	release_dma_lock(flags);
 459
 460	inb_p(base+3);
 461	inb_p(base+2);
 462	if ( wait_timeout(dev,0xfb) ) printk("timed out in handleread\n");
 463}
 464
 465static void handlecommand(struct net_device *dev)
 466{
 467	/* on entry, 0xfa and ltdmacbuf holds command */
 468	int dma = dev->dma;
 469	int base = dev->base_addr;
 470	unsigned long flags;
 471
 472	flags=claim_dma_lock();
 473	disable_dma(dma);
 474	clear_dma_ff(dma);
 475	set_dma_mode(dma,DMA_MODE_WRITE);
 476	set_dma_addr(dma,virt_to_bus(ltdmacbuf));
 477	set_dma_count(dma,50);
 478	enable_dma(dma);
 479	release_dma_lock(flags);
 480	inb_p(base+3);
 481	inb_p(base+2);
 482	if ( wait_timeout(dev,0xfa) ) printk("timed out in handlecommand\n");
 483} 
 484
 485/* ready made command for getting the result from the card */
 486static unsigned char rescbuf[2] = {LT_GETRESULT,0};
 487static unsigned char resdbuf[2];
 488
 489static int QInIdle;
 490
 491/* idle expects to be called with the IRQ line high -- either because of
 492 * an interrupt, or because the line is tri-stated
 493 */
 494
 495static void idle(struct net_device *dev)
 496{
 497	unsigned long flags;
 498	int state;
 499	/* FIXME This is initialized to shut the warning up, but I need to
 500	 * think this through again.
 501	 */
 502	struct xmitQel *q = NULL;
 503	int oops;
 504	int i;
 505	int base = dev->base_addr;
 506
 507	spin_lock_irqsave(&txqueue_lock, flags);
 508	if(QInIdle) {
 509		spin_unlock_irqrestore(&txqueue_lock, flags);
 510		return;
 511	}
 512	QInIdle = 1;
 513	spin_unlock_irqrestore(&txqueue_lock, flags);
 514
 515	/* this tri-states the IRQ line */
 516	(void) inb_p(base+6);
 517
 518	oops = 100;
 519
 520loop:
 521	if (0>oops--) { 
 522		printk("idle: looped too many times\n");
 523		goto done;
 524	}
 525
 526	state = inb_p(base+6);
 527	if (state != inb_p(base+6)) goto loop;
 528
 529	switch(state) {
 530		case 0xfc:
 531			/* incoming command */
 532			if (debug & DEBUG_LOWER) printk("idle: fc\n");
 533			handlefc(dev); 
 534			break;
 535		case 0xfd:
 536			/* incoming data */
 537			if(debug & DEBUG_LOWER) printk("idle: fd\n");
 538			handlefd(dev); 
 539			break;
 540		case 0xf9:
 541			/* result ready */
 542			if (debug & DEBUG_LOWER) printk("idle: f9\n");
 543			if(!mboxinuse[0]) {
 544				mboxinuse[0] = 1;
 545				qels[0].cbuf = rescbuf;
 546				qels[0].cbuflen = 2;
 547				qels[0].dbuf = resdbuf;
 548				qels[0].dbuflen = 2;
 549				qels[0].QWrite = 0;
 550				qels[0].mailbox = 0;
 551				enQ(&qels[0]);
 552			}
 553			inb_p(dev->base_addr+1);
 554			inb_p(dev->base_addr+0);
 555			if( wait_timeout(dev,0xf9) )
 556				printk("timed out idle f9\n");
 557			break;
 558		case 0xf8:
 559			/* ?? */
 560			if (xmQhd) {
 561				inb_p(dev->base_addr+1);
 562				inb_p(dev->base_addr+0);
 563				if(wait_timeout(dev,0xf8) )
 564					printk("timed out idle f8\n");
 565			} else {
 566				goto done;
 567			}
 568			break;
 569		case 0xfa:
 570			/* waiting for command */
 571			if(debug & DEBUG_LOWER) printk("idle: fa\n");
 572			if (xmQhd) {
 573				q=deQ();
 574				memcpy(ltdmacbuf,q->cbuf,q->cbuflen);
 575				ltdmacbuf[1] = q->mailbox;
 576				if (debug>1) { 
 577					int n;
 578					printk("ltpc: sent command     ");
 579					n = q->cbuflen;
 580					if (n>100) n=100;
 581					for(i=0;i<n;i++)
 582						printk("%02x ",ltdmacbuf[i]);
 583					printk("\n");
 584				}
 
 585				handlecommand(dev);
 586					if(0xfa==inb_p(base+6)) {
 587						/* we timed out, so return */
 588						goto done;
 589					} 
 
 590			} else {
 591				/* we don't seem to have a command */
 592				if (!mboxinuse[0]) {
 593					mboxinuse[0] = 1;
 594					qels[0].cbuf = rescbuf;
 595					qels[0].cbuflen = 2;
 596					qels[0].dbuf = resdbuf;
 597					qels[0].dbuflen = 2;
 598					qels[0].QWrite = 0;
 599					qels[0].mailbox = 0;
 600					enQ(&qels[0]);
 601				} else {
 602					printk("trouble: response command already queued\n");
 603					goto done;
 604				}
 605			} 
 606			break;
 607		case 0Xfb:
 608			/* data transfer ready */
 609			if(debug & DEBUG_LOWER) printk("idle: fb\n");
 610			if(q->QWrite) {
 611				memcpy(ltdmabuf,q->dbuf,q->dbuflen);
 612				handlewrite(dev);
 613			} else {
 614				handleread(dev);
 615				/* non-zero mailbox numbers are for
 616				   commmands, 0 is for GETRESULT
 617				   requests */
 618				if(q->mailbox) {
 619					memcpy(q->dbuf,ltdmabuf,q->dbuflen);
 620				} else { 
 621					/* this was a result */
 622					mailbox[ 0x0f & ltdmabuf[0] ] = ltdmabuf[1];
 623					mboxinuse[0]=0;
 624				}
 625			}
 626			break;
 627	}
 628	goto loop;
 629
 630done:
 631	QInIdle=0;
 632
 633	/* now set the interrupts back as appropriate */
 634	/* the first read takes it out of tri-state (but still high) */
 635	/* the second resets it */
 636	/* note that after this point, any read of base+6 will
 637	   trigger an interrupt */
 638
 639	if (dev->irq) {
 640		inb_p(base+7);
 641		inb_p(base+7);
 642	}
 643}
 644
 645
 646static int do_write(struct net_device *dev, void *cbuf, int cbuflen,
 647	void *dbuf, int dbuflen)
 648{
 649
 650	int i = getmbox();
 651	int ret;
 652
 653	if(i) {
 654		qels[i].cbuf = cbuf;
 655		qels[i].cbuflen = cbuflen;
 656		qels[i].dbuf = dbuf;
 657		qels[i].dbuflen = dbuflen;
 658		qels[i].QWrite = 1;
 659		qels[i].mailbox = i;  /* this should be initted rather */
 660		enQ(&qels[i]);
 661		idle(dev);
 662		ret = mailbox[i];
 663		mboxinuse[i]=0;
 664		return ret;
 665	}
 666	printk("ltpc: could not allocate mbox\n");
 667	return -1;
 668}
 669
 670static int do_read(struct net_device *dev, void *cbuf, int cbuflen,
 671	void *dbuf, int dbuflen)
 672{
 673
 674	int i = getmbox();
 675	int ret;
 676
 677	if(i) {
 678		qels[i].cbuf = cbuf;
 679		qels[i].cbuflen = cbuflen;
 680		qels[i].dbuf = dbuf;
 681		qels[i].dbuflen = dbuflen;
 682		qels[i].QWrite = 0;
 683		qels[i].mailbox = i;  /* this should be initted rather */
 684		enQ(&qels[i]);
 685		idle(dev);
 686		ret = mailbox[i];
 687		mboxinuse[i]=0;
 688		return ret;
 689	}
 690	printk("ltpc: could not allocate mbox\n");
 691	return -1;
 692}
 693
 694/* end of idle handlers -- what should be seen is do_read, do_write */
 695
 696static struct timer_list ltpc_timer;
 697static struct net_device *ltpc_timer_dev;
 698
 699static netdev_tx_t ltpc_xmit(struct sk_buff *skb, struct net_device *dev);
 700
 701static int read_30 ( struct net_device *dev)
 702{
 703	lt_command c;
 704	c.getflags.command = LT_GETFLAGS;
 705	return do_read(dev, &c, sizeof(c.getflags),&c,0);
 706}
 707
 708static int set_30 (struct net_device *dev,int x)
 709{
 710	lt_command c;
 711	c.setflags.command = LT_SETFLAGS;
 712	c.setflags.flags = x;
 713	return do_write(dev, &c, sizeof(c.setflags),&c,0);
 714}
 715
 716/* LLAP to DDP translation */
 717
 718static int sendup_buffer (struct net_device *dev)
 719{
 720	/* on entry, command is in ltdmacbuf, data in ltdmabuf */
 721	/* called from idle, non-reentrant */
 722
 723	int dnode, snode, llaptype, len; 
 724	int sklen;
 725	struct sk_buff *skb;
 726	struct lt_rcvlap *ltc = (struct lt_rcvlap *) ltdmacbuf;
 727
 728	if (ltc->command != LT_RCVLAP) {
 729		printk("unknown command 0x%02x from ltpc card\n",ltc->command);
 730		return -1;
 731	}
 732	dnode = ltc->dnode;
 733	snode = ltc->snode;
 734	llaptype = ltc->laptype;
 735	len = ltc->length; 
 736
 737	sklen = len;
 738	if (llaptype == 1) 
 739		sklen += 8;  /* correct for short ddp */
 740	if(sklen > 800) {
 741		printk(KERN_INFO "%s: nonsense length in ltpc command 0x14: 0x%08x\n",
 742			dev->name,sklen);
 743		return -1;
 744	}
 745
 746	if ( (llaptype==0) || (llaptype>2) ) {
 747		printk(KERN_INFO "%s: unknown LLAP type: %d\n",dev->name,llaptype);
 748		return -1;
 749	}
 750
 751
 752	skb = dev_alloc_skb(3+sklen);
 753	if (skb == NULL) 
 754	{
 755		printk("%s: dropping packet due to memory squeeze.\n",
 756			dev->name);
 757		return -1;
 758	}
 759	skb->dev = dev;
 760
 761	if (sklen > len)
 762		skb_reserve(skb,8);
 763	skb_put(skb,len+3);
 764	skb->protocol = htons(ETH_P_LOCALTALK);
 765	/* add LLAP header */
 766	skb->data[0] = dnode;
 767	skb->data[1] = snode;
 768	skb->data[2] = llaptype;
 769	skb_reset_mac_header(skb);	/* save pointer to llap header */
 770	skb_pull(skb,3);
 771
 772	/* copy ddp(s,e)hdr + contents */
 773	skb_copy_to_linear_data(skb, ltdmabuf, len);
 774
 775	skb_reset_transport_header(skb);
 776
 777	dev->stats.rx_packets++;
 778	dev->stats.rx_bytes += skb->len;
 779
 780	/* toss it onwards */
 781	netif_rx(skb);
 782	return 0;
 783}
 784
 785/* the handler for the board interrupt */
 786 
 787static irqreturn_t
 788ltpc_interrupt(int irq, void *dev_id)
 789{
 790	struct net_device *dev = dev_id;
 791
 792	if (dev==NULL) {
 793		printk("ltpc_interrupt: unknown device.\n");
 794		return IRQ_NONE;
 795	}
 796
 797	inb_p(dev->base_addr+6);  /* disable further interrupts from board */
 798
 799	idle(dev); /* handle whatever is coming in */
 800 
 801	/* idle re-enables interrupts from board */ 
 802
 803	return IRQ_HANDLED;
 804}
 805
 806/***
 807 *
 808 *    The ioctls that the driver responds to are:
 809 *
 810 *    SIOCSIFADDR -- do probe using the passed node hint.
 811 *    SIOCGIFADDR -- return net, node.
 812 *
 813 *    some of this stuff should be done elsewhere.
 814 *
 815 ***/
 816
 817static int ltpc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
 818{
 819	struct sockaddr_at *sa = (struct sockaddr_at *) &ifr->ifr_addr;
 820	/* we'll keep the localtalk node address in dev->pa_addr */
 821	struct ltpc_private *ltpc_priv = netdev_priv(dev);
 822	struct atalk_addr *aa = &ltpc_priv->my_addr;
 823	struct lt_init c;
 824	int ltflags;
 825
 826	if(debug & DEBUG_VERBOSE) printk("ltpc_ioctl called\n");
 827
 828	switch(cmd) {
 829		case SIOCSIFADDR:
 830
 831			aa->s_net  = sa->sat_addr.s_net;
 832      
 833			/* this does the probe and returns the node addr */
 834			c.command = LT_INIT;
 835			c.hint = sa->sat_addr.s_node;
 836
 837			aa->s_node = do_read(dev,&c,sizeof(c),&c,0);
 838
 839			/* get all llap frames raw */
 840			ltflags = read_30(dev);
 841			ltflags |= LT_FLAG_ALLLAP;
 842			set_30 (dev,ltflags);  
 843
 844			dev->broadcast[0] = 0xFF;
 845			dev->dev_addr[0] = aa->s_node;
 846
 847			dev->addr_len=1;
 848   
 849			return 0;
 850
 851		case SIOCGIFADDR:
 852
 853			sa->sat_addr.s_net = aa->s_net;
 854			sa->sat_addr.s_node = aa->s_node;
 855
 856			return 0;
 857
 858		default: 
 859			return -EINVAL;
 860	}
 861}
 862
 863static void set_multicast_list(struct net_device *dev)
 864{
 865	/* This needs to be present to keep netatalk happy. */
 866	/* Actually netatalk needs fixing! */
 867}
 868
 869static int ltpc_poll_counter;
 870
 871static void ltpc_poll(struct timer_list *unused)
 872{
 873	del_timer(&ltpc_timer);
 874
 875	if(debug & DEBUG_VERBOSE) {
 876		if (!ltpc_poll_counter) {
 877			ltpc_poll_counter = 50;
 878			printk("ltpc poll is alive\n");
 879		}
 880		ltpc_poll_counter--;
 881	}
 882
 883	/* poll 20 times per second */
 884	idle(ltpc_timer_dev);
 885	ltpc_timer.expires = jiffies + HZ/20;
 886	add_timer(&ltpc_timer);
 887}
 888
 889/* DDP to LLAP translation */
 890
 891static netdev_tx_t ltpc_xmit(struct sk_buff *skb, struct net_device *dev)
 892{
 893	/* in kernel 1.3.xx, on entry skb->data points to ddp header,
 894	 * and skb->len is the length of the ddp data + ddp header
 895	 */
 896	int i;
 897	struct lt_sendlap cbuf;
 898	unsigned char *hdr;
 899
 900	cbuf.command = LT_SENDLAP;
 901	cbuf.dnode = skb->data[0];
 902	cbuf.laptype = skb->data[2];
 903	skb_pull(skb,3);	/* skip past LLAP header */
 904	cbuf.length = skb->len;	/* this is host order */
 905	skb_reset_transport_header(skb);
 906
 907	if(debug & DEBUG_UPPER) {
 908		printk("command ");
 909		for(i=0;i<6;i++)
 910			printk("%02x ",((unsigned char *)&cbuf)[i]);
 911		printk("\n");
 912	}
 913
 914	hdr = skb_transport_header(skb);
 915	do_write(dev, &cbuf, sizeof(cbuf), hdr, skb->len);
 916
 917	if(debug & DEBUG_UPPER) {
 918		printk("sent %d ddp bytes\n",skb->len);
 919		for (i = 0; i < skb->len; i++)
 920			printk("%02x ", hdr[i]);
 921		printk("\n");
 922	}
 923
 924	dev->stats.tx_packets++;
 925	dev->stats.tx_bytes += skb->len;
 926
 927	dev_kfree_skb(skb);
 928	return NETDEV_TX_OK;
 929}
 930
 931/* initialization stuff */
 932  
 933static int __init ltpc_probe_dma(int base, int dma)
 934{
 935	int want = (dma == 3) ? 2 : (dma == 1) ? 1 : 3;
 936  	unsigned long timeout;
 937  	unsigned long f;
 938  
 939  	if (want & 1) {
 940		if (request_dma(1,"ltpc")) {
 941			want &= ~1;
 942		} else {
 943			f=claim_dma_lock();
 944			disable_dma(1);
 945			clear_dma_ff(1);
 946			set_dma_mode(1,DMA_MODE_WRITE);
 947			set_dma_addr(1,virt_to_bus(ltdmabuf));
 948			set_dma_count(1,sizeof(struct lt_mem));
 949			enable_dma(1);
 950			release_dma_lock(f);
 951		}
 952	}
 953	if (want & 2) {
 954		if (request_dma(3,"ltpc")) {
 955			want &= ~2;
 956		} else {
 957			f=claim_dma_lock();
 958			disable_dma(3);
 959			clear_dma_ff(3);
 960			set_dma_mode(3,DMA_MODE_WRITE);
 961			set_dma_addr(3,virt_to_bus(ltdmabuf));
 962			set_dma_count(3,sizeof(struct lt_mem));
 963			enable_dma(3);
 964			release_dma_lock(f);
 965		}
 966	}
 967	/* set up request */
 968
 969	/* FIXME -- do timings better! */
 970
 971	ltdmabuf[0] = LT_READMEM;
 972	ltdmabuf[1] = 1;  /* mailbox */
 973	ltdmabuf[2] = 0; ltdmabuf[3] = 0;  /* address */
 974	ltdmabuf[4] = 0; ltdmabuf[5] = 1;  /* read 0x0100 bytes */
 975	ltdmabuf[6] = 0; /* dunno if this is necessary */
 976
 977	inb_p(io+1);
 978	inb_p(io+0);
 979	timeout = jiffies+100*HZ/100;
 980	while(time_before(jiffies, timeout)) {
 981		if ( 0xfa == inb_p(io+6) ) break;
 982	}
 983
 984	inb_p(io+3);
 985	inb_p(io+2);
 986	while(time_before(jiffies, timeout)) {
 987		if ( 0xfb == inb_p(io+6) ) break;
 988	}
 989
 990	/* release the other dma channel (if we opened both of them) */
 991
 992	if ((want & 2) && (get_dma_residue(3)==sizeof(struct lt_mem))) {
 993		want &= ~2;
 994		free_dma(3);
 995	}
 996
 997	if ((want & 1) && (get_dma_residue(1)==sizeof(struct lt_mem))) {
 998		want &= ~1;
 999		free_dma(1);
1000	}
1001
1002	if (!want)
1003		return 0;
1004
1005	return (want & 2) ? 3 : 1;
1006}
1007
1008static const struct net_device_ops ltpc_netdev = {
1009	.ndo_start_xmit		= ltpc_xmit,
1010	.ndo_do_ioctl		= ltpc_ioctl,
1011	.ndo_set_rx_mode	= set_multicast_list,
1012};
1013
1014struct net_device * __init ltpc_probe(void)
1015{
1016	struct net_device *dev;
1017	int err = -ENOMEM;
1018	int x=0,y=0;
1019	int autoirq;
1020	unsigned long f;
1021	unsigned long timeout;
1022
1023	dev = alloc_ltalkdev(sizeof(struct ltpc_private));
1024	if (!dev)
1025		goto out;
1026
1027	/* probe for the I/O port address */
1028	
1029	if (io != 0x240 && request_region(0x220,8,"ltpc")) {
1030		x = inb_p(0x220+6);
1031		if ( (x!=0xff) && (x>=0xf0) ) {
1032			io = 0x220;
1033			goto got_port;
1034		}
1035		release_region(0x220,8);
1036	}
1037	if (io != 0x220 && request_region(0x240,8,"ltpc")) {
1038		y = inb_p(0x240+6);
1039		if ( (y!=0xff) && (y>=0xf0) ){ 
1040			io = 0x240;
1041			goto got_port;
1042		}
1043		release_region(0x240,8);
1044	} 
1045
1046	/* give up in despair */
1047	printk(KERN_ERR "LocalTalk card not found; 220 = %02x, 240 = %02x.\n", x,y);
1048	err = -ENODEV;
1049	goto out1;
1050
1051 got_port:
1052	/* probe for the IRQ line */
1053	if (irq < 2) {
1054		unsigned long irq_mask;
1055
1056		irq_mask = probe_irq_on();
1057		/* reset the interrupt line */
1058		inb_p(io+7);
1059		inb_p(io+7);
1060		/* trigger an interrupt (I hope) */
1061		inb_p(io+6);
1062		mdelay(2);
1063		autoirq = probe_irq_off(irq_mask);
1064
1065		if (autoirq == 0) {
1066			printk(KERN_ERR "ltpc: probe at %#x failed to detect IRQ line.\n", io);
1067		} else {
1068			irq = autoirq;
1069		}
1070	}
1071
1072	/* allocate a DMA buffer */
1073	ltdmabuf = (unsigned char *) dma_mem_alloc(1000);
1074	if (!ltdmabuf) {
1075		printk(KERN_ERR "ltpc: mem alloc failed\n");
1076		err = -ENOMEM;
1077		goto out2;
1078	}
1079
1080	ltdmacbuf = &ltdmabuf[800];
1081
1082	if(debug & DEBUG_VERBOSE) {
1083		printk("ltdmabuf pointer %08lx\n",(unsigned long) ltdmabuf);
1084	}
1085
1086	/* reset the card */
1087
1088	inb_p(io+1);
1089	inb_p(io+3);
1090
1091	msleep(20);
1092
1093	inb_p(io+0);
1094	inb_p(io+2);
1095	inb_p(io+7); /* clear reset */
1096	inb_p(io+4); 
1097	inb_p(io+5);
1098	inb_p(io+5); /* enable dma */
1099	inb_p(io+6); /* tri-state interrupt line */
1100
1101	ssleep(1);
1102	
1103	/* now, figure out which dma channel we're using, unless it's
1104	   already been specified */
1105	/* well, 0 is a legal DMA channel, but the LTPC card doesn't
1106	   use it... */
1107	dma = ltpc_probe_dma(io, dma);
1108	if (!dma) {  /* no dma channel */
1109		printk(KERN_ERR "No DMA channel found on ltpc card.\n");
1110		err = -ENODEV;
1111		goto out3;
1112	}
1113
1114	/* print out friendly message */
1115	if(irq)
1116		printk(KERN_INFO "Apple/Farallon LocalTalk-PC card at %03x, IR%d, DMA%d.\n",io,irq,dma);
1117	else
1118		printk(KERN_INFO "Apple/Farallon LocalTalk-PC card at %03x, DMA%d.  Using polled mode.\n",io,dma);
1119
1120	dev->netdev_ops = &ltpc_netdev;
1121	dev->base_addr = io;
1122	dev->irq = irq;
1123	dev->dma = dma;
1124
1125	/* the card will want to send a result at this point */
1126	/* (I think... leaving out this part makes the kernel crash,
1127           so I put it back in...) */
1128
1129	f=claim_dma_lock();
1130	disable_dma(dma);
1131	clear_dma_ff(dma);
1132	set_dma_mode(dma,DMA_MODE_READ);
1133	set_dma_addr(dma,virt_to_bus(ltdmabuf));
1134	set_dma_count(dma,0x100);
1135	enable_dma(dma);
1136	release_dma_lock(f);
1137
1138	(void) inb_p(io+3);
1139	(void) inb_p(io+2);
1140	timeout = jiffies+100*HZ/100;
1141
1142	while(time_before(jiffies, timeout)) {
1143		if( 0xf9 == inb_p(io+6))
1144			break;
1145		schedule();
1146	}
1147
1148	if(debug & DEBUG_VERBOSE) {
1149		printk("setting up timer and irq\n");
1150	}
1151
1152	/* grab it and don't let go :-) */
1153	if (irq && request_irq( irq, ltpc_interrupt, 0, "ltpc", dev) >= 0)
1154	{
1155		(void) inb_p(io+7);  /* enable interrupts from board */
1156		(void) inb_p(io+7);  /* and reset irq line */
1157	} else {
1158		if( irq )
1159			printk(KERN_ERR "ltpc: IRQ already in use, using polled mode.\n");
1160		dev->irq = 0;
1161		/* polled mode -- 20 times per second */
1162		/* this is really, really slow... should it poll more often? */
1163		ltpc_timer_dev = dev;
1164		timer_setup(&ltpc_timer, ltpc_poll, 0);
1165
1166		ltpc_timer.expires = jiffies + HZ/20;
1167		add_timer(&ltpc_timer);
1168	}
1169	err = register_netdev(dev);
1170	if (err)
1171		goto out4;
1172
1173	return NULL;
1174out4:
1175	del_timer_sync(&ltpc_timer);
1176	if (dev->irq)
1177		free_irq(dev->irq, dev);
1178out3:
1179	free_pages((unsigned long)ltdmabuf, get_order(1000));
1180out2:
1181	release_region(io, 8);
1182out1:
1183	free_netdev(dev);
1184out:
1185	return ERR_PTR(err);
1186}
1187
1188#ifndef MODULE
1189/* handles "ltpc=io,irq,dma" kernel command lines */
1190static int __init ltpc_setup(char *str)
1191{
1192	int ints[5];
1193
1194	str = get_options(str, ARRAY_SIZE(ints), ints);
1195
1196	if (ints[0] == 0) {
1197		if (str && !strncmp(str, "auto", 4)) {
1198			/* do nothing :-) */
1199		}
1200		else {
1201			/* usage message */
1202			printk (KERN_ERR
1203				"ltpc: usage: ltpc=auto|iobase[,irq[,dma]]\n");
1204			return 0;
1205		}
1206	} else {
1207		io = ints[1];
1208		if (ints[0] > 1) {
1209			irq = ints[2];
1210		}
1211		if (ints[0] > 2) {
1212			dma = ints[3];
1213		}
1214		/* ignore any other parameters */
1215	}
1216	return 1;
1217}
1218
1219__setup("ltpc=", ltpc_setup);
1220#endif /* MODULE */
1221
1222static struct net_device *dev_ltpc;
1223
1224#ifdef MODULE
1225
1226MODULE_LICENSE("GPL");
1227module_param(debug, int, 0);
1228module_param_hw(io, int, ioport, 0);
1229module_param_hw(irq, int, irq, 0);
1230module_param_hw(dma, int, dma, 0);
1231
1232
1233static int __init ltpc_module_init(void)
1234{
1235        if(io == 0)
1236		printk(KERN_NOTICE
1237		       "ltpc: Autoprobing is not recommended for modules\n");
1238
1239	dev_ltpc = ltpc_probe();
1240	return PTR_ERR_OR_ZERO(dev_ltpc);
1241}
1242module_init(ltpc_module_init);
1243#endif
1244
1245static void __exit ltpc_cleanup(void)
1246{
1247
1248	if(debug & DEBUG_VERBOSE) printk("unregister_netdev\n");
1249	unregister_netdev(dev_ltpc);
1250
1251	del_timer_sync(&ltpc_timer);
1252
1253	if(debug & DEBUG_VERBOSE) printk("freeing irq\n");
1254
1255	if (dev_ltpc->irq)
1256		free_irq(dev_ltpc->irq, dev_ltpc);
1257
1258	if(debug & DEBUG_VERBOSE) printk("freeing dma\n");
1259
1260	if (dev_ltpc->dma)
1261		free_dma(dev_ltpc->dma);
1262
1263	if(debug & DEBUG_VERBOSE) printk("freeing ioaddr\n");
1264
1265	if (dev_ltpc->base_addr)
1266		release_region(dev_ltpc->base_addr,8);
1267
1268	free_netdev(dev_ltpc);
1269
1270	if(debug & DEBUG_VERBOSE) printk("free_pages\n");
1271
1272	free_pages( (unsigned long) ltdmabuf, get_order(1000));
1273
1274	if(debug & DEBUG_VERBOSE) printk("returning from cleanup_module\n");
1275}
1276
1277module_exit(ltpc_cleanup);
v5.14.15
   1/***    ltpc.c -- a driver for the LocalTalk PC card.
   2 *
   3 *      Copyright (c) 1995,1996 Bradford W. Johnson <johns393@maroon.tc.umn.edu>
   4 *
   5 *      This software may be used and distributed according to the terms
   6 *      of the GNU General Public License, incorporated herein by reference.
   7 *
   8 *      This is ALPHA code at best.  It may not work for you.  It may
   9 *      damage your equipment.  It may damage your relations with other
  10 *      users of your network.  Use it at your own risk!
  11 *
  12 *      Based in part on:
  13 *      skeleton.c      by Donald Becker
  14 *      dummy.c         by Nick Holloway and Alan Cox
  15 *      loopback.c      by Ross Biro, Fred van Kampen, Donald Becker
  16 *      the netatalk source code (UMICH)
  17 *      lots of work on the card...
  18 *
  19 *      I do not have access to the (proprietary) SDK that goes with the card.
  20 *      If you do, I don't want to know about it, and you can probably write
  21 *      a better driver yourself anyway.  This does mean that the pieces that
  22 *      talk to the card are guesswork on my part, so use at your own risk!
  23 *
  24 *      This is my first try at writing Linux networking code, and is also
  25 *      guesswork.  Again, use at your own risk!  (Although on this part, I'd
  26 *      welcome suggestions)
  27 *
  28 *      This is a loadable kernel module which seems to work at my site
  29 *      consisting of a 1.2.13 linux box running netatalk 1.3.3, and with
  30 *      the kernel support from 1.3.3b2 including patches routing.patch
  31 *      and ddp.disappears.from.chooser.  In order to run it, you will need
  32 *      to patch ddp.c and aarp.c in the kernel, but only a little...
  33 *
  34 *      I'm fairly confident that while this is arguably badly written, the
  35 *      problems that people experience will be "higher level", that is, with
  36 *      complications in the netatalk code.  The driver itself doesn't do
  37 *      anything terribly complicated -- it pretends to be an ether device
  38 *      as far as netatalk is concerned, strips the DDP data out of the ether
  39 *      frame and builds a LLAP packet to send out the card.  In the other
  40 *      direction, it receives LLAP frames from the card and builds a fake
  41 *      ether packet that it then tosses up to the networking code.  You can
  42 *      argue (correctly) that this is an ugly way to do things, but it
  43 *      requires a minimal amount of fooling with the code in ddp.c and aarp.c.
  44 *
  45 *      The card will do a lot more than is used here -- I *think* it has the
  46 *      layers up through ATP.  Even if you knew how that part works (which I
  47 *      don't) it would be a big job to carve up the kernel ddp code to insert
  48 *      things at a higher level, and probably a bad idea...
  49 *
  50 *      There are a number of other cards that do LocalTalk on the PC.  If
  51 *      nobody finds any insurmountable (at the netatalk level) problems
  52 *      here, this driver should encourage people to put some work into the
  53 *      other cards (some of which I gather are still commercially available)
  54 *      and also to put hooks for LocalTalk into the official ddp code.
  55 *
  56 *      I welcome comments and suggestions.  This is my first try at Linux
  57 *      networking stuff, and there are probably lots of things that I did
  58 *      suboptimally.  
  59 *
  60 ***/
  61
  62/***
  63 *
  64 * $Log: ltpc.c,v $
  65 * Revision 1.1.2.1  2000/03/01 05:35:07  jgarzik
  66 * at and tr cleanup
  67 *
  68 * Revision 1.8  1997/01/28 05:44:54  bradford
  69 * Clean up for non-module a little.
  70 * Hacked about a bit to clean things up - Alan Cox 
  71 * Probably broken it from the origina 1.8
  72 *
  73
  74 * 1998/11/09: David Huggins-Daines <dhd@debian.org>
  75 * Cleaned up the initialization code to use the standard autoirq methods,
  76   and to probe for things in the standard order of i/o, irq, dma.  This
  77   removes the "reset the reset" hack, because I couldn't figure out an
  78   easy way to get the card to trigger an interrupt after it.
  79 * Added support for passing configuration parameters on the kernel command
  80   line and through insmod
  81 * Changed the device name from "ltalk0" to "lt0", both to conform with the
  82   other localtalk driver, and to clear up the inconsistency between the
  83   module and the non-module versions of the driver :-)
  84 * Added a bunch of comments (I was going to make some enums for the state
  85   codes and the register offsets, but I'm still not sure exactly what their
  86   semantics are)
  87 * Don't poll anymore in interrupt-driven mode
  88 * It seems to work as a module now (as of 2.1.127), but I don't think
  89   I'm responsible for that...
  90
  91 *
  92 * Revision 1.7  1996/12/12 03:42:33  bradford
  93 * DMA alloc cribbed from 3c505.c.
  94 *
  95 * Revision 1.6  1996/12/12 03:18:58  bradford
  96 * Added virt_to_bus; works in 2.1.13.
  97 *
  98 * Revision 1.5  1996/12/12 03:13:22  root
  99 * xmitQel initialization -- think through better though.
 100 *
 101 * Revision 1.4  1996/06/18 14:55:55  root
 102 * Change names to ltpc. Tabs. Took a shot at dma alloc,
 103 * although more needs to be done eventually.
 104 *
 105 * Revision 1.3  1996/05/22 14:59:39  root
 106 * Change dev->open, dev->close to track dummy.c in 1.99.(around 7)
 107 *
 108 * Revision 1.2  1996/05/22 14:58:24  root
 109 * Change tabs mostly.
 110 *
 111 * Revision 1.1  1996/04/23 04:45:09  root
 112 * Initial revision
 113 *
 114 * Revision 0.16  1996/03/05 15:59:56  root
 115 * Change ARPHRD_LOCALTLK definition to the "real" one.
 116 *
 117 * Revision 0.15  1996/03/05 06:28:30  root
 118 * Changes for kernel 1.3.70.  Still need a few patches to kernel, but
 119 * it's getting closer.
 120 *
 121 * Revision 0.14  1996/02/25 17:38:32  root
 122 * More cleanups.  Removed query to card on get_stats.
 123 *
 124 * Revision 0.13  1996/02/21  16:27:40  root
 125 * Refix debug_print_skb.  Fix mac.raw gotcha that appeared in 1.3.65.
 126 * Clean up receive code a little.
 127 *
 128 * Revision 0.12  1996/02/19  16:34:53  root
 129 * Fix debug_print_skb.  Kludge outgoing snet to 0 when using startup
 130 * range.  Change debug to mask: 1 for verbose, 2 for higher level stuff
 131 * including packet printing, 4 for lower level (card i/o) stuff.
 132 *
 133 * Revision 0.11  1996/02/12  15:53:38  root
 134 * Added router sends (requires new aarp.c patch)
 135 *
 136 * Revision 0.10  1996/02/11  00:19:35  root
 137 * Change source LTALK_LOGGING debug switch to insmod ... debug=2.
 138 *
 139 * Revision 0.9  1996/02/10  23:59:35  root
 140 * Fixed those fixes for 1.2 -- DANGER!  The at.h that comes with netatalk
 141 * has a *different* definition of struct sockaddr_at than the Linux kernel
 142 * does.  This is an "insidious and invidious" bug...
 143 * (Actually the preceding comment is false -- it's the atalk.h in the
 144 * ancient atalk-0.06 that's the problem)
 145 *
 146 * Revision 0.8  1996/02/10 19:09:00  root
 147 * Merge 1.3 changes.  Tested OK under 1.3.60.
 148 *
 149 * Revision 0.7  1996/02/10 17:56:56  root
 150 * Added debug=1 parameter on insmod for debugging prints.  Tried
 151 * to fix timer unload on rmmod, but I don't think that's the problem.
 152 *
 153 * Revision 0.6  1995/12/31  19:01:09  root
 154 * Clean up rmmod, irq comments per feedback from Corin Anderson (Thanks Corey!)
 155 * Clean up initial probing -- sometimes the card wakes up latched in reset.
 156 *
 157 * Revision 0.5  1995/12/22  06:03:44  root
 158 * Added comments in front and cleaned up a bit.
 159 * This version sent out to people.
 160 *
 161 * Revision 0.4  1995/12/18  03:46:44  root
 162 * Return shortDDP to longDDP fake to 0/0.  Added command structs.
 163 *
 164 ***/
 165
 166/* ltpc jumpers are:
 167*
 168*	Interrupts -- set at most one.  If none are set, the driver uses
 169*	polled mode.  Because the card was developed in the XT era, the
 170*	original documentation refers to IRQ2.  Since you'll be running
 171*	this on an AT (or later) class machine, that really means IRQ9.
 172*
 173*	SW1	IRQ 4
 174*	SW2	IRQ 3
 175*	SW3	IRQ 9 (2 in original card documentation only applies to XT)
 176*
 177*
 178*	DMA -- choose DMA 1 or 3, and set both corresponding switches.
 179*
 180*	SW4	DMA 3
 181*	SW5	DMA 1
 182*	SW6	DMA 3
 183*	SW7	DMA 1
 184*
 185*
 186*	I/O address -- choose one.  
 187*
 188*	SW8	220 / 240
 189*/
 190
 191/*	To have some stuff logged, do 
 192*	insmod ltpc.o debug=1
 193*
 194*	For a whole bunch of stuff, use higher numbers.
 195*
 196*	The default is 0, i.e. no messages except for the probe results.
 197*/
 198
 199/* insmod-tweakable variables */
 200static int debug;
 201#define DEBUG_VERBOSE 1
 202#define DEBUG_UPPER 2
 203#define DEBUG_LOWER 4
 204
 205static int io;
 206static int irq;
 207static int dma;
 208
 209#include <linux/module.h>
 210#include <linux/kernel.h>
 211#include <linux/types.h>
 212#include <linux/fcntl.h>
 213#include <linux/interrupt.h>
 214#include <linux/ptrace.h>
 215#include <linux/ioport.h>
 216#include <linux/spinlock.h>
 217#include <linux/in.h>
 218#include <linux/string.h>
 219#include <linux/errno.h>
 220#include <linux/init.h>
 221#include <linux/netdevice.h>
 222#include <linux/etherdevice.h>
 223#include <linux/skbuff.h>
 224#include <linux/if_arp.h>
 225#include <linux/if_ltalk.h>
 226#include <linux/delay.h>
 227#include <linux/timer.h>
 228#include <linux/atalk.h>
 229#include <linux/bitops.h>
 230#include <linux/gfp.h>
 231
 232#include <net/Space.h>
 233
 234#include <asm/dma.h>
 235#include <asm/io.h>
 236
 237/* our stuff */
 238#include "ltpc.h"
 239
 240static DEFINE_SPINLOCK(txqueue_lock);
 241static DEFINE_SPINLOCK(mbox_lock);
 242
 243/* function prototypes */
 244static int do_read(struct net_device *dev, void *cbuf, int cbuflen,
 245	void *dbuf, int dbuflen);
 246static int sendup_buffer (struct net_device *dev);
 247
 248/* Dma Memory related stuff, cribbed directly from 3c505.c */
 249
 250static unsigned long dma_mem_alloc(int size)
 251{
 252        int order = get_order(size);
 253
 254        return __get_dma_pages(GFP_KERNEL, order);
 255}
 256
 257/* DMA data buffer, DMA command buffer */
 258static unsigned char *ltdmabuf;
 259static unsigned char *ltdmacbuf;
 260
 261/* private struct, holds our appletalk address */
 262
 263struct ltpc_private
 264{
 265	struct atalk_addr my_addr;
 266};
 267
 268/* transmit queue element struct */
 269
 270struct xmitQel {
 271	struct xmitQel *next;
 272	/* command buffer */
 273	unsigned char *cbuf;
 274	short cbuflen;
 275	/* data buffer */
 276	unsigned char *dbuf;
 277	short dbuflen;
 278	unsigned char QWrite;	/* read or write data */
 279	unsigned char mailbox;
 280};
 281
 282/* the transmit queue itself */
 283
 284static struct xmitQel *xmQhd, *xmQtl;
 285
 286static void enQ(struct xmitQel *qel)
 287{
 288	unsigned long flags;
 289	qel->next = NULL;
 290	
 291	spin_lock_irqsave(&txqueue_lock, flags);
 292	if (xmQtl) {
 293		xmQtl->next = qel;
 294	} else {
 295		xmQhd = qel;
 296	}
 297	xmQtl = qel;
 298	spin_unlock_irqrestore(&txqueue_lock, flags);
 299
 300	if (debug & DEBUG_LOWER)
 301		printk("enqueued a 0x%02x command\n",qel->cbuf[0]);
 302}
 303
 304static struct xmitQel *deQ(void)
 305{
 306	unsigned long flags;
 307	int i;
 308	struct xmitQel *qel=NULL;
 309	
 310	spin_lock_irqsave(&txqueue_lock, flags);
 311	if (xmQhd) {
 312		qel = xmQhd;
 313		xmQhd = qel->next;
 314		if(!xmQhd) xmQtl = NULL;
 315	}
 316	spin_unlock_irqrestore(&txqueue_lock, flags);
 317
 318	if ((debug & DEBUG_LOWER) && qel) {
 319		int n;
 320		printk(KERN_DEBUG "ltpc: dequeued command ");
 321		n = qel->cbuflen;
 322		if (n>100) n=100;
 323		for(i=0;i<n;i++) printk("%02x ",qel->cbuf[i]);
 324		printk("\n");
 325	}
 326
 327	return qel;
 328}
 329
 330/* and... the queue elements we'll be using */
 331static struct xmitQel qels[16];
 332
 333/* and their corresponding mailboxes */
 334static unsigned char mailbox[16];
 335static unsigned char mboxinuse[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
 336
 337static int wait_timeout(struct net_device *dev, int c)
 338{
 339	/* returns true if it stayed c */
 340	/* this uses base+6, but it's ok */
 341	int i;
 342
 343	/* twenty second or so total */
 344
 345	for(i=0;i<200000;i++) {
 346		if ( c != inb_p(dev->base_addr+6) ) return 0;
 347		udelay(100);
 348	}
 349	return 1; /* timed out */
 350}
 351
 352/* get the first free mailbox */
 353
 354static int getmbox(void)
 355{
 356	unsigned long flags;
 357	int i;
 358
 359	spin_lock_irqsave(&mbox_lock, flags);
 360	for(i=1;i<16;i++) if(!mboxinuse[i]) {
 361		mboxinuse[i]=1;
 362		spin_unlock_irqrestore(&mbox_lock, flags);
 363		return i;
 364	}
 365	spin_unlock_irqrestore(&mbox_lock, flags);
 366	return 0;
 367}
 368
 369/* read a command from the card */
 370static void handlefc(struct net_device *dev)
 371{
 372	/* called *only* from idle, non-reentrant */
 373	int dma = dev->dma;
 374	int base = dev->base_addr;
 375	unsigned long flags;
 376
 377
 378	flags=claim_dma_lock();
 379	disable_dma(dma);
 380	clear_dma_ff(dma);
 381	set_dma_mode(dma,DMA_MODE_READ);
 382	set_dma_addr(dma,virt_to_bus(ltdmacbuf));
 383	set_dma_count(dma,50);
 384	enable_dma(dma);
 385	release_dma_lock(flags);
 386
 387	inb_p(base+3);
 388	inb_p(base+2);
 389
 390	if ( wait_timeout(dev,0xfc) ) printk("timed out in handlefc\n");
 391}
 392
 393/* read data from the card */
 394static void handlefd(struct net_device *dev)
 395{
 396	int dma = dev->dma;
 397	int base = dev->base_addr;
 398	unsigned long flags;
 399
 400	flags=claim_dma_lock();
 401	disable_dma(dma);
 402	clear_dma_ff(dma);
 403	set_dma_mode(dma,DMA_MODE_READ);
 404	set_dma_addr(dma,virt_to_bus(ltdmabuf));
 405	set_dma_count(dma,800);
 406	enable_dma(dma);
 407	release_dma_lock(flags);
 408
 409	inb_p(base+3);
 410	inb_p(base+2);
 411
 412	if ( wait_timeout(dev,0xfd) ) printk("timed out in handlefd\n");
 413	sendup_buffer(dev);
 414} 
 415
 416static void handlewrite(struct net_device *dev)
 417{
 418	/* called *only* from idle, non-reentrant */
 419	/* on entry, 0xfb and ltdmabuf holds data */
 420	int dma = dev->dma;
 421	int base = dev->base_addr;
 422	unsigned long flags;
 423	
 424	flags=claim_dma_lock();
 425	disable_dma(dma);
 426	clear_dma_ff(dma);
 427	set_dma_mode(dma,DMA_MODE_WRITE);
 428	set_dma_addr(dma,virt_to_bus(ltdmabuf));
 429	set_dma_count(dma,800);
 430	enable_dma(dma);
 431	release_dma_lock(flags);
 432	
 433	inb_p(base+3);
 434	inb_p(base+2);
 435
 436	if ( wait_timeout(dev,0xfb) ) {
 437		flags=claim_dma_lock();
 438		printk("timed out in handlewrite, dma res %d\n",
 439			get_dma_residue(dev->dma) );
 440		release_dma_lock(flags);
 441	}
 442}
 443
 444static void handleread(struct net_device *dev)
 445{
 446	/* on entry, 0xfb */
 447	/* on exit, ltdmabuf holds data */
 448	int dma = dev->dma;
 449	int base = dev->base_addr;
 450	unsigned long flags;
 451
 452	
 453	flags=claim_dma_lock();
 454	disable_dma(dma);
 455	clear_dma_ff(dma);
 456	set_dma_mode(dma,DMA_MODE_READ);
 457	set_dma_addr(dma,virt_to_bus(ltdmabuf));
 458	set_dma_count(dma,800);
 459	enable_dma(dma);
 460	release_dma_lock(flags);
 461
 462	inb_p(base+3);
 463	inb_p(base+2);
 464	if ( wait_timeout(dev,0xfb) ) printk("timed out in handleread\n");
 465}
 466
 467static void handlecommand(struct net_device *dev)
 468{
 469	/* on entry, 0xfa and ltdmacbuf holds command */
 470	int dma = dev->dma;
 471	int base = dev->base_addr;
 472	unsigned long flags;
 473
 474	flags=claim_dma_lock();
 475	disable_dma(dma);
 476	clear_dma_ff(dma);
 477	set_dma_mode(dma,DMA_MODE_WRITE);
 478	set_dma_addr(dma,virt_to_bus(ltdmacbuf));
 479	set_dma_count(dma,50);
 480	enable_dma(dma);
 481	release_dma_lock(flags);
 482	inb_p(base+3);
 483	inb_p(base+2);
 484	if ( wait_timeout(dev,0xfa) ) printk("timed out in handlecommand\n");
 485} 
 486
 487/* ready made command for getting the result from the card */
 488static unsigned char rescbuf[2] = {LT_GETRESULT,0};
 489static unsigned char resdbuf[2];
 490
 491static int QInIdle;
 492
 493/* idle expects to be called with the IRQ line high -- either because of
 494 * an interrupt, or because the line is tri-stated
 495 */
 496
 497static void idle(struct net_device *dev)
 498{
 499	unsigned long flags;
 500	int state;
 501	/* FIXME This is initialized to shut the warning up, but I need to
 502	 * think this through again.
 503	 */
 504	struct xmitQel *q = NULL;
 505	int oops;
 506	int i;
 507	int base = dev->base_addr;
 508
 509	spin_lock_irqsave(&txqueue_lock, flags);
 510	if(QInIdle) {
 511		spin_unlock_irqrestore(&txqueue_lock, flags);
 512		return;
 513	}
 514	QInIdle = 1;
 515	spin_unlock_irqrestore(&txqueue_lock, flags);
 516
 517	/* this tri-states the IRQ line */
 518	(void) inb_p(base+6);
 519
 520	oops = 100;
 521
 522loop:
 523	if (0>oops--) { 
 524		printk("idle: looped too many times\n");
 525		goto done;
 526	}
 527
 528	state = inb_p(base+6);
 529	if (state != inb_p(base+6)) goto loop;
 530
 531	switch(state) {
 532		case 0xfc:
 533			/* incoming command */
 534			if (debug & DEBUG_LOWER) printk("idle: fc\n");
 535			handlefc(dev); 
 536			break;
 537		case 0xfd:
 538			/* incoming data */
 539			if(debug & DEBUG_LOWER) printk("idle: fd\n");
 540			handlefd(dev); 
 541			break;
 542		case 0xf9:
 543			/* result ready */
 544			if (debug & DEBUG_LOWER) printk("idle: f9\n");
 545			if(!mboxinuse[0]) {
 546				mboxinuse[0] = 1;
 547				qels[0].cbuf = rescbuf;
 548				qels[0].cbuflen = 2;
 549				qels[0].dbuf = resdbuf;
 550				qels[0].dbuflen = 2;
 551				qels[0].QWrite = 0;
 552				qels[0].mailbox = 0;
 553				enQ(&qels[0]);
 554			}
 555			inb_p(dev->base_addr+1);
 556			inb_p(dev->base_addr+0);
 557			if( wait_timeout(dev,0xf9) )
 558				printk("timed out idle f9\n");
 559			break;
 560		case 0xf8:
 561			/* ?? */
 562			if (xmQhd) {
 563				inb_p(dev->base_addr+1);
 564				inb_p(dev->base_addr+0);
 565				if(wait_timeout(dev,0xf8) )
 566					printk("timed out idle f8\n");
 567			} else {
 568				goto done;
 569			}
 570			break;
 571		case 0xfa:
 572			/* waiting for command */
 573			if(debug & DEBUG_LOWER) printk("idle: fa\n");
 574			if (xmQhd) {
 575				q=deQ();
 576				memcpy(ltdmacbuf,q->cbuf,q->cbuflen);
 577				ltdmacbuf[1] = q->mailbox;
 578				if (debug>1) { 
 579					int n;
 580					printk("ltpc: sent command     ");
 581					n = q->cbuflen;
 582					if (n>100) n=100;
 583					for(i=0;i<n;i++)
 584						printk("%02x ",ltdmacbuf[i]);
 585					printk("\n");
 586				}
 587
 588				handlecommand(dev);
 589
 590				if (0xfa == inb_p(base + 6)) {
 591					/* we timed out, so return */
 592					goto done;
 593				}
 594			} else {
 595				/* we don't seem to have a command */
 596				if (!mboxinuse[0]) {
 597					mboxinuse[0] = 1;
 598					qels[0].cbuf = rescbuf;
 599					qels[0].cbuflen = 2;
 600					qels[0].dbuf = resdbuf;
 601					qels[0].dbuflen = 2;
 602					qels[0].QWrite = 0;
 603					qels[0].mailbox = 0;
 604					enQ(&qels[0]);
 605				} else {
 606					printk("trouble: response command already queued\n");
 607					goto done;
 608				}
 609			} 
 610			break;
 611		case 0Xfb:
 612			/* data transfer ready */
 613			if(debug & DEBUG_LOWER) printk("idle: fb\n");
 614			if(q->QWrite) {
 615				memcpy(ltdmabuf,q->dbuf,q->dbuflen);
 616				handlewrite(dev);
 617			} else {
 618				handleread(dev);
 619				/* non-zero mailbox numbers are for
 620				   commmands, 0 is for GETRESULT
 621				   requests */
 622				if(q->mailbox) {
 623					memcpy(q->dbuf,ltdmabuf,q->dbuflen);
 624				} else { 
 625					/* this was a result */
 626					mailbox[ 0x0f & ltdmabuf[0] ] = ltdmabuf[1];
 627					mboxinuse[0]=0;
 628				}
 629			}
 630			break;
 631	}
 632	goto loop;
 633
 634done:
 635	QInIdle=0;
 636
 637	/* now set the interrupts back as appropriate */
 638	/* the first read takes it out of tri-state (but still high) */
 639	/* the second resets it */
 640	/* note that after this point, any read of base+6 will
 641	   trigger an interrupt */
 642
 643	if (dev->irq) {
 644		inb_p(base+7);
 645		inb_p(base+7);
 646	}
 647}
 648
 649
 650static int do_write(struct net_device *dev, void *cbuf, int cbuflen,
 651	void *dbuf, int dbuflen)
 652{
 653
 654	int i = getmbox();
 655	int ret;
 656
 657	if(i) {
 658		qels[i].cbuf = cbuf;
 659		qels[i].cbuflen = cbuflen;
 660		qels[i].dbuf = dbuf;
 661		qels[i].dbuflen = dbuflen;
 662		qels[i].QWrite = 1;
 663		qels[i].mailbox = i;  /* this should be initted rather */
 664		enQ(&qels[i]);
 665		idle(dev);
 666		ret = mailbox[i];
 667		mboxinuse[i]=0;
 668		return ret;
 669	}
 670	printk("ltpc: could not allocate mbox\n");
 671	return -1;
 672}
 673
 674static int do_read(struct net_device *dev, void *cbuf, int cbuflen,
 675	void *dbuf, int dbuflen)
 676{
 677
 678	int i = getmbox();
 679	int ret;
 680
 681	if(i) {
 682		qels[i].cbuf = cbuf;
 683		qels[i].cbuflen = cbuflen;
 684		qels[i].dbuf = dbuf;
 685		qels[i].dbuflen = dbuflen;
 686		qels[i].QWrite = 0;
 687		qels[i].mailbox = i;  /* this should be initted rather */
 688		enQ(&qels[i]);
 689		idle(dev);
 690		ret = mailbox[i];
 691		mboxinuse[i]=0;
 692		return ret;
 693	}
 694	printk("ltpc: could not allocate mbox\n");
 695	return -1;
 696}
 697
 698/* end of idle handlers -- what should be seen is do_read, do_write */
 699
 700static struct timer_list ltpc_timer;
 701static struct net_device *ltpc_timer_dev;
 702
 703static netdev_tx_t ltpc_xmit(struct sk_buff *skb, struct net_device *dev);
 704
 705static int read_30 ( struct net_device *dev)
 706{
 707	lt_command c;
 708	c.getflags.command = LT_GETFLAGS;
 709	return do_read(dev, &c, sizeof(c.getflags),&c,0);
 710}
 711
 712static int set_30 (struct net_device *dev,int x)
 713{
 714	lt_command c;
 715	c.setflags.command = LT_SETFLAGS;
 716	c.setflags.flags = x;
 717	return do_write(dev, &c, sizeof(c.setflags),&c,0);
 718}
 719
 720/* LLAP to DDP translation */
 721
 722static int sendup_buffer (struct net_device *dev)
 723{
 724	/* on entry, command is in ltdmacbuf, data in ltdmabuf */
 725	/* called from idle, non-reentrant */
 726
 727	int dnode, snode, llaptype, len; 
 728	int sklen;
 729	struct sk_buff *skb;
 730	struct lt_rcvlap *ltc = (struct lt_rcvlap *) ltdmacbuf;
 731
 732	if (ltc->command != LT_RCVLAP) {
 733		printk("unknown command 0x%02x from ltpc card\n",ltc->command);
 734		return -1;
 735	}
 736	dnode = ltc->dnode;
 737	snode = ltc->snode;
 738	llaptype = ltc->laptype;
 739	len = ltc->length; 
 740
 741	sklen = len;
 742	if (llaptype == 1) 
 743		sklen += 8;  /* correct for short ddp */
 744	if(sklen > 800) {
 745		printk(KERN_INFO "%s: nonsense length in ltpc command 0x14: 0x%08x\n",
 746			dev->name,sklen);
 747		return -1;
 748	}
 749
 750	if ( (llaptype==0) || (llaptype>2) ) {
 751		printk(KERN_INFO "%s: unknown LLAP type: %d\n",dev->name,llaptype);
 752		return -1;
 753	}
 754
 755
 756	skb = dev_alloc_skb(3+sklen);
 757	if (skb == NULL) 
 758	{
 759		printk("%s: dropping packet due to memory squeeze.\n",
 760			dev->name);
 761		return -1;
 762	}
 763	skb->dev = dev;
 764
 765	if (sklen > len)
 766		skb_reserve(skb,8);
 767	skb_put(skb,len+3);
 768	skb->protocol = htons(ETH_P_LOCALTALK);
 769	/* add LLAP header */
 770	skb->data[0] = dnode;
 771	skb->data[1] = snode;
 772	skb->data[2] = llaptype;
 773	skb_reset_mac_header(skb);	/* save pointer to llap header */
 774	skb_pull(skb,3);
 775
 776	/* copy ddp(s,e)hdr + contents */
 777	skb_copy_to_linear_data(skb, ltdmabuf, len);
 778
 779	skb_reset_transport_header(skb);
 780
 781	dev->stats.rx_packets++;
 782	dev->stats.rx_bytes += skb->len;
 783
 784	/* toss it onwards */
 785	netif_rx(skb);
 786	return 0;
 787}
 788
 789/* the handler for the board interrupt */
 790 
 791static irqreturn_t
 792ltpc_interrupt(int irq, void *dev_id)
 793{
 794	struct net_device *dev = dev_id;
 795
 796	if (dev==NULL) {
 797		printk("ltpc_interrupt: unknown device.\n");
 798		return IRQ_NONE;
 799	}
 800
 801	inb_p(dev->base_addr+6);  /* disable further interrupts from board */
 802
 803	idle(dev); /* handle whatever is coming in */
 804 
 805	/* idle re-enables interrupts from board */ 
 806
 807	return IRQ_HANDLED;
 808}
 809
 810/***
 811 *
 812 *    The ioctls that the driver responds to are:
 813 *
 814 *    SIOCSIFADDR -- do probe using the passed node hint.
 815 *    SIOCGIFADDR -- return net, node.
 816 *
 817 *    some of this stuff should be done elsewhere.
 818 *
 819 ***/
 820
 821static int ltpc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
 822{
 823	struct sockaddr_at *sa = (struct sockaddr_at *) &ifr->ifr_addr;
 824	/* we'll keep the localtalk node address in dev->pa_addr */
 825	struct ltpc_private *ltpc_priv = netdev_priv(dev);
 826	struct atalk_addr *aa = &ltpc_priv->my_addr;
 827	struct lt_init c;
 828	int ltflags;
 829
 830	if(debug & DEBUG_VERBOSE) printk("ltpc_ioctl called\n");
 831
 832	switch(cmd) {
 833		case SIOCSIFADDR:
 834
 835			aa->s_net  = sa->sat_addr.s_net;
 836      
 837			/* this does the probe and returns the node addr */
 838			c.command = LT_INIT;
 839			c.hint = sa->sat_addr.s_node;
 840
 841			aa->s_node = do_read(dev,&c,sizeof(c),&c,0);
 842
 843			/* get all llap frames raw */
 844			ltflags = read_30(dev);
 845			ltflags |= LT_FLAG_ALLLAP;
 846			set_30 (dev,ltflags);  
 847
 848			dev->broadcast[0] = 0xFF;
 849			dev->dev_addr[0] = aa->s_node;
 850
 851			dev->addr_len=1;
 852   
 853			return 0;
 854
 855		case SIOCGIFADDR:
 856
 857			sa->sat_addr.s_net = aa->s_net;
 858			sa->sat_addr.s_node = aa->s_node;
 859
 860			return 0;
 861
 862		default: 
 863			return -EINVAL;
 864	}
 865}
 866
 867static void set_multicast_list(struct net_device *dev)
 868{
 869	/* This needs to be present to keep netatalk happy. */
 870	/* Actually netatalk needs fixing! */
 871}
 872
 873static int ltpc_poll_counter;
 874
 875static void ltpc_poll(struct timer_list *unused)
 876{
 877	del_timer(&ltpc_timer);
 878
 879	if(debug & DEBUG_VERBOSE) {
 880		if (!ltpc_poll_counter) {
 881			ltpc_poll_counter = 50;
 882			printk("ltpc poll is alive\n");
 883		}
 884		ltpc_poll_counter--;
 885	}
 886
 887	/* poll 20 times per second */
 888	idle(ltpc_timer_dev);
 889	ltpc_timer.expires = jiffies + HZ/20;
 890	add_timer(&ltpc_timer);
 891}
 892
 893/* DDP to LLAP translation */
 894
 895static netdev_tx_t ltpc_xmit(struct sk_buff *skb, struct net_device *dev)
 896{
 897	/* in kernel 1.3.xx, on entry skb->data points to ddp header,
 898	 * and skb->len is the length of the ddp data + ddp header
 899	 */
 900	int i;
 901	struct lt_sendlap cbuf;
 902	unsigned char *hdr;
 903
 904	cbuf.command = LT_SENDLAP;
 905	cbuf.dnode = skb->data[0];
 906	cbuf.laptype = skb->data[2];
 907	skb_pull(skb,3);	/* skip past LLAP header */
 908	cbuf.length = skb->len;	/* this is host order */
 909	skb_reset_transport_header(skb);
 910
 911	if(debug & DEBUG_UPPER) {
 912		printk("command ");
 913		for(i=0;i<6;i++)
 914			printk("%02x ",((unsigned char *)&cbuf)[i]);
 915		printk("\n");
 916	}
 917
 918	hdr = skb_transport_header(skb);
 919	do_write(dev, &cbuf, sizeof(cbuf), hdr, skb->len);
 920
 921	if(debug & DEBUG_UPPER) {
 922		printk("sent %d ddp bytes\n",skb->len);
 923		for (i = 0; i < skb->len; i++)
 924			printk("%02x ", hdr[i]);
 925		printk("\n");
 926	}
 927
 928	dev->stats.tx_packets++;
 929	dev->stats.tx_bytes += skb->len;
 930
 931	dev_kfree_skb(skb);
 932	return NETDEV_TX_OK;
 933}
 934
 935/* initialization stuff */
 936  
 937static int __init ltpc_probe_dma(int base, int dma)
 938{
 939	int want = (dma == 3) ? 2 : (dma == 1) ? 1 : 3;
 940	unsigned long timeout;
 941	unsigned long f;
 942  
 943	if (want & 1) {
 944		if (request_dma(1,"ltpc")) {
 945			want &= ~1;
 946		} else {
 947			f=claim_dma_lock();
 948			disable_dma(1);
 949			clear_dma_ff(1);
 950			set_dma_mode(1,DMA_MODE_WRITE);
 951			set_dma_addr(1,virt_to_bus(ltdmabuf));
 952			set_dma_count(1,sizeof(struct lt_mem));
 953			enable_dma(1);
 954			release_dma_lock(f);
 955		}
 956	}
 957	if (want & 2) {
 958		if (request_dma(3,"ltpc")) {
 959			want &= ~2;
 960		} else {
 961			f=claim_dma_lock();
 962			disable_dma(3);
 963			clear_dma_ff(3);
 964			set_dma_mode(3,DMA_MODE_WRITE);
 965			set_dma_addr(3,virt_to_bus(ltdmabuf));
 966			set_dma_count(3,sizeof(struct lt_mem));
 967			enable_dma(3);
 968			release_dma_lock(f);
 969		}
 970	}
 971	/* set up request */
 972
 973	/* FIXME -- do timings better! */
 974
 975	ltdmabuf[0] = LT_READMEM;
 976	ltdmabuf[1] = 1;  /* mailbox */
 977	ltdmabuf[2] = 0; ltdmabuf[3] = 0;  /* address */
 978	ltdmabuf[4] = 0; ltdmabuf[5] = 1;  /* read 0x0100 bytes */
 979	ltdmabuf[6] = 0; /* dunno if this is necessary */
 980
 981	inb_p(io+1);
 982	inb_p(io+0);
 983	timeout = jiffies+100*HZ/100;
 984	while(time_before(jiffies, timeout)) {
 985		if ( 0xfa == inb_p(io+6) ) break;
 986	}
 987
 988	inb_p(io+3);
 989	inb_p(io+2);
 990	while(time_before(jiffies, timeout)) {
 991		if ( 0xfb == inb_p(io+6) ) break;
 992	}
 993
 994	/* release the other dma channel (if we opened both of them) */
 995
 996	if ((want & 2) && (get_dma_residue(3)==sizeof(struct lt_mem))) {
 997		want &= ~2;
 998		free_dma(3);
 999	}
1000
1001	if ((want & 1) && (get_dma_residue(1)==sizeof(struct lt_mem))) {
1002		want &= ~1;
1003		free_dma(1);
1004	}
1005
1006	if (!want)
1007		return 0;
1008
1009	return (want & 2) ? 3 : 1;
1010}
1011
1012static const struct net_device_ops ltpc_netdev = {
1013	.ndo_start_xmit		= ltpc_xmit,
1014	.ndo_do_ioctl		= ltpc_ioctl,
1015	.ndo_set_rx_mode	= set_multicast_list,
1016};
1017
1018struct net_device * __init ltpc_probe(void)
1019{
1020	struct net_device *dev;
1021	int err = -ENOMEM;
1022	int x=0,y=0;
1023	int autoirq;
1024	unsigned long f;
1025	unsigned long timeout;
1026
1027	dev = alloc_ltalkdev(sizeof(struct ltpc_private));
1028	if (!dev)
1029		goto out;
1030
1031	/* probe for the I/O port address */
1032	
1033	if (io != 0x240 && request_region(0x220,8,"ltpc")) {
1034		x = inb_p(0x220+6);
1035		if ( (x!=0xff) && (x>=0xf0) ) {
1036			io = 0x220;
1037			goto got_port;
1038		}
1039		release_region(0x220,8);
1040	}
1041	if (io != 0x220 && request_region(0x240,8,"ltpc")) {
1042		y = inb_p(0x240+6);
1043		if ( (y!=0xff) && (y>=0xf0) ){ 
1044			io = 0x240;
1045			goto got_port;
1046		}
1047		release_region(0x240,8);
1048	} 
1049
1050	/* give up in despair */
1051	printk(KERN_ERR "LocalTalk card not found; 220 = %02x, 240 = %02x.\n", x,y);
1052	err = -ENODEV;
1053	goto out1;
1054
1055 got_port:
1056	/* probe for the IRQ line */
1057	if (irq < 2) {
1058		unsigned long irq_mask;
1059
1060		irq_mask = probe_irq_on();
1061		/* reset the interrupt line */
1062		inb_p(io+7);
1063		inb_p(io+7);
1064		/* trigger an interrupt (I hope) */
1065		inb_p(io+6);
1066		mdelay(2);
1067		autoirq = probe_irq_off(irq_mask);
1068
1069		if (autoirq == 0) {
1070			printk(KERN_ERR "ltpc: probe at %#x failed to detect IRQ line.\n", io);
1071		} else {
1072			irq = autoirq;
1073		}
1074	}
1075
1076	/* allocate a DMA buffer */
1077	ltdmabuf = (unsigned char *) dma_mem_alloc(1000);
1078	if (!ltdmabuf) {
1079		printk(KERN_ERR "ltpc: mem alloc failed\n");
1080		err = -ENOMEM;
1081		goto out2;
1082	}
1083
1084	ltdmacbuf = &ltdmabuf[800];
1085
1086	if(debug & DEBUG_VERBOSE) {
1087		printk("ltdmabuf pointer %08lx\n",(unsigned long) ltdmabuf);
1088	}
1089
1090	/* reset the card */
1091
1092	inb_p(io+1);
1093	inb_p(io+3);
1094
1095	msleep(20);
1096
1097	inb_p(io+0);
1098	inb_p(io+2);
1099	inb_p(io+7); /* clear reset */
1100	inb_p(io+4); 
1101	inb_p(io+5);
1102	inb_p(io+5); /* enable dma */
1103	inb_p(io+6); /* tri-state interrupt line */
1104
1105	ssleep(1);
1106	
1107	/* now, figure out which dma channel we're using, unless it's
1108	   already been specified */
1109	/* well, 0 is a legal DMA channel, but the LTPC card doesn't
1110	   use it... */
1111	dma = ltpc_probe_dma(io, dma);
1112	if (!dma) {  /* no dma channel */
1113		printk(KERN_ERR "No DMA channel found on ltpc card.\n");
1114		err = -ENODEV;
1115		goto out3;
1116	}
1117
1118	/* print out friendly message */
1119	if(irq)
1120		printk(KERN_INFO "Apple/Farallon LocalTalk-PC card at %03x, IR%d, DMA%d.\n",io,irq,dma);
1121	else
1122		printk(KERN_INFO "Apple/Farallon LocalTalk-PC card at %03x, DMA%d.  Using polled mode.\n",io,dma);
1123
1124	dev->netdev_ops = &ltpc_netdev;
1125	dev->base_addr = io;
1126	dev->irq = irq;
1127	dev->dma = dma;
1128
1129	/* the card will want to send a result at this point */
1130	/* (I think... leaving out this part makes the kernel crash,
1131           so I put it back in...) */
1132
1133	f=claim_dma_lock();
1134	disable_dma(dma);
1135	clear_dma_ff(dma);
1136	set_dma_mode(dma,DMA_MODE_READ);
1137	set_dma_addr(dma,virt_to_bus(ltdmabuf));
1138	set_dma_count(dma,0x100);
1139	enable_dma(dma);
1140	release_dma_lock(f);
1141
1142	(void) inb_p(io+3);
1143	(void) inb_p(io+2);
1144	timeout = jiffies+100*HZ/100;
1145
1146	while(time_before(jiffies, timeout)) {
1147		if( 0xf9 == inb_p(io+6))
1148			break;
1149		schedule();
1150	}
1151
1152	if(debug & DEBUG_VERBOSE) {
1153		printk("setting up timer and irq\n");
1154	}
1155
1156	/* grab it and don't let go :-) */
1157	if (irq && request_irq( irq, ltpc_interrupt, 0, "ltpc", dev) >= 0)
1158	{
1159		(void) inb_p(io+7);  /* enable interrupts from board */
1160		(void) inb_p(io+7);  /* and reset irq line */
1161	} else {
1162		if( irq )
1163			printk(KERN_ERR "ltpc: IRQ already in use, using polled mode.\n");
1164		dev->irq = 0;
1165		/* polled mode -- 20 times per second */
1166		/* this is really, really slow... should it poll more often? */
1167		ltpc_timer_dev = dev;
1168		timer_setup(&ltpc_timer, ltpc_poll, 0);
1169
1170		ltpc_timer.expires = jiffies + HZ/20;
1171		add_timer(&ltpc_timer);
1172	}
1173	err = register_netdev(dev);
1174	if (err)
1175		goto out4;
1176
1177	return NULL;
1178out4:
1179	del_timer_sync(&ltpc_timer);
1180	if (dev->irq)
1181		free_irq(dev->irq, dev);
1182out3:
1183	free_pages((unsigned long)ltdmabuf, get_order(1000));
1184out2:
1185	release_region(io, 8);
1186out1:
1187	free_netdev(dev);
1188out:
1189	return ERR_PTR(err);
1190}
1191
1192#ifndef MODULE
1193/* handles "ltpc=io,irq,dma" kernel command lines */
1194static int __init ltpc_setup(char *str)
1195{
1196	int ints[5];
1197
1198	str = get_options(str, ARRAY_SIZE(ints), ints);
1199
1200	if (ints[0] == 0) {
1201		if (str && !strncmp(str, "auto", 4)) {
1202			/* do nothing :-) */
1203		}
1204		else {
1205			/* usage message */
1206			printk (KERN_ERR
1207				"ltpc: usage: ltpc=auto|iobase[,irq[,dma]]\n");
1208			return 0;
1209		}
1210	} else {
1211		io = ints[1];
1212		if (ints[0] > 1) {
1213			irq = ints[2];
1214		}
1215		if (ints[0] > 2) {
1216			dma = ints[3];
1217		}
1218		/* ignore any other parameters */
1219	}
1220	return 1;
1221}
1222
1223__setup("ltpc=", ltpc_setup);
1224#endif /* MODULE */
1225
1226static struct net_device *dev_ltpc;
1227
1228#ifdef MODULE
1229
1230MODULE_LICENSE("GPL");
1231module_param(debug, int, 0);
1232module_param_hw(io, int, ioport, 0);
1233module_param_hw(irq, int, irq, 0);
1234module_param_hw(dma, int, dma, 0);
1235
1236
1237static int __init ltpc_module_init(void)
1238{
1239        if(io == 0)
1240		printk(KERN_NOTICE
1241		       "ltpc: Autoprobing is not recommended for modules\n");
1242
1243	dev_ltpc = ltpc_probe();
1244	return PTR_ERR_OR_ZERO(dev_ltpc);
1245}
1246module_init(ltpc_module_init);
1247#endif
1248
1249static void __exit ltpc_cleanup(void)
1250{
1251
1252	if(debug & DEBUG_VERBOSE) printk("unregister_netdev\n");
1253	unregister_netdev(dev_ltpc);
1254
1255	del_timer_sync(&ltpc_timer);
1256
1257	if(debug & DEBUG_VERBOSE) printk("freeing irq\n");
1258
1259	if (dev_ltpc->irq)
1260		free_irq(dev_ltpc->irq, dev_ltpc);
1261
1262	if(debug & DEBUG_VERBOSE) printk("freeing dma\n");
1263
1264	if (dev_ltpc->dma)
1265		free_dma(dev_ltpc->dma);
1266
1267	if(debug & DEBUG_VERBOSE) printk("freeing ioaddr\n");
1268
1269	if (dev_ltpc->base_addr)
1270		release_region(dev_ltpc->base_addr,8);
1271
1272	free_netdev(dev_ltpc);
1273
1274	if(debug & DEBUG_VERBOSE) printk("free_pages\n");
1275
1276	free_pages( (unsigned long) ltdmabuf, get_order(1000));
1277
1278	if(debug & DEBUG_VERBOSE) printk("returning from cleanup_module\n");
1279}
1280
1281module_exit(ltpc_cleanup);