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