Import 2.3.18pre1
[davej-history.git] / drivers / net / ltpc.c
blob53fe534644959549ab08a7831a0210ed27e1a0c1
1 /*** ltpc.c -- a driver for the LocalTalk PC card.
3 * Copyright (c) 1995,1996 Bradford W. Johnson <johns393@maroon.tc.umn.edu>
5 * This software may be used and distributed according to the terms
6 * of the GNU General Public License, incorporated herein by reference.
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!
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...
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!
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)
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...
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.
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...
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.
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.
60 ***/
62 /***
64 * $Log: ltpc.c,v $
65 * Revision 1.8 1997/01/28 05:44:54 bradford
66 * Clean up for non-module a little.
67 * Hacked about a bit to clean things up - Alan Cox
68 * Probably broken it from the origina 1.8
71 * 1998/11/09: David Huggins-Daines <dhd@debian.org>
72 * Cleaned up the initialization code to use the standard autoirq methods,
73 and to probe for things in the standard order of i/o, irq, dma. This
74 removes the "reset the reset" hack, because I couldn't figure out an
75 easy way to get the card to trigger an interrupt after it.
76 * Added support for passing configuration parameters on the kernel command
77 line and through insmod
78 * Changed the device name from "ltalk0" to "lt0", both to conform with the
79 other localtalk driver, and to clear up the inconsistency between the
80 module and the non-module versions of the driver :-)
81 * Added a bunch of comments (I was going to make some enums for the state
82 codes and the register offsets, but I'm still not sure exactly what their
83 semantics are)
84 * Don't poll anymore in interrupt-driven mode
85 * It seems to work as a module now (as of 2.1.127), but I don't think
86 I'm responsible for that...
89 * Revision 1.7 1996/12/12 03:42:33 bradford
90 * DMA alloc cribbed from 3c505.c.
92 * Revision 1.6 1996/12/12 03:18:58 bradford
93 * Added virt_to_bus; works in 2.1.13.
95 * Revision 1.5 1996/12/12 03:13:22 root
96 * xmitQel initialization -- think through better though.
98 * Revision 1.4 1996/06/18 14:55:55 root
99 * Change names to ltpc. Tabs. Took a shot at dma alloc,
100 * although more needs to be done eventually.
102 * Revision 1.3 1996/05/22 14:59:39 root
103 * Change dev->open, dev->close to track dummy.c in 1.99.(around 7)
105 * Revision 1.2 1996/05/22 14:58:24 root
106 * Change tabs mostly.
108 * Revision 1.1 1996/04/23 04:45:09 root
109 * Initial revision
111 * Revision 0.16 1996/03/05 15:59:56 root
112 * Change ARPHRD_LOCALTLK definition to the "real" one.
114 * Revision 0.15 1996/03/05 06:28:30 root
115 * Changes for kernel 1.3.70. Still need a few patches to kernel, but
116 * it's getting closer.
118 * Revision 0.14 1996/02/25 17:38:32 root
119 * More cleanups. Removed query to card on get_stats.
121 * Revision 0.13 1996/02/21 16:27:40 root
122 * Refix debug_print_skb. Fix mac.raw gotcha that appeared in 1.3.65.
123 * Clean up receive code a little.
125 * Revision 0.12 1996/02/19 16:34:53 root
126 * Fix debug_print_skb. Kludge outgoing snet to 0 when using startup
127 * range. Change debug to mask: 1 for verbose, 2 for higher level stuff
128 * including packet printing, 4 for lower level (card i/o) stuff.
130 * Revision 0.11 1996/02/12 15:53:38 root
131 * Added router sends (requires new aarp.c patch)
133 * Revision 0.10 1996/02/11 00:19:35 root
134 * Change source LTALK_LOGGING debug switch to insmod ... debug=2.
136 * Revision 0.9 1996/02/10 23:59:35 root
137 * Fixed those fixes for 1.2 -- DANGER! The at.h that comes with netatalk
138 * has a *different* definition of struct sockaddr_at than the Linux kernel
139 * does. This is an "insidious and invidious" bug...
140 * (Actually the preceding comment is false -- it's the atalk.h in the
141 * ancient atalk-0.06 that's the problem)
143 * Revision 0.8 1996/02/10 19:09:00 root
144 * Merge 1.3 changes. Tested OK under 1.3.60.
146 * Revision 0.7 1996/02/10 17:56:56 root
147 * Added debug=1 parameter on insmod for debugging prints. Tried
148 * to fix timer unload on rmmod, but I don't think that's the problem.
150 * Revision 0.6 1995/12/31 19:01:09 root
151 * Clean up rmmod, irq comments per feedback from Corin Anderson (Thanks Corey!)
152 * Clean up initial probing -- sometimes the card wakes up latched in reset.
154 * Revision 0.5 1995/12/22 06:03:44 root
155 * Added comments in front and cleaned up a bit.
156 * This version sent out to people.
158 * Revision 0.4 1995/12/18 03:46:44 root
159 * Return shortDDP to longDDP fake to 0/0. Added command structs.
161 ***/
163 /* ltpc jumpers are:
165 * Interrupts -- set at most one. If none are set, the driver uses
166 * polled mode. Because the card was developed in the XT era, the
167 * original documentation refers to IRQ2. Since you'll be running
168 * this on an AT (or later) class machine, that really means IRQ9.
170 * SW1 IRQ 4
171 * SW2 IRQ 3
172 * SW3 IRQ 9 (2 in original card documentation only applies to XT)
175 * DMA -- choose DMA 1 or 3, and set both corresponding switches.
177 * SW4 DMA 3
178 * SW5 DMA 1
179 * SW6 DMA 3
180 * SW7 DMA 1
183 * I/O address -- choose one.
185 * SW8 220 / 240
188 /* To have some stuff logged, do
189 * insmod ltpc.o debug=1
191 * For a whole bunch of stuff, use higher numbers.
193 * The default is 0, i.e. no messages except for the probe results.
196 /* insmod-tweakable variables */
197 static int debug=0;
198 #define DEBUG_VERBOSE 1
199 #define DEBUG_UPPER 2
200 #define DEBUG_LOWER 4
202 static int io=0;
203 static int irq=0;
204 static int dma=0;
206 #ifdef MODULE
207 #include <linux/module.h>
208 #include <linux/version.h>
209 #endif
211 #include <linux/kernel.h>
212 #include <linux/sched.h>
213 #include <linux/types.h>
214 #include <linux/fcntl.h>
215 #include <linux/interrupt.h>
216 #include <linux/ptrace.h>
217 #include <linux/ioport.h>
218 #include <linux/in.h>
219 #include <linux/malloc.h>
220 #include <linux/string.h>
221 #include <asm/system.h>
222 #include <asm/bitops.h>
223 #include <asm/dma.h>
224 #include <linux/errno.h>
225 #include <linux/init.h>
227 #include <linux/netdevice.h>
228 #include <linux/etherdevice.h>
229 #include <linux/skbuff.h>
231 #include <linux/if_arp.h>
232 #include <linux/if_ltalk.h>
234 #include <linux/delay.h>
235 #include <linux/timer.h>
237 #include <linux/atalk.h>
239 /* our stuff */
240 #include "ltpc.h"
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);
247 /* Dma Memory related stuff, cribbed directly from 3c505.c */
249 /* Pure 2^n version of get_order */
250 static inline int __get_order(unsigned long size)
252 int order;
254 size = (size - 1) >> (PAGE_SHIFT - 1);
255 order = -1;
256 do {
257 size >>= 1;
258 order++;
259 } while (size);
260 return order;
263 static unsigned long dma_mem_alloc(int size)
265 int order = __get_order(size);
267 return __get_dma_pages(GFP_KERNEL, order);
270 /* DMA data buffer, DMA command buffer */
271 static unsigned char *ltdmabuf;
272 static unsigned char *ltdmacbuf;
274 /* private struct, holds our appletalk address */
276 struct ltpc_private
278 struct net_device_stats stats;
279 struct at_addr my_addr;
282 /* transmit queue element struct */
284 struct xmitQel {
285 struct xmitQel *next;
286 /* command buffer */
287 unsigned char *cbuf;
288 short cbuflen;
289 /* data buffer */
290 unsigned char *dbuf;
291 short dbuflen;
292 unsigned char QWrite; /* read or write data */
293 unsigned char mailbox;
296 /* the transmit queue itself */
298 static struct xmitQel *xmQhd=NULL,*xmQtl=NULL;
300 static void enQ(struct xmitQel *qel)
302 unsigned long flags;
303 qel->next = NULL;
304 save_flags(flags);
305 cli();
306 if (xmQtl) {
307 xmQtl->next = qel;
308 } else {
309 xmQhd = qel;
311 xmQtl = qel;
312 restore_flags(flags);
314 if (debug&DEBUG_LOWER)
315 printk("enqueued a 0x%02x command\n",qel->cbuf[0]);
318 static struct xmitQel *deQ(void)
320 unsigned long flags;
321 int i;
322 struct xmitQel *qel=NULL;
323 save_flags(flags);
324 cli();
325 if (xmQhd) {
326 qel = xmQhd;
327 xmQhd = qel->next;
328 if(!xmQhd) xmQtl = NULL;
330 restore_flags(flags);
332 if ((debug&DEBUG_LOWER) && qel) {
333 int n;
334 printk("ltpc: dequeued command ");
335 n = qel->cbuflen;
336 if (n>100) n=100;
337 for(i=0;i<n;i++) printk("%02x ",qel->cbuf[i]);
338 printk("\n");
341 return qel;
344 /* and... the queue elements we'll be using */
345 static struct xmitQel qels[16];
347 /* and their corresponding mailboxes */
348 static unsigned char mailbox[16];
349 static unsigned char mboxinuse[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
351 static int wait_timeout(struct net_device *dev, int c)
353 /* returns true if it stayed c */
354 /* this uses base+6, but it's ok */
355 int i;
356 int timeout;
358 /* twenty second or so total */
360 for(i=0;i<20000;i++) {
361 if ( c != inb_p(dev->base_addr+6) ) return 0;
362 for(timeout=loops_per_sec/1000; timeout > 0; timeout--) ;
364 return 1; /* timed out */
367 /* get the first free mailbox */
369 static int getmbox(void)
371 unsigned long flags;
372 int i;
374 save_flags(flags);
375 cli();
376 for(i=1;i<16;i++) if(!mboxinuse[i]) {
377 mboxinuse[i]=1;
378 restore_flags(flags);
379 return i;
381 restore_flags(flags);
382 return 0;
385 /* read a command from the card */
386 static void handlefc(struct net_device *dev)
388 /* called *only* from idle, non-reentrant */
389 int dma = dev->dma;
390 int base = dev->base_addr;
391 unsigned long flags;
394 flags=claim_dma_lock();
395 disable_dma(dma);
396 clear_dma_ff(dma);
397 set_dma_mode(dma,DMA_MODE_READ);
398 set_dma_addr(dma,virt_to_bus(ltdmacbuf));
399 set_dma_count(dma,50);
400 enable_dma(dma);
401 release_dma_lock(flags);
403 inb_p(base+3);
404 inb_p(base+2);
406 if ( wait_timeout(dev,0xfc) ) printk("timed out in handlefc\n");
409 /* read data from the card */
410 static void handlefd(struct net_device *dev)
412 int dma = dev->dma;
413 int base = dev->base_addr;
414 unsigned long flags;
416 flags=claim_dma_lock();
417 disable_dma(dma);
418 clear_dma_ff(dma);
419 set_dma_mode(dma,DMA_MODE_READ);
420 set_dma_addr(dma,virt_to_bus(ltdmabuf));
421 set_dma_count(dma,800);
422 enable_dma(dma);
423 release_dma_lock(flags);
425 inb_p(base+3);
426 inb_p(base+2);
428 if ( wait_timeout(dev,0xfd) ) printk("timed out in handlefd\n");
429 sendup_buffer(dev);
432 static void handlewrite(struct net_device *dev)
434 /* called *only* from idle, non-reentrant */
435 /* on entry, 0xfb and ltdmabuf holds data */
436 int dma = dev->dma;
437 int base = dev->base_addr;
438 unsigned long flags;
440 flags=claim_dma_lock();
441 disable_dma(dma);
442 clear_dma_ff(dma);
443 set_dma_mode(dma,DMA_MODE_WRITE);
444 set_dma_addr(dma,virt_to_bus(ltdmabuf));
445 set_dma_count(dma,800);
446 enable_dma(dma);
447 release_dma_lock(flags);
449 inb_p(base+3);
450 inb_p(base+2);
452 if ( wait_timeout(dev,0xfb) ) {
453 flags=claim_dma_lock();
454 printk("timed out in handlewrite, dma res %d\n",
455 get_dma_residue(dev->dma) );
456 release_dma_lock(flags);
460 static void handleread(struct net_device *dev)
462 /* on entry, 0xfb */
463 /* on exit, ltdmabuf holds data */
464 int dma = dev->dma;
465 int base = dev->base_addr;
466 unsigned long flags;
469 flags=claim_dma_lock();
470 disable_dma(dma);
471 clear_dma_ff(dma);
472 set_dma_mode(dma,DMA_MODE_READ);
473 set_dma_addr(dma,virt_to_bus(ltdmabuf));
474 set_dma_count(dma,800);
475 enable_dma(dma);
476 release_dma_lock(flags);
478 inb_p(base+3);
479 inb_p(base+2);
480 if ( wait_timeout(dev,0xfb) ) printk("timed out in handleread\n");
483 static void handlecommand(struct net_device *dev)
485 /* on entry, 0xfa and ltdmacbuf holds command */
486 int dma = dev->dma;
487 int base = dev->base_addr;
488 unsigned long flags;
490 flags=claim_dma_lock();
491 disable_dma(dma);
492 clear_dma_ff(dma);
493 set_dma_mode(dma,DMA_MODE_WRITE);
494 set_dma_addr(dma,virt_to_bus(ltdmacbuf));
495 set_dma_count(dma,50);
496 enable_dma(dma);
497 release_dma_lock(flags);
498 inb_p(base+3);
499 inb_p(base+2);
500 if ( wait_timeout(dev,0xfa) ) printk("timed out in handlecommand\n");
503 /* ready made command for getting the result from the card */
504 static unsigned char rescbuf[2] = {LT_GETRESULT,0};
505 static unsigned char resdbuf[2];
507 static int QInIdle=0;
509 /* idle expects to be called with the IRQ line high -- either because of
510 * an interrupt, or because the line is tri-stated
513 static void idle(struct net_device *dev)
515 unsigned long flags;
516 int state;
517 /* FIXME This is initialized to shut the warning up, but I need to
518 * think this through again.
520 struct xmitQel *q=0;
521 int oops;
522 int i;
523 int base = dev->base_addr;
525 save_flags(flags);
526 cli();
527 if(QInIdle) {
528 restore_flags(flags);
529 return;
531 QInIdle = 1;
534 restore_flags(flags);
536 /* this tri-states the IRQ line */
537 (void) inb_p(base+6);
539 oops = 100;
541 loop:
542 if (0>oops--) {
543 printk("idle: looped too many times\n");
544 goto done;
547 state = inb_p(base+6);
548 if (state != inb_p(base+6)) goto loop;
550 switch(state) {
551 case 0xfc:
552 /* incoming command */
553 if (debug&DEBUG_LOWER) printk("idle: fc\n");
554 handlefc(dev);
555 break;
556 case 0xfd:
557 /* incoming data */
558 if(debug&DEBUG_LOWER) printk("idle: fd\n");
559 handlefd(dev);
560 break;
561 case 0xf9:
562 /* result ready */
563 if (debug&DEBUG_LOWER) printk("idle: f9\n");
564 if(!mboxinuse[0]) {
565 mboxinuse[0] = 1;
566 qels[0].cbuf = rescbuf;
567 qels[0].cbuflen = 2;
568 qels[0].dbuf = resdbuf;
569 qels[0].dbuflen = 2;
570 qels[0].QWrite = 0;
571 qels[0].mailbox = 0;
572 enQ(&qels[0]);
574 inb_p(dev->base_addr+1);
575 inb_p(dev->base_addr+0);
576 if( wait_timeout(dev,0xf9) )
577 printk("timed out idle f9\n");
578 break;
579 case 0xf8:
580 /* ?? */
581 if (xmQhd) {
582 inb_p(dev->base_addr+1);
583 inb_p(dev->base_addr+0);
584 if(wait_timeout(dev,0xf8) )
585 printk("timed out idle f8\n");
586 } else {
587 goto done;
589 break;
590 case 0xfa:
591 /* waiting for command */
592 if(debug&DEBUG_LOWER) printk("idle: fa\n");
593 if (xmQhd) {
594 q=deQ();
595 memcpy(ltdmacbuf,q->cbuf,q->cbuflen);
596 ltdmacbuf[1] = q->mailbox;
597 if (debug>1) {
598 int n;
599 printk("ltpc: sent command ");
600 n = q->cbuflen;
601 if (n>100) n=100;
602 for(i=0;i<n;i++)
603 printk("%02x ",ltdmacbuf[i]);
604 printk("\n");
606 handlecommand(dev);
607 if(0xfa==inb_p(base+6)) {
608 /* we timed out, so return */
609 goto done;
611 } else {
612 /* we don't seem to have a command */
613 if (!mboxinuse[0]) {
614 mboxinuse[0] = 1;
615 qels[0].cbuf = rescbuf;
616 qels[0].cbuflen = 2;
617 qels[0].dbuf = resdbuf;
618 qels[0].dbuflen = 2;
619 qels[0].QWrite = 0;
620 qels[0].mailbox = 0;
621 enQ(&qels[0]);
622 } else {
623 printk("trouble: response command already queued\n");
624 goto done;
627 break;
628 case 0Xfb:
629 /* data transfer ready */
630 if(debug&DEBUG_LOWER) printk("idle: fb\n");
631 if(q->QWrite) {
632 memcpy(ltdmabuf,q->dbuf,q->dbuflen);
633 handlewrite(dev);
634 } else {
635 handleread(dev);
636 /* non-zero mailbox numbers are for
637 commmands, 0 is for GETRESULT
638 requests */
639 if(q->mailbox) {
640 memcpy(q->dbuf,ltdmabuf,q->dbuflen);
641 } else {
642 /* this was a result */
643 mailbox[ 0x0f & ltdmabuf[0] ] = ltdmabuf[1];
644 mboxinuse[0]=0;
647 break;
649 goto loop;
651 done:
652 QInIdle=0;
654 /* now set the interrupts back as appropriate */
655 /* the first read takes it out of tri-state (but still high) */
656 /* the second resets it */
657 /* note that after this point, any read of base+6 will
658 trigger an interrupt */
660 if (dev->irq) {
661 inb_p(base+7);
662 inb_p(base+7);
664 return;
668 static int do_write(struct net_device *dev, void *cbuf, int cbuflen,
669 void *dbuf, int dbuflen)
672 int i = getmbox();
673 int ret;
675 if(i) {
676 qels[i].cbuf = (unsigned char *) cbuf;
677 qels[i].cbuflen = cbuflen;
678 qels[i].dbuf = (unsigned char *) dbuf;
679 qels[i].dbuflen = dbuflen;
680 qels[i].QWrite = 1;
681 qels[i].mailbox = i; /* this should be initted rather */
682 enQ(&qels[i]);
683 idle(dev);
684 ret = mailbox[i];
685 mboxinuse[i]=0;
686 return ret;
688 printk("ltpc: could not allocate mbox\n");
689 return -1;
692 static int do_read(struct net_device *dev, void *cbuf, int cbuflen,
693 void *dbuf, int dbuflen)
696 int i = getmbox();
697 int ret;
699 if(i) {
700 qels[i].cbuf = (unsigned char *) cbuf;
701 qels[i].cbuflen = cbuflen;
702 qels[i].dbuf = (unsigned char *) dbuf;
703 qels[i].dbuflen = dbuflen;
704 qels[i].QWrite = 0;
705 qels[i].mailbox = i; /* this should be initted rather */
706 enQ(&qels[i]);
707 idle(dev);
708 ret = mailbox[i];
709 mboxinuse[i]=0;
710 return ret;
712 printk("ltpc: could not allocate mbox\n");
713 return -1;
716 /* end of idle handlers -- what should be seen is do_read, do_write */
718 static struct timer_list ltpc_timer;
720 static int ltpc_xmit(struct sk_buff *skb, struct net_device *dev);
721 static struct net_device_stats *ltpc_get_stats(struct net_device *dev);
723 static int ltpc_open(struct net_device *dev)
725 #ifdef MODULE
726 MOD_INC_USE_COUNT;
727 #endif
728 return 0;
731 static int ltpc_close(struct net_device *dev)
733 #ifdef MODULE
734 MOD_DEC_USE_COUNT;
735 #endif
736 return 0;
739 static int read_30 ( struct net_device *dev)
741 lt_command c;
742 c.getflags.command = LT_GETFLAGS;
743 return do_read(dev, &c, sizeof(c.getflags),&c,0);
746 static int set_30 (struct net_device *dev,int x)
748 lt_command c;
749 c.setflags.command = LT_SETFLAGS;
750 c.setflags.flags = x;
751 return do_write(dev, &c, sizeof(c.setflags),&c,0);
754 /* LLAP to DDP translation */
756 static int sendup_buffer (struct net_device *dev)
758 /* on entry, command is in ltdmacbuf, data in ltdmabuf */
759 /* called from idle, non-reentrant */
761 int dnode, snode, llaptype, len;
762 int sklen;
763 struct sk_buff *skb;
764 struct net_device_stats *stats = &((struct ltpc_private *)dev->priv)->stats;
765 struct lt_rcvlap *ltc = (struct lt_rcvlap *) ltdmacbuf;
767 if (ltc->command != LT_RCVLAP) {
768 printk("unknown command 0x%02x from ltpc card\n",ltc->command);
769 return(-1);
771 dnode = ltc->dnode;
772 snode = ltc->snode;
773 llaptype = ltc->laptype;
774 len = ltc->length;
776 sklen = len;
777 if (llaptype == 1)
778 sklen += 8; /* correct for short ddp */
779 if(sklen > 800) {
780 printk(KERN_INFO "%s: nonsense length in ltpc command 0x14: 0x%08x\n",
781 dev->name,sklen);
782 return -1;
785 if ( (llaptype==0) || (llaptype>2) ) {
786 printk(KERN_INFO "%s: unknown LLAP type: %d\n",dev->name,llaptype);
787 return -1;
791 skb = dev_alloc_skb(3+sklen);
792 if (skb == NULL)
794 printk("%s: dropping packet due to memory squeeze.\n",
795 dev->name);
796 return -1;
798 skb->dev = dev;
800 if (sklen > len)
801 skb_reserve(skb,8);
802 skb_put(skb,len+3);
803 skb->protocol = htons(ETH_P_LOCALTALK);
804 /* add LLAP header */
805 skb->data[0] = dnode;
806 skb->data[1] = snode;
807 skb->data[2] = llaptype;
808 skb->mac.raw = skb->data; /* save pointer to llap header */
809 skb_pull(skb,3);
811 /* copy ddp(s,e)hdr + contents */
812 memcpy(skb->data,(void*)ltdmabuf,len);
814 skb->h.raw = skb->data;
816 stats->rx_packets++;
817 stats->rx_bytes+=skb->len;
819 /* toss it onwards */
820 netif_rx(skb);
821 return 0;
824 /* the handler for the board interrupt */
826 static void ltpc_interrupt(int irq, void *dev_id, struct pt_regs *reg_ptr)
828 struct net_device *dev = dev_id;
830 if (dev==NULL) {
831 printk("ltpc_interrupt: unknown device.\n");
832 return;
835 inb_p(dev->base_addr+6); /* disable further interrupts from board */
837 idle(dev); /* handle whatever is coming in */
839 /* idle re-enables interrupts from board */
841 return;
844 /***
846 * The ioctls that the driver responds to are:
848 * SIOCSIFADDR -- do probe using the passed node hint.
849 * SIOCGIFADDR -- return net, node.
851 * some of this stuff should be done elsewhere.
853 ***/
855 static int ltpc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
857 struct sockaddr_at *sa = (struct sockaddr_at *) &ifr->ifr_addr;
858 /* we'll keep the localtalk node address in dev->pa_addr */
859 struct at_addr *aa = &((struct ltpc_private *)dev->priv)->my_addr;
860 struct lt_init c;
861 int ltflags;
863 if(debug&DEBUG_VERBOSE) printk("ltpc_ioctl called\n");
865 switch(cmd) {
866 case SIOCSIFADDR:
868 aa->s_net = sa->sat_addr.s_net;
870 /* this does the probe and returns the node addr */
871 c.command = LT_INIT;
872 c.hint = sa->sat_addr.s_node;
874 aa->s_node = do_read(dev,&c,sizeof(c),&c,0);
876 /* get all llap frames raw */
877 ltflags = read_30(dev);
878 ltflags |= LT_FLAG_ALLLAP;
879 set_30 (dev,ltflags);
881 dev->broadcast[0] = 0xFF;
882 dev->dev_addr[0] = aa->s_node;
884 dev->addr_len=1;
886 return 0;
888 case SIOCGIFADDR:
890 sa->sat_addr.s_net = aa->s_net;
891 sa->sat_addr.s_node = aa->s_node;
893 return 0;
895 default:
896 return -EINVAL;
900 static void set_multicast_list(struct net_device *dev)
902 /* This needs to be present to keep netatalk happy. */
903 /* Actually netatalk needs fixing! */
906 static int ltpc_hard_header (struct sk_buff *skb, struct net_device *dev,
907 unsigned short type, void *daddr, void *saddr, unsigned len)
909 if(debug&DEBUG_VERBOSE)
910 printk("ltpc_hard_header called for device %s\n",
911 dev->name);
912 return 0;
915 static int ltpc_init(struct net_device *dev)
917 /* Initialize the device structure. */
919 /* Fill in the fields of the device structure with ethernet-generic values. */
920 ltalk_setup(dev);
921 dev->hard_start_xmit = ltpc_xmit;
922 dev->hard_header = ltpc_hard_header;
924 dev->priv = kmalloc(sizeof(struct ltpc_private), GFP_KERNEL);
925 if(!dev->priv)
927 printk(KERN_INFO "%s: could not allocate statistics buffer\n", dev->name);
928 return -ENOMEM;
931 memset(dev->priv, 0, sizeof(struct ltpc_private));
932 dev->get_stats = ltpc_get_stats;
934 dev->open = ltpc_open;
935 dev->stop = ltpc_close;
937 /* add the ltpc-specific things */
938 dev->do_ioctl = &ltpc_ioctl;
940 dev->set_multicast_list = &set_multicast_list;
941 dev->mc_list = NULL;
943 return 0;
946 static int ltpc_poll_counter = 0;
948 static void ltpc_poll(unsigned long l)
950 struct net_device *dev = (struct net_device *) l;
952 del_timer(&ltpc_timer);
954 if(debug&DEBUG_VERBOSE) {
955 if (!ltpc_poll_counter) {
956 ltpc_poll_counter = 50;
957 printk("ltpc poll is alive\n");
959 ltpc_poll_counter--;
962 if (!dev)
963 return; /* we've been downed */
965 idle(dev);
966 ltpc_timer.expires = jiffies+5;
968 add_timer(&ltpc_timer);
971 /* DDP to LLAP translation */
973 static int ltpc_xmit(struct sk_buff *skb, struct net_device *dev)
975 /* in kernel 1.3.xx, on entry skb->data points to ddp header,
976 * and skb->len is the length of the ddp data + ddp header
979 struct net_device_stats *stats = &((struct ltpc_private *)dev->priv)->stats;
981 int i;
982 struct lt_sendlap cbuf;
984 cbuf.command = LT_SENDLAP;
985 cbuf.dnode = skb->data[0];
986 cbuf.laptype = skb->data[2];
987 skb_pull(skb,3); /* skip past LLAP header */
988 cbuf.length = skb->len; /* this is host order */
989 skb->h.raw=skb->data;
991 if(debug&DEBUG_UPPER) {
992 printk("command ");
993 for(i=0;i<6;i++)
994 printk("%02x ",((unsigned char *)&cbuf)[i]);
995 printk("\n");
998 do_write(dev,&cbuf,sizeof(cbuf),skb->h.raw,skb->len);
1000 if(debug&DEBUG_UPPER) {
1001 printk("sent %d ddp bytes\n",skb->len);
1002 for(i=0;i<skb->len;i++) printk("%02x ",skb->h.raw[i]);
1003 printk("\n");
1006 stats->tx_packets++;
1007 stats->tx_bytes+=skb->len;
1009 dev_kfree_skb(skb);
1010 return 0;
1013 static struct net_device_stats *ltpc_get_stats(struct net_device *dev)
1015 struct net_device_stats *stats = &((struct ltpc_private *) dev->priv)->stats;
1016 return stats;
1019 /* initialization stuff */
1021 int __init ltpc_probe_dma(int base)
1023 int dma = 0;
1024 int timeout;
1025 unsigned long f;
1027 if (!request_dma(1,"ltpc")) {
1028 f=claim_dma_lock();
1029 disable_dma(1);
1030 clear_dma_ff(1);
1031 set_dma_mode(1,DMA_MODE_WRITE);
1032 set_dma_addr(1,virt_to_bus(ltdmabuf));
1033 set_dma_count(1,sizeof(struct lt_mem));
1034 enable_dma(1);
1035 release_dma_lock(f);
1036 dma|=1;
1038 if (!request_dma(3,"ltpc")) {
1039 f=claim_dma_lock();
1040 disable_dma(3);
1041 clear_dma_ff(3);
1042 set_dma_mode(3,DMA_MODE_WRITE);
1043 set_dma_addr(3,virt_to_bus(ltdmabuf));
1044 set_dma_count(3,sizeof(struct lt_mem));
1045 enable_dma(3);
1046 release_dma_lock(f);
1047 dma|=2;
1050 /* set up request */
1052 /* FIXME -- do timings better! */
1054 ltdmabuf[0] = LT_READMEM;
1055 ltdmabuf[1] = 1; /* mailbox */
1056 ltdmabuf[2] = 0; ltdmabuf[3] = 0; /* address */
1057 ltdmabuf[4] = 0; ltdmabuf[5] = 1; /* read 0x0100 bytes */
1058 ltdmabuf[6] = 0; /* dunno if this is necessary */
1060 inb_p(io+1);
1061 inb_p(io+0);
1062 timeout = jiffies+100*HZ/100;
1063 while(time_before(jiffies, timeout)) {
1064 if ( 0xfa == inb_p(io+6) ) break;
1067 inb_p(io+3);
1068 inb_p(io+2);
1069 while(time_before(jiffies, timeout)) {
1070 if ( 0xfb == inb_p(io+6) ) break;
1073 /* release the other dma channel (if we opened both of them) */
1075 if ( (dma&0x2) && (get_dma_residue(3)==sizeof(struct lt_mem)) ){
1076 dma&=1;
1077 free_dma(3);
1080 if ( (dma&0x1) && (get_dma_residue(1)==sizeof(struct lt_mem)) ){
1081 dma&=0x2;
1082 free_dma(1);
1085 /* fix up dma number */
1086 dma|=1;
1088 return dma;
1091 int __init ltpc_probe(struct net_device *dev)
1093 int err;
1094 int x=0,y=0;
1095 int timeout;
1096 int autoirq;
1097 unsigned long flags;
1098 unsigned long f;
1100 save_flags(flags);
1102 /* probe for the I/O port address */
1103 if (io != 0x240 && !check_region(0x220,8)) {
1104 x = inb_p(0x220+6);
1105 if ( (x!=0xff) && (x>=0xf0) ) io = 0x220;
1108 if (io != 0x220 && !check_region(0x240,8)) {
1109 y = inb_p(0x240+6);
1110 if ( (y!=0xff) && (y>=0xf0) ) io = 0x240;
1113 if(io) {
1114 /* found it, now grab it */
1115 request_region(io,8,"ltpc");
1116 } else {
1117 /* give up in despair */
1118 printk ("LocalTalk card not found; 220 = %02x, 240 = %02x.\n",
1119 x,y);
1120 restore_flags(flags);
1121 return -1;
1124 /* probe for the IRQ line */
1125 if (irq < 2) {
1126 autoirq_setup(2);
1128 /* reset the interrupt line */
1129 inb_p(io+7);
1130 inb_p(io+7);
1131 /* trigger an interrupt (I hope) */
1132 inb_p(io+6);
1134 autoirq = autoirq_report(1);
1136 if (autoirq == 0) {
1137 printk("ltpc: probe at %#x failed to detect IRQ line.\n",
1138 io);
1140 else {
1141 irq = autoirq;
1145 /* allocate a DMA buffer */
1146 ltdmabuf = (unsigned char *) dma_mem_alloc(1000);
1148 if (ltdmabuf) ltdmacbuf = &ltdmabuf[800];
1150 if (!ltdmabuf) {
1151 printk("ltpc: mem alloc failed\n");
1152 restore_flags(flags);
1153 return(-1);
1156 if(debug&DEBUG_VERBOSE) {
1157 printk("ltdmabuf pointer %08lx\n",(unsigned long) ltdmabuf);
1160 /* reset the card */
1162 inb_p(io+1);
1163 inb_p(io+3);
1164 timeout = jiffies+2*HZ/100;
1165 while(time_before(jiffies, timeout)) ; /* hold it in reset for a coupla jiffies */
1166 inb_p(io+0);
1167 inb_p(io+2);
1168 inb_p(io+7); /* clear reset */
1169 inb_p(io+4);
1170 inb_p(io+5);
1171 inb_p(io+5); /* enable dma */
1172 inb_p(io+6); /* tri-state interrupt line */
1174 timeout = jiffies+100*HZ/100;
1176 while(time_before(jiffies, timeout)) {
1177 /* wait for the card to complete initialization */
1180 /* now, figure out which dma channel we're using, unless it's
1181 already been specified */
1182 /* well, 0 is a legal DMA channel, but the LTPC card doesn't
1183 use it... */
1184 if (dma == 0) {
1185 dma = ltpc_probe_dma(io);
1186 if (!dma) { /* no dma channel */
1187 printk("No DMA channel found on ltpc card.\n");
1188 restore_flags(flags);
1189 return -1;
1193 /* print out friendly message */
1195 if(irq)
1196 printk("Apple/Farallon LocalTalk-PC card at %03x, IR%d, DMA%d.\n",io,irq,dma);
1197 else
1198 printk("Apple/Farallon LocalTalk-PC card at %03x, DMA%d. Using polled mode.\n",io,dma);
1200 /* seems more logical to do this *after* probing the card... */
1201 err = ltpc_init(dev);
1202 if (err) return err;
1204 dev->base_addr = io;
1205 dev->irq = irq;
1206 dev->dma = dma;
1208 /* the card will want to send a result at this point */
1209 /* (I think... leaving out this part makes the kernel crash,
1210 so I put it back in...) */
1212 f=claim_dma_lock();
1213 disable_dma(dma);
1214 clear_dma_ff(dma);
1215 set_dma_mode(dma,DMA_MODE_READ);
1216 set_dma_addr(dma,virt_to_bus(ltdmabuf));
1217 set_dma_count(dma,0x100);
1218 enable_dma(dma);
1219 release_dma_lock(f);
1221 (void) inb_p(io+3);
1222 (void) inb_p(io+2);
1223 timeout = jiffies+100*HZ/100;
1224 while(time_before(jiffies, timeout)) {
1225 if( 0xf9 == inb_p(io+6)) break;
1228 if(debug&DEBUG_VERBOSE) {
1229 printk("setting up timer and irq\n");
1232 if (irq) {
1233 /* grab it and don't let go :-) */
1234 (void) request_irq( irq, &ltpc_interrupt, 0, "ltpc", dev);
1235 (void) inb_p(io+7); /* enable interrupts from board */
1236 (void) inb_p(io+7); /* and reset irq line */
1237 } else {
1238 /* polled mode -- 20 times per second */
1239 /* this is really, really slow... should it poll more often? */
1240 init_timer(&ltpc_timer);
1241 ltpc_timer.function=ltpc_poll;
1242 ltpc_timer.data = (unsigned long) dev;
1244 ltpc_timer.expires = jiffies + 5;
1245 add_timer(&ltpc_timer);
1246 restore_flags(flags);
1249 return 0;
1252 /* handles "ltpc=io,irq,dma" kernel command lines */
1253 void __init ltpc_setup(char *str, int *ints)
1255 if (ints[0] == 0) {
1256 if (str && !strncmp(str, "auto", 4)) {
1257 /* do nothing :-) */
1259 else {
1260 /* usage message */
1261 printk (KERN_ERR
1262 "ltpc: usage: ltpc=auto|iobase[,irq[,dma]]\n");
1264 return;
1265 } else {
1266 io = ints[1];
1267 if (ints[0] > 1) {
1268 irq = ints[2];
1269 return;
1271 if (ints[0] > 2) {
1272 dma = ints[3];
1273 return;
1275 /* ignore any other paramters */
1277 return;
1280 #ifdef MODULE
1282 static char dev_name[8];
1284 static struct net_device dev_ltpc = {
1285 dev_name,
1286 0, 0, 0, 0,
1287 0x0, 0,
1288 0, 0, 0, NULL, ltpc_probe };
1290 MODULE_PARM(debug, "i");
1291 MODULE_PARM(io, "i");
1292 MODULE_PARM(irq, "i");
1293 MODULE_PARM(dma, "i");
1295 int init_module(void)
1297 int err, result;
1299 if(io == 0)
1300 printk(KERN_NOTICE
1301 "ltpc: Autoprobing is not recommended for modules\n");
1303 /* Find a name for this unit */
1304 err=dev_alloc_name(&dev_ltpc,"lt%d");
1306 if(err<0)
1307 return err;
1309 if ((result = register_netdev(&dev_ltpc)) != 0) {
1310 printk(KERN_DEBUG "could not register Localtalk-PC device\n");
1311 return result;
1312 } else {
1313 if(debug&DEBUG_VERBOSE) printk("0 from register_netdev\n");
1314 return 0;
1318 void cleanup_module(void)
1320 long timeout;
1322 ltpc_timer.data = 0; /* signal the poll routine that we're done */
1324 if(debug&DEBUG_VERBOSE) printk("freeing irq\n");
1326 if(dev_ltpc.irq) {
1327 free_irq(dev_ltpc.irq,&dev_ltpc);
1328 dev_ltpc.irq = 0;
1331 if(del_timer(&ltpc_timer))
1333 /* either the poll was never started, or a poll is in process */
1334 if(debug&DEBUG_VERBOSE) printk("waiting\n");
1335 /* if it's in process, wait a bit for it to finish */
1336 timeout = jiffies+HZ;
1337 add_timer(&ltpc_timer);
1338 while(del_timer(&ltpc_timer) && time_after(timeout, jiffies))
1340 add_timer(&ltpc_timer);
1341 schedule();
1345 if(debug&DEBUG_VERBOSE) printk("freeing dma\n");
1347 if(dev_ltpc.dma) {
1348 free_dma(dev_ltpc.dma);
1349 dev_ltpc.dma = 0;
1352 if(debug&DEBUG_VERBOSE) printk("freeing ioaddr\n");
1354 if(dev_ltpc.base_addr) {
1355 release_region(dev_ltpc.base_addr,8);
1356 dev_ltpc.base_addr = 0;
1359 if(debug&DEBUG_VERBOSE) printk("free_pages\n");
1361 free_pages( (unsigned long) ltdmabuf, __get_order(1000));
1362 ltdmabuf=NULL;
1363 ltdmacbuf=NULL;
1365 if(debug&DEBUG_VERBOSE) printk("unregister_netdev\n");
1367 unregister_netdev(&dev_ltpc);
1369 if(debug&DEBUG_VERBOSE) printk("returning from cleanup_module\n");
1371 #endif /* MODULE */