ray_cs.c 89.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10
/*=============================================================================
 *
 * A  PCMCIA client driver for the Raylink wireless LAN card.
 * The starting point for this module was the skeleton.c in the
 * PCMCIA 2.9.12 package written by David Hinds, dahinds@users.sourceforge.net
 *
 *
 * Copyright (c) 1998  Corey Thomas (corey@world.std.com)
 *
 * This driver is free software; you can redistribute it and/or modify
11
 * it under the terms of version 2 only of the GNU General Public License as
L
Linus Torvalds 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 * published by the Free Software Foundation.
 *
 * It is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 *
 * Changes:
 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 08/08/2000
 * - reorganize kmallocs in ray_attach, checking all for failure
 *   and releasing the previous allocations if one fails
 *
 * Daniele Bellucci <bellucda@tiscali.it> - 07/10/2003
 * - Audit copy_to_user in ioctl(SIOCGIWESSID)
30
 *
L
Linus Torvalds 已提交
31 32 33 34 35 36
=============================================================================*/

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/ptrace.h>
37
#include <linux/seq_file.h>
L
Linus Torvalds 已提交
38 39 40 41 42 43 44 45 46 47
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/timer.h>
#include <linux/init.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/if_arp.h>
#include <linux/ioport.h>
#include <linux/skbuff.h>
#include <linux/ethtool.h>
A
Al Viro 已提交
48
#include <linux/ieee80211.h>
L
Linus Torvalds 已提交
49 50 51 52 53 54 55 56 57

#include <pcmcia/cs_types.h>
#include <pcmcia/cs.h>
#include <pcmcia/cistpl.h>
#include <pcmcia/cisreg.h>
#include <pcmcia/ds.h>
#include <pcmcia/mem_op.h>

#include <linux/wireless.h>
58
#include <net/iw_handler.h>
L
Linus Torvalds 已提交
59 60 61 62 63 64 65 66 67

#include <asm/io.h>
#include <asm/system.h>
#include <asm/byteorder.h>
#include <asm/uaccess.h>

/* Warning : these stuff will slow down the driver... */
#define WIRELESS_SPY		/* Enable spying addresses */
/* Definitions we need for spy */
68 69
typedef struct iw_statistics iw_stats;
typedef u_char mac_addr[ETH_ALEN];	/* Hardware address */
L
Linus Torvalds 已提交
70 71 72 73 74 75

#include "rayctl.h"
#include "ray_cs.h"


/** Prototypes based on PCMCIA skeleton driver *******************************/
76
static int ray_config(struct pcmcia_device *link);
77
static void ray_release(struct pcmcia_device *link);
78
static void ray_detach(struct pcmcia_device *p_dev);
L
Linus Torvalds 已提交
79 80 81 82 83 84 85

/***** Prototypes indicated by device structure ******************************/
static int ray_dev_close(struct net_device *dev);
static int ray_dev_config(struct net_device *dev, struct ifmap *map);
static struct net_device_stats *ray_get_stats(struct net_device *dev);
static int ray_dev_init(struct net_device *dev);

86
static const struct ethtool_ops netdev_ethtool_ops;
L
Linus Torvalds 已提交
87 88

static int ray_open(struct net_device *dev);
89 90
static netdev_tx_t ray_dev_start_xmit(struct sk_buff *skb,
					    struct net_device *dev);
L
Linus Torvalds 已提交
91 92 93
static void set_multicast_list(struct net_device *dev);
static void ray_update_multi_list(struct net_device *dev, int all);
static int translate_frame(ray_dev_t *local, struct tx_msg __iomem *ptx,
94 95 96
			   unsigned char *data, int len);
static void ray_build_header(ray_dev_t *local, struct tx_msg __iomem *ptx,
			     UCHAR msg_type, unsigned char *data);
L
Linus Torvalds 已提交
97
static void untranslate(ray_dev_t *local, struct sk_buff *skb, int len);
98 99
static iw_stats *ray_get_wireless_stats(struct net_device *dev);
static const struct iw_handler_def ray_handler_def;
L
Linus Torvalds 已提交
100 101 102 103 104 105 106 107 108 109

/***** Prototypes for raylink functions **************************************/
static int asc_to_int(char a);
static void authenticate(ray_dev_t *local);
static int build_auth_frame(ray_dev_t *local, UCHAR *dest, int auth_type);
static void authenticate_timeout(u_long);
static int get_free_ccs(ray_dev_t *local);
static int get_free_tx_ccs(ray_dev_t *local);
static void init_startup_params(ray_dev_t *local);
static int parse_addr(char *in_str, UCHAR *out);
110
static int ray_hw_xmit(unsigned char *data, int len, struct net_device *dev, UCHAR type);
L
Linus Torvalds 已提交
111 112 113 114 115 116 117
static int ray_init(struct net_device *dev);
static int interrupt_ecf(ray_dev_t *local, int ccs);
static void ray_reset(struct net_device *dev);
static void ray_update_parm(struct net_device *dev, UCHAR objid, UCHAR *value, int len);
static void verify_dl_startup(u_long);

/* Prototypes for interrpt time functions **********************************/
118
static irqreturn_t ray_interrupt(int reg, void *dev_id);
L
Linus Torvalds 已提交
119
static void clear_interrupt(ray_dev_t *local);
120 121
static void rx_deauthenticate(ray_dev_t *local, struct rcs __iomem *prcs,
			      unsigned int pkt_addr, int rx_len);
L
Linus Torvalds 已提交
122 123 124 125
static int copy_from_rx_buff(ray_dev_t *local, UCHAR *dest, int pkt_addr, int len);
static void ray_rx(struct net_device *dev, ray_dev_t *local, struct rcs __iomem *prcs);
static void release_frag_chain(ray_dev_t *local, struct rcs __iomem *prcs);
static void rx_authenticate(ray_dev_t *local, struct rcs __iomem *prcs,
126 127 128
			    unsigned int pkt_addr, int rx_len);
static void rx_data(struct net_device *dev, struct rcs __iomem *prcs,
		    unsigned int pkt_addr, int rx_len);
L
Linus Torvalds 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
static void associate(ray_dev_t *local);

/* Card command functions */
static int dl_startup_params(struct net_device *dev);
static void join_net(u_long local);
static void start_net(u_long local);
/* void start_net(ray_dev_t *local); */

/*===========================================================================*/
/* Parameters that can be set with 'insmod' */

/* ADHOC=0, Infrastructure=1 */
static int net_type = ADHOC;

/* Hop dwell time in Kus (1024 us units defined by 802.11) */
static int hop_dwell = 128;

/* Beacon period in Kus */
static int beacon_period = 256;

/* power save mode (0 = off, 1 = save power) */
static int psm;

/* String for network's Extended Service Set ID. 32 Characters max */
static char *essid;

/* Default to encapsulation unless translation requested */
static int translate = 1;

static int country = USA;

static int sniffer;

static int bc;

/* 48 bit physical card address if overriding card's real physical
 * address is required.  Since IEEE 802.11 addresses are 48 bits
 * like ethernet, an int can't be used, so a string is used. To
 * allow use of addresses starting with a decimal digit, the first
 * character must be a letter and will be ignored. This letter is
 * followed by up to 12 hex digits which are the address.  If less
 * than 12 digits are used, the address will be left filled with 0's.
 * Note that bit 0 of the first byte is the broadcast bit, and evil
 * things will happen if it is not 0 in a card address.
 */
static char *phy_addr = NULL;


177
/* A struct pcmcia_device structure has fields for most things that are needed
L
Linus Torvalds 已提交
178 179
   to keep track of a socket, but there will usually be some device
   specific information that also needs to be kept track of.  The
180
   'priv' pointer in a struct pcmcia_device structure can be used to point to
L
Linus Torvalds 已提交
181 182 183 184
   a device-specific private data structure, like this.
*/
static unsigned int ray_mem_speed = 500;

185 186 187
/* WARNING: THIS DRIVER IS NOT CAPABLE OF HANDLING MULTIPLE DEVICES! */
static struct pcmcia_device *this_device = NULL;

L
Linus Torvalds 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
MODULE_AUTHOR("Corey Thomas <corey@world.std.com>");
MODULE_DESCRIPTION("Raylink/WebGear wireless LAN driver");
MODULE_LICENSE("GPL");

module_param(net_type, int, 0);
module_param(hop_dwell, int, 0);
module_param(beacon_period, int, 0);
module_param(psm, int, 0);
module_param(essid, charp, 0);
module_param(translate, int, 0);
module_param(country, int, 0);
module_param(sniffer, int, 0);
module_param(bc, int, 0);
module_param(phy_addr, charp, 0);
module_param(ray_mem_speed, int, 0);

static UCHAR b5_default_startup_parms[] = {
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
	0, 0,			/* Adhoc station */
	'L', 'I', 'N', 'U', 'X', 0, 0, 0,	/* 32 char ESSID */
	0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0,
	1, 0,			/* Active scan, CA Mode */
	0, 0, 0, 0, 0, 0,	/* No default MAC addr  */
	0x7f, 0xff,		/* Frag threshold */
	0x00, 0x80,		/* Hop time 128 Kus */
	0x01, 0x00,		/* Beacon period 256 Kus */
	0x01, 0x07, 0xa3,	/* DTIM, retries, ack timeout */
	0x1d, 0x82, 0x4e,	/* SIFS, DIFS, PIFS */
	0x7f, 0xff,		/* RTS threshold */
	0x04, 0xe2, 0x38, 0xA4,	/* scan_dwell, max_scan_dwell */
	0x05,			/* assoc resp timeout thresh */
	0x08, 0x02, 0x08,	/* adhoc, infra, super cycle max */
	0,			/* Promiscuous mode */
	0x0c, 0x0bd,		/* Unique word */
	0x32,			/* Slot time */
	0xff, 0xff,		/* roam-low snr, low snr count */
	0x05, 0xff,		/* Infra, adhoc missed bcn thresh */
	0x01, 0x0b, 0x4f,	/* USA, hop pattern, hop pat length */
L
Linus Torvalds 已提交
227
/* b4 - b5 differences start here */
228 229 230 231 232 233 234 235 236
	0x00, 0x3f,		/* CW max */
	0x00, 0x0f,		/* CW min */
	0x04, 0x08,		/* Noise gain, limit offset */
	0x28, 0x28,		/* det rssi, med busy offsets */
	7,			/* det sync thresh */
	0, 2, 2,		/* test mode, min, max */
	0,			/* allow broadcast SSID probe resp */
	0, 0,			/* privacy must start, can join */
	2, 0, 0, 0, 0, 0, 0, 0	/* basic rate set */
L
Linus Torvalds 已提交
237 238 239
};

static UCHAR b4_default_startup_parms[] = {
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
	0, 0,			/* Adhoc station */
	'L', 'I', 'N', 'U', 'X', 0, 0, 0,	/* 32 char ESSID */
	0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0,
	1, 0,			/* Active scan, CA Mode */
	0, 0, 0, 0, 0, 0,	/* No default MAC addr  */
	0x7f, 0xff,		/* Frag threshold */
	0x02, 0x00,		/* Hop time */
	0x00, 0x01,		/* Beacon period */
	0x01, 0x07, 0xa3,	/* DTIM, retries, ack timeout */
	0x1d, 0x82, 0xce,	/* SIFS, DIFS, PIFS */
	0x7f, 0xff,		/* RTS threshold */
	0xfb, 0x1e, 0xc7, 0x5c,	/* scan_dwell, max_scan_dwell */
	0x05,			/* assoc resp timeout thresh */
	0x04, 0x02, 0x4,	/* adhoc, infra, super cycle max */
	0,			/* Promiscuous mode */
	0x0c, 0x0bd,		/* Unique word */
	0x4e,			/* Slot time (TBD seems wrong) */
	0xff, 0xff,		/* roam-low snr, low snr count */
	0x05, 0xff,		/* Infra, adhoc missed bcn thresh */
	0x01, 0x0b, 0x4e,	/* USA, hop pattern, hop pat length */
L
Linus Torvalds 已提交
262
/* b4 - b5 differences start here */
263 264 265 266 267
	0x3f, 0x0f,		/* CW max, min */
	0x04, 0x08,		/* Noise gain, limit offset */
	0x28, 0x28,		/* det rssi, med busy offsets */
	7,			/* det sync thresh */
	0, 2, 2			/* test mode, min, max */
L
Linus Torvalds 已提交
268
};
269

L
Linus Torvalds 已提交
270
/*===========================================================================*/
271
static unsigned char eth2_llc[] = { 0xaa, 0xaa, 3, 0, 0, 0 };
L
Linus Torvalds 已提交
272 273

static char hop_pattern_length[] = { 1,
274 275 276 277 278
	USA_HOP_MOD, EUROPE_HOP_MOD,
	JAPAN_HOP_MOD, KOREA_HOP_MOD,
	SPAIN_HOP_MOD, FRANCE_HOP_MOD,
	ISRAEL_HOP_MOD, AUSTRALIA_HOP_MOD,
	JAPAN_TEST_HOP_MOD
L
Linus Torvalds 已提交
279 280
};

281 282
static char rcsid[] =
    "Raylink/WebGear wireless LAN - Corey <Thomas corey@world.std.com>";
L
Linus Torvalds 已提交
283

284 285 286 287 288 289 290 291 292 293 294 295 296
static const struct net_device_ops ray_netdev_ops = {
	.ndo_init 		= ray_dev_init,
	.ndo_open 		= ray_open,
	.ndo_stop 		= ray_dev_close,
	.ndo_start_xmit		= ray_dev_start_xmit,
	.ndo_set_config		= ray_dev_config,
	.ndo_get_stats		= ray_get_stats,
	.ndo_set_multicast_list = set_multicast_list,
	.ndo_change_mtu		= eth_change_mtu,
	.ndo_set_mac_address 	= eth_mac_addr,
	.ndo_validate_addr	= eth_validate_addr,
};

L
Linus Torvalds 已提交
297 298 299 300 301 302 303 304
/*=============================================================================
    ray_attach() creates an "instance" of the driver, allocating
    local data structures for one device.  The device is registered
    with Card Services.
    The dev_link structure is initialized, but we don't actually
    configure the card at this point -- we wait until we receive a
    card insertion event.
=============================================================================*/
305
static int ray_probe(struct pcmcia_device *p_dev)
L
Linus Torvalds 已提交
306
{
307 308 309
	ray_dev_t *local;
	struct net_device *dev;

310
	dev_dbg(&p_dev->dev, "ray_attach()\n");
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325

	/* Allocate space for private device-specific data */
	dev = alloc_etherdev(sizeof(ray_dev_t));
	if (!dev)
		goto fail_alloc_dev;

	local = netdev_priv(dev);
	local->finder = p_dev;

	/* The io structure describes IO port mapping. None used here */
	p_dev->io.NumPorts1 = 0;
	p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
	p_dev->io.IOAddrLines = 5;

	/* Interrupt setup. For PCMCIA, driver takes what's given */
326
	p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING;
327 328 329 330 331 332 333 334 335 336 337 338 339
	p_dev->irq.Handler = &ray_interrupt;

	/* General socket configuration */
	p_dev->conf.Attributes = CONF_ENABLE_IRQ;
	p_dev->conf.IntType = INT_MEMORY_AND_IO;
	p_dev->conf.ConfigIndex = 1;

	p_dev->priv = dev;

	local->finder = p_dev;
	local->card_status = CARD_INSERTED;
	local->authentication_state = UNAUTHENTICATED;
	local->num_multi = 0;
340
	dev_dbg(&p_dev->dev, "ray_attach p_dev = %p,  dev = %p,  local = %p, intr = %p\n",
341 342 343
	      p_dev, dev, local, &ray_interrupt);

	/* Raylink entries in the device structure */
344
	dev->netdev_ops = &ray_netdev_ops;
345 346
	SET_ETHTOOL_OPS(dev, &netdev_ethtool_ops);
	dev->wireless_handlers = &ray_handler_def;
J
Jean Tourrilhes 已提交
347
#ifdef WIRELESS_SPY
348 349 350
	local->wireless_data.spy_data = &local->spy_data;
	dev->wireless_data = &local->wireless_data;
#endif /* WIRELESS_SPY */
L
Linus Torvalds 已提交
351 352


353
	dev_dbg(&p_dev->dev, "ray_cs ray_attach calling ether_setup.)\n");
354
	netif_stop_queue(dev);
L
Linus Torvalds 已提交
355

356
	init_timer(&local->timer);
L
Linus Torvalds 已提交
357

358 359
	this_device = p_dev;
	return ray_config(p_dev);
L
Linus Torvalds 已提交
360 361

fail_alloc_dev:
362
	return -ENOMEM;
L
Linus Torvalds 已提交
363
} /* ray_attach */
364

