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