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