L
Linus Torvalds 已提交
365 366 367 368 369 370
/*=============================================================================
    This deletes a driver "instance".  The device is de-registered
    with Card Services.  If it has been released, all local data
    structures are freed.  Otherwise, the structures will be freed
    when the device is released.
=============================================================================*/
371
static void ray_detach(struct pcmcia_device *link)
L
Linus Torvalds 已提交
372
{
373 374
	struct net_device *dev;
	ray_dev_t *local;
L
Linus Torvalds 已提交
375

376
	dev_dbg(&link->dev, "ray_detach\n");
L
Linus Torvalds 已提交
377

378 379
	this_device = NULL;
	dev = link->priv;
380

381
	ray_release(link);
382

383 384
	local = netdev_priv(dev);
	del_timer(&local->timer);
L
Linus Torvalds 已提交
385

386 387 388 389 390
	if (link->priv) {
		if (link->dev_node)
			unregister_netdev(dev);
		free_netdev(dev);
	}
391
	dev_dbg(&link->dev, "ray_cs ray_detach ending\n");
L
Linus Torvalds 已提交
392
} /* ray_detach */
393

L
Linus Torvalds 已提交
394 395 396 397 398 399
/*=============================================================================
    ray_config() is run after a CARD_INSERTION event
    is received, to configure the PCMCIA socket, and to make the
    ethernet device available to the system.
=============================================================================*/
#define MAX_TUPLE_SIZE 128
400
static int ray_config(struct pcmcia_device *link)
L
Linus Torvalds 已提交
401
{
402
	int ret = 0;
403 404 405 406 407 408
	int i;
	win_req_t req;
	memreq_t mem;
	struct net_device *dev = (struct net_device *)link->priv;
	ray_dev_t *local = netdev_priv(dev);

409
	dev_dbg(&link->dev, "ray_config\n");
410 411 412 413 414 415 416 417 418 419 420

	/* Determine card type and firmware version */
	printk(KERN_INFO "ray_cs Detected: %s%s%s%s\n",
	       link->prod_id[0] ? link->prod_id[0] : " ",
	       link->prod_id[1] ? link->prod_id[1] : " ",
	       link->prod_id[2] ? link->prod_id[2] : " ",
	       link->prod_id[3] ? link->prod_id[3] : " ");

	/* Now allocate an interrupt line.  Note that this does not
	   actually assign a handler to the interrupt.
	 */
421 422 423
	ret = pcmcia_request_irq(link, &link->irq);
	if (ret)
		goto failed;
424 425 426 427 428
	dev->irq = link->irq.AssignedIRQ;

	/* This actually configures the PCMCIA socket -- setting up
	   the I/O windows and the interrupt mapping.
	 */
429 430 431
	ret = pcmcia_request_configuration(link, &link->conf);
	if (ret)
		goto failed;
L
Linus Torvalds 已提交
432 433

/*** Set up 32k window for shared memory (transmit and control) ************/
434 435 436 437 438
	req.Attributes =
	    WIN_DATA_WIDTH_8 | WIN_MEMORY_TYPE_CM | WIN_ENABLE | WIN_USE_WAIT;
	req.Base = 0;
	req.Size = 0x8000;
	req.AccessSpeed = ray_mem_speed;
439
	ret = pcmcia_request_window(link, &req, &link->win);
440 441
	if (ret)
		goto failed;
442 443
	mem.CardOffset = 0x0000;
	mem.Page = 0;
444
	ret = pcmcia_map_mem_page(link, link->win, &mem);
445 446
	if (ret)
		goto failed;
447
	local->sram = ioremap(req.Base, req.Size);
L
Linus Torvalds 已提交
448 449

/*** Set up 16k window for shared memory (receive buffer) ***************/
450 451 452 453 454
	req.Attributes =
	    WIN_DATA_WIDTH_8 | WIN_MEMORY_TYPE_CM | WIN_ENABLE | WIN_USE_WAIT;
	req.Base = 0;
	req.Size = 0x4000;
	req.AccessSpeed = ray_mem_speed;
455
	ret = pcmcia_request_window(link, &req, &local->rmem_handle);
456 457
	if (ret)
		goto failed;
458 459
	mem.CardOffset = 0x8000;
	mem.Page = 0;
460
	ret = pcmcia_map_mem_page(link, local->rmem_handle, &mem);
461 462
	if (ret)
		goto failed;
463
	local->rmem = ioremap(req.Base, req.Size);
L
Linus Torvalds 已提交
464 465

/*** Set up window for attribute memory ***********************************/
466 467 468 469 470
	req.Attributes =
	    WIN_DATA_WIDTH_8 | WIN_MEMORY_TYPE_AM | WIN_ENABLE | WIN_USE_WAIT;
	req.Base = 0;
	req.Size = 0x1000;
	req.AccessSpeed = ray_mem_speed;
471
	ret = pcmcia_request_window(link, &req, &local->amem_handle);
472 473
	if (ret)
		goto failed;
474 475
	mem.CardOffset = 0x0000;
	mem.Page = 0;
476
	ret = pcmcia_map_mem_page(link, local->amem_handle, &mem);
477 478
	if (ret)
		goto failed;
479 480
	local->amem = ioremap(req.Base, req.Size);

481 482 483
	dev_dbg(&link->dev, "ray_config sram=%p\n", local->sram);
	dev_dbg(&link->dev, "ray_config rmem=%p\n", local->rmem);
	dev_dbg(&link->dev, "ray_config amem=%p\n", local->amem);
484 485 486 487 488
	if (ray_init(dev) < 0) {
		ray_release(link);
		return -ENODEV;
	}

489
	SET_NETDEV_DEV(dev, &link->dev);
490 491 492 493 494 495 496 497 498 499 500 501 502 503
	i = register_netdev(dev);
	if (i != 0) {
		printk("ray_config register_netdev() failed\n");
		ray_release(link);
		return i;
	}

	strcpy(local->node.dev_name, dev->name);
	link->dev_node = &local->node;

	printk(KERN_INFO "%s: RayLink, irq %d, hw_addr %pM\n",
	       dev->name, dev->irq, dev->dev_addr);

	return 0;
L
Linus Torvalds 已提交
504

505
failed:
506 507
	ray_release(link);
	return -ENODEV;
L
Linus Torvalds 已提交
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
} /* ray_config */

static inline struct ccs __iomem *ccs_base(ray_dev_t *dev)
{
	return dev->sram + CCS_BASE;
}

static inline struct rcs __iomem *rcs_base(ray_dev_t *dev)
{
	/*
	 * This looks nonsensical, since there is a separate
	 * RCS_BASE. But the difference between a "struct rcs"
	 * and a "struct ccs" ends up being in the _index_ off
	 * the base, so the base pointer is the same for both
	 * ccs/rcs.
	 */
	return dev->sram + CCS_BASE;
}

/*===========================================================================*/
static int ray_init(struct net_device *dev)
{
530 531 532 533 534
	int i;
	UCHAR *p;
	struct ccs __iomem *pccs;
	ray_dev_t *local = netdev_priv(dev);
	struct pcmcia_device *link = local->finder;
535
	dev_dbg(&link->dev, "ray_init(0x%p)\n", dev);
536
	if (!(pcmcia_dev_present(link))) {
537
		dev_dbg(&link->dev, "ray_init - device not present\n");
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
		return -1;
	}

	local->net_type = net_type;
	local->sta_type = TYPE_STA;

	/* Copy the startup results to local memory */
	memcpy_fromio(&local->startup_res, local->sram + ECF_TO_HOST_BASE,
		      sizeof(struct startup_res_6));

	/* Check Power up test status and get mac address from card */
	if (local->startup_res.startup_word != 0x80) {
		printk(KERN_INFO "ray_init ERROR card status = %2x\n",
		       local->startup_res.startup_word);
		local->card_status = CARD_INIT_ERROR;
		return -1;
	}

	local->fw_ver = local->startup_res.firmware_version[0];
	local->fw_bld = local->startup_res.firmware_version[1];
	local->fw_var = local->startup_res.firmware_version[2];
559
	dev_dbg(&link->dev, "ray_init firmware version %d.%d \n", local->fw_ver,
560 561 562 563 564
	      local->fw_bld);

	local->tib_length = 0x20;
	if ((local->fw_ver == 5) && (local->fw_bld >= 30))
		local->tib_length = local->startup_res.tib_length;
565
	dev_dbg(&link->dev, "ray_init tib_length = 0x%02x\n", local->tib_length);
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
	/* Initialize CCS's to buffer free state */
	pccs = ccs_base(local);
	for (i = 0; i < NUMBER_OF_CCS; i++) {
		writeb(CCS_BUFFER_FREE, &(pccs++)->buffer_status);
	}
	init_startup_params(local);

	/* copy mac address to startup parameters */
	if (parse_addr(phy_addr, local->sparm.b4.a_mac_addr)) {
		p = local->sparm.b4.a_mac_addr;
	} else {
		memcpy(&local->sparm.b4.a_mac_addr,
		       &local->startup_res.station_addr, ADDRLEN);
		p = local->sparm.b4.a_mac_addr;
	}

	clear_interrupt(local);	/* Clear any interrupt from the card */
	local->card_status = CARD_AWAITING_PARAM;
584
	dev_dbg(&link->dev, "ray_init ending\n");
585
	return 0;
L
Linus Torvalds 已提交
586
} /* ray_init */
587

L
Linus Torvalds 已提交
588 589 590 591
/*===========================================================================*/
/* Download startup parameters to the card and command it to read them       */
static int dl_startup_params(struct net_device *dev)
{
592 593 594 595 596
	int ccsindex;
	ray_dev_t *local = netdev_priv(dev);
	struct ccs __iomem *pccs;
	struct pcmcia_device *link = local->finder;

597
	dev_dbg(&link->dev, "dl_startup_params entered\n");
598
	if (!(pcmcia_dev_present(link))) {
599
		dev_dbg(&link->dev, "ray_cs dl_startup_params - device not present\n");
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
		return -1;
	}

	/* Copy parameters to host to ECF area */
	if (local->fw_ver == 0x55)
		memcpy_toio(local->sram + HOST_TO_ECF_BASE, &local->sparm.b4,
			    sizeof(struct b4_startup_params));
	else
		memcpy_toio(local->sram + HOST_TO_ECF_BASE, &local->sparm.b5,
			    sizeof(struct b5_startup_params));

	/* Fill in the CCS fields for the ECF */
	if ((ccsindex = get_free_ccs(local)) < 0)
		return -1;
	local->dl_param_ccs = ccsindex;
	pccs = ccs_base(local) + ccsindex;
	writeb(CCS_DOWNLOAD_STARTUP_PARAMS, &pccs->cmd);
617
	dev_dbg(&link->dev, "dl_startup_params start ccsindex = %d\n",
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
	      local->dl_param_ccs);
	/* Interrupt the firmware to process the command */
	if (interrupt_ecf(local, ccsindex)) {
		printk(KERN_INFO "ray dl_startup_params failed - "
		       "ECF not ready for intr\n");
		local->card_status = CARD_DL_PARAM_ERROR;
		writeb(CCS_BUFFER_FREE, &(pccs++)->buffer_status);
		return -2;
	}
	local->card_status = CARD_DL_PARAM;
	/* Start kernel timer to wait for dl startup to complete. */
	local->timer.expires = jiffies + HZ / 2;
	local->timer.data = (long)local;
	local->timer.function = &verify_dl_startup;
	add_timer(&local->timer);
633
	dev_dbg(&link->dev,
634 635
	      "ray_cs dl_startup_params started timer for verify_dl_startup\n");
	return 0;
L
Linus Torvalds 已提交
636
} /* dl_startup_params */
637

L
Linus Torvalds 已提交
638 639 640
/*===========================================================================*/
static void init_startup_params(ray_dev_t *local)
{
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
	int i;

	if (country > JAPAN_TEST)
		country = USA;
	else if (country < USA)
		country = USA;
	/* structure for hop time and beacon period is defined here using
	 * New 802.11D6.1 format.  Card firmware is still using old format
	 * until version 6.
	 *    Before                    After
	 *    a_hop_time ms byte        a_hop_time ms byte
	 *    a_hop_time 2s byte        a_hop_time ls byte
	 *    a_hop_time ls byte        a_beacon_period ms byte
	 *    a_beacon_period           a_beacon_period ls byte
	 *
	 *    a_hop_time = uS           a_hop_time = KuS
	 *    a_beacon_period = hops    a_beacon_period = KuS
	 *//* 64ms = 010000 */
	if (local->fw_ver == 0x55) {
		memcpy((UCHAR *) &local->sparm.b4, b4_default_startup_parms,
		       sizeof(struct b4_startup_params));
		/* Translate sane kus input values to old build 4/5 format */
		/* i = hop time in uS truncated to 3 bytes */
		i = (hop_dwell * 1024) & 0xffffff;
		local->sparm.b4.a_hop_time[0] = (i >> 16) & 0xff;
		local->sparm.b4.a_hop_time[1] = (i >> 8) & 0xff;
		local->sparm.b4.a_beacon_period[0] = 0;
		local->sparm.b4.a_beacon_period[1] =
		    ((beacon_period / hop_dwell) - 1) & 0xff;
		local->sparm.b4.a_curr_country_code = country;
		local->sparm.b4.a_hop_pattern_length =
		    hop_pattern_length[(int)country] - 1;
		if (bc) {
			local->sparm.b4.a_ack_timeout = 0x50;
			local->sparm.b4.a_sifs = 0x3f;
		}
	} else { /* Version 5 uses real kus values */
		memcpy((UCHAR *) &local->sparm.b5, b5_default_startup_parms,
		       sizeof(struct b5_startup_params));

		local->sparm.b5.a_hop_time[0] = (hop_dwell >> 8) & 0xff;
		local->sparm.b5.a_hop_time[1] = hop_dwell & 0xff;
		local->sparm.b5.a_beacon_period[0] =
		    (beacon_period >> 8) & 0xff;
		local->sparm.b5.a_beacon_period[1] = beacon_period & 0xff;
		if (psm)
			local->sparm.b5.a_power_mgt_state = 1;
		local->sparm.b5.a_curr_country_code = country;
		local->sparm.b5.a_hop_pattern_length =
		    hop_pattern_length[(int)country];
	}

	local->sparm.b4.a_network_type = net_type & 0x01;
	local->sparm.b4.a_acting_as_ap_status = TYPE_STA;

	if (essid != NULL)
		strncpy(local->sparm.b4.a_current_ess_id, essid, ESSID_SIZE);
} /* init_startup_params */

L
Linus Torvalds 已提交
700 701 702
/*===========================================================================*/
static void verify_dl_startup(u_long data)
{
703 704 705 706
	ray_dev_t *local = (ray_dev_t *) data;
	struct ccs __iomem *pccs = ccs_base(local) + local->dl_param_ccs;
	UCHAR status;
	struct pcmcia_device *link = local->finder;
L
Linus Torvalds 已提交
707

708
	if (!(pcmcia_dev_present(link))) {
709
		dev_dbg(&link->dev, "ray_cs verify_dl_startup - device not present\n");
710 711
		return;
	}
712 713
#if 0
	{
714 715 716 717 718 719 720 721 722 723 724
		int i;
		printk(KERN_DEBUG
		       "verify_dl_startup parameters sent via ccs %d:\n",
		       local->dl_param_ccs);
		for (i = 0; i < sizeof(struct b5_startup_params); i++) {
			printk(" %2x",
			       (unsigned int)readb(local->sram +
						   HOST_TO_ECF_BASE + i));
		}
		printk("\n");
	}
L
Linus Torvalds 已提交
725 726
#endif

727 728 729 730 731 732 733 734 735 736 737 738 739 740
	status = readb(&pccs->buffer_status);
	if (status != CCS_BUFFER_FREE) {
		printk(KERN_INFO
		       "Download startup params failed.  Status = %d\n",
		       status);
		local->card_status = CARD_DL_PARAM_ERROR;
		return;
	}
	if (local->sparm.b4.a_network_type == ADHOC)
		start_net((u_long) local);
	else
		join_net((u_long) local);

	return;
L
Linus Torvalds 已提交
741
} /* end verify_dl_startup */
742

