1 /*
2  *	WaveLAN ISA driver
3  *
4  *		Jean II - HPLB '96
5  *
6  * Reorganisation and extension of the driver.
7  * Original copyright follows (also see the end of this file).
8  * See wavelan.p.h for details.
9  *
10  *
11  *
12  * AT&T GIS (nee NCR) WaveLAN card:
13  *	An Ethernet-like radio transceiver
14  *	controlled by an Intel 82586 coprocessor.
15  */
16 
17 #include "wavelan.p.h"		/* Private header */
18 
19 /************************* MISC SUBROUTINES **************************/
20 /*
21  * Subroutines which won't fit in one of the following category
22  * (WaveLAN modem or i82586)
23  */
24 
25 /*------------------------------------------------------------------*/
26 /*
27  * Wrapper for disabling interrupts and locking the driver.
28  * (note : inline, so optimised away)
29  */
wv_splhi(net_local * lp,unsigned long * pflags)30 static inline void wv_splhi(net_local *		lp,
31 			    unsigned long *	pflags)
32 {
33 	spin_lock_irqsave(&lp->spinlock, *pflags);
34 	/* Note : above does the cli(); itself */
35 }
36 
37 /*------------------------------------------------------------------*/
38 /*
39  * Wrapper for re-enabling interrupts and un-locking the driver.
40  */
wv_splx(net_local * lp,unsigned long * pflags)41 static inline void wv_splx(net_local *		lp,
42 			   unsigned long *	pflags)
43 {
44 	spin_unlock_irqrestore(&lp->spinlock, *pflags);
45 }
46 
47 /*------------------------------------------------------------------*/
48 /*
49  * Translate irq number to PSA irq parameter
50  */
wv_irq_to_psa(int irq)51 static u8 wv_irq_to_psa(int irq)
52 {
53 	if (irq < 0 || irq >= NELS(irqvals))
54 		return 0;
55 
56 	return irqvals[irq];
57 }
58 
59 /*------------------------------------------------------------------*/
60 /*
61  * Translate PSA irq parameter to irq number
62  */
wv_psa_to_irq(u8 irqval)63 static int __init wv_psa_to_irq(u8 irqval)
64 {
65 	int irq;
66 
67 	for (irq = 0; irq < NELS(irqvals); irq++)
68 		if (irqvals[irq] == irqval)
69 			return irq;
70 
71 	return -1;
72 }
73 
74 #ifdef STRUCT_CHECK
75 /*------------------------------------------------------------------*/
76 /*
77  * Sanity routine to verify the sizes of the various WaveLAN interface
78  * structures.
79  */
wv_struct_check(void)80 static char *wv_struct_check(void)
81 {
82 #define	SC(t,s,n)	if (sizeof(t) != s) return(n);
83 
84 	SC(psa_t, PSA_SIZE, "psa_t");
85 	SC(mmw_t, MMW_SIZE, "mmw_t");
86 	SC(mmr_t, MMR_SIZE, "mmr_t");
87 	SC(ha_t, HA_SIZE, "ha_t");
88 
89 #undef	SC
90 
91 	return ((char *) NULL);
92 }				/* wv_struct_check */
93 #endif				/* STRUCT_CHECK */
94 
95 /********************* HOST ADAPTER SUBROUTINES *********************/
96 /*
97  * Useful subroutines to manage the WaveLAN ISA interface
98  *
99  * One major difference with the PCMCIA hardware (except the port mapping)
100  * is that we have to keep the state of the Host Control Register
101  * because of the interrupt enable & bus size flags.
102  */
103 
104 /*------------------------------------------------------------------*/
105 /*
106  * Read from card's Host Adaptor Status Register.
107  */
hasr_read(unsigned long ioaddr)108 static inline u16 hasr_read(unsigned long ioaddr)
109 {
110 	return (inw(HASR(ioaddr)));
111 }				/* hasr_read */
112 
113 /*------------------------------------------------------------------*/
114 /*
115  * Write to card's Host Adapter Command Register.
116  */
hacr_write(unsigned long ioaddr,u16 hacr)117 static inline void hacr_write(unsigned long ioaddr, u16 hacr)
118 {
119 	outw(hacr, HACR(ioaddr));
120 }				/* hacr_write */
121 
122 /*------------------------------------------------------------------*/
123 /*
124  * Write to card's Host Adapter Command Register. Include a delay for
125  * those times when it is needed.
126  */
hacr_write_slow(unsigned long ioaddr,u16 hacr)127 static inline void hacr_write_slow(unsigned long ioaddr, u16 hacr)
128 {
129 	hacr_write(ioaddr, hacr);
130 	/* delay might only be needed sometimes */
131 	mdelay(1);
132 }				/* hacr_write_slow */
133 
134 /*------------------------------------------------------------------*/
135 /*
136  * Set the channel attention bit.
137  */
set_chan_attn(unsigned long ioaddr,u16 hacr)138 static inline void set_chan_attn(unsigned long ioaddr, u16 hacr)
139 {
140 	hacr_write(ioaddr, hacr | HACR_CA);
141 }				/* set_chan_attn */
142 
143 /*------------------------------------------------------------------*/
144 /*
145  * Reset, and then set host adaptor into default mode.
146  */
wv_hacr_reset(unsigned long ioaddr)147 static inline void wv_hacr_reset(unsigned long ioaddr)
148 {
149 	hacr_write_slow(ioaddr, HACR_RESET);
150 	hacr_write(ioaddr, HACR_DEFAULT);
151 }				/* wv_hacr_reset */
152 
153 /*------------------------------------------------------------------*/
154 /*
155  * Set the I/O transfer over the ISA bus to 8-bit mode
156  */
wv_16_off(unsigned long ioaddr,u16 hacr)157 static inline void wv_16_off(unsigned long ioaddr, u16 hacr)
158 {
159 	hacr &= ~HACR_16BITS;
160 	hacr_write(ioaddr, hacr);
161 }				/* wv_16_off */
162 
163 /*------------------------------------------------------------------*/
164 /*
165  * Set the I/O transfer over the ISA bus to 8-bit mode
166  */
wv_16_on(unsigned long ioaddr,u16 hacr)167 static inline void wv_16_on(unsigned long ioaddr, u16 hacr)
168 {
169 	hacr |= HACR_16BITS;
170 	hacr_write(ioaddr, hacr);
171 }				/* wv_16_on */
172 
173 /*------------------------------------------------------------------*/
174 /*
175  * Disable interrupts on the WaveLAN hardware.
176  * (called by wv_82586_stop())
177  */
wv_ints_off(device * dev)178 static inline void wv_ints_off(device * dev)
179 {
180 	net_local *lp = (net_local *) dev->priv;
181 	unsigned long ioaddr = dev->base_addr;
182 
183 	lp->hacr &= ~HACR_INTRON;
184 	hacr_write(ioaddr, lp->hacr);
185 }				/* wv_ints_off */
186 
187 /*------------------------------------------------------------------*/
188 /*
189  * Enable interrupts on the WaveLAN hardware.
190  * (called by wv_hw_reset())
191  */
wv_ints_on(device * dev)192 static inline void wv_ints_on(device * dev)
193 {
194 	net_local *lp = (net_local *) dev->priv;
195 	unsigned long ioaddr = dev->base_addr;
196 
197 	lp->hacr |= HACR_INTRON;
198 	hacr_write(ioaddr, lp->hacr);
199 }				/* wv_ints_on */
200 
201 /******************* MODEM MANAGEMENT SUBROUTINES *******************/
202 /*
203  * Useful subroutines to manage the modem of the WaveLAN
204  */
205 
206 /*------------------------------------------------------------------*/
207 /*
208  * Read the Parameter Storage Area from the WaveLAN card's memory
209  */
210 /*
211  * Read bytes from the PSA.
212  */
psa_read(unsigned long ioaddr,u16 hacr,int o,u8 * b,int n)213 static void psa_read(unsigned long ioaddr, u16 hacr, int o,	/* offset in PSA */
214 		     u8 * b,	/* buffer to fill */
215 		     int n)
216 {				/* size to read */
217 	wv_16_off(ioaddr, hacr);
218 
219 	while (n-- > 0) {
220 		outw(o, PIOR2(ioaddr));
221 		o++;
222 		*b++ = inb(PIOP2(ioaddr));
223 	}
224 
225 	wv_16_on(ioaddr, hacr);
226 }				/* psa_read */
227 
228 /*------------------------------------------------------------------*/
229 /*
230  * Write the Parameter Storage Area to the WaveLAN card's memory.
231  */
psa_write(unsigned long ioaddr,u16 hacr,int o,u8 * b,int n)232 static void psa_write(unsigned long ioaddr, u16 hacr, int o,	/* Offset in PSA */
233 		      u8 * b,	/* Buffer in memory */
234 		      int n)
235 {				/* Length of buffer */
236 	int count = 0;
237 
238 	wv_16_off(ioaddr, hacr);
239 
240 	while (n-- > 0) {
241 		outw(o, PIOR2(ioaddr));
242 		o++;
243 
244 		outb(*b, PIOP2(ioaddr));
245 		b++;
246 
247 		/* Wait for the memory to finish its write cycle */
248 		count = 0;
249 		while ((count++ < 100) &&
250 		       (hasr_read(ioaddr) & HASR_PSA_BUSY)) mdelay(1);
251 	}
252 
253 	wv_16_on(ioaddr, hacr);
254 }				/* psa_write */
255 
256 #ifdef SET_PSA_CRC
257 /*------------------------------------------------------------------*/
258 /*
259  * Calculate the PSA CRC
260  * Thanks to Valster, Nico <NVALSTER@wcnd.nl.lucent.com> for the code
261  * NOTE: By specifying a length including the CRC position the
262  * returned value should be zero. (i.e. a correct checksum in the PSA)
263  *
264  * The Windows drivers don't use the CRC, but the AP and the PtP tool
265  * depend on it.
266  */
psa_crc(u8 * psa,int size)267 static inline u16 psa_crc(u8 * psa,	/* The PSA */
268 			      int size)
269 {				/* Number of short for CRC */
270 	int byte_cnt;		/* Loop on the PSA */
271 	u16 crc_bytes = 0;	/* Data in the PSA */
272 	int bit_cnt;		/* Loop on the bits of the short */
273 
274 	for (byte_cnt = 0; byte_cnt < size; byte_cnt++) {
275 		crc_bytes ^= psa[byte_cnt];	/* Its an xor */
276 
277 		for (bit_cnt = 1; bit_cnt < 9; bit_cnt++) {
278 			if (crc_bytes & 0x0001)
279 				crc_bytes = (crc_bytes >> 1) ^ 0xA001;
280 			else
281 				crc_bytes >>= 1;
282 		}
283 	}
284 
285 	return crc_bytes;
286 }				/* psa_crc */
287 #endif				/* SET_PSA_CRC */
288 
289 /*------------------------------------------------------------------*/
290 /*
291  * update the checksum field in the Wavelan's PSA
292  */
update_psa_checksum(device * dev,unsigned long ioaddr,u16 hacr)293 static void update_psa_checksum(device * dev, unsigned long ioaddr, u16 hacr)
294 {
295 #ifdef SET_PSA_CRC
296 	psa_t psa;
297 	u16 crc;
298 
299 	/* read the parameter storage area */
300 	psa_read(ioaddr, hacr, 0, (unsigned char *) &psa, sizeof(psa));
301 
302 	/* update the checksum */
303 	crc = psa_crc((unsigned char *) &psa,
304 		      sizeof(psa) - sizeof(psa.psa_crc[0]) -
305 		      sizeof(psa.psa_crc[1])
306 		      - sizeof(psa.psa_crc_status));
307 
308 	psa.psa_crc[0] = crc & 0xFF;
309 	psa.psa_crc[1] = (crc & 0xFF00) >> 8;
310 
311 	/* Write it ! */
312 	psa_write(ioaddr, hacr, (char *) &psa.psa_crc - (char *) &psa,
313 		  (unsigned char *) &psa.psa_crc, 2);
314 
315 #ifdef DEBUG_IOCTL_INFO
316 	printk(KERN_DEBUG "%s: update_psa_checksum(): crc = 0x%02x%02x\n",
317 	       dev->name, psa.psa_crc[0], psa.psa_crc[1]);
318 
319 	/* Check again (luxury !) */
320 	crc = psa_crc((unsigned char *) &psa,
321 		      sizeof(psa) - sizeof(psa.psa_crc_status));
322 
323 	if (crc != 0)
324 		printk(KERN_WARNING
325 		       "%s: update_psa_checksum(): CRC does not agree with PSA data (even after recalculating)\n",
326 		       dev->name);
327 #endif				/* DEBUG_IOCTL_INFO */
328 #endif				/* SET_PSA_CRC */
329 }				/* update_psa_checksum */
330 
331 /*------------------------------------------------------------------*/
332 /*
333  * Write 1 byte to the MMC.
334  */
mmc_out(unsigned long ioaddr,u16 o,u8 d)335 static inline void mmc_out(unsigned long ioaddr, u16 o, u8 d)
336 {
337 	/* Wait for MMC to go idle */
338 	while (inw(HASR(ioaddr)) & HASR_MMC_BUSY);
339 
340 	outw((u16) (((u16) d << 8) | (o << 1) | 1), MMCR(ioaddr));
341 }
342 
343 /*------------------------------------------------------------------*/
344 /*
345  * Routine to write bytes to the Modem Management Controller.
346  * We start at the end because it is the way it should be!
347  */
mmc_write(unsigned long ioaddr,u8 o,u8 * b,int n)348 static inline void mmc_write(unsigned long ioaddr, u8 o, u8 * b, int n)
349 {
350 	o += n;
351 	b += n;
352 
353 	while (n-- > 0)
354 		mmc_out(ioaddr, --o, *(--b));
355 }				/* mmc_write */
356 
357 /*------------------------------------------------------------------*/
358 /*
359  * Read a byte from the MMC.
360  * Optimised version for 1 byte, avoid using memory.
361  */
mmc_in(unsigned long ioaddr,u16 o)362 static inline u8 mmc_in(unsigned long ioaddr, u16 o)
363 {
364 	while (inw(HASR(ioaddr)) & HASR_MMC_BUSY);
365 	outw(o << 1, MMCR(ioaddr));
366 
367 	while (inw(HASR(ioaddr)) & HASR_MMC_BUSY);
368 	return (u8) (inw(MMCR(ioaddr)) >> 8);
369 }
370 
371 /*------------------------------------------------------------------*/
372 /*
373  * Routine to read bytes from the Modem Management Controller.
374  * The implementation is complicated by a lack of address lines,
375  * which prevents decoding of the low-order bit.
376  * (code has just been moved in the above function)
377  * We start at the end because it is the way it should be!
378  */
mmc_read(unsigned long ioaddr,u8 o,u8 * b,int n)379 static inline void mmc_read(unsigned long ioaddr, u8 o, u8 * b, int n)
380 {
381 	o += n;
382 	b += n;
383 
384 	while (n-- > 0)
385 		*(--b) = mmc_in(ioaddr, --o);
386 }				/* mmc_read */
387 
388 /*------------------------------------------------------------------*/
389 /*
390  * Get the type of encryption available.
391  */
mmc_encr(unsigned long ioaddr)392 static inline int mmc_encr(unsigned long ioaddr)
393 {				/* I/O port of the card */
394 	int temp;
395 
396 	temp = mmc_in(ioaddr, mmroff(0, mmr_des_avail));
397 	if ((temp != MMR_DES_AVAIL_DES) && (temp != MMR_DES_AVAIL_AES))
398 		return 0;
399 	else
400 		return temp;
401 }
402 
403 /*------------------------------------------------------------------*/
404 /*
405  * Wait for the frequency EEPROM to complete a command.
406  * I hope this one will be optimally inlined.
407  */
fee_wait(unsigned long ioaddr,int delay,int number)408 static inline void fee_wait(unsigned long ioaddr,	/* I/O port of the card */
409 			    int delay,	/* Base delay to wait for */
410 			    int number)
411 {				/* Number of time to wait */
412 	int count = 0;		/* Wait only a limited time */
413 
414 	while ((count++ < number) &&
415 	       (mmc_in(ioaddr, mmroff(0, mmr_fee_status)) &
416 		MMR_FEE_STATUS_BUSY)) udelay(delay);
417 }
418 
419 /*------------------------------------------------------------------*/
420 /*
421  * Read bytes from the Frequency EEPROM (frequency select cards).
422  */
fee_read(unsigned long ioaddr,u16 o,u16 * b,int n)423 static void fee_read(unsigned long ioaddr,	/* I/O port of the card */
424 		     u16 o,	/* destination offset */
425 		     u16 * b,	/* data buffer */
426 		     int n)
427 {				/* number of registers */
428 	b += n;			/* Position at the end of the area */
429 
430 	/* Write the address */
431 	mmc_out(ioaddr, mmwoff(0, mmw_fee_addr), o + n - 1);
432 
433 	/* Loop on all buffer */
434 	while (n-- > 0) {
435 		/* Write the read command */
436 		mmc_out(ioaddr, mmwoff(0, mmw_fee_ctrl),
437 			MMW_FEE_CTRL_READ);
438 
439 		/* Wait until EEPROM is ready (should be quick). */
440 		fee_wait(ioaddr, 10, 100);
441 
442 		/* Read the value. */
443 		*--b = ((mmc_in(ioaddr, mmroff(0, mmr_fee_data_h)) << 8) |
444 			mmc_in(ioaddr, mmroff(0, mmr_fee_data_l)));
445 	}
446 }
447 
448 #ifdef WIRELESS_EXT		/* if the wireless extension exists in the kernel */
449 
450 /*------------------------------------------------------------------*/
451 /*
452  * Write bytes from the Frequency EEPROM (frequency select cards).
453  * This is a bit complicated, because the frequency EEPROM has to
454  * be unprotected and the write enabled.
455  * Jean II
456  */
fee_write(unsigned long ioaddr,u16 o,u16 * b,int n)457 static void fee_write(unsigned long ioaddr,	/* I/O port of the card */
458 		      u16 o,	/* destination offset */
459 		      u16 * b,	/* data buffer */
460 		      int n)
461 {				/* number of registers */
462 	b += n;			/* Position at the end of the area. */
463 
464 #ifdef EEPROM_IS_PROTECTED	/* disabled */
465 #ifdef DOESNT_SEEM_TO_WORK	/* disabled */
466 	/* Ask to read the protected register */
467 	mmc_out(ioaddr, mmwoff(0, mmw_fee_ctrl), MMW_FEE_CTRL_PRREAD);
468 
469 	fee_wait(ioaddr, 10, 100);
470 
471 	/* Read the protected register. */
472 	printk("Protected 2:  %02X-%02X\n",
473 	       mmc_in(ioaddr, mmroff(0, mmr_fee_data_h)),
474 	       mmc_in(ioaddr, mmroff(0, mmr_fee_data_l)));
475 #endif				/* DOESNT_SEEM_TO_WORK */
476 
477 	/* Enable protected register. */
478 	mmc_out(ioaddr, mmwoff(0, mmw_fee_addr), MMW_FEE_ADDR_EN);
479 	mmc_out(ioaddr, mmwoff(0, mmw_fee_ctrl), MMW_FEE_CTRL_PREN);
480 
481 	fee_wait(ioaddr, 10, 100);
482 
483 	/* Unprotect area. */
484 	mmc_out(ioaddr, mmwoff(0, mmw_fee_addr), o + n);
485 	mmc_out(ioaddr, mmwoff(0, mmw_fee_ctrl), MMW_FEE_CTRL_PRWRITE);
486 #ifdef DOESNT_SEEM_TO_WORK	/* disabled */
487 	/* or use: */
488 	mmc_out(ioaddr, mmwoff(0, mmw_fee_ctrl), MMW_FEE_CTRL_PRCLEAR);
489 #endif				/* DOESNT_SEEM_TO_WORK */
490 
491 	fee_wait(ioaddr, 10, 100);
492 #endif				/* EEPROM_IS_PROTECTED */
493 
494 	/* Write enable. */
495 	mmc_out(ioaddr, mmwoff(0, mmw_fee_addr), MMW_FEE_ADDR_EN);
496 	mmc_out(ioaddr, mmwoff(0, mmw_fee_ctrl), MMW_FEE_CTRL_WREN);
497 
498 	fee_wait(ioaddr, 10, 100);
499 
500 	/* Write the EEPROM address. */
501 	mmc_out(ioaddr, mmwoff(0, mmw_fee_addr), o + n - 1);
502 
503 	/* Loop on all buffer */
504 	while (n-- > 0) {
505 		/* Write the value. */
506 		mmc_out(ioaddr, mmwoff(0, mmw_fee_data_h), (*--b) >> 8);
507 		mmc_out(ioaddr, mmwoff(0, mmw_fee_data_l), *b & 0xFF);
508 
509 		/* Write the write command. */
510 		mmc_out(ioaddr, mmwoff(0, mmw_fee_ctrl),
511 			MMW_FEE_CTRL_WRITE);
512 
513 		/* WaveLAN documentation says to wait at least 10 ms for EEBUSY = 0 */
514 		mdelay(10);
515 		fee_wait(ioaddr, 10, 100);
516 	}
517 
518 	/* Write disable. */
519 	mmc_out(ioaddr, mmwoff(0, mmw_fee_addr), MMW_FEE_ADDR_DS);
520 	mmc_out(ioaddr, mmwoff(0, mmw_fee_ctrl), MMW_FEE_CTRL_WDS);
521 
522 	fee_wait(ioaddr, 10, 100);
523 
524 #ifdef EEPROM_IS_PROTECTED	/* disabled */
525 	/* Reprotect EEPROM. */
526 	mmc_out(ioaddr, mmwoff(0, mmw_fee_addr), 0x00);
527 	mmc_out(ioaddr, mmwoff(0, mmw_fee_ctrl), MMW_FEE_CTRL_PRWRITE);
528 
529 	fee_wait(ioaddr, 10, 100);
530 #endif				/* EEPROM_IS_PROTECTED */
531 }
532 #endif				/* WIRELESS_EXT */
533 
534 /************************ I82586 SUBROUTINES *************************/
535 /*
536  * Useful subroutines to manage the Ethernet controller
537  */
538 
539 /*------------------------------------------------------------------*/
540 /*
541  * Read bytes from the on-board RAM.
542  * Why does inlining this function make it fail?
543  */
obram_read(unsigned long ioaddr,u16 o,u8 * b,int n)544 static /*inline */ void obram_read(unsigned long ioaddr,
545 				   u16 o, u8 * b, int n)
546 {
547 	outw(o, PIOR1(ioaddr));
548 	insw(PIOP1(ioaddr), (unsigned short *) b, (n + 1) >> 1);
549 }
550 
551 /*------------------------------------------------------------------*/
552 /*
553  * Write bytes to the on-board RAM.
554  */
obram_write(unsigned long ioaddr,u16 o,u8 * b,int n)555 static inline void obram_write(unsigned long ioaddr, u16 o, u8 * b, int n)
556 {
557 	outw(o, PIOR1(ioaddr));
558 	outsw(PIOP1(ioaddr), (unsigned short *) b, (n + 1) >> 1);
559 }
560 
561 /*------------------------------------------------------------------*/
562 /*
563  * Acknowledge the reading of the status issued by the i82586.
564  */
wv_ack(device * dev)565 static void wv_ack(device * dev)
566 {
567 	net_local *lp = (net_local *) dev->priv;
568 	unsigned long ioaddr = dev->base_addr;
569 	u16 scb_cs;
570 	int i;
571 
572 	obram_read(ioaddr, scboff(OFFSET_SCB, scb_status),
573 		   (unsigned char *) &scb_cs, sizeof(scb_cs));
574 	scb_cs &= SCB_ST_INT;
575 
576 	if (scb_cs == 0)
577 		return;
578 
579 	obram_write(ioaddr, scboff(OFFSET_SCB, scb_command),
580 		    (unsigned char *) &scb_cs, sizeof(scb_cs));
581 
582 	set_chan_attn(ioaddr, lp->hacr);
583 
584 	for (i = 1000; i > 0; i--) {
585 		obram_read(ioaddr, scboff(OFFSET_SCB, scb_command),
586 			   (unsigned char *) &scb_cs, sizeof(scb_cs));
587 		if (scb_cs == 0)
588 			break;
589 
590 		udelay(10);
591 	}
592 	udelay(100);
593 
594 #ifdef DEBUG_CONFIG_ERROR
595 	if (i <= 0)
596 		printk(KERN_INFO
597 		       "%s: wv_ack(): board not accepting command.\n",
598 		       dev->name);
599 #endif
600 }
601 
602 /*------------------------------------------------------------------*/
603 /*
604  * Set channel attention bit and busy wait until command has
605  * completed, then acknowledge completion of the command.
606  */
wv_synchronous_cmd(device * dev,const char * str)607 static inline int wv_synchronous_cmd(device * dev, const char *str)
608 {
609 	net_local *lp = (net_local *) dev->priv;
610 	unsigned long ioaddr = dev->base_addr;
611 	u16 scb_cmd;
612 	ach_t cb;
613 	int i;
614 
615 	scb_cmd = SCB_CMD_CUC & SCB_CMD_CUC_GO;
616 	obram_write(ioaddr, scboff(OFFSET_SCB, scb_command),
617 		    (unsigned char *) &scb_cmd, sizeof(scb_cmd));
618 
619 	set_chan_attn(ioaddr, lp->hacr);
620 
621 	for (i = 1000; i > 0; i--) {
622 		obram_read(ioaddr, OFFSET_CU, (unsigned char *) &cb,
623 			   sizeof(cb));
624 		if (cb.ac_status & AC_SFLD_C)
625 			break;
626 
627 		udelay(10);
628 	}
629 	udelay(100);
630 
631 	if (i <= 0 || !(cb.ac_status & AC_SFLD_OK)) {
632 #ifdef DEBUG_CONFIG_ERROR
633 		printk(KERN_INFO "%s: %s failed; status = 0x%x\n",
634 		       dev->name, str, cb.ac_status);
635 #endif
636 #ifdef DEBUG_I82586_SHOW
637 		wv_scb_show(ioaddr);
638 #endif
639 		return -1;
640 	}
641 
642 	/* Ack the status */
643 	wv_ack(dev);
644 
645 	return 0;
646 }
647 
648 /*------------------------------------------------------------------*/
649 /*
650  * Configuration commands completion interrupt.
651  * Check if done, and if OK.
652  */
653 static inline int
wv_config_complete(device * dev,unsigned long ioaddr,net_local * lp)654 wv_config_complete(device * dev, unsigned long ioaddr, net_local * lp)
655 {
656 	unsigned short mcs_addr;
657 	unsigned short status;
658 	int ret;
659 
660 #ifdef DEBUG_INTERRUPT_TRACE
661 	printk(KERN_DEBUG "%s: ->wv_config_complete()\n", dev->name);
662 #endif
663 
664 	mcs_addr = lp->tx_first_in_use + sizeof(ac_tx_t) + sizeof(ac_nop_t)
665 	    + sizeof(tbd_t) + sizeof(ac_cfg_t) + sizeof(ac_ias_t);
666 
667 	/* Read the status of the last command (set mc list). */
668 	obram_read(ioaddr, acoff(mcs_addr, ac_status),
669 		   (unsigned char *) &status, sizeof(status));
670 
671 	/* If not completed -> exit */
672 	if ((status & AC_SFLD_C) == 0)
673 		ret = 0;	/* Not ready to be scrapped */
674 	else {
675 #ifdef DEBUG_CONFIG_ERROR
676 		unsigned short cfg_addr;
677 		unsigned short ias_addr;
678 
679 		/* Check mc_config command */
680 		if ((status & AC_SFLD_OK) != AC_SFLD_OK)
681 			printk(KERN_INFO
682 			       "%s: wv_config_complete(): set_multicast_address failed; status = 0x%x\n",
683 			       dev->name, status);
684 
685 		/* check ia-config command */
686 		ias_addr = mcs_addr - sizeof(ac_ias_t);
687 		obram_read(ioaddr, acoff(ias_addr, ac_status),
688 			   (unsigned char *) &status, sizeof(status));
689 		if ((status & AC_SFLD_OK) != AC_SFLD_OK)
690 			printk(KERN_INFO
691 			       "%s: wv_config_complete(): set_MAC_address failed; status = 0x%x\n",
692 			       dev->name, status);
693 
694 		/* Check config command. */
695 		cfg_addr = ias_addr - sizeof(ac_cfg_t);
696 		obram_read(ioaddr, acoff(cfg_addr, ac_status),
697 			   (unsigned char *) &status, sizeof(status));
698 		if ((status & AC_SFLD_OK) != AC_SFLD_OK)
699 			printk(KERN_INFO
700 			       "%s: wv_config_complete(): configure failed; status = 0x%x\n",
701 			       dev->name, status);
702 #endif	/* DEBUG_CONFIG_ERROR */
703 
704 		ret = 1;	/* Ready to be scrapped */
705 	}
706 
707 #ifdef DEBUG_INTERRUPT_TRACE
708 	printk(KERN_DEBUG "%s: <-wv_config_complete() - %d\n", dev->name,
709 	       ret);
710 #endif
711 	return ret;
712 }
713 
714 /*------------------------------------------------------------------*/
715 /*
716  * Command completion interrupt.
717  * Reclaim as many freed tx buffers as we can.
718  * (called in wavelan_interrupt()).
719  * Note : the spinlock is already grabbed for us.
720  */
wv_complete(device * dev,unsigned long ioaddr,net_local * lp)721 static int wv_complete(device * dev, unsigned long ioaddr, net_local * lp)
722 {
723 	int nreaped = 0;
724 
725 #ifdef DEBUG_INTERRUPT_TRACE
726 	printk(KERN_DEBUG "%s: ->wv_complete()\n", dev->name);
727 #endif
728 
729 	/* Loop on all the transmit buffers */
730 	while (lp->tx_first_in_use != I82586NULL) {
731 		unsigned short tx_status;
732 
733 		/* Read the first transmit buffer */
734 		obram_read(ioaddr, acoff(lp->tx_first_in_use, ac_status),
735 			   (unsigned char *) &tx_status,
736 			   sizeof(tx_status));
737 
738 		/* If not completed -> exit */
739 		if ((tx_status & AC_SFLD_C) == 0)
740 			break;
741 
742 		/* Hack for reconfiguration */
743 		if (tx_status == 0xFFFF)
744 			if (!wv_config_complete(dev, ioaddr, lp))
745 				break;	/* Not completed */
746 
747 		/* We now remove this buffer */
748 		nreaped++;
749 		--lp->tx_n_in_use;
750 
751 /*
752 if (lp->tx_n_in_use > 0)
753 	printk("%c", "0123456789abcdefghijk"[lp->tx_n_in_use]);
754 */
755 
756 		/* Was it the last one? */
757 		if (lp->tx_n_in_use <= 0)
758 			lp->tx_first_in_use = I82586NULL;
759 		else {
760 			/* Next one in the chain */
761 			lp->tx_first_in_use += TXBLOCKZ;
762 			if (lp->tx_first_in_use >=
763 			    OFFSET_CU +
764 			    NTXBLOCKS * TXBLOCKZ) lp->tx_first_in_use -=
765 				    NTXBLOCKS * TXBLOCKZ;
766 		}
767 
768 		/* Hack for reconfiguration */
769 		if (tx_status == 0xFFFF)
770 			continue;
771 
772 		/* Now, check status of the finished command */
773 		if (tx_status & AC_SFLD_OK) {
774 			int ncollisions;
775 
776 			lp->stats.tx_packets++;
777 			ncollisions = tx_status & AC_SFLD_MAXCOL;
778 			lp->stats.collisions += ncollisions;
779 #ifdef DEBUG_TX_INFO
780 			if (ncollisions > 0)
781 				printk(KERN_DEBUG
782 				       "%s: wv_complete(): tx completed after %d collisions.\n",
783 				       dev->name, ncollisions);
784 #endif
785 		} else {
786 			lp->stats.tx_errors++;
787 			if (tx_status & AC_SFLD_S10) {
788 				lp->stats.tx_carrier_errors++;
789 #ifdef DEBUG_TX_FAIL
790 				printk(KERN_DEBUG
791 				       "%s: wv_complete(): tx error: no CS.\n",
792 				       dev->name);
793 #endif
794 			}
795 			if (tx_status & AC_SFLD_S9) {
796 				lp->stats.tx_carrier_errors++;
797 #ifdef DEBUG_TX_FAIL
798 				printk(KERN_DEBUG
799 				       "%s: wv_complete(): tx error: lost CTS.\n",
800 				       dev->name);
801 #endif
802 			}
803 			if (tx_status & AC_SFLD_S8) {
804 				lp->stats.tx_fifo_errors++;
805 #ifdef DEBUG_TX_FAIL
806 				printk(KERN_DEBUG
807 				       "%s: wv_complete(): tx error: slow DMA.\n",
808 				       dev->name);
809 #endif
810 			}
811 			if (tx_status & AC_SFLD_S6) {
812 				lp->stats.tx_heartbeat_errors++;
813 #ifdef DEBUG_TX_FAIL
814 				printk(KERN_DEBUG
815 				       "%s: wv_complete(): tx error: heart beat.\n",
816 				       dev->name);
817 #endif
818 			}
819 			if (tx_status & AC_SFLD_S5) {
820 				lp->stats.tx_aborted_errors++;
821 #ifdef DEBUG_TX_FAIL
822 				printk(KERN_DEBUG
823 				       "%s: wv_complete(): tx error: too many collisions.\n",
824 				       dev->name);
825 #endif
826 			}
827 		}
828 
829 #ifdef DEBUG_TX_INFO
830 		printk(KERN_DEBUG
831 		       "%s: wv_complete(): tx completed, tx_status 0x%04x\n",
832 		       dev->name, tx_status);
833 #endif
834 	}
835 
836 #ifdef DEBUG_INTERRUPT_INFO
837 	if (nreaped > 1)
838 		printk(KERN_DEBUG "%s: wv_complete(): reaped %d\n",
839 		       dev->name, nreaped);
840 #endif
841 
842 	/*
843 	 * Inform upper layers.
844 	 */
845 	if (lp->tx_n_in_use < NTXBLOCKS - 1) {
846 		netif_wake_queue(dev);
847 	}
848 #ifdef DEBUG_INTERRUPT_TRACE
849 	printk(KERN_DEBUG "%s: <-wv_complete()\n", dev->name);
850 #endif
851 	return nreaped;
852 }
853 
854 /*------------------------------------------------------------------*/
855 /*
856  * Reconfigure the i82586, or at least ask for it.
857  * Because wv_82586_config uses a transmission buffer, we must do it
858  * when we are sure that there is one left, so we do it now
859  * or in wavelan_packet_xmit() (I can't find any better place,
860  * wavelan_interrupt is not an option), so you may experience
861  * delays sometimes.
862  */
wv_82586_reconfig(device * dev)863 static inline void wv_82586_reconfig(device * dev)
864 {
865 	net_local *lp = (net_local *) dev->priv;
866 	unsigned long flags;
867 
868 	/* Arm the flag, will be cleard in wv_82586_config() */
869 	lp->reconfig_82586 = 1;
870 
871 	/* Check if we can do it now ! */
872 	if((netif_running(dev)) && !(netif_queue_stopped(dev))) {
873 		wv_splhi(lp, &flags);
874 		/* May fail */
875 		wv_82586_config(dev);
876 		wv_splx(lp, &flags);
877 	}
878 	else {
879 #ifdef DEBUG_CONFIG_INFO
880 		printk(KERN_DEBUG
881 		       "%s: wv_82586_reconfig(): delayed (state = %lX)\n",
882 			       dev->name, dev->state);
883 #endif
884 	}
885 }
886 
887 /********************* DEBUG & INFO SUBROUTINES *********************/
888 /*
889  * This routine is used in the code to show information for debugging.
890  * Most of the time, it dumps the contents of hardware structures.
891  */
892 
893 #ifdef DEBUG_PSA_SHOW
894 /*------------------------------------------------------------------*/
895 /*
896  * Print the formatted contents of the Parameter Storage Area.
897  */
wv_psa_show(psa_t * p)898 static void wv_psa_show(psa_t * p)
899 {
900 	printk(KERN_DEBUG "##### WaveLAN PSA contents: #####\n");
901 	printk(KERN_DEBUG "psa_io_base_addr_1: 0x%02X %02X %02X %02X\n",
902 	       p->psa_io_base_addr_1,
903 	       p->psa_io_base_addr_2,
904 	       p->psa_io_base_addr_3, p->psa_io_base_addr_4);
905 	printk(KERN_DEBUG "psa_rem_boot_addr_1: 0x%02X %02X %02X\n",
906 	       p->psa_rem_boot_addr_1,
907 	       p->psa_rem_boot_addr_2, p->psa_rem_boot_addr_3);
908 	printk(KERN_DEBUG "psa_holi_params: 0x%02x, ", p->psa_holi_params);
909 	printk("psa_int_req_no: %d\n", p->psa_int_req_no);
910 #ifdef DEBUG_SHOW_UNUSED
911 	printk(KERN_DEBUG
912 	       "psa_unused0[]: %02X:%02X:%02X:%02X:%02X:%02X:%02X\n",
913 	       p->psa_unused0[0], p->psa_unused0[1], p->psa_unused0[2],
914 	       p->psa_unused0[3], p->psa_unused0[4], p->psa_unused0[5],
915 	       p->psa_unused0[6]);
916 #endif				/* DEBUG_SHOW_UNUSED */
917 	printk(KERN_DEBUG
918 	       "psa_univ_mac_addr[]: %02x:%02x:%02x:%02x:%02x:%02x\n",
919 	       p->psa_univ_mac_addr[0], p->psa_univ_mac_addr[1],
920 	       p->psa_univ_mac_addr[2], p->psa_univ_mac_addr[3],
921 	       p->psa_univ_mac_addr[4], p->psa_univ_mac_addr[5]);
922 	printk(KERN_DEBUG
923 	       "psa_local_mac_addr[]: %02x:%02x:%02x:%02x:%02x:%02x\n",
924 	       p->psa_local_mac_addr[0], p->psa_local_mac_addr[1],
925 	       p->psa_local_mac_addr[2], p->psa_local_mac_addr[3],
926 	       p->psa_local_mac_addr[4], p->psa_local_mac_addr[5]);
927 	printk(KERN_DEBUG "psa_univ_local_sel: %d, ",
928 	       p->psa_univ_local_sel);
929 	printk("psa_comp_number: %d, ", p->psa_comp_number);
930 	printk("psa_thr_pre_set: 0x%02x\n", p->psa_thr_pre_set);
931 	printk(KERN_DEBUG "psa_feature_select/decay_prm: 0x%02x, ",
932 	       p->psa_feature_select);
933 	printk("psa_subband/decay_update_prm: %d\n", p->psa_subband);
934 	printk(KERN_DEBUG "psa_quality_thr: 0x%02x, ", p->psa_quality_thr);
935 	printk("psa_mod_delay: 0x%02x\n", p->psa_mod_delay);
936 	printk(KERN_DEBUG "psa_nwid: 0x%02x%02x, ", p->psa_nwid[0],
937 	       p->psa_nwid[1]);
938 	printk("psa_nwid_select: %d\n", p->psa_nwid_select);
939 	printk(KERN_DEBUG "psa_encryption_select: %d, ",
940 	       p->psa_encryption_select);
941 	printk
942 	    ("psa_encryption_key[]: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
943 	     p->psa_encryption_key[0], p->psa_encryption_key[1],
944 	     p->psa_encryption_key[2], p->psa_encryption_key[3],
945 	     p->psa_encryption_key[4], p->psa_encryption_key[5],
946 	     p->psa_encryption_key[6], p->psa_encryption_key[7]);
947 	printk(KERN_DEBUG "psa_databus_width: %d\n", p->psa_databus_width);
948 	printk(KERN_DEBUG "psa_call_code/auto_squelch: 0x%02x, ",
949 	       p->psa_call_code[0]);
950 	printk
951 	    ("psa_call_code[]: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n",
952 	     p->psa_call_code[0], p->psa_call_code[1], p->psa_call_code[2],
953 	     p->psa_call_code[3], p->psa_call_code[4], p->psa_call_code[5],
954 	     p->psa_call_code[6], p->psa_call_code[7]);
955 #ifdef DEBUG_SHOW_UNUSED
956 	printk(KERN_DEBUG "psa_reserved[]: %02X:%02X:%02X:%02X\n",
957 	       p->psa_reserved[0],
958 	       p->psa_reserved[1], p->psa_reserved[2], p->psa_reserved[3]);
959 #endif				/* DEBUG_SHOW_UNUSED */
960 	printk(KERN_DEBUG "psa_conf_status: %d, ", p->psa_conf_status);
961 	printk("psa_crc: 0x%02x%02x, ", p->psa_crc[0], p->psa_crc[1]);
962 	printk("psa_crc_status: 0x%02x\n", p->psa_crc_status);
963 }				/* wv_psa_show */
964 #endif				/* DEBUG_PSA_SHOW */
965 
966 #ifdef DEBUG_MMC_SHOW
967 /*------------------------------------------------------------------*/
968 /*
969  * Print the formatted status of the Modem Management Controller.
970  * This function needs to be completed.
971  */
wv_mmc_show(device * dev)972 static void wv_mmc_show(device * dev)
973 {
974 	unsigned long ioaddr = dev->base_addr;
975 	net_local *lp = (net_local *) dev->priv;
976 	mmr_t m;
977 
978 	/* Basic check */
979 	if (hasr_read(ioaddr) & HASR_NO_CLK) {
980 		printk(KERN_WARNING
981 		       "%s: wv_mmc_show: modem not connected\n",
982 		       dev->name);
983 		return;
984 	}
985 
986 	/* Read the mmc */
987 	mmc_out(ioaddr, mmwoff(0, mmw_freeze), 1);
988 	mmc_read(ioaddr, 0, (u8 *) & m, sizeof(m));
989 	mmc_out(ioaddr, mmwoff(0, mmw_freeze), 0);
990 
991 #ifdef WIRELESS_EXT		/* if wireless extension exists in the kernel */
992 	/* Don't forget to update statistics */
993 	lp->wstats.discard.nwid +=
994 	    (m.mmr_wrong_nwid_h << 8) | m.mmr_wrong_nwid_l;
995 #endif				/* WIRELESS_EXT */
996 
997 	printk(KERN_DEBUG "##### WaveLAN modem status registers: #####\n");
998 #ifdef DEBUG_SHOW_UNUSED
999 	printk(KERN_DEBUG
1000 	       "mmc_unused0[]: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n",
1001 	       m.mmr_unused0[0], m.mmr_unused0[1], m.mmr_unused0[2],
1002 	       m.mmr_unused0[3], m.mmr_unused0[4], m.mmr_unused0[5],
1003 	       m.mmr_unused0[6], m.mmr_unused0[7]);
1004 #endif				/* DEBUG_SHOW_UNUSED */
1005 	printk(KERN_DEBUG "Encryption algorithm: %02X - Status: %02X\n",
1006 	       m.mmr_des_avail, m.mmr_des_status);
1007 #ifdef DEBUG_SHOW_UNUSED
1008 	printk(KERN_DEBUG "mmc_unused1[]: %02X:%02X:%02X:%02X:%02X\n",
1009 	       m.mmr_unused1[0],
1010 	       m.mmr_unused1[1],
1011 	       m.mmr_unused1[2], m.mmr_unused1[3], m.mmr_unused1[4]);
1012 #endif				/* DEBUG_SHOW_UNUSED */
1013 	printk(KERN_DEBUG "dce_status: 0x%x [%s%s%s%s]\n",
1014 	       m.mmr_dce_status,
1015 	       (m.
1016 		mmr_dce_status & MMR_DCE_STATUS_RX_BUSY) ?
1017 	       "energy detected," : "",
1018 	       (m.
1019 		mmr_dce_status & MMR_DCE_STATUS_LOOPT_IND) ?
1020 	       "loop test indicated," : "",
1021 	       (m.
1022 		mmr_dce_status & MMR_DCE_STATUS_TX_BUSY) ?
1023 	       "transmitter on," : "",
1024 	       (m.
1025 		mmr_dce_status & MMR_DCE_STATUS_JBR_EXPIRED) ?
1026 	       "jabber timer expired," : "");
1027 	printk(KERN_DEBUG "Dsp ID: %02X\n", m.mmr_dsp_id);
1028 #ifdef DEBUG_SHOW_UNUSED
1029 	printk(KERN_DEBUG "mmc_unused2[]: %02X:%02X\n",
1030 	       m.mmr_unused2[0], m.mmr_unused2[1]);
1031 #endif				/* DEBUG_SHOW_UNUSED */
1032 	printk(KERN_DEBUG "# correct_nwid: %d, # wrong_nwid: %d\n",
1033 	       (m.mmr_correct_nwid_h << 8) | m.mmr_correct_nwid_l,
1034 	       (m.mmr_wrong_nwid_h << 8) | m.mmr_wrong_nwid_l);
1035 	printk(KERN_DEBUG "thr_pre_set: 0x%x [current signal %s]\n",
1036 	       m.mmr_thr_pre_set & MMR_THR_PRE_SET,
1037 	       (m.
1038 		mmr_thr_pre_set & MMR_THR_PRE_SET_CUR) ? "above" :
1039 	       "below");
1040 	printk(KERN_DEBUG "signal_lvl: %d [%s], ",
1041 	       m.mmr_signal_lvl & MMR_SIGNAL_LVL,
1042 	       (m.
1043 		mmr_signal_lvl & MMR_SIGNAL_LVL_VALID) ? "new msg" :
1044 	       "no new msg");
1045 	printk("silence_lvl: %d [%s], ",
1046 	       m.mmr_silence_lvl & MMR_SILENCE_LVL,
1047 	       (m.
1048 		mmr_silence_lvl & MMR_SILENCE_LVL_VALID) ? "update done" :
1049 	       "no new update");
1050 	printk("sgnl_qual: 0x%x [%s]\n", m.mmr_sgnl_qual & MMR_SGNL_QUAL,
1051 	       (m.
1052 		mmr_sgnl_qual & MMR_SGNL_QUAL_ANT) ? "Antenna 1" :
1053 	       "Antenna 0");
1054 #ifdef DEBUG_SHOW_UNUSED
1055 	printk(KERN_DEBUG "netw_id_l: %x\n", m.mmr_netw_id_l);
1056 #endif				/* DEBUG_SHOW_UNUSED */
1057 }				/* wv_mmc_show */
1058 #endif				/* DEBUG_MMC_SHOW */
1059 
1060 #ifdef DEBUG_I82586_SHOW
1061 /*------------------------------------------------------------------*/
1062 /*
1063  * Print the last block of the i82586 memory.
1064  */
wv_scb_show(unsigned long ioaddr)1065 static void wv_scb_show(unsigned long ioaddr)
1066 {
1067 	scb_t scb;
1068 
1069 	obram_read(ioaddr, OFFSET_SCB, (unsigned char *) &scb,
1070 		   sizeof(scb));
1071 
1072 	printk(KERN_DEBUG "##### WaveLAN system control block: #####\n");
1073 
1074 	printk(KERN_DEBUG "status: ");
1075 	printk("stat 0x%x[%s%s%s%s] ",
1076 	       (scb.
1077 		scb_status & (SCB_ST_CX | SCB_ST_FR | SCB_ST_CNA |
1078 			      SCB_ST_RNR)) >> 12,
1079 	       (scb.
1080 		scb_status & SCB_ST_CX) ? "command completion interrupt," :
1081 	       "", (scb.scb_status & SCB_ST_FR) ? "frame received," : "",
1082 	       (scb.
1083 		scb_status & SCB_ST_CNA) ? "command unit not active," : "",
1084 	       (scb.
1085 		scb_status & SCB_ST_RNR) ? "receiving unit not ready," :
1086 	       "");
1087 	printk("cus 0x%x[%s%s%s] ", (scb.scb_status & SCB_ST_CUS) >> 8,
1088 	       ((scb.scb_status & SCB_ST_CUS) ==
1089 		SCB_ST_CUS_IDLE) ? "idle" : "",
1090 	       ((scb.scb_status & SCB_ST_CUS) ==
1091 		SCB_ST_CUS_SUSP) ? "suspended" : "",
1092 	       ((scb.scb_status & SCB_ST_CUS) ==
1093 		SCB_ST_CUS_ACTV) ? "active" : "");
1094 	printk("rus 0x%x[%s%s%s%s]\n", (scb.scb_status & SCB_ST_RUS) >> 4,
1095 	       ((scb.scb_status & SCB_ST_RUS) ==
1096 		SCB_ST_RUS_IDLE) ? "idle" : "",
1097 	       ((scb.scb_status & SCB_ST_RUS) ==
1098 		SCB_ST_RUS_SUSP) ? "suspended" : "",
1099 	       ((scb.scb_status & SCB_ST_RUS) ==
1100 		SCB_ST_RUS_NRES) ? "no resources" : "",
1101 	       ((scb.scb_status & SCB_ST_RUS) ==
1102 		SCB_ST_RUS_RDY) ? "ready" : "");
1103 
1104 	printk(KERN_DEBUG "command: ");
1105 	printk("ack 0x%x[%s%s%s%s] ",
1106 	       (scb.
1107 		scb_command & (SCB_CMD_ACK_CX | SCB_CMD_ACK_FR |
1108 			       SCB_CMD_ACK_CNA | SCB_CMD_ACK_RNR)) >> 12,
1109 	       (scb.
1110 		scb_command & SCB_CMD_ACK_CX) ? "ack cmd completion," : "",
1111 	       (scb.
1112 		scb_command & SCB_CMD_ACK_FR) ? "ack frame received," : "",
1113 	       (scb.
1114 		scb_command & SCB_CMD_ACK_CNA) ? "ack CU not active," : "",
1115 	       (scb.
1116 		scb_command & SCB_CMD_ACK_RNR) ? "ack RU not ready," : "");
1117 	printk("cuc 0x%x[%s%s%s%s%s] ",
1118 	       (scb.scb_command & SCB_CMD_CUC) >> 8,
1119 	       ((scb.scb_command & SCB_CMD_CUC) ==
1120 		SCB_CMD_CUC_NOP) ? "nop" : "",
1121 	       ((scb.scb_command & SCB_CMD_CUC) ==
1122 		SCB_CMD_CUC_GO) ? "start cbl_offset" : "",
1123 	       ((scb.scb_command & SCB_CMD_CUC) ==
1124 		SCB_CMD_CUC_RES) ? "resume execution" : "",
1125 	       ((scb.scb_command & SCB_CMD_CUC) ==
1126 		SCB_CMD_CUC_SUS) ? "suspend execution" : "",
1127 	       ((scb.scb_command & SCB_CMD_CUC) ==
1128 		SCB_CMD_CUC_ABT) ? "abort execution" : "");
1129 	printk("ruc 0x%x[%s%s%s%s%s]\n",
1130 	       (scb.scb_command & SCB_CMD_RUC) >> 4,
1131 	       ((scb.scb_command & SCB_CMD_RUC) ==
1132 		SCB_CMD_RUC_NOP) ? "nop" : "",
1133 	       ((scb.scb_command & SCB_CMD_RUC) ==
1134 		SCB_CMD_RUC_GO) ? "start rfa_offset" : "",
1135 	       ((scb.scb_command & SCB_CMD_RUC) ==
1136 		SCB_CMD_RUC_RES) ? "resume reception" : "",
1137 	       ((scb.scb_command & SCB_CMD_RUC) ==
1138 		SCB_CMD_RUC_SUS) ? "suspend reception" : "",
1139 	       ((scb.scb_command & SCB_CMD_RUC) ==
1140 		SCB_CMD_RUC_ABT) ? "abort reception" : "");
1141 
1142 	printk(KERN_DEBUG "cbl_offset 0x%x ", scb.scb_cbl_offset);
1143 	printk("rfa_offset 0x%x\n", scb.scb_rfa_offset);
1144 
1145 	printk(KERN_DEBUG "crcerrs %d ", scb.scb_crcerrs);
1146 	printk("alnerrs %d ", scb.scb_alnerrs);
1147 	printk("rscerrs %d ", scb.scb_rscerrs);
1148 	printk("ovrnerrs %d\n", scb.scb_ovrnerrs);
1149 }
1150 
1151 /*------------------------------------------------------------------*/
1152 /*
1153  * Print the formatted status of the i82586's receive unit.
1154  */
wv_ru_show(device * dev)1155 static void wv_ru_show(device * dev)
1156 {
1157 	/* net_local *lp = (net_local *) dev->priv; */
1158 
1159 	printk(KERN_DEBUG
1160 	       "##### WaveLAN i82586 receiver unit status: #####\n");
1161 	printk(KERN_DEBUG "ru:");
1162 	/*
1163 	 * Not implemented yet
1164 	 */
1165 	printk("\n");
1166 }				/* wv_ru_show */
1167 
1168 /*------------------------------------------------------------------*/
1169 /*
1170  * Display info about one control block of the i82586 memory.
1171  */
wv_cu_show_one(device * dev,net_local * lp,int i,u16 p)1172 static void wv_cu_show_one(device * dev, net_local * lp, int i, u16 p)
1173 {
1174 	unsigned long ioaddr;
1175 	ac_tx_t actx;
1176 
1177 	ioaddr = dev->base_addr;
1178 
1179 	printk("%d: 0x%x:", i, p);
1180 
1181 	obram_read(ioaddr, p, (unsigned char *) &actx, sizeof(actx));
1182 	printk(" status=0x%x,", actx.tx_h.ac_status);
1183 	printk(" command=0x%x,", actx.tx_h.ac_command);
1184 
1185 	/*
1186 	   {
1187 	   tbd_t      tbd;
1188 
1189 	   obram_read(ioaddr, actx.tx_tbd_offset, (unsigned char *)&tbd, sizeof(tbd));
1190 	   printk(" tbd_status=0x%x,", tbd.tbd_status);
1191 	   }
1192 	 */
1193 
1194 	printk("|");
1195 }
1196 
1197 /*------------------------------------------------------------------*/
1198 /*
1199  * Print status of the command unit of the i82586.
1200  */
wv_cu_show(device * dev)1201 static void wv_cu_show(device * dev)
1202 {
1203 	net_local *lp = (net_local *) dev->priv;
1204 	unsigned int i;
1205 	u16 p;
1206 
1207 	printk(KERN_DEBUG
1208 	       "##### WaveLAN i82586 command unit status: #####\n");
1209 
1210 	printk(KERN_DEBUG);
1211 	for (i = 0, p = lp->tx_first_in_use; i < NTXBLOCKS; i++) {
1212 		wv_cu_show_one(dev, lp, i, p);
1213 
1214 		p += TXBLOCKZ;
1215 		if (p >= OFFSET_CU + NTXBLOCKS * TXBLOCKZ)
1216 			p -= NTXBLOCKS * TXBLOCKZ;
1217 	}
1218 	printk("\n");
1219 }
1220 #endif				/* DEBUG_I82586_SHOW */
1221 
1222 #ifdef DEBUG_DEVICE_SHOW
1223 /*------------------------------------------------------------------*/
1224 /*
1225  * Print the formatted status of the WaveLAN PCMCIA device driver.
1226  */
wv_dev_show(device * dev)1227 static void wv_dev_show(device * dev)
1228 {
1229 	printk(KERN_DEBUG "dev:");
1230 	printk(" state=%lX,", dev->state);
1231 	printk(" trans_start=%ld,", dev->trans_start);
1232 	printk(" flags=0x%x,", dev->flags);
1233 	printk("\n");
1234 }				/* wv_dev_show */
1235 
1236 /*------------------------------------------------------------------*/
1237 /*
1238  * Print the formatted status of the WaveLAN PCMCIA device driver's
1239  * private information.
1240  */
wv_local_show(device * dev)1241 static void wv_local_show(device * dev)
1242 {
1243 	net_local *lp;
1244 
1245 	lp = (net_local *) dev->priv;
1246 
1247 	printk(KERN_DEBUG "local:");
1248 	printk(" tx_n_in_use=%d,", lp->tx_n_in_use);
1249 	printk(" hacr=0x%x,", lp->hacr);
1250 	printk(" rx_head=0x%x,", lp->rx_head);
1251 	printk(" rx_last=0x%x,", lp->rx_last);
1252 	printk(" tx_first_free=0x%x,", lp->tx_first_free);
1253 	printk(" tx_first_in_use=0x%x,", lp->tx_first_in_use);
1254 	printk("\n");
1255 }				/* wv_local_show */
1256 #endif				/* DEBUG_DEVICE_SHOW */
1257 
1258 #if defined(DEBUG_RX_INFO) || defined(DEBUG_TX_INFO)
1259 /*------------------------------------------------------------------*/
1260 /*
1261  * Dump packet header (and content if necessary) on the screen
1262  */
wv_packet_info(u8 * p,int length,char * msg1,char * msg2)1263 static inline void wv_packet_info(u8 * p,	/* Packet to dump */
1264 				  int length,	/* Length of the packet */
1265 				  char *msg1,	/* Name of the device */
1266 				  char *msg2)
1267 {				/* Name of the function */
1268 	int i;
1269 	int maxi;
1270 
1271 	printk(KERN_DEBUG
1272 	       "%s: %s(): dest %02X:%02X:%02X:%02X:%02X:%02X, length %d\n",
1273 	       msg1, msg2, p[0], p[1], p[2], p[3], p[4], p[5], length);
1274 	printk(KERN_DEBUG
1275 	       "%s: %s(): src %02X:%02X:%02X:%02X:%02X:%02X, type 0x%02X%02X\n",
1276 	       msg1, msg2, p[6], p[7], p[8], p[9], p[10], p[11], p[12],
1277 	       p[13]);
1278 
1279 #ifdef DEBUG_PACKET_DUMP
1280 
1281 	printk(KERN_DEBUG "data=\"");
1282 
1283 	if ((maxi = length) > DEBUG_PACKET_DUMP)
1284 		maxi = DEBUG_PACKET_DUMP;
1285 	for (i = 14; i < maxi; i++)
1286 		if (p[i] >= ' ' && p[i] <= '~')
1287 			printk(" %c", p[i]);
1288 		else
1289 			printk("%02X", p[i]);
1290 	if (maxi < length)
1291 		printk("..");
1292 	printk("\"\n");
1293 	printk(KERN_DEBUG "\n");
1294 #endif				/* DEBUG_PACKET_DUMP */
1295 }
1296 #endif				/* defined(DEBUG_RX_INFO) || defined(DEBUG_TX_INFO) */
1297 
1298 /*------------------------------------------------------------------*/
1299 /*
1300  * This is the information which is displayed by the driver at startup.
1301  * There are lots of flags for configuring it to your liking.
1302  */
wv_init_info(device * dev)1303 static inline void wv_init_info(device * dev)
1304 {
1305 	short ioaddr = dev->base_addr;
1306 	net_local *lp = (net_local *) dev->priv;
1307 	psa_t psa;
1308 	int i;
1309 
1310 	/* Read the parameter storage area */
1311 	psa_read(ioaddr, lp->hacr, 0, (unsigned char *) &psa, sizeof(psa));
1312 
1313 #ifdef DEBUG_PSA_SHOW
1314 	wv_psa_show(&psa);
1315 #endif
1316 #ifdef DEBUG_MMC_SHOW
1317 	wv_mmc_show(dev);
1318 #endif
1319 #ifdef DEBUG_I82586_SHOW
1320 	wv_cu_show(dev);
1321 #endif
1322 
1323 #ifdef DEBUG_BASIC_SHOW
1324 	/* Now, let's go for the basic stuff. */
1325 	printk(KERN_NOTICE "%s: WaveLAN at %#x,", dev->name, ioaddr);
1326 	for (i = 0; i < WAVELAN_ADDR_SIZE; i++)
1327 		printk("%s%02X", (i == 0) ? " " : ":", dev->dev_addr[i]);
1328 	printk(", IRQ %d", dev->irq);
1329 
1330 	/* Print current network ID. */
1331 	if (psa.psa_nwid_select)
1332 		printk(", nwid 0x%02X-%02X", psa.psa_nwid[0],
1333 		       psa.psa_nwid[1]);
1334 	else
1335 		printk(", nwid off");
1336 
1337 	/* If 2.00 card */
1338 	if (!(mmc_in(ioaddr, mmroff(0, mmr_fee_status)) &
1339 	      (MMR_FEE_STATUS_DWLD | MMR_FEE_STATUS_BUSY))) {
1340 		unsigned short freq;
1341 
1342 		/* Ask the EEPROM to read the frequency from the first area. */
1343 		fee_read(ioaddr, 0x00, &freq, 1);
1344 
1345 		/* Print frequency */
1346 		printk(", 2.00, %ld", (freq >> 6) + 2400L);
1347 
1348 		/* Hack! */
1349 		if (freq & 0x20)
1350 			printk(".5");
1351 	} else {
1352 		printk(", PC");
1353 		switch (psa.psa_comp_number) {
1354 		case PSA_COMP_PC_AT_915:
1355 		case PSA_COMP_PC_AT_2400:
1356 			printk("-AT");
1357 			break;
1358 		case PSA_COMP_PC_MC_915:
1359 		case PSA_COMP_PC_MC_2400:
1360 			printk("-MC");
1361 			break;
1362 		case PSA_COMP_PCMCIA_915:
1363 			printk("MCIA");
1364 			break;
1365 		default:
1366 			printk("?");
1367 		}
1368 		printk(", ");
1369 		switch (psa.psa_subband) {
1370 		case PSA_SUBBAND_915:
1371 			printk("915");
1372 			break;
1373 		case PSA_SUBBAND_2425:
1374 			printk("2425");
1375 			break;
1376 		case PSA_SUBBAND_2460:
1377 			printk("2460");
1378 			break;
1379 		case PSA_SUBBAND_2484:
1380 			printk("2484");
1381 			break;
1382 		case PSA_SUBBAND_2430_5:
1383 			printk("2430.5");
1384 			break;
1385 		default:
1386 			printk("?");
1387 		}
1388 	}
1389 
1390 	printk(" MHz\n");
1391 #endif				/* DEBUG_BASIC_SHOW */
1392 
1393 #ifdef DEBUG_VERSION_SHOW
1394 	/* Print version information */
1395 	printk(KERN_NOTICE "%s", version);
1396 #endif
1397 }				/* wv_init_info */
1398 
1399 /********************* IOCTL, STATS & RECONFIG *********************/
1400 /*
1401  * We found here routines that are called by Linux on different
1402  * occasions after the configuration and not for transmitting data
1403  * These may be called when the user use ifconfig, /proc/net/dev
1404  * or wireless extensions
1405  */
1406 
1407 /*------------------------------------------------------------------*/
1408 /*
1409  * Get the current Ethernet statistics. This may be called with the
1410  * card open or closed.
1411  * Used when the user read /proc/net/dev
1412  */
wavelan_get_stats(device * dev)1413 static en_stats *wavelan_get_stats(device * dev)
1414 {
1415 #ifdef DEBUG_IOCTL_TRACE
1416 	printk(KERN_DEBUG "%s: <>wavelan_get_stats()\n", dev->name);
1417 #endif
1418 
1419 	return (&((net_local *) dev->priv)->stats);
1420 }
1421 
1422 /*------------------------------------------------------------------*/
1423 /*
1424  * Set or clear the multicast filter for this adaptor.
1425  * num_addrs == -1	Promiscuous mode, receive all packets
1426  * num_addrs == 0	Normal mode, clear multicast list
1427  * num_addrs > 0	Multicast mode, receive normal and MC packets,
1428  *			and do best-effort filtering.
1429  */
wavelan_set_multicast_list(device * dev)1430 static void wavelan_set_multicast_list(device * dev)
1431 {
1432 	net_local *lp = (net_local *) dev->priv;
1433 
1434 #ifdef DEBUG_IOCTL_TRACE
1435 	printk(KERN_DEBUG "%s: ->wavelan_set_multicast_list()\n",
1436 	       dev->name);
1437 #endif
1438 
1439 #ifdef DEBUG_IOCTL_INFO
1440 	printk(KERN_DEBUG
1441 	       "%s: wavelan_set_multicast_list(): setting Rx mode %02X to %d addresses.\n",
1442 	       dev->name, dev->flags, dev->mc_count);
1443 #endif
1444 
1445 	/* Are we asking for promiscuous mode,
1446 	 * or all multicast addresses (we don't have that!)
1447 	 * or too many multicast addresses for the hardware filter? */
1448 	if ((dev->flags & IFF_PROMISC) ||
1449 	    (dev->flags & IFF_ALLMULTI) ||
1450 	    (dev->mc_count > I82586_MAX_MULTICAST_ADDRESSES)) {
1451 		/*
1452 		 * Enable promiscuous mode: receive all packets.
1453 		 */
1454 		if (!lp->promiscuous) {
1455 			lp->promiscuous = 1;
1456 			lp->mc_count = 0;
1457 
1458 			wv_82586_reconfig(dev);
1459 
1460 			/* Tell the kernel that we are doing a really bad job. */
1461 			dev->flags |= IFF_PROMISC;
1462 		}
1463 	} else
1464 		/* Are there multicast addresses to send? */
1465 	if (dev->mc_list != (struct dev_mc_list *) NULL) {
1466 		/*
1467 		 * Disable promiscuous mode, but receive all packets
1468 		 * in multicast list
1469 		 */
1470 #ifdef MULTICAST_AVOID
1471 		if (lp->promiscuous || (dev->mc_count != lp->mc_count))
1472 #endif
1473 		{
1474 			lp->promiscuous = 0;
1475 			lp->mc_count = dev->mc_count;
1476 
1477 			wv_82586_reconfig(dev);
1478 		}
1479 	} else {
1480 		/*
1481 		 * Switch to normal mode: disable promiscuous mode and
1482 		 * clear the multicast list.
1483 		 */
1484 		if (lp->promiscuous || lp->mc_count == 0) {
1485 			lp->promiscuous = 0;
1486 			lp->mc_count = 0;
1487 
1488 			wv_82586_reconfig(dev);
1489 		}
1490 	}
1491 #ifdef DEBUG_IOCTL_TRACE
1492 	printk(KERN_DEBUG "%s: <-wavelan_set_multicast_list()\n",
1493 	       dev->name);
1494 #endif
1495 }
1496 
1497 /*------------------------------------------------------------------*/
1498 /*
1499  * This function doesn't exist.
1500  * (Note : it was a nice way to test the reconfigure stuff...)
1501  */
1502 #ifdef SET_MAC_ADDRESS
wavelan_set_mac_address(device * dev,void * addr)1503 static int wavelan_set_mac_address(device * dev, void *addr)
1504 {
1505 	struct sockaddr *mac = addr;
1506 
1507 	/* Copy the address. */
1508 	memcpy(dev->dev_addr, mac->sa_data, WAVELAN_ADDR_SIZE);
1509 
1510 	/* Reconfigure the beast. */
1511 	wv_82586_reconfig(dev);
1512 
1513 	return 0;
1514 }
1515 #endif				/* SET_MAC_ADDRESS */
1516 
1517 #ifdef WIRELESS_EXT		/* if wireless extensions exist in the kernel */
1518 
1519 /*------------------------------------------------------------------*/
1520 /*
1521  * Frequency setting (for hardware capable of it)
1522  * It's a bit complicated and you don't really want to look into it.
1523  * (called in wavelan_ioctl)
1524  */
wv_set_frequency(unsigned long ioaddr,iw_freq * frequency)1525 static inline int wv_set_frequency(unsigned long ioaddr,	/* I/O port of the card */
1526 				   iw_freq * frequency)
1527 {
1528 	const int BAND_NUM = 10;	/* Number of bands */
1529 	long freq = 0L;		/* offset to 2.4 GHz in .5 MHz */
1530 #ifdef DEBUG_IOCTL_INFO
1531 	int i;
1532 #endif
1533 
1534 	/* Setting by frequency */
1535 	/* Theoretically, you may set any frequency between
1536 	 * the two limits with a 0.5 MHz precision. In practice,
1537 	 * I don't want you to have trouble with local regulations.
1538 	 */
1539 	if ((frequency->e == 1) &&
1540 	    (frequency->m >= (int) 2.412e8)
1541 	    && (frequency->m <= (int) 2.487e8)) {
1542 		freq = ((frequency->m / 10000) - 24000L) / 5;
1543 	}
1544 
1545 	/* Setting by channel (same as wfreqsel) */
1546 	/* Warning: each channel is 22 MHz wide, so some of the channels
1547 	 * will interfere. */
1548 	if ((frequency->e == 0) && (frequency->m < BAND_NUM)) {
1549 		/* Get frequency offset. */
1550 		freq = channel_bands[frequency->m] >> 1;
1551 	}
1552 
1553 	/* Verify that the frequency is allowed. */
1554 	if (freq != 0L) {
1555 		u16 table[10];	/* Authorized frequency table */
1556 
1557 		/* Read the frequency table. */
1558 		fee_read(ioaddr, 0x71, table, 10);
1559 
1560 #ifdef DEBUG_IOCTL_INFO
1561 		printk(KERN_DEBUG "Frequency table: ");
1562 		for (i = 0; i < 10; i++) {
1563 			printk(" %04X", table[i]);
1564 		}
1565 		printk("\n");
1566 #endif
1567 
1568 		/* Look in the table to see whether the frequency is allowed. */
1569 		if (!(table[9 - ((freq - 24) / 16)] &
1570 		      (1 << ((freq - 24) % 16)))) return -EINVAL;	/* not allowed */
1571 	} else
1572 		return -EINVAL;
1573 
1574 	/* if we get a usable frequency */
1575 	if (freq != 0L) {
1576 		unsigned short area[16];
1577 		unsigned short dac[2];
1578 		unsigned short area_verify[16];
1579 		unsigned short dac_verify[2];
1580 		/* Corresponding gain (in the power adjust value table)
1581 		 * See AT&T WaveLAN Data Manual, REF 407-024689/E, page 3-8
1582 		 * and WCIN062D.DOC, page 6.2.9. */
1583 		unsigned short power_limit[] = { 40, 80, 120, 160, 0 };
1584 		int power_band = 0;	/* Selected band */
1585 		unsigned short power_adjust;	/* Correct value */
1586 
1587 		/* Search for the gain. */
1588 		power_band = 0;
1589 		while ((freq > power_limit[power_band]) &&
1590 		       (power_limit[++power_band] != 0));
1591 
1592 		/* Read the first area. */
1593 		fee_read(ioaddr, 0x00, area, 16);
1594 
1595 		/* Read the DAC. */
1596 		fee_read(ioaddr, 0x60, dac, 2);
1597 
1598 		/* Read the new power adjust value. */
1599 		fee_read(ioaddr, 0x6B - (power_band >> 1), &power_adjust,
1600 			 1);
1601 		if (power_band & 0x1)
1602 			power_adjust >>= 8;
1603 		else
1604 			power_adjust &= 0xFF;
1605 
1606 #ifdef DEBUG_IOCTL_INFO
1607 		printk(KERN_DEBUG "WaveLAN EEPROM Area 1: ");
1608 		for (i = 0; i < 16; i++) {
1609 			printk(" %04X", area[i]);
1610 		}
1611 		printk("\n");
1612 
1613 		printk(KERN_DEBUG "WaveLAN EEPROM DAC: %04X %04X\n",
1614 		       dac[0], dac[1]);
1615 #endif
1616 
1617 		/* Frequency offset (for info only) */
1618 		area[0] = ((freq << 5) & 0xFFE0) | (area[0] & 0x1F);
1619 
1620 		/* Receiver Principle main divider coefficient */
1621 		area[3] = (freq >> 1) + 2400L - 352L;
1622 		area[2] = ((freq & 0x1) << 4) | (area[2] & 0xFFEF);
1623 
1624 		/* Transmitter Main divider coefficient */
1625 		area[13] = (freq >> 1) + 2400L;
1626 		area[12] = ((freq & 0x1) << 4) | (area[2] & 0xFFEF);
1627 
1628 		/* Other parts of the area are flags, bit streams or unused. */
1629 
1630 		/* Set the value in the DAC. */
1631 		dac[1] = ((power_adjust >> 1) & 0x7F) | (dac[1] & 0xFF80);
1632 		dac[0] = ((power_adjust & 0x1) << 4) | (dac[0] & 0xFFEF);
1633 
1634 		/* Write the first area. */
1635 		fee_write(ioaddr, 0x00, area, 16);
1636 
1637 		/* Write the DAC. */
1638 		fee_write(ioaddr, 0x60, dac, 2);
1639 
1640 		/* We now should verify here that the writing of the EEPROM went OK. */
1641 
1642 		/* Reread the first area. */
1643 		fee_read(ioaddr, 0x00, area_verify, 16);
1644 
1645 		/* Reread the DAC. */
1646 		fee_read(ioaddr, 0x60, dac_verify, 2);
1647 
1648 		/* Compare. */
1649 		if (memcmp(area, area_verify, 16 * 2) ||
1650 		    memcmp(dac, dac_verify, 2 * 2)) {
1651 #ifdef DEBUG_IOCTL_ERROR
1652 			printk(KERN_INFO
1653 			       "WaveLAN: wv_set_frequency: unable to write new frequency to EEPROM(?).\n");
1654 #endif
1655 			return -EOPNOTSUPP;
1656 		}
1657 
1658 		/* We must download the frequency parameters to the
1659 		 * synthesizers (from the EEPROM - area 1)
1660 		 * Note: as the EEPROM is automatically decremented, we set the end
1661 		 * if the area... */
1662 		mmc_out(ioaddr, mmwoff(0, mmw_fee_addr), 0x0F);
1663 		mmc_out(ioaddr, mmwoff(0, mmw_fee_ctrl),
1664 			MMW_FEE_CTRL_READ | MMW_FEE_CTRL_DWLD);
1665 
1666 		/* Wait until the download is finished. */
1667 		fee_wait(ioaddr, 100, 100);
1668 
1669 		/* We must now download the power adjust value (gain) to
1670 		 * the synthesizers (from the EEPROM - area 7 - DAC). */
1671 		mmc_out(ioaddr, mmwoff(0, mmw_fee_addr), 0x61);
1672 		mmc_out(ioaddr, mmwoff(0, mmw_fee_ctrl),
1673 			MMW_FEE_CTRL_READ | MMW_FEE_CTRL_DWLD);
1674 
1675 		/* Wait for the download to finish. */
1676 		fee_wait(ioaddr, 100, 100);
1677 
1678 #ifdef DEBUG_IOCTL_INFO
1679 		/* Verification of what we have done */
1680 
1681 		printk(KERN_DEBUG "WaveLAN EEPROM Area 1: ");
1682 		for (i = 0; i < 16; i++) {
1683 			printk(" %04X", area_verify[i]);
1684 		}
1685 		printk("\n");
1686 
1687 		printk(KERN_DEBUG "WaveLAN EEPROM DAC:  %04X %04X\n",
1688 		       dac_verify[0], dac_verify[1]);
1689 #endif
1690 
1691 		return 0;
1692 	} else
1693 		return -EINVAL;	/* Bah, never get there... */
1694 }
1695 
1696 /*------------------------------------------------------------------*/
1697 /*
1698  * Give the list of available frequencies.
1699  */
wv_frequency_list(unsigned long ioaddr,iw_freq * list,int max)1700 static inline int wv_frequency_list(unsigned long ioaddr,	/* I/O port of the card */
1701 				    iw_freq * list,	/* List of frequencies to fill */
1702 				    int max)
1703 {				/* Maximum number of frequencies */
1704 	u16 table[10];	/* Authorized frequency table */
1705 	long freq = 0L;		/* offset to 2.4 GHz in .5 MHz + 12 MHz */
1706 	int i;			/* index in the table */
1707 	int c = 0;		/* Channel number */
1708 
1709 	/* Read the frequency table. */
1710 	fee_read(ioaddr, 0x71 /* frequency table */ , table, 10);
1711 
1712 	/* Check all frequencies. */
1713 	i = 0;
1714 	for (freq = 0; freq < 150; freq++)
1715 		/* Look in the table if the frequency is allowed */
1716 		if (table[9 - (freq / 16)] & (1 << (freq % 16))) {
1717 			/* Compute approximate channel number */
1718 			while ((((channel_bands[c] >> 1) - 24) < freq) &&
1719 			       (c < NELS(channel_bands)))
1720 				c++;
1721 			list[i].i = c;	/* Set the list index */
1722 
1723 			/* put in the list */
1724 			list[i].m = (((freq + 24) * 5) + 24000L) * 10000;
1725 			list[i++].e = 1;
1726 
1727 			/* Check number. */
1728 			if (i >= max)
1729 				return (i);
1730 		}
1731 
1732 	return (i);
1733 }
1734 
1735 #ifdef WIRELESS_SPY
1736 /*------------------------------------------------------------------*/
1737 /*
1738  * Gather wireless spy statistics:  for each packet, compare the source
1739  * address with our list, and if they match, get the statistics.
1740  * Sorry, but this function really needs the wireless extensions.
1741  */
wl_spy_gather(device * dev,u8 * mac,u8 * stats)1742 static inline void wl_spy_gather(device * dev, u8 * mac,	/* MAC address */
1743 				 u8 * stats)
1744 {				/* Statistics to gather */
1745 	net_local *lp = (net_local *) dev->priv;
1746 	int i;
1747 
1748 	/* Check all addresses. */
1749 	for (i = 0; i < lp->spy_number; i++)
1750 		/* If match */
1751 		if (!memcmp(mac, lp->spy_address[i], WAVELAN_ADDR_SIZE)) {
1752 			/* Update statistics */
1753 			lp->spy_stat[i].qual = stats[2] & MMR_SGNL_QUAL;
1754 			lp->spy_stat[i].level = stats[0] & MMR_SIGNAL_LVL;
1755 			lp->spy_stat[i].noise = stats[1] & MMR_SILENCE_LVL;
1756 			lp->spy_stat[i].updated = 0x7;
1757 		}
1758 }
1759 #endif				/* WIRELESS_SPY */
1760 
1761 #ifdef HISTOGRAM
1762 /*------------------------------------------------------------------*/
1763 /*
1764  * This function calculates a histogram of the signal level.
1765  * As the noise is quite constant, it's like doing it on the SNR.
1766  * We have defined a set of interval (lp->his_range), and each time
1767  * the level goes in that interval, we increment the count (lp->his_sum).
1768  * With this histogram you may detect if one WaveLAN is really weak,
1769  * or you may also calculate the mean and standard deviation of the level.
1770  */
wl_his_gather(device * dev,u8 * stats)1771 static inline void wl_his_gather(device * dev, u8 * stats)
1772 {				/* Statistics to gather */
1773 	net_local *lp = (net_local *) dev->priv;
1774 	u8 level = stats[0] & MMR_SIGNAL_LVL;
1775 	int i;
1776 
1777 	/* Find the correct interval. */
1778 	i = 0;
1779 	while ((i < (lp->his_number - 1))
1780 	       && (level >= lp->his_range[i++]));
1781 
1782 	/* Increment interval counter. */
1783 	(lp->his_sum[i])++;
1784 }
1785 #endif				/* HISTOGRAM */
1786 
1787 /*------------------------------------------------------------------*/
1788 /*
1789  * Perform ioctl for configuration and information.
1790  * It is here that the wireless extensions are treated (iwconfig).
1791  */
wavelan_ioctl(struct net_device * dev,struct ifreq * rq,int cmd)1792 static int wavelan_ioctl(struct net_device *dev,	/* device on which the ioctl is applied */
1793 			 struct ifreq *rq,	/* data passed */
1794 			 int cmd)
1795 {				/* ioctl number */
1796 	unsigned long ioaddr = dev->base_addr;
1797 	net_local *lp = (net_local *) dev->priv;	/* lp is not unused */
1798 	struct iwreq *wrq = (struct iwreq *) rq;
1799 	psa_t psa;
1800 	mm_t m;
1801 	unsigned long flags;
1802 	int ret = 0;
1803 	int err = 0;
1804 
1805 #ifdef DEBUG_IOCTL_TRACE
1806 	printk(KERN_DEBUG "%s: ->wavelan_ioctl(cmd=0x%X)\n", dev->name,
1807 	       cmd);
1808 #endif
1809 
1810 	/* Disable interrupts and save flags. */
1811 	wv_splhi(lp, &flags);
1812 
1813 	/* Look what is the request */
1814 	switch (cmd) {
1815 		/* --------------- WIRELESS EXTENSIONS --------------- */
1816 
1817 	case SIOCGIWNAME:
1818 		strcpy(wrq->u.name, "WaveLAN");
1819 		break;
1820 
1821 	case SIOCSIWNWID:
1822 		/* Set NWID in WaveLAN. */
1823 		if (!wrq->u.nwid.disabled) {
1824 			/* Set NWID in psa */
1825 			psa.psa_nwid[0] =
1826 			    (wrq->u.nwid.value & 0xFF00) >> 8;
1827 			psa.psa_nwid[1] = wrq->u.nwid.value & 0xFF;
1828 			psa.psa_nwid_select = 0x01;
1829 			psa_write(ioaddr, lp->hacr,
1830 				  (char *) psa.psa_nwid - (char *) &psa,
1831 				  (unsigned char *) psa.psa_nwid, 3);
1832 
1833 			/* Set NWID in mmc. */
1834 			m.w.mmw_netw_id_l = psa.psa_nwid[1];
1835 			m.w.mmw_netw_id_h = psa.psa_nwid[0];
1836 			mmc_write(ioaddr,
1837 				  (char *) &m.w.mmw_netw_id_l -
1838 				  (char *) &m,
1839 				  (unsigned char *) &m.w.mmw_netw_id_l, 2);
1840 			mmc_out(ioaddr, mmwoff(0, mmw_loopt_sel), 0x00);
1841 		} else {
1842 			/* Disable NWID in the psa. */
1843 			psa.psa_nwid_select = 0x00;
1844 			psa_write(ioaddr, lp->hacr,
1845 				  (char *) &psa.psa_nwid_select -
1846 				  (char *) &psa,
1847 				  (unsigned char *) &psa.psa_nwid_select,
1848 				  1);
1849 
1850 			/* Disable NWID in the mmc (no filtering). */
1851 			mmc_out(ioaddr, mmwoff(0, mmw_loopt_sel),
1852 				MMW_LOOPT_SEL_DIS_NWID);
1853 		}
1854 		/* update the Wavelan checksum */
1855 		update_psa_checksum(dev, ioaddr, lp->hacr);
1856 		break;
1857 
1858 	case SIOCGIWNWID:
1859 		/* Read the NWID. */
1860 		psa_read(ioaddr, lp->hacr,
1861 			 (char *) psa.psa_nwid - (char *) &psa,
1862 			 (unsigned char *) psa.psa_nwid, 3);
1863 		wrq->u.nwid.value =
1864 		    (psa.psa_nwid[0] << 8) + psa.psa_nwid[1];
1865 		wrq->u.nwid.disabled = !(psa.psa_nwid_select);
1866 		wrq->u.nwid.fixed = 1;	/* Superfluous */
1867 		break;
1868 
1869 	case SIOCSIWFREQ:
1870 		/* Attempt to recognise 2.00 cards (2.4 GHz frequency selectable). */
1871 		if (!(mmc_in(ioaddr, mmroff(0, mmr_fee_status)) &
1872 		      (MMR_FEE_STATUS_DWLD | MMR_FEE_STATUS_BUSY)))
1873 			ret = wv_set_frequency(ioaddr, &(wrq->u.freq));
1874 		else
1875 			ret = -EOPNOTSUPP;
1876 		break;
1877 
1878 	case SIOCGIWFREQ:
1879 		/* Attempt to recognise 2.00 cards (2.4 GHz frequency selectable).
1880 		 * Does it work for everybody, especially old cards? */
1881 		if (!(mmc_in(ioaddr, mmroff(0, mmr_fee_status)) &
1882 		      (MMR_FEE_STATUS_DWLD | MMR_FEE_STATUS_BUSY))) {
1883 			unsigned short freq;
1884 
1885 			/* Ask the EEPROM to read the frequency from the first area. */
1886 			fee_read(ioaddr, 0x00, &freq, 1);
1887 			wrq->u.freq.m = ((freq >> 5) * 5 + 24000L) * 10000;
1888 			wrq->u.freq.e = 1;
1889 		} else {
1890 			psa_read(ioaddr, lp->hacr,
1891 				 (char *) &psa.psa_subband - (char *) &psa,
1892 				 (unsigned char *) &psa.psa_subband, 1);
1893 
1894 			if (psa.psa_subband <= 4) {
1895 				wrq->u.freq.m =
1896 				    fixed_bands[psa.psa_subband];
1897 				wrq->u.freq.e = (psa.psa_subband != 0);
1898 			} else
1899 				ret = -EOPNOTSUPP;
1900 		}
1901 		break;
1902 
1903 	case SIOCSIWSENS:
1904 		/* Set the level threshold. */
1905 		/* We should complain loudly if wrq->u.sens.fixed = 0, because we
1906 		 * can't set auto mode... */
1907 		psa.psa_thr_pre_set = wrq->u.sens.value & 0x3F;
1908 		psa_write(ioaddr, lp->hacr,
1909 			  (char *) &psa.psa_thr_pre_set - (char *) &psa,
1910 			  (unsigned char *) &psa.psa_thr_pre_set, 1);
1911 		/* update the Wavelan checksum */
1912 		update_psa_checksum(dev, ioaddr, lp->hacr);
1913 		mmc_out(ioaddr, mmwoff(0, mmw_thr_pre_set),
1914 			psa.psa_thr_pre_set);
1915 		break;
1916 
1917 	case SIOCGIWSENS:
1918 		/* Read the level threshold. */
1919 		psa_read(ioaddr, lp->hacr,
1920 			 (char *) &psa.psa_thr_pre_set - (char *) &psa,
1921 			 (unsigned char *) &psa.psa_thr_pre_set, 1);
1922 		wrq->u.sens.value = psa.psa_thr_pre_set & 0x3F;
1923 		wrq->u.sens.fixed = 1;
1924 		break;
1925 
1926 	case SIOCSIWENCODE:
1927 		/* Set encryption key */
1928 		if (!mmc_encr(ioaddr)) {
1929 			ret = -EOPNOTSUPP;
1930 			break;
1931 		}
1932 
1933 		/* Basic checking... */
1934 		if (wrq->u.encoding.pointer != (caddr_t) 0) {
1935 			/* Check the size of the key */
1936 			if (wrq->u.encoding.length != 8) {
1937 				ret = -EINVAL;
1938 				break;
1939 			}
1940 
1941 			/* Copy the key in the driver */
1942 			wv_splx(lp, &flags);
1943 			err = copy_from_user(psa.psa_encryption_key,
1944 					     wrq->u.encoding.pointer,
1945 					     wrq->u.encoding.length);
1946 			wv_splhi(lp, &flags);
1947 			if (err) {
1948 				ret = -EFAULT;
1949 				break;
1950 			}
1951 
1952 			psa.psa_encryption_select = 1;
1953 			psa_write(ioaddr, lp->hacr,
1954 				  (char *) &psa.psa_encryption_select -
1955 				  (char *) &psa,
1956 				  (unsigned char *) &psa.
1957 				  psa_encryption_select, 8 + 1);
1958 
1959 			mmc_out(ioaddr, mmwoff(0, mmw_encr_enable),
1960 				MMW_ENCR_ENABLE_EN | MMW_ENCR_ENABLE_MODE);
1961 			mmc_write(ioaddr, mmwoff(0, mmw_encr_key),
1962 				  (unsigned char *) &psa.
1963 				  psa_encryption_key, 8);
1964 		}
1965 
1966 		if (wrq->u.encoding.flags & IW_ENCODE_DISABLED) {	/* disable encryption */
1967 			psa.psa_encryption_select = 0;
1968 			psa_write(ioaddr, lp->hacr,
1969 				  (char *) &psa.psa_encryption_select -
1970 				  (char *) &psa,
1971 				  (unsigned char *) &psa.
1972 				  psa_encryption_select, 1);
1973 
1974 			mmc_out(ioaddr, mmwoff(0, mmw_encr_enable), 0);
1975 		}
1976 		/* update the Wavelan checksum */
1977 		update_psa_checksum(dev, ioaddr, lp->hacr);
1978 		break;
1979 
1980 	case SIOCGIWENCODE:
1981 		/* Read the encryption key */
1982 		if (!mmc_encr(ioaddr)) {
1983 			ret = -EOPNOTSUPP;
1984 			break;
1985 		}
1986 
1987 		/* only super-user can see encryption key */
1988 		if (!capable(CAP_NET_ADMIN)) {
1989 			ret = -EPERM;
1990 			break;
1991 		}
1992 
1993 		/* Basic checking... */
1994 		if (wrq->u.encoding.pointer != (caddr_t) 0) {
1995 			/* Verify the user buffer */
1996 			ret =
1997 			    verify_area(VERIFY_WRITE,
1998 					wrq->u.encoding.pointer, 8);
1999 			if (ret)
2000 				break;
2001 
2002 			psa_read(ioaddr, lp->hacr,
2003 				 (char *) &psa.psa_encryption_select -
2004 				 (char *) &psa,
2005 				 (unsigned char *) &psa.
2006 				 psa_encryption_select, 1 + 8);
2007 
2008 			/* encryption is enabled ? */
2009 			if (psa.psa_encryption_select)
2010 				wrq->u.encoding.flags = IW_ENCODE_ENABLED;
2011 			else
2012 				wrq->u.encoding.flags = IW_ENCODE_DISABLED;
2013 			wrq->u.encoding.flags |= mmc_encr(ioaddr);
2014 
2015 			/* Copy the key to the user buffer */
2016 			wrq->u.encoding.length = 8;
2017 			wv_splx(lp, &flags);
2018 			if (copy_to_user(wrq->u.encoding.pointer,
2019 					 psa.psa_encryption_key, 8))
2020 				ret = -EFAULT;
2021 			wv_splhi(lp, &flags);
2022 		}
2023 		break;
2024 
2025 	case SIOCGIWRANGE:
2026 		/* basic checking */
2027 		if (wrq->u.data.pointer != (caddr_t) 0) {
2028 			struct iw_range range;
2029 
2030 			/* Set the length (very important for backward
2031 			 * compatibility) */
2032 			wrq->u.data.length = sizeof(struct iw_range);
2033 
2034 			/* Set all the info we don't care or don't know
2035 			 * about to zero */
2036 			memset(&range, 0, sizeof(range));
2037 
2038 			/* Set the Wireless Extension versions */
2039 			range.we_version_compiled = WIRELESS_EXT;
2040 			range.we_version_source = 9;
2041 
2042 			/* Set information in the range struct.  */
2043 			range.throughput = 1.6 * 1000 * 1000;	/* don't argue on this ! */
2044 			range.min_nwid = 0x0000;
2045 			range.max_nwid = 0xFFFF;
2046 
2047 			/* Attempt to recognise 2.00 cards (2.4 GHz frequency selectable). */
2048 			if (!(mmc_in(ioaddr, mmroff(0, mmr_fee_status)) &
2049 			      (MMR_FEE_STATUS_DWLD | MMR_FEE_STATUS_BUSY))) {
2050 				range.num_channels = 10;
2051 				range.num_frequency =
2052 				    wv_frequency_list(ioaddr, range.freq,
2053 						      IW_MAX_FREQUENCIES);
2054 			} else
2055 				range.num_channels = range.num_frequency =
2056 				    0;
2057 
2058 			range.sensitivity = 0x3F;
2059 			range.max_qual.qual = MMR_SGNL_QUAL;
2060 			range.max_qual.level = MMR_SIGNAL_LVL;
2061 			range.max_qual.noise = MMR_SILENCE_LVL;
2062 			range.avg_qual.qual = MMR_SGNL_QUAL; /* Always max */
2063 			/* Need to get better values for those two */
2064 			range.avg_qual.level = 30;
2065 			range.avg_qual.noise = 8;
2066 
2067 			range.num_bitrates = 1;
2068 			range.bitrate[0] = 2000000;	/* 2 Mb/s */
2069 
2070 			/* Encryption supported ? */
2071 			if (mmc_encr(ioaddr)) {
2072 				range.encoding_size[0] = 8;	/* DES = 64 bits key */
2073 				range.num_encoding_sizes = 1;
2074 				range.max_encoding_tokens = 1;	/* Only one key possible */
2075 			} else {
2076 				range.num_encoding_sizes = 0;
2077 				range.max_encoding_tokens = 0;
2078 			}
2079 
2080 			/* Copy structure to the user buffer. */
2081 			wv_splx(lp, &flags);
2082 			if (copy_to_user(wrq->u.data.pointer,
2083 					 &range,
2084 					 sizeof(struct iw_range)))
2085 				ret = -EFAULT;
2086 			wv_splhi(lp, &flags);
2087 		}
2088 		break;
2089 
2090 	case SIOCGIWPRIV:
2091 		/* Basic checking */
2092 		if (wrq->u.data.pointer != (caddr_t) 0) {
2093 			struct iw_priv_args priv[] = {
2094 				/* { cmd,
2095 				     set_args,
2096 				     get_args,
2097 				     name } */
2098 				{ SIOCSIPQTHR,
2099 				  IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | 1,
2100 				  0,
2101 				  "setqualthr" },
2102 				{ SIOCGIPQTHR,
2103 				  0,
2104 				  IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | 1,
2105 				  "getqualthr" },
2106 				{ SIOCSIPHISTO,
2107 				  IW_PRIV_TYPE_BYTE | 16,
2108 				  0,
2109 				  "sethisto" },
2110 				{ SIOCGIPHISTO,
2111 				  0,
2112 				  IW_PRIV_TYPE_INT | 16,
2113 				 "gethisto" },
2114 			};
2115 
2116 			/* Set the number of available ioctls. */
2117 			wrq->u.data.length = 4;
2118 
2119 			/* Copy structure to the user buffer. */
2120 			wv_splx(lp, &flags);
2121 			if (copy_to_user(wrq->u.data.pointer,
2122 					      (u8 *) priv,
2123 					      sizeof(priv)))
2124 				ret = -EFAULT;
2125 			wv_splhi(lp, &flags);
2126 		}
2127 		break;
2128 
2129 #ifdef WIRELESS_SPY
2130 	case SIOCSIWSPY:
2131 		/* Set the spy list */
2132 
2133 		/* Check the number of addresses. */
2134 		if (wrq->u.data.length > IW_MAX_SPY) {
2135 			ret = -E2BIG;
2136 			break;
2137 		}
2138 		lp->spy_number = wrq->u.data.length;
2139 
2140 		/* Are there are addresses to copy? */
2141 		if (lp->spy_number > 0) {
2142 			struct sockaddr address[IW_MAX_SPY];
2143 			int i;
2144 
2145 			/* Copy addresses to the driver. */
2146 			wv_splx(lp, &flags);
2147 			err = copy_from_user(address,
2148 					     wrq->u.data.pointer,
2149 					     sizeof(struct sockaddr)
2150 					     * lp->spy_number);
2151 			wv_splhi(lp, &flags);
2152 			if (err) {
2153 				ret = -EFAULT;
2154 				break;
2155 			}
2156 
2157 			/* Copy addresses to the lp structure. */
2158 			for (i = 0; i < lp->spy_number; i++) {
2159 				memcpy(lp->spy_address[i],
2160 				       address[i].sa_data,
2161 				       WAVELAN_ADDR_SIZE);
2162 			}
2163 
2164 			/* Reset structure. */
2165 			memset(lp->spy_stat, 0x00,
2166 			       sizeof(iw_qual) * IW_MAX_SPY);
2167 
2168 #ifdef DEBUG_IOCTL_INFO
2169 			printk(KERN_DEBUG
2170 			       "SetSpy:  set of new addresses is: \n");
2171 			for (i = 0; i < wrq->u.data.length; i++)
2172 				printk(KERN_DEBUG
2173 				       "%02X:%02X:%02X:%02X:%02X:%02X \n",
2174 				       lp->spy_address[i][0],
2175 				       lp->spy_address[i][1],
2176 				       lp->spy_address[i][2],
2177 				       lp->spy_address[i][3],
2178 				       lp->spy_address[i][4],
2179 				       lp->spy_address[i][5]);
2180 #endif				/* DEBUG_IOCTL_INFO */
2181 		}
2182 
2183 		break;
2184 
2185 	case SIOCGIWSPY:
2186 		/* Get the spy list and spy stats. */
2187 
2188 		/* Set the number of addresses */
2189 		wrq->u.data.length = lp->spy_number;
2190 
2191 		/* Does the user want to have the addresses back? */
2192 		if ((lp->spy_number > 0)
2193 		    && (wrq->u.data.pointer != (caddr_t) 0)) {
2194 			struct sockaddr address[IW_MAX_SPY];
2195 			int i;
2196 
2197 			/* Copy addresses from the lp structure. */
2198 			for (i = 0; i < lp->spy_number; i++) {
2199 				memcpy(address[i].sa_data,
2200 				       lp->spy_address[i],
2201 				       WAVELAN_ADDR_SIZE);
2202 				address[i].sa_family = AF_UNIX;
2203 			}
2204 
2205 			/* Copy addresses to the user buffer. */
2206 			wv_splx(lp, &flags);
2207 			err = copy_to_user(wrq->u.data.pointer,
2208 					   address,
2209 					   sizeof(struct sockaddr)
2210 					   * lp->spy_number);
2211 
2212 			/* Copy stats to the user buffer (just after). */
2213 			err |= copy_to_user(wrq->u.data.pointer
2214 					    + (sizeof(struct sockaddr)
2215 					       * lp->spy_number),
2216 					    lp->spy_stat,
2217 					    sizeof(iw_qual) * lp->spy_number);
2218 			wv_splhi(lp, &flags);
2219 			if (err) {
2220 				ret = -EFAULT;
2221 				break;
2222 			}
2223 
2224 			/* Reset updated flags. */
2225 			for (i = 0; i < lp->spy_number; i++)
2226 				lp->spy_stat[i].updated = 0x0;
2227 		}
2228 		/* if(pointer != NULL) */
2229 		break;
2230 #endif				/* WIRELESS_SPY */
2231 
2232 		/* ------------------ PRIVATE IOCTL ------------------ */
2233 
2234 	case SIOCSIPQTHR:
2235 		if (!capable(CAP_NET_ADMIN)) {
2236 			ret = -EPERM;
2237 			break;
2238 		}
2239 		psa.psa_quality_thr = *(wrq->u.name) & 0x0F;
2240 		psa_write(ioaddr, lp->hacr,
2241 			  (char *) &psa.psa_quality_thr - (char *) &psa,
2242 			  (unsigned char *) &psa.psa_quality_thr, 1);
2243 		/* update the Wavelan checksum */
2244 		update_psa_checksum(dev, ioaddr, lp->hacr);
2245 		mmc_out(ioaddr, mmwoff(0, mmw_quality_thr),
2246 			psa.psa_quality_thr);
2247 		break;
2248 
2249 	case SIOCGIPQTHR:
2250 		psa_read(ioaddr, lp->hacr,
2251 			 (char *) &psa.psa_quality_thr - (char *) &psa,
2252 			 (unsigned char *) &psa.psa_quality_thr, 1);
2253 		*(wrq->u.name) = psa.psa_quality_thr & 0x0F;
2254 		break;
2255 
2256 #ifdef HISTOGRAM
2257 	case SIOCSIPHISTO:
2258 		/* Verify that the user is root. */
2259 		if (!capable(CAP_NET_ADMIN)) {
2260 			ret = -EPERM;
2261 			break;
2262 		}
2263 
2264 		/* Check the number of intervals. */
2265 		if (wrq->u.data.length > 16) {
2266 			ret = -E2BIG;
2267 			break;
2268 		}
2269 		lp->his_number = wrq->u.data.length;
2270 
2271 		/* Are there addresses to copy? */
2272 		if (lp->his_number > 0) {
2273 			/* Copy interval ranges to the driver */
2274 			wv_splx(lp, &flags);
2275 			err = copy_from_user(lp->his_range,
2276 					     wrq->u.data.pointer,
2277 					     sizeof(char) * lp->his_number);
2278 			wv_splhi(lp, &flags);
2279 			if (err) {
2280 				ret = -EFAULT;
2281 				break;
2282 			}
2283 
2284 			/* Reset structure. */
2285 			memset(lp->his_sum, 0x00, sizeof(long) * 16);
2286 		}
2287 		break;
2288 
2289 	case SIOCGIPHISTO:
2290 		/* Set the number of intervals. */
2291 		wrq->u.data.length = lp->his_number;
2292 
2293 		/* Give back the distribution statistics */
2294 		if ((lp->his_number > 0)
2295 		    && (wrq->u.data.pointer != (caddr_t) 0)) {
2296 			/* Copy data to the user buffer. */
2297 			wv_splx(lp, &flags);
2298 			if (copy_to_user(wrq->u.data.pointer,
2299 					 lp->his_sum,
2300 					 sizeof(long) * lp->his_number));
2301 				ret = -EFAULT;
2302 			wv_splhi(lp, &flags);
2303 
2304 		}		/* if(pointer != NULL) */
2305 		break;
2306 #endif				/* HISTOGRAM */
2307 
2308 		/* ------------------- OTHER IOCTL ------------------- */
2309 
2310 	default:
2311 		ret = -EOPNOTSUPP;
2312 	}	/* switch (cmd) */
2313 
2314 	/* Enable interrupts and restore flags. */
2315 	wv_splx(lp, &flags);
2316 
2317 #ifdef DEBUG_IOCTL_TRACE
2318 	printk(KERN_DEBUG "%s: <-wavelan_ioctl()\n", dev->name);
2319 #endif
2320 	return ret;
2321 }
2322 
2323 /*------------------------------------------------------------------*/
2324 /*
2325  * Get wireless statistics.
2326  * Called by /proc/net/wireless
2327  */
wavelan_get_wireless_stats(device * dev)2328 static iw_stats *wavelan_get_wireless_stats(device * dev)
2329 {
2330 	unsigned long ioaddr = dev->base_addr;
2331 	net_local *lp = (net_local *) dev->priv;
2332 	mmr_t m;
2333 	iw_stats *wstats;
2334 	unsigned long flags;
2335 
2336 #ifdef DEBUG_IOCTL_TRACE
2337 	printk(KERN_DEBUG "%s: ->wavelan_get_wireless_stats()\n",
2338 	       dev->name);
2339 #endif
2340 
2341 	/* Check */
2342 	if (lp == (net_local *) NULL)
2343 		return (iw_stats *) NULL;
2344 
2345 	/* Disable interrupts and save flags. */
2346 	wv_splhi(lp, &flags);
2347 
2348 	wstats = &lp->wstats;
2349 
2350 	/* Get data from the mmc. */
2351 	mmc_out(ioaddr, mmwoff(0, mmw_freeze), 1);
2352 
2353 	mmc_read(ioaddr, mmroff(0, mmr_dce_status), &m.mmr_dce_status, 1);
2354 	mmc_read(ioaddr, mmroff(0, mmr_wrong_nwid_l), &m.mmr_wrong_nwid_l,
2355 		 2);
2356 	mmc_read(ioaddr, mmroff(0, mmr_thr_pre_set), &m.mmr_thr_pre_set,
2357 		 4);
2358 
2359 	mmc_out(ioaddr, mmwoff(0, mmw_freeze), 0);
2360 
2361 	/* Copy data to wireless stuff. */
2362 	wstats->status = m.mmr_dce_status & MMR_DCE_STATUS;
2363 	wstats->qual.qual = m.mmr_sgnl_qual & MMR_SGNL_QUAL;
2364 	wstats->qual.level = m.mmr_signal_lvl & MMR_SIGNAL_LVL;
2365 	wstats->qual.noise = m.mmr_silence_lvl & MMR_SILENCE_LVL;
2366 	wstats->qual.updated = (((m. mmr_signal_lvl & MMR_SIGNAL_LVL_VALID) >> 7)
2367 			| ((m.mmr_signal_lvl & MMR_SIGNAL_LVL_VALID) >> 6)
2368 			| ((m.mmr_silence_lvl & MMR_SILENCE_LVL_VALID) >> 5));
2369 	wstats->discard.nwid += (m.mmr_wrong_nwid_h << 8) | m.mmr_wrong_nwid_l;
2370 	wstats->discard.code = 0L;
2371 	wstats->discard.misc = 0L;
2372 
2373 	/* Enable interrupts and restore flags. */
2374 	wv_splx(lp, &flags);
2375 
2376 #ifdef DEBUG_IOCTL_TRACE
2377 	printk(KERN_DEBUG "%s: <-wavelan_get_wireless_stats()\n",
2378 	       dev->name);
2379 #endif
2380 	return &lp->wstats;
2381 }
2382 #endif				/* WIRELESS_EXT */
2383 
2384 /************************* PACKET RECEPTION *************************/
2385 /*
2386  * This part deals with receiving the packets.
2387  * The interrupt handler gets an interrupt when a packet has been
2388  * successfully received and calls this part.
2389  */
2390 
2391 /*------------------------------------------------------------------*/
2392 /*
2393  * This routine does the actual copying of data (including the Ethernet
2394  * header structure) from the WaveLAN card to an sk_buff chain that
2395  * will be passed up to the network interface layer. NOTE: we
2396  * currently don't handle trailer protocols (neither does the rest of
2397  * the network interface), so if that is needed, it will (at least in
2398  * part) be added here.  The contents of the receive ring buffer are
2399  * copied to a message chain that is then passed to the kernel.
2400  *
2401  * Note: if any errors occur, the packet is "dropped on the floor".
2402  * (called by wv_packet_rcv())
2403  */
2404 static inline void
wv_packet_read(device * dev,u16 buf_off,int sksize)2405 wv_packet_read(device * dev, u16 buf_off, int sksize)
2406 {
2407 	net_local *lp = (net_local *) dev->priv;
2408 	unsigned long ioaddr = dev->base_addr;
2409 	struct sk_buff *skb;
2410 
2411 #ifdef DEBUG_RX_TRACE
2412 	printk(KERN_DEBUG "%s: ->wv_packet_read(0x%X, %d)\n",
2413 	       dev->name, buf_off, sksize);
2414 #endif
2415 
2416 	/* Allocate buffer for the data */
2417 	if ((skb = dev_alloc_skb(sksize)) == (struct sk_buff *) NULL) {
2418 #ifdef DEBUG_RX_ERROR
2419 		printk(KERN_INFO
2420 		       "%s: wv_packet_read(): could not alloc_skb(%d, GFP_ATOMIC).\n",
2421 		       dev->name, sksize);
2422 #endif
2423 		lp->stats.rx_dropped++;
2424 		return;
2425 	}
2426 
2427 	skb->dev = dev;
2428 
2429 	/* Copy the packet to the buffer. */
2430 	obram_read(ioaddr, buf_off, skb_put(skb, sksize), sksize);
2431 	skb->protocol = eth_type_trans(skb, dev);
2432 
2433 #ifdef DEBUG_RX_INFO
2434 	wv_packet_info(skb->mac.raw, sksize, dev->name, "wv_packet_read");
2435 #endif				/* DEBUG_RX_INFO */
2436 
2437 	/* Statistics-gathering and associated stuff.
2438 	 * It seem a bit messy with all the define, but it's really simple... */
2439 #if defined(WIRELESS_SPY) || defined(HISTOGRAM)
2440 	if (
2441 #ifdef WIRELESS_SPY
2442 		   (lp->spy_number > 0) ||
2443 #endif				/* WIRELESS_SPY */
2444 #ifdef HISTOGRAM
2445 		   (lp->his_number > 0) ||
2446 #endif				/* HISTOGRAM */
2447 		   0) {
2448 		u8 stats[3];	/* signal level, noise level, signal quality */
2449 
2450 		/* Read signal level, silence level and signal quality bytes. */
2451 		/* Note: in the PCMCIA hardware, these are part of the frame.  It seems
2452 		 * that for the ISA hardware, it's nowhere to be found in the frame,
2453 		 * so I'm obliged to do this (it has a side effect on /proc/net/wireless).
2454 		 * Any ideas?
2455 		 */
2456 		mmc_out(ioaddr, mmwoff(0, mmw_freeze), 1);
2457 		mmc_read(ioaddr, mmroff(0, mmr_signal_lvl), stats, 3);
2458 		mmc_out(ioaddr, mmwoff(0, mmw_freeze), 0);
2459 
2460 #ifdef DEBUG_RX_INFO
2461 		printk(KERN_DEBUG
2462 		       "%s: wv_packet_read(): Signal level %d/63, Silence level %d/63, signal quality %d/16\n",
2463 		       dev->name, stats[0] & 0x3F, stats[1] & 0x3F,
2464 		       stats[2] & 0x0F);
2465 #endif
2466 
2467 		/* Spying stuff */
2468 #ifdef WIRELESS_SPY
2469 		wl_spy_gather(dev, skb->mac.raw + WAVELAN_ADDR_SIZE,
2470 			      stats);
2471 #endif				/* WIRELESS_SPY */
2472 #ifdef HISTOGRAM
2473 		wl_his_gather(dev, stats);
2474 #endif				/* HISTOGRAM */
2475 	}
2476 #endif				/* defined(WIRELESS_SPY) || defined(HISTOGRAM) */
2477 
2478 	/*
2479 	 * Hand the packet to the network module.
2480 	 */
2481 	netif_rx(skb);
2482 
2483 	/* Keep statistics up to date */
2484 	dev->last_rx = jiffies;
2485 	lp->stats.rx_packets++;
2486 	lp->stats.rx_bytes += sksize;
2487 
2488 #ifdef DEBUG_RX_TRACE
2489 	printk(KERN_DEBUG "%s: <-wv_packet_read()\n", dev->name);
2490 #endif
2491 }
2492 
2493 /*------------------------------------------------------------------*/
2494 /*
2495  * Transfer as many packets as we can
2496  * from the device RAM.
2497  * (called in wavelan_interrupt()).
2498  * Note : the spinlock is already grabbed for us.
2499  */
wv_receive(device * dev)2500 static inline void wv_receive(device * dev)
2501 {
2502 	unsigned long ioaddr = dev->base_addr;
2503 	net_local *lp = (net_local *) dev->priv;
2504 	fd_t fd;
2505 	rbd_t rbd;
2506 	int nreaped = 0;
2507 
2508 #ifdef DEBUG_RX_TRACE
2509 	printk(KERN_DEBUG "%s: ->wv_receive()\n", dev->name);
2510 #endif
2511 
2512 	/* Loop on each received packet. */
2513 	for (;;) {
2514 		obram_read(ioaddr, lp->rx_head, (unsigned char *) &fd,
2515 			   sizeof(fd));
2516 
2517 		/* Note about the status :
2518 		 * It start up to be 0 (the value we set). Then, when the RU
2519 		 * grab the buffer to prepare for reception, it sets the
2520 		 * FD_STATUS_B flag. When the RU has finished receiving the
2521 		 * frame, it clears FD_STATUS_B, set FD_STATUS_C to indicate
2522 		 * completion and set the other flags to indicate the eventual
2523 		 * errors. FD_STATUS_OK indicates that the reception was OK.
2524 		 */
2525 
2526 		/* If the current frame is not complete, we have reached the end. */
2527 		if ((fd.fd_status & FD_STATUS_C) != FD_STATUS_C)
2528 			break;	/* This is how we exit the loop. */
2529 
2530 		nreaped++;
2531 
2532 		/* Check whether frame was correctly received. */
2533 		if ((fd.fd_status & FD_STATUS_OK) == FD_STATUS_OK) {
2534 			/* Does the frame contain a pointer to the data?  Let's check. */
2535 			if (fd.fd_rbd_offset != I82586NULL) {
2536 				/* Read the receive buffer descriptor */
2537 				obram_read(ioaddr, fd.fd_rbd_offset,
2538 					   (unsigned char *) &rbd,
2539 					   sizeof(rbd));
2540 
2541 #ifdef DEBUG_RX_ERROR
2542 				if ((rbd.rbd_status & RBD_STATUS_EOF) !=
2543 				    RBD_STATUS_EOF) printk(KERN_INFO
2544 							   "%s: wv_receive(): missing EOF flag.\n",
2545 							   dev->name);
2546 
2547 				if ((rbd.rbd_status & RBD_STATUS_F) !=
2548 				    RBD_STATUS_F) printk(KERN_INFO
2549 							 "%s: wv_receive(): missing F flag.\n",
2550 							 dev->name);
2551 #endif				/* DEBUG_RX_ERROR */
2552 
2553 				/* Read the packet and transmit to Linux */
2554 				wv_packet_read(dev, rbd.rbd_bufl,
2555 					       rbd.
2556 					       rbd_status &
2557 					       RBD_STATUS_ACNT);
2558 			}
2559 #ifdef DEBUG_RX_ERROR
2560 			else	/* if frame has no data */
2561 				printk(KERN_INFO
2562 				       "%s: wv_receive(): frame has no data.\n",
2563 				       dev->name);
2564 #endif
2565 		} else {	/* If reception was no successful */
2566 
2567 			lp->stats.rx_errors++;
2568 
2569 #ifdef DEBUG_RX_INFO
2570 			printk(KERN_DEBUG
2571 			       "%s: wv_receive(): frame not received successfully (%X).\n",
2572 			       dev->name, fd.fd_status);
2573 #endif
2574 
2575 #ifdef DEBUG_RX_ERROR
2576 			if ((fd.fd_status & FD_STATUS_S6) != 0)
2577 				printk(KERN_INFO
2578 				       "%s: wv_receive(): no EOF flag.\n",
2579 				       dev->name);
2580 #endif
2581 
2582 			if ((fd.fd_status & FD_STATUS_S7) != 0) {
2583 				lp->stats.rx_length_errors++;
2584 #ifdef DEBUG_RX_FAIL
2585 				printk(KERN_DEBUG
2586 				       "%s: wv_receive(): frame too short.\n",
2587 				       dev->name);
2588 #endif
2589 			}
2590 
2591 			if ((fd.fd_status & FD_STATUS_S8) != 0) {
2592 				lp->stats.rx_over_errors++;
2593 #ifdef DEBUG_RX_FAIL
2594 				printk(KERN_DEBUG
2595 				       "%s: wv_receive(): rx DMA overrun.\n",
2596 				       dev->name);
2597 #endif
2598 			}
2599 
2600 			if ((fd.fd_status & FD_STATUS_S9) != 0) {
2601 				lp->stats.rx_fifo_errors++;
2602 #ifdef DEBUG_RX_FAIL
2603 				printk(KERN_DEBUG
2604 				       "%s: wv_receive(): ran out of resources.\n",
2605 				       dev->name);
2606 #endif
2607 			}
2608 
2609 			if ((fd.fd_status & FD_STATUS_S10) != 0) {
2610 				lp->stats.rx_frame_errors++;
2611 #ifdef DEBUG_RX_FAIL
2612 				printk(KERN_DEBUG
2613 				       "%s: wv_receive(): alignment error.\n",
2614 				       dev->name);
2615 #endif
2616 			}
2617 
2618 			if ((fd.fd_status & FD_STATUS_S11) != 0) {
2619 				lp->stats.rx_crc_errors++;
2620 #ifdef DEBUG_RX_FAIL
2621 				printk(KERN_DEBUG
2622 				       "%s: wv_receive(): CRC error.\n",
2623 				       dev->name);
2624 #endif
2625 			}
2626 		}
2627 
2628 		fd.fd_status = 0;
2629 		obram_write(ioaddr, fdoff(lp->rx_head, fd_status),
2630 			    (unsigned char *) &fd.fd_status,
2631 			    sizeof(fd.fd_status));
2632 
2633 		fd.fd_command = FD_COMMAND_EL;
2634 		obram_write(ioaddr, fdoff(lp->rx_head, fd_command),
2635 			    (unsigned char *) &fd.fd_command,
2636 			    sizeof(fd.fd_command));
2637 
2638 		fd.fd_command = 0;
2639 		obram_write(ioaddr, fdoff(lp->rx_last, fd_command),
2640 			    (unsigned char *) &fd.fd_command,
2641 			    sizeof(fd.fd_command));
2642 
2643 		lp->rx_last = lp->rx_head;
2644 		lp->rx_head = fd.fd_link_offset;
2645 	}			/* for(;;) -> loop on all frames */
2646 
2647 #ifdef DEBUG_RX_INFO
2648 	if (nreaped > 1)
2649 		printk(KERN_DEBUG "%s: wv_receive(): reaped %d\n",
2650 		       dev->name, nreaped);
2651 #endif
2652 #ifdef DEBUG_RX_TRACE
2653 	printk(KERN_DEBUG "%s: <-wv_receive()\n", dev->name);
2654 #endif
2655 }
2656 
2657 /*********************** PACKET TRANSMISSION ***********************/
2658 /*
2659  * This part deals with sending packets through the WaveLAN.
2660  *
2661  */
2662 
2663 /*------------------------------------------------------------------*/
2664 /*
2665  * This routine fills in the appropriate registers and memory
2666  * locations on the WaveLAN card and starts the card off on
2667  * the transmit.
2668  *
2669  * The principle:
2670  * Each block contains a transmit command, a NOP command,
2671  * a transmit block descriptor and a buffer.
2672  * The CU read the transmit block which point to the tbd,
2673  * read the tbd and the content of the buffer.
2674  * When it has finish with it, it goes to the next command
2675  * which in our case is the NOP. The NOP points on itself,
2676  * so the CU stop here.
2677  * When we add the next block, we modify the previous nop
2678  * to make it point on the new tx command.
2679  * Simple, isn't it ?
2680  *
2681  * (called in wavelan_packet_xmit())
2682  */
wv_packet_write(device * dev,void * buf,short length)2683 static inline int wv_packet_write(device * dev, void *buf, short length)
2684 {
2685 	net_local *lp = (net_local *) dev->priv;
2686 	unsigned long ioaddr = dev->base_addr;
2687 	unsigned short txblock;
2688 	unsigned short txpred;
2689 	unsigned short tx_addr;
2690 	unsigned short nop_addr;
2691 	unsigned short tbd_addr;
2692 	unsigned short buf_addr;
2693 	ac_tx_t tx;
2694 	ac_nop_t nop;
2695 	tbd_t tbd;
2696 	int clen = length;
2697 	unsigned long flags;
2698 
2699 #ifdef DEBUG_TX_TRACE
2700 	printk(KERN_DEBUG "%s: ->wv_packet_write(%d)\n", dev->name,
2701 	       length);
2702 #endif
2703 
2704 	/* Do we need some padding? */
2705 	if (clen < ETH_ZLEN)
2706 		clen = ETH_ZLEN;
2707 
2708 	wv_splhi(lp, &flags);
2709 
2710 	/* Check nothing bad has happened */
2711 	if (lp->tx_n_in_use == (NTXBLOCKS - 1)) {
2712 #ifdef DEBUG_TX_ERROR
2713 		printk(KERN_INFO "%s: wv_packet_write(): Tx queue full.\n",
2714 		       dev->name);
2715 #endif
2716 		wv_splx(lp, &flags);
2717 		return 1;
2718 	}
2719 
2720 	/* Calculate addresses of next block and previous block. */
2721 	txblock = lp->tx_first_free;
2722 	txpred = txblock - TXBLOCKZ;
2723 	if (txpred < OFFSET_CU)
2724 		txpred += NTXBLOCKS * TXBLOCKZ;
2725 	lp->tx_first_free += TXBLOCKZ;
2726 	if (lp->tx_first_free >= OFFSET_CU + NTXBLOCKS * TXBLOCKZ)
2727 		lp->tx_first_free -= NTXBLOCKS * TXBLOCKZ;
2728 
2729 	lp->tx_n_in_use++;
2730 
2731 	/* Calculate addresses of the different parts of the block. */
2732 	tx_addr = txblock;
2733 	nop_addr = tx_addr + sizeof(tx);
2734 	tbd_addr = nop_addr + sizeof(nop);
2735 	buf_addr = tbd_addr + sizeof(tbd);
2736 
2737 	/*
2738 	 * Transmit command
2739 	 */
2740 	tx.tx_h.ac_status = 0;
2741 	obram_write(ioaddr, toff(ac_tx_t, tx_addr, tx_h.ac_status),
2742 		    (unsigned char *) &tx.tx_h.ac_status,
2743 		    sizeof(tx.tx_h.ac_status));
2744 
2745 	/*
2746 	 * NOP command
2747 	 */
2748 	nop.nop_h.ac_status = 0;
2749 	obram_write(ioaddr, toff(ac_nop_t, nop_addr, nop_h.ac_status),
2750 		    (unsigned char *) &nop.nop_h.ac_status,
2751 		    sizeof(nop.nop_h.ac_status));
2752 	nop.nop_h.ac_link = nop_addr;
2753 	obram_write(ioaddr, toff(ac_nop_t, nop_addr, nop_h.ac_link),
2754 		    (unsigned char *) &nop.nop_h.ac_link,
2755 		    sizeof(nop.nop_h.ac_link));
2756 
2757 	/*
2758 	 * Transmit buffer descriptor
2759 	 */
2760 	tbd.tbd_status = TBD_STATUS_EOF | (TBD_STATUS_ACNT & clen);
2761 	tbd.tbd_next_bd_offset = I82586NULL;
2762 	tbd.tbd_bufl = buf_addr;
2763 	tbd.tbd_bufh = 0;
2764 	obram_write(ioaddr, tbd_addr, (unsigned char *) &tbd, sizeof(tbd));
2765 
2766 	/*
2767 	 * Data
2768 	 */
2769 	obram_write(ioaddr, buf_addr, buf, length);
2770 
2771 	/*
2772 	 * Overwrite the predecessor NOP link
2773 	 * so that it points to this txblock.
2774 	 */
2775 	nop_addr = txpred + sizeof(tx);
2776 	nop.nop_h.ac_status = 0;
2777 	obram_write(ioaddr, toff(ac_nop_t, nop_addr, nop_h.ac_status),
2778 		    (unsigned char *) &nop.nop_h.ac_status,
2779 		    sizeof(nop.nop_h.ac_status));
2780 	nop.nop_h.ac_link = txblock;
2781 	obram_write(ioaddr, toff(ac_nop_t, nop_addr, nop_h.ac_link),
2782 		    (unsigned char *) &nop.nop_h.ac_link,
2783 		    sizeof(nop.nop_h.ac_link));
2784 
2785 	/* Make sure the watchdog will keep quiet for a while */
2786 	dev->trans_start = jiffies;
2787 
2788 	/* Keep stats up to date. */
2789 	lp->stats.tx_bytes += length;
2790 
2791 	if (lp->tx_first_in_use == I82586NULL)
2792 		lp->tx_first_in_use = txblock;
2793 
2794 	if (lp->tx_n_in_use < NTXBLOCKS - 1)
2795 		netif_wake_queue(dev);
2796 
2797 	wv_splx(lp, &flags);
2798 
2799 #ifdef DEBUG_TX_INFO
2800 	wv_packet_info((u8 *) buf, length, dev->name,
2801 		       "wv_packet_write");
2802 #endif				/* DEBUG_TX_INFO */
2803 
2804 #ifdef DEBUG_TX_TRACE
2805 	printk(KERN_DEBUG "%s: <-wv_packet_write()\n", dev->name);
2806 #endif
2807 
2808 	return 0;
2809 }
2810 
2811 /*------------------------------------------------------------------*/
2812 /*
2813  * This routine is called when we want to send a packet (NET3 callback)
2814  * In this routine, we check if the harware is ready to accept
2815  * the packet.  We also prevent reentrance.  Then we call the function
2816  * to send the packet.
2817  */
wavelan_packet_xmit(struct sk_buff * skb,device * dev)2818 static int wavelan_packet_xmit(struct sk_buff *skb, device * dev)
2819 {
2820 	net_local *lp = (net_local *) dev->priv;
2821 	unsigned long flags;
2822 
2823 #ifdef DEBUG_TX_TRACE
2824 	printk(KERN_DEBUG "%s: ->wavelan_packet_xmit(0x%X)\n", dev->name,
2825 	       (unsigned) skb);
2826 #endif
2827 
2828 	if(skb->len < ETH_ZLEN)
2829 	{
2830 		skb = skb_padto(skb, ETH_ZLEN);
2831 		if(skb == NULL)
2832 			return 0;
2833 	}
2834 
2835 	/*
2836 	 * Block a timer-based transmit from overlapping.
2837 	 * In other words, prevent reentering this routine.
2838 	 */
2839 	netif_stop_queue(dev);
2840 
2841 	/* If somebody has asked to reconfigure the controller,
2842 	 * we can do it now.
2843 	 */
2844 	if (lp->reconfig_82586) {
2845 		wv_splhi(lp, &flags);
2846 		wv_82586_config(dev);
2847 		wv_splx(lp, &flags);
2848 		/* Check that we can continue */
2849 		if (lp->tx_n_in_use == (NTXBLOCKS - 1))
2850 			return 1;
2851 	}
2852 #ifdef DEBUG_TX_ERROR
2853 	if (skb->next)
2854 		printk(KERN_INFO "skb has next\n");
2855 #endif
2856 
2857 	/* Write packet on the card */
2858 	if(wv_packet_write(dev, skb->data, skb->len))
2859 		return 1;	/* We failed */
2860 
2861 	dev_kfree_skb(skb);
2862 
2863 #ifdef DEBUG_TX_TRACE
2864 	printk(KERN_DEBUG "%s: <-wavelan_packet_xmit()\n", dev->name);
2865 #endif
2866 	return 0;
2867 }
2868 
2869 /*********************** HARDWARE CONFIGURATION ***********************/
2870 /*
2871  * This part does the real job of starting and configuring the hardware.
2872  */
2873 
2874 /*--------------------------------------------------------------------*/
2875 /*
2876  * Routine to initialize the Modem Management Controller.
2877  * (called by wv_hw_reset())
2878  */
wv_mmc_init(device * dev)2879 static inline int wv_mmc_init(device * dev)
2880 {
2881 	unsigned long ioaddr = dev->base_addr;
2882 	net_local *lp = (net_local *) dev->priv;
2883 	psa_t psa;
2884 	mmw_t m;
2885 	int configured;
2886 
2887 #ifdef DEBUG_CONFIG_TRACE
2888 	printk(KERN_DEBUG "%s: ->wv_mmc_init()\n", dev->name);
2889 #endif
2890 
2891 	/* Read the parameter storage area. */
2892 	psa_read(ioaddr, lp->hacr, 0, (unsigned char *) &psa, sizeof(psa));
2893 
2894 #ifdef USE_PSA_CONFIG
2895 	configured = psa.psa_conf_status & 1;
2896 #else
2897 	configured = 0;
2898 #endif
2899 
2900 	/* Is the PSA is not configured */
2901 	if (!configured) {
2902 		/* User will be able to configure NWID later (with iwconfig). */
2903 		psa.psa_nwid[0] = 0;
2904 		psa.psa_nwid[1] = 0;
2905 
2906 		/* no NWID checking since NWID is not set */
2907 		psa.psa_nwid_select = 0;
2908 
2909 		/* Disable encryption */
2910 		psa.psa_encryption_select = 0;
2911 
2912 		/* Set to standard values:
2913 		 * 0x04 for AT,
2914 		 * 0x01 for MCA,
2915 		 * 0x04 for PCMCIA and 2.00 card (AT&T 407-024689/E document)
2916 		 */
2917 		if (psa.psa_comp_number & 1)
2918 			psa.psa_thr_pre_set = 0x01;
2919 		else
2920 			psa.psa_thr_pre_set = 0x04;
2921 		psa.psa_quality_thr = 0x03;
2922 
2923 		/* It is configured */
2924 		psa.psa_conf_status |= 1;
2925 
2926 #ifdef USE_PSA_CONFIG
2927 		/* Write the psa. */
2928 		psa_write(ioaddr, lp->hacr,
2929 			  (char *) psa.psa_nwid - (char *) &psa,
2930 			  (unsigned char *) psa.psa_nwid, 4);
2931 		psa_write(ioaddr, lp->hacr,
2932 			  (char *) &psa.psa_thr_pre_set - (char *) &psa,
2933 			  (unsigned char *) &psa.psa_thr_pre_set, 1);
2934 		psa_write(ioaddr, lp->hacr,
2935 			  (char *) &psa.psa_quality_thr - (char *) &psa,
2936 			  (unsigned char *) &psa.psa_quality_thr, 1);
2937 		psa_write(ioaddr, lp->hacr,
2938 			  (char *) &psa.psa_conf_status - (char *) &psa,
2939 			  (unsigned char *) &psa.psa_conf_status, 1);
2940 		/* update the Wavelan checksum */
2941 		update_psa_checksum(dev, ioaddr, lp->hacr);
2942 #endif
2943 	}
2944 
2945 	/* Zero the mmc structure. */
2946 	memset(&m, 0x00, sizeof(m));
2947 
2948 	/* Copy PSA info to the mmc. */
2949 	m.mmw_netw_id_l = psa.psa_nwid[1];
2950 	m.mmw_netw_id_h = psa.psa_nwid[0];
2951 
2952 	if (psa.psa_nwid_select & 1)
2953 		m.mmw_loopt_sel = 0x00;
2954 	else
2955 		m.mmw_loopt_sel = MMW_LOOPT_SEL_DIS_NWID;
2956 
2957 	memcpy(&m.mmw_encr_key, &psa.psa_encryption_key,
2958 	       sizeof(m.mmw_encr_key));
2959 
2960 	if (psa.psa_encryption_select)
2961 		m.mmw_encr_enable =
2962 		    MMW_ENCR_ENABLE_EN | MMW_ENCR_ENABLE_MODE;
2963 	else
2964 		m.mmw_encr_enable = 0;
2965 
2966 	m.mmw_thr_pre_set = psa.psa_thr_pre_set & 0x3F;
2967 	m.mmw_quality_thr = psa.psa_quality_thr & 0x0F;
2968 
2969 	/*
2970 	 * Set default modem control parameters.
2971 	 * See NCR document 407-0024326 Rev. A.
2972 	 */
2973 	m.mmw_jabber_enable = 0x01;
2974 	m.mmw_freeze = 0;
2975 	m.mmw_anten_sel = MMW_ANTEN_SEL_ALG_EN;
2976 	m.mmw_ifs = 0x20;
2977 	m.mmw_mod_delay = 0x04;
2978 	m.mmw_jam_time = 0x38;
2979 
2980 	m.mmw_des_io_invert = 0;
2981 	m.mmw_decay_prm = 0;
2982 	m.mmw_decay_updat_prm = 0;
2983 
2984 	/* Write all info to MMC. */
2985 	mmc_write(ioaddr, 0, (u8 *) & m, sizeof(m));
2986 
2987 	/* The following code starts the modem of the 2.00 frequency
2988 	 * selectable cards at power on.  It's not strictly needed for the
2989 	 * following boots.
2990 	 * The original patch was by Joe Finney for the PCMCIA driver, but
2991 	 * I've cleaned it up a bit and added documentation.
2992 	 * Thanks to Loeke Brederveld from Lucent for the info.
2993 	 */
2994 
2995 	/* Attempt to recognise 2.00 cards (2.4 GHz frequency selectable)
2996 	 * Does it work for everybody, especially old cards? */
2997 	/* Note: WFREQSEL verifies that it is able to read a sensible
2998 	 * frequency from EEPROM (address 0x00) and that MMR_FEE_STATUS_ID
2999 	 * is 0xA (Xilinx version) or 0xB (Ariadne version).
3000 	 * My test is more crude but does work. */
3001 	if (!(mmc_in(ioaddr, mmroff(0, mmr_fee_status)) &
3002 	      (MMR_FEE_STATUS_DWLD | MMR_FEE_STATUS_BUSY))) {
3003 		/* We must download the frequency parameters to the
3004 		 * synthesizers (from the EEPROM - area 1)
3005 		 * Note: as the EEPROM is automatically decremented, we set the end
3006 		 * if the area... */
3007 		m.mmw_fee_addr = 0x0F;
3008 		m.mmw_fee_ctrl = MMW_FEE_CTRL_READ | MMW_FEE_CTRL_DWLD;
3009 		mmc_write(ioaddr, (char *) &m.mmw_fee_ctrl - (char *) &m,
3010 			  (unsigned char *) &m.mmw_fee_ctrl, 2);
3011 
3012 		/* Wait until the download is finished. */
3013 		fee_wait(ioaddr, 100, 100);
3014 
3015 #ifdef DEBUG_CONFIG_INFO
3016 		/* The frequency was in the last word downloaded. */
3017 		mmc_read(ioaddr, (char *) &m.mmw_fee_data_l - (char *) &m,
3018 			 (unsigned char *) &m.mmw_fee_data_l, 2);
3019 
3020 		/* Print some info for the user. */
3021 		printk(KERN_DEBUG
3022 		       "%s: WaveLAN 2.00 recognised (frequency select).  Current frequency = %ld\n",
3023 		       dev->name,
3024 		       ((m.
3025 			 mmw_fee_data_h << 4) | (m.mmw_fee_data_l >> 4)) *
3026 		       5 / 2 + 24000L);
3027 #endif
3028 
3029 		/* We must now download the power adjust value (gain) to
3030 		 * the synthesizers (from the EEPROM - area 7 - DAC). */
3031 		m.mmw_fee_addr = 0x61;
3032 		m.mmw_fee_ctrl = MMW_FEE_CTRL_READ | MMW_FEE_CTRL_DWLD;
3033 		mmc_write(ioaddr, (char *) &m.mmw_fee_ctrl - (char *) &m,
3034 			  (unsigned char *) &m.mmw_fee_ctrl, 2);
3035 
3036 		/* Wait until the download is finished. */
3037 	}
3038 	/* if 2.00 card */
3039 #ifdef DEBUG_CONFIG_TRACE
3040 	printk(KERN_DEBUG "%s: <-wv_mmc_init()\n", dev->name);
3041 #endif
3042 	return 0;
3043 }
3044 
3045 /*------------------------------------------------------------------*/
3046 /*
3047  * Construct the fd and rbd structures.
3048  * Start the receive unit.
3049  * (called by wv_hw_reset())
3050  */
wv_ru_start(device * dev)3051 static inline int wv_ru_start(device * dev)
3052 {
3053 	net_local *lp = (net_local *) dev->priv;
3054 	unsigned long ioaddr = dev->base_addr;
3055 	u16 scb_cs;
3056 	fd_t fd;
3057 	rbd_t rbd;
3058 	u16 rx;
3059 	u16 rx_next;
3060 	int i;
3061 
3062 #ifdef DEBUG_CONFIG_TRACE
3063 	printk(KERN_DEBUG "%s: ->wv_ru_start()\n", dev->name);
3064 #endif
3065 
3066 	obram_read(ioaddr, scboff(OFFSET_SCB, scb_status),
3067 		   (unsigned char *) &scb_cs, sizeof(scb_cs));
3068 	if ((scb_cs & SCB_ST_RUS) == SCB_ST_RUS_RDY)
3069 		return 0;
3070 
3071 	lp->rx_head = OFFSET_RU;
3072 
3073 	for (i = 0, rx = lp->rx_head; i < NRXBLOCKS; i++, rx = rx_next) {
3074 		rx_next =
3075 		    (i == NRXBLOCKS - 1) ? lp->rx_head : rx + RXBLOCKZ;
3076 
3077 		fd.fd_status = 0;
3078 		fd.fd_command = (i == NRXBLOCKS - 1) ? FD_COMMAND_EL : 0;
3079 		fd.fd_link_offset = rx_next;
3080 		fd.fd_rbd_offset = rx + sizeof(fd);
3081 		obram_write(ioaddr, rx, (unsigned char *) &fd, sizeof(fd));
3082 
3083 		rbd.rbd_status = 0;
3084 		rbd.rbd_next_rbd_offset = I82586NULL;
3085 		rbd.rbd_bufl = rx + sizeof(fd) + sizeof(rbd);
3086 		rbd.rbd_bufh = 0;
3087 		rbd.rbd_el_size = RBD_EL | (RBD_SIZE & MAXDATAZ);
3088 		obram_write(ioaddr, rx + sizeof(fd),
3089 			    (unsigned char *) &rbd, sizeof(rbd));
3090 
3091 		lp->rx_last = rx;
3092 	}
3093 
3094 	obram_write(ioaddr, scboff(OFFSET_SCB, scb_rfa_offset),
3095 		    (unsigned char *) &lp->rx_head, sizeof(lp->rx_head));
3096 
3097 	scb_cs = SCB_CMD_RUC_GO;
3098 	obram_write(ioaddr, scboff(OFFSET_SCB, scb_command),
3099 		    (unsigned char *) &scb_cs, sizeof(scb_cs));
3100 
3101 	set_chan_attn(ioaddr, lp->hacr);
3102 
3103 	for (i = 1000; i > 0; i--) {
3104 		obram_read(ioaddr, scboff(OFFSET_SCB, scb_command),
3105 			   (unsigned char *) &scb_cs, sizeof(scb_cs));
3106 		if (scb_cs == 0)
3107 			break;
3108 
3109 		udelay(10);
3110 	}
3111 
3112 	if (i <= 0) {
3113 #ifdef DEBUG_CONFIG_ERROR
3114 		printk(KERN_INFO
3115 		       "%s: wavelan_ru_start(): board not accepting command.\n",
3116 		       dev->name);
3117 #endif
3118 		return -1;
3119 	}
3120 #ifdef DEBUG_CONFIG_TRACE
3121 	printk(KERN_DEBUG "%s: <-wv_ru_start()\n", dev->name);
3122 #endif
3123 	return 0;
3124 }
3125 
3126 /*------------------------------------------------------------------*/
3127 /*
3128  * Initialise the transmit blocks.
3129  * Start the command unit executing the NOP
3130  * self-loop of the first transmit block.
3131  *
3132  * Here we create the list of send buffers used to transmit packets
3133  * between the PC and the command unit. For each buffer, we create a
3134  * buffer descriptor (pointing on the buffer), a transmit command
3135  * (pointing to the buffer descriptor) and a NOP command.
3136  * The transmit command is linked to the NOP, and the NOP to itself.
3137  * When we will have finished executing the transmit command, we will
3138  * then loop on the NOP. By releasing the NOP link to a new command,
3139  * we may send another buffer.
3140  *
3141  * (called by wv_hw_reset())
3142  */
wv_cu_start(device * dev)3143 static inline int wv_cu_start(device * dev)
3144 {
3145 	net_local *lp = (net_local *) dev->priv;
3146 	unsigned long ioaddr = dev->base_addr;
3147 	int i;
3148 	u16 txblock;
3149 	u16 first_nop;
3150 	u16 scb_cs;
3151 
3152 #ifdef DEBUG_CONFIG_TRACE
3153 	printk(KERN_DEBUG "%s: ->wv_cu_start()\n", dev->name);
3154 #endif
3155 
3156 	lp->tx_first_free = OFFSET_CU;
3157 	lp->tx_first_in_use = I82586NULL;
3158 
3159 	for (i = 0, txblock = OFFSET_CU;
3160 	     i < NTXBLOCKS; i++, txblock += TXBLOCKZ) {
3161 		ac_tx_t tx;
3162 		ac_nop_t nop;
3163 		tbd_t tbd;
3164 		unsigned short tx_addr;
3165 		unsigned short nop_addr;
3166 		unsigned short tbd_addr;
3167 		unsigned short buf_addr;
3168 
3169 		tx_addr = txblock;
3170 		nop_addr = tx_addr + sizeof(tx);
3171 		tbd_addr = nop_addr + sizeof(nop);
3172 		buf_addr = tbd_addr + sizeof(tbd);
3173 
3174 		tx.tx_h.ac_status = 0;
3175 		tx.tx_h.ac_command = acmd_transmit | AC_CFLD_I;
3176 		tx.tx_h.ac_link = nop_addr;
3177 		tx.tx_tbd_offset = tbd_addr;
3178 		obram_write(ioaddr, tx_addr, (unsigned char *) &tx,
3179 			    sizeof(tx));
3180 
3181 		nop.nop_h.ac_status = 0;
3182 		nop.nop_h.ac_command = acmd_nop;
3183 		nop.nop_h.ac_link = nop_addr;
3184 		obram_write(ioaddr, nop_addr, (unsigned char *) &nop,
3185 			    sizeof(nop));
3186 
3187 		tbd.tbd_status = TBD_STATUS_EOF;
3188 		tbd.tbd_next_bd_offset = I82586NULL;
3189 		tbd.tbd_bufl = buf_addr;
3190 		tbd.tbd_bufh = 0;
3191 		obram_write(ioaddr, tbd_addr, (unsigned char *) &tbd,
3192 			    sizeof(tbd));
3193 	}
3194 
3195 	first_nop =
3196 	    OFFSET_CU + (NTXBLOCKS - 1) * TXBLOCKZ + sizeof(ac_tx_t);
3197 	obram_write(ioaddr, scboff(OFFSET_SCB, scb_cbl_offset),
3198 		    (unsigned char *) &first_nop, sizeof(first_nop));
3199 
3200 	scb_cs = SCB_CMD_CUC_GO;
3201 	obram_write(ioaddr, scboff(OFFSET_SCB, scb_command),
3202 		    (unsigned char *) &scb_cs, sizeof(scb_cs));
3203 
3204 	set_chan_attn(ioaddr, lp->hacr);
3205 
3206 	for (i = 1000; i > 0; i--) {
3207 		obram_read(ioaddr, scboff(OFFSET_SCB, scb_command),
3208 			   (unsigned char *) &scb_cs, sizeof(scb_cs));
3209 		if (scb_cs == 0)
3210 			break;
3211 
3212 		udelay(10);
3213 	}
3214 
3215 	if (i <= 0) {
3216 #ifdef DEBUG_CONFIG_ERROR
3217 		printk(KERN_INFO
3218 		       "%s: wavelan_cu_start(): board not accepting command.\n",
3219 		       dev->name);
3220 #endif
3221 		return -1;
3222 	}
3223 
3224 	lp->tx_n_in_use = 0;
3225 	netif_start_queue(dev);
3226 #ifdef DEBUG_CONFIG_TRACE
3227 	printk(KERN_DEBUG "%s: <-wv_cu_start()\n", dev->name);
3228 #endif
3229 	return 0;
3230 }
3231 
3232 /*------------------------------------------------------------------*/
3233 /*
3234  * This routine does a standard configuration of the WaveLAN
3235  * controller (i82586).
3236  *
3237  * It initialises the scp, iscp and scb structure
3238  * The first two are just pointers to the next.
3239  * The last one is used for basic configuration and for basic
3240  * communication (interrupt status).
3241  *
3242  * (called by wv_hw_reset())
3243  */
wv_82586_start(device * dev)3244 static inline int wv_82586_start(device * dev)
3245 {
3246 	net_local *lp = (net_local *) dev->priv;
3247 	unsigned long ioaddr = dev->base_addr;
3248 	scp_t scp;		/* system configuration pointer */
3249 	iscp_t iscp;		/* intermediate scp */
3250 	scb_t scb;		/* system control block */
3251 	ach_t cb;		/* Action command header */
3252 	u8 zeroes[512];
3253 	int i;
3254 
3255 #ifdef DEBUG_CONFIG_TRACE
3256 	printk(KERN_DEBUG "%s: ->wv_82586_start()\n", dev->name);
3257 #endif
3258 
3259 	/*
3260 	 * Clear the onboard RAM.
3261 	 */
3262 	memset(&zeroes[0], 0x00, sizeof(zeroes));
3263 	for (i = 0; i < I82586_MEMZ; i += sizeof(zeroes))
3264 		obram_write(ioaddr, i, &zeroes[0], sizeof(zeroes));
3265 
3266 	/*
3267 	 * Construct the command unit structures:
3268 	 * scp, iscp, scb, cb.
3269 	 */
3270 	memset(&scp, 0x00, sizeof(scp));
3271 	scp.scp_sysbus = SCP_SY_16BBUS;
3272 	scp.scp_iscpl = OFFSET_ISCP;
3273 	obram_write(ioaddr, OFFSET_SCP, (unsigned char *) &scp,
3274 		    sizeof(scp));
3275 
3276 	memset(&iscp, 0x00, sizeof(iscp));
3277 	iscp.iscp_busy = 1;
3278 	iscp.iscp_offset = OFFSET_SCB;
3279 	obram_write(ioaddr, OFFSET_ISCP, (unsigned char *) &iscp,
3280 		    sizeof(iscp));
3281 
3282 	/* Our first command is to reset the i82586. */
3283 	memset(&scb, 0x00, sizeof(scb));
3284 	scb.scb_command = SCB_CMD_RESET;
3285 	scb.scb_cbl_offset = OFFSET_CU;
3286 	scb.scb_rfa_offset = OFFSET_RU;
3287 	obram_write(ioaddr, OFFSET_SCB, (unsigned char *) &scb,
3288 		    sizeof(scb));
3289 
3290 	set_chan_attn(ioaddr, lp->hacr);
3291 
3292 	/* Wait for command to finish. */
3293 	for (i = 1000; i > 0; i--) {
3294 		obram_read(ioaddr, OFFSET_ISCP, (unsigned char *) &iscp,
3295 			   sizeof(iscp));
3296 
3297 		if (iscp.iscp_busy == (unsigned short) 0)
3298 			break;
3299 
3300 		udelay(10);
3301 	}
3302 
3303 	if (i <= 0) {
3304 #ifdef DEBUG_CONFIG_ERROR
3305 		printk(KERN_INFO
3306 		       "%s: wv_82586_start(): iscp_busy timeout.\n",
3307 		       dev->name);
3308 #endif
3309 		return -1;
3310 	}
3311 
3312 	/* Check command completion. */
3313 	for (i = 15; i > 0; i--) {
3314 		obram_read(ioaddr, OFFSET_SCB, (unsigned char *) &scb,
3315 			   sizeof(scb));
3316 
3317 		if (scb.scb_status == (SCB_ST_CX | SCB_ST_CNA))
3318 			break;
3319 
3320 		udelay(10);
3321 	}
3322 
3323 	if (i <= 0) {
3324 #ifdef DEBUG_CONFIG_ERROR
3325 		printk(KERN_INFO
3326 		       "%s: wv_82586_start(): status: expected 0x%02x, got 0x%02x.\n",
3327 		       dev->name, SCB_ST_CX | SCB_ST_CNA, scb.scb_status);
3328 #endif
3329 		return -1;
3330 	}
3331 
3332 	wv_ack(dev);
3333 
3334 	/* Set the action command header. */
3335 	memset(&cb, 0x00, sizeof(cb));
3336 	cb.ac_command = AC_CFLD_EL | (AC_CFLD_CMD & acmd_diagnose);
3337 	cb.ac_link = OFFSET_CU;
3338 	obram_write(ioaddr, OFFSET_CU, (unsigned char *) &cb, sizeof(cb));
3339 
3340 	if (wv_synchronous_cmd(dev, "diag()") == -1)
3341 		return -1;
3342 
3343 	obram_read(ioaddr, OFFSET_CU, (unsigned char *) &cb, sizeof(cb));
3344 	if (cb.ac_status & AC_SFLD_FAIL) {
3345 #ifdef DEBUG_CONFIG_ERROR
3346 		printk(KERN_INFO
3347 		       "%s: wv_82586_start(): i82586 Self Test failed.\n",
3348 		       dev->name);
3349 #endif
3350 		return -1;
3351 	}
3352 #ifdef DEBUG_I82586_SHOW
3353 	wv_scb_show(ioaddr);
3354 #endif
3355 
3356 #ifdef DEBUG_CONFIG_TRACE
3357 	printk(KERN_DEBUG "%s: <-wv_82586_start()\n", dev->name);
3358 #endif
3359 	return 0;
3360 }
3361 
3362 /*------------------------------------------------------------------*/
3363 /*
3364  * This routine does a standard configuration of the WaveLAN
3365  * controller (i82586).
3366  *
3367  * This routine is a violent hack. We use the first free transmit block
3368  * to make our configuration. In the buffer area, we create the three
3369  * configuration commands (linked). We make the previous NOP point to
3370  * the beginning of the buffer instead of the tx command. After, we go
3371  * as usual to the NOP command.
3372  * Note that only the last command (mc_set) will generate an interrupt.
3373  *
3374  * (called by wv_hw_reset(), wv_82586_reconfig(), wavelan_packet_xmit())
3375  */
wv_82586_config(device * dev)3376 static void wv_82586_config(device * dev)
3377 {
3378 	net_local *lp = (net_local *) dev->priv;
3379 	unsigned long ioaddr = dev->base_addr;
3380 	unsigned short txblock;
3381 	unsigned short txpred;
3382 	unsigned short tx_addr;
3383 	unsigned short nop_addr;
3384 	unsigned short tbd_addr;
3385 	unsigned short cfg_addr;
3386 	unsigned short ias_addr;
3387 	unsigned short mcs_addr;
3388 	ac_tx_t tx;
3389 	ac_nop_t nop;
3390 	ac_cfg_t cfg;		/* Configure action */
3391 	ac_ias_t ias;		/* IA-setup action */
3392 	ac_mcs_t mcs;		/* Multicast setup */
3393 	struct dev_mc_list *dmi;
3394 
3395 #ifdef DEBUG_CONFIG_TRACE
3396 	printk(KERN_DEBUG "%s: ->wv_82586_config()\n", dev->name);
3397 #endif
3398 
3399 	/* Check nothing bad has happened */
3400 	if (lp->tx_n_in_use == (NTXBLOCKS - 1)) {
3401 #ifdef DEBUG_CONFIG_ERROR
3402 		printk(KERN_INFO "%s: wv_82586_config(): Tx queue full.\n",
3403 		       dev->name);
3404 #endif
3405 		return;
3406 	}
3407 
3408 	/* Calculate addresses of next block and previous block. */
3409 	txblock = lp->tx_first_free;
3410 	txpred = txblock - TXBLOCKZ;
3411 	if (txpred < OFFSET_CU)
3412 		txpred += NTXBLOCKS * TXBLOCKZ;
3413 	lp->tx_first_free += TXBLOCKZ;
3414 	if (lp->tx_first_free >= OFFSET_CU + NTXBLOCKS * TXBLOCKZ)
3415 		lp->tx_first_free -= NTXBLOCKS * TXBLOCKZ;
3416 
3417 	lp->tx_n_in_use++;
3418 
3419 	/* Calculate addresses of the different parts of the block. */
3420 	tx_addr = txblock;
3421 	nop_addr = tx_addr + sizeof(tx);
3422 	tbd_addr = nop_addr + sizeof(nop);
3423 	cfg_addr = tbd_addr + sizeof(tbd_t);	/* beginning of the buffer */
3424 	ias_addr = cfg_addr + sizeof(cfg);
3425 	mcs_addr = ias_addr + sizeof(ias);
3426 
3427 	/*
3428 	 * Transmit command
3429 	 */
3430 	tx.tx_h.ac_status = 0xFFFF;	/* Fake completion value */
3431 	obram_write(ioaddr, toff(ac_tx_t, tx_addr, tx_h.ac_status),
3432 		    (unsigned char *) &tx.tx_h.ac_status,
3433 		    sizeof(tx.tx_h.ac_status));
3434 
3435 	/*
3436 	 * NOP command
3437 	 */
3438 	nop.nop_h.ac_status = 0;
3439 	obram_write(ioaddr, toff(ac_nop_t, nop_addr, nop_h.ac_status),
3440 		    (unsigned char *) &nop.nop_h.ac_status,
3441 		    sizeof(nop.nop_h.ac_status));
3442 	nop.nop_h.ac_link = nop_addr;
3443 	obram_write(ioaddr, toff(ac_nop_t, nop_addr, nop_h.ac_link),
3444 		    (unsigned char *) &nop.nop_h.ac_link,
3445 		    sizeof(nop.nop_h.ac_link));
3446 
3447 	/* Create a configure action. */
3448 	memset(&cfg, 0x00, sizeof(cfg));
3449 
3450 	/*
3451 	 * For Linux we invert AC_CFG_ALOC() so as to conform
3452 	 * to the way that net packets reach us from above.
3453 	 * (See also ac_tx_t.)
3454 	 *
3455 	 * Updated from Wavelan Manual WCIN085B
3456 	 */
3457 	cfg.cfg_byte_cnt =
3458 	    AC_CFG_BYTE_CNT(sizeof(ac_cfg_t) - sizeof(ach_t));
3459 	cfg.cfg_fifolim = AC_CFG_FIFOLIM(4);
3460 	cfg.cfg_byte8 = AC_CFG_SAV_BF(1) | AC_CFG_SRDY(0);
3461 	cfg.cfg_byte9 = AC_CFG_ELPBCK(0) |
3462 	    AC_CFG_ILPBCK(0) |
3463 	    AC_CFG_PRELEN(AC_CFG_PLEN_2) |
3464 	    AC_CFG_ALOC(1) | AC_CFG_ADDRLEN(WAVELAN_ADDR_SIZE);
3465 	cfg.cfg_byte10 = AC_CFG_BOFMET(1) |
3466 	    AC_CFG_ACR(6) | AC_CFG_LINPRIO(0);
3467 	cfg.cfg_ifs = 0x20;
3468 	cfg.cfg_slotl = 0x0C;
3469 	cfg.cfg_byte13 = AC_CFG_RETRYNUM(15) | AC_CFG_SLTTMHI(0);
3470 	cfg.cfg_byte14 = AC_CFG_FLGPAD(0) |
3471 	    AC_CFG_BTSTF(0) |
3472 	    AC_CFG_CRC16(0) |
3473 	    AC_CFG_NCRC(0) |
3474 	    AC_CFG_TNCRS(1) |
3475 	    AC_CFG_MANCH(0) |
3476 	    AC_CFG_BCDIS(0) | AC_CFG_PRM(lp->promiscuous);
3477 	cfg.cfg_byte15 = AC_CFG_ICDS(0) |
3478 	    AC_CFG_CDTF(0) | AC_CFG_ICSS(0) | AC_CFG_CSTF(0);
3479 /*
3480   cfg.cfg_min_frm_len = AC_CFG_MNFRM(64);
3481 */
3482 	cfg.cfg_min_frm_len = AC_CFG_MNFRM(8);
3483 
3484 	cfg.cfg_h.ac_command = (AC_CFLD_CMD & acmd_configure);
3485 	cfg.cfg_h.ac_link = ias_addr;
3486 	obram_write(ioaddr, cfg_addr, (unsigned char *) &cfg, sizeof(cfg));
3487 
3488 	/* Set up the MAC address */
3489 	memset(&ias, 0x00, sizeof(ias));
3490 	ias.ias_h.ac_command = (AC_CFLD_CMD & acmd_ia_setup);
3491 	ias.ias_h.ac_link = mcs_addr;
3492 	memcpy(&ias.ias_addr[0], (unsigned char *) &dev->dev_addr[0],
3493 	       sizeof(ias.ias_addr));
3494 	obram_write(ioaddr, ias_addr, (unsigned char *) &ias, sizeof(ias));
3495 
3496 	/* Initialize adapter's Ethernet multicast addresses */
3497 	memset(&mcs, 0x00, sizeof(mcs));
3498 	mcs.mcs_h.ac_command = AC_CFLD_I | (AC_CFLD_CMD & acmd_mc_setup);
3499 	mcs.mcs_h.ac_link = nop_addr;
3500 	mcs.mcs_cnt = WAVELAN_ADDR_SIZE * lp->mc_count;
3501 	obram_write(ioaddr, mcs_addr, (unsigned char *) &mcs, sizeof(mcs));
3502 
3503 	/* Any address to set? */
3504 	if (lp->mc_count) {
3505 		for (dmi = dev->mc_list; dmi; dmi = dmi->next)
3506 			outsw(PIOP1(ioaddr), (u16 *) dmi->dmi_addr,
3507 			      WAVELAN_ADDR_SIZE >> 1);
3508 
3509 #ifdef DEBUG_CONFIG_INFO
3510 		printk(KERN_DEBUG
3511 		       "%s: wv_82586_config(): set %d multicast addresses:\n",
3512 		       dev->name, lp->mc_count);
3513 		for (dmi = dev->mc_list; dmi; dmi = dmi->next)
3514 			printk(KERN_DEBUG
3515 			       " %02x:%02x:%02x:%02x:%02x:%02x\n",
3516 			       dmi->dmi_addr[0], dmi->dmi_addr[1],
3517 			       dmi->dmi_addr[2], dmi->dmi_addr[3],
3518 			       dmi->dmi_addr[4], dmi->dmi_addr[5]);
3519 #endif
3520 	}
3521 
3522 	/*
3523 	 * Overwrite the predecessor NOP link
3524 	 * so that it points to the configure action.
3525 	 */
3526 	nop_addr = txpred + sizeof(tx);
3527 	nop.nop_h.ac_status = 0;
3528 	obram_write(ioaddr, toff(ac_nop_t, nop_addr, nop_h.ac_status),
3529 		    (unsigned char *) &nop.nop_h.ac_status,
3530 		    sizeof(nop.nop_h.ac_status));
3531 	nop.nop_h.ac_link = cfg_addr;
3532 	obram_write(ioaddr, toff(ac_nop_t, nop_addr, nop_h.ac_link),
3533 		    (unsigned char *) &nop.nop_h.ac_link,
3534 		    sizeof(nop.nop_h.ac_link));
3535 
3536 	/* Job done, clear the flag */
3537 	lp->reconfig_82586 = 0;
3538 
3539 	if (lp->tx_first_in_use == I82586NULL)
3540 		lp->tx_first_in_use = txblock;
3541 
3542 	if (lp->tx_n_in_use == (NTXBLOCKS - 1))
3543 		netif_stop_queue(dev);
3544 
3545 #ifdef DEBUG_CONFIG_TRACE
3546 	printk(KERN_DEBUG "%s: <-wv_82586_config()\n", dev->name);
3547 #endif
3548 }
3549 
3550 /*------------------------------------------------------------------*/
3551 /*
3552  * This routine, called by wavelan_close(), gracefully stops the
3553  * WaveLAN controller (i82586).
3554  * (called by wavelan_close())
3555  */
wv_82586_stop(device * dev)3556 static inline void wv_82586_stop(device * dev)
3557 {
3558 	net_local *lp = (net_local *) dev->priv;
3559 	unsigned long ioaddr = dev->base_addr;
3560 	u16 scb_cmd;
3561 
3562 #ifdef DEBUG_CONFIG_TRACE
3563 	printk(KERN_DEBUG "%s: ->wv_82586_stop()\n", dev->name);
3564 #endif
3565 
3566 	/* Suspend both command unit and receive unit. */
3567 	scb_cmd =
3568 	    (SCB_CMD_CUC & SCB_CMD_CUC_SUS) | (SCB_CMD_RUC &
3569 					       SCB_CMD_RUC_SUS);
3570 	obram_write(ioaddr, scboff(OFFSET_SCB, scb_command),
3571 		    (unsigned char *) &scb_cmd, sizeof(scb_cmd));
3572 	set_chan_attn(ioaddr, lp->hacr);
3573 
3574 	/* No more interrupts */
3575 	wv_ints_off(dev);
3576 
3577 #ifdef DEBUG_CONFIG_TRACE
3578 	printk(KERN_DEBUG "%s: <-wv_82586_stop()\n", dev->name);
3579 #endif
3580 }
3581 
3582 /*------------------------------------------------------------------*/
3583 /*
3584  * Totally reset the WaveLAN and restart it.
3585  * Performs the following actions:
3586  *	1. A power reset (reset DMA)
3587  *	2. Initialize the radio modem (using wv_mmc_init)
3588  *	3. Reset & Configure LAN controller (using wv_82586_start)
3589  *	4. Start the LAN controller's command unit
3590  *	5. Start the LAN controller's receive unit
3591  * (called by wavelan_interrupt(), wavelan_watchdog() & wavelan_open())
3592  */
wv_hw_reset(device * dev)3593 static int wv_hw_reset(device * dev)
3594 {
3595 	net_local *lp = (net_local *) dev->priv;
3596 	unsigned long ioaddr = dev->base_addr;
3597 
3598 #ifdef DEBUG_CONFIG_TRACE
3599 	printk(KERN_DEBUG "%s: ->wv_hw_reset(dev=0x%x)\n", dev->name,
3600 	       (unsigned int) dev);
3601 #endif
3602 
3603 	/* Increase the number of resets done. */
3604 	lp->nresets++;
3605 
3606 	wv_hacr_reset(ioaddr);
3607 	lp->hacr = HACR_DEFAULT;
3608 
3609 	if ((wv_mmc_init(dev) < 0) || (wv_82586_start(dev) < 0))
3610 		return -1;
3611 
3612 	/* Enable the card to send interrupts. */
3613 	wv_ints_on(dev);
3614 
3615 	/* Start card functions */
3616 	if (wv_cu_start(dev) < 0)
3617 		return -1;
3618 
3619 	/* Setup the controller and parameters */
3620 	wv_82586_config(dev);
3621 
3622 	/* Finish configuration with the receive unit */
3623 	if (wv_ru_start(dev) < 0)
3624 		return -1;
3625 
3626 #ifdef DEBUG_CONFIG_TRACE
3627 	printk(KERN_DEBUG "%s: <-wv_hw_reset()\n", dev->name);
3628 #endif
3629 	return 0;
3630 }
3631 
3632 /*------------------------------------------------------------------*/
3633 /*
3634  * Check if there is a WaveLAN at the specific base address.
3635  * As a side effect, this reads the MAC address.
3636  * (called in wavelan_probe() and init_module())
3637  */
wv_check_ioaddr(unsigned long ioaddr,u8 * mac)3638 static int wv_check_ioaddr(unsigned long ioaddr, u8 * mac)
3639 {
3640 	int i;			/* Loop counter */
3641 
3642 	/* Check if the base address if available. */
3643 	if (check_region(ioaddr, sizeof(ha_t)))
3644 		return -EADDRINUSE;	/* ioaddr already used */
3645 
3646 	/* Reset host interface */
3647 	wv_hacr_reset(ioaddr);
3648 
3649 	/* Read the MAC address from the parameter storage area. */
3650 	psa_read(ioaddr, HACR_DEFAULT, psaoff(0, psa_univ_mac_addr),
3651 		 mac, 6);
3652 
3653 	/*
3654 	 * Check the first three octets of the address for the manufacturer's code.
3655 	 * Note: if this can't find your WaveLAN card, you've got a
3656 	 * non-NCR/AT&T/Lucent ISA card.  See wavelan.p.h for detail on
3657 	 * how to configure your card.
3658 	 */
3659 	for (i = 0; i < (sizeof(MAC_ADDRESSES) / sizeof(char) / 3); i++)
3660 		if ((mac[0] == MAC_ADDRESSES[i][0]) &&
3661 		    (mac[1] == MAC_ADDRESSES[i][1]) &&
3662 		    (mac[2] == MAC_ADDRESSES[i][2]))
3663 			return 0;
3664 
3665 #ifdef DEBUG_CONFIG_INFO
3666 	printk(KERN_WARNING
3667 	       "WaveLAN (0x%3X): your MAC address might be %02X:%02X:%02X.\n",
3668 	       ioaddr, mac[0], mac[1], mac[2]);
3669 #endif
3670 	return -ENODEV;
3671 }
3672 
3673 /************************ INTERRUPT HANDLING ************************/
3674 
3675 /*
3676  * This function is the interrupt handler for the WaveLAN card. This
3677  * routine will be called whenever:
3678  */
wavelan_interrupt(int irq,void * dev_id,struct pt_regs * regs)3679 static void wavelan_interrupt(int irq, void *dev_id, struct pt_regs *regs)
3680 {
3681 	device *dev;
3682 	unsigned long ioaddr;
3683 	net_local *lp;
3684 	u16 hasr;
3685 	u16 status;
3686 	u16 ack_cmd;
3687 
3688 	dev = dev_id;
3689 
3690 #ifdef DEBUG_INTERRUPT_TRACE
3691 	printk(KERN_DEBUG "%s: ->wavelan_interrupt()\n", dev->name);
3692 #endif
3693 
3694 	lp = (net_local *) dev->priv;
3695 	ioaddr = dev->base_addr;
3696 
3697 #ifdef DEBUG_INTERRUPT_INFO
3698 	/* Check state of our spinlock */
3699 	if(spin_is_locked(&lp->spinlock))
3700 		printk(KERN_DEBUG
3701 		       "%s: wavelan_interrupt(): spinlock is already locked !!!\n",
3702 		       dev->name);
3703 #endif
3704 
3705 	/* Prevent reentrancy. We need to do that because we may have
3706 	 * multiple interrupt handler running concurrently.
3707 	 * It is safe because wv_splhi() disables interrupts before acquiring
3708 	 * the spinlock. */
3709 	spin_lock(&lp->spinlock);
3710 
3711 	/* Check modem interrupt */
3712 	if ((hasr = hasr_read(ioaddr)) & HASR_MMC_INTR) {
3713 		u8 dce_status;
3714 
3715 		/*
3716 		 * Interrupt from the modem management controller.
3717 		 * This will clear it -- ignored for now.
3718 		 */
3719 		mmc_read(ioaddr, mmroff(0, mmr_dce_status), &dce_status,
3720 			 sizeof(dce_status));
3721 #ifdef DEBUG_INTERRUPT_ERROR
3722 		printk(KERN_INFO
3723 		       "%s: wavelan_interrupt(): unexpected mmc interrupt: status 0x%04x.\n",
3724 		       dev->name, dce_status);
3725 #endif
3726 	}
3727 
3728 	/* Check if not controller interrupt */
3729 	if ((hasr & HASR_82586_INTR) == 0) {
3730 #ifdef DEBUG_INTERRUPT_ERROR
3731 		printk(KERN_INFO
3732 		       "%s: wavelan_interrupt(): interrupt not coming from i82586\n",
3733 		       dev->name);
3734 #endif
3735 		spin_unlock (&lp->spinlock);
3736 		return;
3737 	}
3738 
3739 	/* Read interrupt data. */
3740 	obram_read(ioaddr, scboff(OFFSET_SCB, scb_status),
3741 		   (unsigned char *) &status, sizeof(status));
3742 
3743 	/*
3744 	 * Acknowledge the interrupt(s).
3745 	 */
3746 	ack_cmd = status & SCB_ST_INT;
3747 	obram_write(ioaddr, scboff(OFFSET_SCB, scb_command),
3748 		    (unsigned char *) &ack_cmd, sizeof(ack_cmd));
3749 	set_chan_attn(ioaddr, lp->hacr);
3750 
3751 #ifdef DEBUG_INTERRUPT_INFO
3752 	printk(KERN_DEBUG "%s: wavelan_interrupt(): status 0x%04x.\n",
3753 	       dev->name, status);
3754 #endif
3755 
3756 	/* Command completed. */
3757 	if ((status & SCB_ST_CX) == SCB_ST_CX) {
3758 #ifdef DEBUG_INTERRUPT_INFO
3759 		printk(KERN_DEBUG
3760 		       "%s: wavelan_interrupt(): command completed.\n",
3761 		       dev->name);
3762 #endif
3763 		wv_complete(dev, ioaddr, lp);
3764 	}
3765 
3766 	/* Frame received. */
3767 	if ((status & SCB_ST_FR) == SCB_ST_FR) {
3768 #ifdef DEBUG_INTERRUPT_INFO
3769 		printk(KERN_DEBUG
3770 		       "%s: wavelan_interrupt(): received packet.\n",
3771 		       dev->name);
3772 #endif
3773 		wv_receive(dev);
3774 	}
3775 
3776 	/* Check the state of the command unit. */
3777 	if (((status & SCB_ST_CNA) == SCB_ST_CNA) ||
3778 	    (((status & SCB_ST_CUS) != SCB_ST_CUS_ACTV) &&
3779 	     (netif_running(dev)))) {
3780 #ifdef DEBUG_INTERRUPT_ERROR
3781 		printk(KERN_INFO
3782 		       "%s: wavelan_interrupt(): CU inactive -- restarting\n",
3783 		       dev->name);
3784 #endif
3785 		wv_hw_reset(dev);
3786 	}
3787 
3788 	/* Check the state of the command unit. */
3789 	if (((status & SCB_ST_RNR) == SCB_ST_RNR) ||
3790 	    (((status & SCB_ST_RUS) != SCB_ST_RUS_RDY) &&
3791 	     (netif_running(dev)))) {
3792 #ifdef DEBUG_INTERRUPT_ERROR
3793 		printk(KERN_INFO
3794 		       "%s: wavelan_interrupt(): RU not ready -- restarting\n",
3795 		       dev->name);
3796 #endif
3797 		wv_hw_reset(dev);
3798 	}
3799 
3800 	/* Release spinlock */
3801 	spin_unlock (&lp->spinlock);
3802 
3803 #ifdef DEBUG_INTERRUPT_TRACE
3804 	printk(KERN_DEBUG "%s: <-wavelan_interrupt()\n", dev->name);
3805 #endif
3806 }
3807 
3808 /*------------------------------------------------------------------*/
3809 /*
3810  * Watchdog: when we start a transmission, a timer is set for us in the
3811  * kernel.  If the transmission completes, this timer is disabled. If
3812  * the timer expires, we are called and we try to unlock the hardware.
3813  */
wavelan_watchdog(device * dev)3814 static void wavelan_watchdog(device *	dev)
3815 {
3816 	net_local *	lp = (net_local *)dev->priv;
3817 	u_long		ioaddr = dev->base_addr;
3818 	unsigned long	flags;
3819 	unsigned int	nreaped;
3820 
3821 #ifdef DEBUG_INTERRUPT_TRACE
3822 	printk(KERN_DEBUG "%s: ->wavelan_watchdog()\n", dev->name);
3823 #endif
3824 
3825 #ifdef DEBUG_INTERRUPT_ERROR
3826 	printk(KERN_INFO "%s: wavelan_watchdog: watchdog timer expired\n",
3827 	       dev->name);
3828 #endif
3829 
3830 	/* Check that we came here for something */
3831 	if (lp->tx_n_in_use <= 0) {
3832 		return;
3833 	}
3834 
3835 	wv_splhi(lp, &flags);
3836 
3837 	/* Try to see if some buffers are not free (in case we missed
3838 	 * an interrupt */
3839 	nreaped = wv_complete(dev, ioaddr, lp);
3840 
3841 #ifdef DEBUG_INTERRUPT_INFO
3842 	printk(KERN_DEBUG
3843 	       "%s: wavelan_watchdog(): %d reaped, %d remain.\n",
3844 	       dev->name, nreaped, lp->tx_n_in_use);
3845 #endif
3846 
3847 #ifdef DEBUG_PSA_SHOW
3848 	{
3849 		psa_t psa;
3850 		psa_read(dev, 0, (unsigned char *) &psa, sizeof(psa));
3851 		wv_psa_show(&psa);
3852 	}
3853 #endif
3854 #ifdef DEBUG_MMC_SHOW
3855 	wv_mmc_show(dev);
3856 #endif
3857 #ifdef DEBUG_I82586_SHOW
3858 	wv_cu_show(dev);
3859 #endif
3860 
3861 	/* If no buffer has been freed */
3862 	if (nreaped == 0) {
3863 #ifdef DEBUG_INTERRUPT_ERROR
3864 		printk(KERN_INFO
3865 		       "%s: wavelan_watchdog(): cleanup failed, trying reset\n",
3866 		       dev->name);
3867 #endif
3868 		wv_hw_reset(dev);
3869 	}
3870 
3871 	/* At this point, we should have some free Tx buffer ;-) */
3872 	if (lp->tx_n_in_use < NTXBLOCKS - 1)
3873 		netif_wake_queue(dev);
3874 
3875 	wv_splx(lp, &flags);
3876 
3877 #ifdef DEBUG_INTERRUPT_TRACE
3878 	printk(KERN_DEBUG "%s: <-wavelan_watchdog()\n", dev->name);
3879 #endif
3880 }
3881 
3882 /********************* CONFIGURATION CALLBACKS *********************/
3883 /*
3884  * Here are the functions called by the Linux networking code (NET3)
3885  * for initialization, configuration and deinstallations of the
3886  * WaveLAN ISA hardware.
3887  */
3888 
3889 /*------------------------------------------------------------------*/
3890 /*
3891  * Configure and start up the WaveLAN PCMCIA adaptor.
3892  * Called by NET3 when it "opens" the device.
3893  */
wavelan_open(device * dev)3894 static int wavelan_open(device * dev)
3895 {
3896 	net_local *	lp = (net_local *)dev->priv;
3897 	unsigned long	flags;
3898 
3899 #ifdef DEBUG_CALLBACK_TRACE
3900 	printk(KERN_DEBUG "%s: ->wavelan_open(dev=0x%x)\n", dev->name,
3901 	       (unsigned int) dev);
3902 #endif
3903 
3904 	/* Check irq */
3905 	if (dev->irq == 0) {
3906 #ifdef DEBUG_CONFIG_ERROR
3907 		printk(KERN_WARNING "%s: wavelan_open(): no IRQ\n",
3908 		       dev->name);
3909 #endif
3910 		return -ENXIO;
3911 	}
3912 
3913 	if (request_irq(dev->irq, &wavelan_interrupt, 0, "WaveLAN", dev) != 0)
3914 	{
3915 #ifdef DEBUG_CONFIG_ERROR
3916 		printk(KERN_WARNING "%s: wavelan_open(): invalid IRQ\n",
3917 		       dev->name);
3918 #endif
3919 		return -EAGAIN;
3920 	}
3921 
3922 	wv_splhi(lp, &flags);
3923 
3924 	if (wv_hw_reset(dev) != -1) {
3925 		netif_start_queue(dev);
3926 	} else {
3927 		free_irq(dev->irq, dev);
3928 #ifdef DEBUG_CONFIG_ERROR
3929 		printk(KERN_INFO
3930 		       "%s: wavelan_open(): impossible to start the card\n",
3931 		       dev->name);
3932 #endif
3933 		wv_splx(lp, &flags);
3934 		return -EAGAIN;
3935 	}
3936 	wv_splx(lp, &flags);
3937 
3938 #ifdef DEBUG_CALLBACK_TRACE
3939 	printk(KERN_DEBUG "%s: <-wavelan_open()\n", dev->name);
3940 #endif
3941 	return 0;
3942 }
3943 
3944 /*------------------------------------------------------------------*/
3945 /*
3946  * Shut down the WaveLAN ISA card.
3947  * Called by NET3 when it "closes" the device.
3948  */
wavelan_close(device * dev)3949 static int wavelan_close(device * dev)
3950 {
3951 	net_local *lp = (net_local *) dev->priv;
3952 	unsigned long flags;
3953 
3954 #ifdef DEBUG_CALLBACK_TRACE
3955 	printk(KERN_DEBUG "%s: ->wavelan_close(dev=0x%x)\n", dev->name,
3956 	       (unsigned int) dev);
3957 #endif
3958 
3959 	netif_stop_queue(dev);
3960 
3961 	/*
3962 	 * Flush the Tx and disable Rx.
3963 	 */
3964 	wv_splhi(lp, &flags);
3965 	wv_82586_stop(dev);
3966 	wv_splx(lp, &flags);
3967 
3968 	free_irq(dev->irq, dev);
3969 
3970 #ifdef DEBUG_CALLBACK_TRACE
3971 	printk(KERN_DEBUG "%s: <-wavelan_close()\n", dev->name);
3972 #endif
3973 	return 0;
3974 }
3975 
3976 /*------------------------------------------------------------------*/
3977 /*
3978  * Probe an I/O address, and if the WaveLAN is there configure the
3979  * device structure
3980  * (called by wavelan_probe() and via init_module()).
3981  */
wavelan_config(device * dev)3982 static int __init wavelan_config(device * dev)
3983 {
3984 	unsigned long ioaddr = dev->base_addr;
3985 	u8 irq_mask;
3986 	int irq;
3987 	net_local *lp;
3988 
3989 #ifdef DEBUG_CALLBACK_TRACE
3990 	printk(KERN_DEBUG "%s: ->wavelan_config(dev=0x%x, ioaddr=0x%lx)\n",
3991 	       dev->name, (unsigned int) dev, ioaddr);
3992 #endif
3993 
3994 	/* Check IRQ argument on command line. */
3995 	if (dev->irq != 0) {
3996 		irq_mask = wv_irq_to_psa(dev->irq);
3997 
3998 		if (irq_mask == 0) {
3999 #ifdef DEBUG_CONFIG_ERROR
4000 			printk(KERN_WARNING
4001 			       "%s: wavelan_config(): invalid IRQ %d ignored.\n",
4002 			       dev->name, dev->irq);
4003 #endif
4004 			dev->irq = 0;
4005 		} else {
4006 #ifdef DEBUG_CONFIG_INFO
4007 			printk(KERN_DEBUG
4008 			       "%s: wavelan_config(): changing IRQ to %d\n",
4009 			       dev->name, dev->irq);
4010 #endif
4011 			psa_write(ioaddr, HACR_DEFAULT,
4012 				  psaoff(0, psa_int_req_no), &irq_mask, 1);
4013 			/* update the Wavelan checksum */
4014 			update_psa_checksum(dev, ioaddr, HACR_DEFAULT);
4015 			wv_hacr_reset(ioaddr);
4016 		}
4017 	}
4018 
4019 	psa_read(ioaddr, HACR_DEFAULT, psaoff(0, psa_int_req_no),
4020 		 &irq_mask, 1);
4021 	if ((irq = wv_psa_to_irq(irq_mask)) == -1) {
4022 #ifdef DEBUG_CONFIG_ERROR
4023 		printk(KERN_INFO
4024 		       "%s: wavelan_config(): could not wavelan_map_irq(%d).\n",
4025 		       dev->name, irq_mask);
4026 #endif
4027 		return -EAGAIN;
4028 	}
4029 
4030 	dev->irq = irq;
4031 
4032 	if (!request_region(ioaddr, sizeof(ha_t), "wavelan"))
4033 		return -EBUSY;
4034 
4035 	dev->mem_start = 0x0000;
4036 	dev->mem_end = 0x0000;
4037 	dev->if_port = 0;
4038 
4039 	/* Initialize device structures */
4040 	dev->priv = kmalloc(sizeof(net_local), GFP_KERNEL);
4041 	if (dev->priv == NULL) {
4042 		release_region(ioaddr, sizeof(ha_t));
4043 		return -ENOMEM;
4044 	}
4045 	memset(dev->priv, 0x00, sizeof(net_local));
4046 	lp = (net_local *) dev->priv;
4047 
4048 	/* Back link to the device structure. */
4049 	lp->dev = dev;
4050 	/* Add the device at the beginning of the linked list. */
4051 	lp->next = wavelan_list;
4052 	wavelan_list = lp;
4053 
4054 	lp->hacr = HACR_DEFAULT;
4055 
4056 	/* Multicast stuff */
4057 	lp->promiscuous = 0;
4058 	lp->mc_count = 0;
4059 
4060 	/* Init spinlock */
4061 	spin_lock_init(&lp->spinlock);
4062 
4063 	/*
4064 	 * Fill in the fields of the device structure
4065 	 * with generic Ethernet values.
4066 	 */
4067 	ether_setup(dev);
4068 
4069 	SET_MODULE_OWNER(dev);
4070 	dev->open = wavelan_open;
4071 	dev->stop = wavelan_close;
4072 	dev->hard_start_xmit = wavelan_packet_xmit;
4073 	dev->get_stats = wavelan_get_stats;
4074 	dev->set_multicast_list = &wavelan_set_multicast_list;
4075         dev->tx_timeout		= &wavelan_watchdog;
4076         dev->watchdog_timeo	= WATCHDOG_JIFFIES;
4077 #ifdef SET_MAC_ADDRESS
4078 	dev->set_mac_address = &wavelan_set_mac_address;
4079 #endif				/* SET_MAC_ADDRESS */
4080 
4081 #ifdef WIRELESS_EXT		/* if wireless extension exists in the kernel */
4082 	dev->do_ioctl = wavelan_ioctl;
4083 	dev->get_wireless_stats = wavelan_get_wireless_stats;
4084 #endif
4085 
4086 	dev->mtu = WAVELAN_MTU;
4087 
4088 	/* Display nice information. */
4089 	wv_init_info(dev);
4090 
4091 #ifdef DEBUG_CALLBACK_TRACE
4092 	printk(KERN_DEBUG "%s: <-wavelan_config()\n", dev->name);
4093 #endif
4094 	return 0;
4095 }
4096 
4097 /*------------------------------------------------------------------*/
4098 /*
4099  * Check for a network adaptor of this type.  Return '0' iff one
4100  * exists.  There seem to be different interpretations of
4101  * the initial value of dev->base_addr.
4102  * We follow the example in drivers/net/ne.c.
4103  * (called in "Space.c")
4104  */
wavelan_probe(device * dev)4105 int __init wavelan_probe(device * dev)
4106 {
4107 	short base_addr;
4108 	mac_addr mac;		/* MAC address (check existence of WaveLAN) */
4109 	int i;
4110 	int r;
4111 
4112 #ifdef DEBUG_CALLBACK_TRACE
4113 	printk(KERN_DEBUG
4114 	       "%s: ->wavelan_probe(dev=0x%x (base_addr=0x%x))\n",
4115 	       dev->name, (unsigned int) dev,
4116 	       (unsigned int) dev->base_addr);
4117 #endif
4118 
4119 #ifdef	STRUCT_CHECK
4120 	if (wv_struct_check() != (char *) NULL) {
4121 		printk(KERN_WARNING
4122 		       "%s: wavelan_probe(): structure/compiler botch: \"%s\"\n",
4123 		       dev->name, wv_struct_check());
4124 		return -ENODEV;
4125 	}
4126 #endif				/* STRUCT_CHECK */
4127 
4128 	/* Check the value of the command line parameter for base address. */
4129 	base_addr = dev->base_addr;
4130 
4131 	/* Don't probe at all. */
4132 	if (base_addr < 0) {
4133 #ifdef DEBUG_CONFIG_ERROR
4134 		printk(KERN_WARNING
4135 		       "%s: wavelan_probe(): invalid base address\n",
4136 		       dev->name);
4137 #endif
4138 		return -ENXIO;
4139 	}
4140 
4141 	/* Check a single specified location. */
4142 	if (base_addr > 0x100) {
4143 		/* Check if there is something at this base address */
4144 		if ((r = wv_check_ioaddr(base_addr, mac)) == 0) {
4145 			memcpy(dev->dev_addr, mac, 6);	/* Copy MAC address. */
4146 			r = wavelan_config(dev);
4147 		}
4148 #ifdef DEBUG_CONFIG_INFO
4149 		if (r != 0)
4150 			printk(KERN_DEBUG
4151 			       "%s: wavelan_probe(): no device at specified base address (0x%X) or address already in use\n",
4152 			       dev->name, base_addr);
4153 #endif
4154 
4155 #ifdef DEBUG_CALLBACK_TRACE
4156 		printk(KERN_DEBUG "%s: <-wavelan_probe()\n", dev->name);
4157 #endif
4158 		return r;
4159 	}
4160 
4161 	/* Scan all possible addresses of the WaveLAN hardware. */
4162 	for (i = 0; i < NELS(iobase); i++) {
4163 		/* Check whether there is something at this base address. */
4164 		if (wv_check_ioaddr(iobase[i], mac) == 0) {
4165 			dev->base_addr = iobase[i];	/* Copy base address. */
4166 			memcpy(dev->dev_addr, mac, 6);	/* Copy MAC address. */
4167 			if (wavelan_config(dev) == 0) {
4168 #ifdef DEBUG_CALLBACK_TRACE
4169 				printk(KERN_DEBUG
4170 				       "%s: <-wavelan_probe()\n",
4171 				       dev->name);
4172 #endif
4173 				return 0;
4174 			}
4175 		}
4176 	}
4177 
4178 	/* We may have touched base_addr.  Another driver may not like it. */
4179 	dev->base_addr = base_addr;
4180 
4181 #ifdef DEBUG_CONFIG_INFO
4182 	printk(KERN_DEBUG "%s: wavelan_probe(): no device found\n",
4183 	       dev->name);
4184 #endif
4185 
4186 	return -ENODEV;
4187 }
4188 
4189 /****************************** MODULE ******************************/
4190 /*
4191  * Module entry point: insertion and removal
4192  */
4193 
4194 #ifdef	MODULE
4195 /*------------------------------------------------------------------*/
4196 /*
4197  * Insertion of the module
4198  * I'm now quite proud of the multi-device support.
4199  */
init_module(void)4200 int init_module(void)
4201 {
4202 	mac_addr mac;		/* MAC address (check WaveLAN existence) */
4203 	int ret = -EIO;		/* Return error if no cards found */
4204 	int i;
4205 
4206 #ifdef DEBUG_MODULE_TRACE
4207 	printk(KERN_DEBUG "-> init_module()\n");
4208 #endif
4209 
4210 	/* If probing is asked */
4211 	if (io[0] == 0) {
4212 #ifdef DEBUG_CONFIG_ERROR
4213 		printk(KERN_WARNING
4214 		       "WaveLAN init_module(): doing device probing (bad !)\n");
4215 		printk(KERN_WARNING
4216 		       "Specify base addresses while loading module to correct the problem\n");
4217 #endif
4218 
4219 		/* Copy the basic set of address to be probed. */
4220 		for (i = 0; i < NELS(iobase); i++)
4221 			io[i] = iobase[i];
4222 	}
4223 
4224 
4225 	/* Loop on all possible base addresses. */
4226 	i = -1;
4227 	while ((io[++i] != 0) && (i < NELS(io))) {
4228 		/* Check if there is something at this base address. */
4229 		if (wv_check_ioaddr(io[i], mac) == 0) {
4230 			device *dev;
4231 
4232 			/* Create device and set basic arguments. */
4233 			dev =
4234 			    kmalloc(sizeof(struct net_device), GFP_KERNEL);
4235 			if (dev == NULL) {
4236 				ret = -ENOMEM;
4237 				break;
4238 			}
4239 			memset(dev, 0x00, sizeof(struct net_device));
4240 			memcpy(dev->name, name[i], IFNAMSIZ);	/* Copy name */
4241 			dev->base_addr = io[i];
4242 			dev->irq = irq[i];
4243 			dev->init = &wavelan_config;
4244 			memcpy(dev->dev_addr, mac, 6);	/* Copy MAC address. */
4245 
4246 			/* Try to create the device. */
4247 			if (register_netdev(dev) != 0) {
4248 				/* Deallocate everything. */
4249 				/* Note: if dev->priv is mallocated, there is no way to fail. */
4250 				kfree(dev);
4251 			} else {
4252 				/* If at least one device OK, we do not fail */
4253 				ret = 0;
4254 			}
4255 		}		/* if there is something at the address */
4256 	}			/* Loop on all addresses. */
4257 
4258 #ifdef DEBUG_CONFIG_ERROR
4259 	if (wavelan_list == (net_local *) NULL)
4260 		printk(KERN_WARNING
4261 		       "WaveLAN init_module(): no device found\n");
4262 #endif
4263 
4264 #ifdef DEBUG_MODULE_TRACE
4265 	printk(KERN_DEBUG "<- init_module()\n");
4266 #endif
4267 	return ret;
4268 }
4269 
4270 /*------------------------------------------------------------------*/
4271 /*
4272  * Removal of the module
4273  */
cleanup_module(void)4274 void cleanup_module(void)
4275 {
4276 #ifdef DEBUG_MODULE_TRACE
4277 	printk(KERN_DEBUG "-> cleanup_module()\n");
4278 #endif
4279 
4280 	/* Loop on all devices and release them. */
4281 	while (wavelan_list != (net_local *) NULL) {
4282 		device *dev = wavelan_list->dev;
4283 
4284 #ifdef DEBUG_CONFIG_INFO
4285 		printk(KERN_DEBUG
4286 		       "%s: cleanup_module(): removing device at 0x%x\n",
4287 		       dev->name, (unsigned int) dev);
4288 #endif
4289 
4290 		/* Release the ioport region. */
4291 		release_region(dev->base_addr, sizeof(ha_t));
4292 
4293 		/* Definitely remove the device. */
4294 		unregister_netdev(dev);
4295 
4296 		/* Unlink the device. */
4297 		wavelan_list = wavelan_list->next;
4298 
4299 		/* Free pieces. */
4300 		kfree(dev->priv);
4301 		kfree(dev);
4302 	}
4303 
4304 #ifdef DEBUG_MODULE_TRACE
4305 	printk(KERN_DEBUG "<- cleanup_module()\n");
4306 #endif
4307 }
4308 #endif				/* MODULE */
4309 MODULE_LICENSE("GPL");
4310 
4311 /*
4312  * This software may only be used and distributed
4313  * according to the terms of the GNU General Public License.
4314  *
4315  * This software was developed as a component of the
4316  * Linux operating system.
4317  * It is based on other device drivers and information
4318  * either written or supplied by:
4319  *	Ajay Bakre (bakre@paul.rutgers.edu),
4320  *	Donald Becker (becker@scyld.com),
4321  *	Loeke Brederveld (Loeke.Brederveld@Utrecht.NCR.com),
4322  *	Anders Klemets (klemets@it.kth.se),
4323  *	Vladimir V. Kolpakov (w@stier.koenig.ru),
4324  *	Marc Meertens (Marc.Meertens@Utrecht.NCR.com),
4325  *	Pauline Middelink (middelin@polyware.iaf.nl),
4326  *	Robert Morris (rtm@das.harvard.edu),
4327  *	Jean Tourrilhes (jt@hplb.hpl.hp.com),
4328  *	Girish Welling (welling@paul.rutgers.edu),
4329  *
4330  * Thanks go also to:
4331  *	James Ashton (jaa101@syseng.anu.edu.au),
4332  *	Alan Cox (alan@redhat.com),
4333  *	Allan Creighton (allanc@cs.usyd.edu.au),
4334  *	Matthew Geier (matthew@cs.usyd.edu.au),
4335  *	Remo di Giovanni (remo@cs.usyd.edu.au),
4336  *	Eckhard Grah (grah@wrcs1.urz.uni-wuppertal.de),
4337  *	Vipul Gupta (vgupta@cs.binghamton.edu),
4338  *	Mark Hagan (mhagan@wtcpost.daytonoh.NCR.COM),
4339  *	Tim Nicholson (tim@cs.usyd.edu.au),
4340  *	Ian Parkin (ian@cs.usyd.edu.au),
4341  *	John Rosenberg (johnr@cs.usyd.edu.au),
4342  *	George Rossi (george@phm.gov.au),
4343  *	Arthur Scott (arthur@cs.usyd.edu.au),
4344  *	Peter Storey,
4345  * for their assistance and advice.
4346  *
4347  * Please send bug reports, updates, comments to:
4348  *
4349  * Bruce Janson                                    Email:  bruce@cs.usyd.edu.au
4350  * Basser Department of Computer Science           Phone:  +61-2-9351-3423
4351  * University of Sydney, N.S.W., 2006, AUSTRALIA   Fax:    +61-2-9351-3838
4352  */
4353