]> err.no Git - linux-2.6/blob - drivers/net/wireless/orinoco.c
[PATCH] orinoco: Fix memory leak and unneeded unlock in orinoco_join_ap()
[linux-2.6] / 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 fail_lock;
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         orinoco_unlock(priv, &flags);
1095
1096  fail_lock:
1097         kfree(buf);
1098 }
1099
1100 /* Send new BSSID to userspace */
1101 static void orinoco_send_wevents(struct net_device *dev)
1102 {
1103         struct orinoco_private *priv = netdev_priv(dev);
1104         struct hermes *hw = &priv->hw;
1105         union iwreq_data wrqu;
1106         int err;
1107         unsigned long flags;
1108
1109         if (orinoco_lock(priv, &flags) != 0)
1110                 return;
1111
1112         err = hermes_read_ltv(hw, IRQ_BAP, HERMES_RID_CURRENTBSSID,
1113                               ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
1114         if (err != 0)
1115                 return;
1116
1117         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1118
1119         /* Send event to user space */
1120         wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
1121         orinoco_unlock(priv, &flags);
1122 }
1123
1124 static void __orinoco_ev_info(struct net_device *dev, hermes_t *hw)
1125 {
1126         struct orinoco_private *priv = netdev_priv(dev);
1127         u16 infofid;
1128         struct {
1129                 u16 len;
1130                 u16 type;
1131         } __attribute__ ((packed)) info;
1132         int len, type;
1133         int err;
1134
1135         /* This is an answer to an INQUIRE command that we did earlier,
1136          * or an information "event" generated by the card
1137          * The controller return to us a pseudo frame containing
1138          * the information in question - Jean II */
1139         infofid = hermes_read_regn(hw, INFOFID);
1140
1141         /* Read the info frame header - don't try too hard */
1142         err = hermes_bap_pread(hw, IRQ_BAP, &info, sizeof(info),
1143                                infofid, 0);
1144         if (err) {
1145                 printk(KERN_ERR "%s: error %d reading info frame. "
1146                        "Frame dropped.\n", dev->name, err);
1147                 return;
1148         }
1149         
1150         len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
1151         type = le16_to_cpu(info.type);
1152
1153         switch (type) {
1154         case HERMES_INQ_TALLIES: {
1155                 struct hermes_tallies_frame tallies;
1156                 struct iw_statistics *wstats = &priv->wstats;
1157                 
1158                 if (len > sizeof(tallies)) {
1159                         printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
1160                                dev->name, len);
1161                         len = sizeof(tallies);
1162                 }
1163                 
1164                 err = hermes_bap_pread(hw, IRQ_BAP, &tallies, len,
1165                                        infofid, sizeof(info));
1166                 if (err)
1167                         break;
1168                 
1169                 /* Increment our various counters */
1170                 /* wstats->discard.nwid - no wrong BSSID stuff */
1171                 wstats->discard.code +=
1172                         le16_to_cpu(tallies.RxWEPUndecryptable);
1173                 if (len == sizeof(tallies))  
1174                         wstats->discard.code +=
1175                                 le16_to_cpu(tallies.RxDiscards_WEPICVError) +
1176                                 le16_to_cpu(tallies.RxDiscards_WEPExcluded);
1177                 wstats->discard.misc +=
1178                         le16_to_cpu(tallies.TxDiscardsWrongSA);
1179                 wstats->discard.fragment +=
1180                         le16_to_cpu(tallies.RxMsgInBadMsgFragments);
1181                 wstats->discard.retries +=
1182                         le16_to_cpu(tallies.TxRetryLimitExceeded);
1183                 /* wstats->miss.beacon - no match */
1184         }
1185         break;
1186         case HERMES_INQ_LINKSTATUS: {
1187                 struct hermes_linkstatus linkstatus;
1188                 u16 newstatus;
1189                 int connected;
1190
1191                 if (priv->iw_mode == IW_MODE_MONITOR)
1192                         break;
1193
1194                 if (len != sizeof(linkstatus)) {
1195                         printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
1196                                dev->name, len);
1197                         break;
1198                 }
1199
1200                 err = hermes_bap_pread(hw, IRQ_BAP, &linkstatus, len,
1201                                        infofid, sizeof(info));
1202                 if (err)
1203                         break;
1204                 newstatus = le16_to_cpu(linkstatus.linkstatus);
1205
1206                 /* Symbol firmware uses "out of range" to signal that
1207                  * the hostscan frame can be requested.  */
1208                 if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
1209                     priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
1210                     priv->has_hostscan && priv->scan_inprogress) {
1211                         hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
1212                         break;
1213                 }
1214
1215                 connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
1216                         || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
1217                         || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
1218
1219                 if (connected)
1220                         netif_carrier_on(dev);
1221                 else if (!ignore_disconnect)
1222                         netif_carrier_off(dev);
1223
1224                 if (newstatus != priv->last_linkstatus) {
1225                         priv->last_linkstatus = newstatus;
1226                         print_linkstatus(dev, newstatus);
1227                         /* The info frame contains only one word which is the
1228                          * status (see hermes.h). The status is pretty boring
1229                          * in itself, that's why we export the new BSSID...
1230                          * Jean II */
1231                         schedule_work(&priv->wevent_work);
1232                 }
1233         }
1234         break;
1235         case HERMES_INQ_SCAN:
1236                 if (!priv->scan_inprogress && priv->bssid_fixed &&
1237                     priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
1238                         schedule_work(&priv->join_work);
1239                         break;
1240                 }
1241                 /* fall through */
1242         case HERMES_INQ_HOSTSCAN:
1243         case HERMES_INQ_HOSTSCAN_SYMBOL: {
1244                 /* Result of a scanning. Contains information about
1245                  * cells in the vicinity - Jean II */
1246                 union iwreq_data        wrqu;
1247                 unsigned char *buf;
1248
1249                 /* Sanity check */
1250                 if (len > 4096) {
1251                         printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
1252                                dev->name, len);
1253                         break;
1254                 }
1255
1256                 /* We are a strict producer. If the previous scan results
1257                  * have not been consumed, we just have to drop this
1258                  * frame. We can't remove the previous results ourselves,
1259                  * that would be *very* racy... Jean II */
1260                 if (priv->scan_result != NULL) {
1261                         printk(KERN_WARNING "%s: Previous scan results not consumed, dropping info frame.\n", dev->name);
1262                         break;
1263                 }
1264
1265                 /* Allocate buffer for results */
1266                 buf = kmalloc(len, GFP_ATOMIC);
1267                 if (buf == NULL)
1268                         /* No memory, so can't printk()... */
1269                         break;
1270
1271                 /* Read scan data */
1272                 err = hermes_bap_pread(hw, IRQ_BAP, (void *) buf, len,
1273                                        infofid, sizeof(info));
1274                 if (err) {
1275                         kfree(buf);
1276                         break;
1277                 }
1278
1279 #ifdef ORINOCO_DEBUG
1280                 {
1281                         int     i;
1282                         printk(KERN_DEBUG "Scan result [%02X", buf[0]);
1283                         for(i = 1; i < (len * 2); i++)
1284                                 printk(":%02X", buf[i]);
1285                         printk("]\n");
1286                 }
1287 #endif  /* ORINOCO_DEBUG */
1288
1289                 /* Allow the clients to access the results */
1290                 priv->scan_len = len;
1291                 priv->scan_result = buf;
1292
1293                 /* Send an empty event to user space.
1294                  * We don't send the received data on the event because
1295                  * it would require us to do complex transcoding, and
1296                  * we want to minimise the work done in the irq handler
1297                  * Use a request to extract the data - Jean II */
1298                 wrqu.data.length = 0;
1299                 wrqu.data.flags = 0;
1300                 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
1301         }
1302         break;
1303         case HERMES_INQ_SEC_STAT_AGERE:
1304                 /* Security status (Agere specific) */
1305                 /* Ignore this frame for now */
1306                 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
1307                         break;
1308                 /* fall through */
1309         default:
1310                 printk(KERN_DEBUG "%s: Unknown information frame received: "
1311                        "type 0x%04x, length %d\n", dev->name, type, len);
1312                 /* We don't actually do anything about it */
1313                 break;
1314         }
1315 }
1316
1317 static void __orinoco_ev_infdrop(struct net_device *dev, hermes_t *hw)
1318 {
1319         if (net_ratelimit())
1320                 printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
1321 }
1322
1323 /********************************************************************/
1324 /* Internal hardware control routines                               */
1325 /********************************************************************/
1326
1327 int __orinoco_up(struct net_device *dev)
1328 {
1329         struct orinoco_private *priv = netdev_priv(dev);
1330         struct hermes *hw = &priv->hw;
1331         int err;
1332
1333         netif_carrier_off(dev); /* just to make sure */
1334
1335         err = __orinoco_program_rids(dev);
1336         if (err) {
1337                 printk(KERN_ERR "%s: Error %d configuring card\n",
1338                        dev->name, err);
1339                 return err;
1340         }
1341
1342         /* Fire things up again */
1343         hermes_set_irqmask(hw, ORINOCO_INTEN);
1344         err = hermes_enable_port(hw, 0);
1345         if (err) {
1346                 printk(KERN_ERR "%s: Error %d enabling MAC port\n",
1347                        dev->name, err);
1348                 return err;
1349         }
1350
1351         netif_start_queue(dev);
1352
1353         return 0;
1354 }
1355
1356 int __orinoco_down(struct net_device *dev)
1357 {
1358         struct orinoco_private *priv = netdev_priv(dev);
1359         struct hermes *hw = &priv->hw;
1360         int err;
1361
1362         netif_stop_queue(dev);
1363
1364         if (! priv->hw_unavailable) {
1365                 if (! priv->broken_disableport) {
1366                         err = hermes_disable_port(hw, 0);
1367                         if (err) {
1368                                 /* Some firmwares (e.g. Intersil 1.3.x) seem
1369                                  * to have problems disabling the port, oh
1370                                  * well, too bad. */
1371                                 printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
1372                                        dev->name, err);
1373                                 priv->broken_disableport = 1;
1374                         }
1375                 }
1376                 hermes_set_irqmask(hw, 0);
1377                 hermes_write_regn(hw, EVACK, 0xffff);
1378         }
1379         
1380         /* firmware will have to reassociate */
1381         netif_carrier_off(dev);
1382         priv->last_linkstatus = 0xffff;
1383
1384         return 0;
1385 }
1386
1387 int orinoco_reinit_firmware(struct net_device *dev)
1388 {
1389         struct orinoco_private *priv = netdev_priv(dev);
1390         struct hermes *hw = &priv->hw;
1391         int err;
1392
1393         err = hermes_init(hw);
1394         if (err)
1395                 return err;
1396
1397         err = hermes_allocate(hw, priv->nicbuf_size, &priv->txfid);
1398         if (err == -EIO && priv->nicbuf_size > TX_NICBUF_SIZE_BUG) {
1399                 /* Try workaround for old Symbol firmware bug */
1400                 printk(KERN_WARNING "%s: firmware ALLOC bug detected "
1401                        "(old Symbol firmware?). Trying to work around... ",
1402                        dev->name);
1403                 
1404                 priv->nicbuf_size = TX_NICBUF_SIZE_BUG;
1405                 err = hermes_allocate(hw, priv->nicbuf_size, &priv->txfid);
1406                 if (err)
1407                         printk("failed!\n");
1408                 else
1409                         printk("ok.\n");
1410         }
1411
1412         return err;
1413 }
1414
1415 static int __orinoco_hw_set_bitrate(struct orinoco_private *priv)
1416 {
1417         hermes_t *hw = &priv->hw;
1418         int err = 0;
1419
1420         if (priv->bitratemode >= BITRATE_TABLE_SIZE) {
1421                 printk(KERN_ERR "%s: BUG: Invalid bitrate mode %d\n",
1422                        priv->ndev->name, priv->bitratemode);
1423                 return -EINVAL;
1424         }
1425
1426         switch (priv->firmware_type) {
1427         case FIRMWARE_TYPE_AGERE:
1428                 err = hermes_write_wordrec(hw, USER_BAP,
1429                                            HERMES_RID_CNFTXRATECONTROL,
1430                                            bitrate_table[priv->bitratemode].agere_txratectrl);
1431                 break;
1432         case FIRMWARE_TYPE_INTERSIL:
1433         case FIRMWARE_TYPE_SYMBOL:
1434                 err = hermes_write_wordrec(hw, USER_BAP,
1435                                            HERMES_RID_CNFTXRATECONTROL,
1436                                            bitrate_table[priv->bitratemode].intersil_txratectrl);
1437                 break;
1438         default:
1439                 BUG();
1440         }
1441
1442         return err;
1443 }
1444
1445 /* Set fixed AP address */
1446 static int __orinoco_hw_set_wap(struct orinoco_private *priv)
1447 {
1448         int roaming_flag;
1449         int err = 0;
1450         hermes_t *hw = &priv->hw;
1451
1452         switch (priv->firmware_type) {
1453         case FIRMWARE_TYPE_AGERE:
1454                 /* not supported */
1455                 break;
1456         case FIRMWARE_TYPE_INTERSIL:
1457                 if (priv->bssid_fixed)
1458                         roaming_flag = 2;
1459                 else
1460                         roaming_flag = 1;
1461
1462                 err = hermes_write_wordrec(hw, USER_BAP,
1463                                            HERMES_RID_CNFROAMINGMODE,
1464                                            roaming_flag);
1465                 break;
1466         case FIRMWARE_TYPE_SYMBOL:
1467                 err = HERMES_WRITE_RECORD(hw, USER_BAP,
1468                                           HERMES_RID_CNFMANDATORYBSSID_SYMBOL,
1469                                           &priv->desired_bssid);
1470                 break;
1471         }
1472         return err;
1473 }
1474
1475 /* Change the WEP keys and/or the current keys.  Can be called
1476  * either from __orinoco_hw_setup_wep() or directly from
1477  * orinoco_ioctl_setiwencode().  In the later case the association
1478  * with the AP is not broken (if the firmware can handle it),
1479  * which is needed for 802.1x implementations. */
1480 static int __orinoco_hw_setup_wepkeys(struct orinoco_private *priv)
1481 {
1482         hermes_t *hw = &priv->hw;
1483         int err = 0;
1484
1485         switch (priv->firmware_type) {
1486         case FIRMWARE_TYPE_AGERE:
1487                 err = HERMES_WRITE_RECORD(hw, USER_BAP,
1488                                           HERMES_RID_CNFWEPKEYS_AGERE,
1489                                           &priv->keys);
1490                 if (err)
1491                         return err;
1492                 err = hermes_write_wordrec(hw, USER_BAP,
1493                                            HERMES_RID_CNFTXKEY_AGERE,
1494                                            priv->tx_key);
1495                 if (err)
1496                         return err;
1497                 break;
1498         case FIRMWARE_TYPE_INTERSIL:
1499         case FIRMWARE_TYPE_SYMBOL:
1500                 {
1501                         int keylen;
1502                         int i;
1503
1504                         /* Force uniform key length to work around firmware bugs */
1505                         keylen = le16_to_cpu(priv->keys[priv->tx_key].len);
1506                         
1507                         if (keylen > LARGE_KEY_SIZE) {
1508                                 printk(KERN_ERR "%s: BUG: Key %d has oversize length %d.\n",
1509                                        priv->ndev->name, priv->tx_key, keylen);
1510                                 return -E2BIG;
1511                         }
1512
1513                         /* Write all 4 keys */
1514                         for(i = 0; i < ORINOCO_MAX_KEYS; i++) {
1515                                 err = hermes_write_ltv(hw, USER_BAP,
1516                                                        HERMES_RID_CNFDEFAULTKEY0 + i,
1517                                                        HERMES_BYTES_TO_RECLEN(keylen),
1518                                                        priv->keys[i].data);
1519                                 if (err)
1520                                         return err;
1521                         }
1522
1523                         /* Write the index of the key used in transmission */
1524                         err = hermes_write_wordrec(hw, USER_BAP,
1525                                                    HERMES_RID_CNFWEPDEFAULTKEYID,
1526                                                    priv->tx_key);
1527                         if (err)
1528                                 return err;
1529                 }
1530                 break;
1531         }
1532
1533         return 0;
1534 }
1535
1536 static int __orinoco_hw_setup_wep(struct orinoco_private *priv)
1537 {
1538         hermes_t *hw = &priv->hw;
1539         int err = 0;
1540         int master_wep_flag;
1541         int auth_flag;
1542
1543         if (priv->wep_on)
1544                 __orinoco_hw_setup_wepkeys(priv);
1545
1546         if (priv->wep_restrict)
1547                 auth_flag = HERMES_AUTH_SHARED_KEY;
1548         else
1549                 auth_flag = HERMES_AUTH_OPEN;
1550
1551         switch (priv->firmware_type) {
1552         case FIRMWARE_TYPE_AGERE: /* Agere style WEP */
1553                 if (priv->wep_on) {
1554                         /* Enable the shared-key authentication. */
1555                         err = hermes_write_wordrec(hw, USER_BAP,
1556                                                    HERMES_RID_CNFAUTHENTICATION_AGERE,
1557                                                    auth_flag);
1558                 }
1559                 err = hermes_write_wordrec(hw, USER_BAP,
1560                                            HERMES_RID_CNFWEPENABLED_AGERE,
1561                                            priv->wep_on);
1562                 if (err)
1563                         return err;
1564                 break;
1565
1566         case FIRMWARE_TYPE_INTERSIL: /* Intersil style WEP */
1567         case FIRMWARE_TYPE_SYMBOL: /* Symbol style WEP */
1568                 if (priv->wep_on) {
1569                         if (priv->wep_restrict ||
1570                             (priv->firmware_type == FIRMWARE_TYPE_SYMBOL))
1571                                 master_wep_flag = HERMES_WEP_PRIVACY_INVOKED |
1572                                                   HERMES_WEP_EXCL_UNENCRYPTED;
1573                         else
1574                                 master_wep_flag = HERMES_WEP_PRIVACY_INVOKED;
1575
1576                         err = hermes_write_wordrec(hw, USER_BAP,
1577                                                    HERMES_RID_CNFAUTHENTICATION,
1578                                                    auth_flag);
1579                         if (err)
1580                                 return err;
1581                 } else
1582                         master_wep_flag = 0;
1583
1584                 if (priv->iw_mode == IW_MODE_MONITOR)
1585                         master_wep_flag |= HERMES_WEP_HOST_DECRYPT;
1586
1587                 /* Master WEP setting : on/off */
1588                 err = hermes_write_wordrec(hw, USER_BAP,
1589                                            HERMES_RID_CNFWEPFLAGS_INTERSIL,
1590                                            master_wep_flag);
1591                 if (err)
1592                         return err;     
1593
1594                 break;
1595         }
1596
1597         return 0;
1598 }
1599
1600 static int __orinoco_program_rids(struct net_device *dev)
1601 {
1602         struct orinoco_private *priv = netdev_priv(dev);
1603         hermes_t *hw = &priv->hw;
1604         int err;
1605         struct hermes_idstring idbuf;
1606
1607         /* Set the MAC address */
1608         err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
1609                                HERMES_BYTES_TO_RECLEN(ETH_ALEN), dev->dev_addr);
1610         if (err) {
1611                 printk(KERN_ERR "%s: Error %d setting MAC address\n",
1612                        dev->name, err);
1613                 return err;
1614         }
1615
1616         /* Set up the link mode */
1617         err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFPORTTYPE,
1618                                    priv->port_type);
1619         if (err) {
1620                 printk(KERN_ERR "%s: Error %d setting port type\n",
1621                        dev->name, err);
1622                 return err;
1623         }
1624         /* Set the channel/frequency */
1625         if (priv->channel != 0 && priv->iw_mode != IW_MODE_INFRA) {
1626                 err = hermes_write_wordrec(hw, USER_BAP,
1627                                            HERMES_RID_CNFOWNCHANNEL,
1628                                            priv->channel);
1629                 if (err) {
1630                         printk(KERN_ERR "%s: Error %d setting channel %d\n",
1631                                dev->name, err, priv->channel);
1632                         return err;
1633                 }
1634         }
1635
1636         if (priv->has_ibss) {
1637                 u16 createibss;
1638
1639                 if ((strlen(priv->desired_essid) == 0) && (priv->createibss)) {
1640                         printk(KERN_WARNING "%s: This firmware requires an "
1641                                "ESSID in IBSS-Ad-Hoc mode.\n", dev->name);
1642                         /* With wvlan_cs, in this case, we would crash.
1643                          * hopefully, this driver will behave better...
1644                          * Jean II */
1645                         createibss = 0;
1646                 } else {
1647                         createibss = priv->createibss;
1648                 }
1649                 
1650                 err = hermes_write_wordrec(hw, USER_BAP,
1651                                            HERMES_RID_CNFCREATEIBSS,
1652                                            createibss);
1653                 if (err) {
1654                         printk(KERN_ERR "%s: Error %d setting CREATEIBSS\n",
1655                                dev->name, err);
1656                         return err;
1657                 }
1658         }
1659
1660         /* Set the desired BSSID */
1661         err = __orinoco_hw_set_wap(priv);
1662         if (err) {
1663                 printk(KERN_ERR "%s: Error %d setting AP address\n",
1664                        dev->name, err);
1665                 return err;
1666         }
1667         /* Set the desired ESSID */
1668         idbuf.len = cpu_to_le16(strlen(priv->desired_essid));
1669         memcpy(&idbuf.val, priv->desired_essid, sizeof(idbuf.val));
1670         /* WinXP wants partner to configure OWNSSID even in IBSS mode. (jimc) */
1671         err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNSSID,
1672                                HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
1673                                &idbuf);
1674         if (err) {
1675                 printk(KERN_ERR "%s: Error %d setting OWNSSID\n",
1676                        dev->name, err);
1677                 return err;
1678         }
1679         err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFDESIREDSSID,
1680                                HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
1681                                &idbuf);
1682         if (err) {
1683                 printk(KERN_ERR "%s: Error %d setting DESIREDSSID\n",
1684                        dev->name, err);
1685                 return err;
1686         }
1687
1688         /* Set the station name */
1689         idbuf.len = cpu_to_le16(strlen(priv->nick));
1690         memcpy(&idbuf.val, priv->nick, sizeof(idbuf.val));
1691         err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
1692                                HERMES_BYTES_TO_RECLEN(strlen(priv->nick)+2),
1693                                &idbuf);
1694         if (err) {
1695                 printk(KERN_ERR "%s: Error %d setting nickname\n",
1696                        dev->name, err);
1697                 return err;
1698         }
1699
1700         /* Set AP density */
1701         if (priv->has_sensitivity) {
1702                 err = hermes_write_wordrec(hw, USER_BAP,
1703                                            HERMES_RID_CNFSYSTEMSCALE,
1704                                            priv->ap_density);
1705                 if (err) {
1706                         printk(KERN_WARNING "%s: Error %d setting SYSTEMSCALE.  "
1707                                "Disabling sensitivity control\n",
1708                                dev->name, err);
1709
1710                         priv->has_sensitivity = 0;
1711                 }
1712         }
1713
1714         /* Set RTS threshold */
1715         err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
1716                                    priv->rts_thresh);
1717         if (err) {
1718                 printk(KERN_ERR "%s: Error %d setting RTS threshold\n",
1719                        dev->name, err);
1720                 return err;
1721         }
1722
1723         /* Set fragmentation threshold or MWO robustness */
1724         if (priv->has_mwo)
1725                 err = hermes_write_wordrec(hw, USER_BAP,
1726                                            HERMES_RID_CNFMWOROBUST_AGERE,
1727                                            priv->mwo_robust);
1728         else
1729                 err = hermes_write_wordrec(hw, USER_BAP,
1730                                            HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
1731                                            priv->frag_thresh);
1732         if (err) {
1733                 printk(KERN_ERR "%s: Error %d setting fragmentation\n",
1734                        dev->name, err);
1735                 return err;
1736         }
1737
1738         /* Set bitrate */
1739         err = __orinoco_hw_set_bitrate(priv);
1740         if (err) {
1741                 printk(KERN_ERR "%s: Error %d setting bitrate\n",
1742                        dev->name, err);
1743                 return err;
1744         }
1745
1746         /* Set power management */
1747         if (priv->has_pm) {
1748                 err = hermes_write_wordrec(hw, USER_BAP,
1749                                            HERMES_RID_CNFPMENABLED,
1750                                            priv->pm_on);
1751                 if (err) {
1752                         printk(KERN_ERR "%s: Error %d setting up PM\n",
1753                                dev->name, err);
1754                         return err;
1755                 }
1756
1757                 err = hermes_write_wordrec(hw, USER_BAP,
1758                                            HERMES_RID_CNFMULTICASTRECEIVE,
1759                                            priv->pm_mcast);
1760                 if (err) {
1761                         printk(KERN_ERR "%s: Error %d setting up PM\n",
1762                                dev->name, err);
1763                         return err;
1764                 }
1765                 err = hermes_write_wordrec(hw, USER_BAP,
1766                                            HERMES_RID_CNFMAXSLEEPDURATION,
1767                                            priv->pm_period);
1768                 if (err) {
1769                         printk(KERN_ERR "%s: Error %d setting up PM\n",
1770                                dev->name, err);
1771                         return err;
1772                 }
1773                 err = hermes_write_wordrec(hw, USER_BAP,
1774                                            HERMES_RID_CNFPMHOLDOVERDURATION,
1775                                            priv->pm_timeout);
1776                 if (err) {
1777                         printk(KERN_ERR "%s: Error %d setting up PM\n",
1778                                dev->name, err);
1779                         return err;
1780                 }
1781         }
1782
1783         /* Set preamble - only for Symbol so far... */
1784         if (priv->has_preamble) {
1785                 err = hermes_write_wordrec(hw, USER_BAP,
1786                                            HERMES_RID_CNFPREAMBLE_SYMBOL,
1787                                            priv->preamble);
1788                 if (err) {
1789                         printk(KERN_ERR "%s: Error %d setting preamble\n",
1790                                dev->name, err);
1791                         return err;
1792                 }
1793         }
1794
1795         /* Set up encryption */
1796         if (priv->has_wep) {
1797                 err = __orinoco_hw_setup_wep(priv);
1798                 if (err) {
1799                         printk(KERN_ERR "%s: Error %d activating WEP\n",
1800                                dev->name, err);
1801                         return err;
1802                 }
1803         }
1804
1805         if (priv->iw_mode == IW_MODE_MONITOR) {
1806                 /* Enable monitor mode */
1807                 dev->type = ARPHRD_IEEE80211;
1808                 err = hermes_docmd_wait(hw, HERMES_CMD_TEST | 
1809                                             HERMES_TEST_MONITOR, 0, NULL);
1810         } else {
1811                 /* Disable monitor mode */
1812                 dev->type = ARPHRD_ETHER;
1813                 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
1814                                             HERMES_TEST_STOP, 0, NULL);
1815         }
1816         if (err)
1817                 return err;
1818
1819         /* Set promiscuity / multicast*/
1820         priv->promiscuous = 0;
1821         priv->mc_count = 0;
1822         __orinoco_set_multicast_list(dev); /* FIXME: what about the xmit_lock */
1823
1824         return 0;
1825 }
1826
1827 /* FIXME: return int? */
1828 static void
1829 __orinoco_set_multicast_list(struct net_device *dev)
1830 {
1831         struct orinoco_private *priv = netdev_priv(dev);
1832         hermes_t *hw = &priv->hw;
1833         int err = 0;
1834         int promisc, mc_count;
1835
1836         /* The Hermes doesn't seem to have an allmulti mode, so we go
1837          * into promiscuous mode and let the upper levels deal. */
1838         if ( (dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
1839              (dev->mc_count > MAX_MULTICAST(priv)) ) {
1840                 promisc = 1;
1841                 mc_count = 0;
1842         } else {
1843                 promisc = 0;
1844                 mc_count = dev->mc_count;
1845         }
1846
1847         if (promisc != priv->promiscuous) {
1848                 err = hermes_write_wordrec(hw, USER_BAP,
1849                                            HERMES_RID_CNFPROMISCUOUSMODE,
1850                                            promisc);
1851                 if (err) {
1852                         printk(KERN_ERR "%s: Error %d setting PROMISCUOUSMODE to 1.\n",
1853                                dev->name, err);
1854                 } else 
1855                         priv->promiscuous = promisc;
1856         }
1857
1858         if (! promisc && (mc_count || priv->mc_count) ) {
1859                 struct dev_mc_list *p = dev->mc_list;
1860                 struct hermes_multicast mclist;
1861                 int i;
1862
1863                 for (i = 0; i < mc_count; i++) {
1864                         /* paranoia: is list shorter than mc_count? */
1865                         BUG_ON(! p);
1866                         /* paranoia: bad address size in list? */
1867                         BUG_ON(p->dmi_addrlen != ETH_ALEN);
1868                         
1869                         memcpy(mclist.addr[i], p->dmi_addr, ETH_ALEN);
1870                         p = p->next;
1871                 }
1872                 
1873                 if (p)
1874                         printk(KERN_WARNING "%s: Multicast list is "
1875                                "longer than mc_count\n", dev->name);
1876
1877                 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFGROUPADDRESSES,
1878                                        HERMES_BYTES_TO_RECLEN(priv->mc_count * ETH_ALEN),
1879                                        &mclist);
1880                 if (err)
1881                         printk(KERN_ERR "%s: Error %d setting multicast list.\n",
1882                                dev->name, err);
1883                 else
1884                         priv->mc_count = mc_count;
1885         }
1886
1887         /* Since we can set the promiscuous flag when it wasn't asked
1888            for, make sure the net_device knows about it. */
1889         if (priv->promiscuous)
1890                 dev->flags |= IFF_PROMISC;
1891         else
1892                 dev->flags &= ~IFF_PROMISC;
1893 }
1894
1895 /* This must be called from user context, without locks held - use
1896  * schedule_work() */
1897 static void orinoco_reset(struct net_device *dev)
1898 {
1899         struct orinoco_private *priv = netdev_priv(dev);
1900         struct hermes *hw = &priv->hw;
1901         int err;
1902         unsigned long flags;
1903
1904         if (orinoco_lock(priv, &flags) != 0)
1905                 /* When the hardware becomes available again, whatever
1906                  * detects that is responsible for re-initializing
1907                  * it. So no need for anything further */
1908                 return;
1909
1910         netif_stop_queue(dev);
1911
1912         /* Shut off interrupts.  Depending on what state the hardware
1913          * is in, this might not work, but we'll try anyway */
1914         hermes_set_irqmask(hw, 0);
1915         hermes_write_regn(hw, EVACK, 0xffff);
1916
1917         priv->hw_unavailable++;
1918         priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
1919         netif_carrier_off(dev);
1920
1921         orinoco_unlock(priv, &flags);
1922
1923         /* Scanning support: Cleanup of driver struct */
1924         kfree(priv->scan_result);
1925         priv->scan_result = NULL;
1926         priv->scan_inprogress = 0;
1927
1928         if (priv->hard_reset) {
1929                 err = (*priv->hard_reset)(priv);
1930                 if (err) {
1931                         printk(KERN_ERR "%s: orinoco_reset: Error %d "
1932                                "performing hard reset\n", dev->name, err);
1933                         goto disable;
1934                 }
1935         }
1936
1937         err = orinoco_reinit_firmware(dev);
1938         if (err) {
1939                 printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
1940                        dev->name, err);
1941                 goto disable;
1942         }
1943
1944         spin_lock_irq(&priv->lock); /* This has to be called from user context */
1945
1946         priv->hw_unavailable--;
1947
1948         /* priv->open or priv->hw_unavailable might have changed while
1949          * we dropped the lock */
1950         if (priv->open && (! priv->hw_unavailable)) {
1951                 err = __orinoco_up(dev);
1952                 if (err) {
1953                         printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
1954                                dev->name, err);
1955                 } else
1956                         dev->trans_start = jiffies;
1957         }
1958
1959         spin_unlock_irq(&priv->lock);
1960
1961         return;
1962  disable:
1963         hermes_set_irqmask(hw, 0);
1964         netif_device_detach(dev);
1965         printk(KERN_ERR "%s: Device has been disabled!\n", dev->name);
1966 }
1967
1968 /********************************************************************/
1969 /* Interrupt handler                                                */
1970 /********************************************************************/
1971
1972 static void __orinoco_ev_tick(struct net_device *dev, hermes_t *hw)
1973 {
1974         printk(KERN_DEBUG "%s: TICK\n", dev->name);
1975 }
1976
1977 static void __orinoco_ev_wterr(struct net_device *dev, hermes_t *hw)
1978 {
1979         /* This seems to happen a fair bit under load, but ignoring it
1980            seems to work fine...*/
1981         printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
1982                dev->name);
1983 }
1984
1985 irqreturn_t orinoco_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1986 {
1987         struct net_device *dev = (struct net_device *)dev_id;
1988         struct orinoco_private *priv = netdev_priv(dev);
1989         hermes_t *hw = &priv->hw;
1990         int count = MAX_IRQLOOPS_PER_IRQ;
1991         u16 evstat, events;
1992         /* These are used to detect a runaway interrupt situation */
1993         /* If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
1994          * we panic and shut down the hardware */
1995         static int last_irq_jiffy = 0; /* jiffies value the last time
1996                                         * we were called */
1997         static int loops_this_jiffy = 0;
1998         unsigned long flags;
1999
2000         if (orinoco_lock(priv, &flags) != 0) {
2001                 /* If hw is unavailable - we don't know if the irq was
2002                  * for us or not */
2003                 return IRQ_HANDLED;
2004         }
2005
2006         evstat = hermes_read_regn(hw, EVSTAT);
2007         events = evstat & hw->inten;
2008         if (! events) {
2009                 orinoco_unlock(priv, &flags);
2010                 return IRQ_NONE;
2011         }
2012         
2013         if (jiffies != last_irq_jiffy)
2014                 loops_this_jiffy = 0;
2015         last_irq_jiffy = jiffies;
2016
2017         while (events && count--) {
2018                 if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
2019                         printk(KERN_WARNING "%s: IRQ handler is looping too "
2020                                "much! Resetting.\n", dev->name);
2021                         /* Disable interrupts for now */
2022                         hermes_set_irqmask(hw, 0);
2023                         schedule_work(&priv->reset_work);
2024                         break;
2025                 }
2026
2027                 /* Check the card hasn't been removed */
2028                 if (! hermes_present(hw)) {
2029                         DEBUG(0, "orinoco_interrupt(): card removed\n");
2030                         break;
2031                 }
2032
2033                 if (events & HERMES_EV_TICK)
2034                         __orinoco_ev_tick(dev, hw);
2035                 if (events & HERMES_EV_WTERR)
2036                         __orinoco_ev_wterr(dev, hw);
2037                 if (events & HERMES_EV_INFDROP)
2038                         __orinoco_ev_infdrop(dev, hw);
2039                 if (events & HERMES_EV_INFO)
2040                         __orinoco_ev_info(dev, hw);
2041                 if (events & HERMES_EV_RX)
2042                         __orinoco_ev_rx(dev, hw);
2043                 if (events & HERMES_EV_TXEXC)
2044                         __orinoco_ev_txexc(dev, hw);
2045                 if (events & HERMES_EV_TX)
2046                         __orinoco_ev_tx(dev, hw);
2047                 if (events & HERMES_EV_ALLOC)
2048                         __orinoco_ev_alloc(dev, hw);
2049                 
2050                 hermes_write_regn(hw, EVACK, evstat);
2051
2052                 evstat = hermes_read_regn(hw, EVSTAT);
2053                 events = evstat & hw->inten;
2054         };
2055
2056         orinoco_unlock(priv, &flags);
2057         return IRQ_HANDLED;
2058 }
2059
2060 /********************************************************************/
2061 /* Initialization                                                   */
2062 /********************************************************************/
2063
2064 struct comp_id {
2065         u16 id, variant, major, minor;
2066 } __attribute__ ((packed));
2067
2068 static inline fwtype_t determine_firmware_type(struct comp_id *nic_id)
2069 {
2070         if (nic_id->id < 0x8000)
2071                 return FIRMWARE_TYPE_AGERE;
2072         else if (nic_id->id == 0x8000 && nic_id->major == 0)
2073                 return FIRMWARE_TYPE_SYMBOL;
2074         else
2075                 return FIRMWARE_TYPE_INTERSIL;
2076 }
2077
2078 /* Set priv->firmware type, determine firmware properties */
2079 static int determine_firmware(struct net_device *dev)
2080 {
2081         struct orinoco_private *priv = netdev_priv(dev);
2082         hermes_t *hw = &priv->hw;
2083         int err;
2084         struct comp_id nic_id, sta_id;
2085         unsigned int firmver;
2086         char tmp[SYMBOL_MAX_VER_LEN+1];
2087
2088         /* Get the hardware version */
2089         err = HERMES_READ_RECORD(hw, USER_BAP, HERMES_RID_NICID, &nic_id);
2090         if (err) {
2091                 printk(KERN_ERR "%s: Cannot read hardware identity: error %d\n",
2092                        dev->name, err);
2093                 return err;
2094         }
2095
2096         le16_to_cpus(&nic_id.id);
2097         le16_to_cpus(&nic_id.variant);
2098         le16_to_cpus(&nic_id.major);
2099         le16_to_cpus(&nic_id.minor);
2100         printk(KERN_DEBUG "%s: Hardware identity %04x:%04x:%04x:%04x\n",
2101                dev->name, nic_id.id, nic_id.variant,
2102                nic_id.major, nic_id.minor);
2103
2104         priv->firmware_type = determine_firmware_type(&nic_id);
2105
2106         /* Get the firmware version */
2107         err = HERMES_READ_RECORD(hw, USER_BAP, HERMES_RID_STAID, &sta_id);
2108         if (err) {
2109                 printk(KERN_ERR "%s: Cannot read station identity: error %d\n",
2110                        dev->name, err);
2111                 return err;
2112         }
2113
2114         le16_to_cpus(&sta_id.id);
2115         le16_to_cpus(&sta_id.variant);
2116         le16_to_cpus(&sta_id.major);
2117         le16_to_cpus(&sta_id.minor);
2118         printk(KERN_DEBUG "%s: Station identity  %04x:%04x:%04x:%04x\n",
2119                dev->name, sta_id.id, sta_id.variant,
2120                sta_id.major, sta_id.minor);
2121
2122         switch (sta_id.id) {
2123         case 0x15:
2124                 printk(KERN_ERR "%s: Primary firmware is active\n",
2125                        dev->name);
2126                 return -ENODEV;
2127         case 0x14b:
2128                 printk(KERN_ERR "%s: Tertiary firmware is active\n",
2129                        dev->name);
2130                 return -ENODEV;
2131         case 0x1f:      /* Intersil, Agere, Symbol Spectrum24 */
2132         case 0x21:      /* Symbol Spectrum24 Trilogy */
2133                 break;
2134         default:
2135                 printk(KERN_NOTICE "%s: Unknown station ID, please report\n",
2136                        dev->name);
2137                 break;
2138         }
2139
2140         /* Default capabilities */
2141         priv->has_sensitivity = 1;
2142         priv->has_mwo = 0;
2143         priv->has_preamble = 0;
2144         priv->has_port3 = 1;
2145         priv->has_ibss = 1;
2146         priv->has_wep = 0;
2147         priv->has_big_wep = 0;
2148
2149         /* Determine capabilities from the firmware version */
2150         switch (priv->firmware_type) {
2151         case FIRMWARE_TYPE_AGERE:
2152                 /* Lucent Wavelan IEEE, Lucent Orinoco, Cabletron RoamAbout,
2153                    ELSA, Melco, HP, IBM, Dell 1150, Compaq 110/210 */
2154                 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
2155                          "Lucent/Agere %d.%02d", sta_id.major, sta_id.minor);
2156
2157                 firmver = ((unsigned long)sta_id.major << 16) | sta_id.minor;
2158
2159                 priv->has_ibss = (firmver >= 0x60006);
2160                 priv->has_wep = (firmver >= 0x40020);
2161                 priv->has_big_wep = 1; /* FIXME: this is wrong - how do we tell
2162                                           Gold cards from the others? */
2163                 priv->has_mwo = (firmver >= 0x60000);
2164                 priv->has_pm = (firmver >= 0x40020); /* Don't work in 7.52 ? */
2165                 priv->ibss_port = 1;
2166                 priv->has_hostscan = (firmver >= 0x8000a);
2167                 priv->broken_monitor = (firmver >= 0x80000);
2168
2169                 /* Tested with Agere firmware :
2170                  *      1.16 ; 4.08 ; 4.52 ; 6.04 ; 6.16 ; 7.28 => Jean II
2171                  * Tested CableTron firmware : 4.32 => Anton */
2172                 break;
2173         case FIRMWARE_TYPE_SYMBOL:
2174                 /* Symbol , 3Com AirConnect, Intel, Ericsson WLAN */
2175                 /* Intel MAC : 00:02:B3:* */
2176                 /* 3Com MAC : 00:50:DA:* */
2177                 memset(tmp, 0, sizeof(tmp));
2178                 /* Get the Symbol firmware version */
2179                 err = hermes_read_ltv(hw, USER_BAP,
2180                                       HERMES_RID_SECONDARYVERSION_SYMBOL,
2181                                       SYMBOL_MAX_VER_LEN, NULL, &tmp);
2182                 if (err) {
2183                         printk(KERN_WARNING
2184                                "%s: Error %d reading Symbol firmware info. Wildly guessing capabilities...\n",
2185                                dev->name, err);
2186                         firmver = 0;
2187                         tmp[0] = '\0';
2188                 } else {
2189                         /* The firmware revision is a string, the format is
2190                          * something like : "V2.20-01".
2191                          * Quick and dirty parsing... - Jean II
2192                          */
2193                         firmver = ((tmp[1] - '0') << 16) | ((tmp[3] - '0') << 12)
2194                                 | ((tmp[4] - '0') << 8) | ((tmp[6] - '0') << 4)
2195                                 | (tmp[7] - '0');
2196
2197                         tmp[SYMBOL_MAX_VER_LEN] = '\0';
2198                 }
2199
2200                 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
2201                          "Symbol %s", tmp);
2202
2203                 priv->has_ibss = (firmver >= 0x20000);
2204                 priv->has_wep = (firmver >= 0x15012);
2205                 priv->has_big_wep = (firmver >= 0x20000);
2206                 priv->has_pm = (firmver >= 0x20000 && firmver < 0x22000) || 
2207                                (firmver >= 0x29000 && firmver < 0x30000) ||
2208                                firmver >= 0x31000;
2209                 priv->has_preamble = (firmver >= 0x20000);
2210                 priv->ibss_port = 4;
2211                 priv->broken_disableport = (firmver == 0x25013) ||
2212                                            (firmver >= 0x30000 && firmver <= 0x31000);
2213                 priv->has_hostscan = (firmver >= 0x31001) ||
2214                                      (firmver >= 0x29057 && firmver < 0x30000);
2215                 /* Tested with Intel firmware : 0x20015 => Jean II */
2216                 /* Tested with 3Com firmware : 0x15012 & 0x22001 => Jean II */
2217                 break;
2218         case FIRMWARE_TYPE_INTERSIL:
2219                 /* D-Link, Linksys, Adtron, ZoomAir, and many others...
2220                  * Samsung, Compaq 100/200 and Proxim are slightly
2221                  * different and less well tested */
2222                 /* D-Link MAC : 00:40:05:* */
2223                 /* Addtron MAC : 00:90:D1:* */
2224                 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
2225                          "Intersil %d.%d.%d", sta_id.major, sta_id.minor,
2226                          sta_id.variant);
2227
2228                 firmver = ((unsigned long)sta_id.major << 16) |
2229                         ((unsigned long)sta_id.minor << 8) | sta_id.variant;
2230
2231                 priv->has_ibss = (firmver >= 0x000700); /* FIXME */
2232                 priv->has_big_wep = priv->has_wep = (firmver >= 0x000800);
2233                 priv->has_pm = (firmver >= 0x000700);
2234                 priv->has_hostscan = (firmver >= 0x010301);
2235
2236                 if (firmver >= 0x000800)
2237                         priv->ibss_port = 0;
2238                 else {
2239                         printk(KERN_NOTICE "%s: Intersil firmware earlier "
2240                                "than v0.8.x - several features not supported\n",
2241                                dev->name);
2242                         priv->ibss_port = 1;
2243                 }
2244                 break;
2245         }
2246         printk(KERN_DEBUG "%s: Firmware determined as %s\n", dev->name,
2247                priv->fw_name);
2248
2249         return 0;
2250 }
2251
2252 static int orinoco_init(struct net_device *dev)
2253 {
2254         struct orinoco_private *priv = netdev_priv(dev);
2255         hermes_t *hw = &priv->hw;
2256         int err = 0;
2257         struct hermes_idstring nickbuf;
2258         u16 reclen;
2259         int len;
2260
2261         TRACE_ENTER(dev->name);
2262
2263         /* No need to lock, the hw_unavailable flag is already set in
2264          * alloc_orinocodev() */
2265         priv->nicbuf_size = IEEE80211_FRAME_LEN + ETH_HLEN;
2266
2267         /* Initialize the firmware */
2268         err = orinoco_reinit_firmware(dev);
2269         if (err != 0) {
2270                 printk(KERN_ERR "%s: failed to initialize firmware (err = %d)\n",
2271                        dev->name, err);
2272                 goto out;
2273         }
2274
2275         err = determine_firmware(dev);
2276         if (err != 0) {
2277                 printk(KERN_ERR "%s: Incompatible firmware, aborting\n",
2278                        dev->name);
2279                 goto out;
2280         }
2281
2282         if (priv->has_port3)
2283                 printk(KERN_DEBUG "%s: Ad-hoc demo mode supported\n", dev->name);
2284         if (priv->has_ibss)
2285                 printk(KERN_DEBUG "%s: IEEE standard IBSS ad-hoc mode supported\n",
2286                        dev->name);
2287         if (priv->has_wep) {
2288                 printk(KERN_DEBUG "%s: WEP supported, ", dev->name);
2289                 if (priv->has_big_wep)
2290                         printk("104-bit key\n");
2291                 else
2292                         printk("40-bit key\n");
2293         }
2294
2295         /* Get the MAC address */
2296         err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
2297                               ETH_ALEN, NULL, dev->dev_addr);
2298         if (err) {
2299                 printk(KERN_WARNING "%s: failed to read MAC address!\n",
2300                        dev->name);
2301                 goto out;
2302         }
2303
2304         printk(KERN_DEBUG "%s: MAC address %02X:%02X:%02X:%02X:%02X:%02X\n",
2305                dev->name, dev->dev_addr[0], dev->dev_addr[1],
2306                dev->dev_addr[2], dev->dev_addr[3], dev->dev_addr[4],
2307                dev->dev_addr[5]);
2308
2309         /* Get the station name */
2310         err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
2311                               sizeof(nickbuf), &reclen, &nickbuf);
2312         if (err) {
2313                 printk(KERN_ERR "%s: failed to read station name\n",
2314                        dev->name);
2315                 goto out;
2316         }
2317         if (nickbuf.len)
2318                 len = min(IW_ESSID_MAX_SIZE, (int)le16_to_cpu(nickbuf.len));
2319         else
2320                 len = min(IW_ESSID_MAX_SIZE, 2 * reclen);
2321         memcpy(priv->nick, &nickbuf.val, len);
2322         priv->nick[len] = '\0';
2323
2324         printk(KERN_DEBUG "%s: Station name \"%s\"\n", dev->name, priv->nick);
2325
2326         /* Get allowed channels */
2327         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CHANNELLIST,
2328                                   &priv->channel_mask);
2329         if (err) {
2330                 printk(KERN_ERR "%s: failed to read channel list!\n",
2331                        dev->name);
2332                 goto out;
2333         }
2334
2335         /* Get initial AP density */
2336         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFSYSTEMSCALE,
2337                                   &priv->ap_density);
2338         if (err || priv->ap_density < 1 || priv->ap_density > 3) {
2339                 priv->has_sensitivity = 0;
2340         }
2341
2342         /* Get initial RTS threshold */
2343         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
2344                                   &priv->rts_thresh);
2345         if (err) {
2346                 printk(KERN_ERR "%s: failed to read RTS threshold!\n",
2347                        dev->name);
2348                 goto out;
2349         }
2350
2351         /* Get initial fragmentation settings */
2352         if (priv->has_mwo)
2353                 err = hermes_read_wordrec(hw, USER_BAP,
2354                                           HERMES_RID_CNFMWOROBUST_AGERE,
2355                                           &priv->mwo_robust);
2356         else
2357                 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
2358                                           &priv->frag_thresh);
2359         if (err) {
2360                 printk(KERN_ERR "%s: failed to read fragmentation settings!\n",
2361                        dev->name);
2362                 goto out;
2363         }
2364
2365         /* Power management setup */
2366         if (priv->has_pm) {
2367                 priv->pm_on = 0;
2368                 priv->pm_mcast = 1;
2369                 err = hermes_read_wordrec(hw, USER_BAP,
2370                                           HERMES_RID_CNFMAXSLEEPDURATION,
2371                                           &priv->pm_period);
2372                 if (err) {
2373                         printk(KERN_ERR "%s: failed to read power management period!\n",
2374                                dev->name);
2375                         goto out;
2376                 }
2377                 err = hermes_read_wordrec(hw, USER_BAP,
2378                                           HERMES_RID_CNFPMHOLDOVERDURATION,
2379                                           &priv->pm_timeout);
2380                 if (err) {
2381                         printk(KERN_ERR "%s: failed to read power management timeout!\n",
2382                                dev->name);
2383                         goto out;
2384                 }
2385         }
2386
2387         /* Preamble setup */
2388         if (priv->has_preamble) {
2389                 err = hermes_read_wordrec(hw, USER_BAP,
2390                                           HERMES_RID_CNFPREAMBLE_SYMBOL,
2391                                           &priv->preamble);
2392                 if (err)
2393                         goto out;
2394         }
2395                 
2396         /* Set up the default configuration */
2397         priv->iw_mode = IW_MODE_INFRA;
2398         /* By default use IEEE/IBSS ad-hoc mode if we have it */
2399         priv->prefer_port3 = priv->has_port3 && (! priv->has_ibss);
2400         set_port_type(priv);
2401         priv->channel = 0; /* use firmware default */
2402
2403         priv->promiscuous = 0;
2404         priv->wep_on = 0;
2405         priv->tx_key = 0;
2406
2407         /* Make the hardware available, as long as it hasn't been
2408          * removed elsewhere (e.g. by PCMCIA hot unplug) */
2409         spin_lock_irq(&priv->lock);
2410         priv->hw_unavailable--;
2411         spin_unlock_irq(&priv->lock);
2412
2413         printk(KERN_DEBUG "%s: ready\n", dev->name);
2414
2415  out:
2416         TRACE_EXIT(dev->name);
2417         return err;
2418 }
2419
2420 struct net_device *alloc_orinocodev(int sizeof_card,
2421                                     int (*hard_reset)(struct orinoco_private *))
2422 {
2423         struct net_device *dev;
2424         struct orinoco_private *priv;
2425
2426         dev = alloc_etherdev(sizeof(struct orinoco_private) + sizeof_card);
2427         if (! dev)
2428                 return NULL;
2429         priv = netdev_priv(dev);
2430         priv->ndev = dev;
2431         if (sizeof_card)
2432                 priv->card = (void *)((unsigned long)priv
2433                                       + sizeof(struct orinoco_private));
2434         else
2435                 priv->card = NULL;
2436
2437         /* Setup / override net_device fields */
2438         dev->init = orinoco_init;
2439         dev->hard_start_xmit = orinoco_xmit;
2440         dev->tx_timeout = orinoco_tx_timeout;
2441         dev->watchdog_timeo = HZ; /* 1 second timeout */
2442         dev->get_stats = orinoco_get_stats;
2443         dev->ethtool_ops = &orinoco_ethtool_ops;
2444         dev->wireless_handlers = (struct iw_handler_def *)&orinoco_handler_def;
2445 #ifdef WIRELESS_SPY
2446         priv->wireless_data.spy_data = &priv->spy_data;
2447         dev->wireless_data = &priv->wireless_data;
2448 #endif
2449         dev->change_mtu = orinoco_change_mtu;
2450         dev->set_multicast_list = orinoco_set_multicast_list;
2451         /* we use the default eth_mac_addr for setting the MAC addr */
2452
2453         /* Set up default callbacks */
2454         dev->open = orinoco_open;
2455         dev->stop = orinoco_stop;
2456         priv->hard_reset = hard_reset;
2457
2458         spin_lock_init(&priv->lock);
2459         priv->open = 0;
2460         priv->hw_unavailable = 1; /* orinoco_init() must clear this
2461                                    * before anything else touches the
2462                                    * hardware */
2463         INIT_WORK(&priv->reset_work, (void (*)(void *))orinoco_reset, dev);
2464         INIT_WORK(&priv->join_work, (void (*)(void *))orinoco_join_ap, dev);
2465         INIT_WORK(&priv->wevent_work, (void (*)(void *))orinoco_send_wevents, dev);
2466
2467         netif_carrier_off(dev);
2468         priv->last_linkstatus = 0xffff;
2469
2470         return dev;
2471
2472 }
2473
2474 void free_orinocodev(struct net_device *dev)
2475 {
2476         struct orinoco_private *priv = netdev_priv(dev);
2477
2478         kfree(priv->scan_result);
2479         free_netdev(dev);
2480 }
2481
2482 /********************************************************************/
2483 /* Wireless extensions                                              */
2484 /********************************************************************/
2485
2486 static int orinoco_hw_get_essid(struct orinoco_private *priv, int *active,
2487                                 char buf[IW_ESSID_MAX_SIZE+1])
2488 {
2489         hermes_t *hw = &priv->hw;
2490         int err = 0;
2491         struct hermes_idstring essidbuf;
2492         char *p = (char *)(&essidbuf.val);
2493         int len;
2494         unsigned long flags;
2495
2496         if (orinoco_lock(priv, &flags) != 0)
2497                 return -EBUSY;
2498
2499         if (strlen(priv->desired_essid) > 0) {
2500                 /* We read the desired SSID from the hardware rather
2501                    than from priv->desired_essid, just in case the
2502                    firmware is allowed to change it on us. I'm not
2503                    sure about this */
2504                 /* My guess is that the OWNSSID should always be whatever
2505                  * we set to the card, whereas CURRENT_SSID is the one that
2506                  * may change... - Jean II */
2507                 u16 rid;
2508
2509                 *active = 1;
2510
2511                 rid = (priv->port_type == 3) ? HERMES_RID_CNFOWNSSID :
2512                         HERMES_RID_CNFDESIREDSSID;
2513                 
2514                 err = hermes_read_ltv(hw, USER_BAP, rid, sizeof(essidbuf),
2515                                       NULL, &essidbuf);
2516                 if (err)
2517                         goto fail_unlock;
2518         } else {
2519                 *active = 0;
2520
2521                 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTSSID,
2522                                       sizeof(essidbuf), NULL, &essidbuf);
2523                 if (err)
2524                         goto fail_unlock;
2525         }
2526
2527         len = le16_to_cpu(essidbuf.len);
2528         BUG_ON(len > IW_ESSID_MAX_SIZE);
2529
2530         memset(buf, 0, IW_ESSID_MAX_SIZE+1);
2531         memcpy(buf, p, len);
2532         buf[len] = '\0';
2533
2534  fail_unlock:
2535         orinoco_unlock(priv, &flags);
2536
2537         return err;       
2538 }
2539
2540 static long orinoco_hw_get_freq(struct orinoco_private *priv)
2541 {
2542         
2543         hermes_t *hw = &priv->hw;
2544         int err = 0;
2545         u16 channel;
2546         long freq = 0;
2547         unsigned long flags;
2548
2549         if (orinoco_lock(priv, &flags) != 0)
2550                 return -EBUSY;
2551         
2552         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CURRENTCHANNEL, &channel);
2553         if (err)
2554                 goto out;
2555
2556         /* Intersil firmware 1.3.5 returns 0 when the interface is down */
2557         if (channel == 0) {
2558                 err = -EBUSY;
2559                 goto out;
2560         }
2561
2562         if ( (channel < 1) || (channel > NUM_CHANNELS) ) {
2563                 printk(KERN_WARNING "%s: Channel out of range (%d)!\n",
2564                        priv->ndev->name, channel);
2565                 err = -EBUSY;
2566                 goto out;
2567
2568         }
2569         freq = channel_frequency[channel-1] * 100000;
2570
2571  out:
2572         orinoco_unlock(priv, &flags);
2573
2574         if (err > 0)
2575                 err = -EBUSY;
2576         return err ? err : freq;
2577 }
2578
2579 static int orinoco_hw_get_bitratelist(struct orinoco_private *priv,
2580                                       int *numrates, s32 *rates, int max)
2581 {
2582         hermes_t *hw = &priv->hw;
2583         struct hermes_idstring list;
2584         unsigned char *p = (unsigned char *)&list.val;
2585         int err = 0;
2586         int num;
2587         int i;
2588         unsigned long flags;
2589
2590         if (orinoco_lock(priv, &flags) != 0)
2591                 return -EBUSY;
2592
2593         err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_SUPPORTEDDATARATES,
2594                               sizeof(list), NULL, &list);
2595         orinoco_unlock(priv, &flags);
2596
2597         if (err)
2598                 return err;
2599         
2600         num = le16_to_cpu(list.len);
2601         *numrates = num;
2602         num = min(num, max);
2603
2604         for (i = 0; i < num; i++) {
2605                 rates[i] = (p[i] & 0x7f) * 500000; /* convert to bps */
2606         }
2607
2608         return 0;
2609 }
2610
2611 static int orinoco_ioctl_getname(struct net_device *dev,
2612                                  struct iw_request_info *info,
2613                                  char *name,
2614                                  char *extra)
2615 {
2616         struct orinoco_private *priv = netdev_priv(dev);
2617         int numrates;
2618         int err;
2619
2620         err = orinoco_hw_get_bitratelist(priv, &numrates, NULL, 0);
2621
2622         if (!err && (numrates > 2))
2623                 strcpy(name, "IEEE 802.11b");
2624         else
2625                 strcpy(name, "IEEE 802.11-DS");
2626
2627         return 0;
2628 }
2629
2630 static int orinoco_ioctl_setwap(struct net_device *dev,
2631                                 struct iw_request_info *info,
2632                                 struct sockaddr *ap_addr,
2633                                 char *extra)
2634 {
2635         struct orinoco_private *priv = netdev_priv(dev);
2636         int err = -EINPROGRESS;         /* Call commit handler */
2637         unsigned long flags;
2638         static const u8 off_addr[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
2639         static const u8 any_addr[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
2640
2641         if (orinoco_lock(priv, &flags) != 0)
2642                 return -EBUSY;
2643
2644         /* Enable automatic roaming - no sanity checks are needed */
2645         if (memcmp(&ap_addr->sa_data, off_addr, ETH_ALEN) == 0 ||
2646             memcmp(&ap_addr->sa_data, any_addr, ETH_ALEN) == 0) {
2647                 priv->bssid_fixed = 0;
2648                 memset(priv->desired_bssid, 0, ETH_ALEN);
2649
2650                 /* "off" means keep existing connection */
2651                 if (ap_addr->sa_data[0] == 0) {
2652                         __orinoco_hw_set_wap(priv);
2653                         err = 0;
2654                 }
2655                 goto out;
2656         }
2657
2658         if (priv->firmware_type == FIRMWARE_TYPE_AGERE) {
2659                 printk(KERN_WARNING "%s: Lucent/Agere firmware doesn't "
2660                        "support manual roaming\n",
2661                        dev->name);
2662                 err = -EOPNOTSUPP;
2663                 goto out;
2664         }
2665
2666         if (priv->iw_mode != IW_MODE_INFRA) {
2667                 printk(KERN_WARNING "%s: Manual roaming supported only in "
2668                        "managed mode\n", dev->name);
2669                 err = -EOPNOTSUPP;
2670                 goto out;
2671         }
2672
2673         /* Intersil firmware hangs without Desired ESSID */
2674         if (priv->firmware_type == FIRMWARE_TYPE_INTERSIL &&
2675             strlen(priv->desired_essid) == 0) {
2676                 printk(KERN_WARNING "%s: Desired ESSID must be set for "
2677                        "manual roaming\n", dev->name);
2678                 err = -EOPNOTSUPP;
2679                 goto out;
2680         }
2681
2682         /* Finally, enable manual roaming */
2683         priv->bssid_fixed = 1;
2684         memcpy(priv->desired_bssid, &ap_addr->sa_data, ETH_ALEN);
2685
2686  out:
2687         orinoco_unlock(priv, &flags);
2688         return err;
2689 }
2690
2691 static int orinoco_ioctl_getwap(struct net_device *dev,
2692                                 struct iw_request_info *info,
2693                                 struct sockaddr *ap_addr,
2694                                 char *extra)
2695 {
2696         struct orinoco_private *priv = netdev_priv(dev);
2697
2698         hermes_t *hw = &priv->hw;
2699         int err = 0;
2700         unsigned long flags;
2701
2702         if (orinoco_lock(priv, &flags) != 0)
2703                 return -EBUSY;
2704
2705         ap_addr->sa_family = ARPHRD_ETHER;
2706         err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
2707                               ETH_ALEN, NULL, ap_addr->sa_data);
2708
2709         orinoco_unlock(priv, &flags);
2710
2711         return err;
2712 }
2713
2714 static int orinoco_ioctl_setmode(struct net_device *dev,
2715                                  struct iw_request_info *info,
2716                                  u32 *mode,
2717                                  char *extra)
2718 {
2719         struct orinoco_private *priv = netdev_priv(dev);
2720         int err = -EINPROGRESS;         /* Call commit handler */
2721         unsigned long flags;
2722
2723         if (priv->iw_mode == *mode)
2724                 return 0;
2725
2726         if (orinoco_lock(priv, &flags) != 0)
2727                 return -EBUSY;
2728
2729         switch (*mode) {
2730         case IW_MODE_ADHOC:
2731                 if (!priv->has_ibss && !priv->has_port3)
2732                         err = -EOPNOTSUPP;
2733                 break;
2734
2735         case IW_MODE_INFRA:
2736                 break;
2737
2738         case IW_MODE_MONITOR:
2739                 if (priv->broken_monitor && !force_monitor) {
2740                         printk(KERN_WARNING "%s: Monitor mode support is "
2741                                "buggy in this firmware, not enabling\n",
2742                                dev->name);
2743                         err = -EOPNOTSUPP;
2744                 }
2745                 break;
2746
2747         default:
2748                 err = -EOPNOTSUPP;
2749                 break;
2750         }
2751
2752         if (err == -EINPROGRESS) {
2753                 priv->iw_mode = *mode;
2754                 set_port_type(priv);
2755         }
2756
2757         orinoco_unlock(priv, &flags);
2758
2759         return err;
2760 }
2761
2762 static int orinoco_ioctl_getmode(struct net_device *dev,
2763                                  struct iw_request_info *info,
2764                                  u32 *mode,
2765                                  char *extra)
2766 {
2767         struct orinoco_private *priv = netdev_priv(dev);
2768
2769         *mode = priv->iw_mode;
2770         return 0;
2771 }
2772
2773 static int orinoco_ioctl_getiwrange(struct net_device *dev,
2774                                     struct iw_request_info *info,
2775                                     struct iw_point *rrq,
2776                                     char *extra)
2777 {
2778         struct orinoco_private *priv = netdev_priv(dev);
2779         int err = 0;
2780         struct iw_range *range = (struct iw_range *) extra;
2781         int numrates;
2782         int i, k;
2783
2784         TRACE_ENTER(dev->name);
2785
2786         rrq->length = sizeof(struct iw_range);
2787         memset(range, 0, sizeof(struct iw_range));
2788
2789         range->we_version_compiled = WIRELESS_EXT;
2790         range->we_version_source = 14;
2791
2792         /* Set available channels/frequencies */
2793         range->num_channels = NUM_CHANNELS;
2794         k = 0;
2795         for (i = 0; i < NUM_CHANNELS; i++) {
2796                 if (priv->channel_mask & (1 << i)) {
2797                         range->freq[k].i = i + 1;
2798                         range->freq[k].m = channel_frequency[i] * 100000;
2799                         range->freq[k].e = 1;
2800                         k++;
2801                 }
2802                 
2803                 if (k >= IW_MAX_FREQUENCIES)
2804                         break;
2805         }
2806         range->num_frequency = k;
2807         range->sensitivity = 3;
2808
2809         if (priv->has_wep) {
2810                 range->max_encoding_tokens = ORINOCO_MAX_KEYS;
2811                 range->encoding_size[0] = SMALL_KEY_SIZE;
2812                 range->num_encoding_sizes = 1;
2813
2814                 if (priv->has_big_wep) {
2815                         range->encoding_size[1] = LARGE_KEY_SIZE;
2816                         range->num_encoding_sizes = 2;
2817                 }
2818         }
2819
2820         if ((priv->iw_mode == IW_MODE_ADHOC) && (!SPY_NUMBER(priv))){
2821                 /* Quality stats meaningless in ad-hoc mode */
2822         } else {
2823                 range->max_qual.qual = 0x8b - 0x2f;
2824                 range->max_qual.level = 0x2f - 0x95 - 1;
2825                 range->max_qual.noise = 0x2f - 0x95 - 1;
2826                 /* Need to get better values */
2827                 range->avg_qual.qual = 0x24;
2828                 range->avg_qual.level = 0xC2;
2829                 range->avg_qual.noise = 0x9E;
2830         }
2831
2832         err = orinoco_hw_get_bitratelist(priv, &numrates,
2833                                          range->bitrate, IW_MAX_BITRATES);
2834         if (err)
2835                 return err;
2836         range->num_bitrates = numrates;
2837
2838         /* Set an indication of the max TCP throughput in bit/s that we can
2839          * expect using this interface. May be use for QoS stuff...
2840          * Jean II */
2841         if (numrates > 2)
2842                 range->throughput = 5 * 1000 * 1000;    /* ~5 Mb/s */
2843         else
2844                 range->throughput = 1.5 * 1000 * 1000;  /* ~1.5 Mb/s */
2845
2846         range->min_rts = 0;
2847         range->max_rts = 2347;
2848         range->min_frag = 256;
2849         range->max_frag = 2346;
2850
2851         range->min_pmp = 0;
2852         range->max_pmp = 65535000;
2853         range->min_pmt = 0;
2854         range->max_pmt = 65535 * 1000;  /* ??? */
2855         range->pmp_flags = IW_POWER_PERIOD;
2856         range->pmt_flags = IW_POWER_TIMEOUT;
2857         range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_UNICAST_R;
2858
2859         range->retry_capa = IW_RETRY_LIMIT | IW_RETRY_LIFETIME;
2860         range->retry_flags = IW_RETRY_LIMIT;
2861         range->r_time_flags = IW_RETRY_LIFETIME;
2862         range->min_retry = 0;
2863         range->max_retry = 65535;       /* ??? */
2864         range->min_r_time = 0;
2865         range->max_r_time = 65535 * 1000;       /* ??? */
2866
2867         /* Event capability (kernel) */
2868         IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
2869         /* Event capability (driver) */
2870         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWTHRSPY);
2871         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
2872         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
2873         IW_EVENT_CAPA_SET(range->event_capa, IWEVTXDROP);
2874
2875         TRACE_EXIT(dev->name);
2876
2877         return 0;
2878 }
2879
2880 static int orinoco_ioctl_setiwencode(struct net_device *dev,
2881                                      struct iw_request_info *info,
2882                                      struct iw_point *erq,
2883                                      char *keybuf)
2884 {
2885         struct orinoco_private *priv = netdev_priv(dev);
2886         int index = (erq->flags & IW_ENCODE_INDEX) - 1;
2887         int setindex = priv->tx_key;
2888         int enable = priv->wep_on;
2889         int restricted = priv->wep_restrict;
2890         u16 xlen = 0;
2891         int err = -EINPROGRESS;         /* Call commit handler */
2892         unsigned long flags;
2893
2894         if (! priv->has_wep)
2895                 return -EOPNOTSUPP;
2896
2897         if (erq->pointer) {
2898                 /* We actually have a key to set - check its length */
2899                 if (erq->length > LARGE_KEY_SIZE)
2900                         return -E2BIG;
2901
2902                 if ( (erq->length > SMALL_KEY_SIZE) && !priv->has_big_wep )
2903                         return -E2BIG;
2904         }
2905
2906         if (orinoco_lock(priv, &flags) != 0)
2907                 return -EBUSY;
2908
2909         if (erq->pointer) {
2910                 if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
2911                         index = priv->tx_key;
2912
2913                 /* Adjust key length to a supported value */
2914                 if (erq->length > SMALL_KEY_SIZE) {
2915                         xlen = LARGE_KEY_SIZE;
2916                 } else if (erq->length > 0) {
2917                         xlen = SMALL_KEY_SIZE;
2918                 } else
2919                         xlen = 0;
2920
2921                 /* Switch on WEP if off */
2922                 if ((!enable) && (xlen > 0)) {
2923                         setindex = index;
2924                         enable = 1;
2925                 }
2926         } else {
2927                 /* Important note : if the user do "iwconfig eth0 enc off",
2928                  * we will arrive there with an index of -1. This is valid
2929                  * but need to be taken care off... Jean II */
2930                 if ((index < 0) || (index >= ORINOCO_MAX_KEYS)) {
2931                         if((index != -1) || (erq->flags == 0)) {
2932                                 err = -EINVAL;
2933                                 goto out;
2934                         }
2935                 } else {
2936                         /* Set the index : Check that the key is valid */
2937                         if(priv->keys[index].len == 0) {
2938                                 err = -EINVAL;
2939                                 goto out;
2940                         }
2941                         setindex = index;
2942                 }
2943         }
2944
2945         if (erq->flags & IW_ENCODE_DISABLED)
2946                 enable = 0;
2947         if (erq->flags & IW_ENCODE_OPEN)
2948                 restricted = 0;
2949         if (erq->flags & IW_ENCODE_RESTRICTED)
2950                 restricted = 1;
2951
2952         if (erq->pointer) {
2953                 priv->keys[index].len = cpu_to_le16(xlen);
2954                 memset(priv->keys[index].data, 0,
2955                        sizeof(priv->keys[index].data));
2956                 memcpy(priv->keys[index].data, keybuf, erq->length);
2957         }
2958         priv->tx_key = setindex;
2959
2960         /* Try fast key change if connected and only keys are changed */
2961         if (priv->wep_on && enable && (priv->wep_restrict == restricted) &&
2962             netif_carrier_ok(dev)) {
2963                 err = __orinoco_hw_setup_wepkeys(priv);
2964                 /* No need to commit if successful */
2965                 goto out;
2966         }
2967
2968         priv->wep_on = enable;
2969         priv->wep_restrict = restricted;
2970
2971  out:
2972         orinoco_unlock(priv, &flags);
2973
2974         return err;
2975 }
2976
2977 static int orinoco_ioctl_getiwencode(struct net_device *dev,
2978                                      struct iw_request_info *info,
2979                                      struct iw_point *erq,
2980                                      char *keybuf)
2981 {
2982         struct orinoco_private *priv = netdev_priv(dev);
2983         int index = (erq->flags & IW_ENCODE_INDEX) - 1;
2984         u16 xlen = 0;
2985         unsigned long flags;
2986
2987         if (! priv->has_wep)
2988                 return -EOPNOTSUPP;
2989
2990         if (orinoco_lock(priv, &flags) != 0)
2991                 return -EBUSY;
2992
2993         if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
2994                 index = priv->tx_key;
2995
2996         erq->flags = 0;
2997         if (! priv->wep_on)
2998                 erq->flags |= IW_ENCODE_DISABLED;
2999         erq->flags |= index + 1;
3000
3001         if (priv->wep_restrict)
3002                 erq->flags |= IW_ENCODE_RESTRICTED;
3003         else
3004                 erq->flags |= IW_ENCODE_OPEN;
3005
3006         xlen = le16_to_cpu(priv->keys[index].len);
3007
3008         erq->length = xlen;
3009
3010         memcpy(keybuf, priv->keys[index].data, ORINOCO_MAX_KEY_SIZE);
3011
3012         orinoco_unlock(priv, &flags);
3013         return 0;
3014 }
3015
3016 static int orinoco_ioctl_setessid(struct net_device *dev,
3017                                   struct iw_request_info *info,
3018                                   struct iw_point *erq,
3019                                   char *essidbuf)
3020 {
3021         struct orinoco_private *priv = netdev_priv(dev);
3022         unsigned long flags;
3023
3024         /* Note : ESSID is ignored in Ad-Hoc demo mode, but we can set it
3025          * anyway... - Jean II */
3026
3027         /* Hum... Should not use Wireless Extension constant (may change),
3028          * should use our own... - Jean II */
3029         if (erq->length > IW_ESSID_MAX_SIZE)
3030                 return -E2BIG;
3031
3032         if (orinoco_lock(priv, &flags) != 0)
3033                 return -EBUSY;
3034
3035         /* NULL the string (for NULL termination & ESSID = ANY) - Jean II */
3036         memset(priv->desired_essid, 0, sizeof(priv->desired_essid));
3037
3038         /* If not ANY, get the new ESSID */
3039         if (erq->flags) {
3040                 memcpy(priv->desired_essid, essidbuf, erq->length);
3041         }
3042
3043         orinoco_unlock(priv, &flags);
3044
3045         return -EINPROGRESS;            /* Call commit handler */
3046 }
3047
3048 static int orinoco_ioctl_getessid(struct net_device *dev,
3049                                   struct iw_request_info *info,
3050                                   struct iw_point *erq,
3051                                   char *essidbuf)
3052 {
3053         struct orinoco_private *priv = netdev_priv(dev);
3054         int active;
3055         int err = 0;
3056         unsigned long flags;
3057
3058         TRACE_ENTER(dev->name);
3059
3060         if (netif_running(dev)) {
3061                 err = orinoco_hw_get_essid(priv, &active, essidbuf);
3062                 if (err)
3063                         return err;
3064         } else {
3065                 if (orinoco_lock(priv, &flags) != 0)
3066                         return -EBUSY;
3067                 memcpy(essidbuf, priv->desired_essid, IW_ESSID_MAX_SIZE + 1);
3068                 orinoco_unlock(priv, &flags);
3069         }
3070
3071         erq->flags = 1;
3072         erq->length = strlen(essidbuf) + 1;
3073
3074         TRACE_EXIT(dev->name);
3075         
3076         return 0;
3077 }
3078
3079 static int orinoco_ioctl_setnick(struct net_device *dev,
3080                                  struct iw_request_info *info,
3081                                  struct iw_point *nrq,
3082                                  char *nickbuf)
3083 {
3084         struct orinoco_private *priv = netdev_priv(dev);
3085         unsigned long flags;
3086
3087         if (nrq->length > IW_ESSID_MAX_SIZE)
3088                 return -E2BIG;
3089
3090         if (orinoco_lock(priv, &flags) != 0)
3091                 return -EBUSY;
3092
3093         memset(priv->nick, 0, sizeof(priv->nick));
3094         memcpy(priv->nick, nickbuf, nrq->length);
3095
3096         orinoco_unlock(priv, &flags);
3097
3098         return -EINPROGRESS;            /* Call commit handler */
3099 }
3100
3101 static int orinoco_ioctl_getnick(struct net_device *dev,
3102                                  struct iw_request_info *info,
3103                                  struct iw_point *nrq,
3104                                  char *nickbuf)
3105 {
3106         struct orinoco_private *priv = netdev_priv(dev);
3107         unsigned long flags;
3108
3109         if (orinoco_lock(priv, &flags) != 0)
3110                 return -EBUSY;
3111
3112         memcpy(nickbuf, priv->nick, IW_ESSID_MAX_SIZE+1);
3113         orinoco_unlock(priv, &flags);
3114
3115         nrq->length = strlen(nickbuf)+1;
3116
3117         return 0;
3118 }
3119
3120 static int orinoco_ioctl_setfreq(struct net_device *dev,
3121                                  struct iw_request_info *info,
3122                                  struct iw_freq *frq,
3123                                  char *extra)
3124 {
3125         struct orinoco_private *priv = netdev_priv(dev);
3126         int chan = -1;
3127         unsigned long flags;
3128         int err = -EINPROGRESS;         /* Call commit handler */
3129
3130         /* In infrastructure mode the AP sets the channel */
3131         if (priv->iw_mode == IW_MODE_INFRA)
3132                 return -EBUSY;
3133
3134         if ( (frq->e == 0) && (frq->m <= 1000) ) {
3135                 /* Setting by channel number */
3136                 chan = frq->m;
3137         } else {
3138                 /* Setting by frequency - search the table */
3139                 int mult = 1;
3140                 int i;
3141
3142                 for (i = 0; i < (6 - frq->e); i++)
3143                         mult *= 10;
3144
3145                 for (i = 0; i < NUM_CHANNELS; i++)
3146                         if (frq->m == (channel_frequency[i] * mult))
3147                                 chan = i+1;
3148         }
3149
3150         if ( (chan < 1) || (chan > NUM_CHANNELS) ||
3151              ! (priv->channel_mask & (1 << (chan-1)) ) )
3152                 return -EINVAL;
3153
3154         if (orinoco_lock(priv, &flags) != 0)
3155                 return -EBUSY;
3156
3157         priv->channel = chan;
3158         if (priv->iw_mode == IW_MODE_MONITOR) {
3159                 /* Fast channel change - no commit if successful */
3160                 hermes_t *hw = &priv->hw;
3161                 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
3162                                             HERMES_TEST_SET_CHANNEL,
3163                                         chan, NULL);
3164         }
3165         orinoco_unlock(priv, &flags);
3166
3167         return err;
3168 }
3169
3170 static int orinoco_ioctl_getfreq(struct net_device *dev,
3171                                  struct iw_request_info *info,
3172                                  struct iw_freq *frq,
3173                                  char *extra)
3174 {
3175         struct orinoco_private *priv = netdev_priv(dev);
3176         int tmp;
3177
3178         /* Locking done in there */
3179         tmp = orinoco_hw_get_freq(priv);
3180         if (tmp < 0) {
3181                 return tmp;
3182         }
3183
3184         frq->m = tmp;
3185         frq->e = 1;
3186
3187         return 0;
3188 }
3189
3190 static int orinoco_ioctl_getsens(struct net_device *dev,
3191                                  struct iw_request_info *info,
3192                                  struct iw_param *srq,
3193                                  char *extra)
3194 {
3195         struct orinoco_private *priv = netdev_priv(dev);
3196         hermes_t *hw = &priv->hw;
3197         u16 val;
3198         int err;
3199         unsigned long flags;
3200
3201         if (!priv->has_sensitivity)
3202                 return -EOPNOTSUPP;
3203
3204         if (orinoco_lock(priv, &flags) != 0)
3205                 return -EBUSY;
3206         err = hermes_read_wordrec(hw, USER_BAP,
3207                                   HERMES_RID_CNFSYSTEMSCALE, &val);
3208         orinoco_unlock(priv, &flags);
3209
3210         if (err)
3211                 return err;
3212
3213         srq->value = val;
3214         srq->fixed = 0; /* auto */
3215
3216         return 0;
3217 }
3218
3219 static int orinoco_ioctl_setsens(struct net_device *dev,
3220                                  struct iw_request_info *info,
3221                                  struct iw_param *srq,
3222                                  char *extra)
3223 {
3224         struct orinoco_private *priv = netdev_priv(dev);
3225         int val = srq->value;
3226         unsigned long flags;
3227
3228         if (!priv->has_sensitivity)
3229                 return -EOPNOTSUPP;
3230
3231         if ((val < 1) || (val > 3))
3232                 return -EINVAL;
3233         
3234         if (orinoco_lock(priv, &flags) != 0)
3235                 return -EBUSY;
3236         priv->ap_density = val;
3237         orinoco_unlock(priv, &flags);
3238
3239         return -EINPROGRESS;            /* Call commit handler */
3240 }
3241
3242 static int orinoco_ioctl_setrts(struct net_device *dev,
3243                                 struct iw_request_info *info,
3244                                 struct iw_param *rrq,
3245                                 char *extra)
3246 {
3247         struct orinoco_private *priv = netdev_priv(dev);
3248         int val = rrq->value;
3249         unsigned long flags;
3250
3251         if (rrq->disabled)
3252                 val = 2347;
3253
3254         if ( (val < 0) || (val > 2347) )
3255                 return -EINVAL;
3256
3257         if (orinoco_lock(priv, &flags) != 0)
3258                 return -EBUSY;
3259
3260         priv->rts_thresh = val;
3261         orinoco_unlock(priv, &flags);
3262
3263         return -EINPROGRESS;            /* Call commit handler */
3264 }
3265
3266 static int orinoco_ioctl_getrts(struct net_device *dev,
3267                                 struct iw_request_info *info,
3268                                 struct iw_param *rrq,
3269                                 char *extra)
3270 {
3271         struct orinoco_private *priv = netdev_priv(dev);
3272
3273         rrq->value = priv->rts_thresh;
3274         rrq->disabled = (rrq->value == 2347);
3275         rrq->fixed = 1;
3276
3277         return 0;
3278 }
3279
3280 static int orinoco_ioctl_setfrag(struct net_device *dev,
3281                                  struct iw_request_info *info,
3282                                  struct iw_param *frq,
3283                                  char *extra)
3284 {
3285         struct orinoco_private *priv = netdev_priv(dev);
3286         int err = -EINPROGRESS;         /* Call commit handler */
3287         unsigned long flags;
3288
3289         if (orinoco_lock(priv, &flags) != 0)
3290                 return -EBUSY;
3291
3292         if (priv->has_mwo) {
3293                 if (frq->disabled)
3294                         priv->mwo_robust = 0;
3295                 else {
3296                         if (frq->fixed)
3297                                 printk(KERN_WARNING "%s: Fixed fragmentation is "
3298                                        "not supported on this firmware. "
3299                                        "Using MWO robust instead.\n", dev->name);
3300                         priv->mwo_robust = 1;
3301                 }
3302         } else {
3303                 if (frq->disabled)
3304                         priv->frag_thresh = 2346;
3305                 else {
3306                         if ( (frq->value < 256) || (frq->value > 2346) )
3307                                 err = -EINVAL;
3308                         else
3309                                 priv->frag_thresh = frq->value & ~0x1; /* must be even */
3310                 }
3311         }
3312
3313         orinoco_unlock(priv, &flags);
3314
3315         return err;
3316 }
3317
3318 static int orinoco_ioctl_getfrag(struct net_device *dev,
3319                                  struct iw_request_info *info,
3320                                  struct iw_param *frq,
3321                                  char *extra)
3322 {
3323         struct orinoco_private *priv = netdev_priv(dev);
3324         hermes_t *hw = &priv->hw;
3325         int err;
3326         u16 val;
3327         unsigned long flags;
3328
3329         if (orinoco_lock(priv, &flags) != 0)
3330                 return -EBUSY;
3331         
3332         if (priv->has_mwo) {
3333                 err = hermes_read_wordrec(hw, USER_BAP,
3334                                           HERMES_RID_CNFMWOROBUST_AGERE,
3335                                           &val);
3336                 if (err)
3337                         val = 0;
3338
3339                 frq->value = val ? 2347 : 0;
3340                 frq->disabled = ! val;
3341                 frq->fixed = 0;
3342         } else {
3343                 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
3344                                           &val);
3345                 if (err)
3346                         val = 0;
3347
3348                 frq->value = val;
3349                 frq->disabled = (val >= 2346);
3350                 frq->fixed = 1;
3351         }
3352
3353         orinoco_unlock(priv, &flags);
3354         
3355         return err;
3356 }
3357
3358 static int orinoco_ioctl_setrate(struct net_device *dev,
3359                                  struct iw_request_info *info,
3360                                  struct iw_param *rrq,
3361                                  char *extra)
3362 {
3363         struct orinoco_private *priv = netdev_priv(dev);
3364         int ratemode = -1;
3365         int bitrate; /* 100s of kilobits */
3366         int i;
3367         unsigned long flags;
3368         
3369         /* As the user space doesn't know our highest rate, it uses -1
3370          * to ask us to set the highest rate.  Test it using "iwconfig
3371          * ethX rate auto" - Jean II */
3372         if (rrq->value == -1)
3373                 bitrate = 110;
3374         else {
3375                 if (rrq->value % 100000)
3376                         return -EINVAL;
3377                 bitrate = rrq->value / 100000;
3378         }
3379
3380         if ( (bitrate != 10) && (bitrate != 20) &&
3381              (bitrate != 55) && (bitrate != 110) )
3382                 return -EINVAL;
3383
3384         for (i = 0; i < BITRATE_TABLE_SIZE; i++)
3385                 if ( (bitrate_table[i].bitrate == bitrate) &&
3386                      (bitrate_table[i].automatic == ! rrq->fixed) ) {
3387                         ratemode = i;
3388                         break;
3389                 }
3390         
3391         if (ratemode == -1)
3392                 return -EINVAL;
3393
3394         if (orinoco_lock(priv, &flags) != 0)
3395                 return -EBUSY;
3396         priv->bitratemode = ratemode;
3397         orinoco_unlock(priv, &flags);
3398
3399         return -EINPROGRESS;
3400 }
3401
3402 static int orinoco_ioctl_getrate(struct net_device *dev,
3403                                  struct iw_request_info *info,
3404                                  struct iw_param *rrq,
3405                                  char *extra)
3406 {
3407         struct orinoco_private *priv = netdev_priv(dev);
3408         hermes_t *hw = &priv->hw;
3409         int err = 0;
3410         int ratemode;
3411         int i;
3412         u16 val;
3413         unsigned long flags;
3414
3415         if (orinoco_lock(priv, &flags) != 0)
3416                 return -EBUSY;
3417
3418         ratemode = priv->bitratemode;
3419
3420         BUG_ON((ratemode < 0) || (ratemode >= BITRATE_TABLE_SIZE));
3421
3422         rrq->value = bitrate_table[ratemode].bitrate * 100000;
3423         rrq->fixed = ! bitrate_table[ratemode].automatic;
3424         rrq->disabled = 0;
3425
3426         /* If the interface is running we try to find more about the
3427            current mode */
3428         if (netif_running(dev)) {
3429                 err = hermes_read_wordrec(hw, USER_BAP,
3430                                           HERMES_RID_CURRENTTXRATE, &val);
3431                 if (err)
3432                         goto out;
3433                 
3434                 switch (priv->firmware_type) {
3435                 case FIRMWARE_TYPE_AGERE: /* Lucent style rate */
3436                         /* Note : in Lucent firmware, the return value of
3437                          * HERMES_RID_CURRENTTXRATE is the bitrate in Mb/s,
3438                          * and therefore is totally different from the
3439                          * encoding of HERMES_RID_CNFTXRATECONTROL.
3440                          * Don't forget that 6Mb/s is really 5.5Mb/s */
3441                         if (val == 6)
3442                                 rrq->value = 5500000;
3443                         else
3444                                 rrq->value = val * 1000000;
3445                         break;
3446                 case FIRMWARE_TYPE_INTERSIL: /* Intersil style rate */
3447                 case FIRMWARE_TYPE_SYMBOL: /* Symbol style rate */
3448                         for (i = 0; i < BITRATE_TABLE_SIZE; i++)
3449                                 if (bitrate_table[i].intersil_txratectrl == val) {
3450                                         ratemode = i;
3451                                         break;
3452                                 }
3453                         if (i >= BITRATE_TABLE_SIZE)
3454                                 printk(KERN_INFO "%s: Unable to determine current bitrate (0x%04hx)\n",
3455                                        dev->name, val);
3456
3457                         rrq->value = bitrate_table[ratemode].bitrate * 100000;
3458                         break;
3459                 default:
3460                         BUG();
3461                 }
3462         }
3463
3464  out:
3465         orinoco_unlock(priv, &flags);
3466
3467         return err;
3468 }
3469
3470 static int orinoco_ioctl_setpower(struct net_device *dev,
3471                                   struct iw_request_info *info,
3472                                   struct iw_param *prq,
3473                                   char *extra)
3474 {
3475         struct orinoco_private *priv = netdev_priv(dev);
3476         int err = -EINPROGRESS;         /* Call commit handler */
3477         unsigned long flags;
3478
3479         if (orinoco_lock(priv, &flags) != 0)
3480                 return -EBUSY;
3481
3482         if (prq->disabled) {
3483                 priv->pm_on = 0;
3484         } else {
3485                 switch (prq->flags & IW_POWER_MODE) {
3486                 case IW_POWER_UNICAST_R:
3487                         priv->pm_mcast = 0;
3488                         priv->pm_on = 1;
3489                         break;
3490                 case IW_POWER_ALL_R:
3491                         priv->pm_mcast = 1;
3492                         priv->pm_on = 1;
3493                         break;
3494                 case IW_POWER_ON:
3495                         /* No flags : but we may have a value - Jean II */
3496                         break;
3497                 default:
3498                         err = -EINVAL;
3499                 }
3500                 if (err)
3501                         goto out;
3502                 
3503                 if (prq->flags & IW_POWER_TIMEOUT) {
3504                         priv->pm_on = 1;
3505                         priv->pm_timeout = prq->value / 1000;
3506                 }
3507                 if (prq->flags & IW_POWER_PERIOD) {
3508                         priv->pm_on = 1;
3509                         priv->pm_period = prq->value / 1000;
3510                 }
3511                 /* It's valid to not have a value if we are just toggling
3512                  * the flags... Jean II */
3513                 if(!priv->pm_on) {
3514                         err = -EINVAL;
3515                         goto out;
3516                 }                       
3517         }
3518
3519  out:
3520         orinoco_unlock(priv, &flags);
3521
3522         return err;
3523 }
3524
3525 static int orinoco_ioctl_getpower(struct net_device *dev,
3526                                   struct iw_request_info *info,
3527                                   struct iw_param *prq,
3528                                   char *extra)
3529 {
3530         struct orinoco_private *priv = netdev_priv(dev);
3531         hermes_t *hw = &priv->hw;
3532         int err = 0;
3533         u16 enable, period, timeout, mcast;
3534         unsigned long flags;
3535
3536         if (orinoco_lock(priv, &flags) != 0)
3537                 return -EBUSY;
3538         
3539         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFPMENABLED, &enable);
3540         if (err)
3541                 goto out;
3542
3543         err = hermes_read_wordrec(hw, USER_BAP,
3544                                   HERMES_RID_CNFMAXSLEEPDURATION, &period);
3545         if (err)
3546                 goto out;
3547
3548         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFPMHOLDOVERDURATION, &timeout);
3549         if (err)
3550                 goto out;
3551
3552         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFMULTICASTRECEIVE, &mcast);
3553         if (err)
3554                 goto out;
3555
3556         prq->disabled = !enable;
3557         /* Note : by default, display the period */
3558         if ((prq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
3559                 prq->flags = IW_POWER_TIMEOUT;
3560                 prq->value = timeout * 1000;
3561         } else {
3562                 prq->flags = IW_POWER_PERIOD;
3563                 prq->value = period * 1000;
3564         }
3565         if (mcast)
3566                 prq->flags |= IW_POWER_ALL_R;
3567         else
3568                 prq->flags |= IW_POWER_UNICAST_R;
3569
3570  out:
3571         orinoco_unlock(priv, &flags);
3572
3573         return err;
3574 }
3575
3576 static int orinoco_ioctl_getretry(struct net_device *dev,
3577                                   struct iw_request_info *info,
3578                                   struct iw_param *rrq,
3579                                   char *extra)
3580 {
3581         struct orinoco_private *priv = netdev_priv(dev);
3582         hermes_t *hw = &priv->hw;
3583         int err = 0;
3584         u16 short_limit, long_limit, lifetime;
3585         unsigned long flags;
3586
3587         if (orinoco_lock(priv, &flags) != 0)
3588                 return -EBUSY;
3589         
3590         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_SHORTRETRYLIMIT,
3591                                   &short_limit);
3592         if (err)
3593                 goto out;
3594
3595         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_LONGRETRYLIMIT,
3596                                   &long_limit);
3597         if (err)
3598                 goto out;
3599
3600         err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_MAXTRANSMITLIFETIME,
3601                                   &lifetime);
3602         if (err)
3603                 goto out;
3604
3605         rrq->disabled = 0;              /* Can't be disabled */
3606
3607         /* Note : by default, display the retry number */
3608         if ((rrq->flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME) {
3609                 rrq->flags = IW_RETRY_LIFETIME;
3610                 rrq->value = lifetime * 1000;   /* ??? */
3611         } else {
3612                 /* By default, display the min number */
3613                 if ((rrq->flags & IW_RETRY_MAX)) {
3614                         rrq->flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
3615                         rrq->value = long_limit;
3616                 } else {
3617                         rrq->flags = IW_RETRY_LIMIT;
3618                         rrq->value = short_limit;
3619                         if(short_limit != long_limit)
3620                                 rrq->flags |= IW_RETRY_MIN;
3621                 }
3622         }
3623
3624  out:
3625         orinoco_unlock(priv, &flags);
3626
3627         return err;
3628 }
3629
3630 static int orinoco_ioctl_reset(struct net_device *dev,
3631                                struct iw_request_info *info,
3632                                void *wrqu,
3633                                char *extra)
3634 {
3635         struct orinoco_private *priv = netdev_priv(dev);
3636
3637         if (! capable(CAP_NET_ADMIN))
3638                 return -EPERM;
3639
3640         if (info->cmd == (SIOCIWFIRSTPRIV + 0x1)) {
3641                 printk(KERN_DEBUG "%s: Forcing reset!\n", dev->name);
3642
3643                 /* Firmware reset */
3644                 orinoco_reset(dev);
3645         } else {
3646                 printk(KERN_DEBUG "%s: Force scheduling reset!\n", dev->name);
3647
3648                 schedule_work(&priv->reset_work);
3649         }
3650
3651         return 0;
3652 }
3653
3654 static int orinoco_ioctl_setibssport(struct net_device *dev,
3655                                      struct iw_request_info *info,
3656                                      void *wrqu,
3657                                      char *extra)
3658
3659 {
3660         struct orinoco_private *priv = netdev_priv(dev);
3661         int val = *( (int *) extra );
3662         unsigned long flags;
3663
3664         if (orinoco_lock(priv, &flags) != 0)
3665                 return -EBUSY;
3666
3667         priv->ibss_port = val ;
3668
3669         /* Actually update the mode we are using */
3670         set_port_type(priv);
3671
3672         orinoco_unlock(priv, &flags);
3673         return -EINPROGRESS;            /* Call commit handler */
3674 }
3675
3676 static int orinoco_ioctl_getibssport(struct net_device *dev,
3677                                      struct iw_request_info *info,
3678                                      void *wrqu,
3679                                      char *extra)
3680 {
3681         struct orinoco_private *priv = netdev_priv(dev);
3682         int *val = (int *) extra;
3683
3684         *val = priv->ibss_port;
3685         return 0;
3686 }
3687
3688 static int orinoco_ioctl_setport3(struct net_device *dev,
3689                                   struct iw_request_info *info,
3690                                   void *wrqu,
3691                                   char *extra)
3692 {
3693         struct orinoco_private *priv = netdev_priv(dev);
3694         int val = *( (int *) extra );
3695         int err = 0;
3696         unsigned long flags;
3697
3698         if (orinoco_lock(priv, &flags) != 0)
3699                 return -EBUSY;
3700
3701         switch (val) {
3702         case 0: /* Try to do IEEE ad-hoc mode */
3703                 if (! priv->has_ibss) {
3704                         err = -EINVAL;
3705                         break;
3706                 }
3707                 priv->prefer_port3 = 0;
3708                         
3709                 break;
3710
3711         case 1: /* Try to do Lucent proprietary ad-hoc mode */
3712                 if (! priv->has_port3) {
3713                         err = -EINVAL;
3714                         break;
3715                 }
3716                 priv->prefer_port3 = 1;
3717                 break;
3718
3719         default:
3720                 err = -EINVAL;
3721         }
3722
3723         if (! err) {
3724                 /* Actually update the mode we are using */
3725                 set_port_type(priv);
3726                 err = -EINPROGRESS;
3727         }
3728
3729         orinoco_unlock(priv, &flags);
3730
3731         return err;
3732 }
3733
3734 static int orinoco_ioctl_getport3(struct net_device *dev,
3735                                   struct iw_request_info *info,
3736                                   void *wrqu,
3737                                   char *extra)
3738 {
3739         struct orinoco_private *priv = netdev_priv(dev);
3740         int *val = (int *) extra;
3741
3742         *val = priv->prefer_port3;
3743         return 0;
3744 }
3745
3746 static int orinoco_ioctl_setpreamble(struct net_device *dev,
3747                                      struct iw_request_info *info,
3748                                      void *wrqu,
3749                                      char *extra)
3750 {
3751         struct orinoco_private *priv = netdev_priv(dev);
3752         unsigned long flags;
3753         int val;
3754
3755         if (! priv->has_preamble)
3756                 return -EOPNOTSUPP;
3757
3758         /* 802.11b has recently defined some short preamble.
3759          * Basically, the Phy header has been reduced in size.
3760          * This increase performance, especially at high rates
3761          * (the preamble is transmitted at 1Mb/s), unfortunately
3762          * this give compatibility troubles... - Jean II */
3763         val = *( (int *) extra );
3764
3765         if (orinoco_lock(priv, &flags) != 0)
3766                 return -EBUSY;
3767
3768         if (val)
3769                 priv->preamble = 1;
3770         else
3771                 priv->preamble = 0;
3772
3773         orinoco_unlock(priv, &flags);
3774
3775         return -EINPROGRESS;            /* Call commit handler */
3776 }
3777
3778 static int orinoco_ioctl_getpreamble(struct net_device *dev,
3779                                      struct iw_request_info *info,
3780                                      void *wrqu,
3781                                      char *extra)
3782 {
3783         struct orinoco_private *priv = netdev_priv(dev);
3784         int *val = (int *) extra;
3785
3786         if (! priv->has_preamble)
3787                 return -EOPNOTSUPP;
3788
3789         *val = priv->preamble;
3790         return 0;
3791 }
3792
3793 /* ioctl interface to hermes_read_ltv()
3794  * To use with iwpriv, pass the RID as the token argument, e.g.
3795  * iwpriv get_rid [0xfc00]
3796  * At least Wireless Tools 25 is required to use iwpriv.
3797  * For Wireless Tools 25 and 26 append "dummy" are the end. */
3798 static int orinoco_ioctl_getrid(struct net_device *dev,
3799                                 struct iw_request_info *info,
3800                                 struct iw_point *data,
3801                                 char *extra)
3802 {
3803         struct orinoco_private *priv = netdev_priv(dev);
3804         hermes_t *hw = &priv->hw;
3805         int rid = data->flags;
3806         u16 length;
3807         int err;
3808         unsigned long flags;
3809
3810         /* It's a "get" function, but we don't want users to access the
3811          * WEP key and other raw firmware data */
3812         if (! capable(CAP_NET_ADMIN))
3813                 return -EPERM;
3814
3815         if (rid < 0xfc00 || rid > 0xffff)
3816                 return -EINVAL;
3817
3818         if (orinoco_lock(priv, &flags) != 0)
3819                 return -EBUSY;
3820
3821         err = hermes_read_ltv(hw, USER_BAP, rid, MAX_RID_LEN, &length,
3822                               extra);
3823         if (err)
3824                 goto out;
3825
3826         data->length = min_t(u16, HERMES_RECLEN_TO_BYTES(length),
3827                              MAX_RID_LEN);
3828
3829  out:
3830         orinoco_unlock(priv, &flags);
3831         return err;
3832 }
3833
3834 /* Trigger a scan (look for other cells in the vicinity */
3835 static int orinoco_ioctl_setscan(struct net_device *dev,
3836                                  struct iw_request_info *info,
3837                                  struct iw_param *srq,
3838                                  char *extra)
3839 {
3840         struct orinoco_private *priv = netdev_priv(dev);
3841         hermes_t *hw = &priv->hw;
3842         int err = 0;
3843         unsigned long flags;
3844
3845         /* Note : you may have realised that, as this is a SET operation,
3846          * this is priviledged and therefore a normal user can't
3847          * perform scanning.
3848          * This is not an error, while the device perform scanning,
3849          * traffic doesn't flow, so it's a perfect DoS...
3850          * Jean II */
3851
3852         if (orinoco_lock(priv, &flags) != 0)
3853                 return -EBUSY;
3854
3855         /* Scanning with port 0 disabled would fail */
3856         if (!netif_running(dev)) {
3857                 err = -ENETDOWN;
3858                 goto out;
3859         }
3860
3861         /* In monitor mode, the scan results are always empty.
3862          * Probe responses are passed to the driver as received
3863          * frames and could be processed in software. */
3864         if (priv->iw_mode == IW_MODE_MONITOR) {
3865                 err = -EOPNOTSUPP;
3866                 goto out;
3867         }
3868
3869         /* Note : because we don't lock out the irq handler, the way
3870          * we access scan variables in priv is critical.
3871          *      o scan_inprogress : not touched by irq handler
3872          *      o scan_mode : not touched by irq handler
3873          *      o scan_result : irq is strict producer, non-irq is strict
3874          *              consumer.
3875          *      o scan_len : synchronised with scan_result
3876          * Before modifying anything on those variables, please think hard !
3877          * Jean II */
3878
3879         /* If there is still some left-over scan results, get rid of it */
3880         if (priv->scan_result != NULL) {
3881                 /* What's likely is that a client did crash or was killed
3882                  * between triggering the scan request and reading the
3883                  * results, so we need to reset everything.
3884                  * Some clients that are too slow may suffer from that...
3885                  * Jean II */
3886                 kfree(priv->scan_result);
3887                 priv->scan_result = NULL;
3888         }
3889
3890         /* Save flags */
3891         priv->scan_mode = srq->flags;
3892
3893         /* Always trigger scanning, even if it's in progress.
3894          * This way, if the info frame get lost, we will recover somewhat
3895          * gracefully  - Jean II */
3896
3897         if (priv->has_hostscan) {
3898                 switch (priv->firmware_type) {
3899                 case FIRMWARE_TYPE_SYMBOL:
3900                         err = hermes_write_wordrec(hw, USER_BAP,
3901                                                    HERMES_RID_CNFHOSTSCAN_SYMBOL,
3902                                                    HERMES_HOSTSCAN_SYMBOL_ONCE |
3903                                                    HERMES_HOSTSCAN_SYMBOL_BCAST);
3904                         break;
3905                 case FIRMWARE_TYPE_INTERSIL: {
3906                         u16 req[3];
3907
3908                         req[0] = cpu_to_le16(0x3fff);   /* All channels */
3909                         req[1] = cpu_to_le16(0x0001);   /* rate 1 Mbps */
3910                         req[2] = 0;                     /* Any ESSID */
3911                         err = HERMES_WRITE_RECORD(hw, USER_BAP,
3912                                                   HERMES_RID_CNFHOSTSCAN, &req);
3913                 }
3914                 break;
3915                 case FIRMWARE_TYPE_AGERE:
3916                         err = hermes_write_wordrec(hw, USER_BAP,
3917                                                    HERMES_RID_CNFSCANSSID_AGERE,
3918                                                    0);  /* Any ESSID */
3919                         if (err)
3920                                 break;
3921
3922                         err = hermes_inquire(hw, HERMES_INQ_SCAN);
3923                         break;
3924                 }
3925         } else
3926                 err = hermes_inquire(hw, HERMES_INQ_SCAN);
3927
3928         /* One more client */
3929         if (! err)
3930                 priv->scan_inprogress = 1;
3931
3932  out:
3933         orinoco_unlock(priv, &flags);
3934         return err;
3935 }
3936
3937 /* Translate scan data returned from the card to a card independant
3938  * format that the Wireless Tools will understand - Jean II
3939  * Return message length or -errno for fatal errors */
3940 static inline int orinoco_translate_scan(struct net_device *dev,
3941                                          char *buffer,
3942                                          char *scan,
3943                                          int scan_len)
3944 {
3945         struct orinoco_private *priv = netdev_priv(dev);
3946         int                     offset;         /* In the scan data */
3947         union hermes_scan_info *atom;
3948         int                     atom_len;
3949         u16                     capabilities;
3950         u16                     channel;
3951         struct iw_event         iwe;            /* Temporary buffer */
3952         char *                  current_ev = buffer;
3953         char *                  end_buf = buffer + IW_SCAN_MAX_DATA;
3954
3955         switch (priv->firmware_type) {
3956         case FIRMWARE_TYPE_AGERE:
3957                 atom_len = sizeof(struct agere_scan_apinfo);
3958                 offset = 0;
3959                 break;
3960         case FIRMWARE_TYPE_SYMBOL:
3961                 /* Lack of documentation necessitates this hack.
3962                  * Different firmwares have 68 or 76 byte long atoms.
3963                  * We try modulo first.  If the length divides by both,
3964                  * we check what would be the channel in the second
3965                  * frame for a 68-byte atom.  76-byte atoms have 0 there.
3966                  * Valid channel cannot be 0.  */
3967                 if (scan_len % 76)
3968                         atom_len = 68;
3969                 else if (scan_len % 68)
3970                         atom_len = 76;
3971                 else if (scan_len >= 1292 && scan[68] == 0)
3972                         atom_len = 76;
3973                 else
3974                         atom_len = 68;
3975                 offset = 0;
3976                 break;
3977         case FIRMWARE_TYPE_INTERSIL:
3978                 offset = 4;
3979                 if (priv->has_hostscan) {
3980                         atom_len = le16_to_cpup((u16 *)scan);
3981                         /* Sanity check for atom_len */
3982                         if (atom_len < sizeof(struct prism2_scan_apinfo)) {
3983                                 printk(KERN_ERR "%s: Invalid atom_len in scan data: %d\n",
3984                                 dev->name, atom_len);
3985                                 return -EIO;
3986                         }
3987                 } else
3988                         atom_len = offsetof(struct prism2_scan_apinfo, atim);
3989                 break;
3990         default:
3991                 return -EOPNOTSUPP;
3992         }
3993
3994         /* Check that we got an whole number of atoms */
3995         if ((scan_len - offset) % atom_len) {
3996                 printk(KERN_ERR "%s: Unexpected scan data length %d, "
3997                        "atom_len %d, offset %d\n", dev->name, scan_len,
3998                        atom_len, offset);
3999                 return -EIO;
4000         }
4001
4002         /* Read the entries one by one */
4003         for (; offset + atom_len <= scan_len; offset += atom_len) {
4004                 /* Get next atom */
4005                 atom = (union hermes_scan_info *) (scan + offset);
4006
4007                 /* First entry *MUST* be the AP MAC address */
4008                 iwe.cmd = SIOCGIWAP;
4009                 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
4010                 memcpy(iwe.u.ap_addr.sa_data, atom->a.bssid, ETH_ALEN);
4011                 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_ADDR_LEN);
4012
4013                 /* Other entries will be displayed in the order we give them */
4014
4015                 /* Add the ESSID */
4016                 iwe.u.data.length = le16_to_cpu(atom->a.essid_len);
4017                 if (iwe.u.data.length > 32)
4018                         iwe.u.data.length = 32;
4019                 iwe.cmd = SIOCGIWESSID;
4020                 iwe.u.data.flags = 1;
4021                 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, atom->a.essid);
4022
4023                 /* Add mode */
4024                 iwe.cmd = SIOCGIWMODE;
4025                 capabilities = le16_to_cpu(atom->a.capabilities);
4026                 if (capabilities & 0x3) {
4027                         if (capabilities & 0x1)
4028                                 iwe.u.mode = IW_MODE_MASTER;
4029                         else
4030                                 iwe.u.mode = IW_MODE_ADHOC;
4031                         current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_UINT_LEN);
4032                 }
4033
4034                 channel = atom->s.channel;
4035                 if ( (channel >= 1) && (channel <= NUM_CHANNELS) ) {
4036                         /* Add frequency */
4037                         iwe.cmd = SIOCGIWFREQ;
4038                         iwe.u.freq.m = channel_frequency[channel-1] * 100000;
4039                         iwe.u.freq.e = 1;
4040                         current_ev = iwe_stream_add_event(current_ev, end_buf,
4041                                                           &iwe, IW_EV_FREQ_LEN);
4042                 }
4043
4044                 /* Add quality statistics */
4045                 iwe.cmd = IWEVQUAL;
4046                 iwe.u.qual.updated = 0x10;      /* no link quality */
4047                 iwe.u.qual.level = (__u8) le16_to_cpu(atom->a.level) - 0x95;
4048                 iwe.u.qual.noise = (__u8) le16_to_cpu(atom->a.noise) - 0x95;
4049                 /* Wireless tools prior to 27.pre22 will show link quality
4050                  * anyway, so we provide a reasonable value. */
4051                 if (iwe.u.qual.level > iwe.u.qual.noise)
4052                         iwe.u.qual.qual = iwe.u.qual.level - iwe.u.qual.noise;
4053                 else
4054                         iwe.u.qual.qual = 0;
4055                 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_QUAL_LEN);
4056
4057                 /* Add encryption capability */
4058                 iwe.cmd = SIOCGIWENCODE;
4059                 if (capabilities & 0x10)
4060                         iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
4061                 else
4062                         iwe.u.data.flags = IW_ENCODE_DISABLED;
4063                 iwe.u.data.length = 0;
4064                 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, atom->a.essid);
4065
4066                 /* Bit rate is not available in Lucent/Agere firmwares */
4067                 if (priv->firmware_type != FIRMWARE_TYPE_AGERE) {
4068                         char *  current_val = current_ev + IW_EV_LCP_LEN;
4069                         int     i;
4070                         int     step;
4071
4072                         if (priv->firmware_type == FIRMWARE_TYPE_SYMBOL)
4073                                 step = 2;
4074                         else
4075                                 step = 1;
4076
4077                         iwe.cmd = SIOCGIWRATE;
4078                         /* Those two flags are ignored... */
4079                         iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
4080                         /* Max 10 values */
4081                         for (i = 0; i < 10; i += step) {
4082                                 /* NULL terminated */
4083                                 if (atom->p.rates[i] == 0x0)
4084                                         break;
4085                                 /* Bit rate given in 500 kb/s units (+ 0x80) */
4086                                 iwe.u.bitrate.value = ((atom->p.rates[i] & 0x7f) * 500000);
4087                                 current_val = iwe_stream_add_value(current_ev, current_val,
4088                                                                    end_buf, &iwe,
4089                                                                    IW_EV_PARAM_LEN);
4090                         }
4091                         /* Check if we added any event */
4092                         if ((current_val - current_ev) > IW_EV_LCP_LEN)
4093                                 current_ev = current_val;
4094                 }
4095
4096                 /* The other data in the scan result are not really
4097                  * interesting, so for now drop it - Jean II */
4098         }
4099         return current_ev - buffer;
4100 }
4101
4102 /* Return results of a scan */
4103 static int orinoco_ioctl_getscan(struct net_device *dev,
4104                                  struct iw_request_info *info,
4105                                  struct iw_point *srq,
4106                                  char *extra)
4107 {
4108         struct orinoco_private *priv = netdev_priv(dev);
4109         int err = 0;
4110         unsigned long flags;
4111
4112         if (orinoco_lock(priv, &flags) != 0)
4113                 return -EBUSY;
4114
4115         /* If no results yet, ask to try again later */
4116         if (priv->scan_result == NULL) {
4117                 if (priv->scan_inprogress)
4118                         /* Important note : we don't want to block the caller
4119                          * until results are ready for various reasons.
4120                          * First, managing wait queues is complex and racy.
4121                          * Second, we grab some rtnetlink lock before comming
4122                          * here (in dev_ioctl()).
4123                          * Third, we generate an Wireless Event, so the
4124                          * caller can wait itself on that - Jean II */
4125                         err = -EAGAIN;
4126                 else
4127                         /* Client error, no scan results...
4128                          * The caller need to restart the scan. */
4129                         err = -ENODATA;
4130         } else {
4131                 /* We have some results to push back to user space */
4132
4133                 /* Translate to WE format */
4134                 int ret = orinoco_translate_scan(dev, extra,
4135                                                  priv->scan_result,
4136                                                  priv->scan_len);
4137
4138                 if (ret < 0) {
4139                         err = ret;
4140                         kfree(priv->scan_result);
4141                         priv->scan_result = NULL;
4142                 } else {
4143                         srq->length = ret;
4144
4145                         /* Return flags */
4146                         srq->flags = (__u16) priv->scan_mode;
4147
4148                         /* In any case, Scan results will be cleaned up in the
4149                          * reset function and when exiting the driver.
4150                          * The person triggering the scanning may never come to
4151                          * pick the results, so we need to do it in those places.
4152                          * Jean II */
4153
4154 #ifdef SCAN_SINGLE_READ
4155                         /* If you enable this option, only one client (the first
4156                          * one) will be able to read the result (and only one
4157                          * time). If there is multiple concurent clients that
4158                          * want to read scan results, this behavior is not
4159                          * advisable - Jean II */
4160                         kfree(priv->scan_result);
4161                         priv->scan_result = NULL;
4162 #endif /* SCAN_SINGLE_READ */
4163                         /* Here, if too much time has elapsed since last scan,
4164                          * we may want to clean up scan results... - Jean II */
4165                 }
4166
4167                 /* Scan is no longer in progress */
4168                 priv->scan_inprogress = 0;
4169         }
4170           
4171         orinoco_unlock(priv, &flags);
4172         return err;
4173 }
4174
4175 /* Commit handler, called after set operations */
4176 static int orinoco_ioctl_commit(struct net_device *dev,
4177                                 struct iw_request_info *info,
4178                                 void *wrqu,
4179                                 char *extra)
4180 {
4181         struct orinoco_private *priv = netdev_priv(dev);
4182         struct hermes *hw = &priv->hw;
4183         unsigned long flags;
4184         int err = 0;
4185
4186         if (!priv->open)
4187                 return 0;
4188
4189         if (priv->broken_disableport) {
4190                 orinoco_reset(dev);
4191                 return 0;
4192         }
4193
4194         if (orinoco_lock(priv, &flags) != 0)
4195                 return err;
4196
4197         err = hermes_disable_port(hw, 0);
4198         if (err) {
4199                 printk(KERN_WARNING "%s: Unable to disable port "
4200                        "while reconfiguring card\n", dev->name);
4201                 priv->broken_disableport = 1;
4202                 goto out;
4203         }
4204
4205         err = __orinoco_program_rids(dev);
4206         if (err) {
4207                 printk(KERN_WARNING "%s: Unable to reconfigure card\n",
4208                        dev->name);
4209                 goto out;
4210         }
4211
4212         err = hermes_enable_port(hw, 0);
4213         if (err) {
4214                 printk(KERN_WARNING "%s: Unable to enable port while reconfiguring card\n",
4215                        dev->name);
4216                 goto out;
4217         }
4218
4219  out:
4220         if (err) {
4221                 printk(KERN_WARNING "%s: Resetting instead...\n", dev->name);
4222                 schedule_work(&priv->reset_work);
4223                 err = 0;
4224         }
4225
4226         orinoco_unlock(priv, &flags);
4227         return err;
4228 }
4229
4230 static const struct iw_priv_args orinoco_privtab[] = {
4231         { SIOCIWFIRSTPRIV + 0x0, 0, 0, "force_reset" },
4232         { SIOCIWFIRSTPRIV + 0x1, 0, 0, "card_reset" },
4233         { SIOCIWFIRSTPRIV + 0x2, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
4234           0, "set_port3" },
4235         { SIOCIWFIRSTPRIV + 0x3, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
4236           "get_port3" },
4237         { SIOCIWFIRSTPRIV + 0x4, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
4238           0, "set_preamble" },
4239         { SIOCIWFIRSTPRIV + 0x5, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
4240           "get_preamble" },
4241         { SIOCIWFIRSTPRIV + 0x6, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
4242           0, "set_ibssport" },
4243         { SIOCIWFIRSTPRIV + 0x7, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
4244           "get_ibssport" },
4245         { SIOCIWFIRSTPRIV + 0x9, 0, IW_PRIV_TYPE_BYTE | MAX_RID_LEN,
4246           "get_rid" },
4247 };
4248
4249
4250 /*
4251  * Structures to export the Wireless Handlers
4252  */
4253
4254 static const iw_handler orinoco_handler[] = {
4255         [SIOCSIWCOMMIT-SIOCIWFIRST] = (iw_handler) orinoco_ioctl_commit,
4256         [SIOCGIWNAME  -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getname,
4257         [SIOCSIWFREQ  -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setfreq,
4258         [SIOCGIWFREQ  -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getfreq,
4259         [SIOCSIWMODE  -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setmode,
4260         [SIOCGIWMODE  -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getmode,
4261         [SIOCSIWSENS  -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setsens,
4262         [SIOCGIWSENS  -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getsens,
4263         [SIOCGIWRANGE -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getiwrange,
4264         [SIOCSIWSPY   -SIOCIWFIRST] = (iw_handler) iw_handler_set_spy,
4265         [SIOCGIWSPY   -SIOCIWFIRST] = (iw_handler) iw_handler_get_spy,
4266         [SIOCSIWTHRSPY-SIOCIWFIRST] = (iw_handler) iw_handler_set_thrspy,
4267         [SIOCGIWTHRSPY-SIOCIWFIRST] = (iw_handler) iw_handler_get_thrspy,
4268         [SIOCSIWAP    -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setwap,
4269         [SIOCGIWAP    -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getwap,
4270         [SIOCSIWSCAN  -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setscan,
4271         [SIOCGIWSCAN  -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getscan,
4272         [SIOCSIWESSID -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setessid,
4273         [SIOCGIWESSID -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getessid,
4274         [SIOCSIWNICKN -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setnick,
4275         [SIOCGIWNICKN -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getnick,
4276         [SIOCSIWRATE  -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setrate,
4277         [SIOCGIWRATE  -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getrate,
4278         [SIOCSIWRTS   -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setrts,
4279         [SIOCGIWRTS   -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getrts,
4280         [SIOCSIWFRAG  -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setfrag,
4281         [SIOCGIWFRAG  -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getfrag,
4282         [SIOCGIWRETRY -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getretry,
4283         [SIOCSIWENCODE-SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setiwencode,
4284         [SIOCGIWENCODE-SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getiwencode,
4285         [SIOCSIWPOWER -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setpower,
4286         [SIOCGIWPOWER -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getpower,
4287 };
4288
4289
4290 /*
4291   Added typecasting since we no longer use iwreq_data -- Moustafa
4292  */
4293 static const iw_handler orinoco_private_handler[] = {
4294         [0] = (iw_handler) orinoco_ioctl_reset,
4295         [1] = (iw_handler) orinoco_ioctl_reset,
4296         [2] = (iw_handler) orinoco_ioctl_setport3,
4297         [3] = (iw_handler) orinoco_ioctl_getport3,
4298         [4] = (iw_handler) orinoco_ioctl_setpreamble,
4299         [5] = (iw_handler) orinoco_ioctl_getpreamble,
4300         [6] = (iw_handler) orinoco_ioctl_setibssport,
4301         [7] = (iw_handler) orinoco_ioctl_getibssport,
4302         [9] = (iw_handler) orinoco_ioctl_getrid,
4303 };
4304
4305 static const struct iw_handler_def orinoco_handler_def = {
4306         .num_standard = ARRAY_SIZE(orinoco_handler),
4307         .num_private = ARRAY_SIZE(orinoco_private_handler),
4308         .num_private_args = ARRAY_SIZE(orinoco_privtab),
4309         .standard = orinoco_handler,
4310         .private = orinoco_private_handler,
4311         .private_args = orinoco_privtab,
4312         .get_wireless_stats = orinoco_get_wireless_stats,
4313 };
4314
4315 static void orinoco_get_drvinfo(struct net_device *dev,
4316                                 struct ethtool_drvinfo *info)
4317 {
4318         struct orinoco_private *priv = netdev_priv(dev);
4319
4320         strncpy(info->driver, DRIVER_NAME, sizeof(info->driver) - 1);
4321         strncpy(info->version, DRIVER_VERSION, sizeof(info->version) - 1);
4322         strncpy(info->fw_version, priv->fw_name, sizeof(info->fw_version) - 1);
4323         if (dev->class_dev.dev)
4324                 strncpy(info->bus_info, dev->class_dev.dev->bus_id,
4325                         sizeof(info->bus_info) - 1);
4326         else
4327                 snprintf(info->bus_info, sizeof(info->bus_info) - 1,
4328                          "PCMCIA %p", priv->hw.iobase);
4329 }
4330
4331 static struct ethtool_ops orinoco_ethtool_ops = {
4332         .get_drvinfo = orinoco_get_drvinfo,
4333         .get_link = ethtool_op_get_link,
4334 };
4335
4336 /********************************************************************/
4337 /* Debugging                                                        */
4338 /********************************************************************/
4339
4340 #if 0
4341 static void show_rx_frame(struct orinoco_rxframe_hdr *frame)
4342 {
4343         printk(KERN_DEBUG "RX descriptor:\n");
4344         printk(KERN_DEBUG "  status      = 0x%04x\n", frame->desc.status);
4345         printk(KERN_DEBUG "  time        = 0x%08x\n", frame->desc.time);
4346         printk(KERN_DEBUG "  silence     = 0x%02x\n", frame->desc.silence);
4347         printk(KERN_DEBUG "  signal      = 0x%02x\n", frame->desc.signal);
4348         printk(KERN_DEBUG "  rate        = 0x%02x\n", frame->desc.rate);
4349         printk(KERN_DEBUG "  rxflow      = 0x%02x\n", frame->desc.rxflow);
4350         printk(KERN_DEBUG "  reserved    = 0x%08x\n", frame->desc.reserved);
4351
4352         printk(KERN_DEBUG "IEEE 802.11 header:\n");
4353         printk(KERN_DEBUG "  frame_ctl   = 0x%04x\n",
4354                frame->p80211.frame_ctl);
4355         printk(KERN_DEBUG "  duration_id = 0x%04x\n",
4356                frame->p80211.duration_id);
4357         printk(KERN_DEBUG "  addr1       = %02x:%02x:%02x:%02x:%02x:%02x\n",
4358                frame->p80211.addr1[0], frame->p80211.addr1[1],
4359                frame->p80211.addr1[2], frame->p80211.addr1[3],
4360                frame->p80211.addr1[4], frame->p80211.addr1[5]);
4361         printk(KERN_DEBUG "  addr2       = %02x:%02x:%02x:%02x:%02x:%02x\n",
4362                frame->p80211.addr2[0], frame->p80211.addr2[1],
4363                frame->p80211.addr2[2], frame->p80211.addr2[3],
4364                frame->p80211.addr2[4], frame->p80211.addr2[5]);
4365         printk(KERN_DEBUG "  addr3       = %02x:%02x:%02x:%02x:%02x:%02x\n",
4366                frame->p80211.addr3[0], frame->p80211.addr3[1],
4367                frame->p80211.addr3[2], frame->p80211.addr3[3],
4368                frame->p80211.addr3[4], frame->p80211.addr3[5]);
4369         printk(KERN_DEBUG "  seq_ctl     = 0x%04x\n",
4370                frame->p80211.seq_ctl);
4371         printk(KERN_DEBUG "  addr4       = %02x:%02x:%02x:%02x:%02x:%02x\n",
4372                frame->p80211.addr4[0], frame->p80211.addr4[1],
4373                frame->p80211.addr4[2], frame->p80211.addr4[3],
4374                frame->p80211.addr4[4], frame->p80211.addr4[5]);
4375         printk(KERN_DEBUG "  data_len    = 0x%04x\n",
4376                frame->p80211.data_len);
4377
4378         printk(KERN_DEBUG "IEEE 802.3 header:\n");
4379         printk(KERN_DEBUG "  dest        = %02x:%02x:%02x:%02x:%02x:%02x\n",
4380                frame->p8023.h_dest[0], frame->p8023.h_dest[1],
4381                frame->p8023.h_dest[2], frame->p8023.h_dest[3],
4382                frame->p8023.h_dest[4], frame->p8023.h_dest[5]);
4383         printk(KERN_DEBUG "  src         = %02x:%02x:%02x:%02x:%02x:%02x\n",
4384                frame->p8023.h_source[0], frame->p8023.h_source[1],
4385                frame->p8023.h_source[2], frame->p8023.h_source[3],
4386                frame->p8023.h_source[4], frame->p8023.h_source[5]);
4387         printk(KERN_DEBUG "  len         = 0x%04x\n", frame->p8023.h_proto);
4388
4389         printk(KERN_DEBUG "IEEE 802.2 LLC/SNAP header:\n");
4390         printk(KERN_DEBUG "  DSAP        = 0x%02x\n", frame->p8022.dsap);
4391         printk(KERN_DEBUG "  SSAP        = 0x%02x\n", frame->p8022.ssap);
4392         printk(KERN_DEBUG "  ctrl        = 0x%02x\n", frame->p8022.ctrl);
4393         printk(KERN_DEBUG "  OUI         = %02x:%02x:%02x\n",
4394                frame->p8022.oui[0], frame->p8022.oui[1], frame->p8022.oui[2]);
4395         printk(KERN_DEBUG "  ethertype  = 0x%04x\n", frame->ethertype);
4396 }
4397 #endif /* 0 */
4398
4399 /********************************************************************/
4400 /* Module initialization                                            */
4401 /********************************************************************/
4402
4403 EXPORT_SYMBOL(alloc_orinocodev);
4404 EXPORT_SYMBOL(free_orinocodev);
4405
4406 EXPORT_SYMBOL(__orinoco_up);
4407 EXPORT_SYMBOL(__orinoco_down);
4408 EXPORT_SYMBOL(orinoco_reinit_firmware);
4409
4410 EXPORT_SYMBOL(orinoco_interrupt);
4411
4412 /* Can't be declared "const" or the whole __initdata section will
4413  * become const */
4414 static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
4415         " (David Gibson <hermes@gibson.dropbear.id.au>, "
4416         "Pavel Roskin <proski@gnu.org>, et al)";
4417
4418 static int __init init_orinoco(void)
4419 {
4420         printk(KERN_DEBUG "%s\n", version);
4421         return 0;
4422 }
4423
4424 static void __exit exit_orinoco(void)
4425 {
4426 }
4427
4428 module_init(init_orinoco);
4429 module_exit(exit_orinoco);