L
Linus Torvalds 已提交
743 744 745 746
/*===========================================================================*/
/* Command card to start a network */
static void start_net(u_long data)
{
747 748 749 750 751
	ray_dev_t *local = (ray_dev_t *) data;
	struct ccs __iomem *pccs;
	int ccsindex;
	struct pcmcia_device *link = local->finder;
	if (!(pcmcia_dev_present(link))) {
752
		dev_dbg(&link->dev, "ray_cs start_net - device not present\n");
753 754 755 756 757 758 759 760 761 762
		return;
	}
	/* Fill in the CCS fields for the ECF */
	if ((ccsindex = get_free_ccs(local)) < 0)
		return;
	pccs = ccs_base(local) + ccsindex;
	writeb(CCS_START_NETWORK, &pccs->cmd);
	writeb(0, &pccs->var.start_network.update_param);
	/* Interrupt the firmware to process the command */
	if (interrupt_ecf(local, ccsindex)) {
763
		dev_dbg(&link->dev, "ray start net failed - card not ready for intr\n");
764 765 766 767 768
		writeb(CCS_BUFFER_FREE, &(pccs++)->buffer_status);
		return;
	}
	local->card_status = CARD_DOING_ACQ;
	return;
L
Linus Torvalds 已提交
769
} /* end start_net */
770

L
Linus Torvalds 已提交
771 772 773 774
/*===========================================================================*/
/* Command card to join a network */
static void join_net(u_long data)
{
775 776 777 778 779 780 781
	ray_dev_t *local = (ray_dev_t *) data;

	struct ccs __iomem *pccs;
	int ccsindex;
	struct pcmcia_device *link = local->finder;

	if (!(pcmcia_dev_present(link))) {
782
		dev_dbg(&link->dev, "ray_cs join_net - device not present\n");
783 784 785 786 787 788 789 790 791 792 793
		return;
	}
	/* Fill in the CCS fields for the ECF */
	if ((ccsindex = get_free_ccs(local)) < 0)
		return;
	pccs = ccs_base(local) + ccsindex;
	writeb(CCS_JOIN_NETWORK, &pccs->cmd);
	writeb(0, &pccs->var.join_network.update_param);
	writeb(0, &pccs->var.join_network.net_initiated);
	/* Interrupt the firmware to process the command */
	if (interrupt_ecf(local, ccsindex)) {
794
		dev_dbg(&link->dev, "ray join net failed - card not ready for intr\n");
795 796 797 798 799
		writeb(CCS_BUFFER_FREE, &(pccs++)->buffer_status);
		return;
	}
	local->card_status = CARD_DOING_ACQ;
	return;
L
Linus Torvalds 已提交
800
}
801

L
Linus Torvalds 已提交
802 803 804 805 806
/*============================================================================
    After a card is removed, ray_release() will unregister the net
    device, and release the PCMCIA configuration.  If the device is
    still open, this will be postponed until it is closed.
=============================================================================*/
807
static void ray_release(struct pcmcia_device *link)
L
Linus Torvalds 已提交
808
{
809 810 811 812
	struct net_device *dev = link->priv;
	ray_dev_t *local = netdev_priv(dev);
	int i;

813
	dev_dbg(&link->dev, "ray_release\n");
814 815 816 817 818 819 820

	del_timer(&local->timer);

	iounmap(local->sram);
	iounmap(local->rmem);
	iounmap(local->amem);
	/* Do bother checking to see if these succeed or not */
821
	i = pcmcia_release_window(link, local->amem_handle);
822
	if (i != 0)
823
		dev_dbg(&link->dev, "ReleaseWindow(local->amem) ret = %x\n", i);
824
	i = pcmcia_release_window(link, local->rmem_handle);
825
	if (i != 0)
826
		dev_dbg(&link->dev, "ReleaseWindow(local->rmem) ret = %x\n", i);
827 828
	pcmcia_disable_device(link);

829
	dev_dbg(&link->dev, "ray_release ending\n");
L
Linus Torvalds 已提交
830 831
}

832
static int ray_suspend(struct pcmcia_device *link)
833 834 835
{
	struct net_device *dev = link->priv;

836
	if (link->open)
837
		netif_device_detach(dev);
838 839 840 841

	return 0;
}

842
static int ray_resume(struct pcmcia_device *link)
843 844 845
{
	struct net_device *dev = link->priv;

846
	if (link->open) {
847 848 849
		ray_reset(dev);
		netif_device_attach(dev);
	}
850 851 852 853

	return 0;
}

L
Linus Torvalds 已提交
854
/*===========================================================================*/
855
static int ray_dev_init(struct net_device *dev)
L
Linus Torvalds 已提交
856 857
{
#ifdef RAY_IMMEDIATE_INIT
858 859 860 861 862
	int i;
#endif /* RAY_IMMEDIATE_INIT */
	ray_dev_t *local = netdev_priv(dev);
	struct pcmcia_device *link = local->finder;

863
	dev_dbg(&link->dev, "ray_dev_init(dev=%p)\n", dev);
864
	if (!(pcmcia_dev_present(link))) {
865
		dev_dbg(&link->dev, "ray_dev_init - device not present\n");
866 867
		return -1;
	}
L
Linus Torvalds 已提交
868
#ifdef RAY_IMMEDIATE_INIT
869 870 871 872 873 874 875 876 877 878
	/* Download startup parameters */
	if ((i = dl_startup_params(dev)) < 0) {
		printk(KERN_INFO "ray_dev_init dl_startup_params failed - "
		       "returns 0x%x\n", i);
		return -1;
	}
#else /* RAY_IMMEDIATE_INIT */
	/* Postpone the card init so that we can still configure the card,
	 * for example using the Wireless Extensions. The init will happen
	 * in ray_open() - Jean II */
879
	dev_dbg(&link->dev,
880 881 882 883 884
	      "ray_dev_init: postponing card init to ray_open() ; Status = %d\n",
	      local->card_status);
#endif /* RAY_IMMEDIATE_INIT */

	/* copy mac and broadcast addresses to linux device */
885
	memcpy(dev->dev_addr, &local->sparm.b4.a_mac_addr, ADDRLEN);
886 887
	memset(dev->broadcast, 0xff, ETH_ALEN);

888
	dev_dbg(&link->dev, "ray_dev_init ending\n");
889
	return 0;
L
Linus Torvalds 已提交
890
}
891

L
Linus Torvalds 已提交
892 893 894
/*===========================================================================*/
static int ray_dev_config(struct net_device *dev, struct ifmap *map)
{
895 896 897
	ray_dev_t *local = netdev_priv(dev);
	struct pcmcia_device *link = local->finder;
	/* Dummy routine to satisfy device structure */
898
	dev_dbg(&link->dev, "ray_dev_config(dev=%p,ifmap=%p)\n", dev, map);
899
	if (!(pcmcia_dev_present(link))) {
900
		dev_dbg(&link->dev, "ray_dev_config - device not present\n");
901 902
		return -1;
	}
L
Linus Torvalds 已提交
903

904
	return 0;
L
Linus Torvalds 已提交
905
}
906

L
Linus Torvalds 已提交
907
/*===========================================================================*/
908 909
static netdev_tx_t ray_dev_start_xmit(struct sk_buff *skb,
					    struct net_device *dev)
L
Linus Torvalds 已提交
910
{
911 912 913 914
	ray_dev_t *local = netdev_priv(dev);
	struct pcmcia_device *link = local->finder;
	short length = skb->len;

915
	if (!pcmcia_dev_present(link)) {
916
		dev_dbg(&link->dev, "ray_dev_start_xmit - device not present\n");
917 918
		dev_kfree_skb(skb);
		return NETDEV_TX_OK;
919
	}
920

921
	dev_dbg(&link->dev, "ray_dev_start_xmit(skb=%p, dev=%p)\n", skb, dev);
922
	if (local->authentication_state == NEED_TO_AUTH) {
923
		dev_dbg(&link->dev, "ray_cs Sending authentication request.\n");
924 925 926
		if (!build_auth_frame(local, local->auth_id, OPEN_AUTH_REQUEST)) {
			local->authentication_state = AUTHENTICATED;
			netif_stop_queue(dev);
927
			return NETDEV_TX_BUSY;
928 929 930 931 932
		}
	}

	if (length < ETH_ZLEN) {
		if (skb_padto(skb, ETH_ZLEN))
933
			return NETDEV_TX_OK;
934 935 936 937 938 939
		length = ETH_ZLEN;
	}
	switch (ray_hw_xmit(skb->data, length, dev, DATA_TYPE)) {
	case XMIT_NO_CCS:
	case XMIT_NEED_AUTH:
		netif_stop_queue(dev);
940
		return NETDEV_TX_BUSY;
941 942 943 944 945 946 947
	case XMIT_NO_INTR:
	case XMIT_MSG_BAD:
	case XMIT_OK:
	default:
		dev->trans_start = jiffies;
		dev_kfree_skb(skb);
	}
948

949
	return NETDEV_TX_OK;
L
Linus Torvalds 已提交
950
} /* ray_dev_start_xmit */
951

L
Linus Torvalds 已提交
952
/*===========================================================================*/
953 954 955 956 957 958 959 960 961 962
static int ray_hw_xmit(unsigned char *data, int len, struct net_device *dev,
		       UCHAR msg_type)
{
	ray_dev_t *local = netdev_priv(dev);
	struct ccs __iomem *pccs;
	int ccsindex;
	int offset;
	struct tx_msg __iomem *ptx;	/* Address of xmit buffer in PC space */
	short int addr;		/* Address of xmit buffer in card space */

963
	pr_debug("ray_hw_xmit(data=%p, len=%d, dev=%p)\n", data, len, dev);
964 965 966 967 968
	if (len + TX_HEADER_LENGTH > TX_BUF_SIZE) {
		printk(KERN_INFO "ray_hw_xmit packet too large: %d bytes\n",
		       len);
		return XMIT_MSG_BAD;
	}
L
Linus Torvalds 已提交
969 970
	switch (ccsindex = get_free_tx_ccs(local)) {
	case ECCSBUSY:
971
		pr_debug("ray_hw_xmit tx_ccs table busy\n");
L
Linus Torvalds 已提交
972
	case ECCSFULL:
973
		pr_debug("ray_hw_xmit No free tx ccs\n");
L
Linus Torvalds 已提交
974
	case ECARDGONE:
975 976
		netif_stop_queue(dev);
		return XMIT_NO_CCS;
L
Linus Torvalds 已提交
977 978 979
	default:
		break;
	}
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
	addr = TX_BUF_BASE + (ccsindex << 11);

	if (msg_type == DATA_TYPE) {
		local->stats.tx_bytes += len;
		local->stats.tx_packets++;
	}

	ptx = local->sram + addr;

	ray_build_header(local, ptx, msg_type, data);
	if (translate) {
		offset = translate_frame(local, ptx, data, len);
	} else { /* Encapsulate frame */
		/* TBD TIB length will move address of ptx->var */
		memcpy_toio(&ptx->var, data, len);
		offset = 0;
	}

	/* fill in the CCS */
	pccs = ccs_base(local) + ccsindex;
	len += TX_HEADER_LENGTH + offset;
	writeb(CCS_TX_REQUEST, &pccs->cmd);
	writeb(addr >> 8, &pccs->var.tx_request.tx_data_ptr[0]);
	writeb(local->tib_length, &pccs->var.tx_request.tx_data_ptr[1]);
	writeb(len >> 8, &pccs->var.tx_request.tx_data_length[0]);
	writeb(len & 0xff, &pccs->var.tx_request.tx_data_length[1]);
L
Linus Torvalds 已提交
1006
/* TBD still need psm_cam? */
1007 1008 1009
	writeb(PSM_CAM, &pccs->var.tx_request.pow_sav_mode);
	writeb(local->net_default_tx_rate, &pccs->var.tx_request.tx_rate);
	writeb(0, &pccs->var.tx_request.antenna);
1010
	pr_debug("ray_hw_xmit default_tx_rate = 0x%x\n",
1011 1012 1013 1014
	      local->net_default_tx_rate);

	/* Interrupt the firmware to process the command */
	if (interrupt_ecf(local, ccsindex)) {
1015
		pr_debug("ray_hw_xmit failed - ECF not ready for intr\n");
L
Linus Torvalds 已提交
1016 1017 1018 1019
/* TBD very inefficient to copy packet to buffer, and then not
   send it, but the alternative is to queue the messages and that
   won't be done for a while.  Maybe set tbusy until a CCS is free?
*/
1020 1021 1022 1023
		writeb(CCS_BUFFER_FREE, &pccs->buffer_status);
		return XMIT_NO_INTR;
	}
	return XMIT_OK;
L
Linus Torvalds 已提交
1024
} /* end ray_hw_xmit */
1025

L
Linus Torvalds 已提交
1026
/*===========================================================================*/
1027 1028 1029 1030 1031
static int translate_frame(ray_dev_t *local, struct tx_msg __iomem *ptx,
			   unsigned char *data, int len)
{
	__be16 proto = ((struct ethhdr *)data)->h_proto;
	if (ntohs(proto) >= 1536) { /* DIX II ethernet frame */
1032
		pr_debug("ray_cs translate_frame DIX II\n");
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
		/* Copy LLC header to card buffer */
		memcpy_toio(&ptx->var, eth2_llc, sizeof(eth2_llc));
		memcpy_toio(((void __iomem *)&ptx->var) + sizeof(eth2_llc),
			    (UCHAR *) &proto, 2);
		if (proto == htons(ETH_P_AARP) || proto == htons(ETH_P_IPX)) {
			/* This is the selective translation table, only 2 entries */
			writeb(0xf8,
			       &((struct snaphdr_t __iomem *)ptx->var)->org[3]);
		}
		/* Copy body of ethernet packet without ethernet header */
		memcpy_toio((void __iomem *)&ptx->var +
			    sizeof(struct snaphdr_t), data + ETH_HLEN,
			    len - ETH_HLEN);
		return (int)sizeof(struct snaphdr_t) - ETH_HLEN;
	} else { /* already  802 type, and proto is length */
1048
		pr_debug("ray_cs translate_frame 802\n");
1049
		if (proto == htons(0xffff)) { /* evil netware IPX 802.3 without LLC */
1050
			pr_debug("ray_cs translate_frame evil IPX\n");
1051 1052 1053 1054 1055 1056 1057
			memcpy_toio(&ptx->var, data + ETH_HLEN, len - ETH_HLEN);
			return 0 - ETH_HLEN;
		}
		memcpy_toio(&ptx->var, data + ETH_HLEN, len - ETH_HLEN);
		return 0 - ETH_HLEN;
	}
	/* TBD do other frame types */
L
Linus Torvalds 已提交
1058
} /* end translate_frame */
1059

L
Linus Torvalds 已提交
1060
/*===========================================================================*/
1061 1062
static void ray_build_header(ray_dev_t *local, struct tx_msg __iomem *ptx,
			     UCHAR msg_type, unsigned char *data)
L
Linus Torvalds 已提交
1063
{
1064
	writeb(PROTOCOL_VER | msg_type, &ptx->mac.frame_ctl_1);
L
Linus Torvalds 已提交
1065
/*** IEEE 802.11 Address field assignments *************
1066 1067 1068 1069 1070
		TODS	FROMDS	addr_1		addr_2		addr_3	addr_4
Adhoc		0	0	dest		src (terminal)	BSSID	N/A
AP to Terminal	0	1	dest		AP(BSSID)	source	N/A
Terminal to AP	1	0	AP(BSSID)	src (terminal)	dest	N/A
AP to AP	1	1	dest AP		src AP		dest	source
L
Linus Torvalds 已提交
1071
*******************************************************/
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
	if (local->net_type == ADHOC) {
		writeb(0, &ptx->mac.frame_ctl_2);
		memcpy_toio(ptx->mac.addr_1, ((struct ethhdr *)data)->h_dest,
			    2 * ADDRLEN);
		memcpy_toio(ptx->mac.addr_3, local->bss_id, ADDRLEN);
	} else { /* infrastructure */

		if (local->sparm.b4.a_acting_as_ap_status) {
			writeb(FC2_FROM_DS, &ptx->mac.frame_ctl_2);
			memcpy_toio(ptx->mac.addr_1,
				    ((struct ethhdr *)data)->h_dest, ADDRLEN);
			memcpy_toio(ptx->mac.addr_2, local->bss_id, 6);
			memcpy_toio(ptx->mac.addr_3,
				    ((struct ethhdr *)data)->h_source, ADDRLEN);
		} else { /* Terminal */

			writeb(FC2_TO_DS, &ptx->mac.frame_ctl_2);
			memcpy_toio(ptx->mac.addr_1, local->bss_id, ADDRLEN);
			memcpy_toio(ptx->mac.addr_2,
				    ((struct ethhdr *)data)->h_source, ADDRLEN);
			memcpy_toio(ptx->mac.addr_3,
				    ((struct ethhdr *)data)->h_dest, ADDRLEN);
		}
	}
L
Linus Torvalds 已提交
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
} /* end encapsulate_frame */

/*===========================================================================*/

static void netdev_get_drvinfo(struct net_device *dev,
			       struct ethtool_drvinfo *info)
{
	strcpy(info->driver, "ray_cs");
}

1106
static const struct ethtool_ops netdev_ethtool_ops = {
1107
	.get_drvinfo = netdev_get_drvinfo,
L
Linus Torvalds 已提交
1108 1109 1110 1111
};

