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