This commit was generated by cvs2svn to compensate for changes in r517,
[linux-2.6.git] / drivers / net / wireless / wavelan_cs.c
1 /*
2  *      Wavelan Pcmcia driver
3  *
4  *              Jean II - HPLB '96
5  *
6  * Reorganisation and extension of the driver.
7  * Original copyright follow. See wavelan_cs.p.h for details.
8  *
9  * This code is derived from Anthony D. Joseph's code and all the changes here
10  * are also under the original copyright below.
11  *
12  * This code supports version 2.00 of WaveLAN/PCMCIA cards (2.4GHz), and
13  * can work on Linux 2.0.36 with support of David Hinds' PCMCIA Card Services
14  *
15  * Joe Finney (joe@comp.lancs.ac.uk) at Lancaster University in UK added
16  * critical code in the routine to initialize the Modem Management Controller.
17  *
18  * Thanks to Alan Cox and Bruce Janson for their advice.
19  *
20  *      -- Yunzhou Li (scip4166@nus.sg)
21  *
22 #ifdef WAVELAN_ROAMING  
23  * Roaming support added 07/22/98 by Justin Seger (jseger@media.mit.edu)
24  * based on patch by Joe Finney from Lancaster University.
25 #endif
26  *
27  * Lucent (formerly AT&T GIS, formerly NCR) WaveLAN PCMCIA card: An
28  * Ethernet-like radio transceiver controlled by an Intel 82593 coprocessor.
29  *
30  *   A non-shared memory PCMCIA ethernet driver for linux
31  *
32  * ISA version modified to support PCMCIA by Anthony Joseph (adj@lcs.mit.edu)
33  *
34  *
35  * Joseph O'Sullivan & John Langford (josullvn@cs.cmu.edu & jcl@cs.cmu.edu)
36  *
37  * Apr 2 '98  made changes to bring the i82593 control/int handling in line
38  *             with offical specs...
39  *
40  ****************************************************************************
41  *   Copyright 1995
42  *   Anthony D. Joseph
43  *   Massachusetts Institute of Technology
44  *
45  *   Permission to use, copy, modify, and distribute this program
46  *   for any purpose and without fee is hereby granted, provided
47  *   that this copyright and permission notice appear on all copies
48  *   and supporting documentation, the name of M.I.T. not be used
49  *   in advertising or publicity pertaining to distribution of the
50  *   program without specific prior permission, and notice be given
51  *   in supporting documentation that copying and distribution is
52  *   by permission of M.I.T.  M.I.T. makes no representations about
53  *   the suitability of this software for any purpose.  It is pro-
54  *   vided "as is" without express or implied warranty.         
55  ****************************************************************************
56  *
57  */
58
59 /* Do *NOT* add other headers here, you are guaranteed to be wrong - Jean II */
60 #include "wavelan_cs.p.h"               /* Private header */
61
62 /************************* MISC SUBROUTINES **************************/
63 /*
64  * Subroutines which won't fit in one of the following category
65  * (wavelan modem or i82593)
66  */
67
68 #ifdef STRUCT_CHECK
69 /*------------------------------------------------------------------*/
70 /*
71  * Sanity routine to verify the sizes of the various WaveLAN interface
72  * structures.
73  */
74 static char *
75 wv_structuct_check(void)
76 {
77 #define SC(t,s,n)       if (sizeof(t) != s) return(n);
78
79   SC(psa_t, PSA_SIZE, "psa_t");
80   SC(mmw_t, MMW_SIZE, "mmw_t");
81   SC(mmr_t, MMR_SIZE, "mmr_t");
82
83 #undef  SC
84
85   return((char *) NULL);
86 } /* wv_structuct_check */
87 #endif  /* STRUCT_CHECK */
88
89 /******************* MODEM MANAGEMENT SUBROUTINES *******************/
90 /*
91  * Useful subroutines to manage the modem of the wavelan
92  */
93
94 /*------------------------------------------------------------------*/
95 /*
96  * Read from card's Host Adaptor Status Register.
97  */
98 static inline u_char
99 hasr_read(u_long        base)
100 {
101   return(inb(HASR(base)));
102 } /* hasr_read */
103
104 /*------------------------------------------------------------------*/
105 /*
106  * Write to card's Host Adapter Command Register.
107  */
108 static inline void
109 hacr_write(u_long       base,
110            u_char       hacr)
111 {
112   outb(hacr, HACR(base));
113 } /* hacr_write */
114
115 /*------------------------------------------------------------------*/
116 /*
117  * Write to card's Host Adapter Command Register. Include a delay for
118  * those times when it is needed.
119  */
120 static inline void
121 hacr_write_slow(u_long  base,
122                 u_char  hacr)
123 {
124   hacr_write(base, hacr);
125   /* delay might only be needed sometimes */
126   mdelay(1);
127 } /* hacr_write_slow */
128
129 /*------------------------------------------------------------------*/
130 /*
131  * Read the Parameter Storage Area from the WaveLAN card's memory
132  */
133 static void
134 psa_read(struct net_device *    dev,
135          int            o,      /* offset in PSA */
136          u_char *       b,      /* buffer to fill */
137          int            n)      /* size to read */
138 {
139   u_char *      ptr = ((u_char *)dev->mem_start) + PSA_ADDR + (o << 1);
140
141   while(n-- > 0)
142     {
143       *b++ = readb(ptr);
144       /* Due to a lack of address decode pins, the WaveLAN PCMCIA card
145        * only supports reading even memory addresses. That means the
146        * increment here MUST be two.
147        * Because of that, we can't use memcpy_fromio()...
148        */
149       ptr += 2;
150     }
151 } /* psa_read */
152
153 /*------------------------------------------------------------------*/
154 /*
155  * Write the Paramter Storage Area to the WaveLAN card's memory
156  */
157 static void
158 psa_write(struct net_device *   dev,
159           int           o,      /* Offset in psa */
160           u_char *      b,      /* Buffer in memory */
161           int           n)      /* Length of buffer */
162 {
163   u_char *      ptr = ((u_char *) dev->mem_start) + PSA_ADDR + (o << 1);
164   int           count = 0;
165   ioaddr_t      base = dev->base_addr;
166   /* As there seem to have no flag PSA_BUSY as in the ISA model, we are
167    * oblige to verify this address to know when the PSA is ready... */
168   volatile u_char *     verify = ((u_char *) dev->mem_start) + PSA_ADDR +
169     (psaoff(0, psa_comp_number) << 1);
170
171   /* Authorize writting to PSA */
172   hacr_write(base, HACR_PWR_STAT | HACR_ROM_WEN);
173
174   while(n-- > 0)
175     {
176       /* write to PSA */
177       writeb(*b++, ptr);
178       ptr += 2;
179
180       /* I don't have the spec, so I don't know what the correct
181        * sequence to write is. This hack seem to work for me... */
182       count = 0;
183       while((readb(verify) != PSA_COMP_PCMCIA_915) && (count++ < 100))
184         mdelay(1);
185     }
186
187   /* Put the host interface back in standard state */
188   hacr_write(base, HACR_DEFAULT);
189 } /* psa_write */
190
191 #ifdef SET_PSA_CRC
192 /*------------------------------------------------------------------*/
193 /*
194  * Calculate the PSA CRC
195  * Thanks to Valster, Nico <NVALSTER@wcnd.nl.lucent.com> for the code
196  * NOTE: By specifying a length including the CRC position the
197  * returned value should be zero. (i.e. a correct checksum in the PSA)
198  *
199  * The Windows drivers don't use the CRC, but the AP and the PtP tool
200  * depend on it.
201  */
202 static u_short
203 psa_crc(unsigned char * psa,    /* The PSA */
204         int             size)   /* Number of short for CRC */
205 {
206   int           byte_cnt;       /* Loop on the PSA */
207   u_short       crc_bytes = 0;  /* Data in the PSA */
208   int           bit_cnt;        /* Loop on the bits of the short */
209
210   for(byte_cnt = 0; byte_cnt < size; byte_cnt++ )
211     {
212       crc_bytes ^= psa[byte_cnt];       /* Its an xor */
213
214       for(bit_cnt = 1; bit_cnt < 9; bit_cnt++ )
215         {
216           if(crc_bytes & 0x0001)
217             crc_bytes = (crc_bytes >> 1) ^ 0xA001;
218           else
219             crc_bytes >>= 1 ;
220         }
221     }
222
223   return crc_bytes;
224 } /* psa_crc */
225 #endif  /* SET_PSA_CRC */
226
227 /*------------------------------------------------------------------*/
228 /*
229  * update the checksum field in the Wavelan's PSA
230  */
231 static void
232 update_psa_checksum(struct net_device * dev)
233 {
234 #ifdef SET_PSA_CRC
235   psa_t         psa;
236   u_short       crc;
237
238   /* read the parameter storage area */
239   psa_read(dev, 0, (unsigned char *) &psa, sizeof(psa));
240
241   /* update the checksum */
242   crc = psa_crc((unsigned char *) &psa,
243                 sizeof(psa) - sizeof(psa.psa_crc[0]) - sizeof(psa.psa_crc[1])
244                 - sizeof(psa.psa_crc_status));
245
246   psa.psa_crc[0] = crc & 0xFF;
247   psa.psa_crc[1] = (crc & 0xFF00) >> 8;
248
249   /* Write it ! */
250   psa_write(dev, (char *)&psa.psa_crc - (char *)&psa,
251             (unsigned char *)&psa.psa_crc, 2);
252
253 #ifdef DEBUG_IOCTL_INFO
254   printk (KERN_DEBUG "%s: update_psa_checksum(): crc = 0x%02x%02x\n",
255           dev->name, psa.psa_crc[0], psa.psa_crc[1]);
256
257   /* Check again (luxury !) */
258   crc = psa_crc((unsigned char *) &psa,
259                  sizeof(psa) - sizeof(psa.psa_crc_status));
260
261   if(crc != 0)
262     printk(KERN_WARNING "%s: update_psa_checksum(): CRC does not agree with PSA data (even after recalculating)\n", dev->name);
263 #endif /* DEBUG_IOCTL_INFO */
264 #endif  /* SET_PSA_CRC */
265 } /* update_psa_checksum */
266
267 /*------------------------------------------------------------------*/
268 /*
269  * Write 1 byte to the MMC.
270  */
271 static inline void
272 mmc_out(u_long          base,
273         u_short         o,
274         u_char          d)
275 {
276   int count = 0;
277
278   /* Wait for MMC to go idle */
279   while((count++ < 100) && (inb(HASR(base)) & HASR_MMI_BUSY))
280     udelay(10);
281
282   outb((u_char)((o << 1) | MMR_MMI_WR), MMR(base));
283   outb(d, MMD(base));
284 }
285
286 /*------------------------------------------------------------------*/
287 /*
288  * Routine to write bytes to the Modem Management Controller.
289  * We start by the end because it is the way it should be !
290  */
291 static inline void
292 mmc_write(u_long        base,
293           u_char        o,
294           u_char *      b,
295           int           n)
296 {
297   o += n;
298   b += n;
299
300   while(n-- > 0 )
301     mmc_out(base, --o, *(--b));
302 } /* mmc_write */
303
304 /*------------------------------------------------------------------*/
305 /*
306  * Read 1 byte from the MMC.
307  * Optimised version for 1 byte, avoid using memory...
308  */
309 static inline u_char
310 mmc_in(u_long   base,
311        u_short  o)
312 {
313   int count = 0;
314
315   while((count++ < 100) && (inb(HASR(base)) & HASR_MMI_BUSY))
316     udelay(10);
317   outb(o << 1, MMR(base));              /* Set the read address */
318
319   outb(0, MMD(base));                   /* Required dummy write */
320
321   while((count++ < 100) && (inb(HASR(base)) & HASR_MMI_BUSY))
322     udelay(10);
323   return (u_char) (inb(MMD(base)));     /* Now do the actual read */
324 }
325
326 /*------------------------------------------------------------------*/
327 /*
328  * Routine to read bytes from the Modem Management Controller.
329  * The implementation is complicated by a lack of address lines,
330  * which prevents decoding of the low-order bit.
331  * (code has just been moved in the above function)
332  * We start by the end because it is the way it should be !
333  */
334 static inline void
335 mmc_read(u_long         base,
336          u_char         o,
337          u_char *       b,
338          int            n)
339 {
340   o += n;
341   b += n;
342
343   while(n-- > 0)
344     *(--b) = mmc_in(base, --o);
345 } /* mmc_read */
346
347 /*------------------------------------------------------------------*/
348 /*
349  * Get the type of encryption available...
350  */
351 static inline int
352 mmc_encr(u_long         base)   /* i/o port of the card */
353 {
354   int   temp;
355
356   temp = mmc_in(base, mmroff(0, mmr_des_avail));
357   if((temp != MMR_DES_AVAIL_DES) && (temp != MMR_DES_AVAIL_AES))
358     return 0;
359   else
360     return temp;
361 }
362
363 /*------------------------------------------------------------------*/
364 /*
365  * Wait for the frequency EEprom to complete a command...
366  * I hope this one will be optimally inlined...
367  */
368 static inline void
369 fee_wait(u_long         base,   /* i/o port of the card */
370          int            delay,  /* Base delay to wait for */
371          int            number) /* Number of time to wait */
372 {
373   int           count = 0;      /* Wait only a limited time */
374
375   while((count++ < number) &&
376         (mmc_in(base, mmroff(0, mmr_fee_status)) & MMR_FEE_STATUS_BUSY))
377     udelay(delay);
378 }
379
380 /*------------------------------------------------------------------*/
381 /*
382  * Read bytes from the Frequency EEprom (frequency select cards).
383  */
384 static void
385 fee_read(u_long         base,   /* i/o port of the card */
386          u_short        o,      /* destination offset */
387          u_short *      b,      /* data buffer */
388          int            n)      /* number of registers */
389 {
390   b += n;               /* Position at the end of the area */
391
392   /* Write the address */
393   mmc_out(base, mmwoff(0, mmw_fee_addr), o + n - 1);
394
395   /* Loop on all buffer */
396   while(n-- > 0)
397     {
398       /* Write the read command */
399       mmc_out(base, mmwoff(0, mmw_fee_ctrl), MMW_FEE_CTRL_READ);
400
401       /* Wait until EEprom is ready (should be quick !) */
402       fee_wait(base, 10, 100);
403
404       /* Read the value */
405       *--b = ((mmc_in(base, mmroff(0, mmr_fee_data_h)) << 8) |
406               mmc_in(base, mmroff(0, mmr_fee_data_l)));
407     }
408 }
409
410 #ifdef WIRELESS_EXT     /* If wireless extension exist in the kernel */
411
412 /*------------------------------------------------------------------*/
413 /*
414  * Write bytes from the Frequency EEprom (frequency select cards).
415  * This is a bit complicated, because the frequency eeprom has to
416  * be unprotected and the write enabled.
417  * Jean II
418  */
419 static void
420 fee_write(u_long        base,   /* i/o port of the card */
421           u_short       o,      /* destination offset */
422           u_short *     b,      /* data buffer */
423           int           n)      /* number of registers */
424 {
425   b += n;               /* Position at the end of the area */
426
427 #ifdef EEPROM_IS_PROTECTED      /* disabled */
428 #ifdef DOESNT_SEEM_TO_WORK      /* disabled */
429   /* Ask to read the protected register */
430   mmc_out(base, mmwoff(0, mmw_fee_ctrl), MMW_FEE_CTRL_PRREAD);
431
432   fee_wait(base, 10, 100);
433
434   /* Read the protected register */
435   printk("Protected 2 : %02X-%02X\n",
436          mmc_in(base, mmroff(0, mmr_fee_data_h)),
437          mmc_in(base, mmroff(0, mmr_fee_data_l)));
438 #endif  /* DOESNT_SEEM_TO_WORK */
439
440   /* Enable protected register */
441   mmc_out(base, mmwoff(0, mmw_fee_addr), MMW_FEE_ADDR_EN);
442   mmc_out(base, mmwoff(0, mmw_fee_ctrl), MMW_FEE_CTRL_PREN);
443
444   fee_wait(base, 10, 100);
445
446   /* Unprotect area */
447   mmc_out(base, mmwoff(0, mmw_fee_addr), o + n);
448   mmc_out(base, mmwoff(0, mmw_fee_ctrl), MMW_FEE_CTRL_PRWRITE);
449 #ifdef DOESNT_SEEM_TO_WORK      /* disabled */
450   /* Or use : */
451   mmc_out(base, mmwoff(0, mmw_fee_ctrl), MMW_FEE_CTRL_PRCLEAR);
452 #endif  /* DOESNT_SEEM_TO_WORK */
453
454   fee_wait(base, 10, 100);
455 #endif  /* EEPROM_IS_PROTECTED */
456
457   /* Write enable */
458   mmc_out(base, mmwoff(0, mmw_fee_addr), MMW_FEE_ADDR_EN);
459   mmc_out(base, mmwoff(0, mmw_fee_ctrl), MMW_FEE_CTRL_WREN);
460
461   fee_wait(base, 10, 100);
462
463   /* Write the EEprom address */
464   mmc_out(base, mmwoff(0, mmw_fee_addr), o + n - 1);
465
466   /* Loop on all buffer */
467   while(n-- > 0)
468     {
469       /* Write the value */
470       mmc_out(base, mmwoff(0, mmw_fee_data_h), (*--b) >> 8);
471       mmc_out(base, mmwoff(0, mmw_fee_data_l), *b & 0xFF);
472
473       /* Write the write command */
474       mmc_out(base, mmwoff(0, mmw_fee_ctrl), MMW_FEE_CTRL_WRITE);
475
476       /* Wavelan doc says : wait at least 10 ms for EEBUSY = 0 */
477       mdelay(10);
478       fee_wait(base, 10, 100);
479     }
480
481   /* Write disable */
482   mmc_out(base, mmwoff(0, mmw_fee_addr), MMW_FEE_ADDR_DS);
483   mmc_out(base, mmwoff(0, mmw_fee_ctrl), MMW_FEE_CTRL_WDS);
484
485   fee_wait(base, 10, 100);
486
487 #ifdef EEPROM_IS_PROTECTED      /* disabled */
488   /* Reprotect EEprom */
489   mmc_out(base, mmwoff(0, mmw_fee_addr), 0x00);
490   mmc_out(base, mmwoff(0, mmw_fee_ctrl), MMW_FEE_CTRL_PRWRITE);
491
492   fee_wait(base, 10, 100);
493 #endif  /* EEPROM_IS_PROTECTED */
494 }
495 #endif  /* WIRELESS_EXT */
496
497 /******************* WaveLAN Roaming routines... ********************/
498
499 #ifdef WAVELAN_ROAMING  /* Conditional compile, see wavelan_cs.h */
500
501 unsigned char WAVELAN_BEACON_ADDRESS[]= {0x09,0x00,0x0e,0x20,0x03,0x00};
502   
503 void wv_roam_init(struct net_device *dev)
504 {
505   net_local  *lp= netdev_priv(dev);
506
507   /* Do not remove this unless you have a good reason */
508   printk(KERN_NOTICE "%s: Warning, you have enabled roaming on"
509          " device %s !\n", dev->name, dev->name);
510   printk(KERN_NOTICE "Roaming is currently an experimental unsupported feature"
511          " of the Wavelan driver.\n");
512   printk(KERN_NOTICE "It may work, but may also make the driver behave in"
513          " erratic ways or crash.\n");
514
515   lp->wavepoint_table.head=NULL;           /* Initialise WavePoint table */
516   lp->wavepoint_table.num_wavepoints=0;
517   lp->wavepoint_table.locked=0;
518   lp->curr_point=NULL;                        /* No default WavePoint */
519   lp->cell_search=0;
520   
521   lp->cell_timer.data=(long)lp;               /* Start cell expiry timer */
522   lp->cell_timer.function=wl_cell_expiry;
523   lp->cell_timer.expires=jiffies+CELL_TIMEOUT;
524   add_timer(&lp->cell_timer);
525   
526   wv_nwid_filter(NWID_PROMISC,lp) ;    /* Enter NWID promiscuous mode */
527   /* to build up a good WavePoint */
528                                            /* table... */
529   printk(KERN_DEBUG "WaveLAN: Roaming enabled on device %s\n",dev->name);
530 }
531  
532 void wv_roam_cleanup(struct net_device *dev)
533 {
534   wavepoint_history *ptr,*old_ptr;
535   net_local *lp= netdev_priv(dev);
536   
537   printk(KERN_DEBUG "WaveLAN: Roaming Disabled on device %s\n",dev->name);
538   
539   /* Fixme : maybe we should check that the timer exist before deleting it */
540   del_timer(&lp->cell_timer);          /* Remove cell expiry timer       */
541   ptr=lp->wavepoint_table.head;        /* Clear device's WavePoint table */
542   while(ptr!=NULL)
543     {
544       old_ptr=ptr;
545       ptr=ptr->next;    
546       wl_del_wavepoint(old_ptr,lp);     
547     }
548 }
549
550 /* Enable/Disable NWID promiscuous mode on a given device */
551 void wv_nwid_filter(unsigned char mode, net_local *lp)
552 {
553   mm_t                  m;
554   unsigned long         flags;
555   
556 #ifdef WAVELAN_ROAMING_DEBUG
557   printk(KERN_DEBUG "WaveLAN: NWID promisc %s, device %s\n",(mode==NWID_PROMISC) ? "on" : "off", lp->dev->name);
558 #endif
559   
560   /* Disable interrupts & save flags */
561   spin_lock_irqsave(&lp->spinlock, flags);
562   
563   m.w.mmw_loopt_sel = (mode==NWID_PROMISC) ? MMW_LOOPT_SEL_DIS_NWID : 0x00;
564   mmc_write(lp->dev->base_addr, (char *)&m.w.mmw_loopt_sel - (char *)&m, (unsigned char *)&m.w.mmw_loopt_sel, 1);
565   
566   if(mode==NWID_PROMISC)
567     lp->cell_search=1;
568   else
569     lp->cell_search=0;
570
571   /* ReEnable interrupts & restore flags */
572   spin_unlock_irqrestore(&lp->spinlock, flags);
573 }
574
575 /* Find a record in the WavePoint table matching a given NWID */
576 wavepoint_history *wl_roam_check(unsigned short nwid, net_local *lp)
577 {
578   wavepoint_history     *ptr=lp->wavepoint_table.head;
579   
580   while(ptr!=NULL){
581     if(ptr->nwid==nwid)
582       return ptr;       
583     ptr=ptr->next;
584   }
585   return NULL;
586 }
587
588 /* Create a new wavepoint table entry */
589 wavepoint_history *wl_new_wavepoint(unsigned short nwid, unsigned char seq, net_local* lp)
590 {
591   wavepoint_history *new_wavepoint;
592
593 #ifdef WAVELAN_ROAMING_DEBUG    
594   printk(KERN_DEBUG "WaveLAN: New Wavepoint, NWID:%.4X\n",nwid);
595 #endif
596   
597   if(lp->wavepoint_table.num_wavepoints==MAX_WAVEPOINTS)
598     return NULL;
599   
600   new_wavepoint=(wavepoint_history *) kmalloc(sizeof(wavepoint_history),GFP_ATOMIC);
601   if(new_wavepoint==NULL)
602     return NULL;
603   
604   new_wavepoint->nwid=nwid;                       /* New WavePoints NWID */
605   new_wavepoint->average_fast=0;                    /* Running Averages..*/
606   new_wavepoint->average_slow=0;
607   new_wavepoint->qualptr=0;                       /* Start of ringbuffer */
608   new_wavepoint->last_seq=seq-1;                /* Last sequence no.seen */
609   memset(new_wavepoint->sigqual,0,WAVEPOINT_HISTORY);/* Empty ringbuffer */
610   
611   new_wavepoint->next=lp->wavepoint_table.head;/* Add to wavepoint table */
612   new_wavepoint->prev=NULL;
613   
614   if(lp->wavepoint_table.head!=NULL)
615     lp->wavepoint_table.head->prev=new_wavepoint;
616   
617   lp->wavepoint_table.head=new_wavepoint;
618   
619   lp->wavepoint_table.num_wavepoints++;     /* no. of visible wavepoints */
620   
621   return new_wavepoint;
622 }
623
624 /* Remove a wavepoint entry from WavePoint table */
625 void wl_del_wavepoint(wavepoint_history *wavepoint, struct net_local *lp)
626 {
627   if(wavepoint==NULL)
628     return;
629   
630   if(lp->curr_point==wavepoint)
631     lp->curr_point=NULL;
632   
633   if(wavepoint->prev!=NULL)
634     wavepoint->prev->next=wavepoint->next;
635   
636   if(wavepoint->next!=NULL)
637     wavepoint->next->prev=wavepoint->prev;
638   
639   if(lp->wavepoint_table.head==wavepoint)
640     lp->wavepoint_table.head=wavepoint->next;
641   
642   lp->wavepoint_table.num_wavepoints--;
643   kfree(wavepoint);
644 }
645
646 /* Timer callback function - checks WavePoint table for stale entries */ 
647 void wl_cell_expiry(unsigned long data)
648 {
649   net_local *lp=(net_local *)data;
650   wavepoint_history *wavepoint=lp->wavepoint_table.head,*old_point;
651   
652 #if WAVELAN_ROAMING_DEBUG > 1
653   printk(KERN_DEBUG "WaveLAN: Wavepoint timeout, dev %s\n",lp->dev->name);
654 #endif
655   
656   if(lp->wavepoint_table.locked)
657     {
658 #if WAVELAN_ROAMING_DEBUG > 1
659       printk(KERN_DEBUG "WaveLAN: Wavepoint table locked...\n");
660 #endif
661       
662       lp->cell_timer.expires=jiffies+1; /* If table in use, come back later */
663       add_timer(&lp->cell_timer);
664       return;
665     }
666   
667   while(wavepoint!=NULL)
668     {
669       if(time_after(jiffies, wavepoint->last_seen + CELL_TIMEOUT))
670         {
671 #ifdef WAVELAN_ROAMING_DEBUG
672           printk(KERN_DEBUG "WaveLAN: Bye bye %.4X\n",wavepoint->nwid);
673 #endif
674           
675           old_point=wavepoint;
676           wavepoint=wavepoint->next;
677           wl_del_wavepoint(old_point,lp);
678         }
679       else
680         wavepoint=wavepoint->next;
681     }
682   lp->cell_timer.expires=jiffies+CELL_TIMEOUT;
683   add_timer(&lp->cell_timer);
684 }
685
686 /* Update SNR history of a wavepoint */
687 void wl_update_history(wavepoint_history *wavepoint, unsigned char sigqual, unsigned char seq)  
688 {
689   int i=0,num_missed=0,ptr=0;
690   int average_fast=0,average_slow=0;
691   
692   num_missed=(seq-wavepoint->last_seq)%WAVEPOINT_HISTORY;/* Have we missed
693                                                             any beacons? */
694   if(num_missed)
695     for(i=0;i<num_missed;i++)
696       {
697         wavepoint->sigqual[wavepoint->qualptr++]=0; /* If so, enter them as 0's */
698         wavepoint->qualptr %=WAVEPOINT_HISTORY;    /* in the ringbuffer. */
699       }
700   wavepoint->last_seen=jiffies;                 /* Add beacon to history */
701   wavepoint->last_seq=seq;      
702   wavepoint->sigqual[wavepoint->qualptr++]=sigqual;          
703   wavepoint->qualptr %=WAVEPOINT_HISTORY;
704   ptr=(wavepoint->qualptr-WAVEPOINT_FAST_HISTORY+WAVEPOINT_HISTORY)%WAVEPOINT_HISTORY;
705   
706   for(i=0;i<WAVEPOINT_FAST_HISTORY;i++)       /* Update running averages */
707     {
708       average_fast+=wavepoint->sigqual[ptr++];
709       ptr %=WAVEPOINT_HISTORY;
710     }
711   
712   average_slow=average_fast;
713   for(i=WAVEPOINT_FAST_HISTORY;i<WAVEPOINT_HISTORY;i++)
714     {
715       average_slow+=wavepoint->sigqual[ptr++];
716       ptr %=WAVEPOINT_HISTORY;
717     }
718   
719   wavepoint->average_fast=average_fast/WAVEPOINT_FAST_HISTORY;
720   wavepoint->average_slow=average_slow/WAVEPOINT_HISTORY;       
721 }
722
723 /* Perform a handover to a new WavePoint */
724 void wv_roam_handover(wavepoint_history *wavepoint, net_local *lp)
725 {
726   ioaddr_t              base = lp->dev->base_addr;  
727   mm_t                  m;
728   unsigned long         flags;
729
730   if(wavepoint==lp->curr_point)          /* Sanity check... */
731     {
732       wv_nwid_filter(!NWID_PROMISC,lp);
733       return;
734     }
735   
736 #ifdef WAVELAN_ROAMING_DEBUG
737   printk(KERN_DEBUG "WaveLAN: Doing handover to %.4X, dev %s\n",wavepoint->nwid,lp->dev->name);
738 #endif
739         
740   /* Disable interrupts & save flags */
741   spin_lock_irqsave(&lp->spinlock, flags);
742
743   m.w.mmw_netw_id_l = wavepoint->nwid & 0xFF;
744   m.w.mmw_netw_id_h = (wavepoint->nwid & 0xFF00) >> 8;
745   
746   mmc_write(base, (char *)&m.w.mmw_netw_id_l - (char *)&m, (unsigned char *)&m.w.mmw_netw_id_l, 2);
747   
748   /* ReEnable interrupts & restore flags */
749   spin_unlock_irqrestore(&lp->spinlock, flags);
750
751   wv_nwid_filter(!NWID_PROMISC,lp);
752   lp->curr_point=wavepoint;
753 }
754
755 /* Called when a WavePoint beacon is received */
756 static inline void wl_roam_gather(struct net_device *  dev,
757                                   u_char *  hdr,   /* Beacon header */
758                                   u_char *  stats) /* SNR, Signal quality 
759                                                       of packet */
760 {
761   wavepoint_beacon *beacon= (wavepoint_beacon *)hdr; /* Rcvd. Beacon */
762   unsigned short nwid=ntohs(beacon->nwid);  
763   unsigned short sigqual=stats[2] & MMR_SGNL_QUAL;   /* SNR of beacon */
764   wavepoint_history *wavepoint=NULL;                /* WavePoint table entry */
765   net_local *lp = netdev_priv(dev);              /* Device info */
766
767 #ifdef I_NEED_THIS_FEATURE
768   /* Some people don't need this, some other may need it */
769   nwid=nwid^ntohs(beacon->domain_id);
770 #endif
771
772 #if WAVELAN_ROAMING_DEBUG > 1
773   printk(KERN_DEBUG "WaveLAN: beacon, dev %s:\n",dev->name);
774   printk(KERN_DEBUG "Domain: %.4X NWID: %.4X SigQual=%d\n",ntohs(beacon->domain_id),nwid,sigqual);
775 #endif
776   
777   lp->wavepoint_table.locked=1;                            /* <Mutex> */
778   
779   wavepoint=wl_roam_check(nwid,lp);            /* Find WavePoint table entry */
780   if(wavepoint==NULL)                    /* If no entry, Create a new one... */
781     {
782       wavepoint=wl_new_wavepoint(nwid,beacon->seq,lp);
783       if(wavepoint==NULL)
784         goto out;
785     }
786   if(lp->curr_point==NULL)             /* If this is the only WavePoint, */
787     wv_roam_handover(wavepoint, lp);             /* Jump on it! */
788   
789   wl_update_history(wavepoint, sigqual, beacon->seq); /* Update SNR history
790                                                          stats. */
791   
792   if(lp->curr_point->average_slow < SEARCH_THRESH_LOW) /* If our current */
793     if(!lp->cell_search)                  /* WavePoint is getting faint, */
794       wv_nwid_filter(NWID_PROMISC,lp);    /* start looking for a new one */
795   
796   if(wavepoint->average_slow > 
797      lp->curr_point->average_slow + WAVELAN_ROAMING_DELTA)
798     wv_roam_handover(wavepoint, lp);   /* Handover to a better WavePoint */
799   
800   if(lp->curr_point->average_slow > SEARCH_THRESH_HIGH) /* If our SNR is */
801     if(lp->cell_search)  /* getting better, drop out of cell search mode */
802       wv_nwid_filter(!NWID_PROMISC,lp);
803   
804 out:
805   lp->wavepoint_table.locked=0;                        /* </MUTEX>   :-) */
806 }
807
808 /* Test this MAC frame a WavePoint beacon */
809 static inline int WAVELAN_BEACON(unsigned char *data)
810 {
811   wavepoint_beacon *beacon= (wavepoint_beacon *)data;
812   static wavepoint_beacon beacon_template={0xaa,0xaa,0x03,0x08,0x00,0x0e,0x20,0x03,0x00};
813   
814   if(memcmp(beacon,&beacon_template,9)==0)
815     return 1;
816   else
817     return 0;
818 }
819 #endif  /* WAVELAN_ROAMING */
820
821 /************************ I82593 SUBROUTINES *************************/
822 /*
823  * Useful subroutines to manage the Ethernet controller
824  */
825
826 /*------------------------------------------------------------------*/
827 /*
828  * Routine to synchronously send a command to the i82593 chip. 
829  * Should be called with interrupts disabled.
830  * (called by wv_packet_write(), wv_ru_stop(), wv_ru_start(),
831  *  wv_82593_config() & wv_diag())
832  */
833 static int
834 wv_82593_cmd(struct net_device *        dev,
835              char *     str,
836              int        cmd,
837              int        result)
838 {
839   ioaddr_t      base = dev->base_addr;
840   int           status;
841   int           wait_completed;
842   long          spin;
843
844   /* Spin until the chip finishes executing its current command (if any) */
845   spin = 1000;
846   do
847     {
848       /* Time calibration of the loop */
849       udelay(10);
850
851       /* Read the interrupt register */
852       outb(OP0_NOP | CR0_STATUS_3, LCCR(base));
853       status = inb(LCSR(base));
854     }
855   while(((status & SR3_EXEC_STATE_MASK) != SR3_EXEC_IDLE) && (spin-- > 0));
856
857   /* If the interrupt hasn't be posted */
858   if(spin <= 0)
859     {
860 #ifdef DEBUG_INTERRUPT_ERROR
861       printk(KERN_INFO "wv_82593_cmd: %s timeout (previous command), status 0x%02x\n",
862              str, status);
863 #endif
864       return(FALSE);
865     }
866
867   /* Issue the command to the controller */
868   outb(cmd, LCCR(base));
869
870   /* If we don't have to check the result of the command
871    * Note : this mean that the irq handler will deal with that */
872   if(result == SR0_NO_RESULT)
873     return(TRUE);
874
875   /* We are waiting for command completion */
876   wait_completed = TRUE;
877
878   /* Busy wait while the LAN controller executes the command. */
879   spin = 1000;
880   do
881     {
882       /* Time calibration of the loop */
883       udelay(10);
884
885       /* Read the interrupt register */
886       outb(CR0_STATUS_0 | OP0_NOP, LCCR(base));
887       status = inb(LCSR(base));
888
889       /* Check if there was an interrupt posted */
890       if((status & SR0_INTERRUPT))
891         {
892           /* Acknowledge the interrupt */
893           outb(CR0_INT_ACK | OP0_NOP, LCCR(base));
894
895           /* Check if interrupt is a command completion */
896           if(((status & SR0_BOTH_RX_TX) != SR0_BOTH_RX_TX) &&
897              ((status & SR0_BOTH_RX_TX) != 0x0) &&
898              !(status & SR0_RECEPTION))
899             {
900               /* Signal command completion */
901               wait_completed = FALSE;
902             }
903           else
904             {
905               /* Note : Rx interrupts will be handled later, because we can
906                * handle multiple Rx packets at once */
907 #ifdef DEBUG_INTERRUPT_INFO
908               printk(KERN_INFO "wv_82593_cmd: not our interrupt\n");
909 #endif
910             }
911         }
912     }
913   while(wait_completed && (spin-- > 0));
914
915   /* If the interrupt hasn't be posted */
916   if(wait_completed)
917     {
918 #ifdef DEBUG_INTERRUPT_ERROR
919       printk(KERN_INFO "wv_82593_cmd: %s timeout, status 0x%02x\n",
920              str, status);
921 #endif
922       return(FALSE);
923     }
924
925   /* Check the return code returned by the card (see above) against
926    * the expected return code provided by the caller */
927   if((status & SR0_EVENT_MASK) != result)
928     {
929 #ifdef DEBUG_INTERRUPT_ERROR
930       printk(KERN_INFO "wv_82593_cmd: %s failed, status = 0x%x\n",
931              str, status);
932 #endif
933       return(FALSE);
934     }
935
936   return(TRUE);
937 } /* wv_82593_cmd */
938
939 /*------------------------------------------------------------------*/
940 /*
941  * This routine does a 593 op-code number 7, and obtains the diagnose
942  * status for the WaveLAN.
943  */
944 static inline int
945 wv_diag(struct net_device *     dev)
946 {
947   int           ret = FALSE;
948
949   if(wv_82593_cmd(dev, "wv_diag(): diagnose",
950                   OP0_DIAGNOSE, SR0_DIAGNOSE_PASSED))
951     ret = TRUE;
952
953 #ifdef DEBUG_CONFIG_ERROR
954   printk(KERN_INFO "wavelan_cs: i82593 Self Test failed!\n");
955 #endif
956   return(ret);
957 } /* wv_diag */
958
959 /*------------------------------------------------------------------*/
960 /*
961  * Routine to read len bytes from the i82593's ring buffer, starting at
962  * chip address addr. The results read from the chip are stored in buf.
963  * The return value is the address to use for next the call.
964  */
965 static int
966 read_ringbuf(struct net_device *        dev,
967              int        addr,
968              char *     buf,
969              int        len)
970 {
971   ioaddr_t      base = dev->base_addr;
972   int           ring_ptr = addr;
973   int           chunk_len;
974   char *        buf_ptr = buf;
975
976   /* Get all the buffer */
977   while(len > 0)
978     {
979       /* Position the Program I/O Register at the ring buffer pointer */
980       outb(ring_ptr & 0xff, PIORL(base));
981       outb(((ring_ptr >> 8) & PIORH_MASK), PIORH(base));
982
983       /* First, determine how much we can read without wrapping around the
984          ring buffer */
985       if((addr + len) < (RX_BASE + RX_SIZE))
986         chunk_len = len;
987       else
988         chunk_len = RX_BASE + RX_SIZE - addr;
989       insb(PIOP(base), buf_ptr, chunk_len);
990       buf_ptr += chunk_len;
991       len -= chunk_len;
992       ring_ptr = (ring_ptr - RX_BASE + chunk_len) % RX_SIZE + RX_BASE;
993     }
994   return(ring_ptr);
995 } /* read_ringbuf */
996
997 /*------------------------------------------------------------------*/
998 /*
999  * Reconfigure the i82593, or at least ask for it...
1000  * Because wv_82593_config use the transmission buffer, we must do it
1001  * when we are sure that there is no transmission, so we do it now
1002  * or in wavelan_packet_xmit() (I can't find any better place,
1003  * wavelan_interrupt is not an option...), so you may experience
1004  * some delay sometime...
1005  */
1006 static inline void
1007 wv_82593_reconfig(struct net_device *   dev)
1008 {
1009   net_local *           lp = netdev_priv(dev);
1010   dev_link_t *          link = lp->link;
1011   unsigned long         flags;
1012
1013   /* Arm the flag, will be cleard in wv_82593_config() */
1014   lp->reconfig_82593 = TRUE;
1015
1016   /* Check if we can do it now ! */
1017   if((link->open) && (netif_running(dev)) && !(netif_queue_stopped(dev)))
1018     {
1019       spin_lock_irqsave(&lp->spinlock, flags);  /* Disable interrupts */
1020       wv_82593_config(dev);
1021       spin_unlock_irqrestore(&lp->spinlock, flags);     /* Re-enable interrupts */
1022     }
1023   else
1024     {
1025 #ifdef DEBUG_IOCTL_INFO
1026       printk(KERN_DEBUG
1027              "%s: wv_82593_reconfig(): delayed (state = %lX, link = %d)\n",
1028              dev->name, dev->state, link->open);
1029 #endif
1030     }
1031 }
1032
1033 /********************* DEBUG & INFO SUBROUTINES *********************/
1034 /*
1035  * This routines are used in the code to show debug informations.
1036  * Most of the time, it dump the content of hardware structures...
1037  */
1038
1039 #ifdef DEBUG_PSA_SHOW
1040 /*------------------------------------------------------------------*/
1041 /*
1042  * Print the formatted contents of the Parameter Storage Area.
1043  */
1044 static void
1045 wv_psa_show(psa_t *     p)
1046 {
1047   printk(KERN_DEBUG "##### wavelan psa contents: #####\n");
1048   printk(KERN_DEBUG "psa_io_base_addr_1: 0x%02X %02X %02X %02X\n",
1049          p->psa_io_base_addr_1,
1050          p->psa_io_base_addr_2,
1051          p->psa_io_base_addr_3,
1052          p->psa_io_base_addr_4);
1053   printk(KERN_DEBUG "psa_rem_boot_addr_1: 0x%02X %02X %02X\n",
1054          p->psa_rem_boot_addr_1,
1055          p->psa_rem_boot_addr_2,
1056          p->psa_rem_boot_addr_3);
1057   printk(KERN_DEBUG "psa_holi_params: 0x%02x, ", p->psa_holi_params);
1058   printk("psa_int_req_no: %d\n", p->psa_int_req_no);
1059 #ifdef DEBUG_SHOW_UNUSED
1060   printk(KERN_DEBUG "psa_unused0[]: %02X:%02X:%02X:%02X:%02X:%02X:%02X\n",
1061          p->psa_unused0[0],
1062          p->psa_unused0[1],
1063          p->psa_unused0[2],
1064          p->psa_unused0[3],
1065          p->psa_unused0[4],
1066          p->psa_unused0[5],
1067          p->psa_unused0[6]);
1068 #endif  /* DEBUG_SHOW_UNUSED */
1069   printk(KERN_DEBUG "psa_univ_mac_addr[]: %02x:%02x:%02x:%02x:%02x:%02x\n",
1070          p->psa_univ_mac_addr[0],
1071          p->psa_univ_mac_addr[1],
1072          p->psa_univ_mac_addr[2],
1073          p->psa_univ_mac_addr[3],
1074          p->psa_univ_mac_addr[4],
1075          p->psa_univ_mac_addr[5]);
1076   printk(KERN_DEBUG "psa_local_mac_addr[]: %02x:%02x:%02x:%02x:%02x:%02x\n",
1077          p->psa_local_mac_addr[0],
1078          p->psa_local_mac_addr[1],
1079          p->psa_local_mac_addr[2],
1080          p->psa_local_mac_addr[3],
1081          p->psa_local_mac_addr[4],
1082          p->psa_local_mac_addr[5]);
1083   printk(KERN_DEBUG "psa_univ_local_sel: %d, ", p->psa_univ_local_sel);
1084   printk("psa_comp_number: %d, ", p->psa_comp_number);
1085   printk("psa_thr_pre_set: 0x%02x\n", p->psa_thr_pre_set);
1086   printk(KERN_DEBUG "psa_feature_select/decay_prm: 0x%02x, ",
1087          p->psa_feature_select);
1088   printk("psa_subband/decay_update_prm: %d\n", p->psa_subband);
1089   printk(KERN_DEBUG "psa_quality_thr: 0x%02x, ", p->psa_quality_thr);
1090   printk("psa_mod_delay: 0x%02x\n", p->psa_mod_delay);
1091   printk(KERN_DEBUG "psa_nwid: 0x%02x%02x, ", p->psa_nwid[0], p->psa_nwid[1]);
1092   printk("psa_nwid_select: %d\n", p->psa_nwid_select);
1093   printk(KERN_DEBUG "psa_encryption_select: %d, ", p->psa_encryption_select);
1094   printk("psa_encryption_key[]: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
1095          p->psa_encryption_key[0],
1096          p->psa_encryption_key[1],
1097          p->psa_encryption_key[2],
1098          p->psa_encryption_key[3],
1099          p->psa_encryption_key[4],
1100          p->psa_encryption_key[5],
1101          p->psa_encryption_key[6],
1102          p->psa_encryption_key[7]);
1103   printk(KERN_DEBUG "psa_databus_width: %d\n", p->psa_databus_width);
1104   printk(KERN_DEBUG "psa_call_code/auto_squelch: 0x%02x, ",
1105          p->psa_call_code[0]);
1106   printk("psa_call_code[]: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n",
1107          p->psa_call_code[0],
1108          p->psa_call_code[1],
1109          p->psa_call_code[2],
1110          p->psa_call_code[3],
1111          p->psa_call_code[4],
1112          p->psa_call_code[5],
1113          p->psa_call_code[6],
1114          p->psa_call_code[7]);
1115 #ifdef DEBUG_SHOW_UNUSED
1116   printk(KERN_DEBUG "psa_reserved[]: %02X:%02X:%02X:%02X\n",
1117          p->psa_reserved[0],
1118          p->psa_reserved[1],
1119          p->psa_reserved[2],
1120          p->psa_reserved[3]);
1121 #endif  /* DEBUG_SHOW_UNUSED */
1122   printk(KERN_DEBUG "psa_conf_status: %d, ", p->psa_conf_status);
1123   printk("psa_crc: 0x%02x%02x, ", p->psa_crc[0], p->psa_crc[1]);
1124   printk("psa_crc_status: 0x%02x\n", p->psa_crc_status);
1125 } /* wv_psa_show */
1126 #endif  /* DEBUG_PSA_SHOW */
1127
1128 #ifdef DEBUG_MMC_SHOW
1129 /*------------------------------------------------------------------*/
1130 /*
1131  * Print the formatted status of the Modem Management Controller.
1132  * This function need to be completed...
1133  */
1134 static void
1135 wv_mmc_show(struct net_device * dev)
1136 {
1137   ioaddr_t      base = dev->base_addr;
1138   net_local *   lp = netdev_priv(dev);
1139   mmr_t         m;
1140
1141   /* Basic check */
1142   if(hasr_read(base) & HASR_NO_CLK)
1143     {
1144       printk(KERN_WARNING "%s: wv_mmc_show: modem not connected\n",
1145              dev->name);
1146       return;
1147     }
1148
1149   spin_lock_irqsave(&lp->spinlock, flags);
1150
1151   /* Read the mmc */
1152   mmc_out(base, mmwoff(0, mmw_freeze), 1);
1153   mmc_read(base, 0, (u_char *)&m, sizeof(m));
1154   mmc_out(base, mmwoff(0, mmw_freeze), 0);
1155
1156 #ifdef WIRELESS_EXT     /* If wireless extension exist in the kernel */
1157   /* Don't forget to update statistics */
1158   lp->wstats.discard.nwid += (m.mmr_wrong_nwid_h << 8) | m.mmr_wrong_nwid_l;
1159 #endif  /* WIRELESS_EXT */
1160
1161   spin_unlock_irqrestore(&lp->spinlock, flags);
1162
1163   printk(KERN_DEBUG "##### wavelan modem status registers: #####\n");
1164 #ifdef DEBUG_SHOW_UNUSED
1165   printk(KERN_DEBUG "mmc_unused0[]: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n",
1166          m.mmr_unused0[0],
1167          m.mmr_unused0[1],
1168          m.mmr_unused0[2],
1169          m.mmr_unused0[3],
1170          m.mmr_unused0[4],
1171          m.mmr_unused0[5],
1172          m.mmr_unused0[6],
1173          m.mmr_unused0[7]);
1174 #endif  /* DEBUG_SHOW_UNUSED */
1175   printk(KERN_DEBUG "Encryption algorythm: %02X - Status: %02X\n",
1176          m.mmr_des_avail, m.mmr_des_status);
1177 #ifdef DEBUG_SHOW_UNUSED
1178   printk(KERN_DEBUG "mmc_unused1[]: %02X:%02X:%02X:%02X:%02X\n",
1179          m.mmr_unused1[0],
1180          m.mmr_unused1[1],
1181          m.mmr_unused1[2],
1182          m.mmr_unused1[3],
1183          m.mmr_unused1[4]);
1184 #endif  /* DEBUG_SHOW_UNUSED */
1185   printk(KERN_DEBUG "dce_status: 0x%x [%s%s%s%s]\n",
1186          m.mmr_dce_status,
1187          (m.mmr_dce_status & MMR_DCE_STATUS_RX_BUSY) ? "energy detected,":"",
1188          (m.mmr_dce_status & MMR_DCE_STATUS_LOOPT_IND) ?
1189          "loop test indicated," : "",
1190          (m.mmr_dce_status & MMR_DCE_STATUS_TX_BUSY) ? "transmitter on," : "",
1191          (m.mmr_dce_status & MMR_DCE_STATUS_JBR_EXPIRED) ?
1192          "jabber timer expired," : "");
1193   printk(KERN_DEBUG "Dsp ID: %02X\n",
1194          m.mmr_dsp_id);
1195 #ifdef DEBUG_SHOW_UNUSED
1196   printk(KERN_DEBUG "mmc_unused2[]: %02X:%02X\n",
1197          m.mmr_unused2[0],
1198          m.mmr_unused2[1]);
1199 #endif  /* DEBUG_SHOW_UNUSED */
1200   printk(KERN_DEBUG "# correct_nwid: %d, # wrong_nwid: %d\n",
1201          (m.mmr_correct_nwid_h << 8) | m.mmr_correct_nwid_l,
1202          (m.mmr_wrong_nwid_h << 8) | m.mmr_wrong_nwid_l);
1203   printk(KERN_DEBUG "thr_pre_set: 0x%x [current signal %s]\n",
1204          m.mmr_thr_pre_set & MMR_THR_PRE_SET,
1205          (m.mmr_thr_pre_set & MMR_THR_PRE_SET_CUR) ? "above" : "below");
1206   printk(KERN_DEBUG "signal_lvl: %d [%s], ",
1207          m.mmr_signal_lvl & MMR_SIGNAL_LVL,
1208          (m.mmr_signal_lvl & MMR_SIGNAL_LVL_VALID) ? "new msg" : "no new msg");
1209   printk("silence_lvl: %d [%s], ", m.mmr_silence_lvl & MMR_SILENCE_LVL,
1210          (m.mmr_silence_lvl & MMR_SILENCE_LVL_VALID) ? "update done" : "no new update");
1211   printk("sgnl_qual: 0x%x [%s]\n", m.mmr_sgnl_qual & MMR_SGNL_QUAL,
1212          (m.mmr_sgnl_qual & MMR_SGNL_QUAL_ANT) ? "Antenna 1" : "Antenna 0");
1213 #ifdef DEBUG_SHOW_UNUSED
1214   printk(KERN_DEBUG "netw_id_l: %x\n", m.mmr_netw_id_l);
1215 #endif  /* DEBUG_SHOW_UNUSED */
1216 } /* wv_mmc_show */
1217 #endif  /* DEBUG_MMC_SHOW */
1218
1219 #ifdef DEBUG_I82593_SHOW
1220 /*------------------------------------------------------------------*/
1221 /*
1222  * Print the formatted status of the i82593's receive unit.
1223  */
1224 static void
1225 wv_ru_show(struct net_device *  dev)
1226 {
1227   net_local *lp = netdev_priv(dev);
1228
1229   printk(KERN_DEBUG "##### wavelan i82593 receiver status: #####\n");
1230   printk(KERN_DEBUG "ru: rfp %d stop %d", lp->rfp, lp->stop);
1231   /*
1232    * Not implemented yet...
1233    */
1234   printk("\n");
1235 } /* wv_ru_show */
1236 #endif  /* DEBUG_I82593_SHOW */
1237
1238 #ifdef DEBUG_DEVICE_SHOW
1239 /*------------------------------------------------------------------*/
1240 /*
1241  * Print the formatted status of the WaveLAN PCMCIA device driver.
1242  */
1243 static void
1244 wv_dev_show(struct net_device * dev)
1245 {
1246   printk(KERN_DEBUG "dev:");
1247   printk(" state=%lX,", dev->state);
1248   printk(" trans_start=%ld,", dev->trans_start);
1249   printk(" flags=0x%x,", dev->flags);
1250   printk("\n");
1251 } /* wv_dev_show */
1252
1253 /*------------------------------------------------------------------*/
1254 /*
1255  * Print the formatted status of the WaveLAN PCMCIA device driver's
1256  * private information.
1257  */
1258 static void
1259 wv_local_show(struct net_device *       dev)
1260 {
1261   net_local *lp = netdev_priv(dev);
1262
1263   printk(KERN_DEBUG "local:");
1264   /*
1265    * Not implemented yet...
1266    */
1267   printk("\n");
1268 } /* wv_local_show */
1269 #endif  /* DEBUG_DEVICE_SHOW */
1270
1271 #if defined(DEBUG_RX_INFO) || defined(DEBUG_TX_INFO)
1272 /*------------------------------------------------------------------*/
1273 /*
1274  * Dump packet header (and content if necessary) on the screen
1275  */
1276 static inline void
1277 wv_packet_info(u_char *         p,              /* Packet to dump */
1278                int              length,         /* Length of the packet */
1279                char *           msg1,           /* Name of the device */
1280                char *           msg2)           /* Name of the function */
1281 {
1282   int           i;
1283   int           maxi;
1284
1285   printk(KERN_DEBUG "%s: %s(): dest %02X:%02X:%02X:%02X:%02X:%02X, length %d\n",
1286          msg1, msg2, p[0], p[1], p[2], p[3], p[4], p[5], length);
1287   printk(KERN_DEBUG "%s: %s(): src %02X:%02X:%02X:%02X:%02X:%02X, type 0x%02X%02X\n",
1288          msg1, msg2, p[6], p[7], p[8], p[9], p[10], p[11], p[12], p[13]);
1289
1290 #ifdef DEBUG_PACKET_DUMP
1291
1292   printk(KERN_DEBUG "data=\"");
1293
1294   if((maxi = length) > DEBUG_PACKET_DUMP)
1295     maxi = DEBUG_PACKET_DUMP;
1296   for(i = 14; i < maxi; i++)
1297     if(p[i] >= ' ' && p[i] <= '~')
1298       printk(" %c", p[i]);
1299     else
1300       printk("%02X", p[i]);
1301   if(maxi < length)
1302     printk("..");
1303   printk("\"\n");
1304   printk(KERN_DEBUG "\n");
1305 #endif  /* DEBUG_PACKET_DUMP */
1306 }
1307 #endif  /* defined(DEBUG_RX_INFO) || defined(DEBUG_TX_INFO) */
1308
1309 /*------------------------------------------------------------------*/
1310 /*
1311  * This is the information which is displayed by the driver at startup
1312  * There  is a lot of flag to configure it at your will...
1313  */
1314 static inline void
1315 wv_init_info(struct net_device *        dev)
1316 {
1317   ioaddr_t      base = dev->base_addr;
1318   psa_t         psa;
1319   int           i;
1320
1321   /* Read the parameter storage area */
1322   psa_read(dev, 0, (unsigned char *) &psa, sizeof(psa));
1323
1324 #ifdef DEBUG_PSA_SHOW
1325   wv_psa_show(&psa);
1326 #endif
1327 #ifdef DEBUG_MMC_SHOW
1328   wv_mmc_show(dev);
1329 #endif
1330 #ifdef DEBUG_I82593_SHOW
1331   wv_ru_show(dev);
1332 #endif
1333
1334 #ifdef DEBUG_BASIC_SHOW
1335   /* Now, let's go for the basic stuff */
1336   printk(KERN_NOTICE "%s: WaveLAN: port %#x, irq %d, hw_addr",
1337          dev->name, base, dev->irq);
1338   for(i = 0; i < WAVELAN_ADDR_SIZE; i++)
1339     printk("%s%02X", (i == 0) ? " " : ":", dev->dev_addr[i]);
1340
1341   /* Print current network id */
1342   if(psa.psa_nwid_select)
1343     printk(", nwid 0x%02X-%02X", psa.psa_nwid[0], psa.psa_nwid[1]);
1344   else
1345     printk(", nwid off");
1346
1347   /* If 2.00 card */
1348   if(!(mmc_in(base, mmroff(0, mmr_fee_status)) &
1349        (MMR_FEE_STATUS_DWLD | MMR_FEE_STATUS_BUSY)))
1350     {
1351       unsigned short    freq;
1352
1353       /* Ask the EEprom to read the frequency from the first area */
1354       fee_read(base, 0x00 /* 1st area - frequency... */,
1355                &freq, 1);
1356
1357       /* Print frequency */
1358       printk(", 2.00, %ld", (freq >> 6) + 2400L);
1359
1360       /* Hack !!! */
1361       if(freq & 0x20)
1362         printk(".5");
1363     }
1364   else
1365     {
1366       printk(", PCMCIA, ");
1367       switch (psa.psa_subband)
1368         {
1369         case PSA_SUBBAND_915:
1370           printk("915");
1371           break;
1372         case PSA_SUBBAND_2425:
1373           printk("2425");
1374           break;
1375         case PSA_SUBBAND_2460:
1376           printk("2460");
1377           break;
1378         case PSA_SUBBAND_2484:
1379           printk("2484");
1380           break;
1381         case PSA_SUBBAND_2430_5:
1382           printk("2430.5");
1383           break;
1384         default:
1385           printk("unknown");
1386         }
1387     }
1388
1389   printk(" MHz\n");
1390 #endif  /* DEBUG_BASIC_SHOW */
1391
1392 #ifdef DEBUG_VERSION_SHOW
1393   /* Print version information */
1394   printk(KERN_NOTICE "%s", version);
1395 #endif
1396 } /* wv_init_info */
1397
1398 /********************* IOCTL, STATS & RECONFIG *********************/
1399 /*
1400  * We found here routines that are called by Linux on differents
1401  * occasions after the configuration and not for transmitting data
1402  * These may be called when the user use ifconfig, /proc/net/dev
1403  * or wireless extensions
1404  */
1405
1406 /*------------------------------------------------------------------*/
1407 /*
1408  * Get the current ethernet statistics. This may be called with the
1409  * card open or closed.
1410  * Used when the user read /proc/net/dev
1411  */
1412 static en_stats *
1413 wavelan_get_stats(struct net_device *   dev)
1414 {
1415 #ifdef DEBUG_IOCTL_TRACE
1416   printk(KERN_DEBUG "%s: <>wavelan_get_stats()\n", dev->name);
1417 #endif
1418
1419   return(&((net_local *)netdev_priv(dev))->stats);
1420 }
1421
1422 /*------------------------------------------------------------------*/
1423 /*
1424  * Set or clear the multicast filter for this adaptor.
1425  * num_addrs == -1      Promiscuous mode, receive all packets
1426  * num_addrs == 0       Normal mode, clear multicast list
1427  * num_addrs > 0        Multicast mode, receive normal and MC packets,
1428  *                      and do best-effort filtering.
1429  */
1430
1431 static void
1432 wavelan_set_multicast_list(struct net_device *  dev)
1433 {
1434   net_local *   lp = netdev_priv(dev);
1435
1436 #ifdef DEBUG_IOCTL_TRACE
1437   printk(KERN_DEBUG "%s: ->wavelan_set_multicast_list()\n", dev->name);
1438 #endif
1439
1440 #ifdef DEBUG_IOCTL_INFO
1441   printk(KERN_DEBUG "%s: wavelan_set_multicast_list(): setting Rx mode %02X to %d addresses.\n",
1442          dev->name, dev->flags, dev->mc_count);
1443 #endif
1444
1445   if(dev->flags & IFF_PROMISC)
1446     {
1447       /*
1448        * Enable promiscuous mode: receive all packets.
1449        */
1450       if(!lp->promiscuous)
1451         {
1452           lp->promiscuous = 1;
1453           lp->allmulticast = 0;
1454           lp->mc_count = 0;
1455
1456           wv_82593_reconfig(dev);
1457
1458           /* Tell the kernel that we are doing a really bad job... */
1459           dev->flags |= IFF_PROMISC;
1460         }
1461     }
1462   else
1463     /* If all multicast addresses
1464      * or too much multicast addresses for the hardware filter */
1465     if((dev->flags & IFF_ALLMULTI) ||
1466        (dev->mc_count > I82593_MAX_MULTICAST_ADDRESSES))
1467       {
1468         /*
1469          * Disable promiscuous mode, but active the all multicast mode
1470          */
1471         if(!lp->allmulticast)
1472           {
1473             lp->promiscuous = 0;
1474             lp->allmulticast = 1;
1475             lp->mc_count = 0;
1476
1477             wv_82593_reconfig(dev);
1478
1479             /* Tell the kernel that we are doing a really bad job... */
1480             dev->flags |= IFF_ALLMULTI;
1481           }
1482       }
1483     else
1484       /* If there is some multicast addresses to send */
1485       if(dev->mc_list != (struct dev_mc_list *) NULL)
1486         {
1487           /*
1488            * Disable promiscuous mode, but receive all packets
1489            * in multicast list
1490            */
1491 #ifdef MULTICAST_AVOID
1492           if(lp->promiscuous || lp->allmulticast ||
1493              (dev->mc_count != lp->mc_count))
1494 #endif
1495             {
1496               lp->promiscuous = 0;
1497               lp->allmulticast = 0;
1498               lp->mc_count = dev->mc_count;
1499
1500               wv_82593_reconfig(dev);
1501             }
1502         }
1503       else
1504         {
1505           /*
1506            * Switch to normal mode: disable promiscuous mode and 
1507            * clear the multicast list.
1508            */
1509           if(lp->promiscuous || lp->mc_count == 0)
1510             {
1511               lp->promiscuous = 0;
1512               lp->allmulticast = 0;
1513               lp->mc_count = 0;
1514
1515               wv_82593_reconfig(dev);
1516             }
1517         }
1518 #ifdef DEBUG_IOCTL_TRACE
1519   printk(KERN_DEBUG "%s: <-wavelan_set_multicast_list()\n", dev->name);
1520 #endif
1521 }
1522
1523 /*------------------------------------------------------------------*/
1524 /*
1525  * This function doesn't exist...
1526  * (Note : it was a nice way to test the reconfigure stuff...)
1527  */
1528 #ifdef SET_MAC_ADDRESS
1529 static int
1530 wavelan_set_mac_address(struct net_device *     dev,
1531                         void *          addr)
1532 {
1533   struct sockaddr *     mac = addr;
1534
1535   /* Copy the address */
1536   memcpy(dev->dev_addr, mac->sa_data, WAVELAN_ADDR_SIZE);
1537
1538   /* Reconfig the beast */
1539   wv_82593_reconfig(dev);
1540
1541   return 0;
1542 }
1543 #endif  /* SET_MAC_ADDRESS */
1544
1545 #ifdef WIRELESS_EXT     /* If wireless extension exist in the kernel */
1546
1547 /*------------------------------------------------------------------*/
1548 /*
1549  * Frequency setting (for hardware able of it)
1550  * It's a bit complicated and you don't really want to look into it...
1551  */
1552 static inline int
1553 wv_set_frequency(u_long         base,   /* i/o port of the card */
1554                  iw_freq *      frequency)
1555 {
1556   const int     BAND_NUM = 10;  /* Number of bands */
1557   long          freq = 0L;      /* offset to 2.4 GHz in .5 MHz */
1558 #ifdef DEBUG_IOCTL_INFO
1559   int           i;
1560 #endif
1561
1562   /* Setting by frequency */
1563   /* Theoritically, you may set any frequency between
1564    * the two limits with a 0.5 MHz precision. In practice,
1565    * I don't want you to have trouble with local
1566    * regulations... */
1567   if((frequency->e == 1) &&
1568      (frequency->m >= (int) 2.412e8) && (frequency->m <= (int) 2.487e8))
1569     {
1570       freq = ((frequency->m / 10000) - 24000L) / 5;
1571     }
1572
1573   /* Setting by channel (same as wfreqsel) */
1574   /* Warning : each channel is 22MHz wide, so some of the channels
1575    * will interfere... */
1576   if((frequency->e == 0) &&
1577      (frequency->m >= 0) && (frequency->m < BAND_NUM))
1578     {
1579       /* Get frequency offset. */
1580       freq = channel_bands[frequency->m] >> 1;
1581     }
1582
1583   /* Verify if the frequency is allowed */
1584   if(freq != 0L)
1585     {
1586       u_short   table[10];      /* Authorized frequency table */
1587
1588       /* Read the frequency table */
1589       fee_read(base, 0x71 /* frequency table */,
1590                table, 10);
1591
1592 #ifdef DEBUG_IOCTL_INFO
1593       printk(KERN_DEBUG "Frequency table :");
1594       for(i = 0; i < 10; i++)
1595         {
1596           printk(" %04X",
1597                  table[i]);
1598         }
1599       printk("\n");
1600 #endif
1601
1602       /* Look in the table if the frequency is allowed */
1603       if(!(table[9 - ((freq - 24) / 16)] &
1604            (1 << ((freq - 24) % 16))))
1605         return -EINVAL;         /* not allowed */
1606     }
1607   else
1608     return -EINVAL;
1609
1610   /* If we get a usable frequency */
1611   if(freq != 0L)
1612     {
1613       unsigned short    area[16];
1614       unsigned short    dac[2];
1615       unsigned short    area_verify[16];
1616       unsigned short    dac_verify[2];
1617       /* Corresponding gain (in the power adjust value table)
1618        * see AT&T Wavelan Data Manual, REF 407-024689/E, page 3-8
1619        * & WCIN062D.DOC, page 6.2.9 */
1620       unsigned short    power_limit[] = { 40, 80, 120, 160, 0 };
1621       int               power_band = 0;         /* Selected band */
1622       unsigned short    power_adjust;           /* Correct value */
1623
1624       /* Search for the gain */
1625       power_band = 0;
1626       while((freq > power_limit[power_band]) &&
1627             (power_limit[++power_band] != 0))
1628         ;
1629
1630       /* Read the first area */
1631       fee_read(base, 0x00,
1632                area, 16);
1633
1634       /* Read the DAC */
1635       fee_read(base, 0x60,
1636                dac, 2);
1637
1638       /* Read the new power adjust value */
1639       fee_read(base, 0x6B - (power_band >> 1),
1640                &power_adjust, 1);
1641       if(power_band & 0x1)
1642         power_adjust >>= 8;
1643       else
1644         power_adjust &= 0xFF;
1645
1646 #ifdef DEBUG_IOCTL_INFO
1647       printk(KERN_DEBUG "Wavelan EEprom Area 1 :");
1648       for(i = 0; i < 16; i++)
1649         {
1650           printk(" %04X",
1651                  area[i]);
1652         }
1653       printk("\n");
1654
1655       printk(KERN_DEBUG "Wavelan EEprom DAC : %04X %04X\n",
1656              dac[0], dac[1]);
1657 #endif
1658
1659       /* Frequency offset (for info only...) */
1660       area[0] = ((freq << 5) & 0xFFE0) | (area[0] & 0x1F);
1661
1662       /* Receiver Principle main divider coefficient */
1663       area[3] = (freq >> 1) + 2400L - 352L;
1664       area[2] = ((freq & 0x1) << 4) | (area[2] & 0xFFEF);
1665
1666       /* Transmitter Main divider coefficient */
1667       area[13] = (freq >> 1) + 2400L;
1668       area[12] = ((freq & 0x1) << 4) | (area[2] & 0xFFEF);
1669
1670       /* Others part of the area are flags, bit streams or unused... */
1671
1672       /* Set the value in the DAC */
1673       dac[1] = ((power_adjust >> 1) & 0x7F) | (dac[1] & 0xFF80);
1674       dac[0] = ((power_adjust & 0x1) << 4) | (dac[0] & 0xFFEF);
1675
1676       /* Write the first area */
1677       fee_write(base, 0x00,
1678                 area, 16);
1679
1680       /* Write the DAC */
1681       fee_write(base, 0x60,
1682                 dac, 2);
1683
1684       /* We now should verify here that the EEprom writting was ok */
1685
1686       /* ReRead the first area */
1687       fee_read(base, 0x00,
1688                area_verify, 16);
1689
1690       /* ReRead the DAC */
1691       fee_read(base, 0x60,
1692                dac_verify, 2);
1693
1694       /* Compare */
1695       if(memcmp(area, area_verify, 16 * 2) ||
1696          memcmp(dac, dac_verify, 2 * 2))
1697         {
1698 #ifdef DEBUG_IOCTL_ERROR
1699           printk(KERN_INFO "Wavelan: wv_set_frequency : unable to write new frequency to EEprom (?)\n");
1700 #endif
1701           return -EOPNOTSUPP;
1702         }
1703
1704       /* We must download the frequency parameters to the
1705        * synthetisers (from the EEprom - area 1)
1706        * Note : as the EEprom is auto decremented, we set the end
1707        * if the area... */
1708       mmc_out(base, mmwoff(0, mmw_fee_addr), 0x0F);
1709       mmc_out(base, mmwoff(0, mmw_fee_ctrl),
1710               MMW_FEE_CTRL_READ | MMW_FEE_CTRL_DWLD);
1711
1712       /* Wait until the download is finished */
1713       fee_wait(base, 100, 100);
1714
1715       /* We must now download the power adjust value (gain) to
1716        * the synthetisers (from the EEprom - area 7 - DAC) */
1717       mmc_out(base, mmwoff(0, mmw_fee_addr), 0x61);
1718       mmc_out(base, mmwoff(0, mmw_fee_ctrl),
1719               MMW_FEE_CTRL_READ | MMW_FEE_CTRL_DWLD);
1720
1721       /* Wait until the download is finished */
1722       fee_wait(base, 100, 100);
1723
1724 #ifdef DEBUG_IOCTL_INFO
1725       /* Verification of what we have done... */
1726
1727       printk(KERN_DEBUG "Wavelan EEprom Area 1 :");
1728       for(i = 0; i < 16; i++)
1729         {
1730           printk(" %04X",
1731                  area_verify[i]);
1732         }
1733       printk("\n");
1734
1735       printk(KERN_DEBUG "Wavelan EEprom DAC : %04X %04X\n",
1736              dac_verify[0], dac_verify[1]);
1737 #endif
1738
1739       return 0;
1740     }
1741   else
1742     return -EINVAL;             /* Bah, never get there... */
1743 }
1744
1745 /*------------------------------------------------------------------*/
1746 /*
1747  * Give the list of available frequencies
1748  */
1749 static inline int
1750 wv_frequency_list(u_long        base,   /* i/o port of the card */
1751                   iw_freq *     list,   /* List of frequency to fill */
1752                   int           max)    /* Maximum number of frequencies */
1753 {
1754   u_short       table[10];      /* Authorized frequency table */
1755   long          freq = 0L;      /* offset to 2.4 GHz in .5 MHz + 12 MHz */
1756   int           i;              /* index in the table */
1757   const int     BAND_NUM = 10;  /* Number of bands */
1758   int           c = 0;          /* Channel number */
1759
1760   /* Read the frequency table */
1761   fee_read(base, 0x71 /* frequency table */,
1762            table, 10);
1763
1764   /* Look all frequencies */
1765   i = 0;
1766   for(freq = 0; freq < 150; freq++)
1767     /* Look in the table if the frequency is allowed */
1768     if(table[9 - (freq / 16)] & (1 << (freq % 16)))
1769       {
1770         /* Compute approximate channel number */
1771         while((((channel_bands[c] >> 1) - 24) < freq) &&
1772               (c < BAND_NUM))
1773           c++;
1774         list[i].i = c;  /* Set the list index */
1775
1776         /* put in the list */
1777         list[i].m = (((freq + 24) * 5) + 24000L) * 10000;
1778         list[i++].e = 1;
1779
1780         /* Check number */
1781         if(i >= max)
1782           return(i);
1783       }
1784
1785   return(i);
1786 }
1787
1788 #ifdef IW_WIRELESS_SPY
1789 /*------------------------------------------------------------------*/
1790 /*
1791  * Gather wireless spy statistics : for each packet, compare the source
1792  * address with out list, and if match, get the stats...
1793  * Sorry, but this function really need wireless extensions...
1794  */
1795 static inline void
1796 wl_spy_gather(struct net_device *       dev,
1797               u_char *  mac,            /* MAC address */
1798               u_char *  stats)          /* Statistics to gather */
1799 {
1800   struct iw_quality wstats;
1801
1802   wstats.qual = stats[2] & MMR_SGNL_QUAL;
1803   wstats.level = stats[0] & MMR_SIGNAL_LVL;
1804   wstats.noise = stats[1] & MMR_SILENCE_LVL;
1805   wstats.updated = 0x7;
1806
1807   /* Update spy records */
1808   wireless_spy_update(dev, mac, &wstats);
1809 }
1810 #endif  /* IW_WIRELESS_SPY */
1811
1812 #ifdef HISTOGRAM
1813 /*------------------------------------------------------------------*/
1814 /*
1815  * This function calculate an histogram on the signal level.
1816  * As the noise is quite constant, it's like doing it on the SNR.
1817  * We have defined a set of interval (lp->his_range), and each time
1818  * the level goes in that interval, we increment the count (lp->his_sum).
1819  * With this histogram you may detect if one wavelan is really weak,
1820  * or you may also calculate the mean and standard deviation of the level...
1821  */
1822 static inline void
1823 wl_his_gather(struct net_device *       dev,
1824               u_char *  stats)          /* Statistics to gather */
1825 {
1826   net_local *   lp = netdev_priv(dev);
1827   u_char        level = stats[0] & MMR_SIGNAL_LVL;
1828   int           i;
1829
1830   /* Find the correct interval */
1831   i = 0;
1832   while((i < (lp->his_number - 1)) && (level >= lp->his_range[i++]))
1833     ;
1834
1835   /* Increment interval counter */
1836   (lp->his_sum[i])++;
1837 }
1838 #endif  /* HISTOGRAM */
1839
1840 static void wl_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1841 {
1842         strncpy(info->driver, "wavelan_cs", sizeof(info->driver)-1);
1843 }
1844
1845 static struct ethtool_ops ops = {
1846         .get_drvinfo = wl_get_drvinfo
1847 };
1848
1849 /*------------------------------------------------------------------*/
1850 /*
1851  * Wireless Handler : get protocol name
1852  */
1853 static int wavelan_get_name(struct net_device *dev,
1854                             struct iw_request_info *info,
1855                             union iwreq_data *wrqu,
1856                             char *extra)
1857 {
1858         strcpy(wrqu->name, "WaveLAN");
1859         return 0;
1860 }
1861
1862 /*------------------------------------------------------------------*/
1863 /*
1864  * Wireless Handler : set NWID
1865  */
1866 static int wavelan_set_nwid(struct net_device *dev,
1867                             struct iw_request_info *info,
1868                             union iwreq_data *wrqu,
1869                             char *extra)
1870 {
1871         ioaddr_t base = dev->base_addr;
1872         net_local *lp = netdev_priv(dev);
1873         psa_t psa;
1874         mm_t m;
1875         unsigned long flags;
1876         int ret = 0;
1877
1878         /* Disable interrupts and save flags. */
1879         spin_lock_irqsave(&lp->spinlock, flags);
1880         
1881         /* Set NWID in WaveLAN. */
1882         if (!wrqu->nwid.disabled) {
1883                 /* Set NWID in psa */
1884                 psa.psa_nwid[0] = (wrqu->nwid.value & 0xFF00) >> 8;
1885                 psa.psa_nwid[1] = wrqu->nwid.value & 0xFF;
1886                 psa.psa_nwid_select = 0x01;
1887                 psa_write(dev,
1888                           (char *) psa.psa_nwid - (char *) &psa,
1889                           (unsigned char *) psa.psa_nwid, 3);
1890
1891                 /* Set NWID in mmc. */
1892                 m.w.mmw_netw_id_l = psa.psa_nwid[1];
1893                 m.w.mmw_netw_id_h = psa.psa_nwid[0];
1894                 mmc_write(base,
1895                           (char *) &m.w.mmw_netw_id_l -
1896                           (char *) &m,
1897                           (unsigned char *) &m.w.mmw_netw_id_l, 2);
1898                 mmc_out(base, mmwoff(0, mmw_loopt_sel), 0x00);
1899         } else {
1900                 /* Disable NWID in the psa. */
1901                 psa.psa_nwid_select = 0x00;
1902                 psa_write(dev,
1903                           (char *) &psa.psa_nwid_select -
1904                           (char *) &psa,
1905                           (unsigned char *) &psa.psa_nwid_select,
1906                           1);
1907
1908                 /* Disable NWID in the mmc (no filtering). */
1909                 mmc_out(base, mmwoff(0, mmw_loopt_sel),
1910                         MMW_LOOPT_SEL_DIS_NWID);
1911         }
1912         /* update the Wavelan checksum */
1913         update_psa_checksum(dev);
1914
1915         /* Enable interrupts and restore flags. */
1916         spin_unlock_irqrestore(&lp->spinlock, flags);
1917
1918         return ret;
1919 }
1920
1921 /*------------------------------------------------------------------*/
1922 /*
1923  * Wireless Handler : get NWID 
1924  */
1925 static int wavelan_get_nwid(struct net_device *dev,
1926                             struct iw_request_info *info,
1927                             union iwreq_data *wrqu,
1928                             char *extra)
1929 {
1930         net_local *lp = netdev_priv(dev);
1931         psa_t psa;
1932         unsigned long flags;
1933         int ret = 0;
1934
1935         /* Disable interrupts and save flags. */
1936         spin_lock_irqsave(&lp->spinlock, flags);
1937         
1938         /* Read the NWID. */
1939         psa_read(dev,
1940                  (char *) psa.psa_nwid - (char *) &psa,
1941                  (unsigned char *) psa.psa_nwid, 3);
1942         wrqu->nwid.value = (psa.psa_nwid[0] << 8) + psa.psa_nwid[1];
1943         wrqu->nwid.disabled = !(psa.psa_nwid_select);
1944         wrqu->nwid.fixed = 1;   /* Superfluous */
1945
1946         /* Enable interrupts and restore flags. */
1947         spin_unlock_irqrestore(&lp->spinlock, flags);
1948
1949         return ret;
1950 }
1951
1952 /*------------------------------------------------------------------*/
1953 /*
1954  * Wireless Handler : set frequency
1955  */
1956 static int wavelan_set_freq(struct net_device *dev,
1957                             struct iw_request_info *info,
1958                             union iwreq_data *wrqu,
1959                             char *extra)
1960 {
1961         ioaddr_t base = dev->base_addr;
1962         net_local *lp = netdev_priv(dev);
1963         unsigned long flags;
1964         int ret;
1965
1966         /* Disable interrupts and save flags. */
1967         spin_lock_irqsave(&lp->spinlock, flags);
1968         
1969         /* Attempt to recognise 2.00 cards (2.4 GHz frequency selectable). */
1970         if (!(mmc_in(base, mmroff(0, mmr_fee_status)) &
1971               (MMR_FEE_STATUS_DWLD | MMR_FEE_STATUS_BUSY)))
1972                 ret = wv_set_frequency(base, &(wrqu->freq));
1973         else
1974                 ret = -EOPNOTSUPP;
1975
1976         /* Enable interrupts and restore flags. */
1977         spin_unlock_irqrestore(&lp->spinlock, flags);
1978
1979         return ret;
1980 }
1981
1982 /*------------------------------------------------------------------*/
1983 /*
1984  * Wireless Handler : get frequency
1985  */
1986 static int wavelan_get_freq(struct net_device *dev,
1987                             struct iw_request_info *info,
1988                             union iwreq_data *wrqu,
1989                             char *extra)
1990 {
1991         ioaddr_t base = dev->base_addr;
1992         net_local *lp = netdev_priv(dev);
1993         psa_t psa;
1994         unsigned long flags;
1995         int ret = 0;
1996
1997         /* Disable interrupts and save flags. */
1998         spin_lock_irqsave(&lp->spinlock, flags);
1999         
2000         /* Attempt to recognise 2.00 cards (2.4 GHz frequency selectable).
2001          * Does it work for everybody, especially old cards? */
2002         if (!(mmc_in(base, mmroff(0, mmr_fee_status)) &
2003               (MMR_FEE_STATUS_DWLD | MMR_FEE_STATUS_BUSY))) {
2004                 unsigned short freq;
2005
2006                 /* Ask the EEPROM to read the frequency from the first area. */
2007                 fee_read(base, 0x00, &freq, 1);
2008                 wrqu->freq.m = ((freq >> 5) * 5 + 24000L) * 10000;
2009                 wrqu->freq.e = 1;
2010         } else {
2011                 psa_read(dev,
2012                          (char *) &psa.psa_subband - (char *) &psa,
2013                          (unsigned char *) &psa.psa_subband, 1);
2014
2015                 if (psa.psa_subband <= 4) {
2016                         wrqu->freq.m = fixed_bands[psa.psa_subband];
2017                         wrqu->freq.e = (psa.psa_subband != 0);
2018                 } else
2019                         ret = -EOPNOTSUPP;
2020         }
2021
2022         /* Enable interrupts and restore flags. */
2023         spin_unlock_irqrestore(&lp->spinlock, flags);
2024
2025         return ret;
2026 }
2027
2028 /*------------------------------------------------------------------*/
2029 /*
2030  * Wireless Handler : set level threshold
2031  */
2032 static int wavelan_set_sens(struct net_device *dev,
2033                             struct iw_request_info *info,
2034                             union iwreq_data *wrqu,
2035                             char *extra)
2036 {
2037         ioaddr_t base = dev->base_addr;
2038         net_local *lp = netdev_priv(dev);
2039         psa_t psa;
2040         unsigned long flags;
2041         int ret = 0;
2042
2043         /* Disable interrupts and save flags. */
2044         spin_lock_irqsave(&lp->spinlock, flags);
2045         
2046         /* Set the level threshold. */
2047         /* We should complain loudly if wrqu->sens.fixed = 0, because we
2048          * can't set auto mode... */
2049         psa.psa_thr_pre_set = wrqu->sens.value & 0x3F;
2050         psa_write(dev,
2051                   (char *) &psa.psa_thr_pre_set - (char *) &psa,
2052                   (unsigned char *) &psa.psa_thr_pre_set, 1);
2053         /* update the Wavelan checksum */
2054         update_psa_checksum(dev);
2055         mmc_out(base, mmwoff(0, mmw_thr_pre_set),
2056                 psa.psa_thr_pre_set);
2057
2058         /* Enable interrupts and restore flags. */
2059         spin_unlock_irqrestore(&lp->spinlock, flags);
2060
2061         return ret;
2062 }
2063
2064 /*------------------------------------------------------------------*/
2065 /*
2066  * Wireless Handler : get level threshold
2067  */
2068 static int wavelan_get_sens(struct net_device *dev,
2069                             struct iw_request_info *info,
2070                             union iwreq_data *wrqu,
2071                             char *extra)
2072 {
2073         net_local *lp = netdev_priv(dev);
2074         psa_t psa;
2075         unsigned long flags;
2076         int ret = 0;
2077
2078         /* Disable interrupts and save flags. */
2079         spin_lock_irqsave(&lp->spinlock, flags);
2080         
2081         /* Read the level threshold. */
2082         psa_read(dev,
2083                  (char *) &psa.psa_thr_pre_set - (char *) &psa,
2084                  (unsigned char *) &psa.psa_thr_pre_set, 1);
2085         wrqu->sens.value = psa.psa_thr_pre_set & 0x3F;
2086         wrqu->sens.fixed = 1;
2087
2088         /* Enable interrupts and restore flags. */
2089         spin_unlock_irqrestore(&lp->spinlock, flags);
2090
2091         return ret;
2092 }
2093
2094 /*------------------------------------------------------------------*/
2095 /*
2096  * Wireless Handler : set encryption key
2097  */
2098 static int wavelan_set_encode(struct net_device *dev,
2099                               struct iw_request_info *info,
2100                               union iwreq_data *wrqu,
2101                               char *extra)
2102 {
2103         ioaddr_t base = dev->base_addr;
2104         net_local *lp = netdev_priv(dev);
2105         unsigned long flags;
2106         psa_t psa;
2107         int ret = 0;
2108
2109         /* Disable interrupts and save flags. */
2110         spin_lock_irqsave(&lp->spinlock, flags);
2111
2112         /* Check if capable of encryption */
2113         if (!mmc_encr(base)) {
2114                 ret = -EOPNOTSUPP;
2115         }
2116
2117         /* Check the size of the key */
2118         if((wrqu->encoding.length != 8) && (wrqu->encoding.length != 0)) {
2119                 ret = -EINVAL;
2120         }
2121
2122         if(!ret) {
2123                 /* Basic checking... */
2124                 if (wrqu->encoding.length == 8) {
2125                         /* Copy the key in the driver */
2126                         memcpy(psa.psa_encryption_key, extra,
2127                                wrqu->encoding.length);
2128                         psa.psa_encryption_select = 1;
2129
2130                         psa_write(dev,
2131                                   (char *) &psa.psa_encryption_select -
2132                                   (char *) &psa,
2133                                   (unsigned char *) &psa.
2134                                   psa_encryption_select, 8 + 1);
2135
2136                         mmc_out(base, mmwoff(0, mmw_encr_enable),
2137                                 MMW_ENCR_ENABLE_EN | MMW_ENCR_ENABLE_MODE);
2138                         mmc_write(base, mmwoff(0, mmw_encr_key),
2139                                   (unsigned char *) &psa.
2140                                   psa_encryption_key, 8);
2141                 }
2142
2143                 /* disable encryption */
2144                 if (wrqu->encoding.flags & IW_ENCODE_DISABLED) {
2145                         psa.psa_encryption_select = 0;
2146                         psa_write(dev,
2147                                   (char *) &psa.psa_encryption_select -
2148                                   (char *) &psa,
2149                                   (unsigned char *) &psa.
2150                                   psa_encryption_select, 1);
2151
2152                         mmc_out(base, mmwoff(0, mmw_encr_enable), 0);
2153                 }
2154                 /* update the Wavelan checksum */
2155                 update_psa_checksum(dev);
2156         }
2157
2158         /* Enable interrupts and restore flags. */
2159         spin_unlock_irqrestore(&lp->spinlock, flags);
2160
2161         return ret;
2162 }
2163
2164 /*------------------------------------------------------------------*/
2165 /*
2166  * Wireless Handler : get encryption key
2167  */
2168 static int wavelan_get_encode(struct net_device *dev,
2169                               struct iw_request_info *info,
2170                               union iwreq_data *wrqu,
2171                               char *extra)
2172 {
2173         ioaddr_t base = dev->base_addr;
2174         net_local *lp = netdev_priv(dev);
2175         psa_t psa;
2176         unsigned long flags;
2177         int ret = 0;
2178
2179         /* Disable interrupts and save flags. */
2180         spin_lock_irqsave(&lp->spinlock, flags);
2181         
2182         /* Check if encryption is available */
2183         if (!mmc_encr(base)) {
2184                 ret = -EOPNOTSUPP;
2185         } else {
2186                 /* Read the encryption key */
2187                 psa_read(dev,
2188                          (char *) &psa.psa_encryption_select -
2189                          (char *) &psa,
2190                          (unsigned char *) &psa.
2191                          psa_encryption_select, 1 + 8);
2192
2193                 /* encryption is enabled ? */
2194                 if (psa.psa_encryption_select)
2195                         wrqu->encoding.flags = IW_ENCODE_ENABLED;
2196                 else
2197                         wrqu->encoding.flags = IW_ENCODE_DISABLED;
2198                 wrqu->encoding.flags |= mmc_encr(base);
2199
2200                 /* Copy the key to the user buffer */
2201                 wrqu->encoding.length = 8;
2202                 memcpy(extra, psa.psa_encryption_key, wrqu->encoding.length);
2203         }
2204
2205         /* Enable interrupts and restore flags. */
2206         spin_unlock_irqrestore(&lp->spinlock, flags);
2207
2208         return ret;
2209 }
2210
2211 #ifdef WAVELAN_ROAMING_EXT
2212 /*------------------------------------------------------------------*/
2213 /*
2214  * Wireless Handler : set ESSID (domain)
2215  */
2216 static int wavelan_set_essid(struct net_device *dev,
2217                              struct iw_request_info *info,
2218                              union iwreq_data *wrqu,
2219                              char *extra)
2220 {
2221         net_local *lp = netdev_priv(dev);
2222         unsigned long flags;
2223         int ret = 0;
2224
2225         /* Disable interrupts and save flags. */
2226         spin_lock_irqsave(&lp->spinlock, flags);
2227         
2228         /* Check if disable */
2229         if(wrqu->data.flags == 0)
2230                 lp->filter_domains = 0;
2231         else {
2232                 char    essid[IW_ESSID_MAX_SIZE + 1];
2233                 char *  endp;
2234
2235                 /* Terminate the string */
2236                 memcpy(essid, extra, wrqu->data.length);
2237                 essid[IW_ESSID_MAX_SIZE] = '\0';
2238
2239 #ifdef DEBUG_IOCTL_INFO
2240                 printk(KERN_DEBUG "SetEssid : ``%s''\n", essid);
2241 #endif  /* DEBUG_IOCTL_INFO */
2242
2243                 /* Convert to a number (note : Wavelan specific) */
2244                 lp->domain_id = simple_strtoul(essid, &endp, 16);
2245                 /* Has it worked  ? */
2246                 if(endp > essid)
2247                         lp->filter_domains = 1;
2248                 else {
2249                         lp->filter_domains = 0;
2250                         ret = -EINVAL;
2251                 }
2252         }
2253
2254         /* Enable interrupts and restore flags. */
2255         spin_unlock_irqrestore(&lp->spinlock, flags);
2256
2257         return ret;
2258 }
2259
2260 /*------------------------------------------------------------------*/
2261 /*
2262  * Wireless Handler : get ESSID (domain)
2263  */
2264 static int wavelan_get_essid(struct net_device *dev,
2265                              struct iw_request_info *info,
2266                              union iwreq_data *wrqu,
2267                              char *extra)
2268 {
2269         net_local *lp = netdev_priv(dev);
2270
2271         /* Is the domain ID active ? */
2272         wrqu->data.flags = lp->filter_domains;
2273
2274         /* Copy Domain ID into a string (Wavelan specific) */
2275         /* Sound crazy, be we can't have a snprintf in the kernel !!! */
2276         sprintf(extra, "%lX", lp->domain_id);
2277         extra[IW_ESSID_MAX_SIZE] = '\0';
2278
2279         /* Set the length */
2280         wrqu->data.length = strlen(extra) + 1;
2281
2282         return 0;
2283 }
2284
2285 /*------------------------------------------------------------------*/
2286 /*
2287  * Wireless Handler : set AP address
2288  */
2289 static int wavelan_set_wap(struct net_device *dev,
2290                            struct iw_request_info *info,
2291                            union iwreq_data *wrqu,
2292                            char *extra)
2293 {
2294 #ifdef DEBUG_IOCTL_INFO
2295         printk(KERN_DEBUG "Set AP to : %02X:%02X:%02X:%02X:%02X:%02X\n",
2296                wrqu->ap_addr.sa_data[0],
2297                wrqu->ap_addr.sa_data[1],
2298                wrqu->ap_addr.sa_data[2],
2299                wrqu->ap_addr.sa_data[3],
2300                wrqu->ap_addr.sa_data[4],
2301                wrqu->ap_addr.sa_data[5]);
2302 #endif  /* DEBUG_IOCTL_INFO */
2303
2304         return -EOPNOTSUPP;
2305 }
2306
2307 /*------------------------------------------------------------------*/
2308 /*
2309  * Wireless Handler : get AP address
2310  */
2311 static int wavelan_get_wap(struct net_device *dev,
2312                            struct iw_request_info *info,
2313                            union iwreq_data *wrqu,
2314                            char *extra)
2315 {
2316         /* Should get the real McCoy instead of own Ethernet address */
2317         memcpy(wrqu->ap_addr.sa_data, dev->dev_addr, WAVELAN_ADDR_SIZE);
2318         wrqu->ap_addr.sa_family = ARPHRD_ETHER;
2319
2320         return -EOPNOTSUPP;
2321 }
2322 #endif  /* WAVELAN_ROAMING_EXT */
2323
2324 #ifdef WAVELAN_ROAMING
2325 /*------------------------------------------------------------------*/
2326 /*
2327  * Wireless Handler : set mode
2328  */
2329 static int wavelan_set_mode(struct net_device *dev,
2330                             struct iw_request_info *info,
2331                             union iwreq_data *wrqu,
2332                             char *extra)
2333 {
2334         net_local *lp = netdev_priv(dev);
2335         unsigned long flags;
2336         int ret = 0;
2337
2338         /* Disable interrupts and save flags. */
2339         spin_lock_irqsave(&lp->spinlock, flags);
2340
2341         /* Check mode */
2342         switch(wrqu->mode) {
2343         case IW_MODE_ADHOC:
2344                 if(do_roaming) {
2345                         wv_roam_cleanup(dev);
2346                         do_roaming = 0;
2347                 }
2348                 break;
2349         case IW_MODE_INFRA:
2350                 if(!do_roaming) {
2351                         wv_roam_init(dev);
2352                         do_roaming = 1;
2353                 }
2354                 break;
2355         default:
2356                 ret = -EINVAL;
2357         }
2358
2359         /* Enable interrupts and restore flags. */
2360         spin_unlock_irqrestore(&lp->spinlock, flags);
2361
2362         return ret;
2363 }
2364
2365 /*------------------------------------------------------------------*/
2366 /*
2367  * Wireless Handler : get mode
2368  */
2369 static int wavelan_get_mode(struct net_device *dev,
2370                             struct iw_request_info *info,
2371                             union iwreq_data *wrqu,
2372                             char *extra)
2373 {
2374         if(do_roaming)
2375                 wrqu->mode = IW_MODE_INFRA;
2376         else
2377                 wrqu->mode = IW_MODE_ADHOC;
2378
2379         return 0;
2380 }
2381 #endif  /* WAVELAN_ROAMING */
2382
2383 /*------------------------------------------------------------------*/
2384 /*
2385  * Wireless Handler : get range info
2386  */
2387 static int wavelan_get_range(struct net_device *dev,
2388                              struct iw_request_info *info,
2389                              union iwreq_data *wrqu,
2390                              char *extra)
2391 {
2392         ioaddr_t base = dev->base_addr;
2393         net_local *lp = netdev_priv(dev);
2394         struct iw_range *range = (struct iw_range *) extra;
2395         unsigned long flags;
2396         int ret = 0;
2397
2398         /* Set the length (very important for backward compatibility) */
2399         wrqu->data.length = sizeof(struct iw_range);
2400
2401         /* Set all the info we don't care or don't know about to zero */
2402         memset(range, 0, sizeof(struct iw_range));
2403
2404         /* Set the Wireless Extension versions */
2405         range->we_version_compiled = WIRELESS_EXT;
2406         range->we_version_source = 9;
2407
2408         /* Set information in the range struct.  */
2409         range->throughput = 1.4 * 1000 * 1000;  /* don't argue on this ! */
2410         range->min_nwid = 0x0000;
2411         range->max_nwid = 0xFFFF;
2412
2413         range->sensitivity = 0x3F;
2414         range->max_qual.qual = MMR_SGNL_QUAL;
2415         range->max_qual.level = MMR_SIGNAL_LVL;
2416         range->max_qual.noise = MMR_SILENCE_LVL;
2417         range->avg_qual.qual = MMR_SGNL_QUAL; /* Always max */
2418         /* Need to get better values for those two */
2419         range->avg_qual.level = 30;
2420         range->avg_qual.noise = 8;
2421
2422         range->num_bitrates = 1;
2423         range->bitrate[0] = 2000000;    /* 2 Mb/s */
2424
2425         /* Event capability (kernel + driver) */
2426         range->event_capa[0] = (IW_EVENT_CAPA_MASK(0x8B02) |
2427                                 IW_EVENT_CAPA_MASK(0x8B04) |
2428                                 IW_EVENT_CAPA_MASK(0x8B06));
2429         range->event_capa[1] = IW_EVENT_CAPA_K_1;
2430
2431         /* Disable interrupts and save flags. */
2432         spin_lock_irqsave(&lp->spinlock, flags);
2433         
2434         /* Attempt to recognise 2.00 cards (2.4 GHz frequency selectable). */
2435         if (!(mmc_in(base, mmroff(0, mmr_fee_status)) &
2436               (MMR_FEE_STATUS_DWLD | MMR_FEE_STATUS_BUSY))) {
2437                 range->num_channels = 10;
2438                 range->num_frequency = wv_frequency_list(base, range->freq,
2439                                                         IW_MAX_FREQUENCIES);
2440         } else
2441                 range->num_channels = range->num_frequency = 0;
2442
2443         /* Encryption supported ? */
2444         if (mmc_encr(base)) {
2445                 range->encoding_size[0] = 8;    /* DES = 64 bits key */
2446                 range->num_encoding_sizes = 1;
2447                 range->max_encoding_tokens = 1; /* Only one key possible */
2448         } else {
2449                 range->num_encoding_sizes = 0;
2450                 range->max_encoding_tokens = 0;
2451         }
2452
2453         /* Enable interrupts and restore flags. */
2454         spin_unlock_irqrestore(&lp->spinlock, flags);
2455
2456         return ret;
2457 }
2458
2459 /*------------------------------------------------------------------*/
2460 /*
2461  * Wireless Private Handler : set quality threshold
2462  */
2463 static int wavelan_set_qthr(struct net_device *dev,
2464                             struct iw_request_info *info,
2465                             union iwreq_data *wrqu,
2466                             char *extra)
2467 {
2468         ioaddr_t base = dev->base_addr;
2469         net_local *lp = netdev_priv(dev);
2470         psa_t psa;
2471         unsigned long flags;
2472
2473         /* Disable interrupts and save flags. */
2474         spin_lock_irqsave(&lp->spinlock, flags);
2475         
2476         psa.psa_quality_thr = *(extra) & 0x0F;
2477         psa_write(dev,
2478                   (char *) &psa.psa_quality_thr - (char *) &psa,
2479                   (unsigned char *) &psa.psa_quality_thr, 1);
2480         /* update the Wavelan checksum */
2481         update_psa_checksum(dev);
2482         mmc_out(base, mmwoff(0, mmw_quality_thr),
2483                 psa.psa_quality_thr);
2484
2485         /* Enable interrupts and restore flags. */
2486         spin_unlock_irqrestore(&lp->spinlock, flags);
2487
2488         return 0;
2489 }
2490
2491 /*------------------------------------------------------------------*/
2492 /*
2493  * Wireless Private Handler : get quality threshold
2494  */
2495 static int wavelan_get_qthr(struct net_device *dev,
2496                             struct iw_request_info *info,
2497                             union iwreq_data *wrqu,
2498                             char *extra)
2499 {
2500         net_local *lp = netdev_priv(dev);
2501         psa_t psa;
2502         unsigned long flags;
2503
2504         /* Disable interrupts and save flags. */
2505         spin_lock_irqsave(&lp->spinlock, flags);
2506         
2507         psa_read(dev,
2508                  (char *) &psa.psa_quality_thr - (char *) &psa,
2509                  (unsigned char *) &psa.psa_quality_thr, 1);
2510         *(extra) = psa.psa_quality_thr & 0x0F;
2511
2512         /* Enable interrupts and restore flags. */
2513         spin_unlock_irqrestore(&lp->spinlock, flags);
2514
2515         return 0;
2516 }
2517
2518 #ifdef WAVELAN_ROAMING
2519 /*------------------------------------------------------------------*/
2520 /*
2521  * Wireless Private Handler : set roaming
2522  */
2523 static int wavelan_set_roam(struct net_device *dev,
2524                             struct iw_request_info *info,
2525                             union iwreq_data *wrqu,
2526                             char *extra)
2527 {
2528         net_local *lp = netdev_priv(dev);
2529         unsigned long flags;
2530
2531         /* Disable interrupts and save flags. */
2532         spin_lock_irqsave(&lp->spinlock, flags);
2533         
2534         /* Note : should check if user == root */
2535         if(do_roaming && (*extra)==0)
2536                 wv_roam_cleanup(dev);
2537         else if(do_roaming==0 && (*extra)!=0)
2538                 wv_roam_init(dev);
2539
2540         do_roaming = (*extra);
2541
2542         /* Enable interrupts and restore flags. */
2543         spin_unlock_irqrestore(&lp->spinlock, flags);
2544
2545         return 0;
2546 }
2547
2548 /*------------------------------------------------------------------*/
2549 /*
2550  * Wireless Private Handler : get quality threshold
2551  */
2552 static int wavelan_get_roam(struct net_device *dev,
2553                             struct iw_request_info *info,
2554                             union iwreq_data *wrqu,
2555                             char *extra)
2556 {
2557         *(extra) = do_roaming;
2558
2559         return 0;
2560 }
2561 #endif  /* WAVELAN_ROAMING */
2562
2563 #ifdef HISTOGRAM
2564 /*------------------------------------------------------------------*/
2565 /*
2566  * Wireless Private Handler : set histogram
2567  */
2568 static int wavelan_set_histo(struct net_device *dev,
2569                              struct iw_request_info *info,
2570                              union iwreq_data *wrqu,
2571                              char *extra)
2572 {
2573         net_local *lp = netdev_priv(dev);
2574
2575         /* Check the number of intervals. */
2576         if (wrqu->data.length > 16) {
2577                 return(-E2BIG);
2578         }
2579
2580         /* Disable histo while we copy the addresses.
2581          * As we don't disable interrupts, we need to do this */
2582         lp->his_number = 0;
2583
2584         /* Are there ranges to copy? */
2585         if (wrqu->data.length > 0) {
2586                 /* Copy interval ranges to the driver */
2587                 memcpy(lp->his_range, extra, wrqu->data.length);
2588
2589                 {
2590                   int i;
2591                   printk(KERN_DEBUG "Histo :");
2592                   for(i = 0; i < wrqu->data.length; i++)
2593                     printk(" %d", lp->his_range[i]);
2594                   printk("\n");
2595                 }
2596
2597                 /* Reset result structure. */
2598                 memset(lp->his_sum, 0x00, sizeof(long) * 16);
2599         }
2600
2601         /* Now we can set the number of ranges */
2602         lp->his_number = wrqu->data.length;
2603
2604         return(0);
2605 }
2606
2607 /*------------------------------------------------------------------*/
2608 /*
2609  * Wireless Private Handler : get histogram
2610  */
2611 static int wavelan_get_histo(struct net_device *dev,
2612                              struct iw_request_info *info,
2613                              union iwreq_data *wrqu,
2614                              char *extra)
2615 {
2616         net_local *lp = netdev_priv(dev);
2617
2618         /* Set the number of intervals. */
2619         wrqu->data.length = lp->his_number;
2620
2621         /* Give back the distribution statistics */
2622         if(lp->his_number > 0)
2623                 memcpy(extra, lp->his_sum, sizeof(long) * lp->his_number);
2624
2625         return(0);
2626 }
2627 #endif                  /* HISTOGRAM */
2628
2629 /*------------------------------------------------------------------*/
2630 /*
2631  * Structures to export the Wireless Handlers
2632  */
2633
2634 static const struct iw_priv_args wavelan_private_args[] = {
2635 /*{ cmd,         set_args,                            get_args, name } */
2636   { SIOCSIPQTHR, IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | 1, 0, "setqualthr" },
2637   { SIOCGIPQTHR, 0, IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | 1, "getqualthr" },
2638   { SIOCSIPROAM, IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | 1, 0, "setroam" },
2639   { SIOCGIPROAM, 0, IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | 1, "getroam" },
2640   { SIOCSIPHISTO, IW_PRIV_TYPE_BYTE | 16,                    0, "sethisto" },
2641   { SIOCGIPHISTO, 0,                     IW_PRIV_TYPE_INT | 16, "gethisto" },
2642 };
2643
2644 static const iw_handler         wavelan_handler[] =
2645 {
2646         NULL,                           /* SIOCSIWNAME */
2647         wavelan_get_name,               /* SIOCGIWNAME */
2648         wavelan_set_nwid,               /* SIOCSIWNWID */
2649         wavelan_get_nwid,               /* SIOCGIWNWID */
2650         wavelan_set_freq,               /* SIOCSIWFREQ */
2651         wavelan_get_freq,               /* SIOCGIWFREQ */
2652 #ifdef WAVELAN_ROAMING
2653         wavelan_set_mode,               /* SIOCSIWMODE */
2654         wavelan_get_mode,               /* SIOCGIWMODE */
2655 #else   /* WAVELAN_ROAMING */
2656         NULL,                           /* SIOCSIWMODE */
2657         NULL,                           /* SIOCGIWMODE */
2658 #endif  /* WAVELAN_ROAMING */
2659         wavelan_set_sens,               /* SIOCSIWSENS */
2660         wavelan_get_sens,               /* SIOCGIWSENS */
2661         NULL,                           /* SIOCSIWRANGE */
2662         wavelan_get_range,              /* SIOCGIWRANGE */
2663         NULL,                           /* SIOCSIWPRIV */
2664         NULL,                           /* SIOCGIWPRIV */
2665         NULL,                           /* SIOCSIWSTATS */
2666         NULL,                           /* SIOCGIWSTATS */
2667         iw_handler_set_spy,             /* SIOCSIWSPY */
2668         iw_handler_get_spy,             /* SIOCGIWSPY */
2669         iw_handler_set_thrspy,          /* SIOCSIWTHRSPY */
2670         iw_handler_get_thrspy,          /* SIOCGIWTHRSPY */
2671 #ifdef WAVELAN_ROAMING_EXT
2672         wavelan_set_wap,                /* SIOCSIWAP */
2673         wavelan_get_wap,                /* SIOCGIWAP */
2674         NULL,                           /* -- hole -- */
2675         NULL,                           /* SIOCGIWAPLIST */
2676         NULL,                           /* -- hole -- */
2677         NULL,                           /* -- hole -- */
2678         wavelan_set_essid,              /* SIOCSIWESSID */
2679         wavelan_get_essid,              /* SIOCGIWESSID */
2680 #else   /* WAVELAN_ROAMING_EXT */
2681         NULL,                           /* SIOCSIWAP */
2682         NULL,                           /* SIOCGIWAP */
2683         NULL,                           /* -- hole -- */
2684         NULL,                           /* SIOCGIWAPLIST */
2685         NULL,                           /* -- hole -- */
2686         NULL,                           /* -- hole -- */
2687         NULL,                           /* SIOCSIWESSID */
2688         NULL,                           /* SIOCGIWESSID */
2689 #endif  /* WAVELAN_ROAMING_EXT */
2690         NULL,                           /* SIOCSIWNICKN */
2691         NULL,                           /* SIOCGIWNICKN */
2692         NULL,                           /* -- hole -- */
2693         NULL,                           /* -- hole -- */
2694         NULL,                           /* SIOCSIWRATE */
2695         NULL,                           /* SIOCGIWRATE */
2696         NULL,                           /* SIOCSIWRTS */
2697         NULL,                           /* SIOCGIWRTS */
2698         NULL,                           /* SIOCSIWFRAG */
2699         NULL,                           /* SIOCGIWFRAG */
2700         NULL,                           /* SIOCSIWTXPOW */
2701         NULL,                           /* SIOCGIWTXPOW */
2702         NULL,                           /* SIOCSIWRETRY */
2703         NULL,                           /* SIOCGIWRETRY */
2704         wavelan_set_encode,             /* SIOCSIWENCODE */
2705         wavelan_get_encode,             /* SIOCGIWENCODE */
2706 };
2707
2708 static const iw_handler         wavelan_private_handler[] =
2709 {
2710         wavelan_set_qthr,               /* SIOCIWFIRSTPRIV */
2711         wavelan_get_qthr,               /* SIOCIWFIRSTPRIV + 1 */
2712 #ifdef WAVELAN_ROAMING
2713         wavelan_set_roam,               /* SIOCIWFIRSTPRIV + 2 */
2714         wavelan_get_roam,               /* SIOCIWFIRSTPRIV + 3 */
2715 #else   /* WAVELAN_ROAMING */
2716         NULL,                           /* SIOCIWFIRSTPRIV + 2 */
2717         NULL,                           /* SIOCIWFIRSTPRIV + 3 */
2718 #endif  /* WAVELAN_ROAMING */
2719 #ifdef HISTOGRAM
2720         wavelan_set_histo,              /* SIOCIWFIRSTPRIV + 4 */
2721         wavelan_get_histo,              /* SIOCIWFIRSTPRIV + 5 */
2722 #endif  /* HISTOGRAM */
2723 };
2724
2725 static const struct iw_handler_def      wavelan_handler_def =
2726 {
2727         .num_standard   = sizeof(wavelan_handler)/sizeof(iw_handler),
2728         .num_private    = sizeof(wavelan_private_handler)/sizeof(iw_handler),
2729         .num_private_args = sizeof(wavelan_private_args)/sizeof(struct iw_priv_args),
2730         .standard       = wavelan_handler,
2731         .private        = wavelan_private_handler,
2732         .private_args   = wavelan_private_args,
2733         .get_wireless_stats = wavelan_get_wireless_stats,
2734 };
2735
2736 /*------------------------------------------------------------------*/
2737 /*
2738  * Get wireless statistics
2739  * Called by /proc/net/wireless...
2740  */
2741 static iw_stats *
2742 wavelan_get_wireless_stats(struct net_device *  dev)
2743 {
2744   ioaddr_t              base = dev->base_addr;
2745   net_local *           lp = netdev_priv(dev);
2746   mmr_t                 m;
2747   iw_stats *            wstats;
2748   unsigned long         flags;
2749
2750 #ifdef DEBUG_IOCTL_TRACE
2751   printk(KERN_DEBUG "%s: ->wavelan_get_wireless_stats()\n", dev->name);
2752 #endif
2753
2754   /* Disable interrupts & save flags */
2755   spin_lock_irqsave(&lp->spinlock, flags);
2756
2757   wstats = &lp->wstats;
2758
2759   /* Get data from the mmc */
2760   mmc_out(base, mmwoff(0, mmw_freeze), 1);
2761
2762   mmc_read(base, mmroff(0, mmr_dce_status), &m.mmr_dce_status, 1);
2763   mmc_read(base, mmroff(0, mmr_wrong_nwid_l), &m.mmr_wrong_nwid_l, 2);
2764   mmc_read(base, mmroff(0, mmr_thr_pre_set), &m.mmr_thr_pre_set, 4);
2765
2766   mmc_out(base, mmwoff(0, mmw_freeze), 0);
2767
2768   /* Copy data to wireless stuff */
2769   wstats->status = m.mmr_dce_status & MMR_DCE_STATUS;
2770   wstats->qual.qual = m.mmr_sgnl_qual & MMR_SGNL_QUAL;
2771   wstats->qual.level = m.mmr_signal_lvl & MMR_SIGNAL_LVL;
2772   wstats->qual.noise = m.mmr_silence_lvl & MMR_SILENCE_LVL;
2773   wstats->qual.updated = (((m.mmr_signal_lvl & MMR_SIGNAL_LVL_VALID) >> 7) |
2774                           ((m.mmr_signal_lvl & MMR_SIGNAL_LVL_VALID) >> 6) |
2775                           ((m.mmr_silence_lvl & MMR_SILENCE_LVL_VALID) >> 5));
2776   wstats->discard.nwid += (m.mmr_wrong_nwid_h << 8) | m.mmr_wrong_nwid_l;
2777   wstats->discard.code = 0L;
2778   wstats->discard.misc = 0L;
2779
2780   /* ReEnable interrupts & restore flags */
2781   spin_unlock_irqrestore(&lp->spinlock, flags);
2782
2783 #ifdef DEBUG_IOCTL_TRACE
2784   printk(KERN_DEBUG "%s: <-wavelan_get_wireless_stats()\n", dev->name);
2785 #endif
2786   return &lp->wstats;
2787 }
2788 #endif  /* WIRELESS_EXT */
2789
2790 /************************* PACKET RECEPTION *************************/
2791 /*
2792  * This part deal with receiving the packets.
2793  * The interrupt handler get an interrupt when a packet has been
2794  * successfully received and called this part...
2795  */
2796
2797 /*------------------------------------------------------------------*/
2798 /*
2799  * Calculate the starting address of the frame pointed to by the receive
2800  * frame pointer and verify that the frame seem correct
2801  * (called by wv_packet_rcv())
2802  */
2803 static inline int
2804 wv_start_of_frame(struct net_device *   dev,
2805                   int           rfp,    /* end of frame */
2806                   int           wrap)   /* start of buffer */
2807 {
2808   ioaddr_t      base = dev->base_addr;
2809   int           rp;
2810   int           len;
2811
2812   rp = (rfp - 5 + RX_SIZE) % RX_SIZE;
2813   outb(rp & 0xff, PIORL(base));
2814   outb(((rp >> 8) & PIORH_MASK), PIORH(base));
2815   len = inb(PIOP(base));
2816   len |= inb(PIOP(base)) << 8;
2817
2818   /* Sanity checks on size */
2819   /* Frame too big */
2820   if(len > MAXDATAZ + 100)
2821     {
2822 #ifdef DEBUG_RX_ERROR
2823       printk(KERN_INFO "%s: wv_start_of_frame: Received frame too large, rfp %d len 0x%x\n",
2824              dev->name, rfp, len);
2825 #endif
2826       return(-1);
2827     }
2828   
2829   /* Frame too short */
2830   if(len < 7)
2831     {
2832 #ifdef DEBUG_RX_ERROR
2833       printk(KERN_INFO "%s: wv_start_of_frame: Received null frame, rfp %d len 0x%x\n",
2834              dev->name, rfp, len);
2835 #endif
2836       return(-1);
2837     }
2838   
2839   /* Wrap around buffer */
2840   if(len > ((wrap - (rfp - len) + RX_SIZE) % RX_SIZE))  /* magic formula ! */
2841     {
2842 #ifdef DEBUG_RX_ERROR
2843       printk(KERN_INFO "%s: wv_start_of_frame: wrap around buffer, wrap %d rfp %d len 0x%x\n",
2844              dev->name, wrap, rfp, len);
2845 #endif
2846       return(-1);
2847     }
2848
2849   return((rp - len + RX_SIZE) % RX_SIZE);
2850 } /* wv_start_of_frame */
2851
2852 /*------------------------------------------------------------------*/
2853 /*
2854  * This routine does the actual copy of data (including the ethernet
2855  * header structure) from the WaveLAN card to an sk_buff chain that
2856  * will be passed up to the network interface layer. NOTE: We
2857  * currently don't handle trailer protocols (neither does the rest of
2858  * the network interface), so if that is needed, it will (at least in
2859  * part) be added here.  The contents of the receive ring buffer are
2860  * copied to a message chain that is then passed to the kernel.
2861  *
2862  * Note: if any errors occur, the packet is "dropped on the floor"
2863  * (called by wv_packet_rcv())
2864  */
2865 static inline void
2866 wv_packet_read(struct net_device *              dev,
2867                int              fd_p,
2868                int              sksize)
2869 {
2870   net_local *           lp = netdev_priv(dev);
2871   struct sk_buff *      skb;
2872
2873 #ifdef DEBUG_RX_TRACE
2874   printk(KERN_DEBUG "%s: ->wv_packet_read(0x%X, %d)\n",
2875          dev->name, fd_p, sksize);
2876 #endif
2877
2878   /* Allocate some buffer for the new packet */
2879   if((skb = dev_alloc_skb(sksize+2)) == (struct sk_buff *) NULL)
2880     {
2881 #ifdef DEBUG_RX_ERROR
2882       printk(KERN_INFO "%s: wv_packet_read(): could not alloc_skb(%d, GFP_ATOMIC)\n",
2883              dev->name, sksize);
2884 #endif
2885       lp->stats.rx_dropped++;
2886       /*
2887        * Not only do we want to return here, but we also need to drop the
2888        * packet on the floor to clear the interrupt.
2889        */
2890       return;
2891     }
2892
2893   skb->dev = dev;
2894
2895   skb_reserve(skb, 2);
2896   fd_p = read_ringbuf(dev, fd_p, (char *) skb_put(skb, sksize), sksize);
2897   skb->protocol = eth_type_trans(skb, dev);
2898
2899 #ifdef DEBUG_RX_INFO
2900   wv_packet_info(skb->mac.raw, sksize, dev->name, "wv_packet_read");
2901 #endif  /* DEBUG_RX_INFO */
2902      
2903   /* Statistics gathering & stuff associated.
2904    * It seem a bit messy with all the define, but it's really simple... */
2905   if(
2906 #ifdef IW_WIRELESS_SPY
2907      (lp->spy_data.spy_number > 0) ||
2908 #endif  /* IW_WIRELESS_SPY */
2909 #ifdef HISTOGRAM
2910      (lp->his_number > 0) ||
2911 #endif  /* HISTOGRAM */
2912 #ifdef WAVELAN_ROAMING
2913      (do_roaming) ||
2914 #endif  /* WAVELAN_ROAMING */
2915      0)
2916     {
2917       u_char    stats[3];       /* Signal level, Noise level, Signal quality */
2918
2919       /* read signal level, silence level and signal quality bytes */
2920       fd_p = read_ringbuf(dev, (fd_p + 4) % RX_SIZE + RX_BASE,
2921                           stats, 3);
2922 #ifdef DEBUG_RX_INFO
2923       printk(KERN_DEBUG "%s: wv_packet_read(): Signal level %d/63, Silence level %d/63, signal quality %d/16\n",
2924              dev->name, stats[0] & 0x3F, stats[1] & 0x3F, stats[2] & 0x0F);
2925 #endif
2926
2927 #ifdef WAVELAN_ROAMING
2928       if(do_roaming)
2929         if(WAVELAN_BEACON(skb->data))
2930           wl_roam_gather(dev, skb->data, stats);
2931 #endif  /* WAVELAN_ROAMING */
2932           
2933 #ifdef WIRELESS_SPY
2934       wl_spy_gather(dev, skb->mac.raw + WAVELAN_ADDR_SIZE, stats);
2935 #endif  /* WIRELESS_SPY */
2936 #ifdef HISTOGRAM
2937       wl_his_gather(dev, stats);
2938 #endif  /* HISTOGRAM */
2939     }
2940
2941   /*
2942    * Hand the packet to the Network Module
2943    */
2944   netif_rx(skb);
2945
2946   /* Keep stats up to date */
2947   dev->last_rx = jiffies;
2948   lp->stats.rx_packets++;
2949   lp->stats.rx_bytes += sksize;
2950
2951 #ifdef DEBUG_RX_TRACE
2952   printk(KERN_DEBUG "%s: <-wv_packet_read()\n", dev->name);
2953 #endif
2954   return;
2955 }
2956
2957 /*------------------------------------------------------------------*/
2958 /*
2959  * This routine is called by the interrupt handler to initiate a
2960  * packet transfer from the card to the network interface layer above
2961  * this driver.  This routine checks if a buffer has been successfully
2962  * received by the WaveLAN card.  If so, the routine wv_packet_read is
2963  * called to do the actual transfer of the card's data including the
2964  * ethernet header into a packet consisting of an sk_buff chain.
2965  * (called by wavelan_interrupt())
2966  * Note : the spinlock is already grabbed for us and irq are disabled.
2967  */
2968 static inline void
2969 wv_packet_rcv(struct net_device *       dev)
2970 {
2971   ioaddr_t      base = dev->base_addr;
2972   net_local *   lp = netdev_priv(dev);
2973   int           newrfp;
2974   int           rp;
2975   int           len;
2976   int           f_start;
2977   int           status;
2978   int           i593_rfp;
2979   int           stat_ptr;
2980   u_char        c[4];
2981
2982 #ifdef DEBUG_RX_TRACE
2983   printk(KERN_DEBUG "%s: ->wv_packet_rcv()\n", dev->name);
2984 #endif
2985
2986   /* Get the new receive frame pointer from the i82593 chip */
2987   outb(CR0_STATUS_2 | OP0_NOP, LCCR(base));
2988   i593_rfp = inb(LCSR(base));
2989   i593_rfp |= inb(LCSR(base)) << 8;
2990   i593_rfp %= RX_SIZE;
2991
2992   /* Get the new receive frame pointer from the WaveLAN card.
2993    * It is 3 bytes more than the increment of the i82593 receive
2994    * frame pointer, for each packet. This is because it includes the
2995    * 3 roaming bytes added by the mmc.
2996    */
2997   newrfp = inb(RPLL(base));
2998   newrfp |= inb(RPLH(base)) << 8;
2999   newrfp %= RX_SIZE;
3000
3001 #ifdef DEBUG_RX_INFO
3002   printk(KERN_DEBUG "%s: wv_packet_rcv(): i593_rfp %d stop %d newrfp %d lp->rfp %d\n",
3003          dev->name, i593_rfp, lp->stop, newrfp, lp->rfp);
3004 #endif
3005
3006 #ifdef DEBUG_RX_ERROR
3007   /* If no new frame pointer... */
3008   if(lp->overrunning || newrfp == lp->rfp)
3009     printk(KERN_INFO "%s: wv_packet_rcv(): no new frame: i593_rfp %d stop %d newrfp %d lp->rfp %d\n",
3010            dev->name, i593_rfp, lp->stop, newrfp, lp->rfp);
3011 #endif
3012
3013   /* Read all frames (packets) received */
3014   while(newrfp != lp->rfp)
3015     {
3016       /* A frame is composed of the packet, followed by a status word,
3017        * the length of the frame (word) and the mmc info (SNR & qual).
3018        * It's because the length is at the end that we can only scan
3019        * frames backward. */
3020
3021       /* Find the first frame by skipping backwards over the frames */
3022       rp = newrfp;      /* End of last frame */
3023       while(((f_start = wv_start_of_frame(dev, rp, newrfp)) != lp->rfp) &&
3024             (f_start != -1))
3025           rp = f_start;
3026
3027       /* If we had a problem */
3028       if(f_start == -1)
3029         {
3030 #ifdef DEBUG_RX_ERROR
3031           printk(KERN_INFO "wavelan_cs: cannot find start of frame ");
3032           printk(" i593_rfp %d stop %d newrfp %d lp->rfp %d\n",
3033                  i593_rfp, lp->stop, newrfp, lp->rfp);
3034 #endif
3035           lp->rfp = rp;         /* Get to the last usable frame */
3036           continue;
3037         }
3038
3039       /* f_start point to the beggining of the first frame received
3040        * and rp to the beggining of the next one */
3041
3042       /* Read status & length of the frame */
3043       stat_ptr = (rp - 7 + RX_SIZE) % RX_SIZE;
3044       stat_ptr = read_ringbuf(dev, stat_ptr, c, 4);
3045       status = c[0] | (c[1] << 8);
3046       len = c[2] | (c[3] << 8);
3047
3048       /* Check status */
3049       if((status & RX_RCV_OK) != RX_RCV_OK)
3050         {
3051           lp->stats.rx_errors++;
3052           if(status & RX_NO_SFD)
3053             lp->stats.rx_frame_errors++;
3054           if(status & RX_CRC_ERR)
3055             lp->stats.rx_crc_errors++;
3056           if(status & RX_OVRRUN)
3057             lp->stats.rx_over_errors++;
3058
3059 #ifdef DEBUG_RX_FAIL
3060           printk(KERN_DEBUG "%s: wv_packet_rcv(): packet not received ok, status = 0x%x\n",
3061                  dev->name, status);
3062 #endif
3063         }
3064       else
3065         /* Read the packet and transmit to Linux */
3066         wv_packet_read(dev, f_start, len - 2);
3067
3068       /* One frame has been processed, skip it */
3069       lp->rfp = rp;
3070     }
3071
3072   /*
3073    * Update the frame stop register, but set it to less than
3074    * the full 8K to allow space for 3 bytes of signal strength
3075    * per packet.
3076    */
3077   lp->stop = (i593_rfp + RX_SIZE - ((RX_SIZE / 64) * 3)) % RX_SIZE;
3078   outb(OP0_SWIT_TO_PORT_1 | CR0_CHNL, LCCR(base));
3079   outb(CR1_STOP_REG_UPDATE | (lp->stop >> RX_SIZE_SHIFT), LCCR(base));
3080   outb(OP1_SWIT_TO_PORT_0, LCCR(base));
3081
3082 #ifdef DEBUG_RX_TRACE
3083   printk(KERN_DEBUG "%s: <-wv_packet_rcv()\n", dev->name);
3084 #endif
3085 }
3086
3087 /*********************** PACKET TRANSMISSION ***********************/
3088 /*
3089  * This part deal with sending packet through the wavelan
3090  * We copy the packet to the send buffer and then issue the send
3091  * command to the i82593. The result of this operation will be
3092  * checked in wavelan_interrupt()
3093  */
3094
3095 /*------------------------------------------------------------------*/
3096 /*
3097  * This routine fills in the appropriate registers and memory
3098  * locations on the WaveLAN card and starts the card off on
3099  * the transmit.
3100  * (called in wavelan_packet_xmit())
3101  */
3102 static inline void
3103 wv_packet_write(struct net_device *     dev,
3104                 void *          buf,
3105                 short           length)
3106 {
3107   net_local *           lp = netdev_priv(dev);
3108   ioaddr_t              base = dev->base_addr;
3109   unsigned long         flags;
3110   int                   clen = length;
3111   register u_short      xmtdata_base = TX_BASE;
3112
3113 #ifdef DEBUG_TX_TRACE
3114   printk(KERN_DEBUG "%s: ->wv_packet_write(%d)\n", dev->name, length);
3115 #endif
3116
3117   spin_lock_irqsave(&lp->spinlock, flags);
3118
3119   /* Write the length of data buffer followed by the buffer */
3120   outb(xmtdata_base & 0xff, PIORL(base));
3121   outb(((xmtdata_base >> 8) & PIORH_MASK) | PIORH_SEL_TX, PIORH(base));
3122   outb(clen & 0xff, PIOP(base));        /* lsb */
3123   outb(clen >> 8, PIOP(base));          /* msb */
3124
3125   /* Send the data */
3126   outsb(PIOP(base), buf, clen);
3127
3128   /* Indicate end of transmit chain */
3129   outb(OP0_NOP, PIOP(base));
3130   /* josullvn@cs.cmu.edu: need to send a second NOP for alignment... */
3131   outb(OP0_NOP, PIOP(base));
3132
3133   /* Reset the transmit DMA pointer */
3134   hacr_write_slow(base, HACR_PWR_STAT | HACR_TX_DMA_RESET);
3135   hacr_write(base, HACR_DEFAULT);
3136   /* Send the transmit command */
3137   wv_82593_cmd(dev, "wv_packet_write(): transmit",
3138                OP0_TRANSMIT, SR0_NO_RESULT);
3139
3140   /* Make sure the watchdog will keep quiet for a while */
3141   dev->trans_start = jiffies;
3142
3143   /* Keep stats up to date */
3144   lp->stats.tx_bytes += length;
3145
3146   spin_unlock_irqrestore(&lp->spinlock, flags);
3147
3148 #ifdef DEBUG_TX_INFO
3149   wv_packet_info((u_char *) buf, length, dev->name, "wv_packet_write");
3150 #endif  /* DEBUG_TX_INFO */
3151
3152 #ifdef DEBUG_TX_TRACE
3153   printk(KERN_DEBUG "%s: <-wv_packet_write()\n", dev->name);
3154 #endif
3155 }
3156
3157 /*------------------------------------------------------------------*/
3158 /*
3159  * This routine is called when we want to send a packet (NET3 callback)
3160  * In this routine, we check if the harware is ready to accept
3161  * the packet. We also prevent reentrance. Then, we call the function
3162  * to send the packet...
3163  */
3164 static int
3165 wavelan_packet_xmit(struct sk_buff *    skb,
3166                     struct net_device *         dev)
3167 {
3168   net_local *           lp = netdev_priv(dev);
3169   unsigned long         flags;
3170
3171 #ifdef DEBUG_TX_TRACE
3172   printk(KERN_DEBUG "%s: ->wavelan_packet_xmit(0x%X)\n", dev->name,
3173          (unsigned) skb);
3174 #endif
3175
3176   /*
3177    * Block a timer-based transmit from overlapping a previous transmit.
3178    * In other words, prevent reentering this routine.
3179    */
3180   netif_stop_queue(dev);
3181
3182   /* If somebody has asked to reconfigure the controller,
3183    * we can do it now */
3184   if(lp->reconfig_82593)
3185     {
3186       spin_lock_irqsave(&lp->spinlock, flags);  /* Disable interrupts */
3187       wv_82593_config(dev);
3188       spin_unlock_irqrestore(&lp->spinlock, flags);     /* Re-enable interrupts */
3189       /* Note : the configure procedure was totally synchronous,
3190        * so the Tx buffer is now free */
3191     }
3192
3193 #ifdef DEBUG_TX_ERROR
3194         if (skb->next)
3195                 printk(KERN_INFO "skb has next\n");
3196 #endif
3197
3198         /* Check if we need some padding */
3199         /* Note : on wireless the propagation time is in the order of 1us,
3200          * and we don't have the Ethernet specific requirement of beeing
3201          * able to detect collisions, therefore in theory we don't really
3202          * need to pad. Jean II */
3203         if (skb->len < ETH_ZLEN) {
3204                 skb = skb_padto(skb, ETH_ZLEN);
3205                 if (skb == NULL)
3206                         return 0;
3207         }
3208
3209   wv_packet_write(dev, skb->data, skb->len);
3210
3211   dev_kfree_skb(skb);
3212
3213 #ifdef DEBUG_TX_TRACE
3214   printk(KERN_DEBUG "%s: <-wavelan_packet_xmit()\n", dev->name);
3215 #endif
3216   return(0);
3217 }
3218
3219 /********************** HARDWARE CONFIGURATION **********************/
3220 /*
3221  * This part do the real job of starting and configuring the hardware.
3222  */
3223
3224 /*------------------------------------------------------------------*/
3225 /*
3226  * Routine to initialize the Modem Management Controller.
3227  * (called by wv_hw_config())
3228  */
3229 static inline int
3230 wv_mmc_init(struct net_device * dev)
3231 {
3232   ioaddr_t      base = dev->base_addr;
3233   psa_t         psa;
3234   mmw_t         m;
3235   int           configured;
3236   int           i;              /* Loop counter */
3237
3238 #ifdef DEBUG_CONFIG_TRACE
3239   printk(KERN_DEBUG "%s: ->wv_mmc_init()\n", dev->name);
3240 #endif
3241
3242   /* Read the parameter storage area */
3243   psa_read(dev, 0, (unsigned char *) &psa, sizeof(psa));
3244
3245   /*
3246    * Check the first three octets of the MAC addr for the manufacturer's code.
3247    * Note: If you get the error message below, you've got a
3248    * non-NCR/AT&T/Lucent PCMCIA cards, see wavelan_cs.h for detail on
3249    * how to configure your card...
3250    */
3251   for(i = 0; i < (sizeof(MAC_ADDRESSES) / sizeof(char) / 3); i++)
3252     if((psa.psa_univ_mac_addr[0] == MAC_ADDRESSES[i][0]) &&
3253        (psa.psa_univ_mac_addr[1] == MAC_ADDRESSES[i][1]) &&
3254        (psa.psa_univ_mac_addr[2] == MAC_ADDRESSES[i][2]))
3255       break;
3256
3257   /* If we have not found it... */
3258   if(i == (sizeof(MAC_ADDRESSES) / sizeof(char) / 3))
3259     {
3260 #ifdef DEBUG_CONFIG_ERRORS
3261       printk(KERN_WARNING "%s: wv_mmc_init(): Invalid MAC address: %02X:%02X:%02X:...\n",
3262              dev->name, psa.psa_univ_mac_addr[0],
3263              psa.psa_univ_mac_addr[1], psa.psa_univ_mac_addr[2]);
3264 #endif
3265       return FALSE;
3266     }
3267
3268   /* Get the MAC address */
3269   memcpy(&dev->dev_addr[0], &psa.psa_univ_mac_addr[0], WAVELAN_ADDR_SIZE);
3270
3271 #ifdef USE_PSA_CONFIG
3272   configured = psa.psa_conf_status & 1;
3273 #else
3274   configured = 0;
3275 #endif
3276
3277   /* Is the PSA is not configured */
3278   if(!configured)
3279     {
3280       /* User will be able to configure NWID after (with iwconfig) */
3281       psa.psa_nwid[0] = 0;
3282       psa.psa_nwid[1] = 0;
3283
3284       /* As NWID is not set : no NWID checking */
3285       psa.psa_nwid_select = 0;
3286
3287       /* Disable encryption */
3288       psa.psa_encryption_select = 0;
3289
3290       /* Set to standard values
3291        * 0x04 for AT,
3292        * 0x01 for MCA,
3293        * 0x04 for PCMCIA and 2.00 card (AT&T 407-024689/E document)
3294        */
3295       if (psa.psa_comp_number & 1)
3296         psa.psa_thr_pre_set = 0x01;
3297       else
3298         psa.psa_thr_pre_set = 0x04;
3299       psa.psa_quality_thr = 0x03;
3300
3301       /* It is configured */
3302       psa.psa_conf_status |= 1;
3303
3304 #ifdef USE_PSA_CONFIG
3305       /* Write the psa */
3306       psa_write(dev, (char *)psa.psa_nwid - (char *)&psa,
3307                 (unsigned char *)psa.psa_nwid, 4);
3308       psa_write(dev, (char *)&psa.psa_thr_pre_set - (char *)&psa,
3309                 (unsigned char *)&psa.psa_thr_pre_set, 1);
3310       psa_write(dev, (char *)&psa.psa_quality_thr - (char *)&psa,
3311                 (unsigned char *)&psa.psa_quality_thr, 1);
3312       psa_write(dev, (char *)&psa.psa_conf_status - (char *)&psa,
3313                 (unsigned char *)&psa.psa_conf_status, 1);
3314       /* update the Wavelan checksum */
3315       update_psa_checksum(dev);
3316 #endif  /* USE_PSA_CONFIG */
3317     }
3318
3319   /* Zero the mmc structure */
3320   memset(&m, 0x00, sizeof(m));
3321
3322   /* Copy PSA info to the mmc */
3323   m.mmw_netw_id_l = psa.psa_nwid[1];
3324   m.mmw_netw_id_h = psa.psa_nwid[0];
3325   
3326   if(psa.psa_nwid_select & 1)
3327     m.mmw_loopt_sel = 0x00;
3328   else
3329     m.mmw_loopt_sel = MMW_LOOPT_SEL_DIS_NWID;
3330
3331   memcpy(&m.mmw_encr_key, &psa.psa_encryption_key, 
3332          sizeof(m.mmw_encr_key));
3333
3334   if(psa.psa_encryption_select)
3335     m.mmw_encr_enable = MMW_ENCR_ENABLE_EN | MMW_ENCR_ENABLE_MODE;
3336   else
3337     m.mmw_encr_enable = 0;
3338
3339   m.mmw_thr_pre_set = psa.psa_thr_pre_set & 0x3F;
3340   m.mmw_quality_thr = psa.psa_quality_thr & 0x0F;
3341
3342   /*
3343    * Set default modem control parameters.
3344    * See NCR document 407-0024326 Rev. A.
3345    */
3346   m.mmw_jabber_enable = 0x01;
3347   m.mmw_anten_sel = MMW_ANTEN_SEL_ALG_EN;
3348   m.mmw_ifs = 0x20;
3349   m.mmw_mod_delay = 0x04;
3350   m.mmw_jam_time = 0x38;
3351
3352   m.mmw_des_io_invert = 0;
3353   m.mmw_freeze = 0;
3354   m.mmw_decay_prm = 0;
3355   m.mmw_decay_updat_prm = 0;
3356
3357   /* Write all info to mmc */
3358   mmc_write(base, 0, (u_char *)&m, sizeof(m));
3359
3360   /* The following code start the modem of the 2.00 frequency
3361    * selectable cards at power on. It's not strictly needed for the
3362    * following boots...
3363    * The original patch was by Joe Finney for the PCMCIA driver, but
3364    * I've cleaned it a bit and add documentation.
3365    * Thanks to Loeke Brederveld from Lucent for the info.
3366    */
3367
3368   /* Attempt to recognise 2.00 cards (2.4 GHz frequency selectable)
3369    * (does it work for everybody ? - especially old cards...) */
3370   /* Note : WFREQSEL verify that it is able to read from EEprom
3371    * a sensible frequency (address 0x00) + that MMR_FEE_STATUS_ID
3372    * is 0xA (Xilinx version) or 0xB (Ariadne version).
3373    * My test is more crude but do work... */
3374   if(!(mmc_in(base, mmroff(0, mmr_fee_status)) &
3375        (MMR_FEE_STATUS_DWLD | MMR_FEE_STATUS_BUSY)))
3376     {
3377       /* We must download the frequency parameters to the
3378        * synthetisers (from the EEprom - area 1)
3379        * Note : as the EEprom is auto decremented, we set the end
3380        * if the area... */
3381       m.mmw_fee_addr = 0x0F;
3382       m.mmw_fee_ctrl = MMW_FEE_CTRL_READ | MMW_FEE_CTRL_DWLD;
3383       mmc_write(base, (char *)&m.mmw_fee_ctrl - (char *)&m,
3384                 (unsigned char *)&m.mmw_fee_ctrl, 2);
3385
3386       /* Wait until the download is finished */
3387       fee_wait(base, 100, 100);
3388
3389 #ifdef DEBUG_CONFIG_INFO
3390       /* The frequency was in the last word downloaded... */
3391       mmc_read(base, (char *)&m.mmw_fee_data_l - (char *)&m,
3392                (unsigned char *)&m.mmw_fee_data_l, 2);
3393
3394       /* Print some info for the user */
3395       printk(KERN_DEBUG "%s: Wavelan 2.00 recognised (frequency select) : Current frequency = %ld\n",
3396              dev->name,
3397              ((m.mmw_fee_data_h << 4) |
3398               (m.mmw_fee_data_l >> 4)) * 5 / 2 + 24000L);
3399 #endif
3400
3401       /* We must now download the power adjust value (gain) to
3402        * the synthetisers (from the EEprom - area 7 - DAC) */
3403       m.mmw_fee_addr = 0x61;
3404       m.mmw_fee_ctrl = MMW_FEE_CTRL_READ | MMW_FEE_CTRL_DWLD;
3405       mmc_write(base, (char *)&m.mmw_fee_ctrl - (char *)&m,
3406                 (unsigned char *)&m.mmw_fee_ctrl, 2);
3407
3408       /* Wait until the download is finished */
3409     }   /* if 2.00 card */
3410
3411 #ifdef DEBUG_CONFIG_TRACE
3412   printk(KERN_DEBUG "%s: <-wv_mmc_init()\n", dev->name);
3413 #endif
3414   return TRUE;
3415 }
3416
3417 /*------------------------------------------------------------------*/
3418 /*
3419  * Routine to gracefully turn off reception, and wait for any commands
3420  * to complete.
3421  * (called in wv_ru_start() and wavelan_close() and wavelan_event())
3422  */
3423 static int
3424 wv_ru_stop(struct net_device *  dev)
3425 {
3426   ioaddr_t      base = dev->base_addr;
3427   net_local *   lp = netdev_priv(dev);
3428   unsigned long flags;
3429   int           status;
3430   int           spin;
3431
3432 #ifdef DEBUG_CONFIG_TRACE
3433   printk(KERN_DEBUG "%s: ->wv_ru_stop()\n", dev->name);
3434 #endif
3435
3436   spin_lock_irqsave(&lp->spinlock, flags);
3437
3438   /* First, send the LAN controller a stop receive command */
3439   wv_82593_cmd(dev, "wv_graceful_shutdown(): stop-rcv",
3440                OP0_STOP_RCV, SR0_NO_RESULT);
3441
3442   /* Then, spin until the receive unit goes idle */
3443   spin = 300;
3444   do
3445     {
3446       udelay(10);
3447       outb(OP0_NOP | CR0_STATUS_3, LCCR(base));
3448       status = inb(LCSR(base));
3449     }
3450   while(((status & SR3_RCV_STATE_MASK) != SR3_RCV_IDLE) && (spin-- > 0));
3451
3452   /* Now, spin until the chip finishes executing its current command */
3453   do
3454     {
3455       udelay(10);
3456       outb(OP0_NOP | CR0_STATUS_3, LCCR(base));
3457       status = inb(LCSR(base));
3458     }
3459   while(((status & SR3_EXEC_STATE_MASK) != SR3_EXEC_IDLE) && (spin-- > 0));
3460
3461   spin_unlock_irqrestore(&lp->spinlock, flags);
3462
3463   /* If there was a problem */
3464   if(spin <= 0)
3465     {
3466 #ifdef DEBUG_CONFIG_ERROR
3467       printk(KERN_INFO "%s: wv_ru_stop(): The chip doesn't want to stop...\n",
3468              dev->name);
3469 #endif
3470       return FALSE;
3471     }
3472
3473 #ifdef DEBUG_CONFIG_TRACE
3474   printk(KERN_DEBUG "%s: <-wv_ru_stop()\n", dev->name);
3475 #endif
3476   return TRUE;
3477 } /* wv_ru_stop */
3478
3479 /*------------------------------------------------------------------*/
3480 /*
3481  * This routine starts the receive unit running.  First, it checks if
3482  * the card is actually ready. Then the card is instructed to receive
3483  * packets again.
3484  * (called in wv_hw_reset() & wavelan_open())
3485  */
3486 static int
3487 wv_ru_start(struct net_device * dev)
3488 {
3489   ioaddr_t      base = dev->base_addr;
3490   net_local *   lp = netdev_priv(dev);
3491   unsigned long flags;
3492
3493 #ifdef DEBUG_CONFIG_TRACE
3494   printk(KERN_DEBUG "%s: ->wv_ru_start()\n", dev->name);
3495 #endif
3496
3497   /*
3498    * We need to start from a quiescent state. To do so, we could check
3499    * if the card is already running, but instead we just try to shut
3500    * it down. First, we disable reception (in case it was already enabled).
3501    */
3502   if(!wv_ru_stop(dev))
3503     return FALSE;
3504
3505   spin_lock_irqsave(&lp->spinlock, flags);
3506
3507   /* Now we know that no command is being executed. */
3508
3509   /* Set the receive frame pointer and stop pointer */
3510   lp->rfp = 0;
3511   outb(OP0_SWIT_TO_PORT_1 | CR0_CHNL, LCCR(base));
3512
3513   /* Reset ring management.  This sets the receive frame pointer to 1 */
3514   outb(OP1_RESET_RING_MNGMT, LCCR(base));
3515
3516 #if 0
3517   /* XXX the i82593 manual page 6-4 seems to indicate that the stop register
3518      should be set as below */
3519   /* outb(CR1_STOP_REG_UPDATE|((RX_SIZE - 0x40)>> RX_SIZE_SHIFT),LCCR(base));*/
3520 #elif 0
3521   /* but I set it 0 instead */
3522   lp->stop = 0;
3523 #else
3524   /* but I set it to 3 bytes per packet less than 8K */
3525   lp->stop = (0 + RX_SIZE - ((RX_SIZE / 64) * 3)) % RX_SIZE;
3526 #endif
3527   outb(CR1_STOP_REG_UPDATE | (lp->stop >> RX_SIZE_SHIFT), LCCR(base));
3528   outb(OP1_INT_ENABLE, LCCR(base));
3529   outb(OP1_SWIT_TO_PORT_0, LCCR(base));
3530
3531   /* Reset receive DMA pointer */
3532   hacr_write_slow(base, HACR_PWR_STAT | HACR_TX_DMA_RESET);
3533   hacr_write_slow(base, HACR_DEFAULT);
3534
3535   /* Receive DMA on channel 1 */
3536   wv_82593_cmd(dev, "wv_ru_start(): rcv-enable",
3537                CR0_CHNL | OP0_RCV_ENABLE, SR0_NO_RESULT);
3538
3539 #ifdef DEBUG_I82593_SHOW
3540   {
3541     int status;
3542     int opri;
3543     int spin = 10000;
3544
3545     /* spin until the chip starts receiving */
3546     do
3547       {
3548         outb(OP0_NOP | CR0_STATUS_3, LCCR(base));
3549         status = inb(LCSR(base));
3550         if(spin-- <= 0)
3551           break;
3552       }
3553     while(((status & SR3_RCV_STATE_MASK) != SR3_RCV_ACTIVE) &&
3554           ((status & SR3_RCV_STATE_MASK) != SR3_RCV_READY));
3555     printk(KERN_DEBUG "rcv status is 0x%x [i:%d]\n",
3556            (status & SR3_RCV_STATE_MASK), i);
3557   }
3558 #endif
3559
3560   spin_unlock_irqrestore(&lp->spinlock, flags);
3561
3562 #ifdef DEBUG_CONFIG_TRACE
3563   printk(KERN_DEBUG "%s: <-wv_ru_start()\n", dev->name);
3564 #endif
3565   return TRUE;
3566 }
3567
3568 /*------------------------------------------------------------------*/
3569 /*
3570  * This routine does a standard config of the WaveLAN controller (i82593).
3571  * In the ISA driver, this is integrated in wavelan_hardware_reset()
3572  * (called by wv_hw_config(), wv_82593_reconfig() & wavelan_packet_xmit())
3573  */
3574 static int
3575 wv_82593_config(struct net_device *     dev)
3576 {
3577   ioaddr_t                      base = dev->base_addr;
3578   net_local *                   lp = netdev_priv(dev);
3579   struct i82593_conf_block      cfblk;
3580   int                           ret = TRUE;
3581
3582 #ifdef DEBUG_CONFIG_TRACE
3583   printk(KERN_DEBUG "%s: ->wv_82593_config()\n", dev->name);
3584 #endif
3585
3586   /* Create & fill i82593 config block
3587    *
3588    * Now conform to Wavelan document WCIN085B
3589    */
3590   memset(&cfblk, 0x00, sizeof(struct i82593_conf_block));
3591   cfblk.d6mod = FALSE;          /* Run in i82593 advanced mode */
3592   cfblk.fifo_limit = 5;         /* = 56 B rx and 40 B tx fifo thresholds */
3593   cfblk.forgnesi = FALSE;       /* 0=82C501, 1=AMD7992B compatibility */
3594   cfblk.fifo_32 = 1;
3595   cfblk.throttle_enb = FALSE;
3596   cfblk.contin = TRUE;          /* enable continuous mode */
3597   cfblk.cntrxint = FALSE;       /* enable continuous mode receive interrupts */
3598   cfblk.addr_len = WAVELAN_ADDR_SIZE;
3599   cfblk.acloc = TRUE;           /* Disable source addr insertion by i82593 */
3600   cfblk.preamb_len = 0;         /* 2 bytes preamble (SFD) */
3601   cfblk.loopback = FALSE;
3602   cfblk.lin_prio = 0;           /* conform to 802.3 backoff algoritm */
3603   cfblk.exp_prio = 5;           /* conform to 802.3 backoff algoritm */
3604   cfblk.bof_met = 1;            /* conform to 802.3 backoff algoritm */
3605   cfblk.ifrm_spc = 0x20;        /* 32 bit times interframe spacing */
3606   cfblk.slottim_low = 0x20;     /* 32 bit times slot time */
3607   cfblk.slottim_hi = 0x0;
3608   cfblk.max_retr = 15;
3609   cfblk.prmisc = ((lp->promiscuous) ? TRUE: FALSE);     /* Promiscuous mode */
3610   cfblk.bc_dis = FALSE;         /* Enable broadcast reception */
3611   cfblk.crs_1 = TRUE;           /* Transmit without carrier sense */
3612   cfblk.nocrc_ins = FALSE;      /* i82593 generates CRC */      
3613   cfblk.crc_1632 = FALSE;       /* 32-bit Autodin-II CRC */
3614   cfblk.crs_cdt = FALSE;        /* CD not to be interpreted as CS */
3615   cfblk.cs_filter = 0;          /* CS is recognized immediately */
3616   cfblk.crs_src = FALSE;        /* External carrier sense */
3617   cfblk.cd_filter = 0;          /* CD is recognized immediately */
3618   cfblk.min_fr_len = ETH_ZLEN >> 2;     /* Minimum frame length 64 bytes */
3619   cfblk.lng_typ = FALSE;        /* Length field > 1500 = type field */
3620   cfblk.lng_fld = TRUE;         /* Disable 802.3 length field check */
3621   cfblk.rxcrc_xf = TRUE;        /* Don't transfer CRC to memory */
3622   cfblk.artx = TRUE;            /* Disable automatic retransmission */
3623   cfblk.sarec = TRUE;           /* Disable source addr trig of CD */
3624   cfblk.tx_jabber = TRUE;       /* Disable jabber jam sequence */
3625   cfblk.hash_1 = FALSE;         /* Use bits 0-5 in mc address hash */
3626   cfblk.lbpkpol = TRUE;         /* Loopback pin active high */
3627   cfblk.fdx = FALSE;            /* Disable full duplex operation */
3628   cfblk.dummy_6 = 0x3f;         /* all ones */
3629   cfblk.mult_ia = FALSE;        /* No multiple individual addresses */
3630   cfblk.dis_bof = FALSE;        /* Disable the backoff algorithm ?! */
3631   cfblk.dummy_1 = TRUE;         /* set to 1 */
3632   cfblk.tx_ifs_retrig = 3;      /* Hmm... Disabled */
3633 #ifdef MULTICAST_ALL
3634   cfblk.mc_all = (lp->allmulticast ? TRUE: FALSE);      /* Allow all multicasts */
3635 #else
3636   cfblk.mc_all = FALSE;         /* No multicast all mode */
3637 #endif
3638   cfblk.rcv_mon = 0;            /* Monitor mode disabled */
3639   cfblk.frag_acpt = TRUE;       /* Do not accept fragments */
3640   cfblk.tstrttrs = FALSE;       /* No start transmission threshold */
3641   cfblk.fretx = TRUE;           /* FIFO automatic retransmission */
3642   cfblk.syncrqs = FALSE;        /* Synchronous DRQ deassertion... */
3643   cfblk.sttlen = TRUE;          /* 6 byte status registers */
3644   cfblk.rx_eop = TRUE;          /* Signal EOP on packet reception */
3645   cfblk.tx_eop = TRUE;          /* Signal EOP on packet transmission */
3646   cfblk.rbuf_size = RX_SIZE>>11;        /* Set receive buffer size */
3647   cfblk.rcvstop = TRUE;         /* Enable Receive Stop Register */
3648
3649 #ifdef DEBUG_I82593_SHOW
3650   {
3651     u_char *c = (u_char *) &cfblk;
3652     int i;
3653     printk(KERN_DEBUG "wavelan_cs: config block:");
3654     for(i = 0; i < sizeof(struct i82593_conf_block); i++,c++)
3655       {
3656         if((i % 16) == 0) printk("\n" KERN_DEBUG);
3657         printk("%02x ", *c);
3658       }
3659     printk("\n");
3660   }
3661 #endif
3662
3663   /* Copy the config block to the i82593 */
3664   outb(TX_BASE & 0xff, PIORL(base));
3665   outb(((TX_BASE >> 8) & PIORH_MASK) | PIORH_SEL_TX, PIORH(base));
3666   outb(sizeof(struct i82593_conf_block) & 0xff, PIOP(base));    /* lsb */
3667   outb(sizeof(struct i82593_conf_block) >> 8, PIOP(base));      /* msb */
3668   outsb(PIOP(base), (char *) &cfblk, sizeof(struct i82593_conf_block));
3669
3670   /* reset transmit DMA pointer */
3671   hacr_write_slow(base, HACR_PWR_STAT | HACR_TX_DMA_RESET);
3672   hacr_write(base, HACR_DEFAULT);
3673   if(!wv_82593_cmd(dev, "wv_82593_config(): configure",
3674                    OP0_CONFIGURE, SR0_CONFIGURE_DONE))
3675     ret = FALSE;
3676
3677   /* Initialize adapter's ethernet MAC address */
3678   outb(TX_BASE & 0xff, PIORL(base));
3679   outb(((TX_BASE >> 8) & PIORH_MASK) | PIORH_SEL_TX, PIORH(base));
3680   outb(WAVELAN_ADDR_SIZE, PIOP(base));  /* byte count lsb */
3681   outb(0, PIOP(base));                  /* byte count msb */
3682   outsb(PIOP(base), &dev->dev_addr[0], WAVELAN_ADDR_SIZE);
3683
3684   /* reset transmit DMA pointer */
3685   hacr_write_slow(base, HACR_PWR_STAT | HACR_TX_DMA_RESET);
3686   hacr_write(base, HACR_DEFAULT);
3687   if(!wv_82593_cmd(dev, "wv_82593_config(): ia-setup",
3688                    OP0_IA_SETUP, SR0_IA_SETUP_DONE))
3689     ret = FALSE;
3690
3691 #ifdef WAVELAN_ROAMING
3692     /* If roaming is enabled, join the "Beacon Request" multicast group... */
3693     /* But only if it's not in there already! */
3694   if(do_roaming)
3695     dev_mc_add(dev,WAVELAN_BEACON_ADDRESS, WAVELAN_ADDR_SIZE, 1);
3696 #endif  /* WAVELAN_ROAMING */
3697
3698   /* If any multicast address to set */
3699   if(lp->mc_count)
3700     {
3701       struct dev_mc_list *      dmi;
3702       int                       addrs_len = WAVELAN_ADDR_SIZE * lp->mc_count;
3703
3704 #ifdef DEBUG_CONFIG_INFO
3705       printk(KERN_DEBUG "%s: wv_hw_config(): set %d multicast addresses:\n",
3706              dev->name, lp->mc_count);
3707       for(dmi=dev->mc_list; dmi; dmi=dmi->next)
3708         printk(KERN_DEBUG " %02x:%02x:%02x:%02x:%02x:%02x\n",
3709                dmi->dmi_addr[0], dmi->dmi_addr[1], dmi->dmi_addr[2],
3710                dmi->dmi_addr[3], dmi->dmi_addr[4], dmi->dmi_addr[5] );
3711 #endif
3712
3713       /* Initialize adapter's ethernet multicast addresses */
3714       outb(TX_BASE & 0xff, PIORL(base));
3715       outb(((TX_BASE >> 8) & PIORH_MASK) | PIORH_SEL_TX, PIORH(base));
3716       outb(addrs_len & 0xff, PIOP(base));       /* byte count lsb */
3717       outb((addrs_len >> 8), PIOP(base));       /* byte count msb */
3718       for(dmi=dev->mc_list; dmi; dmi=dmi->next)
3719         outsb(PIOP(base), dmi->dmi_addr, dmi->dmi_addrlen);
3720
3721       /* reset transmit DMA pointer */
3722       hacr_write_slow(base, HACR_PWR_STAT | HACR_TX_DMA_RESET);
3723       hacr_write(base, HACR_DEFAULT);
3724       if(!wv_82593_cmd(dev, "wv_82593_config(): mc-setup",
3725                        OP0_MC_SETUP, SR0_MC_SETUP_DONE))
3726         ret = FALSE;
3727       lp->mc_count = dev->mc_count;     /* remember to avoid repeated reset */
3728     }
3729
3730   /* Job done, clear the flag */
3731   lp->reconfig_82593 = FALSE;
3732
3733 #ifdef DEBUG_CONFIG_TRACE
3734   printk(KERN_DEBUG "%s: <-wv_82593_config()\n", dev->name);
3735 #endif
3736   return(ret);
3737 }
3738
3739 /*------------------------------------------------------------------*/
3740 /*
3741  * Read the Access Configuration Register, perform a software reset,
3742  * and then re-enable the card's software.
3743  *
3744  * If I understand correctly : reset the pcmcia interface of the
3745  * wavelan.
3746  * (called by wv_config())
3747  */
3748 static inline int
3749 wv_pcmcia_reset(struct net_device *     dev)
3750 {
3751   int           i;
3752   conf_reg_t    reg = { 0, CS_READ, CISREG_COR, 0 };
3753   dev_link_t *  link = ((net_local *)netdev_priv(dev))->link;
3754
3755 #ifdef DEBUG_CONFIG_TRACE
3756   printk(KERN_DEBUG "%s: ->wv_pcmcia_reset()\n", dev->name);
3757 #endif
3758
3759   i = pcmcia_access_configuration_register(link->handle, &reg);
3760   if(i != CS_SUCCESS)
3761     {
3762       cs_error(link->handle, AccessConfigurationRegister, i);
3763       return FALSE;
3764     }
3765       
3766 #ifdef DEBUG_CONFIG_INFO
3767   printk(KERN_DEBUG "%s: wavelan_pcmcia_reset(): Config reg is 0x%x\n",
3768          dev->name, (u_int) reg.Value);
3769 #endif
3770
3771   reg.Action = CS_WRITE;
3772   reg.Value = reg.Value | COR_SW_RESET;
3773   i = pcmcia_access_configuration_register(link->handle, &reg);
3774   if(i != CS_SUCCESS)
3775     {
3776       cs_error(link->handle, AccessConfigurationRegister, i);
3777       return FALSE;
3778     }
3779       
3780   reg.Action = CS_WRITE;
3781   reg.Value = COR_LEVEL_IRQ | COR_CONFIG;
3782   i = pcmcia_access_configuration_register(link->handle, &reg);
3783   if(i != CS_SUCCESS)
3784     {
3785       cs_error(link->handle, AccessConfigurationRegister, i);
3786       return FALSE;
3787     }
3788
3789 #ifdef DEBUG_CONFIG_TRACE
3790   printk(KERN_DEBUG "%s: <-wv_pcmcia_reset()\n", dev->name);
3791 #endif
3792   return TRUE;
3793 }
3794
3795 /*------------------------------------------------------------------*/
3796 /*
3797  * wavelan_hw_config() is called after a CARD_INSERTION event is
3798  * received, to configure the wavelan hardware.
3799  * Note that the reception will be enabled in wavelan->open(), so the
3800  * device is configured but idle...
3801  * Performs the following actions:
3802  *      1. A pcmcia software reset (using wv_pcmcia_reset())
3803  *      2. A power reset (reset DMA)
3804  *      3. Reset the LAN controller
3805  *      4. Initialize the radio modem (using wv_mmc_init)
3806  *      5. Configure LAN controller (using wv_82593_config)
3807  *      6. Perform a diagnostic on the LAN controller
3808  * (called by wavelan_event() & wv_hw_reset())
3809  */
3810 static int
3811 wv_hw_config(struct net_device *        dev)
3812 {
3813   net_local *           lp = netdev_priv(dev);
3814   ioaddr_t              base = dev->base_addr;
3815   unsigned long         flags;
3816   int                   ret = FALSE;
3817
3818 #ifdef DEBUG_CONFIG_TRACE
3819   printk(KERN_DEBUG "%s: ->wv_hw_config()\n", dev->name);
3820 #endif
3821
3822 #ifdef STRUCT_CHECK
3823   if(wv_structuct_check() != (char *) NULL)
3824     {
3825       printk(KERN_WARNING "%s: wv_hw_config: structure/compiler botch: \"%s\"\n",
3826              dev->name, wv_structuct_check());
3827       return FALSE;
3828     }
3829 #endif  /* STRUCT_CHECK == 1 */
3830
3831   /* Reset the pcmcia interface */
3832   if(wv_pcmcia_reset(dev) == FALSE)
3833     return FALSE;
3834
3835   /* Disable interrupts */
3836   spin_lock_irqsave(&lp->spinlock, flags);
3837
3838   /* Disguised goto ;-) */
3839   do
3840     {
3841       /* Power UP the module + reset the modem + reset host adapter
3842        * (in fact, reset DMA channels) */
3843       hacr_write_slow(base, HACR_RESET);
3844       hacr_write(base, HACR_DEFAULT);
3845
3846       /* Check if the module has been powered up... */
3847       if(hasr_read(base) & HASR_NO_CLK)
3848         {
3849 #ifdef DEBUG_CONFIG_ERRORS
3850           printk(KERN_WARNING "%s: wv_hw_config(): modem not connected or not a wavelan card\n",
3851                  dev->name);
3852 #endif
3853           break;
3854         }
3855
3856       /* initialize the modem */
3857       if(wv_mmc_init(dev) == FALSE)
3858         {
3859 #ifdef DEBUG_CONFIG_ERRORS
3860           printk(KERN_WARNING "%s: wv_hw_config(): Can't configure the modem\n",
3861                  dev->name);
3862 #endif
3863           break;
3864         }
3865
3866       /* reset the LAN controller (i82593) */
3867       outb(OP0_RESET, LCCR(base));
3868       mdelay(1);        /* A bit crude ! */
3869
3870       /* Initialize the LAN controller */
3871       if(wv_82593_config(dev) == FALSE)
3872         {
3873 #ifdef DEBUG_CONFIG_ERRORS
3874           printk(KERN_INFO "%s: wv_hw_config(): i82593 init failed\n",
3875                  dev->name);
3876 #endif
3877           break;
3878         }
3879
3880       /* Diagnostic */
3881       if(wv_diag(dev) == FALSE)
3882         {
3883 #ifdef DEBUG_CONFIG_ERRORS
3884           printk(KERN_INFO "%s: wv_hw_config(): i82593 diagnostic failed\n",
3885                  dev->name);
3886 #endif
3887           break;
3888         }
3889
3890       /* 
3891        * insert code for loopback test here
3892        */
3893
3894       /* The device is now configured */
3895       lp->configured = 1;
3896       ret = TRUE;
3897     }
3898   while(0);
3899
3900   /* Re-enable interrupts */
3901   spin_unlock_irqrestore(&lp->spinlock, flags);
3902
3903 #ifdef DEBUG_CONFIG_TRACE
3904   printk(KERN_DEBUG "%s: <-wv_hw_config()\n", dev->name);
3905 #endif
3906   return(ret);
3907 }
3908
3909 /*------------------------------------------------------------------*/
3910 /*
3911  * Totally reset the wavelan and restart it.
3912  * Performs the following actions:
3913  *      1. Call wv_hw_config()
3914  *      2. Start the LAN controller's receive unit
3915  * (called by wavelan_event(), wavelan_watchdog() and wavelan_open())
3916  */
3917 static inline void
3918 wv_hw_reset(struct net_device * dev)
3919 {
3920   net_local *   lp = netdev_priv(dev);
3921
3922 #ifdef DEBUG_CONFIG_TRACE
3923   printk(KERN_DEBUG "%s: ->wv_hw_reset()\n", dev->name);
3924 #endif
3925
3926   lp->nresets++;
3927   lp->configured = 0;
3928   
3929   /* Call wv_hw_config() for most of the reset & init stuff */
3930   if(wv_hw_config(dev) == FALSE)
3931     return;
3932
3933   /* start receive unit */
3934   wv_ru_start(dev);
3935
3936 #ifdef DEBUG_CONFIG_TRACE
3937   printk(KERN_DEBUG "%s: <-wv_hw_reset()\n", dev->name);
3938 #endif
3939 }
3940
3941 /*------------------------------------------------------------------*/
3942 /*
3943  * wv_pcmcia_config() is called after a CARD_INSERTION event is
3944  * received, to configure the PCMCIA socket, and to make the ethernet
3945  * device available to the system.
3946  * (called by wavelan_event())
3947  */
3948 static inline int
3949 wv_pcmcia_config(dev_link_t *   link)
3950 {
3951   client_handle_t       handle;
3952   tuple_t               tuple;
3953   cisparse_t            parse;
3954   struct net_device *   dev;
3955   int                   i;
3956   u_char                buf[64];
3957   win_req_t             req;
3958   memreq_t              mem;
3959
3960   handle = link->handle;
3961   dev = (struct net_device *) link->priv;
3962
3963 #ifdef DEBUG_CONFIG_TRACE
3964   printk(KERN_DEBUG "->wv_pcmcia_config(0x%p)\n", link);
3965 #endif
3966
3967   /*
3968    * This reads the card's CONFIG tuple to find its configuration
3969    * registers.
3970    */
3971   do
3972     {
3973       tuple.Attributes = 0;
3974       tuple.DesiredTuple = CISTPL_CONFIG;
3975       i = pcmcia_get_first_tuple(handle, &tuple);
3976       if(i != CS_SUCCESS)
3977         break;
3978       tuple.TupleData = (cisdata_t *)buf;
3979       tuple.TupleDataMax = 64;
3980       tuple.TupleOffset = 0;
3981       i = pcmcia_get_tuple_data(handle, &tuple);
3982       if(i != CS_SUCCESS)
3983         break;
3984       i = pcmcia_parse_tuple(handle, &tuple, &parse);
3985       if(i != CS_SUCCESS)
3986         break;
3987       link->conf.ConfigBase = parse.config.base;
3988       link->conf.Present = parse.config.rmask[0];
3989     }
3990   while(0);
3991   if(i != CS_SUCCESS)
3992     {
3993       cs_error(link->handle, ParseTuple, i);
3994       link->state &= ~DEV_CONFIG_PENDING;
3995       return FALSE;
3996     }
3997     
3998   /* Configure card */
3999   link->state |= DEV_CONFIG;
4000   do
4001     {
4002       i = pcmcia_request_io(link->handle, &link->io);
4003       if(i != CS_SUCCESS)
4004         {
4005           cs_error(link->handle, RequestIO, i);
4006           break;
4007         }
4008
4009       /*
4010        * Now allocate an interrupt line.  Note that this does not
4011        * actually assign a handler to the interrupt.
4012        */
4013       i = pcmcia_request_irq(link->handle, &link->irq);
4014       if(i != CS_SUCCESS)
4015         {
4016           cs_error(link->handle, RequestIRQ, i);
4017           break;
4018         }
4019
4020       /*
4021        * This actually configures the PCMCIA socket -- setting up
4022        * the I/O windows and the interrupt mapping.
4023        */
4024       link->conf.ConfigIndex = 1;
4025       i = pcmcia_request_configuration(link->handle, &link->conf);
4026       if(i != CS_SUCCESS)
4027         {
4028           cs_error(link->handle, RequestConfiguration, i);
4029           break;
4030         }
4031
4032       /*
4033        * Allocate a small memory window.  Note that the dev_link_t
4034        * structure provides space for one window handle -- if your
4035        * device needs several windows, you'll need to keep track of
4036        * the handles in your private data structure, link->priv.
4037        */
4038       req.Attributes = WIN_DATA_WIDTH_8|WIN_MEMORY_TYPE_AM|WIN_ENABLE;
4039       req.Base = req.Size = 0;
4040       req.AccessSpeed = mem_speed;
4041       i = pcmcia_request_window(&link->handle, &req, &link->win);
4042       if(i != CS_SUCCESS)
4043         {
4044           cs_error(link->handle, RequestWindow, i);
4045           break;
4046         }
4047
4048       dev->mem_start = (u_long)ioremap(req.Base, req.Size);
4049       dev->mem_end = dev->mem_start + req.Size;
4050
4051       mem.CardOffset = 0; mem.Page = 0;
4052       i = pcmcia_map_mem_page(link->win, &mem);
4053       if(i != CS_SUCCESS)
4054         {
4055           cs_error(link->handle, MapMemPage, i);
4056           break;
4057         }
4058
4059       /* Feed device with this info... */
4060       dev->irq = link->irq.AssignedIRQ;
4061       dev->base_addr = link->io.BasePort1;
4062       netif_start_queue(dev);
4063
4064 #ifdef DEBUG_CONFIG_INFO
4065       printk(KERN_DEBUG "wv_pcmcia_config: MEMSTART 0x%x IRQ %d IOPORT 0x%x\n",
4066              (u_int) dev->mem_start, dev->irq, (u_int) dev->base_addr);
4067 #endif
4068
4069       i = register_netdev(dev);
4070       if(i != 0)
4071         {
4072 #ifdef DEBUG_CONFIG_ERRORS
4073           printk(KERN_INFO "wv_pcmcia_config(): register_netdev() failed\n");
4074 #endif
4075           break;
4076         }
4077     }
4078   while(0);             /* Humm... Disguised goto !!! */
4079
4080   link->state &= ~DEV_CONFIG_PENDING;
4081   /* If any step failed, release any partially configured state */
4082   if(i != 0)
4083     {
4084       wv_pcmcia_release(link);
4085       return FALSE;
4086     }
4087
4088   strcpy(((net_local *) netdev_priv(dev))->node.dev_name, dev->name);
4089   link->dev = &((net_local *) netdev_priv(dev))->node;
4090
4091 #ifdef DEBUG_CONFIG_TRACE
4092   printk(KERN_DEBUG "<-wv_pcmcia_config()\n");
4093 #endif
4094   return TRUE;
4095 }
4096
4097 /*------------------------------------------------------------------*/
4098 /*
4099  * After a card is removed, wv_pcmcia_release() will unregister the net
4100  * device, and release the PCMCIA configuration.  If the device is
4101  * still open, this will be postponed until it is closed.
4102  */
4103 static void
4104 wv_pcmcia_release(dev_link_t *link)
4105 {
4106   struct net_device *   dev = (struct net_device *) link->priv;
4107
4108 #ifdef DEBUG_CONFIG_TRACE
4109   printk(KERN_DEBUG "%s: -> wv_pcmcia_release(0x%p)\n", dev->name, link);
4110 #endif
4111
4112   /* Don't bother checking to see if these succeed or not */
4113   iounmap((u_char *)dev->mem_start);
4114   pcmcia_release_window(link->win);
4115   pcmcia_release_configuration(link->handle);
4116   pcmcia_release_io(link->handle, &link->io);
4117   pcmcia_release_irq(link->handle, &link->irq);
4118
4119   link->state &= ~DEV_CONFIG;
4120
4121 #ifdef DEBUG_CONFIG_TRACE
4122   printk(KERN_DEBUG "%s: <- wv_pcmcia_release()\n", dev->name);
4123 #endif
4124 }
4125
4126 /************************ INTERRUPT HANDLING ************************/
4127
4128 /*
4129  * This function is the interrupt handler for the WaveLAN card. This
4130  * routine will be called whenever: 
4131  *      1. A packet is received.
4132  *      2. A packet has successfully been transferred and the unit is
4133  *         ready to transmit another packet.
4134  *      3. A command has completed execution.
4135  */
4136 static irqreturn_t
4137 wavelan_interrupt(int           irq,
4138                   void *        dev_id,
4139                   struct pt_regs * regs)
4140 {
4141   struct net_device *   dev;
4142   net_local *   lp;
4143   ioaddr_t      base;
4144   int           status0;
4145   u_int         tx_status;
4146
4147   if ((dev = dev_id) == NULL)
4148     {
4149 #ifdef DEBUG_INTERRUPT_ERROR
4150       printk(KERN_WARNING "wavelan_interrupt(): irq %d for unknown device.\n",
4151              irq);
4152 #endif
4153       return IRQ_NONE;
4154     }
4155
4156 #ifdef DEBUG_INTERRUPT_TRACE
4157   printk(KERN_DEBUG "%s: ->wavelan_interrupt()\n", dev->name);
4158 #endif
4159
4160   lp = netdev_priv(dev);
4161   base = dev->base_addr;
4162
4163 #ifdef DEBUG_INTERRUPT_INFO
4164   /* Check state of our spinlock (it should be cleared) */
4165   if(spin_is_locked(&lp->spinlock))
4166     printk(KERN_DEBUG
4167            "%s: wavelan_interrupt(): spinlock is already locked !!!\n",
4168            dev->name);
4169 #endif
4170
4171   /* Prevent reentrancy. We need to do that because we may have
4172    * multiple interrupt handler running concurently.
4173    * It is safe because interrupts are disabled before aquiring
4174    * the spinlock. */
4175   spin_lock(&lp->spinlock);
4176
4177   /* Treat all pending interrupts */
4178   while(1)
4179     {
4180       /* ---------------- INTERRUPT CHECKING ---------------- */
4181       /*
4182        * Look for the interrupt and verify the validity
4183        */
4184       outb(CR0_STATUS_0 | OP0_NOP, LCCR(base));
4185       status0 = inb(LCSR(base));
4186
4187 #ifdef DEBUG_INTERRUPT_INFO
4188       printk(KERN_DEBUG "status0 0x%x [%s => 0x%x]", status0, 
4189              (status0&SR0_INTERRUPT)?"int":"no int",status0&~SR0_INTERRUPT);
4190       if(status0&SR0_INTERRUPT)
4191         {
4192           printk(" [%s => %d]\n", (status0 & SR0_CHNL) ? "chnl" :
4193                  ((status0 & SR0_EXECUTION) ? "cmd" :
4194                   ((status0 & SR0_RECEPTION) ? "recv" : "unknown")),
4195                  (status0 & SR0_EVENT_MASK));
4196         }
4197       else
4198         printk("\n");
4199 #endif
4200
4201       /* Return if no actual interrupt from i82593 (normal exit) */
4202       if(!(status0 & SR0_INTERRUPT))
4203         break;
4204
4205       /* If interrupt is both Rx and Tx or none...
4206        * This code in fact is there to catch the spurious interrupt
4207        * when you remove the wavelan pcmcia card from the socket */
4208       if(((status0 & SR0_BOTH_RX_TX) == SR0_BOTH_RX_TX) ||
4209          ((status0 & SR0_BOTH_RX_TX) == 0x0))
4210         {
4211 #ifdef DEBUG_INTERRUPT_INFO
4212           printk(KERN_INFO "%s: wv_interrupt(): bogus interrupt (or from dead card) : %X\n",
4213                  dev->name, status0);
4214 #endif
4215           /* Acknowledge the interrupt */
4216           outb(CR0_INT_ACK | OP0_NOP, LCCR(base));
4217           break;
4218         }
4219
4220       /* ----------------- RECEIVING PACKET ----------------- */
4221       /*
4222        * When the wavelan signal the reception of a new packet,
4223        * we call wv_packet_rcv() to copy if from the buffer and
4224        * send it to NET3
4225        */
4226       if(status0 & SR0_RECEPTION)
4227         {
4228 #ifdef DEBUG_INTERRUPT_INFO
4229           printk(KERN_DEBUG "%s: wv_interrupt(): receive\n", dev->name);
4230 #endif
4231
4232           if((status0 & SR0_EVENT_MASK) == SR0_STOP_REG_HIT)
4233             {
4234 #ifdef DEBUG_INTERRUPT_ERROR
4235               printk(KERN_INFO "%s: wv_interrupt(): receive buffer overflow\n",
4236                      dev->name);
4237 #endif
4238               lp->stats.rx_over_errors++;
4239               lp->overrunning = 1;
4240             }
4241
4242           /* Get the packet */
4243           wv_packet_rcv(dev);
4244           lp->overrunning = 0;
4245
4246           /* Acknowledge the interrupt */
4247           outb(CR0_INT_ACK | OP0_NOP, LCCR(base));
4248           continue;
4249         }
4250
4251       /* ---------------- COMMAND COMPLETION ---------------- */
4252       /*
4253        * Interrupts issued when the i82593 has completed a command.
4254        * Most likely : transmission done
4255        */
4256
4257       /* If a transmission has been done */
4258       if((status0 & SR0_EVENT_MASK) == SR0_TRANSMIT_DONE ||
4259          (status0 & SR0_EVENT_MASK) == SR0_RETRANSMIT_DONE ||
4260          (status0 & SR0_EVENT_MASK) == SR0_TRANSMIT_NO_CRC_DONE)
4261         {
4262 #ifdef DEBUG_TX_ERROR
4263           if((status0 & SR0_EVENT_MASK) == SR0_TRANSMIT_NO_CRC_DONE)
4264             printk(KERN_INFO "%s: wv_interrupt(): packet transmitted without CRC.\n",
4265                    dev->name);
4266 #endif
4267
4268           /* Get transmission status */
4269           tx_status = inb(LCSR(base));
4270           tx_status |= (inb(LCSR(base)) << 8);
4271 #ifdef DEBUG_INTERRUPT_INFO
4272           printk(KERN_DEBUG "%s: wv_interrupt(): transmission done\n",
4273                  dev->name);
4274           {
4275             u_int       rcv_bytes;
4276             u_char      status3;
4277             rcv_bytes = inb(LCSR(base));
4278             rcv_bytes |= (inb(LCSR(base)) << 8);
4279             status3 = inb(LCSR(base));
4280             printk(KERN_DEBUG "tx_status 0x%02x rcv_bytes 0x%02x status3 0x%x\n",
4281                    tx_status, rcv_bytes, (u_int) status3);
4282           }
4283 #endif
4284           /* Check for possible errors */
4285           if((tx_status & TX_OK) != TX_OK)
4286             {
4287               lp->stats.tx_errors++;
4288
4289               if(tx_status & TX_FRTL)
4290                 {
4291 #ifdef DEBUG_TX_ERROR
4292                   printk(KERN_INFO "%s: wv_interrupt(): frame too long\n",
4293                          dev->name);
4294 #endif
4295                 }
4296               if(tx_status & TX_UND_RUN)
4297                 {
4298 #ifdef DEBUG_TX_FAIL
4299                   printk(KERN_DEBUG "%s: wv_interrupt(): DMA underrun\n",
4300                          dev->name);
4301 #endif
4302                   lp->stats.tx_aborted_errors++;
4303                 }
4304               if(tx_status & TX_LOST_CTS)
4305                 {
4306 #ifdef DEBUG_TX_FAIL
4307                   printk(KERN_DEBUG "%s: wv_interrupt(): no CTS\n", dev->name);
4308 #endif
4309                   lp->stats.tx_carrier_errors++;
4310                 }
4311               if(tx_status & TX_LOST_CRS)
4312                 {
4313 #ifdef DEBUG_TX_FAIL
4314                   printk(KERN_DEBUG "%s: wv_interrupt(): no carrier\n",
4315                          dev->name);
4316 #endif
4317                   lp->stats.tx_carrier_errors++;
4318                 }
4319               if(tx_status & TX_HRT_BEAT)
4320                 {
4321 #ifdef DEBUG_TX_FAIL
4322                   printk(KERN_DEBUG "%s: wv_interrupt(): heart beat\n", dev->name);
4323 #endif
4324                   lp->stats.tx_heartbeat_errors++;
4325                 }
4326               if(tx_status & TX_DEFER)
4327                 {
4328 #ifdef DEBUG_TX_FAIL
4329                   printk(KERN_DEBUG "%s: wv_interrupt(): channel jammed\n",
4330                          dev->name);
4331 #endif
4332                 }
4333               /* Ignore late collisions since they're more likely to happen
4334                * here (the WaveLAN design prevents the LAN controller from
4335                * receiving while it is transmitting). We take action only when
4336                * the maximum retransmit attempts is exceeded.
4337                */
4338               if(tx_status & TX_COLL)
4339                 {
4340                   if(tx_status & TX_MAX_COL)
4341                     {
4342 #ifdef DEBUG_TX_FAIL
4343                       printk(KERN_DEBUG "%s: wv_interrupt(): channel congestion\n",
4344                              dev->name);
4345 #endif
4346                       if(!(tx_status & TX_NCOL_MASK))
4347                         {
4348                           lp->stats.collisions += 0x10;
4349                         }
4350                     }
4351                 }
4352             }   /* if(!(tx_status & TX_OK)) */
4353
4354           lp->stats.collisions += (tx_status & TX_NCOL_MASK);
4355           lp->stats.tx_packets++;
4356
4357           netif_wake_queue(dev);
4358           outb(CR0_INT_ACK | OP0_NOP, LCCR(base));      /* Acknowledge the interrupt */
4359         } 
4360       else      /* if interrupt = transmit done or retransmit done */
4361         {
4362 #ifdef DEBUG_INTERRUPT_ERROR
4363           printk(KERN_INFO "wavelan_cs: unknown interrupt, status0 = %02x\n",
4364                  status0);
4365 #endif
4366           outb(CR0_INT_ACK | OP0_NOP, LCCR(base));      /* Acknowledge the interrupt */
4367         }
4368     }   /* while(1) */
4369
4370   spin_unlock(&lp->spinlock);
4371
4372 #ifdef DEBUG_INTERRUPT_TRACE
4373   printk(KERN_DEBUG "%s: <-wavelan_interrupt()\n", dev->name);
4374 #endif
4375
4376   /* We always return IRQ_HANDLED, because we will receive empty
4377    * interrupts under normal operations. Anyway, it doesn't matter
4378    * as we are dealing with an ISA interrupt that can't be shared.
4379    *
4380    * Explanation : under heavy receive, the following happens :
4381    * ->wavelan_interrupt()
4382    *    (status0 & SR0_INTERRUPT) != 0
4383    *       ->wv_packet_rcv()
4384    *    (status0 & SR0_INTERRUPT) != 0
4385    *       ->wv_packet_rcv()
4386    *    (status0 & SR0_INTERRUPT) == 0  // i.e. no more event
4387    * <-wavelan_interrupt()
4388    * ->wavelan_interrupt()
4389    *    (status0 & SR0_INTERRUPT) == 0  // i.e. empty interrupt
4390    * <-wavelan_interrupt()
4391    * Jean II */
4392   return IRQ_HANDLED;
4393 } /* wv_interrupt */
4394
4395 /*------------------------------------------------------------------*/
4396 /*
4397  * Watchdog: when we start a transmission, a timer is set for us in the
4398  * kernel.  If the transmission completes, this timer is disabled. If
4399  * the timer expires, we are called and we try to unlock the hardware.
4400  *
4401  * Note : This watchdog is move clever than the one in the ISA driver,
4402  * because it try to abort the current command before reseting
4403  * everything...
4404  * On the other hand, it's a bit simpler, because we don't have to
4405  * deal with the multiple Tx buffers...
4406  */
4407 static void
4408 wavelan_watchdog(struct net_device *    dev)
4409 {
4410   net_local *           lp = netdev_priv(dev);
4411   ioaddr_t              base = dev->base_addr;
4412   unsigned long         flags;
4413   int                   aborted = FALSE;
4414
4415 #ifdef DEBUG_INTERRUPT_TRACE
4416   printk(KERN_DEBUG "%s: ->wavelan_watchdog()\n", dev->name);
4417 #endif
4418
4419 #ifdef DEBUG_INTERRUPT_ERROR
4420   printk(KERN_INFO "%s: wavelan_watchdog: watchdog timer expired\n",
4421          dev->name);
4422 #endif
4423
4424   spin_lock_irqsave(&lp->spinlock, flags);
4425
4426   /* Ask to abort the current command */
4427   outb(OP0_ABORT, LCCR(base));
4428
4429   /* Wait for the end of the command (a bit hackish) */
4430   if(wv_82593_cmd(dev, "wavelan_watchdog(): abort",
4431                   OP0_NOP | CR0_STATUS_3, SR0_EXECUTION_ABORTED))
4432     aborted = TRUE;
4433
4434   /* Release spinlock here so that wv_hw_reset() can grab it */
4435   spin_unlock_irqrestore(&lp->spinlock, flags);
4436
4437   /* Check if we were successful in aborting it */
4438   if(!aborted)
4439     {
4440       /* It seem that it wasn't enough */
4441 #ifdef DEBUG_INTERRUPT_ERROR
4442       printk(KERN_INFO "%s: wavelan_watchdog: abort failed, trying reset\n",
4443              dev->name);
4444 #endif
4445       wv_hw_reset(dev);
4446     }
4447
4448 #ifdef DEBUG_PSA_SHOW
4449   {
4450     psa_t               psa;
4451     psa_read(dev, 0, (unsigned char *) &psa, sizeof(psa));
4452     wv_psa_show(&psa);
4453   }
4454 #endif
4455 #ifdef DEBUG_MMC_SHOW
4456   wv_mmc_show(dev);
4457 #endif
4458 #ifdef DEBUG_I82593_SHOW
4459   wv_ru_show(dev);
4460 #endif
4461
4462   /* We are no more waiting for something... */
4463   netif_wake_queue(dev);
4464
4465 #ifdef DEBUG_INTERRUPT_TRACE
4466   printk(KERN_DEBUG "%s: <-wavelan_watchdog()\n", dev->name);
4467 #endif
4468 }
4469
4470 /********************* CONFIGURATION CALLBACKS *********************/
4471 /*
4472  * Here are the functions called by the pcmcia package (cardmgr) and
4473  * linux networking (NET3) for initialization, configuration and
4474  * deinstallations of the Wavelan Pcmcia Hardware.
4475  */
4476
4477 /*------------------------------------------------------------------*/
4478 /*
4479  * Configure and start up the WaveLAN PCMCIA adaptor.
4480  * Called by NET3 when it "open" the device.
4481  */
4482 static int
4483 wavelan_open(struct net_device *        dev)
4484 {
4485   net_local *   lp = netdev_priv(dev);
4486   dev_link_t *  link = lp->link;
4487   ioaddr_t      base = dev->base_addr;
4488
4489 #ifdef DEBUG_CALLBACK_TRACE
4490   printk(KERN_DEBUG "%s: ->wavelan_open(dev=0x%x)\n", dev->name,
4491          (unsigned int) dev);
4492 #endif
4493
4494   /* Check if the modem is powered up (wavelan_close() power it down */
4495   if(hasr_read(base) & HASR_NO_CLK)
4496     {
4497       /* Power up (power up time is 250us) */
4498       hacr_write(base, HACR_DEFAULT);
4499
4500       /* Check if the module has been powered up... */
4501       if(hasr_read(base) & HASR_NO_CLK)
4502         {
4503 #ifdef DEBUG_CONFIG_ERRORS
4504           printk(KERN_WARNING "%s: wavelan_open(): modem not connected\n",
4505                  dev->name);
4506 #endif
4507           return FALSE;
4508         }
4509     }
4510
4511   /* Start reception and declare the driver ready */
4512   if(!lp->configured)
4513     return FALSE;
4514   if(!wv_ru_start(dev))
4515     wv_hw_reset(dev);           /* If problem : reset */
4516   netif_start_queue(dev);
4517
4518   /* Mark the device as used */
4519   link->open++;
4520
4521 #ifdef WAVELAN_ROAMING
4522   if(do_roaming)
4523     wv_roam_init(dev);
4524 #endif  /* WAVELAN_ROAMING */
4525
4526 #ifdef DEBUG_CALLBACK_TRACE
4527   printk(KERN_DEBUG "%s: <-wavelan_open()\n", dev->name);
4528 #endif
4529   return 0;
4530 }
4531
4532 /*------------------------------------------------------------------*/
4533 /*
4534  * Shutdown the WaveLAN PCMCIA adaptor.
4535  * Called by NET3 when it "close" the device.
4536  */
4537 static int
4538 wavelan_close(struct net_device *       dev)
4539 {
4540   dev_link_t *  link = ((net_local *)netdev_priv(dev))->link;
4541   ioaddr_t      base = dev->base_addr;
4542
4543 #ifdef DEBUG_CALLBACK_TRACE
4544   printk(KERN_DEBUG "%s: ->wavelan_close(dev=0x%x)\n", dev->name,
4545          (unsigned int) dev);
4546 #endif
4547
4548   /* If the device isn't open, then nothing to do */
4549   if(!link->open)
4550     {
4551 #ifdef DEBUG_CONFIG_INFO
4552       printk(KERN_DEBUG "%s: wavelan_close(): device not open\n", dev->name);
4553 #endif
4554       return 0;
4555     }
4556
4557 #ifdef WAVELAN_ROAMING
4558   /* Cleanup of roaming stuff... */
4559   if(do_roaming)
4560     wv_roam_cleanup(dev);
4561 #endif  /* WAVELAN_ROAMING */
4562
4563   link->open--;
4564
4565   /* If the card is still present */
4566   if(netif_running(dev))
4567     {
4568       netif_stop_queue(dev);
4569
4570       /* Stop receiving new messages and wait end of transmission */
4571       wv_ru_stop(dev);
4572
4573       /* Power down the module */
4574       hacr_write(base, HACR_DEFAULT & (~HACR_PWR_STAT));
4575     }
4576
4577 #ifdef DEBUG_CALLBACK_TRACE
4578   printk(KERN_DEBUG "%s: <-wavelan_close()\n", dev->name);
4579 #endif
4580   return 0;
4581 }
4582
4583 /*------------------------------------------------------------------*/
4584 /*
4585  * wavelan_attach() creates an "instance" of the driver, allocating
4586  * local data structures for one device (one interface).  The device
4587  * is registered with Card Services.
4588  *
4589  * The dev_link structure is initialized, but we don't actually
4590  * configure the card at this point -- we wait until we receive a
4591  * card insertion event.
4592  */
4593 static dev_link_t *
4594 wavelan_attach(void)
4595 {
4596   client_reg_t  client_reg;     /* Register with cardmgr */
4597   dev_link_t *  link;           /* Info for cardmgr */
4598   struct net_device *   dev;            /* Interface generic data */
4599   net_local *   lp;             /* Interface specific data */
4600   int           i, ret;
4601
4602 #ifdef DEBUG_CALLBACK_TRACE
4603   printk(KERN_DEBUG "-> wavelan_attach()\n");
4604 #endif
4605
4606   /* Initialize the dev_link_t structure */
4607   link = kmalloc(sizeof(struct dev_link_t), GFP_KERNEL);
4608   if (!link) return NULL;
4609   memset(link, 0, sizeof(struct dev_link_t));
4610
4611   /* The io structure describes IO port mapping */
4612   link->io.NumPorts1 = 8;
4613   link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
4614   link->io.IOAddrLines = 3;
4615
4616   /* Interrupt setup */
4617   link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
4618   link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
4619   if (irq_list[0] == -1)
4620     link->irq.IRQInfo2 = irq_mask;
4621   else
4622     for (i = 0; i < 4; i++)
4623       link->irq.IRQInfo2 |= 1 << irq_list[i];
4624   link->irq.Handler = wavelan_interrupt;
4625
4626   /* General socket configuration */
4627   link->conf.Attributes = CONF_ENABLE_IRQ;
4628   link->conf.Vcc = 50;
4629   link->conf.IntType = INT_MEMORY_AND_IO;
4630
4631   /* Chain drivers */
4632   link->next = dev_list;
4633   dev_list = link;
4634
4635   /* Allocate the generic data structure */
4636   dev = alloc_etherdev(sizeof(net_local));
4637   if (!dev) {
4638       kfree(link);
4639       return NULL;
4640   }
4641   link->priv = link->irq.Instance = dev;
4642
4643   lp = netdev_priv(dev);
4644
4645   /* Init specific data */
4646   lp->configured = 0;
4647   lp->reconfig_82593 = FALSE;
4648   lp->nresets = 0;
4649   /* Multicast stuff */
4650   lp->promiscuous = 0;
4651   lp->allmulticast = 0;
4652   lp->mc_count = 0;
4653
4654   /* Init spinlock */
4655   spin_lock_init(&lp->spinlock);
4656
4657   /* back links */
4658   lp->link = link;
4659   lp->dev = dev;
4660
4661   /* wavelan NET3 callbacks */
4662   SET_MODULE_OWNER(dev);
4663   dev->open = &wavelan_open;
4664   dev->stop = &wavelan_close;
4665   dev->hard_start_xmit = &wavelan_packet_xmit;
4666   dev->get_stats = &wavelan_get_stats;
4667   dev->set_multicast_list = &wavelan_set_multicast_list;
4668 #ifdef SET_MAC_ADDRESS
4669   dev->set_mac_address = &wavelan_set_mac_address;
4670 #endif  /* SET_MAC_ADDRESS */
4671
4672   /* Set the watchdog timer */
4673   dev->tx_timeout       = &wavelan_watchdog;
4674   dev->watchdog_timeo   = WATCHDOG_JIFFIES;
4675   SET_ETHTOOL_OPS(dev, &ops);
4676
4677 #ifdef WIRELESS_EXT     /* If wireless extension exist in the kernel */
4678   dev->wireless_handlers = &wavelan_handler_def;
4679   lp->wireless_data.spy_data = &lp->spy_data;
4680   dev->wireless_data = &lp->wireless_data;
4681 #endif
4682
4683   /* Other specific data */
4684   dev->mtu = WAVELAN_MTU;
4685
4686   /* Register with Card Services */
4687   client_reg.dev_info = &dev_info;
4688   client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
4689   client_reg.EventMask = 
4690     CS_EVENT_REGISTRATION_COMPLETE |
4691     CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
4692     CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
4693     CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
4694   client_reg.event_handler = &wavelan_event;
4695   client_reg.Version = 0x0210;
4696   client_reg.event_callback_args.client_data = link;
4697
4698 #ifdef DEBUG_CONFIG_INFO
4699   printk(KERN_DEBUG "wavelan_attach(): almost done, calling pcmcia_register_client\n");
4700 #endif
4701
4702   ret = pcmcia_register_client(&link->handle, &client_reg);
4703   if(ret != 0)
4704     {
4705       cs_error(link->handle, RegisterClient, ret);
4706       wavelan_detach(link);
4707       return NULL;
4708     }
4709
4710 #ifdef DEBUG_CALLBACK_TRACE
4711   printk(KERN_DEBUG "<- wavelan_attach()\n");
4712 #endif
4713
4714   return link;
4715 }
4716
4717 /*------------------------------------------------------------------*/
4718 /*
4719  * This deletes a driver "instance".  The device is de-registered with
4720  * Card Services.  If it has been released, all local data structures
4721  * are freed.  Otherwise, the structures will be freed when the device
4722  * is released.
4723  */
4724 static void
4725 wavelan_detach(dev_link_t *     link)
4726 {
4727 #ifdef DEBUG_CALLBACK_TRACE
4728   printk(KERN_DEBUG "-> wavelan_detach(0x%p)\n", link);
4729 #endif
4730
4731   /*
4732    * If the device is currently configured and active, we won't
4733    * actually delete it yet.  Instead, it is marked so that when the
4734    * release() function is called, that will trigger a proper
4735    * detach().
4736    */
4737   if(link->state & DEV_CONFIG)
4738     {
4739       /* Some others haven't done their job : give them another chance */
4740       wv_pcmcia_release(link);
4741     }
4742
4743   /* Break the link with Card Services */
4744   if(link->handle)
4745     pcmcia_deregister_client(link->handle);
4746     
4747   /* Remove the interface data from the linked list */
4748   if(dev_list == link)
4749     dev_list = link->next;
4750   else
4751     {
4752       dev_link_t *      prev = dev_list;
4753
4754       while((prev != (dev_link_t *) NULL) && (prev->next != link))
4755         prev = prev->next;
4756
4757       if(prev == (dev_link_t *) NULL)
4758         {
4759 #ifdef DEBUG_CONFIG_ERRORS
4760           printk(KERN_WARNING "wavelan_detach : Attempting to remove a nonexistent device.\n");
4761 #endif
4762           return;
4763         }
4764
4765       prev->next = link->next;
4766     }
4767
4768   /* Free pieces */
4769   if(link->priv)
4770     {
4771       struct net_device *       dev = (struct net_device *) link->priv;
4772
4773       /* Remove ourselves from the kernel list of ethernet devices */
4774       /* Warning : can't be called from interrupt, timer or wavelan_close() */
4775       if (link->dev)
4776         unregister_netdev(dev);
4777       link->dev = NULL;
4778       ((net_local *)netdev_priv(dev))->link = NULL;
4779       ((net_local *)netdev_priv(dev))->dev = NULL;
4780       free_netdev(dev);
4781     }
4782   kfree(link);
4783
4784 #ifdef DEBUG_CALLBACK_TRACE
4785   printk(KERN_DEBUG "<- wavelan_detach()\n");
4786 #endif
4787 }
4788
4789 /*------------------------------------------------------------------*/
4790 /*
4791  * The card status event handler. Mostly, this schedules other stuff
4792  * to run after an event is received. A CARD_REMOVAL event also sets
4793  * some flags to discourage the net drivers from trying to talk to the
4794  * card any more.
4795  */
4796 static int
4797 wavelan_event(event_t           event,          /* The event received */
4798               int               priority,
4799               event_callback_args_t *   args)
4800 {
4801   dev_link_t *  link = (dev_link_t *) args->client_data;
4802   struct net_device *   dev = (struct net_device *) link->priv;
4803
4804 #ifdef DEBUG_CALLBACK_TRACE
4805   printk(KERN_DEBUG "->wavelan_event(): %s\n",
4806          ((event == CS_EVENT_REGISTRATION_COMPLETE)?"registration complete" :
4807           ((event == CS_EVENT_CARD_REMOVAL) ? "card removal" :
4808            ((event == CS_EVENT_CARD_INSERTION) ? "card insertion" :
4809             ((event == CS_EVENT_PM_SUSPEND) ? "pm suspend" :
4810              ((event == CS_EVENT_RESET_PHYSICAL) ? "physical reset" :
4811               ((event == CS_EVENT_PM_RESUME) ? "pm resume" :
4812                ((event == CS_EVENT_CARD_RESET) ? "card reset" :
4813                 "unknown"))))))));
4814 #endif
4815
4816     switch(event)
4817       {
4818       case CS_EVENT_REGISTRATION_COMPLETE:
4819 #ifdef DEBUG_CONFIG_INFO
4820         printk(KERN_DEBUG "wavelan_cs: registration complete\n");
4821 #endif
4822         break;
4823
4824       case CS_EVENT_CARD_REMOVAL:
4825         /* Oups ! The card is no more there */
4826         link->state &= ~DEV_PRESENT;
4827         if(link->state & DEV_CONFIG)
4828           {
4829             /* Accept no more transmissions */
4830             netif_device_detach(dev);
4831
4832             /* Release the card */
4833             wv_pcmcia_release(link);
4834           }
4835         break;
4836
4837       case CS_EVENT_CARD_INSERTION:
4838         /* Reset and configure the card */
4839         link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
4840         if(wv_pcmcia_config(link) &&
4841            wv_hw_config(dev))
4842           wv_init_info(dev);
4843         else
4844           dev->irq = 0;
4845         break;
4846
4847       case CS_EVENT_PM_SUSPEND:
4848         /* NB: wavelan_close will be called, but too late, so we are
4849          * obliged to close nicely the wavelan here. David, could you
4850          * close the device before suspending them ? And, by the way,
4851          * could you, on resume, add a "route add -net ..." after the
4852          * ifconfig up ? Thanks... */
4853
4854         /* Stop receiving new messages and wait end of transmission */
4855         wv_ru_stop(dev);
4856
4857         /* Power down the module */
4858         hacr_write(dev->base_addr, HACR_DEFAULT & (~HACR_PWR_STAT));
4859
4860         /* The card is now suspended */
4861         link->state |= DEV_SUSPEND;
4862         /* Fall through... */
4863       case CS_EVENT_RESET_PHYSICAL:
4864         if(link->state & DEV_CONFIG)
4865           {
4866             if(link->open)
4867               netif_device_detach(dev);
4868             pcmcia_release_configuration(link->handle);
4869           }
4870         break;
4871
4872       case CS_EVENT_PM_RESUME:
4873         link->state &= ~DEV_SUSPEND;
4874         /* Fall through... */
4875       case CS_EVENT_CARD_RESET:
4876         if(link->state & DEV_CONFIG)
4877           {
4878             pcmcia_request_configuration(link->handle, &link->conf);
4879             if(link->open)      /* If RESET -> True, If RESUME -> False ? */
4880               {
4881                 wv_hw_reset(dev);
4882                 netif_device_attach(dev);
4883               }
4884           }
4885         break;
4886     }
4887
4888 #ifdef DEBUG_CALLBACK_TRACE
4889   printk(KERN_DEBUG "<-wavelan_event()\n");
4890 #endif
4891   return 0;
4892 }
4893
4894 static struct pcmcia_driver wavelan_driver = {
4895         .owner          = THIS_MODULE,
4896         .drv            = {
4897                 .name   = "wavelan_cs",
4898         },
4899         .attach         = wavelan_attach,
4900         .detach         = wavelan_detach,
4901 };
4902
4903 static int __init
4904 init_wavelan_cs(void)
4905 {
4906         return pcmcia_register_driver(&wavelan_driver);
4907 }
4908
4909 static void __exit
4910 exit_wavelan_cs(void)
4911 {
4912         pcmcia_unregister_driver(&wavelan_driver);
4913 }
4914
4915 module_init(init_wavelan_cs);
4916 module_exit(exit_wavelan_cs);