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