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