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