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