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