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