orinoco: add hermes_ops
[orinoco_usb.git] / drivers / net / wireless / orinoco.c
blob3597c886c01c298e2c568d7c2d510e168deaec7a
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/if_arp.h>
88 #include <linux/wireless.h>
89 #include <net/iw_handler.h>
90 #include <net/ieee80211.h>
92 #include <linux/scatterlist.h>
93 #include <linux/crypto.h>
95 #include "hermes_rid.h"
96 #include "hermes_dld.h"
97 #include "orinoco.h"
99 /********************************************************************/
100 /* Module information */
101 /********************************************************************/
103 MODULE_AUTHOR("Pavel Roskin <proski@gnu.org> & David Gibson <hermes@gibson.dropbear.id.au>");
104 MODULE_DESCRIPTION("Driver for Lucent Orinoco, Prism II based and similar wireless cards");
105 MODULE_LICENSE("Dual MPL/GPL");
107 /* Level of debugging. Used in the macros in orinoco.h */
108 #ifdef ORINOCO_DEBUG
109 int orinoco_debug = ORINOCO_DEBUG;
110 module_param(orinoco_debug, int, 0644);
111 MODULE_PARM_DESC(orinoco_debug, "Debug level");
112 EXPORT_SYMBOL(orinoco_debug);
113 #endif
115 static int suppress_linkstatus; /* = 0 */
116 module_param(suppress_linkstatus, bool, 0644);
117 MODULE_PARM_DESC(suppress_linkstatus, "Don't log link status changes");
118 static int ignore_disconnect; /* = 0 */
119 module_param(ignore_disconnect, int, 0644);
120 MODULE_PARM_DESC(ignore_disconnect, "Don't report lost link to the network layer");
122 static int force_monitor; /* = 0 */
123 module_param(force_monitor, int, 0644);
124 MODULE_PARM_DESC(force_monitor, "Allow monitor mode for all firmware versions");
126 /********************************************************************/
127 /* Compile time configuration and compatibility stuff */
128 /********************************************************************/
130 /* We do this this way to avoid ifdefs in the actual code */
131 #ifdef WIRELESS_SPY
132 #define SPY_NUMBER(priv) (priv->spy_data.spy_number)
133 #else
134 #define SPY_NUMBER(priv) 0
135 #endif /* WIRELESS_SPY */
137 /********************************************************************/
138 /* Internal constants */
139 /********************************************************************/
141 /* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
142 static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
143 #define ENCAPS_OVERHEAD (sizeof(encaps_hdr) + 2)
145 #define ORINOCO_MIN_MTU 256
146 #define ORINOCO_MAX_MTU (IEEE80211_DATA_LEN - ENCAPS_OVERHEAD)
148 #define SYMBOL_MAX_VER_LEN (14)
149 #define USER_BAP 0
150 #define IRQ_BAP 1
151 #define MAX_IRQLOOPS_PER_IRQ 10
152 #define MAX_IRQLOOPS_PER_JIFFY (20000/HZ) /* Based on a guestimate of
153 * how many events the
154 * device could
155 * legitimately generate */
156 #define SMALL_KEY_SIZE 5
157 #define LARGE_KEY_SIZE 13
158 #define TX_NICBUF_SIZE_BUG 1585 /* Bug in Symbol firmware */
160 #define DUMMY_FID 0xFFFF
162 /*#define MAX_MULTICAST(priv) (priv->firmware_type == FIRMWARE_TYPE_AGERE ? \
163 HERMES_MAX_MULTICAST : 0)*/
164 #define MAX_MULTICAST(priv) (HERMES_MAX_MULTICAST)
166 #define ORINOCO_INTEN (HERMES_EV_RX | HERMES_EV_ALLOC \
167 | HERMES_EV_TX | HERMES_EV_TXEXC \
168 | HERMES_EV_WTERR | HERMES_EV_INFO \
169 | HERMES_EV_INFDROP )
171 #define MAX_RID_LEN 1024
173 static const struct iw_handler_def orinoco_handler_def;
174 static const struct ethtool_ops orinoco_ethtool_ops;
176 /********************************************************************/
177 /* Data tables */
178 /********************************************************************/
180 /* The frequency of each channel in MHz */
181 static const long channel_frequency[] = {
182 2412, 2417, 2422, 2427, 2432, 2437, 2442,
183 2447, 2452, 2457, 2462, 2467, 2472, 2484
185 #define NUM_CHANNELS ARRAY_SIZE(channel_frequency)
187 /* This tables gives the actual meanings of the bitrate IDs returned
188 * by the firmware. */
189 static struct {
190 int bitrate; /* in 100s of kilobits */
191 int automatic;
192 u16 agere_txratectrl;
193 u16 intersil_txratectrl;
194 } bitrate_table[] = {
195 {110, 1, 3, 15}, /* Entry 0 is the default */
196 {10, 0, 1, 1},
197 {10, 1, 1, 1},
198 {20, 0, 2, 2},
199 {20, 1, 6, 3},
200 {55, 0, 4, 4},
201 {55, 1, 7, 7},
202 {110, 0, 5, 8},
204 #define BITRATE_TABLE_SIZE ARRAY_SIZE(bitrate_table)
206 /********************************************************************/
207 /* Data types */
208 /********************************************************************/
210 /* Beginning of the Tx descriptor, used in TxExc handling */
211 struct hermes_txexc_data {
212 struct hermes_tx_descriptor desc;
213 __le16 frame_ctl;
214 __le16 duration_id;
215 u8 addr1[ETH_ALEN];
216 } __attribute__ ((packed));
218 /* Rx frame header except compatibility 802.3 header */
219 struct hermes_rx_descriptor {
220 /* Control */
221 __le16 status;
222 __le32 time;
223 u8 silence;
224 u8 signal;
225 u8 rate;
226 u8 rxflow;
227 __le32 reserved;
229 /* 802.11 header */
230 __le16 frame_ctl;
231 __le16 duration_id;
232 u8 addr1[ETH_ALEN];
233 u8 addr2[ETH_ALEN];
234 u8 addr3[ETH_ALEN];
235 __le16 seq_ctl;
236 u8 addr4[ETH_ALEN];
238 /* Data length */
239 __le16 data_len;
240 } __attribute__ ((packed));
242 /********************************************************************/
243 /* Function prototypes */
244 /********************************************************************/
246 static int __orinoco_program_rids(struct net_device *dev);
247 static void __orinoco_set_multicast_list(struct net_device *dev);
249 /********************************************************************/
250 /* Michael MIC crypto setup */
251 /********************************************************************/
252 #define MICHAEL_MIC_LEN 8
253 static int orinoco_mic_init(struct orinoco_private *priv)
255 priv->tx_tfm_mic = crypto_alloc_hash("michael_mic", 0, 0);
256 if (IS_ERR(priv->tx_tfm_mic)) {
257 printk(KERN_DEBUG "orinoco_mic_init: could not allocate "
258 "crypto API michael_mic\n");
259 priv->tx_tfm_mic = NULL;
260 return -ENOMEM;
263 priv->rx_tfm_mic = crypto_alloc_hash("michael_mic", 0, 0);
264 if (IS_ERR(priv->rx_tfm_mic)) {
265 printk(KERN_DEBUG "orinoco_mic_init: could not allocate "
266 "crypto API michael_mic\n");
267 priv->rx_tfm_mic = NULL;
268 return -ENOMEM;
271 return 0;
274 static void orinoco_mic_free(struct orinoco_private *priv)
276 if (priv->tx_tfm_mic)
277 crypto_free_hash(priv->tx_tfm_mic);
278 if (priv->rx_tfm_mic)
279 crypto_free_hash(priv->rx_tfm_mic);
282 static int michael_mic(struct crypto_hash *tfm_michael, u8 *key,
283 u8 *da, u8 *sa, u8 priority,
284 u8 *data, size_t data_len, u8 *mic)
286 struct hash_desc desc;
287 struct scatterlist sg[2];
288 u8 hdr[ETH_HLEN + 2]; /* size of header + padding */
290 if (tfm_michael == NULL) {
291 printk(KERN_WARNING "michael_mic: tfm_michael == NULL\n");
292 return -1;
295 /* Copy header into buffer. We need the padding on the end zeroed */
296 memcpy(&hdr[0], da, ETH_ALEN);
297 memcpy(&hdr[ETH_ALEN], sa, ETH_ALEN);
298 hdr[ETH_ALEN*2] = priority;
299 hdr[ETH_ALEN*2+1] = 0;
300 hdr[ETH_ALEN*2+2] = 0;
301 hdr[ETH_ALEN*2+3] = 0;
303 /* Use scatter gather to MIC header and data in one go */
304 sg_init_table(sg, 2);
305 sg_set_buf(&sg[0], hdr, sizeof(hdr));
306 sg_set_buf(&sg[1], data, data_len);
308 if (crypto_hash_setkey(tfm_michael, key, MIC_KEYLEN))
309 return -1;
311 desc.tfm = tfm_michael;
312 desc.flags = 0;
313 return crypto_hash_digest(&desc, sg, data_len + sizeof(hdr),
314 mic);
317 /********************************************************************/
318 /* Internal helper functions */
319 /********************************************************************/
321 static inline void set_port_type(struct orinoco_private *priv)
323 switch (priv->iw_mode) {
324 case IW_MODE_INFRA:
325 priv->port_type = 1;
326 priv->createibss = 0;
327 break;
328 case IW_MODE_ADHOC:
329 if (priv->prefer_port3) {
330 priv->port_type = 3;
331 priv->createibss = 0;
332 } else {
333 priv->port_type = priv->ibss_port;
334 priv->createibss = 1;
336 break;
337 case IW_MODE_MONITOR:
338 priv->port_type = 3;
339 priv->createibss = 0;
340 break;
341 default:
342 printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
343 priv->ndev->name);
347 #define ORINOCO_MAX_BSS_COUNT 64
348 static int orinoco_bss_data_allocate(struct orinoco_private *priv)
350 if (priv->bss_xbss_data)
351 return 0;
353 if (priv->has_ext_scan)
354 priv->bss_xbss_data = kzalloc(ORINOCO_MAX_BSS_COUNT *
355 sizeof(struct xbss_element),
356 GFP_KERNEL);
357 else
358 priv->bss_xbss_data = kzalloc(ORINOCO_MAX_BSS_COUNT *
359 sizeof(struct bss_element),
360 GFP_KERNEL);
362 if (!priv->bss_xbss_data) {
363 printk(KERN_WARNING "Out of memory allocating beacons");
364 return -ENOMEM;
366 return 0;
369 static void orinoco_bss_data_free(struct orinoco_private *priv)
371 kfree(priv->bss_xbss_data);
372 priv->bss_xbss_data = NULL;
375 #define PRIV_BSS ((struct bss_element *)priv->bss_xbss_data)
376 #define PRIV_XBSS ((struct xbss_element *)priv->bss_xbss_data)
377 static void orinoco_bss_data_init(struct orinoco_private *priv)
379 int i;
381 INIT_LIST_HEAD(&priv->bss_free_list);
382 INIT_LIST_HEAD(&priv->bss_list);
383 if (priv->has_ext_scan)
384 for (i = 0; i < ORINOCO_MAX_BSS_COUNT; i++)
385 list_add_tail(&(PRIV_XBSS[i].list),
386 &priv->bss_free_list);
387 else
388 for (i = 0; i < ORINOCO_MAX_BSS_COUNT; i++)
389 list_add_tail(&(PRIV_BSS[i].list),
390 &priv->bss_free_list);
394 static inline u8 *orinoco_get_ie(u8 *data, size_t len,
395 enum ieee80211_mfie eid)
397 u8 *p = data;
398 while ((p + 2) < (data + len)) {
399 if (p[0] == eid)
400 return p;
401 p += p[1] + 2;
403 return NULL;
406 #define WPA_OUI_TYPE "\x00\x50\xF2\x01"
407 #define WPA_SELECTOR_LEN 4
408 static inline u8 *orinoco_get_wpa_ie(u8 *data, size_t len)
410 u8 *p = data;
411 while ((p + 2 + WPA_SELECTOR_LEN) < (data + len)) {
412 if ((p[0] == MFIE_TYPE_GENERIC) &&
413 (memcmp(&p[2], WPA_OUI_TYPE, WPA_SELECTOR_LEN) == 0))
414 return p;
415 p += p[1] + 2;
417 return NULL;
421 /********************************************************************/
422 /* Download functionality */
423 /********************************************************************/
425 struct fw_info {
426 char *pri_fw;
427 char *sta_fw;
428 char *ap_fw;
429 u32 pda_addr;
430 u16 pda_size;
433 const static struct fw_info orinoco_fw[] = {
434 { "", "agere_sta_fw.bin", "agere_ap_fw.bin", 0x00390000, 1000 },
435 { "", "prism_sta_fw.bin", "prism_ap_fw.bin", 0, 1024 },
436 { "symbol_sp24t_prim_fw", "symbol_sp24t_sec_fw", "", 0x00003100, 512 }
439 /* Structure used to access fields in FW
440 * Make sure LE decoding macros are used
442 struct orinoco_fw_header {
443 char hdr_vers[6]; /* ASCII string for header version */
444 __le16 headersize; /* Total length of header */
445 __le32 entry_point; /* NIC entry point */
446 __le32 blocks; /* Number of blocks to program */
447 __le32 block_offset; /* Offset of block data from eof header */
448 __le32 pdr_offset; /* Offset to PDR data from eof header */
449 __le32 pri_offset; /* Offset to primary plug data */
450 __le32 compat_offset; /* Offset to compatibility data*/
451 char signature[0]; /* FW signature length headersize-20 */
452 } __attribute__ ((packed));
454 /* Download either STA or AP firmware into the card. */
455 static int
456 orinoco_dl_firmware(struct orinoco_private *priv,
457 const struct fw_info *fw,
458 int ap)
460 /* Plug Data Area (PDA) */
461 __le16 *pda;
463 hermes_t *hw = &priv->hw;
464 const struct firmware *fw_entry;
465 const struct orinoco_fw_header *hdr;
466 const unsigned char *first_block;
467 const unsigned char *end;
468 const char *firmware;
469 struct net_device *dev = priv->ndev;
470 int err = 0;
472 pda = kzalloc(fw->pda_size, GFP_KERNEL);
473 if (!pda)
474 return -ENOMEM;
476 if (ap)
477 firmware = fw->ap_fw;
478 else
479 firmware = fw->sta_fw;
481 printk(KERN_DEBUG "%s: Attempting to download firmware %s\n",
482 dev->name, firmware);
484 /* Read current plug data */
485 err = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 0);
486 printk(KERN_DEBUG "%s: Read PDA returned %d\n", dev->name, err);
487 if (err)
488 goto free;
490 err = request_firmware(&fw_entry, firmware, priv->dev);
491 if (err) {
492 printk(KERN_ERR "%s: Cannot find firmware %s\n",
493 dev->name, firmware);
494 err = -ENOENT;
495 goto free;
498 hdr = (const struct orinoco_fw_header *) fw_entry->data;
500 /* Enable aux port to allow programming */
501 err = hermesi_program_init(hw, le32_to_cpu(hdr->entry_point));
502 printk(KERN_DEBUG "%s: Program init returned %d\n", dev->name, err);
503 if (err != 0)
504 goto abort;
506 /* Program data */
507 first_block = (fw_entry->data +
508 le16_to_cpu(hdr->headersize) +
509 le32_to_cpu(hdr->block_offset));
510 end = fw_entry->data + fw_entry->size;
512 err = hermes_program(hw, first_block, end);
513 printk(KERN_DEBUG "%s: Program returned %d\n", dev->name, err);
514 if (err != 0)
515 goto abort;
517 /* Update production data */
518 first_block = (fw_entry->data +
519 le16_to_cpu(hdr->headersize) +
520 le32_to_cpu(hdr->pdr_offset));
522 err = hermes_apply_pda_with_defaults(hw, first_block, pda);
523 printk(KERN_DEBUG "%s: Apply PDA returned %d\n", dev->name, err);
524 if (err)
525 goto abort;
527 /* Tell card we've finished */
528 err = hermesi_program_end(hw);
529 printk(KERN_DEBUG "%s: Program end returned %d\n", dev->name, err);
530 if (err != 0)
531 goto abort;
533 /* Check if we're running */
534 printk(KERN_DEBUG "%s: hermes_present returned %d\n",
535 dev->name, hermes_present(hw));
537 abort:
538 release_firmware(fw_entry);
540 free:
541 kfree(pda);
542 return err;
545 /* End markers */
546 #define TEXT_END 0x1A /* End of text header */
549 * Process a firmware image - stop the card, load the firmware, reset
550 * the card and make sure it responds. For the secondary firmware take
551 * care of the PDA - read it and then write it on top of the firmware.
553 static int
554 symbol_dl_image(struct orinoco_private *priv, const struct fw_info *fw,
555 const unsigned char *image, const unsigned char *end,
556 int secondary)
558 hermes_t *hw = &priv->hw;
559 int ret = 0;
560 const unsigned char *ptr;
561 const unsigned char *first_block;
563 /* Plug Data Area (PDA) */
564 __le16 *pda = NULL;
566 /* Binary block begins after the 0x1A marker */
567 ptr = image;
568 while (*ptr++ != TEXT_END);
569 first_block = ptr;
571 /* Read the PDA from EEPROM */
572 if (secondary) {
573 pda = kzalloc(fw->pda_size, GFP_KERNEL);
574 if (!pda)
575 return -ENOMEM;
577 ret = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 1);
578 if (ret)
579 goto free;
582 /* Stop the firmware, so that it can be safely rewritten */
583 if (priv->stop_fw) {
584 ret = priv->stop_fw(priv, 1);
585 if (ret)
586 goto free;
589 /* Program the adapter with new firmware */
590 ret = hermes_program(hw, first_block, end);
591 if (ret)
592 goto free;
594 /* Write the PDA to the adapter */
595 if (secondary) {
596 size_t len = hermes_blocks_length(first_block);
597 ptr = first_block + len;
598 ret = hermes_apply_pda(hw, ptr, pda);
599 kfree(pda);
600 if (ret)
601 return ret;
604 /* Run the firmware */
605 if (priv->stop_fw) {
606 ret = priv->stop_fw(priv, 0);
607 if (ret)
608 return ret;
611 /* Reset hermes chip and make sure it responds */
612 ret = hw->ops->init(hw);
614 /* hermes_reset() should return 0 with the secondary firmware */
615 if (secondary && ret != 0)
616 return -ENODEV;
618 /* And this should work with any firmware */
619 if (!hermes_present(hw))
620 return -ENODEV;
622 return 0;
624 free:
625 kfree(pda);
626 return ret;
631 * Download the firmware into the card, this also does a PCMCIA soft
632 * reset on the card, to make sure it's in a sane state.
634 static int
635 symbol_dl_firmware(struct orinoco_private *priv,
636 const struct fw_info *fw)
638 struct net_device *dev = priv->ndev;
639 int ret;
640 const struct firmware *fw_entry;
642 if (request_firmware(&fw_entry, fw->pri_fw,
643 priv->dev) != 0) {
644 printk(KERN_ERR "%s: Cannot find firmware: %s\n",
645 dev->name, fw->pri_fw);
646 return -ENOENT;
649 /* Load primary firmware */
650 ret = symbol_dl_image(priv, fw, fw_entry->data,
651 fw_entry->data + fw_entry->size, 0);
652 release_firmware(fw_entry);
653 if (ret) {
654 printk(KERN_ERR "%s: Primary firmware download failed\n",
655 dev->name);
656 return ret;
659 if (request_firmware(&fw_entry, fw->sta_fw,
660 priv->dev) != 0) {
661 printk(KERN_ERR "%s: Cannot find firmware: %s\n",
662 dev->name, fw->sta_fw);
663 return -ENOENT;
666 /* Load secondary firmware */
667 ret = symbol_dl_image(priv, fw, fw_entry->data,
668 fw_entry->data + fw_entry->size, 1);
669 release_firmware(fw_entry);
670 if (ret) {
671 printk(KERN_ERR "%s: Secondary firmware download failed\n",
672 dev->name);
675 return ret;
678 static int orinoco_download(struct orinoco_private *priv)
680 int err = 0;
681 /* Reload firmware */
682 switch (priv->firmware_type) {
683 case FIRMWARE_TYPE_AGERE:
684 /* case FIRMWARE_TYPE_INTERSIL: */
685 err = orinoco_dl_firmware(priv,
686 &orinoco_fw[priv->firmware_type], 0);
687 break;
689 case FIRMWARE_TYPE_SYMBOL:
690 err = symbol_dl_firmware(priv,
691 &orinoco_fw[priv->firmware_type]);
692 break;
693 case FIRMWARE_TYPE_INTERSIL:
694 break;
696 /* TODO: if we fail we probably need to reinitialise
697 * the driver */
699 return err;
702 /********************************************************************/
703 /* Device methods */
704 /********************************************************************/
706 static int orinoco_open(struct net_device *dev)
708 struct orinoco_private *priv = netdev_priv(dev);
709 unsigned long flags;
710 int err;
712 if (orinoco_lock(priv, &flags) != 0)
713 return -EBUSY;
715 err = __orinoco_up(dev);
717 if (! err)
718 priv->open = 1;
720 orinoco_unlock(priv, &flags);
722 return err;
725 static int orinoco_stop(struct net_device *dev)
727 struct orinoco_private *priv = netdev_priv(dev);
728 int err = 0;
730 /* We mustn't use orinoco_lock() here, because we need to be
731 able to close the interface even if hw_unavailable is set
732 (e.g. as we're released after a PC Card removal) */
733 spin_lock_irq(&priv->lock);
735 priv->open = 0;
737 err = __orinoco_down(dev);
739 spin_unlock_irq(&priv->lock);
741 return err;
744 static struct net_device_stats *orinoco_get_stats(struct net_device *dev)
746 struct orinoco_private *priv = netdev_priv(dev);
748 return &priv->stats;
751 static struct iw_statistics *orinoco_get_wireless_stats(struct net_device *dev)
753 struct orinoco_private *priv = netdev_priv(dev);
754 hermes_t *hw = &priv->hw;
755 struct iw_statistics *wstats = &priv->wstats;
756 int err;
757 unsigned long flags;
759 if (! netif_device_present(dev)) {
760 printk(KERN_WARNING "%s: get_wireless_stats() called while device not present\n",
761 dev->name);
762 return NULL; /* FIXME: Can we do better than this? */
765 /* If busy, return the old stats. Returning NULL may cause
766 * the interface to disappear from /proc/net/wireless */
767 if (orinoco_lock(priv, &flags) != 0)
768 return wstats;
770 /* We can't really wait for the tallies inquiry command to
771 * complete, so we just use the previous results and trigger
772 * a new tallies inquiry command for next time - Jean II */
773 /* FIXME: Really we should wait for the inquiry to come back -
774 * as it is the stats we give don't make a whole lot of sense.
775 * Unfortunately, it's not clear how to do that within the
776 * wireless extensions framework: I think we're in user
777 * context, but a lock seems to be held by the time we get in
778 * here so we're not safe to sleep here. */
779 hermes_inquire(hw, HERMES_INQ_TALLIES);
781 if (priv->iw_mode == IW_MODE_ADHOC) {
782 memset(&wstats->qual, 0, sizeof(wstats->qual));
783 /* If a spy address is defined, we report stats of the
784 * first spy address - Jean II */
785 if (SPY_NUMBER(priv)) {
786 wstats->qual.qual = priv->spy_data.spy_stat[0].qual;
787 wstats->qual.level = priv->spy_data.spy_stat[0].level;
788 wstats->qual.noise = priv->spy_data.spy_stat[0].noise;
789 wstats->qual.updated = priv->spy_data.spy_stat[0].updated;
791 } else {
792 struct {
793 __le16 qual, signal, noise, unused;
794 } __attribute__ ((packed)) cq;
796 err = HERMES_READ_RECORD(hw, USER_BAP,
797 HERMES_RID_COMMSQUALITY, &cq);
799 if (!err) {
800 wstats->qual.qual = (int)le16_to_cpu(cq.qual);
801 wstats->qual.level = (int)le16_to_cpu(cq.signal) - 0x95;
802 wstats->qual.noise = (int)le16_to_cpu(cq.noise) - 0x95;
803 wstats->qual.updated = 7;
807 orinoco_unlock(priv, &flags);
808 return wstats;
811 static void orinoco_set_multicast_list(struct net_device *dev)
813 struct orinoco_private *priv = netdev_priv(dev);
814 unsigned long flags;
816 if (orinoco_lock(priv, &flags) != 0) {
817 printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
818 "called when hw_unavailable\n", dev->name);
819 return;
822 __orinoco_set_multicast_list(dev);
823 orinoco_unlock(priv, &flags);
826 static int orinoco_change_mtu(struct net_device *dev, int new_mtu)
828 struct orinoco_private *priv = netdev_priv(dev);
830 if ( (new_mtu < ORINOCO_MIN_MTU) || (new_mtu > ORINOCO_MAX_MTU) )
831 return -EINVAL;
833 if ( (new_mtu + ENCAPS_OVERHEAD + IEEE80211_HLEN) >
834 (priv->nicbuf_size - ETH_HLEN) )
835 return -EINVAL;
837 dev->mtu = new_mtu;
839 return 0;
842 /********************************************************************/
843 /* Tx path */
844 /********************************************************************/
846 static int orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
848 struct orinoco_private *priv = netdev_priv(dev);
849 struct net_device_stats *stats = &priv->stats;
850 hermes_t *hw = &priv->hw;
851 int err = 0;
852 u16 txfid = priv->txfid;
853 struct ethhdr *eh;
854 int tx_control;
855 unsigned long flags;
857 if (! netif_running(dev)) {
858 printk(KERN_ERR "%s: Tx on stopped device!\n",
859 dev->name);
860 return NETDEV_TX_BUSY;
863 if (netif_queue_stopped(dev)) {
864 printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
865 dev->name);
866 return NETDEV_TX_BUSY;
869 if (orinoco_lock(priv, &flags) != 0) {
870 printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n",
871 dev->name);
872 return NETDEV_TX_BUSY;
875 if (! netif_carrier_ok(dev) || (priv->iw_mode == IW_MODE_MONITOR)) {
876 /* Oops, the firmware hasn't established a connection,
877 silently drop the packet (this seems to be the
878 safest approach). */
879 goto drop;
882 /* Check packet length */
883 if (skb->len < ETH_HLEN)
884 goto drop;
886 tx_control = HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX;
888 if (priv->encode_alg == IW_ENCODE_ALG_TKIP)
889 tx_control |= (priv->tx_key << HERMES_MIC_KEY_ID_SHIFT) |
890 HERMES_TXCTRL_MIC;
892 if (priv->has_alt_txcntl) {
893 /* WPA enabled firmwares have tx_cntl at the end of
894 * the 802.11 header. So write zeroed descriptor and
895 * 802.11 header at the same time
897 char desc[HERMES_802_3_OFFSET];
898 __le16 *txcntl = (__le16 *) &desc[HERMES_TXCNTL2_OFFSET];
900 memset(&desc, 0, sizeof(desc));
902 *txcntl = cpu_to_le16(tx_control);
903 err = hw->ops->bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
904 txfid, 0);
905 if (err) {
906 if (net_ratelimit())
907 printk(KERN_ERR "%s: Error %d writing Tx "
908 "descriptor to BAP\n", dev->name, err);
909 goto busy;
911 } else {
912 struct hermes_tx_descriptor desc;
914 memset(&desc, 0, sizeof(desc));
916 desc.tx_control = cpu_to_le16(tx_control);
917 err = hw->ops->bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
918 txfid, 0);
919 if (err) {
920 if (net_ratelimit())
921 printk(KERN_ERR "%s: Error %d writing Tx "
922 "descriptor to BAP\n", dev->name, err);
923 goto busy;
926 /* Clear the 802.11 header and data length fields - some
927 * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
928 * if this isn't done. */
929 hermes_clear_words(hw, HERMES_DATA0,
930 HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
933 eh = (struct ethhdr *)skb->data;
935 /* Encapsulate Ethernet-II frames */
936 if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
937 struct header_struct {
938 struct ethhdr eth; /* 802.3 header */
939 u8 encap[6]; /* 802.2 header */
940 } __attribute__ ((packed)) hdr;
942 /* Strip destination and source from the data */
943 skb_pull(skb, 2 * ETH_ALEN);
945 /* And move them to a separate header */
946 memcpy(&hdr.eth, eh, 2 * ETH_ALEN);
947 hdr.eth.h_proto = htons(sizeof(encaps_hdr) + skb->len);
948 memcpy(hdr.encap, encaps_hdr, sizeof(encaps_hdr));
950 /* Insert the SNAP header */
951 if (skb_headroom(skb) < sizeof(hdr)) {
952 printk(KERN_ERR
953 "%s: Not enough headroom for 802.2 headers %d\n",
954 dev->name, skb_headroom(skb));
955 goto drop;
957 eh = (struct ethhdr *) skb_push(skb, sizeof(hdr));
958 memcpy(eh, &hdr, sizeof(hdr));
961 err = hw->ops->bap_pwrite(hw, USER_BAP, skb->data, skb->len,
962 txfid, HERMES_802_3_OFFSET);
963 if (err) {
964 printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
965 dev->name, err);
966 goto busy;
969 /* Calculate Michael MIC */
970 if (priv->encode_alg == IW_ENCODE_ALG_TKIP) {
971 u8 mic_buf[MICHAEL_MIC_LEN + 1];
972 u8 *mic;
973 size_t offset;
974 size_t len;
976 if (skb->len % 2) {
977 /* MIC start is on an odd boundary */
978 mic_buf[0] = skb->data[skb->len - 1];
979 mic = &mic_buf[1];
980 offset = skb->len - 1;
981 len = MICHAEL_MIC_LEN + 1;
982 } else {
983 mic = &mic_buf[0];
984 offset = skb->len;
985 len = MICHAEL_MIC_LEN;
988 michael_mic(priv->tx_tfm_mic,
989 priv->tkip_key[priv->tx_key].tx_mic,
990 eh->h_dest, eh->h_source, 0 /* priority */,
991 skb->data + ETH_HLEN, skb->len - ETH_HLEN, mic);
993 /* Write the MIC */
994 err = hw->ops->bap_pwrite(hw, USER_BAP, &mic_buf[0], len,
995 txfid, HERMES_802_3_OFFSET + offset);
996 if (err) {
997 printk(KERN_ERR "%s: Error %d writing MIC to BAP\n",
998 dev->name, err);
999 goto busy;
1003 /* Finally, we actually initiate the send */
1004 netif_stop_queue(dev);
1006 err = hw->ops->cmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
1007 txfid, NULL);
1008 if (err) {
1009 netif_start_queue(dev);
1010 if (net_ratelimit())
1011 printk(KERN_ERR "%s: Error %d transmitting packet\n",
1012 dev->name, err);
1013 goto busy;
1016 dev->trans_start = jiffies;
1017 stats->tx_bytes += HERMES_802_3_OFFSET + skb->len;
1018 goto ok;
1020 drop:
1021 stats->tx_errors++;
1022 stats->tx_dropped++;
1025 orinoco_unlock(priv, &flags);
1026 dev_kfree_skb(skb);
1027 return NETDEV_TX_OK;
1029 busy:
1030 if (err == -EIO)
1031 schedule_work(&priv->reset_work);
1032 orinoco_unlock(priv, &flags);
1033 return NETDEV_TX_BUSY;
1036 static void __orinoco_ev_alloc(struct net_device *dev, hermes_t *hw)
1038 struct orinoco_private *priv = netdev_priv(dev);
1039 u16 fid = hermes_read_regn(hw, ALLOCFID);
1041 if (fid != priv->txfid) {
1042 if (fid != DUMMY_FID)
1043 printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
1044 dev->name, fid);
1045 return;
1048 hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
1051 static void __orinoco_ev_tx(struct net_device *dev, hermes_t *hw)
1053 struct orinoco_private *priv = netdev_priv(dev);
1054 struct net_device_stats *stats = &priv->stats;
1056 stats->tx_packets++;
1058 netif_wake_queue(dev);
1060 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
1063 static void __orinoco_ev_txexc(struct net_device *dev, hermes_t *hw)
1065 struct orinoco_private *priv = netdev_priv(dev);
1066 struct net_device_stats *stats = &priv->stats;
1067 u16 fid = hermes_read_regn(hw, TXCOMPLFID);
1068 u16 status;
1069 struct hermes_txexc_data hdr;
1070 int err = 0;
1072 if (fid == DUMMY_FID)
1073 return; /* Nothing's really happened */
1075 /* Read part of the frame header - we need status and addr1 */
1076 err = hw->ops->bap_pread(hw, IRQ_BAP, &hdr,
1077 sizeof(struct hermes_txexc_data),
1078 fid, 0);
1080 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
1081 stats->tx_errors++;
1083 if (err) {
1084 printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
1085 "(FID=%04X error %d)\n",
1086 dev->name, fid, err);
1087 return;
1090 DEBUG(1, "%s: Tx error, err %d (FID=%04X)\n", dev->name,
1091 err, fid);
1093 /* We produce a TXDROP event only for retry or lifetime
1094 * exceeded, because that's the only status that really mean
1095 * that this particular node went away.
1096 * Other errors means that *we* screwed up. - Jean II */
1097 status = le16_to_cpu(hdr.desc.status);
1098 if (status & (HERMES_TXSTAT_RETRYERR | HERMES_TXSTAT_AGEDERR)) {
1099 union iwreq_data wrqu;
1101 /* Copy 802.11 dest address.
1102 * We use the 802.11 header because the frame may
1103 * not be 802.3 or may be mangled...
1104 * In Ad-Hoc mode, it will be the node address.
1105 * In managed mode, it will be most likely the AP addr
1106 * User space will figure out how to convert it to
1107 * whatever it needs (IP address or else).
1108 * - Jean II */
1109 memcpy(wrqu.addr.sa_data, hdr.addr1, ETH_ALEN);
1110 wrqu.addr.sa_family = ARPHRD_ETHER;
1112 /* Send event to user space */
1113 wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
1116 netif_wake_queue(dev);
1119 static void orinoco_tx_timeout(struct net_device *dev)
1121 struct orinoco_private *priv = netdev_priv(dev);
1122 struct net_device_stats *stats = &priv->stats;
1123 struct hermes *hw = &priv->hw;
1125 printk(KERN_WARNING "%s: Tx timeout! "
1126 "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
1127 dev->name, hermes_read_regn(hw, ALLOCFID),
1128 hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
1130 stats->tx_errors++;
1132 schedule_work(&priv->reset_work);
1135 /********************************************************************/
1136 /* Rx path (data frames) */
1137 /********************************************************************/
1139 /* Does the frame have a SNAP header indicating it should be
1140 * de-encapsulated to Ethernet-II? */
1141 static inline int is_ethersnap(void *_hdr)
1143 u8 *hdr = _hdr;
1145 /* We de-encapsulate all packets which, a) have SNAP headers
1146 * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
1147 * and where b) the OUI of the SNAP header is 00:00:00 or
1148 * 00:00:f8 - we need both because different APs appear to use
1149 * different OUIs for some reason */
1150 return (memcmp(hdr, &encaps_hdr, 5) == 0)
1151 && ( (hdr[5] == 0x00) || (hdr[5] == 0xf8) );
1154 static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
1155 int level, int noise)
1157 struct iw_quality wstats;
1158 wstats.level = level - 0x95;
1159 wstats.noise = noise - 0x95;
1160 wstats.qual = (level > noise) ? (level - noise) : 0;
1161 wstats.updated = 7;
1162 /* Update spy records */
1163 wireless_spy_update(dev, mac, &wstats);
1166 static void orinoco_stat_gather(struct net_device *dev,
1167 struct sk_buff *skb,
1168 struct hermes_rx_descriptor *desc)
1170 struct orinoco_private *priv = netdev_priv(dev);
1172 /* Using spy support with lots of Rx packets, like in an
1173 * infrastructure (AP), will really slow down everything, because
1174 * the MAC address must be compared to each entry of the spy list.
1175 * If the user really asks for it (set some address in the
1176 * spy list), we do it, but he will pay the price.
1177 * Note that to get here, you need both WIRELESS_SPY
1178 * compiled in AND some addresses in the list !!!
1180 /* Note : gcc will optimise the whole section away if
1181 * WIRELESS_SPY is not defined... - Jean II */
1182 if (SPY_NUMBER(priv)) {
1183 orinoco_spy_gather(dev, skb_mac_header(skb) + ETH_ALEN,
1184 desc->signal, desc->silence);
1189 * orinoco_rx_monitor - handle received monitor frames.
1191 * Arguments:
1192 * dev network device
1193 * rxfid received FID
1194 * desc rx descriptor of the frame
1196 * Call context: interrupt
1198 static void orinoco_rx_monitor(struct net_device *dev, u16 rxfid,
1199 struct hermes_rx_descriptor *desc)
1201 u32 hdrlen = 30; /* return full header by default */
1202 u32 datalen = 0;
1203 u16 fc;
1204 int err;
1205 int len;
1206 struct sk_buff *skb;
1207 struct orinoco_private *priv = netdev_priv(dev);
1208 struct net_device_stats *stats = &priv->stats;
1209 hermes_t *hw = &priv->hw;
1211 len = le16_to_cpu(desc->data_len);
1213 /* Determine the size of the header and the data */
1214 fc = le16_to_cpu(desc->frame_ctl);
1215 switch (fc & IEEE80211_FCTL_FTYPE) {
1216 case IEEE80211_FTYPE_DATA:
1217 if ((fc & IEEE80211_FCTL_TODS)
1218 && (fc & IEEE80211_FCTL_FROMDS))
1219 hdrlen = 30;
1220 else
1221 hdrlen = 24;
1222 datalen = len;
1223 break;
1224 case IEEE80211_FTYPE_MGMT:
1225 hdrlen = 24;
1226 datalen = len;
1227 break;
1228 case IEEE80211_FTYPE_CTL:
1229 switch (fc & IEEE80211_FCTL_STYPE) {
1230 case IEEE80211_STYPE_PSPOLL:
1231 case IEEE80211_STYPE_RTS:
1232 case IEEE80211_STYPE_CFEND:
1233 case IEEE80211_STYPE_CFENDACK:
1234 hdrlen = 16;
1235 break;
1236 case IEEE80211_STYPE_CTS:
1237 case IEEE80211_STYPE_ACK:
1238 hdrlen = 10;
1239 break;
1241 break;
1242 default:
1243 /* Unknown frame type */
1244 break;
1247 /* sanity check the length */
1248 if (datalen > IEEE80211_DATA_LEN + 12) {
1249 printk(KERN_DEBUG "%s: oversized monitor frame, "
1250 "data length = %d\n", dev->name, datalen);
1251 err = -EIO;
1252 stats->rx_length_errors++;
1253 goto update_stats;
1256 skb = dev_alloc_skb(hdrlen + datalen);
1257 if (!skb) {
1258 printk(KERN_WARNING "%s: Cannot allocate skb for monitor frame\n",
1259 dev->name);
1260 err = -ENOMEM;
1261 goto drop;
1264 /* Copy the 802.11 header to the skb */
1265 memcpy(skb_put(skb, hdrlen), &(desc->frame_ctl), hdrlen);
1266 skb_reset_mac_header(skb);
1268 /* If any, copy the data from the card to the skb */
1269 if (datalen > 0) {
1270 err = hw->ops->bap_pread(hw, IRQ_BAP, skb_put(skb, datalen),
1271 ALIGN(datalen, 2), rxfid,
1272 HERMES_802_2_OFFSET);
1273 if (err) {
1274 printk(KERN_ERR "%s: error %d reading monitor frame\n",
1275 dev->name, err);
1276 goto drop;
1280 skb->dev = dev;
1281 skb->ip_summed = CHECKSUM_NONE;
1282 skb->pkt_type = PACKET_OTHERHOST;
1283 skb->protocol = __constant_htons(ETH_P_802_2);
1285 dev->last_rx = jiffies;
1286 stats->rx_packets++;
1287 stats->rx_bytes += skb->len;
1289 netif_rx(skb);
1290 return;
1292 drop:
1293 dev_kfree_skb_irq(skb);
1294 update_stats:
1295 stats->rx_errors++;
1296 stats->rx_dropped++;
1299 /* Get tsc from the firmware */
1300 static int orinoco_hw_get_tkip_iv(struct orinoco_private *priv, int key,
1301 u8 *tsc)
1303 hermes_t *hw = &priv->hw;
1304 int err = 0;
1305 u8 tsc_arr[4][IW_ENCODE_SEQ_MAX_SIZE];
1307 if ((key < 0) || (key > 4))
1308 return -EINVAL;
1310 err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_TKIP_IV,
1311 sizeof(tsc_arr), NULL, &tsc_arr);
1312 if (!err)
1313 memcpy(tsc, &tsc_arr[key][0], sizeof(tsc_arr[0]));
1315 return err;
1318 static void __orinoco_ev_rx(struct net_device *dev, hermes_t *hw)
1320 struct orinoco_private *priv = netdev_priv(dev);
1321 struct net_device_stats *stats = &priv->stats;
1322 struct iw_statistics *wstats = &priv->wstats;
1323 struct sk_buff *skb = NULL;
1324 u16 rxfid, status;
1325 int length;
1326 struct hermes_rx_descriptor *desc;
1327 struct orinoco_rx_data *rx_data;
1328 int err;
1330 desc = kmalloc(sizeof(*desc), GFP_ATOMIC);
1331 if (!desc) {
1332 printk(KERN_WARNING
1333 "%s: Can't allocate space for RX descriptor\n",
1334 dev->name);
1335 goto update_stats;
1338 rxfid = hermes_read_regn(hw, RXFID);
1340 err = hw->ops->bap_pread(hw, IRQ_BAP, desc, sizeof(*desc),
1341 rxfid, 0);
1342 if (err) {
1343 printk(KERN_ERR "%s: error %d reading Rx descriptor. "
1344 "Frame dropped.\n", dev->name, err);
1345 goto update_stats;
1348 status = le16_to_cpu(desc->status);
1350 if (status & HERMES_RXSTAT_BADCRC) {
1351 DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n",
1352 dev->name);
1353 stats->rx_crc_errors++;
1354 goto update_stats;
1357 /* Handle frames in monitor mode */
1358 if (priv->iw_mode == IW_MODE_MONITOR) {
1359 orinoco_rx_monitor(dev, rxfid, desc);
1360 goto out;
1363 if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
1364 DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
1365 dev->name);
1366 wstats->discard.code++;
1367 goto update_stats;
1370 length = le16_to_cpu(desc->data_len);
1372 /* Sanity checks */
1373 if (length < 3) { /* No for even an 802.2 LLC header */
1374 /* At least on Symbol firmware with PCF we get quite a
1375 lot of these legitimately - Poll frames with no
1376 data. */
1377 goto out;
1379 if (length > IEEE80211_DATA_LEN) {
1380 printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
1381 dev->name, length);
1382 stats->rx_length_errors++;
1383 goto update_stats;
1386 /* Payload size does not include Michael MIC. Increase payload
1387 * size to read it together with the data. */
1388 if (status & HERMES_RXSTAT_MIC)
1389 length += MICHAEL_MIC_LEN;
1391 /* We need space for the packet data itself, plus an ethernet
1392 header, plus 2 bytes so we can align the IP header on a
1393 32bit boundary, plus 1 byte so we can read in odd length
1394 packets from the card, which has an IO granularity of 16
1395 bits */
1396 skb = dev_alloc_skb(length+ETH_HLEN+2+1);
1397 if (!skb) {
1398 printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
1399 dev->name);
1400 goto update_stats;
1403 /* We'll prepend the header, so reserve space for it. The worst
1404 case is no decapsulation, when 802.3 header is prepended and
1405 nothing is removed. 2 is for aligning the IP header. */
1406 skb_reserve(skb, ETH_HLEN + 2);
1408 err = hw->ops->bap_pread(hw, IRQ_BAP, skb_put(skb, length),
1409 ALIGN(length, 2), rxfid,
1410 HERMES_802_2_OFFSET);
1411 if (err) {
1412 printk(KERN_ERR "%s: error %d reading frame. "
1413 "Frame dropped.\n", dev->name, err);
1414 goto drop;
1417 /* Add desc and skb to rx queue */
1418 rx_data = kzalloc(sizeof(*rx_data), GFP_ATOMIC);
1419 if (!rx_data) {
1420 printk(KERN_WARNING "%s: Can't allocate RX packet\n",
1421 dev->name);
1422 goto drop;
1424 rx_data->desc = desc;
1425 rx_data->skb = skb;
1426 list_add_tail(&rx_data->list, &priv->rx_list);
1427 tasklet_schedule(&priv->rx_tasklet);
1429 return;
1431 drop:
1432 dev_kfree_skb_irq(skb);
1433 update_stats:
1434 stats->rx_errors++;
1435 stats->rx_dropped++;
1436 out:
1437 kfree(desc);
1440 static void orinoco_rx(struct net_device *dev,
1441 struct hermes_rx_descriptor *desc,
1442 struct sk_buff *skb)
1444 struct orinoco_private *priv = netdev_priv(dev);
1445 struct net_device_stats *stats = &priv->stats;
1446 u16 status, fc;
1447 int length;
1448 struct ethhdr *hdr;
1450 status = le16_to_cpu(desc->status);
1451 length = le16_to_cpu(desc->data_len);
1452 fc = le16_to_cpu(desc->frame_ctl);
1454 /* Calculate and check MIC */
1455 if (status & HERMES_RXSTAT_MIC) {
1456 int key_id = ((status & HERMES_RXSTAT_MIC_KEY_ID) >>
1457 HERMES_MIC_KEY_ID_SHIFT);
1458 u8 mic[MICHAEL_MIC_LEN];
1459 u8 *rxmic;
1460 u8 *src = (fc & IEEE80211_FCTL_FROMDS) ?
1461 desc->addr3 : desc->addr2;
1463 /* Extract Michael MIC from payload */
1464 rxmic = skb->data + skb->len - MICHAEL_MIC_LEN;
1466 skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
1467 length -= MICHAEL_MIC_LEN;
1469 michael_mic(priv->rx_tfm_mic,
1470 priv->tkip_key[key_id].rx_mic,
1471 desc->addr1,
1472 src,
1473 0, /* priority or QoS? */
1474 skb->data,
1475 skb->len,
1476 &mic[0]);
1478 if (memcmp(mic, rxmic,
1479 MICHAEL_MIC_LEN)) {
1480 union iwreq_data wrqu;
1481 struct iw_michaelmicfailure wxmic;
1482 DECLARE_MAC_BUF(mac);
1484 printk(KERN_WARNING "%s: "
1485 "Invalid Michael MIC in data frame from %s, "
1486 "using key %i\n",
1487 dev->name, print_mac(mac, src), key_id);
1489 /* TODO: update stats */
1491 /* Notify userspace */
1492 memset(&wxmic, 0, sizeof(wxmic));
1493 wxmic.flags = key_id & IW_MICFAILURE_KEY_ID;
1494 wxmic.flags |= (desc->addr1[0] & 1) ?
1495 IW_MICFAILURE_GROUP : IW_MICFAILURE_PAIRWISE;
1496 wxmic.src_addr.sa_family = ARPHRD_ETHER;
1497 memcpy(wxmic.src_addr.sa_data, src, ETH_ALEN);
1499 (void) orinoco_hw_get_tkip_iv(priv, key_id,
1500 &wxmic.tsc[0]);
1502 memset(&wrqu, 0, sizeof(wrqu));
1503 wrqu.data.length = sizeof(wxmic);
1504 wireless_send_event(dev, IWEVMICHAELMICFAILURE, &wrqu,
1505 (char *) &wxmic);
1507 goto drop;
1511 /* Handle decapsulation
1512 * In most cases, the firmware tell us about SNAP frames.
1513 * For some reason, the SNAP frames sent by LinkSys APs
1514 * are not properly recognised by most firmwares.
1515 * So, check ourselves */
1516 if (length >= ENCAPS_OVERHEAD &&
1517 (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
1518 ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
1519 is_ethersnap(skb->data))) {
1520 /* These indicate a SNAP within 802.2 LLC within
1521 802.11 frame which we'll need to de-encapsulate to
1522 the original EthernetII frame. */
1523 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN - ENCAPS_OVERHEAD);
1524 } else {
1525 /* 802.3 frame - prepend 802.3 header as is */
1526 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN);
1527 hdr->h_proto = htons(length);
1529 memcpy(hdr->h_dest, desc->addr1, ETH_ALEN);
1530 if (fc & IEEE80211_FCTL_FROMDS)
1531 memcpy(hdr->h_source, desc->addr3, ETH_ALEN);
1532 else
1533 memcpy(hdr->h_source, desc->addr2, ETH_ALEN);
1535 dev->last_rx = jiffies;
1536 skb->protocol = eth_type_trans(skb, dev);
1537 skb->ip_summed = CHECKSUM_NONE;
1538 if (fc & IEEE80211_FCTL_TODS)
1539 skb->pkt_type = PACKET_OTHERHOST;
1541 /* Process the wireless stats if needed */
1542 orinoco_stat_gather(dev, skb, desc);
1544 /* Pass the packet to the networking stack */
1545 netif_rx(skb);
1546 stats->rx_packets++;
1547 stats->rx_bytes += length;
1549 return;
1551 drop:
1552 dev_kfree_skb(skb);
1553 stats->rx_errors++;
1554 stats->rx_dropped++;
1557 static void orinoco_rx_isr_tasklet(unsigned long data)
1559 struct net_device *dev = (struct net_device *) data;
1560 struct orinoco_private *priv = netdev_priv(dev);
1561 struct orinoco_rx_data *rx_data, *temp;
1562 struct hermes_rx_descriptor *desc;
1563 struct sk_buff *skb;
1565 /* extract desc and skb from queue */
1566 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
1567 desc = rx_data->desc;
1568 skb = rx_data->skb;
1569 list_del(&rx_data->list);
1570 kfree(rx_data);
1572 orinoco_rx(dev, desc, skb);
1574 kfree(desc);
1578 /********************************************************************/
1579 /* Rx path (info frames) */
1580 /********************************************************************/
1582 static void print_linkstatus(struct net_device *dev, u16 status)
1584 char * s;
1586 if (suppress_linkstatus)
1587 return;
1589 switch (status) {
1590 case HERMES_LINKSTATUS_NOT_CONNECTED:
1591 s = "Not Connected";
1592 break;
1593 case HERMES_LINKSTATUS_CONNECTED:
1594 s = "Connected";
1595 break;
1596 case HERMES_LINKSTATUS_DISCONNECTED:
1597 s = "Disconnected";
1598 break;
1599 case HERMES_LINKSTATUS_AP_CHANGE:
1600 s = "AP Changed";
1601 break;
1602 case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
1603 s = "AP Out of Range";
1604 break;
1605 case HERMES_LINKSTATUS_AP_IN_RANGE:
1606 s = "AP In Range";
1607 break;
1608 case HERMES_LINKSTATUS_ASSOC_FAILED:
1609 s = "Association Failed";
1610 break;
1611 default:
1612 s = "UNKNOWN";
1615 printk(KERN_INFO "%s: New link status: %s (%04x)\n",
1616 dev->name, s, status);
1619 /* Search scan results for requested BSSID, join it if found */
1620 static void orinoco_join_ap(struct work_struct *work)
1622 struct orinoco_private *priv =
1623 container_of(work, struct orinoco_private, join_work);
1624 struct net_device *dev = priv->ndev;
1625 struct hermes *hw = &priv->hw;
1626 int err;
1627 unsigned long flags;
1628 struct join_req {
1629 u8 bssid[ETH_ALEN];
1630 __le16 channel;
1631 } __attribute__ ((packed)) req;
1632 const int atom_len = offsetof(struct prism2_scan_apinfo, atim);
1633 struct prism2_scan_apinfo *atom = NULL;
1634 int offset = 4;
1635 int found = 0;
1636 u8 *buf;
1637 u16 len;
1639 /* Allocate buffer for scan results */
1640 buf = kmalloc(MAX_SCAN_LEN, GFP_KERNEL);
1641 if (! buf)
1642 return;
1644 if (orinoco_lock(priv, &flags) != 0)
1645 goto fail_lock;
1647 /* Sanity checks in case user changed something in the meantime */
1648 if (! priv->bssid_fixed)
1649 goto out;
1651 if (strlen(priv->desired_essid) == 0)
1652 goto out;
1654 /* Read scan results from the firmware */
1655 err = hw->ops->read_ltv(hw, USER_BAP,
1656 HERMES_RID_SCANRESULTSTABLE,
1657 MAX_SCAN_LEN, &len, buf);
1658 if (err) {
1659 printk(KERN_ERR "%s: Cannot read scan results\n",
1660 dev->name);
1661 goto out;
1664 len = HERMES_RECLEN_TO_BYTES(len);
1666 /* Go through the scan results looking for the channel of the AP
1667 * we were requested to join */
1668 for (; offset + atom_len <= len; offset += atom_len) {
1669 atom = (struct prism2_scan_apinfo *) (buf + offset);
1670 if (memcmp(&atom->bssid, priv->desired_bssid, ETH_ALEN) == 0) {
1671 found = 1;
1672 break;
1676 if (! found) {
1677 DEBUG(1, "%s: Requested AP not found in scan results\n",
1678 dev->name);
1679 goto out;
1682 memcpy(req.bssid, priv->desired_bssid, ETH_ALEN);
1683 req.channel = atom->channel; /* both are little-endian */
1684 err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNFJOINREQUEST,
1685 &req);
1686 if (err)
1687 printk(KERN_ERR "%s: Error issuing join request\n", dev->name);
1689 out:
1690 orinoco_unlock(priv, &flags);
1692 fail_lock:
1693 kfree(buf);
1696 /* Send new BSSID to userspace */
1697 static void orinoco_send_bssid_wevent(struct orinoco_private *priv)
1699 struct net_device *dev = priv->ndev;
1700 struct hermes *hw = &priv->hw;
1701 union iwreq_data wrqu;
1702 int err;
1704 err = hw->ops->read_ltv(hw, IRQ_BAP, HERMES_RID_CURRENTBSSID,
1705 ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
1706 if (err != 0)
1707 return;
1709 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1711 /* Send event to user space */
1712 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
1715 static void orinoco_send_assocreqie_wevent(struct orinoco_private *priv)
1717 struct net_device *dev = priv->ndev;
1718 struct hermes *hw = &priv->hw;
1719 union iwreq_data wrqu;
1720 int err;
1721 u8 buf[88];
1722 u8 *ie;
1724 if (!priv->has_wpa)
1725 return;
1727 err = hw->ops->read_ltv(hw, IRQ_BAP, HERMES_RID_CURRENT_ASSOC_REQ_INFO,
1728 sizeof(buf), NULL, &buf);
1729 if (err != 0)
1730 return;
1732 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1733 if (ie) {
1734 int rem = sizeof(buf) - (ie - &buf[0]);
1735 wrqu.data.length = ie[1] + 2;
1736 if (wrqu.data.length > rem)
1737 wrqu.data.length = rem;
1739 if (wrqu.data.length)
1740 /* Send event to user space */
1741 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, ie);
1745 static void orinoco_send_assocrespie_wevent(struct orinoco_private *priv)
1747 struct net_device *dev = priv->ndev;
1748 struct hermes *hw = &priv->hw;
1749 union iwreq_data wrqu;
1750 int err;
1751 u8 buf[88]; /* TODO: verify max size or IW_GENERIC_IE_MAX */
1752 u8 *ie;
1754 if (!priv->has_wpa)
1755 return;
1757 err = hw->ops->read_ltv(hw, IRQ_BAP, HERMES_RID_CURRENT_ASSOC_RESP_INFO,
1758 sizeof(buf), NULL, &buf);
1759 if (err != 0)
1760 return;
1762 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1763 if (ie) {
1764 int rem = sizeof(buf) - (ie - &buf[0]);
1765 wrqu.data.length = ie[1] + 2;
1766 if (wrqu.data.length > rem)
1767 wrqu.data.length = rem;
1769 if (wrqu.data.length)
1770 /* Send event to user space */
1771 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, ie);
1775 static void orinoco_send_wevents(struct work_struct *work)
1777 struct orinoco_private *priv =
1778 container_of(work, struct orinoco_private, wevent_work);
1779 unsigned long flags;
1781 if (orinoco_lock(priv, &flags) != 0)
1782 return;
1784 orinoco_send_assocreqie_wevent(priv);
1785 orinoco_send_assocrespie_wevent(priv);
1786 orinoco_send_bssid_wevent(priv);
1788 orinoco_unlock(priv, &flags);
1791 static inline void orinoco_clear_scan_results(struct orinoco_private *priv,
1792 unsigned long scan_age)
1794 if (priv->has_ext_scan) {
1795 struct xbss_element *bss;
1796 struct xbss_element *tmp_bss;
1798 /* Blow away current list of scan results */
1799 list_for_each_entry_safe(bss, tmp_bss, &priv->bss_list, list) {
1800 if (!scan_age ||
1801 time_after(jiffies, bss->last_scanned + scan_age)) {
1802 list_move_tail(&bss->list,
1803 &priv->bss_free_list);
1804 /* Don't blow away ->list, just BSS data */
1805 memset(&bss->bss, 0, sizeof(bss->bss));
1806 bss->last_scanned = 0;
1809 } else {
1810 struct bss_element *bss;
1811 struct bss_element *tmp_bss;
1813 /* Blow away current list of scan results */
1814 list_for_each_entry_safe(bss, tmp_bss, &priv->bss_list, list) {
1815 if (!scan_age ||
1816 time_after(jiffies, bss->last_scanned + scan_age)) {
1817 list_move_tail(&bss->list,
1818 &priv->bss_free_list);
1819 /* Don't blow away ->list, just BSS data */
1820 memset(&bss->bss, 0, sizeof(bss->bss));
1821 bss->last_scanned = 0;
1827 static void orinoco_add_ext_scan_result(struct orinoco_private *priv,
1828 struct agere_ext_scan_info *atom)
1830 struct xbss_element *bss = NULL;
1831 int found = 0;
1833 /* Try to update an existing bss first */
1834 list_for_each_entry(bss, &priv->bss_list, list) {
1835 if (compare_ether_addr(bss->bss.bssid, atom->bssid))
1836 continue;
1837 /* ESSID lengths */
1838 if (bss->bss.data[1] != atom->data[1])
1839 continue;
1840 if (memcmp(&bss->bss.data[2], &atom->data[2],
1841 atom->data[1]))
1842 continue;
1843 found = 1;
1844 break;
1847 /* Grab a bss off the free list */
1848 if (!found && !list_empty(&priv->bss_free_list)) {
1849 bss = list_entry(priv->bss_free_list.next,
1850 struct xbss_element, list);
1851 list_del(priv->bss_free_list.next);
1853 list_add_tail(&bss->list, &priv->bss_list);
1856 if (bss) {
1857 /* Always update the BSS to get latest beacon info */
1858 memcpy(&bss->bss, atom, sizeof(bss->bss));
1859 bss->last_scanned = jiffies;
1863 static int orinoco_process_scan_results(struct net_device *dev,
1864 unsigned char *buf,
1865 int len)
1867 struct orinoco_private *priv = netdev_priv(dev);
1868 int offset; /* In the scan data */
1869 union hermes_scan_info *atom;
1870 int atom_len;
1872 switch (priv->firmware_type) {
1873 case FIRMWARE_TYPE_AGERE:
1874 atom_len = sizeof(struct agere_scan_apinfo);
1875 offset = 0;
1876 break;
1877 case FIRMWARE_TYPE_SYMBOL:
1878 /* Lack of documentation necessitates this hack.
1879 * Different firmwares have 68 or 76 byte long atoms.
1880 * We try modulo first. If the length divides by both,
1881 * we check what would be the channel in the second
1882 * frame for a 68-byte atom. 76-byte atoms have 0 there.
1883 * Valid channel cannot be 0. */
1884 if (len % 76)
1885 atom_len = 68;
1886 else if (len % 68)
1887 atom_len = 76;
1888 else if (len >= 1292 && buf[68] == 0)
1889 atom_len = 76;
1890 else
1891 atom_len = 68;
1892 offset = 0;
1893 break;
1894 case FIRMWARE_TYPE_INTERSIL:
1895 offset = 4;
1896 if (priv->has_hostscan) {
1897 atom_len = le16_to_cpup((__le16 *)buf);
1898 /* Sanity check for atom_len */
1899 if (atom_len < sizeof(struct prism2_scan_apinfo)) {
1900 printk(KERN_ERR "%s: Invalid atom_len in scan "
1901 "data: %d\n", dev->name, atom_len);
1902 return -EIO;
1904 } else
1905 atom_len = offsetof(struct prism2_scan_apinfo, atim);
1906 break;
1907 default:
1908 return -EOPNOTSUPP;
1911 /* Check that we got an whole number of atoms */
1912 if ((len - offset) % atom_len) {
1913 printk(KERN_ERR "%s: Unexpected scan data length %d, "
1914 "atom_len %d, offset %d\n", dev->name, len,
1915 atom_len, offset);
1916 return -EIO;
1919 orinoco_clear_scan_results(priv, msecs_to_jiffies(15000));
1921 /* Read the entries one by one */
1922 for (; offset + atom_len <= len; offset += atom_len) {
1923 int found = 0;
1924 struct bss_element *bss = NULL;
1926 /* Get next atom */
1927 atom = (union hermes_scan_info *) (buf + offset);
1929 /* Try to update an existing bss first */
1930 list_for_each_entry(bss, &priv->bss_list, list) {
1931 if (compare_ether_addr(bss->bss.a.bssid, atom->a.bssid))
1932 continue;
1933 if (le16_to_cpu(bss->bss.a.essid_len) !=
1934 le16_to_cpu(atom->a.essid_len))
1935 continue;
1936 if (memcmp(bss->bss.a.essid, atom->a.essid,
1937 le16_to_cpu(atom->a.essid_len)))
1938 continue;
1939 found = 1;
1940 break;
1943 /* Grab a bss off the free list */
1944 if (!found && !list_empty(&priv->bss_free_list)) {
1945 bss = list_entry(priv->bss_free_list.next,
1946 struct bss_element, list);
1947 list_del(priv->bss_free_list.next);
1949 list_add_tail(&bss->list, &priv->bss_list);
1952 if (bss) {
1953 /* Always update the BSS to get latest beacon info */
1954 memcpy(&bss->bss, atom, sizeof(bss->bss));
1955 bss->last_scanned = jiffies;
1959 return 0;
1962 static void __orinoco_ev_info(struct net_device *dev, hermes_t *hw)
1964 struct orinoco_private *priv = netdev_priv(dev);
1965 u16 infofid;
1966 struct {
1967 __le16 len;
1968 __le16 type;
1969 } __attribute__ ((packed)) info;
1970 int len, type;
1971 int err;
1973 /* This is an answer to an INQUIRE command that we did earlier,
1974 * or an information "event" generated by the card
1975 * The controller return to us a pseudo frame containing
1976 * the information in question - Jean II */
1977 infofid = hermes_read_regn(hw, INFOFID);
1979 /* Read the info frame header - don't try too hard */
1980 err = hw->ops->bap_pread(hw, IRQ_BAP, &info, sizeof(info),
1981 infofid, 0);
1982 if (err) {
1983 printk(KERN_ERR "%s: error %d reading info frame. "
1984 "Frame dropped.\n", dev->name, err);
1985 return;
1988 len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
1989 type = le16_to_cpu(info.type);
1991 switch (type) {
1992 case HERMES_INQ_TALLIES: {
1993 struct hermes_tallies_frame tallies;
1994 struct iw_statistics *wstats = &priv->wstats;
1996 if (len > sizeof(tallies)) {
1997 printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
1998 dev->name, len);
1999 len = sizeof(tallies);
2002 err = hw->ops->bap_pread(hw, IRQ_BAP, &tallies, len,
2003 infofid, sizeof(info));
2004 if (err)
2005 break;
2007 /* Increment our various counters */
2008 /* wstats->discard.nwid - no wrong BSSID stuff */
2009 wstats->discard.code +=
2010 le16_to_cpu(tallies.RxWEPUndecryptable);
2011 if (len == sizeof(tallies))
2012 wstats->discard.code +=
2013 le16_to_cpu(tallies.RxDiscards_WEPICVError) +
2014 le16_to_cpu(tallies.RxDiscards_WEPExcluded);
2015 wstats->discard.misc +=
2016 le16_to_cpu(tallies.TxDiscardsWrongSA);
2017 wstats->discard.fragment +=
2018 le16_to_cpu(tallies.RxMsgInBadMsgFragments);
2019 wstats->discard.retries +=
2020 le16_to_cpu(tallies.TxRetryLimitExceeded);
2021 /* wstats->miss.beacon - no match */
2023 break;
2024 case HERMES_INQ_LINKSTATUS: {
2025 struct hermes_linkstatus linkstatus;
2026 u16 newstatus;
2027 int connected;
2029 if (priv->iw_mode == IW_MODE_MONITOR)
2030 break;
2032 if (len != sizeof(linkstatus)) {
2033 printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
2034 dev->name, len);
2035 break;
2038 err = hw->ops->bap_pread(hw, IRQ_BAP, &linkstatus, len,
2039 infofid, sizeof(info));
2040 if (err)
2041 break;
2042 newstatus = le16_to_cpu(linkstatus.linkstatus);
2044 /* Symbol firmware uses "out of range" to signal that
2045 * the hostscan frame can be requested. */
2046 if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
2047 priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
2048 priv->has_hostscan && priv->scan_inprogress) {
2049 hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
2050 break;
2053 connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
2054 || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
2055 || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
2057 if (connected)
2058 netif_carrier_on(dev);
2059 else if (!ignore_disconnect)
2060 netif_carrier_off(dev);
2062 if (newstatus != priv->last_linkstatus) {
2063 priv->last_linkstatus = newstatus;
2064 print_linkstatus(dev, newstatus);
2065 /* The info frame contains only one word which is the
2066 * status (see hermes.h). The status is pretty boring
2067 * in itself, that's why we export the new BSSID...
2068 * Jean II */
2069 schedule_work(&priv->wevent_work);
2072 break;
2073 case HERMES_INQ_SCAN:
2074 if (!priv->scan_inprogress && priv->bssid_fixed &&
2075 priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
2076 schedule_work(&priv->join_work);
2077 break;
2079 /* fall through */
2080 case HERMES_INQ_HOSTSCAN:
2081 case HERMES_INQ_HOSTSCAN_SYMBOL: {
2082 /* Result of a scanning. Contains information about
2083 * cells in the vicinity - Jean II */
2084 union iwreq_data wrqu;
2085 unsigned char *buf;
2087 /* Scan is no longer in progress */
2088 priv->scan_inprogress = 0;
2090 /* Sanity check */
2091 if (len > 4096) {
2092 printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
2093 dev->name, len);
2094 break;
2097 /* Allocate buffer for results */
2098 buf = kmalloc(len, GFP_ATOMIC);
2099 if (buf == NULL)
2100 /* No memory, so can't printk()... */
2101 break;
2103 /* Read scan data */
2104 err = hw->ops->bap_pread(hw, IRQ_BAP, (void *) buf, len,
2105 infofid, sizeof(info));
2106 if (err) {
2107 kfree(buf);
2108 break;
2111 #ifdef ORINOCO_DEBUG
2113 int i;
2114 printk(KERN_DEBUG "Scan result [%02X", buf[0]);
2115 for(i = 1; i < (len * 2); i++)
2116 printk(":%02X", buf[i]);
2117 printk("]\n");
2119 #endif /* ORINOCO_DEBUG */
2121 if (orinoco_process_scan_results(dev, buf, len) == 0) {
2122 /* Send an empty event to user space.
2123 * We don't send the received data on the event because
2124 * it would require us to do complex transcoding, and
2125 * we want to minimise the work done in the irq handler
2126 * Use a request to extract the data - Jean II */
2127 wrqu.data.length = 0;
2128 wrqu.data.flags = 0;
2129 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
2131 kfree(buf);
2133 break;
2134 case HERMES_INQ_CHANNELINFO:
2136 struct agere_ext_scan_info *bss;
2138 if (!priv->scan_inprogress) {
2139 printk(KERN_DEBUG "%s: Got chaninfo without scan, "
2140 "len=%d\n", dev->name, len);
2141 break;
2144 /* An empty result indicates that the scan is complete */
2145 if (len == 0) {
2146 union iwreq_data wrqu;
2148 /* Scan is no longer in progress */
2149 priv->scan_inprogress = 0;
2151 wrqu.data.length = 0;
2152 wrqu.data.flags = 0;
2153 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
2154 break;
2157 /* Sanity check */
2158 else if (len > sizeof(*bss)) {
2159 printk(KERN_WARNING
2160 "%s: Ext scan results too large (%d bytes). "
2161 "Truncating results to %zd bytes.\n",
2162 dev->name, len, sizeof(*bss));
2163 len = sizeof(*bss);
2164 } else if (len < (offsetof(struct agere_ext_scan_info,
2165 data) + 2)) {
2166 /* Drop this result now so we don't have to
2167 * keep checking later */
2168 printk(KERN_WARNING
2169 "%s: Ext scan results too short (%d bytes)\n",
2170 dev->name, len);
2171 break;
2174 bss = kmalloc(sizeof(*bss), GFP_ATOMIC);
2175 if (bss == NULL)
2176 break;
2178 /* Read scan data */
2179 err = hw->ops->bap_pread(hw, IRQ_BAP, (void *) bss, len,
2180 infofid, sizeof(info));
2181 if (err) {
2182 kfree(bss);
2183 break;
2186 orinoco_add_ext_scan_result(priv, bss);
2188 kfree(bss);
2189 break;
2191 case HERMES_INQ_SEC_STAT_AGERE:
2192 /* Security status (Agere specific) */
2193 /* Ignore this frame for now */
2194 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
2195 break;
2196 /* fall through */
2197 default:
2198 printk(KERN_DEBUG "%s: Unknown information frame received: "
2199 "type 0x%04x, length %d\n", dev->name, type, len);
2200 /* We don't actually do anything about it */
2201 break;
2205 static void __orinoco_ev_infdrop(struct net_device *dev, hermes_t *hw)
2207 if (net_ratelimit())
2208 printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
2211 /********************************************************************/
2212 /* Internal hardware control routines */
2213 /********************************************************************/
2215 int __orinoco_up(struct net_device *dev)
2217 struct orinoco_private *priv = netdev_priv(dev);
2218 struct hermes *hw = &priv->hw;
2219 int err;
2221 netif_carrier_off(dev); /* just to make sure */
2223 err = __orinoco_program_rids(dev);
2224 if (err) {
2225 printk(KERN_ERR "%s: Error %d configuring card\n",
2226 dev->name, err);
2227 return err;
2230 /* Fire things up again */
2231 hermes_set_irqmask(hw, ORINOCO_INTEN);
2232 err = hermes_enable_port(hw, 0);
2233 if (err) {
2234 printk(KERN_ERR "%s: Error %d enabling MAC port\n",
2235 dev->name, err);
2236 return err;
2239 netif_start_queue(dev);
2241 return 0;
2244 int __orinoco_down(struct net_device *dev)
2246 struct orinoco_private *priv = netdev_priv(dev);
2247 struct hermes *hw = &priv->hw;
2248 int err;
2250 netif_stop_queue(dev);
2252 if (! priv->hw_unavailable) {
2253 if (! priv->broken_disableport) {
2254 err = hermes_disable_port(hw, 0);
2255 if (err) {
2256 /* Some firmwares (e.g. Intersil 1.3.x) seem
2257 * to have problems disabling the port, oh
2258 * well, too bad. */
2259 printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
2260 dev->name, err);
2261 priv->broken_disableport = 1;
2264 hermes_set_irqmask(hw, 0);
2265 hermes_write_regn(hw, EVACK, 0xffff);
2268 /* firmware will have to reassociate */
2269 netif_carrier_off(dev);
2270 priv->last_linkstatus = 0xffff;
2272 return 0;
2275 static int orinoco_allocate_fid(struct net_device *dev)
2277 struct orinoco_private *priv = netdev_priv(dev);
2278 struct hermes *hw = &priv->hw;
2279 int err;
2281 err = hw->ops->allocate(hw, priv->nicbuf_size, &priv->txfid);
2282 if (err == -EIO && priv->nicbuf_size > TX_NICBUF_SIZE_BUG) {
2283 /* Try workaround for old Symbol firmware bug */
2284 printk(KERN_WARNING "%s: firmware ALLOC bug detected "
2285 "(old Symbol firmware?). Trying to work around... ",
2286 dev->name);
2288 priv->nicbuf_size = TX_NICBUF_SIZE_BUG;
2289 err = hw->ops->allocate(hw, priv->nicbuf_size, &priv->txfid);
2290 if (err)
2291 printk("failed!\n");
2292 else
2293 printk("ok.\n");
2296 return err;
2299 int orinoco_reinit_firmware(struct net_device *dev)
2301 struct orinoco_private *priv = netdev_priv(dev);
2302 struct hermes *hw = &priv->hw;
2303 int err;
2305 err = hw->ops->init(hw);
2306 if (!err)
2307 err = orinoco_allocate_fid(dev);
2309 return err;
2312 static int __orinoco_hw_set_bitrate(struct orinoco_private *priv)
2314 hermes_t *hw = &priv->hw;
2315 int err = 0;
2317 if (priv->bitratemode >= BITRATE_TABLE_SIZE) {
2318 printk(KERN_ERR "%s: BUG: Invalid bitrate mode %d\n",
2319 priv->ndev->name, priv->bitratemode);
2320 return -EINVAL;
2323 switch (priv->firmware_type) {
2324 case FIRMWARE_TYPE_AGERE:
2325 err = hermes_write_wordrec(hw, USER_BAP,
2326 HERMES_RID_CNFTXRATECONTROL,
2327 bitrate_table[priv->bitratemode].agere_txratectrl);
2328 break;
2329 case FIRMWARE_TYPE_INTERSIL:
2330 case FIRMWARE_TYPE_SYMBOL:
2331 err = hermes_write_wordrec(hw, USER_BAP,
2332 HERMES_RID_CNFTXRATECONTROL,
2333 bitrate_table[priv->bitratemode].intersil_txratectrl);
2334 break;
2335 default:
2336 BUG();
2339 return err;
2342 /* Set fixed AP address */
2343 static int __orinoco_hw_set_wap(struct orinoco_private *priv)
2345 int roaming_flag;
2346 int err = 0;
2347 hermes_t *hw = &priv->hw;
2349 switch (priv->firmware_type) {
2350 case FIRMWARE_TYPE_AGERE:
2351 /* not supported */
2352 break;
2353 case FIRMWARE_TYPE_INTERSIL:
2354 if (priv->bssid_fixed)
2355 roaming_flag = 2;
2356 else
2357 roaming_flag = 1;
2359 err = hermes_write_wordrec(hw, USER_BAP,
2360 HERMES_RID_CNFROAMINGMODE,
2361 roaming_flag);
2362 break;
2363 case FIRMWARE_TYPE_SYMBOL:
2364 err = HERMES_WRITE_RECORD(hw, USER_BAP,
2365 HERMES_RID_CNFMANDATORYBSSID_SYMBOL,
2366 &priv->desired_bssid);
2367 break;
2369 return err;
2372 /* Change the WEP keys and/or the current keys. Can be called
2373 * either from __orinoco_hw_setup_enc() or directly from
2374 * orinoco_ioctl_setiwencode(). In the later case the association
2375 * with the AP is not broken (if the firmware can handle it),
2376 * which is needed for 802.1x implementations. */
2377 static int __orinoco_hw_setup_wepkeys(struct orinoco_private *priv)
2379 hermes_t *hw = &priv->hw;
2380 int err = 0;
2382 switch (priv->firmware_type) {
2383 case FIRMWARE_TYPE_AGERE:
2384 err = HERMES_WRITE_RECORD(hw, USER_BAP,
2385 HERMES_RID_CNFWEPKEYS_AGERE,
2386 &priv->keys);
2387 if (err)
2388 return err;
2389 err = hermes_write_wordrec(hw, USER_BAP,
2390 HERMES_RID_CNFTXKEY_AGERE,
2391 priv->tx_key);
2392 if (err)
2393 return err;
2394 break;
2395 case FIRMWARE_TYPE_INTERSIL:
2396 case FIRMWARE_TYPE_SYMBOL:
2398 int keylen;
2399 int i;
2401 /* Force uniform key length to work around firmware bugs */
2402 keylen = le16_to_cpu(priv->keys[priv->tx_key].len);
2404 if (keylen > LARGE_KEY_SIZE) {
2405 printk(KERN_ERR "%s: BUG: Key %d has oversize length %d.\n",
2406 priv->ndev->name, priv->tx_key, keylen);
2407 return -E2BIG;
2410 /* Write all 4 keys */
2411 for(i = 0; i < ORINOCO_MAX_KEYS; i++) {
2412 err = hw->ops->write_ltv(hw, USER_BAP,
2413 HERMES_RID_CNFDEFAULTKEY0 + i,
2414 HERMES_BYTES_TO_RECLEN(keylen),
2415 priv->keys[i].data);
2416 if (err)
2417 return err;
2420 /* Write the index of the key used in transmission */
2421 err = hermes_write_wordrec(hw, USER_BAP,
2422 HERMES_RID_CNFWEPDEFAULTKEYID,
2423 priv->tx_key);
2424 if (err)
2425 return err;
2427 break;
2430 return 0;
2433 static int __orinoco_hw_setup_enc(struct orinoco_private *priv)
2435 hermes_t *hw = &priv->hw;
2436 int err = 0;
2437 int master_wep_flag;
2438 int auth_flag;
2439 int enc_flag;
2441 /* Setup WEP keys for WEP and WPA */
2442 if (priv->encode_alg)
2443 __orinoco_hw_setup_wepkeys(priv);
2445 if (priv->wep_restrict)
2446 auth_flag = HERMES_AUTH_SHARED_KEY;
2447 else
2448 auth_flag = HERMES_AUTH_OPEN;
2450 if (priv->wpa_enabled)
2451 enc_flag = 2;
2452 else if (priv->encode_alg == IW_ENCODE_ALG_WEP)
2453 enc_flag = 1;
2454 else
2455 enc_flag = 0;
2457 switch (priv->firmware_type) {
2458 case FIRMWARE_TYPE_AGERE: /* Agere style WEP */
2459 if (priv->encode_alg == IW_ENCODE_ALG_WEP) {
2460 /* Enable the shared-key authentication. */
2461 err = hermes_write_wordrec(hw, USER_BAP,
2462 HERMES_RID_CNFAUTHENTICATION_AGERE,
2463 auth_flag);
2465 err = hermes_write_wordrec(hw, USER_BAP,
2466 HERMES_RID_CNFWEPENABLED_AGERE,
2467 enc_flag);
2468 if (err)
2469 return err;
2471 if (priv->has_wpa) {
2472 /* Set WPA key management */
2473 err = hermes_write_wordrec(hw, USER_BAP,
2474 HERMES_RID_CNFSETWPAAUTHMGMTSUITE_AGERE,
2475 priv->key_mgmt);
2476 if (err)
2477 return err;
2480 break;
2482 case FIRMWARE_TYPE_INTERSIL: /* Intersil style WEP */
2483 case FIRMWARE_TYPE_SYMBOL: /* Symbol style WEP */
2484 if (priv->encode_alg == IW_ENCODE_ALG_WEP) {
2485 if (priv->wep_restrict ||
2486 (priv->firmware_type == FIRMWARE_TYPE_SYMBOL))
2487 master_wep_flag = HERMES_WEP_PRIVACY_INVOKED |
2488 HERMES_WEP_EXCL_UNENCRYPTED;
2489 else
2490 master_wep_flag = HERMES_WEP_PRIVACY_INVOKED;
2492 err = hermes_write_wordrec(hw, USER_BAP,
2493 HERMES_RID_CNFAUTHENTICATION,
2494 auth_flag);
2495 if (err)
2496 return err;
2497 } else
2498 master_wep_flag = 0;
2500 if (priv->iw_mode == IW_MODE_MONITOR)
2501 master_wep_flag |= HERMES_WEP_HOST_DECRYPT;
2503 /* Master WEP setting : on/off */
2504 err = hermes_write_wordrec(hw, USER_BAP,
2505 HERMES_RID_CNFWEPFLAGS_INTERSIL,
2506 master_wep_flag);
2507 if (err)
2508 return err;
2510 break;
2513 return 0;
2516 /* key must be 32 bytes, including the tx and rx MIC keys.
2517 * rsc must be 8 bytes
2518 * tsc must be 8 bytes or NULL
2520 static int __orinoco_hw_set_tkip_key(hermes_t *hw, int key_idx, int set_tx,
2521 u8 *key, u8 *rsc, u8 *tsc)
2523 struct {
2524 __le16 idx;
2525 u8 rsc[IW_ENCODE_SEQ_MAX_SIZE];
2526 u8 key[TKIP_KEYLEN];
2527 u8 tx_mic[MIC_KEYLEN];
2528 u8 rx_mic[MIC_KEYLEN];
2529 u8 tsc[IW_ENCODE_SEQ_MAX_SIZE];
2530 } __attribute__ ((packed)) buf;
2531 int ret;
2532 int err;
2533 int k;
2534 u16 xmitting;
2536 key_idx &= 0x3;
2538 if (set_tx)
2539 key_idx |= 0x8000;
2541 buf.idx = cpu_to_le16(key_idx);
2542 memcpy(buf.key, key,
2543 sizeof(buf.key) + sizeof(buf.tx_mic) + sizeof(buf.rx_mic));
2545 if (rsc == NULL)
2546 memset(buf.rsc, 0, sizeof(buf.rsc));
2547 else
2548 memcpy(buf.rsc, rsc, sizeof(buf.rsc));
2550 if (tsc == NULL) {
2551 memset(buf.tsc, 0, sizeof(buf.tsc));
2552 buf.tsc[4] = 0x10;
2553 } else {
2554 memcpy(buf.tsc, tsc, sizeof(buf.tsc));
2557 /* Wait upto 100ms for tx queue to empty */
2558 k = 100;
2559 do {
2560 k--;
2561 udelay(1000);
2562 ret = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_TXQUEUEEMPTY,
2563 &xmitting);
2564 if (ret)
2565 break;
2566 } while ((k > 0) && xmitting);
2568 if (k == 0)
2569 ret = -ETIMEDOUT;
2571 err = HERMES_WRITE_RECORD(hw, USER_BAP,
2572 HERMES_RID_CNFADDDEFAULTTKIPKEY_AGERE,
2573 &buf);
2575 return ret ? ret : err;
2578 static int orinoco_clear_tkip_key(struct orinoco_private *priv,
2579 int key_idx)
2581 hermes_t *hw = &priv->hw;
2582 int err;
2584 memset(&priv->tkip_key[key_idx], 0, sizeof(priv->tkip_key[key_idx]));
2585 err = hermes_write_wordrec(hw, USER_BAP,
2586 HERMES_RID_CNFREMDEFAULTTKIPKEY_AGERE,
2587 key_idx);
2588 if (err)
2589 printk(KERN_WARNING "%s: Error %d clearing TKIP key %d\n",
2590 priv->ndev->name, err, key_idx);
2591 return err;
2594 static int __orinoco_program_rids(struct net_device *dev)
2596 struct orinoco_private *priv = netdev_priv(dev);
2597 hermes_t *hw = &priv->hw;
2598 int err;
2599 struct hermes_idstring idbuf;
2601 /* Set the MAC address */
2602 err = hw->ops->write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
2603 HERMES_BYTES_TO_RECLEN(ETH_ALEN), dev->dev_addr);
2604 if (err) {
2605 printk(KERN_ERR "%s: Error %d setting MAC address\n",
2606 dev->name, err);
2607 return err;
2610 /* Set up the link mode */
2611 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFPORTTYPE,
2612 priv->port_type);
2613 if (err) {
2614 printk(KERN_ERR "%s: Error %d setting port type\n",
2615 dev->name, err);
2616 return err;
2618 /* Set the channel/frequency */
2619 if (priv->channel != 0 && priv->iw_mode != IW_MODE_INFRA) {
2620 err = hermes_write_wordrec(hw, USER_BAP,
2621 HERMES_RID_CNFOWNCHANNEL,
2622 priv->channel);
2623 if (err) {
2624 printk(KERN_ERR "%s: Error %d setting channel %d\n",
2625 dev->name, err, priv->channel);
2626 return err;
2630 if (priv->has_ibss) {
2631 u16 createibss;
2633 if ((strlen(priv->desired_essid) == 0) && (priv->createibss)) {
2634 printk(KERN_WARNING "%s: This firmware requires an "
2635 "ESSID in IBSS-Ad-Hoc mode.\n", dev->name);
2636 /* With wvlan_cs, in this case, we would crash.
2637 * hopefully, this driver will behave better...
2638 * Jean II */
2639 createibss = 0;
2640 } else {
2641 createibss = priv->createibss;
2644 err = hermes_write_wordrec(hw, USER_BAP,
2645 HERMES_RID_CNFCREATEIBSS,
2646 createibss);
2647 if (err) {
2648 printk(KERN_ERR "%s: Error %d setting CREATEIBSS\n",
2649 dev->name, err);
2650 return err;
2654 /* Set the desired BSSID */
2655 err = __orinoco_hw_set_wap(priv);
2656 if (err) {
2657 printk(KERN_ERR "%s: Error %d setting AP address\n",
2658 dev->name, err);
2659 return err;
2661 /* Set the desired ESSID */
2662 idbuf.len = cpu_to_le16(strlen(priv->desired_essid));
2663 memcpy(&idbuf.val, priv->desired_essid, sizeof(idbuf.val));
2664 /* WinXP wants partner to configure OWNSSID even in IBSS mode. (jimc) */
2665 err = hw->ops->write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNSSID,
2666 HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
2667 &idbuf);
2668 if (err) {
2669 printk(KERN_ERR "%s: Error %d setting OWNSSID\n",
2670 dev->name, err);
2671 return err;
2673 err = hw->ops->write_ltv(hw, USER_BAP, HERMES_RID_CNFDESIREDSSID,
2674 HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
2675 &idbuf);
2676 if (err) {
2677 printk(KERN_ERR "%s: Error %d setting DESIREDSSID\n",
2678 dev->name, err);
2679 return err;
2682 /* Set the station name */
2683 idbuf.len = cpu_to_le16(strlen(priv->nick));
2684 memcpy(&idbuf.val, priv->nick, sizeof(idbuf.val));
2685 err = hw->ops->write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
2686 HERMES_BYTES_TO_RECLEN(strlen(priv->nick)+2),
2687 &idbuf);
2688 if (err) {
2689 printk(KERN_ERR "%s: Error %d setting nickname\n",
2690 dev->name, err);
2691 return err;
2694 /* Set AP density */
2695 if (priv->has_sensitivity) {
2696 err = hermes_write_wordrec(hw, USER_BAP,
2697 HERMES_RID_CNFSYSTEMSCALE,
2698 priv->ap_density);
2699 if (err) {
2700 printk(KERN_WARNING "%s: Error %d setting SYSTEMSCALE. "
2701 "Disabling sensitivity control\n",
2702 dev->name, err);
2704 priv->has_sensitivity = 0;
2708 /* Set RTS threshold */
2709 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
2710 priv->rts_thresh);
2711 if (err) {
2712 printk(KERN_ERR "%s: Error %d setting RTS threshold\n",
2713 dev->name, err);
2714 return err;
2717 /* Set fragmentation threshold or MWO robustness */
2718 if (priv->has_mwo)
2719 err = hermes_write_wordrec(hw, USER_BAP,
2720 HERMES_RID_CNFMWOROBUST_AGERE,
2721 priv->mwo_robust);
2722 else
2723 err = hermes_write_wordrec(hw, USER_BAP,
2724 HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
2725 priv->frag_thresh);
2726 if (err) {
2727 printk(KERN_ERR "%s: Error %d setting fragmentation\n",
2728 dev->name, err);
2729 return err;
2732 /* Set bitrate */
2733 err = __orinoco_hw_set_bitrate(priv);
2734 if (err) {
2735 printk(KERN_ERR "%s: Error %d setting bitrate\n",
2736 dev->name, err);
2737 return err;
2740 /* Set power management */
2741 if (priv->has_pm) {
2742 err = hermes_write_wordrec(hw, USER_BAP,
2743 HERMES_RID_CNFPMENABLED,
2744 priv->pm_on);
2745 if (err) {
2746 printk(KERN_ERR "%s: Error %d setting up PM\n",
2747 dev->name, err);
2748 return err;
2751 err = hermes_write_wordrec(hw, USER_BAP,
2752 HERMES_RID_CNFMULTICASTRECEIVE,
2753 priv->pm_mcast);
2754 if (err) {
2755 printk(KERN_ERR "%s: Error %d setting up PM\n",
2756 dev->name, err);
2757 return err;
2759 err = hermes_write_wordrec(hw, USER_BAP,
2760 HERMES_RID_CNFMAXSLEEPDURATION,
2761 priv->pm_period);
2762 if (err) {
2763 printk(KERN_ERR "%s: Error %d setting up PM\n",
2764 dev->name, err);
2765 return err;
2767 err = hermes_write_wordrec(hw, USER_BAP,
2768 HERMES_RID_CNFPMHOLDOVERDURATION,
2769 priv->pm_timeout);
2770 if (err) {
2771 printk(KERN_ERR "%s: Error %d setting up PM\n",
2772 dev->name, err);
2773 return err;
2777 /* Set preamble - only for Symbol so far... */
2778 if (priv->has_preamble) {
2779 err = hermes_write_wordrec(hw, USER_BAP,
2780 HERMES_RID_CNFPREAMBLE_SYMBOL,
2781 priv->preamble);
2782 if (err) {
2783 printk(KERN_ERR "%s: Error %d setting preamble\n",
2784 dev->name, err);
2785 return err;
2789 /* Set up encryption */
2790 if (priv->has_wep || priv->has_wpa) {
2791 err = __orinoco_hw_setup_enc(priv);
2792 if (err) {
2793 printk(KERN_ERR "%s: Error %d activating encryption\n",
2794 dev->name, err);
2795 return err;
2799 if (priv->iw_mode == IW_MODE_MONITOR) {
2800 /* Enable monitor mode */
2801 dev->type = ARPHRD_IEEE80211;
2802 err = hw->ops->cmd_wait(hw, HERMES_CMD_TEST |
2803 HERMES_TEST_MONITOR, 0, NULL);
2804 } else {
2805 /* Disable monitor mode */
2806 dev->type = ARPHRD_ETHER;
2807 err = hw->ops->cmd_wait(hw, HERMES_CMD_TEST |
2808 HERMES_TEST_STOP, 0, NULL);
2810 if (err)
2811 return err;
2813 /* Set promiscuity / multicast*/
2814 priv->promiscuous = 0;
2815 priv->mc_count = 0;
2817 /* FIXME: what about netif_tx_lock */
2818 __orinoco_set_multicast_list(dev);
2820 return 0;
2823 /* FIXME: return int? */
2824 static void
2825 __orinoco_set_multicast_list(struct net_device *dev)
2827 struct orinoco_private *priv = netdev_priv(dev);
2828 hermes_t *hw = &priv->hw;
2829 int err = 0;
2830 int promisc, mc_count;
2832 /* The Hermes doesn't seem to have an allmulti mode, so we go
2833 * into promiscuous mode and let the upper levels deal. */
2834 if ( (dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
2835 (dev->mc_count > MAX_MULTICAST(priv)) ) {
2836 promisc = 1;
2837 mc_count = 0;
2838 } else {
2839 promisc = 0;
2840 mc_count = dev->mc_count;
2843 if (promisc != priv->promiscuous) {
2844 err = hermes_write_wordrec(hw, USER_BAP,
2845 HERMES_RID_CNFPROMISCUOUSMODE,
2846 promisc);
2847 if (err) {
2848 printk(KERN_ERR "%s: Error %d setting PROMISCUOUSMODE to 1.\n",
2849 dev->name, err);
2850 } else
2851 priv->promiscuous = promisc;
2854 /* If we're not in promiscuous mode, then we need to set the
2855 * group address if either we want to multicast, or if we were
2856 * multicasting and want to stop */
2857 if (! promisc && (mc_count || priv->mc_count) ) {
2858 struct dev_mc_list *p = dev->mc_list;
2859 struct hermes_multicast mclist;
2860 int i;
2862 for (i = 0; i < mc_count; i++) {
2863 /* paranoia: is list shorter than mc_count? */
2864 BUG_ON(! p);
2865 /* paranoia: bad address size in list? */
2866 BUG_ON(p->dmi_addrlen != ETH_ALEN);
2868 memcpy(mclist.addr[i], p->dmi_addr, ETH_ALEN);
2869 p = p->next;
2872 if (p)
2873 printk(KERN_WARNING "%s: Multicast list is "
2874 "longer than mc_count\n", dev->name);
2876 err = hw->ops->write_ltv(hw, USER_BAP,
2877 HERMES_RID_CNFGROUPADDRESSES,
2878 HERMES_BYTES_TO_RECLEN(mc_count * ETH_ALEN),
2879 &mclist);
2880 if (err)
2881 printk(KERN_ERR "%s: Error %d setting multicast list.\n",
2882 dev->name, err);
2883 else
2884 priv->mc_count = mc_count;
2888 /* This must be called from user context, without locks held - use
2889 * schedule_work() */
2890 static void orinoco_reset(struct work_struct *work)
2892 struct orinoco_private *priv =
2893 container_of(work, struct orinoco_private, reset_work);
2894 struct net_device *dev = priv->ndev;
2895 struct hermes *hw = &priv->hw;
2896 int err;
2897 unsigned long flags;
2899 if (orinoco_lock(priv, &flags) != 0)
2900 /* When the hardware becomes available again, whatever
2901 * detects that is responsible for re-initializing
2902 * it. So no need for anything further */
2903 return;
2905 netif_stop_queue(dev);
2907 /* Shut off interrupts. Depending on what state the hardware
2908 * is in, this might not work, but we'll try anyway */
2909 hermes_set_irqmask(hw, 0);
2910 hermes_write_regn(hw, EVACK, 0xffff);
2912 priv->hw_unavailable++;
2913 priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
2914 netif_carrier_off(dev);
2916 orinoco_unlock(priv, &flags);
2918 /* Scanning support: Cleanup of driver struct */
2919 orinoco_clear_scan_results(priv, 0);
2920 priv->scan_inprogress = 0;
2922 if (priv->hard_reset) {
2923 err = (*priv->hard_reset)(priv);
2924 if (err) {
2925 printk(KERN_ERR "%s: orinoco_reset: Error %d "
2926 "performing hard reset\n", dev->name, err);
2927 goto disable;
2931 if (priv->do_fw_download) {
2932 err = orinoco_download(priv);
2933 if (err)
2934 priv->do_fw_download = 0;
2937 err = orinoco_reinit_firmware(dev);
2938 if (err) {
2939 printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
2940 dev->name, err);
2941 goto disable;
2944 spin_lock_irq(&priv->lock); /* This has to be called from user context */
2946 priv->hw_unavailable--;
2948 /* priv->open or priv->hw_unavailable might have changed while
2949 * we dropped the lock */
2950 if (priv->open && (! priv->hw_unavailable)) {
2951 err = __orinoco_up(dev);
2952 if (err) {
2953 printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
2954 dev->name, err);
2955 } else
2956 dev->trans_start = jiffies;
2959 spin_unlock_irq(&priv->lock);
2961 return;
2962 disable:
2963 hermes_set_irqmask(hw, 0);
2964 netif_device_detach(dev);
2965 printk(KERN_ERR "%s: Device has been disabled!\n", dev->name);
2968 /********************************************************************/
2969 /* Interrupt handler */
2970 /********************************************************************/
2972 static void __orinoco_ev_tick(struct net_device *dev, hermes_t *hw)
2974 printk(KERN_DEBUG "%s: TICK\n", dev->name);
2977 static void __orinoco_ev_wterr(struct net_device *dev, hermes_t *hw)
2979 /* This seems to happen a fair bit under load, but ignoring it
2980 seems to work fine...*/
2981 printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
2982 dev->name);
2985 irqreturn_t orinoco_interrupt(int irq, void *dev_id)
2987 struct net_device *dev = dev_id;
2988 struct orinoco_private *priv = netdev_priv(dev);
2989 hermes_t *hw = &priv->hw;
2990 int count = MAX_IRQLOOPS_PER_IRQ;
2991 u16 evstat, events;
2992 /* These are used to detect a runaway interrupt situation */
2993 /* If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
2994 * we panic and shut down the hardware */
2995 static int last_irq_jiffy = 0; /* jiffies value the last time
2996 * we were called */
2997 static int loops_this_jiffy = 0;
2998 unsigned long flags;
3000 if (orinoco_lock(priv, &flags) != 0) {
3001 /* If hw is unavailable - we don't know if the irq was
3002 * for us or not */
3003 return IRQ_HANDLED;
3006 evstat = hermes_read_regn(hw, EVSTAT);
3007 events = evstat & hw->inten;
3008 if (! events) {
3009 orinoco_unlock(priv, &flags);
3010 return IRQ_NONE;
3013 if (jiffies != last_irq_jiffy)
3014 loops_this_jiffy = 0;
3015 last_irq_jiffy = jiffies;
3017 while (events && count--) {
3018 if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
3019 printk(KERN_WARNING "%s: IRQ handler is looping too "
3020 "much! Resetting.\n", dev->name);
3021 /* Disable interrupts for now */
3022 hermes_set_irqmask(hw, 0);
3023 schedule_work(&priv->reset_work);
3024 break;
3027 /* Check the card hasn't been removed */
3028 if (! hermes_present(hw)) {
3029 DEBUG(0, "orinoco_interrupt(): card removed\n");
3030 break;
3033 if (events & HERMES_EV_TICK)
3034 __orinoco_ev_tick(dev, hw);
3035 if (events & HERMES_EV_WTERR)
3036 __orinoco_ev_wterr(dev, hw);
3037 if (events & HERMES_EV_INFDROP)
3038 __orinoco_ev_infdrop(dev, hw);
3039 if (events & HERMES_EV_INFO)
3040 __orinoco_ev_info(dev, hw);
3041 if (events & HERMES_EV_RX)
3042 __orinoco_ev_rx(dev, hw);
3043 if (events & HERMES_EV_TXEXC)
3044 __orinoco_ev_txexc(dev, hw);
3045 if (events & HERMES_EV_TX)
3046 __orinoco_ev_tx(dev, hw);
3047 if (events & HERMES_EV_ALLOC)
3048 __orinoco_ev_alloc(dev, hw);
3050 hermes_write_regn(hw, EVACK, evstat);
3052 evstat = hermes_read_regn(hw, EVSTAT);
3053 events = evstat & hw->inten;
3056 orinoco_unlock(priv, &flags);
3057 return IRQ_HANDLED;
3060 /********************************************************************/
3061 /* Initialization */
3062 /********************************************************************/
3064 struct comp_id {
3065 u16 id, variant, major, minor;
3066 } __attribute__ ((packed));
3068 static inline fwtype_t determine_firmware_type(struct comp_id *nic_id)
3070 if (nic_id->id < 0x8000)
3071 return FIRMWARE_TYPE_AGERE;
3072 else if (nic_id->id == 0x8000 && nic_id->major == 0)
3073 return FIRMWARE_TYPE_SYMBOL;
3074 else
3075 return FIRMWARE_TYPE_INTERSIL;
3078 /* Set priv->firmware type, determine firmware properties */
3079 static int determine_firmware(struct net_device *dev)
3081 struct orinoco_private *priv = netdev_priv(dev);
3082 hermes_t *hw = &priv->hw;
3083 int err;
3084 struct comp_id nic_id, sta_id;
3085 unsigned int firmver;
3086 char tmp[SYMBOL_MAX_VER_LEN+1] __attribute__((aligned(2)));
3088 /* Get the hardware version */
3089 err = HERMES_READ_RECORD(hw, USER_BAP, HERMES_RID_NICID, &nic_id);
3090 if (err) {
3091 printk(KERN_ERR "%s: Cannot read hardware identity: error %d\n",
3092 dev->name, err);
3093 return err;
3096 le16_to_cpus(&nic_id.id);
3097 le16_to_cpus(&nic_id.variant);
3098 le16_to_cpus(&nic_id.major);
3099 le16_to_cpus(&nic_id.minor);
3100 printk(KERN_DEBUG "%s: Hardware identity %04x:%04x:%04x:%04x\n",
3101 dev->name, nic_id.id, nic_id.variant,
3102 nic_id.major, nic_id.minor);
3104 priv->firmware_type = determine_firmware_type(&nic_id);
3106 /* Get the firmware version */
3107 err = HERMES_READ_RECORD(hw, USER_BAP, HERMES_RID_STAID, &sta_id);
3108 if (err) {
3109 printk(KERN_ERR "%s: Cannot read station identity: error %d\n",
3110 dev->name, err);
3111 return err;
3114 le16_to_cpus(&sta_id.id);
3115 le16_to_cpus(&sta_id.variant);
3116 le16_to_cpus(&sta_id.major);
3117 le16_to_cpus(&sta_id.minor);
3118 printk(KERN_DEBUG "%s: Station identity %04x:%04x:%04x:%04x\n",
3119 dev->name, sta_id.id, sta_id.variant,
3120 sta_id.major, sta_id.minor);
3122 switch (sta_id.id) {
3123 case 0x15:
3124 printk(KERN_ERR "%s: Primary firmware is active\n",
3125 dev->name);
3126 return -ENODEV;
3127 case 0x14b:
3128 printk(KERN_ERR "%s: Tertiary firmware is active\n",
3129 dev->name);
3130 return -ENODEV;
3131 case 0x1f: /* Intersil, Agere, Symbol Spectrum24 */
3132 case 0x21: /* Symbol Spectrum24 Trilogy */
3133 break;
3134 default:
3135 printk(KERN_NOTICE "%s: Unknown station ID, please report\n",
3136 dev->name);
3137 break;
3140 /* Default capabilities */
3141 priv->has_sensitivity = 1;
3142 priv->has_mwo = 0;
3143 priv->has_preamble = 0;
3144 priv->has_port3 = 1;
3145 priv->has_ibss = 1;
3146 priv->has_wep = 0;
3147 priv->has_big_wep = 0;
3148 priv->has_alt_txcntl = 0;
3149 priv->has_ext_scan = 0;
3150 priv->has_wpa = 0;
3151 priv->do_fw_download = 0;
3153 /* Determine capabilities from the firmware version */
3154 switch (priv->firmware_type) {
3155 case FIRMWARE_TYPE_AGERE:
3156 /* Lucent Wavelan IEEE, Lucent Orinoco, Cabletron RoamAbout,
3157 ELSA, Melco, HP, IBM, Dell 1150, Compaq 110/210 */
3158 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
3159 "Lucent/Agere %d.%02d", sta_id.major, sta_id.minor);
3161 firmver = ((unsigned long)sta_id.major << 16) | sta_id.minor;
3163 priv->has_ibss = (firmver >= 0x60006);
3164 priv->has_wep = (firmver >= 0x40020);
3165 priv->has_big_wep = 1; /* FIXME: this is wrong - how do we tell
3166 Gold cards from the others? */
3167 priv->has_mwo = (firmver >= 0x60000);
3168 priv->has_pm = (firmver >= 0x40020); /* Don't work in 7.52 ? */
3169 priv->ibss_port = 1;
3170 priv->has_hostscan = (firmver >= 0x8000a);
3171 priv->do_fw_download = 1;
3172 priv->broken_monitor = (firmver >= 0x80000);
3173 priv->has_alt_txcntl = (firmver >= 0x90000); /* All 9.x ? */
3174 priv->has_ext_scan = (firmver >= 0x90000); /* All 9.x ? */
3175 priv->has_wpa = (firmver >= 0x9002a);
3176 /* Tested with Agere firmware :
3177 * 1.16 ; 4.08 ; 4.52 ; 6.04 ; 6.16 ; 7.28 => Jean II
3178 * Tested CableTron firmware : 4.32 => Anton */
3179 break;
3180 case FIRMWARE_TYPE_SYMBOL:
3181 /* Symbol , 3Com AirConnect, Intel, Ericsson WLAN */
3182 /* Intel MAC : 00:02:B3:* */
3183 /* 3Com MAC : 00:50:DA:* */
3184 memset(tmp, 0, sizeof(tmp));
3185 /* Get the Symbol firmware version */
3186 err = hw->ops->read_ltv(hw, USER_BAP,
3187 HERMES_RID_SECONDARYVERSION_SYMBOL,
3188 SYMBOL_MAX_VER_LEN, NULL, &tmp);
3189 if (err) {
3190 printk(KERN_WARNING
3191 "%s: Error %d reading Symbol firmware info. Wildly guessing capabilities...\n",
3192 dev->name, err);
3193 firmver = 0;
3194 tmp[0] = '\0';
3195 } else {
3196 /* The firmware revision is a string, the format is
3197 * something like : "V2.20-01".
3198 * Quick and dirty parsing... - Jean II
3200 firmver = ((tmp[1] - '0') << 16) | ((tmp[3] - '0') << 12)
3201 | ((tmp[4] - '0') << 8) | ((tmp[6] - '0') << 4)
3202 | (tmp[7] - '0');
3204 tmp[SYMBOL_MAX_VER_LEN] = '\0';
3207 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
3208 "Symbol %s", tmp);
3210 priv->has_ibss = (firmver >= 0x20000);
3211 priv->has_wep = (firmver >= 0x15012);
3212 priv->has_big_wep = (firmver >= 0x20000);
3213 priv->has_pm = (firmver >= 0x20000 && firmver < 0x22000) ||
3214 (firmver >= 0x29000 && firmver < 0x30000) ||
3215 firmver >= 0x31000;
3216 priv->has_preamble = (firmver >= 0x20000);
3217 priv->ibss_port = 4;
3219 /* Symbol firmware is found on various cards, but
3220 * there has been no attempt to check firmware
3221 * download on non-spectrum_cs based cards.
3223 * Given that the Agere firmware download works
3224 * differently, we should avoid doing a firmware
3225 * download with the Symbol algorithm on non-spectrum
3226 * cards.
3228 * For now we can identify a spectrum_cs based card
3229 * because it has a firmware reset function.
3231 priv->do_fw_download = (priv->stop_fw != NULL);
3233 priv->broken_disableport = (firmver == 0x25013) ||
3234 (firmver >= 0x30000 && firmver <= 0x31000);
3235 priv->has_hostscan = (firmver >= 0x31001) ||
3236 (firmver >= 0x29057 && firmver < 0x30000);
3237 /* Tested with Intel firmware : 0x20015 => Jean II */
3238 /* Tested with 3Com firmware : 0x15012 & 0x22001 => Jean II */
3239 break;
3240 case FIRMWARE_TYPE_INTERSIL:
3241 /* D-Link, Linksys, Adtron, ZoomAir, and many others...
3242 * Samsung, Compaq 100/200 and Proxim are slightly
3243 * different and less well tested */
3244 /* D-Link MAC : 00:40:05:* */
3245 /* Addtron MAC : 00:90:D1:* */
3246 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
3247 "Intersil %d.%d.%d", sta_id.major, sta_id.minor,
3248 sta_id.variant);
3250 firmver = ((unsigned long)sta_id.major << 16) |
3251 ((unsigned long)sta_id.minor << 8) | sta_id.variant;
3253 priv->has_ibss = (firmver >= 0x000700); /* FIXME */
3254 priv->has_big_wep = priv->has_wep = (firmver >= 0x000800);
3255 priv->has_pm = (firmver >= 0x000700);
3256 priv->has_hostscan = (firmver >= 0x010301);
3258 if (firmver >= 0x000800)
3259 priv->ibss_port = 0;
3260 else {
3261 printk(KERN_NOTICE "%s: Intersil firmware earlier "
3262 "than v0.8.x - several features not supported\n",
3263 dev->name);
3264 priv->ibss_port = 1;
3266 break;
3268 printk(KERN_DEBUG "%s: Firmware determined as %s\n", dev->name,
3269 priv->fw_name);
3271 return 0;
3274 static int orinoco_init(struct net_device *dev)
3276 struct orinoco_private *priv = netdev_priv(dev);
3277 hermes_t *hw = &priv->hw;
3278 int err = 0;
3279 struct hermes_idstring nickbuf;
3280 u16 reclen;
3281 int len;
3282 DECLARE_MAC_BUF(mac);
3284 /* No need to lock, the hw_unavailable flag is already set in
3285 * alloc_orinocodev() */
3286 priv->nicbuf_size = IEEE80211_FRAME_LEN + ETH_HLEN;
3288 /* Initialize the firmware */
3289 err = hw->ops->init(hw);
3290 if (err != 0) {
3291 printk(KERN_ERR "%s: failed to initialize firmware (err = %d)\n",
3292 dev->name, err);
3293 goto out;
3296 err = determine_firmware(dev);
3297 if (err != 0) {
3298 printk(KERN_ERR "%s: Incompatible firmware, aborting\n",
3299 dev->name);
3300 goto out;
3303 if (priv->do_fw_download) {
3304 err = orinoco_download(priv);
3305 if (err)
3306 priv->do_fw_download = 0;
3308 /* Check firmware version again */
3309 err = determine_firmware(dev);
3310 if (err != 0) {
3311 printk(KERN_ERR "%s: Incompatible firmware, aborting\n",
3312 dev->name);
3313 goto out;
3317 if (priv->has_port3)
3318 printk(KERN_DEBUG "%s: Ad-hoc demo mode supported\n", dev->name);
3319 if (priv->has_ibss)
3320 printk(KERN_DEBUG "%s: IEEE standard IBSS ad-hoc mode supported\n",
3321 dev->name);
3322 if (priv->has_wep) {
3323 printk(KERN_DEBUG "%s: WEP supported, ", dev->name);
3324 if (priv->has_big_wep)
3325 printk("104-bit key\n");
3326 else
3327 printk("40-bit key\n");
3329 if (priv->has_wpa) {
3330 printk(KERN_DEBUG "%s: WPA-PSK supported\n", dev->name);
3331 if (orinoco_mic_init(priv)) {
3332 printk(KERN_ERR "%s: Failed to setup MIC crypto "
3333 "algorithm. Disabling WPA support\n", dev->name);
3334 priv->has_wpa = 0;
3338 /* Now we have the firmware capabilities, allocate appropiate
3339 * sized scan buffers */
3340 if (orinoco_bss_data_allocate(priv))
3341 goto out;
3342 orinoco_bss_data_init(priv);
3344 /* Get the MAC address */
3345 err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
3346 ETH_ALEN, NULL, dev->dev_addr);
3347 if (err) {
3348 printk(KERN_WARNING "%s: failed to read MAC address!\n",
3349 dev->name);
3350 goto out;
3353 printk(KERN_DEBUG "%s: MAC address %s\n",
3354 dev->name, print_mac(mac, dev->dev_addr));
3356 /* Get the station name */
3357 err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
3358 sizeof(nickbuf), &reclen, &nickbuf);
3359 if (err) {
3360 printk(KERN_ERR "%s: failed to read station name\n",
3361 dev->name);
3362 goto out;
3364 if (nickbuf.len)
3365 len = min(IW_ESSID_MAX_SIZE, (int)le16_to_cpu(nickbuf.len));
3366 else
3367 len = min(IW_ESSID_MAX_SIZE, 2 * reclen);
3368 memcpy(priv->nick, &nickbuf.val, len);
3369 priv->nick[len] = '\0';
3371 printk(KERN_DEBUG "%s: Station name \"%s\"\n", dev->name, priv->nick);
3373 err = orinoco_allocate_fid(dev);
3374 if (err) {
3375 printk(KERN_ERR "%s: failed to allocate NIC buffer!\n",
3376 dev->name);
3377 goto out;
3380 /* Get allowed channels */
3381 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CHANNELLIST,
3382 &priv->channel_mask);
3383 if (err) {
3384 printk(KERN_ERR "%s: failed to read channel list!\n",
3385 dev->name);
3386 goto out;
3389 /* Get initial AP density */
3390 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFSYSTEMSCALE,
3391 &priv->ap_density);
3392 if (err || priv->ap_density < 1 || priv->ap_density > 3) {
3393 priv->has_sensitivity = 0;
3396 /* Get initial RTS threshold */
3397 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
3398 &priv->rts_thresh);
3399 if (err) {
3400 printk(KERN_ERR "%s: failed to read RTS threshold!\n",
3401 dev->name);
3402 goto out;
3405 /* Get initial fragmentation settings */
3406 if (priv->has_mwo)
3407 err = hermes_read_wordrec(hw, USER_BAP,
3408 HERMES_RID_CNFMWOROBUST_AGERE,
3409 &priv->mwo_robust);
3410 else
3411 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
3412 &priv->frag_thresh);
3413 if (err) {
3414 printk(KERN_ERR "%s: failed to read fragmentation settings!\n",
3415 dev->name);
3416 goto out;
3419 /* Power management setup */
3420 if (priv->has_pm) {
3421 priv->pm_on = 0;
3422 priv->pm_mcast = 1;
3423 err = hermes_read_wordrec(hw, USER_BAP,
3424 HERMES_RID_CNFMAXSLEEPDURATION,
3425 &priv->pm_period);
3426 if (err) {
3427 printk(KERN_ERR "%s: failed to read power management period!\n",
3428 dev->name);
3429 goto out;
3431 err = hermes_read_wordrec(hw, USER_BAP,
3432 HERMES_RID_CNFPMHOLDOVERDURATION,
3433 &priv->pm_timeout);
3434 if (err) {
3435 printk(KERN_ERR "%s: failed to read power management timeout!\n",
3436 dev->name);
3437 goto out;
3441 /* Preamble setup */
3442 if (priv->has_preamble) {
3443 err = hermes_read_wordrec(hw, USER_BAP,
3444 HERMES_RID_CNFPREAMBLE_SYMBOL,
3445 &priv->preamble);
3446 if (err)
3447 goto out;
3450 /* Set up the default configuration */
3451 priv->iw_mode = IW_MODE_INFRA;
3452 /* By default use IEEE/IBSS ad-hoc mode if we have it */
3453 priv->prefer_port3 = priv->has_port3 && (! priv->has_ibss);
3454 set_port_type(priv);
3455 priv->channel = 0; /* use firmware default */
3457 priv->promiscuous = 0;
3458 priv->encode_alg = IW_ENCODE_ALG_NONE;
3459 priv->tx_key = 0;
3460 priv->wpa_enabled = 0;
3461 priv->tkip_cm_active = 0;
3462 priv->key_mgmt = 0;
3463 priv->wpa_ie_len = 0;
3464 priv->wpa_ie = NULL;
3466 /* Make the hardware available, as long as it hasn't been
3467 * removed elsewhere (e.g. by PCMCIA hot unplug) */
3468 spin_lock_irq(&priv->lock);
3469 priv->hw_unavailable--;
3470 spin_unlock_irq(&priv->lock);
3472 printk(KERN_DEBUG "%s: ready\n", dev->name);
3474 out:
3475 return err;
3478 struct net_device
3479 *alloc_orinocodev(int sizeof_card,
3480 struct device *device,
3481 int (*hard_reset)(struct orinoco_private *),
3482 int (*stop_fw)(struct orinoco_private *, int))
3484 struct net_device *dev;
3485 struct orinoco_private *priv;
3487 dev = alloc_etherdev(sizeof(struct orinoco_private) + sizeof_card);
3488 if (! dev)
3489 return NULL;
3490 priv = netdev_priv(dev);
3491 priv->ndev = dev;
3492 if (sizeof_card)
3493 priv->card = (void *)((unsigned long)priv
3494 + sizeof(struct orinoco_private));
3495 else
3496 priv->card = NULL;
3497 priv->dev = device;
3499 /* Setup / override net_device fields */
3500 dev->init = orinoco_init;
3501 dev->hard_start_xmit = orinoco_xmit;
3502 dev->tx_timeout = orinoco_tx_timeout;
3503 dev->watchdog_timeo = HZ; /* 1 second timeout */
3504 dev->get_stats = orinoco_get_stats;
3505 dev->ethtool_ops = &orinoco_ethtool_ops;
3506 dev->wireless_handlers = (struct iw_handler_def *)&orinoco_handler_def;
3507 #ifdef WIRELESS_SPY
3508 priv->wireless_data.spy_data = &priv->spy_data;
3509 dev->wireless_data = &priv->wireless_data;
3510 #endif
3511 dev->change_mtu = orinoco_change_mtu;
3512 dev->set_multicast_list = orinoco_set_multicast_list;
3513 /* we use the default eth_mac_addr for setting the MAC addr */
3515 /* Reserve space in skb for the SNAP header */
3516 dev->hard_header_len += ENCAPS_OVERHEAD;
3518 /* Set up default callbacks */
3519 dev->open = orinoco_open;
3520 dev->stop = orinoco_stop;
3521 priv->hard_reset = hard_reset;
3522 priv->stop_fw = stop_fw;
3524 spin_lock_init(&priv->lock);
3525 priv->open = 0;
3526 priv->hw_unavailable = 1; /* orinoco_init() must clear this
3527 * before anything else touches the
3528 * hardware */
3529 INIT_WORK(&priv->reset_work, orinoco_reset);
3530 INIT_WORK(&priv->join_work, orinoco_join_ap);
3531 INIT_WORK(&priv->wevent_work, orinoco_send_wevents);
3533 INIT_LIST_HEAD(&priv->rx_list);
3534 tasklet_init(&priv->rx_tasklet, orinoco_rx_isr_tasklet,
3535 (unsigned long) dev);
3537 netif_carrier_off(dev);
3538 priv->last_linkstatus = 0xffff;
3540 return dev;
3543 void free_orinocodev(struct net_device *dev)
3545 struct orinoco_private *priv = netdev_priv(dev);
3547 /* No need to empty priv->rx_list: if the tasklet is scheduled
3548 * when we call tasklet_kill it will run one final time,
3549 * emptying the list */
3550 tasklet_kill(&priv->rx_tasklet);
3551 priv->wpa_ie_len = 0;
3552 kfree(priv->wpa_ie);
3553 orinoco_mic_free(priv);
3554 orinoco_bss_data_free(priv);
3555 free_netdev(dev);
3558 /********************************************************************/
3559 /* Wireless extensions */
3560 /********************************************************************/
3562 /* Return : < 0 -> error code ; >= 0 -> length */
3563 static int orinoco_hw_get_essid(struct orinoco_private *priv, int *active,
3564 char buf[IW_ESSID_MAX_SIZE+1])
3566 hermes_t *hw = &priv->hw;
3567 int err = 0;
3568 struct hermes_idstring essidbuf;
3569 char *p = (char *)(&essidbuf.val);
3570 int len;
3571 unsigned long flags;
3573 if (orinoco_lock(priv, &flags) != 0)
3574 return -EBUSY;
3576 if (strlen(priv->desired_essid) > 0) {
3577 /* We read the desired SSID from the hardware rather
3578 than from priv->desired_essid, just in case the
3579 firmware is allowed to change it on us. I'm not
3580 sure about this */
3581 /* My guess is that the OWNSSID should always be whatever
3582 * we set to the card, whereas CURRENT_SSID is the one that
3583 * may change... - Jean II */
3584 u16 rid;
3586 *active = 1;
3588 rid = (priv->port_type == 3) ? HERMES_RID_CNFOWNSSID :
3589 HERMES_RID_CNFDESIREDSSID;
3591 err = hw->ops->read_ltv(hw, USER_BAP, rid, sizeof(essidbuf),
3592 NULL, &essidbuf);
3593 if (err)
3594 goto fail_unlock;
3595 } else {
3596 *active = 0;
3598 err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CURRENTSSID,
3599 sizeof(essidbuf), NULL, &essidbuf);
3600 if (err)
3601 goto fail_unlock;
3604 len = le16_to_cpu(essidbuf.len);
3605 BUG_ON(len > IW_ESSID_MAX_SIZE);
3607 memset(buf, 0, IW_ESSID_MAX_SIZE);
3608 memcpy(buf, p, len);
3609 err = len;
3611 fail_unlock:
3612 orinoco_unlock(priv, &flags);
3614 return err;
3617 static long orinoco_hw_get_freq(struct orinoco_private *priv)
3620 hermes_t *hw = &priv->hw;
3621 int err = 0;
3622 u16 channel;
3623 long freq = 0;
3624 unsigned long flags;
3626 if (orinoco_lock(priv, &flags) != 0)
3627 return -EBUSY;
3629 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CURRENTCHANNEL, &channel);
3630 if (err)
3631 goto out;
3633 /* Intersil firmware 1.3.5 returns 0 when the interface is down */
3634 if (channel == 0) {
3635 err = -EBUSY;
3636 goto out;
3639 if ( (channel < 1) || (channel > NUM_CHANNELS) ) {
3640 printk(KERN_WARNING "%s: Channel out of range (%d)!\n",
3641 priv->ndev->name, channel);
3642 err = -EBUSY;
3643 goto out;
3646 freq = channel_frequency[channel-1] * 100000;
3648 out:
3649 orinoco_unlock(priv, &flags);
3651 if (err > 0)
3652 err = -EBUSY;
3653 return err ? err : freq;
3656 static int orinoco_hw_get_bitratelist(struct orinoco_private *priv,
3657 int *numrates, s32 *rates, int max)
3659 hermes_t *hw = &priv->hw;
3660 struct hermes_idstring list;
3661 unsigned char *p = (unsigned char *)&list.val;
3662 int err = 0;
3663 int num;
3664 int i;
3665 unsigned long flags;
3667 if (orinoco_lock(priv, &flags) != 0)
3668 return -EBUSY;
3670 err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_SUPPORTEDDATARATES,
3671 sizeof(list), NULL, &list);
3672 orinoco_unlock(priv, &flags);
3674 if (err)
3675 return err;
3677 num = le16_to_cpu(list.len);
3678 *numrates = num;
3679 num = min(num, max);
3681 for (i = 0; i < num; i++) {
3682 rates[i] = (p[i] & 0x7f) * 500000; /* convert to bps */
3685 return 0;
3688 static int orinoco_ioctl_getname(struct net_device *dev,
3689 struct iw_request_info *info,
3690 char *name,
3691 char *extra)
3693 struct orinoco_private *priv = netdev_priv(dev);
3694 int numrates;
3695 int err;
3697 err = orinoco_hw_get_bitratelist(priv, &numrates, NULL, 0);
3699 if (!err && (numrates > 2))
3700 strcpy(name, "IEEE 802.11b");
3701 else
3702 strcpy(name, "IEEE 802.11-DS");
3704 return 0;
3707 static int orinoco_ioctl_setwap(struct net_device *dev,
3708 struct iw_request_info *info,
3709 struct sockaddr *ap_addr,
3710 char *extra)
3712 struct orinoco_private *priv = netdev_priv(dev);
3713 int err = -EINPROGRESS; /* Call commit handler */
3714 unsigned long flags;
3715 static const u8 off_addr[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3716 static const u8 any_addr[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3718 if (orinoco_lock(priv, &flags) != 0)
3719 return -EBUSY;
3721 /* Enable automatic roaming - no sanity checks are needed */
3722 if (memcmp(&ap_addr->sa_data, off_addr, ETH_ALEN) == 0 ||
3723 memcmp(&ap_addr->sa_data, any_addr, ETH_ALEN) == 0) {
3724 priv->bssid_fixed = 0;
3725 memset(priv->desired_bssid, 0, ETH_ALEN);
3727 /* "off" means keep existing connection */
3728 if (ap_addr->sa_data[0] == 0) {
3729 __orinoco_hw_set_wap(priv);
3730 err = 0;
3732 goto out;
3735 if (priv->firmware_type == FIRMWARE_TYPE_AGERE) {
3736 printk(KERN_WARNING "%s: Lucent/Agere firmware doesn't "
3737 "support manual roaming\n",
3738 dev->name);
3739 err = -EOPNOTSUPP;
3740 goto out;
3743 if (priv->iw_mode != IW_MODE_INFRA) {
3744 printk(KERN_WARNING "%s: Manual roaming supported only in "
3745 "managed mode\n", dev->name);
3746 err = -EOPNOTSUPP;
3747 goto out;
3750 /* Intersil firmware hangs without Desired ESSID */
3751 if (priv->firmware_type == FIRMWARE_TYPE_INTERSIL &&
3752 strlen(priv->desired_essid) == 0) {
3753 printk(KERN_WARNING "%s: Desired ESSID must be set for "
3754 "manual roaming\n", dev->name);
3755 err = -EOPNOTSUPP;
3756 goto out;
3759 /* Finally, enable manual roaming */
3760 priv->bssid_fixed = 1;
3761 memcpy(priv->desired_bssid, &ap_addr->sa_data, ETH_ALEN);
3763 out:
3764 orinoco_unlock(priv, &flags);
3765 return err;
3768 static int orinoco_ioctl_getwap(struct net_device *dev,
3769 struct iw_request_info *info,
3770 struct sockaddr *ap_addr,
3771 char *extra)
3773 struct orinoco_private *priv = netdev_priv(dev);
3775 hermes_t *hw = &priv->hw;
3776 int err = 0;
3777 unsigned long flags;
3779 if (orinoco_lock(priv, &flags) != 0)
3780 return -EBUSY;
3782 ap_addr->sa_family = ARPHRD_ETHER;
3783 err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
3784 ETH_ALEN, NULL, ap_addr->sa_data);
3786 orinoco_unlock(priv, &flags);
3788 return err;
3791 static int orinoco_ioctl_setmode(struct net_device *dev,
3792 struct iw_request_info *info,
3793 u32 *mode,
3794 char *extra)
3796 struct orinoco_private *priv = netdev_priv(dev);
3797 int err = -EINPROGRESS; /* Call commit handler */
3798 unsigned long flags;
3800 if (priv->iw_mode == *mode)
3801 return 0;
3803 if (orinoco_lock(priv, &flags) != 0)
3804 return -EBUSY;
3806 switch (*mode) {
3807 case IW_MODE_ADHOC:
3808 if (!priv->has_ibss && !priv->has_port3)
3809 err = -EOPNOTSUPP;
3810 break;
3812 case IW_MODE_INFRA:
3813 break;
3815 case IW_MODE_MONITOR:
3816 if (priv->broken_monitor && !force_monitor) {
3817 printk(KERN_WARNING "%s: Monitor mode support is "
3818 "buggy in this firmware, not enabling\n",
3819 dev->name);
3820 err = -EOPNOTSUPP;
3822 break;
3824 default:
3825 err = -EOPNOTSUPP;
3826 break;
3829 if (err == -EINPROGRESS) {
3830 priv->iw_mode = *mode;
3831 set_port_type(priv);
3834 orinoco_unlock(priv, &flags);
3836 return err;
3839 static int orinoco_ioctl_getmode(struct net_device *dev,
3840 struct iw_request_info *info,
3841 u32 *mode,
3842 char *extra)
3844 struct orinoco_private *priv = netdev_priv(dev);
3846 *mode = priv->iw_mode;
3847 return 0;
3850 static int orinoco_ioctl_getiwrange(struct net_device *dev,
3851 struct iw_request_info *info,
3852 struct iw_point *rrq,
3853 char *extra)
3855 struct orinoco_private *priv = netdev_priv(dev);
3856 int err = 0;
3857 struct iw_range *range = (struct iw_range *) extra;
3858 int numrates;
3859 int i, k;
3861 rrq->length = sizeof(struct iw_range);
3862 memset(range, 0, sizeof(struct iw_range));
3864 range->we_version_compiled = WIRELESS_EXT;
3865 range->we_version_source = 22;
3867 /* Set available channels/frequencies */
3868 range->num_channels = NUM_CHANNELS;
3869 k = 0;
3870 for (i = 0; i < NUM_CHANNELS; i++) {
3871 if (priv->channel_mask & (1 << i)) {
3872 range->freq[k].i = i + 1;
3873 range->freq[k].m = channel_frequency[i] * 100000;
3874 range->freq[k].e = 1;
3875 k++;
3878 if (k >= IW_MAX_FREQUENCIES)
3879 break;
3881 range->num_frequency = k;
3882 range->sensitivity = 3;
3884 if (priv->has_wep) {
3885 range->max_encoding_tokens = ORINOCO_MAX_KEYS;
3886 range->encoding_size[0] = SMALL_KEY_SIZE;
3887 range->num_encoding_sizes = 1;
3889 if (priv->has_big_wep) {
3890 range->encoding_size[1] = LARGE_KEY_SIZE;
3891 range->num_encoding_sizes = 2;
3895 if (priv->has_wpa)
3896 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_CIPHER_TKIP;
3898 if ((priv->iw_mode == IW_MODE_ADHOC) && (!SPY_NUMBER(priv))){
3899 /* Quality stats meaningless in ad-hoc mode */
3900 } else {
3901 range->max_qual.qual = 0x8b - 0x2f;
3902 range->max_qual.level = 0x2f - 0x95 - 1;
3903 range->max_qual.noise = 0x2f - 0x95 - 1;
3904 /* Need to get better values */
3905 range->avg_qual.qual = 0x24;
3906 range->avg_qual.level = 0xC2;
3907 range->avg_qual.noise = 0x9E;
3910 err = orinoco_hw_get_bitratelist(priv, &numrates,
3911 range->bitrate, IW_MAX_BITRATES);
3912 if (err)
3913 return err;
3914 range->num_bitrates = numrates;
3916 /* Set an indication of the max TCP throughput in bit/s that we can
3917 * expect using this interface. May be use for QoS stuff...
3918 * Jean II */
3919 if (numrates > 2)
3920 range->throughput = 5 * 1000 * 1000; /* ~5 Mb/s */
3921 else
3922 range->throughput = 1.5 * 1000 * 1000; /* ~1.5 Mb/s */
3924 range->min_rts = 0;
3925 range->max_rts = 2347;
3926 range->min_frag = 256;
3927 range->max_frag = 2346;
3929 range->min_pmp = 0;
3930 range->max_pmp = 65535000;
3931 range->min_pmt = 0;
3932 range->max_pmt = 65535 * 1000; /* ??? */
3933 range->pmp_flags = IW_POWER_PERIOD;
3934 range->pmt_flags = IW_POWER_TIMEOUT;
3935 range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_UNICAST_R;
3937 range->retry_capa = IW_RETRY_LIMIT | IW_RETRY_LIFETIME;
3938 range->retry_flags = IW_RETRY_LIMIT;
3939 range->r_time_flags = IW_RETRY_LIFETIME;
3940 range->min_retry = 0;
3941 range->max_retry = 65535; /* ??? */
3942 range->min_r_time = 0;
3943 range->max_r_time = 65535 * 1000; /* ??? */
3945 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
3946 range->scan_capa = IW_SCAN_CAPA_ESSID;
3947 else
3948 range->scan_capa = IW_SCAN_CAPA_NONE;
3950 /* Event capability (kernel) */
3951 IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
3952 /* Event capability (driver) */
3953 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWTHRSPY);
3954 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
3955 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
3956 IW_EVENT_CAPA_SET(range->event_capa, IWEVTXDROP);
3958 return 0;
3961 static int orinoco_ioctl_setiwencode(struct net_device *dev,
3962 struct iw_request_info *info,
3963 struct iw_point *erq,
3964 char *keybuf)
3966 struct orinoco_private *priv = netdev_priv(dev);
3967 int index = (erq->flags & IW_ENCODE_INDEX) - 1;
3968 int setindex = priv->tx_key;
3969 int encode_alg = priv->encode_alg;
3970 int restricted = priv->wep_restrict;
3971 u16 xlen = 0;
3972 int err = -EINPROGRESS; /* Call commit handler */
3973 unsigned long flags;
3975 if (! priv->has_wep)
3976 return -EOPNOTSUPP;
3978 if (erq->pointer) {
3979 /* We actually have a key to set - check its length */
3980 if (erq->length > LARGE_KEY_SIZE)
3981 return -E2BIG;
3983 if ( (erq->length > SMALL_KEY_SIZE) && !priv->has_big_wep )
3984 return -E2BIG;
3987 if (orinoco_lock(priv, &flags) != 0)
3988 return -EBUSY;
3990 /* Clear any TKIP key we have */
3991 if ((priv->has_wpa) && (priv->encode_alg == IW_ENCODE_ALG_TKIP))
3992 (void) orinoco_clear_tkip_key(priv, setindex);
3994 if (erq->length > 0) {
3995 if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
3996 index = priv->tx_key;
3998 /* Adjust key length to a supported value */
3999 if (erq->length > SMALL_KEY_SIZE) {
4000 xlen = LARGE_KEY_SIZE;
4001 } else if (erq->length > 0) {
4002 xlen = SMALL_KEY_SIZE;
4003 } else
4004 xlen = 0;
4006 /* Switch on WEP if off */
4007 if ((encode_alg != IW_ENCODE_ALG_WEP) && (xlen > 0)) {
4008 setindex = index;
4009 encode_alg = IW_ENCODE_ALG_WEP;
4011 } else {
4012 /* Important note : if the user do "iwconfig eth0 enc off",
4013 * we will arrive there with an index of -1. This is valid
4014 * but need to be taken care off... Jean II */
4015 if ((index < 0) || (index >= ORINOCO_MAX_KEYS)) {
4016 if((index != -1) || (erq->flags == 0)) {
4017 err = -EINVAL;
4018 goto out;
4020 } else {
4021 /* Set the index : Check that the key is valid */
4022 if(priv->keys[index].len == 0) {
4023 err = -EINVAL;
4024 goto out;
4026 setindex = index;
4030 if (erq->flags & IW_ENCODE_DISABLED)
4031 encode_alg = IW_ENCODE_ALG_NONE;
4032 if (erq->flags & IW_ENCODE_OPEN)
4033 restricted = 0;
4034 if (erq->flags & IW_ENCODE_RESTRICTED)
4035 restricted = 1;
4037 if (erq->pointer && erq->length > 0) {
4038 priv->keys[index].len = cpu_to_le16(xlen);
4039 memset(priv->keys[index].data, 0,
4040 sizeof(priv->keys[index].data));
4041 memcpy(priv->keys[index].data, keybuf, erq->length);
4043 priv->tx_key = setindex;
4045 /* Try fast key change if connected and only keys are changed */
4046 if ((priv->encode_alg == encode_alg) &&
4047 (priv->wep_restrict == restricted) &&
4048 netif_carrier_ok(dev)) {
4049 err = __orinoco_hw_setup_wepkeys(priv);
4050 /* No need to commit if successful */
4051 goto out;
4054 priv->encode_alg = encode_alg;
4055 priv->wep_restrict = restricted;
4057 out:
4058 orinoco_unlock(priv, &flags);
4060 return err;
4063 static int orinoco_ioctl_getiwencode(struct net_device *dev,
4064 struct iw_request_info *info,
4065 struct iw_point *erq,
4066 char *keybuf)
4068 struct orinoco_private *priv = netdev_priv(dev);
4069 int index = (erq->flags & IW_ENCODE_INDEX) - 1;
4070 u16 xlen = 0;
4071 unsigned long flags;
4073 if (! priv->has_wep)
4074 return -EOPNOTSUPP;
4076 if (orinoco_lock(priv, &flags) != 0)
4077 return -EBUSY;
4079 if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
4080 index = priv->tx_key;
4082 erq->flags = 0;
4083 if (!priv->encode_alg)
4084 erq->flags |= IW_ENCODE_DISABLED;
4085 erq->flags |= index + 1;
4087 if (priv->wep_restrict)
4088 erq->flags |= IW_ENCODE_RESTRICTED;
4089 else
4090 erq->flags |= IW_ENCODE_OPEN;
4092 xlen = le16_to_cpu(priv->keys[index].len);
4094 erq->length = xlen;
4096 memcpy(keybuf, priv->keys[index].data, ORINOCO_MAX_KEY_SIZE);
4098 orinoco_unlock(priv, &flags);
4099 return 0;
4102 static int orinoco_ioctl_setessid(struct net_device *dev,
4103 struct iw_request_info *info,
4104 struct iw_point *erq,
4105 char *essidbuf)
4107 struct orinoco_private *priv = netdev_priv(dev);
4108 unsigned long flags;
4110 /* Note : ESSID is ignored in Ad-Hoc demo mode, but we can set it
4111 * anyway... - Jean II */
4113 /* Hum... Should not use Wireless Extension constant (may change),
4114 * should use our own... - Jean II */
4115 if (erq->length > IW_ESSID_MAX_SIZE)
4116 return -E2BIG;
4118 if (orinoco_lock(priv, &flags) != 0)
4119 return -EBUSY;
4121 /* NULL the string (for NULL termination & ESSID = ANY) - Jean II */
4122 memset(priv->desired_essid, 0, sizeof(priv->desired_essid));
4124 /* If not ANY, get the new ESSID */
4125 if (erq->flags) {
4126 memcpy(priv->desired_essid, essidbuf, erq->length);
4129 orinoco_unlock(priv, &flags);
4131 return -EINPROGRESS; /* Call commit handler */
4134 static int orinoco_ioctl_getessid(struct net_device *dev,
4135 struct iw_request_info *info,
4136 struct iw_point *erq,
4137 char *essidbuf)
4139 struct orinoco_private *priv = netdev_priv(dev);
4140 int active;
4141 int err = 0;
4142 unsigned long flags;
4144 if (netif_running(dev)) {
4145 err = orinoco_hw_get_essid(priv, &active, essidbuf);
4146 if (err < 0)
4147 return err;
4148 erq->length = err;
4149 } else {
4150 if (orinoco_lock(priv, &flags) != 0)
4151 return -EBUSY;
4152 memcpy(essidbuf, priv->desired_essid, IW_ESSID_MAX_SIZE);
4153 erq->length = strlen(priv->desired_essid);
4154 orinoco_unlock(priv, &flags);
4157 erq->flags = 1;
4159 return 0;
4162 static int orinoco_ioctl_setnick(struct net_device *dev,
4163 struct iw_request_info *info,
4164 struct iw_point *nrq,
4165 char *nickbuf)
4167 struct orinoco_private *priv = netdev_priv(dev);
4168 unsigned long flags;
4170 if (nrq->length > IW_ESSID_MAX_SIZE)
4171 return -E2BIG;
4173 if (orinoco_lock(priv, &flags) != 0)
4174 return -EBUSY;
4176 memset(priv->nick, 0, sizeof(priv->nick));
4177 memcpy(priv->nick, nickbuf, nrq->length);
4179 orinoco_unlock(priv, &flags);
4181 return -EINPROGRESS; /* Call commit handler */
4184 static int orinoco_ioctl_getnick(struct net_device *dev,
4185 struct iw_request_info *info,
4186 struct iw_point *nrq,
4187 char *nickbuf)
4189 struct orinoco_private *priv = netdev_priv(dev);
4190 unsigned long flags;
4192 if (orinoco_lock(priv, &flags) != 0)
4193 return -EBUSY;
4195 memcpy(nickbuf, priv->nick, IW_ESSID_MAX_SIZE);
4196 orinoco_unlock(priv, &flags);
4198 nrq->length = strlen(priv->nick);
4200 return 0;
4203 static int orinoco_ioctl_setfreq(struct net_device *dev,
4204 struct iw_request_info *info,
4205 struct iw_freq *frq,
4206 char *extra)
4208 struct orinoco_private *priv = netdev_priv(dev);
4209 int chan = -1;
4210 unsigned long flags;
4211 int err = -EINPROGRESS; /* Call commit handler */
4213 /* In infrastructure mode the AP sets the channel */
4214 if (priv->iw_mode == IW_MODE_INFRA)
4215 return -EBUSY;
4217 if ( (frq->e == 0) && (frq->m <= 1000) ) {
4218 /* Setting by channel number */
4219 chan = frq->m;
4220 } else {
4221 /* Setting by frequency - search the table */
4222 int mult = 1;
4223 int i;
4225 for (i = 0; i < (6 - frq->e); i++)
4226 mult *= 10;
4228 for (i = 0; i < NUM_CHANNELS; i++)
4229 if (frq->m == (channel_frequency[i] * mult))
4230 chan = i+1;
4233 if ( (chan < 1) || (chan > NUM_CHANNELS) ||
4234 ! (priv->channel_mask & (1 << (chan-1)) ) )
4235 return -EINVAL;
4237 if (orinoco_lock(priv, &flags) != 0)
4238 return -EBUSY;
4240 priv->channel = chan;
4241 if (priv->iw_mode == IW_MODE_MONITOR) {
4242 /* Fast channel change - no commit if successful */
4243 hermes_t *hw = &priv->hw;
4244 err = hw->ops->cmd_wait(hw, HERMES_CMD_TEST |
4245 HERMES_TEST_SET_CHANNEL,
4246 chan, NULL);
4248 orinoco_unlock(priv, &flags);
4250 return err;
4253 static int orinoco_ioctl_getfreq(struct net_device *dev,
4254 struct iw_request_info *info,
4255 struct iw_freq *frq,
4256 char *extra)
4258 struct orinoco_private *priv = netdev_priv(dev);
4259 int tmp;
4261 /* Locking done in there */
4262 tmp = orinoco_hw_get_freq(priv);
4263 if (tmp < 0) {
4264 return tmp;
4267 frq->m = tmp;
4268 frq->e = 1;
4270 return 0;
4273 static int orinoco_ioctl_getsens(struct net_device *dev,
4274 struct iw_request_info *info,
4275 struct iw_param *srq,
4276 char *extra)
4278 struct orinoco_private *priv = netdev_priv(dev);
4279 hermes_t *hw = &priv->hw;
4280 u16 val;
4281 int err;
4282 unsigned long flags;
4284 if (!priv->has_sensitivity)
4285 return -EOPNOTSUPP;
4287 if (orinoco_lock(priv, &flags) != 0)
4288 return -EBUSY;
4289 err = hermes_read_wordrec(hw, USER_BAP,
4290 HERMES_RID_CNFSYSTEMSCALE, &val);
4291 orinoco_unlock(priv, &flags);
4293 if (err)
4294 return err;
4296 srq->value = val;
4297 srq->fixed = 0; /* auto */
4299 return 0;
4302 static int orinoco_ioctl_setsens(struct net_device *dev,
4303 struct iw_request_info *info,
4304 struct iw_param *srq,
4305 char *extra)
4307 struct orinoco_private *priv = netdev_priv(dev);
4308 int val = srq->value;
4309 unsigned long flags;
4311 if (!priv->has_sensitivity)
4312 return -EOPNOTSUPP;
4314 if ((val < 1) || (val > 3))
4315 return -EINVAL;
4317 if (orinoco_lock(priv, &flags) != 0)
4318 return -EBUSY;
4319 priv->ap_density = val;
4320 orinoco_unlock(priv, &flags);
4322 return -EINPROGRESS; /* Call commit handler */
4325 static int orinoco_ioctl_setrts(struct net_device *dev,
4326 struct iw_request_info *info,
4327 struct iw_param *rrq,
4328 char *extra)
4330 struct orinoco_private *priv = netdev_priv(dev);
4331 int val = rrq->value;
4332 unsigned long flags;
4334 if (rrq->disabled)
4335 val = 2347;
4337 if ( (val < 0) || (val > 2347) )
4338 return -EINVAL;
4340 if (orinoco_lock(priv, &flags) != 0)
4341 return -EBUSY;
4343 priv->rts_thresh = val;
4344 orinoco_unlock(priv, &flags);
4346 return -EINPROGRESS; /* Call commit handler */
4349 static int orinoco_ioctl_getrts(struct net_device *dev,
4350 struct iw_request_info *info,
4351 struct iw_param *rrq,
4352 char *extra)
4354 struct orinoco_private *priv = netdev_priv(dev);
4356 rrq->value = priv->rts_thresh;
4357 rrq->disabled = (rrq->value == 2347);
4358 rrq->fixed = 1;
4360 return 0;
4363 static int orinoco_ioctl_setfrag(struct net_device *dev,
4364 struct iw_request_info *info,
4365 struct iw_param *frq,
4366 char *extra)
4368 struct orinoco_private *priv = netdev_priv(dev);
4369 int err = -EINPROGRESS; /* Call commit handler */
4370 unsigned long flags;
4372 if (orinoco_lock(priv, &flags) != 0)
4373 return -EBUSY;
4375 if (priv->has_mwo) {
4376 if (frq->disabled)
4377 priv->mwo_robust = 0;
4378 else {
4379 if (frq->fixed)
4380 printk(KERN_WARNING "%s: Fixed fragmentation is "
4381 "not supported on this firmware. "
4382 "Using MWO robust instead.\n", dev->name);
4383 priv->mwo_robust = 1;
4385 } else {
4386 if (frq->disabled)
4387 priv->frag_thresh = 2346;
4388 else {
4389 if ( (frq->value < 256) || (frq->value > 2346) )
4390 err = -EINVAL;
4391 else
4392 priv->frag_thresh = frq->value & ~0x1; /* must be even */
4396 orinoco_unlock(priv, &flags);
4398 return err;
4401 static int orinoco_ioctl_getfrag(struct net_device *dev,
4402 struct iw_request_info *info,
4403 struct iw_param *frq,
4404 char *extra)
4406 struct orinoco_private *priv = netdev_priv(dev);
4407 hermes_t *hw = &priv->hw;
4408 int err;
4409 u16 val;
4410 unsigned long flags;
4412 if (orinoco_lock(priv, &flags) != 0)
4413 return -EBUSY;
4415 if (priv->has_mwo) {
4416 err = hermes_read_wordrec(hw, USER_BAP,
4417 HERMES_RID_CNFMWOROBUST_AGERE,
4418 &val);
4419 if (err)
4420 val = 0;
4422 frq->value = val ? 2347 : 0;
4423 frq->disabled = ! val;
4424 frq->fixed = 0;
4425 } else {
4426 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
4427 &val);
4428 if (err)
4429 val = 0;
4431 frq->value = val;
4432 frq->disabled = (val >= 2346);
4433 frq->fixed = 1;
4436 orinoco_unlock(priv, &flags);
4438 return err;
4441 static int orinoco_ioctl_setrate(struct net_device *dev,
4442 struct iw_request_info *info,
4443 struct iw_param *rrq,
4444 char *extra)
4446 struct orinoco_private *priv = netdev_priv(dev);
4447 int ratemode = -1;
4448 int bitrate; /* 100s of kilobits */
4449 int i;
4450 unsigned long flags;
4452 /* As the user space doesn't know our highest rate, it uses -1
4453 * to ask us to set the highest rate. Test it using "iwconfig
4454 * ethX rate auto" - Jean II */
4455 if (rrq->value == -1)
4456 bitrate = 110;
4457 else {
4458 if (rrq->value % 100000)
4459 return -EINVAL;
4460 bitrate = rrq->value / 100000;
4463 if ( (bitrate != 10) && (bitrate != 20) &&
4464 (bitrate != 55) && (bitrate != 110) )
4465 return -EINVAL;
4467 for (i = 0; i < BITRATE_TABLE_SIZE; i++)
4468 if ( (bitrate_table[i].bitrate == bitrate) &&
4469 (bitrate_table[i].automatic == ! rrq->fixed) ) {
4470 ratemode = i;
4471 break;
4474 if (ratemode == -1)
4475 return -EINVAL;
4477 if (orinoco_lock(priv, &flags) != 0)
4478 return -EBUSY;
4479 priv->bitratemode = ratemode;
4480 orinoco_unlock(priv, &flags);
4482 return -EINPROGRESS;
4485 static int orinoco_ioctl_getrate(struct net_device *dev,
4486 struct iw_request_info *info,
4487 struct iw_param *rrq,
4488 char *extra)
4490 struct orinoco_private *priv = netdev_priv(dev);
4491 hermes_t *hw = &priv->hw;
4492 int err = 0;
4493 int ratemode;
4494 int i;
4495 u16 val;
4496 unsigned long flags;
4498 if (orinoco_lock(priv, &flags) != 0)
4499 return -EBUSY;
4501 ratemode = priv->bitratemode;
4503 BUG_ON((ratemode < 0) || (ratemode >= BITRATE_TABLE_SIZE));
4505 rrq->value = bitrate_table[ratemode].bitrate * 100000;
4506 rrq->fixed = ! bitrate_table[ratemode].automatic;
4507 rrq->disabled = 0;
4509 /* If the interface is running we try to find more about the
4510 current mode */
4511 if (netif_running(dev)) {
4512 err = hermes_read_wordrec(hw, USER_BAP,
4513 HERMES_RID_CURRENTTXRATE, &val);
4514 if (err)
4515 goto out;
4517 switch (priv->firmware_type) {
4518 case FIRMWARE_TYPE_AGERE: /* Lucent style rate */
4519 /* Note : in Lucent firmware, the return value of
4520 * HERMES_RID_CURRENTTXRATE is the bitrate in Mb/s,
4521 * and therefore is totally different from the
4522 * encoding of HERMES_RID_CNFTXRATECONTROL.
4523 * Don't forget that 6Mb/s is really 5.5Mb/s */
4524 if (val == 6)
4525 rrq->value = 5500000;
4526 else
4527 rrq->value = val * 1000000;
4528 break;
4529 case FIRMWARE_TYPE_INTERSIL: /* Intersil style rate */
4530 case FIRMWARE_TYPE_SYMBOL: /* Symbol style rate */
4531 for (i = 0; i < BITRATE_TABLE_SIZE; i++)
4532 if (bitrate_table[i].intersil_txratectrl == val) {
4533 ratemode = i;
4534 break;
4536 if (i >= BITRATE_TABLE_SIZE)
4537 printk(KERN_INFO "%s: Unable to determine current bitrate (0x%04hx)\n",
4538 dev->name, val);
4540 rrq->value = bitrate_table[ratemode].bitrate * 100000;
4541 break;
4542 default:
4543 BUG();
4547 out:
4548 orinoco_unlock(priv, &flags);
4550 return err;
4553 static int orinoco_ioctl_setpower(struct net_device *dev,
4554 struct iw_request_info *info,
4555 struct iw_param *prq,
4556 char *extra)
4558 struct orinoco_private *priv = netdev_priv(dev);
4559 int err = -EINPROGRESS; /* Call commit handler */
4560 unsigned long flags;
4562 if (orinoco_lock(priv, &flags) != 0)
4563 return -EBUSY;
4565 if (prq->disabled) {
4566 priv->pm_on = 0;
4567 } else {
4568 switch (prq->flags & IW_POWER_MODE) {
4569 case IW_POWER_UNICAST_R:
4570 priv->pm_mcast = 0;
4571 priv->pm_on = 1;
4572 break;
4573 case IW_POWER_ALL_R:
4574 priv->pm_mcast = 1;
4575 priv->pm_on = 1;
4576 break;
4577 case IW_POWER_ON:
4578 /* No flags : but we may have a value - Jean II */
4579 break;
4580 default:
4581 err = -EINVAL;
4582 goto out;
4585 if (prq->flags & IW_POWER_TIMEOUT) {
4586 priv->pm_on = 1;
4587 priv->pm_timeout = prq->value / 1000;
4589 if (prq->flags & IW_POWER_PERIOD) {
4590 priv->pm_on = 1;
4591 priv->pm_period = prq->value / 1000;
4593 /* It's valid to not have a value if we are just toggling
4594 * the flags... Jean II */
4595 if(!priv->pm_on) {
4596 err = -EINVAL;
4597 goto out;
4601 out:
4602 orinoco_unlock(priv, &flags);
4604 return err;
4607 static int orinoco_ioctl_getpower(struct net_device *dev,
4608 struct iw_request_info *info,
4609 struct iw_param *prq,
4610 char *extra)
4612 struct orinoco_private *priv = netdev_priv(dev);
4613 hermes_t *hw = &priv->hw;
4614 int err = 0;
4615 u16 enable, period, timeout, mcast;
4616 unsigned long flags;
4618 if (orinoco_lock(priv, &flags) != 0)
4619 return -EBUSY;
4621 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFPMENABLED, &enable);
4622 if (err)
4623 goto out;
4625 err = hermes_read_wordrec(hw, USER_BAP,
4626 HERMES_RID_CNFMAXSLEEPDURATION, &period);
4627 if (err)
4628 goto out;
4630 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFPMHOLDOVERDURATION, &timeout);
4631 if (err)
4632 goto out;
4634 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFMULTICASTRECEIVE, &mcast);
4635 if (err)
4636 goto out;
4638 prq->disabled = !enable;
4639 /* Note : by default, display the period */
4640 if ((prq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
4641 prq->flags = IW_POWER_TIMEOUT;
4642 prq->value = timeout * 1000;
4643 } else {
4644 prq->flags = IW_POWER_PERIOD;
4645 prq->value = period * 1000;
4647 if (mcast)
4648 prq->flags |= IW_POWER_ALL_R;
4649 else
4650 prq->flags |= IW_POWER_UNICAST_R;
4652 out:
4653 orinoco_unlock(priv, &flags);
4655 return err;
4658 static int orinoco_ioctl_set_encodeext(struct net_device *dev,
4659 struct iw_request_info *info,
4660 union iwreq_data *wrqu,
4661 char *extra)
4663 struct orinoco_private *priv = netdev_priv(dev);
4664 struct iw_point *encoding = &wrqu->encoding;
4665 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
4666 int idx, alg = ext->alg, set_key = 1;
4667 unsigned long flags;
4668 int err = -EINVAL;
4669 u16 key_len;
4671 if (orinoco_lock(priv, &flags) != 0)
4672 return -EBUSY;
4674 /* Determine and validate the key index */
4675 idx = encoding->flags & IW_ENCODE_INDEX;
4676 if (idx) {
4677 if ((idx < 1) || (idx > WEP_KEYS))
4678 goto out;
4679 idx--;
4680 } else
4681 idx = priv->tx_key;
4683 if (encoding->flags & IW_ENCODE_DISABLED)
4684 alg = IW_ENCODE_ALG_NONE;
4686 if (priv->has_wpa && (alg != IW_ENCODE_ALG_TKIP)) {
4687 /* Clear any TKIP TX key we had */
4688 (void) orinoco_clear_tkip_key(priv, priv->tx_key);
4691 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
4692 priv->tx_key = idx;
4693 set_key = ((alg == IW_ENCODE_ALG_TKIP) ||
4694 (ext->key_len > 0)) ? 1 : 0;
4697 if (set_key) {
4698 /* Set the requested key first */
4699 switch (alg) {
4700 case IW_ENCODE_ALG_NONE:
4701 priv->encode_alg = alg;
4702 priv->keys[idx].len = 0;
4703 break;
4705 case IW_ENCODE_ALG_WEP:
4706 if (ext->key_len > SMALL_KEY_SIZE)
4707 key_len = LARGE_KEY_SIZE;
4708 else if (ext->key_len > 0)
4709 key_len = SMALL_KEY_SIZE;
4710 else
4711 goto out;
4713 priv->encode_alg = alg;
4714 priv->keys[idx].len = cpu_to_le16(key_len);
4716 key_len = min(ext->key_len, key_len);
4718 memset(priv->keys[idx].data, 0, ORINOCO_MAX_KEY_SIZE);
4719 memcpy(priv->keys[idx].data, ext->key, key_len);
4720 break;
4722 case IW_ENCODE_ALG_TKIP:
4724 hermes_t *hw = &priv->hw;
4725 u8 *tkip_iv = NULL;
4727 if (!priv->has_wpa ||
4728 (ext->key_len > sizeof(priv->tkip_key[0])))
4729 goto out;
4731 priv->encode_alg = alg;
4732 memset(&priv->tkip_key[idx], 0,
4733 sizeof(priv->tkip_key[idx]));
4734 memcpy(&priv->tkip_key[idx], ext->key, ext->key_len);
4736 if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID)
4737 tkip_iv = &ext->rx_seq[0];
4739 err = __orinoco_hw_set_tkip_key(hw, idx,
4740 ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY,
4741 (u8 *) &priv->tkip_key[idx],
4742 tkip_iv, NULL);
4743 if (err)
4744 printk(KERN_ERR "%s: Error %d setting TKIP key"
4745 "\n", dev->name, err);
4747 goto out;
4749 default:
4750 goto out;
4753 err = -EINPROGRESS;
4754 out:
4755 orinoco_unlock(priv, &flags);
4757 return err;
4760 static int orinoco_ioctl_get_encodeext(struct net_device *dev,
4761 struct iw_request_info *info,
4762 union iwreq_data *wrqu,
4763 char *extra)
4765 struct orinoco_private *priv = netdev_priv(dev);
4766 struct iw_point *encoding = &wrqu->encoding;
4767 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
4768 int idx, max_key_len;
4769 unsigned long flags;
4770 int err;
4772 if (orinoco_lock(priv, &flags) != 0)
4773 return -EBUSY;
4775 err = -EINVAL;
4776 max_key_len = encoding->length - sizeof(*ext);
4777 if (max_key_len < 0)
4778 goto out;
4780 idx = encoding->flags & IW_ENCODE_INDEX;
4781 if (idx) {
4782 if ((idx < 1) || (idx > WEP_KEYS))
4783 goto out;
4784 idx--;
4785 } else
4786 idx = priv->tx_key;
4788 encoding->flags = idx + 1;
4789 memset(ext, 0, sizeof(*ext));
4791 ext->alg = priv->encode_alg;
4792 switch (priv->encode_alg) {
4793 case IW_ENCODE_ALG_NONE:
4794 ext->key_len = 0;
4795 encoding->flags |= IW_ENCODE_DISABLED;
4796 break;
4797 case IW_ENCODE_ALG_WEP:
4798 ext->key_len = min_t(u16, le16_to_cpu(priv->keys[idx].len),
4799 max_key_len);
4800 memcpy(ext->key, priv->keys[idx].data, ext->key_len);
4801 encoding->flags |= IW_ENCODE_ENABLED;
4802 break;
4803 case IW_ENCODE_ALG_TKIP:
4804 ext->key_len = min_t(u16, sizeof(struct orinoco_tkip_key),
4805 max_key_len);
4806 memcpy(ext->key, &priv->tkip_key[idx], ext->key_len);
4807 encoding->flags |= IW_ENCODE_ENABLED;
4808 break;
4811 err = 0;
4812 out:
4813 orinoco_unlock(priv, &flags);
4815 return err;
4818 static int orinoco_ioctl_set_auth(struct net_device *dev,
4819 struct iw_request_info *info,
4820 union iwreq_data *wrqu, char *extra)
4822 struct orinoco_private *priv = netdev_priv(dev);
4823 hermes_t *hw = &priv->hw;
4824 struct iw_param *param = &wrqu->param;
4825 unsigned long flags;
4826 int ret = -EINPROGRESS;
4828 if (orinoco_lock(priv, &flags) != 0)
4829 return -EBUSY;
4831 switch (param->flags & IW_AUTH_INDEX) {
4832 case IW_AUTH_WPA_VERSION:
4833 case IW_AUTH_CIPHER_PAIRWISE:
4834 case IW_AUTH_CIPHER_GROUP:
4835 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
4836 case IW_AUTH_PRIVACY_INVOKED:
4837 case IW_AUTH_DROP_UNENCRYPTED:
4839 * orinoco does not use these parameters
4841 break;
4843 case IW_AUTH_KEY_MGMT:
4844 /* wl_lkm implies value 2 == PSK for Hermes I
4845 * which ties in with WEXT
4846 * no other hints tho :(
4848 priv->key_mgmt = param->value;
4849 break;
4851 case IW_AUTH_TKIP_COUNTERMEASURES:
4852 /* When countermeasures are enabled, shut down the
4853 * card; when disabled, re-enable the card. This must
4854 * take effect immediately.
4856 * TODO: Make sure that the EAPOL message is getting
4857 * out before card disabled
4859 if (param->value) {
4860 priv->tkip_cm_active = 1;
4861 ret = hermes_enable_port(hw, 0);
4862 } else {
4863 priv->tkip_cm_active = 0;
4864 ret = hermes_disable_port(hw, 0);
4866 break;
4868 case IW_AUTH_80211_AUTH_ALG:
4869 if (param->value & IW_AUTH_ALG_SHARED_KEY)
4870 priv->wep_restrict = 1;
4871 else if (param->value & IW_AUTH_ALG_OPEN_SYSTEM)
4872 priv->wep_restrict = 0;
4873 else
4874 ret = -EINVAL;
4875 break;
4877 case IW_AUTH_WPA_ENABLED:
4878 if (priv->has_wpa) {
4879 priv->wpa_enabled = param->value ? 1 : 0;
4880 } else {
4881 if (param->value)
4882 ret = -EOPNOTSUPP;
4883 /* else silently accept disable of WPA */
4884 priv->wpa_enabled = 0;
4886 break;
4888 default:
4889 ret = -EOPNOTSUPP;
4892 orinoco_unlock(priv, &flags);
4893 return ret;
4896 static int orinoco_ioctl_get_auth(struct net_device *dev,
4897 struct iw_request_info *info,
4898 union iwreq_data *wrqu, char *extra)
4900 struct orinoco_private *priv = netdev_priv(dev);
4901 struct iw_param *param = &wrqu->param;
4902 unsigned long flags;
4903 int ret = 0;
4905 if (orinoco_lock(priv, &flags) != 0)
4906 return -EBUSY;
4908 switch (param->flags & IW_AUTH_INDEX) {
4909 case IW_AUTH_KEY_MGMT:
4910 param->value = priv->key_mgmt;
4911 break;
4913 case IW_AUTH_TKIP_COUNTERMEASURES:
4914 param->value = priv->tkip_cm_active;
4915 break;
4917 case IW_AUTH_80211_AUTH_ALG:
4918 if (priv->wep_restrict)
4919 param->value = IW_AUTH_ALG_SHARED_KEY;
4920 else
4921 param->value = IW_AUTH_ALG_OPEN_SYSTEM;
4922 break;
4924 case IW_AUTH_WPA_ENABLED:
4925 param->value = priv->wpa_enabled;
4926 break;
4928 default:
4929 ret = -EOPNOTSUPP;
4932 orinoco_unlock(priv, &flags);
4933 return ret;
4936 static int orinoco_ioctl_set_genie(struct net_device *dev,
4937 struct iw_request_info *info,
4938 union iwreq_data *wrqu, char *extra)
4940 struct orinoco_private *priv = netdev_priv(dev);
4941 u8 *buf;
4942 unsigned long flags;
4943 int err = 0;
4945 if ((wrqu->data.length > MAX_WPA_IE_LEN) ||
4946 (wrqu->data.length && (extra == NULL)))
4947 return -EINVAL;
4949 if (orinoco_lock(priv, &flags) != 0)
4950 return -EBUSY;
4952 if (wrqu->data.length) {
4953 buf = kmalloc(wrqu->data.length, GFP_KERNEL);
4954 if (buf == NULL) {
4955 err = -ENOMEM;
4956 goto out;
4959 memcpy(buf, extra, wrqu->data.length);
4960 kfree(priv->wpa_ie);
4961 priv->wpa_ie = buf;
4962 priv->wpa_ie_len = wrqu->data.length;
4963 } else {
4964 kfree(priv->wpa_ie);
4965 priv->wpa_ie = NULL;
4966 priv->wpa_ie_len = 0;
4969 if (priv->wpa_ie) {
4970 /* Looks like wl_lkm wants to check the auth alg, and
4971 * somehow pass it to the firmware.
4972 * Instead it just calls the key mgmt rid
4973 * - we do this in set auth.
4977 out:
4978 orinoco_unlock(priv, &flags);
4979 return err;
4982 static int orinoco_ioctl_get_genie(struct net_device *dev,
4983 struct iw_request_info *info,
4984 union iwreq_data *wrqu, char *extra)
4986 struct orinoco_private *priv = netdev_priv(dev);
4987 unsigned long flags;
4988 int err = 0;
4990 if (orinoco_lock(priv, &flags) != 0)
4991 return -EBUSY;
4993 if ((priv->wpa_ie_len == 0) || (priv->wpa_ie == NULL)) {
4994 wrqu->data.length = 0;
4995 goto out;
4998 if (wrqu->data.length < priv->wpa_ie_len) {
4999 err = -E2BIG;
5000 goto out;
5003 wrqu->data.length = priv->wpa_ie_len;
5004 memcpy(extra, priv->wpa_ie, priv->wpa_ie_len);
5006 out:
5007 orinoco_unlock(priv, &flags);
5008 return err;
5011 static int orinoco_ioctl_set_mlme(struct net_device *dev,
5012 struct iw_request_info *info,
5013 union iwreq_data *wrqu, char *extra)
5015 struct orinoco_private *priv = netdev_priv(dev);
5016 hermes_t *hw = &priv->hw;
5017 struct iw_mlme *mlme = (struct iw_mlme *)extra;
5018 unsigned long flags;
5019 int ret = 0;
5021 if (orinoco_lock(priv, &flags) != 0)
5022 return -EBUSY;
5024 switch (mlme->cmd) {
5025 case IW_MLME_DEAUTH:
5026 /* silently ignore */
5027 break;
5029 case IW_MLME_DISASSOC:
5031 struct {
5032 u8 addr[ETH_ALEN];
5033 __le16 reason_code;
5034 } __attribute__ ((packed)) buf;
5036 memcpy(buf.addr, mlme->addr.sa_data, ETH_ALEN);
5037 buf.reason_code = cpu_to_le16(mlme->reason_code);
5038 ret = HERMES_WRITE_RECORD(hw, USER_BAP,
5039 HERMES_RID_CNFDISASSOCIATE,
5040 &buf);
5041 break;
5043 default:
5044 ret = -EOPNOTSUPP;
5047 orinoco_unlock(priv, &flags);
5048 return ret;
5051 static int orinoco_ioctl_getretry(struct net_device *dev,
5052 struct iw_request_info *info,
5053 struct iw_param *rrq,
5054 char *extra)
5056 struct orinoco_private *priv = netdev_priv(dev);
5057 hermes_t *hw = &priv->hw;
5058 int err = 0;
5059 u16 short_limit, long_limit, lifetime;
5060 unsigned long flags;
5062 if (orinoco_lock(priv, &flags) != 0)
5063 return -EBUSY;
5065 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_SHORTRETRYLIMIT,
5066 &short_limit);
5067 if (err)
5068 goto out;
5070 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_LONGRETRYLIMIT,
5071 &long_limit);
5072 if (err)
5073 goto out;
5075 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_MAXTRANSMITLIFETIME,
5076 &lifetime);
5077 if (err)
5078 goto out;
5080 rrq->disabled = 0; /* Can't be disabled */
5082 /* Note : by default, display the retry number */
5083 if ((rrq->flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME) {
5084 rrq->flags = IW_RETRY_LIFETIME;
5085 rrq->value = lifetime * 1000; /* ??? */
5086 } else {
5087 /* By default, display the min number */
5088 if ((rrq->flags & IW_RETRY_LONG)) {
5089 rrq->flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
5090 rrq->value = long_limit;
5091 } else {
5092 rrq->flags = IW_RETRY_LIMIT;
5093 rrq->value = short_limit;
5094 if(short_limit != long_limit)
5095 rrq->flags |= IW_RETRY_SHORT;
5099 out:
5100 orinoco_unlock(priv, &flags);
5102 return err;
5105 static int orinoco_ioctl_reset(struct net_device *dev,
5106 struct iw_request_info *info,
5107 void *wrqu,
5108 char *extra)
5110 struct orinoco_private *priv = netdev_priv(dev);
5112 if (! capable(CAP_NET_ADMIN))
5113 return -EPERM;
5115 if (info->cmd == (SIOCIWFIRSTPRIV + 0x1)) {
5116 printk(KERN_DEBUG "%s: Forcing reset!\n", dev->name);
5118 /* Firmware reset */
5119 orinoco_reset(&priv->reset_work);
5120 } else {
5121 printk(KERN_DEBUG "%s: Force scheduling reset!\n", dev->name);
5123 schedule_work(&priv->reset_work);
5126 return 0;
5129 static int orinoco_ioctl_setibssport(struct net_device *dev,
5130 struct iw_request_info *info,
5131 void *wrqu,
5132 char *extra)
5135 struct orinoco_private *priv = netdev_priv(dev);
5136 int val = *( (int *) extra );
5137 unsigned long flags;
5139 if (orinoco_lock(priv, &flags) != 0)
5140 return -EBUSY;
5142 priv->ibss_port = val ;
5144 /* Actually update the mode we are using */
5145 set_port_type(priv);
5147 orinoco_unlock(priv, &flags);
5148 return -EINPROGRESS; /* Call commit handler */
5151 static int orinoco_ioctl_getibssport(struct net_device *dev,
5152 struct iw_request_info *info,
5153 void *wrqu,
5154 char *extra)
5156 struct orinoco_private *priv = netdev_priv(dev);
5157 int *val = (int *) extra;
5159 *val = priv->ibss_port;
5160 return 0;
5163 static int orinoco_ioctl_setport3(struct net_device *dev,
5164 struct iw_request_info *info,
5165 void *wrqu,
5166 char *extra)
5168 struct orinoco_private *priv = netdev_priv(dev);
5169 int val = *( (int *) extra );
5170 int err = 0;
5171 unsigned long flags;
5173 if (orinoco_lock(priv, &flags) != 0)
5174 return -EBUSY;
5176 switch (val) {
5177 case 0: /* Try to do IEEE ad-hoc mode */
5178 if (! priv->has_ibss) {
5179 err = -EINVAL;
5180 break;
5182 priv->prefer_port3 = 0;
5184 break;
5186 case 1: /* Try to do Lucent proprietary ad-hoc mode */
5187 if (! priv->has_port3) {
5188 err = -EINVAL;
5189 break;
5191 priv->prefer_port3 = 1;
5192 break;
5194 default:
5195 err = -EINVAL;
5198 if (! err) {
5199 /* Actually update the mode we are using */
5200 set_port_type(priv);
5201 err = -EINPROGRESS;
5204 orinoco_unlock(priv, &flags);
5206 return err;
5209 static int orinoco_ioctl_getport3(struct net_device *dev,
5210 struct iw_request_info *info,
5211 void *wrqu,
5212 char *extra)
5214 struct orinoco_private *priv = netdev_priv(dev);
5215 int *val = (int *) extra;
5217 *val = priv->prefer_port3;
5218 return 0;
5221 static int orinoco_ioctl_setpreamble(struct net_device *dev,
5222 struct iw_request_info *info,
5223 void *wrqu,
5224 char *extra)
5226 struct orinoco_private *priv = netdev_priv(dev);
5227 unsigned long flags;
5228 int val;
5230 if (! priv->has_preamble)
5231 return -EOPNOTSUPP;
5233 /* 802.11b has recently defined some short preamble.
5234 * Basically, the Phy header has been reduced in size.
5235 * This increase performance, especially at high rates
5236 * (the preamble is transmitted at 1Mb/s), unfortunately
5237 * this give compatibility troubles... - Jean II */
5238 val = *( (int *) extra );
5240 if (orinoco_lock(priv, &flags) != 0)
5241 return -EBUSY;
5243 if (val)
5244 priv->preamble = 1;
5245 else
5246 priv->preamble = 0;
5248 orinoco_unlock(priv, &flags);
5250 return -EINPROGRESS; /* Call commit handler */
5253 static int orinoco_ioctl_getpreamble(struct net_device *dev,
5254 struct iw_request_info *info,
5255 void *wrqu,
5256 char *extra)
5258 struct orinoco_private *priv = netdev_priv(dev);
5259 int *val = (int *) extra;
5261 if (! priv->has_preamble)
5262 return -EOPNOTSUPP;
5264 *val = priv->preamble;
5265 return 0;
5268 /* ioctl interface to hermes_read_ltv()
5269 * To use with iwpriv, pass the RID as the token argument, e.g.
5270 * iwpriv get_rid [0xfc00]
5271 * At least Wireless Tools 25 is required to use iwpriv.
5272 * For Wireless Tools 25 and 26 append "dummy" are the end. */
5273 static int orinoco_ioctl_getrid(struct net_device *dev,
5274 struct iw_request_info *info,
5275 struct iw_point *data,
5276 char *extra)
5278 struct orinoco_private *priv = netdev_priv(dev);
5279 hermes_t *hw = &priv->hw;
5280 int rid = data->flags;
5281 u16 length;
5282 int err;
5283 unsigned long flags;
5285 /* It's a "get" function, but we don't want users to access the
5286 * WEP key and other raw firmware data */
5287 if (! capable(CAP_NET_ADMIN))
5288 return -EPERM;
5290 if (rid < 0xfc00 || rid > 0xffff)
5291 return -EINVAL;
5293 if (orinoco_lock(priv, &flags) != 0)
5294 return -EBUSY;
5296 err = hw->ops->read_ltv(hw, USER_BAP, rid, MAX_RID_LEN, &length,
5297 extra);
5298 if (err)
5299 goto out;
5301 data->length = min_t(u16, HERMES_RECLEN_TO_BYTES(length),
5302 MAX_RID_LEN);
5304 out:
5305 orinoco_unlock(priv, &flags);
5306 return err;
5309 /* Trigger a scan (look for other cells in the vicinity) */
5310 static int orinoco_ioctl_setscan(struct net_device *dev,
5311 struct iw_request_info *info,
5312 struct iw_point *srq,
5313 char *extra)
5315 struct orinoco_private *priv = netdev_priv(dev);
5316 hermes_t *hw = &priv->hw;
5317 struct iw_scan_req *si = (struct iw_scan_req *) extra;
5318 int err = 0;
5319 unsigned long flags;
5321 /* Note : you may have realised that, as this is a SET operation,
5322 * this is privileged and therefore a normal user can't
5323 * perform scanning.
5324 * This is not an error, while the device perform scanning,
5325 * traffic doesn't flow, so it's a perfect DoS...
5326 * Jean II */
5328 if (orinoco_lock(priv, &flags) != 0)
5329 return -EBUSY;
5331 /* Scanning with port 0 disabled would fail */
5332 if (!netif_running(dev)) {
5333 err = -ENETDOWN;
5334 goto out;
5337 /* In monitor mode, the scan results are always empty.
5338 * Probe responses are passed to the driver as received
5339 * frames and could be processed in software. */
5340 if (priv->iw_mode == IW_MODE_MONITOR) {
5341 err = -EOPNOTSUPP;
5342 goto out;
5345 /* Note : because we don't lock out the irq handler, the way
5346 * we access scan variables in priv is critical.
5347 * o scan_inprogress : not touched by irq handler
5348 * o scan_mode : not touched by irq handler
5349 * Before modifying anything on those variables, please think hard !
5350 * Jean II */
5352 /* Save flags */
5353 priv->scan_mode = srq->flags;
5355 /* Always trigger scanning, even if it's in progress.
5356 * This way, if the info frame get lost, we will recover somewhat
5357 * gracefully - Jean II */
5359 if (priv->has_hostscan) {
5360 switch (priv->firmware_type) {
5361 case FIRMWARE_TYPE_SYMBOL:
5362 err = hermes_write_wordrec(hw, USER_BAP,
5363 HERMES_RID_CNFHOSTSCAN_SYMBOL,
5364 HERMES_HOSTSCAN_SYMBOL_ONCE |
5365 HERMES_HOSTSCAN_SYMBOL_BCAST);
5366 break;
5367 case FIRMWARE_TYPE_INTERSIL: {
5368 __le16 req[3];
5370 req[0] = cpu_to_le16(0x3fff); /* All channels */
5371 req[1] = cpu_to_le16(0x0001); /* rate 1 Mbps */
5372 req[2] = 0; /* Any ESSID */
5373 err = HERMES_WRITE_RECORD(hw, USER_BAP,
5374 HERMES_RID_CNFHOSTSCAN, &req);
5376 break;
5377 case FIRMWARE_TYPE_AGERE:
5378 if (priv->scan_mode & IW_SCAN_THIS_ESSID) {
5379 struct hermes_idstring idbuf;
5380 size_t len = min(sizeof(idbuf.val),
5381 (size_t) si->essid_len);
5382 idbuf.len = cpu_to_le16(len);
5383 memcpy(idbuf.val, si->essid, len);
5385 err = hw->ops->write_ltv(hw, USER_BAP,
5386 HERMES_RID_CNFSCANSSID_AGERE,
5387 HERMES_BYTES_TO_RECLEN(len + 2),
5388 &idbuf);
5389 } else
5390 err = hermes_write_wordrec(hw, USER_BAP,
5391 HERMES_RID_CNFSCANSSID_AGERE,
5392 0); /* Any ESSID */
5393 if (err)
5394 break;
5396 if (priv->has_ext_scan) {
5397 /* Clear scan results at the start of
5398 * an extended scan */
5399 orinoco_clear_scan_results(priv,
5400 msecs_to_jiffies(15000));
5402 /* TODO: Is this available on older firmware?
5403 * Can we use it to scan specific channels
5404 * for IW_SCAN_THIS_FREQ? */
5405 err = hermes_write_wordrec(hw, USER_BAP,
5406 HERMES_RID_CNFSCANCHANNELS2GHZ,
5407 0x7FFF);
5408 if (err)
5409 goto out;
5411 err = hermes_inquire(hw,
5412 HERMES_INQ_CHANNELINFO);
5413 } else
5414 err = hermes_inquire(hw, HERMES_INQ_SCAN);
5415 break;
5417 } else
5418 err = hermes_inquire(hw, HERMES_INQ_SCAN);
5420 /* One more client */
5421 if (! err)
5422 priv->scan_inprogress = 1;
5424 out:
5425 orinoco_unlock(priv, &flags);
5426 return err;
5429 #define MAX_CUSTOM_LEN 64
5431 /* Translate scan data returned from the card to a card independant
5432 * format that the Wireless Tools will understand - Jean II */
5433 static inline char *orinoco_translate_scan(struct net_device *dev,
5434 struct iw_request_info *info,
5435 char *current_ev,
5436 char *end_buf,
5437 union hermes_scan_info *bss,
5438 unsigned int last_scanned)
5440 struct orinoco_private *priv = netdev_priv(dev);
5441 u16 capabilities;
5442 u16 channel;
5443 struct iw_event iwe; /* Temporary buffer */
5444 char custom[MAX_CUSTOM_LEN];
5446 memset(&iwe, 0, sizeof(iwe));
5448 /* First entry *MUST* be the AP MAC address */
5449 iwe.cmd = SIOCGIWAP;
5450 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
5451 memcpy(iwe.u.ap_addr.sa_data, bss->a.bssid, ETH_ALEN);
5452 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5453 &iwe, IW_EV_ADDR_LEN);
5455 /* Other entries will be displayed in the order we give them */
5457 /* Add the ESSID */
5458 iwe.u.data.length = le16_to_cpu(bss->a.essid_len);
5459 if (iwe.u.data.length > 32)
5460 iwe.u.data.length = 32;
5461 iwe.cmd = SIOCGIWESSID;
5462 iwe.u.data.flags = 1;
5463 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5464 &iwe, bss->a.essid);
5466 /* Add mode */
5467 iwe.cmd = SIOCGIWMODE;
5468 capabilities = le16_to_cpu(bss->a.capabilities);
5469 if (capabilities & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
5470 if (capabilities & WLAN_CAPABILITY_ESS)
5471 iwe.u.mode = IW_MODE_MASTER;
5472 else
5473 iwe.u.mode = IW_MODE_ADHOC;
5474 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5475 &iwe, IW_EV_UINT_LEN);
5478 channel = bss->s.channel;
5479 if ((channel >= 1) && (channel <= NUM_CHANNELS)) {
5480 /* Add channel and frequency */
5481 iwe.cmd = SIOCGIWFREQ;
5482 iwe.u.freq.m = channel;
5483 iwe.u.freq.e = 0;
5484 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5485 &iwe, IW_EV_FREQ_LEN);
5487 iwe.u.freq.m = channel_frequency[channel-1] * 100000;
5488 iwe.u.freq.e = 1;
5489 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5490 &iwe, IW_EV_FREQ_LEN);
5493 /* Add quality statistics. level and noise in dB. No link quality */
5494 iwe.cmd = IWEVQUAL;
5495 iwe.u.qual.updated = IW_QUAL_DBM | IW_QUAL_QUAL_INVALID;
5496 iwe.u.qual.level = (__u8) le16_to_cpu(bss->a.level) - 0x95;
5497 iwe.u.qual.noise = (__u8) le16_to_cpu(bss->a.noise) - 0x95;
5498 /* Wireless tools prior to 27.pre22 will show link quality
5499 * anyway, so we provide a reasonable value. */
5500 if (iwe.u.qual.level > iwe.u.qual.noise)
5501 iwe.u.qual.qual = iwe.u.qual.level - iwe.u.qual.noise;
5502 else
5503 iwe.u.qual.qual = 0;
5504 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5505 &iwe, IW_EV_QUAL_LEN);
5507 /* Add encryption capability */
5508 iwe.cmd = SIOCGIWENCODE;
5509 if (capabilities & WLAN_CAPABILITY_PRIVACY)
5510 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
5511 else
5512 iwe.u.data.flags = IW_ENCODE_DISABLED;
5513 iwe.u.data.length = 0;
5514 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5515 &iwe, NULL);
5517 /* Bit rate is not available in Lucent/Agere firmwares */
5518 if (priv->firmware_type != FIRMWARE_TYPE_AGERE) {
5519 char *current_val = current_ev + iwe_stream_lcp_len(info);
5520 int i;
5521 int step;
5523 if (priv->firmware_type == FIRMWARE_TYPE_SYMBOL)
5524 step = 2;
5525 else
5526 step = 1;
5528 iwe.cmd = SIOCGIWRATE;
5529 /* Those two flags are ignored... */
5530 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
5531 /* Max 10 values */
5532 for (i = 0; i < 10; i += step) {
5533 /* NULL terminated */
5534 if (bss->p.rates[i] == 0x0)
5535 break;
5536 /* Bit rate given in 500 kb/s units (+ 0x80) */
5537 iwe.u.bitrate.value =
5538 ((bss->p.rates[i] & 0x7f) * 500000);
5539 current_val = iwe_stream_add_value(info, current_ev,
5540 current_val,
5541 end_buf, &iwe,
5542 IW_EV_PARAM_LEN);
5544 /* Check if we added any event */
5545 if ((current_val - current_ev) > iwe_stream_lcp_len(info))
5546 current_ev = current_val;
5549 /* Beacon interval */
5550 iwe.cmd = IWEVCUSTOM;
5551 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5552 "bcn_int=%d",
5553 le16_to_cpu(bss->a.beacon_interv));
5554 if (iwe.u.data.length)
5555 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5556 &iwe, custom);
5558 /* Capabilites */
5559 iwe.cmd = IWEVCUSTOM;
5560 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5561 "capab=0x%04x",
5562 capabilities);
5563 if (iwe.u.data.length)
5564 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5565 &iwe, custom);
5567 /* Add EXTRA: Age to display seconds since last beacon/probe response
5568 * for given network. */
5569 iwe.cmd = IWEVCUSTOM;
5570 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5571 " Last beacon: %dms ago",
5572 jiffies_to_msecs(jiffies - last_scanned));
5573 if (iwe.u.data.length)
5574 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5575 &iwe, custom);
5577 return current_ev;
5580 static inline char *orinoco_translate_ext_scan(struct net_device *dev,
5581 struct iw_request_info *info,
5582 char *current_ev,
5583 char *end_buf,
5584 struct agere_ext_scan_info *bss,
5585 unsigned int last_scanned)
5587 u16 capabilities;
5588 u16 channel;
5589 struct iw_event iwe; /* Temporary buffer */
5590 char custom[MAX_CUSTOM_LEN];
5591 u8 *ie;
5593 memset(&iwe, 0, sizeof(iwe));
5595 /* First entry *MUST* be the AP MAC address */
5596 iwe.cmd = SIOCGIWAP;
5597 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
5598 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
5599 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5600 &iwe, IW_EV_ADDR_LEN);
5602 /* Other entries will be displayed in the order we give them */
5604 /* Add the ESSID */
5605 ie = bss->data;
5606 iwe.u.data.length = ie[1];
5607 if (iwe.u.data.length) {
5608 if (iwe.u.data.length > 32)
5609 iwe.u.data.length = 32;
5610 iwe.cmd = SIOCGIWESSID;
5611 iwe.u.data.flags = 1;
5612 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5613 &iwe, &ie[2]);
5616 /* Add mode */
5617 capabilities = le16_to_cpu(bss->capabilities);
5618 if (capabilities & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
5619 iwe.cmd = SIOCGIWMODE;
5620 if (capabilities & WLAN_CAPABILITY_ESS)
5621 iwe.u.mode = IW_MODE_MASTER;
5622 else
5623 iwe.u.mode = IW_MODE_ADHOC;
5624 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5625 &iwe, IW_EV_UINT_LEN);
5628 ie = orinoco_get_ie(bss->data, sizeof(bss->data), MFIE_TYPE_DS_SET);
5629 channel = ie ? ie[2] : 0;
5630 if ((channel >= 1) && (channel <= NUM_CHANNELS)) {
5631 /* Add channel and frequency */
5632 iwe.cmd = SIOCGIWFREQ;
5633 iwe.u.freq.m = channel;
5634 iwe.u.freq.e = 0;
5635 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5636 &iwe, IW_EV_FREQ_LEN);
5638 iwe.u.freq.m = channel_frequency[channel-1] * 100000;
5639 iwe.u.freq.e = 1;
5640 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5641 &iwe, IW_EV_FREQ_LEN);
5644 /* Add quality statistics. level and noise in dB. No link quality */
5645 iwe.cmd = IWEVQUAL;
5646 iwe.u.qual.updated = IW_QUAL_DBM | IW_QUAL_QUAL_INVALID;
5647 iwe.u.qual.level = bss->level - 0x95;
5648 iwe.u.qual.noise = bss->noise - 0x95;
5649 /* Wireless tools prior to 27.pre22 will show link quality
5650 * anyway, so we provide a reasonable value. */
5651 if (iwe.u.qual.level > iwe.u.qual.noise)
5652 iwe.u.qual.qual = iwe.u.qual.level - iwe.u.qual.noise;
5653 else
5654 iwe.u.qual.qual = 0;
5655 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5656 &iwe, IW_EV_QUAL_LEN);
5658 /* Add encryption capability */
5659 iwe.cmd = SIOCGIWENCODE;
5660 if (capabilities & WLAN_CAPABILITY_PRIVACY)
5661 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
5662 else
5663 iwe.u.data.flags = IW_ENCODE_DISABLED;
5664 iwe.u.data.length = 0;
5665 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5666 &iwe, NULL);
5668 /* WPA IE */
5669 ie = orinoco_get_wpa_ie(bss->data, sizeof(bss->data));
5670 if (ie) {
5671 iwe.cmd = IWEVGENIE;
5672 iwe.u.data.length = ie[1] + 2;
5673 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5674 &iwe, ie);
5677 /* RSN IE */
5678 ie = orinoco_get_ie(bss->data, sizeof(bss->data), MFIE_TYPE_RSN);
5679 if (ie) {
5680 iwe.cmd = IWEVGENIE;
5681 iwe.u.data.length = ie[1] + 2;
5682 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5683 &iwe, ie);
5686 ie = orinoco_get_ie(bss->data, sizeof(bss->data), MFIE_TYPE_RATES);
5687 if (ie) {
5688 char *p = current_ev + iwe_stream_lcp_len(info);
5689 int i;
5691 iwe.cmd = SIOCGIWRATE;
5692 /* Those two flags are ignored... */
5693 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
5695 for (i = 2; i < (ie[1] + 2); i++) {
5696 iwe.u.bitrate.value = ((ie[i] & 0x7F) * 500000);
5697 p = iwe_stream_add_value(info, current_ev, p, end_buf,
5698 &iwe, IW_EV_PARAM_LEN);
5700 /* Check if we added any event */
5701 if (p > (current_ev + iwe_stream_lcp_len(info)))
5702 current_ev = p;
5705 /* Timestamp */
5706 iwe.cmd = IWEVCUSTOM;
5707 iwe.u.data.length =
5708 snprintf(custom, MAX_CUSTOM_LEN, "tsf=%016llx",
5709 (unsigned long long) le64_to_cpu(bss->timestamp));
5710 if (iwe.u.data.length)
5711 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5712 &iwe, custom);
5714 /* Beacon interval */
5715 iwe.cmd = IWEVCUSTOM;
5716 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5717 "bcn_int=%d",
5718 le16_to_cpu(bss->beacon_interval));
5719 if (iwe.u.data.length)
5720 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5721 &iwe, custom);
5723 /* Capabilites */
5724 iwe.cmd = IWEVCUSTOM;
5725 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5726 "capab=0x%04x",
5727 capabilities);
5728 if (iwe.u.data.length)
5729 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5730 &iwe, custom);
5732 /* Add EXTRA: Age to display seconds since last beacon/probe response
5733 * for given network. */
5734 iwe.cmd = IWEVCUSTOM;
5735 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5736 " Last beacon: %dms ago",
5737 jiffies_to_msecs(jiffies - last_scanned));
5738 if (iwe.u.data.length)
5739 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5740 &iwe, custom);
5742 return current_ev;
5745 /* Return results of a scan */
5746 static int orinoco_ioctl_getscan(struct net_device *dev,
5747 struct iw_request_info *info,
5748 struct iw_point *srq,
5749 char *extra)
5751 struct orinoco_private *priv = netdev_priv(dev);
5752 int err = 0;
5753 unsigned long flags;
5754 char *current_ev = extra;
5756 if (orinoco_lock(priv, &flags) != 0)
5757 return -EBUSY;
5759 if (priv->scan_inprogress) {
5760 /* Important note : we don't want to block the caller
5761 * until results are ready for various reasons.
5762 * First, managing wait queues is complex and racy.
5763 * Second, we grab some rtnetlink lock before comming
5764 * here (in dev_ioctl()).
5765 * Third, we generate an Wireless Event, so the
5766 * caller can wait itself on that - Jean II */
5767 err = -EAGAIN;
5768 goto out;
5771 if (priv->has_ext_scan) {
5772 struct xbss_element *bss;
5774 list_for_each_entry(bss, &priv->bss_list, list) {
5775 /* Translate this entry to WE format */
5776 current_ev =
5777 orinoco_translate_ext_scan(dev, info,
5778 current_ev,
5779 extra + srq->length,
5780 &bss->bss,
5781 bss->last_scanned);
5783 /* Check if there is space for one more entry */
5784 if ((extra + srq->length - current_ev)
5785 <= IW_EV_ADDR_LEN) {
5786 /* Ask user space to try again with a
5787 * bigger buffer */
5788 err = -E2BIG;
5789 goto out;
5793 } else {
5794 struct bss_element *bss;
5796 list_for_each_entry(bss, &priv->bss_list, list) {
5797 /* Translate this entry to WE format */
5798 current_ev = orinoco_translate_scan(dev, info,
5799 current_ev,
5800 extra + srq->length,
5801 &bss->bss,
5802 bss->last_scanned);
5804 /* Check if there is space for one more entry */
5805 if ((extra + srq->length - current_ev)
5806 <= IW_EV_ADDR_LEN) {
5807 /* Ask user space to try again with a
5808 * bigger buffer */
5809 err = -E2BIG;
5810 goto out;
5815 srq->length = (current_ev - extra);
5816 srq->flags = (__u16) priv->scan_mode;
5818 out:
5819 orinoco_unlock(priv, &flags);
5820 return err;
5823 /* Commit handler, called after set operations */
5824 static int orinoco_ioctl_commit(struct net_device *dev,
5825 struct iw_request_info *info,
5826 void *wrqu,
5827 char *extra)
5829 struct orinoco_private *priv = netdev_priv(dev);
5830 struct hermes *hw = &priv->hw;
5831 unsigned long flags;
5832 int err = 0;
5834 if (!priv->open)
5835 return 0;
5837 if (priv->broken_disableport) {
5838 orinoco_reset(&priv->reset_work);
5839 return 0;
5842 if (orinoco_lock(priv, &flags) != 0)
5843 return err;
5845 err = hermes_disable_port(hw, 0);
5846 if (err) {
5847 printk(KERN_WARNING "%s: Unable to disable port "
5848 "while reconfiguring card\n", dev->name);
5849 priv->broken_disableport = 1;
5850 goto out;
5853 err = __orinoco_program_rids(dev);
5854 if (err) {
5855 printk(KERN_WARNING "%s: Unable to reconfigure card\n",
5856 dev->name);
5857 goto out;
5860 err = hermes_enable_port(hw, 0);
5861 if (err) {
5862 printk(KERN_WARNING "%s: Unable to enable port while reconfiguring card\n",
5863 dev->name);
5864 goto out;
5867 out:
5868 if (err) {
5869 printk(KERN_WARNING "%s: Resetting instead...\n", dev->name);
5870 schedule_work(&priv->reset_work);
5871 err = 0;
5874 orinoco_unlock(priv, &flags);
5875 return err;
5878 static const struct iw_priv_args orinoco_privtab[] = {
5879 { SIOCIWFIRSTPRIV + 0x0, 0, 0, "force_reset" },
5880 { SIOCIWFIRSTPRIV + 0x1, 0, 0, "card_reset" },
5881 { SIOCIWFIRSTPRIV + 0x2, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5882 0, "set_port3" },
5883 { SIOCIWFIRSTPRIV + 0x3, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5884 "get_port3" },
5885 { SIOCIWFIRSTPRIV + 0x4, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5886 0, "set_preamble" },
5887 { SIOCIWFIRSTPRIV + 0x5, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5888 "get_preamble" },
5889 { SIOCIWFIRSTPRIV + 0x6, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5890 0, "set_ibssport" },
5891 { SIOCIWFIRSTPRIV + 0x7, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5892 "get_ibssport" },
5893 { SIOCIWFIRSTPRIV + 0x9, 0, IW_PRIV_TYPE_BYTE | MAX_RID_LEN,
5894 "get_rid" },
5899 * Structures to export the Wireless Handlers
5902 #define STD_IW_HANDLER(id, func) \
5903 [IW_IOCTL_IDX(id)] = (iw_handler) func
5904 static const iw_handler orinoco_handler[] = {
5905 STD_IW_HANDLER(SIOCSIWCOMMIT, orinoco_ioctl_commit),
5906 STD_IW_HANDLER(SIOCGIWNAME, orinoco_ioctl_getname),
5907 STD_IW_HANDLER(SIOCSIWFREQ, orinoco_ioctl_setfreq),
5908 STD_IW_HANDLER(SIOCGIWFREQ, orinoco_ioctl_getfreq),
5909 STD_IW_HANDLER(SIOCSIWMODE, orinoco_ioctl_setmode),
5910 STD_IW_HANDLER(SIOCGIWMODE, orinoco_ioctl_getmode),
5911 STD_IW_HANDLER(SIOCSIWSENS, orinoco_ioctl_setsens),
5912 STD_IW_HANDLER(SIOCGIWSENS, orinoco_ioctl_getsens),
5913 STD_IW_HANDLER(SIOCGIWRANGE, orinoco_ioctl_getiwrange),
5914 STD_IW_HANDLER(SIOCSIWSPY, iw_handler_set_spy),
5915 STD_IW_HANDLER(SIOCGIWSPY, iw_handler_get_spy),
5916 STD_IW_HANDLER(SIOCSIWTHRSPY, iw_handler_set_thrspy),
5917 STD_IW_HANDLER(SIOCGIWTHRSPY, iw_handler_get_thrspy),
5918 STD_IW_HANDLER(SIOCSIWAP, orinoco_ioctl_setwap),
5919 STD_IW_HANDLER(SIOCGIWAP, orinoco_ioctl_getwap),
5920 STD_IW_HANDLER(SIOCSIWSCAN, orinoco_ioctl_setscan),
5921 STD_IW_HANDLER(SIOCGIWSCAN, orinoco_ioctl_getscan),
5922 STD_IW_HANDLER(SIOCSIWESSID, orinoco_ioctl_setessid),
5923 STD_IW_HANDLER(SIOCGIWESSID, orinoco_ioctl_getessid),
5924 STD_IW_HANDLER(SIOCSIWNICKN, orinoco_ioctl_setnick),
5925 STD_IW_HANDLER(SIOCGIWNICKN, orinoco_ioctl_getnick),
5926 STD_IW_HANDLER(SIOCSIWRATE, orinoco_ioctl_setrate),
5927 STD_IW_HANDLER(SIOCGIWRATE, orinoco_ioctl_getrate),
5928 STD_IW_HANDLER(SIOCSIWRTS, orinoco_ioctl_setrts),
5929 STD_IW_HANDLER(SIOCGIWRTS, orinoco_ioctl_getrts),
5930 STD_IW_HANDLER(SIOCSIWFRAG, orinoco_ioctl_setfrag),
5931 STD_IW_HANDLER(SIOCGIWFRAG, orinoco_ioctl_getfrag),
5932 STD_IW_HANDLER(SIOCGIWRETRY, orinoco_ioctl_getretry),
5933 STD_IW_HANDLER(SIOCSIWENCODE, orinoco_ioctl_setiwencode),
5934 STD_IW_HANDLER(SIOCGIWENCODE, orinoco_ioctl_getiwencode),
5935 STD_IW_HANDLER(SIOCSIWPOWER, orinoco_ioctl_setpower),
5936 STD_IW_HANDLER(SIOCGIWPOWER, orinoco_ioctl_getpower),
5937 STD_IW_HANDLER(SIOCSIWGENIE, orinoco_ioctl_set_genie),
5938 STD_IW_HANDLER(SIOCGIWGENIE, orinoco_ioctl_get_genie),
5939 STD_IW_HANDLER(SIOCSIWMLME, orinoco_ioctl_set_mlme),
5940 STD_IW_HANDLER(SIOCSIWAUTH, orinoco_ioctl_set_auth),
5941 STD_IW_HANDLER(SIOCGIWAUTH, orinoco_ioctl_get_auth),
5942 STD_IW_HANDLER(SIOCSIWENCODEEXT, orinoco_ioctl_set_encodeext),
5943 STD_IW_HANDLER(SIOCGIWENCODEEXT, orinoco_ioctl_get_encodeext),
5948 Added typecasting since we no longer use iwreq_data -- Moustafa
5950 static const iw_handler orinoco_private_handler[] = {
5951 [0] = (iw_handler) orinoco_ioctl_reset,
5952 [1] = (iw_handler) orinoco_ioctl_reset,
5953 [2] = (iw_handler) orinoco_ioctl_setport3,
5954 [3] = (iw_handler) orinoco_ioctl_getport3,
5955 [4] = (iw_handler) orinoco_ioctl_setpreamble,
5956 [5] = (iw_handler) orinoco_ioctl_getpreamble,
5957 [6] = (iw_handler) orinoco_ioctl_setibssport,
5958 [7] = (iw_handler) orinoco_ioctl_getibssport,
5959 [9] = (iw_handler) orinoco_ioctl_getrid,
5962 static const struct iw_handler_def orinoco_handler_def = {
5963 .num_standard = ARRAY_SIZE(orinoco_handler),
5964 .num_private = ARRAY_SIZE(orinoco_private_handler),
5965 .num_private_args = ARRAY_SIZE(orinoco_privtab),
5966 .standard = orinoco_handler,
5967 .private = orinoco_private_handler,
5968 .private_args = orinoco_privtab,
5969 .get_wireless_stats = orinoco_get_wireless_stats,
5972 static void orinoco_get_drvinfo(struct net_device *dev,
5973 struct ethtool_drvinfo *info)
5975 struct orinoco_private *priv = netdev_priv(dev);
5977 strncpy(info->driver, DRIVER_NAME, sizeof(info->driver) - 1);
5978 strncpy(info->version, DRIVER_VERSION, sizeof(info->version) - 1);
5979 strncpy(info->fw_version, priv->fw_name, sizeof(info->fw_version) - 1);
5980 if (dev->dev.parent)
5981 strncpy(info->bus_info, dev->dev.parent->bus_id,
5982 sizeof(info->bus_info) - 1);
5983 else
5984 snprintf(info->bus_info, sizeof(info->bus_info) - 1,
5985 "PCMCIA %p", priv->hw.iobase);
5988 static const struct ethtool_ops orinoco_ethtool_ops = {
5989 .get_drvinfo = orinoco_get_drvinfo,
5990 .get_link = ethtool_op_get_link,
5993 /********************************************************************/
5994 /* Module initialization */
5995 /********************************************************************/
5997 EXPORT_SYMBOL(alloc_orinocodev);
5998 EXPORT_SYMBOL(free_orinocodev);
6000 EXPORT_SYMBOL(__orinoco_up);
6001 EXPORT_SYMBOL(__orinoco_down);
6002 EXPORT_SYMBOL(orinoco_reinit_firmware);
6004 EXPORT_SYMBOL(orinoco_interrupt);
6006 /* Can't be declared "const" or the whole __initdata section will
6007 * become const */
6008 static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
6009 " (David Gibson <hermes@gibson.dropbear.id.au>, "
6010 "Pavel Roskin <proski@gnu.org>, et al)";
6012 static int __init init_orinoco(void)
6014 printk(KERN_DEBUG "%s\n", version);
6015 return 0;
6018 static void __exit exit_orinoco(void)
6022 module_init(init_orinoco);
6023 module_exit(exit_orinoco);