Pull trivial1 into release branch
[linux-2.6] / drivers / net / wireless / arlan-main.c
1 /*
2  *  Copyright (C) 1997 Cullen Jennings
3  *  Copyright (C) 1998 Elmer Joandiu, elmer@ylenurme.ee
4  *  GNU General Public License applies
5  * This module provides support for the Arlan 655 card made by Aironet
6  */
7
8 #include <linux/config.h>
9 #include "arlan.h"
10
11 #if BITS_PER_LONG != 32
12 #  error FIXME: this driver requires a 32-bit platform
13 #endif
14
15 static const char *arlan_version = "C.Jennigs 97 & Elmer.Joandi@ut.ee  Oct'98, http://www.ylenurme.ee/~elmer/655/";
16
17 struct net_device *arlan_device[MAX_ARLANS];
18
19 static int SID = SIDUNKNOWN;
20 static int radioNodeId = radioNodeIdUNKNOWN;
21 static char encryptionKey[12] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
22 int arlan_debug = debugUNKNOWN;
23 static int spreadingCode = spreadingCodeUNKNOWN;
24 static int channelNumber = channelNumberUNKNOWN;
25 static int channelSet = channelSetUNKNOWN;
26 static int systemId = systemIdUNKNOWN;
27 static int registrationMode = registrationModeUNKNOWN;
28 static int keyStart;
29 static int tx_delay_ms;
30 static int retries = 5;
31 static int tx_queue_len = 1;
32 static int arlan_EEPROM_bad;
33
34 #ifdef ARLAN_DEBUGGING
35
36 static int testMemory = testMemoryUNKNOWN;
37 static int irq = irqUNKNOWN;
38 static int txScrambled = 1;
39 static int mdebug;
40
41 module_param(irq, int, 0);
42 module_param(mdebug, int, 0);
43 module_param(testMemory, int, 0);
44 module_param(txScrambled, int, 0);
45 MODULE_PARM_DESC(irq, "(unused)");
46 MODULE_PARM_DESC(testMemory, "(unused)");
47 MODULE_PARM_DESC(mdebug, "Arlan multicast debugging (0-1)");
48 #endif
49
50 module_param_named(debug, arlan_debug, int, 0);
51 module_param(spreadingCode, int, 0);
52 module_param(channelNumber, int, 0);
53 module_param(channelSet, int, 0);
54 module_param(systemId, int, 0);
55 module_param(registrationMode, int, 0);
56 module_param(radioNodeId, int, 0);
57 module_param(SID, int, 0);
58 module_param(keyStart, int, 0);
59 module_param(tx_delay_ms, int, 0);
60 module_param(retries, int, 0);
61 module_param(tx_queue_len, int, 0);
62 module_param_named(EEPROM_bad, arlan_EEPROM_bad, int, 0);
63 MODULE_PARM_DESC(debug, "Arlan debug enable (0-1)");
64 MODULE_PARM_DESC(retries, "Arlan maximum packet retransmisions");
65 #ifdef ARLAN_ENTRY_EXIT_DEBUGGING
66 static int arlan_entry_debug;
67 static int arlan_exit_debug;
68 static int arlan_entry_and_exit_debug;
69 module_param_named(entry_debug, arlan_entry_debug, int, 0);
70 module_param_named(exit_debug, arlan_exit_debug, int, 0);
71 module_param_named(entry_and_exit_debug, arlan_entry_and_exit_debug, int, 0);
72 MODULE_PARM_DESC(entry_debug, "Arlan driver function entry debugging");
73 MODULE_PARM_DESC(exit_debug, "Arlan driver function exit debugging");
74 MODULE_PARM_DESC(entry_and_exit_debug, "Arlan driver function entry and exit debugging");
75 #endif
76
77 struct arlan_conf_stru arlan_conf[MAX_ARLANS];
78 static int arlans_found;
79
80 static  int     arlan_open(struct net_device *dev);
81 static  int     arlan_tx(struct sk_buff *skb, struct net_device *dev);
82 static  irqreturn_t arlan_interrupt(int irq, void *dev_id, struct pt_regs *regs);
83 static  int     arlan_close(struct net_device *dev);
84 static  struct net_device_stats *
85                 arlan_statistics                (struct net_device *dev);
86 static  void    arlan_set_multicast             (struct net_device *dev);
87 static  int     arlan_hw_tx                     (struct net_device* dev, char *buf, int length );
88 static  int     arlan_hw_config                 (struct net_device * dev);
89 static  void    arlan_tx_done_interrupt         (struct net_device * dev, int status);
90 static  void    arlan_rx_interrupt              (struct net_device * dev, u_char rxStatus, u_short, u_short);
91 static  void    arlan_process_interrupt         (struct net_device * dev);
92 static  void    arlan_tx_timeout                (struct net_device *dev);
93
94 static inline long us2ticks(int us)
95 {
96         return us * (1000000 / HZ);
97 }
98
99
100 #ifdef ARLAN_ENTRY_EXIT_DEBUGGING
101 #define ARLAN_DEBUG_ENTRY(name) \
102         {\
103         struct timeval timev;\
104         do_gettimeofday(&timev);\
105                 if (arlan_entry_debug || arlan_entry_and_exit_debug)\
106                         printk("--->>>" name " %ld " "\n",((long int) timev.tv_sec * 1000000 + timev.tv_usec));\
107         }
108 #define ARLAN_DEBUG_EXIT(name) \
109         {\
110         struct timeval timev;\
111         do_gettimeofday(&timev);\
112                 if (arlan_exit_debug || arlan_entry_and_exit_debug)\
113                         printk("<<<---" name " %ld " "\n",((long int) timev.tv_sec * 1000000 + timev.tv_usec) );\
114         }
115 #else
116 #define ARLAN_DEBUG_ENTRY(name)
117 #define ARLAN_DEBUG_EXIT(name)
118 #endif
119
120
121 #define arlan_interrupt_ack(dev)\
122         clearClearInterrupt(dev);\
123         setClearInterrupt(dev);
124
125 static inline int arlan_drop_tx(struct net_device *dev)
126 {
127         struct arlan_private *priv = netdev_priv(dev);
128
129         priv->stats.tx_errors++;
130         if (priv->Conf->tx_delay_ms)
131         {
132                 priv->tx_done_delayed = jiffies + priv->Conf->tx_delay_ms * HZ / 1000 + 1;
133         }
134         else
135         {
136                 priv->waiting_command_mask &= ~ARLAN_COMMAND_TX;
137                 TXHEAD(dev).offset = 0;
138                 TXTAIL(dev).offset = 0;
139                 priv->txLast = 0;
140                 priv->bad = 0;
141                 if (!priv->under_reset && !priv->under_config)
142                         netif_wake_queue (dev);
143         }
144         return 1;
145 }
146
147
148 int arlan_command(struct net_device *dev, int command_p)
149 {
150         struct arlan_private *priv = netdev_priv(dev);
151         volatile struct arlan_shmem __iomem *arlan = priv->card;
152         struct arlan_conf_stru *conf = priv->Conf;
153         int udelayed = 0;
154         int i = 0;
155         unsigned long flags;
156
157         ARLAN_DEBUG_ENTRY("arlan_command");
158
159         if (priv->card_polling_interval)
160                 priv->card_polling_interval = 1;
161
162         if (arlan_debug & ARLAN_DEBUG_CHAIN_LOCKS)
163                 printk(KERN_DEBUG "arlan_command, %lx commandByte %x waiting %lx incoming %x \n",
164                 jiffies, READSHMB(arlan->commandByte),
165                        priv->waiting_command_mask, command_p);
166
167         priv->waiting_command_mask |= command_p;
168
169         if (priv->waiting_command_mask & ARLAN_COMMAND_RESET)
170                 if (time_after(jiffies, priv->lastReset + 5 * HZ))
171                         priv->waiting_command_mask &= ~ARLAN_COMMAND_RESET;
172
173         if (priv->waiting_command_mask & ARLAN_COMMAND_INT_ACK)
174         {
175                 arlan_interrupt_ack(dev);
176                 priv->waiting_command_mask &= ~ARLAN_COMMAND_INT_ACK;
177         }
178         if (priv->waiting_command_mask & ARLAN_COMMAND_INT_ENABLE)
179         {
180                 setInterruptEnable(dev);
181                 priv->waiting_command_mask &= ~ARLAN_COMMAND_INT_ENABLE;
182         }
183
184         /* Card access serializing lock */
185         spin_lock_irqsave(&priv->lock, flags);
186
187         /* Check cards status and waiting */
188
189         if (priv->waiting_command_mask & (ARLAN_COMMAND_LONG_WAIT_NOW | ARLAN_COMMAND_WAIT_NOW))
190         {
191                 while (priv->waiting_command_mask & (ARLAN_COMMAND_LONG_WAIT_NOW | ARLAN_COMMAND_WAIT_NOW))
192                 {
193                         if (READSHMB(arlan->resetFlag) ||
194                                 READSHMB(arlan->commandByte))   /* || 
195                                                                    (readControlRegister(dev) & ARLAN_ACCESS))
196                                                                  */
197                                 udelay(40);
198                         else
199                                 priv->waiting_command_mask &= ~(ARLAN_COMMAND_LONG_WAIT_NOW | ARLAN_COMMAND_WAIT_NOW);
200
201                         udelayed++;
202
203                         if (priv->waiting_command_mask & ARLAN_COMMAND_LONG_WAIT_NOW)
204                         {
205                                 if (udelayed * 40 > 1000000)
206                                 {
207                                         printk(KERN_ERR "%s long wait too long \n", dev->name);
208                                         priv->waiting_command_mask |= ARLAN_COMMAND_RESET;
209                                         break;
210                                 }
211                         }
212                         else if (priv->waiting_command_mask & ARLAN_COMMAND_WAIT_NOW)
213                         {
214                                 if (udelayed * 40 > 1000)
215                                 {
216                                         printk(KERN_ERR "%s short wait too long \n", dev->name);
217                                         goto bad_end;
218                                 }
219                         }
220                 }
221         }
222         else
223         {
224                 i = 0;
225                 while ((READSHMB(arlan->resetFlag) ||
226                         READSHMB(arlan->commandByte)) &&
227                         conf->pre_Command_Wait > (i++) * 10)
228                         udelay(10);
229
230
231                 if ((READSHMB(arlan->resetFlag) ||
232                         READSHMB(arlan->commandByte)) &&
233                         !(priv->waiting_command_mask & ARLAN_COMMAND_RESET))
234                 {
235                         goto card_busy_end;
236                 }
237         }
238         if (priv->waiting_command_mask & ARLAN_COMMAND_RESET)
239                 priv->under_reset = 1;
240         if (priv->waiting_command_mask & ARLAN_COMMAND_CONF)
241                 priv->under_config = 1;
242
243         /* Issuing command */
244         arlan_lock_card_access(dev);
245         if (priv->waiting_command_mask & ARLAN_COMMAND_POWERUP)
246         {
247         //     if (readControlRegister(dev) & (ARLAN_ACCESS && ARLAN_POWER))
248                 setPowerOn(dev);
249                 arlan_interrupt_lancpu(dev);
250                 priv->waiting_command_mask &= ~ARLAN_COMMAND_POWERUP;
251                 priv->waiting_command_mask |= ARLAN_COMMAND_RESET;
252                 priv->card_polling_interval = HZ / 10;
253         }
254         else if (priv->waiting_command_mask & ARLAN_COMMAND_ACTIVATE)
255         {
256                 WRITESHMB(arlan->commandByte, ARLAN_COM_ACTIVATE);
257                 arlan_interrupt_lancpu(dev);
258                 priv->waiting_command_mask &= ~ARLAN_COMMAND_ACTIVATE;
259                 priv->card_polling_interval = HZ / 10;
260         }
261         else if (priv->waiting_command_mask & ARLAN_COMMAND_RX_ABORT)
262         {
263                 if (priv->rx_command_given)
264                 {
265                         WRITESHMB(arlan->commandByte, ARLAN_COM_RX_ABORT);
266                         arlan_interrupt_lancpu(dev);
267                         priv->rx_command_given = 0;
268                 }
269                 priv->waiting_command_mask &= ~ARLAN_COMMAND_RX_ABORT;
270                 priv->card_polling_interval = 1;
271         }
272         else if (priv->waiting_command_mask & ARLAN_COMMAND_TX_ABORT)
273         {
274                 if (priv->tx_command_given)
275                 {
276                         WRITESHMB(arlan->commandByte, ARLAN_COM_TX_ABORT);
277                         arlan_interrupt_lancpu(dev);
278                         priv->tx_command_given = 0;
279                 }
280                 priv->waiting_command_mask &= ~ARLAN_COMMAND_TX_ABORT;
281                 priv->card_polling_interval = 1;
282         }
283         else if (priv->waiting_command_mask & ARLAN_COMMAND_RESET)
284         {
285                 priv->under_reset=1;
286                 netif_stop_queue (dev);
287
288                 arlan_drop_tx(dev);
289                 if (priv->tx_command_given || priv->rx_command_given)
290                 {
291                         printk(KERN_ERR "%s: Reset under tx or rx command \n", dev->name);
292                 }
293                 netif_stop_queue (dev);
294                 if (arlan_debug & ARLAN_DEBUG_RESET)
295                         printk(KERN_ERR "%s: Doing chip reset\n", dev->name);
296                 priv->lastReset = jiffies;
297                 WRITESHM(arlan->commandByte, 0, u_char);
298                 /* hold card in reset state */
299                 setHardwareReset(dev);
300                 /* set reset flag and then release reset */
301                 WRITESHM(arlan->resetFlag, 0xff, u_char);
302                 clearChannelAttention(dev);
303                 clearHardwareReset(dev);
304                 priv->card_polling_interval = HZ / 4;
305                 priv->waiting_command_mask &= ~ARLAN_COMMAND_RESET;
306                 priv->waiting_command_mask |= ARLAN_COMMAND_INT_RACK;
307 //              priv->waiting_command_mask |= ARLAN_COMMAND_INT_RENABLE; 
308 //              priv->waiting_command_mask |= ARLAN_COMMAND_RX;
309         }
310         else if (priv->waiting_command_mask & ARLAN_COMMAND_INT_RACK)
311         {
312                 clearHardwareReset(dev);
313                 clearClearInterrupt(dev);
314                 setClearInterrupt(dev);
315                 setInterruptEnable(dev);
316                 priv->waiting_command_mask &= ~ARLAN_COMMAND_INT_RACK;
317                 priv->waiting_command_mask |= ARLAN_COMMAND_CONF;
318                 priv->under_config = 1;
319                 priv->under_reset = 0;
320         }
321         else if (priv->waiting_command_mask & ARLAN_COMMAND_INT_RENABLE)
322         {
323                 setInterruptEnable(dev);
324                 priv->waiting_command_mask &= ~ARLAN_COMMAND_INT_RENABLE;
325         }
326         else if (priv->waiting_command_mask & ARLAN_COMMAND_CONF)
327         {
328                 if (priv->tx_command_given || priv->rx_command_given)
329                 {
330                         printk(KERN_ERR "%s: Reset under tx or rx command \n", dev->name);
331                 }
332                 arlan_drop_tx(dev);
333                 setInterruptEnable(dev);
334                 arlan_hw_config(dev);
335                 arlan_interrupt_lancpu(dev);
336                 priv->waiting_command_mask &= ~ARLAN_COMMAND_CONF;
337                 priv->card_polling_interval = HZ / 10;
338 //              priv->waiting_command_mask |= ARLAN_COMMAND_INT_RACK;   
339 //              priv->waiting_command_mask |= ARLAN_COMMAND_INT_ENABLE; 
340                 priv->waiting_command_mask |= ARLAN_COMMAND_CONF_WAIT;
341         }
342         else if (priv->waiting_command_mask & ARLAN_COMMAND_CONF_WAIT)
343         {
344                 if (READSHMB(arlan->configuredStatusFlag) != 0 &&
345                         READSHMB(arlan->diagnosticInfo) == 0xff)
346                 {
347                         priv->waiting_command_mask &= ~ARLAN_COMMAND_CONF_WAIT;
348                         priv->waiting_command_mask |= ARLAN_COMMAND_RX;
349                         priv->waiting_command_mask |= ARLAN_COMMAND_TBUSY_CLEAR;
350                         priv->card_polling_interval = HZ / 10;
351                         priv->tx_command_given = 0;
352                         priv->under_config = 0;
353                 }
354                 else
355                 {
356                         priv->card_polling_interval = 1;
357                         if (arlan_debug & ARLAN_DEBUG_TIMING)
358                                 printk(KERN_ERR "configure delayed \n");
359                 }
360         }
361         else if (priv->waiting_command_mask & ARLAN_COMMAND_RX)
362         {
363                 if (!registrationBad(dev))
364                 {
365                         setInterruptEnable(dev);
366                         memset_io(arlan->commandParameter, 0, 0xf);
367                         WRITESHMB(arlan->commandByte, ARLAN_COM_INT | ARLAN_COM_RX_ENABLE);
368                         WRITESHMB(arlan->commandParameter[0], conf->rxParameter);
369                         arlan_interrupt_lancpu(dev);
370                         priv->rx_command_given = 0; // mnjah, bad
371                         priv->waiting_command_mask &= ~ARLAN_COMMAND_RX;
372                         priv->card_polling_interval = 1;
373                 }
374                 else
375                         priv->card_polling_interval = 2;
376         }
377         else if (priv->waiting_command_mask & ARLAN_COMMAND_TBUSY_CLEAR)
378         {
379                 if ( !registrationBad(dev) &&
380                      (netif_queue_stopped(dev) || !netif_running(dev)) )
381                         {
382                                 priv->waiting_command_mask &= ~ARLAN_COMMAND_TBUSY_CLEAR;
383                                 netif_wake_queue (dev);
384                         }
385         }
386         else if (priv->waiting_command_mask & ARLAN_COMMAND_TX)
387         {
388                 if (!test_and_set_bit(0, (void *) &priv->tx_command_given))
389                 {
390                         if (time_after(jiffies, 
391                                        priv->tx_last_sent + us2ticks(conf->rx_tweak1))
392                             || time_before(jiffies,
393                                            priv->last_rx_int_ack_time + us2ticks(conf->rx_tweak2)))
394                         {
395                                 setInterruptEnable(dev);
396                                 memset_io(arlan->commandParameter, 0, 0xf);
397                                 WRITESHMB(arlan->commandByte, ARLAN_COM_TX_ENABLE | ARLAN_COM_INT);
398                                 memcpy_toio(arlan->commandParameter, &TXLAST(dev), 14);
399 //                              for ( i=1 ; i < 15 ; i++) printk("%02x:",READSHMB(arlan->commandParameter[i]));
400                                 priv->tx_last_sent = jiffies;
401                                 arlan_interrupt_lancpu(dev);
402                                 priv->tx_command_given = 1;
403                                 priv->waiting_command_mask &= ~ARLAN_COMMAND_TX;
404                                 priv->card_polling_interval = 1;
405                         }
406                         else
407                         {
408                                 priv->tx_command_given = 0;
409                                 priv->card_polling_interval = 1;
410                         }
411                 } 
412                 else if (arlan_debug & ARLAN_DEBUG_CHAIN_LOCKS)
413                         printk(KERN_ERR "tx command when tx chain locked \n");
414         }
415         else if (priv->waiting_command_mask & ARLAN_COMMAND_NOOPINT)
416         {
417                 {
418                         WRITESHMB(arlan->commandByte, ARLAN_COM_NOP | ARLAN_COM_INT);
419                 }
420                 arlan_interrupt_lancpu(dev);
421                 priv->waiting_command_mask &= ~ARLAN_COMMAND_NOOPINT;
422                 priv->card_polling_interval = HZ / 3;
423         }
424         else if (priv->waiting_command_mask & ARLAN_COMMAND_NOOP)
425         {
426                 WRITESHMB(arlan->commandByte, ARLAN_COM_NOP);
427                 arlan_interrupt_lancpu(dev);
428                 priv->waiting_command_mask &= ~ARLAN_COMMAND_NOOP;
429                 priv->card_polling_interval = HZ / 3;
430         }
431         else if (priv->waiting_command_mask & ARLAN_COMMAND_SLOW_POLL)
432         {
433                 WRITESHMB(arlan->commandByte, ARLAN_COM_GOTO_SLOW_POLL);
434                 arlan_interrupt_lancpu(dev);
435                 priv->waiting_command_mask &= ~ARLAN_COMMAND_SLOW_POLL;
436                 priv->card_polling_interval = HZ / 3;
437         } 
438         else if (priv->waiting_command_mask & ARLAN_COMMAND_POWERDOWN)
439         {
440                 setPowerOff(dev);
441                 if (arlan_debug & ARLAN_DEBUG_CARD_STATE)
442                         printk(KERN_WARNING "%s: Arlan Going Standby\n", dev->name);
443                 priv->waiting_command_mask &= ~ARLAN_COMMAND_POWERDOWN;
444                 priv->card_polling_interval = 3 * HZ;
445         }
446         arlan_unlock_card_access(dev);
447         for (i = 0; READSHMB(arlan->commandByte) && i < 20; i++)
448                 udelay(10);
449         if (READSHMB(arlan->commandByte))
450                 if (arlan_debug & ARLAN_DEBUG_CARD_STATE)
451                         printk(KERN_ERR "card busy leaving command %lx\n", priv->waiting_command_mask);
452
453         spin_unlock_irqrestore(&priv->lock, flags);
454         ARLAN_DEBUG_EXIT("arlan_command");
455         priv->last_command_buff_free_time = jiffies;
456         return 0;
457
458 card_busy_end:
459         if (time_after(jiffies, priv->last_command_buff_free_time + HZ))
460                 priv->waiting_command_mask |= ARLAN_COMMAND_CLEAN_AND_RESET;
461
462         if (arlan_debug & ARLAN_DEBUG_CARD_STATE)
463                 printk(KERN_ERR "%s arlan_command card busy end \n", dev->name);
464         spin_unlock_irqrestore(&priv->lock, flags);
465         ARLAN_DEBUG_EXIT("arlan_command");
466         return 1;
467
468 bad_end:
469         printk(KERN_ERR "%s arlan_command bad end \n", dev->name);
470
471         spin_unlock_irqrestore(&priv->lock, flags);
472         ARLAN_DEBUG_EXIT("arlan_command");
473
474         return -1;
475 }
476
477 static inline void arlan_command_process(struct net_device *dev)
478 {
479         struct arlan_private *priv = netdev_priv(dev);
480
481         int times = 0;
482         while (priv->waiting_command_mask && times < 8)
483         {
484                 if (priv->waiting_command_mask)
485                 {
486                         if (arlan_command(dev, 0))
487                                 break;
488                         times++;
489                 }
490                 /* if long command, we won't repeat trying */ ;
491                 if (priv->card_polling_interval > 1)
492                         break;
493                 times++;
494         }
495 }
496
497
498 static inline void arlan_retransmit_now(struct net_device *dev)
499 {
500         struct arlan_private *priv = netdev_priv(dev);
501
502
503         ARLAN_DEBUG_ENTRY("arlan_retransmit_now");
504         if (TXLAST(dev).offset == 0)
505         {
506                 if (TXHEAD(dev).offset)
507                 {
508                         priv->txLast = 0;
509                         IFDEBUG(ARLAN_DEBUG_TX_CHAIN) printk(KERN_DEBUG "TX buff switch to head \n");
510
511                 }
512                 else if (TXTAIL(dev).offset)
513                 {
514                         IFDEBUG(ARLAN_DEBUG_TX_CHAIN) printk(KERN_DEBUG "TX buff switch to tail \n");
515                         priv->txLast = 1;
516                 }
517                 else
518                         IFDEBUG(ARLAN_DEBUG_TX_CHAIN) printk(KERN_ERR "ReTransmit buff empty");
519                 netif_wake_queue (dev);
520                 return;
521
522         }
523         arlan_command(dev, ARLAN_COMMAND_TX);
524
525         priv->Conf->driverRetransmissions++;
526         priv->retransmissions++;
527
528         IFDEBUG(ARLAN_DEBUG_TX_CHAIN) printk("Retransmit %d bytes \n", TXLAST(dev).length);
529
530         ARLAN_DEBUG_EXIT("arlan_retransmit_now");
531 }
532
533
534
535 static void arlan_registration_timer(unsigned long data)
536 {
537         struct net_device *dev = (struct net_device *) data;
538         struct arlan_private *priv = netdev_priv(dev);
539         int bh_mark_needed = 0;
540         int next_tick = 1;
541         long lostTime = ((long)jiffies - (long)priv->registrationLastSeen)
542                         * (1000/HZ);
543
544         if (registrationBad(dev))
545         {
546                 priv->registrationLostCount++;
547                 if (lostTime > 7000 && lostTime < 7200)
548                 {
549                         printk(KERN_NOTICE "%s registration Lost \n", dev->name);
550                 }
551                 if (lostTime / priv->reRegisterExp > 2000)
552                         arlan_command(dev, ARLAN_COMMAND_CLEAN_AND_CONF);
553                 if (lostTime / (priv->reRegisterExp) > 3500)
554                         arlan_command(dev, ARLAN_COMMAND_CLEAN_AND_RESET);
555                 if (priv->reRegisterExp < 400)
556                         priv->reRegisterExp += 2;
557                 if (lostTime > 7200)
558                 {
559                         next_tick = HZ;
560                         arlan_command(dev, ARLAN_COMMAND_CLEAN_AND_RESET);
561                 }
562         }
563         else
564         {
565                 if (priv->Conf->registrationMode && lostTime > 10000 &&
566                         priv->registrationLostCount)
567                 {
568                         printk(KERN_NOTICE "%s registration is back after %ld milliseconds\n",
569                                dev->name, lostTime);
570                 }
571                 priv->registrationLastSeen = jiffies;
572                 priv->registrationLostCount = 0;
573                 priv->reRegisterExp = 1;
574                 if (!netif_running(dev) )
575                         netif_wake_queue(dev);
576                 if (time_after(priv->tx_last_sent,priv->tx_last_cleared) &&
577                     time_after(jiffies, priv->tx_last_sent * 5*HZ) ){
578                         arlan_command(dev, ARLAN_COMMAND_CLEAN_AND_RESET);              
579                         priv->tx_last_cleared = jiffies;
580                 }
581         }
582
583
584         if (!registrationBad(dev) && priv->ReTransmitRequested)
585         {
586                 IFDEBUG(ARLAN_DEBUG_TX_CHAIN)
587                         printk(KERN_ERR "Retransmit from timer \n");
588                 priv->ReTransmitRequested = 0;
589                 arlan_retransmit_now(dev);
590         }
591         if (!registrationBad(dev) &&
592                 time_after(jiffies, priv->tx_done_delayed) &&
593                 priv->tx_done_delayed != 0)
594         {
595                 TXLAST(dev).offset = 0;
596                 if (priv->txLast)
597                         priv->txLast = 0;
598                 else if (TXTAIL(dev).offset)
599                         priv->txLast = 1;
600                 if (TXLAST(dev).offset)
601                 {
602                         arlan_retransmit_now(dev);
603                         dev->trans_start = jiffies;
604                 }
605                 if (!(TXHEAD(dev).offset && TXTAIL(dev).offset))
606                 {
607                         netif_wake_queue (dev);
608                 }
609                 priv->tx_done_delayed = 0;
610                 bh_mark_needed = 1;
611         }
612         if (bh_mark_needed)
613         {
614                 netif_wake_queue (dev);
615         }
616         arlan_process_interrupt(dev);
617
618         if (next_tick < priv->card_polling_interval)
619                 next_tick = priv->card_polling_interval;
620
621         priv->timer.expires = jiffies + next_tick;
622
623         add_timer(&priv->timer);
624 }
625
626
627 #ifdef ARLAN_DEBUGGING
628
629 static void arlan_print_registers(struct net_device *dev, int line)
630 {
631         struct arlan_private *priv = netdev_priv(dev);
632         volatile struct arlan_shmem *arlan = priv->card;
633
634         u_char hostcpuLock, lancpuLock, controlRegister, cntrlRegImage,
635                 txStatus, rxStatus, interruptInProgress, commandByte;
636
637
638         ARLAN_DEBUG_ENTRY("arlan_print_registers");
639         READSHM(interruptInProgress, arlan->interruptInProgress, u_char);
640         READSHM(hostcpuLock, arlan->hostcpuLock, u_char);
641         READSHM(lancpuLock, arlan->lancpuLock, u_char);
642         READSHM(controlRegister, arlan->controlRegister, u_char);
643         READSHM(cntrlRegImage, arlan->cntrlRegImage, u_char);
644         READSHM(txStatus, arlan->txStatus, u_char);
645         READSHM(rxStatus, arlan->rxStatus, u_char);
646         READSHM(commandByte, arlan->commandByte, u_char);
647
648         printk(KERN_WARNING "line %04d IP %02x HL %02x LL %02x CB %02x CR %02x CRI %02x TX %02x RX %02x\n",
649                 line, interruptInProgress, hostcpuLock, lancpuLock, commandByte,
650                 controlRegister, cntrlRegImage, txStatus, rxStatus);
651
652         ARLAN_DEBUG_EXIT("arlan_print_registers");
653 }
654 #endif
655
656
657 static int arlan_hw_tx(struct net_device *dev, char *buf, int length)
658 {
659         int i;
660
661         struct arlan_private *priv = netdev_priv(dev);
662         volatile struct arlan_shmem __iomem *arlan = priv->card;
663         struct arlan_conf_stru *conf = priv->Conf;
664
665         int tailStarts = 0x800;
666         int headEnds = 0x0;
667
668
669         ARLAN_DEBUG_ENTRY("arlan_hw_tx");
670         if (TXHEAD(dev).offset)
671                 headEnds = (((TXHEAD(dev).offset + TXHEAD(dev).length - offsetof(struct arlan_shmem, txBuffer)) / 64) + 1) * 64;
672         if (TXTAIL(dev).offset)
673                 tailStarts = 0x800 - (((TXTAIL(dev).offset - offsetof(struct arlan_shmem, txBuffer)) / 64) + 2) * 64;
674
675
676         if (!TXHEAD(dev).offset && length < tailStarts)
677         {
678                 IFDEBUG(ARLAN_DEBUG_TX_CHAIN)
679                         printk(KERN_ERR "TXHEAD insert, tailStart %d\n", tailStarts);
680
681                 TXHEAD(dev).offset =
682                         offsetof(struct arlan_shmem, txBuffer);
683                 TXHEAD(dev).length = length - ARLAN_FAKE_HDR_LEN;
684                 for (i = 0; i < 6; i++)
685                         TXHEAD(dev).dest[i] = buf[i];
686                 TXHEAD(dev).clear = conf->txClear;
687                 TXHEAD(dev).retries = conf->txRetries;  /* 0 is use default */
688                 TXHEAD(dev).routing = conf->txRouting;
689                 TXHEAD(dev).scrambled = conf->txScrambled;
690                 memcpy_toio((char __iomem *)arlan + TXHEAD(dev).offset, buf + ARLAN_FAKE_HDR_LEN, TXHEAD(dev).length);
691         }
692         else if (!TXTAIL(dev).offset && length < (0x800 - headEnds))
693         {
694                 IFDEBUG(ARLAN_DEBUG_TX_CHAIN)
695                         printk(KERN_ERR "TXTAIL insert, headEnd %d\n", headEnds);
696
697                 TXTAIL(dev).offset =
698                         offsetof(struct arlan_shmem, txBuffer) + 0x800 - (length / 64 + 2) * 64;
699                 TXTAIL(dev).length = length - ARLAN_FAKE_HDR_LEN;
700                 for (i = 0; i < 6; i++)
701                         TXTAIL(dev).dest[i] = buf[i];
702                 TXTAIL(dev).clear = conf->txClear;
703                 TXTAIL(dev).retries = conf->txRetries;
704                 TXTAIL(dev).routing = conf->txRouting;
705                 TXTAIL(dev).scrambled = conf->txScrambled;
706                 memcpy_toio(((char __iomem *)arlan + TXTAIL(dev).offset), buf + ARLAN_FAKE_HDR_LEN, TXTAIL(dev).length);
707         }
708         else
709         {
710                 netif_stop_queue (dev);
711                 IFDEBUG(ARLAN_DEBUG_TX_CHAIN)
712                         printk(KERN_ERR "TX TAIL & HEAD full, return, tailStart %d headEnd %d\n", tailStarts, headEnds);
713                 return -1;
714         }
715         priv->out_bytes += length;
716         priv->out_bytes10 += length;
717         if (conf->measure_rate < 1)
718                 conf->measure_rate = 1;
719         if (time_after(jiffies, priv->out_time + conf->measure_rate * HZ))
720         {
721                 conf->out_speed = priv->out_bytes / conf->measure_rate;
722                 priv->out_bytes = 0;
723                 priv->out_time = jiffies;
724         }
725         if (time_after(jiffies, priv->out_time10 + conf->measure_rate * 10*HZ))
726         {
727                 conf->out_speed10 = priv->out_bytes10 / (10 * conf->measure_rate);
728                 priv->out_bytes10 = 0;
729                 priv->out_time10 = jiffies;
730         }
731         if (TXHEAD(dev).offset && TXTAIL(dev).offset)
732         {
733                 netif_stop_queue (dev);
734                 return 0;
735         }
736         else
737                 netif_start_queue (dev);
738
739
740         IFDEBUG(ARLAN_DEBUG_HEADER_DUMP)
741                 printk(KERN_WARNING "%s Transmit t %2x:%2x:%2x:%2x:%2x:%2x f %2x:%2x:%2x:%2x:%2x:%2x \n", dev->name,
742                    (unsigned char) buf[0], (unsigned char) buf[1], (unsigned char) buf[2], (unsigned char) buf[3],
743                    (unsigned char) buf[4], (unsigned char) buf[5], (unsigned char) buf[6], (unsigned char) buf[7],
744                    (unsigned char) buf[8], (unsigned char) buf[9], (unsigned char) buf[10], (unsigned char) buf[11]);
745
746         IFDEBUG(ARLAN_DEBUG_TX_CHAIN) printk(KERN_ERR "TX command prepare for buffer %d\n", priv->txLast);
747
748         arlan_command(dev, ARLAN_COMMAND_TX);
749
750         priv->tx_last_sent = jiffies;
751
752         IFDEBUG(ARLAN_DEBUG_TX_CHAIN) printk("%s TX Qued %d bytes \n", dev->name, length);
753
754         ARLAN_DEBUG_EXIT("arlan_hw_tx");
755
756         return 0;
757 }
758
759
760 static int arlan_hw_config(struct net_device *dev)
761 {
762         struct arlan_private *priv = netdev_priv(dev);
763         volatile struct arlan_shmem __iomem *arlan = priv->card;
764         struct arlan_conf_stru *conf = priv->Conf;
765
766         ARLAN_DEBUG_ENTRY("arlan_hw_config");
767
768         printk(KERN_NOTICE "%s arlan configure called \n", dev->name);
769         if (arlan_EEPROM_bad)
770                 printk(KERN_NOTICE "arlan configure with eeprom bad option \n");
771
772
773         WRITESHM(arlan->spreadingCode, conf->spreadingCode, u_char);
774         WRITESHM(arlan->channelSet, conf->channelSet, u_char);
775
776         if (arlan_EEPROM_bad)
777                 WRITESHM(arlan->defaultChannelSet, conf->channelSet, u_char);
778
779         WRITESHM(arlan->channelNumber, conf->channelNumber, u_char);
780
781         WRITESHM(arlan->scramblingDisable, conf->scramblingDisable, u_char);
782         WRITESHM(arlan->txAttenuation, conf->txAttenuation, u_char);
783
784         WRITESHM(arlan->systemId, conf->systemId, u_int);
785
786         WRITESHM(arlan->maxRetries, conf->maxRetries, u_char);
787         WRITESHM(arlan->receiveMode, conf->receiveMode, u_char);
788         WRITESHM(arlan->priority, conf->priority, u_char);
789         WRITESHM(arlan->rootOrRepeater, conf->rootOrRepeater, u_char);
790         WRITESHM(arlan->SID, conf->SID, u_int);
791
792         WRITESHM(arlan->registrationMode, conf->registrationMode, u_char);
793
794         WRITESHM(arlan->registrationFill, conf->registrationFill, u_char);
795         WRITESHM(arlan->localTalkAddress, conf->localTalkAddress, u_char);
796         WRITESHM(arlan->codeFormat, conf->codeFormat, u_char);
797         WRITESHM(arlan->numChannels, conf->numChannels, u_char);
798         WRITESHM(arlan->channel1, conf->channel1, u_char);
799         WRITESHM(arlan->channel2, conf->channel2, u_char);
800         WRITESHM(arlan->channel3, conf->channel3, u_char);
801         WRITESHM(arlan->channel4, conf->channel4, u_char);
802         WRITESHM(arlan->radioNodeId, conf->radioNodeId, u_short);
803         WRITESHM(arlan->SID, conf->SID, u_int);
804         WRITESHM(arlan->waitTime, conf->waitTime, u_short);
805         WRITESHM(arlan->lParameter, conf->lParameter, u_short);
806         memcpy_toio(&(arlan->_15), &(conf->_15), 3);
807         WRITESHM(arlan->_15, conf->_15, u_short);
808         WRITESHM(arlan->headerSize, conf->headerSize, u_short);
809         if (arlan_EEPROM_bad)
810                 WRITESHM(arlan->hardwareType, conf->hardwareType, u_char);
811         WRITESHM(arlan->radioType, conf->radioType, u_char);
812         if (arlan_EEPROM_bad)
813                 WRITESHM(arlan->radioModule, conf->radioType, u_char);
814
815         memcpy_toio(arlan->encryptionKey + keyStart, encryptionKey, 8);
816         memcpy_toio(arlan->name, conf->siteName, 16);
817
818         WRITESHMB(arlan->commandByte, ARLAN_COM_INT | ARLAN_COM_CONF);  /* do configure */
819         memset_io(arlan->commandParameter, 0, 0xf);     /* 0xf */
820         memset_io(arlan->commandParameter + 1, 0, 2);
821         if (conf->writeEEPROM)
822         {
823                   memset_io(arlan->commandParameter, conf->writeEEPROM, 1);
824 //              conf->writeEEPROM=0;
825         }
826         if (conf->registrationMode && conf->registrationInterrupts)
827                 memset_io(arlan->commandParameter + 3, 1, 1);
828         else
829                 memset_io(arlan->commandParameter + 3, 0, 1);
830
831         priv->irq_test_done = 0;
832
833         if (conf->tx_queue_len)
834                 dev->tx_queue_len = conf->tx_queue_len;
835         udelay(100);
836
837         ARLAN_DEBUG_EXIT("arlan_hw_config");
838         return 0;
839 }
840
841
842 static int arlan_read_card_configuration(struct net_device *dev)
843 {
844         u_char tlx415;
845         struct arlan_private *priv = netdev_priv(dev);
846         volatile struct arlan_shmem __iomem *arlan = priv->card;
847         struct arlan_conf_stru *conf = priv->Conf;
848
849         ARLAN_DEBUG_ENTRY("arlan_read_card_configuration");
850
851         if (radioNodeId == radioNodeIdUNKNOWN)
852         {
853                 READSHM(conf->radioNodeId, arlan->radioNodeId, u_short);
854         }
855         else
856                 conf->radioNodeId = radioNodeId;
857                 
858         if (SID == SIDUNKNOWN)
859         {
860                 READSHM(conf->SID, arlan->SID, u_int);
861         }
862         else conf->SID = SID;
863                 
864         if (spreadingCode == spreadingCodeUNKNOWN)
865         {
866                   READSHM(conf->spreadingCode, arlan->spreadingCode, u_char);
867         }
868         else
869                 conf->spreadingCode = spreadingCode;
870                 
871         if (channelSet == channelSetUNKNOWN)
872         {
873                 READSHM(conf->channelSet, arlan->channelSet, u_char);
874         }
875         else conf->channelSet = channelSet;
876
877         if (channelNumber == channelNumberUNKNOWN)
878         {
879                 READSHM(conf->channelNumber, arlan->channelNumber, u_char);
880         }
881         else conf->channelNumber = channelNumber;
882         
883         READSHM(conf->scramblingDisable, arlan->scramblingDisable, u_char);
884         READSHM(conf->txAttenuation, arlan->txAttenuation, u_char);
885         
886         if (systemId == systemIdUNKNOWN)
887         {
888                 READSHM(conf->systemId, arlan->systemId, u_int);
889         } 
890         else conf->systemId = systemId;
891         
892         READSHM(conf->maxDatagramSize, arlan->maxDatagramSize, u_short);
893         READSHM(conf->maxFrameSize, arlan->maxFrameSize, u_short);
894         READSHM(conf->maxRetries, arlan->maxRetries, u_char);
895         READSHM(conf->receiveMode, arlan->receiveMode, u_char);
896         READSHM(conf->priority, arlan->priority, u_char);
897         READSHM(conf->rootOrRepeater, arlan->rootOrRepeater, u_char);
898
899         if (SID == SIDUNKNOWN)
900         {
901                   READSHM(conf->SID, arlan->SID, u_int);
902         }
903         else conf->SID = SID;
904         
905         if (registrationMode == registrationModeUNKNOWN)
906         {
907                   READSHM(conf->registrationMode, arlan->registrationMode, u_char);
908         }
909         else conf->registrationMode = registrationMode;
910         
911         READSHM(conf->registrationFill, arlan->registrationFill, u_char);
912         READSHM(conf->localTalkAddress, arlan->localTalkAddress, u_char);
913         READSHM(conf->codeFormat, arlan->codeFormat, u_char);
914         READSHM(conf->numChannels, arlan->numChannels, u_char);
915         READSHM(conf->channel1, arlan->channel1, u_char);
916         READSHM(conf->channel2, arlan->channel2, u_char);
917         READSHM(conf->channel3, arlan->channel3, u_char);
918         READSHM(conf->channel4, arlan->channel4, u_char);
919         READSHM(conf->waitTime, arlan->waitTime, u_short);
920         READSHM(conf->lParameter, arlan->lParameter, u_short);
921         READSHM(conf->_15, arlan->_15, u_short);
922         READSHM(conf->headerSize, arlan->headerSize, u_short);
923         READSHM(conf->hardwareType, arlan->hardwareType, u_char);
924         READSHM(conf->radioType, arlan->radioModule, u_char);
925         
926         if (conf->radioType == 0)
927                 conf->radioType = 0xc;
928
929         WRITESHM(arlan->configStatus, 0xA5, u_char);
930         READSHM(tlx415, arlan->configStatus, u_char);
931         
932         if (tlx415 != 0xA5)
933                 printk(KERN_INFO "%s tlx415 chip \n", dev->name);
934         
935         conf->txClear = 0;
936         conf->txRetries = 1;
937         conf->txRouting = 1;
938         conf->txScrambled = 0;
939         conf->rxParameter = 1;
940         conf->txTimeoutMs = 4000;
941         conf->waitCardTimeout = 100000;
942         conf->receiveMode = ARLAN_RCV_CLEAN;
943         memcpy_fromio(conf->siteName, arlan->name, 16);
944         conf->siteName[16] = '\0';
945         conf->retries = retries;
946         conf->tx_delay_ms = tx_delay_ms;
947         conf->ReTransmitPacketMaxSize = 200;
948         conf->waitReTransmitPacketMaxSize = 200;
949         conf->txAckTimeoutMs = 900;
950         conf->fastReTransCount = 3;
951
952         ARLAN_DEBUG_EXIT("arlan_read_card_configuration");
953
954         return 0;
955 }
956
957
958 static int lastFoundAt = 0xbe000;
959
960
961 /*
962  * This is the real probe routine. Linux has a history of friendly device
963  * probes on the ISA bus. A good device probes avoids doing writes, and
964  * verifies that the correct device exists and functions.
965  */
966 #define ARLAN_SHMEM_SIZE        0x2000
967 static int __init arlan_check_fingerprint(unsigned long memaddr)
968 {
969         static const char probeText[] = "TELESYSTEM SLW INC.    ARLAN \0";
970         volatile struct arlan_shmem __iomem *arlan = (struct arlan_shmem *) memaddr;
971         unsigned long paddr = virt_to_phys((void *) memaddr);
972         char tempBuf[49];
973
974         ARLAN_DEBUG_ENTRY("arlan_check_fingerprint");
975
976         if (!request_mem_region(paddr, ARLAN_SHMEM_SIZE, "arlan")) {
977                 // printk(KERN_WARNING "arlan: memory region %lx excluded from probing \n",paddr);
978                 return -ENODEV;
979         }
980
981         memcpy_fromio(tempBuf, arlan->textRegion, 29);
982         tempBuf[30] = 0;
983
984         /* check for card at this address */
985         if (0 != strncmp(tempBuf, probeText, 29)){
986                 release_mem_region(paddr, ARLAN_SHMEM_SIZE);
987                 return -ENODEV;
988         }
989
990 //   printk(KERN_INFO "arlan found at 0x%x \n",memaddr);
991         ARLAN_DEBUG_EXIT("arlan_check_fingerprint");
992
993         return 0;
994 }
995
996 static int arlan_change_mtu(struct net_device *dev, int new_mtu)
997 {
998         struct arlan_private *priv = netdev_priv(dev);
999         struct arlan_conf_stru *conf = priv->Conf;
1000
1001         ARLAN_DEBUG_ENTRY("arlan_change_mtu");
1002         if (new_mtu > 2032)
1003                 return -EINVAL;
1004         dev->mtu = new_mtu;
1005         if (new_mtu < 256)
1006                 new_mtu = 256;  /* cards book suggests 1600 */
1007         conf->maxDatagramSize = new_mtu;
1008         conf->maxFrameSize = new_mtu + 48;
1009
1010         arlan_command(dev, ARLAN_COMMAND_CLEAN_AND_CONF);
1011         printk(KERN_NOTICE "%s mtu changed to %d \n", dev->name, new_mtu);
1012
1013         ARLAN_DEBUG_EXIT("arlan_change_mtu");
1014
1015         return 0;
1016 }
1017
1018 static int arlan_mac_addr(struct net_device *dev, void *p)
1019 {
1020         struct sockaddr *addr = p;
1021
1022
1023         ARLAN_DEBUG_ENTRY("arlan_mac_addr");
1024         return -EINVAL;
1025
1026         if (!netif_running(dev))
1027                 return -EBUSY;
1028         memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1029
1030         ARLAN_DEBUG_EXIT("arlan_mac_addr");
1031         return 0;
1032 }
1033
1034
1035
1036 static int __init arlan_setup_device(struct net_device *dev, int num)
1037 {
1038         struct arlan_private *ap = netdev_priv(dev);
1039         int err;
1040
1041         ARLAN_DEBUG_ENTRY("arlan_setup_device");
1042
1043         ap->conf = (struct arlan_shmem *)(ap+1);
1044
1045         dev->tx_queue_len = tx_queue_len;
1046         dev->open = arlan_open;
1047         dev->stop = arlan_close;
1048         dev->hard_start_xmit = arlan_tx;
1049         dev->get_stats = arlan_statistics;
1050         dev->set_multicast_list = arlan_set_multicast;
1051         dev->change_mtu = arlan_change_mtu;
1052         dev->set_mac_address = arlan_mac_addr;
1053         dev->tx_timeout = arlan_tx_timeout;
1054         dev->watchdog_timeo = 3*HZ;
1055         
1056         ap->irq_test_done = 0;
1057         ap->Conf = &arlan_conf[num];
1058
1059         ap->Conf->pre_Command_Wait = 40;
1060         ap->Conf->rx_tweak1 = 30;
1061         ap->Conf->rx_tweak2 = 0;
1062
1063
1064         err = register_netdev(dev);
1065         if (err) {
1066                 release_mem_region(virt_to_phys((void *) dev->mem_start), 
1067                            ARLAN_SHMEM_SIZE);
1068                 free_netdev(dev);
1069                 return err;
1070         }
1071         arlan_device[num] = dev;
1072         ARLAN_DEBUG_EXIT("arlan_setup_device");
1073         return 0;
1074 }
1075
1076 static int __init arlan_probe_here(struct net_device *dev, 
1077                                    unsigned long memaddr)
1078 {
1079         struct arlan_private *ap = netdev_priv(dev);
1080
1081         ARLAN_DEBUG_ENTRY("arlan_probe_here");
1082
1083         if (arlan_check_fingerprint(memaddr))
1084                 return -ENODEV;
1085
1086         printk(KERN_NOTICE "%s: Arlan found at %x, \n ", dev->name, 
1087                (int) virt_to_phys((void*)memaddr));
1088
1089         ap->card = (void *) memaddr;
1090         dev->mem_start = memaddr;
1091         dev->mem_end = memaddr + ARLAN_SHMEM_SIZE-1;
1092
1093         if (dev->irq < 2)
1094         {
1095                 READSHM(dev->irq, ap->card->irqLevel, u_char);
1096         } else if (dev->irq == 2)
1097                 dev->irq = 9;
1098
1099         arlan_read_card_configuration(dev);
1100
1101         ARLAN_DEBUG_EXIT("arlan_probe_here");
1102         return 0;
1103 }
1104
1105
1106 static int arlan_open(struct net_device *dev)
1107 {
1108         struct arlan_private *priv = netdev_priv(dev);
1109         volatile struct arlan_shmem __iomem *arlan = priv->card;
1110         int ret = 0;
1111
1112         ARLAN_DEBUG_ENTRY("arlan_open");
1113
1114         ret = request_irq(dev->irq, &arlan_interrupt, 0, dev->name, dev);
1115         if (ret)
1116         {
1117                 printk(KERN_ERR "%s: unable to get IRQ %d .\n",
1118                         dev->name, dev->irq);
1119                 return ret;
1120         }
1121
1122
1123         priv->bad = 0;
1124         priv->lastReset = 0;
1125         priv->reset = 0;
1126         memcpy_fromio(dev->dev_addr, arlan->lanCardNodeId, 6);
1127         memset(dev->broadcast, 0xff, 6);
1128         dev->tx_queue_len = tx_queue_len;
1129         priv->interrupt_processing_active = 0;
1130         spin_lock_init(&priv->lock);
1131
1132         netif_start_queue (dev);
1133
1134         priv->registrationLostCount = 0;
1135         priv->registrationLastSeen = jiffies;
1136         priv->txLast = 0;
1137         priv->tx_command_given = 0;
1138         priv->rx_command_given = 0;
1139         
1140         priv->reRegisterExp = 1;
1141         priv->tx_last_sent = jiffies - 1;
1142         priv->tx_last_cleared = jiffies;
1143         priv->Conf->writeEEPROM = 0;
1144         priv->Conf->registrationInterrupts = 1;
1145
1146         init_timer(&priv->timer);
1147         priv->timer.expires = jiffies + HZ / 10;
1148         priv->timer.data = (unsigned long) dev;
1149         priv->timer.function = &arlan_registration_timer;       /* timer handler */
1150
1151         arlan_command(dev, ARLAN_COMMAND_POWERUP | ARLAN_COMMAND_LONG_WAIT_NOW);
1152         mdelay(200);
1153         add_timer(&priv->timer);
1154
1155         ARLAN_DEBUG_EXIT("arlan_open");
1156         return 0;
1157 }
1158
1159
1160 static void arlan_tx_timeout (struct net_device *dev)
1161 {
1162         printk(KERN_ERR "%s: arlan transmit timed out, kernel decided\n", dev->name);
1163         /* Try to restart the adaptor. */
1164         arlan_command(dev, ARLAN_COMMAND_CLEAN_AND_RESET);
1165         // dev->trans_start = jiffies;
1166         // netif_start_queue (dev);
1167 }
1168
1169
1170 static int arlan_tx(struct sk_buff *skb, struct net_device *dev)
1171 {
1172         short length;
1173         unsigned char *buf;
1174
1175         ARLAN_DEBUG_ENTRY("arlan_tx");
1176         
1177         length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
1178         buf = skb->data;
1179
1180         if (length + 0x12 > 0x800) {
1181                 printk(KERN_ERR "TX RING overflow \n");
1182                 netif_stop_queue (dev);
1183         }
1184
1185         if (arlan_hw_tx(dev, buf, length) == -1)
1186                 goto bad_end;
1187
1188         dev->trans_start = jiffies;
1189
1190         dev_kfree_skb(skb);
1191
1192         arlan_process_interrupt(dev);
1193         ARLAN_DEBUG_EXIT("arlan_tx");
1194         return 0;
1195
1196 bad_end:
1197         arlan_process_interrupt(dev);
1198         netif_stop_queue (dev);
1199         ARLAN_DEBUG_EXIT("arlan_tx");
1200         return 1;
1201 }
1202
1203
1204 static inline int DoNotReTransmitCrap(struct net_device *dev)
1205 {
1206         struct arlan_private *priv = netdev_priv(dev);
1207
1208         if (TXLAST(dev).length < priv->Conf->ReTransmitPacketMaxSize)
1209                 return 1;
1210         return 0;
1211
1212 }
1213
1214 static inline int DoNotWaitReTransmitCrap(struct net_device *dev)
1215 {
1216         struct arlan_private *priv = netdev_priv(dev);
1217
1218         if (TXLAST(dev).length < priv->Conf->waitReTransmitPacketMaxSize)
1219                 return 1;
1220         return 0;
1221 }
1222
1223 static inline void arlan_queue_retransmit(struct net_device *dev)
1224 {
1225         struct arlan_private *priv = netdev_priv(dev);
1226
1227         ARLAN_DEBUG_ENTRY("arlan_queue_retransmit");
1228
1229         if (DoNotWaitReTransmitCrap(dev))
1230         {
1231                   arlan_drop_tx(dev);
1232         } else
1233                 priv->ReTransmitRequested++;
1234
1235         ARLAN_DEBUG_EXIT("arlan_queue_retransmit");
1236 }
1237
1238 static inline void RetryOrFail(struct net_device *dev)
1239 {
1240         struct arlan_private *priv = netdev_priv(dev);
1241
1242         ARLAN_DEBUG_ENTRY("RetryOrFail");
1243
1244         if (priv->retransmissions > priv->Conf->retries ||
1245             DoNotReTransmitCrap(dev))
1246         {
1247                 arlan_drop_tx(dev);
1248         }
1249         else if (priv->bad <= priv->Conf->fastReTransCount)
1250         {
1251                 arlan_retransmit_now(dev);
1252         }
1253         else arlan_queue_retransmit(dev);
1254
1255         ARLAN_DEBUG_EXIT("RetryOrFail");
1256 }
1257
1258
1259 static void arlan_tx_done_interrupt(struct net_device *dev, int status)
1260 {
1261         struct arlan_private *priv = netdev_priv(dev);
1262
1263         ARLAN_DEBUG_ENTRY("arlan_tx_done_interrupt");
1264
1265         priv->tx_last_cleared = jiffies;
1266         priv->tx_command_given = 0;
1267         switch (status)
1268         {
1269                 case 1:
1270                 {
1271                         IFDEBUG(ARLAN_DEBUG_TX_CHAIN)
1272                                 printk("arlan intr: transmit OK\n");
1273                         priv->stats.tx_packets++;
1274                         priv->bad = 0;
1275                         priv->reset = 0;
1276                         priv->retransmissions = 0;
1277                         if (priv->Conf->tx_delay_ms)
1278                         {
1279                                 priv->tx_done_delayed = jiffies + (priv->Conf->tx_delay_ms * HZ) / 1000 + 1;
1280                         }
1281                         else
1282                         {
1283                                 TXLAST(dev).offset = 0;
1284                                 if (priv->txLast)
1285                                         priv->txLast = 0;
1286                                 else if (TXTAIL(dev).offset)
1287                                         priv->txLast = 1;
1288                                 if (TXLAST(dev).offset)
1289                                 {
1290                                         arlan_retransmit_now(dev);
1291                                         dev->trans_start = jiffies;
1292                                 }
1293                                 if (!TXHEAD(dev).offset || !TXTAIL(dev).offset)
1294                                 {
1295                                         netif_wake_queue (dev);
1296                                 }
1297                         }
1298                 }
1299                 break;
1300                 
1301                 case 2:
1302                 {
1303                         IFDEBUG(ARLAN_DEBUG_TX_CHAIN)
1304                                 printk("arlan intr: transmit timed out\n");
1305                         priv->bad += 1;
1306                         //arlan_queue_retransmit(dev);
1307                         RetryOrFail(dev);
1308                 }
1309                 break;
1310
1311                 case 3:
1312                 {
1313                         IFDEBUG(ARLAN_DEBUG_TX_CHAIN)
1314                                 printk("arlan intr: transmit max retries\n");
1315                         priv->bad += 1;
1316                         priv->reset = 0;
1317                         //arlan_queue_retransmit(dev);
1318                         RetryOrFail(dev);
1319                 }
1320                 break;
1321                 
1322                 case 4:
1323                 {
1324                         IFDEBUG(ARLAN_DEBUG_TX_CHAIN)
1325                                 printk("arlan intr: transmit aborted\n");
1326                         priv->bad += 1;
1327                         arlan_queue_retransmit(dev);
1328                         //RetryOrFail(dev);
1329                 }
1330                 break;
1331
1332                 case 5:
1333                 {
1334                         IFDEBUG(ARLAN_DEBUG_TX_CHAIN)
1335                                 printk("arlan intr: transmit not registered\n");
1336                         priv->bad += 1;
1337                         //debug=101;
1338                         arlan_queue_retransmit(dev);
1339                 }
1340                 break;
1341
1342                 case 6:
1343                 {
1344                         IFDEBUG(ARLAN_DEBUG_TX_CHAIN) 
1345                                 printk("arlan intr: transmit destination full\n");
1346                         priv->bad += 1;
1347                         priv->reset = 0;
1348                         //arlan_drop_tx(dev);
1349                         arlan_queue_retransmit(dev);
1350                 }
1351                 break;
1352
1353                 case 7:
1354                 {
1355                         IFDEBUG(ARLAN_DEBUG_TX_CHAIN)
1356                                 printk("arlan intr: transmit unknown ack\n");
1357                         priv->bad += 1;
1358                         priv->reset = 0;
1359                         arlan_queue_retransmit(dev);
1360                 }
1361                 break;
1362                 
1363                 case 8:
1364                 {
1365                         IFDEBUG(ARLAN_DEBUG_TX_CHAIN)
1366                                 printk("arlan intr: transmit dest mail box full\n");
1367                         priv->bad += 1;
1368                         priv->reset = 0;
1369                         //arlan_drop_tx(dev);
1370                         arlan_queue_retransmit(dev);
1371                 }
1372                 break;
1373
1374                 case 9:
1375                 {
1376                         IFDEBUG(ARLAN_DEBUG_TX_CHAIN)
1377                                 printk("arlan intr: transmit root dest not reg.\n");
1378                         priv->bad += 1;
1379                         priv->reset = 1;
1380                         //arlan_drop_tx(dev);
1381                         arlan_queue_retransmit(dev);
1382                 }
1383                 break;
1384
1385                 default:
1386                 {
1387                         printk(KERN_ERR "arlan intr: transmit status unknown\n");
1388                         priv->bad += 1;
1389                         priv->reset = 1;
1390                         arlan_drop_tx(dev);
1391                 }
1392         }
1393
1394         ARLAN_DEBUG_EXIT("arlan_tx_done_interrupt");
1395 }
1396
1397
1398 static void arlan_rx_interrupt(struct net_device *dev, u_char rxStatus, u_short rxOffset, u_short pkt_len)
1399 {
1400         char *skbtmp;
1401         int i = 0;
1402
1403         struct arlan_private *priv = netdev_priv(dev);
1404         volatile struct arlan_shmem __iomem *arlan = priv->card;
1405         struct arlan_conf_stru *conf = priv->Conf;
1406
1407
1408         ARLAN_DEBUG_ENTRY("arlan_rx_interrupt");
1409         // by spec,   not                WRITESHMB(arlan->rxStatus,0x00);
1410         // prohibited here              arlan_command(dev, ARLAN_COMMAND_RX);
1411
1412         if (pkt_len < 10 || pkt_len > 2048)
1413         {
1414                 printk(KERN_WARNING "%s: got too short or long packet, len %d \n", dev->name, pkt_len);
1415                 return;
1416         }
1417         if (rxOffset + pkt_len > 0x2000)
1418         {
1419                 printk("%s: got too long packet, len %d offset %x\n", dev->name, pkt_len, rxOffset);
1420                 return;
1421         }
1422         priv->in_bytes += pkt_len;
1423         priv->in_bytes10 += pkt_len;
1424         if (conf->measure_rate < 1)
1425                 conf->measure_rate = 1;
1426         if (time_after(jiffies, priv->in_time + conf->measure_rate * HZ))
1427         {
1428                 conf->in_speed = priv->in_bytes / conf->measure_rate;
1429                 priv->in_bytes = 0;
1430                 priv->in_time = jiffies;
1431         }
1432         if (time_after(jiffies, priv->in_time10 + conf->measure_rate * 10*HZ))
1433         {
1434                 conf->in_speed10 = priv->in_bytes10 / (10 * conf->measure_rate);
1435                 priv->in_bytes10 = 0;
1436                 priv->in_time10 = jiffies;
1437         }
1438         DEBUGSHM(1, "arlan rcv pkt rxStatus= %d ", arlan->rxStatus, u_char);
1439         switch (rxStatus)
1440         {
1441                 case 1:
1442                 case 2:
1443                 case 3:
1444                 {
1445                         /* Malloc up new buffer. */
1446                         struct sk_buff *skb;
1447
1448                         DEBUGSHM(50, "arlan recv pkt offs=%d\n", arlan->rxOffset, u_short);
1449                         DEBUGSHM(1, "arlan rxFrmType = %d \n", arlan->rxFrmType, u_char);
1450                         DEBUGSHM(1, KERN_INFO "arlan rx scrambled = %d \n", arlan->scrambled, u_char);
1451
1452                         /* here we do multicast filtering to avoid slow 8-bit memcopy */
1453 #ifdef ARLAN_MULTICAST
1454                         if (!(dev->flags & IFF_ALLMULTI) &&
1455                                 !(dev->flags & IFF_PROMISC) &&
1456                                 dev->mc_list)
1457                         {
1458                                 char hw_dst_addr[6];
1459                                 struct dev_mc_list *dmi = dev->mc_list;
1460                                 int i;
1461
1462                                 memcpy_fromio(hw_dst_addr, arlan->ultimateDestAddress, 6);
1463                                 if (hw_dst_addr[0] == 0x01)
1464                                 {
1465                                         if (mdebug)
1466                                                 if (hw_dst_addr[1] == 0x00)
1467                                                         printk(KERN_ERR "%s mcast 0x0100 \n", dev->name);
1468                                                 else if (hw_dst_addr[1] == 0x40)
1469                                                         printk(KERN_ERR "%s m/bcast 0x0140 \n", dev->name);
1470                                         while (dmi)
1471                                         {                                                       if (dmi->dmi_addrlen == 6)
1472                                                 {
1473                                                         if (arlan_debug & ARLAN_DEBUG_HEADER_DUMP)
1474                                                                 printk(KERN_ERR "%s mcl %2x:%2x:%2x:%2x:%2x:%2x \n", dev->name,
1475                                                                                  dmi->dmi_addr[0], dmi->dmi_addr[1], dmi->dmi_addr[2],
1476                                                                                  dmi->dmi_addr[3], dmi->dmi_addr[4], dmi->dmi_addr[5]);
1477                                                         for (i = 0; i < 6; i++)
1478                                                                 if (dmi->dmi_addr[i] != hw_dst_addr[i])
1479                                                                         break;
1480                                                         if (i == 6)
1481                                                                 break;
1482                                                 }
1483                                                 else
1484                                                         printk(KERN_ERR "%s: invalid multicast address length given.\n", dev->name);
1485                                                 dmi = dmi->next;
1486                                         }
1487                                         /* we reach here if multicast filtering is on and packet 
1488                                          * is multicast and not for receive */
1489                                         goto end_of_interrupt;
1490                                 }
1491                         }
1492 #endif                          // ARLAN_MULTICAST
1493                         /* multicast filtering ends here */
1494                         pkt_len += ARLAN_FAKE_HDR_LEN;
1495
1496                         skb = dev_alloc_skb(pkt_len + 4);
1497                         if (skb == NULL)
1498                         {
1499                                 printk(KERN_ERR "%s: Memory squeeze, dropping packet.\n", dev->name);
1500                                 priv->stats.rx_dropped++;
1501                                 break;
1502                         }
1503                         skb_reserve(skb, 2);
1504                         skb->dev = dev;
1505                         skbtmp = skb_put(skb, pkt_len);
1506
1507                         memcpy_fromio(skbtmp + ARLAN_FAKE_HDR_LEN, ((char __iomem *) arlan) + rxOffset, pkt_len - ARLAN_FAKE_HDR_LEN);
1508                         memcpy_fromio(skbtmp, arlan->ultimateDestAddress, 6);
1509                         memcpy_fromio(skbtmp + 6, arlan->rxSrc, 6);
1510                         WRITESHMB(arlan->rxStatus, 0x00);
1511                         arlan_command(dev, ARLAN_COMMAND_RX);
1512
1513                         IFDEBUG(ARLAN_DEBUG_HEADER_DUMP)
1514                         {
1515                                 char immedDestAddress[6];
1516                                 char immedSrcAddress[6];
1517                                 memcpy_fromio(immedDestAddress, arlan->immedDestAddress, 6);
1518                                 memcpy_fromio(immedSrcAddress, arlan->immedSrcAddress, 6);
1519
1520                                 printk(KERN_WARNING "%s t %2x:%2x:%2x:%2x:%2x:%2x f %2x:%2x:%2x:%2x:%2x:%2x imd %2x:%2x:%2x:%2x:%2x:%2x ims %2x:%2x:%2x:%2x:%2x:%2x\n", dev->name,
1521                                         (unsigned char) skbtmp[0], (unsigned char) skbtmp[1], (unsigned char) skbtmp[2], (unsigned char) skbtmp[3],
1522                                         (unsigned char) skbtmp[4], (unsigned char) skbtmp[5], (unsigned char) skbtmp[6], (unsigned char) skbtmp[7],
1523                                         (unsigned char) skbtmp[8], (unsigned char) skbtmp[9], (unsigned char) skbtmp[10], (unsigned char) skbtmp[11],
1524                                         immedDestAddress[0], immedDestAddress[1], immedDestAddress[2],
1525                                         immedDestAddress[3], immedDestAddress[4], immedDestAddress[5],
1526                                         immedSrcAddress[0], immedSrcAddress[1], immedSrcAddress[2],
1527                                         immedSrcAddress[3], immedSrcAddress[4], immedSrcAddress[5]);
1528                         }
1529                         skb->protocol = eth_type_trans(skb, dev);
1530                         IFDEBUG(ARLAN_DEBUG_HEADER_DUMP)
1531                                 if (skb->protocol != 0x608 && skb->protocol != 0x8)
1532                                 {
1533                                         for (i = 0; i <= 22; i++)
1534                                                 printk("%02x:", (u_char) skbtmp[i + 12]);
1535                                         printk(KERN_ERR "\n");
1536                                         printk(KERN_WARNING "arlan kernel pkt type trans %x \n", skb->protocol);
1537                                 }
1538                         netif_rx(skb);
1539                         dev->last_rx = jiffies;
1540                         priv->stats.rx_packets++;
1541                         priv->stats.rx_bytes += pkt_len;
1542                 }
1543                 break;
1544                 
1545                 default:
1546                         printk(KERN_ERR "arlan intr: received unknown status\n");
1547                         priv->stats.rx_crc_errors++;
1548                         break;
1549         }
1550         ARLAN_DEBUG_EXIT("arlan_rx_interrupt");
1551 }
1552
1553 static void arlan_process_interrupt(struct net_device *dev)
1554 {
1555         struct arlan_private *priv = netdev_priv(dev);
1556         volatile struct arlan_shmem __iomem *arlan = priv->card;
1557         u_char rxStatus = READSHMB(arlan->rxStatus);
1558         u_char txStatus = READSHMB(arlan->txStatus);
1559         u_short rxOffset = READSHMS(arlan->rxOffset);
1560         u_short pkt_len = READSHMS(arlan->rxLength);
1561         int interrupt_count = 0;
1562
1563         ARLAN_DEBUG_ENTRY("arlan_process_interrupt");
1564
1565         if (test_and_set_bit(0, (void *) &priv->interrupt_processing_active))
1566         {
1567                 if (arlan_debug & ARLAN_DEBUG_CHAIN_LOCKS)
1568                         printk(KERN_ERR "interrupt chain reentering \n");
1569                 goto end_int_process;
1570         }
1571         while ((rxStatus || txStatus || priv->interrupt_ack_requested)
1572                         && (interrupt_count < 5))
1573         {
1574                 if (rxStatus)
1575                         priv->last_rx_int_ack_time = jiffies;
1576
1577                 arlan_command(dev, ARLAN_COMMAND_INT_ACK);
1578                 arlan_command(dev, ARLAN_COMMAND_INT_ENABLE);
1579                 
1580                 IFDEBUG(ARLAN_DEBUG_INTERRUPT)
1581                         printk(KERN_ERR "%s:  got IRQ rx %x tx %x comm %x rxOff %x rxLen %x \n",
1582                                         dev->name, rxStatus, txStatus, READSHMB(arlan->commandByte),
1583                                         rxOffset, pkt_len);
1584
1585                 if (rxStatus == 0 && txStatus == 0)
1586                 {
1587                         if (priv->irq_test_done)
1588                         {
1589                                 if (!registrationBad(dev))
1590                                         IFDEBUG(ARLAN_DEBUG_INTERRUPT) printk(KERN_ERR "%s unknown interrupt(nop? regLost ?) reason tx %d rx %d ",
1591                                                                                     dev->name, txStatus, rxStatus);
1592                         } else {
1593                                 IFDEBUG(ARLAN_DEBUG_INTERRUPT)
1594                                         printk(KERN_INFO "%s irq $%d test OK \n", dev->name, dev->irq);
1595
1596                         }
1597                         priv->interrupt_ack_requested = 0;
1598                         goto ends;
1599                 }
1600                 if (txStatus != 0)
1601                 {
1602                         WRITESHMB(arlan->txStatus, 0x00);
1603                         arlan_tx_done_interrupt(dev, txStatus);
1604                         goto ends;
1605                 }
1606                 if (rxStatus == 1 || rxStatus == 2)
1607                 {               /* a packet waiting */
1608                         arlan_rx_interrupt(dev, rxStatus, rxOffset, pkt_len);
1609                         goto ends;
1610                 }
1611                 if (rxStatus > 2 && rxStatus < 0xff)
1612                 {
1613                         WRITESHMB(arlan->rxStatus, 0x00);
1614                         printk(KERN_ERR "%s unknown rxStatus reason tx %d rx %d ",
1615                                 dev->name, txStatus, rxStatus);
1616                         goto ends;
1617                 }
1618                 if (rxStatus == 0xff)
1619                 {
1620                         WRITESHMB(arlan->rxStatus, 0x00);
1621                         arlan_command(dev, ARLAN_COMMAND_RX);
1622                         if (registrationBad(dev))
1623                                 netif_device_detach(dev);
1624                         if (!registrationBad(dev))
1625                         {
1626                                 priv->registrationLastSeen = jiffies;
1627                                 if (!netif_queue_stopped(dev) && !priv->under_reset && !priv->under_config)
1628                                         netif_wake_queue (dev);
1629                         }
1630                         goto ends;
1631                 }
1632 ends:
1633
1634                 arlan_command_process(dev);
1635
1636                 rxStatus = READSHMB(arlan->rxStatus);
1637                 txStatus = READSHMB(arlan->txStatus);
1638                 rxOffset = READSHMS(arlan->rxOffset);
1639                 pkt_len = READSHMS(arlan->rxLength);
1640
1641
1642                 priv->irq_test_done = 1;
1643
1644                 interrupt_count++;
1645         }
1646         priv->interrupt_processing_active = 0;
1647
1648 end_int_process:
1649         arlan_command_process(dev);
1650
1651         ARLAN_DEBUG_EXIT("arlan_process_interrupt");
1652         return;
1653 }
1654
1655 static irqreturn_t arlan_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1656 {
1657         struct net_device *dev = dev_id;
1658         struct arlan_private *priv = netdev_priv(dev);
1659         volatile struct arlan_shmem __iomem *arlan = priv->card;
1660         u_char rxStatus = READSHMB(arlan->rxStatus);
1661         u_char txStatus = READSHMB(arlan->txStatus);
1662
1663         ARLAN_DEBUG_ENTRY("arlan_interrupt");
1664
1665
1666         if (!rxStatus && !txStatus)
1667                 priv->interrupt_ack_requested++;
1668
1669         arlan_process_interrupt(dev);
1670         
1671         priv->irq_test_done = 1;
1672
1673         ARLAN_DEBUG_EXIT("arlan_interrupt");
1674         return IRQ_HANDLED;
1675
1676 }
1677
1678
1679 static int arlan_close(struct net_device *dev)
1680 {
1681         struct arlan_private *priv = netdev_priv(dev);
1682
1683         ARLAN_DEBUG_ENTRY("arlan_close");
1684
1685         del_timer_sync(&priv->timer);
1686
1687         arlan_command(dev, ARLAN_COMMAND_POWERDOWN);
1688
1689         IFDEBUG(ARLAN_DEBUG_STARTUP)
1690                 printk(KERN_NOTICE "%s: Closing device\n", dev->name);
1691
1692         netif_stop_queue(dev);
1693         free_irq(dev->irq, dev);
1694
1695         ARLAN_DEBUG_EXIT("arlan_close");
1696         return 0;
1697 }
1698
1699 #ifdef ARLAN_DEBUGGING
1700 static long alignLong(volatile u_char * ptr)
1701 {
1702         long ret;
1703         memcpy_fromio(&ret, (void *) ptr, 4);
1704         return ret;
1705 }
1706 #endif
1707
1708 /*
1709  * Get the current statistics.
1710  * This may be called with the card open or closed.
1711  */
1712
1713 static struct net_device_stats *arlan_statistics(struct net_device *dev)
1714 {
1715         struct arlan_private *priv = netdev_priv(dev);
1716         volatile struct arlan_shmem __iomem *arlan = priv->card;
1717
1718
1719         ARLAN_DEBUG_ENTRY("arlan_statistics");
1720
1721         /* Update the statistics from the device registers. */
1722
1723         READSHM(priv->stats.collisions, arlan->numReTransmissions, u_int);
1724         READSHM(priv->stats.rx_crc_errors, arlan->numCRCErrors, u_int);
1725         READSHM(priv->stats.rx_dropped, arlan->numFramesDiscarded, u_int);
1726         READSHM(priv->stats.rx_fifo_errors, arlan->numRXBufferOverflows, u_int);
1727         READSHM(priv->stats.rx_frame_errors, arlan->numReceiveFramesLost, u_int);
1728         READSHM(priv->stats.rx_over_errors, arlan->numRXOverruns, u_int);
1729         READSHM(priv->stats.rx_packets, arlan->numDatagramsReceived, u_int);
1730         READSHM(priv->stats.tx_aborted_errors, arlan->numAbortErrors, u_int);
1731         READSHM(priv->stats.tx_carrier_errors, arlan->numStatusTimeouts, u_int);
1732         READSHM(priv->stats.tx_dropped, arlan->numDatagramsDiscarded, u_int);
1733         READSHM(priv->stats.tx_fifo_errors, arlan->numTXUnderruns, u_int);
1734         READSHM(priv->stats.tx_packets, arlan->numDatagramsTransmitted, u_int);
1735         READSHM(priv->stats.tx_window_errors, arlan->numHoldOffs, u_int);
1736
1737         ARLAN_DEBUG_EXIT("arlan_statistics");
1738
1739         return &priv->stats;
1740 }
1741
1742
1743 static void arlan_set_multicast(struct net_device *dev)
1744 {
1745         struct arlan_private *priv = netdev_priv(dev);
1746         volatile struct arlan_shmem __iomem *arlan = priv->card;
1747         struct arlan_conf_stru *conf = priv->Conf;
1748         int board_conf_needed = 0;
1749
1750
1751         ARLAN_DEBUG_ENTRY("arlan_set_multicast");
1752
1753         if (dev->flags & IFF_PROMISC)
1754         {
1755                 unsigned char recMode;
1756                 READSHM(recMode, arlan->receiveMode, u_char);
1757                 conf->receiveMode = (ARLAN_RCV_PROMISC | ARLAN_RCV_CONTROL);
1758                 if (conf->receiveMode != recMode)
1759                         board_conf_needed = 1;
1760         }
1761         else
1762         {
1763                 /* turn off promiscuous mode  */
1764                 unsigned char recMode;
1765                 READSHM(recMode, arlan->receiveMode, u_char);
1766                 conf->receiveMode = ARLAN_RCV_CLEAN | ARLAN_RCV_CONTROL;
1767                 if (conf->receiveMode != recMode)
1768                         board_conf_needed = 1;
1769         }
1770         if (board_conf_needed)
1771                 arlan_command(dev, ARLAN_COMMAND_CONF);
1772
1773         ARLAN_DEBUG_EXIT("arlan_set_multicast");
1774 }
1775
1776
1777 struct net_device * __init arlan_probe(int unit)
1778 {
1779         struct net_device *dev;
1780         int err;
1781         int m;
1782
1783         ARLAN_DEBUG_ENTRY("arlan_probe");
1784
1785         if (arlans_found == MAX_ARLANS)
1786                 return ERR_PTR(-ENODEV);
1787
1788         /* 
1789          * Reserve space for local data and a copy of the shared memory
1790          * that is used by the /proc interface.
1791          */
1792         dev = alloc_etherdev(sizeof(struct arlan_private)
1793                              + sizeof(struct arlan_shmem));
1794         if (!dev)
1795                 return ERR_PTR(-ENOMEM);
1796
1797         SET_MODULE_OWNER(dev);
1798
1799         if (unit >= 0) {
1800                 sprintf(dev->name, "eth%d", unit);
1801                 netdev_boot_setup_check(dev);
1802                 
1803                 if (dev->mem_start) {
1804                         if (arlan_probe_here(dev, dev->mem_start) == 0)
1805                                 goto found;
1806                         goto not_found;
1807                 }
1808                         
1809         }
1810
1811
1812         for (m = (int)phys_to_virt(lastFoundAt) + ARLAN_SHMEM_SIZE; 
1813              m <= (int)phys_to_virt(0xDE000); 
1814              m += ARLAN_SHMEM_SIZE)
1815         {
1816                 if (arlan_probe_here(dev, m) == 0)
1817                 {
1818                         lastFoundAt = (int)virt_to_phys((void*)m);
1819                         goto found;
1820                 }
1821         }
1822
1823         if (lastFoundAt == 0xbe000)
1824                 printk(KERN_ERR "arlan: No Arlan devices found \n");
1825
1826  not_found:
1827         free_netdev(dev);
1828         return ERR_PTR(-ENODEV);
1829
1830  found:
1831         err = arlan_setup_device(dev, arlans_found);
1832         if (err)
1833                 dev = ERR_PTR(err);
1834         else if (!arlans_found++)
1835                 printk(KERN_INFO "Arlan driver %s\n", arlan_version);
1836
1837         return dev;
1838 }
1839
1840 #ifdef  MODULE
1841 int __init init_module(void)
1842 {
1843         int i = 0;
1844
1845         ARLAN_DEBUG_ENTRY("init_module");
1846
1847         if (channelSet != channelSetUNKNOWN || channelNumber != channelNumberUNKNOWN || systemId != systemIdUNKNOWN)
1848                 return -EINVAL;
1849
1850         for (i = 0; i < MAX_ARLANS; i++) {
1851                 struct net_device *dev = arlan_probe(i);
1852
1853                 if (IS_ERR(dev)) 
1854                         return PTR_ERR(dev);
1855         }
1856         init_arlan_proc();
1857         printk(KERN_INFO "Arlan driver %s\n", arlan_version);
1858         ARLAN_DEBUG_EXIT("init_module");
1859         return 0;
1860 }
1861
1862
1863 void __exit cleanup_module(void)
1864 {
1865         int i = 0;
1866         struct net_device *dev;
1867
1868         ARLAN_DEBUG_ENTRY("cleanup_module");
1869
1870         IFDEBUG(ARLAN_DEBUG_SHUTDOWN)
1871                 printk(KERN_INFO "arlan: unloading module\n");
1872
1873         cleanup_arlan_proc();
1874
1875         for (i = 0; i < MAX_ARLANS; i++)
1876         {
1877                 dev = arlan_device[i];
1878                 if (dev) {
1879                         arlan_command(dev, ARLAN_COMMAND_POWERDOWN );
1880
1881                         unregister_netdev(dev);
1882                         release_mem_region(virt_to_phys((void *) dev->mem_start), 
1883                                            ARLAN_SHMEM_SIZE);
1884                         free_netdev(dev);
1885                         arlan_device[i] = NULL;
1886                 }
1887         }
1888
1889         ARLAN_DEBUG_EXIT("cleanup_module");
1890 }
1891
1892
1893 #endif
1894 MODULE_LICENSE("GPL");