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