]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/net/wireless/orinoco.c
orinoco: Process bulk of receive interrupt in a tasklet
[linux-2.6-omap-h63xx.git] / drivers / net / wireless / orinoco.c
1 /* orinoco.c - (formerly known as dldwd_cs.c and orinoco_cs.c)
2  *
3  * A driver for Hermes or Prism 2 chipset based PCMCIA wireless
4  * adaptors, with Lucent/Agere, Intersil or Symbol firmware.
5  *
6  * Current maintainers (as of 29 September 2003) are:
7  *      Pavel Roskin <proski AT gnu.org>
8  * and  David Gibson <hermes AT gibson.dropbear.id.au>
9  *
10  * (C) Copyright David Gibson, IBM Corporation 2001-2003.
11  * Copyright (C) 2000 David Gibson, Linuxcare Australia.
12  *      With some help from :
13  * Copyright (C) 2001 Jean Tourrilhes, HP Labs
14  * Copyright (C) 2001 Benjamin Herrenschmidt
15  *
16  * Based on dummy_cs.c 1.27 2000/06/12 21:27:25
17  *
18  * Portions based on wvlan_cs.c 1.0.6, Copyright Andreas Neuhaus <andy
19  * AT fasta.fh-dortmund.de>
20  *      http://www.stud.fh-dortmund.de/~andy/wvlan/
21  *
22  * The contents of this file are subject to the Mozilla Public License
23  * Version 1.1 (the "License"); you may not use this file except in
24  * compliance with the License. You may obtain a copy of the License
25  * at http://www.mozilla.org/MPL/
26  *
27  * Software distributed under the License is distributed on an "AS IS"
28  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
29  * the License for the specific language governing rights and
30  * limitations under the License.
31  *
32  * The initial developer of the original code is David A. Hinds
33  * <dahinds AT users.sourceforge.net>.  Portions created by David
34  * A. Hinds are Copyright (C) 1999 David A. Hinds.  All Rights
35  * Reserved.
36  *
37  * Alternatively, the contents of this file may be used under the
38  * terms of the GNU General Public License version 2 (the "GPL"), in
39  * which case the provisions of the GPL are applicable instead of the
40  * above.  If you wish to allow the use of your version of this file
41  * only under the terms of the GPL and not to allow others to use your
42  * version of this file under the MPL, indicate your decision by
43  * deleting the provisions above and replace them with the notice and
44  * other provisions required by the GPL.  If you do not delete the
45  * provisions above, a recipient may use your version of this file
46  * under either the MPL or the GPL.  */
47
48 /*
49  * TODO
50  *      o Handle de-encapsulation within network layer, provide 802.11
51  *        headers (patch from Thomas 'Dent' Mirlacher)
52  *      o Fix possible races in SPY handling.
53  *      o Disconnect wireless extensions from fundamental configuration.
54  *      o (maybe) Software WEP support (patch from Stano Meduna).
55  *      o (maybe) Use multiple Tx buffers - driver handling queue
56  *        rather than firmware.
57  */
58
59 /* Locking and synchronization:
60  *
61  * The basic principle is that everything is serialized through a
62  * single spinlock, priv->lock.  The lock is used in user, bh and irq
63  * context, so when taken outside hardirq context it should always be
64  * taken with interrupts disabled.  The lock protects both the
65  * hardware and the struct orinoco_private.
66  *
67  * Another flag, priv->hw_unavailable indicates that the hardware is
68  * unavailable for an extended period of time (e.g. suspended, or in
69  * the middle of a hard reset).  This flag is protected by the
70  * spinlock.  All code which touches the hardware should check the
71  * flag after taking the lock, and if it is set, give up on whatever
72  * they are doing and drop the lock again.  The orinoco_lock()
73  * function handles this (it unlocks and returns -EBUSY if
74  * hw_unavailable is non-zero).
75  */
76
77 #define DRIVER_NAME "orinoco"
78
79 #include <linux/module.h>
80 #include <linux/kernel.h>
81 #include <linux/init.h>
82 #include <linux/delay.h>
83 #include <linux/netdevice.h>
84 #include <linux/etherdevice.h>
85 #include <linux/ethtool.h>
86 #include <linux/firmware.h>
87 #include <linux/if_arp.h>
88 #include <linux/wireless.h>
89 #include <net/iw_handler.h>
90 #include <net/ieee80211.h>
91
92 #include "hermes_rid.h"
93 #include "hermes_dld.h"
94 #include "orinoco.h"
95
96 /********************************************************************/
97 /* Module information                                               */
98 /********************************************************************/
99
100 MODULE_AUTHOR("Pavel Roskin <proski@gnu.org> & David Gibson <hermes@gibson.dropbear.id.au>");
101 MODULE_DESCRIPTION("Driver for Lucent Orinoco, Prism II based and similar wireless cards");
102 MODULE_LICENSE("Dual MPL/GPL");
103
104 /* Level of debugging. Used in the macros in orinoco.h */
105 #ifdef ORINOCO_DEBUG
106 int orinoco_debug = ORINOCO_DEBUG;
107 module_param(orinoco_debug, int, 0644);
108 MODULE_PARM_DESC(orinoco_debug, "Debug level");
109 EXPORT_SYMBOL(orinoco_debug);
110 #endif
111
112 static int suppress_linkstatus; /* = 0 */
113 module_param(suppress_linkstatus, bool, 0644);
114 MODULE_PARM_DESC(suppress_linkstatus, "Don't log link status changes");
115 static int ignore_disconnect; /* = 0 */
116 module_param(ignore_disconnect, int, 0644);
117 MODULE_PARM_DESC(ignore_disconnect, "Don't report lost link to the network layer");
118
119 static int force_monitor; /* = 0 */
120 module_param(force_monitor, int, 0644);
121 MODULE_PARM_DESC(force_monitor, "Allow monitor mode for all firmware versions");
122
123 /********************************************************************/
124 /* Compile time configuration and compatibility stuff               */
125 /********************************************************************/
126
127 /* We do this this way to avoid ifdefs in the actual code */
128 #ifdef WIRELESS_SPY
129 #define SPY_NUMBER(priv)        (priv->spy_data.spy_number)
130 #else
131 #define SPY_NUMBER(priv)        0
132 #endif /* WIRELESS_SPY */
133
134 /********************************************************************/
135 /* Internal constants                                               */
136 /********************************************************************/
137
138 /* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
139 static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
140 #define ENCAPS_OVERHEAD         (sizeof(encaps_hdr) + 2)
141
142 #define ORINOCO_MIN_MTU         256
143 #define ORINOCO_MAX_MTU         (IEEE80211_DATA_LEN - ENCAPS_OVERHEAD)
144
145 #define SYMBOL_MAX_VER_LEN      (14)
146 #define USER_BAP                0
147 #define IRQ_BAP                 1
148 #define MAX_IRQLOOPS_PER_IRQ    10
149 #define MAX_IRQLOOPS_PER_JIFFY  (20000/HZ) /* Based on a guestimate of
150                                             * how many events the
151                                             * device could
152                                             * legitimately generate */
153 #define SMALL_KEY_SIZE          5
154 #define LARGE_KEY_SIZE          13
155 #define TX_NICBUF_SIZE_BUG      1585            /* Bug in Symbol firmware */
156
157 #define DUMMY_FID               0xFFFF
158
159 /*#define MAX_MULTICAST(priv)   (priv->firmware_type == FIRMWARE_TYPE_AGERE ? \
160   HERMES_MAX_MULTICAST : 0)*/
161 #define MAX_MULTICAST(priv)     (HERMES_MAX_MULTICAST)
162
163 #define ORINOCO_INTEN           (HERMES_EV_RX | HERMES_EV_ALLOC \
164                                  | HERMES_EV_TX | HERMES_EV_TXEXC \
165                                  | HERMES_EV_WTERR | HERMES_EV_INFO \
166                                  | HERMES_EV_INFDROP )
167
168 #define MAX_RID_LEN 1024
169
170 static const struct iw_handler_def orinoco_handler_def;
171 static const struct ethtool_ops orinoco_ethtool_ops;
172
173 /********************************************************************/
174 /* Data tables                                                      */
175 /********************************************************************/
176
177 /* The frequency of each channel in MHz */
178 static const long channel_frequency[] = {
179         2412, 2417, 2422, 2427, 2432, 2437, 2442,
180         2447, 2452, 2457, 2462, 2467, 2472, 2484
181 };
182 #define NUM_CHANNELS ARRAY_SIZE(channel_frequency)
183
184 /* This tables gives the actual meanings of the bitrate IDs returned
185  * by the firmware. */
186 static struct {
187         int bitrate; /* in 100s of kilobits */
188         int automatic;
189         u16 agere_txratectrl;
190         u16 intersil_txratectrl;
191 } bitrate_table[] = {
192         {110, 1,  3, 15}, /* Entry 0 is the default */
193         {10,  0,  1,  1},
194         {10,  1,  1,  1},
195         {20,  0,  2,  2},
196         {20,  1,  6,  3},
197         {55,  0,  4,  4},
198         {55,  1,  7,  7},
199         {110, 0,  5,  8},
200 };
201 #define BITRATE_TABLE_SIZE ARRAY_SIZE(bitrate_table)
202
203 /********************************************************************/
204 /* Data types                                                       */
205 /********************************************************************/
206
207 /* Beginning of the Tx descriptor, used in TxExc handling */
208 struct hermes_txexc_data {
209         struct hermes_tx_descriptor desc;
210         __le16 frame_ctl;
211         __le16 duration_id;
212         u8 addr1[ETH_ALEN];
213 } __attribute__ ((packed));
214
215 /* Rx frame header except compatibility 802.3 header */
216 struct hermes_rx_descriptor {
217         /* Control */
218         __le16 status;
219         __le32 time;
220         u8 silence;
221         u8 signal;
222         u8 rate;
223         u8 rxflow;
224         __le32 reserved;
225
226         /* 802.11 header */
227         __le16 frame_ctl;
228         __le16 duration_id;
229         u8 addr1[ETH_ALEN];
230         u8 addr2[ETH_ALEN];
231         u8 addr3[ETH_ALEN];
232         __le16 seq_ctl;
233         u8 addr4[ETH_ALEN];
234
235         /* Data length */
236         __le16 data_len;
237 } __attribute__ ((packed));
238
239 /********************************************************************/
240 /* Function prototypes                                              */
241 /********************************************************************/
242
243 static int __orinoco_program_rids(struct net_device *dev);
244 static void __orinoco_set_multicast_list(struct net_device *dev);
245
246 /********************************************************************/
247 /* Internal helper functions                                        */
248 /********************************************************************/
249
250 static inline void set_port_type(struct orinoco_private *priv)
251 {
252         switch (priv->iw_mode) {
253         case IW_MODE_INFRA:
254                 priv->port_type = 1;
255                 priv->createibss = 0;
256                 break;
257         case IW_MODE_ADHOC:
258                 if (priv->prefer_port3) {
259                         priv->port_type = 3;
260                         priv->createibss = 0;
261                 } else {
262                         priv->port_type = priv->ibss_port;
263                         priv->createibss = 1;
264                 }
265                 break;
266         case IW_MODE_MONITOR:
267                 priv->port_type = 3;
268                 priv->createibss = 0;
269                 break;
270         default:
271                 printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
272                        priv->ndev->name);
273         }
274 }
275
276 #define ORINOCO_MAX_BSS_COUNT   64
277 static int orinoco_bss_data_allocate(struct orinoco_private *priv)
278 {
279         if (priv->bss_xbss_data)
280                 return 0;
281
282         if (priv->has_ext_scan)
283                 priv->bss_xbss_data = kzalloc(ORINOCO_MAX_BSS_COUNT *
284                                               sizeof(struct xbss_element),
285                                               GFP_KERNEL);
286         else
287                 priv->bss_xbss_data = kzalloc(ORINOCO_MAX_BSS_COUNT *
288                                               sizeof(struct bss_element),
289                                               GFP_KERNEL);
290
291         if (!priv->bss_xbss_data) {
292                 printk(KERN_WARNING "Out of memory allocating beacons");
293                 return -ENOMEM;
294         }
295         return 0;
296 }
297
298 static void orinoco_bss_data_free(struct orinoco_private *priv)
299 {
300         kfree(priv->bss_xbss_data);
301         priv->bss_xbss_data = NULL;
302 }
303
304 #define PRIV_BSS        ((struct bss_element *)priv->bss_xbss_data)
305 #define PRIV_XBSS       ((struct xbss_element *)priv->bss_xbss_data)
306 static void orinoco_bss_data_init(struct orinoco_private *priv)
307 {
308         int i;
309
310         INIT_LIST_HEAD(&priv->bss_free_list);
311         INIT_LIST_HEAD(&priv->bss_list);
312         if (priv->has_ext_scan)
313                 for (i = 0; i < ORINOCO_MAX_BSS_COUNT; i++)
314                         list_add_tail(&(PRIV_XBSS[i].list),
315                                       &priv->bss_free_list);
316         else
317                 for (i = 0; i < ORINOCO_MAX_BSS_COUNT; i++)
318                         list_add_tail(&(PRIV_BSS[i].list),
319                                       &priv->bss_free_list);
320
321 }
322
323 static inline u8 *orinoco_get_ie(u8 *data, size_t len,
324                                  enum ieee80211_mfie eid)
325 {
326         u8 *p = data;
327         while ((p + 2) < (data + len)) {
328                 if (p[0] == eid)
329                         return p;
330                 p += p[1] + 2;
331         }
332         return NULL;
333 }
334
335 #define WPA_OUI_TYPE    "\x00\x50\xF2\x01"
336 #define WPA_SELECTOR_LEN 4
337 static inline u8 *orinoco_get_wpa_ie(u8 *data, size_t len)
338 {
339         u8 *p = data;
340         while ((p + 2 + WPA_SELECTOR_LEN) < (data + len)) {
341                 if ((p[0] == MFIE_TYPE_GENERIC) &&
342                     (memcmp(&p[2], WPA_OUI_TYPE, WPA_SELECTOR_LEN) == 0))
343                         return p;
344                 p += p[1] + 2;
345         }
346         return NULL;
347 }
348
349
350 /********************************************************************/
351 /* Download functionality                                           */
352 /********************************************************************/
353
354 struct fw_info {
355         char *pri_fw;
356         char *sta_fw;
357         char *ap_fw;
358         u32 pda_addr;
359         u16 pda_size;
360 };
361
362 const static struct fw_info orinoco_fw[] = {
363         { "", "agere_sta_fw.bin", "agere_ap_fw.bin", 0x00390000, 1000 },
364         { "", "prism_sta_fw.bin", "prism_ap_fw.bin", 0, 1024 },
365         { "symbol_sp24t_prim_fw", "symbol_sp24t_sec_fw", "", 0x00003100, 0x100 }
366 };
367
368 /* Structure used to access fields in FW
369  * Make sure LE decoding macros are used
370  */
371 struct orinoco_fw_header {
372         char hdr_vers[6];       /* ASCII string for header version */
373         __le16 headersize;      /* Total length of header */
374         __le32 entry_point;     /* NIC entry point */
375         __le32 blocks;          /* Number of blocks to program */
376         __le32 block_offset;    /* Offset of block data from eof header */
377         __le32 pdr_offset;      /* Offset to PDR data from eof header */
378         __le32 pri_offset;      /* Offset to primary plug data */
379         __le32 compat_offset;   /* Offset to compatibility data*/
380         char signature[0];      /* FW signature length headersize-20 */
381 } __attribute__ ((packed));
382
383 /* Download either STA or AP firmware into the card. */
384 static int
385 orinoco_dl_firmware(struct orinoco_private *priv,
386                     const struct fw_info *fw,
387                     int ap)
388 {
389         /* Plug Data Area (PDA) */
390         __le16 pda[512] = { 0 };
391
392         hermes_t *hw = &priv->hw;
393         const struct firmware *fw_entry;
394         const struct orinoco_fw_header *hdr;
395         const unsigned char *first_block;
396         const unsigned char *end;
397         const char *firmware;
398         struct net_device *dev = priv->ndev;
399         int err;
400
401         if (ap)
402                 firmware = fw->ap_fw;
403         else
404                 firmware = fw->sta_fw;
405
406         printk(KERN_DEBUG "%s: Attempting to download firmware %s\n",
407                dev->name, firmware);
408
409         /* Read current plug data */
410         err = hermes_read_pda(hw, pda, fw->pda_addr,
411                               min_t(u16, fw->pda_size, sizeof(pda)), 0);
412         printk(KERN_DEBUG "%s: Read PDA returned %d\n", dev->name, err);
413         if (err)
414                 return err;
415
416         err = request_firmware(&fw_entry, firmware, priv->dev);
417         if (err) {
418                 printk(KERN_ERR "%s: Cannot find firmware %s\n",
419                        dev->name, firmware);
420                 return -ENOENT;
421         }
422
423         hdr = (const struct orinoco_fw_header *) fw_entry->data;
424
425         /* Enable aux port to allow programming */
426         err = hermesi_program_init(hw, le32_to_cpu(hdr->entry_point));
427         printk(KERN_DEBUG "%s: Program init returned %d\n", dev->name, err);
428         if (err != 0)
429                 goto abort;
430
431         /* Program data */
432         first_block = (fw_entry->data +
433                        le16_to_cpu(hdr->headersize) +
434                        le32_to_cpu(hdr->block_offset));
435         end = fw_entry->data + fw_entry->size;
436
437         err = hermes_program(hw, first_block, end);
438         printk(KERN_DEBUG "%s: Program returned %d\n", dev->name, err);
439         if (err != 0)
440                 goto abort;
441
442         /* Update production data */
443         first_block = (fw_entry->data +
444                        le16_to_cpu(hdr->headersize) +
445                        le32_to_cpu(hdr->pdr_offset));
446
447         err = hermes_apply_pda_with_defaults(hw, first_block, pda);
448         printk(KERN_DEBUG "%s: Apply PDA returned %d\n", dev->name, err);
449         if (err)
450                 goto abort;
451
452         /* Tell card we've finished */
453         err = hermesi_program_end(hw);
454         printk(KERN_DEBUG "%s: Program end returned %d\n", dev->name, err);
455         if (err != 0)
456                 goto abort;
457
458         /* Check if we're running */
459         printk(KERN_DEBUG "%s: hermes_present returned %d\n",
460                dev->name, hermes_present(hw));
461
462 abort:
463         release_firmware(fw_entry);
464         return err;
465 }
466
467 /* End markers */
468 #define TEXT_END        0x1A            /* End of text header */
469
470 /*
471  * Process a firmware image - stop the card, load the firmware, reset
472  * the card and make sure it responds.  For the secondary firmware take
473  * care of the PDA - read it and then write it on top of the firmware.
474  */
475 static int
476 symbol_dl_image(struct orinoco_private *priv, const struct fw_info *fw,
477                 const unsigned char *image, const unsigned char *end,
478                 int secondary)
479 {
480         hermes_t *hw = &priv->hw;
481         int ret;
482         const unsigned char *ptr;
483         const unsigned char *first_block;
484
485         /* Plug Data Area (PDA) */
486         __le16 pda[256];
487
488         /* Binary block begins after the 0x1A marker */
489         ptr = image;
490         while (*ptr++ != TEXT_END);
491         first_block = ptr;
492
493         /* Read the PDA from EEPROM */
494         if (secondary) {
495                 ret = hermes_read_pda(hw, pda, fw->pda_addr, sizeof(pda), 1);
496                 if (ret)
497                         return ret;
498         }
499
500         /* Stop the firmware, so that it can be safely rewritten */
501         if (priv->stop_fw) {
502                 ret = priv->stop_fw(priv, 1);
503                 if (ret)
504                         return ret;
505         }
506
507         /* Program the adapter with new firmware */
508         ret = hermes_program(hw, first_block, end);
509         if (ret)
510                 return ret;
511
512         /* Write the PDA to the adapter */
513         if (secondary) {
514                 size_t len = hermes_blocks_length(first_block);
515                 ptr = first_block + len;
516                 ret = hermes_apply_pda(hw, ptr, pda);
517                 if (ret)
518                         return ret;
519         }
520
521         /* Run the firmware */
522         if (priv->stop_fw) {
523                 ret = priv->stop_fw(priv, 0);
524                 if (ret)
525                         return ret;
526         }
527
528         /* Reset hermes chip and make sure it responds */
529         ret = hermes_init(hw);
530
531         /* hermes_reset() should return 0 with the secondary firmware */
532         if (secondary && ret != 0)
533                 return -ENODEV;
534
535         /* And this should work with any firmware */
536         if (!hermes_present(hw))
537                 return -ENODEV;
538
539         return 0;
540 }
541
542
543 /*
544  * Download the firmware into the card, this also does a PCMCIA soft
545  * reset on the card, to make sure it's in a sane state.
546  */
547 static int
548 symbol_dl_firmware(struct orinoco_private *priv,
549                    const struct fw_info *fw)
550 {
551         struct net_device *dev = priv->ndev;
552         int ret;
553         const struct firmware *fw_entry;
554
555         if (request_firmware(&fw_entry, fw->pri_fw,
556                              priv->dev) != 0) {
557                 printk(KERN_ERR "%s: Cannot find firmware: %s\n",
558                        dev->name, fw->pri_fw);
559                 return -ENOENT;
560         }
561
562         /* Load primary firmware */
563         ret = symbol_dl_image(priv, fw, fw_entry->data,
564                               fw_entry->data + fw_entry->size, 0);
565         release_firmware(fw_entry);
566         if (ret) {
567                 printk(KERN_ERR "%s: Primary firmware download failed\n",
568                        dev->name);
569                 return ret;
570         }
571
572         if (request_firmware(&fw_entry, fw->sta_fw,
573                              priv->dev) != 0) {
574                 printk(KERN_ERR "%s: Cannot find firmware: %s\n",
575                        dev->name, fw->sta_fw);
576                 return -ENOENT;
577         }
578
579         /* Load secondary firmware */
580         ret = symbol_dl_image(priv, fw, fw_entry->data,
581                               fw_entry->data + fw_entry->size, 1);
582         release_firmware(fw_entry);
583         if (ret) {
584                 printk(KERN_ERR "%s: Secondary firmware download failed\n",
585                        dev->name);
586         }
587
588         return ret;
589 }
590
591 static int orinoco_download(struct orinoco_private *priv)
592 {
593         int err = 0;
594         /* Reload firmware */
595         switch (priv->firmware_type) {
596         case FIRMWARE_TYPE_AGERE:
597                 /* case FIRMWARE_TYPE_INTERSIL: */
598                 err = orinoco_dl_firmware(priv,
599                                           &orinoco_fw[priv->firmware_type], 0);
600                 break;
601
602         case FIRMWARE_TYPE_SYMBOL:
603                 err = symbol_dl_firmware(priv,
604                                          &orinoco_fw[priv->firmware_type]);
605                 break;
606         case FIRMWARE_TYPE_INTERSIL:
607                 break;
608         }
609         /* TODO: if we fail we probably need to reinitialise
610          * the driver */
611
612         return err;
613 }
614
615 /********************************************************************/
616 /* Device methods                                                   */
617 /********************************************************************/
618
619 static int orinoco_open(struct net_device *dev)
620 {
621         struct orinoco_private *priv = netdev_priv(dev);
622         unsigned long flags;
623         int err;
624
625         if (orinoco_lock(priv, &flags) != 0)
626                 return -EBUSY;
627
628         err = __orinoco_up(dev);
629
630         if (! err)
631                 priv->open = 1;
632
633         orinoco_unlock(priv, &flags);
634
635         return err;
636 }
637
638 static int orinoco_stop(struct net_device *dev)
639 {
640         struct orinoco_private *priv = netdev_priv(dev);
641         int err = 0;
642
643         /* We mustn't use orinoco_lock() here, because we need to be
644            able to close the interface even if hw_unavailable is set
645            (e.g. as we're released after a PC Card removal) */
646         spin_lock_irq(&priv->lock);
647
648         priv->open = 0;
649
650         err = __orinoco_down(dev);
651
652         spin_unlock_irq(&priv->lock);
653
654         return err;
655 }
656
657 static struct net_device_stats *orinoco_get_stats(struct net_device *dev)
658 {
659         struct orinoco_private *priv = netdev_priv(dev);
660         
661         return &priv->stats;
662 }
663
664 static struct iw_statistics *orinoco_get_wireless_stats(struct net_device *dev)
665 {
666         struct orinoco_private *priv = netdev_priv(dev);
667         hermes_t *hw = &priv->hw;
668         struct iw_statistics *wstats = &priv->wstats;
669         int err;
670         unsigned long flags;
671
672         if (! netif_device_present(dev)) {
673                 printk(KERN_WARNING "%s: get_wireless_stats() called while device not present\n",
674                        dev->name);
675                 return NULL; /* FIXME: Can we do better than this? */
676         }
677
678         /* If busy, return the old stats.  Returning NULL may cause
679          * the interface to disappear from /proc/net/wireless */
680         if (orinoco_lock(priv, &flags) != 0)
681                 return wstats;
682
683         /* We can't really wait for the tallies inquiry command to
684          * complete, so we just use the previous results and trigger
685          * a new tallies inquiry command for next time - Jean II */
686         /* FIXME: Really we should wait for the inquiry to come back -
687          * as it is the stats we give don't make a whole lot of sense.
688          * Unfortunately, it's not clear how to do that within the
689          * wireless extensions framework: I think we're in user
690          * context, but a lock seems to be held by the time we get in
691          * here so we're not safe to sleep here. */
692         hermes_inquire(hw, HERMES_INQ_TALLIES);
693
694         if (priv->iw_mode == IW_MODE_ADHOC) {
695                 memset(&wstats->qual, 0, sizeof(wstats->qual));
696                 /* If a spy address is defined, we report stats of the
697                  * first spy address - Jean II */
698                 if (SPY_NUMBER(priv)) {
699                         wstats->qual.qual = priv->spy_data.spy_stat[0].qual;
700                         wstats->qual.level = priv->spy_data.spy_stat[0].level;
701                         wstats->qual.noise = priv->spy_data.spy_stat[0].noise;
702                         wstats->qual.updated = priv->spy_data.spy_stat[0].updated;
703                 }
704         } else {
705                 struct {
706                         __le16 qual, signal, noise, unused;
707                 } __attribute__ ((packed)) cq;
708
709                 err = HERMES_READ_RECORD(hw, USER_BAP,
710                                          HERMES_RID_COMMSQUALITY, &cq);
711
712                 if (!err) {
713                         wstats->qual.qual = (int)le16_to_cpu(cq.qual);
714                         wstats->qual.level = (int)le16_to_cpu(cq.signal) - 0x95;
715                         wstats->qual.noise = (int)le16_to_cpu(cq.noise) - 0x95;
716                         wstats->qual.updated = 7;
717                 }
718         }
719
720         orinoco_unlock(priv, &flags);
721         return wstats;
722 }
723
724 static void orinoco_set_multicast_list(struct net_device *dev)
725 {
726         struct orinoco_private *priv = netdev_priv(dev);
727         unsigned long flags;
728
729         if (orinoco_lock(priv, &flags) != 0) {
730                 printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
731                        "called when hw_unavailable\n", dev->name);
732                 return;
733         }
734
735         __orinoco_set_multicast_list(dev);
736         orinoco_unlock(priv, &flags);
737 }
738
739 static int orinoco_change_mtu(struct net_device *dev, int new_mtu)
740 {
741         struct orinoco_private *priv = netdev_priv(dev);
742
743         if ( (new_mtu < ORINOCO_MIN_MTU) || (new_mtu > ORINOCO_MAX_MTU) )
744                 return -EINVAL;
745
746         if ( (new_mtu + ENCAPS_OVERHEAD + IEEE80211_HLEN) >
747              (priv->nicbuf_size - ETH_HLEN) )
748                 return -EINVAL;
749
750         dev->mtu = new_mtu;
751
752         return 0;
753 }
754
755 /********************************************************************/
756 /* Tx path                                                          */
757 /********************************************************************/
758
759 static int orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
760 {
761         struct orinoco_private *priv = netdev_priv(dev);
762         struct net_device_stats *stats = &priv->stats;
763         hermes_t *hw = &priv->hw;
764         int err = 0;
765         u16 txfid = priv->txfid;
766         struct ethhdr *eh;
767         int data_off;
768         int tx_control;
769         unsigned long flags;
770
771         if (! netif_running(dev)) {
772                 printk(KERN_ERR "%s: Tx on stopped device!\n",
773                        dev->name);
774                 return NETDEV_TX_BUSY;
775         }
776         
777         if (netif_queue_stopped(dev)) {
778                 printk(KERN_DEBUG "%s: Tx while transmitter busy!\n", 
779                        dev->name);
780                 return NETDEV_TX_BUSY;
781         }
782         
783         if (orinoco_lock(priv, &flags) != 0) {
784                 printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n",
785                        dev->name);
786                 return NETDEV_TX_BUSY;
787         }
788
789         if (! netif_carrier_ok(dev) || (priv->iw_mode == IW_MODE_MONITOR)) {
790                 /* Oops, the firmware hasn't established a connection,
791                    silently drop the packet (this seems to be the
792                    safest approach). */
793                 goto drop;
794         }
795
796         /* Check packet length */
797         if (skb->len < ETH_HLEN)
798                 goto drop;
799
800         eh = (struct ethhdr *)skb->data;
801
802         tx_control = HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX;
803
804         if (priv->has_alt_txcntl) {
805                 /* WPA enabled firmwares have tx_cntl at the end of
806                  * the 802.11 header.  So write zeroed descriptor and
807                  * 802.11 header at the same time
808                  */
809                 char desc[HERMES_802_3_OFFSET];
810                 __le16 *txcntl = (__le16 *) &desc[HERMES_TXCNTL2_OFFSET];
811
812                 memset(&desc, 0, sizeof(desc));
813
814                 *txcntl = cpu_to_le16(tx_control);
815                 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
816                                         txfid, 0);
817                 if (err) {
818                         if (net_ratelimit())
819                                 printk(KERN_ERR "%s: Error %d writing Tx "
820                                        "descriptor to BAP\n", dev->name, err);
821                         goto busy;
822                 }
823         } else {
824                 struct hermes_tx_descriptor desc;
825
826                 memset(&desc, 0, sizeof(desc));
827
828                 desc.tx_control = cpu_to_le16(tx_control);
829                 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
830                                         txfid, 0);
831                 if (err) {
832                         if (net_ratelimit())
833                                 printk(KERN_ERR "%s: Error %d writing Tx "
834                                        "descriptor to BAP\n", dev->name, err);
835                         goto busy;
836                 }
837
838                 /* Clear the 802.11 header and data length fields - some
839                  * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
840                  * if this isn't done. */
841                 hermes_clear_words(hw, HERMES_DATA0,
842                                    HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
843         }
844
845         /* Encapsulate Ethernet-II frames */
846         if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
847                 struct header_struct {
848                         struct ethhdr eth;      /* 802.3 header */
849                         u8 encap[6];            /* 802.2 header */
850                 } __attribute__ ((packed)) hdr;
851
852                 /* Strip destination and source from the data */
853                 skb_pull(skb, 2 * ETH_ALEN);
854                 data_off = HERMES_802_2_OFFSET + sizeof(encaps_hdr);
855
856                 /* And move them to a separate header */
857                 memcpy(&hdr.eth, eh, 2 * ETH_ALEN);
858                 hdr.eth.h_proto = htons(sizeof(encaps_hdr) + skb->len);
859                 memcpy(hdr.encap, encaps_hdr, sizeof(encaps_hdr));
860
861                 err = hermes_bap_pwrite(hw, USER_BAP, &hdr, sizeof(hdr),
862                                         txfid, HERMES_802_3_OFFSET);
863                 if (err) {
864                         if (net_ratelimit())
865                                 printk(KERN_ERR "%s: Error %d writing packet "
866                                        "header to BAP\n", dev->name, err);
867                         goto busy;
868                 }
869         } else { /* IEEE 802.3 frame */
870                 data_off = HERMES_802_3_OFFSET;
871         }
872
873         err = hermes_bap_pwrite(hw, USER_BAP, skb->data, skb->len,
874                                 txfid, data_off);
875         if (err) {
876                 printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
877                        dev->name, err);
878                 goto busy;
879         }
880
881         /* Finally, we actually initiate the send */
882         netif_stop_queue(dev);
883
884         err = hermes_docmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
885                                 txfid, NULL);
886         if (err) {
887                 netif_start_queue(dev);
888                 if (net_ratelimit())
889                         printk(KERN_ERR "%s: Error %d transmitting packet\n",
890                                 dev->name, err);
891                 goto busy;
892         }
893
894         dev->trans_start = jiffies;
895         stats->tx_bytes += data_off + skb->len;
896         goto ok;
897
898  drop:
899         stats->tx_errors++;
900         stats->tx_dropped++;
901
902  ok:
903         orinoco_unlock(priv, &flags);
904         dev_kfree_skb(skb);
905         return NETDEV_TX_OK;
906
907  busy:
908         if (err == -EIO)
909                 schedule_work(&priv->reset_work);
910         orinoco_unlock(priv, &flags);
911         return NETDEV_TX_BUSY;
912 }
913
914 static void __orinoco_ev_alloc(struct net_device *dev, hermes_t *hw)
915 {
916         struct orinoco_private *priv = netdev_priv(dev);
917         u16 fid = hermes_read_regn(hw, ALLOCFID);
918
919         if (fid != priv->txfid) {
920                 if (fid != DUMMY_FID)
921                         printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
922                                dev->name, fid);
923                 return;
924         }
925
926         hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
927 }
928
929 static void __orinoco_ev_tx(struct net_device *dev, hermes_t *hw)
930 {
931         struct orinoco_private *priv = netdev_priv(dev);
932         struct net_device_stats *stats = &priv->stats;
933
934         stats->tx_packets++;
935
936         netif_wake_queue(dev);
937
938         hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
939 }
940
941 static void __orinoco_ev_txexc(struct net_device *dev, hermes_t *hw)
942 {
943         struct orinoco_private *priv = netdev_priv(dev);
944         struct net_device_stats *stats = &priv->stats;
945         u16 fid = hermes_read_regn(hw, TXCOMPLFID);
946         u16 status;
947         struct hermes_txexc_data hdr;
948         int err = 0;
949
950         if (fid == DUMMY_FID)
951                 return; /* Nothing's really happened */
952
953         /* Read part of the frame header - we need status and addr1 */
954         err = hermes_bap_pread(hw, IRQ_BAP, &hdr,
955                                sizeof(struct hermes_txexc_data),
956                                fid, 0);
957
958         hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
959         stats->tx_errors++;
960
961         if (err) {
962                 printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
963                        "(FID=%04X error %d)\n",
964                        dev->name, fid, err);
965                 return;
966         }
967         
968         DEBUG(1, "%s: Tx error, err %d (FID=%04X)\n", dev->name,
969               err, fid);
970     
971         /* We produce a TXDROP event only for retry or lifetime
972          * exceeded, because that's the only status that really mean
973          * that this particular node went away.
974          * Other errors means that *we* screwed up. - Jean II */
975         status = le16_to_cpu(hdr.desc.status);
976         if (status & (HERMES_TXSTAT_RETRYERR | HERMES_TXSTAT_AGEDERR)) {
977                 union iwreq_data        wrqu;
978
979                 /* Copy 802.11 dest address.
980                  * We use the 802.11 header because the frame may
981                  * not be 802.3 or may be mangled...
982                  * In Ad-Hoc mode, it will be the node address.
983                  * In managed mode, it will be most likely the AP addr
984                  * User space will figure out how to convert it to
985                  * whatever it needs (IP address or else).
986                  * - Jean II */
987                 memcpy(wrqu.addr.sa_data, hdr.addr1, ETH_ALEN);
988                 wrqu.addr.sa_family = ARPHRD_ETHER;
989
990                 /* Send event to user space */
991                 wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
992         }
993
994         netif_wake_queue(dev);
995 }
996
997 static void orinoco_tx_timeout(struct net_device *dev)
998 {
999         struct orinoco_private *priv = netdev_priv(dev);
1000         struct net_device_stats *stats = &priv->stats;
1001         struct hermes *hw = &priv->hw;
1002
1003         printk(KERN_WARNING "%s: Tx timeout! "
1004                "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
1005                dev->name, hermes_read_regn(hw, ALLOCFID),
1006                hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
1007
1008         stats->tx_errors++;
1009
1010         schedule_work(&priv->reset_work);
1011 }
1012
1013 /********************************************************************/
1014 /* Rx path (data frames)                                            */
1015 /********************************************************************/
1016
1017 /* Does the frame have a SNAP header indicating it should be
1018  * de-encapsulated to Ethernet-II? */
1019 static inline int is_ethersnap(void *_hdr)
1020 {
1021         u8 *hdr = _hdr;
1022
1023         /* We de-encapsulate all packets which, a) have SNAP headers
1024          * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
1025          * and where b) the OUI of the SNAP header is 00:00:00 or
1026          * 00:00:f8 - we need both because different APs appear to use
1027          * different OUIs for some reason */
1028         return (memcmp(hdr, &encaps_hdr, 5) == 0)
1029                 && ( (hdr[5] == 0x00) || (hdr[5] == 0xf8) );
1030 }
1031
1032 static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
1033                                       int level, int noise)
1034 {
1035         struct iw_quality wstats;
1036         wstats.level = level - 0x95;
1037         wstats.noise = noise - 0x95;
1038         wstats.qual = (level > noise) ? (level - noise) : 0;
1039         wstats.updated = 7;
1040         /* Update spy records */
1041         wireless_spy_update(dev, mac, &wstats);
1042 }
1043
1044 static void orinoco_stat_gather(struct net_device *dev,
1045                                 struct sk_buff *skb,
1046                                 struct hermes_rx_descriptor *desc)
1047 {
1048         struct orinoco_private *priv = netdev_priv(dev);
1049
1050         /* Using spy support with lots of Rx packets, like in an
1051          * infrastructure (AP), will really slow down everything, because
1052          * the MAC address must be compared to each entry of the spy list.
1053          * If the user really asks for it (set some address in the
1054          * spy list), we do it, but he will pay the price.
1055          * Note that to get here, you need both WIRELESS_SPY
1056          * compiled in AND some addresses in the list !!!
1057          */
1058         /* Note : gcc will optimise the whole section away if
1059          * WIRELESS_SPY is not defined... - Jean II */
1060         if (SPY_NUMBER(priv)) {
1061                 orinoco_spy_gather(dev, skb_mac_header(skb) + ETH_ALEN,
1062                                    desc->signal, desc->silence);
1063         }
1064 }
1065
1066 /*
1067  * orinoco_rx_monitor - handle received monitor frames.
1068  *
1069  * Arguments:
1070  *      dev             network device
1071  *      rxfid           received FID
1072  *      desc            rx descriptor of the frame
1073  *
1074  * Call context: interrupt
1075  */
1076 static void orinoco_rx_monitor(struct net_device *dev, u16 rxfid,
1077                                struct hermes_rx_descriptor *desc)
1078 {
1079         u32 hdrlen = 30;        /* return full header by default */
1080         u32 datalen = 0;
1081         u16 fc;
1082         int err;
1083         int len;
1084         struct sk_buff *skb;
1085         struct orinoco_private *priv = netdev_priv(dev);
1086         struct net_device_stats *stats = &priv->stats;
1087         hermes_t *hw = &priv->hw;
1088
1089         len = le16_to_cpu(desc->data_len);
1090
1091         /* Determine the size of the header and the data */
1092         fc = le16_to_cpu(desc->frame_ctl);
1093         switch (fc & IEEE80211_FCTL_FTYPE) {
1094         case IEEE80211_FTYPE_DATA:
1095                 if ((fc & IEEE80211_FCTL_TODS)
1096                     && (fc & IEEE80211_FCTL_FROMDS))
1097                         hdrlen = 30;
1098                 else
1099                         hdrlen = 24;
1100                 datalen = len;
1101                 break;
1102         case IEEE80211_FTYPE_MGMT:
1103                 hdrlen = 24;
1104                 datalen = len;
1105                 break;
1106         case IEEE80211_FTYPE_CTL:
1107                 switch (fc & IEEE80211_FCTL_STYPE) {
1108                 case IEEE80211_STYPE_PSPOLL:
1109                 case IEEE80211_STYPE_RTS:
1110                 case IEEE80211_STYPE_CFEND:
1111                 case IEEE80211_STYPE_CFENDACK:
1112                         hdrlen = 16;
1113                         break;
1114                 case IEEE80211_STYPE_CTS:
1115                 case IEEE80211_STYPE_ACK:
1116                         hdrlen = 10;
1117                         break;
1118                 }
1119                 break;
1120         default:
1121                 /* Unknown frame type */
1122                 break;
1123         }
1124
1125         /* sanity check the length */
1126         if (datalen > IEEE80211_DATA_LEN + 12) {
1127                 printk(KERN_DEBUG "%s: oversized monitor frame, "
1128                        "data length = %d\n", dev->name, datalen);
1129                 stats->rx_length_errors++;
1130                 goto update_stats;
1131         }
1132
1133         skb = dev_alloc_skb(hdrlen + datalen);
1134         if (!skb) {
1135                 printk(KERN_WARNING "%s: Cannot allocate skb for monitor frame\n",
1136                        dev->name);
1137                 goto update_stats;
1138         }
1139
1140         /* Copy the 802.11 header to the skb */
1141         memcpy(skb_put(skb, hdrlen), &(desc->frame_ctl), hdrlen);
1142         skb_reset_mac_header(skb);
1143
1144         /* If any, copy the data from the card to the skb */
1145         if (datalen > 0) {
1146                 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, datalen),
1147                                        ALIGN(datalen, 2), rxfid,
1148                                        HERMES_802_2_OFFSET);
1149                 if (err) {
1150                         printk(KERN_ERR "%s: error %d reading monitor frame\n",
1151                                dev->name, err);
1152                         goto drop;
1153                 }
1154         }
1155
1156         skb->dev = dev;
1157         skb->ip_summed = CHECKSUM_NONE;
1158         skb->pkt_type = PACKET_OTHERHOST;
1159         skb->protocol = __constant_htons(ETH_P_802_2);
1160         
1161         dev->last_rx = jiffies;
1162         stats->rx_packets++;
1163         stats->rx_bytes += skb->len;
1164
1165         netif_rx(skb);
1166         return;
1167
1168  drop:
1169         dev_kfree_skb_irq(skb);
1170  update_stats:
1171         stats->rx_errors++;
1172         stats->rx_dropped++;
1173 }
1174
1175 static void __orinoco_ev_rx(struct net_device *dev, hermes_t *hw)
1176 {
1177         struct orinoco_private *priv = netdev_priv(dev);
1178         struct net_device_stats *stats = &priv->stats;
1179         struct iw_statistics *wstats = &priv->wstats;
1180         struct sk_buff *skb = NULL;
1181         u16 rxfid, status;
1182         int length;
1183         struct hermes_rx_descriptor *desc;
1184         struct orinoco_rx_data *rx_data;
1185         int err;
1186
1187         desc = kmalloc(sizeof(*desc), GFP_ATOMIC);
1188         if (!desc) {
1189                 printk(KERN_WARNING
1190                        "%s: Can't allocate space for RX descriptor\n",
1191                        dev->name);
1192                 goto update_stats;
1193         }
1194
1195         rxfid = hermes_read_regn(hw, RXFID);
1196
1197         err = hermes_bap_pread(hw, IRQ_BAP, desc, sizeof(*desc),
1198                                rxfid, 0);
1199         if (err) {
1200                 printk(KERN_ERR "%s: error %d reading Rx descriptor. "
1201                        "Frame dropped.\n", dev->name, err);
1202                 goto update_stats;
1203         }
1204
1205         status = le16_to_cpu(desc->status);
1206
1207         if (status & HERMES_RXSTAT_BADCRC) {
1208                 DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n",
1209                       dev->name);
1210                 stats->rx_crc_errors++;
1211                 goto update_stats;
1212         }
1213
1214         /* Handle frames in monitor mode */
1215         if (priv->iw_mode == IW_MODE_MONITOR) {
1216                 orinoco_rx_monitor(dev, rxfid, desc);
1217                 goto out;
1218         }
1219
1220         if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
1221                 DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
1222                       dev->name);
1223                 wstats->discard.code++;
1224                 goto update_stats;
1225         }
1226
1227         length = le16_to_cpu(desc->data_len);
1228
1229         /* Sanity checks */
1230         if (length < 3) { /* No for even an 802.2 LLC header */
1231                 /* At least on Symbol firmware with PCF we get quite a
1232                    lot of these legitimately - Poll frames with no
1233                    data. */
1234                 goto out;
1235         }
1236         if (length > IEEE80211_DATA_LEN) {
1237                 printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
1238                        dev->name, length);
1239                 stats->rx_length_errors++;
1240                 goto update_stats;
1241         }
1242
1243         /* We need space for the packet data itself, plus an ethernet
1244            header, plus 2 bytes so we can align the IP header on a
1245            32bit boundary, plus 1 byte so we can read in odd length
1246            packets from the card, which has an IO granularity of 16
1247            bits */  
1248         skb = dev_alloc_skb(length+ETH_HLEN+2+1);
1249         if (!skb) {
1250                 printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
1251                        dev->name);
1252                 goto update_stats;
1253         }
1254
1255         /* We'll prepend the header, so reserve space for it.  The worst
1256            case is no decapsulation, when 802.3 header is prepended and
1257            nothing is removed.  2 is for aligning the IP header.  */
1258         skb_reserve(skb, ETH_HLEN + 2);
1259
1260         err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, length),
1261                                ALIGN(length, 2), rxfid,
1262                                HERMES_802_2_OFFSET);
1263         if (err) {
1264                 printk(KERN_ERR "%s: error %d reading frame. "
1265                        "Frame dropped.\n", dev->name, err);
1266                 goto drop;
1267         }
1268
1269         /* Add desc and skb to rx queue */
1270         rx_data = kzalloc(sizeof(*rx_data), GFP_ATOMIC);
1271         if (!rx_data) {
1272                 printk(KERN_WARNING "%s: Can't allocate RX packet\n",
1273                         dev->name);
1274                 goto drop;
1275         }
1276         rx_data->desc = desc;
1277         rx_data->skb = skb;
1278         list_add_tail(&rx_data->list, &priv->rx_list);
1279         tasklet_schedule(&priv->rx_tasklet);
1280
1281         return;
1282
1283 drop:
1284         dev_kfree_skb_irq(skb);
1285 update_stats:
1286         stats->rx_errors++;
1287         stats->rx_dropped++;
1288 out:
1289         kfree(desc);
1290 }
1291
1292 static void orinoco_rx(struct net_device *dev,
1293                        struct hermes_rx_descriptor *desc,
1294                        struct sk_buff *skb)
1295 {
1296         struct orinoco_private *priv = netdev_priv(dev);
1297         struct net_device_stats *stats = &priv->stats;
1298         u16 status, fc;
1299         int length;
1300         struct ethhdr *hdr;
1301
1302         status = le16_to_cpu(desc->status);
1303         length = le16_to_cpu(desc->data_len);
1304         fc = le16_to_cpu(desc->frame_ctl);
1305
1306         /* Handle decapsulation
1307          * In most cases, the firmware tell us about SNAP frames.
1308          * For some reason, the SNAP frames sent by LinkSys APs
1309          * are not properly recognised by most firmwares.
1310          * So, check ourselves */
1311         if (length >= ENCAPS_OVERHEAD &&
1312             (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
1313              ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
1314              is_ethersnap(skb->data))) {
1315                 /* These indicate a SNAP within 802.2 LLC within
1316                    802.11 frame which we'll need to de-encapsulate to
1317                    the original EthernetII frame. */
1318                 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN - ENCAPS_OVERHEAD);
1319         } else {
1320                 /* 802.3 frame - prepend 802.3 header as is */
1321                 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN);
1322                 hdr->h_proto = htons(length);
1323         }
1324         memcpy(hdr->h_dest, desc->addr1, ETH_ALEN);
1325         if (fc & IEEE80211_FCTL_FROMDS)
1326                 memcpy(hdr->h_source, desc->addr3, ETH_ALEN);
1327         else
1328                 memcpy(hdr->h_source, desc->addr2, ETH_ALEN);
1329
1330         dev->last_rx = jiffies;
1331         skb->protocol = eth_type_trans(skb, dev);
1332         skb->ip_summed = CHECKSUM_NONE;
1333         if (fc & IEEE80211_FCTL_TODS)
1334                 skb->pkt_type = PACKET_OTHERHOST;
1335         
1336         /* Process the wireless stats if needed */
1337         orinoco_stat_gather(dev, skb, desc);
1338
1339         /* Pass the packet to the networking stack */
1340         netif_rx(skb);
1341         stats->rx_packets++;
1342         stats->rx_bytes += length;
1343
1344         return;
1345 }
1346
1347 static void orinoco_rx_isr_tasklet(unsigned long data)
1348 {
1349         struct net_device *dev = (struct net_device *) data;
1350         struct orinoco_private *priv = netdev_priv(dev);
1351         struct orinoco_rx_data *rx_data, *temp;
1352         struct hermes_rx_descriptor *desc;
1353         struct sk_buff *skb;
1354
1355         /* extract desc and skb from queue */
1356         list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
1357                 desc = rx_data->desc;
1358                 skb = rx_data->skb;
1359                 list_del(&rx_data->list);
1360                 kfree(rx_data);
1361
1362                 orinoco_rx(dev, desc, skb);
1363
1364                 kfree(desc);
1365         }
1366 }
1367
1368 /********************************************************************/
1369 /* Rx path (info frames)                                            */
1370 /********************************************************************/
1371
1372 static void print_linkstatus(struct net_device *dev, u16 status)
1373 {
1374         char * s;
1375
1376         if (suppress_linkstatus)
1377                 return;
1378
1379         switch (status) {
1380         case HERMES_LINKSTATUS_NOT_CONNECTED:
1381                 s = "Not Connected";
1382                 break;
1383         case HERMES_LINKSTATUS_CONNECTED:
1384                 s = "Connected";
1385                 break;
1386         case HERMES_LINKSTATUS_DISCONNECTED:
1387                 s = "Disconnected";
1388                 break;
1389         case HERMES_LINKSTATUS_AP_CHANGE:
1390                 s = "AP Changed";
1391                 break;
1392         case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
1393                 s = "AP Out of Range";
1394                 break;
1395         case HERMES_LINKSTATUS_AP_IN_RANGE:
1396                 s = "AP In Range";
1397                 break;
1398         case HERMES_LINKSTATUS_ASSOC_FAILED:
1399                 s = "Association Failed";
1400                 break;
1401         default:
1402                 s = "UNKNOWN";
1403         }
1404         
1405         printk(KERN_INFO "%s: New link status: %s (%04x)\n",
1406                dev->name, s, status);
1407 }
1408
1409 /* Search scan results for requested BSSID, join it if found */
1410 static void orinoco_join_ap(struct work_struct *work)
1411 {
1412         struct orinoco_private *priv =
1413                 container_of(work, struct orinoco_private, join_work);
1414         struct net_device *dev = priv->ndev;
1415         struct hermes *hw = &priv->hw;
1416         int err;
1417         unsigned long flags;
1418         struct join_req {
1419                 u8 bssid[ETH_ALEN];
1420                 __le16 channel;
1421         } __attribute__ ((packed)) req;
1422         const int atom_len = offsetof(struct prism2_scan_apinfo, atim);
1423         struct prism2_scan_apinfo *atom = NULL;
1424         int offset = 4;
1425         int found = 0;
1426         u8 *buf;
1427         u16 len;
1428
1429         /* Allocate buffer for scan results */
1430         buf = kmalloc(MAX_SCAN_LEN, GFP_KERNEL);
1431         if (! buf)
1432                 return;
1433
1434         if (orinoco_lock(priv, &flags) != 0)
1435                 goto fail_lock;
1436
1437         /* Sanity checks in case user changed something in the meantime */
1438         if (! priv->bssid_fixed)
1439                 goto out;
1440
1441         if (strlen(priv->desired_essid) == 0)
1442                 goto out;
1443
1444         /* Read scan results from the firmware */
1445         err = hermes_read_ltv(hw, USER_BAP,
1446                               HERMES_RID_SCANRESULTSTABLE,
1447                               MAX_SCAN_LEN, &len, buf);
1448         if (err) {
1449                 printk(KERN_ERR "%s: Cannot read scan results\n",
1450                        dev->name);
1451                 goto out;
1452         }
1453
1454         len = HERMES_RECLEN_TO_BYTES(len);
1455
1456         /* Go through the scan results looking for the channel of the AP
1457          * we were requested to join */
1458         for (; offset + atom_len <= len; offset += atom_len) {
1459                 atom = (struct prism2_scan_apinfo *) (buf + offset);
1460                 if (memcmp(&atom->bssid, priv->desired_bssid, ETH_ALEN) == 0) {
1461                         found = 1;
1462                         break;
1463                 }
1464         }
1465
1466         if (! found) {
1467                 DEBUG(1, "%s: Requested AP not found in scan results\n",
1468                       dev->name);
1469                 goto out;
1470         }
1471
1472         memcpy(req.bssid, priv->desired_bssid, ETH_ALEN);
1473         req.channel = atom->channel;    /* both are little-endian */
1474         err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNFJOINREQUEST,
1475                                   &req);
1476         if (err)
1477                 printk(KERN_ERR "%s: Error issuing join request\n", dev->name);
1478
1479  out:
1480         orinoco_unlock(priv, &flags);
1481
1482  fail_lock:
1483         kfree(buf);
1484 }
1485
1486 /* Send new BSSID to userspace */
1487 static void orinoco_send_bssid_wevent(struct orinoco_private *priv)
1488 {
1489         struct net_device *dev = priv->ndev;
1490         struct hermes *hw = &priv->hw;
1491         union iwreq_data wrqu;
1492         int err;
1493
1494         err = hermes_read_ltv(hw, IRQ_BAP, HERMES_RID_CURRENTBSSID,
1495                               ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
1496         if (err != 0)
1497                 return;
1498
1499         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1500
1501         /* Send event to user space */
1502         wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
1503 }
1504
1505 static void orinoco_send_assocreqie_wevent(struct orinoco_private *priv)
1506 {
1507         struct net_device *dev = priv->ndev;
1508         struct hermes *hw = &priv->hw;
1509         union iwreq_data wrqu;
1510         int err;
1511         u8 buf[88];
1512         u8 *ie;
1513
1514         if (!priv->has_wpa)
1515                 return;
1516
1517         err = hermes_read_ltv(hw, IRQ_BAP, HERMES_RID_CURRENT_ASSOC_REQ_INFO,
1518                               sizeof(buf), NULL, &buf);
1519         if (err != 0)
1520                 return;
1521
1522         ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1523         if (ie) {
1524                 int rem = sizeof(buf) - (ie - &buf[0]);
1525                 wrqu.data.length = ie[1] + 2;
1526                 if (wrqu.data.length > rem)
1527                         wrqu.data.length = rem;
1528
1529                 if (wrqu.data.length)
1530                         /* Send event to user space */
1531                         wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, ie);
1532         }
1533 }
1534
1535 static void orinoco_send_assocrespie_wevent(struct orinoco_private *priv)
1536 {
1537         struct net_device *dev = priv->ndev;
1538         struct hermes *hw = &priv->hw;
1539         union iwreq_data wrqu;
1540         int err;
1541         u8 buf[88]; /* TODO: verify max size or IW_GENERIC_IE_MAX */
1542         u8 *ie;
1543
1544         if (!priv->has_wpa)
1545                 return;
1546
1547         err = hermes_read_ltv(hw, IRQ_BAP, HERMES_RID_CURRENT_ASSOC_RESP_INFO,
1548                               sizeof(buf), NULL, &buf);
1549         if (err != 0)
1550                 return;
1551
1552         ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1553         if (ie) {
1554                 int rem = sizeof(buf) - (ie - &buf[0]);
1555                 wrqu.data.length = ie[1] + 2;
1556                 if (wrqu.data.length > rem)
1557                         wrqu.data.length = rem;
1558
1559                 if (wrqu.data.length)
1560                         /* Send event to user space */
1561                         wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, ie);
1562         }
1563 }
1564
1565 static void orinoco_send_wevents(struct work_struct *work)
1566 {
1567         struct orinoco_private *priv =
1568                 container_of(work, struct orinoco_private, wevent_work);
1569         unsigned long flags;
1570
1571         if (orinoco_lock(priv, &flags) != 0)
1572                 return;
1573
1574         orinoco_send_assocreqie_wevent(priv);
1575         orinoco_send_assocrespie_wevent(priv);
1576         orinoco_send_bssid_wevent(priv);
1577
1578         orinoco_unlock(priv, &flags);
1579 }
1580
1581 static inline void orinoco_clear_scan_results(struct orinoco_private *priv,
1582                                               unsigned long scan_age)
1583 {
1584         if (priv->has_ext_scan) {
1585                 struct xbss_element *bss;
1586                 struct xbss_element *tmp_bss;
1587
1588                 /* Blow away current list of scan results */
1589                 list_for_each_entry_safe(bss, tmp_bss, &priv->bss_list, list) {
1590                         if (!scan_age ||
1591                             time_after(jiffies, bss->last_scanned + scan_age)) {
1592                                 list_move_tail(&bss->list,
1593                                                &priv->bss_free_list);
1594                                 /* Don't blow away ->list, just BSS data */
1595                                 memset(&bss->bss, 0, sizeof(bss->bss));
1596                                 bss->last_scanned = 0;
1597                         }
1598                 }
1599         } else {
1600                 struct bss_element *bss;
1601                 struct bss_element *tmp_bss;
1602
1603                 /* Blow away current list of scan results */
1604                 list_for_each_entry_safe(bss, tmp_bss, &priv->bss_list, list) {
1605                         if (!scan_age ||
1606                             time_after(jiffies, bss->last_scanned + scan_age)) {
1607                                 list_move_tail(&bss->list,
1608                                                &priv->bss_free_list);
1609                                 /* Don't blow away ->list, just BSS data */
1610                                 memset(&bss->bss, 0, sizeof(bss->bss));
1611                                 bss->last_scanned = 0;
1612                         }
1613                 }
1614         }
1615 }
1616
1617 static void orinoco_add_ext_scan_result(struct orinoco_private *priv,
1618                                         struct agere_ext_scan_info *atom)
1619 {
1620         struct xbss_element *bss = NULL;
1621         int found = 0;
1622
1623         /* Try to update an existing bss first */
1624         list_for_each_entry(bss, &priv->bss_list, list) {
1625                 if (compare_ether_addr(bss->bss.bssid, atom->bssid))
1626                         continue;
1627                 /* ESSID lengths */
1628                 if (bss->bss.data[1] != atom->data[1])
1629                         continue;
1630                 if (memcmp(&bss->bss.data[2], &atom->data[2],
1631                            atom->data[1]))
1632                         continue;
1633                 found = 1;
1634                 break;
1635         }
1636
1637         /* Grab a bss off the free list */
1638         if (!found && !list_empty(&priv->bss_free_list)) {
1639                 bss = list_entry(priv->bss_free_list.next,
1640                                  struct xbss_element, list);
1641                 list_del(priv->bss_free_list.next);
1642
1643                 list_add_tail(&bss->list, &priv->bss_list);
1644         }
1645
1646         if (bss) {
1647                 /* Always update the BSS to get latest beacon info */
1648                 memcpy(&bss->bss, atom, sizeof(bss->bss));
1649                 bss->last_scanned = jiffies;
1650         }
1651 }
1652
1653 static int orinoco_process_scan_results(struct net_device *dev,
1654                                         unsigned char *buf,
1655                                         int len)
1656 {
1657         struct orinoco_private *priv = netdev_priv(dev);
1658         int                     offset;         /* In the scan data */
1659         union hermes_scan_info *atom;
1660         int                     atom_len;
1661
1662         switch (priv->firmware_type) {
1663         case FIRMWARE_TYPE_AGERE:
1664                 atom_len = sizeof(struct agere_scan_apinfo);
1665                 offset = 0;
1666                 break;
1667         case FIRMWARE_TYPE_SYMBOL:
1668                 /* Lack of documentation necessitates this hack.
1669                  * Different firmwares have 68 or 76 byte long atoms.
1670                  * We try modulo first.  If the length divides by both,
1671                  * we check what would be the channel in the second
1672                  * frame for a 68-byte atom.  76-byte atoms have 0 there.
1673                  * Valid channel cannot be 0.  */
1674                 if (len % 76)
1675                         atom_len = 68;
1676                 else if (len % 68)
1677                         atom_len = 76;
1678                 else if (len >= 1292 && buf[68] == 0)
1679                         atom_len = 76;
1680                 else
1681                         atom_len = 68;
1682                 offset = 0;
1683                 break;
1684         case FIRMWARE_TYPE_INTERSIL:
1685                 offset = 4;
1686                 if (priv->has_hostscan) {
1687                         atom_len = le16_to_cpup((__le16 *)buf);
1688                         /* Sanity check for atom_len */
1689                         if (atom_len < sizeof(struct prism2_scan_apinfo)) {
1690                                 printk(KERN_ERR "%s: Invalid atom_len in scan "
1691                                        "data: %d\n", dev->name, atom_len);
1692                                 return -EIO;
1693                         }
1694                 } else
1695                         atom_len = offsetof(struct prism2_scan_apinfo, atim);
1696                 break;
1697         default:
1698                 return -EOPNOTSUPP;
1699         }
1700
1701         /* Check that we got an whole number of atoms */
1702         if ((len - offset) % atom_len) {
1703                 printk(KERN_ERR "%s: Unexpected scan data length %d, "
1704                        "atom_len %d, offset %d\n", dev->name, len,
1705                        atom_len, offset);
1706                 return -EIO;
1707         }
1708
1709         orinoco_clear_scan_results(priv, msecs_to_jiffies(15000));
1710
1711         /* Read the entries one by one */
1712         for (; offset + atom_len <= len; offset += atom_len) {
1713                 int found = 0;
1714                 struct bss_element *bss = NULL;
1715
1716                 /* Get next atom */
1717                 atom = (union hermes_scan_info *) (buf + offset);
1718
1719                 /* Try to update an existing bss first */
1720                 list_for_each_entry(bss, &priv->bss_list, list) {
1721                         if (compare_ether_addr(bss->bss.a.bssid, atom->a.bssid))
1722                                 continue;
1723                         if (le16_to_cpu(bss->bss.a.essid_len) !=
1724                               le16_to_cpu(atom->a.essid_len))
1725                                 continue;
1726                         if (memcmp(bss->bss.a.essid, atom->a.essid,
1727                               le16_to_cpu(atom->a.essid_len)))
1728                                 continue;
1729                         found = 1;
1730                         break;
1731                 }
1732
1733                 /* Grab a bss off the free list */
1734                 if (!found && !list_empty(&priv->bss_free_list)) {
1735                         bss = list_entry(priv->bss_free_list.next,
1736                                          struct bss_element, list);
1737                         list_del(priv->bss_free_list.next);
1738
1739                         list_add_tail(&bss->list, &priv->bss_list);
1740                 }
1741
1742                 if (bss) {
1743                         /* Always update the BSS to get latest beacon info */
1744                         memcpy(&bss->bss, atom, sizeof(bss->bss));
1745                         bss->last_scanned = jiffies;
1746                 }
1747         }
1748
1749         return 0;
1750 }
1751
1752 static void __orinoco_ev_info(struct net_device *dev, hermes_t *hw)
1753 {
1754         struct orinoco_private *priv = netdev_priv(dev);
1755         u16 infofid;
1756         struct {
1757                 __le16 len;
1758                 __le16 type;
1759         } __attribute__ ((packed)) info;
1760         int len, type;
1761         int err;
1762
1763         /* This is an answer to an INQUIRE command that we did earlier,
1764          * or an information "event" generated by the card
1765          * The controller return to us a pseudo frame containing
1766          * the information in question - Jean II */
1767         infofid = hermes_read_regn(hw, INFOFID);
1768
1769         /* Read the info frame header - don't try too hard */
1770         err = hermes_bap_pread(hw, IRQ_BAP, &info, sizeof(info),
1771                                infofid, 0);
1772         if (err) {
1773                 printk(KERN_ERR "%s: error %d reading info frame. "
1774                        "Frame dropped.\n", dev->name, err);
1775                 return;
1776         }
1777         
1778         len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
1779         type = le16_to_cpu(info.type);
1780
1781         switch (type) {
1782         case HERMES_INQ_TALLIES: {
1783                 struct hermes_tallies_frame tallies;
1784                 struct iw_statistics *wstats = &priv->wstats;
1785                 
1786                 if (len > sizeof(tallies)) {
1787                         printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
1788                                dev->name, len);
1789                         len = sizeof(tallies);
1790                 }
1791                 
1792                 err = hermes_bap_pread(hw, IRQ_BAP, &tallies, len,
1793                                        infofid, sizeof(info));
1794                 if (err)
1795                         break;
1796                 
1797                 /* Increment our various counters */
1798                 /* wstats->discard.nwid - no wrong BSSID stuff */
1799                 wstats->discard.code +=
1800                         le16_to_cpu(tallies.RxWEPUndecryptable);
1801                 if (len == sizeof(tallies))  
1802                         wstats->discard.code +=
1803                                 le16_to_cpu(tallies.RxDiscards_WEPICVError) +
1804                                 le16_to_cpu(tallies.RxDiscards_WEPExcluded);
1805                 wstats->discard.misc +=
1806                         le16_to_cpu(tallies.TxDiscardsWrongSA);
1807                 wstats->discard.fragment +=
1808                         le16_to_cpu(tallies.RxMsgInBadMsgFragments);
1809                 wstats->discard.retries +=
1810                         le16_to_cpu(tallies.TxRetryLimitExceeded);
1811                 /* wstats->miss.beacon - no match */
1812         }
1813         break;
1814         case HERMES_INQ_LINKSTATUS: {
1815                 struct hermes_linkstatus linkstatus;
1816                 u16 newstatus;
1817                 int connected;
1818
1819                 if (priv->iw_mode == IW_MODE_MONITOR)
1820                         break;
1821
1822                 if (len != sizeof(linkstatus)) {
1823                         printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
1824                                dev->name, len);
1825                         break;
1826                 }
1827
1828                 err = hermes_bap_pread(hw, IRQ_BAP, &linkstatus, len,
1829                                        infofid, sizeof(info));
1830                 if (err)
1831                         break;
1832                 newstatus = le16_to_cpu(linkstatus.linkstatus);
1833
1834                 /* Symbol firmware uses "out of range" to signal that
1835                  * the hostscan frame can be requested.  */
1836                 if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
1837                     priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
1838                     priv->has_hostscan && priv->scan_inprogress) {
1839                         hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
1840                         break;
1841                 }
1842
1843                 connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
1844                         || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
1845                         || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
1846
1847                 if (connected)
1848                         netif_carrier_on(dev);
1849                 else if (!ignore_disconnect)
1850                         netif_carrier_off(dev);
1851
1852                 if (newstatus != priv->last_linkstatus) {
1853                         priv->last_linkstatus = newstatus;
1854                         print_linkstatus(dev, newstatus);
1855                         /* The info frame contains only one word which is the
1856                          * status (see hermes.h). The status is pretty boring
1857                          * in itself, that's why we export the new BSSID...
1858                          * Jean II */
1859                         schedule_work(&priv->wevent_work);
1860                 }
1861         }
1862         break;
1863         case HERMES_INQ_SCAN:
1864                 if (!priv->scan_inprogress && priv->bssid_fixed &&
1865                     priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
1866                         schedule_work(&priv->join_work);
1867                         break;
1868                 }
1869                 /* fall through */
1870         case HERMES_INQ_HOSTSCAN:
1871         case HERMES_INQ_HOSTSCAN_SYMBOL: {
1872                 /* Result of a scanning. Contains information about
1873                  * cells in the vicinity - Jean II */
1874                 union iwreq_data        wrqu;
1875                 unsigned char *buf;
1876
1877                 /* Scan is no longer in progress */
1878                 priv->scan_inprogress = 0;
1879
1880                 /* Sanity check */
1881                 if (len > 4096) {
1882                         printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
1883                                dev->name, len);
1884                         break;
1885                 }
1886
1887                 /* Allocate buffer for results */
1888                 buf = kmalloc(len, GFP_ATOMIC);
1889                 if (buf == NULL)
1890                         /* No memory, so can't printk()... */
1891                         break;
1892
1893                 /* Read scan data */
1894                 err = hermes_bap_pread(hw, IRQ_BAP, (void *) buf, len,
1895                                        infofid, sizeof(info));
1896                 if (err) {
1897                         kfree(buf);
1898                         break;
1899                 }
1900
1901 #ifdef ORINOCO_DEBUG
1902                 {
1903                         int     i;
1904                         printk(KERN_DEBUG "Scan result [%02X", buf[0]);
1905                         for(i = 1; i < (len * 2); i++)
1906                                 printk(":%02X", buf[i]);
1907                         printk("]\n");
1908                 }
1909 #endif  /* ORINOCO_DEBUG */
1910
1911                 if (orinoco_process_scan_results(dev, buf, len) == 0) {
1912                         /* Send an empty event to user space.
1913                          * We don't send the received data on the event because
1914                          * it would require us to do complex transcoding, and
1915                          * we want to minimise the work done in the irq handler
1916                          * Use a request to extract the data - Jean II */
1917                         wrqu.data.length = 0;
1918                         wrqu.data.flags = 0;
1919                         wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
1920                 }
1921                 kfree(buf);
1922         }
1923         break;
1924         case HERMES_INQ_CHANNELINFO:
1925         {
1926                 struct agere_ext_scan_info *bss;
1927
1928                 if (!priv->scan_inprogress) {
1929                         printk(KERN_DEBUG "%s: Got chaninfo without scan, "
1930                                "len=%d\n", dev->name, len);
1931                         break;
1932                 }
1933
1934                 /* An empty result indicates that the scan is complete */
1935                 if (len == 0) {
1936                         union iwreq_data        wrqu;
1937
1938                         /* Scan is no longer in progress */
1939                         priv->scan_inprogress = 0;
1940
1941                         wrqu.data.length = 0;
1942                         wrqu.data.flags = 0;
1943                         wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
1944                         break;
1945                 }
1946
1947                 /* Sanity check */
1948                 else if (len > sizeof(*bss)) {
1949                         printk(KERN_WARNING
1950                                "%s: Ext scan results too large (%d bytes). "
1951                                "Truncating results to %zd bytes.\n",
1952                                dev->name, len, sizeof(*bss));
1953                         len = sizeof(*bss);
1954                 } else if (len < (offsetof(struct agere_ext_scan_info,
1955                                            data) + 2)) {
1956                         /* Drop this result now so we don't have to
1957                          * keep checking later */
1958                         printk(KERN_WARNING
1959                                "%s: Ext scan results too short (%d bytes)\n",
1960                                dev->name, len);
1961                         break;
1962                 }
1963
1964                 bss = kmalloc(sizeof(*bss), GFP_ATOMIC);
1965                 if (bss == NULL)
1966                         break;
1967
1968                 /* Read scan data */
1969                 err = hermes_bap_pread(hw, IRQ_BAP, (void *) bss, len,
1970                                        infofid, sizeof(info));
1971                 if (err) {
1972                         kfree(bss);
1973                         break;
1974                 }
1975
1976                 orinoco_add_ext_scan_result(priv, bss);
1977
1978                 kfree(bss);
1979                 break;
1980         }
1981         case HERMES_INQ_SEC_STAT_AGERE:
1982                 /* Security status (Agere specific) */
1983                 /* Ignore this frame for now */
1984                 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
1985                         break;
1986                 /* fall through */
1987         default:
1988                 printk(KERN_DEBUG "%s: Unknown information frame received: "
1989                        "type 0x%04x, length %d\n", dev->name, type, len);
1990                 /* We don't actually do anything about it */
1991                 break;
1992         }
1993 }
1994
1995 static void __orinoco_ev_infdrop(struct net_device *dev, hermes_t *hw)
1996 {
1997         if (net_ratelimit())
1998                 printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
1999 }
2000
2001 /********************************************************************/
2002 /* Internal hardware control routines                               */
2003 /********************************************************************/
2004
2005 int __orinoco_up(struct net_device *dev)
2006 {
2007         struct orinoco_private *priv = netdev_priv(dev);
2008         struct hermes *hw = &priv->hw;
2009         int err;
2010
2011         netif_carrier_off(dev); /* just to make sure */
2012
2013         err = __orinoco_program_rids(dev);
2014         if (err) {
2015                 printk(KERN_ERR "%s: Error %d configuring card\n",
2016                        dev->name, err);
2017                 return err;
2018         }
2019
2020         /* Fire things up again */
2021         hermes_set_irqmask(hw, ORINOCO_INTEN);
2022         err = hermes_enable_port(hw, 0);
2023         if (err) {
2024                 printk(KERN_ERR "%s: Error %d enabling MAC port\n",
2025                        dev->name, err);
2026                 return err;
2027         }
2028
2029         netif_start_queue(dev);
2030
2031         return 0;
2032 }
2033
2034 int __orinoco_down(struct net_device *dev)
2035 {
2036         struct orinoco_private *priv = netdev_priv(dev);
2037         struct hermes *hw = &priv->hw;
2038         int err;
2039
2040         netif_stop_queue(dev);
2041
2042         if (! priv->hw_unavailable) {
2043                 if (! priv->broken_disableport) {
2044                         err = hermes_disable_port(hw, 0);
2045                         if (err) {
2046                                 /* Some firmwares (e.g. Intersil 1.3.x) seem
2047                                  * to have problems disabling the port, oh
2048                                  * well, too bad. */
2049                                 printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
2050                                        dev->name, err);
2051                                 priv->broken_disableport = 1;
2052                         }
2053                 }
2054                 hermes_set_irqmask(hw, 0);
2055                 hermes_write_regn(hw, EVACK, 0xffff);
2056         }
2057         
2058         /* firmware will have to reassociate */
2059         netif_carrier_off(dev);
2060         priv->last_linkstatus = 0xffff;
2061
2062         return 0;
2063 }
2064
2065 static int orinoco_allocate_fid(struct net_device *dev)
2066 {
2067         struct orinoco_private *priv = netdev_priv(dev);
2068         struct hermes *hw = &priv->hw;
2069         int err;
2070
2071         err = hermes_allocate(hw, priv->nicbuf_size, &priv->txfid);
2072         if (err == -EIO && priv->nicbuf_size > TX_NICBUF_SIZE_BUG) {
2073                 /* Try workaround for old Symbol firmware bug */
2074                 printk(KERN_WARNING "%s: firmware ALLOC bug detected "
2075                        "(old Symbol firmware?). Trying to work around... ",
2076                        dev->name);
2077                 
2078                 priv->nicbuf_size = TX_NICBUF_SIZE_BUG;
2079                 err = hermes_allocate(hw, priv->nicbuf_size, &priv->txfid);
2080                 if (err)
2081                         printk("failed!\n");
2082                 else
2083                         printk("ok.\n");
2084         }
2085
2086         return err;
2087 }
2088
2089 int orinoco_reinit_firmware(struct net_device *dev)
2090 {
2091         struct orinoco_private *priv = netdev_priv(dev);
2092         struct hermes *hw = &priv->hw;
2093         int err;
2094
2095         err = hermes_init(hw);
2096         if (!err)
2097                 err = orinoco_allocate_fid(dev);
2098
2099         return err;
2100 }
2101
2102 static int __orinoco_hw_set_bitrate(struct orinoco_private *priv)
2103 {
2104         hermes_t *hw = &priv->hw;
2105         int err = 0;
2106
2107         if (priv->bitratemode >= BITRATE_TABLE_SIZE) {
2108                 printk(KERN_ERR "%s: BUG: Invalid bitrate mode %d\n",
2109                        priv->ndev->name, priv->bitratemode);
2110                 return -EINVAL;
2111         }
2112
2113         switch (priv->firmware_type) {
2114         case FIRMWARE_TYPE_AGERE:
2115                 err = hermes_write_wordrec(hw, USER_BAP,
2116                                            HERMES_RID_CNFTXRATECONTROL,
2117                                            bitrate_table[priv->bitratemode].agere_txratectrl);
2118                 break;
2119         case FIRMWARE_TYPE_INTERSIL:
2120         case FIRMWARE_TYPE_SYMBOL:
2121                 err = hermes_write_wordrec(hw, USER_BAP,
2122                                            HERMES_RID_CNFTXRATECONTROL,
2123                                            bitrate_table[priv->bitratemode].intersil_txratectrl);
2124                 break;
2125         default:
2126                 BUG();
2127         }
2128
2129         return err;
2130 }
2131
2132 /* Set fixed AP address */
2133 static int __orinoco_hw_set_wap(struct orinoco_private *priv)
2134 {
2135         int roaming_flag;
2136         int err = 0;
2137         hermes_t *hw = &priv->hw;
2138
2139         switch (priv->firmware_type) {
2140         case FIRMWARE_TYPE_AGERE:
2141                 /* not supported */
2142                 break;
2143         case FIRMWARE_TYPE_INTERSIL:
2144                 if (priv->bssid_fixed)
2145                         roaming_flag = 2;
2146                 else
2147                         roaming_flag = 1;
2148
2149                 err = hermes_write_wordrec(hw, USER_BAP,
2150                                            HERMES_RID_CNFROAMINGMODE,
2151                                            roaming_flag);
2152                 break;
2153         case FIRMWARE_TYPE_SYMBOL:
2154                 err = HERMES_WRITE_RECORD(hw, USER_BAP,
2155                                           HERMES_RID_CNFMANDATORYBSSID_SYMBOL,
2156                                           &priv->desired_bssid);
2157                 break;
2158         }
2159         return err;
2160 }
2161
2162 /* Change the WEP keys and/or the current keys.  Can be called
2163  * either from __orinoco_hw_setup_enc() or directly from
2164  * orinoco_ioctl_setiwencode().  In the later case the association
2165  * with the AP is not broken (if the firmware can handle it),
2166  * which is needed for 802.1x implementations. */
2167 static int __orinoco_hw_setup_wepkeys(struct orinoco_private *priv)
2168 {
2169         hermes_t *hw = &priv->hw;
2170         int err = 0;
2171
2172         switch (priv->firmware_type) {
2173         case FIRMWARE_TYPE_AGERE:
2174                 err = HERMES_WRITE_RECORD(hw, USER_BAP,
2175                                           HERMES_RID_CNFWEPKEYS_AGERE,
2176                                           &priv->keys);
2177                 if (err)
2178                         return err;
2179                 err = hermes_write_wordrec(hw, USER_BAP,
2180                                            HERMES_RID_CNFTXKEY_AGERE,
2181                                            priv->tx_key);
2182                 if (err)
2183                         return err;
2184                 break;
2185         case FIRMWARE_TYPE_INTERSIL:
2186         case FIRMWARE_TYPE_SYMBOL:
2187                 {
2188                         int keylen;
2189                         int i;
2190
2191                         /* Force uniform key length to work around firmware bugs */
2192                         keylen = le16_to_cpu(priv->keys[priv->tx_key].len);
2193                         
2194                         if (keylen > LARGE_KEY_SIZE) {
2195                                 printk(KERN_ERR "%s: BUG: Key %d has oversize length %d.\n",
2196                                        priv->ndev->name, priv->tx_key, keylen);
2197                                 return -E2BIG;
2198                         }
2199
2200                         /* Write all 4 keys */
2201                         for(i = 0; i < ORINOCO_MAX_KEYS; i++) {
2202                                 err = hermes_write_ltv(hw, USER_BAP,
2203                                                        HERMES_RID_CNFDEFAULTKEY0 + i,
2204                                                        HERMES_BYTES_TO_RECLEN(keylen),
2205                                                        priv->keys[i].data);
2206                                 if (err)
2207                                         return err;
2208                         }
2209
2210                         /* Write the index of the key used in transmission */
2211                         err = hermes_write_wordrec(hw, USER_BAP,
2212                                                    HERMES_RID_CNFWEPDEFAULTKEYID,
2213                                                    priv->tx_key);
2214                         if (err)
2215                                 return err;
2216                 }
2217                 break;
2218         }
2219
2220         return 0;
2221 }
2222
2223 static int __orinoco_hw_setup_enc(struct orinoco_private *priv)
2224 {
2225         hermes_t *hw = &priv->hw;
2226         int err = 0;
2227         int master_wep_flag;
2228         int auth_flag;
2229         int enc_flag;
2230
2231         /* Setup WEP keys for WEP and WPA */
2232         if (priv->encode_alg)
2233                 __orinoco_hw_setup_wepkeys(priv);
2234
2235         if (priv->wep_restrict)
2236                 auth_flag = HERMES_AUTH_SHARED_KEY;
2237         else
2238                 auth_flag = HERMES_AUTH_OPEN;
2239
2240         if (priv->wpa_enabled)
2241                 enc_flag = 2;
2242         else if (priv->encode_alg == IW_ENCODE_ALG_WEP)
2243                 enc_flag = 1;
2244         else
2245                 enc_flag = 0;
2246
2247         switch (priv->firmware_type) {
2248         case FIRMWARE_TYPE_AGERE: /* Agere style WEP */
2249                 if (priv->encode_alg == IW_ENCODE_ALG_WEP) {
2250                         /* Enable the shared-key authentication. */
2251                         err = hermes_write_wordrec(hw, USER_BAP,
2252                                                    HERMES_RID_CNFAUTHENTICATION_AGERE,
2253                                                    auth_flag);
2254                 }
2255                 err = hermes_write_wordrec(hw, USER_BAP,
2256                                            HERMES_RID_CNFWEPENABLED_AGERE,
2257                                            enc_flag);
2258                 if (err)
2259                         return err;
2260
2261                 if (priv->has_wpa) {
2262                         /* Set WPA key management */
2263                         err = hermes_write_wordrec(hw, USER_BAP,
2264                                   HERMES_RID_CNFSETWPAAUTHMGMTSUITE_AGERE,
2265                                   priv->key_mgmt);
2266                         if (err)
2267                                 return err;
2268                 }
2269
2270                 break;
2271
2272         case FIRMWARE_TYPE_INTERSIL: /* Intersil style WEP */
2273         case FIRMWARE_TYPE_SYMBOL: /* Symbol style WEP */
2274                 if (priv->encode_alg == IW_ENCODE_ALG_WEP) {
2275                         if (priv->wep_restrict ||
2276                             (priv->firmware_type == FIRMWARE_TYPE_SYMBOL))
2277                                 master_wep_flag = HERMES_WEP_PRIVACY_INVOKED |
2278                                                   HERMES_WEP_EXCL_UNENCRYPTED;
2279                         else
2280                                 master_wep_flag = HERMES_WEP_PRIVACY_INVOKED;
2281
2282                         err = hermes_write_wordrec(hw, USER_BAP,
2283                                                    HERMES_RID_CNFAUTHENTICATION,
2284                                                    auth_flag);
2285                         if (err)
2286                                 return err;
2287                 } else
2288                         master_wep_flag = 0;
2289
2290                 if (priv->iw_mode == IW_MODE_MONITOR)
2291                         master_wep_flag |= HERMES_WEP_HOST_DECRYPT;
2292
2293                 /* Master WEP setting : on/off */
2294                 err = hermes_write_wordrec(hw, USER_BAP,
2295                                            HERMES_RID_CNFWEPFLAGS_INTERSIL,
2296                                            master_wep_flag);
2297                 if (err)
2298                         return err;     
2299
2300                 break;
2301         }
2302
2303         return 0;
2304 }
2305
2306 /* key must be 32 bytes, including the tx and rx MIC keys.
2307  * rsc must be 8 bytes
2308  * tsc must be 8 bytes or NULL
2309  */
2310 static int __orinoco_hw_set_tkip_key(hermes_t *hw, int key_idx, int set_tx,
2311                                      u8 *key, u8 *rsc, u8 *tsc)
2312 {
2313         struct {
2314                 __le16 idx;
2315                 u8 rsc[IW_ENCODE_SEQ_MAX_SIZE];
2316                 u8 key[TKIP_KEYLEN];
2317                 u8 tx_mic[MIC_KEYLEN];
2318                 u8 rx_mic[MIC_KEYLEN];
2319                 u8 tsc[IW_ENCODE_SEQ_MAX_SIZE];
2320         } __attribute__ ((packed)) buf;
2321         int ret;
2322         int err;
2323         int k;
2324         u16 xmitting;
2325
2326         key_idx &= 0x3;
2327
2328         if (set_tx)
2329                 key_idx |= 0x8000;
2330
2331         buf.idx = cpu_to_le16(key_idx);
2332         memcpy(buf.key, key,
2333                sizeof(buf.key) + sizeof(buf.tx_mic) + sizeof(buf.rx_mic));
2334
2335         if (rsc == NULL)
2336                 memset(buf.rsc, 0, sizeof(buf.rsc));
2337         else
2338                 memcpy(buf.rsc, rsc, sizeof(buf.rsc));
2339
2340         if (tsc == NULL) {
2341                 memset(buf.tsc, 0, sizeof(buf.tsc));
2342                 buf.tsc[4] = 0x10;
2343         } else {
2344                 memcpy(buf.tsc, tsc, sizeof(buf.tsc));
2345         }
2346
2347         /* Wait upto 100ms for tx queue to empty */
2348         k = 100;
2349         do {
2350                 k--;
2351                 udelay(1000);
2352                 ret = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_TXQUEUEEMPTY,
2353                                           &xmitting);
2354                 if (ret)
2355                         break;
2356         } while ((k > 0) && xmitting);
2357
2358         if (k == 0)
2359                 ret = -ETIMEDOUT;
2360
2361         err = HERMES_WRITE_RECORD(hw, USER_BAP,
2362                                   HERMES_RID_CNFADDDEFAULTTKIPKEY_AGERE,
2363                                   &buf);
2364
2365         return ret ? ret : err;
2366 }
2367
2368 static int orinoco_clear_tkip_key(struct orinoco_private *priv,
2369                                   int key_idx)
2370 {
2371         hermes_t *hw = &priv->hw;
2372         int err;
2373
2374         memset(&priv->tkip_key[key_idx], 0, sizeof(priv->tkip_key[key_idx]));
2375         err = hermes_write_wordrec(hw, USER_BAP,
2376                                    HERMES_RID_CNFREMDEFAULTTKIPKEY_AGERE,
2377                                    key_idx);
2378         if (err)
2379                 printk(KERN_WARNING "%s: Error %d clearing TKIP key %d\n",
2380                        priv->ndev->name, err, key_idx);
2381         return err;
2382 }
2383
2384 static int __orinoco_program_rids(struct net_device *dev)
2385 {
2386         struct orinoco_private *priv = netdev_priv(dev);
2387         hermes_t *hw = &priv->hw;
2388         int err;
2389         struct hermes_idstring idbuf;
2390
2391         /* Set the MAC address */
2392         err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
2393                                HERMES_BYTES_TO_RECLEN(ETH_ALEN), dev->dev_addr);
2394         if (err) {
2395                 printk(KERN_ERR "%s: Error %d setting MAC address\n",
2396                        dev->name, err);
2397                 return err;
2398         }
2399
2400         /* Set up the link mode */
2401         err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFPORTTYPE,
2402                                    priv->port_type);
2403         if (err) {
2404                 printk(KERN_ERR "%s: Error %d setting port type\n",
2405                        dev->name, err);
2406                 return err;
2407         }
2408         /* Set the channel/frequency */
2409         if (priv->channel != 0 && priv->iw_mode != IW_MODE_INFRA) {
2410                 err = hermes_write_wordrec(hw, USER_BAP,
2411                                            HERMES_RID_CNFOWNCHANNEL,
2412                                            priv->channel);
2413                 if (err) {
2414                         printk(KERN_ERR "%s: Error %d setting channel %d\n",
2415                                dev->name, err, priv->channel);
2416                         return err;
2417                 }
2418         }
2419
2420         if (priv->has_ibss) {
2421                 u16 createibss;
2422
2423                 if ((strlen(priv->desired_essid) == 0) && (priv->createibss)) {
2424                         printk(KERN_WARNING "%s: This firmware requires an "
2425                                "ESSID in IBSS-Ad-Hoc mode.\n", dev->name);
2426                         /* With wvlan_cs, in this case, we would crash.
2427                          * hopefully, this driver will behave better...
2428                          * Jean II */
2429                         createibss = 0;
2430                 } else {
2431                         createibss = priv->createibss;
2432                 }
2433                 
2434                 err = hermes_write_wordrec(hw, USER_BAP,
2435                                            HERMES_RID_CNFCREATEIBSS,
2436                                            createibss);
2437                 if (err) {
2438                         printk(KERN_ERR "%s: Error %d setting CREATEIBSS\n",
2439                                dev->name, err);
2440                         return err;
2441                 }
2442         }
2443
2444         /* Set the desired BSSID */
2445         err = __orinoco_hw_set_wap(priv);
2446         if (err) {
2447                 printk(KERN_ERR "%s: Error %d setting AP address\n",
2448                        dev->name, err);
2449                 return err;
2450         }
2451         /* Set the desired ESSID */
2452         idbuf.len = cpu_to_le16(strlen(priv->desired_essid));
2453         memcpy(&idbuf.val, priv->desired_essid, sizeof(idbuf.val));
2454         /* WinXP wants partner to configure OWNSSID even in IBSS mode. (jimc) */
2455         err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNSSID,
2456                                HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
2457                                &idbuf);
2458         if (err) {
2459                 printk(KERN_ERR "%s: Error %d setting OWNSSID\n",
2460                        dev->name, err);
2461                 return err;
2462         }
2463         err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFDESIREDSSID,
2464                                HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
2465                                &idbuf);
2466         if (err) {
2467                 printk(KERN_ERR "%s: Error %d setting DESIREDSSID\n",
2468                        dev->name, err);
2469                 return err;
2470         }
2471
2472         /* Set the station name */
2473         idbuf.len = cpu_to_le16(strlen(priv->nick));
2474         memcpy(&idbuf.val, priv->nick, sizeof(idbuf.val));
2475         err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
2476                                HERMES_BYTES_TO_RECLEN(strlen(priv->nick)+2),
2477                                &idbuf);
2478         if (err) {
2479                 printk(KERN_ERR "%s: Error %d setting nickname\n",
2480                        dev->name, err);
2481                 return err;
2482         }
2483
2484         /* Set AP density */
2485         if (priv->has_sensitivity) {
2486                 err = hermes_write_wordrec(hw, USER_BAP,
2487                                            HERMES_RID_CNFSYSTEMSCALE,
2488                                            priv->ap_density);
2489                 if (err) {
2490                         printk(KERN_WARNING "%s: Error %d setting SYSTEMSCALE.  "
2491                                "Disabling sensitivity control\n",
2492                                dev->name, err);
2493
2494                         priv->has_sensitivity = 0;
2495                 }
2496         }
2497
2498         /* Set RTS threshold */
2499         err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
2500                                    priv->rts_thresh);
2501         if (err) {
2502                 printk(KERN_ERR "%s: Error %d setting RTS threshold\n",
2503                        dev->name, err);
2504                 return err;
2505         }
2506
2507         /* Set fragmentation threshold or MWO robustness */
2508         if (priv->has_mwo)
2509                 err = hermes_write_wordrec(hw, USER_BAP,
2510                                            HERMES_RID_CNFMWOROBUST_AGERE,
2511                                            priv->mwo_robust);
2512         else
2513                 err = hermes_write_wordrec(hw, USER_BAP,
2514                                            HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
2515                                            priv->frag_thresh);
2516         if (err) {
2517                 printk(KERN_ERR "%s: Error %d setting fragmentation\n",
2518                        dev->name, err);
2519                 return err;
2520         }
2521
2522         /* Set bitrate */
2523         err = __orinoco_hw_set_bitrate(priv);
2524         if (err) {
2525                 printk(KERN_ERR "%s: Error %d setting bitrate\n",
2526                        dev->name, err);
2527                 return err;
2528         }
2529
2530         /* Set power management */
2531         if (priv->has_pm) {
2532                 err = hermes_write_wordrec(hw, USER_BAP,
2533                                            HERMES_RID_CNFPMENABLED,
2534                                            priv->pm_on);
2535                 if (err) {
2536                         printk(KERN_ERR "%s: Error %d setting up PM\n",
2537                                dev->name, err);
2538                         return err;
2539                 }
2540
2541                 err = hermes_write_wordrec(hw, USER_BAP,
2542                                            HERMES_RID_CNFMULTICASTRECEIVE,
2543                                            priv->pm_mcast);
2544                 if (err) {
2545                         printk(KERN_ERR "%s: Error %d setting up PM\n",
2546                                dev->name, err);
2547                         return err;
2548                 }
2549                 err = hermes_write_wordrec(hw, USER_BAP,
2550                                            HERMES_RID_CNFMAXSLEEPDURATION,
2551                                            priv->pm_period);
2552                 if (err) {
2553                         printk(KERN_ERR "%s: Error %d setting up PM\n",
2554                                dev->name, err);
2555                         return err;
2556                 }
2557                 err = hermes_write_wordrec(hw, USER_BAP,
2558                                            HERMES_RID_CNFPMHOLDOVERDURATION,
2559                                            priv->pm_timeout);
2560                 if (err) {
2561                         printk(KERN_ERR "%s: Error %d setting up PM\n",
2562                                dev->name, err);
2563                         return err;
2564                 }
2565         }
2566
2567         /* Set preamble - only for Symbol so far... */
2568         if (priv->has_preamble) {
2569                 err = hermes_write_wordrec(hw, USER_BAP,
2570                                            HERMES_RID_CNFPREAMBLE_SYMBOL,
2571                                            priv->preamble);
2572                 if (err) {
2573                         printk(KERN_ERR "%s: Error %d setting preamble\n",
2574                                dev->name, err);
2575                         return err;
2576                 }
2577         }
2578
2579         /* Set up encryption */
2580         if (priv->has_wep || priv->has_wpa) {
2581                 err = __orinoco_hw_setup_enc(priv);
2582                 if (err) {
2583                         printk(KERN_ERR "%s: Error %d activating encryption\n",
2584                                dev->name, err);
2585                         return err;
2586                 }
2587         }
2588
2589         if (priv->iw_mode == IW_MODE_MONITOR) {
2590                 /* Enable monitor mode */
2591                 dev->type = ARPHRD_IEEE80211;
2592                 err = hermes_docmd_wait(hw, HERMES_CMD_TEST | 
2593                                             HERMES_TEST_MONITOR, 0, NULL);
2594         } else {
2595                 /* Disable monitor mode */
2596                 dev->type = ARPHRD_ETHER;
2597                 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
2598                                             HERMES_TEST_STOP, 0, NULL);
2599         }
2600         if (err)
2601                 return err;
2602
2603         /* Set promiscuity / multicast*/
2604         priv->promiscuous = 0;
2605         priv->mc_count = 0;
2606
2607         /* FIXME: what about netif_tx_lock */
2608         __orinoco_set_multicast_list(dev);
2609
2610         return 0;
2611 }
2612
2613 /* FIXME: return int? */
2614 static void
2615 __orinoco_set_multicast_list(struct net_device *dev)
2616 {
2617         struct orinoco_private *priv = netdev_priv(dev);
2618         hermes_t *hw = &priv->hw;
2619         int err = 0;
2620         int promisc, mc_count;
2621
2622         /* The Hermes doesn't seem to have an allmulti mode, so we go
2623          * into promiscuous mode and let the upper levels deal. */
2624         if ( (dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
2625              (dev->mc_count > MAX_MULTICAST(priv)) ) {
2626                 promisc = 1;
2627                 mc_count = 0;
2628         } else {
2629                 promisc = 0;
2630                 mc_count = dev->mc_count;
2631         }
2632
2633         if (promisc != priv->promiscuous) {
2634                 err = hermes_write_wordrec(hw, USER_BAP,
2635                                            HERMES_RID_CNFPROMISCUOUSMODE,
2636                                            promisc);
2637                 if (err) {
2638                         printk(KERN_ERR "%s: Error %d setting PROMISCUOUSMODE to 1.\n",
2639                                dev->name, err);
2640                 } else 
2641                         priv->promiscuous = promisc;
2642         }
2643
2644         if (! promisc && (mc_count || priv->mc_count) ) {
2645                 struct dev_mc_list *p = dev->mc_list;
2646                 struct hermes_multicast mclist;
2647                 int i;
2648
2649                 for (i = 0; i < mc_count; i++) {
2650                         /* paranoia: is list shorter than mc_count? */
2651                         BUG_ON(! p);
2652                         /* paranoia: bad address size in list? */
2653                         BUG_ON(p->dmi_addrlen != ETH_ALEN);
2654                         
2655                         memcpy(mclist.addr[i], p->dmi_addr, ETH_ALEN);
2656                         p = p->next;
2657                 }
2658                 
2659                 if (p)
2660                         printk(KERN_WARNING "%s: Multicast list is "
2661                                "longer than mc_count\n", dev->name);
2662
2663                 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFGROUPADDRESSES,
2664                                        HERMES_BYTES_TO_RECLEN(priv->mc_count * ETH_ALEN),
2665                                        &mclist);
2666                 if (err)
2667                         printk(KERN_ERR "%s: Error %d setting multicast list.\n",
2668                                dev->name, err);
2669                 else
2670                         priv->mc_count = mc_count;
2671         }
2672 }
2673
2674 /* This must be called from user context, without locks held - use
2675  * schedule_work() */
2676 static void orinoco_reset(struct work_struct *work)
2677 {
2678         struct orinoco_private *priv =
2679                 container_of(work, struct orinoco_private, reset_work);
2680         struct net_device *dev = priv->ndev;
2681         struct hermes *hw = &priv->hw;
2682         int err;
2683         unsigned long flags;
2684
2685         if (orinoco_lock(priv, &flags) != 0)
2686                 /* When the hardware becomes available again, whatever
2687                  * detects that is responsible for re-initializing
2688                  * it. So no need for anything further */
2689                 return;
2690
2691         netif_stop_queue(dev);
2692
2693         /* Shut off interrupts.  Depending on what state the hardware
2694          * is in, this might not work, but we'll try anyway */
2695         hermes_set_irqmask(hw, 0);
2696         hermes_write_regn(hw, EVACK, 0xffff);
2697
2698         priv->hw_unavailable++;
2699         priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
2700         netif_carrier_off(dev);
2701
2702         orinoco_unlock(priv, &flags);
2703
2704         /* Scanning support: Cleanup of driver struct */
2705         orinoco_clear_scan_results(priv, 0);
2706         priv->scan_inprogress = 0;
2707
2708         if (priv->hard_reset) {
2709                 err = (*priv->hard_reset)(priv);
2710                 if (err) {
2711                         printk(KERN_ERR "%s: orinoco_reset: Error %d "
2712                                "performing hard reset\n", dev->name, err);
2713                         goto disable;
2714                 }
2715         }
2716
2717         if (priv->do_fw_download) {
2718                 err = orinoco_download(priv);
2719                 if (err)
2720                         priv->do_fw_download = 0;
2721         }
2722
2723         err = orinoco_reinit_firmware(dev);
2724         if (err) {
2725                 printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
2726                        dev->name, err);
2727                 goto disable;
2728         }
2729
2730         spin_lock_irq(&priv->lock); /* This has to be called from user context */
2731
2732         priv->hw_unavailable--;
2733
2734         /* priv->open or priv->hw_unavailable might have changed while
2735          * we dropped the lock */
2736         if (priv->open && (! priv->hw_unavailable)) {
2737                 err = __orinoco_up(dev);
2738                 if (err) {
2739                         printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
2740                                dev->name, err);
2741                 } else
2742                         dev->trans_start = jiffies;
2743         }
2744
2745         spin_unlock_irq(&priv->lock);
2746
2747         return;
2748  disable:
2749         hermes_set_irqmask(hw, 0);
2750         netif_device_detach(dev);
2751         printk(KERN_ERR "%s: Device has been disabled!\n", dev->name);
2752 }
2753
2754 /********************************************************************/
2755 /* Interrupt handler                                                */
2756 /********************************************************************/
2757
2758 static void __orinoco_ev_tick(struct net_device *dev, hermes_t *hw)
2759 {
2760         printk(KERN_DEBUG "%s: TICK\n", dev->name);
2761 }
2762
2763 static void __orinoco_ev_wterr(struct net_device *dev, hermes_t *hw)
2764 {
2765         /* This seems to happen a fair bit under load, but ignoring it
2766            seems to work fine...*/
2767         printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
2768                dev->name);
2769 }
2770
2771 irqreturn_t orinoco_interrupt(int irq, void *dev_id)
2772 {
2773         struct net_device *dev = dev_id;
2774         struct orinoco_private *priv = netdev_priv(dev);
2775         hermes_t *hw = &priv->hw;
2776         int count = MAX_IRQLOOPS_PER_IRQ;
2777         u16 evstat, events;
2778         /* These are used to detect a runaway interrupt situation */
2779         /* If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
2780          * we panic and shut down the hardware */
2781         static int last_irq_jiffy = 0; /* jiffies value the last time
2782                                         * we were called */
2783         static int loops_this_jiffy = 0;
2784         unsigned long flags;
2785
2786         if (orinoco_lock(priv, &flags) != 0) {
2787                 /* If hw is unavailable - we don't know if the irq was
2788                  * for us or not */
2789                 return IRQ_HANDLED;
2790         }
2791
2792         evstat = hermes_read_regn(hw, EVSTAT);
2793         events = evstat & hw->inten;
2794         if (! events) {
2795                 orinoco_unlock(priv, &flags);
2796                 return IRQ_NONE;
2797         }
2798         
2799         if (jiffies != last_irq_jiffy)
2800                 loops_this_jiffy = 0;
2801         last_irq_jiffy = jiffies;
2802
2803         while (events && count--) {
2804                 if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
2805                         printk(KERN_WARNING "%s: IRQ handler is looping too "
2806                                "much! Resetting.\n", dev->name);
2807                         /* Disable interrupts for now */
2808                         hermes_set_irqmask(hw, 0);
2809                         schedule_work(&priv->reset_work);
2810                         break;
2811                 }
2812
2813                 /* Check the card hasn't been removed */
2814                 if (! hermes_present(hw)) {
2815                         DEBUG(0, "orinoco_interrupt(): card removed\n");
2816                         break;
2817                 }
2818
2819                 if (events & HERMES_EV_TICK)
2820                         __orinoco_ev_tick(dev, hw);
2821                 if (events & HERMES_EV_WTERR)
2822                         __orinoco_ev_wterr(dev, hw);
2823                 if (events & HERMES_EV_INFDROP)
2824                         __orinoco_ev_infdrop(dev, hw);
2825                 if (events & HERMES_EV_INFO)
2826                         __orinoco_ev_info(dev, hw);
2827                 if (events & HERMES_EV_RX)
2828                         __orinoco_ev_rx(dev, hw);
2829                 if (events & HERMES_EV_TXEXC)
2830                         __orinoco_ev_txexc(dev, hw);
2831                 if (events & HERMES_EV_TX)
2832                         __orinoco_ev_tx(dev, hw);
2833                 if (events & HERMES_EV_ALLOC)
2834                         __orinoco_ev_alloc(dev, hw);
2835                 
2836                 hermes_write_regn(hw, EVACK, evstat);
2837
2838                 evstat = hermes_read_regn(hw, EVSTAT);
2839                 events = evstat & hw->inten;
2840         };
2841
2842         orinoco_unlock(priv, &flags);
2843         return IRQ_HANDLED;
2844 }
2845
2846 /********************************************************************/
2847 /* Initialization                                                   */
2848 /********************************************************************/
2849
2850 struct comp_id {
2851         u16 id, variant, major, minor;
2852 } __attribute__ ((packed));
2853
2854 static inline fwtype_t determine_firmware_type(struct comp_id *nic_id)
2855 {
2856         if (nic_id->id < 0x8000)
2857                 return FIRMWARE_TYPE_AGERE;
2858         else if (nic_id->id == 0x8000 && nic_id->major == 0)
2859                 return FIRMWARE_TYPE_SYMBOL;
2860         else
2861                 return FIRMWARE_TYPE_INTERSIL;
2862 }
2863
2864 /* Set priv->firmware type, determine firmware properties */
2865 static int determine_firmware(struct net_device *dev)
2866 {
2867         struct orinoco_private *priv = netdev_priv(dev);
2868         hermes_t *hw = &priv->hw;
2869         int err;
2870         struct comp_id nic_id, sta_id;
2871         unsigned int firmver;
2872         char tmp[SYMBOL_MAX_VER_LEN+1] __attribute__((aligned(2)));
2873
2874         /* Get the hardware version */
2875         err = HERMES_READ_RECORD(hw, USER_BAP, HERMES_RID_NICID, &nic_id);
2876         if (err) {
2877                 printk(KERN_ERR "%s: Cannot read hardware identity: error %d\n",
2878                        dev->name, err);
2879                 return err;
2880         }
2881
2882         le16_to_cpus(&nic_id.id);
2883         le16_to_cpus(&nic_id.variant);
2884         le16_to_cpus(&nic_id.major);
2885         le16_to_cpus(&nic_id.minor);
2886         printk(KERN_DEBUG "%s: Hardware identity %04x:%04x:%04x:%04x\n",
2887                dev->name, nic_id.id, nic_id.variant,
2888                nic_id.major, nic_id.minor);
2889
2890         priv->firmware_type = determine_firmware_type(&nic_id);
2891
2892         /* Get the firmware version */
2893         err = HERMES_READ_RECORD(hw, USER_BAP, HERMES_RID_STAID, &sta_id);
2894         if (err) {
2895                 printk(KERN_ERR "%s: Cannot read station identity: error %d\n",
2896                        dev->name, err);
2897                 return err;
2898         }
2899
2900         le16_to_cpus(&sta_id.id);
2901         le16_to_cpus(&sta_id.variant);
2902         le16_to_cpus(&sta_id.major);
2903         le16_to_cpus(&sta_id.minor);
2904         printk(KERN_DEBUG "%s: Station identity  %04x:%04x:%04x:%04x\n",
2905                dev->name, sta_id.id, sta_id.variant,
2906                sta_id.major, sta_id.minor);
2907
2908         switch (sta_id.id) {
2909         case 0x15:
2910                 printk(KERN_ERR "%s: Primary firmware is active\n",
2911                        dev->name);
2912                 return -ENODEV;
2913         case 0x14b:
2914                 printk(KERN_ERR "%s: Tertiary firmware is active\n",
2915                        dev->name);
2916                 return -ENODEV;
2917         case 0x1f:      /* Intersil, Agere, Symbol Spectrum24 */
2918         case 0x21:      /* Symbol Spectrum24 Trilogy */
2919                 break;
2920         default:
2921                 printk(KERN_NOTICE "%s: Unknown station ID, please report\n",
2922                        dev->name);
2923                 break;
2924         }
2925
2926         /* Default capabilities */
2927         priv->has_sensitivity = 1;
2928         priv->has_mwo = 0;
2929         priv->has_preamble = 0;
2930         priv->has_port3 = 1;
2931         priv->has_ibss = 1;
2932         priv->has_wep = 0;
2933         priv->has_big_wep = 0;
2934         priv->has_alt_txcntl = 0;
2935         priv->has_ext_scan = 0;
2936         priv->has_wpa = 0;
2937         priv->do_fw_download = 0;
2938
2939         /* Determine capabilities from the firmware version */
2940         switch (priv->firmware_type) {
2941         case FIRMWARE_TYPE_AGERE:
2942                 /* Lucent Wavelan IEEE, Lucent Orinoco, Cabletron RoamAbout,
2943                    ELSA, Melco, HP, IBM, Dell 1150, Compaq 110/210 */
2944                 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
2945                          "Lucent/Agere %d.%02d", sta_id.major, sta_id.minor);
2946
2947                 firmver = ((unsigned long)sta_id.major << 16) | sta_id.minor;
2948
2949                 priv->has_ibss = (firmver >= 0x60006);
2950                 priv->has_wep = (firmver >= 0x40020);
2951                 priv->has_big_wep = 1; /* FIXME: this is wrong - how do we tell
2952                                           Gold cards from the others? */
2953                 priv->has_mwo = (firmver >= 0x60000);
2954                 priv->has_pm = (firmver >= 0x40020); /* Don't work in 7.52 ? */
2955                 priv->ibss_port = 1;
2956                 priv->has_hostscan = (firmver >= 0x8000a);
2957                 priv->do_fw_download = 1;
2958                 priv->broken_monitor = (firmver >= 0x80000);
2959                 priv->has_alt_txcntl = (firmver >= 0x90000); /* All 9.x ? */
2960                 priv->has_ext_scan = (firmver >= 0x90000); /* All 9.x ? */
2961                 priv->has_wpa = (firmver >= 0x9002a);
2962                 /* Tested with Agere firmware :
2963                  *      1.16 ; 4.08 ; 4.52 ; 6.04 ; 6.16 ; 7.28 => Jean II
2964                  * Tested CableTron firmware : 4.32 => Anton */
2965                 break;
2966         case FIRMWARE_TYPE_SYMBOL:
2967                 /* Symbol , 3Com AirConnect, Intel, Ericsson WLAN */
2968                 /* Intel MAC : 00:02:B3:* */
2969                 /* 3Com MAC : 00:50:DA:* */
2970                 memset(tmp, 0, sizeof(tmp));
2971                 /* Get the Symbol firmware version */
2972                 err = hermes_read_ltv(hw, USER_BAP,
2973                                       HERMES_RID_SECONDARYVERSION_SYMBOL,
2974                                       SYMBOL_MAX_VER_LEN, NULL, &tmp);
2975                 if (err) {
2976                         printk(KERN_WARNING
2977                                "%s: Error %d reading Symbol firmware info. Wildly guessing capabilities...\n",
2978                                dev->name, err);
2979                         firmver = 0;
2980                         tmp[0] = '\0';
2981                 } else {
2982                         /* The firmware revision is a string, the format is
2983                          * something like : "V2.20-01".
2984                          * Quick and dirty parsing... - Jean II
2985                          */
2986                         firmver = ((tmp[1] - '0') << 16) | ((tmp[3] - '0') << 12)
2987                                 | ((tmp[4] - '0') << 8) | ((tmp[6] - '0') << 4)
2988                                 | (tmp[7] - '0');
2989
2990                         tmp[SYMBOL_MAX_VER_LEN] = '\0';
2991                 }
2992
2993                 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
2994                          "Symbol %s", tmp);
2995
2996                 priv->has_ibss = (firmver >= 0x20000);
2997                 priv->has_wep = (firmver >= 0x15012);
2998                 priv->has_big_wep = (firmver >= 0x20000);
2999                 priv->has_pm = (firmver >= 0x20000 && firmver < 0x22000) || 
3000                                (firmver >= 0x29000 && firmver < 0x30000) ||
3001                                firmver >= 0x31000;
3002                 priv->has_preamble = (firmver >= 0x20000);
3003                 priv->ibss_port = 4;
3004
3005                 /* Symbol firmware is found on various cards, but
3006                  * there has been no attempt to check firmware
3007                  * download on non-spectrum_cs based cards.
3008                  *
3009                  * Given that the Agere firmware download works
3010                  * differently, we should avoid doing a firmware
3011                  * download with the Symbol algorithm on non-spectrum
3012                  * cards.
3013                  *
3014                  * For now we can identify a spectrum_cs based card
3015                  * because it has a firmware reset function.
3016                  */
3017                 priv->do_fw_download = (priv->stop_fw != NULL);
3018
3019                 priv->broken_disableport = (firmver == 0x25013) ||
3020                                            (firmver >= 0x30000 && firmver <= 0x31000);
3021                 priv->has_hostscan = (firmver >= 0x31001) ||
3022                                      (firmver >= 0x29057 && firmver < 0x30000);
3023                 /* Tested with Intel firmware : 0x20015 => Jean II */
3024                 /* Tested with 3Com firmware : 0x15012 & 0x22001 => Jean II */
3025                 break;
3026         case FIRMWARE_TYPE_INTERSIL:
3027                 /* D-Link, Linksys, Adtron, ZoomAir, and many others...
3028                  * Samsung, Compaq 100/200 and Proxim are slightly
3029                  * different and less well tested */
3030                 /* D-Link MAC : 00:40:05:* */
3031                 /* Addtron MAC : 00:90:D1:* */
3032                 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
3033                          "Intersil %d.%d.%d", sta_id.major, sta_id.minor,
3034                          sta_id.variant);
3035
3036                 firmver = ((unsigned long)sta_id.major << 16) |
3037                         ((unsigned long)sta_id.minor << 8) | sta_id.variant;
3038
3039                 priv->has_ibss = (firmver >= 0x000700); /* FIXME */
3040                 priv->has_big_wep = priv->has_wep = (firmver >= 0x000800);
3041                 priv->has_pm = (firmver >= 0x000700);
3042                 priv->has_hostscan = (firmver >= 0x010301);
3043
3044                 if (firmver >= 0x000800)
3045                         priv->ibss_port = 0;
3046                 else {
3047                         printk(KERN_NOTICE "%s: Intersil firmware earlier "
3048                                "than v0.8.x - several features not supported\n",
3049                                dev->name);
3050                         priv->ibss_port = 1;
3051                 }
3052                 break;
3053         }
3054         printk(KERN_DEBUG "%s: Firmware determined as %s\n", dev->name,
3055                priv->fw_name);
3056
3057         return 0;
3058 }
3059
3060 static int orinoco_init(struct net_device *dev)
3061 {
3062         struct orinoco_private *priv = netdev_priv(dev);
3063         hermes_t *hw = &priv->hw;
3064         int err = 0;
3065         struct hermes_idstring nickbuf;
3066         u16 reclen;
3067         int len;
3068         DECLARE_MAC_BUF(mac);
3069
3070         /* No need to lock, the hw_unavailable flag is already set in
3071          * alloc_orinocodev() */
3072         priv->nicbuf_size = IEEE80211_FRAME_LEN + ETH_HLEN;
3073
3074         /* Initialize the firmware */
3075         err = hermes_init(hw);
3076         if (err != 0) {
3077                 printk(KERN_ERR "%s: failed to initialize firmware (err = %d)\n",
3078                        dev->name, err);
3079                 goto out;
3080         }
3081
3082         err = determine_firmware(dev);
3083         if (err != 0) {
3084                 printk(KERN_ERR "%s: Incompatible firmware, aborting\n",
3085                        dev->name);
3086                 goto out;
3087         }
3088
3089         if (priv->do_fw_download) {
3090                 err = orinoco_download(priv);
3091                 if (err)
3092                         priv->do_fw_download = 0;
3093
3094                 /* Check firmware version again */
3095                 err = determine_firmware(dev);
3096                 if (err != 0) {
3097                         printk(KERN_ERR "%s: Incompatible firmware, aborting\n",
3098                                dev->name);
3099                         goto out;
3100                 }
3101         }
3102
3103         if (priv->has_port3)
3104                 printk(KERN_DEBUG "%s: Ad-hoc demo mode supported\n", dev->name);
3105         if (priv->has_ibss)
3106                 printk(KERN_DEBUG "%s: IEEE standard IBSS ad-hoc mode supported\n",
3107                        dev->name);
3108         if (priv->has_wep) {
3109                 printk(KERN_DEBUG "%s: WEP supported, ", dev->name);
3110                 if (priv->has_big_wep)
3111                         printk("104-bit key\n");
3112                 else
3113                         printk("40-bit key\n");
3114         }
3115         if (priv->has_wpa)
3116                 printk(KERN_DEBUG "%s: WPA-PSK supported\n", dev->name);
3117
3118         /* Now we have the firmware capabilities, allocate appropiate
3119          * sized scan buffers */
3120         if (orinoco_bss_data_allocate(priv))
3121                 goto out;
3122         orinoco_bss_data_init(priv);
3123
3124         /* Get the MAC address */
3125         err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
3126                               ETH_ALEN, NULL, dev->dev_addr);
3127         if (err) {
3128                 printk(KERN_WARNING "%s: failed to read MAC address!\n",
3129                        dev->name);
3130                 goto out;
3131         }
3132
3133         printk(KERN_DEBUG "%s: MAC address %s\n",
3134                dev->name, print_mac(mac, dev->dev_addr));
3135
3136         /* Get the station name */
3137         err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
3138                               sizeof(nickbuf), &reclen, &nickbuf);
3139         if (err) {
3140                 printk(KERN_ERR "%s: failed to read station name\n",
3141                        dev->name);
3142                 goto out;
3143         }
3144         if (nickbuf.len)
3145                 len = min(IW_ESSID_MAX_SIZE, (int)le16_to_cpu(nickbuf.len));
3146         else
3147                 len = min(IW_ESSID_MAX_SIZE, 2 * reclen);
3148         memcpy(priv->nick, &nickbuf.val, len);
3149         priv->nick[len] = '\0';
3150
3151         printk(KERN_DEBUG "%s: Station name \"%s\"\n", dev->name, priv->nick);
3152
3153         err = orinoco_allocate_fid(dev);
3154         if (err) {
3155                 printk(KERN_ERR "%s: failed to allocate NIC buffer!\n",
3156                        dev->name);
3157                 goto out;
3158         }
3159
3160         /* Get allowed channels */
3161         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CHANNELLIST,
3162                                   &priv->channel_mask);
3163         if (err) {
3164                 printk(KERN_ERR "%s: failed to read channel list!\n",
3165                        dev->name);
3166                 goto out;
3167         }
3168
3169         /* Get initial AP density */
3170         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFSYSTEMSCALE,
3171                                   &priv->ap_density);
3172         if (err || priv->ap_density < 1 || priv->ap_density > 3) {
3173                 priv->has_sensitivity = 0;
3174         }
3175
3176         /* Get initial RTS threshold */
3177         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
3178                                   &priv->rts_thresh);
3179         if (err) {
3180                 printk(KERN_ERR "%s: failed to read RTS threshold!\n",
3181                        dev->name);
3182                 goto out;
3183         }
3184
3185         /* Get initial fragmentation settings */
3186         if (priv->has_mwo)
3187                 err = hermes_read_wordrec(hw, USER_BAP,
3188                                           HERMES_RID_CNFMWOROBUST_AGERE,
3189                                           &priv->mwo_robust);
3190         else
3191                 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
3192                                           &priv->frag_thresh);
3193         if (err) {
3194                 printk(KERN_ERR "%s: failed to read fragmentation settings!\n",
3195                        dev->name);
3196                 goto out;
3197         }
3198
3199         /* Power management setup */
3200         if (priv->has_pm) {
3201                 priv->pm_on = 0;
3202                 priv->pm_mcast = 1;
3203                 err = hermes_read_wordrec(hw, USER_BAP,
3204                                           HERMES_RID_CNFMAXSLEEPDURATION,
3205                                           &priv->pm_period);
3206                 if (err) {
3207                         printk(KERN_ERR "%s: failed to read power management period!\n",
3208                                dev->name);
3209                         goto out;
3210                 }
3211                 err = hermes_read_wordrec(hw, USER_BAP,
3212                                           HERMES_RID_CNFPMHOLDOVERDURATION,
3213                                           &priv->pm_timeout);
3214                 if (err) {
3215                         printk(KERN_ERR "%s: failed to read power management timeout!\n",
3216                                dev->name);
3217                         goto out;
3218                 }
3219         }
3220
3221         /* Preamble setup */
3222         if (priv->has_preamble) {
3223                 err = hermes_read_wordrec(hw, USER_BAP,
3224                                           HERMES_RID_CNFPREAMBLE_SYMBOL,
3225                                           &priv->preamble);
3226                 if (err)
3227                         goto out;
3228         }
3229                 
3230         /* Set up the default configuration */
3231         priv->iw_mode = IW_MODE_INFRA;
3232         /* By default use IEEE/IBSS ad-hoc mode if we have it */
3233         priv->prefer_port3 = priv->has_port3 && (! priv->has_ibss);
3234         set_port_type(priv);
3235         priv->channel = 0; /* use firmware default */
3236
3237         priv->promiscuous = 0;
3238         priv->encode_alg = IW_ENCODE_ALG_NONE;
3239         priv->tx_key = 0;
3240         priv->wpa_enabled = 0;
3241         priv->tkip_cm_active = 0;
3242         priv->key_mgmt = 0;
3243         priv->wpa_ie_len = 0;
3244         priv->wpa_ie = NULL;
3245
3246         /* Make the hardware available, as long as it hasn't been
3247          * removed elsewhere (e.g. by PCMCIA hot unplug) */
3248         spin_lock_irq(&priv->lock);
3249         priv->hw_unavailable--;
3250         spin_unlock_irq(&priv->lock);
3251
3252         printk(KERN_DEBUG "%s: ready\n", dev->name);
3253
3254  out:
3255         return err;
3256 }
3257
3258 struct net_device
3259 *alloc_orinocodev(int sizeof_card,
3260                   struct device *device,
3261                   int (*hard_reset)(struct orinoco_private *),
3262                   int (*stop_fw)(struct orinoco_private *, int))
3263 {
3264         struct net_device *dev;
3265         struct orinoco_private *priv;
3266
3267         dev = alloc_etherdev(sizeof(struct orinoco_private) + sizeof_card);
3268         if (! dev)
3269                 return NULL;
3270         priv = netdev_priv(dev);
3271         priv->ndev = dev;
3272         if (sizeof_card)
3273                 priv->card = (void *)((unsigned long)priv
3274                                       + sizeof(struct orinoco_private));
3275         else
3276                 priv->card = NULL;
3277         priv->dev = device;
3278
3279         /* Setup / override net_device fields */
3280         dev->init = orinoco_init;
3281         dev->hard_start_xmit = orinoco_xmit;
3282         dev->tx_timeout = orinoco_tx_timeout;
3283         dev->watchdog_timeo = HZ; /* 1 second timeout */
3284         dev->get_stats = orinoco_get_stats;
3285         dev->ethtool_ops = &orinoco_ethtool_ops;
3286         dev->wireless_handlers = (struct iw_handler_def *)&orinoco_handler_def;
3287 #ifdef WIRELESS_SPY
3288         priv->wireless_data.spy_data = &priv->spy_data;
3289         dev->wireless_data = &priv->wireless_data;
3290 #endif
3291         dev->change_mtu = orinoco_change_mtu;
3292         dev->set_multicast_list = orinoco_set_multicast_list;
3293         /* we use the default eth_mac_addr for setting the MAC addr */
3294
3295         /* Set up default callbacks */
3296         dev->open = orinoco_open;
3297         dev->stop = orinoco_stop;
3298         priv->hard_reset = hard_reset;
3299         priv->stop_fw = stop_fw;
3300
3301         spin_lock_init(&priv->lock);
3302         priv->open = 0;
3303         priv->hw_unavailable = 1; /* orinoco_init() must clear this
3304                                    * before anything else touches the
3305                                    * hardware */
3306         INIT_WORK(&priv->reset_work, orinoco_reset);
3307         INIT_WORK(&priv->join_work, orinoco_join_ap);
3308         INIT_WORK(&priv->wevent_work, orinoco_send_wevents);
3309
3310         INIT_LIST_HEAD(&priv->rx_list);
3311         tasklet_init(&priv->rx_tasklet, orinoco_rx_isr_tasklet,
3312                      (unsigned long) dev);
3313
3314         netif_carrier_off(dev);
3315         priv->last_linkstatus = 0xffff;
3316
3317         return dev;
3318 }
3319
3320 void free_orinocodev(struct net_device *dev)
3321 {
3322         struct orinoco_private *priv = netdev_priv(dev);
3323
3324         /* No need to empty priv->rx_list: if the tasklet is scheduled
3325          * when we call tasklet_kill it will run one final time,
3326          * emptying the list */
3327         tasklet_kill(&priv->rx_tasklet);
3328         priv->wpa_ie_len = 0;
3329         kfree(priv->wpa_ie);
3330         orinoco_bss_data_free(priv);
3331         free_netdev(dev);
3332 }
3333
3334 /********************************************************************/
3335 /* Wireless extensions                                              */
3336 /********************************************************************/
3337
3338 /* Return : < 0 -> error code ; >= 0 -> length */
3339 static int orinoco_hw_get_essid(struct orinoco_private *priv, int *active,
3340                                 char buf[IW_ESSID_MAX_SIZE+1])
3341 {
3342         hermes_t *hw = &priv->hw;
3343         int err = 0;
3344         struct hermes_idstring essidbuf;
3345         char *p = (char *)(&essidbuf.val);
3346         int len;
3347         unsigned long flags;
3348
3349         if (orinoco_lock(priv, &flags) != 0)
3350                 return -EBUSY;
3351
3352         if (strlen(priv->desired_essid) > 0) {
3353                 /* We read the desired SSID from the hardware rather
3354                    than from priv->desired_essid, just in case the
3355                    firmware is allowed to change it on us. I'm not
3356                    sure about this */
3357                 /* My guess is that the OWNSSID should always be whatever
3358                  * we set to the card, whereas CURRENT_SSID is the one that
3359                  * may change... - Jean II */
3360                 u16 rid;
3361
3362                 *active = 1;
3363
3364                 rid = (priv->port_type == 3) ? HERMES_RID_CNFOWNSSID :
3365                         HERMES_RID_CNFDESIREDSSID;
3366                 
3367                 err = hermes_read_ltv(hw, USER_BAP, rid, sizeof(essidbuf),
3368                                       NULL, &essidbuf);
3369                 if (err)
3370                         goto fail_unlock;
3371         } else {
3372                 *active = 0;
3373
3374                 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTSSID,
3375                                       sizeof(essidbuf), NULL, &essidbuf);
3376                 if (err)
3377                         goto fail_unlock;
3378         }
3379
3380         len = le16_to_cpu(essidbuf.len);
3381         BUG_ON(len > IW_ESSID_MAX_SIZE);
3382
3383         memset(buf, 0, IW_ESSID_MAX_SIZE);
3384         memcpy(buf, p, len);
3385         err = len;
3386
3387  fail_unlock:
3388         orinoco_unlock(priv, &flags);
3389
3390         return err;       
3391 }
3392
3393 static long orinoco_hw_get_freq(struct orinoco_private *priv)
3394 {
3395         
3396         hermes_t *hw = &priv->hw;
3397         int err = 0;
3398         u16 channel;
3399         long freq = 0;
3400         unsigned long flags;
3401
3402         if (orinoco_lock(priv, &flags) != 0)
3403                 return -EBUSY;
3404         
3405         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CURRENTCHANNEL, &channel);
3406         if (err)
3407                 goto out;
3408
3409         /* Intersil firmware 1.3.5 returns 0 when the interface is down */
3410         if (channel == 0) {
3411                 err = -EBUSY;
3412                 goto out;
3413         }
3414
3415         if ( (channel < 1) || (channel > NUM_CHANNELS) ) {
3416                 printk(KERN_WARNING "%s: Channel out of range (%d)!\n",
3417                        priv->ndev->name, channel);
3418                 err = -EBUSY;
3419                 goto out;
3420
3421         }
3422         freq = channel_frequency[channel-1] * 100000;
3423
3424  out:
3425         orinoco_unlock(priv, &flags);
3426
3427         if (err > 0)
3428                 err = -EBUSY;
3429         return err ? err : freq;
3430 }
3431
3432 static int orinoco_hw_get_bitratelist(struct orinoco_private *priv,
3433                                       int *numrates, s32 *rates, int max)
3434 {
3435         hermes_t *hw = &priv->hw;
3436         struct hermes_idstring list;
3437         unsigned char *p = (unsigned char *)&list.val;
3438         int err = 0;
3439         int num;
3440         int i;
3441         unsigned long flags;
3442
3443         if (orinoco_lock(priv, &flags) != 0)
3444                 return -EBUSY;
3445
3446         err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_SUPPORTEDDATARATES,
3447                               sizeof(list), NULL, &list);
3448         orinoco_unlock(priv, &flags);
3449
3450         if (err)
3451                 return err;
3452         
3453         num = le16_to_cpu(list.len);
3454         *numrates = num;
3455         num = min(num, max);
3456
3457         for (i = 0; i < num; i++) {
3458                 rates[i] = (p[i] & 0x7f) * 500000; /* convert to bps */
3459         }
3460
3461         return 0;
3462 }
3463
3464 static int orinoco_ioctl_getname(struct net_device *dev,
3465                                  struct iw_request_info *info,
3466                                  char *name,
3467                                  char *extra)
3468 {
3469         struct orinoco_private *priv = netdev_priv(dev);
3470         int numrates;
3471         int err;
3472
3473         err = orinoco_hw_get_bitratelist(priv, &numrates, NULL, 0);
3474
3475         if (!err && (numrates > 2))
3476                 strcpy(name, "IEEE 802.11b");
3477         else
3478                 strcpy(name, "IEEE 802.11-DS");
3479
3480         return 0;
3481 }
3482
3483 static int orinoco_ioctl_setwap(struct net_device *dev,
3484                                 struct iw_request_info *info,
3485                                 struct sockaddr *ap_addr,
3486                                 char *extra)
3487 {
3488         struct orinoco_private *priv = netdev_priv(dev);
3489         int err = -EINPROGRESS;         /* Call commit handler */
3490         unsigned long flags;
3491         static const u8 off_addr[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3492         static const u8 any_addr[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3493
3494         if (orinoco_lock(priv, &flags) != 0)
3495                 return -EBUSY;
3496
3497         /* Enable automatic roaming - no sanity checks are needed */
3498         if (memcmp(&ap_addr->sa_data, off_addr, ETH_ALEN) == 0 ||
3499             memcmp(&ap_addr->sa_data, any_addr, ETH_ALEN) == 0) {
3500                 priv->bssid_fixed = 0;
3501                 memset(priv->desired_bssid, 0, ETH_ALEN);
3502
3503                 /* "off" means keep existing connection */
3504                 if (ap_addr->sa_data[0] == 0) {
3505                         __orinoco_hw_set_wap(priv);
3506                         err = 0;
3507                 }
3508                 goto out;
3509         }
3510
3511         if (priv->firmware_type == FIRMWARE_TYPE_AGERE) {
3512                 printk(KERN_WARNING "%s: Lucent/Agere firmware doesn't "
3513                        "support manual roaming\n",
3514                        dev->name);
3515                 err = -EOPNOTSUPP;
3516                 goto out;
3517         }
3518
3519         if (priv->iw_mode != IW_MODE_INFRA) {
3520                 printk(KERN_WARNING "%s: Manual roaming supported only in "
3521                        "managed mode\n", dev->name);
3522                 err = -EOPNOTSUPP;
3523                 goto out;
3524         }
3525
3526         /* Intersil firmware hangs without Desired ESSID */
3527         if (priv->firmware_type == FIRMWARE_TYPE_INTERSIL &&
3528             strlen(priv->desired_essid) == 0) {
3529                 printk(KERN_WARNING "%s: Desired ESSID must be set for "
3530                        "manual roaming\n", dev->name);
3531                 err = -EOPNOTSUPP;
3532                 goto out;
3533         }
3534
3535         /* Finally, enable manual roaming */
3536         priv->bssid_fixed = 1;
3537         memcpy(priv->desired_bssid, &ap_addr->sa_data, ETH_ALEN);
3538
3539  out:
3540         orinoco_unlock(priv, &flags);
3541         return err;
3542 }
3543
3544 static int orinoco_ioctl_getwap(struct net_device *dev,
3545                                 struct iw_request_info *info,
3546                                 struct sockaddr *ap_addr,
3547                                 char *extra)
3548 {
3549         struct orinoco_private *priv = netdev_priv(dev);
3550
3551         hermes_t *hw = &priv->hw;
3552         int err = 0;
3553         unsigned long flags;
3554
3555         if (orinoco_lock(priv, &flags) != 0)
3556                 return -EBUSY;
3557
3558         ap_addr->sa_family = ARPHRD_ETHER;
3559         err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
3560                               ETH_ALEN, NULL, ap_addr->sa_data);
3561
3562         orinoco_unlock(priv, &flags);
3563
3564         return err;
3565 }
3566
3567 static int orinoco_ioctl_setmode(struct net_device *dev,
3568                                  struct iw_request_info *info,
3569                                  u32 *mode,
3570                                  char *extra)
3571 {
3572         struct orinoco_private *priv = netdev_priv(dev);
3573         int err = -EINPROGRESS;         /* Call commit handler */
3574         unsigned long flags;
3575
3576         if (priv->iw_mode == *mode)
3577                 return 0;
3578
3579         if (orinoco_lock(priv, &flags) != 0)
3580                 return -EBUSY;
3581
3582         switch (*mode) {
3583         case IW_MODE_ADHOC:
3584                 if (!priv->has_ibss && !priv->has_port3)
3585                         err = -EOPNOTSUPP;
3586                 break;
3587
3588         case IW_MODE_INFRA:
3589                 break;
3590
3591         case IW_MODE_MONITOR:
3592                 if (priv->broken_monitor && !force_monitor) {
3593                         printk(KERN_WARNING "%s: Monitor mode support is "
3594                                "buggy in this firmware, not enabling\n",
3595                                dev->name);
3596                         err = -EOPNOTSUPP;
3597                 }
3598                 break;
3599
3600         default:
3601                 err = -EOPNOTSUPP;
3602                 break;
3603         }
3604
3605         if (err == -EINPROGRESS) {
3606                 priv->iw_mode = *mode;
3607                 set_port_type(priv);
3608         }
3609
3610         orinoco_unlock(priv, &flags);
3611
3612         return err;
3613 }
3614
3615 static int orinoco_ioctl_getmode(struct net_device *dev,
3616                                  struct iw_request_info *info,
3617                                  u32 *mode,
3618                                  char *extra)
3619 {
3620         struct orinoco_private *priv = netdev_priv(dev);
3621
3622         *mode = priv->iw_mode;
3623         return 0;
3624 }
3625
3626 static int orinoco_ioctl_getiwrange(struct net_device *dev,
3627                                     struct iw_request_info *info,
3628                                     struct iw_point *rrq,
3629                                     char *extra)
3630 {
3631         struct orinoco_private *priv = netdev_priv(dev);
3632         int err = 0;
3633         struct iw_range *range = (struct iw_range *) extra;
3634         int numrates;
3635         int i, k;
3636
3637         rrq->length = sizeof(struct iw_range);
3638         memset(range, 0, sizeof(struct iw_range));
3639
3640         range->we_version_compiled = WIRELESS_EXT;
3641         range->we_version_source = 22;
3642
3643         /* Set available channels/frequencies */
3644         range->num_channels = NUM_CHANNELS;
3645         k = 0;
3646         for (i = 0; i < NUM_CHANNELS; i++) {
3647                 if (priv->channel_mask & (1 << i)) {
3648                         range->freq[k].i = i + 1;
3649                         range->freq[k].m = channel_frequency[i] * 100000;
3650                         range->freq[k].e = 1;
3651                         k++;
3652                 }
3653                 
3654                 if (k >= IW_MAX_FREQUENCIES)
3655                         break;
3656         }
3657         range->num_frequency = k;
3658         range->sensitivity = 3;
3659
3660         if (priv->has_wep) {
3661                 range->max_encoding_tokens = ORINOCO_MAX_KEYS;
3662                 range->encoding_size[0] = SMALL_KEY_SIZE;
3663                 range->num_encoding_sizes = 1;
3664
3665                 if (priv->has_big_wep) {
3666                         range->encoding_size[1] = LARGE_KEY_SIZE;
3667                         range->num_encoding_sizes = 2;
3668                 }
3669         }
3670
3671         if (priv->has_wpa)
3672                 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_CIPHER_TKIP;
3673
3674         if ((priv->iw_mode == IW_MODE_ADHOC) && (!SPY_NUMBER(priv))){
3675                 /* Quality stats meaningless in ad-hoc mode */
3676         } else {
3677                 range->max_qual.qual = 0x8b - 0x2f;
3678                 range->max_qual.level = 0x2f - 0x95 - 1;
3679                 range->max_qual.noise = 0x2f - 0x95 - 1;
3680                 /* Need to get better values */
3681                 range->avg_qual.qual = 0x24;
3682                 range->avg_qual.level = 0xC2;
3683                 range->avg_qual.noise = 0x9E;
3684         }
3685
3686         err = orinoco_hw_get_bitratelist(priv, &numrates,
3687                                          range->bitrate, IW_MAX_BITRATES);
3688         if (err)
3689                 return err;
3690         range->num_bitrates = numrates;
3691
3692         /* Set an indication of the max TCP throughput in bit/s that we can
3693          * expect using this interface. May be use for QoS stuff...
3694          * Jean II */
3695         if (numrates > 2)
3696                 range->throughput = 5 * 1000 * 1000;    /* ~5 Mb/s */
3697         else
3698                 range->throughput = 1.5 * 1000 * 1000;  /* ~1.5 Mb/s */
3699
3700         range->min_rts = 0;
3701         range->max_rts = 2347;
3702         range->min_frag = 256;
3703         range->max_frag = 2346;
3704
3705         range->min_pmp = 0;
3706         range->max_pmp = 65535000;
3707         range->min_pmt = 0;
3708         range->max_pmt = 65535 * 1000;  /* ??? */
3709         range->pmp_flags = IW_POWER_PERIOD;
3710         range->pmt_flags = IW_POWER_TIMEOUT;
3711         range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_UNICAST_R;
3712
3713         range->retry_capa = IW_RETRY_LIMIT | IW_RETRY_LIFETIME;
3714         range->retry_flags = IW_RETRY_LIMIT;
3715         range->r_time_flags = IW_RETRY_LIFETIME;
3716         range->min_retry = 0;
3717         range->max_retry = 65535;       /* ??? */
3718         range->min_r_time = 0;
3719         range->max_r_time = 65535 * 1000;       /* ??? */
3720
3721         if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
3722                 range->scan_capa = IW_SCAN_CAPA_ESSID;
3723         else
3724                 range->scan_capa = IW_SCAN_CAPA_NONE;
3725
3726         /* Event capability (kernel) */
3727         IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
3728         /* Event capability (driver) */
3729         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWTHRSPY);
3730         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
3731         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
3732         IW_EVENT_CAPA_SET(range->event_capa, IWEVTXDROP);
3733
3734         return 0;
3735 }
3736
3737 static int orinoco_ioctl_setiwencode(struct net_device *dev,
3738                                      struct iw_request_info *info,
3739                                      struct iw_point *erq,
3740                                      char *keybuf)
3741 {
3742         struct orinoco_private *priv = netdev_priv(dev);
3743         int index = (erq->flags & IW_ENCODE_INDEX) - 1;
3744         int setindex = priv->tx_key;
3745         int encode_alg = priv->encode_alg;
3746         int restricted = priv->wep_restrict;
3747         u16 xlen = 0;
3748         int err = -EINPROGRESS;         /* Call commit handler */
3749         unsigned long flags;
3750
3751         if (! priv->has_wep)
3752                 return -EOPNOTSUPP;
3753
3754         if (erq->pointer) {
3755                 /* We actually have a key to set - check its length */
3756                 if (erq->length > LARGE_KEY_SIZE)
3757                         return -E2BIG;
3758
3759                 if ( (erq->length > SMALL_KEY_SIZE) && !priv->has_big_wep )
3760                         return -E2BIG;
3761         }
3762
3763         if (orinoco_lock(priv, &flags) != 0)
3764                 return -EBUSY;
3765
3766         /* Clear any TKIP key we have */
3767         if ((priv->has_wpa) && (priv->encode_alg == IW_ENCODE_ALG_TKIP))
3768                 (void) orinoco_clear_tkip_key(priv, setindex);
3769
3770         if (erq->length > 0) {
3771                 if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
3772                         index = priv->tx_key;
3773
3774                 /* Adjust key length to a supported value */
3775                 if (erq->length > SMALL_KEY_SIZE) {
3776                         xlen = LARGE_KEY_SIZE;
3777                 } else if (erq->length > 0) {
3778                         xlen = SMALL_KEY_SIZE;
3779                 } else
3780                         xlen = 0;
3781
3782                 /* Switch on WEP if off */
3783                 if ((encode_alg != IW_ENCODE_ALG_WEP) && (xlen > 0)) {
3784                         setindex = index;
3785                         encode_alg = IW_ENCODE_ALG_WEP;
3786                 }
3787         } else {
3788                 /* Important note : if the user do "iwconfig eth0 enc off",
3789                  * we will arrive there with an index of -1. This is valid
3790                  * but need to be taken care off... Jean II */
3791                 if ((index < 0) || (index >= ORINOCO_MAX_KEYS)) {
3792                         if((index != -1) || (erq->flags == 0)) {
3793                                 err = -EINVAL;
3794                                 goto out;
3795                         }
3796                 } else {
3797                         /* Set the index : Check that the key is valid */
3798                         if(priv->keys[index].len == 0) {
3799                                 err = -EINVAL;
3800                                 goto out;
3801                         }
3802                         setindex = index;
3803                 }
3804         }
3805
3806         if (erq->flags & IW_ENCODE_DISABLED)
3807                 encode_alg = IW_ENCODE_ALG_NONE;
3808         if (erq->flags & IW_ENCODE_OPEN)
3809                 restricted = 0;
3810         if (erq->flags & IW_ENCODE_RESTRICTED)
3811                 restricted = 1;
3812
3813         if (erq->pointer && erq->length > 0) {
3814                 priv->keys[index].len = cpu_to_le16(xlen);
3815                 memset(priv->keys[index].data, 0,
3816                        sizeof(priv->keys[index].data));
3817                 memcpy(priv->keys[index].data, keybuf, erq->length);
3818         }
3819         priv->tx_key = setindex;
3820
3821         /* Try fast key change if connected and only keys are changed */
3822         if ((priv->encode_alg == encode_alg) &&
3823             (priv->wep_restrict == restricted) &&
3824             netif_carrier_ok(dev)) {
3825                 err = __orinoco_hw_setup_wepkeys(priv);
3826                 /* No need to commit if successful */
3827                 goto out;
3828         }
3829
3830         priv->encode_alg = encode_alg;
3831         priv->wep_restrict = restricted;
3832
3833  out:
3834         orinoco_unlock(priv, &flags);
3835
3836         return err;
3837 }
3838
3839 static int orinoco_ioctl_getiwencode(struct net_device *dev,
3840                                      struct iw_request_info *info,
3841                                      struct iw_point *erq,
3842                                      char *keybuf)
3843 {
3844         struct orinoco_private *priv = netdev_priv(dev);
3845         int index = (erq->flags & IW_ENCODE_INDEX) - 1;
3846         u16 xlen = 0;
3847         unsigned long flags;
3848
3849         if (! priv->has_wep)
3850                 return -EOPNOTSUPP;
3851
3852         if (orinoco_lock(priv, &flags) != 0)
3853                 return -EBUSY;
3854
3855         if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
3856                 index = priv->tx_key;
3857
3858         erq->flags = 0;
3859         if (!priv->encode_alg)
3860                 erq->flags |= IW_ENCODE_DISABLED;
3861         erq->flags |= index + 1;
3862
3863         if (priv->wep_restrict)
3864                 erq->flags |= IW_ENCODE_RESTRICTED;
3865         else
3866                 erq->flags |= IW_ENCODE_OPEN;
3867
3868         xlen = le16_to_cpu(priv->keys[index].len);
3869
3870         erq->length = xlen;
3871
3872         memcpy(keybuf, priv->keys[index].data, ORINOCO_MAX_KEY_SIZE);
3873
3874         orinoco_unlock(priv, &flags);
3875         return 0;
3876 }
3877
3878 static int orinoco_ioctl_setessid(struct net_device *dev,
3879                                   struct iw_request_info *info,
3880                                   struct iw_point *erq,
3881                                   char *essidbuf)
3882 {
3883         struct orinoco_private *priv = netdev_priv(dev);
3884         unsigned long flags;
3885
3886         /* Note : ESSID is ignored in Ad-Hoc demo mode, but we can set it
3887          * anyway... - Jean II */
3888
3889         /* Hum... Should not use Wireless Extension constant (may change),
3890          * should use our own... - Jean II */
3891         if (erq->length > IW_ESSID_MAX_SIZE)
3892                 return -E2BIG;
3893
3894         if (orinoco_lock(priv, &flags) != 0)
3895                 return -EBUSY;
3896
3897         /* NULL the string (for NULL termination & ESSID = ANY) - Jean II */
3898         memset(priv->desired_essid, 0, sizeof(priv->desired_essid));
3899
3900         /* If not ANY, get the new ESSID */
3901         if (erq->flags) {
3902                 memcpy(priv->desired_essid, essidbuf, erq->length);
3903         }
3904
3905         orinoco_unlock(priv, &flags);
3906
3907         return -EINPROGRESS;            /* Call commit handler */
3908 }
3909
3910 static int orinoco_ioctl_getessid(struct net_device *dev,
3911                                   struct iw_request_info *info,
3912                                   struct iw_point *erq,
3913                                   char *essidbuf)
3914 {
3915         struct orinoco_private *priv = netdev_priv(dev);
3916         int active;
3917         int err = 0;
3918         unsigned long flags;
3919
3920         if (netif_running(dev)) {
3921                 err = orinoco_hw_get_essid(priv, &active, essidbuf);
3922                 if (err < 0)
3923                         return err;
3924                 erq->length = err;
3925         } else {
3926                 if (orinoco_lock(priv, &flags) != 0)
3927                         return -EBUSY;
3928                 memcpy(essidbuf, priv->desired_essid, IW_ESSID_MAX_SIZE);
3929                 erq->length = strlen(priv->desired_essid);
3930                 orinoco_unlock(priv, &flags);
3931         }
3932
3933         erq->flags = 1;
3934
3935         return 0;
3936 }
3937
3938 static int orinoco_ioctl_setnick(struct net_device *dev,
3939                                  struct iw_request_info *info,
3940                                  struct iw_point *nrq,
3941                                  char *nickbuf)
3942 {
3943         struct orinoco_private *priv = netdev_priv(dev);
3944         unsigned long flags;
3945
3946         if (nrq->length > IW_ESSID_MAX_SIZE)
3947                 return -E2BIG;
3948
3949         if (orinoco_lock(priv, &flags) != 0)
3950                 return -EBUSY;
3951
3952         memset(priv->nick, 0, sizeof(priv->nick));
3953         memcpy(priv->nick, nickbuf, nrq->length);
3954
3955         orinoco_unlock(priv, &flags);
3956
3957         return -EINPROGRESS;            /* Call commit handler */
3958 }
3959
3960 static int orinoco_ioctl_getnick(struct net_device *dev,
3961                                  struct iw_request_info *info,
3962                                  struct iw_point *nrq,
3963                                  char *nickbuf)
3964 {
3965         struct orinoco_private *priv = netdev_priv(dev);
3966         unsigned long flags;
3967
3968         if (orinoco_lock(priv, &flags) != 0)
3969                 return -EBUSY;
3970
3971         memcpy(nickbuf, priv->nick, IW_ESSID_MAX_SIZE);
3972         orinoco_unlock(priv, &flags);
3973
3974         nrq->length = strlen(priv->nick);
3975
3976         return 0;
3977 }
3978
3979 static int orinoco_ioctl_setfreq(struct net_device *dev,
3980                                  struct iw_request_info *info,
3981                                  struct iw_freq *frq,
3982                                  char *extra)
3983 {
3984         struct orinoco_private *priv = netdev_priv(dev);
3985         int chan = -1;
3986         unsigned long flags;
3987         int err = -EINPROGRESS;         /* Call commit handler */
3988
3989         /* In infrastructure mode the AP sets the channel */
3990         if (priv->iw_mode == IW_MODE_INFRA)
3991                 return -EBUSY;
3992
3993         if ( (frq->e == 0) && (frq->m <= 1000) ) {
3994                 /* Setting by channel number */
3995                 chan = frq->m;
3996         } else {
3997                 /* Setting by frequency - search the table */
3998                 int mult = 1;
3999                 int i;
4000
4001                 for (i = 0; i < (6 - frq->e); i++)
4002                         mult *= 10;
4003
4004                 for (i = 0; i < NUM_CHANNELS; i++)
4005                         if (frq->m == (channel_frequency[i] * mult))
4006                                 chan = i+1;
4007         }
4008
4009         if ( (chan < 1) || (chan > NUM_CHANNELS) ||
4010              ! (priv->channel_mask & (1 << (chan-1)) ) )
4011                 return -EINVAL;
4012
4013         if (orinoco_lock(priv, &flags) != 0)
4014                 return -EBUSY;
4015
4016         priv->channel = chan;
4017         if (priv->iw_mode == IW_MODE_MONITOR) {
4018                 /* Fast channel change - no commit if successful */
4019                 hermes_t *hw = &priv->hw;
4020                 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
4021                                             HERMES_TEST_SET_CHANNEL,
4022                                         chan, NULL);
4023         }
4024         orinoco_unlock(priv, &flags);
4025
4026         return err;
4027 }
4028
4029 static int orinoco_ioctl_getfreq(struct net_device *dev,
4030                                  struct iw_request_info *info,
4031                                  struct iw_freq *frq,
4032                                  char *extra)
4033 {
4034         struct orinoco_private *priv = netdev_priv(dev);
4035         int tmp;
4036
4037         /* Locking done in there */
4038         tmp = orinoco_hw_get_freq(priv);
4039         if (tmp < 0) {
4040                 return tmp;
4041         }
4042
4043         frq->m = tmp;
4044         frq->e = 1;
4045
4046         return 0;
4047 }
4048
4049 static int orinoco_ioctl_getsens(struct net_device *dev,
4050                                  struct iw_request_info *info,
4051                                  struct iw_param *srq,
4052                                  char *extra)
4053 {
4054         struct orinoco_private *priv = netdev_priv(dev);
4055         hermes_t *hw = &priv->hw;
4056         u16 val;
4057         int err;
4058         unsigned long flags;
4059
4060         if (!priv->has_sensitivity)
4061                 return -EOPNOTSUPP;
4062
4063         if (orinoco_lock(priv, &flags) != 0)
4064                 return -EBUSY;
4065         err = hermes_read_wordrec(hw, USER_BAP,
4066                                   HERMES_RID_CNFSYSTEMSCALE, &val);
4067         orinoco_unlock(priv, &flags);
4068
4069         if (err)
4070                 return err;
4071
4072         srq->value = val;
4073         srq->fixed = 0; /* auto */
4074
4075         return 0;
4076 }
4077
4078 static int orinoco_ioctl_setsens(struct net_device *dev,
4079                                  struct iw_request_info *info,
4080                                  struct iw_param *srq,
4081                                  char *extra)
4082 {
4083         struct orinoco_private *priv = netdev_priv(dev);
4084         int val = srq->value;
4085         unsigned long flags;
4086
4087         if (!priv->has_sensitivity)
4088                 return -EOPNOTSUPP;
4089
4090         if ((val < 1) || (val > 3))
4091                 return -EINVAL;
4092         
4093         if (orinoco_lock(priv, &flags) != 0)
4094                 return -EBUSY;
4095         priv->ap_density = val;
4096         orinoco_unlock(priv, &flags);
4097
4098         return -EINPROGRESS;            /* Call commit handler */
4099 }
4100
4101 static int orinoco_ioctl_setrts(struct net_device *dev,
4102                                 struct iw_request_info *info,
4103                                 struct iw_param *rrq,
4104                                 char *extra)
4105 {
4106         struct orinoco_private *priv = netdev_priv(dev);
4107         int val = rrq->value;
4108         unsigned long flags;
4109
4110         if (rrq->disabled)
4111                 val = 2347;
4112
4113         if ( (val < 0) || (val > 2347) )
4114                 return -EINVAL;
4115
4116         if (orinoco_lock(priv, &flags) != 0)
4117                 return -EBUSY;
4118
4119         priv->rts_thresh = val;
4120         orinoco_unlock(priv, &flags);
4121
4122         return -EINPROGRESS;            /* Call commit handler */
4123 }
4124
4125 static int orinoco_ioctl_getrts(struct net_device *dev,
4126                                 struct iw_request_info *info,
4127                                 struct iw_param *rrq,
4128                                 char *extra)
4129 {
4130         struct orinoco_private *priv = netdev_priv(dev);
4131
4132         rrq->value = priv->rts_thresh;
4133         rrq->disabled = (rrq->value == 2347);
4134         rrq->fixed = 1;
4135
4136         return 0;
4137 }
4138
4139 static int orinoco_ioctl_setfrag(struct net_device *dev,
4140                                  struct iw_request_info *info,
4141                                  struct iw_param *frq,
4142                                  char *extra)
4143 {
4144         struct orinoco_private *priv = netdev_priv(dev);
4145         int err = -EINPROGRESS;         /* Call commit handler */
4146         unsigned long flags;
4147
4148         if (orinoco_lock(priv, &flags) != 0)
4149                 return -EBUSY;
4150
4151         if (priv->has_mwo) {
4152                 if (frq->disabled)
4153                         priv->mwo_robust = 0;
4154                 else {
4155                         if (frq->fixed)
4156                                 printk(KERN_WARNING "%s: Fixed fragmentation is "
4157                                        "not supported on this firmware. "
4158                                        "Using MWO robust instead.\n", dev->name);
4159                         priv->mwo_robust = 1;
4160                 }
4161         } else {
4162                 if (frq->disabled)
4163                         priv->frag_thresh = 2346;
4164                 else {
4165                         if ( (frq->value < 256) || (frq->value > 2346) )
4166                                 err = -EINVAL;
4167                         else
4168                                 priv->frag_thresh = frq->value & ~0x1; /* must be even */
4169                 }
4170         }
4171
4172         orinoco_unlock(priv, &flags);
4173
4174         return err;
4175 }
4176
4177 static int orinoco_ioctl_getfrag(struct net_device *dev,
4178                                  struct iw_request_info *info,
4179                                  struct iw_param *frq,
4180                                  char *extra)
4181 {
4182         struct orinoco_private *priv = netdev_priv(dev);
4183         hermes_t *hw = &priv->hw;
4184         int err;
4185         u16 val;
4186         unsigned long flags;
4187
4188         if (orinoco_lock(priv, &flags) != 0)
4189                 return -EBUSY;
4190         
4191         if (priv->has_mwo) {
4192                 err = hermes_read_wordrec(hw, USER_BAP,
4193                                           HERMES_RID_CNFMWOROBUST_AGERE,
4194                                           &val);
4195                 if (err)
4196                         val = 0;
4197
4198                 frq->value = val ? 2347 : 0;
4199                 frq->disabled = ! val;
4200                 frq->fixed = 0;
4201         } else {
4202                 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
4203                                           &val);
4204                 if (err)
4205                         val = 0;
4206
4207                 frq->value = val;
4208                 frq->disabled = (val >= 2346);
4209                 frq->fixed = 1;
4210         }
4211
4212         orinoco_unlock(priv, &flags);
4213         
4214         return err;
4215 }
4216
4217 static int orinoco_ioctl_setrate(struct net_device *dev,
4218                                  struct iw_request_info *info,
4219                                  struct iw_param *rrq,
4220                                  char *extra)
4221 {
4222         struct orinoco_private *priv = netdev_priv(dev);
4223         int ratemode = -1;
4224         int bitrate; /* 100s of kilobits */
4225         int i;
4226         unsigned long flags;
4227         
4228         /* As the user space doesn't know our highest rate, it uses -1
4229          * to ask us to set the highest rate.  Test it using "iwconfig
4230          * ethX rate auto" - Jean II */
4231         if (rrq->value == -1)
4232                 bitrate = 110;
4233         else {
4234                 if (rrq->value % 100000)
4235                         return -EINVAL;
4236                 bitrate = rrq->value / 100000;
4237         }
4238
4239         if ( (bitrate != 10) && (bitrate != 20) &&
4240              (bitrate != 55) && (bitrate != 110) )
4241                 return -EINVAL;
4242
4243         for (i = 0; i < BITRATE_TABLE_SIZE; i++)
4244                 if ( (bitrate_table[i].bitrate == bitrate) &&
4245                      (bitrate_table[i].automatic == ! rrq->fixed) ) {
4246                         ratemode = i;
4247                         break;
4248                 }
4249         
4250         if (ratemode == -1)
4251                 return -EINVAL;
4252
4253         if (orinoco_lock(priv, &flags) != 0)
4254                 return -EBUSY;
4255         priv->bitratemode = ratemode;
4256         orinoco_unlock(priv, &flags);
4257
4258         return -EINPROGRESS;
4259 }
4260
4261 static int orinoco_ioctl_getrate(struct net_device *dev,
4262                                  struct iw_request_info *info,
4263                                  struct iw_param *rrq,
4264                                  char *extra)
4265 {
4266         struct orinoco_private *priv = netdev_priv(dev);
4267         hermes_t *hw = &priv->hw;
4268         int err = 0;
4269         int ratemode;
4270         int i;
4271         u16 val;
4272         unsigned long flags;
4273
4274         if (orinoco_lock(priv, &flags) != 0)
4275                 return -EBUSY;
4276
4277         ratemode = priv->bitratemode;
4278
4279         BUG_ON((ratemode < 0) || (ratemode >= BITRATE_TABLE_SIZE));
4280
4281         rrq->value = bitrate_table[ratemode].bitrate * 100000;
4282         rrq->fixed = ! bitrate_table[ratemode].automatic;
4283         rrq->disabled = 0;
4284
4285         /* If the interface is running we try to find more about the
4286            current mode */
4287         if (netif_running(dev)) {
4288                 err = hermes_read_wordrec(hw, USER_BAP,
4289                                           HERMES_RID_CURRENTTXRATE, &val);
4290                 if (err)
4291                         goto out;
4292                 
4293                 switch (priv->firmware_type) {
4294                 case FIRMWARE_TYPE_AGERE: /* Lucent style rate */
4295                         /* Note : in Lucent firmware, the return value of
4296                          * HERMES_RID_CURRENTTXRATE is the bitrate in Mb/s,
4297                          * and therefore is totally different from the
4298                          * encoding of HERMES_RID_CNFTXRATECONTROL.
4299                          * Don't forget that 6Mb/s is really 5.5Mb/s */
4300                         if (val == 6)
4301                                 rrq->value = 5500000;
4302                         else
4303                                 rrq->value = val * 1000000;
4304                         break;
4305                 case FIRMWARE_TYPE_INTERSIL: /* Intersil style rate */
4306                 case FIRMWARE_TYPE_SYMBOL: /* Symbol style rate */
4307                         for (i = 0; i < BITRATE_TABLE_SIZE; i++)
4308                                 if (bitrate_table[i].intersil_txratectrl == val) {
4309                                         ratemode = i;
4310                                         break;
4311                                 }
4312                         if (i >= BITRATE_TABLE_SIZE)
4313                                 printk(KERN_INFO "%s: Unable to determine current bitrate (0x%04hx)\n",
4314                                        dev->name, val);
4315
4316                         rrq->value = bitrate_table[ratemode].bitrate * 100000;
4317                         break;
4318                 default:
4319                         BUG();
4320                 }
4321         }
4322
4323  out:
4324         orinoco_unlock(priv, &flags);
4325
4326         return err;
4327 }
4328
4329 static int orinoco_ioctl_setpower(struct net_device *dev,
4330                                   struct iw_request_info *info,
4331                                   struct iw_param *prq,
4332                                   char *extra)
4333 {
4334         struct orinoco_private *priv = netdev_priv(dev);
4335         int err = -EINPROGRESS;         /* Call commit handler */
4336         unsigned long flags;
4337
4338         if (orinoco_lock(priv, &flags) != 0)
4339                 return -EBUSY;
4340
4341         if (prq->disabled) {
4342                 priv->pm_on = 0;
4343         } else {
4344                 switch (prq->flags & IW_POWER_MODE) {
4345                 case IW_POWER_UNICAST_R:
4346                         priv->pm_mcast = 0;
4347                         priv->pm_on = 1;
4348                         break;
4349                 case IW_POWER_ALL_R:
4350                         priv->pm_mcast = 1;
4351                         priv->pm_on = 1;
4352                         break;
4353                 case IW_POWER_ON:
4354                         /* No flags : but we may have a value - Jean II */
4355                         break;
4356                 default:
4357                         err = -EINVAL;
4358                         goto out;
4359                 }
4360                 
4361                 if (prq->flags & IW_POWER_TIMEOUT) {
4362                         priv->pm_on = 1;
4363                         priv->pm_timeout = prq->value / 1000;
4364                 }
4365                 if (prq->flags & IW_POWER_PERIOD) {
4366                         priv->pm_on = 1;
4367                         priv->pm_period = prq->value / 1000;
4368                 }
4369                 /* It's valid to not have a value if we are just toggling
4370                  * the flags... Jean II */
4371                 if(!priv->pm_on) {
4372                         err = -EINVAL;
4373                         goto out;
4374                 }                       
4375         }
4376
4377  out:
4378         orinoco_unlock(priv, &flags);
4379
4380         return err;
4381 }
4382
4383 static int orinoco_ioctl_getpower(struct net_device *dev,
4384                                   struct iw_request_info *info,
4385                                   struct iw_param *prq,
4386                                   char *extra)
4387 {
4388         struct orinoco_private *priv = netdev_priv(dev);
4389         hermes_t *hw = &priv->hw;
4390         int err = 0;
4391         u16 enable, period, timeout, mcast;
4392         unsigned long flags;
4393
4394         if (orinoco_lock(priv, &flags) != 0)
4395                 return -EBUSY;
4396         
4397         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFPMENABLED, &enable);
4398         if (err)
4399                 goto out;
4400
4401         err = hermes_read_wordrec(hw, USER_BAP,
4402                                   HERMES_RID_CNFMAXSLEEPDURATION, &period);
4403         if (err)
4404                 goto out;
4405
4406         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFPMHOLDOVERDURATION, &timeout);
4407         if (err)
4408                 goto out;
4409
4410         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFMULTICASTRECEIVE, &mcast);
4411         if (err)
4412                 goto out;
4413
4414         prq->disabled = !enable;
4415         /* Note : by default, display the period */
4416         if ((prq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
4417                 prq->flags = IW_POWER_TIMEOUT;
4418                 prq->value = timeout * 1000;
4419         } else {
4420                 prq->flags = IW_POWER_PERIOD;
4421                 prq->value = period * 1000;
4422         }
4423         if (mcast)
4424                 prq->flags |= IW_POWER_ALL_R;
4425         else
4426                 prq->flags |= IW_POWER_UNICAST_R;
4427
4428  out:
4429         orinoco_unlock(priv, &flags);
4430
4431         return err;
4432 }
4433
4434 static int orinoco_ioctl_set_encodeext(struct net_device *dev,
4435                                        struct iw_request_info *info,
4436                                        union iwreq_data *wrqu,
4437                                        char *extra)
4438 {
4439         struct orinoco_private *priv = netdev_priv(dev);
4440         struct iw_point *encoding = &wrqu->encoding;
4441         struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
4442         int idx, alg = ext->alg, set_key = 1;
4443         unsigned long flags;
4444         int err = -EINVAL;
4445         u16 key_len;
4446
4447         if (orinoco_lock(priv, &flags) != 0)
4448                 return -EBUSY;
4449
4450         /* Determine and validate the key index */
4451         idx = encoding->flags & IW_ENCODE_INDEX;
4452         if (idx) {
4453                 if ((idx < 1) || (idx > WEP_KEYS))
4454                         goto out;
4455                 idx--;
4456         } else
4457                 idx = priv->tx_key;
4458
4459         if (encoding->flags & IW_ENCODE_DISABLED)
4460             alg = IW_ENCODE_ALG_NONE;
4461
4462         if (priv->has_wpa && (alg != IW_ENCODE_ALG_TKIP)) {
4463                 /* Clear any TKIP TX key we had */
4464                 (void) orinoco_clear_tkip_key(priv, priv->tx_key);
4465         }
4466
4467         if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
4468                 priv->tx_key = idx;
4469                 set_key = ((alg == IW_ENCODE_ALG_TKIP) ||
4470                            (ext->key_len > 0)) ? 1 : 0;
4471         }
4472
4473         if (set_key) {
4474                 /* Set the requested key first */
4475                 switch (alg) {
4476                 case IW_ENCODE_ALG_NONE:
4477                         priv->encode_alg = alg;
4478                         priv->keys[idx].len = 0;
4479                         break;
4480
4481                 case IW_ENCODE_ALG_WEP:
4482                         if (ext->key_len > SMALL_KEY_SIZE)
4483                                 key_len = LARGE_KEY_SIZE;
4484                         else if (ext->key_len > 0)
4485                                 key_len = SMALL_KEY_SIZE;
4486                         else
4487                                 goto out;
4488
4489                         priv->encode_alg = alg;
4490                         priv->keys[idx].len = cpu_to_le16(key_len);
4491
4492                         key_len = min(ext->key_len, key_len);
4493
4494                         memset(priv->keys[idx].data, 0, ORINOCO_MAX_KEY_SIZE);
4495                         memcpy(priv->keys[idx].data, ext->key, key_len);
4496                         break;
4497
4498                 case IW_ENCODE_ALG_TKIP:
4499                 {
4500                         hermes_t *hw = &priv->hw;
4501                         u8 *tkip_iv = NULL;
4502
4503                         if (!priv->has_wpa ||
4504                             (ext->key_len > sizeof(priv->tkip_key[0])))
4505                                 goto out;
4506
4507                         priv->encode_alg = alg;
4508                         memset(&priv->tkip_key[idx], 0,
4509                                sizeof(priv->tkip_key[idx]));
4510                         memcpy(&priv->tkip_key[idx], ext->key, ext->key_len);
4511
4512                         if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID)
4513                                 tkip_iv = &ext->rx_seq[0];
4514
4515                         err = __orinoco_hw_set_tkip_key(hw, idx,
4516                                  ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY,
4517                                  (u8 *) &priv->tkip_key[idx],
4518                                  tkip_iv, NULL);
4519                         if (err)
4520                                 printk(KERN_ERR "%s: Error %d setting TKIP key"
4521                                        "\n", dev->name, err);
4522
4523                         goto out;
4524                 }
4525                 default:
4526                         goto out;
4527                 }
4528         }
4529         err = -EINPROGRESS;
4530  out:
4531         orinoco_unlock(priv, &flags);
4532
4533         return err;
4534 }
4535
4536 static int orinoco_ioctl_get_encodeext(struct net_device *dev,
4537                                        struct iw_request_info *info,
4538                                        union iwreq_data *wrqu,
4539                                        char *extra)
4540 {
4541         struct orinoco_private *priv = netdev_priv(dev);
4542         struct iw_point *encoding = &wrqu->encoding;
4543         struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
4544         int idx, max_key_len;
4545         unsigned long flags;
4546         int err;
4547
4548         if (orinoco_lock(priv, &flags) != 0)
4549                 return -EBUSY;
4550
4551         err = -EINVAL;
4552         max_key_len = encoding->length - sizeof(*ext);
4553         if (max_key_len < 0)
4554                 goto out;
4555
4556         idx = encoding->flags & IW_ENCODE_INDEX;
4557         if (idx) {
4558                 if ((idx < 1) || (idx > WEP_KEYS))
4559                         goto out;
4560                 idx--;
4561         } else
4562                 idx = priv->tx_key;
4563
4564         encoding->flags = idx + 1;
4565         memset(ext, 0, sizeof(*ext));
4566
4567         ext->alg = priv->encode_alg;
4568         switch (priv->encode_alg) {
4569         case IW_ENCODE_ALG_NONE:
4570                 ext->key_len = 0;
4571                 encoding->flags |= IW_ENCODE_DISABLED;
4572                 break;
4573         case IW_ENCODE_ALG_WEP:
4574                 ext->key_len = min(le16_to_cpu(priv->keys[idx].len),
4575                                    (u16) max_key_len);
4576                 memcpy(ext->key, priv->keys[idx].data, ext->key_len);
4577                 encoding->flags |= IW_ENCODE_ENABLED;
4578                 break;
4579         case IW_ENCODE_ALG_TKIP:
4580                 ext->key_len = min((u16) sizeof(struct orinoco_tkip_key),
4581                                    (u16) max_key_len);
4582                 memcpy(ext->key, &priv->tkip_key[idx], ext->key_len);
4583                 encoding->flags |= IW_ENCODE_ENABLED;
4584                 break;
4585         }
4586
4587         err = 0;
4588  out:
4589         orinoco_unlock(priv, &flags);
4590
4591         return err;
4592 }
4593
4594 static int orinoco_ioctl_set_auth(struct net_device *dev,
4595                                   struct iw_request_info *info,
4596                                   union iwreq_data *wrqu, char *extra)
4597 {
4598         struct orinoco_private *priv = netdev_priv(dev);
4599         hermes_t *hw = &priv->hw;
4600         struct iw_param *param = &wrqu->param;
4601         unsigned long flags;
4602         int ret = -EINPROGRESS;
4603
4604         if (orinoco_lock(priv, &flags) != 0)
4605                 return -EBUSY;
4606
4607         switch (param->flags & IW_AUTH_INDEX) {
4608         case IW_AUTH_WPA_VERSION:
4609         case IW_AUTH_CIPHER_PAIRWISE:
4610         case IW_AUTH_CIPHER_GROUP:
4611         case IW_AUTH_RX_UNENCRYPTED_EAPOL:
4612         case IW_AUTH_PRIVACY_INVOKED:
4613         case IW_AUTH_DROP_UNENCRYPTED:
4614                 /*
4615                  * orinoco does not use these parameters
4616                  */
4617                 break;
4618
4619         case IW_AUTH_KEY_MGMT:
4620                 /* wl_lkm implies value 2 == PSK for Hermes I
4621                  * which ties in with WEXT
4622                  * no other hints tho :(
4623                  */
4624                 priv->key_mgmt = param->value;
4625                 break;
4626
4627         case IW_AUTH_TKIP_COUNTERMEASURES:
4628                 /* When countermeasures are enabled, shut down the
4629                  * card; when disabled, re-enable the card. This must
4630                  * take effect immediately.
4631                  *
4632                  * TODO: Make sure that the EAPOL message is getting
4633                  *       out before card disabled
4634                  */
4635                 if (param->value) {
4636                         priv->tkip_cm_active = 1;
4637                         ret = hermes_enable_port(hw, 0);
4638                 } else {
4639                         priv->tkip_cm_active = 0;
4640                         ret = hermes_disable_port(hw, 0);
4641                 }
4642                 break;
4643
4644         case IW_AUTH_80211_AUTH_ALG:
4645                 if (param->value & IW_AUTH_ALG_SHARED_KEY)
4646                         priv->wep_restrict = 1;
4647                 else if (param->value & IW_AUTH_ALG_OPEN_SYSTEM)
4648                         priv->wep_restrict = 0;
4649                 else
4650                         ret = -EINVAL;
4651                 break;
4652
4653         case IW_AUTH_WPA_ENABLED:
4654                 if (priv->has_wpa) {
4655                         priv->wpa_enabled = param->value ? 1 : 0;
4656                 } else {
4657                         if (param->value)
4658                                 ret = -EOPNOTSUPP;
4659                         /* else silently accept disable of WPA */
4660                         priv->wpa_enabled = 0;
4661                 }
4662                 break;
4663
4664         default:
4665                 ret = -EOPNOTSUPP;
4666         }
4667
4668         orinoco_unlock(priv, &flags);
4669         return ret;
4670 }
4671
4672 static int orinoco_ioctl_get_auth(struct net_device *dev,
4673                                   struct iw_request_info *info,
4674                                   union iwreq_data *wrqu, char *extra)
4675 {
4676         struct orinoco_private *priv = netdev_priv(dev);
4677         struct iw_param *param = &wrqu->param;
4678         unsigned long flags;
4679         int ret = 0;
4680
4681         if (orinoco_lock(priv, &flags) != 0)
4682                 return -EBUSY;
4683
4684         switch (param->flags & IW_AUTH_INDEX) {
4685         case IW_AUTH_KEY_MGMT:
4686                 param->value = priv->key_mgmt;
4687                 break;
4688
4689         case IW_AUTH_TKIP_COUNTERMEASURES:
4690                 param->value = priv->tkip_cm_active;
4691                 break;
4692
4693         case IW_AUTH_80211_AUTH_ALG:
4694                 if (priv->wep_restrict)
4695                         param->value = IW_AUTH_ALG_SHARED_KEY;
4696                 else
4697                         param->value = IW_AUTH_ALG_OPEN_SYSTEM;
4698                 break;
4699
4700         case IW_AUTH_WPA_ENABLED:
4701                 param->value = priv->wpa_enabled;
4702                 break;
4703
4704         default:
4705                 ret = -EOPNOTSUPP;
4706         }
4707
4708         orinoco_unlock(priv, &flags);
4709         return ret;
4710 }
4711
4712 static int orinoco_ioctl_set_genie(struct net_device *dev,
4713                                    struct iw_request_info *info,
4714                                    union iwreq_data *wrqu, char *extra)
4715 {
4716         struct orinoco_private *priv = netdev_priv(dev);
4717         u8 *buf;
4718         unsigned long flags;
4719         int err = 0;
4720
4721         if ((wrqu->data.length > MAX_WPA_IE_LEN) ||
4722             (wrqu->data.length && (extra == NULL)))
4723                 return -EINVAL;
4724
4725         if (orinoco_lock(priv, &flags) != 0)
4726                 return -EBUSY;
4727
4728         if (wrqu->data.length) {
4729                 buf = kmalloc(wrqu->data.length, GFP_KERNEL);
4730                 if (buf == NULL) {
4731                         err = -ENOMEM;
4732                         goto out;
4733                 }
4734
4735                 memcpy(buf, extra, wrqu->data.length);
4736                 kfree(priv->wpa_ie);
4737                 priv->wpa_ie = buf;
4738                 priv->wpa_ie_len = wrqu->data.length;
4739         } else {
4740                 kfree(priv->wpa_ie);
4741                 priv->wpa_ie = NULL;
4742                 priv->wpa_ie_len = 0;
4743         }
4744
4745         if (priv->wpa_ie) {
4746                 /* Looks like wl_lkm wants to check the auth alg, and
4747                  * somehow pass it to the firmware.
4748                  * Instead it just calls the key mgmt rid
4749                  *   - we do this in set auth.
4750                  */
4751         }
4752
4753 out:
4754         orinoco_unlock(priv, &flags);
4755         return err;
4756 }
4757
4758 static int orinoco_ioctl_get_genie(struct net_device *dev,
4759                                    struct iw_request_info *info,
4760                                    union iwreq_data *wrqu, char *extra)
4761 {
4762         struct orinoco_private *priv = netdev_priv(dev);
4763         unsigned long flags;
4764         int err = 0;
4765
4766         if (orinoco_lock(priv, &flags) != 0)
4767                 return -EBUSY;
4768
4769         if ((priv->wpa_ie_len == 0) || (priv->wpa_ie == NULL)) {
4770                 wrqu->data.length = 0;
4771                 goto out;
4772         }
4773
4774         if (wrqu->data.length < priv->wpa_ie_len) {
4775                 err = -E2BIG;
4776                 goto out;
4777         }
4778
4779         wrqu->data.length = priv->wpa_ie_len;
4780         memcpy(extra, priv->wpa_ie, priv->wpa_ie_len);
4781
4782 out:
4783         orinoco_unlock(priv, &flags);
4784         return err;
4785 }
4786
4787 static int orinoco_ioctl_set_mlme(struct net_device *dev,
4788                                   struct iw_request_info *info,
4789                                   union iwreq_data *wrqu, char *extra)
4790 {
4791         struct orinoco_private *priv = netdev_priv(dev);
4792         hermes_t *hw = &priv->hw;
4793         struct iw_mlme *mlme = (struct iw_mlme *)extra;
4794         unsigned long flags;
4795         int ret = 0;
4796
4797         if (orinoco_lock(priv, &flags) != 0)
4798                 return -EBUSY;
4799
4800         switch (mlme->cmd) {
4801         case IW_MLME_DEAUTH:
4802                 /* silently ignore */
4803                 break;
4804
4805         case IW_MLME_DISASSOC:
4806         {
4807                 struct {
4808                         u8 addr[ETH_ALEN];
4809                         __le16 reason_code;
4810                 } __attribute__ ((packed)) buf;
4811
4812                 memcpy(buf.addr, mlme->addr.sa_data, ETH_ALEN);
4813                 buf.reason_code = cpu_to_le16(mlme->reason_code);
4814                 ret = HERMES_WRITE_RECORD(hw, USER_BAP,
4815                                           HERMES_RID_CNFDISASSOCIATE,
4816                                           &buf);
4817                 break;
4818         }
4819         default:
4820                 ret = -EOPNOTSUPP;
4821         }
4822
4823         orinoco_unlock(priv, &flags);
4824         return ret;
4825 }
4826
4827 static int orinoco_ioctl_getretry(struct net_device *dev,
4828                                   struct iw_request_info *info,
4829                                   struct iw_param *rrq,
4830                                   char *extra)
4831 {
4832         struct orinoco_private *priv = netdev_priv(dev);
4833         hermes_t *hw = &priv->hw;
4834         int err = 0;
4835         u16 short_limit, long_limit, lifetime;
4836         unsigned long flags;
4837
4838         if (orinoco_lock(priv, &flags) != 0)
4839                 return -EBUSY;
4840         
4841         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_SHORTRETRYLIMIT,
4842                                   &short_limit);
4843         if (err)
4844                 goto out;
4845
4846         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_LONGRETRYLIMIT,
4847                                   &long_limit);
4848         if (err)
4849                 goto out;
4850
4851         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_MAXTRANSMITLIFETIME,
4852                                   &lifetime);
4853         if (err)
4854                 goto out;
4855
4856         rrq->disabled = 0;              /* Can't be disabled */
4857
4858         /* Note : by default, display the retry number */
4859         if ((rrq->flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME) {
4860                 rrq->flags = IW_RETRY_LIFETIME;
4861                 rrq->value = lifetime * 1000;   /* ??? */
4862         } else {
4863                 /* By default, display the min number */
4864                 if ((rrq->flags & IW_RETRY_LONG)) {
4865                         rrq->flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
4866                         rrq->value = long_limit;
4867                 } else {
4868                         rrq->flags = IW_RETRY_LIMIT;
4869                         rrq->value = short_limit;
4870                         if(short_limit != long_limit)
4871                                 rrq->flags |= IW_RETRY_SHORT;
4872                 }
4873         }
4874
4875  out:
4876         orinoco_unlock(priv, &flags);
4877
4878         return err;
4879 }
4880
4881 static int orinoco_ioctl_reset(struct net_device *dev,
4882                                struct iw_request_info *info,
4883                                void *wrqu,
4884                                char *extra)
4885 {
4886         struct orinoco_private *priv = netdev_priv(dev);
4887
4888         if (! capable(CAP_NET_ADMIN))
4889                 return -EPERM;
4890
4891         if (info->cmd == (SIOCIWFIRSTPRIV + 0x1)) {
4892                 printk(KERN_DEBUG "%s: Forcing reset!\n", dev->name);
4893
4894                 /* Firmware reset */
4895                 orinoco_reset(&priv->reset_work);
4896         } else {
4897                 printk(KERN_DEBUG "%s: Force scheduling reset!\n", dev->name);
4898
4899                 schedule_work(&priv->reset_work);
4900         }
4901
4902         return 0;
4903 }
4904
4905 static int orinoco_ioctl_setibssport(struct net_device *dev,
4906                                      struct iw_request_info *info,
4907                                      void *wrqu,
4908                                      char *extra)
4909
4910 {
4911         struct orinoco_private *priv = netdev_priv(dev);
4912         int val = *( (int *) extra );
4913         unsigned long flags;
4914
4915         if (orinoco_lock(priv, &flags) != 0)
4916                 return -EBUSY;
4917
4918         priv->ibss_port = val ;
4919
4920         /* Actually update the mode we are using */
4921         set_port_type(priv);
4922
4923         orinoco_unlock(priv, &flags);
4924         return -EINPROGRESS;            /* Call commit handler */
4925 }
4926
4927 static int orinoco_ioctl_getibssport(struct net_device *dev,
4928                                      struct iw_request_info *info,
4929                                      void *wrqu,
4930                                      char *extra)
4931 {
4932         struct orinoco_private *priv = netdev_priv(dev);
4933         int *val = (int *) extra;
4934
4935         *val = priv->ibss_port;
4936         return 0;
4937 }
4938
4939 static int orinoco_ioctl_setport3(struct net_device *dev,
4940                                   struct iw_request_info *info,
4941                                   void *wrqu,
4942                                   char *extra)
4943 {
4944         struct orinoco_private *priv = netdev_priv(dev);
4945         int val = *( (int *) extra );
4946         int err = 0;
4947         unsigned long flags;
4948
4949         if (orinoco_lock(priv, &flags) != 0)
4950                 return -EBUSY;
4951
4952         switch (val) {
4953         case 0: /* Try to do IEEE ad-hoc mode */
4954                 if (! priv->has_ibss) {
4955                         err = -EINVAL;
4956                         break;
4957                 }
4958                 priv->prefer_port3 = 0;
4959                         
4960                 break;
4961
4962         case 1: /* Try to do Lucent proprietary ad-hoc mode */
4963                 if (! priv->has_port3) {
4964                         err = -EINVAL;
4965                         break;
4966                 }
4967                 priv->prefer_port3 = 1;
4968                 break;
4969
4970         default:
4971                 err = -EINVAL;
4972         }
4973
4974         if (! err) {
4975                 /* Actually update the mode we are using */
4976                 set_port_type(priv);
4977                 err = -EINPROGRESS;
4978         }
4979
4980         orinoco_unlock(priv, &flags);
4981
4982         return err;
4983 }
4984
4985 static int orinoco_ioctl_getport3(struct net_device *dev,
4986                                   struct iw_request_info *info,
4987                                   void *wrqu,
4988                                   char *extra)
4989 {
4990         struct orinoco_private *priv = netdev_priv(dev);
4991         int *val = (int *) extra;
4992
4993         *val = priv->prefer_port3;
4994         return 0;
4995 }
4996
4997 static int orinoco_ioctl_setpreamble(struct net_device *dev,
4998                                      struct iw_request_info *info,
4999                                      void *wrqu,
5000                                      char *extra)
5001 {
5002         struct orinoco_private *priv = netdev_priv(dev);
5003         unsigned long flags;
5004         int val;
5005
5006         if (! priv->has_preamble)
5007                 return -EOPNOTSUPP;
5008
5009         /* 802.11b has recently defined some short preamble.
5010          * Basically, the Phy header has been reduced in size.
5011          * This increase performance, especially at high rates
5012          * (the preamble is transmitted at 1Mb/s), unfortunately
5013          * this give compatibility troubles... - Jean II */
5014         val = *( (int *) extra );
5015
5016         if (orinoco_lock(priv, &flags) != 0)
5017                 return -EBUSY;
5018
5019         if (val)
5020                 priv->preamble = 1;
5021         else
5022                 priv->preamble = 0;
5023
5024         orinoco_unlock(priv, &flags);
5025
5026         return -EINPROGRESS;            /* Call commit handler */
5027 }
5028
5029 static int orinoco_ioctl_getpreamble(struct net_device *dev,
5030                                      struct iw_request_info *info,
5031                                      void *wrqu,
5032                                      char *extra)
5033 {
5034         struct orinoco_private *priv = netdev_priv(dev);
5035         int *val = (int *) extra;
5036
5037         if (! priv->has_preamble)
5038                 return -EOPNOTSUPP;
5039
5040         *val = priv->preamble;
5041         return 0;
5042 }
5043
5044 /* ioctl interface to hermes_read_ltv()
5045  * To use with iwpriv, pass the RID as the token argument, e.g.
5046  * iwpriv get_rid [0xfc00]
5047  * At least Wireless Tools 25 is required to use iwpriv.
5048  * For Wireless Tools 25 and 26 append "dummy" are the end. */
5049 static int orinoco_ioctl_getrid(struct net_device *dev,
5050                                 struct iw_request_info *info,
5051                                 struct iw_point *data,
5052                                 char *extra)
5053 {
5054         struct orinoco_private *priv = netdev_priv(dev);
5055         hermes_t *hw = &priv->hw;
5056         int rid = data->flags;
5057         u16 length;
5058         int err;
5059         unsigned long flags;
5060
5061         /* It's a "get" function, but we don't want users to access the
5062          * WEP key and other raw firmware data */
5063         if (! capable(CAP_NET_ADMIN))
5064                 return -EPERM;
5065
5066         if (rid < 0xfc00 || rid > 0xffff)
5067                 return -EINVAL;
5068
5069         if (orinoco_lock(priv, &flags) != 0)
5070                 return -EBUSY;
5071
5072         err = hermes_read_ltv(hw, USER_BAP, rid, MAX_RID_LEN, &length,
5073                               extra);
5074         if (err)
5075                 goto out;
5076
5077         data->length = min_t(u16, HERMES_RECLEN_TO_BYTES(length),
5078                              MAX_RID_LEN);
5079
5080  out:
5081         orinoco_unlock(priv, &flags);
5082         return err;
5083 }
5084
5085 /* Trigger a scan (look for other cells in the vicinity) */
5086 static int orinoco_ioctl_setscan(struct net_device *dev,
5087                                  struct iw_request_info *info,
5088                                  struct iw_param *srq,
5089                                  char *extra)
5090 {
5091         struct orinoco_private *priv = netdev_priv(dev);
5092         hermes_t *hw = &priv->hw;
5093         struct iw_scan_req *si = (struct iw_scan_req *) extra;
5094         int err = 0;
5095         unsigned long flags;
5096
5097         /* Note : you may have realised that, as this is a SET operation,
5098          * this is privileged and therefore a normal user can't
5099          * perform scanning.
5100          * This is not an error, while the device perform scanning,
5101          * traffic doesn't flow, so it's a perfect DoS...
5102          * Jean II */
5103
5104         if (orinoco_lock(priv, &flags) != 0)
5105                 return -EBUSY;
5106
5107         /* Scanning with port 0 disabled would fail */
5108         if (!netif_running(dev)) {
5109                 err = -ENETDOWN;
5110                 goto out;
5111         }
5112
5113         /* In monitor mode, the scan results are always empty.
5114          * Probe responses are passed to the driver as received
5115          * frames and could be processed in software. */
5116         if (priv->iw_mode == IW_MODE_MONITOR) {
5117                 err = -EOPNOTSUPP;
5118                 goto out;
5119         }
5120
5121         /* Note : because we don't lock out the irq handler, the way
5122          * we access scan variables in priv is critical.
5123          *      o scan_inprogress : not touched by irq handler
5124          *      o scan_mode : not touched by irq handler
5125          * Before modifying anything on those variables, please think hard !
5126          * Jean II */
5127
5128         /* Save flags */
5129         priv->scan_mode = srq->flags;
5130
5131         /* Always trigger scanning, even if it's in progress.
5132          * This way, if the info frame get lost, we will recover somewhat
5133          * gracefully  - Jean II */
5134
5135         if (priv->has_hostscan) {
5136                 switch (priv->firmware_type) {
5137                 case FIRMWARE_TYPE_SYMBOL:
5138                         err = hermes_write_wordrec(hw, USER_BAP,
5139                                                    HERMES_RID_CNFHOSTSCAN_SYMBOL,
5140                                                    HERMES_HOSTSCAN_SYMBOL_ONCE |
5141                                                    HERMES_HOSTSCAN_SYMBOL_BCAST);
5142                         break;
5143                 case FIRMWARE_TYPE_INTERSIL: {
5144                         __le16 req[3];
5145
5146                         req[0] = cpu_to_le16(0x3fff);   /* All channels */
5147                         req[1] = cpu_to_le16(0x0001);   /* rate 1 Mbps */
5148                         req[2] = 0;                     /* Any ESSID */
5149                         err = HERMES_WRITE_RECORD(hw, USER_BAP,
5150                                                   HERMES_RID_CNFHOSTSCAN, &req);
5151                 }
5152                 break;
5153                 case FIRMWARE_TYPE_AGERE:
5154                         if (priv->scan_mode & IW_SCAN_THIS_ESSID) {
5155                                 struct hermes_idstring idbuf;
5156                                 size_t len = min(sizeof(idbuf.val),
5157                                                  (size_t) si->essid_len);
5158                                 idbuf.len = cpu_to_le16(len);
5159                                 memcpy(idbuf.val, si->essid, len);
5160
5161                                 err = hermes_write_ltv(hw, USER_BAP,
5162                                                HERMES_RID_CNFSCANSSID_AGERE,
5163                                                HERMES_BYTES_TO_RECLEN(len + 2),
5164                                                &idbuf);
5165                         } else
5166                                 err = hermes_write_wordrec(hw, USER_BAP,
5167                                                    HERMES_RID_CNFSCANSSID_AGERE,
5168                                                    0);  /* Any ESSID */
5169                         if (err)
5170                                 break;
5171
5172                         if (priv->has_ext_scan) {
5173                                 /* Clear scan results at the start of
5174                                  * an extended scan */
5175                                 orinoco_clear_scan_results(priv,
5176                                                 msecs_to_jiffies(15000));
5177
5178                                 /* TODO: Is this available on older firmware?
5179                                  *   Can we use it to scan specific channels
5180                                  *   for IW_SCAN_THIS_FREQ? */
5181                                 err = hermes_write_wordrec(hw, USER_BAP,
5182                                                 HERMES_RID_CNFSCANCHANNELS2GHZ,
5183                                                 0x7FFF);
5184                                 if (err)
5185                                         goto out;
5186
5187                                 err = hermes_inquire(hw,
5188                                                      HERMES_INQ_CHANNELINFO);
5189                         } else
5190                                 err = hermes_inquire(hw, HERMES_INQ_SCAN);
5191                         break;
5192                 }
5193         } else
5194                 err = hermes_inquire(hw, HERMES_INQ_SCAN);
5195
5196         /* One more client */
5197         if (! err)
5198                 priv->scan_inprogress = 1;
5199
5200  out:
5201         orinoco_unlock(priv, &flags);
5202         return err;
5203 }
5204
5205 #define MAX_CUSTOM_LEN 64
5206
5207 /* Translate scan data returned from the card to a card independant
5208  * format that the Wireless Tools will understand - Jean II */
5209 static inline char *orinoco_translate_scan(struct net_device *dev,
5210                                            struct iw_request_info *info,
5211                                            char *current_ev,
5212                                            char *end_buf,
5213                                            union hermes_scan_info *bss,
5214                                            unsigned int last_scanned)
5215 {
5216         struct orinoco_private *priv = netdev_priv(dev);
5217         u16                     capabilities;
5218         u16                     channel;
5219         struct iw_event         iwe;            /* Temporary buffer */
5220         char custom[MAX_CUSTOM_LEN];
5221
5222         memset(&iwe, 0, sizeof(iwe));
5223
5224         /* First entry *MUST* be the AP MAC address */
5225         iwe.cmd = SIOCGIWAP;
5226         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
5227         memcpy(iwe.u.ap_addr.sa_data, bss->a.bssid, ETH_ALEN);
5228         current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5229                                           &iwe, IW_EV_ADDR_LEN);
5230
5231         /* Other entries will be displayed in the order we give them */
5232
5233         /* Add the ESSID */
5234         iwe.u.data.length = le16_to_cpu(bss->a.essid_len);
5235         if (iwe.u.data.length > 32)
5236                 iwe.u.data.length = 32;
5237         iwe.cmd = SIOCGIWESSID;
5238         iwe.u.data.flags = 1;
5239         current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5240                                           &iwe, bss->a.essid);
5241
5242         /* Add mode */
5243         iwe.cmd = SIOCGIWMODE;
5244         capabilities = le16_to_cpu(bss->a.capabilities);
5245         if (capabilities & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
5246                 if (capabilities & WLAN_CAPABILITY_ESS)
5247                         iwe.u.mode = IW_MODE_MASTER;
5248                 else
5249                         iwe.u.mode = IW_MODE_ADHOC;
5250                 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5251                                                   &iwe, IW_EV_UINT_LEN);
5252         }
5253
5254         channel = bss->s.channel;
5255         if ((channel >= 1) && (channel <= NUM_CHANNELS)) {
5256                 /* Add channel and frequency */
5257                 iwe.cmd = SIOCGIWFREQ;
5258                 iwe.u.freq.m = channel;
5259                 iwe.u.freq.e = 0;
5260                 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5261                                                   &iwe, IW_EV_FREQ_LEN);
5262
5263                 iwe.u.freq.m = channel_frequency[channel-1] * 100000;
5264                 iwe.u.freq.e = 1;
5265                 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5266                                                   &iwe, IW_EV_FREQ_LEN);
5267         }
5268
5269         /* Add quality statistics. level and noise in dB. No link quality */
5270         iwe.cmd = IWEVQUAL;
5271         iwe.u.qual.updated = IW_QUAL_DBM | IW_QUAL_QUAL_INVALID;
5272         iwe.u.qual.level = (__u8) le16_to_cpu(bss->a.level) - 0x95;
5273         iwe.u.qual.noise = (__u8) le16_to_cpu(bss->a.noise) - 0x95;
5274         /* Wireless tools prior to 27.pre22 will show link quality
5275          * anyway, so we provide a reasonable value. */
5276         if (iwe.u.qual.level > iwe.u.qual.noise)
5277                 iwe.u.qual.qual = iwe.u.qual.level - iwe.u.qual.noise;
5278         else
5279                 iwe.u.qual.qual = 0;
5280         current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5281                                           &iwe, IW_EV_QUAL_LEN);
5282
5283         /* Add encryption capability */
5284         iwe.cmd = SIOCGIWENCODE;
5285         if (capabilities & WLAN_CAPABILITY_PRIVACY)
5286                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
5287         else
5288                 iwe.u.data.flags = IW_ENCODE_DISABLED;
5289         iwe.u.data.length = 0;
5290         current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5291                                           &iwe, NULL);
5292
5293         /* Bit rate is not available in Lucent/Agere firmwares */
5294         if (priv->firmware_type != FIRMWARE_TYPE_AGERE) {
5295                 char *current_val = current_ev + iwe_stream_lcp_len(info);
5296                 int i;
5297                 int step;
5298
5299                 if (priv->firmware_type == FIRMWARE_TYPE_SYMBOL)
5300                         step = 2;
5301                 else
5302                         step = 1;
5303
5304                 iwe.cmd = SIOCGIWRATE;
5305                 /* Those two flags are ignored... */
5306                 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
5307                 /* Max 10 values */
5308                 for (i = 0; i < 10; i += step) {
5309                         /* NULL terminated */
5310                         if (bss->p.rates[i] == 0x0)
5311                                 break;
5312                         /* Bit rate given in 500 kb/s units (+ 0x80) */
5313                         iwe.u.bitrate.value =
5314                                 ((bss->p.rates[i] & 0x7f) * 500000);
5315                         current_val = iwe_stream_add_value(info, current_ev,
5316                                                            current_val,
5317                                                            end_buf, &iwe,
5318                                                            IW_EV_PARAM_LEN);
5319                 }
5320                 /* Check if we added any event */
5321                 if ((current_val - current_ev) > iwe_stream_lcp_len(info))
5322                         current_ev = current_val;
5323         }
5324
5325         /* Beacon interval */
5326         iwe.cmd = IWEVCUSTOM;
5327         iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5328                                      "bcn_int=%d",
5329                                      le16_to_cpu(bss->a.beacon_interv));
5330         if (iwe.u.data.length)
5331                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5332                                                   &iwe, custom);
5333
5334         /* Capabilites */
5335         iwe.cmd = IWEVCUSTOM;
5336         iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5337                                      "capab=0x%04x",
5338                                      capabilities);
5339         if (iwe.u.data.length)
5340                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5341                                                   &iwe, custom);
5342
5343         /* Add EXTRA: Age to display seconds since last beacon/probe response
5344          * for given network. */
5345         iwe.cmd = IWEVCUSTOM;
5346         iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5347                                      " Last beacon: %dms ago",
5348                                      jiffies_to_msecs(jiffies - last_scanned));
5349         if (iwe.u.data.length)
5350                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5351                                                   &iwe, custom);
5352
5353         return current_ev;
5354 }
5355
5356 static inline char *orinoco_translate_ext_scan(struct net_device *dev,
5357                                                struct iw_request_info *info,
5358                                                char *current_ev,
5359                                                char *end_buf,
5360                                                struct agere_ext_scan_info *bss,
5361                                                unsigned int last_scanned)
5362 {
5363         u16                     capabilities;
5364         u16                     channel;
5365         struct iw_event         iwe;            /* Temporary buffer */
5366         char custom[MAX_CUSTOM_LEN];
5367         u8 *ie;
5368
5369         memset(&iwe, 0, sizeof(iwe));
5370
5371         /* First entry *MUST* be the AP MAC address */
5372         iwe.cmd = SIOCGIWAP;
5373         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
5374         memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
5375         current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5376                                           &iwe, IW_EV_ADDR_LEN);
5377
5378         /* Other entries will be displayed in the order we give them */
5379
5380         /* Add the ESSID */
5381         ie = bss->data;
5382         iwe.u.data.length = ie[1];
5383         if (iwe.u.data.length) {
5384                 if (iwe.u.data.length > 32)
5385                         iwe.u.data.length = 32;
5386                 iwe.cmd = SIOCGIWESSID;
5387                 iwe.u.data.flags = 1;
5388                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5389                                                   &iwe, &ie[2]);
5390         }
5391
5392         /* Add mode */
5393         capabilities = le16_to_cpu(bss->capabilities);
5394         if (capabilities & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
5395                 iwe.cmd = SIOCGIWMODE;
5396                 if (capabilities & WLAN_CAPABILITY_ESS)
5397                         iwe.u.mode = IW_MODE_MASTER;
5398                 else
5399                         iwe.u.mode = IW_MODE_ADHOC;
5400                 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5401                                                   &iwe, IW_EV_UINT_LEN);
5402         }
5403
5404         ie = orinoco_get_ie(bss->data, sizeof(bss->data), MFIE_TYPE_DS_SET);
5405         channel = ie ? ie[2] : 0;
5406         if ((channel >= 1) && (channel <= NUM_CHANNELS)) {
5407                 /* Add channel and frequency */
5408                 iwe.cmd = SIOCGIWFREQ;
5409                 iwe.u.freq.m = channel;
5410                 iwe.u.freq.e = 0;
5411                 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5412                                                   &iwe, IW_EV_FREQ_LEN);
5413
5414                 iwe.u.freq.m = channel_frequency[channel-1] * 100000;
5415                 iwe.u.freq.e = 1;
5416                 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5417                                                   &iwe, IW_EV_FREQ_LEN);
5418         }
5419
5420         /* Add quality statistics. level and noise in dB. No link quality */
5421         iwe.cmd = IWEVQUAL;
5422         iwe.u.qual.updated = IW_QUAL_DBM | IW_QUAL_QUAL_INVALID;
5423         iwe.u.qual.level = bss->level - 0x95;
5424         iwe.u.qual.noise = bss->noise - 0x95;
5425         /* Wireless tools prior to 27.pre22 will show link quality
5426          * anyway, so we provide a reasonable value. */
5427         if (iwe.u.qual.level > iwe.u.qual.noise)
5428                 iwe.u.qual.qual = iwe.u.qual.level - iwe.u.qual.noise;
5429         else
5430                 iwe.u.qual.qual = 0;
5431         current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5432                                           &iwe, IW_EV_QUAL_LEN);
5433
5434         /* Add encryption capability */
5435         iwe.cmd = SIOCGIWENCODE;
5436         if (capabilities & WLAN_CAPABILITY_PRIVACY)
5437                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
5438         else
5439                 iwe.u.data.flags = IW_ENCODE_DISABLED;
5440         iwe.u.data.length = 0;
5441         current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5442                                           &iwe, NULL);
5443
5444         /* WPA IE */
5445         ie = orinoco_get_wpa_ie(bss->data, sizeof(bss->data));
5446         if (ie) {
5447                 iwe.cmd = IWEVGENIE;
5448                 iwe.u.data.length = ie[1] + 2;
5449                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5450                                                   &iwe, ie);
5451         }
5452
5453         /* RSN IE */
5454         ie = orinoco_get_ie(bss->data, sizeof(bss->data), MFIE_TYPE_RSN);
5455         if (ie) {
5456                 iwe.cmd = IWEVGENIE;
5457                 iwe.u.data.length = ie[1] + 2;
5458                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5459                                                   &iwe, ie);
5460         }
5461
5462         ie = orinoco_get_ie(bss->data, sizeof(bss->data), MFIE_TYPE_RATES);
5463         if (ie) {
5464                 char *p = current_ev + iwe_stream_lcp_len(info);
5465                 int i;
5466
5467                 iwe.cmd = SIOCGIWRATE;
5468                 /* Those two flags are ignored... */
5469                 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
5470
5471                 for (i = 2; i < (ie[1] + 2); i++) {
5472                         iwe.u.bitrate.value = ((ie[i] & 0x7F) * 500000);
5473                         p = iwe_stream_add_value(info, current_ev, p, end_buf,
5474                                                  &iwe, IW_EV_PARAM_LEN);
5475                 }
5476                 /* Check if we added any event */
5477                 if (p > (current_ev + iwe_stream_lcp_len(info)))
5478                         current_ev = p;
5479         }
5480
5481         /* Timestamp */
5482         iwe.cmd = IWEVCUSTOM;
5483         iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5484                                      "tsf=%016llx",
5485                                      le64_to_cpu(bss->timestamp));
5486         if (iwe.u.data.length)
5487                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5488                                                   &iwe, custom);
5489
5490         /* Beacon interval */
5491         iwe.cmd = IWEVCUSTOM;
5492         iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5493                                      "bcn_int=%d",
5494                                      le16_to_cpu(bss->beacon_interval));
5495         if (iwe.u.data.length)
5496                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5497                                                   &iwe, custom);
5498
5499         /* Capabilites */
5500         iwe.cmd = IWEVCUSTOM;
5501         iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5502                                      "capab=0x%04x",
5503                                      capabilities);
5504         if (iwe.u.data.length)
5505                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5506                                                   &iwe, custom);
5507
5508         /* Add EXTRA: Age to display seconds since last beacon/probe response
5509          * for given network. */
5510         iwe.cmd = IWEVCUSTOM;
5511         iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5512                                      " Last beacon: %dms ago",
5513                                      jiffies_to_msecs(jiffies - last_scanned));
5514         if (iwe.u.data.length)
5515                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5516                                                   &iwe, custom);
5517
5518         return current_ev;
5519 }
5520
5521 /* Return results of a scan */
5522 static int orinoco_ioctl_getscan(struct net_device *dev,
5523                                  struct iw_request_info *info,
5524                                  struct iw_point *srq,
5525                                  char *extra)
5526 {
5527         struct orinoco_private *priv = netdev_priv(dev);
5528         int err = 0;
5529         unsigned long flags;
5530         char *current_ev = extra;
5531
5532         if (orinoco_lock(priv, &flags) != 0)
5533                 return -EBUSY;
5534
5535         if (priv->scan_inprogress) {
5536                 /* Important note : we don't want to block the caller
5537                  * until results are ready for various reasons.
5538                  * First, managing wait queues is complex and racy.
5539                  * Second, we grab some rtnetlink lock before comming
5540                  * here (in dev_ioctl()).
5541                  * Third, we generate an Wireless Event, so the
5542                  * caller can wait itself on that - Jean II */
5543                 err = -EAGAIN;
5544                 goto out;
5545         }
5546
5547         if (priv->has_ext_scan) {
5548                 struct xbss_element *bss;
5549
5550                 list_for_each_entry(bss, &priv->bss_list, list) {
5551                         /* Translate this entry to WE format */
5552                         current_ev =
5553                                 orinoco_translate_ext_scan(dev, info,
5554                                                            current_ev,
5555                                                            extra + srq->length,
5556                                                            &bss->bss,
5557                                                            bss->last_scanned);
5558
5559                         /* Check if there is space for one more entry */
5560                         if ((extra + srq->length - current_ev)
5561                             <= IW_EV_ADDR_LEN) {
5562                                 /* Ask user space to try again with a
5563                                  * bigger buffer */
5564                                 err = -E2BIG;
5565                                 goto out;
5566                         }
5567                 }
5568
5569         } else {
5570                 struct bss_element *bss;
5571
5572                 list_for_each_entry(bss, &priv->bss_list, list) {
5573                         /* Translate this entry to WE format */
5574                         current_ev = orinoco_translate_scan(dev, info,
5575                                                             current_ev,
5576                                                             extra + srq->length,
5577                                                             &bss->bss,
5578                                                             bss->last_scanned);
5579
5580                         /* Check if there is space for one more entry */
5581                         if ((extra + srq->length - current_ev)
5582                             <= IW_EV_ADDR_LEN) {
5583                                 /* Ask user space to try again with a
5584                                  * bigger buffer */
5585                                 err = -E2BIG;
5586                                 goto out;
5587                         }
5588                 }
5589         }
5590
5591         srq->length = (current_ev - extra);
5592         srq->flags = (__u16) priv->scan_mode;
5593
5594 out:
5595         orinoco_unlock(priv, &flags);
5596         return err;
5597 }
5598
5599 /* Commit handler, called after set operations */
5600 static int orinoco_ioctl_commit(struct net_device *dev,
5601                                 struct iw_request_info *info,
5602                                 void *wrqu,
5603                                 char *extra)
5604 {
5605         struct orinoco_private *priv = netdev_priv(dev);
5606         struct hermes *hw = &priv->hw;
5607         unsigned long flags;
5608         int err = 0;
5609
5610         if (!priv->open)
5611                 return 0;
5612
5613         if (priv->broken_disableport) {
5614                 orinoco_reset(&priv->reset_work);
5615                 return 0;
5616         }
5617
5618         if (orinoco_lock(priv, &flags) != 0)
5619                 return err;
5620
5621         err = hermes_disable_port(hw, 0);
5622         if (err) {
5623                 printk(KERN_WARNING "%s: Unable to disable port "
5624                        "while reconfiguring card\n", dev->name);
5625                 priv->broken_disableport = 1;
5626                 goto out;
5627         }
5628
5629         err = __orinoco_program_rids(dev);
5630         if (err) {
5631                 printk(KERN_WARNING "%s: Unable to reconfigure card\n",
5632                        dev->name);
5633                 goto out;
5634         }
5635
5636         err = hermes_enable_port(hw, 0);
5637         if (err) {
5638                 printk(KERN_WARNING "%s: Unable to enable port while reconfiguring card\n",
5639                        dev->name);
5640                 goto out;
5641         }
5642
5643  out:
5644         if (err) {
5645                 printk(KERN_WARNING "%s: Resetting instead...\n", dev->name);
5646                 schedule_work(&priv->reset_work);
5647                 err = 0;
5648         }
5649
5650         orinoco_unlock(priv, &flags);
5651         return err;
5652 }
5653
5654 static const struct iw_priv_args orinoco_privtab[] = {
5655         { SIOCIWFIRSTPRIV + 0x0, 0, 0, "force_reset" },
5656         { SIOCIWFIRSTPRIV + 0x1, 0, 0, "card_reset" },
5657         { SIOCIWFIRSTPRIV + 0x2, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5658           0, "set_port3" },
5659         { SIOCIWFIRSTPRIV + 0x3, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5660           "get_port3" },
5661         { SIOCIWFIRSTPRIV + 0x4, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5662           0, "set_preamble" },
5663         { SIOCIWFIRSTPRIV + 0x5, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5664           "get_preamble" },
5665         { SIOCIWFIRSTPRIV + 0x6, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5666           0, "set_ibssport" },
5667         { SIOCIWFIRSTPRIV + 0x7, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5668           "get_ibssport" },
5669         { SIOCIWFIRSTPRIV + 0x9, 0, IW_PRIV_TYPE_BYTE | MAX_RID_LEN,
5670           "get_rid" },
5671 };
5672
5673
5674 /*
5675  * Structures to export the Wireless Handlers
5676  */
5677
5678 #define STD_IW_HANDLER(id, func) \
5679         [IW_IOCTL_IDX(id)] = (iw_handler) func
5680 static const iw_handler orinoco_handler[] = {
5681         STD_IW_HANDLER(SIOCSIWCOMMIT,   orinoco_ioctl_commit),
5682         STD_IW_HANDLER(SIOCGIWNAME,     orinoco_ioctl_getname),
5683         STD_IW_HANDLER(SIOCSIWFREQ,     orinoco_ioctl_setfreq),
5684         STD_IW_HANDLER(SIOCGIWFREQ,     orinoco_ioctl_getfreq),
5685         STD_IW_HANDLER(SIOCSIWMODE,     orinoco_ioctl_setmode),
5686         STD_IW_HANDLER(SIOCGIWMODE,     orinoco_ioctl_getmode),
5687         STD_IW_HANDLER(SIOCSIWSENS,     orinoco_ioctl_setsens),
5688         STD_IW_HANDLER(SIOCGIWSENS,     orinoco_ioctl_getsens),
5689         STD_IW_HANDLER(SIOCGIWRANGE,    orinoco_ioctl_getiwrange),
5690         STD_IW_HANDLER(SIOCSIWSPY,      iw_handler_set_spy),
5691         STD_IW_HANDLER(SIOCGIWSPY,      iw_handler_get_spy),
5692         STD_IW_HANDLER(SIOCSIWTHRSPY,   iw_handler_set_thrspy),
5693         STD_IW_HANDLER(SIOCGIWTHRSPY,   iw_handler_get_thrspy),
5694         STD_IW_HANDLER(SIOCSIWAP,       orinoco_ioctl_setwap),
5695         STD_IW_HANDLER(SIOCGIWAP,       orinoco_ioctl_getwap),
5696         STD_IW_HANDLER(SIOCSIWSCAN,     orinoco_ioctl_setscan),
5697         STD_IW_HANDLER(SIOCGIWSCAN,     orinoco_ioctl_getscan),
5698         STD_IW_HANDLER(SIOCSIWESSID,    orinoco_ioctl_setessid),
5699         STD_IW_HANDLER(SIOCGIWESSID,    orinoco_ioctl_getessid),
5700         STD_IW_HANDLER(SIOCSIWNICKN,    orinoco_ioctl_setnick),
5701         STD_IW_HANDLER(SIOCGIWNICKN,    orinoco_ioctl_getnick),
5702         STD_IW_HANDLER(SIOCSIWRATE,     orinoco_ioctl_setrate),
5703         STD_IW_HANDLER(SIOCGIWRATE,     orinoco_ioctl_getrate),
5704         STD_IW_HANDLER(SIOCSIWRTS,      orinoco_ioctl_setrts),
5705         STD_IW_HANDLER(SIOCGIWRTS,      orinoco_ioctl_getrts),
5706         STD_IW_HANDLER(SIOCSIWFRAG,     orinoco_ioctl_setfrag),
5707         STD_IW_HANDLER(SIOCGIWFRAG,     orinoco_ioctl_getfrag),
5708         STD_IW_HANDLER(SIOCGIWRETRY,    orinoco_ioctl_getretry),
5709         STD_IW_HANDLER(SIOCSIWENCODE,   orinoco_ioctl_setiwencode),
5710         STD_IW_HANDLER(SIOCGIWENCODE,   orinoco_ioctl_getiwencode),
5711         STD_IW_HANDLER(SIOCSIWPOWER,    orinoco_ioctl_setpower),
5712         STD_IW_HANDLER(SIOCGIWPOWER,    orinoco_ioctl_getpower),
5713         STD_IW_HANDLER(SIOCSIWGENIE,    orinoco_ioctl_set_genie),
5714         STD_IW_HANDLER(SIOCGIWGENIE,    orinoco_ioctl_get_genie),
5715         STD_IW_HANDLER(SIOCSIWMLME,     orinoco_ioctl_set_mlme),
5716         STD_IW_HANDLER(SIOCSIWAUTH,     orinoco_ioctl_set_auth),
5717         STD_IW_HANDLER(SIOCGIWAUTH,     orinoco_ioctl_get_auth),
5718         STD_IW_HANDLER(SIOCSIWENCODEEXT, orinoco_ioctl_set_encodeext),
5719         STD_IW_HANDLER(SIOCGIWENCODEEXT, orinoco_ioctl_get_encodeext),
5720 };
5721
5722
5723 /*
5724   Added typecasting since we no longer use iwreq_data -- Moustafa
5725  */
5726 static const iw_handler orinoco_private_handler[] = {
5727         [0] = (iw_handler) orinoco_ioctl_reset,
5728         [1] = (iw_handler) orinoco_ioctl_reset,
5729         [2] = (iw_handler) orinoco_ioctl_setport3,
5730         [3] = (iw_handler) orinoco_ioctl_getport3,
5731         [4] = (iw_handler) orinoco_ioctl_setpreamble,
5732         [5] = (iw_handler) orinoco_ioctl_getpreamble,
5733         [6] = (iw_handler) orinoco_ioctl_setibssport,
5734         [7] = (iw_handler) orinoco_ioctl_getibssport,
5735         [9] = (iw_handler) orinoco_ioctl_getrid,
5736 };
5737
5738 static const struct iw_handler_def orinoco_handler_def = {
5739         .num_standard = ARRAY_SIZE(orinoco_handler),
5740         .num_private = ARRAY_SIZE(orinoco_private_handler),
5741         .num_private_args = ARRAY_SIZE(orinoco_privtab),
5742         .standard = orinoco_handler,
5743         .private = orinoco_private_handler,
5744         .private_args = orinoco_privtab,
5745         .get_wireless_stats = orinoco_get_wireless_stats,
5746 };
5747
5748 static void orinoco_get_drvinfo(struct net_device *dev,
5749                                 struct ethtool_drvinfo *info)
5750 {
5751         struct orinoco_private *priv = netdev_priv(dev);
5752
5753         strncpy(info->driver, DRIVER_NAME, sizeof(info->driver) - 1);
5754         strncpy(info->version, DRIVER_VERSION, sizeof(info->version) - 1);
5755         strncpy(info->fw_version, priv->fw_name, sizeof(info->fw_version) - 1);
5756         if (dev->dev.parent)
5757                 strncpy(info->bus_info, dev->dev.parent->bus_id,
5758                         sizeof(info->bus_info) - 1);
5759         else
5760                 snprintf(info->bus_info, sizeof(info->bus_info) - 1,
5761                          "PCMCIA %p", priv->hw.iobase);
5762 }
5763
5764 static const struct ethtool_ops orinoco_ethtool_ops = {
5765         .get_drvinfo = orinoco_get_drvinfo,
5766         .get_link = ethtool_op_get_link,
5767 };
5768
5769 /********************************************************************/
5770 /* Module initialization                                            */
5771 /********************************************************************/
5772
5773 EXPORT_SYMBOL(alloc_orinocodev);
5774 EXPORT_SYMBOL(free_orinocodev);
5775
5776 EXPORT_SYMBOL(__orinoco_up);
5777 EXPORT_SYMBOL(__orinoco_down);
5778 EXPORT_SYMBOL(orinoco_reinit_firmware);
5779
5780 EXPORT_SYMBOL(orinoco_interrupt);
5781
5782 /* Can't be declared "const" or the whole __initdata section will
5783  * become const */
5784 static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
5785         " (David Gibson <hermes@gibson.dropbear.id.au>, "
5786         "Pavel Roskin <proski@gnu.org>, et al)";
5787
5788 static int __init init_orinoco(void)
5789 {
5790         printk(KERN_DEBUG "%s\n", version);
5791         return 0;
5792 }
5793
5794 static void __exit exit_orinoco(void)
5795 {
5796 }
5797
5798 module_init(init_orinoco);
5799 module_exit(exit_orinoco);