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