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