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