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