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 */
200 static int debug;
201 #define DEBUG_VERBOSE 1
202 #define DEBUG_UPPER 2
203 #define DEBUG_LOWER 4
204 
205 static int io;
206 static int irq;
207 static int dma;
208 
209 #include <linux/module.h>
210 #include <linux/kernel.h>
211 #include <linux/sched.h>
212 #include <linux/types.h>
213 #include <linux/fcntl.h>
214 #include <linux/interrupt.h>
215 #include <linux/ptrace.h>
216 #include <linux/ioport.h>
217 #include <linux/in.h>
218 #include <linux/slab.h>
219 #include <linux/string.h>
220 #include <asm/system.h>
221 #include <asm/bitops.h>
222 #include <asm/dma.h>
223 #include <asm/io.h>
224 #include <linux/errno.h>
225 #include <linux/init.h>
226 
227 #include <linux/netdevice.h>
228 #include <linux/etherdevice.h>
229 #include <linux/skbuff.h>
230 
231 #include <linux/if_arp.h>
232 #include <linux/if_ltalk.h>
233 
234 #include <linux/delay.h>
235 #include <linux/timer.h>
236 
237 #include <linux/atalk.h>
238 
239 /* our stuff */
240 #include "ltpc.h"
241 
242 /* function prototypes */
243 static int do_read(struct net_device *dev, void *cbuf, int cbuflen,
244 	void *dbuf, int dbuflen);
245 static int sendup_buffer (struct net_device *dev);
246 
247 /* Dma Memory related stuff, cribbed directly from 3c505.c */
248 
dma_mem_alloc(int size)249 static unsigned long dma_mem_alloc(int size)
250 {
251         int order = get_order(size);
252 
253         return __get_dma_pages(GFP_KERNEL, order);
254 }
255 
256 /* DMA data buffer, DMA command buffer */
257 static unsigned char *ltdmabuf;
258 static unsigned char *ltdmacbuf;
259 
260 /* private struct, holds our appletalk address */
261 
262 struct ltpc_private
263 {
264 	struct net_device_stats stats;
265 	struct at_addr my_addr;
266 };
267 
268 /* transmit queue element struct */
269 
270 struct 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 
284 static struct xmitQel *xmQhd, *xmQtl;
285 
enQ(struct xmitQel * qel)286 static void enQ(struct xmitQel *qel)
287 {
288 	unsigned long flags;
289 	qel->next = NULL;
290 	save_flags(flags);
291 	cli();
292 	if (xmQtl) {
293 		xmQtl->next = qel;
294 	} else {
295 		xmQhd = qel;
296 	}
297 	xmQtl = qel;
298 	restore_flags(flags);
299 
300 	if (debug&DEBUG_LOWER)
301 		printk("enqueued a 0x%02x command\n",qel->cbuf[0]);
302 }
303 
deQ(void)304 static struct xmitQel *deQ(void)
305 {
306 	unsigned long flags;
307 	int i;
308 	struct xmitQel *qel=NULL;
309 	save_flags(flags);
310 	cli();
311 	if (xmQhd) {
312 		qel = xmQhd;
313 		xmQhd = qel->next;
314 		if(!xmQhd) xmQtl = NULL;
315 	}
316 	restore_flags(flags);
317 
318 	if ((debug&DEBUG_LOWER) && qel) {
319 		int n;
320 		printk("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 */
331 static struct xmitQel qels[16];
332 
333 /* and their corresponding mailboxes */
334 static unsigned char mailbox[16];
335 static unsigned char mboxinuse[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
336 
wait_timeout(struct net_device * dev,int c)337 static 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 
getmbox(void)354 static int getmbox(void)
355 {
356 	unsigned long flags;
357 	int i;
358 
359 	save_flags(flags);
360 	cli();
361 	for(i=1;i<16;i++) if(!mboxinuse[i]) {
362 		mboxinuse[i]=1;
363 		restore_flags(flags);
364 		return i;
365 	}
366 	restore_flags(flags);
367 	return 0;
368 }
369 
370 /* read a command from the card */
handlefc(struct net_device * dev)371 static void handlefc(struct net_device *dev)
372 {
373 	/* called *only* from idle, non-reentrant */
374 	int dma = dev->dma;
375 	int base = dev->base_addr;
376 	unsigned long flags;
377 
378 
379 	flags=claim_dma_lock();
380 	disable_dma(dma);
381 	clear_dma_ff(dma);
382 	set_dma_mode(dma,DMA_MODE_READ);
383 	set_dma_addr(dma,virt_to_bus(ltdmacbuf));
384 	set_dma_count(dma,50);
385 	enable_dma(dma);
386 	release_dma_lock(flags);
387 
388 	inb_p(base+3);
389 	inb_p(base+2);
390 
391 	if ( wait_timeout(dev,0xfc) ) printk("timed out in handlefc\n");
392 }
393 
394 /* read data from the card */
handlefd(struct net_device * dev)395 static void handlefd(struct net_device *dev)
396 {
397 	int dma = dev->dma;
398 	int base = dev->base_addr;
399 	unsigned long flags;
400 
401 	flags=claim_dma_lock();
402 	disable_dma(dma);
403 	clear_dma_ff(dma);
404 	set_dma_mode(dma,DMA_MODE_READ);
405 	set_dma_addr(dma,virt_to_bus(ltdmabuf));
406 	set_dma_count(dma,800);
407 	enable_dma(dma);
408 	release_dma_lock(flags);
409 
410 	inb_p(base+3);
411 	inb_p(base+2);
412 
413 	if ( wait_timeout(dev,0xfd) ) printk("timed out in handlefd\n");
414 	sendup_buffer(dev);
415 }
416 
handlewrite(struct net_device * dev)417 static void handlewrite(struct net_device *dev)
418 {
419 	/* called *only* from idle, non-reentrant */
420 	/* on entry, 0xfb and ltdmabuf holds data */
421 	int dma = dev->dma;
422 	int base = dev->base_addr;
423 	unsigned long flags;
424 
425 	flags=claim_dma_lock();
426 	disable_dma(dma);
427 	clear_dma_ff(dma);
428 	set_dma_mode(dma,DMA_MODE_WRITE);
429 	set_dma_addr(dma,virt_to_bus(ltdmabuf));
430 	set_dma_count(dma,800);
431 	enable_dma(dma);
432 	release_dma_lock(flags);
433 
434 	inb_p(base+3);
435 	inb_p(base+2);
436 
437 	if ( wait_timeout(dev,0xfb) ) {
438 		flags=claim_dma_lock();
439 		printk("timed out in handlewrite, dma res %d\n",
440 			get_dma_residue(dev->dma) );
441 		release_dma_lock(flags);
442 	}
443 }
444 
handleread(struct net_device * dev)445 static void handleread(struct net_device *dev)
446 {
447 	/* on entry, 0xfb */
448 	/* on exit, ltdmabuf holds data */
449 	int dma = dev->dma;
450 	int base = dev->base_addr;
451 	unsigned long flags;
452 
453 
454 	flags=claim_dma_lock();
455 	disable_dma(dma);
456 	clear_dma_ff(dma);
457 	set_dma_mode(dma,DMA_MODE_READ);
458 	set_dma_addr(dma,virt_to_bus(ltdmabuf));
459 	set_dma_count(dma,800);
460 	enable_dma(dma);
461 	release_dma_lock(flags);
462 
463 	inb_p(base+3);
464 	inb_p(base+2);
465 	if ( wait_timeout(dev,0xfb) ) printk("timed out in handleread\n");
466 }
467 
handlecommand(struct net_device * dev)468 static void handlecommand(struct net_device *dev)
469 {
470 	/* on entry, 0xfa and ltdmacbuf holds command */
471 	int dma = dev->dma;
472 	int base = dev->base_addr;
473 	unsigned long flags;
474 
475 	flags=claim_dma_lock();
476 	disable_dma(dma);
477 	clear_dma_ff(dma);
478 	set_dma_mode(dma,DMA_MODE_WRITE);
479 	set_dma_addr(dma,virt_to_bus(ltdmacbuf));
480 	set_dma_count(dma,50);
481 	enable_dma(dma);
482 	release_dma_lock(flags);
483 	inb_p(base+3);
484 	inb_p(base+2);
485 	if ( wait_timeout(dev,0xfa) ) printk("timed out in handlecommand\n");
486 }
487 
488 /* ready made command for getting the result from the card */
489 static unsigned char rescbuf[2] = {LT_GETRESULT,0};
490 static unsigned char resdbuf[2];
491 
492 static int QInIdle;
493 
494 /* idle expects to be called with the IRQ line high -- either because of
495  * an interrupt, or because the line is tri-stated
496  */
497 
idle(struct net_device * dev)498 static void idle(struct net_device *dev)
499 {
500 	unsigned long flags;
501 	int state;
502 	/* FIXME This is initialized to shut the warning up, but I need to
503 	 * think this through again.
504 	 */
505 	struct xmitQel *q=0;
506 	int oops;
507 	int i;
508 	int base = dev->base_addr;
509 
510 	save_flags(flags);
511 	cli();
512 	if(QInIdle) {
513 		restore_flags(flags);
514 		return;
515 	}
516 	QInIdle = 1;
517 
518 
519 	restore_flags(flags);
520 
521 	/* this tri-states the IRQ line */
522 	(void) inb_p(base+6);
523 
524 	oops = 100;
525 
526 loop:
527 	if (0>oops--) {
528 		printk("idle: looped too many times\n");
529 		goto done;
530 	}
531 
532 	state = inb_p(base+6);
533 	if (state != inb_p(base+6)) goto loop;
534 
535 	switch(state) {
536 		case 0xfc:
537 			/* incoming command */
538 			if (debug&DEBUG_LOWER) printk("idle: fc\n");
539 			handlefc(dev);
540 			break;
541 		case 0xfd:
542 			/* incoming data */
543 			if(debug&DEBUG_LOWER) printk("idle: fd\n");
544 			handlefd(dev);
545 			break;
546 		case 0xf9:
547 			/* result ready */
548 			if (debug&DEBUG_LOWER) printk("idle: f9\n");
549 			if(!mboxinuse[0]) {
550 				mboxinuse[0] = 1;
551 				qels[0].cbuf = rescbuf;
552 				qels[0].cbuflen = 2;
553 				qels[0].dbuf = resdbuf;
554 				qels[0].dbuflen = 2;
555 				qels[0].QWrite = 0;
556 				qels[0].mailbox = 0;
557 				enQ(&qels[0]);
558 			}
559 			inb_p(dev->base_addr+1);
560 			inb_p(dev->base_addr+0);
561 			if( wait_timeout(dev,0xf9) )
562 				printk("timed out idle f9\n");
563 			break;
564 		case 0xf8:
565 			/* ?? */
566 			if (xmQhd) {
567 				inb_p(dev->base_addr+1);
568 				inb_p(dev->base_addr+0);
569 				if(wait_timeout(dev,0xf8) )
570 					printk("timed out idle f8\n");
571 			} else {
572 				goto done;
573 			}
574 			break;
575 		case 0xfa:
576 			/* waiting for command */
577 			if(debug&DEBUG_LOWER) printk("idle: fa\n");
578 			if (xmQhd) {
579 				q=deQ();
580 				memcpy(ltdmacbuf,q->cbuf,q->cbuflen);
581 				ltdmacbuf[1] = q->mailbox;
582 				if (debug>1) {
583 					int n;
584 					printk("ltpc: sent command     ");
585 					n = q->cbuflen;
586 					if (n>100) n=100;
587 					for(i=0;i<n;i++)
588 						printk("%02x ",ltdmacbuf[i]);
589 					printk("\n");
590 				}
591 				handlecommand(dev);
592 					if(0xfa==inb_p(base+6)) {
593 						/* we timed out, so return */
594 						goto done;
595 					}
596 			} else {
597 				/* we don't seem to have a command */
598 				if (!mboxinuse[0]) {
599 					mboxinuse[0] = 1;
600 					qels[0].cbuf = rescbuf;
601 					qels[0].cbuflen = 2;
602 					qels[0].dbuf = resdbuf;
603 					qels[0].dbuflen = 2;
604 					qels[0].QWrite = 0;
605 					qels[0].mailbox = 0;
606 					enQ(&qels[0]);
607 				} else {
608 					printk("trouble: response command already queued\n");
609 					goto done;
610 				}
611 			}
612 			break;
613 		case 0Xfb:
614 			/* data transfer ready */
615 			if(debug&DEBUG_LOWER) printk("idle: fb\n");
616 			if(q->QWrite) {
617 				memcpy(ltdmabuf,q->dbuf,q->dbuflen);
618 				handlewrite(dev);
619 			} else {
620 				handleread(dev);
621 				/* non-zero mailbox numbers are for
622 				   commmands, 0 is for GETRESULT
623 				   requests */
624 				if(q->mailbox) {
625 					memcpy(q->dbuf,ltdmabuf,q->dbuflen);
626 				} else {
627 					/* this was a result */
628 					mailbox[ 0x0f & ltdmabuf[0] ] = ltdmabuf[1];
629 					mboxinuse[0]=0;
630 				}
631 			}
632 			break;
633 	}
634 	goto loop;
635 
636 done:
637 	QInIdle=0;
638 
639 	/* now set the interrupts back as appropriate */
640 	/* the first read takes it out of tri-state (but still high) */
641 	/* the second resets it */
642 	/* note that after this point, any read of base+6 will
643 	   trigger an interrupt */
644 
645 	if (dev->irq) {
646 		inb_p(base+7);
647 		inb_p(base+7);
648 	}
649 	return;
650 }
651 
652 
do_write(struct net_device * dev,void * cbuf,int cbuflen,void * dbuf,int dbuflen)653 static int do_write(struct net_device *dev, void *cbuf, int cbuflen,
654 	void *dbuf, int dbuflen)
655 {
656 
657 	int i = getmbox();
658 	int ret;
659 
660 	if(i) {
661 		qels[i].cbuf = (unsigned char *) cbuf;
662 		qels[i].cbuflen = cbuflen;
663 		qels[i].dbuf = (unsigned char *) dbuf;
664 		qels[i].dbuflen = dbuflen;
665 		qels[i].QWrite = 1;
666 		qels[i].mailbox = i;  /* this should be initted rather */
667 		enQ(&qels[i]);
668 		idle(dev);
669 		ret = mailbox[i];
670 		mboxinuse[i]=0;
671 		return ret;
672 	}
673 	printk("ltpc: could not allocate mbox\n");
674 	return -1;
675 }
676 
do_read(struct net_device * dev,void * cbuf,int cbuflen,void * dbuf,int dbuflen)677 static int do_read(struct net_device *dev, void *cbuf, int cbuflen,
678 	void *dbuf, int dbuflen)
679 {
680 
681 	int i = getmbox();
682 	int ret;
683 
684 	if(i) {
685 		qels[i].cbuf = (unsigned char *) cbuf;
686 		qels[i].cbuflen = cbuflen;
687 		qels[i].dbuf = (unsigned char *) dbuf;
688 		qels[i].dbuflen = dbuflen;
689 		qels[i].QWrite = 0;
690 		qels[i].mailbox = i;  /* this should be initted rather */
691 		enQ(&qels[i]);
692 		idle(dev);
693 		ret = mailbox[i];
694 		mboxinuse[i]=0;
695 		return ret;
696 	}
697 	printk("ltpc: could not allocate mbox\n");
698 	return -1;
699 }
700 
701 /* end of idle handlers -- what should be seen is do_read, do_write */
702 
703 static struct timer_list ltpc_timer;
704 
705 static int ltpc_xmit(struct sk_buff *skb, struct net_device *dev);
706 static struct net_device_stats *ltpc_get_stats(struct net_device *dev);
707 
read_30(struct net_device * dev)708 static int read_30 ( struct net_device *dev)
709 {
710 	lt_command c;
711 	c.getflags.command = LT_GETFLAGS;
712 	return do_read(dev, &c, sizeof(c.getflags),&c,0);
713 }
714 
set_30(struct net_device * dev,int x)715 static int set_30 (struct net_device *dev,int x)
716 {
717 	lt_command c;
718 	c.setflags.command = LT_SETFLAGS;
719 	c.setflags.flags = x;
720 	return do_write(dev, &c, sizeof(c.setflags),&c,0);
721 }
722 
723 /* LLAP to DDP translation */
724 
sendup_buffer(struct net_device * dev)725 static int sendup_buffer (struct net_device *dev)
726 {
727 	/* on entry, command is in ltdmacbuf, data in ltdmabuf */
728 	/* called from idle, non-reentrant */
729 
730 	int dnode, snode, llaptype, len;
731 	int sklen;
732 	struct sk_buff *skb;
733 	struct net_device_stats *stats = &((struct ltpc_private *)dev->priv)->stats;
734 	struct lt_rcvlap *ltc = (struct lt_rcvlap *) ltdmacbuf;
735 
736 	if (ltc->command != LT_RCVLAP) {
737 		printk("unknown command 0x%02x from ltpc card\n",ltc->command);
738 		return(-1);
739 	}
740 	dnode = ltc->dnode;
741 	snode = ltc->snode;
742 	llaptype = ltc->laptype;
743 	len = ltc->length;
744 
745 	sklen = len;
746 	if (llaptype == 1)
747 		sklen += 8;  /* correct for short ddp */
748 	if(sklen > 800) {
749 		printk(KERN_INFO "%s: nonsense length in ltpc command 0x14: 0x%08x\n",
750 			dev->name,sklen);
751 		return -1;
752 	}
753 
754 	if ( (llaptype==0) || (llaptype>2) ) {
755 		printk(KERN_INFO "%s: unknown LLAP type: %d\n",dev->name,llaptype);
756 		return -1;
757 	}
758 
759 
760 	skb = dev_alloc_skb(3+sklen);
761 	if (skb == NULL)
762 	{
763 		printk("%s: dropping packet due to memory squeeze.\n",
764 			dev->name);
765 		return -1;
766 	}
767 	skb->dev = dev;
768 
769 	if (sklen > len)
770 		skb_reserve(skb,8);
771 	skb_put(skb,len+3);
772 	skb->protocol = htons(ETH_P_LOCALTALK);
773 	/* add LLAP header */
774 	skb->data[0] = dnode;
775 	skb->data[1] = snode;
776 	skb->data[2] = llaptype;
777 	skb->mac.raw = skb->data;	/* save pointer to llap header */
778 	skb_pull(skb,3);
779 
780 	/* copy ddp(s,e)hdr + contents */
781 	memcpy(skb->data,(void*)ltdmabuf,len);
782 
783 	skb->h.raw = skb->data;
784 
785 	stats->rx_packets++;
786 	stats->rx_bytes+=skb->len;
787 
788 	/* toss it onwards */
789 	netif_rx(skb);
790 	dev->last_rx = jiffies;
791 	return 0;
792 }
793 
794 /* the handler for the board interrupt */
795 
ltpc_interrupt(int irq,void * dev_id,struct pt_regs * reg_ptr)796 static void ltpc_interrupt(int irq, void *dev_id, struct pt_regs *reg_ptr)
797 {
798 	struct net_device *dev = dev_id;
799 
800 	if (dev==NULL) {
801 		printk("ltpc_interrupt: unknown device.\n");
802 		return;
803 	}
804 
805 	inb_p(dev->base_addr+6);  /* disable further interrupts from board */
806 
807 	idle(dev); /* handle whatever is coming in */
808 
809 	/* idle re-enables interrupts from board */
810 
811 	return;
812 }
813 
814 /***
815  *
816  *    The ioctls that the driver responds to are:
817  *
818  *    SIOCSIFADDR -- do probe using the passed node hint.
819  *    SIOCGIFADDR -- return net, node.
820  *
821  *    some of this stuff should be done elsewhere.
822  *
823  ***/
824 
ltpc_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)825 static int ltpc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
826 {
827 	struct sockaddr_at *sa = (struct sockaddr_at *) &ifr->ifr_addr;
828 	/* we'll keep the localtalk node address in dev->pa_addr */
829 	struct at_addr *aa = &((struct ltpc_private *)dev->priv)->my_addr;
830 	struct lt_init c;
831 	int ltflags;
832 
833 	if(debug&DEBUG_VERBOSE) printk("ltpc_ioctl called\n");
834 
835 	switch(cmd) {
836 		case SIOCSIFADDR:
837 
838 			aa->s_net  = sa->sat_addr.s_net;
839 
840 			/* this does the probe and returns the node addr */
841 			c.command = LT_INIT;
842 			c.hint = sa->sat_addr.s_node;
843 
844 			aa->s_node = do_read(dev,&c,sizeof(c),&c,0);
845 
846 			/* get all llap frames raw */
847 			ltflags = read_30(dev);
848 			ltflags |= LT_FLAG_ALLLAP;
849 			set_30 (dev,ltflags);
850 
851 			dev->broadcast[0] = 0xFF;
852 			dev->dev_addr[0] = aa->s_node;
853 
854 			dev->addr_len=1;
855 
856 			return 0;
857 
858 		case SIOCGIFADDR:
859 
860 			sa->sat_addr.s_net = aa->s_net;
861 			sa->sat_addr.s_node = aa->s_node;
862 
863 			return 0;
864 
865 		default:
866 			return -EINVAL;
867 	}
868 }
869 
set_multicast_list(struct net_device * dev)870 static void set_multicast_list(struct net_device *dev)
871 {
872 	/* This needs to be present to keep netatalk happy. */
873 	/* Actually netatalk needs fixing! */
874 }
875 
ltpc_hard_header(struct sk_buff * skb,struct net_device * dev,unsigned short type,void * daddr,void * saddr,unsigned len)876 static int ltpc_hard_header (struct sk_buff *skb, struct net_device *dev,
877 	unsigned short type, void *daddr, void *saddr, unsigned len)
878 {
879 	if(debug&DEBUG_VERBOSE)
880 		printk("ltpc_hard_header called for device %s\n",
881 			dev->name);
882 	return 0;
883 }
884 
ltpc_init(struct net_device * dev)885 static int ltpc_init(struct net_device *dev)
886 {
887 	/* Initialize the device structure. */
888 
889 	/* Fill in the fields of the device structure with ethernet-generic values. */
890 	ltalk_setup(dev);
891 	dev->hard_start_xmit = ltpc_xmit;
892 	dev->hard_header = ltpc_hard_header;
893 
894 	dev->priv = kmalloc(sizeof(struct ltpc_private), GFP_KERNEL);
895 	if(!dev->priv)
896 	{
897 		printk(KERN_INFO "%s: could not allocate statistics buffer\n", dev->name);
898 		return -ENOMEM;
899 	}
900 
901 	memset(dev->priv, 0, sizeof(struct ltpc_private));
902 	dev->get_stats = ltpc_get_stats;
903 
904 	/* add the ltpc-specific things */
905 	dev->do_ioctl = &ltpc_ioctl;
906 
907 	dev->set_multicast_list = &set_multicast_list;
908 	dev->mc_list = NULL;
909 
910 	return 0;
911 }
912 
913 static int ltpc_poll_counter;
914 
ltpc_poll(unsigned long l)915 static void ltpc_poll(unsigned long l)
916 {
917 	struct net_device *dev = (struct net_device *) l;
918 
919 	del_timer(&ltpc_timer);
920 
921 	if(debug&DEBUG_VERBOSE) {
922 		if (!ltpc_poll_counter) {
923 			ltpc_poll_counter = 50;
924 			printk("ltpc poll is alive\n");
925 		}
926 		ltpc_poll_counter--;
927 	}
928 
929 	if (!dev)
930 		return;  /* we've been downed */
931 
932 	idle(dev);
933 	ltpc_timer.expires = jiffies+5;
934 
935 	add_timer(&ltpc_timer);
936 }
937 
938 /* DDP to LLAP translation */
939 
ltpc_xmit(struct sk_buff * skb,struct net_device * dev)940 static int ltpc_xmit(struct sk_buff *skb, struct net_device *dev)
941 {
942 	/* in kernel 1.3.xx, on entry skb->data points to ddp header,
943 	 * and skb->len is the length of the ddp data + ddp header
944 	 */
945 
946 	struct net_device_stats *stats = &((struct ltpc_private *)dev->priv)->stats;
947 
948 	int i;
949 	struct lt_sendlap cbuf;
950 
951 	cbuf.command = LT_SENDLAP;
952 	cbuf.dnode = skb->data[0];
953 	cbuf.laptype = skb->data[2];
954 	skb_pull(skb,3);	/* skip past LLAP header */
955 	cbuf.length = skb->len;	/* this is host order */
956 	skb->h.raw=skb->data;
957 
958 	if(debug&DEBUG_UPPER) {
959 		printk("command ");
960 		for(i=0;i<6;i++)
961 			printk("%02x ",((unsigned char *)&cbuf)[i]);
962 		printk("\n");
963 	}
964 
965 	do_write(dev,&cbuf,sizeof(cbuf),skb->h.raw,skb->len);
966 
967 	if(debug&DEBUG_UPPER) {
968 		printk("sent %d ddp bytes\n",skb->len);
969 		for(i=0;i<skb->len;i++) printk("%02x ",skb->h.raw[i]);
970 		printk("\n");
971 	}
972 
973 	stats->tx_packets++;
974 	stats->tx_bytes+=skb->len;
975 
976 	dev_kfree_skb(skb);
977 	return 0;
978 }
979 
ltpc_get_stats(struct net_device * dev)980 static struct net_device_stats *ltpc_get_stats(struct net_device *dev)
981 {
982 	struct net_device_stats *stats = &((struct ltpc_private *) dev->priv)->stats;
983 	return stats;
984 }
985 
986 /* initialization stuff */
987 
ltpc_probe_dma(int base)988 static int __init ltpc_probe_dma(int base)
989 {
990 	int dma = 0;
991   	int timeout;
992   	unsigned long f;
993 
994   	if (!request_dma(1,"ltpc")) {
995   		f=claim_dma_lock();
996   		disable_dma(1);
997   		clear_dma_ff(1);
998 		set_dma_mode(1,DMA_MODE_WRITE);
999 		set_dma_addr(1,virt_to_bus(ltdmabuf));
1000 		set_dma_count(1,sizeof(struct lt_mem));
1001 		enable_dma(1);
1002 		release_dma_lock(f);
1003 		dma|=1;
1004 	}
1005 	if (!request_dma(3,"ltpc")) {
1006 		f=claim_dma_lock();
1007 		disable_dma(3);
1008 		clear_dma_ff(3);
1009 		set_dma_mode(3,DMA_MODE_WRITE);
1010 		set_dma_addr(3,virt_to_bus(ltdmabuf));
1011 		set_dma_count(3,sizeof(struct lt_mem));
1012 		enable_dma(3);
1013 		release_dma_lock(f);
1014 		dma|=2;
1015 	}
1016 
1017 	/* set up request */
1018 
1019 	/* FIXME -- do timings better! */
1020 
1021 	ltdmabuf[0] = LT_READMEM;
1022 	ltdmabuf[1] = 1;  /* mailbox */
1023 	ltdmabuf[2] = 0; ltdmabuf[3] = 0;  /* address */
1024 	ltdmabuf[4] = 0; ltdmabuf[5] = 1;  /* read 0x0100 bytes */
1025 	ltdmabuf[6] = 0; /* dunno if this is necessary */
1026 
1027 	inb_p(io+1);
1028 	inb_p(io+0);
1029 	timeout = jiffies+100*HZ/100;
1030 	while(time_before(jiffies, timeout)) {
1031 		if ( 0xfa == inb_p(io+6) ) break;
1032 	}
1033 
1034 	inb_p(io+3);
1035 	inb_p(io+2);
1036 	while(time_before(jiffies, timeout)) {
1037 		if ( 0xfb == inb_p(io+6) ) break;
1038 	}
1039 
1040 	/* release the other dma channel (if we opened both of them) */
1041 
1042 	if ( (dma&0x2) && (get_dma_residue(3)==sizeof(struct lt_mem)) ){
1043 		dma&=1;
1044 		free_dma(3);
1045 	}
1046 
1047 	if ( (dma&0x1) && (get_dma_residue(1)==sizeof(struct lt_mem)) ){
1048 		dma&=0x2;
1049 		free_dma(1);
1050 	}
1051 
1052 	/* fix up dma number */
1053 	dma|=1;
1054 
1055 	return dma;
1056 }
1057 
ltpc_probe(struct net_device * dev)1058 int __init ltpc_probe(struct net_device *dev)
1059 {
1060 	int err;
1061 	int x=0,y=0;
1062 	int timeout;
1063 	int autoirq;
1064 	unsigned long flags;
1065 	unsigned long f;
1066 
1067 	SET_MODULE_OWNER(dev);
1068 
1069 	save_flags(flags);
1070 
1071 	/* probe for the I/O port address */
1072 	if (io != 0x240 && !check_region(0x220,8)) {
1073 		x = inb_p(0x220+6);
1074 		if ( (x!=0xff) && (x>=0xf0) ) io = 0x220;
1075 	}
1076 
1077 	if (io != 0x220 && !check_region(0x240,8)) {
1078 		y = inb_p(0x240+6);
1079 		if ( (y!=0xff) && (y>=0xf0) ) io = 0x240;
1080 	}
1081 
1082 	if(io) {
1083 		/* found it, now grab it */
1084 		request_region(io,8,"ltpc");
1085 	} else {
1086 		/* give up in despair */
1087 		printk ("LocalTalk card not found; 220 = %02x, 240 = %02x.\n",
1088 			x,y);
1089 		restore_flags(flags);
1090 		return -1;
1091 	}
1092 
1093 	/* probe for the IRQ line */
1094 	if (irq < 2) {
1095 		autoirq_setup(2);
1096 
1097 		/* reset the interrupt line */
1098 		inb_p(io+7);
1099 		inb_p(io+7);
1100 		/* trigger an interrupt (I hope) */
1101 		inb_p(io+6);
1102 
1103 		autoirq = autoirq_report(1);
1104 
1105 		if (autoirq == 0) {
1106 			printk("ltpc: probe at %#x failed to detect IRQ line.\n",
1107 				io);
1108 		}
1109 		else {
1110 			irq = autoirq;
1111 		}
1112 	}
1113 
1114 	/* allocate a DMA buffer */
1115 	ltdmabuf = (unsigned char *) dma_mem_alloc(1000);
1116 
1117 	if (ltdmabuf) ltdmacbuf = &ltdmabuf[800];
1118 
1119 	if (!ltdmabuf) {
1120 		printk("ltpc: mem alloc failed\n");
1121 		restore_flags(flags);
1122 		return(-1);
1123 	}
1124 
1125 	if(debug&DEBUG_VERBOSE) {
1126 		printk("ltdmabuf pointer %08lx\n",(unsigned long) ltdmabuf);
1127 	}
1128 
1129 	/* reset the card */
1130 
1131 	inb_p(io+1);
1132 	inb_p(io+3);
1133 	timeout = jiffies+2*HZ/100;
1134 	while(time_before(jiffies, timeout)) ; /* hold it in reset for a coupla jiffies */
1135 	inb_p(io+0);
1136 	inb_p(io+2);
1137 	inb_p(io+7); /* clear reset */
1138 	inb_p(io+4);
1139 	inb_p(io+5);
1140 	inb_p(io+5); /* enable dma */
1141 	inb_p(io+6); /* tri-state interrupt line */
1142 
1143 	timeout = jiffies+100*HZ/100;
1144 
1145 	while(time_before(jiffies, timeout)) {
1146 		/* wait for the card to complete initialization */
1147 	}
1148 
1149 	/* now, figure out which dma channel we're using, unless it's
1150 	   already been specified */
1151 	/* well, 0 is a legal DMA channel, but the LTPC card doesn't
1152 	   use it... */
1153 	if (dma == 0) {
1154 		dma = ltpc_probe_dma(io);
1155 		if (!dma) {  /* no dma channel */
1156 			printk("No DMA channel found on ltpc card.\n");
1157 			restore_flags(flags);
1158 			return -1;
1159 		}
1160 	}
1161 
1162 	/* print out friendly message */
1163 
1164 	if(irq)
1165 		printk("Apple/Farallon LocalTalk-PC card at %03x, IR%d, DMA%d.\n",io,irq,dma);
1166 	else
1167 		printk("Apple/Farallon LocalTalk-PC card at %03x, DMA%d.  Using polled mode.\n",io,dma);
1168 
1169 	/* seems more logical to do this *after* probing the card... */
1170 	err = ltpc_init(dev);
1171 	if (err) return err;
1172 
1173 	dev->base_addr = io;
1174 	dev->irq = irq;
1175 	dev->dma = dma;
1176 
1177 	/* the card will want to send a result at this point */
1178 	/* (I think... leaving out this part makes the kernel crash,
1179            so I put it back in...) */
1180 
1181 	f=claim_dma_lock();
1182 	disable_dma(dma);
1183 	clear_dma_ff(dma);
1184 	set_dma_mode(dma,DMA_MODE_READ);
1185 	set_dma_addr(dma,virt_to_bus(ltdmabuf));
1186 	set_dma_count(dma,0x100);
1187 	enable_dma(dma);
1188 	release_dma_lock(f);
1189 
1190 	(void) inb_p(io+3);
1191 	(void) inb_p(io+2);
1192 	timeout = jiffies+100*HZ/100;
1193 	while(time_before(jiffies, timeout)) {
1194 		if( 0xf9 == inb_p(io+6)) break;
1195 	}
1196 
1197 	if(debug&DEBUG_VERBOSE) {
1198 		printk("setting up timer and irq\n");
1199 	}
1200 
1201 	if (irq) {
1202 		/* grab it and don't let go :-) */
1203 		(void) request_irq( irq, &ltpc_interrupt, 0, "ltpc", dev);
1204 		(void) inb_p(io+7);  /* enable interrupts from board */
1205 		(void) inb_p(io+7);  /* and reset irq line */
1206 	} else {
1207 		/* polled mode -- 20 times per second */
1208 		/* this is really, really slow... should it poll more often? */
1209 		init_timer(&ltpc_timer);
1210 		ltpc_timer.function=ltpc_poll;
1211 		ltpc_timer.data = (unsigned long) dev;
1212 
1213 		ltpc_timer.expires = jiffies + 5;
1214 		add_timer(&ltpc_timer);
1215 		restore_flags(flags);
1216 	}
1217 
1218 	return 0;
1219 }
1220 
1221 #ifndef MODULE
1222 /* handles "ltpc=io,irq,dma" kernel command lines */
ltpc_setup(char * str)1223 static int __init ltpc_setup(char *str)
1224 {
1225 	int ints[5];
1226 
1227 	str = get_options(str, ARRAY_SIZE(ints), ints);
1228 
1229 	if (ints[0] == 0) {
1230 		if (str && !strncmp(str, "auto", 4)) {
1231 			/* do nothing :-) */
1232 		}
1233 		else {
1234 			/* usage message */
1235 			printk (KERN_ERR
1236 				"ltpc: usage: ltpc=auto|iobase[,irq[,dma]]\n");
1237 			return 0;
1238 		}
1239 	} else {
1240 		io = ints[1];
1241 		if (ints[0] > 1) {
1242 			irq = ints[2];
1243 		}
1244 		if (ints[0] > 2) {
1245 			dma = ints[3];
1246 		}
1247 		/* ignore any other paramters */
1248 	}
1249 	return 1;
1250 }
1251 
1252 __setup("ltpc=", ltpc_setup);
1253 #endif /* MODULE */
1254 
1255 static struct net_device dev_ltpc;
1256 
1257 #ifdef MODULE
1258 
1259 MODULE_LICENSE("GPL");
1260 MODULE_PARM(debug, "i");
1261 MODULE_PARM(io, "i");
1262 MODULE_PARM(irq, "i");
1263 MODULE_PARM(dma, "i");
1264 
1265 
init_module(void)1266 int __init init_module(void)
1267 {
1268 	int err, result;
1269 
1270         if(io == 0)
1271 		printk(KERN_NOTICE
1272 		       "ltpc: Autoprobing is not recommended for modules\n");
1273 
1274 	/* Find a name for this unit */
1275 	dev_ltpc.init = ltpc_probe;
1276 	err=dev_alloc_name(&dev_ltpc,"lt%d");
1277 
1278 	if(err<0)
1279 		return err;
1280 
1281 	if ((result = register_netdev(&dev_ltpc)) != 0) {
1282 		printk(KERN_DEBUG "could not register Localtalk-PC device\n");
1283 		return result;
1284 	} else {
1285 		if(debug&DEBUG_VERBOSE) printk("0 from register_netdev\n");
1286 		return 0;
1287 	}
1288 }
1289 #endif
1290 
ltpc_cleanup(void)1291 static void __exit ltpc_cleanup(void)
1292 {
1293 	long timeout;
1294 
1295 	ltpc_timer.data = 0;  /* signal the poll routine that we're done */
1296 
1297 	if(debug&DEBUG_VERBOSE) printk("freeing irq\n");
1298 
1299 	if(dev_ltpc.irq) {
1300 		free_irq(dev_ltpc.irq,&dev_ltpc);
1301 		dev_ltpc.irq = 0;
1302 	}
1303 
1304 	if(del_timer(&ltpc_timer))
1305 	{
1306 		/* either the poll was never started, or a poll is in process */
1307 		if(debug&DEBUG_VERBOSE) printk("waiting\n");
1308 		/* if it's in process, wait a bit for it to finish */
1309 		timeout = jiffies+HZ;
1310 		add_timer(&ltpc_timer);
1311 		while(del_timer(&ltpc_timer) && time_after(timeout, jiffies))
1312 		{
1313 			add_timer(&ltpc_timer);
1314 			schedule();
1315 		}
1316 	}
1317 
1318 	if(debug&DEBUG_VERBOSE) printk("freeing dma\n");
1319 
1320 	if(dev_ltpc.dma) {
1321 		free_dma(dev_ltpc.dma);
1322 		dev_ltpc.dma = 0;
1323 	}
1324 
1325 	if(debug&DEBUG_VERBOSE) printk("freeing ioaddr\n");
1326 
1327 	if(dev_ltpc.base_addr) {
1328 		release_region(dev_ltpc.base_addr,8);
1329 		dev_ltpc.base_addr = 0;
1330 	}
1331 
1332 	if(debug&DEBUG_VERBOSE) printk("free_pages\n");
1333 
1334 	free_pages( (unsigned long) ltdmabuf, get_order(1000));
1335 	ltdmabuf=NULL;
1336 	ltdmacbuf=NULL;
1337 
1338 	if(debug&DEBUG_VERBOSE) printk("unregister_netdev\n");
1339 
1340 	unregister_netdev(&dev_ltpc);
1341 
1342 	if(debug&DEBUG_VERBOSE) printk("returning from cleanup_module\n");
1343 }
1344 
1345 module_exit(ltpc_cleanup);
1346