[PATCH] pcmcia: remove dev_link_t and client_handle_t indirection
[safe/jmp/linux-2.6] / drivers / net / wireless / netwave_cs.c
1 /*********************************************************************
2  *                
3  * Filename:      netwave_cs.c
4  * Version:       0.4.1
5  * Description:   Netwave AirSurfer Wireless LAN PC Card driver
6  * Status:        Experimental.
7  * Authors:       John Markus Bjørndalen <johnm@cs.uit.no>
8  *                Dag Brattli <dagb@cs.uit.no>
9  *                David Hinds <dahinds@users.sourceforge.net>
10  * Created at:    A long time ago!
11  * Modified at:   Mon Nov 10 11:54:37 1997
12  * Modified by:   Dag Brattli <dagb@cs.uit.no>
13  * 
14  *     Copyright (c) 1997 University of Tromsø, Norway
15  *
16  * Revision History:
17  *
18  *   08-Nov-97 15:14:47   John Markus Bjørndalen <johnm@cs.uit.no>
19  *    - Fixed some bugs in netwave_rx and cleaned it up a bit. 
20  *      (One of the bugs would have destroyed packets when receiving
21  *      multiple packets per interrupt). 
22  *    - Cleaned up parts of newave_hw_xmit. 
23  *    - A few general cleanups. 
24  *   24-Oct-97 13:17:36   Dag Brattli <dagb@cs.uit.no>
25  *    - Fixed netwave_rx receive function (got updated docs)
26  *   Others:
27  *    - Changed name from xircnw to netwave, take a look at 
28  *      http://www.netwave-wireless.com
29  *    - Some reorganizing of the code
30  *    - Removed possible race condition between interrupt handler and transmit
31  *      function
32  *    - Started to add wireless extensions, but still needs some coding
33  *    - Added watchdog for better handling of transmission timeouts 
34  *      (hopefully this works better)
35  ********************************************************************/
36
37 /* To have statistics (just packets sent) define this */
38 #undef NETWAVE_STATS
39
40 #include <linux/config.h>
41 #include <linux/module.h>
42 #include <linux/kernel.h>
43 #include <linux/init.h>
44 #include <linux/types.h>
45 #include <linux/fcntl.h>
46 #include <linux/interrupt.h>
47 #include <linux/ptrace.h>
48 #include <linux/ioport.h>
49 #include <linux/in.h>
50 #include <linux/slab.h>
51 #include <linux/string.h>
52 #include <linux/timer.h>
53 #include <linux/errno.h>
54 #include <linux/netdevice.h>
55 #include <linux/etherdevice.h>
56 #include <linux/skbuff.h>
57 #include <linux/bitops.h>
58 #include <linux/wireless.h>
59 #include <net/iw_handler.h>
60
61 #include <pcmcia/cs_types.h>
62 #include <pcmcia/cs.h>
63 #include <pcmcia/cistpl.h>
64 #include <pcmcia/cisreg.h>
65 #include <pcmcia/ds.h>
66 #include <pcmcia/mem_op.h>
67
68 #include <asm/system.h>
69 #include <asm/io.h>
70 #include <asm/dma.h>
71
72 #define NETWAVE_REGOFF         0x8000
73 /* The Netwave IO registers, offsets to iobase */
74 #define NETWAVE_REG_COR        0x0
75 #define NETWAVE_REG_CCSR       0x2
76 #define NETWAVE_REG_ASR        0x4
77 #define NETWAVE_REG_IMR        0xa
78 #define NETWAVE_REG_PMR        0xc
79 #define NETWAVE_REG_IOLOW      0x6
80 #define NETWAVE_REG_IOHI       0x7
81 #define NETWAVE_REG_IOCONTROL  0x8
82 #define NETWAVE_REG_DATA       0xf
83 /* The Netwave Extended IO registers, offsets to RamBase */
84 #define NETWAVE_EREG_ASCC      0x114
85 #define NETWAVE_EREG_RSER      0x120
86 #define NETWAVE_EREG_RSERW     0x124
87 #define NETWAVE_EREG_TSER      0x130
88 #define NETWAVE_EREG_TSERW     0x134
89 #define NETWAVE_EREG_CB        0x100
90 #define NETWAVE_EREG_SPCQ      0x154
91 #define NETWAVE_EREG_SPU       0x155
92 #define NETWAVE_EREG_LIF       0x14e
93 #define NETWAVE_EREG_ISPLQ     0x156
94 #define NETWAVE_EREG_HHC       0x158
95 #define NETWAVE_EREG_NI        0x16e
96 #define NETWAVE_EREG_MHS       0x16b
97 #define NETWAVE_EREG_TDP       0x140
98 #define NETWAVE_EREG_RDP       0x150
99 #define NETWAVE_EREG_PA        0x160
100 #define NETWAVE_EREG_EC        0x180
101 #define NETWAVE_EREG_CRBP      0x17a
102 #define NETWAVE_EREG_ARW       0x166
103
104 /*
105  * Commands used in the extended command buffer
106  * NETWAVE_EREG_CB (0x100-0x10F) 
107  */
108 #define NETWAVE_CMD_NOP        0x00
109 #define NETWAVE_CMD_SRC        0x01
110 #define NETWAVE_CMD_STC        0x02
111 #define NETWAVE_CMD_AMA        0x03
112 #define NETWAVE_CMD_DMA        0x04
113 #define NETWAVE_CMD_SAMA       0x05
114 #define NETWAVE_CMD_ER         0x06
115 #define NETWAVE_CMD_DR         0x07
116 #define NETWAVE_CMD_TL         0x08
117 #define NETWAVE_CMD_SRP        0x09
118 #define NETWAVE_CMD_SSK        0x0a
119 #define NETWAVE_CMD_SMD        0x0b
120 #define NETWAVE_CMD_SAPD       0x0c
121 #define NETWAVE_CMD_SSS        0x11
122 /* End of Command marker */
123 #define NETWAVE_CMD_EOC        0x00
124
125 /* ASR register bits */
126 #define NETWAVE_ASR_RXRDY   0x80
127 #define NETWAVE_ASR_TXBA    0x01
128
129 #define TX_TIMEOUT              ((32*HZ)/100)
130
131 static const unsigned int imrConfRFU1 = 0x10; /* RFU interrupt mask, keep high */
132 static const unsigned int imrConfIENA = 0x02; /* Interrupt enable */
133
134 static const unsigned int corConfIENA   = 0x01; /* Interrupt enable */
135 static const unsigned int corConfLVLREQ = 0x40; /* Keep high */
136
137 static const unsigned int rxConfRxEna  = 0x80; /* Receive Enable */
138 static const unsigned int rxConfMAC    = 0x20; /* MAC host receive mode*/ 
139 static const unsigned int rxConfPro    = 0x10; /* Promiscuous */
140 static const unsigned int rxConfAMP    = 0x08; /* Accept Multicast Packets */
141 static const unsigned int rxConfBcast  = 0x04; /* Accept Broadcast Packets */
142
143 static const unsigned int txConfTxEna  = 0x80; /* Transmit Enable */
144 static const unsigned int txConfMAC    = 0x20; /* Host sends MAC mode */
145 static const unsigned int txConfEUD    = 0x10; /* Enable Uni-Data packets */
146 static const unsigned int txConfKey    = 0x02; /* Scramble data packets */
147 static const unsigned int txConfLoop   = 0x01; /* Loopback mode */
148
149 /*
150    All the PCMCIA modules use PCMCIA_DEBUG to control debugging.  If
151    you do not define PCMCIA_DEBUG at all, all the debug code will be
152    left out.  If you compile with PCMCIA_DEBUG=0, the debug code will
153    be present but disabled -- but it can then be enabled for specific
154    modules at load time with a 'pc_debug=#' option to insmod.
155 */
156
157 #ifdef PCMCIA_DEBUG
158 static int pc_debug = PCMCIA_DEBUG;
159 module_param(pc_debug, int, 0);
160 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
161 static char *version =
162 "netwave_cs.c 0.3.0 Thu Jul 17 14:36:02 1997 (John Markus Bjørndalen)\n";
163 #else
164 #define DEBUG(n, args...)
165 #endif
166
167 /*====================================================================*/
168
169 /* Parameters that can be set with 'insmod' */
170
171 /* Choose the domain, default is 0x100 */
172 static u_int  domain = 0x100;
173
174 /* Scramble key, range from 0x0 to 0xffff.  
175  * 0x0 is no scrambling. 
176  */
177 static u_int  scramble_key = 0x0;
178
179 /* Shared memory speed, in ns. The documentation states that 
180  * the card should not be read faster than every 400ns. 
181  * This timing should be provided by the HBA. If it becomes a 
182  * problem, try setting mem_speed to 400. 
183  */
184 static int mem_speed;
185
186 module_param(domain, int, 0);
187 module_param(scramble_key, int, 0);
188 module_param(mem_speed, int, 0);
189
190 /*====================================================================*/
191
192 /* PCMCIA (Card Services) related functions */
193 static void netwave_release(struct pcmcia_device *link);     /* Card removal */
194 static void netwave_pcmcia_config(struct pcmcia_device *arg); /* Runs after card
195                                                                                                            insertion */
196 static void netwave_detach(struct pcmcia_device *p_dev);    /* Destroy instance */
197
198 /* Hardware configuration */
199 static void netwave_doreset(kio_addr_t iobase, u_char __iomem *ramBase);
200 static void netwave_reset(struct net_device *dev);
201
202 /* Misc device stuff */
203 static int netwave_open(struct net_device *dev);  /* Open the device */
204 static int netwave_close(struct net_device *dev); /* Close the device */
205
206 /* Packet transmission and Packet reception */
207 static int netwave_start_xmit( struct sk_buff *skb, struct net_device *dev);
208 static int netwave_rx( struct net_device *dev);
209
210 /* Interrupt routines */
211 static irqreturn_t netwave_interrupt(int irq, void *dev_id, struct pt_regs *regs);
212 static void netwave_watchdog(struct net_device *);
213
214 /* Statistics */
215 static void update_stats(struct net_device *dev);
216 static struct net_device_stats *netwave_get_stats(struct net_device *dev);
217
218 /* Wireless extensions */
219 static struct iw_statistics* netwave_get_wireless_stats(struct net_device *dev);
220
221 static void set_multicast_list(struct net_device *dev);
222
223 /*
224    A struct pcmcia_device structure has fields for most things that are needed
225    to keep track of a socket, but there will usually be some device
226    specific information that also needs to be kept track of.  The
227    'priv' pointer in a struct pcmcia_device structure can be used to point to
228    a device-specific private data structure, like this.
229
230    A driver needs to provide a dev_node_t structure for each device
231    on a card.  In some cases, there is only one device per card (for
232    example, ethernet cards, modems).  In other cases, there may be
233    many actual or logical devices (SCSI adapters, memory cards with
234    multiple partitions).  The dev_node_t structures need to be kept
235    in a linked list starting at the 'dev' field of a struct pcmcia_device
236    structure.  We allocate them in the card's private data structure,
237    because they generally can't be allocated dynamically.
238 */
239
240 static const struct iw_handler_def      netwave_handler_def;
241
242 #define SIOCGIPSNAP     SIOCIWFIRSTPRIV + 1     /* Site Survey Snapshot */
243
244 #define MAX_ESA 10
245
246 typedef struct net_addr {
247     u_char addr48[6];
248 } net_addr;
249
250 struct site_survey {
251     u_short length;
252     u_char  struct_revision;
253     u_char  roaming_state;
254         
255     u_char  sp_existsFlag;
256     u_char  sp_link_quality;
257     u_char  sp_max_link_quality;
258     u_char  linkQualityGoodFairBoundary;
259     u_char  linkQualityFairPoorBoundary;
260     u_char  sp_utilization;
261     u_char  sp_goodness;
262     u_char  sp_hotheadcount;
263     u_char  roaming_condition;
264         
265     net_addr sp;
266     u_char   numAPs;
267     net_addr nearByAccessPoints[MAX_ESA];
268 };      
269    
270 typedef struct netwave_private {
271         struct pcmcia_device    *p_dev;
272     spinlock_t  spinlock;       /* Serialize access to the hardware (SMP) */
273     dev_node_t node;
274     u_char     __iomem *ramBase;
275     int        timeoutCounter;
276     int        lastExec;
277     struct timer_list      watchdog;    /* To avoid blocking state */
278     struct site_survey     nss;
279     struct net_device_stats stats;
280     struct iw_statistics   iw_stats;    /* Wireless stats */
281 } netwave_private;
282
283 #ifdef NETWAVE_STATS
284 static struct net_device_stats *netwave_get_stats(struct net_device *dev);
285 #endif
286
287 /*
288  * The Netwave card is little-endian, so won't work for big endian
289  * systems.
290  */
291 static inline unsigned short get_uint16(u_char __iomem *staddr) 
292 {
293     return readw(staddr); /* Return only 16 bits */
294 }
295
296 static inline short get_int16(u_char __iomem * staddr)
297 {
298     return readw(staddr);
299 }
300
301 /* 
302  * Wait until the WOC (Write Operation Complete) bit in the 
303  * ASR (Adapter Status Register) is asserted. 
304  * This should have aborted if it takes too long time. 
305  */
306 static inline void wait_WOC(unsigned int iobase)
307 {
308     /* Spin lock */
309     while ((inb(iobase + NETWAVE_REG_ASR) & 0x8) != 0x8) ; 
310 }
311
312 static void netwave_snapshot(netwave_private *priv, u_char __iomem *ramBase, 
313                              kio_addr_t iobase) {
314     u_short resultBuffer;
315
316     /* if time since last snapshot is > 1 sec. (100 jiffies?)  then take 
317      * new snapshot, else return cached data. This is the recommended rate.  
318      */
319     if ( jiffies - priv->lastExec > 100) { 
320         /* Take site survey  snapshot */ 
321         /*printk( KERN_DEBUG "Taking new snapshot. %ld\n", jiffies -
322           priv->lastExec); */
323         wait_WOC(iobase); 
324         writeb(NETWAVE_CMD_SSS, ramBase + NETWAVE_EREG_CB + 0); 
325         writeb(NETWAVE_CMD_EOC, ramBase + NETWAVE_EREG_CB + 1); 
326         wait_WOC(iobase); 
327
328         /* Get result and copy to cach */ 
329         resultBuffer = readw(ramBase + NETWAVE_EREG_CRBP); 
330         copy_from_pc( &priv->nss, ramBase+resultBuffer, 
331                       sizeof(struct site_survey)); 
332     } 
333 }
334
335 /*
336  * Function netwave_get_wireless_stats (dev)
337  *
338  *    Wireless extensions statistics
339  *
340  */
341 static struct iw_statistics *netwave_get_wireless_stats(struct net_device *dev)
342 {       
343     unsigned long flags;
344     kio_addr_t iobase = dev->base_addr;
345     netwave_private *priv = netdev_priv(dev);
346     u_char __iomem *ramBase = priv->ramBase;
347     struct iw_statistics* wstats;
348         
349     wstats = &priv->iw_stats;
350
351     spin_lock_irqsave(&priv->spinlock, flags);
352         
353     netwave_snapshot( priv, ramBase, iobase);
354
355     wstats->status = priv->nss.roaming_state;
356     wstats->qual.qual = readb( ramBase + NETWAVE_EREG_SPCQ); 
357     wstats->qual.level = readb( ramBase + NETWAVE_EREG_ISPLQ);
358     wstats->qual.noise = readb( ramBase + NETWAVE_EREG_SPU) & 0x3f;
359     wstats->discard.nwid = 0L;
360     wstats->discard.code = 0L;
361     wstats->discard.misc = 0L;
362
363     spin_unlock_irqrestore(&priv->spinlock, flags);
364     
365     return &priv->iw_stats;
366 }
367
368 /*
369  * Function netwave_attach (void)
370  *
371  *     Creates an "instance" of the driver, allocating local data 
372  *     structures for one device.  The device is registered with Card 
373  *     Services.
374  *
375  *     The dev_link structure is initialized, but we don't actually
376  *     configure the card at this point -- we wait until we receive a
377  *     card insertion event.
378  */
379 static int netwave_attach(struct pcmcia_device *link)
380 {
381     struct net_device *dev;
382     netwave_private *priv;
383
384     DEBUG(0, "netwave_attach()\n");
385
386     /* Initialize the struct pcmcia_device structure */
387     dev = alloc_etherdev(sizeof(netwave_private));
388     if (!dev)
389         return -ENOMEM;
390     priv = netdev_priv(dev);
391     priv->p_dev = link;
392     link->priv = dev;
393
394     /* The io structure describes IO port mapping */
395     link->io.NumPorts1 = 16;
396     link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
397     /* link->io.NumPorts2 = 16; 
398        link->io.Attributes2 = IO_DATA_PATH_WIDTH_16; */
399     link->io.IOAddrLines = 5;
400     
401     /* Interrupt setup */
402     link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
403     link->irq.IRQInfo1 = IRQ_LEVEL_ID;
404     link->irq.Handler = &netwave_interrupt;
405     
406     /* General socket configuration */
407     link->conf.Attributes = CONF_ENABLE_IRQ;
408     link->conf.IntType = INT_MEMORY_AND_IO;
409     link->conf.ConfigIndex = 1;
410     link->conf.Present = PRESENT_OPTION;
411
412     /* Netwave private struct init. link/dev/node already taken care of,
413      * other stuff zero'd - Jean II */
414     spin_lock_init(&priv->spinlock);
415
416     /* Netwave specific entries in the device structure */
417     SET_MODULE_OWNER(dev);
418     dev->hard_start_xmit = &netwave_start_xmit;
419     dev->get_stats  = &netwave_get_stats;
420     dev->set_multicast_list = &set_multicast_list;
421     /* wireless extensions */
422     dev->wireless_handlers = (struct iw_handler_def *)&netwave_handler_def;
423
424     dev->tx_timeout = &netwave_watchdog;
425     dev->watchdog_timeo = TX_TIMEOUT;
426
427     dev->open = &netwave_open;
428     dev->stop = &netwave_close;
429     link->irq.Instance = dev;
430
431     link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
432     netwave_pcmcia_config( link);
433
434     return 0;
435 } /* netwave_attach */
436
437 /*
438  * Function netwave_detach (link)
439  *
440  *    This deletes a driver "instance".  The device is de-registered
441  *    with Card Services.  If it has been released, all local data
442  *    structures are freed.  Otherwise, the structures will be freed
443  *    when the device is released.
444  */
445 static void netwave_detach(struct pcmcia_device *link)
446 {
447         struct net_device *dev = link->priv;
448
449         DEBUG(0, "netwave_detach(0x%p)\n", link);
450
451         if (link->state & DEV_CONFIG)
452                 netwave_release(link);
453
454         if (link->dev_node)
455                 unregister_netdev(dev);
456
457         free_netdev(dev);
458 } /* netwave_detach */
459
460 /*
461  * Wireless Handler : get protocol name
462  */
463 static int netwave_get_name(struct net_device *dev,
464                             struct iw_request_info *info,
465                             union iwreq_data *wrqu,
466                             char *extra)
467 {
468         strcpy(wrqu->name, "Netwave");
469         return 0;
470 }
471
472 /*
473  * Wireless Handler : set Network ID
474  */
475 static int netwave_set_nwid(struct net_device *dev,
476                             struct iw_request_info *info,
477                             union iwreq_data *wrqu,
478                             char *extra)
479 {
480         unsigned long flags;
481         kio_addr_t iobase = dev->base_addr;
482         netwave_private *priv = netdev_priv(dev);
483         u_char __iomem *ramBase = priv->ramBase;
484
485         /* Disable interrupts & save flags */
486         spin_lock_irqsave(&priv->spinlock, flags);
487
488         if(!wrqu->nwid.disabled) {
489             domain = wrqu->nwid.value;
490             printk( KERN_DEBUG "Setting domain to 0x%x%02x\n", 
491                     (domain >> 8) & 0x01, domain & 0xff);
492             wait_WOC(iobase);
493             writeb(NETWAVE_CMD_SMD, ramBase + NETWAVE_EREG_CB + 0);
494             writeb( domain & 0xff, ramBase + NETWAVE_EREG_CB + 1);
495             writeb((domain >>8 ) & 0x01,ramBase + NETWAVE_EREG_CB+2);
496             writeb(NETWAVE_CMD_EOC, ramBase + NETWAVE_EREG_CB + 3);
497         }
498
499         /* ReEnable interrupts & restore flags */
500         spin_unlock_irqrestore(&priv->spinlock, flags);
501     
502         return 0;
503 }
504
505 /*
506  * Wireless Handler : get Network ID
507  */
508 static int netwave_get_nwid(struct net_device *dev,
509                             struct iw_request_info *info,
510                             union iwreq_data *wrqu,
511                             char *extra)
512 {
513         wrqu->nwid.value = domain;
514         wrqu->nwid.disabled = 0;
515         wrqu->nwid.fixed = 1;
516         return 0;
517 }
518
519 /*
520  * Wireless Handler : set scramble key
521  */
522 static int netwave_set_scramble(struct net_device *dev,
523                                 struct iw_request_info *info,
524                                 union iwreq_data *wrqu,
525                                 char *key)
526 {
527         unsigned long flags;
528         kio_addr_t iobase = dev->base_addr;
529         netwave_private *priv = netdev_priv(dev);
530         u_char __iomem *ramBase = priv->ramBase;
531
532         /* Disable interrupts & save flags */
533         spin_lock_irqsave(&priv->spinlock, flags);
534
535         scramble_key = (key[0] << 8) | key[1];
536         wait_WOC(iobase);
537         writeb(NETWAVE_CMD_SSK, ramBase + NETWAVE_EREG_CB + 0);
538         writeb(scramble_key & 0xff, ramBase + NETWAVE_EREG_CB + 1);
539         writeb((scramble_key>>8) & 0xff, ramBase + NETWAVE_EREG_CB + 2);
540         writeb(NETWAVE_CMD_EOC, ramBase + NETWAVE_EREG_CB + 3);
541
542         /* ReEnable interrupts & restore flags */
543         spin_unlock_irqrestore(&priv->spinlock, flags);
544     
545         return 0;
546 }
547
548 /*
549  * Wireless Handler : get scramble key
550  */
551 static int netwave_get_scramble(struct net_device *dev,
552                                 struct iw_request_info *info,
553                                 union iwreq_data *wrqu,
554                                 char *key)
555 {
556         key[1] = scramble_key & 0xff;
557         key[0] = (scramble_key>>8) & 0xff;
558         wrqu->encoding.flags = IW_ENCODE_ENABLED;
559         wrqu->encoding.length = 2;
560         return 0;
561 }
562
563 /*
564  * Wireless Handler : get mode
565  */
566 static int netwave_get_mode(struct net_device *dev,
567                             struct iw_request_info *info,
568                             union iwreq_data *wrqu,
569                             char *extra)
570 {
571         if(domain & 0x100)
572                 wrqu->mode = IW_MODE_INFRA;
573         else
574                 wrqu->mode = IW_MODE_ADHOC;
575
576         return 0;
577 }
578
579 /*
580  * Wireless Handler : get range info
581  */
582 static int netwave_get_range(struct net_device *dev,
583                              struct iw_request_info *info,
584                              union iwreq_data *wrqu,
585                              char *extra)
586 {
587         struct iw_range *range = (struct iw_range *) extra;
588         int ret = 0;
589
590         /* Set the length (very important for backward compatibility) */
591         wrqu->data.length = sizeof(struct iw_range);
592
593         /* Set all the info we don't care or don't know about to zero */
594         memset(range, 0, sizeof(struct iw_range));
595
596         /* Set the Wireless Extension versions */
597         range->we_version_compiled = WIRELESS_EXT;
598         range->we_version_source = 9;   /* Nothing for us in v10 and v11 */
599                    
600         /* Set information in the range struct */
601         range->throughput = 450 * 1000; /* don't argue on this ! */
602         range->min_nwid = 0x0000;
603         range->max_nwid = 0x01FF;
604
605         range->num_channels = range->num_frequency = 0;
606                    
607         range->sensitivity = 0x3F;
608         range->max_qual.qual = 255;
609         range->max_qual.level = 255;
610         range->max_qual.noise = 0;
611                    
612         range->num_bitrates = 1;
613         range->bitrate[0] = 1000000;    /* 1 Mb/s */
614
615         range->encoding_size[0] = 2;            /* 16 bits scrambling */
616         range->num_encoding_sizes = 1;
617         range->max_encoding_tokens = 1; /* Only one key possible */
618
619         return ret;
620 }
621
622 /*
623  * Wireless Private Handler : get snapshot
624  */
625 static int netwave_get_snap(struct net_device *dev,
626                             struct iw_request_info *info,
627                             union iwreq_data *wrqu,
628                             char *extra)
629 {
630         unsigned long flags;
631         kio_addr_t iobase = dev->base_addr;
632         netwave_private *priv = netdev_priv(dev);
633         u_char __iomem *ramBase = priv->ramBase;
634
635         /* Disable interrupts & save flags */
636         spin_lock_irqsave(&priv->spinlock, flags);
637
638         /* Take snapshot of environment */
639         netwave_snapshot( priv, ramBase, iobase);
640         wrqu->data.length = priv->nss.length;
641         memcpy(extra, (u_char *) &priv->nss, sizeof( struct site_survey));
642
643         priv->lastExec = jiffies;
644
645         /* ReEnable interrupts & restore flags */
646         spin_unlock_irqrestore(&priv->spinlock, flags);
647     
648         return(0);
649 }
650
651 /*
652  * Structures to export the Wireless Handlers
653  *     This is the stuff that are treated the wireless extensions (iwconfig)
654  */
655
656 static const struct iw_priv_args netwave_private_args[] = {
657 /*{ cmd,         set_args,                            get_args, name } */
658   { SIOCGIPSNAP, 0, 
659     IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | sizeof(struct site_survey), 
660     "getsitesurvey" },
661 };
662
663 static const iw_handler         netwave_handler[] =
664 {
665         NULL,                           /* SIOCSIWNAME */
666         netwave_get_name,               /* SIOCGIWNAME */
667         netwave_set_nwid,               /* SIOCSIWNWID */
668         netwave_get_nwid,               /* SIOCGIWNWID */
669         NULL,                           /* SIOCSIWFREQ */
670         NULL,                           /* SIOCGIWFREQ */
671         NULL,                           /* SIOCSIWMODE */
672         netwave_get_mode,               /* SIOCGIWMODE */
673         NULL,                           /* SIOCSIWSENS */
674         NULL,                           /* SIOCGIWSENS */
675         NULL,                           /* SIOCSIWRANGE */
676         netwave_get_range,              /* SIOCGIWRANGE */
677         NULL,                           /* SIOCSIWPRIV */
678         NULL,                           /* SIOCGIWPRIV */
679         NULL,                           /* SIOCSIWSTATS */
680         NULL,                           /* SIOCGIWSTATS */
681         NULL,                           /* SIOCSIWSPY */
682         NULL,                           /* SIOCGIWSPY */
683         NULL,                           /* -- hole -- */
684         NULL,                           /* -- hole -- */
685         NULL,                           /* SIOCSIWAP */
686         NULL,                           /* SIOCGIWAP */
687         NULL,                           /* -- hole -- */
688         NULL,                           /* SIOCGIWAPLIST */
689         NULL,                           /* -- hole -- */
690         NULL,                           /* -- hole -- */
691         NULL,                           /* SIOCSIWESSID */
692         NULL,                           /* SIOCGIWESSID */
693         NULL,                           /* SIOCSIWNICKN */
694         NULL,                           /* SIOCGIWNICKN */
695         NULL,                           /* -- hole -- */
696         NULL,                           /* -- hole -- */
697         NULL,                           /* SIOCSIWRATE */
698         NULL,                           /* SIOCGIWRATE */
699         NULL,                           /* SIOCSIWRTS */
700         NULL,                           /* SIOCGIWRTS */
701         NULL,                           /* SIOCSIWFRAG */
702         NULL,                           /* SIOCGIWFRAG */
703         NULL,                           /* SIOCSIWTXPOW */
704         NULL,                           /* SIOCGIWTXPOW */
705         NULL,                           /* SIOCSIWRETRY */
706         NULL,                           /* SIOCGIWRETRY */
707         netwave_set_scramble,           /* SIOCSIWENCODE */
708         netwave_get_scramble,           /* SIOCGIWENCODE */
709 };
710
711 static const iw_handler         netwave_private_handler[] =
712 {
713         NULL,                           /* SIOCIWFIRSTPRIV */
714         netwave_get_snap,               /* SIOCIWFIRSTPRIV + 1 */
715 };
716
717 static const struct iw_handler_def      netwave_handler_def =
718 {
719         .num_standard   = sizeof(netwave_handler)/sizeof(iw_handler),
720         .num_private    = sizeof(netwave_private_handler)/sizeof(iw_handler),
721         .num_private_args = sizeof(netwave_private_args)/sizeof(struct iw_priv_args),
722         .standard       = (iw_handler *) netwave_handler,
723         .private        = (iw_handler *) netwave_private_handler,
724         .private_args   = (struct iw_priv_args *) netwave_private_args,
725         .get_wireless_stats = netwave_get_wireless_stats,
726 };
727
728 /*
729  * Function netwave_pcmcia_config (link)
730  *
731  *     netwave_pcmcia_config() is scheduled to run after a CARD_INSERTION 
732  *     event is received, to configure the PCMCIA socket, and to make the
733  *     device available to the system. 
734  *
735  */
736
737 #define CS_CHECK(fn, ret) \
738 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
739
740 static void netwave_pcmcia_config(struct pcmcia_device *link) {
741     struct net_device *dev = link->priv;
742     netwave_private *priv = netdev_priv(dev);
743     tuple_t tuple;
744     cisparse_t parse;
745     int i, j, last_ret, last_fn;
746     u_char buf[64];
747     win_req_t req;
748     memreq_t mem;
749     u_char __iomem *ramBase = NULL;
750
751     DEBUG(0, "netwave_pcmcia_config(0x%p)\n", link);
752
753     /*
754       This reads the card's CONFIG tuple to find its configuration
755       registers.
756     */
757     tuple.Attributes = 0;
758     tuple.TupleData = (cisdata_t *) buf;
759     tuple.TupleDataMax = 64;
760     tuple.TupleOffset = 0;
761     tuple.DesiredTuple = CISTPL_CONFIG;
762     CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
763     CS_CHECK(GetTupleData, pcmcia_get_tuple_data(link, &tuple));
764     CS_CHECK(ParseTuple, pcmcia_parse_tuple(link, &tuple, &parse));
765     link->conf.ConfigBase = parse.config.base;
766     link->conf.Present = parse.config.rmask[0];
767
768     /* Configure card */
769     link->state |= DEV_CONFIG;
770
771     /*
772      *  Try allocating IO ports.  This tries a few fixed addresses.
773      *  If you want, you can also read the card's config table to
774      *  pick addresses -- see the serial driver for an example.
775      */
776     for (i = j = 0x0; j < 0x400; j += 0x20) {
777         link->io.BasePort1 = j ^ 0x300;
778         i = pcmcia_request_io(link, &link->io);
779         if (i == CS_SUCCESS) break;
780     }
781     if (i != CS_SUCCESS) {
782         cs_error(link, RequestIO, i);
783         goto failed;
784     }
785
786     /*
787      *  Now allocate an interrupt line.  Note that this does not
788      *  actually assign a handler to the interrupt.
789      */
790     CS_CHECK(RequestIRQ, pcmcia_request_irq(link, &link->irq));
791
792     /*
793      *  This actually configures the PCMCIA socket -- setting up
794      *  the I/O windows and the interrupt mapping.
795      */
796     CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link, &link->conf));
797
798     /*
799      *  Allocate a 32K memory window.  Note that the struct pcmcia_device
800      *  structure provides space for one window handle -- if your
801      *  device needs several windows, you'll need to keep track of
802      *  the handles in your private data structure, dev->priv.
803      */
804     DEBUG(1, "Setting mem speed of %d\n", mem_speed);
805
806     req.Attributes = WIN_DATA_WIDTH_8|WIN_MEMORY_TYPE_CM|WIN_ENABLE;
807     req.Base = 0; req.Size = 0x8000;
808     req.AccessSpeed = mem_speed;
809     CS_CHECK(RequestWindow, pcmcia_request_window(&link, &req, &link->win));
810     mem.CardOffset = 0x20000; mem.Page = 0; 
811     CS_CHECK(MapMemPage, pcmcia_map_mem_page(link->win, &mem));
812
813     /* Store base address of the common window frame */
814     ramBase = ioremap(req.Base, 0x8000);
815     priv->ramBase = ramBase;
816
817     dev->irq = link->irq.AssignedIRQ;
818     dev->base_addr = link->io.BasePort1;
819     SET_NETDEV_DEV(dev, &handle_to_dev(link));
820
821     if (register_netdev(dev) != 0) {
822         printk(KERN_DEBUG "netwave_cs: register_netdev() failed\n");
823         goto failed;
824     }
825
826     strcpy(priv->node.dev_name, dev->name);
827     link->dev_node = &priv->node;
828     link->state &= ~DEV_CONFIG_PENDING;
829
830     /* Reset card before reading physical address */
831     netwave_doreset(dev->base_addr, ramBase);
832
833     /* Read the ethernet address and fill in the Netwave registers. */
834     for (i = 0; i < 6; i++) 
835         dev->dev_addr[i] = readb(ramBase + NETWAVE_EREG_PA + i);
836
837     printk(KERN_INFO "%s: Netwave: port %#3lx, irq %d, mem %lx id "
838            "%c%c, hw_addr ", dev->name, dev->base_addr, dev->irq,
839            (u_long) ramBase, (int) readb(ramBase+NETWAVE_EREG_NI),
840            (int) readb(ramBase+NETWAVE_EREG_NI+1));
841     for (i = 0; i < 6; i++)
842         printk("%02X%s", dev->dev_addr[i], ((i<5) ? ":" : "\n"));
843
844     /* get revision words */
845     printk(KERN_DEBUG "Netwave_reset: revision %04x %04x\n", 
846            get_uint16(ramBase + NETWAVE_EREG_ARW),
847            get_uint16(ramBase + NETWAVE_EREG_ARW+2));
848     return;
849
850 cs_failed:
851     cs_error(link, last_fn, last_ret);
852 failed:
853     netwave_release(link);
854 } /* netwave_pcmcia_config */
855
856 /*
857  * Function netwave_release (arg)
858  *
859  *    After a card is removed, netwave_release() will unregister the net
860  *    device, and release the PCMCIA configuration.  If the device is
861  *    still open, this will be postponed until it is closed.
862  */
863 static void netwave_release(struct pcmcia_device *link)
864 {
865         struct net_device *dev = link->priv;
866         netwave_private *priv = netdev_priv(dev);
867
868         DEBUG(0, "netwave_release(0x%p)\n", link);
869
870         pcmcia_disable_device(link);
871         if (link->win)
872                 iounmap(priv->ramBase);
873 }
874
875 static int netwave_suspend(struct pcmcia_device *link)
876 {
877         struct net_device *dev = link->priv;
878
879         if ((link->state & DEV_CONFIG) && (link->open))
880                 netif_device_detach(dev);
881
882         return 0;
883 }
884
885 static int netwave_resume(struct pcmcia_device *link)
886 {
887         struct net_device *dev = link->priv;
888
889         if ((link->state & DEV_CONFIG) && (link->open)) {
890                 netwave_reset(dev);
891                 netif_device_attach(dev);
892         }
893
894         return 0;
895 }
896
897
898 /*
899  * Function netwave_doreset (ioBase, ramBase)
900  *
901  *    Proper hardware reset of the card.
902  */
903 static void netwave_doreset(kio_addr_t ioBase, u_char __iomem *ramBase)
904 {
905     /* Reset card */
906     wait_WOC(ioBase);
907     outb(0x80, ioBase + NETWAVE_REG_PMR);
908     writeb(0x08, ramBase + NETWAVE_EREG_ASCC); /* Bit 3 is WOC */
909     outb(0x0, ioBase + NETWAVE_REG_PMR); /* release reset */
910 }
911
912 /*
913  * Function netwave_reset (dev)
914  *
915  *    Reset and restore all of the netwave registers 
916  */
917 static void netwave_reset(struct net_device *dev) {
918     /* u_char state; */
919     netwave_private *priv = netdev_priv(dev);
920     u_char __iomem *ramBase = priv->ramBase;
921     kio_addr_t iobase = dev->base_addr;
922
923     DEBUG(0, "netwave_reset: Done with hardware reset\n");
924
925     priv->timeoutCounter = 0;
926
927     /* Reset card */
928     netwave_doreset(iobase, ramBase);
929     printk(KERN_DEBUG "netwave_reset: Done with hardware reset\n");
930         
931     /* Write a NOP to check the card */
932     wait_WOC(iobase);
933     writeb(NETWAVE_CMD_NOP, ramBase + NETWAVE_EREG_CB + 0);
934     writeb(NETWAVE_CMD_EOC, ramBase + NETWAVE_EREG_CB + 1);
935         
936     /* Set receive conf */
937     wait_WOC(iobase);
938     writeb(NETWAVE_CMD_SRC, ramBase + NETWAVE_EREG_CB + 0);
939     writeb(rxConfRxEna + rxConfBcast, ramBase + NETWAVE_EREG_CB + 1);
940     writeb(NETWAVE_CMD_EOC, ramBase + NETWAVE_EREG_CB + 2);
941     
942     /* Set transmit conf */
943     wait_WOC(iobase);
944     writeb(NETWAVE_CMD_STC, ramBase + NETWAVE_EREG_CB + 0);
945     writeb(txConfTxEna, ramBase + NETWAVE_EREG_CB + 1);
946     writeb(NETWAVE_CMD_EOC, ramBase + NETWAVE_EREG_CB + 2);
947     
948     /* Now set the MU Domain */
949     printk(KERN_DEBUG "Setting domain to 0x%x%02x\n", (domain >> 8) & 0x01, domain & 0xff);
950     wait_WOC(iobase);
951     writeb(NETWAVE_CMD_SMD, ramBase + NETWAVE_EREG_CB + 0);
952     writeb(domain & 0xff, ramBase + NETWAVE_EREG_CB + 1);
953     writeb((domain>>8) & 0x01, ramBase + NETWAVE_EREG_CB + 2);
954     writeb(NETWAVE_CMD_EOC, ramBase + NETWAVE_EREG_CB + 3);
955         
956     /* Set scramble key */
957     printk(KERN_DEBUG "Setting scramble key to 0x%x\n", scramble_key);
958     wait_WOC(iobase);
959     writeb(NETWAVE_CMD_SSK, ramBase + NETWAVE_EREG_CB + 0);
960     writeb(scramble_key & 0xff, ramBase + NETWAVE_EREG_CB + 1);
961     writeb((scramble_key>>8) & 0xff, ramBase + NETWAVE_EREG_CB + 2);
962     writeb(NETWAVE_CMD_EOC, ramBase + NETWAVE_EREG_CB + 3);
963
964     /* Enable interrupts, bit 4 high to keep unused
965      * source from interrupting us, bit 2 high to 
966      * set interrupt enable, 567 to enable TxDN, 
967      * RxErr and RxRdy
968      */
969     wait_WOC(iobase);
970     outb(imrConfIENA+imrConfRFU1, iobase + NETWAVE_REG_IMR);
971
972     /* Hent 4 bytes fra 0x170. Skal vaere 0a,29,88,36
973      * waitWOC
974      * skriv 80 til d000:3688
975      * sjekk om det ble 80
976      */
977     
978     /* Enable Receiver */
979     wait_WOC(iobase);
980     writeb(NETWAVE_CMD_ER, ramBase + NETWAVE_EREG_CB + 0);
981     writeb(NETWAVE_CMD_EOC, ramBase + NETWAVE_EREG_CB + 1);
982         
983     /* Set the IENA bit in COR */
984     wait_WOC(iobase);
985     outb(corConfIENA + corConfLVLREQ, iobase + NETWAVE_REG_COR);
986 }
987
988 /*
989  * Function netwave_hw_xmit (data, len, dev)    
990  */
991 static int netwave_hw_xmit(unsigned char* data, int len,
992                            struct net_device* dev) {
993     unsigned long flags;
994     unsigned int TxFreeList,
995                  curBuff,
996                  MaxData, 
997                  DataOffset;
998     int tmpcount; 
999         
1000     netwave_private *priv = netdev_priv(dev);
1001     u_char __iomem * ramBase = priv->ramBase;
1002     kio_addr_t iobase = dev->base_addr;
1003
1004     /* Disable interrupts & save flags */
1005     spin_lock_irqsave(&priv->spinlock, flags);
1006
1007     /* Check if there are transmit buffers available */
1008     wait_WOC(iobase);
1009     if ((inb(iobase+NETWAVE_REG_ASR) & NETWAVE_ASR_TXBA) == 0) {
1010         /* No buffers available */
1011         printk(KERN_DEBUG "netwave_hw_xmit: %s - no xmit buffers available.\n",
1012                dev->name);
1013         spin_unlock_irqrestore(&priv->spinlock, flags);
1014         return 1;
1015     }
1016
1017     priv->stats.tx_bytes += len;
1018
1019     DEBUG(3, "Transmitting with SPCQ %x SPU %x LIF %x ISPLQ %x\n",
1020           readb(ramBase + NETWAVE_EREG_SPCQ),
1021           readb(ramBase + NETWAVE_EREG_SPU),
1022           readb(ramBase + NETWAVE_EREG_LIF),
1023           readb(ramBase + NETWAVE_EREG_ISPLQ));
1024
1025     /* Now try to insert it into the adapters free memory */
1026     wait_WOC(iobase);
1027     TxFreeList = get_uint16(ramBase + NETWAVE_EREG_TDP);
1028     MaxData    = get_uint16(ramBase + NETWAVE_EREG_TDP+2);
1029     DataOffset = get_uint16(ramBase + NETWAVE_EREG_TDP+4);
1030         
1031     DEBUG(3, "TxFreeList %x, MaxData %x, DataOffset %x\n",
1032           TxFreeList, MaxData, DataOffset);
1033
1034     /* Copy packet to the adapter fragment buffers */
1035     curBuff = TxFreeList; 
1036     tmpcount = 0; 
1037     while (tmpcount < len) {
1038         int tmplen = len - tmpcount; 
1039         copy_to_pc(ramBase + curBuff + DataOffset, data + tmpcount, 
1040                    (tmplen < MaxData) ? tmplen : MaxData);
1041         tmpcount += MaxData;
1042                         
1043         /* Advance to next buffer */
1044         curBuff = get_uint16(ramBase + curBuff);
1045     }
1046     
1047     /* Now issue transmit list */
1048     wait_WOC(iobase);
1049     writeb(NETWAVE_CMD_TL, ramBase + NETWAVE_EREG_CB + 0);
1050     writeb(len & 0xff, ramBase + NETWAVE_EREG_CB + 1);
1051     writeb((len>>8) & 0xff, ramBase + NETWAVE_EREG_CB + 2);
1052     writeb(NETWAVE_CMD_EOC, ramBase + NETWAVE_EREG_CB + 3);
1053
1054     spin_unlock_irqrestore(&priv->spinlock, flags);
1055     return 0;
1056 }
1057
1058 static int netwave_start_xmit(struct sk_buff *skb, struct net_device *dev) {
1059         /* This flag indicate that the hardware can't perform a transmission.
1060          * Theoritically, NET3 check it before sending a packet to the driver,
1061          * but in fact it never do that and pool continuously.
1062          * As the watchdog will abort too long transmissions, we are quite safe...
1063          */
1064
1065     netif_stop_queue(dev);
1066
1067     {
1068         short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
1069         unsigned char* buf = skb->data;
1070         
1071         if (netwave_hw_xmit( buf, length, dev) == 1) {
1072             /* Some error, let's make them call us another time? */
1073             netif_start_queue(dev);
1074         }
1075         dev->trans_start = jiffies;
1076     }
1077     dev_kfree_skb(skb);
1078     
1079     return 0;
1080 } /* netwave_start_xmit */
1081
1082 /*
1083  * Function netwave_interrupt (irq, dev_id, regs)
1084  *
1085  *    This function is the interrupt handler for the Netwave card. This
1086  *    routine will be called whenever: 
1087  *        1. A packet is received.
1088  *        2. A packet has successfully been transferred and the unit is
1089  *           ready to transmit another packet.
1090  *        3. A command has completed execution.
1091  */
1092 static irqreturn_t netwave_interrupt(int irq, void* dev_id, struct pt_regs *regs)
1093 {
1094     kio_addr_t iobase;
1095     u_char __iomem *ramBase;
1096     struct net_device *dev = (struct net_device *)dev_id;
1097     struct netwave_private *priv = netdev_priv(dev);
1098     struct pcmcia_device *link = priv->p_dev;
1099     int i;
1100     
1101     if (!netif_device_present(dev))
1102         return IRQ_NONE;
1103     
1104     iobase = dev->base_addr;
1105     ramBase = priv->ramBase;
1106         
1107     /* Now find what caused the interrupt, check while interrupts ready */
1108     for (i = 0; i < 10; i++) {
1109         u_char status;
1110                 
1111         wait_WOC(iobase);       
1112         if (!(inb(iobase+NETWAVE_REG_CCSR) & 0x02))
1113             break; /* None of the interrupt sources asserted (normal exit) */
1114         
1115         status = inb(iobase + NETWAVE_REG_ASR);
1116                 
1117         if (!DEV_OK(link)) {
1118             DEBUG(1, "netwave_interrupt: Interrupt with status 0x%x "
1119                   "from removed or suspended card!\n", status);
1120             break;
1121         }
1122                 
1123         /* RxRdy */
1124         if (status & 0x80) {
1125             netwave_rx(dev);
1126             /* wait_WOC(iobase); */
1127             /* RxRdy cannot be reset directly by the host */
1128         }
1129         /* RxErr */
1130         if (status & 0x40) {
1131             u_char rser;
1132                         
1133             rser = readb(ramBase + NETWAVE_EREG_RSER);                  
1134             
1135             if (rser & 0x04) {
1136                 ++priv->stats.rx_dropped; 
1137                 ++priv->stats.rx_crc_errors;
1138             }
1139             if (rser & 0x02)
1140                 ++priv->stats.rx_frame_errors;
1141                         
1142             /* Clear the RxErr bit in RSER. RSER+4 is the
1143              * write part. Also clear the RxCRC (0x04) and 
1144              * RxBig (0x02) bits if present */
1145             wait_WOC(iobase);
1146             writeb(0x40 | (rser & 0x06), ramBase + NETWAVE_EREG_RSER + 4);
1147
1148             /* Write bit 6 high to ASCC to clear RxErr in ASR,
1149              * WOC must be set first! 
1150              */
1151             wait_WOC(iobase);
1152             writeb(0x40, ramBase + NETWAVE_EREG_ASCC);
1153
1154             /* Remember to count up priv->stats on error packets */
1155             ++priv->stats.rx_errors;
1156         }
1157         /* TxDN */
1158         if (status & 0x20) {
1159             int txStatus;
1160
1161             txStatus = readb(ramBase + NETWAVE_EREG_TSER);
1162             DEBUG(3, "Transmit done. TSER = %x id %x\n", 
1163                   txStatus, readb(ramBase + NETWAVE_EREG_TSER + 1));
1164             
1165             if (txStatus & 0x20) {
1166                 /* Transmitting was okay, clear bits */
1167                 wait_WOC(iobase);
1168                 writeb(0x2f, ramBase + NETWAVE_EREG_TSER + 4);
1169                 ++priv->stats.tx_packets;
1170             }
1171                         
1172             if (txStatus & 0xd0) {
1173                 if (txStatus & 0x80) {
1174                     ++priv->stats.collisions; /* Because of /proc/net/dev*/
1175                     /* ++priv->stats.tx_aborted_errors; */
1176                     /* printk("Collision. %ld\n", jiffies - dev->trans_start); */
1177                 }
1178                 if (txStatus & 0x40) 
1179                     ++priv->stats.tx_carrier_errors;
1180                 /* 0x80 TxGU Transmit giveup - nine times and no luck
1181                  * 0x40 TxNOAP No access point. Discarded packet.
1182                  * 0x10 TxErr Transmit error. Always set when 
1183                  *      TxGU and TxNOAP is set. (Those are the only ones
1184                  *      to set TxErr).
1185                  */
1186                 DEBUG(3, "netwave_interrupt: TxDN with error status %x\n", 
1187                       txStatus);
1188                 
1189                 /* Clear out TxGU, TxNOAP, TxErr and TxTrys */
1190                 wait_WOC(iobase);
1191                 writeb(0xdf & txStatus, ramBase+NETWAVE_EREG_TSER+4);
1192                 ++priv->stats.tx_errors;
1193             }
1194             DEBUG(3, "New status is TSER %x ASR %x\n",
1195                   readb(ramBase + NETWAVE_EREG_TSER),
1196                   inb(iobase + NETWAVE_REG_ASR));
1197
1198             netif_wake_queue(dev);
1199         }
1200         /* TxBA, this would trigger on all error packets received */
1201         /* if (status & 0x01) {
1202            DEBUG(4, "Transmit buffers available, %x\n", status);
1203            }
1204            */
1205     }
1206     /* Handled if we looped at least one time - Jean II */
1207     return IRQ_RETVAL(i);
1208 } /* netwave_interrupt */
1209
1210 /*
1211  * Function netwave_watchdog (a)
1212  *
1213  *    Watchdog : when we start a transmission, we set a timer in the
1214  *    kernel.  If the transmission complete, this timer is disabled. If
1215  *    it expire, we reset the card.
1216  *
1217  */
1218 static void netwave_watchdog(struct net_device *dev) {
1219
1220     DEBUG(1, "%s: netwave_watchdog: watchdog timer expired\n", dev->name);
1221     netwave_reset(dev);
1222     dev->trans_start = jiffies;
1223     netif_wake_queue(dev);
1224 } /* netwave_watchdog */
1225
1226 static struct net_device_stats *netwave_get_stats(struct net_device *dev) {
1227     netwave_private *priv = netdev_priv(dev);
1228
1229     update_stats(dev);
1230
1231     DEBUG(2, "netwave: SPCQ %x SPU %x LIF %x ISPLQ %x MHS %x rxtx %x"
1232           " %x tx %x %x %x %x\n", 
1233           readb(priv->ramBase + NETWAVE_EREG_SPCQ),
1234           readb(priv->ramBase + NETWAVE_EREG_SPU),
1235           readb(priv->ramBase + NETWAVE_EREG_LIF),
1236           readb(priv->ramBase + NETWAVE_EREG_ISPLQ),
1237           readb(priv->ramBase + NETWAVE_EREG_MHS),
1238           readb(priv->ramBase + NETWAVE_EREG_EC + 0xe),
1239           readb(priv->ramBase + NETWAVE_EREG_EC + 0xf),
1240           readb(priv->ramBase + NETWAVE_EREG_EC + 0x18),
1241           readb(priv->ramBase + NETWAVE_EREG_EC + 0x19),
1242           readb(priv->ramBase + NETWAVE_EREG_EC + 0x1a),
1243           readb(priv->ramBase + NETWAVE_EREG_EC + 0x1b));
1244
1245     return &priv->stats;
1246 }
1247
1248 static void update_stats(struct net_device *dev) {
1249     //unsigned long flags;
1250 /*     netwave_private *priv = netdev_priv(dev); */
1251
1252     //spin_lock_irqsave(&priv->spinlock, flags);
1253
1254 /*    priv->stats.rx_packets = readb(priv->ramBase + 0x18e); 
1255     priv->stats.tx_packets = readb(priv->ramBase + 0x18f); */
1256
1257     //spin_unlock_irqrestore(&priv->spinlock, flags);
1258 }
1259
1260 static int netwave_rx(struct net_device *dev)
1261 {
1262     netwave_private *priv = netdev_priv(dev);
1263     u_char __iomem *ramBase = priv->ramBase;
1264     kio_addr_t iobase = dev->base_addr;
1265     u_char rxStatus;
1266     struct sk_buff *skb = NULL;
1267     unsigned int curBuffer,
1268                 rcvList;
1269     int rcvLen;
1270     int tmpcount = 0;
1271     int dataCount, dataOffset;
1272     int i;
1273     u_char *ptr;
1274         
1275     DEBUG(3, "xinw_rx: Receiving ... \n");
1276
1277     /* Receive max 10 packets for now. */
1278     for (i = 0; i < 10; i++) {
1279         /* Any packets? */
1280         wait_WOC(iobase);
1281         rxStatus = readb(ramBase + NETWAVE_EREG_RSER);          
1282         if ( !( rxStatus & 0x80)) /* No more packets */
1283             break;
1284                 
1285         /* Check if multicast/broadcast or other */
1286         /* multicast = (rxStatus & 0x20);  */
1287                 
1288         /* The receive list pointer and length of the packet */
1289         wait_WOC(iobase);
1290         rcvLen  = get_int16( ramBase + NETWAVE_EREG_RDP);
1291         rcvList = get_uint16( ramBase + NETWAVE_EREG_RDP + 2);
1292                 
1293         if (rcvLen < 0) {
1294             printk(KERN_DEBUG "netwave_rx: Receive packet with len %d\n", 
1295                    rcvLen);
1296             return 0;
1297         }
1298                 
1299         skb = dev_alloc_skb(rcvLen+5);
1300         if (skb == NULL) {
1301             DEBUG(1, "netwave_rx: Could not allocate an sk_buff of "
1302                   "length %d\n", rcvLen);
1303             ++priv->stats.rx_dropped; 
1304             /* Tell the adapter to skip the packet */
1305             wait_WOC(iobase);
1306             writeb(NETWAVE_CMD_SRP, ramBase + NETWAVE_EREG_CB + 0);
1307             writeb(NETWAVE_CMD_EOC, ramBase + NETWAVE_EREG_CB + 1);
1308             return 0;
1309         }
1310
1311         skb_reserve( skb, 2);  /* Align IP on 16 byte */
1312         skb_put( skb, rcvLen);
1313         skb->dev = dev;
1314
1315         /* Copy packet fragments to the skb data area */
1316         ptr = (u_char*) skb->data;
1317         curBuffer = rcvList;
1318         tmpcount = 0; 
1319         while ( tmpcount < rcvLen) {
1320             /* Get length and offset of current buffer */
1321             dataCount  = get_uint16( ramBase+curBuffer+2);
1322             dataOffset = get_uint16( ramBase+curBuffer+4);
1323                 
1324             copy_from_pc( ptr + tmpcount,
1325                           ramBase+curBuffer+dataOffset, dataCount);
1326
1327             tmpcount += dataCount;
1328                 
1329             /* Point to next buffer */
1330             curBuffer = get_uint16(ramBase + curBuffer);
1331         }
1332         
1333         skb->protocol = eth_type_trans(skb,dev);
1334         /* Queue packet for network layer */
1335         netif_rx(skb);
1336
1337         dev->last_rx = jiffies;
1338         priv->stats.rx_packets++;
1339         priv->stats.rx_bytes += rcvLen;
1340
1341         /* Got the packet, tell the adapter to skip it */
1342         wait_WOC(iobase);
1343         writeb(NETWAVE_CMD_SRP, ramBase + NETWAVE_EREG_CB + 0);
1344         writeb(NETWAVE_CMD_EOC, ramBase + NETWAVE_EREG_CB + 1);
1345         DEBUG(3, "Packet reception ok\n");
1346     }
1347     return 0;
1348 }
1349
1350 static int netwave_open(struct net_device *dev) {
1351     netwave_private *priv = netdev_priv(dev);
1352     struct pcmcia_device *link = priv->p_dev;
1353
1354     DEBUG(1, "netwave_open: starting.\n");
1355     
1356     if (!DEV_OK(link))
1357         return -ENODEV;
1358
1359     link->open++;
1360
1361     netif_start_queue(dev);
1362     netwave_reset(dev);
1363         
1364     return 0;
1365 }
1366
1367 static int netwave_close(struct net_device *dev) {
1368     netwave_private *priv = netdev_priv(dev);
1369     struct pcmcia_device *link = priv->p_dev;
1370
1371     DEBUG(1, "netwave_close: finishing.\n");
1372
1373     link->open--;
1374     netif_stop_queue(dev);
1375
1376     return 0;
1377 }
1378
1379 static struct pcmcia_device_id netwave_ids[] = {
1380         PCMCIA_DEVICE_PROD_ID12("Xircom", "CreditCard Netwave", 0x2e3ee845, 0x54e28a28),
1381         PCMCIA_DEVICE_NULL,
1382 };
1383 MODULE_DEVICE_TABLE(pcmcia, netwave_ids);
1384
1385 static struct pcmcia_driver netwave_driver = {
1386         .owner          = THIS_MODULE,
1387         .drv            = {
1388                 .name   = "netwave_cs",
1389         },
1390         .probe          = netwave_attach,
1391         .remove         = netwave_detach,
1392         .id_table       = netwave_ids,
1393         .suspend        = netwave_suspend,
1394         .resume         = netwave_resume,
1395 };
1396
1397 static int __init init_netwave_cs(void)
1398 {
1399         return pcmcia_register_driver(&netwave_driver);
1400 }
1401
1402 static void __exit exit_netwave_cs(void)
1403 {
1404         pcmcia_unregister_driver(&netwave_driver);
1405 }
1406
1407 module_init(init_netwave_cs);
1408 module_exit(exit_netwave_cs);
1409
1410 /* Set or clear the multicast filter for this adaptor.
1411    num_addrs == -1      Promiscuous mode, receive all packets
1412    num_addrs == 0       Normal mode, clear multicast list
1413    num_addrs > 0        Multicast mode, receive normal and MC packets, and do
1414    best-effort filtering.
1415  */
1416 static void set_multicast_list(struct net_device *dev)
1417 {
1418     kio_addr_t iobase = dev->base_addr;
1419     netwave_private *priv = netdev_priv(dev);
1420     u_char __iomem * ramBase = priv->ramBase;
1421     u_char  rcvMode = 0;
1422    
1423 #ifdef PCMCIA_DEBUG
1424     if (pc_debug > 2) {
1425         static int old;
1426         if (old != dev->mc_count) {
1427             old = dev->mc_count;
1428             DEBUG(0, "%s: setting Rx mode to %d addresses.\n",
1429                   dev->name, dev->mc_count);
1430         }
1431     }
1432 #endif
1433         
1434     if (dev->mc_count || (dev->flags & IFF_ALLMULTI)) {
1435         /* Multicast Mode */
1436         rcvMode = rxConfRxEna + rxConfAMP + rxConfBcast;
1437     } else if (dev->flags & IFF_PROMISC) {
1438         /* Promiscous mode */
1439         rcvMode = rxConfRxEna + rxConfPro + rxConfAMP + rxConfBcast;
1440     } else {
1441         /* Normal mode */
1442         rcvMode = rxConfRxEna + rxConfBcast;
1443     }
1444         
1445     /* printk("netwave set_multicast_list: rcvMode to %x\n", rcvMode);*/
1446     /* Now set receive mode */
1447     wait_WOC(iobase);
1448     writeb(NETWAVE_CMD_SRC, ramBase + NETWAVE_EREG_CB + 0);
1449     writeb(rcvMode, ramBase + NETWAVE_EREG_CB + 1);
1450     writeb(NETWAVE_CMD_EOC, ramBase + NETWAVE_EREG_CB + 2);
1451 }
1452 MODULE_LICENSE("GPL");