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