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