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