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