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