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