setup enviroment for compilation
[linux-2.4.21-pre4.git] / drivers / net / de600.c
1 static const char version[] =
2         "de600.c: $Revision: 1.1.1.1 $,  Bjorn Ekwall (bj0rn@blox.se)\n";
3 /*
4  *      de600.c
5  *
6  *      Linux driver for the D-Link DE-600 Ethernet pocket adapter.
7  *
8  *      Portions (C) Copyright 1993, 1994 by Bjorn Ekwall
9  *      The Author may be reached as bj0rn@blox.se
10  *
11  *      Based on adapter information gathered from DE600.ASM by D-Link Inc.,
12  *      as included on disk C in the v.2.11 of PC/TCP from FTP Software.
13  *      For DE600.asm:
14  *              Portions (C) Copyright 1990 D-Link, Inc.
15  *              Copyright, 1988-1992, Russell Nelson, Crynwr Software
16  *
17  *      Adapted to the sample network driver core for linux,
18  *      written by: Donald Becker <becker@super.org>
19  *              (Now at <becker@scyld.com>)
20  *
21  *      compile-command:
22  *      "gcc -D__KERNEL__  -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer \
23  *       -m486 -c de600.c
24  *
25  **************************************************************/
26 /*
27  *      This program is free software; you can redistribute it and/or modify
28  *      it under the terms of the GNU General Public License as published by
29  *      the Free Software Foundation; either version 2, or (at your option)
30  *      any later version.
31  *
32  *      This program is distributed in the hope that it will be useful,
33  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
34  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35  *      GNU General Public License for more details.
36  *
37  *      You should have received a copy of the GNU General Public License
38  *      along with this program; if not, write to the Free Software
39  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
40  *
41  **************************************************************/
42 /* Add more time here if your adapter won't work OK: */
43 #define DE600_SLOW_DOWN udelay(delay_time)
44
45  /*
46  * If you still have trouble reading/writing to the adapter,
47  * modify the following "#define": (see <asm/io.h> for more info)
48 #define REALLY_SLOW_IO
49  */
50 #define SLOW_IO_BY_JUMPING /* Looks "better" than dummy write to port 0x80 :-) */
51
52 /*
53  * If you want to enable automatic continuous checking for the DE600,
54  * keep this #define enabled.
55  * It doesn't cost much per packet, so I think it is worth it!
56  * If you disagree, comment away the #define, and live with it...
57  *
58  */
59 #define CHECK_LOST_DE600
60
61 /*
62  * Enable this #define if you want the adapter to do a "ifconfig down" on
63  * itself when we have detected that something is possibly wrong with it.
64  * The default behaviour is to retry with "adapter_init()" until success.
65  * This should be used for debugging purposes only.
66  * (Depends on the CHECK_LOST_DE600 above)
67  *
68  */
69 #define SHUTDOWN_WHEN_LOST
70
71 /*
72  * See comment at "de600_rspace()"!
73  * This is an *ugly* hack, but for now it achieves its goal of
74  * faking a TCP flow-control that will not flood the poor DE600.
75  *
76  * Tricks TCP to announce a small max window (max 2 fast packets please :-)
77  *
78  * Comment away at your own risk!
79  *
80  * Update: Use the more general per-device maxwindow parameter instead.
81  */
82 #undef FAKE_SMALL_MAX
83
84 /* use 0 for production, 1 for verification, >2 for debug */
85 #ifdef DE600_DEBUG
86 #define PRINTK(x) if (de600_debug >= 2) printk x
87 #else
88 #define DE600_DEBUG 0
89 #define PRINTK(x) /**/
90 #endif
91 \f
92 #include <linux/module.h>
93
94 #include <linux/kernel.h>
95 #include <linux/sched.h>
96 #include <linux/types.h>
97 #include <linux/fcntl.h>
98 #include <linux/string.h>
99 #include <linux/interrupt.h>
100 #include <linux/ioport.h>
101 #include <asm/io.h>
102 #include <linux/in.h>
103 #include <linux/ptrace.h>
104 #include <asm/system.h>
105 #include <linux/errno.h>
106 #include <linux/init.h>
107 #include <linux/delay.h>
108
109 #include <linux/inet.h>
110 #include <linux/netdevice.h>
111 #include <linux/etherdevice.h>
112 #include <linux/skbuff.h>
113
114 static unsigned int de600_debug = DE600_DEBUG;
115 MODULE_PARM(de600_debug, "i");
116 MODULE_PARM_DESC(de600_debug, "DE-600 debug level (0-2)");
117
118 static unsigned int delay_time = 10;
119 MODULE_PARM(delay_time, "i");
120 MODULE_PARM_DESC(delay_time, "DE-600 deley on I/O in microseconds");
121
122 #ifdef FAKE_SMALL_MAX
123 static unsigned long de600_rspace(struct sock *sk);
124 #include <net/sock.h>
125 #endif
126
127 typedef unsigned char byte;
128
129 /**************************************************
130  *                                                *
131  * Definition of D-Link Ethernet Pocket adapter   *
132  *                                                *
133  **************************************************/
134 /*
135  * D-Link Ethernet pocket adapter ports
136  */
137 /*
138  * OK, so I'm cheating, but there are an awful lot of
139  * reads and writes in order to get anything in and out
140  * of the DE-600 with 4 bits at a time in the parallel port,
141  * so every saved instruction really helps :-)
142  *
143  * That is, I don't care what the device struct says
144  * but hope that Space.c will keep the rest of the drivers happy.
145  */
146 #ifndef DE600_IO
147 #define DE600_IO 0x378
148 #endif
149
150 #define DATA_PORT       (DE600_IO)
151 #define STATUS_PORT     (DE600_IO + 1)
152 #define COMMAND_PORT    (DE600_IO + 2)
153
154 #ifndef DE600_IRQ
155 #define DE600_IRQ       7
156 #endif
157 /*
158  * It really should look like this, and autoprobing as well...
159  *
160 #define DATA_PORT       (dev->base_addr + 0)
161 #define STATUS_PORT     (dev->base_addr + 1)
162 #define COMMAND_PORT    (dev->base_addr + 2)
163 #define DE600_IRQ       dev->irq
164  */
165
166 /*
167  * D-Link COMMAND_PORT commands
168  */
169 #define SELECT_NIC      0x04 /* select Network Interface Card */
170 #define SELECT_PRN      0x1c /* select Printer */
171 #define NML_PRN         0xec /* normal Printer situation */
172 #define IRQEN           0x10 /* enable IRQ line */
173
174 /*
175  * D-Link STATUS_PORT
176  */
177 #define RX_BUSY         0x80
178 #define RX_GOOD         0x40
179 #define TX_FAILED16     0x10
180 #define TX_BUSY         0x08
181
182 /*
183  * D-Link DATA_PORT commands
184  * command in low 4 bits
185  * data in high 4 bits
186  * select current data nibble with HI_NIBBLE bit
187  */
188 #define WRITE_DATA      0x00 /* write memory */
189 #define READ_DATA       0x01 /* read memory */
190 #define STATUS          0x02 /* read  status register */
191 #define COMMAND         0x03 /* write command register (see COMMAND below) */
192 #define NULL_COMMAND    0x04 /* null command */
193 #define RX_LEN          0x05 /* read  received packet length */
194 #define TX_ADDR         0x06 /* set adapter transmit memory address */
195 #define RW_ADDR         0x07 /* set adapter read/write memory address */
196 #define HI_NIBBLE       0x08 /* read/write the high nibble of data,
197                                 or-ed with rest of command */
198
199 /*
200  * command register, accessed through DATA_PORT with low bits = COMMAND
201  */
202 #define RX_ALL          0x01 /* PROMISCUOUS */
203 #define RX_BP           0x02 /* default: BROADCAST & PHYSICAL ADDRESS */
204 #define RX_MBP          0x03 /* MULTICAST, BROADCAST & PHYSICAL ADDRESS */
205
206 #define TX_ENABLE       0x04 /* bit 2 */
207 #define RX_ENABLE       0x08 /* bit 3 */
208
209 #define RESET           0x80 /* set bit 7 high */
210 #define STOP_RESET      0x00 /* set bit 7 low */
211
212 /*
213  * data to command register
214  * (high 4 bits in write to DATA_PORT)
215  */
216 #define RX_PAGE2_SELECT 0x10 /* bit 4, only 2 pages to select */
217 #define RX_BASE_PAGE    0x20 /* bit 5, always set when specifying RX_ADDR */
218 #define FLIP_IRQ        0x40 /* bit 6 */
219
220 /*
221  * D-Link adapter internal memory:
222  *
223  * 0-2K 1:st transmit page (send from pointer up to 2K)
224  * 2-4K 2:nd transmit page (send from pointer up to 4K)
225  *
226  * 4-6K 1:st receive page (data from 4K upwards)
227  * 6-8K 2:nd receive page (data from 6K upwards)
228  *
229  * 8K+  Adapter ROM (contains magic code and last 3 bytes of Ethernet address)
230  */
231 #define MEM_2K          0x0800 /* 2048 */
232 #define MEM_4K          0x1000 /* 4096 */
233 #define MEM_6K          0x1800 /* 6144 */
234 #define NODE_ADDRESS    0x2000 /* 8192 */
235
236 #define RUNT 60         /* Too small Ethernet packet */
237
238 /**************************************************
239  *                                                *
240  *             End of definition                  *
241  *                                                *
242  **************************************************/
243
244 /*
245  * Index to functions, as function prototypes.
246  */
247 /* Routines used internally. (See "convenience macros") */
248 static byte     de600_read_status(struct net_device *dev);
249 static byte     de600_read_byte(unsigned char type, struct net_device *dev);
250
251 /* Put in the device structure. */
252 static int      de600_open(struct net_device *dev);
253 static int      de600_close(struct net_device *dev);
254 static struct net_device_stats *get_stats(struct net_device *dev);
255 static int      de600_start_xmit(struct sk_buff *skb, struct net_device *dev);
256
257 /* Dispatch from interrupts. */
258 static void     de600_interrupt(int irq, void *dev_id, struct pt_regs *regs);
259 static int      de600_tx_intr(struct net_device *dev, int irq_status);
260 static void     de600_rx_intr(struct net_device *dev);
261
262 /* Initialization */
263 static void     trigger_interrupt(struct net_device *dev);
264 int             de600_probe(struct net_device *dev);
265 static int      adapter_init(struct net_device *dev);
266
267 /*
268  * D-Link driver variables:
269  */
270 static volatile int             rx_page;
271
272 #define TX_PAGES 2
273 static volatile int             tx_fifo[TX_PAGES];
274 static volatile int             tx_fifo_in;
275 static volatile int             tx_fifo_out;
276 static volatile int             free_tx_pages = TX_PAGES;
277 static int                      was_down;
278
279 /*
280  * Convenience macros/functions for D-Link adapter
281  */
282
283 #define select_prn() outb_p(SELECT_PRN, COMMAND_PORT); DE600_SLOW_DOWN
284 #define select_nic() outb_p(SELECT_NIC, COMMAND_PORT); DE600_SLOW_DOWN
285
286 /* Thanks for hints from Mark Burton <markb@ordern.demon.co.uk> */
287 #define de600_put_byte(data) ( \
288         outb_p(((data) << 4)   | WRITE_DATA            , DATA_PORT), \
289         outb_p(((data) & 0xf0) | WRITE_DATA | HI_NIBBLE, DATA_PORT))
290
291 /*
292  * The first two outb_p()'s below could perhaps be deleted if there
293  * would be more delay in the last two. Not certain about it yet...
294  */
295 #define de600_put_command(cmd) ( \
296         outb_p(( rx_page        << 4)   | COMMAND            , DATA_PORT), \
297         outb_p(( rx_page        & 0xf0) | COMMAND | HI_NIBBLE, DATA_PORT), \
298         outb_p(((rx_page | cmd) << 4)   | COMMAND            , DATA_PORT), \
299         outb_p(((rx_page | cmd) & 0xf0) | COMMAND | HI_NIBBLE, DATA_PORT))
300
301 #define de600_setup_address(addr,type) ( \
302         outb_p((((addr) << 4) & 0xf0) | type            , DATA_PORT), \
303         outb_p(( (addr)       & 0xf0) | type | HI_NIBBLE, DATA_PORT), \
304         outb_p((((addr) >> 4) & 0xf0) | type            , DATA_PORT), \
305         outb_p((((addr) >> 8) & 0xf0) | type | HI_NIBBLE, DATA_PORT))
306
307 #define rx_page_adr() ((rx_page & RX_PAGE2_SELECT)?(MEM_6K):(MEM_4K))
308
309 /* Flip bit, only 2 pages */
310 #define next_rx_page() (rx_page ^= RX_PAGE2_SELECT)
311
312 #define tx_page_adr(a) (((a) + 1) * MEM_2K)
313
314 static inline byte
315 de600_read_status(struct net_device *dev)
316 {
317         byte status;
318
319         outb_p(STATUS, DATA_PORT);
320         status = inb(STATUS_PORT);
321         outb_p(NULL_COMMAND | HI_NIBBLE, DATA_PORT);
322
323         return status;
324 }
325
326 static inline byte
327 de600_read_byte(unsigned char type, struct net_device *dev) { /* dev used by macros */
328         byte lo;
329
330         (void)outb_p((type), DATA_PORT);
331         lo = ((unsigned char)inb(STATUS_PORT)) >> 4;
332         (void)outb_p((type) | HI_NIBBLE, DATA_PORT);
333         return ((unsigned char)inb(STATUS_PORT) & (unsigned char)0xf0) | lo;
334 }
335 \f
336 /*
337  * Open/initialize the board.  This is called (in the current kernel)
338  * after booting when 'ifconfig <dev->name> $IP_ADDR' is run (in rc.inet1).
339  *
340  * This routine should set everything up anew at each open, even
341  * registers that "should" only need to be set once at boot, so that
342  * there is a non-reboot way to recover if something goes wrong.
343  */
344 static int
345 de600_open(struct net_device *dev)
346 {
347         int ret = request_irq(DE600_IRQ, de600_interrupt, 0, dev->name, dev);
348         if (ret) {
349                 printk ("%s: unable to get IRQ %d\n", dev->name, DE600_IRQ);
350                 return ret;
351         }
352
353         if (adapter_init(dev))
354                 return -EIO;
355
356         return 0;
357 }
358
359 /*
360  * The inverse routine to de600_open().
361  */
362 static int
363 de600_close(struct net_device *dev)
364 {
365         select_nic();
366         rx_page = 0;
367         de600_put_command(RESET);
368         de600_put_command(STOP_RESET);
369         de600_put_command(0);
370         select_prn();
371
372         if (netif_running(dev)) { /* perhaps not needed? */
373                 free_irq(DE600_IRQ, dev);
374         }
375         return 0;
376 }
377
378 static struct net_device_stats *
379 get_stats(struct net_device *dev)
380 {
381     return (struct net_device_stats *)(dev->priv);
382 }
383
384 static inline void
385 trigger_interrupt(struct net_device *dev)
386 {
387         de600_put_command(FLIP_IRQ);
388         select_prn();
389         DE600_SLOW_DOWN;
390         select_nic();
391         de600_put_command(0);
392 }
393
394 /*
395  * Copy a buffer to the adapter transmit page memory.
396  * Start sending.
397  */
398 static int
399 de600_start_xmit(struct sk_buff *skb, struct net_device *dev)
400 {
401         unsigned long flags;
402         int     transmit_from;
403         int     len;
404         int     tickssofar;
405         byte    *buffer = skb->data;
406         int     i;
407
408         if (free_tx_pages <= 0) {       /* Do timeouts, to avoid hangs. */
409                 tickssofar = jiffies - dev->trans_start;
410
411                 if (tickssofar < 5)
412                         return 1;
413
414                 /* else */
415                 printk("%s: transmit timed out (%d), %s?\n",
416                         dev->name,
417                         tickssofar,
418                         "network cable problem"
419                         );
420                 /* Restart the adapter. */
421                 if (adapter_init(dev)) {
422                         return 1;
423                 }
424         }
425
426         /* Start real output */
427         PRINTK(("de600_start_xmit:len=%d, page %d/%d\n", skb->len, tx_fifo_in, free_tx_pages));
428
429         if ((len = skb->len) < RUNT)
430                 len = RUNT;
431
432         save_flags(flags);
433         cli();
434         select_nic();
435         tx_fifo[tx_fifo_in] = transmit_from = tx_page_adr(tx_fifo_in) - len;
436         tx_fifo_in = (tx_fifo_in + 1) % TX_PAGES; /* Next free tx page */
437
438 #ifdef CHECK_LOST_DE600
439         /* This costs about 40 instructions per packet... */
440         de600_setup_address(NODE_ADDRESS, RW_ADDR);
441         de600_read_byte(READ_DATA, dev);
442         if (was_down || (de600_read_byte(READ_DATA, dev) != 0xde)) {
443                 if (adapter_init(dev)) {
444                         restore_flags(flags);
445                         return 1;
446                 }
447         }
448 #endif
449
450         de600_setup_address(transmit_from, RW_ADDR);
451         for (i = 0;  i < skb->len ; ++i, ++buffer)
452                 de600_put_byte(*buffer);
453         for (; i < len; ++i)
454                 de600_put_byte(0);
455
456         if (free_tx_pages-- == TX_PAGES) { /* No transmission going on */
457                 dev->trans_start = jiffies;
458                 netif_start_queue(dev); /* allow more packets into adapter */
459                 /* Send page and generate a faked interrupt */
460                 de600_setup_address(transmit_from, TX_ADDR);
461                 de600_put_command(TX_ENABLE);
462         }
463         else {
464                 if (free_tx_pages)
465                         netif_start_queue(dev);
466                 else
467                         netif_stop_queue(dev);
468                 select_prn();
469         }
470
471         restore_flags(flags);
472
473 #ifdef FAKE_SMALL_MAX
474         /* This will "patch" the socket TCP proto at an early moment */
475         if (skb->sk && (skb->sk->protocol == IPPROTO_TCP) &&
476                 (skb->sk->prot->rspace != &de600_rspace))
477                 skb->sk->prot->rspace = de600_rspace; /* Ugh! */
478 #endif
479
480         dev_kfree_skb (skb);
481
482         return 0;
483 }
484 \f
485 /*
486  * The typical workload of the driver:
487  * Handle the network interface interrupts.
488  */
489 static void
490 de600_interrupt(int irq, void *dev_id, struct pt_regs * regs)
491 {
492         struct net_device       *dev = dev_id;
493         byte            irq_status;
494         int             retrig = 0;
495         int             boguscount = 0;
496
497         /* This might just as well be deleted now, no crummy drivers present :-) */
498         if ((dev == NULL) || (DE600_IRQ != irq)) {
499                 printk("%s: bogus interrupt %d\n", dev?dev->name:"DE-600", irq);
500                 return;
501         }
502
503         select_nic();
504         irq_status = de600_read_status(dev);
505
506         do {
507                 PRINTK(("de600_interrupt (%02X)\n", irq_status));
508
509                 if (irq_status & RX_GOOD)
510                         de600_rx_intr(dev);
511                 else if (!(irq_status & RX_BUSY))
512                         de600_put_command(RX_ENABLE);
513
514                 /* Any transmission in progress? */
515                 if (free_tx_pages < TX_PAGES)
516                         retrig = de600_tx_intr(dev, irq_status);
517                 else
518                         retrig = 0;
519
520                 irq_status = de600_read_status(dev);
521         } while ( (irq_status & RX_GOOD) || ((++boguscount < 100) && retrig) );
522         /*
523          * Yeah, it _looks_ like busy waiting, smells like busy waiting
524          * and I know it's not PC, but please, it will only occur once
525          * in a while and then only for a loop or so (< 1ms for sure!)
526          */
527
528         /* Enable adapter interrupts */
529         select_prn();
530
531         if (retrig)
532                 trigger_interrupt(dev);
533
534         return;
535 }
536
537 static int
538 de600_tx_intr(struct net_device *dev, int irq_status)
539 {
540         /*
541          * Returns 1 if tx still not done
542          */
543
544         /* Check if current transmission is done yet */
545         if (irq_status & TX_BUSY)
546                 return 1; /* tx not done, try again */
547
548         /* else */
549         /* If last transmission OK then bump fifo index */
550         if (!(irq_status & TX_FAILED16)) {
551                 tx_fifo_out = (tx_fifo_out + 1) % TX_PAGES;
552                 ++free_tx_pages;
553                 ((struct net_device_stats *)(dev->priv))->tx_packets++;
554                 netif_wake_queue(dev);
555         }
556
557         /* More to send, or resend last packet? */
558         if ((free_tx_pages < TX_PAGES) || (irq_status & TX_FAILED16)) {
559                 dev->trans_start = jiffies;
560                 de600_setup_address(tx_fifo[tx_fifo_out], TX_ADDR);
561                 de600_put_command(TX_ENABLE);
562                 return 1;
563         }
564         /* else */
565
566         return 0;
567 }
568
569 /*
570  * We have a good packet, get it out of the adapter.
571  */
572 static void
573 de600_rx_intr(struct net_device *dev)
574 {
575         struct sk_buff  *skb;
576         unsigned long flags;
577         int             i;
578         int             read_from;
579         int             size;
580         register unsigned char  *buffer;
581
582         save_flags(flags);
583         cli();
584
585         /* Get size of received packet */
586         size = de600_read_byte(RX_LEN, dev);    /* low byte */
587         size += (de600_read_byte(RX_LEN, dev) << 8);    /* high byte */
588         size -= 4;      /* Ignore trailing 4 CRC-bytes */
589
590         /* Tell adapter where to store next incoming packet, enable receiver */
591         read_from = rx_page_adr();
592         next_rx_page();
593         de600_put_command(RX_ENABLE);
594
595         restore_flags(flags);
596
597         if ((size < 32)  ||  (size > 1535)) {
598                 printk("%s: Bogus packet size %d.\n", dev->name, size);
599                 if (size > 10000)
600                         adapter_init(dev);
601                 return;
602         }
603
604         skb = dev_alloc_skb(size+2);
605         if (skb == NULL) {
606                 printk("%s: Couldn't allocate a sk_buff of size %d.\n",
607                         dev->name, size);
608                 return;
609         }
610         /* else */
611
612         skb->dev = dev;
613         skb_reserve(skb,2);     /* Align */
614
615         /* 'skb->data' points to the start of sk_buff data area. */
616         buffer = skb_put(skb,size);
617
618         /* copy the packet into the buffer */
619         de600_setup_address(read_from, RW_ADDR);
620         for (i = size; i > 0; --i, ++buffer)
621                 *buffer = de600_read_byte(READ_DATA, dev);
622
623         skb->protocol=eth_type_trans(skb,dev);
624
625         netif_rx(skb);
626
627         /* update stats */
628         dev->last_rx = jiffies;
629         ((struct net_device_stats *)(dev->priv))->rx_packets++; /* count all receives */
630         ((struct net_device_stats *)(dev->priv))->rx_bytes += size; /* count all received bytes */
631
632         /*
633          * If any worth-while packets have been received, netif_rx()
634          * has done a mark_bh(INET_BH) for us and will work on them
635          * when we get to the bottom-half routine.
636          */
637 }
638
639 int __init 
640 de600_probe(struct net_device *dev)
641 {
642         int     i;
643         static struct net_device_stats de600_netstats;
644         /*dev->priv = kmalloc(sizeof(struct net_device_stats), GFP_KERNEL);*/
645
646         SET_MODULE_OWNER(dev);
647
648         printk("%s: D-Link DE-600 pocket adapter", dev->name);
649         /* Alpha testers must have the version number to report bugs. */
650         if (de600_debug > 1)
651                 printk(version);
652
653         /* probe for adapter */
654         rx_page = 0;
655         select_nic();
656         (void)de600_read_status(dev);
657         de600_put_command(RESET);
658         de600_put_command(STOP_RESET);
659         if (de600_read_status(dev) & 0xf0) {
660                 printk(": not at I/O %#3x.\n", DATA_PORT);
661                 return -ENODEV;
662         }
663
664         /*
665          * Maybe we found one,
666          * have to check if it is a D-Link DE-600 adapter...
667          */
668
669         /* Get the adapter ethernet address from the ROM */
670         de600_setup_address(NODE_ADDRESS, RW_ADDR);
671         for (i = 0; i < ETH_ALEN; i++) {
672                 dev->dev_addr[i] = de600_read_byte(READ_DATA, dev);
673                 dev->broadcast[i] = 0xff;
674         }
675
676         /* Check magic code */
677         if ((dev->dev_addr[1] == 0xde) && (dev->dev_addr[2] == 0x15)) {
678                 /* OK, install real address */
679                 dev->dev_addr[0] = 0x00;
680                 dev->dev_addr[1] = 0x80;
681                 dev->dev_addr[2] = 0xc8;
682                 dev->dev_addr[3] &= 0x0f;
683                 dev->dev_addr[3] |= 0x70;
684         } else {
685                 printk(" not identified in the printer port\n");
686                 return -ENODEV;
687         }
688
689 #if 0 /* Not yet */
690         if (check_region(DE600_IO, 3)) {
691                 printk(", port 0x%x busy\n", DE600_IO);
692                 return -EBUSY;
693         }
694 #endif
695         request_region(DE600_IO, 3, "de600");
696
697         printk(", Ethernet Address: %02X", dev->dev_addr[0]);
698         for (i = 1; i < ETH_ALEN; i++)
699                 printk(":%02X",dev->dev_addr[i]);
700         printk("\n");
701
702         /* Initialize the device structure. */
703         dev->priv = &de600_netstats;
704
705         memset(dev->priv, 0, sizeof(struct net_device_stats));
706         dev->get_stats = get_stats;
707
708         dev->open = de600_open;
709         dev->stop = de600_close;
710         dev->hard_start_xmit = &de600_start_xmit;
711
712         ether_setup(dev);
713
714         dev->flags&=~IFF_MULTICAST;
715
716         select_prn();
717         return 0;
718 }
719
720 static int
721 adapter_init(struct net_device *dev)
722 {
723         int     i;
724         unsigned long flags;
725
726         save_flags(flags);
727         cli();
728
729         select_nic();
730         rx_page = 0; /* used by RESET */
731         de600_put_command(RESET);
732         de600_put_command(STOP_RESET);
733 #ifdef CHECK_LOST_DE600
734         /* Check if it is still there... */
735         /* Get the some bytes of the adapter ethernet address from the ROM */
736         de600_setup_address(NODE_ADDRESS, RW_ADDR);
737         de600_read_byte(READ_DATA, dev);
738         if ((de600_read_byte(READ_DATA, dev) != 0xde) ||
739             (de600_read_byte(READ_DATA, dev) != 0x15)) {
740         /* was: if (de600_read_status(dev) & 0xf0) { */
741                 printk("Something has happened to the DE-600!  Please check it"
742 #ifdef SHUTDOWN_WHEN_LOST
743                         " and do a new ifconfig"
744 #endif /* SHUTDOWN_WHEN_LOST */
745                         "!\n");
746 #ifdef SHUTDOWN_WHEN_LOST
747                 /* Goodbye, cruel world... */
748                 dev->flags &= ~IFF_UP;
749                 de600_close(dev);
750 #endif /* SHUTDOWN_WHEN_LOST */
751                 was_down = 1;
752                 netif_stop_queue(dev); /* Transmit busy...  */
753                 restore_flags(flags);
754                 return 1; /* failed */
755         }
756 #endif /* CHECK_LOST_DE600 */
757         if (was_down) {
758                 printk("Thanks, I feel much better now!\n");
759                 was_down = 0;
760         }
761
762         netif_start_queue(dev);
763         tx_fifo_in = 0;
764         tx_fifo_out = 0;
765         free_tx_pages = TX_PAGES;
766
767         /* set the ether address. */
768         de600_setup_address(NODE_ADDRESS, RW_ADDR);
769         for (i = 0; i < ETH_ALEN; i++)
770                 de600_put_byte(dev->dev_addr[i]);
771
772         /* where to start saving incoming packets */
773         rx_page = RX_BP | RX_BASE_PAGE;
774         de600_setup_address(MEM_4K, RW_ADDR);
775         /* Enable receiver */
776         de600_put_command(RX_ENABLE);
777         select_prn();
778         restore_flags(flags);
779
780         return 0; /* OK */
781 }
782
783 #ifdef FAKE_SMALL_MAX
784 /*
785  *      The new router code (coming soon 8-) ) will fix this properly.
786  */
787 #define DE600_MIN_WINDOW 1024
788 #define DE600_MAX_WINDOW 2048
789 #define DE600_TCP_WINDOW_DIFF 1024
790 /*
791  * Copied from "net/inet/sock.c"
792  *
793  * Sets a lower max receive window in order to achieve <= 2
794  * packets arriving at the adapter in fast succession.
795  * (No way that a DE-600 can keep up with a net saturated
796  *  with packets homing in on it :-( )
797  *
798  * Since there are only 2 receive buffers in the DE-600
799  * and it takes some time to copy from the adapter,
800  * this is absolutely necessary for any TCP performance whatsoever!
801  *
802  * Note that the returned window info will never be smaller than
803  * DE600_MIN_WINDOW, i.e. 1024
804  * This differs from the standard function, that can return an
805  * arbitrarily small window!
806  */
807 static unsigned long
808 de600_rspace(struct sock *sk)
809 {
810   int amt;
811
812   if (sk != NULL) {
813 /*
814  * Hack! You might want to play with commenting away the following line,
815  * if you know what you do!
816         sk->max_unacked = DE600_MAX_WINDOW - DE600_TCP_WINDOW_DIFF;
817  */
818
819         if (atomic_read(&sk->rmem_alloc) >= sk->rcvbuf-2*DE600_MIN_WINDOW) return(0);
820         amt = min_t(int, (sk->rcvbuf-atomic_read(&sk->rmem_alloc))/2/*-DE600_MIN_WINDOW*/, DE600_MAX_WINDOW);
821         if (amt < 0) return(0);
822         return(amt);
823   }
824   return(0);
825 }
826 #endif
827 \f
828 #ifdef MODULE
829 static struct net_device de600_dev;
830
831 int
832 init_module(void)
833 {
834         de600_dev.init = de600_probe;
835         if (register_netdev(&de600_dev) != 0)
836                 return -EIO;
837         return 0;
838 }
839
840 void
841 cleanup_module(void)
842 {
843         unregister_netdev(&de600_dev);
844         release_region(DE600_IO, 3);
845 }
846 #endif /* MODULE */
847
848 MODULE_LICENSE("GPL");
849
850 /*
851  * Local variables:
852  *  kernel-compile-command: "gcc -D__KERNEL__ -Ilinux/include -I../../net/inet -Wall -Wstrict-prototypes -O2 -m486 -c de600.c"
853  *  module-compile-command: "gcc -D__KERNEL__ -DMODULE -Ilinux/include -I../../net/inet -Wall -Wstrict-prototypes -O2 -m486 -c de600.c"
854  *  compile-command: "gcc -D__KERNEL__ -DMODULE -Ilinux/include -I../../net/inet -Wall -Wstrict-prototypes -O2 -m486 -c de600.c"
855  * End:
856  */