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