]> err.no Git - linux-2.6/blob - drivers/net/wireless/orinoco.c
[PATCH] orinoco: fix setting of 32 character ESSIDs
[linux-2.6] / drivers / net / wireless / orinoco.c
1 /* orinoco.c - (formerly known as dldwd_cs.c and orinoco_cs.c)
2  *
3  * A driver for Hermes or Prism 2 chipset based PCMCIA wireless
4  * adaptors, with Lucent/Agere, Intersil or Symbol firmware.
5  *
6  * Current maintainers (as of 29 September 2003) are:
7  *      Pavel Roskin <proski AT gnu.org>
8  * and  David Gibson <hermes AT gibson.dropbear.id.au>
9  *
10  * (C) Copyright David Gibson, IBM Corporation 2001-2003.
11  * Copyright (C) 2000 David Gibson, Linuxcare Australia.
12  *      With some help from :
13  * Copyright (C) 2001 Jean Tourrilhes, HP Labs
14  * Copyright (C) 2001 Benjamin Herrenschmidt
15  *
16  * Based on dummy_cs.c 1.27 2000/06/12 21:27:25
17  *
18  * Portions based on wvlan_cs.c 1.0.6, Copyright Andreas Neuhaus <andy
19  * AT fasta.fh-dortmund.de>
20  *      http://www.stud.fh-dortmund.de/~andy/wvlan/
21  *
22  * The contents of this file are subject to the Mozilla Public License
23  * Version 1.1 (the "License"); you may not use this file except in
24  * compliance with the License. You may obtain a copy of the License
25  * at http://www.mozilla.org/MPL/
26  *
27  * Software distributed under the License is distributed on an "AS IS"
28  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
29  * the License for the specific language governing rights and
30  * limitations under the License.
31  *
32  * The initial developer of the original code is David A. Hinds
33  * <dahinds AT users.sourceforge.net>.  Portions created by David
34  * A. Hinds are Copyright (C) 1999 David A. Hinds.  All Rights
35  * Reserved.
36  *
37  * Alternatively, the contents of this file may be used under the
38  * terms of the GNU General Public License version 2 (the "GPL"), in
39  * which case the provisions of the GPL are applicable instead of the
40  * above.  If you wish to allow the use of your version of this file
41  * only under the terms of the GPL and not to allow others to use your
42  * version of this file under the MPL, indicate your decision by
43  * deleting the provisions above and replace them with the notice and
44  * other provisions required by the GPL.  If you do not delete the
45  * provisions above, a recipient may use your version of this file
46  * under either the MPL or the GPL.  */
47
48 /*
49  * 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 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                 /* Read directly the data (no seek) */
1284                 hermes_read_words(hw, HERMES_DATA1, (void *) &tallies,
1285                                   len / 2); /* FIXME: blech! */
1286                 
1287                 /* Increment our various counters */
1288                 /* wstats->discard.nwid - no wrong BSSID stuff */
1289                 wstats->discard.code +=
1290                         le16_to_cpu(tallies.RxWEPUndecryptable);
1291                 if (len == sizeof(tallies))  
1292                         wstats->discard.code +=
1293                                 le16_to_cpu(tallies.RxDiscards_WEPICVError) +
1294                                 le16_to_cpu(tallies.RxDiscards_WEPExcluded);
1295                 wstats->discard.misc +=
1296                         le16_to_cpu(tallies.TxDiscardsWrongSA);
1297                 wstats->discard.fragment +=
1298                         le16_to_cpu(tallies.RxMsgInBadMsgFragments);
1299                 wstats->discard.retries +=
1300                         le16_to_cpu(tallies.TxRetryLimitExceeded);
1301                 /* wstats->miss.beacon - no match */
1302         }
1303         break;
1304         case HERMES_INQ_LINKSTATUS: {
1305                 struct hermes_linkstatus linkstatus;
1306                 u16 newstatus;
1307                 int connected;
1308
1309                 if (len != sizeof(linkstatus)) {
1310                         printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
1311                                dev->name, len);
1312                         break;
1313                 }
1314
1315                 hermes_read_words(hw, HERMES_DATA1, (void *) &linkstatus,
1316                                   len / 2);
1317                 newstatus = le16_to_cpu(linkstatus.linkstatus);
1318
1319                 connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
1320                         || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
1321                         || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
1322
1323                 if (connected)
1324                         netif_carrier_on(dev);
1325                 else if (!ignore_disconnect)
1326                         netif_carrier_off(dev);
1327
1328                 if (newstatus != priv->last_linkstatus)
1329                         print_linkstatus(dev, newstatus);
1330
1331                 priv->last_linkstatus = newstatus;
1332         }
1333         break;
1334         default:
1335                 printk(KERN_DEBUG "%s: Unknown information frame received: "
1336                        "type 0x%04x, length %d\n", dev->name, type, len);
1337                 /* We don't actually do anything about it */
1338                 break;
1339         }
1340 }
1341
1342 static void __orinoco_ev_infdrop(struct net_device *dev, hermes_t *hw)
1343 {
1344         if (net_ratelimit())
1345                 printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
1346 }
1347
1348 /********************************************************************/
1349 /* Internal hardware control routines                               */
1350 /********************************************************************/
1351
1352 int __orinoco_up(struct net_device *dev)
1353 {
1354         struct orinoco_private *priv = netdev_priv(dev);
1355         struct hermes *hw = &priv->hw;
1356         int err;
1357
1358         err = __orinoco_program_rids(dev);
1359         if (err) {
1360                 printk(KERN_ERR "%s: Error %d configuring card\n",
1361                        dev->name, err);
1362                 return err;
1363         }
1364
1365         /* Fire things up again */
1366         hermes_set_irqmask(hw, ORINOCO_INTEN);
1367         err = hermes_enable_port(hw, 0);
1368         if (err) {
1369                 printk(KERN_ERR "%s: Error %d enabling MAC port\n",
1370                        dev->name, err);
1371                 return err;
1372         }
1373
1374         netif_start_queue(dev);
1375
1376         return 0;
1377 }
1378
1379 int __orinoco_down(struct net_device *dev)
1380 {
1381         struct orinoco_private *priv = netdev_priv(dev);
1382         struct hermes *hw = &priv->hw;
1383         int err;
1384
1385         netif_stop_queue(dev);
1386
1387         if (! priv->hw_unavailable) {
1388                 if (! priv->broken_disableport) {
1389                         err = hermes_disable_port(hw, 0);
1390                         if (err) {
1391                                 /* Some firmwares (e.g. Intersil 1.3.x) seem
1392                                  * to have problems disabling the port, oh
1393                                  * well, too bad. */
1394                                 printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
1395                                        dev->name, err);
1396                                 priv->broken_disableport = 1;
1397                         }
1398                 }
1399                 hermes_set_irqmask(hw, 0);
1400                 hermes_write_regn(hw, EVACK, 0xffff);
1401         }
1402         
1403         /* firmware will have to reassociate */
1404         netif_carrier_off(dev);
1405         priv->last_linkstatus = 0xffff;
1406
1407         return 0;
1408 }
1409
1410 int orinoco_reinit_firmware(struct net_device *dev)
1411 {
1412         struct orinoco_private *priv = netdev_priv(dev);
1413         struct hermes *hw = &priv->hw;
1414         int err;
1415
1416         err = hermes_init(hw);
1417         if (err)
1418                 return err;
1419
1420         err = hermes_allocate(hw, priv->nicbuf_size, &priv->txfid);
1421         if (err == -EIO && priv->nicbuf_size > TX_NICBUF_SIZE_BUG) {
1422                 /* Try workaround for old Symbol firmware bug */
1423                 printk(KERN_WARNING "%s: firmware ALLOC bug detected "
1424                        "(old Symbol firmware?). Trying to work around... ",
1425                        dev->name);
1426                 
1427                 priv->nicbuf_size = TX_NICBUF_SIZE_BUG;
1428                 err = hermes_allocate(hw, priv->nicbuf_size, &priv->txfid);
1429                 if (err)
1430                         printk("failed!\n");
1431                 else
1432                         printk("ok.\n");
1433         }
1434
1435         return err;
1436 }
1437
1438 static int __orinoco_hw_set_bitrate(struct orinoco_private *priv)
1439 {
1440         hermes_t *hw = &priv->hw;
1441         int err = 0;
1442
1443         if (priv->bitratemode >= BITRATE_TABLE_SIZE) {
1444                 printk(KERN_ERR "%s: BUG: Invalid bitrate mode %d\n",
1445                        priv->ndev->name, priv->bitratemode);
1446                 return -EINVAL;
1447         }
1448
1449         switch (priv->firmware_type) {
1450         case FIRMWARE_TYPE_AGERE:
1451                 err = hermes_write_wordrec(hw, USER_BAP,
1452                                            HERMES_RID_CNFTXRATECONTROL,
1453                                            bitrate_table[priv->bitratemode].agere_txratectrl);
1454                 break;
1455         case FIRMWARE_TYPE_INTERSIL:
1456         case FIRMWARE_TYPE_SYMBOL:
1457                 err = hermes_write_wordrec(hw, USER_BAP,
1458                                            HERMES_RID_CNFTXRATECONTROL,
1459                                            bitrate_table[priv->bitratemode].intersil_txratectrl);
1460                 break;
1461         default:
1462                 BUG();
1463         }
1464
1465         return err;
1466 }
1467
1468 /* Change the WEP keys and/or the current keys.  Can be called
1469  * either from __orinoco_hw_setup_wep() or directly from
1470  * orinoco_ioctl_setiwencode().  In the later case the association
1471  * with the AP is not broken (if the firmware can handle it),
1472  * which is needed for 802.1x implementations. */
1473 static int __orinoco_hw_setup_wepkeys(struct orinoco_private *priv)
1474 {
1475         hermes_t *hw = &priv->hw;
1476         int err = 0;
1477
1478         switch (priv->firmware_type) {
1479         case FIRMWARE_TYPE_AGERE:
1480                 err = HERMES_WRITE_RECORD(hw, USER_BAP,
1481                                           HERMES_RID_CNFWEPKEYS_AGERE,
1482                                           &priv->keys);
1483                 if (err)
1484                         return err;
1485                 err = hermes_write_wordrec(hw, USER_BAP,
1486                                            HERMES_RID_CNFTXKEY_AGERE,
1487                                            priv->tx_key);
1488                 if (err)
1489                         return err;
1490                 break;
1491         case FIRMWARE_TYPE_INTERSIL:
1492         case FIRMWARE_TYPE_SYMBOL:
1493                 {
1494                         int keylen;
1495                         int i;
1496
1497                         /* Force uniform key length to work around firmware bugs */
1498                         keylen = le16_to_cpu(priv->keys[priv->tx_key].len);
1499                         
1500                         if (keylen > LARGE_KEY_SIZE) {
1501                                 printk(KERN_ERR "%s: BUG: Key %d has oversize length %d.\n",
1502                                        priv->ndev->name, priv->tx_key, keylen);
1503                                 return -E2BIG;
1504                         }
1505
1506                         /* Write all 4 keys */
1507                         for(i = 0; i < ORINOCO_MAX_KEYS; i++) {
1508                                 err = hermes_write_ltv(hw, USER_BAP,
1509                                                        HERMES_RID_CNFDEFAULTKEY0 + i,
1510                                                        HERMES_BYTES_TO_RECLEN(keylen),
1511                                                        priv->keys[i].data);
1512                                 if (err)
1513                                         return err;
1514                         }
1515
1516                         /* Write the index of the key used in transmission */
1517                         err = hermes_write_wordrec(hw, USER_BAP,
1518                                                    HERMES_RID_CNFWEPDEFAULTKEYID,
1519                                                    priv->tx_key);
1520                         if (err)
1521                                 return err;
1522                 }
1523                 break;
1524         }
1525
1526         return 0;
1527 }
1528
1529 static int __orinoco_hw_setup_wep(struct orinoco_private *priv)
1530 {
1531         hermes_t *hw = &priv->hw;
1532         int err = 0;
1533         int master_wep_flag;
1534         int auth_flag;
1535
1536         if (priv->wep_on)
1537                 __orinoco_hw_setup_wepkeys(priv);
1538
1539         if (priv->wep_restrict)
1540                 auth_flag = HERMES_AUTH_SHARED_KEY;
1541         else
1542                 auth_flag = HERMES_AUTH_OPEN;
1543
1544         switch (priv->firmware_type) {
1545         case FIRMWARE_TYPE_AGERE: /* Agere style WEP */
1546                 if (priv->wep_on) {
1547                         /* Enable the shared-key authentication. */
1548                         err = hermes_write_wordrec(hw, USER_BAP,
1549                                                    HERMES_RID_CNFAUTHENTICATION_AGERE,
1550                                                    auth_flag);
1551                 }
1552                 err = hermes_write_wordrec(hw, USER_BAP,
1553                                            HERMES_RID_CNFWEPENABLED_AGERE,
1554                                            priv->wep_on);
1555                 if (err)
1556                         return err;
1557                 break;
1558
1559         case FIRMWARE_TYPE_INTERSIL: /* Intersil style WEP */
1560         case FIRMWARE_TYPE_SYMBOL: /* Symbol style WEP */
1561                 if (priv->wep_on) {
1562                         if (priv->wep_restrict ||
1563                             (priv->firmware_type == FIRMWARE_TYPE_SYMBOL))
1564                                 master_wep_flag = HERMES_WEP_PRIVACY_INVOKED |
1565                                                   HERMES_WEP_EXCL_UNENCRYPTED;
1566                         else
1567                                 master_wep_flag = HERMES_WEP_PRIVACY_INVOKED;
1568
1569                         err = hermes_write_wordrec(hw, USER_BAP,
1570                                                    HERMES_RID_CNFAUTHENTICATION,
1571                                                    auth_flag);
1572                         if (err)
1573                                 return err;
1574                 } else
1575                         master_wep_flag = 0;
1576
1577                 if (priv->iw_mode == IW_MODE_MONITOR)
1578                         master_wep_flag |= HERMES_WEP_HOST_DECRYPT;
1579
1580                 /* Master WEP setting : on/off */
1581                 err = hermes_write_wordrec(hw, USER_BAP,
1582                                            HERMES_RID_CNFWEPFLAGS_INTERSIL,
1583                                            master_wep_flag);
1584                 if (err)
1585                         return err;     
1586
1587                 break;
1588         }
1589
1590         return 0;
1591 }
1592
1593 static int __orinoco_program_rids(struct net_device *dev)
1594 {
1595         struct orinoco_private *priv = netdev_priv(dev);
1596         hermes_t *hw = &priv->hw;
1597         int err;
1598         struct hermes_idstring idbuf;
1599
1600         /* Set the MAC address */
1601         err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
1602                                HERMES_BYTES_TO_RECLEN(ETH_ALEN), dev->dev_addr);
1603         if (err) {
1604                 printk(KERN_ERR "%s: Error %d setting MAC address\n",
1605                        dev->name, err);
1606                 return err;
1607         }
1608
1609         /* Set up the link mode */
1610         err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFPORTTYPE,
1611                                    priv->port_type);
1612         if (err) {
1613                 printk(KERN_ERR "%s: Error %d setting port type\n",
1614                        dev->name, err);
1615                 return err;
1616         }
1617         /* Set the channel/frequency */
1618         if (priv->channel != 0 && priv->iw_mode != IW_MODE_INFRA) {
1619                 err = hermes_write_wordrec(hw, USER_BAP,
1620                                            HERMES_RID_CNFOWNCHANNEL,
1621                                            priv->channel);
1622                 if (err) {
1623                         printk(KERN_ERR "%s: Error %d setting channel %d\n",
1624                                dev->name, err, priv->channel);
1625                         return err;
1626                 }
1627         }
1628
1629         if (priv->has_ibss) {
1630                 u16 createibss;
1631
1632                 if ((strlen(priv->desired_essid) == 0) && (priv->createibss)) {
1633                         printk(KERN_WARNING "%s: This firmware requires an "
1634                                "ESSID in IBSS-Ad-Hoc mode.\n", dev->name);
1635                         /* With wvlan_cs, in this case, we would crash.
1636                          * hopefully, this driver will behave better...
1637                          * Jean II */
1638                         createibss = 0;
1639                 } else {
1640                         createibss = priv->createibss;
1641                 }
1642                 
1643                 err = hermes_write_wordrec(hw, USER_BAP,
1644                                            HERMES_RID_CNFCREATEIBSS,
1645                                            createibss);
1646                 if (err) {
1647                         printk(KERN_ERR "%s: Error %d setting CREATEIBSS\n",
1648                                dev->name, err);
1649                         return err;
1650                 }
1651         }
1652
1653         /* Set the desired ESSID */
1654         idbuf.len = cpu_to_le16(strlen(priv->desired_essid));
1655         memcpy(&idbuf.val, priv->desired_essid, sizeof(idbuf.val));
1656         /* WinXP wants partner to configure OWNSSID even in IBSS mode. (jimc) */
1657         err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNSSID,
1658                                HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
1659                                &idbuf);
1660         if (err) {
1661                 printk(KERN_ERR "%s: Error %d setting OWNSSID\n",
1662                        dev->name, err);
1663                 return err;
1664         }
1665         err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFDESIREDSSID,
1666                                HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
1667                                &idbuf);
1668         if (err) {
1669                 printk(KERN_ERR "%s: Error %d setting DESIREDSSID\n",
1670                        dev->name, err);
1671                 return err;
1672         }
1673
1674         /* Set the station name */
1675         idbuf.len = cpu_to_le16(strlen(priv->nick));
1676         memcpy(&idbuf.val, priv->nick, sizeof(idbuf.val));
1677         err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
1678                                HERMES_BYTES_TO_RECLEN(strlen(priv->nick)+2),
1679                                &idbuf);
1680         if (err) {
1681                 printk(KERN_ERR "%s: Error %d setting nickname\n",
1682                        dev->name, err);
1683                 return err;
1684         }
1685
1686         /* Set AP density */
1687         if (priv->has_sensitivity) {
1688                 err = hermes_write_wordrec(hw, USER_BAP,
1689                                            HERMES_RID_CNFSYSTEMSCALE,
1690                                            priv->ap_density);
1691                 if (err) {
1692                         printk(KERN_WARNING "%s: Error %d setting SYSTEMSCALE.  "
1693                                "Disabling sensitivity control\n",
1694                                dev->name, err);
1695
1696                         priv->has_sensitivity = 0;
1697                 }
1698         }
1699
1700         /* Set RTS threshold */
1701         err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
1702                                    priv->rts_thresh);
1703         if (err) {
1704                 printk(KERN_ERR "%s: Error %d setting RTS threshold\n",
1705                        dev->name, err);
1706                 return err;
1707         }
1708
1709         /* Set fragmentation threshold or MWO robustness */
1710         if (priv->has_mwo)
1711                 err = hermes_write_wordrec(hw, USER_BAP,
1712                                            HERMES_RID_CNFMWOROBUST_AGERE,
1713                                            priv->mwo_robust);
1714         else
1715                 err = hermes_write_wordrec(hw, USER_BAP,
1716                                            HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
1717                                            priv->frag_thresh);
1718         if (err) {
1719                 printk(KERN_ERR "%s: Error %d setting fragmentation\n",
1720                        dev->name, err);
1721                 return err;
1722         }
1723
1724         /* Set bitrate */
1725         err = __orinoco_hw_set_bitrate(priv);
1726         if (err) {
1727                 printk(KERN_ERR "%s: Error %d setting bitrate\n",
1728                        dev->name, err);
1729                 return err;
1730         }
1731
1732         /* Set power management */
1733         if (priv->has_pm) {
1734                 err = hermes_write_wordrec(hw, USER_BAP,
1735                                            HERMES_RID_CNFPMENABLED,
1736                                            priv->pm_on);
1737                 if (err) {
1738                         printk(KERN_ERR "%s: Error %d setting up PM\n",
1739                                dev->name, err);
1740                         return err;
1741                 }
1742
1743                 err = hermes_write_wordrec(hw, USER_BAP,
1744                                            HERMES_RID_CNFMULTICASTRECEIVE,
1745                                            priv->pm_mcast);
1746                 if (err) {
1747                         printk(KERN_ERR "%s: Error %d setting up PM\n",
1748                                dev->name, err);
1749                         return err;
1750                 }
1751                 err = hermes_write_wordrec(hw, USER_BAP,
1752                                            HERMES_RID_CNFMAXSLEEPDURATION,
1753                                            priv->pm_period);
1754                 if (err) {
1755                         printk(KERN_ERR "%s: Error %d setting up PM\n",
1756                                dev->name, err);
1757                         return err;
1758                 }
1759                 err = hermes_write_wordrec(hw, USER_BAP,
1760                                            HERMES_RID_CNFPMHOLDOVERDURATION,
1761                                            priv->pm_timeout);
1762                 if (err) {
1763                         printk(KERN_ERR "%s: Error %d setting up PM\n",
1764                                dev->name, err);
1765                         return err;
1766                 }
1767         }
1768
1769         /* Set preamble - only for Symbol so far... */
1770         if (priv->has_preamble) {
1771                 err = hermes_write_wordrec(hw, USER_BAP,
1772                                            HERMES_RID_CNFPREAMBLE_SYMBOL,
1773                                            priv->preamble);
1774                 if (err) {
1775                         printk(KERN_ERR "%s: Error %d setting preamble\n",
1776                                dev->name, err);
1777                         return err;
1778                 }
1779         }
1780
1781         /* Set up encryption */
1782         if (priv->has_wep) {
1783                 err = __orinoco_hw_setup_wep(priv);
1784                 if (err) {
1785                         printk(KERN_ERR "%s: Error %d activating WEP\n",
1786                                dev->name, err);
1787                         return err;
1788                 }
1789         }
1790
1791         /* Set promiscuity / multicast*/
1792         priv->promiscuous = 0;
1793         priv->mc_count = 0;
1794         __orinoco_set_multicast_list(dev); /* FIXME: what about the xmit_lock */
1795
1796         return 0;
1797 }
1798
1799 /* FIXME: return int? */
1800 static void
1801 __orinoco_set_multicast_list(struct net_device *dev)
1802 {
1803         struct orinoco_private *priv = netdev_priv(dev);
1804         hermes_t *hw = &priv->hw;
1805         int err = 0;
1806         int promisc, mc_count;
1807
1808         /* The Hermes doesn't seem to have an allmulti mode, so we go
1809          * into promiscuous mode and let the upper levels deal. */
1810         if ( (dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
1811              (dev->mc_count > MAX_MULTICAST(priv)) ) {
1812                 promisc = 1;
1813                 mc_count = 0;
1814         } else {
1815                 promisc = 0;
1816                 mc_count = dev->mc_count;
1817         }
1818
1819         if (promisc != priv->promiscuous) {
1820                 err = hermes_write_wordrec(hw, USER_BAP,
1821                                            HERMES_RID_CNFPROMISCUOUSMODE,
1822                                            promisc);
1823                 if (err) {
1824                         printk(KERN_ERR "%s: Error %d setting PROMISCUOUSMODE to 1.\n",
1825                                dev->name, err);
1826                 } else 
1827                         priv->promiscuous = promisc;
1828         }
1829
1830         if (! promisc && (mc_count || priv->mc_count) ) {
1831                 struct dev_mc_list *p = dev->mc_list;
1832                 struct hermes_multicast mclist;
1833                 int i;
1834
1835                 for (i = 0; i < mc_count; i++) {
1836                         /* paranoia: is list shorter than mc_count? */
1837                         BUG_ON(! p);
1838                         /* paranoia: bad address size in list? */
1839                         BUG_ON(p->dmi_addrlen != ETH_ALEN);
1840                         
1841                         memcpy(mclist.addr[i], p->dmi_addr, ETH_ALEN);
1842                         p = p->next;
1843                 }
1844                 
1845                 if (p)
1846                         printk(KERN_WARNING "%s: Multicast list is "
1847                                "longer than mc_count\n", dev->name);
1848
1849                 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFGROUPADDRESSES,
1850                                        HERMES_BYTES_TO_RECLEN(priv->mc_count * ETH_ALEN),
1851                                        &mclist);
1852                 if (err)
1853                         printk(KERN_ERR "%s: Error %d setting multicast list.\n",
1854                                dev->name, err);
1855                 else
1856                         priv->mc_count = mc_count;
1857         }
1858
1859         /* Since we can set the promiscuous flag when it wasn't asked
1860            for, make sure the net_device knows about it. */
1861         if (priv->promiscuous)
1862                 dev->flags |= IFF_PROMISC;
1863         else
1864                 dev->flags &= ~IFF_PROMISC;
1865 }
1866
1867 static int orinoco_reconfigure(struct net_device *dev)
1868 {
1869         struct orinoco_private *priv = netdev_priv(dev);
1870         struct hermes *hw = &priv->hw;
1871         unsigned long flags;
1872         int err = 0;
1873
1874         if (priv->broken_disableport) {
1875                 schedule_work(&priv->reset_work);
1876                 return 0;
1877         }
1878
1879         if (orinoco_lock(priv, &flags) != 0)
1880                 return -EBUSY;
1881                 
1882         err = hermes_disable_port(hw, 0);
1883         if (err) {
1884                 printk(KERN_WARNING "%s: Unable to disable port while reconfiguring card\n",
1885                        dev->name);
1886                 priv->broken_disableport = 1;
1887                 goto out;
1888         }
1889
1890         err = __orinoco_program_rids(dev);
1891         if (err) {
1892                 printk(KERN_WARNING "%s: Unable to reconfigure card\n",
1893                        dev->name);
1894                 goto out;
1895         }
1896
1897         err = hermes_enable_port(hw, 0);
1898         if (err) {
1899                 printk(KERN_WARNING "%s: Unable to enable port while reconfiguring card\n",
1900                        dev->name);
1901                 goto out;
1902         }
1903
1904  out:
1905         if (err) {
1906                 printk(KERN_WARNING "%s: Resetting instead...\n", dev->name);
1907                 schedule_work(&priv->reset_work);
1908                 err = 0;
1909         }
1910
1911         orinoco_unlock(priv, &flags);
1912         return err;
1913
1914 }
1915
1916 /* This must be called from user context, without locks held - use
1917  * schedule_work() */
1918 static void orinoco_reset(struct net_device *dev)
1919 {
1920         struct orinoco_private *priv = netdev_priv(dev);
1921         struct hermes *hw = &priv->hw;
1922         int err = 0;
1923         unsigned long flags;
1924
1925         if (orinoco_lock(priv, &flags) != 0)
1926                 /* When the hardware becomes available again, whatever
1927                  * detects that is responsible for re-initializing
1928                  * it. So no need for anything further */
1929                 return;
1930
1931         netif_stop_queue(dev);
1932
1933         /* Shut off interrupts.  Depending on what state the hardware
1934          * is in, this might not work, but we'll try anyway */
1935         hermes_set_irqmask(hw, 0);
1936         hermes_write_regn(hw, EVACK, 0xffff);
1937
1938         priv->hw_unavailable++;
1939         priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
1940         netif_carrier_off(dev);
1941
1942         orinoco_unlock(priv, &flags);
1943
1944         if (priv->hard_reset)
1945                 err = (*priv->hard_reset)(priv);
1946         if (err) {
1947                 printk(KERN_ERR "%s: orinoco_reset: Error %d "
1948                        "performing  hard reset\n", dev->name, err);
1949                 /* FIXME: shutdown of some sort */
1950                 return;
1951         }
1952
1953         err = orinoco_reinit_firmware(dev);
1954         if (err) {
1955                 printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
1956                        dev->name, err);
1957                 return;
1958         }
1959
1960         spin_lock_irq(&priv->lock); /* This has to be called from user context */
1961
1962         priv->hw_unavailable--;
1963
1964         /* priv->open or priv->hw_unavailable might have changed while
1965          * we dropped the lock */
1966         if (priv->open && (! priv->hw_unavailable)) {
1967                 err = __orinoco_up(dev);
1968                 if (err) {
1969                         printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
1970                                dev->name, err);
1971                 } else
1972                         dev->trans_start = jiffies;
1973         }
1974
1975         spin_unlock_irq(&priv->lock);
1976
1977         return;
1978 }
1979
1980 /********************************************************************/
1981 /* Interrupt handler                                                */
1982 /********************************************************************/
1983
1984 static void __orinoco_ev_tick(struct net_device *dev, hermes_t *hw)
1985 {
1986         printk(KERN_DEBUG "%s: TICK\n", dev->name);
1987 }
1988
1989 static void __orinoco_ev_wterr(struct net_device *dev, hermes_t *hw)
1990 {
1991         /* This seems to happen a fair bit under load, but ignoring it
1992            seems to work fine...*/
1993         printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
1994                dev->name);
1995 }
1996
1997 irqreturn_t orinoco_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1998 {
1999         struct net_device *dev = (struct net_device *)dev_id;
2000         struct orinoco_private *priv = netdev_priv(dev);
2001         hermes_t *hw = &priv->hw;
2002         int count = MAX_IRQLOOPS_PER_IRQ;
2003         u16 evstat, events;
2004         /* These are used to detect a runaway interrupt situation */
2005         /* If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
2006          * we panic and shut down the hardware */
2007         static int last_irq_jiffy = 0; /* jiffies value the last time
2008                                         * we were called */
2009         static int loops_this_jiffy = 0;
2010         unsigned long flags;
2011
2012         if (orinoco_lock(priv, &flags) != 0) {
2013                 /* If hw is unavailable - we don't know if the irq was
2014                  * for us or not */
2015                 return IRQ_HANDLED;
2016         }
2017
2018         evstat = hermes_read_regn(hw, EVSTAT);
2019         events = evstat & hw->inten;
2020         if (! events) {
2021                 orinoco_unlock(priv, &flags);
2022                 return IRQ_NONE;
2023         }
2024         
2025         if (jiffies != last_irq_jiffy)
2026                 loops_this_jiffy = 0;
2027         last_irq_jiffy = jiffies;
2028
2029         while (events && count--) {
2030                 if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
2031                         printk(KERN_WARNING "%s: IRQ handler is looping too "
2032                                "much! Resetting.\n", dev->name);
2033                         /* Disable interrupts for now */
2034                         hermes_set_irqmask(hw, 0);
2035                         schedule_work(&priv->reset_work);
2036                         break;
2037                 }
2038
2039                 /* Check the card hasn't been removed */
2040                 if (! hermes_present(hw)) {
2041                         DEBUG(0, "orinoco_interrupt(): card removed\n");
2042                         break;
2043                 }
2044
2045                 if (events & HERMES_EV_TICK)
2046                         __orinoco_ev_tick(dev, hw);
2047                 if (events & HERMES_EV_WTERR)
2048                         __orinoco_ev_wterr(dev, hw);
2049                 if (events & HERMES_EV_INFDROP)
2050                         __orinoco_ev_infdrop(dev, hw);
2051                 if (events & HERMES_EV_INFO)
2052                         __orinoco_ev_info(dev, hw);
2053                 if (events & HERMES_EV_RX)
2054                         __orinoco_ev_rx(dev, hw);
2055                 if (events & HERMES_EV_TXEXC)
2056                         __orinoco_ev_txexc(dev, hw);
2057                 if (events & HERMES_EV_TX)
2058                         __orinoco_ev_tx(dev, hw);
2059                 if (events & HERMES_EV_ALLOC)
2060                         __orinoco_ev_alloc(dev, hw);
2061                 
2062                 hermes_write_regn(hw, EVACK, events);
2063
2064                 evstat = hermes_read_regn(hw, EVSTAT);
2065                 events = evstat & hw->inten;
2066         };
2067
2068         orinoco_unlock(priv, &flags);
2069         return IRQ_HANDLED;
2070 }
2071
2072 /********************************************************************/
2073 /* Initialization                                                   */
2074 /********************************************************************/
2075
2076 struct comp_id {
2077         u16 id, variant, major, minor;
2078 } __attribute__ ((packed));
2079
2080 static inline fwtype_t determine_firmware_type(struct comp_id *nic_id)
2081 {
2082         if (nic_id->id < 0x8000)
2083                 return FIRMWARE_TYPE_AGERE;
2084         else if (nic_id->id == 0x8000 && nic_id->major == 0)
2085                 return FIRMWARE_TYPE_SYMBOL;
2086         else
2087                 return FIRMWARE_TYPE_INTERSIL;
2088 }
2089
2090 /* Set priv->firmware type, determine firmware properties */
2091 static int determine_firmware(struct net_device *dev)
2092 {
2093         struct orinoco_private *priv = netdev_priv(dev);
2094         hermes_t *hw = &priv->hw;
2095         int err;
2096         struct comp_id nic_id, sta_id;
2097         unsigned int firmver;
2098         char tmp[SYMBOL_MAX_VER_LEN+1];
2099
2100         /* Get the hardware version */
2101         err = HERMES_READ_RECORD(hw, USER_BAP, HERMES_RID_NICID, &nic_id);
2102         if (err) {
2103                 printk(KERN_ERR "%s: Cannot read hardware identity: error %d\n",
2104                        dev->name, err);
2105                 return err;
2106         }
2107
2108         le16_to_cpus(&nic_id.id);
2109         le16_to_cpus(&nic_id.variant);
2110         le16_to_cpus(&nic_id.major);
2111         le16_to_cpus(&nic_id.minor);
2112         printk(KERN_DEBUG "%s: Hardware identity %04x:%04x:%04x:%04x\n",
2113                dev->name, nic_id.id, nic_id.variant,
2114                nic_id.major, nic_id.minor);
2115
2116         priv->firmware_type = determine_firmware_type(&nic_id);
2117
2118         /* Get the firmware version */
2119         err = HERMES_READ_RECORD(hw, USER_BAP, HERMES_RID_STAID, &sta_id);
2120         if (err) {
2121                 printk(KERN_ERR "%s: Cannot read station identity: error %d\n",
2122                        dev->name, err);
2123                 return err;
2124         }
2125
2126         le16_to_cpus(&sta_id.id);
2127         le16_to_cpus(&sta_id.variant);
2128         le16_to_cpus(&sta_id.major);
2129         le16_to_cpus(&sta_id.minor);
2130         printk(KERN_DEBUG "%s: Station identity  %04x:%04x:%04x:%04x\n",
2131                dev->name, sta_id.id, sta_id.variant,
2132                sta_id.major, sta_id.minor);
2133
2134         switch (sta_id.id) {
2135         case 0x15:
2136                 printk(KERN_ERR "%s: Primary firmware is active\n",
2137                        dev->name);
2138                 return -ENODEV;
2139         case 0x14b:
2140                 printk(KERN_ERR "%s: Tertiary firmware is active\n",
2141                        dev->name);
2142                 return -ENODEV;
2143         case 0x1f:      /* Intersil, Agere, Symbol Spectrum24 */
2144         case 0x21:      /* Symbol Spectrum24 Trilogy */
2145                 break;
2146         default:
2147                 printk(KERN_NOTICE "%s: Unknown station ID, please report\n",
2148                        dev->name);
2149                 break;
2150         }
2151
2152         /* Default capabilities */
2153         priv->has_sensitivity = 1;
2154         priv->has_mwo = 0;
2155         priv->has_preamble = 0;
2156         priv->has_port3 = 1;
2157         priv->has_ibss = 1;
2158         priv->has_wep = 0;
2159         priv->has_big_wep = 0;
2160
2161         /* Determine capabilities from the firmware version */
2162         switch (priv->firmware_type) {
2163         case FIRMWARE_TYPE_AGERE:
2164                 /* Lucent Wavelan IEEE, Lucent Orinoco, Cabletron RoamAbout,
2165                    ELSA, Melco, HP, IBM, Dell 1150, Compaq 110/210 */
2166                 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
2167                          "Lucent/Agere %d.%02d", sta_id.major, sta_id.minor);
2168
2169                 firmver = ((unsigned long)sta_id.major << 16) | sta_id.minor;
2170
2171                 priv->has_ibss = (firmver >= 0x60006);
2172                 priv->has_wep = (firmver >= 0x40020);
2173                 priv->has_big_wep = 1; /* FIXME: this is wrong - how do we tell
2174                                           Gold cards from the others? */
2175                 priv->has_mwo = (firmver >= 0x60000);
2176                 priv->has_pm = (firmver >= 0x40020); /* Don't work in 7.52 ? */
2177                 priv->ibss_port = 1;
2178
2179                 /* Tested with Agere firmware :
2180                  *      1.16 ; 4.08 ; 4.52 ; 6.04 ; 6.16 ; 7.28 => Jean II
2181                  * Tested CableTron firmware : 4.32 => Anton */
2182                 break;
2183         case FIRMWARE_TYPE_SYMBOL:
2184                 /* Symbol , 3Com AirConnect, Intel, Ericsson WLAN */
2185                 /* Intel MAC : 00:02:B3:* */
2186                 /* 3Com MAC : 00:50:DA:* */
2187                 memset(tmp, 0, sizeof(tmp));
2188                 /* Get the Symbol firmware version */
2189                 err = hermes_read_ltv(hw, USER_BAP,
2190                                       HERMES_RID_SECONDARYVERSION_SYMBOL,
2191                                       SYMBOL_MAX_VER_LEN, NULL, &tmp);
2192                 if (err) {
2193                         printk(KERN_WARNING
2194                                "%s: Error %d reading Symbol firmware info. Wildly guessing capabilities...\n",
2195                                dev->name, err);
2196                         firmver = 0;
2197                         tmp[0] = '\0';
2198                 } else {
2199                         /* The firmware revision is a string, the format is
2200                          * something like : "V2.20-01".
2201                          * Quick and dirty parsing... - Jean II
2202                          */
2203                         firmver = ((tmp[1] - '0') << 16) | ((tmp[3] - '0') << 12)
2204                                 | ((tmp[4] - '0') << 8) | ((tmp[6] - '0') << 4)
2205                                 | (tmp[7] - '0');
2206
2207                         tmp[SYMBOL_MAX_VER_LEN] = '\0';
2208                 }
2209
2210                 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
2211                          "Symbol %s", tmp);
2212
2213                 priv->has_ibss = (firmver >= 0x20000);
2214                 priv->has_wep = (firmver >= 0x15012);
2215                 priv->has_big_wep = (firmver >= 0x20000);
2216                 priv->has_pm = (firmver >= 0x20000 && firmver < 0x22000) || 
2217                                (firmver >= 0x29000 && firmver < 0x30000) ||
2218                                firmver >= 0x31000;
2219                 priv->has_preamble = (firmver >= 0x20000);
2220                 priv->ibss_port = 4;
2221                 /* Tested with Intel firmware : 0x20015 => Jean II */
2222                 /* Tested with 3Com firmware : 0x15012 & 0x22001 => Jean II */
2223                 break;
2224         case FIRMWARE_TYPE_INTERSIL:
2225                 /* D-Link, Linksys, Adtron, ZoomAir, and many others...
2226                  * Samsung, Compaq 100/200 and Proxim are slightly
2227                  * different and less well tested */
2228                 /* D-Link MAC : 00:40:05:* */
2229                 /* Addtron MAC : 00:90:D1:* */
2230                 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
2231                          "Intersil %d.%d.%d", sta_id.major, sta_id.minor,
2232                          sta_id.variant);
2233
2234                 firmver = ((unsigned long)sta_id.major << 16) |
2235                         ((unsigned long)sta_id.minor << 8) | sta_id.variant;
2236
2237                 priv->has_ibss = (firmver >= 0x000700); /* FIXME */
2238                 priv->has_big_wep = priv->has_wep = (firmver >= 0x000800);
2239                 priv->has_pm = (firmver >= 0x000700);
2240
2241                 if (firmver >= 0x000800)
2242                         priv->ibss_port = 0;
2243                 else {
2244                         printk(KERN_NOTICE "%s: Intersil firmware earlier "
2245                                "than v0.8.x - several features not supported\n",
2246                                dev->name);
2247                         priv->ibss_port = 1;
2248                 }
2249                 break;
2250         }
2251         printk(KERN_DEBUG "%s: Firmware determined as %s\n", dev->name,
2252                priv->fw_name);
2253
2254         return 0;
2255 }
2256
2257 static int orinoco_init(struct net_device *dev)
2258 {
2259         struct orinoco_private *priv = netdev_priv(dev);
2260         hermes_t *hw = &priv->hw;
2261         int err = 0;
2262         struct hermes_idstring nickbuf;
2263         u16 reclen;
2264         int len;
2265
2266         TRACE_ENTER(dev->name);
2267
2268         /* No need to lock, the hw_unavailable flag is already set in
2269          * alloc_orinocodev() */
2270         priv->nicbuf_size = IEEE802_11_FRAME_LEN + ETH_HLEN;
2271
2272         /* Initialize the firmware */
2273         err = orinoco_reinit_firmware(dev);
2274         if (err != 0) {
2275                 printk(KERN_ERR "%s: failed to initialize firmware (err = %d)\n",
2276                        dev->name, err);
2277                 goto out;
2278         }
2279
2280         err = determine_firmware(dev);
2281         if (err != 0) {
2282                 printk(KERN_ERR "%s: Incompatible firmware, aborting\n",
2283                        dev->name);
2284                 goto out;
2285         }
2286
2287         if (priv->has_port3)
2288                 printk(KERN_DEBUG "%s: Ad-hoc demo mode supported\n", dev->name);
2289         if (priv->has_ibss)
2290                 printk(KERN_DEBUG "%s: IEEE standard IBSS ad-hoc mode supported\n",
2291                        dev->name);
2292         if (priv->has_wep) {
2293                 printk(KERN_DEBUG "%s: WEP supported, ", dev->name);
2294                 if (priv->has_big_wep)
2295                         printk("104-bit key\n");
2296                 else
2297                         printk("40-bit key\n");
2298         }
2299
2300         /* Get the MAC address */
2301         err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
2302                               ETH_ALEN, NULL, dev->dev_addr);
2303         if (err) {
2304                 printk(KERN_WARNING "%s: failed to read MAC address!\n",
2305                        dev->name);
2306                 goto out;
2307         }
2308
2309         printk(KERN_DEBUG "%s: MAC address %02X:%02X:%02X:%02X:%02X:%02X\n",
2310                dev->name, dev->dev_addr[0], dev->dev_addr[1],
2311                dev->dev_addr[2], dev->dev_addr[3], dev->dev_addr[4],
2312                dev->dev_addr[5]);
2313
2314         /* Get the station name */
2315         err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
2316                               sizeof(nickbuf), &reclen, &nickbuf);
2317         if (err) {
2318                 printk(KERN_ERR "%s: failed to read station name\n",
2319                        dev->name);
2320                 goto out;
2321         }
2322         if (nickbuf.len)
2323                 len = min(IW_ESSID_MAX_SIZE, (int)le16_to_cpu(nickbuf.len));
2324         else
2325                 len = min(IW_ESSID_MAX_SIZE, 2 * reclen);
2326         memcpy(priv->nick, &nickbuf.val, len);
2327         priv->nick[len] = '\0';
2328
2329         printk(KERN_DEBUG "%s: Station name \"%s\"\n", dev->name, priv->nick);
2330
2331         /* Get allowed channels */
2332         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CHANNELLIST,
2333                                   &priv->channel_mask);
2334         if (err) {
2335                 printk(KERN_ERR "%s: failed to read channel list!\n",
2336                        dev->name);
2337                 goto out;
2338         }
2339
2340         /* Get initial AP density */
2341         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFSYSTEMSCALE,
2342                                   &priv->ap_density);
2343         if (err || priv->ap_density < 1 || priv->ap_density > 3) {
2344                 priv->has_sensitivity = 0;
2345         }
2346
2347         /* Get initial RTS threshold */
2348         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
2349                                   &priv->rts_thresh);
2350         if (err) {
2351                 printk(KERN_ERR "%s: failed to read RTS threshold!\n",
2352                        dev->name);
2353                 goto out;
2354         }
2355
2356         /* Get initial fragmentation settings */
2357         if (priv->has_mwo)
2358                 err = hermes_read_wordrec(hw, USER_BAP,
2359                                           HERMES_RID_CNFMWOROBUST_AGERE,
2360                                           &priv->mwo_robust);
2361         else
2362                 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
2363                                           &priv->frag_thresh);
2364         if (err) {
2365                 printk(KERN_ERR "%s: failed to read fragmentation settings!\n",
2366                        dev->name);
2367                 goto out;
2368         }
2369
2370         /* Power management setup */
2371         if (priv->has_pm) {
2372                 priv->pm_on = 0;
2373                 priv->pm_mcast = 1;
2374                 err = hermes_read_wordrec(hw, USER_BAP,
2375                                           HERMES_RID_CNFMAXSLEEPDURATION,
2376                                           &priv->pm_period);
2377                 if (err) {
2378                         printk(KERN_ERR "%s: failed to read power management period!\n",
2379                                dev->name);
2380                         goto out;
2381                 }
2382                 err = hermes_read_wordrec(hw, USER_BAP,
2383                                           HERMES_RID_CNFPMHOLDOVERDURATION,
2384                                           &priv->pm_timeout);
2385                 if (err) {
2386                         printk(KERN_ERR "%s: failed to read power management timeout!\n",
2387                                dev->name);
2388                         goto out;
2389                 }
2390         }
2391
2392         /* Preamble setup */
2393         if (priv->has_preamble) {
2394                 err = hermes_read_wordrec(hw, USER_BAP,
2395                                           HERMES_RID_CNFPREAMBLE_SYMBOL,
2396                                           &priv->preamble);
2397                 if (err)
2398                         goto out;
2399         }
2400                 
2401         /* Set up the default configuration */
2402         priv->iw_mode = IW_MODE_INFRA;
2403         /* By default use IEEE/IBSS ad-hoc mode if we have it */
2404         priv->prefer_port3 = priv->has_port3 && (! priv->has_ibss);
2405         set_port_type(priv);
2406         priv->channel = 0; /* use firmware default */
2407
2408         priv->promiscuous = 0;
2409         priv->wep_on = 0;
2410         priv->tx_key = 0;
2411
2412         /* Make the hardware available, as long as it hasn't been
2413          * removed elsewhere (e.g. by PCMCIA hot unplug) */
2414         spin_lock_irq(&priv->lock);
2415         priv->hw_unavailable--;
2416         spin_unlock_irq(&priv->lock);
2417
2418         printk(KERN_DEBUG "%s: ready\n", dev->name);
2419
2420  out:
2421         TRACE_EXIT(dev->name);
2422         return err;
2423 }
2424
2425 struct net_device *alloc_orinocodev(int sizeof_card,
2426                                     int (*hard_reset)(struct orinoco_private *))
2427 {
2428         struct net_device *dev;
2429         struct orinoco_private *priv;
2430
2431         dev = alloc_etherdev(sizeof(struct orinoco_private) + sizeof_card);
2432         if (! dev)
2433                 return NULL;
2434         priv = netdev_priv(dev);
2435         priv->ndev = dev;
2436         if (sizeof_card)
2437                 priv->card = (void *)((unsigned long)netdev_priv(dev)
2438                                       + sizeof(struct orinoco_private));
2439         else
2440                 priv->card = NULL;
2441
2442         /* Setup / override net_device fields */
2443         dev->init = orinoco_init;
2444         dev->hard_start_xmit = orinoco_xmit;
2445         dev->tx_timeout = orinoco_tx_timeout;
2446         dev->watchdog_timeo = HZ; /* 1 second timeout */
2447         dev->get_stats = orinoco_get_stats;
2448         dev->get_wireless_stats = orinoco_get_wireless_stats;
2449         dev->do_ioctl = orinoco_ioctl;
2450         dev->change_mtu = orinoco_change_mtu;
2451         dev->set_multicast_list = orinoco_set_multicast_list;
2452         /* we use the default eth_mac_addr for setting the MAC addr */
2453
2454         /* Set up default callbacks */
2455         dev->open = orinoco_open;
2456         dev->stop = orinoco_stop;
2457         priv->hard_reset = hard_reset;
2458
2459         spin_lock_init(&priv->lock);
2460         priv->open = 0;
2461         priv->hw_unavailable = 1; /* orinoco_init() must clear this
2462                                    * before anything else touches the
2463                                    * hardware */
2464         INIT_WORK(&priv->reset_work, (void (*)(void *))orinoco_reset, dev);
2465
2466         netif_carrier_off(dev);
2467         priv->last_linkstatus = 0xffff;
2468
2469         return dev;
2470
2471 }
2472
2473 void free_orinocodev(struct net_device *dev)
2474 {
2475         free_netdev(dev);
2476 }
2477
2478 /********************************************************************/
2479 /* Wireless extensions                                              */
2480 /********************************************************************/
2481
2482 static int orinoco_hw_get_bssid(struct orinoco_private *priv,
2483                                 char buf[ETH_ALEN])
2484 {
2485         hermes_t *hw = &priv->hw;
2486         int err = 0;
2487         unsigned long flags;
2488
2489         if (orinoco_lock(priv, &flags) != 0)
2490                 return -EBUSY;
2491
2492         err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
2493                               ETH_ALEN, NULL, buf);
2494
2495         orinoco_unlock(priv, &flags);
2496
2497         return err;
2498 }
2499
2500 static int orinoco_hw_get_essid(struct orinoco_private *priv, int *active,
2501                                 char buf[IW_ESSID_MAX_SIZE+1])
2502 {
2503         hermes_t *hw = &priv->hw;
2504         int err = 0;
2505         struct hermes_idstring essidbuf;
2506         char *p = (char *)(&essidbuf.val);
2507         int len;
2508         unsigned long flags;
2509
2510         if (orinoco_lock(priv, &flags) != 0)
2511                 return -EBUSY;
2512
2513         if (strlen(priv->desired_essid) > 0) {
2514                 /* We read the desired SSID from the hardware rather
2515                    than from priv->desired_essid, just in case the
2516                    firmware is allowed to change it on us. I'm not
2517                    sure about this */
2518                 /* My guess is that the OWNSSID should always be whatever
2519                  * we set to the card, whereas CURRENT_SSID is the one that
2520                  * may change... - Jean II */
2521                 u16 rid;
2522
2523                 *active = 1;
2524
2525                 rid = (priv->port_type == 3) ? HERMES_RID_CNFOWNSSID :
2526                         HERMES_RID_CNFDESIREDSSID;
2527                 
2528                 err = hermes_read_ltv(hw, USER_BAP, rid, sizeof(essidbuf),
2529                                       NULL, &essidbuf);
2530                 if (err)
2531                         goto fail_unlock;
2532         } else {
2533                 *active = 0;
2534
2535                 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTSSID,
2536                                       sizeof(essidbuf), NULL, &essidbuf);
2537                 if (err)
2538                         goto fail_unlock;
2539         }
2540
2541         len = le16_to_cpu(essidbuf.len);
2542
2543         memset(buf, 0, IW_ESSID_MAX_SIZE+1);
2544         memcpy(buf, p, len);
2545         buf[len] = '\0';
2546
2547  fail_unlock:
2548         orinoco_unlock(priv, &flags);
2549
2550         return err;       
2551 }
2552
2553 static long orinoco_hw_get_freq(struct orinoco_private *priv)
2554 {
2555         
2556         hermes_t *hw = &priv->hw;
2557         int err = 0;
2558         u16 channel;
2559         long freq = 0;
2560         unsigned long flags;
2561
2562         if (orinoco_lock(priv, &flags) != 0)
2563                 return -EBUSY;
2564         
2565         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CURRENTCHANNEL, &channel);
2566         if (err)
2567                 goto out;
2568
2569         /* Intersil firmware 1.3.5 returns 0 when the interface is down */
2570         if (channel == 0) {
2571                 err = -EBUSY;
2572                 goto out;
2573         }
2574
2575         if ( (channel < 1) || (channel > NUM_CHANNELS) ) {
2576                 printk(KERN_WARNING "%s: Channel out of range (%d)!\n",
2577                        priv->ndev->name, channel);
2578                 err = -EBUSY;
2579                 goto out;
2580
2581         }
2582         freq = channel_frequency[channel-1] * 100000;
2583
2584  out:
2585         orinoco_unlock(priv, &flags);
2586
2587         if (err > 0)
2588                 err = -EBUSY;
2589         return err ? err : freq;
2590 }
2591
2592 static int orinoco_hw_get_bitratelist(struct orinoco_private *priv,
2593                                       int *numrates, s32 *rates, int max)
2594 {
2595         hermes_t *hw = &priv->hw;
2596         struct hermes_idstring list;
2597         unsigned char *p = (unsigned char *)&list.val;
2598         int err = 0;
2599         int num;
2600         int i;
2601         unsigned long flags;
2602
2603         if (orinoco_lock(priv, &flags) != 0)
2604                 return -EBUSY;
2605
2606         err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_SUPPORTEDDATARATES,
2607                               sizeof(list), NULL, &list);
2608         orinoco_unlock(priv, &flags);
2609
2610         if (err)
2611                 return err;
2612         
2613         num = le16_to_cpu(list.len);
2614         *numrates = num;
2615         num = min(num, max);
2616
2617         for (i = 0; i < num; i++) {
2618                 rates[i] = (p[i] & 0x7f) * 500000; /* convert to bps */
2619         }
2620
2621         return 0;
2622 }
2623
2624 static int orinoco_ioctl_getiwrange(struct net_device *dev, struct iw_point *rrq)
2625 {
2626         struct orinoco_private *priv = netdev_priv(dev);
2627         int err = 0;
2628         int mode;
2629         struct iw_range range;
2630         int numrates;
2631         int i, k;
2632         unsigned long flags;
2633
2634         TRACE_ENTER(dev->name);
2635
2636         if (!access_ok(VERIFY_WRITE, rrq->pointer, sizeof(range)))
2637                 return -EFAULT;
2638
2639         rrq->length = sizeof(range);
2640
2641         if (orinoco_lock(priv, &flags) != 0)
2642                 return -EBUSY;
2643
2644         mode = priv->iw_mode;
2645         orinoco_unlock(priv, &flags);
2646
2647         memset(&range, 0, sizeof(range));
2648
2649         /* Much of this shamelessly taken from wvlan_cs.c. No idea
2650          * what it all means -dgibson */
2651         range.we_version_compiled = WIRELESS_EXT;
2652         range.we_version_source = 11;
2653
2654         range.min_nwid = range.max_nwid = 0; /* We don't use nwids */
2655
2656         /* Set available channels/frequencies */
2657         range.num_channels = NUM_CHANNELS;
2658         k = 0;
2659         for (i = 0; i < NUM_CHANNELS; i++) {
2660                 if (priv->channel_mask & (1 << i)) {
2661                         range.freq[k].i = i + 1;
2662                         range.freq[k].m = channel_frequency[i] * 100000;
2663                         range.freq[k].e = 1;
2664                         k++;
2665                 }
2666                 
2667                 if (k >= IW_MAX_FREQUENCIES)
2668                         break;
2669         }
2670         range.num_frequency = k;
2671
2672         range.sensitivity = 3;
2673
2674         if ((mode == IW_MODE_ADHOC) && (priv->spy_number == 0)){
2675                 /* Quality stats meaningless in ad-hoc mode */
2676                 range.max_qual.qual = 0;
2677                 range.max_qual.level = 0;
2678                 range.max_qual.noise = 0;
2679                 range.avg_qual.qual = 0;
2680                 range.avg_qual.level = 0;
2681                 range.avg_qual.noise = 0;
2682         } else {
2683                 range.max_qual.qual = 0x8b - 0x2f;
2684                 range.max_qual.level = 0x2f - 0x95 - 1;
2685                 range.max_qual.noise = 0x2f - 0x95 - 1;
2686                 /* Need to get better values */
2687                 range.avg_qual.qual = 0x24;
2688                 range.avg_qual.level = 0xC2;
2689                 range.avg_qual.noise = 0x9E;
2690         }
2691
2692         err = orinoco_hw_get_bitratelist(priv, &numrates,
2693                                          range.bitrate, IW_MAX_BITRATES);
2694         if (err)
2695                 return err;
2696         range.num_bitrates = numrates;
2697         
2698         /* Set an indication of the max TCP throughput in bit/s that we can
2699          * expect using this interface. May be use for QoS stuff...
2700          * Jean II */
2701         if(numrates > 2)
2702                 range.throughput = 5 * 1000 * 1000;     /* ~5 Mb/s */
2703         else
2704                 range.throughput = 1.5 * 1000 * 1000;   /* ~1.5 Mb/s */
2705
2706         range.min_rts = 0;
2707         range.max_rts = 2347;
2708         range.min_frag = 256;
2709         range.max_frag = 2346;
2710
2711         if (orinoco_lock(priv, &flags) != 0)
2712                 return -EBUSY;
2713         if (priv->has_wep) {
2714                 range.max_encoding_tokens = ORINOCO_MAX_KEYS;
2715
2716                 range.encoding_size[0] = SMALL_KEY_SIZE;
2717                 range.num_encoding_sizes = 1;
2718
2719                 if (priv->has_big_wep) {
2720                         range.encoding_size[1] = LARGE_KEY_SIZE;
2721                         range.num_encoding_sizes = 2;
2722                 }
2723         } else {
2724                 range.num_encoding_sizes = 0;
2725                 range.max_encoding_tokens = 0;
2726         }
2727         orinoco_unlock(priv, &flags);
2728                 
2729         range.min_pmp = 0;
2730         range.max_pmp = 65535000;
2731         range.min_pmt = 0;
2732         range.max_pmt = 65535 * 1000;   /* ??? */
2733         range.pmp_flags = IW_POWER_PERIOD;
2734         range.pmt_flags = IW_POWER_TIMEOUT;
2735         range.pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_UNICAST_R;
2736
2737         range.num_txpower = 1;
2738         range.txpower[0] = 15; /* 15dBm */
2739         range.txpower_capa = IW_TXPOW_DBM;
2740
2741         range.retry_capa = IW_RETRY_LIMIT | IW_RETRY_LIFETIME;
2742         range.retry_flags = IW_RETRY_LIMIT;
2743         range.r_time_flags = IW_RETRY_LIFETIME;
2744         range.min_retry = 0;
2745         range.max_retry = 65535;        /* ??? */
2746         range.min_r_time = 0;
2747         range.max_r_time = 65535 * 1000;        /* ??? */
2748
2749         if (copy_to_user(rrq->pointer, &range, sizeof(range)))
2750                 return -EFAULT;
2751
2752         TRACE_EXIT(dev->name);
2753
2754         return 0;
2755 }
2756
2757 static int orinoco_ioctl_setiwencode(struct net_device *dev, struct iw_point *erq)
2758 {
2759         struct orinoco_private *priv = netdev_priv(dev);
2760         int index = (erq->flags & IW_ENCODE_INDEX) - 1;
2761         int setindex = priv->tx_key;
2762         int enable = priv->wep_on;
2763         int restricted = priv->wep_restrict;
2764         u16 xlen = 0;
2765         int err = 0;
2766         char keybuf[ORINOCO_MAX_KEY_SIZE];
2767         unsigned long flags;
2768
2769         if (! priv->has_wep)
2770                 return -EOPNOTSUPP;
2771
2772         if (erq->pointer) {
2773                 /* We actually have a key to set - check its length */
2774                 if (erq->length > LARGE_KEY_SIZE)
2775                         return -E2BIG;
2776
2777                 if ( (erq->length > SMALL_KEY_SIZE) && !priv->has_big_wep )
2778                         return -E2BIG;
2779                 
2780                 if (copy_from_user(keybuf, erq->pointer, erq->length))
2781                         return -EFAULT;
2782         }
2783
2784         if (orinoco_lock(priv, &flags) != 0)
2785                 return -EBUSY;
2786
2787         if (erq->pointer) {
2788                 if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
2789                         index = priv->tx_key;
2790
2791                 /* Adjust key length to a supported value */
2792                 if (erq->length > SMALL_KEY_SIZE) {
2793                         xlen = LARGE_KEY_SIZE;
2794                 } else if (erq->length > 0) {
2795                         xlen = SMALL_KEY_SIZE;
2796                 } else
2797                         xlen = 0;
2798
2799                 /* Switch on WEP if off */
2800                 if ((!enable) && (xlen > 0)) {
2801                         setindex = index;
2802                         enable = 1;
2803                 }
2804         } else {
2805                 /* Important note : if the user do "iwconfig eth0 enc off",
2806                  * we will arrive there with an index of -1. This is valid
2807                  * but need to be taken care off... Jean II */
2808                 if ((index < 0) || (index >= ORINOCO_MAX_KEYS)) {
2809                         if((index != -1) || (erq->flags == 0)) {
2810                                 err = -EINVAL;
2811                                 goto out;
2812                         }
2813                 } else {
2814                         /* Set the index : Check that the key is valid */
2815                         if(priv->keys[index].len == 0) {
2816                                 err = -EINVAL;
2817                                 goto out;
2818                         }
2819                         setindex = index;
2820                 }
2821         }
2822
2823         if (erq->flags & IW_ENCODE_DISABLED)
2824                 enable = 0;
2825         if (erq->flags & IW_ENCODE_OPEN)
2826                 restricted = 0;
2827         if (erq->flags & IW_ENCODE_RESTRICTED)
2828                 restricted = 1;
2829
2830         if (erq->pointer) {
2831                 priv->keys[index].len = cpu_to_le16(xlen);
2832                 memset(priv->keys[index].data, 0,
2833                        sizeof(priv->keys[index].data));
2834                 memcpy(priv->keys[index].data, keybuf, erq->length);
2835         }
2836         priv->tx_key = setindex;
2837
2838         /* Try fast key change if connected and only keys are changed */
2839         if (priv->wep_on && enable && (priv->wep_restrict == restricted) &&
2840             netif_carrier_ok(dev)) {
2841                 err = __orinoco_hw_setup_wepkeys(priv);
2842                 /* No need to commit if successful */
2843                 goto out;
2844         }
2845
2846         priv->wep_on = enable;
2847         priv->wep_restrict = restricted;
2848
2849  out:
2850         orinoco_unlock(priv, &flags);
2851
2852         return err;
2853 }
2854
2855 static int orinoco_ioctl_getiwencode(struct net_device *dev, struct iw_point *erq)
2856 {
2857         struct orinoco_private *priv = netdev_priv(dev);
2858         int index = (erq->flags & IW_ENCODE_INDEX) - 1;
2859         u16 xlen = 0;
2860         char keybuf[ORINOCO_MAX_KEY_SIZE];
2861         unsigned long flags;
2862
2863         if (! priv->has_wep)
2864                 return -EOPNOTSUPP;
2865
2866         if (orinoco_lock(priv, &flags) != 0)
2867                 return -EBUSY;
2868
2869         if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
2870                 index = priv->tx_key;
2871
2872         erq->flags = 0;
2873         if (! priv->wep_on)
2874                 erq->flags |= IW_ENCODE_DISABLED;
2875         erq->flags |= index + 1;
2876
2877         if (priv->wep_restrict)
2878                 erq->flags |= IW_ENCODE_RESTRICTED;
2879         else
2880                 erq->flags |= IW_ENCODE_OPEN;
2881
2882         xlen = le16_to_cpu(priv->keys[index].len);
2883
2884         erq->length = xlen;
2885
2886         memcpy(keybuf, priv->keys[index].data, ORINOCO_MAX_KEY_SIZE);
2887
2888         orinoco_unlock(priv, &flags);
2889
2890         if (erq->pointer) {
2891                 if (copy_to_user(erq->pointer, keybuf, xlen))
2892                         return -EFAULT;
2893         }
2894
2895         return 0;
2896 }
2897
2898 static int orinoco_ioctl_setessid(struct net_device *dev, struct iw_point *erq)
2899 {
2900         struct orinoco_private *priv = netdev_priv(dev);
2901         char essidbuf[IW_ESSID_MAX_SIZE+1];
2902         unsigned long flags;
2903
2904         /* Note : ESSID is ignored in Ad-Hoc demo mode, but we can set it
2905          * anyway... - Jean II */
2906
2907         memset(&essidbuf, 0, sizeof(essidbuf));
2908
2909         if (erq->flags) {
2910                 /* iwconfig includes the NUL in the specified length */
2911                 if (erq->length > IW_ESSID_MAX_SIZE+1)
2912                         return -E2BIG;
2913                 
2914                 if (copy_from_user(&essidbuf, erq->pointer, erq->length))
2915                         return -EFAULT;
2916
2917                 essidbuf[IW_ESSID_MAX_SIZE] = '\0';
2918         }
2919
2920         if (orinoco_lock(priv, &flags) != 0)
2921                 return -EBUSY;
2922
2923         memcpy(priv->desired_essid, essidbuf, sizeof(priv->desired_essid));
2924
2925         orinoco_unlock(priv, &flags);
2926
2927         return 0;
2928 }
2929
2930 static int orinoco_ioctl_getessid(struct net_device *dev, struct iw_point *erq)
2931 {
2932         struct orinoco_private *priv = netdev_priv(dev);
2933         char essidbuf[IW_ESSID_MAX_SIZE+1];
2934         int active;
2935         int err = 0;
2936         unsigned long flags;
2937
2938         TRACE_ENTER(dev->name);
2939
2940         if (netif_running(dev)) {
2941                 err = orinoco_hw_get_essid(priv, &active, essidbuf);
2942                 if (err)
2943                         return err;
2944         } else {
2945                 if (orinoco_lock(priv, &flags) != 0)
2946                         return -EBUSY;
2947                 memcpy(essidbuf, priv->desired_essid, sizeof(essidbuf));
2948                 orinoco_unlock(priv, &flags);
2949         }
2950
2951         erq->flags = 1;
2952         erq->length = strlen(essidbuf) + 1;
2953         if (erq->pointer)
2954                 if (copy_to_user(erq->pointer, essidbuf, erq->length))
2955                         return -EFAULT;
2956
2957         TRACE_EXIT(dev->name);
2958         
2959         return 0;
2960 }
2961
2962 static int orinoco_ioctl_setnick(struct net_device *dev, struct iw_point *nrq)
2963 {
2964         struct orinoco_private *priv = netdev_priv(dev);
2965         char nickbuf[IW_ESSID_MAX_SIZE+1];
2966         unsigned long flags;
2967
2968         if (nrq->length > IW_ESSID_MAX_SIZE)
2969                 return -E2BIG;
2970
2971         memset(nickbuf, 0, sizeof(nickbuf));
2972
2973         if (copy_from_user(nickbuf, nrq->pointer, nrq->length))
2974                 return -EFAULT;
2975
2976         nickbuf[nrq->length] = '\0';
2977         
2978         if (orinoco_lock(priv, &flags) != 0)
2979                 return -EBUSY;
2980
2981         memcpy(priv->nick, nickbuf, sizeof(priv->nick));
2982
2983         orinoco_unlock(priv, &flags);
2984
2985         return 0;
2986 }
2987
2988 static int orinoco_ioctl_getnick(struct net_device *dev, struct iw_point *nrq)
2989 {
2990         struct orinoco_private *priv = netdev_priv(dev);
2991         char nickbuf[IW_ESSID_MAX_SIZE+1];
2992         unsigned long flags;
2993
2994         if (orinoco_lock(priv, &flags) != 0)
2995                 return -EBUSY;
2996
2997         memcpy(nickbuf, priv->nick, IW_ESSID_MAX_SIZE+1);
2998         orinoco_unlock(priv, &flags);
2999
3000         nrq->length = strlen(nickbuf)+1;
3001
3002         if (copy_to_user(nrq->pointer, nickbuf, sizeof(nickbuf)))
3003                 return -EFAULT;
3004
3005         return 0;
3006 }
3007
3008 static int orinoco_ioctl_setfreq(struct net_device *dev, struct iw_freq *frq)
3009 {
3010         struct orinoco_private *priv = netdev_priv(dev);
3011         int chan = -1;
3012         unsigned long flags;
3013
3014         /* We can only use this in Ad-Hoc demo mode to set the operating
3015          * frequency, or in IBSS mode to set the frequency where the IBSS
3016          * will be created - Jean II */
3017         if (priv->iw_mode != IW_MODE_ADHOC)
3018                 return -EOPNOTSUPP;
3019
3020         if ( (frq->e == 0) && (frq->m <= 1000) ) {
3021                 /* Setting by channel number */
3022                 chan = frq->m;
3023         } else {
3024                 /* Setting by frequency - search the table */
3025                 int mult = 1;
3026                 int i;
3027
3028                 for (i = 0; i < (6 - frq->e); i++)
3029                         mult *= 10;
3030
3031                 for (i = 0; i < NUM_CHANNELS; i++)
3032                         if (frq->m == (channel_frequency[i] * mult))
3033                                 chan = i+1;
3034         }
3035
3036         if ( (chan < 1) || (chan > NUM_CHANNELS) ||
3037              ! (priv->channel_mask & (1 << (chan-1)) ) )
3038                 return -EINVAL;
3039
3040         if (orinoco_lock(priv, &flags) != 0)
3041                 return -EBUSY;
3042         priv->channel = chan;
3043         orinoco_unlock(priv, &flags);
3044
3045         return 0;
3046 }
3047
3048 static int orinoco_ioctl_getsens(struct net_device *dev, struct iw_param *srq)
3049 {
3050         struct orinoco_private *priv = netdev_priv(dev);
3051         hermes_t *hw = &priv->hw;
3052         u16 val;
3053         int err;
3054         unsigned long flags;
3055
3056         if (!priv->has_sensitivity)
3057                 return -EOPNOTSUPP;
3058
3059         if (orinoco_lock(priv, &flags) != 0)
3060                 return -EBUSY;
3061         err = hermes_read_wordrec(hw, USER_BAP,
3062                                   HERMES_RID_CNFSYSTEMSCALE, &val);
3063         orinoco_unlock(priv, &flags);
3064
3065         if (err)
3066                 return err;
3067
3068         srq->value = val;
3069         srq->fixed = 0; /* auto */
3070
3071         return 0;
3072 }
3073
3074 static int orinoco_ioctl_setsens(struct net_device *dev, struct iw_param *srq)
3075 {
3076         struct orinoco_private *priv = netdev_priv(dev);
3077         int val = srq->value;
3078         unsigned long flags;
3079
3080         if (!priv->has_sensitivity)
3081                 return -EOPNOTSUPP;
3082
3083         if ((val < 1) || (val > 3))
3084                 return -EINVAL;
3085         
3086         if (orinoco_lock(priv, &flags) != 0)
3087                 return -EBUSY;
3088         priv->ap_density = val;
3089         orinoco_unlock(priv, &flags);
3090
3091         return 0;
3092 }
3093
3094 static int orinoco_ioctl_setrts(struct net_device *dev, struct iw_param *rrq)
3095 {
3096         struct orinoco_private *priv = netdev_priv(dev);
3097         int val = rrq->value;
3098         unsigned long flags;
3099
3100         if (rrq->disabled)
3101                 val = 2347;
3102
3103         if ( (val < 0) || (val > 2347) )
3104                 return -EINVAL;
3105
3106         if (orinoco_lock(priv, &flags) != 0)
3107                 return -EBUSY;
3108
3109         priv->rts_thresh = val;
3110         orinoco_unlock(priv, &flags);
3111
3112         return 0;
3113 }
3114
3115 static int orinoco_ioctl_setfrag(struct net_device *dev, struct iw_param *frq)
3116 {
3117         struct orinoco_private *priv = netdev_priv(dev);
3118         int err = 0;
3119         unsigned long flags;
3120
3121         if (orinoco_lock(priv, &flags) != 0)
3122                 return -EBUSY;
3123
3124         if (priv->has_mwo) {
3125                 if (frq->disabled)
3126                         priv->mwo_robust = 0;
3127                 else {
3128                         if (frq->fixed)
3129                                 printk(KERN_WARNING "%s: Fixed fragmentation is "
3130                                        "not supported on this firmware. "
3131                                        "Using MWO robust instead.\n", dev->name);
3132                         priv->mwo_robust = 1;
3133                 }
3134         } else {
3135                 if (frq->disabled)
3136                         priv->frag_thresh = 2346;
3137                 else {
3138                         if ( (frq->value < 256) || (frq->value > 2346) )
3139                                 err = -EINVAL;
3140                         else
3141                                 priv->frag_thresh = frq->value & ~0x1; /* must be even */
3142                 }
3143         }
3144
3145         orinoco_unlock(priv, &flags);
3146
3147         return err;
3148 }
3149
3150 static int orinoco_ioctl_getfrag(struct net_device *dev, struct iw_param *frq)
3151 {
3152         struct orinoco_private *priv = netdev_priv(dev);
3153         hermes_t *hw = &priv->hw;
3154         int err = 0;
3155         u16 val;
3156         unsigned long flags;
3157
3158         if (orinoco_lock(priv, &flags) != 0)
3159                 return -EBUSY;
3160         
3161         if (priv->has_mwo) {
3162                 err = hermes_read_wordrec(hw, USER_BAP,
3163                                           HERMES_RID_CNFMWOROBUST_AGERE,
3164                                           &val);
3165                 if (err)
3166                         val = 0;
3167
3168                 frq->value = val ? 2347 : 0;
3169                 frq->disabled = ! val;
3170                 frq->fixed = 0;
3171         } else {
3172                 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
3173                                           &val);
3174                 if (err)
3175                         val = 0;
3176
3177                 frq->value = val;
3178                 frq->disabled = (val >= 2346);
3179                 frq->fixed = 1;
3180         }
3181
3182         orinoco_unlock(priv, &flags);
3183         
3184         return err;
3185 }
3186
3187 static int orinoco_ioctl_setrate(struct net_device *dev, struct iw_param *rrq)
3188 {
3189         struct orinoco_private *priv = netdev_priv(dev);
3190         int err = 0;
3191         int ratemode = -1;
3192         int bitrate; /* 100s of kilobits */
3193         int i;
3194         unsigned long flags;
3195         
3196         /* As the user space doesn't know our highest rate, it uses -1
3197          * to ask us to set the highest rate.  Test it using "iwconfig
3198          * ethX rate auto" - Jean II */
3199         if (rrq->value == -1)
3200                 bitrate = 110;
3201         else {
3202                 if (rrq->value % 100000)
3203                         return -EINVAL;
3204                 bitrate = rrq->value / 100000;
3205         }
3206
3207         if ( (bitrate != 10) && (bitrate != 20) &&
3208              (bitrate != 55) && (bitrate != 110) )
3209                 return -EINVAL;
3210
3211         for (i = 0; i < BITRATE_TABLE_SIZE; i++)
3212                 if ( (bitrate_table[i].bitrate == bitrate) &&
3213                      (bitrate_table[i].automatic == ! rrq->fixed) ) {
3214                         ratemode = i;
3215                         break;
3216                 }
3217         
3218         if (ratemode == -1)
3219                 return -EINVAL;
3220
3221         if (orinoco_lock(priv, &flags) != 0)
3222                 return -EBUSY;
3223         priv->bitratemode = ratemode;
3224         orinoco_unlock(priv, &flags);
3225
3226         return err;
3227 }
3228
3229 static int orinoco_ioctl_getrate(struct net_device *dev, struct iw_param *rrq)
3230 {
3231         struct orinoco_private *priv = netdev_priv(dev);
3232         hermes_t *hw = &priv->hw;
3233         int err = 0;
3234         int ratemode;
3235         int i;
3236         u16 val;
3237         unsigned long flags;
3238
3239         if (orinoco_lock(priv, &flags) != 0)
3240                 return -EBUSY;
3241
3242         ratemode = priv->bitratemode;
3243
3244         BUG_ON((ratemode < 0) || (ratemode >= BITRATE_TABLE_SIZE));
3245
3246         rrq->value = bitrate_table[ratemode].bitrate * 100000;
3247         rrq->fixed = ! bitrate_table[ratemode].automatic;
3248         rrq->disabled = 0;
3249
3250         /* If the interface is running we try to find more about the
3251            current mode */
3252         if (netif_running(dev)) {
3253                 err = hermes_read_wordrec(hw, USER_BAP,
3254                                           HERMES_RID_CURRENTTXRATE, &val);
3255                 if (err)
3256                         goto out;
3257                 
3258                 switch (priv->firmware_type) {
3259                 case FIRMWARE_TYPE_AGERE: /* Lucent style rate */
3260                         /* Note : in Lucent firmware, the return value of
3261                          * HERMES_RID_CURRENTTXRATE is the bitrate in Mb/s,
3262                          * and therefore is totally different from the
3263                          * encoding of HERMES_RID_CNFTXRATECONTROL.
3264                          * Don't forget that 6Mb/s is really 5.5Mb/s */
3265                         if (val == 6)
3266                                 rrq->value = 5500000;
3267                         else
3268                                 rrq->value = val * 1000000;
3269                         break;
3270                 case FIRMWARE_TYPE_INTERSIL: /* Intersil style rate */
3271                 case FIRMWARE_TYPE_SYMBOL: /* Symbol style rate */
3272                         for (i = 0; i < BITRATE_TABLE_SIZE; i++)
3273                                 if (bitrate_table[i].intersil_txratectrl == val) {
3274                                         ratemode = i;
3275                                         break;
3276                                 }
3277                         if (i >= BITRATE_TABLE_SIZE)
3278                                 printk(KERN_INFO "%s: Unable to determine current bitrate (0x%04hx)\n",
3279                                        dev->name, val);
3280
3281                         rrq->value = bitrate_table[ratemode].bitrate * 100000;
3282                         break;
3283                 default:
3284                         BUG();
3285                 }
3286         }
3287
3288  out:
3289         orinoco_unlock(priv, &flags);
3290
3291         return err;
3292 }
3293
3294 static int orinoco_ioctl_setpower(struct net_device *dev, struct iw_param *prq)
3295 {
3296         struct orinoco_private *priv = netdev_priv(dev);
3297         int err = 0;
3298         unsigned long flags;
3299
3300         if (orinoco_lock(priv, &flags) != 0)
3301                 return -EBUSY;
3302
3303         if (prq->disabled) {
3304                 priv->pm_on = 0;
3305         } else {
3306                 switch (prq->flags & IW_POWER_MODE) {
3307                 case IW_POWER_UNICAST_R:
3308                         priv->pm_mcast = 0;
3309                         priv->pm_on = 1;
3310                         break;
3311                 case IW_POWER_ALL_R:
3312                         priv->pm_mcast = 1;
3313                         priv->pm_on = 1;
3314                         break;
3315                 case IW_POWER_ON:
3316                         /* No flags : but we may have a value - Jean II */
3317                         break;
3318                 default:
3319                         err = -EINVAL;
3320                 }
3321                 if (err)
3322                         goto out;
3323                 
3324                 if (prq->flags & IW_POWER_TIMEOUT) {
3325                         priv->pm_on = 1;
3326                         priv->pm_timeout = prq->value / 1000;
3327                 }
3328                 if (prq->flags & IW_POWER_PERIOD) {
3329                         priv->pm_on = 1;
3330                         priv->pm_period = prq->value / 1000;
3331                 }
3332                 /* It's valid to not have a value if we are just toggling
3333                  * the flags... Jean II */
3334                 if(!priv->pm_on) {
3335                         err = -EINVAL;
3336                         goto out;
3337                 }                       
3338         }
3339
3340  out:
3341         orinoco_unlock(priv, &flags);
3342
3343         return err;
3344 }
3345
3346 static int orinoco_ioctl_getpower(struct net_device *dev, struct iw_param *prq)
3347 {
3348         struct orinoco_private *priv = netdev_priv(dev);
3349         hermes_t *hw = &priv->hw;
3350         int err = 0;
3351         u16 enable, period, timeout, mcast;
3352         unsigned long flags;
3353
3354         if (orinoco_lock(priv, &flags) != 0)
3355                 return -EBUSY;
3356         
3357         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFPMENABLED, &enable);
3358         if (err)
3359                 goto out;
3360
3361         err = hermes_read_wordrec(hw, USER_BAP,
3362                                   HERMES_RID_CNFMAXSLEEPDURATION, &period);
3363         if (err)
3364                 goto out;
3365
3366         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFPMHOLDOVERDURATION, &timeout);
3367         if (err)
3368                 goto out;
3369
3370         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFMULTICASTRECEIVE, &mcast);
3371         if (err)
3372                 goto out;
3373
3374         prq->disabled = !enable;
3375         /* Note : by default, display the period */
3376         if ((prq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
3377                 prq->flags = IW_POWER_TIMEOUT;
3378                 prq->value = timeout * 1000;
3379         } else {
3380                 prq->flags = IW_POWER_PERIOD;
3381                 prq->value = period * 1000;
3382         }
3383         if (mcast)
3384                 prq->flags |= IW_POWER_ALL_R;
3385         else
3386                 prq->flags |= IW_POWER_UNICAST_R;
3387
3388  out:
3389         orinoco_unlock(priv, &flags);
3390
3391         return err;
3392 }
3393
3394 static int orinoco_ioctl_getretry(struct net_device *dev, struct iw_param *rrq)
3395 {
3396         struct orinoco_private *priv = netdev_priv(dev);
3397         hermes_t *hw = &priv->hw;
3398         int err = 0;
3399         u16 short_limit, long_limit, lifetime;
3400         unsigned long flags;
3401
3402         if (orinoco_lock(priv, &flags) != 0)
3403                 return -EBUSY;
3404         
3405         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_SHORTRETRYLIMIT,
3406                                   &short_limit);
3407         if (err)
3408                 goto out;
3409
3410         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_LONGRETRYLIMIT,
3411                                   &long_limit);
3412         if (err)
3413                 goto out;
3414
3415         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_MAXTRANSMITLIFETIME,
3416                                   &lifetime);
3417         if (err)
3418                 goto out;
3419
3420         rrq->disabled = 0;              /* Can't be disabled */
3421
3422         /* Note : by default, display the retry number */
3423         if ((rrq->flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME) {
3424                 rrq->flags = IW_RETRY_LIFETIME;
3425                 rrq->value = lifetime * 1000;   /* ??? */
3426         } else {
3427                 /* By default, display the min number */
3428                 if ((rrq->flags & IW_RETRY_MAX)) {
3429                         rrq->flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
3430                         rrq->value = long_limit;
3431                 } else {
3432                         rrq->flags = IW_RETRY_LIMIT;
3433                         rrq->value = short_limit;
3434                         if(short_limit != long_limit)
3435                                 rrq->flags |= IW_RETRY_MIN;
3436                 }
3437         }
3438
3439  out:
3440         orinoco_unlock(priv, &flags);
3441
3442         return err;
3443 }
3444
3445 static int orinoco_ioctl_setibssport(struct net_device *dev, struct iwreq *wrq)
3446 {
3447         struct orinoco_private *priv = netdev_priv(dev);
3448         int val = *( (int *) wrq->u.name );
3449         unsigned long flags;
3450
3451         if (orinoco_lock(priv, &flags) != 0)
3452                 return -EBUSY;
3453
3454         priv->ibss_port = val ;
3455
3456         /* Actually update the mode we are using */
3457         set_port_type(priv);
3458
3459         orinoco_unlock(priv, &flags);
3460         return 0;
3461 }
3462
3463 static int orinoco_ioctl_getibssport(struct net_device *dev, struct iwreq *wrq)
3464 {
3465         struct orinoco_private *priv = netdev_priv(dev);
3466         int *val = (int *)wrq->u.name;
3467         unsigned long flags;
3468
3469         if (orinoco_lock(priv, &flags) != 0)
3470                 return -EBUSY;
3471
3472         *val = priv->ibss_port;
3473         orinoco_unlock(priv, &flags);
3474
3475         return 0;
3476 }
3477
3478 static int orinoco_ioctl_setport3(struct net_device *dev, struct iwreq *wrq)
3479 {
3480         struct orinoco_private *priv = netdev_priv(dev);
3481         int val = *( (int *) wrq->u.name );
3482         int err = 0;
3483         unsigned long flags;
3484
3485         if (orinoco_lock(priv, &flags) != 0)
3486                 return -EBUSY;
3487
3488         switch (val) {
3489         case 0: /* Try to do IEEE ad-hoc mode */
3490                 if (! priv->has_ibss) {
3491                         err = -EINVAL;
3492                         break;
3493                 }
3494                 priv->prefer_port3 = 0;
3495                         
3496                 break;
3497
3498         case 1: /* Try to do Lucent proprietary ad-hoc mode */
3499                 if (! priv->has_port3) {
3500                         err = -EINVAL;
3501                         break;
3502                 }
3503                 priv->prefer_port3 = 1;
3504                 break;
3505
3506         default:
3507                 err = -EINVAL;
3508         }
3509
3510         if (! err)
3511                 /* Actually update the mode we are using */
3512                 set_port_type(priv);
3513
3514         orinoco_unlock(priv, &flags);
3515
3516         return err;
3517 }
3518
3519 static int orinoco_ioctl_getport3(struct net_device *dev, struct iwreq *wrq)
3520 {
3521         struct orinoco_private *priv = netdev_priv(dev);
3522         int *val = (int *)wrq->u.name;
3523         unsigned long flags;
3524
3525         if (orinoco_lock(priv, &flags) != 0)
3526                 return -EBUSY;
3527
3528         *val = priv->prefer_port3;
3529         orinoco_unlock(priv, &flags);
3530         return 0;
3531 }
3532
3533 /* Spy is used for link quality/strength measurements in Ad-Hoc mode
3534  * Jean II */
3535 static int orinoco_ioctl_setspy(struct net_device *dev, struct iw_point *srq)
3536 {
3537         struct orinoco_private *priv = netdev_priv(dev);
3538         struct sockaddr address[IW_MAX_SPY];
3539         int number = srq->length;
3540         int i;
3541         int err = 0;
3542         unsigned long flags;
3543
3544         /* Check the number of addresses */
3545         if (number > IW_MAX_SPY)
3546                 return -E2BIG;
3547
3548         /* Get the data in the driver */
3549         if (srq->pointer) {
3550                 if (copy_from_user(address, srq->pointer,
3551                                    sizeof(struct sockaddr) * number))
3552                         return -EFAULT;
3553         }
3554
3555         /* Make sure nobody mess with the structure while we do */
3556         if (orinoco_lock(priv, &flags) != 0)
3557                 return -EBUSY;
3558
3559         /* orinoco_lock() doesn't disable interrupts, so make sure the
3560          * interrupt rx path don't get confused while we copy */
3561         priv->spy_number = 0;
3562
3563         if (number > 0) {
3564                 /* Extract the addresses */
3565                 for (i = 0; i < number; i++)
3566                         memcpy(priv->spy_address[i], address[i].sa_data,
3567                                ETH_ALEN);
3568                 /* Reset stats */
3569                 memset(priv->spy_stat, 0,
3570                        sizeof(struct iw_quality) * IW_MAX_SPY);
3571                 /* Set number of addresses */
3572                 priv->spy_number = number;
3573         }
3574
3575         /* Now, let the others play */
3576         orinoco_unlock(priv, &flags);
3577
3578         return err;
3579 }
3580
3581 static int orinoco_ioctl_getspy(struct net_device *dev, struct iw_point *srq)
3582 {
3583         struct orinoco_private *priv = netdev_priv(dev);
3584         struct sockaddr address[IW_MAX_SPY];
3585         struct iw_quality spy_stat[IW_MAX_SPY];
3586         int number;
3587         int i;
3588         unsigned long flags;
3589
3590         if (orinoco_lock(priv, &flags) != 0)
3591                 return -EBUSY;
3592
3593         number = priv->spy_number;
3594         if ((number > 0) && (srq->pointer)) {
3595                 /* Create address struct */
3596                 for (i = 0; i < number; i++) {
3597                         memcpy(address[i].sa_data, priv->spy_address[i],
3598                                ETH_ALEN);
3599                         address[i].sa_family = AF_UNIX;
3600                 }
3601                 /* Copy stats */
3602                 /* In theory, we should disable irqs while copying the stats
3603                  * because the rx path might update it in the middle...
3604                  * Bah, who care ? - Jean II */
3605                 memcpy(&spy_stat, priv->spy_stat,
3606                        sizeof(struct iw_quality) * IW_MAX_SPY);
3607                 for (i=0; i < number; i++)
3608                         priv->spy_stat[i].updated = 0;
3609         }
3610
3611         orinoco_unlock(priv, &flags);
3612
3613         /* Push stuff to user space */
3614         srq->length = number;
3615         if(copy_to_user(srq->pointer, address,
3616                          sizeof(struct sockaddr) * number))
3617                 return -EFAULT;
3618         if(copy_to_user(srq->pointer + (sizeof(struct sockaddr)*number),
3619                         &spy_stat, sizeof(struct iw_quality) * number))
3620                 return -EFAULT;
3621
3622         return 0;
3623 }
3624
3625 static int
3626 orinoco_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3627 {
3628         struct orinoco_private *priv = netdev_priv(dev);
3629         struct iwreq *wrq = (struct iwreq *)rq;
3630         int err = 0;
3631         int tmp;
3632         int changed = 0;
3633         unsigned long flags;
3634
3635         TRACE_ENTER(dev->name);
3636
3637         /* In theory, we could allow most of the the SET stuff to be
3638          * done. In practice, the lapse of time at startup when the
3639          * card is not ready is very short, so why bother...  Note
3640          * that netif_device_present is different from up/down
3641          * (ifconfig), when the device is not yet up, it is usually
3642          * already ready...  Jean II */
3643         if (! netif_device_present(dev))
3644                 return -ENODEV;
3645
3646         switch (cmd) {
3647         case SIOCGIWNAME:
3648                 strcpy(wrq->u.name, "IEEE 802.11-DS");
3649                 break;
3650                 
3651         case SIOCGIWAP:
3652                 wrq->u.ap_addr.sa_family = ARPHRD_ETHER;
3653                 err = orinoco_hw_get_bssid(priv, wrq->u.ap_addr.sa_data);
3654                 break;
3655
3656         case SIOCGIWRANGE:
3657                 err = orinoco_ioctl_getiwrange(dev, &wrq->u.data);
3658                 break;
3659
3660         case SIOCSIWMODE:
3661                 if (orinoco_lock(priv, &flags) != 0)
3662                         return -EBUSY;
3663                 switch (wrq->u.mode) {
3664                 case IW_MODE_ADHOC:
3665                         if (! (priv->has_ibss || priv->has_port3) )
3666                                 err = -EINVAL;
3667                         else {
3668                                 priv->iw_mode = IW_MODE_ADHOC;
3669                                 changed = 1;
3670                         }
3671                         break;
3672
3673                 case IW_MODE_INFRA:
3674                         priv->iw_mode = IW_MODE_INFRA;
3675                         changed = 1;
3676                         break;
3677
3678                 default:
3679                         err = -EINVAL;
3680                         break;
3681                 }
3682                 set_port_type(priv);
3683                 orinoco_unlock(priv, &flags);
3684                 break;
3685
3686         case SIOCGIWMODE:
3687                 if (orinoco_lock(priv, &flags) != 0)
3688                         return -EBUSY;
3689                 wrq->u.mode = priv->iw_mode;
3690                 orinoco_unlock(priv, &flags);
3691                 break;
3692
3693         case SIOCSIWENCODE:
3694                 err = orinoco_ioctl_setiwencode(dev, &wrq->u.encoding);
3695                 if (! err)
3696                         changed = 1;
3697                 break;
3698
3699         case SIOCGIWENCODE:
3700                 if (! capable(CAP_NET_ADMIN)) {
3701                         err = -EPERM;
3702                         break;
3703                 }
3704
3705                 err = orinoco_ioctl_getiwencode(dev, &wrq->u.encoding);
3706                 break;
3707
3708         case SIOCSIWESSID:
3709                 err = orinoco_ioctl_setessid(dev, &wrq->u.essid);
3710                 if (! err)
3711                         changed = 1;
3712                 break;
3713
3714         case SIOCGIWESSID:
3715                 err = orinoco_ioctl_getessid(dev, &wrq->u.essid);
3716                 break;
3717
3718         case SIOCSIWNICKN:
3719                 err = orinoco_ioctl_setnick(dev, &wrq->u.data);
3720                 if (! err)
3721                         changed = 1;
3722                 break;
3723
3724         case SIOCGIWNICKN:
3725                 err = orinoco_ioctl_getnick(dev, &wrq->u.data);
3726                 break;
3727
3728         case SIOCGIWFREQ:
3729                 tmp = orinoco_hw_get_freq(priv);
3730                 if (tmp < 0) {
3731                         err = tmp;
3732                 } else {
3733                         wrq->u.freq.m = tmp;
3734                         wrq->u.freq.e = 1;
3735                 }
3736                 break;
3737
3738         case SIOCSIWFREQ:
3739                 err = orinoco_ioctl_setfreq(dev, &wrq->u.freq);
3740                 if (! err)
3741                         changed = 1;
3742                 break;
3743
3744         case SIOCGIWSENS:
3745                 err = orinoco_ioctl_getsens(dev, &wrq->u.sens);
3746                 break;
3747
3748         case SIOCSIWSENS:
3749                 err = orinoco_ioctl_setsens(dev, &wrq->u.sens);
3750                 if (! err)
3751                         changed = 1;
3752                 break;
3753
3754         case SIOCGIWRTS:
3755                 wrq->u.rts.value = priv->rts_thresh;
3756                 wrq->u.rts.disabled = (wrq->u.rts.value == 2347);
3757                 wrq->u.rts.fixed = 1;
3758                 break;
3759
3760         case SIOCSIWRTS:
3761                 err = orinoco_ioctl_setrts(dev, &wrq->u.rts);
3762                 if (! err)
3763                         changed = 1;
3764                 break;
3765
3766         case SIOCSIWFRAG:
3767                 err = orinoco_ioctl_setfrag(dev, &wrq->u.frag);
3768                 if (! err)
3769                         changed = 1;
3770                 break;
3771
3772         case SIOCGIWFRAG:
3773                 err = orinoco_ioctl_getfrag(dev, &wrq->u.frag);
3774                 break;
3775
3776         case SIOCSIWRATE:
3777                 err = orinoco_ioctl_setrate(dev, &wrq->u.bitrate);
3778                 if (! err)
3779                         changed = 1;
3780                 break;
3781
3782         case SIOCGIWRATE:
3783                 err = orinoco_ioctl_getrate(dev, &wrq->u.bitrate);
3784                 break;
3785
3786         case SIOCSIWPOWER:
3787                 err = orinoco_ioctl_setpower(dev, &wrq->u.power);
3788                 if (! err)
3789                         changed = 1;
3790                 break;
3791
3792         case SIOCGIWPOWER:
3793                 err = orinoco_ioctl_getpower(dev, &wrq->u.power);
3794                 break;
3795
3796         case SIOCGIWTXPOW:
3797                 /* The card only supports one tx power, so this is easy */
3798                 wrq->u.txpower.value = 15; /* dBm */
3799                 wrq->u.txpower.fixed = 1;
3800                 wrq->u.txpower.disabled = 0;
3801                 wrq->u.txpower.flags = IW_TXPOW_DBM;
3802                 break;
3803
3804         case SIOCSIWRETRY:
3805                 err = -EOPNOTSUPP;
3806                 break;
3807
3808         case SIOCGIWRETRY:
3809                 err = orinoco_ioctl_getretry(dev, &wrq->u.retry);
3810                 break;
3811
3812         case SIOCSIWSPY:
3813                 err = orinoco_ioctl_setspy(dev, &wrq->u.data);
3814                 break;
3815
3816         case SIOCGIWSPY:
3817                 err = orinoco_ioctl_getspy(dev, &wrq->u.data);
3818                 break;
3819
3820         case SIOCGIWPRIV:
3821                 if (wrq->u.data.pointer) {
3822                         struct iw_priv_args privtab[] = {
3823                                 { SIOCIWFIRSTPRIV + 0x0, 0, 0, "force_reset" },
3824                                 { SIOCIWFIRSTPRIV + 0x1, 0, 0, "card_reset" },
3825                                 { SIOCIWFIRSTPRIV + 0x2,
3826                                   IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
3827                                   0, "set_port3" },
3828                                 { SIOCIWFIRSTPRIV + 0x3, 0,
3829                                   IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
3830                                   "get_port3" },
3831                                 { SIOCIWFIRSTPRIV + 0x4,
3832                                   IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
3833                                   0, "set_preamble" },
3834                                 { SIOCIWFIRSTPRIV + 0x5, 0,
3835                                   IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
3836                                   "get_preamble" },
3837                                 { SIOCIWFIRSTPRIV + 0x6,
3838                                   IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
3839                                   0, "set_ibssport" },
3840                                 { SIOCIWFIRSTPRIV + 0x7, 0,
3841                                   IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
3842                                   "get_ibssport" },
3843                         };
3844
3845                         wrq->u.data.length = sizeof(privtab) / sizeof(privtab[0]);
3846                         if (copy_to_user(wrq->u.data.pointer, privtab, sizeof(privtab)))
3847                                 err = -EFAULT;
3848                 }
3849                 break;
3850                
3851         case SIOCIWFIRSTPRIV + 0x0: /* force_reset */
3852         case SIOCIWFIRSTPRIV + 0x1: /* card_reset */
3853                 if (! capable(CAP_NET_ADMIN)) {
3854                         err = -EPERM;
3855                         break;
3856                 }
3857                 
3858                 printk(KERN_DEBUG "%s: Force scheduling reset!\n", dev->name);
3859
3860                 schedule_work(&priv->reset_work);
3861                 break;
3862
3863         case SIOCIWFIRSTPRIV + 0x2: /* set_port3 */
3864                 if (! capable(CAP_NET_ADMIN)) {
3865                         err = -EPERM;
3866                         break;
3867                 }
3868
3869                 err = orinoco_ioctl_setport3(dev, wrq);
3870                 if (! err)
3871                         changed = 1;
3872                 break;
3873
3874         case SIOCIWFIRSTPRIV + 0x3: /* get_port3 */
3875                 err = orinoco_ioctl_getport3(dev, wrq);
3876                 break;
3877
3878         case SIOCIWFIRSTPRIV + 0x4: /* set_preamble */
3879                 if (! capable(CAP_NET_ADMIN)) {
3880                         err = -EPERM;
3881                         break;
3882                 }
3883
3884                 /* 802.11b has recently defined some short preamble.
3885                  * Basically, the Phy header has been reduced in size.
3886                  * This increase performance, especially at high rates
3887                  * (the preamble is transmitted at 1Mb/s), unfortunately
3888                  * this give compatibility troubles... - Jean II */
3889                 if(priv->has_preamble) {
3890                         int val = *( (int *) wrq->u.name );
3891
3892                         if (orinoco_lock(priv, &flags) != 0)
3893                                 return -EBUSY;
3894                         if (val)
3895                                 priv->preamble = 1;
3896                         else
3897                                 priv->preamble = 0;
3898                         orinoco_unlock(priv, &flags);
3899                         changed = 1;
3900                 } else
3901                         err = -EOPNOTSUPP;
3902                 break;
3903
3904         case SIOCIWFIRSTPRIV + 0x5: /* get_preamble */
3905                 if(priv->has_preamble) {
3906                         int *val = (int *)wrq->u.name;
3907
3908                         if (orinoco_lock(priv, &flags) != 0)
3909                                 return -EBUSY;
3910                         *val = priv->preamble;
3911                         orinoco_unlock(priv, &flags);
3912                 } else
3913                         err = -EOPNOTSUPP;
3914                 break;
3915         case SIOCIWFIRSTPRIV + 0x6: /* set_ibssport */
3916                 if (! capable(CAP_NET_ADMIN)) {
3917                         err = -EPERM;
3918                         break;
3919                 }
3920
3921                 err = orinoco_ioctl_setibssport(dev, wrq);
3922                 if (! err)
3923                         changed = 1;
3924                 break;
3925
3926         case SIOCIWFIRSTPRIV + 0x7: /* get_ibssport */
3927                 err = orinoco_ioctl_getibssport(dev, wrq);
3928                 break;
3929
3930         default:
3931                 err = -EOPNOTSUPP;
3932         }
3933         
3934         if (! err && changed && netif_running(dev)) {
3935                 err = orinoco_reconfigure(dev);
3936         }               
3937
3938         TRACE_EXIT(dev->name);
3939
3940         return err;
3941 }
3942
3943
3944 /********************************************************************/
3945 /* Debugging                                                        */
3946 /********************************************************************/
3947
3948 #if 0
3949 static void show_rx_frame(struct orinoco_rxframe_hdr *frame)
3950 {
3951         printk(KERN_DEBUG "RX descriptor:\n");
3952         printk(KERN_DEBUG "  status      = 0x%04x\n", frame->desc.status);
3953         printk(KERN_DEBUG "  time        = 0x%08x\n", frame->desc.time);
3954         printk(KERN_DEBUG "  silence     = 0x%02x\n", frame->desc.silence);
3955         printk(KERN_DEBUG "  signal      = 0x%02x\n", frame->desc.signal);
3956         printk(KERN_DEBUG "  rate        = 0x%02x\n", frame->desc.rate);
3957         printk(KERN_DEBUG "  rxflow      = 0x%02x\n", frame->desc.rxflow);
3958         printk(KERN_DEBUG "  reserved    = 0x%08x\n", frame->desc.reserved);
3959
3960         printk(KERN_DEBUG "IEEE 802.11 header:\n");
3961         printk(KERN_DEBUG "  frame_ctl   = 0x%04x\n",
3962                frame->p80211.frame_ctl);
3963         printk(KERN_DEBUG "  duration_id = 0x%04x\n",
3964                frame->p80211.duration_id);
3965         printk(KERN_DEBUG "  addr1       = %02x:%02x:%02x:%02x:%02x:%02x\n",
3966                frame->p80211.addr1[0], frame->p80211.addr1[1],
3967                frame->p80211.addr1[2], frame->p80211.addr1[3],
3968                frame->p80211.addr1[4], frame->p80211.addr1[5]);
3969         printk(KERN_DEBUG "  addr2       = %02x:%02x:%02x:%02x:%02x:%02x\n",
3970                frame->p80211.addr2[0], frame->p80211.addr2[1],
3971                frame->p80211.addr2[2], frame->p80211.addr2[3],
3972                frame->p80211.addr2[4], frame->p80211.addr2[5]);
3973         printk(KERN_DEBUG "  addr3       = %02x:%02x:%02x:%02x:%02x:%02x\n",
3974                frame->p80211.addr3[0], frame->p80211.addr3[1],
3975                frame->p80211.addr3[2], frame->p80211.addr3[3],
3976                frame->p80211.addr3[4], frame->p80211.addr3[5]);
3977         printk(KERN_DEBUG "  seq_ctl     = 0x%04x\n",
3978                frame->p80211.seq_ctl);
3979         printk(KERN_DEBUG "  addr4       = %02x:%02x:%02x:%02x:%02x:%02x\n",
3980                frame->p80211.addr4[0], frame->p80211.addr4[1],
3981                frame->p80211.addr4[2], frame->p80211.addr4[3],
3982                frame->p80211.addr4[4], frame->p80211.addr4[5]);
3983         printk(KERN_DEBUG "  data_len    = 0x%04x\n",
3984                frame->p80211.data_len);
3985
3986         printk(KERN_DEBUG "IEEE 802.3 header:\n");
3987         printk(KERN_DEBUG "  dest        = %02x:%02x:%02x:%02x:%02x:%02x\n",
3988                frame->p8023.h_dest[0], frame->p8023.h_dest[1],
3989                frame->p8023.h_dest[2], frame->p8023.h_dest[3],
3990                frame->p8023.h_dest[4], frame->p8023.h_dest[5]);
3991         printk(KERN_DEBUG "  src         = %02x:%02x:%02x:%02x:%02x:%02x\n",
3992                frame->p8023.h_source[0], frame->p8023.h_source[1],
3993                frame->p8023.h_source[2], frame->p8023.h_source[3],
3994                frame->p8023.h_source[4], frame->p8023.h_source[5]);
3995         printk(KERN_DEBUG "  len         = 0x%04x\n", frame->p8023.h_proto);
3996
3997         printk(KERN_DEBUG "IEEE 802.2 LLC/SNAP header:\n");
3998         printk(KERN_DEBUG "  DSAP        = 0x%02x\n", frame->p8022.dsap);
3999         printk(KERN_DEBUG "  SSAP        = 0x%02x\n", frame->p8022.ssap);
4000         printk(KERN_DEBUG "  ctrl        = 0x%02x\n", frame->p8022.ctrl);
4001         printk(KERN_DEBUG "  OUI         = %02x:%02x:%02x\n",
4002                frame->p8022.oui[0], frame->p8022.oui[1], frame->p8022.oui[2]);
4003         printk(KERN_DEBUG "  ethertype  = 0x%04x\n", frame->ethertype);
4004 }
4005 #endif /* 0 */
4006
4007 /********************************************************************/
4008 /* Module initialization                                            */
4009 /********************************************************************/
4010
4011 EXPORT_SYMBOL(alloc_orinocodev);
4012 EXPORT_SYMBOL(free_orinocodev);
4013
4014 EXPORT_SYMBOL(__orinoco_up);
4015 EXPORT_SYMBOL(__orinoco_down);
4016 EXPORT_SYMBOL(orinoco_stop);
4017 EXPORT_SYMBOL(orinoco_reinit_firmware);
4018
4019 EXPORT_SYMBOL(orinoco_interrupt);
4020
4021 /* Can't be declared "const" or the whole __initdata section will
4022  * become const */
4023 static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
4024         " (David Gibson <hermes@gibson.dropbear.id.au>, "
4025         "Pavel Roskin <proski@gnu.org>, et al)";
4026
4027 static int __init init_orinoco(void)
4028 {
4029         printk(KERN_DEBUG "%s\n", version);
4030         return 0;
4031 }
4032
4033 static void __exit exit_orinoco(void)
4034 {
4035 }
4036
4037 module_init(init_orinoco);
4038 module_exit(exit_orinoco);