[ARM] pxa: Gumstix Verdex PCMCIA support
[linux-2.6/verdex.git] / drivers / net / appletalk / ltpc.c
blob08760baece7ac97fc638f52bf4b6a66251dcdaff
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.1.2.1 2000/03/01 05:35:07 jgarzik
66 * at and tr cleanup
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
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...
92 * Revision 1.7 1996/12/12 03:42:33 bradford
93 * DMA alloc cribbed from 3c505.c.
95 * Revision 1.6 1996/12/12 03:18:58 bradford
96 * Added virt_to_bus; works in 2.1.13.
98 * Revision 1.5 1996/12/12 03:13:22 root
99 * xmitQel initialization -- think through better though.
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.
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)
108 * Revision 1.2 1996/05/22 14:58:24 root
109 * Change tabs mostly.
111 * Revision 1.1 1996/04/23 04:45:09 root
112 * Initial revision
114 * Revision 0.16 1996/03/05 15:59:56 root
115 * Change ARPHRD_LOCALTLK definition to the "real" one.
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.
121 * Revision 0.14 1996/02/25 17:38:32 root
122 * More cleanups. Removed query to card on get_stats.
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.
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.
133 * Revision 0.11 1996/02/12 15:53:38 root
134 * Added router sends (requires new aarp.c patch)
136 * Revision 0.10 1996/02/11 00:19:35 root
137 * Change source LTALK_LOGGING debug switch to insmod ... debug=2.
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)
146 * Revision 0.8 1996/02/10 19:09:00 root
147 * Merge 1.3 changes. Tested OK under 1.3.60.
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.
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.
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.
161 * Revision 0.4 1995/12/18 03:46:44 root
162 * Return shortDDP to longDDP fake to 0/0. Added command structs.
164 ***/
166 /* ltpc jumpers are:
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.
173 * SW1 IRQ 4
174 * SW2 IRQ 3
175 * SW3 IRQ 9 (2 in original card documentation only applies to XT)
178 * DMA -- choose DMA 1 or 3, and set both corresponding switches.
180 * SW4 DMA 3
181 * SW5 DMA 1
182 * SW6 DMA 3
183 * SW7 DMA 1
186 * I/O address -- choose one.
188 * SW8 220 / 240
191 /* To have some stuff logged, do
192 * insmod ltpc.o debug=1
194 * For a whole bunch of stuff, use higher numbers.
196 * The default is 0, i.e. no messages except for the probe results.
199 /* insmod-tweakable variables */
200 static int debug;
201 #define DEBUG_VERBOSE 1
202 #define DEBUG_UPPER 2
203 #define DEBUG_LOWER 4
205 static int io;
206 static int irq;
207 static int dma;
209 #include <linux/module.h>
210 #include <linux/kernel.h>
211 #include <linux/types.h>
212 #include <linux/fcntl.h>
213 #include <linux/interrupt.h>
214 #include <linux/ptrace.h>
215 #include <linux/ioport.h>
216 #include <linux/spinlock.h>
217 #include <linux/in.h>
218 #include <linux/slab.h>
219 #include <linux/string.h>
220 #include <linux/errno.h>
221 #include <linux/init.h>
222 #include <linux/netdevice.h>
223 #include <linux/etherdevice.h>
224 #include <linux/skbuff.h>
225 #include <linux/if_arp.h>
226 #include <linux/if_ltalk.h>
227 #include <linux/delay.h>
228 #include <linux/timer.h>
229 #include <linux/atalk.h>
230 #include <linux/bitops.h>
232 #include <asm/system.h>
233 #include <asm/dma.h>
234 #include <asm/io.h>
236 /* our stuff */
237 #include "ltpc.h"
239 static DEFINE_SPINLOCK(txqueue_lock);
240 static DEFINE_SPINLOCK(mbox_lock);
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 static unsigned long dma_mem_alloc(int size)
251 int order = get_order(size);
253 return __get_dma_pages(GFP_KERNEL, order);
256 /* DMA data buffer, DMA command buffer */
257 static unsigned char *ltdmabuf;
258 static unsigned char *ltdmacbuf;
260 /* private struct, holds our appletalk address */
262 struct ltpc_private
264 struct atalk_addr my_addr;
267 /* transmit queue element struct */
269 struct xmitQel {
270 struct xmitQel *next;
271 /* command buffer */
272 unsigned char *cbuf;
273 short cbuflen;
274 /* data buffer */
275 unsigned char *dbuf;
276 short dbuflen;
277 unsigned char QWrite; /* read or write data */
278 unsigned char mailbox;
281 /* the transmit queue itself */
283 static struct xmitQel *xmQhd, *xmQtl;
285 static void enQ(struct xmitQel *qel)
287 unsigned long flags;
288 qel->next = NULL;
290 spin_lock_irqsave(&txqueue_lock, flags);
291 if (xmQtl) {
292 xmQtl->next = qel;
293 } else {
294 xmQhd = qel;
296 xmQtl = qel;
297 spin_unlock_irqrestore(&txqueue_lock, flags);
299 if (debug & DEBUG_LOWER)
300 printk("enqueued a 0x%02x command\n",qel->cbuf[0]);
303 static struct xmitQel *deQ(void)
305 unsigned long flags;
306 int i;
307 struct xmitQel *qel=NULL;
309 spin_lock_irqsave(&txqueue_lock, flags);
310 if (xmQhd) {
311 qel = xmQhd;
312 xmQhd = qel->next;
313 if(!xmQhd) xmQtl = NULL;
315 spin_unlock_irqrestore(&txqueue_lock, flags);
317 if ((debug & DEBUG_LOWER) && qel) {
318 int n;
319 printk(KERN_DEBUG "ltpc: dequeued command ");
320 n = qel->cbuflen;
321 if (n>100) n=100;
322 for(i=0;i<n;i++) printk("%02x ",qel->cbuf[i]);
323 printk("\n");
326 return qel;
329 /* and... the queue elements we'll be using */
330 static struct xmitQel qels[16];
332 /* and their corresponding mailboxes */
333 static unsigned char mailbox[16];
334 static unsigned char mboxinuse[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
336 static int wait_timeout(struct net_device *dev, int c)
338 /* returns true if it stayed c */
339 /* this uses base+6, but it's ok */
340 int i;
342 /* twenty second or so total */
344 for(i=0;i<200000;i++) {
345 if ( c != inb_p(dev->base_addr+6) ) return 0;
346 udelay(100);
348 return 1; /* timed out */
351 /* get the first free mailbox */
353 static int getmbox(void)
355 unsigned long flags;
356 int i;
358 spin_lock_irqsave(&mbox_lock, flags);
359 for(i=1;i<16;i++) if(!mboxinuse[i]) {
360 mboxinuse[i]=1;
361 spin_unlock_irqrestore(&mbox_lock, flags);
362 return i;
364 spin_unlock_irqrestore(&mbox_lock, flags);
365 return 0;
368 /* read a command from the card */
369 static void handlefc(struct net_device *dev)
371 /* called *only* from idle, non-reentrant */
372 int dma = dev->dma;
373 int base = dev->base_addr;
374 unsigned long flags;
377 flags=claim_dma_lock();
378 disable_dma(dma);
379 clear_dma_ff(dma);
380 set_dma_mode(dma,DMA_MODE_READ);
381 set_dma_addr(dma,virt_to_bus(ltdmacbuf));
382 set_dma_count(dma,50);
383 enable_dma(dma);
384 release_dma_lock(flags);
386 inb_p(base+3);
387 inb_p(base+2);
389 if ( wait_timeout(dev,0xfc) ) printk("timed out in handlefc\n");
392 /* read data from the card */
393 static void handlefd(struct net_device *dev)
395 int dma = dev->dma;
396 int base = dev->base_addr;
397 unsigned long flags;
399 flags=claim_dma_lock();
400 disable_dma(dma);
401 clear_dma_ff(dma);
402 set_dma_mode(dma,DMA_MODE_READ);
403 set_dma_addr(dma,virt_to_bus(ltdmabuf));
404 set_dma_count(dma,800);
405 enable_dma(dma);
406 release_dma_lock(flags);
408 inb_p(base+3);
409 inb_p(base+2);
411 if ( wait_timeout(dev,0xfd) ) printk("timed out in handlefd\n");
412 sendup_buffer(dev);
415 static void handlewrite(struct net_device *dev)
417 /* called *only* from idle, non-reentrant */
418 /* on entry, 0xfb and ltdmabuf holds data */
419 int dma = dev->dma;
420 int base = dev->base_addr;
421 unsigned long flags;
423 flags=claim_dma_lock();
424 disable_dma(dma);
425 clear_dma_ff(dma);
426 set_dma_mode(dma,DMA_MODE_WRITE);
427 set_dma_addr(dma,virt_to_bus(ltdmabuf));
428 set_dma_count(dma,800);
429 enable_dma(dma);
430 release_dma_lock(flags);
432 inb_p(base+3);
433 inb_p(base+2);
435 if ( wait_timeout(dev,0xfb) ) {
436 flags=claim_dma_lock();
437 printk("timed out in handlewrite, dma res %d\n",
438 get_dma_residue(dev->dma) );
439 release_dma_lock(flags);
443 static void handleread(struct net_device *dev)
445 /* on entry, 0xfb */
446 /* on exit, ltdmabuf holds data */
447 int dma = dev->dma;
448 int base = dev->base_addr;
449 unsigned long flags;
452 flags=claim_dma_lock();
453 disable_dma(dma);
454 clear_dma_ff(dma);
455 set_dma_mode(dma,DMA_MODE_READ);
456 set_dma_addr(dma,virt_to_bus(ltdmabuf));
457 set_dma_count(dma,800);
458 enable_dma(dma);
459 release_dma_lock(flags);
461 inb_p(base+3);
462 inb_p(base+2);
463 if ( wait_timeout(dev,0xfb) ) printk("timed out in handleread\n");
466 static void handlecommand(struct net_device *dev)
468 /* on entry, 0xfa and ltdmacbuf holds command */
469 int dma = dev->dma;
470 int base = dev->base_addr;
471 unsigned long flags;
473 flags=claim_dma_lock();
474 disable_dma(dma);
475 clear_dma_ff(dma);
476 set_dma_mode(dma,DMA_MODE_WRITE);
477 set_dma_addr(dma,virt_to_bus(ltdmacbuf));
478 set_dma_count(dma,50);
479 enable_dma(dma);
480 release_dma_lock(flags);
481 inb_p(base+3);
482 inb_p(base+2);
483 if ( wait_timeout(dev,0xfa) ) printk("timed out in handlecommand\n");
486 /* ready made command for getting the result from the card */
487 static unsigned char rescbuf[2] = {LT_GETRESULT,0};
488 static unsigned char resdbuf[2];
490 static int QInIdle;
492 /* idle expects to be called with the IRQ line high -- either because of
493 * an interrupt, or because the line is tri-stated
496 static void idle(struct net_device *dev)
498 unsigned long flags;
499 int state;
500 /* FIXME This is initialized to shut the warning up, but I need to
501 * think this through again.
503 struct xmitQel *q = NULL;
504 int oops;
505 int i;
506 int base = dev->base_addr;
508 spin_lock_irqsave(&txqueue_lock, flags);
509 if(QInIdle) {
510 spin_unlock_irqrestore(&txqueue_lock, flags);
511 return;
513 QInIdle = 1;
514 spin_unlock_irqrestore(&txqueue_lock, flags);
516 /* this tri-states the IRQ line */
517 (void) inb_p(base+6);
519 oops = 100;
521 loop:
522 if (0>oops--) {
523 printk("idle: looped too many times\n");
524 goto done;
527 state = inb_p(base+6);
528 if (state != inb_p(base+6)) goto loop;
530 switch(state) {
531 case 0xfc:
532 /* incoming command */
533 if (debug & DEBUG_LOWER) printk("idle: fc\n");
534 handlefc(dev);
535 break;
536 case 0xfd:
537 /* incoming data */
538 if(debug & DEBUG_LOWER) printk("idle: fd\n");
539 handlefd(dev);
540 break;
541 case 0xf9:
542 /* result ready */
543 if (debug & DEBUG_LOWER) printk("idle: f9\n");
544 if(!mboxinuse[0]) {
545 mboxinuse[0] = 1;
546 qels[0].cbuf = rescbuf;
547 qels[0].cbuflen = 2;
548 qels[0].dbuf = resdbuf;
549 qels[0].dbuflen = 2;
550 qels[0].QWrite = 0;
551 qels[0].mailbox = 0;
552 enQ(&qels[0]);
554 inb_p(dev->base_addr+1);
555 inb_p(dev->base_addr+0);
556 if( wait_timeout(dev,0xf9) )
557 printk("timed out idle f9\n");
558 break;
559 case 0xf8:
560 /* ?? */
561 if (xmQhd) {
562 inb_p(dev->base_addr+1);
563 inb_p(dev->base_addr+0);
564 if(wait_timeout(dev,0xf8) )
565 printk("timed out idle f8\n");
566 } else {
567 goto done;
569 break;
570 case 0xfa:
571 /* waiting for command */
572 if(debug & DEBUG_LOWER) printk("idle: fa\n");
573 if (xmQhd) {
574 q=deQ();
575 memcpy(ltdmacbuf,q->cbuf,q->cbuflen);
576 ltdmacbuf[1] = q->mailbox;
577 if (debug>1) {
578 int n;
579 printk("ltpc: sent command ");
580 n = q->cbuflen;
581 if (n>100) n=100;
582 for(i=0;i<n;i++)
583 printk("%02x ",ltdmacbuf[i]);
584 printk("\n");
586 handlecommand(dev);
587 if(0xfa==inb_p(base+6)) {
588 /* we timed out, so return */
589 goto done;
591 } else {
592 /* we don't seem to have a command */
593 if (!mboxinuse[0]) {
594 mboxinuse[0] = 1;
595 qels[0].cbuf = rescbuf;
596 qels[0].cbuflen = 2;
597 qels[0].dbuf = resdbuf;
598 qels[0].dbuflen = 2;
599 qels[0].QWrite = 0;
600 qels[0].mailbox = 0;
601 enQ(&qels[0]);
602 } else {
603 printk("trouble: response command already queued\n");
604 goto done;
607 break;
608 case 0Xfb:
609 /* data transfer ready */
610 if(debug & DEBUG_LOWER) printk("idle: fb\n");
611 if(q->QWrite) {
612 memcpy(ltdmabuf,q->dbuf,q->dbuflen);
613 handlewrite(dev);
614 } else {
615 handleread(dev);
616 /* non-zero mailbox numbers are for
617 commmands, 0 is for GETRESULT
618 requests */
619 if(q->mailbox) {
620 memcpy(q->dbuf,ltdmabuf,q->dbuflen);
621 } else {
622 /* this was a result */
623 mailbox[ 0x0f & ltdmabuf[0] ] = ltdmabuf[1];
624 mboxinuse[0]=0;
627 break;
629 goto loop;
631 done:
632 QInIdle=0;
634 /* now set the interrupts back as appropriate */
635 /* the first read takes it out of tri-state (but still high) */
636 /* the second resets it */
637 /* note that after this point, any read of base+6 will
638 trigger an interrupt */
640 if (dev->irq) {
641 inb_p(base+7);
642 inb_p(base+7);
644 return;
648 static int do_write(struct net_device *dev, void *cbuf, int cbuflen,
649 void *dbuf, int dbuflen)
652 int i = getmbox();
653 int ret;
655 if(i) {
656 qels[i].cbuf = (unsigned char *) cbuf;
657 qels[i].cbuflen = cbuflen;
658 qels[i].dbuf = (unsigned char *) dbuf;
659 qels[i].dbuflen = dbuflen;
660 qels[i].QWrite = 1;
661 qels[i].mailbox = i; /* this should be initted rather */
662 enQ(&qels[i]);
663 idle(dev);
664 ret = mailbox[i];
665 mboxinuse[i]=0;
666 return ret;
668 printk("ltpc: could not allocate mbox\n");
669 return -1;
672 static int do_read(struct net_device *dev, void *cbuf, int cbuflen,
673 void *dbuf, int dbuflen)
676 int i = getmbox();
677 int ret;
679 if(i) {
680 qels[i].cbuf = (unsigned char *) cbuf;
681 qels[i].cbuflen = cbuflen;
682 qels[i].dbuf = (unsigned char *) dbuf;
683 qels[i].dbuflen = dbuflen;
684 qels[i].QWrite = 0;
685 qels[i].mailbox = i; /* this should be initted rather */
686 enQ(&qels[i]);
687 idle(dev);
688 ret = mailbox[i];
689 mboxinuse[i]=0;
690 return ret;
692 printk("ltpc: could not allocate mbox\n");
693 return -1;
696 /* end of idle handlers -- what should be seen is do_read, do_write */
698 static struct timer_list ltpc_timer;
700 static netdev_tx_t ltpc_xmit(struct sk_buff *skb, struct net_device *dev);
702 static int read_30 ( struct net_device *dev)
704 lt_command c;
705 c.getflags.command = LT_GETFLAGS;
706 return do_read(dev, &c, sizeof(c.getflags),&c,0);
709 static int set_30 (struct net_device *dev,int x)
711 lt_command c;
712 c.setflags.command = LT_SETFLAGS;
713 c.setflags.flags = x;
714 return do_write(dev, &c, sizeof(c.setflags),&c,0);
717 /* LLAP to DDP translation */
719 static int sendup_buffer (struct net_device *dev)
721 /* on entry, command is in ltdmacbuf, data in ltdmabuf */
722 /* called from idle, non-reentrant */
724 int dnode, snode, llaptype, len;
725 int sklen;
726 struct sk_buff *skb;
727 struct lt_rcvlap *ltc = (struct lt_rcvlap *) ltdmacbuf;
729 if (ltc->command != LT_RCVLAP) {
730 printk("unknown command 0x%02x from ltpc card\n",ltc->command);
731 return(-1);
733 dnode = ltc->dnode;
734 snode = ltc->snode;
735 llaptype = ltc->laptype;
736 len = ltc->length;
738 sklen = len;
739 if (llaptype == 1)
740 sklen += 8; /* correct for short ddp */
741 if(sklen > 800) {
742 printk(KERN_INFO "%s: nonsense length in ltpc command 0x14: 0x%08x\n",
743 dev->name,sklen);
744 return -1;
747 if ( (llaptype==0) || (llaptype>2) ) {
748 printk(KERN_INFO "%s: unknown LLAP type: %d\n",dev->name,llaptype);
749 return -1;
753 skb = dev_alloc_skb(3+sklen);
754 if (skb == NULL)
756 printk("%s: dropping packet due to memory squeeze.\n",
757 dev->name);
758 return -1;
760 skb->dev = dev;
762 if (sklen > len)
763 skb_reserve(skb,8);
764 skb_put(skb,len+3);
765 skb->protocol = htons(ETH_P_LOCALTALK);
766 /* add LLAP header */
767 skb->data[0] = dnode;
768 skb->data[1] = snode;
769 skb->data[2] = llaptype;
770 skb_reset_mac_header(skb); /* save pointer to llap header */
771 skb_pull(skb,3);
773 /* copy ddp(s,e)hdr + contents */
774 skb_copy_to_linear_data(skb, ltdmabuf, len);
776 skb_reset_transport_header(skb);
778 dev->stats.rx_packets++;
779 dev->stats.rx_bytes += skb->len;
781 /* toss it onwards */
782 netif_rx(skb);
783 return 0;
786 /* the handler for the board interrupt */
788 static irqreturn_t
789 ltpc_interrupt(int irq, void *dev_id)
791 struct net_device *dev = dev_id;
793 if (dev==NULL) {
794 printk("ltpc_interrupt: unknown device.\n");
795 return IRQ_NONE;
798 inb_p(dev->base_addr+6); /* disable further interrupts from board */
800 idle(dev); /* handle whatever is coming in */
802 /* idle re-enables interrupts from board */
804 return IRQ_HANDLED;
807 /***
809 * The ioctls that the driver responds to are:
811 * SIOCSIFADDR -- do probe using the passed node hint.
812 * SIOCGIFADDR -- return net, node.
814 * some of this stuff should be done elsewhere.
816 ***/
818 static int ltpc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
820 struct sockaddr_at *sa = (struct sockaddr_at *) &ifr->ifr_addr;
821 /* we'll keep the localtalk node address in dev->pa_addr */
822 struct ltpc_private *ltpc_priv = netdev_priv(dev);
823 struct atalk_addr *aa = &ltpc_priv->my_addr;
824 struct lt_init c;
825 int ltflags;
827 if(debug & DEBUG_VERBOSE) printk("ltpc_ioctl called\n");
829 switch(cmd) {
830 case SIOCSIFADDR:
832 aa->s_net = sa->sat_addr.s_net;
834 /* this does the probe and returns the node addr */
835 c.command = LT_INIT;
836 c.hint = sa->sat_addr.s_node;
838 aa->s_node = do_read(dev,&c,sizeof(c),&c,0);
840 /* get all llap frames raw */
841 ltflags = read_30(dev);
842 ltflags |= LT_FLAG_ALLLAP;
843 set_30 (dev,ltflags);
845 dev->broadcast[0] = 0xFF;
846 dev->dev_addr[0] = aa->s_node;
848 dev->addr_len=1;
850 return 0;
852 case SIOCGIFADDR:
854 sa->sat_addr.s_net = aa->s_net;
855 sa->sat_addr.s_node = aa->s_node;
857 return 0;
859 default:
860 return -EINVAL;
864 static void set_multicast_list(struct net_device *dev)
866 /* This needs to be present to keep netatalk happy. */
867 /* Actually netatalk needs fixing! */
870 static int ltpc_poll_counter;
872 static void ltpc_poll(unsigned long l)
874 struct net_device *dev = (struct net_device *) l;
876 del_timer(&ltpc_timer);
878 if(debug & DEBUG_VERBOSE) {
879 if (!ltpc_poll_counter) {
880 ltpc_poll_counter = 50;
881 printk("ltpc poll is alive\n");
883 ltpc_poll_counter--;
886 if (!dev)
887 return; /* we've been downed */
889 /* poll 20 times per second */
890 idle(dev);
891 ltpc_timer.expires = jiffies + HZ/20;
893 add_timer(&ltpc_timer);
896 /* DDP to LLAP translation */
898 static netdev_tx_t ltpc_xmit(struct sk_buff *skb, struct net_device *dev)
900 /* in kernel 1.3.xx, on entry skb->data points to ddp header,
901 * and skb->len is the length of the ddp data + ddp header
903 int i;
904 struct lt_sendlap cbuf;
905 unsigned char *hdr;
907 cbuf.command = LT_SENDLAP;
908 cbuf.dnode = skb->data[0];
909 cbuf.laptype = skb->data[2];
910 skb_pull(skb,3); /* skip past LLAP header */
911 cbuf.length = skb->len; /* this is host order */
912 skb_reset_transport_header(skb);
914 if(debug & DEBUG_UPPER) {
915 printk("command ");
916 for(i=0;i<6;i++)
917 printk("%02x ",((unsigned char *)&cbuf)[i]);
918 printk("\n");
921 hdr = skb_transport_header(skb);
922 do_write(dev, &cbuf, sizeof(cbuf), hdr, skb->len);
924 if(debug & DEBUG_UPPER) {
925 printk("sent %d ddp bytes\n",skb->len);
926 for (i = 0; i < skb->len; i++)
927 printk("%02x ", hdr[i]);
928 printk("\n");
931 dev->stats.tx_packets++;
932 dev->stats.tx_bytes += skb->len;
934 dev_kfree_skb(skb);
935 return NETDEV_TX_OK;
938 /* initialization stuff */
940 static int __init ltpc_probe_dma(int base, int dma)
942 int want = (dma == 3) ? 2 : (dma == 1) ? 1 : 3;
943 unsigned long timeout;
944 unsigned long f;
946 if (want & 1) {
947 if (request_dma(1,"ltpc")) {
948 want &= ~1;
949 } else {
950 f=claim_dma_lock();
951 disable_dma(1);
952 clear_dma_ff(1);
953 set_dma_mode(1,DMA_MODE_WRITE);
954 set_dma_addr(1,virt_to_bus(ltdmabuf));
955 set_dma_count(1,sizeof(struct lt_mem));
956 enable_dma(1);
957 release_dma_lock(f);
960 if (want & 2) {
961 if (request_dma(3,"ltpc")) {
962 want &= ~2;
963 } else {
964 f=claim_dma_lock();
965 disable_dma(3);
966 clear_dma_ff(3);
967 set_dma_mode(3,DMA_MODE_WRITE);
968 set_dma_addr(3,virt_to_bus(ltdmabuf));
969 set_dma_count(3,sizeof(struct lt_mem));
970 enable_dma(3);
971 release_dma_lock(f);
974 /* set up request */
976 /* FIXME -- do timings better! */
978 ltdmabuf[0] = LT_READMEM;
979 ltdmabuf[1] = 1; /* mailbox */
980 ltdmabuf[2] = 0; ltdmabuf[3] = 0; /* address */
981 ltdmabuf[4] = 0; ltdmabuf[5] = 1; /* read 0x0100 bytes */
982 ltdmabuf[6] = 0; /* dunno if this is necessary */
984 inb_p(io+1);
985 inb_p(io+0);
986 timeout = jiffies+100*HZ/100;
987 while(time_before(jiffies, timeout)) {
988 if ( 0xfa == inb_p(io+6) ) break;
991 inb_p(io+3);
992 inb_p(io+2);
993 while(time_before(jiffies, timeout)) {
994 if ( 0xfb == inb_p(io+6) ) break;
997 /* release the other dma channel (if we opened both of them) */
999 if ((want & 2) && (get_dma_residue(3)==sizeof(struct lt_mem))) {
1000 want &= ~2;
1001 free_dma(3);
1004 if ((want & 1) && (get_dma_residue(1)==sizeof(struct lt_mem))) {
1005 want &= ~1;
1006 free_dma(1);
1009 if (!want)
1010 return 0;
1012 return (want & 2) ? 3 : 1;
1015 static const struct net_device_ops ltpc_netdev = {
1016 .ndo_start_xmit = ltpc_xmit,
1017 .ndo_do_ioctl = ltpc_ioctl,
1018 .ndo_set_multicast_list = set_multicast_list,
1021 struct net_device * __init ltpc_probe(void)
1023 struct net_device *dev;
1024 int err = -ENOMEM;
1025 int x=0,y=0;
1026 int autoirq;
1027 unsigned long f;
1028 unsigned long timeout;
1030 dev = alloc_ltalkdev(sizeof(struct ltpc_private));
1031 if (!dev)
1032 goto out;
1034 /* probe for the I/O port address */
1036 if (io != 0x240 && request_region(0x220,8,"ltpc")) {
1037 x = inb_p(0x220+6);
1038 if ( (x!=0xff) && (x>=0xf0) ) {
1039 io = 0x220;
1040 goto got_port;
1042 release_region(0x220,8);
1044 if (io != 0x220 && request_region(0x240,8,"ltpc")) {
1045 y = inb_p(0x240+6);
1046 if ( (y!=0xff) && (y>=0xf0) ){
1047 io = 0x240;
1048 goto got_port;
1050 release_region(0x240,8);
1053 /* give up in despair */
1054 printk(KERN_ERR "LocalTalk card not found; 220 = %02x, 240 = %02x.\n", x,y);
1055 err = -ENODEV;
1056 goto out1;
1058 got_port:
1059 /* probe for the IRQ line */
1060 if (irq < 2) {
1061 unsigned long irq_mask;
1063 irq_mask = probe_irq_on();
1064 /* reset the interrupt line */
1065 inb_p(io+7);
1066 inb_p(io+7);
1067 /* trigger an interrupt (I hope) */
1068 inb_p(io+6);
1069 mdelay(2);
1070 autoirq = probe_irq_off(irq_mask);
1072 if (autoirq == 0) {
1073 printk(KERN_ERR "ltpc: probe at %#x failed to detect IRQ line.\n", io);
1074 } else {
1075 irq = autoirq;
1079 /* allocate a DMA buffer */
1080 ltdmabuf = (unsigned char *) dma_mem_alloc(1000);
1081 if (!ltdmabuf) {
1082 printk(KERN_ERR "ltpc: mem alloc failed\n");
1083 err = -ENOMEM;
1084 goto out2;
1087 ltdmacbuf = &ltdmabuf[800];
1089 if(debug & DEBUG_VERBOSE) {
1090 printk("ltdmabuf pointer %08lx\n",(unsigned long) ltdmabuf);
1093 /* reset the card */
1095 inb_p(io+1);
1096 inb_p(io+3);
1098 msleep(20);
1100 inb_p(io+0);
1101 inb_p(io+2);
1102 inb_p(io+7); /* clear reset */
1103 inb_p(io+4);
1104 inb_p(io+5);
1105 inb_p(io+5); /* enable dma */
1106 inb_p(io+6); /* tri-state interrupt line */
1108 ssleep(1);
1110 /* now, figure out which dma channel we're using, unless it's
1111 already been specified */
1112 /* well, 0 is a legal DMA channel, but the LTPC card doesn't
1113 use it... */
1114 dma = ltpc_probe_dma(io, dma);
1115 if (!dma) { /* no dma channel */
1116 printk(KERN_ERR "No DMA channel found on ltpc card.\n");
1117 err = -ENODEV;
1118 goto out3;
1121 /* print out friendly message */
1122 if(irq)
1123 printk(KERN_INFO "Apple/Farallon LocalTalk-PC card at %03x, IR%d, DMA%d.\n",io,irq,dma);
1124 else
1125 printk(KERN_INFO "Apple/Farallon LocalTalk-PC card at %03x, DMA%d. Using polled mode.\n",io,dma);
1127 dev->netdev_ops = &ltpc_netdev;
1128 dev->mc_list = NULL;
1129 dev->base_addr = io;
1130 dev->irq = irq;
1131 dev->dma = dma;
1133 /* the card will want to send a result at this point */
1134 /* (I think... leaving out this part makes the kernel crash,
1135 so I put it back in...) */
1137 f=claim_dma_lock();
1138 disable_dma(dma);
1139 clear_dma_ff(dma);
1140 set_dma_mode(dma,DMA_MODE_READ);
1141 set_dma_addr(dma,virt_to_bus(ltdmabuf));
1142 set_dma_count(dma,0x100);
1143 enable_dma(dma);
1144 release_dma_lock(f);
1146 (void) inb_p(io+3);
1147 (void) inb_p(io+2);
1148 timeout = jiffies+100*HZ/100;
1150 while(time_before(jiffies, timeout)) {
1151 if( 0xf9 == inb_p(io+6))
1152 break;
1153 schedule();
1156 if(debug & DEBUG_VERBOSE) {
1157 printk("setting up timer and irq\n");
1160 /* grab it and don't let go :-) */
1161 if (irq && request_irq( irq, &ltpc_interrupt, 0, "ltpc", dev) >= 0)
1163 (void) inb_p(io+7); /* enable interrupts from board */
1164 (void) inb_p(io+7); /* and reset irq line */
1165 } else {
1166 if( irq )
1167 printk(KERN_ERR "ltpc: IRQ already in use, using polled mode.\n");
1168 dev->irq = 0;
1169 /* polled mode -- 20 times per second */
1170 /* this is really, really slow... should it poll more often? */
1171 init_timer(&ltpc_timer);
1172 ltpc_timer.function=ltpc_poll;
1173 ltpc_timer.data = (unsigned long) dev;
1175 ltpc_timer.expires = jiffies + HZ/20;
1176 add_timer(&ltpc_timer);
1178 err = register_netdev(dev);
1179 if (err)
1180 goto out4;
1182 return NULL;
1183 out4:
1184 del_timer_sync(&ltpc_timer);
1185 if (dev->irq)
1186 free_irq(dev->irq, dev);
1187 out3:
1188 free_pages((unsigned long)ltdmabuf, get_order(1000));
1189 out2:
1190 release_region(io, 8);
1191 out1:
1192 free_netdev(dev);
1193 out:
1194 return ERR_PTR(err);
1197 #ifndef MODULE
1198 /* handles "ltpc=io,irq,dma" kernel command lines */
1199 static int __init ltpc_setup(char *str)
1201 int ints[5];
1203 str = get_options(str, ARRAY_SIZE(ints), ints);
1205 if (ints[0] == 0) {
1206 if (str && !strncmp(str, "auto", 4)) {
1207 /* do nothing :-) */
1209 else {
1210 /* usage message */
1211 printk (KERN_ERR
1212 "ltpc: usage: ltpc=auto|iobase[,irq[,dma]]\n");
1213 return 0;
1215 } else {
1216 io = ints[1];
1217 if (ints[0] > 1) {
1218 irq = ints[2];
1220 if (ints[0] > 2) {
1221 dma = ints[3];
1223 /* ignore any other parameters */
1225 return 1;
1228 __setup("ltpc=", ltpc_setup);
1229 #endif /* MODULE */
1231 static struct net_device *dev_ltpc;
1233 #ifdef MODULE
1235 MODULE_LICENSE("GPL");
1236 module_param(debug, int, 0);
1237 module_param(io, int, 0);
1238 module_param(irq, int, 0);
1239 module_param(dma, int, 0);
1242 static int __init ltpc_module_init(void)
1244 if(io == 0)
1245 printk(KERN_NOTICE
1246 "ltpc: Autoprobing is not recommended for modules\n");
1248 dev_ltpc = ltpc_probe();
1249 if (IS_ERR(dev_ltpc))
1250 return PTR_ERR(dev_ltpc);
1251 return 0;
1253 module_init(ltpc_module_init);
1254 #endif
1256 static void __exit ltpc_cleanup(void)
1259 if(debug & DEBUG_VERBOSE) printk("unregister_netdev\n");
1260 unregister_netdev(dev_ltpc);
1262 ltpc_timer.data = 0; /* signal the poll routine that we're done */
1264 del_timer_sync(&ltpc_timer);
1266 if(debug & DEBUG_VERBOSE) printk("freeing irq\n");
1268 if (dev_ltpc->irq)
1269 free_irq(dev_ltpc->irq, dev_ltpc);
1271 if(debug & DEBUG_VERBOSE) printk("freeing dma\n");
1273 if (dev_ltpc->dma)
1274 free_dma(dev_ltpc->dma);
1276 if(debug & DEBUG_VERBOSE) printk("freeing ioaddr\n");
1278 if (dev_ltpc->base_addr)
1279 release_region(dev_ltpc->base_addr,8);
1281 free_netdev(dev_ltpc);
1283 if(debug & DEBUG_VERBOSE) printk("free_pages\n");
1285 free_pages( (unsigned long) ltdmabuf, get_order(1000));
1287 if(debug & DEBUG_VERBOSE) printk("returning from cleanup_module\n");
1290 module_exit(ltpc_cleanup);