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