/*====================================================================*/

J
Jean Tourrilhes 已提交
1112 1113 1114 1115 1116
/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get protocol name
 */
static int ray_get_name(struct net_device *dev,
1117
			struct iw_request_info *info, char *cwrq, char *extra)
L
Linus Torvalds 已提交
1118
{
J
Jean Tourrilhes 已提交
1119 1120 1121
	strcpy(cwrq, "IEEE 802.11-FH");
	return 0;
}
L
Linus Torvalds 已提交
1122

J
Jean Tourrilhes 已提交
1123 1124 1125 1126 1127 1128
/*------------------------------------------------------------------*/
/*
 * Wireless Handler : set frequency
 */
static int ray_set_freq(struct net_device *dev,
			struct iw_request_info *info,
1129
			struct iw_freq *fwrq, char *extra)
J
Jean Tourrilhes 已提交
1130
{
1131
	ray_dev_t *local = netdev_priv(dev);
1132
	int err = -EINPROGRESS;	/* Call commit handler */
L
Linus Torvalds 已提交
1133

J
Jean Tourrilhes 已提交
1134
	/* Reject if card is already initialised */
1135
	if (local->card_status != CARD_AWAITING_PARAM)
J
Jean Tourrilhes 已提交
1136
		return -EBUSY;
L
Linus Torvalds 已提交
1137

J
Jean Tourrilhes 已提交
1138 1139 1140 1141 1142
	/* Setting by channel number */
	if ((fwrq->m > USA_HOP_MOD) || (fwrq->e > 0))
		err = -EOPNOTSUPP;
	else
		local->sparm.b5.a_hop_pattern = fwrq->m;
L
Linus Torvalds 已提交
1143

J
Jean Tourrilhes 已提交
1144 1145
	return err;
}
1146

J
Jean Tourrilhes 已提交
1147 1148 1149 1150 1151 1152
/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get frequency
 */
static int ray_get_freq(struct net_device *dev,
			struct iw_request_info *info,
1153
			struct iw_freq *fwrq, char *extra)
