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