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