blob: 171bfa03868ea5c55718fdb91d7994e6395caac8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* orinoco.c - (formerly known as dldwd_cs.c and orinoco_cs.c)
2 *
3 * A driver for Hermes or Prism 2 chipset based PCMCIA wireless
4 * adaptors, with Lucent/Agere, Intersil or Symbol firmware.
5 *
6 * Current maintainers (as of 29 September 2003) are:
7 * Pavel Roskin <proski AT gnu.org>
8 * and David Gibson <hermes AT gibson.dropbear.id.au>
9 *
10 * (C) Copyright David Gibson, IBM Corporation 2001-2003.
11 * Copyright (C) 2000 David Gibson, Linuxcare Australia.
12 * With some help from :
13 * Copyright (C) 2001 Jean Tourrilhes, HP Labs
14 * Copyright (C) 2001 Benjamin Herrenschmidt
15 *
16 * Based on dummy_cs.c 1.27 2000/06/12 21:27:25
17 *
18 * Portions based on wvlan_cs.c 1.0.6, Copyright Andreas Neuhaus <andy
19 * AT fasta.fh-dortmund.de>
20 * http://www.stud.fh-dortmund.de/~andy/wvlan/
21 *
22 * The contents of this file are subject to the Mozilla Public License
23 * Version 1.1 (the "License"); you may not use this file except in
24 * compliance with the License. You may obtain a copy of the License
25 * at http://www.mozilla.org/MPL/
26 *
27 * Software distributed under the License is distributed on an "AS IS"
28 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
29 * the License for the specific language governing rights and
30 * limitations under the License.
31 *
32 * The initial developer of the original code is David A. Hinds
33 * <dahinds AT users.sourceforge.net>. Portions created by David
34 * A. Hinds are Copyright (C) 1999 David A. Hinds. All Rights
35 * Reserved.
36 *
37 * Alternatively, the contents of this file may be used under the
38 * terms of the GNU General Public License version 2 (the "GPL"), in
39 * which case the provisions of the GPL are applicable instead of the
40 * above. If you wish to allow the use of your version of this file
41 * only under the terms of the GPL and not to allow others to use your
42 * version of this file under the MPL, indicate your decision by
43 * deleting the provisions above and replace them with the notice and
44 * other provisions required by the GPL. If you do not delete the
45 * provisions above, a recipient may use your version of this file
46 * under either the MPL or the GPL. */
47
48/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * TODO
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * o Handle de-encapsulation within network layer, provide 802.11
51 * headers (patch from Thomas 'Dent' Mirlacher)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * o Fix possible races in SPY handling.
53 * o Disconnect wireless extensions from fundamental configuration.
54 * o (maybe) Software WEP support (patch from Stano Meduna).
55 * o (maybe) Use multiple Tx buffers - driver handling queue
56 * rather than firmware.
57 */
58
59/* Locking and synchronization:
60 *
61 * The basic principle is that everything is serialized through a
62 * single spinlock, priv->lock. The lock is used in user, bh and irq
63 * context, so when taken outside hardirq context it should always be
64 * taken with interrupts disabled. The lock protects both the
65 * hardware and the struct orinoco_private.
66 *
67 * Another flag, priv->hw_unavailable indicates that the hardware is
68 * unavailable for an extended period of time (e.g. suspended, or in
69 * the middle of a hard reset). This flag is protected by the
70 * spinlock. All code which touches the hardware should check the
71 * flag after taking the lock, and if it is set, give up on whatever
72 * they are doing and drop the lock again. The orinoco_lock()
73 * function handles this (it unlocks and returns -EBUSY if
74 * hw_unavailable is non-zero).
75 */
76
77#define DRIVER_NAME "orinoco"
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <linux/module.h>
80#include <linux/kernel.h>
81#include <linux/init.h>
David Kilroyd03032a2008-08-21 23:28:02 +010082#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include <linux/etherdevice.h>
Christoph Hellwig1fab2e82005-06-19 01:27:40 +020085#include <linux/ethtool.h>
David Kilroy3994d502008-08-21 23:27:54 +010086#include <linux/firmware.h>
David Kilroy39d1ffe2008-11-22 10:37:28 +000087#include <linux/suspend.h>
Pavel Roskin9c974fb2006-08-15 20:45:03 -040088#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#include <linux/wireless.h>
Johannes Berg2c7060022008-10-30 22:09:54 +010090#include <linux/ieee80211.h>
Christoph Hellwig620554e2005-06-19 01:27:33 +020091#include <net/iw_handler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
David Kilroy23edcc42008-08-21 23:28:05 +010093#include <linux/scatterlist.h>
94#include <linux/crypto.h>
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#include "hermes_rid.h"
David Kilroy3994d502008-08-21 23:27:54 +010097#include "hermes_dld.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#include "orinoco.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100/********************************************************************/
101/* Module information */
102/********************************************************************/
103
104MODULE_AUTHOR("Pavel Roskin <proski@gnu.org> & David Gibson <hermes@gibson.dropbear.id.au>");
105MODULE_DESCRIPTION("Driver for Lucent Orinoco, Prism II based and similar wireless cards");
106MODULE_LICENSE("Dual MPL/GPL");
107
108/* Level of debugging. Used in the macros in orinoco.h */
109#ifdef ORINOCO_DEBUG
110int orinoco_debug = ORINOCO_DEBUG;
111module_param(orinoco_debug, int, 0644);
112MODULE_PARM_DESC(orinoco_debug, "Debug level");
113EXPORT_SYMBOL(orinoco_debug);
114#endif
115
116static int suppress_linkstatus; /* = 0 */
117module_param(suppress_linkstatus, bool, 0644);
118MODULE_PARM_DESC(suppress_linkstatus, "Don't log link status changes");
David Gibson7bb7c3a2005-05-12 20:02:10 -0400119static int ignore_disconnect; /* = 0 */
120module_param(ignore_disconnect, int, 0644);
121MODULE_PARM_DESC(ignore_disconnect, "Don't report lost link to the network layer");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200123static int force_monitor; /* = 0 */
124module_param(force_monitor, int, 0644);
125MODULE_PARM_DESC(force_monitor, "Allow monitor mode for all firmware versions");
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/********************************************************************/
128/* Compile time configuration and compatibility stuff */
129/********************************************************************/
130
131/* We do this this way to avoid ifdefs in the actual code */
132#ifdef WIRELESS_SPY
Pavel Roskin343c6862005-09-09 18:43:02 -0400133#define SPY_NUMBER(priv) (priv->spy_data.spy_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#else
135#define SPY_NUMBER(priv) 0
136#endif /* WIRELESS_SPY */
137
138/********************************************************************/
139/* Internal constants */
140/********************************************************************/
141
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200142/* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
143static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
144#define ENCAPS_OVERHEAD (sizeof(encaps_hdr) + 2)
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#define ORINOCO_MIN_MTU 256
Johannes Berg2c7060022008-10-30 22:09:54 +0100147#define ORINOCO_MAX_MTU (IEEE80211_MAX_DATA_LEN - ENCAPS_OVERHEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149#define SYMBOL_MAX_VER_LEN (14)
150#define USER_BAP 0
151#define IRQ_BAP 1
152#define MAX_IRQLOOPS_PER_IRQ 10
153#define MAX_IRQLOOPS_PER_JIFFY (20000/HZ) /* Based on a guestimate of
154 * how many events the
155 * device could
156 * legitimately generate */
157#define SMALL_KEY_SIZE 5
158#define LARGE_KEY_SIZE 13
159#define TX_NICBUF_SIZE_BUG 1585 /* Bug in Symbol firmware */
160
161#define DUMMY_FID 0xFFFF
162
163/*#define MAX_MULTICAST(priv) (priv->firmware_type == FIRMWARE_TYPE_AGERE ? \
164 HERMES_MAX_MULTICAST : 0)*/
165#define MAX_MULTICAST(priv) (HERMES_MAX_MULTICAST)
166
167#define ORINOCO_INTEN (HERMES_EV_RX | HERMES_EV_ALLOC \
168 | HERMES_EV_TX | HERMES_EV_TXEXC \
169 | HERMES_EV_WTERR | HERMES_EV_INFO \
170 | HERMES_EV_INFDROP )
171
Christoph Hellwig620554e2005-06-19 01:27:33 +0200172#define MAX_RID_LEN 1024
173
174static const struct iw_handler_def orinoco_handler_def;
Jeff Garzik7282d492006-09-13 14:30:00 -0400175static const struct ethtool_ops orinoco_ethtool_ops;
Christoph Hellwig620554e2005-06-19 01:27:33 +0200176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/********************************************************************/
178/* Data tables */
179/********************************************************************/
180
181/* The frequency of each channel in MHz */
182static const long channel_frequency[] = {
183 2412, 2417, 2422, 2427, 2432, 2437, 2442,
184 2447, 2452, 2457, 2462, 2467, 2472, 2484
185};
186#define NUM_CHANNELS ARRAY_SIZE(channel_frequency)
187
188/* This tables gives the actual meanings of the bitrate IDs returned
189 * by the firmware. */
190static struct {
191 int bitrate; /* in 100s of kilobits */
192 int automatic;
193 u16 agere_txratectrl;
194 u16 intersil_txratectrl;
195} bitrate_table[] = {
196 {110, 1, 3, 15}, /* Entry 0 is the default */
197 {10, 0, 1, 1},
198 {10, 1, 1, 1},
199 {20, 0, 2, 2},
200 {20, 1, 6, 3},
201 {55, 0, 4, 4},
202 {55, 1, 7, 7},
203 {110, 0, 5, 8},
204};
205#define BITRATE_TABLE_SIZE ARRAY_SIZE(bitrate_table)
206
207/********************************************************************/
208/* Data types */
209/********************************************************************/
210
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400211/* Beginning of the Tx descriptor, used in TxExc handling */
212struct hermes_txexc_data {
213 struct hermes_tx_descriptor desc;
Pavel Roskind133ae42005-09-23 04:18:06 -0400214 __le16 frame_ctl;
215 __le16 duration_id;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200216 u8 addr1[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217} __attribute__ ((packed));
218
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200219/* Rx frame header except compatibility 802.3 header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220struct hermes_rx_descriptor {
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200221 /* Control */
Pavel Roskind133ae42005-09-23 04:18:06 -0400222 __le16 status;
223 __le32 time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 u8 silence;
225 u8 signal;
226 u8 rate;
227 u8 rxflow;
Pavel Roskind133ae42005-09-23 04:18:06 -0400228 __le32 reserved;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200229
230 /* 802.11 header */
Pavel Roskind133ae42005-09-23 04:18:06 -0400231 __le16 frame_ctl;
232 __le16 duration_id;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200233 u8 addr1[ETH_ALEN];
234 u8 addr2[ETH_ALEN];
235 u8 addr3[ETH_ALEN];
Pavel Roskind133ae42005-09-23 04:18:06 -0400236 __le16 seq_ctl;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200237 u8 addr4[ETH_ALEN];
238
239 /* Data length */
Pavel Roskind133ae42005-09-23 04:18:06 -0400240 __le16 data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241} __attribute__ ((packed));
242
243/********************************************************************/
244/* Function prototypes */
245/********************************************************************/
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247static int __orinoco_program_rids(struct net_device *dev);
248static void __orinoco_set_multicast_list(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250/********************************************************************/
David Kilroy23edcc42008-08-21 23:28:05 +0100251/* Michael MIC crypto setup */
252/********************************************************************/
253#define MICHAEL_MIC_LEN 8
254static int orinoco_mic_init(struct orinoco_private *priv)
255{
256 priv->tx_tfm_mic = crypto_alloc_hash("michael_mic", 0, 0);
257 if (IS_ERR(priv->tx_tfm_mic)) {
258 printk(KERN_DEBUG "orinoco_mic_init: could not allocate "
259 "crypto API michael_mic\n");
260 priv->tx_tfm_mic = NULL;
261 return -ENOMEM;
262 }
263
264 priv->rx_tfm_mic = crypto_alloc_hash("michael_mic", 0, 0);
265 if (IS_ERR(priv->rx_tfm_mic)) {
266 printk(KERN_DEBUG "orinoco_mic_init: could not allocate "
267 "crypto API michael_mic\n");
268 priv->rx_tfm_mic = NULL;
269 return -ENOMEM;
270 }
271
272 return 0;
273}
274
275static void orinoco_mic_free(struct orinoco_private *priv)
276{
277 if (priv->tx_tfm_mic)
278 crypto_free_hash(priv->tx_tfm_mic);
279 if (priv->rx_tfm_mic)
280 crypto_free_hash(priv->rx_tfm_mic);
281}
282
283static int michael_mic(struct crypto_hash *tfm_michael, u8 *key,
284 u8 *da, u8 *sa, u8 priority,
285 u8 *data, size_t data_len, u8 *mic)
286{
287 struct hash_desc desc;
288 struct scatterlist sg[2];
289 u8 hdr[ETH_HLEN + 2]; /* size of header + padding */
290
291 if (tfm_michael == NULL) {
292 printk(KERN_WARNING "michael_mic: tfm_michael == NULL\n");
293 return -1;
294 }
295
296 /* Copy header into buffer. We need the padding on the end zeroed */
297 memcpy(&hdr[0], da, ETH_ALEN);
298 memcpy(&hdr[ETH_ALEN], sa, ETH_ALEN);
299 hdr[ETH_ALEN*2] = priority;
300 hdr[ETH_ALEN*2+1] = 0;
301 hdr[ETH_ALEN*2+2] = 0;
302 hdr[ETH_ALEN*2+3] = 0;
303
304 /* Use scatter gather to MIC header and data in one go */
305 sg_init_table(sg, 2);
306 sg_set_buf(&sg[0], hdr, sizeof(hdr));
307 sg_set_buf(&sg[1], data, data_len);
308
309 if (crypto_hash_setkey(tfm_michael, key, MIC_KEYLEN))
310 return -1;
311
312 desc.tfm = tfm_michael;
313 desc.flags = 0;
314 return crypto_hash_digest(&desc, sg, data_len + sizeof(hdr),
315 mic);
316}
317
318/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319/* Internal helper functions */
320/********************************************************************/
321
322static inline void set_port_type(struct orinoco_private *priv)
323{
324 switch (priv->iw_mode) {
325 case IW_MODE_INFRA:
326 priv->port_type = 1;
327 priv->createibss = 0;
328 break;
329 case IW_MODE_ADHOC:
330 if (priv->prefer_port3) {
331 priv->port_type = 3;
332 priv->createibss = 0;
333 } else {
334 priv->port_type = priv->ibss_port;
335 priv->createibss = 1;
336 }
337 break;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200338 case IW_MODE_MONITOR:
339 priv->port_type = 3;
340 priv->createibss = 0;
341 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 default:
343 printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
344 priv->ndev->name);
345 }
346}
347
Dan Williams1e3428e2007-10-10 23:56:25 -0400348#define ORINOCO_MAX_BSS_COUNT 64
349static int orinoco_bss_data_allocate(struct orinoco_private *priv)
350{
David Kilroy01632fa2008-08-21 23:27:58 +0100351 if (priv->bss_xbss_data)
Dan Williams1e3428e2007-10-10 23:56:25 -0400352 return 0;
353
David Kilroy01632fa2008-08-21 23:27:58 +0100354 if (priv->has_ext_scan)
355 priv->bss_xbss_data = kzalloc(ORINOCO_MAX_BSS_COUNT *
356 sizeof(struct xbss_element),
357 GFP_KERNEL);
358 else
359 priv->bss_xbss_data = kzalloc(ORINOCO_MAX_BSS_COUNT *
360 sizeof(struct bss_element),
361 GFP_KERNEL);
362
363 if (!priv->bss_xbss_data) {
Dan Williams1e3428e2007-10-10 23:56:25 -0400364 printk(KERN_WARNING "Out of memory allocating beacons");
365 return -ENOMEM;
366 }
367 return 0;
368}
369
370static void orinoco_bss_data_free(struct orinoco_private *priv)
371{
David Kilroy01632fa2008-08-21 23:27:58 +0100372 kfree(priv->bss_xbss_data);
373 priv->bss_xbss_data = NULL;
Dan Williams1e3428e2007-10-10 23:56:25 -0400374}
375
David Kilroy01632fa2008-08-21 23:27:58 +0100376#define PRIV_BSS ((struct bss_element *)priv->bss_xbss_data)
377#define PRIV_XBSS ((struct xbss_element *)priv->bss_xbss_data)
Dan Williams1e3428e2007-10-10 23:56:25 -0400378static void orinoco_bss_data_init(struct orinoco_private *priv)
379{
380 int i;
381
382 INIT_LIST_HEAD(&priv->bss_free_list);
383 INIT_LIST_HEAD(&priv->bss_list);
David Kilroy01632fa2008-08-21 23:27:58 +0100384 if (priv->has_ext_scan)
385 for (i = 0; i < ORINOCO_MAX_BSS_COUNT; i++)
386 list_add_tail(&(PRIV_XBSS[i].list),
387 &priv->bss_free_list);
388 else
389 for (i = 0; i < ORINOCO_MAX_BSS_COUNT; i++)
390 list_add_tail(&(PRIV_BSS[i].list),
391 &priv->bss_free_list);
392
393}
394
395static inline u8 *orinoco_get_ie(u8 *data, size_t len,
Johannes Berg2c7060022008-10-30 22:09:54 +0100396 enum ieee80211_eid eid)
David Kilroy01632fa2008-08-21 23:27:58 +0100397{
398 u8 *p = data;
399 while ((p + 2) < (data + len)) {
400 if (p[0] == eid)
401 return p;
402 p += p[1] + 2;
403 }
404 return NULL;
405}
406
407#define WPA_OUI_TYPE "\x00\x50\xF2\x01"
408#define WPA_SELECTOR_LEN 4
409static inline u8 *orinoco_get_wpa_ie(u8 *data, size_t len)
410{
411 u8 *p = data;
412 while ((p + 2 + WPA_SELECTOR_LEN) < (data + len)) {
Johannes Berg2c7060022008-10-30 22:09:54 +0100413 if ((p[0] == WLAN_EID_GENERIC) &&
David Kilroy01632fa2008-08-21 23:27:58 +0100414 (memcmp(&p[2], WPA_OUI_TYPE, WPA_SELECTOR_LEN) == 0))
415 return p;
416 p += p[1] + 2;
417 }
418 return NULL;
Dan Williams1e3428e2007-10-10 23:56:25 -0400419}
420
David Kilroy3994d502008-08-21 23:27:54 +0100421
422/********************************************************************/
423/* Download functionality */
424/********************************************************************/
425
426struct fw_info {
427 char *pri_fw;
428 char *sta_fw;
429 char *ap_fw;
430 u32 pda_addr;
431 u16 pda_size;
432};
433
434const static struct fw_info orinoco_fw[] = {
David Kilroy74734312008-11-22 10:37:25 +0000435 { NULL, "agere_sta_fw.bin", "agere_ap_fw.bin", 0x00390000, 1000 },
436 { NULL, "prism_sta_fw.bin", "prism_ap_fw.bin", 0, 1024 },
437 { "symbol_sp24t_prim_fw", "symbol_sp24t_sec_fw", NULL, 0x00003100, 512 }
David Kilroy3994d502008-08-21 23:27:54 +0100438};
439
440/* Structure used to access fields in FW
441 * Make sure LE decoding macros are used
442 */
443struct orinoco_fw_header {
444 char hdr_vers[6]; /* ASCII string for header version */
445 __le16 headersize; /* Total length of header */
446 __le32 entry_point; /* NIC entry point */
447 __le32 blocks; /* Number of blocks to program */
448 __le32 block_offset; /* Offset of block data from eof header */
449 __le32 pdr_offset; /* Offset to PDR data from eof header */
450 __le32 pri_offset; /* Offset to primary plug data */
451 __le32 compat_offset; /* Offset to compatibility data*/
452 char signature[0]; /* FW signature length headersize-20 */
453} __attribute__ ((packed));
454
455/* Download either STA or AP firmware into the card. */
456static int
457orinoco_dl_firmware(struct orinoco_private *priv,
458 const struct fw_info *fw,
459 int ap)
460{
461 /* Plug Data Area (PDA) */
Andrey Borzenkov70458252008-10-10 21:26:30 +0400462 __le16 *pda;
David Kilroy3994d502008-08-21 23:27:54 +0100463
464 hermes_t *hw = &priv->hw;
465 const struct firmware *fw_entry;
466 const struct orinoco_fw_header *hdr;
467 const unsigned char *first_block;
468 const unsigned char *end;
469 const char *firmware;
470 struct net_device *dev = priv->ndev;
Andrey Borzenkov70458252008-10-10 21:26:30 +0400471 int err = 0;
472
473 pda = kzalloc(fw->pda_size, GFP_KERNEL);
474 if (!pda)
475 return -ENOMEM;
David Kilroy3994d502008-08-21 23:27:54 +0100476
477 if (ap)
478 firmware = fw->ap_fw;
479 else
480 firmware = fw->sta_fw;
481
482 printk(KERN_DEBUG "%s: Attempting to download firmware %s\n",
483 dev->name, firmware);
484
485 /* Read current plug data */
Andrey Borzenkov70458252008-10-10 21:26:30 +0400486 err = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 0);
David Kilroy3994d502008-08-21 23:27:54 +0100487 printk(KERN_DEBUG "%s: Read PDA returned %d\n", dev->name, err);
488 if (err)
Andrey Borzenkov70458252008-10-10 21:26:30 +0400489 goto free;
David Kilroy3994d502008-08-21 23:27:54 +0100490
David Kilroy74734312008-11-22 10:37:25 +0000491 if (!priv->cached_fw) {
Andrey Borzenkov4fb30782008-10-19 12:06:11 +0400492 err = request_firmware(&fw_entry, firmware, priv->dev);
David Kilroy74734312008-11-22 10:37:25 +0000493
Andrey Borzenkov4fb30782008-10-19 12:06:11 +0400494 if (err) {
495 printk(KERN_ERR "%s: Cannot find firmware %s\n",
496 dev->name, firmware);
497 err = -ENOENT;
498 goto free;
499 }
David Kilroy74734312008-11-22 10:37:25 +0000500 } else
501 fw_entry = priv->cached_fw;
David Kilroy3994d502008-08-21 23:27:54 +0100502
503 hdr = (const struct orinoco_fw_header *) fw_entry->data;
504
505 /* Enable aux port to allow programming */
506 err = hermesi_program_init(hw, le32_to_cpu(hdr->entry_point));
507 printk(KERN_DEBUG "%s: Program init returned %d\n", dev->name, err);
508 if (err != 0)
509 goto abort;
510
511 /* Program data */
512 first_block = (fw_entry->data +
513 le16_to_cpu(hdr->headersize) +
514 le32_to_cpu(hdr->block_offset));
515 end = fw_entry->data + fw_entry->size;
516
517 err = hermes_program(hw, first_block, end);
518 printk(KERN_DEBUG "%s: Program returned %d\n", dev->name, err);
519 if (err != 0)
520 goto abort;
521
522 /* Update production data */
523 first_block = (fw_entry->data +
524 le16_to_cpu(hdr->headersize) +
525 le32_to_cpu(hdr->pdr_offset));
526
527 err = hermes_apply_pda_with_defaults(hw, first_block, pda);
528 printk(KERN_DEBUG "%s: Apply PDA returned %d\n", dev->name, err);
529 if (err)
530 goto abort;
531
532 /* Tell card we've finished */
533 err = hermesi_program_end(hw);
534 printk(KERN_DEBUG "%s: Program end returned %d\n", dev->name, err);
535 if (err != 0)
536 goto abort;
537
538 /* Check if we're running */
539 printk(KERN_DEBUG "%s: hermes_present returned %d\n",
540 dev->name, hermes_present(hw));
541
542abort:
David Kilroy74734312008-11-22 10:37:25 +0000543 /* If we requested the firmware, release it. */
544 if (!priv->cached_fw)
Andrey Borzenkov4fb30782008-10-19 12:06:11 +0400545 release_firmware(fw_entry);
Andrey Borzenkov70458252008-10-10 21:26:30 +0400546
547free:
548 kfree(pda);
David Kilroy3994d502008-08-21 23:27:54 +0100549 return err;
550}
551
552/* End markers */
553#define TEXT_END 0x1A /* End of text header */
554
555/*
556 * Process a firmware image - stop the card, load the firmware, reset
557 * the card and make sure it responds. For the secondary firmware take
558 * care of the PDA - read it and then write it on top of the firmware.
559 */
560static int
561symbol_dl_image(struct orinoco_private *priv, const struct fw_info *fw,
562 const unsigned char *image, const unsigned char *end,
563 int secondary)
564{
565 hermes_t *hw = &priv->hw;
Andrey Borzenkov70458252008-10-10 21:26:30 +0400566 int ret = 0;
David Kilroy3994d502008-08-21 23:27:54 +0100567 const unsigned char *ptr;
568 const unsigned char *first_block;
569
570 /* Plug Data Area (PDA) */
Andrey Borzenkov70458252008-10-10 21:26:30 +0400571 __le16 *pda = NULL;
David Kilroy3994d502008-08-21 23:27:54 +0100572
573 /* Binary block begins after the 0x1A marker */
574 ptr = image;
575 while (*ptr++ != TEXT_END);
576 first_block = ptr;
577
578 /* Read the PDA from EEPROM */
579 if (secondary) {
Andrey Borzenkov70458252008-10-10 21:26:30 +0400580 pda = kzalloc(fw->pda_size, GFP_KERNEL);
581 if (!pda)
582 return -ENOMEM;
583
584 ret = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 1);
David Kilroy3994d502008-08-21 23:27:54 +0100585 if (ret)
Andrey Borzenkov70458252008-10-10 21:26:30 +0400586 goto free;
David Kilroy3994d502008-08-21 23:27:54 +0100587 }
588
589 /* Stop the firmware, so that it can be safely rewritten */
590 if (priv->stop_fw) {
591 ret = priv->stop_fw(priv, 1);
592 if (ret)
Andrey Borzenkov70458252008-10-10 21:26:30 +0400593 goto free;
David Kilroy3994d502008-08-21 23:27:54 +0100594 }
595
596 /* Program the adapter with new firmware */
597 ret = hermes_program(hw, first_block, end);
598 if (ret)
Andrey Borzenkov70458252008-10-10 21:26:30 +0400599 goto free;
David Kilroy3994d502008-08-21 23:27:54 +0100600
601 /* Write the PDA to the adapter */
602 if (secondary) {
603 size_t len = hermes_blocks_length(first_block);
604 ptr = first_block + len;
605 ret = hermes_apply_pda(hw, ptr, pda);
Andrey Borzenkov70458252008-10-10 21:26:30 +0400606 kfree(pda);
David Kilroy3994d502008-08-21 23:27:54 +0100607 if (ret)
608 return ret;
609 }
610
611 /* Run the firmware */
612 if (priv->stop_fw) {
613 ret = priv->stop_fw(priv, 0);
614 if (ret)
615 return ret;
616 }
617
618 /* Reset hermes chip and make sure it responds */
619 ret = hermes_init(hw);
620
621 /* hermes_reset() should return 0 with the secondary firmware */
622 if (secondary && ret != 0)
623 return -ENODEV;
624
625 /* And this should work with any firmware */
626 if (!hermes_present(hw))
627 return -ENODEV;
628
629 return 0;
Andrey Borzenkov70458252008-10-10 21:26:30 +0400630
631free:
632 kfree(pda);
633 return ret;
David Kilroy3994d502008-08-21 23:27:54 +0100634}
635
636
637/*
638 * Download the firmware into the card, this also does a PCMCIA soft
639 * reset on the card, to make sure it's in a sane state.
640 */
641static int
642symbol_dl_firmware(struct orinoco_private *priv,
643 const struct fw_info *fw)
644{
645 struct net_device *dev = priv->ndev;
646 int ret;
647 const struct firmware *fw_entry;
648
David Kilroy2cea7b22008-11-22 10:37:26 +0000649 if (!priv->cached_pri_fw) {
650 if (request_firmware(&fw_entry, fw->pri_fw, priv->dev) != 0) {
651 printk(KERN_ERR "%s: Cannot find firmware: %s\n",
652 dev->name, fw->pri_fw);
653 return -ENOENT;
654 }
655 } else
656 fw_entry = priv->cached_pri_fw;
David Kilroy3994d502008-08-21 23:27:54 +0100657
658 /* Load primary firmware */
659 ret = symbol_dl_image(priv, fw, fw_entry->data,
660 fw_entry->data + fw_entry->size, 0);
David Kilroy2cea7b22008-11-22 10:37:26 +0000661
662 if (!priv->cached_pri_fw)
663 release_firmware(fw_entry);
David Kilroy3994d502008-08-21 23:27:54 +0100664 if (ret) {
665 printk(KERN_ERR "%s: Primary firmware download failed\n",
666 dev->name);
667 return ret;
668 }
669
David Kilroy2cea7b22008-11-22 10:37:26 +0000670 if (!priv->cached_fw) {
671 if (request_firmware(&fw_entry, fw->sta_fw, priv->dev) != 0) {
672 printk(KERN_ERR "%s: Cannot find firmware: %s\n",
673 dev->name, fw->sta_fw);
674 return -ENOENT;
675 }
676 } else
677 fw_entry = priv->cached_fw;
David Kilroy3994d502008-08-21 23:27:54 +0100678
679 /* Load secondary firmware */
680 ret = symbol_dl_image(priv, fw, fw_entry->data,
681 fw_entry->data + fw_entry->size, 1);
David Kilroy2cea7b22008-11-22 10:37:26 +0000682 if (!priv->cached_fw)
683 release_firmware(fw_entry);
David Kilroy3994d502008-08-21 23:27:54 +0100684 if (ret) {
685 printk(KERN_ERR "%s: Secondary firmware download failed\n",
686 dev->name);
687 }
688
689 return ret;
690}
691
692static int orinoco_download(struct orinoco_private *priv)
693{
694 int err = 0;
695 /* Reload firmware */
696 switch (priv->firmware_type) {
697 case FIRMWARE_TYPE_AGERE:
698 /* case FIRMWARE_TYPE_INTERSIL: */
699 err = orinoco_dl_firmware(priv,
700 &orinoco_fw[priv->firmware_type], 0);
701 break;
702
703 case FIRMWARE_TYPE_SYMBOL:
704 err = symbol_dl_firmware(priv,
705 &orinoco_fw[priv->firmware_type]);
706 break;
707 case FIRMWARE_TYPE_INTERSIL:
708 break;
709 }
710 /* TODO: if we fail we probably need to reinitialise
711 * the driver */
712
713 return err;
714}
715
David Kilroy39d1ffe2008-11-22 10:37:28 +0000716#if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
David Kilroy74734312008-11-22 10:37:25 +0000717static void orinoco_cache_fw(struct orinoco_private *priv, int ap)
718{
719 const struct firmware *fw_entry = NULL;
David Kilroy2cea7b22008-11-22 10:37:26 +0000720 const char *pri_fw;
David Kilroy74734312008-11-22 10:37:25 +0000721 const char *fw;
722
David Kilroy2cea7b22008-11-22 10:37:26 +0000723 pri_fw = orinoco_fw[priv->firmware_type].pri_fw;
David Kilroy74734312008-11-22 10:37:25 +0000724 if (ap)
725 fw = orinoco_fw[priv->firmware_type].ap_fw;
726 else
727 fw = orinoco_fw[priv->firmware_type].sta_fw;
728
David Kilroy2cea7b22008-11-22 10:37:26 +0000729 if (pri_fw) {
730 if (request_firmware(&fw_entry, pri_fw, priv->dev) == 0)
731 priv->cached_pri_fw = fw_entry;
732 }
733
David Kilroy74734312008-11-22 10:37:25 +0000734 if (fw) {
735 if (request_firmware(&fw_entry, fw, priv->dev) == 0)
736 priv->cached_fw = fw_entry;
737 }
738}
739
740static void orinoco_uncache_fw(struct orinoco_private *priv)
741{
David Kilroy2cea7b22008-11-22 10:37:26 +0000742 if (priv->cached_pri_fw)
743 release_firmware(priv->cached_pri_fw);
David Kilroy74734312008-11-22 10:37:25 +0000744 if (priv->cached_fw)
745 release_firmware(priv->cached_fw);
746
David Kilroy2cea7b22008-11-22 10:37:26 +0000747 priv->cached_pri_fw = NULL;
David Kilroy74734312008-11-22 10:37:25 +0000748 priv->cached_fw = NULL;
749}
David Kilroy39d1ffe2008-11-22 10:37:28 +0000750#else
751#define orinoco_cache_fw(priv, ap)
752#define orinoco_uncache_fw(priv)
753#endif
David Kilroy74734312008-11-22 10:37:25 +0000754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755/********************************************************************/
756/* Device methods */
757/********************************************************************/
758
759static int orinoco_open(struct net_device *dev)
760{
761 struct orinoco_private *priv = netdev_priv(dev);
762 unsigned long flags;
763 int err;
764
765 if (orinoco_lock(priv, &flags) != 0)
766 return -EBUSY;
767
768 err = __orinoco_up(dev);
769
770 if (! err)
771 priv->open = 1;
772
773 orinoco_unlock(priv, &flags);
774
775 return err;
776}
777
Christoph Hellwigad8f4512005-05-14 17:30:17 +0200778static int orinoco_stop(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 struct orinoco_private *priv = netdev_priv(dev);
781 int err = 0;
782
783 /* We mustn't use orinoco_lock() here, because we need to be
784 able to close the interface even if hw_unavailable is set
785 (e.g. as we're released after a PC Card removal) */
786 spin_lock_irq(&priv->lock);
787
788 priv->open = 0;
789
790 err = __orinoco_down(dev);
791
792 spin_unlock_irq(&priv->lock);
793
794 return err;
795}
796
797static struct net_device_stats *orinoco_get_stats(struct net_device *dev)
798{
799 struct orinoco_private *priv = netdev_priv(dev);
800
801 return &priv->stats;
802}
803
804static struct iw_statistics *orinoco_get_wireless_stats(struct net_device *dev)
805{
806 struct orinoco_private *priv = netdev_priv(dev);
807 hermes_t *hw = &priv->hw;
808 struct iw_statistics *wstats = &priv->wstats;
David Gibsone67d9d92005-05-12 20:01:22 -0400809 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 unsigned long flags;
811
812 if (! netif_device_present(dev)) {
813 printk(KERN_WARNING "%s: get_wireless_stats() called while device not present\n",
814 dev->name);
815 return NULL; /* FIXME: Can we do better than this? */
816 }
817
David Gibsone67d9d92005-05-12 20:01:22 -0400818 /* If busy, return the old stats. Returning NULL may cause
819 * the interface to disappear from /proc/net/wireless */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (orinoco_lock(priv, &flags) != 0)
David Gibsone67d9d92005-05-12 20:01:22 -0400821 return wstats;
822
823 /* We can't really wait for the tallies inquiry command to
824 * complete, so we just use the previous results and trigger
825 * a new tallies inquiry command for next time - Jean II */
826 /* FIXME: Really we should wait for the inquiry to come back -
827 * as it is the stats we give don't make a whole lot of sense.
828 * Unfortunately, it's not clear how to do that within the
829 * wireless extensions framework: I think we're in user
830 * context, but a lock seems to be held by the time we get in
831 * here so we're not safe to sleep here. */
832 hermes_inquire(hw, HERMES_INQ_TALLIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 if (priv->iw_mode == IW_MODE_ADHOC) {
835 memset(&wstats->qual, 0, sizeof(wstats->qual));
836 /* If a spy address is defined, we report stats of the
837 * first spy address - Jean II */
838 if (SPY_NUMBER(priv)) {
Pavel Roskin343c6862005-09-09 18:43:02 -0400839 wstats->qual.qual = priv->spy_data.spy_stat[0].qual;
840 wstats->qual.level = priv->spy_data.spy_stat[0].level;
841 wstats->qual.noise = priv->spy_data.spy_stat[0].noise;
842 wstats->qual.updated = priv->spy_data.spy_stat[0].updated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844 } else {
845 struct {
Pavel Roskina208c4e2006-04-07 04:10:26 -0400846 __le16 qual, signal, noise, unused;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 } __attribute__ ((packed)) cq;
848
849 err = HERMES_READ_RECORD(hw, USER_BAP,
850 HERMES_RID_COMMSQUALITY, &cq);
David Gibsone67d9d92005-05-12 20:01:22 -0400851
852 if (!err) {
853 wstats->qual.qual = (int)le16_to_cpu(cq.qual);
854 wstats->qual.level = (int)le16_to_cpu(cq.signal) - 0x95;
855 wstats->qual.noise = (int)le16_to_cpu(cq.noise) - 0x95;
Andrey Borzenkovf941f852008-11-15 17:15:09 +0300856 wstats->qual.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
David Gibsone67d9d92005-05-12 20:01:22 -0400857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return wstats;
862}
863
864static void orinoco_set_multicast_list(struct net_device *dev)
865{
866 struct orinoco_private *priv = netdev_priv(dev);
867 unsigned long flags;
868
869 if (orinoco_lock(priv, &flags) != 0) {
870 printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
871 "called when hw_unavailable\n", dev->name);
872 return;
873 }
874
875 __orinoco_set_multicast_list(dev);
876 orinoco_unlock(priv, &flags);
877}
878
879static int orinoco_change_mtu(struct net_device *dev, int new_mtu)
880{
881 struct orinoco_private *priv = netdev_priv(dev);
882
883 if ( (new_mtu < ORINOCO_MIN_MTU) || (new_mtu > ORINOCO_MAX_MTU) )
884 return -EINVAL;
885
Johannes Berg2c7060022008-10-30 22:09:54 +0100886 /* MTU + encapsulation + header length */
887 if ( (new_mtu + ENCAPS_OVERHEAD + sizeof(struct ieee80211_hdr)) >
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 (priv->nicbuf_size - ETH_HLEN) )
889 return -EINVAL;
890
891 dev->mtu = new_mtu;
892
893 return 0;
894}
895
896/********************************************************************/
897/* Tx path */
898/********************************************************************/
899
900static int orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
901{
902 struct orinoco_private *priv = netdev_priv(dev);
903 struct net_device_stats *stats = &priv->stats;
904 hermes_t *hw = &priv->hw;
905 int err = 0;
906 u16 txfid = priv->txfid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 struct ethhdr *eh;
David Kilroy6eecad72008-08-21 23:27:56 +0100908 int tx_control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 unsigned long flags;
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (! netif_running(dev)) {
912 printk(KERN_ERR "%s: Tx on stopped device!\n",
913 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400914 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
916
917 if (netif_queue_stopped(dev)) {
918 printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
919 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400920 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
922
923 if (orinoco_lock(priv, &flags) != 0) {
924 printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n",
925 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400926 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200929 if (! netif_carrier_ok(dev) || (priv->iw_mode == IW_MODE_MONITOR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 /* Oops, the firmware hasn't established a connection,
931 silently drop the packet (this seems to be the
932 safest approach). */
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400933 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
935
Pavel Roskin8d5be082006-04-07 04:10:41 -0400936 /* Check packet length */
Pavel Roskina28dc812006-04-07 04:10:45 -0400937 if (skb->len < ETH_HLEN)
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400938 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
David Kilroy6eecad72008-08-21 23:27:56 +0100940 tx_control = HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
David Kilroy23edcc42008-08-21 23:28:05 +0100942 if (priv->encode_alg == IW_ENCODE_ALG_TKIP)
943 tx_control |= (priv->tx_key << HERMES_MIC_KEY_ID_SHIFT) |
944 HERMES_TXCTRL_MIC;
945
David Kilroy6eecad72008-08-21 23:27:56 +0100946 if (priv->has_alt_txcntl) {
947 /* WPA enabled firmwares have tx_cntl at the end of
948 * the 802.11 header. So write zeroed descriptor and
949 * 802.11 header at the same time
950 */
951 char desc[HERMES_802_3_OFFSET];
952 __le16 *txcntl = (__le16 *) &desc[HERMES_TXCNTL2_OFFSET];
953
954 memset(&desc, 0, sizeof(desc));
955
956 *txcntl = cpu_to_le16(tx_control);
957 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
958 txfid, 0);
959 if (err) {
960 if (net_ratelimit())
961 printk(KERN_ERR "%s: Error %d writing Tx "
962 "descriptor to BAP\n", dev->name, err);
963 goto busy;
964 }
965 } else {
966 struct hermes_tx_descriptor desc;
967
968 memset(&desc, 0, sizeof(desc));
969
970 desc.tx_control = cpu_to_le16(tx_control);
971 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
972 txfid, 0);
973 if (err) {
974 if (net_ratelimit())
975 printk(KERN_ERR "%s: Error %d writing Tx "
976 "descriptor to BAP\n", dev->name, err);
977 goto busy;
978 }
979
980 /* Clear the 802.11 header and data length fields - some
981 * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
982 * if this isn't done. */
983 hermes_clear_words(hw, HERMES_DATA0,
984 HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
David Kilroy23edcc42008-08-21 23:28:05 +0100987 eh = (struct ethhdr *)skb->data;
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 /* Encapsulate Ethernet-II frames */
990 if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
Pavel Roskina28dc812006-04-07 04:10:45 -0400991 struct header_struct {
992 struct ethhdr eth; /* 802.3 header */
993 u8 encap[6]; /* 802.2 header */
994 } __attribute__ ((packed)) hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Pavel Roskina28dc812006-04-07 04:10:45 -0400996 /* Strip destination and source from the data */
997 skb_pull(skb, 2 * ETH_ALEN);
Pavel Roskina28dc812006-04-07 04:10:45 -0400998
999 /* And move them to a separate header */
1000 memcpy(&hdr.eth, eh, 2 * ETH_ALEN);
1001 hdr.eth.h_proto = htons(sizeof(encaps_hdr) + skb->len);
1002 memcpy(hdr.encap, encaps_hdr, sizeof(encaps_hdr));
1003
David Kilroy23edcc42008-08-21 23:28:05 +01001004 /* Insert the SNAP header */
1005 if (skb_headroom(skb) < sizeof(hdr)) {
1006 printk(KERN_ERR
1007 "%s: Not enough headroom for 802.2 headers %d\n",
1008 dev->name, skb_headroom(skb));
1009 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
David Kilroy23edcc42008-08-21 23:28:05 +01001011 eh = (struct ethhdr *) skb_push(skb, sizeof(hdr));
1012 memcpy(eh, &hdr, sizeof(hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
1014
Pavel Roskina28dc812006-04-07 04:10:45 -04001015 err = hermes_bap_pwrite(hw, USER_BAP, skb->data, skb->len,
David Kilroy23edcc42008-08-21 23:28:05 +01001016 txfid, HERMES_802_3_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (err) {
1018 printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
1019 dev->name, err);
Pavel Roskin470e2aa2006-04-07 04:10:43 -04001020 goto busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
1022
David Kilroy23edcc42008-08-21 23:28:05 +01001023 /* Calculate Michael MIC */
1024 if (priv->encode_alg == IW_ENCODE_ALG_TKIP) {
1025 u8 mic_buf[MICHAEL_MIC_LEN + 1];
1026 u8 *mic;
1027 size_t offset;
1028 size_t len;
1029
1030 if (skb->len % 2) {
1031 /* MIC start is on an odd boundary */
1032 mic_buf[0] = skb->data[skb->len - 1];
1033 mic = &mic_buf[1];
1034 offset = skb->len - 1;
1035 len = MICHAEL_MIC_LEN + 1;
1036 } else {
1037 mic = &mic_buf[0];
1038 offset = skb->len;
1039 len = MICHAEL_MIC_LEN;
1040 }
1041
1042 michael_mic(priv->tx_tfm_mic,
1043 priv->tkip_key[priv->tx_key].tx_mic,
1044 eh->h_dest, eh->h_source, 0 /* priority */,
1045 skb->data + ETH_HLEN, skb->len - ETH_HLEN, mic);
1046
1047 /* Write the MIC */
1048 err = hermes_bap_pwrite(hw, USER_BAP, &mic_buf[0], len,
1049 txfid, HERMES_802_3_OFFSET + offset);
1050 if (err) {
1051 printk(KERN_ERR "%s: Error %d writing MIC to BAP\n",
1052 dev->name, err);
1053 goto busy;
1054 }
1055 }
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 /* Finally, we actually initiate the send */
1058 netif_stop_queue(dev);
1059
1060 err = hermes_docmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
1061 txfid, NULL);
1062 if (err) {
1063 netif_start_queue(dev);
Andrew Mortonc367c212005-10-19 21:23:44 -07001064 if (net_ratelimit())
1065 printk(KERN_ERR "%s: Error %d transmitting packet\n",
1066 dev->name, err);
Pavel Roskin470e2aa2006-04-07 04:10:43 -04001067 goto busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069
1070 dev->trans_start = jiffies;
David Kilroy23edcc42008-08-21 23:28:05 +01001071 stats->tx_bytes += HERMES_802_3_OFFSET + skb->len;
Pavel Roskin470e2aa2006-04-07 04:10:43 -04001072 goto ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Pavel Roskin470e2aa2006-04-07 04:10:43 -04001074 drop:
1075 stats->tx_errors++;
1076 stats->tx_dropped++;
1077
1078 ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 dev_kfree_skb(skb);
Pavel Roskinb34b8672006-04-07 04:10:36 -04001081 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Pavel Roskin470e2aa2006-04-07 04:10:43 -04001083 busy:
Jiri Benc2c1bd262006-04-07 04:10:47 -04001084 if (err == -EIO)
1085 schedule_work(&priv->reset_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 orinoco_unlock(priv, &flags);
Pavel Roskinb34b8672006-04-07 04:10:36 -04001087 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
1090static void __orinoco_ev_alloc(struct net_device *dev, hermes_t *hw)
1091{
1092 struct orinoco_private *priv = netdev_priv(dev);
1093 u16 fid = hermes_read_regn(hw, ALLOCFID);
1094
1095 if (fid != priv->txfid) {
1096 if (fid != DUMMY_FID)
1097 printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
1098 dev->name, fid);
1099 return;
1100 }
1101
1102 hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
1103}
1104
1105static void __orinoco_ev_tx(struct net_device *dev, hermes_t *hw)
1106{
1107 struct orinoco_private *priv = netdev_priv(dev);
1108 struct net_device_stats *stats = &priv->stats;
1109
1110 stats->tx_packets++;
1111
1112 netif_wake_queue(dev);
1113
1114 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
1115}
1116
1117static void __orinoco_ev_txexc(struct net_device *dev, hermes_t *hw)
1118{
1119 struct orinoco_private *priv = netdev_priv(dev);
1120 struct net_device_stats *stats = &priv->stats;
1121 u16 fid = hermes_read_regn(hw, TXCOMPLFID);
Pavel Roskind133ae42005-09-23 04:18:06 -04001122 u16 status;
Pavel Roskin30c2d3b2006-04-07 04:10:34 -04001123 struct hermes_txexc_data hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 int err = 0;
1125
1126 if (fid == DUMMY_FID)
1127 return; /* Nothing's really happened */
1128
Pavel Roskin48ca7032005-09-23 04:18:06 -04001129 /* Read part of the frame header - we need status and addr1 */
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001130 err = hermes_bap_pread(hw, IRQ_BAP, &hdr,
Pavel Roskin30c2d3b2006-04-07 04:10:34 -04001131 sizeof(struct hermes_txexc_data),
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001132 fid, 0);
1133
1134 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
1135 stats->tx_errors++;
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (err) {
1138 printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
1139 "(FID=%04X error %d)\n",
1140 dev->name, fid, err);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001141 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001144 DEBUG(1, "%s: Tx error, err %d (FID=%04X)\n", dev->name,
1145 err, fid);
1146
1147 /* We produce a TXDROP event only for retry or lifetime
1148 * exceeded, because that's the only status that really mean
1149 * that this particular node went away.
1150 * Other errors means that *we* screwed up. - Jean II */
Pavel Roskin30c2d3b2006-04-07 04:10:34 -04001151 status = le16_to_cpu(hdr.desc.status);
Pavel Roskind133ae42005-09-23 04:18:06 -04001152 if (status & (HERMES_TXSTAT_RETRYERR | HERMES_TXSTAT_AGEDERR)) {
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001153 union iwreq_data wrqu;
1154
1155 /* Copy 802.11 dest address.
1156 * We use the 802.11 header because the frame may
1157 * not be 802.3 or may be mangled...
1158 * In Ad-Hoc mode, it will be the node address.
1159 * In managed mode, it will be most likely the AP addr
1160 * User space will figure out how to convert it to
1161 * whatever it needs (IP address or else).
1162 * - Jean II */
1163 memcpy(wrqu.addr.sa_data, hdr.addr1, ETH_ALEN);
1164 wrqu.addr.sa_family = ARPHRD_ETHER;
1165
1166 /* Send event to user space */
1167 wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
1168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
1172
1173static void orinoco_tx_timeout(struct net_device *dev)
1174{
1175 struct orinoco_private *priv = netdev_priv(dev);
1176 struct net_device_stats *stats = &priv->stats;
1177 struct hermes *hw = &priv->hw;
1178
1179 printk(KERN_WARNING "%s: Tx timeout! "
1180 "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
1181 dev->name, hermes_read_regn(hw, ALLOCFID),
1182 hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
1183
1184 stats->tx_errors++;
1185
1186 schedule_work(&priv->reset_work);
1187}
1188
1189/********************************************************************/
1190/* Rx path (data frames) */
1191/********************************************************************/
1192
1193/* Does the frame have a SNAP header indicating it should be
1194 * de-encapsulated to Ethernet-II? */
1195static inline int is_ethersnap(void *_hdr)
1196{
1197 u8 *hdr = _hdr;
1198
1199 /* We de-encapsulate all packets which, a) have SNAP headers
1200 * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
1201 * and where b) the OUI of the SNAP header is 00:00:00 or
1202 * 00:00:f8 - we need both because different APs appear to use
1203 * different OUIs for some reason */
1204 return (memcmp(hdr, &encaps_hdr, 5) == 0)
1205 && ( (hdr[5] == 0x00) || (hdr[5] == 0xf8) );
1206}
1207
1208static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
1209 int level, int noise)
1210{
Pavel Roskin343c6862005-09-09 18:43:02 -04001211 struct iw_quality wstats;
1212 wstats.level = level - 0x95;
1213 wstats.noise = noise - 0x95;
1214 wstats.qual = (level > noise) ? (level - noise) : 0;
Andrey Borzenkovf941f852008-11-15 17:15:09 +03001215 wstats.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
Pavel Roskin343c6862005-09-09 18:43:02 -04001216 /* Update spy records */
1217 wireless_spy_update(dev, mac, &wstats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
1220static void orinoco_stat_gather(struct net_device *dev,
1221 struct sk_buff *skb,
1222 struct hermes_rx_descriptor *desc)
1223{
1224 struct orinoco_private *priv = netdev_priv(dev);
1225
1226 /* Using spy support with lots of Rx packets, like in an
1227 * infrastructure (AP), will really slow down everything, because
1228 * the MAC address must be compared to each entry of the spy list.
1229 * If the user really asks for it (set some address in the
1230 * spy list), we do it, but he will pay the price.
1231 * Note that to get here, you need both WIRELESS_SPY
1232 * compiled in AND some addresses in the list !!!
1233 */
1234 /* Note : gcc will optimise the whole section away if
1235 * WIRELESS_SPY is not defined... - Jean II */
1236 if (SPY_NUMBER(priv)) {
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001237 orinoco_spy_gather(dev, skb_mac_header(skb) + ETH_ALEN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 desc->signal, desc->silence);
1239 }
1240}
1241
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001242/*
1243 * orinoco_rx_monitor - handle received monitor frames.
1244 *
1245 * Arguments:
1246 * dev network device
1247 * rxfid received FID
1248 * desc rx descriptor of the frame
1249 *
1250 * Call context: interrupt
1251 */
1252static void orinoco_rx_monitor(struct net_device *dev, u16 rxfid,
1253 struct hermes_rx_descriptor *desc)
1254{
1255 u32 hdrlen = 30; /* return full header by default */
1256 u32 datalen = 0;
1257 u16 fc;
1258 int err;
1259 int len;
1260 struct sk_buff *skb;
1261 struct orinoco_private *priv = netdev_priv(dev);
1262 struct net_device_stats *stats = &priv->stats;
1263 hermes_t *hw = &priv->hw;
1264
1265 len = le16_to_cpu(desc->data_len);
1266
1267 /* Determine the size of the header and the data */
1268 fc = le16_to_cpu(desc->frame_ctl);
1269 switch (fc & IEEE80211_FCTL_FTYPE) {
1270 case IEEE80211_FTYPE_DATA:
1271 if ((fc & IEEE80211_FCTL_TODS)
1272 && (fc & IEEE80211_FCTL_FROMDS))
1273 hdrlen = 30;
1274 else
1275 hdrlen = 24;
1276 datalen = len;
1277 break;
1278 case IEEE80211_FTYPE_MGMT:
1279 hdrlen = 24;
1280 datalen = len;
1281 break;
1282 case IEEE80211_FTYPE_CTL:
1283 switch (fc & IEEE80211_FCTL_STYPE) {
1284 case IEEE80211_STYPE_PSPOLL:
1285 case IEEE80211_STYPE_RTS:
1286 case IEEE80211_STYPE_CFEND:
1287 case IEEE80211_STYPE_CFENDACK:
1288 hdrlen = 16;
1289 break;
1290 case IEEE80211_STYPE_CTS:
1291 case IEEE80211_STYPE_ACK:
1292 hdrlen = 10;
1293 break;
1294 }
1295 break;
1296 default:
1297 /* Unknown frame type */
1298 break;
1299 }
1300
1301 /* sanity check the length */
Johannes Berg2c7060022008-10-30 22:09:54 +01001302 if (datalen > IEEE80211_MAX_DATA_LEN + 12) {
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001303 printk(KERN_DEBUG "%s: oversized monitor frame, "
1304 "data length = %d\n", dev->name, datalen);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001305 stats->rx_length_errors++;
1306 goto update_stats;
1307 }
1308
1309 skb = dev_alloc_skb(hdrlen + datalen);
1310 if (!skb) {
1311 printk(KERN_WARNING "%s: Cannot allocate skb for monitor frame\n",
1312 dev->name);
Florin Malitabb6e0932006-05-22 22:35:30 -07001313 goto update_stats;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001314 }
1315
1316 /* Copy the 802.11 header to the skb */
1317 memcpy(skb_put(skb, hdrlen), &(desc->frame_ctl), hdrlen);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001318 skb_reset_mac_header(skb);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001319
1320 /* If any, copy the data from the card to the skb */
1321 if (datalen > 0) {
1322 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, datalen),
1323 ALIGN(datalen, 2), rxfid,
1324 HERMES_802_2_OFFSET);
1325 if (err) {
1326 printk(KERN_ERR "%s: error %d reading monitor frame\n",
1327 dev->name, err);
1328 goto drop;
1329 }
1330 }
1331
1332 skb->dev = dev;
1333 skb->ip_summed = CHECKSUM_NONE;
1334 skb->pkt_type = PACKET_OTHERHOST;
1335 skb->protocol = __constant_htons(ETH_P_802_2);
1336
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001337 stats->rx_packets++;
1338 stats->rx_bytes += skb->len;
1339
1340 netif_rx(skb);
1341 return;
1342
1343 drop:
1344 dev_kfree_skb_irq(skb);
1345 update_stats:
1346 stats->rx_errors++;
1347 stats->rx_dropped++;
1348}
1349
David Kilroy23edcc42008-08-21 23:28:05 +01001350/* Get tsc from the firmware */
1351static int orinoco_hw_get_tkip_iv(struct orinoco_private *priv, int key,
1352 u8 *tsc)
1353{
1354 hermes_t *hw = &priv->hw;
1355 int err = 0;
1356 u8 tsc_arr[4][IW_ENCODE_SEQ_MAX_SIZE];
1357
1358 if ((key < 0) || (key > 4))
1359 return -EINVAL;
1360
1361 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_TKIP_IV,
1362 sizeof(tsc_arr), NULL, &tsc_arr);
1363 if (!err)
1364 memcpy(tsc, &tsc_arr[key][0], sizeof(tsc_arr[0]));
1365
1366 return err;
1367}
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369static void __orinoco_ev_rx(struct net_device *dev, hermes_t *hw)
1370{
1371 struct orinoco_private *priv = netdev_priv(dev);
1372 struct net_device_stats *stats = &priv->stats;
1373 struct iw_statistics *wstats = &priv->wstats;
1374 struct sk_buff *skb = NULL;
David Kilroy31afcef2008-08-21 23:28:04 +01001375 u16 rxfid, status;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001376 int length;
David Kilroy31afcef2008-08-21 23:28:04 +01001377 struct hermes_rx_descriptor *desc;
1378 struct orinoco_rx_data *rx_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 int err;
1380
David Kilroy31afcef2008-08-21 23:28:04 +01001381 desc = kmalloc(sizeof(*desc), GFP_ATOMIC);
1382 if (!desc) {
1383 printk(KERN_WARNING
1384 "%s: Can't allocate space for RX descriptor\n",
1385 dev->name);
1386 goto update_stats;
1387 }
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 rxfid = hermes_read_regn(hw, RXFID);
1390
David Kilroy31afcef2008-08-21 23:28:04 +01001391 err = hermes_bap_pread(hw, IRQ_BAP, desc, sizeof(*desc),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 rxfid, 0);
1393 if (err) {
1394 printk(KERN_ERR "%s: error %d reading Rx descriptor. "
1395 "Frame dropped.\n", dev->name, err);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001396 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
1398
David Kilroy31afcef2008-08-21 23:28:04 +01001399 status = le16_to_cpu(desc->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001401 if (status & HERMES_RXSTAT_BADCRC) {
1402 DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n",
1403 dev->name);
1404 stats->rx_crc_errors++;
1405 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001408 /* Handle frames in monitor mode */
1409 if (priv->iw_mode == IW_MODE_MONITOR) {
David Kilroy31afcef2008-08-21 23:28:04 +01001410 orinoco_rx_monitor(dev, rxfid, desc);
1411 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 }
1413
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001414 if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
1415 DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
1416 dev->name);
1417 wstats->discard.code++;
1418 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
1420
David Kilroy31afcef2008-08-21 23:28:04 +01001421 length = le16_to_cpu(desc->data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 /* Sanity checks */
1424 if (length < 3) { /* No for even an 802.2 LLC header */
1425 /* At least on Symbol firmware with PCF we get quite a
1426 lot of these legitimately - Poll frames with no
1427 data. */
David Kilroy31afcef2008-08-21 23:28:04 +01001428 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
Johannes Berg2c7060022008-10-30 22:09:54 +01001430 if (length > IEEE80211_MAX_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
1432 dev->name, length);
1433 stats->rx_length_errors++;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001434 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
1436
David Kilroy23edcc42008-08-21 23:28:05 +01001437 /* Payload size does not include Michael MIC. Increase payload
1438 * size to read it together with the data. */
1439 if (status & HERMES_RXSTAT_MIC)
1440 length += MICHAEL_MIC_LEN;
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 /* We need space for the packet data itself, plus an ethernet
1443 header, plus 2 bytes so we can align the IP header on a
1444 32bit boundary, plus 1 byte so we can read in odd length
1445 packets from the card, which has an IO granularity of 16
1446 bits */
1447 skb = dev_alloc_skb(length+ETH_HLEN+2+1);
1448 if (!skb) {
1449 printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
1450 dev->name);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001451 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
1453
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001454 /* We'll prepend the header, so reserve space for it. The worst
1455 case is no decapsulation, when 802.3 header is prepended and
1456 nothing is removed. 2 is for aligning the IP header. */
1457 skb_reserve(skb, ETH_HLEN + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001459 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, length),
1460 ALIGN(length, 2), rxfid,
1461 HERMES_802_2_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (err) {
1463 printk(KERN_ERR "%s: error %d reading frame. "
1464 "Frame dropped.\n", dev->name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 goto drop;
1466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
David Kilroy31afcef2008-08-21 23:28:04 +01001468 /* Add desc and skb to rx queue */
1469 rx_data = kzalloc(sizeof(*rx_data), GFP_ATOMIC);
1470 if (!rx_data) {
1471 printk(KERN_WARNING "%s: Can't allocate RX packet\n",
1472 dev->name);
1473 goto drop;
1474 }
1475 rx_data->desc = desc;
1476 rx_data->skb = skb;
1477 list_add_tail(&rx_data->list, &priv->rx_list);
1478 tasklet_schedule(&priv->rx_tasklet);
1479
1480 return;
1481
1482drop:
1483 dev_kfree_skb_irq(skb);
1484update_stats:
1485 stats->rx_errors++;
1486 stats->rx_dropped++;
1487out:
1488 kfree(desc);
1489}
1490
1491static void orinoco_rx(struct net_device *dev,
1492 struct hermes_rx_descriptor *desc,
1493 struct sk_buff *skb)
1494{
1495 struct orinoco_private *priv = netdev_priv(dev);
1496 struct net_device_stats *stats = &priv->stats;
1497 u16 status, fc;
1498 int length;
1499 struct ethhdr *hdr;
1500
1501 status = le16_to_cpu(desc->status);
1502 length = le16_to_cpu(desc->data_len);
1503 fc = le16_to_cpu(desc->frame_ctl);
1504
David Kilroy23edcc42008-08-21 23:28:05 +01001505 /* Calculate and check MIC */
1506 if (status & HERMES_RXSTAT_MIC) {
1507 int key_id = ((status & HERMES_RXSTAT_MIC_KEY_ID) >>
1508 HERMES_MIC_KEY_ID_SHIFT);
1509 u8 mic[MICHAEL_MIC_LEN];
1510 u8 *rxmic;
1511 u8 *src = (fc & IEEE80211_FCTL_FROMDS) ?
1512 desc->addr3 : desc->addr2;
1513
1514 /* Extract Michael MIC from payload */
1515 rxmic = skb->data + skb->len - MICHAEL_MIC_LEN;
1516
1517 skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
1518 length -= MICHAEL_MIC_LEN;
1519
1520 michael_mic(priv->rx_tfm_mic,
1521 priv->tkip_key[key_id].rx_mic,
1522 desc->addr1,
1523 src,
1524 0, /* priority or QoS? */
1525 skb->data,
1526 skb->len,
1527 &mic[0]);
1528
1529 if (memcmp(mic, rxmic,
1530 MICHAEL_MIC_LEN)) {
1531 union iwreq_data wrqu;
1532 struct iw_michaelmicfailure wxmic;
David Kilroy23edcc42008-08-21 23:28:05 +01001533
1534 printk(KERN_WARNING "%s: "
Johannes Berge1749612008-10-27 15:59:26 -07001535 "Invalid Michael MIC in data frame from %pM, "
David Kilroy23edcc42008-08-21 23:28:05 +01001536 "using key %i\n",
Johannes Berge1749612008-10-27 15:59:26 -07001537 dev->name, src, key_id);
David Kilroy23edcc42008-08-21 23:28:05 +01001538
1539 /* TODO: update stats */
1540
1541 /* Notify userspace */
1542 memset(&wxmic, 0, sizeof(wxmic));
1543 wxmic.flags = key_id & IW_MICFAILURE_KEY_ID;
1544 wxmic.flags |= (desc->addr1[0] & 1) ?
1545 IW_MICFAILURE_GROUP : IW_MICFAILURE_PAIRWISE;
1546 wxmic.src_addr.sa_family = ARPHRD_ETHER;
1547 memcpy(wxmic.src_addr.sa_data, src, ETH_ALEN);
1548
1549 (void) orinoco_hw_get_tkip_iv(priv, key_id,
1550 &wxmic.tsc[0]);
1551
1552 memset(&wrqu, 0, sizeof(wrqu));
1553 wrqu.data.length = sizeof(wxmic);
1554 wireless_send_event(dev, IWEVMICHAELMICFAILURE, &wrqu,
1555 (char *) &wxmic);
1556
1557 goto drop;
1558 }
1559 }
1560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 /* Handle decapsulation
1562 * In most cases, the firmware tell us about SNAP frames.
1563 * For some reason, the SNAP frames sent by LinkSys APs
1564 * are not properly recognised by most firmwares.
1565 * So, check ourselves */
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001566 if (length >= ENCAPS_OVERHEAD &&
1567 (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
1568 ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
1569 is_ethersnap(skb->data))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 /* These indicate a SNAP within 802.2 LLC within
1571 802.11 frame which we'll need to de-encapsulate to
1572 the original EthernetII frame. */
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001573 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN - ENCAPS_OVERHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 } else {
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001575 /* 802.3 frame - prepend 802.3 header as is */
1576 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN);
1577 hdr->h_proto = htons(length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 }
David Kilroy31afcef2008-08-21 23:28:04 +01001579 memcpy(hdr->h_dest, desc->addr1, ETH_ALEN);
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001580 if (fc & IEEE80211_FCTL_FROMDS)
David Kilroy31afcef2008-08-21 23:28:04 +01001581 memcpy(hdr->h_source, desc->addr3, ETH_ALEN);
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001582 else
David Kilroy31afcef2008-08-21 23:28:04 +01001583 memcpy(hdr->h_source, desc->addr2, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 skb->protocol = eth_type_trans(skb, dev);
1586 skb->ip_summed = CHECKSUM_NONE;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001587 if (fc & IEEE80211_FCTL_TODS)
1588 skb->pkt_type = PACKET_OTHERHOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 /* Process the wireless stats if needed */
David Kilroy31afcef2008-08-21 23:28:04 +01001591 orinoco_stat_gather(dev, skb, desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
1593 /* Pass the packet to the networking stack */
1594 netif_rx(skb);
1595 stats->rx_packets++;
1596 stats->rx_bytes += length;
1597
1598 return;
David Kilroy23edcc42008-08-21 23:28:05 +01001599
1600 drop:
1601 dev_kfree_skb(skb);
1602 stats->rx_errors++;
1603 stats->rx_dropped++;
David Kilroy31afcef2008-08-21 23:28:04 +01001604}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
David Kilroy31afcef2008-08-21 23:28:04 +01001606static void orinoco_rx_isr_tasklet(unsigned long data)
1607{
1608 struct net_device *dev = (struct net_device *) data;
1609 struct orinoco_private *priv = netdev_priv(dev);
1610 struct orinoco_rx_data *rx_data, *temp;
1611 struct hermes_rx_descriptor *desc;
1612 struct sk_buff *skb;
1613
1614 /* extract desc and skb from queue */
1615 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
1616 desc = rx_data->desc;
1617 skb = rx_data->skb;
1618 list_del(&rx_data->list);
1619 kfree(rx_data);
1620
1621 orinoco_rx(dev, desc, skb);
1622
1623 kfree(desc);
1624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
1626
1627/********************************************************************/
1628/* Rx path (info frames) */
1629/********************************************************************/
1630
1631static void print_linkstatus(struct net_device *dev, u16 status)
1632{
1633 char * s;
1634
1635 if (suppress_linkstatus)
1636 return;
1637
1638 switch (status) {
1639 case HERMES_LINKSTATUS_NOT_CONNECTED:
1640 s = "Not Connected";
1641 break;
1642 case HERMES_LINKSTATUS_CONNECTED:
1643 s = "Connected";
1644 break;
1645 case HERMES_LINKSTATUS_DISCONNECTED:
1646 s = "Disconnected";
1647 break;
1648 case HERMES_LINKSTATUS_AP_CHANGE:
1649 s = "AP Changed";
1650 break;
1651 case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
1652 s = "AP Out of Range";
1653 break;
1654 case HERMES_LINKSTATUS_AP_IN_RANGE:
1655 s = "AP In Range";
1656 break;
1657 case HERMES_LINKSTATUS_ASSOC_FAILED:
1658 s = "Association Failed";
1659 break;
1660 default:
1661 s = "UNKNOWN";
1662 }
1663
1664 printk(KERN_INFO "%s: New link status: %s (%04x)\n",
1665 dev->name, s, status);
1666}
1667
Christoph Hellwig16739b02005-06-19 01:27:51 +02001668/* Search scan results for requested BSSID, join it if found */
David Howellsc4028952006-11-22 14:57:56 +00001669static void orinoco_join_ap(struct work_struct *work)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001670{
David Howellsc4028952006-11-22 14:57:56 +00001671 struct orinoco_private *priv =
1672 container_of(work, struct orinoco_private, join_work);
1673 struct net_device *dev = priv->ndev;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001674 struct hermes *hw = &priv->hw;
1675 int err;
1676 unsigned long flags;
1677 struct join_req {
1678 u8 bssid[ETH_ALEN];
Pavel Roskind133ae42005-09-23 04:18:06 -04001679 __le16 channel;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001680 } __attribute__ ((packed)) req;
1681 const int atom_len = offsetof(struct prism2_scan_apinfo, atim);
Pavel Roskinc89cc222005-09-01 20:06:06 -04001682 struct prism2_scan_apinfo *atom = NULL;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001683 int offset = 4;
Pavel Roskinc89cc222005-09-01 20:06:06 -04001684 int found = 0;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001685 u8 *buf;
1686 u16 len;
1687
1688 /* Allocate buffer for scan results */
1689 buf = kmalloc(MAX_SCAN_LEN, GFP_KERNEL);
1690 if (! buf)
1691 return;
1692
1693 if (orinoco_lock(priv, &flags) != 0)
Pavel Roskinf3cb4cc2005-09-23 04:18:06 -04001694 goto fail_lock;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001695
1696 /* Sanity checks in case user changed something in the meantime */
1697 if (! priv->bssid_fixed)
1698 goto out;
1699
1700 if (strlen(priv->desired_essid) == 0)
1701 goto out;
1702
1703 /* Read scan results from the firmware */
1704 err = hermes_read_ltv(hw, USER_BAP,
1705 HERMES_RID_SCANRESULTSTABLE,
1706 MAX_SCAN_LEN, &len, buf);
1707 if (err) {
1708 printk(KERN_ERR "%s: Cannot read scan results\n",
1709 dev->name);
1710 goto out;
1711 }
1712
1713 len = HERMES_RECLEN_TO_BYTES(len);
1714
1715 /* Go through the scan results looking for the channel of the AP
1716 * we were requested to join */
1717 for (; offset + atom_len <= len; offset += atom_len) {
1718 atom = (struct prism2_scan_apinfo *) (buf + offset);
Pavel Roskinc89cc222005-09-01 20:06:06 -04001719 if (memcmp(&atom->bssid, priv->desired_bssid, ETH_ALEN) == 0) {
1720 found = 1;
1721 break;
1722 }
Christoph Hellwig16739b02005-06-19 01:27:51 +02001723 }
1724
Pavel Roskinc89cc222005-09-01 20:06:06 -04001725 if (! found) {
1726 DEBUG(1, "%s: Requested AP not found in scan results\n",
1727 dev->name);
1728 goto out;
1729 }
Christoph Hellwig16739b02005-06-19 01:27:51 +02001730
Christoph Hellwig16739b02005-06-19 01:27:51 +02001731 memcpy(req.bssid, priv->desired_bssid, ETH_ALEN);
1732 req.channel = atom->channel; /* both are little-endian */
1733 err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNFJOINREQUEST,
1734 &req);
1735 if (err)
1736 printk(KERN_ERR "%s: Error issuing join request\n", dev->name);
1737
1738 out:
Christoph Hellwig16739b02005-06-19 01:27:51 +02001739 orinoco_unlock(priv, &flags);
Pavel Roskinf3cb4cc2005-09-23 04:18:06 -04001740
1741 fail_lock:
1742 kfree(buf);
Christoph Hellwig16739b02005-06-19 01:27:51 +02001743}
1744
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001745/* Send new BSSID to userspace */
David Kilroy6cd90b12008-08-21 23:28:00 +01001746static void orinoco_send_bssid_wevent(struct orinoco_private *priv)
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001747{
David Howellsc4028952006-11-22 14:57:56 +00001748 struct net_device *dev = priv->ndev;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001749 struct hermes *hw = &priv->hw;
1750 union iwreq_data wrqu;
1751 int err;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001752
1753 err = hermes_read_ltv(hw, IRQ_BAP, HERMES_RID_CURRENTBSSID,
1754 ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
1755 if (err != 0)
David Kilroy6cd90b12008-08-21 23:28:00 +01001756 return;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001757
1758 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1759
1760 /* Send event to user space */
1761 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001762}
1763
David Kilroy06009fd2008-08-21 23:28:03 +01001764static void orinoco_send_assocreqie_wevent(struct orinoco_private *priv)
1765{
1766 struct net_device *dev = priv->ndev;
1767 struct hermes *hw = &priv->hw;
1768 union iwreq_data wrqu;
1769 int err;
1770 u8 buf[88];
1771 u8 *ie;
1772
1773 if (!priv->has_wpa)
1774 return;
1775
1776 err = hermes_read_ltv(hw, IRQ_BAP, HERMES_RID_CURRENT_ASSOC_REQ_INFO,
1777 sizeof(buf), NULL, &buf);
1778 if (err != 0)
1779 return;
1780
1781 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1782 if (ie) {
1783 int rem = sizeof(buf) - (ie - &buf[0]);
1784 wrqu.data.length = ie[1] + 2;
1785 if (wrqu.data.length > rem)
1786 wrqu.data.length = rem;
1787
1788 if (wrqu.data.length)
1789 /* Send event to user space */
1790 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, ie);
1791 }
1792}
1793
1794static void orinoco_send_assocrespie_wevent(struct orinoco_private *priv)
1795{
1796 struct net_device *dev = priv->ndev;
1797 struct hermes *hw = &priv->hw;
1798 union iwreq_data wrqu;
1799 int err;
1800 u8 buf[88]; /* TODO: verify max size or IW_GENERIC_IE_MAX */
1801 u8 *ie;
1802
1803 if (!priv->has_wpa)
1804 return;
1805
1806 err = hermes_read_ltv(hw, IRQ_BAP, HERMES_RID_CURRENT_ASSOC_RESP_INFO,
1807 sizeof(buf), NULL, &buf);
1808 if (err != 0)
1809 return;
1810
1811 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1812 if (ie) {
1813 int rem = sizeof(buf) - (ie - &buf[0]);
1814 wrqu.data.length = ie[1] + 2;
1815 if (wrqu.data.length > rem)
1816 wrqu.data.length = rem;
1817
1818 if (wrqu.data.length)
1819 /* Send event to user space */
1820 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, ie);
1821 }
1822}
1823
David Kilroy6cd90b12008-08-21 23:28:00 +01001824static void orinoco_send_wevents(struct work_struct *work)
1825{
1826 struct orinoco_private *priv =
1827 container_of(work, struct orinoco_private, wevent_work);
1828 unsigned long flags;
1829
1830 if (orinoco_lock(priv, &flags) != 0)
1831 return;
1832
David Kilroy06009fd2008-08-21 23:28:03 +01001833 orinoco_send_assocreqie_wevent(priv);
1834 orinoco_send_assocrespie_wevent(priv);
David Kilroy6cd90b12008-08-21 23:28:00 +01001835 orinoco_send_bssid_wevent(priv);
1836
1837 orinoco_unlock(priv, &flags);
1838}
Dan Williams1e3428e2007-10-10 23:56:25 -04001839
1840static inline void orinoco_clear_scan_results(struct orinoco_private *priv,
1841 unsigned long scan_age)
1842{
David Kilroy01632fa2008-08-21 23:27:58 +01001843 if (priv->has_ext_scan) {
1844 struct xbss_element *bss;
1845 struct xbss_element *tmp_bss;
Dan Williams1e3428e2007-10-10 23:56:25 -04001846
David Kilroy01632fa2008-08-21 23:27:58 +01001847 /* Blow away current list of scan results */
1848 list_for_each_entry_safe(bss, tmp_bss, &priv->bss_list, list) {
1849 if (!scan_age ||
1850 time_after(jiffies, bss->last_scanned + scan_age)) {
1851 list_move_tail(&bss->list,
1852 &priv->bss_free_list);
1853 /* Don't blow away ->list, just BSS data */
1854 memset(&bss->bss, 0, sizeof(bss->bss));
1855 bss->last_scanned = 0;
1856 }
Dan Williams1e3428e2007-10-10 23:56:25 -04001857 }
David Kilroy01632fa2008-08-21 23:27:58 +01001858 } else {
1859 struct bss_element *bss;
1860 struct bss_element *tmp_bss;
1861
1862 /* Blow away current list of scan results */
1863 list_for_each_entry_safe(bss, tmp_bss, &priv->bss_list, list) {
1864 if (!scan_age ||
1865 time_after(jiffies, bss->last_scanned + scan_age)) {
1866 list_move_tail(&bss->list,
1867 &priv->bss_free_list);
1868 /* Don't blow away ->list, just BSS data */
1869 memset(&bss->bss, 0, sizeof(bss->bss));
1870 bss->last_scanned = 0;
1871 }
1872 }
1873 }
1874}
1875
1876static void orinoco_add_ext_scan_result(struct orinoco_private *priv,
1877 struct agere_ext_scan_info *atom)
1878{
1879 struct xbss_element *bss = NULL;
1880 int found = 0;
1881
1882 /* Try to update an existing bss first */
1883 list_for_each_entry(bss, &priv->bss_list, list) {
1884 if (compare_ether_addr(bss->bss.bssid, atom->bssid))
1885 continue;
1886 /* ESSID lengths */
1887 if (bss->bss.data[1] != atom->data[1])
1888 continue;
1889 if (memcmp(&bss->bss.data[2], &atom->data[2],
1890 atom->data[1]))
1891 continue;
1892 found = 1;
1893 break;
1894 }
1895
1896 /* Grab a bss off the free list */
1897 if (!found && !list_empty(&priv->bss_free_list)) {
1898 bss = list_entry(priv->bss_free_list.next,
1899 struct xbss_element, list);
1900 list_del(priv->bss_free_list.next);
1901
1902 list_add_tail(&bss->list, &priv->bss_list);
1903 }
1904
1905 if (bss) {
1906 /* Always update the BSS to get latest beacon info */
1907 memcpy(&bss->bss, atom, sizeof(bss->bss));
1908 bss->last_scanned = jiffies;
Dan Williams1e3428e2007-10-10 23:56:25 -04001909 }
1910}
1911
1912static int orinoco_process_scan_results(struct net_device *dev,
1913 unsigned char *buf,
1914 int len)
1915{
1916 struct orinoco_private *priv = netdev_priv(dev);
1917 int offset; /* In the scan data */
1918 union hermes_scan_info *atom;
1919 int atom_len;
1920
1921 switch (priv->firmware_type) {
1922 case FIRMWARE_TYPE_AGERE:
1923 atom_len = sizeof(struct agere_scan_apinfo);
1924 offset = 0;
1925 break;
1926 case FIRMWARE_TYPE_SYMBOL:
1927 /* Lack of documentation necessitates this hack.
1928 * Different firmwares have 68 or 76 byte long atoms.
1929 * We try modulo first. If the length divides by both,
1930 * we check what would be the channel in the second
1931 * frame for a 68-byte atom. 76-byte atoms have 0 there.
1932 * Valid channel cannot be 0. */
1933 if (len % 76)
1934 atom_len = 68;
1935 else if (len % 68)
1936 atom_len = 76;
1937 else if (len >= 1292 && buf[68] == 0)
1938 atom_len = 76;
1939 else
1940 atom_len = 68;
1941 offset = 0;
1942 break;
1943 case FIRMWARE_TYPE_INTERSIL:
1944 offset = 4;
1945 if (priv->has_hostscan) {
1946 atom_len = le16_to_cpup((__le16 *)buf);
1947 /* Sanity check for atom_len */
1948 if (atom_len < sizeof(struct prism2_scan_apinfo)) {
1949 printk(KERN_ERR "%s: Invalid atom_len in scan "
1950 "data: %d\n", dev->name, atom_len);
1951 return -EIO;
1952 }
1953 } else
1954 atom_len = offsetof(struct prism2_scan_apinfo, atim);
1955 break;
1956 default:
1957 return -EOPNOTSUPP;
1958 }
1959
1960 /* Check that we got an whole number of atoms */
1961 if ((len - offset) % atom_len) {
1962 printk(KERN_ERR "%s: Unexpected scan data length %d, "
1963 "atom_len %d, offset %d\n", dev->name, len,
1964 atom_len, offset);
1965 return -EIO;
1966 }
1967
1968 orinoco_clear_scan_results(priv, msecs_to_jiffies(15000));
1969
1970 /* Read the entries one by one */
1971 for (; offset + atom_len <= len; offset += atom_len) {
1972 int found = 0;
David Kilroy3056c402008-08-21 23:27:57 +01001973 struct bss_element *bss = NULL;
Dan Williams1e3428e2007-10-10 23:56:25 -04001974
1975 /* Get next atom */
1976 atom = (union hermes_scan_info *) (buf + offset);
1977
1978 /* Try to update an existing bss first */
1979 list_for_each_entry(bss, &priv->bss_list, list) {
1980 if (compare_ether_addr(bss->bss.a.bssid, atom->a.bssid))
1981 continue;
1982 if (le16_to_cpu(bss->bss.a.essid_len) !=
1983 le16_to_cpu(atom->a.essid_len))
1984 continue;
1985 if (memcmp(bss->bss.a.essid, atom->a.essid,
1986 le16_to_cpu(atom->a.essid_len)))
1987 continue;
Dan Williams1e3428e2007-10-10 23:56:25 -04001988 found = 1;
1989 break;
1990 }
1991
1992 /* Grab a bss off the free list */
1993 if (!found && !list_empty(&priv->bss_free_list)) {
1994 bss = list_entry(priv->bss_free_list.next,
David Kilroy3056c402008-08-21 23:27:57 +01001995 struct bss_element, list);
Dan Williams1e3428e2007-10-10 23:56:25 -04001996 list_del(priv->bss_free_list.next);
1997
Dan Williams1e3428e2007-10-10 23:56:25 -04001998 list_add_tail(&bss->list, &priv->bss_list);
1999 }
Dan Williams22367612007-12-05 11:01:23 -05002000
2001 if (bss) {
2002 /* Always update the BSS to get latest beacon info */
2003 memcpy(&bss->bss, atom, sizeof(bss->bss));
2004 bss->last_scanned = jiffies;
2005 }
Dan Williams1e3428e2007-10-10 23:56:25 -04002006 }
2007
2008 return 0;
2009}
2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011static void __orinoco_ev_info(struct net_device *dev, hermes_t *hw)
2012{
2013 struct orinoco_private *priv = netdev_priv(dev);
2014 u16 infofid;
2015 struct {
Pavel Roskind133ae42005-09-23 04:18:06 -04002016 __le16 len;
2017 __le16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 } __attribute__ ((packed)) info;
2019 int len, type;
2020 int err;
2021
2022 /* This is an answer to an INQUIRE command that we did earlier,
2023 * or an information "event" generated by the card
2024 * The controller return to us a pseudo frame containing
2025 * the information in question - Jean II */
2026 infofid = hermes_read_regn(hw, INFOFID);
2027
2028 /* Read the info frame header - don't try too hard */
2029 err = hermes_bap_pread(hw, IRQ_BAP, &info, sizeof(info),
2030 infofid, 0);
2031 if (err) {
2032 printk(KERN_ERR "%s: error %d reading info frame. "
2033 "Frame dropped.\n", dev->name, err);
2034 return;
2035 }
2036
2037 len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
2038 type = le16_to_cpu(info.type);
2039
2040 switch (type) {
2041 case HERMES_INQ_TALLIES: {
2042 struct hermes_tallies_frame tallies;
2043 struct iw_statistics *wstats = &priv->wstats;
2044
2045 if (len > sizeof(tallies)) {
2046 printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
2047 dev->name, len);
2048 len = sizeof(tallies);
2049 }
2050
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02002051 err = hermes_bap_pread(hw, IRQ_BAP, &tallies, len,
2052 infofid, sizeof(info));
2053 if (err)
2054 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
2056 /* Increment our various counters */
2057 /* wstats->discard.nwid - no wrong BSSID stuff */
2058 wstats->discard.code +=
2059 le16_to_cpu(tallies.RxWEPUndecryptable);
2060 if (len == sizeof(tallies))
2061 wstats->discard.code +=
2062 le16_to_cpu(tallies.RxDiscards_WEPICVError) +
2063 le16_to_cpu(tallies.RxDiscards_WEPExcluded);
2064 wstats->discard.misc +=
2065 le16_to_cpu(tallies.TxDiscardsWrongSA);
2066 wstats->discard.fragment +=
2067 le16_to_cpu(tallies.RxMsgInBadMsgFragments);
2068 wstats->discard.retries +=
2069 le16_to_cpu(tallies.TxRetryLimitExceeded);
2070 /* wstats->miss.beacon - no match */
2071 }
2072 break;
2073 case HERMES_INQ_LINKSTATUS: {
2074 struct hermes_linkstatus linkstatus;
2075 u16 newstatus;
2076 int connected;
2077
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02002078 if (priv->iw_mode == IW_MODE_MONITOR)
2079 break;
2080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 if (len != sizeof(linkstatus)) {
2082 printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
2083 dev->name, len);
2084 break;
2085 }
2086
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02002087 err = hermes_bap_pread(hw, IRQ_BAP, &linkstatus, len,
2088 infofid, sizeof(info));
2089 if (err)
2090 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 newstatus = le16_to_cpu(linkstatus.linkstatus);
2092
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002093 /* Symbol firmware uses "out of range" to signal that
2094 * the hostscan frame can be requested. */
2095 if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
2096 priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
2097 priv->has_hostscan && priv->scan_inprogress) {
2098 hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
2099 break;
2100 }
2101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
2103 || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
2104 || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
2105
2106 if (connected)
2107 netif_carrier_on(dev);
David Gibson7bb7c3a2005-05-12 20:02:10 -04002108 else if (!ignore_disconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 netif_carrier_off(dev);
2110
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002111 if (newstatus != priv->last_linkstatus) {
2112 priv->last_linkstatus = newstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 print_linkstatus(dev, newstatus);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002114 /* The info frame contains only one word which is the
2115 * status (see hermes.h). The status is pretty boring
2116 * in itself, that's why we export the new BSSID...
2117 * Jean II */
2118 schedule_work(&priv->wevent_work);
2119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 }
2121 break;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002122 case HERMES_INQ_SCAN:
2123 if (!priv->scan_inprogress && priv->bssid_fixed &&
2124 priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
2125 schedule_work(&priv->join_work);
2126 break;
2127 }
2128 /* fall through */
2129 case HERMES_INQ_HOSTSCAN:
2130 case HERMES_INQ_HOSTSCAN_SYMBOL: {
2131 /* Result of a scanning. Contains information about
2132 * cells in the vicinity - Jean II */
2133 union iwreq_data wrqu;
2134 unsigned char *buf;
2135
Dan Williams1e3428e2007-10-10 23:56:25 -04002136 /* Scan is no longer in progress */
2137 priv->scan_inprogress = 0;
2138
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002139 /* Sanity check */
2140 if (len > 4096) {
2141 printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
2142 dev->name, len);
2143 break;
2144 }
2145
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002146 /* Allocate buffer for results */
2147 buf = kmalloc(len, GFP_ATOMIC);
2148 if (buf == NULL)
2149 /* No memory, so can't printk()... */
2150 break;
2151
2152 /* Read scan data */
2153 err = hermes_bap_pread(hw, IRQ_BAP, (void *) buf, len,
2154 infofid, sizeof(info));
Pavel Roskin708218b2005-09-01 20:05:19 -04002155 if (err) {
2156 kfree(buf);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002157 break;
Pavel Roskin708218b2005-09-01 20:05:19 -04002158 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002159
2160#ifdef ORINOCO_DEBUG
2161 {
2162 int i;
2163 printk(KERN_DEBUG "Scan result [%02X", buf[0]);
2164 for(i = 1; i < (len * 2); i++)
2165 printk(":%02X", buf[i]);
2166 printk("]\n");
2167 }
2168#endif /* ORINOCO_DEBUG */
2169
Dan Williams1e3428e2007-10-10 23:56:25 -04002170 if (orinoco_process_scan_results(dev, buf, len) == 0) {
2171 /* Send an empty event to user space.
2172 * We don't send the received data on the event because
2173 * it would require us to do complex transcoding, and
2174 * we want to minimise the work done in the irq handler
2175 * Use a request to extract the data - Jean II */
2176 wrqu.data.length = 0;
2177 wrqu.data.flags = 0;
2178 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
2179 }
2180 kfree(buf);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002181 }
2182 break;
David Kilroy01632fa2008-08-21 23:27:58 +01002183 case HERMES_INQ_CHANNELINFO:
2184 {
2185 struct agere_ext_scan_info *bss;
2186
2187 if (!priv->scan_inprogress) {
2188 printk(KERN_DEBUG "%s: Got chaninfo without scan, "
2189 "len=%d\n", dev->name, len);
2190 break;
2191 }
2192
2193 /* An empty result indicates that the scan is complete */
2194 if (len == 0) {
2195 union iwreq_data wrqu;
2196
2197 /* Scan is no longer in progress */
2198 priv->scan_inprogress = 0;
2199
2200 wrqu.data.length = 0;
2201 wrqu.data.flags = 0;
2202 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
2203 break;
2204 }
2205
2206 /* Sanity check */
2207 else if (len > sizeof(*bss)) {
2208 printk(KERN_WARNING
2209 "%s: Ext scan results too large (%d bytes). "
2210 "Truncating results to %zd bytes.\n",
2211 dev->name, len, sizeof(*bss));
2212 len = sizeof(*bss);
2213 } else if (len < (offsetof(struct agere_ext_scan_info,
2214 data) + 2)) {
2215 /* Drop this result now so we don't have to
2216 * keep checking later */
2217 printk(KERN_WARNING
2218 "%s: Ext scan results too short (%d bytes)\n",
2219 dev->name, len);
2220 break;
2221 }
2222
2223 bss = kmalloc(sizeof(*bss), GFP_ATOMIC);
2224 if (bss == NULL)
2225 break;
2226
2227 /* Read scan data */
2228 err = hermes_bap_pread(hw, IRQ_BAP, (void *) bss, len,
2229 infofid, sizeof(info));
2230 if (err) {
2231 kfree(bss);
2232 break;
2233 }
2234
2235 orinoco_add_ext_scan_result(priv, bss);
2236
2237 kfree(bss);
2238 break;
2239 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002240 case HERMES_INQ_SEC_STAT_AGERE:
2241 /* Security status (Agere specific) */
2242 /* Ignore this frame for now */
2243 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
2244 break;
2245 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 default:
2247 printk(KERN_DEBUG "%s: Unknown information frame received: "
2248 "type 0x%04x, length %d\n", dev->name, type, len);
2249 /* We don't actually do anything about it */
2250 break;
2251 }
2252}
2253
2254static void __orinoco_ev_infdrop(struct net_device *dev, hermes_t *hw)
2255{
2256 if (net_ratelimit())
2257 printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
2258}
2259
2260/********************************************************************/
2261/* Internal hardware control routines */
2262/********************************************************************/
2263
2264int __orinoco_up(struct net_device *dev)
2265{
2266 struct orinoco_private *priv = netdev_priv(dev);
2267 struct hermes *hw = &priv->hw;
2268 int err;
2269
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02002270 netif_carrier_off(dev); /* just to make sure */
2271
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 err = __orinoco_program_rids(dev);
2273 if (err) {
2274 printk(KERN_ERR "%s: Error %d configuring card\n",
2275 dev->name, err);
2276 return err;
2277 }
2278
2279 /* Fire things up again */
2280 hermes_set_irqmask(hw, ORINOCO_INTEN);
2281 err = hermes_enable_port(hw, 0);
2282 if (err) {
2283 printk(KERN_ERR "%s: Error %d enabling MAC port\n",
2284 dev->name, err);
2285 return err;
2286 }
2287
2288 netif_start_queue(dev);
2289
2290 return 0;
2291}
2292
2293int __orinoco_down(struct net_device *dev)
2294{
2295 struct orinoco_private *priv = netdev_priv(dev);
2296 struct hermes *hw = &priv->hw;
2297 int err;
2298
2299 netif_stop_queue(dev);
2300
2301 if (! priv->hw_unavailable) {
2302 if (! priv->broken_disableport) {
2303 err = hermes_disable_port(hw, 0);
2304 if (err) {
2305 /* Some firmwares (e.g. Intersil 1.3.x) seem
2306 * to have problems disabling the port, oh
2307 * well, too bad. */
2308 printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
2309 dev->name, err);
2310 priv->broken_disableport = 1;
2311 }
2312 }
2313 hermes_set_irqmask(hw, 0);
2314 hermes_write_regn(hw, EVACK, 0xffff);
2315 }
2316
2317 /* firmware will have to reassociate */
2318 netif_carrier_off(dev);
2319 priv->last_linkstatus = 0xffff;
2320
2321 return 0;
2322}
2323
Pavel Roskin37a6c612006-04-07 04:10:49 -04002324static int orinoco_allocate_fid(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
2326 struct orinoco_private *priv = netdev_priv(dev);
2327 struct hermes *hw = &priv->hw;
2328 int err;
2329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 err = hermes_allocate(hw, priv->nicbuf_size, &priv->txfid);
David Gibsonb24d4582005-05-12 20:04:16 -04002331 if (err == -EIO && priv->nicbuf_size > TX_NICBUF_SIZE_BUG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 /* Try workaround for old Symbol firmware bug */
2333 printk(KERN_WARNING "%s: firmware ALLOC bug detected "
2334 "(old Symbol firmware?). Trying to work around... ",
2335 dev->name);
2336
2337 priv->nicbuf_size = TX_NICBUF_SIZE_BUG;
2338 err = hermes_allocate(hw, priv->nicbuf_size, &priv->txfid);
2339 if (err)
2340 printk("failed!\n");
2341 else
2342 printk("ok.\n");
2343 }
2344
2345 return err;
2346}
2347
Pavel Roskin37a6c612006-04-07 04:10:49 -04002348int orinoco_reinit_firmware(struct net_device *dev)
2349{
2350 struct orinoco_private *priv = netdev_priv(dev);
2351 struct hermes *hw = &priv->hw;
2352 int err;
2353
2354 err = hermes_init(hw);
Andrey Borzenkov0df6cbb2008-10-12 20:15:43 +04002355 if (priv->do_fw_download && !err) {
2356 err = orinoco_download(priv);
2357 if (err)
2358 priv->do_fw_download = 0;
2359 }
Pavel Roskin37a6c612006-04-07 04:10:49 -04002360 if (!err)
2361 err = orinoco_allocate_fid(dev);
2362
2363 return err;
2364}
2365
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366static int __orinoco_hw_set_bitrate(struct orinoco_private *priv)
2367{
2368 hermes_t *hw = &priv->hw;
2369 int err = 0;
2370
2371 if (priv->bitratemode >= BITRATE_TABLE_SIZE) {
2372 printk(KERN_ERR "%s: BUG: Invalid bitrate mode %d\n",
2373 priv->ndev->name, priv->bitratemode);
2374 return -EINVAL;
2375 }
2376
2377 switch (priv->firmware_type) {
2378 case FIRMWARE_TYPE_AGERE:
2379 err = hermes_write_wordrec(hw, USER_BAP,
2380 HERMES_RID_CNFTXRATECONTROL,
2381 bitrate_table[priv->bitratemode].agere_txratectrl);
2382 break;
2383 case FIRMWARE_TYPE_INTERSIL:
2384 case FIRMWARE_TYPE_SYMBOL:
2385 err = hermes_write_wordrec(hw, USER_BAP,
2386 HERMES_RID_CNFTXRATECONTROL,
2387 bitrate_table[priv->bitratemode].intersil_txratectrl);
2388 break;
2389 default:
2390 BUG();
2391 }
2392
2393 return err;
2394}
2395
Christoph Hellwig16739b02005-06-19 01:27:51 +02002396/* Set fixed AP address */
2397static int __orinoco_hw_set_wap(struct orinoco_private *priv)
2398{
2399 int roaming_flag;
2400 int err = 0;
2401 hermes_t *hw = &priv->hw;
2402
2403 switch (priv->firmware_type) {
2404 case FIRMWARE_TYPE_AGERE:
2405 /* not supported */
2406 break;
2407 case FIRMWARE_TYPE_INTERSIL:
2408 if (priv->bssid_fixed)
2409 roaming_flag = 2;
2410 else
2411 roaming_flag = 1;
2412
2413 err = hermes_write_wordrec(hw, USER_BAP,
2414 HERMES_RID_CNFROAMINGMODE,
2415 roaming_flag);
2416 break;
2417 case FIRMWARE_TYPE_SYMBOL:
2418 err = HERMES_WRITE_RECORD(hw, USER_BAP,
2419 HERMES_RID_CNFMANDATORYBSSID_SYMBOL,
2420 &priv->desired_bssid);
2421 break;
2422 }
2423 return err;
2424}
2425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426/* Change the WEP keys and/or the current keys. Can be called
David Kilroyd03032a2008-08-21 23:28:02 +01002427 * either from __orinoco_hw_setup_enc() or directly from
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 * orinoco_ioctl_setiwencode(). In the later case the association
2429 * with the AP is not broken (if the firmware can handle it),
2430 * which is needed for 802.1x implementations. */
2431static int __orinoco_hw_setup_wepkeys(struct orinoco_private *priv)
2432{
2433 hermes_t *hw = &priv->hw;
2434 int err = 0;
2435
2436 switch (priv->firmware_type) {
2437 case FIRMWARE_TYPE_AGERE:
2438 err = HERMES_WRITE_RECORD(hw, USER_BAP,
2439 HERMES_RID_CNFWEPKEYS_AGERE,
2440 &priv->keys);
2441 if (err)
2442 return err;
2443 err = hermes_write_wordrec(hw, USER_BAP,
2444 HERMES_RID_CNFTXKEY_AGERE,
2445 priv->tx_key);
2446 if (err)
2447 return err;
2448 break;
2449 case FIRMWARE_TYPE_INTERSIL:
2450 case FIRMWARE_TYPE_SYMBOL:
2451 {
2452 int keylen;
2453 int i;
2454
2455 /* Force uniform key length to work around firmware bugs */
2456 keylen = le16_to_cpu(priv->keys[priv->tx_key].len);
2457
2458 if (keylen > LARGE_KEY_SIZE) {
2459 printk(KERN_ERR "%s: BUG: Key %d has oversize length %d.\n",
2460 priv->ndev->name, priv->tx_key, keylen);
2461 return -E2BIG;
2462 }
2463
2464 /* Write all 4 keys */
2465 for(i = 0; i < ORINOCO_MAX_KEYS; i++) {
2466 err = hermes_write_ltv(hw, USER_BAP,
2467 HERMES_RID_CNFDEFAULTKEY0 + i,
2468 HERMES_BYTES_TO_RECLEN(keylen),
2469 priv->keys[i].data);
2470 if (err)
2471 return err;
2472 }
2473
2474 /* Write the index of the key used in transmission */
2475 err = hermes_write_wordrec(hw, USER_BAP,
2476 HERMES_RID_CNFWEPDEFAULTKEYID,
2477 priv->tx_key);
2478 if (err)
2479 return err;
2480 }
2481 break;
2482 }
2483
2484 return 0;
2485}
2486
David Kilroyd03032a2008-08-21 23:28:02 +01002487static int __orinoco_hw_setup_enc(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488{
2489 hermes_t *hw = &priv->hw;
2490 int err = 0;
2491 int master_wep_flag;
2492 int auth_flag;
David Kilroy4ae6ee22008-08-21 23:27:59 +01002493 int enc_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
David Kilroyd03032a2008-08-21 23:28:02 +01002495 /* Setup WEP keys for WEP and WPA */
2496 if (priv->encode_alg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 __orinoco_hw_setup_wepkeys(priv);
2498
2499 if (priv->wep_restrict)
2500 auth_flag = HERMES_AUTH_SHARED_KEY;
2501 else
2502 auth_flag = HERMES_AUTH_OPEN;
2503
David Kilroyd03032a2008-08-21 23:28:02 +01002504 if (priv->wpa_enabled)
2505 enc_flag = 2;
2506 else if (priv->encode_alg == IW_ENCODE_ALG_WEP)
David Kilroy4ae6ee22008-08-21 23:27:59 +01002507 enc_flag = 1;
2508 else
2509 enc_flag = 0;
2510
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 switch (priv->firmware_type) {
2512 case FIRMWARE_TYPE_AGERE: /* Agere style WEP */
David Kilroy4ae6ee22008-08-21 23:27:59 +01002513 if (priv->encode_alg == IW_ENCODE_ALG_WEP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 /* Enable the shared-key authentication. */
2515 err = hermes_write_wordrec(hw, USER_BAP,
2516 HERMES_RID_CNFAUTHENTICATION_AGERE,
2517 auth_flag);
2518 }
2519 err = hermes_write_wordrec(hw, USER_BAP,
2520 HERMES_RID_CNFWEPENABLED_AGERE,
David Kilroy4ae6ee22008-08-21 23:27:59 +01002521 enc_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 if (err)
2523 return err;
David Kilroyd03032a2008-08-21 23:28:02 +01002524
2525 if (priv->has_wpa) {
2526 /* Set WPA key management */
2527 err = hermes_write_wordrec(hw, USER_BAP,
2528 HERMES_RID_CNFSETWPAAUTHMGMTSUITE_AGERE,
2529 priv->key_mgmt);
2530 if (err)
2531 return err;
2532 }
2533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 break;
2535
2536 case FIRMWARE_TYPE_INTERSIL: /* Intersil style WEP */
2537 case FIRMWARE_TYPE_SYMBOL: /* Symbol style WEP */
David Kilroy4ae6ee22008-08-21 23:27:59 +01002538 if (priv->encode_alg == IW_ENCODE_ALG_WEP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 if (priv->wep_restrict ||
2540 (priv->firmware_type == FIRMWARE_TYPE_SYMBOL))
2541 master_wep_flag = HERMES_WEP_PRIVACY_INVOKED |
2542 HERMES_WEP_EXCL_UNENCRYPTED;
2543 else
2544 master_wep_flag = HERMES_WEP_PRIVACY_INVOKED;
2545
2546 err = hermes_write_wordrec(hw, USER_BAP,
2547 HERMES_RID_CNFAUTHENTICATION,
2548 auth_flag);
2549 if (err)
2550 return err;
2551 } else
2552 master_wep_flag = 0;
2553
2554 if (priv->iw_mode == IW_MODE_MONITOR)
2555 master_wep_flag |= HERMES_WEP_HOST_DECRYPT;
2556
2557 /* Master WEP setting : on/off */
2558 err = hermes_write_wordrec(hw, USER_BAP,
2559 HERMES_RID_CNFWEPFLAGS_INTERSIL,
2560 master_wep_flag);
2561 if (err)
2562 return err;
2563
2564 break;
2565 }
2566
2567 return 0;
2568}
2569
David Kilroyd03032a2008-08-21 23:28:02 +01002570/* key must be 32 bytes, including the tx and rx MIC keys.
2571 * rsc must be 8 bytes
2572 * tsc must be 8 bytes or NULL
2573 */
2574static int __orinoco_hw_set_tkip_key(hermes_t *hw, int key_idx, int set_tx,
2575 u8 *key, u8 *rsc, u8 *tsc)
2576{
2577 struct {
2578 __le16 idx;
2579 u8 rsc[IW_ENCODE_SEQ_MAX_SIZE];
2580 u8 key[TKIP_KEYLEN];
2581 u8 tx_mic[MIC_KEYLEN];
2582 u8 rx_mic[MIC_KEYLEN];
2583 u8 tsc[IW_ENCODE_SEQ_MAX_SIZE];
2584 } __attribute__ ((packed)) buf;
2585 int ret;
2586 int err;
2587 int k;
2588 u16 xmitting;
2589
2590 key_idx &= 0x3;
2591
2592 if (set_tx)
2593 key_idx |= 0x8000;
2594
2595 buf.idx = cpu_to_le16(key_idx);
2596 memcpy(buf.key, key,
2597 sizeof(buf.key) + sizeof(buf.tx_mic) + sizeof(buf.rx_mic));
2598
2599 if (rsc == NULL)
2600 memset(buf.rsc, 0, sizeof(buf.rsc));
2601 else
2602 memcpy(buf.rsc, rsc, sizeof(buf.rsc));
2603
2604 if (tsc == NULL) {
2605 memset(buf.tsc, 0, sizeof(buf.tsc));
2606 buf.tsc[4] = 0x10;
2607 } else {
2608 memcpy(buf.tsc, tsc, sizeof(buf.tsc));
2609 }
2610
2611 /* Wait upto 100ms for tx queue to empty */
2612 k = 100;
2613 do {
2614 k--;
2615 udelay(1000);
2616 ret = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_TXQUEUEEMPTY,
2617 &xmitting);
2618 if (ret)
2619 break;
2620 } while ((k > 0) && xmitting);
2621
2622 if (k == 0)
2623 ret = -ETIMEDOUT;
2624
2625 err = HERMES_WRITE_RECORD(hw, USER_BAP,
2626 HERMES_RID_CNFADDDEFAULTTKIPKEY_AGERE,
2627 &buf);
2628
2629 return ret ? ret : err;
2630}
2631
2632static int orinoco_clear_tkip_key(struct orinoco_private *priv,
2633 int key_idx)
2634{
2635 hermes_t *hw = &priv->hw;
2636 int err;
2637
2638 memset(&priv->tkip_key[key_idx], 0, sizeof(priv->tkip_key[key_idx]));
2639 err = hermes_write_wordrec(hw, USER_BAP,
2640 HERMES_RID_CNFREMDEFAULTTKIPKEY_AGERE,
2641 key_idx);
2642 if (err)
2643 printk(KERN_WARNING "%s: Error %d clearing TKIP key %d\n",
2644 priv->ndev->name, err, key_idx);
2645 return err;
2646}
2647
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648static int __orinoco_program_rids(struct net_device *dev)
2649{
2650 struct orinoco_private *priv = netdev_priv(dev);
2651 hermes_t *hw = &priv->hw;
2652 int err;
2653 struct hermes_idstring idbuf;
2654
2655 /* Set the MAC address */
2656 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
2657 HERMES_BYTES_TO_RECLEN(ETH_ALEN), dev->dev_addr);
2658 if (err) {
2659 printk(KERN_ERR "%s: Error %d setting MAC address\n",
2660 dev->name, err);
2661 return err;
2662 }
2663
2664 /* Set up the link mode */
2665 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFPORTTYPE,
2666 priv->port_type);
2667 if (err) {
2668 printk(KERN_ERR "%s: Error %d setting port type\n",
2669 dev->name, err);
2670 return err;
2671 }
2672 /* Set the channel/frequency */
David Gibsond51d8b12005-05-12 20:03:36 -04002673 if (priv->channel != 0 && priv->iw_mode != IW_MODE_INFRA) {
2674 err = hermes_write_wordrec(hw, USER_BAP,
2675 HERMES_RID_CNFOWNCHANNEL,
2676 priv->channel);
2677 if (err) {
2678 printk(KERN_ERR "%s: Error %d setting channel %d\n",
2679 dev->name, err, priv->channel);
2680 return err;
2681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 }
2683
2684 if (priv->has_ibss) {
2685 u16 createibss;
2686
2687 if ((strlen(priv->desired_essid) == 0) && (priv->createibss)) {
2688 printk(KERN_WARNING "%s: This firmware requires an "
2689 "ESSID in IBSS-Ad-Hoc mode.\n", dev->name);
2690 /* With wvlan_cs, in this case, we would crash.
2691 * hopefully, this driver will behave better...
2692 * Jean II */
2693 createibss = 0;
2694 } else {
2695 createibss = priv->createibss;
2696 }
2697
2698 err = hermes_write_wordrec(hw, USER_BAP,
2699 HERMES_RID_CNFCREATEIBSS,
2700 createibss);
2701 if (err) {
2702 printk(KERN_ERR "%s: Error %d setting CREATEIBSS\n",
2703 dev->name, err);
2704 return err;
2705 }
2706 }
2707
Christoph Hellwig16739b02005-06-19 01:27:51 +02002708 /* Set the desired BSSID */
2709 err = __orinoco_hw_set_wap(priv);
2710 if (err) {
2711 printk(KERN_ERR "%s: Error %d setting AP address\n",
2712 dev->name, err);
2713 return err;
2714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 /* Set the desired ESSID */
2716 idbuf.len = cpu_to_le16(strlen(priv->desired_essid));
2717 memcpy(&idbuf.val, priv->desired_essid, sizeof(idbuf.val));
2718 /* WinXP wants partner to configure OWNSSID even in IBSS mode. (jimc) */
2719 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNSSID,
2720 HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
2721 &idbuf);
2722 if (err) {
2723 printk(KERN_ERR "%s: Error %d setting OWNSSID\n",
2724 dev->name, err);
2725 return err;
2726 }
2727 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFDESIREDSSID,
2728 HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
2729 &idbuf);
2730 if (err) {
2731 printk(KERN_ERR "%s: Error %d setting DESIREDSSID\n",
2732 dev->name, err);
2733 return err;
2734 }
2735
2736 /* Set the station name */
2737 idbuf.len = cpu_to_le16(strlen(priv->nick));
2738 memcpy(&idbuf.val, priv->nick, sizeof(idbuf.val));
2739 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
2740 HERMES_BYTES_TO_RECLEN(strlen(priv->nick)+2),
2741 &idbuf);
2742 if (err) {
2743 printk(KERN_ERR "%s: Error %d setting nickname\n",
2744 dev->name, err);
2745 return err;
2746 }
2747
2748 /* Set AP density */
2749 if (priv->has_sensitivity) {
2750 err = hermes_write_wordrec(hw, USER_BAP,
2751 HERMES_RID_CNFSYSTEMSCALE,
2752 priv->ap_density);
2753 if (err) {
2754 printk(KERN_WARNING "%s: Error %d setting SYSTEMSCALE. "
2755 "Disabling sensitivity control\n",
2756 dev->name, err);
2757
2758 priv->has_sensitivity = 0;
2759 }
2760 }
2761
2762 /* Set RTS threshold */
2763 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
2764 priv->rts_thresh);
2765 if (err) {
2766 printk(KERN_ERR "%s: Error %d setting RTS threshold\n",
2767 dev->name, err);
2768 return err;
2769 }
2770
2771 /* Set fragmentation threshold or MWO robustness */
2772 if (priv->has_mwo)
2773 err = hermes_write_wordrec(hw, USER_BAP,
2774 HERMES_RID_CNFMWOROBUST_AGERE,
2775 priv->mwo_robust);
2776 else
2777 err = hermes_write_wordrec(hw, USER_BAP,
2778 HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
2779 priv->frag_thresh);
2780 if (err) {
2781 printk(KERN_ERR "%s: Error %d setting fragmentation\n",
2782 dev->name, err);
2783 return err;
2784 }
2785
2786 /* Set bitrate */
2787 err = __orinoco_hw_set_bitrate(priv);
2788 if (err) {
2789 printk(KERN_ERR "%s: Error %d setting bitrate\n",
2790 dev->name, err);
2791 return err;
2792 }
2793
2794 /* Set power management */
2795 if (priv->has_pm) {
2796 err = hermes_write_wordrec(hw, USER_BAP,
2797 HERMES_RID_CNFPMENABLED,
2798 priv->pm_on);
2799 if (err) {
2800 printk(KERN_ERR "%s: Error %d setting up PM\n",
2801 dev->name, err);
2802 return err;
2803 }
2804
2805 err = hermes_write_wordrec(hw, USER_BAP,
2806 HERMES_RID_CNFMULTICASTRECEIVE,
2807 priv->pm_mcast);
2808 if (err) {
2809 printk(KERN_ERR "%s: Error %d setting up PM\n",
2810 dev->name, err);
2811 return err;
2812 }
2813 err = hermes_write_wordrec(hw, USER_BAP,
2814 HERMES_RID_CNFMAXSLEEPDURATION,
2815 priv->pm_period);
2816 if (err) {
2817 printk(KERN_ERR "%s: Error %d setting up PM\n",
2818 dev->name, err);
2819 return err;
2820 }
2821 err = hermes_write_wordrec(hw, USER_BAP,
2822 HERMES_RID_CNFPMHOLDOVERDURATION,
2823 priv->pm_timeout);
2824 if (err) {
2825 printk(KERN_ERR "%s: Error %d setting up PM\n",
2826 dev->name, err);
2827 return err;
2828 }
2829 }
2830
2831 /* Set preamble - only for Symbol so far... */
2832 if (priv->has_preamble) {
2833 err = hermes_write_wordrec(hw, USER_BAP,
2834 HERMES_RID_CNFPREAMBLE_SYMBOL,
2835 priv->preamble);
2836 if (err) {
2837 printk(KERN_ERR "%s: Error %d setting preamble\n",
2838 dev->name, err);
2839 return err;
2840 }
2841 }
2842
2843 /* Set up encryption */
David Kilroyd03032a2008-08-21 23:28:02 +01002844 if (priv->has_wep || priv->has_wpa) {
2845 err = __orinoco_hw_setup_enc(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 if (err) {
David Kilroyd03032a2008-08-21 23:28:02 +01002847 printk(KERN_ERR "%s: Error %d activating encryption\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 dev->name, err);
2849 return err;
2850 }
2851 }
2852
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02002853 if (priv->iw_mode == IW_MODE_MONITOR) {
2854 /* Enable monitor mode */
2855 dev->type = ARPHRD_IEEE80211;
2856 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
2857 HERMES_TEST_MONITOR, 0, NULL);
2858 } else {
2859 /* Disable monitor mode */
2860 dev->type = ARPHRD_ETHER;
2861 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
2862 HERMES_TEST_STOP, 0, NULL);
2863 }
2864 if (err)
2865 return err;
2866
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 /* Set promiscuity / multicast*/
2868 priv->promiscuous = 0;
2869 priv->mc_count = 0;
Herbert Xu932ff272006-06-09 12:20:56 -07002870
2871 /* FIXME: what about netif_tx_lock */
2872 __orinoco_set_multicast_list(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
2874 return 0;
2875}
2876
2877/* FIXME: return int? */
2878static void
2879__orinoco_set_multicast_list(struct net_device *dev)
2880{
2881 struct orinoco_private *priv = netdev_priv(dev);
2882 hermes_t *hw = &priv->hw;
2883 int err = 0;
2884 int promisc, mc_count;
2885
2886 /* The Hermes doesn't seem to have an allmulti mode, so we go
2887 * into promiscuous mode and let the upper levels deal. */
2888 if ( (dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
2889 (dev->mc_count > MAX_MULTICAST(priv)) ) {
2890 promisc = 1;
2891 mc_count = 0;
2892 } else {
2893 promisc = 0;
2894 mc_count = dev->mc_count;
2895 }
2896
2897 if (promisc != priv->promiscuous) {
2898 err = hermes_write_wordrec(hw, USER_BAP,
2899 HERMES_RID_CNFPROMISCUOUSMODE,
2900 promisc);
2901 if (err) {
2902 printk(KERN_ERR "%s: Error %d setting PROMISCUOUSMODE to 1.\n",
2903 dev->name, err);
2904 } else
2905 priv->promiscuous = promisc;
2906 }
2907
David Kilroy667d4102008-08-23 19:03:34 +01002908 /* If we're not in promiscuous mode, then we need to set the
2909 * group address if either we want to multicast, or if we were
2910 * multicasting and want to stop */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 if (! promisc && (mc_count || priv->mc_count) ) {
2912 struct dev_mc_list *p = dev->mc_list;
2913 struct hermes_multicast mclist;
2914 int i;
2915
2916 for (i = 0; i < mc_count; i++) {
2917 /* paranoia: is list shorter than mc_count? */
2918 BUG_ON(! p);
2919 /* paranoia: bad address size in list? */
2920 BUG_ON(p->dmi_addrlen != ETH_ALEN);
2921
2922 memcpy(mclist.addr[i], p->dmi_addr, ETH_ALEN);
2923 p = p->next;
2924 }
2925
2926 if (p)
2927 printk(KERN_WARNING "%s: Multicast list is "
2928 "longer than mc_count\n", dev->name);
2929
David Kilroy667d4102008-08-23 19:03:34 +01002930 err = hermes_write_ltv(hw, USER_BAP,
2931 HERMES_RID_CNFGROUPADDRESSES,
2932 HERMES_BYTES_TO_RECLEN(mc_count * ETH_ALEN),
2933 &mclist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 if (err)
2935 printk(KERN_ERR "%s: Error %d setting multicast list.\n",
2936 dev->name, err);
2937 else
2938 priv->mc_count = mc_count;
2939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940}
2941
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942/* This must be called from user context, without locks held - use
2943 * schedule_work() */
David Howellsc4028952006-11-22 14:57:56 +00002944static void orinoco_reset(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945{
David Howellsc4028952006-11-22 14:57:56 +00002946 struct orinoco_private *priv =
2947 container_of(work, struct orinoco_private, reset_work);
2948 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 struct hermes *hw = &priv->hw;
Christoph Hellwig8551cb92005-05-14 17:30:04 +02002950 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 unsigned long flags;
2952
2953 if (orinoco_lock(priv, &flags) != 0)
2954 /* When the hardware becomes available again, whatever
2955 * detects that is responsible for re-initializing
2956 * it. So no need for anything further */
2957 return;
2958
2959 netif_stop_queue(dev);
2960
2961 /* Shut off interrupts. Depending on what state the hardware
2962 * is in, this might not work, but we'll try anyway */
2963 hermes_set_irqmask(hw, 0);
2964 hermes_write_regn(hw, EVACK, 0xffff);
2965
2966 priv->hw_unavailable++;
2967 priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
2968 netif_carrier_off(dev);
2969
2970 orinoco_unlock(priv, &flags);
2971
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002972 /* Scanning support: Cleanup of driver struct */
Dan Williams1e3428e2007-10-10 23:56:25 -04002973 orinoco_clear_scan_results(priv, 0);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002974 priv->scan_inprogress = 0;
2975
Christoph Hellwig8551cb92005-05-14 17:30:04 +02002976 if (priv->hard_reset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 err = (*priv->hard_reset)(priv);
Christoph Hellwig8551cb92005-05-14 17:30:04 +02002978 if (err) {
2979 printk(KERN_ERR "%s: orinoco_reset: Error %d "
2980 "performing hard reset\n", dev->name, err);
2981 goto disable;
2982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 }
2984
2985 err = orinoco_reinit_firmware(dev);
2986 if (err) {
2987 printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
2988 dev->name, err);
Christoph Hellwig8551cb92005-05-14 17:30:04 +02002989 goto disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 }
2991
2992 spin_lock_irq(&priv->lock); /* This has to be called from user context */
2993
2994 priv->hw_unavailable--;
2995
2996 /* priv->open or priv->hw_unavailable might have changed while
2997 * we dropped the lock */
2998 if (priv->open && (! priv->hw_unavailable)) {
2999 err = __orinoco_up(dev);
3000 if (err) {
3001 printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
3002 dev->name, err);
3003 } else
3004 dev->trans_start = jiffies;
3005 }
3006
3007 spin_unlock_irq(&priv->lock);
3008
3009 return;
Christoph Hellwig8551cb92005-05-14 17:30:04 +02003010 disable:
3011 hermes_set_irqmask(hw, 0);
3012 netif_device_detach(dev);
3013 printk(KERN_ERR "%s: Device has been disabled!\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014}
3015
3016/********************************************************************/
3017/* Interrupt handler */
3018/********************************************************************/
3019
3020static void __orinoco_ev_tick(struct net_device *dev, hermes_t *hw)
3021{
3022 printk(KERN_DEBUG "%s: TICK\n", dev->name);
3023}
3024
3025static void __orinoco_ev_wterr(struct net_device *dev, hermes_t *hw)
3026{
3027 /* This seems to happen a fair bit under load, but ignoring it
3028 seems to work fine...*/
3029 printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
3030 dev->name);
3031}
3032
David Howells7d12e782006-10-05 14:55:46 +01003033irqreturn_t orinoco_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034{
Jeff Garzikc31f28e2006-10-06 14:56:04 -04003035 struct net_device *dev = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 struct orinoco_private *priv = netdev_priv(dev);
3037 hermes_t *hw = &priv->hw;
3038 int count = MAX_IRQLOOPS_PER_IRQ;
3039 u16 evstat, events;
3040 /* These are used to detect a runaway interrupt situation */
3041 /* If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
3042 * we panic and shut down the hardware */
3043 static int last_irq_jiffy = 0; /* jiffies value the last time
3044 * we were called */
3045 static int loops_this_jiffy = 0;
3046 unsigned long flags;
3047
3048 if (orinoco_lock(priv, &flags) != 0) {
3049 /* If hw is unavailable - we don't know if the irq was
3050 * for us or not */
3051 return IRQ_HANDLED;
3052 }
3053
3054 evstat = hermes_read_regn(hw, EVSTAT);
3055 events = evstat & hw->inten;
3056 if (! events) {
3057 orinoco_unlock(priv, &flags);
3058 return IRQ_NONE;
3059 }
3060
3061 if (jiffies != last_irq_jiffy)
3062 loops_this_jiffy = 0;
3063 last_irq_jiffy = jiffies;
3064
3065 while (events && count--) {
3066 if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
3067 printk(KERN_WARNING "%s: IRQ handler is looping too "
3068 "much! Resetting.\n", dev->name);
3069 /* Disable interrupts for now */
3070 hermes_set_irqmask(hw, 0);
3071 schedule_work(&priv->reset_work);
3072 break;
3073 }
3074
3075 /* Check the card hasn't been removed */
3076 if (! hermes_present(hw)) {
3077 DEBUG(0, "orinoco_interrupt(): card removed\n");
3078 break;
3079 }
3080
3081 if (events & HERMES_EV_TICK)
3082 __orinoco_ev_tick(dev, hw);
3083 if (events & HERMES_EV_WTERR)
3084 __orinoco_ev_wterr(dev, hw);
3085 if (events & HERMES_EV_INFDROP)
3086 __orinoco_ev_infdrop(dev, hw);
3087 if (events & HERMES_EV_INFO)
3088 __orinoco_ev_info(dev, hw);
3089 if (events & HERMES_EV_RX)
3090 __orinoco_ev_rx(dev, hw);
3091 if (events & HERMES_EV_TXEXC)
3092 __orinoco_ev_txexc(dev, hw);
3093 if (events & HERMES_EV_TX)
3094 __orinoco_ev_tx(dev, hw);
3095 if (events & HERMES_EV_ALLOC)
3096 __orinoco_ev_alloc(dev, hw);
3097
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02003098 hermes_write_regn(hw, EVACK, evstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
3100 evstat = hermes_read_regn(hw, EVSTAT);
3101 events = evstat & hw->inten;
3102 };
3103
3104 orinoco_unlock(priv, &flags);
3105 return IRQ_HANDLED;
3106}
3107
3108/********************************************************************/
David Kilroy39d1ffe2008-11-22 10:37:28 +00003109/* Power management */
3110/********************************************************************/
3111#if defined(CONFIG_PM_SLEEP) && !defined(CONFIG_HERMES_CACHE_FW_ON_INIT)
3112static int orinoco_pm_notifier(struct notifier_block *notifier,
3113 unsigned long pm_event,
3114 void *unused)
3115{
3116 struct orinoco_private *priv = container_of(notifier,
3117 struct orinoco_private,
3118 pm_notifier);
3119
3120 /* All we need to do is cache the firmware before suspend, and
3121 * release it when we come out.
3122 *
3123 * Only need to do this if we're downloading firmware. */
3124 if (!priv->do_fw_download)
3125 return NOTIFY_DONE;
3126
3127 switch (pm_event) {
3128 case PM_HIBERNATION_PREPARE:
3129 case PM_SUSPEND_PREPARE:
3130 orinoco_cache_fw(priv, 0);
3131 break;
3132
3133 case PM_POST_RESTORE:
3134 /* Restore from hibernation failed. We need to clean
3135 * up in exactly the same way, so fall through. */
3136 case PM_POST_HIBERNATION:
3137 case PM_POST_SUSPEND:
3138 orinoco_uncache_fw(priv);
3139 break;
3140
3141 case PM_RESTORE_PREPARE:
3142 default:
3143 break;
3144 }
3145
3146 return NOTIFY_DONE;
3147}
3148#else /* !PM_SLEEP || HERMES_CACHE_FW_ON_INIT */
3149#define orinoco_pm_notifier NULL
3150#endif
3151
3152/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153/* Initialization */
3154/********************************************************************/
3155
3156struct comp_id {
3157 u16 id, variant, major, minor;
3158} __attribute__ ((packed));
3159
3160static inline fwtype_t determine_firmware_type(struct comp_id *nic_id)
3161{
3162 if (nic_id->id < 0x8000)
3163 return FIRMWARE_TYPE_AGERE;
3164 else if (nic_id->id == 0x8000 && nic_id->major == 0)
3165 return FIRMWARE_TYPE_SYMBOL;
3166 else
3167 return FIRMWARE_TYPE_INTERSIL;
3168}
3169
3170/* Set priv->firmware type, determine firmware properties */
3171static int determine_firmware(struct net_device *dev)
3172{
3173 struct orinoco_private *priv = netdev_priv(dev);
3174 hermes_t *hw = &priv->hw;
3175 int err;
3176 struct comp_id nic_id, sta_id;
3177 unsigned int firmver;
Hennerich, Michaeldde6d432007-02-05 16:41:35 -08003178 char tmp[SYMBOL_MAX_VER_LEN+1] __attribute__((aligned(2)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
3180 /* Get the hardware version */
3181 err = HERMES_READ_RECORD(hw, USER_BAP, HERMES_RID_NICID, &nic_id);
3182 if (err) {
3183 printk(KERN_ERR "%s: Cannot read hardware identity: error %d\n",
3184 dev->name, err);
3185 return err;
3186 }
3187
3188 le16_to_cpus(&nic_id.id);
3189 le16_to_cpus(&nic_id.variant);
3190 le16_to_cpus(&nic_id.major);
3191 le16_to_cpus(&nic_id.minor);
3192 printk(KERN_DEBUG "%s: Hardware identity %04x:%04x:%04x:%04x\n",
3193 dev->name, nic_id.id, nic_id.variant,
3194 nic_id.major, nic_id.minor);
3195
3196 priv->firmware_type = determine_firmware_type(&nic_id);
3197
3198 /* Get the firmware version */
3199 err = HERMES_READ_RECORD(hw, USER_BAP, HERMES_RID_STAID, &sta_id);
3200 if (err) {
3201 printk(KERN_ERR "%s: Cannot read station identity: error %d\n",
3202 dev->name, err);
3203 return err;
3204 }
3205
3206 le16_to_cpus(&sta_id.id);
3207 le16_to_cpus(&sta_id.variant);
3208 le16_to_cpus(&sta_id.major);
3209 le16_to_cpus(&sta_id.minor);
3210 printk(KERN_DEBUG "%s: Station identity %04x:%04x:%04x:%04x\n",
3211 dev->name, sta_id.id, sta_id.variant,
3212 sta_id.major, sta_id.minor);
3213
3214 switch (sta_id.id) {
3215 case 0x15:
3216 printk(KERN_ERR "%s: Primary firmware is active\n",
3217 dev->name);
3218 return -ENODEV;
3219 case 0x14b:
3220 printk(KERN_ERR "%s: Tertiary firmware is active\n",
3221 dev->name);
3222 return -ENODEV;
3223 case 0x1f: /* Intersil, Agere, Symbol Spectrum24 */
3224 case 0x21: /* Symbol Spectrum24 Trilogy */
3225 break;
3226 default:
3227 printk(KERN_NOTICE "%s: Unknown station ID, please report\n",
3228 dev->name);
3229 break;
3230 }
3231
3232 /* Default capabilities */
3233 priv->has_sensitivity = 1;
3234 priv->has_mwo = 0;
3235 priv->has_preamble = 0;
3236 priv->has_port3 = 1;
3237 priv->has_ibss = 1;
3238 priv->has_wep = 0;
3239 priv->has_big_wep = 0;
David Kilroy6eecad72008-08-21 23:27:56 +01003240 priv->has_alt_txcntl = 0;
David Kilroy01632fa2008-08-21 23:27:58 +01003241 priv->has_ext_scan = 0;
David Kilroyd03032a2008-08-21 23:28:02 +01003242 priv->has_wpa = 0;
David Kilroy3994d502008-08-21 23:27:54 +01003243 priv->do_fw_download = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
3245 /* Determine capabilities from the firmware version */
3246 switch (priv->firmware_type) {
3247 case FIRMWARE_TYPE_AGERE:
3248 /* Lucent Wavelan IEEE, Lucent Orinoco, Cabletron RoamAbout,
3249 ELSA, Melco, HP, IBM, Dell 1150, Compaq 110/210 */
3250 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
3251 "Lucent/Agere %d.%02d", sta_id.major, sta_id.minor);
3252
3253 firmver = ((unsigned long)sta_id.major << 16) | sta_id.minor;
3254
3255 priv->has_ibss = (firmver >= 0x60006);
3256 priv->has_wep = (firmver >= 0x40020);
3257 priv->has_big_wep = 1; /* FIXME: this is wrong - how do we tell
3258 Gold cards from the others? */
3259 priv->has_mwo = (firmver >= 0x60000);
3260 priv->has_pm = (firmver >= 0x40020); /* Don't work in 7.52 ? */
3261 priv->ibss_port = 1;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003262 priv->has_hostscan = (firmver >= 0x8000a);
David Kilroy3994d502008-08-21 23:27:54 +01003263 priv->do_fw_download = 1;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02003264 priv->broken_monitor = (firmver >= 0x80000);
David Kilroy6eecad72008-08-21 23:27:56 +01003265 priv->has_alt_txcntl = (firmver >= 0x90000); /* All 9.x ? */
David Kilroy01632fa2008-08-21 23:27:58 +01003266 priv->has_ext_scan = (firmver >= 0x90000); /* All 9.x ? */
David Kilroyd03032a2008-08-21 23:28:02 +01003267 priv->has_wpa = (firmver >= 0x9002a);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 /* Tested with Agere firmware :
3269 * 1.16 ; 4.08 ; 4.52 ; 6.04 ; 6.16 ; 7.28 => Jean II
3270 * Tested CableTron firmware : 4.32 => Anton */
3271 break;
3272 case FIRMWARE_TYPE_SYMBOL:
3273 /* Symbol , 3Com AirConnect, Intel, Ericsson WLAN */
3274 /* Intel MAC : 00:02:B3:* */
3275 /* 3Com MAC : 00:50:DA:* */
3276 memset(tmp, 0, sizeof(tmp));
3277 /* Get the Symbol firmware version */
3278 err = hermes_read_ltv(hw, USER_BAP,
3279 HERMES_RID_SECONDARYVERSION_SYMBOL,
3280 SYMBOL_MAX_VER_LEN, NULL, &tmp);
3281 if (err) {
3282 printk(KERN_WARNING
3283 "%s: Error %d reading Symbol firmware info. Wildly guessing capabilities...\n",
3284 dev->name, err);
3285 firmver = 0;
3286 tmp[0] = '\0';
3287 } else {
3288 /* The firmware revision is a string, the format is
3289 * something like : "V2.20-01".
3290 * Quick and dirty parsing... - Jean II
3291 */
3292 firmver = ((tmp[1] - '0') << 16) | ((tmp[3] - '0') << 12)
3293 | ((tmp[4] - '0') << 8) | ((tmp[6] - '0') << 4)
3294 | (tmp[7] - '0');
3295
3296 tmp[SYMBOL_MAX_VER_LEN] = '\0';
3297 }
3298
3299 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
3300 "Symbol %s", tmp);
3301
3302 priv->has_ibss = (firmver >= 0x20000);
3303 priv->has_wep = (firmver >= 0x15012);
3304 priv->has_big_wep = (firmver >= 0x20000);
3305 priv->has_pm = (firmver >= 0x20000 && firmver < 0x22000) ||
3306 (firmver >= 0x29000 && firmver < 0x30000) ||
3307 firmver >= 0x31000;
3308 priv->has_preamble = (firmver >= 0x20000);
3309 priv->ibss_port = 4;
David Kilroy3994d502008-08-21 23:27:54 +01003310
3311 /* Symbol firmware is found on various cards, but
3312 * there has been no attempt to check firmware
3313 * download on non-spectrum_cs based cards.
3314 *
3315 * Given that the Agere firmware download works
3316 * differently, we should avoid doing a firmware
3317 * download with the Symbol algorithm on non-spectrum
3318 * cards.
3319 *
3320 * For now we can identify a spectrum_cs based card
3321 * because it has a firmware reset function.
3322 */
3323 priv->do_fw_download = (priv->stop_fw != NULL);
3324
Christoph Hellwig649e59e2005-05-14 17:30:10 +02003325 priv->broken_disableport = (firmver == 0x25013) ||
3326 (firmver >= 0x30000 && firmver <= 0x31000);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003327 priv->has_hostscan = (firmver >= 0x31001) ||
3328 (firmver >= 0x29057 && firmver < 0x30000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 /* Tested with Intel firmware : 0x20015 => Jean II */
3330 /* Tested with 3Com firmware : 0x15012 & 0x22001 => Jean II */
3331 break;
3332 case FIRMWARE_TYPE_INTERSIL:
3333 /* D-Link, Linksys, Adtron, ZoomAir, and many others...
3334 * Samsung, Compaq 100/200 and Proxim are slightly
3335 * different and less well tested */
3336 /* D-Link MAC : 00:40:05:* */
3337 /* Addtron MAC : 00:90:D1:* */
3338 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
3339 "Intersil %d.%d.%d", sta_id.major, sta_id.minor,
3340 sta_id.variant);
3341
3342 firmver = ((unsigned long)sta_id.major << 16) |
3343 ((unsigned long)sta_id.minor << 8) | sta_id.variant;
3344
3345 priv->has_ibss = (firmver >= 0x000700); /* FIXME */
3346 priv->has_big_wep = priv->has_wep = (firmver >= 0x000800);
3347 priv->has_pm = (firmver >= 0x000700);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003348 priv->has_hostscan = (firmver >= 0x010301);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
3350 if (firmver >= 0x000800)
3351 priv->ibss_port = 0;
3352 else {
3353 printk(KERN_NOTICE "%s: Intersil firmware earlier "
3354 "than v0.8.x - several features not supported\n",
3355 dev->name);
3356 priv->ibss_port = 1;
3357 }
3358 break;
3359 }
3360 printk(KERN_DEBUG "%s: Firmware determined as %s\n", dev->name,
3361 priv->fw_name);
3362
3363 return 0;
3364}
3365
3366static int orinoco_init(struct net_device *dev)
3367{
3368 struct orinoco_private *priv = netdev_priv(dev);
3369 hermes_t *hw = &priv->hw;
3370 int err = 0;
3371 struct hermes_idstring nickbuf;
3372 u16 reclen;
3373 int len;
3374
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 /* No need to lock, the hw_unavailable flag is already set in
3376 * alloc_orinocodev() */
Johannes Berg2c7060022008-10-30 22:09:54 +01003377 priv->nicbuf_size = IEEE80211_MAX_FRAME_LEN + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378
3379 /* Initialize the firmware */
Pavel Roskin37a6c612006-04-07 04:10:49 -04003380 err = hermes_init(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 if (err != 0) {
3382 printk(KERN_ERR "%s: failed to initialize firmware (err = %d)\n",
3383 dev->name, err);
3384 goto out;
3385 }
3386
3387 err = determine_firmware(dev);
3388 if (err != 0) {
3389 printk(KERN_ERR "%s: Incompatible firmware, aborting\n",
3390 dev->name);
3391 goto out;
3392 }
3393
David Kilroy3994d502008-08-21 23:27:54 +01003394 if (priv->do_fw_download) {
David Kilroy39d1ffe2008-11-22 10:37:28 +00003395#ifdef CONFIG_HERMES_CACHE_FW_ON_INIT
David Kilroy74734312008-11-22 10:37:25 +00003396 orinoco_cache_fw(priv, 0);
David Kilroy39d1ffe2008-11-22 10:37:28 +00003397#endif
David Kilroy74734312008-11-22 10:37:25 +00003398
David Kilroy3994d502008-08-21 23:27:54 +01003399 err = orinoco_download(priv);
3400 if (err)
3401 priv->do_fw_download = 0;
3402
3403 /* Check firmware version again */
3404 err = determine_firmware(dev);
3405 if (err != 0) {
3406 printk(KERN_ERR "%s: Incompatible firmware, aborting\n",
3407 dev->name);
3408 goto out;
3409 }
3410 }
3411
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 if (priv->has_port3)
3413 printk(KERN_DEBUG "%s: Ad-hoc demo mode supported\n", dev->name);
3414 if (priv->has_ibss)
3415 printk(KERN_DEBUG "%s: IEEE standard IBSS ad-hoc mode supported\n",
3416 dev->name);
3417 if (priv->has_wep) {
3418 printk(KERN_DEBUG "%s: WEP supported, ", dev->name);
3419 if (priv->has_big_wep)
3420 printk("104-bit key\n");
3421 else
3422 printk("40-bit key\n");
3423 }
David Kilroy23edcc42008-08-21 23:28:05 +01003424 if (priv->has_wpa) {
David Kilroyd03032a2008-08-21 23:28:02 +01003425 printk(KERN_DEBUG "%s: WPA-PSK supported\n", dev->name);
David Kilroy23edcc42008-08-21 23:28:05 +01003426 if (orinoco_mic_init(priv)) {
3427 printk(KERN_ERR "%s: Failed to setup MIC crypto "
3428 "algorithm. Disabling WPA support\n", dev->name);
3429 priv->has_wpa = 0;
3430 }
3431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432
David Kilroy01632fa2008-08-21 23:27:58 +01003433 /* Now we have the firmware capabilities, allocate appropiate
3434 * sized scan buffers */
3435 if (orinoco_bss_data_allocate(priv))
3436 goto out;
3437 orinoco_bss_data_init(priv);
3438
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 /* Get the MAC address */
3440 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
3441 ETH_ALEN, NULL, dev->dev_addr);
3442 if (err) {
3443 printk(KERN_WARNING "%s: failed to read MAC address!\n",
3444 dev->name);
3445 goto out;
3446 }
3447
Johannes Berge1749612008-10-27 15:59:26 -07003448 printk(KERN_DEBUG "%s: MAC address %pM\n",
3449 dev->name, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
3451 /* Get the station name */
3452 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
3453 sizeof(nickbuf), &reclen, &nickbuf);
3454 if (err) {
3455 printk(KERN_ERR "%s: failed to read station name\n",
3456 dev->name);
3457 goto out;
3458 }
3459 if (nickbuf.len)
3460 len = min(IW_ESSID_MAX_SIZE, (int)le16_to_cpu(nickbuf.len));
3461 else
3462 len = min(IW_ESSID_MAX_SIZE, 2 * reclen);
3463 memcpy(priv->nick, &nickbuf.val, len);
3464 priv->nick[len] = '\0';
3465
3466 printk(KERN_DEBUG "%s: Station name \"%s\"\n", dev->name, priv->nick);
3467
Pavel Roskin37a6c612006-04-07 04:10:49 -04003468 err = orinoco_allocate_fid(dev);
3469 if (err) {
3470 printk(KERN_ERR "%s: failed to allocate NIC buffer!\n",
3471 dev->name);
3472 goto out;
3473 }
3474
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 /* Get allowed channels */
3476 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CHANNELLIST,
3477 &priv->channel_mask);
3478 if (err) {
3479 printk(KERN_ERR "%s: failed to read channel list!\n",
3480 dev->name);
3481 goto out;
3482 }
3483
3484 /* Get initial AP density */
3485 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFSYSTEMSCALE,
3486 &priv->ap_density);
3487 if (err || priv->ap_density < 1 || priv->ap_density > 3) {
3488 priv->has_sensitivity = 0;
3489 }
3490
3491 /* Get initial RTS threshold */
3492 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
3493 &priv->rts_thresh);
3494 if (err) {
3495 printk(KERN_ERR "%s: failed to read RTS threshold!\n",
3496 dev->name);
3497 goto out;
3498 }
3499
3500 /* Get initial fragmentation settings */
3501 if (priv->has_mwo)
3502 err = hermes_read_wordrec(hw, USER_BAP,
3503 HERMES_RID_CNFMWOROBUST_AGERE,
3504 &priv->mwo_robust);
3505 else
3506 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
3507 &priv->frag_thresh);
3508 if (err) {
3509 printk(KERN_ERR "%s: failed to read fragmentation settings!\n",
3510 dev->name);
3511 goto out;
3512 }
3513
3514 /* Power management setup */
3515 if (priv->has_pm) {
3516 priv->pm_on = 0;
3517 priv->pm_mcast = 1;
3518 err = hermes_read_wordrec(hw, USER_BAP,
3519 HERMES_RID_CNFMAXSLEEPDURATION,
3520 &priv->pm_period);
3521 if (err) {
3522 printk(KERN_ERR "%s: failed to read power management period!\n",
3523 dev->name);
3524 goto out;
3525 }
3526 err = hermes_read_wordrec(hw, USER_BAP,
3527 HERMES_RID_CNFPMHOLDOVERDURATION,
3528 &priv->pm_timeout);
3529 if (err) {
3530 printk(KERN_ERR "%s: failed to read power management timeout!\n",
3531 dev->name);
3532 goto out;
3533 }
3534 }
3535
3536 /* Preamble setup */
3537 if (priv->has_preamble) {
3538 err = hermes_read_wordrec(hw, USER_BAP,
3539 HERMES_RID_CNFPREAMBLE_SYMBOL,
3540 &priv->preamble);
3541 if (err)
3542 goto out;
3543 }
3544
3545 /* Set up the default configuration */
3546 priv->iw_mode = IW_MODE_INFRA;
3547 /* By default use IEEE/IBSS ad-hoc mode if we have it */
3548 priv->prefer_port3 = priv->has_port3 && (! priv->has_ibss);
3549 set_port_type(priv);
David Gibsond51d8b12005-05-12 20:03:36 -04003550 priv->channel = 0; /* use firmware default */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
3552 priv->promiscuous = 0;
David Kilroy4ae6ee22008-08-21 23:27:59 +01003553 priv->encode_alg = IW_ENCODE_ALG_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 priv->tx_key = 0;
David Kilroyd03032a2008-08-21 23:28:02 +01003555 priv->wpa_enabled = 0;
3556 priv->tkip_cm_active = 0;
3557 priv->key_mgmt = 0;
3558 priv->wpa_ie_len = 0;
3559 priv->wpa_ie = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 /* Make the hardware available, as long as it hasn't been
3562 * removed elsewhere (e.g. by PCMCIA hot unplug) */
3563 spin_lock_irq(&priv->lock);
3564 priv->hw_unavailable--;
3565 spin_unlock_irq(&priv->lock);
3566
3567 printk(KERN_DEBUG "%s: ready\n", dev->name);
3568
3569 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 return err;
3571}
3572
David Kilroy3994d502008-08-21 23:27:54 +01003573struct net_device
3574*alloc_orinocodev(int sizeof_card,
3575 struct device *device,
3576 int (*hard_reset)(struct orinoco_private *),
3577 int (*stop_fw)(struct orinoco_private *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578{
3579 struct net_device *dev;
3580 struct orinoco_private *priv;
3581
3582 dev = alloc_etherdev(sizeof(struct orinoco_private) + sizeof_card);
3583 if (! dev)
3584 return NULL;
3585 priv = netdev_priv(dev);
3586 priv->ndev = dev;
3587 if (sizeof_card)
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02003588 priv->card = (void *)((unsigned long)priv
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 + sizeof(struct orinoco_private));
3590 else
3591 priv->card = NULL;
David Kilroy3994d502008-08-21 23:27:54 +01003592 priv->dev = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
3594 /* Setup / override net_device fields */
3595 dev->init = orinoco_init;
3596 dev->hard_start_xmit = orinoco_xmit;
3597 dev->tx_timeout = orinoco_tx_timeout;
3598 dev->watchdog_timeo = HZ; /* 1 second timeout */
3599 dev->get_stats = orinoco_get_stats;
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02003600 dev->ethtool_ops = &orinoco_ethtool_ops;
Christoph Hellwig620554e2005-06-19 01:27:33 +02003601 dev->wireless_handlers = (struct iw_handler_def *)&orinoco_handler_def;
Pavel Roskin343c6862005-09-09 18:43:02 -04003602#ifdef WIRELESS_SPY
3603 priv->wireless_data.spy_data = &priv->spy_data;
3604 dev->wireless_data = &priv->wireless_data;
3605#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 dev->change_mtu = orinoco_change_mtu;
3607 dev->set_multicast_list = orinoco_set_multicast_list;
3608 /* we use the default eth_mac_addr for setting the MAC addr */
3609
David Kilroy23edcc42008-08-21 23:28:05 +01003610 /* Reserve space in skb for the SNAP header */
3611 dev->hard_header_len += ENCAPS_OVERHEAD;
3612
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 /* Set up default callbacks */
3614 dev->open = orinoco_open;
3615 dev->stop = orinoco_stop;
3616 priv->hard_reset = hard_reset;
David Kilroy3994d502008-08-21 23:27:54 +01003617 priv->stop_fw = stop_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618
3619 spin_lock_init(&priv->lock);
3620 priv->open = 0;
3621 priv->hw_unavailable = 1; /* orinoco_init() must clear this
3622 * before anything else touches the
3623 * hardware */
David Howellsc4028952006-11-22 14:57:56 +00003624 INIT_WORK(&priv->reset_work, orinoco_reset);
3625 INIT_WORK(&priv->join_work, orinoco_join_ap);
3626 INIT_WORK(&priv->wevent_work, orinoco_send_wevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
David Kilroy31afcef2008-08-21 23:28:04 +01003628 INIT_LIST_HEAD(&priv->rx_list);
3629 tasklet_init(&priv->rx_tasklet, orinoco_rx_isr_tasklet,
3630 (unsigned long) dev);
3631
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 netif_carrier_off(dev);
3633 priv->last_linkstatus = 0xffff;
3634
David Kilroy2cea7b22008-11-22 10:37:26 +00003635 priv->cached_pri_fw = NULL;
Andrey Borzenkov4fb30782008-10-19 12:06:11 +04003636 priv->cached_fw = NULL;
3637
David Kilroy39d1ffe2008-11-22 10:37:28 +00003638 /* Register PM notifiers */
3639 priv->pm_notifier.notifier_call = orinoco_pm_notifier;
3640 register_pm_notifier(&priv->pm_notifier);
3641
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 return dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643}
3644
3645void free_orinocodev(struct net_device *dev)
3646{
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003647 struct orinoco_private *priv = netdev_priv(dev);
3648
David Kilroy31afcef2008-08-21 23:28:04 +01003649 /* No need to empty priv->rx_list: if the tasklet is scheduled
3650 * when we call tasklet_kill it will run one final time,
3651 * emptying the list */
3652 tasklet_kill(&priv->rx_tasklet);
David Kilroy74734312008-11-22 10:37:25 +00003653
David Kilroy39d1ffe2008-11-22 10:37:28 +00003654 unregister_pm_notifier(&priv->pm_notifier);
David Kilroy74734312008-11-22 10:37:25 +00003655 orinoco_uncache_fw(priv);
3656
David Kilroyd03032a2008-08-21 23:28:02 +01003657 priv->wpa_ie_len = 0;
3658 kfree(priv->wpa_ie);
David Kilroy23edcc42008-08-21 23:28:05 +01003659 orinoco_mic_free(priv);
Dan Williams1e3428e2007-10-10 23:56:25 -04003660 orinoco_bss_data_free(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 free_netdev(dev);
3662}
3663
3664/********************************************************************/
3665/* Wireless extensions */
3666/********************************************************************/
3667
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07003668/* Return : < 0 -> error code ; >= 0 -> length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669static int orinoco_hw_get_essid(struct orinoco_private *priv, int *active,
3670 char buf[IW_ESSID_MAX_SIZE+1])
3671{
3672 hermes_t *hw = &priv->hw;
3673 int err = 0;
3674 struct hermes_idstring essidbuf;
3675 char *p = (char *)(&essidbuf.val);
3676 int len;
3677 unsigned long flags;
3678
3679 if (orinoco_lock(priv, &flags) != 0)
3680 return -EBUSY;
3681
3682 if (strlen(priv->desired_essid) > 0) {
3683 /* We read the desired SSID from the hardware rather
3684 than from priv->desired_essid, just in case the
3685 firmware is allowed to change it on us. I'm not
3686 sure about this */
3687 /* My guess is that the OWNSSID should always be whatever
3688 * we set to the card, whereas CURRENT_SSID is the one that
3689 * may change... - Jean II */
3690 u16 rid;
3691
3692 *active = 1;
3693
3694 rid = (priv->port_type == 3) ? HERMES_RID_CNFOWNSSID :
3695 HERMES_RID_CNFDESIREDSSID;
3696
3697 err = hermes_read_ltv(hw, USER_BAP, rid, sizeof(essidbuf),
3698 NULL, &essidbuf);
3699 if (err)
3700 goto fail_unlock;
3701 } else {
3702 *active = 0;
3703
3704 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTSSID,
3705 sizeof(essidbuf), NULL, &essidbuf);
3706 if (err)
3707 goto fail_unlock;
3708 }
3709
3710 len = le16_to_cpu(essidbuf.len);
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02003711 BUG_ON(len > IW_ESSID_MAX_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07003713 memset(buf, 0, IW_ESSID_MAX_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 memcpy(buf, p, len);
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07003715 err = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
3717 fail_unlock:
3718 orinoco_unlock(priv, &flags);
3719
3720 return err;
3721}
3722
3723static long orinoco_hw_get_freq(struct orinoco_private *priv)
3724{
3725
3726 hermes_t *hw = &priv->hw;
3727 int err = 0;
3728 u16 channel;
3729 long freq = 0;
3730 unsigned long flags;
3731
3732 if (orinoco_lock(priv, &flags) != 0)
3733 return -EBUSY;
3734
3735 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CURRENTCHANNEL, &channel);
3736 if (err)
3737 goto out;
3738
3739 /* Intersil firmware 1.3.5 returns 0 when the interface is down */
3740 if (channel == 0) {
3741 err = -EBUSY;
3742 goto out;
3743 }
3744
3745 if ( (channel < 1) || (channel > NUM_CHANNELS) ) {
3746 printk(KERN_WARNING "%s: Channel out of range (%d)!\n",
3747 priv->ndev->name, channel);
3748 err = -EBUSY;
3749 goto out;
3750
3751 }
3752 freq = channel_frequency[channel-1] * 100000;
3753
3754 out:
3755 orinoco_unlock(priv, &flags);
3756
3757 if (err > 0)
3758 err = -EBUSY;
3759 return err ? err : freq;
3760}
3761
3762static int orinoco_hw_get_bitratelist(struct orinoco_private *priv,
3763 int *numrates, s32 *rates, int max)
3764{
3765 hermes_t *hw = &priv->hw;
3766 struct hermes_idstring list;
3767 unsigned char *p = (unsigned char *)&list.val;
3768 int err = 0;
3769 int num;
3770 int i;
3771 unsigned long flags;
3772
3773 if (orinoco_lock(priv, &flags) != 0)
3774 return -EBUSY;
3775
3776 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_SUPPORTEDDATARATES,
3777 sizeof(list), NULL, &list);
3778 orinoco_unlock(priv, &flags);
3779
3780 if (err)
3781 return err;
3782
3783 num = le16_to_cpu(list.len);
3784 *numrates = num;
3785 num = min(num, max);
3786
3787 for (i = 0; i < num; i++) {
3788 rates[i] = (p[i] & 0x7f) * 500000; /* convert to bps */
3789 }
3790
3791 return 0;
3792}
3793
Christoph Hellwig620554e2005-06-19 01:27:33 +02003794static int orinoco_ioctl_getname(struct net_device *dev,
3795 struct iw_request_info *info,
3796 char *name,
3797 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798{
3799 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 int numrates;
Christoph Hellwig620554e2005-06-19 01:27:33 +02003801 int err;
3802
3803 err = orinoco_hw_get_bitratelist(priv, &numrates, NULL, 0);
3804
3805 if (!err && (numrates > 2))
3806 strcpy(name, "IEEE 802.11b");
3807 else
3808 strcpy(name, "IEEE 802.11-DS");
3809
3810 return 0;
3811}
3812
Christoph Hellwig16739b02005-06-19 01:27:51 +02003813static int orinoco_ioctl_setwap(struct net_device *dev,
3814 struct iw_request_info *info,
3815 struct sockaddr *ap_addr,
3816 char *extra)
3817{
3818 struct orinoco_private *priv = netdev_priv(dev);
3819 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 unsigned long flags;
Christoph Hellwig16739b02005-06-19 01:27:51 +02003821 static const u8 off_addr[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3822 static const u8 any_addr[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823
3824 if (orinoco_lock(priv, &flags) != 0)
3825 return -EBUSY;
3826
Christoph Hellwig16739b02005-06-19 01:27:51 +02003827 /* Enable automatic roaming - no sanity checks are needed */
3828 if (memcmp(&ap_addr->sa_data, off_addr, ETH_ALEN) == 0 ||
3829 memcmp(&ap_addr->sa_data, any_addr, ETH_ALEN) == 0) {
3830 priv->bssid_fixed = 0;
3831 memset(priv->desired_bssid, 0, ETH_ALEN);
3832
3833 /* "off" means keep existing connection */
3834 if (ap_addr->sa_data[0] == 0) {
3835 __orinoco_hw_set_wap(priv);
3836 err = 0;
3837 }
3838 goto out;
3839 }
3840
3841 if (priv->firmware_type == FIRMWARE_TYPE_AGERE) {
3842 printk(KERN_WARNING "%s: Lucent/Agere firmware doesn't "
3843 "support manual roaming\n",
3844 dev->name);
3845 err = -EOPNOTSUPP;
3846 goto out;
3847 }
3848
3849 if (priv->iw_mode != IW_MODE_INFRA) {
3850 printk(KERN_WARNING "%s: Manual roaming supported only in "
3851 "managed mode\n", dev->name);
3852 err = -EOPNOTSUPP;
3853 goto out;
3854 }
3855
3856 /* Intersil firmware hangs without Desired ESSID */
3857 if (priv->firmware_type == FIRMWARE_TYPE_INTERSIL &&
3858 strlen(priv->desired_essid) == 0) {
3859 printk(KERN_WARNING "%s: Desired ESSID must be set for "
3860 "manual roaming\n", dev->name);
3861 err = -EOPNOTSUPP;
3862 goto out;
3863 }
3864
3865 /* Finally, enable manual roaming */
3866 priv->bssid_fixed = 1;
3867 memcpy(priv->desired_bssid, &ap_addr->sa_data, ETH_ALEN);
3868
3869 out:
3870 orinoco_unlock(priv, &flags);
3871 return err;
3872}
3873
Christoph Hellwig620554e2005-06-19 01:27:33 +02003874static int orinoco_ioctl_getwap(struct net_device *dev,
3875 struct iw_request_info *info,
3876 struct sockaddr *ap_addr,
3877 char *extra)
3878{
3879 struct orinoco_private *priv = netdev_priv(dev);
3880
3881 hermes_t *hw = &priv->hw;
3882 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 unsigned long flags;
3884
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 if (orinoco_lock(priv, &flags) != 0)
3886 return -EBUSY;
3887
Christoph Hellwig620554e2005-06-19 01:27:33 +02003888 ap_addr->sa_family = ARPHRD_ETHER;
3889 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
3890 ETH_ALEN, NULL, ap_addr->sa_data);
3891
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 orinoco_unlock(priv, &flags);
3893
Christoph Hellwig620554e2005-06-19 01:27:33 +02003894 return err;
3895}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896
Christoph Hellwig620554e2005-06-19 01:27:33 +02003897static int orinoco_ioctl_setmode(struct net_device *dev,
3898 struct iw_request_info *info,
3899 u32 *mode,
3900 char *extra)
3901{
3902 struct orinoco_private *priv = netdev_priv(dev);
3903 int err = -EINPROGRESS; /* Call commit handler */
3904 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905
Christoph Hellwig620554e2005-06-19 01:27:33 +02003906 if (priv->iw_mode == *mode)
3907 return 0;
3908
3909 if (orinoco_lock(priv, &flags) != 0)
3910 return -EBUSY;
3911
3912 switch (*mode) {
3913 case IW_MODE_ADHOC:
3914 if (!priv->has_ibss && !priv->has_port3)
3915 err = -EOPNOTSUPP;
3916 break;
3917
3918 case IW_MODE_INFRA:
3919 break;
3920
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02003921 case IW_MODE_MONITOR:
3922 if (priv->broken_monitor && !force_monitor) {
3923 printk(KERN_WARNING "%s: Monitor mode support is "
3924 "buggy in this firmware, not enabling\n",
3925 dev->name);
3926 err = -EOPNOTSUPP;
3927 }
3928 break;
3929
Christoph Hellwig620554e2005-06-19 01:27:33 +02003930 default:
3931 err = -EOPNOTSUPP;
3932 break;
3933 }
3934
3935 if (err == -EINPROGRESS) {
3936 priv->iw_mode = *mode;
3937 set_port_type(priv);
3938 }
3939
3940 orinoco_unlock(priv, &flags);
3941
3942 return err;
3943}
3944
3945static int orinoco_ioctl_getmode(struct net_device *dev,
3946 struct iw_request_info *info,
3947 u32 *mode,
3948 char *extra)
3949{
3950 struct orinoco_private *priv = netdev_priv(dev);
3951
3952 *mode = priv->iw_mode;
3953 return 0;
3954}
3955
3956static int orinoco_ioctl_getiwrange(struct net_device *dev,
3957 struct iw_request_info *info,
3958 struct iw_point *rrq,
3959 char *extra)
3960{
3961 struct orinoco_private *priv = netdev_priv(dev);
3962 int err = 0;
3963 struct iw_range *range = (struct iw_range *) extra;
3964 int numrates;
3965 int i, k;
3966
Christoph Hellwig620554e2005-06-19 01:27:33 +02003967 rrq->length = sizeof(struct iw_range);
3968 memset(range, 0, sizeof(struct iw_range));
3969
3970 range->we_version_compiled = WIRELESS_EXT;
David Kilroyd03032a2008-08-21 23:28:02 +01003971 range->we_version_source = 22;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972
3973 /* Set available channels/frequencies */
Christoph Hellwig620554e2005-06-19 01:27:33 +02003974 range->num_channels = NUM_CHANNELS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 k = 0;
3976 for (i = 0; i < NUM_CHANNELS; i++) {
3977 if (priv->channel_mask & (1 << i)) {
Christoph Hellwig620554e2005-06-19 01:27:33 +02003978 range->freq[k].i = i + 1;
3979 range->freq[k].m = channel_frequency[i] * 100000;
3980 range->freq[k].e = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 k++;
3982 }
3983
3984 if (k >= IW_MAX_FREQUENCIES)
3985 break;
3986 }
Christoph Hellwig620554e2005-06-19 01:27:33 +02003987 range->num_frequency = k;
3988 range->sensitivity = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989
Christoph Hellwig620554e2005-06-19 01:27:33 +02003990 if (priv->has_wep) {
3991 range->max_encoding_tokens = ORINOCO_MAX_KEYS;
3992 range->encoding_size[0] = SMALL_KEY_SIZE;
3993 range->num_encoding_sizes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
Christoph Hellwig620554e2005-06-19 01:27:33 +02003995 if (priv->has_big_wep) {
3996 range->encoding_size[1] = LARGE_KEY_SIZE;
3997 range->num_encoding_sizes = 2;
3998 }
3999 }
4000
David Kilroyd03032a2008-08-21 23:28:02 +01004001 if (priv->has_wpa)
4002 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_CIPHER_TKIP;
4003
Pavel Roskin343c6862005-09-09 18:43:02 -04004004 if ((priv->iw_mode == IW_MODE_ADHOC) && (!SPY_NUMBER(priv))){
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 /* Quality stats meaningless in ad-hoc mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 } else {
Christoph Hellwig620554e2005-06-19 01:27:33 +02004007 range->max_qual.qual = 0x8b - 0x2f;
4008 range->max_qual.level = 0x2f - 0x95 - 1;
4009 range->max_qual.noise = 0x2f - 0x95 - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 /* Need to get better values */
Christoph Hellwig620554e2005-06-19 01:27:33 +02004011 range->avg_qual.qual = 0x24;
4012 range->avg_qual.level = 0xC2;
4013 range->avg_qual.noise = 0x9E;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 }
4015
4016 err = orinoco_hw_get_bitratelist(priv, &numrates,
Christoph Hellwig620554e2005-06-19 01:27:33 +02004017 range->bitrate, IW_MAX_BITRATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 if (err)
4019 return err;
Christoph Hellwig620554e2005-06-19 01:27:33 +02004020 range->num_bitrates = numrates;
4021
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 /* Set an indication of the max TCP throughput in bit/s that we can
4023 * expect using this interface. May be use for QoS stuff...
4024 * Jean II */
Christoph Hellwig620554e2005-06-19 01:27:33 +02004025 if (numrates > 2)
4026 range->throughput = 5 * 1000 * 1000; /* ~5 Mb/s */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 else
Christoph Hellwig620554e2005-06-19 01:27:33 +02004028 range->throughput = 1.5 * 1000 * 1000; /* ~1.5 Mb/s */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
Christoph Hellwig620554e2005-06-19 01:27:33 +02004030 range->min_rts = 0;
4031 range->max_rts = 2347;
4032 range->min_frag = 256;
4033 range->max_frag = 2346;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034
Christoph Hellwig620554e2005-06-19 01:27:33 +02004035 range->min_pmp = 0;
4036 range->max_pmp = 65535000;
4037 range->min_pmt = 0;
4038 range->max_pmt = 65535 * 1000; /* ??? */
4039 range->pmp_flags = IW_POWER_PERIOD;
4040 range->pmt_flags = IW_POWER_TIMEOUT;
4041 range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_UNICAST_R;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
Christoph Hellwig620554e2005-06-19 01:27:33 +02004043 range->retry_capa = IW_RETRY_LIMIT | IW_RETRY_LIFETIME;
4044 range->retry_flags = IW_RETRY_LIMIT;
4045 range->r_time_flags = IW_RETRY_LIFETIME;
4046 range->min_retry = 0;
4047 range->max_retry = 65535; /* ??? */
4048 range->min_r_time = 0;
4049 range->max_r_time = 65535 * 1000; /* ??? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050
David Kilroy0753bba2008-08-21 23:27:46 +01004051 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
4052 range->scan_capa = IW_SCAN_CAPA_ESSID;
4053 else
4054 range->scan_capa = IW_SCAN_CAPA_NONE;
4055
Pavel Roskin343c6862005-09-09 18:43:02 -04004056 /* Event capability (kernel) */
4057 IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
4058 /* Event capability (driver) */
4059 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWTHRSPY);
4060 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
4061 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
4062 IW_EVENT_CAPA_SET(range->event_capa, IWEVTXDROP);
4063
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 return 0;
4065}
4066
Christoph Hellwig620554e2005-06-19 01:27:33 +02004067static int orinoco_ioctl_setiwencode(struct net_device *dev,
4068 struct iw_request_info *info,
4069 struct iw_point *erq,
4070 char *keybuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071{
4072 struct orinoco_private *priv = netdev_priv(dev);
4073 int index = (erq->flags & IW_ENCODE_INDEX) - 1;
4074 int setindex = priv->tx_key;
David Kilroy4ae6ee22008-08-21 23:27:59 +01004075 int encode_alg = priv->encode_alg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 int restricted = priv->wep_restrict;
4077 u16 xlen = 0;
Christoph Hellwig620554e2005-06-19 01:27:33 +02004078 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 unsigned long flags;
4080
4081 if (! priv->has_wep)
4082 return -EOPNOTSUPP;
4083
4084 if (erq->pointer) {
4085 /* We actually have a key to set - check its length */
4086 if (erq->length > LARGE_KEY_SIZE)
4087 return -E2BIG;
4088
4089 if ( (erq->length > SMALL_KEY_SIZE) && !priv->has_big_wep )
4090 return -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 }
4092
4093 if (orinoco_lock(priv, &flags) != 0)
4094 return -EBUSY;
4095
David Kilroyd03032a2008-08-21 23:28:02 +01004096 /* Clear any TKIP key we have */
4097 if ((priv->has_wpa) && (priv->encode_alg == IW_ENCODE_ALG_TKIP))
4098 (void) orinoco_clear_tkip_key(priv, setindex);
4099
Dan Williamsfe397d42006-07-14 11:41:47 -04004100 if (erq->length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
4102 index = priv->tx_key;
4103
4104 /* Adjust key length to a supported value */
4105 if (erq->length > SMALL_KEY_SIZE) {
4106 xlen = LARGE_KEY_SIZE;
4107 } else if (erq->length > 0) {
4108 xlen = SMALL_KEY_SIZE;
4109 } else
4110 xlen = 0;
4111
4112 /* Switch on WEP if off */
David Kilroy4ae6ee22008-08-21 23:27:59 +01004113 if ((encode_alg != IW_ENCODE_ALG_WEP) && (xlen > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 setindex = index;
David Kilroy4ae6ee22008-08-21 23:27:59 +01004115 encode_alg = IW_ENCODE_ALG_WEP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 }
4117 } else {
4118 /* Important note : if the user do "iwconfig eth0 enc off",
4119 * we will arrive there with an index of -1. This is valid
4120 * but need to be taken care off... Jean II */
4121 if ((index < 0) || (index >= ORINOCO_MAX_KEYS)) {
4122 if((index != -1) || (erq->flags == 0)) {
4123 err = -EINVAL;
4124 goto out;
4125 }
4126 } else {
4127 /* Set the index : Check that the key is valid */
4128 if(priv->keys[index].len == 0) {
4129 err = -EINVAL;
4130 goto out;
4131 }
4132 setindex = index;
4133 }
4134 }
4135
4136 if (erq->flags & IW_ENCODE_DISABLED)
David Kilroy4ae6ee22008-08-21 23:27:59 +01004137 encode_alg = IW_ENCODE_ALG_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 if (erq->flags & IW_ENCODE_OPEN)
4139 restricted = 0;
4140 if (erq->flags & IW_ENCODE_RESTRICTED)
4141 restricted = 1;
4142
Dan Williamsfe397d42006-07-14 11:41:47 -04004143 if (erq->pointer && erq->length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 priv->keys[index].len = cpu_to_le16(xlen);
4145 memset(priv->keys[index].data, 0,
4146 sizeof(priv->keys[index].data));
4147 memcpy(priv->keys[index].data, keybuf, erq->length);
4148 }
4149 priv->tx_key = setindex;
4150
4151 /* Try fast key change if connected and only keys are changed */
David Kilroy4ae6ee22008-08-21 23:27:59 +01004152 if ((priv->encode_alg == encode_alg) &&
4153 (priv->wep_restrict == restricted) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 netif_carrier_ok(dev)) {
4155 err = __orinoco_hw_setup_wepkeys(priv);
4156 /* No need to commit if successful */
4157 goto out;
4158 }
4159
David Kilroy4ae6ee22008-08-21 23:27:59 +01004160 priv->encode_alg = encode_alg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 priv->wep_restrict = restricted;
4162
4163 out:
4164 orinoco_unlock(priv, &flags);
4165
4166 return err;
4167}
4168
Christoph Hellwig620554e2005-06-19 01:27:33 +02004169static int orinoco_ioctl_getiwencode(struct net_device *dev,
4170 struct iw_request_info *info,
4171 struct iw_point *erq,
4172 char *keybuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173{
4174 struct orinoco_private *priv = netdev_priv(dev);
4175 int index = (erq->flags & IW_ENCODE_INDEX) - 1;
4176 u16 xlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 unsigned long flags;
4178
4179 if (! priv->has_wep)
4180 return -EOPNOTSUPP;
4181
4182 if (orinoco_lock(priv, &flags) != 0)
4183 return -EBUSY;
4184
4185 if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
4186 index = priv->tx_key;
4187
4188 erq->flags = 0;
David Kilroy4ae6ee22008-08-21 23:27:59 +01004189 if (!priv->encode_alg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 erq->flags |= IW_ENCODE_DISABLED;
4191 erq->flags |= index + 1;
4192
4193 if (priv->wep_restrict)
4194 erq->flags |= IW_ENCODE_RESTRICTED;
4195 else
4196 erq->flags |= IW_ENCODE_OPEN;
4197
4198 xlen = le16_to_cpu(priv->keys[index].len);
4199
4200 erq->length = xlen;
4201
4202 memcpy(keybuf, priv->keys[index].data, ORINOCO_MAX_KEY_SIZE);
4203
4204 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 return 0;
4206}
4207
Christoph Hellwig620554e2005-06-19 01:27:33 +02004208static int orinoco_ioctl_setessid(struct net_device *dev,
4209 struct iw_request_info *info,
4210 struct iw_point *erq,
4211 char *essidbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212{
4213 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 unsigned long flags;
4215
4216 /* Note : ESSID is ignored in Ad-Hoc demo mode, but we can set it
4217 * anyway... - Jean II */
4218
Christoph Hellwig620554e2005-06-19 01:27:33 +02004219 /* Hum... Should not use Wireless Extension constant (may change),
4220 * should use our own... - Jean II */
4221 if (erq->length > IW_ESSID_MAX_SIZE)
4222 return -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223
4224 if (orinoco_lock(priv, &flags) != 0)
4225 return -EBUSY;
4226
Christoph Hellwig620554e2005-06-19 01:27:33 +02004227 /* NULL the string (for NULL termination & ESSID = ANY) - Jean II */
4228 memset(priv->desired_essid, 0, sizeof(priv->desired_essid));
4229
4230 /* If not ANY, get the new ESSID */
4231 if (erq->flags) {
4232 memcpy(priv->desired_essid, essidbuf, erq->length);
4233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234
4235 orinoco_unlock(priv, &flags);
4236
Christoph Hellwig620554e2005-06-19 01:27:33 +02004237 return -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238}
4239
Christoph Hellwig620554e2005-06-19 01:27:33 +02004240static int orinoco_ioctl_getessid(struct net_device *dev,
4241 struct iw_request_info *info,
4242 struct iw_point *erq,
4243 char *essidbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244{
4245 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 int active;
4247 int err = 0;
4248 unsigned long flags;
4249
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 if (netif_running(dev)) {
4251 err = orinoco_hw_get_essid(priv, &active, essidbuf);
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07004252 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 return err;
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07004254 erq->length = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 } else {
4256 if (orinoco_lock(priv, &flags) != 0)
4257 return -EBUSY;
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07004258 memcpy(essidbuf, priv->desired_essid, IW_ESSID_MAX_SIZE);
4259 erq->length = strlen(priv->desired_essid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 orinoco_unlock(priv, &flags);
4261 }
4262
4263 erq->flags = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 return 0;
4266}
4267
Christoph Hellwig620554e2005-06-19 01:27:33 +02004268static int orinoco_ioctl_setnick(struct net_device *dev,
4269 struct iw_request_info *info,
4270 struct iw_point *nrq,
4271 char *nickbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272{
4273 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 unsigned long flags;
4275
4276 if (nrq->length > IW_ESSID_MAX_SIZE)
4277 return -E2BIG;
4278
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 if (orinoco_lock(priv, &flags) != 0)
4280 return -EBUSY;
4281
Christoph Hellwig620554e2005-06-19 01:27:33 +02004282 memset(priv->nick, 0, sizeof(priv->nick));
4283 memcpy(priv->nick, nickbuf, nrq->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
4285 orinoco_unlock(priv, &flags);
4286
Christoph Hellwig620554e2005-06-19 01:27:33 +02004287 return -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288}
4289
Christoph Hellwig620554e2005-06-19 01:27:33 +02004290static int orinoco_ioctl_getnick(struct net_device *dev,
4291 struct iw_request_info *info,
4292 struct iw_point *nrq,
4293 char *nickbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294{
4295 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 unsigned long flags;
4297
4298 if (orinoco_lock(priv, &flags) != 0)
4299 return -EBUSY;
4300
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07004301 memcpy(nickbuf, priv->nick, IW_ESSID_MAX_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 orinoco_unlock(priv, &flags);
4303
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07004304 nrq->length = strlen(priv->nick);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 return 0;
4307}
4308
Christoph Hellwig620554e2005-06-19 01:27:33 +02004309static int orinoco_ioctl_setfreq(struct net_device *dev,
4310 struct iw_request_info *info,
4311 struct iw_freq *frq,
4312 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313{
4314 struct orinoco_private *priv = netdev_priv(dev);
4315 int chan = -1;
4316 unsigned long flags;
Christoph Hellwig620554e2005-06-19 01:27:33 +02004317 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02004319 /* In infrastructure mode the AP sets the channel */
4320 if (priv->iw_mode == IW_MODE_INFRA)
4321 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322
4323 if ( (frq->e == 0) && (frq->m <= 1000) ) {
4324 /* Setting by channel number */
4325 chan = frq->m;
4326 } else {
4327 /* Setting by frequency - search the table */
4328 int mult = 1;
4329 int i;
4330
4331 for (i = 0; i < (6 - frq->e); i++)
4332 mult *= 10;
4333
4334 for (i = 0; i < NUM_CHANNELS; i++)
4335 if (frq->m == (channel_frequency[i] * mult))
4336 chan = i+1;
4337 }
4338
4339 if ( (chan < 1) || (chan > NUM_CHANNELS) ||
4340 ! (priv->channel_mask & (1 << (chan-1)) ) )
4341 return -EINVAL;
4342
4343 if (orinoco_lock(priv, &flags) != 0)
4344 return -EBUSY;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02004345
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 priv->channel = chan;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02004347 if (priv->iw_mode == IW_MODE_MONITOR) {
4348 /* Fast channel change - no commit if successful */
4349 hermes_t *hw = &priv->hw;
4350 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
4351 HERMES_TEST_SET_CHANNEL,
4352 chan, NULL);
4353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 orinoco_unlock(priv, &flags);
4355
Christoph Hellwig620554e2005-06-19 01:27:33 +02004356 return err;
4357}
4358
4359static int orinoco_ioctl_getfreq(struct net_device *dev,
4360 struct iw_request_info *info,
4361 struct iw_freq *frq,
4362 char *extra)
4363{
4364 struct orinoco_private *priv = netdev_priv(dev);
4365 int tmp;
4366
4367 /* Locking done in there */
4368 tmp = orinoco_hw_get_freq(priv);
4369 if (tmp < 0) {
4370 return tmp;
4371 }
4372
4373 frq->m = tmp;
4374 frq->e = 1;
4375
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 return 0;
4377}
4378
Christoph Hellwig620554e2005-06-19 01:27:33 +02004379static int orinoco_ioctl_getsens(struct net_device *dev,
4380 struct iw_request_info *info,
4381 struct iw_param *srq,
4382 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383{
4384 struct orinoco_private *priv = netdev_priv(dev);
4385 hermes_t *hw = &priv->hw;
4386 u16 val;
4387 int err;
4388 unsigned long flags;
4389
4390 if (!priv->has_sensitivity)
4391 return -EOPNOTSUPP;
4392
4393 if (orinoco_lock(priv, &flags) != 0)
4394 return -EBUSY;
4395 err = hermes_read_wordrec(hw, USER_BAP,
4396 HERMES_RID_CNFSYSTEMSCALE, &val);
4397 orinoco_unlock(priv, &flags);
4398
4399 if (err)
4400 return err;
4401
4402 srq->value = val;
4403 srq->fixed = 0; /* auto */
4404
4405 return 0;
4406}
4407
Christoph Hellwig620554e2005-06-19 01:27:33 +02004408static int orinoco_ioctl_setsens(struct net_device *dev,
4409 struct iw_request_info *info,
4410 struct iw_param *srq,
4411 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412{
4413 struct orinoco_private *priv = netdev_priv(dev);
4414 int val = srq->value;
4415 unsigned long flags;
4416
4417 if (!priv->has_sensitivity)
4418 return -EOPNOTSUPP;
4419
4420 if ((val < 1) || (val > 3))
4421 return -EINVAL;
4422
4423 if (orinoco_lock(priv, &flags) != 0)
4424 return -EBUSY;
4425 priv->ap_density = val;
4426 orinoco_unlock(priv, &flags);
4427
Christoph Hellwig620554e2005-06-19 01:27:33 +02004428 return -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429}
4430
Christoph Hellwig620554e2005-06-19 01:27:33 +02004431static int orinoco_ioctl_setrts(struct net_device *dev,
4432 struct iw_request_info *info,
4433 struct iw_param *rrq,
4434 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435{
4436 struct orinoco_private *priv = netdev_priv(dev);
4437 int val = rrq->value;
4438 unsigned long flags;
4439
4440 if (rrq->disabled)
4441 val = 2347;
4442
4443 if ( (val < 0) || (val > 2347) )
4444 return -EINVAL;
4445
4446 if (orinoco_lock(priv, &flags) != 0)
4447 return -EBUSY;
4448
4449 priv->rts_thresh = val;
4450 orinoco_unlock(priv, &flags);
4451
Christoph Hellwig620554e2005-06-19 01:27:33 +02004452 return -EINPROGRESS; /* Call commit handler */
4453}
4454
4455static int orinoco_ioctl_getrts(struct net_device *dev,
4456 struct iw_request_info *info,
4457 struct iw_param *rrq,
4458 char *extra)
4459{
4460 struct orinoco_private *priv = netdev_priv(dev);
4461
4462 rrq->value = priv->rts_thresh;
4463 rrq->disabled = (rrq->value == 2347);
4464 rrq->fixed = 1;
4465
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 return 0;
4467}
4468
Christoph Hellwig620554e2005-06-19 01:27:33 +02004469static int orinoco_ioctl_setfrag(struct net_device *dev,
4470 struct iw_request_info *info,
4471 struct iw_param *frq,
4472 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473{
4474 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004475 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 unsigned long flags;
4477
4478 if (orinoco_lock(priv, &flags) != 0)
4479 return -EBUSY;
4480
4481 if (priv->has_mwo) {
4482 if (frq->disabled)
4483 priv->mwo_robust = 0;
4484 else {
4485 if (frq->fixed)
4486 printk(KERN_WARNING "%s: Fixed fragmentation is "
4487 "not supported on this firmware. "
4488 "Using MWO robust instead.\n", dev->name);
4489 priv->mwo_robust = 1;
4490 }
4491 } else {
4492 if (frq->disabled)
4493 priv->frag_thresh = 2346;
4494 else {
4495 if ( (frq->value < 256) || (frq->value > 2346) )
4496 err = -EINVAL;
4497 else
4498 priv->frag_thresh = frq->value & ~0x1; /* must be even */
4499 }
4500 }
4501
4502 orinoco_unlock(priv, &flags);
4503
4504 return err;
4505}
4506
Christoph Hellwig620554e2005-06-19 01:27:33 +02004507static int orinoco_ioctl_getfrag(struct net_device *dev,
4508 struct iw_request_info *info,
4509 struct iw_param *frq,
4510 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511{
4512 struct orinoco_private *priv = netdev_priv(dev);
4513 hermes_t *hw = &priv->hw;
Christoph Hellwig620554e2005-06-19 01:27:33 +02004514 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 u16 val;
4516 unsigned long flags;
4517
4518 if (orinoco_lock(priv, &flags) != 0)
4519 return -EBUSY;
4520
4521 if (priv->has_mwo) {
4522 err = hermes_read_wordrec(hw, USER_BAP,
4523 HERMES_RID_CNFMWOROBUST_AGERE,
4524 &val);
4525 if (err)
4526 val = 0;
4527
4528 frq->value = val ? 2347 : 0;
4529 frq->disabled = ! val;
4530 frq->fixed = 0;
4531 } else {
4532 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
4533 &val);
4534 if (err)
4535 val = 0;
4536
4537 frq->value = val;
4538 frq->disabled = (val >= 2346);
4539 frq->fixed = 1;
4540 }
4541
4542 orinoco_unlock(priv, &flags);
4543
4544 return err;
4545}
4546
Christoph Hellwig620554e2005-06-19 01:27:33 +02004547static int orinoco_ioctl_setrate(struct net_device *dev,
4548 struct iw_request_info *info,
4549 struct iw_param *rrq,
4550 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551{
4552 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 int ratemode = -1;
4554 int bitrate; /* 100s of kilobits */
4555 int i;
4556 unsigned long flags;
4557
4558 /* As the user space doesn't know our highest rate, it uses -1
4559 * to ask us to set the highest rate. Test it using "iwconfig
4560 * ethX rate auto" - Jean II */
4561 if (rrq->value == -1)
4562 bitrate = 110;
4563 else {
4564 if (rrq->value % 100000)
4565 return -EINVAL;
4566 bitrate = rrq->value / 100000;
4567 }
4568
4569 if ( (bitrate != 10) && (bitrate != 20) &&
4570 (bitrate != 55) && (bitrate != 110) )
4571 return -EINVAL;
4572
4573 for (i = 0; i < BITRATE_TABLE_SIZE; i++)
4574 if ( (bitrate_table[i].bitrate == bitrate) &&
4575 (bitrate_table[i].automatic == ! rrq->fixed) ) {
4576 ratemode = i;
4577 break;
4578 }
4579
4580 if (ratemode == -1)
4581 return -EINVAL;
4582
4583 if (orinoco_lock(priv, &flags) != 0)
4584 return -EBUSY;
4585 priv->bitratemode = ratemode;
4586 orinoco_unlock(priv, &flags);
4587
Christoph Hellwig620554e2005-06-19 01:27:33 +02004588 return -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589}
4590
Christoph Hellwig620554e2005-06-19 01:27:33 +02004591static int orinoco_ioctl_getrate(struct net_device *dev,
4592 struct iw_request_info *info,
4593 struct iw_param *rrq,
4594 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595{
4596 struct orinoco_private *priv = netdev_priv(dev);
4597 hermes_t *hw = &priv->hw;
4598 int err = 0;
4599 int ratemode;
4600 int i;
4601 u16 val;
4602 unsigned long flags;
4603
4604 if (orinoco_lock(priv, &flags) != 0)
4605 return -EBUSY;
4606
4607 ratemode = priv->bitratemode;
4608
4609 BUG_ON((ratemode < 0) || (ratemode >= BITRATE_TABLE_SIZE));
4610
4611 rrq->value = bitrate_table[ratemode].bitrate * 100000;
4612 rrq->fixed = ! bitrate_table[ratemode].automatic;
4613 rrq->disabled = 0;
4614
4615 /* If the interface is running we try to find more about the
4616 current mode */
4617 if (netif_running(dev)) {
4618 err = hermes_read_wordrec(hw, USER_BAP,
4619 HERMES_RID_CURRENTTXRATE, &val);
4620 if (err)
4621 goto out;
4622
4623 switch (priv->firmware_type) {
4624 case FIRMWARE_TYPE_AGERE: /* Lucent style rate */
4625 /* Note : in Lucent firmware, the return value of
4626 * HERMES_RID_CURRENTTXRATE is the bitrate in Mb/s,
4627 * and therefore is totally different from the
4628 * encoding of HERMES_RID_CNFTXRATECONTROL.
4629 * Don't forget that 6Mb/s is really 5.5Mb/s */
4630 if (val == 6)
4631 rrq->value = 5500000;
4632 else
4633 rrq->value = val * 1000000;
4634 break;
4635 case FIRMWARE_TYPE_INTERSIL: /* Intersil style rate */
4636 case FIRMWARE_TYPE_SYMBOL: /* Symbol style rate */
4637 for (i = 0; i < BITRATE_TABLE_SIZE; i++)
4638 if (bitrate_table[i].intersil_txratectrl == val) {
4639 ratemode = i;
4640 break;
4641 }
4642 if (i >= BITRATE_TABLE_SIZE)
4643 printk(KERN_INFO "%s: Unable to determine current bitrate (0x%04hx)\n",
4644 dev->name, val);
4645
4646 rrq->value = bitrate_table[ratemode].bitrate * 100000;
4647 break;
4648 default:
4649 BUG();
4650 }
4651 }
4652
4653 out:
4654 orinoco_unlock(priv, &flags);
4655
4656 return err;
4657}
4658
Christoph Hellwig620554e2005-06-19 01:27:33 +02004659static int orinoco_ioctl_setpower(struct net_device *dev,
4660 struct iw_request_info *info,
4661 struct iw_param *prq,
4662 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663{
4664 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004665 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 unsigned long flags;
4667
4668 if (orinoco_lock(priv, &flags) != 0)
4669 return -EBUSY;
4670
4671 if (prq->disabled) {
4672 priv->pm_on = 0;
4673 } else {
4674 switch (prq->flags & IW_POWER_MODE) {
4675 case IW_POWER_UNICAST_R:
4676 priv->pm_mcast = 0;
4677 priv->pm_on = 1;
4678 break;
4679 case IW_POWER_ALL_R:
4680 priv->pm_mcast = 1;
4681 priv->pm_on = 1;
4682 break;
4683 case IW_POWER_ON:
4684 /* No flags : but we may have a value - Jean II */
4685 break;
4686 default:
4687 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 goto out;
Pavel Roskinc08ad1e2005-11-29 02:59:27 -05004689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690
4691 if (prq->flags & IW_POWER_TIMEOUT) {
4692 priv->pm_on = 1;
4693 priv->pm_timeout = prq->value / 1000;
4694 }
4695 if (prq->flags & IW_POWER_PERIOD) {
4696 priv->pm_on = 1;
4697 priv->pm_period = prq->value / 1000;
4698 }
4699 /* It's valid to not have a value if we are just toggling
4700 * the flags... Jean II */
4701 if(!priv->pm_on) {
4702 err = -EINVAL;
4703 goto out;
4704 }
4705 }
4706
4707 out:
4708 orinoco_unlock(priv, &flags);
4709
4710 return err;
4711}
4712
Christoph Hellwig620554e2005-06-19 01:27:33 +02004713static int orinoco_ioctl_getpower(struct net_device *dev,
4714 struct iw_request_info *info,
4715 struct iw_param *prq,
4716 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717{
4718 struct orinoco_private *priv = netdev_priv(dev);
4719 hermes_t *hw = &priv->hw;
4720 int err = 0;
4721 u16 enable, period, timeout, mcast;
4722 unsigned long flags;
4723
4724 if (orinoco_lock(priv, &flags) != 0)
4725 return -EBUSY;
4726
4727 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFPMENABLED, &enable);
4728 if (err)
4729 goto out;
4730
4731 err = hermes_read_wordrec(hw, USER_BAP,
4732 HERMES_RID_CNFMAXSLEEPDURATION, &period);
4733 if (err)
4734 goto out;
4735
4736 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFPMHOLDOVERDURATION, &timeout);
4737 if (err)
4738 goto out;
4739
4740 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFMULTICASTRECEIVE, &mcast);
4741 if (err)
4742 goto out;
4743
4744 prq->disabled = !enable;
4745 /* Note : by default, display the period */
4746 if ((prq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
4747 prq->flags = IW_POWER_TIMEOUT;
4748 prq->value = timeout * 1000;
4749 } else {
4750 prq->flags = IW_POWER_PERIOD;
4751 prq->value = period * 1000;
4752 }
4753 if (mcast)
4754 prq->flags |= IW_POWER_ALL_R;
4755 else
4756 prq->flags |= IW_POWER_UNICAST_R;
4757
4758 out:
4759 orinoco_unlock(priv, &flags);
4760
4761 return err;
4762}
4763
David Kilroyd03032a2008-08-21 23:28:02 +01004764static int orinoco_ioctl_set_encodeext(struct net_device *dev,
4765 struct iw_request_info *info,
4766 union iwreq_data *wrqu,
4767 char *extra)
4768{
4769 struct orinoco_private *priv = netdev_priv(dev);
4770 struct iw_point *encoding = &wrqu->encoding;
4771 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
4772 int idx, alg = ext->alg, set_key = 1;
4773 unsigned long flags;
4774 int err = -EINVAL;
4775 u16 key_len;
4776
4777 if (orinoco_lock(priv, &flags) != 0)
4778 return -EBUSY;
4779
4780 /* Determine and validate the key index */
4781 idx = encoding->flags & IW_ENCODE_INDEX;
4782 if (idx) {
Johannes Berg2c7060022008-10-30 22:09:54 +01004783 if ((idx < 1) || (idx > 4))
David Kilroyd03032a2008-08-21 23:28:02 +01004784 goto out;
4785 idx--;
4786 } else
4787 idx = priv->tx_key;
4788
4789 if (encoding->flags & IW_ENCODE_DISABLED)
4790 alg = IW_ENCODE_ALG_NONE;
4791
4792 if (priv->has_wpa && (alg != IW_ENCODE_ALG_TKIP)) {
4793 /* Clear any TKIP TX key we had */
4794 (void) orinoco_clear_tkip_key(priv, priv->tx_key);
4795 }
4796
4797 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
4798 priv->tx_key = idx;
4799 set_key = ((alg == IW_ENCODE_ALG_TKIP) ||
4800 (ext->key_len > 0)) ? 1 : 0;
4801 }
4802
4803 if (set_key) {
4804 /* Set the requested key first */
4805 switch (alg) {
4806 case IW_ENCODE_ALG_NONE:
4807 priv->encode_alg = alg;
4808 priv->keys[idx].len = 0;
4809 break;
4810
4811 case IW_ENCODE_ALG_WEP:
4812 if (ext->key_len > SMALL_KEY_SIZE)
4813 key_len = LARGE_KEY_SIZE;
4814 else if (ext->key_len > 0)
4815 key_len = SMALL_KEY_SIZE;
4816 else
4817 goto out;
4818
4819 priv->encode_alg = alg;
4820 priv->keys[idx].len = cpu_to_le16(key_len);
4821
4822 key_len = min(ext->key_len, key_len);
4823
4824 memset(priv->keys[idx].data, 0, ORINOCO_MAX_KEY_SIZE);
4825 memcpy(priv->keys[idx].data, ext->key, key_len);
4826 break;
4827
4828 case IW_ENCODE_ALG_TKIP:
4829 {
4830 hermes_t *hw = &priv->hw;
4831 u8 *tkip_iv = NULL;
4832
4833 if (!priv->has_wpa ||
4834 (ext->key_len > sizeof(priv->tkip_key[0])))
4835 goto out;
4836
4837 priv->encode_alg = alg;
4838 memset(&priv->tkip_key[idx], 0,
4839 sizeof(priv->tkip_key[idx]));
4840 memcpy(&priv->tkip_key[idx], ext->key, ext->key_len);
4841
4842 if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID)
4843 tkip_iv = &ext->rx_seq[0];
4844
4845 err = __orinoco_hw_set_tkip_key(hw, idx,
4846 ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY,
4847 (u8 *) &priv->tkip_key[idx],
4848 tkip_iv, NULL);
4849 if (err)
4850 printk(KERN_ERR "%s: Error %d setting TKIP key"
4851 "\n", dev->name, err);
4852
4853 goto out;
4854 }
4855 default:
4856 goto out;
4857 }
4858 }
4859 err = -EINPROGRESS;
4860 out:
4861 orinoco_unlock(priv, &flags);
4862
4863 return err;
4864}
4865
4866static int orinoco_ioctl_get_encodeext(struct net_device *dev,
4867 struct iw_request_info *info,
4868 union iwreq_data *wrqu,
4869 char *extra)
4870{
4871 struct orinoco_private *priv = netdev_priv(dev);
4872 struct iw_point *encoding = &wrqu->encoding;
4873 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
4874 int idx, max_key_len;
4875 unsigned long flags;
4876 int err;
4877
4878 if (orinoco_lock(priv, &flags) != 0)
4879 return -EBUSY;
4880
4881 err = -EINVAL;
4882 max_key_len = encoding->length - sizeof(*ext);
4883 if (max_key_len < 0)
4884 goto out;
4885
4886 idx = encoding->flags & IW_ENCODE_INDEX;
4887 if (idx) {
Johannes Berg2c7060022008-10-30 22:09:54 +01004888 if ((idx < 1) || (idx > 4))
David Kilroyd03032a2008-08-21 23:28:02 +01004889 goto out;
4890 idx--;
4891 } else
4892 idx = priv->tx_key;
4893
4894 encoding->flags = idx + 1;
4895 memset(ext, 0, sizeof(*ext));
4896
4897 ext->alg = priv->encode_alg;
4898 switch (priv->encode_alg) {
4899 case IW_ENCODE_ALG_NONE:
4900 ext->key_len = 0;
4901 encoding->flags |= IW_ENCODE_DISABLED;
4902 break;
4903 case IW_ENCODE_ALG_WEP:
David Kilroy75d31cf2008-09-12 22:28:18 +01004904 ext->key_len = min_t(u16, le16_to_cpu(priv->keys[idx].len),
4905 max_key_len);
David Kilroyd03032a2008-08-21 23:28:02 +01004906 memcpy(ext->key, priv->keys[idx].data, ext->key_len);
4907 encoding->flags |= IW_ENCODE_ENABLED;
4908 break;
4909 case IW_ENCODE_ALG_TKIP:
David Kilroy75d31cf2008-09-12 22:28:18 +01004910 ext->key_len = min_t(u16, sizeof(struct orinoco_tkip_key),
4911 max_key_len);
David Kilroyd03032a2008-08-21 23:28:02 +01004912 memcpy(ext->key, &priv->tkip_key[idx], ext->key_len);
4913 encoding->flags |= IW_ENCODE_ENABLED;
4914 break;
4915 }
4916
4917 err = 0;
4918 out:
4919 orinoco_unlock(priv, &flags);
4920
4921 return err;
4922}
4923
4924static int orinoco_ioctl_set_auth(struct net_device *dev,
4925 struct iw_request_info *info,
4926 union iwreq_data *wrqu, char *extra)
4927{
4928 struct orinoco_private *priv = netdev_priv(dev);
4929 hermes_t *hw = &priv->hw;
4930 struct iw_param *param = &wrqu->param;
4931 unsigned long flags;
4932 int ret = -EINPROGRESS;
4933
4934 if (orinoco_lock(priv, &flags) != 0)
4935 return -EBUSY;
4936
4937 switch (param->flags & IW_AUTH_INDEX) {
4938 case IW_AUTH_WPA_VERSION:
4939 case IW_AUTH_CIPHER_PAIRWISE:
4940 case IW_AUTH_CIPHER_GROUP:
4941 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
4942 case IW_AUTH_PRIVACY_INVOKED:
4943 case IW_AUTH_DROP_UNENCRYPTED:
4944 /*
4945 * orinoco does not use these parameters
4946 */
4947 break;
4948
4949 case IW_AUTH_KEY_MGMT:
4950 /* wl_lkm implies value 2 == PSK for Hermes I
4951 * which ties in with WEXT
4952 * no other hints tho :(
4953 */
4954 priv->key_mgmt = param->value;
4955 break;
4956
4957 case IW_AUTH_TKIP_COUNTERMEASURES:
4958 /* When countermeasures are enabled, shut down the
4959 * card; when disabled, re-enable the card. This must
4960 * take effect immediately.
4961 *
4962 * TODO: Make sure that the EAPOL message is getting
4963 * out before card disabled
4964 */
4965 if (param->value) {
4966 priv->tkip_cm_active = 1;
4967 ret = hermes_enable_port(hw, 0);
4968 } else {
4969 priv->tkip_cm_active = 0;
4970 ret = hermes_disable_port(hw, 0);
4971 }
4972 break;
4973
4974 case IW_AUTH_80211_AUTH_ALG:
4975 if (param->value & IW_AUTH_ALG_SHARED_KEY)
4976 priv->wep_restrict = 1;
4977 else if (param->value & IW_AUTH_ALG_OPEN_SYSTEM)
4978 priv->wep_restrict = 0;
4979 else
4980 ret = -EINVAL;
4981 break;
4982
4983 case IW_AUTH_WPA_ENABLED:
4984 if (priv->has_wpa) {
4985 priv->wpa_enabled = param->value ? 1 : 0;
4986 } else {
4987 if (param->value)
4988 ret = -EOPNOTSUPP;
4989 /* else silently accept disable of WPA */
4990 priv->wpa_enabled = 0;
4991 }
4992 break;
4993
4994 default:
4995 ret = -EOPNOTSUPP;
4996 }
4997
4998 orinoco_unlock(priv, &flags);
4999 return ret;
5000}
5001
5002static int orinoco_ioctl_get_auth(struct net_device *dev,
5003 struct iw_request_info *info,
5004 union iwreq_data *wrqu, char *extra)
5005{
5006 struct orinoco_private *priv = netdev_priv(dev);
5007 struct iw_param *param = &wrqu->param;
5008 unsigned long flags;
5009 int ret = 0;
5010
5011 if (orinoco_lock(priv, &flags) != 0)
5012 return -EBUSY;
5013
5014 switch (param->flags & IW_AUTH_INDEX) {
5015 case IW_AUTH_KEY_MGMT:
5016 param->value = priv->key_mgmt;
5017 break;
5018
5019 case IW_AUTH_TKIP_COUNTERMEASURES:
5020 param->value = priv->tkip_cm_active;
5021 break;
5022
5023 case IW_AUTH_80211_AUTH_ALG:
5024 if (priv->wep_restrict)
5025 param->value = IW_AUTH_ALG_SHARED_KEY;
5026 else
5027 param->value = IW_AUTH_ALG_OPEN_SYSTEM;
5028 break;
5029
5030 case IW_AUTH_WPA_ENABLED:
5031 param->value = priv->wpa_enabled;
5032 break;
5033
5034 default:
5035 ret = -EOPNOTSUPP;
5036 }
5037
5038 orinoco_unlock(priv, &flags);
5039 return ret;
5040}
5041
5042static int orinoco_ioctl_set_genie(struct net_device *dev,
5043 struct iw_request_info *info,
5044 union iwreq_data *wrqu, char *extra)
5045{
5046 struct orinoco_private *priv = netdev_priv(dev);
5047 u8 *buf;
5048 unsigned long flags;
5049 int err = 0;
5050
Johannes Berg2c7060022008-10-30 22:09:54 +01005051 /* cut off at IEEE80211_MAX_DATA_LEN */
5052 if ((wrqu->data.length > IEEE80211_MAX_DATA_LEN) ||
David Kilroyd03032a2008-08-21 23:28:02 +01005053 (wrqu->data.length && (extra == NULL)))
5054 return -EINVAL;
5055
5056 if (orinoco_lock(priv, &flags) != 0)
5057 return -EBUSY;
5058
5059 if (wrqu->data.length) {
5060 buf = kmalloc(wrqu->data.length, GFP_KERNEL);
5061 if (buf == NULL) {
5062 err = -ENOMEM;
5063 goto out;
5064 }
5065
5066 memcpy(buf, extra, wrqu->data.length);
5067 kfree(priv->wpa_ie);
5068 priv->wpa_ie = buf;
5069 priv->wpa_ie_len = wrqu->data.length;
5070 } else {
5071 kfree(priv->wpa_ie);
5072 priv->wpa_ie = NULL;
5073 priv->wpa_ie_len = 0;
5074 }
5075
5076 if (priv->wpa_ie) {
5077 /* Looks like wl_lkm wants to check the auth alg, and
5078 * somehow pass it to the firmware.
5079 * Instead it just calls the key mgmt rid
5080 * - we do this in set auth.
5081 */
5082 }
5083
5084out:
5085 orinoco_unlock(priv, &flags);
5086 return err;
5087}
5088
5089static int orinoco_ioctl_get_genie(struct net_device *dev,
5090 struct iw_request_info *info,
5091 union iwreq_data *wrqu, char *extra)
5092{
5093 struct orinoco_private *priv = netdev_priv(dev);
5094 unsigned long flags;
5095 int err = 0;
5096
5097 if (orinoco_lock(priv, &flags) != 0)
5098 return -EBUSY;
5099
5100 if ((priv->wpa_ie_len == 0) || (priv->wpa_ie == NULL)) {
5101 wrqu->data.length = 0;
5102 goto out;
5103 }
5104
5105 if (wrqu->data.length < priv->wpa_ie_len) {
5106 err = -E2BIG;
5107 goto out;
5108 }
5109
5110 wrqu->data.length = priv->wpa_ie_len;
5111 memcpy(extra, priv->wpa_ie, priv->wpa_ie_len);
5112
5113out:
5114 orinoco_unlock(priv, &flags);
5115 return err;
5116}
5117
5118static int orinoco_ioctl_set_mlme(struct net_device *dev,
5119 struct iw_request_info *info,
5120 union iwreq_data *wrqu, char *extra)
5121{
5122 struct orinoco_private *priv = netdev_priv(dev);
5123 hermes_t *hw = &priv->hw;
5124 struct iw_mlme *mlme = (struct iw_mlme *)extra;
5125 unsigned long flags;
5126 int ret = 0;
5127
5128 if (orinoco_lock(priv, &flags) != 0)
5129 return -EBUSY;
5130
5131 switch (mlme->cmd) {
5132 case IW_MLME_DEAUTH:
5133 /* silently ignore */
5134 break;
5135
5136 case IW_MLME_DISASSOC:
5137 {
5138 struct {
5139 u8 addr[ETH_ALEN];
5140 __le16 reason_code;
5141 } __attribute__ ((packed)) buf;
5142
5143 memcpy(buf.addr, mlme->addr.sa_data, ETH_ALEN);
5144 buf.reason_code = cpu_to_le16(mlme->reason_code);
5145 ret = HERMES_WRITE_RECORD(hw, USER_BAP,
5146 HERMES_RID_CNFDISASSOCIATE,
5147 &buf);
5148 break;
5149 }
5150 default:
5151 ret = -EOPNOTSUPP;
5152 }
5153
5154 orinoco_unlock(priv, &flags);
5155 return ret;
5156}
5157
Christoph Hellwig620554e2005-06-19 01:27:33 +02005158static int orinoco_ioctl_getretry(struct net_device *dev,
5159 struct iw_request_info *info,
5160 struct iw_param *rrq,
5161 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162{
5163 struct orinoco_private *priv = netdev_priv(dev);
5164 hermes_t *hw = &priv->hw;
5165 int err = 0;
5166 u16 short_limit, long_limit, lifetime;
5167 unsigned long flags;
5168
5169 if (orinoco_lock(priv, &flags) != 0)
5170 return -EBUSY;
5171
5172 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_SHORTRETRYLIMIT,
5173 &short_limit);
5174 if (err)
5175 goto out;
5176
5177 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_LONGRETRYLIMIT,
5178 &long_limit);
5179 if (err)
5180 goto out;
5181
5182 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_MAXTRANSMITLIFETIME,
5183 &lifetime);
5184 if (err)
5185 goto out;
5186
5187 rrq->disabled = 0; /* Can't be disabled */
5188
5189 /* Note : by default, display the retry number */
5190 if ((rrq->flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME) {
5191 rrq->flags = IW_RETRY_LIFETIME;
5192 rrq->value = lifetime * 1000; /* ??? */
5193 } else {
5194 /* By default, display the min number */
Jean Tourrilheseeec9f12006-08-29 18:02:31 -07005195 if ((rrq->flags & IW_RETRY_LONG)) {
5196 rrq->flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 rrq->value = long_limit;
5198 } else {
5199 rrq->flags = IW_RETRY_LIMIT;
5200 rrq->value = short_limit;
5201 if(short_limit != long_limit)
Jean Tourrilheseeec9f12006-08-29 18:02:31 -07005202 rrq->flags |= IW_RETRY_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 }
5204 }
5205
5206 out:
5207 orinoco_unlock(priv, &flags);
5208
5209 return err;
5210}
5211
Christoph Hellwig620554e2005-06-19 01:27:33 +02005212static int orinoco_ioctl_reset(struct net_device *dev,
5213 struct iw_request_info *info,
5214 void *wrqu,
5215 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216{
5217 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005218
5219 if (! capable(CAP_NET_ADMIN))
5220 return -EPERM;
5221
5222 if (info->cmd == (SIOCIWFIRSTPRIV + 0x1)) {
5223 printk(KERN_DEBUG "%s: Forcing reset!\n", dev->name);
5224
5225 /* Firmware reset */
David Howellsc4028952006-11-22 14:57:56 +00005226 orinoco_reset(&priv->reset_work);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005227 } else {
5228 printk(KERN_DEBUG "%s: Force scheduling reset!\n", dev->name);
5229
5230 schedule_work(&priv->reset_work);
5231 }
5232
5233 return 0;
5234}
5235
5236static int orinoco_ioctl_setibssport(struct net_device *dev,
5237 struct iw_request_info *info,
5238 void *wrqu,
5239 char *extra)
5240
5241{
5242 struct orinoco_private *priv = netdev_priv(dev);
5243 int val = *( (int *) extra );
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 unsigned long flags;
5245
5246 if (orinoco_lock(priv, &flags) != 0)
5247 return -EBUSY;
5248
5249 priv->ibss_port = val ;
5250
5251 /* Actually update the mode we are using */
5252 set_port_type(priv);
5253
5254 orinoco_unlock(priv, &flags);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005255 return -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256}
5257
Christoph Hellwig620554e2005-06-19 01:27:33 +02005258static int orinoco_ioctl_getibssport(struct net_device *dev,
5259 struct iw_request_info *info,
5260 void *wrqu,
5261 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262{
5263 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005264 int *val = (int *) extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265
5266 *val = priv->ibss_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 return 0;
5268}
5269
Christoph Hellwig620554e2005-06-19 01:27:33 +02005270static int orinoco_ioctl_setport3(struct net_device *dev,
5271 struct iw_request_info *info,
5272 void *wrqu,
5273 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274{
5275 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005276 int val = *( (int *) extra );
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 int err = 0;
5278 unsigned long flags;
5279
5280 if (orinoco_lock(priv, &flags) != 0)
5281 return -EBUSY;
5282
5283 switch (val) {
5284 case 0: /* Try to do IEEE ad-hoc mode */
5285 if (! priv->has_ibss) {
5286 err = -EINVAL;
5287 break;
5288 }
5289 priv->prefer_port3 = 0;
5290
5291 break;
5292
5293 case 1: /* Try to do Lucent proprietary ad-hoc mode */
5294 if (! priv->has_port3) {
5295 err = -EINVAL;
5296 break;
5297 }
5298 priv->prefer_port3 = 1;
5299 break;
5300
5301 default:
5302 err = -EINVAL;
5303 }
5304
Christoph Hellwig620554e2005-06-19 01:27:33 +02005305 if (! err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 /* Actually update the mode we are using */
5307 set_port_type(priv);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005308 err = -EINPROGRESS;
5309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310
5311 orinoco_unlock(priv, &flags);
5312
5313 return err;
5314}
5315
Christoph Hellwig620554e2005-06-19 01:27:33 +02005316static int orinoco_ioctl_getport3(struct net_device *dev,
5317 struct iw_request_info *info,
5318 void *wrqu,
5319 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320{
5321 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005322 int *val = (int *) extra;
5323
5324 *val = priv->prefer_port3;
5325 return 0;
5326}
5327
5328static int orinoco_ioctl_setpreamble(struct net_device *dev,
5329 struct iw_request_info *info,
5330 void *wrqu,
5331 char *extra)
5332{
5333 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 unsigned long flags;
Christoph Hellwig620554e2005-06-19 01:27:33 +02005335 int val;
5336
5337 if (! priv->has_preamble)
5338 return -EOPNOTSUPP;
5339
5340 /* 802.11b has recently defined some short preamble.
5341 * Basically, the Phy header has been reduced in size.
5342 * This increase performance, especially at high rates
5343 * (the preamble is transmitted at 1Mb/s), unfortunately
5344 * this give compatibility troubles... - Jean II */
5345 val = *( (int *) extra );
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346
5347 if (orinoco_lock(priv, &flags) != 0)
5348 return -EBUSY;
5349
Christoph Hellwig620554e2005-06-19 01:27:33 +02005350 if (val)
5351 priv->preamble = 1;
5352 else
5353 priv->preamble = 0;
5354
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 orinoco_unlock(priv, &flags);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005356
5357 return -EINPROGRESS; /* Call commit handler */
5358}
5359
5360static int orinoco_ioctl_getpreamble(struct net_device *dev,
5361 struct iw_request_info *info,
5362 void *wrqu,
5363 char *extra)
5364{
5365 struct orinoco_private *priv = netdev_priv(dev);
5366 int *val = (int *) extra;
5367
5368 if (! priv->has_preamble)
5369 return -EOPNOTSUPP;
5370
5371 *val = priv->preamble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 return 0;
5373}
5374
Christoph Hellwig620554e2005-06-19 01:27:33 +02005375/* ioctl interface to hermes_read_ltv()
5376 * To use with iwpriv, pass the RID as the token argument, e.g.
5377 * iwpriv get_rid [0xfc00]
5378 * At least Wireless Tools 25 is required to use iwpriv.
5379 * For Wireless Tools 25 and 26 append "dummy" are the end. */
5380static int orinoco_ioctl_getrid(struct net_device *dev,
5381 struct iw_request_info *info,
5382 struct iw_point *data,
5383 char *extra)
5384{
5385 struct orinoco_private *priv = netdev_priv(dev);
5386 hermes_t *hw = &priv->hw;
5387 int rid = data->flags;
5388 u16 length;
5389 int err;
5390 unsigned long flags;
5391
5392 /* It's a "get" function, but we don't want users to access the
5393 * WEP key and other raw firmware data */
5394 if (! capable(CAP_NET_ADMIN))
5395 return -EPERM;
5396
5397 if (rid < 0xfc00 || rid > 0xffff)
5398 return -EINVAL;
5399
5400 if (orinoco_lock(priv, &flags) != 0)
5401 return -EBUSY;
5402
5403 err = hermes_read_ltv(hw, USER_BAP, rid, MAX_RID_LEN, &length,
5404 extra);
5405 if (err)
5406 goto out;
5407
5408 data->length = min_t(u16, HERMES_RECLEN_TO_BYTES(length),
5409 MAX_RID_LEN);
5410
5411 out:
5412 orinoco_unlock(priv, &flags);
5413 return err;
5414}
5415
David Kilroy17a1a882008-08-21 23:27:47 +01005416/* Trigger a scan (look for other cells in the vicinity) */
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005417static int orinoco_ioctl_setscan(struct net_device *dev,
5418 struct iw_request_info *info,
David Kilroy9930cce2008-09-13 12:22:05 +01005419 struct iw_point *srq,
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005420 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421{
5422 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005423 hermes_t *hw = &priv->hw;
David Kilroy0753bba2008-08-21 23:27:46 +01005424 struct iw_scan_req *si = (struct iw_scan_req *) extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 unsigned long flags;
5427
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005428 /* Note : you may have realised that, as this is a SET operation,
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -08005429 * this is privileged and therefore a normal user can't
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005430 * perform scanning.
5431 * This is not an error, while the device perform scanning,
5432 * traffic doesn't flow, so it's a perfect DoS...
5433 * Jean II */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005435 if (orinoco_lock(priv, &flags) != 0)
5436 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005438 /* Scanning with port 0 disabled would fail */
5439 if (!netif_running(dev)) {
5440 err = -ENETDOWN;
5441 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005444 /* In monitor mode, the scan results are always empty.
5445 * Probe responses are passed to the driver as received
5446 * frames and could be processed in software. */
5447 if (priv->iw_mode == IW_MODE_MONITOR) {
5448 err = -EOPNOTSUPP;
5449 goto out;
5450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005452 /* Note : because we don't lock out the irq handler, the way
5453 * we access scan variables in priv is critical.
5454 * o scan_inprogress : not touched by irq handler
5455 * o scan_mode : not touched by irq handler
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005456 * Before modifying anything on those variables, please think hard !
5457 * Jean II */
5458
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005459 /* Save flags */
5460 priv->scan_mode = srq->flags;
5461
5462 /* Always trigger scanning, even if it's in progress.
5463 * This way, if the info frame get lost, we will recover somewhat
5464 * gracefully - Jean II */
5465
5466 if (priv->has_hostscan) {
5467 switch (priv->firmware_type) {
5468 case FIRMWARE_TYPE_SYMBOL:
5469 err = hermes_write_wordrec(hw, USER_BAP,
5470 HERMES_RID_CNFHOSTSCAN_SYMBOL,
5471 HERMES_HOSTSCAN_SYMBOL_ONCE |
5472 HERMES_HOSTSCAN_SYMBOL_BCAST);
5473 break;
5474 case FIRMWARE_TYPE_INTERSIL: {
Pavel Roskind133ae42005-09-23 04:18:06 -04005475 __le16 req[3];
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005476
5477 req[0] = cpu_to_le16(0x3fff); /* All channels */
5478 req[1] = cpu_to_le16(0x0001); /* rate 1 Mbps */
5479 req[2] = 0; /* Any ESSID */
5480 err = HERMES_WRITE_RECORD(hw, USER_BAP,
5481 HERMES_RID_CNFHOSTSCAN, &req);
5482 }
5483 break;
5484 case FIRMWARE_TYPE_AGERE:
David Kilroy0753bba2008-08-21 23:27:46 +01005485 if (priv->scan_mode & IW_SCAN_THIS_ESSID) {
5486 struct hermes_idstring idbuf;
5487 size_t len = min(sizeof(idbuf.val),
5488 (size_t) si->essid_len);
5489 idbuf.len = cpu_to_le16(len);
5490 memcpy(idbuf.val, si->essid, len);
5491
5492 err = hermes_write_ltv(hw, USER_BAP,
5493 HERMES_RID_CNFSCANSSID_AGERE,
5494 HERMES_BYTES_TO_RECLEN(len + 2),
5495 &idbuf);
5496 } else
5497 err = hermes_write_wordrec(hw, USER_BAP,
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005498 HERMES_RID_CNFSCANSSID_AGERE,
5499 0); /* Any ESSID */
5500 if (err)
5501 break;
5502
David Kilroy01632fa2008-08-21 23:27:58 +01005503 if (priv->has_ext_scan) {
5504 /* Clear scan results at the start of
5505 * an extended scan */
5506 orinoco_clear_scan_results(priv,
5507 msecs_to_jiffies(15000));
5508
5509 /* TODO: Is this available on older firmware?
5510 * Can we use it to scan specific channels
5511 * for IW_SCAN_THIS_FREQ? */
5512 err = hermes_write_wordrec(hw, USER_BAP,
5513 HERMES_RID_CNFSCANCHANNELS2GHZ,
5514 0x7FFF);
5515 if (err)
5516 goto out;
5517
5518 err = hermes_inquire(hw,
5519 HERMES_INQ_CHANNELINFO);
5520 } else
5521 err = hermes_inquire(hw, HERMES_INQ_SCAN);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005522 break;
5523 }
5524 } else
5525 err = hermes_inquire(hw, HERMES_INQ_SCAN);
5526
5527 /* One more client */
5528 if (! err)
5529 priv->scan_inprogress = 1;
5530
5531 out:
5532 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 return err;
5534}
5535
Dan Williams1e3428e2007-10-10 23:56:25 -04005536#define MAX_CUSTOM_LEN 64
5537
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005538/* Translate scan data returned from the card to a card independant
David Kilroy17a1a882008-08-21 23:27:47 +01005539 * format that the Wireless Tools will understand - Jean II */
Dan Williams1e3428e2007-10-10 23:56:25 -04005540static inline char *orinoco_translate_scan(struct net_device *dev,
David S. Millerccc58052008-06-16 18:50:49 -07005541 struct iw_request_info *info,
Dan Williams1e3428e2007-10-10 23:56:25 -04005542 char *current_ev,
5543 char *end_buf,
5544 union hermes_scan_info *bss,
Pavel Roskindfe1baf2008-11-10 09:25:53 -05005545 unsigned long last_scanned)
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005546{
5547 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005548 u16 capabilities;
5549 u16 channel;
5550 struct iw_event iwe; /* Temporary buffer */
Dan Williams1e3428e2007-10-10 23:56:25 -04005551 char custom[MAX_CUSTOM_LEN];
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005552
David Kilroy17a1a882008-08-21 23:27:47 +01005553 memset(&iwe, 0, sizeof(iwe));
5554
Dan Williams1e3428e2007-10-10 23:56:25 -04005555 /* First entry *MUST* be the AP MAC address */
5556 iwe.cmd = SIOCGIWAP;
5557 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
5558 memcpy(iwe.u.ap_addr.sa_data, bss->a.bssid, ETH_ALEN);
David S. Millerccc58052008-06-16 18:50:49 -07005559 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5560 &iwe, IW_EV_ADDR_LEN);
Dan Williams1e3428e2007-10-10 23:56:25 -04005561
5562 /* Other entries will be displayed in the order we give them */
5563
5564 /* Add the ESSID */
5565 iwe.u.data.length = le16_to_cpu(bss->a.essid_len);
5566 if (iwe.u.data.length > 32)
5567 iwe.u.data.length = 32;
5568 iwe.cmd = SIOCGIWESSID;
5569 iwe.u.data.flags = 1;
David S. Millerccc58052008-06-16 18:50:49 -07005570 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5571 &iwe, bss->a.essid);
Dan Williams1e3428e2007-10-10 23:56:25 -04005572
5573 /* Add mode */
5574 iwe.cmd = SIOCGIWMODE;
5575 capabilities = le16_to_cpu(bss->a.capabilities);
David Kilroy17a1a882008-08-21 23:27:47 +01005576 if (capabilities & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
5577 if (capabilities & WLAN_CAPABILITY_ESS)
Dan Williams1e3428e2007-10-10 23:56:25 -04005578 iwe.u.mode = IW_MODE_MASTER;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005579 else
Dan Williams1e3428e2007-10-10 23:56:25 -04005580 iwe.u.mode = IW_MODE_ADHOC;
David S. Millerccc58052008-06-16 18:50:49 -07005581 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5582 &iwe, IW_EV_UINT_LEN);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005583 }
5584
Dan Williams1e3428e2007-10-10 23:56:25 -04005585 channel = bss->s.channel;
5586 if ((channel >= 1) && (channel <= NUM_CHANNELS)) {
David Kilroy17a1a882008-08-21 23:27:47 +01005587 /* Add channel and frequency */
Dan Williams1e3428e2007-10-10 23:56:25 -04005588 iwe.cmd = SIOCGIWFREQ;
David Kilroy17a1a882008-08-21 23:27:47 +01005589 iwe.u.freq.m = channel;
5590 iwe.u.freq.e = 0;
5591 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5592 &iwe, IW_EV_FREQ_LEN);
5593
Dan Williams1e3428e2007-10-10 23:56:25 -04005594 iwe.u.freq.m = channel_frequency[channel-1] * 100000;
5595 iwe.u.freq.e = 1;
David S. Millerccc58052008-06-16 18:50:49 -07005596 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
Dan Williams1e3428e2007-10-10 23:56:25 -04005597 &iwe, IW_EV_FREQ_LEN);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005598 }
5599
David Kilroy17a1a882008-08-21 23:27:47 +01005600 /* Add quality statistics. level and noise in dB. No link quality */
Dan Williams1e3428e2007-10-10 23:56:25 -04005601 iwe.cmd = IWEVQUAL;
David Kilroy17a1a882008-08-21 23:27:47 +01005602 iwe.u.qual.updated = IW_QUAL_DBM | IW_QUAL_QUAL_INVALID;
Dan Williams1e3428e2007-10-10 23:56:25 -04005603 iwe.u.qual.level = (__u8) le16_to_cpu(bss->a.level) - 0x95;
5604 iwe.u.qual.noise = (__u8) le16_to_cpu(bss->a.noise) - 0x95;
5605 /* Wireless tools prior to 27.pre22 will show link quality
5606 * anyway, so we provide a reasonable value. */
5607 if (iwe.u.qual.level > iwe.u.qual.noise)
5608 iwe.u.qual.qual = iwe.u.qual.level - iwe.u.qual.noise;
5609 else
5610 iwe.u.qual.qual = 0;
David S. Millerccc58052008-06-16 18:50:49 -07005611 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5612 &iwe, IW_EV_QUAL_LEN);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005613
Dan Williams1e3428e2007-10-10 23:56:25 -04005614 /* Add encryption capability */
5615 iwe.cmd = SIOCGIWENCODE;
David Kilroy17a1a882008-08-21 23:27:47 +01005616 if (capabilities & WLAN_CAPABILITY_PRIVACY)
Dan Williams1e3428e2007-10-10 23:56:25 -04005617 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
5618 else
5619 iwe.u.data.flags = IW_ENCODE_DISABLED;
5620 iwe.u.data.length = 0;
David S. Millerccc58052008-06-16 18:50:49 -07005621 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
David Kilroy17a1a882008-08-21 23:27:47 +01005622 &iwe, NULL);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005623
Dan Williams1e3428e2007-10-10 23:56:25 -04005624 /* Bit rate is not available in Lucent/Agere firmwares */
5625 if (priv->firmware_type != FIRMWARE_TYPE_AGERE) {
David S. Millerccc58052008-06-16 18:50:49 -07005626 char *current_val = current_ev + iwe_stream_lcp_len(info);
Dan Williams1e3428e2007-10-10 23:56:25 -04005627 int i;
5628 int step;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005629
Dan Williams1e3428e2007-10-10 23:56:25 -04005630 if (priv->firmware_type == FIRMWARE_TYPE_SYMBOL)
5631 step = 2;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005632 else
Dan Williams1e3428e2007-10-10 23:56:25 -04005633 step = 1;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005634
Dan Williams1e3428e2007-10-10 23:56:25 -04005635 iwe.cmd = SIOCGIWRATE;
5636 /* Those two flags are ignored... */
5637 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
5638 /* Max 10 values */
5639 for (i = 0; i < 10; i += step) {
5640 /* NULL terminated */
5641 if (bss->p.rates[i] == 0x0)
5642 break;
5643 /* Bit rate given in 500 kb/s units (+ 0x80) */
David Kilroy17a1a882008-08-21 23:27:47 +01005644 iwe.u.bitrate.value =
5645 ((bss->p.rates[i] & 0x7f) * 500000);
David S. Millerccc58052008-06-16 18:50:49 -07005646 current_val = iwe_stream_add_value(info, current_ev,
5647 current_val,
Dan Williams1e3428e2007-10-10 23:56:25 -04005648 end_buf, &iwe,
5649 IW_EV_PARAM_LEN);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005650 }
Dan Williams1e3428e2007-10-10 23:56:25 -04005651 /* Check if we added any event */
David S. Millerccc58052008-06-16 18:50:49 -07005652 if ((current_val - current_ev) > iwe_stream_lcp_len(info))
Dan Williams1e3428e2007-10-10 23:56:25 -04005653 current_ev = current_val;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005654 }
Dan Williams1e3428e2007-10-10 23:56:25 -04005655
David Kilroy17a1a882008-08-21 23:27:47 +01005656 /* Beacon interval */
5657 iwe.cmd = IWEVCUSTOM;
5658 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5659 "bcn_int=%d",
5660 le16_to_cpu(bss->a.beacon_interv));
5661 if (iwe.u.data.length)
5662 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5663 &iwe, custom);
5664
5665 /* Capabilites */
5666 iwe.cmd = IWEVCUSTOM;
5667 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5668 "capab=0x%04x",
5669 capabilities);
5670 if (iwe.u.data.length)
5671 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5672 &iwe, custom);
5673
5674 /* Add EXTRA: Age to display seconds since last beacon/probe response
5675 * for given network. */
5676 iwe.cmd = IWEVCUSTOM;
5677 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5678 " Last beacon: %dms ago",
5679 jiffies_to_msecs(jiffies - last_scanned));
5680 if (iwe.u.data.length)
5681 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5682 &iwe, custom);
5683
Dan Williams1e3428e2007-10-10 23:56:25 -04005684 return current_ev;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005685}
5686
David Kilroy01632fa2008-08-21 23:27:58 +01005687static inline char *orinoco_translate_ext_scan(struct net_device *dev,
5688 struct iw_request_info *info,
5689 char *current_ev,
5690 char *end_buf,
5691 struct agere_ext_scan_info *bss,
Pavel Roskindfe1baf2008-11-10 09:25:53 -05005692 unsigned long last_scanned)
David Kilroy01632fa2008-08-21 23:27:58 +01005693{
5694 u16 capabilities;
5695 u16 channel;
5696 struct iw_event iwe; /* Temporary buffer */
5697 char custom[MAX_CUSTOM_LEN];
5698 u8 *ie;
5699
5700 memset(&iwe, 0, sizeof(iwe));
5701
5702 /* First entry *MUST* be the AP MAC address */
5703 iwe.cmd = SIOCGIWAP;
5704 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
5705 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
5706 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5707 &iwe, IW_EV_ADDR_LEN);
5708
5709 /* Other entries will be displayed in the order we give them */
5710
5711 /* Add the ESSID */
5712 ie = bss->data;
5713 iwe.u.data.length = ie[1];
5714 if (iwe.u.data.length) {
5715 if (iwe.u.data.length > 32)
5716 iwe.u.data.length = 32;
5717 iwe.cmd = SIOCGIWESSID;
5718 iwe.u.data.flags = 1;
5719 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5720 &iwe, &ie[2]);
5721 }
5722
5723 /* Add mode */
5724 capabilities = le16_to_cpu(bss->capabilities);
5725 if (capabilities & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
5726 iwe.cmd = SIOCGIWMODE;
5727 if (capabilities & WLAN_CAPABILITY_ESS)
5728 iwe.u.mode = IW_MODE_MASTER;
5729 else
5730 iwe.u.mode = IW_MODE_ADHOC;
5731 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5732 &iwe, IW_EV_UINT_LEN);
5733 }
5734
Johannes Berg2c7060022008-10-30 22:09:54 +01005735 ie = orinoco_get_ie(bss->data, sizeof(bss->data), WLAN_EID_DS_PARAMS);
David Kilroy01632fa2008-08-21 23:27:58 +01005736 channel = ie ? ie[2] : 0;
5737 if ((channel >= 1) && (channel <= NUM_CHANNELS)) {
5738 /* Add channel and frequency */
5739 iwe.cmd = SIOCGIWFREQ;
5740 iwe.u.freq.m = channel;
5741 iwe.u.freq.e = 0;
5742 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5743 &iwe, IW_EV_FREQ_LEN);
5744
5745 iwe.u.freq.m = channel_frequency[channel-1] * 100000;
5746 iwe.u.freq.e = 1;
5747 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5748 &iwe, IW_EV_FREQ_LEN);
5749 }
5750
5751 /* Add quality statistics. level and noise in dB. No link quality */
5752 iwe.cmd = IWEVQUAL;
5753 iwe.u.qual.updated = IW_QUAL_DBM | IW_QUAL_QUAL_INVALID;
5754 iwe.u.qual.level = bss->level - 0x95;
5755 iwe.u.qual.noise = bss->noise - 0x95;
5756 /* Wireless tools prior to 27.pre22 will show link quality
5757 * anyway, so we provide a reasonable value. */
5758 if (iwe.u.qual.level > iwe.u.qual.noise)
5759 iwe.u.qual.qual = iwe.u.qual.level - iwe.u.qual.noise;
5760 else
5761 iwe.u.qual.qual = 0;
5762 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5763 &iwe, IW_EV_QUAL_LEN);
5764
5765 /* Add encryption capability */
5766 iwe.cmd = SIOCGIWENCODE;
5767 if (capabilities & WLAN_CAPABILITY_PRIVACY)
5768 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
5769 else
5770 iwe.u.data.flags = IW_ENCODE_DISABLED;
5771 iwe.u.data.length = 0;
5772 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5773 &iwe, NULL);
5774
5775 /* WPA IE */
5776 ie = orinoco_get_wpa_ie(bss->data, sizeof(bss->data));
5777 if (ie) {
5778 iwe.cmd = IWEVGENIE;
5779 iwe.u.data.length = ie[1] + 2;
5780 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5781 &iwe, ie);
5782 }
5783
5784 /* RSN IE */
Johannes Berg2c7060022008-10-30 22:09:54 +01005785 ie = orinoco_get_ie(bss->data, sizeof(bss->data), WLAN_EID_RSN);
David Kilroy01632fa2008-08-21 23:27:58 +01005786 if (ie) {
5787 iwe.cmd = IWEVGENIE;
5788 iwe.u.data.length = ie[1] + 2;
5789 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5790 &iwe, ie);
5791 }
5792
Johannes Berg2c7060022008-10-30 22:09:54 +01005793 ie = orinoco_get_ie(bss->data, sizeof(bss->data), WLAN_EID_SUPP_RATES);
David Kilroy01632fa2008-08-21 23:27:58 +01005794 if (ie) {
5795 char *p = current_ev + iwe_stream_lcp_len(info);
5796 int i;
5797
5798 iwe.cmd = SIOCGIWRATE;
5799 /* Those two flags are ignored... */
5800 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
5801
5802 for (i = 2; i < (ie[1] + 2); i++) {
5803 iwe.u.bitrate.value = ((ie[i] & 0x7F) * 500000);
5804 p = iwe_stream_add_value(info, current_ev, p, end_buf,
5805 &iwe, IW_EV_PARAM_LEN);
5806 }
5807 /* Check if we added any event */
5808 if (p > (current_ev + iwe_stream_lcp_len(info)))
5809 current_ev = p;
5810 }
5811
5812 /* Timestamp */
5813 iwe.cmd = IWEVCUSTOM;
David Kilroy75d31cf2008-09-12 22:28:18 +01005814 iwe.u.data.length =
5815 snprintf(custom, MAX_CUSTOM_LEN, "tsf=%016llx",
5816 (unsigned long long) le64_to_cpu(bss->timestamp));
David Kilroy01632fa2008-08-21 23:27:58 +01005817 if (iwe.u.data.length)
5818 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5819 &iwe, custom);
5820
5821 /* Beacon interval */
5822 iwe.cmd = IWEVCUSTOM;
5823 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5824 "bcn_int=%d",
5825 le16_to_cpu(bss->beacon_interval));
5826 if (iwe.u.data.length)
5827 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5828 &iwe, custom);
5829
5830 /* Capabilites */
5831 iwe.cmd = IWEVCUSTOM;
5832 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5833 "capab=0x%04x",
5834 capabilities);
5835 if (iwe.u.data.length)
5836 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5837 &iwe, custom);
5838
5839 /* Add EXTRA: Age to display seconds since last beacon/probe response
5840 * for given network. */
5841 iwe.cmd = IWEVCUSTOM;
5842 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5843 " Last beacon: %dms ago",
5844 jiffies_to_msecs(jiffies - last_scanned));
5845 if (iwe.u.data.length)
5846 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5847 &iwe, custom);
5848
5849 return current_ev;
5850}
5851
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005852/* Return results of a scan */
5853static int orinoco_ioctl_getscan(struct net_device *dev,
5854 struct iw_request_info *info,
5855 struct iw_point *srq,
5856 char *extra)
5857{
5858 struct orinoco_private *priv = netdev_priv(dev);
5859 int err = 0;
5860 unsigned long flags;
Dan Williams1e3428e2007-10-10 23:56:25 -04005861 char *current_ev = extra;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005862
5863 if (orinoco_lock(priv, &flags) != 0)
5864 return -EBUSY;
5865
Dan Williams1e3428e2007-10-10 23:56:25 -04005866 if (priv->scan_inprogress) {
5867 /* Important note : we don't want to block the caller
5868 * until results are ready for various reasons.
5869 * First, managing wait queues is complex and racy.
5870 * Second, we grab some rtnetlink lock before comming
5871 * here (in dev_ioctl()).
5872 * Third, we generate an Wireless Event, so the
5873 * caller can wait itself on that - Jean II */
5874 err = -EAGAIN;
5875 goto out;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005876 }
Dan Williams1e3428e2007-10-10 23:56:25 -04005877
David Kilroy01632fa2008-08-21 23:27:58 +01005878 if (priv->has_ext_scan) {
5879 struct xbss_element *bss;
Dan Williams1e3428e2007-10-10 23:56:25 -04005880
David Kilroy01632fa2008-08-21 23:27:58 +01005881 list_for_each_entry(bss, &priv->bss_list, list) {
5882 /* Translate this entry to WE format */
5883 current_ev =
5884 orinoco_translate_ext_scan(dev, info,
5885 current_ev,
5886 extra + srq->length,
5887 &bss->bss,
5888 bss->last_scanned);
5889
5890 /* Check if there is space for one more entry */
5891 if ((extra + srq->length - current_ev)
5892 <= IW_EV_ADDR_LEN) {
5893 /* Ask user space to try again with a
5894 * bigger buffer */
5895 err = -E2BIG;
5896 goto out;
5897 }
5898 }
5899
5900 } else {
5901 struct bss_element *bss;
5902
5903 list_for_each_entry(bss, &priv->bss_list, list) {
5904 /* Translate this entry to WE format */
5905 current_ev = orinoco_translate_scan(dev, info,
5906 current_ev,
5907 extra + srq->length,
5908 &bss->bss,
5909 bss->last_scanned);
5910
5911 /* Check if there is space for one more entry */
5912 if ((extra + srq->length - current_ev)
5913 <= IW_EV_ADDR_LEN) {
5914 /* Ask user space to try again with a
5915 * bigger buffer */
5916 err = -E2BIG;
5917 goto out;
5918 }
Dan Williams1e3428e2007-10-10 23:56:25 -04005919 }
5920 }
5921
5922 srq->length = (current_ev - extra);
5923 srq->flags = (__u16) priv->scan_mode;
5924
5925out:
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005926 orinoco_unlock(priv, &flags);
5927 return err;
5928}
5929
Christoph Hellwig620554e2005-06-19 01:27:33 +02005930/* Commit handler, called after set operations */
5931static int orinoco_ioctl_commit(struct net_device *dev,
5932 struct iw_request_info *info,
5933 void *wrqu,
5934 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935{
5936 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005937 struct hermes *hw = &priv->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938 unsigned long flags;
Christoph Hellwig620554e2005-06-19 01:27:33 +02005939 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940
Christoph Hellwig620554e2005-06-19 01:27:33 +02005941 if (!priv->open)
5942 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943
Christoph Hellwig620554e2005-06-19 01:27:33 +02005944 if (priv->broken_disableport) {
David Howellsc4028952006-11-22 14:57:56 +00005945 orinoco_reset(&priv->reset_work);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005946 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948
Christoph Hellwig620554e2005-06-19 01:27:33 +02005949 if (orinoco_lock(priv, &flags) != 0)
5950 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951
Christoph Hellwig620554e2005-06-19 01:27:33 +02005952 err = hermes_disable_port(hw, 0);
5953 if (err) {
5954 printk(KERN_WARNING "%s: Unable to disable port "
5955 "while reconfiguring card\n", dev->name);
5956 priv->broken_disableport = 1;
5957 goto out;
5958 }
5959
5960 err = __orinoco_program_rids(dev);
5961 if (err) {
5962 printk(KERN_WARNING "%s: Unable to reconfigure card\n",
5963 dev->name);
5964 goto out;
5965 }
5966
5967 err = hermes_enable_port(hw, 0);
5968 if (err) {
5969 printk(KERN_WARNING "%s: Unable to enable port while reconfiguring card\n",
5970 dev->name);
5971 goto out;
5972 }
5973
5974 out:
5975 if (err) {
5976 printk(KERN_WARNING "%s: Resetting instead...\n", dev->name);
5977 schedule_work(&priv->reset_work);
5978 err = 0;
5979 }
5980
5981 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 return err;
5983}
5984
Christoph Hellwig620554e2005-06-19 01:27:33 +02005985static const struct iw_priv_args orinoco_privtab[] = {
5986 { SIOCIWFIRSTPRIV + 0x0, 0, 0, "force_reset" },
5987 { SIOCIWFIRSTPRIV + 0x1, 0, 0, "card_reset" },
5988 { SIOCIWFIRSTPRIV + 0x2, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5989 0, "set_port3" },
5990 { SIOCIWFIRSTPRIV + 0x3, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5991 "get_port3" },
5992 { SIOCIWFIRSTPRIV + 0x4, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5993 0, "set_preamble" },
5994 { SIOCIWFIRSTPRIV + 0x5, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5995 "get_preamble" },
5996 { SIOCIWFIRSTPRIV + 0x6, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5997 0, "set_ibssport" },
5998 { SIOCIWFIRSTPRIV + 0x7, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5999 "get_ibssport" },
6000 { SIOCIWFIRSTPRIV + 0x9, 0, IW_PRIV_TYPE_BYTE | MAX_RID_LEN,
6001 "get_rid" },
6002};
6003
6004
6005/*
6006 * Structures to export the Wireless Handlers
6007 */
6008
David Kilroy409644a2008-08-21 23:28:01 +01006009#define STD_IW_HANDLER(id, func) \
6010 [IW_IOCTL_IDX(id)] = (iw_handler) func
Christoph Hellwig620554e2005-06-19 01:27:33 +02006011static const iw_handler orinoco_handler[] = {
David Kilroy409644a2008-08-21 23:28:01 +01006012 STD_IW_HANDLER(SIOCSIWCOMMIT, orinoco_ioctl_commit),
6013 STD_IW_HANDLER(SIOCGIWNAME, orinoco_ioctl_getname),
6014 STD_IW_HANDLER(SIOCSIWFREQ, orinoco_ioctl_setfreq),
6015 STD_IW_HANDLER(SIOCGIWFREQ, orinoco_ioctl_getfreq),
6016 STD_IW_HANDLER(SIOCSIWMODE, orinoco_ioctl_setmode),
6017 STD_IW_HANDLER(SIOCGIWMODE, orinoco_ioctl_getmode),
6018 STD_IW_HANDLER(SIOCSIWSENS, orinoco_ioctl_setsens),
6019 STD_IW_HANDLER(SIOCGIWSENS, orinoco_ioctl_getsens),
6020 STD_IW_HANDLER(SIOCGIWRANGE, orinoco_ioctl_getiwrange),
6021 STD_IW_HANDLER(SIOCSIWSPY, iw_handler_set_spy),
6022 STD_IW_HANDLER(SIOCGIWSPY, iw_handler_get_spy),
6023 STD_IW_HANDLER(SIOCSIWTHRSPY, iw_handler_set_thrspy),
6024 STD_IW_HANDLER(SIOCGIWTHRSPY, iw_handler_get_thrspy),
6025 STD_IW_HANDLER(SIOCSIWAP, orinoco_ioctl_setwap),
6026 STD_IW_HANDLER(SIOCGIWAP, orinoco_ioctl_getwap),
6027 STD_IW_HANDLER(SIOCSIWSCAN, orinoco_ioctl_setscan),
6028 STD_IW_HANDLER(SIOCGIWSCAN, orinoco_ioctl_getscan),
6029 STD_IW_HANDLER(SIOCSIWESSID, orinoco_ioctl_setessid),
6030 STD_IW_HANDLER(SIOCGIWESSID, orinoco_ioctl_getessid),
6031 STD_IW_HANDLER(SIOCSIWNICKN, orinoco_ioctl_setnick),
6032 STD_IW_HANDLER(SIOCGIWNICKN, orinoco_ioctl_getnick),
6033 STD_IW_HANDLER(SIOCSIWRATE, orinoco_ioctl_setrate),
6034 STD_IW_HANDLER(SIOCGIWRATE, orinoco_ioctl_getrate),
6035 STD_IW_HANDLER(SIOCSIWRTS, orinoco_ioctl_setrts),
6036 STD_IW_HANDLER(SIOCGIWRTS, orinoco_ioctl_getrts),
6037 STD_IW_HANDLER(SIOCSIWFRAG, orinoco_ioctl_setfrag),
6038 STD_IW_HANDLER(SIOCGIWFRAG, orinoco_ioctl_getfrag),
6039 STD_IW_HANDLER(SIOCGIWRETRY, orinoco_ioctl_getretry),
6040 STD_IW_HANDLER(SIOCSIWENCODE, orinoco_ioctl_setiwencode),
6041 STD_IW_HANDLER(SIOCGIWENCODE, orinoco_ioctl_getiwencode),
6042 STD_IW_HANDLER(SIOCSIWPOWER, orinoco_ioctl_setpower),
6043 STD_IW_HANDLER(SIOCGIWPOWER, orinoco_ioctl_getpower),
David Kilroyd03032a2008-08-21 23:28:02 +01006044 STD_IW_HANDLER(SIOCSIWGENIE, orinoco_ioctl_set_genie),
6045 STD_IW_HANDLER(SIOCGIWGENIE, orinoco_ioctl_get_genie),
6046 STD_IW_HANDLER(SIOCSIWMLME, orinoco_ioctl_set_mlme),
6047 STD_IW_HANDLER(SIOCSIWAUTH, orinoco_ioctl_set_auth),
6048 STD_IW_HANDLER(SIOCGIWAUTH, orinoco_ioctl_get_auth),
6049 STD_IW_HANDLER(SIOCSIWENCODEEXT, orinoco_ioctl_set_encodeext),
6050 STD_IW_HANDLER(SIOCGIWENCODEEXT, orinoco_ioctl_get_encodeext),
Christoph Hellwig620554e2005-06-19 01:27:33 +02006051};
6052
6053
6054/*
6055 Added typecasting since we no longer use iwreq_data -- Moustafa
6056 */
6057static const iw_handler orinoco_private_handler[] = {
Peter Hagervall6b9b97c2005-07-27 01:14:46 -07006058 [0] = (iw_handler) orinoco_ioctl_reset,
6059 [1] = (iw_handler) orinoco_ioctl_reset,
6060 [2] = (iw_handler) orinoco_ioctl_setport3,
6061 [3] = (iw_handler) orinoco_ioctl_getport3,
6062 [4] = (iw_handler) orinoco_ioctl_setpreamble,
6063 [5] = (iw_handler) orinoco_ioctl_getpreamble,
6064 [6] = (iw_handler) orinoco_ioctl_setibssport,
6065 [7] = (iw_handler) orinoco_ioctl_getibssport,
6066 [9] = (iw_handler) orinoco_ioctl_getrid,
Christoph Hellwig620554e2005-06-19 01:27:33 +02006067};
6068
6069static const struct iw_handler_def orinoco_handler_def = {
6070 .num_standard = ARRAY_SIZE(orinoco_handler),
6071 .num_private = ARRAY_SIZE(orinoco_private_handler),
6072 .num_private_args = ARRAY_SIZE(orinoco_privtab),
6073 .standard = orinoco_handler,
6074 .private = orinoco_private_handler,
6075 .private_args = orinoco_privtab,
Pavel Roskin343c6862005-09-09 18:43:02 -04006076 .get_wireless_stats = orinoco_get_wireless_stats,
Christoph Hellwig620554e2005-06-19 01:27:33 +02006077};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02006079static void orinoco_get_drvinfo(struct net_device *dev,
6080 struct ethtool_drvinfo *info)
6081{
6082 struct orinoco_private *priv = netdev_priv(dev);
6083
6084 strncpy(info->driver, DRIVER_NAME, sizeof(info->driver) - 1);
6085 strncpy(info->version, DRIVER_VERSION, sizeof(info->version) - 1);
6086 strncpy(info->fw_version, priv->fw_name, sizeof(info->fw_version) - 1);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07006087 if (dev->dev.parent)
Kay Sieversfb28ad32008-11-10 13:55:14 -08006088 strncpy(info->bus_info, dev_name(dev->dev.parent),
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02006089 sizeof(info->bus_info) - 1);
6090 else
6091 snprintf(info->bus_info, sizeof(info->bus_info) - 1,
6092 "PCMCIA %p", priv->hw.iobase);
6093}
6094
Jeff Garzik7282d492006-09-13 14:30:00 -04006095static const struct ethtool_ops orinoco_ethtool_ops = {
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02006096 .get_drvinfo = orinoco_get_drvinfo,
6097 .get_link = ethtool_op_get_link,
6098};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099
6100/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101/* Module initialization */
6102/********************************************************************/
6103
6104EXPORT_SYMBOL(alloc_orinocodev);
6105EXPORT_SYMBOL(free_orinocodev);
6106
6107EXPORT_SYMBOL(__orinoco_up);
6108EXPORT_SYMBOL(__orinoco_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109EXPORT_SYMBOL(orinoco_reinit_firmware);
6110
6111EXPORT_SYMBOL(orinoco_interrupt);
6112
6113/* Can't be declared "const" or the whole __initdata section will
6114 * become const */
6115static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
6116 " (David Gibson <hermes@gibson.dropbear.id.au>, "
6117 "Pavel Roskin <proski@gnu.org>, et al)";
6118
6119static int __init init_orinoco(void)
6120{
6121 printk(KERN_DEBUG "%s\n", version);
6122 return 0;
6123}
6124
6125static void __exit exit_orinoco(void)
6126{
6127}
6128
6129module_init(init_orinoco);
6130module_exit(exit_orinoco);