blob: acfced8ea0d5723572e9bab99c6deeb9a019ace8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* orinoco.c - (formerly known as dldwd_cs.c and orinoco_cs.c)
2 *
3 * A driver for Hermes or Prism 2 chipset based PCMCIA wireless
4 * adaptors, with Lucent/Agere, Intersil or Symbol firmware.
5 *
6 * Current maintainers (as of 29 September 2003) are:
7 * Pavel Roskin <proski AT gnu.org>
8 * and David Gibson <hermes AT gibson.dropbear.id.au>
9 *
10 * (C) Copyright David Gibson, IBM Corporation 2001-2003.
11 * Copyright (C) 2000 David Gibson, Linuxcare Australia.
12 * With some help from :
13 * Copyright (C) 2001 Jean Tourrilhes, HP Labs
14 * Copyright (C) 2001 Benjamin Herrenschmidt
15 *
16 * Based on dummy_cs.c 1.27 2000/06/12 21:27:25
17 *
18 * Portions based on wvlan_cs.c 1.0.6, Copyright Andreas Neuhaus <andy
19 * AT fasta.fh-dortmund.de>
20 * http://www.stud.fh-dortmund.de/~andy/wvlan/
21 *
22 * The contents of this file are subject to the Mozilla Public License
23 * Version 1.1 (the "License"); you may not use this file except in
24 * compliance with the License. You may obtain a copy of the License
25 * at http://www.mozilla.org/MPL/
26 *
27 * Software distributed under the License is distributed on an "AS IS"
28 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
29 * the License for the specific language governing rights and
30 * limitations under the License.
31 *
32 * The initial developer of the original code is David A. Hinds
33 * <dahinds AT users.sourceforge.net>. Portions created by David
34 * A. Hinds are Copyright (C) 1999 David A. Hinds. All Rights
35 * Reserved.
36 *
37 * Alternatively, the contents of this file may be used under the
38 * terms of the GNU General Public License version 2 (the "GPL"), in
39 * which case the provisions of the GPL are applicable instead of the
40 * above. If you wish to allow the use of your version of this file
41 * only under the terms of the GPL and not to allow others to use your
42 * version of this file under the MPL, indicate your decision by
43 * deleting the provisions above and replace them with the notice and
44 * other provisions required by the GPL. If you do not delete the
45 * provisions above, a recipient may use your version of this file
46 * under either the MPL or the GPL. */
47
48/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * TODO
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * o Handle de-encapsulation within network layer, provide 802.11
51 * headers (patch from Thomas 'Dent' Mirlacher)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * o Fix possible races in SPY handling.
53 * o Disconnect wireless extensions from fundamental configuration.
54 * o (maybe) Software WEP support (patch from Stano Meduna).
55 * o (maybe) Use multiple Tx buffers - driver handling queue
56 * rather than firmware.
57 */
58
59/* Locking and synchronization:
60 *
61 * The basic principle is that everything is serialized through a
62 * single spinlock, priv->lock. The lock is used in user, bh and irq
63 * context, so when taken outside hardirq context it should always be
64 * taken with interrupts disabled. The lock protects both the
65 * hardware and the struct orinoco_private.
66 *
67 * Another flag, priv->hw_unavailable indicates that the hardware is
68 * unavailable for an extended period of time (e.g. suspended, or in
69 * the middle of a hard reset). This flag is protected by the
70 * spinlock. All code which touches the hardware should check the
71 * flag after taking the lock, and if it is set, give up on whatever
72 * they are doing and drop the lock again. The orinoco_lock()
73 * function handles this (it unlocks and returns -EBUSY if
74 * hw_unavailable is non-zero).
75 */
76
77#define DRIVER_NAME "orinoco"
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <linux/module.h>
80#include <linux/kernel.h>
81#include <linux/init.h>
David Kilroyd03032a2008-08-21 23:28:02 +010082#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include <linux/etherdevice.h>
Christoph Hellwig1fab2e82005-06-19 01:27:40 +020085#include <linux/ethtool.h>
David Kilroy3994d502008-08-21 23:27:54 +010086#include <linux/firmware.h>
David Kilroy39d1ffe2008-11-22 10:37:28 +000087#include <linux/suspend.h>
Pavel Roskin9c974fb2006-08-15 20:45:03 -040088#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#include <linux/wireless.h>
Johannes Berg2c7060022008-10-30 22:09:54 +010090#include <linux/ieee80211.h>
Christoph Hellwig620554e2005-06-19 01:27:33 +020091#include <net/iw_handler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
David Kilroy23edcc42008-08-21 23:28:05 +010093#include <linux/scatterlist.h>
94#include <linux/crypto.h>
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#include "hermes_rid.h"
David Kilroy3994d502008-08-21 23:27:54 +010097#include "hermes_dld.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#include "orinoco.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100/********************************************************************/
101/* Module information */
102/********************************************************************/
103
David Kilroyb2f30a02009-02-04 23:05:46 +0000104MODULE_AUTHOR("Pavel Roskin <proski@gnu.org> & "
105 "David Gibson <hermes@gibson.dropbear.id.au>");
106MODULE_DESCRIPTION("Driver for Lucent Orinoco, Prism II based "
107 "and similar wireless cards");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108MODULE_LICENSE("Dual MPL/GPL");
109
110/* Level of debugging. Used in the macros in orinoco.h */
111#ifdef ORINOCO_DEBUG
112int orinoco_debug = ORINOCO_DEBUG;
113module_param(orinoco_debug, int, 0644);
114MODULE_PARM_DESC(orinoco_debug, "Debug level");
115EXPORT_SYMBOL(orinoco_debug);
116#endif
117
118static int suppress_linkstatus; /* = 0 */
119module_param(suppress_linkstatus, bool, 0644);
120MODULE_PARM_DESC(suppress_linkstatus, "Don't log link status changes");
David Kilroyb2f30a02009-02-04 23:05:46 +0000121
David Gibson7bb7c3a2005-05-12 20:02:10 -0400122static int ignore_disconnect; /* = 0 */
123module_param(ignore_disconnect, int, 0644);
David Kilroyb2f30a02009-02-04 23:05:46 +0000124MODULE_PARM_DESC(ignore_disconnect,
125 "Don't report lost link to the network layer");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200127static int force_monitor; /* = 0 */
128module_param(force_monitor, int, 0644);
129MODULE_PARM_DESC(force_monitor, "Allow monitor mode for all firmware versions");
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/********************************************************************/
132/* Compile time configuration and compatibility stuff */
133/********************************************************************/
134
135/* We do this this way to avoid ifdefs in the actual code */
136#ifdef WIRELESS_SPY
Pavel Roskin343c6862005-09-09 18:43:02 -0400137#define SPY_NUMBER(priv) (priv->spy_data.spy_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#else
139#define SPY_NUMBER(priv) 0
140#endif /* WIRELESS_SPY */
141
142/********************************************************************/
143/* Internal constants */
144/********************************************************************/
145
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200146/* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
147static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
148#define ENCAPS_OVERHEAD (sizeof(encaps_hdr) + 2)
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#define ORINOCO_MIN_MTU 256
Johannes Berg2c7060022008-10-30 22:09:54 +0100151#define ORINOCO_MAX_MTU (IEEE80211_MAX_DATA_LEN - ENCAPS_OVERHEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153#define SYMBOL_MAX_VER_LEN (14)
154#define USER_BAP 0
155#define IRQ_BAP 1
156#define MAX_IRQLOOPS_PER_IRQ 10
157#define MAX_IRQLOOPS_PER_JIFFY (20000/HZ) /* Based on a guestimate of
158 * how many events the
159 * device could
160 * legitimately generate */
161#define SMALL_KEY_SIZE 5
162#define LARGE_KEY_SIZE 13
163#define TX_NICBUF_SIZE_BUG 1585 /* Bug in Symbol firmware */
164
165#define DUMMY_FID 0xFFFF
166
167/*#define MAX_MULTICAST(priv) (priv->firmware_type == FIRMWARE_TYPE_AGERE ? \
168 HERMES_MAX_MULTICAST : 0)*/
169#define MAX_MULTICAST(priv) (HERMES_MAX_MULTICAST)
170
171#define ORINOCO_INTEN (HERMES_EV_RX | HERMES_EV_ALLOC \
172 | HERMES_EV_TX | HERMES_EV_TXEXC \
173 | HERMES_EV_WTERR | HERMES_EV_INFO \
David Kilroya94e8422009-02-04 23:05:44 +0000174 | HERMES_EV_INFDROP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Christoph Hellwig620554e2005-06-19 01:27:33 +0200176#define MAX_RID_LEN 1024
177
178static const struct iw_handler_def orinoco_handler_def;
Jeff Garzik7282d492006-09-13 14:30:00 -0400179static const struct ethtool_ops orinoco_ethtool_ops;
Christoph Hellwig620554e2005-06-19 01:27:33 +0200180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/********************************************************************/
182/* Data tables */
183/********************************************************************/
184
David Kilroy9ee677c2008-12-23 14:03:38 +0000185#define NUM_CHANNELS 14
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
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
David Kilroy47166792009-01-07 00:43:54 +0000242struct orinoco_rx_data {
243 struct hermes_rx_descriptor *desc;
244 struct sk_buff *skb;
245 struct list_head list;
246};
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/********************************************************************/
249/* Function prototypes */
250/********************************************************************/
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252static int __orinoco_program_rids(struct net_device *dev);
253static void __orinoco_set_multicast_list(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255/********************************************************************/
David Kilroy23edcc42008-08-21 23:28:05 +0100256/* Michael MIC crypto setup */
257/********************************************************************/
258#define MICHAEL_MIC_LEN 8
259static int orinoco_mic_init(struct orinoco_private *priv)
260{
261 priv->tx_tfm_mic = crypto_alloc_hash("michael_mic", 0, 0);
262 if (IS_ERR(priv->tx_tfm_mic)) {
263 printk(KERN_DEBUG "orinoco_mic_init: could not allocate "
264 "crypto API michael_mic\n");
265 priv->tx_tfm_mic = NULL;
266 return -ENOMEM;
267 }
268
269 priv->rx_tfm_mic = crypto_alloc_hash("michael_mic", 0, 0);
270 if (IS_ERR(priv->rx_tfm_mic)) {
271 printk(KERN_DEBUG "orinoco_mic_init: could not allocate "
272 "crypto API michael_mic\n");
273 priv->rx_tfm_mic = NULL;
274 return -ENOMEM;
275 }
276
277 return 0;
278}
279
280static void orinoco_mic_free(struct orinoco_private *priv)
281{
282 if (priv->tx_tfm_mic)
283 crypto_free_hash(priv->tx_tfm_mic);
284 if (priv->rx_tfm_mic)
285 crypto_free_hash(priv->rx_tfm_mic);
286}
287
288static int michael_mic(struct crypto_hash *tfm_michael, u8 *key,
289 u8 *da, u8 *sa, u8 priority,
290 u8 *data, size_t data_len, u8 *mic)
291{
292 struct hash_desc desc;
293 struct scatterlist sg[2];
294 u8 hdr[ETH_HLEN + 2]; /* size of header + padding */
295
296 if (tfm_michael == NULL) {
297 printk(KERN_WARNING "michael_mic: tfm_michael == NULL\n");
298 return -1;
299 }
300
301 /* Copy header into buffer. We need the padding on the end zeroed */
302 memcpy(&hdr[0], da, ETH_ALEN);
303 memcpy(&hdr[ETH_ALEN], sa, ETH_ALEN);
304 hdr[ETH_ALEN*2] = priority;
305 hdr[ETH_ALEN*2+1] = 0;
306 hdr[ETH_ALEN*2+2] = 0;
307 hdr[ETH_ALEN*2+3] = 0;
308
309 /* Use scatter gather to MIC header and data in one go */
310 sg_init_table(sg, 2);
311 sg_set_buf(&sg[0], hdr, sizeof(hdr));
312 sg_set_buf(&sg[1], data, data_len);
313
314 if (crypto_hash_setkey(tfm_michael, key, MIC_KEYLEN))
315 return -1;
316
317 desc.tfm = tfm_michael;
318 desc.flags = 0;
319 return crypto_hash_digest(&desc, sg, data_len + sizeof(hdr),
320 mic);
321}
322
323/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324/* Internal helper functions */
325/********************************************************************/
326
327static inline void set_port_type(struct orinoco_private *priv)
328{
329 switch (priv->iw_mode) {
330 case IW_MODE_INFRA:
331 priv->port_type = 1;
332 priv->createibss = 0;
333 break;
334 case IW_MODE_ADHOC:
335 if (priv->prefer_port3) {
336 priv->port_type = 3;
337 priv->createibss = 0;
338 } else {
339 priv->port_type = priv->ibss_port;
340 priv->createibss = 1;
341 }
342 break;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200343 case IW_MODE_MONITOR:
344 priv->port_type = 3;
345 priv->createibss = 0;
346 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 default:
348 printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
349 priv->ndev->name);
350 }
351}
352
Dan Williams1e3428e2007-10-10 23:56:25 -0400353#define ORINOCO_MAX_BSS_COUNT 64
354static int orinoco_bss_data_allocate(struct orinoco_private *priv)
355{
David Kilroy01632fa2008-08-21 23:27:58 +0100356 if (priv->bss_xbss_data)
Dan Williams1e3428e2007-10-10 23:56:25 -0400357 return 0;
358
David Kilroy01632fa2008-08-21 23:27:58 +0100359 if (priv->has_ext_scan)
360 priv->bss_xbss_data = kzalloc(ORINOCO_MAX_BSS_COUNT *
361 sizeof(struct xbss_element),
362 GFP_KERNEL);
363 else
364 priv->bss_xbss_data = kzalloc(ORINOCO_MAX_BSS_COUNT *
365 sizeof(struct bss_element),
366 GFP_KERNEL);
367
368 if (!priv->bss_xbss_data) {
Dan Williams1e3428e2007-10-10 23:56:25 -0400369 printk(KERN_WARNING "Out of memory allocating beacons");
370 return -ENOMEM;
371 }
372 return 0;
373}
374
375static void orinoco_bss_data_free(struct orinoco_private *priv)
376{
David Kilroy01632fa2008-08-21 23:27:58 +0100377 kfree(priv->bss_xbss_data);
378 priv->bss_xbss_data = NULL;
Dan Williams1e3428e2007-10-10 23:56:25 -0400379}
380
David Kilroy01632fa2008-08-21 23:27:58 +0100381#define PRIV_BSS ((struct bss_element *)priv->bss_xbss_data)
382#define PRIV_XBSS ((struct xbss_element *)priv->bss_xbss_data)
Dan Williams1e3428e2007-10-10 23:56:25 -0400383static void orinoco_bss_data_init(struct orinoco_private *priv)
384{
385 int i;
386
387 INIT_LIST_HEAD(&priv->bss_free_list);
388 INIT_LIST_HEAD(&priv->bss_list);
David Kilroy01632fa2008-08-21 23:27:58 +0100389 if (priv->has_ext_scan)
390 for (i = 0; i < ORINOCO_MAX_BSS_COUNT; i++)
391 list_add_tail(&(PRIV_XBSS[i].list),
392 &priv->bss_free_list);
393 else
394 for (i = 0; i < ORINOCO_MAX_BSS_COUNT; i++)
395 list_add_tail(&(PRIV_BSS[i].list),
396 &priv->bss_free_list);
397
398}
399
400static inline u8 *orinoco_get_ie(u8 *data, size_t len,
Johannes Berg2c7060022008-10-30 22:09:54 +0100401 enum ieee80211_eid eid)
David Kilroy01632fa2008-08-21 23:27:58 +0100402{
403 u8 *p = data;
404 while ((p + 2) < (data + len)) {
405 if (p[0] == eid)
406 return p;
407 p += p[1] + 2;
408 }
409 return NULL;
410}
411
412#define WPA_OUI_TYPE "\x00\x50\xF2\x01"
413#define WPA_SELECTOR_LEN 4
414static inline u8 *orinoco_get_wpa_ie(u8 *data, size_t len)
415{
416 u8 *p = data;
417 while ((p + 2 + WPA_SELECTOR_LEN) < (data + len)) {
Johannes Berg2c7060022008-10-30 22:09:54 +0100418 if ((p[0] == WLAN_EID_GENERIC) &&
David Kilroy01632fa2008-08-21 23:27:58 +0100419 (memcmp(&p[2], WPA_OUI_TYPE, WPA_SELECTOR_LEN) == 0))
420 return p;
421 p += p[1] + 2;
422 }
423 return NULL;
Dan Williams1e3428e2007-10-10 23:56:25 -0400424}
425
David Kilroy3994d502008-08-21 23:27:54 +0100426
427/********************************************************************/
428/* Download functionality */
429/********************************************************************/
430
431struct fw_info {
432 char *pri_fw;
433 char *sta_fw;
434 char *ap_fw;
435 u32 pda_addr;
436 u16 pda_size;
437};
438
439const static struct fw_info orinoco_fw[] = {
David Kilroy74734312008-11-22 10:37:25 +0000440 { NULL, "agere_sta_fw.bin", "agere_ap_fw.bin", 0x00390000, 1000 },
441 { NULL, "prism_sta_fw.bin", "prism_ap_fw.bin", 0, 1024 },
442 { "symbol_sp24t_prim_fw", "symbol_sp24t_sec_fw", NULL, 0x00003100, 512 }
David Kilroy3994d502008-08-21 23:27:54 +0100443};
444
445/* Structure used to access fields in FW
446 * Make sure LE decoding macros are used
447 */
448struct orinoco_fw_header {
449 char hdr_vers[6]; /* ASCII string for header version */
450 __le16 headersize; /* Total length of header */
451 __le32 entry_point; /* NIC entry point */
452 __le32 blocks; /* Number of blocks to program */
453 __le32 block_offset; /* Offset of block data from eof header */
454 __le32 pdr_offset; /* Offset to PDR data from eof header */
455 __le32 pri_offset; /* Offset to primary plug data */
456 __le32 compat_offset; /* Offset to compatibility data*/
457 char signature[0]; /* FW signature length headersize-20 */
458} __attribute__ ((packed));
459
460/* Download either STA or AP firmware into the card. */
461static int
462orinoco_dl_firmware(struct orinoco_private *priv,
463 const struct fw_info *fw,
464 int ap)
465{
466 /* Plug Data Area (PDA) */
Andrey Borzenkov70458252008-10-10 21:26:30 +0400467 __le16 *pda;
David Kilroy3994d502008-08-21 23:27:54 +0100468
469 hermes_t *hw = &priv->hw;
470 const struct firmware *fw_entry;
471 const struct orinoco_fw_header *hdr;
472 const unsigned char *first_block;
473 const unsigned char *end;
474 const char *firmware;
475 struct net_device *dev = priv->ndev;
Andrey Borzenkov70458252008-10-10 21:26:30 +0400476 int err = 0;
477
478 pda = kzalloc(fw->pda_size, GFP_KERNEL);
479 if (!pda)
480 return -ENOMEM;
David Kilroy3994d502008-08-21 23:27:54 +0100481
482 if (ap)
483 firmware = fw->ap_fw;
484 else
485 firmware = fw->sta_fw;
486
487 printk(KERN_DEBUG "%s: Attempting to download firmware %s\n",
488 dev->name, firmware);
489
490 /* Read current plug data */
Andrey Borzenkov70458252008-10-10 21:26:30 +0400491 err = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 0);
David Kilroy3994d502008-08-21 23:27:54 +0100492 printk(KERN_DEBUG "%s: Read PDA returned %d\n", dev->name, err);
493 if (err)
Andrey Borzenkov70458252008-10-10 21:26:30 +0400494 goto free;
David Kilroy3994d502008-08-21 23:27:54 +0100495
David Kilroy74734312008-11-22 10:37:25 +0000496 if (!priv->cached_fw) {
Andrey Borzenkov4fb30782008-10-19 12:06:11 +0400497 err = request_firmware(&fw_entry, firmware, priv->dev);
David Kilroy74734312008-11-22 10:37:25 +0000498
Andrey Borzenkov4fb30782008-10-19 12:06:11 +0400499 if (err) {
500 printk(KERN_ERR "%s: Cannot find firmware %s\n",
501 dev->name, firmware);
502 err = -ENOENT;
503 goto free;
504 }
David Kilroy74734312008-11-22 10:37:25 +0000505 } else
506 fw_entry = priv->cached_fw;
David Kilroy3994d502008-08-21 23:27:54 +0100507
508 hdr = (const struct orinoco_fw_header *) fw_entry->data;
509
510 /* Enable aux port to allow programming */
511 err = hermesi_program_init(hw, le32_to_cpu(hdr->entry_point));
512 printk(KERN_DEBUG "%s: Program init returned %d\n", dev->name, err);
513 if (err != 0)
514 goto abort;
515
516 /* Program data */
517 first_block = (fw_entry->data +
518 le16_to_cpu(hdr->headersize) +
519 le32_to_cpu(hdr->block_offset));
520 end = fw_entry->data + fw_entry->size;
521
522 err = hermes_program(hw, first_block, end);
523 printk(KERN_DEBUG "%s: Program returned %d\n", dev->name, err);
524 if (err != 0)
525 goto abort;
526
527 /* Update production data */
528 first_block = (fw_entry->data +
529 le16_to_cpu(hdr->headersize) +
530 le32_to_cpu(hdr->pdr_offset));
531
532 err = hermes_apply_pda_with_defaults(hw, first_block, pda);
533 printk(KERN_DEBUG "%s: Apply PDA returned %d\n", dev->name, err);
534 if (err)
535 goto abort;
536
537 /* Tell card we've finished */
538 err = hermesi_program_end(hw);
539 printk(KERN_DEBUG "%s: Program end returned %d\n", dev->name, err);
540 if (err != 0)
541 goto abort;
542
543 /* Check if we're running */
544 printk(KERN_DEBUG "%s: hermes_present returned %d\n",
545 dev->name, hermes_present(hw));
546
547abort:
David Kilroy74734312008-11-22 10:37:25 +0000548 /* If we requested the firmware, release it. */
549 if (!priv->cached_fw)
Andrey Borzenkov4fb30782008-10-19 12:06:11 +0400550 release_firmware(fw_entry);
Andrey Borzenkov70458252008-10-10 21:26:30 +0400551
552free:
553 kfree(pda);
David Kilroy3994d502008-08-21 23:27:54 +0100554 return err;
555}
556
557/* End markers */
558#define TEXT_END 0x1A /* End of text header */
559
560/*
561 * Process a firmware image - stop the card, load the firmware, reset
562 * the card and make sure it responds. For the secondary firmware take
563 * care of the PDA - read it and then write it on top of the firmware.
564 */
565static int
566symbol_dl_image(struct orinoco_private *priv, const struct fw_info *fw,
567 const unsigned char *image, const unsigned char *end,
568 int secondary)
569{
570 hermes_t *hw = &priv->hw;
Andrey Borzenkov70458252008-10-10 21:26:30 +0400571 int ret = 0;
David Kilroy3994d502008-08-21 23:27:54 +0100572 const unsigned char *ptr;
573 const unsigned char *first_block;
574
575 /* Plug Data Area (PDA) */
Andrey Borzenkov70458252008-10-10 21:26:30 +0400576 __le16 *pda = NULL;
David Kilroy3994d502008-08-21 23:27:54 +0100577
578 /* Binary block begins after the 0x1A marker */
579 ptr = image;
580 while (*ptr++ != TEXT_END);
581 first_block = ptr;
582
583 /* Read the PDA from EEPROM */
584 if (secondary) {
Andrey Borzenkov70458252008-10-10 21:26:30 +0400585 pda = kzalloc(fw->pda_size, GFP_KERNEL);
586 if (!pda)
587 return -ENOMEM;
588
589 ret = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 1);
David Kilroy3994d502008-08-21 23:27:54 +0100590 if (ret)
Andrey Borzenkov70458252008-10-10 21:26:30 +0400591 goto free;
David Kilroy3994d502008-08-21 23:27:54 +0100592 }
593
594 /* Stop the firmware, so that it can be safely rewritten */
595 if (priv->stop_fw) {
596 ret = priv->stop_fw(priv, 1);
597 if (ret)
Andrey Borzenkov70458252008-10-10 21:26:30 +0400598 goto free;
David Kilroy3994d502008-08-21 23:27:54 +0100599 }
600
601 /* Program the adapter with new firmware */
602 ret = hermes_program(hw, first_block, end);
603 if (ret)
Andrey Borzenkov70458252008-10-10 21:26:30 +0400604 goto free;
David Kilroy3994d502008-08-21 23:27:54 +0100605
606 /* Write the PDA to the adapter */
607 if (secondary) {
608 size_t len = hermes_blocks_length(first_block);
609 ptr = first_block + len;
610 ret = hermes_apply_pda(hw, ptr, pda);
Andrey Borzenkov70458252008-10-10 21:26:30 +0400611 kfree(pda);
David Kilroy3994d502008-08-21 23:27:54 +0100612 if (ret)
613 return ret;
614 }
615
616 /* Run the firmware */
617 if (priv->stop_fw) {
618 ret = priv->stop_fw(priv, 0);
619 if (ret)
620 return ret;
621 }
622
623 /* Reset hermes chip and make sure it responds */
624 ret = hermes_init(hw);
625
626 /* hermes_reset() should return 0 with the secondary firmware */
627 if (secondary && ret != 0)
628 return -ENODEV;
629
630 /* And this should work with any firmware */
631 if (!hermes_present(hw))
632 return -ENODEV;
633
634 return 0;
Andrey Borzenkov70458252008-10-10 21:26:30 +0400635
636free:
637 kfree(pda);
638 return ret;
David Kilroy3994d502008-08-21 23:27:54 +0100639}
640
641
642/*
643 * Download the firmware into the card, this also does a PCMCIA soft
644 * reset on the card, to make sure it's in a sane state.
645 */
646static int
647symbol_dl_firmware(struct orinoco_private *priv,
648 const struct fw_info *fw)
649{
650 struct net_device *dev = priv->ndev;
651 int ret;
652 const struct firmware *fw_entry;
653
David Kilroy2cea7b22008-11-22 10:37:26 +0000654 if (!priv->cached_pri_fw) {
655 if (request_firmware(&fw_entry, fw->pri_fw, priv->dev) != 0) {
656 printk(KERN_ERR "%s: Cannot find firmware: %s\n",
657 dev->name, fw->pri_fw);
658 return -ENOENT;
659 }
660 } else
661 fw_entry = priv->cached_pri_fw;
David Kilroy3994d502008-08-21 23:27:54 +0100662
663 /* Load primary firmware */
664 ret = symbol_dl_image(priv, fw, fw_entry->data,
665 fw_entry->data + fw_entry->size, 0);
David Kilroy2cea7b22008-11-22 10:37:26 +0000666
667 if (!priv->cached_pri_fw)
668 release_firmware(fw_entry);
David Kilroy3994d502008-08-21 23:27:54 +0100669 if (ret) {
670 printk(KERN_ERR "%s: Primary firmware download failed\n",
671 dev->name);
672 return ret;
673 }
674
David Kilroy2cea7b22008-11-22 10:37:26 +0000675 if (!priv->cached_fw) {
676 if (request_firmware(&fw_entry, fw->sta_fw, priv->dev) != 0) {
677 printk(KERN_ERR "%s: Cannot find firmware: %s\n",
678 dev->name, fw->sta_fw);
679 return -ENOENT;
680 }
681 } else
682 fw_entry = priv->cached_fw;
David Kilroy3994d502008-08-21 23:27:54 +0100683
684 /* Load secondary firmware */
685 ret = symbol_dl_image(priv, fw, fw_entry->data,
686 fw_entry->data + fw_entry->size, 1);
David Kilroy2cea7b22008-11-22 10:37:26 +0000687 if (!priv->cached_fw)
688 release_firmware(fw_entry);
David Kilroy3994d502008-08-21 23:27:54 +0100689 if (ret) {
690 printk(KERN_ERR "%s: Secondary firmware download failed\n",
691 dev->name);
692 }
693
694 return ret;
695}
696
697static int orinoco_download(struct orinoco_private *priv)
698{
699 int err = 0;
700 /* Reload firmware */
701 switch (priv->firmware_type) {
702 case FIRMWARE_TYPE_AGERE:
703 /* case FIRMWARE_TYPE_INTERSIL: */
704 err = orinoco_dl_firmware(priv,
705 &orinoco_fw[priv->firmware_type], 0);
706 break;
707
708 case FIRMWARE_TYPE_SYMBOL:
709 err = symbol_dl_firmware(priv,
710 &orinoco_fw[priv->firmware_type]);
711 break;
712 case FIRMWARE_TYPE_INTERSIL:
713 break;
714 }
715 /* TODO: if we fail we probably need to reinitialise
716 * the driver */
717
718 return err;
719}
720
David Kilroy39d1ffe2008-11-22 10:37:28 +0000721#if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
David Kilroy74734312008-11-22 10:37:25 +0000722static void orinoco_cache_fw(struct orinoco_private *priv, int ap)
723{
724 const struct firmware *fw_entry = NULL;
David Kilroy2cea7b22008-11-22 10:37:26 +0000725 const char *pri_fw;
David Kilroy74734312008-11-22 10:37:25 +0000726 const char *fw;
727
David Kilroy2cea7b22008-11-22 10:37:26 +0000728 pri_fw = orinoco_fw[priv->firmware_type].pri_fw;
David Kilroy74734312008-11-22 10:37:25 +0000729 if (ap)
730 fw = orinoco_fw[priv->firmware_type].ap_fw;
731 else
732 fw = orinoco_fw[priv->firmware_type].sta_fw;
733
David Kilroy2cea7b22008-11-22 10:37:26 +0000734 if (pri_fw) {
735 if (request_firmware(&fw_entry, pri_fw, priv->dev) == 0)
736 priv->cached_pri_fw = fw_entry;
737 }
738
David Kilroy74734312008-11-22 10:37:25 +0000739 if (fw) {
740 if (request_firmware(&fw_entry, fw, priv->dev) == 0)
741 priv->cached_fw = fw_entry;
742 }
743}
744
745static void orinoco_uncache_fw(struct orinoco_private *priv)
746{
David Kilroy2cea7b22008-11-22 10:37:26 +0000747 if (priv->cached_pri_fw)
748 release_firmware(priv->cached_pri_fw);
David Kilroy74734312008-11-22 10:37:25 +0000749 if (priv->cached_fw)
750 release_firmware(priv->cached_fw);
751
David Kilroy2cea7b22008-11-22 10:37:26 +0000752 priv->cached_pri_fw = NULL;
David Kilroy74734312008-11-22 10:37:25 +0000753 priv->cached_fw = NULL;
754}
David Kilroy39d1ffe2008-11-22 10:37:28 +0000755#else
756#define orinoco_cache_fw(priv, ap)
757#define orinoco_uncache_fw(priv)
758#endif
David Kilroy74734312008-11-22 10:37:25 +0000759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760/********************************************************************/
761/* Device methods */
762/********************************************************************/
763
764static int orinoco_open(struct net_device *dev)
765{
766 struct orinoco_private *priv = netdev_priv(dev);
767 unsigned long flags;
768 int err;
769
770 if (orinoco_lock(priv, &flags) != 0)
771 return -EBUSY;
772
773 err = __orinoco_up(dev);
774
David Kilroya94e8422009-02-04 23:05:44 +0000775 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 priv->open = 1;
777
778 orinoco_unlock(priv, &flags);
779
780 return err;
781}
782
Christoph Hellwigad8f4512005-05-14 17:30:17 +0200783static int orinoco_stop(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 struct orinoco_private *priv = netdev_priv(dev);
786 int err = 0;
787
788 /* We mustn't use orinoco_lock() here, because we need to be
789 able to close the interface even if hw_unavailable is set
790 (e.g. as we're released after a PC Card removal) */
791 spin_lock_irq(&priv->lock);
792
793 priv->open = 0;
794
795 err = __orinoco_down(dev);
796
797 spin_unlock_irq(&priv->lock);
798
799 return err;
800}
801
802static struct net_device_stats *orinoco_get_stats(struct net_device *dev)
803{
804 struct orinoco_private *priv = netdev_priv(dev);
David Kilroy6fe9deb2009-02-04 23:05:43 +0000805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return &priv->stats;
807}
808
809static struct iw_statistics *orinoco_get_wireless_stats(struct net_device *dev)
810{
811 struct orinoco_private *priv = netdev_priv(dev);
812 hermes_t *hw = &priv->hw;
813 struct iw_statistics *wstats = &priv->wstats;
David Gibsone67d9d92005-05-12 20:01:22 -0400814 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 unsigned long flags;
816
David Kilroya94e8422009-02-04 23:05:44 +0000817 if (!netif_device_present(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 printk(KERN_WARNING "%s: get_wireless_stats() called while device not present\n",
819 dev->name);
820 return NULL; /* FIXME: Can we do better than this? */
821 }
822
David Gibsone67d9d92005-05-12 20:01:22 -0400823 /* If busy, return the old stats. Returning NULL may cause
824 * the interface to disappear from /proc/net/wireless */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (orinoco_lock(priv, &flags) != 0)
David Gibsone67d9d92005-05-12 20:01:22 -0400826 return wstats;
827
828 /* We can't really wait for the tallies inquiry command to
829 * complete, so we just use the previous results and trigger
830 * a new tallies inquiry command for next time - Jean II */
831 /* FIXME: Really we should wait for the inquiry to come back -
832 * as it is the stats we give don't make a whole lot of sense.
833 * Unfortunately, it's not clear how to do that within the
834 * wireless extensions framework: I think we're in user
835 * context, but a lock seems to be held by the time we get in
836 * here so we're not safe to sleep here. */
837 hermes_inquire(hw, HERMES_INQ_TALLIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 if (priv->iw_mode == IW_MODE_ADHOC) {
840 memset(&wstats->qual, 0, sizeof(wstats->qual));
841 /* If a spy address is defined, we report stats of the
842 * first spy address - Jean II */
843 if (SPY_NUMBER(priv)) {
Pavel Roskin343c6862005-09-09 18:43:02 -0400844 wstats->qual.qual = priv->spy_data.spy_stat[0].qual;
845 wstats->qual.level = priv->spy_data.spy_stat[0].level;
846 wstats->qual.noise = priv->spy_data.spy_stat[0].noise;
David Kilroyb2f30a02009-02-04 23:05:46 +0000847 wstats->qual.updated =
848 priv->spy_data.spy_stat[0].updated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850 } else {
851 struct {
Pavel Roskina208c4e2006-04-07 04:10:26 -0400852 __le16 qual, signal, noise, unused;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 } __attribute__ ((packed)) cq;
854
855 err = HERMES_READ_RECORD(hw, USER_BAP,
856 HERMES_RID_COMMSQUALITY, &cq);
David Gibsone67d9d92005-05-12 20:01:22 -0400857
858 if (!err) {
859 wstats->qual.qual = (int)le16_to_cpu(cq.qual);
860 wstats->qual.level = (int)le16_to_cpu(cq.signal) - 0x95;
861 wstats->qual.noise = (int)le16_to_cpu(cq.noise) - 0x95;
David Kilroyb2f30a02009-02-04 23:05:46 +0000862 wstats->qual.updated =
863 IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
David Gibsone67d9d92005-05-12 20:01:22 -0400864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return wstats;
869}
870
871static void orinoco_set_multicast_list(struct net_device *dev)
872{
873 struct orinoco_private *priv = netdev_priv(dev);
874 unsigned long flags;
875
876 if (orinoco_lock(priv, &flags) != 0) {
877 printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
878 "called when hw_unavailable\n", dev->name);
879 return;
880 }
881
882 __orinoco_set_multicast_list(dev);
883 orinoco_unlock(priv, &flags);
884}
885
886static int orinoco_change_mtu(struct net_device *dev, int new_mtu)
887{
888 struct orinoco_private *priv = netdev_priv(dev);
889
David Kilroya94e8422009-02-04 23:05:44 +0000890 if ((new_mtu < ORINOCO_MIN_MTU) || (new_mtu > ORINOCO_MAX_MTU))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return -EINVAL;
892
Johannes Berg2c7060022008-10-30 22:09:54 +0100893 /* MTU + encapsulation + header length */
David Kilroya94e8422009-02-04 23:05:44 +0000894 if ((new_mtu + ENCAPS_OVERHEAD + sizeof(struct ieee80211_hdr)) >
895 (priv->nicbuf_size - ETH_HLEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return -EINVAL;
897
898 dev->mtu = new_mtu;
899
900 return 0;
901}
902
903/********************************************************************/
904/* Tx path */
905/********************************************************************/
906
907static int orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
908{
909 struct orinoco_private *priv = netdev_priv(dev);
910 struct net_device_stats *stats = &priv->stats;
911 hermes_t *hw = &priv->hw;
912 int err = 0;
913 u16 txfid = priv->txfid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 struct ethhdr *eh;
David Kilroy6eecad72008-08-21 23:27:56 +0100915 int tx_control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 unsigned long flags;
917
David Kilroya94e8422009-02-04 23:05:44 +0000918 if (!netif_running(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 printk(KERN_ERR "%s: Tx on stopped device!\n",
920 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400921 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
David Kilroy6fe9deb2009-02-04 23:05:43 +0000923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (netif_queue_stopped(dev)) {
David Kilroy6fe9deb2009-02-04 23:05:43 +0000925 printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400927 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
David Kilroy6fe9deb2009-02-04 23:05:43 +0000929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (orinoco_lock(priv, &flags) != 0) {
931 printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n",
932 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400933 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
935
David Kilroya94e8422009-02-04 23:05:44 +0000936 if (!netif_carrier_ok(dev) || (priv->iw_mode == IW_MODE_MONITOR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 /* Oops, the firmware hasn't established a connection,
David Kilroy6fe9deb2009-02-04 23:05:43 +0000938 silently drop the packet (this seems to be the
939 safest approach). */
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400940 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942
Pavel Roskin8d5be082006-04-07 04:10:41 -0400943 /* Check packet length */
Pavel Roskina28dc812006-04-07 04:10:45 -0400944 if (skb->len < ETH_HLEN)
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400945 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
David Kilroy6eecad72008-08-21 23:27:56 +0100947 tx_control = HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
David Kilroy23edcc42008-08-21 23:28:05 +0100949 if (priv->encode_alg == IW_ENCODE_ALG_TKIP)
950 tx_control |= (priv->tx_key << HERMES_MIC_KEY_ID_SHIFT) |
951 HERMES_TXCTRL_MIC;
952
David Kilroy6eecad72008-08-21 23:27:56 +0100953 if (priv->has_alt_txcntl) {
954 /* WPA enabled firmwares have tx_cntl at the end of
955 * the 802.11 header. So write zeroed descriptor and
956 * 802.11 header at the same time
957 */
958 char desc[HERMES_802_3_OFFSET];
959 __le16 *txcntl = (__le16 *) &desc[HERMES_TXCNTL2_OFFSET];
960
961 memset(&desc, 0, sizeof(desc));
962
963 *txcntl = cpu_to_le16(tx_control);
964 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
965 txfid, 0);
966 if (err) {
967 if (net_ratelimit())
968 printk(KERN_ERR "%s: Error %d writing Tx "
969 "descriptor to BAP\n", dev->name, err);
970 goto busy;
971 }
972 } else {
973 struct hermes_tx_descriptor desc;
974
975 memset(&desc, 0, sizeof(desc));
976
977 desc.tx_control = cpu_to_le16(tx_control);
978 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
979 txfid, 0);
980 if (err) {
981 if (net_ratelimit())
982 printk(KERN_ERR "%s: Error %d writing Tx "
983 "descriptor to BAP\n", dev->name, err);
984 goto busy;
985 }
986
987 /* Clear the 802.11 header and data length fields - some
988 * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
989 * if this isn't done. */
990 hermes_clear_words(hw, HERMES_DATA0,
991 HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
David Kilroy23edcc42008-08-21 23:28:05 +0100994 eh = (struct ethhdr *)skb->data;
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 /* Encapsulate Ethernet-II frames */
997 if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
Pavel Roskina28dc812006-04-07 04:10:45 -0400998 struct header_struct {
999 struct ethhdr eth; /* 802.3 header */
1000 u8 encap[6]; /* 802.2 header */
1001 } __attribute__ ((packed)) hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Pavel Roskina28dc812006-04-07 04:10:45 -04001003 /* Strip destination and source from the data */
1004 skb_pull(skb, 2 * ETH_ALEN);
Pavel Roskina28dc812006-04-07 04:10:45 -04001005
1006 /* And move them to a separate header */
1007 memcpy(&hdr.eth, eh, 2 * ETH_ALEN);
1008 hdr.eth.h_proto = htons(sizeof(encaps_hdr) + skb->len);
1009 memcpy(hdr.encap, encaps_hdr, sizeof(encaps_hdr));
1010
David Kilroy23edcc42008-08-21 23:28:05 +01001011 /* Insert the SNAP header */
1012 if (skb_headroom(skb) < sizeof(hdr)) {
1013 printk(KERN_ERR
1014 "%s: Not enough headroom for 802.2 headers %d\n",
1015 dev->name, skb_headroom(skb));
1016 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
David Kilroy23edcc42008-08-21 23:28:05 +01001018 eh = (struct ethhdr *) skb_push(skb, sizeof(hdr));
1019 memcpy(eh, &hdr, sizeof(hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021
Pavel Roskina28dc812006-04-07 04:10:45 -04001022 err = hermes_bap_pwrite(hw, USER_BAP, skb->data, skb->len,
David Kilroy23edcc42008-08-21 23:28:05 +01001023 txfid, HERMES_802_3_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 if (err) {
1025 printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
1026 dev->name, err);
Pavel Roskin470e2aa2006-04-07 04:10:43 -04001027 goto busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029
David Kilroy23edcc42008-08-21 23:28:05 +01001030 /* Calculate Michael MIC */
1031 if (priv->encode_alg == IW_ENCODE_ALG_TKIP) {
1032 u8 mic_buf[MICHAEL_MIC_LEN + 1];
1033 u8 *mic;
1034 size_t offset;
1035 size_t len;
1036
1037 if (skb->len % 2) {
1038 /* MIC start is on an odd boundary */
1039 mic_buf[0] = skb->data[skb->len - 1];
1040 mic = &mic_buf[1];
1041 offset = skb->len - 1;
1042 len = MICHAEL_MIC_LEN + 1;
1043 } else {
1044 mic = &mic_buf[0];
1045 offset = skb->len;
1046 len = MICHAEL_MIC_LEN;
1047 }
1048
1049 michael_mic(priv->tx_tfm_mic,
1050 priv->tkip_key[priv->tx_key].tx_mic,
1051 eh->h_dest, eh->h_source, 0 /* priority */,
1052 skb->data + ETH_HLEN, skb->len - ETH_HLEN, mic);
1053
1054 /* Write the MIC */
1055 err = hermes_bap_pwrite(hw, USER_BAP, &mic_buf[0], len,
1056 txfid, HERMES_802_3_OFFSET + offset);
1057 if (err) {
1058 printk(KERN_ERR "%s: Error %d writing MIC to BAP\n",
1059 dev->name, err);
1060 goto busy;
1061 }
1062 }
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 /* Finally, we actually initiate the send */
1065 netif_stop_queue(dev);
1066
1067 err = hermes_docmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
1068 txfid, NULL);
1069 if (err) {
1070 netif_start_queue(dev);
Andrew Mortonc367c212005-10-19 21:23:44 -07001071 if (net_ratelimit())
1072 printk(KERN_ERR "%s: Error %d transmitting packet\n",
1073 dev->name, err);
Pavel Roskin470e2aa2006-04-07 04:10:43 -04001074 goto busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
1076
1077 dev->trans_start = jiffies;
David Kilroy23edcc42008-08-21 23:28:05 +01001078 stats->tx_bytes += HERMES_802_3_OFFSET + skb->len;
Pavel Roskin470e2aa2006-04-07 04:10:43 -04001079 goto ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Pavel Roskin470e2aa2006-04-07 04:10:43 -04001081 drop:
1082 stats->tx_errors++;
1083 stats->tx_dropped++;
1084
1085 ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 dev_kfree_skb(skb);
Pavel Roskinb34b8672006-04-07 04:10:36 -04001088 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Pavel Roskin470e2aa2006-04-07 04:10:43 -04001090 busy:
Jiri Benc2c1bd262006-04-07 04:10:47 -04001091 if (err == -EIO)
1092 schedule_work(&priv->reset_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 orinoco_unlock(priv, &flags);
Pavel Roskinb34b8672006-04-07 04:10:36 -04001094 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095}
1096
1097static void __orinoco_ev_alloc(struct net_device *dev, hermes_t *hw)
1098{
1099 struct orinoco_private *priv = netdev_priv(dev);
1100 u16 fid = hermes_read_regn(hw, ALLOCFID);
1101
1102 if (fid != priv->txfid) {
1103 if (fid != DUMMY_FID)
1104 printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
1105 dev->name, fid);
1106 return;
1107 }
1108
1109 hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
1110}
1111
1112static void __orinoco_ev_tx(struct net_device *dev, hermes_t *hw)
1113{
1114 struct orinoco_private *priv = netdev_priv(dev);
1115 struct net_device_stats *stats = &priv->stats;
1116
1117 stats->tx_packets++;
1118
1119 netif_wake_queue(dev);
1120
1121 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
1122}
1123
1124static void __orinoco_ev_txexc(struct net_device *dev, hermes_t *hw)
1125{
1126 struct orinoco_private *priv = netdev_priv(dev);
1127 struct net_device_stats *stats = &priv->stats;
1128 u16 fid = hermes_read_regn(hw, TXCOMPLFID);
Pavel Roskind133ae42005-09-23 04:18:06 -04001129 u16 status;
Pavel Roskin30c2d3b2006-04-07 04:10:34 -04001130 struct hermes_txexc_data hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 int err = 0;
1132
1133 if (fid == DUMMY_FID)
1134 return; /* Nothing's really happened */
1135
Pavel Roskin48ca7032005-09-23 04:18:06 -04001136 /* Read part of the frame header - we need status and addr1 */
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001137 err = hermes_bap_pread(hw, IRQ_BAP, &hdr,
Pavel Roskin30c2d3b2006-04-07 04:10:34 -04001138 sizeof(struct hermes_txexc_data),
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001139 fid, 0);
1140
1141 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
1142 stats->tx_errors++;
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (err) {
1145 printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
1146 "(FID=%04X error %d)\n",
1147 dev->name, fid, err);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001148 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001150
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001151 DEBUG(1, "%s: Tx error, err %d (FID=%04X)\n", dev->name,
1152 err, fid);
David Kilroy6fe9deb2009-02-04 23:05:43 +00001153
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001154 /* We produce a TXDROP event only for retry or lifetime
1155 * exceeded, because that's the only status that really mean
1156 * that this particular node went away.
1157 * Other errors means that *we* screwed up. - Jean II */
Pavel Roskin30c2d3b2006-04-07 04:10:34 -04001158 status = le16_to_cpu(hdr.desc.status);
Pavel Roskind133ae42005-09-23 04:18:06 -04001159 if (status & (HERMES_TXSTAT_RETRYERR | HERMES_TXSTAT_AGEDERR)) {
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001160 union iwreq_data wrqu;
1161
1162 /* Copy 802.11 dest address.
1163 * We use the 802.11 header because the frame may
1164 * not be 802.3 or may be mangled...
1165 * In Ad-Hoc mode, it will be the node address.
1166 * In managed mode, it will be most likely the AP addr
1167 * User space will figure out how to convert it to
1168 * whatever it needs (IP address or else).
1169 * - Jean II */
1170 memcpy(wrqu.addr.sa_data, hdr.addr1, ETH_ALEN);
1171 wrqu.addr.sa_family = ARPHRD_ETHER;
1172
1173 /* Send event to user space */
1174 wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
1175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
1180static void orinoco_tx_timeout(struct net_device *dev)
1181{
1182 struct orinoco_private *priv = netdev_priv(dev);
1183 struct net_device_stats *stats = &priv->stats;
1184 struct hermes *hw = &priv->hw;
1185
1186 printk(KERN_WARNING "%s: Tx timeout! "
1187 "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
1188 dev->name, hermes_read_regn(hw, ALLOCFID),
1189 hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
1190
1191 stats->tx_errors++;
1192
1193 schedule_work(&priv->reset_work);
1194}
1195
1196/********************************************************************/
1197/* Rx path (data frames) */
1198/********************************************************************/
1199
1200/* Does the frame have a SNAP header indicating it should be
1201 * de-encapsulated to Ethernet-II? */
1202static inline int is_ethersnap(void *_hdr)
1203{
1204 u8 *hdr = _hdr;
1205
1206 /* We de-encapsulate all packets which, a) have SNAP headers
1207 * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
1208 * and where b) the OUI of the SNAP header is 00:00:00 or
1209 * 00:00:f8 - we need both because different APs appear to use
1210 * different OUIs for some reason */
1211 return (memcmp(hdr, &encaps_hdr, 5) == 0)
David Kilroya94e8422009-02-04 23:05:44 +00001212 && ((hdr[5] == 0x00) || (hdr[5] == 0xf8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
1215static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
1216 int level, int noise)
1217{
Pavel Roskin343c6862005-09-09 18:43:02 -04001218 struct iw_quality wstats;
1219 wstats.level = level - 0x95;
1220 wstats.noise = noise - 0x95;
1221 wstats.qual = (level > noise) ? (level - noise) : 0;
Andrey Borzenkovf941f852008-11-15 17:15:09 +03001222 wstats.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
Pavel Roskin343c6862005-09-09 18:43:02 -04001223 /* Update spy records */
1224 wireless_spy_update(dev, mac, &wstats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
1227static void orinoco_stat_gather(struct net_device *dev,
1228 struct sk_buff *skb,
1229 struct hermes_rx_descriptor *desc)
1230{
1231 struct orinoco_private *priv = netdev_priv(dev);
1232
1233 /* Using spy support with lots of Rx packets, like in an
1234 * infrastructure (AP), will really slow down everything, because
1235 * the MAC address must be compared to each entry of the spy list.
1236 * If the user really asks for it (set some address in the
1237 * spy list), we do it, but he will pay the price.
1238 * Note that to get here, you need both WIRELESS_SPY
1239 * compiled in AND some addresses in the list !!!
1240 */
1241 /* Note : gcc will optimise the whole section away if
1242 * WIRELESS_SPY is not defined... - Jean II */
1243 if (SPY_NUMBER(priv)) {
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001244 orinoco_spy_gather(dev, skb_mac_header(skb) + ETH_ALEN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 desc->signal, desc->silence);
1246 }
1247}
1248
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001249/*
1250 * orinoco_rx_monitor - handle received monitor frames.
1251 *
1252 * Arguments:
1253 * dev network device
1254 * rxfid received FID
1255 * desc rx descriptor of the frame
1256 *
1257 * Call context: interrupt
1258 */
1259static void orinoco_rx_monitor(struct net_device *dev, u16 rxfid,
1260 struct hermes_rx_descriptor *desc)
1261{
1262 u32 hdrlen = 30; /* return full header by default */
1263 u32 datalen = 0;
1264 u16 fc;
1265 int err;
1266 int len;
1267 struct sk_buff *skb;
1268 struct orinoco_private *priv = netdev_priv(dev);
1269 struct net_device_stats *stats = &priv->stats;
1270 hermes_t *hw = &priv->hw;
1271
1272 len = le16_to_cpu(desc->data_len);
1273
1274 /* Determine the size of the header and the data */
1275 fc = le16_to_cpu(desc->frame_ctl);
1276 switch (fc & IEEE80211_FCTL_FTYPE) {
1277 case IEEE80211_FTYPE_DATA:
1278 if ((fc & IEEE80211_FCTL_TODS)
1279 && (fc & IEEE80211_FCTL_FROMDS))
1280 hdrlen = 30;
1281 else
1282 hdrlen = 24;
1283 datalen = len;
1284 break;
1285 case IEEE80211_FTYPE_MGMT:
1286 hdrlen = 24;
1287 datalen = len;
1288 break;
1289 case IEEE80211_FTYPE_CTL:
1290 switch (fc & IEEE80211_FCTL_STYPE) {
1291 case IEEE80211_STYPE_PSPOLL:
1292 case IEEE80211_STYPE_RTS:
1293 case IEEE80211_STYPE_CFEND:
1294 case IEEE80211_STYPE_CFENDACK:
1295 hdrlen = 16;
1296 break;
1297 case IEEE80211_STYPE_CTS:
1298 case IEEE80211_STYPE_ACK:
1299 hdrlen = 10;
1300 break;
1301 }
1302 break;
1303 default:
1304 /* Unknown frame type */
1305 break;
1306 }
1307
1308 /* sanity check the length */
Johannes Berg2c7060022008-10-30 22:09:54 +01001309 if (datalen > IEEE80211_MAX_DATA_LEN + 12) {
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001310 printk(KERN_DEBUG "%s: oversized monitor frame, "
1311 "data length = %d\n", dev->name, datalen);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001312 stats->rx_length_errors++;
1313 goto update_stats;
1314 }
1315
1316 skb = dev_alloc_skb(hdrlen + datalen);
1317 if (!skb) {
1318 printk(KERN_WARNING "%s: Cannot allocate skb for monitor frame\n",
1319 dev->name);
Florin Malitabb6e0932006-05-22 22:35:30 -07001320 goto update_stats;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001321 }
1322
1323 /* Copy the 802.11 header to the skb */
1324 memcpy(skb_put(skb, hdrlen), &(desc->frame_ctl), hdrlen);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001325 skb_reset_mac_header(skb);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001326
1327 /* If any, copy the data from the card to the skb */
1328 if (datalen > 0) {
1329 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, datalen),
1330 ALIGN(datalen, 2), rxfid,
1331 HERMES_802_2_OFFSET);
1332 if (err) {
1333 printk(KERN_ERR "%s: error %d reading monitor frame\n",
1334 dev->name, err);
1335 goto drop;
1336 }
1337 }
1338
1339 skb->dev = dev;
1340 skb->ip_summed = CHECKSUM_NONE;
1341 skb->pkt_type = PACKET_OTHERHOST;
Harvey Harrisonc1b4aa32009-01-29 13:26:44 -08001342 skb->protocol = cpu_to_be16(ETH_P_802_2);
David Kilroy6fe9deb2009-02-04 23:05:43 +00001343
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001344 stats->rx_packets++;
1345 stats->rx_bytes += skb->len;
1346
1347 netif_rx(skb);
1348 return;
1349
1350 drop:
1351 dev_kfree_skb_irq(skb);
1352 update_stats:
1353 stats->rx_errors++;
1354 stats->rx_dropped++;
1355}
1356
David Kilroy23edcc42008-08-21 23:28:05 +01001357/* Get tsc from the firmware */
1358static int orinoco_hw_get_tkip_iv(struct orinoco_private *priv, int key,
1359 u8 *tsc)
1360{
1361 hermes_t *hw = &priv->hw;
1362 int err = 0;
1363 u8 tsc_arr[4][IW_ENCODE_SEQ_MAX_SIZE];
1364
1365 if ((key < 0) || (key > 4))
1366 return -EINVAL;
1367
1368 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_TKIP_IV,
1369 sizeof(tsc_arr), NULL, &tsc_arr);
1370 if (!err)
1371 memcpy(tsc, &tsc_arr[key][0], sizeof(tsc_arr[0]));
1372
1373 return err;
1374}
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376static void __orinoco_ev_rx(struct net_device *dev, hermes_t *hw)
1377{
1378 struct orinoco_private *priv = netdev_priv(dev);
1379 struct net_device_stats *stats = &priv->stats;
1380 struct iw_statistics *wstats = &priv->wstats;
1381 struct sk_buff *skb = NULL;
David Kilroy31afcef2008-08-21 23:28:04 +01001382 u16 rxfid, status;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001383 int length;
David Kilroy31afcef2008-08-21 23:28:04 +01001384 struct hermes_rx_descriptor *desc;
1385 struct orinoco_rx_data *rx_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 int err;
1387
David Kilroy31afcef2008-08-21 23:28:04 +01001388 desc = kmalloc(sizeof(*desc), GFP_ATOMIC);
1389 if (!desc) {
1390 printk(KERN_WARNING
1391 "%s: Can't allocate space for RX descriptor\n",
1392 dev->name);
1393 goto update_stats;
1394 }
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 rxfid = hermes_read_regn(hw, RXFID);
1397
David Kilroy31afcef2008-08-21 23:28:04 +01001398 err = hermes_bap_pread(hw, IRQ_BAP, desc, sizeof(*desc),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 rxfid, 0);
1400 if (err) {
1401 printk(KERN_ERR "%s: error %d reading Rx descriptor. "
1402 "Frame dropped.\n", dev->name, err);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001403 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
1405
David Kilroy31afcef2008-08-21 23:28:04 +01001406 status = le16_to_cpu(desc->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001408 if (status & HERMES_RXSTAT_BADCRC) {
1409 DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n",
1410 dev->name);
1411 stats->rx_crc_errors++;
1412 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
1414
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001415 /* Handle frames in monitor mode */
1416 if (priv->iw_mode == IW_MODE_MONITOR) {
David Kilroy31afcef2008-08-21 23:28:04 +01001417 orinoco_rx_monitor(dev, rxfid, desc);
1418 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
1420
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001421 if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
1422 DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
1423 dev->name);
1424 wstats->discard.code++;
1425 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
1427
David Kilroy31afcef2008-08-21 23:28:04 +01001428 length = le16_to_cpu(desc->data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 /* Sanity checks */
1431 if (length < 3) { /* No for even an 802.2 LLC header */
1432 /* At least on Symbol firmware with PCF we get quite a
David Kilroy6fe9deb2009-02-04 23:05:43 +00001433 lot of these legitimately - Poll frames with no
1434 data. */
David Kilroy31afcef2008-08-21 23:28:04 +01001435 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 }
Johannes Berg2c7060022008-10-30 22:09:54 +01001437 if (length > IEEE80211_MAX_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
1439 dev->name, length);
1440 stats->rx_length_errors++;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001441 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 }
1443
David Kilroy23edcc42008-08-21 23:28:05 +01001444 /* Payload size does not include Michael MIC. Increase payload
1445 * size to read it together with the data. */
1446 if (status & HERMES_RXSTAT_MIC)
1447 length += MICHAEL_MIC_LEN;
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 /* We need space for the packet data itself, plus an ethernet
1450 header, plus 2 bytes so we can align the IP header on a
1451 32bit boundary, plus 1 byte so we can read in odd length
1452 packets from the card, which has an IO granularity of 16
David Kilroy6fe9deb2009-02-04 23:05:43 +00001453 bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 skb = dev_alloc_skb(length+ETH_HLEN+2+1);
1455 if (!skb) {
1456 printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
1457 dev->name);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001458 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 }
1460
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001461 /* We'll prepend the header, so reserve space for it. The worst
1462 case is no decapsulation, when 802.3 header is prepended and
1463 nothing is removed. 2 is for aligning the IP header. */
1464 skb_reserve(skb, ETH_HLEN + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001466 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, length),
1467 ALIGN(length, 2), rxfid,
1468 HERMES_802_2_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 if (err) {
1470 printk(KERN_ERR "%s: error %d reading frame. "
1471 "Frame dropped.\n", dev->name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 goto drop;
1473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
David Kilroy31afcef2008-08-21 23:28:04 +01001475 /* Add desc and skb to rx queue */
1476 rx_data = kzalloc(sizeof(*rx_data), GFP_ATOMIC);
1477 if (!rx_data) {
1478 printk(KERN_WARNING "%s: Can't allocate RX packet\n",
1479 dev->name);
1480 goto drop;
1481 }
1482 rx_data->desc = desc;
1483 rx_data->skb = skb;
1484 list_add_tail(&rx_data->list, &priv->rx_list);
1485 tasklet_schedule(&priv->rx_tasklet);
1486
1487 return;
1488
1489drop:
1490 dev_kfree_skb_irq(skb);
1491update_stats:
1492 stats->rx_errors++;
1493 stats->rx_dropped++;
1494out:
1495 kfree(desc);
1496}
1497
1498static void orinoco_rx(struct net_device *dev,
1499 struct hermes_rx_descriptor *desc,
1500 struct sk_buff *skb)
1501{
1502 struct orinoco_private *priv = netdev_priv(dev);
1503 struct net_device_stats *stats = &priv->stats;
1504 u16 status, fc;
1505 int length;
1506 struct ethhdr *hdr;
1507
1508 status = le16_to_cpu(desc->status);
1509 length = le16_to_cpu(desc->data_len);
1510 fc = le16_to_cpu(desc->frame_ctl);
1511
David Kilroy23edcc42008-08-21 23:28:05 +01001512 /* Calculate and check MIC */
1513 if (status & HERMES_RXSTAT_MIC) {
1514 int key_id = ((status & HERMES_RXSTAT_MIC_KEY_ID) >>
1515 HERMES_MIC_KEY_ID_SHIFT);
1516 u8 mic[MICHAEL_MIC_LEN];
1517 u8 *rxmic;
1518 u8 *src = (fc & IEEE80211_FCTL_FROMDS) ?
1519 desc->addr3 : desc->addr2;
1520
1521 /* Extract Michael MIC from payload */
1522 rxmic = skb->data + skb->len - MICHAEL_MIC_LEN;
1523
1524 skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
1525 length -= MICHAEL_MIC_LEN;
1526
1527 michael_mic(priv->rx_tfm_mic,
1528 priv->tkip_key[key_id].rx_mic,
1529 desc->addr1,
1530 src,
1531 0, /* priority or QoS? */
1532 skb->data,
1533 skb->len,
1534 &mic[0]);
1535
1536 if (memcmp(mic, rxmic,
1537 MICHAEL_MIC_LEN)) {
1538 union iwreq_data wrqu;
1539 struct iw_michaelmicfailure wxmic;
David Kilroy23edcc42008-08-21 23:28:05 +01001540
1541 printk(KERN_WARNING "%s: "
Johannes Berge1749612008-10-27 15:59:26 -07001542 "Invalid Michael MIC in data frame from %pM, "
David Kilroy23edcc42008-08-21 23:28:05 +01001543 "using key %i\n",
Johannes Berge1749612008-10-27 15:59:26 -07001544 dev->name, src, key_id);
David Kilroy23edcc42008-08-21 23:28:05 +01001545
1546 /* TODO: update stats */
1547
1548 /* Notify userspace */
1549 memset(&wxmic, 0, sizeof(wxmic));
1550 wxmic.flags = key_id & IW_MICFAILURE_KEY_ID;
1551 wxmic.flags |= (desc->addr1[0] & 1) ?
1552 IW_MICFAILURE_GROUP : IW_MICFAILURE_PAIRWISE;
1553 wxmic.src_addr.sa_family = ARPHRD_ETHER;
1554 memcpy(wxmic.src_addr.sa_data, src, ETH_ALEN);
1555
1556 (void) orinoco_hw_get_tkip_iv(priv, key_id,
1557 &wxmic.tsc[0]);
1558
1559 memset(&wrqu, 0, sizeof(wrqu));
1560 wrqu.data.length = sizeof(wxmic);
1561 wireless_send_event(dev, IWEVMICHAELMICFAILURE, &wrqu,
1562 (char *) &wxmic);
1563
1564 goto drop;
1565 }
1566 }
1567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 /* Handle decapsulation
1569 * In most cases, the firmware tell us about SNAP frames.
1570 * For some reason, the SNAP frames sent by LinkSys APs
1571 * are not properly recognised by most firmwares.
1572 * So, check ourselves */
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001573 if (length >= ENCAPS_OVERHEAD &&
1574 (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
1575 ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
1576 is_ethersnap(skb->data))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 /* These indicate a SNAP within 802.2 LLC within
1578 802.11 frame which we'll need to de-encapsulate to
1579 the original EthernetII frame. */
David Kilroyb2f30a02009-02-04 23:05:46 +00001580 hdr = (struct ethhdr *)skb_push(skb,
1581 ETH_HLEN - ENCAPS_OVERHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 } else {
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001583 /* 802.3 frame - prepend 802.3 header as is */
1584 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN);
1585 hdr->h_proto = htons(length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
David Kilroy31afcef2008-08-21 23:28:04 +01001587 memcpy(hdr->h_dest, desc->addr1, ETH_ALEN);
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001588 if (fc & IEEE80211_FCTL_FROMDS)
David Kilroy31afcef2008-08-21 23:28:04 +01001589 memcpy(hdr->h_source, desc->addr3, ETH_ALEN);
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001590 else
David Kilroy31afcef2008-08-21 23:28:04 +01001591 memcpy(hdr->h_source, desc->addr2, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 skb->protocol = eth_type_trans(skb, dev);
1594 skb->ip_summed = CHECKSUM_NONE;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001595 if (fc & IEEE80211_FCTL_TODS)
1596 skb->pkt_type = PACKET_OTHERHOST;
David Kilroy6fe9deb2009-02-04 23:05:43 +00001597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 /* Process the wireless stats if needed */
David Kilroy31afcef2008-08-21 23:28:04 +01001599 orinoco_stat_gather(dev, skb, desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 /* Pass the packet to the networking stack */
1602 netif_rx(skb);
1603 stats->rx_packets++;
1604 stats->rx_bytes += length;
1605
1606 return;
David Kilroy23edcc42008-08-21 23:28:05 +01001607
1608 drop:
1609 dev_kfree_skb(skb);
1610 stats->rx_errors++;
1611 stats->rx_dropped++;
David Kilroy31afcef2008-08-21 23:28:04 +01001612}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
David Kilroy31afcef2008-08-21 23:28:04 +01001614static void orinoco_rx_isr_tasklet(unsigned long data)
1615{
1616 struct net_device *dev = (struct net_device *) data;
1617 struct orinoco_private *priv = netdev_priv(dev);
1618 struct orinoco_rx_data *rx_data, *temp;
1619 struct hermes_rx_descriptor *desc;
1620 struct sk_buff *skb;
David Kilroy20953ad2009-01-07 00:23:55 +00001621 unsigned long flags;
1622
1623 /* orinoco_rx requires the driver lock, and we also need to
1624 * protect priv->rx_list, so just hold the lock over the
1625 * lot.
1626 *
1627 * If orinoco_lock fails, we've unplugged the card. In this
1628 * case just abort. */
1629 if (orinoco_lock(priv, &flags) != 0)
1630 return;
David Kilroy31afcef2008-08-21 23:28:04 +01001631
1632 /* extract desc and skb from queue */
1633 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
1634 desc = rx_data->desc;
1635 skb = rx_data->skb;
1636 list_del(&rx_data->list);
1637 kfree(rx_data);
1638
1639 orinoco_rx(dev, desc, skb);
1640
1641 kfree(desc);
1642 }
David Kilroy20953ad2009-01-07 00:23:55 +00001643
1644 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645}
1646
1647/********************************************************************/
1648/* Rx path (info frames) */
1649/********************************************************************/
1650
1651static void print_linkstatus(struct net_device *dev, u16 status)
1652{
1653 char * s;
1654
1655 if (suppress_linkstatus)
1656 return;
1657
1658 switch (status) {
1659 case HERMES_LINKSTATUS_NOT_CONNECTED:
1660 s = "Not Connected";
1661 break;
1662 case HERMES_LINKSTATUS_CONNECTED:
1663 s = "Connected";
1664 break;
1665 case HERMES_LINKSTATUS_DISCONNECTED:
1666 s = "Disconnected";
1667 break;
1668 case HERMES_LINKSTATUS_AP_CHANGE:
1669 s = "AP Changed";
1670 break;
1671 case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
1672 s = "AP Out of Range";
1673 break;
1674 case HERMES_LINKSTATUS_AP_IN_RANGE:
1675 s = "AP In Range";
1676 break;
1677 case HERMES_LINKSTATUS_ASSOC_FAILED:
1678 s = "Association Failed";
1679 break;
1680 default:
1681 s = "UNKNOWN";
1682 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00001683
Pavel Roskin11eaea42009-01-18 23:20:58 -05001684 printk(KERN_DEBUG "%s: New link status: %s (%04x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 dev->name, s, status);
1686}
1687
Christoph Hellwig16739b02005-06-19 01:27:51 +02001688/* Search scan results for requested BSSID, join it if found */
David Howellsc4028952006-11-22 14:57:56 +00001689static void orinoco_join_ap(struct work_struct *work)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001690{
David Howellsc4028952006-11-22 14:57:56 +00001691 struct orinoco_private *priv =
1692 container_of(work, struct orinoco_private, join_work);
1693 struct net_device *dev = priv->ndev;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001694 struct hermes *hw = &priv->hw;
1695 int err;
1696 unsigned long flags;
1697 struct join_req {
1698 u8 bssid[ETH_ALEN];
Pavel Roskind133ae42005-09-23 04:18:06 -04001699 __le16 channel;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001700 } __attribute__ ((packed)) req;
1701 const int atom_len = offsetof(struct prism2_scan_apinfo, atim);
Pavel Roskinc89cc222005-09-01 20:06:06 -04001702 struct prism2_scan_apinfo *atom = NULL;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001703 int offset = 4;
Pavel Roskinc89cc222005-09-01 20:06:06 -04001704 int found = 0;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001705 u8 *buf;
1706 u16 len;
1707
1708 /* Allocate buffer for scan results */
1709 buf = kmalloc(MAX_SCAN_LEN, GFP_KERNEL);
David Kilroya94e8422009-02-04 23:05:44 +00001710 if (!buf)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001711 return;
1712
1713 if (orinoco_lock(priv, &flags) != 0)
Pavel Roskinf3cb4cc2005-09-23 04:18:06 -04001714 goto fail_lock;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001715
1716 /* Sanity checks in case user changed something in the meantime */
David Kilroya94e8422009-02-04 23:05:44 +00001717 if (!priv->bssid_fixed)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001718 goto out;
1719
1720 if (strlen(priv->desired_essid) == 0)
1721 goto out;
1722
1723 /* Read scan results from the firmware */
1724 err = hermes_read_ltv(hw, USER_BAP,
1725 HERMES_RID_SCANRESULTSTABLE,
1726 MAX_SCAN_LEN, &len, buf);
1727 if (err) {
1728 printk(KERN_ERR "%s: Cannot read scan results\n",
1729 dev->name);
1730 goto out;
1731 }
1732
1733 len = HERMES_RECLEN_TO_BYTES(len);
1734
1735 /* Go through the scan results looking for the channel of the AP
1736 * we were requested to join */
1737 for (; offset + atom_len <= len; offset += atom_len) {
1738 atom = (struct prism2_scan_apinfo *) (buf + offset);
Pavel Roskinc89cc222005-09-01 20:06:06 -04001739 if (memcmp(&atom->bssid, priv->desired_bssid, ETH_ALEN) == 0) {
1740 found = 1;
1741 break;
1742 }
Christoph Hellwig16739b02005-06-19 01:27:51 +02001743 }
1744
David Kilroya94e8422009-02-04 23:05:44 +00001745 if (!found) {
Pavel Roskinc89cc222005-09-01 20:06:06 -04001746 DEBUG(1, "%s: Requested AP not found in scan results\n",
1747 dev->name);
1748 goto out;
1749 }
Christoph Hellwig16739b02005-06-19 01:27:51 +02001750
Christoph Hellwig16739b02005-06-19 01:27:51 +02001751 memcpy(req.bssid, priv->desired_bssid, ETH_ALEN);
1752 req.channel = atom->channel; /* both are little-endian */
1753 err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNFJOINREQUEST,
1754 &req);
1755 if (err)
1756 printk(KERN_ERR "%s: Error issuing join request\n", dev->name);
1757
1758 out:
Christoph Hellwig16739b02005-06-19 01:27:51 +02001759 orinoco_unlock(priv, &flags);
Pavel Roskinf3cb4cc2005-09-23 04:18:06 -04001760
1761 fail_lock:
1762 kfree(buf);
Christoph Hellwig16739b02005-06-19 01:27:51 +02001763}
1764
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001765/* Send new BSSID to userspace */
David Kilroy6cd90b12008-08-21 23:28:00 +01001766static void orinoco_send_bssid_wevent(struct orinoco_private *priv)
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001767{
David Howellsc4028952006-11-22 14:57:56 +00001768 struct net_device *dev = priv->ndev;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001769 struct hermes *hw = &priv->hw;
1770 union iwreq_data wrqu;
1771 int err;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001772
David Kilroy499b7022008-12-09 21:46:29 +00001773 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001774 ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
1775 if (err != 0)
David Kilroy6cd90b12008-08-21 23:28:00 +01001776 return;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001777
1778 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1779
1780 /* Send event to user space */
1781 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001782}
1783
David Kilroy06009fd2008-08-21 23:28:03 +01001784static void orinoco_send_assocreqie_wevent(struct orinoco_private *priv)
1785{
1786 struct net_device *dev = priv->ndev;
1787 struct hermes *hw = &priv->hw;
1788 union iwreq_data wrqu;
1789 int err;
1790 u8 buf[88];
1791 u8 *ie;
1792
1793 if (!priv->has_wpa)
1794 return;
1795
David Kilroy499b7022008-12-09 21:46:29 +00001796 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_REQ_INFO,
David Kilroy06009fd2008-08-21 23:28:03 +01001797 sizeof(buf), NULL, &buf);
1798 if (err != 0)
1799 return;
1800
1801 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1802 if (ie) {
1803 int rem = sizeof(buf) - (ie - &buf[0]);
1804 wrqu.data.length = ie[1] + 2;
1805 if (wrqu.data.length > rem)
1806 wrqu.data.length = rem;
1807
1808 if (wrqu.data.length)
1809 /* Send event to user space */
1810 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, ie);
1811 }
1812}
1813
1814static void orinoco_send_assocrespie_wevent(struct orinoco_private *priv)
1815{
1816 struct net_device *dev = priv->ndev;
1817 struct hermes *hw = &priv->hw;
1818 union iwreq_data wrqu;
1819 int err;
1820 u8 buf[88]; /* TODO: verify max size or IW_GENERIC_IE_MAX */
1821 u8 *ie;
1822
1823 if (!priv->has_wpa)
1824 return;
1825
David Kilroy499b7022008-12-09 21:46:29 +00001826 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_RESP_INFO,
David Kilroy06009fd2008-08-21 23:28:03 +01001827 sizeof(buf), NULL, &buf);
1828 if (err != 0)
1829 return;
1830
1831 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1832 if (ie) {
1833 int rem = sizeof(buf) - (ie - &buf[0]);
1834 wrqu.data.length = ie[1] + 2;
1835 if (wrqu.data.length > rem)
1836 wrqu.data.length = rem;
1837
1838 if (wrqu.data.length)
1839 /* Send event to user space */
1840 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, ie);
1841 }
1842}
1843
David Kilroy6cd90b12008-08-21 23:28:00 +01001844static void orinoco_send_wevents(struct work_struct *work)
1845{
1846 struct orinoco_private *priv =
1847 container_of(work, struct orinoco_private, wevent_work);
1848 unsigned long flags;
1849
1850 if (orinoco_lock(priv, &flags) != 0)
1851 return;
1852
David Kilroy06009fd2008-08-21 23:28:03 +01001853 orinoco_send_assocreqie_wevent(priv);
1854 orinoco_send_assocrespie_wevent(priv);
David Kilroy6cd90b12008-08-21 23:28:00 +01001855 orinoco_send_bssid_wevent(priv);
1856
1857 orinoco_unlock(priv, &flags);
1858}
Dan Williams1e3428e2007-10-10 23:56:25 -04001859
1860static inline void orinoco_clear_scan_results(struct orinoco_private *priv,
1861 unsigned long scan_age)
1862{
David Kilroy01632fa2008-08-21 23:27:58 +01001863 if (priv->has_ext_scan) {
1864 struct xbss_element *bss;
1865 struct xbss_element *tmp_bss;
Dan Williams1e3428e2007-10-10 23:56:25 -04001866
David Kilroy01632fa2008-08-21 23:27:58 +01001867 /* Blow away current list of scan results */
1868 list_for_each_entry_safe(bss, tmp_bss, &priv->bss_list, list) {
1869 if (!scan_age ||
1870 time_after(jiffies, bss->last_scanned + scan_age)) {
1871 list_move_tail(&bss->list,
1872 &priv->bss_free_list);
1873 /* Don't blow away ->list, just BSS data */
1874 memset(&bss->bss, 0, sizeof(bss->bss));
1875 bss->last_scanned = 0;
1876 }
Dan Williams1e3428e2007-10-10 23:56:25 -04001877 }
David Kilroy01632fa2008-08-21 23:27:58 +01001878 } else {
1879 struct bss_element *bss;
1880 struct bss_element *tmp_bss;
1881
1882 /* Blow away current list of scan results */
1883 list_for_each_entry_safe(bss, tmp_bss, &priv->bss_list, list) {
1884 if (!scan_age ||
1885 time_after(jiffies, bss->last_scanned + scan_age)) {
1886 list_move_tail(&bss->list,
1887 &priv->bss_free_list);
1888 /* Don't blow away ->list, just BSS data */
1889 memset(&bss->bss, 0, sizeof(bss->bss));
1890 bss->last_scanned = 0;
1891 }
1892 }
1893 }
1894}
1895
1896static void orinoco_add_ext_scan_result(struct orinoco_private *priv,
1897 struct agere_ext_scan_info *atom)
1898{
1899 struct xbss_element *bss = NULL;
1900 int found = 0;
1901
1902 /* Try to update an existing bss first */
1903 list_for_each_entry(bss, &priv->bss_list, list) {
1904 if (compare_ether_addr(bss->bss.bssid, atom->bssid))
1905 continue;
1906 /* ESSID lengths */
1907 if (bss->bss.data[1] != atom->data[1])
1908 continue;
1909 if (memcmp(&bss->bss.data[2], &atom->data[2],
1910 atom->data[1]))
1911 continue;
1912 found = 1;
1913 break;
1914 }
1915
1916 /* Grab a bss off the free list */
1917 if (!found && !list_empty(&priv->bss_free_list)) {
1918 bss = list_entry(priv->bss_free_list.next,
1919 struct xbss_element, list);
1920 list_del(priv->bss_free_list.next);
1921
1922 list_add_tail(&bss->list, &priv->bss_list);
1923 }
1924
1925 if (bss) {
1926 /* Always update the BSS to get latest beacon info */
1927 memcpy(&bss->bss, atom, sizeof(bss->bss));
1928 bss->last_scanned = jiffies;
Dan Williams1e3428e2007-10-10 23:56:25 -04001929 }
1930}
1931
1932static int orinoco_process_scan_results(struct net_device *dev,
1933 unsigned char *buf,
1934 int len)
1935{
1936 struct orinoco_private *priv = netdev_priv(dev);
1937 int offset; /* In the scan data */
1938 union hermes_scan_info *atom;
1939 int atom_len;
1940
1941 switch (priv->firmware_type) {
1942 case FIRMWARE_TYPE_AGERE:
1943 atom_len = sizeof(struct agere_scan_apinfo);
1944 offset = 0;
1945 break;
1946 case FIRMWARE_TYPE_SYMBOL:
1947 /* Lack of documentation necessitates this hack.
1948 * Different firmwares have 68 or 76 byte long atoms.
1949 * We try modulo first. If the length divides by both,
1950 * we check what would be the channel in the second
1951 * frame for a 68-byte atom. 76-byte atoms have 0 there.
1952 * Valid channel cannot be 0. */
1953 if (len % 76)
1954 atom_len = 68;
1955 else if (len % 68)
1956 atom_len = 76;
1957 else if (len >= 1292 && buf[68] == 0)
1958 atom_len = 76;
1959 else
1960 atom_len = 68;
1961 offset = 0;
1962 break;
1963 case FIRMWARE_TYPE_INTERSIL:
1964 offset = 4;
1965 if (priv->has_hostscan) {
1966 atom_len = le16_to_cpup((__le16 *)buf);
1967 /* Sanity check for atom_len */
1968 if (atom_len < sizeof(struct prism2_scan_apinfo)) {
1969 printk(KERN_ERR "%s: Invalid atom_len in scan "
1970 "data: %d\n", dev->name, atom_len);
1971 return -EIO;
1972 }
1973 } else
1974 atom_len = offsetof(struct prism2_scan_apinfo, atim);
1975 break;
1976 default:
1977 return -EOPNOTSUPP;
1978 }
1979
1980 /* Check that we got an whole number of atoms */
1981 if ((len - offset) % atom_len) {
1982 printk(KERN_ERR "%s: Unexpected scan data length %d, "
1983 "atom_len %d, offset %d\n", dev->name, len,
1984 atom_len, offset);
1985 return -EIO;
1986 }
1987
1988 orinoco_clear_scan_results(priv, msecs_to_jiffies(15000));
1989
1990 /* Read the entries one by one */
1991 for (; offset + atom_len <= len; offset += atom_len) {
1992 int found = 0;
David Kilroy3056c402008-08-21 23:27:57 +01001993 struct bss_element *bss = NULL;
Dan Williams1e3428e2007-10-10 23:56:25 -04001994
1995 /* Get next atom */
1996 atom = (union hermes_scan_info *) (buf + offset);
1997
1998 /* Try to update an existing bss first */
1999 list_for_each_entry(bss, &priv->bss_list, list) {
2000 if (compare_ether_addr(bss->bss.a.bssid, atom->a.bssid))
2001 continue;
2002 if (le16_to_cpu(bss->bss.a.essid_len) !=
2003 le16_to_cpu(atom->a.essid_len))
2004 continue;
2005 if (memcmp(bss->bss.a.essid, atom->a.essid,
2006 le16_to_cpu(atom->a.essid_len)))
2007 continue;
Dan Williams1e3428e2007-10-10 23:56:25 -04002008 found = 1;
2009 break;
2010 }
2011
2012 /* Grab a bss off the free list */
2013 if (!found && !list_empty(&priv->bss_free_list)) {
2014 bss = list_entry(priv->bss_free_list.next,
David Kilroy3056c402008-08-21 23:27:57 +01002015 struct bss_element, list);
Dan Williams1e3428e2007-10-10 23:56:25 -04002016 list_del(priv->bss_free_list.next);
2017
Dan Williams1e3428e2007-10-10 23:56:25 -04002018 list_add_tail(&bss->list, &priv->bss_list);
2019 }
Dan Williams22367612007-12-05 11:01:23 -05002020
2021 if (bss) {
2022 /* Always update the BSS to get latest beacon info */
2023 memcpy(&bss->bss, atom, sizeof(bss->bss));
2024 bss->last_scanned = jiffies;
2025 }
Dan Williams1e3428e2007-10-10 23:56:25 -04002026 }
2027
2028 return 0;
2029}
2030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031static void __orinoco_ev_info(struct net_device *dev, hermes_t *hw)
2032{
2033 struct orinoco_private *priv = netdev_priv(dev);
2034 u16 infofid;
2035 struct {
Pavel Roskind133ae42005-09-23 04:18:06 -04002036 __le16 len;
2037 __le16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 } __attribute__ ((packed)) info;
2039 int len, type;
2040 int err;
2041
2042 /* This is an answer to an INQUIRE command that we did earlier,
2043 * or an information "event" generated by the card
2044 * The controller return to us a pseudo frame containing
2045 * the information in question - Jean II */
2046 infofid = hermes_read_regn(hw, INFOFID);
2047
2048 /* Read the info frame header - don't try too hard */
2049 err = hermes_bap_pread(hw, IRQ_BAP, &info, sizeof(info),
2050 infofid, 0);
2051 if (err) {
2052 printk(KERN_ERR "%s: error %d reading info frame. "
2053 "Frame dropped.\n", dev->name, err);
2054 return;
2055 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00002056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
2058 type = le16_to_cpu(info.type);
2059
2060 switch (type) {
2061 case HERMES_INQ_TALLIES: {
2062 struct hermes_tallies_frame tallies;
2063 struct iw_statistics *wstats = &priv->wstats;
David Kilroy6fe9deb2009-02-04 23:05:43 +00002064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 if (len > sizeof(tallies)) {
2066 printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
2067 dev->name, len);
2068 len = sizeof(tallies);
2069 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00002070
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02002071 err = hermes_bap_pread(hw, IRQ_BAP, &tallies, len,
2072 infofid, sizeof(info));
2073 if (err)
2074 break;
David Kilroy6fe9deb2009-02-04 23:05:43 +00002075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 /* Increment our various counters */
2077 /* wstats->discard.nwid - no wrong BSSID stuff */
2078 wstats->discard.code +=
2079 le16_to_cpu(tallies.RxWEPUndecryptable);
David Kilroy6fe9deb2009-02-04 23:05:43 +00002080 if (len == sizeof(tallies))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 wstats->discard.code +=
2082 le16_to_cpu(tallies.RxDiscards_WEPICVError) +
2083 le16_to_cpu(tallies.RxDiscards_WEPExcluded);
2084 wstats->discard.misc +=
2085 le16_to_cpu(tallies.TxDiscardsWrongSA);
2086 wstats->discard.fragment +=
2087 le16_to_cpu(tallies.RxMsgInBadMsgFragments);
2088 wstats->discard.retries +=
2089 le16_to_cpu(tallies.TxRetryLimitExceeded);
2090 /* wstats->miss.beacon - no match */
2091 }
2092 break;
2093 case HERMES_INQ_LINKSTATUS: {
2094 struct hermes_linkstatus linkstatus;
2095 u16 newstatus;
2096 int connected;
2097
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02002098 if (priv->iw_mode == IW_MODE_MONITOR)
2099 break;
2100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (len != sizeof(linkstatus)) {
2102 printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
2103 dev->name, len);
2104 break;
2105 }
2106
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02002107 err = hermes_bap_pread(hw, IRQ_BAP, &linkstatus, len,
2108 infofid, sizeof(info));
2109 if (err)
2110 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 newstatus = le16_to_cpu(linkstatus.linkstatus);
2112
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002113 /* Symbol firmware uses "out of range" to signal that
2114 * the hostscan frame can be requested. */
2115 if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
2116 priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
2117 priv->has_hostscan && priv->scan_inprogress) {
2118 hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
2119 break;
2120 }
2121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
2123 || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
2124 || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
2125
2126 if (connected)
2127 netif_carrier_on(dev);
David Gibson7bb7c3a2005-05-12 20:02:10 -04002128 else if (!ignore_disconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 netif_carrier_off(dev);
2130
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002131 if (newstatus != priv->last_linkstatus) {
2132 priv->last_linkstatus = newstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 print_linkstatus(dev, newstatus);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002134 /* The info frame contains only one word which is the
2135 * status (see hermes.h). The status is pretty boring
2136 * in itself, that's why we export the new BSSID...
2137 * Jean II */
2138 schedule_work(&priv->wevent_work);
2139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
2141 break;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002142 case HERMES_INQ_SCAN:
2143 if (!priv->scan_inprogress && priv->bssid_fixed &&
2144 priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
2145 schedule_work(&priv->join_work);
2146 break;
2147 }
2148 /* fall through */
2149 case HERMES_INQ_HOSTSCAN:
2150 case HERMES_INQ_HOSTSCAN_SYMBOL: {
2151 /* Result of a scanning. Contains information about
2152 * cells in the vicinity - Jean II */
2153 union iwreq_data wrqu;
2154 unsigned char *buf;
2155
Dan Williams1e3428e2007-10-10 23:56:25 -04002156 /* Scan is no longer in progress */
2157 priv->scan_inprogress = 0;
2158
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002159 /* Sanity check */
2160 if (len > 4096) {
2161 printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
2162 dev->name, len);
2163 break;
2164 }
2165
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002166 /* Allocate buffer for results */
2167 buf = kmalloc(len, GFP_ATOMIC);
2168 if (buf == NULL)
2169 /* No memory, so can't printk()... */
2170 break;
2171
2172 /* Read scan data */
2173 err = hermes_bap_pread(hw, IRQ_BAP, (void *) buf, len,
2174 infofid, sizeof(info));
Pavel Roskin708218b2005-09-01 20:05:19 -04002175 if (err) {
2176 kfree(buf);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002177 break;
Pavel Roskin708218b2005-09-01 20:05:19 -04002178 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002179
2180#ifdef ORINOCO_DEBUG
2181 {
2182 int i;
2183 printk(KERN_DEBUG "Scan result [%02X", buf[0]);
David Kilroya94e8422009-02-04 23:05:44 +00002184 for (i = 1; i < (len * 2); i++)
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002185 printk(":%02X", buf[i]);
2186 printk("]\n");
2187 }
2188#endif /* ORINOCO_DEBUG */
2189
Dan Williams1e3428e2007-10-10 23:56:25 -04002190 if (orinoco_process_scan_results(dev, buf, len) == 0) {
2191 /* Send an empty event to user space.
2192 * We don't send the received data on the event because
2193 * it would require us to do complex transcoding, and
2194 * we want to minimise the work done in the irq handler
2195 * Use a request to extract the data - Jean II */
2196 wrqu.data.length = 0;
2197 wrqu.data.flags = 0;
2198 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
2199 }
2200 kfree(buf);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002201 }
2202 break;
David Kilroy01632fa2008-08-21 23:27:58 +01002203 case HERMES_INQ_CHANNELINFO:
2204 {
2205 struct agere_ext_scan_info *bss;
2206
2207 if (!priv->scan_inprogress) {
2208 printk(KERN_DEBUG "%s: Got chaninfo without scan, "
2209 "len=%d\n", dev->name, len);
2210 break;
2211 }
2212
2213 /* An empty result indicates that the scan is complete */
2214 if (len == 0) {
2215 union iwreq_data wrqu;
2216
2217 /* Scan is no longer in progress */
2218 priv->scan_inprogress = 0;
2219
2220 wrqu.data.length = 0;
2221 wrqu.data.flags = 0;
2222 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
2223 break;
2224 }
2225
2226 /* Sanity check */
2227 else if (len > sizeof(*bss)) {
2228 printk(KERN_WARNING
2229 "%s: Ext scan results too large (%d bytes). "
2230 "Truncating results to %zd bytes.\n",
2231 dev->name, len, sizeof(*bss));
2232 len = sizeof(*bss);
2233 } else if (len < (offsetof(struct agere_ext_scan_info,
2234 data) + 2)) {
2235 /* Drop this result now so we don't have to
2236 * keep checking later */
2237 printk(KERN_WARNING
2238 "%s: Ext scan results too short (%d bytes)\n",
2239 dev->name, len);
2240 break;
2241 }
2242
2243 bss = kmalloc(sizeof(*bss), GFP_ATOMIC);
2244 if (bss == NULL)
2245 break;
2246
2247 /* Read scan data */
2248 err = hermes_bap_pread(hw, IRQ_BAP, (void *) bss, len,
2249 infofid, sizeof(info));
2250 if (err) {
2251 kfree(bss);
2252 break;
2253 }
2254
2255 orinoco_add_ext_scan_result(priv, bss);
2256
2257 kfree(bss);
2258 break;
2259 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002260 case HERMES_INQ_SEC_STAT_AGERE:
2261 /* Security status (Agere specific) */
2262 /* Ignore this frame for now */
2263 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
2264 break;
2265 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 default:
2267 printk(KERN_DEBUG "%s: Unknown information frame received: "
2268 "type 0x%04x, length %d\n", dev->name, type, len);
2269 /* We don't actually do anything about it */
2270 break;
2271 }
2272}
2273
2274static void __orinoco_ev_infdrop(struct net_device *dev, hermes_t *hw)
2275{
2276 if (net_ratelimit())
2277 printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
2278}
2279
2280/********************************************************************/
2281/* Internal hardware control routines */
2282/********************************************************************/
2283
2284int __orinoco_up(struct net_device *dev)
2285{
2286 struct orinoco_private *priv = netdev_priv(dev);
2287 struct hermes *hw = &priv->hw;
2288 int err;
2289
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02002290 netif_carrier_off(dev); /* just to make sure */
2291
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 err = __orinoco_program_rids(dev);
2293 if (err) {
2294 printk(KERN_ERR "%s: Error %d configuring card\n",
2295 dev->name, err);
2296 return err;
2297 }
2298
2299 /* Fire things up again */
2300 hermes_set_irqmask(hw, ORINOCO_INTEN);
2301 err = hermes_enable_port(hw, 0);
2302 if (err) {
2303 printk(KERN_ERR "%s: Error %d enabling MAC port\n",
2304 dev->name, err);
2305 return err;
2306 }
2307
2308 netif_start_queue(dev);
2309
2310 return 0;
2311}
2312
2313int __orinoco_down(struct net_device *dev)
2314{
2315 struct orinoco_private *priv = netdev_priv(dev);
2316 struct hermes *hw = &priv->hw;
2317 int err;
2318
2319 netif_stop_queue(dev);
2320
David Kilroya94e8422009-02-04 23:05:44 +00002321 if (!priv->hw_unavailable) {
2322 if (!priv->broken_disableport) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 err = hermes_disable_port(hw, 0);
2324 if (err) {
2325 /* Some firmwares (e.g. Intersil 1.3.x) seem
2326 * to have problems disabling the port, oh
2327 * well, too bad. */
2328 printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
2329 dev->name, err);
2330 priv->broken_disableport = 1;
2331 }
2332 }
2333 hermes_set_irqmask(hw, 0);
2334 hermes_write_regn(hw, EVACK, 0xffff);
2335 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00002336
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 /* firmware will have to reassociate */
2338 netif_carrier_off(dev);
2339 priv->last_linkstatus = 0xffff;
2340
2341 return 0;
2342}
2343
Pavel Roskin37a6c612006-04-07 04:10:49 -04002344static int orinoco_allocate_fid(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
2346 struct orinoco_private *priv = netdev_priv(dev);
2347 struct hermes *hw = &priv->hw;
2348 int err;
2349
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 err = hermes_allocate(hw, priv->nicbuf_size, &priv->txfid);
David Gibsonb24d4582005-05-12 20:04:16 -04002351 if (err == -EIO && priv->nicbuf_size > TX_NICBUF_SIZE_BUG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 /* Try workaround for old Symbol firmware bug */
2353 printk(KERN_WARNING "%s: firmware ALLOC bug detected "
2354 "(old Symbol firmware?). Trying to work around... ",
2355 dev->name);
David Kilroy6fe9deb2009-02-04 23:05:43 +00002356
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 priv->nicbuf_size = TX_NICBUF_SIZE_BUG;
2358 err = hermes_allocate(hw, priv->nicbuf_size, &priv->txfid);
2359 if (err)
2360 printk("failed!\n");
2361 else
2362 printk("ok.\n");
2363 }
2364
2365 return err;
2366}
2367
Pavel Roskin37a6c612006-04-07 04:10:49 -04002368int orinoco_reinit_firmware(struct net_device *dev)
2369{
2370 struct orinoco_private *priv = netdev_priv(dev);
2371 struct hermes *hw = &priv->hw;
2372 int err;
2373
2374 err = hermes_init(hw);
Andrey Borzenkov0df6cbb2008-10-12 20:15:43 +04002375 if (priv->do_fw_download && !err) {
2376 err = orinoco_download(priv);
2377 if (err)
2378 priv->do_fw_download = 0;
2379 }
Pavel Roskin37a6c612006-04-07 04:10:49 -04002380 if (!err)
2381 err = orinoco_allocate_fid(dev);
2382
2383 return err;
2384}
2385
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386static int __orinoco_hw_set_bitrate(struct orinoco_private *priv)
2387{
2388 hermes_t *hw = &priv->hw;
David Kilroyb2f30a02009-02-04 23:05:46 +00002389 int ratemode = priv->bitratemode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 int err = 0;
2391
David Kilroyb2f30a02009-02-04 23:05:46 +00002392 if (ratemode >= BITRATE_TABLE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 printk(KERN_ERR "%s: BUG: Invalid bitrate mode %d\n",
David Kilroyb2f30a02009-02-04 23:05:46 +00002394 priv->ndev->name, ratemode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 return -EINVAL;
2396 }
2397
2398 switch (priv->firmware_type) {
2399 case FIRMWARE_TYPE_AGERE:
2400 err = hermes_write_wordrec(hw, USER_BAP,
David Kilroyb2f30a02009-02-04 23:05:46 +00002401 HERMES_RID_CNFTXRATECONTROL,
2402 bitrate_table[ratemode].agere_txratectrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 break;
2404 case FIRMWARE_TYPE_INTERSIL:
2405 case FIRMWARE_TYPE_SYMBOL:
2406 err = hermes_write_wordrec(hw, USER_BAP,
David Kilroyb2f30a02009-02-04 23:05:46 +00002407 HERMES_RID_CNFTXRATECONTROL,
2408 bitrate_table[ratemode].intersil_txratectrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 break;
2410 default:
2411 BUG();
2412 }
2413
2414 return err;
2415}
2416
Christoph Hellwig16739b02005-06-19 01:27:51 +02002417/* Set fixed AP address */
2418static int __orinoco_hw_set_wap(struct orinoco_private *priv)
2419{
2420 int roaming_flag;
2421 int err = 0;
2422 hermes_t *hw = &priv->hw;
2423
2424 switch (priv->firmware_type) {
2425 case FIRMWARE_TYPE_AGERE:
2426 /* not supported */
2427 break;
2428 case FIRMWARE_TYPE_INTERSIL:
2429 if (priv->bssid_fixed)
2430 roaming_flag = 2;
2431 else
2432 roaming_flag = 1;
2433
2434 err = hermes_write_wordrec(hw, USER_BAP,
2435 HERMES_RID_CNFROAMINGMODE,
2436 roaming_flag);
2437 break;
2438 case FIRMWARE_TYPE_SYMBOL:
2439 err = HERMES_WRITE_RECORD(hw, USER_BAP,
2440 HERMES_RID_CNFMANDATORYBSSID_SYMBOL,
2441 &priv->desired_bssid);
2442 break;
2443 }
2444 return err;
2445}
2446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447/* Change the WEP keys and/or the current keys. Can be called
David Kilroyd03032a2008-08-21 23:28:02 +01002448 * either from __orinoco_hw_setup_enc() or directly from
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 * orinoco_ioctl_setiwencode(). In the later case the association
2450 * with the AP is not broken (if the firmware can handle it),
2451 * which is needed for 802.1x implementations. */
2452static int __orinoco_hw_setup_wepkeys(struct orinoco_private *priv)
2453{
2454 hermes_t *hw = &priv->hw;
2455 int err = 0;
2456
2457 switch (priv->firmware_type) {
2458 case FIRMWARE_TYPE_AGERE:
2459 err = HERMES_WRITE_RECORD(hw, USER_BAP,
2460 HERMES_RID_CNFWEPKEYS_AGERE,
2461 &priv->keys);
2462 if (err)
2463 return err;
2464 err = hermes_write_wordrec(hw, USER_BAP,
2465 HERMES_RID_CNFTXKEY_AGERE,
2466 priv->tx_key);
2467 if (err)
2468 return err;
2469 break;
2470 case FIRMWARE_TYPE_INTERSIL:
2471 case FIRMWARE_TYPE_SYMBOL:
2472 {
2473 int keylen;
2474 int i;
2475
David Kilroyb2f30a02009-02-04 23:05:46 +00002476 /* Force uniform key length to work around
2477 * firmware bugs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 keylen = le16_to_cpu(priv->keys[priv->tx_key].len);
David Kilroy6fe9deb2009-02-04 23:05:43 +00002479
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 if (keylen > LARGE_KEY_SIZE) {
2481 printk(KERN_ERR "%s: BUG: Key %d has oversize length %d.\n",
2482 priv->ndev->name, priv->tx_key, keylen);
2483 return -E2BIG;
2484 }
2485
2486 /* Write all 4 keys */
David Kilroya94e8422009-02-04 23:05:44 +00002487 for (i = 0; i < ORINOCO_MAX_KEYS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 err = hermes_write_ltv(hw, USER_BAP,
David Kilroyb2f30a02009-02-04 23:05:46 +00002489 HERMES_RID_CNFDEFAULTKEY0 + i,
2490 HERMES_BYTES_TO_RECLEN(keylen),
2491 priv->keys[i].data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 if (err)
2493 return err;
2494 }
2495
2496 /* Write the index of the key used in transmission */
2497 err = hermes_write_wordrec(hw, USER_BAP,
David Kilroyb2f30a02009-02-04 23:05:46 +00002498 HERMES_RID_CNFWEPDEFAULTKEYID,
2499 priv->tx_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 if (err)
2501 return err;
2502 }
2503 break;
2504 }
2505
2506 return 0;
2507}
2508
David Kilroyd03032a2008-08-21 23:28:02 +01002509static int __orinoco_hw_setup_enc(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510{
2511 hermes_t *hw = &priv->hw;
2512 int err = 0;
2513 int master_wep_flag;
2514 int auth_flag;
David Kilroy4ae6ee22008-08-21 23:27:59 +01002515 int enc_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
David Kilroyd03032a2008-08-21 23:28:02 +01002517 /* Setup WEP keys for WEP and WPA */
2518 if (priv->encode_alg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 __orinoco_hw_setup_wepkeys(priv);
2520
2521 if (priv->wep_restrict)
2522 auth_flag = HERMES_AUTH_SHARED_KEY;
2523 else
2524 auth_flag = HERMES_AUTH_OPEN;
2525
David Kilroyd03032a2008-08-21 23:28:02 +01002526 if (priv->wpa_enabled)
2527 enc_flag = 2;
2528 else if (priv->encode_alg == IW_ENCODE_ALG_WEP)
David Kilroy4ae6ee22008-08-21 23:27:59 +01002529 enc_flag = 1;
2530 else
2531 enc_flag = 0;
2532
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 switch (priv->firmware_type) {
2534 case FIRMWARE_TYPE_AGERE: /* Agere style WEP */
David Kilroy4ae6ee22008-08-21 23:27:59 +01002535 if (priv->encode_alg == IW_ENCODE_ALG_WEP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 /* Enable the shared-key authentication. */
2537 err = hermes_write_wordrec(hw, USER_BAP,
David Kilroyb2f30a02009-02-04 23:05:46 +00002538 HERMES_RID_CNFAUTHENTICATION_AGERE,
2539 auth_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 }
2541 err = hermes_write_wordrec(hw, USER_BAP,
2542 HERMES_RID_CNFWEPENABLED_AGERE,
David Kilroy4ae6ee22008-08-21 23:27:59 +01002543 enc_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 if (err)
2545 return err;
David Kilroyd03032a2008-08-21 23:28:02 +01002546
2547 if (priv->has_wpa) {
2548 /* Set WPA key management */
2549 err = hermes_write_wordrec(hw, USER_BAP,
2550 HERMES_RID_CNFSETWPAAUTHMGMTSUITE_AGERE,
2551 priv->key_mgmt);
2552 if (err)
2553 return err;
2554 }
2555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 break;
2557
2558 case FIRMWARE_TYPE_INTERSIL: /* Intersil style WEP */
2559 case FIRMWARE_TYPE_SYMBOL: /* Symbol style WEP */
David Kilroy4ae6ee22008-08-21 23:27:59 +01002560 if (priv->encode_alg == IW_ENCODE_ALG_WEP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 if (priv->wep_restrict ||
2562 (priv->firmware_type == FIRMWARE_TYPE_SYMBOL))
2563 master_wep_flag = HERMES_WEP_PRIVACY_INVOKED |
2564 HERMES_WEP_EXCL_UNENCRYPTED;
2565 else
2566 master_wep_flag = HERMES_WEP_PRIVACY_INVOKED;
2567
2568 err = hermes_write_wordrec(hw, USER_BAP,
2569 HERMES_RID_CNFAUTHENTICATION,
2570 auth_flag);
2571 if (err)
2572 return err;
2573 } else
2574 master_wep_flag = 0;
2575
2576 if (priv->iw_mode == IW_MODE_MONITOR)
2577 master_wep_flag |= HERMES_WEP_HOST_DECRYPT;
2578
2579 /* Master WEP setting : on/off */
2580 err = hermes_write_wordrec(hw, USER_BAP,
2581 HERMES_RID_CNFWEPFLAGS_INTERSIL,
2582 master_wep_flag);
2583 if (err)
David Kilroy6fe9deb2009-02-04 23:05:43 +00002584 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
2586 break;
2587 }
2588
2589 return 0;
2590}
2591
David Kilroyd03032a2008-08-21 23:28:02 +01002592/* key must be 32 bytes, including the tx and rx MIC keys.
2593 * rsc must be 8 bytes
2594 * tsc must be 8 bytes or NULL
2595 */
2596static int __orinoco_hw_set_tkip_key(hermes_t *hw, int key_idx, int set_tx,
2597 u8 *key, u8 *rsc, u8 *tsc)
2598{
2599 struct {
2600 __le16 idx;
2601 u8 rsc[IW_ENCODE_SEQ_MAX_SIZE];
2602 u8 key[TKIP_KEYLEN];
2603 u8 tx_mic[MIC_KEYLEN];
2604 u8 rx_mic[MIC_KEYLEN];
2605 u8 tsc[IW_ENCODE_SEQ_MAX_SIZE];
2606 } __attribute__ ((packed)) buf;
2607 int ret;
2608 int err;
2609 int k;
2610 u16 xmitting;
2611
2612 key_idx &= 0x3;
2613
2614 if (set_tx)
2615 key_idx |= 0x8000;
2616
2617 buf.idx = cpu_to_le16(key_idx);
2618 memcpy(buf.key, key,
2619 sizeof(buf.key) + sizeof(buf.tx_mic) + sizeof(buf.rx_mic));
2620
2621 if (rsc == NULL)
2622 memset(buf.rsc, 0, sizeof(buf.rsc));
2623 else
2624 memcpy(buf.rsc, rsc, sizeof(buf.rsc));
2625
2626 if (tsc == NULL) {
2627 memset(buf.tsc, 0, sizeof(buf.tsc));
2628 buf.tsc[4] = 0x10;
2629 } else {
2630 memcpy(buf.tsc, tsc, sizeof(buf.tsc));
2631 }
2632
2633 /* Wait upto 100ms for tx queue to empty */
2634 k = 100;
2635 do {
2636 k--;
2637 udelay(1000);
2638 ret = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_TXQUEUEEMPTY,
2639 &xmitting);
2640 if (ret)
2641 break;
2642 } while ((k > 0) && xmitting);
2643
2644 if (k == 0)
2645 ret = -ETIMEDOUT;
2646
2647 err = HERMES_WRITE_RECORD(hw, USER_BAP,
2648 HERMES_RID_CNFADDDEFAULTTKIPKEY_AGERE,
2649 &buf);
2650
2651 return ret ? ret : err;
2652}
2653
2654static int orinoco_clear_tkip_key(struct orinoco_private *priv,
2655 int key_idx)
2656{
2657 hermes_t *hw = &priv->hw;
2658 int err;
2659
2660 memset(&priv->tkip_key[key_idx], 0, sizeof(priv->tkip_key[key_idx]));
2661 err = hermes_write_wordrec(hw, USER_BAP,
2662 HERMES_RID_CNFREMDEFAULTTKIPKEY_AGERE,
2663 key_idx);
2664 if (err)
2665 printk(KERN_WARNING "%s: Error %d clearing TKIP key %d\n",
2666 priv->ndev->name, err, key_idx);
2667 return err;
2668}
2669
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670static int __orinoco_program_rids(struct net_device *dev)
2671{
2672 struct orinoco_private *priv = netdev_priv(dev);
2673 hermes_t *hw = &priv->hw;
2674 int err;
2675 struct hermes_idstring idbuf;
2676
2677 /* Set the MAC address */
2678 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
2679 HERMES_BYTES_TO_RECLEN(ETH_ALEN), dev->dev_addr);
2680 if (err) {
2681 printk(KERN_ERR "%s: Error %d setting MAC address\n",
2682 dev->name, err);
2683 return err;
2684 }
2685
2686 /* Set up the link mode */
2687 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFPORTTYPE,
2688 priv->port_type);
2689 if (err) {
2690 printk(KERN_ERR "%s: Error %d setting port type\n",
2691 dev->name, err);
2692 return err;
2693 }
2694 /* Set the channel/frequency */
David Gibsond51d8b12005-05-12 20:03:36 -04002695 if (priv->channel != 0 && priv->iw_mode != IW_MODE_INFRA) {
2696 err = hermes_write_wordrec(hw, USER_BAP,
2697 HERMES_RID_CNFOWNCHANNEL,
2698 priv->channel);
2699 if (err) {
2700 printk(KERN_ERR "%s: Error %d setting channel %d\n",
2701 dev->name, err, priv->channel);
2702 return err;
2703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 }
2705
2706 if (priv->has_ibss) {
2707 u16 createibss;
2708
2709 if ((strlen(priv->desired_essid) == 0) && (priv->createibss)) {
2710 printk(KERN_WARNING "%s: This firmware requires an "
2711 "ESSID in IBSS-Ad-Hoc mode.\n", dev->name);
2712 /* With wvlan_cs, in this case, we would crash.
2713 * hopefully, this driver will behave better...
2714 * Jean II */
2715 createibss = 0;
2716 } else {
2717 createibss = priv->createibss;
2718 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00002719
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 err = hermes_write_wordrec(hw, USER_BAP,
2721 HERMES_RID_CNFCREATEIBSS,
2722 createibss);
2723 if (err) {
2724 printk(KERN_ERR "%s: Error %d setting CREATEIBSS\n",
2725 dev->name, err);
2726 return err;
2727 }
2728 }
2729
Christoph Hellwig16739b02005-06-19 01:27:51 +02002730 /* Set the desired BSSID */
2731 err = __orinoco_hw_set_wap(priv);
2732 if (err) {
2733 printk(KERN_ERR "%s: Error %d setting AP address\n",
2734 dev->name, err);
2735 return err;
2736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 /* Set the desired ESSID */
2738 idbuf.len = cpu_to_le16(strlen(priv->desired_essid));
2739 memcpy(&idbuf.val, priv->desired_essid, sizeof(idbuf.val));
2740 /* WinXP wants partner to configure OWNSSID even in IBSS mode. (jimc) */
2741 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNSSID,
David Kilroyb2f30a02009-02-04 23:05:46 +00002742 HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
2743 &idbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 if (err) {
2745 printk(KERN_ERR "%s: Error %d setting OWNSSID\n",
2746 dev->name, err);
2747 return err;
2748 }
2749 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFDESIREDSSID,
David Kilroyb2f30a02009-02-04 23:05:46 +00002750 HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
2751 &idbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 if (err) {
2753 printk(KERN_ERR "%s: Error %d setting DESIREDSSID\n",
2754 dev->name, err);
2755 return err;
2756 }
2757
2758 /* Set the station name */
2759 idbuf.len = cpu_to_le16(strlen(priv->nick));
2760 memcpy(&idbuf.val, priv->nick, sizeof(idbuf.val));
2761 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
2762 HERMES_BYTES_TO_RECLEN(strlen(priv->nick)+2),
2763 &idbuf);
2764 if (err) {
2765 printk(KERN_ERR "%s: Error %d setting nickname\n",
2766 dev->name, err);
2767 return err;
2768 }
2769
2770 /* Set AP density */
2771 if (priv->has_sensitivity) {
2772 err = hermes_write_wordrec(hw, USER_BAP,
2773 HERMES_RID_CNFSYSTEMSCALE,
2774 priv->ap_density);
2775 if (err) {
David Kilroyb2f30a02009-02-04 23:05:46 +00002776 printk(KERN_WARNING "%s: Error %d setting SYSTEMSCALE. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 "Disabling sensitivity control\n",
2778 dev->name, err);
2779
2780 priv->has_sensitivity = 0;
2781 }
2782 }
2783
2784 /* Set RTS threshold */
2785 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
2786 priv->rts_thresh);
2787 if (err) {
2788 printk(KERN_ERR "%s: Error %d setting RTS threshold\n",
2789 dev->name, err);
2790 return err;
2791 }
2792
2793 /* Set fragmentation threshold or MWO robustness */
2794 if (priv->has_mwo)
2795 err = hermes_write_wordrec(hw, USER_BAP,
2796 HERMES_RID_CNFMWOROBUST_AGERE,
2797 priv->mwo_robust);
2798 else
2799 err = hermes_write_wordrec(hw, USER_BAP,
2800 HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
2801 priv->frag_thresh);
2802 if (err) {
2803 printk(KERN_ERR "%s: Error %d setting fragmentation\n",
2804 dev->name, err);
2805 return err;
2806 }
2807
2808 /* Set bitrate */
2809 err = __orinoco_hw_set_bitrate(priv);
2810 if (err) {
2811 printk(KERN_ERR "%s: Error %d setting bitrate\n",
2812 dev->name, err);
2813 return err;
2814 }
2815
2816 /* Set power management */
2817 if (priv->has_pm) {
2818 err = hermes_write_wordrec(hw, USER_BAP,
2819 HERMES_RID_CNFPMENABLED,
2820 priv->pm_on);
2821 if (err) {
2822 printk(KERN_ERR "%s: Error %d setting up PM\n",
2823 dev->name, err);
2824 return err;
2825 }
2826
2827 err = hermes_write_wordrec(hw, USER_BAP,
2828 HERMES_RID_CNFMULTICASTRECEIVE,
2829 priv->pm_mcast);
2830 if (err) {
2831 printk(KERN_ERR "%s: Error %d setting up PM\n",
2832 dev->name, err);
2833 return err;
2834 }
2835 err = hermes_write_wordrec(hw, USER_BAP,
2836 HERMES_RID_CNFMAXSLEEPDURATION,
2837 priv->pm_period);
2838 if (err) {
2839 printk(KERN_ERR "%s: Error %d setting up PM\n",
2840 dev->name, err);
2841 return err;
2842 }
2843 err = hermes_write_wordrec(hw, USER_BAP,
2844 HERMES_RID_CNFPMHOLDOVERDURATION,
2845 priv->pm_timeout);
2846 if (err) {
2847 printk(KERN_ERR "%s: Error %d setting up PM\n",
2848 dev->name, err);
2849 return err;
2850 }
2851 }
2852
2853 /* Set preamble - only for Symbol so far... */
2854 if (priv->has_preamble) {
2855 err = hermes_write_wordrec(hw, USER_BAP,
2856 HERMES_RID_CNFPREAMBLE_SYMBOL,
2857 priv->preamble);
2858 if (err) {
2859 printk(KERN_ERR "%s: Error %d setting preamble\n",
2860 dev->name, err);
2861 return err;
2862 }
2863 }
2864
2865 /* Set up encryption */
David Kilroyd03032a2008-08-21 23:28:02 +01002866 if (priv->has_wep || priv->has_wpa) {
2867 err = __orinoco_hw_setup_enc(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 if (err) {
David Kilroyd03032a2008-08-21 23:28:02 +01002869 printk(KERN_ERR "%s: Error %d activating encryption\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 dev->name, err);
2871 return err;
2872 }
2873 }
2874
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02002875 if (priv->iw_mode == IW_MODE_MONITOR) {
2876 /* Enable monitor mode */
2877 dev->type = ARPHRD_IEEE80211;
David Kilroy6fe9deb2009-02-04 23:05:43 +00002878 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02002879 HERMES_TEST_MONITOR, 0, NULL);
2880 } else {
2881 /* Disable monitor mode */
2882 dev->type = ARPHRD_ETHER;
2883 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
2884 HERMES_TEST_STOP, 0, NULL);
2885 }
2886 if (err)
2887 return err;
2888
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 /* Set promiscuity / multicast*/
2890 priv->promiscuous = 0;
2891 priv->mc_count = 0;
Herbert Xu932ff272006-06-09 12:20:56 -07002892
2893 /* FIXME: what about netif_tx_lock */
2894 __orinoco_set_multicast_list(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
2896 return 0;
2897}
2898
2899/* FIXME: return int? */
2900static void
2901__orinoco_set_multicast_list(struct net_device *dev)
2902{
2903 struct orinoco_private *priv = netdev_priv(dev);
2904 hermes_t *hw = &priv->hw;
2905 int err = 0;
2906 int promisc, mc_count;
2907
2908 /* The Hermes doesn't seem to have an allmulti mode, so we go
2909 * into promiscuous mode and let the upper levels deal. */
David Kilroya94e8422009-02-04 23:05:44 +00002910 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
2911 (dev->mc_count > MAX_MULTICAST(priv))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 promisc = 1;
2913 mc_count = 0;
2914 } else {
2915 promisc = 0;
2916 mc_count = dev->mc_count;
2917 }
2918
2919 if (promisc != priv->promiscuous) {
2920 err = hermes_write_wordrec(hw, USER_BAP,
2921 HERMES_RID_CNFPROMISCUOUSMODE,
2922 promisc);
2923 if (err) {
2924 printk(KERN_ERR "%s: Error %d setting PROMISCUOUSMODE to 1.\n",
2925 dev->name, err);
David Kilroy6fe9deb2009-02-04 23:05:43 +00002926 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 priv->promiscuous = promisc;
2928 }
2929
David Kilroy667d4102008-08-23 19:03:34 +01002930 /* If we're not in promiscuous mode, then we need to set the
2931 * group address if either we want to multicast, or if we were
2932 * multicasting and want to stop */
David Kilroya94e8422009-02-04 23:05:44 +00002933 if (!promisc && (mc_count || priv->mc_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 struct dev_mc_list *p = dev->mc_list;
2935 struct hermes_multicast mclist;
2936 int i;
2937
2938 for (i = 0; i < mc_count; i++) {
2939 /* paranoia: is list shorter than mc_count? */
David Kilroya94e8422009-02-04 23:05:44 +00002940 BUG_ON(!p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 /* paranoia: bad address size in list? */
2942 BUG_ON(p->dmi_addrlen != ETH_ALEN);
David Kilroy6fe9deb2009-02-04 23:05:43 +00002943
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 memcpy(mclist.addr[i], p->dmi_addr, ETH_ALEN);
2945 p = p->next;
2946 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00002947
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 if (p)
2949 printk(KERN_WARNING "%s: Multicast list is "
2950 "longer than mc_count\n", dev->name);
2951
David Kilroy667d4102008-08-23 19:03:34 +01002952 err = hermes_write_ltv(hw, USER_BAP,
2953 HERMES_RID_CNFGROUPADDRESSES,
2954 HERMES_BYTES_TO_RECLEN(mc_count * ETH_ALEN),
2955 &mclist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 if (err)
2957 printk(KERN_ERR "%s: Error %d setting multicast list.\n",
2958 dev->name, err);
2959 else
2960 priv->mc_count = mc_count;
2961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962}
2963
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964/* This must be called from user context, without locks held - use
2965 * schedule_work() */
David Howellsc4028952006-11-22 14:57:56 +00002966static void orinoco_reset(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967{
David Howellsc4028952006-11-22 14:57:56 +00002968 struct orinoco_private *priv =
2969 container_of(work, struct orinoco_private, reset_work);
2970 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 struct hermes *hw = &priv->hw;
Christoph Hellwig8551cb92005-05-14 17:30:04 +02002972 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 unsigned long flags;
2974
2975 if (orinoco_lock(priv, &flags) != 0)
2976 /* When the hardware becomes available again, whatever
2977 * detects that is responsible for re-initializing
2978 * it. So no need for anything further */
2979 return;
2980
2981 netif_stop_queue(dev);
2982
2983 /* Shut off interrupts. Depending on what state the hardware
2984 * is in, this might not work, but we'll try anyway */
2985 hermes_set_irqmask(hw, 0);
2986 hermes_write_regn(hw, EVACK, 0xffff);
2987
2988 priv->hw_unavailable++;
2989 priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
2990 netif_carrier_off(dev);
2991
2992 orinoco_unlock(priv, &flags);
2993
David Kilroy6fe9deb2009-02-04 23:05:43 +00002994 /* Scanning support: Cleanup of driver struct */
Dan Williams1e3428e2007-10-10 23:56:25 -04002995 orinoco_clear_scan_results(priv, 0);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002996 priv->scan_inprogress = 0;
2997
Christoph Hellwig8551cb92005-05-14 17:30:04 +02002998 if (priv->hard_reset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 err = (*priv->hard_reset)(priv);
Christoph Hellwig8551cb92005-05-14 17:30:04 +02003000 if (err) {
3001 printk(KERN_ERR "%s: orinoco_reset: Error %d "
3002 "performing hard reset\n", dev->name, err);
3003 goto disable;
3004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 }
3006
3007 err = orinoco_reinit_firmware(dev);
3008 if (err) {
3009 printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
3010 dev->name, err);
Christoph Hellwig8551cb92005-05-14 17:30:04 +02003011 goto disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 }
3013
David Kilroyb2f30a02009-02-04 23:05:46 +00003014 /* This has to be called from user context */
3015 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
3017 priv->hw_unavailable--;
3018
3019 /* priv->open or priv->hw_unavailable might have changed while
3020 * we dropped the lock */
David Kilroya94e8422009-02-04 23:05:44 +00003021 if (priv->open && (!priv->hw_unavailable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 err = __orinoco_up(dev);
3023 if (err) {
3024 printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
3025 dev->name, err);
3026 } else
3027 dev->trans_start = jiffies;
3028 }
3029
3030 spin_unlock_irq(&priv->lock);
3031
3032 return;
Christoph Hellwig8551cb92005-05-14 17:30:04 +02003033 disable:
3034 hermes_set_irqmask(hw, 0);
3035 netif_device_detach(dev);
3036 printk(KERN_ERR "%s: Device has been disabled!\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037}
3038
3039/********************************************************************/
3040/* Interrupt handler */
3041/********************************************************************/
3042
3043static void __orinoco_ev_tick(struct net_device *dev, hermes_t *hw)
3044{
3045 printk(KERN_DEBUG "%s: TICK\n", dev->name);
3046}
3047
3048static void __orinoco_ev_wterr(struct net_device *dev, hermes_t *hw)
3049{
3050 /* This seems to happen a fair bit under load, but ignoring it
3051 seems to work fine...*/
3052 printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
3053 dev->name);
3054}
3055
David Howells7d12e782006-10-05 14:55:46 +01003056irqreturn_t orinoco_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
Jeff Garzikc31f28e2006-10-06 14:56:04 -04003058 struct net_device *dev = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 struct orinoco_private *priv = netdev_priv(dev);
3060 hermes_t *hw = &priv->hw;
3061 int count = MAX_IRQLOOPS_PER_IRQ;
3062 u16 evstat, events;
3063 /* These are used to detect a runaway interrupt situation */
3064 /* If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
3065 * we panic and shut down the hardware */
3066 static int last_irq_jiffy = 0; /* jiffies value the last time
3067 * we were called */
3068 static int loops_this_jiffy = 0;
3069 unsigned long flags;
3070
3071 if (orinoco_lock(priv, &flags) != 0) {
3072 /* If hw is unavailable - we don't know if the irq was
3073 * for us or not */
3074 return IRQ_HANDLED;
3075 }
3076
3077 evstat = hermes_read_regn(hw, EVSTAT);
3078 events = evstat & hw->inten;
David Kilroya94e8422009-02-04 23:05:44 +00003079 if (!events) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 orinoco_unlock(priv, &flags);
3081 return IRQ_NONE;
3082 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00003083
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 if (jiffies != last_irq_jiffy)
3085 loops_this_jiffy = 0;
3086 last_irq_jiffy = jiffies;
3087
3088 while (events && count--) {
3089 if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
3090 printk(KERN_WARNING "%s: IRQ handler is looping too "
3091 "much! Resetting.\n", dev->name);
3092 /* Disable interrupts for now */
3093 hermes_set_irqmask(hw, 0);
3094 schedule_work(&priv->reset_work);
3095 break;
3096 }
3097
3098 /* Check the card hasn't been removed */
David Kilroya94e8422009-02-04 23:05:44 +00003099 if (!hermes_present(hw)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 DEBUG(0, "orinoco_interrupt(): card removed\n");
3101 break;
3102 }
3103
3104 if (events & HERMES_EV_TICK)
3105 __orinoco_ev_tick(dev, hw);
3106 if (events & HERMES_EV_WTERR)
3107 __orinoco_ev_wterr(dev, hw);
3108 if (events & HERMES_EV_INFDROP)
3109 __orinoco_ev_infdrop(dev, hw);
3110 if (events & HERMES_EV_INFO)
3111 __orinoco_ev_info(dev, hw);
3112 if (events & HERMES_EV_RX)
3113 __orinoco_ev_rx(dev, hw);
3114 if (events & HERMES_EV_TXEXC)
3115 __orinoco_ev_txexc(dev, hw);
3116 if (events & HERMES_EV_TX)
3117 __orinoco_ev_tx(dev, hw);
3118 if (events & HERMES_EV_ALLOC)
3119 __orinoco_ev_alloc(dev, hw);
David Kilroy6fe9deb2009-02-04 23:05:43 +00003120
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02003121 hermes_write_regn(hw, EVACK, evstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
3123 evstat = hermes_read_regn(hw, EVSTAT);
3124 events = evstat & hw->inten;
3125 };
3126
3127 orinoco_unlock(priv, &flags);
3128 return IRQ_HANDLED;
3129}
3130
3131/********************************************************************/
David Kilroy39d1ffe2008-11-22 10:37:28 +00003132/* Power management */
3133/********************************************************************/
3134#if defined(CONFIG_PM_SLEEP) && !defined(CONFIG_HERMES_CACHE_FW_ON_INIT)
3135static int orinoco_pm_notifier(struct notifier_block *notifier,
3136 unsigned long pm_event,
3137 void *unused)
3138{
3139 struct orinoco_private *priv = container_of(notifier,
3140 struct orinoco_private,
3141 pm_notifier);
3142
3143 /* All we need to do is cache the firmware before suspend, and
3144 * release it when we come out.
3145 *
3146 * Only need to do this if we're downloading firmware. */
3147 if (!priv->do_fw_download)
3148 return NOTIFY_DONE;
3149
3150 switch (pm_event) {
3151 case PM_HIBERNATION_PREPARE:
3152 case PM_SUSPEND_PREPARE:
3153 orinoco_cache_fw(priv, 0);
3154 break;
3155
3156 case PM_POST_RESTORE:
3157 /* Restore from hibernation failed. We need to clean
3158 * up in exactly the same way, so fall through. */
3159 case PM_POST_HIBERNATION:
3160 case PM_POST_SUSPEND:
3161 orinoco_uncache_fw(priv);
3162 break;
3163
3164 case PM_RESTORE_PREPARE:
3165 default:
3166 break;
3167 }
3168
3169 return NOTIFY_DONE;
3170}
3171#else /* !PM_SLEEP || HERMES_CACHE_FW_ON_INIT */
3172#define orinoco_pm_notifier NULL
3173#endif
3174
3175/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176/* Initialization */
3177/********************************************************************/
3178
3179struct comp_id {
3180 u16 id, variant, major, minor;
3181} __attribute__ ((packed));
3182
3183static inline fwtype_t determine_firmware_type(struct comp_id *nic_id)
3184{
3185 if (nic_id->id < 0x8000)
3186 return FIRMWARE_TYPE_AGERE;
3187 else if (nic_id->id == 0x8000 && nic_id->major == 0)
3188 return FIRMWARE_TYPE_SYMBOL;
3189 else
3190 return FIRMWARE_TYPE_INTERSIL;
3191}
3192
3193/* Set priv->firmware type, determine firmware properties */
3194static int determine_firmware(struct net_device *dev)
3195{
3196 struct orinoco_private *priv = netdev_priv(dev);
3197 hermes_t *hw = &priv->hw;
3198 int err;
3199 struct comp_id nic_id, sta_id;
3200 unsigned int firmver;
Hennerich, Michaeldde6d432007-02-05 16:41:35 -08003201 char tmp[SYMBOL_MAX_VER_LEN+1] __attribute__((aligned(2)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
3203 /* Get the hardware version */
3204 err = HERMES_READ_RECORD(hw, USER_BAP, HERMES_RID_NICID, &nic_id);
3205 if (err) {
3206 printk(KERN_ERR "%s: Cannot read hardware identity: error %d\n",
3207 dev->name, err);
3208 return err;
3209 }
3210
3211 le16_to_cpus(&nic_id.id);
3212 le16_to_cpus(&nic_id.variant);
3213 le16_to_cpus(&nic_id.major);
3214 le16_to_cpus(&nic_id.minor);
3215 printk(KERN_DEBUG "%s: Hardware identity %04x:%04x:%04x:%04x\n",
3216 dev->name, nic_id.id, nic_id.variant,
3217 nic_id.major, nic_id.minor);
3218
3219 priv->firmware_type = determine_firmware_type(&nic_id);
3220
3221 /* Get the firmware version */
3222 err = HERMES_READ_RECORD(hw, USER_BAP, HERMES_RID_STAID, &sta_id);
3223 if (err) {
3224 printk(KERN_ERR "%s: Cannot read station identity: error %d\n",
3225 dev->name, err);
3226 return err;
3227 }
3228
3229 le16_to_cpus(&sta_id.id);
3230 le16_to_cpus(&sta_id.variant);
3231 le16_to_cpus(&sta_id.major);
3232 le16_to_cpus(&sta_id.minor);
3233 printk(KERN_DEBUG "%s: Station identity %04x:%04x:%04x:%04x\n",
3234 dev->name, sta_id.id, sta_id.variant,
3235 sta_id.major, sta_id.minor);
3236
3237 switch (sta_id.id) {
3238 case 0x15:
3239 printk(KERN_ERR "%s: Primary firmware is active\n",
3240 dev->name);
3241 return -ENODEV;
3242 case 0x14b:
3243 printk(KERN_ERR "%s: Tertiary firmware is active\n",
3244 dev->name);
3245 return -ENODEV;
3246 case 0x1f: /* Intersil, Agere, Symbol Spectrum24 */
3247 case 0x21: /* Symbol Spectrum24 Trilogy */
3248 break;
3249 default:
3250 printk(KERN_NOTICE "%s: Unknown station ID, please report\n",
3251 dev->name);
3252 break;
3253 }
3254
3255 /* Default capabilities */
3256 priv->has_sensitivity = 1;
3257 priv->has_mwo = 0;
3258 priv->has_preamble = 0;
3259 priv->has_port3 = 1;
3260 priv->has_ibss = 1;
3261 priv->has_wep = 0;
3262 priv->has_big_wep = 0;
David Kilroy6eecad72008-08-21 23:27:56 +01003263 priv->has_alt_txcntl = 0;
David Kilroy01632fa2008-08-21 23:27:58 +01003264 priv->has_ext_scan = 0;
David Kilroyd03032a2008-08-21 23:28:02 +01003265 priv->has_wpa = 0;
David Kilroy3994d502008-08-21 23:27:54 +01003266 priv->do_fw_download = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
3268 /* Determine capabilities from the firmware version */
3269 switch (priv->firmware_type) {
3270 case FIRMWARE_TYPE_AGERE:
3271 /* Lucent Wavelan IEEE, Lucent Orinoco, Cabletron RoamAbout,
3272 ELSA, Melco, HP, IBM, Dell 1150, Compaq 110/210 */
3273 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
3274 "Lucent/Agere %d.%02d", sta_id.major, sta_id.minor);
3275
3276 firmver = ((unsigned long)sta_id.major << 16) | sta_id.minor;
3277
3278 priv->has_ibss = (firmver >= 0x60006);
3279 priv->has_wep = (firmver >= 0x40020);
3280 priv->has_big_wep = 1; /* FIXME: this is wrong - how do we tell
3281 Gold cards from the others? */
3282 priv->has_mwo = (firmver >= 0x60000);
3283 priv->has_pm = (firmver >= 0x40020); /* Don't work in 7.52 ? */
3284 priv->ibss_port = 1;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003285 priv->has_hostscan = (firmver >= 0x8000a);
David Kilroy3994d502008-08-21 23:27:54 +01003286 priv->do_fw_download = 1;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02003287 priv->broken_monitor = (firmver >= 0x80000);
David Kilroy6eecad72008-08-21 23:27:56 +01003288 priv->has_alt_txcntl = (firmver >= 0x90000); /* All 9.x ? */
David Kilroy01632fa2008-08-21 23:27:58 +01003289 priv->has_ext_scan = (firmver >= 0x90000); /* All 9.x ? */
David Kilroyd03032a2008-08-21 23:28:02 +01003290 priv->has_wpa = (firmver >= 0x9002a);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 /* Tested with Agere firmware :
3292 * 1.16 ; 4.08 ; 4.52 ; 6.04 ; 6.16 ; 7.28 => Jean II
3293 * Tested CableTron firmware : 4.32 => Anton */
3294 break;
3295 case FIRMWARE_TYPE_SYMBOL:
3296 /* Symbol , 3Com AirConnect, Intel, Ericsson WLAN */
3297 /* Intel MAC : 00:02:B3:* */
3298 /* 3Com MAC : 00:50:DA:* */
3299 memset(tmp, 0, sizeof(tmp));
3300 /* Get the Symbol firmware version */
3301 err = hermes_read_ltv(hw, USER_BAP,
3302 HERMES_RID_SECONDARYVERSION_SYMBOL,
3303 SYMBOL_MAX_VER_LEN, NULL, &tmp);
3304 if (err) {
3305 printk(KERN_WARNING
David Kilroyb2f30a02009-02-04 23:05:46 +00003306 "%s: Error %d reading Symbol firmware info. "
3307 "Wildly guessing capabilities...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 dev->name, err);
3309 firmver = 0;
3310 tmp[0] = '\0';
3311 } else {
3312 /* The firmware revision is a string, the format is
3313 * something like : "V2.20-01".
3314 * Quick and dirty parsing... - Jean II
3315 */
David Kilroyb2f30a02009-02-04 23:05:46 +00003316 firmver = ((tmp[1] - '0') << 16)
3317 | ((tmp[3] - '0') << 12)
3318 | ((tmp[4] - '0') << 8)
3319 | ((tmp[6] - '0') << 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 | (tmp[7] - '0');
3321
3322 tmp[SYMBOL_MAX_VER_LEN] = '\0';
3323 }
3324
3325 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
3326 "Symbol %s", tmp);
3327
3328 priv->has_ibss = (firmver >= 0x20000);
3329 priv->has_wep = (firmver >= 0x15012);
3330 priv->has_big_wep = (firmver >= 0x20000);
David Kilroy6fe9deb2009-02-04 23:05:43 +00003331 priv->has_pm = (firmver >= 0x20000 && firmver < 0x22000) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 (firmver >= 0x29000 && firmver < 0x30000) ||
3333 firmver >= 0x31000;
3334 priv->has_preamble = (firmver >= 0x20000);
3335 priv->ibss_port = 4;
David Kilroy3994d502008-08-21 23:27:54 +01003336
3337 /* Symbol firmware is found on various cards, but
3338 * there has been no attempt to check firmware
3339 * download on non-spectrum_cs based cards.
3340 *
3341 * Given that the Agere firmware download works
3342 * differently, we should avoid doing a firmware
3343 * download with the Symbol algorithm on non-spectrum
3344 * cards.
3345 *
3346 * For now we can identify a spectrum_cs based card
3347 * because it has a firmware reset function.
3348 */
3349 priv->do_fw_download = (priv->stop_fw != NULL);
3350
David Kilroy6fe9deb2009-02-04 23:05:43 +00003351 priv->broken_disableport = (firmver == 0x25013) ||
David Kilroyb2f30a02009-02-04 23:05:46 +00003352 (firmver >= 0x30000 && firmver <= 0x31000);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003353 priv->has_hostscan = (firmver >= 0x31001) ||
3354 (firmver >= 0x29057 && firmver < 0x30000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 /* Tested with Intel firmware : 0x20015 => Jean II */
3356 /* Tested with 3Com firmware : 0x15012 & 0x22001 => Jean II */
3357 break;
3358 case FIRMWARE_TYPE_INTERSIL:
3359 /* D-Link, Linksys, Adtron, ZoomAir, and many others...
3360 * Samsung, Compaq 100/200 and Proxim are slightly
3361 * different and less well tested */
3362 /* D-Link MAC : 00:40:05:* */
3363 /* Addtron MAC : 00:90:D1:* */
3364 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
3365 "Intersil %d.%d.%d", sta_id.major, sta_id.minor,
3366 sta_id.variant);
3367
3368 firmver = ((unsigned long)sta_id.major << 16) |
3369 ((unsigned long)sta_id.minor << 8) | sta_id.variant;
3370
3371 priv->has_ibss = (firmver >= 0x000700); /* FIXME */
3372 priv->has_big_wep = priv->has_wep = (firmver >= 0x000800);
3373 priv->has_pm = (firmver >= 0x000700);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003374 priv->has_hostscan = (firmver >= 0x010301);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375
3376 if (firmver >= 0x000800)
3377 priv->ibss_port = 0;
3378 else {
3379 printk(KERN_NOTICE "%s: Intersil firmware earlier "
3380 "than v0.8.x - several features not supported\n",
3381 dev->name);
3382 priv->ibss_port = 1;
3383 }
3384 break;
3385 }
3386 printk(KERN_DEBUG "%s: Firmware determined as %s\n", dev->name,
3387 priv->fw_name);
3388
3389 return 0;
3390}
3391
3392static int orinoco_init(struct net_device *dev)
3393{
3394 struct orinoco_private *priv = netdev_priv(dev);
3395 hermes_t *hw = &priv->hw;
3396 int err = 0;
3397 struct hermes_idstring nickbuf;
3398 u16 reclen;
3399 int len;
3400
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 /* No need to lock, the hw_unavailable flag is already set in
3402 * alloc_orinocodev() */
Johannes Berg2c7060022008-10-30 22:09:54 +01003403 priv->nicbuf_size = IEEE80211_MAX_FRAME_LEN + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
3405 /* Initialize the firmware */
Pavel Roskin37a6c612006-04-07 04:10:49 -04003406 err = hermes_init(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 if (err != 0) {
3408 printk(KERN_ERR "%s: failed to initialize firmware (err = %d)\n",
3409 dev->name, err);
3410 goto out;
3411 }
3412
3413 err = determine_firmware(dev);
3414 if (err != 0) {
3415 printk(KERN_ERR "%s: Incompatible firmware, aborting\n",
3416 dev->name);
3417 goto out;
3418 }
3419
David Kilroy3994d502008-08-21 23:27:54 +01003420 if (priv->do_fw_download) {
David Kilroy39d1ffe2008-11-22 10:37:28 +00003421#ifdef CONFIG_HERMES_CACHE_FW_ON_INIT
David Kilroy74734312008-11-22 10:37:25 +00003422 orinoco_cache_fw(priv, 0);
David Kilroy39d1ffe2008-11-22 10:37:28 +00003423#endif
David Kilroy74734312008-11-22 10:37:25 +00003424
David Kilroy3994d502008-08-21 23:27:54 +01003425 err = orinoco_download(priv);
3426 if (err)
3427 priv->do_fw_download = 0;
3428
3429 /* Check firmware version again */
3430 err = determine_firmware(dev);
3431 if (err != 0) {
3432 printk(KERN_ERR "%s: Incompatible firmware, aborting\n",
3433 dev->name);
3434 goto out;
3435 }
3436 }
3437
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 if (priv->has_port3)
David Kilroyb2f30a02009-02-04 23:05:46 +00003439 printk(KERN_DEBUG "%s: Ad-hoc demo mode supported\n",
3440 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 if (priv->has_ibss)
3442 printk(KERN_DEBUG "%s: IEEE standard IBSS ad-hoc mode supported\n",
3443 dev->name);
3444 if (priv->has_wep) {
3445 printk(KERN_DEBUG "%s: WEP supported, ", dev->name);
3446 if (priv->has_big_wep)
3447 printk("104-bit key\n");
3448 else
3449 printk("40-bit key\n");
3450 }
David Kilroy23edcc42008-08-21 23:28:05 +01003451 if (priv->has_wpa) {
David Kilroyd03032a2008-08-21 23:28:02 +01003452 printk(KERN_DEBUG "%s: WPA-PSK supported\n", dev->name);
David Kilroy23edcc42008-08-21 23:28:05 +01003453 if (orinoco_mic_init(priv)) {
3454 printk(KERN_ERR "%s: Failed to setup MIC crypto "
3455 "algorithm. Disabling WPA support\n", dev->name);
3456 priv->has_wpa = 0;
3457 }
3458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
David Kilroy01632fa2008-08-21 23:27:58 +01003460 /* Now we have the firmware capabilities, allocate appropiate
3461 * sized scan buffers */
3462 if (orinoco_bss_data_allocate(priv))
3463 goto out;
3464 orinoco_bss_data_init(priv);
3465
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 /* Get the MAC address */
3467 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
3468 ETH_ALEN, NULL, dev->dev_addr);
3469 if (err) {
3470 printk(KERN_WARNING "%s: failed to read MAC address!\n",
3471 dev->name);
3472 goto out;
3473 }
3474
Johannes Berge1749612008-10-27 15:59:26 -07003475 printk(KERN_DEBUG "%s: MAC address %pM\n",
3476 dev->name, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477
3478 /* Get the station name */
3479 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
3480 sizeof(nickbuf), &reclen, &nickbuf);
3481 if (err) {
3482 printk(KERN_ERR "%s: failed to read station name\n",
3483 dev->name);
3484 goto out;
3485 }
3486 if (nickbuf.len)
3487 len = min(IW_ESSID_MAX_SIZE, (int)le16_to_cpu(nickbuf.len));
3488 else
3489 len = min(IW_ESSID_MAX_SIZE, 2 * reclen);
3490 memcpy(priv->nick, &nickbuf.val, len);
3491 priv->nick[len] = '\0';
3492
3493 printk(KERN_DEBUG "%s: Station name \"%s\"\n", dev->name, priv->nick);
3494
Pavel Roskin37a6c612006-04-07 04:10:49 -04003495 err = orinoco_allocate_fid(dev);
3496 if (err) {
3497 printk(KERN_ERR "%s: failed to allocate NIC buffer!\n",
3498 dev->name);
3499 goto out;
3500 }
3501
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 /* Get allowed channels */
3503 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CHANNELLIST,
3504 &priv->channel_mask);
3505 if (err) {
3506 printk(KERN_ERR "%s: failed to read channel list!\n",
3507 dev->name);
3508 goto out;
3509 }
3510
3511 /* Get initial AP density */
3512 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFSYSTEMSCALE,
3513 &priv->ap_density);
David Kilroy566f2d92009-02-04 23:05:45 +00003514 if (err || priv->ap_density < 1 || priv->ap_density > 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 priv->has_sensitivity = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
3517 /* Get initial RTS threshold */
3518 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
3519 &priv->rts_thresh);
3520 if (err) {
3521 printk(KERN_ERR "%s: failed to read RTS threshold!\n",
3522 dev->name);
3523 goto out;
3524 }
3525
3526 /* Get initial fragmentation settings */
3527 if (priv->has_mwo)
3528 err = hermes_read_wordrec(hw, USER_BAP,
3529 HERMES_RID_CNFMWOROBUST_AGERE,
3530 &priv->mwo_robust);
3531 else
David Kilroyb2f30a02009-02-04 23:05:46 +00003532 err = hermes_read_wordrec(hw, USER_BAP,
3533 HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 &priv->frag_thresh);
3535 if (err) {
3536 printk(KERN_ERR "%s: failed to read fragmentation settings!\n",
3537 dev->name);
3538 goto out;
3539 }
3540
3541 /* Power management setup */
3542 if (priv->has_pm) {
3543 priv->pm_on = 0;
3544 priv->pm_mcast = 1;
3545 err = hermes_read_wordrec(hw, USER_BAP,
3546 HERMES_RID_CNFMAXSLEEPDURATION,
3547 &priv->pm_period);
3548 if (err) {
3549 printk(KERN_ERR "%s: failed to read power management period!\n",
3550 dev->name);
3551 goto out;
3552 }
3553 err = hermes_read_wordrec(hw, USER_BAP,
3554 HERMES_RID_CNFPMHOLDOVERDURATION,
3555 &priv->pm_timeout);
3556 if (err) {
3557 printk(KERN_ERR "%s: failed to read power management timeout!\n",
3558 dev->name);
3559 goto out;
3560 }
3561 }
3562
3563 /* Preamble setup */
3564 if (priv->has_preamble) {
3565 err = hermes_read_wordrec(hw, USER_BAP,
3566 HERMES_RID_CNFPREAMBLE_SYMBOL,
3567 &priv->preamble);
3568 if (err)
3569 goto out;
3570 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00003571
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 /* Set up the default configuration */
3573 priv->iw_mode = IW_MODE_INFRA;
3574 /* By default use IEEE/IBSS ad-hoc mode if we have it */
David Kilroya94e8422009-02-04 23:05:44 +00003575 priv->prefer_port3 = priv->has_port3 && (!priv->has_ibss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 set_port_type(priv);
David Gibsond51d8b12005-05-12 20:03:36 -04003577 priv->channel = 0; /* use firmware default */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
3579 priv->promiscuous = 0;
David Kilroy4ae6ee22008-08-21 23:27:59 +01003580 priv->encode_alg = IW_ENCODE_ALG_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 priv->tx_key = 0;
David Kilroyd03032a2008-08-21 23:28:02 +01003582 priv->wpa_enabled = 0;
3583 priv->tkip_cm_active = 0;
3584 priv->key_mgmt = 0;
3585 priv->wpa_ie_len = 0;
3586 priv->wpa_ie = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 /* Make the hardware available, as long as it hasn't been
3589 * removed elsewhere (e.g. by PCMCIA hot unplug) */
3590 spin_lock_irq(&priv->lock);
3591 priv->hw_unavailable--;
3592 spin_unlock_irq(&priv->lock);
3593
3594 printk(KERN_DEBUG "%s: ready\n", dev->name);
3595
3596 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 return err;
3598}
3599
Andrey Borzenkov89ea4092009-01-21 20:46:46 +03003600static const struct net_device_ops orinoco_netdev_ops = {
3601 .ndo_init = orinoco_init,
3602 .ndo_open = orinoco_open,
3603 .ndo_stop = orinoco_stop,
3604 .ndo_start_xmit = orinoco_xmit,
3605 .ndo_set_multicast_list = orinoco_set_multicast_list,
3606 .ndo_change_mtu = orinoco_change_mtu,
3607 .ndo_tx_timeout = orinoco_tx_timeout,
3608 .ndo_get_stats = orinoco_get_stats,
3609};
3610
David Kilroy3994d502008-08-21 23:27:54 +01003611struct net_device
3612*alloc_orinocodev(int sizeof_card,
3613 struct device *device,
3614 int (*hard_reset)(struct orinoco_private *),
3615 int (*stop_fw)(struct orinoco_private *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616{
3617 struct net_device *dev;
3618 struct orinoco_private *priv;
3619
3620 dev = alloc_etherdev(sizeof(struct orinoco_private) + sizeof_card);
Andrey Borzenkove129a942009-01-21 21:55:29 +03003621 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 return NULL;
3623 priv = netdev_priv(dev);
3624 priv->ndev = dev;
3625 if (sizeof_card)
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02003626 priv->card = (void *)((unsigned long)priv
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 + sizeof(struct orinoco_private));
3628 else
3629 priv->card = NULL;
David Kilroy3994d502008-08-21 23:27:54 +01003630 priv->dev = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
3632 /* Setup / override net_device fields */
Andrey Borzenkov89ea4092009-01-21 20:46:46 +03003633 dev->netdev_ops = &orinoco_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 dev->watchdog_timeo = HZ; /* 1 second timeout */
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02003635 dev->ethtool_ops = &orinoco_ethtool_ops;
Andrey Borzenkove129a942009-01-21 21:55:29 +03003636 dev->wireless_handlers = &orinoco_handler_def;
Pavel Roskin343c6862005-09-09 18:43:02 -04003637#ifdef WIRELESS_SPY
3638 priv->wireless_data.spy_data = &priv->spy_data;
3639 dev->wireless_data = &priv->wireless_data;
3640#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 /* we use the default eth_mac_addr for setting the MAC addr */
3642
David Kilroy23edcc42008-08-21 23:28:05 +01003643 /* Reserve space in skb for the SNAP header */
3644 dev->hard_header_len += ENCAPS_OVERHEAD;
3645
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 /* Set up default callbacks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 priv->hard_reset = hard_reset;
David Kilroy3994d502008-08-21 23:27:54 +01003648 priv->stop_fw = stop_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649
3650 spin_lock_init(&priv->lock);
3651 priv->open = 0;
3652 priv->hw_unavailable = 1; /* orinoco_init() must clear this
3653 * before anything else touches the
3654 * hardware */
David Howellsc4028952006-11-22 14:57:56 +00003655 INIT_WORK(&priv->reset_work, orinoco_reset);
3656 INIT_WORK(&priv->join_work, orinoco_join_ap);
3657 INIT_WORK(&priv->wevent_work, orinoco_send_wevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658
David Kilroy31afcef2008-08-21 23:28:04 +01003659 INIT_LIST_HEAD(&priv->rx_list);
3660 tasklet_init(&priv->rx_tasklet, orinoco_rx_isr_tasklet,
3661 (unsigned long) dev);
3662
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 netif_carrier_off(dev);
3664 priv->last_linkstatus = 0xffff;
3665
David Kilroy2cea7b22008-11-22 10:37:26 +00003666 priv->cached_pri_fw = NULL;
Andrey Borzenkov4fb30782008-10-19 12:06:11 +04003667 priv->cached_fw = NULL;
3668
David Kilroy39d1ffe2008-11-22 10:37:28 +00003669 /* Register PM notifiers */
3670 priv->pm_notifier.notifier_call = orinoco_pm_notifier;
3671 register_pm_notifier(&priv->pm_notifier);
3672
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 return dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674}
3675
3676void free_orinocodev(struct net_device *dev)
3677{
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003678 struct orinoco_private *priv = netdev_priv(dev);
David Kilroy20953ad2009-01-07 00:23:55 +00003679 struct orinoco_rx_data *rx_data, *temp;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003680
David Kilroy20953ad2009-01-07 00:23:55 +00003681 /* If the tasklet is scheduled when we call tasklet_kill it
3682 * will run one final time. However the tasklet will only
3683 * drain priv->rx_list if the hw is still available. */
David Kilroy31afcef2008-08-21 23:28:04 +01003684 tasklet_kill(&priv->rx_tasklet);
David Kilroy74734312008-11-22 10:37:25 +00003685
David Kilroy20953ad2009-01-07 00:23:55 +00003686 /* Explicitly drain priv->rx_list */
3687 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
3688 list_del(&rx_data->list);
3689
3690 dev_kfree_skb(rx_data->skb);
3691 kfree(rx_data->desc);
3692 kfree(rx_data);
3693 }
3694
David Kilroy39d1ffe2008-11-22 10:37:28 +00003695 unregister_pm_notifier(&priv->pm_notifier);
David Kilroy74734312008-11-22 10:37:25 +00003696 orinoco_uncache_fw(priv);
3697
David Kilroyd03032a2008-08-21 23:28:02 +01003698 priv->wpa_ie_len = 0;
3699 kfree(priv->wpa_ie);
David Kilroy23edcc42008-08-21 23:28:05 +01003700 orinoco_mic_free(priv);
Dan Williams1e3428e2007-10-10 23:56:25 -04003701 orinoco_bss_data_free(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 free_netdev(dev);
3703}
3704
3705/********************************************************************/
3706/* Wireless extensions */
3707/********************************************************************/
3708
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07003709/* Return : < 0 -> error code ; >= 0 -> length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710static int orinoco_hw_get_essid(struct orinoco_private *priv, int *active,
3711 char buf[IW_ESSID_MAX_SIZE+1])
3712{
3713 hermes_t *hw = &priv->hw;
3714 int err = 0;
3715 struct hermes_idstring essidbuf;
3716 char *p = (char *)(&essidbuf.val);
3717 int len;
3718 unsigned long flags;
3719
3720 if (orinoco_lock(priv, &flags) != 0)
3721 return -EBUSY;
3722
3723 if (strlen(priv->desired_essid) > 0) {
3724 /* We read the desired SSID from the hardware rather
3725 than from priv->desired_essid, just in case the
3726 firmware is allowed to change it on us. I'm not
3727 sure about this */
3728 /* My guess is that the OWNSSID should always be whatever
3729 * we set to the card, whereas CURRENT_SSID is the one that
3730 * may change... - Jean II */
3731 u16 rid;
3732
3733 *active = 1;
3734
3735 rid = (priv->port_type == 3) ? HERMES_RID_CNFOWNSSID :
3736 HERMES_RID_CNFDESIREDSSID;
David Kilroy6fe9deb2009-02-04 23:05:43 +00003737
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 err = hermes_read_ltv(hw, USER_BAP, rid, sizeof(essidbuf),
3739 NULL, &essidbuf);
3740 if (err)
3741 goto fail_unlock;
3742 } else {
3743 *active = 0;
3744
3745 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTSSID,
3746 sizeof(essidbuf), NULL, &essidbuf);
3747 if (err)
3748 goto fail_unlock;
3749 }
3750
3751 len = le16_to_cpu(essidbuf.len);
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02003752 BUG_ON(len > IW_ESSID_MAX_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07003754 memset(buf, 0, IW_ESSID_MAX_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 memcpy(buf, p, len);
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07003756 err = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757
3758 fail_unlock:
3759 orinoco_unlock(priv, &flags);
3760
David Kilroy6fe9deb2009-02-04 23:05:43 +00003761 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762}
3763
David Kilroy9ee677c2008-12-23 14:03:38 +00003764static int orinoco_hw_get_freq(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765{
David Kilroy6fe9deb2009-02-04 23:05:43 +00003766
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 hermes_t *hw = &priv->hw;
3768 int err = 0;
3769 u16 channel;
David Kilroy9ee677c2008-12-23 14:03:38 +00003770 int freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 unsigned long flags;
3772
3773 if (orinoco_lock(priv, &flags) != 0)
3774 return -EBUSY;
David Kilroy6fe9deb2009-02-04 23:05:43 +00003775
David Kilroyb2f30a02009-02-04 23:05:46 +00003776 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CURRENTCHANNEL,
3777 &channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 if (err)
3779 goto out;
3780
3781 /* Intersil firmware 1.3.5 returns 0 when the interface is down */
3782 if (channel == 0) {
3783 err = -EBUSY;
3784 goto out;
3785 }
3786
David Kilroya94e8422009-02-04 23:05:44 +00003787 if ((channel < 1) || (channel > NUM_CHANNELS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 printk(KERN_WARNING "%s: Channel out of range (%d)!\n",
3789 priv->ndev->name, channel);
3790 err = -EBUSY;
3791 goto out;
3792
3793 }
David Kilroy9ee677c2008-12-23 14:03:38 +00003794 freq = ieee80211_dsss_chan_to_freq(channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795
3796 out:
3797 orinoco_unlock(priv, &flags);
3798
3799 if (err > 0)
3800 err = -EBUSY;
3801 return err ? err : freq;
3802}
3803
3804static int orinoco_hw_get_bitratelist(struct orinoco_private *priv,
3805 int *numrates, s32 *rates, int max)
3806{
3807 hermes_t *hw = &priv->hw;
3808 struct hermes_idstring list;
3809 unsigned char *p = (unsigned char *)&list.val;
3810 int err = 0;
3811 int num;
3812 int i;
3813 unsigned long flags;
3814
3815 if (orinoco_lock(priv, &flags) != 0)
3816 return -EBUSY;
3817
3818 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_SUPPORTEDDATARATES,
3819 sizeof(list), NULL, &list);
3820 orinoco_unlock(priv, &flags);
3821
3822 if (err)
3823 return err;
David Kilroy6fe9deb2009-02-04 23:05:43 +00003824
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 num = le16_to_cpu(list.len);
3826 *numrates = num;
3827 num = min(num, max);
3828
David Kilroy566f2d92009-02-04 23:05:45 +00003829 for (i = 0; i < num; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 rates[i] = (p[i] & 0x7f) * 500000; /* convert to bps */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831
3832 return 0;
3833}
3834
Christoph Hellwig620554e2005-06-19 01:27:33 +02003835static int orinoco_ioctl_getname(struct net_device *dev,
3836 struct iw_request_info *info,
3837 char *name,
3838 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839{
3840 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 int numrates;
Christoph Hellwig620554e2005-06-19 01:27:33 +02003842 int err;
3843
3844 err = orinoco_hw_get_bitratelist(priv, &numrates, NULL, 0);
3845
3846 if (!err && (numrates > 2))
3847 strcpy(name, "IEEE 802.11b");
3848 else
3849 strcpy(name, "IEEE 802.11-DS");
3850
3851 return 0;
3852}
3853
Christoph Hellwig16739b02005-06-19 01:27:51 +02003854static int orinoco_ioctl_setwap(struct net_device *dev,
3855 struct iw_request_info *info,
3856 struct sockaddr *ap_addr,
3857 char *extra)
3858{
3859 struct orinoco_private *priv = netdev_priv(dev);
3860 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 unsigned long flags;
Christoph Hellwig16739b02005-06-19 01:27:51 +02003862 static const u8 off_addr[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3863 static const u8 any_addr[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864
3865 if (orinoco_lock(priv, &flags) != 0)
3866 return -EBUSY;
3867
Christoph Hellwig16739b02005-06-19 01:27:51 +02003868 /* Enable automatic roaming - no sanity checks are needed */
3869 if (memcmp(&ap_addr->sa_data, off_addr, ETH_ALEN) == 0 ||
3870 memcmp(&ap_addr->sa_data, any_addr, ETH_ALEN) == 0) {
3871 priv->bssid_fixed = 0;
3872 memset(priv->desired_bssid, 0, ETH_ALEN);
3873
3874 /* "off" means keep existing connection */
3875 if (ap_addr->sa_data[0] == 0) {
3876 __orinoco_hw_set_wap(priv);
3877 err = 0;
3878 }
3879 goto out;
3880 }
3881
3882 if (priv->firmware_type == FIRMWARE_TYPE_AGERE) {
3883 printk(KERN_WARNING "%s: Lucent/Agere firmware doesn't "
3884 "support manual roaming\n",
3885 dev->name);
3886 err = -EOPNOTSUPP;
3887 goto out;
3888 }
3889
3890 if (priv->iw_mode != IW_MODE_INFRA) {
3891 printk(KERN_WARNING "%s: Manual roaming supported only in "
3892 "managed mode\n", dev->name);
3893 err = -EOPNOTSUPP;
3894 goto out;
3895 }
3896
3897 /* Intersil firmware hangs without Desired ESSID */
3898 if (priv->firmware_type == FIRMWARE_TYPE_INTERSIL &&
3899 strlen(priv->desired_essid) == 0) {
3900 printk(KERN_WARNING "%s: Desired ESSID must be set for "
3901 "manual roaming\n", dev->name);
3902 err = -EOPNOTSUPP;
3903 goto out;
3904 }
3905
3906 /* Finally, enable manual roaming */
3907 priv->bssid_fixed = 1;
3908 memcpy(priv->desired_bssid, &ap_addr->sa_data, ETH_ALEN);
3909
3910 out:
3911 orinoco_unlock(priv, &flags);
3912 return err;
3913}
3914
Christoph Hellwig620554e2005-06-19 01:27:33 +02003915static int orinoco_ioctl_getwap(struct net_device *dev,
3916 struct iw_request_info *info,
3917 struct sockaddr *ap_addr,
3918 char *extra)
3919{
3920 struct orinoco_private *priv = netdev_priv(dev);
3921
3922 hermes_t *hw = &priv->hw;
3923 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 unsigned long flags;
3925
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 if (orinoco_lock(priv, &flags) != 0)
3927 return -EBUSY;
3928
Christoph Hellwig620554e2005-06-19 01:27:33 +02003929 ap_addr->sa_family = ARPHRD_ETHER;
3930 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
3931 ETH_ALEN, NULL, ap_addr->sa_data);
3932
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 orinoco_unlock(priv, &flags);
3934
Christoph Hellwig620554e2005-06-19 01:27:33 +02003935 return err;
3936}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937
Christoph Hellwig620554e2005-06-19 01:27:33 +02003938static int orinoco_ioctl_setmode(struct net_device *dev,
3939 struct iw_request_info *info,
3940 u32 *mode,
3941 char *extra)
3942{
3943 struct orinoco_private *priv = netdev_priv(dev);
3944 int err = -EINPROGRESS; /* Call commit handler */
3945 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946
Christoph Hellwig620554e2005-06-19 01:27:33 +02003947 if (priv->iw_mode == *mode)
3948 return 0;
3949
3950 if (orinoco_lock(priv, &flags) != 0)
3951 return -EBUSY;
3952
3953 switch (*mode) {
3954 case IW_MODE_ADHOC:
3955 if (!priv->has_ibss && !priv->has_port3)
3956 err = -EOPNOTSUPP;
3957 break;
3958
3959 case IW_MODE_INFRA:
3960 break;
3961
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02003962 case IW_MODE_MONITOR:
3963 if (priv->broken_monitor && !force_monitor) {
3964 printk(KERN_WARNING "%s: Monitor mode support is "
3965 "buggy in this firmware, not enabling\n",
3966 dev->name);
3967 err = -EOPNOTSUPP;
3968 }
3969 break;
3970
Christoph Hellwig620554e2005-06-19 01:27:33 +02003971 default:
3972 err = -EOPNOTSUPP;
3973 break;
3974 }
3975
3976 if (err == -EINPROGRESS) {
3977 priv->iw_mode = *mode;
3978 set_port_type(priv);
3979 }
3980
3981 orinoco_unlock(priv, &flags);
3982
3983 return err;
3984}
3985
3986static int orinoco_ioctl_getmode(struct net_device *dev,
3987 struct iw_request_info *info,
3988 u32 *mode,
3989 char *extra)
3990{
3991 struct orinoco_private *priv = netdev_priv(dev);
3992
3993 *mode = priv->iw_mode;
3994 return 0;
3995}
3996
3997static int orinoco_ioctl_getiwrange(struct net_device *dev,
3998 struct iw_request_info *info,
3999 struct iw_point *rrq,
4000 char *extra)
4001{
4002 struct orinoco_private *priv = netdev_priv(dev);
4003 int err = 0;
4004 struct iw_range *range = (struct iw_range *) extra;
4005 int numrates;
4006 int i, k;
4007
Christoph Hellwig620554e2005-06-19 01:27:33 +02004008 rrq->length = sizeof(struct iw_range);
4009 memset(range, 0, sizeof(struct iw_range));
4010
4011 range->we_version_compiled = WIRELESS_EXT;
David Kilroyd03032a2008-08-21 23:28:02 +01004012 range->we_version_source = 22;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013
4014 /* Set available channels/frequencies */
Christoph Hellwig620554e2005-06-19 01:27:33 +02004015 range->num_channels = NUM_CHANNELS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 k = 0;
4017 for (i = 0; i < NUM_CHANNELS; i++) {
4018 if (priv->channel_mask & (1 << i)) {
Christoph Hellwig620554e2005-06-19 01:27:33 +02004019 range->freq[k].i = i + 1;
David Kilroy9ee677c2008-12-23 14:03:38 +00004020 range->freq[k].m = (ieee80211_dsss_chan_to_freq(i + 1) *
4021 100000);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004022 range->freq[k].e = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 k++;
4024 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00004025
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 if (k >= IW_MAX_FREQUENCIES)
4027 break;
4028 }
Christoph Hellwig620554e2005-06-19 01:27:33 +02004029 range->num_frequency = k;
4030 range->sensitivity = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031
Christoph Hellwig620554e2005-06-19 01:27:33 +02004032 if (priv->has_wep) {
4033 range->max_encoding_tokens = ORINOCO_MAX_KEYS;
4034 range->encoding_size[0] = SMALL_KEY_SIZE;
4035 range->num_encoding_sizes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036
Christoph Hellwig620554e2005-06-19 01:27:33 +02004037 if (priv->has_big_wep) {
4038 range->encoding_size[1] = LARGE_KEY_SIZE;
4039 range->num_encoding_sizes = 2;
4040 }
4041 }
4042
David Kilroyd03032a2008-08-21 23:28:02 +01004043 if (priv->has_wpa)
4044 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_CIPHER_TKIP;
4045
David Kilroya94e8422009-02-04 23:05:44 +00004046 if ((priv->iw_mode == IW_MODE_ADHOC) && (!SPY_NUMBER(priv))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 /* Quality stats meaningless in ad-hoc mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 } else {
Christoph Hellwig620554e2005-06-19 01:27:33 +02004049 range->max_qual.qual = 0x8b - 0x2f;
4050 range->max_qual.level = 0x2f - 0x95 - 1;
4051 range->max_qual.noise = 0x2f - 0x95 - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 /* Need to get better values */
Christoph Hellwig620554e2005-06-19 01:27:33 +02004053 range->avg_qual.qual = 0x24;
4054 range->avg_qual.level = 0xC2;
4055 range->avg_qual.noise = 0x9E;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 }
4057
4058 err = orinoco_hw_get_bitratelist(priv, &numrates,
Christoph Hellwig620554e2005-06-19 01:27:33 +02004059 range->bitrate, IW_MAX_BITRATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 if (err)
4061 return err;
Christoph Hellwig620554e2005-06-19 01:27:33 +02004062 range->num_bitrates = numrates;
4063
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 /* Set an indication of the max TCP throughput in bit/s that we can
4065 * expect using this interface. May be use for QoS stuff...
4066 * Jean II */
Christoph Hellwig620554e2005-06-19 01:27:33 +02004067 if (numrates > 2)
4068 range->throughput = 5 * 1000 * 1000; /* ~5 Mb/s */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 else
Christoph Hellwig620554e2005-06-19 01:27:33 +02004070 range->throughput = 1.5 * 1000 * 1000; /* ~1.5 Mb/s */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071
Christoph Hellwig620554e2005-06-19 01:27:33 +02004072 range->min_rts = 0;
4073 range->max_rts = 2347;
4074 range->min_frag = 256;
4075 range->max_frag = 2346;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076
Christoph Hellwig620554e2005-06-19 01:27:33 +02004077 range->min_pmp = 0;
4078 range->max_pmp = 65535000;
4079 range->min_pmt = 0;
4080 range->max_pmt = 65535 * 1000; /* ??? */
4081 range->pmp_flags = IW_POWER_PERIOD;
4082 range->pmt_flags = IW_POWER_TIMEOUT;
David Kilroyb2f30a02009-02-04 23:05:46 +00004083 range->pm_capa = (IW_POWER_PERIOD | IW_POWER_TIMEOUT |
4084 IW_POWER_UNICAST_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085
Christoph Hellwig620554e2005-06-19 01:27:33 +02004086 range->retry_capa = IW_RETRY_LIMIT | IW_RETRY_LIFETIME;
4087 range->retry_flags = IW_RETRY_LIMIT;
4088 range->r_time_flags = IW_RETRY_LIFETIME;
4089 range->min_retry = 0;
4090 range->max_retry = 65535; /* ??? */
4091 range->min_r_time = 0;
4092 range->max_r_time = 65535 * 1000; /* ??? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
David Kilroy0753bba2008-08-21 23:27:46 +01004094 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
4095 range->scan_capa = IW_SCAN_CAPA_ESSID;
4096 else
4097 range->scan_capa = IW_SCAN_CAPA_NONE;
4098
Pavel Roskin343c6862005-09-09 18:43:02 -04004099 /* Event capability (kernel) */
4100 IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
4101 /* Event capability (driver) */
4102 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWTHRSPY);
4103 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
4104 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
4105 IW_EVENT_CAPA_SET(range->event_capa, IWEVTXDROP);
4106
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 return 0;
4108}
4109
Christoph Hellwig620554e2005-06-19 01:27:33 +02004110static int orinoco_ioctl_setiwencode(struct net_device *dev,
4111 struct iw_request_info *info,
4112 struct iw_point *erq,
4113 char *keybuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114{
4115 struct orinoco_private *priv = netdev_priv(dev);
4116 int index = (erq->flags & IW_ENCODE_INDEX) - 1;
4117 int setindex = priv->tx_key;
David Kilroy4ae6ee22008-08-21 23:27:59 +01004118 int encode_alg = priv->encode_alg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 int restricted = priv->wep_restrict;
4120 u16 xlen = 0;
Christoph Hellwig620554e2005-06-19 01:27:33 +02004121 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 unsigned long flags;
4123
David Kilroya94e8422009-02-04 23:05:44 +00004124 if (!priv->has_wep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 return -EOPNOTSUPP;
4126
4127 if (erq->pointer) {
4128 /* We actually have a key to set - check its length */
4129 if (erq->length > LARGE_KEY_SIZE)
4130 return -E2BIG;
4131
David Kilroya94e8422009-02-04 23:05:44 +00004132 if ((erq->length > SMALL_KEY_SIZE) && !priv->has_big_wep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 return -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 }
4135
4136 if (orinoco_lock(priv, &flags) != 0)
4137 return -EBUSY;
4138
David Kilroyd03032a2008-08-21 23:28:02 +01004139 /* Clear any TKIP key we have */
4140 if ((priv->has_wpa) && (priv->encode_alg == IW_ENCODE_ALG_TKIP))
4141 (void) orinoco_clear_tkip_key(priv, setindex);
4142
Dan Williamsfe397d42006-07-14 11:41:47 -04004143 if (erq->length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
4145 index = priv->tx_key;
4146
4147 /* Adjust key length to a supported value */
David Kilroy566f2d92009-02-04 23:05:45 +00004148 if (erq->length > SMALL_KEY_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 xlen = LARGE_KEY_SIZE;
David Kilroy566f2d92009-02-04 23:05:45 +00004150 else if (erq->length > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 xlen = SMALL_KEY_SIZE;
David Kilroy566f2d92009-02-04 23:05:45 +00004152 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 xlen = 0;
4154
4155 /* Switch on WEP if off */
David Kilroy4ae6ee22008-08-21 23:27:59 +01004156 if ((encode_alg != IW_ENCODE_ALG_WEP) && (xlen > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 setindex = index;
David Kilroy4ae6ee22008-08-21 23:27:59 +01004158 encode_alg = IW_ENCODE_ALG_WEP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 }
4160 } else {
4161 /* Important note : if the user do "iwconfig eth0 enc off",
4162 * we will arrive there with an index of -1. This is valid
4163 * but need to be taken care off... Jean II */
4164 if ((index < 0) || (index >= ORINOCO_MAX_KEYS)) {
David Kilroya94e8422009-02-04 23:05:44 +00004165 if ((index != -1) || (erq->flags == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 err = -EINVAL;
4167 goto out;
4168 }
4169 } else {
4170 /* Set the index : Check that the key is valid */
David Kilroya94e8422009-02-04 23:05:44 +00004171 if (priv->keys[index].len == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 err = -EINVAL;
4173 goto out;
4174 }
4175 setindex = index;
4176 }
4177 }
4178
4179 if (erq->flags & IW_ENCODE_DISABLED)
David Kilroy4ae6ee22008-08-21 23:27:59 +01004180 encode_alg = IW_ENCODE_ALG_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 if (erq->flags & IW_ENCODE_OPEN)
4182 restricted = 0;
4183 if (erq->flags & IW_ENCODE_RESTRICTED)
4184 restricted = 1;
4185
Dan Williamsfe397d42006-07-14 11:41:47 -04004186 if (erq->pointer && erq->length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 priv->keys[index].len = cpu_to_le16(xlen);
4188 memset(priv->keys[index].data, 0,
4189 sizeof(priv->keys[index].data));
4190 memcpy(priv->keys[index].data, keybuf, erq->length);
4191 }
4192 priv->tx_key = setindex;
4193
4194 /* Try fast key change if connected and only keys are changed */
David Kilroy4ae6ee22008-08-21 23:27:59 +01004195 if ((priv->encode_alg == encode_alg) &&
4196 (priv->wep_restrict == restricted) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 netif_carrier_ok(dev)) {
4198 err = __orinoco_hw_setup_wepkeys(priv);
4199 /* No need to commit if successful */
4200 goto out;
4201 }
4202
David Kilroy4ae6ee22008-08-21 23:27:59 +01004203 priv->encode_alg = encode_alg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 priv->wep_restrict = restricted;
4205
4206 out:
4207 orinoco_unlock(priv, &flags);
4208
4209 return err;
4210}
4211
Christoph Hellwig620554e2005-06-19 01:27:33 +02004212static int orinoco_ioctl_getiwencode(struct net_device *dev,
4213 struct iw_request_info *info,
4214 struct iw_point *erq,
4215 char *keybuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216{
4217 struct orinoco_private *priv = netdev_priv(dev);
4218 int index = (erq->flags & IW_ENCODE_INDEX) - 1;
4219 u16 xlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 unsigned long flags;
4221
David Kilroya94e8422009-02-04 23:05:44 +00004222 if (!priv->has_wep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 return -EOPNOTSUPP;
4224
4225 if (orinoco_lock(priv, &flags) != 0)
4226 return -EBUSY;
4227
4228 if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
4229 index = priv->tx_key;
4230
4231 erq->flags = 0;
David Kilroy4ae6ee22008-08-21 23:27:59 +01004232 if (!priv->encode_alg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 erq->flags |= IW_ENCODE_DISABLED;
4234 erq->flags |= index + 1;
4235
4236 if (priv->wep_restrict)
4237 erq->flags |= IW_ENCODE_RESTRICTED;
4238 else
4239 erq->flags |= IW_ENCODE_OPEN;
4240
4241 xlen = le16_to_cpu(priv->keys[index].len);
4242
4243 erq->length = xlen;
4244
4245 memcpy(keybuf, priv->keys[index].data, ORINOCO_MAX_KEY_SIZE);
4246
4247 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 return 0;
4249}
4250
Christoph Hellwig620554e2005-06-19 01:27:33 +02004251static int orinoco_ioctl_setessid(struct net_device *dev,
4252 struct iw_request_info *info,
4253 struct iw_point *erq,
4254 char *essidbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255{
4256 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 unsigned long flags;
4258
4259 /* Note : ESSID is ignored in Ad-Hoc demo mode, but we can set it
4260 * anyway... - Jean II */
4261
Christoph Hellwig620554e2005-06-19 01:27:33 +02004262 /* Hum... Should not use Wireless Extension constant (may change),
4263 * should use our own... - Jean II */
4264 if (erq->length > IW_ESSID_MAX_SIZE)
4265 return -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
4267 if (orinoco_lock(priv, &flags) != 0)
4268 return -EBUSY;
4269
Christoph Hellwig620554e2005-06-19 01:27:33 +02004270 /* NULL the string (for NULL termination & ESSID = ANY) - Jean II */
4271 memset(priv->desired_essid, 0, sizeof(priv->desired_essid));
4272
4273 /* If not ANY, get the new ESSID */
David Kilroy566f2d92009-02-04 23:05:45 +00004274 if (erq->flags)
Christoph Hellwig620554e2005-06-19 01:27:33 +02004275 memcpy(priv->desired_essid, essidbuf, erq->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276
4277 orinoco_unlock(priv, &flags);
4278
Christoph Hellwig620554e2005-06-19 01:27:33 +02004279 return -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280}
4281
Christoph Hellwig620554e2005-06-19 01:27:33 +02004282static int orinoco_ioctl_getessid(struct net_device *dev,
4283 struct iw_request_info *info,
4284 struct iw_point *erq,
4285 char *essidbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286{
4287 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 int active;
4289 int err = 0;
4290 unsigned long flags;
4291
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 if (netif_running(dev)) {
4293 err = orinoco_hw_get_essid(priv, &active, essidbuf);
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07004294 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 return err;
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07004296 erq->length = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 } else {
4298 if (orinoco_lock(priv, &flags) != 0)
4299 return -EBUSY;
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07004300 memcpy(essidbuf, priv->desired_essid, IW_ESSID_MAX_SIZE);
4301 erq->length = strlen(priv->desired_essid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 orinoco_unlock(priv, &flags);
4303 }
4304
4305 erq->flags = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 return 0;
4308}
4309
Christoph Hellwig620554e2005-06-19 01:27:33 +02004310static int orinoco_ioctl_setnick(struct net_device *dev,
4311 struct iw_request_info *info,
4312 struct iw_point *nrq,
4313 char *nickbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314{
4315 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 unsigned long flags;
4317
4318 if (nrq->length > IW_ESSID_MAX_SIZE)
4319 return -E2BIG;
4320
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 if (orinoco_lock(priv, &flags) != 0)
4322 return -EBUSY;
4323
Christoph Hellwig620554e2005-06-19 01:27:33 +02004324 memset(priv->nick, 0, sizeof(priv->nick));
4325 memcpy(priv->nick, nickbuf, nrq->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326
4327 orinoco_unlock(priv, &flags);
4328
Christoph Hellwig620554e2005-06-19 01:27:33 +02004329 return -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330}
4331
Christoph Hellwig620554e2005-06-19 01:27:33 +02004332static int orinoco_ioctl_getnick(struct net_device *dev,
4333 struct iw_request_info *info,
4334 struct iw_point *nrq,
4335 char *nickbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336{
4337 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 unsigned long flags;
4339
4340 if (orinoco_lock(priv, &flags) != 0)
4341 return -EBUSY;
4342
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07004343 memcpy(nickbuf, priv->nick, IW_ESSID_MAX_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 orinoco_unlock(priv, &flags);
4345
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07004346 nrq->length = strlen(priv->nick);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 return 0;
4349}
4350
Christoph Hellwig620554e2005-06-19 01:27:33 +02004351static int orinoco_ioctl_setfreq(struct net_device *dev,
4352 struct iw_request_info *info,
4353 struct iw_freq *frq,
4354 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355{
4356 struct orinoco_private *priv = netdev_priv(dev);
4357 int chan = -1;
4358 unsigned long flags;
Christoph Hellwig620554e2005-06-19 01:27:33 +02004359 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02004361 /* In infrastructure mode the AP sets the channel */
4362 if (priv->iw_mode == IW_MODE_INFRA)
4363 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364
David Kilroya94e8422009-02-04 23:05:44 +00004365 if ((frq->e == 0) && (frq->m <= 1000)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 /* Setting by channel number */
4367 chan = frq->m;
4368 } else {
David Kilroy9ee677c2008-12-23 14:03:38 +00004369 /* Setting by frequency */
4370 int denom = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 int i;
4372
David Kilroy9ee677c2008-12-23 14:03:38 +00004373 /* Calculate denominator to rescale to MHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 for (i = 0; i < (6 - frq->e); i++)
David Kilroy9ee677c2008-12-23 14:03:38 +00004375 denom *= 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376
David Kilroy9ee677c2008-12-23 14:03:38 +00004377 chan = ieee80211_freq_to_dsss_chan(frq->m / denom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 }
4379
David Kilroya94e8422009-02-04 23:05:44 +00004380 if ((chan < 1) || (chan > NUM_CHANNELS) ||
4381 !(priv->channel_mask & (1 << (chan-1))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 return -EINVAL;
4383
4384 if (orinoco_lock(priv, &flags) != 0)
4385 return -EBUSY;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02004386
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 priv->channel = chan;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02004388 if (priv->iw_mode == IW_MODE_MONITOR) {
4389 /* Fast channel change - no commit if successful */
4390 hermes_t *hw = &priv->hw;
4391 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
4392 HERMES_TEST_SET_CHANNEL,
4393 chan, NULL);
4394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 orinoco_unlock(priv, &flags);
4396
Christoph Hellwig620554e2005-06-19 01:27:33 +02004397 return err;
4398}
4399
4400static int orinoco_ioctl_getfreq(struct net_device *dev,
4401 struct iw_request_info *info,
4402 struct iw_freq *frq,
4403 char *extra)
4404{
4405 struct orinoco_private *priv = netdev_priv(dev);
4406 int tmp;
4407
4408 /* Locking done in there */
4409 tmp = orinoco_hw_get_freq(priv);
David Kilroy566f2d92009-02-04 23:05:45 +00004410 if (tmp < 0)
Christoph Hellwig620554e2005-06-19 01:27:33 +02004411 return tmp;
Christoph Hellwig620554e2005-06-19 01:27:33 +02004412
David Kilroy9ee677c2008-12-23 14:03:38 +00004413 frq->m = tmp * 100000;
Christoph Hellwig620554e2005-06-19 01:27:33 +02004414 frq->e = 1;
4415
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 return 0;
4417}
4418
Christoph Hellwig620554e2005-06-19 01:27:33 +02004419static int orinoco_ioctl_getsens(struct net_device *dev,
4420 struct iw_request_info *info,
4421 struct iw_param *srq,
4422 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423{
4424 struct orinoco_private *priv = netdev_priv(dev);
4425 hermes_t *hw = &priv->hw;
4426 u16 val;
4427 int err;
4428 unsigned long flags;
4429
4430 if (!priv->has_sensitivity)
4431 return -EOPNOTSUPP;
4432
4433 if (orinoco_lock(priv, &flags) != 0)
4434 return -EBUSY;
4435 err = hermes_read_wordrec(hw, USER_BAP,
4436 HERMES_RID_CNFSYSTEMSCALE, &val);
4437 orinoco_unlock(priv, &flags);
4438
4439 if (err)
4440 return err;
4441
4442 srq->value = val;
4443 srq->fixed = 0; /* auto */
4444
4445 return 0;
4446}
4447
Christoph Hellwig620554e2005-06-19 01:27:33 +02004448static int orinoco_ioctl_setsens(struct net_device *dev,
4449 struct iw_request_info *info,
4450 struct iw_param *srq,
4451 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452{
4453 struct orinoco_private *priv = netdev_priv(dev);
4454 int val = srq->value;
4455 unsigned long flags;
4456
4457 if (!priv->has_sensitivity)
4458 return -EOPNOTSUPP;
4459
4460 if ((val < 1) || (val > 3))
4461 return -EINVAL;
David Kilroy6fe9deb2009-02-04 23:05:43 +00004462
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 if (orinoco_lock(priv, &flags) != 0)
4464 return -EBUSY;
4465 priv->ap_density = val;
4466 orinoco_unlock(priv, &flags);
4467
Christoph Hellwig620554e2005-06-19 01:27:33 +02004468 return -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469}
4470
Christoph Hellwig620554e2005-06-19 01:27:33 +02004471static int orinoco_ioctl_setrts(struct net_device *dev,
4472 struct iw_request_info *info,
4473 struct iw_param *rrq,
4474 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475{
4476 struct orinoco_private *priv = netdev_priv(dev);
4477 int val = rrq->value;
4478 unsigned long flags;
4479
4480 if (rrq->disabled)
4481 val = 2347;
4482
David Kilroya94e8422009-02-04 23:05:44 +00004483 if ((val < 0) || (val > 2347))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 return -EINVAL;
4485
4486 if (orinoco_lock(priv, &flags) != 0)
4487 return -EBUSY;
4488
4489 priv->rts_thresh = val;
4490 orinoco_unlock(priv, &flags);
4491
Christoph Hellwig620554e2005-06-19 01:27:33 +02004492 return -EINPROGRESS; /* Call commit handler */
4493}
4494
4495static int orinoco_ioctl_getrts(struct net_device *dev,
4496 struct iw_request_info *info,
4497 struct iw_param *rrq,
4498 char *extra)
4499{
4500 struct orinoco_private *priv = netdev_priv(dev);
4501
4502 rrq->value = priv->rts_thresh;
4503 rrq->disabled = (rrq->value == 2347);
4504 rrq->fixed = 1;
4505
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 return 0;
4507}
4508
Christoph Hellwig620554e2005-06-19 01:27:33 +02004509static int orinoco_ioctl_setfrag(struct net_device *dev,
4510 struct iw_request_info *info,
4511 struct iw_param *frq,
4512 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513{
4514 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004515 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 unsigned long flags;
4517
4518 if (orinoco_lock(priv, &flags) != 0)
4519 return -EBUSY;
4520
4521 if (priv->has_mwo) {
4522 if (frq->disabled)
4523 priv->mwo_robust = 0;
4524 else {
4525 if (frq->fixed)
David Kilroyb2f30a02009-02-04 23:05:46 +00004526 printk(KERN_WARNING "%s: Fixed fragmentation "
4527 "is not supported on this firmware. "
4528 "Using MWO robust instead.\n",
4529 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 priv->mwo_robust = 1;
4531 }
4532 } else {
4533 if (frq->disabled)
4534 priv->frag_thresh = 2346;
4535 else {
David Kilroya94e8422009-02-04 23:05:44 +00004536 if ((frq->value < 256) || (frq->value > 2346))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 err = -EINVAL;
4538 else
David Kilroyb2f30a02009-02-04 23:05:46 +00004539 /* must be even */
4540 priv->frag_thresh = frq->value & ~0x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 }
4542 }
4543
4544 orinoco_unlock(priv, &flags);
4545
4546 return err;
4547}
4548
Christoph Hellwig620554e2005-06-19 01:27:33 +02004549static int orinoco_ioctl_getfrag(struct net_device *dev,
4550 struct iw_request_info *info,
4551 struct iw_param *frq,
4552 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553{
4554 struct orinoco_private *priv = netdev_priv(dev);
4555 hermes_t *hw = &priv->hw;
Christoph Hellwig620554e2005-06-19 01:27:33 +02004556 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 u16 val;
4558 unsigned long flags;
4559
4560 if (orinoco_lock(priv, &flags) != 0)
4561 return -EBUSY;
David Kilroy6fe9deb2009-02-04 23:05:43 +00004562
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 if (priv->has_mwo) {
4564 err = hermes_read_wordrec(hw, USER_BAP,
4565 HERMES_RID_CNFMWOROBUST_AGERE,
4566 &val);
4567 if (err)
4568 val = 0;
4569
4570 frq->value = val ? 2347 : 0;
David Kilroya94e8422009-02-04 23:05:44 +00004571 frq->disabled = !val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 frq->fixed = 0;
4573 } else {
David Kilroyb2f30a02009-02-04 23:05:46 +00004574 err = hermes_read_wordrec(hw, USER_BAP,
4575 HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 &val);
4577 if (err)
4578 val = 0;
4579
4580 frq->value = val;
4581 frq->disabled = (val >= 2346);
4582 frq->fixed = 1;
4583 }
4584
4585 orinoco_unlock(priv, &flags);
David Kilroy6fe9deb2009-02-04 23:05:43 +00004586
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 return err;
4588}
4589
Christoph Hellwig620554e2005-06-19 01:27:33 +02004590static int orinoco_ioctl_setrate(struct net_device *dev,
4591 struct iw_request_info *info,
4592 struct iw_param *rrq,
4593 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594{
4595 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 int ratemode = -1;
4597 int bitrate; /* 100s of kilobits */
4598 int i;
4599 unsigned long flags;
David Kilroy6fe9deb2009-02-04 23:05:43 +00004600
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 /* As the user space doesn't know our highest rate, it uses -1
4602 * to ask us to set the highest rate. Test it using "iwconfig
4603 * ethX rate auto" - Jean II */
4604 if (rrq->value == -1)
4605 bitrate = 110;
4606 else {
4607 if (rrq->value % 100000)
4608 return -EINVAL;
4609 bitrate = rrq->value / 100000;
4610 }
4611
David Kilroya94e8422009-02-04 23:05:44 +00004612 if ((bitrate != 10) && (bitrate != 20) &&
4613 (bitrate != 55) && (bitrate != 110))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 return -EINVAL;
4615
4616 for (i = 0; i < BITRATE_TABLE_SIZE; i++)
David Kilroya94e8422009-02-04 23:05:44 +00004617 if ((bitrate_table[i].bitrate == bitrate) &&
4618 (bitrate_table[i].automatic == !rrq->fixed)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 ratemode = i;
4620 break;
4621 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00004622
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 if (ratemode == -1)
4624 return -EINVAL;
4625
4626 if (orinoco_lock(priv, &flags) != 0)
4627 return -EBUSY;
4628 priv->bitratemode = ratemode;
4629 orinoco_unlock(priv, &flags);
4630
Christoph Hellwig620554e2005-06-19 01:27:33 +02004631 return -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632}
4633
Christoph Hellwig620554e2005-06-19 01:27:33 +02004634static int orinoco_ioctl_getrate(struct net_device *dev,
4635 struct iw_request_info *info,
4636 struct iw_param *rrq,
4637 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638{
4639 struct orinoco_private *priv = netdev_priv(dev);
4640 hermes_t *hw = &priv->hw;
4641 int err = 0;
4642 int ratemode;
4643 int i;
4644 u16 val;
4645 unsigned long flags;
4646
4647 if (orinoco_lock(priv, &flags) != 0)
4648 return -EBUSY;
4649
4650 ratemode = priv->bitratemode;
4651
4652 BUG_ON((ratemode < 0) || (ratemode >= BITRATE_TABLE_SIZE));
4653
4654 rrq->value = bitrate_table[ratemode].bitrate * 100000;
David Kilroya94e8422009-02-04 23:05:44 +00004655 rrq->fixed = !bitrate_table[ratemode].automatic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 rrq->disabled = 0;
4657
4658 /* If the interface is running we try to find more about the
4659 current mode */
4660 if (netif_running(dev)) {
4661 err = hermes_read_wordrec(hw, USER_BAP,
4662 HERMES_RID_CURRENTTXRATE, &val);
4663 if (err)
4664 goto out;
David Kilroy6fe9deb2009-02-04 23:05:43 +00004665
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 switch (priv->firmware_type) {
4667 case FIRMWARE_TYPE_AGERE: /* Lucent style rate */
4668 /* Note : in Lucent firmware, the return value of
4669 * HERMES_RID_CURRENTTXRATE is the bitrate in Mb/s,
4670 * and therefore is totally different from the
4671 * encoding of HERMES_RID_CNFTXRATECONTROL.
4672 * Don't forget that 6Mb/s is really 5.5Mb/s */
4673 if (val == 6)
4674 rrq->value = 5500000;
4675 else
4676 rrq->value = val * 1000000;
4677 break;
4678 case FIRMWARE_TYPE_INTERSIL: /* Intersil style rate */
4679 case FIRMWARE_TYPE_SYMBOL: /* Symbol style rate */
4680 for (i = 0; i < BITRATE_TABLE_SIZE; i++)
4681 if (bitrate_table[i].intersil_txratectrl == val) {
4682 ratemode = i;
4683 break;
4684 }
4685 if (i >= BITRATE_TABLE_SIZE)
4686 printk(KERN_INFO "%s: Unable to determine current bitrate (0x%04hx)\n",
4687 dev->name, val);
4688
4689 rrq->value = bitrate_table[ratemode].bitrate * 100000;
4690 break;
4691 default:
4692 BUG();
4693 }
4694 }
4695
4696 out:
4697 orinoco_unlock(priv, &flags);
4698
4699 return err;
4700}
4701
Christoph Hellwig620554e2005-06-19 01:27:33 +02004702static int orinoco_ioctl_setpower(struct net_device *dev,
4703 struct iw_request_info *info,
4704 struct iw_param *prq,
4705 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706{
4707 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004708 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 unsigned long flags;
4710
4711 if (orinoco_lock(priv, &flags) != 0)
4712 return -EBUSY;
4713
4714 if (prq->disabled) {
4715 priv->pm_on = 0;
4716 } else {
4717 switch (prq->flags & IW_POWER_MODE) {
4718 case IW_POWER_UNICAST_R:
4719 priv->pm_mcast = 0;
4720 priv->pm_on = 1;
4721 break;
4722 case IW_POWER_ALL_R:
4723 priv->pm_mcast = 1;
4724 priv->pm_on = 1;
4725 break;
4726 case IW_POWER_ON:
4727 /* No flags : but we may have a value - Jean II */
4728 break;
4729 default:
4730 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 goto out;
Pavel Roskinc08ad1e2005-11-29 02:59:27 -05004732 }
David Kilroy6fe9deb2009-02-04 23:05:43 +00004733
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 if (prq->flags & IW_POWER_TIMEOUT) {
4735 priv->pm_on = 1;
4736 priv->pm_timeout = prq->value / 1000;
4737 }
4738 if (prq->flags & IW_POWER_PERIOD) {
4739 priv->pm_on = 1;
4740 priv->pm_period = prq->value / 1000;
4741 }
4742 /* It's valid to not have a value if we are just toggling
4743 * the flags... Jean II */
David Kilroya94e8422009-02-04 23:05:44 +00004744 if (!priv->pm_on) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 err = -EINVAL;
4746 goto out;
David Kilroy6fe9deb2009-02-04 23:05:43 +00004747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 }
4749
4750 out:
4751 orinoco_unlock(priv, &flags);
4752
4753 return err;
4754}
4755
Christoph Hellwig620554e2005-06-19 01:27:33 +02004756static int orinoco_ioctl_getpower(struct net_device *dev,
4757 struct iw_request_info *info,
4758 struct iw_param *prq,
4759 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760{
4761 struct orinoco_private *priv = netdev_priv(dev);
4762 hermes_t *hw = &priv->hw;
4763 int err = 0;
4764 u16 enable, period, timeout, mcast;
4765 unsigned long flags;
4766
4767 if (orinoco_lock(priv, &flags) != 0)
4768 return -EBUSY;
David Kilroy6fe9deb2009-02-04 23:05:43 +00004769
David Kilroyb2f30a02009-02-04 23:05:46 +00004770 err = hermes_read_wordrec(hw, USER_BAP,
4771 HERMES_RID_CNFPMENABLED, &enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 if (err)
4773 goto out;
4774
4775 err = hermes_read_wordrec(hw, USER_BAP,
4776 HERMES_RID_CNFMAXSLEEPDURATION, &period);
4777 if (err)
4778 goto out;
4779
David Kilroyb2f30a02009-02-04 23:05:46 +00004780 err = hermes_read_wordrec(hw, USER_BAP,
4781 HERMES_RID_CNFPMHOLDOVERDURATION, &timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 if (err)
4783 goto out;
4784
David Kilroyb2f30a02009-02-04 23:05:46 +00004785 err = hermes_read_wordrec(hw, USER_BAP,
4786 HERMES_RID_CNFMULTICASTRECEIVE, &mcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 if (err)
4788 goto out;
4789
4790 prq->disabled = !enable;
4791 /* Note : by default, display the period */
4792 if ((prq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
4793 prq->flags = IW_POWER_TIMEOUT;
4794 prq->value = timeout * 1000;
4795 } else {
4796 prq->flags = IW_POWER_PERIOD;
4797 prq->value = period * 1000;
4798 }
4799 if (mcast)
4800 prq->flags |= IW_POWER_ALL_R;
4801 else
4802 prq->flags |= IW_POWER_UNICAST_R;
4803
4804 out:
4805 orinoco_unlock(priv, &flags);
4806
4807 return err;
4808}
4809
David Kilroyd03032a2008-08-21 23:28:02 +01004810static int orinoco_ioctl_set_encodeext(struct net_device *dev,
4811 struct iw_request_info *info,
4812 union iwreq_data *wrqu,
4813 char *extra)
4814{
4815 struct orinoco_private *priv = netdev_priv(dev);
4816 struct iw_point *encoding = &wrqu->encoding;
4817 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
4818 int idx, alg = ext->alg, set_key = 1;
4819 unsigned long flags;
4820 int err = -EINVAL;
4821 u16 key_len;
4822
4823 if (orinoco_lock(priv, &flags) != 0)
4824 return -EBUSY;
4825
4826 /* Determine and validate the key index */
4827 idx = encoding->flags & IW_ENCODE_INDEX;
4828 if (idx) {
Johannes Berg2c7060022008-10-30 22:09:54 +01004829 if ((idx < 1) || (idx > 4))
David Kilroyd03032a2008-08-21 23:28:02 +01004830 goto out;
4831 idx--;
4832 } else
4833 idx = priv->tx_key;
4834
4835 if (encoding->flags & IW_ENCODE_DISABLED)
David Kilroy6fe9deb2009-02-04 23:05:43 +00004836 alg = IW_ENCODE_ALG_NONE;
David Kilroyd03032a2008-08-21 23:28:02 +01004837
4838 if (priv->has_wpa && (alg != IW_ENCODE_ALG_TKIP)) {
4839 /* Clear any TKIP TX key we had */
4840 (void) orinoco_clear_tkip_key(priv, priv->tx_key);
4841 }
4842
4843 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
4844 priv->tx_key = idx;
4845 set_key = ((alg == IW_ENCODE_ALG_TKIP) ||
4846 (ext->key_len > 0)) ? 1 : 0;
4847 }
4848
4849 if (set_key) {
4850 /* Set the requested key first */
4851 switch (alg) {
4852 case IW_ENCODE_ALG_NONE:
4853 priv->encode_alg = alg;
4854 priv->keys[idx].len = 0;
4855 break;
4856
4857 case IW_ENCODE_ALG_WEP:
4858 if (ext->key_len > SMALL_KEY_SIZE)
4859 key_len = LARGE_KEY_SIZE;
4860 else if (ext->key_len > 0)
4861 key_len = SMALL_KEY_SIZE;
4862 else
4863 goto out;
4864
4865 priv->encode_alg = alg;
4866 priv->keys[idx].len = cpu_to_le16(key_len);
4867
4868 key_len = min(ext->key_len, key_len);
4869
4870 memset(priv->keys[idx].data, 0, ORINOCO_MAX_KEY_SIZE);
4871 memcpy(priv->keys[idx].data, ext->key, key_len);
4872 break;
4873
4874 case IW_ENCODE_ALG_TKIP:
4875 {
4876 hermes_t *hw = &priv->hw;
4877 u8 *tkip_iv = NULL;
4878
4879 if (!priv->has_wpa ||
4880 (ext->key_len > sizeof(priv->tkip_key[0])))
4881 goto out;
4882
4883 priv->encode_alg = alg;
4884 memset(&priv->tkip_key[idx], 0,
4885 sizeof(priv->tkip_key[idx]));
4886 memcpy(&priv->tkip_key[idx], ext->key, ext->key_len);
4887
4888 if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID)
4889 tkip_iv = &ext->rx_seq[0];
4890
4891 err = __orinoco_hw_set_tkip_key(hw, idx,
4892 ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY,
4893 (u8 *) &priv->tkip_key[idx],
4894 tkip_iv, NULL);
4895 if (err)
4896 printk(KERN_ERR "%s: Error %d setting TKIP key"
4897 "\n", dev->name, err);
4898
4899 goto out;
4900 }
4901 default:
4902 goto out;
4903 }
4904 }
4905 err = -EINPROGRESS;
4906 out:
4907 orinoco_unlock(priv, &flags);
4908
4909 return err;
4910}
4911
4912static int orinoco_ioctl_get_encodeext(struct net_device *dev,
4913 struct iw_request_info *info,
4914 union iwreq_data *wrqu,
4915 char *extra)
4916{
4917 struct orinoco_private *priv = netdev_priv(dev);
4918 struct iw_point *encoding = &wrqu->encoding;
4919 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
4920 int idx, max_key_len;
4921 unsigned long flags;
4922 int err;
4923
4924 if (orinoco_lock(priv, &flags) != 0)
4925 return -EBUSY;
4926
4927 err = -EINVAL;
4928 max_key_len = encoding->length - sizeof(*ext);
4929 if (max_key_len < 0)
4930 goto out;
4931
4932 idx = encoding->flags & IW_ENCODE_INDEX;
4933 if (idx) {
Johannes Berg2c7060022008-10-30 22:09:54 +01004934 if ((idx < 1) || (idx > 4))
David Kilroyd03032a2008-08-21 23:28:02 +01004935 goto out;
4936 idx--;
4937 } else
4938 idx = priv->tx_key;
4939
4940 encoding->flags = idx + 1;
4941 memset(ext, 0, sizeof(*ext));
4942
4943 ext->alg = priv->encode_alg;
4944 switch (priv->encode_alg) {
4945 case IW_ENCODE_ALG_NONE:
4946 ext->key_len = 0;
4947 encoding->flags |= IW_ENCODE_DISABLED;
4948 break;
4949 case IW_ENCODE_ALG_WEP:
David Kilroy75d31cf2008-09-12 22:28:18 +01004950 ext->key_len = min_t(u16, le16_to_cpu(priv->keys[idx].len),
4951 max_key_len);
David Kilroyd03032a2008-08-21 23:28:02 +01004952 memcpy(ext->key, priv->keys[idx].data, ext->key_len);
4953 encoding->flags |= IW_ENCODE_ENABLED;
4954 break;
4955 case IW_ENCODE_ALG_TKIP:
David Kilroy75d31cf2008-09-12 22:28:18 +01004956 ext->key_len = min_t(u16, sizeof(struct orinoco_tkip_key),
4957 max_key_len);
David Kilroyd03032a2008-08-21 23:28:02 +01004958 memcpy(ext->key, &priv->tkip_key[idx], ext->key_len);
4959 encoding->flags |= IW_ENCODE_ENABLED;
4960 break;
4961 }
4962
4963 err = 0;
4964 out:
4965 orinoco_unlock(priv, &flags);
4966
4967 return err;
4968}
4969
4970static int orinoco_ioctl_set_auth(struct net_device *dev,
4971 struct iw_request_info *info,
4972 union iwreq_data *wrqu, char *extra)
4973{
4974 struct orinoco_private *priv = netdev_priv(dev);
4975 hermes_t *hw = &priv->hw;
4976 struct iw_param *param = &wrqu->param;
4977 unsigned long flags;
4978 int ret = -EINPROGRESS;
4979
4980 if (orinoco_lock(priv, &flags) != 0)
4981 return -EBUSY;
4982
4983 switch (param->flags & IW_AUTH_INDEX) {
4984 case IW_AUTH_WPA_VERSION:
4985 case IW_AUTH_CIPHER_PAIRWISE:
4986 case IW_AUTH_CIPHER_GROUP:
4987 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
4988 case IW_AUTH_PRIVACY_INVOKED:
4989 case IW_AUTH_DROP_UNENCRYPTED:
4990 /*
4991 * orinoco does not use these parameters
4992 */
4993 break;
4994
4995 case IW_AUTH_KEY_MGMT:
4996 /* wl_lkm implies value 2 == PSK for Hermes I
4997 * which ties in with WEXT
4998 * no other hints tho :(
4999 */
5000 priv->key_mgmt = param->value;
5001 break;
5002
5003 case IW_AUTH_TKIP_COUNTERMEASURES:
5004 /* When countermeasures are enabled, shut down the
5005 * card; when disabled, re-enable the card. This must
5006 * take effect immediately.
5007 *
5008 * TODO: Make sure that the EAPOL message is getting
5009 * out before card disabled
5010 */
5011 if (param->value) {
5012 priv->tkip_cm_active = 1;
5013 ret = hermes_enable_port(hw, 0);
5014 } else {
5015 priv->tkip_cm_active = 0;
5016 ret = hermes_disable_port(hw, 0);
5017 }
5018 break;
5019
5020 case IW_AUTH_80211_AUTH_ALG:
5021 if (param->value & IW_AUTH_ALG_SHARED_KEY)
5022 priv->wep_restrict = 1;
5023 else if (param->value & IW_AUTH_ALG_OPEN_SYSTEM)
5024 priv->wep_restrict = 0;
5025 else
5026 ret = -EINVAL;
5027 break;
5028
5029 case IW_AUTH_WPA_ENABLED:
5030 if (priv->has_wpa) {
5031 priv->wpa_enabled = param->value ? 1 : 0;
5032 } else {
5033 if (param->value)
5034 ret = -EOPNOTSUPP;
5035 /* else silently accept disable of WPA */
5036 priv->wpa_enabled = 0;
5037 }
5038 break;
5039
5040 default:
5041 ret = -EOPNOTSUPP;
5042 }
5043
5044 orinoco_unlock(priv, &flags);
5045 return ret;
5046}
5047
5048static int orinoco_ioctl_get_auth(struct net_device *dev,
5049 struct iw_request_info *info,
5050 union iwreq_data *wrqu, char *extra)
5051{
5052 struct orinoco_private *priv = netdev_priv(dev);
5053 struct iw_param *param = &wrqu->param;
5054 unsigned long flags;
5055 int ret = 0;
5056
5057 if (orinoco_lock(priv, &flags) != 0)
5058 return -EBUSY;
5059
5060 switch (param->flags & IW_AUTH_INDEX) {
5061 case IW_AUTH_KEY_MGMT:
5062 param->value = priv->key_mgmt;
5063 break;
5064
5065 case IW_AUTH_TKIP_COUNTERMEASURES:
5066 param->value = priv->tkip_cm_active;
5067 break;
5068
5069 case IW_AUTH_80211_AUTH_ALG:
5070 if (priv->wep_restrict)
5071 param->value = IW_AUTH_ALG_SHARED_KEY;
5072 else
5073 param->value = IW_AUTH_ALG_OPEN_SYSTEM;
5074 break;
5075
5076 case IW_AUTH_WPA_ENABLED:
5077 param->value = priv->wpa_enabled;
5078 break;
5079
5080 default:
5081 ret = -EOPNOTSUPP;
5082 }
5083
5084 orinoco_unlock(priv, &flags);
5085 return ret;
5086}
5087
5088static int orinoco_ioctl_set_genie(struct net_device *dev,
5089 struct iw_request_info *info,
5090 union iwreq_data *wrqu, char *extra)
5091{
5092 struct orinoco_private *priv = netdev_priv(dev);
5093 u8 *buf;
5094 unsigned long flags;
David Kilroyd03032a2008-08-21 23:28:02 +01005095
Johannes Berg2c7060022008-10-30 22:09:54 +01005096 /* cut off at IEEE80211_MAX_DATA_LEN */
5097 if ((wrqu->data.length > IEEE80211_MAX_DATA_LEN) ||
David Kilroyd03032a2008-08-21 23:28:02 +01005098 (wrqu->data.length && (extra == NULL)))
5099 return -EINVAL;
5100
David Kilroyd03032a2008-08-21 23:28:02 +01005101 if (wrqu->data.length) {
5102 buf = kmalloc(wrqu->data.length, GFP_KERNEL);
Andrey Borzenkov7fe99c42009-01-20 20:26:46 +03005103 if (buf == NULL)
5104 return -ENOMEM;
David Kilroyd03032a2008-08-21 23:28:02 +01005105
5106 memcpy(buf, extra, wrqu->data.length);
Andrey Borzenkov7fe99c42009-01-20 20:26:46 +03005107 } else
5108 buf = NULL;
5109
5110 if (orinoco_lock(priv, &flags) != 0) {
5111 kfree(buf);
5112 return -EBUSY;
David Kilroyd03032a2008-08-21 23:28:02 +01005113 }
5114
Andrey Borzenkov7fe99c42009-01-20 20:26:46 +03005115 kfree(priv->wpa_ie);
5116 priv->wpa_ie = buf;
5117 priv->wpa_ie_len = wrqu->data.length;
5118
David Kilroyd03032a2008-08-21 23:28:02 +01005119 if (priv->wpa_ie) {
5120 /* Looks like wl_lkm wants to check the auth alg, and
5121 * somehow pass it to the firmware.
5122 * Instead it just calls the key mgmt rid
5123 * - we do this in set auth.
5124 */
5125 }
5126
David Kilroyd03032a2008-08-21 23:28:02 +01005127 orinoco_unlock(priv, &flags);
Andrey Borzenkov7fe99c42009-01-20 20:26:46 +03005128 return 0;
David Kilroyd03032a2008-08-21 23:28:02 +01005129}
5130
5131static int orinoco_ioctl_get_genie(struct net_device *dev,
5132 struct iw_request_info *info,
5133 union iwreq_data *wrqu, char *extra)
5134{
5135 struct orinoco_private *priv = netdev_priv(dev);
5136 unsigned long flags;
5137 int err = 0;
5138
5139 if (orinoco_lock(priv, &flags) != 0)
5140 return -EBUSY;
5141
5142 if ((priv->wpa_ie_len == 0) || (priv->wpa_ie == NULL)) {
5143 wrqu->data.length = 0;
5144 goto out;
5145 }
5146
5147 if (wrqu->data.length < priv->wpa_ie_len) {
5148 err = -E2BIG;
5149 goto out;
5150 }
5151
5152 wrqu->data.length = priv->wpa_ie_len;
5153 memcpy(extra, priv->wpa_ie, priv->wpa_ie_len);
5154
5155out:
5156 orinoco_unlock(priv, &flags);
5157 return err;
5158}
5159
5160static int orinoco_ioctl_set_mlme(struct net_device *dev,
5161 struct iw_request_info *info,
5162 union iwreq_data *wrqu, char *extra)
5163{
5164 struct orinoco_private *priv = netdev_priv(dev);
5165 hermes_t *hw = &priv->hw;
5166 struct iw_mlme *mlme = (struct iw_mlme *)extra;
5167 unsigned long flags;
5168 int ret = 0;
5169
5170 if (orinoco_lock(priv, &flags) != 0)
5171 return -EBUSY;
5172
5173 switch (mlme->cmd) {
5174 case IW_MLME_DEAUTH:
5175 /* silently ignore */
5176 break;
5177
5178 case IW_MLME_DISASSOC:
5179 {
5180 struct {
5181 u8 addr[ETH_ALEN];
5182 __le16 reason_code;
5183 } __attribute__ ((packed)) buf;
5184
5185 memcpy(buf.addr, mlme->addr.sa_data, ETH_ALEN);
5186 buf.reason_code = cpu_to_le16(mlme->reason_code);
5187 ret = HERMES_WRITE_RECORD(hw, USER_BAP,
5188 HERMES_RID_CNFDISASSOCIATE,
5189 &buf);
5190 break;
5191 }
5192 default:
5193 ret = -EOPNOTSUPP;
5194 }
5195
5196 orinoco_unlock(priv, &flags);
5197 return ret;
5198}
5199
Christoph Hellwig620554e2005-06-19 01:27:33 +02005200static int orinoco_ioctl_getretry(struct net_device *dev,
5201 struct iw_request_info *info,
5202 struct iw_param *rrq,
5203 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204{
5205 struct orinoco_private *priv = netdev_priv(dev);
5206 hermes_t *hw = &priv->hw;
5207 int err = 0;
5208 u16 short_limit, long_limit, lifetime;
5209 unsigned long flags;
5210
5211 if (orinoco_lock(priv, &flags) != 0)
5212 return -EBUSY;
David Kilroy6fe9deb2009-02-04 23:05:43 +00005213
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_SHORTRETRYLIMIT,
5215 &short_limit);
5216 if (err)
5217 goto out;
5218
5219 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_LONGRETRYLIMIT,
5220 &long_limit);
5221 if (err)
5222 goto out;
5223
5224 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_MAXTRANSMITLIFETIME,
5225 &lifetime);
5226 if (err)
5227 goto out;
5228
5229 rrq->disabled = 0; /* Can't be disabled */
5230
5231 /* Note : by default, display the retry number */
5232 if ((rrq->flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME) {
5233 rrq->flags = IW_RETRY_LIFETIME;
5234 rrq->value = lifetime * 1000; /* ??? */
5235 } else {
5236 /* By default, display the min number */
Jean Tourrilheseeec9f12006-08-29 18:02:31 -07005237 if ((rrq->flags & IW_RETRY_LONG)) {
5238 rrq->flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 rrq->value = long_limit;
5240 } else {
5241 rrq->flags = IW_RETRY_LIMIT;
5242 rrq->value = short_limit;
David Kilroya94e8422009-02-04 23:05:44 +00005243 if (short_limit != long_limit)
Jean Tourrilheseeec9f12006-08-29 18:02:31 -07005244 rrq->flags |= IW_RETRY_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 }
5246 }
5247
5248 out:
5249 orinoco_unlock(priv, &flags);
5250
5251 return err;
5252}
5253
Christoph Hellwig620554e2005-06-19 01:27:33 +02005254static int orinoco_ioctl_reset(struct net_device *dev,
5255 struct iw_request_info *info,
5256 void *wrqu,
5257 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258{
5259 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005260
David Kilroya94e8422009-02-04 23:05:44 +00005261 if (!capable(CAP_NET_ADMIN))
Christoph Hellwig620554e2005-06-19 01:27:33 +02005262 return -EPERM;
5263
5264 if (info->cmd == (SIOCIWFIRSTPRIV + 0x1)) {
5265 printk(KERN_DEBUG "%s: Forcing reset!\n", dev->name);
5266
5267 /* Firmware reset */
David Howellsc4028952006-11-22 14:57:56 +00005268 orinoco_reset(&priv->reset_work);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005269 } else {
5270 printk(KERN_DEBUG "%s: Force scheduling reset!\n", dev->name);
5271
5272 schedule_work(&priv->reset_work);
5273 }
5274
5275 return 0;
5276}
5277
5278static int orinoco_ioctl_setibssport(struct net_device *dev,
5279 struct iw_request_info *info,
5280 void *wrqu,
5281 char *extra)
5282
5283{
5284 struct orinoco_private *priv = netdev_priv(dev);
David Kilroya94e8422009-02-04 23:05:44 +00005285 int val = *((int *) extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 unsigned long flags;
5287
5288 if (orinoco_lock(priv, &flags) != 0)
5289 return -EBUSY;
5290
5291 priv->ibss_port = val ;
5292
5293 /* Actually update the mode we are using */
5294 set_port_type(priv);
5295
5296 orinoco_unlock(priv, &flags);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005297 return -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298}
5299
Christoph Hellwig620554e2005-06-19 01:27:33 +02005300static int orinoco_ioctl_getibssport(struct net_device *dev,
5301 struct iw_request_info *info,
5302 void *wrqu,
5303 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304{
5305 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005306 int *val = (int *) extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307
5308 *val = priv->ibss_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 return 0;
5310}
5311
Christoph Hellwig620554e2005-06-19 01:27:33 +02005312static int orinoco_ioctl_setport3(struct net_device *dev,
5313 struct iw_request_info *info,
5314 void *wrqu,
5315 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316{
5317 struct orinoco_private *priv = netdev_priv(dev);
David Kilroya94e8422009-02-04 23:05:44 +00005318 int val = *((int *) extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 int err = 0;
5320 unsigned long flags;
5321
5322 if (orinoco_lock(priv, &flags) != 0)
5323 return -EBUSY;
5324
5325 switch (val) {
5326 case 0: /* Try to do IEEE ad-hoc mode */
David Kilroya94e8422009-02-04 23:05:44 +00005327 if (!priv->has_ibss) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 err = -EINVAL;
5329 break;
5330 }
5331 priv->prefer_port3 = 0;
David Kilroy6fe9deb2009-02-04 23:05:43 +00005332
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 break;
5334
5335 case 1: /* Try to do Lucent proprietary ad-hoc mode */
David Kilroya94e8422009-02-04 23:05:44 +00005336 if (!priv->has_port3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 err = -EINVAL;
5338 break;
5339 }
5340 priv->prefer_port3 = 1;
5341 break;
5342
5343 default:
5344 err = -EINVAL;
5345 }
5346
David Kilroya94e8422009-02-04 23:05:44 +00005347 if (!err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 /* Actually update the mode we are using */
5349 set_port_type(priv);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005350 err = -EINPROGRESS;
5351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352
5353 orinoco_unlock(priv, &flags);
5354
5355 return err;
5356}
5357
Christoph Hellwig620554e2005-06-19 01:27:33 +02005358static int orinoco_ioctl_getport3(struct net_device *dev,
5359 struct iw_request_info *info,
5360 void *wrqu,
5361 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362{
5363 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005364 int *val = (int *) extra;
5365
5366 *val = priv->prefer_port3;
5367 return 0;
5368}
5369
5370static int orinoco_ioctl_setpreamble(struct net_device *dev,
5371 struct iw_request_info *info,
5372 void *wrqu,
5373 char *extra)
5374{
5375 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 unsigned long flags;
Christoph Hellwig620554e2005-06-19 01:27:33 +02005377 int val;
5378
David Kilroya94e8422009-02-04 23:05:44 +00005379 if (!priv->has_preamble)
Christoph Hellwig620554e2005-06-19 01:27:33 +02005380 return -EOPNOTSUPP;
5381
5382 /* 802.11b has recently defined some short preamble.
5383 * Basically, the Phy header has been reduced in size.
5384 * This increase performance, especially at high rates
5385 * (the preamble is transmitted at 1Mb/s), unfortunately
5386 * this give compatibility troubles... - Jean II */
David Kilroya94e8422009-02-04 23:05:44 +00005387 val = *((int *) extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388
5389 if (orinoco_lock(priv, &flags) != 0)
5390 return -EBUSY;
5391
Christoph Hellwig620554e2005-06-19 01:27:33 +02005392 if (val)
5393 priv->preamble = 1;
5394 else
5395 priv->preamble = 0;
5396
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 orinoco_unlock(priv, &flags);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005398
5399 return -EINPROGRESS; /* Call commit handler */
5400}
5401
5402static int orinoco_ioctl_getpreamble(struct net_device *dev,
5403 struct iw_request_info *info,
5404 void *wrqu,
5405 char *extra)
5406{
5407 struct orinoco_private *priv = netdev_priv(dev);
5408 int *val = (int *) extra;
5409
David Kilroya94e8422009-02-04 23:05:44 +00005410 if (!priv->has_preamble)
Christoph Hellwig620554e2005-06-19 01:27:33 +02005411 return -EOPNOTSUPP;
5412
5413 *val = priv->preamble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 return 0;
5415}
5416
Christoph Hellwig620554e2005-06-19 01:27:33 +02005417/* ioctl interface to hermes_read_ltv()
5418 * To use with iwpriv, pass the RID as the token argument, e.g.
5419 * iwpriv get_rid [0xfc00]
5420 * At least Wireless Tools 25 is required to use iwpriv.
5421 * For Wireless Tools 25 and 26 append "dummy" are the end. */
5422static int orinoco_ioctl_getrid(struct net_device *dev,
5423 struct iw_request_info *info,
5424 struct iw_point *data,
5425 char *extra)
5426{
5427 struct orinoco_private *priv = netdev_priv(dev);
5428 hermes_t *hw = &priv->hw;
5429 int rid = data->flags;
5430 u16 length;
5431 int err;
5432 unsigned long flags;
5433
5434 /* It's a "get" function, but we don't want users to access the
5435 * WEP key and other raw firmware data */
David Kilroya94e8422009-02-04 23:05:44 +00005436 if (!capable(CAP_NET_ADMIN))
Christoph Hellwig620554e2005-06-19 01:27:33 +02005437 return -EPERM;
5438
5439 if (rid < 0xfc00 || rid > 0xffff)
5440 return -EINVAL;
5441
5442 if (orinoco_lock(priv, &flags) != 0)
5443 return -EBUSY;
5444
5445 err = hermes_read_ltv(hw, USER_BAP, rid, MAX_RID_LEN, &length,
5446 extra);
5447 if (err)
5448 goto out;
5449
5450 data->length = min_t(u16, HERMES_RECLEN_TO_BYTES(length),
5451 MAX_RID_LEN);
5452
5453 out:
5454 orinoco_unlock(priv, &flags);
5455 return err;
5456}
5457
David Kilroy17a1a882008-08-21 23:27:47 +01005458/* Trigger a scan (look for other cells in the vicinity) */
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005459static int orinoco_ioctl_setscan(struct net_device *dev,
5460 struct iw_request_info *info,
David Kilroy9930cce2008-09-13 12:22:05 +01005461 struct iw_point *srq,
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005462 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463{
5464 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005465 hermes_t *hw = &priv->hw;
David Kilroy0753bba2008-08-21 23:27:46 +01005466 struct iw_scan_req *si = (struct iw_scan_req *) extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 unsigned long flags;
5469
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005470 /* Note : you may have realised that, as this is a SET operation,
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -08005471 * this is privileged and therefore a normal user can't
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005472 * perform scanning.
5473 * This is not an error, while the device perform scanning,
5474 * traffic doesn't flow, so it's a perfect DoS...
5475 * Jean II */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005477 if (orinoco_lock(priv, &flags) != 0)
5478 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005480 /* Scanning with port 0 disabled would fail */
5481 if (!netif_running(dev)) {
5482 err = -ENETDOWN;
5483 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005486 /* In monitor mode, the scan results are always empty.
5487 * Probe responses are passed to the driver as received
5488 * frames and could be processed in software. */
5489 if (priv->iw_mode == IW_MODE_MONITOR) {
5490 err = -EOPNOTSUPP;
5491 goto out;
5492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005494 /* Note : because we don't lock out the irq handler, the way
5495 * we access scan variables in priv is critical.
5496 * o scan_inprogress : not touched by irq handler
5497 * o scan_mode : not touched by irq handler
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005498 * Before modifying anything on those variables, please think hard !
5499 * Jean II */
5500
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005501 /* Save flags */
5502 priv->scan_mode = srq->flags;
5503
5504 /* Always trigger scanning, even if it's in progress.
5505 * This way, if the info frame get lost, we will recover somewhat
5506 * gracefully - Jean II */
5507
5508 if (priv->has_hostscan) {
5509 switch (priv->firmware_type) {
5510 case FIRMWARE_TYPE_SYMBOL:
5511 err = hermes_write_wordrec(hw, USER_BAP,
David Kilroyb2f30a02009-02-04 23:05:46 +00005512 HERMES_RID_CNFHOSTSCAN_SYMBOL,
5513 HERMES_HOSTSCAN_SYMBOL_ONCE |
5514 HERMES_HOSTSCAN_SYMBOL_BCAST);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005515 break;
5516 case FIRMWARE_TYPE_INTERSIL: {
Pavel Roskind133ae42005-09-23 04:18:06 -04005517 __le16 req[3];
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005518
5519 req[0] = cpu_to_le16(0x3fff); /* All channels */
5520 req[1] = cpu_to_le16(0x0001); /* rate 1 Mbps */
5521 req[2] = 0; /* Any ESSID */
5522 err = HERMES_WRITE_RECORD(hw, USER_BAP,
5523 HERMES_RID_CNFHOSTSCAN, &req);
5524 }
5525 break;
5526 case FIRMWARE_TYPE_AGERE:
David Kilroy0753bba2008-08-21 23:27:46 +01005527 if (priv->scan_mode & IW_SCAN_THIS_ESSID) {
5528 struct hermes_idstring idbuf;
5529 size_t len = min(sizeof(idbuf.val),
5530 (size_t) si->essid_len);
5531 idbuf.len = cpu_to_le16(len);
5532 memcpy(idbuf.val, si->essid, len);
5533
5534 err = hermes_write_ltv(hw, USER_BAP,
5535 HERMES_RID_CNFSCANSSID_AGERE,
5536 HERMES_BYTES_TO_RECLEN(len + 2),
5537 &idbuf);
5538 } else
5539 err = hermes_write_wordrec(hw, USER_BAP,
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005540 HERMES_RID_CNFSCANSSID_AGERE,
5541 0); /* Any ESSID */
5542 if (err)
5543 break;
5544
David Kilroy01632fa2008-08-21 23:27:58 +01005545 if (priv->has_ext_scan) {
5546 /* Clear scan results at the start of
5547 * an extended scan */
5548 orinoco_clear_scan_results(priv,
5549 msecs_to_jiffies(15000));
5550
5551 /* TODO: Is this available on older firmware?
5552 * Can we use it to scan specific channels
5553 * for IW_SCAN_THIS_FREQ? */
5554 err = hermes_write_wordrec(hw, USER_BAP,
5555 HERMES_RID_CNFSCANCHANNELS2GHZ,
5556 0x7FFF);
5557 if (err)
5558 goto out;
5559
5560 err = hermes_inquire(hw,
5561 HERMES_INQ_CHANNELINFO);
5562 } else
5563 err = hermes_inquire(hw, HERMES_INQ_SCAN);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005564 break;
5565 }
5566 } else
5567 err = hermes_inquire(hw, HERMES_INQ_SCAN);
5568
5569 /* One more client */
David Kilroya94e8422009-02-04 23:05:44 +00005570 if (!err)
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005571 priv->scan_inprogress = 1;
5572
5573 out:
5574 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 return err;
5576}
5577
Dan Williams1e3428e2007-10-10 23:56:25 -04005578#define MAX_CUSTOM_LEN 64
5579
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005580/* Translate scan data returned from the card to a card independant
David Kilroy17a1a882008-08-21 23:27:47 +01005581 * format that the Wireless Tools will understand - Jean II */
Dan Williams1e3428e2007-10-10 23:56:25 -04005582static inline char *orinoco_translate_scan(struct net_device *dev,
David S. Millerccc58052008-06-16 18:50:49 -07005583 struct iw_request_info *info,
Dan Williams1e3428e2007-10-10 23:56:25 -04005584 char *current_ev,
5585 char *end_buf,
5586 union hermes_scan_info *bss,
Pavel Roskindfe1baf2008-11-10 09:25:53 -05005587 unsigned long last_scanned)
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005588{
5589 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005590 u16 capabilities;
5591 u16 channel;
5592 struct iw_event iwe; /* Temporary buffer */
Dan Williams1e3428e2007-10-10 23:56:25 -04005593 char custom[MAX_CUSTOM_LEN];
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005594
David Kilroy17a1a882008-08-21 23:27:47 +01005595 memset(&iwe, 0, sizeof(iwe));
5596
Dan Williams1e3428e2007-10-10 23:56:25 -04005597 /* First entry *MUST* be the AP MAC address */
5598 iwe.cmd = SIOCGIWAP;
5599 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
5600 memcpy(iwe.u.ap_addr.sa_data, bss->a.bssid, ETH_ALEN);
David S. Millerccc58052008-06-16 18:50:49 -07005601 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5602 &iwe, IW_EV_ADDR_LEN);
Dan Williams1e3428e2007-10-10 23:56:25 -04005603
5604 /* Other entries will be displayed in the order we give them */
5605
5606 /* Add the ESSID */
5607 iwe.u.data.length = le16_to_cpu(bss->a.essid_len);
5608 if (iwe.u.data.length > 32)
5609 iwe.u.data.length = 32;
5610 iwe.cmd = SIOCGIWESSID;
5611 iwe.u.data.flags = 1;
David S. Millerccc58052008-06-16 18:50:49 -07005612 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5613 &iwe, bss->a.essid);
Dan Williams1e3428e2007-10-10 23:56:25 -04005614
5615 /* Add mode */
5616 iwe.cmd = SIOCGIWMODE;
5617 capabilities = le16_to_cpu(bss->a.capabilities);
David Kilroy17a1a882008-08-21 23:27:47 +01005618 if (capabilities & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
5619 if (capabilities & WLAN_CAPABILITY_ESS)
Dan Williams1e3428e2007-10-10 23:56:25 -04005620 iwe.u.mode = IW_MODE_MASTER;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005621 else
Dan Williams1e3428e2007-10-10 23:56:25 -04005622 iwe.u.mode = IW_MODE_ADHOC;
David S. Millerccc58052008-06-16 18:50:49 -07005623 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5624 &iwe, IW_EV_UINT_LEN);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005625 }
5626
Dan Williams1e3428e2007-10-10 23:56:25 -04005627 channel = bss->s.channel;
5628 if ((channel >= 1) && (channel <= NUM_CHANNELS)) {
David Kilroy17a1a882008-08-21 23:27:47 +01005629 /* Add channel and frequency */
Dan Williams1e3428e2007-10-10 23:56:25 -04005630 iwe.cmd = SIOCGIWFREQ;
David Kilroy17a1a882008-08-21 23:27:47 +01005631 iwe.u.freq.m = channel;
5632 iwe.u.freq.e = 0;
5633 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5634 &iwe, IW_EV_FREQ_LEN);
5635
David Kilroy9ee677c2008-12-23 14:03:38 +00005636 iwe.u.freq.m = ieee80211_dsss_chan_to_freq(channel) * 100000;
Dan Williams1e3428e2007-10-10 23:56:25 -04005637 iwe.u.freq.e = 1;
David S. Millerccc58052008-06-16 18:50:49 -07005638 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
Dan Williams1e3428e2007-10-10 23:56:25 -04005639 &iwe, IW_EV_FREQ_LEN);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005640 }
5641
David Kilroy17a1a882008-08-21 23:27:47 +01005642 /* Add quality statistics. level and noise in dB. No link quality */
Dan Williams1e3428e2007-10-10 23:56:25 -04005643 iwe.cmd = IWEVQUAL;
David Kilroy17a1a882008-08-21 23:27:47 +01005644 iwe.u.qual.updated = IW_QUAL_DBM | IW_QUAL_QUAL_INVALID;
Dan Williams1e3428e2007-10-10 23:56:25 -04005645 iwe.u.qual.level = (__u8) le16_to_cpu(bss->a.level) - 0x95;
5646 iwe.u.qual.noise = (__u8) le16_to_cpu(bss->a.noise) - 0x95;
5647 /* Wireless tools prior to 27.pre22 will show link quality
5648 * anyway, so we provide a reasonable value. */
5649 if (iwe.u.qual.level > iwe.u.qual.noise)
5650 iwe.u.qual.qual = iwe.u.qual.level - iwe.u.qual.noise;
5651 else
5652 iwe.u.qual.qual = 0;
David S. Millerccc58052008-06-16 18:50:49 -07005653 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5654 &iwe, IW_EV_QUAL_LEN);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005655
Dan Williams1e3428e2007-10-10 23:56:25 -04005656 /* Add encryption capability */
5657 iwe.cmd = SIOCGIWENCODE;
David Kilroy17a1a882008-08-21 23:27:47 +01005658 if (capabilities & WLAN_CAPABILITY_PRIVACY)
Dan Williams1e3428e2007-10-10 23:56:25 -04005659 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
5660 else
5661 iwe.u.data.flags = IW_ENCODE_DISABLED;
5662 iwe.u.data.length = 0;
David S. Millerccc58052008-06-16 18:50:49 -07005663 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
David Kilroy17a1a882008-08-21 23:27:47 +01005664 &iwe, NULL);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005665
Dan Williams1e3428e2007-10-10 23:56:25 -04005666 /* Bit rate is not available in Lucent/Agere firmwares */
5667 if (priv->firmware_type != FIRMWARE_TYPE_AGERE) {
David S. Millerccc58052008-06-16 18:50:49 -07005668 char *current_val = current_ev + iwe_stream_lcp_len(info);
Dan Williams1e3428e2007-10-10 23:56:25 -04005669 int i;
5670 int step;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005671
Dan Williams1e3428e2007-10-10 23:56:25 -04005672 if (priv->firmware_type == FIRMWARE_TYPE_SYMBOL)
5673 step = 2;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005674 else
Dan Williams1e3428e2007-10-10 23:56:25 -04005675 step = 1;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005676
Dan Williams1e3428e2007-10-10 23:56:25 -04005677 iwe.cmd = SIOCGIWRATE;
5678 /* Those two flags are ignored... */
5679 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
5680 /* Max 10 values */
5681 for (i = 0; i < 10; i += step) {
5682 /* NULL terminated */
5683 if (bss->p.rates[i] == 0x0)
5684 break;
5685 /* Bit rate given in 500 kb/s units (+ 0x80) */
David Kilroy17a1a882008-08-21 23:27:47 +01005686 iwe.u.bitrate.value =
5687 ((bss->p.rates[i] & 0x7f) * 500000);
David S. Millerccc58052008-06-16 18:50:49 -07005688 current_val = iwe_stream_add_value(info, current_ev,
5689 current_val,
Dan Williams1e3428e2007-10-10 23:56:25 -04005690 end_buf, &iwe,
5691 IW_EV_PARAM_LEN);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005692 }
Dan Williams1e3428e2007-10-10 23:56:25 -04005693 /* Check if we added any event */
David S. Millerccc58052008-06-16 18:50:49 -07005694 if ((current_val - current_ev) > iwe_stream_lcp_len(info))
Dan Williams1e3428e2007-10-10 23:56:25 -04005695 current_ev = current_val;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005696 }
Dan Williams1e3428e2007-10-10 23:56:25 -04005697
David Kilroy17a1a882008-08-21 23:27:47 +01005698 /* Beacon interval */
5699 iwe.cmd = IWEVCUSTOM;
5700 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5701 "bcn_int=%d",
5702 le16_to_cpu(bss->a.beacon_interv));
5703 if (iwe.u.data.length)
5704 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5705 &iwe, custom);
5706
5707 /* Capabilites */
5708 iwe.cmd = IWEVCUSTOM;
5709 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5710 "capab=0x%04x",
5711 capabilities);
5712 if (iwe.u.data.length)
5713 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5714 &iwe, custom);
5715
5716 /* Add EXTRA: Age to display seconds since last beacon/probe response
5717 * for given network. */
5718 iwe.cmd = IWEVCUSTOM;
5719 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5720 " Last beacon: %dms ago",
5721 jiffies_to_msecs(jiffies - last_scanned));
5722 if (iwe.u.data.length)
5723 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5724 &iwe, custom);
5725
Dan Williams1e3428e2007-10-10 23:56:25 -04005726 return current_ev;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005727}
5728
David Kilroy01632fa2008-08-21 23:27:58 +01005729static inline char *orinoco_translate_ext_scan(struct net_device *dev,
5730 struct iw_request_info *info,
5731 char *current_ev,
5732 char *end_buf,
5733 struct agere_ext_scan_info *bss,
Pavel Roskindfe1baf2008-11-10 09:25:53 -05005734 unsigned long last_scanned)
David Kilroy01632fa2008-08-21 23:27:58 +01005735{
5736 u16 capabilities;
5737 u16 channel;
5738 struct iw_event iwe; /* Temporary buffer */
5739 char custom[MAX_CUSTOM_LEN];
5740 u8 *ie;
5741
5742 memset(&iwe, 0, sizeof(iwe));
5743
5744 /* First entry *MUST* be the AP MAC address */
5745 iwe.cmd = SIOCGIWAP;
5746 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
5747 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
5748 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5749 &iwe, IW_EV_ADDR_LEN);
5750
5751 /* Other entries will be displayed in the order we give them */
5752
5753 /* Add the ESSID */
5754 ie = bss->data;
5755 iwe.u.data.length = ie[1];
5756 if (iwe.u.data.length) {
5757 if (iwe.u.data.length > 32)
5758 iwe.u.data.length = 32;
5759 iwe.cmd = SIOCGIWESSID;
5760 iwe.u.data.flags = 1;
5761 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5762 &iwe, &ie[2]);
5763 }
5764
5765 /* Add mode */
5766 capabilities = le16_to_cpu(bss->capabilities);
5767 if (capabilities & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
5768 iwe.cmd = SIOCGIWMODE;
5769 if (capabilities & WLAN_CAPABILITY_ESS)
5770 iwe.u.mode = IW_MODE_MASTER;
5771 else
5772 iwe.u.mode = IW_MODE_ADHOC;
5773 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5774 &iwe, IW_EV_UINT_LEN);
5775 }
5776
Johannes Berg2c7060022008-10-30 22:09:54 +01005777 ie = orinoco_get_ie(bss->data, sizeof(bss->data), WLAN_EID_DS_PARAMS);
David Kilroy01632fa2008-08-21 23:27:58 +01005778 channel = ie ? ie[2] : 0;
5779 if ((channel >= 1) && (channel <= NUM_CHANNELS)) {
5780 /* Add channel and frequency */
5781 iwe.cmd = SIOCGIWFREQ;
5782 iwe.u.freq.m = channel;
5783 iwe.u.freq.e = 0;
5784 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5785 &iwe, IW_EV_FREQ_LEN);
5786
David Kilroy9ee677c2008-12-23 14:03:38 +00005787 iwe.u.freq.m = ieee80211_dsss_chan_to_freq(channel) * 100000;
David Kilroy01632fa2008-08-21 23:27:58 +01005788 iwe.u.freq.e = 1;
5789 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5790 &iwe, IW_EV_FREQ_LEN);
5791 }
5792
5793 /* Add quality statistics. level and noise in dB. No link quality */
5794 iwe.cmd = IWEVQUAL;
5795 iwe.u.qual.updated = IW_QUAL_DBM | IW_QUAL_QUAL_INVALID;
5796 iwe.u.qual.level = bss->level - 0x95;
5797 iwe.u.qual.noise = bss->noise - 0x95;
5798 /* Wireless tools prior to 27.pre22 will show link quality
5799 * anyway, so we provide a reasonable value. */
5800 if (iwe.u.qual.level > iwe.u.qual.noise)
5801 iwe.u.qual.qual = iwe.u.qual.level - iwe.u.qual.noise;
5802 else
5803 iwe.u.qual.qual = 0;
5804 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5805 &iwe, IW_EV_QUAL_LEN);
5806
5807 /* Add encryption capability */
5808 iwe.cmd = SIOCGIWENCODE;
5809 if (capabilities & WLAN_CAPABILITY_PRIVACY)
5810 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
5811 else
5812 iwe.u.data.flags = IW_ENCODE_DISABLED;
5813 iwe.u.data.length = 0;
5814 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5815 &iwe, NULL);
5816
5817 /* WPA IE */
5818 ie = orinoco_get_wpa_ie(bss->data, sizeof(bss->data));
5819 if (ie) {
5820 iwe.cmd = IWEVGENIE;
5821 iwe.u.data.length = ie[1] + 2;
5822 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5823 &iwe, ie);
5824 }
5825
5826 /* RSN IE */
Johannes Berg2c7060022008-10-30 22:09:54 +01005827 ie = orinoco_get_ie(bss->data, sizeof(bss->data), WLAN_EID_RSN);
David Kilroy01632fa2008-08-21 23:27:58 +01005828 if (ie) {
5829 iwe.cmd = IWEVGENIE;
5830 iwe.u.data.length = ie[1] + 2;
5831 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5832 &iwe, ie);
5833 }
5834
Johannes Berg2c7060022008-10-30 22:09:54 +01005835 ie = orinoco_get_ie(bss->data, sizeof(bss->data), WLAN_EID_SUPP_RATES);
David Kilroy01632fa2008-08-21 23:27:58 +01005836 if (ie) {
5837 char *p = current_ev + iwe_stream_lcp_len(info);
5838 int i;
5839
5840 iwe.cmd = SIOCGIWRATE;
5841 /* Those two flags are ignored... */
5842 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
5843
5844 for (i = 2; i < (ie[1] + 2); i++) {
5845 iwe.u.bitrate.value = ((ie[i] & 0x7F) * 500000);
5846 p = iwe_stream_add_value(info, current_ev, p, end_buf,
5847 &iwe, IW_EV_PARAM_LEN);
5848 }
5849 /* Check if we added any event */
5850 if (p > (current_ev + iwe_stream_lcp_len(info)))
5851 current_ev = p;
5852 }
5853
5854 /* Timestamp */
5855 iwe.cmd = IWEVCUSTOM;
David Kilroy75d31cf2008-09-12 22:28:18 +01005856 iwe.u.data.length =
5857 snprintf(custom, MAX_CUSTOM_LEN, "tsf=%016llx",
5858 (unsigned long long) le64_to_cpu(bss->timestamp));
David Kilroy01632fa2008-08-21 23:27:58 +01005859 if (iwe.u.data.length)
5860 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5861 &iwe, custom);
5862
5863 /* Beacon interval */
5864 iwe.cmd = IWEVCUSTOM;
5865 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5866 "bcn_int=%d",
5867 le16_to_cpu(bss->beacon_interval));
5868 if (iwe.u.data.length)
5869 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5870 &iwe, custom);
5871
5872 /* Capabilites */
5873 iwe.cmd = IWEVCUSTOM;
5874 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5875 "capab=0x%04x",
5876 capabilities);
5877 if (iwe.u.data.length)
5878 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5879 &iwe, custom);
5880
5881 /* Add EXTRA: Age to display seconds since last beacon/probe response
5882 * for given network. */
5883 iwe.cmd = IWEVCUSTOM;
5884 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5885 " Last beacon: %dms ago",
5886 jiffies_to_msecs(jiffies - last_scanned));
5887 if (iwe.u.data.length)
5888 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5889 &iwe, custom);
5890
5891 return current_ev;
5892}
5893
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005894/* Return results of a scan */
5895static int orinoco_ioctl_getscan(struct net_device *dev,
5896 struct iw_request_info *info,
5897 struct iw_point *srq,
5898 char *extra)
5899{
5900 struct orinoco_private *priv = netdev_priv(dev);
5901 int err = 0;
5902 unsigned long flags;
Dan Williams1e3428e2007-10-10 23:56:25 -04005903 char *current_ev = extra;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005904
5905 if (orinoco_lock(priv, &flags) != 0)
5906 return -EBUSY;
5907
Dan Williams1e3428e2007-10-10 23:56:25 -04005908 if (priv->scan_inprogress) {
5909 /* Important note : we don't want to block the caller
5910 * until results are ready for various reasons.
5911 * First, managing wait queues is complex and racy.
5912 * Second, we grab some rtnetlink lock before comming
5913 * here (in dev_ioctl()).
5914 * Third, we generate an Wireless Event, so the
5915 * caller can wait itself on that - Jean II */
5916 err = -EAGAIN;
5917 goto out;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005918 }
Dan Williams1e3428e2007-10-10 23:56:25 -04005919
David Kilroy01632fa2008-08-21 23:27:58 +01005920 if (priv->has_ext_scan) {
5921 struct xbss_element *bss;
Dan Williams1e3428e2007-10-10 23:56:25 -04005922
David Kilroy01632fa2008-08-21 23:27:58 +01005923 list_for_each_entry(bss, &priv->bss_list, list) {
5924 /* Translate this entry to WE format */
5925 current_ev =
5926 orinoco_translate_ext_scan(dev, info,
5927 current_ev,
5928 extra + srq->length,
5929 &bss->bss,
5930 bss->last_scanned);
5931
5932 /* Check if there is space for one more entry */
5933 if ((extra + srq->length - current_ev)
5934 <= IW_EV_ADDR_LEN) {
5935 /* Ask user space to try again with a
5936 * bigger buffer */
5937 err = -E2BIG;
5938 goto out;
5939 }
5940 }
5941
5942 } else {
5943 struct bss_element *bss;
5944
5945 list_for_each_entry(bss, &priv->bss_list, list) {
5946 /* Translate this entry to WE format */
5947 current_ev = orinoco_translate_scan(dev, info,
5948 current_ev,
5949 extra + srq->length,
5950 &bss->bss,
5951 bss->last_scanned);
5952
5953 /* Check if there is space for one more entry */
5954 if ((extra + srq->length - current_ev)
5955 <= IW_EV_ADDR_LEN) {
5956 /* Ask user space to try again with a
5957 * bigger buffer */
5958 err = -E2BIG;
5959 goto out;
5960 }
Dan Williams1e3428e2007-10-10 23:56:25 -04005961 }
5962 }
5963
5964 srq->length = (current_ev - extra);
5965 srq->flags = (__u16) priv->scan_mode;
5966
5967out:
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005968 orinoco_unlock(priv, &flags);
5969 return err;
5970}
5971
Christoph Hellwig620554e2005-06-19 01:27:33 +02005972/* Commit handler, called after set operations */
5973static int orinoco_ioctl_commit(struct net_device *dev,
5974 struct iw_request_info *info,
5975 void *wrqu,
5976 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977{
5978 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005979 struct hermes *hw = &priv->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 unsigned long flags;
Christoph Hellwig620554e2005-06-19 01:27:33 +02005981 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982
Christoph Hellwig620554e2005-06-19 01:27:33 +02005983 if (!priv->open)
5984 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985
Christoph Hellwig620554e2005-06-19 01:27:33 +02005986 if (priv->broken_disableport) {
David Howellsc4028952006-11-22 14:57:56 +00005987 orinoco_reset(&priv->reset_work);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005988 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990
Christoph Hellwig620554e2005-06-19 01:27:33 +02005991 if (orinoco_lock(priv, &flags) != 0)
5992 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993
Christoph Hellwig620554e2005-06-19 01:27:33 +02005994 err = hermes_disable_port(hw, 0);
5995 if (err) {
5996 printk(KERN_WARNING "%s: Unable to disable port "
5997 "while reconfiguring card\n", dev->name);
5998 priv->broken_disableport = 1;
5999 goto out;
6000 }
6001
6002 err = __orinoco_program_rids(dev);
6003 if (err) {
6004 printk(KERN_WARNING "%s: Unable to reconfigure card\n",
6005 dev->name);
6006 goto out;
6007 }
6008
6009 err = hermes_enable_port(hw, 0);
6010 if (err) {
6011 printk(KERN_WARNING "%s: Unable to enable port while reconfiguring card\n",
6012 dev->name);
6013 goto out;
6014 }
6015
6016 out:
6017 if (err) {
6018 printk(KERN_WARNING "%s: Resetting instead...\n", dev->name);
6019 schedule_work(&priv->reset_work);
6020 err = 0;
6021 }
6022
6023 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 return err;
6025}
6026
Christoph Hellwig620554e2005-06-19 01:27:33 +02006027static const struct iw_priv_args orinoco_privtab[] = {
6028 { SIOCIWFIRSTPRIV + 0x0, 0, 0, "force_reset" },
6029 { SIOCIWFIRSTPRIV + 0x1, 0, 0, "card_reset" },
6030 { SIOCIWFIRSTPRIV + 0x2, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
6031 0, "set_port3" },
6032 { SIOCIWFIRSTPRIV + 0x3, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
6033 "get_port3" },
6034 { SIOCIWFIRSTPRIV + 0x4, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
6035 0, "set_preamble" },
6036 { SIOCIWFIRSTPRIV + 0x5, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
6037 "get_preamble" },
6038 { SIOCIWFIRSTPRIV + 0x6, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
6039 0, "set_ibssport" },
6040 { SIOCIWFIRSTPRIV + 0x7, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
6041 "get_ibssport" },
6042 { SIOCIWFIRSTPRIV + 0x9, 0, IW_PRIV_TYPE_BYTE | MAX_RID_LEN,
6043 "get_rid" },
6044};
6045
6046
6047/*
6048 * Structures to export the Wireless Handlers
6049 */
6050
David Kilroy409644a2008-08-21 23:28:01 +01006051#define STD_IW_HANDLER(id, func) \
6052 [IW_IOCTL_IDX(id)] = (iw_handler) func
Christoph Hellwig620554e2005-06-19 01:27:33 +02006053static const iw_handler orinoco_handler[] = {
David Kilroy409644a2008-08-21 23:28:01 +01006054 STD_IW_HANDLER(SIOCSIWCOMMIT, orinoco_ioctl_commit),
6055 STD_IW_HANDLER(SIOCGIWNAME, orinoco_ioctl_getname),
6056 STD_IW_HANDLER(SIOCSIWFREQ, orinoco_ioctl_setfreq),
6057 STD_IW_HANDLER(SIOCGIWFREQ, orinoco_ioctl_getfreq),
6058 STD_IW_HANDLER(SIOCSIWMODE, orinoco_ioctl_setmode),
6059 STD_IW_HANDLER(SIOCGIWMODE, orinoco_ioctl_getmode),
6060 STD_IW_HANDLER(SIOCSIWSENS, orinoco_ioctl_setsens),
6061 STD_IW_HANDLER(SIOCGIWSENS, orinoco_ioctl_getsens),
6062 STD_IW_HANDLER(SIOCGIWRANGE, orinoco_ioctl_getiwrange),
6063 STD_IW_HANDLER(SIOCSIWSPY, iw_handler_set_spy),
6064 STD_IW_HANDLER(SIOCGIWSPY, iw_handler_get_spy),
6065 STD_IW_HANDLER(SIOCSIWTHRSPY, iw_handler_set_thrspy),
6066 STD_IW_HANDLER(SIOCGIWTHRSPY, iw_handler_get_thrspy),
6067 STD_IW_HANDLER(SIOCSIWAP, orinoco_ioctl_setwap),
6068 STD_IW_HANDLER(SIOCGIWAP, orinoco_ioctl_getwap),
6069 STD_IW_HANDLER(SIOCSIWSCAN, orinoco_ioctl_setscan),
6070 STD_IW_HANDLER(SIOCGIWSCAN, orinoco_ioctl_getscan),
6071 STD_IW_HANDLER(SIOCSIWESSID, orinoco_ioctl_setessid),
6072 STD_IW_HANDLER(SIOCGIWESSID, orinoco_ioctl_getessid),
6073 STD_IW_HANDLER(SIOCSIWNICKN, orinoco_ioctl_setnick),
6074 STD_IW_HANDLER(SIOCGIWNICKN, orinoco_ioctl_getnick),
6075 STD_IW_HANDLER(SIOCSIWRATE, orinoco_ioctl_setrate),
6076 STD_IW_HANDLER(SIOCGIWRATE, orinoco_ioctl_getrate),
6077 STD_IW_HANDLER(SIOCSIWRTS, orinoco_ioctl_setrts),
6078 STD_IW_HANDLER(SIOCGIWRTS, orinoco_ioctl_getrts),
6079 STD_IW_HANDLER(SIOCSIWFRAG, orinoco_ioctl_setfrag),
6080 STD_IW_HANDLER(SIOCGIWFRAG, orinoco_ioctl_getfrag),
6081 STD_IW_HANDLER(SIOCGIWRETRY, orinoco_ioctl_getretry),
6082 STD_IW_HANDLER(SIOCSIWENCODE, orinoco_ioctl_setiwencode),
6083 STD_IW_HANDLER(SIOCGIWENCODE, orinoco_ioctl_getiwencode),
6084 STD_IW_HANDLER(SIOCSIWPOWER, orinoco_ioctl_setpower),
6085 STD_IW_HANDLER(SIOCGIWPOWER, orinoco_ioctl_getpower),
David Kilroyd03032a2008-08-21 23:28:02 +01006086 STD_IW_HANDLER(SIOCSIWGENIE, orinoco_ioctl_set_genie),
6087 STD_IW_HANDLER(SIOCGIWGENIE, orinoco_ioctl_get_genie),
6088 STD_IW_HANDLER(SIOCSIWMLME, orinoco_ioctl_set_mlme),
6089 STD_IW_HANDLER(SIOCSIWAUTH, orinoco_ioctl_set_auth),
6090 STD_IW_HANDLER(SIOCGIWAUTH, orinoco_ioctl_get_auth),
6091 STD_IW_HANDLER(SIOCSIWENCODEEXT, orinoco_ioctl_set_encodeext),
6092 STD_IW_HANDLER(SIOCGIWENCODEEXT, orinoco_ioctl_get_encodeext),
Christoph Hellwig620554e2005-06-19 01:27:33 +02006093};
6094
6095
6096/*
6097 Added typecasting since we no longer use iwreq_data -- Moustafa
6098 */
6099static const iw_handler orinoco_private_handler[] = {
Peter Hagervall6b9b97c2005-07-27 01:14:46 -07006100 [0] = (iw_handler) orinoco_ioctl_reset,
6101 [1] = (iw_handler) orinoco_ioctl_reset,
6102 [2] = (iw_handler) orinoco_ioctl_setport3,
6103 [3] = (iw_handler) orinoco_ioctl_getport3,
6104 [4] = (iw_handler) orinoco_ioctl_setpreamble,
6105 [5] = (iw_handler) orinoco_ioctl_getpreamble,
6106 [6] = (iw_handler) orinoco_ioctl_setibssport,
6107 [7] = (iw_handler) orinoco_ioctl_getibssport,
6108 [9] = (iw_handler) orinoco_ioctl_getrid,
Christoph Hellwig620554e2005-06-19 01:27:33 +02006109};
6110
6111static const struct iw_handler_def orinoco_handler_def = {
6112 .num_standard = ARRAY_SIZE(orinoco_handler),
6113 .num_private = ARRAY_SIZE(orinoco_private_handler),
6114 .num_private_args = ARRAY_SIZE(orinoco_privtab),
6115 .standard = orinoco_handler,
6116 .private = orinoco_private_handler,
6117 .private_args = orinoco_privtab,
Pavel Roskin343c6862005-09-09 18:43:02 -04006118 .get_wireless_stats = orinoco_get_wireless_stats,
Christoph Hellwig620554e2005-06-19 01:27:33 +02006119};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02006121static void orinoco_get_drvinfo(struct net_device *dev,
6122 struct ethtool_drvinfo *info)
6123{
6124 struct orinoco_private *priv = netdev_priv(dev);
6125
6126 strncpy(info->driver, DRIVER_NAME, sizeof(info->driver) - 1);
6127 strncpy(info->version, DRIVER_VERSION, sizeof(info->version) - 1);
6128 strncpy(info->fw_version, priv->fw_name, sizeof(info->fw_version) - 1);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07006129 if (dev->dev.parent)
Kay Sieversfb28ad32008-11-10 13:55:14 -08006130 strncpy(info->bus_info, dev_name(dev->dev.parent),
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02006131 sizeof(info->bus_info) - 1);
6132 else
6133 snprintf(info->bus_info, sizeof(info->bus_info) - 1,
6134 "PCMCIA %p", priv->hw.iobase);
6135}
6136
Jeff Garzik7282d492006-09-13 14:30:00 -04006137static const struct ethtool_ops orinoco_ethtool_ops = {
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02006138 .get_drvinfo = orinoco_get_drvinfo,
6139 .get_link = ethtool_op_get_link,
6140};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141
6142/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143/* Module initialization */
6144/********************************************************************/
6145
6146EXPORT_SYMBOL(alloc_orinocodev);
6147EXPORT_SYMBOL(free_orinocodev);
6148
6149EXPORT_SYMBOL(__orinoco_up);
6150EXPORT_SYMBOL(__orinoco_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151EXPORT_SYMBOL(orinoco_reinit_firmware);
6152
6153EXPORT_SYMBOL(orinoco_interrupt);
6154
6155/* Can't be declared "const" or the whole __initdata section will
6156 * become const */
6157static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
6158 " (David Gibson <hermes@gibson.dropbear.id.au>, "
6159 "Pavel Roskin <proski@gnu.org>, et al)";
6160
6161static int __init init_orinoco(void)
6162{
6163 printk(KERN_DEBUG "%s\n", version);
6164 return 0;
6165}
6166
6167static void __exit exit_orinoco(void)
6168{
6169}
6170
6171module_init(init_orinoco);
6172module_exit(exit_orinoco);