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