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