vserver 1.9.5.x5
[linux-2.6.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  * v0.01 -> v0.02 - 21/3/2001 - Jean II
50  *      o Allow to use regular ethX device name instead of dldwdX
51  *      o Warning on IBSS with ESSID=any for firmware 6.06
52  *      o Put proper range.throughput values (optimistic)
53  *      o IWSPY support (IOCTL and stat gather in Rx path)
54  *      o Allow setting frequency in Ad-Hoc mode
55  *      o Disable WEP setting if !has_wep to work on old firmware
56  *      o Fix txpower range
57  *      o Start adding support for Samsung/Compaq firmware
58  *
59  * v0.02 -> v0.03 - 23/3/2001 - Jean II
60  *      o Start adding Symbol support - need to check all that
61  *      o Fix Prism2/Symbol WEP to accept 128 bits keys
62  *      o Add Symbol WEP (add authentication type)
63  *      o Add Prism2/Symbol rate
64  *      o Add PM timeout (holdover duration)
65  *      o Enable "iwconfig eth0 key off" and friends (toggle flags)
66  *      o Enable "iwconfig eth0 power unicast/all" (toggle flags)
67  *      o Try with an Intel card. It report firmware 1.01, behave like
68  *        an antiquated firmware, however on windows it says 2.00. Yuck !
69  *      o Workaround firmware bug in allocate buffer (Intel 1.01)
70  *      o Finish external renaming to orinoco...
71  *      o Testing with various Wavelan firmwares
72  *
73  * v0.03 -> v0.04 - 30/3/2001 - Jean II
74  *      o Update to Wireless 11 -> add retry limit/lifetime support
75  *      o Tested with a D-Link DWL 650 card, fill in firmware support
76  *      o Warning on Vcc mismatch (D-Link 3.3v card in Lucent 5v only slot)
77  *      o Fixed the Prism2 WEP bugs that I introduced in v0.03 :-(
78  *        It works on D-Link *only* after a tcpdump. Weird...
79  *        And still doesn't work on Intel card. Grrrr...
80  *      o Update the mode after a setport3
81  *      o Add preamble setting for Symbol cards (not yet enabled)
82  *      o Don't complain as much about Symbol cards...
83  *
84  * v0.04 -> v0.04b - 22/4/2001 - David Gibson
85  *      o Removed the 'eth' parameter - always use ethXX as the
86  *        interface name instead of dldwdXX.  The other was racy
87  *        anyway.
88  *      o Clean up RID definitions in hermes.h, other cleanups
89  *
90  * v0.04b -> v0.04c - 24/4/2001 - Jean II
91  *      o Tim Hurley <timster AT seiki.bliztech.com> reported a D-Link card
92  *        with vendor 02 and firmware 0.08. Added in the capabilities...
93  *      o Tested Lucent firmware 7.28, everything works...
94  *
95  * v0.04c -> v0.05 - 3/5/2001 - Benjamin Herrenschmidt
96  *      o Spin-off Pcmcia code. This file is renamed orinoco.c,
97  *        and orinoco_cs.c now contains only the Pcmcia specific stuff
98  *      o Add Airport driver support on top of orinoco.c (see airport.c)
99  *
100  * v0.05 -> v0.05a - 4/5/2001 - Jean II
101  *      o Revert to old Pcmcia code to fix breakage of Ben's changes...
102  *
103  * v0.05a -> v0.05b - 4/5/2001 - Jean II
104  *      o add module parameter 'ignore_cis_vcc' for D-Link @ 5V
105  *      o D-Link firmware doesn't support multicast. We just print a few
106  *        error messages, but otherwise everything works...
107  *      o For David : set/getport3 works fine, just upgrade iwpriv...
108  *
109  * v0.05b -> v0.05c - 5/5/2001 - Benjamin Herrenschmidt
110  *      o Adapt airport.c to latest changes in orinoco.c
111  *      o Remove deferred power enabling code
112  *
113  * v0.05c -> v0.05d - 5/5/2001 - Jean II
114  *      o Workaround to SNAP decapsulate frame from Linksys AP
115  *        original patch from : Dong Liu <dliu AT research.bell-labs.com>
116  *        (note : the memcmp bug was mine - fixed)
117  *      o Remove set_retry stuff, no firmware support it (bloat--).
118  *
119  * v0.05d -> v0.06 - 25/5/2001 - Jean II
120  *              Original patch from "Hong Lin" <alin AT redhat.com>,
121  *              "Ian Kinner" <ikinner AT redhat.com>
122  *              and "David Smith" <dsmith AT redhat.com>
123  *      o Init of priv->tx_rate_ctrl in firmware specific section.
124  *      o Prism2/Symbol rate, upto should be 0xF and not 0x15. Doh !
125  *      o Spectrum card always need cor_reset (for every reset)
126  *      o Fix cor_reset to not lose bit 7 in the register
127  *      o flush_stale_links to remove zombie Pcmcia instances
128  *      o Ack previous hermes event before reset
129  *              Me (with my little hands)
130  *      o Allow orinoco.c to call cor_reset via priv->card_reset_handler
131  *      o Add priv->need_card_reset to toggle this feature
132  *      o Fix various buglets when setting WEP in Symbol firmware
133  *        Now, encryption is fully functional on Symbol cards. Youpi !
134  *
135  * v0.06 -> v0.06b - 25/5/2001 - Jean II
136  *      o IBSS on Symbol use port_mode = 4. Please don't ask...
137  *
138  * v0.06b -> v0.06c - 29/5/2001 - Jean II
139  *      o Show first spy address in /proc/net/wireless for IBSS mode as well
140  *
141  * v0.06c -> v0.06d - 6/7/2001 - David Gibson
142  *      o Change a bunch of KERN_INFO messages to KERN_DEBUG, as per Linus'
143  *        wishes to reduce the number of unnecessary messages.
144  *      o Removed bogus message on CRC error.
145  *      o Merged fixes for v0.08 Prism 2 firmware from William Waghorn
146  *        <willwaghorn AT yahoo.co.uk>
147  *      o Slight cleanup/re-arrangement of firmware detection code.
148  *
149  * v0.06d -> v0.06e - 1/8/2001 - David Gibson
150  *      o Removed some redundant global initializers (orinoco_cs.c).
151  *      o Added some module metadata
152  *
153  * v0.06e -> v0.06f - 14/8/2001 - David Gibson
154  *      o Wording fix to license
155  *      o Added a 'use_alternate_encaps' module parameter for APs which need an
156  *        oui of 00:00:00.  We really need a better way of handling this, but
157  *        the module flag is better than nothing for now.
158  *
159  * v0.06f -> v0.07 - 20/8/2001 - David Gibson
160  *      o Removed BAP error retries from hermes_bap_seek().  For Tx we now
161  *        let the upper layers handle the retry, we retry explicitly in the
162  *        Rx path, but don't make as much noise about it.
163  *      o Firmware detection cleanups.
164  *
165  * v0.07 -> v0.07a - 1/10/3001 - Jean II
166  *      o Add code to read Symbol firmware revision, inspired by latest code
167  *        in Spectrum24 by Lee John Keyser-Allen - Thanks Lee !
168  *      o Thanks to Jared Valentine <hidden AT xmission.com> for "providing" me
169  *        a 3Com card with a recent firmware, fill out Symbol firmware
170  *        capabilities of latest rev (2.20), as well as older Symbol cards.
171  *      o Disable Power Management in newer Symbol firmware, the API 
172  *        has changed (documentation needed).
173  *
174  * v0.07a -> v0.08 - 3/10/2001 - David Gibson
175  *      o Fixed a possible buffer overrun found by the Stanford checker (in
176  *        dldwd_ioctl_setiwencode()).  Can only be called by root anyway, so not
177  *        a big problem.
178  *      o Turned has_big_wep on for Intersil cards.  That's not true for all of
179  *        them but we should at least let the capable ones try.
180  *      o Wait for BUSY to clear at the beginning of hermes_bap_seek().  I
181  *        realized that my assumption that the driver's serialization
182  *        would prevent the BAP being busy on entry was possibly false, because
183  *        things other than seeks may make the BAP busy.
184  *      o Use "alternate" (oui 00:00:00) encapsulation by default.
185  *        Setting use_old_encaps will mimic the old behaviour, but I think we
186  *        will be able to eliminate this.
187  *      o Don't try to make __initdata const (the version string).  This can't
188  *        work because of the way the __initdata sectioning works.
189  *      o Added MODULE_LICENSE tags.
190  *      o Support for PLX (transparent PCMCIA->PCI bridge) cards.
191  *      o Changed to using the new type-fascist min/max.
192  *
193  * v0.08 -> v0.08a - 9/10/2001 - David Gibson
194  *      o Inserted some missing acknowledgements/info into the Changelog.
195  *      o Fixed some bugs in the normalization of signal level reporting.
196  *      o Fixed bad bug in WEP key handling on Intersil and Symbol firmware,
197  *        which led to an instant crash on big-endian machines.
198  *
199  * v0.08a -> v0.08b - 20/11/2001 - David Gibson
200  *      o Lots of cleanup and bugfixes in orinoco_plx.c
201  *      o Cleanup to handling of Tx rate setting.
202  *      o Removed support for old encapsulation method.
203  *      o Removed old "dldwd" names.
204  *      o Split RID constants into a new file hermes_rid.h
205  *      o Renamed RID constants to match linux-wlan-ng and prism2.o
206  *      o Bugfixes in hermes.c
207  *      o Poke the PLX's INTCSR register, so it actually starts
208  *        generating interrupts.  These cards might actually work now.
209  *      o Update to wireless extensions v12 (Jean II)
210  *      o Support for tallies and inquire command (Jean II)
211  *      o Airport updates for newer PPC kernels (BenH)
212  *
213  * v0.08b -> v0.09 - 21/12/2001 - David Gibson
214  *      o Some new PCI IDs for PLX cards.
215  *      o Removed broken attempt to do ALLMULTI reception.  Just use
216  *        promiscuous mode instead
217  *      o Preliminary work for list-AP (Jean II)
218  *      o Airport updates from (BenH)
219  *      o Eliminated racy hw_ready stuff
220  *      o Fixed generation of fake events in irq handler.  This should
221  *        finally kill the EIO problems (Jean II & dgibson)
222  *      o Fixed breakage of bitrate set/get on Agere firmware (Jean II)
223  *
224  * v0.09 -> v0.09a - 2/1/2002 - David Gibson
225  *      o Fixed stupid mistake in multicast list handling, triggering
226  *        a BUG()
227  *
228  * v0.09a -> v0.09b - 16/1/2002 - David Gibson
229  *      o Fixed even stupider mistake in new interrupt handling, which
230  *        seriously broke things on big-endian machines.
231  *      o Removed a bunch of redundant includes and exports.
232  *      o Removed a redundant MOD_{INC,DEC}_USE_COUNT pair in airport.c
233  *      o Don't attempt to do hardware level multicast reception on
234  *        Intersil firmware, just go promisc instead.
235  *      o Typo fixed in hermes_issue_cmd()
236  *      o Eliminated WIRELESS_SPY #ifdefs
237  *      o Status code reported on Tx exceptions
238  *      o Moved netif_wake_queue() from ALLOC interrupts to TX and TXEXC
239  *        interrupts, which should fix the timeouts we're seeing.
240  *
241  * v0.09b -> v0.10 - 25 Feb 2002 - David Gibson
242  *      o Removed nested structures used for header parsing, so the
243  *        driver should now work without hackery on ARM
244  *      o Fix for WEP handling on Intersil (Hawk Newton)
245  *      o Eliminated the /proc/hermes/ethXX/regs debugging file.  It
246  *        was never very useful.
247  *      o Make Rx errors less noisy.
248  *
249  * v0.10 -> v0.11 - 5 Apr 2002 - David Gibson
250  *      o Laid the groundwork in hermes.[ch] for devices which map
251  *        into PCI memory space rather than IO space.
252  *      o Fixed bug in multicast handling (cleared multicast list when
253  *        leaving promiscuous mode).
254  *      o Relegated Tx error messages to debug.
255  *      o Cleaned up / corrected handling of allocation lengths.
256  *      o Set OWNSSID in IBSS mode for WinXP interoperability (jimc).
257  *      o Change to using alloc_etherdev() for structure allocations. 
258  *      o Check for and drop undersized packets.
259  *      o Fixed a race in stopping/waking the queue.  This should fix
260  *        the timeout problems (Pavel Roskin)
261  *      o Reverted to netif_wake_queue() on the ALLOC event.
262  *      o Fixes for recent Symbol firmwares which lack AP density
263  *        (Pavel Roskin).
264  *
265  * v0.11 -> v0.11a - 29 Apr 2002 - David Gibson
266  *      o Handle different register spacing, necessary for Prism 2.5
267  *        PCI adaptors (Steve Hill).
268  *      o Cleaned up initialization of card structures in orinoco_cs
269  *        and airport.  Removed card->priv field.
270  *      o Make response structure optional for hermes_docmd_wait()
271  *        Pavel Roskin)
272  *      o Added PCI id for Nortel emobility to orinoco_plx.c.
273  *      o Cleanup to handling of Symbol's allocation bug. (Pavel Roskin)
274  *      o Cleanups to firmware capability detection.
275  *      o Arrange for orinoco_pci.c to override firmware detection.
276  *        We should be able to support the PCI Intersil cards now.
277  *      o Cleanup handling of reset_cor and hard_reset (Pavel Roskin).
278  *      o Remove erroneous use of USER_BAP in the TxExc handler (Jouni
279  *        Malinen).
280  *      o Makefile changes for better integration into David Hinds
281  *        pcmcia-cs package.
282  *
283  * v0.11a -> v0.11b - 1 May 2002 - David Gibson
284  *      o Better error reporting in orinoco_plx_init_one()
285  *      o Fixed multiple bad kfree() bugs introduced by the
286  *        alloc_orinocodev() changes.
287  *
288  * v0.11b -> v0.12 - 19 Jun 2002 - David Gibson
289  *      o Support changing the MAC address.
290  *      o Correct display of Intersil firmware revision numbers.
291  *      o Entirely revised locking scheme.  Should be both simpler and
292  *         better.
293  *      o Merged some common code in orinoco_plx, orinoco_pci and
294  *        airport by creating orinoco_default_{open,stop,reset}()
295  *        which are used as the dev->open, dev->stop, priv->reset
296  *        callbacks if none are specified when alloc_orinocodev() is
297  *        called.
298  *      o Removed orinoco_plx_interrupt() and orinoco_pci_interrupt().
299  *        They didn't do anything.
300  *
301  * v0.12 -> v0.12a - 4 Jul 2002 - David Gibson
302  *      o Some rearrangement of code.
303  *      o Numerous fixups to locking and rest handling, particularly
304  *        for PCMCIA.
305  *      o This allows open and stop net_device methods to be in
306  *        orinoco.c now, rather than in the init modules.
307  *      o In orinoco_cs.c link->priv now points to the struct
308  *        net_device not to the struct orinoco_private.
309  *      o Added a check for undersized SNAP frames, which could cause
310  *        crashes.
311  *
312  * v0.12a -> v0.12b - 11 Jul 2002 - David Gibson
313  *      o Fix hw->num_init testing code, so num_init is actually
314  *        incremented.
315  *      o Fix very stupid bug in orinoco_cs which broke compile with
316  *        CONFIG_SMP.
317  *      o Squashed a warning.
318  *
319  * v0.12b -> v0.12c - 26 Jul 2002 - David Gibson
320  *      o Change to C9X style designated initializers.
321  *      o Add support for 3Com AirConnect PCI.
322  *      o No longer ignore the hard_reset argument to
323  *        alloc_orinocodev().  Oops.
324  *
325  * v0.12c -> v0.13beta1 - 13 Sep 2002 - David Gibson
326  *      o Revert the broken 0.12* locking scheme and go to a new yet
327  *        simpler scheme.
328  *      o Do firmware resets only in orinoco_init() and when waking
329  *        the card from hard sleep.
330  *
331  * v0.13beta1 -> v0.13 - 27 Sep 2002 - David Gibson
332  *      o Re-introduced full resets (via schedule_task()) on Tx
333  *        timeout.
334  *
335  * v0.13 -> v0.13a - 30 Sep 2002 - David Gibson
336  *      o Minor cleanups to info frame handling.  Add basic support
337  *        for linkstatus info frames.
338  *      o Include required kernel headers in orinoco.h, to avoid
339  *        compile problems.
340  *
341  * v0.13a -> v0.13b - 10 Feb 2003 - David Gibson
342  *      o Implemented hard reset for Airport cards
343  *      o Experimental suspend/resume implementation for orinoco_pci
344  *      o Abolished /proc debugging support, replaced with a debugging
345  *        iwpriv.  Now it's ugly and simple instead of ugly and complex.
346  *      o Bugfix in hermes.c if the firmware returned a record length
347  *        of 0, we could go clobbering memory.
348  *      o Bugfix in orinoco_stop() - it used to fail if hw_unavailable
349  *        was set, which was usually true on PCMCIA hot removes.
350  *      o Track LINKSTATUS messages, silently drop Tx packets before
351  *        we are connected (avoids confusing the firmware), and only
352  *        give LINKSTATUS printk()s if the status has changed.
353  *
354  * v0.13b -> v0.13c - 11 Mar 2003 - David Gibson
355  *      o Cleanup: use dev instead of priv in various places.
356  *      o Bug fix: Don't ReleaseConfiguration on RESET_PHYSICAL event
357  *        if we're in the middle of a (driver initiated) hard reset.
358  *      o Bug fix: ETH_ZLEN is supposed to include the header
359  *        (Dionysus Blazakis & Manish Karir)
360  *      o Convert to using workqueues instead of taskqueues (and
361  *        backwards compatibility macros for pre 2.5.41 kernels).
362  *      o Drop redundant (I think...) MOD_{INC,DEC}_USE_COUNT in
363  *        airport.c
364  *      o New orinoco_tmd.c init module from Joerg Dorchain for
365  *        TMD7160 based PCI to PCMCIA bridges (similar to
366  *        orinoco_plx.c).
367  *
368  * v0.13c -> v0.13d - 22 Apr 2003 - David Gibson
369  *      o Make hw_unavailable a counter, rather than just a flag, this
370  *        is necessary to avoid some races (such as a card being
371  *        removed in the middle of orinoco_reset().
372  *      o Restore Release/RequestConfiguration in the PCMCIA event handler
373  *        when dealing with a driver initiated hard reset.  This is
374  *        necessary to prevent hangs due to a spurious interrupt while
375  *        the reset is in progress.
376  *      o Clear the 802.11 header when transmitting, even though we
377  *        don't use it.  This fixes a long standing bug on some
378  *        firmwares, which seem to get confused if that isn't done.
379  *      o Be less eager to de-encapsulate SNAP frames, only do so if
380  *        the OUI is 00:00:00 or 00:00:f8, leave others alone.  The old
381  *        behaviour broke CDP (Cisco Discovery Protocol).
382  *      o Use dev instead of priv for free_irq() as well as
383  *        request_irq() (oops).
384  *      o Attempt to reset rather than giving up if we get too many
385  *        IRQs.
386  *      o Changed semantics of __orinoco_down() so it can be called
387  *        safely with hw_unavailable set.  It also now clears the
388  *        linkstatus (since we're going to have to reassociate).
389  *
390  * v0.13d -> v0.13e - 12 May 2003 - David Gibson
391  *      o Support for post-2.5.68 return values from irq handler.
392  *      o Fixed bug where underlength packets would be double counted
393  *        in the rx_dropped statistics.
394  *      o Provided a module parameter to suppress linkstatus messages.
395  *
396  * TODO
397  *      o New wireless extensions API (patch from Moustafa
398  *        Youssef, updated by Jim Carter and Pavel Roskin).
399  *      o Handle de-encapsulation within network layer, provide 802.11
400  *        headers (patch from Thomas 'Dent' Mirlacher)
401  *      o RF monitor mode support
402  *      o Fix possible races in SPY handling.
403  *      o Disconnect wireless extensions from fundamental configuration.
404  *      o (maybe) Software WEP support (patch from Stano Meduna).
405  *      o (maybe) Use multiple Tx buffers - driver handling queue
406  *        rather than firmware.
407  */
408
409 /* Locking and synchronization:
410  *
411  * The basic principle is that everything is serialized through a
412  * single spinlock, priv->lock.  The lock is used in user, bh and irq
413  * context, so when taken outside hardirq context it should always be
414  * taken with interrupts disabled.  The lock protects both the
415  * hardware and the struct orinoco_private.
416  *
417  * Another flag, priv->hw_unavailable indicates that the hardware is
418  * unavailable for an extended period of time (e.g. suspended, or in
419  * the middle of a hard reset).  This flag is protected by the
420  * spinlock.  All code which touches the hardware should check the
421  * flag after taking the lock, and if it is set, give up on whatever
422  * they are doing and drop the lock again.  The orinoco_lock()
423  * function handles this (it unlocks and returns -EBUSY if
424  * hw_unavailable is non-zero).
425  */
426
427 #define DRIVER_NAME "orinoco"
428
429 #include <linux/config.h>
430
431 #include <linux/module.h>
432 #include <linux/kernel.h>
433 #include <linux/init.h>
434 #include <linux/ptrace.h>
435 #include <linux/slab.h>
436 #include <linux/string.h>
437 #include <linux/timer.h>
438 #include <linux/ioport.h>
439 #include <linux/netdevice.h>
440 #include <linux/if_arp.h>
441 #include <linux/etherdevice.h>
442 #include <linux/wireless.h>
443
444 #include <asm/uaccess.h>
445 #include <asm/io.h>
446 #include <asm/system.h>
447
448 #include "hermes.h"
449 #include "hermes_rid.h"
450 #include "orinoco.h"
451 #include "ieee802_11.h"
452
453 /********************************************************************/
454 /* Module information                                               */
455 /********************************************************************/
456
457 MODULE_AUTHOR("Pavel Roskin <proski@gnu.org> & David Gibson <hermes@gibson.dropbear.id.au>");
458 MODULE_DESCRIPTION("Driver for Lucent Orinoco, Prism II based and similar wireless cards");
459 MODULE_LICENSE("Dual MPL/GPL");
460
461 /* Level of debugging. Used in the macros in orinoco.h */
462 #ifdef ORINOCO_DEBUG
463 int orinoco_debug = ORINOCO_DEBUG;
464 module_param(orinoco_debug, int, 0);
465 EXPORT_SYMBOL(orinoco_debug);
466 #endif
467
468 static int suppress_linkstatus; /* = 0 */
469 module_param(suppress_linkstatus, bool, 0);
470
471 /********************************************************************/
472 /* Compile time configuration and compatibility stuff               */
473 /********************************************************************/
474
475 /* We do this this way to avoid ifdefs in the actual code */
476 #ifdef WIRELESS_SPY
477 #define SPY_NUMBER(priv)        (priv->spy_number)
478 #else
479 #define SPY_NUMBER(priv)        0
480 #endif /* WIRELESS_SPY */
481
482 /********************************************************************/
483 /* Internal constants                                               */
484 /********************************************************************/
485
486 #define ORINOCO_MIN_MTU         256
487 #define ORINOCO_MAX_MTU         (IEEE802_11_DATA_LEN - ENCAPS_OVERHEAD)
488
489 #define SYMBOL_MAX_VER_LEN      (14)
490 #define USER_BAP                0
491 #define IRQ_BAP                 1
492 #define MAX_IRQLOOPS_PER_IRQ    10
493 #define MAX_IRQLOOPS_PER_JIFFY  (20000/HZ) /* Based on a guestimate of
494                                             * how many events the
495                                             * device could
496                                             * legitimately generate */
497 #define SMALL_KEY_SIZE          5
498 #define LARGE_KEY_SIZE          13
499 #define TX_NICBUF_SIZE_BUG      1585            /* Bug in Symbol firmware */
500
501 #define DUMMY_FID               0xFFFF
502
503 /*#define MAX_MULTICAST(priv)   (priv->firmware_type == FIRMWARE_TYPE_AGERE ? \
504   HERMES_MAX_MULTICAST : 0)*/
505 #define MAX_MULTICAST(priv)     (HERMES_MAX_MULTICAST)
506
507 #define ORINOCO_INTEN           (HERMES_EV_RX | HERMES_EV_ALLOC \
508                                  | HERMES_EV_TX | HERMES_EV_TXEXC \
509                                  | HERMES_EV_WTERR | HERMES_EV_INFO \
510                                  | HERMES_EV_INFDROP )
511
512 /********************************************************************/
513 /* Data tables                                                      */
514 /********************************************************************/
515
516 /* The frequency of each channel in MHz */
517 static const long channel_frequency[] = {
518         2412, 2417, 2422, 2427, 2432, 2437, 2442,
519         2447, 2452, 2457, 2462, 2467, 2472, 2484
520 };
521 #define NUM_CHANNELS ARRAY_SIZE(channel_frequency)
522
523 /* This tables gives the actual meanings of the bitrate IDs returned
524  * by the firmware. */
525 static struct {
526         int bitrate; /* in 100s of kilobits */
527         int automatic;
528         u16 agere_txratectrl;
529         u16 intersil_txratectrl;
530 } bitrate_table[] = {
531         {110, 1,  3, 15}, /* Entry 0 is the default */
532         {10,  0,  1,  1},
533         {10,  1,  1,  1},
534         {20,  0,  2,  2},
535         {20,  1,  6,  3},
536         {55,  0,  4,  4},
537         {55,  1,  7,  7},
538         {110, 0,  5,  8},
539 };
540 #define BITRATE_TABLE_SIZE ARRAY_SIZE(bitrate_table)
541
542 /********************************************************************/
543 /* Data types                                                       */
544 /********************************************************************/
545
546 struct header_struct {
547         /* 802.3 */
548         u8 dest[ETH_ALEN];
549         u8 src[ETH_ALEN];
550         u16 len;
551         /* 802.2 */
552         u8 dsap;
553         u8 ssap;
554         u8 ctrl;
555         /* SNAP */
556         u8 oui[3];
557         u16 ethertype;
558 } __attribute__ ((packed));
559
560 /* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
561 u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
562
563 #define ENCAPS_OVERHEAD         (sizeof(encaps_hdr) + 2)
564
565 struct hermes_rx_descriptor {
566         u16 status;
567         u32 time;
568         u8 silence;
569         u8 signal;
570         u8 rate;
571         u8 rxflow;
572         u32 reserved;
573 } __attribute__ ((packed));
574
575 /********************************************************************/
576 /* Function prototypes                                              */
577 /********************************************************************/
578
579 static int orinoco_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
580 static int __orinoco_program_rids(struct net_device *dev);
581 static void __orinoco_set_multicast_list(struct net_device *dev);
582 static int orinoco_debug_dump_recs(struct net_device *dev);
583
584 /********************************************************************/
585 /* Internal helper functions                                        */
586 /********************************************************************/
587
588 static inline void set_port_type(struct orinoco_private *priv)
589 {
590         switch (priv->iw_mode) {
591         case IW_MODE_INFRA:
592                 priv->port_type = 1;
593                 priv->createibss = 0;
594                 break;
595         case IW_MODE_ADHOC:
596                 if (priv->prefer_port3) {
597                         priv->port_type = 3;
598                         priv->createibss = 0;
599                 } else {
600                         priv->port_type = priv->ibss_port;
601                         priv->createibss = 1;
602                 }
603                 break;
604         default:
605                 printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
606                        priv->ndev->name);
607         }
608 }
609
610 /********************************************************************/
611 /* Device methods                                                   */
612 /********************************************************************/
613
614 static int orinoco_open(struct net_device *dev)
615 {
616         struct orinoco_private *priv = netdev_priv(dev);
617         unsigned long flags;
618         int err;
619
620         if (orinoco_lock(priv, &flags) != 0)
621                 return -EBUSY;
622
623         err = __orinoco_up(dev);
624
625         if (! err)
626                 priv->open = 1;
627
628         orinoco_unlock(priv, &flags);
629
630         return err;
631 }
632
633 int orinoco_stop(struct net_device *dev)
634 {
635         struct orinoco_private *priv = netdev_priv(dev);
636         int err = 0;
637
638         /* We mustn't use orinoco_lock() here, because we need to be
639            able to close the interface even if hw_unavailable is set
640            (e.g. as we're released after a PC Card removal) */
641         spin_lock_irq(&priv->lock);
642
643         priv->open = 0;
644
645         err = __orinoco_down(dev);
646
647         spin_unlock_irq(&priv->lock);
648
649         return err;
650 }
651
652 static struct net_device_stats *orinoco_get_stats(struct net_device *dev)
653 {
654         struct orinoco_private *priv = netdev_priv(dev);
655         
656         return &priv->stats;
657 }
658
659 static struct iw_statistics *orinoco_get_wireless_stats(struct net_device *dev)
660 {
661         struct orinoco_private *priv = netdev_priv(dev);
662         hermes_t *hw = &priv->hw;
663         struct iw_statistics *wstats = &priv->wstats;
664         int err = 0;
665         unsigned long flags;
666
667         if (! netif_device_present(dev)) {
668                 printk(KERN_WARNING "%s: get_wireless_stats() called while device not present\n",
669                        dev->name);
670                 return NULL; /* FIXME: Can we do better than this? */
671         }
672
673         if (orinoco_lock(priv, &flags) != 0)
674                 return NULL;  /* FIXME: Erg, we've been signalled, how
675                                * do we propagate this back up? */
676
677         if (priv->iw_mode == IW_MODE_ADHOC) {
678                 memset(&wstats->qual, 0, sizeof(wstats->qual));
679                 /* If a spy address is defined, we report stats of the
680                  * first spy address - Jean II */
681                 if (SPY_NUMBER(priv)) {
682                         wstats->qual.qual = priv->spy_stat[0].qual;
683                         wstats->qual.level = priv->spy_stat[0].level;
684                         wstats->qual.noise = priv->spy_stat[0].noise;
685                         wstats->qual.updated = priv->spy_stat[0].updated;
686                 }
687         } else {
688                 struct {
689                         u16 qual, signal, noise;
690                 } __attribute__ ((packed)) cq;
691
692                 err = HERMES_READ_RECORD(hw, USER_BAP,
693                                          HERMES_RID_COMMSQUALITY, &cq);
694                 
695                 wstats->qual.qual = (int)le16_to_cpu(cq.qual);
696                 wstats->qual.level = (int)le16_to_cpu(cq.signal) - 0x95;
697                 wstats->qual.noise = (int)le16_to_cpu(cq.noise) - 0x95;
698                 wstats->qual.updated = 7;
699         }
700
701         /* We can't really wait for the tallies inquiry command to
702          * complete, so we just use the previous results and trigger
703          * a new tallies inquiry command for next time - Jean II */
704         /* FIXME: We're in user context (I think?), so we should just
705            wait for the tallies to come through */
706         err = hermes_inquire(hw, HERMES_INQ_TALLIES);
707                
708         orinoco_unlock(priv, &flags);
709
710         if (err)
711                 return NULL;
712                 
713         return wstats;
714 }
715
716 static void orinoco_set_multicast_list(struct net_device *dev)
717 {
718         struct orinoco_private *priv = netdev_priv(dev);
719         unsigned long flags;
720
721         if (orinoco_lock(priv, &flags) != 0) {
722                 printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
723                        "called when hw_unavailable\n", dev->name);
724                 return;
725         }
726
727         __orinoco_set_multicast_list(dev);
728         orinoco_unlock(priv, &flags);
729 }
730
731 static int orinoco_change_mtu(struct net_device *dev, int new_mtu)
732 {
733         struct orinoco_private *priv = netdev_priv(dev);
734
735         if ( (new_mtu < ORINOCO_MIN_MTU) || (new_mtu > ORINOCO_MAX_MTU) )
736                 return -EINVAL;
737
738         if ( (new_mtu + ENCAPS_OVERHEAD + IEEE802_11_HLEN) >
739              (priv->nicbuf_size - ETH_HLEN) )
740                 return -EINVAL;
741
742         dev->mtu = new_mtu;
743
744         return 0;
745 }
746
747 /********************************************************************/
748 /* Tx path                                                          */
749 /********************************************************************/
750
751 static int orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
752 {
753         struct orinoco_private *priv = netdev_priv(dev);
754         struct net_device_stats *stats = &priv->stats;
755         hermes_t *hw = &priv->hw;
756         int err = 0;
757         u16 txfid = priv->txfid;
758         char *p;
759         struct ethhdr *eh;
760         int len, data_len, data_off;
761         struct hermes_tx_descriptor desc;
762         unsigned long flags;
763
764         TRACE_ENTER(dev->name);
765
766         if (! netif_running(dev)) {
767                 printk(KERN_ERR "%s: Tx on stopped device!\n",
768                        dev->name);
769                 TRACE_EXIT(dev->name);
770                 return 1;
771         }
772         
773         if (netif_queue_stopped(dev)) {
774                 printk(KERN_DEBUG "%s: Tx while transmitter busy!\n", 
775                        dev->name);
776                 TRACE_EXIT(dev->name);
777                 return 1;
778         }
779         
780         if (orinoco_lock(priv, &flags) != 0) {
781                 printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n",
782                        dev->name);
783                 TRACE_EXIT(dev->name);
784                 return 1;
785         }
786
787         if (! priv->connected) {
788                 /* Oops, the firmware hasn't established a connection,
789                    silently drop the packet (this seems to be the
790                    safest approach). */
791                 stats->tx_errors++;
792                 orinoco_unlock(priv, &flags);
793                 dev_kfree_skb(skb);
794                 TRACE_EXIT(dev->name);
795                 return 0;
796         }
797
798         /* Length of the packet body */
799         /* FIXME: what if the skb is smaller than this? */
800         len = max_t(int,skb->len - ETH_HLEN, ETH_ZLEN - ETH_HLEN);
801
802         eh = (struct ethhdr *)skb->data;
803
804         memset(&desc, 0, sizeof(desc));
805         desc.tx_control = cpu_to_le16(HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX);
806         err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc), txfid, 0);
807         if (err) {
808                 printk(KERN_ERR "%s: Error %d writing Tx descriptor to BAP\n",
809                        dev->name, err);
810                 stats->tx_errors++;
811                 goto fail;
812         }
813
814         /* Clear the 802.11 header and data length fields - some
815          * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
816          * if this isn't done. */
817         hermes_clear_words(hw, HERMES_DATA0,
818                            HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
819
820         /* Encapsulate Ethernet-II frames */
821         if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
822                 struct header_struct hdr;
823                 data_len = len;
824                 data_off = HERMES_802_3_OFFSET + sizeof(hdr);
825                 p = skb->data + ETH_HLEN;
826
827                 /* 802.3 header */
828                 memcpy(hdr.dest, eh->h_dest, ETH_ALEN);
829                 memcpy(hdr.src, eh->h_source, ETH_ALEN);
830                 hdr.len = htons(data_len + ENCAPS_OVERHEAD);
831                 
832                 /* 802.2 header */
833                 memcpy(&hdr.dsap, &encaps_hdr, sizeof(encaps_hdr));
834                         
835                 hdr.ethertype = eh->h_proto;
836                 err  = hermes_bap_pwrite(hw, USER_BAP, &hdr, sizeof(hdr),
837                                          txfid, HERMES_802_3_OFFSET);
838                 if (err) {
839                         printk(KERN_ERR "%s: Error %d writing packet header to BAP\n",
840                                dev->name, err);
841                         stats->tx_errors++;
842                         goto fail;
843                 }
844         } else { /* IEEE 802.3 frame */
845                 data_len = len + ETH_HLEN;
846                 data_off = HERMES_802_3_OFFSET;
847                 p = skb->data;
848         }
849
850         /* Round up for odd length packets */
851         err = hermes_bap_pwrite(hw, USER_BAP, p, ALIGN(data_len, 2),
852                                 txfid, data_off);
853         if (err) {
854                 printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
855                        dev->name, err);
856                 stats->tx_errors++;
857                 goto fail;
858         }
859
860         /* Finally, we actually initiate the send */
861         netif_stop_queue(dev);
862
863         err = hermes_docmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
864                                 txfid, NULL);
865         if (err) {
866                 netif_start_queue(dev);
867                 printk(KERN_ERR "%s: Error %d transmitting packet\n",
868                        dev->name, err);
869                 stats->tx_errors++;
870                 goto fail;
871         }
872
873         dev->trans_start = jiffies;
874         stats->tx_bytes += data_off + data_len;
875
876         orinoco_unlock(priv, &flags);
877
878         dev_kfree_skb(skb);
879
880         TRACE_EXIT(dev->name);
881
882         return 0;
883  fail:
884         TRACE_EXIT(dev->name);
885
886         orinoco_unlock(priv, &flags);
887         return err;
888 }
889
890 static void __orinoco_ev_alloc(struct net_device *dev, hermes_t *hw)
891 {
892         struct orinoco_private *priv = netdev_priv(dev);
893         u16 fid = hermes_read_regn(hw, ALLOCFID);
894
895         if (fid != priv->txfid) {
896                 if (fid != DUMMY_FID)
897                         printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
898                                dev->name, fid);
899                 return;
900         } else {
901                 netif_wake_queue(dev);
902         }
903
904         hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
905 }
906
907 static void __orinoco_ev_tx(struct net_device *dev, hermes_t *hw)
908 {
909         struct orinoco_private *priv = netdev_priv(dev);
910         struct net_device_stats *stats = &priv->stats;
911
912         stats->tx_packets++;
913
914         hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
915 }
916
917 static void __orinoco_ev_txexc(struct net_device *dev, hermes_t *hw)
918 {
919         struct orinoco_private *priv = netdev_priv(dev);
920         struct net_device_stats *stats = &priv->stats;
921         u16 fid = hermes_read_regn(hw, TXCOMPLFID);
922         struct hermes_tx_descriptor desc;
923         int err = 0;
924
925         if (fid == DUMMY_FID)
926                 return; /* Nothing's really happened */
927
928         err = hermes_bap_pread(hw, IRQ_BAP, &desc, sizeof(desc), fid, 0);
929         if (err) {
930                 printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
931                        "(FID=%04X error %d)\n",
932                        dev->name, fid, err);
933         } else {
934                 DEBUG(1, "%s: Tx error, status %d\n",
935                       dev->name, le16_to_cpu(desc.status));
936         }
937         
938         stats->tx_errors++;
939
940         hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
941 }
942
943 static void orinoco_tx_timeout(struct net_device *dev)
944 {
945         struct orinoco_private *priv = netdev_priv(dev);
946         struct net_device_stats *stats = &priv->stats;
947         struct hermes *hw = &priv->hw;
948
949         printk(KERN_WARNING "%s: Tx timeout! "
950                "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
951                dev->name, hermes_read_regn(hw, ALLOCFID),
952                hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
953
954         stats->tx_errors++;
955
956         schedule_work(&priv->reset_work);
957 }
958
959 /********************************************************************/
960 /* Rx path (data frames)                                            */
961 /********************************************************************/
962
963 /* Does the frame have a SNAP header indicating it should be
964  * de-encapsulated to Ethernet-II? */
965 static inline int is_ethersnap(struct header_struct *hdr)
966 {
967         /* We de-encapsulate all packets which, a) have SNAP headers
968          * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
969          * and where b) the OUI of the SNAP header is 00:00:00 or
970          * 00:00:f8 - we need both because different APs appear to use
971          * different OUIs for some reason */
972         return (memcmp(&hdr->dsap, &encaps_hdr, 5) == 0)
973                 && ( (hdr->oui[2] == 0x00) || (hdr->oui[2] == 0xf8) );
974 }
975
976 static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
977                                       int level, int noise)
978 {
979         struct orinoco_private *priv = netdev_priv(dev);
980         int i;
981
982         /* Gather wireless spy statistics: for each packet, compare the
983          * source address with out list, and if match, get the stats... */
984         for (i = 0; i < priv->spy_number; i++)
985                 if (!memcmp(mac, priv->spy_address[i], ETH_ALEN)) {
986                         priv->spy_stat[i].level = level - 0x95;
987                         priv->spy_stat[i].noise = noise - 0x95;
988                         priv->spy_stat[i].qual = (level > noise) ? (level - noise) : 0;
989                         priv->spy_stat[i].updated = 7;
990                 }
991 }
992
993 static void orinoco_stat_gather(struct net_device *dev,
994                                 struct sk_buff *skb,
995                                 struct hermes_rx_descriptor *desc)
996 {
997         struct orinoco_private *priv = netdev_priv(dev);
998
999         /* Using spy support with lots of Rx packets, like in an
1000          * infrastructure (AP), will really slow down everything, because
1001          * the MAC address must be compared to each entry of the spy list.
1002          * If the user really asks for it (set some address in the
1003          * spy list), we do it, but he will pay the price.
1004          * Note that to get here, you need both WIRELESS_SPY
1005          * compiled in AND some addresses in the list !!!
1006          */
1007         /* Note : gcc will optimise the whole section away if
1008          * WIRELESS_SPY is not defined... - Jean II */
1009         if (SPY_NUMBER(priv)) {
1010                 orinoco_spy_gather(dev, skb->mac.raw + ETH_ALEN,
1011                                    desc->signal, desc->silence);
1012         }
1013 }
1014
1015 static void __orinoco_ev_rx(struct net_device *dev, hermes_t *hw)
1016 {
1017         struct orinoco_private *priv = netdev_priv(dev);
1018         struct net_device_stats *stats = &priv->stats;
1019         struct iw_statistics *wstats = &priv->wstats;
1020         struct sk_buff *skb = NULL;
1021         u16 rxfid, status;
1022         int length, data_len, data_off;
1023         char *p;
1024         struct hermes_rx_descriptor desc;
1025         struct header_struct hdr;
1026         struct ethhdr *eh;
1027         int err;
1028
1029         rxfid = hermes_read_regn(hw, RXFID);
1030
1031         err = hermes_bap_pread(hw, IRQ_BAP, &desc, sizeof(desc),
1032                                rxfid, 0);
1033         if (err) {
1034                 printk(KERN_ERR "%s: error %d reading Rx descriptor. "
1035                        "Frame dropped.\n", dev->name, err);
1036                 stats->rx_errors++;
1037                 goto drop;
1038         }
1039
1040         status = le16_to_cpu(desc.status);
1041
1042         if (status & HERMES_RXSTAT_ERR) {
1043                 if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
1044                         wstats->discard.code++;
1045                         DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
1046                                dev->name);
1047                 } else {
1048                         stats->rx_crc_errors++;
1049                         DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n", dev->name);
1050                 }
1051                 stats->rx_errors++;
1052                 goto drop;
1053         }
1054
1055         /* For now we ignore the 802.11 header completely, assuming
1056            that the card's firmware has handled anything vital */
1057
1058         err = hermes_bap_pread(hw, IRQ_BAP, &hdr, sizeof(hdr),
1059                                rxfid, HERMES_802_3_OFFSET);
1060         if (err) {
1061                 printk(KERN_ERR "%s: error %d reading frame header. "
1062                        "Frame dropped.\n", dev->name, err);
1063                 stats->rx_errors++;
1064                 goto drop;
1065         }
1066
1067         length = ntohs(hdr.len);
1068         
1069         /* Sanity checks */
1070         if (length < 3) { /* No for even an 802.2 LLC header */
1071                 /* At least on Symbol firmware with PCF we get quite a
1072                    lot of these legitimately - Poll frames with no
1073                    data. */
1074                 stats->rx_dropped++;
1075                 goto drop;
1076         }
1077         if (length > IEEE802_11_DATA_LEN) {
1078                 printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
1079                        dev->name, length);
1080                 stats->rx_length_errors++;
1081                 stats->rx_errors++;
1082                 goto drop;
1083         }
1084
1085         /* We need space for the packet data itself, plus an ethernet
1086            header, plus 2 bytes so we can align the IP header on a
1087            32bit boundary, plus 1 byte so we can read in odd length
1088            packets from the card, which has an IO granularity of 16
1089            bits */  
1090         skb = dev_alloc_skb(length+ETH_HLEN+2+1);
1091         if (!skb) {
1092                 printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
1093                        dev->name);
1094                 goto drop;
1095         }
1096
1097         skb_reserve(skb, 2); /* This way the IP header is aligned */
1098
1099         /* Handle decapsulation
1100          * In most cases, the firmware tell us about SNAP frames.
1101          * For some reason, the SNAP frames sent by LinkSys APs
1102          * are not properly recognised by most firmwares.
1103          * So, check ourselves */
1104         if (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
1105             ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
1106             is_ethersnap(&hdr)) {
1107                 /* These indicate a SNAP within 802.2 LLC within
1108                    802.11 frame which we'll need to de-encapsulate to
1109                    the original EthernetII frame. */
1110
1111                 if (length < ENCAPS_OVERHEAD) { /* No room for full LLC+SNAP */
1112                         stats->rx_length_errors++;
1113                         goto drop;
1114                 }
1115
1116                 /* Remove SNAP header, reconstruct EthernetII frame */
1117                 data_len = length - ENCAPS_OVERHEAD;
1118                 data_off = HERMES_802_3_OFFSET + sizeof(hdr);
1119
1120                 eh = (struct ethhdr *)skb_put(skb, ETH_HLEN);
1121
1122                 memcpy(eh, &hdr, 2 * ETH_ALEN);
1123                 eh->h_proto = hdr.ethertype;
1124         } else {
1125                 /* All other cases indicate a genuine 802.3 frame.  No
1126                    decapsulation needed.  We just throw the whole
1127                    thing in, and hope the protocol layer can deal with
1128                    it as 802.3 */
1129                 data_len = length;
1130                 data_off = HERMES_802_3_OFFSET;
1131                 /* FIXME: we re-read from the card data we already read here */
1132         }
1133
1134         p = skb_put(skb, data_len);
1135         err = hermes_bap_pread(hw, IRQ_BAP, p, ALIGN(data_len, 2),
1136                                rxfid, data_off);
1137         if (err) {
1138                 printk(KERN_ERR "%s: error %d reading frame. "
1139                        "Frame dropped.\n", dev->name, err);
1140                 stats->rx_errors++;
1141                 goto drop;
1142         }
1143
1144         dev->last_rx = jiffies;
1145         skb->dev = dev;
1146         skb->protocol = eth_type_trans(skb, dev);
1147         skb->ip_summed = CHECKSUM_NONE;
1148         
1149         /* Process the wireless stats if needed */
1150         orinoco_stat_gather(dev, skb, &desc);
1151
1152         /* Pass the packet to the networking stack */
1153         netif_rx(skb);
1154         stats->rx_packets++;
1155         stats->rx_bytes += length;
1156
1157         return;
1158
1159  drop:  
1160         stats->rx_dropped++;
1161
1162         if (skb)
1163                 dev_kfree_skb_irq(skb);
1164         return;
1165 }
1166
1167 /********************************************************************/
1168 /* Rx path (info frames)                                            */
1169 /********************************************************************/
1170
1171 static void print_linkstatus(struct net_device *dev, u16 status)
1172 {
1173         char * s;
1174
1175         if (suppress_linkstatus)
1176                 return;
1177
1178         switch (status) {
1179         case HERMES_LINKSTATUS_NOT_CONNECTED:
1180                 s = "Not Connected";
1181                 break;
1182         case HERMES_LINKSTATUS_CONNECTED:
1183                 s = "Connected";
1184                 break;
1185         case HERMES_LINKSTATUS_DISCONNECTED:
1186                 s = "Disconnected";
1187                 break;
1188         case HERMES_LINKSTATUS_AP_CHANGE:
1189                 s = "AP Changed";
1190                 break;
1191         case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
1192                 s = "AP Out of Range";
1193                 break;
1194         case HERMES_LINKSTATUS_AP_IN_RANGE:
1195                 s = "AP In Range";
1196                 break;
1197         case HERMES_LINKSTATUS_ASSOC_FAILED:
1198                 s = "Association Failed";
1199                 break;
1200         default:
1201                 s = "UNKNOWN";
1202         }
1203         
1204         printk(KERN_INFO "%s: New link status: %s (%04x)\n",
1205                dev->name, s, status);
1206 }
1207
1208 static void __orinoco_ev_info(struct net_device *dev, hermes_t *hw)
1209 {
1210         struct orinoco_private *priv = netdev_priv(dev);
1211         u16 infofid;
1212         struct {
1213                 u16 len;
1214                 u16 type;
1215         } __attribute__ ((packed)) info;
1216         int len, type;
1217         int err;
1218
1219         /* This is an answer to an INQUIRE command that we did earlier,
1220          * or an information "event" generated by the card
1221          * The controller return to us a pseudo frame containing
1222          * the information in question - Jean II */
1223         infofid = hermes_read_regn(hw, INFOFID);
1224
1225         /* Read the info frame header - don't try too hard */
1226         err = hermes_bap_pread(hw, IRQ_BAP, &info, sizeof(info),
1227                                infofid, 0);
1228         if (err) {
1229                 printk(KERN_ERR "%s: error %d reading info frame. "
1230                        "Frame dropped.\n", dev->name, err);
1231                 return;
1232         }
1233         
1234         len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
1235         type = le16_to_cpu(info.type);
1236
1237         switch (type) {
1238         case HERMES_INQ_TALLIES: {
1239                 struct hermes_tallies_frame tallies;
1240                 struct iw_statistics *wstats = &priv->wstats;
1241                 
1242                 if (len > sizeof(tallies)) {
1243                         printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
1244                                dev->name, len);
1245                         len = sizeof(tallies);
1246                 }
1247                 
1248                 /* Read directly the data (no seek) */
1249                 hermes_read_words(hw, HERMES_DATA1, (void *) &tallies,
1250                                   len / 2); /* FIXME: blech! */
1251                 
1252                 /* Increment our various counters */
1253                 /* wstats->discard.nwid - no wrong BSSID stuff */
1254                 wstats->discard.code +=
1255                         le16_to_cpu(tallies.RxWEPUndecryptable);
1256                 if (len == sizeof(tallies))  
1257                         wstats->discard.code +=
1258                                 le16_to_cpu(tallies.RxDiscards_WEPICVError) +
1259                                 le16_to_cpu(tallies.RxDiscards_WEPExcluded);
1260                 wstats->discard.misc +=
1261                         le16_to_cpu(tallies.TxDiscardsWrongSA);
1262                 wstats->discard.fragment +=
1263                         le16_to_cpu(tallies.RxMsgInBadMsgFragments);
1264                 wstats->discard.retries +=
1265                         le16_to_cpu(tallies.TxRetryLimitExceeded);
1266                 /* wstats->miss.beacon - no match */
1267         }
1268         break;
1269         case HERMES_INQ_LINKSTATUS: {
1270                 struct hermes_linkstatus linkstatus;
1271                 u16 newstatus;
1272
1273                 if (len != sizeof(linkstatus)) {
1274                         printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
1275                                dev->name, len);
1276                         break;
1277                 }
1278
1279                 hermes_read_words(hw, HERMES_DATA1, (void *) &linkstatus,
1280                                   len / 2);
1281                 newstatus = le16_to_cpu(linkstatus.linkstatus);
1282
1283                 if ( (newstatus == HERMES_LINKSTATUS_CONNECTED)
1284                      || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
1285                      || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE) )
1286                         priv->connected = 1;
1287                 else if ( (newstatus == HERMES_LINKSTATUS_NOT_CONNECTED)
1288                           || (newstatus == HERMES_LINKSTATUS_DISCONNECTED)
1289                           || (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE)
1290                           || (newstatus == HERMES_LINKSTATUS_ASSOC_FAILED) )
1291                         priv->connected = 0;
1292
1293                 if (newstatus != priv->last_linkstatus)
1294                         print_linkstatus(dev, newstatus);
1295
1296                 priv->last_linkstatus = newstatus;
1297         }
1298         break;
1299         default:
1300                 printk(KERN_DEBUG "%s: Unknown information frame received "
1301                        "(type %04x).\n", dev->name, type);
1302                 /* We don't actually do anything about it */
1303                 break;
1304         }
1305 }
1306
1307 static void __orinoco_ev_infdrop(struct net_device *dev, hermes_t *hw)
1308 {
1309         if (net_ratelimit())
1310                 printk(KERN_WARNING "%s: Information frame lost.\n", dev->name);
1311 }
1312
1313 /********************************************************************/
1314 /* Internal hardware control routines                               */
1315 /********************************************************************/
1316
1317 int __orinoco_up(struct net_device *dev)
1318 {
1319         struct orinoco_private *priv = netdev_priv(dev);
1320         struct hermes *hw = &priv->hw;
1321         int err;
1322
1323         err = __orinoco_program_rids(dev);
1324         if (err) {
1325                 printk(KERN_ERR "%s: Error %d configuring card\n",
1326                        dev->name, err);
1327                 return err;
1328         }
1329
1330         /* Fire things up again */
1331         hermes_set_irqmask(hw, ORINOCO_INTEN);
1332         err = hermes_enable_port(hw, 0);
1333         if (err) {
1334                 printk(KERN_ERR "%s: Error %d enabling MAC port\n",
1335                        dev->name, err);
1336                 return err;
1337         }
1338
1339         netif_start_queue(dev);
1340
1341         return 0;
1342 }
1343
1344 int __orinoco_down(struct net_device *dev)
1345 {
1346         struct orinoco_private *priv = netdev_priv(dev);
1347         struct hermes *hw = &priv->hw;
1348         int err;
1349
1350         netif_stop_queue(dev);
1351
1352         if (! priv->hw_unavailable) {
1353                 if (! priv->broken_disableport) {
1354                         err = hermes_disable_port(hw, 0);
1355                         if (err) {
1356                                 /* Some firmwares (e.g. Intersil 1.3.x) seem
1357                                  * to have problems disabling the port, oh
1358                                  * well, too bad. */
1359                                 printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
1360                                        dev->name, err);
1361                                 priv->broken_disableport = 1;
1362                         }
1363                 }
1364                 hermes_set_irqmask(hw, 0);
1365                 hermes_write_regn(hw, EVACK, 0xffff);
1366         }
1367         
1368         /* firmware will have to reassociate */
1369         priv->last_linkstatus = 0xffff;
1370         priv->connected = 0;
1371
1372         return 0;
1373 }
1374
1375 int orinoco_reinit_firmware(struct net_device *dev)
1376 {
1377         struct orinoco_private *priv = netdev_priv(dev);
1378         struct hermes *hw = &priv->hw;
1379         int err;
1380
1381         err = hermes_init(hw);
1382         if (err)
1383                 return err;
1384
1385         err = hermes_allocate(hw, priv->nicbuf_size, &priv->txfid);
1386         if (err == -EIO) {
1387                 /* Try workaround for old Symbol firmware bug */
1388                 printk(KERN_WARNING "%s: firmware ALLOC bug detected "
1389                        "(old Symbol firmware?). Trying to work around... ",
1390                        dev->name);
1391                 
1392                 priv->nicbuf_size = TX_NICBUF_SIZE_BUG;
1393                 err = hermes_allocate(hw, priv->nicbuf_size, &priv->txfid);
1394                 if (err)
1395                         printk("failed!\n");
1396                 else
1397                         printk("ok.\n");
1398         }
1399
1400         return err;
1401 }
1402
1403 static int __orinoco_hw_set_bitrate(struct orinoco_private *priv)
1404 {
1405         hermes_t *hw = &priv->hw;
1406         int err = 0;
1407
1408         if (priv->bitratemode >= BITRATE_TABLE_SIZE) {
1409                 printk(KERN_ERR "%s: BUG: Invalid bitrate mode %d\n",
1410                        priv->ndev->name, priv->bitratemode);
1411                 return -EINVAL;
1412         }
1413
1414         switch (priv->firmware_type) {
1415         case FIRMWARE_TYPE_AGERE:
1416                 err = hermes_write_wordrec(hw, USER_BAP,
1417                                            HERMES_RID_CNFTXRATECONTROL,
1418                                            bitrate_table[priv->bitratemode].agere_txratectrl);
1419                 break;
1420         case FIRMWARE_TYPE_INTERSIL:
1421         case FIRMWARE_TYPE_SYMBOL:
1422                 err = hermes_write_wordrec(hw, USER_BAP,
1423                                            HERMES_RID_CNFTXRATECONTROL,
1424                                            bitrate_table[priv->bitratemode].intersil_txratectrl);
1425                 break;
1426         default:
1427                 BUG();
1428         }
1429
1430         return err;
1431 }
1432
1433 static int __orinoco_hw_setup_wep(struct orinoco_private *priv)
1434 {
1435         hermes_t *hw = &priv->hw;
1436         int err = 0;
1437         int     master_wep_flag;
1438         int     auth_flag;
1439
1440         switch (priv->firmware_type) {
1441         case FIRMWARE_TYPE_AGERE: /* Agere style WEP */
1442                 if (priv->wep_on) {
1443                         err = hermes_write_wordrec(hw, USER_BAP,
1444                                                    HERMES_RID_CNFTXKEY_AGERE,
1445                                                    priv->tx_key);
1446                         if (err)
1447                                 return err;
1448                         
1449                         err = HERMES_WRITE_RECORD(hw, USER_BAP,
1450                                                   HERMES_RID_CNFWEPKEYS_AGERE,
1451                                                   &priv->keys);
1452                         if (err)
1453                                 return err;
1454                 }
1455                 err = hermes_write_wordrec(hw, USER_BAP,
1456                                            HERMES_RID_CNFWEPENABLED_AGERE,
1457                                            priv->wep_on);
1458                 if (err)
1459                         return err;
1460                 break;
1461
1462         case FIRMWARE_TYPE_INTERSIL: /* Intersil style WEP */
1463         case FIRMWARE_TYPE_SYMBOL: /* Symbol style WEP */
1464                 master_wep_flag = 0;            /* Off */
1465                 if (priv->wep_on) {
1466                         int keylen;
1467                         int i;
1468
1469                         /* Fudge around firmware weirdness */
1470                         keylen = le16_to_cpu(priv->keys[priv->tx_key].len);
1471                         
1472                         /* Write all 4 keys */
1473                         for(i = 0; i < ORINOCO_MAX_KEYS; i++) {
1474 /*                              int keylen = le16_to_cpu(priv->keys[i].len); */
1475                                 
1476                                 if (keylen > LARGE_KEY_SIZE) {
1477                                         printk(KERN_ERR "%s: BUG: Key %d has oversize length %d.\n",
1478                                                priv->ndev->name, i, keylen);
1479                                         return -E2BIG;
1480                                 }
1481
1482                                 err = hermes_write_ltv(hw, USER_BAP,
1483                                                        HERMES_RID_CNFDEFAULTKEY0 + i,
1484                                                        HERMES_BYTES_TO_RECLEN(keylen),
1485                                                        priv->keys[i].data);
1486                                 if (err)
1487                                         return err;
1488                         }
1489
1490                         /* Write the index of the key used in transmission */
1491                         err = hermes_write_wordrec(hw, USER_BAP,
1492                                                    HERMES_RID_CNFWEPDEFAULTKEYID,
1493                                                    priv->tx_key);
1494                         if (err)
1495                                 return err;
1496                         
1497                         if (priv->wep_restrict) {
1498                                 auth_flag = 2;
1499                                 master_wep_flag = 3;
1500                         } else {
1501                                 /* Authentication is where Intersil and Symbol
1502                                  * firmware differ... */
1503                                 auth_flag = 1;
1504                                 if (priv->firmware_type == FIRMWARE_TYPE_SYMBOL)
1505                                         master_wep_flag = 3; /* Symbol */ 
1506                                 else 
1507                                         master_wep_flag = 1; /* Intersil */
1508                         }
1509
1510
1511                         err = hermes_write_wordrec(hw, USER_BAP,
1512                                                    HERMES_RID_CNFAUTHENTICATION,
1513                                                    auth_flag);
1514                         if (err)
1515                                 return err;
1516                 }
1517
1518                 /* Master WEP setting : on/off */
1519                 err = hermes_write_wordrec(hw, USER_BAP,
1520                                            HERMES_RID_CNFWEPFLAGS_INTERSIL,
1521                                            master_wep_flag);
1522                 if (err)
1523                         return err;     
1524
1525                 break;
1526
1527         default:
1528                 if (priv->wep_on) {
1529                         printk(KERN_ERR "%s: WEP enabled, although not supported!\n",
1530                                priv->ndev->name);
1531                         return -EINVAL;
1532                 }
1533         }
1534
1535         return 0;
1536 }
1537
1538 static int __orinoco_program_rids(struct net_device *dev)
1539 {
1540         struct orinoco_private *priv = netdev_priv(dev);
1541         hermes_t *hw = &priv->hw;
1542         int err;
1543         struct hermes_idstring idbuf;
1544
1545         /* Set the MAC address */
1546         err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
1547                                HERMES_BYTES_TO_RECLEN(ETH_ALEN), dev->dev_addr);
1548         if (err) {
1549                 printk(KERN_ERR "%s: Error %d setting MAC address\n",
1550                        dev->name, err);
1551                 return err;
1552         }
1553
1554         /* Set up the link mode */
1555         err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFPORTTYPE,
1556                                    priv->port_type);
1557         if (err) {
1558                 printk(KERN_ERR "%s: Error %d setting port type\n",
1559                        dev->name, err);
1560                 return err;
1561         }
1562         /* Set the channel/frequency */
1563         if (priv->channel == 0) {
1564                 printk(KERN_DEBUG "%s: Channel is 0 in __orinoco_program_rids()\n", dev->name);
1565                 if (priv->createibss)
1566                         priv->channel = 10;
1567         }
1568         err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFOWNCHANNEL,
1569                                    priv->channel);
1570         if (err) {
1571                 printk(KERN_ERR "%s: Error %d setting channel\n",
1572                        dev->name, err);
1573                 return err;
1574         }
1575
1576         if (priv->has_ibss) {
1577                 err = hermes_write_wordrec(hw, USER_BAP,
1578                                            HERMES_RID_CNFCREATEIBSS,
1579                                            priv->createibss);
1580                 if (err) {
1581                         printk(KERN_ERR "%s: Error %d setting CREATEIBSS\n", dev->name, err);
1582                         return err;
1583                 }
1584
1585                 if ((strlen(priv->desired_essid) == 0) && (priv->createibss)
1586                    && (!priv->has_ibss_any)) {
1587                         printk(KERN_WARNING "%s: This firmware requires an "
1588                                "ESSID in IBSS-Ad-Hoc mode.\n", dev->name);
1589                         /* With wvlan_cs, in this case, we would crash.
1590                          * hopefully, this driver will behave better...
1591                          * Jean II */
1592                 }
1593         }
1594
1595         /* Set the desired ESSID */
1596         idbuf.len = cpu_to_le16(strlen(priv->desired_essid));
1597         memcpy(&idbuf.val, priv->desired_essid, sizeof(idbuf.val));
1598         /* WinXP wants partner to configure OWNSSID even in IBSS mode. (jimc) */
1599         err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNSSID,
1600                                HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
1601                                &idbuf);
1602         if (err) {
1603                 printk(KERN_ERR "%s: Error %d setting OWNSSID\n",
1604                        dev->name, err);
1605                 return err;
1606         }
1607         err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFDESIREDSSID,
1608                                HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
1609                                &idbuf);
1610         if (err) {
1611                 printk(KERN_ERR "%s: Error %d setting DESIREDSSID\n",
1612                        dev->name, err);
1613                 return err;
1614         }
1615
1616         /* Set the station name */
1617         idbuf.len = cpu_to_le16(strlen(priv->nick));
1618         memcpy(&idbuf.val, priv->nick, sizeof(idbuf.val));
1619         err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
1620                                HERMES_BYTES_TO_RECLEN(strlen(priv->nick)+2),
1621                                &idbuf);
1622         if (err) {
1623                 printk(KERN_ERR "%s: Error %d setting nickname\n",
1624                        dev->name, err);
1625                 return err;
1626         }
1627
1628         /* Set AP density */
1629         if (priv->has_sensitivity) {
1630                 err = hermes_write_wordrec(hw, USER_BAP,
1631                                            HERMES_RID_CNFSYSTEMSCALE,
1632                                            priv->ap_density);
1633                 if (err) {
1634                         printk(KERN_WARNING "%s: Error %d setting SYSTEMSCALE.  "
1635                                "Disabling sensitivity control\n",
1636                                dev->name, err);
1637
1638                         priv->has_sensitivity = 0;
1639                 }
1640         }
1641
1642         /* Set RTS threshold */
1643         err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
1644                                    priv->rts_thresh);
1645         if (err) {
1646                 printk(KERN_ERR "%s: Error %d setting RTS threshold\n",
1647                        dev->name, err);
1648                 return err;
1649         }
1650
1651         /* Set fragmentation threshold or MWO robustness */
1652         if (priv->has_mwo)
1653                 err = hermes_write_wordrec(hw, USER_BAP,
1654                                            HERMES_RID_CNFMWOROBUST_AGERE,
1655                                            priv->mwo_robust);
1656         else
1657                 err = hermes_write_wordrec(hw, USER_BAP,
1658                                            HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
1659                                            priv->frag_thresh);
1660         if (err) {
1661                 printk(KERN_ERR "%s: Error %d setting fragmentation\n",
1662                        dev->name, err);
1663                 return err;
1664         }
1665
1666         /* Set bitrate */
1667         err = __orinoco_hw_set_bitrate(priv);
1668         if (err) {
1669                 printk(KERN_ERR "%s: Error %d setting bitrate\n",
1670                        dev->name, err);
1671                 return err;
1672         }
1673
1674         /* Set power management */
1675         if (priv->has_pm) {
1676                 err = hermes_write_wordrec(hw, USER_BAP,
1677                                            HERMES_RID_CNFPMENABLED,
1678                                            priv->pm_on);
1679                 if (err) {
1680                         printk(KERN_ERR "%s: Error %d setting up PM\n",
1681                                dev->name, err);
1682                         return err;
1683                 }
1684
1685                 err = hermes_write_wordrec(hw, USER_BAP,
1686                                            HERMES_RID_CNFMULTICASTRECEIVE,
1687                                            priv->pm_mcast);
1688                 if (err) {
1689                         printk(KERN_ERR "%s: Error %d setting up PM\n",
1690                                dev->name, err);
1691                         return err;
1692                 }
1693                 err = hermes_write_wordrec(hw, USER_BAP,
1694                                            HERMES_RID_CNFMAXSLEEPDURATION,
1695                                            priv->pm_period);
1696                 if (err) {
1697                         printk(KERN_ERR "%s: Error %d setting up PM\n",
1698                                dev->name, err);
1699                         return err;
1700                 }
1701                 err = hermes_write_wordrec(hw, USER_BAP,
1702                                            HERMES_RID_CNFPMHOLDOVERDURATION,
1703                                            priv->pm_timeout);
1704                 if (err) {
1705                         printk(KERN_ERR "%s: Error %d setting up PM\n",
1706                                dev->name, err);
1707                         return err;
1708                 }
1709         }
1710
1711         /* Set preamble - only for Symbol so far... */
1712         if (priv->has_preamble) {
1713                 err = hermes_write_wordrec(hw, USER_BAP,
1714                                            HERMES_RID_CNFPREAMBLE_SYMBOL,
1715                                            priv->preamble);
1716                 if (err) {
1717                         printk(KERN_ERR "%s: Error %d setting preamble\n",
1718                                dev->name, err);
1719                         return err;
1720                 }
1721         }
1722
1723         /* Set up encryption */
1724         if (priv->has_wep) {
1725                 err = __orinoco_hw_setup_wep(priv);
1726                 if (err) {
1727                         printk(KERN_ERR "%s: Error %d activating WEP\n",
1728                                dev->name, err);
1729                         return err;
1730                 }
1731         }
1732
1733         /* Set promiscuity / multicast*/
1734         priv->promiscuous = 0;
1735         priv->mc_count = 0;
1736         __orinoco_set_multicast_list(dev); /* FIXME: what about the xmit_lock */
1737
1738         return 0;
1739 }
1740
1741 /* FIXME: return int? */
1742 static void
1743 __orinoco_set_multicast_list(struct net_device *dev)
1744 {
1745         struct orinoco_private *priv = netdev_priv(dev);
1746         hermes_t *hw = &priv->hw;
1747         int err = 0;
1748         int promisc, mc_count;
1749
1750         /* The Hermes doesn't seem to have an allmulti mode, so we go
1751          * into promiscuous mode and let the upper levels deal. */
1752         if ( (dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
1753              (dev->mc_count > MAX_MULTICAST(priv)) ) {
1754                 promisc = 1;
1755                 mc_count = 0;
1756         } else {
1757                 promisc = 0;
1758                 mc_count = dev->mc_count;
1759         }
1760
1761         if (promisc != priv->promiscuous) {
1762                 err = hermes_write_wordrec(hw, USER_BAP,
1763                                            HERMES_RID_CNFPROMISCUOUSMODE,
1764                                            promisc);
1765                 if (err) {
1766                         printk(KERN_ERR "%s: Error %d setting PROMISCUOUSMODE to 1.\n",
1767                                dev->name, err);
1768                 } else 
1769                         priv->promiscuous = promisc;
1770         }
1771
1772         if (! promisc && (mc_count || priv->mc_count) ) {
1773                 struct dev_mc_list *p = dev->mc_list;
1774                 struct hermes_multicast mclist;
1775                 int i;
1776
1777                 for (i = 0; i < mc_count; i++) {
1778                         /* paranoia: is list shorter than mc_count? */
1779                         BUG_ON(! p);
1780                         /* paranoia: bad address size in list? */
1781                         BUG_ON(p->dmi_addrlen != ETH_ALEN);
1782                         
1783                         memcpy(mclist.addr[i], p->dmi_addr, ETH_ALEN);
1784                         p = p->next;
1785                 }
1786                 
1787                 if (p)
1788                         printk(KERN_WARNING "Multicast list is longer than mc_count\n");
1789
1790                 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFGROUPADDRESSES,
1791                                        HERMES_BYTES_TO_RECLEN(priv->mc_count * ETH_ALEN),
1792                                        &mclist);
1793                 if (err)
1794                         printk(KERN_ERR "%s: Error %d setting multicast list.\n",
1795                                dev->name, err);
1796                 else
1797                         priv->mc_count = mc_count;
1798         }
1799
1800         /* Since we can set the promiscuous flag when it wasn't asked
1801            for, make sure the net_device knows about it. */
1802         if (priv->promiscuous)
1803                 dev->flags |= IFF_PROMISC;
1804         else
1805                 dev->flags &= ~IFF_PROMISC;
1806 }
1807
1808 static int orinoco_reconfigure(struct net_device *dev)
1809 {
1810         struct orinoco_private *priv = netdev_priv(dev);
1811         struct hermes *hw = &priv->hw;
1812         unsigned long flags;
1813         int err = 0;
1814
1815         if (priv->broken_disableport) {
1816                 schedule_work(&priv->reset_work);
1817                 return 0;
1818         }
1819
1820         if (orinoco_lock(priv, &flags) != 0)
1821                 return -EBUSY;
1822                 
1823         err = hermes_disable_port(hw, 0);
1824         if (err) {
1825                 printk(KERN_WARNING "%s: Unable to disable port while reconfiguring card\n",
1826                        dev->name);
1827                 priv->broken_disableport = 1;
1828                 goto out;
1829         }
1830
1831         err = __orinoco_program_rids(dev);
1832         if (err) {
1833                 printk(KERN_WARNING "%s: Unable to reconfigure card\n",
1834                        dev->name);
1835                 goto out;
1836         }
1837
1838         err = hermes_enable_port(hw, 0);
1839         if (err) {
1840                 printk(KERN_WARNING "%s: Unable to enable port while reconfiguring card\n",
1841                        dev->name);
1842                 goto out;
1843         }
1844
1845  out:
1846         if (err) {
1847                 printk(KERN_WARNING "%s: Resetting instead...\n", dev->name);
1848                 schedule_work(&priv->reset_work);
1849                 err = 0;
1850         }
1851
1852         orinoco_unlock(priv, &flags);
1853         return err;
1854
1855 }
1856
1857 /* This must be called from user context, without locks held - use
1858  * schedule_work() */
1859 static void orinoco_reset(struct net_device *dev)
1860 {
1861         struct orinoco_private *priv = netdev_priv(dev);
1862         struct hermes *hw = &priv->hw;
1863         int err = 0;
1864         unsigned long flags;
1865
1866         if (orinoco_lock(priv, &flags) != 0)
1867                 /* When the hardware becomes available again, whatever
1868                  * detects that is responsible for re-initializing
1869                  * it. So no need for anything further */
1870                 return;
1871
1872         netif_stop_queue(dev);
1873
1874         /* Shut off interrupts.  Depending on what state the hardware
1875          * is in, this might not work, but we'll try anyway */
1876         hermes_set_irqmask(hw, 0);
1877         hermes_write_regn(hw, EVACK, 0xffff);
1878
1879         priv->hw_unavailable++;
1880         priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
1881         priv->connected = 0;
1882
1883         orinoco_unlock(priv, &flags);
1884
1885         if (priv->hard_reset)
1886                 err = (*priv->hard_reset)(priv);
1887         if (err) {
1888                 printk(KERN_ERR "%s: orinoco_reset: Error %d "
1889                        "performing  hard reset\n", dev->name, err);
1890                 /* FIXME: shutdown of some sort */
1891                 return;
1892         }
1893
1894         err = orinoco_reinit_firmware(dev);
1895         if (err) {
1896                 printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
1897                        dev->name, err);
1898                 return;
1899         }
1900
1901         spin_lock_irq(&priv->lock); /* This has to be called from user context */
1902
1903         priv->hw_unavailable--;
1904
1905         /* priv->open or priv->hw_unavailable might have changed while
1906          * we dropped the lock */
1907         if (priv->open && (! priv->hw_unavailable)) {
1908                 err = __orinoco_up(dev);
1909                 if (err) {
1910                         printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
1911                                dev->name, err);
1912                 } else
1913                         dev->trans_start = jiffies;
1914         }
1915
1916         spin_unlock_irq(&priv->lock);
1917
1918         return;
1919 }
1920
1921 /********************************************************************/
1922 /* Interrupt handler                                                */
1923 /********************************************************************/
1924
1925 static void __orinoco_ev_tick(struct net_device *dev, hermes_t *hw)
1926 {
1927         printk(KERN_DEBUG "%s: TICK\n", dev->name);
1928 }
1929
1930 static void __orinoco_ev_wterr(struct net_device *dev, hermes_t *hw)
1931 {
1932         /* This seems to happen a fair bit under load, but ignoring it
1933            seems to work fine...*/
1934         printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
1935                dev->name);
1936 }
1937
1938 irqreturn_t orinoco_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1939 {
1940         struct net_device *dev = (struct net_device *)dev_id;
1941         struct orinoco_private *priv = netdev_priv(dev);
1942         hermes_t *hw = &priv->hw;
1943         int count = MAX_IRQLOOPS_PER_IRQ;
1944         u16 evstat, events;
1945         /* These are used to detect a runaway interrupt situation */
1946         /* If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
1947          * we panic and shut down the hardware */
1948         static int last_irq_jiffy = 0; /* jiffies value the last time
1949                                         * we were called */
1950         static int loops_this_jiffy = 0;
1951         unsigned long flags;
1952
1953         if (orinoco_lock(priv, &flags) != 0) {
1954                 /* If hw is unavailable - we don't know if the irq was
1955                  * for us or not */
1956                 return IRQ_HANDLED;
1957         }
1958
1959         evstat = hermes_read_regn(hw, EVSTAT);
1960         events = evstat & hw->inten;
1961         if (! events) {
1962                 orinoco_unlock(priv, &flags);
1963                 return IRQ_NONE;
1964         }
1965         
1966         if (jiffies != last_irq_jiffy)
1967                 loops_this_jiffy = 0;
1968         last_irq_jiffy = jiffies;
1969
1970         while (events && count--) {
1971                 if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
1972                         printk(KERN_WARNING "%s: IRQ handler is looping too "
1973                                "much! Resetting.\n", dev->name);
1974                         /* Disable interrupts for now */
1975                         hermes_set_irqmask(hw, 0);
1976                         schedule_work(&priv->reset_work);
1977                         break;
1978                 }
1979
1980                 /* Check the card hasn't been removed */
1981                 if (! hermes_present(hw)) {
1982                         DEBUG(0, "orinoco_interrupt(): card removed\n");
1983                         break;
1984                 }
1985
1986                 if (events & HERMES_EV_TICK)
1987                         __orinoco_ev_tick(dev, hw);
1988                 if (events & HERMES_EV_WTERR)
1989                         __orinoco_ev_wterr(dev, hw);
1990                 if (events & HERMES_EV_INFDROP)
1991                         __orinoco_ev_infdrop(dev, hw);
1992                 if (events & HERMES_EV_INFO)
1993                         __orinoco_ev_info(dev, hw);
1994                 if (events & HERMES_EV_RX)
1995                         __orinoco_ev_rx(dev, hw);
1996                 if (events & HERMES_EV_TXEXC)
1997                         __orinoco_ev_txexc(dev, hw);
1998                 if (events & HERMES_EV_TX)
1999                         __orinoco_ev_tx(dev, hw);
2000                 if (events & HERMES_EV_ALLOC)
2001                         __orinoco_ev_alloc(dev, hw);
2002                 
2003                 hermes_write_regn(hw, EVACK, events);
2004
2005                 evstat = hermes_read_regn(hw, EVSTAT);
2006                 events = evstat & hw->inten;
2007         };
2008
2009         orinoco_unlock(priv, &flags);
2010         return IRQ_HANDLED;
2011 }
2012
2013 /********************************************************************/
2014 /* Initialization                                                   */
2015 /********************************************************************/
2016
2017 struct sta_id {
2018         u16 id, variant, major, minor;
2019 } __attribute__ ((packed));
2020
2021 static int determine_firmware_type(struct net_device *dev, struct sta_id *sta_id)
2022 {
2023         /* FIXME: this is fundamentally broken */
2024         unsigned int firmver = ((u32)sta_id->major << 16) | sta_id->minor;
2025         
2026         if (sta_id->variant == 1)
2027                 return FIRMWARE_TYPE_AGERE;
2028         else if ((sta_id->variant == 2) &&
2029                    ((firmver == 0x10001) || (firmver == 0x20001)))
2030                 return FIRMWARE_TYPE_SYMBOL;
2031         else
2032                 return FIRMWARE_TYPE_INTERSIL;
2033 }
2034
2035 static void determine_firmware(struct net_device *dev)
2036 {
2037         struct orinoco_private *priv = netdev_priv(dev);
2038         hermes_t *hw = &priv->hw;
2039         int err;
2040         struct sta_id sta_id;
2041         unsigned int firmver;
2042         char tmp[SYMBOL_MAX_VER_LEN+1];
2043
2044         /* Get the firmware version */
2045         err = HERMES_READ_RECORD(hw, USER_BAP, HERMES_RID_STAID, &sta_id);
2046         if (err) {
2047                 printk(KERN_WARNING "%s: Error %d reading firmware info. Wildly guessing capabilities...\n",
2048                        dev->name, err);
2049                 memset(&sta_id, 0, sizeof(sta_id));
2050         }
2051
2052         le16_to_cpus(&sta_id.id);
2053         le16_to_cpus(&sta_id.variant);
2054         le16_to_cpus(&sta_id.major);
2055         le16_to_cpus(&sta_id.minor);
2056         printk(KERN_DEBUG "%s: Station identity %04x:%04x:%04x:%04x\n",
2057                dev->name, sta_id.id, sta_id.variant,
2058                sta_id.major, sta_id.minor);
2059
2060         if (! priv->firmware_type)
2061                 priv->firmware_type = determine_firmware_type(dev, &sta_id);
2062
2063         /* Default capabilities */
2064         priv->has_sensitivity = 1;
2065         priv->has_mwo = 0;
2066         priv->has_preamble = 0;
2067         priv->has_port3 = 1;
2068         priv->has_ibss = 1;
2069         priv->has_ibss_any = 0;
2070         priv->has_wep = 0;
2071         priv->has_big_wep = 0;
2072
2073         /* Determine capabilities from the firmware version */
2074         switch (priv->firmware_type) {
2075         case FIRMWARE_TYPE_AGERE:
2076                 /* Lucent Wavelan IEEE, Lucent Orinoco, Cabletron RoamAbout,
2077                    ELSA, Melco, HP, IBM, Dell 1150, Compaq 110/210 */
2078                 printk(KERN_DEBUG "%s: Looks like a Lucent/Agere firmware "
2079                        "version %d.%02d\n", dev->name,
2080                        sta_id.major, sta_id.minor);
2081
2082                 firmver = ((unsigned long)sta_id.major << 16) | sta_id.minor;
2083
2084                 priv->has_ibss = (firmver >= 0x60006);
2085                 priv->has_ibss_any = (firmver >= 0x60010);
2086                 priv->has_wep = (firmver >= 0x40020);
2087                 priv->has_big_wep = 1; /* FIXME: this is wrong - how do we tell
2088                                           Gold cards from the others? */
2089                 priv->has_mwo = (firmver >= 0x60000);
2090                 priv->has_pm = (firmver >= 0x40020); /* Don't work in 7.52 ? */
2091                 priv->ibss_port = 1;
2092
2093                 /* Tested with Agere firmware :
2094                  *      1.16 ; 4.08 ; 4.52 ; 6.04 ; 6.16 ; 7.28 => Jean II
2095                  * Tested CableTron firmware : 4.32 => Anton */
2096                 break;
2097         case FIRMWARE_TYPE_SYMBOL:
2098                 /* Symbol , 3Com AirConnect, Intel, Ericsson WLAN */
2099                 /* Intel MAC : 00:02:B3:* */
2100                 /* 3Com MAC : 00:50:DA:* */
2101                 memset(tmp, 0, sizeof(tmp));
2102                 /* Get the Symbol firmware version */
2103                 err = hermes_read_ltv(hw, USER_BAP,
2104                                       HERMES_RID_SECONDARYVERSION_SYMBOL,
2105                                       SYMBOL_MAX_VER_LEN, NULL, &tmp);
2106                 if (err) {
2107                         printk(KERN_WARNING
2108                                "%s: Error %d reading Symbol firmware info. Wildly guessing capabilities...\n",
2109                                dev->name, err);
2110                         firmver = 0;
2111                         tmp[0] = '\0';
2112                 } else {
2113                         /* The firmware revision is a string, the format is
2114                          * something like : "V2.20-01".
2115                          * Quick and dirty parsing... - Jean II
2116                          */
2117                         firmver = ((tmp[1] - '0') << 16) | ((tmp[3] - '0') << 12)
2118                                 | ((tmp[4] - '0') << 8) | ((tmp[6] - '0') << 4)
2119                                 | (tmp[7] - '0');
2120
2121                         tmp[SYMBOL_MAX_VER_LEN] = '\0';
2122                 }
2123
2124                 printk(KERN_DEBUG "%s: Looks like a Symbol firmware "
2125                        "version [%s] (parsing to %X)\n", dev->name,
2126                        tmp, firmver);
2127
2128                 priv->has_ibss = (firmver >= 0x20000);
2129                 priv->has_wep = (firmver >= 0x15012);
2130                 priv->has_big_wep = (firmver >= 0x20000);
2131                 priv->has_pm = (firmver >= 0x20000) && (firmver < 0x22000);
2132                 priv->has_preamble = (firmver >= 0x20000);
2133                 priv->ibss_port = 4;
2134                 /* Tested with Intel firmware : 0x20015 => Jean II */
2135                 /* Tested with 3Com firmware : 0x15012 & 0x22001 => Jean II */
2136                 break;
2137         case FIRMWARE_TYPE_INTERSIL:
2138                 /* D-Link, Linksys, Adtron, ZoomAir, and many others...
2139                  * Samsung, Compaq 100/200 and Proxim are slightly
2140                  * different and less well tested */
2141                 /* D-Link MAC : 00:40:05:* */
2142                 /* Addtron MAC : 00:90:D1:* */
2143                 printk(KERN_DEBUG "%s: Looks like an Intersil firmware "
2144                        "version %d.%d.%d\n", dev->name,
2145                        sta_id.major, sta_id.minor, sta_id.variant);
2146
2147                 firmver = ((unsigned long)sta_id.major << 16) |
2148                         ((unsigned long)sta_id.minor << 8) | sta_id.variant;
2149
2150                 priv->has_ibss = (firmver >= 0x000700); /* FIXME */
2151                 priv->has_big_wep = priv->has_wep = (firmver >= 0x000800);
2152                 priv->has_pm = (firmver >= 0x000700);
2153
2154                 if (firmver >= 0x000800)
2155                         priv->ibss_port = 0;
2156                 else {
2157                         printk(KERN_NOTICE "%s: Intersil firmware earlier "
2158                                "than v0.8.x - several features not supported\n",
2159                                dev->name);
2160                         priv->ibss_port = 1;
2161                 }
2162                 break;
2163         default:
2164                 break;
2165         }
2166 }
2167
2168 static int orinoco_init(struct net_device *dev)
2169 {
2170         struct orinoco_private *priv = netdev_priv(dev);
2171         hermes_t *hw = &priv->hw;
2172         int err = 0;
2173         struct hermes_idstring nickbuf;
2174         u16 reclen;
2175         int len;
2176
2177         TRACE_ENTER(dev->name);
2178
2179         /* No need to lock, the hw_unavailable flag is already set in
2180          * alloc_orinocodev() */
2181         priv->nicbuf_size = IEEE802_11_FRAME_LEN + ETH_HLEN;
2182
2183         /* Initialize the firmware */
2184         err = hermes_init(hw);
2185         if (err != 0) {
2186                 printk(KERN_ERR "%s: failed to initialize firmware (err = %d)\n",
2187                        dev->name, err);
2188                 goto out;
2189         }
2190
2191         determine_firmware(dev);
2192
2193         if (priv->has_port3)
2194                 printk(KERN_DEBUG "%s: Ad-hoc demo mode supported\n", dev->name);
2195         if (priv->has_ibss)
2196                 printk(KERN_DEBUG "%s: IEEE standard IBSS ad-hoc mode supported\n",
2197                        dev->name);
2198         if (priv->has_wep) {
2199                 printk(KERN_DEBUG "%s: WEP supported, ", dev->name);
2200                 if (priv->has_big_wep)
2201                         printk("104-bit key\n");
2202                 else
2203                         printk("40-bit key\n");
2204         }
2205
2206         /* Get the MAC address */
2207         err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
2208                               ETH_ALEN, NULL, dev->dev_addr);
2209         if (err) {
2210                 printk(KERN_WARNING "%s: failed to read MAC address!\n",
2211                        dev->name);
2212                 goto out;
2213         }
2214
2215         printk(KERN_DEBUG "%s: MAC address %02X:%02X:%02X:%02X:%02X:%02X\n",
2216                dev->name, dev->dev_addr[0], dev->dev_addr[1],
2217                dev->dev_addr[2], dev->dev_addr[3], dev->dev_addr[4],
2218                dev->dev_addr[5]);
2219
2220         /* Get the station name */
2221         err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
2222                               sizeof(nickbuf), &reclen, &nickbuf);
2223         if (err) {
2224                 printk(KERN_ERR "%s: failed to read station name\n",
2225                        dev->name);
2226                 goto out;
2227         }
2228         if (nickbuf.len)
2229                 len = min(IW_ESSID_MAX_SIZE, (int)le16_to_cpu(nickbuf.len));
2230         else
2231                 len = min(IW_ESSID_MAX_SIZE, 2 * reclen);
2232         memcpy(priv->nick, &nickbuf.val, len);
2233         priv->nick[len] = '\0';
2234
2235         printk(KERN_DEBUG "%s: Station name \"%s\"\n", dev->name, priv->nick);
2236
2237         /* Get allowed channels */
2238         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CHANNELLIST,
2239                                   &priv->channel_mask);
2240         if (err) {
2241                 printk(KERN_ERR "%s: failed to read channel list!\n",
2242                        dev->name);
2243                 goto out;
2244         }
2245
2246         /* Get initial AP density */
2247         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFSYSTEMSCALE,
2248                                   &priv->ap_density);
2249         if (err || priv->ap_density < 1 || priv->ap_density > 3) {
2250                 priv->has_sensitivity = 0;
2251         }
2252
2253         /* Get initial RTS threshold */
2254         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
2255                                   &priv->rts_thresh);
2256         if (err) {
2257                 printk(KERN_ERR "%s: failed to read RTS threshold!\n",
2258                        dev->name);
2259                 goto out;
2260         }
2261
2262         /* Get initial fragmentation settings */
2263         if (priv->has_mwo)
2264                 err = hermes_read_wordrec(hw, USER_BAP,
2265                                           HERMES_RID_CNFMWOROBUST_AGERE,
2266                                           &priv->mwo_robust);
2267         else
2268                 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
2269                                           &priv->frag_thresh);
2270         if (err) {
2271                 printk(KERN_ERR "%s: failed to read fragmentation settings!\n",
2272                        dev->name);
2273                 goto out;
2274         }
2275
2276         /* Power management setup */
2277         if (priv->has_pm) {
2278                 priv->pm_on = 0;
2279                 priv->pm_mcast = 1;
2280                 err = hermes_read_wordrec(hw, USER_BAP,
2281                                           HERMES_RID_CNFMAXSLEEPDURATION,
2282                                           &priv->pm_period);
2283                 if (err) {
2284                         printk(KERN_ERR "%s: failed to read power management period!\n",
2285                                dev->name);
2286                         goto out;
2287                 }
2288                 err = hermes_read_wordrec(hw, USER_BAP,
2289                                           HERMES_RID_CNFPMHOLDOVERDURATION,
2290                                           &priv->pm_timeout);
2291                 if (err) {
2292                         printk(KERN_ERR "%s: failed to read power management timeout!\n",
2293                                dev->name);
2294                         goto out;
2295                 }
2296         }
2297
2298         /* Preamble setup */
2299         if (priv->has_preamble) {
2300                 err = hermes_read_wordrec(hw, USER_BAP,
2301                                           HERMES_RID_CNFPREAMBLE_SYMBOL,
2302                                           &priv->preamble);
2303                 if (err)
2304                         goto out;
2305         }
2306                 
2307         /* Set up the default configuration */
2308         priv->iw_mode = IW_MODE_INFRA;
2309         /* By default use IEEE/IBSS ad-hoc mode if we have it */
2310         priv->prefer_port3 = priv->has_port3 && (! priv->has_ibss);
2311         set_port_type(priv);
2312         priv->channel = 10; /* default channel, more-or-less arbitrary */
2313
2314         priv->promiscuous = 0;
2315         priv->wep_on = 0;
2316         priv->tx_key = 0;
2317
2318         err = hermes_allocate(hw, priv->nicbuf_size, &priv->txfid);
2319         if (err == -EIO) {
2320                 /* Try workaround for old Symbol firmware bug */
2321                 printk(KERN_WARNING "%s: firmware ALLOC bug detected "
2322                        "(old Symbol firmware?). Trying to work around... ",
2323                        dev->name);
2324                 
2325                 priv->nicbuf_size = TX_NICBUF_SIZE_BUG;
2326                 err = hermes_allocate(hw, priv->nicbuf_size, &priv->txfid);
2327                 if (err)
2328                         printk("failed!\n");
2329                 else
2330                         printk("ok.\n");
2331         }
2332         if (err) {
2333                 printk("%s: Error %d allocating Tx buffer\n", dev->name, err);
2334                 goto out;
2335         }
2336
2337         /* Make the hardware available, as long as it hasn't been
2338          * removed elsewhere (e.g. by PCMCIA hot unplug) */
2339         spin_lock_irq(&priv->lock);
2340         priv->hw_unavailable--;
2341         spin_unlock_irq(&priv->lock);
2342
2343         printk(KERN_DEBUG "%s: ready\n", dev->name);
2344
2345  out:
2346         TRACE_EXIT(dev->name);
2347         return err;
2348 }
2349
2350 struct net_device *alloc_orinocodev(int sizeof_card,
2351                                     int (*hard_reset)(struct orinoco_private *))
2352 {
2353         struct net_device *dev;
2354         struct orinoco_private *priv;
2355
2356         dev = alloc_etherdev(sizeof(struct orinoco_private) + sizeof_card);
2357         if (! dev)
2358                 return NULL;
2359         priv = netdev_priv(dev);
2360         priv->ndev = dev;
2361         if (sizeof_card)
2362                 priv->card = (void *)((unsigned long)netdev_priv(dev)
2363                                       + sizeof(struct orinoco_private));
2364         else
2365                 priv->card = NULL;
2366
2367         /* Setup / override net_device fields */
2368         dev->init = orinoco_init;
2369         dev->hard_start_xmit = orinoco_xmit;
2370         dev->tx_timeout = orinoco_tx_timeout;
2371         dev->watchdog_timeo = HZ; /* 1 second timeout */
2372         dev->get_stats = orinoco_get_stats;
2373         dev->get_wireless_stats = orinoco_get_wireless_stats;
2374         dev->do_ioctl = orinoco_ioctl;
2375         dev->change_mtu = orinoco_change_mtu;
2376         dev->set_multicast_list = orinoco_set_multicast_list;
2377         /* we use the default eth_mac_addr for setting the MAC addr */
2378
2379         /* Set up default callbacks */
2380         dev->open = orinoco_open;
2381         dev->stop = orinoco_stop;
2382         priv->hard_reset = hard_reset;
2383
2384         spin_lock_init(&priv->lock);
2385         priv->open = 0;
2386         priv->hw_unavailable = 1; /* orinoco_init() must clear this
2387                                    * before anything else touches the
2388                                    * hardware */
2389         INIT_WORK(&priv->reset_work, (void (*)(void *))orinoco_reset, dev);
2390
2391         priv->last_linkstatus = 0xffff;
2392         priv->connected = 0;
2393
2394         return dev;
2395
2396 }
2397
2398 /********************************************************************/
2399 /* Wireless extensions                                              */
2400 /********************************************************************/
2401
2402 static int orinoco_hw_get_bssid(struct orinoco_private *priv,
2403                                 char buf[ETH_ALEN])
2404 {
2405         hermes_t *hw = &priv->hw;
2406         int err = 0;
2407         unsigned long flags;
2408
2409         if (orinoco_lock(priv, &flags) != 0)
2410                 return -EBUSY;
2411
2412         err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
2413                               ETH_ALEN, NULL, buf);
2414
2415         orinoco_unlock(priv, &flags);
2416
2417         return err;
2418 }
2419
2420 static int orinoco_hw_get_essid(struct orinoco_private *priv, int *active,
2421                                 char buf[IW_ESSID_MAX_SIZE+1])
2422 {
2423         hermes_t *hw = &priv->hw;
2424         int err = 0;
2425         struct hermes_idstring essidbuf;
2426         char *p = (char *)(&essidbuf.val);
2427         int len;
2428         unsigned long flags;
2429
2430         if (orinoco_lock(priv, &flags) != 0)
2431                 return -EBUSY;
2432
2433         if (strlen(priv->desired_essid) > 0) {
2434                 /* We read the desired SSID from the hardware rather
2435                    than from priv->desired_essid, just in case the
2436                    firmware is allowed to change it on us. I'm not
2437                    sure about this */
2438                 /* My guess is that the OWNSSID should always be whatever
2439                  * we set to the card, whereas CURRENT_SSID is the one that
2440                  * may change... - Jean II */
2441                 u16 rid;
2442
2443                 *active = 1;
2444
2445                 rid = (priv->port_type == 3) ? HERMES_RID_CNFOWNSSID :
2446                         HERMES_RID_CNFDESIREDSSID;
2447                 
2448                 err = hermes_read_ltv(hw, USER_BAP, rid, sizeof(essidbuf),
2449                                       NULL, &essidbuf);
2450                 if (err)
2451                         goto fail_unlock;
2452         } else {
2453                 *active = 0;
2454
2455                 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTSSID,
2456                                       sizeof(essidbuf), NULL, &essidbuf);
2457                 if (err)
2458                         goto fail_unlock;
2459         }
2460
2461         len = le16_to_cpu(essidbuf.len);
2462
2463         memset(buf, 0, IW_ESSID_MAX_SIZE+1);
2464         memcpy(buf, p, len);
2465         buf[len] = '\0';
2466
2467  fail_unlock:
2468         orinoco_unlock(priv, &flags);
2469
2470         return err;       
2471 }
2472
2473 static long orinoco_hw_get_freq(struct orinoco_private *priv)
2474 {
2475         
2476         hermes_t *hw = &priv->hw;
2477         int err = 0;
2478         u16 channel;
2479         long freq = 0;
2480         unsigned long flags;
2481
2482         if (orinoco_lock(priv, &flags) != 0)
2483                 return -EBUSY;
2484         
2485         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CURRENTCHANNEL, &channel);
2486         if (err)
2487                 goto out;
2488
2489         /* Intersil firmware 1.3.5 returns 0 when the interface is down */
2490         if (channel == 0) {
2491                 err = -EBUSY;
2492                 goto out;
2493         }
2494
2495         if ( (channel < 1) || (channel > NUM_CHANNELS) ) {
2496                 printk(KERN_WARNING "%s: Channel out of range (%d)!\n",
2497                        priv->ndev->name, channel);
2498                 err = -EBUSY;
2499                 goto out;
2500
2501         }
2502         freq = channel_frequency[channel-1] * 100000;
2503
2504  out:
2505         orinoco_unlock(priv, &flags);
2506
2507         if (err > 0)
2508                 err = -EBUSY;
2509         return err ? err : freq;
2510 }
2511
2512 static int orinoco_hw_get_bitratelist(struct orinoco_private *priv,
2513                                       int *numrates, s32 *rates, int max)
2514 {
2515         hermes_t *hw = &priv->hw;
2516         struct hermes_idstring list;
2517         unsigned char *p = (unsigned char *)&list.val;
2518         int err = 0;
2519         int num;
2520         int i;
2521         unsigned long flags;
2522
2523         if (orinoco_lock(priv, &flags) != 0)
2524                 return -EBUSY;
2525
2526         err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_SUPPORTEDDATARATES,
2527                               sizeof(list), NULL, &list);
2528         orinoco_unlock(priv, &flags);
2529
2530         if (err)
2531                 return err;
2532         
2533         num = le16_to_cpu(list.len);
2534         *numrates = num;
2535         num = min(num, max);
2536
2537         for (i = 0; i < num; i++) {
2538                 rates[i] = (p[i] & 0x7f) * 500000; /* convert to bps */
2539         }
2540
2541         return 0;
2542 }
2543
2544 static int orinoco_ioctl_getiwrange(struct net_device *dev, struct iw_point *rrq)
2545 {
2546         struct orinoco_private *priv = netdev_priv(dev);
2547         int err = 0;
2548         int mode;
2549         struct iw_range range;
2550         int numrates;
2551         int i, k;
2552         unsigned long flags;
2553
2554         TRACE_ENTER(dev->name);
2555
2556         err = verify_area(VERIFY_WRITE, rrq->pointer, sizeof(range));
2557         if (err)
2558                 return err;
2559
2560         rrq->length = sizeof(range);
2561
2562         if (orinoco_lock(priv, &flags) != 0)
2563                 return -EBUSY;
2564
2565         mode = priv->iw_mode;
2566         orinoco_unlock(priv, &flags);
2567
2568         memset(&range, 0, sizeof(range));
2569
2570         /* Much of this shamelessly taken from wvlan_cs.c. No idea
2571          * what it all means -dgibson */
2572         range.we_version_compiled = WIRELESS_EXT;
2573         range.we_version_source = 11;
2574
2575         range.min_nwid = range.max_nwid = 0; /* We don't use nwids */
2576
2577         /* Set available channels/frequencies */
2578         range.num_channels = NUM_CHANNELS;
2579         k = 0;
2580         for (i = 0; i < NUM_CHANNELS; i++) {
2581                 if (priv->channel_mask & (1 << i)) {
2582                         range.freq[k].i = i + 1;
2583                         range.freq[k].m = channel_frequency[i] * 100000;
2584                         range.freq[k].e = 1;
2585                         k++;
2586                 }
2587                 
2588                 if (k >= IW_MAX_FREQUENCIES)
2589                         break;
2590         }
2591         range.num_frequency = k;
2592
2593         range.sensitivity = 3;
2594
2595         if ((mode == IW_MODE_ADHOC) && (priv->spy_number == 0)){
2596                 /* Quality stats meaningless in ad-hoc mode */
2597                 range.max_qual.qual = 0;
2598                 range.max_qual.level = 0;
2599                 range.max_qual.noise = 0;
2600                 range.avg_qual.qual = 0;
2601                 range.avg_qual.level = 0;
2602                 range.avg_qual.noise = 0;
2603         } else {
2604                 range.max_qual.qual = 0x8b - 0x2f;
2605                 range.max_qual.level = 0x2f - 0x95 - 1;
2606                 range.max_qual.noise = 0x2f - 0x95 - 1;
2607                 /* Need to get better values */
2608                 range.avg_qual.qual = 0x24;
2609                 range.avg_qual.level = 0xC2;
2610                 range.avg_qual.noise = 0x9E;
2611         }
2612
2613         err = orinoco_hw_get_bitratelist(priv, &numrates,
2614                                          range.bitrate, IW_MAX_BITRATES);
2615         if (err)
2616                 return err;
2617         range.num_bitrates = numrates;
2618         
2619         /* Set an indication of the max TCP throughput in bit/s that we can
2620          * expect using this interface. May be use for QoS stuff...
2621          * Jean II */
2622         if(numrates > 2)
2623                 range.throughput = 5 * 1000 * 1000;     /* ~5 Mb/s */
2624         else
2625                 range.throughput = 1.5 * 1000 * 1000;   /* ~1.5 Mb/s */
2626
2627         range.min_rts = 0;
2628         range.max_rts = 2347;
2629         range.min_frag = 256;
2630         range.max_frag = 2346;
2631
2632         if (orinoco_lock(priv, &flags) != 0)
2633                 return -EBUSY;
2634         if (priv->has_wep) {
2635                 range.max_encoding_tokens = ORINOCO_MAX_KEYS;
2636
2637                 range.encoding_size[0] = SMALL_KEY_SIZE;
2638                 range.num_encoding_sizes = 1;
2639
2640                 if (priv->has_big_wep) {
2641                         range.encoding_size[1] = LARGE_KEY_SIZE;
2642                         range.num_encoding_sizes = 2;
2643                 }
2644         } else {
2645                 range.num_encoding_sizes = 0;
2646                 range.max_encoding_tokens = 0;
2647         }
2648         orinoco_unlock(priv, &flags);
2649                 
2650         range.min_pmp = 0;
2651         range.max_pmp = 65535000;
2652         range.min_pmt = 0;
2653         range.max_pmt = 65535 * 1000;   /* ??? */
2654         range.pmp_flags = IW_POWER_PERIOD;
2655         range.pmt_flags = IW_POWER_TIMEOUT;
2656         range.pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_UNICAST_R;
2657
2658         range.num_txpower = 1;
2659         range.txpower[0] = 15; /* 15dBm */
2660         range.txpower_capa = IW_TXPOW_DBM;
2661
2662         range.retry_capa = IW_RETRY_LIMIT | IW_RETRY_LIFETIME;
2663         range.retry_flags = IW_RETRY_LIMIT;
2664         range.r_time_flags = IW_RETRY_LIFETIME;
2665         range.min_retry = 0;
2666         range.max_retry = 65535;        /* ??? */
2667         range.min_r_time = 0;
2668         range.max_r_time = 65535 * 1000;        /* ??? */
2669
2670         if (copy_to_user(rrq->pointer, &range, sizeof(range)))
2671                 return -EFAULT;
2672
2673         TRACE_EXIT(dev->name);
2674
2675         return 0;
2676 }
2677
2678 static int orinoco_ioctl_setiwencode(struct net_device *dev, struct iw_point *erq)
2679 {
2680         struct orinoco_private *priv = netdev_priv(dev);
2681         int index = (erq->flags & IW_ENCODE_INDEX) - 1;
2682         int setindex = priv->tx_key;
2683         int enable = priv->wep_on;
2684         int restricted = priv->wep_restrict;
2685         u16 xlen = 0;
2686         int err = 0;
2687         char keybuf[ORINOCO_MAX_KEY_SIZE];
2688         unsigned long flags;
2689         
2690         if (erq->pointer) {
2691                 /* We actually have a key to set */
2692                 if ( (erq->length < SMALL_KEY_SIZE) || (erq->length > ORINOCO_MAX_KEY_SIZE) )
2693                         return -EINVAL;
2694                 
2695                 if (copy_from_user(keybuf, erq->pointer, erq->length))
2696                         return -EFAULT;
2697         }
2698
2699         if (orinoco_lock(priv, &flags) != 0)
2700                 return -EBUSY;
2701         
2702         if (erq->pointer) {
2703                 if (erq->length > ORINOCO_MAX_KEY_SIZE) {
2704                         err = -E2BIG;
2705                         goto out;
2706                 }
2707                 
2708                 if ( (erq->length > LARGE_KEY_SIZE)
2709                      || ( ! priv->has_big_wep && (erq->length > SMALL_KEY_SIZE))  ) {
2710                         err = -EINVAL;
2711                         goto out;
2712                 }
2713                 
2714                 if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
2715                         index = priv->tx_key;
2716
2717                 /* Adjust key length to a supported value */
2718                 if (erq->length > SMALL_KEY_SIZE) {
2719                         xlen = LARGE_KEY_SIZE;
2720                 } else if (erq->length > 0) {
2721                         xlen = SMALL_KEY_SIZE;
2722                 } else
2723                         xlen = 0;
2724                 
2725                 /* Switch on WEP if off */
2726                 if ((!enable) && (xlen > 0)) {
2727                         setindex = index;
2728                         enable = 1;
2729                 }
2730         } else {
2731                 /* Important note : if the user do "iwconfig eth0 enc off",
2732                  * we will arrive there with an index of -1. This is valid
2733                  * but need to be taken care off... Jean II */
2734                 if ((index < 0) || (index >= ORINOCO_MAX_KEYS)) {
2735                         if((index != -1) || (erq->flags == 0)) {
2736                                 err = -EINVAL;
2737                                 goto out;
2738                         }
2739                 } else {
2740                         /* Set the index : Check that the key is valid */
2741                         if(priv->keys[index].len == 0) {
2742                                 err = -EINVAL;
2743                                 goto out;
2744                         }
2745                         setindex = index;
2746                 }
2747         }
2748         
2749         if (erq->flags & IW_ENCODE_DISABLED)
2750                 enable = 0;
2751         /* Only for Prism2 & Symbol cards (so far) - Jean II */
2752         if (erq->flags & IW_ENCODE_OPEN)
2753                 restricted = 0;
2754         if (erq->flags & IW_ENCODE_RESTRICTED)
2755                 restricted = 1;
2756
2757         if (erq->pointer) {
2758                 priv->keys[index].len = cpu_to_le16(xlen);
2759                 memset(priv->keys[index].data, 0,
2760                        sizeof(priv->keys[index].data));
2761                 memcpy(priv->keys[index].data, keybuf, erq->length);
2762         }
2763         priv->tx_key = setindex;
2764         priv->wep_on = enable;
2765         priv->wep_restrict = restricted;
2766
2767  out:
2768         orinoco_unlock(priv, &flags);
2769
2770         return err;
2771 }
2772
2773 static int orinoco_ioctl_getiwencode(struct net_device *dev, struct iw_point *erq)
2774 {
2775         struct orinoco_private *priv = netdev_priv(dev);
2776         int index = (erq->flags & IW_ENCODE_INDEX) - 1;
2777         u16 xlen = 0;
2778         char keybuf[ORINOCO_MAX_KEY_SIZE];
2779         unsigned long flags;
2780
2781         if (orinoco_lock(priv, &flags) != 0)
2782                 return -EBUSY;
2783
2784         if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
2785                 index = priv->tx_key;
2786
2787         erq->flags = 0;
2788         if (! priv->wep_on)
2789                 erq->flags |= IW_ENCODE_DISABLED;
2790         erq->flags |= index + 1;
2791         
2792         /* Only for symbol cards - Jean II */
2793         if (priv->firmware_type != FIRMWARE_TYPE_AGERE) {
2794                 if(priv->wep_restrict)
2795                         erq->flags |= IW_ENCODE_RESTRICTED;
2796                 else
2797                         erq->flags |= IW_ENCODE_OPEN;
2798         }
2799
2800         xlen = le16_to_cpu(priv->keys[index].len);
2801
2802         erq->length = xlen;
2803
2804         if (erq->pointer) {
2805                 memcpy(keybuf, priv->keys[index].data, ORINOCO_MAX_KEY_SIZE);
2806         }
2807         
2808         orinoco_unlock(priv, &flags);
2809
2810         if (erq->pointer) {
2811                 if (copy_to_user(erq->pointer, keybuf, xlen))
2812                         return -EFAULT;
2813         }
2814
2815         return 0;
2816 }
2817
2818 static int orinoco_ioctl_setessid(struct net_device *dev, struct iw_point *erq)
2819 {
2820         struct orinoco_private *priv = netdev_priv(dev);
2821         char essidbuf[IW_ESSID_MAX_SIZE+1];
2822         unsigned long flags;
2823
2824         /* Note : ESSID is ignored in Ad-Hoc demo mode, but we can set it
2825          * anyway... - Jean II */
2826
2827         memset(&essidbuf, 0, sizeof(essidbuf));
2828
2829         if (erq->flags) {
2830                 if (erq->length > IW_ESSID_MAX_SIZE)
2831                         return -E2BIG;
2832                 
2833                 if (copy_from_user(&essidbuf, erq->pointer, erq->length))
2834                         return -EFAULT;
2835
2836                 essidbuf[erq->length] = '\0';
2837         }
2838
2839         if (orinoco_lock(priv, &flags) != 0)
2840                 return -EBUSY;
2841
2842         memcpy(priv->desired_essid, essidbuf, sizeof(priv->desired_essid));
2843
2844         orinoco_unlock(priv, &flags);
2845
2846         return 0;
2847 }
2848
2849 static int orinoco_ioctl_getessid(struct net_device *dev, struct iw_point *erq)
2850 {
2851         struct orinoco_private *priv = netdev_priv(dev);
2852         char essidbuf[IW_ESSID_MAX_SIZE+1];
2853         int active;
2854         int err = 0;
2855         unsigned long flags;
2856
2857         TRACE_ENTER(dev->name);
2858
2859         if (netif_running(dev)) {
2860                 err = orinoco_hw_get_essid(priv, &active, essidbuf);
2861                 if (err)
2862                         return err;
2863         } else {
2864                 if (orinoco_lock(priv, &flags) != 0)
2865                         return -EBUSY;
2866                 memcpy(essidbuf, priv->desired_essid, sizeof(essidbuf));
2867                 orinoco_unlock(priv, &flags);
2868         }
2869
2870         erq->flags = 1;
2871         erq->length = strlen(essidbuf) + 1;
2872         if (erq->pointer)
2873                 if (copy_to_user(erq->pointer, essidbuf, erq->length))
2874                         return -EFAULT;
2875
2876         TRACE_EXIT(dev->name);
2877         
2878         return 0;
2879 }
2880
2881 static int orinoco_ioctl_setnick(struct net_device *dev, struct iw_point *nrq)
2882 {
2883         struct orinoco_private *priv = netdev_priv(dev);
2884         char nickbuf[IW_ESSID_MAX_SIZE+1];
2885         unsigned long flags;
2886
2887         if (nrq->length > IW_ESSID_MAX_SIZE)
2888                 return -E2BIG;
2889
2890         memset(nickbuf, 0, sizeof(nickbuf));
2891
2892         if (copy_from_user(nickbuf, nrq->pointer, nrq->length))
2893                 return -EFAULT;
2894
2895         nickbuf[nrq->length] = '\0';
2896         
2897         if (orinoco_lock(priv, &flags) != 0)
2898                 return -EBUSY;
2899
2900         memcpy(priv->nick, nickbuf, sizeof(priv->nick));
2901
2902         orinoco_unlock(priv, &flags);
2903
2904         return 0;
2905 }
2906
2907 static int orinoco_ioctl_getnick(struct net_device *dev, struct iw_point *nrq)
2908 {
2909         struct orinoco_private *priv = netdev_priv(dev);
2910         char nickbuf[IW_ESSID_MAX_SIZE+1];
2911         unsigned long flags;
2912
2913         if (orinoco_lock(priv, &flags) != 0)
2914                 return -EBUSY;
2915
2916         memcpy(nickbuf, priv->nick, IW_ESSID_MAX_SIZE+1);
2917         orinoco_unlock(priv, &flags);
2918
2919         nrq->length = strlen(nickbuf)+1;
2920
2921         if (copy_to_user(nrq->pointer, nickbuf, sizeof(nickbuf)))
2922                 return -EFAULT;
2923
2924         return 0;
2925 }
2926
2927 static int orinoco_ioctl_setfreq(struct net_device *dev, struct iw_freq *frq)
2928 {
2929         struct orinoco_private *priv = netdev_priv(dev);
2930         int chan = -1;
2931         unsigned long flags;
2932
2933         /* We can only use this in Ad-Hoc demo mode to set the operating
2934          * frequency, or in IBSS mode to set the frequency where the IBSS
2935          * will be created - Jean II */
2936         if (priv->iw_mode != IW_MODE_ADHOC)
2937                 return -EOPNOTSUPP;
2938
2939         if ( (frq->e == 0) && (frq->m <= 1000) ) {
2940                 /* Setting by channel number */
2941                 chan = frq->m;
2942         } else {
2943                 /* Setting by frequency - search the table */
2944                 int mult = 1;
2945                 int i;
2946
2947                 for (i = 0; i < (6 - frq->e); i++)
2948                         mult *= 10;
2949
2950                 for (i = 0; i < NUM_CHANNELS; i++)
2951                         if (frq->m == (channel_frequency[i] * mult))
2952                                 chan = i+1;
2953         }
2954
2955         if ( (chan < 1) || (chan > NUM_CHANNELS) ||
2956              ! (priv->channel_mask & (1 << (chan-1)) ) )
2957                 return -EINVAL;
2958
2959         if (orinoco_lock(priv, &flags) != 0)
2960                 return -EBUSY;
2961         priv->channel = chan;
2962         orinoco_unlock(priv, &flags);
2963
2964         return 0;
2965 }
2966
2967 static int orinoco_ioctl_getsens(struct net_device *dev, struct iw_param *srq)
2968 {
2969         struct orinoco_private *priv = netdev_priv(dev);
2970         hermes_t *hw = &priv->hw;
2971         u16 val;
2972         int err;
2973         unsigned long flags;
2974
2975         if (!priv->has_sensitivity)
2976                 return -EOPNOTSUPP;
2977
2978         if (orinoco_lock(priv, &flags) != 0)
2979                 return -EBUSY;
2980         err = hermes_read_wordrec(hw, USER_BAP,
2981                                   HERMES_RID_CNFSYSTEMSCALE, &val);
2982         orinoco_unlock(priv, &flags);
2983
2984         if (err)
2985                 return err;
2986
2987         srq->value = val;
2988         srq->fixed = 0; /* auto */
2989
2990         return 0;
2991 }
2992
2993 static int orinoco_ioctl_setsens(struct net_device *dev, struct iw_param *srq)
2994 {
2995         struct orinoco_private *priv = netdev_priv(dev);
2996         int val = srq->value;
2997         unsigned long flags;
2998
2999         if (!priv->has_sensitivity)
3000                 return -EOPNOTSUPP;
3001
3002         if ((val < 1) || (val > 3))
3003                 return -EINVAL;
3004         
3005         if (orinoco_lock(priv, &flags) != 0)
3006                 return -EBUSY;
3007         priv->ap_density = val;
3008         orinoco_unlock(priv, &flags);
3009
3010         return 0;
3011 }
3012
3013 static int orinoco_ioctl_setrts(struct net_device *dev, struct iw_param *rrq)
3014 {
3015         struct orinoco_private *priv = netdev_priv(dev);
3016         int val = rrq->value;
3017         unsigned long flags;
3018
3019         if (rrq->disabled)
3020                 val = 2347;
3021
3022         if ( (val < 0) || (val > 2347) )
3023                 return -EINVAL;
3024
3025         if (orinoco_lock(priv, &flags) != 0)
3026                 return -EBUSY;
3027
3028         priv->rts_thresh = val;
3029         orinoco_unlock(priv, &flags);
3030
3031         return 0;
3032 }
3033
3034 static int orinoco_ioctl_setfrag(struct net_device *dev, struct iw_param *frq)
3035 {
3036         struct orinoco_private *priv = netdev_priv(dev);
3037         int err = 0;
3038         unsigned long flags;
3039
3040         if (orinoco_lock(priv, &flags) != 0)
3041                 return -EBUSY;
3042
3043         if (priv->has_mwo) {
3044                 if (frq->disabled)
3045                         priv->mwo_robust = 0;
3046                 else {
3047                         if (frq->fixed)
3048                                 printk(KERN_WARNING "%s: Fixed fragmentation not \
3049 supported on this firmware. Using MWO robust instead.\n", dev->name);
3050                         priv->mwo_robust = 1;
3051                 }
3052         } else {
3053                 if (frq->disabled)
3054                         priv->frag_thresh = 2346;
3055                 else {
3056                         if ( (frq->value < 256) || (frq->value > 2346) )
3057                                 err = -EINVAL;
3058                         else
3059                                 priv->frag_thresh = frq->value & ~0x1; /* must be even */
3060                 }
3061         }
3062
3063         orinoco_unlock(priv, &flags);
3064
3065         return err;
3066 }
3067
3068 static int orinoco_ioctl_getfrag(struct net_device *dev, struct iw_param *frq)
3069 {
3070         struct orinoco_private *priv = netdev_priv(dev);
3071         hermes_t *hw = &priv->hw;
3072         int err = 0;
3073         u16 val;
3074         unsigned long flags;
3075
3076         if (orinoco_lock(priv, &flags) != 0)
3077                 return -EBUSY;
3078         
3079         if (priv->has_mwo) {
3080                 err = hermes_read_wordrec(hw, USER_BAP,
3081                                           HERMES_RID_CNFMWOROBUST_AGERE,
3082                                           &val);
3083                 if (err)
3084                         val = 0;
3085
3086                 frq->value = val ? 2347 : 0;
3087                 frq->disabled = ! val;
3088                 frq->fixed = 0;
3089         } else {
3090                 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
3091                                           &val);
3092                 if (err)
3093                         val = 0;
3094
3095                 frq->value = val;
3096                 frq->disabled = (val >= 2346);
3097                 frq->fixed = 1;
3098         }
3099
3100         orinoco_unlock(priv, &flags);
3101         
3102         return err;
3103 }
3104
3105 static int orinoco_ioctl_setrate(struct net_device *dev, struct iw_param *rrq)
3106 {
3107         struct orinoco_private *priv = netdev_priv(dev);
3108         int err = 0;
3109         int ratemode = -1;
3110         int bitrate; /* 100s of kilobits */
3111         int i;
3112         unsigned long flags;
3113         
3114         /* As the user space doesn't know our highest rate, it uses -1
3115          * to ask us to set the highest rate.  Test it using "iwconfig
3116          * ethX rate auto" - Jean II */
3117         if (rrq->value == -1)
3118                 bitrate = 110;
3119         else {
3120                 if (rrq->value % 100000)
3121                         return -EINVAL;
3122                 bitrate = rrq->value / 100000;
3123         }
3124
3125         if ( (bitrate != 10) && (bitrate != 20) &&
3126              (bitrate != 55) && (bitrate != 110) )
3127                 return -EINVAL;
3128
3129         for (i = 0; i < BITRATE_TABLE_SIZE; i++)
3130                 if ( (bitrate_table[i].bitrate == bitrate) &&
3131                      (bitrate_table[i].automatic == ! rrq->fixed) ) {
3132                         ratemode = i;
3133                         break;
3134                 }
3135         
3136         if (ratemode == -1)
3137                 return -EINVAL;
3138
3139         if (orinoco_lock(priv, &flags) != 0)
3140                 return -EBUSY;
3141         priv->bitratemode = ratemode;
3142         orinoco_unlock(priv, &flags);
3143
3144         return err;
3145 }
3146
3147 static int orinoco_ioctl_getrate(struct net_device *dev, struct iw_param *rrq)
3148 {
3149         struct orinoco_private *priv = netdev_priv(dev);
3150         hermes_t *hw = &priv->hw;
3151         int err = 0;
3152         int ratemode;
3153         int i;
3154         u16 val;
3155         unsigned long flags;
3156
3157         if (orinoco_lock(priv, &flags) != 0)
3158                 return -EBUSY;
3159
3160         ratemode = priv->bitratemode;
3161
3162         BUG_ON((ratemode < 0) || (ratemode >= BITRATE_TABLE_SIZE));
3163
3164         rrq->value = bitrate_table[ratemode].bitrate * 100000;
3165         rrq->fixed = ! bitrate_table[ratemode].automatic;
3166         rrq->disabled = 0;
3167
3168         /* If the interface is running we try to find more about the
3169            current mode */
3170         if (netif_running(dev)) {
3171                 err = hermes_read_wordrec(hw, USER_BAP,
3172                                           HERMES_RID_CURRENTTXRATE, &val);
3173                 if (err)
3174                         goto out;
3175                 
3176                 switch (priv->firmware_type) {
3177                 case FIRMWARE_TYPE_AGERE: /* Lucent style rate */
3178                         /* Note : in Lucent firmware, the return value of
3179                          * HERMES_RID_CURRENTTXRATE is the bitrate in Mb/s,
3180                          * and therefore is totally different from the
3181                          * encoding of HERMES_RID_CNFTXRATECONTROL.
3182                          * Don't forget that 6Mb/s is really 5.5Mb/s */
3183                         if (val == 6)
3184                                 rrq->value = 5500000;
3185                         else
3186                                 rrq->value = val * 1000000;
3187                         break;
3188                 case FIRMWARE_TYPE_INTERSIL: /* Intersil style rate */
3189                 case FIRMWARE_TYPE_SYMBOL: /* Symbol style rate */
3190                         for (i = 0; i < BITRATE_TABLE_SIZE; i++)
3191                                 if (bitrate_table[i].intersil_txratectrl == val) {
3192                                         ratemode = i;
3193                                         break;
3194                                 }
3195                         if (i >= BITRATE_TABLE_SIZE)
3196                                 printk(KERN_INFO "%s: Unable to determine current bitrate (0x%04hx)\n",
3197                                        dev->name, val);
3198
3199                         rrq->value = bitrate_table[ratemode].bitrate * 100000;
3200                         break;
3201                 default:
3202                         BUG();
3203                 }
3204         }
3205
3206  out:
3207         orinoco_unlock(priv, &flags);
3208
3209         return err;
3210 }
3211
3212 static int orinoco_ioctl_setpower(struct net_device *dev, struct iw_param *prq)
3213 {
3214         struct orinoco_private *priv = netdev_priv(dev);
3215         int err = 0;
3216         unsigned long flags;
3217
3218         if (orinoco_lock(priv, &flags) != 0)
3219                 return -EBUSY;
3220
3221         if (prq->disabled) {
3222                 priv->pm_on = 0;
3223         } else {
3224                 switch (prq->flags & IW_POWER_MODE) {
3225                 case IW_POWER_UNICAST_R:
3226                         priv->pm_mcast = 0;
3227                         priv->pm_on = 1;
3228                         break;
3229                 case IW_POWER_ALL_R:
3230                         priv->pm_mcast = 1;
3231                         priv->pm_on = 1;
3232                         break;
3233                 case IW_POWER_ON:
3234                         /* No flags : but we may have a value - Jean II */
3235                         break;
3236                 default:
3237                         err = -EINVAL;
3238                 }
3239                 if (err)
3240                         goto out;
3241                 
3242                 if (prq->flags & IW_POWER_TIMEOUT) {
3243                         priv->pm_on = 1;
3244                         priv->pm_timeout = prq->value / 1000;
3245                 }
3246                 if (prq->flags & IW_POWER_PERIOD) {
3247                         priv->pm_on = 1;
3248                         priv->pm_period = prq->value / 1000;
3249                 }
3250                 /* It's valid to not have a value if we are just toggling
3251                  * the flags... Jean II */
3252                 if(!priv->pm_on) {
3253                         err = -EINVAL;
3254                         goto out;
3255                 }                       
3256         }
3257
3258  out:
3259         orinoco_unlock(priv, &flags);
3260
3261         return err;
3262 }
3263
3264 static int orinoco_ioctl_getpower(struct net_device *dev, struct iw_param *prq)
3265 {
3266         struct orinoco_private *priv = netdev_priv(dev);
3267         hermes_t *hw = &priv->hw;
3268         int err = 0;
3269         u16 enable, period, timeout, mcast;
3270         unsigned long flags;
3271
3272         if (orinoco_lock(priv, &flags) != 0)
3273                 return -EBUSY;
3274         
3275         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFPMENABLED, &enable);
3276         if (err)
3277                 goto out;
3278
3279         err = hermes_read_wordrec(hw, USER_BAP,
3280                                   HERMES_RID_CNFMAXSLEEPDURATION, &period);
3281         if (err)
3282                 goto out;
3283
3284         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFPMHOLDOVERDURATION, &timeout);
3285         if (err)
3286                 goto out;
3287
3288         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFMULTICASTRECEIVE, &mcast);
3289         if (err)
3290                 goto out;
3291
3292         prq->disabled = !enable;
3293         /* Note : by default, display the period */
3294         if ((prq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
3295                 prq->flags = IW_POWER_TIMEOUT;
3296                 prq->value = timeout * 1000;
3297         } else {
3298                 prq->flags = IW_POWER_PERIOD;
3299                 prq->value = period * 1000;
3300         }
3301         if (mcast)
3302                 prq->flags |= IW_POWER_ALL_R;
3303         else
3304                 prq->flags |= IW_POWER_UNICAST_R;
3305
3306  out:
3307         orinoco_unlock(priv, &flags);
3308
3309         return err;
3310 }
3311
3312 static int orinoco_ioctl_getretry(struct net_device *dev, struct iw_param *rrq)
3313 {
3314         struct orinoco_private *priv = netdev_priv(dev);
3315         hermes_t *hw = &priv->hw;
3316         int err = 0;
3317         u16 short_limit, long_limit, lifetime;
3318         unsigned long flags;
3319
3320         if (orinoco_lock(priv, &flags) != 0)
3321                 return -EBUSY;
3322         
3323         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_SHORTRETRYLIMIT,
3324                                   &short_limit);
3325         if (err)
3326                 goto out;
3327
3328         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_LONGRETRYLIMIT,
3329                                   &long_limit);
3330         if (err)
3331                 goto out;
3332
3333         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_MAXTRANSMITLIFETIME,
3334                                   &lifetime);
3335         if (err)
3336                 goto out;
3337
3338         rrq->disabled = 0;              /* Can't be disabled */
3339
3340         /* Note : by default, display the retry number */
3341         if ((rrq->flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME) {
3342                 rrq->flags = IW_RETRY_LIFETIME;
3343                 rrq->value = lifetime * 1000;   /* ??? */
3344         } else {
3345                 /* By default, display the min number */
3346                 if ((rrq->flags & IW_RETRY_MAX)) {
3347                         rrq->flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
3348                         rrq->value = long_limit;
3349                 } else {
3350                         rrq->flags = IW_RETRY_LIMIT;
3351                         rrq->value = short_limit;
3352                         if(short_limit != long_limit)
3353                                 rrq->flags |= IW_RETRY_MIN;
3354                 }
3355         }
3356
3357  out:
3358         orinoco_unlock(priv, &flags);
3359
3360         return err;
3361 }
3362
3363 static int orinoco_ioctl_setibssport(struct net_device *dev, struct iwreq *wrq)
3364 {
3365         struct orinoco_private *priv = netdev_priv(dev);
3366         int val = *( (int *) wrq->u.name );
3367         unsigned long flags;
3368
3369         if (orinoco_lock(priv, &flags) != 0)
3370                 return -EBUSY;
3371
3372         priv->ibss_port = val ;
3373
3374         /* Actually update the mode we are using */
3375         set_port_type(priv);
3376
3377         orinoco_unlock(priv, &flags);
3378         return 0;
3379 }
3380
3381 static int orinoco_ioctl_getibssport(struct net_device *dev, struct iwreq *wrq)
3382 {
3383         struct orinoco_private *priv = netdev_priv(dev);
3384         int *val = (int *)wrq->u.name;
3385         unsigned long flags;
3386
3387         if (orinoco_lock(priv, &flags) != 0)
3388                 return -EBUSY;
3389
3390         *val = priv->ibss_port;
3391         orinoco_unlock(priv, &flags);
3392
3393         return 0;
3394 }
3395
3396 static int orinoco_ioctl_setport3(struct net_device *dev, struct iwreq *wrq)
3397 {
3398         struct orinoco_private *priv = netdev_priv(dev);
3399         int val = *( (int *) wrq->u.name );
3400         int err = 0;
3401         unsigned long flags;
3402
3403         if (orinoco_lock(priv, &flags) != 0)
3404                 return -EBUSY;
3405
3406         switch (val) {
3407         case 0: /* Try to do IEEE ad-hoc mode */
3408                 if (! priv->has_ibss) {
3409                         err = -EINVAL;
3410                         break;
3411                 }
3412                 priv->prefer_port3 = 0;
3413                         
3414                 break;
3415
3416         case 1: /* Try to do Lucent proprietary ad-hoc mode */
3417                 if (! priv->has_port3) {
3418                         err = -EINVAL;
3419                         break;
3420                 }
3421                 priv->prefer_port3 = 1;
3422                 break;
3423
3424         default:
3425                 err = -EINVAL;
3426         }
3427
3428         if (! err)
3429                 /* Actually update the mode we are using */
3430                 set_port_type(priv);
3431
3432         orinoco_unlock(priv, &flags);
3433
3434         return err;
3435 }
3436
3437 static int orinoco_ioctl_getport3(struct net_device *dev, struct iwreq *wrq)
3438 {
3439         struct orinoco_private *priv = netdev_priv(dev);
3440         int *val = (int *)wrq->u.name;
3441         unsigned long flags;
3442
3443         if (orinoco_lock(priv, &flags) != 0)
3444                 return -EBUSY;
3445
3446         *val = priv->prefer_port3;
3447         orinoco_unlock(priv, &flags);
3448         return 0;
3449 }
3450
3451 /* Spy is used for link quality/strength measurements in Ad-Hoc mode
3452  * Jean II */
3453 static int orinoco_ioctl_setspy(struct net_device *dev, struct iw_point *srq)
3454 {
3455         struct orinoco_private *priv = netdev_priv(dev);
3456         struct sockaddr address[IW_MAX_SPY];
3457         int number = srq->length;
3458         int i;
3459         int err = 0;
3460         unsigned long flags;
3461
3462         /* Check the number of addresses */
3463         if (number > IW_MAX_SPY)
3464                 return -E2BIG;
3465
3466         /* Get the data in the driver */
3467         if (srq->pointer) {
3468                 if (copy_from_user(address, srq->pointer,
3469                                    sizeof(struct sockaddr) * number))
3470                         return -EFAULT;
3471         }
3472
3473         /* Make sure nobody mess with the structure while we do */
3474         if (orinoco_lock(priv, &flags) != 0)
3475                 return -EBUSY;
3476
3477         /* orinoco_lock() doesn't disable interrupts, so make sure the
3478          * interrupt rx path don't get confused while we copy */
3479         priv->spy_number = 0;
3480
3481         if (number > 0) {
3482                 /* Extract the addresses */
3483                 for (i = 0; i < number; i++)
3484                         memcpy(priv->spy_address[i], address[i].sa_data,
3485                                ETH_ALEN);
3486                 /* Reset stats */
3487                 memset(priv->spy_stat, 0,
3488                        sizeof(struct iw_quality) * IW_MAX_SPY);
3489                 /* Set number of addresses */
3490                 priv->spy_number = number;
3491         }
3492
3493         /* Now, let the others play */
3494         orinoco_unlock(priv, &flags);
3495
3496         return err;
3497 }
3498
3499 static int orinoco_ioctl_getspy(struct net_device *dev, struct iw_point *srq)
3500 {
3501         struct orinoco_private *priv = netdev_priv(dev);
3502         struct sockaddr address[IW_MAX_SPY];
3503         struct iw_quality spy_stat[IW_MAX_SPY];
3504         int number;
3505         int i;
3506         unsigned long flags;
3507
3508         if (orinoco_lock(priv, &flags) != 0)
3509                 return -EBUSY;
3510
3511         number = priv->spy_number;
3512         if ((number > 0) && (srq->pointer)) {
3513                 /* Create address struct */
3514                 for (i = 0; i < number; i++) {
3515                         memcpy(address[i].sa_data, priv->spy_address[i],
3516                                ETH_ALEN);
3517                         address[i].sa_family = AF_UNIX;
3518                 }
3519                 /* Copy stats */
3520                 /* In theory, we should disable irqs while copying the stats
3521                  * because the rx path migh update it in the middle...
3522                  * Bah, who care ? - Jean II */
3523                 memcpy(&spy_stat, priv->spy_stat,
3524                        sizeof(struct iw_quality) * IW_MAX_SPY);
3525                 for (i=0; i < number; i++)
3526                         priv->spy_stat[i].updated = 0;
3527         }
3528
3529         orinoco_unlock(priv, &flags);
3530
3531         /* Push stuff to user space */
3532         srq->length = number;
3533         if(copy_to_user(srq->pointer, address,
3534                          sizeof(struct sockaddr) * number))
3535                 return -EFAULT;
3536         if(copy_to_user(srq->pointer + (sizeof(struct sockaddr)*number),
3537                         &spy_stat, sizeof(struct iw_quality) * number))
3538                 return -EFAULT;
3539
3540         return 0;
3541 }
3542
3543 static int
3544 orinoco_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3545 {
3546         struct orinoco_private *priv = netdev_priv(dev);
3547         struct iwreq *wrq = (struct iwreq *)rq;
3548         int err = 0;
3549         int tmp;
3550         int changed = 0;
3551         unsigned long flags;
3552
3553         TRACE_ENTER(dev->name);
3554
3555         /* In theory, we could allow most of the the SET stuff to be
3556          * done. In practice, the lapse of time at startup when the
3557          * card is not ready is very short, so why bother...  Note
3558          * that netif_device_present is different from up/down
3559          * (ifconfig), when the device is not yet up, it is usually
3560          * already ready...  Jean II */
3561         if (! netif_device_present(dev))
3562                 return -ENODEV;
3563
3564         switch (cmd) {
3565         case SIOCGIWNAME:
3566                 strcpy(wrq->u.name, "IEEE 802.11-DS");
3567                 break;
3568                 
3569         case SIOCGIWAP:
3570                 wrq->u.ap_addr.sa_family = ARPHRD_ETHER;
3571                 err = orinoco_hw_get_bssid(priv, wrq->u.ap_addr.sa_data);
3572                 break;
3573
3574         case SIOCGIWRANGE:
3575                 err = orinoco_ioctl_getiwrange(dev, &wrq->u.data);
3576                 break;
3577
3578         case SIOCSIWMODE:
3579                 if (orinoco_lock(priv, &flags) != 0)
3580                         return -EBUSY;
3581                 switch (wrq->u.mode) {
3582                 case IW_MODE_ADHOC:
3583                         if (! (priv->has_ibss || priv->has_port3) )
3584                                 err = -EINVAL;
3585                         else {
3586                                 priv->iw_mode = IW_MODE_ADHOC;
3587                                 changed = 1;
3588                         }
3589                         break;
3590
3591                 case IW_MODE_INFRA:
3592                         priv->iw_mode = IW_MODE_INFRA;
3593                         changed = 1;
3594                         break;
3595
3596                 default:
3597                         err = -EINVAL;
3598                         break;
3599                 }
3600                 set_port_type(priv);
3601                 orinoco_unlock(priv, &flags);
3602                 break;
3603
3604         case SIOCGIWMODE:
3605                 if (orinoco_lock(priv, &flags) != 0)
3606                         return -EBUSY;
3607                 wrq->u.mode = priv->iw_mode;
3608                 orinoco_unlock(priv, &flags);
3609                 break;
3610
3611         case SIOCSIWENCODE:
3612                 if (! priv->has_wep) {
3613                         err = -EOPNOTSUPP;
3614                         break;
3615                 }
3616
3617                 err = orinoco_ioctl_setiwencode(dev, &wrq->u.encoding);
3618                 if (! err)
3619                         changed = 1;
3620                 break;
3621
3622         case SIOCGIWENCODE:
3623                 if (! priv->has_wep) {
3624                         err = -EOPNOTSUPP;
3625                         break;
3626                 }
3627
3628                 if (! capable(CAP_NET_ADMIN)) {
3629                         err = -EPERM;
3630                         break;
3631                 }
3632
3633                 err = orinoco_ioctl_getiwencode(dev, &wrq->u.encoding);
3634                 break;
3635
3636         case SIOCSIWESSID:
3637                 err = orinoco_ioctl_setessid(dev, &wrq->u.essid);
3638                 if (! err)
3639                         changed = 1;
3640                 break;
3641
3642         case SIOCGIWESSID:
3643                 err = orinoco_ioctl_getessid(dev, &wrq->u.essid);
3644                 break;
3645
3646         case SIOCSIWNICKN:
3647                 err = orinoco_ioctl_setnick(dev, &wrq->u.data);
3648                 if (! err)
3649                         changed = 1;
3650                 break;
3651
3652         case SIOCGIWNICKN:
3653                 err = orinoco_ioctl_getnick(dev, &wrq->u.data);
3654                 break;
3655
3656         case SIOCGIWFREQ:
3657                 tmp = orinoco_hw_get_freq(priv);
3658                 if (tmp < 0) {
3659                         err = tmp;
3660                 } else {
3661                         wrq->u.freq.m = tmp;
3662                         wrq->u.freq.e = 1;
3663                 }
3664                 break;
3665
3666         case SIOCSIWFREQ:
3667                 err = orinoco_ioctl_setfreq(dev, &wrq->u.freq);
3668                 if (! err)
3669                         changed = 1;
3670                 break;
3671
3672         case SIOCGIWSENS:
3673                 err = orinoco_ioctl_getsens(dev, &wrq->u.sens);
3674                 break;
3675
3676         case SIOCSIWSENS:
3677                 err = orinoco_ioctl_setsens(dev, &wrq->u.sens);
3678                 if (! err)
3679                         changed = 1;
3680                 break;
3681
3682         case SIOCGIWRTS:
3683                 wrq->u.rts.value = priv->rts_thresh;
3684                 wrq->u.rts.disabled = (wrq->u.rts.value == 2347);
3685                 wrq->u.rts.fixed = 1;
3686                 break;
3687
3688         case SIOCSIWRTS:
3689                 err = orinoco_ioctl_setrts(dev, &wrq->u.rts);
3690                 if (! err)
3691                         changed = 1;
3692                 break;
3693
3694         case SIOCSIWFRAG:
3695                 err = orinoco_ioctl_setfrag(dev, &wrq->u.frag);
3696                 if (! err)
3697                         changed = 1;
3698                 break;
3699
3700         case SIOCGIWFRAG:
3701                 err = orinoco_ioctl_getfrag(dev, &wrq->u.frag);
3702                 break;
3703
3704         case SIOCSIWRATE:
3705                 err = orinoco_ioctl_setrate(dev, &wrq->u.bitrate);
3706                 if (! err)
3707                         changed = 1;
3708                 break;
3709
3710         case SIOCGIWRATE:
3711                 err = orinoco_ioctl_getrate(dev, &wrq->u.bitrate);
3712                 break;
3713
3714         case SIOCSIWPOWER:
3715                 err = orinoco_ioctl_setpower(dev, &wrq->u.power);
3716                 if (! err)
3717                         changed = 1;
3718                 break;
3719
3720         case SIOCGIWPOWER:
3721                 err = orinoco_ioctl_getpower(dev, &wrq->u.power);
3722                 break;
3723
3724         case SIOCGIWTXPOW:
3725                 /* The card only supports one tx power, so this is easy */
3726                 wrq->u.txpower.value = 15; /* dBm */
3727                 wrq->u.txpower.fixed = 1;
3728                 wrq->u.txpower.disabled = 0;
3729                 wrq->u.txpower.flags = IW_TXPOW_DBM;
3730                 break;
3731
3732         case SIOCSIWRETRY:
3733                 err = -EOPNOTSUPP;
3734                 break;
3735
3736         case SIOCGIWRETRY:
3737                 err = orinoco_ioctl_getretry(dev, &wrq->u.retry);
3738                 break;
3739
3740         case SIOCSIWSPY:
3741                 err = orinoco_ioctl_setspy(dev, &wrq->u.data);
3742                 break;
3743
3744         case SIOCGIWSPY:
3745                 err = orinoco_ioctl_getspy(dev, &wrq->u.data);
3746                 break;
3747
3748         case SIOCGIWPRIV:
3749                 if (wrq->u.data.pointer) {
3750                         struct iw_priv_args privtab[] = {
3751                                 { SIOCIWFIRSTPRIV + 0x0, 0, 0, "force_reset" },
3752                                 { SIOCIWFIRSTPRIV + 0x1, 0, 0, "card_reset" },
3753                                 { SIOCIWFIRSTPRIV + 0x2,
3754                                   IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
3755                                   0, "set_port3" },
3756                                 { SIOCIWFIRSTPRIV + 0x3, 0,
3757                                   IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
3758                                   "get_port3" },
3759                                 { SIOCIWFIRSTPRIV + 0x4,
3760                                   IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
3761                                   0, "set_preamble" },
3762                                 { SIOCIWFIRSTPRIV + 0x5, 0,
3763                                   IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
3764                                   "get_preamble" },
3765                                 { SIOCIWFIRSTPRIV + 0x6,
3766                                   IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
3767                                   0, "set_ibssport" },
3768                                 { SIOCIWFIRSTPRIV + 0x7, 0,
3769                                   IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
3770                                   "get_ibssport" },
3771                                 { SIOCIWLASTPRIV, 0, 0, "dump_recs" },
3772                         };
3773
3774                         wrq->u.data.length = sizeof(privtab) / sizeof(privtab[0]);
3775                         if (copy_to_user(wrq->u.data.pointer, privtab, sizeof(privtab)))
3776                                 err = -EFAULT;
3777                 }
3778                 break;
3779                
3780         case SIOCIWFIRSTPRIV + 0x0: /* force_reset */
3781         case SIOCIWFIRSTPRIV + 0x1: /* card_reset */
3782                 if (! capable(CAP_NET_ADMIN)) {
3783                         err = -EPERM;
3784                         break;
3785                 }
3786                 
3787                 printk(KERN_DEBUG "%s: Force scheduling reset!\n", dev->name);
3788
3789                 schedule_work(&priv->reset_work);
3790                 break;
3791
3792         case SIOCIWFIRSTPRIV + 0x2: /* set_port3 */
3793                 if (! capable(CAP_NET_ADMIN)) {
3794                         err = -EPERM;
3795                         break;
3796                 }
3797
3798                 err = orinoco_ioctl_setport3(dev, wrq);
3799                 if (! err)
3800                         changed = 1;
3801                 break;
3802
3803         case SIOCIWFIRSTPRIV + 0x3: /* get_port3 */
3804                 err = orinoco_ioctl_getport3(dev, wrq);
3805                 break;
3806
3807         case SIOCIWFIRSTPRIV + 0x4: /* set_preamble */
3808                 if (! capable(CAP_NET_ADMIN)) {
3809                         err = -EPERM;
3810                         break;
3811                 }
3812
3813                 /* 802.11b has recently defined some short preamble.
3814                  * Basically, the Phy header has been reduced in size.
3815                  * This increase performance, especially at high rates
3816                  * (the preamble is transmitted at 1Mb/s), unfortunately
3817                  * this give compatibility troubles... - Jean II */
3818                 if(priv->has_preamble) {
3819                         int val = *( (int *) wrq->u.name );
3820
3821                         if (orinoco_lock(priv, &flags) != 0)
3822                                 return -EBUSY;
3823                         if (val)
3824                                 priv->preamble = 1;
3825                         else
3826                                 priv->preamble = 0;
3827                         orinoco_unlock(priv, &flags);
3828                         changed = 1;
3829                 } else
3830                         err = -EOPNOTSUPP;
3831                 break;
3832
3833         case SIOCIWFIRSTPRIV + 0x5: /* get_preamble */
3834                 if(priv->has_preamble) {
3835                         int *val = (int *)wrq->u.name;
3836
3837                         if (orinoco_lock(priv, &flags) != 0)
3838                                 return -EBUSY;
3839                         *val = priv->preamble;
3840                         orinoco_unlock(priv, &flags);
3841                 } else
3842                         err = -EOPNOTSUPP;
3843                 break;
3844         case SIOCIWFIRSTPRIV + 0x6: /* set_ibssport */
3845                 if (! capable(CAP_NET_ADMIN)) {
3846                         err = -EPERM;
3847                         break;
3848                 }
3849
3850                 err = orinoco_ioctl_setibssport(dev, wrq);
3851                 if (! err)
3852                         changed = 1;
3853                 break;
3854
3855         case SIOCIWFIRSTPRIV + 0x7: /* get_ibssport */
3856                 err = orinoco_ioctl_getibssport(dev, wrq);
3857                 break;
3858
3859         case SIOCIWLASTPRIV:
3860                 err = orinoco_debug_dump_recs(dev);
3861                 if (err)
3862                         printk(KERN_ERR "%s: Unable to dump records (%d)\n",
3863                                dev->name, err);
3864                 break;
3865
3866
3867         default:
3868                 err = -EOPNOTSUPP;
3869         }
3870         
3871         if (! err && changed && netif_running(dev)) {
3872                 err = orinoco_reconfigure(dev);
3873         }               
3874
3875         TRACE_EXIT(dev->name);
3876
3877         return err;
3878 }
3879
3880 struct {
3881         u16 rid;
3882         char *name;
3883         int displaytype;
3884 #define DISPLAY_WORDS   0
3885 #define DISPLAY_BYTES   1
3886 #define DISPLAY_STRING  2
3887 #define DISPLAY_XSTRING 3
3888 } record_table[] = {
3889 #define DEBUG_REC(name,type) { HERMES_RID_##name, #name, DISPLAY_##type }
3890         DEBUG_REC(CNFPORTTYPE,WORDS),
3891         DEBUG_REC(CNFOWNMACADDR,BYTES),
3892         DEBUG_REC(CNFDESIREDSSID,STRING),
3893         DEBUG_REC(CNFOWNCHANNEL,WORDS),
3894         DEBUG_REC(CNFOWNSSID,STRING),
3895         DEBUG_REC(CNFOWNATIMWINDOW,WORDS),
3896         DEBUG_REC(CNFSYSTEMSCALE,WORDS),
3897         DEBUG_REC(CNFMAXDATALEN,WORDS),
3898         DEBUG_REC(CNFPMENABLED,WORDS),
3899         DEBUG_REC(CNFPMEPS,WORDS),
3900         DEBUG_REC(CNFMULTICASTRECEIVE,WORDS),
3901         DEBUG_REC(CNFMAXSLEEPDURATION,WORDS),
3902         DEBUG_REC(CNFPMHOLDOVERDURATION,WORDS),
3903         DEBUG_REC(CNFOWNNAME,STRING),
3904         DEBUG_REC(CNFOWNDTIMPERIOD,WORDS),
3905         DEBUG_REC(CNFMULTICASTPMBUFFERING,WORDS),
3906         DEBUG_REC(CNFWEPENABLED_AGERE,WORDS),
3907         DEBUG_REC(CNFMANDATORYBSSID_SYMBOL,WORDS),
3908         DEBUG_REC(CNFWEPDEFAULTKEYID,WORDS),
3909         DEBUG_REC(CNFDEFAULTKEY0,BYTES),
3910         DEBUG_REC(CNFDEFAULTKEY1,BYTES),
3911         DEBUG_REC(CNFMWOROBUST_AGERE,WORDS),
3912         DEBUG_REC(CNFDEFAULTKEY2,BYTES),
3913         DEBUG_REC(CNFDEFAULTKEY3,BYTES),
3914         DEBUG_REC(CNFWEPFLAGS_INTERSIL,WORDS),
3915         DEBUG_REC(CNFWEPKEYMAPPINGTABLE,WORDS),
3916         DEBUG_REC(CNFAUTHENTICATION,WORDS),
3917         DEBUG_REC(CNFMAXASSOCSTA,WORDS),
3918         DEBUG_REC(CNFKEYLENGTH_SYMBOL,WORDS),
3919         DEBUG_REC(CNFTXCONTROL,WORDS),
3920         DEBUG_REC(CNFROAMINGMODE,WORDS),
3921         DEBUG_REC(CNFHOSTAUTHENTICATION,WORDS),
3922         DEBUG_REC(CNFRCVCRCERROR,WORDS),
3923         DEBUG_REC(CNFMMLIFE,WORDS),
3924         DEBUG_REC(CNFALTRETRYCOUNT,WORDS),
3925         DEBUG_REC(CNFBEACONINT,WORDS),
3926         DEBUG_REC(CNFAPPCFINFO,WORDS),
3927         DEBUG_REC(CNFSTAPCFINFO,WORDS),
3928         DEBUG_REC(CNFPRIORITYQUSAGE,WORDS),
3929         DEBUG_REC(CNFTIMCTRL,WORDS),
3930         DEBUG_REC(CNFTHIRTY2TALLY,WORDS),
3931         DEBUG_REC(CNFENHSECURITY,WORDS),
3932         DEBUG_REC(CNFGROUPADDRESSES,BYTES),
3933         DEBUG_REC(CNFCREATEIBSS,WORDS),
3934         DEBUG_REC(CNFFRAGMENTATIONTHRESHOLD,WORDS),
3935         DEBUG_REC(CNFRTSTHRESHOLD,WORDS),
3936         DEBUG_REC(CNFTXRATECONTROL,WORDS),
3937         DEBUG_REC(CNFPROMISCUOUSMODE,WORDS),
3938         DEBUG_REC(CNFBASICRATES_SYMBOL,WORDS),
3939         DEBUG_REC(CNFPREAMBLE_SYMBOL,WORDS),
3940         DEBUG_REC(CNFSHORTPREAMBLE,WORDS),
3941         DEBUG_REC(CNFWEPKEYS_AGERE,BYTES),
3942         DEBUG_REC(CNFEXCLUDELONGPREAMBLE,WORDS),
3943         DEBUG_REC(CNFTXKEY_AGERE,WORDS),
3944         DEBUG_REC(CNFAUTHENTICATIONRSPTO,WORDS),
3945         DEBUG_REC(CNFBASICRATES,WORDS),
3946         DEBUG_REC(CNFSUPPORTEDRATES,WORDS),
3947         DEBUG_REC(CNFTICKTIME,WORDS),
3948         DEBUG_REC(CNFSCANREQUEST,WORDS),
3949         DEBUG_REC(CNFJOINREQUEST,WORDS),
3950         DEBUG_REC(CNFAUTHENTICATESTATION,WORDS),
3951         DEBUG_REC(CNFCHANNELINFOREQUEST,WORDS),
3952         DEBUG_REC(MAXLOADTIME,WORDS),
3953         DEBUG_REC(DOWNLOADBUFFER,WORDS),
3954         DEBUG_REC(PRIID,WORDS),
3955         DEBUG_REC(PRISUPRANGE,WORDS),
3956         DEBUG_REC(CFIACTRANGES,WORDS),
3957         DEBUG_REC(NICSERNUM,XSTRING),
3958         DEBUG_REC(NICID,WORDS),
3959         DEBUG_REC(MFISUPRANGE,WORDS),
3960         DEBUG_REC(CFISUPRANGE,WORDS),
3961         DEBUG_REC(CHANNELLIST,WORDS),
3962         DEBUG_REC(REGULATORYDOMAINS,WORDS),
3963         DEBUG_REC(TEMPTYPE,WORDS),
3964 /*      DEBUG_REC(CIS,BYTES), */
3965         DEBUG_REC(STAID,WORDS),
3966         DEBUG_REC(CURRENTSSID,STRING),
3967         DEBUG_REC(CURRENTBSSID,BYTES),
3968         DEBUG_REC(COMMSQUALITY,WORDS),
3969         DEBUG_REC(CURRENTTXRATE,WORDS),
3970         DEBUG_REC(CURRENTBEACONINTERVAL,WORDS),
3971         DEBUG_REC(CURRENTSCALETHRESHOLDS,WORDS),
3972         DEBUG_REC(PROTOCOLRSPTIME,WORDS),
3973         DEBUG_REC(SHORTRETRYLIMIT,WORDS),
3974         DEBUG_REC(LONGRETRYLIMIT,WORDS),
3975         DEBUG_REC(MAXTRANSMITLIFETIME,WORDS),
3976         DEBUG_REC(MAXRECEIVELIFETIME,WORDS),
3977         DEBUG_REC(CFPOLLABLE,WORDS),
3978         DEBUG_REC(AUTHENTICATIONALGORITHMS,WORDS),
3979         DEBUG_REC(PRIVACYOPTIONIMPLEMENTED,WORDS),
3980         DEBUG_REC(OWNMACADDR,BYTES),
3981         DEBUG_REC(SCANRESULTSTABLE,WORDS),
3982         DEBUG_REC(PHYTYPE,WORDS),
3983         DEBUG_REC(CURRENTCHANNEL,WORDS),
3984         DEBUG_REC(CURRENTPOWERSTATE,WORDS),
3985         DEBUG_REC(CCAMODE,WORDS),
3986         DEBUG_REC(SUPPORTEDDATARATES,WORDS),
3987         DEBUG_REC(BUILDSEQ,BYTES),
3988         DEBUG_REC(FWID,XSTRING)
3989 #undef DEBUG_REC
3990 };
3991
3992 #define DEBUG_LTV_SIZE          128
3993
3994 static int orinoco_debug_dump_recs(struct net_device *dev)
3995 {
3996         struct orinoco_private *priv = netdev_priv(dev);
3997         hermes_t *hw = &priv->hw;
3998         u8 *val8;
3999         u16 *val16;
4000         int i,j;
4001         u16 length;
4002         int err;
4003
4004         /* I'm not sure: we might have a lock here, so we'd better go
4005            atomic, just in case. */
4006         val8 = kmalloc(DEBUG_LTV_SIZE + 2, GFP_ATOMIC);
4007         if (! val8)
4008                 return -ENOMEM;
4009         val16 = (u16 *)val8;
4010
4011         for (i = 0; i < ARRAY_SIZE(record_table); i++) {
4012                 u16 rid = record_table[i].rid;
4013                 int len;
4014
4015                 memset(val8, 0, DEBUG_LTV_SIZE + 2);
4016
4017                 err = hermes_read_ltv(hw, USER_BAP, rid, DEBUG_LTV_SIZE,
4018                                       &length, val8);
4019                 if (err) {
4020                         DEBUG(0, "Error %d reading RID 0x%04x\n", err, rid);
4021                         continue;
4022                 }
4023                 val16 = (u16 *)val8;
4024                 if (length == 0)
4025                         continue;
4026
4027                 printk(KERN_DEBUG "%-15s (0x%04x): length=%d (%d bytes)\tvalue=",
4028                        record_table[i].name,
4029                        rid, length, (length-1)*2);
4030                 len = min(((int)length-1)*2, DEBUG_LTV_SIZE);
4031
4032                 switch (record_table[i].displaytype) {
4033                 case DISPLAY_WORDS:
4034                         for (j = 0; j < len / 2; j++)
4035                                 printk("%04X-", le16_to_cpu(val16[j]));
4036                         break;
4037
4038                 case DISPLAY_BYTES:
4039                 default:
4040                         for (j = 0; j < len; j++)
4041                                 printk("%02X:", val8[j]);
4042                         break;
4043
4044                 case DISPLAY_STRING:
4045                         len = min(len, le16_to_cpu(val16[0])+2);
4046                         val8[len] = '\0';
4047                         printk("\"%s\"", (char *)&val16[1]);
4048                         break;
4049
4050                 case DISPLAY_XSTRING:
4051                         printk("'%s'", (char *)val8);
4052                 }
4053
4054                 printk("\n");
4055         }
4056
4057         kfree(val8);
4058
4059         return 0;
4060 }
4061
4062 /********************************************************************/
4063 /* Debugging                                                        */
4064 /********************************************************************/
4065
4066 #if 0
4067 static void show_rx_frame(struct orinoco_rxframe_hdr *frame)
4068 {
4069         printk(KERN_DEBUG "RX descriptor:\n");
4070         printk(KERN_DEBUG "  status      = 0x%04x\n", frame->desc.status);
4071         printk(KERN_DEBUG "  time        = 0x%08x\n", frame->desc.time);
4072         printk(KERN_DEBUG "  silence     = 0x%02x\n", frame->desc.silence);
4073         printk(KERN_DEBUG "  signal      = 0x%02x\n", frame->desc.signal);
4074         printk(KERN_DEBUG "  rate        = 0x%02x\n", frame->desc.rate);
4075         printk(KERN_DEBUG "  rxflow      = 0x%02x\n", frame->desc.rxflow);
4076         printk(KERN_DEBUG "  reserved    = 0x%08x\n", frame->desc.reserved);
4077
4078         printk(KERN_DEBUG "IEEE 802.11 header:\n");
4079         printk(KERN_DEBUG "  frame_ctl   = 0x%04x\n",
4080                frame->p80211.frame_ctl);
4081         printk(KERN_DEBUG "  duration_id = 0x%04x\n",
4082                frame->p80211.duration_id);
4083         printk(KERN_DEBUG "  addr1       = %02x:%02x:%02x:%02x:%02x:%02x\n",
4084                frame->p80211.addr1[0], frame->p80211.addr1[1],
4085                frame->p80211.addr1[2], frame->p80211.addr1[3],
4086                frame->p80211.addr1[4], frame->p80211.addr1[5]);
4087         printk(KERN_DEBUG "  addr2       = %02x:%02x:%02x:%02x:%02x:%02x\n",
4088                frame->p80211.addr2[0], frame->p80211.addr2[1],
4089                frame->p80211.addr2[2], frame->p80211.addr2[3],
4090                frame->p80211.addr2[4], frame->p80211.addr2[5]);
4091         printk(KERN_DEBUG "  addr3       = %02x:%02x:%02x:%02x:%02x:%02x\n",
4092                frame->p80211.addr3[0], frame->p80211.addr3[1],
4093                frame->p80211.addr3[2], frame->p80211.addr3[3],
4094                frame->p80211.addr3[4], frame->p80211.addr3[5]);
4095         printk(KERN_DEBUG "  seq_ctl     = 0x%04x\n",
4096                frame->p80211.seq_ctl);
4097         printk(KERN_DEBUG "  addr4       = %02x:%02x:%02x:%02x:%02x:%02x\n",
4098                frame->p80211.addr4[0], frame->p80211.addr4[1],
4099                frame->p80211.addr4[2], frame->p80211.addr4[3],
4100                frame->p80211.addr4[4], frame->p80211.addr4[5]);
4101         printk(KERN_DEBUG "  data_len    = 0x%04x\n",
4102                frame->p80211.data_len);
4103
4104         printk(KERN_DEBUG "IEEE 802.3 header:\n");
4105         printk(KERN_DEBUG "  dest        = %02x:%02x:%02x:%02x:%02x:%02x\n",
4106                frame->p8023.h_dest[0], frame->p8023.h_dest[1],
4107                frame->p8023.h_dest[2], frame->p8023.h_dest[3],
4108                frame->p8023.h_dest[4], frame->p8023.h_dest[5]);
4109         printk(KERN_DEBUG "  src         = %02x:%02x:%02x:%02x:%02x:%02x\n",
4110                frame->p8023.h_source[0], frame->p8023.h_source[1],
4111                frame->p8023.h_source[2], frame->p8023.h_source[3],
4112                frame->p8023.h_source[4], frame->p8023.h_source[5]);
4113         printk(KERN_DEBUG "  len         = 0x%04x\n", frame->p8023.h_proto);
4114
4115         printk(KERN_DEBUG "IEEE 802.2 LLC/SNAP header:\n");
4116         printk(KERN_DEBUG "  DSAP        = 0x%02x\n", frame->p8022.dsap);
4117         printk(KERN_DEBUG "  SSAP        = 0x%02x\n", frame->p8022.ssap);
4118         printk(KERN_DEBUG "  ctrl        = 0x%02x\n", frame->p8022.ctrl);
4119         printk(KERN_DEBUG "  OUI         = %02x:%02x:%02x\n",
4120                frame->p8022.oui[0], frame->p8022.oui[1], frame->p8022.oui[2]);
4121         printk(KERN_DEBUG "  ethertype  = 0x%04x\n", frame->ethertype);
4122 }
4123 #endif /* 0 */
4124
4125 /********************************************************************/
4126 /* Module initialization                                            */
4127 /********************************************************************/
4128
4129 EXPORT_SYMBOL(alloc_orinocodev);
4130
4131 EXPORT_SYMBOL(__orinoco_up);
4132 EXPORT_SYMBOL(__orinoco_down);
4133 EXPORT_SYMBOL(orinoco_stop);
4134 EXPORT_SYMBOL(orinoco_reinit_firmware);
4135
4136 EXPORT_SYMBOL(orinoco_interrupt);
4137
4138 /* Can't be declared "const" or the whole __initdata section will
4139  * become const */
4140 static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
4141         " (David Gibson <hermes@gibson.dropbear.id.au>, "
4142         "Pavel Roskin <proski@gnu.org>, et al)";
4143
4144 static int __init init_orinoco(void)
4145 {
4146         printk(KERN_DEBUG "%s\n", version);
4147         return 0;
4148 }
4149
4150 static void __exit exit_orinoco(void)
4151 {
4152 }
4153
4154 module_init(init_orinoco);
4155 module_exit(exit_orinoco);