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!
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
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
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
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.
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.
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.
183 * I/O address -- choose one.
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 */
198 #define DEBUG_VERBOSE 1
199 #define DEBUG_UPPER 2
200 #define DEBUG_LOWER 4
207 #include <linux/module.h>
208 #include <linux/version.h>
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>
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>
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
)
254 size
= (size
- 1) >> (PAGE_SHIFT
- 1);
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 */
278 struct net_device_stats stats
;
279 struct at_addr my_addr
;
282 /* transmit queue element struct */
285 struct xmitQel
*next
;
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
)
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)
322 struct xmitQel
*qel
=NULL
;
328 if(!xmQhd
) xmQtl
= NULL
;
330 restore_flags(flags
);
332 if ((debug
&DEBUG_LOWER
) && qel
) {
334 printk("ltpc: dequeued command ");
337 for(i
=0;i
<n
;i
++) printk("%02x ",qel
->cbuf
[i
]);
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 */
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)
376 for(i
=1;i
<16;i
++) if(!mboxinuse
[i
]) {
378 restore_flags(flags
);
381 restore_flags(flags
);
385 /* read a command from the card */
386 static void handlefc(struct net_device
*dev
)
388 /* called *only* from idle, non-reentrant */
390 int base
= dev
->base_addr
;
394 flags
=claim_dma_lock();
397 set_dma_mode(dma
,DMA_MODE_READ
);
398 set_dma_addr(dma
,virt_to_bus(ltdmacbuf
));
399 set_dma_count(dma
,50);
401 release_dma_lock(flags
);
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
)
413 int base
= dev
->base_addr
;
416 flags
=claim_dma_lock();
419 set_dma_mode(dma
,DMA_MODE_READ
);
420 set_dma_addr(dma
,virt_to_bus(ltdmabuf
));
421 set_dma_count(dma
,800);
423 release_dma_lock(flags
);
428 if ( wait_timeout(dev
,0xfd) ) printk("timed out in handlefd\n");
432 static void handlewrite(struct net_device
*dev
)
434 /* called *only* from idle, non-reentrant */
435 /* on entry, 0xfb and ltdmabuf holds data */
437 int base
= dev
->base_addr
;
440 flags
=claim_dma_lock();
443 set_dma_mode(dma
,DMA_MODE_WRITE
);
444 set_dma_addr(dma
,virt_to_bus(ltdmabuf
));
445 set_dma_count(dma
,800);
447 release_dma_lock(flags
);
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
)
463 /* on exit, ltdmabuf holds data */
465 int base
= dev
->base_addr
;
469 flags
=claim_dma_lock();
472 set_dma_mode(dma
,DMA_MODE_READ
);
473 set_dma_addr(dma
,virt_to_bus(ltdmabuf
));
474 set_dma_count(dma
,800);
476 release_dma_lock(flags
);
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 */
487 int base
= dev
->base_addr
;
490 flags
=claim_dma_lock();
493 set_dma_mode(dma
,DMA_MODE_WRITE
);
494 set_dma_addr(dma
,virt_to_bus(ltdmacbuf
));
495 set_dma_count(dma
,50);
497 release_dma_lock(flags
);
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
)
517 /* FIXME This is initialized to shut the warning up, but I need to
518 * think this through again.
523 int base
= dev
->base_addr
;
528 restore_flags(flags
);
534 restore_flags(flags
);
536 /* this tri-states the IRQ line */
537 (void) inb_p(base
+6);
543 printk("idle: looped too many times\n");
547 state
= inb_p(base
+6);
548 if (state
!= inb_p(base
+6)) goto loop
;
552 /* incoming command */
553 if (debug
&DEBUG_LOWER
) printk("idle: fc\n");
558 if(debug
&DEBUG_LOWER
) printk("idle: fd\n");
563 if (debug
&DEBUG_LOWER
) printk("idle: f9\n");
566 qels
[0].cbuf
= rescbuf
;
568 qels
[0].dbuf
= resdbuf
;
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");
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");
591 /* waiting for command */
592 if(debug
&DEBUG_LOWER
) printk("idle: fa\n");
595 memcpy(ltdmacbuf
,q
->cbuf
,q
->cbuflen
);
596 ltdmacbuf
[1] = q
->mailbox
;
599 printk("ltpc: sent command ");
603 printk("%02x ",ltdmacbuf
[i
]);
607 if(0xfa==inb_p(base
+6)) {
608 /* we timed out, so return */
612 /* we don't seem to have a command */
615 qels
[0].cbuf
= rescbuf
;
617 qels
[0].dbuf
= resdbuf
;
623 printk("trouble: response command already queued\n");
629 /* data transfer ready */
630 if(debug
&DEBUG_LOWER
) printk("idle: fb\n");
632 memcpy(ltdmabuf
,q
->dbuf
,q
->dbuflen
);
636 /* non-zero mailbox numbers are for
637 commmands, 0 is for GETRESULT
640 memcpy(q
->dbuf
,ltdmabuf
,q
->dbuflen
);
642 /* this was a result */
643 mailbox
[ 0x0f & ltdmabuf
[0] ] = ltdmabuf
[1];
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 */
668 static int do_write(struct net_device
*dev
, void *cbuf
, int cbuflen
,
669 void *dbuf
, int dbuflen
)
676 qels
[i
].cbuf
= (unsigned char *) cbuf
;
677 qels
[i
].cbuflen
= cbuflen
;
678 qels
[i
].dbuf
= (unsigned char *) dbuf
;
679 qels
[i
].dbuflen
= dbuflen
;
681 qels
[i
].mailbox
= i
; /* this should be initted rather */
688 printk("ltpc: could not allocate mbox\n");
692 static int do_read(struct net_device
*dev
, void *cbuf
, int cbuflen
,
693 void *dbuf
, int dbuflen
)
700 qels
[i
].cbuf
= (unsigned char *) cbuf
;
701 qels
[i
].cbuflen
= cbuflen
;
702 qels
[i
].dbuf
= (unsigned char *) dbuf
;
703 qels
[i
].dbuflen
= dbuflen
;
705 qels
[i
].mailbox
= i
; /* this should be initted rather */
712 printk("ltpc: could not allocate mbox\n");
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
)
731 static int ltpc_close(struct net_device
*dev
)
739 static int read_30 ( struct net_device
*dev
)
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
)
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
;
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
);
773 llaptype
= ltc
->laptype
;
778 sklen
+= 8; /* correct for short ddp */
780 printk(KERN_INFO
"%s: nonsense length in ltpc command 0x14: 0x%08x\n",
785 if ( (llaptype
==0) || (llaptype
>2) ) {
786 printk(KERN_INFO
"%s: unknown LLAP type: %d\n",dev
->name
,llaptype
);
791 skb
= dev_alloc_skb(3+sklen
);
794 printk("%s: dropping packet due to memory squeeze.\n",
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 */
811 /* copy ddp(s,e)hdr + contents */
812 memcpy(skb
->data
,(void*)ltdmabuf
,len
);
814 skb
->h
.raw
= skb
->data
;
817 stats
->rx_bytes
+=skb
->len
;
819 /* toss it onwards */
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
;
831 printk("ltpc_interrupt: unknown device.\n");
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 */
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.
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
;
863 if(debug
&DEBUG_VERBOSE
) printk("ltpc_ioctl called\n");
868 aa
->s_net
= sa
->sat_addr
.s_net
;
870 /* this does the probe and returns the node addr */
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
;
890 sa
->sat_addr
.s_net
= aa
->s_net
;
891 sa
->sat_addr
.s_node
= aa
->s_node
;
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",
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. */
921 dev
->hard_start_xmit
= ltpc_xmit
;
922 dev
->hard_header
= ltpc_hard_header
;
924 dev
->priv
= kmalloc(sizeof(struct ltpc_private
), GFP_KERNEL
);
927 printk(KERN_INFO
"%s: could not allocate statistics buffer\n", dev
->name
);
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
= <pc_ioctl
;
940 dev
->set_multicast_list
= &set_multicast_list
;
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(<pc_timer
);
954 if(debug
&DEBUG_VERBOSE
) {
955 if (!ltpc_poll_counter
) {
956 ltpc_poll_counter
= 50;
957 printk("ltpc poll is alive\n");
963 return; /* we've been downed */
966 ltpc_timer
.expires
= jiffies
+5;
968 add_timer(<pc_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
;
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
) {
994 printk("%02x ",((unsigned char *)&cbuf
)[i
]);
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
]);
1006 stats
->tx_packets
++;
1007 stats
->tx_bytes
+=skb
->len
;
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
;
1019 /* initialization stuff */
1021 int __init
ltpc_probe_dma(int base
)
1027 if (!request_dma(1,"ltpc")) {
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
));
1035 release_dma_lock(f
);
1038 if (!request_dma(3,"ltpc")) {
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
));
1046 release_dma_lock(f
);
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 */
1062 timeout
= jiffies
+100*HZ
/100;
1063 while(time_before(jiffies
, timeout
)) {
1064 if ( 0xfa == inb_p(io
+6) ) break;
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
)) ){
1080 if ( (dma
&0x1) && (get_dma_residue(1)==sizeof(struct lt_mem
)) ){
1085 /* fix up dma number */
1091 int __init
ltpc_probe(struct net_device
*dev
)
1097 unsigned long flags
;
1102 /* probe for the I/O port address */
1103 if (io
!= 0x240 && !check_region(0x220,8)) {
1105 if ( (x
!=0xff) && (x
>=0xf0) ) io
= 0x220;
1108 if (io
!= 0x220 && !check_region(0x240,8)) {
1110 if ( (y
!=0xff) && (y
>=0xf0) ) io
= 0x240;
1114 /* found it, now grab it */
1115 request_region(io
,8,"ltpc");
1117 /* give up in despair */
1118 printk ("LocalTalk card not found; 220 = %02x, 240 = %02x.\n",
1120 restore_flags(flags
);
1124 /* probe for the IRQ line */
1128 /* reset the interrupt line */
1131 /* trigger an interrupt (I hope) */
1134 autoirq
= autoirq_report(1);
1137 printk("ltpc: probe at %#x failed to detect IRQ line.\n",
1145 /* allocate a DMA buffer */
1146 ltdmabuf
= (unsigned char *) dma_mem_alloc(1000);
1148 if (ltdmabuf
) ltdmacbuf
= <dmabuf
[800];
1151 printk("ltpc: mem alloc failed\n");
1152 restore_flags(flags
);
1156 if(debug
&DEBUG_VERBOSE
) {
1157 printk("ltdmabuf pointer %08lx\n",(unsigned long) ltdmabuf
);
1160 /* reset the card */
1164 timeout
= jiffies
+2*HZ
/100;
1165 while(time_before(jiffies
, timeout
)) ; /* hold it in reset for a coupla jiffies */
1168 inb_p(io
+7); /* clear reset */
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
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
);
1193 /* print out friendly message */
1196 printk("Apple/Farallon LocalTalk-PC card at %03x, IR%d, DMA%d.\n",io
,irq
,dma
);
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
;
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...) */
1215 set_dma_mode(dma
,DMA_MODE_READ
);
1216 set_dma_addr(dma
,virt_to_bus(ltdmabuf
));
1217 set_dma_count(dma
,0x100);
1219 release_dma_lock(f
);
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");
1233 /* grab it and don't let go :-) */
1234 (void) request_irq( irq
, <pc_interrupt
, 0, "ltpc", dev
);
1235 (void) inb_p(io
+7); /* enable interrupts from board */
1236 (void) inb_p(io
+7); /* and reset irq line */
1238 /* polled mode -- 20 times per second */
1239 /* this is really, really slow... should it poll more often? */
1240 init_timer(<pc_timer
);
1241 ltpc_timer
.function
=ltpc_poll
;
1242 ltpc_timer
.data
= (unsigned long) dev
;
1244 ltpc_timer
.expires
= jiffies
+ 5;
1245 add_timer(<pc_timer
);
1246 restore_flags(flags
);
1252 /* handles "ltpc=io,irq,dma" kernel command lines */
1253 void __init
ltpc_setup(char *str
, int *ints
)
1256 if (str
&& !strncmp(str
, "auto", 4)) {
1257 /* do nothing :-) */
1262 "ltpc: usage: ltpc=auto|iobase[,irq[,dma]]\n");
1275 /* ignore any other paramters */
1282 static char dev_name
[8];
1284 static struct net_device dev_ltpc
= {
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)
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");
1309 if ((result
= register_netdev(&dev_ltpc
)) != 0) {
1310 printk(KERN_DEBUG
"could not register Localtalk-PC device\n");
1313 if(debug
&DEBUG_VERBOSE
) printk("0 from register_netdev\n");
1318 void cleanup_module(void)
1322 ltpc_timer
.data
= 0; /* signal the poll routine that we're done */
1324 if(debug
&DEBUG_VERBOSE
) printk("freeing irq\n");
1327 free_irq(dev_ltpc
.irq
,&dev_ltpc
);
1331 if(del_timer(<pc_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(<pc_timer
);
1338 while(del_timer(<pc_timer
) && time_after(timeout
, jiffies
))
1340 add_timer(<pc_timer
);
1345 if(debug
&DEBUG_VERBOSE
) printk("freeing dma\n");
1348 free_dma(dev_ltpc
.dma
);
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));
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");