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