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