blob: 09eec7f620bcf64f098c348be56e0da8cd0b7185 [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>
Pavel Roskin9c974fb2006-08-15 20:45:03 -040087#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#include <linux/wireless.h>
Johannes Berg2c7060022008-10-30 22:09:54 +010089#include <linux/ieee80211.h>
Christoph Hellwig620554e2005-06-19 01:27:33 +020090#include <net/iw_handler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
David Kilroy23edcc42008-08-21 23:28:05 +010092#include <linux/scatterlist.h>
93#include <linux/crypto.h>
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#include "hermes_rid.h"
David Kilroy3994d502008-08-21 23:27:54 +010096#include "hermes_dld.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#include "orinoco.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99/********************************************************************/
100/* Module information */
101/********************************************************************/
102
103MODULE_AUTHOR("Pavel Roskin <proski@gnu.org> & David Gibson <hermes@gibson.dropbear.id.au>");
104MODULE_DESCRIPTION("Driver for Lucent Orinoco, Prism II based and similar wireless cards");
105MODULE_LICENSE("Dual MPL/GPL");
106
107/* Level of debugging. Used in the macros in orinoco.h */
108#ifdef ORINOCO_DEBUG
109int orinoco_debug = ORINOCO_DEBUG;
110module_param(orinoco_debug, int, 0644);
111MODULE_PARM_DESC(orinoco_debug, "Debug level");
112EXPORT_SYMBOL(orinoco_debug);
113#endif
114
115static int suppress_linkstatus; /* = 0 */
116module_param(suppress_linkstatus, bool, 0644);
117MODULE_PARM_DESC(suppress_linkstatus, "Don't log link status changes");
David Gibson7bb7c3a2005-05-12 20:02:10 -0400118static int ignore_disconnect; /* = 0 */
119module_param(ignore_disconnect, int, 0644);
120MODULE_PARM_DESC(ignore_disconnect, "Don't report lost link to the network layer");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200122static int force_monitor; /* = 0 */
123module_param(force_monitor, int, 0644);
124MODULE_PARM_DESC(force_monitor, "Allow monitor mode for all firmware versions");
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126/********************************************************************/
127/* Compile time configuration and compatibility stuff */
128/********************************************************************/
129
130/* We do this this way to avoid ifdefs in the actual code */
131#ifdef WIRELESS_SPY
Pavel Roskin343c6862005-09-09 18:43:02 -0400132#define SPY_NUMBER(priv) (priv->spy_data.spy_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#else
134#define SPY_NUMBER(priv) 0
135#endif /* WIRELESS_SPY */
136
137/********************************************************************/
138/* Internal constants */
139/********************************************************************/
140
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200141/* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
142static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
143#define ENCAPS_OVERHEAD (sizeof(encaps_hdr) + 2)
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#define ORINOCO_MIN_MTU 256
Johannes Berg2c7060022008-10-30 22:09:54 +0100146#define ORINOCO_MAX_MTU (IEEE80211_MAX_DATA_LEN - ENCAPS_OVERHEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148#define SYMBOL_MAX_VER_LEN (14)
149#define USER_BAP 0
150#define IRQ_BAP 1
151#define MAX_IRQLOOPS_PER_IRQ 10
152#define MAX_IRQLOOPS_PER_JIFFY (20000/HZ) /* Based on a guestimate of
153 * how many events the
154 * device could
155 * legitimately generate */
156#define SMALL_KEY_SIZE 5
157#define LARGE_KEY_SIZE 13
158#define TX_NICBUF_SIZE_BUG 1585 /* Bug in Symbol firmware */
159
160#define DUMMY_FID 0xFFFF
161
162/*#define MAX_MULTICAST(priv) (priv->firmware_type == FIRMWARE_TYPE_AGERE ? \
163 HERMES_MAX_MULTICAST : 0)*/
164#define MAX_MULTICAST(priv) (HERMES_MAX_MULTICAST)
165
166#define ORINOCO_INTEN (HERMES_EV_RX | HERMES_EV_ALLOC \
167 | HERMES_EV_TX | HERMES_EV_TXEXC \
168 | HERMES_EV_WTERR | HERMES_EV_INFO \
169 | HERMES_EV_INFDROP )
170
Christoph Hellwig620554e2005-06-19 01:27:33 +0200171#define MAX_RID_LEN 1024
172
173static const struct iw_handler_def orinoco_handler_def;
Jeff Garzik7282d492006-09-13 14:30:00 -0400174static const struct ethtool_ops orinoco_ethtool_ops;
Christoph Hellwig620554e2005-06-19 01:27:33 +0200175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176/********************************************************************/
177/* Data tables */
178/********************************************************************/
179
180/* The frequency of each channel in MHz */
181static const long channel_frequency[] = {
182 2412, 2417, 2422, 2427, 2432, 2437, 2442,
183 2447, 2452, 2457, 2462, 2467, 2472, 2484
184};
185#define NUM_CHANNELS ARRAY_SIZE(channel_frequency)
186
187/* This tables gives the actual meanings of the bitrate IDs returned
188 * by the firmware. */
189static struct {
190 int bitrate; /* in 100s of kilobits */
191 int automatic;
192 u16 agere_txratectrl;
193 u16 intersil_txratectrl;
194} bitrate_table[] = {
195 {110, 1, 3, 15}, /* Entry 0 is the default */
196 {10, 0, 1, 1},
197 {10, 1, 1, 1},
198 {20, 0, 2, 2},
199 {20, 1, 6, 3},
200 {55, 0, 4, 4},
201 {55, 1, 7, 7},
202 {110, 0, 5, 8},
203};
204#define BITRATE_TABLE_SIZE ARRAY_SIZE(bitrate_table)
205
206/********************************************************************/
207/* Data types */
208/********************************************************************/
209
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400210/* Beginning of the Tx descriptor, used in TxExc handling */
211struct hermes_txexc_data {
212 struct hermes_tx_descriptor desc;
Pavel Roskind133ae42005-09-23 04:18:06 -0400213 __le16 frame_ctl;
214 __le16 duration_id;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200215 u8 addr1[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216} __attribute__ ((packed));
217
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200218/* Rx frame header except compatibility 802.3 header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219struct hermes_rx_descriptor {
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200220 /* Control */
Pavel Roskind133ae42005-09-23 04:18:06 -0400221 __le16 status;
222 __le32 time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 u8 silence;
224 u8 signal;
225 u8 rate;
226 u8 rxflow;
Pavel Roskind133ae42005-09-23 04:18:06 -0400227 __le32 reserved;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200228
229 /* 802.11 header */
Pavel Roskind133ae42005-09-23 04:18:06 -0400230 __le16 frame_ctl;
231 __le16 duration_id;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200232 u8 addr1[ETH_ALEN];
233 u8 addr2[ETH_ALEN];
234 u8 addr3[ETH_ALEN];
Pavel Roskind133ae42005-09-23 04:18:06 -0400235 __le16 seq_ctl;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200236 u8 addr4[ETH_ALEN];
237
238 /* Data length */
Pavel Roskind133ae42005-09-23 04:18:06 -0400239 __le16 data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240} __attribute__ ((packed));
241
242/********************************************************************/
243/* Function prototypes */
244/********************************************************************/
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246static int __orinoco_program_rids(struct net_device *dev);
247static void __orinoco_set_multicast_list(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249/********************************************************************/
David Kilroy23edcc42008-08-21 23:28:05 +0100250/* Michael MIC crypto setup */
251/********************************************************************/
252#define MICHAEL_MIC_LEN 8
253static int orinoco_mic_init(struct orinoco_private *priv)
254{
255 priv->tx_tfm_mic = crypto_alloc_hash("michael_mic", 0, 0);
256 if (IS_ERR(priv->tx_tfm_mic)) {
257 printk(KERN_DEBUG "orinoco_mic_init: could not allocate "
258 "crypto API michael_mic\n");
259 priv->tx_tfm_mic = NULL;
260 return -ENOMEM;
261 }
262
263 priv->rx_tfm_mic = crypto_alloc_hash("michael_mic", 0, 0);
264 if (IS_ERR(priv->rx_tfm_mic)) {
265 printk(KERN_DEBUG "orinoco_mic_init: could not allocate "
266 "crypto API michael_mic\n");
267 priv->rx_tfm_mic = NULL;
268 return -ENOMEM;
269 }
270
271 return 0;
272}
273
274static void orinoco_mic_free(struct orinoco_private *priv)
275{
276 if (priv->tx_tfm_mic)
277 crypto_free_hash(priv->tx_tfm_mic);
278 if (priv->rx_tfm_mic)
279 crypto_free_hash(priv->rx_tfm_mic);
280}
281
282static int michael_mic(struct crypto_hash *tfm_michael, u8 *key,
283 u8 *da, u8 *sa, u8 priority,
284 u8 *data, size_t data_len, u8 *mic)
285{
286 struct hash_desc desc;
287 struct scatterlist sg[2];
288 u8 hdr[ETH_HLEN + 2]; /* size of header + padding */
289
290 if (tfm_michael == NULL) {
291 printk(KERN_WARNING "michael_mic: tfm_michael == NULL\n");
292 return -1;
293 }
294
295 /* Copy header into buffer. We need the padding on the end zeroed */
296 memcpy(&hdr[0], da, ETH_ALEN);
297 memcpy(&hdr[ETH_ALEN], sa, ETH_ALEN);
298 hdr[ETH_ALEN*2] = priority;
299 hdr[ETH_ALEN*2+1] = 0;
300 hdr[ETH_ALEN*2+2] = 0;
301 hdr[ETH_ALEN*2+3] = 0;
302
303 /* Use scatter gather to MIC header and data in one go */
304 sg_init_table(sg, 2);
305 sg_set_buf(&sg[0], hdr, sizeof(hdr));
306 sg_set_buf(&sg[1], data, data_len);
307
308 if (crypto_hash_setkey(tfm_michael, key, MIC_KEYLEN))
309 return -1;
310
311 desc.tfm = tfm_michael;
312 desc.flags = 0;
313 return crypto_hash_digest(&desc, sg, data_len + sizeof(hdr),
314 mic);
315}
316
317/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318/* Internal helper functions */
319/********************************************************************/
320
321static inline void set_port_type(struct orinoco_private *priv)
322{
323 switch (priv->iw_mode) {
324 case IW_MODE_INFRA:
325 priv->port_type = 1;
326 priv->createibss = 0;
327 break;
328 case IW_MODE_ADHOC:
329 if (priv->prefer_port3) {
330 priv->port_type = 3;
331 priv->createibss = 0;
332 } else {
333 priv->port_type = priv->ibss_port;
334 priv->createibss = 1;
335 }
336 break;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200337 case IW_MODE_MONITOR:
338 priv->port_type = 3;
339 priv->createibss = 0;
340 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 default:
342 printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
343 priv->ndev->name);
344 }
345}
346
Dan Williams1e3428e2007-10-10 23:56:25 -0400347#define ORINOCO_MAX_BSS_COUNT 64
348static int orinoco_bss_data_allocate(struct orinoco_private *priv)
349{
David Kilroy01632fa2008-08-21 23:27:58 +0100350 if (priv->bss_xbss_data)
Dan Williams1e3428e2007-10-10 23:56:25 -0400351 return 0;
352
David Kilroy01632fa2008-08-21 23:27:58 +0100353 if (priv->has_ext_scan)
354 priv->bss_xbss_data = kzalloc(ORINOCO_MAX_BSS_COUNT *
355 sizeof(struct xbss_element),
356 GFP_KERNEL);
357 else
358 priv->bss_xbss_data = kzalloc(ORINOCO_MAX_BSS_COUNT *
359 sizeof(struct bss_element),
360 GFP_KERNEL);
361
362 if (!priv->bss_xbss_data) {
Dan Williams1e3428e2007-10-10 23:56:25 -0400363 printk(KERN_WARNING "Out of memory allocating beacons");
364 return -ENOMEM;
365 }
366 return 0;
367}
368
369static void orinoco_bss_data_free(struct orinoco_private *priv)
370{
David Kilroy01632fa2008-08-21 23:27:58 +0100371 kfree(priv->bss_xbss_data);
372 priv->bss_xbss_data = NULL;
Dan Williams1e3428e2007-10-10 23:56:25 -0400373}
374
David Kilroy01632fa2008-08-21 23:27:58 +0100375#define PRIV_BSS ((struct bss_element *)priv->bss_xbss_data)
376#define PRIV_XBSS ((struct xbss_element *)priv->bss_xbss_data)
Dan Williams1e3428e2007-10-10 23:56:25 -0400377static void orinoco_bss_data_init(struct orinoco_private *priv)
378{
379 int i;
380
381 INIT_LIST_HEAD(&priv->bss_free_list);
382 INIT_LIST_HEAD(&priv->bss_list);
David Kilroy01632fa2008-08-21 23:27:58 +0100383 if (priv->has_ext_scan)
384 for (i = 0; i < ORINOCO_MAX_BSS_COUNT; i++)
385 list_add_tail(&(PRIV_XBSS[i].list),
386 &priv->bss_free_list);
387 else
388 for (i = 0; i < ORINOCO_MAX_BSS_COUNT; i++)
389 list_add_tail(&(PRIV_BSS[i].list),
390 &priv->bss_free_list);
391
392}
393
394static inline u8 *orinoco_get_ie(u8 *data, size_t len,
Johannes Berg2c7060022008-10-30 22:09:54 +0100395 enum ieee80211_eid eid)
David Kilroy01632fa2008-08-21 23:27:58 +0100396{
397 u8 *p = data;
398 while ((p + 2) < (data + len)) {
399 if (p[0] == eid)
400 return p;
401 p += p[1] + 2;
402 }
403 return NULL;
404}
405
406#define WPA_OUI_TYPE "\x00\x50\xF2\x01"
407#define WPA_SELECTOR_LEN 4
408static inline u8 *orinoco_get_wpa_ie(u8 *data, size_t len)
409{
410 u8 *p = data;
411 while ((p + 2 + WPA_SELECTOR_LEN) < (data + len)) {
Johannes Berg2c7060022008-10-30 22:09:54 +0100412 if ((p[0] == WLAN_EID_GENERIC) &&
David Kilroy01632fa2008-08-21 23:27:58 +0100413 (memcmp(&p[2], WPA_OUI_TYPE, WPA_SELECTOR_LEN) == 0))
414 return p;
415 p += p[1] + 2;
416 }
417 return NULL;
Dan Williams1e3428e2007-10-10 23:56:25 -0400418}
419
David Kilroy3994d502008-08-21 23:27:54 +0100420
421/********************************************************************/
422/* Download functionality */
423/********************************************************************/
424
425struct fw_info {
426 char *pri_fw;
427 char *sta_fw;
428 char *ap_fw;
429 u32 pda_addr;
430 u16 pda_size;
431};
432
433const static struct fw_info orinoco_fw[] = {
David Kilroy74734312008-11-22 10:37:25 +0000434 { NULL, "agere_sta_fw.bin", "agere_ap_fw.bin", 0x00390000, 1000 },
435 { NULL, "prism_sta_fw.bin", "prism_ap_fw.bin", 0, 1024 },
436 { "symbol_sp24t_prim_fw", "symbol_sp24t_sec_fw", NULL, 0x00003100, 512 }
David Kilroy3994d502008-08-21 23:27:54 +0100437};
438
439/* Structure used to access fields in FW
440 * Make sure LE decoding macros are used
441 */
442struct orinoco_fw_header {
443 char hdr_vers[6]; /* ASCII string for header version */
444 __le16 headersize; /* Total length of header */
445 __le32 entry_point; /* NIC entry point */
446 __le32 blocks; /* Number of blocks to program */
447 __le32 block_offset; /* Offset of block data from eof header */
448 __le32 pdr_offset; /* Offset to PDR data from eof header */
449 __le32 pri_offset; /* Offset to primary plug data */
450 __le32 compat_offset; /* Offset to compatibility data*/
451 char signature[0]; /* FW signature length headersize-20 */
452} __attribute__ ((packed));
453
454/* Download either STA or AP firmware into the card. */
455static int
456orinoco_dl_firmware(struct orinoco_private *priv,
457 const struct fw_info *fw,
458 int ap)
459{
460 /* Plug Data Area (PDA) */
Andrey Borzenkov70458252008-10-10 21:26:30 +0400461 __le16 *pda;
David Kilroy3994d502008-08-21 23:27:54 +0100462
463 hermes_t *hw = &priv->hw;
464 const struct firmware *fw_entry;
465 const struct orinoco_fw_header *hdr;
466 const unsigned char *first_block;
467 const unsigned char *end;
468 const char *firmware;
469 struct net_device *dev = priv->ndev;
Andrey Borzenkov70458252008-10-10 21:26:30 +0400470 int err = 0;
471
472 pda = kzalloc(fw->pda_size, GFP_KERNEL);
473 if (!pda)
474 return -ENOMEM;
David Kilroy3994d502008-08-21 23:27:54 +0100475
476 if (ap)
477 firmware = fw->ap_fw;
478 else
479 firmware = fw->sta_fw;
480
481 printk(KERN_DEBUG "%s: Attempting to download firmware %s\n",
482 dev->name, firmware);
483
484 /* Read current plug data */
Andrey Borzenkov70458252008-10-10 21:26:30 +0400485 err = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 0);
David Kilroy3994d502008-08-21 23:27:54 +0100486 printk(KERN_DEBUG "%s: Read PDA returned %d\n", dev->name, err);
487 if (err)
Andrey Borzenkov70458252008-10-10 21:26:30 +0400488 goto free;
David Kilroy3994d502008-08-21 23:27:54 +0100489
David Kilroy74734312008-11-22 10:37:25 +0000490 if (!priv->cached_fw) {
Andrey Borzenkov4fb30782008-10-19 12:06:11 +0400491 err = request_firmware(&fw_entry, firmware, priv->dev);
David Kilroy74734312008-11-22 10:37:25 +0000492
Andrey Borzenkov4fb30782008-10-19 12:06:11 +0400493 if (err) {
494 printk(KERN_ERR "%s: Cannot find firmware %s\n",
495 dev->name, firmware);
496 err = -ENOENT;
497 goto free;
498 }
David Kilroy74734312008-11-22 10:37:25 +0000499 } else
500 fw_entry = priv->cached_fw;
David Kilroy3994d502008-08-21 23:27:54 +0100501
502 hdr = (const struct orinoco_fw_header *) fw_entry->data;
503
504 /* Enable aux port to allow programming */
505 err = hermesi_program_init(hw, le32_to_cpu(hdr->entry_point));
506 printk(KERN_DEBUG "%s: Program init returned %d\n", dev->name, err);
507 if (err != 0)
508 goto abort;
509
510 /* Program data */
511 first_block = (fw_entry->data +
512 le16_to_cpu(hdr->headersize) +
513 le32_to_cpu(hdr->block_offset));
514 end = fw_entry->data + fw_entry->size;
515
516 err = hermes_program(hw, first_block, end);
517 printk(KERN_DEBUG "%s: Program returned %d\n", dev->name, err);
518 if (err != 0)
519 goto abort;
520
521 /* Update production data */
522 first_block = (fw_entry->data +
523 le16_to_cpu(hdr->headersize) +
524 le32_to_cpu(hdr->pdr_offset));
525
526 err = hermes_apply_pda_with_defaults(hw, first_block, pda);
527 printk(KERN_DEBUG "%s: Apply PDA returned %d\n", dev->name, err);
528 if (err)
529 goto abort;
530
531 /* Tell card we've finished */
532 err = hermesi_program_end(hw);
533 printk(KERN_DEBUG "%s: Program end returned %d\n", dev->name, err);
534 if (err != 0)
535 goto abort;
536
537 /* Check if we're running */
538 printk(KERN_DEBUG "%s: hermes_present returned %d\n",
539 dev->name, hermes_present(hw));
540
541abort:
David Kilroy74734312008-11-22 10:37:25 +0000542 /* If we requested the firmware, release it. */
543 if (!priv->cached_fw)
Andrey Borzenkov4fb30782008-10-19 12:06:11 +0400544 release_firmware(fw_entry);
Andrey Borzenkov70458252008-10-10 21:26:30 +0400545
546free:
547 kfree(pda);
David Kilroy3994d502008-08-21 23:27:54 +0100548 return err;
549}
550
551/* End markers */
552#define TEXT_END 0x1A /* End of text header */
553
554/*
555 * Process a firmware image - stop the card, load the firmware, reset
556 * the card and make sure it responds. For the secondary firmware take
557 * care of the PDA - read it and then write it on top of the firmware.
558 */
559static int
560symbol_dl_image(struct orinoco_private *priv, const struct fw_info *fw,
561 const unsigned char *image, const unsigned char *end,
562 int secondary)
563{
564 hermes_t *hw = &priv->hw;
Andrey Borzenkov70458252008-10-10 21:26:30 +0400565 int ret = 0;
David Kilroy3994d502008-08-21 23:27:54 +0100566 const unsigned char *ptr;
567 const unsigned char *first_block;
568
569 /* Plug Data Area (PDA) */
Andrey Borzenkov70458252008-10-10 21:26:30 +0400570 __le16 *pda = NULL;
David Kilroy3994d502008-08-21 23:27:54 +0100571
572 /* Binary block begins after the 0x1A marker */
573 ptr = image;
574 while (*ptr++ != TEXT_END);
575 first_block = ptr;
576
577 /* Read the PDA from EEPROM */
578 if (secondary) {
Andrey Borzenkov70458252008-10-10 21:26:30 +0400579 pda = kzalloc(fw->pda_size, GFP_KERNEL);
580 if (!pda)
581 return -ENOMEM;
582
583 ret = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 1);
David Kilroy3994d502008-08-21 23:27:54 +0100584 if (ret)
Andrey Borzenkov70458252008-10-10 21:26:30 +0400585 goto free;
David Kilroy3994d502008-08-21 23:27:54 +0100586 }
587
588 /* Stop the firmware, so that it can be safely rewritten */
589 if (priv->stop_fw) {
590 ret = priv->stop_fw(priv, 1);
591 if (ret)
Andrey Borzenkov70458252008-10-10 21:26:30 +0400592 goto free;
David Kilroy3994d502008-08-21 23:27:54 +0100593 }
594
595 /* Program the adapter with new firmware */
596 ret = hermes_program(hw, first_block, end);
597 if (ret)
Andrey Borzenkov70458252008-10-10 21:26:30 +0400598 goto free;
David Kilroy3994d502008-08-21 23:27:54 +0100599
600 /* Write the PDA to the adapter */
601 if (secondary) {
602 size_t len = hermes_blocks_length(first_block);
603 ptr = first_block + len;
604 ret = hermes_apply_pda(hw, ptr, pda);
Andrey Borzenkov70458252008-10-10 21:26:30 +0400605 kfree(pda);
David Kilroy3994d502008-08-21 23:27:54 +0100606 if (ret)
607 return ret;
608 }
609
610 /* Run the firmware */
611 if (priv->stop_fw) {
612 ret = priv->stop_fw(priv, 0);
613 if (ret)
614 return ret;
615 }
616
617 /* Reset hermes chip and make sure it responds */
618 ret = hermes_init(hw);
619
620 /* hermes_reset() should return 0 with the secondary firmware */
621 if (secondary && ret != 0)
622 return -ENODEV;
623
624 /* And this should work with any firmware */
625 if (!hermes_present(hw))
626 return -ENODEV;
627
628 return 0;
Andrey Borzenkov70458252008-10-10 21:26:30 +0400629
630free:
631 kfree(pda);
632 return ret;
David Kilroy3994d502008-08-21 23:27:54 +0100633}
634
635
636/*
637 * Download the firmware into the card, this also does a PCMCIA soft
638 * reset on the card, to make sure it's in a sane state.
639 */
640static int
641symbol_dl_firmware(struct orinoco_private *priv,
642 const struct fw_info *fw)
643{
644 struct net_device *dev = priv->ndev;
645 int ret;
646 const struct firmware *fw_entry;
647
David Kilroy2cea7b22008-11-22 10:37:26 +0000648 if (!priv->cached_pri_fw) {
649 if (request_firmware(&fw_entry, fw->pri_fw, priv->dev) != 0) {
650 printk(KERN_ERR "%s: Cannot find firmware: %s\n",
651 dev->name, fw->pri_fw);
652 return -ENOENT;
653 }
654 } else
655 fw_entry = priv->cached_pri_fw;
David Kilroy3994d502008-08-21 23:27:54 +0100656
657 /* Load primary firmware */
658 ret = symbol_dl_image(priv, fw, fw_entry->data,
659 fw_entry->data + fw_entry->size, 0);
David Kilroy2cea7b22008-11-22 10:37:26 +0000660
661 if (!priv->cached_pri_fw)
662 release_firmware(fw_entry);
David Kilroy3994d502008-08-21 23:27:54 +0100663 if (ret) {
664 printk(KERN_ERR "%s: Primary firmware download failed\n",
665 dev->name);
666 return ret;
667 }
668
David Kilroy2cea7b22008-11-22 10:37:26 +0000669 if (!priv->cached_fw) {
670 if (request_firmware(&fw_entry, fw->sta_fw, priv->dev) != 0) {
671 printk(KERN_ERR "%s: Cannot find firmware: %s\n",
672 dev->name, fw->sta_fw);
673 return -ENOENT;
674 }
675 } else
676 fw_entry = priv->cached_fw;
David Kilroy3994d502008-08-21 23:27:54 +0100677
678 /* Load secondary firmware */
679 ret = symbol_dl_image(priv, fw, fw_entry->data,
680 fw_entry->data + fw_entry->size, 1);
David Kilroy2cea7b22008-11-22 10:37:26 +0000681 if (!priv->cached_fw)
682 release_firmware(fw_entry);
David Kilroy3994d502008-08-21 23:27:54 +0100683 if (ret) {
684 printk(KERN_ERR "%s: Secondary firmware download failed\n",
685 dev->name);
686 }
687
688 return ret;
689}
690
691static int orinoco_download(struct orinoco_private *priv)
692{
693 int err = 0;
694 /* Reload firmware */
695 switch (priv->firmware_type) {
696 case FIRMWARE_TYPE_AGERE:
697 /* case FIRMWARE_TYPE_INTERSIL: */
698 err = orinoco_dl_firmware(priv,
699 &orinoco_fw[priv->firmware_type], 0);
700 break;
701
702 case FIRMWARE_TYPE_SYMBOL:
703 err = symbol_dl_firmware(priv,
704 &orinoco_fw[priv->firmware_type]);
705 break;
706 case FIRMWARE_TYPE_INTERSIL:
707 break;
708 }
709 /* TODO: if we fail we probably need to reinitialise
710 * the driver */
711
712 return err;
713}
714
David Kilroy74734312008-11-22 10:37:25 +0000715static void orinoco_cache_fw(struct orinoco_private *priv, int ap)
716{
717 const struct firmware *fw_entry = NULL;
David Kilroy2cea7b22008-11-22 10:37:26 +0000718 const char *pri_fw;
David Kilroy74734312008-11-22 10:37:25 +0000719 const char *fw;
720
David Kilroy2cea7b22008-11-22 10:37:26 +0000721 pri_fw = orinoco_fw[priv->firmware_type].pri_fw;
David Kilroy74734312008-11-22 10:37:25 +0000722 if (ap)
723 fw = orinoco_fw[priv->firmware_type].ap_fw;
724 else
725 fw = orinoco_fw[priv->firmware_type].sta_fw;
726
David Kilroy2cea7b22008-11-22 10:37:26 +0000727 if (pri_fw) {
728 if (request_firmware(&fw_entry, pri_fw, priv->dev) == 0)
729 priv->cached_pri_fw = fw_entry;
730 }
731
David Kilroy74734312008-11-22 10:37:25 +0000732 if (fw) {
733 if (request_firmware(&fw_entry, fw, priv->dev) == 0)
734 priv->cached_fw = fw_entry;
735 }
736}
737
738static void orinoco_uncache_fw(struct orinoco_private *priv)
739{
David Kilroy2cea7b22008-11-22 10:37:26 +0000740 if (priv->cached_pri_fw)
741 release_firmware(priv->cached_pri_fw);
David Kilroy74734312008-11-22 10:37:25 +0000742 if (priv->cached_fw)
743 release_firmware(priv->cached_fw);
744
David Kilroy2cea7b22008-11-22 10:37:26 +0000745 priv->cached_pri_fw = NULL;
David Kilroy74734312008-11-22 10:37:25 +0000746 priv->cached_fw = NULL;
747}
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749/********************************************************************/
750/* Device methods */
751/********************************************************************/
752
753static int orinoco_open(struct net_device *dev)
754{
755 struct orinoco_private *priv = netdev_priv(dev);
756 unsigned long flags;
757 int err;
758
759 if (orinoco_lock(priv, &flags) != 0)
760 return -EBUSY;
761
762 err = __orinoco_up(dev);
763
764 if (! err)
765 priv->open = 1;
766
767 orinoco_unlock(priv, &flags);
768
769 return err;
770}
771
Christoph Hellwigad8f4512005-05-14 17:30:17 +0200772static int orinoco_stop(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 struct orinoco_private *priv = netdev_priv(dev);
775 int err = 0;
776
777 /* We mustn't use orinoco_lock() here, because we need to be
778 able to close the interface even if hw_unavailable is set
779 (e.g. as we're released after a PC Card removal) */
780 spin_lock_irq(&priv->lock);
781
782 priv->open = 0;
783
784 err = __orinoco_down(dev);
785
786 spin_unlock_irq(&priv->lock);
787
788 return err;
789}
790
791static struct net_device_stats *orinoco_get_stats(struct net_device *dev)
792{
793 struct orinoco_private *priv = netdev_priv(dev);
794
795 return &priv->stats;
796}
797
798static struct iw_statistics *orinoco_get_wireless_stats(struct net_device *dev)
799{
800 struct orinoco_private *priv = netdev_priv(dev);
801 hermes_t *hw = &priv->hw;
802 struct iw_statistics *wstats = &priv->wstats;
David Gibsone67d9d92005-05-12 20:01:22 -0400803 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 unsigned long flags;
805
806 if (! netif_device_present(dev)) {
807 printk(KERN_WARNING "%s: get_wireless_stats() called while device not present\n",
808 dev->name);
809 return NULL; /* FIXME: Can we do better than this? */
810 }
811
David Gibsone67d9d92005-05-12 20:01:22 -0400812 /* If busy, return the old stats. Returning NULL may cause
813 * the interface to disappear from /proc/net/wireless */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (orinoco_lock(priv, &flags) != 0)
David Gibsone67d9d92005-05-12 20:01:22 -0400815 return wstats;
816
817 /* We can't really wait for the tallies inquiry command to
818 * complete, so we just use the previous results and trigger
819 * a new tallies inquiry command for next time - Jean II */
820 /* FIXME: Really we should wait for the inquiry to come back -
821 * as it is the stats we give don't make a whole lot of sense.
822 * Unfortunately, it's not clear how to do that within the
823 * wireless extensions framework: I think we're in user
824 * context, but a lock seems to be held by the time we get in
825 * here so we're not safe to sleep here. */
826 hermes_inquire(hw, HERMES_INQ_TALLIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 if (priv->iw_mode == IW_MODE_ADHOC) {
829 memset(&wstats->qual, 0, sizeof(wstats->qual));
830 /* If a spy address is defined, we report stats of the
831 * first spy address - Jean II */
832 if (SPY_NUMBER(priv)) {
Pavel Roskin343c6862005-09-09 18:43:02 -0400833 wstats->qual.qual = priv->spy_data.spy_stat[0].qual;
834 wstats->qual.level = priv->spy_data.spy_stat[0].level;
835 wstats->qual.noise = priv->spy_data.spy_stat[0].noise;
836 wstats->qual.updated = priv->spy_data.spy_stat[0].updated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838 } else {
839 struct {
Pavel Roskina208c4e2006-04-07 04:10:26 -0400840 __le16 qual, signal, noise, unused;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 } __attribute__ ((packed)) cq;
842
843 err = HERMES_READ_RECORD(hw, USER_BAP,
844 HERMES_RID_COMMSQUALITY, &cq);
David Gibsone67d9d92005-05-12 20:01:22 -0400845
846 if (!err) {
847 wstats->qual.qual = (int)le16_to_cpu(cq.qual);
848 wstats->qual.level = (int)le16_to_cpu(cq.signal) - 0x95;
849 wstats->qual.noise = (int)le16_to_cpu(cq.noise) - 0x95;
Andrey Borzenkovf941f852008-11-15 17:15:09 +0300850 wstats->qual.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
David Gibsone67d9d92005-05-12 20:01:22 -0400851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return wstats;
856}
857
858static void orinoco_set_multicast_list(struct net_device *dev)
859{
860 struct orinoco_private *priv = netdev_priv(dev);
861 unsigned long flags;
862
863 if (orinoco_lock(priv, &flags) != 0) {
864 printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
865 "called when hw_unavailable\n", dev->name);
866 return;
867 }
868
869 __orinoco_set_multicast_list(dev);
870 orinoco_unlock(priv, &flags);
871}
872
873static int orinoco_change_mtu(struct net_device *dev, int new_mtu)
874{
875 struct orinoco_private *priv = netdev_priv(dev);
876
877 if ( (new_mtu < ORINOCO_MIN_MTU) || (new_mtu > ORINOCO_MAX_MTU) )
878 return -EINVAL;
879
Johannes Berg2c7060022008-10-30 22:09:54 +0100880 /* MTU + encapsulation + header length */
881 if ( (new_mtu + ENCAPS_OVERHEAD + sizeof(struct ieee80211_hdr)) >
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 (priv->nicbuf_size - ETH_HLEN) )
883 return -EINVAL;
884
885 dev->mtu = new_mtu;
886
887 return 0;
888}
889
890/********************************************************************/
891/* Tx path */
892/********************************************************************/
893
894static int orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
895{
896 struct orinoco_private *priv = netdev_priv(dev);
897 struct net_device_stats *stats = &priv->stats;
898 hermes_t *hw = &priv->hw;
899 int err = 0;
900 u16 txfid = priv->txfid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 struct ethhdr *eh;
David Kilroy6eecad72008-08-21 23:27:56 +0100902 int tx_control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 unsigned long flags;
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (! netif_running(dev)) {
906 printk(KERN_ERR "%s: Tx on stopped device!\n",
907 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400908 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910
911 if (netif_queue_stopped(dev)) {
912 printk(KERN_DEBUG "%s: Tx while transmitter busy!\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 (orinoco_lock(priv, &flags) != 0) {
918 printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\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
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200923 if (! netif_carrier_ok(dev) || (priv->iw_mode == IW_MODE_MONITOR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 /* Oops, the firmware hasn't established a connection,
925 silently drop the packet (this seems to be the
926 safest approach). */
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400927 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929
Pavel Roskin8d5be082006-04-07 04:10:41 -0400930 /* Check packet length */
Pavel Roskina28dc812006-04-07 04:10:45 -0400931 if (skb->len < ETH_HLEN)
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400932 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
David Kilroy6eecad72008-08-21 23:27:56 +0100934 tx_control = HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
David Kilroy23edcc42008-08-21 23:28:05 +0100936 if (priv->encode_alg == IW_ENCODE_ALG_TKIP)
937 tx_control |= (priv->tx_key << HERMES_MIC_KEY_ID_SHIFT) |
938 HERMES_TXCTRL_MIC;
939
David Kilroy6eecad72008-08-21 23:27:56 +0100940 if (priv->has_alt_txcntl) {
941 /* WPA enabled firmwares have tx_cntl at the end of
942 * the 802.11 header. So write zeroed descriptor and
943 * 802.11 header at the same time
944 */
945 char desc[HERMES_802_3_OFFSET];
946 __le16 *txcntl = (__le16 *) &desc[HERMES_TXCNTL2_OFFSET];
947
948 memset(&desc, 0, sizeof(desc));
949
950 *txcntl = cpu_to_le16(tx_control);
951 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
952 txfid, 0);
953 if (err) {
954 if (net_ratelimit())
955 printk(KERN_ERR "%s: Error %d writing Tx "
956 "descriptor to BAP\n", dev->name, err);
957 goto busy;
958 }
959 } else {
960 struct hermes_tx_descriptor desc;
961
962 memset(&desc, 0, sizeof(desc));
963
964 desc.tx_control = cpu_to_le16(tx_control);
965 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
966 txfid, 0);
967 if (err) {
968 if (net_ratelimit())
969 printk(KERN_ERR "%s: Error %d writing Tx "
970 "descriptor to BAP\n", dev->name, err);
971 goto busy;
972 }
973
974 /* Clear the 802.11 header and data length fields - some
975 * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
976 * if this isn't done. */
977 hermes_clear_words(hw, HERMES_DATA0,
978 HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
David Kilroy23edcc42008-08-21 23:28:05 +0100981 eh = (struct ethhdr *)skb->data;
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 /* Encapsulate Ethernet-II frames */
984 if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
Pavel Roskina28dc812006-04-07 04:10:45 -0400985 struct header_struct {
986 struct ethhdr eth; /* 802.3 header */
987 u8 encap[6]; /* 802.2 header */
988 } __attribute__ ((packed)) hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Pavel Roskina28dc812006-04-07 04:10:45 -0400990 /* Strip destination and source from the data */
991 skb_pull(skb, 2 * ETH_ALEN);
Pavel Roskina28dc812006-04-07 04:10:45 -0400992
993 /* And move them to a separate header */
994 memcpy(&hdr.eth, eh, 2 * ETH_ALEN);
995 hdr.eth.h_proto = htons(sizeof(encaps_hdr) + skb->len);
996 memcpy(hdr.encap, encaps_hdr, sizeof(encaps_hdr));
997
David Kilroy23edcc42008-08-21 23:28:05 +0100998 /* Insert the SNAP header */
999 if (skb_headroom(skb) < sizeof(hdr)) {
1000 printk(KERN_ERR
1001 "%s: Not enough headroom for 802.2 headers %d\n",
1002 dev->name, skb_headroom(skb));
1003 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
David Kilroy23edcc42008-08-21 23:28:05 +01001005 eh = (struct ethhdr *) skb_push(skb, sizeof(hdr));
1006 memcpy(eh, &hdr, sizeof(hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
1008
Pavel Roskina28dc812006-04-07 04:10:45 -04001009 err = hermes_bap_pwrite(hw, USER_BAP, skb->data, skb->len,
David Kilroy23edcc42008-08-21 23:28:05 +01001010 txfid, HERMES_802_3_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (err) {
1012 printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
1013 dev->name, err);
Pavel Roskin470e2aa2006-04-07 04:10:43 -04001014 goto busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016
David Kilroy23edcc42008-08-21 23:28:05 +01001017 /* Calculate Michael MIC */
1018 if (priv->encode_alg == IW_ENCODE_ALG_TKIP) {
1019 u8 mic_buf[MICHAEL_MIC_LEN + 1];
1020 u8 *mic;
1021 size_t offset;
1022 size_t len;
1023
1024 if (skb->len % 2) {
1025 /* MIC start is on an odd boundary */
1026 mic_buf[0] = skb->data[skb->len - 1];
1027 mic = &mic_buf[1];
1028 offset = skb->len - 1;
1029 len = MICHAEL_MIC_LEN + 1;
1030 } else {
1031 mic = &mic_buf[0];
1032 offset = skb->len;
1033 len = MICHAEL_MIC_LEN;
1034 }
1035
1036 michael_mic(priv->tx_tfm_mic,
1037 priv->tkip_key[priv->tx_key].tx_mic,
1038 eh->h_dest, eh->h_source, 0 /* priority */,
1039 skb->data + ETH_HLEN, skb->len - ETH_HLEN, mic);
1040
1041 /* Write the MIC */
1042 err = hermes_bap_pwrite(hw, USER_BAP, &mic_buf[0], len,
1043 txfid, HERMES_802_3_OFFSET + offset);
1044 if (err) {
1045 printk(KERN_ERR "%s: Error %d writing MIC to BAP\n",
1046 dev->name, err);
1047 goto busy;
1048 }
1049 }
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 /* Finally, we actually initiate the send */
1052 netif_stop_queue(dev);
1053
1054 err = hermes_docmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
1055 txfid, NULL);
1056 if (err) {
1057 netif_start_queue(dev);
Andrew Mortonc367c212005-10-19 21:23:44 -07001058 if (net_ratelimit())
1059 printk(KERN_ERR "%s: Error %d transmitting packet\n",
1060 dev->name, err);
Pavel Roskin470e2aa2006-04-07 04:10:43 -04001061 goto busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063
1064 dev->trans_start = jiffies;
David Kilroy23edcc42008-08-21 23:28:05 +01001065 stats->tx_bytes += HERMES_802_3_OFFSET + skb->len;
Pavel Roskin470e2aa2006-04-07 04:10:43 -04001066 goto ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Pavel Roskin470e2aa2006-04-07 04:10:43 -04001068 drop:
1069 stats->tx_errors++;
1070 stats->tx_dropped++;
1071
1072 ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 dev_kfree_skb(skb);
Pavel Roskinb34b8672006-04-07 04:10:36 -04001075 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Pavel Roskin470e2aa2006-04-07 04:10:43 -04001077 busy:
Jiri Benc2c1bd262006-04-07 04:10:47 -04001078 if (err == -EIO)
1079 schedule_work(&priv->reset_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 orinoco_unlock(priv, &flags);
Pavel Roskinb34b8672006-04-07 04:10:36 -04001081 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
1084static void __orinoco_ev_alloc(struct net_device *dev, hermes_t *hw)
1085{
1086 struct orinoco_private *priv = netdev_priv(dev);
1087 u16 fid = hermes_read_regn(hw, ALLOCFID);
1088
1089 if (fid != priv->txfid) {
1090 if (fid != DUMMY_FID)
1091 printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
1092 dev->name, fid);
1093 return;
1094 }
1095
1096 hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
1097}
1098
1099static void __orinoco_ev_tx(struct net_device *dev, hermes_t *hw)
1100{
1101 struct orinoco_private *priv = netdev_priv(dev);
1102 struct net_device_stats *stats = &priv->stats;
1103
1104 stats->tx_packets++;
1105
1106 netif_wake_queue(dev);
1107
1108 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
1109}
1110
1111static void __orinoco_ev_txexc(struct net_device *dev, hermes_t *hw)
1112{
1113 struct orinoco_private *priv = netdev_priv(dev);
1114 struct net_device_stats *stats = &priv->stats;
1115 u16 fid = hermes_read_regn(hw, TXCOMPLFID);
Pavel Roskind133ae42005-09-23 04:18:06 -04001116 u16 status;
Pavel Roskin30c2d3b2006-04-07 04:10:34 -04001117 struct hermes_txexc_data hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 int err = 0;
1119
1120 if (fid == DUMMY_FID)
1121 return; /* Nothing's really happened */
1122
Pavel Roskin48ca7032005-09-23 04:18:06 -04001123 /* Read part of the frame header - we need status and addr1 */
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001124 err = hermes_bap_pread(hw, IRQ_BAP, &hdr,
Pavel Roskin30c2d3b2006-04-07 04:10:34 -04001125 sizeof(struct hermes_txexc_data),
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001126 fid, 0);
1127
1128 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
1129 stats->tx_errors++;
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (err) {
1132 printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
1133 "(FID=%04X error %d)\n",
1134 dev->name, fid, err);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001135 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
1137
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001138 DEBUG(1, "%s: Tx error, err %d (FID=%04X)\n", dev->name,
1139 err, fid);
1140
1141 /* We produce a TXDROP event only for retry or lifetime
1142 * exceeded, because that's the only status that really mean
1143 * that this particular node went away.
1144 * Other errors means that *we* screwed up. - Jean II */
Pavel Roskin30c2d3b2006-04-07 04:10:34 -04001145 status = le16_to_cpu(hdr.desc.status);
Pavel Roskind133ae42005-09-23 04:18:06 -04001146 if (status & (HERMES_TXSTAT_RETRYERR | HERMES_TXSTAT_AGEDERR)) {
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001147 union iwreq_data wrqu;
1148
1149 /* Copy 802.11 dest address.
1150 * We use the 802.11 header because the frame may
1151 * not be 802.3 or may be mangled...
1152 * In Ad-Hoc mode, it will be the node address.
1153 * In managed mode, it will be most likely the AP addr
1154 * User space will figure out how to convert it to
1155 * whatever it needs (IP address or else).
1156 * - Jean II */
1157 memcpy(wrqu.addr.sa_data, hdr.addr1, ETH_ALEN);
1158 wrqu.addr.sa_family = ARPHRD_ETHER;
1159
1160 /* Send event to user space */
1161 wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
1162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
1167static void orinoco_tx_timeout(struct net_device *dev)
1168{
1169 struct orinoco_private *priv = netdev_priv(dev);
1170 struct net_device_stats *stats = &priv->stats;
1171 struct hermes *hw = &priv->hw;
1172
1173 printk(KERN_WARNING "%s: Tx timeout! "
1174 "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
1175 dev->name, hermes_read_regn(hw, ALLOCFID),
1176 hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
1177
1178 stats->tx_errors++;
1179
1180 schedule_work(&priv->reset_work);
1181}
1182
1183/********************************************************************/
1184/* Rx path (data frames) */
1185/********************************************************************/
1186
1187/* Does the frame have a SNAP header indicating it should be
1188 * de-encapsulated to Ethernet-II? */
1189static inline int is_ethersnap(void *_hdr)
1190{
1191 u8 *hdr = _hdr;
1192
1193 /* We de-encapsulate all packets which, a) have SNAP headers
1194 * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
1195 * and where b) the OUI of the SNAP header is 00:00:00 or
1196 * 00:00:f8 - we need both because different APs appear to use
1197 * different OUIs for some reason */
1198 return (memcmp(hdr, &encaps_hdr, 5) == 0)
1199 && ( (hdr[5] == 0x00) || (hdr[5] == 0xf8) );
1200}
1201
1202static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
1203 int level, int noise)
1204{
Pavel Roskin343c6862005-09-09 18:43:02 -04001205 struct iw_quality wstats;
1206 wstats.level = level - 0x95;
1207 wstats.noise = noise - 0x95;
1208 wstats.qual = (level > noise) ? (level - noise) : 0;
Andrey Borzenkovf941f852008-11-15 17:15:09 +03001209 wstats.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
Pavel Roskin343c6862005-09-09 18:43:02 -04001210 /* Update spy records */
1211 wireless_spy_update(dev, mac, &wstats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
1214static void orinoco_stat_gather(struct net_device *dev,
1215 struct sk_buff *skb,
1216 struct hermes_rx_descriptor *desc)
1217{
1218 struct orinoco_private *priv = netdev_priv(dev);
1219
1220 /* Using spy support with lots of Rx packets, like in an
1221 * infrastructure (AP), will really slow down everything, because
1222 * the MAC address must be compared to each entry of the spy list.
1223 * If the user really asks for it (set some address in the
1224 * spy list), we do it, but he will pay the price.
1225 * Note that to get here, you need both WIRELESS_SPY
1226 * compiled in AND some addresses in the list !!!
1227 */
1228 /* Note : gcc will optimise the whole section away if
1229 * WIRELESS_SPY is not defined... - Jean II */
1230 if (SPY_NUMBER(priv)) {
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001231 orinoco_spy_gather(dev, skb_mac_header(skb) + ETH_ALEN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 desc->signal, desc->silence);
1233 }
1234}
1235
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001236/*
1237 * orinoco_rx_monitor - handle received monitor frames.
1238 *
1239 * Arguments:
1240 * dev network device
1241 * rxfid received FID
1242 * desc rx descriptor of the frame
1243 *
1244 * Call context: interrupt
1245 */
1246static void orinoco_rx_monitor(struct net_device *dev, u16 rxfid,
1247 struct hermes_rx_descriptor *desc)
1248{
1249 u32 hdrlen = 30; /* return full header by default */
1250 u32 datalen = 0;
1251 u16 fc;
1252 int err;
1253 int len;
1254 struct sk_buff *skb;
1255 struct orinoco_private *priv = netdev_priv(dev);
1256 struct net_device_stats *stats = &priv->stats;
1257 hermes_t *hw = &priv->hw;
1258
1259 len = le16_to_cpu(desc->data_len);
1260
1261 /* Determine the size of the header and the data */
1262 fc = le16_to_cpu(desc->frame_ctl);
1263 switch (fc & IEEE80211_FCTL_FTYPE) {
1264 case IEEE80211_FTYPE_DATA:
1265 if ((fc & IEEE80211_FCTL_TODS)
1266 && (fc & IEEE80211_FCTL_FROMDS))
1267 hdrlen = 30;
1268 else
1269 hdrlen = 24;
1270 datalen = len;
1271 break;
1272 case IEEE80211_FTYPE_MGMT:
1273 hdrlen = 24;
1274 datalen = len;
1275 break;
1276 case IEEE80211_FTYPE_CTL:
1277 switch (fc & IEEE80211_FCTL_STYPE) {
1278 case IEEE80211_STYPE_PSPOLL:
1279 case IEEE80211_STYPE_RTS:
1280 case IEEE80211_STYPE_CFEND:
1281 case IEEE80211_STYPE_CFENDACK:
1282 hdrlen = 16;
1283 break;
1284 case IEEE80211_STYPE_CTS:
1285 case IEEE80211_STYPE_ACK:
1286 hdrlen = 10;
1287 break;
1288 }
1289 break;
1290 default:
1291 /* Unknown frame type */
1292 break;
1293 }
1294
1295 /* sanity check the length */
Johannes Berg2c7060022008-10-30 22:09:54 +01001296 if (datalen > IEEE80211_MAX_DATA_LEN + 12) {
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001297 printk(KERN_DEBUG "%s: oversized monitor frame, "
1298 "data length = %d\n", dev->name, datalen);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001299 stats->rx_length_errors++;
1300 goto update_stats;
1301 }
1302
1303 skb = dev_alloc_skb(hdrlen + datalen);
1304 if (!skb) {
1305 printk(KERN_WARNING "%s: Cannot allocate skb for monitor frame\n",
1306 dev->name);
Florin Malitabb6e0932006-05-22 22:35:30 -07001307 goto update_stats;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001308 }
1309
1310 /* Copy the 802.11 header to the skb */
1311 memcpy(skb_put(skb, hdrlen), &(desc->frame_ctl), hdrlen);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001312 skb_reset_mac_header(skb);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001313
1314 /* If any, copy the data from the card to the skb */
1315 if (datalen > 0) {
1316 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, datalen),
1317 ALIGN(datalen, 2), rxfid,
1318 HERMES_802_2_OFFSET);
1319 if (err) {
1320 printk(KERN_ERR "%s: error %d reading monitor frame\n",
1321 dev->name, err);
1322 goto drop;
1323 }
1324 }
1325
1326 skb->dev = dev;
1327 skb->ip_summed = CHECKSUM_NONE;
1328 skb->pkt_type = PACKET_OTHERHOST;
1329 skb->protocol = __constant_htons(ETH_P_802_2);
1330
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001331 stats->rx_packets++;
1332 stats->rx_bytes += skb->len;
1333
1334 netif_rx(skb);
1335 return;
1336
1337 drop:
1338 dev_kfree_skb_irq(skb);
1339 update_stats:
1340 stats->rx_errors++;
1341 stats->rx_dropped++;
1342}
1343
David Kilroy23edcc42008-08-21 23:28:05 +01001344/* Get tsc from the firmware */
1345static int orinoco_hw_get_tkip_iv(struct orinoco_private *priv, int key,
1346 u8 *tsc)
1347{
1348 hermes_t *hw = &priv->hw;
1349 int err = 0;
1350 u8 tsc_arr[4][IW_ENCODE_SEQ_MAX_SIZE];
1351
1352 if ((key < 0) || (key > 4))
1353 return -EINVAL;
1354
1355 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_TKIP_IV,
1356 sizeof(tsc_arr), NULL, &tsc_arr);
1357 if (!err)
1358 memcpy(tsc, &tsc_arr[key][0], sizeof(tsc_arr[0]));
1359
1360 return err;
1361}
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363static void __orinoco_ev_rx(struct net_device *dev, hermes_t *hw)
1364{
1365 struct orinoco_private *priv = netdev_priv(dev);
1366 struct net_device_stats *stats = &priv->stats;
1367 struct iw_statistics *wstats = &priv->wstats;
1368 struct sk_buff *skb = NULL;
David Kilroy31afcef2008-08-21 23:28:04 +01001369 u16 rxfid, status;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001370 int length;
David Kilroy31afcef2008-08-21 23:28:04 +01001371 struct hermes_rx_descriptor *desc;
1372 struct orinoco_rx_data *rx_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 int err;
1374
David Kilroy31afcef2008-08-21 23:28:04 +01001375 desc = kmalloc(sizeof(*desc), GFP_ATOMIC);
1376 if (!desc) {
1377 printk(KERN_WARNING
1378 "%s: Can't allocate space for RX descriptor\n",
1379 dev->name);
1380 goto update_stats;
1381 }
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 rxfid = hermes_read_regn(hw, RXFID);
1384
David Kilroy31afcef2008-08-21 23:28:04 +01001385 err = hermes_bap_pread(hw, IRQ_BAP, desc, sizeof(*desc),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 rxfid, 0);
1387 if (err) {
1388 printk(KERN_ERR "%s: error %d reading Rx descriptor. "
1389 "Frame dropped.\n", dev->name, err);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001390 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
1392
David Kilroy31afcef2008-08-21 23:28:04 +01001393 status = le16_to_cpu(desc->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001395 if (status & HERMES_RXSTAT_BADCRC) {
1396 DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n",
1397 dev->name);
1398 stats->rx_crc_errors++;
1399 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
1401
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001402 /* Handle frames in monitor mode */
1403 if (priv->iw_mode == IW_MODE_MONITOR) {
David Kilroy31afcef2008-08-21 23:28:04 +01001404 orinoco_rx_monitor(dev, rxfid, desc);
1405 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001408 if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
1409 DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
1410 dev->name);
1411 wstats->discard.code++;
1412 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
1414
David Kilroy31afcef2008-08-21 23:28:04 +01001415 length = le16_to_cpu(desc->data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 /* Sanity checks */
1418 if (length < 3) { /* No for even an 802.2 LLC header */
1419 /* At least on Symbol firmware with PCF we get quite a
1420 lot of these legitimately - Poll frames with no
1421 data. */
David Kilroy31afcef2008-08-21 23:28:04 +01001422 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
Johannes Berg2c7060022008-10-30 22:09:54 +01001424 if (length > IEEE80211_MAX_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
1426 dev->name, length);
1427 stats->rx_length_errors++;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001428 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
1430
David Kilroy23edcc42008-08-21 23:28:05 +01001431 /* Payload size does not include Michael MIC. Increase payload
1432 * size to read it together with the data. */
1433 if (status & HERMES_RXSTAT_MIC)
1434 length += MICHAEL_MIC_LEN;
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 /* We need space for the packet data itself, plus an ethernet
1437 header, plus 2 bytes so we can align the IP header on a
1438 32bit boundary, plus 1 byte so we can read in odd length
1439 packets from the card, which has an IO granularity of 16
1440 bits */
1441 skb = dev_alloc_skb(length+ETH_HLEN+2+1);
1442 if (!skb) {
1443 printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
1444 dev->name);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001445 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
1447
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001448 /* We'll prepend the header, so reserve space for it. The worst
1449 case is no decapsulation, when 802.3 header is prepended and
1450 nothing is removed. 2 is for aligning the IP header. */
1451 skb_reserve(skb, ETH_HLEN + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001453 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, length),
1454 ALIGN(length, 2), rxfid,
1455 HERMES_802_2_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 if (err) {
1457 printk(KERN_ERR "%s: error %d reading frame. "
1458 "Frame dropped.\n", dev->name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 goto drop;
1460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
David Kilroy31afcef2008-08-21 23:28:04 +01001462 /* Add desc and skb to rx queue */
1463 rx_data = kzalloc(sizeof(*rx_data), GFP_ATOMIC);
1464 if (!rx_data) {
1465 printk(KERN_WARNING "%s: Can't allocate RX packet\n",
1466 dev->name);
1467 goto drop;
1468 }
1469 rx_data->desc = desc;
1470 rx_data->skb = skb;
1471 list_add_tail(&rx_data->list, &priv->rx_list);
1472 tasklet_schedule(&priv->rx_tasklet);
1473
1474 return;
1475
1476drop:
1477 dev_kfree_skb_irq(skb);
1478update_stats:
1479 stats->rx_errors++;
1480 stats->rx_dropped++;
1481out:
1482 kfree(desc);
1483}
1484
1485static void orinoco_rx(struct net_device *dev,
1486 struct hermes_rx_descriptor *desc,
1487 struct sk_buff *skb)
1488{
1489 struct orinoco_private *priv = netdev_priv(dev);
1490 struct net_device_stats *stats = &priv->stats;
1491 u16 status, fc;
1492 int length;
1493 struct ethhdr *hdr;
1494
1495 status = le16_to_cpu(desc->status);
1496 length = le16_to_cpu(desc->data_len);
1497 fc = le16_to_cpu(desc->frame_ctl);
1498
David Kilroy23edcc42008-08-21 23:28:05 +01001499 /* Calculate and check MIC */
1500 if (status & HERMES_RXSTAT_MIC) {
1501 int key_id = ((status & HERMES_RXSTAT_MIC_KEY_ID) >>
1502 HERMES_MIC_KEY_ID_SHIFT);
1503 u8 mic[MICHAEL_MIC_LEN];
1504 u8 *rxmic;
1505 u8 *src = (fc & IEEE80211_FCTL_FROMDS) ?
1506 desc->addr3 : desc->addr2;
1507
1508 /* Extract Michael MIC from payload */
1509 rxmic = skb->data + skb->len - MICHAEL_MIC_LEN;
1510
1511 skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
1512 length -= MICHAEL_MIC_LEN;
1513
1514 michael_mic(priv->rx_tfm_mic,
1515 priv->tkip_key[key_id].rx_mic,
1516 desc->addr1,
1517 src,
1518 0, /* priority or QoS? */
1519 skb->data,
1520 skb->len,
1521 &mic[0]);
1522
1523 if (memcmp(mic, rxmic,
1524 MICHAEL_MIC_LEN)) {
1525 union iwreq_data wrqu;
1526 struct iw_michaelmicfailure wxmic;
David Kilroy23edcc42008-08-21 23:28:05 +01001527
1528 printk(KERN_WARNING "%s: "
Johannes Berge1749612008-10-27 15:59:26 -07001529 "Invalid Michael MIC in data frame from %pM, "
David Kilroy23edcc42008-08-21 23:28:05 +01001530 "using key %i\n",
Johannes Berge1749612008-10-27 15:59:26 -07001531 dev->name, src, key_id);
David Kilroy23edcc42008-08-21 23:28:05 +01001532
1533 /* TODO: update stats */
1534
1535 /* Notify userspace */
1536 memset(&wxmic, 0, sizeof(wxmic));
1537 wxmic.flags = key_id & IW_MICFAILURE_KEY_ID;
1538 wxmic.flags |= (desc->addr1[0] & 1) ?
1539 IW_MICFAILURE_GROUP : IW_MICFAILURE_PAIRWISE;
1540 wxmic.src_addr.sa_family = ARPHRD_ETHER;
1541 memcpy(wxmic.src_addr.sa_data, src, ETH_ALEN);
1542
1543 (void) orinoco_hw_get_tkip_iv(priv, key_id,
1544 &wxmic.tsc[0]);
1545
1546 memset(&wrqu, 0, sizeof(wrqu));
1547 wrqu.data.length = sizeof(wxmic);
1548 wireless_send_event(dev, IWEVMICHAELMICFAILURE, &wrqu,
1549 (char *) &wxmic);
1550
1551 goto drop;
1552 }
1553 }
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 /* Handle decapsulation
1556 * In most cases, the firmware tell us about SNAP frames.
1557 * For some reason, the SNAP frames sent by LinkSys APs
1558 * are not properly recognised by most firmwares.
1559 * So, check ourselves */
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001560 if (length >= ENCAPS_OVERHEAD &&
1561 (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
1562 ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
1563 is_ethersnap(skb->data))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 /* These indicate a SNAP within 802.2 LLC within
1565 802.11 frame which we'll need to de-encapsulate to
1566 the original EthernetII frame. */
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001567 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN - ENCAPS_OVERHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 } else {
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001569 /* 802.3 frame - prepend 802.3 header as is */
1570 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN);
1571 hdr->h_proto = htons(length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 }
David Kilroy31afcef2008-08-21 23:28:04 +01001573 memcpy(hdr->h_dest, desc->addr1, ETH_ALEN);
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001574 if (fc & IEEE80211_FCTL_FROMDS)
David Kilroy31afcef2008-08-21 23:28:04 +01001575 memcpy(hdr->h_source, desc->addr3, ETH_ALEN);
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001576 else
David Kilroy31afcef2008-08-21 23:28:04 +01001577 memcpy(hdr->h_source, desc->addr2, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 skb->protocol = eth_type_trans(skb, dev);
1580 skb->ip_summed = CHECKSUM_NONE;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001581 if (fc & IEEE80211_FCTL_TODS)
1582 skb->pkt_type = PACKET_OTHERHOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 /* Process the wireless stats if needed */
David Kilroy31afcef2008-08-21 23:28:04 +01001585 orinoco_stat_gather(dev, skb, desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 /* Pass the packet to the networking stack */
1588 netif_rx(skb);
1589 stats->rx_packets++;
1590 stats->rx_bytes += length;
1591
1592 return;
David Kilroy23edcc42008-08-21 23:28:05 +01001593
1594 drop:
1595 dev_kfree_skb(skb);
1596 stats->rx_errors++;
1597 stats->rx_dropped++;
David Kilroy31afcef2008-08-21 23:28:04 +01001598}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
David Kilroy31afcef2008-08-21 23:28:04 +01001600static void orinoco_rx_isr_tasklet(unsigned long data)
1601{
1602 struct net_device *dev = (struct net_device *) data;
1603 struct orinoco_private *priv = netdev_priv(dev);
1604 struct orinoco_rx_data *rx_data, *temp;
1605 struct hermes_rx_descriptor *desc;
1606 struct sk_buff *skb;
1607
1608 /* extract desc and skb from queue */
1609 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
1610 desc = rx_data->desc;
1611 skb = rx_data->skb;
1612 list_del(&rx_data->list);
1613 kfree(rx_data);
1614
1615 orinoco_rx(dev, desc, skb);
1616
1617 kfree(desc);
1618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
1620
1621/********************************************************************/
1622/* Rx path (info frames) */
1623/********************************************************************/
1624
1625static void print_linkstatus(struct net_device *dev, u16 status)
1626{
1627 char * s;
1628
1629 if (suppress_linkstatus)
1630 return;
1631
1632 switch (status) {
1633 case HERMES_LINKSTATUS_NOT_CONNECTED:
1634 s = "Not Connected";
1635 break;
1636 case HERMES_LINKSTATUS_CONNECTED:
1637 s = "Connected";
1638 break;
1639 case HERMES_LINKSTATUS_DISCONNECTED:
1640 s = "Disconnected";
1641 break;
1642 case HERMES_LINKSTATUS_AP_CHANGE:
1643 s = "AP Changed";
1644 break;
1645 case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
1646 s = "AP Out of Range";
1647 break;
1648 case HERMES_LINKSTATUS_AP_IN_RANGE:
1649 s = "AP In Range";
1650 break;
1651 case HERMES_LINKSTATUS_ASSOC_FAILED:
1652 s = "Association Failed";
1653 break;
1654 default:
1655 s = "UNKNOWN";
1656 }
1657
1658 printk(KERN_INFO "%s: New link status: %s (%04x)\n",
1659 dev->name, s, status);
1660}
1661
Christoph Hellwig16739b02005-06-19 01:27:51 +02001662/* Search scan results for requested BSSID, join it if found */
David Howellsc4028952006-11-22 14:57:56 +00001663static void orinoco_join_ap(struct work_struct *work)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001664{
David Howellsc4028952006-11-22 14:57:56 +00001665 struct orinoco_private *priv =
1666 container_of(work, struct orinoco_private, join_work);
1667 struct net_device *dev = priv->ndev;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001668 struct hermes *hw = &priv->hw;
1669 int err;
1670 unsigned long flags;
1671 struct join_req {
1672 u8 bssid[ETH_ALEN];
Pavel Roskind133ae42005-09-23 04:18:06 -04001673 __le16 channel;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001674 } __attribute__ ((packed)) req;
1675 const int atom_len = offsetof(struct prism2_scan_apinfo, atim);
Pavel Roskinc89cc222005-09-01 20:06:06 -04001676 struct prism2_scan_apinfo *atom = NULL;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001677 int offset = 4;
Pavel Roskinc89cc222005-09-01 20:06:06 -04001678 int found = 0;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001679 u8 *buf;
1680 u16 len;
1681
1682 /* Allocate buffer for scan results */
1683 buf = kmalloc(MAX_SCAN_LEN, GFP_KERNEL);
1684 if (! buf)
1685 return;
1686
1687 if (orinoco_lock(priv, &flags) != 0)
Pavel Roskinf3cb4cc2005-09-23 04:18:06 -04001688 goto fail_lock;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001689
1690 /* Sanity checks in case user changed something in the meantime */
1691 if (! priv->bssid_fixed)
1692 goto out;
1693
1694 if (strlen(priv->desired_essid) == 0)
1695 goto out;
1696
1697 /* Read scan results from the firmware */
1698 err = hermes_read_ltv(hw, USER_BAP,
1699 HERMES_RID_SCANRESULTSTABLE,
1700 MAX_SCAN_LEN, &len, buf);
1701 if (err) {
1702 printk(KERN_ERR "%s: Cannot read scan results\n",
1703 dev->name);
1704 goto out;
1705 }
1706
1707 len = HERMES_RECLEN_TO_BYTES(len);
1708
1709 /* Go through the scan results looking for the channel of the AP
1710 * we were requested to join */
1711 for (; offset + atom_len <= len; offset += atom_len) {
1712 atom = (struct prism2_scan_apinfo *) (buf + offset);
Pavel Roskinc89cc222005-09-01 20:06:06 -04001713 if (memcmp(&atom->bssid, priv->desired_bssid, ETH_ALEN) == 0) {
1714 found = 1;
1715 break;
1716 }
Christoph Hellwig16739b02005-06-19 01:27:51 +02001717 }
1718
Pavel Roskinc89cc222005-09-01 20:06:06 -04001719 if (! found) {
1720 DEBUG(1, "%s: Requested AP not found in scan results\n",
1721 dev->name);
1722 goto out;
1723 }
Christoph Hellwig16739b02005-06-19 01:27:51 +02001724
Christoph Hellwig16739b02005-06-19 01:27:51 +02001725 memcpy(req.bssid, priv->desired_bssid, ETH_ALEN);
1726 req.channel = atom->channel; /* both are little-endian */
1727 err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNFJOINREQUEST,
1728 &req);
1729 if (err)
1730 printk(KERN_ERR "%s: Error issuing join request\n", dev->name);
1731
1732 out:
Christoph Hellwig16739b02005-06-19 01:27:51 +02001733 orinoco_unlock(priv, &flags);
Pavel Roskinf3cb4cc2005-09-23 04:18:06 -04001734
1735 fail_lock:
1736 kfree(buf);
Christoph Hellwig16739b02005-06-19 01:27:51 +02001737}
1738
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001739/* Send new BSSID to userspace */
David Kilroy6cd90b12008-08-21 23:28:00 +01001740static void orinoco_send_bssid_wevent(struct orinoco_private *priv)
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001741{
David Howellsc4028952006-11-22 14:57:56 +00001742 struct net_device *dev = priv->ndev;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001743 struct hermes *hw = &priv->hw;
1744 union iwreq_data wrqu;
1745 int err;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001746
1747 err = hermes_read_ltv(hw, IRQ_BAP, HERMES_RID_CURRENTBSSID,
1748 ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
1749 if (err != 0)
David Kilroy6cd90b12008-08-21 23:28:00 +01001750 return;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001751
1752 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1753
1754 /* Send event to user space */
1755 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001756}
1757
David Kilroy06009fd2008-08-21 23:28:03 +01001758static void orinoco_send_assocreqie_wevent(struct orinoco_private *priv)
1759{
1760 struct net_device *dev = priv->ndev;
1761 struct hermes *hw = &priv->hw;
1762 union iwreq_data wrqu;
1763 int err;
1764 u8 buf[88];
1765 u8 *ie;
1766
1767 if (!priv->has_wpa)
1768 return;
1769
1770 err = hermes_read_ltv(hw, IRQ_BAP, HERMES_RID_CURRENT_ASSOC_REQ_INFO,
1771 sizeof(buf), NULL, &buf);
1772 if (err != 0)
1773 return;
1774
1775 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1776 if (ie) {
1777 int rem = sizeof(buf) - (ie - &buf[0]);
1778 wrqu.data.length = ie[1] + 2;
1779 if (wrqu.data.length > rem)
1780 wrqu.data.length = rem;
1781
1782 if (wrqu.data.length)
1783 /* Send event to user space */
1784 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, ie);
1785 }
1786}
1787
1788static void orinoco_send_assocrespie_wevent(struct orinoco_private *priv)
1789{
1790 struct net_device *dev = priv->ndev;
1791 struct hermes *hw = &priv->hw;
1792 union iwreq_data wrqu;
1793 int err;
1794 u8 buf[88]; /* TODO: verify max size or IW_GENERIC_IE_MAX */
1795 u8 *ie;
1796
1797 if (!priv->has_wpa)
1798 return;
1799
1800 err = hermes_read_ltv(hw, IRQ_BAP, HERMES_RID_CURRENT_ASSOC_RESP_INFO,
1801 sizeof(buf), NULL, &buf);
1802 if (err != 0)
1803 return;
1804
1805 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1806 if (ie) {
1807 int rem = sizeof(buf) - (ie - &buf[0]);
1808 wrqu.data.length = ie[1] + 2;
1809 if (wrqu.data.length > rem)
1810 wrqu.data.length = rem;
1811
1812 if (wrqu.data.length)
1813 /* Send event to user space */
1814 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, ie);
1815 }
1816}
1817
David Kilroy6cd90b12008-08-21 23:28:00 +01001818static void orinoco_send_wevents(struct work_struct *work)
1819{
1820 struct orinoco_private *priv =
1821 container_of(work, struct orinoco_private, wevent_work);
1822 unsigned long flags;
1823
1824 if (orinoco_lock(priv, &flags) != 0)
1825 return;
1826
David Kilroy06009fd2008-08-21 23:28:03 +01001827 orinoco_send_assocreqie_wevent(priv);
1828 orinoco_send_assocrespie_wevent(priv);
David Kilroy6cd90b12008-08-21 23:28:00 +01001829 orinoco_send_bssid_wevent(priv);
1830
1831 orinoco_unlock(priv, &flags);
1832}
Dan Williams1e3428e2007-10-10 23:56:25 -04001833
1834static inline void orinoco_clear_scan_results(struct orinoco_private *priv,
1835 unsigned long scan_age)
1836{
David Kilroy01632fa2008-08-21 23:27:58 +01001837 if (priv->has_ext_scan) {
1838 struct xbss_element *bss;
1839 struct xbss_element *tmp_bss;
Dan Williams1e3428e2007-10-10 23:56:25 -04001840
David Kilroy01632fa2008-08-21 23:27:58 +01001841 /* Blow away current list of scan results */
1842 list_for_each_entry_safe(bss, tmp_bss, &priv->bss_list, list) {
1843 if (!scan_age ||
1844 time_after(jiffies, bss->last_scanned + scan_age)) {
1845 list_move_tail(&bss->list,
1846 &priv->bss_free_list);
1847 /* Don't blow away ->list, just BSS data */
1848 memset(&bss->bss, 0, sizeof(bss->bss));
1849 bss->last_scanned = 0;
1850 }
Dan Williams1e3428e2007-10-10 23:56:25 -04001851 }
David Kilroy01632fa2008-08-21 23:27:58 +01001852 } else {
1853 struct bss_element *bss;
1854 struct bss_element *tmp_bss;
1855
1856 /* Blow away current list of scan results */
1857 list_for_each_entry_safe(bss, tmp_bss, &priv->bss_list, list) {
1858 if (!scan_age ||
1859 time_after(jiffies, bss->last_scanned + scan_age)) {
1860 list_move_tail(&bss->list,
1861 &priv->bss_free_list);
1862 /* Don't blow away ->list, just BSS data */
1863 memset(&bss->bss, 0, sizeof(bss->bss));
1864 bss->last_scanned = 0;
1865 }
1866 }
1867 }
1868}
1869
1870static void orinoco_add_ext_scan_result(struct orinoco_private *priv,
1871 struct agere_ext_scan_info *atom)
1872{
1873 struct xbss_element *bss = NULL;
1874 int found = 0;
1875
1876 /* Try to update an existing bss first */
1877 list_for_each_entry(bss, &priv->bss_list, list) {
1878 if (compare_ether_addr(bss->bss.bssid, atom->bssid))
1879 continue;
1880 /* ESSID lengths */
1881 if (bss->bss.data[1] != atom->data[1])
1882 continue;
1883 if (memcmp(&bss->bss.data[2], &atom->data[2],
1884 atom->data[1]))
1885 continue;
1886 found = 1;
1887 break;
1888 }
1889
1890 /* Grab a bss off the free list */
1891 if (!found && !list_empty(&priv->bss_free_list)) {
1892 bss = list_entry(priv->bss_free_list.next,
1893 struct xbss_element, list);
1894 list_del(priv->bss_free_list.next);
1895
1896 list_add_tail(&bss->list, &priv->bss_list);
1897 }
1898
1899 if (bss) {
1900 /* Always update the BSS to get latest beacon info */
1901 memcpy(&bss->bss, atom, sizeof(bss->bss));
1902 bss->last_scanned = jiffies;
Dan Williams1e3428e2007-10-10 23:56:25 -04001903 }
1904}
1905
1906static int orinoco_process_scan_results(struct net_device *dev,
1907 unsigned char *buf,
1908 int len)
1909{
1910 struct orinoco_private *priv = netdev_priv(dev);
1911 int offset; /* In the scan data */
1912 union hermes_scan_info *atom;
1913 int atom_len;
1914
1915 switch (priv->firmware_type) {
1916 case FIRMWARE_TYPE_AGERE:
1917 atom_len = sizeof(struct agere_scan_apinfo);
1918 offset = 0;
1919 break;
1920 case FIRMWARE_TYPE_SYMBOL:
1921 /* Lack of documentation necessitates this hack.
1922 * Different firmwares have 68 or 76 byte long atoms.
1923 * We try modulo first. If the length divides by both,
1924 * we check what would be the channel in the second
1925 * frame for a 68-byte atom. 76-byte atoms have 0 there.
1926 * Valid channel cannot be 0. */
1927 if (len % 76)
1928 atom_len = 68;
1929 else if (len % 68)
1930 atom_len = 76;
1931 else if (len >= 1292 && buf[68] == 0)
1932 atom_len = 76;
1933 else
1934 atom_len = 68;
1935 offset = 0;
1936 break;
1937 case FIRMWARE_TYPE_INTERSIL:
1938 offset = 4;
1939 if (priv->has_hostscan) {
1940 atom_len = le16_to_cpup((__le16 *)buf);
1941 /* Sanity check for atom_len */
1942 if (atom_len < sizeof(struct prism2_scan_apinfo)) {
1943 printk(KERN_ERR "%s: Invalid atom_len in scan "
1944 "data: %d\n", dev->name, atom_len);
1945 return -EIO;
1946 }
1947 } else
1948 atom_len = offsetof(struct prism2_scan_apinfo, atim);
1949 break;
1950 default:
1951 return -EOPNOTSUPP;
1952 }
1953
1954 /* Check that we got an whole number of atoms */
1955 if ((len - offset) % atom_len) {
1956 printk(KERN_ERR "%s: Unexpected scan data length %d, "
1957 "atom_len %d, offset %d\n", dev->name, len,
1958 atom_len, offset);
1959 return -EIO;
1960 }
1961
1962 orinoco_clear_scan_results(priv, msecs_to_jiffies(15000));
1963
1964 /* Read the entries one by one */
1965 for (; offset + atom_len <= len; offset += atom_len) {
1966 int found = 0;
David Kilroy3056c402008-08-21 23:27:57 +01001967 struct bss_element *bss = NULL;
Dan Williams1e3428e2007-10-10 23:56:25 -04001968
1969 /* Get next atom */
1970 atom = (union hermes_scan_info *) (buf + offset);
1971
1972 /* Try to update an existing bss first */
1973 list_for_each_entry(bss, &priv->bss_list, list) {
1974 if (compare_ether_addr(bss->bss.a.bssid, atom->a.bssid))
1975 continue;
1976 if (le16_to_cpu(bss->bss.a.essid_len) !=
1977 le16_to_cpu(atom->a.essid_len))
1978 continue;
1979 if (memcmp(bss->bss.a.essid, atom->a.essid,
1980 le16_to_cpu(atom->a.essid_len)))
1981 continue;
Dan Williams1e3428e2007-10-10 23:56:25 -04001982 found = 1;
1983 break;
1984 }
1985
1986 /* Grab a bss off the free list */
1987 if (!found && !list_empty(&priv->bss_free_list)) {
1988 bss = list_entry(priv->bss_free_list.next,
David Kilroy3056c402008-08-21 23:27:57 +01001989 struct bss_element, list);
Dan Williams1e3428e2007-10-10 23:56:25 -04001990 list_del(priv->bss_free_list.next);
1991
Dan Williams1e3428e2007-10-10 23:56:25 -04001992 list_add_tail(&bss->list, &priv->bss_list);
1993 }
Dan Williams22367612007-12-05 11:01:23 -05001994
1995 if (bss) {
1996 /* Always update the BSS to get latest beacon info */
1997 memcpy(&bss->bss, atom, sizeof(bss->bss));
1998 bss->last_scanned = jiffies;
1999 }
Dan Williams1e3428e2007-10-10 23:56:25 -04002000 }
2001
2002 return 0;
2003}
2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005static void __orinoco_ev_info(struct net_device *dev, hermes_t *hw)
2006{
2007 struct orinoco_private *priv = netdev_priv(dev);
2008 u16 infofid;
2009 struct {
Pavel Roskind133ae42005-09-23 04:18:06 -04002010 __le16 len;
2011 __le16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 } __attribute__ ((packed)) info;
2013 int len, type;
2014 int err;
2015
2016 /* This is an answer to an INQUIRE command that we did earlier,
2017 * or an information "event" generated by the card
2018 * The controller return to us a pseudo frame containing
2019 * the information in question - Jean II */
2020 infofid = hermes_read_regn(hw, INFOFID);
2021
2022 /* Read the info frame header - don't try too hard */
2023 err = hermes_bap_pread(hw, IRQ_BAP, &info, sizeof(info),
2024 infofid, 0);
2025 if (err) {
2026 printk(KERN_ERR "%s: error %d reading info frame. "
2027 "Frame dropped.\n", dev->name, err);
2028 return;
2029 }
2030
2031 len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
2032 type = le16_to_cpu(info.type);
2033
2034 switch (type) {
2035 case HERMES_INQ_TALLIES: {
2036 struct hermes_tallies_frame tallies;
2037 struct iw_statistics *wstats = &priv->wstats;
2038
2039 if (len > sizeof(tallies)) {
2040 printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
2041 dev->name, len);
2042 len = sizeof(tallies);
2043 }
2044
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02002045 err = hermes_bap_pread(hw, IRQ_BAP, &tallies, len,
2046 infofid, sizeof(info));
2047 if (err)
2048 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
2050 /* Increment our various counters */
2051 /* wstats->discard.nwid - no wrong BSSID stuff */
2052 wstats->discard.code +=
2053 le16_to_cpu(tallies.RxWEPUndecryptable);
2054 if (len == sizeof(tallies))
2055 wstats->discard.code +=
2056 le16_to_cpu(tallies.RxDiscards_WEPICVError) +
2057 le16_to_cpu(tallies.RxDiscards_WEPExcluded);
2058 wstats->discard.misc +=
2059 le16_to_cpu(tallies.TxDiscardsWrongSA);
2060 wstats->discard.fragment +=
2061 le16_to_cpu(tallies.RxMsgInBadMsgFragments);
2062 wstats->discard.retries +=
2063 le16_to_cpu(tallies.TxRetryLimitExceeded);
2064 /* wstats->miss.beacon - no match */
2065 }
2066 break;
2067 case HERMES_INQ_LINKSTATUS: {
2068 struct hermes_linkstatus linkstatus;
2069 u16 newstatus;
2070 int connected;
2071
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02002072 if (priv->iw_mode == IW_MODE_MONITOR)
2073 break;
2074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 if (len != sizeof(linkstatus)) {
2076 printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
2077 dev->name, len);
2078 break;
2079 }
2080
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02002081 err = hermes_bap_pread(hw, IRQ_BAP, &linkstatus, len,
2082 infofid, sizeof(info));
2083 if (err)
2084 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 newstatus = le16_to_cpu(linkstatus.linkstatus);
2086
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002087 /* Symbol firmware uses "out of range" to signal that
2088 * the hostscan frame can be requested. */
2089 if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
2090 priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
2091 priv->has_hostscan && priv->scan_inprogress) {
2092 hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
2093 break;
2094 }
2095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
2097 || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
2098 || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
2099
2100 if (connected)
2101 netif_carrier_on(dev);
David Gibson7bb7c3a2005-05-12 20:02:10 -04002102 else if (!ignore_disconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 netif_carrier_off(dev);
2104
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002105 if (newstatus != priv->last_linkstatus) {
2106 priv->last_linkstatus = newstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 print_linkstatus(dev, newstatus);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002108 /* The info frame contains only one word which is the
2109 * status (see hermes.h). The status is pretty boring
2110 * in itself, that's why we export the new BSSID...
2111 * Jean II */
2112 schedule_work(&priv->wevent_work);
2113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 }
2115 break;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002116 case HERMES_INQ_SCAN:
2117 if (!priv->scan_inprogress && priv->bssid_fixed &&
2118 priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
2119 schedule_work(&priv->join_work);
2120 break;
2121 }
2122 /* fall through */
2123 case HERMES_INQ_HOSTSCAN:
2124 case HERMES_INQ_HOSTSCAN_SYMBOL: {
2125 /* Result of a scanning. Contains information about
2126 * cells in the vicinity - Jean II */
2127 union iwreq_data wrqu;
2128 unsigned char *buf;
2129
Dan Williams1e3428e2007-10-10 23:56:25 -04002130 /* Scan is no longer in progress */
2131 priv->scan_inprogress = 0;
2132
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002133 /* Sanity check */
2134 if (len > 4096) {
2135 printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
2136 dev->name, len);
2137 break;
2138 }
2139
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002140 /* Allocate buffer for results */
2141 buf = kmalloc(len, GFP_ATOMIC);
2142 if (buf == NULL)
2143 /* No memory, so can't printk()... */
2144 break;
2145
2146 /* Read scan data */
2147 err = hermes_bap_pread(hw, IRQ_BAP, (void *) buf, len,
2148 infofid, sizeof(info));
Pavel Roskin708218b2005-09-01 20:05:19 -04002149 if (err) {
2150 kfree(buf);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002151 break;
Pavel Roskin708218b2005-09-01 20:05:19 -04002152 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002153
2154#ifdef ORINOCO_DEBUG
2155 {
2156 int i;
2157 printk(KERN_DEBUG "Scan result [%02X", buf[0]);
2158 for(i = 1; i < (len * 2); i++)
2159 printk(":%02X", buf[i]);
2160 printk("]\n");
2161 }
2162#endif /* ORINOCO_DEBUG */
2163
Dan Williams1e3428e2007-10-10 23:56:25 -04002164 if (orinoco_process_scan_results(dev, buf, len) == 0) {
2165 /* Send an empty event to user space.
2166 * We don't send the received data on the event because
2167 * it would require us to do complex transcoding, and
2168 * we want to minimise the work done in the irq handler
2169 * Use a request to extract the data - Jean II */
2170 wrqu.data.length = 0;
2171 wrqu.data.flags = 0;
2172 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
2173 }
2174 kfree(buf);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002175 }
2176 break;
David Kilroy01632fa2008-08-21 23:27:58 +01002177 case HERMES_INQ_CHANNELINFO:
2178 {
2179 struct agere_ext_scan_info *bss;
2180
2181 if (!priv->scan_inprogress) {
2182 printk(KERN_DEBUG "%s: Got chaninfo without scan, "
2183 "len=%d\n", dev->name, len);
2184 break;
2185 }
2186
2187 /* An empty result indicates that the scan is complete */
2188 if (len == 0) {
2189 union iwreq_data wrqu;
2190
2191 /* Scan is no longer in progress */
2192 priv->scan_inprogress = 0;
2193
2194 wrqu.data.length = 0;
2195 wrqu.data.flags = 0;
2196 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
2197 break;
2198 }
2199
2200 /* Sanity check */
2201 else if (len > sizeof(*bss)) {
2202 printk(KERN_WARNING
2203 "%s: Ext scan results too large (%d bytes). "
2204 "Truncating results to %zd bytes.\n",
2205 dev->name, len, sizeof(*bss));
2206 len = sizeof(*bss);
2207 } else if (len < (offsetof(struct agere_ext_scan_info,
2208 data) + 2)) {
2209 /* Drop this result now so we don't have to
2210 * keep checking later */
2211 printk(KERN_WARNING
2212 "%s: Ext scan results too short (%d bytes)\n",
2213 dev->name, len);
2214 break;
2215 }
2216
2217 bss = kmalloc(sizeof(*bss), GFP_ATOMIC);
2218 if (bss == NULL)
2219 break;
2220
2221 /* Read scan data */
2222 err = hermes_bap_pread(hw, IRQ_BAP, (void *) bss, len,
2223 infofid, sizeof(info));
2224 if (err) {
2225 kfree(bss);
2226 break;
2227 }
2228
2229 orinoco_add_ext_scan_result(priv, bss);
2230
2231 kfree(bss);
2232 break;
2233 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002234 case HERMES_INQ_SEC_STAT_AGERE:
2235 /* Security status (Agere specific) */
2236 /* Ignore this frame for now */
2237 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
2238 break;
2239 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 default:
2241 printk(KERN_DEBUG "%s: Unknown information frame received: "
2242 "type 0x%04x, length %d\n", dev->name, type, len);
2243 /* We don't actually do anything about it */
2244 break;
2245 }
2246}
2247
2248static void __orinoco_ev_infdrop(struct net_device *dev, hermes_t *hw)
2249{
2250 if (net_ratelimit())
2251 printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
2252}
2253
2254/********************************************************************/
2255/* Internal hardware control routines */
2256/********************************************************************/
2257
2258int __orinoco_up(struct net_device *dev)
2259{
2260 struct orinoco_private *priv = netdev_priv(dev);
2261 struct hermes *hw = &priv->hw;
2262 int err;
2263
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02002264 netif_carrier_off(dev); /* just to make sure */
2265
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 err = __orinoco_program_rids(dev);
2267 if (err) {
2268 printk(KERN_ERR "%s: Error %d configuring card\n",
2269 dev->name, err);
2270 return err;
2271 }
2272
2273 /* Fire things up again */
2274 hermes_set_irqmask(hw, ORINOCO_INTEN);
2275 err = hermes_enable_port(hw, 0);
2276 if (err) {
2277 printk(KERN_ERR "%s: Error %d enabling MAC port\n",
2278 dev->name, err);
2279 return err;
2280 }
2281
2282 netif_start_queue(dev);
2283
2284 return 0;
2285}
2286
2287int __orinoco_down(struct net_device *dev)
2288{
2289 struct orinoco_private *priv = netdev_priv(dev);
2290 struct hermes *hw = &priv->hw;
2291 int err;
2292
2293 netif_stop_queue(dev);
2294
2295 if (! priv->hw_unavailable) {
2296 if (! priv->broken_disableport) {
2297 err = hermes_disable_port(hw, 0);
2298 if (err) {
2299 /* Some firmwares (e.g. Intersil 1.3.x) seem
2300 * to have problems disabling the port, oh
2301 * well, too bad. */
2302 printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
2303 dev->name, err);
2304 priv->broken_disableport = 1;
2305 }
2306 }
2307 hermes_set_irqmask(hw, 0);
2308 hermes_write_regn(hw, EVACK, 0xffff);
2309 }
2310
2311 /* firmware will have to reassociate */
2312 netif_carrier_off(dev);
2313 priv->last_linkstatus = 0xffff;
2314
2315 return 0;
2316}
2317
Pavel Roskin37a6c612006-04-07 04:10:49 -04002318static int orinoco_allocate_fid(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319{
2320 struct orinoco_private *priv = netdev_priv(dev);
2321 struct hermes *hw = &priv->hw;
2322 int err;
2323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 err = hermes_allocate(hw, priv->nicbuf_size, &priv->txfid);
David Gibsonb24d4582005-05-12 20:04:16 -04002325 if (err == -EIO && priv->nicbuf_size > TX_NICBUF_SIZE_BUG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 /* Try workaround for old Symbol firmware bug */
2327 printk(KERN_WARNING "%s: firmware ALLOC bug detected "
2328 "(old Symbol firmware?). Trying to work around... ",
2329 dev->name);
2330
2331 priv->nicbuf_size = TX_NICBUF_SIZE_BUG;
2332 err = hermes_allocate(hw, priv->nicbuf_size, &priv->txfid);
2333 if (err)
2334 printk("failed!\n");
2335 else
2336 printk("ok.\n");
2337 }
2338
2339 return err;
2340}
2341
Pavel Roskin37a6c612006-04-07 04:10:49 -04002342int orinoco_reinit_firmware(struct net_device *dev)
2343{
2344 struct orinoco_private *priv = netdev_priv(dev);
2345 struct hermes *hw = &priv->hw;
2346 int err;
2347
2348 err = hermes_init(hw);
Andrey Borzenkov0df6cbb2008-10-12 20:15:43 +04002349 if (priv->do_fw_download && !err) {
2350 err = orinoco_download(priv);
2351 if (err)
2352 priv->do_fw_download = 0;
2353 }
Pavel Roskin37a6c612006-04-07 04:10:49 -04002354 if (!err)
2355 err = orinoco_allocate_fid(dev);
2356
2357 return err;
2358}
2359
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360static int __orinoco_hw_set_bitrate(struct orinoco_private *priv)
2361{
2362 hermes_t *hw = &priv->hw;
2363 int err = 0;
2364
2365 if (priv->bitratemode >= BITRATE_TABLE_SIZE) {
2366 printk(KERN_ERR "%s: BUG: Invalid bitrate mode %d\n",
2367 priv->ndev->name, priv->bitratemode);
2368 return -EINVAL;
2369 }
2370
2371 switch (priv->firmware_type) {
2372 case FIRMWARE_TYPE_AGERE:
2373 err = hermes_write_wordrec(hw, USER_BAP,
2374 HERMES_RID_CNFTXRATECONTROL,
2375 bitrate_table[priv->bitratemode].agere_txratectrl);
2376 break;
2377 case FIRMWARE_TYPE_INTERSIL:
2378 case FIRMWARE_TYPE_SYMBOL:
2379 err = hermes_write_wordrec(hw, USER_BAP,
2380 HERMES_RID_CNFTXRATECONTROL,
2381 bitrate_table[priv->bitratemode].intersil_txratectrl);
2382 break;
2383 default:
2384 BUG();
2385 }
2386
2387 return err;
2388}
2389
Christoph Hellwig16739b02005-06-19 01:27:51 +02002390/* Set fixed AP address */
2391static int __orinoco_hw_set_wap(struct orinoco_private *priv)
2392{
2393 int roaming_flag;
2394 int err = 0;
2395 hermes_t *hw = &priv->hw;
2396
2397 switch (priv->firmware_type) {
2398 case FIRMWARE_TYPE_AGERE:
2399 /* not supported */
2400 break;
2401 case FIRMWARE_TYPE_INTERSIL:
2402 if (priv->bssid_fixed)
2403 roaming_flag = 2;
2404 else
2405 roaming_flag = 1;
2406
2407 err = hermes_write_wordrec(hw, USER_BAP,
2408 HERMES_RID_CNFROAMINGMODE,
2409 roaming_flag);
2410 break;
2411 case FIRMWARE_TYPE_SYMBOL:
2412 err = HERMES_WRITE_RECORD(hw, USER_BAP,
2413 HERMES_RID_CNFMANDATORYBSSID_SYMBOL,
2414 &priv->desired_bssid);
2415 break;
2416 }
2417 return err;
2418}
2419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420/* Change the WEP keys and/or the current keys. Can be called
David Kilroyd03032a2008-08-21 23:28:02 +01002421 * either from __orinoco_hw_setup_enc() or directly from
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 * orinoco_ioctl_setiwencode(). In the later case the association
2423 * with the AP is not broken (if the firmware can handle it),
2424 * which is needed for 802.1x implementations. */
2425static int __orinoco_hw_setup_wepkeys(struct orinoco_private *priv)
2426{
2427 hermes_t *hw = &priv->hw;
2428 int err = 0;
2429
2430 switch (priv->firmware_type) {
2431 case FIRMWARE_TYPE_AGERE:
2432 err = HERMES_WRITE_RECORD(hw, USER_BAP,
2433 HERMES_RID_CNFWEPKEYS_AGERE,
2434 &priv->keys);
2435 if (err)
2436 return err;
2437 err = hermes_write_wordrec(hw, USER_BAP,
2438 HERMES_RID_CNFTXKEY_AGERE,
2439 priv->tx_key);
2440 if (err)
2441 return err;
2442 break;
2443 case FIRMWARE_TYPE_INTERSIL:
2444 case FIRMWARE_TYPE_SYMBOL:
2445 {
2446 int keylen;
2447 int i;
2448
2449 /* Force uniform key length to work around firmware bugs */
2450 keylen = le16_to_cpu(priv->keys[priv->tx_key].len);
2451
2452 if (keylen > LARGE_KEY_SIZE) {
2453 printk(KERN_ERR "%s: BUG: Key %d has oversize length %d.\n",
2454 priv->ndev->name, priv->tx_key, keylen);
2455 return -E2BIG;
2456 }
2457
2458 /* Write all 4 keys */
2459 for(i = 0; i < ORINOCO_MAX_KEYS; i++) {
2460 err = hermes_write_ltv(hw, USER_BAP,
2461 HERMES_RID_CNFDEFAULTKEY0 + i,
2462 HERMES_BYTES_TO_RECLEN(keylen),
2463 priv->keys[i].data);
2464 if (err)
2465 return err;
2466 }
2467
2468 /* Write the index of the key used in transmission */
2469 err = hermes_write_wordrec(hw, USER_BAP,
2470 HERMES_RID_CNFWEPDEFAULTKEYID,
2471 priv->tx_key);
2472 if (err)
2473 return err;
2474 }
2475 break;
2476 }
2477
2478 return 0;
2479}
2480
David Kilroyd03032a2008-08-21 23:28:02 +01002481static int __orinoco_hw_setup_enc(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482{
2483 hermes_t *hw = &priv->hw;
2484 int err = 0;
2485 int master_wep_flag;
2486 int auth_flag;
David Kilroy4ae6ee22008-08-21 23:27:59 +01002487 int enc_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
David Kilroyd03032a2008-08-21 23:28:02 +01002489 /* Setup WEP keys for WEP and WPA */
2490 if (priv->encode_alg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 __orinoco_hw_setup_wepkeys(priv);
2492
2493 if (priv->wep_restrict)
2494 auth_flag = HERMES_AUTH_SHARED_KEY;
2495 else
2496 auth_flag = HERMES_AUTH_OPEN;
2497
David Kilroyd03032a2008-08-21 23:28:02 +01002498 if (priv->wpa_enabled)
2499 enc_flag = 2;
2500 else if (priv->encode_alg == IW_ENCODE_ALG_WEP)
David Kilroy4ae6ee22008-08-21 23:27:59 +01002501 enc_flag = 1;
2502 else
2503 enc_flag = 0;
2504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 switch (priv->firmware_type) {
2506 case FIRMWARE_TYPE_AGERE: /* Agere style WEP */
David Kilroy4ae6ee22008-08-21 23:27:59 +01002507 if (priv->encode_alg == IW_ENCODE_ALG_WEP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 /* Enable the shared-key authentication. */
2509 err = hermes_write_wordrec(hw, USER_BAP,
2510 HERMES_RID_CNFAUTHENTICATION_AGERE,
2511 auth_flag);
2512 }
2513 err = hermes_write_wordrec(hw, USER_BAP,
2514 HERMES_RID_CNFWEPENABLED_AGERE,
David Kilroy4ae6ee22008-08-21 23:27:59 +01002515 enc_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 if (err)
2517 return err;
David Kilroyd03032a2008-08-21 23:28:02 +01002518
2519 if (priv->has_wpa) {
2520 /* Set WPA key management */
2521 err = hermes_write_wordrec(hw, USER_BAP,
2522 HERMES_RID_CNFSETWPAAUTHMGMTSUITE_AGERE,
2523 priv->key_mgmt);
2524 if (err)
2525 return err;
2526 }
2527
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 break;
2529
2530 case FIRMWARE_TYPE_INTERSIL: /* Intersil style WEP */
2531 case FIRMWARE_TYPE_SYMBOL: /* Symbol style WEP */
David Kilroy4ae6ee22008-08-21 23:27:59 +01002532 if (priv->encode_alg == IW_ENCODE_ALG_WEP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 if (priv->wep_restrict ||
2534 (priv->firmware_type == FIRMWARE_TYPE_SYMBOL))
2535 master_wep_flag = HERMES_WEP_PRIVACY_INVOKED |
2536 HERMES_WEP_EXCL_UNENCRYPTED;
2537 else
2538 master_wep_flag = HERMES_WEP_PRIVACY_INVOKED;
2539
2540 err = hermes_write_wordrec(hw, USER_BAP,
2541 HERMES_RID_CNFAUTHENTICATION,
2542 auth_flag);
2543 if (err)
2544 return err;
2545 } else
2546 master_wep_flag = 0;
2547
2548 if (priv->iw_mode == IW_MODE_MONITOR)
2549 master_wep_flag |= HERMES_WEP_HOST_DECRYPT;
2550
2551 /* Master WEP setting : on/off */
2552 err = hermes_write_wordrec(hw, USER_BAP,
2553 HERMES_RID_CNFWEPFLAGS_INTERSIL,
2554 master_wep_flag);
2555 if (err)
2556 return err;
2557
2558 break;
2559 }
2560
2561 return 0;
2562}
2563
David Kilroyd03032a2008-08-21 23:28:02 +01002564/* key must be 32 bytes, including the tx and rx MIC keys.
2565 * rsc must be 8 bytes
2566 * tsc must be 8 bytes or NULL
2567 */
2568static int __orinoco_hw_set_tkip_key(hermes_t *hw, int key_idx, int set_tx,
2569 u8 *key, u8 *rsc, u8 *tsc)
2570{
2571 struct {
2572 __le16 idx;
2573 u8 rsc[IW_ENCODE_SEQ_MAX_SIZE];
2574 u8 key[TKIP_KEYLEN];
2575 u8 tx_mic[MIC_KEYLEN];
2576 u8 rx_mic[MIC_KEYLEN];
2577 u8 tsc[IW_ENCODE_SEQ_MAX_SIZE];
2578 } __attribute__ ((packed)) buf;
2579 int ret;
2580 int err;
2581 int k;
2582 u16 xmitting;
2583
2584 key_idx &= 0x3;
2585
2586 if (set_tx)
2587 key_idx |= 0x8000;
2588
2589 buf.idx = cpu_to_le16(key_idx);
2590 memcpy(buf.key, key,
2591 sizeof(buf.key) + sizeof(buf.tx_mic) + sizeof(buf.rx_mic));
2592
2593 if (rsc == NULL)
2594 memset(buf.rsc, 0, sizeof(buf.rsc));
2595 else
2596 memcpy(buf.rsc, rsc, sizeof(buf.rsc));
2597
2598 if (tsc == NULL) {
2599 memset(buf.tsc, 0, sizeof(buf.tsc));
2600 buf.tsc[4] = 0x10;
2601 } else {
2602 memcpy(buf.tsc, tsc, sizeof(buf.tsc));
2603 }
2604
2605 /* Wait upto 100ms for tx queue to empty */
2606 k = 100;
2607 do {
2608 k--;
2609 udelay(1000);
2610 ret = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_TXQUEUEEMPTY,
2611 &xmitting);
2612 if (ret)
2613 break;
2614 } while ((k > 0) && xmitting);
2615
2616 if (k == 0)
2617 ret = -ETIMEDOUT;
2618
2619 err = HERMES_WRITE_RECORD(hw, USER_BAP,
2620 HERMES_RID_CNFADDDEFAULTTKIPKEY_AGERE,
2621 &buf);
2622
2623 return ret ? ret : err;
2624}
2625
2626static int orinoco_clear_tkip_key(struct orinoco_private *priv,
2627 int key_idx)
2628{
2629 hermes_t *hw = &priv->hw;
2630 int err;
2631
2632 memset(&priv->tkip_key[key_idx], 0, sizeof(priv->tkip_key[key_idx]));
2633 err = hermes_write_wordrec(hw, USER_BAP,
2634 HERMES_RID_CNFREMDEFAULTTKIPKEY_AGERE,
2635 key_idx);
2636 if (err)
2637 printk(KERN_WARNING "%s: Error %d clearing TKIP key %d\n",
2638 priv->ndev->name, err, key_idx);
2639 return err;
2640}
2641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642static int __orinoco_program_rids(struct net_device *dev)
2643{
2644 struct orinoco_private *priv = netdev_priv(dev);
2645 hermes_t *hw = &priv->hw;
2646 int err;
2647 struct hermes_idstring idbuf;
2648
2649 /* Set the MAC address */
2650 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
2651 HERMES_BYTES_TO_RECLEN(ETH_ALEN), dev->dev_addr);
2652 if (err) {
2653 printk(KERN_ERR "%s: Error %d setting MAC address\n",
2654 dev->name, err);
2655 return err;
2656 }
2657
2658 /* Set up the link mode */
2659 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFPORTTYPE,
2660 priv->port_type);
2661 if (err) {
2662 printk(KERN_ERR "%s: Error %d setting port type\n",
2663 dev->name, err);
2664 return err;
2665 }
2666 /* Set the channel/frequency */
David Gibsond51d8b12005-05-12 20:03:36 -04002667 if (priv->channel != 0 && priv->iw_mode != IW_MODE_INFRA) {
2668 err = hermes_write_wordrec(hw, USER_BAP,
2669 HERMES_RID_CNFOWNCHANNEL,
2670 priv->channel);
2671 if (err) {
2672 printk(KERN_ERR "%s: Error %d setting channel %d\n",
2673 dev->name, err, priv->channel);
2674 return err;
2675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 }
2677
2678 if (priv->has_ibss) {
2679 u16 createibss;
2680
2681 if ((strlen(priv->desired_essid) == 0) && (priv->createibss)) {
2682 printk(KERN_WARNING "%s: This firmware requires an "
2683 "ESSID in IBSS-Ad-Hoc mode.\n", dev->name);
2684 /* With wvlan_cs, in this case, we would crash.
2685 * hopefully, this driver will behave better...
2686 * Jean II */
2687 createibss = 0;
2688 } else {
2689 createibss = priv->createibss;
2690 }
2691
2692 err = hermes_write_wordrec(hw, USER_BAP,
2693 HERMES_RID_CNFCREATEIBSS,
2694 createibss);
2695 if (err) {
2696 printk(KERN_ERR "%s: Error %d setting CREATEIBSS\n",
2697 dev->name, err);
2698 return err;
2699 }
2700 }
2701
Christoph Hellwig16739b02005-06-19 01:27:51 +02002702 /* Set the desired BSSID */
2703 err = __orinoco_hw_set_wap(priv);
2704 if (err) {
2705 printk(KERN_ERR "%s: Error %d setting AP address\n",
2706 dev->name, err);
2707 return err;
2708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 /* Set the desired ESSID */
2710 idbuf.len = cpu_to_le16(strlen(priv->desired_essid));
2711 memcpy(&idbuf.val, priv->desired_essid, sizeof(idbuf.val));
2712 /* WinXP wants partner to configure OWNSSID even in IBSS mode. (jimc) */
2713 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNSSID,
2714 HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
2715 &idbuf);
2716 if (err) {
2717 printk(KERN_ERR "%s: Error %d setting OWNSSID\n",
2718 dev->name, err);
2719 return err;
2720 }
2721 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFDESIREDSSID,
2722 HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
2723 &idbuf);
2724 if (err) {
2725 printk(KERN_ERR "%s: Error %d setting DESIREDSSID\n",
2726 dev->name, err);
2727 return err;
2728 }
2729
2730 /* Set the station name */
2731 idbuf.len = cpu_to_le16(strlen(priv->nick));
2732 memcpy(&idbuf.val, priv->nick, sizeof(idbuf.val));
2733 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
2734 HERMES_BYTES_TO_RECLEN(strlen(priv->nick)+2),
2735 &idbuf);
2736 if (err) {
2737 printk(KERN_ERR "%s: Error %d setting nickname\n",
2738 dev->name, err);
2739 return err;
2740 }
2741
2742 /* Set AP density */
2743 if (priv->has_sensitivity) {
2744 err = hermes_write_wordrec(hw, USER_BAP,
2745 HERMES_RID_CNFSYSTEMSCALE,
2746 priv->ap_density);
2747 if (err) {
2748 printk(KERN_WARNING "%s: Error %d setting SYSTEMSCALE. "
2749 "Disabling sensitivity control\n",
2750 dev->name, err);
2751
2752 priv->has_sensitivity = 0;
2753 }
2754 }
2755
2756 /* Set RTS threshold */
2757 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
2758 priv->rts_thresh);
2759 if (err) {
2760 printk(KERN_ERR "%s: Error %d setting RTS threshold\n",
2761 dev->name, err);
2762 return err;
2763 }
2764
2765 /* Set fragmentation threshold or MWO robustness */
2766 if (priv->has_mwo)
2767 err = hermes_write_wordrec(hw, USER_BAP,
2768 HERMES_RID_CNFMWOROBUST_AGERE,
2769 priv->mwo_robust);
2770 else
2771 err = hermes_write_wordrec(hw, USER_BAP,
2772 HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
2773 priv->frag_thresh);
2774 if (err) {
2775 printk(KERN_ERR "%s: Error %d setting fragmentation\n",
2776 dev->name, err);
2777 return err;
2778 }
2779
2780 /* Set bitrate */
2781 err = __orinoco_hw_set_bitrate(priv);
2782 if (err) {
2783 printk(KERN_ERR "%s: Error %d setting bitrate\n",
2784 dev->name, err);
2785 return err;
2786 }
2787
2788 /* Set power management */
2789 if (priv->has_pm) {
2790 err = hermes_write_wordrec(hw, USER_BAP,
2791 HERMES_RID_CNFPMENABLED,
2792 priv->pm_on);
2793 if (err) {
2794 printk(KERN_ERR "%s: Error %d setting up PM\n",
2795 dev->name, err);
2796 return err;
2797 }
2798
2799 err = hermes_write_wordrec(hw, USER_BAP,
2800 HERMES_RID_CNFMULTICASTRECEIVE,
2801 priv->pm_mcast);
2802 if (err) {
2803 printk(KERN_ERR "%s: Error %d setting up PM\n",
2804 dev->name, err);
2805 return err;
2806 }
2807 err = hermes_write_wordrec(hw, USER_BAP,
2808 HERMES_RID_CNFMAXSLEEPDURATION,
2809 priv->pm_period);
2810 if (err) {
2811 printk(KERN_ERR "%s: Error %d setting up PM\n",
2812 dev->name, err);
2813 return err;
2814 }
2815 err = hermes_write_wordrec(hw, USER_BAP,
2816 HERMES_RID_CNFPMHOLDOVERDURATION,
2817 priv->pm_timeout);
2818 if (err) {
2819 printk(KERN_ERR "%s: Error %d setting up PM\n",
2820 dev->name, err);
2821 return err;
2822 }
2823 }
2824
2825 /* Set preamble - only for Symbol so far... */
2826 if (priv->has_preamble) {
2827 err = hermes_write_wordrec(hw, USER_BAP,
2828 HERMES_RID_CNFPREAMBLE_SYMBOL,
2829 priv->preamble);
2830 if (err) {
2831 printk(KERN_ERR "%s: Error %d setting preamble\n",
2832 dev->name, err);
2833 return err;
2834 }
2835 }
2836
2837 /* Set up encryption */
David Kilroyd03032a2008-08-21 23:28:02 +01002838 if (priv->has_wep || priv->has_wpa) {
2839 err = __orinoco_hw_setup_enc(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 if (err) {
David Kilroyd03032a2008-08-21 23:28:02 +01002841 printk(KERN_ERR "%s: Error %d activating encryption\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 dev->name, err);
2843 return err;
2844 }
2845 }
2846
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02002847 if (priv->iw_mode == IW_MODE_MONITOR) {
2848 /* Enable monitor mode */
2849 dev->type = ARPHRD_IEEE80211;
2850 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
2851 HERMES_TEST_MONITOR, 0, NULL);
2852 } else {
2853 /* Disable monitor mode */
2854 dev->type = ARPHRD_ETHER;
2855 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
2856 HERMES_TEST_STOP, 0, NULL);
2857 }
2858 if (err)
2859 return err;
2860
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 /* Set promiscuity / multicast*/
2862 priv->promiscuous = 0;
2863 priv->mc_count = 0;
Herbert Xu932ff272006-06-09 12:20:56 -07002864
2865 /* FIXME: what about netif_tx_lock */
2866 __orinoco_set_multicast_list(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
2868 return 0;
2869}
2870
2871/* FIXME: return int? */
2872static void
2873__orinoco_set_multicast_list(struct net_device *dev)
2874{
2875 struct orinoco_private *priv = netdev_priv(dev);
2876 hermes_t *hw = &priv->hw;
2877 int err = 0;
2878 int promisc, mc_count;
2879
2880 /* The Hermes doesn't seem to have an allmulti mode, so we go
2881 * into promiscuous mode and let the upper levels deal. */
2882 if ( (dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
2883 (dev->mc_count > MAX_MULTICAST(priv)) ) {
2884 promisc = 1;
2885 mc_count = 0;
2886 } else {
2887 promisc = 0;
2888 mc_count = dev->mc_count;
2889 }
2890
2891 if (promisc != priv->promiscuous) {
2892 err = hermes_write_wordrec(hw, USER_BAP,
2893 HERMES_RID_CNFPROMISCUOUSMODE,
2894 promisc);
2895 if (err) {
2896 printk(KERN_ERR "%s: Error %d setting PROMISCUOUSMODE to 1.\n",
2897 dev->name, err);
2898 } else
2899 priv->promiscuous = promisc;
2900 }
2901
David Kilroy667d4102008-08-23 19:03:34 +01002902 /* If we're not in promiscuous mode, then we need to set the
2903 * group address if either we want to multicast, or if we were
2904 * multicasting and want to stop */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 if (! promisc && (mc_count || priv->mc_count) ) {
2906 struct dev_mc_list *p = dev->mc_list;
2907 struct hermes_multicast mclist;
2908 int i;
2909
2910 for (i = 0; i < mc_count; i++) {
2911 /* paranoia: is list shorter than mc_count? */
2912 BUG_ON(! p);
2913 /* paranoia: bad address size in list? */
2914 BUG_ON(p->dmi_addrlen != ETH_ALEN);
2915
2916 memcpy(mclist.addr[i], p->dmi_addr, ETH_ALEN);
2917 p = p->next;
2918 }
2919
2920 if (p)
2921 printk(KERN_WARNING "%s: Multicast list is "
2922 "longer than mc_count\n", dev->name);
2923
David Kilroy667d4102008-08-23 19:03:34 +01002924 err = hermes_write_ltv(hw, USER_BAP,
2925 HERMES_RID_CNFGROUPADDRESSES,
2926 HERMES_BYTES_TO_RECLEN(mc_count * ETH_ALEN),
2927 &mclist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 if (err)
2929 printk(KERN_ERR "%s: Error %d setting multicast list.\n",
2930 dev->name, err);
2931 else
2932 priv->mc_count = mc_count;
2933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934}
2935
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936/* This must be called from user context, without locks held - use
2937 * schedule_work() */
David Howellsc4028952006-11-22 14:57:56 +00002938static void orinoco_reset(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939{
David Howellsc4028952006-11-22 14:57:56 +00002940 struct orinoco_private *priv =
2941 container_of(work, struct orinoco_private, reset_work);
2942 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 struct hermes *hw = &priv->hw;
Christoph Hellwig8551cb92005-05-14 17:30:04 +02002944 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 unsigned long flags;
2946
2947 if (orinoco_lock(priv, &flags) != 0)
2948 /* When the hardware becomes available again, whatever
2949 * detects that is responsible for re-initializing
2950 * it. So no need for anything further */
2951 return;
2952
2953 netif_stop_queue(dev);
2954
2955 /* Shut off interrupts. Depending on what state the hardware
2956 * is in, this might not work, but we'll try anyway */
2957 hermes_set_irqmask(hw, 0);
2958 hermes_write_regn(hw, EVACK, 0xffff);
2959
2960 priv->hw_unavailable++;
2961 priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
2962 netif_carrier_off(dev);
2963
2964 orinoco_unlock(priv, &flags);
2965
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002966 /* Scanning support: Cleanup of driver struct */
Dan Williams1e3428e2007-10-10 23:56:25 -04002967 orinoco_clear_scan_results(priv, 0);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002968 priv->scan_inprogress = 0;
2969
Christoph Hellwig8551cb92005-05-14 17:30:04 +02002970 if (priv->hard_reset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 err = (*priv->hard_reset)(priv);
Christoph Hellwig8551cb92005-05-14 17:30:04 +02002972 if (err) {
2973 printk(KERN_ERR "%s: orinoco_reset: Error %d "
2974 "performing hard reset\n", dev->name, err);
2975 goto disable;
2976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 }
2978
2979 err = orinoco_reinit_firmware(dev);
2980 if (err) {
2981 printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
2982 dev->name, err);
Christoph Hellwig8551cb92005-05-14 17:30:04 +02002983 goto disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 }
2985
2986 spin_lock_irq(&priv->lock); /* This has to be called from user context */
2987
2988 priv->hw_unavailable--;
2989
2990 /* priv->open or priv->hw_unavailable might have changed while
2991 * we dropped the lock */
2992 if (priv->open && (! priv->hw_unavailable)) {
2993 err = __orinoco_up(dev);
2994 if (err) {
2995 printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
2996 dev->name, err);
2997 } else
2998 dev->trans_start = jiffies;
2999 }
3000
3001 spin_unlock_irq(&priv->lock);
3002
3003 return;
Christoph Hellwig8551cb92005-05-14 17:30:04 +02003004 disable:
3005 hermes_set_irqmask(hw, 0);
3006 netif_device_detach(dev);
3007 printk(KERN_ERR "%s: Device has been disabled!\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008}
3009
3010/********************************************************************/
3011/* Interrupt handler */
3012/********************************************************************/
3013
3014static void __orinoco_ev_tick(struct net_device *dev, hermes_t *hw)
3015{
3016 printk(KERN_DEBUG "%s: TICK\n", dev->name);
3017}
3018
3019static void __orinoco_ev_wterr(struct net_device *dev, hermes_t *hw)
3020{
3021 /* This seems to happen a fair bit under load, but ignoring it
3022 seems to work fine...*/
3023 printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
3024 dev->name);
3025}
3026
David Howells7d12e782006-10-05 14:55:46 +01003027irqreturn_t orinoco_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028{
Jeff Garzikc31f28e2006-10-06 14:56:04 -04003029 struct net_device *dev = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 struct orinoco_private *priv = netdev_priv(dev);
3031 hermes_t *hw = &priv->hw;
3032 int count = MAX_IRQLOOPS_PER_IRQ;
3033 u16 evstat, events;
3034 /* These are used to detect a runaway interrupt situation */
3035 /* If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
3036 * we panic and shut down the hardware */
3037 static int last_irq_jiffy = 0; /* jiffies value the last time
3038 * we were called */
3039 static int loops_this_jiffy = 0;
3040 unsigned long flags;
3041
3042 if (orinoco_lock(priv, &flags) != 0) {
3043 /* If hw is unavailable - we don't know if the irq was
3044 * for us or not */
3045 return IRQ_HANDLED;
3046 }
3047
3048 evstat = hermes_read_regn(hw, EVSTAT);
3049 events = evstat & hw->inten;
3050 if (! events) {
3051 orinoco_unlock(priv, &flags);
3052 return IRQ_NONE;
3053 }
3054
3055 if (jiffies != last_irq_jiffy)
3056 loops_this_jiffy = 0;
3057 last_irq_jiffy = jiffies;
3058
3059 while (events && count--) {
3060 if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
3061 printk(KERN_WARNING "%s: IRQ handler is looping too "
3062 "much! Resetting.\n", dev->name);
3063 /* Disable interrupts for now */
3064 hermes_set_irqmask(hw, 0);
3065 schedule_work(&priv->reset_work);
3066 break;
3067 }
3068
3069 /* Check the card hasn't been removed */
3070 if (! hermes_present(hw)) {
3071 DEBUG(0, "orinoco_interrupt(): card removed\n");
3072 break;
3073 }
3074
3075 if (events & HERMES_EV_TICK)
3076 __orinoco_ev_tick(dev, hw);
3077 if (events & HERMES_EV_WTERR)
3078 __orinoco_ev_wterr(dev, hw);
3079 if (events & HERMES_EV_INFDROP)
3080 __orinoco_ev_infdrop(dev, hw);
3081 if (events & HERMES_EV_INFO)
3082 __orinoco_ev_info(dev, hw);
3083 if (events & HERMES_EV_RX)
3084 __orinoco_ev_rx(dev, hw);
3085 if (events & HERMES_EV_TXEXC)
3086 __orinoco_ev_txexc(dev, hw);
3087 if (events & HERMES_EV_TX)
3088 __orinoco_ev_tx(dev, hw);
3089 if (events & HERMES_EV_ALLOC)
3090 __orinoco_ev_alloc(dev, hw);
3091
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02003092 hermes_write_regn(hw, EVACK, evstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
3094 evstat = hermes_read_regn(hw, EVSTAT);
3095 events = evstat & hw->inten;
3096 };
3097
3098 orinoco_unlock(priv, &flags);
3099 return IRQ_HANDLED;
3100}
3101
3102/********************************************************************/
3103/* Initialization */
3104/********************************************************************/
3105
3106struct comp_id {
3107 u16 id, variant, major, minor;
3108} __attribute__ ((packed));
3109
3110static inline fwtype_t determine_firmware_type(struct comp_id *nic_id)
3111{
3112 if (nic_id->id < 0x8000)
3113 return FIRMWARE_TYPE_AGERE;
3114 else if (nic_id->id == 0x8000 && nic_id->major == 0)
3115 return FIRMWARE_TYPE_SYMBOL;
3116 else
3117 return FIRMWARE_TYPE_INTERSIL;
3118}
3119
3120/* Set priv->firmware type, determine firmware properties */
3121static int determine_firmware(struct net_device *dev)
3122{
3123 struct orinoco_private *priv = netdev_priv(dev);
3124 hermes_t *hw = &priv->hw;
3125 int err;
3126 struct comp_id nic_id, sta_id;
3127 unsigned int firmver;
Hennerich, Michaeldde6d432007-02-05 16:41:35 -08003128 char tmp[SYMBOL_MAX_VER_LEN+1] __attribute__((aligned(2)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
3130 /* Get the hardware version */
3131 err = HERMES_READ_RECORD(hw, USER_BAP, HERMES_RID_NICID, &nic_id);
3132 if (err) {
3133 printk(KERN_ERR "%s: Cannot read hardware identity: error %d\n",
3134 dev->name, err);
3135 return err;
3136 }
3137
3138 le16_to_cpus(&nic_id.id);
3139 le16_to_cpus(&nic_id.variant);
3140 le16_to_cpus(&nic_id.major);
3141 le16_to_cpus(&nic_id.minor);
3142 printk(KERN_DEBUG "%s: Hardware identity %04x:%04x:%04x:%04x\n",
3143 dev->name, nic_id.id, nic_id.variant,
3144 nic_id.major, nic_id.minor);
3145
3146 priv->firmware_type = determine_firmware_type(&nic_id);
3147
3148 /* Get the firmware version */
3149 err = HERMES_READ_RECORD(hw, USER_BAP, HERMES_RID_STAID, &sta_id);
3150 if (err) {
3151 printk(KERN_ERR "%s: Cannot read station identity: error %d\n",
3152 dev->name, err);
3153 return err;
3154 }
3155
3156 le16_to_cpus(&sta_id.id);
3157 le16_to_cpus(&sta_id.variant);
3158 le16_to_cpus(&sta_id.major);
3159 le16_to_cpus(&sta_id.minor);
3160 printk(KERN_DEBUG "%s: Station identity %04x:%04x:%04x:%04x\n",
3161 dev->name, sta_id.id, sta_id.variant,
3162 sta_id.major, sta_id.minor);
3163
3164 switch (sta_id.id) {
3165 case 0x15:
3166 printk(KERN_ERR "%s: Primary firmware is active\n",
3167 dev->name);
3168 return -ENODEV;
3169 case 0x14b:
3170 printk(KERN_ERR "%s: Tertiary firmware is active\n",
3171 dev->name);
3172 return -ENODEV;
3173 case 0x1f: /* Intersil, Agere, Symbol Spectrum24 */
3174 case 0x21: /* Symbol Spectrum24 Trilogy */
3175 break;
3176 default:
3177 printk(KERN_NOTICE "%s: Unknown station ID, please report\n",
3178 dev->name);
3179 break;
3180 }
3181
3182 /* Default capabilities */
3183 priv->has_sensitivity = 1;
3184 priv->has_mwo = 0;
3185 priv->has_preamble = 0;
3186 priv->has_port3 = 1;
3187 priv->has_ibss = 1;
3188 priv->has_wep = 0;
3189 priv->has_big_wep = 0;
David Kilroy6eecad72008-08-21 23:27:56 +01003190 priv->has_alt_txcntl = 0;
David Kilroy01632fa2008-08-21 23:27:58 +01003191 priv->has_ext_scan = 0;
David Kilroyd03032a2008-08-21 23:28:02 +01003192 priv->has_wpa = 0;
David Kilroy3994d502008-08-21 23:27:54 +01003193 priv->do_fw_download = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
3195 /* Determine capabilities from the firmware version */
3196 switch (priv->firmware_type) {
3197 case FIRMWARE_TYPE_AGERE:
3198 /* Lucent Wavelan IEEE, Lucent Orinoco, Cabletron RoamAbout,
3199 ELSA, Melco, HP, IBM, Dell 1150, Compaq 110/210 */
3200 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
3201 "Lucent/Agere %d.%02d", sta_id.major, sta_id.minor);
3202
3203 firmver = ((unsigned long)sta_id.major << 16) | sta_id.minor;
3204
3205 priv->has_ibss = (firmver >= 0x60006);
3206 priv->has_wep = (firmver >= 0x40020);
3207 priv->has_big_wep = 1; /* FIXME: this is wrong - how do we tell
3208 Gold cards from the others? */
3209 priv->has_mwo = (firmver >= 0x60000);
3210 priv->has_pm = (firmver >= 0x40020); /* Don't work in 7.52 ? */
3211 priv->ibss_port = 1;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003212 priv->has_hostscan = (firmver >= 0x8000a);
David Kilroy3994d502008-08-21 23:27:54 +01003213 priv->do_fw_download = 1;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02003214 priv->broken_monitor = (firmver >= 0x80000);
David Kilroy6eecad72008-08-21 23:27:56 +01003215 priv->has_alt_txcntl = (firmver >= 0x90000); /* All 9.x ? */
David Kilroy01632fa2008-08-21 23:27:58 +01003216 priv->has_ext_scan = (firmver >= 0x90000); /* All 9.x ? */
David Kilroyd03032a2008-08-21 23:28:02 +01003217 priv->has_wpa = (firmver >= 0x9002a);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 /* Tested with Agere firmware :
3219 * 1.16 ; 4.08 ; 4.52 ; 6.04 ; 6.16 ; 7.28 => Jean II
3220 * Tested CableTron firmware : 4.32 => Anton */
3221 break;
3222 case FIRMWARE_TYPE_SYMBOL:
3223 /* Symbol , 3Com AirConnect, Intel, Ericsson WLAN */
3224 /* Intel MAC : 00:02:B3:* */
3225 /* 3Com MAC : 00:50:DA:* */
3226 memset(tmp, 0, sizeof(tmp));
3227 /* Get the Symbol firmware version */
3228 err = hermes_read_ltv(hw, USER_BAP,
3229 HERMES_RID_SECONDARYVERSION_SYMBOL,
3230 SYMBOL_MAX_VER_LEN, NULL, &tmp);
3231 if (err) {
3232 printk(KERN_WARNING
3233 "%s: Error %d reading Symbol firmware info. Wildly guessing capabilities...\n",
3234 dev->name, err);
3235 firmver = 0;
3236 tmp[0] = '\0';
3237 } else {
3238 /* The firmware revision is a string, the format is
3239 * something like : "V2.20-01".
3240 * Quick and dirty parsing... - Jean II
3241 */
3242 firmver = ((tmp[1] - '0') << 16) | ((tmp[3] - '0') << 12)
3243 | ((tmp[4] - '0') << 8) | ((tmp[6] - '0') << 4)
3244 | (tmp[7] - '0');
3245
3246 tmp[SYMBOL_MAX_VER_LEN] = '\0';
3247 }
3248
3249 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
3250 "Symbol %s", tmp);
3251
3252 priv->has_ibss = (firmver >= 0x20000);
3253 priv->has_wep = (firmver >= 0x15012);
3254 priv->has_big_wep = (firmver >= 0x20000);
3255 priv->has_pm = (firmver >= 0x20000 && firmver < 0x22000) ||
3256 (firmver >= 0x29000 && firmver < 0x30000) ||
3257 firmver >= 0x31000;
3258 priv->has_preamble = (firmver >= 0x20000);
3259 priv->ibss_port = 4;
David Kilroy3994d502008-08-21 23:27:54 +01003260
3261 /* Symbol firmware is found on various cards, but
3262 * there has been no attempt to check firmware
3263 * download on non-spectrum_cs based cards.
3264 *
3265 * Given that the Agere firmware download works
3266 * differently, we should avoid doing a firmware
3267 * download with the Symbol algorithm on non-spectrum
3268 * cards.
3269 *
3270 * For now we can identify a spectrum_cs based card
3271 * because it has a firmware reset function.
3272 */
3273 priv->do_fw_download = (priv->stop_fw != NULL);
3274
Christoph Hellwig649e59e2005-05-14 17:30:10 +02003275 priv->broken_disableport = (firmver == 0x25013) ||
3276 (firmver >= 0x30000 && firmver <= 0x31000);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003277 priv->has_hostscan = (firmver >= 0x31001) ||
3278 (firmver >= 0x29057 && firmver < 0x30000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 /* Tested with Intel firmware : 0x20015 => Jean II */
3280 /* Tested with 3Com firmware : 0x15012 & 0x22001 => Jean II */
3281 break;
3282 case FIRMWARE_TYPE_INTERSIL:
3283 /* D-Link, Linksys, Adtron, ZoomAir, and many others...
3284 * Samsung, Compaq 100/200 and Proxim are slightly
3285 * different and less well tested */
3286 /* D-Link MAC : 00:40:05:* */
3287 /* Addtron MAC : 00:90:D1:* */
3288 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
3289 "Intersil %d.%d.%d", sta_id.major, sta_id.minor,
3290 sta_id.variant);
3291
3292 firmver = ((unsigned long)sta_id.major << 16) |
3293 ((unsigned long)sta_id.minor << 8) | sta_id.variant;
3294
3295 priv->has_ibss = (firmver >= 0x000700); /* FIXME */
3296 priv->has_big_wep = priv->has_wep = (firmver >= 0x000800);
3297 priv->has_pm = (firmver >= 0x000700);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003298 priv->has_hostscan = (firmver >= 0x010301);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299
3300 if (firmver >= 0x000800)
3301 priv->ibss_port = 0;
3302 else {
3303 printk(KERN_NOTICE "%s: Intersil firmware earlier "
3304 "than v0.8.x - several features not supported\n",
3305 dev->name);
3306 priv->ibss_port = 1;
3307 }
3308 break;
3309 }
3310 printk(KERN_DEBUG "%s: Firmware determined as %s\n", dev->name,
3311 priv->fw_name);
3312
3313 return 0;
3314}
3315
3316static int orinoco_init(struct net_device *dev)
3317{
3318 struct orinoco_private *priv = netdev_priv(dev);
3319 hermes_t *hw = &priv->hw;
3320 int err = 0;
3321 struct hermes_idstring nickbuf;
3322 u16 reclen;
3323 int len;
3324
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 /* No need to lock, the hw_unavailable flag is already set in
3326 * alloc_orinocodev() */
Johannes Berg2c7060022008-10-30 22:09:54 +01003327 priv->nicbuf_size = IEEE80211_MAX_FRAME_LEN + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
3329 /* Initialize the firmware */
Pavel Roskin37a6c612006-04-07 04:10:49 -04003330 err = hermes_init(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 if (err != 0) {
3332 printk(KERN_ERR "%s: failed to initialize firmware (err = %d)\n",
3333 dev->name, err);
3334 goto out;
3335 }
3336
3337 err = determine_firmware(dev);
3338 if (err != 0) {
3339 printk(KERN_ERR "%s: Incompatible firmware, aborting\n",
3340 dev->name);
3341 goto out;
3342 }
3343
David Kilroy3994d502008-08-21 23:27:54 +01003344 if (priv->do_fw_download) {
David Kilroy74734312008-11-22 10:37:25 +00003345 orinoco_cache_fw(priv, 0);
3346
David Kilroy3994d502008-08-21 23:27:54 +01003347 err = orinoco_download(priv);
3348 if (err)
3349 priv->do_fw_download = 0;
3350
3351 /* Check firmware version again */
3352 err = determine_firmware(dev);
3353 if (err != 0) {
3354 printk(KERN_ERR "%s: Incompatible firmware, aborting\n",
3355 dev->name);
3356 goto out;
3357 }
3358 }
3359
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 if (priv->has_port3)
3361 printk(KERN_DEBUG "%s: Ad-hoc demo mode supported\n", dev->name);
3362 if (priv->has_ibss)
3363 printk(KERN_DEBUG "%s: IEEE standard IBSS ad-hoc mode supported\n",
3364 dev->name);
3365 if (priv->has_wep) {
3366 printk(KERN_DEBUG "%s: WEP supported, ", dev->name);
3367 if (priv->has_big_wep)
3368 printk("104-bit key\n");
3369 else
3370 printk("40-bit key\n");
3371 }
David Kilroy23edcc42008-08-21 23:28:05 +01003372 if (priv->has_wpa) {
David Kilroyd03032a2008-08-21 23:28:02 +01003373 printk(KERN_DEBUG "%s: WPA-PSK supported\n", dev->name);
David Kilroy23edcc42008-08-21 23:28:05 +01003374 if (orinoco_mic_init(priv)) {
3375 printk(KERN_ERR "%s: Failed to setup MIC crypto "
3376 "algorithm. Disabling WPA support\n", dev->name);
3377 priv->has_wpa = 0;
3378 }
3379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380
David Kilroy01632fa2008-08-21 23:27:58 +01003381 /* Now we have the firmware capabilities, allocate appropiate
3382 * sized scan buffers */
3383 if (orinoco_bss_data_allocate(priv))
3384 goto out;
3385 orinoco_bss_data_init(priv);
3386
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 /* Get the MAC address */
3388 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
3389 ETH_ALEN, NULL, dev->dev_addr);
3390 if (err) {
3391 printk(KERN_WARNING "%s: failed to read MAC address!\n",
3392 dev->name);
3393 goto out;
3394 }
3395
Johannes Berge1749612008-10-27 15:59:26 -07003396 printk(KERN_DEBUG "%s: MAC address %pM\n",
3397 dev->name, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398
3399 /* Get the station name */
3400 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
3401 sizeof(nickbuf), &reclen, &nickbuf);
3402 if (err) {
3403 printk(KERN_ERR "%s: failed to read station name\n",
3404 dev->name);
3405 goto out;
3406 }
3407 if (nickbuf.len)
3408 len = min(IW_ESSID_MAX_SIZE, (int)le16_to_cpu(nickbuf.len));
3409 else
3410 len = min(IW_ESSID_MAX_SIZE, 2 * reclen);
3411 memcpy(priv->nick, &nickbuf.val, len);
3412 priv->nick[len] = '\0';
3413
3414 printk(KERN_DEBUG "%s: Station name \"%s\"\n", dev->name, priv->nick);
3415
Pavel Roskin37a6c612006-04-07 04:10:49 -04003416 err = orinoco_allocate_fid(dev);
3417 if (err) {
3418 printk(KERN_ERR "%s: failed to allocate NIC buffer!\n",
3419 dev->name);
3420 goto out;
3421 }
3422
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 /* Get allowed channels */
3424 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CHANNELLIST,
3425 &priv->channel_mask);
3426 if (err) {
3427 printk(KERN_ERR "%s: failed to read channel list!\n",
3428 dev->name);
3429 goto out;
3430 }
3431
3432 /* Get initial AP density */
3433 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFSYSTEMSCALE,
3434 &priv->ap_density);
3435 if (err || priv->ap_density < 1 || priv->ap_density > 3) {
3436 priv->has_sensitivity = 0;
3437 }
3438
3439 /* Get initial RTS threshold */
3440 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
3441 &priv->rts_thresh);
3442 if (err) {
3443 printk(KERN_ERR "%s: failed to read RTS threshold!\n",
3444 dev->name);
3445 goto out;
3446 }
3447
3448 /* Get initial fragmentation settings */
3449 if (priv->has_mwo)
3450 err = hermes_read_wordrec(hw, USER_BAP,
3451 HERMES_RID_CNFMWOROBUST_AGERE,
3452 &priv->mwo_robust);
3453 else
3454 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
3455 &priv->frag_thresh);
3456 if (err) {
3457 printk(KERN_ERR "%s: failed to read fragmentation settings!\n",
3458 dev->name);
3459 goto out;
3460 }
3461
3462 /* Power management setup */
3463 if (priv->has_pm) {
3464 priv->pm_on = 0;
3465 priv->pm_mcast = 1;
3466 err = hermes_read_wordrec(hw, USER_BAP,
3467 HERMES_RID_CNFMAXSLEEPDURATION,
3468 &priv->pm_period);
3469 if (err) {
3470 printk(KERN_ERR "%s: failed to read power management period!\n",
3471 dev->name);
3472 goto out;
3473 }
3474 err = hermes_read_wordrec(hw, USER_BAP,
3475 HERMES_RID_CNFPMHOLDOVERDURATION,
3476 &priv->pm_timeout);
3477 if (err) {
3478 printk(KERN_ERR "%s: failed to read power management timeout!\n",
3479 dev->name);
3480 goto out;
3481 }
3482 }
3483
3484 /* Preamble setup */
3485 if (priv->has_preamble) {
3486 err = hermes_read_wordrec(hw, USER_BAP,
3487 HERMES_RID_CNFPREAMBLE_SYMBOL,
3488 &priv->preamble);
3489 if (err)
3490 goto out;
3491 }
3492
3493 /* Set up the default configuration */
3494 priv->iw_mode = IW_MODE_INFRA;
3495 /* By default use IEEE/IBSS ad-hoc mode if we have it */
3496 priv->prefer_port3 = priv->has_port3 && (! priv->has_ibss);
3497 set_port_type(priv);
David Gibsond51d8b12005-05-12 20:03:36 -04003498 priv->channel = 0; /* use firmware default */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499
3500 priv->promiscuous = 0;
David Kilroy4ae6ee22008-08-21 23:27:59 +01003501 priv->encode_alg = IW_ENCODE_ALG_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 priv->tx_key = 0;
David Kilroyd03032a2008-08-21 23:28:02 +01003503 priv->wpa_enabled = 0;
3504 priv->tkip_cm_active = 0;
3505 priv->key_mgmt = 0;
3506 priv->wpa_ie_len = 0;
3507 priv->wpa_ie = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 /* Make the hardware available, as long as it hasn't been
3510 * removed elsewhere (e.g. by PCMCIA hot unplug) */
3511 spin_lock_irq(&priv->lock);
3512 priv->hw_unavailable--;
3513 spin_unlock_irq(&priv->lock);
3514
3515 printk(KERN_DEBUG "%s: ready\n", dev->name);
3516
3517 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 return err;
3519}
3520
David Kilroy3994d502008-08-21 23:27:54 +01003521struct net_device
3522*alloc_orinocodev(int sizeof_card,
3523 struct device *device,
3524 int (*hard_reset)(struct orinoco_private *),
3525 int (*stop_fw)(struct orinoco_private *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526{
3527 struct net_device *dev;
3528 struct orinoco_private *priv;
3529
3530 dev = alloc_etherdev(sizeof(struct orinoco_private) + sizeof_card);
3531 if (! dev)
3532 return NULL;
3533 priv = netdev_priv(dev);
3534 priv->ndev = dev;
3535 if (sizeof_card)
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02003536 priv->card = (void *)((unsigned long)priv
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 + sizeof(struct orinoco_private));
3538 else
3539 priv->card = NULL;
David Kilroy3994d502008-08-21 23:27:54 +01003540 priv->dev = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
3542 /* Setup / override net_device fields */
3543 dev->init = orinoco_init;
3544 dev->hard_start_xmit = orinoco_xmit;
3545 dev->tx_timeout = orinoco_tx_timeout;
3546 dev->watchdog_timeo = HZ; /* 1 second timeout */
3547 dev->get_stats = orinoco_get_stats;
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02003548 dev->ethtool_ops = &orinoco_ethtool_ops;
Christoph Hellwig620554e2005-06-19 01:27:33 +02003549 dev->wireless_handlers = (struct iw_handler_def *)&orinoco_handler_def;
Pavel Roskin343c6862005-09-09 18:43:02 -04003550#ifdef WIRELESS_SPY
3551 priv->wireless_data.spy_data = &priv->spy_data;
3552 dev->wireless_data = &priv->wireless_data;
3553#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 dev->change_mtu = orinoco_change_mtu;
3555 dev->set_multicast_list = orinoco_set_multicast_list;
3556 /* we use the default eth_mac_addr for setting the MAC addr */
3557
David Kilroy23edcc42008-08-21 23:28:05 +01003558 /* Reserve space in skb for the SNAP header */
3559 dev->hard_header_len += ENCAPS_OVERHEAD;
3560
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 /* Set up default callbacks */
3562 dev->open = orinoco_open;
3563 dev->stop = orinoco_stop;
3564 priv->hard_reset = hard_reset;
David Kilroy3994d502008-08-21 23:27:54 +01003565 priv->stop_fw = stop_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
3567 spin_lock_init(&priv->lock);
3568 priv->open = 0;
3569 priv->hw_unavailable = 1; /* orinoco_init() must clear this
3570 * before anything else touches the
3571 * hardware */
David Howellsc4028952006-11-22 14:57:56 +00003572 INIT_WORK(&priv->reset_work, orinoco_reset);
3573 INIT_WORK(&priv->join_work, orinoco_join_ap);
3574 INIT_WORK(&priv->wevent_work, orinoco_send_wevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575
David Kilroy31afcef2008-08-21 23:28:04 +01003576 INIT_LIST_HEAD(&priv->rx_list);
3577 tasklet_init(&priv->rx_tasklet, orinoco_rx_isr_tasklet,
3578 (unsigned long) dev);
3579
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 netif_carrier_off(dev);
3581 priv->last_linkstatus = 0xffff;
3582
David Kilroy2cea7b22008-11-22 10:37:26 +00003583 priv->cached_pri_fw = NULL;
Andrey Borzenkov4fb30782008-10-19 12:06:11 +04003584 priv->cached_fw = NULL;
3585
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 return dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587}
3588
3589void free_orinocodev(struct net_device *dev)
3590{
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003591 struct orinoco_private *priv = netdev_priv(dev);
3592
David Kilroy31afcef2008-08-21 23:28:04 +01003593 /* No need to empty priv->rx_list: if the tasklet is scheduled
3594 * when we call tasklet_kill it will run one final time,
3595 * emptying the list */
3596 tasklet_kill(&priv->rx_tasklet);
David Kilroy74734312008-11-22 10:37:25 +00003597
3598 orinoco_uncache_fw(priv);
3599
David Kilroyd03032a2008-08-21 23:28:02 +01003600 priv->wpa_ie_len = 0;
3601 kfree(priv->wpa_ie);
David Kilroy23edcc42008-08-21 23:28:05 +01003602 orinoco_mic_free(priv);
Dan Williams1e3428e2007-10-10 23:56:25 -04003603 orinoco_bss_data_free(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 free_netdev(dev);
3605}
3606
3607/********************************************************************/
3608/* Wireless extensions */
3609/********************************************************************/
3610
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07003611/* Return : < 0 -> error code ; >= 0 -> length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612static int orinoco_hw_get_essid(struct orinoco_private *priv, int *active,
3613 char buf[IW_ESSID_MAX_SIZE+1])
3614{
3615 hermes_t *hw = &priv->hw;
3616 int err = 0;
3617 struct hermes_idstring essidbuf;
3618 char *p = (char *)(&essidbuf.val);
3619 int len;
3620 unsigned long flags;
3621
3622 if (orinoco_lock(priv, &flags) != 0)
3623 return -EBUSY;
3624
3625 if (strlen(priv->desired_essid) > 0) {
3626 /* We read the desired SSID from the hardware rather
3627 than from priv->desired_essid, just in case the
3628 firmware is allowed to change it on us. I'm not
3629 sure about this */
3630 /* My guess is that the OWNSSID should always be whatever
3631 * we set to the card, whereas CURRENT_SSID is the one that
3632 * may change... - Jean II */
3633 u16 rid;
3634
3635 *active = 1;
3636
3637 rid = (priv->port_type == 3) ? HERMES_RID_CNFOWNSSID :
3638 HERMES_RID_CNFDESIREDSSID;
3639
3640 err = hermes_read_ltv(hw, USER_BAP, rid, sizeof(essidbuf),
3641 NULL, &essidbuf);
3642 if (err)
3643 goto fail_unlock;
3644 } else {
3645 *active = 0;
3646
3647 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTSSID,
3648 sizeof(essidbuf), NULL, &essidbuf);
3649 if (err)
3650 goto fail_unlock;
3651 }
3652
3653 len = le16_to_cpu(essidbuf.len);
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02003654 BUG_ON(len > IW_ESSID_MAX_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07003656 memset(buf, 0, IW_ESSID_MAX_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 memcpy(buf, p, len);
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07003658 err = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659
3660 fail_unlock:
3661 orinoco_unlock(priv, &flags);
3662
3663 return err;
3664}
3665
3666static long orinoco_hw_get_freq(struct orinoco_private *priv)
3667{
3668
3669 hermes_t *hw = &priv->hw;
3670 int err = 0;
3671 u16 channel;
3672 long freq = 0;
3673 unsigned long flags;
3674
3675 if (orinoco_lock(priv, &flags) != 0)
3676 return -EBUSY;
3677
3678 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CURRENTCHANNEL, &channel);
3679 if (err)
3680 goto out;
3681
3682 /* Intersil firmware 1.3.5 returns 0 when the interface is down */
3683 if (channel == 0) {
3684 err = -EBUSY;
3685 goto out;
3686 }
3687
3688 if ( (channel < 1) || (channel > NUM_CHANNELS) ) {
3689 printk(KERN_WARNING "%s: Channel out of range (%d)!\n",
3690 priv->ndev->name, channel);
3691 err = -EBUSY;
3692 goto out;
3693
3694 }
3695 freq = channel_frequency[channel-1] * 100000;
3696
3697 out:
3698 orinoco_unlock(priv, &flags);
3699
3700 if (err > 0)
3701 err = -EBUSY;
3702 return err ? err : freq;
3703}
3704
3705static int orinoco_hw_get_bitratelist(struct orinoco_private *priv,
3706 int *numrates, s32 *rates, int max)
3707{
3708 hermes_t *hw = &priv->hw;
3709 struct hermes_idstring list;
3710 unsigned char *p = (unsigned char *)&list.val;
3711 int err = 0;
3712 int num;
3713 int i;
3714 unsigned long flags;
3715
3716 if (orinoco_lock(priv, &flags) != 0)
3717 return -EBUSY;
3718
3719 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_SUPPORTEDDATARATES,
3720 sizeof(list), NULL, &list);
3721 orinoco_unlock(priv, &flags);
3722
3723 if (err)
3724 return err;
3725
3726 num = le16_to_cpu(list.len);
3727 *numrates = num;
3728 num = min(num, max);
3729
3730 for (i = 0; i < num; i++) {
3731 rates[i] = (p[i] & 0x7f) * 500000; /* convert to bps */
3732 }
3733
3734 return 0;
3735}
3736
Christoph Hellwig620554e2005-06-19 01:27:33 +02003737static int orinoco_ioctl_getname(struct net_device *dev,
3738 struct iw_request_info *info,
3739 char *name,
3740 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741{
3742 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 int numrates;
Christoph Hellwig620554e2005-06-19 01:27:33 +02003744 int err;
3745
3746 err = orinoco_hw_get_bitratelist(priv, &numrates, NULL, 0);
3747
3748 if (!err && (numrates > 2))
3749 strcpy(name, "IEEE 802.11b");
3750 else
3751 strcpy(name, "IEEE 802.11-DS");
3752
3753 return 0;
3754}
3755
Christoph Hellwig16739b02005-06-19 01:27:51 +02003756static int orinoco_ioctl_setwap(struct net_device *dev,
3757 struct iw_request_info *info,
3758 struct sockaddr *ap_addr,
3759 char *extra)
3760{
3761 struct orinoco_private *priv = netdev_priv(dev);
3762 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 unsigned long flags;
Christoph Hellwig16739b02005-06-19 01:27:51 +02003764 static const u8 off_addr[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3765 static const u8 any_addr[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
3767 if (orinoco_lock(priv, &flags) != 0)
3768 return -EBUSY;
3769
Christoph Hellwig16739b02005-06-19 01:27:51 +02003770 /* Enable automatic roaming - no sanity checks are needed */
3771 if (memcmp(&ap_addr->sa_data, off_addr, ETH_ALEN) == 0 ||
3772 memcmp(&ap_addr->sa_data, any_addr, ETH_ALEN) == 0) {
3773 priv->bssid_fixed = 0;
3774 memset(priv->desired_bssid, 0, ETH_ALEN);
3775
3776 /* "off" means keep existing connection */
3777 if (ap_addr->sa_data[0] == 0) {
3778 __orinoco_hw_set_wap(priv);
3779 err = 0;
3780 }
3781 goto out;
3782 }
3783
3784 if (priv->firmware_type == FIRMWARE_TYPE_AGERE) {
3785 printk(KERN_WARNING "%s: Lucent/Agere firmware doesn't "
3786 "support manual roaming\n",
3787 dev->name);
3788 err = -EOPNOTSUPP;
3789 goto out;
3790 }
3791
3792 if (priv->iw_mode != IW_MODE_INFRA) {
3793 printk(KERN_WARNING "%s: Manual roaming supported only in "
3794 "managed mode\n", dev->name);
3795 err = -EOPNOTSUPP;
3796 goto out;
3797 }
3798
3799 /* Intersil firmware hangs without Desired ESSID */
3800 if (priv->firmware_type == FIRMWARE_TYPE_INTERSIL &&
3801 strlen(priv->desired_essid) == 0) {
3802 printk(KERN_WARNING "%s: Desired ESSID must be set for "
3803 "manual roaming\n", dev->name);
3804 err = -EOPNOTSUPP;
3805 goto out;
3806 }
3807
3808 /* Finally, enable manual roaming */
3809 priv->bssid_fixed = 1;
3810 memcpy(priv->desired_bssid, &ap_addr->sa_data, ETH_ALEN);
3811
3812 out:
3813 orinoco_unlock(priv, &flags);
3814 return err;
3815}
3816
Christoph Hellwig620554e2005-06-19 01:27:33 +02003817static int orinoco_ioctl_getwap(struct net_device *dev,
3818 struct iw_request_info *info,
3819 struct sockaddr *ap_addr,
3820 char *extra)
3821{
3822 struct orinoco_private *priv = netdev_priv(dev);
3823
3824 hermes_t *hw = &priv->hw;
3825 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 unsigned long flags;
3827
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 if (orinoco_lock(priv, &flags) != 0)
3829 return -EBUSY;
3830
Christoph Hellwig620554e2005-06-19 01:27:33 +02003831 ap_addr->sa_family = ARPHRD_ETHER;
3832 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
3833 ETH_ALEN, NULL, ap_addr->sa_data);
3834
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 orinoco_unlock(priv, &flags);
3836
Christoph Hellwig620554e2005-06-19 01:27:33 +02003837 return err;
3838}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839
Christoph Hellwig620554e2005-06-19 01:27:33 +02003840static int orinoco_ioctl_setmode(struct net_device *dev,
3841 struct iw_request_info *info,
3842 u32 *mode,
3843 char *extra)
3844{
3845 struct orinoco_private *priv = netdev_priv(dev);
3846 int err = -EINPROGRESS; /* Call commit handler */
3847 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848
Christoph Hellwig620554e2005-06-19 01:27:33 +02003849 if (priv->iw_mode == *mode)
3850 return 0;
3851
3852 if (orinoco_lock(priv, &flags) != 0)
3853 return -EBUSY;
3854
3855 switch (*mode) {
3856 case IW_MODE_ADHOC:
3857 if (!priv->has_ibss && !priv->has_port3)
3858 err = -EOPNOTSUPP;
3859 break;
3860
3861 case IW_MODE_INFRA:
3862 break;
3863
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02003864 case IW_MODE_MONITOR:
3865 if (priv->broken_monitor && !force_monitor) {
3866 printk(KERN_WARNING "%s: Monitor mode support is "
3867 "buggy in this firmware, not enabling\n",
3868 dev->name);
3869 err = -EOPNOTSUPP;
3870 }
3871 break;
3872
Christoph Hellwig620554e2005-06-19 01:27:33 +02003873 default:
3874 err = -EOPNOTSUPP;
3875 break;
3876 }
3877
3878 if (err == -EINPROGRESS) {
3879 priv->iw_mode = *mode;
3880 set_port_type(priv);
3881 }
3882
3883 orinoco_unlock(priv, &flags);
3884
3885 return err;
3886}
3887
3888static int orinoco_ioctl_getmode(struct net_device *dev,
3889 struct iw_request_info *info,
3890 u32 *mode,
3891 char *extra)
3892{
3893 struct orinoco_private *priv = netdev_priv(dev);
3894
3895 *mode = priv->iw_mode;
3896 return 0;
3897}
3898
3899static int orinoco_ioctl_getiwrange(struct net_device *dev,
3900 struct iw_request_info *info,
3901 struct iw_point *rrq,
3902 char *extra)
3903{
3904 struct orinoco_private *priv = netdev_priv(dev);
3905 int err = 0;
3906 struct iw_range *range = (struct iw_range *) extra;
3907 int numrates;
3908 int i, k;
3909
Christoph Hellwig620554e2005-06-19 01:27:33 +02003910 rrq->length = sizeof(struct iw_range);
3911 memset(range, 0, sizeof(struct iw_range));
3912
3913 range->we_version_compiled = WIRELESS_EXT;
David Kilroyd03032a2008-08-21 23:28:02 +01003914 range->we_version_source = 22;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915
3916 /* Set available channels/frequencies */
Christoph Hellwig620554e2005-06-19 01:27:33 +02003917 range->num_channels = NUM_CHANNELS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 k = 0;
3919 for (i = 0; i < NUM_CHANNELS; i++) {
3920 if (priv->channel_mask & (1 << i)) {
Christoph Hellwig620554e2005-06-19 01:27:33 +02003921 range->freq[k].i = i + 1;
3922 range->freq[k].m = channel_frequency[i] * 100000;
3923 range->freq[k].e = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 k++;
3925 }
3926
3927 if (k >= IW_MAX_FREQUENCIES)
3928 break;
3929 }
Christoph Hellwig620554e2005-06-19 01:27:33 +02003930 range->num_frequency = k;
3931 range->sensitivity = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932
Christoph Hellwig620554e2005-06-19 01:27:33 +02003933 if (priv->has_wep) {
3934 range->max_encoding_tokens = ORINOCO_MAX_KEYS;
3935 range->encoding_size[0] = SMALL_KEY_SIZE;
3936 range->num_encoding_sizes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937
Christoph Hellwig620554e2005-06-19 01:27:33 +02003938 if (priv->has_big_wep) {
3939 range->encoding_size[1] = LARGE_KEY_SIZE;
3940 range->num_encoding_sizes = 2;
3941 }
3942 }
3943
David Kilroyd03032a2008-08-21 23:28:02 +01003944 if (priv->has_wpa)
3945 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_CIPHER_TKIP;
3946
Pavel Roskin343c6862005-09-09 18:43:02 -04003947 if ((priv->iw_mode == IW_MODE_ADHOC) && (!SPY_NUMBER(priv))){
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 /* Quality stats meaningless in ad-hoc mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 } else {
Christoph Hellwig620554e2005-06-19 01:27:33 +02003950 range->max_qual.qual = 0x8b - 0x2f;
3951 range->max_qual.level = 0x2f - 0x95 - 1;
3952 range->max_qual.noise = 0x2f - 0x95 - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 /* Need to get better values */
Christoph Hellwig620554e2005-06-19 01:27:33 +02003954 range->avg_qual.qual = 0x24;
3955 range->avg_qual.level = 0xC2;
3956 range->avg_qual.noise = 0x9E;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 }
3958
3959 err = orinoco_hw_get_bitratelist(priv, &numrates,
Christoph Hellwig620554e2005-06-19 01:27:33 +02003960 range->bitrate, IW_MAX_BITRATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 if (err)
3962 return err;
Christoph Hellwig620554e2005-06-19 01:27:33 +02003963 range->num_bitrates = numrates;
3964
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 /* Set an indication of the max TCP throughput in bit/s that we can
3966 * expect using this interface. May be use for QoS stuff...
3967 * Jean II */
Christoph Hellwig620554e2005-06-19 01:27:33 +02003968 if (numrates > 2)
3969 range->throughput = 5 * 1000 * 1000; /* ~5 Mb/s */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 else
Christoph Hellwig620554e2005-06-19 01:27:33 +02003971 range->throughput = 1.5 * 1000 * 1000; /* ~1.5 Mb/s */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972
Christoph Hellwig620554e2005-06-19 01:27:33 +02003973 range->min_rts = 0;
3974 range->max_rts = 2347;
3975 range->min_frag = 256;
3976 range->max_frag = 2346;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977
Christoph Hellwig620554e2005-06-19 01:27:33 +02003978 range->min_pmp = 0;
3979 range->max_pmp = 65535000;
3980 range->min_pmt = 0;
3981 range->max_pmt = 65535 * 1000; /* ??? */
3982 range->pmp_flags = IW_POWER_PERIOD;
3983 range->pmt_flags = IW_POWER_TIMEOUT;
3984 range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_UNICAST_R;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
Christoph Hellwig620554e2005-06-19 01:27:33 +02003986 range->retry_capa = IW_RETRY_LIMIT | IW_RETRY_LIFETIME;
3987 range->retry_flags = IW_RETRY_LIMIT;
3988 range->r_time_flags = IW_RETRY_LIFETIME;
3989 range->min_retry = 0;
3990 range->max_retry = 65535; /* ??? */
3991 range->min_r_time = 0;
3992 range->max_r_time = 65535 * 1000; /* ??? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
David Kilroy0753bba2008-08-21 23:27:46 +01003994 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
3995 range->scan_capa = IW_SCAN_CAPA_ESSID;
3996 else
3997 range->scan_capa = IW_SCAN_CAPA_NONE;
3998
Pavel Roskin343c6862005-09-09 18:43:02 -04003999 /* Event capability (kernel) */
4000 IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
4001 /* Event capability (driver) */
4002 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWTHRSPY);
4003 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
4004 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
4005 IW_EVENT_CAPA_SET(range->event_capa, IWEVTXDROP);
4006
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 return 0;
4008}
4009
Christoph Hellwig620554e2005-06-19 01:27:33 +02004010static int orinoco_ioctl_setiwencode(struct net_device *dev,
4011 struct iw_request_info *info,
4012 struct iw_point *erq,
4013 char *keybuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014{
4015 struct orinoco_private *priv = netdev_priv(dev);
4016 int index = (erq->flags & IW_ENCODE_INDEX) - 1;
4017 int setindex = priv->tx_key;
David Kilroy4ae6ee22008-08-21 23:27:59 +01004018 int encode_alg = priv->encode_alg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 int restricted = priv->wep_restrict;
4020 u16 xlen = 0;
Christoph Hellwig620554e2005-06-19 01:27:33 +02004021 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 unsigned long flags;
4023
4024 if (! priv->has_wep)
4025 return -EOPNOTSUPP;
4026
4027 if (erq->pointer) {
4028 /* We actually have a key to set - check its length */
4029 if (erq->length > LARGE_KEY_SIZE)
4030 return -E2BIG;
4031
4032 if ( (erq->length > SMALL_KEY_SIZE) && !priv->has_big_wep )
4033 return -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 }
4035
4036 if (orinoco_lock(priv, &flags) != 0)
4037 return -EBUSY;
4038
David Kilroyd03032a2008-08-21 23:28:02 +01004039 /* Clear any TKIP key we have */
4040 if ((priv->has_wpa) && (priv->encode_alg == IW_ENCODE_ALG_TKIP))
4041 (void) orinoco_clear_tkip_key(priv, setindex);
4042
Dan Williamsfe397d42006-07-14 11:41:47 -04004043 if (erq->length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
4045 index = priv->tx_key;
4046
4047 /* Adjust key length to a supported value */
4048 if (erq->length > SMALL_KEY_SIZE) {
4049 xlen = LARGE_KEY_SIZE;
4050 } else if (erq->length > 0) {
4051 xlen = SMALL_KEY_SIZE;
4052 } else
4053 xlen = 0;
4054
4055 /* Switch on WEP if off */
David Kilroy4ae6ee22008-08-21 23:27:59 +01004056 if ((encode_alg != IW_ENCODE_ALG_WEP) && (xlen > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 setindex = index;
David Kilroy4ae6ee22008-08-21 23:27:59 +01004058 encode_alg = IW_ENCODE_ALG_WEP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 }
4060 } else {
4061 /* Important note : if the user do "iwconfig eth0 enc off",
4062 * we will arrive there with an index of -1. This is valid
4063 * but need to be taken care off... Jean II */
4064 if ((index < 0) || (index >= ORINOCO_MAX_KEYS)) {
4065 if((index != -1) || (erq->flags == 0)) {
4066 err = -EINVAL;
4067 goto out;
4068 }
4069 } else {
4070 /* Set the index : Check that the key is valid */
4071 if(priv->keys[index].len == 0) {
4072 err = -EINVAL;
4073 goto out;
4074 }
4075 setindex = index;
4076 }
4077 }
4078
4079 if (erq->flags & IW_ENCODE_DISABLED)
David Kilroy4ae6ee22008-08-21 23:27:59 +01004080 encode_alg = IW_ENCODE_ALG_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 if (erq->flags & IW_ENCODE_OPEN)
4082 restricted = 0;
4083 if (erq->flags & IW_ENCODE_RESTRICTED)
4084 restricted = 1;
4085
Dan Williamsfe397d42006-07-14 11:41:47 -04004086 if (erq->pointer && erq->length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 priv->keys[index].len = cpu_to_le16(xlen);
4088 memset(priv->keys[index].data, 0,
4089 sizeof(priv->keys[index].data));
4090 memcpy(priv->keys[index].data, keybuf, erq->length);
4091 }
4092 priv->tx_key = setindex;
4093
4094 /* Try fast key change if connected and only keys are changed */
David Kilroy4ae6ee22008-08-21 23:27:59 +01004095 if ((priv->encode_alg == encode_alg) &&
4096 (priv->wep_restrict == restricted) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 netif_carrier_ok(dev)) {
4098 err = __orinoco_hw_setup_wepkeys(priv);
4099 /* No need to commit if successful */
4100 goto out;
4101 }
4102
David Kilroy4ae6ee22008-08-21 23:27:59 +01004103 priv->encode_alg = encode_alg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 priv->wep_restrict = restricted;
4105
4106 out:
4107 orinoco_unlock(priv, &flags);
4108
4109 return err;
4110}
4111
Christoph Hellwig620554e2005-06-19 01:27:33 +02004112static int orinoco_ioctl_getiwencode(struct net_device *dev,
4113 struct iw_request_info *info,
4114 struct iw_point *erq,
4115 char *keybuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116{
4117 struct orinoco_private *priv = netdev_priv(dev);
4118 int index = (erq->flags & IW_ENCODE_INDEX) - 1;
4119 u16 xlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 unsigned long flags;
4121
4122 if (! priv->has_wep)
4123 return -EOPNOTSUPP;
4124
4125 if (orinoco_lock(priv, &flags) != 0)
4126 return -EBUSY;
4127
4128 if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
4129 index = priv->tx_key;
4130
4131 erq->flags = 0;
David Kilroy4ae6ee22008-08-21 23:27:59 +01004132 if (!priv->encode_alg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 erq->flags |= IW_ENCODE_DISABLED;
4134 erq->flags |= index + 1;
4135
4136 if (priv->wep_restrict)
4137 erq->flags |= IW_ENCODE_RESTRICTED;
4138 else
4139 erq->flags |= IW_ENCODE_OPEN;
4140
4141 xlen = le16_to_cpu(priv->keys[index].len);
4142
4143 erq->length = xlen;
4144
4145 memcpy(keybuf, priv->keys[index].data, ORINOCO_MAX_KEY_SIZE);
4146
4147 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 return 0;
4149}
4150
Christoph Hellwig620554e2005-06-19 01:27:33 +02004151static int orinoco_ioctl_setessid(struct net_device *dev,
4152 struct iw_request_info *info,
4153 struct iw_point *erq,
4154 char *essidbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155{
4156 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 unsigned long flags;
4158
4159 /* Note : ESSID is ignored in Ad-Hoc demo mode, but we can set it
4160 * anyway... - Jean II */
4161
Christoph Hellwig620554e2005-06-19 01:27:33 +02004162 /* Hum... Should not use Wireless Extension constant (may change),
4163 * should use our own... - Jean II */
4164 if (erq->length > IW_ESSID_MAX_SIZE)
4165 return -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166
4167 if (orinoco_lock(priv, &flags) != 0)
4168 return -EBUSY;
4169
Christoph Hellwig620554e2005-06-19 01:27:33 +02004170 /* NULL the string (for NULL termination & ESSID = ANY) - Jean II */
4171 memset(priv->desired_essid, 0, sizeof(priv->desired_essid));
4172
4173 /* If not ANY, get the new ESSID */
4174 if (erq->flags) {
4175 memcpy(priv->desired_essid, essidbuf, erq->length);
4176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177
4178 orinoco_unlock(priv, &flags);
4179
Christoph Hellwig620554e2005-06-19 01:27:33 +02004180 return -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181}
4182
Christoph Hellwig620554e2005-06-19 01:27:33 +02004183static int orinoco_ioctl_getessid(struct net_device *dev,
4184 struct iw_request_info *info,
4185 struct iw_point *erq,
4186 char *essidbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187{
4188 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 int active;
4190 int err = 0;
4191 unsigned long flags;
4192
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 if (netif_running(dev)) {
4194 err = orinoco_hw_get_essid(priv, &active, essidbuf);
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07004195 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 return err;
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07004197 erq->length = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 } else {
4199 if (orinoco_lock(priv, &flags) != 0)
4200 return -EBUSY;
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07004201 memcpy(essidbuf, priv->desired_essid, IW_ESSID_MAX_SIZE);
4202 erq->length = strlen(priv->desired_essid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 orinoco_unlock(priv, &flags);
4204 }
4205
4206 erq->flags = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 return 0;
4209}
4210
Christoph Hellwig620554e2005-06-19 01:27:33 +02004211static int orinoco_ioctl_setnick(struct net_device *dev,
4212 struct iw_request_info *info,
4213 struct iw_point *nrq,
4214 char *nickbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215{
4216 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 unsigned long flags;
4218
4219 if (nrq->length > IW_ESSID_MAX_SIZE)
4220 return -E2BIG;
4221
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 if (orinoco_lock(priv, &flags) != 0)
4223 return -EBUSY;
4224
Christoph Hellwig620554e2005-06-19 01:27:33 +02004225 memset(priv->nick, 0, sizeof(priv->nick));
4226 memcpy(priv->nick, nickbuf, nrq->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
4228 orinoco_unlock(priv, &flags);
4229
Christoph Hellwig620554e2005-06-19 01:27:33 +02004230 return -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231}
4232
Christoph Hellwig620554e2005-06-19 01:27:33 +02004233static int orinoco_ioctl_getnick(struct net_device *dev,
4234 struct iw_request_info *info,
4235 struct iw_point *nrq,
4236 char *nickbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237{
4238 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 unsigned long flags;
4240
4241 if (orinoco_lock(priv, &flags) != 0)
4242 return -EBUSY;
4243
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07004244 memcpy(nickbuf, priv->nick, IW_ESSID_MAX_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 orinoco_unlock(priv, &flags);
4246
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07004247 nrq->length = strlen(priv->nick);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 return 0;
4250}
4251
Christoph Hellwig620554e2005-06-19 01:27:33 +02004252static int orinoco_ioctl_setfreq(struct net_device *dev,
4253 struct iw_request_info *info,
4254 struct iw_freq *frq,
4255 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256{
4257 struct orinoco_private *priv = netdev_priv(dev);
4258 int chan = -1;
4259 unsigned long flags;
Christoph Hellwig620554e2005-06-19 01:27:33 +02004260 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02004262 /* In infrastructure mode the AP sets the channel */
4263 if (priv->iw_mode == IW_MODE_INFRA)
4264 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265
4266 if ( (frq->e == 0) && (frq->m <= 1000) ) {
4267 /* Setting by channel number */
4268 chan = frq->m;
4269 } else {
4270 /* Setting by frequency - search the table */
4271 int mult = 1;
4272 int i;
4273
4274 for (i = 0; i < (6 - frq->e); i++)
4275 mult *= 10;
4276
4277 for (i = 0; i < NUM_CHANNELS; i++)
4278 if (frq->m == (channel_frequency[i] * mult))
4279 chan = i+1;
4280 }
4281
4282 if ( (chan < 1) || (chan > NUM_CHANNELS) ||
4283 ! (priv->channel_mask & (1 << (chan-1)) ) )
4284 return -EINVAL;
4285
4286 if (orinoco_lock(priv, &flags) != 0)
4287 return -EBUSY;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02004288
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 priv->channel = chan;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02004290 if (priv->iw_mode == IW_MODE_MONITOR) {
4291 /* Fast channel change - no commit if successful */
4292 hermes_t *hw = &priv->hw;
4293 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
4294 HERMES_TEST_SET_CHANNEL,
4295 chan, NULL);
4296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 orinoco_unlock(priv, &flags);
4298
Christoph Hellwig620554e2005-06-19 01:27:33 +02004299 return err;
4300}
4301
4302static int orinoco_ioctl_getfreq(struct net_device *dev,
4303 struct iw_request_info *info,
4304 struct iw_freq *frq,
4305 char *extra)
4306{
4307 struct orinoco_private *priv = netdev_priv(dev);
4308 int tmp;
4309
4310 /* Locking done in there */
4311 tmp = orinoco_hw_get_freq(priv);
4312 if (tmp < 0) {
4313 return tmp;
4314 }
4315
4316 frq->m = tmp;
4317 frq->e = 1;
4318
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 return 0;
4320}
4321
Christoph Hellwig620554e2005-06-19 01:27:33 +02004322static int orinoco_ioctl_getsens(struct net_device *dev,
4323 struct iw_request_info *info,
4324 struct iw_param *srq,
4325 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326{
4327 struct orinoco_private *priv = netdev_priv(dev);
4328 hermes_t *hw = &priv->hw;
4329 u16 val;
4330 int err;
4331 unsigned long flags;
4332
4333 if (!priv->has_sensitivity)
4334 return -EOPNOTSUPP;
4335
4336 if (orinoco_lock(priv, &flags) != 0)
4337 return -EBUSY;
4338 err = hermes_read_wordrec(hw, USER_BAP,
4339 HERMES_RID_CNFSYSTEMSCALE, &val);
4340 orinoco_unlock(priv, &flags);
4341
4342 if (err)
4343 return err;
4344
4345 srq->value = val;
4346 srq->fixed = 0; /* auto */
4347
4348 return 0;
4349}
4350
Christoph Hellwig620554e2005-06-19 01:27:33 +02004351static int orinoco_ioctl_setsens(struct net_device *dev,
4352 struct iw_request_info *info,
4353 struct iw_param *srq,
4354 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355{
4356 struct orinoco_private *priv = netdev_priv(dev);
4357 int val = srq->value;
4358 unsigned long flags;
4359
4360 if (!priv->has_sensitivity)
4361 return -EOPNOTSUPP;
4362
4363 if ((val < 1) || (val > 3))
4364 return -EINVAL;
4365
4366 if (orinoco_lock(priv, &flags) != 0)
4367 return -EBUSY;
4368 priv->ap_density = val;
4369 orinoco_unlock(priv, &flags);
4370
Christoph Hellwig620554e2005-06-19 01:27:33 +02004371 return -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372}
4373
Christoph Hellwig620554e2005-06-19 01:27:33 +02004374static int orinoco_ioctl_setrts(struct net_device *dev,
4375 struct iw_request_info *info,
4376 struct iw_param *rrq,
4377 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378{
4379 struct orinoco_private *priv = netdev_priv(dev);
4380 int val = rrq->value;
4381 unsigned long flags;
4382
4383 if (rrq->disabled)
4384 val = 2347;
4385
4386 if ( (val < 0) || (val > 2347) )
4387 return -EINVAL;
4388
4389 if (orinoco_lock(priv, &flags) != 0)
4390 return -EBUSY;
4391
4392 priv->rts_thresh = val;
4393 orinoco_unlock(priv, &flags);
4394
Christoph Hellwig620554e2005-06-19 01:27:33 +02004395 return -EINPROGRESS; /* Call commit handler */
4396}
4397
4398static int orinoco_ioctl_getrts(struct net_device *dev,
4399 struct iw_request_info *info,
4400 struct iw_param *rrq,
4401 char *extra)
4402{
4403 struct orinoco_private *priv = netdev_priv(dev);
4404
4405 rrq->value = priv->rts_thresh;
4406 rrq->disabled = (rrq->value == 2347);
4407 rrq->fixed = 1;
4408
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 return 0;
4410}
4411
Christoph Hellwig620554e2005-06-19 01:27:33 +02004412static int orinoco_ioctl_setfrag(struct net_device *dev,
4413 struct iw_request_info *info,
4414 struct iw_param *frq,
4415 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416{
4417 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004418 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 unsigned long flags;
4420
4421 if (orinoco_lock(priv, &flags) != 0)
4422 return -EBUSY;
4423
4424 if (priv->has_mwo) {
4425 if (frq->disabled)
4426 priv->mwo_robust = 0;
4427 else {
4428 if (frq->fixed)
4429 printk(KERN_WARNING "%s: Fixed fragmentation is "
4430 "not supported on this firmware. "
4431 "Using MWO robust instead.\n", dev->name);
4432 priv->mwo_robust = 1;
4433 }
4434 } else {
4435 if (frq->disabled)
4436 priv->frag_thresh = 2346;
4437 else {
4438 if ( (frq->value < 256) || (frq->value > 2346) )
4439 err = -EINVAL;
4440 else
4441 priv->frag_thresh = frq->value & ~0x1; /* must be even */
4442 }
4443 }
4444
4445 orinoco_unlock(priv, &flags);
4446
4447 return err;
4448}
4449
Christoph Hellwig620554e2005-06-19 01:27:33 +02004450static int orinoco_ioctl_getfrag(struct net_device *dev,
4451 struct iw_request_info *info,
4452 struct iw_param *frq,
4453 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454{
4455 struct orinoco_private *priv = netdev_priv(dev);
4456 hermes_t *hw = &priv->hw;
Christoph Hellwig620554e2005-06-19 01:27:33 +02004457 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 u16 val;
4459 unsigned long flags;
4460
4461 if (orinoco_lock(priv, &flags) != 0)
4462 return -EBUSY;
4463
4464 if (priv->has_mwo) {
4465 err = hermes_read_wordrec(hw, USER_BAP,
4466 HERMES_RID_CNFMWOROBUST_AGERE,
4467 &val);
4468 if (err)
4469 val = 0;
4470
4471 frq->value = val ? 2347 : 0;
4472 frq->disabled = ! val;
4473 frq->fixed = 0;
4474 } else {
4475 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
4476 &val);
4477 if (err)
4478 val = 0;
4479
4480 frq->value = val;
4481 frq->disabled = (val >= 2346);
4482 frq->fixed = 1;
4483 }
4484
4485 orinoco_unlock(priv, &flags);
4486
4487 return err;
4488}
4489
Christoph Hellwig620554e2005-06-19 01:27:33 +02004490static int orinoco_ioctl_setrate(struct net_device *dev,
4491 struct iw_request_info *info,
4492 struct iw_param *rrq,
4493 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494{
4495 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 int ratemode = -1;
4497 int bitrate; /* 100s of kilobits */
4498 int i;
4499 unsigned long flags;
4500
4501 /* As the user space doesn't know our highest rate, it uses -1
4502 * to ask us to set the highest rate. Test it using "iwconfig
4503 * ethX rate auto" - Jean II */
4504 if (rrq->value == -1)
4505 bitrate = 110;
4506 else {
4507 if (rrq->value % 100000)
4508 return -EINVAL;
4509 bitrate = rrq->value / 100000;
4510 }
4511
4512 if ( (bitrate != 10) && (bitrate != 20) &&
4513 (bitrate != 55) && (bitrate != 110) )
4514 return -EINVAL;
4515
4516 for (i = 0; i < BITRATE_TABLE_SIZE; i++)
4517 if ( (bitrate_table[i].bitrate == bitrate) &&
4518 (bitrate_table[i].automatic == ! rrq->fixed) ) {
4519 ratemode = i;
4520 break;
4521 }
4522
4523 if (ratemode == -1)
4524 return -EINVAL;
4525
4526 if (orinoco_lock(priv, &flags) != 0)
4527 return -EBUSY;
4528 priv->bitratemode = ratemode;
4529 orinoco_unlock(priv, &flags);
4530
Christoph Hellwig620554e2005-06-19 01:27:33 +02004531 return -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532}
4533
Christoph Hellwig620554e2005-06-19 01:27:33 +02004534static int orinoco_ioctl_getrate(struct net_device *dev,
4535 struct iw_request_info *info,
4536 struct iw_param *rrq,
4537 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538{
4539 struct orinoco_private *priv = netdev_priv(dev);
4540 hermes_t *hw = &priv->hw;
4541 int err = 0;
4542 int ratemode;
4543 int i;
4544 u16 val;
4545 unsigned long flags;
4546
4547 if (orinoco_lock(priv, &flags) != 0)
4548 return -EBUSY;
4549
4550 ratemode = priv->bitratemode;
4551
4552 BUG_ON((ratemode < 0) || (ratemode >= BITRATE_TABLE_SIZE));
4553
4554 rrq->value = bitrate_table[ratemode].bitrate * 100000;
4555 rrq->fixed = ! bitrate_table[ratemode].automatic;
4556 rrq->disabled = 0;
4557
4558 /* If the interface is running we try to find more about the
4559 current mode */
4560 if (netif_running(dev)) {
4561 err = hermes_read_wordrec(hw, USER_BAP,
4562 HERMES_RID_CURRENTTXRATE, &val);
4563 if (err)
4564 goto out;
4565
4566 switch (priv->firmware_type) {
4567 case FIRMWARE_TYPE_AGERE: /* Lucent style rate */
4568 /* Note : in Lucent firmware, the return value of
4569 * HERMES_RID_CURRENTTXRATE is the bitrate in Mb/s,
4570 * and therefore is totally different from the
4571 * encoding of HERMES_RID_CNFTXRATECONTROL.
4572 * Don't forget that 6Mb/s is really 5.5Mb/s */
4573 if (val == 6)
4574 rrq->value = 5500000;
4575 else
4576 rrq->value = val * 1000000;
4577 break;
4578 case FIRMWARE_TYPE_INTERSIL: /* Intersil style rate */
4579 case FIRMWARE_TYPE_SYMBOL: /* Symbol style rate */
4580 for (i = 0; i < BITRATE_TABLE_SIZE; i++)
4581 if (bitrate_table[i].intersil_txratectrl == val) {
4582 ratemode = i;
4583 break;
4584 }
4585 if (i >= BITRATE_TABLE_SIZE)
4586 printk(KERN_INFO "%s: Unable to determine current bitrate (0x%04hx)\n",
4587 dev->name, val);
4588
4589 rrq->value = bitrate_table[ratemode].bitrate * 100000;
4590 break;
4591 default:
4592 BUG();
4593 }
4594 }
4595
4596 out:
4597 orinoco_unlock(priv, &flags);
4598
4599 return err;
4600}
4601
Christoph Hellwig620554e2005-06-19 01:27:33 +02004602static int orinoco_ioctl_setpower(struct net_device *dev,
4603 struct iw_request_info *info,
4604 struct iw_param *prq,
4605 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606{
4607 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004608 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 unsigned long flags;
4610
4611 if (orinoco_lock(priv, &flags) != 0)
4612 return -EBUSY;
4613
4614 if (prq->disabled) {
4615 priv->pm_on = 0;
4616 } else {
4617 switch (prq->flags & IW_POWER_MODE) {
4618 case IW_POWER_UNICAST_R:
4619 priv->pm_mcast = 0;
4620 priv->pm_on = 1;
4621 break;
4622 case IW_POWER_ALL_R:
4623 priv->pm_mcast = 1;
4624 priv->pm_on = 1;
4625 break;
4626 case IW_POWER_ON:
4627 /* No flags : but we may have a value - Jean II */
4628 break;
4629 default:
4630 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 goto out;
Pavel Roskinc08ad1e2005-11-29 02:59:27 -05004632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633
4634 if (prq->flags & IW_POWER_TIMEOUT) {
4635 priv->pm_on = 1;
4636 priv->pm_timeout = prq->value / 1000;
4637 }
4638 if (prq->flags & IW_POWER_PERIOD) {
4639 priv->pm_on = 1;
4640 priv->pm_period = prq->value / 1000;
4641 }
4642 /* It's valid to not have a value if we are just toggling
4643 * the flags... Jean II */
4644 if(!priv->pm_on) {
4645 err = -EINVAL;
4646 goto out;
4647 }
4648 }
4649
4650 out:
4651 orinoco_unlock(priv, &flags);
4652
4653 return err;
4654}
4655
Christoph Hellwig620554e2005-06-19 01:27:33 +02004656static int orinoco_ioctl_getpower(struct net_device *dev,
4657 struct iw_request_info *info,
4658 struct iw_param *prq,
4659 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660{
4661 struct orinoco_private *priv = netdev_priv(dev);
4662 hermes_t *hw = &priv->hw;
4663 int err = 0;
4664 u16 enable, period, timeout, mcast;
4665 unsigned long flags;
4666
4667 if (orinoco_lock(priv, &flags) != 0)
4668 return -EBUSY;
4669
4670 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFPMENABLED, &enable);
4671 if (err)
4672 goto out;
4673
4674 err = hermes_read_wordrec(hw, USER_BAP,
4675 HERMES_RID_CNFMAXSLEEPDURATION, &period);
4676 if (err)
4677 goto out;
4678
4679 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFPMHOLDOVERDURATION, &timeout);
4680 if (err)
4681 goto out;
4682
4683 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFMULTICASTRECEIVE, &mcast);
4684 if (err)
4685 goto out;
4686
4687 prq->disabled = !enable;
4688 /* Note : by default, display the period */
4689 if ((prq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
4690 prq->flags = IW_POWER_TIMEOUT;
4691 prq->value = timeout * 1000;
4692 } else {
4693 prq->flags = IW_POWER_PERIOD;
4694 prq->value = period * 1000;
4695 }
4696 if (mcast)
4697 prq->flags |= IW_POWER_ALL_R;
4698 else
4699 prq->flags |= IW_POWER_UNICAST_R;
4700
4701 out:
4702 orinoco_unlock(priv, &flags);
4703
4704 return err;
4705}
4706
David Kilroyd03032a2008-08-21 23:28:02 +01004707static int orinoco_ioctl_set_encodeext(struct net_device *dev,
4708 struct iw_request_info *info,
4709 union iwreq_data *wrqu,
4710 char *extra)
4711{
4712 struct orinoco_private *priv = netdev_priv(dev);
4713 struct iw_point *encoding = &wrqu->encoding;
4714 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
4715 int idx, alg = ext->alg, set_key = 1;
4716 unsigned long flags;
4717 int err = -EINVAL;
4718 u16 key_len;
4719
4720 if (orinoco_lock(priv, &flags) != 0)
4721 return -EBUSY;
4722
4723 /* Determine and validate the key index */
4724 idx = encoding->flags & IW_ENCODE_INDEX;
4725 if (idx) {
Johannes Berg2c7060022008-10-30 22:09:54 +01004726 if ((idx < 1) || (idx > 4))
David Kilroyd03032a2008-08-21 23:28:02 +01004727 goto out;
4728 idx--;
4729 } else
4730 idx = priv->tx_key;
4731
4732 if (encoding->flags & IW_ENCODE_DISABLED)
4733 alg = IW_ENCODE_ALG_NONE;
4734
4735 if (priv->has_wpa && (alg != IW_ENCODE_ALG_TKIP)) {
4736 /* Clear any TKIP TX key we had */
4737 (void) orinoco_clear_tkip_key(priv, priv->tx_key);
4738 }
4739
4740 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
4741 priv->tx_key = idx;
4742 set_key = ((alg == IW_ENCODE_ALG_TKIP) ||
4743 (ext->key_len > 0)) ? 1 : 0;
4744 }
4745
4746 if (set_key) {
4747 /* Set the requested key first */
4748 switch (alg) {
4749 case IW_ENCODE_ALG_NONE:
4750 priv->encode_alg = alg;
4751 priv->keys[idx].len = 0;
4752 break;
4753
4754 case IW_ENCODE_ALG_WEP:
4755 if (ext->key_len > SMALL_KEY_SIZE)
4756 key_len = LARGE_KEY_SIZE;
4757 else if (ext->key_len > 0)
4758 key_len = SMALL_KEY_SIZE;
4759 else
4760 goto out;
4761
4762 priv->encode_alg = alg;
4763 priv->keys[idx].len = cpu_to_le16(key_len);
4764
4765 key_len = min(ext->key_len, key_len);
4766
4767 memset(priv->keys[idx].data, 0, ORINOCO_MAX_KEY_SIZE);
4768 memcpy(priv->keys[idx].data, ext->key, key_len);
4769 break;
4770
4771 case IW_ENCODE_ALG_TKIP:
4772 {
4773 hermes_t *hw = &priv->hw;
4774 u8 *tkip_iv = NULL;
4775
4776 if (!priv->has_wpa ||
4777 (ext->key_len > sizeof(priv->tkip_key[0])))
4778 goto out;
4779
4780 priv->encode_alg = alg;
4781 memset(&priv->tkip_key[idx], 0,
4782 sizeof(priv->tkip_key[idx]));
4783 memcpy(&priv->tkip_key[idx], ext->key, ext->key_len);
4784
4785 if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID)
4786 tkip_iv = &ext->rx_seq[0];
4787
4788 err = __orinoco_hw_set_tkip_key(hw, idx,
4789 ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY,
4790 (u8 *) &priv->tkip_key[idx],
4791 tkip_iv, NULL);
4792 if (err)
4793 printk(KERN_ERR "%s: Error %d setting TKIP key"
4794 "\n", dev->name, err);
4795
4796 goto out;
4797 }
4798 default:
4799 goto out;
4800 }
4801 }
4802 err = -EINPROGRESS;
4803 out:
4804 orinoco_unlock(priv, &flags);
4805
4806 return err;
4807}
4808
4809static int orinoco_ioctl_get_encodeext(struct net_device *dev,
4810 struct iw_request_info *info,
4811 union iwreq_data *wrqu,
4812 char *extra)
4813{
4814 struct orinoco_private *priv = netdev_priv(dev);
4815 struct iw_point *encoding = &wrqu->encoding;
4816 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
4817 int idx, max_key_len;
4818 unsigned long flags;
4819 int err;
4820
4821 if (orinoco_lock(priv, &flags) != 0)
4822 return -EBUSY;
4823
4824 err = -EINVAL;
4825 max_key_len = encoding->length - sizeof(*ext);
4826 if (max_key_len < 0)
4827 goto out;
4828
4829 idx = encoding->flags & IW_ENCODE_INDEX;
4830 if (idx) {
Johannes Berg2c7060022008-10-30 22:09:54 +01004831 if ((idx < 1) || (idx > 4))
David Kilroyd03032a2008-08-21 23:28:02 +01004832 goto out;
4833 idx--;
4834 } else
4835 idx = priv->tx_key;
4836
4837 encoding->flags = idx + 1;
4838 memset(ext, 0, sizeof(*ext));
4839
4840 ext->alg = priv->encode_alg;
4841 switch (priv->encode_alg) {
4842 case IW_ENCODE_ALG_NONE:
4843 ext->key_len = 0;
4844 encoding->flags |= IW_ENCODE_DISABLED;
4845 break;
4846 case IW_ENCODE_ALG_WEP:
David Kilroy75d31cf2008-09-12 22:28:18 +01004847 ext->key_len = min_t(u16, le16_to_cpu(priv->keys[idx].len),
4848 max_key_len);
David Kilroyd03032a2008-08-21 23:28:02 +01004849 memcpy(ext->key, priv->keys[idx].data, ext->key_len);
4850 encoding->flags |= IW_ENCODE_ENABLED;
4851 break;
4852 case IW_ENCODE_ALG_TKIP:
David Kilroy75d31cf2008-09-12 22:28:18 +01004853 ext->key_len = min_t(u16, sizeof(struct orinoco_tkip_key),
4854 max_key_len);
David Kilroyd03032a2008-08-21 23:28:02 +01004855 memcpy(ext->key, &priv->tkip_key[idx], ext->key_len);
4856 encoding->flags |= IW_ENCODE_ENABLED;
4857 break;
4858 }
4859
4860 err = 0;
4861 out:
4862 orinoco_unlock(priv, &flags);
4863
4864 return err;
4865}
4866
4867static int orinoco_ioctl_set_auth(struct net_device *dev,
4868 struct iw_request_info *info,
4869 union iwreq_data *wrqu, char *extra)
4870{
4871 struct orinoco_private *priv = netdev_priv(dev);
4872 hermes_t *hw = &priv->hw;
4873 struct iw_param *param = &wrqu->param;
4874 unsigned long flags;
4875 int ret = -EINPROGRESS;
4876
4877 if (orinoco_lock(priv, &flags) != 0)
4878 return -EBUSY;
4879
4880 switch (param->flags & IW_AUTH_INDEX) {
4881 case IW_AUTH_WPA_VERSION:
4882 case IW_AUTH_CIPHER_PAIRWISE:
4883 case IW_AUTH_CIPHER_GROUP:
4884 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
4885 case IW_AUTH_PRIVACY_INVOKED:
4886 case IW_AUTH_DROP_UNENCRYPTED:
4887 /*
4888 * orinoco does not use these parameters
4889 */
4890 break;
4891
4892 case IW_AUTH_KEY_MGMT:
4893 /* wl_lkm implies value 2 == PSK for Hermes I
4894 * which ties in with WEXT
4895 * no other hints tho :(
4896 */
4897 priv->key_mgmt = param->value;
4898 break;
4899
4900 case IW_AUTH_TKIP_COUNTERMEASURES:
4901 /* When countermeasures are enabled, shut down the
4902 * card; when disabled, re-enable the card. This must
4903 * take effect immediately.
4904 *
4905 * TODO: Make sure that the EAPOL message is getting
4906 * out before card disabled
4907 */
4908 if (param->value) {
4909 priv->tkip_cm_active = 1;
4910 ret = hermes_enable_port(hw, 0);
4911 } else {
4912 priv->tkip_cm_active = 0;
4913 ret = hermes_disable_port(hw, 0);
4914 }
4915 break;
4916
4917 case IW_AUTH_80211_AUTH_ALG:
4918 if (param->value & IW_AUTH_ALG_SHARED_KEY)
4919 priv->wep_restrict = 1;
4920 else if (param->value & IW_AUTH_ALG_OPEN_SYSTEM)
4921 priv->wep_restrict = 0;
4922 else
4923 ret = -EINVAL;
4924 break;
4925
4926 case IW_AUTH_WPA_ENABLED:
4927 if (priv->has_wpa) {
4928 priv->wpa_enabled = param->value ? 1 : 0;
4929 } else {
4930 if (param->value)
4931 ret = -EOPNOTSUPP;
4932 /* else silently accept disable of WPA */
4933 priv->wpa_enabled = 0;
4934 }
4935 break;
4936
4937 default:
4938 ret = -EOPNOTSUPP;
4939 }
4940
4941 orinoco_unlock(priv, &flags);
4942 return ret;
4943}
4944
4945static int orinoco_ioctl_get_auth(struct net_device *dev,
4946 struct iw_request_info *info,
4947 union iwreq_data *wrqu, char *extra)
4948{
4949 struct orinoco_private *priv = netdev_priv(dev);
4950 struct iw_param *param = &wrqu->param;
4951 unsigned long flags;
4952 int ret = 0;
4953
4954 if (orinoco_lock(priv, &flags) != 0)
4955 return -EBUSY;
4956
4957 switch (param->flags & IW_AUTH_INDEX) {
4958 case IW_AUTH_KEY_MGMT:
4959 param->value = priv->key_mgmt;
4960 break;
4961
4962 case IW_AUTH_TKIP_COUNTERMEASURES:
4963 param->value = priv->tkip_cm_active;
4964 break;
4965
4966 case IW_AUTH_80211_AUTH_ALG:
4967 if (priv->wep_restrict)
4968 param->value = IW_AUTH_ALG_SHARED_KEY;
4969 else
4970 param->value = IW_AUTH_ALG_OPEN_SYSTEM;
4971 break;
4972
4973 case IW_AUTH_WPA_ENABLED:
4974 param->value = priv->wpa_enabled;
4975 break;
4976
4977 default:
4978 ret = -EOPNOTSUPP;
4979 }
4980
4981 orinoco_unlock(priv, &flags);
4982 return ret;
4983}
4984
4985static int orinoco_ioctl_set_genie(struct net_device *dev,
4986 struct iw_request_info *info,
4987 union iwreq_data *wrqu, char *extra)
4988{
4989 struct orinoco_private *priv = netdev_priv(dev);
4990 u8 *buf;
4991 unsigned long flags;
4992 int err = 0;
4993
Johannes Berg2c7060022008-10-30 22:09:54 +01004994 /* cut off at IEEE80211_MAX_DATA_LEN */
4995 if ((wrqu->data.length > IEEE80211_MAX_DATA_LEN) ||
David Kilroyd03032a2008-08-21 23:28:02 +01004996 (wrqu->data.length && (extra == NULL)))
4997 return -EINVAL;
4998
4999 if (orinoco_lock(priv, &flags) != 0)
5000 return -EBUSY;
5001
5002 if (wrqu->data.length) {
5003 buf = kmalloc(wrqu->data.length, GFP_KERNEL);
5004 if (buf == NULL) {
5005 err = -ENOMEM;
5006 goto out;
5007 }
5008
5009 memcpy(buf, extra, wrqu->data.length);
5010 kfree(priv->wpa_ie);
5011 priv->wpa_ie = buf;
5012 priv->wpa_ie_len = wrqu->data.length;
5013 } else {
5014 kfree(priv->wpa_ie);
5015 priv->wpa_ie = NULL;
5016 priv->wpa_ie_len = 0;
5017 }
5018
5019 if (priv->wpa_ie) {
5020 /* Looks like wl_lkm wants to check the auth alg, and
5021 * somehow pass it to the firmware.
5022 * Instead it just calls the key mgmt rid
5023 * - we do this in set auth.
5024 */
5025 }
5026
5027out:
5028 orinoco_unlock(priv, &flags);
5029 return err;
5030}
5031
5032static int orinoco_ioctl_get_genie(struct net_device *dev,
5033 struct iw_request_info *info,
5034 union iwreq_data *wrqu, char *extra)
5035{
5036 struct orinoco_private *priv = netdev_priv(dev);
5037 unsigned long flags;
5038 int err = 0;
5039
5040 if (orinoco_lock(priv, &flags) != 0)
5041 return -EBUSY;
5042
5043 if ((priv->wpa_ie_len == 0) || (priv->wpa_ie == NULL)) {
5044 wrqu->data.length = 0;
5045 goto out;
5046 }
5047
5048 if (wrqu->data.length < priv->wpa_ie_len) {
5049 err = -E2BIG;
5050 goto out;
5051 }
5052
5053 wrqu->data.length = priv->wpa_ie_len;
5054 memcpy(extra, priv->wpa_ie, priv->wpa_ie_len);
5055
5056out:
5057 orinoco_unlock(priv, &flags);
5058 return err;
5059}
5060
5061static int orinoco_ioctl_set_mlme(struct net_device *dev,
5062 struct iw_request_info *info,
5063 union iwreq_data *wrqu, char *extra)
5064{
5065 struct orinoco_private *priv = netdev_priv(dev);
5066 hermes_t *hw = &priv->hw;
5067 struct iw_mlme *mlme = (struct iw_mlme *)extra;
5068 unsigned long flags;
5069 int ret = 0;
5070
5071 if (orinoco_lock(priv, &flags) != 0)
5072 return -EBUSY;
5073
5074 switch (mlme->cmd) {
5075 case IW_MLME_DEAUTH:
5076 /* silently ignore */
5077 break;
5078
5079 case IW_MLME_DISASSOC:
5080 {
5081 struct {
5082 u8 addr[ETH_ALEN];
5083 __le16 reason_code;
5084 } __attribute__ ((packed)) buf;
5085
5086 memcpy(buf.addr, mlme->addr.sa_data, ETH_ALEN);
5087 buf.reason_code = cpu_to_le16(mlme->reason_code);
5088 ret = HERMES_WRITE_RECORD(hw, USER_BAP,
5089 HERMES_RID_CNFDISASSOCIATE,
5090 &buf);
5091 break;
5092 }
5093 default:
5094 ret = -EOPNOTSUPP;
5095 }
5096
5097 orinoco_unlock(priv, &flags);
5098 return ret;
5099}
5100
Christoph Hellwig620554e2005-06-19 01:27:33 +02005101static int orinoco_ioctl_getretry(struct net_device *dev,
5102 struct iw_request_info *info,
5103 struct iw_param *rrq,
5104 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105{
5106 struct orinoco_private *priv = netdev_priv(dev);
5107 hermes_t *hw = &priv->hw;
5108 int err = 0;
5109 u16 short_limit, long_limit, lifetime;
5110 unsigned long flags;
5111
5112 if (orinoco_lock(priv, &flags) != 0)
5113 return -EBUSY;
5114
5115 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_SHORTRETRYLIMIT,
5116 &short_limit);
5117 if (err)
5118 goto out;
5119
5120 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_LONGRETRYLIMIT,
5121 &long_limit);
5122 if (err)
5123 goto out;
5124
5125 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_MAXTRANSMITLIFETIME,
5126 &lifetime);
5127 if (err)
5128 goto out;
5129
5130 rrq->disabled = 0; /* Can't be disabled */
5131
5132 /* Note : by default, display the retry number */
5133 if ((rrq->flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME) {
5134 rrq->flags = IW_RETRY_LIFETIME;
5135 rrq->value = lifetime * 1000; /* ??? */
5136 } else {
5137 /* By default, display the min number */
Jean Tourrilheseeec9f12006-08-29 18:02:31 -07005138 if ((rrq->flags & IW_RETRY_LONG)) {
5139 rrq->flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 rrq->value = long_limit;
5141 } else {
5142 rrq->flags = IW_RETRY_LIMIT;
5143 rrq->value = short_limit;
5144 if(short_limit != long_limit)
Jean Tourrilheseeec9f12006-08-29 18:02:31 -07005145 rrq->flags |= IW_RETRY_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 }
5147 }
5148
5149 out:
5150 orinoco_unlock(priv, &flags);
5151
5152 return err;
5153}
5154
Christoph Hellwig620554e2005-06-19 01:27:33 +02005155static int orinoco_ioctl_reset(struct net_device *dev,
5156 struct iw_request_info *info,
5157 void *wrqu,
5158 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159{
5160 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005161
5162 if (! capable(CAP_NET_ADMIN))
5163 return -EPERM;
5164
5165 if (info->cmd == (SIOCIWFIRSTPRIV + 0x1)) {
5166 printk(KERN_DEBUG "%s: Forcing reset!\n", dev->name);
5167
5168 /* Firmware reset */
David Howellsc4028952006-11-22 14:57:56 +00005169 orinoco_reset(&priv->reset_work);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005170 } else {
5171 printk(KERN_DEBUG "%s: Force scheduling reset!\n", dev->name);
5172
5173 schedule_work(&priv->reset_work);
5174 }
5175
5176 return 0;
5177}
5178
5179static int orinoco_ioctl_setibssport(struct net_device *dev,
5180 struct iw_request_info *info,
5181 void *wrqu,
5182 char *extra)
5183
5184{
5185 struct orinoco_private *priv = netdev_priv(dev);
5186 int val = *( (int *) extra );
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 unsigned long flags;
5188
5189 if (orinoco_lock(priv, &flags) != 0)
5190 return -EBUSY;
5191
5192 priv->ibss_port = val ;
5193
5194 /* Actually update the mode we are using */
5195 set_port_type(priv);
5196
5197 orinoco_unlock(priv, &flags);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005198 return -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199}
5200
Christoph Hellwig620554e2005-06-19 01:27:33 +02005201static int orinoco_ioctl_getibssport(struct net_device *dev,
5202 struct iw_request_info *info,
5203 void *wrqu,
5204 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205{
5206 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005207 int *val = (int *) extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208
5209 *val = priv->ibss_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 return 0;
5211}
5212
Christoph Hellwig620554e2005-06-19 01:27:33 +02005213static int orinoco_ioctl_setport3(struct net_device *dev,
5214 struct iw_request_info *info,
5215 void *wrqu,
5216 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217{
5218 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005219 int val = *( (int *) extra );
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 int err = 0;
5221 unsigned long flags;
5222
5223 if (orinoco_lock(priv, &flags) != 0)
5224 return -EBUSY;
5225
5226 switch (val) {
5227 case 0: /* Try to do IEEE ad-hoc mode */
5228 if (! priv->has_ibss) {
5229 err = -EINVAL;
5230 break;
5231 }
5232 priv->prefer_port3 = 0;
5233
5234 break;
5235
5236 case 1: /* Try to do Lucent proprietary ad-hoc mode */
5237 if (! priv->has_port3) {
5238 err = -EINVAL;
5239 break;
5240 }
5241 priv->prefer_port3 = 1;
5242 break;
5243
5244 default:
5245 err = -EINVAL;
5246 }
5247
Christoph Hellwig620554e2005-06-19 01:27:33 +02005248 if (! err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 /* Actually update the mode we are using */
5250 set_port_type(priv);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005251 err = -EINPROGRESS;
5252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253
5254 orinoco_unlock(priv, &flags);
5255
5256 return err;
5257}
5258
Christoph Hellwig620554e2005-06-19 01:27:33 +02005259static int orinoco_ioctl_getport3(struct net_device *dev,
5260 struct iw_request_info *info,
5261 void *wrqu,
5262 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263{
5264 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005265 int *val = (int *) extra;
5266
5267 *val = priv->prefer_port3;
5268 return 0;
5269}
5270
5271static int orinoco_ioctl_setpreamble(struct net_device *dev,
5272 struct iw_request_info *info,
5273 void *wrqu,
5274 char *extra)
5275{
5276 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 unsigned long flags;
Christoph Hellwig620554e2005-06-19 01:27:33 +02005278 int val;
5279
5280 if (! priv->has_preamble)
5281 return -EOPNOTSUPP;
5282
5283 /* 802.11b has recently defined some short preamble.
5284 * Basically, the Phy header has been reduced in size.
5285 * This increase performance, especially at high rates
5286 * (the preamble is transmitted at 1Mb/s), unfortunately
5287 * this give compatibility troubles... - Jean II */
5288 val = *( (int *) extra );
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289
5290 if (orinoco_lock(priv, &flags) != 0)
5291 return -EBUSY;
5292
Christoph Hellwig620554e2005-06-19 01:27:33 +02005293 if (val)
5294 priv->preamble = 1;
5295 else
5296 priv->preamble = 0;
5297
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 orinoco_unlock(priv, &flags);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005299
5300 return -EINPROGRESS; /* Call commit handler */
5301}
5302
5303static int orinoco_ioctl_getpreamble(struct net_device *dev,
5304 struct iw_request_info *info,
5305 void *wrqu,
5306 char *extra)
5307{
5308 struct orinoco_private *priv = netdev_priv(dev);
5309 int *val = (int *) extra;
5310
5311 if (! priv->has_preamble)
5312 return -EOPNOTSUPP;
5313
5314 *val = priv->preamble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 return 0;
5316}
5317
Christoph Hellwig620554e2005-06-19 01:27:33 +02005318/* ioctl interface to hermes_read_ltv()
5319 * To use with iwpriv, pass the RID as the token argument, e.g.
5320 * iwpriv get_rid [0xfc00]
5321 * At least Wireless Tools 25 is required to use iwpriv.
5322 * For Wireless Tools 25 and 26 append "dummy" are the end. */
5323static int orinoco_ioctl_getrid(struct net_device *dev,
5324 struct iw_request_info *info,
5325 struct iw_point *data,
5326 char *extra)
5327{
5328 struct orinoco_private *priv = netdev_priv(dev);
5329 hermes_t *hw = &priv->hw;
5330 int rid = data->flags;
5331 u16 length;
5332 int err;
5333 unsigned long flags;
5334
5335 /* It's a "get" function, but we don't want users to access the
5336 * WEP key and other raw firmware data */
5337 if (! capable(CAP_NET_ADMIN))
5338 return -EPERM;
5339
5340 if (rid < 0xfc00 || rid > 0xffff)
5341 return -EINVAL;
5342
5343 if (orinoco_lock(priv, &flags) != 0)
5344 return -EBUSY;
5345
5346 err = hermes_read_ltv(hw, USER_BAP, rid, MAX_RID_LEN, &length,
5347 extra);
5348 if (err)
5349 goto out;
5350
5351 data->length = min_t(u16, HERMES_RECLEN_TO_BYTES(length),
5352 MAX_RID_LEN);
5353
5354 out:
5355 orinoco_unlock(priv, &flags);
5356 return err;
5357}
5358
David Kilroy17a1a882008-08-21 23:27:47 +01005359/* Trigger a scan (look for other cells in the vicinity) */
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005360static int orinoco_ioctl_setscan(struct net_device *dev,
5361 struct iw_request_info *info,
David Kilroy9930cce2008-09-13 12:22:05 +01005362 struct iw_point *srq,
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005363 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364{
5365 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005366 hermes_t *hw = &priv->hw;
David Kilroy0753bba2008-08-21 23:27:46 +01005367 struct iw_scan_req *si = (struct iw_scan_req *) extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 unsigned long flags;
5370
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005371 /* Note : you may have realised that, as this is a SET operation,
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -08005372 * this is privileged and therefore a normal user can't
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005373 * perform scanning.
5374 * This is not an error, while the device perform scanning,
5375 * traffic doesn't flow, so it's a perfect DoS...
5376 * Jean II */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005378 if (orinoco_lock(priv, &flags) != 0)
5379 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005381 /* Scanning with port 0 disabled would fail */
5382 if (!netif_running(dev)) {
5383 err = -ENETDOWN;
5384 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005387 /* In monitor mode, the scan results are always empty.
5388 * Probe responses are passed to the driver as received
5389 * frames and could be processed in software. */
5390 if (priv->iw_mode == IW_MODE_MONITOR) {
5391 err = -EOPNOTSUPP;
5392 goto out;
5393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005395 /* Note : because we don't lock out the irq handler, the way
5396 * we access scan variables in priv is critical.
5397 * o scan_inprogress : not touched by irq handler
5398 * o scan_mode : not touched by irq handler
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005399 * Before modifying anything on those variables, please think hard !
5400 * Jean II */
5401
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005402 /* Save flags */
5403 priv->scan_mode = srq->flags;
5404
5405 /* Always trigger scanning, even if it's in progress.
5406 * This way, if the info frame get lost, we will recover somewhat
5407 * gracefully - Jean II */
5408
5409 if (priv->has_hostscan) {
5410 switch (priv->firmware_type) {
5411 case FIRMWARE_TYPE_SYMBOL:
5412 err = hermes_write_wordrec(hw, USER_BAP,
5413 HERMES_RID_CNFHOSTSCAN_SYMBOL,
5414 HERMES_HOSTSCAN_SYMBOL_ONCE |
5415 HERMES_HOSTSCAN_SYMBOL_BCAST);
5416 break;
5417 case FIRMWARE_TYPE_INTERSIL: {
Pavel Roskind133ae42005-09-23 04:18:06 -04005418 __le16 req[3];
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005419
5420 req[0] = cpu_to_le16(0x3fff); /* All channels */
5421 req[1] = cpu_to_le16(0x0001); /* rate 1 Mbps */
5422 req[2] = 0; /* Any ESSID */
5423 err = HERMES_WRITE_RECORD(hw, USER_BAP,
5424 HERMES_RID_CNFHOSTSCAN, &req);
5425 }
5426 break;
5427 case FIRMWARE_TYPE_AGERE:
David Kilroy0753bba2008-08-21 23:27:46 +01005428 if (priv->scan_mode & IW_SCAN_THIS_ESSID) {
5429 struct hermes_idstring idbuf;
5430 size_t len = min(sizeof(idbuf.val),
5431 (size_t) si->essid_len);
5432 idbuf.len = cpu_to_le16(len);
5433 memcpy(idbuf.val, si->essid, len);
5434
5435 err = hermes_write_ltv(hw, USER_BAP,
5436 HERMES_RID_CNFSCANSSID_AGERE,
5437 HERMES_BYTES_TO_RECLEN(len + 2),
5438 &idbuf);
5439 } else
5440 err = hermes_write_wordrec(hw, USER_BAP,
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005441 HERMES_RID_CNFSCANSSID_AGERE,
5442 0); /* Any ESSID */
5443 if (err)
5444 break;
5445
David Kilroy01632fa2008-08-21 23:27:58 +01005446 if (priv->has_ext_scan) {
5447 /* Clear scan results at the start of
5448 * an extended scan */
5449 orinoco_clear_scan_results(priv,
5450 msecs_to_jiffies(15000));
5451
5452 /* TODO: Is this available on older firmware?
5453 * Can we use it to scan specific channels
5454 * for IW_SCAN_THIS_FREQ? */
5455 err = hermes_write_wordrec(hw, USER_BAP,
5456 HERMES_RID_CNFSCANCHANNELS2GHZ,
5457 0x7FFF);
5458 if (err)
5459 goto out;
5460
5461 err = hermes_inquire(hw,
5462 HERMES_INQ_CHANNELINFO);
5463 } else
5464 err = hermes_inquire(hw, HERMES_INQ_SCAN);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005465 break;
5466 }
5467 } else
5468 err = hermes_inquire(hw, HERMES_INQ_SCAN);
5469
5470 /* One more client */
5471 if (! err)
5472 priv->scan_inprogress = 1;
5473
5474 out:
5475 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 return err;
5477}
5478
Dan Williams1e3428e2007-10-10 23:56:25 -04005479#define MAX_CUSTOM_LEN 64
5480
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005481/* Translate scan data returned from the card to a card independant
David Kilroy17a1a882008-08-21 23:27:47 +01005482 * format that the Wireless Tools will understand - Jean II */
Dan Williams1e3428e2007-10-10 23:56:25 -04005483static inline char *orinoco_translate_scan(struct net_device *dev,
David S. Millerccc58052008-06-16 18:50:49 -07005484 struct iw_request_info *info,
Dan Williams1e3428e2007-10-10 23:56:25 -04005485 char *current_ev,
5486 char *end_buf,
5487 union hermes_scan_info *bss,
Pavel Roskindfe1baf2008-11-10 09:25:53 -05005488 unsigned long last_scanned)
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005489{
5490 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005491 u16 capabilities;
5492 u16 channel;
5493 struct iw_event iwe; /* Temporary buffer */
Dan Williams1e3428e2007-10-10 23:56:25 -04005494 char custom[MAX_CUSTOM_LEN];
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005495
David Kilroy17a1a882008-08-21 23:27:47 +01005496 memset(&iwe, 0, sizeof(iwe));
5497
Dan Williams1e3428e2007-10-10 23:56:25 -04005498 /* First entry *MUST* be the AP MAC address */
5499 iwe.cmd = SIOCGIWAP;
5500 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
5501 memcpy(iwe.u.ap_addr.sa_data, bss->a.bssid, ETH_ALEN);
David S. Millerccc58052008-06-16 18:50:49 -07005502 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5503 &iwe, IW_EV_ADDR_LEN);
Dan Williams1e3428e2007-10-10 23:56:25 -04005504
5505 /* Other entries will be displayed in the order we give them */
5506
5507 /* Add the ESSID */
5508 iwe.u.data.length = le16_to_cpu(bss->a.essid_len);
5509 if (iwe.u.data.length > 32)
5510 iwe.u.data.length = 32;
5511 iwe.cmd = SIOCGIWESSID;
5512 iwe.u.data.flags = 1;
David S. Millerccc58052008-06-16 18:50:49 -07005513 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5514 &iwe, bss->a.essid);
Dan Williams1e3428e2007-10-10 23:56:25 -04005515
5516 /* Add mode */
5517 iwe.cmd = SIOCGIWMODE;
5518 capabilities = le16_to_cpu(bss->a.capabilities);
David Kilroy17a1a882008-08-21 23:27:47 +01005519 if (capabilities & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
5520 if (capabilities & WLAN_CAPABILITY_ESS)
Dan Williams1e3428e2007-10-10 23:56:25 -04005521 iwe.u.mode = IW_MODE_MASTER;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005522 else
Dan Williams1e3428e2007-10-10 23:56:25 -04005523 iwe.u.mode = IW_MODE_ADHOC;
David S. Millerccc58052008-06-16 18:50:49 -07005524 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5525 &iwe, IW_EV_UINT_LEN);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005526 }
5527
Dan Williams1e3428e2007-10-10 23:56:25 -04005528 channel = bss->s.channel;
5529 if ((channel >= 1) && (channel <= NUM_CHANNELS)) {
David Kilroy17a1a882008-08-21 23:27:47 +01005530 /* Add channel and frequency */
Dan Williams1e3428e2007-10-10 23:56:25 -04005531 iwe.cmd = SIOCGIWFREQ;
David Kilroy17a1a882008-08-21 23:27:47 +01005532 iwe.u.freq.m = channel;
5533 iwe.u.freq.e = 0;
5534 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5535 &iwe, IW_EV_FREQ_LEN);
5536
Dan Williams1e3428e2007-10-10 23:56:25 -04005537 iwe.u.freq.m = channel_frequency[channel-1] * 100000;
5538 iwe.u.freq.e = 1;
David S. Millerccc58052008-06-16 18:50:49 -07005539 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
Dan Williams1e3428e2007-10-10 23:56:25 -04005540 &iwe, IW_EV_FREQ_LEN);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005541 }
5542
David Kilroy17a1a882008-08-21 23:27:47 +01005543 /* Add quality statistics. level and noise in dB. No link quality */
Dan Williams1e3428e2007-10-10 23:56:25 -04005544 iwe.cmd = IWEVQUAL;
David Kilroy17a1a882008-08-21 23:27:47 +01005545 iwe.u.qual.updated = IW_QUAL_DBM | IW_QUAL_QUAL_INVALID;
Dan Williams1e3428e2007-10-10 23:56:25 -04005546 iwe.u.qual.level = (__u8) le16_to_cpu(bss->a.level) - 0x95;
5547 iwe.u.qual.noise = (__u8) le16_to_cpu(bss->a.noise) - 0x95;
5548 /* Wireless tools prior to 27.pre22 will show link quality
5549 * anyway, so we provide a reasonable value. */
5550 if (iwe.u.qual.level > iwe.u.qual.noise)
5551 iwe.u.qual.qual = iwe.u.qual.level - iwe.u.qual.noise;
5552 else
5553 iwe.u.qual.qual = 0;
David S. Millerccc58052008-06-16 18:50:49 -07005554 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5555 &iwe, IW_EV_QUAL_LEN);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005556
Dan Williams1e3428e2007-10-10 23:56:25 -04005557 /* Add encryption capability */
5558 iwe.cmd = SIOCGIWENCODE;
David Kilroy17a1a882008-08-21 23:27:47 +01005559 if (capabilities & WLAN_CAPABILITY_PRIVACY)
Dan Williams1e3428e2007-10-10 23:56:25 -04005560 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
5561 else
5562 iwe.u.data.flags = IW_ENCODE_DISABLED;
5563 iwe.u.data.length = 0;
David S. Millerccc58052008-06-16 18:50:49 -07005564 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
David Kilroy17a1a882008-08-21 23:27:47 +01005565 &iwe, NULL);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005566
Dan Williams1e3428e2007-10-10 23:56:25 -04005567 /* Bit rate is not available in Lucent/Agere firmwares */
5568 if (priv->firmware_type != FIRMWARE_TYPE_AGERE) {
David S. Millerccc58052008-06-16 18:50:49 -07005569 char *current_val = current_ev + iwe_stream_lcp_len(info);
Dan Williams1e3428e2007-10-10 23:56:25 -04005570 int i;
5571 int step;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005572
Dan Williams1e3428e2007-10-10 23:56:25 -04005573 if (priv->firmware_type == FIRMWARE_TYPE_SYMBOL)
5574 step = 2;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005575 else
Dan Williams1e3428e2007-10-10 23:56:25 -04005576 step = 1;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005577
Dan Williams1e3428e2007-10-10 23:56:25 -04005578 iwe.cmd = SIOCGIWRATE;
5579 /* Those two flags are ignored... */
5580 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
5581 /* Max 10 values */
5582 for (i = 0; i < 10; i += step) {
5583 /* NULL terminated */
5584 if (bss->p.rates[i] == 0x0)
5585 break;
5586 /* Bit rate given in 500 kb/s units (+ 0x80) */
David Kilroy17a1a882008-08-21 23:27:47 +01005587 iwe.u.bitrate.value =
5588 ((bss->p.rates[i] & 0x7f) * 500000);
David S. Millerccc58052008-06-16 18:50:49 -07005589 current_val = iwe_stream_add_value(info, current_ev,
5590 current_val,
Dan Williams1e3428e2007-10-10 23:56:25 -04005591 end_buf, &iwe,
5592 IW_EV_PARAM_LEN);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005593 }
Dan Williams1e3428e2007-10-10 23:56:25 -04005594 /* Check if we added any event */
David S. Millerccc58052008-06-16 18:50:49 -07005595 if ((current_val - current_ev) > iwe_stream_lcp_len(info))
Dan Williams1e3428e2007-10-10 23:56:25 -04005596 current_ev = current_val;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005597 }
Dan Williams1e3428e2007-10-10 23:56:25 -04005598
David Kilroy17a1a882008-08-21 23:27:47 +01005599 /* Beacon interval */
5600 iwe.cmd = IWEVCUSTOM;
5601 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5602 "bcn_int=%d",
5603 le16_to_cpu(bss->a.beacon_interv));
5604 if (iwe.u.data.length)
5605 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5606 &iwe, custom);
5607
5608 /* Capabilites */
5609 iwe.cmd = IWEVCUSTOM;
5610 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5611 "capab=0x%04x",
5612 capabilities);
5613 if (iwe.u.data.length)
5614 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5615 &iwe, custom);
5616
5617 /* Add EXTRA: Age to display seconds since last beacon/probe response
5618 * for given network. */
5619 iwe.cmd = IWEVCUSTOM;
5620 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5621 " Last beacon: %dms ago",
5622 jiffies_to_msecs(jiffies - last_scanned));
5623 if (iwe.u.data.length)
5624 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5625 &iwe, custom);
5626
Dan Williams1e3428e2007-10-10 23:56:25 -04005627 return current_ev;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005628}
5629
David Kilroy01632fa2008-08-21 23:27:58 +01005630static inline char *orinoco_translate_ext_scan(struct net_device *dev,
5631 struct iw_request_info *info,
5632 char *current_ev,
5633 char *end_buf,
5634 struct agere_ext_scan_info *bss,
Pavel Roskindfe1baf2008-11-10 09:25:53 -05005635 unsigned long last_scanned)
David Kilroy01632fa2008-08-21 23:27:58 +01005636{
5637 u16 capabilities;
5638 u16 channel;
5639 struct iw_event iwe; /* Temporary buffer */
5640 char custom[MAX_CUSTOM_LEN];
5641 u8 *ie;
5642
5643 memset(&iwe, 0, sizeof(iwe));
5644
5645 /* First entry *MUST* be the AP MAC address */
5646 iwe.cmd = SIOCGIWAP;
5647 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
5648 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
5649 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5650 &iwe, IW_EV_ADDR_LEN);
5651
5652 /* Other entries will be displayed in the order we give them */
5653
5654 /* Add the ESSID */
5655 ie = bss->data;
5656 iwe.u.data.length = ie[1];
5657 if (iwe.u.data.length) {
5658 if (iwe.u.data.length > 32)
5659 iwe.u.data.length = 32;
5660 iwe.cmd = SIOCGIWESSID;
5661 iwe.u.data.flags = 1;
5662 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5663 &iwe, &ie[2]);
5664 }
5665
5666 /* Add mode */
5667 capabilities = le16_to_cpu(bss->capabilities);
5668 if (capabilities & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
5669 iwe.cmd = SIOCGIWMODE;
5670 if (capabilities & WLAN_CAPABILITY_ESS)
5671 iwe.u.mode = IW_MODE_MASTER;
5672 else
5673 iwe.u.mode = IW_MODE_ADHOC;
5674 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5675 &iwe, IW_EV_UINT_LEN);
5676 }
5677
Johannes Berg2c7060022008-10-30 22:09:54 +01005678 ie = orinoco_get_ie(bss->data, sizeof(bss->data), WLAN_EID_DS_PARAMS);
David Kilroy01632fa2008-08-21 23:27:58 +01005679 channel = ie ? ie[2] : 0;
5680 if ((channel >= 1) && (channel <= NUM_CHANNELS)) {
5681 /* Add channel and frequency */
5682 iwe.cmd = SIOCGIWFREQ;
5683 iwe.u.freq.m = channel;
5684 iwe.u.freq.e = 0;
5685 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5686 &iwe, IW_EV_FREQ_LEN);
5687
5688 iwe.u.freq.m = channel_frequency[channel-1] * 100000;
5689 iwe.u.freq.e = 1;
5690 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5691 &iwe, IW_EV_FREQ_LEN);
5692 }
5693
5694 /* Add quality statistics. level and noise in dB. No link quality */
5695 iwe.cmd = IWEVQUAL;
5696 iwe.u.qual.updated = IW_QUAL_DBM | IW_QUAL_QUAL_INVALID;
5697 iwe.u.qual.level = bss->level - 0x95;
5698 iwe.u.qual.noise = bss->noise - 0x95;
5699 /* Wireless tools prior to 27.pre22 will show link quality
5700 * anyway, so we provide a reasonable value. */
5701 if (iwe.u.qual.level > iwe.u.qual.noise)
5702 iwe.u.qual.qual = iwe.u.qual.level - iwe.u.qual.noise;
5703 else
5704 iwe.u.qual.qual = 0;
5705 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5706 &iwe, IW_EV_QUAL_LEN);
5707
5708 /* Add encryption capability */
5709 iwe.cmd = SIOCGIWENCODE;
5710 if (capabilities & WLAN_CAPABILITY_PRIVACY)
5711 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
5712 else
5713 iwe.u.data.flags = IW_ENCODE_DISABLED;
5714 iwe.u.data.length = 0;
5715 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5716 &iwe, NULL);
5717
5718 /* WPA IE */
5719 ie = orinoco_get_wpa_ie(bss->data, sizeof(bss->data));
5720 if (ie) {
5721 iwe.cmd = IWEVGENIE;
5722 iwe.u.data.length = ie[1] + 2;
5723 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5724 &iwe, ie);
5725 }
5726
5727 /* RSN IE */
Johannes Berg2c7060022008-10-30 22:09:54 +01005728 ie = orinoco_get_ie(bss->data, sizeof(bss->data), WLAN_EID_RSN);
David Kilroy01632fa2008-08-21 23:27:58 +01005729 if (ie) {
5730 iwe.cmd = IWEVGENIE;
5731 iwe.u.data.length = ie[1] + 2;
5732 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5733 &iwe, ie);
5734 }
5735
Johannes Berg2c7060022008-10-30 22:09:54 +01005736 ie = orinoco_get_ie(bss->data, sizeof(bss->data), WLAN_EID_SUPP_RATES);
David Kilroy01632fa2008-08-21 23:27:58 +01005737 if (ie) {
5738 char *p = current_ev + iwe_stream_lcp_len(info);
5739 int i;
5740
5741 iwe.cmd = SIOCGIWRATE;
5742 /* Those two flags are ignored... */
5743 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
5744
5745 for (i = 2; i < (ie[1] + 2); i++) {
5746 iwe.u.bitrate.value = ((ie[i] & 0x7F) * 500000);
5747 p = iwe_stream_add_value(info, current_ev, p, end_buf,
5748 &iwe, IW_EV_PARAM_LEN);
5749 }
5750 /* Check if we added any event */
5751 if (p > (current_ev + iwe_stream_lcp_len(info)))
5752 current_ev = p;
5753 }
5754
5755 /* Timestamp */
5756 iwe.cmd = IWEVCUSTOM;
David Kilroy75d31cf2008-09-12 22:28:18 +01005757 iwe.u.data.length =
5758 snprintf(custom, MAX_CUSTOM_LEN, "tsf=%016llx",
5759 (unsigned long long) le64_to_cpu(bss->timestamp));
David Kilroy01632fa2008-08-21 23:27:58 +01005760 if (iwe.u.data.length)
5761 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5762 &iwe, custom);
5763
5764 /* Beacon interval */
5765 iwe.cmd = IWEVCUSTOM;
5766 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5767 "bcn_int=%d",
5768 le16_to_cpu(bss->beacon_interval));
5769 if (iwe.u.data.length)
5770 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5771 &iwe, custom);
5772
5773 /* Capabilites */
5774 iwe.cmd = IWEVCUSTOM;
5775 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5776 "capab=0x%04x",
5777 capabilities);
5778 if (iwe.u.data.length)
5779 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5780 &iwe, custom);
5781
5782 /* Add EXTRA: Age to display seconds since last beacon/probe response
5783 * for given network. */
5784 iwe.cmd = IWEVCUSTOM;
5785 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5786 " Last beacon: %dms ago",
5787 jiffies_to_msecs(jiffies - last_scanned));
5788 if (iwe.u.data.length)
5789 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5790 &iwe, custom);
5791
5792 return current_ev;
5793}
5794
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005795/* Return results of a scan */
5796static int orinoco_ioctl_getscan(struct net_device *dev,
5797 struct iw_request_info *info,
5798 struct iw_point *srq,
5799 char *extra)
5800{
5801 struct orinoco_private *priv = netdev_priv(dev);
5802 int err = 0;
5803 unsigned long flags;
Dan Williams1e3428e2007-10-10 23:56:25 -04005804 char *current_ev = extra;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005805
5806 if (orinoco_lock(priv, &flags) != 0)
5807 return -EBUSY;
5808
Dan Williams1e3428e2007-10-10 23:56:25 -04005809 if (priv->scan_inprogress) {
5810 /* Important note : we don't want to block the caller
5811 * until results are ready for various reasons.
5812 * First, managing wait queues is complex and racy.
5813 * Second, we grab some rtnetlink lock before comming
5814 * here (in dev_ioctl()).
5815 * Third, we generate an Wireless Event, so the
5816 * caller can wait itself on that - Jean II */
5817 err = -EAGAIN;
5818 goto out;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005819 }
Dan Williams1e3428e2007-10-10 23:56:25 -04005820
David Kilroy01632fa2008-08-21 23:27:58 +01005821 if (priv->has_ext_scan) {
5822 struct xbss_element *bss;
Dan Williams1e3428e2007-10-10 23:56:25 -04005823
David Kilroy01632fa2008-08-21 23:27:58 +01005824 list_for_each_entry(bss, &priv->bss_list, list) {
5825 /* Translate this entry to WE format */
5826 current_ev =
5827 orinoco_translate_ext_scan(dev, info,
5828 current_ev,
5829 extra + srq->length,
5830 &bss->bss,
5831 bss->last_scanned);
5832
5833 /* Check if there is space for one more entry */
5834 if ((extra + srq->length - current_ev)
5835 <= IW_EV_ADDR_LEN) {
5836 /* Ask user space to try again with a
5837 * bigger buffer */
5838 err = -E2BIG;
5839 goto out;
5840 }
5841 }
5842
5843 } else {
5844 struct bss_element *bss;
5845
5846 list_for_each_entry(bss, &priv->bss_list, list) {
5847 /* Translate this entry to WE format */
5848 current_ev = orinoco_translate_scan(dev, info,
5849 current_ev,
5850 extra + srq->length,
5851 &bss->bss,
5852 bss->last_scanned);
5853
5854 /* Check if there is space for one more entry */
5855 if ((extra + srq->length - current_ev)
5856 <= IW_EV_ADDR_LEN) {
5857 /* Ask user space to try again with a
5858 * bigger buffer */
5859 err = -E2BIG;
5860 goto out;
5861 }
Dan Williams1e3428e2007-10-10 23:56:25 -04005862 }
5863 }
5864
5865 srq->length = (current_ev - extra);
5866 srq->flags = (__u16) priv->scan_mode;
5867
5868out:
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005869 orinoco_unlock(priv, &flags);
5870 return err;
5871}
5872
Christoph Hellwig620554e2005-06-19 01:27:33 +02005873/* Commit handler, called after set operations */
5874static int orinoco_ioctl_commit(struct net_device *dev,
5875 struct iw_request_info *info,
5876 void *wrqu,
5877 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878{
5879 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005880 struct hermes *hw = &priv->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 unsigned long flags;
Christoph Hellwig620554e2005-06-19 01:27:33 +02005882 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883
Christoph Hellwig620554e2005-06-19 01:27:33 +02005884 if (!priv->open)
5885 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886
Christoph Hellwig620554e2005-06-19 01:27:33 +02005887 if (priv->broken_disableport) {
David Howellsc4028952006-11-22 14:57:56 +00005888 orinoco_reset(&priv->reset_work);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005889 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891
Christoph Hellwig620554e2005-06-19 01:27:33 +02005892 if (orinoco_lock(priv, &flags) != 0)
5893 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894
Christoph Hellwig620554e2005-06-19 01:27:33 +02005895 err = hermes_disable_port(hw, 0);
5896 if (err) {
5897 printk(KERN_WARNING "%s: Unable to disable port "
5898 "while reconfiguring card\n", dev->name);
5899 priv->broken_disableport = 1;
5900 goto out;
5901 }
5902
5903 err = __orinoco_program_rids(dev);
5904 if (err) {
5905 printk(KERN_WARNING "%s: Unable to reconfigure card\n",
5906 dev->name);
5907 goto out;
5908 }
5909
5910 err = hermes_enable_port(hw, 0);
5911 if (err) {
5912 printk(KERN_WARNING "%s: Unable to enable port while reconfiguring card\n",
5913 dev->name);
5914 goto out;
5915 }
5916
5917 out:
5918 if (err) {
5919 printk(KERN_WARNING "%s: Resetting instead...\n", dev->name);
5920 schedule_work(&priv->reset_work);
5921 err = 0;
5922 }
5923
5924 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 return err;
5926}
5927
Christoph Hellwig620554e2005-06-19 01:27:33 +02005928static const struct iw_priv_args orinoco_privtab[] = {
5929 { SIOCIWFIRSTPRIV + 0x0, 0, 0, "force_reset" },
5930 { SIOCIWFIRSTPRIV + 0x1, 0, 0, "card_reset" },
5931 { SIOCIWFIRSTPRIV + 0x2, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5932 0, "set_port3" },
5933 { SIOCIWFIRSTPRIV + 0x3, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5934 "get_port3" },
5935 { SIOCIWFIRSTPRIV + 0x4, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5936 0, "set_preamble" },
5937 { SIOCIWFIRSTPRIV + 0x5, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5938 "get_preamble" },
5939 { SIOCIWFIRSTPRIV + 0x6, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5940 0, "set_ibssport" },
5941 { SIOCIWFIRSTPRIV + 0x7, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5942 "get_ibssport" },
5943 { SIOCIWFIRSTPRIV + 0x9, 0, IW_PRIV_TYPE_BYTE | MAX_RID_LEN,
5944 "get_rid" },
5945};
5946
5947
5948/*
5949 * Structures to export the Wireless Handlers
5950 */
5951
David Kilroy409644a2008-08-21 23:28:01 +01005952#define STD_IW_HANDLER(id, func) \
5953 [IW_IOCTL_IDX(id)] = (iw_handler) func
Christoph Hellwig620554e2005-06-19 01:27:33 +02005954static const iw_handler orinoco_handler[] = {
David Kilroy409644a2008-08-21 23:28:01 +01005955 STD_IW_HANDLER(SIOCSIWCOMMIT, orinoco_ioctl_commit),
5956 STD_IW_HANDLER(SIOCGIWNAME, orinoco_ioctl_getname),
5957 STD_IW_HANDLER(SIOCSIWFREQ, orinoco_ioctl_setfreq),
5958 STD_IW_HANDLER(SIOCGIWFREQ, orinoco_ioctl_getfreq),
5959 STD_IW_HANDLER(SIOCSIWMODE, orinoco_ioctl_setmode),
5960 STD_IW_HANDLER(SIOCGIWMODE, orinoco_ioctl_getmode),
5961 STD_IW_HANDLER(SIOCSIWSENS, orinoco_ioctl_setsens),
5962 STD_IW_HANDLER(SIOCGIWSENS, orinoco_ioctl_getsens),
5963 STD_IW_HANDLER(SIOCGIWRANGE, orinoco_ioctl_getiwrange),
5964 STD_IW_HANDLER(SIOCSIWSPY, iw_handler_set_spy),
5965 STD_IW_HANDLER(SIOCGIWSPY, iw_handler_get_spy),
5966 STD_IW_HANDLER(SIOCSIWTHRSPY, iw_handler_set_thrspy),
5967 STD_IW_HANDLER(SIOCGIWTHRSPY, iw_handler_get_thrspy),
5968 STD_IW_HANDLER(SIOCSIWAP, orinoco_ioctl_setwap),
5969 STD_IW_HANDLER(SIOCGIWAP, orinoco_ioctl_getwap),
5970 STD_IW_HANDLER(SIOCSIWSCAN, orinoco_ioctl_setscan),
5971 STD_IW_HANDLER(SIOCGIWSCAN, orinoco_ioctl_getscan),
5972 STD_IW_HANDLER(SIOCSIWESSID, orinoco_ioctl_setessid),
5973 STD_IW_HANDLER(SIOCGIWESSID, orinoco_ioctl_getessid),
5974 STD_IW_HANDLER(SIOCSIWNICKN, orinoco_ioctl_setnick),
5975 STD_IW_HANDLER(SIOCGIWNICKN, orinoco_ioctl_getnick),
5976 STD_IW_HANDLER(SIOCSIWRATE, orinoco_ioctl_setrate),
5977 STD_IW_HANDLER(SIOCGIWRATE, orinoco_ioctl_getrate),
5978 STD_IW_HANDLER(SIOCSIWRTS, orinoco_ioctl_setrts),
5979 STD_IW_HANDLER(SIOCGIWRTS, orinoco_ioctl_getrts),
5980 STD_IW_HANDLER(SIOCSIWFRAG, orinoco_ioctl_setfrag),
5981 STD_IW_HANDLER(SIOCGIWFRAG, orinoco_ioctl_getfrag),
5982 STD_IW_HANDLER(SIOCGIWRETRY, orinoco_ioctl_getretry),
5983 STD_IW_HANDLER(SIOCSIWENCODE, orinoco_ioctl_setiwencode),
5984 STD_IW_HANDLER(SIOCGIWENCODE, orinoco_ioctl_getiwencode),
5985 STD_IW_HANDLER(SIOCSIWPOWER, orinoco_ioctl_setpower),
5986 STD_IW_HANDLER(SIOCGIWPOWER, orinoco_ioctl_getpower),
David Kilroyd03032a2008-08-21 23:28:02 +01005987 STD_IW_HANDLER(SIOCSIWGENIE, orinoco_ioctl_set_genie),
5988 STD_IW_HANDLER(SIOCGIWGENIE, orinoco_ioctl_get_genie),
5989 STD_IW_HANDLER(SIOCSIWMLME, orinoco_ioctl_set_mlme),
5990 STD_IW_HANDLER(SIOCSIWAUTH, orinoco_ioctl_set_auth),
5991 STD_IW_HANDLER(SIOCGIWAUTH, orinoco_ioctl_get_auth),
5992 STD_IW_HANDLER(SIOCSIWENCODEEXT, orinoco_ioctl_set_encodeext),
5993 STD_IW_HANDLER(SIOCGIWENCODEEXT, orinoco_ioctl_get_encodeext),
Christoph Hellwig620554e2005-06-19 01:27:33 +02005994};
5995
5996
5997/*
5998 Added typecasting since we no longer use iwreq_data -- Moustafa
5999 */
6000static const iw_handler orinoco_private_handler[] = {
Peter Hagervall6b9b97c2005-07-27 01:14:46 -07006001 [0] = (iw_handler) orinoco_ioctl_reset,
6002 [1] = (iw_handler) orinoco_ioctl_reset,
6003 [2] = (iw_handler) orinoco_ioctl_setport3,
6004 [3] = (iw_handler) orinoco_ioctl_getport3,
6005 [4] = (iw_handler) orinoco_ioctl_setpreamble,
6006 [5] = (iw_handler) orinoco_ioctl_getpreamble,
6007 [6] = (iw_handler) orinoco_ioctl_setibssport,
6008 [7] = (iw_handler) orinoco_ioctl_getibssport,
6009 [9] = (iw_handler) orinoco_ioctl_getrid,
Christoph Hellwig620554e2005-06-19 01:27:33 +02006010};
6011
6012static const struct iw_handler_def orinoco_handler_def = {
6013 .num_standard = ARRAY_SIZE(orinoco_handler),
6014 .num_private = ARRAY_SIZE(orinoco_private_handler),
6015 .num_private_args = ARRAY_SIZE(orinoco_privtab),
6016 .standard = orinoco_handler,
6017 .private = orinoco_private_handler,
6018 .private_args = orinoco_privtab,
Pavel Roskin343c6862005-09-09 18:43:02 -04006019 .get_wireless_stats = orinoco_get_wireless_stats,
Christoph Hellwig620554e2005-06-19 01:27:33 +02006020};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02006022static void orinoco_get_drvinfo(struct net_device *dev,
6023 struct ethtool_drvinfo *info)
6024{
6025 struct orinoco_private *priv = netdev_priv(dev);
6026
6027 strncpy(info->driver, DRIVER_NAME, sizeof(info->driver) - 1);
6028 strncpy(info->version, DRIVER_VERSION, sizeof(info->version) - 1);
6029 strncpy(info->fw_version, priv->fw_name, sizeof(info->fw_version) - 1);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07006030 if (dev->dev.parent)
Kay Sieversfb28ad32008-11-10 13:55:14 -08006031 strncpy(info->bus_info, dev_name(dev->dev.parent),
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02006032 sizeof(info->bus_info) - 1);
6033 else
6034 snprintf(info->bus_info, sizeof(info->bus_info) - 1,
6035 "PCMCIA %p", priv->hw.iobase);
6036}
6037
Jeff Garzik7282d492006-09-13 14:30:00 -04006038static const struct ethtool_ops orinoco_ethtool_ops = {
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02006039 .get_drvinfo = orinoco_get_drvinfo,
6040 .get_link = ethtool_op_get_link,
6041};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042
6043/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044/* Module initialization */
6045/********************************************************************/
6046
6047EXPORT_SYMBOL(alloc_orinocodev);
6048EXPORT_SYMBOL(free_orinocodev);
6049
6050EXPORT_SYMBOL(__orinoco_up);
6051EXPORT_SYMBOL(__orinoco_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052EXPORT_SYMBOL(orinoco_reinit_firmware);
6053
6054EXPORT_SYMBOL(orinoco_interrupt);
6055
6056/* Can't be declared "const" or the whole __initdata section will
6057 * become const */
6058static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
6059 " (David Gibson <hermes@gibson.dropbear.id.au>, "
6060 "Pavel Roskin <proski@gnu.org>, et al)";
6061
6062static int __init init_orinoco(void)
6063{
6064 printk(KERN_DEBUG "%s\n", version);
6065 return 0;
6066}
6067
6068static void __exit exit_orinoco(void)
6069{
6070}
6071
6072module_init(init_orinoco);
6073module_exit(exit_orinoco);