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