Fix compile on v2.6.30
[orinoco_usb.git] / drivers / net / wireless / orinoco.c
blob974c8ec39d924b323f04a2b9381bbc5d2c6dfbac
1 /* orinoco.c - (formerly known as dldwd_cs.c and orinoco_cs.c)
3 * A driver for Hermes or Prism 2 chipset based PCMCIA wireless
4 * adaptors, with Lucent/Agere, Intersil or Symbol firmware.
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>
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
16 * Based on dummy_cs.c 1.27 2000/06/12 21:27:25
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/
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/
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.
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.
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. */
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.
59 /* Locking and synchronization:
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.
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).
77 #define DRIVER_NAME "orinoco"
79 #include <linux/module.h>
80 #include <linux/kernel.h>
81 #include <linux/init.h>
82 #include <linux/netdevice.h>
83 #include <linux/etherdevice.h>
84 #include <linux/ethtool.h>
85 #include <linux/if_arp.h>
86 #include <linux/wireless.h>
87 #include <linux/ieee80211.h>
88 #include <net/iw_handler.h>
90 #include "hermes_rid.h"
91 #include "orinoco.h"
93 /********************************************************************/
94 /* Module information */
95 /********************************************************************/
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");
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
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");
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");
120 /********************************************************************/
121 /* Compile time configuration and compatibility stuff */
122 /********************************************************************/
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 */
131 /********************************************************************/
132 /* Internal constants */
133 /********************************************************************/
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)
139 #define ORINOCO_MIN_MTU 256
140 #define ORINOCO_MAX_MTU (IEEE80211_MAX_DATA_LEN - ENCAPS_OVERHEAD)
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 */
154 #define DUMMY_FID 0xFFFF
156 /*#define MAX_MULTICAST(priv) (priv->firmware_type == FIRMWARE_TYPE_AGERE ? \
157 HERMES_MAX_MULTICAST : 0)*/
158 #define MAX_MULTICAST(priv) (HERMES_MAX_MULTICAST)
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 )
165 #define MAX_RID_LEN 1024
167 static const struct iw_handler_def orinoco_handler_def;
168 static const struct ethtool_ops orinoco_ethtool_ops;
170 /********************************************************************/
171 /* Data tables */
172 /********************************************************************/
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
179 #define NUM_CHANNELS ARRAY_SIZE(channel_frequency)
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},
198 #define BITRATE_TABLE_SIZE ARRAY_SIZE(bitrate_table)
200 /********************************************************************/
201 /* Data types */
202 /********************************************************************/
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));
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;
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];
232 /* Data length */
233 __le16 data_len;
234 } __attribute__ ((packed));
236 /********************************************************************/
237 /* Function prototypes */
238 /********************************************************************/
240 static int __orinoco_program_rids(struct net_device *dev);
241 static void __orinoco_set_multicast_list(struct net_device *dev);
243 /********************************************************************/
244 /* Internal helper functions */
245 /********************************************************************/
247 static inline void set_port_type(struct orinoco_private *priv)
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;
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);
273 #define ORINOCO_MAX_BSS_COUNT 64
274 static int orinoco_bss_data_allocate(struct orinoco_private *priv)
276 if (priv->bss_data)
277 return 0;
279 priv->bss_data =
280 kzalloc(ORINOCO_MAX_BSS_COUNT * sizeof(bss_element), GFP_KERNEL);
281 if (!priv->bss_data) {
282 printk(KERN_WARNING "Out of memory allocating beacons");
283 return -ENOMEM;
285 return 0;
288 static void orinoco_bss_data_free(struct orinoco_private *priv)
290 kfree(priv->bss_data);
291 priv->bss_data = NULL;
294 static void orinoco_bss_data_init(struct orinoco_private *priv)
296 int i;
298 INIT_LIST_HEAD(&priv->bss_free_list);
299 INIT_LIST_HEAD(&priv->bss_list);
300 for (i = 0; i < ORINOCO_MAX_BSS_COUNT; i++)
301 list_add_tail(&priv->bss_data[i].list, &priv->bss_free_list);
304 /********************************************************************/
305 /* Device methods */
306 /********************************************************************/
308 static int orinoco_open(struct net_device *dev)
310 struct orinoco_private *priv = netdev_priv(dev);
311 unsigned long flags;
312 int err;
314 if (orinoco_lock(priv, &flags) != 0)
315 return -EBUSY;
317 err = __orinoco_up(dev);
319 if (! err)
320 priv->open = 1;
322 orinoco_unlock(priv, &flags);
324 return err;
327 static int orinoco_stop(struct net_device *dev)
329 struct orinoco_private *priv = netdev_priv(dev);
330 int err = 0;
332 /* We mustn't use orinoco_lock() here, because we need to be
333 able to close the interface even if hw_unavailable is set
334 (e.g. as we're released after a PC Card removal) */
335 orinoco_spin_lock(priv);
337 priv->open = 0;
339 err = __orinoco_down(dev);
341 orinoco_spin_unlock(priv);
343 return err;
346 static struct net_device_stats *orinoco_get_stats(struct net_device *dev)
348 struct orinoco_private *priv = netdev_priv(dev);
350 return &priv->stats;
353 static struct iw_statistics *orinoco_get_wireless_stats(struct net_device *dev)
355 struct orinoco_private *priv = netdev_priv(dev);
356 hermes_t *hw = &priv->hw;
357 struct iw_statistics *wstats = &priv->wstats;
358 int err;
359 unsigned long flags;
361 if (! netif_device_present(dev)) {
362 printk(KERN_WARNING "%s: get_wireless_stats() called while device not present\n",
363 dev->name);
364 return NULL; /* FIXME: Can we do better than this? */
367 /* If busy, return the old stats. Returning NULL may cause
368 * the interface to disappear from /proc/net/wireless */
369 if (orinoco_lock(priv, &flags) != 0)
370 return wstats;
372 /* We can't really wait for the tallies inquiry command to
373 * complete, so we just use the previous results and trigger
374 * a new tallies inquiry command for next time - Jean II */
375 /* FIXME: Really we should wait for the inquiry to come back -
376 * as it is the stats we give don't make a whole lot of sense.
377 * Unfortunately, it's not clear how to do that within the
378 * wireless extensions framework: I think we're in user
379 * context, but a lock seems to be held by the time we get in
380 * here so we're not safe to sleep here. */
381 hermes_inquire(hw, HERMES_INQ_TALLIES);
383 if (priv->iw_mode == IW_MODE_ADHOC) {
384 memset(&wstats->qual, 0, sizeof(wstats->qual));
385 /* If a spy address is defined, we report stats of the
386 * first spy address - Jean II */
387 if (SPY_NUMBER(priv)) {
388 wstats->qual.qual = priv->spy_data.spy_stat[0].qual;
389 wstats->qual.level = priv->spy_data.spy_stat[0].level;
390 wstats->qual.noise = priv->spy_data.spy_stat[0].noise;
391 wstats->qual.updated = priv->spy_data.spy_stat[0].updated;
393 } else {
394 struct {
395 __le16 qual, signal, noise, unused;
396 } __attribute__ ((packed)) cq;
398 err = HERMES_READ_RECORD(hw, USER_BAP,
399 HERMES_RID_COMMSQUALITY, &cq);
401 if (!err) {
402 wstats->qual.qual = (int)le16_to_cpu(cq.qual);
403 wstats->qual.level = (int)le16_to_cpu(cq.signal) - 0x95;
404 wstats->qual.noise = (int)le16_to_cpu(cq.noise) - 0x95;
405 wstats->qual.updated = 7;
409 orinoco_unlock(priv, &flags);
410 return wstats;
413 static void orinoco_set_multicast_list(struct net_device *dev)
415 struct orinoco_private *priv = netdev_priv(dev);
416 unsigned long flags;
418 if (orinoco_lock(priv, &flags) != 0) {
419 printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
420 "called when hw_unavailable\n", dev->name);
421 return;
424 __orinoco_set_multicast_list(dev);
425 orinoco_unlock(priv, &flags);
428 static int orinoco_change_mtu(struct net_device *dev, int new_mtu)
430 struct orinoco_private *priv = netdev_priv(dev);
432 if ( (new_mtu < ORINOCO_MIN_MTU) || (new_mtu > ORINOCO_MAX_MTU) )
433 return -EINVAL;
435 if ( (new_mtu + ENCAPS_OVERHEAD + sizeof(struct ieee80211_hdr)) >
436 (priv->nicbuf_size - ETH_HLEN) )
437 return -EINVAL;
439 dev->mtu = new_mtu;
441 return 0;
444 /********************************************************************/
445 /* Tx path */
446 /********************************************************************/
448 static int orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
450 struct orinoco_private *priv = netdev_priv(dev);
451 struct net_device_stats *stats = &priv->stats;
452 hermes_t *hw = &priv->hw;
453 int err = 0;
454 u16 txfid = priv->txfid;
455 struct ethhdr *eh;
456 int data_off;
457 struct hermes_tx_descriptor desc;
458 unsigned long flags;
460 if (! netif_running(dev)) {
461 printk(KERN_ERR "%s: Tx on stopped device!\n",
462 dev->name);
463 return NETDEV_TX_BUSY;
466 if (netif_queue_stopped(dev)) {
467 printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
468 dev->name);
469 return NETDEV_TX_BUSY;
472 if (orinoco_lock(priv, &flags) != 0) {
473 printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n",
474 dev->name);
475 return NETDEV_TX_BUSY;
478 if (! netif_carrier_ok(dev) || (priv->iw_mode == IW_MODE_MONITOR)) {
479 /* Oops, the firmware hasn't established a connection,
480 silently drop the packet (this seems to be the
481 safest approach). */
482 goto drop;
485 /* Check packet length */
486 if (skb->len < ETH_HLEN)
487 goto drop;
489 eh = (struct ethhdr *)skb->data;
491 memset(&desc, 0, sizeof(desc));
492 desc.tx_control = cpu_to_le16(HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX);
493 err = hw->ops->bap_pwrite(hw, USER_BAP, &desc, sizeof(desc), txfid, 0);
494 if (err) {
495 if (net_ratelimit())
496 printk(KERN_ERR "%s: Error %d writing Tx descriptor "
497 "to BAP\n", dev->name, err);
498 goto busy;
501 /* Clear the 802.11 header and data length fields - some
502 * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
503 * if this isn't done. */
504 hermes_clear_words(hw, HERMES_DATA0,
505 HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
507 /* Encapsulate Ethernet-II frames */
508 if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
509 struct header_struct {
510 struct ethhdr eth; /* 802.3 header */
511 u8 encap[6]; /* 802.2 header */
512 } __attribute__ ((packed)) hdr;
514 /* Strip destination and source from the data */
515 skb_pull(skb, 2 * ETH_ALEN);
516 data_off = HERMES_802_2_OFFSET + sizeof(encaps_hdr);
518 /* And move them to a separate header */
519 memcpy(&hdr.eth, eh, 2 * ETH_ALEN);
520 hdr.eth.h_proto = htons(sizeof(encaps_hdr) + skb->len);
521 memcpy(hdr.encap, encaps_hdr, sizeof(encaps_hdr));
523 err = hw->ops->bap_pwrite(hw, USER_BAP, &hdr, sizeof(hdr),
524 txfid, HERMES_802_3_OFFSET);
525 if (err) {
526 if (net_ratelimit())
527 printk(KERN_ERR "%s: Error %d writing packet "
528 "header to BAP\n", dev->name, err);
529 goto busy;
531 } else { /* IEEE 802.3 frame */
532 data_off = HERMES_802_3_OFFSET;
535 err = hw->ops->bap_pwrite(hw, USER_BAP, skb->data, skb->len,
536 txfid, data_off);
537 if (err) {
538 printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
539 dev->name, err);
540 goto busy;
543 /* Finally, we actually initiate the send */
544 netif_stop_queue(dev);
546 err = hw->ops->cmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
547 txfid, NULL);
548 if (err) {
549 netif_start_queue(dev);
550 if (net_ratelimit())
551 printk(KERN_ERR "%s: Error %d transmitting packet\n",
552 dev->name, err);
553 goto busy;
556 dev->trans_start = jiffies;
557 stats->tx_bytes += data_off + skb->len;
558 goto ok;
560 drop:
561 stats->tx_errors++;
562 stats->tx_dropped++;
565 orinoco_unlock(priv, &flags);
566 dev_kfree_skb(skb);
567 return NETDEV_TX_OK;
569 busy:
570 if (err == -EIO)
571 schedule_work(&priv->reset_work);
572 orinoco_unlock(priv, &flags);
573 return NETDEV_TX_BUSY;
576 static void __orinoco_ev_alloc(struct net_device *dev, hermes_t *hw)
578 struct orinoco_private *priv = netdev_priv(dev);
579 u16 fid = hermes_read_regn(hw, ALLOCFID);
581 if (fid != priv->txfid) {
582 if (fid != DUMMY_FID)
583 printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
584 dev->name, fid);
585 return;
588 hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
591 static void __orinoco_ev_tx(struct net_device *dev, hermes_t *hw)
593 struct orinoco_private *priv = netdev_priv(dev);
594 struct net_device_stats *stats = &priv->stats;
596 stats->tx_packets++;
598 netif_wake_queue(dev);
600 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
603 static void __orinoco_ev_txexc(struct net_device *dev, hermes_t *hw)
605 struct orinoco_private *priv = netdev_priv(dev);
606 struct net_device_stats *stats = &priv->stats;
607 u16 fid = hermes_read_regn(hw, TXCOMPLFID);
608 u16 status;
609 struct hermes_txexc_data hdr;
610 int err = 0;
612 if (fid == DUMMY_FID)
613 return; /* Nothing's really happened */
615 /* Read part of the frame header - we need status and addr1 */
616 err = hw->ops->bap_pread(hw, IRQ_BAP, &hdr,
617 sizeof(struct hermes_txexc_data),
618 fid, 0);
620 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
621 stats->tx_errors++;
623 if (err) {
624 printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
625 "(FID=%04X error %d)\n",
626 dev->name, fid, err);
627 return;
630 DEBUG(1, "%s: Tx error, err %d (FID=%04X)\n", dev->name,
631 err, fid);
633 /* We produce a TXDROP event only for retry or lifetime
634 * exceeded, because that's the only status that really mean
635 * that this particular node went away.
636 * Other errors means that *we* screwed up. - Jean II */
637 status = le16_to_cpu(hdr.desc.status);
638 if (status & (HERMES_TXSTAT_RETRYERR | HERMES_TXSTAT_AGEDERR)) {
639 union iwreq_data wrqu;
641 /* Copy 802.11 dest address.
642 * We use the 802.11 header because the frame may
643 * not be 802.3 or may be mangled...
644 * In Ad-Hoc mode, it will be the node address.
645 * In managed mode, it will be most likely the AP addr
646 * User space will figure out how to convert it to
647 * whatever it needs (IP address or else).
648 * - Jean II */
649 memcpy(wrqu.addr.sa_data, hdr.addr1, ETH_ALEN);
650 wrqu.addr.sa_family = ARPHRD_ETHER;
652 /* Send event to user space */
653 wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
656 netif_wake_queue(dev);
659 static void orinoco_tx_timeout(struct net_device *dev)
661 struct orinoco_private *priv = netdev_priv(dev);
662 struct net_device_stats *stats = &priv->stats;
663 struct hermes *hw = &priv->hw;
665 printk(KERN_WARNING "%s: Tx timeout! "
666 "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
667 dev->name, hermes_read_regn(hw, ALLOCFID),
668 hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
670 stats->tx_errors++;
672 schedule_work(&priv->reset_work);
675 /********************************************************************/
676 /* Rx path (data frames) */
677 /********************************************************************/
679 /* Does the frame have a SNAP header indicating it should be
680 * de-encapsulated to Ethernet-II? */
681 static inline int is_ethersnap(void *_hdr)
683 u8 *hdr = _hdr;
685 /* We de-encapsulate all packets which, a) have SNAP headers
686 * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
687 * and where b) the OUI of the SNAP header is 00:00:00 or
688 * 00:00:f8 - we need both because different APs appear to use
689 * different OUIs for some reason */
690 return (memcmp(hdr, &encaps_hdr, 5) == 0)
691 && ( (hdr[5] == 0x00) || (hdr[5] == 0xf8) );
694 static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
695 int level, int noise)
697 struct iw_quality wstats;
698 wstats.level = level - 0x95;
699 wstats.noise = noise - 0x95;
700 wstats.qual = (level > noise) ? (level - noise) : 0;
701 wstats.updated = 7;
702 /* Update spy records */
703 wireless_spy_update(dev, mac, &wstats);
706 static void orinoco_stat_gather(struct net_device *dev,
707 struct sk_buff *skb,
708 struct hermes_rx_descriptor *desc)
710 struct orinoco_private *priv = netdev_priv(dev);
712 /* Using spy support with lots of Rx packets, like in an
713 * infrastructure (AP), will really slow down everything, because
714 * the MAC address must be compared to each entry of the spy list.
715 * If the user really asks for it (set some address in the
716 * spy list), we do it, but he will pay the price.
717 * Note that to get here, you need both WIRELESS_SPY
718 * compiled in AND some addresses in the list !!!
720 /* Note : gcc will optimise the whole section away if
721 * WIRELESS_SPY is not defined... - Jean II */
722 if (SPY_NUMBER(priv)) {
723 orinoco_spy_gather(dev, skb_mac_header(skb) + ETH_ALEN,
724 desc->signal, desc->silence);
729 * orinoco_rx_monitor - handle received monitor frames.
731 * Arguments:
732 * dev network device
733 * rxfid received FID
734 * desc rx descriptor of the frame
736 * Call context: interrupt
738 static void orinoco_rx_monitor(struct net_device *dev, u16 rxfid,
739 struct hermes_rx_descriptor *desc)
741 u32 hdrlen = 30; /* return full header by default */
742 u32 datalen = 0;
743 u16 fc;
744 int err;
745 int len;
746 struct sk_buff *skb;
747 struct orinoco_private *priv = netdev_priv(dev);
748 struct net_device_stats *stats = &priv->stats;
749 hermes_t *hw = &priv->hw;
751 len = le16_to_cpu(desc->data_len);
753 /* Determine the size of the header and the data */
754 fc = le16_to_cpu(desc->frame_ctl);
755 switch (fc & IEEE80211_FCTL_FTYPE) {
756 case IEEE80211_FTYPE_DATA:
757 if ((fc & IEEE80211_FCTL_TODS)
758 && (fc & IEEE80211_FCTL_FROMDS))
759 hdrlen = 30;
760 else
761 hdrlen = 24;
762 datalen = len;
763 break;
764 case IEEE80211_FTYPE_MGMT:
765 hdrlen = 24;
766 datalen = len;
767 break;
768 case IEEE80211_FTYPE_CTL:
769 switch (fc & IEEE80211_FCTL_STYPE) {
770 case IEEE80211_STYPE_PSPOLL:
771 case IEEE80211_STYPE_RTS:
772 case IEEE80211_STYPE_CFEND:
773 case IEEE80211_STYPE_CFENDACK:
774 hdrlen = 16;
775 break;
776 case IEEE80211_STYPE_CTS:
777 case IEEE80211_STYPE_ACK:
778 hdrlen = 10;
779 break;
781 break;
782 default:
783 /* Unknown frame type */
784 break;
787 /* sanity check the length */
788 if (datalen > IEEE80211_MAX_DATA_LEN + 12) {
789 printk(KERN_DEBUG "%s: oversized monitor frame, "
790 "data length = %d\n", dev->name, datalen);
791 err = -EIO;
792 stats->rx_length_errors++;
793 goto update_stats;
796 skb = dev_alloc_skb(hdrlen + datalen);
797 if (!skb) {
798 printk(KERN_WARNING "%s: Cannot allocate skb for monitor frame\n",
799 dev->name);
800 err = -ENOMEM;
801 goto drop;
804 /* Copy the 802.11 header to the skb */
805 memcpy(skb_put(skb, hdrlen), &(desc->frame_ctl), hdrlen);
806 skb_reset_mac_header(skb);
808 /* If any, copy the data from the card to the skb */
809 if (datalen > 0) {
810 err = hw->ops->bap_pread(hw, IRQ_BAP, skb_put(skb, datalen),
811 ALIGN(datalen, 2), rxfid,
812 HERMES_802_2_OFFSET);
813 if (err) {
814 printk(KERN_ERR "%s: error %d reading monitor frame\n",
815 dev->name, err);
816 goto drop;
820 skb->dev = dev;
821 skb->ip_summed = CHECKSUM_NONE;
822 skb->pkt_type = PACKET_OTHERHOST;
823 skb->protocol = __constant_htons(ETH_P_802_2);
825 dev->last_rx = jiffies;
826 stats->rx_packets++;
827 stats->rx_bytes += skb->len;
829 netif_rx(skb);
830 return;
832 drop:
833 dev_kfree_skb_irq(skb);
834 update_stats:
835 stats->rx_errors++;
836 stats->rx_dropped++;
839 void __orinoco_ev_rx(struct net_device *dev, hermes_t *hw)
841 struct orinoco_private *priv = netdev_priv(dev);
842 struct net_device_stats *stats = &priv->stats;
843 struct iw_statistics *wstats = &priv->wstats;
844 struct sk_buff *skb = NULL;
845 u16 rxfid, status, fc;
846 int length;
847 struct hermes_rx_descriptor desc;
848 struct ethhdr *hdr;
849 int err;
851 rxfid = hermes_read_regn(hw, RXFID);
853 err = hw->ops->bap_pread(hw, IRQ_BAP, &desc, sizeof(desc),
854 rxfid, 0);
855 if (err) {
856 printk(KERN_ERR "%s: error %d reading Rx descriptor. "
857 "Frame dropped.\n", dev->name, err);
858 goto update_stats;
861 status = le16_to_cpu(desc.status);
863 if (status & HERMES_RXSTAT_BADCRC) {
864 DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n",
865 dev->name);
866 stats->rx_crc_errors++;
867 goto update_stats;
870 /* Handle frames in monitor mode */
871 if (priv->iw_mode == IW_MODE_MONITOR) {
872 orinoco_rx_monitor(dev, rxfid, &desc);
873 return;
876 if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
877 DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
878 dev->name);
879 wstats->discard.code++;
880 goto update_stats;
883 length = le16_to_cpu(desc.data_len);
884 fc = le16_to_cpu(desc.frame_ctl);
886 /* Sanity checks */
887 if (length < 3) { /* No for even an 802.2 LLC header */
888 /* At least on Symbol firmware with PCF we get quite a
889 lot of these legitimately - Poll frames with no
890 data. */
891 return;
893 if (length > IEEE80211_MAX_DATA_LEN) {
894 printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
895 dev->name, length);
896 stats->rx_length_errors++;
897 goto update_stats;
900 /* We need space for the packet data itself, plus an ethernet
901 header, plus 2 bytes so we can align the IP header on a
902 32bit boundary, plus 1 byte so we can read in odd length
903 packets from the card, which has an IO granularity of 16
904 bits */
905 skb = dev_alloc_skb(length+ETH_HLEN+2+1);
906 if (!skb) {
907 printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
908 dev->name);
909 goto update_stats;
912 /* We'll prepend the header, so reserve space for it. The worst
913 case is no decapsulation, when 802.3 header is prepended and
914 nothing is removed. 2 is for aligning the IP header. */
915 skb_reserve(skb, ETH_HLEN + 2);
917 err = hw->ops->bap_pread(hw, IRQ_BAP, skb_put(skb, length),
918 ALIGN(length, 2), rxfid,
919 HERMES_802_2_OFFSET);
920 if (err) {
921 printk(KERN_ERR "%s: error %d reading frame. "
922 "Frame dropped.\n", dev->name, err);
923 goto drop;
926 /* Handle decapsulation
927 * In most cases, the firmware tell us about SNAP frames.
928 * For some reason, the SNAP frames sent by LinkSys APs
929 * are not properly recognised by most firmwares.
930 * So, check ourselves */
931 if (length >= ENCAPS_OVERHEAD &&
932 (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
933 ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
934 is_ethersnap(skb->data))) {
935 /* These indicate a SNAP within 802.2 LLC within
936 802.11 frame which we'll need to de-encapsulate to
937 the original EthernetII frame. */
938 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN - ENCAPS_OVERHEAD);
939 } else {
940 /* 802.3 frame - prepend 802.3 header as is */
941 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN);
942 hdr->h_proto = htons(length);
944 memcpy(hdr->h_dest, desc.addr1, ETH_ALEN);
945 if (fc & IEEE80211_FCTL_FROMDS)
946 memcpy(hdr->h_source, desc.addr3, ETH_ALEN);
947 else
948 memcpy(hdr->h_source, desc.addr2, ETH_ALEN);
950 dev->last_rx = jiffies;
951 skb->protocol = eth_type_trans(skb, dev);
952 skb->ip_summed = CHECKSUM_NONE;
953 if (fc & IEEE80211_FCTL_TODS)
954 skb->pkt_type = PACKET_OTHERHOST;
956 /* Process the wireless stats if needed */
957 orinoco_stat_gather(dev, skb, &desc);
959 /* Pass the packet to the networking stack */
960 netif_rx(skb);
961 stats->rx_packets++;
962 stats->rx_bytes += length;
964 return;
966 drop:
967 dev_kfree_skb_irq(skb);
968 update_stats:
969 stats->rx_errors++;
970 stats->rx_dropped++;
972 EXPORT_SYMBOL(__orinoco_ev_rx);
974 /********************************************************************/
975 /* Rx path (info frames) */
976 /********************************************************************/
978 static void print_linkstatus(struct net_device *dev, u16 status)
980 char * s;
982 if (suppress_linkstatus)
983 return;
985 switch (status) {
986 case HERMES_LINKSTATUS_NOT_CONNECTED:
987 s = "Not Connected";
988 break;
989 case HERMES_LINKSTATUS_CONNECTED:
990 s = "Connected";
991 break;
992 case HERMES_LINKSTATUS_DISCONNECTED:
993 s = "Disconnected";
994 break;
995 case HERMES_LINKSTATUS_AP_CHANGE:
996 s = "AP Changed";
997 break;
998 case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
999 s = "AP Out of Range";
1000 break;
1001 case HERMES_LINKSTATUS_AP_IN_RANGE:
1002 s = "AP In Range";
1003 break;
1004 case HERMES_LINKSTATUS_ASSOC_FAILED:
1005 s = "Association Failed";
1006 break;
1007 default:
1008 s = "UNKNOWN";
1011 printk(KERN_INFO "%s: New link status: %s (%04x)\n",
1012 dev->name, s, status);
1015 /* Search scan results for requested BSSID, join it if found */
1016 static void orinoco_join_ap(struct work_struct *work)
1018 struct orinoco_private *priv =
1019 container_of(work, struct orinoco_private, join_work);
1020 struct net_device *dev = priv->ndev;
1021 struct hermes *hw = &priv->hw;
1022 int err;
1023 unsigned long flags;
1024 struct join_req {
1025 u8 bssid[ETH_ALEN];
1026 __le16 channel;
1027 } __attribute__ ((packed)) req;
1028 const int atom_len = offsetof(struct prism2_scan_apinfo, atim);
1029 struct prism2_scan_apinfo *atom = NULL;
1030 int offset = 4;
1031 int found = 0;
1032 u8 *buf;
1033 u16 len;
1035 /* Allocate buffer for scan results */
1036 buf = kmalloc(MAX_SCAN_LEN, GFP_KERNEL);
1037 if (! buf)
1038 return;
1040 if (orinoco_lock(priv, &flags) != 0)
1041 goto fail_lock;
1043 /* Sanity checks in case user changed something in the meantime */
1044 if (! priv->bssid_fixed)
1045 goto out;
1047 if (strlen(priv->desired_essid) == 0)
1048 goto out;
1050 /* Read scan results from the firmware */
1051 err = hw->ops->read_ltv(hw, USER_BAP,
1052 HERMES_RID_SCANRESULTSTABLE,
1053 MAX_SCAN_LEN, &len, buf);
1054 if (err) {
1055 printk(KERN_ERR "%s: Cannot read scan results\n",
1056 dev->name);
1057 goto out;
1060 len = HERMES_RECLEN_TO_BYTES(len);
1062 /* Go through the scan results looking for the channel of the AP
1063 * we were requested to join */
1064 for (; offset + atom_len <= len; offset += atom_len) {
1065 atom = (struct prism2_scan_apinfo *) (buf + offset);
1066 if (memcmp(&atom->bssid, priv->desired_bssid, ETH_ALEN) == 0) {
1067 found = 1;
1068 break;
1072 if (! found) {
1073 DEBUG(1, "%s: Requested AP not found in scan results\n",
1074 dev->name);
1075 goto out;
1078 memcpy(req.bssid, priv->desired_bssid, ETH_ALEN);
1079 req.channel = atom->channel; /* both are little-endian */
1080 err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNFJOINREQUEST,
1081 &req);
1082 if (err)
1083 printk(KERN_ERR "%s: Error issuing join request\n", dev->name);
1085 out:
1086 orinoco_unlock(priv, &flags);
1088 fail_lock:
1089 kfree(buf);
1092 /* Send new BSSID to userspace */
1093 static void orinoco_send_wevents(struct work_struct *work)
1095 struct orinoco_private *priv =
1096 container_of(work, struct orinoco_private, wevent_work);
1097 struct net_device *dev = priv->ndev;
1098 struct hermes *hw = &priv->hw;
1099 union iwreq_data wrqu;
1100 int err;
1101 unsigned long flags;
1103 if (orinoco_lock(priv, &flags) != 0)
1104 return;
1106 err = hw->ops->read_ltv(hw, IRQ_BAP, HERMES_RID_CURRENTBSSID,
1107 ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
1108 if (err != 0)
1109 goto out;
1111 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1113 /* Send event to user space */
1114 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
1116 out:
1117 orinoco_unlock(priv, &flags);
1121 static inline void orinoco_clear_scan_results(struct orinoco_private *priv,
1122 unsigned long scan_age)
1124 bss_element *bss;
1125 bss_element *tmp_bss;
1127 /* Blow away current list of scan results */
1128 list_for_each_entry_safe(bss, tmp_bss, &priv->bss_list, list) {
1129 if (!scan_age ||
1130 time_after(jiffies, bss->last_scanned + scan_age)) {
1131 list_move_tail(&bss->list, &priv->bss_free_list);
1132 /* Don't blow away ->list, just BSS data */
1133 memset(bss, 0, sizeof(bss->bss));
1134 bss->last_scanned = 0;
1139 static int orinoco_process_scan_results(struct net_device *dev,
1140 unsigned char *buf,
1141 int len)
1143 struct orinoco_private *priv = netdev_priv(dev);
1144 int offset; /* In the scan data */
1145 union hermes_scan_info *atom;
1146 int atom_len;
1148 switch (priv->firmware_type) {
1149 case FIRMWARE_TYPE_AGERE:
1150 atom_len = sizeof(struct agere_scan_apinfo);
1151 offset = 0;
1152 break;
1153 case FIRMWARE_TYPE_SYMBOL:
1154 /* Lack of documentation necessitates this hack.
1155 * Different firmwares have 68 or 76 byte long atoms.
1156 * We try modulo first. If the length divides by both,
1157 * we check what would be the channel in the second
1158 * frame for a 68-byte atom. 76-byte atoms have 0 there.
1159 * Valid channel cannot be 0. */
1160 if (len % 76)
1161 atom_len = 68;
1162 else if (len % 68)
1163 atom_len = 76;
1164 else if (len >= 1292 && buf[68] == 0)
1165 atom_len = 76;
1166 else
1167 atom_len = 68;
1168 offset = 0;
1169 break;
1170 case FIRMWARE_TYPE_INTERSIL:
1171 offset = 4;
1172 if (priv->has_hostscan) {
1173 atom_len = le16_to_cpup((__le16 *)buf);
1174 /* Sanity check for atom_len */
1175 if (atom_len < sizeof(struct prism2_scan_apinfo)) {
1176 printk(KERN_ERR "%s: Invalid atom_len in scan "
1177 "data: %d\n", dev->name, atom_len);
1178 return -EIO;
1180 } else
1181 atom_len = offsetof(struct prism2_scan_apinfo, atim);
1182 break;
1183 default:
1184 return -EOPNOTSUPP;
1187 /* Check that we got an whole number of atoms */
1188 if ((len - offset) % atom_len) {
1189 printk(KERN_ERR "%s: Unexpected scan data length %d, "
1190 "atom_len %d, offset %d\n", dev->name, len,
1191 atom_len, offset);
1192 return -EIO;
1195 orinoco_clear_scan_results(priv, msecs_to_jiffies(15000));
1197 /* Read the entries one by one */
1198 for (; offset + atom_len <= len; offset += atom_len) {
1199 int found = 0;
1200 bss_element *bss = NULL;
1202 /* Get next atom */
1203 atom = (union hermes_scan_info *) (buf + offset);
1205 /* Try to update an existing bss first */
1206 list_for_each_entry(bss, &priv->bss_list, list) {
1207 if (compare_ether_addr(bss->bss.a.bssid, atom->a.bssid))
1208 continue;
1209 if (le16_to_cpu(bss->bss.a.essid_len) !=
1210 le16_to_cpu(atom->a.essid_len))
1211 continue;
1212 if (memcmp(bss->bss.a.essid, atom->a.essid,
1213 le16_to_cpu(atom->a.essid_len)))
1214 continue;
1215 found = 1;
1216 break;
1219 /* Grab a bss off the free list */
1220 if (!found && !list_empty(&priv->bss_free_list)) {
1221 bss = list_entry(priv->bss_free_list.next,
1222 bss_element, list);
1223 list_del(priv->bss_free_list.next);
1225 list_add_tail(&bss->list, &priv->bss_list);
1228 if (bss) {
1229 /* Always update the BSS to get latest beacon info */
1230 memcpy(&bss->bss, atom, sizeof(bss->bss));
1231 bss->last_scanned = jiffies;
1235 return 0;
1238 void __orinoco_ev_info(struct net_device *dev, hermes_t *hw)
1240 struct orinoco_private *priv = netdev_priv(dev);
1241 u16 infofid;
1242 struct {
1243 __le16 len;
1244 __le16 type;
1245 } __attribute__ ((packed)) info;
1246 int len, type;
1247 int err;
1249 /* This is an answer to an INQUIRE command that we did earlier,
1250 * or an information "event" generated by the card
1251 * The controller return to us a pseudo frame containing
1252 * the information in question - Jean II */
1253 infofid = hermes_read_regn(hw, INFOFID);
1255 /* Read the info frame header - don't try too hard */
1256 err = hw->ops->bap_pread(hw, IRQ_BAP, &info, sizeof(info),
1257 infofid, 0);
1258 if (err) {
1259 printk(KERN_ERR "%s: error %d reading info frame. "
1260 "Frame dropped.\n", dev->name, err);
1261 return;
1264 len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
1265 type = le16_to_cpu(info.type);
1267 switch (type) {
1268 case HERMES_INQ_TALLIES: {
1269 struct hermes_tallies_frame tallies;
1270 struct iw_statistics *wstats = &priv->wstats;
1272 if (len > sizeof(tallies)) {
1273 printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
1274 dev->name, len);
1275 len = sizeof(tallies);
1278 err = hw->ops->bap_pread(hw, IRQ_BAP, &tallies, len,
1279 infofid, sizeof(info));
1280 if (err)
1281 break;
1283 /* Increment our various counters */
1284 /* wstats->discard.nwid - no wrong BSSID stuff */
1285 wstats->discard.code +=
1286 le16_to_cpu(tallies.RxWEPUndecryptable);
1287 if (len == sizeof(tallies))
1288 wstats->discard.code +=
1289 le16_to_cpu(tallies.RxDiscards_WEPICVError) +
1290 le16_to_cpu(tallies.RxDiscards_WEPExcluded);
1291 wstats->discard.misc +=
1292 le16_to_cpu(tallies.TxDiscardsWrongSA);
1293 wstats->discard.fragment +=
1294 le16_to_cpu(tallies.RxMsgInBadMsgFragments);
1295 wstats->discard.retries +=
1296 le16_to_cpu(tallies.TxRetryLimitExceeded);
1297 /* wstats->miss.beacon - no match */
1299 break;
1300 case HERMES_INQ_LINKSTATUS: {
1301 struct hermes_linkstatus linkstatus;
1302 u16 newstatus;
1303 int connected;
1305 if (priv->iw_mode == IW_MODE_MONITOR)
1306 break;
1308 if (len != sizeof(linkstatus)) {
1309 printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
1310 dev->name, len);
1311 break;
1314 err = hw->ops->bap_pread(hw, IRQ_BAP, &linkstatus, len,
1315 infofid, sizeof(info));
1316 if (err)
1317 break;
1318 newstatus = le16_to_cpu(linkstatus.linkstatus);
1320 /* Symbol firmware uses "out of range" to signal that
1321 * the hostscan frame can be requested. */
1322 if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
1323 priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
1324 priv->has_hostscan && priv->scan_inprogress) {
1325 hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
1326 break;
1329 connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
1330 || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
1331 || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
1333 if (connected)
1334 netif_carrier_on(dev);
1335 else if (!ignore_disconnect)
1336 netif_carrier_off(dev);
1338 if (newstatus != priv->last_linkstatus) {
1339 priv->last_linkstatus = newstatus;
1340 print_linkstatus(dev, newstatus);
1341 /* The info frame contains only one word which is the
1342 * status (see hermes.h). The status is pretty boring
1343 * in itself, that's why we export the new BSSID...
1344 * Jean II */
1345 schedule_work(&priv->wevent_work);
1348 break;
1349 case HERMES_INQ_SCAN:
1350 if (!priv->scan_inprogress && priv->bssid_fixed &&
1351 priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
1352 schedule_work(&priv->join_work);
1353 break;
1355 /* fall through */
1356 case HERMES_INQ_HOSTSCAN:
1357 case HERMES_INQ_HOSTSCAN_SYMBOL: {
1358 /* Result of a scanning. Contains information about
1359 * cells in the vicinity - Jean II */
1360 union iwreq_data wrqu;
1361 unsigned char *buf;
1363 /* Scan is no longer in progress */
1364 priv->scan_inprogress = 0;
1366 /* Sanity check */
1367 if (len > 4096) {
1368 printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
1369 dev->name, len);
1370 break;
1373 /* Allocate buffer for results */
1374 buf = kmalloc(len, GFP_ATOMIC);
1375 if (buf == NULL)
1376 /* No memory, so can't printk()... */
1377 break;
1379 /* Read scan data */
1380 err = hw->ops->bap_pread(hw, IRQ_BAP, (void *) buf, len,
1381 infofid, sizeof(info));
1382 if (err) {
1383 kfree(buf);
1384 break;
1387 #ifdef ORINOCO_DEBUG
1389 int i;
1390 printk(KERN_DEBUG "Scan result [%02X", buf[0]);
1391 for(i = 1; i < (len * 2); i++)
1392 printk(":%02X", buf[i]);
1393 printk("]\n");
1395 #endif /* ORINOCO_DEBUG */
1397 if (orinoco_process_scan_results(dev, buf, len) == 0) {
1398 /* Send an empty event to user space.
1399 * We don't send the received data on the event because
1400 * it would require us to do complex transcoding, and
1401 * we want to minimise the work done in the irq handler
1402 * Use a request to extract the data - Jean II */
1403 wrqu.data.length = 0;
1404 wrqu.data.flags = 0;
1405 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
1407 kfree(buf);
1409 break;
1410 case HERMES_INQ_SEC_STAT_AGERE:
1411 /* Security status (Agere specific) */
1412 /* Ignore this frame for now */
1413 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
1414 break;
1415 /* fall through */
1416 default:
1417 printk(KERN_DEBUG "%s: Unknown information frame received: "
1418 "type 0x%04x, length %d\n", dev->name, type, len);
1419 /* We don't actually do anything about it */
1420 break;
1423 EXPORT_SYMBOL(__orinoco_ev_info);
1425 static void __orinoco_ev_infdrop(struct net_device *dev, hermes_t *hw)
1427 if (net_ratelimit())
1428 printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
1431 /********************************************************************/
1432 /* Internal hardware control routines */
1433 /********************************************************************/
1435 int __orinoco_up(struct net_device *dev)
1437 struct orinoco_private *priv = netdev_priv(dev);
1438 struct hermes *hw = &priv->hw;
1439 int err;
1441 netif_carrier_off(dev); /* just to make sure */
1443 err = __orinoco_program_rids(dev);
1444 if (err) {
1445 printk(KERN_ERR "%s: Error %d configuring card\n",
1446 dev->name, err);
1447 return err;
1450 /* Fire things up again */
1451 hermes_set_irqmask(hw, ORINOCO_INTEN);
1452 err = hermes_enable_port(hw, 0);
1453 if (err) {
1454 printk(KERN_ERR "%s: Error %d enabling MAC port\n",
1455 dev->name, err);
1456 return err;
1459 netif_start_queue(dev);
1461 return 0;
1464 int __orinoco_down(struct net_device *dev)
1466 struct orinoco_private *priv = netdev_priv(dev);
1467 struct hermes *hw = &priv->hw;
1468 int err;
1470 netif_stop_queue(dev);
1472 if (! priv->hw_unavailable) {
1473 if (! priv->broken_disableport) {
1474 err = hermes_disable_port(hw, 0);
1475 if (err) {
1476 /* Some firmwares (e.g. Intersil 1.3.x) seem
1477 * to have problems disabling the port, oh
1478 * well, too bad. */
1479 printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
1480 dev->name, err);
1481 priv->broken_disableport = 1;
1484 hermes_set_irqmask(hw, 0);
1485 hermes_write_regn(hw, EVACK, 0xffff);
1488 /* firmware will have to reassociate */
1489 netif_carrier_off(dev);
1490 priv->last_linkstatus = 0xffff;
1492 return 0;
1495 static int orinoco_allocate_fid(struct net_device *dev)
1497 struct orinoco_private *priv = netdev_priv(dev);
1498 struct hermes *hw = &priv->hw;
1499 int err;
1501 err = hw->ops->allocate(hw, priv->nicbuf_size, &priv->txfid);
1502 if (err == -EIO && priv->nicbuf_size > TX_NICBUF_SIZE_BUG) {
1503 /* Try workaround for old Symbol firmware bug */
1504 printk(KERN_WARNING "%s: firmware ALLOC bug detected "
1505 "(old Symbol firmware?). Trying to work around... ",
1506 dev->name);
1508 priv->nicbuf_size = TX_NICBUF_SIZE_BUG;
1509 err = hw->ops->allocate(hw, priv->nicbuf_size, &priv->txfid);
1510 if (err)
1511 printk("failed!\n");
1512 else
1513 printk("ok.\n");
1516 return err;
1519 int orinoco_reinit_firmware(struct net_device *dev)
1521 struct orinoco_private *priv = netdev_priv(dev);
1522 struct hermes *hw = &priv->hw;
1523 int err;
1525 err = hw->ops->init(hw);
1526 if (!err)
1527 err = orinoco_allocate_fid(dev);
1529 return err;
1532 static int __orinoco_hw_set_bitrate(struct orinoco_private *priv)
1534 hermes_t *hw = &priv->hw;
1535 int err = 0;
1537 if (priv->bitratemode >= BITRATE_TABLE_SIZE) {
1538 printk(KERN_ERR "%s: BUG: Invalid bitrate mode %d\n",
1539 priv->ndev->name, priv->bitratemode);
1540 return -EINVAL;
1543 switch (priv->firmware_type) {
1544 case FIRMWARE_TYPE_AGERE:
1545 err = hermes_write_wordrec(hw, USER_BAP,
1546 HERMES_RID_CNFTXRATECONTROL,
1547 bitrate_table[priv->bitratemode].agere_txratectrl);
1548 break;
1549 case FIRMWARE_TYPE_INTERSIL:
1550 case FIRMWARE_TYPE_SYMBOL:
1551 err = hermes_write_wordrec(hw, USER_BAP,
1552 HERMES_RID_CNFTXRATECONTROL,
1553 bitrate_table[priv->bitratemode].intersil_txratectrl);
1554 break;
1555 default:
1556 BUG();
1559 return err;
1562 /* Set fixed AP address */
1563 static int __orinoco_hw_set_wap(struct orinoco_private *priv)
1565 int roaming_flag;
1566 int err = 0;
1567 hermes_t *hw = &priv->hw;
1569 switch (priv->firmware_type) {
1570 case FIRMWARE_TYPE_AGERE:
1571 /* not supported */
1572 break;
1573 case FIRMWARE_TYPE_INTERSIL:
1574 if (priv->bssid_fixed)
1575 roaming_flag = 2;
1576 else
1577 roaming_flag = 1;
1579 err = hermes_write_wordrec(hw, USER_BAP,
1580 HERMES_RID_CNFROAMINGMODE,
1581 roaming_flag);
1582 break;
1583 case FIRMWARE_TYPE_SYMBOL:
1584 err = HERMES_WRITE_RECORD(hw, USER_BAP,
1585 HERMES_RID_CNFMANDATORYBSSID_SYMBOL,
1586 &priv->desired_bssid);
1587 break;
1589 return err;
1592 /* Change the WEP keys and/or the current keys. Can be called
1593 * either from __orinoco_hw_setup_wep() or directly from
1594 * orinoco_ioctl_setiwencode(). In the later case the association
1595 * with the AP is not broken (if the firmware can handle it),
1596 * which is needed for 802.1x implementations. */
1597 static int __orinoco_hw_setup_wepkeys(struct orinoco_private *priv)
1599 hermes_t *hw = &priv->hw;
1600 int err = 0;
1602 switch (priv->firmware_type) {
1603 case FIRMWARE_TYPE_AGERE:
1604 err = HERMES_WRITE_RECORD(hw, USER_BAP,
1605 HERMES_RID_CNFWEPKEYS_AGERE,
1606 &priv->keys);
1607 if (err)
1608 return err;
1609 err = hermes_write_wordrec(hw, USER_BAP,
1610 HERMES_RID_CNFTXKEY_AGERE,
1611 priv->tx_key);
1612 if (err)
1613 return err;
1614 break;
1615 case FIRMWARE_TYPE_INTERSIL:
1616 case FIRMWARE_TYPE_SYMBOL:
1618 int keylen;
1619 int i;
1621 /* Force uniform key length to work around firmware bugs */
1622 keylen = le16_to_cpu(priv->keys[priv->tx_key].len);
1624 if (keylen > LARGE_KEY_SIZE) {
1625 printk(KERN_ERR "%s: BUG: Key %d has oversize length %d.\n",
1626 priv->ndev->name, priv->tx_key, keylen);
1627 return -E2BIG;
1630 /* Write all 4 keys */
1631 for(i = 0; i < ORINOCO_MAX_KEYS; i++) {
1632 err = hw->ops->write_ltv(hw, USER_BAP,
1633 HERMES_RID_CNFDEFAULTKEY0 + i,
1634 HERMES_BYTES_TO_RECLEN(keylen),
1635 priv->keys[i].data);
1636 if (err)
1637 return err;
1640 /* Write the index of the key used in transmission */
1641 err = hermes_write_wordrec(hw, USER_BAP,
1642 HERMES_RID_CNFWEPDEFAULTKEYID,
1643 priv->tx_key);
1644 if (err)
1645 return err;
1647 break;
1650 return 0;
1653 static int __orinoco_hw_setup_wep(struct orinoco_private *priv)
1655 hermes_t *hw = &priv->hw;
1656 int err = 0;
1657 int master_wep_flag;
1658 int auth_flag;
1660 if (priv->wep_on)
1661 __orinoco_hw_setup_wepkeys(priv);
1663 if (priv->wep_restrict)
1664 auth_flag = HERMES_AUTH_SHARED_KEY;
1665 else
1666 auth_flag = HERMES_AUTH_OPEN;
1668 switch (priv->firmware_type) {
1669 case FIRMWARE_TYPE_AGERE: /* Agere style WEP */
1670 if (priv->wep_on) {
1671 /* Enable the shared-key authentication. */
1672 err = hermes_write_wordrec(hw, USER_BAP,
1673 HERMES_RID_CNFAUTHENTICATION_AGERE,
1674 auth_flag);
1676 err = hermes_write_wordrec(hw, USER_BAP,
1677 HERMES_RID_CNFWEPENABLED_AGERE,
1678 priv->wep_on);
1679 if (err)
1680 return err;
1681 break;
1683 case FIRMWARE_TYPE_INTERSIL: /* Intersil style WEP */
1684 case FIRMWARE_TYPE_SYMBOL: /* Symbol style WEP */
1685 if (priv->wep_on) {
1686 if (priv->wep_restrict ||
1687 (priv->firmware_type == FIRMWARE_TYPE_SYMBOL))
1688 master_wep_flag = HERMES_WEP_PRIVACY_INVOKED |
1689 HERMES_WEP_EXCL_UNENCRYPTED;
1690 else
1691 master_wep_flag = HERMES_WEP_PRIVACY_INVOKED;
1693 err = hermes_write_wordrec(hw, USER_BAP,
1694 HERMES_RID_CNFAUTHENTICATION,
1695 auth_flag);
1696 if (err)
1697 return err;
1698 } else
1699 master_wep_flag = 0;
1701 if (priv->iw_mode == IW_MODE_MONITOR)
1702 master_wep_flag |= HERMES_WEP_HOST_DECRYPT;
1704 /* Master WEP setting : on/off */
1705 err = hermes_write_wordrec(hw, USER_BAP,
1706 HERMES_RID_CNFWEPFLAGS_INTERSIL,
1707 master_wep_flag);
1708 if (err)
1709 return err;
1711 break;
1714 return 0;
1717 static int __orinoco_program_rids(struct net_device *dev)
1719 struct orinoco_private *priv = netdev_priv(dev);
1720 hermes_t *hw = &priv->hw;
1721 int err;
1722 struct hermes_idstring idbuf;
1724 /* Set the MAC address */
1725 err = hw->ops->write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
1726 HERMES_BYTES_TO_RECLEN(ETH_ALEN), dev->dev_addr);
1727 if (err) {
1728 printk(KERN_ERR "%s: Error %d setting MAC address\n",
1729 dev->name, err);
1730 return err;
1733 /* Set up the link mode */
1734 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFPORTTYPE,
1735 priv->port_type);
1736 if (err) {
1737 printk(KERN_ERR "%s: Error %d setting port type\n",
1738 dev->name, err);
1739 return err;
1741 /* Set the channel/frequency */
1742 if (priv->channel != 0 && priv->iw_mode != IW_MODE_INFRA) {
1743 err = hermes_write_wordrec(hw, USER_BAP,
1744 HERMES_RID_CNFOWNCHANNEL,
1745 priv->channel);
1746 if (err) {
1747 printk(KERN_ERR "%s: Error %d setting channel %d\n",
1748 dev->name, err, priv->channel);
1749 return err;
1753 if (priv->has_ibss) {
1754 u16 createibss;
1756 if ((strlen(priv->desired_essid) == 0) && (priv->createibss)) {
1757 printk(KERN_WARNING "%s: This firmware requires an "
1758 "ESSID in IBSS-Ad-Hoc mode.\n", dev->name);
1759 /* With wvlan_cs, in this case, we would crash.
1760 * hopefully, this driver will behave better...
1761 * Jean II */
1762 createibss = 0;
1763 } else {
1764 createibss = priv->createibss;
1767 err = hermes_write_wordrec(hw, USER_BAP,
1768 HERMES_RID_CNFCREATEIBSS,
1769 createibss);
1770 if (err) {
1771 printk(KERN_ERR "%s: Error %d setting CREATEIBSS\n",
1772 dev->name, err);
1773 return err;
1777 /* Set the desired BSSID */
1778 err = __orinoco_hw_set_wap(priv);
1779 if (err) {
1780 printk(KERN_ERR "%s: Error %d setting AP address\n",
1781 dev->name, err);
1782 return err;
1784 /* Set the desired ESSID */
1785 idbuf.len = cpu_to_le16(strlen(priv->desired_essid));
1786 memcpy(&idbuf.val, priv->desired_essid, sizeof(idbuf.val));
1787 /* WinXP wants partner to configure OWNSSID even in IBSS mode. (jimc) */
1788 err = hw->ops->write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNSSID,
1789 HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
1790 &idbuf);
1791 if (err) {
1792 printk(KERN_ERR "%s: Error %d setting OWNSSID\n",
1793 dev->name, err);
1794 return err;
1796 err = hw->ops->write_ltv(hw, USER_BAP, HERMES_RID_CNFDESIREDSSID,
1797 HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
1798 &idbuf);
1799 if (err) {
1800 printk(KERN_ERR "%s: Error %d setting DESIREDSSID\n",
1801 dev->name, err);
1802 return err;
1805 /* Set the station name */
1806 idbuf.len = cpu_to_le16(strlen(priv->nick));
1807 memcpy(&idbuf.val, priv->nick, sizeof(idbuf.val));
1808 err = hw->ops->write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
1809 HERMES_BYTES_TO_RECLEN(strlen(priv->nick)+2),
1810 &idbuf);
1811 if (err) {
1812 printk(KERN_ERR "%s: Error %d setting nickname\n",
1813 dev->name, err);
1814 return err;
1817 /* Set AP density */
1818 if (priv->has_sensitivity) {
1819 err = hermes_write_wordrec(hw, USER_BAP,
1820 HERMES_RID_CNFSYSTEMSCALE,
1821 priv->ap_density);
1822 if (err) {
1823 printk(KERN_WARNING "%s: Error %d setting SYSTEMSCALE. "
1824 "Disabling sensitivity control\n",
1825 dev->name, err);
1827 priv->has_sensitivity = 0;
1831 /* Set RTS threshold */
1832 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
1833 priv->rts_thresh);
1834 if (err) {
1835 printk(KERN_ERR "%s: Error %d setting RTS threshold\n",
1836 dev->name, err);
1837 return err;
1840 /* Set fragmentation threshold or MWO robustness */
1841 if (priv->has_mwo)
1842 err = hermes_write_wordrec(hw, USER_BAP,
1843 HERMES_RID_CNFMWOROBUST_AGERE,
1844 priv->mwo_robust);
1845 else
1846 err = hermes_write_wordrec(hw, USER_BAP,
1847 HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
1848 priv->frag_thresh);
1849 if (err) {
1850 printk(KERN_ERR "%s: Error %d setting fragmentation\n",
1851 dev->name, err);
1852 return err;
1855 /* Set bitrate */
1856 err = __orinoco_hw_set_bitrate(priv);
1857 if (err) {
1858 printk(KERN_ERR "%s: Error %d setting bitrate\n",
1859 dev->name, err);
1860 return err;
1863 /* Set power management */
1864 if (priv->has_pm) {
1865 err = hermes_write_wordrec(hw, USER_BAP,
1866 HERMES_RID_CNFPMENABLED,
1867 priv->pm_on);
1868 if (err) {
1869 printk(KERN_ERR "%s: Error %d setting up PM\n",
1870 dev->name, err);
1871 return err;
1874 err = hermes_write_wordrec(hw, USER_BAP,
1875 HERMES_RID_CNFMULTICASTRECEIVE,
1876 priv->pm_mcast);
1877 if (err) {
1878 printk(KERN_ERR "%s: Error %d setting up PM\n",
1879 dev->name, err);
1880 return err;
1882 err = hermes_write_wordrec(hw, USER_BAP,
1883 HERMES_RID_CNFMAXSLEEPDURATION,
1884 priv->pm_period);
1885 if (err) {
1886 printk(KERN_ERR "%s: Error %d setting up PM\n",
1887 dev->name, err);
1888 return err;
1890 err = hermes_write_wordrec(hw, USER_BAP,
1891 HERMES_RID_CNFPMHOLDOVERDURATION,
1892 priv->pm_timeout);
1893 if (err) {
1894 printk(KERN_ERR "%s: Error %d setting up PM\n",
1895 dev->name, err);
1896 return err;
1900 /* Set preamble - only for Symbol so far... */
1901 if (priv->has_preamble) {
1902 err = hermes_write_wordrec(hw, USER_BAP,
1903 HERMES_RID_CNFPREAMBLE_SYMBOL,
1904 priv->preamble);
1905 if (err) {
1906 printk(KERN_ERR "%s: Error %d setting preamble\n",
1907 dev->name, err);
1908 return err;
1912 /* Set up encryption */
1913 if (priv->has_wep) {
1914 err = __orinoco_hw_setup_wep(priv);
1915 if (err) {
1916 printk(KERN_ERR "%s: Error %d activating WEP\n",
1917 dev->name, err);
1918 return err;
1922 if (priv->iw_mode == IW_MODE_MONITOR) {
1923 /* Enable monitor mode */
1924 dev->type = ARPHRD_IEEE80211;
1925 err = hw->ops->cmd_wait(hw, HERMES_CMD_TEST |
1926 HERMES_TEST_MONITOR, 0, NULL);
1927 } else {
1928 /* Disable monitor mode */
1929 dev->type = ARPHRD_ETHER;
1930 err = hw->ops->cmd_wait(hw, HERMES_CMD_TEST |
1931 HERMES_TEST_STOP, 0, NULL);
1933 if (err)
1934 return err;
1936 /* Set promiscuity / multicast*/
1937 priv->promiscuous = 0;
1938 priv->mc_count = 0;
1940 /* FIXME: what about netif_tx_lock */
1941 __orinoco_set_multicast_list(dev);
1943 return 0;
1946 /* FIXME: return int? */
1947 static void
1948 __orinoco_set_multicast_list(struct net_device *dev)
1950 struct orinoco_private *priv = netdev_priv(dev);
1951 hermes_t *hw = &priv->hw;
1952 int err = 0;
1953 int promisc, mc_count;
1955 /* The Hermes doesn't seem to have an allmulti mode, so we go
1956 * into promiscuous mode and let the upper levels deal. */
1957 if ( (dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
1958 (dev->mc_count > MAX_MULTICAST(priv)) ) {
1959 promisc = 1;
1960 mc_count = 0;
1961 } else {
1962 promisc = 0;
1963 mc_count = dev->mc_count;
1966 if (promisc != priv->promiscuous) {
1967 err = hermes_write_wordrec(hw, USER_BAP,
1968 HERMES_RID_CNFPROMISCUOUSMODE,
1969 promisc);
1970 if (err) {
1971 printk(KERN_ERR "%s: Error %d setting PROMISCUOUSMODE to 1.\n",
1972 dev->name, err);
1973 } else
1974 priv->promiscuous = promisc;
1977 /* If we're not in promiscuous mode, then we need to set the
1978 * group address if either we want to multicast, or if we were
1979 * multicasting and want to stop */
1980 if (! promisc && (mc_count || priv->mc_count) ) {
1981 struct dev_mc_list *p = dev->mc_list;
1982 struct hermes_multicast mclist;
1983 int i;
1985 for (i = 0; i < mc_count; i++) {
1986 /* paranoia: is list shorter than mc_count? */
1987 BUG_ON(! p);
1988 /* paranoia: bad address size in list? */
1989 BUG_ON(p->dmi_addrlen != ETH_ALEN);
1991 memcpy(mclist.addr[i], p->dmi_addr, ETH_ALEN);
1992 p = p->next;
1995 if (p)
1996 printk(KERN_WARNING "%s: Multicast list is "
1997 "longer than mc_count\n", dev->name);
1999 err = hw->ops->write_ltv(hw, USER_BAP,
2000 HERMES_RID_CNFGROUPADDRESSES,
2001 HERMES_BYTES_TO_RECLEN(mc_count * ETH_ALEN),
2002 &mclist);
2003 if (err)
2004 printk(KERN_ERR "%s: Error %d setting multicast list.\n",
2005 dev->name, err);
2006 else
2007 priv->mc_count = mc_count;
2011 /* This must be called from user context, without locks held - use
2012 * schedule_work() */
2013 static void orinoco_reset(struct work_struct *work)
2015 struct orinoco_private *priv =
2016 container_of(work, struct orinoco_private, reset_work);
2017 struct net_device *dev = priv->ndev;
2018 struct hermes *hw = &priv->hw;
2019 int err;
2020 unsigned long flags;
2022 if (orinoco_lock(priv, &flags) != 0)
2023 /* When the hardware becomes available again, whatever
2024 * detects that is responsible for re-initializing
2025 * it. So no need for anything further */
2026 return;
2028 netif_stop_queue(dev);
2030 /* Shut off interrupts. Depending on what state the hardware
2031 * is in, this might not work, but we'll try anyway */
2032 hermes_set_irqmask(hw, 0);
2033 hermes_write_regn(hw, EVACK, 0xffff);
2035 priv->hw_unavailable++;
2036 priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
2037 netif_carrier_off(dev);
2039 orinoco_unlock(priv, &flags);
2041 /* Scanning support: Cleanup of driver struct */
2042 orinoco_clear_scan_results(priv, 0);
2043 priv->scan_inprogress = 0;
2045 if (priv->hard_reset) {
2046 err = (*priv->hard_reset)(priv);
2047 if (err) {
2048 printk(KERN_ERR "%s: orinoco_reset: Error %d "
2049 "performing hard reset\n", dev->name, err);
2050 goto disable;
2054 err = orinoco_reinit_firmware(dev);
2055 if (err) {
2056 printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
2057 dev->name, err);
2058 goto disable;
2061 orinoco_spin_lock(priv); /* This has to be called from user context */
2063 priv->hw_unavailable--;
2065 /* priv->open or priv->hw_unavailable might have changed while
2066 * we dropped the lock */
2067 if (priv->open && (! priv->hw_unavailable)) {
2068 err = __orinoco_up(dev);
2069 if (err) {
2070 printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
2071 dev->name, err);
2072 } else
2073 dev->trans_start = jiffies;
2076 orinoco_spin_unlock(priv);
2078 return;
2079 disable:
2080 hermes_set_irqmask(hw, 0);
2081 netif_device_detach(dev);
2082 printk(KERN_ERR "%s: Device has been disabled!\n", dev->name);
2085 /********************************************************************/
2086 /* Interrupt handler */
2087 /********************************************************************/
2089 static void __orinoco_ev_tick(struct net_device *dev, hermes_t *hw)
2091 printk(KERN_DEBUG "%s: TICK\n", dev->name);
2094 static void __orinoco_ev_wterr(struct net_device *dev, hermes_t *hw)
2096 /* This seems to happen a fair bit under load, but ignoring it
2097 seems to work fine...*/
2098 printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
2099 dev->name);
2102 irqreturn_t orinoco_interrupt(int irq, void *dev_id)
2104 struct net_device *dev = dev_id;
2105 struct orinoco_private *priv = netdev_priv(dev);
2106 hermes_t *hw = &priv->hw;
2107 int count = MAX_IRQLOOPS_PER_IRQ;
2108 u16 evstat, events;
2109 /* These are used to detect a runaway interrupt situation */
2110 /* If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
2111 * we panic and shut down the hardware */
2112 static int last_irq_jiffy = 0; /* jiffies value the last time
2113 * we were called */
2114 static int loops_this_jiffy = 0;
2115 unsigned long flags;
2117 if (orinoco_lock(priv, &flags) != 0) {
2118 /* If hw is unavailable - we don't know if the irq was
2119 * for us or not */
2120 return IRQ_HANDLED;
2123 evstat = hermes_read_regn(hw, EVSTAT);
2124 events = evstat & hw->inten;
2125 if (! events) {
2126 orinoco_unlock(priv, &flags);
2127 return IRQ_NONE;
2130 if (jiffies != last_irq_jiffy)
2131 loops_this_jiffy = 0;
2132 last_irq_jiffy = jiffies;
2134 while (events && count--) {
2135 if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
2136 printk(KERN_WARNING "%s: IRQ handler is looping too "
2137 "much! Resetting.\n", dev->name);
2138 /* Disable interrupts for now */
2139 hermes_set_irqmask(hw, 0);
2140 schedule_work(&priv->reset_work);
2141 break;
2144 /* Check the card hasn't been removed */
2145 if (! hermes_present(hw)) {
2146 DEBUG(0, "orinoco_interrupt(): card removed\n");
2147 break;
2150 if (events & HERMES_EV_TICK)
2151 __orinoco_ev_tick(dev, hw);
2152 if (events & HERMES_EV_WTERR)
2153 __orinoco_ev_wterr(dev, hw);
2154 if (events & HERMES_EV_INFDROP)
2155 __orinoco_ev_infdrop(dev, hw);
2156 if (events & HERMES_EV_INFO)
2157 __orinoco_ev_info(dev, hw);
2158 if (events & HERMES_EV_RX)
2159 __orinoco_ev_rx(dev, hw);
2160 if (events & HERMES_EV_TXEXC)
2161 __orinoco_ev_txexc(dev, hw);
2162 if (events & HERMES_EV_TX)
2163 __orinoco_ev_tx(dev, hw);
2164 if (events & HERMES_EV_ALLOC)
2165 __orinoco_ev_alloc(dev, hw);
2167 hermes_write_regn(hw, EVACK, evstat);
2169 evstat = hermes_read_regn(hw, EVSTAT);
2170 events = evstat & hw->inten;
2173 orinoco_unlock(priv, &flags);
2174 return IRQ_HANDLED;
2177 /********************************************************************/
2178 /* Initialization */
2179 /********************************************************************/
2181 struct comp_id {
2182 u16 id, variant, major, minor;
2183 } __attribute__ ((packed));
2185 static inline fwtype_t determine_firmware_type(struct comp_id *nic_id)
2187 if (nic_id->id < 0x8000)
2188 return FIRMWARE_TYPE_AGERE;
2189 else if (nic_id->id == 0x8000 && nic_id->major == 0)
2190 return FIRMWARE_TYPE_SYMBOL;
2191 else
2192 return FIRMWARE_TYPE_INTERSIL;
2195 /* Set priv->firmware type, determine firmware properties */
2196 static int determine_firmware(struct net_device *dev)
2198 struct orinoco_private *priv = netdev_priv(dev);
2199 hermes_t *hw = &priv->hw;
2200 int err;
2201 struct comp_id nic_id, sta_id;
2202 unsigned int firmver;
2203 char tmp[SYMBOL_MAX_VER_LEN+1] __attribute__((aligned(2)));
2205 /* Get the hardware version */
2206 err = HERMES_READ_RECORD(hw, USER_BAP, HERMES_RID_NICID, &nic_id);
2207 if (err) {
2208 printk(KERN_ERR "%s: Cannot read hardware identity: error %d\n",
2209 dev->name, err);
2210 return err;
2213 le16_to_cpus(&nic_id.id);
2214 le16_to_cpus(&nic_id.variant);
2215 le16_to_cpus(&nic_id.major);
2216 le16_to_cpus(&nic_id.minor);
2217 printk(KERN_DEBUG "%s: Hardware identity %04x:%04x:%04x:%04x\n",
2218 dev->name, nic_id.id, nic_id.variant,
2219 nic_id.major, nic_id.minor);
2221 priv->firmware_type = determine_firmware_type(&nic_id);
2223 /* Get the firmware version */
2224 err = HERMES_READ_RECORD(hw, USER_BAP, HERMES_RID_STAID, &sta_id);
2225 if (err) {
2226 printk(KERN_ERR "%s: Cannot read station identity: error %d\n",
2227 dev->name, err);
2228 return err;
2231 le16_to_cpus(&sta_id.id);
2232 le16_to_cpus(&sta_id.variant);
2233 le16_to_cpus(&sta_id.major);
2234 le16_to_cpus(&sta_id.minor);
2235 printk(KERN_DEBUG "%s: Station identity %04x:%04x:%04x:%04x\n",
2236 dev->name, sta_id.id, sta_id.variant,
2237 sta_id.major, sta_id.minor);
2239 switch (sta_id.id) {
2240 case 0x15:
2241 printk(KERN_ERR "%s: Primary firmware is active\n",
2242 dev->name);
2243 return -ENODEV;
2244 case 0x14b:
2245 printk(KERN_ERR "%s: Tertiary firmware is active\n",
2246 dev->name);
2247 return -ENODEV;
2248 case 0x1f: /* Intersil, Agere, Symbol Spectrum24 */
2249 case 0x21: /* Symbol Spectrum24 Trilogy */
2250 break;
2251 default:
2252 printk(KERN_NOTICE "%s: Unknown station ID, please report\n",
2253 dev->name);
2254 break;
2257 /* Default capabilities */
2258 priv->has_sensitivity = 1;
2259 priv->has_mwo = 0;
2260 priv->has_preamble = 0;
2261 priv->has_port3 = 1;
2262 priv->has_ibss = 1;
2263 priv->has_wep = 0;
2264 priv->has_big_wep = 0;
2266 /* Determine capabilities from the firmware version */
2267 switch (priv->firmware_type) {
2268 case FIRMWARE_TYPE_AGERE:
2269 /* Lucent Wavelan IEEE, Lucent Orinoco, Cabletron RoamAbout,
2270 ELSA, Melco, HP, IBM, Dell 1150, Compaq 110/210 */
2271 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
2272 "Lucent/Agere %d.%02d", sta_id.major, sta_id.minor);
2274 firmver = ((unsigned long)sta_id.major << 16) | sta_id.minor;
2276 priv->has_ibss = (firmver >= 0x60006);
2277 priv->has_wep = (firmver >= 0x40020);
2278 priv->has_big_wep = 1; /* FIXME: this is wrong - how do we tell
2279 Gold cards from the others? */
2280 priv->has_mwo = (firmver >= 0x60000);
2281 priv->has_pm = (firmver >= 0x40020); /* Don't work in 7.52 ? */
2282 priv->ibss_port = 1;
2283 priv->has_hostscan = (firmver >= 0x8000a);
2284 priv->broken_monitor = (firmver >= 0x80000);
2286 /* Tested with Agere firmware :
2287 * 1.16 ; 4.08 ; 4.52 ; 6.04 ; 6.16 ; 7.28 => Jean II
2288 * Tested CableTron firmware : 4.32 => Anton */
2289 break;
2290 case FIRMWARE_TYPE_SYMBOL:
2291 /* Symbol , 3Com AirConnect, Intel, Ericsson WLAN */
2292 /* Intel MAC : 00:02:B3:* */
2293 /* 3Com MAC : 00:50:DA:* */
2294 memset(tmp, 0, sizeof(tmp));
2295 /* Get the Symbol firmware version */
2296 err = hw->ops->read_ltv(hw, USER_BAP,
2297 HERMES_RID_SECONDARYVERSION_SYMBOL,
2298 SYMBOL_MAX_VER_LEN, NULL, &tmp);
2299 if (err) {
2300 printk(KERN_WARNING
2301 "%s: Error %d reading Symbol firmware info. Wildly guessing capabilities...\n",
2302 dev->name, err);
2303 firmver = 0;
2304 tmp[0] = '\0';
2305 } else {
2306 /* The firmware revision is a string, the format is
2307 * something like : "V2.20-01".
2308 * Quick and dirty parsing... - Jean II
2310 firmver = ((tmp[1] - '0') << 16) | ((tmp[3] - '0') << 12)
2311 | ((tmp[4] - '0') << 8) | ((tmp[6] - '0') << 4)
2312 | (tmp[7] - '0');
2314 tmp[SYMBOL_MAX_VER_LEN] = '\0';
2317 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
2318 "Symbol %s", tmp);
2320 priv->has_ibss = (firmver >= 0x20000);
2321 priv->has_wep = (firmver >= 0x15012);
2322 priv->has_big_wep = (firmver >= 0x20000);
2323 priv->has_pm = (firmver >= 0x20000 && firmver < 0x22000) ||
2324 (firmver >= 0x29000 && firmver < 0x30000) ||
2325 firmver >= 0x31000;
2326 priv->has_preamble = (firmver >= 0x20000);
2327 priv->ibss_port = 4;
2328 priv->broken_disableport = (firmver == 0x25013) ||
2329 (firmver >= 0x30000 && firmver <= 0x31000);
2330 priv->has_hostscan = (firmver >= 0x31001) ||
2331 (firmver >= 0x29057 && firmver < 0x30000);
2332 /* Tested with Intel firmware : 0x20015 => Jean II */
2333 /* Tested with 3Com firmware : 0x15012 & 0x22001 => Jean II */
2334 break;
2335 case FIRMWARE_TYPE_INTERSIL:
2336 /* D-Link, Linksys, Adtron, ZoomAir, and many others...
2337 * Samsung, Compaq 100/200 and Proxim are slightly
2338 * different and less well tested */
2339 /* D-Link MAC : 00:40:05:* */
2340 /* Addtron MAC : 00:90:D1:* */
2341 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
2342 "Intersil %d.%d.%d", sta_id.major, sta_id.minor,
2343 sta_id.variant);
2345 firmver = ((unsigned long)sta_id.major << 16) |
2346 ((unsigned long)sta_id.minor << 8) | sta_id.variant;
2348 priv->has_ibss = (firmver >= 0x000700); /* FIXME */
2349 priv->has_big_wep = priv->has_wep = (firmver >= 0x000800);
2350 priv->has_pm = (firmver >= 0x000700);
2351 priv->has_hostscan = (firmver >= 0x010301);
2353 if (firmver >= 0x000800)
2354 priv->ibss_port = 0;
2355 else {
2356 printk(KERN_NOTICE "%s: Intersil firmware earlier "
2357 "than v0.8.x - several features not supported\n",
2358 dev->name);
2359 priv->ibss_port = 1;
2361 break;
2363 printk(KERN_DEBUG "%s: Firmware determined as %s\n", dev->name,
2364 priv->fw_name);
2366 return 0;
2369 static int orinoco_init(struct net_device *dev)
2371 struct orinoco_private *priv = netdev_priv(dev);
2372 hermes_t *hw = &priv->hw;
2373 int err = 0;
2374 struct hermes_idstring nickbuf;
2375 u16 reclen;
2376 int len;
2377 DECLARE_MAC_BUF(mac);
2379 /* No need to lock, the hw_unavailable flag is already set in
2380 * alloc_orinocodev() */
2381 priv->nicbuf_size = IEEE80211_MAX_FRAME_LEN + ETH_HLEN;
2383 /* Initialize the firmware */
2384 err = hw->ops->init(hw);
2385 if (err != 0) {
2386 printk(KERN_ERR "%s: failed to initialize firmware (err = %d)\n",
2387 dev->name, err);
2388 goto out;
2391 err = determine_firmware(dev);
2392 if (err != 0) {
2393 printk(KERN_ERR "%s: Incompatible firmware, aborting\n",
2394 dev->name);
2395 goto out;
2398 if (priv->has_port3)
2399 printk(KERN_DEBUG "%s: Ad-hoc demo mode supported\n", dev->name);
2400 if (priv->has_ibss)
2401 printk(KERN_DEBUG "%s: IEEE standard IBSS ad-hoc mode supported\n",
2402 dev->name);
2403 if (priv->has_wep) {
2404 printk(KERN_DEBUG "%s: WEP supported, ", dev->name);
2405 if (priv->has_big_wep)
2406 printk("104-bit key\n");
2407 else
2408 printk("40-bit key\n");
2411 /* Get the MAC address */
2412 err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
2413 ETH_ALEN, NULL, dev->dev_addr);
2414 if (err) {
2415 printk(KERN_WARNING "%s: failed to read MAC address!\n",
2416 dev->name);
2417 goto out;
2420 printk(KERN_DEBUG "%s: MAC address %s\n",
2421 dev->name, print_mac(mac, dev->dev_addr));
2423 /* Get the station name */
2424 err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
2425 sizeof(nickbuf), &reclen, &nickbuf);
2426 if (err) {
2427 printk(KERN_ERR "%s: failed to read station name\n",
2428 dev->name);
2429 goto out;
2431 if (nickbuf.len)
2432 len = min(IW_ESSID_MAX_SIZE, (int)le16_to_cpu(nickbuf.len));
2433 else
2434 len = min(IW_ESSID_MAX_SIZE, 2 * reclen);
2435 memcpy(priv->nick, &nickbuf.val, len);
2436 priv->nick[len] = '\0';
2438 printk(KERN_DEBUG "%s: Station name \"%s\"\n", dev->name, priv->nick);
2440 err = orinoco_allocate_fid(dev);
2441 if (err) {
2442 printk(KERN_ERR "%s: failed to allocate NIC buffer!\n",
2443 dev->name);
2444 goto out;
2447 /* Get allowed channels */
2448 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CHANNELLIST,
2449 &priv->channel_mask);
2450 if (err) {
2451 printk(KERN_ERR "%s: failed to read channel list!\n",
2452 dev->name);
2453 goto out;
2456 /* Get initial AP density */
2457 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFSYSTEMSCALE,
2458 &priv->ap_density);
2459 if (err || priv->ap_density < 1 || priv->ap_density > 3) {
2460 priv->has_sensitivity = 0;
2463 /* Get initial RTS threshold */
2464 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
2465 &priv->rts_thresh);
2466 if (err) {
2467 printk(KERN_ERR "%s: failed to read RTS threshold!\n",
2468 dev->name);
2469 goto out;
2472 /* Get initial fragmentation settings */
2473 if (priv->has_mwo)
2474 err = hermes_read_wordrec(hw, USER_BAP,
2475 HERMES_RID_CNFMWOROBUST_AGERE,
2476 &priv->mwo_robust);
2477 else
2478 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
2479 &priv->frag_thresh);
2480 if (err) {
2481 printk(KERN_ERR "%s: failed to read fragmentation settings!\n",
2482 dev->name);
2483 goto out;
2486 /* Power management setup */
2487 if (priv->has_pm) {
2488 priv->pm_on = 0;
2489 priv->pm_mcast = 1;
2490 err = hermes_read_wordrec(hw, USER_BAP,
2491 HERMES_RID_CNFMAXSLEEPDURATION,
2492 &priv->pm_period);
2493 if (err) {
2494 printk(KERN_ERR "%s: failed to read power management period!\n",
2495 dev->name);
2496 goto out;
2498 err = hermes_read_wordrec(hw, USER_BAP,
2499 HERMES_RID_CNFPMHOLDOVERDURATION,
2500 &priv->pm_timeout);
2501 if (err) {
2502 printk(KERN_ERR "%s: failed to read power management timeout!\n",
2503 dev->name);
2504 goto out;
2508 /* Preamble setup */
2509 if (priv->has_preamble) {
2510 err = hermes_read_wordrec(hw, USER_BAP,
2511 HERMES_RID_CNFPREAMBLE_SYMBOL,
2512 &priv->preamble);
2513 if (err)
2514 goto out;
2517 /* Set up the default configuration */
2518 priv->iw_mode = IW_MODE_INFRA;
2519 /* By default use IEEE/IBSS ad-hoc mode if we have it */
2520 priv->prefer_port3 = priv->has_port3 && (! priv->has_ibss);
2521 set_port_type(priv);
2522 priv->channel = 0; /* use firmware default */
2524 priv->promiscuous = 0;
2525 priv->wep_on = 0;
2526 priv->tx_key = 0;
2528 /* Make the hardware available, as long as it hasn't been
2529 * removed elsewhere (e.g. by PCMCIA hot unplug) */
2530 orinoco_spin_lock(priv);
2531 priv->hw_unavailable--;
2532 orinoco_spin_unlock(priv);
2534 printk(KERN_DEBUG "%s: ready\n", dev->name);
2536 out:
2537 return err;
2540 struct net_device *alloc_orinocodev(int sizeof_card,
2541 int (*hard_reset)(struct orinoco_private *))
2543 struct net_device *dev;
2544 struct orinoco_private *priv;
2546 dev = alloc_etherdev(sizeof(struct orinoco_private) + sizeof_card);
2547 if (! dev)
2548 return NULL;
2549 priv = netdev_priv(dev);
2550 priv->ndev = dev;
2551 if (sizeof_card)
2552 priv->card = (void *)((unsigned long)priv
2553 + sizeof(struct orinoco_private));
2554 else
2555 priv->card = NULL;
2557 if (orinoco_bss_data_allocate(priv))
2558 goto err_out_free;
2559 orinoco_bss_data_init(priv);
2561 /* Setup / override net_device fields */
2562 dev->init = orinoco_init;
2563 dev->hard_start_xmit = orinoco_xmit;
2564 dev->tx_timeout = orinoco_tx_timeout;
2565 dev->watchdog_timeo = HZ; /* 1 second timeout */
2566 dev->get_stats = orinoco_get_stats;
2567 dev->ethtool_ops = &orinoco_ethtool_ops;
2568 dev->wireless_handlers = (struct iw_handler_def *)&orinoco_handler_def;
2569 #ifdef WIRELESS_SPY
2570 priv->wireless_data.spy_data = &priv->spy_data;
2571 dev->wireless_data = &priv->wireless_data;
2572 #endif
2573 dev->change_mtu = orinoco_change_mtu;
2574 dev->set_multicast_list = orinoco_set_multicast_list;
2575 /* we use the default eth_mac_addr for setting the MAC addr */
2577 /* Set up default callbacks */
2578 dev->open = orinoco_open;
2579 dev->stop = orinoco_stop;
2580 priv->hard_reset = hard_reset;
2582 spin_lock_init(&priv->lock);
2583 priv->open = 0;
2584 priv->hw_unavailable = 1; /* orinoco_init() must clear this
2585 * before anything else touches the
2586 * hardware */
2587 INIT_WORK(&priv->reset_work, orinoco_reset);
2588 INIT_WORK(&priv->join_work, orinoco_join_ap);
2589 INIT_WORK(&priv->wevent_work, orinoco_send_wevents);
2591 netif_carrier_off(dev);
2592 priv->last_linkstatus = 0xffff;
2594 return dev;
2596 err_out_free:
2597 free_netdev(dev);
2598 return NULL;
2601 void free_orinocodev(struct net_device *dev)
2603 struct orinoco_private *priv = netdev_priv(dev);
2605 orinoco_bss_data_free(priv);
2606 free_netdev(dev);
2609 /********************************************************************/
2610 /* Wireless extensions */
2611 /********************************************************************/
2613 /* Return : < 0 -> error code ; >= 0 -> length */
2614 static int orinoco_hw_get_essid(struct orinoco_private *priv, int *active,
2615 char buf[IW_ESSID_MAX_SIZE+1])
2617 hermes_t *hw = &priv->hw;
2618 int err = 0;
2619 struct hermes_idstring essidbuf;
2620 char *p = (char *)(&essidbuf.val);
2621 int len;
2622 unsigned long flags;
2624 if (orinoco_lock(priv, &flags) != 0)
2625 return -EBUSY;
2627 if (strlen(priv->desired_essid) > 0) {
2628 /* We read the desired SSID from the hardware rather
2629 than from priv->desired_essid, just in case the
2630 firmware is allowed to change it on us. I'm not
2631 sure about this */
2632 /* My guess is that the OWNSSID should always be whatever
2633 * we set to the card, whereas CURRENT_SSID is the one that
2634 * may change... - Jean II */
2635 u16 rid;
2637 *active = 1;
2639 rid = (priv->port_type == 3) ? HERMES_RID_CNFOWNSSID :
2640 HERMES_RID_CNFDESIREDSSID;
2642 err = hw->ops->read_ltv(hw, USER_BAP, rid, sizeof(essidbuf),
2643 NULL, &essidbuf);
2644 if (err)
2645 goto fail_unlock;
2646 } else {
2647 *active = 0;
2649 err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CURRENTSSID,
2650 sizeof(essidbuf), NULL, &essidbuf);
2651 if (err)
2652 goto fail_unlock;
2655 len = le16_to_cpu(essidbuf.len);
2656 BUG_ON(len > IW_ESSID_MAX_SIZE);
2658 memset(buf, 0, IW_ESSID_MAX_SIZE);
2659 memcpy(buf, p, len);
2660 err = len;
2662 fail_unlock:
2663 orinoco_unlock(priv, &flags);
2665 return err;
2668 static long orinoco_hw_get_freq(struct orinoco_private *priv)
2671 hermes_t *hw = &priv->hw;
2672 int err = 0;
2673 u16 channel;
2674 long freq = 0;
2675 unsigned long flags;
2677 if (orinoco_lock(priv, &flags) != 0)
2678 return -EBUSY;
2680 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CURRENTCHANNEL, &channel);
2681 if (err)
2682 goto out;
2684 /* Intersil firmware 1.3.5 returns 0 when the interface is down */
2685 if (channel == 0) {
2686 err = -EBUSY;
2687 goto out;
2690 if ( (channel < 1) || (channel > NUM_CHANNELS) ) {
2691 printk(KERN_WARNING "%s: Channel out of range (%d)!\n",
2692 priv->ndev->name, channel);
2693 err = -EBUSY;
2694 goto out;
2697 freq = channel_frequency[channel-1] * 100000;
2699 out:
2700 orinoco_unlock(priv, &flags);
2702 if (err > 0)
2703 err = -EBUSY;
2704 return err ? err : freq;
2707 static int orinoco_hw_get_bitratelist(struct orinoco_private *priv,
2708 int *numrates, s32 *rates, int max)
2710 hermes_t *hw = &priv->hw;
2711 struct hermes_idstring list;
2712 unsigned char *p = (unsigned char *)&list.val;
2713 int err = 0;
2714 int num;
2715 int i;
2716 unsigned long flags;
2718 if (orinoco_lock(priv, &flags) != 0)
2719 return -EBUSY;
2721 err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_SUPPORTEDDATARATES,
2722 sizeof(list), NULL, &list);
2723 orinoco_unlock(priv, &flags);
2725 if (err)
2726 return err;
2728 num = le16_to_cpu(list.len);
2729 *numrates = num;
2730 num = min(num, max);
2732 for (i = 0; i < num; i++) {
2733 rates[i] = (p[i] & 0x7f) * 500000; /* convert to bps */
2736 return 0;
2739 static int orinoco_ioctl_getname(struct net_device *dev,
2740 struct iw_request_info *info,
2741 char *name,
2742 char *extra)
2744 struct orinoco_private *priv = netdev_priv(dev);
2745 int numrates;
2746 int err;
2748 err = orinoco_hw_get_bitratelist(priv, &numrates, NULL, 0);
2750 if (!err && (numrates > 2))
2751 strcpy(name, "IEEE 802.11b");
2752 else
2753 strcpy(name, "IEEE 802.11-DS");
2755 return 0;
2758 static int orinoco_ioctl_setwap(struct net_device *dev,
2759 struct iw_request_info *info,
2760 struct sockaddr *ap_addr,
2761 char *extra)
2763 struct orinoco_private *priv = netdev_priv(dev);
2764 int err = -EINPROGRESS; /* Call commit handler */
2765 unsigned long flags;
2766 static const u8 off_addr[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
2767 static const u8 any_addr[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
2769 if (orinoco_lock(priv, &flags) != 0)
2770 return -EBUSY;
2772 /* Enable automatic roaming - no sanity checks are needed */
2773 if (memcmp(&ap_addr->sa_data, off_addr, ETH_ALEN) == 0 ||
2774 memcmp(&ap_addr->sa_data, any_addr, ETH_ALEN) == 0) {
2775 priv->bssid_fixed = 0;
2776 memset(priv->desired_bssid, 0, ETH_ALEN);
2778 /* "off" means keep existing connection */
2779 if (ap_addr->sa_data[0] == 0) {
2780 __orinoco_hw_set_wap(priv);
2781 err = 0;
2783 goto out;
2786 if (priv->firmware_type == FIRMWARE_TYPE_AGERE) {
2787 printk(KERN_WARNING "%s: Lucent/Agere firmware doesn't "
2788 "support manual roaming\n",
2789 dev->name);
2790 err = -EOPNOTSUPP;
2791 goto out;
2794 if (priv->iw_mode != IW_MODE_INFRA) {
2795 printk(KERN_WARNING "%s: Manual roaming supported only in "
2796 "managed mode\n", dev->name);
2797 err = -EOPNOTSUPP;
2798 goto out;
2801 /* Intersil firmware hangs without Desired ESSID */
2802 if (priv->firmware_type == FIRMWARE_TYPE_INTERSIL &&
2803 strlen(priv->desired_essid) == 0) {
2804 printk(KERN_WARNING "%s: Desired ESSID must be set for "
2805 "manual roaming\n", dev->name);
2806 err = -EOPNOTSUPP;
2807 goto out;
2810 /* Finally, enable manual roaming */
2811 priv->bssid_fixed = 1;
2812 memcpy(priv->desired_bssid, &ap_addr->sa_data, ETH_ALEN);
2814 out:
2815 orinoco_unlock(priv, &flags);
2816 return err;
2819 static int orinoco_ioctl_getwap(struct net_device *dev,
2820 struct iw_request_info *info,
2821 struct sockaddr *ap_addr,
2822 char *extra)
2824 struct orinoco_private *priv = netdev_priv(dev);
2826 hermes_t *hw = &priv->hw;
2827 int err = 0;
2828 unsigned long flags;
2830 if (orinoco_lock(priv, &flags) != 0)
2831 return -EBUSY;
2833 ap_addr->sa_family = ARPHRD_ETHER;
2834 err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
2835 ETH_ALEN, NULL, ap_addr->sa_data);
2837 orinoco_unlock(priv, &flags);
2839 return err;
2842 static int orinoco_ioctl_setmode(struct net_device *dev,
2843 struct iw_request_info *info,
2844 u32 *mode,
2845 char *extra)
2847 struct orinoco_private *priv = netdev_priv(dev);
2848 int err = -EINPROGRESS; /* Call commit handler */
2849 unsigned long flags;
2851 if (priv->iw_mode == *mode)
2852 return 0;
2854 if (orinoco_lock(priv, &flags) != 0)
2855 return -EBUSY;
2857 switch (*mode) {
2858 case IW_MODE_ADHOC:
2859 if (!priv->has_ibss && !priv->has_port3)
2860 err = -EOPNOTSUPP;
2861 break;
2863 case IW_MODE_INFRA:
2864 break;
2866 case IW_MODE_MONITOR:
2867 if (priv->broken_monitor && !force_monitor) {
2868 printk(KERN_WARNING "%s: Monitor mode support is "
2869 "buggy in this firmware, not enabling\n",
2870 dev->name);
2871 err = -EOPNOTSUPP;
2873 break;
2875 default:
2876 err = -EOPNOTSUPP;
2877 break;
2880 if (err == -EINPROGRESS) {
2881 priv->iw_mode = *mode;
2882 set_port_type(priv);
2885 orinoco_unlock(priv, &flags);
2887 return err;
2890 static int orinoco_ioctl_getmode(struct net_device *dev,
2891 struct iw_request_info *info,
2892 u32 *mode,
2893 char *extra)
2895 struct orinoco_private *priv = netdev_priv(dev);
2897 *mode = priv->iw_mode;
2898 return 0;
2901 static int orinoco_ioctl_getiwrange(struct net_device *dev,
2902 struct iw_request_info *info,
2903 struct iw_point *rrq,
2904 char *extra)
2906 struct orinoco_private *priv = netdev_priv(dev);
2907 int err = 0;
2908 struct iw_range *range = (struct iw_range *) extra;
2909 int numrates;
2910 int i, k;
2912 rrq->length = sizeof(struct iw_range);
2913 memset(range, 0, sizeof(struct iw_range));
2915 range->we_version_compiled = WIRELESS_EXT;
2916 range->we_version_source = 14;
2918 /* Set available channels/frequencies */
2919 range->num_channels = NUM_CHANNELS;
2920 k = 0;
2921 for (i = 0; i < NUM_CHANNELS; i++) {
2922 if (priv->channel_mask & (1 << i)) {
2923 range->freq[k].i = i + 1;
2924 range->freq[k].m = channel_frequency[i] * 100000;
2925 range->freq[k].e = 1;
2926 k++;
2929 if (k >= IW_MAX_FREQUENCIES)
2930 break;
2932 range->num_frequency = k;
2933 range->sensitivity = 3;
2935 if (priv->has_wep) {
2936 range->max_encoding_tokens = ORINOCO_MAX_KEYS;
2937 range->encoding_size[0] = SMALL_KEY_SIZE;
2938 range->num_encoding_sizes = 1;
2940 if (priv->has_big_wep) {
2941 range->encoding_size[1] = LARGE_KEY_SIZE;
2942 range->num_encoding_sizes = 2;
2946 if ((priv->iw_mode == IW_MODE_ADHOC) && (!SPY_NUMBER(priv))){
2947 /* Quality stats meaningless in ad-hoc mode */
2948 } else {
2949 range->max_qual.qual = 0x8b - 0x2f;
2950 range->max_qual.level = 0x2f - 0x95 - 1;
2951 range->max_qual.noise = 0x2f - 0x95 - 1;
2952 /* Need to get better values */
2953 range->avg_qual.qual = 0x24;
2954 range->avg_qual.level = 0xC2;
2955 range->avg_qual.noise = 0x9E;
2958 err = orinoco_hw_get_bitratelist(priv, &numrates,
2959 range->bitrate, IW_MAX_BITRATES);
2960 if (err)
2961 return err;
2962 range->num_bitrates = numrates;
2964 /* Set an indication of the max TCP throughput in bit/s that we can
2965 * expect using this interface. May be use for QoS stuff...
2966 * Jean II */
2967 if (numrates > 2)
2968 range->throughput = 5 * 1000 * 1000; /* ~5 Mb/s */
2969 else
2970 range->throughput = 1.5 * 1000 * 1000; /* ~1.5 Mb/s */
2972 range->min_rts = 0;
2973 range->max_rts = 2347;
2974 range->min_frag = 256;
2975 range->max_frag = 2346;
2977 range->min_pmp = 0;
2978 range->max_pmp = 65535000;
2979 range->min_pmt = 0;
2980 range->max_pmt = 65535 * 1000; /* ??? */
2981 range->pmp_flags = IW_POWER_PERIOD;
2982 range->pmt_flags = IW_POWER_TIMEOUT;
2983 range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_UNICAST_R;
2985 range->retry_capa = IW_RETRY_LIMIT | IW_RETRY_LIFETIME;
2986 range->retry_flags = IW_RETRY_LIMIT;
2987 range->r_time_flags = IW_RETRY_LIFETIME;
2988 range->min_retry = 0;
2989 range->max_retry = 65535; /* ??? */
2990 range->min_r_time = 0;
2991 range->max_r_time = 65535 * 1000; /* ??? */
2993 /* Event capability (kernel) */
2994 IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
2995 /* Event capability (driver) */
2996 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWTHRSPY);
2997 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
2998 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
2999 IW_EVENT_CAPA_SET(range->event_capa, IWEVTXDROP);
3001 return 0;
3004 static int orinoco_ioctl_setiwencode(struct net_device *dev,
3005 struct iw_request_info *info,
3006 struct iw_point *erq,
3007 char *keybuf)
3009 struct orinoco_private *priv = netdev_priv(dev);
3010 int index = (erq->flags & IW_ENCODE_INDEX) - 1;
3011 int setindex = priv->tx_key;
3012 int enable = priv->wep_on;
3013 int restricted = priv->wep_restrict;
3014 u16 xlen = 0;
3015 int err = -EINPROGRESS; /* Call commit handler */
3016 unsigned long flags;
3018 if (! priv->has_wep)
3019 return -EOPNOTSUPP;
3021 if (erq->pointer) {
3022 /* We actually have a key to set - check its length */
3023 if (erq->length > LARGE_KEY_SIZE)
3024 return -E2BIG;
3026 if ( (erq->length > SMALL_KEY_SIZE) && !priv->has_big_wep )
3027 return -E2BIG;
3030 if (orinoco_lock(priv, &flags) != 0)
3031 return -EBUSY;
3033 if (erq->length > 0) {
3034 if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
3035 index = priv->tx_key;
3037 /* Adjust key length to a supported value */
3038 if (erq->length > SMALL_KEY_SIZE) {
3039 xlen = LARGE_KEY_SIZE;
3040 } else if (erq->length > 0) {
3041 xlen = SMALL_KEY_SIZE;
3042 } else
3043 xlen = 0;
3045 /* Switch on WEP if off */
3046 if ((!enable) && (xlen > 0)) {
3047 setindex = index;
3048 enable = 1;
3050 } else {
3051 /* Important note : if the user do "iwconfig eth0 enc off",
3052 * we will arrive there with an index of -1. This is valid
3053 * but need to be taken care off... Jean II */
3054 if ((index < 0) || (index >= ORINOCO_MAX_KEYS)) {
3055 if((index != -1) || (erq->flags == 0)) {
3056 err = -EINVAL;
3057 goto out;
3059 } else {
3060 /* Set the index : Check that the key is valid */
3061 if(priv->keys[index].len == 0) {
3062 err = -EINVAL;
3063 goto out;
3065 setindex = index;
3069 if (erq->flags & IW_ENCODE_DISABLED)
3070 enable = 0;
3071 if (erq->flags & IW_ENCODE_OPEN)
3072 restricted = 0;
3073 if (erq->flags & IW_ENCODE_RESTRICTED)
3074 restricted = 1;
3076 if (erq->pointer && erq->length > 0) {
3077 priv->keys[index].len = cpu_to_le16(xlen);
3078 memset(priv->keys[index].data, 0,
3079 sizeof(priv->keys[index].data));
3080 memcpy(priv->keys[index].data, keybuf, erq->length);
3082 priv->tx_key = setindex;
3084 /* Try fast key change if connected and only keys are changed */
3085 if (priv->wep_on && enable && (priv->wep_restrict == restricted) &&
3086 netif_carrier_ok(dev)) {
3087 err = __orinoco_hw_setup_wepkeys(priv);
3088 /* No need to commit if successful */
3089 goto out;
3092 priv->wep_on = enable;
3093 priv->wep_restrict = restricted;
3095 out:
3096 orinoco_unlock(priv, &flags);
3098 return err;
3101 static int orinoco_ioctl_getiwencode(struct net_device *dev,
3102 struct iw_request_info *info,
3103 struct iw_point *erq,
3104 char *keybuf)
3106 struct orinoco_private *priv = netdev_priv(dev);
3107 int index = (erq->flags & IW_ENCODE_INDEX) - 1;
3108 u16 xlen = 0;
3109 unsigned long flags;
3111 if (! priv->has_wep)
3112 return -EOPNOTSUPP;
3114 if (orinoco_lock(priv, &flags) != 0)
3115 return -EBUSY;
3117 if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
3118 index = priv->tx_key;
3120 erq->flags = 0;
3121 if (! priv->wep_on)
3122 erq->flags |= IW_ENCODE_DISABLED;
3123 erq->flags |= index + 1;
3125 if (priv->wep_restrict)
3126 erq->flags |= IW_ENCODE_RESTRICTED;
3127 else
3128 erq->flags |= IW_ENCODE_OPEN;
3130 xlen = le16_to_cpu(priv->keys[index].len);
3132 erq->length = xlen;
3134 memcpy(keybuf, priv->keys[index].data, ORINOCO_MAX_KEY_SIZE);
3136 orinoco_unlock(priv, &flags);
3137 return 0;
3140 static int orinoco_ioctl_setessid(struct net_device *dev,
3141 struct iw_request_info *info,
3142 struct iw_point *erq,
3143 char *essidbuf)
3145 struct orinoco_private *priv = netdev_priv(dev);
3146 unsigned long flags;
3148 /* Note : ESSID is ignored in Ad-Hoc demo mode, but we can set it
3149 * anyway... - Jean II */
3151 /* Hum... Should not use Wireless Extension constant (may change),
3152 * should use our own... - Jean II */
3153 if (erq->length > IW_ESSID_MAX_SIZE)
3154 return -E2BIG;
3156 if (orinoco_lock(priv, &flags) != 0)
3157 return -EBUSY;
3159 /* NULL the string (for NULL termination & ESSID = ANY) - Jean II */
3160 memset(priv->desired_essid, 0, sizeof(priv->desired_essid));
3162 /* If not ANY, get the new ESSID */
3163 if (erq->flags) {
3164 memcpy(priv->desired_essid, essidbuf, erq->length);
3167 orinoco_unlock(priv, &flags);
3169 return -EINPROGRESS; /* Call commit handler */
3172 static int orinoco_ioctl_getessid(struct net_device *dev,
3173 struct iw_request_info *info,
3174 struct iw_point *erq,
3175 char *essidbuf)
3177 struct orinoco_private *priv = netdev_priv(dev);
3178 int active;
3179 int err = 0;
3180 unsigned long flags;
3182 if (netif_running(dev)) {
3183 err = orinoco_hw_get_essid(priv, &active, essidbuf);
3184 if (err < 0)
3185 return err;
3186 erq->length = err;
3187 } else {
3188 if (orinoco_lock(priv, &flags) != 0)
3189 return -EBUSY;
3190 memcpy(essidbuf, priv->desired_essid, IW_ESSID_MAX_SIZE);
3191 erq->length = strlen(priv->desired_essid);
3192 orinoco_unlock(priv, &flags);
3195 erq->flags = 1;
3197 return 0;
3200 static int orinoco_ioctl_setnick(struct net_device *dev,
3201 struct iw_request_info *info,
3202 struct iw_point *nrq,
3203 char *nickbuf)
3205 struct orinoco_private *priv = netdev_priv(dev);
3206 unsigned long flags;
3208 if (nrq->length > IW_ESSID_MAX_SIZE)
3209 return -E2BIG;
3211 if (orinoco_lock(priv, &flags) != 0)
3212 return -EBUSY;
3214 memset(priv->nick, 0, sizeof(priv->nick));
3215 memcpy(priv->nick, nickbuf, nrq->length);
3217 orinoco_unlock(priv, &flags);
3219 return -EINPROGRESS; /* Call commit handler */
3222 static int orinoco_ioctl_getnick(struct net_device *dev,
3223 struct iw_request_info *info,
3224 struct iw_point *nrq,
3225 char *nickbuf)
3227 struct orinoco_private *priv = netdev_priv(dev);
3228 unsigned long flags;
3230 if (orinoco_lock(priv, &flags) != 0)
3231 return -EBUSY;
3233 memcpy(nickbuf, priv->nick, IW_ESSID_MAX_SIZE);
3234 orinoco_unlock(priv, &flags);
3236 nrq->length = strlen(priv->nick);
3238 return 0;
3241 static int orinoco_ioctl_setfreq(struct net_device *dev,
3242 struct iw_request_info *info,
3243 struct iw_freq *frq,
3244 char *extra)
3246 struct orinoco_private *priv = netdev_priv(dev);
3247 int chan = -1;
3248 unsigned long flags;
3249 int err = -EINPROGRESS; /* Call commit handler */
3251 /* In infrastructure mode the AP sets the channel */
3252 if (priv->iw_mode == IW_MODE_INFRA)
3253 return -EBUSY;
3255 if ( (frq->e == 0) && (frq->m <= 1000) ) {
3256 /* Setting by channel number */
3257 chan = frq->m;
3258 } else {
3259 /* Setting by frequency - search the table */
3260 int mult = 1;
3261 int i;
3263 for (i = 0; i < (6 - frq->e); i++)
3264 mult *= 10;
3266 for (i = 0; i < NUM_CHANNELS; i++)
3267 if (frq->m == (channel_frequency[i] * mult))
3268 chan = i+1;
3271 if ( (chan < 1) || (chan > NUM_CHANNELS) ||
3272 ! (priv->channel_mask & (1 << (chan-1)) ) )
3273 return -EINVAL;
3275 if (orinoco_lock(priv, &flags) != 0)
3276 return -EBUSY;
3278 priv->channel = chan;
3279 if (priv->iw_mode == IW_MODE_MONITOR) {
3280 /* Fast channel change - no commit if successful */
3281 hermes_t *hw = &priv->hw;
3282 err = hw->ops->cmd_wait(hw, HERMES_CMD_TEST |
3283 HERMES_TEST_SET_CHANNEL,
3284 chan, NULL);
3286 orinoco_unlock(priv, &flags);
3288 return err;
3291 static int orinoco_ioctl_getfreq(struct net_device *dev,
3292 struct iw_request_info *info,
3293 struct iw_freq *frq,
3294 char *extra)
3296 struct orinoco_private *priv = netdev_priv(dev);
3297 int tmp;
3299 /* Locking done in there */
3300 tmp = orinoco_hw_get_freq(priv);
3301 if (tmp < 0) {
3302 return tmp;
3305 frq->m = tmp;
3306 frq->e = 1;
3308 return 0;
3311 static int orinoco_ioctl_getsens(struct net_device *dev,
3312 struct iw_request_info *info,
3313 struct iw_param *srq,
3314 char *extra)
3316 struct orinoco_private *priv = netdev_priv(dev);
3317 hermes_t *hw = &priv->hw;
3318 u16 val;
3319 int err;
3320 unsigned long flags;
3322 if (!priv->has_sensitivity)
3323 return -EOPNOTSUPP;
3325 if (orinoco_lock(priv, &flags) != 0)
3326 return -EBUSY;
3327 err = hermes_read_wordrec(hw, USER_BAP,
3328 HERMES_RID_CNFSYSTEMSCALE, &val);
3329 orinoco_unlock(priv, &flags);
3331 if (err)
3332 return err;
3334 srq->value = val;
3335 srq->fixed = 0; /* auto */
3337 return 0;
3340 static int orinoco_ioctl_setsens(struct net_device *dev,
3341 struct iw_request_info *info,
3342 struct iw_param *srq,
3343 char *extra)
3345 struct orinoco_private *priv = netdev_priv(dev);
3346 int val = srq->value;
3347 unsigned long flags;
3349 if (!priv->has_sensitivity)
3350 return -EOPNOTSUPP;
3352 if ((val < 1) || (val > 3))
3353 return -EINVAL;
3355 if (orinoco_lock(priv, &flags) != 0)
3356 return -EBUSY;
3357 priv->ap_density = val;
3358 orinoco_unlock(priv, &flags);
3360 return -EINPROGRESS; /* Call commit handler */
3363 static int orinoco_ioctl_setrts(struct net_device *dev,
3364 struct iw_request_info *info,
3365 struct iw_param *rrq,
3366 char *extra)
3368 struct orinoco_private *priv = netdev_priv(dev);
3369 int val = rrq->value;
3370 unsigned long flags;
3372 if (rrq->disabled)
3373 val = 2347;
3375 if ( (val < 0) || (val > 2347) )
3376 return -EINVAL;
3378 if (orinoco_lock(priv, &flags) != 0)
3379 return -EBUSY;
3381 priv->rts_thresh = val;
3382 orinoco_unlock(priv, &flags);
3384 return -EINPROGRESS; /* Call commit handler */
3387 static int orinoco_ioctl_getrts(struct net_device *dev,
3388 struct iw_request_info *info,
3389 struct iw_param *rrq,
3390 char *extra)
3392 struct orinoco_private *priv = netdev_priv(dev);
3394 rrq->value = priv->rts_thresh;
3395 rrq->disabled = (rrq->value == 2347);
3396 rrq->fixed = 1;
3398 return 0;
3401 static int orinoco_ioctl_setfrag(struct net_device *dev,
3402 struct iw_request_info *info,
3403 struct iw_param *frq,
3404 char *extra)
3406 struct orinoco_private *priv = netdev_priv(dev);
3407 int err = -EINPROGRESS; /* Call commit handler */
3408 unsigned long flags;
3410 if (orinoco_lock(priv, &flags) != 0)
3411 return -EBUSY;
3413 if (priv->has_mwo) {
3414 if (frq->disabled)
3415 priv->mwo_robust = 0;
3416 else {
3417 if (frq->fixed)
3418 printk(KERN_WARNING "%s: Fixed fragmentation is "
3419 "not supported on this firmware. "
3420 "Using MWO robust instead.\n", dev->name);
3421 priv->mwo_robust = 1;
3423 } else {
3424 if (frq->disabled)
3425 priv->frag_thresh = 2346;
3426 else {
3427 if ( (frq->value < 256) || (frq->value > 2346) )
3428 err = -EINVAL;
3429 else
3430 priv->frag_thresh = frq->value & ~0x1; /* must be even */
3434 orinoco_unlock(priv, &flags);
3436 return err;
3439 static int orinoco_ioctl_getfrag(struct net_device *dev,
3440 struct iw_request_info *info,
3441 struct iw_param *frq,
3442 char *extra)
3444 struct orinoco_private *priv = netdev_priv(dev);
3445 hermes_t *hw = &priv->hw;
3446 int err;
3447 u16 val;
3448 unsigned long flags;
3450 if (orinoco_lock(priv, &flags) != 0)
3451 return -EBUSY;
3453 if (priv->has_mwo) {
3454 err = hermes_read_wordrec(hw, USER_BAP,
3455 HERMES_RID_CNFMWOROBUST_AGERE,
3456 &val);
3457 if (err)
3458 val = 0;
3460 frq->value = val ? 2347 : 0;
3461 frq->disabled = ! val;
3462 frq->fixed = 0;
3463 } else {
3464 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
3465 &val);
3466 if (err)
3467 val = 0;
3469 frq->value = val;
3470 frq->disabled = (val >= 2346);
3471 frq->fixed = 1;
3474 orinoco_unlock(priv, &flags);
3476 return err;
3479 static int orinoco_ioctl_setrate(struct net_device *dev,
3480 struct iw_request_info *info,
3481 struct iw_param *rrq,
3482 char *extra)
3484 struct orinoco_private *priv = netdev_priv(dev);
3485 int ratemode = -1;
3486 int bitrate; /* 100s of kilobits */
3487 int i;
3488 unsigned long flags;
3490 /* As the user space doesn't know our highest rate, it uses -1
3491 * to ask us to set the highest rate. Test it using "iwconfig
3492 * ethX rate auto" - Jean II */
3493 if (rrq->value == -1)
3494 bitrate = 110;
3495 else {
3496 if (rrq->value % 100000)
3497 return -EINVAL;
3498 bitrate = rrq->value / 100000;
3501 if ( (bitrate != 10) && (bitrate != 20) &&
3502 (bitrate != 55) && (bitrate != 110) )
3503 return -EINVAL;
3505 for (i = 0; i < BITRATE_TABLE_SIZE; i++)
3506 if ( (bitrate_table[i].bitrate == bitrate) &&
3507 (bitrate_table[i].automatic == ! rrq->fixed) ) {
3508 ratemode = i;
3509 break;
3512 if (ratemode == -1)
3513 return -EINVAL;
3515 if (orinoco_lock(priv, &flags) != 0)
3516 return -EBUSY;
3517 priv->bitratemode = ratemode;
3518 orinoco_unlock(priv, &flags);
3520 return -EINPROGRESS;
3523 static int orinoco_ioctl_getrate(struct net_device *dev,
3524 struct iw_request_info *info,
3525 struct iw_param *rrq,
3526 char *extra)
3528 struct orinoco_private *priv = netdev_priv(dev);
3529 hermes_t *hw = &priv->hw;
3530 int err = 0;
3531 int ratemode;
3532 int i;
3533 u16 val;
3534 unsigned long flags;
3536 if (orinoco_lock(priv, &flags) != 0)
3537 return -EBUSY;
3539 ratemode = priv->bitratemode;
3541 BUG_ON((ratemode < 0) || (ratemode >= BITRATE_TABLE_SIZE));
3543 rrq->value = bitrate_table[ratemode].bitrate * 100000;
3544 rrq->fixed = ! bitrate_table[ratemode].automatic;
3545 rrq->disabled = 0;
3547 /* If the interface is running we try to find more about the
3548 current mode */
3549 if (netif_running(dev)) {
3550 err = hermes_read_wordrec(hw, USER_BAP,
3551 HERMES_RID_CURRENTTXRATE, &val);
3552 if (err)
3553 goto out;
3555 switch (priv->firmware_type) {
3556 case FIRMWARE_TYPE_AGERE: /* Lucent style rate */
3557 /* Note : in Lucent firmware, the return value of
3558 * HERMES_RID_CURRENTTXRATE is the bitrate in Mb/s,
3559 * and therefore is totally different from the
3560 * encoding of HERMES_RID_CNFTXRATECONTROL.
3561 * Don't forget that 6Mb/s is really 5.5Mb/s */
3562 if (val == 6)
3563 rrq->value = 5500000;
3564 else
3565 rrq->value = val * 1000000;
3566 break;
3567 case FIRMWARE_TYPE_INTERSIL: /* Intersil style rate */
3568 case FIRMWARE_TYPE_SYMBOL: /* Symbol style rate */
3569 for (i = 0; i < BITRATE_TABLE_SIZE; i++)
3570 if (bitrate_table[i].intersil_txratectrl == val) {
3571 ratemode = i;
3572 break;
3574 if (i >= BITRATE_TABLE_SIZE)
3575 printk(KERN_INFO "%s: Unable to determine current bitrate (0x%04hx)\n",
3576 dev->name, val);
3578 rrq->value = bitrate_table[ratemode].bitrate * 100000;
3579 break;
3580 default:
3581 BUG();
3585 out:
3586 orinoco_unlock(priv, &flags);
3588 return err;
3591 static int orinoco_ioctl_setpower(struct net_device *dev,
3592 struct iw_request_info *info,
3593 struct iw_param *prq,
3594 char *extra)
3596 struct orinoco_private *priv = netdev_priv(dev);
3597 int err = -EINPROGRESS; /* Call commit handler */
3598 unsigned long flags;
3600 if (orinoco_lock(priv, &flags) != 0)
3601 return -EBUSY;
3603 if (prq->disabled) {
3604 priv->pm_on = 0;
3605 } else {
3606 switch (prq->flags & IW_POWER_MODE) {
3607 case IW_POWER_UNICAST_R:
3608 priv->pm_mcast = 0;
3609 priv->pm_on = 1;
3610 break;
3611 case IW_POWER_ALL_R:
3612 priv->pm_mcast = 1;
3613 priv->pm_on = 1;
3614 break;
3615 case IW_POWER_ON:
3616 /* No flags : but we may have a value - Jean II */
3617 break;
3618 default:
3619 err = -EINVAL;
3620 goto out;
3623 if (prq->flags & IW_POWER_TIMEOUT) {
3624 priv->pm_on = 1;
3625 priv->pm_timeout = prq->value / 1000;
3627 if (prq->flags & IW_POWER_PERIOD) {
3628 priv->pm_on = 1;
3629 priv->pm_period = prq->value / 1000;
3631 /* It's valid to not have a value if we are just toggling
3632 * the flags... Jean II */
3633 if(!priv->pm_on) {
3634 err = -EINVAL;
3635 goto out;
3639 out:
3640 orinoco_unlock(priv, &flags);
3642 return err;
3645 static int orinoco_ioctl_getpower(struct net_device *dev,
3646 struct iw_request_info *info,
3647 struct iw_param *prq,
3648 char *extra)
3650 struct orinoco_private *priv = netdev_priv(dev);
3651 hermes_t *hw = &priv->hw;
3652 int err = 0;
3653 u16 enable, period, timeout, mcast;
3654 unsigned long flags;
3656 if (orinoco_lock(priv, &flags) != 0)
3657 return -EBUSY;
3659 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFPMENABLED, &enable);
3660 if (err)
3661 goto out;
3663 err = hermes_read_wordrec(hw, USER_BAP,
3664 HERMES_RID_CNFMAXSLEEPDURATION, &period);
3665 if (err)
3666 goto out;
3668 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFPMHOLDOVERDURATION, &timeout);
3669 if (err)
3670 goto out;
3672 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFMULTICASTRECEIVE, &mcast);
3673 if (err)
3674 goto out;
3676 prq->disabled = !enable;
3677 /* Note : by default, display the period */
3678 if ((prq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
3679 prq->flags = IW_POWER_TIMEOUT;
3680 prq->value = timeout * 1000;
3681 } else {
3682 prq->flags = IW_POWER_PERIOD;
3683 prq->value = period * 1000;
3685 if (mcast)
3686 prq->flags |= IW_POWER_ALL_R;
3687 else
3688 prq->flags |= IW_POWER_UNICAST_R;
3690 out:
3691 orinoco_unlock(priv, &flags);
3693 return err;
3696 static int orinoco_ioctl_getretry(struct net_device *dev,
3697 struct iw_request_info *info,
3698 struct iw_param *rrq,
3699 char *extra)
3701 struct orinoco_private *priv = netdev_priv(dev);
3702 hermes_t *hw = &priv->hw;
3703 int err = 0;
3704 u16 short_limit, long_limit, lifetime;
3705 unsigned long flags;
3707 if (orinoco_lock(priv, &flags) != 0)
3708 return -EBUSY;
3710 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_SHORTRETRYLIMIT,
3711 &short_limit);
3712 if (err)
3713 goto out;
3715 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_LONGRETRYLIMIT,
3716 &long_limit);
3717 if (err)
3718 goto out;
3720 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_MAXTRANSMITLIFETIME,
3721 &lifetime);
3722 if (err)
3723 goto out;
3725 rrq->disabled = 0; /* Can't be disabled */
3727 /* Note : by default, display the retry number */
3728 if ((rrq->flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME) {
3729 rrq->flags = IW_RETRY_LIFETIME;
3730 rrq->value = lifetime * 1000; /* ??? */
3731 } else {
3732 /* By default, display the min number */
3733 if ((rrq->flags & IW_RETRY_LONG)) {
3734 rrq->flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
3735 rrq->value = long_limit;
3736 } else {
3737 rrq->flags = IW_RETRY_LIMIT;
3738 rrq->value = short_limit;
3739 if(short_limit != long_limit)
3740 rrq->flags |= IW_RETRY_SHORT;
3744 out:
3745 orinoco_unlock(priv, &flags);
3747 return err;
3750 static int orinoco_ioctl_reset(struct net_device *dev,
3751 struct iw_request_info *info,
3752 void *wrqu,
3753 char *extra)
3755 struct orinoco_private *priv = netdev_priv(dev);
3757 if (! capable(CAP_NET_ADMIN))
3758 return -EPERM;
3760 if (info->cmd == (SIOCIWFIRSTPRIV + 0x1)) {
3761 printk(KERN_DEBUG "%s: Forcing reset!\n", dev->name);
3763 /* Firmware reset */
3764 orinoco_reset(&priv->reset_work);
3765 } else {
3766 printk(KERN_DEBUG "%s: Force scheduling reset!\n", dev->name);
3768 schedule_work(&priv->reset_work);
3771 return 0;
3774 static int orinoco_ioctl_setibssport(struct net_device *dev,
3775 struct iw_request_info *info,
3776 void *wrqu,
3777 char *extra)
3780 struct orinoco_private *priv = netdev_priv(dev);
3781 int val = *( (int *) extra );
3782 unsigned long flags;
3784 if (orinoco_lock(priv, &flags) != 0)
3785 return -EBUSY;
3787 priv->ibss_port = val ;
3789 /* Actually update the mode we are using */
3790 set_port_type(priv);
3792 orinoco_unlock(priv, &flags);
3793 return -EINPROGRESS; /* Call commit handler */
3796 static int orinoco_ioctl_getibssport(struct net_device *dev,
3797 struct iw_request_info *info,
3798 void *wrqu,
3799 char *extra)
3801 struct orinoco_private *priv = netdev_priv(dev);
3802 int *val = (int *) extra;
3804 *val = priv->ibss_port;
3805 return 0;
3808 static int orinoco_ioctl_setport3(struct net_device *dev,
3809 struct iw_request_info *info,
3810 void *wrqu,
3811 char *extra)
3813 struct orinoco_private *priv = netdev_priv(dev);
3814 int val = *( (int *) extra );
3815 int err = 0;
3816 unsigned long flags;
3818 if (orinoco_lock(priv, &flags) != 0)
3819 return -EBUSY;
3821 switch (val) {
3822 case 0: /* Try to do IEEE ad-hoc mode */
3823 if (! priv->has_ibss) {
3824 err = -EINVAL;
3825 break;
3827 priv->prefer_port3 = 0;
3829 break;
3831 case 1: /* Try to do Lucent proprietary ad-hoc mode */
3832 if (! priv->has_port3) {
3833 err = -EINVAL;
3834 break;
3836 priv->prefer_port3 = 1;
3837 break;
3839 default:
3840 err = -EINVAL;
3843 if (! err) {
3844 /* Actually update the mode we are using */
3845 set_port_type(priv);
3846 err = -EINPROGRESS;
3849 orinoco_unlock(priv, &flags);
3851 return err;
3854 static int orinoco_ioctl_getport3(struct net_device *dev,
3855 struct iw_request_info *info,
3856 void *wrqu,
3857 char *extra)
3859 struct orinoco_private *priv = netdev_priv(dev);
3860 int *val = (int *) extra;
3862 *val = priv->prefer_port3;
3863 return 0;
3866 static int orinoco_ioctl_setpreamble(struct net_device *dev,
3867 struct iw_request_info *info,
3868 void *wrqu,
3869 char *extra)
3871 struct orinoco_private *priv = netdev_priv(dev);
3872 unsigned long flags;
3873 int val;
3875 if (! priv->has_preamble)
3876 return -EOPNOTSUPP;
3878 /* 802.11b has recently defined some short preamble.
3879 * Basically, the Phy header has been reduced in size.
3880 * This increase performance, especially at high rates
3881 * (the preamble is transmitted at 1Mb/s), unfortunately
3882 * this give compatibility troubles... - Jean II */
3883 val = *( (int *) extra );
3885 if (orinoco_lock(priv, &flags) != 0)
3886 return -EBUSY;
3888 if (val)
3889 priv->preamble = 1;
3890 else
3891 priv->preamble = 0;
3893 orinoco_unlock(priv, &flags);
3895 return -EINPROGRESS; /* Call commit handler */
3898 static int orinoco_ioctl_getpreamble(struct net_device *dev,
3899 struct iw_request_info *info,
3900 void *wrqu,
3901 char *extra)
3903 struct orinoco_private *priv = netdev_priv(dev);
3904 int *val = (int *) extra;
3906 if (! priv->has_preamble)
3907 return -EOPNOTSUPP;
3909 *val = priv->preamble;
3910 return 0;
3913 /* ioctl interface to hermes_read_ltv()
3914 * To use with iwpriv, pass the RID as the token argument, e.g.
3915 * iwpriv get_rid [0xfc00]
3916 * At least Wireless Tools 25 is required to use iwpriv.
3917 * For Wireless Tools 25 and 26 append "dummy" are the end. */
3918 static int orinoco_ioctl_getrid(struct net_device *dev,
3919 struct iw_request_info *info,
3920 struct iw_point *data,
3921 char *extra)
3923 struct orinoco_private *priv = netdev_priv(dev);
3924 hermes_t *hw = &priv->hw;
3925 int rid = data->flags;
3926 u16 length;
3927 int err;
3928 unsigned long flags;
3930 /* It's a "get" function, but we don't want users to access the
3931 * WEP key and other raw firmware data */
3932 if (! capable(CAP_NET_ADMIN))
3933 return -EPERM;
3935 if (rid < 0xfc00 || rid > 0xffff)
3936 return -EINVAL;
3938 if (orinoco_lock(priv, &flags) != 0)
3939 return -EBUSY;
3941 err = hw->ops->read_ltv(hw, USER_BAP, rid, MAX_RID_LEN, &length,
3942 extra);
3943 if (err)
3944 goto out;
3946 data->length = min_t(u16, HERMES_RECLEN_TO_BYTES(length),
3947 MAX_RID_LEN);
3949 out:
3950 orinoco_unlock(priv, &flags);
3951 return err;
3954 /* Trigger a scan (look for other cells in the vicinity */
3955 static int orinoco_ioctl_setscan(struct net_device *dev,
3956 struct iw_request_info *info,
3957 struct iw_param *srq,
3958 char *extra)
3960 struct orinoco_private *priv = netdev_priv(dev);
3961 hermes_t *hw = &priv->hw;
3962 int err = 0;
3963 unsigned long flags;
3965 /* Note : you may have realised that, as this is a SET operation,
3966 * this is privileged and therefore a normal user can't
3967 * perform scanning.
3968 * This is not an error, while the device perform scanning,
3969 * traffic doesn't flow, so it's a perfect DoS...
3970 * Jean II */
3972 if (orinoco_lock(priv, &flags) != 0)
3973 return -EBUSY;
3975 /* Scanning with port 0 disabled would fail */
3976 if (!netif_running(dev)) {
3977 err = -ENETDOWN;
3978 goto out;
3981 /* In monitor mode, the scan results are always empty.
3982 * Probe responses are passed to the driver as received
3983 * frames and could be processed in software. */
3984 if (priv->iw_mode == IW_MODE_MONITOR) {
3985 err = -EOPNOTSUPP;
3986 goto out;
3989 /* Note : because we don't lock out the irq handler, the way
3990 * we access scan variables in priv is critical.
3991 * o scan_inprogress : not touched by irq handler
3992 * o scan_mode : not touched by irq handler
3993 * o scan_len : synchronised with scan_result
3994 * Before modifying anything on those variables, please think hard !
3995 * Jean II */
3997 /* Save flags */
3998 priv->scan_mode = srq->flags;
4000 /* Always trigger scanning, even if it's in progress.
4001 * This way, if the info frame get lost, we will recover somewhat
4002 * gracefully - Jean II */
4004 if (priv->has_hostscan) {
4005 switch (priv->firmware_type) {
4006 case FIRMWARE_TYPE_SYMBOL:
4007 err = hermes_write_wordrec(hw, USER_BAP,
4008 HERMES_RID_CNFHOSTSCAN_SYMBOL,
4009 HERMES_HOSTSCAN_SYMBOL_ONCE |
4010 HERMES_HOSTSCAN_SYMBOL_BCAST);
4011 break;
4012 case FIRMWARE_TYPE_INTERSIL: {
4013 __le16 req[3];
4015 req[0] = cpu_to_le16(0x3fff); /* All channels */
4016 req[1] = cpu_to_le16(0x0001); /* rate 1 Mbps */
4017 req[2] = 0; /* Any ESSID */
4018 err = HERMES_WRITE_RECORD(hw, USER_BAP,
4019 HERMES_RID_CNFHOSTSCAN, &req);
4021 break;
4022 case FIRMWARE_TYPE_AGERE:
4023 err = hermes_write_wordrec(hw, USER_BAP,
4024 HERMES_RID_CNFSCANSSID_AGERE,
4025 0); /* Any ESSID */
4026 if (err)
4027 break;
4029 err = hermes_inquire(hw, HERMES_INQ_SCAN);
4030 break;
4032 } else
4033 err = hermes_inquire(hw, HERMES_INQ_SCAN);
4035 /* One more client */
4036 if (! err)
4037 priv->scan_inprogress = 1;
4039 out:
4040 orinoco_unlock(priv, &flags);
4041 return err;
4044 #define MAX_CUSTOM_LEN 64
4046 /* Translate scan data returned from the card to a card independant
4047 * format that the Wireless Tools will understand - Jean II
4048 * Return message length or -errno for fatal errors */
4049 static inline char *orinoco_translate_scan(struct net_device *dev,
4050 struct iw_request_info *info,
4051 char *current_ev,
4052 char *end_buf,
4053 union hermes_scan_info *bss,
4054 unsigned int last_scanned)
4056 struct orinoco_private *priv = netdev_priv(dev);
4057 u16 capabilities;
4058 u16 channel;
4059 struct iw_event iwe; /* Temporary buffer */
4060 char *p;
4061 char custom[MAX_CUSTOM_LEN];
4063 /* First entry *MUST* be the AP MAC address */
4064 iwe.cmd = SIOCGIWAP;
4065 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
4066 memcpy(iwe.u.ap_addr.sa_data, bss->a.bssid, ETH_ALEN);
4067 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
4068 &iwe, IW_EV_ADDR_LEN);
4070 /* Other entries will be displayed in the order we give them */
4072 /* Add the ESSID */
4073 iwe.u.data.length = le16_to_cpu(bss->a.essid_len);
4074 if (iwe.u.data.length > 32)
4075 iwe.u.data.length = 32;
4076 iwe.cmd = SIOCGIWESSID;
4077 iwe.u.data.flags = 1;
4078 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4079 &iwe, bss->a.essid);
4081 /* Add mode */
4082 iwe.cmd = SIOCGIWMODE;
4083 capabilities = le16_to_cpu(bss->a.capabilities);
4084 if (capabilities & 0x3) {
4085 if (capabilities & 0x1)
4086 iwe.u.mode = IW_MODE_MASTER;
4087 else
4088 iwe.u.mode = IW_MODE_ADHOC;
4089 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
4090 &iwe, IW_EV_UINT_LEN);
4093 channel = bss->s.channel;
4094 if ((channel >= 1) && (channel <= NUM_CHANNELS)) {
4095 /* Add frequency */
4096 iwe.cmd = SIOCGIWFREQ;
4097 iwe.u.freq.m = channel_frequency[channel-1] * 100000;
4098 iwe.u.freq.e = 1;
4099 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
4100 &iwe, IW_EV_FREQ_LEN);
4103 /* Add quality statistics */
4104 iwe.cmd = IWEVQUAL;
4105 iwe.u.qual.updated = 0x10; /* no link quality */
4106 iwe.u.qual.level = (__u8) le16_to_cpu(bss->a.level) - 0x95;
4107 iwe.u.qual.noise = (__u8) le16_to_cpu(bss->a.noise) - 0x95;
4108 /* Wireless tools prior to 27.pre22 will show link quality
4109 * anyway, so we provide a reasonable value. */
4110 if (iwe.u.qual.level > iwe.u.qual.noise)
4111 iwe.u.qual.qual = iwe.u.qual.level - iwe.u.qual.noise;
4112 else
4113 iwe.u.qual.qual = 0;
4114 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
4115 &iwe, IW_EV_QUAL_LEN);
4117 /* Add encryption capability */
4118 iwe.cmd = SIOCGIWENCODE;
4119 if (capabilities & 0x10)
4120 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
4121 else
4122 iwe.u.data.flags = IW_ENCODE_DISABLED;
4123 iwe.u.data.length = 0;
4124 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4125 &iwe, bss->a.essid);
4127 /* Add EXTRA: Age to display seconds since last beacon/probe response
4128 * for given network. */
4129 iwe.cmd = IWEVCUSTOM;
4130 p = custom;
4131 p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
4132 " Last beacon: %dms ago",
4133 jiffies_to_msecs(jiffies - last_scanned));
4134 iwe.u.data.length = p - custom;
4135 if (iwe.u.data.length)
4136 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4137 &iwe, custom);
4139 /* Bit rate is not available in Lucent/Agere firmwares */
4140 if (priv->firmware_type != FIRMWARE_TYPE_AGERE) {
4141 char *current_val = current_ev + iwe_stream_lcp_len(info);
4142 int i;
4143 int step;
4145 if (priv->firmware_type == FIRMWARE_TYPE_SYMBOL)
4146 step = 2;
4147 else
4148 step = 1;
4150 iwe.cmd = SIOCGIWRATE;
4151 /* Those two flags are ignored... */
4152 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
4153 /* Max 10 values */
4154 for (i = 0; i < 10; i += step) {
4155 /* NULL terminated */
4156 if (bss->p.rates[i] == 0x0)
4157 break;
4158 /* Bit rate given in 500 kb/s units (+ 0x80) */
4159 iwe.u.bitrate.value = ((bss->p.rates[i] & 0x7f) * 500000);
4160 current_val = iwe_stream_add_value(info, current_ev,
4161 current_val,
4162 end_buf, &iwe,
4163 IW_EV_PARAM_LEN);
4165 /* Check if we added any event */
4166 if ((current_val - current_ev) > iwe_stream_lcp_len(info))
4167 current_ev = current_val;
4170 return current_ev;
4173 /* Return results of a scan */
4174 static int orinoco_ioctl_getscan(struct net_device *dev,
4175 struct iw_request_info *info,
4176 struct iw_point *srq,
4177 char *extra)
4179 struct orinoco_private *priv = netdev_priv(dev);
4180 bss_element *bss;
4181 int err = 0;
4182 unsigned long flags;
4183 char *current_ev = extra;
4185 if (orinoco_lock(priv, &flags) != 0)
4186 return -EBUSY;
4188 if (priv->scan_inprogress) {
4189 /* Important note : we don't want to block the caller
4190 * until results are ready for various reasons.
4191 * First, managing wait queues is complex and racy.
4192 * Second, we grab some rtnetlink lock before comming
4193 * here (in dev_ioctl()).
4194 * Third, we generate an Wireless Event, so the
4195 * caller can wait itself on that - Jean II */
4196 err = -EAGAIN;
4197 goto out;
4200 list_for_each_entry(bss, &priv->bss_list, list) {
4201 /* Translate to WE format this entry */
4202 current_ev = orinoco_translate_scan(dev, info, current_ev,
4203 extra + srq->length,
4204 &bss->bss,
4205 bss->last_scanned);
4207 /* Check if there is space for one more entry */
4208 if ((extra + srq->length - current_ev) <= IW_EV_ADDR_LEN) {
4209 /* Ask user space to try again with a bigger buffer */
4210 err = -E2BIG;
4211 goto out;
4215 srq->length = (current_ev - extra);
4216 srq->flags = (__u16) priv->scan_mode;
4218 out:
4219 orinoco_unlock(priv, &flags);
4220 return err;
4223 /* Commit handler, called after set operations */
4224 static int orinoco_ioctl_commit(struct net_device *dev,
4225 struct iw_request_info *info,
4226 void *wrqu,
4227 char *extra)
4229 struct orinoco_private *priv = netdev_priv(dev);
4230 struct hermes *hw = &priv->hw;
4231 unsigned long flags;
4232 int err = 0;
4234 if (!priv->open)
4235 return 0;
4237 if (priv->broken_disableport) {
4238 orinoco_reset(&priv->reset_work);
4239 return 0;
4242 if (orinoco_lock(priv, &flags) != 0)
4243 return err;
4245 err = hermes_disable_port(hw, 0);
4246 if (err) {
4247 printk(KERN_WARNING "%s: Unable to disable port "
4248 "while reconfiguring card\n", dev->name);
4249 priv->broken_disableport = 1;
4250 goto out;
4253 err = __orinoco_program_rids(dev);
4254 if (err) {
4255 printk(KERN_WARNING "%s: Unable to reconfigure card\n",
4256 dev->name);
4257 goto out;
4260 err = hermes_enable_port(hw, 0);
4261 if (err) {
4262 printk(KERN_WARNING "%s: Unable to enable port while reconfiguring card\n",
4263 dev->name);
4264 goto out;
4267 out:
4268 if (err) {
4269 printk(KERN_WARNING "%s: Resetting instead...\n", dev->name);
4270 schedule_work(&priv->reset_work);
4271 err = 0;
4274 orinoco_unlock(priv, &flags);
4275 return err;
4278 static const struct iw_priv_args orinoco_privtab[] = {
4279 { SIOCIWFIRSTPRIV + 0x0, 0, 0, "force_reset" },
4280 { SIOCIWFIRSTPRIV + 0x1, 0, 0, "card_reset" },
4281 { SIOCIWFIRSTPRIV + 0x2, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
4282 0, "set_port3" },
4283 { SIOCIWFIRSTPRIV + 0x3, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
4284 "get_port3" },
4285 { SIOCIWFIRSTPRIV + 0x4, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
4286 0, "set_preamble" },
4287 { SIOCIWFIRSTPRIV + 0x5, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
4288 "get_preamble" },
4289 { SIOCIWFIRSTPRIV + 0x6, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
4290 0, "set_ibssport" },
4291 { SIOCIWFIRSTPRIV + 0x7, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
4292 "get_ibssport" },
4293 { SIOCIWFIRSTPRIV + 0x9, 0, IW_PRIV_TYPE_BYTE | MAX_RID_LEN,
4294 "get_rid" },
4299 * Structures to export the Wireless Handlers
4302 static const iw_handler orinoco_handler[] = {
4303 [SIOCSIWCOMMIT-SIOCIWFIRST] = (iw_handler) orinoco_ioctl_commit,
4304 [SIOCGIWNAME -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getname,
4305 [SIOCSIWFREQ -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setfreq,
4306 [SIOCGIWFREQ -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getfreq,
4307 [SIOCSIWMODE -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setmode,
4308 [SIOCGIWMODE -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getmode,
4309 [SIOCSIWSENS -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setsens,
4310 [SIOCGIWSENS -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getsens,
4311 [SIOCGIWRANGE -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getiwrange,
4312 [SIOCSIWSPY -SIOCIWFIRST] = (iw_handler) iw_handler_set_spy,
4313 [SIOCGIWSPY -SIOCIWFIRST] = (iw_handler) iw_handler_get_spy,
4314 [SIOCSIWTHRSPY-SIOCIWFIRST] = (iw_handler) iw_handler_set_thrspy,
4315 [SIOCGIWTHRSPY-SIOCIWFIRST] = (iw_handler) iw_handler_get_thrspy,
4316 [SIOCSIWAP -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setwap,
4317 [SIOCGIWAP -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getwap,
4318 [SIOCSIWSCAN -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setscan,
4319 [SIOCGIWSCAN -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getscan,
4320 [SIOCSIWESSID -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setessid,
4321 [SIOCGIWESSID -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getessid,
4322 [SIOCSIWNICKN -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setnick,
4323 [SIOCGIWNICKN -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getnick,
4324 [SIOCSIWRATE -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setrate,
4325 [SIOCGIWRATE -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getrate,
4326 [SIOCSIWRTS -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setrts,
4327 [SIOCGIWRTS -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getrts,
4328 [SIOCSIWFRAG -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setfrag,
4329 [SIOCGIWFRAG -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getfrag,
4330 [SIOCGIWRETRY -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getretry,
4331 [SIOCSIWENCODE-SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setiwencode,
4332 [SIOCGIWENCODE-SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getiwencode,
4333 [SIOCSIWPOWER -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setpower,
4334 [SIOCGIWPOWER -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getpower,
4339 Added typecasting since we no longer use iwreq_data -- Moustafa
4341 static const iw_handler orinoco_private_handler[] = {
4342 [0] = (iw_handler) orinoco_ioctl_reset,
4343 [1] = (iw_handler) orinoco_ioctl_reset,
4344 [2] = (iw_handler) orinoco_ioctl_setport3,
4345 [3] = (iw_handler) orinoco_ioctl_getport3,
4346 [4] = (iw_handler) orinoco_ioctl_setpreamble,
4347 [5] = (iw_handler) orinoco_ioctl_getpreamble,
4348 [6] = (iw_handler) orinoco_ioctl_setibssport,
4349 [7] = (iw_handler) orinoco_ioctl_getibssport,
4350 [9] = (iw_handler) orinoco_ioctl_getrid,
4353 static const struct iw_handler_def orinoco_handler_def = {
4354 .num_standard = ARRAY_SIZE(orinoco_handler),
4355 .num_private = ARRAY_SIZE(orinoco_private_handler),
4356 .num_private_args = ARRAY_SIZE(orinoco_privtab),
4357 .standard = orinoco_handler,
4358 .private = orinoco_private_handler,
4359 .private_args = orinoco_privtab,
4360 .get_wireless_stats = orinoco_get_wireless_stats,
4363 static void orinoco_get_drvinfo(struct net_device *dev,
4364 struct ethtool_drvinfo *info)
4366 struct orinoco_private *priv = netdev_priv(dev);
4368 strncpy(info->driver, DRIVER_NAME, sizeof(info->driver) - 1);
4369 strncpy(info->version, DRIVER_VERSION, sizeof(info->version) - 1);
4370 strncpy(info->fw_version, priv->fw_name, sizeof(info->fw_version) - 1);
4373 static const struct ethtool_ops orinoco_ethtool_ops = {
4374 .get_drvinfo = orinoco_get_drvinfo,
4375 .get_link = ethtool_op_get_link,
4378 /********************************************************************/
4379 /* Module initialization */
4380 /********************************************************************/
4382 EXPORT_SYMBOL(alloc_orinocodev);
4383 EXPORT_SYMBOL(free_orinocodev);
4385 EXPORT_SYMBOL(__orinoco_up);
4386 EXPORT_SYMBOL(__orinoco_down);
4387 EXPORT_SYMBOL(orinoco_reinit_firmware);
4389 EXPORT_SYMBOL(orinoco_interrupt);
4391 /* Can't be declared "const" or the whole __initdata section will
4392 * become const */
4393 static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
4394 " (David Gibson <hermes@gibson.dropbear.id.au>, "
4395 "Pavel Roskin <proski@gnu.org>, et al)";
4397 static int __init init_orinoco(void)
4399 printk(KERN_DEBUG "%s\n", version);
4400 return 0;
4403 static void __exit exit_orinoco(void)
4407 module_init(init_orinoco);
4408 module_exit(exit_orinoco);