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