orinoco: encapsulate driver locking
[orinoco_usb.git] / drivers / net / wireless / orinoco / orinoco.c
blobc0f90cb05448930925092cbd30a98fba82ee0248
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/delay.h>
83 #include <linux/netdevice.h>
84 #include <linux/etherdevice.h>
85 #include <linux/ethtool.h>
86 #include <linux/firmware.h>
87 #include <linux/suspend.h>
88 #include <linux/if_arp.h>
89 #include <linux/wireless.h>
90 #include <linux/ieee80211.h>
91 #include <net/iw_handler.h>
93 #include <linux/scatterlist.h>
94 #include <linux/crypto.h>
96 #include "hermes_rid.h"
97 #include "hermes_dld.h"
98 #include "orinoco.h"
100 /********************************************************************/
101 /* Module information */
102 /********************************************************************/
104 MODULE_AUTHOR("Pavel Roskin <proski@gnu.org> & David Gibson <hermes@gibson.dropbear.id.au>");
105 MODULE_DESCRIPTION("Driver for Lucent Orinoco, Prism II based and similar wireless cards");
106 MODULE_LICENSE("Dual MPL/GPL");
108 /* Level of debugging. Used in the macros in orinoco.h */
109 #ifdef ORINOCO_DEBUG
110 int orinoco_debug = ORINOCO_DEBUG;
111 module_param(orinoco_debug, int, 0644);
112 MODULE_PARM_DESC(orinoco_debug, "Debug level");
113 EXPORT_SYMBOL(orinoco_debug);
114 #endif
116 static int suppress_linkstatus; /* = 0 */
117 module_param(suppress_linkstatus, bool, 0644);
118 MODULE_PARM_DESC(suppress_linkstatus, "Don't log link status changes");
119 static int ignore_disconnect; /* = 0 */
120 module_param(ignore_disconnect, int, 0644);
121 MODULE_PARM_DESC(ignore_disconnect, "Don't report lost link to the network layer");
123 static int force_monitor; /* = 0 */
124 module_param(force_monitor, int, 0644);
125 MODULE_PARM_DESC(force_monitor, "Allow monitor mode for all firmware versions");
127 /********************************************************************/
128 /* Compile time configuration and compatibility stuff */
129 /********************************************************************/
131 /* We do this this way to avoid ifdefs in the actual code */
132 #ifdef WIRELESS_SPY
133 #define SPY_NUMBER(priv) (priv->spy_data.spy_number)
134 #else
135 #define SPY_NUMBER(priv) 0
136 #endif /* WIRELESS_SPY */
138 /********************************************************************/
139 /* Internal constants */
140 /********************************************************************/
142 /* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
143 static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
144 #define ENCAPS_OVERHEAD (sizeof(encaps_hdr) + 2)
146 #define ORINOCO_MIN_MTU 256
147 #define ORINOCO_MAX_MTU (IEEE80211_MAX_DATA_LEN - ENCAPS_OVERHEAD)
149 #define SYMBOL_MAX_VER_LEN (14)
150 #define USER_BAP 0
151 #define IRQ_BAP 1
152 #define MAX_IRQLOOPS_PER_IRQ 10
153 #define MAX_IRQLOOPS_PER_JIFFY (20000/HZ) /* Based on a guestimate of
154 * how many events the
155 * device could
156 * legitimately generate */
157 #define SMALL_KEY_SIZE 5
158 #define LARGE_KEY_SIZE 13
159 #define TX_NICBUF_SIZE_BUG 1585 /* Bug in Symbol firmware */
161 #define DUMMY_FID 0xFFFF
163 /*#define MAX_MULTICAST(priv) (priv->firmware_type == FIRMWARE_TYPE_AGERE ? \
164 HERMES_MAX_MULTICAST : 0)*/
165 #define MAX_MULTICAST(priv) (HERMES_MAX_MULTICAST)
167 #define ORINOCO_INTEN (HERMES_EV_RX | HERMES_EV_ALLOC \
168 | HERMES_EV_TX | HERMES_EV_TXEXC \
169 | HERMES_EV_WTERR | HERMES_EV_INFO \
170 | HERMES_EV_INFDROP )
172 #define MAX_RID_LEN 1024
174 static const struct iw_handler_def orinoco_handler_def;
175 static const struct ethtool_ops orinoco_ethtool_ops;
177 /********************************************************************/
178 /* Data tables */
179 /********************************************************************/
181 /* The frequency of each channel in MHz */
182 static const long channel_frequency[] = {
183 2412, 2417, 2422, 2427, 2432, 2437, 2442,
184 2447, 2452, 2457, 2462, 2467, 2472, 2484
186 #define NUM_CHANNELS ARRAY_SIZE(channel_frequency)
188 /* This tables gives the actual meanings of the bitrate IDs returned
189 * by the firmware. */
190 static struct {
191 int bitrate; /* in 100s of kilobits */
192 int automatic;
193 u16 agere_txratectrl;
194 u16 intersil_txratectrl;
195 } bitrate_table[] = {
196 {110, 1, 3, 15}, /* Entry 0 is the default */
197 {10, 0, 1, 1},
198 {10, 1, 1, 1},
199 {20, 0, 2, 2},
200 {20, 1, 6, 3},
201 {55, 0, 4, 4},
202 {55, 1, 7, 7},
203 {110, 0, 5, 8},
205 #define BITRATE_TABLE_SIZE ARRAY_SIZE(bitrate_table)
207 /********************************************************************/
208 /* Data types */
209 /********************************************************************/
211 /* Beginning of the Tx descriptor, used in TxExc handling */
212 struct hermes_txexc_data {
213 struct hermes_tx_descriptor desc;
214 __le16 frame_ctl;
215 __le16 duration_id;
216 u8 addr1[ETH_ALEN];
217 } __attribute__ ((packed));
219 /* Rx frame header except compatibility 802.3 header */
220 struct hermes_rx_descriptor {
221 /* Control */
222 __le16 status;
223 __le32 time;
224 u8 silence;
225 u8 signal;
226 u8 rate;
227 u8 rxflow;
228 __le32 reserved;
230 /* 802.11 header */
231 __le16 frame_ctl;
232 __le16 duration_id;
233 u8 addr1[ETH_ALEN];
234 u8 addr2[ETH_ALEN];
235 u8 addr3[ETH_ALEN];
236 __le16 seq_ctl;
237 u8 addr4[ETH_ALEN];
239 /* Data length */
240 __le16 data_len;
241 } __attribute__ ((packed));
243 /********************************************************************/
244 /* Function prototypes */
245 /********************************************************************/
247 static int __orinoco_program_rids(struct net_device *dev);
248 static void __orinoco_set_multicast_list(struct net_device *dev);
250 /********************************************************************/
251 /* Michael MIC crypto setup */
252 /********************************************************************/
253 #define MICHAEL_MIC_LEN 8
254 static int orinoco_mic_init(struct orinoco_private *priv)
256 priv->tx_tfm_mic = crypto_alloc_hash("michael_mic", 0, 0);
257 if (IS_ERR(priv->tx_tfm_mic)) {
258 printk(KERN_DEBUG "orinoco_mic_init: could not allocate "
259 "crypto API michael_mic\n");
260 priv->tx_tfm_mic = NULL;
261 return -ENOMEM;
264 priv->rx_tfm_mic = crypto_alloc_hash("michael_mic", 0, 0);
265 if (IS_ERR(priv->rx_tfm_mic)) {
266 printk(KERN_DEBUG "orinoco_mic_init: could not allocate "
267 "crypto API michael_mic\n");
268 priv->rx_tfm_mic = NULL;
269 return -ENOMEM;
272 return 0;
275 static void orinoco_mic_free(struct orinoco_private *priv)
277 if (priv->tx_tfm_mic)
278 crypto_free_hash(priv->tx_tfm_mic);
279 if (priv->rx_tfm_mic)
280 crypto_free_hash(priv->rx_tfm_mic);
283 static int michael_mic(struct crypto_hash *tfm_michael, u8 *key,
284 u8 *da, u8 *sa, u8 priority,
285 u8 *data, size_t data_len, u8 *mic)
287 struct hash_desc desc;
288 struct scatterlist sg[2];
289 u8 hdr[ETH_HLEN + 2]; /* size of header + padding */
291 if (tfm_michael == NULL) {
292 printk(KERN_WARNING "michael_mic: tfm_michael == NULL\n");
293 return -1;
296 /* Copy header into buffer. We need the padding on the end zeroed */
297 memcpy(&hdr[0], da, ETH_ALEN);
298 memcpy(&hdr[ETH_ALEN], sa, ETH_ALEN);
299 hdr[ETH_ALEN*2] = priority;
300 hdr[ETH_ALEN*2+1] = 0;
301 hdr[ETH_ALEN*2+2] = 0;
302 hdr[ETH_ALEN*2+3] = 0;
304 /* Use scatter gather to MIC header and data in one go */
305 sg_init_table(sg, 2);
306 sg_set_buf(&sg[0], hdr, sizeof(hdr));
307 sg_set_buf(&sg[1], data, data_len);
309 if (crypto_hash_setkey(tfm_michael, key, MIC_KEYLEN))
310 return -1;
312 desc.tfm = tfm_michael;
313 desc.flags = 0;
314 return crypto_hash_digest(&desc, sg, data_len + sizeof(hdr),
315 mic);
318 /********************************************************************/
319 /* Internal helper functions */
320 /********************************************************************/
322 static inline void set_port_type(struct orinoco_private *priv)
324 switch (priv->iw_mode) {
325 case IW_MODE_INFRA:
326 priv->port_type = 1;
327 priv->createibss = 0;
328 break;
329 case IW_MODE_ADHOC:
330 if (priv->prefer_port3) {
331 priv->port_type = 3;
332 priv->createibss = 0;
333 } else {
334 priv->port_type = priv->ibss_port;
335 priv->createibss = 1;
337 break;
338 case IW_MODE_MONITOR:
339 priv->port_type = 3;
340 priv->createibss = 0;
341 break;
342 default:
343 printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
344 priv->ndev->name);
348 #define ORINOCO_MAX_BSS_COUNT 64
349 static int orinoco_bss_data_allocate(struct orinoco_private *priv)
351 if (priv->bss_xbss_data)
352 return 0;
354 if (priv->has_ext_scan)
355 priv->bss_xbss_data = kzalloc(ORINOCO_MAX_BSS_COUNT *
356 sizeof(struct xbss_element),
357 GFP_KERNEL);
358 else
359 priv->bss_xbss_data = kzalloc(ORINOCO_MAX_BSS_COUNT *
360 sizeof(struct bss_element),
361 GFP_KERNEL);
363 if (!priv->bss_xbss_data) {
364 printk(KERN_WARNING "Out of memory allocating beacons");
365 return -ENOMEM;
367 return 0;
370 static void orinoco_bss_data_free(struct orinoco_private *priv)
372 kfree(priv->bss_xbss_data);
373 priv->bss_xbss_data = NULL;
376 #define PRIV_BSS ((struct bss_element *)priv->bss_xbss_data)
377 #define PRIV_XBSS ((struct xbss_element *)priv->bss_xbss_data)
378 static void orinoco_bss_data_init(struct orinoco_private *priv)
380 int i;
382 INIT_LIST_HEAD(&priv->bss_free_list);
383 INIT_LIST_HEAD(&priv->bss_list);
384 if (priv->has_ext_scan)
385 for (i = 0; i < ORINOCO_MAX_BSS_COUNT; i++)
386 list_add_tail(&(PRIV_XBSS[i].list),
387 &priv->bss_free_list);
388 else
389 for (i = 0; i < ORINOCO_MAX_BSS_COUNT; i++)
390 list_add_tail(&(PRIV_BSS[i].list),
391 &priv->bss_free_list);
395 static inline u8 *orinoco_get_ie(u8 *data, size_t len,
396 enum ieee80211_eid eid)
398 u8 *p = data;
399 while ((p + 2) < (data + len)) {
400 if (p[0] == eid)
401 return p;
402 p += p[1] + 2;
404 return NULL;
407 #define WPA_OUI_TYPE "\x00\x50\xF2\x01"
408 #define WPA_SELECTOR_LEN 4
409 static inline u8 *orinoco_get_wpa_ie(u8 *data, size_t len)
411 u8 *p = data;
412 while ((p + 2 + WPA_SELECTOR_LEN) < (data + len)) {
413 if ((p[0] == WLAN_EID_GENERIC) &&
414 (memcmp(&p[2], WPA_OUI_TYPE, WPA_SELECTOR_LEN) == 0))
415 return p;
416 p += p[1] + 2;
418 return NULL;
422 /********************************************************************/
423 /* Download functionality */
424 /********************************************************************/
426 struct fw_info {
427 char *pri_fw;
428 char *sta_fw;
429 char *ap_fw;
430 u32 pda_addr;
431 u16 pda_size;
434 const static struct fw_info orinoco_fw[] = {
435 { NULL, "agere_sta_fw.bin", "agere_ap_fw.bin", 0x00390000, 1000 },
436 { NULL, "prism_sta_fw.bin", "prism_ap_fw.bin", 0, 1024 },
437 { "symbol_sp24t_prim_fw", "symbol_sp24t_sec_fw", NULL, 0x00003100, 512 }
440 /* Structure used to access fields in FW
441 * Make sure LE decoding macros are used
443 struct orinoco_fw_header {
444 char hdr_vers[6]; /* ASCII string for header version */
445 __le16 headersize; /* Total length of header */
446 __le32 entry_point; /* NIC entry point */
447 __le32 blocks; /* Number of blocks to program */
448 __le32 block_offset; /* Offset of block data from eof header */
449 __le32 pdr_offset; /* Offset to PDR data from eof header */
450 __le32 pri_offset; /* Offset to primary plug data */
451 __le32 compat_offset; /* Offset to compatibility data*/
452 char signature[0]; /* FW signature length headersize-20 */
453 } __attribute__ ((packed));
455 /* Download either STA or AP firmware into the card. */
456 static int
457 orinoco_dl_firmware(struct orinoco_private *priv,
458 const struct fw_info *fw,
459 int ap)
461 /* Plug Data Area (PDA) */
462 __le16 *pda;
464 hermes_t *hw = &priv->hw;
465 const struct firmware *fw_entry;
466 const struct orinoco_fw_header *hdr;
467 const unsigned char *first_block;
468 const unsigned char *end;
469 const char *firmware;
470 struct net_device *dev = priv->ndev;
471 int err = 0;
473 pda = kzalloc(fw->pda_size, GFP_KERNEL);
474 if (!pda)
475 return -ENOMEM;
477 if (ap)
478 firmware = fw->ap_fw;
479 else
480 firmware = fw->sta_fw;
482 printk(KERN_DEBUG "%s: Attempting to download firmware %s\n",
483 dev->name, firmware);
485 /* Read current plug data */
486 err = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 0);
487 printk(KERN_DEBUG "%s: Read PDA returned %d\n", dev->name, err);
488 if (err)
489 goto free;
491 if (!priv->cached_fw) {
492 err = request_firmware(&fw_entry, firmware, priv->dev);
494 if (err) {
495 printk(KERN_ERR "%s: Cannot find firmware %s\n",
496 dev->name, firmware);
497 err = -ENOENT;
498 goto free;
500 } else
501 fw_entry = priv->cached_fw;
503 hdr = (const struct orinoco_fw_header *) fw_entry->data;
505 /* Enable aux port to allow programming */
506 err = hermesi_program_init(hw, le32_to_cpu(hdr->entry_point));
507 printk(KERN_DEBUG "%s: Program init returned %d\n", dev->name, err);
508 if (err != 0)
509 goto abort;
511 /* Program data */
512 first_block = (fw_entry->data +
513 le16_to_cpu(hdr->headersize) +
514 le32_to_cpu(hdr->block_offset));
515 end = fw_entry->data + fw_entry->size;
517 err = hermes_program(hw, first_block, end);
518 printk(KERN_DEBUG "%s: Program returned %d\n", dev->name, err);
519 if (err != 0)
520 goto abort;
522 /* Update production data */
523 first_block = (fw_entry->data +
524 le16_to_cpu(hdr->headersize) +
525 le32_to_cpu(hdr->pdr_offset));
527 err = hermes_apply_pda_with_defaults(hw, first_block, pda);
528 printk(KERN_DEBUG "%s: Apply PDA returned %d\n", dev->name, err);
529 if (err)
530 goto abort;
532 /* Tell card we've finished */
533 err = hermesi_program_end(hw);
534 printk(KERN_DEBUG "%s: Program end returned %d\n", dev->name, err);
535 if (err != 0)
536 goto abort;
538 /* Check if we're running */
539 printk(KERN_DEBUG "%s: hermes_present returned %d\n",
540 dev->name, hermes_present(hw));
542 abort:
543 /* If we requested the firmware, release it. */
544 if (!priv->cached_fw)
545 release_firmware(fw_entry);
547 free:
548 kfree(pda);
549 return err;
552 /* End markers */
553 #define TEXT_END 0x1A /* End of text header */
556 * Process a firmware image - stop the card, load the firmware, reset
557 * the card and make sure it responds. For the secondary firmware take
558 * care of the PDA - read it and then write it on top of the firmware.
560 static int
561 symbol_dl_image(struct orinoco_private *priv, const struct fw_info *fw,
562 const unsigned char *image, const unsigned char *end,
563 int secondary)
565 hermes_t *hw = &priv->hw;
566 int ret = 0;
567 const unsigned char *ptr;
568 const unsigned char *first_block;
570 /* Plug Data Area (PDA) */
571 __le16 *pda = NULL;
573 /* Binary block begins after the 0x1A marker */
574 ptr = image;
575 while (*ptr++ != TEXT_END);
576 first_block = ptr;
578 /* Read the PDA from EEPROM */
579 if (secondary) {
580 pda = kzalloc(fw->pda_size, GFP_KERNEL);
581 if (!pda)
582 return -ENOMEM;
584 ret = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 1);
585 if (ret)
586 goto free;
589 /* Stop the firmware, so that it can be safely rewritten */
590 if (priv->stop_fw) {
591 ret = priv->stop_fw(priv, 1);
592 if (ret)
593 goto free;
596 /* Program the adapter with new firmware */
597 ret = hermes_program(hw, first_block, end);
598 if (ret)
599 goto free;
601 /* Write the PDA to the adapter */
602 if (secondary) {
603 size_t len = hermes_blocks_length(first_block);
604 ptr = first_block + len;
605 ret = hermes_apply_pda(hw, ptr, pda);
606 kfree(pda);
607 if (ret)
608 return ret;
611 /* Run the firmware */
612 if (priv->stop_fw) {
613 ret = priv->stop_fw(priv, 0);
614 if (ret)
615 return ret;
618 /* Reset hermes chip and make sure it responds */
619 ret = hw->ops->init(hw);
621 /* hermes_reset() should return 0 with the secondary firmware */
622 if (secondary && ret != 0)
623 return -ENODEV;
625 /* And this should work with any firmware */
626 if (!hermes_present(hw))
627 return -ENODEV;
629 return 0;
631 free:
632 kfree(pda);
633 return ret;
638 * Download the firmware into the card, this also does a PCMCIA soft
639 * reset on the card, to make sure it's in a sane state.
641 static int
642 symbol_dl_firmware(struct orinoco_private *priv,
643 const struct fw_info *fw)
645 struct net_device *dev = priv->ndev;
646 int ret;
647 const struct firmware *fw_entry;
649 if (!priv->cached_pri_fw) {
650 if (request_firmware(&fw_entry, fw->pri_fw, priv->dev) != 0) {
651 printk(KERN_ERR "%s: Cannot find firmware: %s\n",
652 dev->name, fw->pri_fw);
653 return -ENOENT;
655 } else
656 fw_entry = priv->cached_pri_fw;
658 /* Load primary firmware */
659 ret = symbol_dl_image(priv, fw, fw_entry->data,
660 fw_entry->data + fw_entry->size, 0);
662 if (!priv->cached_pri_fw)
663 release_firmware(fw_entry);
664 if (ret) {
665 printk(KERN_ERR "%s: Primary firmware download failed\n",
666 dev->name);
667 return ret;
670 if (!priv->cached_fw) {
671 if (request_firmware(&fw_entry, fw->sta_fw, priv->dev) != 0) {
672 printk(KERN_ERR "%s: Cannot find firmware: %s\n",
673 dev->name, fw->sta_fw);
674 return -ENOENT;
676 } else
677 fw_entry = priv->cached_fw;
679 /* Load secondary firmware */
680 ret = symbol_dl_image(priv, fw, fw_entry->data,
681 fw_entry->data + fw_entry->size, 1);
682 if (!priv->cached_fw)
683 release_firmware(fw_entry);
684 if (ret) {
685 printk(KERN_ERR "%s: Secondary firmware download failed\n",
686 dev->name);
689 return ret;
692 static int orinoco_download(struct orinoco_private *priv)
694 int err = 0;
695 /* Reload firmware */
696 switch (priv->firmware_type) {
697 case FIRMWARE_TYPE_AGERE:
698 /* case FIRMWARE_TYPE_INTERSIL: */
699 err = orinoco_dl_firmware(priv,
700 &orinoco_fw[priv->firmware_type], 0);
701 break;
703 case FIRMWARE_TYPE_SYMBOL:
704 err = symbol_dl_firmware(priv,
705 &orinoco_fw[priv->firmware_type]);
706 break;
707 case FIRMWARE_TYPE_INTERSIL:
708 break;
710 /* TODO: if we fail we probably need to reinitialise
711 * the driver */
713 return err;
716 #if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
717 static void orinoco_cache_fw(struct orinoco_private *priv, int ap)
719 const struct firmware *fw_entry = NULL;
720 const char *pri_fw;
721 const char *fw;
723 pri_fw = orinoco_fw[priv->firmware_type].pri_fw;
724 if (ap)
725 fw = orinoco_fw[priv->firmware_type].ap_fw;
726 else
727 fw = orinoco_fw[priv->firmware_type].sta_fw;
729 if (pri_fw) {
730 if (request_firmware(&fw_entry, pri_fw, priv->dev) == 0)
731 priv->cached_pri_fw = fw_entry;
734 if (fw) {
735 if (request_firmware(&fw_entry, fw, priv->dev) == 0)
736 priv->cached_fw = fw_entry;
740 static void orinoco_uncache_fw(struct orinoco_private *priv)
742 if (priv->cached_pri_fw)
743 release_firmware(priv->cached_pri_fw);
744 if (priv->cached_fw)
745 release_firmware(priv->cached_fw);
747 priv->cached_pri_fw = NULL;
748 priv->cached_fw = NULL;
750 #else
751 #define orinoco_cache_fw(priv, ap)
752 #define orinoco_uncache_fw(priv)
753 #endif
755 /********************************************************************/
756 /* Device methods */
757 /********************************************************************/
759 static int orinoco_open(struct net_device *dev)
761 struct orinoco_private *priv = netdev_priv(dev);
762 unsigned long flags;
763 int err;
765 if (orinoco_lock(priv, &flags) != 0)
766 return -EBUSY;
768 err = __orinoco_up(dev);
770 if (! err)
771 priv->open = 1;
773 orinoco_unlock(priv, &flags);
775 return err;
778 static int orinoco_stop(struct net_device *dev)
780 struct orinoco_private *priv = netdev_priv(dev);
781 int err = 0;
783 /* We mustn't use orinoco_lock() here, because we need to be
784 able to close the interface even if hw_unavailable is set
785 (e.g. as we're released after a PC Card removal) */
786 orinoco_spin_lock(priv);
788 priv->open = 0;
790 err = __orinoco_down(dev);
792 orinoco_spin_unlock(priv);
794 return err;
797 static struct net_device_stats *orinoco_get_stats(struct net_device *dev)
799 struct orinoco_private *priv = netdev_priv(dev);
801 return &priv->stats;
804 static struct iw_statistics *orinoco_get_wireless_stats(struct net_device *dev)
806 struct orinoco_private *priv = netdev_priv(dev);
807 hermes_t *hw = &priv->hw;
808 struct iw_statistics *wstats = &priv->wstats;
809 int err;
810 unsigned long flags;
812 if (! netif_device_present(dev)) {
813 printk(KERN_WARNING "%s: get_wireless_stats() called while device not present\n",
814 dev->name);
815 return NULL; /* FIXME: Can we do better than this? */
818 /* If busy, return the old stats. Returning NULL may cause
819 * the interface to disappear from /proc/net/wireless */
820 if (orinoco_lock(priv, &flags) != 0)
821 return wstats;
823 /* We can't really wait for the tallies inquiry command to
824 * complete, so we just use the previous results and trigger
825 * a new tallies inquiry command for next time - Jean II */
826 /* FIXME: Really we should wait for the inquiry to come back -
827 * as it is the stats we give don't make a whole lot of sense.
828 * Unfortunately, it's not clear how to do that within the
829 * wireless extensions framework: I think we're in user
830 * context, but a lock seems to be held by the time we get in
831 * here so we're not safe to sleep here. */
832 hermes_inquire(hw, HERMES_INQ_TALLIES);
834 if (priv->iw_mode == IW_MODE_ADHOC) {
835 memset(&wstats->qual, 0, sizeof(wstats->qual));
836 /* If a spy address is defined, we report stats of the
837 * first spy address - Jean II */
838 if (SPY_NUMBER(priv)) {
839 wstats->qual.qual = priv->spy_data.spy_stat[0].qual;
840 wstats->qual.level = priv->spy_data.spy_stat[0].level;
841 wstats->qual.noise = priv->spy_data.spy_stat[0].noise;
842 wstats->qual.updated = priv->spy_data.spy_stat[0].updated;
844 } else {
845 struct {
846 __le16 qual, signal, noise, unused;
847 } __attribute__ ((packed)) cq;
849 err = HERMES_READ_RECORD(hw, USER_BAP,
850 HERMES_RID_COMMSQUALITY, &cq);
852 if (!err) {
853 wstats->qual.qual = (int)le16_to_cpu(cq.qual);
854 wstats->qual.level = (int)le16_to_cpu(cq.signal) - 0x95;
855 wstats->qual.noise = (int)le16_to_cpu(cq.noise) - 0x95;
856 wstats->qual.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
860 orinoco_unlock(priv, &flags);
861 return wstats;
864 static void orinoco_set_multicast_list(struct net_device *dev)
866 struct orinoco_private *priv = netdev_priv(dev);
867 unsigned long flags;
869 if (orinoco_lock(priv, &flags) != 0) {
870 printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
871 "called when hw_unavailable\n", dev->name);
872 return;
875 __orinoco_set_multicast_list(dev);
876 orinoco_unlock(priv, &flags);
879 static int orinoco_change_mtu(struct net_device *dev, int new_mtu)
881 struct orinoco_private *priv = netdev_priv(dev);
883 if ( (new_mtu < ORINOCO_MIN_MTU) || (new_mtu > ORINOCO_MAX_MTU) )
884 return -EINVAL;
886 /* MTU + encapsulation + header length */
887 if ( (new_mtu + ENCAPS_OVERHEAD + sizeof(struct ieee80211_hdr)) >
888 (priv->nicbuf_size - ETH_HLEN) )
889 return -EINVAL;
891 dev->mtu = new_mtu;
893 return 0;
896 /********************************************************************/
897 /* Tx path */
898 /********************************************************************/
900 static int orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
902 struct orinoco_private *priv = netdev_priv(dev);
903 struct net_device_stats *stats = &priv->stats;
904 hermes_t *hw = &priv->hw;
905 int err = 0;
906 u16 txfid = priv->txfid;
907 struct ethhdr *eh;
908 int tx_control;
909 unsigned long flags;
911 if (! netif_running(dev)) {
912 printk(KERN_ERR "%s: Tx on stopped device!\n",
913 dev->name);
914 return NETDEV_TX_BUSY;
917 if (netif_queue_stopped(dev)) {
918 printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
919 dev->name);
920 return NETDEV_TX_BUSY;
923 if (orinoco_lock(priv, &flags) != 0) {
924 printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n",
925 dev->name);
926 return NETDEV_TX_BUSY;
929 if (! netif_carrier_ok(dev) || (priv->iw_mode == IW_MODE_MONITOR)) {
930 /* Oops, the firmware hasn't established a connection,
931 silently drop the packet (this seems to be the
932 safest approach). */
933 goto drop;
936 /* Check packet length */
937 if (skb->len < ETH_HLEN)
938 goto drop;
940 tx_control = HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX;
942 if (priv->encode_alg == IW_ENCODE_ALG_TKIP)
943 tx_control |= (priv->tx_key << HERMES_MIC_KEY_ID_SHIFT) |
944 HERMES_TXCTRL_MIC;
946 if (priv->has_alt_txcntl) {
947 /* WPA enabled firmwares have tx_cntl at the end of
948 * the 802.11 header. So write zeroed descriptor and
949 * 802.11 header at the same time
951 char desc[HERMES_802_3_OFFSET];
952 __le16 *txcntl = (__le16 *) &desc[HERMES_TXCNTL2_OFFSET];
954 memset(&desc, 0, sizeof(desc));
956 *txcntl = cpu_to_le16(tx_control);
957 err = hw->ops->bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
958 txfid, 0);
959 if (err) {
960 if (net_ratelimit())
961 printk(KERN_ERR "%s: Error %d writing Tx "
962 "descriptor to BAP\n", dev->name, err);
963 goto busy;
965 } else {
966 struct hermes_tx_descriptor desc;
968 memset(&desc, 0, sizeof(desc));
970 desc.tx_control = cpu_to_le16(tx_control);
971 err = hw->ops->bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
972 txfid, 0);
973 if (err) {
974 if (net_ratelimit())
975 printk(KERN_ERR "%s: Error %d writing Tx "
976 "descriptor to BAP\n", dev->name, err);
977 goto busy;
980 /* Clear the 802.11 header and data length fields - some
981 * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
982 * if this isn't done. */
983 hermes_clear_words(hw, HERMES_DATA0,
984 HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
987 eh = (struct ethhdr *)skb->data;
989 /* Encapsulate Ethernet-II frames */
990 if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
991 struct header_struct {
992 struct ethhdr eth; /* 802.3 header */
993 u8 encap[6]; /* 802.2 header */
994 } __attribute__ ((packed)) hdr;
996 /* Strip destination and source from the data */
997 skb_pull(skb, 2 * ETH_ALEN);
999 /* And move them to a separate header */
1000 memcpy(&hdr.eth, eh, 2 * ETH_ALEN);
1001 hdr.eth.h_proto = htons(sizeof(encaps_hdr) + skb->len);
1002 memcpy(hdr.encap, encaps_hdr, sizeof(encaps_hdr));
1004 /* Insert the SNAP header */
1005 if (skb_headroom(skb) < sizeof(hdr)) {
1006 printk(KERN_ERR
1007 "%s: Not enough headroom for 802.2 headers %d\n",
1008 dev->name, skb_headroom(skb));
1009 goto drop;
1011 eh = (struct ethhdr *) skb_push(skb, sizeof(hdr));
1012 memcpy(eh, &hdr, sizeof(hdr));
1015 err = hw->ops->bap_pwrite(hw, USER_BAP, skb->data, skb->len,
1016 txfid, HERMES_802_3_OFFSET);
1017 if (err) {
1018 printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
1019 dev->name, err);
1020 goto busy;
1023 /* Calculate Michael MIC */
1024 if (priv->encode_alg == IW_ENCODE_ALG_TKIP) {
1025 u8 mic_buf[MICHAEL_MIC_LEN + 1];
1026 u8 *mic;
1027 size_t offset;
1028 size_t len;
1030 if (skb->len % 2) {
1031 /* MIC start is on an odd boundary */
1032 mic_buf[0] = skb->data[skb->len - 1];
1033 mic = &mic_buf[1];
1034 offset = skb->len - 1;
1035 len = MICHAEL_MIC_LEN + 1;
1036 } else {
1037 mic = &mic_buf[0];
1038 offset = skb->len;
1039 len = MICHAEL_MIC_LEN;
1042 michael_mic(priv->tx_tfm_mic,
1043 priv->tkip_key[priv->tx_key].tx_mic,
1044 eh->h_dest, eh->h_source, 0 /* priority */,
1045 skb->data + ETH_HLEN, skb->len - ETH_HLEN, mic);
1047 /* Write the MIC */
1048 err = hw->ops->bap_pwrite(hw, USER_BAP, &mic_buf[0], len,
1049 txfid, HERMES_802_3_OFFSET + offset);
1050 if (err) {
1051 printk(KERN_ERR "%s: Error %d writing MIC to BAP\n",
1052 dev->name, err);
1053 goto busy;
1057 /* Finally, we actually initiate the send */
1058 netif_stop_queue(dev);
1060 err = hw->ops->cmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
1061 txfid, NULL);
1062 if (err) {
1063 netif_start_queue(dev);
1064 if (net_ratelimit())
1065 printk(KERN_ERR "%s: Error %d transmitting packet\n",
1066 dev->name, err);
1067 goto busy;
1070 dev->trans_start = jiffies;
1071 stats->tx_bytes += HERMES_802_3_OFFSET + skb->len;
1072 goto ok;
1074 drop:
1075 stats->tx_errors++;
1076 stats->tx_dropped++;
1079 orinoco_unlock(priv, &flags);
1080 dev_kfree_skb(skb);
1081 return NETDEV_TX_OK;
1083 busy:
1084 if (err == -EIO)
1085 schedule_work(&priv->reset_work);
1086 orinoco_unlock(priv, &flags);
1087 return NETDEV_TX_BUSY;
1090 static void __orinoco_ev_alloc(struct net_device *dev, hermes_t *hw)
1092 struct orinoco_private *priv = netdev_priv(dev);
1093 u16 fid = hermes_read_regn(hw, ALLOCFID);
1095 if (fid != priv->txfid) {
1096 if (fid != DUMMY_FID)
1097 printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
1098 dev->name, fid);
1099 return;
1102 hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
1105 static void __orinoco_ev_tx(struct net_device *dev, hermes_t *hw)
1107 struct orinoco_private *priv = netdev_priv(dev);
1108 struct net_device_stats *stats = &priv->stats;
1110 stats->tx_packets++;
1112 netif_wake_queue(dev);
1114 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
1117 static void __orinoco_ev_txexc(struct net_device *dev, hermes_t *hw)
1119 struct orinoco_private *priv = netdev_priv(dev);
1120 struct net_device_stats *stats = &priv->stats;
1121 u16 fid = hermes_read_regn(hw, TXCOMPLFID);
1122 u16 status;
1123 struct hermes_txexc_data hdr;
1124 int err = 0;
1126 if (fid == DUMMY_FID)
1127 return; /* Nothing's really happened */
1129 /* Read part of the frame header - we need status and addr1 */
1130 err = hw->ops->bap_pread(hw, IRQ_BAP, &hdr,
1131 sizeof(struct hermes_txexc_data),
1132 fid, 0);
1134 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
1135 stats->tx_errors++;
1137 if (err) {
1138 printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
1139 "(FID=%04X error %d)\n",
1140 dev->name, fid, err);
1141 return;
1144 DEBUG(1, "%s: Tx error, err %d (FID=%04X)\n", dev->name,
1145 err, fid);
1147 /* We produce a TXDROP event only for retry or lifetime
1148 * exceeded, because that's the only status that really mean
1149 * that this particular node went away.
1150 * Other errors means that *we* screwed up. - Jean II */
1151 status = le16_to_cpu(hdr.desc.status);
1152 if (status & (HERMES_TXSTAT_RETRYERR | HERMES_TXSTAT_AGEDERR)) {
1153 union iwreq_data wrqu;
1155 /* Copy 802.11 dest address.
1156 * We use the 802.11 header because the frame may
1157 * not be 802.3 or may be mangled...
1158 * In Ad-Hoc mode, it will be the node address.
1159 * In managed mode, it will be most likely the AP addr
1160 * User space will figure out how to convert it to
1161 * whatever it needs (IP address or else).
1162 * - Jean II */
1163 memcpy(wrqu.addr.sa_data, hdr.addr1, ETH_ALEN);
1164 wrqu.addr.sa_family = ARPHRD_ETHER;
1166 /* Send event to user space */
1167 wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
1170 netif_wake_queue(dev);
1173 static void orinoco_tx_timeout(struct net_device *dev)
1175 struct orinoco_private *priv = netdev_priv(dev);
1176 struct net_device_stats *stats = &priv->stats;
1177 struct hermes *hw = &priv->hw;
1179 printk(KERN_WARNING "%s: Tx timeout! "
1180 "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
1181 dev->name, hermes_read_regn(hw, ALLOCFID),
1182 hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
1184 stats->tx_errors++;
1186 schedule_work(&priv->reset_work);
1189 /********************************************************************/
1190 /* Rx path (data frames) */
1191 /********************************************************************/
1193 /* Does the frame have a SNAP header indicating it should be
1194 * de-encapsulated to Ethernet-II? */
1195 static inline int is_ethersnap(void *_hdr)
1197 u8 *hdr = _hdr;
1199 /* We de-encapsulate all packets which, a) have SNAP headers
1200 * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
1201 * and where b) the OUI of the SNAP header is 00:00:00 or
1202 * 00:00:f8 - we need both because different APs appear to use
1203 * different OUIs for some reason */
1204 return (memcmp(hdr, &encaps_hdr, 5) == 0)
1205 && ( (hdr[5] == 0x00) || (hdr[5] == 0xf8) );
1208 static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
1209 int level, int noise)
1211 struct iw_quality wstats;
1212 wstats.level = level - 0x95;
1213 wstats.noise = noise - 0x95;
1214 wstats.qual = (level > noise) ? (level - noise) : 0;
1215 wstats.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
1216 /* Update spy records */
1217 wireless_spy_update(dev, mac, &wstats);
1220 static void orinoco_stat_gather(struct net_device *dev,
1221 struct sk_buff *skb,
1222 struct hermes_rx_descriptor *desc)
1224 struct orinoco_private *priv = netdev_priv(dev);
1226 /* Using spy support with lots of Rx packets, like in an
1227 * infrastructure (AP), will really slow down everything, because
1228 * the MAC address must be compared to each entry of the spy list.
1229 * If the user really asks for it (set some address in the
1230 * spy list), we do it, but he will pay the price.
1231 * Note that to get here, you need both WIRELESS_SPY
1232 * compiled in AND some addresses in the list !!!
1234 /* Note : gcc will optimise the whole section away if
1235 * WIRELESS_SPY is not defined... - Jean II */
1236 if (SPY_NUMBER(priv)) {
1237 orinoco_spy_gather(dev, skb_mac_header(skb) + ETH_ALEN,
1238 desc->signal, desc->silence);
1243 * orinoco_rx_monitor - handle received monitor frames.
1245 * Arguments:
1246 * dev network device
1247 * rxfid received FID
1248 * desc rx descriptor of the frame
1250 * Call context: interrupt
1252 static void orinoco_rx_monitor(struct net_device *dev, u16 rxfid,
1253 struct hermes_rx_descriptor *desc)
1255 u32 hdrlen = 30; /* return full header by default */
1256 u32 datalen = 0;
1257 u16 fc;
1258 int err;
1259 int len;
1260 struct sk_buff *skb;
1261 struct orinoco_private *priv = netdev_priv(dev);
1262 struct net_device_stats *stats = &priv->stats;
1263 hermes_t *hw = &priv->hw;
1265 len = le16_to_cpu(desc->data_len);
1267 /* Determine the size of the header and the data */
1268 fc = le16_to_cpu(desc->frame_ctl);
1269 switch (fc & IEEE80211_FCTL_FTYPE) {
1270 case IEEE80211_FTYPE_DATA:
1271 if ((fc & IEEE80211_FCTL_TODS)
1272 && (fc & IEEE80211_FCTL_FROMDS))
1273 hdrlen = 30;
1274 else
1275 hdrlen = 24;
1276 datalen = len;
1277 break;
1278 case IEEE80211_FTYPE_MGMT:
1279 hdrlen = 24;
1280 datalen = len;
1281 break;
1282 case IEEE80211_FTYPE_CTL:
1283 switch (fc & IEEE80211_FCTL_STYPE) {
1284 case IEEE80211_STYPE_PSPOLL:
1285 case IEEE80211_STYPE_RTS:
1286 case IEEE80211_STYPE_CFEND:
1287 case IEEE80211_STYPE_CFENDACK:
1288 hdrlen = 16;
1289 break;
1290 case IEEE80211_STYPE_CTS:
1291 case IEEE80211_STYPE_ACK:
1292 hdrlen = 10;
1293 break;
1295 break;
1296 default:
1297 /* Unknown frame type */
1298 break;
1301 /* sanity check the length */
1302 if (datalen > IEEE80211_MAX_DATA_LEN + 12) {
1303 printk(KERN_DEBUG "%s: oversized monitor frame, "
1304 "data length = %d\n", dev->name, datalen);
1305 err = -EIO;
1306 stats->rx_length_errors++;
1307 goto update_stats;
1310 skb = dev_alloc_skb(hdrlen + datalen);
1311 if (!skb) {
1312 printk(KERN_WARNING "%s: Cannot allocate skb for monitor frame\n",
1313 dev->name);
1314 err = -ENOMEM;
1315 goto drop;
1318 /* Copy the 802.11 header to the skb */
1319 memcpy(skb_put(skb, hdrlen), &(desc->frame_ctl), hdrlen);
1320 skb_reset_mac_header(skb);
1322 /* If any, copy the data from the card to the skb */
1323 if (datalen > 0) {
1324 err = hw->ops->bap_pread(hw, IRQ_BAP, skb_put(skb, datalen),
1325 ALIGN(datalen, 2), rxfid,
1326 HERMES_802_2_OFFSET);
1327 if (err) {
1328 printk(KERN_ERR "%s: error %d reading monitor frame\n",
1329 dev->name, err);
1330 goto drop;
1334 skb->dev = dev;
1335 skb->ip_summed = CHECKSUM_NONE;
1336 skb->pkt_type = PACKET_OTHERHOST;
1337 skb->protocol = __constant_htons(ETH_P_802_2);
1339 stats->rx_packets++;
1340 stats->rx_bytes += skb->len;
1342 netif_rx(skb);
1343 return;
1345 drop:
1346 dev_kfree_skb_irq(skb);
1347 update_stats:
1348 stats->rx_errors++;
1349 stats->rx_dropped++;
1352 /* Get tsc from the firmware */
1353 static int orinoco_hw_get_tkip_iv(struct orinoco_private *priv, int key,
1354 u8 *tsc)
1356 hermes_t *hw = &priv->hw;
1357 int err = 0;
1358 u8 tsc_arr[4][IW_ENCODE_SEQ_MAX_SIZE];
1360 if ((key < 0) || (key > 4))
1361 return -EINVAL;
1363 err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_TKIP_IV,
1364 sizeof(tsc_arr), NULL, &tsc_arr);
1365 if (!err)
1366 memcpy(tsc, &tsc_arr[key][0], sizeof(tsc_arr[0]));
1368 return err;
1371 static void __orinoco_ev_rx(struct net_device *dev, hermes_t *hw)
1373 struct orinoco_private *priv = netdev_priv(dev);
1374 struct net_device_stats *stats = &priv->stats;
1375 struct iw_statistics *wstats = &priv->wstats;
1376 struct sk_buff *skb = NULL;
1377 u16 rxfid, status;
1378 int length;
1379 struct hermes_rx_descriptor *desc;
1380 struct orinoco_rx_data *rx_data;
1381 int err;
1383 desc = kmalloc(sizeof(*desc), GFP_ATOMIC);
1384 if (!desc) {
1385 printk(KERN_WARNING
1386 "%s: Can't allocate space for RX descriptor\n",
1387 dev->name);
1388 goto update_stats;
1391 rxfid = hermes_read_regn(hw, RXFID);
1393 err = hw->ops->bap_pread(hw, IRQ_BAP, desc, sizeof(*desc),
1394 rxfid, 0);
1395 if (err) {
1396 printk(KERN_ERR "%s: error %d reading Rx descriptor. "
1397 "Frame dropped.\n", dev->name, err);
1398 goto update_stats;
1401 status = le16_to_cpu(desc->status);
1403 if (status & HERMES_RXSTAT_BADCRC) {
1404 DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n",
1405 dev->name);
1406 stats->rx_crc_errors++;
1407 goto update_stats;
1410 /* Handle frames in monitor mode */
1411 if (priv->iw_mode == IW_MODE_MONITOR) {
1412 orinoco_rx_monitor(dev, rxfid, desc);
1413 goto out;
1416 if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
1417 DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
1418 dev->name);
1419 wstats->discard.code++;
1420 goto update_stats;
1423 length = le16_to_cpu(desc->data_len);
1425 /* Sanity checks */
1426 if (length < 3) { /* No for even an 802.2 LLC header */
1427 /* At least on Symbol firmware with PCF we get quite a
1428 lot of these legitimately - Poll frames with no
1429 data. */
1430 goto out;
1432 if (length > IEEE80211_MAX_DATA_LEN) {
1433 printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
1434 dev->name, length);
1435 stats->rx_length_errors++;
1436 goto update_stats;
1439 /* Payload size does not include Michael MIC. Increase payload
1440 * size to read it together with the data. */
1441 if (status & HERMES_RXSTAT_MIC)
1442 length += MICHAEL_MIC_LEN;
1444 /* We need space for the packet data itself, plus an ethernet
1445 header, plus 2 bytes so we can align the IP header on a
1446 32bit boundary, plus 1 byte so we can read in odd length
1447 packets from the card, which has an IO granularity of 16
1448 bits */
1449 skb = dev_alloc_skb(length+ETH_HLEN+2+1);
1450 if (!skb) {
1451 printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
1452 dev->name);
1453 goto update_stats;
1456 /* We'll prepend the header, so reserve space for it. The worst
1457 case is no decapsulation, when 802.3 header is prepended and
1458 nothing is removed. 2 is for aligning the IP header. */
1459 skb_reserve(skb, ETH_HLEN + 2);
1461 err = hw->ops->bap_pread(hw, IRQ_BAP, skb_put(skb, length),
1462 ALIGN(length, 2), rxfid,
1463 HERMES_802_2_OFFSET);
1464 if (err) {
1465 printk(KERN_ERR "%s: error %d reading frame. "
1466 "Frame dropped.\n", dev->name, err);
1467 goto drop;
1470 /* Add desc and skb to rx queue */
1471 rx_data = kzalloc(sizeof(*rx_data), GFP_ATOMIC);
1472 if (!rx_data) {
1473 printk(KERN_WARNING "%s: Can't allocate RX packet\n",
1474 dev->name);
1475 goto drop;
1477 rx_data->desc = desc;
1478 rx_data->skb = skb;
1479 list_add_tail(&rx_data->list, &priv->rx_list);
1480 tasklet_schedule(&priv->rx_tasklet);
1482 return;
1484 drop:
1485 dev_kfree_skb_irq(skb);
1486 update_stats:
1487 stats->rx_errors++;
1488 stats->rx_dropped++;
1489 out:
1490 kfree(desc);
1493 static void orinoco_rx(struct net_device *dev,
1494 struct hermes_rx_descriptor *desc,
1495 struct sk_buff *skb)
1497 struct orinoco_private *priv = netdev_priv(dev);
1498 struct net_device_stats *stats = &priv->stats;
1499 u16 status, fc;
1500 int length;
1501 struct ethhdr *hdr;
1503 status = le16_to_cpu(desc->status);
1504 length = le16_to_cpu(desc->data_len);
1505 fc = le16_to_cpu(desc->frame_ctl);
1507 /* Calculate and check MIC */
1508 if (status & HERMES_RXSTAT_MIC) {
1509 int key_id = ((status & HERMES_RXSTAT_MIC_KEY_ID) >>
1510 HERMES_MIC_KEY_ID_SHIFT);
1511 u8 mic[MICHAEL_MIC_LEN];
1512 u8 *rxmic;
1513 u8 *src = (fc & IEEE80211_FCTL_FROMDS) ?
1514 desc->addr3 : desc->addr2;
1516 /* Extract Michael MIC from payload */
1517 rxmic = skb->data + skb->len - MICHAEL_MIC_LEN;
1519 skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
1520 length -= MICHAEL_MIC_LEN;
1522 michael_mic(priv->rx_tfm_mic,
1523 priv->tkip_key[key_id].rx_mic,
1524 desc->addr1,
1525 src,
1526 0, /* priority or QoS? */
1527 skb->data,
1528 skb->len,
1529 &mic[0]);
1531 if (memcmp(mic, rxmic,
1532 MICHAEL_MIC_LEN)) {
1533 union iwreq_data wrqu;
1534 struct iw_michaelmicfailure wxmic;
1536 printk(KERN_WARNING "%s: "
1537 "Invalid Michael MIC in data frame from %pM, "
1538 "using key %i\n",
1539 dev->name, src, key_id);
1541 /* TODO: update stats */
1543 /* Notify userspace */
1544 memset(&wxmic, 0, sizeof(wxmic));
1545 wxmic.flags = key_id & IW_MICFAILURE_KEY_ID;
1546 wxmic.flags |= (desc->addr1[0] & 1) ?
1547 IW_MICFAILURE_GROUP : IW_MICFAILURE_PAIRWISE;
1548 wxmic.src_addr.sa_family = ARPHRD_ETHER;
1549 memcpy(wxmic.src_addr.sa_data, src, ETH_ALEN);
1551 (void) orinoco_hw_get_tkip_iv(priv, key_id,
1552 &wxmic.tsc[0]);
1554 memset(&wrqu, 0, sizeof(wrqu));
1555 wrqu.data.length = sizeof(wxmic);
1556 wireless_send_event(dev, IWEVMICHAELMICFAILURE, &wrqu,
1557 (char *) &wxmic);
1559 goto drop;
1563 /* Handle decapsulation
1564 * In most cases, the firmware tell us about SNAP frames.
1565 * For some reason, the SNAP frames sent by LinkSys APs
1566 * are not properly recognised by most firmwares.
1567 * So, check ourselves */
1568 if (length >= ENCAPS_OVERHEAD &&
1569 (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
1570 ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
1571 is_ethersnap(skb->data))) {
1572 /* These indicate a SNAP within 802.2 LLC within
1573 802.11 frame which we'll need to de-encapsulate to
1574 the original EthernetII frame. */
1575 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN - ENCAPS_OVERHEAD);
1576 } else {
1577 /* 802.3 frame - prepend 802.3 header as is */
1578 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN);
1579 hdr->h_proto = htons(length);
1581 memcpy(hdr->h_dest, desc->addr1, ETH_ALEN);
1582 if (fc & IEEE80211_FCTL_FROMDS)
1583 memcpy(hdr->h_source, desc->addr3, ETH_ALEN);
1584 else
1585 memcpy(hdr->h_source, desc->addr2, ETH_ALEN);
1587 skb->protocol = eth_type_trans(skb, dev);
1588 skb->ip_summed = CHECKSUM_NONE;
1589 if (fc & IEEE80211_FCTL_TODS)
1590 skb->pkt_type = PACKET_OTHERHOST;
1592 /* Process the wireless stats if needed */
1593 orinoco_stat_gather(dev, skb, desc);
1595 /* Pass the packet to the networking stack */
1596 netif_rx(skb);
1597 stats->rx_packets++;
1598 stats->rx_bytes += length;
1600 return;
1602 drop:
1603 dev_kfree_skb(skb);
1604 stats->rx_errors++;
1605 stats->rx_dropped++;
1608 static void orinoco_rx_isr_tasklet(unsigned long data)
1610 struct net_device *dev = (struct net_device *) data;
1611 struct orinoco_private *priv = netdev_priv(dev);
1612 struct orinoco_rx_data *rx_data, *temp;
1613 struct hermes_rx_descriptor *desc;
1614 struct sk_buff *skb;
1615 unsigned long flags;
1617 /* orinoco_rx requires the driver lock, and we also need to
1618 * protect priv->rx_list, so just hold the lock over the
1619 * lot.
1621 * If orinoco_lock fails, we've unplugged the card. In this
1622 * case just abort. */
1623 if (orinoco_lock(priv, &flags) != 0)
1624 return;
1626 /* extract desc and skb from queue */
1627 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
1628 desc = rx_data->desc;
1629 skb = rx_data->skb;
1630 list_del(&rx_data->list);
1631 kfree(rx_data);
1633 orinoco_rx(dev, desc, skb);
1635 kfree(desc);
1638 orinoco_unlock(priv, &flags);
1641 /********************************************************************/
1642 /* Rx path (info frames) */
1643 /********************************************************************/
1645 static void print_linkstatus(struct net_device *dev, u16 status)
1647 char * s;
1649 if (suppress_linkstatus)
1650 return;
1652 switch (status) {
1653 case HERMES_LINKSTATUS_NOT_CONNECTED:
1654 s = "Not Connected";
1655 break;
1656 case HERMES_LINKSTATUS_CONNECTED:
1657 s = "Connected";
1658 break;
1659 case HERMES_LINKSTATUS_DISCONNECTED:
1660 s = "Disconnected";
1661 break;
1662 case HERMES_LINKSTATUS_AP_CHANGE:
1663 s = "AP Changed";
1664 break;
1665 case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
1666 s = "AP Out of Range";
1667 break;
1668 case HERMES_LINKSTATUS_AP_IN_RANGE:
1669 s = "AP In Range";
1670 break;
1671 case HERMES_LINKSTATUS_ASSOC_FAILED:
1672 s = "Association Failed";
1673 break;
1674 default:
1675 s = "UNKNOWN";
1678 printk(KERN_DEBUG "%s: New link status: %s (%04x)\n",
1679 dev->name, s, status);
1682 /* Search scan results for requested BSSID, join it if found */
1683 static void orinoco_join_ap(struct work_struct *work)
1685 struct orinoco_private *priv =
1686 container_of(work, struct orinoco_private, join_work);
1687 struct net_device *dev = priv->ndev;
1688 struct hermes *hw = &priv->hw;
1689 int err;
1690 unsigned long flags;
1691 struct join_req {
1692 u8 bssid[ETH_ALEN];
1693 __le16 channel;
1694 } __attribute__ ((packed)) req;
1695 const int atom_len = offsetof(struct prism2_scan_apinfo, atim);
1696 struct prism2_scan_apinfo *atom = NULL;
1697 int offset = 4;
1698 int found = 0;
1699 u8 *buf;
1700 u16 len;
1702 /* Allocate buffer for scan results */
1703 buf = kmalloc(MAX_SCAN_LEN, GFP_KERNEL);
1704 if (! buf)
1705 return;
1707 if (orinoco_lock(priv, &flags) != 0)
1708 goto fail_lock;
1710 /* Sanity checks in case user changed something in the meantime */
1711 if (! priv->bssid_fixed)
1712 goto out;
1714 if (strlen(priv->desired_essid) == 0)
1715 goto out;
1717 /* Read scan results from the firmware */
1718 err = hw->ops->read_ltv(hw, USER_BAP,
1719 HERMES_RID_SCANRESULTSTABLE,
1720 MAX_SCAN_LEN, &len, buf);
1721 if (err) {
1722 printk(KERN_ERR "%s: Cannot read scan results\n",
1723 dev->name);
1724 goto out;
1727 len = HERMES_RECLEN_TO_BYTES(len);
1729 /* Go through the scan results looking for the channel of the AP
1730 * we were requested to join */
1731 for (; offset + atom_len <= len; offset += atom_len) {
1732 atom = (struct prism2_scan_apinfo *) (buf + offset);
1733 if (memcmp(&atom->bssid, priv->desired_bssid, ETH_ALEN) == 0) {
1734 found = 1;
1735 break;
1739 if (! found) {
1740 DEBUG(1, "%s: Requested AP not found in scan results\n",
1741 dev->name);
1742 goto out;
1745 memcpy(req.bssid, priv->desired_bssid, ETH_ALEN);
1746 req.channel = atom->channel; /* both are little-endian */
1747 err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNFJOINREQUEST,
1748 &req);
1749 if (err)
1750 printk(KERN_ERR "%s: Error issuing join request\n", dev->name);
1752 out:
1753 orinoco_unlock(priv, &flags);
1755 fail_lock:
1756 kfree(buf);
1759 /* Send new BSSID to userspace */
1760 static void orinoco_send_bssid_wevent(struct orinoco_private *priv)
1762 struct net_device *dev = priv->ndev;
1763 struct hermes *hw = &priv->hw;
1764 union iwreq_data wrqu;
1765 int err;
1767 err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
1768 ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
1770 if (err != 0)
1771 return;
1773 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1775 /* Send event to user space */
1776 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
1779 static void orinoco_send_assocreqie_wevent(struct orinoco_private *priv)
1781 struct net_device *dev = priv->ndev;
1782 struct hermes *hw = &priv->hw;
1783 union iwreq_data wrqu;
1784 int err;
1785 u8 buf[88];
1786 u8 *ie;
1788 if (!priv->has_wpa)
1789 return;
1791 err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_REQ_INFO,
1792 sizeof(buf), NULL, &buf);
1794 if (err != 0)
1795 return;
1797 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1798 if (ie) {
1799 int rem = sizeof(buf) - (ie - &buf[0]);
1800 wrqu.data.length = ie[1] + 2;
1801 if (wrqu.data.length > rem)
1802 wrqu.data.length = rem;
1804 if (wrqu.data.length)
1805 /* Send event to user space */
1806 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, ie);
1810 static void orinoco_send_assocrespie_wevent(struct orinoco_private *priv)
1812 struct net_device *dev = priv->ndev;
1813 struct hermes *hw = &priv->hw;
1814 union iwreq_data wrqu;
1815 int err;
1816 u8 buf[88]; /* TODO: verify max size or IW_GENERIC_IE_MAX */
1817 u8 *ie;
1819 if (!priv->has_wpa)
1820 return;
1822 err = hw->ops->read_ltv(hw, USER_BAP,
1823 HERMES_RID_CURRENT_ASSOC_RESP_INFO,
1824 sizeof(buf), NULL, &buf);
1826 if (err != 0)
1827 return;
1829 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1830 if (ie) {
1831 int rem = sizeof(buf) - (ie - &buf[0]);
1832 wrqu.data.length = ie[1] + 2;
1833 if (wrqu.data.length > rem)
1834 wrqu.data.length = rem;
1836 if (wrqu.data.length)
1837 /* Send event to user space */
1838 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, ie);
1842 static void orinoco_send_wevents(struct work_struct *work)
1844 struct orinoco_private *priv =
1845 container_of(work, struct orinoco_private, wevent_work);
1846 unsigned long flags;
1848 if (orinoco_lock(priv, &flags) != 0)
1849 return;
1851 orinoco_send_assocreqie_wevent(priv);
1852 orinoco_send_assocrespie_wevent(priv);
1853 orinoco_send_bssid_wevent(priv);
1855 orinoco_unlock(priv, &flags);
1858 static inline void orinoco_clear_scan_results(struct orinoco_private *priv,
1859 unsigned long scan_age)
1861 if (priv->has_ext_scan) {
1862 struct xbss_element *bss;
1863 struct xbss_element *tmp_bss;
1865 /* Blow away current list of scan results */
1866 list_for_each_entry_safe(bss, tmp_bss, &priv->bss_list, list) {
1867 if (!scan_age ||
1868 time_after(jiffies, bss->last_scanned + scan_age)) {
1869 list_move_tail(&bss->list,
1870 &priv->bss_free_list);
1871 /* Don't blow away ->list, just BSS data */
1872 memset(&bss->bss, 0, sizeof(bss->bss));
1873 bss->last_scanned = 0;
1876 } else {
1877 struct bss_element *bss;
1878 struct bss_element *tmp_bss;
1880 /* Blow away current list of scan results */
1881 list_for_each_entry_safe(bss, tmp_bss, &priv->bss_list, list) {
1882 if (!scan_age ||
1883 time_after(jiffies, bss->last_scanned + scan_age)) {
1884 list_move_tail(&bss->list,
1885 &priv->bss_free_list);
1886 /* Don't blow away ->list, just BSS data */
1887 memset(&bss->bss, 0, sizeof(bss->bss));
1888 bss->last_scanned = 0;
1894 static void orinoco_add_ext_scan_result(struct orinoco_private *priv,
1895 struct agere_ext_scan_info *atom)
1897 struct xbss_element *bss = NULL;
1898 int found = 0;
1900 /* Try to update an existing bss first */
1901 list_for_each_entry(bss, &priv->bss_list, list) {
1902 if (compare_ether_addr(bss->bss.bssid, atom->bssid))
1903 continue;
1904 /* ESSID lengths */
1905 if (bss->bss.data[1] != atom->data[1])
1906 continue;
1907 if (memcmp(&bss->bss.data[2], &atom->data[2],
1908 atom->data[1]))
1909 continue;
1910 found = 1;
1911 break;
1914 /* Grab a bss off the free list */
1915 if (!found && !list_empty(&priv->bss_free_list)) {
1916 bss = list_entry(priv->bss_free_list.next,
1917 struct xbss_element, list);
1918 list_del(priv->bss_free_list.next);
1920 list_add_tail(&bss->list, &priv->bss_list);
1923 if (bss) {
1924 /* Always update the BSS to get latest beacon info */
1925 memcpy(&bss->bss, atom, sizeof(bss->bss));
1926 bss->last_scanned = jiffies;
1930 static int orinoco_process_scan_results(struct net_device *dev,
1931 unsigned char *buf,
1932 int len)
1934 struct orinoco_private *priv = netdev_priv(dev);
1935 int offset; /* In the scan data */
1936 union hermes_scan_info *atom;
1937 int atom_len;
1939 switch (priv->firmware_type) {
1940 case FIRMWARE_TYPE_AGERE:
1941 atom_len = sizeof(struct agere_scan_apinfo);
1942 offset = 0;
1943 break;
1944 case FIRMWARE_TYPE_SYMBOL:
1945 /* Lack of documentation necessitates this hack.
1946 * Different firmwares have 68 or 76 byte long atoms.
1947 * We try modulo first. If the length divides by both,
1948 * we check what would be the channel in the second
1949 * frame for a 68-byte atom. 76-byte atoms have 0 there.
1950 * Valid channel cannot be 0. */
1951 if (len % 76)
1952 atom_len = 68;
1953 else if (len % 68)
1954 atom_len = 76;
1955 else if (len >= 1292 && buf[68] == 0)
1956 atom_len = 76;
1957 else
1958 atom_len = 68;
1959 offset = 0;
1960 break;
1961 case FIRMWARE_TYPE_INTERSIL:
1962 offset = 4;
1963 if (priv->has_hostscan) {
1964 atom_len = le16_to_cpup((__le16 *)buf);
1965 /* Sanity check for atom_len */
1966 if (atom_len < sizeof(struct prism2_scan_apinfo)) {
1967 printk(KERN_ERR "%s: Invalid atom_len in scan "
1968 "data: %d\n", dev->name, atom_len);
1969 return -EIO;
1971 } else
1972 atom_len = offsetof(struct prism2_scan_apinfo, atim);
1973 break;
1974 default:
1975 return -EOPNOTSUPP;
1978 /* Check that we got an whole number of atoms */
1979 if ((len - offset) % atom_len) {
1980 printk(KERN_ERR "%s: Unexpected scan data length %d, "
1981 "atom_len %d, offset %d\n", dev->name, len,
1982 atom_len, offset);
1983 return -EIO;
1986 orinoco_clear_scan_results(priv, msecs_to_jiffies(15000));
1988 /* Read the entries one by one */
1989 for (; offset + atom_len <= len; offset += atom_len) {
1990 int found = 0;
1991 struct bss_element *bss = NULL;
1993 /* Get next atom */
1994 atom = (union hermes_scan_info *) (buf + offset);
1996 /* Try to update an existing bss first */
1997 list_for_each_entry(bss, &priv->bss_list, list) {
1998 if (compare_ether_addr(bss->bss.a.bssid, atom->a.bssid))
1999 continue;
2000 if (le16_to_cpu(bss->bss.a.essid_len) !=
2001 le16_to_cpu(atom->a.essid_len))
2002 continue;
2003 if (memcmp(bss->bss.a.essid, atom->a.essid,
2004 le16_to_cpu(atom->a.essid_len)))
2005 continue;
2006 found = 1;
2007 break;
2010 /* Grab a bss off the free list */
2011 if (!found && !list_empty(&priv->bss_free_list)) {
2012 bss = list_entry(priv->bss_free_list.next,
2013 struct bss_element, list);
2014 list_del(priv->bss_free_list.next);
2016 list_add_tail(&bss->list, &priv->bss_list);
2019 if (bss) {
2020 /* Always update the BSS to get latest beacon info */
2021 memcpy(&bss->bss, atom, sizeof(bss->bss));
2022 bss->last_scanned = jiffies;
2026 return 0;
2029 static void __orinoco_ev_info(struct net_device *dev, hermes_t *hw)
2031 struct orinoco_private *priv = netdev_priv(dev);
2032 u16 infofid;
2033 struct {
2034 __le16 len;
2035 __le16 type;
2036 } __attribute__ ((packed)) info;
2037 int len, type;
2038 int err;
2040 /* This is an answer to an INQUIRE command that we did earlier,
2041 * or an information "event" generated by the card
2042 * The controller return to us a pseudo frame containing
2043 * the information in question - Jean II */
2044 infofid = hermes_read_regn(hw, INFOFID);
2046 /* Read the info frame header - don't try too hard */
2047 err = hw->ops->bap_pread(hw, IRQ_BAP, &info, sizeof(info),
2048 infofid, 0);
2049 if (err) {
2050 printk(KERN_ERR "%s: error %d reading info frame. "
2051 "Frame dropped.\n", dev->name, err);
2052 return;
2055 len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
2056 type = le16_to_cpu(info.type);
2058 switch (type) {
2059 case HERMES_INQ_TALLIES: {
2060 struct hermes_tallies_frame tallies;
2061 struct iw_statistics *wstats = &priv->wstats;
2063 if (len > sizeof(tallies)) {
2064 printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
2065 dev->name, len);
2066 len = sizeof(tallies);
2069 err = hw->ops->bap_pread(hw, IRQ_BAP, &tallies, len,
2070 infofid, sizeof(info));
2071 if (err)
2072 break;
2074 /* Increment our various counters */
2075 /* wstats->discard.nwid - no wrong BSSID stuff */
2076 wstats->discard.code +=
2077 le16_to_cpu(tallies.RxWEPUndecryptable);
2078 if (len == sizeof(tallies))
2079 wstats->discard.code +=
2080 le16_to_cpu(tallies.RxDiscards_WEPICVError) +
2081 le16_to_cpu(tallies.RxDiscards_WEPExcluded);
2082 wstats->discard.misc +=
2083 le16_to_cpu(tallies.TxDiscardsWrongSA);
2084 wstats->discard.fragment +=
2085 le16_to_cpu(tallies.RxMsgInBadMsgFragments);
2086 wstats->discard.retries +=
2087 le16_to_cpu(tallies.TxRetryLimitExceeded);
2088 /* wstats->miss.beacon - no match */
2090 break;
2091 case HERMES_INQ_LINKSTATUS: {
2092 struct hermes_linkstatus linkstatus;
2093 u16 newstatus;
2094 int connected;
2096 if (priv->iw_mode == IW_MODE_MONITOR)
2097 break;
2099 if (len != sizeof(linkstatus)) {
2100 printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
2101 dev->name, len);
2102 break;
2105 err = hw->ops->bap_pread(hw, IRQ_BAP, &linkstatus, len,
2106 infofid, sizeof(info));
2107 if (err)
2108 break;
2109 newstatus = le16_to_cpu(linkstatus.linkstatus);
2111 /* Symbol firmware uses "out of range" to signal that
2112 * the hostscan frame can be requested. */
2113 if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
2114 priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
2115 priv->has_hostscan && priv->scan_inprogress) {
2116 hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
2117 break;
2120 connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
2121 || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
2122 || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
2124 if (connected)
2125 netif_carrier_on(dev);
2126 else if (!ignore_disconnect)
2127 netif_carrier_off(dev);
2129 if (newstatus != priv->last_linkstatus) {
2130 priv->last_linkstatus = newstatus;
2131 print_linkstatus(dev, newstatus);
2132 /* The info frame contains only one word which is the
2133 * status (see hermes.h). The status is pretty boring
2134 * in itself, that's why we export the new BSSID...
2135 * Jean II */
2136 schedule_work(&priv->wevent_work);
2139 break;
2140 case HERMES_INQ_SCAN:
2141 if (!priv->scan_inprogress && priv->bssid_fixed &&
2142 priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
2143 schedule_work(&priv->join_work);
2144 break;
2146 /* fall through */
2147 case HERMES_INQ_HOSTSCAN:
2148 case HERMES_INQ_HOSTSCAN_SYMBOL: {
2149 /* Result of a scanning. Contains information about
2150 * cells in the vicinity - Jean II */
2151 union iwreq_data wrqu;
2152 unsigned char *buf;
2154 /* Scan is no longer in progress */
2155 priv->scan_inprogress = 0;
2157 /* Sanity check */
2158 if (len > 4096) {
2159 printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
2160 dev->name, len);
2161 break;
2164 /* Allocate buffer for results */
2165 buf = kmalloc(len, GFP_ATOMIC);
2166 if (buf == NULL)
2167 /* No memory, so can't printk()... */
2168 break;
2170 /* Read scan data */
2171 err = hw->ops->bap_pread(hw, IRQ_BAP, (void *) buf, len,
2172 infofid, sizeof(info));
2173 if (err) {
2174 kfree(buf);
2175 break;
2178 #ifdef ORINOCO_DEBUG
2180 int i;
2181 printk(KERN_DEBUG "Scan result [%02X", buf[0]);
2182 for(i = 1; i < (len * 2); i++)
2183 printk(":%02X", buf[i]);
2184 printk("]\n");
2186 #endif /* ORINOCO_DEBUG */
2188 if (orinoco_process_scan_results(dev, buf, len) == 0) {
2189 /* Send an empty event to user space.
2190 * We don't send the received data on the event because
2191 * it would require us to do complex transcoding, and
2192 * we want to minimise the work done in the irq handler
2193 * Use a request to extract the data - Jean II */
2194 wrqu.data.length = 0;
2195 wrqu.data.flags = 0;
2196 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
2198 kfree(buf);
2200 break;
2201 case HERMES_INQ_CHANNELINFO:
2203 struct agere_ext_scan_info *bss;
2205 if (!priv->scan_inprogress) {
2206 printk(KERN_DEBUG "%s: Got chaninfo without scan, "
2207 "len=%d\n", dev->name, len);
2208 break;
2211 /* An empty result indicates that the scan is complete */
2212 if (len == 0) {
2213 union iwreq_data wrqu;
2215 /* Scan is no longer in progress */
2216 priv->scan_inprogress = 0;
2218 wrqu.data.length = 0;
2219 wrqu.data.flags = 0;
2220 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
2221 break;
2224 /* Sanity check */
2225 else if (len > sizeof(*bss)) {
2226 printk(KERN_WARNING
2227 "%s: Ext scan results too large (%d bytes). "
2228 "Truncating results to %zd bytes.\n",
2229 dev->name, len, sizeof(*bss));
2230 len = sizeof(*bss);
2231 } else if (len < (offsetof(struct agere_ext_scan_info,
2232 data) + 2)) {
2233 /* Drop this result now so we don't have to
2234 * keep checking later */
2235 printk(KERN_WARNING
2236 "%s: Ext scan results too short (%d bytes)\n",
2237 dev->name, len);
2238 break;
2241 bss = kmalloc(sizeof(*bss), GFP_ATOMIC);
2242 if (bss == NULL)
2243 break;
2245 /* Read scan data */
2246 err = hw->ops->bap_pread(hw, IRQ_BAP, (void *) bss, len,
2247 infofid, sizeof(info));
2248 if (err) {
2249 kfree(bss);
2250 break;
2253 orinoco_add_ext_scan_result(priv, bss);
2255 kfree(bss);
2256 break;
2258 case HERMES_INQ_SEC_STAT_AGERE:
2259 /* Security status (Agere specific) */
2260 /* Ignore this frame for now */
2261 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
2262 break;
2263 /* fall through */
2264 default:
2265 printk(KERN_DEBUG "%s: Unknown information frame received: "
2266 "type 0x%04x, length %d\n", dev->name, type, len);
2267 /* We don't actually do anything about it */
2268 break;
2272 static void __orinoco_ev_infdrop(struct net_device *dev, hermes_t *hw)
2274 if (net_ratelimit())
2275 printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
2278 /********************************************************************/
2279 /* Internal hardware control routines */
2280 /********************************************************************/
2282 int __orinoco_up(struct net_device *dev)
2284 struct orinoco_private *priv = netdev_priv(dev);
2285 struct hermes *hw = &priv->hw;
2286 int err;
2288 netif_carrier_off(dev); /* just to make sure */
2290 err = __orinoco_program_rids(dev);
2291 if (err) {
2292 printk(KERN_ERR "%s: Error %d configuring card\n",
2293 dev->name, err);
2294 return err;
2297 /* Fire things up again */
2298 hermes_set_irqmask(hw, ORINOCO_INTEN);
2299 err = hermes_enable_port(hw, 0);
2300 if (err) {
2301 printk(KERN_ERR "%s: Error %d enabling MAC port\n",
2302 dev->name, err);
2303 return err;
2306 netif_start_queue(dev);
2308 return 0;
2311 int __orinoco_down(struct net_device *dev)
2313 struct orinoco_private *priv = netdev_priv(dev);
2314 struct hermes *hw = &priv->hw;
2315 int err;
2317 netif_stop_queue(dev);
2319 if (! priv->hw_unavailable) {
2320 if (! priv->broken_disableport) {
2321 err = hermes_disable_port(hw, 0);
2322 if (err) {
2323 /* Some firmwares (e.g. Intersil 1.3.x) seem
2324 * to have problems disabling the port, oh
2325 * well, too bad. */
2326 printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
2327 dev->name, err);
2328 priv->broken_disableport = 1;
2331 hermes_set_irqmask(hw, 0);
2332 hermes_write_regn(hw, EVACK, 0xffff);
2335 /* firmware will have to reassociate */
2336 netif_carrier_off(dev);
2337 priv->last_linkstatus = 0xffff;
2339 return 0;
2342 static int orinoco_allocate_fid(struct net_device *dev)
2344 struct orinoco_private *priv = netdev_priv(dev);
2345 struct hermes *hw = &priv->hw;
2346 int err;
2348 err = hw->ops->allocate(hw, priv->nicbuf_size, &priv->txfid);
2349 if (err == -EIO && priv->nicbuf_size > TX_NICBUF_SIZE_BUG) {
2350 /* Try workaround for old Symbol firmware bug */
2351 printk(KERN_WARNING "%s: firmware ALLOC bug detected "
2352 "(old Symbol firmware?). Trying to work around... ",
2353 dev->name);
2355 priv->nicbuf_size = TX_NICBUF_SIZE_BUG;
2356 err = hw->ops->allocate(hw, priv->nicbuf_size, &priv->txfid);
2357 if (err)
2358 printk("failed!\n");
2359 else
2360 printk("ok.\n");
2363 return err;
2366 int orinoco_reinit_firmware(struct net_device *dev)
2368 struct orinoco_private *priv = netdev_priv(dev);
2369 struct hermes *hw = &priv->hw;
2370 int err;
2372 err = hw->ops->init(hw);
2373 if (priv->do_fw_download && !err) {
2374 err = orinoco_download(priv);
2375 if (err)
2376 priv->do_fw_download = 0;
2378 if (!err)
2379 err = orinoco_allocate_fid(dev);
2381 return err;
2384 static int __orinoco_hw_set_bitrate(struct orinoco_private *priv)
2386 hermes_t *hw = &priv->hw;
2387 int err = 0;
2389 if (priv->bitratemode >= BITRATE_TABLE_SIZE) {
2390 printk(KERN_ERR "%s: BUG: Invalid bitrate mode %d\n",
2391 priv->ndev->name, priv->bitratemode);
2392 return -EINVAL;
2395 switch (priv->firmware_type) {
2396 case FIRMWARE_TYPE_AGERE:
2397 err = hermes_write_wordrec(hw, USER_BAP,
2398 HERMES_RID_CNFTXRATECONTROL,
2399 bitrate_table[priv->bitratemode].agere_txratectrl);
2400 break;
2401 case FIRMWARE_TYPE_INTERSIL:
2402 case FIRMWARE_TYPE_SYMBOL:
2403 err = hermes_write_wordrec(hw, USER_BAP,
2404 HERMES_RID_CNFTXRATECONTROL,
2405 bitrate_table[priv->bitratemode].intersil_txratectrl);
2406 break;
2407 default:
2408 BUG();
2411 return err;
2414 /* Set fixed AP address */
2415 static int __orinoco_hw_set_wap(struct orinoco_private *priv)
2417 int roaming_flag;
2418 int err = 0;
2419 hermes_t *hw = &priv->hw;
2421 switch (priv->firmware_type) {
2422 case FIRMWARE_TYPE_AGERE:
2423 /* not supported */
2424 break;
2425 case FIRMWARE_TYPE_INTERSIL:
2426 if (priv->bssid_fixed)
2427 roaming_flag = 2;
2428 else
2429 roaming_flag = 1;
2431 err = hermes_write_wordrec(hw, USER_BAP,
2432 HERMES_RID_CNFROAMINGMODE,
2433 roaming_flag);
2434 break;
2435 case FIRMWARE_TYPE_SYMBOL:
2436 err = HERMES_WRITE_RECORD(hw, USER_BAP,
2437 HERMES_RID_CNFMANDATORYBSSID_SYMBOL,
2438 &priv->desired_bssid);
2439 break;
2441 return err;
2444 /* Change the WEP keys and/or the current keys. Can be called
2445 * either from __orinoco_hw_setup_enc() or directly from
2446 * orinoco_ioctl_setiwencode(). In the later case the association
2447 * with the AP is not broken (if the firmware can handle it),
2448 * which is needed for 802.1x implementations. */
2449 static int __orinoco_hw_setup_wepkeys(struct orinoco_private *priv)
2451 hermes_t *hw = &priv->hw;
2452 int err = 0;
2454 switch (priv->firmware_type) {
2455 case FIRMWARE_TYPE_AGERE:
2456 err = HERMES_WRITE_RECORD(hw, USER_BAP,
2457 HERMES_RID_CNFWEPKEYS_AGERE,
2458 &priv->keys);
2459 if (err)
2460 return err;
2461 err = hermes_write_wordrec(hw, USER_BAP,
2462 HERMES_RID_CNFTXKEY_AGERE,
2463 priv->tx_key);
2464 if (err)
2465 return err;
2466 break;
2467 case FIRMWARE_TYPE_INTERSIL:
2468 case FIRMWARE_TYPE_SYMBOL:
2470 int keylen;
2471 int i;
2473 /* Force uniform key length to work around firmware bugs */
2474 keylen = le16_to_cpu(priv->keys[priv->tx_key].len);
2476 if (keylen > LARGE_KEY_SIZE) {
2477 printk(KERN_ERR "%s: BUG: Key %d has oversize length %d.\n",
2478 priv->ndev->name, priv->tx_key, keylen);
2479 return -E2BIG;
2482 /* Write all 4 keys */
2483 for(i = 0; i < ORINOCO_MAX_KEYS; i++) {
2484 err = hw->ops->write_ltv(hw, USER_BAP,
2485 HERMES_RID_CNFDEFAULTKEY0 + i,
2486 HERMES_BYTES_TO_RECLEN(keylen),
2487 priv->keys[i].data);
2488 if (err)
2489 return err;
2492 /* Write the index of the key used in transmission */
2493 err = hermes_write_wordrec(hw, USER_BAP,
2494 HERMES_RID_CNFWEPDEFAULTKEYID,
2495 priv->tx_key);
2496 if (err)
2497 return err;
2499 break;
2502 return 0;
2505 static int __orinoco_hw_setup_enc(struct orinoco_private *priv)
2507 hermes_t *hw = &priv->hw;
2508 int err = 0;
2509 int master_wep_flag;
2510 int auth_flag;
2511 int enc_flag;
2513 /* Setup WEP keys for WEP and WPA */
2514 if (priv->encode_alg)
2515 __orinoco_hw_setup_wepkeys(priv);
2517 if (priv->wep_restrict)
2518 auth_flag = HERMES_AUTH_SHARED_KEY;
2519 else
2520 auth_flag = HERMES_AUTH_OPEN;
2522 if (priv->wpa_enabled)
2523 enc_flag = 2;
2524 else if (priv->encode_alg == IW_ENCODE_ALG_WEP)
2525 enc_flag = 1;
2526 else
2527 enc_flag = 0;
2529 switch (priv->firmware_type) {
2530 case FIRMWARE_TYPE_AGERE: /* Agere style WEP */
2531 if (priv->encode_alg == IW_ENCODE_ALG_WEP) {
2532 /* Enable the shared-key authentication. */
2533 err = hermes_write_wordrec(hw, USER_BAP,
2534 HERMES_RID_CNFAUTHENTICATION_AGERE,
2535 auth_flag);
2537 err = hermes_write_wordrec(hw, USER_BAP,
2538 HERMES_RID_CNFWEPENABLED_AGERE,
2539 enc_flag);
2540 if (err)
2541 return err;
2543 if (priv->has_wpa) {
2544 /* Set WPA key management */
2545 err = hermes_write_wordrec(hw, USER_BAP,
2546 HERMES_RID_CNFSETWPAAUTHMGMTSUITE_AGERE,
2547 priv->key_mgmt);
2548 if (err)
2549 return err;
2552 break;
2554 case FIRMWARE_TYPE_INTERSIL: /* Intersil style WEP */
2555 case FIRMWARE_TYPE_SYMBOL: /* Symbol style WEP */
2556 if (priv->encode_alg == IW_ENCODE_ALG_WEP) {
2557 if (priv->wep_restrict ||
2558 (priv->firmware_type == FIRMWARE_TYPE_SYMBOL))
2559 master_wep_flag = HERMES_WEP_PRIVACY_INVOKED |
2560 HERMES_WEP_EXCL_UNENCRYPTED;
2561 else
2562 master_wep_flag = HERMES_WEP_PRIVACY_INVOKED;
2564 err = hermes_write_wordrec(hw, USER_BAP,
2565 HERMES_RID_CNFAUTHENTICATION,
2566 auth_flag);
2567 if (err)
2568 return err;
2569 } else
2570 master_wep_flag = 0;
2572 if (priv->iw_mode == IW_MODE_MONITOR)
2573 master_wep_flag |= HERMES_WEP_HOST_DECRYPT;
2575 /* Master WEP setting : on/off */
2576 err = hermes_write_wordrec(hw, USER_BAP,
2577 HERMES_RID_CNFWEPFLAGS_INTERSIL,
2578 master_wep_flag);
2579 if (err)
2580 return err;
2582 break;
2585 return 0;
2588 /* key must be 32 bytes, including the tx and rx MIC keys.
2589 * rsc must be 8 bytes
2590 * tsc must be 8 bytes or NULL
2592 static int __orinoco_hw_set_tkip_key(hermes_t *hw, int key_idx, int set_tx,
2593 u8 *key, u8 *rsc, u8 *tsc)
2595 struct {
2596 __le16 idx;
2597 u8 rsc[IW_ENCODE_SEQ_MAX_SIZE];
2598 u8 key[TKIP_KEYLEN];
2599 u8 tx_mic[MIC_KEYLEN];
2600 u8 rx_mic[MIC_KEYLEN];
2601 u8 tsc[IW_ENCODE_SEQ_MAX_SIZE];
2602 } __attribute__ ((packed)) buf;
2603 int ret;
2604 int err;
2605 int k;
2606 u16 xmitting;
2608 key_idx &= 0x3;
2610 if (set_tx)
2611 key_idx |= 0x8000;
2613 buf.idx = cpu_to_le16(key_idx);
2614 memcpy(buf.key, key,
2615 sizeof(buf.key) + sizeof(buf.tx_mic) + sizeof(buf.rx_mic));
2617 if (rsc == NULL)
2618 memset(buf.rsc, 0, sizeof(buf.rsc));
2619 else
2620 memcpy(buf.rsc, rsc, sizeof(buf.rsc));
2622 if (tsc == NULL) {
2623 memset(buf.tsc, 0, sizeof(buf.tsc));
2624 buf.tsc[4] = 0x10;
2625 } else {
2626 memcpy(buf.tsc, tsc, sizeof(buf.tsc));
2629 /* Wait upto 100ms for tx queue to empty */
2630 k = 100;
2631 do {
2632 k--;
2633 udelay(1000);
2634 ret = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_TXQUEUEEMPTY,
2635 &xmitting);
2636 if (ret)
2637 break;
2638 } while ((k > 0) && xmitting);
2640 if (k == 0)
2641 ret = -ETIMEDOUT;
2643 err = HERMES_WRITE_RECORD(hw, USER_BAP,
2644 HERMES_RID_CNFADDDEFAULTTKIPKEY_AGERE,
2645 &buf);
2647 return ret ? ret : err;
2650 static int orinoco_clear_tkip_key(struct orinoco_private *priv,
2651 int key_idx)
2653 hermes_t *hw = &priv->hw;
2654 int err;
2656 memset(&priv->tkip_key[key_idx], 0, sizeof(priv->tkip_key[key_idx]));
2657 err = hermes_write_wordrec(hw, USER_BAP,
2658 HERMES_RID_CNFREMDEFAULTTKIPKEY_AGERE,
2659 key_idx);
2660 if (err)
2661 printk(KERN_WARNING "%s: Error %d clearing TKIP key %d\n",
2662 priv->ndev->name, err, key_idx);
2663 return err;
2666 static int __orinoco_program_rids(struct net_device *dev)
2668 struct orinoco_private *priv = netdev_priv(dev);
2669 hermes_t *hw = &priv->hw;
2670 int err;
2671 struct hermes_idstring idbuf;
2673 /* Set the MAC address */
2674 err = hw->ops->write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
2675 HERMES_BYTES_TO_RECLEN(ETH_ALEN), dev->dev_addr);
2676 if (err) {
2677 printk(KERN_ERR "%s: Error %d setting MAC address\n",
2678 dev->name, err);
2679 return err;
2682 /* Set up the link mode */
2683 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFPORTTYPE,
2684 priv->port_type);
2685 if (err) {
2686 printk(KERN_ERR "%s: Error %d setting port type\n",
2687 dev->name, err);
2688 return err;
2690 /* Set the channel/frequency */
2691 if (priv->channel != 0 && priv->iw_mode != IW_MODE_INFRA) {
2692 err = hermes_write_wordrec(hw, USER_BAP,
2693 HERMES_RID_CNFOWNCHANNEL,
2694 priv->channel);
2695 if (err) {
2696 printk(KERN_ERR "%s: Error %d setting channel %d\n",
2697 dev->name, err, priv->channel);
2698 return err;
2702 if (priv->has_ibss) {
2703 u16 createibss;
2705 if ((strlen(priv->desired_essid) == 0) && (priv->createibss)) {
2706 printk(KERN_WARNING "%s: This firmware requires an "
2707 "ESSID in IBSS-Ad-Hoc mode.\n", dev->name);
2708 /* With wvlan_cs, in this case, we would crash.
2709 * hopefully, this driver will behave better...
2710 * Jean II */
2711 createibss = 0;
2712 } else {
2713 createibss = priv->createibss;
2716 err = hermes_write_wordrec(hw, USER_BAP,
2717 HERMES_RID_CNFCREATEIBSS,
2718 createibss);
2719 if (err) {
2720 printk(KERN_ERR "%s: Error %d setting CREATEIBSS\n",
2721 dev->name, err);
2722 return err;
2726 /* Set the desired BSSID */
2727 err = __orinoco_hw_set_wap(priv);
2728 if (err) {
2729 printk(KERN_ERR "%s: Error %d setting AP address\n",
2730 dev->name, err);
2731 return err;
2733 /* Set the desired ESSID */
2734 idbuf.len = cpu_to_le16(strlen(priv->desired_essid));
2735 memcpy(&idbuf.val, priv->desired_essid, sizeof(idbuf.val));
2736 /* WinXP wants partner to configure OWNSSID even in IBSS mode. (jimc) */
2737 err = hw->ops->write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNSSID,
2738 HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
2739 &idbuf);
2740 if (err) {
2741 printk(KERN_ERR "%s: Error %d setting OWNSSID\n",
2742 dev->name, err);
2743 return err;
2745 err = hw->ops->write_ltv(hw, USER_BAP, HERMES_RID_CNFDESIREDSSID,
2746 HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
2747 &idbuf);
2748 if (err) {
2749 printk(KERN_ERR "%s: Error %d setting DESIREDSSID\n",
2750 dev->name, err);
2751 return err;
2754 /* Set the station name */
2755 idbuf.len = cpu_to_le16(strlen(priv->nick));
2756 memcpy(&idbuf.val, priv->nick, sizeof(idbuf.val));
2757 err = hw->ops->write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
2758 HERMES_BYTES_TO_RECLEN(strlen(priv->nick)+2),
2759 &idbuf);
2760 if (err) {
2761 printk(KERN_ERR "%s: Error %d setting nickname\n",
2762 dev->name, err);
2763 return err;
2766 /* Set AP density */
2767 if (priv->has_sensitivity) {
2768 err = hermes_write_wordrec(hw, USER_BAP,
2769 HERMES_RID_CNFSYSTEMSCALE,
2770 priv->ap_density);
2771 if (err) {
2772 printk(KERN_WARNING "%s: Error %d setting SYSTEMSCALE. "
2773 "Disabling sensitivity control\n",
2774 dev->name, err);
2776 priv->has_sensitivity = 0;
2780 /* Set RTS threshold */
2781 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
2782 priv->rts_thresh);
2783 if (err) {
2784 printk(KERN_ERR "%s: Error %d setting RTS threshold\n",
2785 dev->name, err);
2786 return err;
2789 /* Set fragmentation threshold or MWO robustness */
2790 if (priv->has_mwo)
2791 err = hermes_write_wordrec(hw, USER_BAP,
2792 HERMES_RID_CNFMWOROBUST_AGERE,
2793 priv->mwo_robust);
2794 else
2795 err = hermes_write_wordrec(hw, USER_BAP,
2796 HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
2797 priv->frag_thresh);
2798 if (err) {
2799 printk(KERN_ERR "%s: Error %d setting fragmentation\n",
2800 dev->name, err);
2801 return err;
2804 /* Set bitrate */
2805 err = __orinoco_hw_set_bitrate(priv);
2806 if (err) {
2807 printk(KERN_ERR "%s: Error %d setting bitrate\n",
2808 dev->name, err);
2809 return err;
2812 /* Set power management */
2813 if (priv->has_pm) {
2814 err = hermes_write_wordrec(hw, USER_BAP,
2815 HERMES_RID_CNFPMENABLED,
2816 priv->pm_on);
2817 if (err) {
2818 printk(KERN_ERR "%s: Error %d setting up PM\n",
2819 dev->name, err);
2820 return err;
2823 err = hermes_write_wordrec(hw, USER_BAP,
2824 HERMES_RID_CNFMULTICASTRECEIVE,
2825 priv->pm_mcast);
2826 if (err) {
2827 printk(KERN_ERR "%s: Error %d setting up PM\n",
2828 dev->name, err);
2829 return err;
2831 err = hermes_write_wordrec(hw, USER_BAP,
2832 HERMES_RID_CNFMAXSLEEPDURATION,
2833 priv->pm_period);
2834 if (err) {
2835 printk(KERN_ERR "%s: Error %d setting up PM\n",
2836 dev->name, err);
2837 return err;
2839 err = hermes_write_wordrec(hw, USER_BAP,
2840 HERMES_RID_CNFPMHOLDOVERDURATION,
2841 priv->pm_timeout);
2842 if (err) {
2843 printk(KERN_ERR "%s: Error %d setting up PM\n",
2844 dev->name, err);
2845 return err;
2849 /* Set preamble - only for Symbol so far... */
2850 if (priv->has_preamble) {
2851 err = hermes_write_wordrec(hw, USER_BAP,
2852 HERMES_RID_CNFPREAMBLE_SYMBOL,
2853 priv->preamble);
2854 if (err) {
2855 printk(KERN_ERR "%s: Error %d setting preamble\n",
2856 dev->name, err);
2857 return err;
2861 /* Set up encryption */
2862 if (priv->has_wep || priv->has_wpa) {
2863 err = __orinoco_hw_setup_enc(priv);
2864 if (err) {
2865 printk(KERN_ERR "%s: Error %d activating encryption\n",
2866 dev->name, err);
2867 return err;
2871 if (priv->iw_mode == IW_MODE_MONITOR) {
2872 /* Enable monitor mode */
2873 dev->type = ARPHRD_IEEE80211;
2874 err = hw->ops->cmd_wait(hw, HERMES_CMD_TEST |
2875 HERMES_TEST_MONITOR, 0, NULL);
2876 } else {
2877 /* Disable monitor mode */
2878 dev->type = ARPHRD_ETHER;
2879 err = hw->ops->cmd_wait(hw, HERMES_CMD_TEST |
2880 HERMES_TEST_STOP, 0, NULL);
2882 if (err)
2883 return err;
2885 /* Set promiscuity / multicast*/
2886 priv->promiscuous = 0;
2887 priv->mc_count = 0;
2889 /* FIXME: what about netif_tx_lock */
2890 __orinoco_set_multicast_list(dev);
2892 return 0;
2895 /* FIXME: return int? */
2896 static void
2897 __orinoco_set_multicast_list(struct net_device *dev)
2899 struct orinoco_private *priv = netdev_priv(dev);
2900 hermes_t *hw = &priv->hw;
2901 int err = 0;
2902 int promisc, mc_count;
2904 /* The Hermes doesn't seem to have an allmulti mode, so we go
2905 * into promiscuous mode and let the upper levels deal. */
2906 if ( (dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
2907 (dev->mc_count > MAX_MULTICAST(priv)) ) {
2908 promisc = 1;
2909 mc_count = 0;
2910 } else {
2911 promisc = 0;
2912 mc_count = dev->mc_count;
2915 if (promisc != priv->promiscuous) {
2916 err = hermes_write_wordrec(hw, USER_BAP,
2917 HERMES_RID_CNFPROMISCUOUSMODE,
2918 promisc);
2919 if (err) {
2920 printk(KERN_ERR "%s: Error %d setting PROMISCUOUSMODE to 1.\n",
2921 dev->name, err);
2922 } else
2923 priv->promiscuous = promisc;
2926 /* If we're not in promiscuous mode, then we need to set the
2927 * group address if either we want to multicast, or if we were
2928 * multicasting and want to stop */
2929 if (! promisc && (mc_count || priv->mc_count) ) {
2930 struct dev_mc_list *p = dev->mc_list;
2931 struct hermes_multicast mclist;
2932 int i;
2934 for (i = 0; i < mc_count; i++) {
2935 /* paranoia: is list shorter than mc_count? */
2936 BUG_ON(! p);
2937 /* paranoia: bad address size in list? */
2938 BUG_ON(p->dmi_addrlen != ETH_ALEN);
2940 memcpy(mclist.addr[i], p->dmi_addr, ETH_ALEN);
2941 p = p->next;
2944 if (p)
2945 printk(KERN_WARNING "%s: Multicast list is "
2946 "longer than mc_count\n", dev->name);
2948 err = hw->ops->write_ltv(hw, USER_BAP,
2949 HERMES_RID_CNFGROUPADDRESSES,
2950 HERMES_BYTES_TO_RECLEN(mc_count * ETH_ALEN),
2951 &mclist);
2952 if (err)
2953 printk(KERN_ERR "%s: Error %d setting multicast list.\n",
2954 dev->name, err);
2955 else
2956 priv->mc_count = mc_count;
2960 /* This must be called from user context, without locks held - use
2961 * schedule_work() */
2962 static void orinoco_reset(struct work_struct *work)
2964 struct orinoco_private *priv =
2965 container_of(work, struct orinoco_private, reset_work);
2966 struct net_device *dev = priv->ndev;
2967 struct hermes *hw = &priv->hw;
2968 int err;
2969 unsigned long flags;
2971 if (orinoco_lock(priv, &flags) != 0)
2972 /* When the hardware becomes available again, whatever
2973 * detects that is responsible for re-initializing
2974 * it. So no need for anything further */
2975 return;
2977 netif_stop_queue(dev);
2979 /* Shut off interrupts. Depending on what state the hardware
2980 * is in, this might not work, but we'll try anyway */
2981 hermes_set_irqmask(hw, 0);
2982 hermes_write_regn(hw, EVACK, 0xffff);
2984 priv->hw_unavailable++;
2985 priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
2986 netif_carrier_off(dev);
2988 orinoco_unlock(priv, &flags);
2990 /* Scanning support: Cleanup of driver struct */
2991 orinoco_clear_scan_results(priv, 0);
2992 priv->scan_inprogress = 0;
2994 if (priv->hard_reset) {
2995 err = (*priv->hard_reset)(priv);
2996 if (err) {
2997 printk(KERN_ERR "%s: orinoco_reset: Error %d "
2998 "performing hard reset\n", dev->name, err);
2999 goto disable;
3003 err = orinoco_reinit_firmware(dev);
3004 if (err) {
3005 printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
3006 dev->name, err);
3007 goto disable;
3010 orinoco_spin_lock(priv); /* This has to be called from user context */
3012 priv->hw_unavailable--;
3014 /* priv->open or priv->hw_unavailable might have changed while
3015 * we dropped the lock */
3016 if (priv->open && (! priv->hw_unavailable)) {
3017 err = __orinoco_up(dev);
3018 if (err) {
3019 printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
3020 dev->name, err);
3021 } else
3022 dev->trans_start = jiffies;
3025 orinoco_spin_unlock(priv);
3027 return;
3028 disable:
3029 hermes_set_irqmask(hw, 0);
3030 netif_device_detach(dev);
3031 printk(KERN_ERR "%s: Device has been disabled!\n", dev->name);
3034 /********************************************************************/
3035 /* Interrupt handler */
3036 /********************************************************************/
3038 static void __orinoco_ev_tick(struct net_device *dev, hermes_t *hw)
3040 printk(KERN_DEBUG "%s: TICK\n", dev->name);
3043 static void __orinoco_ev_wterr(struct net_device *dev, hermes_t *hw)
3045 /* This seems to happen a fair bit under load, but ignoring it
3046 seems to work fine...*/
3047 printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
3048 dev->name);
3051 irqreturn_t orinoco_interrupt(int irq, void *dev_id)
3053 struct net_device *dev = dev_id;
3054 struct orinoco_private *priv = netdev_priv(dev);
3055 hermes_t *hw = &priv->hw;
3056 int count = MAX_IRQLOOPS_PER_IRQ;
3057 u16 evstat, events;
3058 /* These are used to detect a runaway interrupt situation */
3059 /* If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
3060 * we panic and shut down the hardware */
3061 static int last_irq_jiffy = 0; /* jiffies value the last time
3062 * we were called */
3063 static int loops_this_jiffy = 0;
3064 unsigned long flags;
3066 if (orinoco_lock(priv, &flags) != 0) {
3067 /* If hw is unavailable - we don't know if the irq was
3068 * for us or not */
3069 return IRQ_HANDLED;
3072 evstat = hermes_read_regn(hw, EVSTAT);
3073 events = evstat & hw->inten;
3074 if (! events) {
3075 orinoco_unlock(priv, &flags);
3076 return IRQ_NONE;
3079 if (jiffies != last_irq_jiffy)
3080 loops_this_jiffy = 0;
3081 last_irq_jiffy = jiffies;
3083 while (events && count--) {
3084 if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
3085 printk(KERN_WARNING "%s: IRQ handler is looping too "
3086 "much! Resetting.\n", dev->name);
3087 /* Disable interrupts for now */
3088 hermes_set_irqmask(hw, 0);
3089 schedule_work(&priv->reset_work);
3090 break;
3093 /* Check the card hasn't been removed */
3094 if (! hermes_present(hw)) {
3095 DEBUG(0, "orinoco_interrupt(): card removed\n");
3096 break;
3099 if (events & HERMES_EV_TICK)
3100 __orinoco_ev_tick(dev, hw);
3101 if (events & HERMES_EV_WTERR)
3102 __orinoco_ev_wterr(dev, hw);
3103 if (events & HERMES_EV_INFDROP)
3104 __orinoco_ev_infdrop(dev, hw);
3105 if (events & HERMES_EV_INFO)
3106 __orinoco_ev_info(dev, hw);
3107 if (events & HERMES_EV_RX)
3108 __orinoco_ev_rx(dev, hw);
3109 if (events & HERMES_EV_TXEXC)
3110 __orinoco_ev_txexc(dev, hw);
3111 if (events & HERMES_EV_TX)
3112 __orinoco_ev_tx(dev, hw);
3113 if (events & HERMES_EV_ALLOC)
3114 __orinoco_ev_alloc(dev, hw);
3116 hermes_write_regn(hw, EVACK, evstat);
3118 evstat = hermes_read_regn(hw, EVSTAT);
3119 events = evstat & hw->inten;
3122 orinoco_unlock(priv, &flags);
3123 return IRQ_HANDLED;
3126 /********************************************************************/
3127 /* Power management */
3128 /********************************************************************/
3129 #if defined(CONFIG_PM_SLEEP) && !defined(CONFIG_HERMES_CACHE_FW_ON_INIT)
3130 static int orinoco_pm_notifier(struct notifier_block *notifier,
3131 unsigned long pm_event,
3132 void *unused)
3134 struct orinoco_private *priv = container_of(notifier,
3135 struct orinoco_private,
3136 pm_notifier);
3138 /* All we need to do is cache the firmware before suspend, and
3139 * release it when we come out.
3141 * Only need to do this if we're downloading firmware. */
3142 if (!priv->do_fw_download)
3143 return NOTIFY_DONE;
3145 switch (pm_event) {
3146 case PM_HIBERNATION_PREPARE:
3147 case PM_SUSPEND_PREPARE:
3148 orinoco_cache_fw(priv, 0);
3149 break;
3151 case PM_POST_RESTORE:
3152 /* Restore from hibernation failed. We need to clean
3153 * up in exactly the same way, so fall through. */
3154 case PM_POST_HIBERNATION:
3155 case PM_POST_SUSPEND:
3156 orinoco_uncache_fw(priv);
3157 break;
3159 case PM_RESTORE_PREPARE:
3160 default:
3161 break;
3164 return NOTIFY_DONE;
3167 static void orinoco_register_pm_notifier(struct orinoco_private *priv)
3169 priv->pm_notifier.notifier_call = orinoco_pm_notifier;
3170 register_pm_notifier(&priv->pm_notifier);
3173 static void orinoco_unregister_pm_notifier(struct orinoco_private *priv)
3175 unregister_pm_notifier(&priv->pm_notifier);
3177 #else /* !PM_SLEEP || HERMES_CACHE_FW_ON_INIT */
3178 #define orinoco_register_pm_notifier(priv) do { } while(0)
3179 #define orinoco_unregister_pm_notifier(priv) do { } while(0)
3180 #endif
3182 /********************************************************************/
3183 /* Initialization */
3184 /********************************************************************/
3186 struct comp_id {
3187 u16 id, variant, major, minor;
3188 } __attribute__ ((packed));
3190 static inline fwtype_t determine_firmware_type(struct comp_id *nic_id)
3192 if (nic_id->id < 0x8000)
3193 return FIRMWARE_TYPE_AGERE;
3194 else if (nic_id->id == 0x8000 && nic_id->major == 0)
3195 return FIRMWARE_TYPE_SYMBOL;
3196 else
3197 return FIRMWARE_TYPE_INTERSIL;
3200 /* Set priv->firmware type, determine firmware properties */
3201 static int determine_firmware(struct net_device *dev)
3203 struct orinoco_private *priv = netdev_priv(dev);
3204 hermes_t *hw = &priv->hw;
3205 int err;
3206 struct comp_id nic_id, sta_id;
3207 unsigned int firmver;
3208 char tmp[SYMBOL_MAX_VER_LEN+1] __attribute__((aligned(2)));
3210 /* Get the hardware version */
3211 err = HERMES_READ_RECORD(hw, USER_BAP, HERMES_RID_NICID, &nic_id);
3212 if (err) {
3213 printk(KERN_ERR "%s: Cannot read hardware identity: error %d\n",
3214 dev->name, err);
3215 return err;
3218 le16_to_cpus(&nic_id.id);
3219 le16_to_cpus(&nic_id.variant);
3220 le16_to_cpus(&nic_id.major);
3221 le16_to_cpus(&nic_id.minor);
3222 printk(KERN_DEBUG "%s: Hardware identity %04x:%04x:%04x:%04x\n",
3223 dev->name, nic_id.id, nic_id.variant,
3224 nic_id.major, nic_id.minor);
3226 priv->firmware_type = determine_firmware_type(&nic_id);
3228 /* Get the firmware version */
3229 err = HERMES_READ_RECORD(hw, USER_BAP, HERMES_RID_STAID, &sta_id);
3230 if (err) {
3231 printk(KERN_ERR "%s: Cannot read station identity: error %d\n",
3232 dev->name, err);
3233 return err;
3236 le16_to_cpus(&sta_id.id);
3237 le16_to_cpus(&sta_id.variant);
3238 le16_to_cpus(&sta_id.major);
3239 le16_to_cpus(&sta_id.minor);
3240 printk(KERN_DEBUG "%s: Station identity %04x:%04x:%04x:%04x\n",
3241 dev->name, sta_id.id, sta_id.variant,
3242 sta_id.major, sta_id.minor);
3244 switch (sta_id.id) {
3245 case 0x15:
3246 printk(KERN_ERR "%s: Primary firmware is active\n",
3247 dev->name);
3248 return -ENODEV;
3249 case 0x14b:
3250 printk(KERN_ERR "%s: Tertiary firmware is active\n",
3251 dev->name);
3252 return -ENODEV;
3253 case 0x1f: /* Intersil, Agere, Symbol Spectrum24 */
3254 case 0x21: /* Symbol Spectrum24 Trilogy */
3255 break;
3256 default:
3257 printk(KERN_NOTICE "%s: Unknown station ID, please report\n",
3258 dev->name);
3259 break;
3262 /* Default capabilities */
3263 priv->has_sensitivity = 1;
3264 priv->has_mwo = 0;
3265 priv->has_preamble = 0;
3266 priv->has_port3 = 1;
3267 priv->has_ibss = 1;
3268 priv->has_wep = 0;
3269 priv->has_big_wep = 0;
3270 priv->has_alt_txcntl = 0;
3271 priv->has_ext_scan = 0;
3272 priv->has_wpa = 0;
3273 priv->do_fw_download = 0;
3275 /* Determine capabilities from the firmware version */
3276 switch (priv->firmware_type) {
3277 case FIRMWARE_TYPE_AGERE:
3278 /* Lucent Wavelan IEEE, Lucent Orinoco, Cabletron RoamAbout,
3279 ELSA, Melco, HP, IBM, Dell 1150, Compaq 110/210 */
3280 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
3281 "Lucent/Agere %d.%02d", sta_id.major, sta_id.minor);
3283 firmver = ((unsigned long)sta_id.major << 16) | sta_id.minor;
3285 priv->has_ibss = (firmver >= 0x60006);
3286 priv->has_wep = (firmver >= 0x40020);
3287 priv->has_big_wep = 1; /* FIXME: this is wrong - how do we tell
3288 Gold cards from the others? */
3289 priv->has_mwo = (firmver >= 0x60000);
3290 priv->has_pm = (firmver >= 0x40020); /* Don't work in 7.52 ? */
3291 priv->ibss_port = 1;
3292 priv->has_hostscan = (firmver >= 0x8000a);
3293 priv->do_fw_download = 1;
3294 priv->broken_monitor = (firmver >= 0x80000);
3295 priv->has_alt_txcntl = (firmver >= 0x90000); /* All 9.x ? */
3296 priv->has_ext_scan = (firmver >= 0x90000); /* All 9.x ? */
3297 priv->has_wpa = (firmver >= 0x9002a);
3298 /* Tested with Agere firmware :
3299 * 1.16 ; 4.08 ; 4.52 ; 6.04 ; 6.16 ; 7.28 => Jean II
3300 * Tested CableTron firmware : 4.32 => Anton */
3301 break;
3302 case FIRMWARE_TYPE_SYMBOL:
3303 /* Symbol , 3Com AirConnect, Intel, Ericsson WLAN */
3304 /* Intel MAC : 00:02:B3:* */
3305 /* 3Com MAC : 00:50:DA:* */
3306 memset(tmp, 0, sizeof(tmp));
3307 /* Get the Symbol firmware version */
3308 err = hw->ops->read_ltv(hw, USER_BAP,
3309 HERMES_RID_SECONDARYVERSION_SYMBOL,
3310 SYMBOL_MAX_VER_LEN, NULL, &tmp);
3311 if (err) {
3312 printk(KERN_WARNING
3313 "%s: Error %d reading Symbol firmware info. Wildly guessing capabilities...\n",
3314 dev->name, err);
3315 firmver = 0;
3316 tmp[0] = '\0';
3317 } else {
3318 /* The firmware revision is a string, the format is
3319 * something like : "V2.20-01".
3320 * Quick and dirty parsing... - Jean II
3322 firmver = ((tmp[1] - '0') << 16) | ((tmp[3] - '0') << 12)
3323 | ((tmp[4] - '0') << 8) | ((tmp[6] - '0') << 4)
3324 | (tmp[7] - '0');
3326 tmp[SYMBOL_MAX_VER_LEN] = '\0';
3329 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
3330 "Symbol %s", tmp);
3332 priv->has_ibss = (firmver >= 0x20000);
3333 priv->has_wep = (firmver >= 0x15012);
3334 priv->has_big_wep = (firmver >= 0x20000);
3335 priv->has_pm = (firmver >= 0x20000 && firmver < 0x22000) ||
3336 (firmver >= 0x29000 && firmver < 0x30000) ||
3337 firmver >= 0x31000;
3338 priv->has_preamble = (firmver >= 0x20000);
3339 priv->ibss_port = 4;
3341 /* Symbol firmware is found on various cards, but
3342 * there has been no attempt to check firmware
3343 * download on non-spectrum_cs based cards.
3345 * Given that the Agere firmware download works
3346 * differently, we should avoid doing a firmware
3347 * download with the Symbol algorithm on non-spectrum
3348 * cards.
3350 * For now we can identify a spectrum_cs based card
3351 * because it has a firmware reset function.
3353 priv->do_fw_download = (priv->stop_fw != NULL);
3355 priv->broken_disableport = (firmver == 0x25013) ||
3356 (firmver >= 0x30000 && firmver <= 0x31000);
3357 priv->has_hostscan = (firmver >= 0x31001) ||
3358 (firmver >= 0x29057 && firmver < 0x30000);
3359 /* Tested with Intel firmware : 0x20015 => Jean II */
3360 /* Tested with 3Com firmware : 0x15012 & 0x22001 => Jean II */
3361 break;
3362 case FIRMWARE_TYPE_INTERSIL:
3363 /* D-Link, Linksys, Adtron, ZoomAir, and many others...
3364 * Samsung, Compaq 100/200 and Proxim are slightly
3365 * different and less well tested */
3366 /* D-Link MAC : 00:40:05:* */
3367 /* Addtron MAC : 00:90:D1:* */
3368 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
3369 "Intersil %d.%d.%d", sta_id.major, sta_id.minor,
3370 sta_id.variant);
3372 firmver = ((unsigned long)sta_id.major << 16) |
3373 ((unsigned long)sta_id.minor << 8) | sta_id.variant;
3375 priv->has_ibss = (firmver >= 0x000700); /* FIXME */
3376 priv->has_big_wep = priv->has_wep = (firmver >= 0x000800);
3377 priv->has_pm = (firmver >= 0x000700);
3378 priv->has_hostscan = (firmver >= 0x010301);
3380 if (firmver >= 0x000800)
3381 priv->ibss_port = 0;
3382 else {
3383 printk(KERN_NOTICE "%s: Intersil firmware earlier "
3384 "than v0.8.x - several features not supported\n",
3385 dev->name);
3386 priv->ibss_port = 1;
3388 break;
3390 printk(KERN_DEBUG "%s: Firmware determined as %s\n", dev->name,
3391 priv->fw_name);
3393 return 0;
3396 static int orinoco_init(struct net_device *dev)
3398 struct orinoco_private *priv = netdev_priv(dev);
3399 hermes_t *hw = &priv->hw;
3400 int err = 0;
3401 struct hermes_idstring nickbuf;
3402 u16 reclen;
3403 int len;
3405 /* No need to lock, the hw_unavailable flag is already set in
3406 * alloc_orinocodev() */
3407 priv->nicbuf_size = IEEE80211_MAX_FRAME_LEN + ETH_HLEN;
3409 /* Initialize the firmware */
3410 err = hw->ops->init(hw);
3411 if (err != 0) {
3412 printk(KERN_ERR "%s: failed to initialize firmware (err = %d)\n",
3413 dev->name, err);
3414 goto out;
3417 err = determine_firmware(dev);
3418 if (err != 0) {
3419 printk(KERN_ERR "%s: Incompatible firmware, aborting\n",
3420 dev->name);
3421 goto out;
3424 if (priv->do_fw_download) {
3425 #ifdef CONFIG_HERMES_CACHE_FW_ON_INIT
3426 orinoco_cache_fw(priv, 0);
3427 #endif
3429 err = orinoco_download(priv);
3430 if (err)
3431 priv->do_fw_download = 0;
3433 /* Check firmware version again */
3434 err = determine_firmware(dev);
3435 if (err != 0) {
3436 printk(KERN_ERR "%s: Incompatible firmware, aborting\n",
3437 dev->name);
3438 goto out;
3442 if (priv->has_port3)
3443 printk(KERN_DEBUG "%s: Ad-hoc demo mode supported\n", dev->name);
3444 if (priv->has_ibss)
3445 printk(KERN_DEBUG "%s: IEEE standard IBSS ad-hoc mode supported\n",
3446 dev->name);
3447 if (priv->has_wep) {
3448 printk(KERN_DEBUG "%s: WEP supported, ", dev->name);
3449 if (priv->has_big_wep)
3450 printk("104-bit key\n");
3451 else
3452 printk("40-bit key\n");
3454 if (priv->has_wpa) {
3455 printk(KERN_DEBUG "%s: WPA-PSK supported\n", dev->name);
3456 if (orinoco_mic_init(priv)) {
3457 printk(KERN_ERR "%s: Failed to setup MIC crypto "
3458 "algorithm. Disabling WPA support\n", dev->name);
3459 priv->has_wpa = 0;
3463 /* Now we have the firmware capabilities, allocate appropiate
3464 * sized scan buffers */
3465 if (orinoco_bss_data_allocate(priv))
3466 goto out;
3467 orinoco_bss_data_init(priv);
3469 /* Get the MAC address */
3470 err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
3471 ETH_ALEN, NULL, dev->dev_addr);
3472 if (err) {
3473 printk(KERN_WARNING "%s: failed to read MAC address!\n",
3474 dev->name);
3475 goto out;
3478 printk(KERN_DEBUG "%s: MAC address %pM\n",
3479 dev->name, dev->dev_addr);
3481 /* Get the station name */
3482 err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
3483 sizeof(nickbuf), &reclen, &nickbuf);
3484 if (err) {
3485 printk(KERN_ERR "%s: failed to read station name\n",
3486 dev->name);
3487 goto out;
3489 if (nickbuf.len)
3490 len = min(IW_ESSID_MAX_SIZE, (int)le16_to_cpu(nickbuf.len));
3491 else
3492 len = min(IW_ESSID_MAX_SIZE, 2 * reclen);
3493 memcpy(priv->nick, &nickbuf.val, len);
3494 priv->nick[len] = '\0';
3496 printk(KERN_DEBUG "%s: Station name \"%s\"\n", dev->name, priv->nick);
3498 err = orinoco_allocate_fid(dev);
3499 if (err) {
3500 printk(KERN_ERR "%s: failed to allocate NIC buffer!\n",
3501 dev->name);
3502 goto out;
3505 /* Get allowed channels */
3506 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CHANNELLIST,
3507 &priv->channel_mask);
3508 if (err) {
3509 printk(KERN_ERR "%s: failed to read channel list!\n",
3510 dev->name);
3511 goto out;
3514 /* Get initial AP density */
3515 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFSYSTEMSCALE,
3516 &priv->ap_density);
3517 if (err || priv->ap_density < 1 || priv->ap_density > 3) {
3518 priv->has_sensitivity = 0;
3521 /* Get initial RTS threshold */
3522 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
3523 &priv->rts_thresh);
3524 if (err) {
3525 printk(KERN_ERR "%s: failed to read RTS threshold!\n",
3526 dev->name);
3527 goto out;
3530 /* Get initial fragmentation settings */
3531 if (priv->has_mwo)
3532 err = hermes_read_wordrec(hw, USER_BAP,
3533 HERMES_RID_CNFMWOROBUST_AGERE,
3534 &priv->mwo_robust);
3535 else
3536 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
3537 &priv->frag_thresh);
3538 if (err) {
3539 printk(KERN_ERR "%s: failed to read fragmentation settings!\n",
3540 dev->name);
3541 goto out;
3544 /* Power management setup */
3545 if (priv->has_pm) {
3546 priv->pm_on = 0;
3547 priv->pm_mcast = 1;
3548 err = hermes_read_wordrec(hw, USER_BAP,
3549 HERMES_RID_CNFMAXSLEEPDURATION,
3550 &priv->pm_period);
3551 if (err) {
3552 printk(KERN_ERR "%s: failed to read power management period!\n",
3553 dev->name);
3554 goto out;
3556 err = hermes_read_wordrec(hw, USER_BAP,
3557 HERMES_RID_CNFPMHOLDOVERDURATION,
3558 &priv->pm_timeout);
3559 if (err) {
3560 printk(KERN_ERR "%s: failed to read power management timeout!\n",
3561 dev->name);
3562 goto out;
3566 /* Preamble setup */
3567 if (priv->has_preamble) {
3568 err = hermes_read_wordrec(hw, USER_BAP,
3569 HERMES_RID_CNFPREAMBLE_SYMBOL,
3570 &priv->preamble);
3571 if (err)
3572 goto out;
3575 /* Set up the default configuration */
3576 priv->iw_mode = IW_MODE_INFRA;
3577 /* By default use IEEE/IBSS ad-hoc mode if we have it */
3578 priv->prefer_port3 = priv->has_port3 && (! priv->has_ibss);
3579 set_port_type(priv);
3580 priv->channel = 0; /* use firmware default */
3582 priv->promiscuous = 0;
3583 priv->encode_alg = IW_ENCODE_ALG_NONE;
3584 priv->tx_key = 0;
3585 priv->wpa_enabled = 0;
3586 priv->tkip_cm_active = 0;
3587 priv->key_mgmt = 0;
3588 priv->wpa_ie_len = 0;
3589 priv->wpa_ie = NULL;
3591 /* Make the hardware available, as long as it hasn't been
3592 * removed elsewhere (e.g. by PCMCIA hot unplug) */
3593 orinoco_spin_lock(priv);
3594 priv->hw_unavailable--;
3595 orinoco_spin_unlock(priv);
3597 printk(KERN_DEBUG "%s: ready\n", dev->name);
3599 out:
3600 return err;
3603 struct net_device
3604 *alloc_orinocodev(int sizeof_card,
3605 struct device *device,
3606 int (*hard_reset)(struct orinoco_private *),
3607 int (*stop_fw)(struct orinoco_private *, int))
3609 struct net_device *dev;
3610 struct orinoco_private *priv;
3612 dev = alloc_etherdev(sizeof(struct orinoco_private) + sizeof_card);
3613 if (! dev)
3614 return NULL;
3615 priv = netdev_priv(dev);
3616 priv->ndev = dev;
3617 if (sizeof_card)
3618 priv->card = (void *)((unsigned long)priv
3619 + sizeof(struct orinoco_private));
3620 else
3621 priv->card = NULL;
3622 priv->dev = device;
3624 /* Setup / override net_device fields */
3625 dev->init = orinoco_init;
3626 dev->hard_start_xmit = orinoco_xmit;
3627 dev->tx_timeout = orinoco_tx_timeout;
3628 dev->watchdog_timeo = HZ; /* 1 second timeout */
3629 dev->get_stats = orinoco_get_stats;
3630 dev->ethtool_ops = &orinoco_ethtool_ops;
3631 dev->wireless_handlers = (struct iw_handler_def *)&orinoco_handler_def;
3632 #ifdef WIRELESS_SPY
3633 priv->wireless_data.spy_data = &priv->spy_data;
3634 dev->wireless_data = &priv->wireless_data;
3635 #endif
3636 dev->change_mtu = orinoco_change_mtu;
3637 dev->set_multicast_list = orinoco_set_multicast_list;
3638 /* we use the default eth_mac_addr for setting the MAC addr */
3640 /* Reserve space in skb for the SNAP header */
3641 dev->hard_header_len += ENCAPS_OVERHEAD;
3643 /* Set up default callbacks */
3644 dev->open = orinoco_open;
3645 dev->stop = orinoco_stop;
3646 priv->hard_reset = hard_reset;
3647 priv->stop_fw = stop_fw;
3649 spin_lock_init(&priv->lock);
3650 priv->open = 0;
3651 priv->hw_unavailable = 1; /* orinoco_init() must clear this
3652 * before anything else touches the
3653 * hardware */
3654 INIT_WORK(&priv->reset_work, orinoco_reset);
3655 INIT_WORK(&priv->join_work, orinoco_join_ap);
3656 INIT_WORK(&priv->wevent_work, orinoco_send_wevents);
3658 INIT_LIST_HEAD(&priv->rx_list);
3659 tasklet_init(&priv->rx_tasklet, orinoco_rx_isr_tasklet,
3660 (unsigned long) dev);
3662 netif_carrier_off(dev);
3663 priv->last_linkstatus = 0xffff;
3665 priv->cached_pri_fw = NULL;
3666 priv->cached_fw = NULL;
3668 /* Register PM notifiers */
3669 orinoco_register_pm_notifier(priv);
3671 return dev;
3674 void free_orinocodev(struct net_device *dev)
3676 struct orinoco_private *priv = netdev_priv(dev);
3677 struct orinoco_rx_data *rx_data, *temp;
3679 /* If the tasklet is scheduled when we call tasklet_kill it
3680 * will run one final time. However the tasklet will only
3681 * drain priv->rx_list if the hw is still available. */
3682 tasklet_kill(&priv->rx_tasklet);
3684 /* Explicitly drain priv->rx_list */
3685 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
3686 list_del(&rx_data->list);
3688 dev_kfree_skb(rx_data->skb);
3689 kfree(rx_data->desc);
3690 kfree(rx_data);
3693 orinoco_unregister_pm_notifier(priv);
3694 orinoco_uncache_fw(priv);
3696 priv->wpa_ie_len = 0;
3697 kfree(priv->wpa_ie);
3698 orinoco_mic_free(priv);
3699 orinoco_bss_data_free(priv);
3700 free_netdev(dev);
3703 /********************************************************************/
3704 /* Wireless extensions */
3705 /********************************************************************/
3707 /* Return : < 0 -> error code ; >= 0 -> length */
3708 static int orinoco_hw_get_essid(struct orinoco_private *priv, int *active,
3709 char buf[IW_ESSID_MAX_SIZE+1])
3711 hermes_t *hw = &priv->hw;
3712 int err = 0;
3713 struct hermes_idstring essidbuf;
3714 char *p = (char *)(&essidbuf.val);
3715 int len;
3716 unsigned long flags;
3718 if (orinoco_lock(priv, &flags) != 0)
3719 return -EBUSY;
3721 if (strlen(priv->desired_essid) > 0) {
3722 /* We read the desired SSID from the hardware rather
3723 than from priv->desired_essid, just in case the
3724 firmware is allowed to change it on us. I'm not
3725 sure about this */
3726 /* My guess is that the OWNSSID should always be whatever
3727 * we set to the card, whereas CURRENT_SSID is the one that
3728 * may change... - Jean II */
3729 u16 rid;
3731 *active = 1;
3733 rid = (priv->port_type == 3) ? HERMES_RID_CNFOWNSSID :
3734 HERMES_RID_CNFDESIREDSSID;
3736 err = hw->ops->read_ltv(hw, USER_BAP, rid, sizeof(essidbuf),
3737 NULL, &essidbuf);
3738 if (err)
3739 goto fail_unlock;
3740 } else {
3741 *active = 0;
3743 err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CURRENTSSID,
3744 sizeof(essidbuf), NULL, &essidbuf);
3745 if (err)
3746 goto fail_unlock;
3749 len = le16_to_cpu(essidbuf.len);
3750 BUG_ON(len > IW_ESSID_MAX_SIZE);
3752 memset(buf, 0, IW_ESSID_MAX_SIZE);
3753 memcpy(buf, p, len);
3754 err = len;
3756 fail_unlock:
3757 orinoco_unlock(priv, &flags);
3759 return err;
3762 static long orinoco_hw_get_freq(struct orinoco_private *priv)
3765 hermes_t *hw = &priv->hw;
3766 int err = 0;
3767 u16 channel;
3768 long freq = 0;
3769 unsigned long flags;
3771 if (orinoco_lock(priv, &flags) != 0)
3772 return -EBUSY;
3774 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CURRENTCHANNEL, &channel);
3775 if (err)
3776 goto out;
3778 /* Intersil firmware 1.3.5 returns 0 when the interface is down */
3779 if (channel == 0) {
3780 err = -EBUSY;
3781 goto out;
3784 if ( (channel < 1) || (channel > NUM_CHANNELS) ) {
3785 printk(KERN_WARNING "%s: Channel out of range (%d)!\n",
3786 priv->ndev->name, channel);
3787 err = -EBUSY;
3788 goto out;
3791 freq = channel_frequency[channel-1] * 100000;
3793 out:
3794 orinoco_unlock(priv, &flags);
3796 if (err > 0)
3797 err = -EBUSY;
3798 return err ? err : freq;
3801 static int orinoco_hw_get_bitratelist(struct orinoco_private *priv,
3802 int *numrates, s32 *rates, int max)
3804 hermes_t *hw = &priv->hw;
3805 struct hermes_idstring list;
3806 unsigned char *p = (unsigned char *)&list.val;
3807 int err = 0;
3808 int num;
3809 int i;
3810 unsigned long flags;
3812 if (orinoco_lock(priv, &flags) != 0)
3813 return -EBUSY;
3815 err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_SUPPORTEDDATARATES,
3816 sizeof(list), NULL, &list);
3817 orinoco_unlock(priv, &flags);
3819 if (err)
3820 return err;
3822 num = le16_to_cpu(list.len);
3823 *numrates = num;
3824 num = min(num, max);
3826 for (i = 0; i < num; i++) {
3827 rates[i] = (p[i] & 0x7f) * 500000; /* convert to bps */
3830 return 0;
3833 static int orinoco_ioctl_getname(struct net_device *dev,
3834 struct iw_request_info *info,
3835 char *name,
3836 char *extra)
3838 struct orinoco_private *priv = netdev_priv(dev);
3839 int numrates;
3840 int err;
3842 err = orinoco_hw_get_bitratelist(priv, &numrates, NULL, 0);
3844 if (!err && (numrates > 2))
3845 strcpy(name, "IEEE 802.11b");
3846 else
3847 strcpy(name, "IEEE 802.11-DS");
3849 return 0;
3852 static int orinoco_ioctl_setwap(struct net_device *dev,
3853 struct iw_request_info *info,
3854 struct sockaddr *ap_addr,
3855 char *extra)
3857 struct orinoco_private *priv = netdev_priv(dev);
3858 int err = -EINPROGRESS; /* Call commit handler */
3859 unsigned long flags;
3860 static const u8 off_addr[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3861 static const u8 any_addr[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3863 if (orinoco_lock(priv, &flags) != 0)
3864 return -EBUSY;
3866 /* Enable automatic roaming - no sanity checks are needed */
3867 if (memcmp(&ap_addr->sa_data, off_addr, ETH_ALEN) == 0 ||
3868 memcmp(&ap_addr->sa_data, any_addr, ETH_ALEN) == 0) {
3869 priv->bssid_fixed = 0;
3870 memset(priv->desired_bssid, 0, ETH_ALEN);
3872 /* "off" means keep existing connection */
3873 if (ap_addr->sa_data[0] == 0) {
3874 __orinoco_hw_set_wap(priv);
3875 err = 0;
3877 goto out;
3880 if (priv->firmware_type == FIRMWARE_TYPE_AGERE) {
3881 printk(KERN_WARNING "%s: Lucent/Agere firmware doesn't "
3882 "support manual roaming\n",
3883 dev->name);
3884 err = -EOPNOTSUPP;
3885 goto out;
3888 if (priv->iw_mode != IW_MODE_INFRA) {
3889 printk(KERN_WARNING "%s: Manual roaming supported only in "
3890 "managed mode\n", dev->name);
3891 err = -EOPNOTSUPP;
3892 goto out;
3895 /* Intersil firmware hangs without Desired ESSID */
3896 if (priv->firmware_type == FIRMWARE_TYPE_INTERSIL &&
3897 strlen(priv->desired_essid) == 0) {
3898 printk(KERN_WARNING "%s: Desired ESSID must be set for "
3899 "manual roaming\n", dev->name);
3900 err = -EOPNOTSUPP;
3901 goto out;
3904 /* Finally, enable manual roaming */
3905 priv->bssid_fixed = 1;
3906 memcpy(priv->desired_bssid, &ap_addr->sa_data, ETH_ALEN);
3908 out:
3909 orinoco_unlock(priv, &flags);
3910 return err;
3913 static int orinoco_ioctl_getwap(struct net_device *dev,
3914 struct iw_request_info *info,
3915 struct sockaddr *ap_addr,
3916 char *extra)
3918 struct orinoco_private *priv = netdev_priv(dev);
3920 hermes_t *hw = &priv->hw;
3921 int err = 0;
3922 unsigned long flags;
3924 if (orinoco_lock(priv, &flags) != 0)
3925 return -EBUSY;
3927 ap_addr->sa_family = ARPHRD_ETHER;
3928 err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
3929 ETH_ALEN, NULL, ap_addr->sa_data);
3931 orinoco_unlock(priv, &flags);
3933 return err;
3936 static int orinoco_ioctl_setmode(struct net_device *dev,
3937 struct iw_request_info *info,
3938 u32 *mode,
3939 char *extra)
3941 struct orinoco_private *priv = netdev_priv(dev);
3942 int err = -EINPROGRESS; /* Call commit handler */
3943 unsigned long flags;
3945 if (priv->iw_mode == *mode)
3946 return 0;
3948 if (orinoco_lock(priv, &flags) != 0)
3949 return -EBUSY;
3951 switch (*mode) {
3952 case IW_MODE_ADHOC:
3953 if (!priv->has_ibss && !priv->has_port3)
3954 err = -EOPNOTSUPP;
3955 break;
3957 case IW_MODE_INFRA:
3958 break;
3960 case IW_MODE_MONITOR:
3961 if (priv->broken_monitor && !force_monitor) {
3962 printk(KERN_WARNING "%s: Monitor mode support is "
3963 "buggy in this firmware, not enabling\n",
3964 dev->name);
3965 err = -EOPNOTSUPP;
3967 break;
3969 default:
3970 err = -EOPNOTSUPP;
3971 break;
3974 if (err == -EINPROGRESS) {
3975 priv->iw_mode = *mode;
3976 set_port_type(priv);
3979 orinoco_unlock(priv, &flags);
3981 return err;
3984 static int orinoco_ioctl_getmode(struct net_device *dev,
3985 struct iw_request_info *info,
3986 u32 *mode,
3987 char *extra)
3989 struct orinoco_private *priv = netdev_priv(dev);
3991 *mode = priv->iw_mode;
3992 return 0;
3995 static int orinoco_ioctl_getiwrange(struct net_device *dev,
3996 struct iw_request_info *info,
3997 struct iw_point *rrq,
3998 char *extra)
4000 struct orinoco_private *priv = netdev_priv(dev);
4001 int err = 0;
4002 struct iw_range *range = (struct iw_range *) extra;
4003 int numrates;
4004 int i, k;
4006 rrq->length = sizeof(struct iw_range);
4007 memset(range, 0, sizeof(struct iw_range));
4009 range->we_version_compiled = WIRELESS_EXT;
4010 range->we_version_source = 22;
4012 /* Set available channels/frequencies */
4013 range->num_channels = NUM_CHANNELS;
4014 k = 0;
4015 for (i = 0; i < NUM_CHANNELS; i++) {
4016 if (priv->channel_mask & (1 << i)) {
4017 range->freq[k].i = i + 1;
4018 range->freq[k].m = channel_frequency[i] * 100000;
4019 range->freq[k].e = 1;
4020 k++;
4023 if (k >= IW_MAX_FREQUENCIES)
4024 break;
4026 range->num_frequency = k;
4027 range->sensitivity = 3;
4029 if (priv->has_wep) {
4030 range->max_encoding_tokens = ORINOCO_MAX_KEYS;
4031 range->encoding_size[0] = SMALL_KEY_SIZE;
4032 range->num_encoding_sizes = 1;
4034 if (priv->has_big_wep) {
4035 range->encoding_size[1] = LARGE_KEY_SIZE;
4036 range->num_encoding_sizes = 2;
4040 if (priv->has_wpa)
4041 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_CIPHER_TKIP;
4043 if ((priv->iw_mode == IW_MODE_ADHOC) && (!SPY_NUMBER(priv))){
4044 /* Quality stats meaningless in ad-hoc mode */
4045 } else {
4046 range->max_qual.qual = 0x8b - 0x2f;
4047 range->max_qual.level = 0x2f - 0x95 - 1;
4048 range->max_qual.noise = 0x2f - 0x95 - 1;
4049 /* Need to get better values */
4050 range->avg_qual.qual = 0x24;
4051 range->avg_qual.level = 0xC2;
4052 range->avg_qual.noise = 0x9E;
4055 err = orinoco_hw_get_bitratelist(priv, &numrates,
4056 range->bitrate, IW_MAX_BITRATES);
4057 if (err)
4058 return err;
4059 range->num_bitrates = numrates;
4061 /* Set an indication of the max TCP throughput in bit/s that we can
4062 * expect using this interface. May be use for QoS stuff...
4063 * Jean II */
4064 if (numrates > 2)
4065 range->throughput = 5 * 1000 * 1000; /* ~5 Mb/s */
4066 else
4067 range->throughput = 1.5 * 1000 * 1000; /* ~1.5 Mb/s */
4069 range->min_rts = 0;
4070 range->max_rts = 2347;
4071 range->min_frag = 256;
4072 range->max_frag = 2346;
4074 range->min_pmp = 0;
4075 range->max_pmp = 65535000;
4076 range->min_pmt = 0;
4077 range->max_pmt = 65535 * 1000; /* ??? */
4078 range->pmp_flags = IW_POWER_PERIOD;
4079 range->pmt_flags = IW_POWER_TIMEOUT;
4080 range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_UNICAST_R;
4082 range->retry_capa = IW_RETRY_LIMIT | IW_RETRY_LIFETIME;
4083 range->retry_flags = IW_RETRY_LIMIT;
4084 range->r_time_flags = IW_RETRY_LIFETIME;
4085 range->min_retry = 0;
4086 range->max_retry = 65535; /* ??? */
4087 range->min_r_time = 0;
4088 range->max_r_time = 65535 * 1000; /* ??? */
4090 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
4091 range->scan_capa = IW_SCAN_CAPA_ESSID;
4092 else
4093 range->scan_capa = IW_SCAN_CAPA_NONE;
4095 /* Event capability (kernel) */
4096 IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
4097 /* Event capability (driver) */
4098 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWTHRSPY);
4099 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
4100 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
4101 IW_EVENT_CAPA_SET(range->event_capa, IWEVTXDROP);
4103 return 0;
4106 static int orinoco_ioctl_setiwencode(struct net_device *dev,
4107 struct iw_request_info *info,
4108 struct iw_point *erq,
4109 char *keybuf)
4111 struct orinoco_private *priv = netdev_priv(dev);
4112 int index = (erq->flags & IW_ENCODE_INDEX) - 1;
4113 int setindex = priv->tx_key;
4114 int encode_alg = priv->encode_alg;
4115 int restricted = priv->wep_restrict;
4116 u16 xlen = 0;
4117 int err = -EINPROGRESS; /* Call commit handler */
4118 unsigned long flags;
4120 if (! priv->has_wep)
4121 return -EOPNOTSUPP;
4123 if (erq->pointer) {
4124 /* We actually have a key to set - check its length */
4125 if (erq->length > LARGE_KEY_SIZE)
4126 return -E2BIG;
4128 if ( (erq->length > SMALL_KEY_SIZE) && !priv->has_big_wep )
4129 return -E2BIG;
4132 if (orinoco_lock(priv, &flags) != 0)
4133 return -EBUSY;
4135 /* Clear any TKIP key we have */
4136 if ((priv->has_wpa) && (priv->encode_alg == IW_ENCODE_ALG_TKIP))
4137 (void) orinoco_clear_tkip_key(priv, setindex);
4139 if (erq->length > 0) {
4140 if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
4141 index = priv->tx_key;
4143 /* Adjust key length to a supported value */
4144 if (erq->length > SMALL_KEY_SIZE) {
4145 xlen = LARGE_KEY_SIZE;
4146 } else if (erq->length > 0) {
4147 xlen = SMALL_KEY_SIZE;
4148 } else
4149 xlen = 0;
4151 /* Switch on WEP if off */
4152 if ((encode_alg != IW_ENCODE_ALG_WEP) && (xlen > 0)) {
4153 setindex = index;
4154 encode_alg = IW_ENCODE_ALG_WEP;
4156 } else {
4157 /* Important note : if the user do "iwconfig eth0 enc off",
4158 * we will arrive there with an index of -1. This is valid
4159 * but need to be taken care off... Jean II */
4160 if ((index < 0) || (index >= ORINOCO_MAX_KEYS)) {
4161 if((index != -1) || (erq->flags == 0)) {
4162 err = -EINVAL;
4163 goto out;
4165 } else {
4166 /* Set the index : Check that the key is valid */
4167 if(priv->keys[index].len == 0) {
4168 err = -EINVAL;
4169 goto out;
4171 setindex = index;
4175 if (erq->flags & IW_ENCODE_DISABLED)
4176 encode_alg = IW_ENCODE_ALG_NONE;
4177 if (erq->flags & IW_ENCODE_OPEN)
4178 restricted = 0;
4179 if (erq->flags & IW_ENCODE_RESTRICTED)
4180 restricted = 1;
4182 if (erq->pointer && erq->length > 0) {
4183 priv->keys[index].len = cpu_to_le16(xlen);
4184 memset(priv->keys[index].data, 0,
4185 sizeof(priv->keys[index].data));
4186 memcpy(priv->keys[index].data, keybuf, erq->length);
4188 priv->tx_key = setindex;
4190 /* Try fast key change if connected and only keys are changed */
4191 if ((priv->encode_alg == encode_alg) &&
4192 (priv->wep_restrict == restricted) &&
4193 netif_carrier_ok(dev)) {
4194 err = __orinoco_hw_setup_wepkeys(priv);
4195 /* No need to commit if successful */
4196 goto out;
4199 priv->encode_alg = encode_alg;
4200 priv->wep_restrict = restricted;
4202 out:
4203 orinoco_unlock(priv, &flags);
4205 return err;
4208 static int orinoco_ioctl_getiwencode(struct net_device *dev,
4209 struct iw_request_info *info,
4210 struct iw_point *erq,
4211 char *keybuf)
4213 struct orinoco_private *priv = netdev_priv(dev);
4214 int index = (erq->flags & IW_ENCODE_INDEX) - 1;
4215 u16 xlen = 0;
4216 unsigned long flags;
4218 if (! priv->has_wep)
4219 return -EOPNOTSUPP;
4221 if (orinoco_lock(priv, &flags) != 0)
4222 return -EBUSY;
4224 if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
4225 index = priv->tx_key;
4227 erq->flags = 0;
4228 if (!priv->encode_alg)
4229 erq->flags |= IW_ENCODE_DISABLED;
4230 erq->flags |= index + 1;
4232 if (priv->wep_restrict)
4233 erq->flags |= IW_ENCODE_RESTRICTED;
4234 else
4235 erq->flags |= IW_ENCODE_OPEN;
4237 xlen = le16_to_cpu(priv->keys[index].len);
4239 erq->length = xlen;
4241 memcpy(keybuf, priv->keys[index].data, ORINOCO_MAX_KEY_SIZE);
4243 orinoco_unlock(priv, &flags);
4244 return 0;
4247 static int orinoco_ioctl_setessid(struct net_device *dev,
4248 struct iw_request_info *info,
4249 struct iw_point *erq,
4250 char *essidbuf)
4252 struct orinoco_private *priv = netdev_priv(dev);
4253 unsigned long flags;
4255 /* Note : ESSID is ignored in Ad-Hoc demo mode, but we can set it
4256 * anyway... - Jean II */
4258 /* Hum... Should not use Wireless Extension constant (may change),
4259 * should use our own... - Jean II */
4260 if (erq->length > IW_ESSID_MAX_SIZE)
4261 return -E2BIG;
4263 if (orinoco_lock(priv, &flags) != 0)
4264 return -EBUSY;
4266 /* NULL the string (for NULL termination & ESSID = ANY) - Jean II */
4267 memset(priv->desired_essid, 0, sizeof(priv->desired_essid));
4269 /* If not ANY, get the new ESSID */
4270 if (erq->flags) {
4271 memcpy(priv->desired_essid, essidbuf, erq->length);
4274 orinoco_unlock(priv, &flags);
4276 return -EINPROGRESS; /* Call commit handler */
4279 static int orinoco_ioctl_getessid(struct net_device *dev,
4280 struct iw_request_info *info,
4281 struct iw_point *erq,
4282 char *essidbuf)
4284 struct orinoco_private *priv = netdev_priv(dev);
4285 int active;
4286 int err = 0;
4287 unsigned long flags;
4289 if (netif_running(dev)) {
4290 err = orinoco_hw_get_essid(priv, &active, essidbuf);
4291 if (err < 0)
4292 return err;
4293 erq->length = err;
4294 } else {
4295 if (orinoco_lock(priv, &flags) != 0)
4296 return -EBUSY;
4297 memcpy(essidbuf, priv->desired_essid, IW_ESSID_MAX_SIZE);
4298 erq->length = strlen(priv->desired_essid);
4299 orinoco_unlock(priv, &flags);
4302 erq->flags = 1;
4304 return 0;
4307 static int orinoco_ioctl_setnick(struct net_device *dev,
4308 struct iw_request_info *info,
4309 struct iw_point *nrq,
4310 char *nickbuf)
4312 struct orinoco_private *priv = netdev_priv(dev);
4313 unsigned long flags;
4315 if (nrq->length > IW_ESSID_MAX_SIZE)
4316 return -E2BIG;
4318 if (orinoco_lock(priv, &flags) != 0)
4319 return -EBUSY;
4321 memset(priv->nick, 0, sizeof(priv->nick));
4322 memcpy(priv->nick, nickbuf, nrq->length);
4324 orinoco_unlock(priv, &flags);
4326 return -EINPROGRESS; /* Call commit handler */
4329 static int orinoco_ioctl_getnick(struct net_device *dev,
4330 struct iw_request_info *info,
4331 struct iw_point *nrq,
4332 char *nickbuf)
4334 struct orinoco_private *priv = netdev_priv(dev);
4335 unsigned long flags;
4337 if (orinoco_lock(priv, &flags) != 0)
4338 return -EBUSY;
4340 memcpy(nickbuf, priv->nick, IW_ESSID_MAX_SIZE);
4341 orinoco_unlock(priv, &flags);
4343 nrq->length = strlen(priv->nick);
4345 return 0;
4348 static int orinoco_ioctl_setfreq(struct net_device *dev,
4349 struct iw_request_info *info,
4350 struct iw_freq *frq,
4351 char *extra)
4353 struct orinoco_private *priv = netdev_priv(dev);
4354 int chan = -1;
4355 unsigned long flags;
4356 int err = -EINPROGRESS; /* Call commit handler */
4358 /* In infrastructure mode the AP sets the channel */
4359 if (priv->iw_mode == IW_MODE_INFRA)
4360 return -EBUSY;
4362 if ( (frq->e == 0) && (frq->m <= 1000) ) {
4363 /* Setting by channel number */
4364 chan = frq->m;
4365 } else {
4366 /* Setting by frequency - search the table */
4367 int mult = 1;
4368 int i;
4370 for (i = 0; i < (6 - frq->e); i++)
4371 mult *= 10;
4373 for (i = 0; i < NUM_CHANNELS; i++)
4374 if (frq->m == (channel_frequency[i] * mult))
4375 chan = i+1;
4378 if ( (chan < 1) || (chan > NUM_CHANNELS) ||
4379 ! (priv->channel_mask & (1 << (chan-1)) ) )
4380 return -EINVAL;
4382 if (orinoco_lock(priv, &flags) != 0)
4383 return -EBUSY;
4385 priv->channel = chan;
4386 if (priv->iw_mode == IW_MODE_MONITOR) {
4387 /* Fast channel change - no commit if successful */
4388 hermes_t *hw = &priv->hw;
4389 err = hw->ops->cmd_wait(hw, HERMES_CMD_TEST |
4390 HERMES_TEST_SET_CHANNEL,
4391 chan, NULL);
4393 orinoco_unlock(priv, &flags);
4395 return err;
4398 static int orinoco_ioctl_getfreq(struct net_device *dev,
4399 struct iw_request_info *info,
4400 struct iw_freq *frq,
4401 char *extra)
4403 struct orinoco_private *priv = netdev_priv(dev);
4404 int tmp;
4406 /* Locking done in there */
4407 tmp = orinoco_hw_get_freq(priv);
4408 if (tmp < 0) {
4409 return tmp;
4412 frq->m = tmp;
4413 frq->e = 1;
4415 return 0;
4418 static int orinoco_ioctl_getsens(struct net_device *dev,
4419 struct iw_request_info *info,
4420 struct iw_param *srq,
4421 char *extra)
4423 struct orinoco_private *priv = netdev_priv(dev);
4424 hermes_t *hw = &priv->hw;
4425 u16 val;
4426 int err;
4427 unsigned long flags;
4429 if (!priv->has_sensitivity)
4430 return -EOPNOTSUPP;
4432 if (orinoco_lock(priv, &flags) != 0)
4433 return -EBUSY;
4434 err = hermes_read_wordrec(hw, USER_BAP,
4435 HERMES_RID_CNFSYSTEMSCALE, &val);
4436 orinoco_unlock(priv, &flags);
4438 if (err)
4439 return err;
4441 srq->value = val;
4442 srq->fixed = 0; /* auto */
4444 return 0;
4447 static int orinoco_ioctl_setsens(struct net_device *dev,
4448 struct iw_request_info *info,
4449 struct iw_param *srq,
4450 char *extra)
4452 struct orinoco_private *priv = netdev_priv(dev);
4453 int val = srq->value;
4454 unsigned long flags;
4456 if (!priv->has_sensitivity)
4457 return -EOPNOTSUPP;
4459 if ((val < 1) || (val > 3))
4460 return -EINVAL;
4462 if (orinoco_lock(priv, &flags) != 0)
4463 return -EBUSY;
4464 priv->ap_density = val;
4465 orinoco_unlock(priv, &flags);
4467 return -EINPROGRESS; /* Call commit handler */
4470 static int orinoco_ioctl_setrts(struct net_device *dev,
4471 struct iw_request_info *info,
4472 struct iw_param *rrq,
4473 char *extra)
4475 struct orinoco_private *priv = netdev_priv(dev);
4476 int val = rrq->value;
4477 unsigned long flags;
4479 if (rrq->disabled)
4480 val = 2347;
4482 if ( (val < 0) || (val > 2347) )
4483 return -EINVAL;
4485 if (orinoco_lock(priv, &flags) != 0)
4486 return -EBUSY;
4488 priv->rts_thresh = val;
4489 orinoco_unlock(priv, &flags);
4491 return -EINPROGRESS; /* Call commit handler */
4494 static int orinoco_ioctl_getrts(struct net_device *dev,
4495 struct iw_request_info *info,
4496 struct iw_param *rrq,
4497 char *extra)
4499 struct orinoco_private *priv = netdev_priv(dev);
4501 rrq->value = priv->rts_thresh;
4502 rrq->disabled = (rrq->value == 2347);
4503 rrq->fixed = 1;
4505 return 0;
4508 static int orinoco_ioctl_setfrag(struct net_device *dev,
4509 struct iw_request_info *info,
4510 struct iw_param *frq,
4511 char *extra)
4513 struct orinoco_private *priv = netdev_priv(dev);
4514 int err = -EINPROGRESS; /* Call commit handler */
4515 unsigned long flags;
4517 if (orinoco_lock(priv, &flags) != 0)
4518 return -EBUSY;
4520 if (priv->has_mwo) {
4521 if (frq->disabled)
4522 priv->mwo_robust = 0;
4523 else {
4524 if (frq->fixed)
4525 printk(KERN_WARNING "%s: Fixed fragmentation is "
4526 "not supported on this firmware. "
4527 "Using MWO robust instead.\n", dev->name);
4528 priv->mwo_robust = 1;
4530 } else {
4531 if (frq->disabled)
4532 priv->frag_thresh = 2346;
4533 else {
4534 if ( (frq->value < 256) || (frq->value > 2346) )
4535 err = -EINVAL;
4536 else
4537 priv->frag_thresh = frq->value & ~0x1; /* must be even */
4541 orinoco_unlock(priv, &flags);
4543 return err;
4546 static int orinoco_ioctl_getfrag(struct net_device *dev,
4547 struct iw_request_info *info,
4548 struct iw_param *frq,
4549 char *extra)
4551 struct orinoco_private *priv = netdev_priv(dev);
4552 hermes_t *hw = &priv->hw;
4553 int err;
4554 u16 val;
4555 unsigned long flags;
4557 if (orinoco_lock(priv, &flags) != 0)
4558 return -EBUSY;
4560 if (priv->has_mwo) {
4561 err = hermes_read_wordrec(hw, USER_BAP,
4562 HERMES_RID_CNFMWOROBUST_AGERE,
4563 &val);
4564 if (err)
4565 val = 0;
4567 frq->value = val ? 2347 : 0;
4568 frq->disabled = ! val;
4569 frq->fixed = 0;
4570 } else {
4571 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
4572 &val);
4573 if (err)
4574 val = 0;
4576 frq->value = val;
4577 frq->disabled = (val >= 2346);
4578 frq->fixed = 1;
4581 orinoco_unlock(priv, &flags);
4583 return err;
4586 static int orinoco_ioctl_setrate(struct net_device *dev,
4587 struct iw_request_info *info,
4588 struct iw_param *rrq,
4589 char *extra)
4591 struct orinoco_private *priv = netdev_priv(dev);
4592 int ratemode = -1;
4593 int bitrate; /* 100s of kilobits */
4594 int i;
4595 unsigned long flags;
4597 /* As the user space doesn't know our highest rate, it uses -1
4598 * to ask us to set the highest rate. Test it using "iwconfig
4599 * ethX rate auto" - Jean II */
4600 if (rrq->value == -1)
4601 bitrate = 110;
4602 else {
4603 if (rrq->value % 100000)
4604 return -EINVAL;
4605 bitrate = rrq->value / 100000;
4608 if ( (bitrate != 10) && (bitrate != 20) &&
4609 (bitrate != 55) && (bitrate != 110) )
4610 return -EINVAL;
4612 for (i = 0; i < BITRATE_TABLE_SIZE; i++)
4613 if ( (bitrate_table[i].bitrate == bitrate) &&
4614 (bitrate_table[i].automatic == ! rrq->fixed) ) {
4615 ratemode = i;
4616 break;
4619 if (ratemode == -1)
4620 return -EINVAL;
4622 if (orinoco_lock(priv, &flags) != 0)
4623 return -EBUSY;
4624 priv->bitratemode = ratemode;
4625 orinoco_unlock(priv, &flags);
4627 return -EINPROGRESS;
4630 static int orinoco_ioctl_getrate(struct net_device *dev,
4631 struct iw_request_info *info,
4632 struct iw_param *rrq,
4633 char *extra)
4635 struct orinoco_private *priv = netdev_priv(dev);
4636 hermes_t *hw = &priv->hw;
4637 int err = 0;
4638 int ratemode;
4639 int i;
4640 u16 val;
4641 unsigned long flags;
4643 if (orinoco_lock(priv, &flags) != 0)
4644 return -EBUSY;
4646 ratemode = priv->bitratemode;
4648 BUG_ON((ratemode < 0) || (ratemode >= BITRATE_TABLE_SIZE));
4650 rrq->value = bitrate_table[ratemode].bitrate * 100000;
4651 rrq->fixed = ! bitrate_table[ratemode].automatic;
4652 rrq->disabled = 0;
4654 /* If the interface is running we try to find more about the
4655 current mode */
4656 if (netif_running(dev)) {
4657 err = hermes_read_wordrec(hw, USER_BAP,
4658 HERMES_RID_CURRENTTXRATE, &val);
4659 if (err)
4660 goto out;
4662 switch (priv->firmware_type) {
4663 case FIRMWARE_TYPE_AGERE: /* Lucent style rate */
4664 /* Note : in Lucent firmware, the return value of
4665 * HERMES_RID_CURRENTTXRATE is the bitrate in Mb/s,
4666 * and therefore is totally different from the
4667 * encoding of HERMES_RID_CNFTXRATECONTROL.
4668 * Don't forget that 6Mb/s is really 5.5Mb/s */
4669 if (val == 6)
4670 rrq->value = 5500000;
4671 else
4672 rrq->value = val * 1000000;
4673 break;
4674 case FIRMWARE_TYPE_INTERSIL: /* Intersil style rate */
4675 case FIRMWARE_TYPE_SYMBOL: /* Symbol style rate */
4676 for (i = 0; i < BITRATE_TABLE_SIZE; i++)
4677 if (bitrate_table[i].intersil_txratectrl == val) {
4678 ratemode = i;
4679 break;
4681 if (i >= BITRATE_TABLE_SIZE)
4682 printk(KERN_INFO "%s: Unable to determine current bitrate (0x%04hx)\n",
4683 dev->name, val);
4685 rrq->value = bitrate_table[ratemode].bitrate * 100000;
4686 break;
4687 default:
4688 BUG();
4692 out:
4693 orinoco_unlock(priv, &flags);
4695 return err;
4698 static int orinoco_ioctl_setpower(struct net_device *dev,
4699 struct iw_request_info *info,
4700 struct iw_param *prq,
4701 char *extra)
4703 struct orinoco_private *priv = netdev_priv(dev);
4704 int err = -EINPROGRESS; /* Call commit handler */
4705 unsigned long flags;
4707 if (orinoco_lock(priv, &flags) != 0)
4708 return -EBUSY;
4710 if (prq->disabled) {
4711 priv->pm_on = 0;
4712 } else {
4713 switch (prq->flags & IW_POWER_MODE) {
4714 case IW_POWER_UNICAST_R:
4715 priv->pm_mcast = 0;
4716 priv->pm_on = 1;
4717 break;
4718 case IW_POWER_ALL_R:
4719 priv->pm_mcast = 1;
4720 priv->pm_on = 1;
4721 break;
4722 case IW_POWER_ON:
4723 /* No flags : but we may have a value - Jean II */
4724 break;
4725 default:
4726 err = -EINVAL;
4727 goto out;
4730 if (prq->flags & IW_POWER_TIMEOUT) {
4731 priv->pm_on = 1;
4732 priv->pm_timeout = prq->value / 1000;
4734 if (prq->flags & IW_POWER_PERIOD) {
4735 priv->pm_on = 1;
4736 priv->pm_period = prq->value / 1000;
4738 /* It's valid to not have a value if we are just toggling
4739 * the flags... Jean II */
4740 if(!priv->pm_on) {
4741 err = -EINVAL;
4742 goto out;
4746 out:
4747 orinoco_unlock(priv, &flags);
4749 return err;
4752 static int orinoco_ioctl_getpower(struct net_device *dev,
4753 struct iw_request_info *info,
4754 struct iw_param *prq,
4755 char *extra)
4757 struct orinoco_private *priv = netdev_priv(dev);
4758 hermes_t *hw = &priv->hw;
4759 int err = 0;
4760 u16 enable, period, timeout, mcast;
4761 unsigned long flags;
4763 if (orinoco_lock(priv, &flags) != 0)
4764 return -EBUSY;
4766 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFPMENABLED, &enable);
4767 if (err)
4768 goto out;
4770 err = hermes_read_wordrec(hw, USER_BAP,
4771 HERMES_RID_CNFMAXSLEEPDURATION, &period);
4772 if (err)
4773 goto out;
4775 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFPMHOLDOVERDURATION, &timeout);
4776 if (err)
4777 goto out;
4779 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFMULTICASTRECEIVE, &mcast);
4780 if (err)
4781 goto out;
4783 prq->disabled = !enable;
4784 /* Note : by default, display the period */
4785 if ((prq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
4786 prq->flags = IW_POWER_TIMEOUT;
4787 prq->value = timeout * 1000;
4788 } else {
4789 prq->flags = IW_POWER_PERIOD;
4790 prq->value = period * 1000;
4792 if (mcast)
4793 prq->flags |= IW_POWER_ALL_R;
4794 else
4795 prq->flags |= IW_POWER_UNICAST_R;
4797 out:
4798 orinoco_unlock(priv, &flags);
4800 return err;
4803 static int orinoco_ioctl_set_encodeext(struct net_device *dev,
4804 struct iw_request_info *info,
4805 union iwreq_data *wrqu,
4806 char *extra)
4808 struct orinoco_private *priv = netdev_priv(dev);
4809 struct iw_point *encoding = &wrqu->encoding;
4810 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
4811 int idx, alg = ext->alg, set_key = 1;
4812 unsigned long flags;
4813 int err = -EINVAL;
4814 u16 key_len;
4816 if (orinoco_lock(priv, &flags) != 0)
4817 return -EBUSY;
4819 /* Determine and validate the key index */
4820 idx = encoding->flags & IW_ENCODE_INDEX;
4821 if (idx) {
4822 if ((idx < 1) || (idx > 4))
4823 goto out;
4824 idx--;
4825 } else
4826 idx = priv->tx_key;
4828 if (encoding->flags & IW_ENCODE_DISABLED)
4829 alg = IW_ENCODE_ALG_NONE;
4831 if (priv->has_wpa && (alg != IW_ENCODE_ALG_TKIP)) {
4832 /* Clear any TKIP TX key we had */
4833 (void) orinoco_clear_tkip_key(priv, priv->tx_key);
4836 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
4837 priv->tx_key = idx;
4838 set_key = ((alg == IW_ENCODE_ALG_TKIP) ||
4839 (ext->key_len > 0)) ? 1 : 0;
4842 if (set_key) {
4843 /* Set the requested key first */
4844 switch (alg) {
4845 case IW_ENCODE_ALG_NONE:
4846 priv->encode_alg = alg;
4847 priv->keys[idx].len = 0;
4848 break;
4850 case IW_ENCODE_ALG_WEP:
4851 if (ext->key_len > SMALL_KEY_SIZE)
4852 key_len = LARGE_KEY_SIZE;
4853 else if (ext->key_len > 0)
4854 key_len = SMALL_KEY_SIZE;
4855 else
4856 goto out;
4858 priv->encode_alg = alg;
4859 priv->keys[idx].len = cpu_to_le16(key_len);
4861 key_len = min(ext->key_len, key_len);
4863 memset(priv->keys[idx].data, 0, ORINOCO_MAX_KEY_SIZE);
4864 memcpy(priv->keys[idx].data, ext->key, key_len);
4865 break;
4867 case IW_ENCODE_ALG_TKIP:
4869 hermes_t *hw = &priv->hw;
4870 u8 *tkip_iv = NULL;
4872 if (!priv->has_wpa ||
4873 (ext->key_len > sizeof(priv->tkip_key[0])))
4874 goto out;
4876 priv->encode_alg = alg;
4877 memset(&priv->tkip_key[idx], 0,
4878 sizeof(priv->tkip_key[idx]));
4879 memcpy(&priv->tkip_key[idx], ext->key, ext->key_len);
4881 if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID)
4882 tkip_iv = &ext->rx_seq[0];
4884 err = __orinoco_hw_set_tkip_key(hw, idx,
4885 ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY,
4886 (u8 *) &priv->tkip_key[idx],
4887 tkip_iv, NULL);
4888 if (err)
4889 printk(KERN_ERR "%s: Error %d setting TKIP key"
4890 "\n", dev->name, err);
4892 goto out;
4894 default:
4895 goto out;
4898 err = -EINPROGRESS;
4899 out:
4900 orinoco_unlock(priv, &flags);
4902 return err;
4905 static int orinoco_ioctl_get_encodeext(struct net_device *dev,
4906 struct iw_request_info *info,
4907 union iwreq_data *wrqu,
4908 char *extra)
4910 struct orinoco_private *priv = netdev_priv(dev);
4911 struct iw_point *encoding = &wrqu->encoding;
4912 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
4913 int idx, max_key_len;
4914 unsigned long flags;
4915 int err;
4917 if (orinoco_lock(priv, &flags) != 0)
4918 return -EBUSY;
4920 err = -EINVAL;
4921 max_key_len = encoding->length - sizeof(*ext);
4922 if (max_key_len < 0)
4923 goto out;
4925 idx = encoding->flags & IW_ENCODE_INDEX;
4926 if (idx) {
4927 if ((idx < 1) || (idx > 4))
4928 goto out;
4929 idx--;
4930 } else
4931 idx = priv->tx_key;
4933 encoding->flags = idx + 1;
4934 memset(ext, 0, sizeof(*ext));
4936 ext->alg = priv->encode_alg;
4937 switch (priv->encode_alg) {
4938 case IW_ENCODE_ALG_NONE:
4939 ext->key_len = 0;
4940 encoding->flags |= IW_ENCODE_DISABLED;
4941 break;
4942 case IW_ENCODE_ALG_WEP:
4943 ext->key_len = min_t(u16, le16_to_cpu(priv->keys[idx].len),
4944 max_key_len);
4945 memcpy(ext->key, priv->keys[idx].data, ext->key_len);
4946 encoding->flags |= IW_ENCODE_ENABLED;
4947 break;
4948 case IW_ENCODE_ALG_TKIP:
4949 ext->key_len = min_t(u16, sizeof(struct orinoco_tkip_key),
4950 max_key_len);
4951 memcpy(ext->key, &priv->tkip_key[idx], ext->key_len);
4952 encoding->flags |= IW_ENCODE_ENABLED;
4953 break;
4956 err = 0;
4957 out:
4958 orinoco_unlock(priv, &flags);
4960 return err;
4963 static int orinoco_ioctl_set_auth(struct net_device *dev,
4964 struct iw_request_info *info,
4965 union iwreq_data *wrqu, char *extra)
4967 struct orinoco_private *priv = netdev_priv(dev);
4968 hermes_t *hw = &priv->hw;
4969 struct iw_param *param = &wrqu->param;
4970 unsigned long flags;
4971 int ret = -EINPROGRESS;
4973 if (orinoco_lock(priv, &flags) != 0)
4974 return -EBUSY;
4976 switch (param->flags & IW_AUTH_INDEX) {
4977 case IW_AUTH_WPA_VERSION:
4978 case IW_AUTH_CIPHER_PAIRWISE:
4979 case IW_AUTH_CIPHER_GROUP:
4980 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
4981 case IW_AUTH_PRIVACY_INVOKED:
4982 case IW_AUTH_DROP_UNENCRYPTED:
4984 * orinoco does not use these parameters
4986 break;
4988 case IW_AUTH_KEY_MGMT:
4989 /* wl_lkm implies value 2 == PSK for Hermes I
4990 * which ties in with WEXT
4991 * no other hints tho :(
4993 priv->key_mgmt = param->value;
4994 break;
4996 case IW_AUTH_TKIP_COUNTERMEASURES:
4997 /* When countermeasures are enabled, shut down the
4998 * card; when disabled, re-enable the card. This must
4999 * take effect immediately.
5001 * TODO: Make sure that the EAPOL message is getting
5002 * out before card disabled
5004 if (param->value) {
5005 priv->tkip_cm_active = 1;
5006 ret = hermes_enable_port(hw, 0);
5007 } else {
5008 priv->tkip_cm_active = 0;
5009 ret = hermes_disable_port(hw, 0);
5011 break;
5013 case IW_AUTH_80211_AUTH_ALG:
5014 if (param->value & IW_AUTH_ALG_SHARED_KEY)
5015 priv->wep_restrict = 1;
5016 else if (param->value & IW_AUTH_ALG_OPEN_SYSTEM)
5017 priv->wep_restrict = 0;
5018 else
5019 ret = -EINVAL;
5020 break;
5022 case IW_AUTH_WPA_ENABLED:
5023 if (priv->has_wpa) {
5024 priv->wpa_enabled = param->value ? 1 : 0;
5025 } else {
5026 if (param->value)
5027 ret = -EOPNOTSUPP;
5028 /* else silently accept disable of WPA */
5029 priv->wpa_enabled = 0;
5031 break;
5033 default:
5034 ret = -EOPNOTSUPP;
5037 orinoco_unlock(priv, &flags);
5038 return ret;
5041 static int orinoco_ioctl_get_auth(struct net_device *dev,
5042 struct iw_request_info *info,
5043 union iwreq_data *wrqu, char *extra)
5045 struct orinoco_private *priv = netdev_priv(dev);
5046 struct iw_param *param = &wrqu->param;
5047 unsigned long flags;
5048 int ret = 0;
5050 if (orinoco_lock(priv, &flags) != 0)
5051 return -EBUSY;
5053 switch (param->flags & IW_AUTH_INDEX) {
5054 case IW_AUTH_KEY_MGMT:
5055 param->value = priv->key_mgmt;
5056 break;
5058 case IW_AUTH_TKIP_COUNTERMEASURES:
5059 param->value = priv->tkip_cm_active;
5060 break;
5062 case IW_AUTH_80211_AUTH_ALG:
5063 if (priv->wep_restrict)
5064 param->value = IW_AUTH_ALG_SHARED_KEY;
5065 else
5066 param->value = IW_AUTH_ALG_OPEN_SYSTEM;
5067 break;
5069 case IW_AUTH_WPA_ENABLED:
5070 param->value = priv->wpa_enabled;
5071 break;
5073 default:
5074 ret = -EOPNOTSUPP;
5077 orinoco_unlock(priv, &flags);
5078 return ret;
5081 static int orinoco_ioctl_set_genie(struct net_device *dev,
5082 struct iw_request_info *info,
5083 union iwreq_data *wrqu, char *extra)
5085 struct orinoco_private *priv = netdev_priv(dev);
5086 u8 *buf;
5087 unsigned long flags;
5089 /* cut off at IEEE80211_MAX_DATA_LEN */
5090 if ((wrqu->data.length > IEEE80211_MAX_DATA_LEN) ||
5091 (wrqu->data.length && (extra == NULL)))
5092 return -EINVAL;
5094 if (wrqu->data.length) {
5095 buf = kmalloc(wrqu->data.length, GFP_KERNEL);
5096 if (buf == NULL)
5097 return -ENOMEM;
5099 memcpy(buf, extra, wrqu->data.length);
5100 } else
5101 buf = NULL;
5103 if (orinoco_lock(priv, &flags) != 0) {
5104 kfree(buf);
5105 return -EBUSY;
5108 kfree(priv->wpa_ie);
5109 priv->wpa_ie = buf;
5110 priv->wpa_ie_len = wrqu->data.length;
5112 if (priv->wpa_ie) {
5113 /* Looks like wl_lkm wants to check the auth alg, and
5114 * somehow pass it to the firmware.
5115 * Instead it just calls the key mgmt rid
5116 * - we do this in set auth.
5120 orinoco_unlock(priv, &flags);
5121 return 0;
5124 static int orinoco_ioctl_get_genie(struct net_device *dev,
5125 struct iw_request_info *info,
5126 union iwreq_data *wrqu, char *extra)
5128 struct orinoco_private *priv = netdev_priv(dev);
5129 unsigned long flags;
5130 int err = 0;
5132 if (orinoco_lock(priv, &flags) != 0)
5133 return -EBUSY;
5135 if ((priv->wpa_ie_len == 0) || (priv->wpa_ie == NULL)) {
5136 wrqu->data.length = 0;
5137 goto out;
5140 if (wrqu->data.length < priv->wpa_ie_len) {
5141 err = -E2BIG;
5142 goto out;
5145 wrqu->data.length = priv->wpa_ie_len;
5146 memcpy(extra, priv->wpa_ie, priv->wpa_ie_len);
5148 out:
5149 orinoco_unlock(priv, &flags);
5150 return err;
5153 static int orinoco_ioctl_set_mlme(struct net_device *dev,
5154 struct iw_request_info *info,
5155 union iwreq_data *wrqu, char *extra)
5157 struct orinoco_private *priv = netdev_priv(dev);
5158 hermes_t *hw = &priv->hw;
5159 struct iw_mlme *mlme = (struct iw_mlme *)extra;
5160 unsigned long flags;
5161 int ret = 0;
5163 if (orinoco_lock(priv, &flags) != 0)
5164 return -EBUSY;
5166 switch (mlme->cmd) {
5167 case IW_MLME_DEAUTH:
5168 /* silently ignore */
5169 break;
5171 case IW_MLME_DISASSOC:
5173 struct {
5174 u8 addr[ETH_ALEN];
5175 __le16 reason_code;
5176 } __attribute__ ((packed)) buf;
5178 memcpy(buf.addr, mlme->addr.sa_data, ETH_ALEN);
5179 buf.reason_code = cpu_to_le16(mlme->reason_code);
5180 ret = HERMES_WRITE_RECORD(hw, USER_BAP,
5181 HERMES_RID_CNFDISASSOCIATE,
5182 &buf);
5183 break;
5185 default:
5186 ret = -EOPNOTSUPP;
5189 orinoco_unlock(priv, &flags);
5190 return ret;
5193 static int orinoco_ioctl_getretry(struct net_device *dev,
5194 struct iw_request_info *info,
5195 struct iw_param *rrq,
5196 char *extra)
5198 struct orinoco_private *priv = netdev_priv(dev);
5199 hermes_t *hw = &priv->hw;
5200 int err = 0;
5201 u16 short_limit, long_limit, lifetime;
5202 unsigned long flags;
5204 if (orinoco_lock(priv, &flags) != 0)
5205 return -EBUSY;
5207 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_SHORTRETRYLIMIT,
5208 &short_limit);
5209 if (err)
5210 goto out;
5212 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_LONGRETRYLIMIT,
5213 &long_limit);
5214 if (err)
5215 goto out;
5217 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_MAXTRANSMITLIFETIME,
5218 &lifetime);
5219 if (err)
5220 goto out;
5222 rrq->disabled = 0; /* Can't be disabled */
5224 /* Note : by default, display the retry number */
5225 if ((rrq->flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME) {
5226 rrq->flags = IW_RETRY_LIFETIME;
5227 rrq->value = lifetime * 1000; /* ??? */
5228 } else {
5229 /* By default, display the min number */
5230 if ((rrq->flags & IW_RETRY_LONG)) {
5231 rrq->flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
5232 rrq->value = long_limit;
5233 } else {
5234 rrq->flags = IW_RETRY_LIMIT;
5235 rrq->value = short_limit;
5236 if(short_limit != long_limit)
5237 rrq->flags |= IW_RETRY_SHORT;
5241 out:
5242 orinoco_unlock(priv, &flags);
5244 return err;
5247 static int orinoco_ioctl_reset(struct net_device *dev,
5248 struct iw_request_info *info,
5249 void *wrqu,
5250 char *extra)
5252 struct orinoco_private *priv = netdev_priv(dev);
5254 if (! capable(CAP_NET_ADMIN))
5255 return -EPERM;
5257 if (info->cmd == (SIOCIWFIRSTPRIV + 0x1)) {
5258 printk(KERN_DEBUG "%s: Forcing reset!\n", dev->name);
5260 /* Firmware reset */
5261 orinoco_reset(&priv->reset_work);
5262 } else {
5263 printk(KERN_DEBUG "%s: Force scheduling reset!\n", dev->name);
5265 schedule_work(&priv->reset_work);
5268 return 0;
5271 static int orinoco_ioctl_setibssport(struct net_device *dev,
5272 struct iw_request_info *info,
5273 void *wrqu,
5274 char *extra)
5277 struct orinoco_private *priv = netdev_priv(dev);
5278 int val = *( (int *) extra );
5279 unsigned long flags;
5281 if (orinoco_lock(priv, &flags) != 0)
5282 return -EBUSY;
5284 priv->ibss_port = val ;
5286 /* Actually update the mode we are using */
5287 set_port_type(priv);
5289 orinoco_unlock(priv, &flags);
5290 return -EINPROGRESS; /* Call commit handler */
5293 static int orinoco_ioctl_getibssport(struct net_device *dev,
5294 struct iw_request_info *info,
5295 void *wrqu,
5296 char *extra)
5298 struct orinoco_private *priv = netdev_priv(dev);
5299 int *val = (int *) extra;
5301 *val = priv->ibss_port;
5302 return 0;
5305 static int orinoco_ioctl_setport3(struct net_device *dev,
5306 struct iw_request_info *info,
5307 void *wrqu,
5308 char *extra)
5310 struct orinoco_private *priv = netdev_priv(dev);
5311 int val = *( (int *) extra );
5312 int err = 0;
5313 unsigned long flags;
5315 if (orinoco_lock(priv, &flags) != 0)
5316 return -EBUSY;
5318 switch (val) {
5319 case 0: /* Try to do IEEE ad-hoc mode */
5320 if (! priv->has_ibss) {
5321 err = -EINVAL;
5322 break;
5324 priv->prefer_port3 = 0;
5326 break;
5328 case 1: /* Try to do Lucent proprietary ad-hoc mode */
5329 if (! priv->has_port3) {
5330 err = -EINVAL;
5331 break;
5333 priv->prefer_port3 = 1;
5334 break;
5336 default:
5337 err = -EINVAL;
5340 if (! err) {
5341 /* Actually update the mode we are using */
5342 set_port_type(priv);
5343 err = -EINPROGRESS;
5346 orinoco_unlock(priv, &flags);
5348 return err;
5351 static int orinoco_ioctl_getport3(struct net_device *dev,
5352 struct iw_request_info *info,
5353 void *wrqu,
5354 char *extra)
5356 struct orinoco_private *priv = netdev_priv(dev);
5357 int *val = (int *) extra;
5359 *val = priv->prefer_port3;
5360 return 0;
5363 static int orinoco_ioctl_setpreamble(struct net_device *dev,
5364 struct iw_request_info *info,
5365 void *wrqu,
5366 char *extra)
5368 struct orinoco_private *priv = netdev_priv(dev);
5369 unsigned long flags;
5370 int val;
5372 if (! priv->has_preamble)
5373 return -EOPNOTSUPP;
5375 /* 802.11b has recently defined some short preamble.
5376 * Basically, the Phy header has been reduced in size.
5377 * This increase performance, especially at high rates
5378 * (the preamble is transmitted at 1Mb/s), unfortunately
5379 * this give compatibility troubles... - Jean II */
5380 val = *( (int *) extra );
5382 if (orinoco_lock(priv, &flags) != 0)
5383 return -EBUSY;
5385 if (val)
5386 priv->preamble = 1;
5387 else
5388 priv->preamble = 0;
5390 orinoco_unlock(priv, &flags);
5392 return -EINPROGRESS; /* Call commit handler */
5395 static int orinoco_ioctl_getpreamble(struct net_device *dev,
5396 struct iw_request_info *info,
5397 void *wrqu,
5398 char *extra)
5400 struct orinoco_private *priv = netdev_priv(dev);
5401 int *val = (int *) extra;
5403 if (! priv->has_preamble)
5404 return -EOPNOTSUPP;
5406 *val = priv->preamble;
5407 return 0;
5410 /* ioctl interface to hermes_read_ltv()
5411 * To use with iwpriv, pass the RID as the token argument, e.g.
5412 * iwpriv get_rid [0xfc00]
5413 * At least Wireless Tools 25 is required to use iwpriv.
5414 * For Wireless Tools 25 and 26 append "dummy" are the end. */
5415 static int orinoco_ioctl_getrid(struct net_device *dev,
5416 struct iw_request_info *info,
5417 struct iw_point *data,
5418 char *extra)
5420 struct orinoco_private *priv = netdev_priv(dev);
5421 hermes_t *hw = &priv->hw;
5422 int rid = data->flags;
5423 u16 length;
5424 int err;
5425 unsigned long flags;
5427 /* It's a "get" function, but we don't want users to access the
5428 * WEP key and other raw firmware data */
5429 if (! capable(CAP_NET_ADMIN))
5430 return -EPERM;
5432 if (rid < 0xfc00 || rid > 0xffff)
5433 return -EINVAL;
5435 if (orinoco_lock(priv, &flags) != 0)
5436 return -EBUSY;
5438 err = hw->ops->read_ltv(hw, USER_BAP, rid, MAX_RID_LEN, &length,
5439 extra);
5440 if (err)
5441 goto out;
5443 data->length = min_t(u16, HERMES_RECLEN_TO_BYTES(length),
5444 MAX_RID_LEN);
5446 out:
5447 orinoco_unlock(priv, &flags);
5448 return err;
5451 /* Trigger a scan (look for other cells in the vicinity) */
5452 static int orinoco_ioctl_setscan(struct net_device *dev,
5453 struct iw_request_info *info,
5454 struct iw_point *srq,
5455 char *extra)
5457 struct orinoco_private *priv = netdev_priv(dev);
5458 hermes_t *hw = &priv->hw;
5459 struct iw_scan_req *si = (struct iw_scan_req *) extra;
5460 int err = 0;
5461 unsigned long flags;
5463 /* Note : you may have realised that, as this is a SET operation,
5464 * this is privileged and therefore a normal user can't
5465 * perform scanning.
5466 * This is not an error, while the device perform scanning,
5467 * traffic doesn't flow, so it's a perfect DoS...
5468 * Jean II */
5470 if (orinoco_lock(priv, &flags) != 0)
5471 return -EBUSY;
5473 /* Scanning with port 0 disabled would fail */
5474 if (!netif_running(dev)) {
5475 err = -ENETDOWN;
5476 goto out;
5479 /* In monitor mode, the scan results are always empty.
5480 * Probe responses are passed to the driver as received
5481 * frames and could be processed in software. */
5482 if (priv->iw_mode == IW_MODE_MONITOR) {
5483 err = -EOPNOTSUPP;
5484 goto out;
5487 /* Note : because we don't lock out the irq handler, the way
5488 * we access scan variables in priv is critical.
5489 * o scan_inprogress : not touched by irq handler
5490 * o scan_mode : not touched by irq handler
5491 * Before modifying anything on those variables, please think hard !
5492 * Jean II */
5494 /* Save flags */
5495 priv->scan_mode = srq->flags;
5497 /* Always trigger scanning, even if it's in progress.
5498 * This way, if the info frame get lost, we will recover somewhat
5499 * gracefully - Jean II */
5501 if (priv->has_hostscan) {
5502 switch (priv->firmware_type) {
5503 case FIRMWARE_TYPE_SYMBOL:
5504 err = hermes_write_wordrec(hw, USER_BAP,
5505 HERMES_RID_CNFHOSTSCAN_SYMBOL,
5506 HERMES_HOSTSCAN_SYMBOL_ONCE |
5507 HERMES_HOSTSCAN_SYMBOL_BCAST);
5508 break;
5509 case FIRMWARE_TYPE_INTERSIL: {
5510 __le16 req[3];
5512 req[0] = cpu_to_le16(0x3fff); /* All channels */
5513 req[1] = cpu_to_le16(0x0001); /* rate 1 Mbps */
5514 req[2] = 0; /* Any ESSID */
5515 err = HERMES_WRITE_RECORD(hw, USER_BAP,
5516 HERMES_RID_CNFHOSTSCAN, &req);
5518 break;
5519 case FIRMWARE_TYPE_AGERE:
5520 if (priv->scan_mode & IW_SCAN_THIS_ESSID) {
5521 struct hermes_idstring idbuf;
5522 size_t len = min(sizeof(idbuf.val),
5523 (size_t) si->essid_len);
5524 idbuf.len = cpu_to_le16(len);
5525 memcpy(idbuf.val, si->essid, len);
5527 err = hw->ops->write_ltv(hw, USER_BAP,
5528 HERMES_RID_CNFSCANSSID_AGERE,
5529 HERMES_BYTES_TO_RECLEN(len + 2),
5530 &idbuf);
5531 } else
5532 err = hermes_write_wordrec(hw, USER_BAP,
5533 HERMES_RID_CNFSCANSSID_AGERE,
5534 0); /* Any ESSID */
5535 if (err)
5536 break;
5538 if (priv->has_ext_scan) {
5539 /* Clear scan results at the start of
5540 * an extended scan */
5541 orinoco_clear_scan_results(priv,
5542 msecs_to_jiffies(15000));
5544 /* TODO: Is this available on older firmware?
5545 * Can we use it to scan specific channels
5546 * for IW_SCAN_THIS_FREQ? */
5547 err = hermes_write_wordrec(hw, USER_BAP,
5548 HERMES_RID_CNFSCANCHANNELS2GHZ,
5549 0x7FFF);
5550 if (err)
5551 goto out;
5553 err = hermes_inquire(hw,
5554 HERMES_INQ_CHANNELINFO);
5555 } else
5556 err = hermes_inquire(hw, HERMES_INQ_SCAN);
5557 break;
5559 } else
5560 err = hermes_inquire(hw, HERMES_INQ_SCAN);
5562 /* One more client */
5563 if (! err)
5564 priv->scan_inprogress = 1;
5566 out:
5567 orinoco_unlock(priv, &flags);
5568 return err;
5571 #define MAX_CUSTOM_LEN 64
5573 /* Translate scan data returned from the card to a card independant
5574 * format that the Wireless Tools will understand - Jean II */
5575 static inline char *orinoco_translate_scan(struct net_device *dev,
5576 struct iw_request_info *info,
5577 char *current_ev,
5578 char *end_buf,
5579 union hermes_scan_info *bss,
5580 unsigned long last_scanned)
5582 struct orinoco_private *priv = netdev_priv(dev);
5583 u16 capabilities;
5584 u16 channel;
5585 struct iw_event iwe; /* Temporary buffer */
5586 char custom[MAX_CUSTOM_LEN];
5588 memset(&iwe, 0, sizeof(iwe));
5590 /* First entry *MUST* be the AP MAC address */
5591 iwe.cmd = SIOCGIWAP;
5592 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
5593 memcpy(iwe.u.ap_addr.sa_data, bss->a.bssid, ETH_ALEN);
5594 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5595 &iwe, IW_EV_ADDR_LEN);
5597 /* Other entries will be displayed in the order we give them */
5599 /* Add the ESSID */
5600 iwe.u.data.length = le16_to_cpu(bss->a.essid_len);
5601 if (iwe.u.data.length > 32)
5602 iwe.u.data.length = 32;
5603 iwe.cmd = SIOCGIWESSID;
5604 iwe.u.data.flags = 1;
5605 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5606 &iwe, bss->a.essid);
5608 /* Add mode */
5609 iwe.cmd = SIOCGIWMODE;
5610 capabilities = le16_to_cpu(bss->a.capabilities);
5611 if (capabilities & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
5612 if (capabilities & WLAN_CAPABILITY_ESS)
5613 iwe.u.mode = IW_MODE_MASTER;
5614 else
5615 iwe.u.mode = IW_MODE_ADHOC;
5616 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5617 &iwe, IW_EV_UINT_LEN);
5620 channel = bss->s.channel;
5621 if ((channel >= 1) && (channel <= NUM_CHANNELS)) {
5622 /* Add channel and frequency */
5623 iwe.cmd = SIOCGIWFREQ;
5624 iwe.u.freq.m = channel;
5625 iwe.u.freq.e = 0;
5626 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5627 &iwe, IW_EV_FREQ_LEN);
5629 iwe.u.freq.m = channel_frequency[channel-1] * 100000;
5630 iwe.u.freq.e = 1;
5631 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5632 &iwe, IW_EV_FREQ_LEN);
5635 /* Add quality statistics. level and noise in dB. No link quality */
5636 iwe.cmd = IWEVQUAL;
5637 iwe.u.qual.updated = IW_QUAL_DBM | IW_QUAL_QUAL_INVALID;
5638 iwe.u.qual.level = (__u8) le16_to_cpu(bss->a.level) - 0x95;
5639 iwe.u.qual.noise = (__u8) le16_to_cpu(bss->a.noise) - 0x95;
5640 /* Wireless tools prior to 27.pre22 will show link quality
5641 * anyway, so we provide a reasonable value. */
5642 if (iwe.u.qual.level > iwe.u.qual.noise)
5643 iwe.u.qual.qual = iwe.u.qual.level - iwe.u.qual.noise;
5644 else
5645 iwe.u.qual.qual = 0;
5646 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5647 &iwe, IW_EV_QUAL_LEN);
5649 /* Add encryption capability */
5650 iwe.cmd = SIOCGIWENCODE;
5651 if (capabilities & WLAN_CAPABILITY_PRIVACY)
5652 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
5653 else
5654 iwe.u.data.flags = IW_ENCODE_DISABLED;
5655 iwe.u.data.length = 0;
5656 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5657 &iwe, NULL);
5659 /* Bit rate is not available in Lucent/Agere firmwares */
5660 if (priv->firmware_type != FIRMWARE_TYPE_AGERE) {
5661 char *current_val = current_ev + iwe_stream_lcp_len(info);
5662 int i;
5663 int step;
5665 if (priv->firmware_type == FIRMWARE_TYPE_SYMBOL)
5666 step = 2;
5667 else
5668 step = 1;
5670 iwe.cmd = SIOCGIWRATE;
5671 /* Those two flags are ignored... */
5672 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
5673 /* Max 10 values */
5674 for (i = 0; i < 10; i += step) {
5675 /* NULL terminated */
5676 if (bss->p.rates[i] == 0x0)
5677 break;
5678 /* Bit rate given in 500 kb/s units (+ 0x80) */
5679 iwe.u.bitrate.value =
5680 ((bss->p.rates[i] & 0x7f) * 500000);
5681 current_val = iwe_stream_add_value(info, current_ev,
5682 current_val,
5683 end_buf, &iwe,
5684 IW_EV_PARAM_LEN);
5686 /* Check if we added any event */
5687 if ((current_val - current_ev) > iwe_stream_lcp_len(info))
5688 current_ev = current_val;
5691 /* Beacon interval */
5692 iwe.cmd = IWEVCUSTOM;
5693 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5694 "bcn_int=%d",
5695 le16_to_cpu(bss->a.beacon_interv));
5696 if (iwe.u.data.length)
5697 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5698 &iwe, custom);
5700 /* Capabilites */
5701 iwe.cmd = IWEVCUSTOM;
5702 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5703 "capab=0x%04x",
5704 capabilities);
5705 if (iwe.u.data.length)
5706 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5707 &iwe, custom);
5709 /* Add EXTRA: Age to display seconds since last beacon/probe response
5710 * for given network. */
5711 iwe.cmd = IWEVCUSTOM;
5712 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5713 " Last beacon: %dms ago",
5714 jiffies_to_msecs(jiffies - last_scanned));
5715 if (iwe.u.data.length)
5716 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5717 &iwe, custom);
5719 return current_ev;
5722 static inline char *orinoco_translate_ext_scan(struct net_device *dev,
5723 struct iw_request_info *info,
5724 char *current_ev,
5725 char *end_buf,
5726 struct agere_ext_scan_info *bss,
5727 unsigned long last_scanned)
5729 u16 capabilities;
5730 u16 channel;
5731 struct iw_event iwe; /* Temporary buffer */
5732 char custom[MAX_CUSTOM_LEN];
5733 u8 *ie;
5735 memset(&iwe, 0, sizeof(iwe));
5737 /* First entry *MUST* be the AP MAC address */
5738 iwe.cmd = SIOCGIWAP;
5739 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
5740 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
5741 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5742 &iwe, IW_EV_ADDR_LEN);
5744 /* Other entries will be displayed in the order we give them */
5746 /* Add the ESSID */
5747 ie = bss->data;
5748 iwe.u.data.length = ie[1];
5749 if (iwe.u.data.length) {
5750 if (iwe.u.data.length > 32)
5751 iwe.u.data.length = 32;
5752 iwe.cmd = SIOCGIWESSID;
5753 iwe.u.data.flags = 1;
5754 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5755 &iwe, &ie[2]);
5758 /* Add mode */
5759 capabilities = le16_to_cpu(bss->capabilities);
5760 if (capabilities & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
5761 iwe.cmd = SIOCGIWMODE;
5762 if (capabilities & WLAN_CAPABILITY_ESS)
5763 iwe.u.mode = IW_MODE_MASTER;
5764 else
5765 iwe.u.mode = IW_MODE_ADHOC;
5766 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5767 &iwe, IW_EV_UINT_LEN);
5770 ie = orinoco_get_ie(bss->data, sizeof(bss->data), WLAN_EID_DS_PARAMS);
5771 channel = ie ? ie[2] : 0;
5772 if ((channel >= 1) && (channel <= NUM_CHANNELS)) {
5773 /* Add channel and frequency */
5774 iwe.cmd = SIOCGIWFREQ;
5775 iwe.u.freq.m = channel;
5776 iwe.u.freq.e = 0;
5777 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5778 &iwe, IW_EV_FREQ_LEN);
5780 iwe.u.freq.m = channel_frequency[channel-1] * 100000;
5781 iwe.u.freq.e = 1;
5782 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5783 &iwe, IW_EV_FREQ_LEN);
5786 /* Add quality statistics. level and noise in dB. No link quality */
5787 iwe.cmd = IWEVQUAL;
5788 iwe.u.qual.updated = IW_QUAL_DBM | IW_QUAL_QUAL_INVALID;
5789 iwe.u.qual.level = bss->level - 0x95;
5790 iwe.u.qual.noise = bss->noise - 0x95;
5791 /* Wireless tools prior to 27.pre22 will show link quality
5792 * anyway, so we provide a reasonable value. */
5793 if (iwe.u.qual.level > iwe.u.qual.noise)
5794 iwe.u.qual.qual = iwe.u.qual.level - iwe.u.qual.noise;
5795 else
5796 iwe.u.qual.qual = 0;
5797 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5798 &iwe, IW_EV_QUAL_LEN);
5800 /* Add encryption capability */
5801 iwe.cmd = SIOCGIWENCODE;
5802 if (capabilities & WLAN_CAPABILITY_PRIVACY)
5803 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
5804 else
5805 iwe.u.data.flags = IW_ENCODE_DISABLED;
5806 iwe.u.data.length = 0;
5807 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5808 &iwe, NULL);
5810 /* WPA IE */
5811 ie = orinoco_get_wpa_ie(bss->data, sizeof(bss->data));
5812 if (ie) {
5813 iwe.cmd = IWEVGENIE;
5814 iwe.u.data.length = ie[1] + 2;
5815 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5816 &iwe, ie);
5819 /* RSN IE */
5820 ie = orinoco_get_ie(bss->data, sizeof(bss->data), WLAN_EID_RSN);
5821 if (ie) {
5822 iwe.cmd = IWEVGENIE;
5823 iwe.u.data.length = ie[1] + 2;
5824 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5825 &iwe, ie);
5828 ie = orinoco_get_ie(bss->data, sizeof(bss->data), WLAN_EID_SUPP_RATES);
5829 if (ie) {
5830 char *p = current_ev + iwe_stream_lcp_len(info);
5831 int i;
5833 iwe.cmd = SIOCGIWRATE;
5834 /* Those two flags are ignored... */
5835 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
5837 for (i = 2; i < (ie[1] + 2); i++) {
5838 iwe.u.bitrate.value = ((ie[i] & 0x7F) * 500000);
5839 p = iwe_stream_add_value(info, current_ev, p, end_buf,
5840 &iwe, IW_EV_PARAM_LEN);
5842 /* Check if we added any event */
5843 if (p > (current_ev + iwe_stream_lcp_len(info)))
5844 current_ev = p;
5847 /* Timestamp */
5848 iwe.cmd = IWEVCUSTOM;
5849 iwe.u.data.length =
5850 snprintf(custom, MAX_CUSTOM_LEN, "tsf=%016llx",
5851 (unsigned long long) le64_to_cpu(bss->timestamp));
5852 if (iwe.u.data.length)
5853 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5854 &iwe, custom);
5856 /* Beacon interval */
5857 iwe.cmd = IWEVCUSTOM;
5858 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5859 "bcn_int=%d",
5860 le16_to_cpu(bss->beacon_interval));
5861 if (iwe.u.data.length)
5862 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5863 &iwe, custom);
5865 /* Capabilites */
5866 iwe.cmd = IWEVCUSTOM;
5867 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5868 "capab=0x%04x",
5869 capabilities);
5870 if (iwe.u.data.length)
5871 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5872 &iwe, custom);
5874 /* Add EXTRA: Age to display seconds since last beacon/probe response
5875 * for given network. */
5876 iwe.cmd = IWEVCUSTOM;
5877 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5878 " Last beacon: %dms ago",
5879 jiffies_to_msecs(jiffies - last_scanned));
5880 if (iwe.u.data.length)
5881 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5882 &iwe, custom);
5884 return current_ev;
5887 /* Return results of a scan */
5888 static int orinoco_ioctl_getscan(struct net_device *dev,
5889 struct iw_request_info *info,
5890 struct iw_point *srq,
5891 char *extra)
5893 struct orinoco_private *priv = netdev_priv(dev);
5894 int err = 0;
5895 unsigned long flags;
5896 char *current_ev = extra;
5898 if (orinoco_lock(priv, &flags) != 0)
5899 return -EBUSY;
5901 if (priv->scan_inprogress) {
5902 /* Important note : we don't want to block the caller
5903 * until results are ready for various reasons.
5904 * First, managing wait queues is complex and racy.
5905 * Second, we grab some rtnetlink lock before comming
5906 * here (in dev_ioctl()).
5907 * Third, we generate an Wireless Event, so the
5908 * caller can wait itself on that - Jean II */
5909 err = -EAGAIN;
5910 goto out;
5913 if (priv->has_ext_scan) {
5914 struct xbss_element *bss;
5916 list_for_each_entry(bss, &priv->bss_list, list) {
5917 /* Translate this entry to WE format */
5918 current_ev =
5919 orinoco_translate_ext_scan(dev, info,
5920 current_ev,
5921 extra + srq->length,
5922 &bss->bss,
5923 bss->last_scanned);
5925 /* Check if there is space for one more entry */
5926 if ((extra + srq->length - current_ev)
5927 <= IW_EV_ADDR_LEN) {
5928 /* Ask user space to try again with a
5929 * bigger buffer */
5930 err = -E2BIG;
5931 goto out;
5935 } else {
5936 struct bss_element *bss;
5938 list_for_each_entry(bss, &priv->bss_list, list) {
5939 /* Translate this entry to WE format */
5940 current_ev = orinoco_translate_scan(dev, info,
5941 current_ev,
5942 extra + srq->length,
5943 &bss->bss,
5944 bss->last_scanned);
5946 /* Check if there is space for one more entry */
5947 if ((extra + srq->length - current_ev)
5948 <= IW_EV_ADDR_LEN) {
5949 /* Ask user space to try again with a
5950 * bigger buffer */
5951 err = -E2BIG;
5952 goto out;
5957 srq->length = (current_ev - extra);
5958 srq->flags = (__u16) priv->scan_mode;
5960 out:
5961 orinoco_unlock(priv, &flags);
5962 return err;
5965 /* Commit handler, called after set operations */
5966 static int orinoco_ioctl_commit(struct net_device *dev,
5967 struct iw_request_info *info,
5968 void *wrqu,
5969 char *extra)
5971 struct orinoco_private *priv = netdev_priv(dev);
5972 struct hermes *hw = &priv->hw;
5973 unsigned long flags;
5974 int err = 0;
5976 if (!priv->open)
5977 return 0;
5979 if (priv->broken_disableport) {
5980 orinoco_reset(&priv->reset_work);
5981 return 0;
5984 if (orinoco_lock(priv, &flags) != 0)
5985 return err;
5987 err = hermes_disable_port(hw, 0);
5988 if (err) {
5989 printk(KERN_WARNING "%s: Unable to disable port "
5990 "while reconfiguring card\n", dev->name);
5991 priv->broken_disableport = 1;
5992 goto out;
5995 err = __orinoco_program_rids(dev);
5996 if (err) {
5997 printk(KERN_WARNING "%s: Unable to reconfigure card\n",
5998 dev->name);
5999 goto out;
6002 err = hermes_enable_port(hw, 0);
6003 if (err) {
6004 printk(KERN_WARNING "%s: Unable to enable port while reconfiguring card\n",
6005 dev->name);
6006 goto out;
6009 out:
6010 if (err) {
6011 printk(KERN_WARNING "%s: Resetting instead...\n", dev->name);
6012 schedule_work(&priv->reset_work);
6013 err = 0;
6016 orinoco_unlock(priv, &flags);
6017 return err;
6020 static const struct iw_priv_args orinoco_privtab[] = {
6021 { SIOCIWFIRSTPRIV + 0x0, 0, 0, "force_reset" },
6022 { SIOCIWFIRSTPRIV + 0x1, 0, 0, "card_reset" },
6023 { SIOCIWFIRSTPRIV + 0x2, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
6024 0, "set_port3" },
6025 { SIOCIWFIRSTPRIV + 0x3, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
6026 "get_port3" },
6027 { SIOCIWFIRSTPRIV + 0x4, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
6028 0, "set_preamble" },
6029 { SIOCIWFIRSTPRIV + 0x5, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
6030 "get_preamble" },
6031 { SIOCIWFIRSTPRIV + 0x6, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
6032 0, "set_ibssport" },
6033 { SIOCIWFIRSTPRIV + 0x7, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
6034 "get_ibssport" },
6035 { SIOCIWFIRSTPRIV + 0x9, 0, IW_PRIV_TYPE_BYTE | MAX_RID_LEN,
6036 "get_rid" },
6041 * Structures to export the Wireless Handlers
6044 #define STD_IW_HANDLER(id, func) \
6045 [IW_IOCTL_IDX(id)] = (iw_handler) func
6046 static const iw_handler orinoco_handler[] = {
6047 STD_IW_HANDLER(SIOCSIWCOMMIT, orinoco_ioctl_commit),
6048 STD_IW_HANDLER(SIOCGIWNAME, orinoco_ioctl_getname),
6049 STD_IW_HANDLER(SIOCSIWFREQ, orinoco_ioctl_setfreq),
6050 STD_IW_HANDLER(SIOCGIWFREQ, orinoco_ioctl_getfreq),
6051 STD_IW_HANDLER(SIOCSIWMODE, orinoco_ioctl_setmode),
6052 STD_IW_HANDLER(SIOCGIWMODE, orinoco_ioctl_getmode),
6053 STD_IW_HANDLER(SIOCSIWSENS, orinoco_ioctl_setsens),
6054 STD_IW_HANDLER(SIOCGIWSENS, orinoco_ioctl_getsens),
6055 STD_IW_HANDLER(SIOCGIWRANGE, orinoco_ioctl_getiwrange),
6056 STD_IW_HANDLER(SIOCSIWSPY, iw_handler_set_spy),
6057 STD_IW_HANDLER(SIOCGIWSPY, iw_handler_get_spy),
6058 STD_IW_HANDLER(SIOCSIWTHRSPY, iw_handler_set_thrspy),
6059 STD_IW_HANDLER(SIOCGIWTHRSPY, iw_handler_get_thrspy),
6060 STD_IW_HANDLER(SIOCSIWAP, orinoco_ioctl_setwap),
6061 STD_IW_HANDLER(SIOCGIWAP, orinoco_ioctl_getwap),
6062 STD_IW_HANDLER(SIOCSIWSCAN, orinoco_ioctl_setscan),
6063 STD_IW_HANDLER(SIOCGIWSCAN, orinoco_ioctl_getscan),
6064 STD_IW_HANDLER(SIOCSIWESSID, orinoco_ioctl_setessid),
6065 STD_IW_HANDLER(SIOCGIWESSID, orinoco_ioctl_getessid),
6066 STD_IW_HANDLER(SIOCSIWNICKN, orinoco_ioctl_setnick),
6067 STD_IW_HANDLER(SIOCGIWNICKN, orinoco_ioctl_getnick),
6068 STD_IW_HANDLER(SIOCSIWRATE, orinoco_ioctl_setrate),
6069 STD_IW_HANDLER(SIOCGIWRATE, orinoco_ioctl_getrate),
6070 STD_IW_HANDLER(SIOCSIWRTS, orinoco_ioctl_setrts),
6071 STD_IW_HANDLER(SIOCGIWRTS, orinoco_ioctl_getrts),
6072 STD_IW_HANDLER(SIOCSIWFRAG, orinoco_ioctl_setfrag),
6073 STD_IW_HANDLER(SIOCGIWFRAG, orinoco_ioctl_getfrag),
6074 STD_IW_HANDLER(SIOCGIWRETRY, orinoco_ioctl_getretry),
6075 STD_IW_HANDLER(SIOCSIWENCODE, orinoco_ioctl_setiwencode),
6076 STD_IW_HANDLER(SIOCGIWENCODE, orinoco_ioctl_getiwencode),
6077 STD_IW_HANDLER(SIOCSIWPOWER, orinoco_ioctl_setpower),
6078 STD_IW_HANDLER(SIOCGIWPOWER, orinoco_ioctl_getpower),
6079 STD_IW_HANDLER(SIOCSIWGENIE, orinoco_ioctl_set_genie),
6080 STD_IW_HANDLER(SIOCGIWGENIE, orinoco_ioctl_get_genie),
6081 STD_IW_HANDLER(SIOCSIWMLME, orinoco_ioctl_set_mlme),
6082 STD_IW_HANDLER(SIOCSIWAUTH, orinoco_ioctl_set_auth),
6083 STD_IW_HANDLER(SIOCGIWAUTH, orinoco_ioctl_get_auth),
6084 STD_IW_HANDLER(SIOCSIWENCODEEXT, orinoco_ioctl_set_encodeext),
6085 STD_IW_HANDLER(SIOCGIWENCODEEXT, orinoco_ioctl_get_encodeext),
6090 Added typecasting since we no longer use iwreq_data -- Moustafa
6092 static const iw_handler orinoco_private_handler[] = {
6093 [0] = (iw_handler) orinoco_ioctl_reset,
6094 [1] = (iw_handler) orinoco_ioctl_reset,
6095 [2] = (iw_handler) orinoco_ioctl_setport3,
6096 [3] = (iw_handler) orinoco_ioctl_getport3,
6097 [4] = (iw_handler) orinoco_ioctl_setpreamble,
6098 [5] = (iw_handler) orinoco_ioctl_getpreamble,
6099 [6] = (iw_handler) orinoco_ioctl_setibssport,
6100 [7] = (iw_handler) orinoco_ioctl_getibssport,
6101 [9] = (iw_handler) orinoco_ioctl_getrid,
6104 static const struct iw_handler_def orinoco_handler_def = {
6105 .num_standard = ARRAY_SIZE(orinoco_handler),
6106 .num_private = ARRAY_SIZE(orinoco_private_handler),
6107 .num_private_args = ARRAY_SIZE(orinoco_privtab),
6108 .standard = orinoco_handler,
6109 .private = orinoco_private_handler,
6110 .private_args = orinoco_privtab,
6111 .get_wireless_stats = orinoco_get_wireless_stats,
6114 static void orinoco_get_drvinfo(struct net_device *dev,
6115 struct ethtool_drvinfo *info)
6117 struct orinoco_private *priv = netdev_priv(dev);
6119 strncpy(info->driver, DRIVER_NAME, sizeof(info->driver) - 1);
6120 strncpy(info->version, DRIVER_VERSION, sizeof(info->version) - 1);
6121 strncpy(info->fw_version, priv->fw_name, sizeof(info->fw_version) - 1);
6122 if (dev->dev.parent)
6123 strncpy(info->bus_info, dev_name(dev->dev.parent),
6124 sizeof(info->bus_info) - 1);
6125 else
6126 snprintf(info->bus_info, sizeof(info->bus_info) - 1,
6127 "PCMCIA %p", priv->hw.iobase);
6130 static const struct ethtool_ops orinoco_ethtool_ops = {
6131 .get_drvinfo = orinoco_get_drvinfo,
6132 .get_link = ethtool_op_get_link,
6135 /********************************************************************/
6136 /* Module initialization */
6137 /********************************************************************/
6139 EXPORT_SYMBOL(alloc_orinocodev);
6140 EXPORT_SYMBOL(free_orinocodev);
6142 EXPORT_SYMBOL(__orinoco_up);
6143 EXPORT_SYMBOL(__orinoco_down);
6144 EXPORT_SYMBOL(orinoco_reinit_firmware);
6146 EXPORT_SYMBOL(orinoco_interrupt);
6148 /* Can't be declared "const" or the whole __initdata section will
6149 * become const */
6150 static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
6151 " (David Gibson <hermes@gibson.dropbear.id.au>, "
6152 "Pavel Roskin <proski@gnu.org>, et al)";
6154 static int __init init_orinoco(void)
6156 printk(KERN_DEBUG "%s\n", version);
6157 return 0;
6160 static void __exit exit_orinoco(void)
6164 module_init(init_orinoco);
6165 module_exit(exit_orinoco);