• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6/drivers/net/wireless/
1/*=============================================================================
2 *
3 * A  PCMCIA client driver for the Raylink wireless LAN card.
4 * The starting point for this module was the skeleton.c in the
5 * PCMCIA 2.9.12 package written by David Hinds, dahinds@users.sourceforge.net
6 *
7 *
8 * Copyright (c) 1998  Corey Thomas (corey@world.std.com)
9 *
10 * 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
12 * published by the Free Software Foundation.
13 *
14 * It is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
22 *
23 * Changes:
24 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 08/08/2000
25 * - reorganize kmallocs in ray_attach, checking all for failure
26 *   and releasing the previous allocations if one fails
27 *
28 * Daniele Bellucci <bellucda@tiscali.it> - 07/10/2003
29 * - Audit copy_to_user in ioctl(SIOCGIWESSID)
30 *
31=============================================================================*/
32
33#include <linux/module.h>
34#include <linux/kernel.h>
35#include <linux/proc_fs.h>
36#include <linux/ptrace.h>
37#include <linux/seq_file.h>
38#include <linux/string.h>
39#include <linux/timer.h>
40#include <linux/init.h>
41#include <linux/netdevice.h>
42#include <linux/etherdevice.h>
43#include <linux/if_arp.h>
44#include <linux/ioport.h>
45#include <linux/skbuff.h>
46#include <linux/ethtool.h>
47#include <linux/ieee80211.h>
48
49#include <pcmcia/cs.h>
50#include <pcmcia/cistpl.h>
51#include <pcmcia/cisreg.h>
52#include <pcmcia/ds.h>
53
54#include <linux/wireless.h>
55#include <net/iw_handler.h>
56
57#include <asm/io.h>
58#include <asm/system.h>
59#include <asm/byteorder.h>
60#include <asm/uaccess.h>
61
62/* Warning : these stuff will slow down the driver... */
63#define WIRELESS_SPY		/* Enable spying addresses */
64/* Definitions we need for spy */
65typedef struct iw_statistics iw_stats;
66typedef u_char mac_addr[ETH_ALEN];	/* Hardware address */
67
68#include "rayctl.h"
69#include "ray_cs.h"
70
71
72/** Prototypes based on PCMCIA skeleton driver *******************************/
73static int ray_config(struct pcmcia_device *link);
74static void ray_release(struct pcmcia_device *link);
75static void ray_detach(struct pcmcia_device *p_dev);
76
77/***** Prototypes indicated by device structure ******************************/
78static int ray_dev_close(struct net_device *dev);
79static int ray_dev_config(struct net_device *dev, struct ifmap *map);
80static struct net_device_stats *ray_get_stats(struct net_device *dev);
81static int ray_dev_init(struct net_device *dev);
82
83static const struct ethtool_ops netdev_ethtool_ops;
84
85static int ray_open(struct net_device *dev);
86static netdev_tx_t ray_dev_start_xmit(struct sk_buff *skb,
87					    struct net_device *dev);
88static void set_multicast_list(struct net_device *dev);
89static void ray_update_multi_list(struct net_device *dev, int all);
90static int translate_frame(ray_dev_t *local, struct tx_msg __iomem *ptx,
91			   unsigned char *data, int len);
92static void ray_build_header(ray_dev_t *local, struct tx_msg __iomem *ptx,
93			     UCHAR msg_type, unsigned char *data);
94static void untranslate(ray_dev_t *local, struct sk_buff *skb, int len);
95static iw_stats *ray_get_wireless_stats(struct net_device *dev);
96static const struct iw_handler_def ray_handler_def;
97
98/***** Prototypes for raylink functions **************************************/
99static void authenticate(ray_dev_t *local);
100static int build_auth_frame(ray_dev_t *local, UCHAR *dest, int auth_type);
101static void authenticate_timeout(u_long);
102static int get_free_ccs(ray_dev_t *local);
103static int get_free_tx_ccs(ray_dev_t *local);
104static void init_startup_params(ray_dev_t *local);
105static int parse_addr(char *in_str, UCHAR *out);
106static int ray_hw_xmit(unsigned char *data, int len, struct net_device *dev, UCHAR type);
107static int ray_init(struct net_device *dev);
108static int interrupt_ecf(ray_dev_t *local, int ccs);
109static void ray_reset(struct net_device *dev);
110static void ray_update_parm(struct net_device *dev, UCHAR objid, UCHAR *value, int len);
111static void verify_dl_startup(u_long);
112
113/* Prototypes for interrpt time functions **********************************/
114static irqreturn_t ray_interrupt(int reg, void *dev_id);
115static void clear_interrupt(ray_dev_t *local);
116static void rx_deauthenticate(ray_dev_t *local, struct rcs __iomem *prcs,
117			      unsigned int pkt_addr, int rx_len);
118static int copy_from_rx_buff(ray_dev_t *local, UCHAR *dest, int pkt_addr, int len);
119static void ray_rx(struct net_device *dev, ray_dev_t *local, struct rcs __iomem *prcs);
120static void release_frag_chain(ray_dev_t *local, struct rcs __iomem *prcs);
121static void rx_authenticate(ray_dev_t *local, struct rcs __iomem *prcs,
122			    unsigned int pkt_addr, int rx_len);
123static void rx_data(struct net_device *dev, struct rcs __iomem *prcs,
124		    unsigned int pkt_addr, int rx_len);
125static void associate(ray_dev_t *local);
126
127/* Card command functions */
128static int dl_startup_params(struct net_device *dev);
129static void join_net(u_long local);
130static void start_net(u_long local);
131/* void start_net(ray_dev_t *local); */
132
133/*===========================================================================*/
134/* Parameters that can be set with 'insmod' */
135
136/* ADHOC=0, Infrastructure=1 */
137static int net_type = ADHOC;
138
139/* Hop dwell time in Kus (1024 us units defined by 802.11) */
140static int hop_dwell = 128;
141
142/* Beacon period in Kus */
143static int beacon_period = 256;
144
145/* power save mode (0 = off, 1 = save power) */
146static int psm;
147
148/* String for network's Extended Service Set ID. 32 Characters max */
149static char *essid;
150
151/* Default to encapsulation unless translation requested */
152static int translate = 1;
153
154static int country = USA;
155
156static int sniffer;
157
158static int bc;
159
160/* 48 bit physical card address if overriding card's real physical
161 * address is required.  Since IEEE 802.11 addresses are 48 bits
162 * like ethernet, an int can't be used, so a string is used. To
163 * allow use of addresses starting with a decimal digit, the first
164 * character must be a letter and will be ignored. This letter is
165 * followed by up to 12 hex digits which are the address.  If less
166 * than 12 digits are used, the address will be left filled with 0's.
167 * Note that bit 0 of the first byte is the broadcast bit, and evil
168 * things will happen if it is not 0 in a card address.
169 */
170static char *phy_addr = NULL;
171
172
173/* A struct pcmcia_device structure has fields for most things that are needed
174   to keep track of a socket, but there will usually be some device
175   specific information that also needs to be kept track of.  The
176   'priv' pointer in a struct pcmcia_device structure can be used to point to
177   a device-specific private data structure, like this.
178*/
179static unsigned int ray_mem_speed = 500;
180
181/* WARNING: THIS DRIVER IS NOT CAPABLE OF HANDLING MULTIPLE DEVICES! */
182static struct pcmcia_device *this_device = NULL;
183
184MODULE_AUTHOR("Corey Thomas <corey@world.std.com>");
185MODULE_DESCRIPTION("Raylink/WebGear wireless LAN driver");
186MODULE_LICENSE("GPL");
187
188module_param(net_type, int, 0);
189module_param(hop_dwell, int, 0);
190module_param(beacon_period, int, 0);
191module_param(psm, int, 0);
192module_param(essid, charp, 0);
193module_param(translate, int, 0);
194module_param(country, int, 0);
195module_param(sniffer, int, 0);
196module_param(bc, int, 0);
197module_param(phy_addr, charp, 0);
198module_param(ray_mem_speed, int, 0);
199
200static UCHAR b5_default_startup_parms[] = {
201	0, 0,			/* Adhoc station */
202	'L', 'I', 'N', 'U', 'X', 0, 0, 0,	/* 32 char ESSID */
203	0, 0, 0, 0, 0, 0, 0, 0,
204	0, 0, 0, 0, 0, 0, 0, 0,
205	0, 0, 0, 0, 0, 0, 0, 0,
206	1, 0,			/* Active scan, CA Mode */
207	0, 0, 0, 0, 0, 0,	/* No default MAC addr  */
208	0x7f, 0xff,		/* Frag threshold */
209	0x00, 0x80,		/* Hop time 128 Kus */
210	0x01, 0x00,		/* Beacon period 256 Kus */
211	0x01, 0x07, 0xa3,	/* DTIM, retries, ack timeout */
212	0x1d, 0x82, 0x4e,	/* SIFS, DIFS, PIFS */
213	0x7f, 0xff,		/* RTS threshold */
214	0x04, 0xe2, 0x38, 0xA4,	/* scan_dwell, max_scan_dwell */
215	0x05,			/* assoc resp timeout thresh */
216	0x08, 0x02, 0x08,	/* adhoc, infra, super cycle max */
217	0,			/* Promiscuous mode */
218	0x0c, 0x0bd,		/* Unique word */
219	0x32,			/* Slot time */
220	0xff, 0xff,		/* roam-low snr, low snr count */
221	0x05, 0xff,		/* Infra, adhoc missed bcn thresh */
222	0x01, 0x0b, 0x4f,	/* USA, hop pattern, hop pat length */
223/* b4 - b5 differences start here */
224	0x00, 0x3f,		/* CW max */
225	0x00, 0x0f,		/* CW min */
226	0x04, 0x08,		/* Noise gain, limit offset */
227	0x28, 0x28,		/* det rssi, med busy offsets */
228	7,			/* det sync thresh */
229	0, 2, 2,		/* test mode, min, max */
230	0,			/* allow broadcast SSID probe resp */
231	0, 0,			/* privacy must start, can join */
232	2, 0, 0, 0, 0, 0, 0, 0	/* basic rate set */
233};
234
235static UCHAR b4_default_startup_parms[] = {
236	0, 0,			/* Adhoc station */
237	'L', 'I', 'N', 'U', 'X', 0, 0, 0,	/* 32 char ESSID */
238	0, 0, 0, 0, 0, 0, 0, 0,
239	0, 0, 0, 0, 0, 0, 0, 0,
240	0, 0, 0, 0, 0, 0, 0, 0,
241	1, 0,			/* Active scan, CA Mode */
242	0, 0, 0, 0, 0, 0,	/* No default MAC addr  */
243	0x7f, 0xff,		/* Frag threshold */
244	0x02, 0x00,		/* Hop time */
245	0x00, 0x01,		/* Beacon period */
246	0x01, 0x07, 0xa3,	/* DTIM, retries, ack timeout */
247	0x1d, 0x82, 0xce,	/* SIFS, DIFS, PIFS */
248	0x7f, 0xff,		/* RTS threshold */
249	0xfb, 0x1e, 0xc7, 0x5c,	/* scan_dwell, max_scan_dwell */
250	0x05,			/* assoc resp timeout thresh */
251	0x04, 0x02, 0x4,	/* adhoc, infra, super cycle max */
252	0,			/* Promiscuous mode */
253	0x0c, 0x0bd,		/* Unique word */
254	0x4e,			/* Slot time (TBD seems wrong) */
255	0xff, 0xff,		/* roam-low snr, low snr count */
256	0x05, 0xff,		/* Infra, adhoc missed bcn thresh */
257	0x01, 0x0b, 0x4e,	/* USA, hop pattern, hop pat length */
258/* b4 - b5 differences start here */
259	0x3f, 0x0f,		/* CW max, min */
260	0x04, 0x08,		/* Noise gain, limit offset */
261	0x28, 0x28,		/* det rssi, med busy offsets */
262	7,			/* det sync thresh */
263	0, 2, 2			/* test mode, min, max */
264};
265
266/*===========================================================================*/
267static unsigned char eth2_llc[] = { 0xaa, 0xaa, 3, 0, 0, 0 };
268
269static char hop_pattern_length[] = { 1,
270	USA_HOP_MOD, EUROPE_HOP_MOD,
271	JAPAN_HOP_MOD, KOREA_HOP_MOD,
272	SPAIN_HOP_MOD, FRANCE_HOP_MOD,
273	ISRAEL_HOP_MOD, AUSTRALIA_HOP_MOD,
274	JAPAN_TEST_HOP_MOD
275};
276
277static char rcsid[] =
278    "Raylink/WebGear wireless LAN - Corey <Thomas corey@world.std.com>";
279
280static const struct net_device_ops ray_netdev_ops = {
281	.ndo_init 		= ray_dev_init,
282	.ndo_open 		= ray_open,
283	.ndo_stop 		= ray_dev_close,
284	.ndo_start_xmit		= ray_dev_start_xmit,
285	.ndo_set_config		= ray_dev_config,
286	.ndo_get_stats		= ray_get_stats,
287	.ndo_set_multicast_list = set_multicast_list,
288	.ndo_change_mtu		= eth_change_mtu,
289	.ndo_set_mac_address 	= eth_mac_addr,
290	.ndo_validate_addr	= eth_validate_addr,
291};
292
293/*=============================================================================
294    ray_attach() creates an "instance" of the driver, allocating
295    local data structures for one device.  The device is registered
296    with Card Services.
297    The dev_link structure is initialized, but we don't actually
298    configure the card at this point -- we wait until we receive a
299    card insertion event.
300=============================================================================*/
301static int ray_probe(struct pcmcia_device *p_dev)
302{
303	ray_dev_t *local;
304	struct net_device *dev;
305
306	dev_dbg(&p_dev->dev, "ray_attach()\n");
307
308	/* Allocate space for private device-specific data */
309	dev = alloc_etherdev(sizeof(ray_dev_t));
310	if (!dev)
311		goto fail_alloc_dev;
312
313	local = netdev_priv(dev);
314	local->finder = p_dev;
315
316	/* The io structure describes IO port mapping. None used here */
317	p_dev->resource[0]->end = 0;
318	p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
319
320	/* General socket configuration */
321	p_dev->conf.Attributes = CONF_ENABLE_IRQ;
322	p_dev->conf.IntType = INT_MEMORY_AND_IO;
323	p_dev->conf.ConfigIndex = 1;
324
325	p_dev->priv = dev;
326
327	local->finder = p_dev;
328	local->card_status = CARD_INSERTED;
329	local->authentication_state = UNAUTHENTICATED;
330	local->num_multi = 0;
331	dev_dbg(&p_dev->dev, "ray_attach p_dev = %p,  dev = %p,  local = %p, intr = %p\n",
332	      p_dev, dev, local, &ray_interrupt);
333
334	/* Raylink entries in the device structure */
335	dev->netdev_ops = &ray_netdev_ops;
336	SET_ETHTOOL_OPS(dev, &netdev_ethtool_ops);
337	dev->wireless_handlers = &ray_handler_def;
338#ifdef WIRELESS_SPY
339	local->wireless_data.spy_data = &local->spy_data;
340	dev->wireless_data = &local->wireless_data;
341#endif /* WIRELESS_SPY */
342
343
344	dev_dbg(&p_dev->dev, "ray_cs ray_attach calling ether_setup.)\n");
345	netif_stop_queue(dev);
346
347	init_timer(&local->timer);
348
349	this_device = p_dev;
350	return ray_config(p_dev);
351
352fail_alloc_dev:
353	return -ENOMEM;
354} /* ray_attach */
355
356/*=============================================================================
357    This deletes a driver "instance".  The device is de-registered
358    with Card Services.  If it has been released, all local data
359    structures are freed.  Otherwise, the structures will be freed
360    when the device is released.
361=============================================================================*/
362static void ray_detach(struct pcmcia_device *link)
363{
364	struct net_device *dev;
365	ray_dev_t *local;
366
367	dev_dbg(&link->dev, "ray_detach\n");
368
369	this_device = NULL;
370	dev = link->priv;
371
372	ray_release(link);
373
374	local = netdev_priv(dev);
375	del_timer(&local->timer);
376
377	if (link->priv) {
378		unregister_netdev(dev);
379		free_netdev(dev);
380	}
381	dev_dbg(&link->dev, "ray_cs ray_detach ending\n");
382} /* ray_detach */
383
384/*=============================================================================
385    ray_config() is run after a CARD_INSERTION event
386    is received, to configure the PCMCIA socket, and to make the
387    ethernet device available to the system.
388=============================================================================*/
389#define MAX_TUPLE_SIZE 128
390static int ray_config(struct pcmcia_device *link)
391{
392	int ret = 0;
393	int i;
394	win_req_t req;
395	struct net_device *dev = (struct net_device *)link->priv;
396	ray_dev_t *local = netdev_priv(dev);
397
398	dev_dbg(&link->dev, "ray_config\n");
399
400	/* Determine card type and firmware version */
401	printk(KERN_INFO "ray_cs Detected: %s%s%s%s\n",
402	       link->prod_id[0] ? link->prod_id[0] : " ",
403	       link->prod_id[1] ? link->prod_id[1] : " ",
404	       link->prod_id[2] ? link->prod_id[2] : " ",
405	       link->prod_id[3] ? link->prod_id[3] : " ");
406
407	/* Now allocate an interrupt line.  Note that this does not
408	   actually assign a handler to the interrupt.
409	 */
410	ret = pcmcia_request_irq(link, ray_interrupt);
411	if (ret)
412		goto failed;
413	dev->irq = link->irq;
414
415	/* This actually configures the PCMCIA socket -- setting up
416	   the I/O windows and the interrupt mapping.
417	 */
418	ret = pcmcia_request_configuration(link, &link->conf);
419	if (ret)
420		goto failed;
421
422/*** Set up 32k window for shared memory (transmit and control) ************/
423	req.Attributes =
424	    WIN_DATA_WIDTH_8 | WIN_MEMORY_TYPE_CM | WIN_ENABLE | WIN_USE_WAIT;
425	req.Base = 0;
426	req.Size = 0x8000;
427	req.AccessSpeed = ray_mem_speed;
428	ret = pcmcia_request_window(link, &req, &link->win);
429	if (ret)
430		goto failed;
431	ret = pcmcia_map_mem_page(link, link->win, 0);
432	if (ret)
433		goto failed;
434	local->sram = ioremap(req.Base, req.Size);
435
436/*** Set up 16k window for shared memory (receive buffer) ***************/
437	req.Attributes =
438	    WIN_DATA_WIDTH_8 | WIN_MEMORY_TYPE_CM | WIN_ENABLE | WIN_USE_WAIT;
439	req.Base = 0;
440	req.Size = 0x4000;
441	req.AccessSpeed = ray_mem_speed;
442	ret = pcmcia_request_window(link, &req, &local->rmem_handle);
443	if (ret)
444		goto failed;
445	ret = pcmcia_map_mem_page(link, local->rmem_handle, 0x8000);
446	if (ret)
447		goto failed;
448	local->rmem = ioremap(req.Base, req.Size);
449
450/*** Set up window for attribute memory ***********************************/
451	req.Attributes =
452	    WIN_DATA_WIDTH_8 | WIN_MEMORY_TYPE_AM | WIN_ENABLE | WIN_USE_WAIT;
453	req.Base = 0;
454	req.Size = 0x1000;
455	req.AccessSpeed = ray_mem_speed;
456	ret = pcmcia_request_window(link, &req, &local->amem_handle);
457	if (ret)
458		goto failed;
459	ret = pcmcia_map_mem_page(link, local->amem_handle, 0);
460	if (ret)
461		goto failed;
462	local->amem = ioremap(req.Base, req.Size);
463
464	dev_dbg(&link->dev, "ray_config sram=%p\n", local->sram);
465	dev_dbg(&link->dev, "ray_config rmem=%p\n", local->rmem);
466	dev_dbg(&link->dev, "ray_config amem=%p\n", local->amem);
467	if (ray_init(dev) < 0) {
468		ray_release(link);
469		return -ENODEV;
470	}
471
472	SET_NETDEV_DEV(dev, &link->dev);
473	i = register_netdev(dev);
474	if (i != 0) {
475		printk("ray_config register_netdev() failed\n");
476		ray_release(link);
477		return i;
478	}
479
480	printk(KERN_INFO "%s: RayLink, irq %d, hw_addr %pM\n",
481	       dev->name, dev->irq, dev->dev_addr);
482
483	return 0;
484
485failed:
486	ray_release(link);
487	return -ENODEV;
488} /* ray_config */
489
490static inline struct ccs __iomem *ccs_base(ray_dev_t *dev)
491{
492	return dev->sram + CCS_BASE;
493}
494
495static inline struct rcs __iomem *rcs_base(ray_dev_t *dev)
496{
497	/*
498	 * This looks nonsensical, since there is a separate
499	 * RCS_BASE. But the difference between a "struct rcs"
500	 * and a "struct ccs" ends up being in the _index_ off
501	 * the base, so the base pointer is the same for both
502	 * ccs/rcs.
503	 */
504	return dev->sram + CCS_BASE;
505}
506
507/*===========================================================================*/
508static int ray_init(struct net_device *dev)
509{
510	int i;
511	UCHAR *p;
512	struct ccs __iomem *pccs;
513	ray_dev_t *local = netdev_priv(dev);
514	struct pcmcia_device *link = local->finder;
515	dev_dbg(&link->dev, "ray_init(0x%p)\n", dev);
516	if (!(pcmcia_dev_present(link))) {
517		dev_dbg(&link->dev, "ray_init - device not present\n");
518		return -1;
519	}
520
521	local->net_type = net_type;
522	local->sta_type = TYPE_STA;
523
524	/* Copy the startup results to local memory */
525	memcpy_fromio(&local->startup_res, local->sram + ECF_TO_HOST_BASE,
526		      sizeof(struct startup_res_6));
527
528	/* Check Power up test status and get mac address from card */
529	if (local->startup_res.startup_word != 0x80) {
530		printk(KERN_INFO "ray_init ERROR card status = %2x\n",
531		       local->startup_res.startup_word);
532		local->card_status = CARD_INIT_ERROR;
533		return -1;
534	}
535
536	local->fw_ver = local->startup_res.firmware_version[0];
537	local->fw_bld = local->startup_res.firmware_version[1];
538	local->fw_var = local->startup_res.firmware_version[2];
539	dev_dbg(&link->dev, "ray_init firmware version %d.%d\n", local->fw_ver,
540	      local->fw_bld);
541
542	local->tib_length = 0x20;
543	if ((local->fw_ver == 5) && (local->fw_bld >= 30))
544		local->tib_length = local->startup_res.tib_length;
545	dev_dbg(&link->dev, "ray_init tib_length = 0x%02x\n", local->tib_length);
546	/* Initialize CCS's to buffer free state */
547	pccs = ccs_base(local);
548	for (i = 0; i < NUMBER_OF_CCS; i++) {
549		writeb(CCS_BUFFER_FREE, &(pccs++)->buffer_status);
550	}
551	init_startup_params(local);
552
553	/* copy mac address to startup parameters */
554	if (parse_addr(phy_addr, local->sparm.b4.a_mac_addr)) {
555		p = local->sparm.b4.a_mac_addr;
556	} else {
557		memcpy(&local->sparm.b4.a_mac_addr,
558		       &local->startup_res.station_addr, ADDRLEN);
559		p = local->sparm.b4.a_mac_addr;
560	}
561
562	clear_interrupt(local);	/* Clear any interrupt from the card */
563	local->card_status = CARD_AWAITING_PARAM;
564	dev_dbg(&link->dev, "ray_init ending\n");
565	return 0;
566} /* ray_init */
567
568/*===========================================================================*/
569/* Download startup parameters to the card and command it to read them       */
570static int dl_startup_params(struct net_device *dev)
571{
572	int ccsindex;
573	ray_dev_t *local = netdev_priv(dev);
574	struct ccs __iomem *pccs;
575	struct pcmcia_device *link = local->finder;
576
577	dev_dbg(&link->dev, "dl_startup_params entered\n");
578	if (!(pcmcia_dev_present(link))) {
579		dev_dbg(&link->dev, "ray_cs dl_startup_params - device not present\n");
580		return -1;
581	}
582
583	/* Copy parameters to host to ECF area */
584	if (local->fw_ver == 0x55)
585		memcpy_toio(local->sram + HOST_TO_ECF_BASE, &local->sparm.b4,
586			    sizeof(struct b4_startup_params));
587	else
588		memcpy_toio(local->sram + HOST_TO_ECF_BASE, &local->sparm.b5,
589			    sizeof(struct b5_startup_params));
590
591	/* Fill in the CCS fields for the ECF */
592	if ((ccsindex = get_free_ccs(local)) < 0)
593		return -1;
594	local->dl_param_ccs = ccsindex;
595	pccs = ccs_base(local) + ccsindex;
596	writeb(CCS_DOWNLOAD_STARTUP_PARAMS, &pccs->cmd);
597	dev_dbg(&link->dev, "dl_startup_params start ccsindex = %d\n",
598	      local->dl_param_ccs);
599	/* Interrupt the firmware to process the command */
600	if (interrupt_ecf(local, ccsindex)) {
601		printk(KERN_INFO "ray dl_startup_params failed - "
602		       "ECF not ready for intr\n");
603		local->card_status = CARD_DL_PARAM_ERROR;
604		writeb(CCS_BUFFER_FREE, &(pccs++)->buffer_status);
605		return -2;
606	}
607	local->card_status = CARD_DL_PARAM;
608	/* Start kernel timer to wait for dl startup to complete. */
609	local->timer.expires = jiffies + HZ / 2;
610	local->timer.data = (long)local;
611	local->timer.function = &verify_dl_startup;
612	add_timer(&local->timer);
613	dev_dbg(&link->dev,
614	      "ray_cs dl_startup_params started timer for verify_dl_startup\n");
615	return 0;
616} /* dl_startup_params */
617
618/*===========================================================================*/
619static void init_startup_params(ray_dev_t *local)
620{
621	int i;
622
623	if (country > JAPAN_TEST)
624		country = USA;
625	else if (country < USA)
626		country = USA;
627	/* structure for hop time and beacon period is defined here using
628	 * New 802.11D6.1 format.  Card firmware is still using old format
629	 * until version 6.
630	 *    Before                    After
631	 *    a_hop_time ms byte        a_hop_time ms byte
632	 *    a_hop_time 2s byte        a_hop_time ls byte
633	 *    a_hop_time ls byte        a_beacon_period ms byte
634	 *    a_beacon_period           a_beacon_period ls byte
635	 *
636	 *    a_hop_time = uS           a_hop_time = KuS
637	 *    a_beacon_period = hops    a_beacon_period = KuS
638	 *//* 64ms = 010000 */
639	if (local->fw_ver == 0x55) {
640		memcpy((UCHAR *) &local->sparm.b4, b4_default_startup_parms,
641		       sizeof(struct b4_startup_params));
642		/* Translate sane kus input values to old build 4/5 format */
643		/* i = hop time in uS truncated to 3 bytes */
644		i = (hop_dwell * 1024) & 0xffffff;
645		local->sparm.b4.a_hop_time[0] = (i >> 16) & 0xff;
646		local->sparm.b4.a_hop_time[1] = (i >> 8) & 0xff;
647		local->sparm.b4.a_beacon_period[0] = 0;
648		local->sparm.b4.a_beacon_period[1] =
649		    ((beacon_period / hop_dwell) - 1) & 0xff;
650		local->sparm.b4.a_curr_country_code = country;
651		local->sparm.b4.a_hop_pattern_length =
652		    hop_pattern_length[(int)country] - 1;
653		if (bc) {
654			local->sparm.b4.a_ack_timeout = 0x50;
655			local->sparm.b4.a_sifs = 0x3f;
656		}
657	} else { /* Version 5 uses real kus values */
658		memcpy((UCHAR *) &local->sparm.b5, b5_default_startup_parms,
659		       sizeof(struct b5_startup_params));
660
661		local->sparm.b5.a_hop_time[0] = (hop_dwell >> 8) & 0xff;
662		local->sparm.b5.a_hop_time[1] = hop_dwell & 0xff;
663		local->sparm.b5.a_beacon_period[0] =
664		    (beacon_period >> 8) & 0xff;
665		local->sparm.b5.a_beacon_period[1] = beacon_period & 0xff;
666		if (psm)
667			local->sparm.b5.a_power_mgt_state = 1;
668		local->sparm.b5.a_curr_country_code = country;
669		local->sparm.b5.a_hop_pattern_length =
670		    hop_pattern_length[(int)country];
671	}
672
673	local->sparm.b4.a_network_type = net_type & 0x01;
674	local->sparm.b4.a_acting_as_ap_status = TYPE_STA;
675
676	if (essid != NULL)
677		strncpy(local->sparm.b4.a_current_ess_id, essid, ESSID_SIZE);
678} /* init_startup_params */
679
680/*===========================================================================*/
681static void verify_dl_startup(u_long data)
682{
683	ray_dev_t *local = (ray_dev_t *) data;
684	struct ccs __iomem *pccs = ccs_base(local) + local->dl_param_ccs;
685	UCHAR status;
686	struct pcmcia_device *link = local->finder;
687
688	if (!(pcmcia_dev_present(link))) {
689		dev_dbg(&link->dev, "ray_cs verify_dl_startup - device not present\n");
690		return;
691	}
692
693	status = readb(&pccs->buffer_status);
694	if (status != CCS_BUFFER_FREE) {
695		printk(KERN_INFO
696		       "Download startup params failed.  Status = %d\n",
697		       status);
698		local->card_status = CARD_DL_PARAM_ERROR;
699		return;
700	}
701	if (local->sparm.b4.a_network_type == ADHOC)
702		start_net((u_long) local);
703	else
704		join_net((u_long) local);
705} /* end verify_dl_startup */
706
707/*===========================================================================*/
708/* Command card to start a network */
709static void start_net(u_long data)
710{
711	ray_dev_t *local = (ray_dev_t *) data;
712	struct ccs __iomem *pccs;
713	int ccsindex;
714	struct pcmcia_device *link = local->finder;
715	if (!(pcmcia_dev_present(link))) {
716		dev_dbg(&link->dev, "ray_cs start_net - device not present\n");
717		return;
718	}
719	/* Fill in the CCS fields for the ECF */
720	if ((ccsindex = get_free_ccs(local)) < 0)
721		return;
722	pccs = ccs_base(local) + ccsindex;
723	writeb(CCS_START_NETWORK, &pccs->cmd);
724	writeb(0, &pccs->var.start_network.update_param);
725	/* Interrupt the firmware to process the command */
726	if (interrupt_ecf(local, ccsindex)) {
727		dev_dbg(&link->dev, "ray start net failed - card not ready for intr\n");
728		writeb(CCS_BUFFER_FREE, &(pccs++)->buffer_status);
729		return;
730	}
731	local->card_status = CARD_DOING_ACQ;
732} /* end start_net */
733
734/*===========================================================================*/
735/* Command card to join a network */
736static void join_net(u_long data)
737{
738	ray_dev_t *local = (ray_dev_t *) data;
739
740	struct ccs __iomem *pccs;
741	int ccsindex;
742	struct pcmcia_device *link = local->finder;
743
744	if (!(pcmcia_dev_present(link))) {
745		dev_dbg(&link->dev, "ray_cs join_net - device not present\n");
746		return;
747	}
748	/* Fill in the CCS fields for the ECF */
749	if ((ccsindex = get_free_ccs(local)) < 0)
750		return;
751	pccs = ccs_base(local) + ccsindex;
752	writeb(CCS_JOIN_NETWORK, &pccs->cmd);
753	writeb(0, &pccs->var.join_network.update_param);
754	writeb(0, &pccs->var.join_network.net_initiated);
755	/* Interrupt the firmware to process the command */
756	if (interrupt_ecf(local, ccsindex)) {
757		dev_dbg(&link->dev, "ray join net failed - card not ready for intr\n");
758		writeb(CCS_BUFFER_FREE, &(pccs++)->buffer_status);
759		return;
760	}
761	local->card_status = CARD_DOING_ACQ;
762}
763
764/*============================================================================
765    After a card is removed, ray_release() will unregister the net
766    device, and release the PCMCIA configuration.  If the device is
767    still open, this will be postponed until it is closed.
768=============================================================================*/
769static void ray_release(struct pcmcia_device *link)
770{
771	struct net_device *dev = link->priv;
772	ray_dev_t *local = netdev_priv(dev);
773
774	dev_dbg(&link->dev, "ray_release\n");
775
776	del_timer(&local->timer);
777
778	iounmap(local->sram);
779	iounmap(local->rmem);
780	iounmap(local->amem);
781	pcmcia_disable_device(link);
782
783	dev_dbg(&link->dev, "ray_release ending\n");
784}
785
786static int ray_suspend(struct pcmcia_device *link)
787{
788	struct net_device *dev = link->priv;
789
790	if (link->open)
791		netif_device_detach(dev);
792
793	return 0;
794}
795
796static int ray_resume(struct pcmcia_device *link)
797{
798	struct net_device *dev = link->priv;
799
800	if (link->open) {
801		ray_reset(dev);
802		netif_device_attach(dev);
803	}
804
805	return 0;
806}
807
808/*===========================================================================*/
809static int ray_dev_init(struct net_device *dev)
810{
811#ifdef RAY_IMMEDIATE_INIT
812	int i;
813#endif /* RAY_IMMEDIATE_INIT */
814	ray_dev_t *local = netdev_priv(dev);
815	struct pcmcia_device *link = local->finder;
816
817	dev_dbg(&link->dev, "ray_dev_init(dev=%p)\n", dev);
818	if (!(pcmcia_dev_present(link))) {
819		dev_dbg(&link->dev, "ray_dev_init - device not present\n");
820		return -1;
821	}
822#ifdef RAY_IMMEDIATE_INIT
823	/* Download startup parameters */
824	if ((i = dl_startup_params(dev)) < 0) {
825		printk(KERN_INFO "ray_dev_init dl_startup_params failed - "
826		       "returns 0x%x\n", i);
827		return -1;
828	}
829#else /* RAY_IMMEDIATE_INIT */
830	/* Postpone the card init so that we can still configure the card,
831	 * for example using the Wireless Extensions. The init will happen
832	 * in ray_open() - Jean II */
833	dev_dbg(&link->dev,
834	      "ray_dev_init: postponing card init to ray_open() ; Status = %d\n",
835	      local->card_status);
836#endif /* RAY_IMMEDIATE_INIT */
837
838	/* copy mac and broadcast addresses to linux device */
839	memcpy(dev->dev_addr, &local->sparm.b4.a_mac_addr, ADDRLEN);
840	memset(dev->broadcast, 0xff, ETH_ALEN);
841
842	dev_dbg(&link->dev, "ray_dev_init ending\n");
843	return 0;
844}
845
846/*===========================================================================*/
847static int ray_dev_config(struct net_device *dev, struct ifmap *map)
848{
849	ray_dev_t *local = netdev_priv(dev);
850	struct pcmcia_device *link = local->finder;
851	/* Dummy routine to satisfy device structure */
852	dev_dbg(&link->dev, "ray_dev_config(dev=%p,ifmap=%p)\n", dev, map);
853	if (!(pcmcia_dev_present(link))) {
854		dev_dbg(&link->dev, "ray_dev_config - device not present\n");
855		return -1;
856	}
857
858	return 0;
859}
860
861/*===========================================================================*/
862static netdev_tx_t ray_dev_start_xmit(struct sk_buff *skb,
863					    struct net_device *dev)
864{
865	ray_dev_t *local = netdev_priv(dev);
866	struct pcmcia_device *link = local->finder;
867	short length = skb->len;
868
869	if (!pcmcia_dev_present(link)) {
870		dev_dbg(&link->dev, "ray_dev_start_xmit - device not present\n");
871		dev_kfree_skb(skb);
872		return NETDEV_TX_OK;
873	}
874
875	dev_dbg(&link->dev, "ray_dev_start_xmit(skb=%p, dev=%p)\n", skb, dev);
876	if (local->authentication_state == NEED_TO_AUTH) {
877		dev_dbg(&link->dev, "ray_cs Sending authentication request.\n");
878		if (!build_auth_frame(local, local->auth_id, OPEN_AUTH_REQUEST)) {
879			local->authentication_state = AUTHENTICATED;
880			netif_stop_queue(dev);
881			return NETDEV_TX_BUSY;
882		}
883	}
884
885	if (length < ETH_ZLEN) {
886		if (skb_padto(skb, ETH_ZLEN))
887			return NETDEV_TX_OK;
888		length = ETH_ZLEN;
889	}
890	switch (ray_hw_xmit(skb->data, length, dev, DATA_TYPE)) {
891	case XMIT_NO_CCS:
892	case XMIT_NEED_AUTH:
893		netif_stop_queue(dev);
894		return NETDEV_TX_BUSY;
895	case XMIT_NO_INTR:
896	case XMIT_MSG_BAD:
897	case XMIT_OK:
898	default:
899		dev_kfree_skb(skb);
900	}
901
902	return NETDEV_TX_OK;
903} /* ray_dev_start_xmit */
904
905/*===========================================================================*/
906static int ray_hw_xmit(unsigned char *data, int len, struct net_device *dev,
907		       UCHAR msg_type)
908{
909	ray_dev_t *local = netdev_priv(dev);
910	struct ccs __iomem *pccs;
911	int ccsindex;
912	int offset;
913	struct tx_msg __iomem *ptx;	/* Address of xmit buffer in PC space */
914	short int addr;		/* Address of xmit buffer in card space */
915
916	pr_debug("ray_hw_xmit(data=%p, len=%d, dev=%p)\n", data, len, dev);
917	if (len + TX_HEADER_LENGTH > TX_BUF_SIZE) {
918		printk(KERN_INFO "ray_hw_xmit packet too large: %d bytes\n",
919		       len);
920		return XMIT_MSG_BAD;
921	}
922	switch (ccsindex = get_free_tx_ccs(local)) {
923	case ECCSBUSY:
924		pr_debug("ray_hw_xmit tx_ccs table busy\n");
925	case ECCSFULL:
926		pr_debug("ray_hw_xmit No free tx ccs\n");
927	case ECARDGONE:
928		netif_stop_queue(dev);
929		return XMIT_NO_CCS;
930	default:
931		break;
932	}
933	addr = TX_BUF_BASE + (ccsindex << 11);
934
935	if (msg_type == DATA_TYPE) {
936		local->stats.tx_bytes += len;
937		local->stats.tx_packets++;
938	}
939
940	ptx = local->sram + addr;
941
942	ray_build_header(local, ptx, msg_type, data);
943	if (translate) {
944		offset = translate_frame(local, ptx, data, len);
945	} else { /* Encapsulate frame */
946		/* TBD TIB length will move address of ptx->var */
947		memcpy_toio(&ptx->var, data, len);
948		offset = 0;
949	}
950
951	/* fill in the CCS */
952	pccs = ccs_base(local) + ccsindex;
953	len += TX_HEADER_LENGTH + offset;
954	writeb(CCS_TX_REQUEST, &pccs->cmd);
955	writeb(addr >> 8, &pccs->var.tx_request.tx_data_ptr[0]);
956	writeb(local->tib_length, &pccs->var.tx_request.tx_data_ptr[1]);
957	writeb(len >> 8, &pccs->var.tx_request.tx_data_length[0]);
958	writeb(len & 0xff, &pccs->var.tx_request.tx_data_length[1]);
959/* TBD still need psm_cam? */
960	writeb(PSM_CAM, &pccs->var.tx_request.pow_sav_mode);
961	writeb(local->net_default_tx_rate, &pccs->var.tx_request.tx_rate);
962	writeb(0, &pccs->var.tx_request.antenna);
963	pr_debug("ray_hw_xmit default_tx_rate = 0x%x\n",
964	      local->net_default_tx_rate);
965
966	/* Interrupt the firmware to process the command */
967	if (interrupt_ecf(local, ccsindex)) {
968		pr_debug("ray_hw_xmit failed - ECF not ready for intr\n");
969/* TBD very inefficient to copy packet to buffer, and then not
970   send it, but the alternative is to queue the messages and that
971   won't be done for a while.  Maybe set tbusy until a CCS is free?
972*/
973		writeb(CCS_BUFFER_FREE, &pccs->buffer_status);
974		return XMIT_NO_INTR;
975	}
976	return XMIT_OK;
977} /* end ray_hw_xmit */
978
979/*===========================================================================*/
980static int translate_frame(ray_dev_t *local, struct tx_msg __iomem *ptx,
981			   unsigned char *data, int len)
982{
983	__be16 proto = ((struct ethhdr *)data)->h_proto;
984	if (ntohs(proto) >= 1536) { /* DIX II ethernet frame */
985		pr_debug("ray_cs translate_frame DIX II\n");
986		/* Copy LLC header to card buffer */
987		memcpy_toio(&ptx->var, eth2_llc, sizeof(eth2_llc));
988		memcpy_toio(((void __iomem *)&ptx->var) + sizeof(eth2_llc),
989			    (UCHAR *) &proto, 2);
990		if (proto == htons(ETH_P_AARP) || proto == htons(ETH_P_IPX)) {
991			/* This is the selective translation table, only 2 entries */
992			writeb(0xf8,
993			       &((struct snaphdr_t __iomem *)ptx->var)->org[3]);
994		}
995		/* Copy body of ethernet packet without ethernet header */
996		memcpy_toio((void __iomem *)&ptx->var +
997			    sizeof(struct snaphdr_t), data + ETH_HLEN,
998			    len - ETH_HLEN);
999		return (int)sizeof(struct snaphdr_t) - ETH_HLEN;
1000	} else { /* already  802 type, and proto is length */
1001		pr_debug("ray_cs translate_frame 802\n");
1002		if (proto == htons(0xffff)) { /* evil netware IPX 802.3 without LLC */
1003			pr_debug("ray_cs translate_frame evil IPX\n");
1004			memcpy_toio(&ptx->var, data + ETH_HLEN, len - ETH_HLEN);
1005			return 0 - ETH_HLEN;
1006		}
1007		memcpy_toio(&ptx->var, data + ETH_HLEN, len - ETH_HLEN);
1008		return 0 - ETH_HLEN;
1009	}
1010	/* TBD do other frame types */
1011} /* end translate_frame */
1012
1013/*===========================================================================*/
1014static void ray_build_header(ray_dev_t *local, struct tx_msg __iomem *ptx,
1015			     UCHAR msg_type, unsigned char *data)
1016{
1017	writeb(PROTOCOL_VER | msg_type, &ptx->mac.frame_ctl_1);
1018/*** IEEE 802.11 Address field assignments *************
1019		TODS	FROMDS	addr_1		addr_2		addr_3	addr_4
1020Adhoc		0	0	dest		src (terminal)	BSSID	N/A
1021AP to Terminal	0	1	dest		AP(BSSID)	source	N/A
1022Terminal to AP	1	0	AP(BSSID)	src (terminal)	dest	N/A
1023AP to AP	1	1	dest AP		src AP		dest	source
1024*******************************************************/
1025	if (local->net_type == ADHOC) {
1026		writeb(0, &ptx->mac.frame_ctl_2);
1027		memcpy_toio(ptx->mac.addr_1, ((struct ethhdr *)data)->h_dest,
1028			    2 * ADDRLEN);
1029		memcpy_toio(ptx->mac.addr_3, local->bss_id, ADDRLEN);
1030	} else { /* infrastructure */
1031
1032		if (local->sparm.b4.a_acting_as_ap_status) {
1033			writeb(FC2_FROM_DS, &ptx->mac.frame_ctl_2);
1034			memcpy_toio(ptx->mac.addr_1,
1035				    ((struct ethhdr *)data)->h_dest, ADDRLEN);
1036			memcpy_toio(ptx->mac.addr_2, local->bss_id, 6);
1037			memcpy_toio(ptx->mac.addr_3,
1038				    ((struct ethhdr *)data)->h_source, ADDRLEN);
1039		} else { /* Terminal */
1040
1041			writeb(FC2_TO_DS, &ptx->mac.frame_ctl_2);
1042			memcpy_toio(ptx->mac.addr_1, local->bss_id, ADDRLEN);
1043			memcpy_toio(ptx->mac.addr_2,
1044				    ((struct ethhdr *)data)->h_source, ADDRLEN);
1045			memcpy_toio(ptx->mac.addr_3,
1046				    ((struct ethhdr *)data)->h_dest, ADDRLEN);
1047		}
1048	}
1049} /* end encapsulate_frame */
1050
1051/*===========================================================================*/
1052
1053static void netdev_get_drvinfo(struct net_device *dev,
1054			       struct ethtool_drvinfo *info)
1055{
1056	strcpy(info->driver, "ray_cs");
1057}
1058
1059static const struct ethtool_ops netdev_ethtool_ops = {
1060	.get_drvinfo = netdev_get_drvinfo,
1061};
1062
1063/*====================================================================*/
1064
1065/*------------------------------------------------------------------*/
1066/*
1067 * Wireless Handler : get protocol name
1068 */
1069static int ray_get_name(struct net_device *dev, struct iw_request_info *info,
1070			union iwreq_data *wrqu, char *extra)
1071{
1072	strcpy(wrqu->name, "IEEE 802.11-FH");
1073	return 0;
1074}
1075
1076/*------------------------------------------------------------------*/
1077/*
1078 * Wireless Handler : set frequency
1079 */
1080static int ray_set_freq(struct net_device *dev, struct iw_request_info *info,
1081			union iwreq_data *wrqu, char *extra)
1082{
1083	ray_dev_t *local = netdev_priv(dev);
1084	int err = -EINPROGRESS;	/* Call commit handler */
1085
1086	/* Reject if card is already initialised */
1087	if (local->card_status != CARD_AWAITING_PARAM)
1088		return -EBUSY;
1089
1090	/* Setting by channel number */
1091	if ((wrqu->freq.m > USA_HOP_MOD) || (wrqu->freq.e > 0))
1092		err = -EOPNOTSUPP;
1093	else
1094		local->sparm.b5.a_hop_pattern = wrqu->freq.m;
1095
1096	return err;
1097}
1098
1099/*------------------------------------------------------------------*/
1100/*
1101 * Wireless Handler : get frequency
1102 */
1103static int ray_get_freq(struct net_device *dev, struct iw_request_info *info,
1104			union iwreq_data *wrqu, char *extra)
1105{
1106	ray_dev_t *local = netdev_priv(dev);
1107
1108	wrqu->freq.m = local->sparm.b5.a_hop_pattern;
1109	wrqu->freq.e = 0;
1110	return 0;
1111}
1112
1113/*------------------------------------------------------------------*/
1114/*
1115 * Wireless Handler : set ESSID
1116 */
1117static int ray_set_essid(struct net_device *dev, struct iw_request_info *info,
1118			 union iwreq_data *wrqu, char *extra)
1119{
1120	ray_dev_t *local = netdev_priv(dev);
1121
1122	/* Reject if card is already initialised */
1123	if (local->card_status != CARD_AWAITING_PARAM)
1124		return -EBUSY;
1125
1126	/* Check if we asked for `any' */
1127	if (wrqu->essid.flags == 0)
1128		/* Corey : can you do that ? */
1129		return -EOPNOTSUPP;
1130
1131	/* Check the size of the string */
1132	if (wrqu->essid.length > IW_ESSID_MAX_SIZE)
1133		return -E2BIG;
1134
1135	/* Set the ESSID in the card */
1136	memset(local->sparm.b5.a_current_ess_id, 0, IW_ESSID_MAX_SIZE);
1137	memcpy(local->sparm.b5.a_current_ess_id, extra, wrqu->essid.length);
1138
1139	return -EINPROGRESS;	/* Call commit handler */
1140}
1141
1142/*------------------------------------------------------------------*/
1143/*
1144 * Wireless Handler : get ESSID
1145 */
1146static int ray_get_essid(struct net_device *dev, struct iw_request_info *info,
1147			 union iwreq_data *wrqu, char *extra)
1148{
1149	ray_dev_t *local = netdev_priv(dev);
1150
1151	/* Get the essid that was set */
1152	memcpy(extra, local->sparm.b5.a_current_ess_id, IW_ESSID_MAX_SIZE);
1153
1154	/* Push it out ! */
1155	wrqu->essid.length = strlen(extra);
1156	wrqu->essid.flags = 1;	/* active */
1157
1158	return 0;
1159}
1160
1161/*------------------------------------------------------------------*/
1162/*
1163 * Wireless Handler : get AP address
1164 */
1165static int ray_get_wap(struct net_device *dev, struct iw_request_info *info,
1166		       union iwreq_data *wrqu, char *extra)
1167{
1168	ray_dev_t *local = netdev_priv(dev);
1169
1170	memcpy(wrqu->ap_addr.sa_data, local->bss_id, ETH_ALEN);
1171	wrqu->ap_addr.sa_family = ARPHRD_ETHER;
1172
1173	return 0;
1174}
1175
1176/*------------------------------------------------------------------*/
1177/*
1178 * Wireless Handler : set Bit-Rate
1179 */
1180static int ray_set_rate(struct net_device *dev, struct iw_request_info *info,
1181			union iwreq_data *wrqu, char *extra)
1182{
1183	ray_dev_t *local = netdev_priv(dev);
1184
1185	/* Reject if card is already initialised */
1186	if (local->card_status != CARD_AWAITING_PARAM)
1187		return -EBUSY;
1188
1189	/* Check if rate is in range */
1190	if ((wrqu->bitrate.value != 1000000) && (wrqu->bitrate.value != 2000000))
1191		return -EINVAL;
1192
1193	/* Hack for 1.5 Mb/s instead of 2 Mb/s */
1194	if ((local->fw_ver == 0x55) &&	/* Please check */
1195	    (wrqu->bitrate.value == 2000000))
1196		local->net_default_tx_rate = 3;
1197	else
1198		local->net_default_tx_rate = wrqu->bitrate.value / 500000;
1199
1200	return 0;
1201}
1202
1203/*------------------------------------------------------------------*/
1204/*
1205 * Wireless Handler : get Bit-Rate
1206 */
1207static int ray_get_rate(struct net_device *dev, struct iw_request_info *info,
1208			union iwreq_data *wrqu, char *extra)
1209{
1210	ray_dev_t *local = netdev_priv(dev);
1211
1212	if (local->net_default_tx_rate == 3)
1213		wrqu->bitrate.value = 2000000;	/* Hum... */
1214	else
1215		wrqu->bitrate.value = local->net_default_tx_rate * 500000;
1216	wrqu->bitrate.fixed = 0;	/* We are in auto mode */
1217
1218	return 0;
1219}
1220
1221/*------------------------------------------------------------------*/
1222/*
1223 * Wireless Handler : set RTS threshold
1224 */
1225static int ray_set_rts(struct net_device *dev, struct iw_request_info *info,
1226		       union iwreq_data *wrqu, char *extra)
1227{
1228	ray_dev_t *local = netdev_priv(dev);
1229	int rthr = wrqu->rts.value;
1230
1231	/* Reject if card is already initialised */
1232	if (local->card_status != CARD_AWAITING_PARAM)
1233		return -EBUSY;
1234
1235	/* if(wrq->u.rts.fixed == 0) we should complain */
1236	if (wrqu->rts.disabled)
1237		rthr = 32767;
1238	else {
1239		if ((rthr < 0) || (rthr > 2347))   /* What's the max packet size ??? */
1240			return -EINVAL;
1241	}
1242	local->sparm.b5.a_rts_threshold[0] = (rthr >> 8) & 0xFF;
1243	local->sparm.b5.a_rts_threshold[1] = rthr & 0xFF;
1244
1245	return -EINPROGRESS;	/* Call commit handler */
1246}
1247
1248/*------------------------------------------------------------------*/
1249/*
1250 * Wireless Handler : get RTS threshold
1251 */
1252static int ray_get_rts(struct net_device *dev, struct iw_request_info *info,
1253		       union iwreq_data *wrqu, char *extra)
1254{
1255	ray_dev_t *local = netdev_priv(dev);
1256
1257	wrqu->rts.value = (local->sparm.b5.a_rts_threshold[0] << 8)
1258	    + local->sparm.b5.a_rts_threshold[1];
1259	wrqu->rts.disabled = (wrqu->rts.value == 32767);
1260	wrqu->rts.fixed = 1;
1261
1262	return 0;
1263}
1264
1265/*------------------------------------------------------------------*/
1266/*
1267 * Wireless Handler : set Fragmentation threshold
1268 */
1269static int ray_set_frag(struct net_device *dev, struct iw_request_info *info,
1270			union iwreq_data *wrqu, char *extra)
1271{
1272	ray_dev_t *local = netdev_priv(dev);
1273	int fthr = wrqu->frag.value;
1274
1275	/* Reject if card is already initialised */
1276	if (local->card_status != CARD_AWAITING_PARAM)
1277		return -EBUSY;
1278
1279	/* if(wrq->u.frag.fixed == 0) should complain */
1280	if (wrqu->frag.disabled)
1281		fthr = 32767;
1282	else {
1283		if ((fthr < 256) || (fthr > 2347))	/* To check out ! */
1284			return -EINVAL;
1285	}
1286	local->sparm.b5.a_frag_threshold[0] = (fthr >> 8) & 0xFF;
1287	local->sparm.b5.a_frag_threshold[1] = fthr & 0xFF;
1288
1289	return -EINPROGRESS;	/* Call commit handler */
1290}
1291
1292/*------------------------------------------------------------------*/
1293/*
1294 * Wireless Handler : get Fragmentation threshold
1295 */
1296static int ray_get_frag(struct net_device *dev, struct iw_request_info *info,
1297			union iwreq_data *wrqu, char *extra)
1298{
1299	ray_dev_t *local = netdev_priv(dev);
1300
1301	wrqu->frag.value = (local->sparm.b5.a_frag_threshold[0] << 8)
1302	    + local->sparm.b5.a_frag_threshold[1];
1303	wrqu->frag.disabled = (wrqu->frag.value == 32767);
1304	wrqu->frag.fixed = 1;
1305
1306	return 0;
1307}
1308
1309/*------------------------------------------------------------------*/
1310/*
1311 * Wireless Handler : set Mode of Operation
1312 */
1313static int ray_set_mode(struct net_device *dev, struct iw_request_info *info,
1314			union iwreq_data *wrqu, char *extra)
1315{
1316	ray_dev_t *local = netdev_priv(dev);
1317	int err = -EINPROGRESS;	/* Call commit handler */
1318	char card_mode = 1;
1319
1320	/* Reject if card is already initialised */
1321	if (local->card_status != CARD_AWAITING_PARAM)
1322		return -EBUSY;
1323
1324	switch (wrqu->mode) {
1325	case IW_MODE_ADHOC:
1326		card_mode = 0;
1327		/* Fall through */
1328	case IW_MODE_INFRA:
1329		local->sparm.b5.a_network_type = card_mode;
1330		break;
1331	default:
1332		err = -EINVAL;
1333	}
1334
1335	return err;
1336}
1337
1338/*------------------------------------------------------------------*/
1339/*
1340 * Wireless Handler : get Mode of Operation
1341 */
1342static int ray_get_mode(struct net_device *dev, struct iw_request_info *info,
1343			union iwreq_data *wrqu, char *extra)
1344{
1345	ray_dev_t *local = netdev_priv(dev);
1346
1347	if (local->sparm.b5.a_network_type)
1348		wrqu->mode = IW_MODE_INFRA;
1349	else
1350		wrqu->mode = IW_MODE_ADHOC;
1351
1352	return 0;
1353}
1354
1355/*------------------------------------------------------------------*/
1356/*
1357 * Wireless Handler : get range info
1358 */
1359static int ray_get_range(struct net_device *dev, struct iw_request_info *info,
1360			 union iwreq_data *wrqu, char *extra)
1361{
1362	struct iw_range *range = (struct iw_range *)extra;
1363
1364	memset(range, 0, sizeof(struct iw_range));
1365
1366	/* Set the length (very important for backward compatibility) */
1367	wrqu->data.length = sizeof(struct iw_range);
1368
1369	/* Set the Wireless Extension versions */
1370	range->we_version_compiled = WIRELESS_EXT;
1371	range->we_version_source = 9;
1372
1373	/* Set information in the range struct */
1374	range->throughput = 1.1 * 1000 * 1000;	/* Put the right number here */
1375	range->num_channels = hop_pattern_length[(int)country];
1376	range->num_frequency = 0;
1377	range->max_qual.qual = 0;
1378	range->max_qual.level = 255;	/* What's the correct value ? */
1379	range->max_qual.noise = 255;	/* Idem */
1380	range->num_bitrates = 2;
1381	range->bitrate[0] = 1000000;	/* 1 Mb/s */
1382	range->bitrate[1] = 2000000;	/* 2 Mb/s */
1383	return 0;
1384}
1385
1386/*------------------------------------------------------------------*/
1387/*
1388 * Wireless Private Handler : set framing mode
1389 */
1390static int ray_set_framing(struct net_device *dev, struct iw_request_info *info,
1391			   union iwreq_data *wrqu, char *extra)
1392{
1393	translate = *(extra);	/* Set framing mode */
1394
1395	return 0;
1396}
1397
1398/*------------------------------------------------------------------*/
1399/*
1400 * Wireless Private Handler : get framing mode
1401 */
1402static int ray_get_framing(struct net_device *dev, struct iw_request_info *info,
1403			   union iwreq_data *wrqu, char *extra)
1404{
1405	*(extra) = translate;
1406
1407	return 0;
1408}
1409
1410/*------------------------------------------------------------------*/
1411/*
1412 * Wireless Private Handler : get country
1413 */
1414static int ray_get_country(struct net_device *dev, struct iw_request_info *info,
1415			   union iwreq_data *wrqu, char *extra)
1416{
1417	*(extra) = country;
1418
1419	return 0;
1420}
1421
1422/*------------------------------------------------------------------*/
1423/*
1424 * Commit handler : called after a bunch of SET operations
1425 */
1426static int ray_commit(struct net_device *dev, struct iw_request_info *info,
1427		      union iwreq_data *wrqu, char *extra)
1428{
1429	return 0;
1430}
1431
1432/*------------------------------------------------------------------*/
1433/*
1434 * Stats handler : return Wireless Stats
1435 */
1436static iw_stats *ray_get_wireless_stats(struct net_device *dev)
1437{
1438	ray_dev_t *local = netdev_priv(dev);
1439	struct pcmcia_device *link = local->finder;
1440	struct status __iomem *p = local->sram + STATUS_BASE;
1441
1442	local->wstats.status = local->card_status;
1443#ifdef WIRELESS_SPY
1444	if ((local->spy_data.spy_number > 0)
1445	    && (local->sparm.b5.a_network_type == 0)) {
1446		/* Get it from the first node in spy list */
1447		local->wstats.qual.qual = local->spy_data.spy_stat[0].qual;
1448		local->wstats.qual.level = local->spy_data.spy_stat[0].level;
1449		local->wstats.qual.noise = local->spy_data.spy_stat[0].noise;
1450		local->wstats.qual.updated =
1451		    local->spy_data.spy_stat[0].updated;
1452	}
1453#endif /* WIRELESS_SPY */
1454
1455	if (pcmcia_dev_present(link)) {
1456		local->wstats.qual.noise = readb(&p->rxnoise);
1457		local->wstats.qual.updated |= 4;
1458	}
1459
1460	return &local->wstats;
1461} /* end ray_get_wireless_stats */
1462
1463/*------------------------------------------------------------------*/
1464/*
1465 * Structures to export the Wireless Handlers
1466 */
1467
1468static const iw_handler ray_handler[] = {
1469	IW_HANDLER(SIOCSIWCOMMIT, ray_commit),
1470	IW_HANDLER(SIOCGIWNAME, ray_get_name),
1471	IW_HANDLER(SIOCSIWFREQ, ray_set_freq),
1472	IW_HANDLER(SIOCGIWFREQ, ray_get_freq),
1473	IW_HANDLER(SIOCSIWMODE, ray_set_mode),
1474	IW_HANDLER(SIOCGIWMODE, ray_get_mode),
1475	IW_HANDLER(SIOCGIWRANGE, ray_get_range),
1476#ifdef WIRELESS_SPY
1477	IW_HANDLER(SIOCSIWSPY, iw_handler_set_spy),
1478	IW_HANDLER(SIOCGIWSPY, iw_handler_get_spy),
1479	IW_HANDLER(SIOCSIWTHRSPY, iw_handler_set_thrspy),
1480	IW_HANDLER(SIOCGIWTHRSPY, iw_handler_get_thrspy),
1481#endif /* WIRELESS_SPY */
1482	IW_HANDLER(SIOCGIWAP, ray_get_wap),
1483	IW_HANDLER(SIOCSIWESSID, ray_set_essid),
1484	IW_HANDLER(SIOCGIWESSID, ray_get_essid),
1485	IW_HANDLER(SIOCSIWRATE, ray_set_rate),
1486	IW_HANDLER(SIOCGIWRATE, ray_get_rate),
1487	IW_HANDLER(SIOCSIWRTS, ray_set_rts),
1488	IW_HANDLER(SIOCGIWRTS, ray_get_rts),
1489	IW_HANDLER(SIOCSIWFRAG, ray_set_frag),
1490	IW_HANDLER(SIOCGIWFRAG, ray_get_frag),
1491};
1492
1493#define SIOCSIPFRAMING	SIOCIWFIRSTPRIV	/* Set framing mode */
1494#define SIOCGIPFRAMING	SIOCIWFIRSTPRIV + 1	/* Get framing mode */
1495#define SIOCGIPCOUNTRY	SIOCIWFIRSTPRIV + 3	/* Get country code */
1496
1497static const iw_handler ray_private_handler[] = {
1498	[0] = ray_set_framing,
1499	[1] = ray_get_framing,
1500	[3] = ray_get_country,
1501};
1502
1503static const struct iw_priv_args ray_private_args[] = {
1504/* cmd,		set_args,	get_args,	name */
1505	{SIOCSIPFRAMING, IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | 1, 0,
1506	 "set_framing"},
1507	{SIOCGIPFRAMING, 0, IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | 1,
1508	 "get_framing"},
1509	{SIOCGIPCOUNTRY, 0, IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | 1,
1510	 "get_country"},
1511};
1512
1513static const struct iw_handler_def ray_handler_def = {
1514	.num_standard = ARRAY_SIZE(ray_handler),
1515	.num_private = ARRAY_SIZE(ray_private_handler),
1516	.num_private_args = ARRAY_SIZE(ray_private_args),
1517	.standard = ray_handler,
1518	.private = ray_private_handler,
1519	.private_args = ray_private_args,
1520	.get_wireless_stats = ray_get_wireless_stats,
1521};
1522
1523/*===========================================================================*/
1524static int ray_open(struct net_device *dev)
1525{
1526	ray_dev_t *local = netdev_priv(dev);
1527	struct pcmcia_device *link;
1528	link = local->finder;
1529
1530	dev_dbg(&link->dev, "ray_open('%s')\n", dev->name);
1531
1532	if (link->open == 0)
1533		local->num_multi = 0;
1534	link->open++;
1535
1536	/* If the card is not started, time to start it ! - Jean II */
1537	if (local->card_status == CARD_AWAITING_PARAM) {
1538		int i;
1539
1540		dev_dbg(&link->dev, "ray_open: doing init now !\n");
1541
1542		/* Download startup parameters */
1543		if ((i = dl_startup_params(dev)) < 0) {
1544			printk(KERN_INFO
1545			       "ray_dev_init dl_startup_params failed - "
1546			       "returns 0x%x\n", i);
1547			return -1;
1548		}
1549	}
1550
1551	if (sniffer)
1552		netif_stop_queue(dev);
1553	else
1554		netif_start_queue(dev);
1555
1556	dev_dbg(&link->dev, "ray_open ending\n");
1557	return 0;
1558} /* end ray_open */
1559
1560/*===========================================================================*/
1561static int ray_dev_close(struct net_device *dev)
1562{
1563	ray_dev_t *local = netdev_priv(dev);
1564	struct pcmcia_device *link;
1565	link = local->finder;
1566
1567	dev_dbg(&link->dev, "ray_dev_close('%s')\n", dev->name);
1568
1569	link->open--;
1570	netif_stop_queue(dev);
1571
1572	/* In here, we should stop the hardware (stop card from beeing active)
1573	 * and set local->card_status to CARD_AWAITING_PARAM, so that while the
1574	 * card is closed we can chage its configuration.
1575	 * Probably also need a COR reset to get sane state - Jean II */
1576
1577	return 0;
1578} /* end ray_dev_close */
1579
1580/*===========================================================================*/
1581static void ray_reset(struct net_device *dev)
1582{
1583	pr_debug("ray_reset entered\n");
1584}
1585
1586/*===========================================================================*/
1587/* Cause a firmware interrupt if it is ready for one                         */
1588/* Return nonzero if not ready                                               */
1589static int interrupt_ecf(ray_dev_t *local, int ccs)
1590{
1591	int i = 50;
1592	struct pcmcia_device *link = local->finder;
1593
1594	if (!(pcmcia_dev_present(link))) {
1595		dev_dbg(&link->dev, "ray_cs interrupt_ecf - device not present\n");
1596		return -1;
1597	}
1598	dev_dbg(&link->dev, "interrupt_ecf(local=%p, ccs = 0x%x\n", local, ccs);
1599
1600	while (i &&
1601	       (readb(local->amem + CIS_OFFSET + ECF_INTR_OFFSET) &
1602		ECF_INTR_SET))
1603		i--;
1604	if (i == 0) {
1605		dev_dbg(&link->dev, "ray_cs interrupt_ecf card not ready for interrupt\n");
1606		return -1;
1607	}
1608	/* Fill the mailbox, then kick the card */
1609	writeb(ccs, local->sram + SCB_BASE);
1610	writeb(ECF_INTR_SET, local->amem + CIS_OFFSET + ECF_INTR_OFFSET);
1611	return 0;
1612} /* interrupt_ecf */
1613
1614/*===========================================================================*/
1615/* Get next free transmit CCS                                                */
1616/* Return - index of current tx ccs                                          */
1617static int get_free_tx_ccs(ray_dev_t *local)
1618{
1619	int i;
1620	struct ccs __iomem *pccs = ccs_base(local);
1621	struct pcmcia_device *link = local->finder;
1622
1623	if (!(pcmcia_dev_present(link))) {
1624		dev_dbg(&link->dev, "ray_cs get_free_tx_ccs - device not present\n");
1625		return ECARDGONE;
1626	}
1627
1628	if (test_and_set_bit(0, &local->tx_ccs_lock)) {
1629		dev_dbg(&link->dev, "ray_cs tx_ccs_lock busy\n");
1630		return ECCSBUSY;
1631	}
1632
1633	for (i = 0; i < NUMBER_OF_TX_CCS; i++) {
1634		if (readb(&(pccs + i)->buffer_status) == CCS_BUFFER_FREE) {
1635			writeb(CCS_BUFFER_BUSY, &(pccs + i)->buffer_status);
1636			writeb(CCS_END_LIST, &(pccs + i)->link);
1637			local->tx_ccs_lock = 0;
1638			return i;
1639		}
1640	}
1641	local->tx_ccs_lock = 0;
1642	dev_dbg(&link->dev, "ray_cs ERROR no free tx CCS for raylink card\n");
1643	return ECCSFULL;
1644} /* get_free_tx_ccs */
1645
1646/*===========================================================================*/
1647/* Get next free CCS                                                         */
1648/* Return - index of current ccs                                             */
1649static int get_free_ccs(ray_dev_t *local)
1650{
1651	int i;
1652	struct ccs __iomem *pccs = ccs_base(local);
1653	struct pcmcia_device *link = local->finder;
1654
1655	if (!(pcmcia_dev_present(link))) {
1656		dev_dbg(&link->dev, "ray_cs get_free_ccs - device not present\n");
1657		return ECARDGONE;
1658	}
1659	if (test_and_set_bit(0, &local->ccs_lock)) {
1660		dev_dbg(&link->dev, "ray_cs ccs_lock busy\n");
1661		return ECCSBUSY;
1662	}
1663
1664	for (i = NUMBER_OF_TX_CCS; i < NUMBER_OF_CCS; i++) {
1665		if (readb(&(pccs + i)->buffer_status) == CCS_BUFFER_FREE) {
1666			writeb(CCS_BUFFER_BUSY, &(pccs + i)->buffer_status);
1667			writeb(CCS_END_LIST, &(pccs + i)->link);
1668			local->ccs_lock = 0;
1669			return i;
1670		}
1671	}
1672	local->ccs_lock = 0;
1673	dev_dbg(&link->dev, "ray_cs ERROR no free CCS for raylink card\n");
1674	return ECCSFULL;
1675} /* get_free_ccs */
1676
1677/*===========================================================================*/
1678static void authenticate_timeout(u_long data)
1679{
1680	ray_dev_t *local = (ray_dev_t *) data;
1681	del_timer(&local->timer);
1682	printk(KERN_INFO "ray_cs Authentication with access point failed"
1683	       " - timeout\n");
1684	join_net((u_long) local);
1685}
1686
1687/*===========================================================================*/
1688static int parse_addr(char *in_str, UCHAR *out)
1689{
1690	int len;
1691	int i, j, k;
1692	int status;
1693
1694	if (in_str == NULL)
1695		return 0;
1696	if ((len = strlen(in_str)) < 2)
1697		return 0;
1698	memset(out, 0, ADDRLEN);
1699
1700	status = 1;
1701	j = len - 1;
1702	if (j > 12)
1703		j = 12;
1704	i = 5;
1705
1706	while (j > 0) {
1707		if ((k = hex_to_bin(in_str[j--])) != -1)
1708			out[i] = k;
1709		else
1710			return 0;
1711
1712		if (j == 0)
1713			break;
1714		if ((k = hex_to_bin(in_str[j--])) != -1)
1715			out[i] += k << 4;
1716		else
1717			return 0;
1718		if (!i--)
1719			break;
1720	}
1721	return status;
1722}
1723
1724/*===========================================================================*/
1725static struct net_device_stats *ray_get_stats(struct net_device *dev)
1726{
1727	ray_dev_t *local = netdev_priv(dev);
1728	struct pcmcia_device *link = local->finder;
1729	struct status __iomem *p = local->sram + STATUS_BASE;
1730	if (!(pcmcia_dev_present(link))) {
1731		dev_dbg(&link->dev, "ray_cs net_device_stats - device not present\n");
1732		return &local->stats;
1733	}
1734	if (readb(&p->mrx_overflow_for_host)) {
1735		local->stats.rx_over_errors += swab16(readw(&p->mrx_overflow));
1736		writeb(0, &p->mrx_overflow);
1737		writeb(0, &p->mrx_overflow_for_host);
1738	}
1739	if (readb(&p->mrx_checksum_error_for_host)) {
1740		local->stats.rx_crc_errors +=
1741		    swab16(readw(&p->mrx_checksum_error));
1742		writeb(0, &p->mrx_checksum_error);
1743		writeb(0, &p->mrx_checksum_error_for_host);
1744	}
1745	if (readb(&p->rx_hec_error_for_host)) {
1746		local->stats.rx_frame_errors += swab16(readw(&p->rx_hec_error));
1747		writeb(0, &p->rx_hec_error);
1748		writeb(0, &p->rx_hec_error_for_host);
1749	}
1750	return &local->stats;
1751}
1752
1753/*===========================================================================*/
1754static void ray_update_parm(struct net_device *dev, UCHAR objid, UCHAR *value,
1755			    int len)
1756{
1757	ray_dev_t *local = netdev_priv(dev);
1758	struct pcmcia_device *link = local->finder;
1759	int ccsindex;
1760	int i;
1761	struct ccs __iomem *pccs;
1762
1763	if (!(pcmcia_dev_present(link))) {
1764		dev_dbg(&link->dev, "ray_update_parm - device not present\n");
1765		return;
1766	}
1767
1768	if ((ccsindex = get_free_ccs(local)) < 0) {
1769		dev_dbg(&link->dev, "ray_update_parm - No free ccs\n");
1770		return;
1771	}
1772	pccs = ccs_base(local) + ccsindex;
1773	writeb(CCS_UPDATE_PARAMS, &pccs->cmd);
1774	writeb(objid, &pccs->var.update_param.object_id);
1775	writeb(1, &pccs->var.update_param.number_objects);
1776	writeb(0, &pccs->var.update_param.failure_cause);
1777	for (i = 0; i < len; i++) {
1778		writeb(value[i], local->sram + HOST_TO_ECF_BASE);
1779	}
1780	/* Interrupt the firmware to process the command */
1781	if (interrupt_ecf(local, ccsindex)) {
1782		dev_dbg(&link->dev, "ray_cs associate failed - ECF not ready for intr\n");
1783		writeb(CCS_BUFFER_FREE, &(pccs++)->buffer_status);
1784	}
1785}
1786
1787/*===========================================================================*/
1788static void ray_update_multi_list(struct net_device *dev, int all)
1789{
1790	int ccsindex;
1791	struct ccs __iomem *pccs;
1792	ray_dev_t *local = netdev_priv(dev);
1793	struct pcmcia_device *link = local->finder;
1794	void __iomem *p = local->sram + HOST_TO_ECF_BASE;
1795
1796	if (!(pcmcia_dev_present(link))) {
1797		dev_dbg(&link->dev, "ray_update_multi_list - device not present\n");
1798		return;
1799	} else
1800		dev_dbg(&link->dev, "ray_update_multi_list(%p)\n", dev);
1801	if ((ccsindex = get_free_ccs(local)) < 0) {
1802		dev_dbg(&link->dev, "ray_update_multi - No free ccs\n");
1803		return;
1804	}
1805	pccs = ccs_base(local) + ccsindex;
1806	writeb(CCS_UPDATE_MULTICAST_LIST, &pccs->cmd);
1807
1808	if (all) {
1809		writeb(0xff, &pccs->var);
1810		local->num_multi = 0xff;
1811	} else {
1812		struct netdev_hw_addr *ha;
1813		int i = 0;
1814
1815		/* Copy the kernel's list of MC addresses to card */
1816		netdev_for_each_mc_addr(ha, dev) {
1817			memcpy_toio(p, ha->addr, ETH_ALEN);
1818			dev_dbg(&link->dev,
1819			      "ray_update_multi add addr %02x%02x%02x%02x%02x%02x\n",
1820			      ha->addr[0], ha->addr[1],
1821			      ha->addr[2], ha->addr[3],
1822			      ha->addr[4], ha->addr[5]);
1823			p += ETH_ALEN;
1824			i++;
1825		}
1826		if (i > 256 / ADDRLEN)
1827			i = 256 / ADDRLEN;
1828		writeb((UCHAR) i, &pccs->var);
1829		dev_dbg(&link->dev, "ray_cs update_multi %d addresses in list\n", i);
1830		/* Interrupt the firmware to process the command */
1831		local->num_multi = i;
1832	}
1833	if (interrupt_ecf(local, ccsindex)) {
1834		dev_dbg(&link->dev,
1835		      "ray_cs update_multi failed - ECF not ready for intr\n");
1836		writeb(CCS_BUFFER_FREE, &(pccs++)->buffer_status);
1837	}
1838} /* end ray_update_multi_list */
1839
1840/*===========================================================================*/
1841static void set_multicast_list(struct net_device *dev)
1842{
1843	ray_dev_t *local = netdev_priv(dev);
1844	UCHAR promisc;
1845
1846	pr_debug("ray_cs set_multicast_list(%p)\n", dev);
1847
1848	if (dev->flags & IFF_PROMISC) {
1849		if (local->sparm.b5.a_promiscuous_mode == 0) {
1850			pr_debug("ray_cs set_multicast_list promisc on\n");
1851			local->sparm.b5.a_promiscuous_mode = 1;
1852			promisc = 1;
1853			ray_update_parm(dev, OBJID_promiscuous_mode,
1854					&promisc, sizeof(promisc));
1855		}
1856	} else {
1857		if (local->sparm.b5.a_promiscuous_mode == 1) {
1858			pr_debug("ray_cs set_multicast_list promisc off\n");
1859			local->sparm.b5.a_promiscuous_mode = 0;
1860			promisc = 0;
1861			ray_update_parm(dev, OBJID_promiscuous_mode,
1862					&promisc, sizeof(promisc));
1863		}
1864	}
1865
1866	if (dev->flags & IFF_ALLMULTI)
1867		ray_update_multi_list(dev, 1);
1868	else {
1869		if (local->num_multi != netdev_mc_count(dev))
1870			ray_update_multi_list(dev, 0);
1871	}
1872} /* end set_multicast_list */
1873
1874/*=============================================================================
1875 * All routines below here are run at interrupt time.
1876=============================================================================*/
1877static irqreturn_t ray_interrupt(int irq, void *dev_id)
1878{
1879	struct net_device *dev = (struct net_device *)dev_id;
1880	struct pcmcia_device *link;
1881	ray_dev_t *local;
1882	struct ccs __iomem *pccs;
1883	struct rcs __iomem *prcs;
1884	UCHAR rcsindex;
1885	UCHAR tmp;
1886	UCHAR cmd;
1887	UCHAR status;
1888
1889	if (dev == NULL)	/* Note that we want interrupts with dev->start == 0 */
1890		return IRQ_NONE;
1891
1892	pr_debug("ray_cs: interrupt for *dev=%p\n", dev);
1893
1894	local = netdev_priv(dev);
1895	link = (struct pcmcia_device *)local->finder;
1896	if (!pcmcia_dev_present(link)) {
1897		pr_debug(
1898			"ray_cs interrupt from device not present or suspended.\n");
1899		return IRQ_NONE;
1900	}
1901	rcsindex = readb(&((struct scb __iomem *)(local->sram))->rcs_index);
1902
1903	if (rcsindex >= (NUMBER_OF_CCS + NUMBER_OF_RCS)) {
1904		dev_dbg(&link->dev, "ray_cs interrupt bad rcsindex = 0x%x\n", rcsindex);
1905		clear_interrupt(local);
1906		return IRQ_HANDLED;
1907	}
1908	if (rcsindex < NUMBER_OF_CCS) { /* If it's a returned CCS */
1909		pccs = ccs_base(local) + rcsindex;
1910		cmd = readb(&pccs->cmd);
1911		status = readb(&pccs->buffer_status);
1912		switch (cmd) {
1913		case CCS_DOWNLOAD_STARTUP_PARAMS:	/* Happens in firmware someday */
1914			del_timer(&local->timer);
1915			if (status == CCS_COMMAND_COMPLETE) {
1916				dev_dbg(&link->dev,
1917				      "ray_cs interrupt download_startup_parameters OK\n");
1918			} else {
1919				dev_dbg(&link->dev,
1920				      "ray_cs interrupt download_startup_parameters fail\n");
1921			}
1922			break;
1923		case CCS_UPDATE_PARAMS:
1924			dev_dbg(&link->dev, "ray_cs interrupt update params done\n");
1925			if (status != CCS_COMMAND_COMPLETE) {
1926				tmp =
1927				    readb(&pccs->var.update_param.
1928					  failure_cause);
1929				dev_dbg(&link->dev,
1930				      "ray_cs interrupt update params failed - reason %d\n",
1931				      tmp);
1932			}
1933			break;
1934		case CCS_REPORT_PARAMS:
1935			dev_dbg(&link->dev, "ray_cs interrupt report params done\n");
1936			break;
1937		case CCS_UPDATE_MULTICAST_LIST:	/* Note that this CCS isn't returned */
1938			dev_dbg(&link->dev,
1939			      "ray_cs interrupt CCS Update Multicast List done\n");
1940			break;
1941		case CCS_UPDATE_POWER_SAVINGS_MODE:
1942			dev_dbg(&link->dev,
1943			      "ray_cs interrupt update power save mode done\n");
1944			break;
1945		case CCS_START_NETWORK:
1946		case CCS_JOIN_NETWORK:
1947			if (status == CCS_COMMAND_COMPLETE) {
1948				if (readb
1949				    (&pccs->var.start_network.net_initiated) ==
1950				    1) {
1951					dev_dbg(&link->dev,
1952					      "ray_cs interrupt network \"%s\" started\n",
1953					      local->sparm.b4.a_current_ess_id);
1954				} else {
1955					dev_dbg(&link->dev,
1956					      "ray_cs interrupt network \"%s\" joined\n",
1957					      local->sparm.b4.a_current_ess_id);
1958				}
1959				memcpy_fromio(&local->bss_id,
1960					      pccs->var.start_network.bssid,
1961					      ADDRLEN);
1962
1963				if (local->fw_ver == 0x55)
1964					local->net_default_tx_rate = 3;
1965				else
1966					local->net_default_tx_rate =
1967					    readb(&pccs->var.start_network.
1968						  net_default_tx_rate);
1969				local->encryption =
1970				    readb(&pccs->var.start_network.encryption);
1971				if (!sniffer && (local->net_type == INFRA)
1972				    && !(local->sparm.b4.a_acting_as_ap_status)) {
1973					authenticate(local);
1974				}
1975				local->card_status = CARD_ACQ_COMPLETE;
1976			} else {
1977				local->card_status = CARD_ACQ_FAILED;
1978
1979				del_timer(&local->timer);
1980				local->timer.expires = jiffies + HZ * 5;
1981				local->timer.data = (long)local;
1982				if (status == CCS_START_NETWORK) {
1983					dev_dbg(&link->dev,
1984					      "ray_cs interrupt network \"%s\" start failed\n",
1985					      local->sparm.b4.a_current_ess_id);
1986					local->timer.function = &start_net;
1987				} else {
1988					dev_dbg(&link->dev,
1989					      "ray_cs interrupt network \"%s\" join failed\n",
1990					      local->sparm.b4.a_current_ess_id);
1991					local->timer.function = &join_net;
1992				}
1993				add_timer(&local->timer);
1994			}
1995			break;
1996		case CCS_START_ASSOCIATION:
1997			if (status == CCS_COMMAND_COMPLETE) {
1998				local->card_status = CARD_ASSOC_COMPLETE;
1999				dev_dbg(&link->dev, "ray_cs association successful\n");
2000			} else {
2001				dev_dbg(&link->dev, "ray_cs association failed,\n");
2002				local->card_status = CARD_ASSOC_FAILED;
2003				join_net((u_long) local);
2004			}
2005			break;
2006		case CCS_TX_REQUEST:
2007			if (status == CCS_COMMAND_COMPLETE) {
2008				dev_dbg(&link->dev,
2009				      "ray_cs interrupt tx request complete\n");
2010			} else {
2011				dev_dbg(&link->dev,
2012				      "ray_cs interrupt tx request failed\n");
2013			}
2014			if (!sniffer)
2015				netif_start_queue(dev);
2016			netif_wake_queue(dev);
2017			break;
2018		case CCS_TEST_MEMORY:
2019			dev_dbg(&link->dev, "ray_cs interrupt mem test done\n");
2020			break;
2021		case CCS_SHUTDOWN:
2022			dev_dbg(&link->dev,
2023			      "ray_cs interrupt Unexpected CCS returned - Shutdown\n");
2024			break;
2025		case CCS_DUMP_MEMORY:
2026			dev_dbg(&link->dev, "ray_cs interrupt dump memory done\n");
2027			break;
2028		case CCS_START_TIMER:
2029			dev_dbg(&link->dev,
2030			      "ray_cs interrupt DING - raylink timer expired\n");
2031			break;
2032		default:
2033			dev_dbg(&link->dev,
2034			      "ray_cs interrupt Unexpected CCS 0x%x returned 0x%x\n",
2035			      rcsindex, cmd);
2036		}
2037		writeb(CCS_BUFFER_FREE, &pccs->buffer_status);
2038	} else { /* It's an RCS */
2039
2040		prcs = rcs_base(local) + rcsindex;
2041
2042		switch (readb(&prcs->interrupt_id)) {
2043		case PROCESS_RX_PACKET:
2044			ray_rx(dev, local, prcs);
2045			break;
2046		case REJOIN_NET_COMPLETE:
2047			dev_dbg(&link->dev, "ray_cs interrupt rejoin net complete\n");
2048			local->card_status = CARD_ACQ_COMPLETE;
2049			/* do we need to clear tx buffers CCS's? */
2050			if (local->sparm.b4.a_network_type == ADHOC) {
2051				if (!sniffer)
2052					netif_start_queue(dev);
2053			} else {
2054				memcpy_fromio(&local->bss_id,
2055					      prcs->var.rejoin_net_complete.
2056					      bssid, ADDRLEN);
2057				dev_dbg(&link->dev,
2058				      "ray_cs new BSSID = %02x%02x%02x%02x%02x%02x\n",
2059				      local->bss_id[0], local->bss_id[1],
2060				      local->bss_id[2], local->bss_id[3],
2061				      local->bss_id[4], local->bss_id[5]);
2062				if (!sniffer)
2063					authenticate(local);
2064			}
2065			break;
2066		case ROAMING_INITIATED:
2067			dev_dbg(&link->dev, "ray_cs interrupt roaming initiated\n");
2068			netif_stop_queue(dev);
2069			local->card_status = CARD_DOING_ACQ;
2070			break;
2071		case JAPAN_CALL_SIGN_RXD:
2072			dev_dbg(&link->dev, "ray_cs interrupt japan call sign rx\n");
2073			break;
2074		default:
2075			dev_dbg(&link->dev,
2076			      "ray_cs Unexpected interrupt for RCS 0x%x cmd = 0x%x\n",
2077			      rcsindex,
2078			      (unsigned int)readb(&prcs->interrupt_id));
2079			break;
2080		}
2081		writeb(CCS_BUFFER_FREE, &prcs->buffer_status);
2082	}
2083	clear_interrupt(local);
2084	return IRQ_HANDLED;
2085} /* ray_interrupt */
2086
2087/*===========================================================================*/
2088static void ray_rx(struct net_device *dev, ray_dev_t *local,
2089		   struct rcs __iomem *prcs)
2090{
2091	int rx_len;
2092	unsigned int pkt_addr;
2093	void __iomem *pmsg;
2094	pr_debug("ray_rx process rx packet\n");
2095
2096	/* Calculate address of packet within Rx buffer */
2097	pkt_addr = ((readb(&prcs->var.rx_packet.rx_data_ptr[0]) << 8)
2098		    + readb(&prcs->var.rx_packet.rx_data_ptr[1])) & RX_BUFF_END;
2099	/* Length of first packet fragment */
2100	rx_len = (readb(&prcs->var.rx_packet.rx_data_length[0]) << 8)
2101	    + readb(&prcs->var.rx_packet.rx_data_length[1]);
2102
2103	local->last_rsl = readb(&prcs->var.rx_packet.rx_sig_lev);
2104	pmsg = local->rmem + pkt_addr;
2105	switch (readb(pmsg)) {
2106	case DATA_TYPE:
2107		pr_debug("ray_rx data type\n");
2108		rx_data(dev, prcs, pkt_addr, rx_len);
2109		break;
2110	case AUTHENTIC_TYPE:
2111		pr_debug("ray_rx authentic type\n");
2112		if (sniffer)
2113			rx_data(dev, prcs, pkt_addr, rx_len);
2114		else
2115			rx_authenticate(local, prcs, pkt_addr, rx_len);
2116		break;
2117	case DEAUTHENTIC_TYPE:
2118		pr_debug("ray_rx deauth type\n");
2119		if (sniffer)
2120			rx_data(dev, prcs, pkt_addr, rx_len);
2121		else
2122			rx_deauthenticate(local, prcs, pkt_addr, rx_len);
2123		break;
2124	case NULL_MSG_TYPE:
2125		pr_debug("ray_cs rx NULL msg\n");
2126		break;
2127	case BEACON_TYPE:
2128		pr_debug("ray_rx beacon type\n");
2129		if (sniffer)
2130			rx_data(dev, prcs, pkt_addr, rx_len);
2131
2132		copy_from_rx_buff(local, (UCHAR *) &local->last_bcn, pkt_addr,
2133				  rx_len < sizeof(struct beacon_rx) ?
2134				  rx_len : sizeof(struct beacon_rx));
2135
2136		local->beacon_rxed = 1;
2137		/* Get the statistics so the card counters never overflow */
2138		ray_get_stats(dev);
2139		break;
2140	default:
2141		pr_debug("ray_cs unknown pkt type %2x\n",
2142		      (unsigned int)readb(pmsg));
2143		break;
2144	}
2145
2146} /* end ray_rx */
2147
2148/*===========================================================================*/
2149static void rx_data(struct net_device *dev, struct rcs __iomem *prcs,
2150		    unsigned int pkt_addr, int rx_len)
2151{
2152	struct sk_buff *skb = NULL;
2153	struct rcs __iomem *prcslink = prcs;
2154	ray_dev_t *local = netdev_priv(dev);
2155	UCHAR *rx_ptr;
2156	int total_len;
2157	int tmp;
2158#ifdef WIRELESS_SPY
2159	int siglev = local->last_rsl;
2160	u_char linksrcaddr[ETH_ALEN];	/* Other end of the wireless link */
2161#endif
2162
2163	if (!sniffer) {
2164		if (translate) {
2165/* TBD length needs fixing for translated header */
2166			if (rx_len < (ETH_HLEN + RX_MAC_HEADER_LENGTH) ||
2167			    rx_len >
2168			    (dev->mtu + RX_MAC_HEADER_LENGTH + ETH_HLEN +
2169			     FCS_LEN)) {
2170				pr_debug(
2171				      "ray_cs invalid packet length %d received\n",
2172				      rx_len);
2173				return;
2174			}
2175		} else { /* encapsulated ethernet */
2176
2177			if (rx_len < (ETH_HLEN + RX_MAC_HEADER_LENGTH) ||
2178			    rx_len >
2179			    (dev->mtu + RX_MAC_HEADER_LENGTH + ETH_HLEN +
2180			     FCS_LEN)) {
2181				pr_debug(
2182				      "ray_cs invalid packet length %d received\n",
2183				      rx_len);
2184				return;
2185			}
2186		}
2187	}
2188	pr_debug("ray_cs rx_data packet\n");
2189	/* If fragmented packet, verify sizes of fragments add up */
2190	if (readb(&prcs->var.rx_packet.next_frag_rcs_index) != 0xFF) {
2191		pr_debug("ray_cs rx'ed fragment\n");
2192		tmp = (readb(&prcs->var.rx_packet.totalpacketlength[0]) << 8)
2193		    + readb(&prcs->var.rx_packet.totalpacketlength[1]);
2194		total_len = tmp;
2195		prcslink = prcs;
2196		do {
2197			tmp -=
2198			    (readb(&prcslink->var.rx_packet.rx_data_length[0])
2199			     << 8)
2200			    + readb(&prcslink->var.rx_packet.rx_data_length[1]);
2201			if (readb(&prcslink->var.rx_packet.next_frag_rcs_index)
2202			    == 0xFF || tmp < 0)
2203				break;
2204			prcslink = rcs_base(local)
2205			    + readb(&prcslink->link_field);
2206		} while (1);
2207
2208		if (tmp < 0) {
2209			pr_debug(
2210			      "ray_cs rx_data fragment lengths don't add up\n");
2211			local->stats.rx_dropped++;
2212			release_frag_chain(local, prcs);
2213			return;
2214		}
2215	} else { /* Single unfragmented packet */
2216		total_len = rx_len;
2217	}
2218
2219	skb = dev_alloc_skb(total_len + 5);
2220	if (skb == NULL) {
2221		pr_debug("ray_cs rx_data could not allocate skb\n");
2222		local->stats.rx_dropped++;
2223		if (readb(&prcs->var.rx_packet.next_frag_rcs_index) != 0xFF)
2224			release_frag_chain(local, prcs);
2225		return;
2226	}
2227	skb_reserve(skb, 2);	/* Align IP on 16 byte (TBD check this) */
2228
2229	pr_debug("ray_cs rx_data total_len = %x, rx_len = %x\n", total_len,
2230	      rx_len);
2231
2232/************************/
2233	/* Reserve enough room for the whole damn packet. */
2234	rx_ptr = skb_put(skb, total_len);
2235	/* Copy the whole packet to sk_buff */
2236	rx_ptr +=
2237	    copy_from_rx_buff(local, rx_ptr, pkt_addr & RX_BUFF_END, rx_len);
2238	/* Get source address */
2239#ifdef WIRELESS_SPY
2240	skb_copy_from_linear_data_offset(skb,
2241					 offsetof(struct mac_header, addr_2),
2242					 linksrcaddr, ETH_ALEN);
2243#endif
2244	/* Now, deal with encapsulation/translation/sniffer */
2245	if (!sniffer) {
2246		if (!translate) {
2247			/* Encapsulated ethernet, so just lop off 802.11 MAC header */
2248/* TBD reserve            skb_reserve( skb, RX_MAC_HEADER_LENGTH); */
2249			skb_pull(skb, RX_MAC_HEADER_LENGTH);
2250		} else {
2251			/* Do translation */
2252			untranslate(local, skb, total_len);
2253		}
2254	} else { /* sniffer mode, so just pass whole packet */
2255	};
2256
2257/************************/
2258	/* Now pick up the rest of the fragments if any */
2259	tmp = 17;
2260	if (readb(&prcs->var.rx_packet.next_frag_rcs_index) != 0xFF) {
2261		prcslink = prcs;
2262		pr_debug("ray_cs rx_data in fragment loop\n");
2263		do {
2264			prcslink = rcs_base(local)
2265			    +
2266			    readb(&prcslink->var.rx_packet.next_frag_rcs_index);
2267			rx_len =
2268			    ((readb(&prcslink->var.rx_packet.rx_data_length[0])
2269			      << 8)
2270			     +
2271			     readb(&prcslink->var.rx_packet.rx_data_length[1]))
2272			    & RX_BUFF_END;
2273			pkt_addr =
2274			    ((readb(&prcslink->var.rx_packet.rx_data_ptr[0]) <<
2275			      8)
2276			     + readb(&prcslink->var.rx_packet.rx_data_ptr[1]))
2277			    & RX_BUFF_END;
2278
2279			rx_ptr +=
2280			    copy_from_rx_buff(local, rx_ptr, pkt_addr, rx_len);
2281
2282		} while (tmp-- &&
2283			 readb(&prcslink->var.rx_packet.next_frag_rcs_index) !=
2284			 0xFF);
2285		release_frag_chain(local, prcs);
2286	}
2287
2288	skb->protocol = eth_type_trans(skb, dev);
2289	netif_rx(skb);
2290	local->stats.rx_packets++;
2291	local->stats.rx_bytes += total_len;
2292
2293	/* Gather signal strength per address */
2294#ifdef WIRELESS_SPY
2295	/* For the Access Point or the node having started the ad-hoc net
2296	 * note : ad-hoc work only in some specific configurations, but we
2297	 * kludge in ray_get_wireless_stats... */
2298	if (!memcmp(linksrcaddr, local->bss_id, ETH_ALEN)) {
2299		/* Update statistics */
2300		/*local->wstats.qual.qual = none ? */
2301		local->wstats.qual.level = siglev;
2302		/*local->wstats.qual.noise = none ? */
2303		local->wstats.qual.updated = 0x2;
2304	}
2305	/* Now, update the spy stuff */
2306	{
2307		struct iw_quality wstats;
2308		wstats.level = siglev;
2309		/* wstats.noise = none ? */
2310		/* wstats.qual = none ? */
2311		wstats.updated = 0x2;
2312		/* Update spy records */
2313		wireless_spy_update(dev, linksrcaddr, &wstats);
2314	}
2315#endif /* WIRELESS_SPY */
2316} /* end rx_data */
2317
2318/*===========================================================================*/
2319static void untranslate(ray_dev_t *local, struct sk_buff *skb, int len)
2320{
2321	snaphdr_t *psnap = (snaphdr_t *) (skb->data + RX_MAC_HEADER_LENGTH);
2322	struct ieee80211_hdr *pmac = (struct ieee80211_hdr *)skb->data;
2323	__be16 type = *(__be16 *) psnap->ethertype;
2324	int delta;
2325	struct ethhdr *peth;
2326	UCHAR srcaddr[ADDRLEN];
2327	UCHAR destaddr[ADDRLEN];
2328	static UCHAR org_bridge[3] = { 0, 0, 0xf8 };
2329	static UCHAR org_1042[3] = { 0, 0, 0 };
2330
2331	memcpy(destaddr, ieee80211_get_DA(pmac), ADDRLEN);
2332	memcpy(srcaddr, ieee80211_get_SA(pmac), ADDRLEN);
2333
2334
2335	if (psnap->dsap != 0xaa || psnap->ssap != 0xaa || psnap->ctrl != 3) {
2336		/* not a snap type so leave it alone */
2337		pr_debug("ray_cs untranslate NOT SNAP %02x %02x %02x\n",
2338		      psnap->dsap, psnap->ssap, psnap->ctrl);
2339
2340		delta = RX_MAC_HEADER_LENGTH - ETH_HLEN;
2341		peth = (struct ethhdr *)(skb->data + delta);
2342		peth->h_proto = htons(len - RX_MAC_HEADER_LENGTH);
2343	} else { /* Its a SNAP */
2344		if (memcmp(psnap->org, org_bridge, 3) == 0) {
2345		/* EtherII and nuke the LLC */
2346			pr_debug("ray_cs untranslate Bridge encap\n");
2347			delta = RX_MAC_HEADER_LENGTH
2348			    + sizeof(struct snaphdr_t) - ETH_HLEN;
2349			peth = (struct ethhdr *)(skb->data + delta);
2350			peth->h_proto = type;
2351		} else if (memcmp(psnap->org, org_1042, 3) == 0) {
2352			switch (ntohs(type)) {
2353			case ETH_P_IPX:
2354			case ETH_P_AARP:
2355				pr_debug("ray_cs untranslate RFC IPX/AARP\n");
2356				delta = RX_MAC_HEADER_LENGTH - ETH_HLEN;
2357				peth = (struct ethhdr *)(skb->data + delta);
2358				peth->h_proto =
2359				    htons(len - RX_MAC_HEADER_LENGTH);
2360				break;
2361			default:
2362				pr_debug("ray_cs untranslate RFC default\n");
2363				delta = RX_MAC_HEADER_LENGTH +
2364				    sizeof(struct snaphdr_t) - ETH_HLEN;
2365				peth = (struct ethhdr *)(skb->data + delta);
2366				peth->h_proto = type;
2367				break;
2368			}
2369		} else {
2370			printk("ray_cs untranslate very confused by packet\n");
2371			delta = RX_MAC_HEADER_LENGTH - ETH_HLEN;
2372			peth = (struct ethhdr *)(skb->data + delta);
2373			peth->h_proto = type;
2374		}
2375	}
2376/* TBD reserve  skb_reserve(skb, delta); */
2377	skb_pull(skb, delta);
2378	pr_debug("untranslate after skb_pull(%d), skb->data = %p\n", delta,
2379	      skb->data);
2380	memcpy(peth->h_dest, destaddr, ADDRLEN);
2381	memcpy(peth->h_source, srcaddr, ADDRLEN);
2382} /* end untranslate */
2383
2384/*===========================================================================*/
2385/* Copy data from circular receive buffer to PC memory.
2386 * dest     = destination address in PC memory
2387 * pkt_addr = source address in receive buffer
2388 * len      = length of packet to copy
2389 */
2390static int copy_from_rx_buff(ray_dev_t *local, UCHAR *dest, int pkt_addr,
2391			     int length)
2392{
2393	int wrap_bytes = (pkt_addr + length) - (RX_BUFF_END + 1);
2394	if (wrap_bytes <= 0) {
2395		memcpy_fromio(dest, local->rmem + pkt_addr, length);
2396	} else { /* Packet wrapped in circular buffer */
2397
2398		memcpy_fromio(dest, local->rmem + pkt_addr,
2399			      length - wrap_bytes);
2400		memcpy_fromio(dest + length - wrap_bytes, local->rmem,
2401			      wrap_bytes);
2402	}
2403	return length;
2404}
2405
2406/*===========================================================================*/
2407static void release_frag_chain(ray_dev_t *local, struct rcs __iomem *prcs)
2408{
2409	struct rcs __iomem *prcslink = prcs;
2410	int tmp = 17;
2411	unsigned rcsindex = readb(&prcs->var.rx_packet.next_frag_rcs_index);
2412
2413	while (tmp--) {
2414		writeb(CCS_BUFFER_FREE, &prcslink->buffer_status);
2415		if (rcsindex >= (NUMBER_OF_CCS + NUMBER_OF_RCS)) {
2416			pr_debug("ray_cs interrupt bad rcsindex = 0x%x\n",
2417			      rcsindex);
2418			break;
2419		}
2420		prcslink = rcs_base(local) + rcsindex;
2421		rcsindex = readb(&prcslink->var.rx_packet.next_frag_rcs_index);
2422	}
2423	writeb(CCS_BUFFER_FREE, &prcslink->buffer_status);
2424}
2425
2426/*===========================================================================*/
2427static void authenticate(ray_dev_t *local)
2428{
2429	struct pcmcia_device *link = local->finder;
2430	dev_dbg(&link->dev, "ray_cs Starting authentication.\n");
2431	if (!(pcmcia_dev_present(link))) {
2432		dev_dbg(&link->dev, "ray_cs authenticate - device not present\n");
2433		return;
2434	}
2435
2436	del_timer(&local->timer);
2437	if (build_auth_frame(local, local->bss_id, OPEN_AUTH_REQUEST)) {
2438		local->timer.function = &join_net;
2439	} else {
2440		local->timer.function = &authenticate_timeout;
2441	}
2442	local->timer.expires = jiffies + HZ * 2;
2443	local->timer.data = (long)local;
2444	add_timer(&local->timer);
2445	local->authentication_state = AWAITING_RESPONSE;
2446} /* end authenticate */
2447
2448/*===========================================================================*/
2449static void rx_authenticate(ray_dev_t *local, struct rcs __iomem *prcs,
2450			    unsigned int pkt_addr, int rx_len)
2451{
2452	UCHAR buff[256];
2453	struct rx_msg *msg = (struct rx_msg *)buff;
2454
2455	del_timer(&local->timer);
2456
2457	copy_from_rx_buff(local, buff, pkt_addr, rx_len & 0xff);
2458	/* if we are trying to get authenticated */
2459	if (local->sparm.b4.a_network_type == ADHOC) {
2460		pr_debug("ray_cs rx_auth var= %02x %02x %02x %02x %02x %02x\n",
2461		      msg->var[0], msg->var[1], msg->var[2], msg->var[3],
2462		      msg->var[4], msg->var[5]);
2463		if (msg->var[2] == 1) {
2464			pr_debug("ray_cs Sending authentication response.\n");
2465			if (!build_auth_frame
2466			    (local, msg->mac.addr_2, OPEN_AUTH_RESPONSE)) {
2467				local->authentication_state = NEED_TO_AUTH;
2468				memcpy(local->auth_id, msg->mac.addr_2,
2469				       ADDRLEN);
2470			}
2471		}
2472	} else { /* Infrastructure network */
2473
2474		if (local->authentication_state == AWAITING_RESPONSE) {
2475			/* Verify authentication sequence #2 and success */
2476			if (msg->var[2] == 2) {
2477				if ((msg->var[3] | msg->var[4]) == 0) {
2478					pr_debug("Authentication successful\n");
2479					local->card_status = CARD_AUTH_COMPLETE;
2480					associate(local);
2481					local->authentication_state =
2482					    AUTHENTICATED;
2483				} else {
2484					pr_debug("Authentication refused\n");
2485					local->card_status = CARD_AUTH_REFUSED;
2486					join_net((u_long) local);
2487					local->authentication_state =
2488					    UNAUTHENTICATED;
2489				}
2490			}
2491		}
2492	}
2493
2494} /* end rx_authenticate */
2495
2496/*===========================================================================*/
2497static void associate(ray_dev_t *local)
2498{
2499	struct ccs __iomem *pccs;
2500	struct pcmcia_device *link = local->finder;
2501	struct net_device *dev = link->priv;
2502	int ccsindex;
2503	if (!(pcmcia_dev_present(link))) {
2504		dev_dbg(&link->dev, "ray_cs associate - device not present\n");
2505		return;
2506	}
2507	/* If no tx buffers available, return */
2508	if ((ccsindex = get_free_ccs(local)) < 0) {
2509/* TBD should never be here but... what if we are? */
2510		dev_dbg(&link->dev, "ray_cs associate - No free ccs\n");
2511		return;
2512	}
2513	dev_dbg(&link->dev, "ray_cs Starting association with access point\n");
2514	pccs = ccs_base(local) + ccsindex;
2515	/* fill in the CCS */
2516	writeb(CCS_START_ASSOCIATION, &pccs->cmd);
2517	/* Interrupt the firmware to process the command */
2518	if (interrupt_ecf(local, ccsindex)) {
2519		dev_dbg(&link->dev, "ray_cs associate failed - ECF not ready for intr\n");
2520		writeb(CCS_BUFFER_FREE, &(pccs++)->buffer_status);
2521
2522		del_timer(&local->timer);
2523		local->timer.expires = jiffies + HZ * 2;
2524		local->timer.data = (long)local;
2525		local->timer.function = &join_net;
2526		add_timer(&local->timer);
2527		local->card_status = CARD_ASSOC_FAILED;
2528		return;
2529	}
2530	if (!sniffer)
2531		netif_start_queue(dev);
2532
2533} /* end associate */
2534
2535/*===========================================================================*/
2536static void rx_deauthenticate(ray_dev_t *local, struct rcs __iomem *prcs,
2537			      unsigned int pkt_addr, int rx_len)
2538{
2539/*  UCHAR buff[256];
2540    struct rx_msg *msg = (struct rx_msg *)buff;
2541*/
2542	pr_debug("Deauthentication frame received\n");
2543	local->authentication_state = UNAUTHENTICATED;
2544	/* Need to reauthenticate or rejoin depending on reason code */
2545/*  copy_from_rx_buff(local, buff, pkt_addr, rx_len & 0xff);
2546 */
2547}
2548
2549/*===========================================================================*/
2550static void clear_interrupt(ray_dev_t *local)
2551{
2552	writeb(0, local->amem + CIS_OFFSET + HCS_INTR_OFFSET);
2553}
2554
2555/*===========================================================================*/
2556#ifdef CONFIG_PROC_FS
2557#define MAXDATA (PAGE_SIZE - 80)
2558
2559static char *card_status[] = {
2560	"Card inserted - uninitialized",	/* 0 */
2561	"Card not downloaded",			/* 1 */
2562	"Waiting for download parameters",	/* 2 */
2563	"Card doing acquisition",		/* 3 */
2564	"Acquisition complete",			/* 4 */
2565	"Authentication complete",		/* 5 */
2566	"Association complete",			/* 6 */
2567	"???", "???", "???", "???",		/* 7 8 9 10 undefined */
2568	"Card init error",			/* 11 */
2569	"Download parameters error",		/* 12 */
2570	"???",					/* 13 */
2571	"Acquisition failed",			/* 14 */
2572	"Authentication refused",		/* 15 */
2573	"Association failed"			/* 16 */
2574};
2575
2576static char *nettype[] = { "Adhoc", "Infra " };
2577static char *framing[] = { "Encapsulation", "Translation" }
2578
2579;
2580/*===========================================================================*/
2581static int ray_cs_proc_show(struct seq_file *m, void *v)
2582{
2583/* Print current values which are not available via other means
2584 * eg ifconfig
2585 */
2586	int i;
2587	struct pcmcia_device *link;
2588	struct net_device *dev;
2589	ray_dev_t *local;
2590	UCHAR *p;
2591	struct freq_hop_element *pfh;
2592	UCHAR c[33];
2593
2594	link = this_device;
2595	if (!link)
2596		return 0;
2597	dev = (struct net_device *)link->priv;
2598	if (!dev)
2599		return 0;
2600	local = netdev_priv(dev);
2601	if (!local)
2602		return 0;
2603
2604	seq_puts(m, "Raylink Wireless LAN driver status\n");
2605	seq_printf(m, "%s\n", rcsid);
2606	/* build 4 does not report version, and field is 0x55 after memtest */
2607	seq_puts(m, "Firmware version     = ");
2608	if (local->fw_ver == 0x55)
2609		seq_puts(m, "4 - Use dump_cis for more details\n");
2610	else
2611		seq_printf(m, "%2d.%02d.%02d\n",
2612			   local->fw_ver, local->fw_bld, local->fw_var);
2613
2614	for (i = 0; i < 32; i++)
2615		c[i] = local->sparm.b5.a_current_ess_id[i];
2616	c[32] = 0;
2617	seq_printf(m, "%s network ESSID = \"%s\"\n",
2618		   nettype[local->sparm.b5.a_network_type], c);
2619
2620	p = local->bss_id;
2621	seq_printf(m, "BSSID                = %pM\n", p);
2622
2623	seq_printf(m, "Country code         = %d\n",
2624		   local->sparm.b5.a_curr_country_code);
2625
2626	i = local->card_status;
2627	if (i < 0)
2628		i = 10;
2629	if (i > 16)
2630		i = 10;
2631	seq_printf(m, "Card status          = %s\n", card_status[i]);
2632
2633	seq_printf(m, "Framing mode         = %s\n", framing[translate]);
2634
2635	seq_printf(m, "Last pkt signal lvl  = %d\n", local->last_rsl);
2636
2637	if (local->beacon_rxed) {
2638		/* Pull some fields out of last beacon received */
2639		seq_printf(m, "Beacon Interval      = %d Kus\n",
2640			   local->last_bcn.beacon_intvl[0]
2641			   + 256 * local->last_bcn.beacon_intvl[1]);
2642
2643		p = local->last_bcn.elements;
2644		if (p[0] == C_ESSID_ELEMENT_ID)
2645			p += p[1] + 2;
2646		else {
2647			seq_printf(m,
2648				   "Parse beacon failed at essid element id = %d\n",
2649				   p[0]);
2650			return 0;
2651		}
2652
2653		if (p[0] == C_SUPPORTED_RATES_ELEMENT_ID) {
2654			seq_puts(m, "Supported rate codes = ");
2655			for (i = 2; i < p[1] + 2; i++)
2656				seq_printf(m, "0x%02x ", p[i]);
2657			seq_putc(m, '\n');
2658			p += p[1] + 2;
2659		} else {
2660			seq_puts(m, "Parse beacon failed at rates element\n");
2661			return 0;
2662		}
2663
2664		if (p[0] == C_FH_PARAM_SET_ELEMENT_ID) {
2665			pfh = (struct freq_hop_element *)p;
2666			seq_printf(m, "Hop dwell            = %d Kus\n",
2667				   pfh->dwell_time[0] +
2668				   256 * pfh->dwell_time[1]);
2669			seq_printf(m, "Hop set              = %d\n",
2670				   pfh->hop_set);
2671			seq_printf(m, "Hop pattern          = %d\n",
2672				   pfh->hop_pattern);
2673			seq_printf(m, "Hop index            = %d\n",
2674				   pfh->hop_index);
2675			p += p[1] + 2;
2676		} else {
2677			seq_puts(m,
2678				 "Parse beacon failed at FH param element\n");
2679			return 0;
2680		}
2681	} else {
2682		seq_puts(m, "No beacons received\n");
2683	}
2684	return 0;
2685}
2686
2687static int ray_cs_proc_open(struct inode *inode, struct file *file)
2688{
2689	return single_open(file, ray_cs_proc_show, NULL);
2690}
2691
2692static const struct file_operations ray_cs_proc_fops = {
2693	.owner = THIS_MODULE,
2694	.open = ray_cs_proc_open,
2695	.read = seq_read,
2696	.llseek = seq_lseek,
2697	.release = single_release,
2698};
2699#endif
2700/*===========================================================================*/
2701static int build_auth_frame(ray_dev_t *local, UCHAR *dest, int auth_type)
2702{
2703	int addr;
2704	struct ccs __iomem *pccs;
2705	struct tx_msg __iomem *ptx;
2706	int ccsindex;
2707
2708	/* If no tx buffers available, return */
2709	if ((ccsindex = get_free_tx_ccs(local)) < 0) {
2710		pr_debug("ray_cs send authenticate - No free tx ccs\n");
2711		return -1;
2712	}
2713
2714	pccs = ccs_base(local) + ccsindex;
2715
2716	/* Address in card space */
2717	addr = TX_BUF_BASE + (ccsindex << 11);
2718	/* fill in the CCS */
2719	writeb(CCS_TX_REQUEST, &pccs->cmd);
2720	writeb(addr >> 8, pccs->var.tx_request.tx_data_ptr);
2721	writeb(0x20, pccs->var.tx_request.tx_data_ptr + 1);
2722	writeb(TX_AUTHENTICATE_LENGTH_MSB, pccs->var.tx_request.tx_data_length);
2723	writeb(TX_AUTHENTICATE_LENGTH_LSB,
2724	       pccs->var.tx_request.tx_data_length + 1);
2725	writeb(0, &pccs->var.tx_request.pow_sav_mode);
2726
2727	ptx = local->sram + addr;
2728	/* fill in the mac header */
2729	writeb(PROTOCOL_VER | AUTHENTIC_TYPE, &ptx->mac.frame_ctl_1);
2730	writeb(0, &ptx->mac.frame_ctl_2);
2731
2732	memcpy_toio(ptx->mac.addr_1, dest, ADDRLEN);
2733	memcpy_toio(ptx->mac.addr_2, local->sparm.b4.a_mac_addr, ADDRLEN);
2734	memcpy_toio(ptx->mac.addr_3, local->bss_id, ADDRLEN);
2735
2736	/* Fill in msg body with protocol 00 00, sequence 01 00 ,status 00 00 */
2737	memset_io(ptx->var, 0, 6);
2738	writeb(auth_type & 0xff, ptx->var + 2);
2739
2740	/* Interrupt the firmware to process the command */
2741	if (interrupt_ecf(local, ccsindex)) {
2742		pr_debug(
2743		      "ray_cs send authentication request failed - ECF not ready for intr\n");
2744		writeb(CCS_BUFFER_FREE, &(pccs++)->buffer_status);
2745		return -1;
2746	}
2747	return 0;
2748} /* End build_auth_frame */
2749
2750/*===========================================================================*/
2751#ifdef CONFIG_PROC_FS
2752static ssize_t ray_cs_essid_proc_write(struct file *file,
2753		const char __user *buffer, size_t count, loff_t *pos)
2754{
2755	static char proc_essid[33];
2756	unsigned int len = count;
2757
2758	if (len > 32)
2759		len = 32;
2760	memset(proc_essid, 0, 33);
2761	if (copy_from_user(proc_essid, buffer, len))
2762		return -EFAULT;
2763	essid = proc_essid;
2764	return count;
2765}
2766
2767static const struct file_operations ray_cs_essid_proc_fops = {
2768	.owner		= THIS_MODULE,
2769	.write		= ray_cs_essid_proc_write,
2770};
2771
2772static ssize_t int_proc_write(struct file *file, const char __user *buffer,
2773			      size_t count, loff_t *pos)
2774{
2775	static char proc_number[10];
2776	char *p;
2777	int nr, len;
2778
2779	if (!count)
2780		return 0;
2781
2782	if (count > 9)
2783		return -EINVAL;
2784	if (copy_from_user(proc_number, buffer, count))
2785		return -EFAULT;
2786	p = proc_number;
2787	nr = 0;
2788	len = count;
2789	do {
2790		unsigned int c = *p - '0';
2791		if (c > 9)
2792			return -EINVAL;
2793		nr = nr * 10 + c;
2794		p++;
2795	} while (--len);
2796	*(int *)PDE(file->f_path.dentry->d_inode)->data = nr;
2797	return count;
2798}
2799
2800static const struct file_operations int_proc_fops = {
2801	.owner		= THIS_MODULE,
2802	.write		= int_proc_write,
2803};
2804#endif
2805
2806static struct pcmcia_device_id ray_ids[] = {
2807	PCMCIA_DEVICE_MANF_CARD(0x01a6, 0x0000),
2808	PCMCIA_DEVICE_NULL,
2809};
2810
2811MODULE_DEVICE_TABLE(pcmcia, ray_ids);
2812
2813static struct pcmcia_driver ray_driver = {
2814	.owner = THIS_MODULE,
2815	.drv = {
2816		.name = "ray_cs",
2817		},
2818	.probe = ray_probe,
2819	.remove = ray_detach,
2820	.id_table = ray_ids,
2821	.suspend = ray_suspend,
2822	.resume = ray_resume,
2823};
2824
2825static int __init init_ray_cs(void)
2826{
2827	int rc;
2828
2829	pr_debug("%s\n", rcsid);
2830	rc = pcmcia_register_driver(&ray_driver);
2831	pr_debug("raylink init_module register_pcmcia_driver returns 0x%x\n",
2832	      rc);
2833
2834#ifdef CONFIG_PROC_FS
2835	proc_mkdir("driver/ray_cs", NULL);
2836
2837	proc_create("driver/ray_cs/ray_cs", 0, NULL, &ray_cs_proc_fops);
2838	proc_create("driver/ray_cs/essid", S_IWUSR, NULL, &ray_cs_essid_proc_fops);
2839	proc_create_data("driver/ray_cs/net_type", S_IWUSR, NULL, &int_proc_fops, &net_type);
2840	proc_create_data("driver/ray_cs/translate", S_IWUSR, NULL, &int_proc_fops, &translate);
2841#endif
2842	if (translate != 0)
2843		translate = 1;
2844	return 0;
2845} /* init_ray_cs */
2846
2847/*===========================================================================*/
2848
2849static void __exit exit_ray_cs(void)
2850{
2851	pr_debug("ray_cs: cleanup_module\n");
2852
2853#ifdef CONFIG_PROC_FS
2854	remove_proc_entry("driver/ray_cs/ray_cs", NULL);
2855	remove_proc_entry("driver/ray_cs/essid", NULL);
2856	remove_proc_entry("driver/ray_cs/net_type", NULL);
2857	remove_proc_entry("driver/ray_cs/translate", NULL);
2858	remove_proc_entry("driver/ray_cs", NULL);
2859#endif
2860
2861	pcmcia_unregister_driver(&ray_driver);
2862} /* exit_ray_cs */
2863
2864module_init(init_ray_cs);
2865module_exit(exit_ray_cs);
2866
2867/*===========================================================================*/
2868