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