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