v2.4.2.7 -> v2.4.2.8
[linux-flexiantxendom0-3.2.10.git] / drivers / net / pcmcia / orinoco_cs.c
1 /* orinoco_cs.c 0.03    - (formerly known as dldwd_cs.c)
2  *
3  * A driver for "Hermes" chipset based PCMCIA wireless adaptors, such
4  * as the Lucent WavelanIEEE/Orinoco cards and their OEM (Cabletron/
5  * EnteraSys RoamAbout 802.11, ELSA Airlancer, Melco Buffalo and others).
6  * It should also be usable on various Prism II based cards such as the
7  * Linksys, D-Link and Farallon Skyline. It should also work on Symbol
8  * cards such as the 3Com AirConnect and Ericsson WLAN.
9  *
10  * Copyright (C) 2000 David Gibson, Linuxcare Australia <hermes@gibson.dropbear.id.au>
11  *      With some help from :
12  * Copyright (C) 2001 Jean Tourrilhes, HP Labs <jt@hpl.hp.com>
13  *
14  * Based on dummy_cs.c 1.27 2000/06/12 21:27:25
15  *
16  * Portions based on wvlan_cs.c 1.0.6, Copyright Andreas Neuhaus <andy@fasta.fh-dortmund.de>
17  *      http://www.fasta.fh-dortmund.de/users/andy/wvlan/
18  *
19  * The contents of this file are subject to the Mozilla Public License
20  * Version 1.1 (the "License"); you may not use this file except in
21  * compliance with the License. You may obtain a copy of the License
22  * at http://www.mozilla.org/MPL/
23  *
24  * Software distributed under the License is distributed on an "AS IS"
25  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
26  * the License for the specific language governing rights and
27  * limitations under the License.
28  *
29  * The initial developer of the original code is David A. Hinds
30  * <dahinds@users.sourceforge.net>.  Portions created by David
31  * A. Hinds are Copyright (C) 1999 David A. Hinds.  All Rights
32  * Reserved.
33  *
34  * Alternatively, the contents of this file may be used under the
35  * terms of the GNU Public License version 2 (the "GPL"), in which
36  * case the provisions of the GPL are applicable instead of the above.
37  * If you wish to allow the use of your version of this file only
38  * under the terms of the GPL and not to allow others to use your
39  * version of this file under the MPL, indicate your decision by
40  * deleting the provisions above and replace them with the notice and
41  * other provisions required by the GPL.  If you do not delete the
42  * provisions above, a recipient may use your version of this file
43  * under either the MPL or the GPL.
44  */
45
46 /* Notes on locking:
47  *
48  * The basic principle of operation is that everything except the
49  * interrupt handler is serialized through a single spinlock in the
50  * dldwd_priv_t structure, using dldwd_lock() and
51  * dldwd_unlock() (which in turn use spin_lock_bh() and spin_unlock_bh()).
52  *
53  * The kernel's IRQ handling stuff ensures that the interrupt handler
54  * does not re-enter itself. The interrupt handler is written such
55  * that everything it does is safe without a lock: chiefly this means
56  * that the Rx path uses one of the Hermes chipset's BAPs while
57  * everything else uses the other.
58  *
59  * For the moment access to the device statistics from the interrupt
60  * handler is unsafe - we just put up with any resulting errors in the
61  * statisics. FIXME: This should probably be changed to store the
62  * stats in atomic types.
63  *
64  * EXCEPT that we don't want the irq handler running when we actually
65  * reset or shut down the card, because strange things might happen
66  * (probably the worst would be one packet of garbage, but you can't
67  * be too careful). For this we use __dldwd_stop_irqs() which will set
68  * a flag to disable the interrupt handler, and wait for any
69  * outstanding instances of the handler to complete. THIS WILL LOSE
70  * INTERRUPTS! so it shouldn't be used except for resets, when we
71  * don't care about that.*/
72
73 /*
74  * Tentative changelog...
75  *
76  * v0.01 -> v0.02 - 21/3/2001 - Jean II
77  *      o Allow to use regular ethX device name instead of dldwdX
78  *      o Warning on IBSS with ESSID=any for firmware 6.06
79  *      o Put proper range.throughput values (optimistic)
80  *      o IWSPY support (IOCTL and stat gather in Rx path)
81  *      o Allow setting frequency in Ad-Hoc mode
82  *      o Disable WEP setting if !has_wep to work on old firmware
83  *      o Fix txpower range
84  *      o Start adding support for Samsung/Compaq firmware
85  *
86  * v0.02 -> v0.03 - 23/3/2001 - Jean II
87  *      o Start adding Symbol support - need to check all that
88  *      o Fix Prism2/Symbol WEP to accept 128 bits keys
89  *      o Add Symbol WEP (add authentication type)
90  *      o Add Prism2/Symbol rate
91  *      o Add PM timeout (holdover duration)
92  *      o Enable "iwconfig eth0 key off" and friends (toggle flags)
93  *      o Enable "iwconfig eth0 power unicast/all" (toggle flags)
94  *      o Try with an intel card. It report firmware 1.01, behave like
95  *        an antiquated firmware, however on windows it says 2.00. Yuck !
96  *      o Workaround firmware bug in allocate buffer (Intel 1.01)
97  *      o Finish external renaming to orinoco...
98  *      o Testing with various Wavelan firmwares
99  *
100  * TODO - Jean II
101  *      o inline functions (lot's of candidate, need to reorder code)
102  *      o Separate Pcmcia specific code to help Airport/Mini PCI driver
103  *      o Test PrismII/Symbol cards & firmware versions
104  */
105
106 #include <linux/module.h>
107 #include <linux/kernel.h>
108 #include <linux/init.h>
109 #include <linux/sched.h>
110 #include <linux/ptrace.h>
111 #include <linux/malloc.h>
112 #include <linux/string.h>
113 #include <linux/timer.h>
114 #include <linux/ioport.h>
115 #include <asm/uaccess.h>
116 #include <asm/io.h>
117 #include <asm/system.h>
118 #include <linux/proc_fs.h>
119 #include <linux/netdevice.h>
120 #include <linux/if_arp.h>
121 #include <linux/etherdevice.h>
122 #include <linux/wireless.h>
123
124 #include <pcmcia/version.h>
125 #include <pcmcia/cs_types.h>
126 #include <pcmcia/cs.h>
127 #include <pcmcia/cistpl.h>
128 #include <pcmcia/cisreg.h>
129 #include <pcmcia/ds.h>
130 #include <pcmcia/bus_ops.h>
131
132 #include "hermes.h"
133
134 #ifdef PCMCIA_DEBUG
135 static int pc_debug = PCMCIA_DEBUG;
136 static char *version = "orinoco_cs.c 0.03 (David Gibson <hermes@gibson.dropbear.id.au>)";
137 MODULE_PARM(pc_debug, "i");
138 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
139 #define DEBUGMORE(n, args...) do { if (pc_debug>(n)) printk(args); } while (0)
140 #else
141 #define DEBUG(n, args...) do { } while (0)
142 #define DEBUGMORE(n, args...) do { } while (0)
143 #endif
144
145 #define TRACE_ENTER(devname) DEBUG(2, "%s: -> " __FUNCTION__ "()\n", devname);
146 #define TRACE_EXIT(devname)  DEBUG(2, "%s: <- " __FUNCTION__ "()\n", devname);
147
148 #define MAX(a, b) ( (a) > (b) ? (a) : (b) )
149 #define MIN(a, b) ( (a) < (b) ? (a) : (b) )
150
151 #define RUP_EVEN(a) ( (a) % 2 ? (a) + 1 : (a) )
152
153
154 #if (! defined (WIRELESS_EXT)) || (WIRELESS_EXT < 10)
155 #error "orinoco_cs requires Wireless extensions v10 or later."
156 #endif /* (! defined (WIRELESS_EXT)) || (WIRELESS_EXT < 10) */
157 #define WIRELESS_SPY            // enable iwspy support
158
159 /*====================================================================*/
160
161 /* Parameters that can be set with 'insmod' */
162
163 /* The old way: bit map of interrupts to choose from */
164 /* This means pick from 15, 14, 12, 11, 10, 9, 7, 5, 4, and 3 */
165 static uint irq_mask = 0xdeb8;
166 /* Newer, simpler way of listing specific interrupts */
167 static int irq_list[4] = { -1 };
168 /* Control device name allocation. 0 -> dldwdX ; 1 -> ethX */
169 static int eth = 1;
170
171 MODULE_PARM(irq_mask, "i");
172 MODULE_PARM(irq_list, "1-4i");
173 MODULE_PARM(eth, "i");
174
175 /*====================================================================*/
176
177 #define DLDWD_MIN_MTU           256
178 #define DLDWD_MAX_MTU           (HERMES_FRAME_LEN_MAX - ENCAPS_OVERHEAD)
179
180 #define LTV_BUF_SIZE            128
181 #define USER_BAP                0
182 #define IRQ_BAP                 1
183 #define DLDWD_MACPORT           0
184 #define IRQ_LOOP_MAX            10
185 #define TX_NICBUF_SIZE          2048
186 #define TX_NICBUF_SIZE_BUG      1585            /* Bug in Intel firmware */
187 #define MAX_KEYS                4
188 #define MAX_KEY_SIZE            14
189 #define LARGE_KEY_SIZE          13
190 #define SMALL_KEY_SIZE          5
191 #define MAX_FRAME_SIZE          2304
192
193 const long channel_frequency[] = {
194         2412, 2417, 2422, 2427, 2432, 2437, 2442,
195         2447, 2452, 2457, 2462, 2467, 2472, 2484
196 };
197
198 #define NUM_CHANNELS ( sizeof(channel_frequency) / sizeof(channel_frequency[0]) )
199
200 /* This tables gives the actual meanings of the bitrate IDs returned by the firmware.
201    It gives the rate in halfMb/s, negative indicates auto mode */
202 const int rate_list[] = { 0, 2, 4, -22, 11, 22, -4, -11, 0, 0, 0, 0};
203
204 #define NUM_RATES (sizeof(rate_list) / sizeof(rate_list[0]))
205 typedef struct dldwd_key {
206         uint16_t len;
207         char data[MAX_KEY_SIZE];
208 } __attribute__ ((packed)) dldwd_key_t;
209
210 typedef dldwd_key_t dldwd_keys_t[MAX_KEYS];
211
212 typedef struct dldwd_priv {
213         dev_link_t link;
214         dev_node_t node;
215         int instance;
216
217         spinlock_t lock;
218         long state;
219 #define DLDWD_STATE_INIRQ 0
220 #define DLDWD_STATE_DOIRQ 1
221
222         /* Net device stuff */
223         struct net_device ndev;
224         struct net_device_stats stats;
225         struct iw_statistics wstats;
226
227
228         /* Hardware control variables */
229         hermes_t hw;
230         uint16_t txfid;
231
232         /* Capabilities of the hardware/firmware */
233         hermes_identity_t firmware_info;
234         int firmware_type;
235 #define FIRMWARE_TYPE_LUCENT 1
236 #define FIRMWARE_TYPE_PRISM2 2
237 #define FIRMWARE_TYPE_SYMBOL 3
238         int has_ibss, has_port3, prefer_port3, has_ibss_any;
239         int has_wep, has_big_wep;
240         int has_mwo;
241         int has_pm;
242         int broken_reset, broken_allocate;
243         uint16_t channel_mask;
244
245         /* Current configuration */
246         uint32_t iw_mode;
247         int port_type, allow_ibss;
248         uint16_t wep_on, wep_auth, tx_key;
249         dldwd_keys_t keys;
250         char nick[IW_ESSID_MAX_SIZE+1];
251         char desired_essid[IW_ESSID_MAX_SIZE+1];
252         uint16_t frag_thresh, mwo_robust;
253         uint16_t channel;
254         uint16_t ap_density, rts_thresh;
255         uint16_t tx_rate_ctrl;
256         uint16_t pm_on, pm_mcast, pm_period, pm_timeout;
257
258         int promiscuous, allmulti, mc_count;
259
260 #ifdef WIRELESS_SPY
261         int                     spy_number;
262         u_char                  spy_address[IW_MAX_SPY][ETH_ALEN];
263         struct iw_quality       spy_stat[IW_MAX_SPY];
264 #endif
265
266         /* /proc based debugging stuff */
267         struct proc_dir_entry *dir_dev;
268         struct proc_dir_entry *dir_regs;
269         struct proc_dir_entry *dir_recs;
270 } dldwd_priv_t;
271
272 struct p80211_hdr {
273         uint16_t frame_ctl;
274         uint16_t duration_id;
275         uint8_t addr1[ETH_ALEN];
276         uint8_t addr2[ETH_ALEN];
277         uint8_t addr3[ETH_ALEN];
278         uint16_t seq_ctl;
279         uint8_t addr4[ETH_ALEN];
280         uint16_t data_len;
281 } __attribute__ ((packed));
282
283 /* Frame control field constants */
284 #define DLDWD_FCTL_VERS                 0x0002
285 #define DLDWD_FCTL_FTYPE                0x000c
286 #define DLDWD_FCTL_STYPE                0x00f0
287 #define DLDWD_FCTL_TODS                 0x0100
288 #define DLDWD_FCTL_FROMDS               0x0200
289 #define DLDWD_FCTL_MOREFRAGS            0x0400
290 #define DLDWD_FCTL_RETRY                0x0800
291 #define DLDWD_FCTL_PM                   0x1000
292 #define DLDWD_FCTL_MOREDATA             0x2000
293 #define DLDWD_FCTL_WEP                  0x4000
294 #define DLDWD_FCTL_ORDER                0x8000
295
296 #define DLDWD_FTYPE_MGMT                0x0000
297 #define DLDWD_FTYPE_CTL                 0x0004
298 #define DLDWD_FTYPE_DATA                0x0008
299
300 struct p8022_hdr {
301         uint8_t dsap;
302         uint8_t ssap;
303         uint8_t ctrl;
304         uint8_t oui[3];
305 } __attribute__ ((packed));
306
307 struct dldwd_frame_hdr {
308         hermes_frame_desc_t desc;
309         struct p80211_hdr p80211;
310         struct ethhdr p8023;
311         struct p8022_hdr p8022;
312         uint16_t ethertype;
313 } __attribute__ ((packed));
314
315 #define P8023_OFFSET            (sizeof(hermes_frame_desc_t) + \
316                                 sizeof(struct p80211_hdr))
317 #define ENCAPS_OVERHEAD         (sizeof(struct p8022_hdr) + 2)
318
319 /*
320  * Function prototypes
321  */
322
323 /* PCMCIA gumpf */
324
325 static void dldwd_config(dev_link_t * link);
326 static void dldwd_release(u_long arg);
327 static int dldwd_event(event_t event, int priority,
328                        event_callback_args_t * args);
329
330 static dev_link_t *dldwd_attach(void);
331 static void dldwd_detach(dev_link_t *);
332
333 /* Hardware control routines */
334
335 static int __dldwd_hw_reset(dldwd_priv_t *priv);
336 static void dldwd_shutdown(dldwd_priv_t *dev);
337 static int dldwd_reset(dldwd_priv_t *dev);
338 static int __dldwd_hw_setup_wep(dldwd_priv_t *priv);
339 static int dldwd_hw_get_bssid(dldwd_priv_t *priv, char buf[ETH_ALEN]);
340 static int dldwd_hw_get_essid(dldwd_priv_t *priv, int *active, char buf[IW_ESSID_MAX_SIZE+1]);
341 static long dldwd_hw_get_freq(dldwd_priv_t *priv);
342 static int dldwd_hw_get_bitratelist(dldwd_priv_t *priv, int *numrates,
343                                     int32_t *rates, int max);
344
345 /* Interrupt handling routines */
346 void dldwd_interrupt(int irq, void * dev_id, struct pt_regs *regs);
347 static void __dldwd_ev_tick(dldwd_priv_t *priv, hermes_t *hw);
348 static void __dldwd_ev_wterr(dldwd_priv_t *priv, hermes_t *hw);
349 static void __dldwd_ev_infdrop(dldwd_priv_t *priv, hermes_t *hw);
350 static void __dldwd_ev_info(dldwd_priv_t *priv, hermes_t *hw);
351 static void __dldwd_ev_rx(dldwd_priv_t *priv, hermes_t *hw);
352 static void __dldwd_ev_txexc(dldwd_priv_t *priv, hermes_t *hw);
353 static void __dldwd_ev_tx(dldwd_priv_t *priv, hermes_t *hw);
354 static void __dldwd_ev_alloc(dldwd_priv_t *priv, hermes_t *hw);
355
356 /* struct net_device methods */
357 static int dldwd_init(struct net_device *dev);
358 static int dldwd_open(struct net_device *dev);
359 static int dldwd_stop(struct net_device *dev);
360
361 static int dldwd_xmit(struct sk_buff *skb, struct net_device *dev);
362 static void dldwd_tx_timeout(struct net_device *dev);
363
364 static struct net_device_stats *dldwd_get_stats(struct net_device *dev);
365 static struct iw_statistics *dldwd_get_wireless_stats(struct net_device *dev);
366 static void dldwd_stat_gather(struct net_device *dev,
367                               struct sk_buff *skb,
368                               struct dldwd_frame_hdr *hdr);
369
370 static int dldwd_ioctl_getiwrange(struct net_device *dev, struct iw_point *rrq);
371 static int dldwd_ioctl_setiwencode(struct net_device *dev, struct iw_point *erq);
372 static int dldwd_ioctl_getiwencode(struct net_device *dev, struct iw_point *erq);
373 static int dldwd_ioctl_setessid(struct net_device *dev, struct iw_point *erq);
374 static int dldwd_ioctl_getessid(struct net_device *dev, struct iw_point *erq);
375 static int dldwd_ioctl_setnick(struct net_device *dev, struct iw_point *nrq);
376 static int dldwd_ioctl_getnick(struct net_device *dev, struct iw_point *nrq);
377 static int dldwd_ioctl_setfreq(struct net_device *dev, struct iw_freq *frq);
378 static int dldwd_ioctl_getsens(struct net_device *dev, struct iw_param *srq);
379 static int dldwd_ioctl_setsens(struct net_device *dev, struct iw_param *srq);
380 static int dldwd_ioctl_setrts(struct net_device *dev, struct iw_param *rrq);
381 static int dldwd_ioctl_setfrag(struct net_device *dev, struct iw_param *frq);
382 static int dldwd_ioctl_getfrag(struct net_device *dev, struct iw_param *frq);
383 static int dldwd_ioctl_setrate(struct net_device *dev, struct iw_param *frq);
384 static int dldwd_ioctl_getrate(struct net_device *dev, struct iw_param *frq);
385 static int dldwd_ioctl_setpower(struct net_device *dev, struct iw_param *prq);
386 static int dldwd_ioctl_getpower(struct net_device *dev, struct iw_param *prq);
387 static int dldwd_ioctl_setport3(struct net_device *dev, struct iwreq *wrq);
388 static int dldwd_ioctl_getport3(struct net_device *dev, struct iwreq *wrq);
389 static int dldwd_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
390
391 static int dldwd_change_mtu(struct net_device *dev, int new_mtu);
392 static void __dldwd_set_multicast_list(struct net_device *dev);
393
394 /* /proc debugging stuff */
395 static int dldwd_proc_init(void);
396 static void dldwd_proc_cleanup(void);
397 static int dldwd_proc_dev_init(dldwd_priv_t *dev);
398 static void dldwd_proc_dev_cleanup(dldwd_priv_t *dev);
399
400 /*
401  * Inline functions
402  */
403 static inline void
404 dldwd_lock(dldwd_priv_t *priv)
405 {
406         spin_lock_bh(&priv->lock);
407 }
408
409 static inline void
410 dldwd_unlock(dldwd_priv_t *priv)
411 {
412         spin_unlock_bh(&priv->lock);
413 }
414
415 static inline int
416 dldwd_irqs_allowed(dldwd_priv_t *priv)
417 {
418         return test_bit(DLDWD_STATE_DOIRQ, &priv->state);
419 }
420
421 static inline void
422 __dldwd_stop_irqs(dldwd_priv_t *priv)
423 {
424         hermes_t *hw = &priv->hw;
425
426         hermes_set_irqmask(hw, 0);
427         clear_bit(DLDWD_STATE_DOIRQ, &priv->state);
428         while (test_bit(DLDWD_STATE_INIRQ, &priv->state))
429                 ;
430 }
431
432 static inline void
433 __dldwd_start_irqs(dldwd_priv_t *priv, uint16_t irqmask)
434 {
435         hermes_t *hw = &priv->hw;
436
437         TRACE_ENTER(priv->ndev.name);
438
439         __cli();
440         set_bit(DLDWD_STATE_DOIRQ, &priv->state);
441         hermes_set_irqmask(hw, irqmask);
442         __sti();
443
444         TRACE_EXIT(priv->ndev.name);
445 }
446
447 static inline void
448 set_port_type(dldwd_priv_t *priv)
449 {
450         switch (priv->iw_mode) {
451         case IW_MODE_INFRA:
452                 priv->port_type = 1;
453                 priv->allow_ibss = 0;
454                 break;
455         case IW_MODE_ADHOC:
456                 if (priv->prefer_port3) {
457                         priv->port_type = 3;
458                         priv->allow_ibss = 0;
459                 } else {
460                         priv->port_type = 1;
461                         priv->allow_ibss = 1;
462                 }
463                 break;
464         default:
465                 printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
466                        priv->ndev.name);
467         }
468 }
469
470 static inline void
471 dldwd_set_multicast_list(struct net_device *dev)
472 {
473         dldwd_priv_t *priv = dev->priv;
474
475         dldwd_lock(priv);
476         __dldwd_set_multicast_list(dev);
477         dldwd_unlock(priv);
478 }
479
480 /*
481  * Hardware control routines
482  */
483
484 static int
485 __dldwd_hw_reset(dldwd_priv_t *priv)
486 {
487         hermes_t *hw = &priv->hw;
488         int err;
489
490         if (! priv->broken_reset)
491                 return hermes_reset(hw);
492         else {
493                 hw->inten = 0;
494                 hermes_write_regn(hw, INTEN, 0);
495                 err = hermes_disable_port(hw, 0);
496                 hermes_write_regn(hw, EVACK, 0xffff);
497                 return err;
498         }
499 }
500
501 static void
502 dldwd_shutdown(dldwd_priv_t *priv)
503 {
504 /*      hermes_t *hw = &priv->hw; */
505         int err = 0;
506
507         TRACE_ENTER(priv->ndev.name);
508
509         dldwd_lock(priv);
510         __dldwd_stop_irqs(priv);
511
512         err = __dldwd_hw_reset(priv);
513         if (err && err != -ENODEV) /* If the card is gone, we don't care about shutting it down */
514                 printk(KERN_ERR "%s: Error %d shutting down Hermes chipset\n", priv->ndev.name, err);
515
516         dldwd_unlock(priv);
517
518         TRACE_EXIT(priv->ndev.name);
519 }
520
521 static int
522 dldwd_reset(dldwd_priv_t *priv)
523 {
524         struct net_device *dev = &priv->ndev;
525         hermes_t *hw = &priv->hw;
526         int err = 0;
527         hermes_id_t idbuf;
528         int frame_size;
529
530         TRACE_ENTER(priv->ndev.name);
531
532         dldwd_lock(priv);
533
534         __dldwd_stop_irqs(priv);
535
536         err = __dldwd_hw_reset(priv);
537         if (err)
538                 goto out;
539
540         frame_size = TX_NICBUF_SIZE;
541         /* This stupid bug is present in Intel firmware 1.10, and
542          * may be fixed in later firmwares - Jean II */
543         if(priv->broken_allocate)
544                 frame_size = TX_NICBUF_SIZE_BUG;
545         err = hermes_allocate(hw, frame_size, &priv->txfid);
546         if (err)
547                 goto out;
548
549         /* Now set up all the parameters on the card */
550         
551         /* Set up the link mode */
552         
553         err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNF_PORTTYPE, priv->port_type);
554         if (err)
555                 goto out;
556         if (priv->has_ibss) {
557                 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNF_CREATEIBSS,
558                                            priv->allow_ibss);
559                 if (err)
560                         goto out;
561                 if((strlen(priv->desired_essid) == 0) && (priv->allow_ibss)
562                    && (!priv->has_ibss_any)) {
563                         printk(KERN_WARNING "%s: This firmware requires an \
564 ESSID in IBSS-Ad-Hoc mode.\n", dev->name);
565                         /* With wvlan_cs, in this case, we would crash.
566                          * hopefully, this driver will behave better...
567                          * Jean II */
568                 }
569         }
570
571         /* Set up encryption */
572         if (priv->has_wep) {
573                 err = __dldwd_hw_setup_wep(priv);
574                 if (err)
575                         goto out;
576         }
577
578         /* Set the desired ESSID */
579         idbuf.len = cpu_to_le16(strlen(priv->desired_essid));
580         memcpy(&idbuf.val, priv->desired_essid, sizeof(idbuf.val));
581         err = hermes_write_ltv(hw, USER_BAP, (priv->port_type == 3) ?
582                                HERMES_RID_CNF_OWN_SSID : HERMES_RID_CNF_DESIRED_SSID,
583                                HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
584                                &idbuf);
585         if (err)
586                 goto out;
587
588         /* Set the station name */
589         idbuf.len = cpu_to_le16(strlen(priv->nick));
590         memcpy(&idbuf.val, priv->nick, sizeof(idbuf.val));
591         err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNF_NICKNAME,
592                                HERMES_BYTES_TO_RECLEN(strlen(priv->nick)+2),
593                                &idbuf);
594         if (err)
595                 goto out;
596
597         /* Set the channel/frequency */
598         err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNF_CHANNEL, priv->channel);
599         if (err)
600                 goto out;
601
602         /* Set AP density */
603         err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNF_SYSTEM_SCALE, priv->ap_density);
604         if (err)
605                 goto out;
606
607         /* Set RTS threshold */
608         err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNF_RTS_THRESH, priv->rts_thresh);
609         if (err)
610                 goto out;
611
612         /* Set fragmentation threshold or MWO robustness */
613         if (priv->has_mwo)
614                 err = hermes_write_wordrec(hw, USER_BAP,
615                                            HERMES_RID_CNF_MWO_ROBUST, priv->mwo_robust);
616         else
617                 err = hermes_write_wordrec(hw, USER_BAP,
618                                            HERMES_RID_CNF_FRAG_THRESH, priv->frag_thresh);
619         if (err)
620                 goto out;
621
622         /* Set bitrate */
623         err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNF_TX_RATE_CTRL,
624                                    priv->tx_rate_ctrl);
625         if (err)
626                 goto out;
627
628         /* Set power management */
629         if (priv->has_pm) {
630                 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNF_PM_ENABLE,
631                                            priv->pm_on);
632                 if (err)
633                         goto out;
634                 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNF_PM_MCAST_RX,
635                                            priv->pm_mcast);
636                 if (err)
637                         goto out;
638                 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNF_PM_PERIOD,
639                                            priv->pm_period);
640                 if (err)
641                         goto out;
642                 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNF_PM_HOLDOVER,
643                                            priv->pm_timeout);
644                 if (err)
645                         goto out;
646         }
647
648         /* Set promiscuity / multicast*/
649         priv->promiscuous = 0;
650         priv->allmulti = 0;
651         priv->mc_count = 0;
652         __dldwd_set_multicast_list(dev);
653         
654         err = hermes_enable_port(hw, DLDWD_MACPORT);
655         if (err)
656                 goto out;
657         
658         __dldwd_start_irqs(priv, HERMES_EV_RX | HERMES_EV_ALLOC |
659                            HERMES_EV_TX | HERMES_EV_TXEXC |
660                            HERMES_EV_WTERR | HERMES_EV_INFO |
661                            HERMES_EV_INFDROP);
662
663  out:
664         dldwd_unlock(priv);
665
666         TRACE_EXIT(priv->ndev.name);
667
668         return err;
669 }
670
671 static int __dldwd_hw_setup_wep(dldwd_priv_t *priv)
672 {
673         hermes_t *hw = &priv->hw;
674         int err = 0;
675         
676         switch (priv->firmware_type) {
677         case FIRMWARE_TYPE_LUCENT: /* Lucent style WEP */
678                 if (priv->wep_on) {
679                         err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNF_TX_KEY, priv->tx_key);
680                         if (err)
681                                 return err;
682                         
683                         err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNF_KEYS, &priv->keys);
684                         if (err)
685                                 return err;
686                 }
687                 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNF_WEP_ON, priv->wep_on);
688                 if (err)
689                         return err;
690                 break;
691
692         case FIRMWARE_TYPE_PRISM2: /* Prism II style WEP */
693         case FIRMWARE_TYPE_SYMBOL: /* Symbol style WEP */
694                 if (priv->wep_on) {
695                         char keybuf[LARGE_KEY_SIZE+1];
696                         int keylen;
697                         int i;
698                         
699                         err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNF_PRISM2_TX_KEY,
700                                                    priv->tx_key);
701                         if (err)
702                                 return err;
703                         
704                         keybuf[LARGE_KEY_SIZE] = '\0';
705
706                         /* Write all 4 keys */
707                         for(i = 0; i < MAX_KEYS; i++) {
708                                 keylen = priv->keys[i].len;
709                                 keybuf[SMALL_KEY_SIZE] = '\0';
710                                 memcpy(keybuf, priv->keys[i].data, keylen);
711                                 err = HERMES_WRITE_RECORD_LEN(hw, USER_BAP, HERMES_RID_CNF_PRISM2_KEY0, &keybuf, keylen);
712                                 if (err)
713                                         return err;
714                         }
715                         /* Symbol cards : set the authentication :
716                          * 0 -> no encryption, 1 -> open,
717                          * 2 -> shared key, 3 -> shared key 128bit only */
718                         if (priv->firmware_type == FIRMWARE_TYPE_SYMBOL) {
719                                 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNF_SYMBOL_AUTH_TYPE, priv->wep_auth);
720                                 if (err)
721                                         return err;
722                         }
723                 }
724                 
725                 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNF_PRISM2_WEP_ON, priv->wep_on);
726                 if (err)
727                         return err;     
728                 break;
729
730         default:
731                 if (priv->wep_on) {
732                         printk(KERN_ERR "%s: WEP enabled, although not supported!\n",
733                                priv->ndev.name);
734                         return -EINVAL;
735                 }
736         }
737
738         return 0;
739 }
740
741 static int dldwd_hw_get_bssid(dldwd_priv_t *priv, char buf[ETH_ALEN])
742 {
743         hermes_t *hw = &priv->hw;
744         int err = 0;
745
746         dldwd_lock(priv);
747
748         err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_BSSID,
749                               ETH_ALEN, NULL, buf);
750
751         dldwd_unlock(priv);
752
753         return err;
754 }
755
756 static int dldwd_hw_get_essid(dldwd_priv_t *priv, int *active,
757                               char buf[IW_ESSID_MAX_SIZE+1])
758 {
759         hermes_t *hw = &priv->hw;
760         int err = 0;
761         hermes_id_t essidbuf;
762         char *p = (char *)(&essidbuf.val);
763         int len;
764
765         TRACE_ENTER(priv->ndev.name);
766
767         dldwd_lock(priv);
768
769         if (strlen(priv->desired_essid) > 0) {
770                 /* We read the desired SSID from the hardware rather
771                    than from priv->desired_essid, just in case the
772                    firmware is allowed to change it on us. I'm not
773                    sure about this */
774                 /* My guess is that the OWN_SSID should always be whatever
775                  * we set to the card, whereas CURRENT_SSID is the one that
776                  * may change... - Jean II */
777                 uint16_t rid;
778
779                 *active = 1;
780
781                 rid = (priv->port_type == 3) ? HERMES_RID_CNF_OWN_SSID :
782                         HERMES_RID_CNF_DESIRED_SSID;
783                 
784                 err = hermes_read_ltv(hw, USER_BAP, rid, sizeof(essidbuf),
785                                       NULL, &essidbuf);
786                 if (err)
787                         goto fail_unlock;
788         } else {
789                 *active = 0;
790
791                 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_SSID,
792                                       sizeof(essidbuf), NULL, &essidbuf);
793                 if (err)
794                         goto fail_unlock;
795         }
796
797         len = le16_to_cpu(essidbuf.len);
798
799         memset(buf, 0, sizeof(buf));
800         memcpy(buf, p, len);
801         buf[len] = '\0';
802
803  fail_unlock:
804         dldwd_unlock(priv);
805
806         TRACE_EXIT(priv->ndev.name);
807
808         return err;       
809 }
810
811 static long dldwd_hw_get_freq(dldwd_priv_t *priv)
812 {
813         
814         hermes_t *hw = &priv->hw;
815         int err = 0;
816         uint16_t channel;
817         long freq = 0;
818
819         dldwd_lock(priv);
820         
821         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CURRENT_CHANNEL, &channel);
822         if (err)
823                 goto out;
824
825         if ( (channel < 1) || (channel > NUM_CHANNELS) ) {
826                 struct net_device *dev = &priv->ndev;
827
828                 printk(KERN_WARNING "%s: Channel out of range (%d)!\n", dev->name, channel);
829                 err = -EBUSY;
830                 goto out;
831
832         }
833         freq = channel_frequency[channel-1] * 100000;
834
835  out:
836         dldwd_unlock(priv);
837
838         if (err > 0)
839                 err = -EBUSY;
840         return err ? err : freq;
841 }
842
843 static int dldwd_hw_get_bitratelist(dldwd_priv_t *priv, int *numrates,
844                                     int32_t *rates, int max)
845 {
846         hermes_t *hw = &priv->hw;
847         hermes_id_t list;
848         unsigned char *p = (unsigned char *)&list.val;
849         int err = 0;
850         int num;
851         int i;
852
853         dldwd_lock(priv);
854         err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_DATARATES, sizeof(list),
855                               NULL, &list);
856         dldwd_unlock(priv);
857
858         if (err)
859                 return err;
860         
861         num = le16_to_cpu(list.len);
862         *numrates = num;
863         num = MIN(num, max);
864
865         for (i = 0; i < num; i++) {
866                 rates[i] = (p[i] & 0x7f) * 500000; /* convert to bps */
867         }
868
869         return 0;
870 }
871
872 #ifndef PCMCIA_DEBUG
873 static inline void show_rx_frame(struct dldwd_frame_hdr *frame) {}
874 #else
875 static void show_rx_frame(struct dldwd_frame_hdr *frame)
876 {
877         printk(KERN_DEBUG "RX descriptor:\n");
878         printk(KERN_DEBUG "  status      = 0x%04x\n", frame->desc.status);
879         printk(KERN_DEBUG "  res1        = 0x%04x\n", frame->desc.res1);
880         printk(KERN_DEBUG "  res2        = 0x%04x\n", frame->desc.res2);
881         printk(KERN_DEBUG "  q_info      = 0x%04x\n", frame->desc.q_info);
882         printk(KERN_DEBUG "  res3        = 0x%04x\n", frame->desc.res3);
883         printk(KERN_DEBUG "  res4        = 0x%04x\n", frame->desc.res4);
884         printk(KERN_DEBUG "  tx_ctl      = 0x%04x\n", frame->desc.tx_ctl);
885
886         printk(KERN_DEBUG "IEEE 802.11 header:\n");
887         printk(KERN_DEBUG "  frame_ctl   = 0x%04x\n",
888                frame->p80211.frame_ctl);
889         printk(KERN_DEBUG "  duration_id = 0x%04x\n",
890                frame->p80211.duration_id);
891         printk(KERN_DEBUG "  addr1       = %02x:%02x:%02x:%02x:%02x:%02x\n",
892                frame->p80211.addr1[0], frame->p80211.addr1[1],
893                frame->p80211.addr1[2], frame->p80211.addr1[3],
894                frame->p80211.addr1[4], frame->p80211.addr1[5]);
895         printk(KERN_DEBUG "  addr2       = %02x:%02x:%02x:%02x:%02x:%02x\n",
896                frame->p80211.addr2[0], frame->p80211.addr2[1],
897                frame->p80211.addr2[2], frame->p80211.addr2[3],
898                frame->p80211.addr2[4], frame->p80211.addr2[5]);
899         printk(KERN_DEBUG "  addr3       = %02x:%02x:%02x:%02x:%02x:%02x\n",
900                frame->p80211.addr3[0], frame->p80211.addr3[1],
901                frame->p80211.addr3[2], frame->p80211.addr3[3],
902                frame->p80211.addr3[4], frame->p80211.addr3[5]);
903         printk(KERN_DEBUG "  seq_ctl     = 0x%04x\n",
904                frame->p80211.seq_ctl);
905         printk(KERN_DEBUG "  addr4       = %02x:%02x:%02x:%02x:%02x:%02x\n",
906                frame->p80211.addr4[0], frame->p80211.addr4[1],
907                frame->p80211.addr4[2], frame->p80211.addr4[3],
908                frame->p80211.addr4[4], frame->p80211.addr4[5]);
909         printk(KERN_DEBUG "  data_len    = 0x%04x\n",
910                frame->p80211.data_len);
911
912         printk(KERN_DEBUG "IEEE 802.3 header:\n");
913         printk(KERN_DEBUG "  dest        = %02x:%02x:%02x:%02x:%02x:%02x\n",
914                frame->p8023.h_dest[0], frame->p8023.h_dest[1],
915                frame->p8023.h_dest[2], frame->p8023.h_dest[3],
916                frame->p8023.h_dest[4], frame->p8023.h_dest[5]);
917         printk(KERN_DEBUG "  src         = %02x:%02x:%02x:%02x:%02x:%02x\n",
918                frame->p8023.h_source[0], frame->p8023.h_source[1],
919                frame->p8023.h_source[2], frame->p8023.h_source[3],
920                frame->p8023.h_source[4], frame->p8023.h_source[5]);
921         printk(KERN_DEBUG "  len         = 0x%04x\n", frame->p8023.h_proto);
922
923         printk(KERN_DEBUG "IEEE 802.2 LLC/SNAP header:\n");
924         printk(KERN_DEBUG "  DSAP        = 0x%02x\n", frame->p8022.dsap);
925         printk(KERN_DEBUG "  SSAP        = 0x%02x\n", frame->p8022.ssap);
926         printk(KERN_DEBUG "  ctrl        = 0x%02x\n", frame->p8022.ctrl);
927         printk(KERN_DEBUG "  OUI         = %02x:%02x:%02x\n",
928                frame->p8022.oui[0], frame->p8022.oui[1], frame->p8022.oui[2]);
929         printk(KERN_DEBUG "  ethertype  = 0x%04x\n", frame->ethertype);
930 }
931 #endif
932
933 /*
934  * Interrupt handler
935  */
936 void dldwd_interrupt(int irq, void * dev_id, struct pt_regs *regs)
937 {
938         dldwd_priv_t *priv = (dldwd_priv_t *) dev_id;
939         hermes_t *hw = &priv->hw;
940         struct net_device *dev = &priv->ndev;
941         int count = IRQ_LOOP_MAX;
942         uint16_t evstat, events;
943         static int old_time = 0, timecount = 0; /* Eugh, revolting hack for now */
944
945         if (test_and_set_bit(DLDWD_STATE_INIRQ, &priv->state))
946                 BUG();
947
948         if (! dldwd_irqs_allowed(priv)) {
949                 clear_bit(DLDWD_STATE_INIRQ, &priv->state);
950                 return;
951         }
952
953         DEBUG(3, "%s: dldwd_interrupt()  irq %d\n", priv->ndev.name, irq);
954
955         while (1) {
956                 if (jiffies != old_time)
957                         timecount = 0;
958                 if ( (++timecount > 50) || (! count--) ) {
959                         printk(KERN_CRIT "%s: IRQ handler is looping too \
960 much! Shutting down.\n",
961                                dev->name);
962                         /* Perform an emergency shutdown */
963                         clear_bit(DLDWD_STATE_DOIRQ, &priv->state);
964                         hermes_set_irqmask(hw, 0);
965                         break;
966                 }
967
968                 evstat = hermes_read_regn(hw, EVSTAT);
969                 DEBUG(3, "__dldwd_interrupt(): count=%d EVSTAT=0x%04x inten=0x%04x\n",
970                       count, evstat, hw->inten);
971
972                 events = evstat & hw->inten;
973
974                 if (! events) {
975                         if (netif_queue_stopped(dev)) {
976                                 /* There seems to be a firmware bug which
977                                    sometimes causes the card to give an
978                                    interrupt with no event set, when there
979                                    sould be a Tx completed event. */
980                                 DEBUG(3, "%s: Interrupt with no event (ALLOCFID=0x%04x)\n",
981                                       dev->name, (int)hermes_read_regn(hw, ALLOCFID));
982                                 events = HERMES_EV_TX | HERMES_EV_ALLOC;
983                         } else /* Nothing's happening, we're done */
984                                 break;
985                 }
986
987                 /* Check the card hasn't been removed */
988                 if (! hermes_present(hw)) {
989                         DEBUG(0, "dldwd_interrupt(): card removed\n");
990                         break;
991                 }
992
993                 if (events & HERMES_EV_TICK)
994                         __dldwd_ev_tick(priv, hw);
995                 if (events & HERMES_EV_WTERR)
996                         __dldwd_ev_wterr(priv, hw);
997                 if (events & HERMES_EV_INFDROP)
998                         __dldwd_ev_infdrop(priv, hw);
999                 if (events & HERMES_EV_INFO)
1000                         __dldwd_ev_info(priv, hw);
1001                 if (events & HERMES_EV_RX)
1002                         __dldwd_ev_rx(priv, hw);
1003                 if (events & HERMES_EV_TXEXC)
1004                         __dldwd_ev_txexc(priv, hw);
1005                 if (events & HERMES_EV_TX)
1006                         __dldwd_ev_tx(priv, hw);
1007                 if (events & HERMES_EV_ALLOC)
1008                         __dldwd_ev_alloc(priv, hw);
1009                 
1010                 hermes_write_regn(hw, EVACK, events);
1011         }
1012
1013         clear_bit(DLDWD_STATE_INIRQ, &priv->state);
1014 }
1015
1016 static void __dldwd_ev_tick(dldwd_priv_t *priv, hermes_t *hw)
1017 {
1018         printk(KERN_DEBUG "%s: TICK\n", priv->ndev.name);
1019 }
1020
1021 static void __dldwd_ev_wterr(dldwd_priv_t *priv, hermes_t *hw)
1022 {
1023         /* This seems to happen a fair bit under load, but ignoring it
1024            seems to work fine...*/
1025         DEBUG(1, "%s: MAC controller error (WTERR). Ignoring.\n",
1026               priv->ndev.name);
1027 }
1028
1029 static void __dldwd_ev_infdrop(dldwd_priv_t *priv, hermes_t *hw)
1030 {
1031         printk(KERN_WARNING "%s: Information frame lost.\n", priv->ndev.name);
1032 }
1033
1034 static void __dldwd_ev_info(dldwd_priv_t *priv, hermes_t *hw)
1035 {
1036         DEBUG(3, "%s: Information frame received.\n", priv->ndev.name);
1037         /* We don't actually do anything about it - we assume the MAC
1038            controller can deal with it */
1039 }
1040
1041 static void __dldwd_ev_rx(dldwd_priv_t *priv, hermes_t *hw)
1042 {
1043         struct net_device *dev = &priv->ndev;
1044         struct net_device_stats *stats = &priv->stats;
1045         struct iw_statistics *wstats = &priv->wstats;
1046         struct sk_buff *skb = NULL;
1047         uint16_t rxfid, status;
1048         int length, data_len, data_off;
1049         char *p;
1050         struct dldwd_frame_hdr hdr;
1051         struct ethhdr *eh;
1052         int err;
1053
1054         rxfid = hermes_read_regn(hw, RXFID);
1055         DEBUG(3, "__dldwd_ev_rx(): RXFID=0x%04x\n", rxfid);
1056
1057         /* We read in the entire frame header here. This isn't really
1058            necessary, since we ignore most of it, but it's
1059            conceptually simpler. We can tune this later if
1060            necessary. */
1061         err = hermes_bap_pread(hw, IRQ_BAP, &hdr, sizeof(hdr), rxfid, 0);
1062         if (err) {
1063                 printk(KERN_ERR "%s: error %d reading frame header. "
1064                        "Frame dropped.\n", dev->name, err);
1065                 stats->rx_errors++;
1066                 goto drop;
1067         }
1068
1069         status = le16_to_cpu(hdr.desc.status);
1070         
1071         if (status & HERMES_RXSTAT_ERR) {
1072                 if ((status & HERMES_RXSTAT_ERR) == HERMES_RXSTAT_BADCRC) {
1073                         stats->rx_crc_errors++;
1074                         printk(KERN_WARNING "%s: Bad CRC on Rx. Frame dropped.\n",
1075                                dev->name);
1076                         show_rx_frame(&hdr);
1077                 } else if ((status & HERMES_RXSTAT_ERR)
1078                            == HERMES_RXSTAT_UNDECRYPTABLE) {
1079                         wstats->discard.code++;
1080                         printk(KERN_WARNING "%s: Undecryptable frame on Rx. Frame dropped.\n",
1081                                dev->name);
1082                 } else {
1083                         wstats->discard.misc++;
1084                         printk("%s: Unknown Rx error (0x%x). Frame dropped.\n",
1085                                dev->name, status & HERMES_RXSTAT_ERR);
1086                 }
1087                 stats->rx_errors++;
1088                 goto drop;
1089         }
1090
1091         length = le16_to_cpu(hdr.p80211.data_len);
1092         /* Yes, you heard right, that's le16. 802.2 and 802.3 are
1093            big-endian, but 802.11 is little-endian believe it or
1094            not. */
1095         /* Correct. 802.3 is big-endian byte order and little endian bit
1096          * order, whereas 802.11 is little endian for both byte and bit
1097          * order. That's specified in the 802.11 spec. - Jean II */
1098         
1099         /* Sanity check */
1100         if (length > MAX_FRAME_SIZE) {
1101                 printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
1102                        dev->name, length);
1103                 stats->rx_length_errors++;
1104                 stats->rx_errors++;
1105                 goto drop;
1106         }
1107
1108         /* We need space for the packet data itself, plus an ethernet
1109            header, plus 2 bytes so we can align the IP header on a
1110            32bit boundary, plus 1 byte so we can read in odd length
1111            packets from the card, which has an IO granularity of 16
1112            bits */  
1113         skb = dev_alloc_skb(length+ETH_HLEN+2+1);
1114         if (!skb) {
1115                 printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
1116                        dev->name);
1117                 stats->rx_dropped++;
1118                 goto drop;
1119         }
1120
1121         skb_reserve(skb, 2); /* This way the IP header is aligned */
1122
1123         /* Handle decapsulation */
1124         switch (status & HERMES_RXSTAT_MSGTYPE) {
1125                 /* These both indicate a SNAP within 802.2 LLC within
1126                    802.3 within 802.11 frame which we'll need to
1127                    de-encapsulate. IEEE and ISO OSI have a lot to
1128                    answer for.  */
1129         case HERMES_RXSTAT_1042:
1130         case HERMES_RXSTAT_TUNNEL:
1131                 data_len = length - ENCAPS_OVERHEAD;
1132                 data_off = sizeof(hdr);
1133
1134                 eh = (struct ethhdr *)skb_put(skb, ETH_HLEN);
1135
1136                 memcpy(eh, &hdr.p8023, sizeof(hdr.p8023));
1137                 eh->h_proto = hdr.ethertype;
1138
1139                 break;
1140
1141                 /* Otherwise, we just throw the whole thing in, and hope
1142                    the protocol layer can deal with it as 802.3 */
1143         default:
1144                 data_len = length;
1145                 data_off = P8023_OFFSET;
1146                 break;
1147         }
1148
1149         p = skb_put(skb, data_len);
1150         if (hermes_bap_pread(hw, IRQ_BAP, p, RUP_EVEN(data_len),
1151                              rxfid, data_off) != 0) {
1152                 printk(KERN_WARNING "%s: Error reading packet data\n",
1153                        dev->name);
1154                 stats->rx_errors++;
1155                 goto drop;
1156         }
1157
1158         dev->last_rx = jiffies;
1159         skb->dev = dev;
1160         skb->protocol = eth_type_trans(skb, dev);
1161         skb->ip_summed = CHECKSUM_NONE;
1162         
1163         /* Process the wireless stats if needed */
1164         dldwd_stat_gather(dev, skb, &hdr);
1165
1166         /* Pass the packet to the networking stack */
1167         netif_rx(skb);
1168         stats->rx_packets++;
1169         stats->rx_bytes += length;
1170
1171         return;
1172
1173  drop:  
1174         if (skb)
1175                 dev_kfree_skb_irq(skb);
1176         return;
1177 }
1178
1179 static void __dldwd_ev_txexc(dldwd_priv_t *priv, hermes_t *hw)
1180 {
1181         struct net_device *dev = &priv->ndev;
1182         struct net_device_stats *stats = &priv->stats;
1183
1184         printk(KERN_WARNING "%s: Tx error!\n", dev->name);
1185
1186         netif_wake_queue(dev);
1187         stats->tx_errors++;
1188 }
1189
1190 static void __dldwd_ev_tx(dldwd_priv_t *priv, hermes_t *hw)
1191 {
1192         struct net_device *dev = &priv->ndev;
1193         struct net_device_stats *stats = &priv->stats;
1194
1195         DEBUG(3, "%s: Transmit completed\n", dev->name);
1196
1197         stats->tx_packets++;
1198         netif_wake_queue(dev);
1199 }
1200
1201 static void __dldwd_ev_alloc(dldwd_priv_t *priv, hermes_t *hw)
1202 {
1203         uint16_t allocfid;
1204
1205         allocfid = hermes_read_regn(hw, ALLOCFID);
1206         DEBUG(3, "%s: Allocation complete FID=0x%04x\n", priv->ndev.name, allocfid);
1207
1208         /* For some reason we don't seem to get transmit completed events properly */
1209         if (allocfid == priv->txfid)
1210                 __dldwd_ev_tx(priv, hw);
1211
1212 /*      hermes_write_regn(hw, ALLOCFID, 0); */
1213 }
1214
1215 /*
1216  * struct net_device methods
1217  */
1218
1219 static int dldwd_init(struct net_device *dev)
1220 {
1221         dldwd_priv_t *priv = dev->priv;
1222         hermes_t *hw = &priv->hw;
1223         int err = 0;
1224         hermes_id_t nickbuf;
1225         uint16_t reclen;
1226         int len;
1227         char *vendor_str;
1228         uint32_t firmver;
1229
1230         TRACE_ENTER("dldwd");
1231         
1232         dldwd_lock(priv);
1233
1234         err = hermes_reset(hw);
1235         if (err != 0) {
1236                 printk(KERN_ERR "%s: failed to reset hardware\n", dev->name);
1237                 goto out;
1238         }
1239         
1240         /* Get the firmware version */
1241         err = hermes_read_staidentity(hw, USER_BAP, &priv->firmware_info);
1242         if (err) {
1243                 printk(KERN_WARNING "%s: Error %d reading firmware info. Wildly guessing capabilities...\n",
1244                        dev->name, err);
1245                 memset(&priv->firmware_info, 0, sizeof(priv->firmware_info));
1246         }
1247
1248         firmver = ((uint32_t)priv->firmware_info.major << 16) | priv->firmware_info.minor;
1249
1250         /* Determine capabilities from the firmware version */
1251
1252         switch (priv->firmware_info.vendor) {
1253         case 0x1:
1254                 /* Lucent Wavelan IEEE, Lucent Orinoco, Cabletron RoamAbout,
1255                  * ELSA, Melco, HP, IBM, Dell 1150 cards */
1256                 vendor_str = "Lucent";
1257                 /* Lucent MAC : 00:60:1D:* & 00:02:2D:* */
1258
1259                 priv->firmware_type = FIRMWARE_TYPE_LUCENT;
1260                 priv->broken_reset = 0;
1261                 priv->broken_allocate = 0;
1262                 priv->has_port3 = 1;
1263                 priv->has_ibss = (firmver >= 0x60006);
1264                 priv->has_ibss_any = (firmver >= 0x60010);
1265                 priv->has_wep = (firmver >= 0x40020);
1266                 priv->has_big_wep = 1; /* FIXME: this is wrong - how do we tell
1267                                           Gold cards from the others? */
1268                 priv->has_mwo = (firmver >= 0x60000);
1269                 priv->has_pm = (firmver >= 0x40020);
1270                 /* Tested with Lucent firmware :
1271                  *      1.16 ; 4.08 ; 4.52 ; 6.04 ; 6.16 => Jean II
1272                  * Tested CableTron firmware : 4.32 => Anton */
1273                 break;
1274         case 0x2:
1275                 vendor_str = "Generic Prism II";
1276                 /* Note : my Intel card report this value, but I can't do
1277                  * much with it, so I guess it's broken - Jean II */
1278
1279                 priv->firmware_type = FIRMWARE_TYPE_PRISM2;
1280                 priv->broken_reset = 0;
1281                 priv->broken_allocate = (firmver <= 0x10001);
1282                 priv->has_port3 = 1;
1283                 priv->has_ibss = 0; /* FIXME: no idea if this is right */
1284                 priv->has_wep = (firmver >= 0x20000);
1285                 priv->has_big_wep = 1;
1286                 priv->has_mwo = 0;
1287                 priv->has_pm = (firmver >= 0x20000);
1288                 /* Tested with Intel firmware : 1.01 => Jean II */
1289                 /* Note : firmware 1.01 is *seriously* broken */
1290                 break;
1291         case 0x3:
1292                 vendor_str = "Samsung";
1293                 /* To check - Should cover Samsung & Compaq */
1294
1295                 priv->firmware_type = FIRMWARE_TYPE_PRISM2;
1296                 priv->broken_reset = 0;
1297                 priv->broken_allocate = 0;
1298                 priv->has_port3 = 1;
1299                 priv->has_ibss = 0; /* FIXME: available in later firmwares */
1300                 priv->has_wep = (firmver >= 0x20000); /* FIXME */
1301                 priv->has_big_wep = 0; /* FIXME */
1302                 priv->has_mwo = 0;
1303                 priv->has_pm = (firmver >= 0x20000); /* FIXME */
1304                 break;
1305         case 0x6:
1306                 vendor_str = "LinkSys/D-Link";
1307                 /* To check */
1308
1309                 priv->firmware_type = FIRMWARE_TYPE_PRISM2;
1310                 priv->broken_reset = 0;
1311                 priv->broken_allocate = 0;
1312                 priv->has_port3 = 1;
1313                 priv->has_ibss = 0; /* FIXME: available in later firmwares */
1314                 priv->has_wep = (firmver >= 0x20000); /* FIXME */
1315                 priv->has_big_wep = 0;
1316                 priv->has_mwo = 0;
1317                 priv->has_pm = (firmver >= 0x20000); /* FIXME */
1318                 break;
1319 #if 0
1320         case 0x???:             /* Could someone help here ??? */
1321                 vendor_str = "Symbol";
1322                 /* Symbol , 3Com AirConnect, Ericsson WLAN */
1323
1324                 priv->firmware_type = FIRMWARE_TYPE_SYMBOL;
1325                 priv->broken_reset = 0;
1326                 priv->broken_allocate = 0;
1327                 priv->has_port3 = 1;
1328                 priv->has_ibss = 0; /* FIXME: available in later firmwares */
1329                 priv->has_wep = (firmver >= 0x20000); /* FIXME */
1330                 priv->has_big_wep = 1;  /* Probably RID_SYMBOL_KEY_LENGTH */
1331                 priv->has_mwo = 0;
1332                 priv->has_pm = (firmver >= 0x20000);
1333                 break;
1334 #endif
1335         default:
1336                 vendor_str = "UNKNOWN";
1337
1338                 priv->firmware_type = 0;
1339                 priv->broken_reset = 0;
1340                 priv->broken_allocate = 0;
1341                 priv->has_port3 = 0;
1342                 priv->has_ibss = 0;
1343                 priv->has_wep = 0;
1344                 priv->has_big_wep = 0;
1345                 priv->has_mwo = 0;
1346                 priv->has_pm = 0;
1347         }
1348
1349         printk(KERN_INFO "%s: Firmware ID %02X vendor 0x%x (%s) version %d.%02d\n",
1350                dev->name, priv->firmware_info.id, priv->firmware_info.vendor,
1351                vendor_str, priv->firmware_info.major, priv->firmware_info.minor);
1352         
1353         if ((priv->broken_reset) || (priv->broken_allocate))
1354                 printk(KERN_INFO "%s: Buggy firmware, please upgrade ASAP.\n", dev->name);
1355         if (priv->has_port3)
1356                 printk(KERN_INFO "%s: Ad-hoc demo mode supported.\n", dev->name);
1357         if (priv->has_ibss)
1358                 printk(KERN_INFO "%s: IEEE standard IBSS ad-hoc mode supported.\n",
1359                        dev->name);
1360         if (priv->has_wep) {
1361                 printk(KERN_INFO "%s: WEP supported, ", dev->name);
1362                 if (priv->has_big_wep)
1363                         printk("\"128\"-bit key.\n");
1364                 else
1365                         printk("40-bit key.");
1366         }
1367
1368         /* Get the MAC address */
1369         err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CNF_MACADDR,
1370                               ETH_ALEN, NULL, dev->dev_addr);
1371         if (err) {
1372                 printk(KERN_WARNING "%s: failed to read MAC address!\n",
1373                        dev->name);
1374                 goto out;
1375         }
1376
1377         printk(KERN_INFO "%s: MAC address %02X:%02X:%02X:%02X:%02X:%02X\n",
1378                dev->name, dev->dev_addr[0], dev->dev_addr[1],
1379                dev->dev_addr[2], dev->dev_addr[3], dev->dev_addr[4],
1380                dev->dev_addr[5]);
1381
1382         /* Get the station name */
1383         err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CNF_NICKNAME,
1384                               sizeof(nickbuf), &reclen, &nickbuf);
1385         if (err) {
1386                 printk(KERN_ERR "%s: failed to read station name!n",
1387                        dev->name);
1388                 goto out;
1389         }
1390         if ( nickbuf.len )
1391                 len = MIN(IW_ESSID_MAX_SIZE, le16_to_cpu(nickbuf.len));
1392         else
1393                 len = MIN(IW_ESSID_MAX_SIZE, 2 * reclen);
1394         memcpy(priv->nick, &nickbuf.val, len);
1395         priv->nick[len] = '\0';
1396
1397         printk(KERN_INFO "%s: Station name \"%s\"\n", dev->name, priv->nick);
1398
1399         /* Get allowed channels */
1400         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CHANNEL_LIST, &priv->channel_mask);
1401         if (err) {
1402                 printk(KERN_ERR "%s: failed to read channel list!\n",
1403                        dev->name);
1404                 goto out;
1405         }
1406
1407         /* Get initial AP density */
1408         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNF_SYSTEM_SCALE, &priv->ap_density);
1409         if (err) {
1410                 printk(KERN_ERR "%s: failed to read AP density!\n", dev->name);
1411                 goto out;
1412         }
1413
1414         /* Get initial RTS threshold */
1415         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNF_RTS_THRESH, &priv->rts_thresh);
1416         if (err) {
1417                 printk(KERN_ERR "%s: failed to read RTS threshold!\n", dev->name);
1418                 goto out;
1419         }
1420
1421         /* Get initial fragmentation settings */
1422         if (priv->has_mwo)
1423                 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNF_MWO_ROBUST,
1424                                           &priv->mwo_robust);
1425         else
1426                 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNF_FRAG_THRESH,
1427                                           &priv->frag_thresh);
1428         if (err) {
1429                 printk(KERN_ERR "%s: failed to read fragmentation settings!\n", dev->name);
1430                 goto out;
1431         }
1432
1433         /* Set initial bitrate control*/
1434         priv->tx_rate_ctrl = 3;
1435
1436         /* Power management setup */
1437         if (priv->has_pm) {
1438                 priv->pm_on = 0;
1439                 priv->pm_mcast = 1;
1440                 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNF_PM_PERIOD,
1441                                           &priv->pm_period);
1442                 if (err) {
1443                         printk(KERN_ERR "%s: failed to read power management period!\n",
1444                                dev->name);
1445                         goto out;
1446                 }
1447                 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNF_PM_HOLDOVER,
1448                                           &priv->pm_timeout);
1449                 if (err) {
1450                         printk(KERN_ERR "%s: failed to read power management timeout!\n",
1451                                dev->name);
1452                         goto out;
1453                 }
1454         }
1455                 
1456         /* Set up the default configuration */
1457         priv->iw_mode = IW_MODE_INFRA;
1458         /* By default use IEEE/IBSS ad-hoc mode if we have it */
1459         priv->prefer_port3 = priv->has_port3 && (! priv->has_ibss);
1460         set_port_type(priv);
1461
1462         priv->promiscuous = 0;
1463         priv->allmulti = 0;
1464         priv->wep_on = 0;
1465         priv->tx_key = 0;
1466
1467         printk(KERN_INFO "%s: ready\n", dev->name);
1468
1469  out:
1470         dldwd_unlock(priv);
1471
1472         TRACE_EXIT("dldwd");
1473
1474         return err;
1475 }
1476
1477 static int dldwd_open(struct net_device *dev)
1478 {
1479         dldwd_priv_t *priv = (dldwd_priv_t *)dev->priv;
1480         dev_link_t *link = &priv->link;
1481         int err = 0;
1482         
1483         TRACE_ENTER(dev->name);
1484
1485         link->open++;
1486         MOD_INC_USE_COUNT;
1487         netif_device_attach(dev);
1488         
1489         err = dldwd_reset(priv);
1490         if (err)
1491                 dldwd_stop(dev);
1492         else
1493                 netif_start_queue(dev);
1494
1495         TRACE_EXIT(dev->name);
1496
1497         return err;
1498 }
1499
1500 static int dldwd_stop(struct net_device *dev)
1501 {
1502         dldwd_priv_t *priv = (dldwd_priv_t *)dev->priv;
1503         dev_link_t *link = &priv->link;
1504
1505         TRACE_ENTER(dev->name);
1506
1507         netif_stop_queue(dev);
1508
1509         dldwd_shutdown(priv);
1510         
1511         link->open--;
1512
1513         if (link->state & DEV_STALE_CONFIG)
1514                 mod_timer(&link->release, jiffies + HZ/20);
1515         
1516         MOD_DEC_USE_COUNT;
1517
1518         TRACE_EXIT(dev->name);
1519         
1520         return 0;
1521 }
1522
1523 static struct net_device_stats *dldwd_get_stats(struct net_device *dev)
1524 {
1525         dldwd_priv_t *priv = (dldwd_priv_t *)dev->priv;
1526         
1527         return &priv->stats;
1528 }
1529
1530 static struct iw_statistics *dldwd_get_wireless_stats(struct net_device *dev)
1531 {
1532         dldwd_priv_t *priv = (dldwd_priv_t *)dev->priv;
1533         hermes_t *hw = &priv->hw;
1534         struct iw_statistics *wstats = &priv->wstats;
1535         int err = 0;
1536         hermes_commsqual_t cq;
1537
1538         dldwd_lock(priv);
1539
1540         if (priv->port_type == 3) {
1541                 memset(&wstats->qual, 0, sizeof(wstats->qual));
1542 #ifdef WIRELESS_SPY
1543                 /* If a spy address is defined, we report stats of the
1544                  * first spy address - Jean II */
1545                 if (priv->spy_number > 0) {
1546                         wstats->qual.qual = priv->spy_stat[0].qual;
1547                         wstats->qual.level = priv->spy_stat[0].level;
1548                         wstats->qual.noise = priv->spy_stat[0].noise;
1549                         wstats->qual.updated = priv->spy_stat[0].updated;
1550                 }
1551 #endif /* WIRELESS_SPY */
1552         } else {
1553                 err = hermes_read_commsqual(hw, USER_BAP, &cq);
1554                 
1555                 DEBUG(3, "%s: Global stats = %X-%X-%X\n", dev->name,
1556                       cq.qual, cq.signal, cq.noise);
1557
1558                 /* Why are we using MIN/MAX ? We don't really care
1559                  * if the value goes above max, because we export the
1560                  * raw dBm values anyway. The normalisation should be done
1561                  * in user space - Jean II */
1562                 wstats->qual.qual = MAX(MIN(cq.qual, 0x8b-0x2f), 0);
1563                 wstats->qual.level = MAX(MIN(cq.signal, 0x8a), 0x2f) - 0x95;
1564                 wstats->qual.noise = MAX(MIN(cq.noise, 0x8a), 0x2f) - 0x95;
1565                 wstats->qual.updated = 7;
1566         }
1567
1568         dldwd_unlock(priv);
1569
1570         if (err)
1571                 return NULL;
1572                 
1573         return wstats;
1574 }
1575
1576 #ifdef WIRELESS_SPY
1577 static inline void dldwd_spy_gather(struct net_device *dev,
1578                                     u_char *mac,
1579                                     hermes_commsqual_t *cq)
1580 {
1581         dldwd_priv_t *priv = (dldwd_priv_t *)dev->priv;
1582         int i;
1583
1584         /* Gather wireless spy statistics: for each packet, compare the
1585          * source address with out list, and if match, get the stats... */
1586         for (i = 0; i < priv->spy_number; i++)
1587                 if (!memcmp(mac, priv->spy_address[i], ETH_ALEN)) {
1588                         priv->spy_stat[i].qual = MAX(MIN(cq->qual, 0x8b-0x2f), 0);
1589                         priv->spy_stat[i].level = MAX(MIN(cq->signal, 0x8a), 0x2f) - 0x95;
1590                         priv->spy_stat[i].noise = MAX(MIN(cq->noise, 0x8a), 0x2f) - 0x95;
1591                         priv->spy_stat[i].updated = 7;
1592                 }
1593 }
1594 #endif /* WIRELESS_SPY */
1595
1596 static void dldwd_stat_gather(struct net_device *dev,
1597                               struct sk_buff *skb,
1598                               struct dldwd_frame_hdr *hdr)
1599 {
1600         dldwd_priv_t *priv = (dldwd_priv_t *)dev->priv;
1601         hermes_commsqual_t cq;
1602
1603         /* Using spy support with lots of Rx packets, like in an
1604          * infrastructure (AP), will really slow down everything, because
1605          * the MAC address must be compared to each entry of the spy list.
1606          * If the user really asks for it (set some address in the
1607          * spy list), we do it, but he will pay the price.
1608          * Note that to get here, you need both WIRELESS_SPY
1609          * compiled in AND some addresses in the list !!!
1610          */
1611 #ifdef WIRELESS_EXT
1612         /* Note : gcc will optimise the whole section away if
1613          * WIRELESS_SPY is not defined... - Jean II */
1614         if (
1615 #ifdef WIRELESS_SPY
1616                 (priv->spy_number > 0) ||
1617 #endif
1618                 0 )
1619         {
1620                 u_char *stats = (u_char *) &(hdr->desc.q_info);
1621                 /* This code may look strange. Everywhere we are using 16 bit
1622                  * ints except here. I've verified that these are are the
1623                  * correct values. Please check on PPC - Jean II */
1624                 cq.signal = stats[1];   /* High order byte */
1625                 cq.noise = stats[0];    /* Low order byte */
1626                 cq.qual = stats[0] - stats[1];  /* Better than nothing */
1627
1628                 DEBUG(3, "%s: Packet stats = %X-%X-%X\n", dev->name,
1629                       cq.qual, cq.signal, cq.noise);
1630
1631 #ifdef WIRELESS_SPY
1632                 dldwd_spy_gather(dev, skb->mac.raw + ETH_ALEN, &cq);  
1633 #endif
1634         }
1635 #endif /* WIRELESS_EXT */
1636 }
1637
1638 struct p8022_hdr encaps_hdr = {
1639         0xaa, 0xaa, 0x03, {0x00, 0x00, 0xf8}
1640 };
1641
1642 static int dldwd_xmit(struct sk_buff *skb, struct net_device *dev)
1643 {
1644         dldwd_priv_t *priv = (dldwd_priv_t *)dev->priv;
1645         struct net_device_stats *stats = &priv->stats;
1646         hermes_t *hw = &priv->hw;
1647         int err = 0;
1648         uint16_t txfid = priv->txfid;
1649         char *p;
1650         struct ethhdr *eh;
1651         int len, data_len, data_off;
1652         struct dldwd_frame_hdr hdr;
1653         hermes_response_t resp;
1654
1655         if (! netif_running(dev)) {
1656                 printk(KERN_ERR "%s: Tx on stopped device!\n",
1657                        dev->name);
1658                 return 1;
1659
1660         }
1661         
1662         if (netif_queue_stopped(dev)) {
1663                 printk(KERN_ERR "%s: Tx while transmitter busy!\n", 
1664                        dev->name);
1665                 return 1;
1666         }
1667         
1668         dldwd_lock(priv);
1669
1670         /* Length of the packet body */
1671         len = MAX(skb->len - ETH_HLEN, ETH_ZLEN);
1672
1673         eh = (struct ethhdr *)skb->data;
1674
1675         /* Build the IEEE 802.11 header */
1676         memset(&hdr, 0, sizeof(hdr));
1677         memcpy(hdr.p80211.addr1, eh->h_dest, ETH_ALEN);
1678         memcpy(hdr.p80211.addr2, eh->h_source, ETH_ALEN);
1679         hdr.p80211.frame_ctl = DLDWD_FTYPE_DATA;
1680
1681         /* Encapsulate Ethernet-II frames */
1682         if (ntohs(eh->h_proto) > 1500) { /* Ethernet-II frame */
1683                 data_len = len;
1684                 data_off = sizeof(hdr);
1685                 p = skb->data + ETH_HLEN;
1686
1687                 /* 802.11 header */
1688                 hdr.p80211.data_len = cpu_to_le16(data_len + ENCAPS_OVERHEAD);
1689
1690                 /* 802.3 header */
1691                 memcpy(hdr.p8023.h_dest, eh->h_dest, ETH_ALEN);
1692                 memcpy(hdr.p8023.h_source, eh->h_source, ETH_ALEN);
1693                 hdr.p8023.h_proto = htons(data_len + ENCAPS_OVERHEAD);
1694                 
1695                 /* 802.2 header */
1696                 memcpy(&hdr.p8022, &encaps_hdr, sizeof(encaps_hdr));
1697
1698                 hdr.ethertype = eh->h_proto;
1699                 err  = hermes_bap_pwrite(hw, USER_BAP, &hdr, sizeof(hdr),
1700                                          txfid, 0);
1701                 if (err) {
1702                         printk(KERN_ERR
1703                                "%s: Error %d writing packet header to BAP\n",
1704                                dev->name, err);
1705                         stats->tx_errors++;
1706                         goto fail;
1707                 }
1708         } else { /* IEEE 802.3 frame */
1709                 data_len = len + ETH_HLEN;
1710                 data_off = P8023_OFFSET;
1711                 p = skb->data;
1712                 
1713                 /* 802.11 header */
1714                 hdr.p80211.data_len = cpu_to_le16(len);
1715                 err = hermes_bap_pwrite(hw, USER_BAP, &hdr, P8023_OFFSET,
1716                                         txfid, 0);
1717                 if (err) {
1718                         printk(KERN_ERR
1719                                "%s: Error %d writing packet header to BAP\n",
1720                                dev->name, err);
1721                         stats->tx_errors++;
1722                         goto fail;
1723                 }
1724         }
1725
1726         /* Round up for odd length packets */
1727         err = hermes_bap_pwrite(hw, USER_BAP, p, RUP_EVEN(data_len), txfid, data_off);
1728         if (err) {
1729                 printk(KERN_ERR "%s: Error %d writing packet data to BAP\n",
1730                        dev->name, err);
1731                 stats->tx_errors++;
1732                 goto fail;
1733         }
1734         
1735
1736         /* Finally, we actually initiate the send */
1737         err = hermes_docmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL, txfid, &resp);
1738         if (err) {
1739                 printk(KERN_ERR "%s: Error %d transmitting packet\n", dev->name, err);
1740                 stats->tx_errors++;
1741                 goto fail;
1742         }
1743
1744         dev->trans_start = jiffies;
1745         stats->tx_bytes += data_off + data_len;
1746
1747         netif_stop_queue(dev);
1748
1749         dldwd_unlock(priv);
1750
1751         dev_kfree_skb(skb);
1752
1753         return 0;
1754  fail:
1755
1756         dldwd_unlock(priv);
1757         return err;
1758 }
1759
1760 static void dldwd_tx_timeout(struct net_device *dev)
1761 {
1762         dldwd_priv_t *priv = (dldwd_priv_t *)dev->priv;
1763         struct net_device_stats *stats = &priv->stats;
1764         int err = 0;
1765
1766         printk(KERN_WARNING "%s: Tx timeout! Resetting card.\n", dev->name);
1767
1768         stats->tx_errors++;
1769
1770         err = dldwd_reset(priv);
1771         if (err)
1772                 printk(KERN_ERR "%s: Error %d resetting card on Tx timeout!\n",
1773                        dev->name, err);
1774         else {
1775                 dev->trans_start = jiffies;
1776                 netif_wake_queue(dev);
1777         }
1778 }
1779
1780 static int dldwd_ioctl_getiwrange(struct net_device *dev, struct iw_point *rrq)
1781 {
1782         dldwd_priv_t *priv = dev->priv;
1783         int err = 0;
1784         int ptype;
1785         struct iw_range range;
1786         int numrates;
1787         int i, k;
1788
1789         TRACE_ENTER(dev->name);
1790
1791         err = verify_area(VERIFY_WRITE, rrq->pointer, sizeof(range));
1792         if (err)
1793                 return err;
1794
1795         rrq->length = sizeof(range);
1796
1797         dldwd_lock(priv);
1798         ptype = priv->port_type;
1799         dldwd_unlock(priv);
1800
1801         memset(&range, 0, sizeof(range));
1802
1803         /* Much of this shamelessly taken from wvlan_cs.c. No idea
1804          * what it all means -dgibson */
1805         range.min_nwid = range.max_nwid = 0; /* We don't use nwids */
1806
1807         /* Set available channels/frequencies */
1808         range.num_channels = NUM_CHANNELS;
1809         k = 0;
1810         for (i = 0; i < NUM_CHANNELS; i++) {
1811                 if (priv->channel_mask & (1 << i)) {
1812                         range.freq[k].i = i + 1;
1813                         range.freq[k].m = channel_frequency[i] * 100000;
1814                         range.freq[k].e = 1;
1815                         k++;
1816                 }
1817                 
1818                 if (k >= IW_MAX_FREQUENCIES)
1819                         break;
1820         }
1821         range.num_frequency = k;
1822
1823         range.sensitivity = 3;
1824
1825         if ((ptype == 3) && (priv->spy_number == 0)){
1826                 /* Quality stats meaningless in ad-hoc mode */
1827                 range.max_qual.qual = 0;
1828                 range.max_qual.level = 0;
1829                 range.max_qual.noise = 0;
1830         } else {
1831                 range.max_qual.qual = 0x8b - 0x2f;
1832                 range.max_qual.level = 0x2f - 0x95 - 1;
1833                 range.max_qual.noise = 0x2f - 0x95 - 1;
1834         }
1835
1836         err = dldwd_hw_get_bitratelist(priv, &numrates,
1837                                        range.bitrate, IW_MAX_BITRATES);
1838         if (err)
1839                 return err;
1840         range.num_bitrates = numrates;
1841         
1842         /* Set an indication of the max TCP throughput in bit/s that we can
1843          * expect using this interface. May be use for QoS stuff...
1844          * Jean II */
1845         if(numrates > 2)
1846                 range.throughput = 5 * 1000 * 1000;     /* ~5 Mb/s */
1847         else
1848                 range.throughput = 1.5 * 1000 * 1000;   /* ~1.5 Mb/s */
1849
1850         range.min_rts = 0;
1851         range.max_rts = 2347;
1852         range.min_frag = 256;
1853         range.max_frag = 2346;
1854
1855         dldwd_lock(priv);
1856         if (priv->has_wep) {
1857                 range.max_encoding_tokens = MAX_KEYS;
1858
1859                 range.encoding_size[0] = SMALL_KEY_SIZE;
1860                 range.num_encoding_sizes = 1;
1861
1862                 if (priv->has_big_wep) {
1863                         range.encoding_size[1] = LARGE_KEY_SIZE;
1864                         range.num_encoding_sizes = 2;
1865                 }
1866         } else {
1867                 range.num_encoding_sizes = 0;
1868                 range.max_encoding_tokens = 0;
1869         }
1870         dldwd_unlock(priv);
1871                 
1872         range.min_pmp = 0;
1873         range.max_pmp = 65535000;
1874         range.min_pmt = 0;
1875         range.max_pmt = 65535 * 1000;   /* ??? */
1876         range.pmp_flags = IW_POWER_PERIOD;
1877         range.pmt_flags = IW_POWER_TIMEOUT;
1878         range.pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_UNICAST_R;
1879
1880         range.num_txpower = 1;
1881         range.txpower[0] = 15; /* 15dBm */
1882         range.txpower_capa = IW_TXPOW_DBM;
1883
1884         if (copy_to_user(rrq->pointer, &range, sizeof(range)))
1885                 return -EFAULT;
1886
1887         TRACE_EXIT(dev->name);
1888
1889         return 0;
1890 }
1891
1892 static int dldwd_ioctl_setiwencode(struct net_device *dev, struct iw_point *erq)
1893 {
1894         dldwd_priv_t *priv = dev->priv;
1895         int index = (erq->flags & IW_ENCODE_INDEX) - 1;
1896         int setindex = priv->tx_key;
1897         int enable = priv->wep_on;
1898         int auth = priv->wep_auth;
1899         uint16_t xlen = 0;
1900         int err = 0;
1901         char keybuf[MAX_KEY_SIZE];
1902
1903         if (erq->pointer) {
1904                 /* We actually have a key to set */
1905                 
1906                 if (copy_from_user(keybuf, erq->pointer, erq->length))
1907                         return -EFAULT;
1908         }
1909         
1910         dldwd_lock(priv);
1911         
1912         if (erq->pointer) {
1913                 if (erq->length > MAX_KEY_SIZE) {
1914                         err = -E2BIG;
1915                         goto out;
1916                 }
1917                 
1918                 if ( (erq->length > LARGE_KEY_SIZE)
1919                      || ( ! priv->has_big_wep && (erq->length > SMALL_KEY_SIZE))  ) {
1920                         err = -EINVAL;
1921                         goto out;
1922                 }
1923                 
1924                 if ((index < 0) || (index >= MAX_KEYS))
1925                         index = priv->tx_key;
1926                 
1927                 if (erq->length > SMALL_KEY_SIZE) {
1928                         xlen = LARGE_KEY_SIZE;
1929                 } else if (erq->length > 0) {
1930                         xlen = SMALL_KEY_SIZE;
1931                 } else
1932                         xlen = 0;
1933                 
1934                 /* Switch on WEP if off */
1935                 if ((!enable) && (xlen > 0)) {
1936                         setindex = index;
1937                         enable = 1;
1938                 }
1939         } else {
1940                 /* Important note : if the user do "iwconfig eth0 enc off",
1941                  * we will arrive there with an index of -1. This is valid
1942                  * but need to be taken care off... Jean II */
1943                 if ((index < 0) || (index >= MAX_KEYS)) {
1944                         if((index != -1) || (erq->flags == 0)) {
1945                                 err = -EINVAL;
1946                                 goto out;
1947                         }
1948                 } else {
1949                         /* Set the index : Check that the key is valid */
1950                         if(priv->keys[index].len == 0) {
1951                                 err = -EINVAL;
1952                                 goto out;
1953                         }
1954                         setindex = index;
1955                 }
1956         }
1957         
1958         if (erq->flags & IW_ENCODE_DISABLED)
1959                 enable = 0;
1960         /* Only for symbol cards (so far) - Jean II */
1961         if (erq->flags & IW_ENCODE_OPEN)
1962                 auth = 1;
1963         if (erq->flags & IW_ENCODE_RESTRICTED)
1964                 auth = 2;       /* If all key are 128 -> should be 3 ??? */
1965         /* Agree with master wep setting */
1966         if (enable == 0)
1967                 auth = 0;
1968         else if(auth == 0)
1969                 auth = 1;       /* Encryption require some authentication */
1970
1971         if (erq->pointer) {
1972                 priv->keys[index].len = cpu_to_le16(xlen);
1973                 memset(priv->keys[index].data, 0, sizeof(priv->keys[index].data));
1974                 memcpy(priv->keys[index].data, keybuf, erq->length);
1975         }
1976         priv->tx_key = setindex;
1977         priv->wep_on = enable;
1978         priv->wep_auth = auth;
1979         
1980  out:
1981         dldwd_unlock(priv);
1982
1983         return 0;
1984 }
1985
1986 static int dldwd_ioctl_getiwencode(struct net_device *dev, struct iw_point *erq)
1987 {
1988         dldwd_priv_t *priv = dev->priv;
1989         int index = (erq->flags & IW_ENCODE_INDEX) - 1;
1990         uint16_t xlen = 0;
1991         char keybuf[MAX_KEY_SIZE];
1992
1993         
1994         dldwd_lock(priv);
1995
1996         if ((index < 0) || (index >= MAX_KEYS))
1997                 index = priv->tx_key;
1998
1999         erq->flags = 0;
2000         if (! priv->wep_on)
2001                 erq->flags |= IW_ENCODE_DISABLED;
2002         erq->flags |= index + 1;
2003         
2004         /* Only for symbol cards - Jean II */
2005         if (priv->firmware_type == FIRMWARE_TYPE_SYMBOL) {
2006                 switch(priv->wep_auth)  {
2007                 case 1:
2008                         erq->flags |= IW_ENCODE_OPEN;
2009                         break;
2010                 case 2:
2011                 case 3:
2012                         erq->flags |= IW_ENCODE_RESTRICTED;
2013                         break;
2014                 case 0:
2015                 default:
2016                         break;
2017                 }
2018         }
2019
2020         xlen = le16_to_cpu(priv->keys[index].len);
2021
2022         erq->length = xlen;
2023
2024         if (erq->pointer) {
2025                 memcpy(keybuf, priv->keys[index].data, MAX_KEY_SIZE);
2026         }
2027         
2028         dldwd_unlock(priv);
2029
2030         if (erq->pointer) {
2031                 if (copy_to_user(erq->pointer, keybuf, xlen))
2032                         return -EFAULT;
2033         }
2034
2035         return 0;
2036 }
2037
2038 static int dldwd_ioctl_setessid(struct net_device *dev, struct iw_point *erq)
2039 {
2040         dldwd_priv_t *priv = dev->priv;
2041         char essidbuf[IW_ESSID_MAX_SIZE+1];
2042
2043         /* Note : ESSID is ignored in Ad-Hoc demo mode, but we can set it
2044          * anyway... - Jean II */
2045
2046         memset(&essidbuf, 0, sizeof(essidbuf));
2047
2048         if (erq->flags) { 
2049                 if (erq->length > IW_ESSID_MAX_SIZE)
2050                         return -E2BIG;
2051                 
2052                 if (copy_from_user(&essidbuf, erq->pointer, erq->length))
2053                         return -EFAULT;
2054
2055                 essidbuf[erq->length] = '\0';
2056         }
2057
2058         dldwd_lock(priv);
2059
2060         memcpy(priv->desired_essid, essidbuf, IW_ESSID_MAX_SIZE+1);
2061
2062         dldwd_unlock(priv);
2063
2064         return 0;
2065 }
2066
2067 static int dldwd_ioctl_getessid(struct net_device *dev, struct iw_point *erq)
2068 {
2069         dldwd_priv_t *priv = dev->priv;
2070         char essidbuf[IW_ESSID_MAX_SIZE+1];
2071         int active;
2072         int err = 0;
2073
2074         TRACE_ENTER(dev->name);
2075
2076         err = dldwd_hw_get_essid(priv, &active, essidbuf);
2077         if (err)
2078                 return err;
2079
2080         erq->flags = 1;
2081         erq->length = strlen(essidbuf) + 1;
2082         if (erq->pointer)
2083                 if ( copy_to_user(erq->pointer, essidbuf, erq->length) )
2084                         return -EFAULT;
2085
2086         TRACE_EXIT(dev->name);
2087         
2088         return 0;
2089 }
2090
2091 static int dldwd_ioctl_setnick(struct net_device *dev, struct iw_point *nrq)
2092 {
2093         dldwd_priv_t *priv = dev->priv;
2094         char nickbuf[IW_ESSID_MAX_SIZE+1];
2095
2096         if (nrq->length > IW_ESSID_MAX_SIZE)
2097                 return -E2BIG;
2098
2099         memset(nickbuf, 0, sizeof(nickbuf));
2100
2101         if (copy_from_user(nickbuf, nrq->pointer, nrq->length))
2102                 return -EFAULT;
2103
2104         nickbuf[nrq->length] = '\0';
2105         
2106         dldwd_lock(priv);
2107
2108         memcpy(priv->nick, nickbuf, sizeof(priv->nick));
2109
2110         dldwd_unlock(priv);
2111
2112         return 0;
2113 }
2114
2115 static int dldwd_ioctl_getnick(struct net_device *dev, struct iw_point *nrq)
2116 {
2117         dldwd_priv_t *priv = dev->priv;
2118         char nickbuf[IW_ESSID_MAX_SIZE+1];
2119
2120         dldwd_lock(priv);
2121         memcpy(nickbuf, priv->nick, IW_ESSID_MAX_SIZE+1);
2122         dldwd_unlock(priv);
2123
2124         nrq->length = strlen(nickbuf)+1;
2125
2126         if (copy_to_user(nrq->pointer, nickbuf, sizeof(nickbuf)))
2127                 return -EFAULT;
2128
2129         return 0;
2130 }
2131
2132 static int dldwd_ioctl_setfreq(struct net_device *dev, struct iw_freq *frq)
2133 {
2134         dldwd_priv_t *priv = dev->priv;
2135         int chan = -1;
2136
2137         /* We can only use this in Ad-Hoc demo mode to set the operating
2138          * frequency, or in IBSS mode to set the frequency where the IBSS
2139          * will be created - Jean II */
2140         if (priv->iw_mode != IW_MODE_ADHOC)
2141                 return -EOPNOTSUPP;
2142
2143         if ( (frq->e == 0) && (frq->m <= 1000) ) {
2144                 /* Setting by channel number */
2145                 chan = frq->m;
2146         } else {
2147                 /* Setting by frequency - search the table */
2148                 int mult = 1;
2149                 int i;
2150
2151                 for (i = 0; i < (6 - frq->e); i++)
2152                         mult *= 10;
2153
2154                 for (i = 0; i < NUM_CHANNELS; i++)
2155                         if (frq->m == (channel_frequency[i] * mult))
2156                                 chan = i+1;
2157         }
2158
2159         if ( (chan < 1) || (chan > NUM_CHANNELS) ||
2160              ! (priv->channel_mask & (1 << (chan-1)) ) )
2161                 return -EINVAL;
2162
2163         dldwd_lock(priv);
2164         priv->channel = chan;
2165         dldwd_unlock(priv);
2166
2167         return 0;
2168 }
2169
2170 static int dldwd_ioctl_getsens(struct net_device *dev, struct iw_param *srq)
2171 {
2172         dldwd_priv_t *priv = dev->priv;
2173         hermes_t *hw = &priv->hw;
2174         uint16_t val;
2175         int err;
2176
2177         dldwd_lock(priv);
2178         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNF_SYSTEM_SCALE, &val);
2179         dldwd_unlock(priv);
2180
2181         if (err)
2182                 return err;
2183
2184         srq->value = val;
2185         srq->fixed = 0; /* auto */
2186
2187         return 0;
2188 }
2189
2190 static int dldwd_ioctl_setsens(struct net_device *dev, struct iw_param *srq)
2191 {
2192         dldwd_priv_t *priv = dev->priv;
2193         int val = srq->value;
2194
2195         if ((val < 1) || (val > 3))
2196                 return -EINVAL;
2197         
2198         dldwd_lock(priv);
2199         priv->ap_density = val;
2200         dldwd_unlock(priv);
2201
2202         return 0;
2203 }
2204
2205 static int dldwd_ioctl_setrts(struct net_device *dev, struct iw_param *rrq)
2206 {
2207         dldwd_priv_t *priv = dev->priv;
2208         int val = rrq->value;
2209
2210         if (rrq->disabled)
2211                 val = 2347;
2212
2213         if ( (val < 0) || (val > 2347) )
2214                 return -EINVAL;
2215
2216         dldwd_lock(priv);
2217         priv->rts_thresh = val;
2218         dldwd_unlock(priv);
2219
2220         return 0;
2221 }
2222
2223 static int dldwd_ioctl_setfrag(struct net_device *dev, struct iw_param *frq)
2224 {
2225         dldwd_priv_t *priv = dev->priv;
2226         int err = 0;
2227
2228         dldwd_lock(priv);
2229
2230         if (priv->has_mwo) {
2231                 if (frq->disabled)
2232                         priv->mwo_robust = 0;
2233                 else {
2234                         if (frq->fixed)
2235                                 printk(KERN_WARNING "%s: Fixed fragmentation not \
2236 supported on this firmware. Using MWO robust instead.\n", dev->name);
2237                         priv->mwo_robust = 1;
2238                 }
2239         } else {
2240                 if (frq->disabled)
2241                         priv->frag_thresh = 2346;
2242                 else {
2243                         if ( (frq->value < 256) || (frq->value > 2346) )
2244                                 err = -EINVAL;
2245                         else
2246                                 priv->frag_thresh = frq->value & ~0x1; /* must be even */
2247                 }
2248         }
2249
2250         dldwd_unlock(priv);
2251
2252         return err;
2253 }
2254
2255 static int dldwd_ioctl_getfrag(struct net_device *dev, struct iw_param *frq)
2256 {
2257         dldwd_priv_t *priv = dev->priv;
2258         hermes_t *hw = &priv->hw;
2259         int err = 0;
2260         uint16_t val;
2261
2262         dldwd_lock(priv);
2263         
2264         if (priv->has_mwo) {
2265                 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNF_MWO_ROBUST, &val);
2266                 if (err)
2267                         val = 0;
2268
2269                 frq->value = val ? 2347 : 0;
2270                 frq->disabled = ! val;
2271                 frq->fixed = 0;
2272         } else {
2273                 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNF_FRAG_THRESH, &val);
2274                 if (err)
2275                         val = 0;
2276
2277                 frq->value = val;
2278                 frq->disabled = (val >= 2346);
2279                 frq->fixed = 1;
2280         }
2281
2282         dldwd_unlock(priv);
2283         
2284         return err;
2285 }
2286
2287 static int dldwd_ioctl_setrate(struct net_device *dev, struct iw_param *rrq)
2288 {
2289         dldwd_priv_t *priv = dev->priv;
2290         int err = 0;
2291         int rate_ctrl = -1;
2292         int fixed, upto;
2293         int brate;
2294         int i;
2295
2296         dldwd_lock(priv);
2297
2298         /* Normalise value */
2299         brate = rrq->value / 500000;
2300
2301         switch (priv->firmware_type) {
2302         case FIRMWARE_TYPE_LUCENT: /* Lucent style rate */
2303                 if (! rrq->fixed) {
2304                         if (brate > 0)
2305                                 brate = -brate;
2306                         else
2307                                 brate = -22;
2308                 }
2309         
2310                 for (i = 0; i < NUM_RATES; i++)
2311                         if (rate_list[i] == brate) {
2312                                 rate_ctrl = i;
2313                                 break;
2314                         }
2315         
2316                 if ( (rate_ctrl < 1) || (rate_ctrl >= NUM_RATES) )
2317                         err = -EINVAL;
2318                 else
2319                         priv->tx_rate_ctrl = rate_ctrl;
2320                 break;
2321         case FIRMWARE_TYPE_PRISM2: /* Prism II style rate */
2322         case FIRMWARE_TYPE_SYMBOL: /* Symbol style rate */
2323                 switch(brate) {
2324                 case 0:
2325                         fixed = 0x0;
2326                         upto = 0x15;
2327                         break;
2328                 case 2:
2329                         fixed = 0x1;
2330                         upto = 0x1;
2331                         break;
2332                 case 4:
2333                         fixed = 0x2;
2334                         upto = 0x3;
2335                         break;
2336                 case 11:
2337                         fixed = 0x4;
2338                         upto = 0x7;
2339                         break;
2340                 case 22:
2341                         fixed = 0x8;
2342                         upto = 0x15;
2343                         break;
2344                 default:
2345                         fixed = 0x0;
2346                         upto = 0x0;
2347                 }
2348                 if (rrq->fixed)
2349                         rate_ctrl = fixed;
2350                 else
2351                         rate_ctrl = upto;
2352                 if (rate_ctrl == 0)
2353                         err = -EINVAL;
2354                 else
2355                         priv->tx_rate_ctrl = rate_ctrl;
2356                 break;
2357         }
2358
2359         dldwd_unlock(priv);
2360
2361         return err;
2362 }
2363
2364 static int dldwd_ioctl_getrate(struct net_device *dev, struct iw_param *rrq)
2365 {
2366         dldwd_priv_t *priv = dev->priv;
2367         hermes_t *hw = &priv->hw;
2368         int err = 0;
2369         uint16_t val;
2370         int brate = 0;
2371
2372         dldwd_lock(priv);
2373         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNF_TX_RATE_CTRL, &val);
2374         if (err)
2375                 goto out;
2376         
2377         switch (priv->firmware_type) {
2378         case FIRMWARE_TYPE_LUCENT: /* Lucent style rate */
2379                 brate = rate_list[val];
2380         
2381                 if (brate < 0) {
2382                         rrq->fixed = 0;
2383
2384                         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CURRENT_TX_RATE, &val);
2385                         if (err)
2386                                 goto out;
2387
2388                         if (val == 6)
2389                                 brate = 11;
2390                         else
2391                                 brate = 2*val;
2392                 } else
2393                         rrq->fixed = 1;
2394                 break;
2395         case FIRMWARE_TYPE_PRISM2: /* Prism II style rate */
2396         case FIRMWARE_TYPE_SYMBOL: /* Symbol style rate */
2397                 /* Check if auto or fixed (crude approximation) */
2398                 if((val & 0x1) && (val > 1)) {
2399                         rrq->fixed = 0;
2400
2401                         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CURRENT_TX_RATE, &val);
2402                         if (err)
2403                                 goto out;
2404                 } else
2405                         rrq->fixed = 1;
2406
2407                 if(val >= 8)
2408                         brate = 22;
2409                 else if(val >= 4)
2410                         brate = 11;
2411                 else if(val >= 2)
2412                         brate = 4;
2413                 else
2414                         brate = 2;
2415                 break;
2416         }
2417
2418         rrq->value = brate * 500000;
2419         rrq->disabled = 0;
2420
2421  out:
2422         dldwd_unlock(priv);
2423
2424         return err;
2425 }
2426
2427 static int dldwd_ioctl_setpower(struct net_device *dev, struct iw_param *prq)
2428 {
2429         dldwd_priv_t *priv = dev->priv;
2430         int err = 0;
2431
2432
2433         dldwd_lock(priv);
2434
2435         if (prq->disabled) {
2436                 priv->pm_on = 0;
2437         } else {
2438                 switch (prq->flags & IW_POWER_MODE) {
2439                 case IW_POWER_UNICAST_R:
2440                         priv->pm_mcast = 0;
2441                         priv->pm_on = 1;
2442                         break;
2443                 case IW_POWER_ALL_R:
2444                         priv->pm_mcast = 1;
2445                         priv->pm_on = 1;
2446                         break;
2447                 case IW_POWER_ON:
2448                         /* No flags : but we may have a value - Jean II */
2449                         break;
2450                 default:
2451                         err = -EINVAL;
2452                 }
2453                 if (err)
2454                         goto out;
2455                 
2456                 if (prq->flags & IW_POWER_TIMEOUT) {
2457                         priv->pm_on = 1;
2458                         priv->pm_timeout = prq->value / 1000;
2459                 }
2460                 if (prq->flags & IW_POWER_PERIOD) {
2461                         priv->pm_on = 1;
2462                         priv->pm_period = prq->value / 1000;
2463                 }
2464                 /* It's valid to not have a value if we are just toggling
2465                  * the flags... Jean II */
2466                 if(!priv->pm_on) {
2467                         err = -EINVAL;
2468                         goto out;
2469                 }                       
2470         }
2471
2472  out:
2473         dldwd_unlock(priv);
2474
2475         return err;
2476 }
2477
2478 static int dldwd_ioctl_getpower(struct net_device *dev, struct iw_param *prq)
2479 {
2480         dldwd_priv_t *priv = dev->priv;
2481         hermes_t *hw = &priv->hw;
2482         int err = 0;
2483         uint16_t enable, period, timeout, mcast;
2484
2485         dldwd_lock(priv);
2486         
2487         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNF_PM_ENABLE, &enable);
2488         if (err)
2489                 goto out;
2490
2491         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNF_PM_PERIOD, &period);
2492         if (err)
2493                 goto out;
2494
2495         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNF_PM_HOLDOVER, &timeout);
2496         if (err)
2497                 goto out;
2498
2499         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNF_PM_MCAST_RX, &mcast);
2500         if (err)
2501                 goto out;
2502
2503         prq->disabled = !enable;
2504         /* Note : by default, display the period */
2505         if ((prq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
2506                 prq->flags = IW_POWER_TIMEOUT;
2507                 prq->value = timeout * 1000;
2508         } else {
2509                 prq->flags = IW_POWER_PERIOD;
2510                 prq->value = period * 1000;
2511         }
2512         if (mcast)
2513                 prq->flags |= IW_POWER_ALL_R;
2514         else
2515                 prq->flags |= IW_POWER_UNICAST_R;
2516
2517  out:
2518         dldwd_unlock(priv);
2519
2520         return err;
2521 }
2522
2523 static int dldwd_ioctl_setport3(struct net_device *dev, struct iwreq *wrq)
2524 {
2525         dldwd_priv_t *priv = dev->priv;
2526         int val = *( (int *) wrq->u.name );
2527         int err = 0;
2528
2529         dldwd_lock(priv);
2530         switch (val) {
2531         case 0: /* Try to do IEEE ad-hoc mode */
2532                 if (! priv->has_ibss) {
2533                         err = -EINVAL;
2534                         break;
2535                 }
2536                 priv->prefer_port3 = 0;
2537                         
2538                 break;
2539
2540         case 1: /* Try to do Lucent proprietary ad-hoc mode */
2541                 if (! priv->has_port3) {
2542                         err = -EINVAL;
2543                         break;
2544                 }
2545                 priv->prefer_port3 = 1;
2546                 break;
2547
2548         default:
2549                 err = -EINVAL;
2550         }
2551
2552         dldwd_unlock(priv);
2553
2554         return err;
2555 }
2556
2557 static int dldwd_ioctl_getport3(struct net_device *dev, struct iwreq *wrq)
2558 {
2559         dldwd_priv_t *priv = dev->priv;
2560         int *val = (int *)wrq->u.name;
2561
2562         dldwd_lock(priv);
2563         *val = priv->prefer_port3;
2564         dldwd_unlock(priv);
2565
2566         return 0;
2567 }
2568
2569 /* Spy is used for link quality/strength measurements in Ad-Hoc mode
2570  * Jean II */
2571 static int dldwd_ioctl_setspy(struct net_device *dev, struct iw_point *srq)
2572 {
2573         dldwd_priv_t *priv = dev->priv;
2574         struct sockaddr address[IW_MAX_SPY];
2575         int number = srq->length;
2576         int i;
2577         int err = 0;
2578
2579         /* Check the number of addresses */
2580         if (number > IW_MAX_SPY)
2581                 return -E2BIG;
2582
2583         /* Get the data in the driver */
2584         if (srq->pointer) {
2585                 if (copy_from_user(address, srq->pointer,
2586                                    sizeof(struct sockaddr) * number))
2587                         return -EFAULT;
2588         }
2589
2590         /* Make sure nobody mess with the structure while we do */
2591         dldwd_lock(priv);
2592
2593         /* dldwd_lock() doesn't disable interrupts, so make sure the
2594          * interrupt rx path don't get confused while we copy */
2595         priv->spy_number = 0;
2596
2597         if (number > 0) {
2598                 /* Extract the addresses */
2599                 for (i = 0; i < number; i++)
2600                         memcpy(priv->spy_address[i], address[i].sa_data,
2601                                ETH_ALEN);
2602                 /* Reset stats */
2603                 memset(priv->spy_stat, 0,
2604                        sizeof(struct iw_quality) * IW_MAX_SPY);
2605                 /* Set number of addresses */
2606                 priv->spy_number = number;
2607         }
2608
2609         /* Time to show what we have done... */
2610         DEBUG(0, "%s: New spy list:\n", dev->name);
2611         for (i = 0; i < number; i++) {
2612                 DEBUG(0, "%s: %d - %02x:%02x:%02x:%02x:%02x:%02x\n",
2613                       dev->name, i+1,
2614                       priv->spy_address[i][0], priv->spy_address[i][1],
2615                       priv->spy_address[i][2], priv->spy_address[i][3],
2616                       priv->spy_address[i][4], priv->spy_address[i][5]);
2617         }
2618
2619         /* Now, let the others play */
2620         dldwd_unlock(priv);
2621
2622         return err;
2623 }
2624
2625 static int dldwd_ioctl_getspy(struct net_device *dev, struct iw_point *srq)
2626 {
2627         dldwd_priv_t *priv = dev->priv;
2628         struct sockaddr address[IW_MAX_SPY];
2629         struct iw_quality spy_stat[IW_MAX_SPY];
2630         int number;
2631         int i;
2632
2633         dldwd_lock(priv);
2634
2635         number = priv->spy_number;
2636         if ((number > 0) && (srq->pointer)) {
2637                 /* Create address struct */
2638                 for (i = 0; i < number; i++) {
2639                         memcpy(address[i].sa_data, priv->spy_address[i],
2640                                ETH_ALEN);
2641                         address[i].sa_family = AF_UNIX;
2642                 }
2643                 /* Copy stats */
2644                 /* In theory, we should disable irqs while copying the stats
2645                  * because the rx path migh update it in the middle...
2646                  * Bah, who care ? - Jean II */
2647                 memcpy(&spy_stat, priv->spy_stat,
2648                        sizeof(struct iw_quality) * IW_MAX_SPY);
2649                 for (i=0; i < number; i++)
2650                         priv->spy_stat[i].updated = 0;
2651         }
2652
2653         dldwd_unlock(priv);
2654
2655         /* Push stuff to user space */
2656         srq->length = number;
2657         if(copy_to_user(srq->pointer, address,
2658                          sizeof(struct sockaddr) * number))
2659                 return -EFAULT;
2660         if(copy_to_user(srq->pointer + (sizeof(struct sockaddr)*number),
2661                         &spy_stat, sizeof(struct iw_quality) * number))
2662                 return -EFAULT;
2663
2664         return 0;
2665 }
2666
2667 static int dldwd_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2668 {
2669         dldwd_priv_t *priv = dev->priv;
2670         struct iwreq *wrq = (struct iwreq *)rq;
2671         int err = 0;
2672         int changed = 0;
2673
2674         TRACE_ENTER(dev->name);
2675
2676         switch (cmd) {
2677         case SIOCGIWNAME:
2678                 DEBUG(1, "%s: SIOCGIWNAME\n", dev->name);
2679                 strcpy(wrq->u.name, "IEEE 802.11-DS");
2680                 break;
2681                 
2682         case SIOCGIWAP:
2683                 DEBUG(1, "%s: SIOCGIWAP\n", dev->name);
2684                 wrq->u.ap_addr.sa_family = ARPHRD_ETHER;
2685                 err = dldwd_hw_get_bssid(priv, wrq->u.ap_addr.sa_data);
2686                 break;
2687
2688         case SIOCGIWRANGE:
2689                 DEBUG(1, "%s: SIOCGIWRANGE\n", dev->name);
2690                 err = dldwd_ioctl_getiwrange(dev, &wrq->u.data);
2691                 break;
2692
2693         case SIOCSIWMODE:
2694                 DEBUG(1, "%s: SIOCSIWMODE\n", dev->name);
2695                 dldwd_lock(priv);
2696                 switch (wrq->u.mode) {
2697                 case IW_MODE_ADHOC:
2698                         if (! (priv->has_ibss || priv->has_port3) )
2699                                 err = -EINVAL;
2700                         else {
2701                                 priv->iw_mode = IW_MODE_ADHOC;
2702                                 changed = 1;
2703                         }
2704                         break;
2705
2706                 case IW_MODE_INFRA:
2707                         priv->iw_mode = IW_MODE_INFRA;
2708                         changed = 1;
2709                         break;
2710
2711                 default:
2712                         err = -EINVAL;
2713                         break;
2714                 }
2715                 set_port_type(priv);
2716                 dldwd_unlock(priv);
2717                 break;
2718
2719         case SIOCGIWMODE:
2720                 DEBUG(1, "%s: SIOCGIWMODE\n", dev->name);
2721                 dldwd_lock(priv);
2722                 wrq->u.mode = priv->iw_mode;
2723                 dldwd_unlock(priv);
2724                 break;
2725
2726         case SIOCSIWENCODE:
2727                 DEBUG(1, "%s: SIOCSIWENCODE\n", dev->name);
2728                 if (! priv->has_wep) {
2729                         err = -EOPNOTSUPP;
2730                         break;
2731                 }
2732
2733                 err = dldwd_ioctl_setiwencode(dev, &wrq->u.encoding);
2734                 if (! err)
2735                         changed = 1;
2736                 break;
2737
2738         case SIOCGIWENCODE:
2739                 DEBUG(1, "%s: SIOCGIWENCODE\n", dev->name);
2740                 if (! priv->has_wep) {
2741                         err = -EOPNOTSUPP;
2742                         break;
2743                 }
2744
2745                 if (! capable(CAP_NET_ADMIN)) {
2746                         err = -EPERM;
2747                         break;
2748                 }
2749
2750                 err = dldwd_ioctl_getiwencode(dev, &wrq->u.encoding);
2751                 break;
2752
2753         case SIOCSIWESSID:
2754                 DEBUG(1, "%s: SIOCSIWESSID\n", dev->name);
2755                 err = dldwd_ioctl_setessid(dev, &wrq->u.essid);
2756                 if (! err)
2757                         changed = 1;
2758                 break;
2759
2760         case SIOCGIWESSID:
2761                 DEBUG(1, "%s: SIOCGIWESSID\n", dev->name);
2762                 err = dldwd_ioctl_getessid(dev, &wrq->u.essid);
2763                 break;
2764
2765         case SIOCSIWNICKN:
2766                 DEBUG(1, "%s: SIOCSIWNICKN\n", dev->name);
2767                 err = dldwd_ioctl_setnick(dev, &wrq->u.data);
2768                 if (! err)
2769                         changed = 1;
2770                 break;
2771
2772         case SIOCGIWNICKN:
2773                 DEBUG(1, "%s: SIOCGIWNICKN\n", dev->name);
2774                 err = dldwd_ioctl_getnick(dev, &wrq->u.data);
2775                 break;
2776
2777         case SIOCGIWFREQ:
2778                 DEBUG(1, "%s: SIOCGIWFREQ\n", dev->name);
2779                 wrq->u.freq.m = dldwd_hw_get_freq(priv);
2780                 wrq->u.freq.e = 1;
2781                 break;
2782
2783         case SIOCSIWFREQ:
2784                 DEBUG(1, "%s: SIOCSIWFREQ\n", dev->name);
2785                 err = dldwd_ioctl_setfreq(dev, &wrq->u.freq);
2786                 if (! err)
2787                         changed = 1;
2788                 break;
2789
2790         case SIOCGIWSENS:
2791                 DEBUG(1, "%s: SIOCGIWSENS\n", dev->name);
2792                 err = dldwd_ioctl_getsens(dev, &wrq->u.sens);
2793                 break;
2794
2795         case SIOCSIWSENS:
2796                 DEBUG(1, "%s: SIOCSIWSENS\n", dev->name);
2797                 err = dldwd_ioctl_setsens(dev, &wrq->u.sens);
2798                 if (! err)
2799                         changed = 1;
2800                 break;
2801
2802         case SIOCGIWRTS:
2803                 DEBUG(1, "%s: SIOCGIWRTS\n", dev->name);
2804                 wrq->u.rts.value = priv->rts_thresh;
2805                 wrq->u.rts.disabled = (wrq->u.rts.value == 2347);
2806                 wrq->u.rts.fixed = 1;
2807                 break;
2808
2809         case SIOCSIWRTS:
2810                 DEBUG(1, "%s: SIOCSIWRTS\n", dev->name);
2811                 err = dldwd_ioctl_setrts(dev, &wrq->u.rts);
2812                 if (! err)
2813                         changed = 1;
2814                 break;
2815
2816         case SIOCSIWFRAG:
2817                 DEBUG(1, "%s: SIOCSIWFRAG\n", dev->name);
2818                 err = dldwd_ioctl_setfrag(dev, &wrq->u.frag);
2819                 if (! err)
2820                         changed = 1;
2821                 break;
2822
2823         case SIOCGIWFRAG:
2824                 DEBUG(1, "%s: SIOCGIWFRAG\n", dev->name);
2825                 err = dldwd_ioctl_getfrag(dev, &wrq->u.frag);
2826                 break;
2827
2828         case SIOCSIWRATE:
2829                 DEBUG(1, "%s: SIOCSIWRATE\n", dev->name);
2830                 err = dldwd_ioctl_setrate(dev, &wrq->u.bitrate);
2831                 if (! err)
2832                         changed = 1;
2833                 break;
2834
2835         case SIOCGIWRATE:
2836                 DEBUG(1, "%s: SIOCGIWRATE\n", dev->name);
2837                 err = dldwd_ioctl_getrate(dev, &wrq->u.bitrate);
2838                 break;
2839
2840         case SIOCSIWPOWER:
2841                 DEBUG(1, "%s: SIOCSIWPOWER\n", dev->name);
2842                 err = dldwd_ioctl_setpower(dev, &wrq->u.power);
2843                 if (! err)
2844                         changed = 1;
2845                 break;
2846
2847         case SIOCGIWPOWER:
2848                 DEBUG(1, "%s: SIOCGIWPOWER\n", dev->name);
2849                 err = dldwd_ioctl_getpower(dev, &wrq->u.power);
2850                 break;
2851
2852         case SIOCGIWTXPOW:
2853                 DEBUG(1, "%s: SIOCGIWTXPOW\n", dev->name);
2854                 /* The card only supports one tx power, so this is easy */
2855                 wrq->u.txpower.value = 15; /* dBm */
2856                 wrq->u.txpower.fixed = 1;
2857                 wrq->u.txpower.disabled = 0;
2858                 wrq->u.txpower.flags = IW_TXPOW_DBM;
2859                 break;
2860
2861         case SIOCSIWSPY:
2862                 DEBUG(1, "%s: SIOCSIWSPY\n", dev->name);
2863
2864                 err = dldwd_ioctl_setspy(dev, &wrq->u.data);
2865                 break;
2866
2867         case SIOCGIWSPY:
2868                 DEBUG(1, "%s: SIOCGIWSPY\n", dev->name);
2869
2870                 err = dldwd_ioctl_getspy(dev, &wrq->u.data);
2871                 break;
2872
2873         case SIOCGIWPRIV:
2874                 DEBUG(1, "%s: SIOCGIWPRIV\n", dev->name);
2875                 if (wrq->u.data.pointer) {
2876                         struct iw_priv_args privtab[] = {
2877                                 { SIOCDEVPRIVATE + 0x0, 0, 0, "force_reset" },
2878                                 { SIOCDEVPRIVATE + 0x2,
2879                                   IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
2880                                   0, "set_port3" },
2881                                 { SIOCDEVPRIVATE + 0x3, 0,
2882                                   IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
2883                                   "get_port3" }
2884                         };
2885
2886                         err = verify_area(VERIFY_WRITE, wrq->u.data.pointer, sizeof(privtab));
2887                         if (err)
2888                                 break;
2889                         
2890                         wrq->u.data.length = sizeof(privtab) / sizeof(privtab[0]);
2891                         if (copy_to_user(wrq->u.data.pointer, privtab, sizeof(privtab)))
2892                                 err = -EFAULT;
2893                 }
2894                 break;
2895                
2896         case SIOCDEVPRIVATE + 0x0: /* force_reset */
2897                 DEBUG(1, "%s: SIOCDEVPRIVATE + 0x0 (force_reset)\n",
2898                       dev->name);
2899                 if (! capable(CAP_NET_ADMIN)) {
2900                         err = -EPERM;
2901                         break;
2902                 }
2903                 
2904                 printk(KERN_DEBUG "%s: Forcing reset!\n", dev->name);
2905                 dldwd_reset(priv);
2906                 break;
2907
2908         case SIOCDEVPRIVATE + 0x2: /* set_port3 */
2909                 DEBUG(1, "%s: SIOCDEVPRIVATE + 0x2 (set_port3)\n",
2910                       dev->name);
2911                 if (! capable(CAP_NET_ADMIN)) {
2912                         err = -EPERM;
2913                         break;
2914                 }
2915
2916                 err = dldwd_ioctl_setport3(dev, wrq);
2917                 if (! err)
2918                         changed = 1;
2919                 break;
2920
2921         case SIOCDEVPRIVATE + 0x3: /* get_port3 */
2922                 DEBUG(1, "%s: SIOCDEVPRIVATE + 0x3 (get_port3)\n",
2923                       dev->name);
2924                 err = dldwd_ioctl_getport3(dev, wrq);
2925                 break;
2926
2927         default:
2928                 err = -EOPNOTSUPP;
2929         }
2930         
2931         if (! err && changed && netif_running(dev)) {
2932                 err = dldwd_reset(priv);
2933                 if (err)
2934                         dldwd_stop(dev);
2935         }               
2936
2937         TRACE_EXIT(dev->name);
2938                 
2939         return err;
2940 }
2941
2942 static int dldwd_change_mtu(struct net_device *dev, int new_mtu)
2943 {
2944         TRACE_ENTER(dev->name);
2945
2946         if ( (new_mtu < DLDWD_MIN_MTU) || (new_mtu > DLDWD_MAX_MTU) )
2947                 return -EINVAL;
2948
2949         dev->mtu = new_mtu;
2950
2951         TRACE_EXIT(dev->name);
2952
2953         return 0;
2954 }
2955
2956 static void __dldwd_set_multicast_list(struct net_device *dev)
2957 {
2958         dldwd_priv_t *priv = dev->priv;
2959         hermes_t *hw = &priv->hw;
2960         int err = 0;
2961         int promisc, allmulti, mc_count;
2962
2963         TRACE_ENTER(dev->name);
2964
2965         DEBUG(3, "dev->flags=0x%x, priv->promiscuous=%d, dev->mc_count=%d priv->mc_count=%d\n",
2966               dev->flags, priv->promiscuous, dev->mc_count, priv->mc_count);
2967
2968         /* The Hermes doesn't seem to have an allmulti mode, so we go
2969          * into promiscuous mode and let the upper levels deal. */
2970         if ( (dev->flags & IFF_PROMISC) ) {
2971                 promisc = 1;
2972                 allmulti = 0;
2973                 mc_count = 0;
2974         } else if ( (dev->flags & IFF_ALLMULTI) ||
2975                     (dev->mc_count > HERMES_MAX_MULTICAST) ) {
2976                 promisc = 0;
2977                 allmulti = 1;
2978                 mc_count = HERMES_MAX_MULTICAST;
2979         } else {
2980                 promisc = 0;
2981                 allmulti = 0;
2982                 mc_count = dev->mc_count;
2983         }
2984
2985         DEBUG(3, "promisc=%d mc_count=%d\n",
2986               promisc, mc_count);
2987
2988         if (promisc != priv->promiscuous) { /* Don't touch the hardware if we don't have to */
2989                 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNF_PROMISCUOUS,
2990                                            promisc);
2991                 if (err) {
2992                         printk(KERN_ERR "%s: Error %d setting promiscuity to %d.\n",
2993                                dev->name, err, promisc);
2994                 } else 
2995                         priv->promiscuous = promisc;
2996         }
2997
2998         if (allmulti) {
2999                 /* FIXME: This method of doing allmulticast reception
3000                    comes from the NetBSD driver. Haven't actually
3001                    tested whether it works or not. */
3002                 hermes_multicast_t mclist;
3003
3004                 memset(&mclist, 0, sizeof(mclist));
3005                 err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNF_MULTICAST_LIST, &mclist);
3006                 if (err)
3007                         printk(KERN_ERR "%s: Error %d setting multicast list.\n",
3008                                dev->name, err);
3009                 else
3010                         priv->allmulti = 1;
3011                        
3012         } else if (mc_count || (! mc_count && priv->mc_count) ) {
3013                 struct dev_mc_list *p = dev->mc_list;
3014                 hermes_multicast_t mclist;
3015                 int i;
3016
3017                 for (i = 0; i < mc_count; i++) {
3018                         /* First some paranoid checks */
3019                         if (! p) {
3020                                 printk(KERN_ERR "%s: Multicast list shorter than mc_count.\n",
3021                                        dev->name);
3022                                 break;
3023                         }
3024                         if (p->dmi_addrlen != ETH_ALEN) {
3025
3026                                 printk(KERN_ERR "%s: Bad address size (%d) in multicast list.\n",
3027                                        dev->name, p->dmi_addrlen);
3028                                 break;
3029                         }
3030
3031                         memcpy(mclist.addr[i], p->dmi_addr, ETH_ALEN);
3032                         p = p->next;
3033                 }
3034
3035                 /* More paranoia */
3036                 if (p)
3037                         printk(KERN_ERR "%s: Multicast list longer than mc_count.\n",
3038                                dev->name);
3039
3040                 priv->mc_count = i;                     
3041
3042                 DEBUG(3, "priv->mc_count = %d\n", priv->mc_count);
3043
3044                 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNF_MULTICAST_LIST,
3045                                        HERMES_BYTES_TO_RECLEN(priv->mc_count * ETH_ALEN),
3046                                        &mclist);
3047                 if (err)
3048                         printk(KERN_ERR "%s: Error %d setting multicast list.\n",
3049                                dev->name, err);
3050                 else
3051                         priv->allmulti = 0;
3052         }
3053
3054         /* Since we can set the promiscuous flag when it wasn't asked
3055            for, make sure the net_device knows about it. */
3056         if (priv->promiscuous)
3057                 dev->flags |= IFF_PROMISC;
3058         else
3059                 dev->flags &= ~IFF_PROMISC;
3060
3061         if (priv->allmulti)
3062                 dev->flags |= IFF_ALLMULTI;
3063         else
3064                 dev->flags &= ~IFF_ALLMULTI;
3065
3066         TRACE_EXIT(dev->name);
3067 }
3068
3069 /*
3070  * procfs stuff
3071  */
3072
3073 static struct proc_dir_entry *dir_base = NULL;
3074
3075 /*
3076  * This function updates the total amount of data printed so far. It then
3077  * determines if the amount of data printed into a buffer  has reached the
3078  * offset requested. If it hasn't, then the buffer is shifted over so that
3079  * the next bit of data can be printed over the old bit. If the total
3080  * amount printed so far exceeds the total amount requested, then this
3081  * function returns 1, otherwise 0.
3082  */
3083 static int 
3084
3085 shift_buffer(char *buffer, int requested_offset, int requested_len,
3086              int *total, int *slop, char **buf)
3087 {
3088         int printed;
3089         
3090         printed = *buf - buffer;
3091         if (*total + printed <= requested_offset) {
3092                 *total += printed;
3093                 *buf = buffer;
3094         }
3095         else {
3096                 if (*total < requested_offset) {
3097                         *slop = requested_offset - *total;
3098                 }
3099                 *total = requested_offset + printed - *slop;
3100         }
3101         if (*total > requested_offset + requested_len) {
3102                 return 1;
3103         }
3104         else {
3105                 return 0;
3106         }
3107 }
3108
3109 /*
3110  * This function calculates the actual start of the requested data
3111  * in the buffer. It also calculates actual length of data returned,
3112  * which could be less that the amount of data requested.
3113  */
3114 #define PROC_BUFFER_SIZE 4096
3115 #define PROC_SAFE_SIZE 3072
3116
3117 static int
3118 calc_start_len(char *buffer, char **start, int requested_offset,
3119                int requested_len, int total, char *buf)
3120 {
3121         int return_len, buffer_len;
3122         
3123         buffer_len = buf - buffer;
3124         if (buffer_len >= PROC_BUFFER_SIZE - 1) {
3125                 printk(KERN_ERR "calc_start_len: exceeded /proc buffer size\n");
3126         }
3127         
3128         /*
3129          * There may be bytes before and after the
3130          * chunk that was actually requested.
3131          */
3132         return_len = total - requested_offset;
3133         if (return_len < 0) {
3134                 return_len = 0;
3135         }
3136         *start = buf - return_len;
3137         if (return_len > requested_len) {
3138                 return_len = requested_len;
3139         }
3140         return return_len;
3141 }
3142
3143 static int
3144 dldwd_proc_get_hermes_regs(char *page, char **start, off_t requested_offset,
3145                            int requested_len, int *eof, void *data)
3146 {
3147         dldwd_priv_t *dev = (dldwd_priv_t *)data;
3148         hermes_t *hw = &dev->hw;
3149         char *buf;
3150         int total = 0, slop = 0;
3151
3152         buf = page;
3153
3154 #define DHERMESREG(name) buf += sprintf(buf, "%-16s: %04x\n", #name, hermes_read_regn(hw, name))
3155
3156         DHERMESREG(CMD);
3157         DHERMESREG(PARAM0);
3158         DHERMESREG(PARAM1);
3159         DHERMESREG(PARAM2);
3160         DHERMESREG(STATUS);
3161         DHERMESREG(RESP0);
3162         DHERMESREG(RESP1);
3163         DHERMESREG(RESP2);
3164         DHERMESREG(INFOFID);
3165         DHERMESREG(RXFID);
3166         DHERMESREG(ALLOCFID);
3167         DHERMESREG(TXCOMPLFID);
3168         DHERMESREG(SELECT0);
3169         DHERMESREG(OFFSET0);
3170         DHERMESREG(SELECT1);
3171         DHERMESREG(OFFSET1);
3172         DHERMESREG(EVSTAT);
3173         DHERMESREG(INTEN);
3174         DHERMESREG(EVACK);
3175         DHERMESREG(CONTROL);
3176         DHERMESREG(SWSUPPORT0);
3177         DHERMESREG(SWSUPPORT1);
3178         DHERMESREG(SWSUPPORT2);
3179         DHERMESREG(AUXPAGE);
3180         DHERMESREG(AUXOFFSET);
3181         DHERMESREG(AUXDATA);
3182 #undef DHERMESREG
3183
3184         shift_buffer(page, requested_offset, requested_len, &total,
3185                      &slop, &buf);
3186         return calc_start_len(page, start, requested_offset, requested_len,
3187                               total, buf);
3188 }
3189
3190 struct {
3191         uint16_t rid;
3192         char *name;
3193         int minlen, maxlen;
3194         int displaytype;
3195 #define DISPLAY_WORDS   0
3196 #define DISPLAY_BYTES   1
3197 #define DISPLAY_STRING  2
3198 } record_table[] = {
3199 #define RTCNFENTRY(name, type) { HERMES_RID_CNF_##name, #name, 0, LTV_BUF_SIZE, type }
3200         RTCNFENTRY(PORTTYPE, DISPLAY_WORDS),
3201         RTCNFENTRY(MACADDR, DISPLAY_BYTES),
3202         RTCNFENTRY(DESIRED_SSID, DISPLAY_STRING),
3203         RTCNFENTRY(CHANNEL, DISPLAY_WORDS),
3204         RTCNFENTRY(OWN_SSID, DISPLAY_STRING),
3205         RTCNFENTRY(SYSTEM_SCALE, DISPLAY_WORDS),
3206         RTCNFENTRY(MAX_DATA_LEN, DISPLAY_WORDS),
3207         RTCNFENTRY(PM_ENABLE, DISPLAY_WORDS),
3208         RTCNFENTRY(PM_MCAST_RX, DISPLAY_WORDS),
3209         RTCNFENTRY(PM_PERIOD, DISPLAY_WORDS),
3210         RTCNFENTRY(NICKNAME, DISPLAY_STRING),
3211         RTCNFENTRY(WEP_ON, DISPLAY_WORDS),
3212         RTCNFENTRY(MWO_ROBUST, DISPLAY_WORDS),
3213         RTCNFENTRY(MULTICAST_LIST, DISPLAY_BYTES),
3214         RTCNFENTRY(CREATEIBSS, DISPLAY_WORDS),
3215         RTCNFENTRY(FRAG_THRESH, DISPLAY_WORDS),
3216         RTCNFENTRY(RTS_THRESH, DISPLAY_WORDS),
3217         RTCNFENTRY(TX_RATE_CTRL, DISPLAY_WORDS),
3218         RTCNFENTRY(PROMISCUOUS, DISPLAY_WORDS),
3219         RTCNFENTRY(KEYS, DISPLAY_BYTES),
3220         RTCNFENTRY(TX_KEY, DISPLAY_WORDS),
3221         RTCNFENTRY(TICKTIME, DISPLAY_WORDS),
3222         RTCNFENTRY(PRISM2_TX_KEY, DISPLAY_WORDS),
3223         RTCNFENTRY(PRISM2_KEY0, DISPLAY_BYTES),
3224         RTCNFENTRY(PRISM2_KEY1, DISPLAY_BYTES),
3225         RTCNFENTRY(PRISM2_KEY2, DISPLAY_BYTES),
3226         RTCNFENTRY(PRISM2_KEY3, DISPLAY_BYTES),
3227         RTCNFENTRY(PRISM2_WEP_ON, DISPLAY_WORDS),
3228 #undef RTCNFENTRY
3229 #define RTINFENTRY(name,type) { HERMES_RID_##name, #name, 0, LTV_BUF_SIZE, type }
3230         RTINFENTRY(CHANNEL_LIST, DISPLAY_WORDS),
3231         RTINFENTRY(STAIDENTITY, DISPLAY_WORDS),
3232         RTINFENTRY(CURRENT_SSID, DISPLAY_STRING),
3233         RTINFENTRY(CURRENT_BSSID, DISPLAY_BYTES),
3234         RTINFENTRY(COMMSQUALITY, DISPLAY_WORDS),
3235         RTINFENTRY(CURRENT_TX_RATE, DISPLAY_WORDS),
3236         RTINFENTRY(WEP_AVAIL, DISPLAY_WORDS),
3237         RTINFENTRY(CURRENT_CHANNEL, DISPLAY_WORDS),
3238         RTINFENTRY(DATARATES, DISPLAY_BYTES),
3239 #undef RTINFENTRY
3240 };
3241 #define NUM_RIDS ( sizeof(record_table) / sizeof(record_table[0]) )
3242
3243 static int
3244 dldwd_proc_get_hermes_recs(char *page, char **start, off_t requested_offset,
3245                            int requested_len, int *eof, void *data)
3246 {
3247         dldwd_priv_t *dev = (dldwd_priv_t *)data;
3248         hermes_t *hw = &dev->hw;
3249         char *buf;
3250         int total = 0, slop = 0;
3251         int i;
3252         uint16_t length;
3253         int err;
3254
3255         buf = page;
3256
3257         /* print out all the config RIDs */
3258         for (i = 0; i < NUM_RIDS; i++) {
3259                 uint16_t rid = record_table[i].rid;
3260                 int minlen = record_table[i].minlen;
3261                 int maxlen = record_table[i].maxlen;
3262                 int len;
3263                 uint8_t *val8;
3264                 uint16_t *val16;
3265                 int j;
3266
3267                 val8 = kmalloc(maxlen + 2, GFP_KERNEL);
3268                 if (! val8)
3269                         return -ENOMEM;
3270
3271                 err = hermes_read_ltv(hw, USER_BAP, rid, maxlen,
3272                                       &length, val8);
3273                 if (err) {
3274                         DEBUG(0, "Error %d reading RID 0x%04x\n", err, rid);
3275                         continue;
3276                 }
3277                 val16 = (uint16_t *)val8;
3278
3279                 buf += sprintf(buf, "%-15s (0x%04x): length=%d (%d bytes)\tvalue=", record_table[i].name,
3280                                rid, length, (length-1)*2);
3281                 len = MIN( MAX(minlen, (length-1)*2), maxlen);
3282
3283                 switch (record_table[i].displaytype) {
3284                 case DISPLAY_WORDS:
3285                         for (j = 0; j < len / 2; j++) {
3286                                 buf += sprintf(buf, "%04X-", le16_to_cpu(val16[j]));
3287                         }
3288                         buf--;
3289                         break;
3290
3291                 case DISPLAY_BYTES:
3292                 default:
3293                         for (j = 0; j < len; j++) {
3294                                 buf += sprintf(buf, "%02X:", val8[j]);
3295                         }
3296                         buf--;
3297                         break;
3298
3299                 case DISPLAY_STRING:
3300                         len = MIN(len, le16_to_cpu(val16[0])+2);
3301                         val8[len] = '\0';
3302                         buf += sprintf(buf, "\"%s\"", (char *)&val16[1]);
3303                         break;
3304                 }
3305
3306                 buf += sprintf(buf, "\n");
3307
3308                 kfree(val8);
3309
3310                 if (shift_buffer(page, requested_offset, requested_len,
3311                                  &total, &slop, &buf))
3312                         break;
3313
3314                 if ( (buf - page) > PROC_SAFE_SIZE )
3315                         break;
3316         }
3317
3318         return calc_start_len(page, start, requested_offset, requested_len,
3319                               total, buf);
3320 }
3321
3322 /* initialise the /proc subsystem for the hermes driver, creating the
3323  * separate entries */
3324 static int
3325 dldwd_proc_init(void)
3326 {
3327         int err = 0;
3328
3329         TRACE_ENTER("dldwd");
3330
3331         /* create the directory for it to sit in */
3332         dir_base = create_proc_entry("hermes", S_IFDIR, &proc_root);
3333         if (dir_base == NULL) {
3334                 printk(KERN_ERR "Unable to initialise /proc/hermes.\n");
3335                 dldwd_proc_cleanup();
3336                 err = -ENOMEM;
3337         }
3338
3339         TRACE_EXIT("dldwd");
3340
3341         return err;
3342 }
3343
3344 static int
3345 dldwd_proc_dev_init(dldwd_priv_t *dev)
3346 {
3347         dev->dir_dev = NULL;
3348         /* create the directory for it to sit in */
3349         dev->dir_dev = create_proc_entry(dev->node.dev_name, S_IFDIR | S_IRUGO | S_IXUGO,
3350                                          dir_base);
3351         if (dev->dir_dev == NULL) {
3352                 printk(KERN_ERR "Unable to initialise /proc/hermes/%s.\n",  dev->node.dev_name);
3353                 goto fail;
3354         }
3355
3356         dev->dir_regs = NULL;
3357         dev->dir_regs = create_proc_read_entry("regs", S_IFREG | S_IRUGO,
3358                                                dev->dir_dev, dldwd_proc_get_hermes_regs, dev);
3359         if (dev->dir_regs == NULL) {
3360                 printk(KERN_ERR "Unable to initialise /proc/hermes/%s/regs.\n",  dev->node.dev_name);
3361                 goto fail;
3362         }
3363
3364         dev->dir_recs = NULL;
3365         dev->dir_recs = create_proc_read_entry("recs", S_IFREG | S_IRUGO,
3366                                                dev->dir_dev, dldwd_proc_get_hermes_recs, dev);
3367         if (dev->dir_recs == NULL) {
3368                 printk(KERN_ERR "Unable to initialise /proc/hermes/%s/recs.\n",  dev->node.dev_name);
3369                 goto fail;
3370         }
3371
3372         return 0;
3373  fail:
3374         dldwd_proc_dev_cleanup(dev);
3375         return -ENOMEM;
3376 }
3377
3378 static void
3379 dldwd_proc_dev_cleanup(dldwd_priv_t *priv)
3380 {
3381         if (priv->dir_regs) {
3382                 remove_proc_entry("regs", priv->dir_dev);
3383                 priv->dir_regs = NULL;
3384         }               
3385         if (priv->dir_recs) {
3386                 remove_proc_entry("recs", priv->dir_dev);
3387                 priv->dir_recs = NULL;
3388         }               
3389         if (priv->dir_dev) {
3390                 remove_proc_entry(priv->node.dev_name, dir_base);
3391                 priv->dir_dev = NULL;
3392         }
3393 }
3394
3395 static void
3396 dldwd_proc_cleanup(void)
3397 {
3398         TRACE_ENTER("dldwd");
3399
3400         if (dir_base) {
3401                 remove_proc_entry("hermes", &proc_root);
3402                 dir_base = NULL;
3403         }
3404         
3405         TRACE_EXIT("dldwd");
3406 }
3407
3408 /*====================================================================*/
3409
3410 /*
3411  * From here on in, it's all PCMCIA junk, taken almost directly from
3412  * dummy_cs.c
3413  */
3414
3415 /*
3416    The dev_info variable is the "key" that is used to match up this
3417    device driver with appropriate cards, through the card configuration
3418    database.
3419 */
3420
3421 static dev_info_t dev_info = "orinoco_cs";
3422
3423 /*
3424    A linked list of "instances" of the dummy device.  Each actual
3425    PCMCIA card corresponds to one device instance, and is described
3426    by one dev_link_t structure (defined in ds.h).
3427
3428    You may not want to use a linked list for this -- for example, the
3429    memory card driver uses an array of dev_link_t pointers, where minor
3430    device numbers are used to derive the corresponding array index.
3431 */
3432
3433 static dev_link_t *dev_list = NULL;
3434 static int num_instances = 0;
3435
3436 /*====================================================================*/
3437
3438 static void cs_error(client_handle_t handle, int func, int ret)
3439 {
3440         error_info_t err = { func, ret };
3441         CardServices(ReportError, handle, &err);
3442 }
3443
3444 /*======================================================================
3445   dldwd_attach() creates an "instance" of the driver, allocating
3446   local data structures for one device.  The device is registered
3447   with Card Services.
3448   
3449   The dev_link structure is initialized, but we don't actually
3450   configure the card at this point -- we wait until we receive a
3451   card insertion event.
3452   ======================================================================*/
3453
3454 static dev_link_t *dldwd_attach(void)
3455 {
3456         dldwd_priv_t *priv;
3457         dev_link_t *link;
3458         struct net_device *ndev;
3459         client_reg_t client_reg;
3460         int ret, i;
3461
3462         TRACE_ENTER("dldwd");
3463
3464         /* Allocate space for private device-specific data */
3465         priv = kmalloc(sizeof(*priv), GFP_KERNEL);
3466         if (! priv) {
3467                 link = NULL;
3468                 goto out;
3469         }
3470
3471         memset(priv, 0, sizeof(*priv));
3472         priv->instance = num_instances++; /* FIXME: Racy? */
3473         spin_lock_init(&priv->lock);
3474
3475         link = &priv->link;
3476         ndev = &priv->ndev;
3477         link->priv = ndev->priv = priv;
3478
3479         /* Initialize the dev_link_t structure */
3480         link->release.function = &dldwd_release;
3481         link->release.data = (u_long) link;
3482
3483         /* Interrupt setup */
3484         link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
3485         link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
3486         if (irq_list[0] == -1)
3487                 link->irq.IRQInfo2 = irq_mask;
3488         else
3489                 for (i = 0; i < 4; i++)
3490                         link->irq.IRQInfo2 |= 1 << irq_list[i];
3491         link->irq.Handler = NULL;
3492
3493         /*
3494            General socket configuration defaults can go here.  In this
3495            client, we assume very little, and rely on the CIS for almost
3496            everything.  In most clients, many details (i.e., number, sizes,
3497            and attributes of IO windows) are fixed by the nature of the
3498            device, and can be hard-wired here.
3499          */
3500         link->conf.Attributes = 0;
3501         link->conf.IntType = INT_MEMORY_AND_IO;
3502
3503         /* Set up the net_device */
3504         ether_setup(ndev);
3505         ndev->init = dldwd_init;
3506         ndev->open = dldwd_open;
3507         ndev->stop = dldwd_stop;
3508         ndev->hard_start_xmit = dldwd_xmit;
3509         ndev->tx_timeout = dldwd_tx_timeout;
3510         ndev->watchdog_timeo = 4*HZ; /* 4 second timeout */
3511
3512         ndev->get_stats = dldwd_get_stats;
3513         ndev->get_wireless_stats = dldwd_get_wireless_stats;
3514
3515         ndev->do_ioctl = dldwd_ioctl;
3516         
3517         ndev->change_mtu = dldwd_change_mtu;
3518         ndev->set_multicast_list = dldwd_set_multicast_list;
3519
3520         netif_stop_queue(ndev);
3521
3522         /* Register with Card Services */
3523         link->next = dev_list;
3524         dev_list = link;
3525         client_reg.dev_info = &dev_info;
3526         client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
3527         client_reg.EventMask =
3528             CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
3529             CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
3530             CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
3531         client_reg.event_handler = &dldwd_event;
3532         client_reg.Version = 0x0210;
3533         client_reg.event_callback_args.client_data = link;
3534         ret = CardServices(RegisterClient, &link->handle, &client_reg);
3535         if (ret != CS_SUCCESS) {
3536                 cs_error(link->handle, RegisterClient, ret);
3537                 dldwd_detach(link);
3538                 link = NULL;
3539                 goto out;
3540         }
3541
3542  out:
3543         TRACE_EXIT("dldwd");
3544         return link;
3545 }                               /* dldwd_attach */
3546
3547 /*======================================================================
3548   This deletes a driver "instance".  The device is de-registered
3549   with Card Services.  If it has been released, all local data
3550   structures are freed.  Otherwise, the structures will be freed
3551   when the device is released.
3552   ======================================================================*/
3553
3554 static void dldwd_detach(dev_link_t * link)
3555 {
3556         dev_link_t **linkp;
3557         dldwd_priv_t *priv = link->priv;
3558
3559         TRACE_ENTER("dldwd");
3560
3561         /* Locate device structure */
3562         for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
3563                 if (*linkp == link)
3564                         break;
3565         if (*linkp == NULL)
3566                 goto out;
3567
3568         /*
3569            If the device is currently configured and active, we won't
3570            actually delete it yet.  Instead, it is marked so that when
3571            the release() function is called, that will trigger a proper
3572            detach().
3573          */
3574         if (link->state & DEV_CONFIG) {
3575 #ifdef PCMCIA_DEBUG
3576                 printk(KERN_DEBUG "orinoco_cs: detach postponed, '%s' "
3577                        "still locked\n", link->dev->dev_name);
3578 #endif
3579                 link->state |= DEV_STALE_LINK;
3580                 goto out;
3581         }
3582
3583         /* Break the link with Card Services */
3584         if (link->handle)
3585                 CardServices(DeregisterClient, link->handle);
3586
3587         /* Unlink device structure, and free it */
3588         *linkp = link->next;
3589         DEBUG(0, "orinoco_cs: detach: link=%p link->dev=%p\n", link, link->dev);
3590         if (link->dev) {
3591                 DEBUG(0, "orinoco_cs: About to unregister net device %p\n",
3592                       &priv->ndev);
3593                 unregister_netdev(&priv->ndev);
3594         }
3595         kfree(priv);
3596
3597         num_instances--; /* FIXME: Racy? */
3598
3599  out:
3600         TRACE_EXIT("dldwd");
3601 }                               /* dldwd_detach */
3602
3603 /*======================================================================
3604   dldwd_config() is scheduled to run after a CARD_INSERTION event
3605   is received, to configure the PCMCIA socket, and to make the
3606   device available to the system.
3607   ======================================================================*/
3608
3609 #define CS_CHECK(fn, args...) \
3610 while ((last_ret=CardServices(last_fn=(fn),args))!=0) goto cs_failed
3611
3612 #define CFG_CHECK(fn, args...) \
3613 if (CardServices(fn, args) != 0) goto next_entry
3614
3615 static void dldwd_config(dev_link_t * link)
3616 {
3617         client_handle_t handle = link->handle;
3618         dldwd_priv_t *priv = link->priv;
3619         hermes_t *hw = &priv->hw;
3620         struct net_device *ndev = &priv->ndev;
3621         tuple_t tuple;
3622         cisparse_t parse;
3623         int last_fn, last_ret;
3624         u_char buf[64];
3625         config_info_t conf;
3626         cisinfo_t info;
3627
3628         TRACE_ENTER("dldwd");
3629
3630         CS_CHECK(ValidateCIS, handle, &info);
3631
3632         /*
3633            This reads the card's CONFIG tuple to find its configuration
3634            registers.
3635          */
3636         tuple.DesiredTuple = CISTPL_CONFIG;
3637         tuple.Attributes = 0;
3638         tuple.TupleData = buf;
3639         tuple.TupleDataMax = sizeof(buf);
3640         tuple.TupleOffset = 0;
3641         CS_CHECK(GetFirstTuple, handle, &tuple);
3642         CS_CHECK(GetTupleData, handle, &tuple);
3643         CS_CHECK(ParseTuple, handle, &tuple, &parse);
3644         link->conf.ConfigBase = parse.config.base;
3645         link->conf.Present = parse.config.rmask[0];
3646
3647         /* Configure card */
3648         link->state |= DEV_CONFIG;
3649
3650         /* Look up the current Vcc */
3651         CS_CHECK(GetConfigurationInfo, handle, &conf);
3652         link->conf.Vcc = conf.Vcc;
3653
3654         DEBUG(0, "dldwd_config: ConfigBase = 0x%x link->conf.Vcc = %d\n", 
3655               link->conf.ConfigBase, link->conf.Vcc);
3656
3657         /*
3658            In this loop, we scan the CIS for configuration table entries,
3659            each of which describes a valid card configuration, including
3660            voltage, IO window, memory window, and interrupt settings.
3661
3662            We make no assumptions about the card to be configured: we use
3663            just the information available in the CIS.  In an ideal world,
3664            this would work for any PCMCIA card, but it requires a complete
3665            and accurate CIS.  In practice, a driver usually "knows" most of
3666            these things without consulting the CIS, and most client drivers
3667            will only use the CIS to fill in implementation-defined details.
3668          */
3669         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
3670         CS_CHECK(GetFirstTuple, handle, &tuple);
3671         while (1) {
3672                 cistpl_cftable_entry_t dflt = { 0 };
3673                 cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
3674                 CFG_CHECK(GetTupleData, handle, &tuple);
3675                 CFG_CHECK(ParseTuple, handle, &tuple, &parse);
3676
3677                 DEBUG(0, "dldwd_config: index = 0x%x, flags = 0x%x\n",
3678                       cfg->index, cfg->flags);
3679
3680                 if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
3681                         dflt = *cfg;
3682                 if (cfg->index == 0)
3683                         goto next_entry;
3684                 link->conf.ConfigIndex = cfg->index;
3685
3686                 /* Does this card need audio output? */
3687                 if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
3688                         link->conf.Attributes |= CONF_ENABLE_SPKR;
3689                         link->conf.Status = CCSR_AUDIO_ENA;
3690                 }
3691
3692                 /* Use power settings for Vcc and Vpp if present */
3693                 /*  Note that the CIS values need to be rescaled */
3694                 if (cfg->vcc.present & (1 << CISTPL_POWER_VNOM)) {
3695                         if (conf.Vcc !=
3696                             cfg->vcc.param[CISTPL_POWER_VNOM] /
3697                             10000) goto next_entry;
3698                 } else if (dflt.vcc.present & (1 << CISTPL_POWER_VNOM)) {
3699                         if (conf.Vcc !=
3700                             dflt.vcc.param[CISTPL_POWER_VNOM] /
3701                             10000) goto next_entry;
3702                 }
3703
3704                 if (cfg->vpp1.present & (1 << CISTPL_POWER_VNOM))
3705                         link->conf.Vpp1 = link->conf.Vpp2 =
3706                             cfg->vpp1.param[CISTPL_POWER_VNOM] / 10000;
3707                 else if (dflt.vpp1.present & (1 << CISTPL_POWER_VNOM))
3708                         link->conf.Vpp1 = link->conf.Vpp2 =
3709                             dflt.vpp1.param[CISTPL_POWER_VNOM] / 10000;
3710                 
3711                 DEBUG(0, "dldwd_config: We seem to have configured Vcc and Vpp\n");
3712
3713                 /* Do we need to allocate an interrupt? */
3714                 if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1)
3715                         link->conf.Attributes |= CONF_ENABLE_IRQ;
3716
3717                 /* IO window settings */
3718                 link->io.NumPorts1 = link->io.NumPorts2 = 0;
3719                 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
3720                         cistpl_io_t *io =
3721                             (cfg->io.nwin) ? &cfg->io : &dflt.io;
3722                         link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
3723                         if (!(io->flags & CISTPL_IO_8BIT))
3724                                 link->io.Attributes1 =
3725                                     IO_DATA_PATH_WIDTH_16;
3726                         if (!(io->flags & CISTPL_IO_16BIT))
3727                                 link->io.Attributes1 =
3728                                     IO_DATA_PATH_WIDTH_8;
3729                         link->io.IOAddrLines =
3730                             io->flags & CISTPL_IO_LINES_MASK;
3731                         link->io.BasePort1 = io->win[0].base;
3732                         link->io.NumPorts1 = io->win[0].len;
3733                         if (io->nwin > 1) {
3734                                 link->io.Attributes2 =
3735                                     link->io.Attributes1;
3736                                 link->io.BasePort2 = io->win[1].base;
3737                                 link->io.NumPorts2 = io->win[1].len;
3738                         }
3739
3740                         /* This reserves IO space but doesn't actually enable it */
3741                         CFG_CHECK(RequestIO, link->handle, &link->io);
3742                 }
3743
3744
3745                 /* If we got this far, we're cool! */
3746
3747                 break;
3748                 
3749         next_entry:
3750                 if (link->io.NumPorts1)
3751                         CardServices(ReleaseIO, link->handle, &link->io);
3752                 CS_CHECK(GetNextTuple, handle, &tuple);
3753         }
3754
3755         /*
3756            Allocate an interrupt line.  Note that this does not assign a
3757            handler to the interrupt, unless the 'Handler' member of the
3758            irq structure is initialized.
3759          */
3760         if (link->conf.Attributes & CONF_ENABLE_IRQ) {
3761                 int i;
3762
3763                 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
3764                 link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
3765                 if (irq_list[0] == -1)
3766                         link->irq.IRQInfo2 = irq_mask;
3767                 else
3768                         for (i=0; i<4; i++)
3769                                 link->irq.IRQInfo2 |= 1 << irq_list[i];
3770                 
3771                 link->irq.Handler = dldwd_interrupt; 
3772                 link->irq.Instance = priv; 
3773                 
3774                 CS_CHECK(RequestIRQ, link->handle, &link->irq);
3775         }
3776
3777         /* We initialize the hermes structure before completing PCMCIA
3778            configuration just in case the interrupt handler gets
3779            called. */
3780         hermes_struct_init(hw, link->io.BasePort1);
3781
3782         /*
3783            This actually configures the PCMCIA socket -- setting up
3784            the I/O windows and the interrupt mapping, and putting the
3785            card and host interface into "Memory and IO" mode.
3786          */
3787         CS_CHECK(RequestConfiguration, link->handle, &link->conf);
3788
3789         ndev->base_addr = link->io.BasePort1;
3790         ndev->irq = link->irq.AssignedIRQ;
3791
3792         /* Instance name : by default, use hermesX, on demand use the
3793          * regular ethX (less risky) - Jean II */
3794         if(!eth)
3795                 sprintf(ndev->name, "hermes%d", priv->instance);
3796         else
3797                 ndev->name[0] = '\0';
3798         /* Tell the stack we exist */
3799         if (register_netdev(ndev) != 0) {
3800                 printk(KERN_ERR "orinoco_cs: register_netdev() failed\n");
3801                 goto failed;
3802         }
3803         strcpy(priv->node.dev_name, ndev->name);
3804         
3805         /* Finally, report what we've done */
3806         printk(KERN_INFO "%s: index 0x%02x: Vcc %d.%d",
3807                priv->node.dev_name, link->conf.ConfigIndex,
3808                link->conf.Vcc / 10, link->conf.Vcc % 10);
3809         if (link->conf.Vpp1)
3810                 printk(", Vpp %d.%d", link->conf.Vpp1 / 10,
3811                        link->conf.Vpp1 % 10);
3812         if (link->conf.Attributes & CONF_ENABLE_IRQ)
3813                 printk(", irq %d", link->irq.AssignedIRQ);
3814         if (link->io.NumPorts1)
3815                 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
3816                        link->io.BasePort1 + link->io.NumPorts1 - 1);
3817         if (link->io.NumPorts2)
3818                 printk(" & 0x%04x-0x%04x", link->io.BasePort2,
3819                        link->io.BasePort2 + link->io.NumPorts2 - 1);
3820         printk("\n");
3821         
3822         /* And give us the proc nodes for debugging */
3823         if (dldwd_proc_dev_init(priv) != 0) {
3824                 printk(KERN_ERR "orinoco_cs: Failed to create /proc node for %s\n",
3825                        priv->node.dev_name);
3826                 goto failed;
3827         }
3828         
3829         /*
3830            At this point, the dev_node_t structure(s) need to be
3831            initialized and arranged in a linked list at link->dev.
3832          */
3833         priv->node.major = priv->node.minor = 0;
3834         link->dev = &priv->node;
3835         link->state &= ~DEV_CONFIG_PENDING;
3836
3837         TRACE_EXIT("dldwd");
3838
3839         return;
3840
3841  cs_failed:
3842         cs_error(link->handle, last_fn, last_ret);
3843  failed:
3844         dldwd_release((u_long) link);
3845
3846         TRACE_EXIT("dldwd");
3847 }                               /* dldwd_config */
3848
3849 /*======================================================================
3850   After a card is removed, dldwd_release() will unregister the
3851   device, and release the PCMCIA configuration.  If the device is
3852   still open, this will be postponed until it is closed.
3853   ======================================================================*/
3854
3855 static void dldwd_release(u_long arg)
3856 {
3857         dev_link_t *link = (dev_link_t *) arg;
3858         dldwd_priv_t *priv = link->priv;
3859
3860         TRACE_ENTER(link->dev->dev_name);
3861
3862         /*
3863            If the device is currently in use, we won't release until it
3864            is actually closed, because until then, we can't be sure that
3865            no one will try to access the device or its data structures.
3866          */
3867         if (link->open) {
3868                 DEBUG(0, "orinoco_cs: release postponed, '%s' still open\n",
3869                       link->dev->dev_name);
3870                 link->state |= DEV_STALE_CONFIG;
3871                 return;
3872         }
3873
3874         /*
3875            In a normal driver, additional code may be needed to release
3876            other kernel data structures associated with this device. 
3877          */
3878
3879         dldwd_proc_dev_cleanup(priv);
3880
3881         /* Don't bother checking to see if these succeed or not */
3882         CardServices(ReleaseConfiguration, link->handle);
3883         if (link->io.NumPorts1)
3884                 CardServices(ReleaseIO, link->handle, &link->io);
3885         if (link->irq.AssignedIRQ)
3886                 CardServices(ReleaseIRQ, link->handle, &link->irq);
3887         link->state &= ~DEV_CONFIG;
3888
3889         TRACE_EXIT(link->dev->dev_name);
3890 }                               /* dldwd_release */
3891
3892 /*======================================================================
3893   The card status event handler.  Mostly, this schedules other
3894   stuff to run after an event is received.
3895
3896   When a CARD_REMOVAL event is received, we immediately set a
3897   private flag to block future accesses to this device.  All the
3898   functions that actually access the device should check this flag
3899   to make sure the card is still present.
3900   ======================================================================*/
3901
3902 static int dldwd_event(event_t event, int priority,
3903                        event_callback_args_t * args)
3904 {
3905         dev_link_t *link = args->client_data;
3906         dldwd_priv_t *priv = (dldwd_priv_t *)link->priv;
3907         struct net_device *dev = &priv->ndev;
3908
3909         TRACE_ENTER("dldwd");
3910
3911         switch (event) {
3912         case CS_EVENT_CARD_REMOVAL:
3913                 dldwd_shutdown(priv);
3914                 link->state &= ~DEV_PRESENT;
3915                 if (link->state & DEV_CONFIG) {
3916                         netif_stop_queue(dev);
3917                         netif_device_detach(dev);
3918                         mod_timer(&link->release, jiffies + HZ / 20);
3919                 }
3920                 break;
3921         case CS_EVENT_CARD_INSERTION:
3922                 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
3923                 dldwd_config(link);
3924                 break;
3925         case CS_EVENT_PM_SUSPEND:
3926
3927                 link->state |= DEV_SUSPEND;
3928                 /* Fall through... */
3929         case CS_EVENT_RESET_PHYSICAL:
3930                 dldwd_shutdown(priv);
3931                 /* Mark the device as stopped, to block IO until later */
3932
3933                 if (link->state & DEV_CONFIG) {
3934                         if (link->open) {
3935                                 netif_stop_queue(dev);
3936                                 netif_device_detach(dev);
3937                         }
3938                         CardServices(ReleaseConfiguration, link->handle);
3939                 }
3940                 break;
3941         case CS_EVENT_PM_RESUME:
3942                 link->state &= ~DEV_SUSPEND;
3943                 /* Fall through... */
3944         case CS_EVENT_CARD_RESET:
3945                 if (link->state & DEV_CONFIG) {
3946                         CardServices(RequestConfiguration, link->handle,
3947                                      &link->conf);
3948
3949                         if (link->open) {
3950                                 if (dldwd_reset(priv) == 0) {
3951                                         netif_device_attach(dev);
3952                                         netif_start_queue(dev);
3953                                 } else {
3954                                         printk(KERN_ERR "%s: Error resetting device on PCMCIA event\n",
3955                                                dev->name);
3956                                         dldwd_stop(dev);
3957                                 }
3958                         }
3959                 }
3960                 /*
3961                    In a normal driver, additional code may go here to restore
3962                    the device state and restart IO. 
3963                  */
3964                 break;
3965         }
3966
3967         TRACE_EXIT("dldwd");
3968
3969         return 0;
3970 }                               /* dldwd_event */
3971
3972 static int __init init_dldwd_cs(void)
3973 {
3974         servinfo_t serv;
3975         int err;
3976         
3977         TRACE_ENTER("dldwd");
3978
3979         printk(KERN_INFO "dldwd: David's Less Dodgy WaveLAN/IEEE Driver\n");
3980
3981         DEBUG(0, "%s\n", version);
3982         CardServices(GetCardServicesInfo, &serv);
3983         if (serv.Revision != CS_RELEASE_CODE) {
3984                 printk(KERN_NOTICE "orinoco_cs: Card Services release "
3985                        "does not match!\n");
3986                 return -1;
3987         }
3988         register_pccard_driver(&dev_info, &dldwd_attach, &dldwd_detach);
3989
3990         
3991         err = dldwd_proc_init();
3992
3993         TRACE_EXIT("dldwd");
3994         return err;
3995 }
3996
3997 static void __exit exit_dldwd_cs(void)
3998 {
3999         TRACE_ENTER("dldwd");
4000
4001         dldwd_proc_cleanup();
4002
4003         unregister_pccard_driver(&dev_info);
4004
4005         if (dev_list)
4006                 DEBUG(0, "orinoco_cs: Removing leftover devices.\n");
4007         while (dev_list != NULL) {
4008                 del_timer(&dev_list->release);
4009                 if (dev_list->state & DEV_CONFIG)
4010                         dldwd_release((u_long) dev_list);
4011                 dldwd_detach(dev_list);
4012         }
4013
4014         TRACE_EXIT("dldwd");
4015 }
4016
4017 module_init(init_dldwd_cs);
4018 module_exit(exit_dldwd_cs);