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