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