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