J
Jean Tourrilhes 已提交
1154
{
1155
	ray_dev_t *local = netdev_priv(dev);
L
Linus Torvalds 已提交
1156

J
Jean Tourrilhes 已提交
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
	fwrq->m = local->sparm.b5.a_hop_pattern;
	fwrq->e = 0;
	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : set ESSID
 */
static int ray_set_essid(struct net_device *dev,
			 struct iw_request_info *info,
1168
			 struct iw_point *dwrq, char *extra)
J
Jean Tourrilhes 已提交
1169
{
1170
	ray_dev_t *local = netdev_priv(dev);
J
Jean Tourrilhes 已提交
1171 1172

	/* Reject if card is already initialised */
1173
	if (local->card_status != CARD_AWAITING_PARAM)
J
Jean Tourrilhes 已提交
1174 1175 1176
		return -EBUSY;

	/* Check if we asked for `any' */
1177
	if (dwrq->flags == 0) {
L
Linus Torvalds 已提交
1178
		/* Corey : can you do that ? */
J
Jean Tourrilhes 已提交
1179 1180
		return -EOPNOTSUPP;
	} else {
L
Linus Torvalds 已提交
1181
		/* Check the size of the string */
1182
		if (dwrq->length > IW_ESSID_MAX_SIZE) {
J
Jean Tourrilhes 已提交
1183
			return -E2BIG;
L
Linus Torvalds 已提交
1184 1185 1186
		}

		/* Set the ESSID in the card */
J
Jean Tourrilhes 已提交
1187 1188
		memset(local->sparm.b5.a_current_ess_id, 0, IW_ESSID_MAX_SIZE);
		memcpy(local->sparm.b5.a_current_ess_id, extra, dwrq->length);
L
Linus Torvalds 已提交
1189 1190
	}

1191
	return -EINPROGRESS;	/* Call commit handler */
J
Jean Tourrilhes 已提交
1192
}
L
Linus Torvalds 已提交
1193

J
Jean Tourrilhes 已提交
1194 1195 1196 1197 1198 1199
/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get ESSID
 */
static int ray_get_essid(struct net_device *dev,
			 struct iw_request_info *info,
1200
			 struct iw_point *dwrq, char *extra)
J
Jean Tourrilhes 已提交
1201
{
1202
	ray_dev_t *local = netdev_priv(dev);
J
Jean Tourrilhes 已提交
1203 1204 1205 1206 1207

	/* Get the essid that was set */
	memcpy(extra, local->sparm.b5.a_current_ess_id, IW_ESSID_MAX_SIZE);

	/* Push it out ! */
1208
	dwrq->length = strlen(extra);
1209
	dwrq->flags = 1;	/* active */
J
Jean Tourrilhes 已提交
1210 1211 1212 1213 1214 1215 1216 1217 1218

	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get AP address
 */
static int ray_get_wap(struct net_device *dev,
1219 1220
		       struct iw_request_info *info,
		       struct sockaddr *awrq, char *extra)
J
Jean Tourrilhes 已提交
1221
{
1222
	ray_dev_t *local = netdev_priv(dev);
J
Jean Tourrilhes 已提交
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235

	memcpy(awrq->sa_data, local->bss_id, ETH_ALEN);
	awrq->sa_family = ARPHRD_ETHER;

	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : set Bit-Rate
 */
static int ray_set_rate(struct net_device *dev,
			struct iw_request_info *info,
1236
			struct iw_param *vwrq, char *extra)
J
Jean Tourrilhes 已提交
1237
{
1238
	ray_dev_t *local = netdev_priv(dev);
J
Jean Tourrilhes 已提交
1239 1240

	/* Reject if card is already initialised */
1241
	if (local->card_status != CARD_AWAITING_PARAM)
J
Jean Tourrilhes 已提交
1242 1243 1244
		return -EBUSY;

	/* Check if rate is in range */
1245
	if ((vwrq->value != 1000000) && (vwrq->value != 2000000))
J
Jean Tourrilhes 已提交
1246 1247 1248
		return -EINVAL;

	/* Hack for 1.5 Mb/s instead of 2 Mb/s */
1249 1250
	if ((local->fw_ver == 0x55) &&	/* Please check */
	    (vwrq->value == 2000000))
J
Jean Tourrilhes 已提交
1251
		local->net_default_tx_rate = 3;
L
Linus Torvalds 已提交
1252
	else
1253
		local->net_default_tx_rate = vwrq->value / 500000;
J
Jean Tourrilhes 已提交
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263

	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get Bit-Rate
 */
static int ray_get_rate(struct net_device *dev,
			struct iw_request_info *info,
1264
			struct iw_param *vwrq, char *extra)
J
Jean Tourrilhes 已提交
1265
{
1266
	ray_dev_t *local = netdev_priv(dev);
J
Jean Tourrilhes 已提交
1267

1268 1269
	if (local->net_default_tx_rate == 3)
		vwrq->value = 2000000;	/* Hum... */
J
Jean Tourrilhes 已提交
1270 1271
	else
		vwrq->value = local->net_default_tx_rate * 500000;
1272
	vwrq->fixed = 0;	/* We are in auto mode */
J
Jean Tourrilhes 已提交
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282

	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : set RTS threshold
 */
static int ray_set_rts(struct net_device *dev,
		       struct iw_request_info *info,
1283
		       struct iw_param *vwrq, char *extra)
J
Jean Tourrilhes 已提交
1284
{
1285
	ray_dev_t *local = netdev_priv(dev);
J
Jean Tourrilhes 已提交
1286 1287 1288
	int rthr = vwrq->value;

	/* Reject if card is already initialised */
1289
	if (local->card_status != CARD_AWAITING_PARAM)
J
Jean Tourrilhes 已提交
1290 1291 1292
		return -EBUSY;

	/* if(wrq->u.rts.fixed == 0) we should complain */
1293
	if (vwrq->disabled)
J
Jean Tourrilhes 已提交
1294 1295
		rthr = 32767;
	else {
1296
		if ((rthr < 0) || (rthr > 2347))   /* What's the max packet size ??? */
J
Jean Tourrilhes 已提交
1297 1298
			return -EINVAL;
	}
L
Linus Torvalds 已提交
1299 1300 1301
	local->sparm.b5.a_rts_threshold[0] = (rthr >> 8) & 0xFF;
	local->sparm.b5.a_rts_threshold[1] = rthr & 0xFF;

1302
	return -EINPROGRESS;	/* Call commit handler */
J
Jean Tourrilhes 已提交
1303
}
L
Linus Torvalds 已提交
1304

J
Jean Tourrilhes 已提交
1305 1306 1307 1308 1309 1310
/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get RTS threshold
 */
static int ray_get_rts(struct net_device *dev,
		       struct iw_request_info *info,
1311
		       struct iw_param *vwrq, char *extra)
J
Jean Tourrilhes 已提交
1312
{
1313
	ray_dev_t *local = netdev_priv(dev);
J
Jean Tourrilhes 已提交
1314 1315

	vwrq->value = (local->sparm.b5.a_rts_threshold[0] << 8)
1316
	    + local->sparm.b5.a_rts_threshold[1];
J
Jean Tourrilhes 已提交
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
	vwrq->disabled = (vwrq->value == 32767);
	vwrq->fixed = 1;

	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : set Fragmentation threshold
 */
static int ray_set_frag(struct net_device *dev,
			struct iw_request_info *info,
1329
			struct iw_param *vwrq, char *extra)
J
Jean Tourrilhes 已提交
1330
{
1331
	ray_dev_t *local = netdev_priv(dev);
J
Jean Tourrilhes 已提交
1332 1333 1334
	int fthr = vwrq->value;

	/* Reject if card is already initialised */
1335
	if (local->card_status != CARD_AWAITING_PARAM)
J
Jean Tourrilhes 已提交
1336
		return -EBUSY;
L
Linus Torvalds 已提交
1337 1338

	/* if(wrq->u.frag.fixed == 0) should complain */
1339
	if (vwrq->disabled)
J
Jean Tourrilhes 已提交
1340 1341
		fthr = 32767;
	else {
1342
		if ((fthr < 256) || (fthr > 2347))	/* To check out ! */
J
Jean Tourrilhes 已提交
1343 1344
			return -EINVAL;
	}
L
Linus Torvalds 已提交
1345 1346 1347
	local->sparm.b5.a_frag_threshold[0] = (fthr >> 8) & 0xFF;
	local->sparm.b5.a_frag_threshold[1] = fthr & 0xFF;

1348
	return -EINPROGRESS;	/* Call commit handler */
J
Jean Tourrilhes 已提交
1349 1350 1351 1352 1353 1354 1355 1356
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get Fragmentation threshold
 */
static int ray_get_frag(struct net_device *dev,
			struct iw_request_info *info,
1357
			struct iw_param *vwrq, char *extra)
J
Jean Tourrilhes 已提交
1358
{
1359
	ray_dev_t *local = netdev_priv(dev);
J
Jean Tourrilhes 已提交
1360 1361

	vwrq->value = (local->sparm.b5.a_frag_threshold[0] << 8)
1362
	    + local->sparm.b5.a_frag_threshold[1];
J
Jean Tourrilhes 已提交
1363 1364
	vwrq->disabled = (vwrq->value == 32767);
	vwrq->fixed = 1;
L
Linus Torvalds 已提交
1365

J
Jean Tourrilhes 已提交
1366 1367
	return 0;
}
L
Linus Torvalds 已提交
1368

J
Jean Tourrilhes 已提交
1369 1370 1371 1372 1373
/*------------------------------------------------------------------*/
/*
 * Wireless Handler : set Mode of Operation
 */
static int ray_set_mode(struct net_device *dev,
1374
			struct iw_request_info *info, __u32 *uwrq, char *extra)
J
Jean Tourrilhes 已提交
1375
{
1376
	ray_dev_t *local = netdev_priv(dev);
1377
	int err = -EINPROGRESS;	/* Call commit handler */
L
Linus Torvalds 已提交
1378 1379
	char card_mode = 1;

J
Jean Tourrilhes 已提交
1380
	/* Reject if card is already initialised */
1381
	if (local->card_status != CARD_AWAITING_PARAM)
J
Jean Tourrilhes 已提交
1382 1383
		return -EBUSY;

1384
	switch (*uwrq) {
L
Linus Torvalds 已提交
1385
	case IW_MODE_ADHOC:
J
Jean Tourrilhes 已提交
1386
		card_mode = 0;
1387
		/* Fall through */
L
Linus Torvalds 已提交
1388
	case IW_MODE_INFRA:
J
Jean Tourrilhes 已提交
1389 1390
		local->sparm.b5.a_network_type = card_mode;
		break;
L
Linus Torvalds 已提交
1391
	default:
J
Jean Tourrilhes 已提交
1392
		err = -EINVAL;
L
Linus Torvalds 已提交
1393 1394
	}

J
Jean Tourrilhes 已提交
1395 1396
	return err;
}
L
Linus Torvalds 已提交
1397

J
Jean Tourrilhes 已提交
1398 1399 1400 1401 1402
/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get Mode of Operation
 */
static int ray_get_mode(struct net_device *dev,
1403
			struct iw_request_info *info, __u32 *uwrq, char *extra)
J
Jean Tourrilhes 已提交
1404
{
1405
	ray_dev_t *local = netdev_priv(dev);
L
Linus Torvalds 已提交
1406

1407
	if (local->sparm.b5.a_network_type)
J
Jean Tourrilhes 已提交
1408 1409 1410
		*uwrq = IW_MODE_INFRA;
	else
		*uwrq = IW_MODE_ADHOC;
L
Linus Torvalds 已提交
1411

J
Jean Tourrilhes 已提交
1412 1413
	return 0;
}
L
Linus Torvalds 已提交
1414

J
Jean Tourrilhes 已提交
1415 1416 1417 1418 1419 1420
/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get range info
 */
static int ray_get_range(struct net_device *dev,
			 struct iw_request_info *info,
1421
			 struct iw_point *dwrq, char *extra)
J
Jean Tourrilhes 已提交
1422
{
1423
	struct iw_range *range = (struct iw_range *)extra;
J
Jean Tourrilhes 已提交
1424

1425
	memset((char *)range, 0, sizeof(struct iw_range));
J
Jean Tourrilhes 已提交
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435

	/* Set the length (very important for backward compatibility) */
	dwrq->length = sizeof(struct iw_range);

	/* Set the Wireless Extension versions */
	range->we_version_compiled = WIRELESS_EXT;
	range->we_version_source = 9;

	/* Set information in the range struct */
	range->throughput = 1.1 * 1000 * 1000;	/* Put the right number here */
1436
	range->num_channels = hop_pattern_length[(int)country];
J
Jean Tourrilhes 已提交
1437 1438 1439 1440 1441 1442 1443 1444 1445
	range->num_frequency = 0;
	range->max_qual.qual = 0;
	range->max_qual.level = 255;	/* What's the correct value ? */
	range->max_qual.noise = 255;	/* Idem */
	range->num_bitrates = 2;
	range->bitrate[0] = 1000000;	/* 1 Mb/s */
	range->bitrate[1] = 2000000;	/* 2 Mb/s */
	return 0;
}
L
Linus Torvalds 已提交
1446

J
Jean Tourrilhes 已提交
1447 1448 1449 1450 1451 1452
/*------------------------------------------------------------------*/
/*
 * Wireless Private Handler : set framing mode
 */
static int ray_set_framing(struct net_device *dev,
			   struct iw_request_info *info,
1453
			   union iwreq_data *wrqu, char *extra)
J
Jean Tourrilhes 已提交
1454 1455
{
	translate = *(extra);	/* Set framing mode */
L
Linus Torvalds 已提交
1456

J
Jean Tourrilhes 已提交
1457 1458
	return 0;
}
L
Linus Torvalds 已提交
1459

J
Jean Tourrilhes 已提交
1460 1461 1462 1463 1464 1465
/*------------------------------------------------------------------*/
/*
 * Wireless Private Handler : get framing mode
 */
static int ray_get_framing(struct net_device *dev,
			   struct iw_request_info *info,
1466
			   union iwreq_data *wrqu, char *extra)
J
Jean Tourrilhes 已提交
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
{
	*(extra) = translate;

	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Private Handler : get country
 */
static int ray_get_country(struct net_device *dev,
			   struct iw_request_info *info,
1479
			   union iwreq_data *wrqu, char *extra)
J
Jean Tourrilhes 已提交
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
{
	*(extra) = country;

	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Commit handler : called after a bunch of SET operations
 */
1490 1491 1492 1493
static int ray_commit(struct net_device *dev, struct iw_request_info *info,	/* NULL */
		      void *zwrq,	/* NULL */
		      char *extra)
{ /* NULL */
J
Jean Tourrilhes 已提交
1494 1495 1496 1497 1498 1499 1500
	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Stats handler : return Wireless Stats
 */
1501
static iw_stats *ray_get_wireless_stats(struct net_device *dev)
L
Linus Torvalds 已提交
1502
{
1503 1504 1505
	ray_dev_t *local = netdev_priv(dev);
	struct pcmcia_device *link = local->finder;
	struct status __iomem *p = local->sram + STATUS_BASE;
L
Linus Torvalds 已提交
1506

1507
	local->wstats.status = local->card_status;
L
Linus Torvalds 已提交
1508
#ifdef WIRELESS_SPY
1509 1510 1511 1512 1513 1514 1515 1516 1517
	if ((local->spy_data.spy_number > 0)
	    && (local->sparm.b5.a_network_type == 0)) {
		/* Get it from the first node in spy list */
		local->wstats.qual.qual = local->spy_data.spy_stat[0].qual;
		local->wstats.qual.level = local->spy_data.spy_stat[0].level;
		local->wstats.qual.noise = local->spy_data.spy_stat[0].noise;
		local->wstats.qual.updated =
		    local->spy_data.spy_stat[0].updated;
	}
L
Linus Torvalds 已提交
1518 1519
#endif /* WIRELESS_SPY */

1520 1521 1522 1523
	if (pcmcia_dev_present(link)) {
		local->wstats.qual.noise = readb(&p->rxnoise);
		local->wstats.qual.updated |= 4;
	}
L
Linus Torvalds 已提交
1524

1525
	return &local->wstats;
L
Linus Torvalds 已提交
1526
} /* end ray_get_wireless_stats */
J
Jean Tourrilhes 已提交
1527 1528 1529 1530 1531 1532

/*------------------------------------------------------------------*/
/*
 * Structures to export the Wireless Handlers
 */

1533
static const iw_handler ray_handler[] = {
1534 1535 1536 1537 1538 1539 1540
	IW_HANDLER(SIOCSIWCOMMIT, (iw_handler)ray_commit),
	IW_HANDLER(SIOCGIWNAME, (iw_handler)ray_get_name),
	IW_HANDLER(SIOCSIWFREQ, (iw_handler)ray_set_freq),
	IW_HANDLER(SIOCGIWFREQ, (iw_handler)ray_get_freq),
	IW_HANDLER(SIOCSIWMODE, (iw_handler)ray_set_mode),
	IW_HANDLER(SIOCGIWMODE, (iw_handler)ray_get_mode),
	IW_HANDLER(SIOCGIWRANGE, (iw_handler)ray_get_range),
J
Jean Tourrilhes 已提交
1541
#ifdef WIRELESS_SPY
1542 1543 1544 1545
	IW_HANDLER(SIOCSIWSPY, iw_handler_set_spy),
	IW_HANDLER(SIOCGIWSPY, iw_handler_get_spy),
	IW_HANDLER(SIOCSIWTHRSPY, iw_handler_set_thrspy),
	IW_HANDLER(SIOCGIWTHRSPY, iw_handler_get_thrspy),
1546
#endif /* WIRELESS_SPY */
1547 1548 1549 1550 1551 1552 1553 1554 1555
	IW_HANDLER(SIOCGIWAP, (iw_handler)ray_get_wap),
	IW_HANDLER(SIOCSIWESSID, (iw_handler)ray_set_essid),
	IW_HANDLER(SIOCGIWESSID, (iw_handler)ray_get_essid),
	IW_HANDLER(SIOCSIWRATE, (iw_handler)ray_set_rate),
	IW_HANDLER(SIOCGIWRATE, (iw_handler)ray_get_rate),
	IW_HANDLER(SIOCSIWRTS, (iw_handler)ray_set_rts),
	IW_HANDLER(SIOCGIWRTS, (iw_handler)ray_get_rts),
	IW_HANDLER(SIOCSIWFRAG, (iw_handler)ray_set_frag),
	IW_HANDLER(SIOCGIWFRAG, (iw_handler)ray_get_frag),
J
Jean Tourrilhes 已提交
1556 1557
};

1558
#define SIOCSIPFRAMING	SIOCIWFIRSTPRIV	/* Set framing mode */
J
Jean Tourrilhes 已提交
1559 1560 1561
#define SIOCGIPFRAMING	SIOCIWFIRSTPRIV + 1	/* Get framing mode */
#define SIOCGIPCOUNTRY	SIOCIWFIRSTPRIV + 3	/* Get country code */

1562
static const iw_handler ray_private_handler[] = {
1563 1564 1565
	[0] = (iw_handler) ray_set_framing,
	[1] = (iw_handler) ray_get_framing,
	[3] = (iw_handler) ray_get_country,
J
Jean Tourrilhes 已提交
1566 1567
};

1568
static const struct iw_priv_args ray_private_args[] = {
J
Jean Tourrilhes 已提交
1569
/* cmd,		set_args,	get_args,	name */
1570 1571 1572 1573 1574 1575
	{SIOCSIPFRAMING, IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | 1, 0,
	 "set_framing"},
	{SIOCGIPFRAMING, 0, IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | 1,
	 "get_framing"},
	{SIOCGIPCOUNTRY, 0, IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | 1,
	 "get_country"},
J
Jean Tourrilhes 已提交
1576 1577
};

1578 1579 1580
static const struct iw_handler_def ray_handler_def = {
	.num_standard = ARRAY_SIZE(ray_handler),
	.num_private = ARRAY_SIZE(ray_private_handler),
1581
	.num_private_args = ARRAY_SIZE(ray_private_args),
1582 1583 1584
	.standard = ray_handler,
	.private = ray_private_handler,
	.private_args = ray_private_args,
J
Jean Tourrilhes 已提交
1585 1586 1587
	.get_wireless_stats = ray_get_wireless_stats,
};

L
Linus Torvalds 已提交
1588 1589 1590
/*===========================================================================*/
static int ray_open(struct net_device *dev)
{
1591 1592 1593
	ray_dev_t *local = netdev_priv(dev);
	struct pcmcia_device *link;
	link = local->finder;
L
Linus Torvalds 已提交
1594

1595
	dev_dbg(&link->dev, "ray_open('%s')\n", dev->name);
L
Linus Torvalds 已提交
1596

1597 1598 1599
	if (link->open == 0)
		local->num_multi = 0;
	link->open++;
L
Linus Torvalds 已提交
1600

1601 1602 1603
	/* If the card is not started, time to start it ! - Jean II */
	if (local->card_status == CARD_AWAITING_PARAM) {
		int i;
L
Linus Torvalds 已提交
1604

1605
		dev_dbg(&link->dev, "ray_open: doing init now !\n");
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620

		/* Download startup parameters */
		if ((i = dl_startup_params(dev)) < 0) {
			printk(KERN_INFO
			       "ray_dev_init dl_startup_params failed - "
			       "returns 0x%x\n", i);
			return -1;
		}
	}

	if (sniffer)
		netif_stop_queue(dev);
	else
		netif_start_queue(dev);

1621
	dev_dbg(&link->dev, "ray_open ending\n");
1622
	return 0;
L
Linus Torvalds 已提交
1623
} /* end ray_open */
1624

L
Linus Torvalds 已提交
1625 1626 1627
/*===========================================================================*/
static int ray_dev_close(struct net_device *dev)
{
1628 1629 1630
	ray_dev_t *local = netdev_priv(dev);
	struct pcmcia_device *link;
	link = local->finder;
L
Linus Torvalds 已提交
1631

1632
	dev_dbg(&link->dev, "ray_dev_close('%s')\n", dev->name);
L
Linus Torvalds 已提交
1633

1634 1635
	link->open--;
	netif_stop_queue(dev);
L
Linus Torvalds 已提交
1636

1637 1638 1639 1640
	/* In here, we should stop the hardware (stop card from beeing active)
	 * and set local->card_status to CARD_AWAITING_PARAM, so that while the
	 * card is closed we can chage its configuration.
	 * Probably also need a COR reset to get sane state - Jean II */
L
Linus Torvalds 已提交
1641

1642
	return 0;
L
Linus Torvalds 已提交
1643
} /* end ray_dev_close */
1644

L
Linus Torvalds 已提交
1645
/*===========================================================================*/
1646 1647
static void ray_reset(struct net_device *dev)
{
1648
	pr_debug("ray_reset entered\n");
1649
	return;
L
Linus Torvalds 已提交
1650
}
1651

L
Linus Torvalds 已提交
1652 1653 1654 1655 1656
/*===========================================================================*/
/* Cause a firmware interrupt if it is ready for one                         */
/* Return nonzero if not ready                                               */
static int interrupt_ecf(ray_dev_t *local, int ccs)
{
1657 1658 1659 1660
	int i = 50;
	struct pcmcia_device *link = local->finder;

	if (!(pcmcia_dev_present(link))) {
1661
		dev_dbg(&link->dev, "ray_cs interrupt_ecf - device not present\n");
1662 1663
		return -1;
	}
1664
	dev_dbg(&link->dev, "interrupt_ecf(local=%p, ccs = 0x%x\n", local, ccs);
1665 1666 1667 1668 1669 1670

	while (i &&
	       (readb(local->amem + CIS_OFFSET + ECF_INTR_OFFSET) &
		ECF_INTR_SET))
		i--;
	if (i == 0) {
1671
		dev_dbg(&link->dev, "ray_cs interrupt_ecf card not ready for interrupt\n");
1672 1673
		return -1;
	}
L
Linus Torvalds 已提交
1674
	/* Fill the mailbox, then kick the card */
1675 1676 1677
	writeb(ccs, local->sram + SCB_BASE);
	writeb(ECF_INTR_SET, local->amem + CIS_OFFSET + ECF_INTR_OFFSET);
	return 0;
L
Linus Torvalds 已提交
1678
} /* interrupt_ecf */
1679

L
Linus Torvalds 已提交
1680 1681 1682 1683 1684
/*===========================================================================*/
/* Get next free transmit CCS                                                */
/* Return - index of current tx ccs                                          */
static int get_free_tx_ccs(ray_dev_t *local)
{
1685 1686 1687
	int i;
	struct ccs __iomem *pccs = ccs_base(local);
	struct pcmcia_device *link = local->finder;
L
Linus Torvalds 已提交
1688

1689
	if (!(pcmcia_dev_present(link))) {
1690
		dev_dbg(&link->dev, "ray_cs get_free_tx_ccs - device not present\n");
1691 1692
		return ECARDGONE;
	}
L
Linus Torvalds 已提交
1693

1694
	if (test_and_set_bit(0, &local->tx_ccs_lock)) {
1695
		dev_dbg(&link->dev, "ray_cs tx_ccs_lock busy\n");
1696 1697
		return ECCSBUSY;
	}
L
Linus Torvalds 已提交
1698

1699 1700 1701 1702
	for (i = 0; i < NUMBER_OF_TX_CCS; i++) {
		if (readb(&(pccs + i)->buffer_status) == CCS_BUFFER_FREE) {
			writeb(CCS_BUFFER_BUSY, &(pccs + i)->buffer_status);
			writeb(CCS_END_LIST, &(pccs + i)->link);
L
Linus Torvalds 已提交
1703
			local->tx_ccs_lock = 0;
1704 1705 1706
			return i;
		}
	}
L
Linus Torvalds 已提交
1707
	local->tx_ccs_lock = 0;
1708
	dev_dbg(&link->dev, "ray_cs ERROR no free tx CCS for raylink card\n");
1709
	return ECCSFULL;
L
Linus Torvalds 已提交
1710
} /* get_free_tx_ccs */
1711

L
Linus Torvalds 已提交
1712 1713 1714 1715 1716
/*===========================================================================*/
/* Get next free CCS                                                         */
/* Return - index of current ccs                                             */
static int get_free_ccs(ray_dev_t *local)
{
1717 1718 1719 1720 1721
	int i;
	struct ccs __iomem *pccs = ccs_base(local);
	struct pcmcia_device *link = local->finder;

	if (!(pcmcia_dev_present(link))) {
1722
		dev_dbg(&link->dev, "ray_cs get_free_ccs - device not present\n");
1723 1724 1725
		return ECARDGONE;
	}
	if (test_and_set_bit(0, &local->ccs_lock)) {
1726
		dev_dbg(&link->dev, "ray_cs ccs_lock busy\n");
1727 1728 1729 1730 1731 1732 1733
		return ECCSBUSY;
	}

	for (i = NUMBER_OF_TX_CCS; i < NUMBER_OF_CCS; i++) {
		if (readb(&(pccs + i)->buffer_status) == CCS_BUFFER_FREE) {
			writeb(CCS_BUFFER_BUSY, &(pccs + i)->buffer_status);
			writeb(CCS_END_LIST, &(pccs + i)->link);
L
Linus Torvalds 已提交
1734
			local->ccs_lock = 0;
1735 1736 1737
			return i;
		}
	}
L
Linus Torvalds 已提交
1738
	local->ccs_lock = 0;
1739
	dev_dbg(&link->dev, "ray_cs ERROR no free CCS for raylink card\n");
1740
	return ECCSFULL;
L
Linus Torvalds 已提交
1741
} /* get_free_ccs */
1742

L
Linus Torvalds 已提交
1743 1744 1745
/*===========================================================================*/
static void authenticate_timeout(u_long data)
{
1746 1747 1748 1749 1750
	ray_dev_t *local = (ray_dev_t *) data;
	del_timer(&local->timer);
	printk(KERN_INFO "ray_cs Authentication with access point failed"
	       " - timeout\n");
	join_net((u_long) local);
L
Linus Torvalds 已提交
1751
}
1752

L
Linus Torvalds 已提交
1753 1754 1755
/*===========================================================================*/
static int asc_to_int(char a)
{
1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768
	if (a < '0')
		return -1;
	if (a <= '9')
		return (a - '0');
	if (a < 'A')
		return -1;
	if (a <= 'F')
		return (10 + a - 'A');
	if (a < 'a')
		return -1;
	if (a <= 'f')
		return (10 + a - 'a');
	return -1;
L
Linus Torvalds 已提交
1769
}
1770

L
Linus Torvalds 已提交
1771 1772 1773
/*===========================================================================*/
static int parse_addr(char *in_str, UCHAR *out)
{
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
	int len;
	int i, j, k;
	int status;

	if (in_str == NULL)
		return 0;
	if ((len = strlen(in_str)) < 2)
		return 0;
	memset(out, 0, ADDRLEN);

	status = 1;
	j = len - 1;
	if (j > 12)
		j = 12;
	i = 5;

	while (j > 0) {
		if ((k = asc_to_int(in_str[j--])) != -1)
			out[i] = k;
		else
			return 0;

		if (j == 0)
			break;
		if ((k = asc_to_int(in_str[j--])) != -1)
			out[i] += k << 4;
		else
			return 0;
		if (!i--)
			break;
	}
	return status;
L
Linus Torvalds 已提交
1806
}
1807

L
Linus Torvalds 已提交
1808 1809 1810
/*===========================================================================*/
static struct net_device_stats *ray_get_stats(struct net_device *dev)
{
1811 1812 1813 1814
	ray_dev_t *local = netdev_priv(dev);
	struct pcmcia_device *link = local->finder;
	struct status __iomem *p = local->sram + STATUS_BASE;
	if (!(pcmcia_dev_present(link))) {
1815
		dev_dbg(&link->dev, "ray_cs net_device_stats - device not present\n");
1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
		return &local->stats;
	}
	if (readb(&p->mrx_overflow_for_host)) {
		local->stats.rx_over_errors += swab16(readw(&p->mrx_overflow));
		writeb(0, &p->mrx_overflow);
		writeb(0, &p->mrx_overflow_for_host);
	}
	if (readb(&p->mrx_checksum_error_for_host)) {
		local->stats.rx_crc_errors +=
		    swab16(readw(&p->mrx_checksum_error));
		writeb(0, &p->mrx_checksum_error);
		writeb(0, &p->mrx_checksum_error_for_host);
	}
	if (readb(&p->rx_hec_error_for_host)) {
		local->stats.rx_frame_errors += swab16(readw(&p->rx_hec_error));
		writeb(0, &p->rx_hec_error);
		writeb(0, &p->rx_hec_error_for_host);
	}
	return &local->stats;
L
Linus Torvalds 已提交
1835
}
1836

L
Linus Torvalds 已提交
1837
/*===========================================================================*/
1838 1839 1840 1841 1842 1843 1844 1845 1846 1847
static void ray_update_parm(struct net_device *dev, UCHAR objid, UCHAR *value,
			    int len)
{
	ray_dev_t *local = netdev_priv(dev);
	struct pcmcia_device *link = local->finder;
	int ccsindex;
	int i;
	struct ccs __iomem *pccs;

	if (!(pcmcia_dev_present(link))) {
1848
		dev_dbg(&link->dev, "ray_update_parm - device not present\n");
1849 1850 1851 1852
		return;
	}

	if ((ccsindex = get_free_ccs(local)) < 0) {
1853
		dev_dbg(&link->dev, "ray_update_parm - No free ccs\n");
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
		return;
	}
	pccs = ccs_base(local) + ccsindex;
	writeb(CCS_UPDATE_PARAMS, &pccs->cmd);
	writeb(objid, &pccs->var.update_param.object_id);
	writeb(1, &pccs->var.update_param.number_objects);
	writeb(0, &pccs->var.update_param.failure_cause);
	for (i = 0; i < len; i++) {
		writeb(value[i], local->sram + HOST_TO_ECF_BASE);
	}
	/* Interrupt the firmware to process the command */
	if (interrupt_ecf(local, ccsindex)) {
1866
		dev_dbg(&link->dev, "ray_cs associate failed - ECF not ready for intr\n");
1867 1868
		writeb(CCS_BUFFER_FREE, &(pccs++)->buffer_status);
	}
L
Linus Torvalds 已提交
1869
}
1870

L
Linus Torvalds 已提交
1871 1872 1873
/*===========================================================================*/
static void ray_update_multi_list(struct net_device *dev, int all)
{
1874 1875 1876 1877 1878 1879 1880
	int ccsindex;
	struct ccs __iomem *pccs;
	ray_dev_t *local = netdev_priv(dev);
	struct pcmcia_device *link = local->finder;
	void __iomem *p = local->sram + HOST_TO_ECF_BASE;

	if (!(pcmcia_dev_present(link))) {
1881
		dev_dbg(&link->dev, "ray_update_multi_list - device not present\n");
1882 1883
		return;
	} else
1884
		dev_dbg(&link->dev, "ray_update_multi_list(%p)\n", dev);
1885
	if ((ccsindex = get_free_ccs(local)) < 0) {
1886
		dev_dbg(&link->dev, "ray_update_multi - No free ccs\n");
1887 1888 1889 1890 1891 1892 1893 1894 1895
		return;
	}
	pccs = ccs_base(local) + ccsindex;
	writeb(CCS_UPDATE_MULTICAST_LIST, &pccs->cmd);

	if (all) {
		writeb(0xff, &pccs->var);
		local->num_multi = 0xff;
	} else {
1896 1897 1898
		struct dev_mc_list *dmi;
		int i = 0;

1899
		/* Copy the kernel's list of MC addresses to card */
1900
		netdev_for_each_mc_addr(dmi, dev) {
1901
			memcpy_toio(p, dmi->dmi_addr, ETH_ALEN);
1902
			dev_dbg(&link->dev,
1903 1904 1905 1906 1907 1908 1909 1910 1911 1912
			      "ray_update_multi add addr %02x%02x%02x%02x%02x%02x\n",
			      dmi->dmi_addr[0], dmi->dmi_addr[1],
			      dmi->dmi_addr[2], dmi->dmi_addr[3],
			      dmi->dmi_addr[4], dmi->dmi_addr[5]);
			p += ETH_ALEN;
			i++;
		}
		if (i > 256 / ADDRLEN)
			i = 256 / ADDRLEN;
		writeb((UCHAR) i, &pccs->var);
1913
		dev_dbg(&link->dev, "ray_cs update_multi %d addresses in list\n", i);
1914 1915 1916 1917
		/* Interrupt the firmware to process the command */
		local->num_multi = i;
	}
	if (interrupt_ecf(local, ccsindex)) {
1918
		dev_dbg(&link->dev,
1919 1920 1921
		      "ray_cs update_multi failed - ECF not ready for intr\n");
		writeb(CCS_BUFFER_FREE, &(pccs++)->buffer_status);
	}
L
Linus Torvalds 已提交
1922
} /* end ray_update_multi_list */
1923

L
Linus Torvalds 已提交
1924 1925 1926
/*===========================================================================*/
static void set_multicast_list(struct net_device *dev)
{
1927 1928 1929
	ray_dev_t *local = netdev_priv(dev);
	UCHAR promisc;

1930
	pr_debug("ray_cs set_multicast_list(%p)\n", dev);
1931 1932 1933

	if (dev->flags & IFF_PROMISC) {
		if (local->sparm.b5.a_promiscuous_mode == 0) {
1934
			pr_debug("ray_cs set_multicast_list promisc on\n");
1935 1936 1937 1938 1939 1940 1941
			local->sparm.b5.a_promiscuous_mode = 1;
			promisc = 1;
			ray_update_parm(dev, OBJID_promiscuous_mode,
					&promisc, sizeof(promisc));
		}
	} else {
		if (local->sparm.b5.a_promiscuous_mode == 1) {
1942
			pr_debug("ray_cs set_multicast_list promisc off\n");
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
			local->sparm.b5.a_promiscuous_mode = 0;
			promisc = 0;
			ray_update_parm(dev, OBJID_promiscuous_mode,
					&promisc, sizeof(promisc));
		}
	}

	if (dev->flags & IFF_ALLMULTI)
		ray_update_multi_list(dev, 1);
	else {
1953
		if (local->num_multi != netdev_mc_count(dev))
1954 1955
			ray_update_multi_list(dev, 0);
	}
L
Linus Torvalds 已提交
1956
} /* end set_multicast_list */
1957

L
Linus Torvalds 已提交
1958 1959 1960
/*=============================================================================
 * All routines below here are run at interrupt time.
=============================================================================*/
1961
static irqreturn_t ray_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
1962
{
1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975
	struct net_device *dev = (struct net_device *)dev_id;
	struct pcmcia_device *link;
	ray_dev_t *local;
	struct ccs __iomem *pccs;
	struct rcs __iomem *prcs;
	UCHAR rcsindex;
	UCHAR tmp;
	UCHAR cmd;
	UCHAR status;

	if (dev == NULL)	/* Note that we want interrupts with dev->start == 0 */
		return IRQ_NONE;

1976
	pr_debug("ray_cs: interrupt for *dev=%p\n", dev);
1977 1978 1979 1980

	local = netdev_priv(dev);
	link = (struct pcmcia_device *)local->finder;
	if (!pcmcia_dev_present(link)) {
1981 1982
		pr_debug(
			"ray_cs interrupt from device not present or suspended.\n");
1983 1984 1985 1986 1987
		return IRQ_NONE;
	}
	rcsindex = readb(&((struct scb __iomem *)(local->sram))->rcs_index);

	if (rcsindex >= (NUMBER_OF_CCS + NUMBER_OF_RCS)) {
1988
		dev_dbg(&link->dev, "ray_cs interrupt bad rcsindex = 0x%x\n", rcsindex);
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
		clear_interrupt(local);
		return IRQ_HANDLED;
	}
	if (rcsindex < NUMBER_OF_CCS) { /* If it's a returned CCS */
		pccs = ccs_base(local) + rcsindex;
		cmd = readb(&pccs->cmd);
		status = readb(&pccs->buffer_status);
		switch (cmd) {
		case CCS_DOWNLOAD_STARTUP_PARAMS:	/* Happens in firmware someday */
			del_timer(&local->timer);
			if (status == CCS_COMMAND_COMPLETE) {
2000
				dev_dbg(&link->dev,
2001 2002
				      "ray_cs interrupt download_startup_parameters OK\n");
			} else {
2003
				dev_dbg(&link->dev,
2004 2005 2006 2007
				      "ray_cs interrupt download_startup_parameters fail\n");
			}
			break;
		case CCS_UPDATE_PARAMS:
2008
			dev_dbg(&link->dev, "ray_cs interrupt update params done\n");
2009 2010 2011 2012
			if (status != CCS_COMMAND_COMPLETE) {
				tmp =
				    readb(&pccs->var.update_param.
					  failure_cause);
2013
				dev_dbg(&link->dev,
2014 2015 2016 2017 2018
				      "ray_cs interrupt update params failed - reason %d\n",
				      tmp);
			}
			break;
		case CCS_REPORT_PARAMS:
2019
			dev_dbg(&link->dev, "ray_cs interrupt report params done\n");
2020 2021
			break;
		case CCS_UPDATE_MULTICAST_LIST:	/* Note that this CCS isn't returned */
2022
			dev_dbg(&link->dev,
2023 2024 2025
			      "ray_cs interrupt CCS Update Multicast List done\n");
			break;
		case CCS_UPDATE_POWER_SAVINGS_MODE:
2026
			dev_dbg(&link->dev,
2027 2028 2029 2030 2031 2032 2033 2034
			      "ray_cs interrupt update power save mode done\n");
			break;
		case CCS_START_NETWORK:
		case CCS_JOIN_NETWORK:
			if (status == CCS_COMMAND_COMPLETE) {
				if (readb
				    (&pccs->var.start_network.net_initiated) ==
				    1) {
2035
					dev_dbg(&link->dev,
2036 2037 2038
					      "ray_cs interrupt network \"%s\" started\n",
					      local->sparm.b4.a_current_ess_id);
				} else {
2039
					dev_dbg(&link->dev,
2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066
					      "ray_cs interrupt network \"%s\" joined\n",
					      local->sparm.b4.a_current_ess_id);
				}
				memcpy_fromio(&local->bss_id,
					      pccs->var.start_network.bssid,
					      ADDRLEN);

				if (local->fw_ver == 0x55)
					local->net_default_tx_rate = 3;
				else
					local->net_default_tx_rate =
					    readb(&pccs->var.start_network.
						  net_default_tx_rate);
				local->encryption =
				    readb(&pccs->var.start_network.encryption);
				if (!sniffer && (local->net_type == INFRA)
				    && !(local->sparm.b4.a_acting_as_ap_status)) {
					authenticate(local);
				}
				local->card_status = CARD_ACQ_COMPLETE;
			} else {
				local->card_status = CARD_ACQ_FAILED;

				del_timer(&local->timer);
				local->timer.expires = jiffies + HZ * 5;
				local->timer.data = (long)local;
				if (status == CCS_START_NETWORK) {
2067
					dev_dbg(&link->dev,
2068 2069 2070 2071
					      "ray_cs interrupt network \"%s\" start failed\n",
					      local->sparm.b4.a_current_ess_id);
					local->timer.function = &start_net;
				} else {
2072
					dev_dbg(&link->dev,
2073 2074 2075 2076 2077 2078 2079 2080 2081 2082
					      "ray_cs interrupt network \"%s\" join failed\n",
					      local->sparm.b4.a_current_ess_id);
					local->timer.function = &join_net;
				}
				add_timer(&local->timer);
			}
			break;
		case CCS_START_ASSOCIATION:
			if (status == CCS_COMMAND_COMPLETE) {
				local->card_status = CARD_ASSOC_COMPLETE;
2083
				dev_dbg(&link->dev, "ray_cs association successful\n");
2084
			} else {
2085
				dev_dbg(&link->dev, "ray_cs association failed,\n");
2086 2087 2088 2089 2090 2091
				local->card_status = CARD_ASSOC_FAILED;
				join_net((u_long) local);
			}
			break;
		case CCS_TX_REQUEST:
			if (status == CCS_COMMAND_COMPLETE) {
2092
				dev_dbg(&link->dev,
2093 2094
				      "ray_cs interrupt tx request complete\n");
			} else {
2095
				dev_dbg(&link->dev,
2096 2097 2098 2099 2100 2101 2102
				      "ray_cs interrupt tx request failed\n");
			}
			if (!sniffer)
				netif_start_queue(dev);
			netif_wake_queue(dev);
			break;
		case CCS_TEST_MEMORY:
2103
			dev_dbg(&link->dev, "ray_cs interrupt mem test done\n");
2104 2105
			break;
		case CCS_SHUTDOWN:
2106
			dev_dbg(&link->dev,
2107 2108 2109
			      "ray_cs interrupt Unexpected CCS returned - Shutdown\n");
			break;
		case CCS_DUMP_MEMORY:
2110
			dev_dbg(&link->dev, "ray_cs interrupt dump memory done\n");
2111 2112
			break;
		case CCS_START_TIMER:
2113
			dev_dbg(&link->dev,
2114 2115 2116
			      "ray_cs interrupt DING - raylink timer expired\n");
			break;
		default:
2117
			dev_dbg(&link->dev,
2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130
			      "ray_cs interrupt Unexpected CCS 0x%x returned 0x%x\n",
			      rcsindex, cmd);
		}
		writeb(CCS_BUFFER_FREE, &pccs->buffer_status);
	} else { /* It's an RCS */

		prcs = rcs_base(local) + rcsindex;

		switch (readb(&prcs->interrupt_id)) {
		case PROCESS_RX_PACKET:
			ray_rx(dev, local, prcs);
			break;
		case REJOIN_NET_COMPLETE:
2131
			dev_dbg(&link->dev, "ray_cs interrupt rejoin net complete\n");
2132 2133 2134 2135 2136 2137 2138 2139 2140
			local->card_status = CARD_ACQ_COMPLETE;
			/* do we need to clear tx buffers CCS's? */
			if (local->sparm.b4.a_network_type == ADHOC) {
				if (!sniffer)
					netif_start_queue(dev);
			} else {
				memcpy_fromio(&local->bss_id,
					      prcs->var.rejoin_net_complete.
					      bssid, ADDRLEN);
2141
				dev_dbg(&link->dev,
2142 2143 2144 2145 2146 2147 2148 2149 2150
				      "ray_cs new BSSID = %02x%02x%02x%02x%02x%02x\n",
				      local->bss_id[0], local->bss_id[1],
				      local->bss_id[2], local->bss_id[3],
				      local->bss_id[4], local->bss_id[5]);
				if (!sniffer)
					authenticate(local);
			}
			break;
		case ROAMING_INITIATED:
2151
			dev_dbg(&link->dev, "ray_cs interrupt roaming initiated\n");
2152 2153 2154 2155
			netif_stop_queue(dev);
			local->card_status = CARD_DOING_ACQ;
			break;
		case JAPAN_CALL_SIGN_RXD:
2156
			dev_dbg(&link->dev, "ray_cs interrupt japan call sign rx\n");
2157 2158
			break;
		default:
2159
			dev_dbg(&link->dev,
2160 2161 2162 2163 2164 2165 2166 2167 2168
			      "ray_cs Unexpected interrupt for RCS 0x%x cmd = 0x%x\n",
			      rcsindex,
			      (unsigned int)readb(&prcs->interrupt_id));
			break;
		}
		writeb(CCS_BUFFER_FREE, &prcs->buffer_status);
	}
	clear_interrupt(local);
	return IRQ_HANDLED;
L
Linus Torvalds 已提交
2169
} /* ray_interrupt */
2170

L
Linus Torvalds 已提交
2171
/*===========================================================================*/
2172 2173 2174 2175 2176 2177
static void ray_rx(struct net_device *dev, ray_dev_t *local,
		   struct rcs __iomem *prcs)
{
	int rx_len;
	unsigned int pkt_addr;
	void __iomem *pmsg;
2178
	pr_debug("ray_rx process rx packet\n");
2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190

	/* Calculate address of packet within Rx buffer */
	pkt_addr = ((readb(&prcs->var.rx_packet.rx_data_ptr[0]) << 8)
		    + readb(&prcs->var.rx_packet.rx_data_ptr[1])) & RX_BUFF_END;
	/* Length of first packet fragment */
	rx_len = (readb(&prcs->var.rx_packet.rx_data_length[0]) << 8)
	    + readb(&prcs->var.rx_packet.rx_data_length[1]);

	local->last_rsl = readb(&prcs->var.rx_packet.rx_sig_lev);
	pmsg = local->rmem + pkt_addr;
	switch (readb(pmsg)) {
	case DATA_TYPE:
2191
		pr_debug("ray_rx data type\n");
2192 2193 2194
		rx_data(dev, prcs, pkt_addr, rx_len);
		break;
	case AUTHENTIC_TYPE:
2195
		pr_debug("ray_rx authentic type\n");
2196 2197 2198 2199 2200 2201
		if (sniffer)
			rx_data(dev, prcs, pkt_addr, rx_len);
		else
			rx_authenticate(local, prcs, pkt_addr, rx_len);
		break;
	case DEAUTHENTIC_TYPE:
2202
		pr_debug("ray_rx deauth type\n");
2203 2204 2205 2206 2207 2208
		if (sniffer)
			rx_data(dev, prcs, pkt_addr, rx_len);
		else
			rx_deauthenticate(local, prcs, pkt_addr, rx_len);
		break;
	case NULL_MSG_TYPE:
2209
		pr_debug("ray_cs rx NULL msg\n");
2210 2211
		break;
	case BEACON_TYPE:
2212
		pr_debug("ray_rx beacon type\n");
2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
		if (sniffer)
			rx_data(dev, prcs, pkt_addr, rx_len);

		copy_from_rx_buff(local, (UCHAR *) &local->last_bcn, pkt_addr,
				  rx_len < sizeof(struct beacon_rx) ?
				  rx_len : sizeof(struct beacon_rx));

		local->beacon_rxed = 1;
		/* Get the statistics so the card counters never overflow */
		ray_get_stats(dev);
		break;
	default:
2225
		pr_debug("ray_cs unknown pkt type %2x\n",
2226 2227 2228
		      (unsigned int)readb(pmsg));
		break;
	}
L
Linus Torvalds 已提交
2229 2230

} /* end ray_rx */
2231

L
Linus Torvalds 已提交
2232
/*===========================================================================*/
2233 2234 2235 2236 2237 2238 2239 2240 2241
static void rx_data(struct net_device *dev, struct rcs __iomem *prcs,
		    unsigned int pkt_addr, int rx_len)
{
	struct sk_buff *skb = NULL;
	struct rcs __iomem *prcslink = prcs;
	ray_dev_t *local = netdev_priv(dev);
	UCHAR *rx_ptr;
	int total_len;
	int tmp;
L
Linus Torvalds 已提交
2242
#ifdef WIRELESS_SPY
2243 2244
	int siglev = local->last_rsl;
	u_char linksrcaddr[ETH_ALEN];	/* Other end of the wireless link */
L
Linus Torvalds 已提交
2245 2246
#endif

2247 2248
	if (!sniffer) {
		if (translate) {
L
Linus Torvalds 已提交
2249
/* TBD length needs fixing for translated header */
2250 2251 2252 2253
			if (rx_len < (ETH_HLEN + RX_MAC_HEADER_LENGTH) ||
			    rx_len >
			    (dev->mtu + RX_MAC_HEADER_LENGTH + ETH_HLEN +
			     FCS_LEN)) {
2254
				pr_debug(
2255 2256 2257 2258 2259 2260 2261 2262 2263 2264
				      "ray_cs invalid packet length %d received \n",
				      rx_len);
				return;
			}
		} else { /* encapsulated ethernet */

			if (rx_len < (ETH_HLEN + RX_MAC_HEADER_LENGTH) ||
			    rx_len >
			    (dev->mtu + RX_MAC_HEADER_LENGTH + ETH_HLEN +
			     FCS_LEN)) {
2265
				pr_debug(
2266 2267 2268 2269 2270 2271
				      "ray_cs invalid packet length %d received \n",
				      rx_len);
				return;
			}
		}
	}
2272
	pr_debug("ray_cs rx_data packet\n");
2273 2274
	/* If fragmented packet, verify sizes of fragments add up */
	if (readb(&prcs->var.rx_packet.next_frag_rcs_index) != 0xFF) {
2275
		pr_debug("ray_cs rx'ed fragment\n");
2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292
		tmp = (readb(&prcs->var.rx_packet.totalpacketlength[0]) << 8)
		    + readb(&prcs->var.rx_packet.totalpacketlength[1]);
		total_len = tmp;
		prcslink = prcs;
		do {
			tmp -=
			    (readb(&prcslink->var.rx_packet.rx_data_length[0])
			     << 8)
			    + readb(&prcslink->var.rx_packet.rx_data_length[1]);
			if (readb(&prcslink->var.rx_packet.next_frag_rcs_index)
			    == 0xFF || tmp < 0)
				break;
			prcslink = rcs_base(local)
			    + readb(&prcslink->link_field);
		} while (1);

		if (tmp < 0) {
2293
			pr_debug(
2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304
			      "ray_cs rx_data fragment lengths don't add up\n");
			local->stats.rx_dropped++;
			release_frag_chain(local, prcs);
			return;
		}
	} else { /* Single unfragmented packet */
		total_len = rx_len;
	}

	skb = dev_alloc_skb(total_len + 5);
	if (skb == NULL) {
2305
		pr_debug("ray_cs rx_data could not allocate skb\n");
2306 2307 2308 2309 2310 2311 2312
		local->stats.rx_dropped++;
		if (readb(&prcs->var.rx_packet.next_frag_rcs_index) != 0xFF)
			release_frag_chain(local, prcs);
		return;
	}
	skb_reserve(skb, 2);	/* Align IP on 16 byte (TBD check this) */

2313
	pr_debug("ray_cs rx_data total_len = %x, rx_len = %x\n", total_len,
2314
	      rx_len);
L
Linus Torvalds 已提交
2315 2316

/************************/
2317 2318 2319 2320 2321 2322
	/* Reserve enough room for the whole damn packet. */
	rx_ptr = skb_put(skb, total_len);
	/* Copy the whole packet to sk_buff */
	rx_ptr +=
	    copy_from_rx_buff(local, rx_ptr, pkt_addr & RX_BUFF_END, rx_len);
	/* Get source address */
L
Linus Torvalds 已提交
2323
#ifdef WIRELESS_SPY
2324 2325 2326
	skb_copy_from_linear_data_offset(skb,
					 offsetof(struct mac_header, addr_2),
					 linksrcaddr, ETH_ALEN);
L
Linus Torvalds 已提交
2327
#endif
2328 2329 2330 2331
	/* Now, deal with encapsulation/translation/sniffer */
	if (!sniffer) {
		if (!translate) {
			/* Encapsulated ethernet, so just lop off 802.11 MAC header */
L
Linus Torvalds 已提交
2332
/* TBD reserve            skb_reserve( skb, RX_MAC_HEADER_LENGTH); */
2333 2334 2335 2336 2337 2338 2339
			skb_pull(skb, RX_MAC_HEADER_LENGTH);
		} else {
			/* Do translation */
			untranslate(local, skb, total_len);
		}
	} else { /* sniffer mode, so just pass whole packet */
	};
L
Linus Torvalds 已提交
2340 2341

/************************/
2342 2343 2344 2345
	/* Now pick up the rest of the fragments if any */
	tmp = 17;
	if (readb(&prcs->var.rx_packet.next_frag_rcs_index) != 0xFF) {
		prcslink = prcs;
2346
		pr_debug("ray_cs rx_data in fragment loop\n");
2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377
		do {
			prcslink = rcs_base(local)
			    +
			    readb(&prcslink->var.rx_packet.next_frag_rcs_index);
			rx_len =
			    ((readb(&prcslink->var.rx_packet.rx_data_length[0])
			      << 8)
			     +
			     readb(&prcslink->var.rx_packet.rx_data_length[1]))
			    & RX_BUFF_END;
			pkt_addr =
			    ((readb(&prcslink->var.rx_packet.rx_data_ptr[0]) <<
			      8)
			     + readb(&prcslink->var.rx_packet.rx_data_ptr[1]))
			    & RX_BUFF_END;

			rx_ptr +=
			    copy_from_rx_buff(local, rx_ptr, pkt_addr, rx_len);

		} while (tmp-- &&
			 readb(&prcslink->var.rx_packet.next_frag_rcs_index) !=
			 0xFF);
		release_frag_chain(local, prcs);
	}

	skb->protocol = eth_type_trans(skb, dev);
	netif_rx(skb);
	local->stats.rx_packets++;
	local->stats.rx_bytes += total_len;

	/* Gather signal strength per address */
L
Linus Torvalds 已提交
2378
#ifdef WIRELESS_SPY
2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399
	/* For the Access Point or the node having started the ad-hoc net
	 * note : ad-hoc work only in some specific configurations, but we
	 * kludge in ray_get_wireless_stats... */
	if (!memcmp(linksrcaddr, local->bss_id, ETH_ALEN)) {
		/* Update statistics */
		/*local->wstats.qual.qual = none ? */
		local->wstats.qual.level = siglev;
		/*local->wstats.qual.noise = none ? */
		local->wstats.qual.updated = 0x2;
	}
	/* Now, update the spy stuff */
	{
		struct iw_quality wstats;
		wstats.level = siglev;
		/* wstats.noise = none ? */
		/* wstats.qual = none ? */
		wstats.updated = 0x2;
		/* Update spy records */
		wireless_spy_update(dev, linksrcaddr, &wstats);
	}
#endif /* WIRELESS_SPY */
L
Linus Torvalds 已提交
2400
} /* end rx_data */
2401

L
Linus Torvalds 已提交
2402 2403 2404
/*===========================================================================*/
static void untranslate(ray_dev_t *local, struct sk_buff *skb, int len)
{
2405 2406 2407 2408 2409 2410 2411 2412 2413
	snaphdr_t *psnap = (snaphdr_t *) (skb->data + RX_MAC_HEADER_LENGTH);
	struct ieee80211_hdr *pmac = (struct ieee80211_hdr *)skb->data;
	__be16 type = *(__be16 *) psnap->ethertype;
	int delta;
	struct ethhdr *peth;
	UCHAR srcaddr[ADDRLEN];
	UCHAR destaddr[ADDRLEN];
	static UCHAR org_bridge[3] = { 0, 0, 0xf8 };
	static UCHAR org_1042[3] = { 0, 0, 0 };
L
Linus Torvalds 已提交
2414

2415 2416
	memcpy(destaddr, ieee80211_get_DA(pmac), ADDRLEN);
	memcpy(srcaddr, ieee80211_get_SA(pmac), ADDRLEN);
L
Linus Torvalds 已提交
2417

2418 2419
#if 0
	if {
2420 2421 2422 2423
		print_hex_dump(KERN_DEBUG, "skb->data before untranslate: ",
			       DUMP_PREFIX_NONE, 16, 1,
			       skb->data, 64, true);
		printk(KERN_DEBUG
2424 2425 2426 2427 2428
		       "type = %08x, xsap = %02x%02x%02x, org = %02x02x02x\n",
		       ntohs(type), psnap->dsap, psnap->ssap, psnap->ctrl,
		       psnap->org[0], psnap->org[1], psnap->org[2]);
		printk(KERN_DEBUG "untranslate skb->data = %p\n", skb->data);
	}
L
Linus Torvalds 已提交
2429 2430
#endif

2431 2432
	if (psnap->dsap != 0xaa || psnap->ssap != 0xaa || psnap->ctrl != 3) {
		/* not a snap type so leave it alone */
2433
		pr_debug("ray_cs untranslate NOT SNAP %02x %02x %02x\n",
2434 2435 2436 2437 2438 2439 2440 2441
		      psnap->dsap, psnap->ssap, psnap->ctrl);

		delta = RX_MAC_HEADER_LENGTH - ETH_HLEN;
		peth = (struct ethhdr *)(skb->data + delta);
		peth->h_proto = htons(len - RX_MAC_HEADER_LENGTH);
	} else { /* Its a SNAP */
		if (memcmp(psnap->org, org_bridge, 3) == 0) {
		/* EtherII and nuke the LLC */
2442
			pr_debug("ray_cs untranslate Bridge encap\n");
2443 2444 2445 2446 2447 2448 2449 2450
			delta = RX_MAC_HEADER_LENGTH
			    + sizeof(struct snaphdr_t) - ETH_HLEN;
			peth = (struct ethhdr *)(skb->data + delta);
			peth->h_proto = type;
		} else if (memcmp(psnap->org, org_1042, 3) == 0) {
			switch (ntohs(type)) {
			case ETH_P_IPX:
			case ETH_P_AARP:
2451
				pr_debug("ray_cs untranslate RFC IPX/AARP\n");
2452 2453 2454 2455 2456 2457
				delta = RX_MAC_HEADER_LENGTH - ETH_HLEN;
				peth = (struct ethhdr *)(skb->data + delta);
				peth->h_proto =
				    htons(len - RX_MAC_HEADER_LENGTH);
				break;
			default:
2458
				pr_debug("ray_cs untranslate RFC default\n");
2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470
				delta = RX_MAC_HEADER_LENGTH +
				    sizeof(struct snaphdr_t) - ETH_HLEN;
				peth = (struct ethhdr *)(skb->data + delta);
				peth->h_proto = type;
				break;
			}
		} else {
			printk("ray_cs untranslate very confused by packet\n");
			delta = RX_MAC_HEADER_LENGTH - ETH_HLEN;
			peth = (struct ethhdr *)(skb->data + delta);
			peth->h_proto = type;
		}
A
Al Viro 已提交
2471
	}
L
Linus Torvalds 已提交
2472
/* TBD reserve  skb_reserve(skb, delta); */
2473
	skb_pull(skb, delta);
2474
	pr_debug("untranslate after skb_pull(%d), skb->data = %p\n", delta,
2475 2476 2477
	      skb->data);
	memcpy(peth->h_dest, destaddr, ADDRLEN);
	memcpy(peth->h_source, srcaddr, ADDRLEN);
2478 2479
#if 0
	{
2480 2481 2482 2483 2484 2485
		int i;
		printk(KERN_DEBUG "skb->data after untranslate:");
		for (i = 0; i < 64; i++)
			printk("%02x ", skb->data[i]);
		printk("\n");
	}
L
Linus Torvalds 已提交
2486 2487
#endif
} /* end untranslate */
2488

L
Linus Torvalds 已提交
2489 2490 2491 2492 2493 2494
/*===========================================================================*/
/* Copy data from circular receive buffer to PC memory.
 * dest     = destination address in PC memory
 * pkt_addr = source address in receive buffer
 * len      = length of packet to copy
 */
2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508
static int copy_from_rx_buff(ray_dev_t *local, UCHAR *dest, int pkt_addr,
			     int length)
{
	int wrap_bytes = (pkt_addr + length) - (RX_BUFF_END + 1);
	if (wrap_bytes <= 0) {
		memcpy_fromio(dest, local->rmem + pkt_addr, length);
	} else { /* Packet wrapped in circular buffer */

		memcpy_fromio(dest, local->rmem + pkt_addr,
			      length - wrap_bytes);
		memcpy_fromio(dest + length - wrap_bytes, local->rmem,
			      wrap_bytes);
	}
	return length;
L
Linus Torvalds 已提交
2509
}
2510

L
Linus Torvalds 已提交
2511
/*===========================================================================*/
2512 2513 2514 2515 2516 2517 2518 2519 2520
static void release_frag_chain(ray_dev_t *local, struct rcs __iomem *prcs)
{
	struct rcs __iomem *prcslink = prcs;
	int tmp = 17;
	unsigned rcsindex = readb(&prcs->var.rx_packet.next_frag_rcs_index);

	while (tmp--) {
		writeb(CCS_BUFFER_FREE, &prcslink->buffer_status);
		if (rcsindex >= (NUMBER_OF_CCS + NUMBER_OF_RCS)) {
2521
			pr_debug("ray_cs interrupt bad rcsindex = 0x%x\n",
2522 2523 2524 2525 2526 2527 2528
			      rcsindex);
			break;
		}
		prcslink = rcs_base(local) + rcsindex;
		rcsindex = readb(&prcslink->var.rx_packet.next_frag_rcs_index);
	}
	writeb(CCS_BUFFER_FREE, &prcslink->buffer_status);
L
Linus Torvalds 已提交
2529
}
2530

L
Linus Torvalds 已提交
2531 2532 2533
/*===========================================================================*/
static void authenticate(ray_dev_t *local)
{
2534
	struct pcmcia_device *link = local->finder;
2535
	dev_dbg(&link->dev, "ray_cs Starting authentication.\n");
2536
	if (!(pcmcia_dev_present(link))) {
2537
		dev_dbg(&link->dev, "ray_cs authenticate - device not present\n");
2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550
		return;
	}

	del_timer(&local->timer);
	if (build_auth_frame(local, local->bss_id, OPEN_AUTH_REQUEST)) {
		local->timer.function = &join_net;
	} else {
		local->timer.function = &authenticate_timeout;
	}
	local->timer.expires = jiffies + HZ * 2;
	local->timer.data = (long)local;
	add_timer(&local->timer);
	local->authentication_state = AWAITING_RESPONSE;
L
Linus Torvalds 已提交
2551
} /* end authenticate */
2552

L
Linus Torvalds 已提交
2553 2554
/*===========================================================================*/
static void rx_authenticate(ray_dev_t *local, struct rcs __iomem *prcs,
2555 2556 2557 2558 2559 2560 2561 2562 2563 2564
			    unsigned int pkt_addr, int rx_len)
{
	UCHAR buff[256];
	struct rx_msg *msg = (struct rx_msg *)buff;

	del_timer(&local->timer);

	copy_from_rx_buff(local, buff, pkt_addr, rx_len & 0xff);
	/* if we are trying to get authenticated */
	if (local->sparm.b4.a_network_type == ADHOC) {
2565
		pr_debug("ray_cs rx_auth var= %02x %02x %02x %02x %02x %02x\n",
2566 2567 2568
		      msg->var[0], msg->var[1], msg->var[2], msg->var[3],
		      msg->var[4], msg->var[5]);
		if (msg->var[2] == 1) {
2569
			pr_debug("ray_cs Sending authentication response.\n");
2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582
			if (!build_auth_frame
			    (local, msg->mac.addr_2, OPEN_AUTH_RESPONSE)) {
				local->authentication_state = NEED_TO_AUTH;
				memcpy(local->auth_id, msg->mac.addr_2,
				       ADDRLEN);
			}
		}
	} else { /* Infrastructure network */

		if (local->authentication_state == AWAITING_RESPONSE) {
			/* Verify authentication sequence #2 and success */
			if (msg->var[2] == 2) {
				if ((msg->var[3] | msg->var[4]) == 0) {
2583
					pr_debug("Authentication successful\n");
2584 2585 2586 2587 2588
					local->card_status = CARD_AUTH_COMPLETE;
					associate(local);
					local->authentication_state =
					    AUTHENTICATED;
				} else {
2589
					pr_debug("Authentication refused\n");
2590 2591 2592 2593 2594 2595 2596 2597
					local->card_status = CARD_AUTH_REFUSED;
					join_net((u_long) local);
					local->authentication_state =
					    UNAUTHENTICATED;
				}
			}
		}
	}
L
Linus Torvalds 已提交
2598 2599

} /* end rx_authenticate */
2600

L
Linus Torvalds 已提交
2601 2602 2603
/*===========================================================================*/
static void associate(ray_dev_t *local)
{
2604 2605 2606 2607 2608
	struct ccs __iomem *pccs;
	struct pcmcia_device *link = local->finder;
	struct net_device *dev = link->priv;
	int ccsindex;
	if (!(pcmcia_dev_present(link))) {
2609
		dev_dbg(&link->dev, "ray_cs associate - device not present\n");
2610 2611 2612 2613
		return;
	}
	/* If no tx buffers available, return */
	if ((ccsindex = get_free_ccs(local)) < 0) {
L
Linus Torvalds 已提交
2614
/* TBD should never be here but... what if we are? */
2615
		dev_dbg(&link->dev, "ray_cs associate - No free ccs\n");
2616 2617
		return;
	}
2618
	dev_dbg(&link->dev, "ray_cs Starting association with access point\n");
2619 2620 2621 2622 2623
	pccs = ccs_base(local) + ccsindex;
	/* fill in the CCS */
	writeb(CCS_START_ASSOCIATION, &pccs->cmd);
	/* Interrupt the firmware to process the command */
	if (interrupt_ecf(local, ccsindex)) {
2624
		dev_dbg(&link->dev, "ray_cs associate failed - ECF not ready for intr\n");
2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636
		writeb(CCS_BUFFER_FREE, &(pccs++)->buffer_status);

		del_timer(&local->timer);
		local->timer.expires = jiffies + HZ * 2;
		local->timer.data = (long)local;
		local->timer.function = &join_net;
		add_timer(&local->timer);
		local->card_status = CARD_ASSOC_FAILED;
		return;
	}
	if (!sniffer)
		netif_start_queue(dev);
L
Linus Torvalds 已提交
2637 2638

} /* end associate */
2639

L
Linus Torvalds 已提交
2640
/*===========================================================================*/
2641 2642
static void rx_deauthenticate(ray_dev_t *local, struct rcs __iomem *prcs,
			      unsigned int pkt_addr, int rx_len)
L
Linus Torvalds 已提交
2643 2644 2645 2646
{
/*  UCHAR buff[256];
    struct rx_msg *msg = (struct rx_msg *)buff;
*/
2647
	pr_debug("Deauthentication frame received\n");
2648 2649
	local->authentication_state = UNAUTHENTICATED;
	/* Need to reauthenticate or rejoin depending on reason code */
L
Linus Torvalds 已提交
2650 2651 2652
/*  copy_from_rx_buff(local, buff, pkt_addr, rx_len & 0xff);
 */
}
2653

L
Linus Torvalds 已提交
2654 2655 2656
/*===========================================================================*/
static void clear_interrupt(ray_dev_t *local)
{
2657
	writeb(0, local->amem + CIS_OFFSET + HCS_INTR_OFFSET);
L
Linus Torvalds 已提交
2658
}
2659

L
Linus Torvalds 已提交
2660 2661 2662 2663 2664
/*===========================================================================*/
#ifdef CONFIG_PROC_FS
#define MAXDATA (PAGE_SIZE - 80)

static char *card_status[] = {
2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678
	"Card inserted - uninitialized",	/* 0 */
	"Card not downloaded",			/* 1 */
	"Waiting for download parameters",	/* 2 */
	"Card doing acquisition",		/* 3 */
	"Acquisition complete",			/* 4 */
	"Authentication complete",		/* 5 */
	"Association complete",			/* 6 */
	"???", "???", "???", "???",		/* 7 8 9 10 undefined */
	"Card init error",			/* 11 */
	"Download parameters error",		/* 12 */
	"???",					/* 13 */
	"Acquisition failed",			/* 14 */
	"Authentication refused",		/* 15 */
	"Association failed"			/* 16 */
L
Linus Torvalds 已提交
2679 2680
};

2681 2682 2683
static char *nettype[] = { "Adhoc", "Infra " };
static char *framing[] = { "Encapsulation", "Translation" }

L
Linus Torvalds 已提交
2684 2685
;
/*===========================================================================*/
2686
static int ray_cs_proc_show(struct seq_file *m, void *v)
L
Linus Torvalds 已提交
2687 2688
{
/* Print current values which are not available via other means
2689
 * eg ifconfig
L
Linus Torvalds 已提交
2690
 */
2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787
	int i;
	struct pcmcia_device *link;
	struct net_device *dev;
	ray_dev_t *local;
	UCHAR *p;
	struct freq_hop_element *pfh;
	UCHAR c[33];

	link = this_device;
	if (!link)
		return 0;
	dev = (struct net_device *)link->priv;
	if (!dev)
		return 0;
	local = netdev_priv(dev);
	if (!local)
		return 0;

	seq_puts(m, "Raylink Wireless LAN driver status\n");
	seq_printf(m, "%s\n", rcsid);
	/* build 4 does not report version, and field is 0x55 after memtest */
	seq_puts(m, "Firmware version     = ");
	if (local->fw_ver == 0x55)
		seq_puts(m, "4 - Use dump_cis for more details\n");
	else
		seq_printf(m, "%2d.%02d.%02d\n",
			   local->fw_ver, local->fw_bld, local->fw_var);

	for (i = 0; i < 32; i++)
		c[i] = local->sparm.b5.a_current_ess_id[i];
	c[32] = 0;
	seq_printf(m, "%s network ESSID = \"%s\"\n",
		   nettype[local->sparm.b5.a_network_type], c);

	p = local->bss_id;
	seq_printf(m, "BSSID                = %pM\n", p);

	seq_printf(m, "Country code         = %d\n",
		   local->sparm.b5.a_curr_country_code);

	i = local->card_status;
	if (i < 0)
		i = 10;
	if (i > 16)
		i = 10;
	seq_printf(m, "Card status          = %s\n", card_status[i]);

	seq_printf(m, "Framing mode         = %s\n", framing[translate]);

	seq_printf(m, "Last pkt signal lvl  = %d\n", local->last_rsl);

	if (local->beacon_rxed) {
		/* Pull some fields out of last beacon received */
		seq_printf(m, "Beacon Interval      = %d Kus\n",
			   local->last_bcn.beacon_intvl[0]
			   + 256 * local->last_bcn.beacon_intvl[1]);

		p = local->last_bcn.elements;
		if (p[0] == C_ESSID_ELEMENT_ID)
			p += p[1] + 2;
		else {
			seq_printf(m,
				   "Parse beacon failed at essid element id = %d\n",
				   p[0]);
			return 0;
		}

		if (p[0] == C_SUPPORTED_RATES_ELEMENT_ID) {
			seq_puts(m, "Supported rate codes = ");
			for (i = 2; i < p[1] + 2; i++)
				seq_printf(m, "0x%02x ", p[i]);
			seq_putc(m, '\n');
			p += p[1] + 2;
		} else {
			seq_puts(m, "Parse beacon failed at rates element\n");
			return 0;
		}

		if (p[0] == C_FH_PARAM_SET_ELEMENT_ID) {
			pfh = (struct freq_hop_element *)p;
			seq_printf(m, "Hop dwell            = %d Kus\n",
				   pfh->dwell_time[0] +
				   256 * pfh->dwell_time[1]);
			seq_printf(m, "Hop set              = %d \n",
				   pfh->hop_set);
			seq_printf(m, "Hop pattern          = %d \n",
				   pfh->hop_pattern);
			seq_printf(m, "Hop index            = %d \n",
				   pfh->hop_index);
			p += p[1] + 2;
		} else {
			seq_puts(m,
				 "Parse beacon failed at FH param element\n");
			return 0;
		}
	} else {
		seq_puts(m, "No beacons received\n");
L
Linus Torvalds 已提交
2788
	}
2789
	return 0;
L
Linus Torvalds 已提交
2790 2791
}

2792 2793 2794 2795 2796 2797
static int ray_cs_proc_open(struct inode *inode, struct file *file)
{
	return single_open(file, ray_cs_proc_show, NULL);
}

static const struct file_operations ray_cs_proc_fops = {
2798 2799 2800 2801 2802
	.owner = THIS_MODULE,
	.open = ray_cs_proc_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
2803
};
L
Linus Torvalds 已提交
2804 2805 2806 2807
#endif
/*===========================================================================*/
static int build_auth_frame(ray_dev_t *local, UCHAR *dest, int auth_type)
{
2808 2809 2810 2811 2812 2813 2814
	int addr;
	struct ccs __iomem *pccs;
	struct tx_msg __iomem *ptx;
	int ccsindex;

	/* If no tx buffers available, return */
	if ((ccsindex = get_free_tx_ccs(local)) < 0) {
2815
		pr_debug("ray_cs send authenticate - No free tx ccs\n");
2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846
		return -1;
	}

	pccs = ccs_base(local) + ccsindex;

	/* Address in card space */
	addr = TX_BUF_BASE + (ccsindex << 11);
	/* fill in the CCS */
	writeb(CCS_TX_REQUEST, &pccs->cmd);
	writeb(addr >> 8, pccs->var.tx_request.tx_data_ptr);
	writeb(0x20, pccs->var.tx_request.tx_data_ptr + 1);
	writeb(TX_AUTHENTICATE_LENGTH_MSB, pccs->var.tx_request.tx_data_length);
	writeb(TX_AUTHENTICATE_LENGTH_LSB,
	       pccs->var.tx_request.tx_data_length + 1);
	writeb(0, &pccs->var.tx_request.pow_sav_mode);

	ptx = local->sram + addr;
	/* fill in the mac header */
	writeb(PROTOCOL_VER | AUTHENTIC_TYPE, &ptx->mac.frame_ctl_1);
	writeb(0, &ptx->mac.frame_ctl_2);

	memcpy_toio(ptx->mac.addr_1, dest, ADDRLEN);
	memcpy_toio(ptx->mac.addr_2, local->sparm.b4.a_mac_addr, ADDRLEN);
	memcpy_toio(ptx->mac.addr_3, local->bss_id, ADDRLEN);

	/* Fill in msg body with protocol 00 00, sequence 01 00 ,status 00 00 */
	memset_io(ptx->var, 0, 6);
	writeb(auth_type & 0xff, ptx->var + 2);

	/* Interrupt the firmware to process the command */
	if (interrupt_ecf(local, ccsindex)) {
2847
		pr_debug(
2848 2849 2850 2851 2852
		      "ray_cs send authentication request failed - ECF not ready for intr\n");
		writeb(CCS_BUFFER_FREE, &(pccs++)->buffer_status);
		return -1;
	}
	return 0;
L
Linus Torvalds 已提交
2853 2854 2855 2856
} /* End build_auth_frame */

/*===========================================================================*/
#ifdef CONFIG_PROC_FS
A
Alexey Dobriyan 已提交
2857 2858
static ssize_t ray_cs_essid_proc_write(struct file *file,
		const char __user *buffer, size_t count, loff_t *pos)
L
Linus Torvalds 已提交
2859 2860
{
	static char proc_essid[33];
A
Alan Cox 已提交
2861
	unsigned int len = count;
L
Linus Torvalds 已提交
2862 2863 2864 2865 2866 2867 2868 2869 2870 2871

	if (len > 32)
		len = 32;
	memset(proc_essid, 0, 33);
	if (copy_from_user(proc_essid, buffer, len))
		return -EFAULT;
	essid = proc_essid;
	return count;
}

A
Alexey Dobriyan 已提交
2872 2873 2874 2875 2876 2877 2878
static const struct file_operations ray_cs_essid_proc_fops = {
	.owner		= THIS_MODULE,
	.write		= ray_cs_essid_proc_write,
};

static ssize_t int_proc_write(struct file *file, const char __user *buffer,
			      size_t count, loff_t *pos)
L
Linus Torvalds 已提交
2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897
{
	static char proc_number[10];
	char *p;
	int nr, len;

	if (!count)
		return 0;

	if (count > 9)
		return -EINVAL;
	if (copy_from_user(proc_number, buffer, count))
		return -EFAULT;
	p = proc_number;
	nr = 0;
	len = count;
	do {
		unsigned int c = *p - '0';
		if (c > 9)
			return -EINVAL;
2898
		nr = nr * 10 + c;
L
Linus Torvalds 已提交
2899 2900
		p++;
	} while (--len);
A
Alexey Dobriyan 已提交
2901
	*(int *)PDE(file->f_path.dentry->d_inode)->data = nr;
L
Linus Torvalds 已提交
2902 2903
	return count;
}
A
Alexey Dobriyan 已提交
2904 2905 2906 2907 2908

static const struct file_operations int_proc_fops = {
	.owner		= THIS_MODULE,
	.write		= int_proc_write,
};
L
Linus Torvalds 已提交
2909 2910
#endif

2911 2912 2913 2914
static struct pcmcia_device_id ray_ids[] = {
	PCMCIA_DEVICE_MANF_CARD(0x01a6, 0x0000),
	PCMCIA_DEVICE_NULL,
};
2915

2916 2917
MODULE_DEVICE_TABLE(pcmcia, ray_ids);

L
Linus Torvalds 已提交
2918
static struct pcmcia_driver ray_driver = {
2919 2920 2921 2922 2923 2924 2925 2926 2927
	.owner = THIS_MODULE,
	.drv = {
		.name = "ray_cs",
		},
	.probe = ray_probe,
	.remove = ray_detach,
	.id_table = ray_ids,
	.suspend = ray_suspend,
	.resume = ray_resume,
L
Linus Torvalds 已提交
2928 2929 2930 2931
};

static int __init init_ray_cs(void)
{
2932 2933
	int rc;

2934
	pr_debug("%s\n", rcsid);
2935
	rc = pcmcia_register_driver(&ray_driver);
2936
	pr_debug("raylink init_module register_pcmcia_driver returns 0x%x\n",
2937
	      rc);
L
Linus Torvalds 已提交
2938 2939

#ifdef CONFIG_PROC_FS
2940
	proc_mkdir("driver/ray_cs", NULL);
L
Linus Torvalds 已提交
2941

2942
	proc_create("driver/ray_cs/ray_cs", 0, NULL, &ray_cs_proc_fops);
A
Alexey Dobriyan 已提交
2943 2944 2945
	proc_create("driver/ray_cs/essid", S_IWUSR, NULL, &ray_cs_essid_proc_fops);
	proc_create_data("driver/ray_cs/net_type", S_IWUSR, NULL, &int_proc_fops, &net_type);
	proc_create_data("driver/ray_cs/translate", S_IWUSR, NULL, &int_proc_fops, &translate);
L
Linus Torvalds 已提交
2946
#endif
2947 2948 2949
	if (translate != 0)
		translate = 1;
	return 0;
L
Linus Torvalds 已提交
2950 2951 2952 2953 2954 2955
} /* init_ray_cs */

/*===========================================================================*/

static void __exit exit_ray_cs(void)
{
2956
	pr_debug("ray_cs: cleanup_module\n");
L
Linus Torvalds 已提交
2957 2958

#ifdef CONFIG_PROC_FS
2959 2960 2961 2962 2963
	remove_proc_entry("driver/ray_cs/ray_cs", NULL);
	remove_proc_entry("driver/ray_cs/essid", NULL);
	remove_proc_entry("driver/ray_cs/net_type", NULL);
	remove_proc_entry("driver/ray_cs/translate", NULL);
	remove_proc_entry("driver/ray_cs", NULL);
L
Linus Torvalds 已提交
2964 2965
#endif

2966
	pcmcia_unregister_driver(&ray_driver);
L
Linus Torvalds 已提交
2967 2968 2969 2970 2971 2972
} /* exit_ray_cs */

module_init(init_ray_cs);
module_exit(exit_ray_cs);

/*===========================================================================*/