blob: 5e6f90ba9088c75953d58fea09450c0fdc1b3dc0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* orinoco.c - (formerly known as dldwd_cs.c and orinoco_cs.c)
2 *
3 * A driver for Hermes or Prism 2 chipset based PCMCIA wireless
4 * adaptors, with Lucent/Agere, Intersil or Symbol firmware.
5 *
6 * Current maintainers (as of 29 September 2003) are:
7 * Pavel Roskin <proski AT gnu.org>
8 * and David Gibson <hermes AT gibson.dropbear.id.au>
9 *
10 * (C) Copyright David Gibson, IBM Corporation 2001-2003.
11 * Copyright (C) 2000 David Gibson, Linuxcare Australia.
12 * With some help from :
13 * Copyright (C) 2001 Jean Tourrilhes, HP Labs
14 * Copyright (C) 2001 Benjamin Herrenschmidt
15 *
16 * Based on dummy_cs.c 1.27 2000/06/12 21:27:25
17 *
18 * Portions based on wvlan_cs.c 1.0.6, Copyright Andreas Neuhaus <andy
19 * AT fasta.fh-dortmund.de>
20 * http://www.stud.fh-dortmund.de/~andy/wvlan/
21 *
22 * The contents of this file are subject to the Mozilla Public License
23 * Version 1.1 (the "License"); you may not use this file except in
24 * compliance with the License. You may obtain a copy of the License
25 * at http://www.mozilla.org/MPL/
26 *
27 * Software distributed under the License is distributed on an "AS IS"
28 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
29 * the License for the specific language governing rights and
30 * limitations under the License.
31 *
32 * The initial developer of the original code is David A. Hinds
33 * <dahinds AT users.sourceforge.net>. Portions created by David
34 * A. Hinds are Copyright (C) 1999 David A. Hinds. All Rights
35 * Reserved.
36 *
37 * Alternatively, the contents of this file may be used under the
38 * terms of the GNU General Public License version 2 (the "GPL"), in
39 * which case the provisions of the GPL are applicable instead of the
40 * above. If you wish to allow the use of your version of this file
41 * only under the terms of the GPL and not to allow others to use your
42 * version of this file under the MPL, indicate your decision by
43 * deleting the provisions above and replace them with the notice and
44 * other provisions required by the GPL. If you do not delete the
45 * provisions above, a recipient may use your version of this file
46 * under either the MPL or the GPL. */
47
48/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * TODO
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * o Handle de-encapsulation within network layer, provide 802.11
51 * headers (patch from Thomas 'Dent' Mirlacher)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * o Fix possible races in SPY handling.
53 * o Disconnect wireless extensions from fundamental configuration.
54 * o (maybe) Software WEP support (patch from Stano Meduna).
55 * o (maybe) Use multiple Tx buffers - driver handling queue
56 * rather than firmware.
57 */
58
59/* Locking and synchronization:
60 *
61 * The basic principle is that everything is serialized through a
62 * single spinlock, priv->lock. The lock is used in user, bh and irq
63 * context, so when taken outside hardirq context it should always be
64 * taken with interrupts disabled. The lock protects both the
65 * hardware and the struct orinoco_private.
66 *
67 * Another flag, priv->hw_unavailable indicates that the hardware is
68 * unavailable for an extended period of time (e.g. suspended, or in
69 * the middle of a hard reset). This flag is protected by the
70 * spinlock. All code which touches the hardware should check the
71 * flag after taking the lock, and if it is set, give up on whatever
72 * they are doing and drop the lock again. The orinoco_lock()
73 * function handles this (it unlocks and returns -EBUSY if
74 * hw_unavailable is non-zero).
75 */
76
77#define DRIVER_NAME "orinoco"
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <linux/module.h>
80#include <linux/kernel.h>
81#include <linux/init.h>
David Kilroyd03032a2008-08-21 23:28:02 +010082#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include <linux/etherdevice.h>
Christoph Hellwig1fab2e82005-06-19 01:27:40 +020085#include <linux/ethtool.h>
David Kilroy3994d502008-08-21 23:27:54 +010086#include <linux/firmware.h>
Pavel Roskin9c974fb2006-08-15 20:45:03 -040087#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#include <linux/wireless.h>
Christoph Hellwig620554e2005-06-19 01:27:33 +020089#include <net/iw_handler.h>
Christoph Hellwig5d558b72005-06-19 01:27:28 +020090#include <net/ieee80211.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#include "hermes_rid.h"
David Kilroy3994d502008-08-21 23:27:54 +010093#include "hermes_dld.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#include "orinoco.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/********************************************************************/
97/* Module information */
98/********************************************************************/
99
100MODULE_AUTHOR("Pavel Roskin <proski@gnu.org> & David Gibson <hermes@gibson.dropbear.id.au>");
101MODULE_DESCRIPTION("Driver for Lucent Orinoco, Prism II based and similar wireless cards");
102MODULE_LICENSE("Dual MPL/GPL");
103
104/* Level of debugging. Used in the macros in orinoco.h */
105#ifdef ORINOCO_DEBUG
106int orinoco_debug = ORINOCO_DEBUG;
107module_param(orinoco_debug, int, 0644);
108MODULE_PARM_DESC(orinoco_debug, "Debug level");
109EXPORT_SYMBOL(orinoco_debug);
110#endif
111
112static int suppress_linkstatus; /* = 0 */
113module_param(suppress_linkstatus, bool, 0644);
114MODULE_PARM_DESC(suppress_linkstatus, "Don't log link status changes");
David Gibson7bb7c3a2005-05-12 20:02:10 -0400115static int ignore_disconnect; /* = 0 */
116module_param(ignore_disconnect, int, 0644);
117MODULE_PARM_DESC(ignore_disconnect, "Don't report lost link to the network layer");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200119static int force_monitor; /* = 0 */
120module_param(force_monitor, int, 0644);
121MODULE_PARM_DESC(force_monitor, "Allow monitor mode for all firmware versions");
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/********************************************************************/
124/* Compile time configuration and compatibility stuff */
125/********************************************************************/
126
127/* We do this this way to avoid ifdefs in the actual code */
128#ifdef WIRELESS_SPY
Pavel Roskin343c6862005-09-09 18:43:02 -0400129#define SPY_NUMBER(priv) (priv->spy_data.spy_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#else
131#define SPY_NUMBER(priv) 0
132#endif /* WIRELESS_SPY */
133
134/********************************************************************/
135/* Internal constants */
136/********************************************************************/
137
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200138/* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
139static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
140#define ENCAPS_OVERHEAD (sizeof(encaps_hdr) + 2)
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#define ORINOCO_MIN_MTU 256
Jeff Garzikb4538722005-05-12 22:48:20 -0400143#define ORINOCO_MAX_MTU (IEEE80211_DATA_LEN - ENCAPS_OVERHEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145#define SYMBOL_MAX_VER_LEN (14)
146#define USER_BAP 0
147#define IRQ_BAP 1
148#define MAX_IRQLOOPS_PER_IRQ 10
149#define MAX_IRQLOOPS_PER_JIFFY (20000/HZ) /* Based on a guestimate of
150 * how many events the
151 * device could
152 * legitimately generate */
153#define SMALL_KEY_SIZE 5
154#define LARGE_KEY_SIZE 13
155#define TX_NICBUF_SIZE_BUG 1585 /* Bug in Symbol firmware */
156
157#define DUMMY_FID 0xFFFF
158
159/*#define MAX_MULTICAST(priv) (priv->firmware_type == FIRMWARE_TYPE_AGERE ? \
160 HERMES_MAX_MULTICAST : 0)*/
161#define MAX_MULTICAST(priv) (HERMES_MAX_MULTICAST)
162
163#define ORINOCO_INTEN (HERMES_EV_RX | HERMES_EV_ALLOC \
164 | HERMES_EV_TX | HERMES_EV_TXEXC \
165 | HERMES_EV_WTERR | HERMES_EV_INFO \
166 | HERMES_EV_INFDROP )
167
Christoph Hellwig620554e2005-06-19 01:27:33 +0200168#define MAX_RID_LEN 1024
169
170static const struct iw_handler_def orinoco_handler_def;
Jeff Garzik7282d492006-09-13 14:30:00 -0400171static const struct ethtool_ops orinoco_ethtool_ops;
Christoph Hellwig620554e2005-06-19 01:27:33 +0200172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/********************************************************************/
174/* Data tables */
175/********************************************************************/
176
177/* The frequency of each channel in MHz */
178static const long channel_frequency[] = {
179 2412, 2417, 2422, 2427, 2432, 2437, 2442,
180 2447, 2452, 2457, 2462, 2467, 2472, 2484
181};
182#define NUM_CHANNELS ARRAY_SIZE(channel_frequency)
183
184/* This tables gives the actual meanings of the bitrate IDs returned
185 * by the firmware. */
186static struct {
187 int bitrate; /* in 100s of kilobits */
188 int automatic;
189 u16 agere_txratectrl;
190 u16 intersil_txratectrl;
191} bitrate_table[] = {
192 {110, 1, 3, 15}, /* Entry 0 is the default */
193 {10, 0, 1, 1},
194 {10, 1, 1, 1},
195 {20, 0, 2, 2},
196 {20, 1, 6, 3},
197 {55, 0, 4, 4},
198 {55, 1, 7, 7},
199 {110, 0, 5, 8},
200};
201#define BITRATE_TABLE_SIZE ARRAY_SIZE(bitrate_table)
202
203/********************************************************************/
204/* Data types */
205/********************************************************************/
206
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400207/* Beginning of the Tx descriptor, used in TxExc handling */
208struct hermes_txexc_data {
209 struct hermes_tx_descriptor desc;
Pavel Roskind133ae42005-09-23 04:18:06 -0400210 __le16 frame_ctl;
211 __le16 duration_id;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200212 u8 addr1[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213} __attribute__ ((packed));
214
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200215/* Rx frame header except compatibility 802.3 header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216struct hermes_rx_descriptor {
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200217 /* Control */
Pavel Roskind133ae42005-09-23 04:18:06 -0400218 __le16 status;
219 __le32 time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 u8 silence;
221 u8 signal;
222 u8 rate;
223 u8 rxflow;
Pavel Roskind133ae42005-09-23 04:18:06 -0400224 __le32 reserved;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200225
226 /* 802.11 header */
Pavel Roskind133ae42005-09-23 04:18:06 -0400227 __le16 frame_ctl;
228 __le16 duration_id;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200229 u8 addr1[ETH_ALEN];
230 u8 addr2[ETH_ALEN];
231 u8 addr3[ETH_ALEN];
Pavel Roskind133ae42005-09-23 04:18:06 -0400232 __le16 seq_ctl;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200233 u8 addr4[ETH_ALEN];
234
235 /* Data length */
Pavel Roskind133ae42005-09-23 04:18:06 -0400236 __le16 data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237} __attribute__ ((packed));
238
239/********************************************************************/
240/* Function prototypes */
241/********************************************************************/
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243static int __orinoco_program_rids(struct net_device *dev);
244static void __orinoco_set_multicast_list(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246/********************************************************************/
247/* Internal helper functions */
248/********************************************************************/
249
250static inline void set_port_type(struct orinoco_private *priv)
251{
252 switch (priv->iw_mode) {
253 case IW_MODE_INFRA:
254 priv->port_type = 1;
255 priv->createibss = 0;
256 break;
257 case IW_MODE_ADHOC:
258 if (priv->prefer_port3) {
259 priv->port_type = 3;
260 priv->createibss = 0;
261 } else {
262 priv->port_type = priv->ibss_port;
263 priv->createibss = 1;
264 }
265 break;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200266 case IW_MODE_MONITOR:
267 priv->port_type = 3;
268 priv->createibss = 0;
269 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 default:
271 printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
272 priv->ndev->name);
273 }
274}
275
Dan Williams1e3428e2007-10-10 23:56:25 -0400276#define ORINOCO_MAX_BSS_COUNT 64
277static int orinoco_bss_data_allocate(struct orinoco_private *priv)
278{
David Kilroy01632fa2008-08-21 23:27:58 +0100279 if (priv->bss_xbss_data)
Dan Williams1e3428e2007-10-10 23:56:25 -0400280 return 0;
281
David Kilroy01632fa2008-08-21 23:27:58 +0100282 if (priv->has_ext_scan)
283 priv->bss_xbss_data = kzalloc(ORINOCO_MAX_BSS_COUNT *
284 sizeof(struct xbss_element),
285 GFP_KERNEL);
286 else
287 priv->bss_xbss_data = kzalloc(ORINOCO_MAX_BSS_COUNT *
288 sizeof(struct bss_element),
289 GFP_KERNEL);
290
291 if (!priv->bss_xbss_data) {
Dan Williams1e3428e2007-10-10 23:56:25 -0400292 printk(KERN_WARNING "Out of memory allocating beacons");
293 return -ENOMEM;
294 }
295 return 0;
296}
297
298static void orinoco_bss_data_free(struct orinoco_private *priv)
299{
David Kilroy01632fa2008-08-21 23:27:58 +0100300 kfree(priv->bss_xbss_data);
301 priv->bss_xbss_data = NULL;
Dan Williams1e3428e2007-10-10 23:56:25 -0400302}
303
David Kilroy01632fa2008-08-21 23:27:58 +0100304#define PRIV_BSS ((struct bss_element *)priv->bss_xbss_data)
305#define PRIV_XBSS ((struct xbss_element *)priv->bss_xbss_data)
Dan Williams1e3428e2007-10-10 23:56:25 -0400306static void orinoco_bss_data_init(struct orinoco_private *priv)
307{
308 int i;
309
310 INIT_LIST_HEAD(&priv->bss_free_list);
311 INIT_LIST_HEAD(&priv->bss_list);
David Kilroy01632fa2008-08-21 23:27:58 +0100312 if (priv->has_ext_scan)
313 for (i = 0; i < ORINOCO_MAX_BSS_COUNT; i++)
314 list_add_tail(&(PRIV_XBSS[i].list),
315 &priv->bss_free_list);
316 else
317 for (i = 0; i < ORINOCO_MAX_BSS_COUNT; i++)
318 list_add_tail(&(PRIV_BSS[i].list),
319 &priv->bss_free_list);
320
321}
322
323static inline u8 *orinoco_get_ie(u8 *data, size_t len,
324 enum ieee80211_mfie eid)
325{
326 u8 *p = data;
327 while ((p + 2) < (data + len)) {
328 if (p[0] == eid)
329 return p;
330 p += p[1] + 2;
331 }
332 return NULL;
333}
334
335#define WPA_OUI_TYPE "\x00\x50\xF2\x01"
336#define WPA_SELECTOR_LEN 4
337static inline u8 *orinoco_get_wpa_ie(u8 *data, size_t len)
338{
339 u8 *p = data;
340 while ((p + 2 + WPA_SELECTOR_LEN) < (data + len)) {
341 if ((p[0] == MFIE_TYPE_GENERIC) &&
342 (memcmp(&p[2], WPA_OUI_TYPE, WPA_SELECTOR_LEN) == 0))
343 return p;
344 p += p[1] + 2;
345 }
346 return NULL;
Dan Williams1e3428e2007-10-10 23:56:25 -0400347}
348
David Kilroy3994d502008-08-21 23:27:54 +0100349
350/********************************************************************/
351/* Download functionality */
352/********************************************************************/
353
354struct fw_info {
355 char *pri_fw;
356 char *sta_fw;
357 char *ap_fw;
358 u32 pda_addr;
359 u16 pda_size;
360};
361
362const static struct fw_info orinoco_fw[] = {
363 { "", "agere_sta_fw.bin", "agere_ap_fw.bin", 0x00390000, 1000 },
364 { "", "prism_sta_fw.bin", "prism_ap_fw.bin", 0, 1024 },
365 { "symbol_sp24t_prim_fw", "symbol_sp24t_sec_fw", "", 0x00003100, 0x100 }
366};
367
368/* Structure used to access fields in FW
369 * Make sure LE decoding macros are used
370 */
371struct orinoco_fw_header {
372 char hdr_vers[6]; /* ASCII string for header version */
373 __le16 headersize; /* Total length of header */
374 __le32 entry_point; /* NIC entry point */
375 __le32 blocks; /* Number of blocks to program */
376 __le32 block_offset; /* Offset of block data from eof header */
377 __le32 pdr_offset; /* Offset to PDR data from eof header */
378 __le32 pri_offset; /* Offset to primary plug data */
379 __le32 compat_offset; /* Offset to compatibility data*/
380 char signature[0]; /* FW signature length headersize-20 */
381} __attribute__ ((packed));
382
383/* Download either STA or AP firmware into the card. */
384static int
385orinoco_dl_firmware(struct orinoco_private *priv,
386 const struct fw_info *fw,
387 int ap)
388{
389 /* Plug Data Area (PDA) */
390 __le16 pda[512] = { 0 };
391
392 hermes_t *hw = &priv->hw;
393 const struct firmware *fw_entry;
394 const struct orinoco_fw_header *hdr;
395 const unsigned char *first_block;
396 const unsigned char *end;
397 const char *firmware;
398 struct net_device *dev = priv->ndev;
399 int err;
400
401 if (ap)
402 firmware = fw->ap_fw;
403 else
404 firmware = fw->sta_fw;
405
406 printk(KERN_DEBUG "%s: Attempting to download firmware %s\n",
407 dev->name, firmware);
408
409 /* Read current plug data */
410 err = hermes_read_pda(hw, pda, fw->pda_addr,
411 min_t(u16, fw->pda_size, sizeof(pda)), 0);
412 printk(KERN_DEBUG "%s: Read PDA returned %d\n", dev->name, err);
413 if (err)
414 return err;
415
416 err = request_firmware(&fw_entry, firmware, priv->dev);
417 if (err) {
418 printk(KERN_ERR "%s: Cannot find firmware %s\n",
419 dev->name, firmware);
420 return -ENOENT;
421 }
422
423 hdr = (const struct orinoco_fw_header *) fw_entry->data;
424
425 /* Enable aux port to allow programming */
426 err = hermesi_program_init(hw, le32_to_cpu(hdr->entry_point));
427 printk(KERN_DEBUG "%s: Program init returned %d\n", dev->name, err);
428 if (err != 0)
429 goto abort;
430
431 /* Program data */
432 first_block = (fw_entry->data +
433 le16_to_cpu(hdr->headersize) +
434 le32_to_cpu(hdr->block_offset));
435 end = fw_entry->data + fw_entry->size;
436
437 err = hermes_program(hw, first_block, end);
438 printk(KERN_DEBUG "%s: Program returned %d\n", dev->name, err);
439 if (err != 0)
440 goto abort;
441
442 /* Update production data */
443 first_block = (fw_entry->data +
444 le16_to_cpu(hdr->headersize) +
445 le32_to_cpu(hdr->pdr_offset));
446
447 err = hermes_apply_pda_with_defaults(hw, first_block, pda);
448 printk(KERN_DEBUG "%s: Apply PDA returned %d\n", dev->name, err);
449 if (err)
450 goto abort;
451
452 /* Tell card we've finished */
453 err = hermesi_program_end(hw);
454 printk(KERN_DEBUG "%s: Program end returned %d\n", dev->name, err);
455 if (err != 0)
456 goto abort;
457
458 /* Check if we're running */
459 printk(KERN_DEBUG "%s: hermes_present returned %d\n",
460 dev->name, hermes_present(hw));
461
462abort:
463 release_firmware(fw_entry);
464 return err;
465}
466
467/* End markers */
468#define TEXT_END 0x1A /* End of text header */
469
470/*
471 * Process a firmware image - stop the card, load the firmware, reset
472 * the card and make sure it responds. For the secondary firmware take
473 * care of the PDA - read it and then write it on top of the firmware.
474 */
475static int
476symbol_dl_image(struct orinoco_private *priv, const struct fw_info *fw,
477 const unsigned char *image, const unsigned char *end,
478 int secondary)
479{
480 hermes_t *hw = &priv->hw;
481 int ret;
482 const unsigned char *ptr;
483 const unsigned char *first_block;
484
485 /* Plug Data Area (PDA) */
486 __le16 pda[256];
487
488 /* Binary block begins after the 0x1A marker */
489 ptr = image;
490 while (*ptr++ != TEXT_END);
491 first_block = ptr;
492
493 /* Read the PDA from EEPROM */
494 if (secondary) {
495 ret = hermes_read_pda(hw, pda, fw->pda_addr, sizeof(pda), 1);
496 if (ret)
497 return ret;
498 }
499
500 /* Stop the firmware, so that it can be safely rewritten */
501 if (priv->stop_fw) {
502 ret = priv->stop_fw(priv, 1);
503 if (ret)
504 return ret;
505 }
506
507 /* Program the adapter with new firmware */
508 ret = hermes_program(hw, first_block, end);
509 if (ret)
510 return ret;
511
512 /* Write the PDA to the adapter */
513 if (secondary) {
514 size_t len = hermes_blocks_length(first_block);
515 ptr = first_block + len;
516 ret = hermes_apply_pda(hw, ptr, pda);
517 if (ret)
518 return ret;
519 }
520
521 /* Run the firmware */
522 if (priv->stop_fw) {
523 ret = priv->stop_fw(priv, 0);
524 if (ret)
525 return ret;
526 }
527
528 /* Reset hermes chip and make sure it responds */
529 ret = hermes_init(hw);
530
531 /* hermes_reset() should return 0 with the secondary firmware */
532 if (secondary && ret != 0)
533 return -ENODEV;
534
535 /* And this should work with any firmware */
536 if (!hermes_present(hw))
537 return -ENODEV;
538
539 return 0;
540}
541
542
543/*
544 * Download the firmware into the card, this also does a PCMCIA soft
545 * reset on the card, to make sure it's in a sane state.
546 */
547static int
548symbol_dl_firmware(struct orinoco_private *priv,
549 const struct fw_info *fw)
550{
551 struct net_device *dev = priv->ndev;
552 int ret;
553 const struct firmware *fw_entry;
554
555 if (request_firmware(&fw_entry, fw->pri_fw,
556 priv->dev) != 0) {
557 printk(KERN_ERR "%s: Cannot find firmware: %s\n",
558 dev->name, fw->pri_fw);
559 return -ENOENT;
560 }
561
562 /* Load primary firmware */
563 ret = symbol_dl_image(priv, fw, fw_entry->data,
564 fw_entry->data + fw_entry->size, 0);
565 release_firmware(fw_entry);
566 if (ret) {
567 printk(KERN_ERR "%s: Primary firmware download failed\n",
568 dev->name);
569 return ret;
570 }
571
572 if (request_firmware(&fw_entry, fw->sta_fw,
573 priv->dev) != 0) {
574 printk(KERN_ERR "%s: Cannot find firmware: %s\n",
575 dev->name, fw->sta_fw);
576 return -ENOENT;
577 }
578
579 /* Load secondary firmware */
580 ret = symbol_dl_image(priv, fw, fw_entry->data,
581 fw_entry->data + fw_entry->size, 1);
582 release_firmware(fw_entry);
583 if (ret) {
584 printk(KERN_ERR "%s: Secondary firmware download failed\n",
585 dev->name);
586 }
587
588 return ret;
589}
590
591static int orinoco_download(struct orinoco_private *priv)
592{
593 int err = 0;
594 /* Reload firmware */
595 switch (priv->firmware_type) {
596 case FIRMWARE_TYPE_AGERE:
597 /* case FIRMWARE_TYPE_INTERSIL: */
598 err = orinoco_dl_firmware(priv,
599 &orinoco_fw[priv->firmware_type], 0);
600 break;
601
602 case FIRMWARE_TYPE_SYMBOL:
603 err = symbol_dl_firmware(priv,
604 &orinoco_fw[priv->firmware_type]);
605 break;
606 case FIRMWARE_TYPE_INTERSIL:
607 break;
608 }
609 /* TODO: if we fail we probably need to reinitialise
610 * the driver */
611
612 return err;
613}
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615/********************************************************************/
616/* Device methods */
617/********************************************************************/
618
619static int orinoco_open(struct net_device *dev)
620{
621 struct orinoco_private *priv = netdev_priv(dev);
622 unsigned long flags;
623 int err;
624
625 if (orinoco_lock(priv, &flags) != 0)
626 return -EBUSY;
627
628 err = __orinoco_up(dev);
629
630 if (! err)
631 priv->open = 1;
632
633 orinoco_unlock(priv, &flags);
634
635 return err;
636}
637
Christoph Hellwigad8f4512005-05-14 17:30:17 +0200638static int orinoco_stop(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 struct orinoco_private *priv = netdev_priv(dev);
641 int err = 0;
642
643 /* We mustn't use orinoco_lock() here, because we need to be
644 able to close the interface even if hw_unavailable is set
645 (e.g. as we're released after a PC Card removal) */
646 spin_lock_irq(&priv->lock);
647
648 priv->open = 0;
649
650 err = __orinoco_down(dev);
651
652 spin_unlock_irq(&priv->lock);
653
654 return err;
655}
656
657static struct net_device_stats *orinoco_get_stats(struct net_device *dev)
658{
659 struct orinoco_private *priv = netdev_priv(dev);
660
661 return &priv->stats;
662}
663
664static struct iw_statistics *orinoco_get_wireless_stats(struct net_device *dev)
665{
666 struct orinoco_private *priv = netdev_priv(dev);
667 hermes_t *hw = &priv->hw;
668 struct iw_statistics *wstats = &priv->wstats;
David Gibsone67d9d92005-05-12 20:01:22 -0400669 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 unsigned long flags;
671
672 if (! netif_device_present(dev)) {
673 printk(KERN_WARNING "%s: get_wireless_stats() called while device not present\n",
674 dev->name);
675 return NULL; /* FIXME: Can we do better than this? */
676 }
677
David Gibsone67d9d92005-05-12 20:01:22 -0400678 /* If busy, return the old stats. Returning NULL may cause
679 * the interface to disappear from /proc/net/wireless */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (orinoco_lock(priv, &flags) != 0)
David Gibsone67d9d92005-05-12 20:01:22 -0400681 return wstats;
682
683 /* We can't really wait for the tallies inquiry command to
684 * complete, so we just use the previous results and trigger
685 * a new tallies inquiry command for next time - Jean II */
686 /* FIXME: Really we should wait for the inquiry to come back -
687 * as it is the stats we give don't make a whole lot of sense.
688 * Unfortunately, it's not clear how to do that within the
689 * wireless extensions framework: I think we're in user
690 * context, but a lock seems to be held by the time we get in
691 * here so we're not safe to sleep here. */
692 hermes_inquire(hw, HERMES_INQ_TALLIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 if (priv->iw_mode == IW_MODE_ADHOC) {
695 memset(&wstats->qual, 0, sizeof(wstats->qual));
696 /* If a spy address is defined, we report stats of the
697 * first spy address - Jean II */
698 if (SPY_NUMBER(priv)) {
Pavel Roskin343c6862005-09-09 18:43:02 -0400699 wstats->qual.qual = priv->spy_data.spy_stat[0].qual;
700 wstats->qual.level = priv->spy_data.spy_stat[0].level;
701 wstats->qual.noise = priv->spy_data.spy_stat[0].noise;
702 wstats->qual.updated = priv->spy_data.spy_stat[0].updated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704 } else {
705 struct {
Pavel Roskina208c4e2006-04-07 04:10:26 -0400706 __le16 qual, signal, noise, unused;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 } __attribute__ ((packed)) cq;
708
709 err = HERMES_READ_RECORD(hw, USER_BAP,
710 HERMES_RID_COMMSQUALITY, &cq);
David Gibsone67d9d92005-05-12 20:01:22 -0400711
712 if (!err) {
713 wstats->qual.qual = (int)le16_to_cpu(cq.qual);
714 wstats->qual.level = (int)le16_to_cpu(cq.signal) - 0x95;
715 wstats->qual.noise = (int)le16_to_cpu(cq.noise) - 0x95;
716 wstats->qual.updated = 7;
717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return wstats;
722}
723
724static void orinoco_set_multicast_list(struct net_device *dev)
725{
726 struct orinoco_private *priv = netdev_priv(dev);
727 unsigned long flags;
728
729 if (orinoco_lock(priv, &flags) != 0) {
730 printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
731 "called when hw_unavailable\n", dev->name);
732 return;
733 }
734
735 __orinoco_set_multicast_list(dev);
736 orinoco_unlock(priv, &flags);
737}
738
739static int orinoco_change_mtu(struct net_device *dev, int new_mtu)
740{
741 struct orinoco_private *priv = netdev_priv(dev);
742
743 if ( (new_mtu < ORINOCO_MIN_MTU) || (new_mtu > ORINOCO_MAX_MTU) )
744 return -EINVAL;
745
Jeff Garzikb4538722005-05-12 22:48:20 -0400746 if ( (new_mtu + ENCAPS_OVERHEAD + IEEE80211_HLEN) >
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 (priv->nicbuf_size - ETH_HLEN) )
748 return -EINVAL;
749
750 dev->mtu = new_mtu;
751
752 return 0;
753}
754
755/********************************************************************/
756/* Tx path */
757/********************************************************************/
758
759static int orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
760{
761 struct orinoco_private *priv = netdev_priv(dev);
762 struct net_device_stats *stats = &priv->stats;
763 hermes_t *hw = &priv->hw;
764 int err = 0;
765 u16 txfid = priv->txfid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 struct ethhdr *eh;
Pavel Roskina28dc812006-04-07 04:10:45 -0400767 int data_off;
David Kilroy6eecad72008-08-21 23:27:56 +0100768 int tx_control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 unsigned long flags;
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (! netif_running(dev)) {
772 printk(KERN_ERR "%s: Tx on stopped device!\n",
773 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400774 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776
777 if (netif_queue_stopped(dev)) {
778 printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
779 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400780 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782
783 if (orinoco_lock(priv, &flags) != 0) {
784 printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n",
785 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400786 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200789 if (! netif_carrier_ok(dev) || (priv->iw_mode == IW_MODE_MONITOR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 /* Oops, the firmware hasn't established a connection,
791 silently drop the packet (this seems to be the
792 safest approach). */
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400793 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795
Pavel Roskin8d5be082006-04-07 04:10:41 -0400796 /* Check packet length */
Pavel Roskina28dc812006-04-07 04:10:45 -0400797 if (skb->len < ETH_HLEN)
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400798 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 eh = (struct ethhdr *)skb->data;
801
David Kilroy6eecad72008-08-21 23:27:56 +0100802 tx_control = HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
David Kilroy6eecad72008-08-21 23:27:56 +0100804 if (priv->has_alt_txcntl) {
805 /* WPA enabled firmwares have tx_cntl at the end of
806 * the 802.11 header. So write zeroed descriptor and
807 * 802.11 header at the same time
808 */
809 char desc[HERMES_802_3_OFFSET];
810 __le16 *txcntl = (__le16 *) &desc[HERMES_TXCNTL2_OFFSET];
811
812 memset(&desc, 0, sizeof(desc));
813
814 *txcntl = cpu_to_le16(tx_control);
815 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
816 txfid, 0);
817 if (err) {
818 if (net_ratelimit())
819 printk(KERN_ERR "%s: Error %d writing Tx "
820 "descriptor to BAP\n", dev->name, err);
821 goto busy;
822 }
823 } else {
824 struct hermes_tx_descriptor desc;
825
826 memset(&desc, 0, sizeof(desc));
827
828 desc.tx_control = cpu_to_le16(tx_control);
829 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
830 txfid, 0);
831 if (err) {
832 if (net_ratelimit())
833 printk(KERN_ERR "%s: Error %d writing Tx "
834 "descriptor to BAP\n", dev->name, err);
835 goto busy;
836 }
837
838 /* Clear the 802.11 header and data length fields - some
839 * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
840 * if this isn't done. */
841 hermes_clear_words(hw, HERMES_DATA0,
842 HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 /* Encapsulate Ethernet-II frames */
846 if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
Pavel Roskina28dc812006-04-07 04:10:45 -0400847 struct header_struct {
848 struct ethhdr eth; /* 802.3 header */
849 u8 encap[6]; /* 802.2 header */
850 } __attribute__ ((packed)) hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Pavel Roskina28dc812006-04-07 04:10:45 -0400852 /* Strip destination and source from the data */
853 skb_pull(skb, 2 * ETH_ALEN);
854 data_off = HERMES_802_2_OFFSET + sizeof(encaps_hdr);
855
856 /* And move them to a separate header */
857 memcpy(&hdr.eth, eh, 2 * ETH_ALEN);
858 hdr.eth.h_proto = htons(sizeof(encaps_hdr) + skb->len);
859 memcpy(hdr.encap, encaps_hdr, sizeof(encaps_hdr));
860
861 err = hermes_bap_pwrite(hw, USER_BAP, &hdr, sizeof(hdr),
862 txfid, HERMES_802_3_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (err) {
864 if (net_ratelimit())
865 printk(KERN_ERR "%s: Error %d writing packet "
866 "header to BAP\n", dev->name, err);
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400867 goto busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869 } else { /* IEEE 802.3 frame */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 data_off = HERMES_802_3_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
872
Pavel Roskina28dc812006-04-07 04:10:45 -0400873 err = hermes_bap_pwrite(hw, USER_BAP, skb->data, skb->len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 txfid, data_off);
875 if (err) {
876 printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
877 dev->name, err);
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400878 goto busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 }
880
881 /* Finally, we actually initiate the send */
882 netif_stop_queue(dev);
883
884 err = hermes_docmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
885 txfid, NULL);
886 if (err) {
887 netif_start_queue(dev);
Andrew Mortonc367c212005-10-19 21:23:44 -0700888 if (net_ratelimit())
889 printk(KERN_ERR "%s: Error %d transmitting packet\n",
890 dev->name, err);
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400891 goto busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
893
894 dev->trans_start = jiffies;
Pavel Roskina28dc812006-04-07 04:10:45 -0400895 stats->tx_bytes += data_off + skb->len;
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400896 goto ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400898 drop:
899 stats->tx_errors++;
900 stats->tx_dropped++;
901
902 ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 dev_kfree_skb(skb);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400905 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400907 busy:
Jiri Benc2c1bd262006-04-07 04:10:47 -0400908 if (err == -EIO)
909 schedule_work(&priv->reset_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 orinoco_unlock(priv, &flags);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400911 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
914static void __orinoco_ev_alloc(struct net_device *dev, hermes_t *hw)
915{
916 struct orinoco_private *priv = netdev_priv(dev);
917 u16 fid = hermes_read_regn(hw, ALLOCFID);
918
919 if (fid != priv->txfid) {
920 if (fid != DUMMY_FID)
921 printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
922 dev->name, fid);
923 return;
924 }
925
926 hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
927}
928
929static void __orinoco_ev_tx(struct net_device *dev, hermes_t *hw)
930{
931 struct orinoco_private *priv = netdev_priv(dev);
932 struct net_device_stats *stats = &priv->stats;
933
934 stats->tx_packets++;
935
936 netif_wake_queue(dev);
937
938 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
939}
940
941static void __orinoco_ev_txexc(struct net_device *dev, hermes_t *hw)
942{
943 struct orinoco_private *priv = netdev_priv(dev);
944 struct net_device_stats *stats = &priv->stats;
945 u16 fid = hermes_read_regn(hw, TXCOMPLFID);
Pavel Roskind133ae42005-09-23 04:18:06 -0400946 u16 status;
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400947 struct hermes_txexc_data hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 int err = 0;
949
950 if (fid == DUMMY_FID)
951 return; /* Nothing's really happened */
952
Pavel Roskin48ca7032005-09-23 04:18:06 -0400953 /* Read part of the frame header - we need status and addr1 */
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200954 err = hermes_bap_pread(hw, IRQ_BAP, &hdr,
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400955 sizeof(struct hermes_txexc_data),
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200956 fid, 0);
957
958 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
959 stats->tx_errors++;
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (err) {
962 printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
963 "(FID=%04X error %d)\n",
964 dev->name, fid, err);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200965 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
967
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200968 DEBUG(1, "%s: Tx error, err %d (FID=%04X)\n", dev->name,
969 err, fid);
970
971 /* We produce a TXDROP event only for retry or lifetime
972 * exceeded, because that's the only status that really mean
973 * that this particular node went away.
974 * Other errors means that *we* screwed up. - Jean II */
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400975 status = le16_to_cpu(hdr.desc.status);
Pavel Roskind133ae42005-09-23 04:18:06 -0400976 if (status & (HERMES_TXSTAT_RETRYERR | HERMES_TXSTAT_AGEDERR)) {
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200977 union iwreq_data wrqu;
978
979 /* Copy 802.11 dest address.
980 * We use the 802.11 header because the frame may
981 * not be 802.3 or may be mangled...
982 * In Ad-Hoc mode, it will be the node address.
983 * In managed mode, it will be most likely the AP addr
984 * User space will figure out how to convert it to
985 * whatever it needs (IP address or else).
986 * - Jean II */
987 memcpy(wrqu.addr.sa_data, hdr.addr1, ETH_ALEN);
988 wrqu.addr.sa_family = ARPHRD_ETHER;
989
990 /* Send event to user space */
991 wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
997static void orinoco_tx_timeout(struct net_device *dev)
998{
999 struct orinoco_private *priv = netdev_priv(dev);
1000 struct net_device_stats *stats = &priv->stats;
1001 struct hermes *hw = &priv->hw;
1002
1003 printk(KERN_WARNING "%s: Tx timeout! "
1004 "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
1005 dev->name, hermes_read_regn(hw, ALLOCFID),
1006 hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
1007
1008 stats->tx_errors++;
1009
1010 schedule_work(&priv->reset_work);
1011}
1012
1013/********************************************************************/
1014/* Rx path (data frames) */
1015/********************************************************************/
1016
1017/* Does the frame have a SNAP header indicating it should be
1018 * de-encapsulated to Ethernet-II? */
1019static inline int is_ethersnap(void *_hdr)
1020{
1021 u8 *hdr = _hdr;
1022
1023 /* We de-encapsulate all packets which, a) have SNAP headers
1024 * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
1025 * and where b) the OUI of the SNAP header is 00:00:00 or
1026 * 00:00:f8 - we need both because different APs appear to use
1027 * different OUIs for some reason */
1028 return (memcmp(hdr, &encaps_hdr, 5) == 0)
1029 && ( (hdr[5] == 0x00) || (hdr[5] == 0xf8) );
1030}
1031
1032static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
1033 int level, int noise)
1034{
Pavel Roskin343c6862005-09-09 18:43:02 -04001035 struct iw_quality wstats;
1036 wstats.level = level - 0x95;
1037 wstats.noise = noise - 0x95;
1038 wstats.qual = (level > noise) ? (level - noise) : 0;
1039 wstats.updated = 7;
1040 /* Update spy records */
1041 wireless_spy_update(dev, mac, &wstats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
1044static void orinoco_stat_gather(struct net_device *dev,
1045 struct sk_buff *skb,
1046 struct hermes_rx_descriptor *desc)
1047{
1048 struct orinoco_private *priv = netdev_priv(dev);
1049
1050 /* Using spy support with lots of Rx packets, like in an
1051 * infrastructure (AP), will really slow down everything, because
1052 * the MAC address must be compared to each entry of the spy list.
1053 * If the user really asks for it (set some address in the
1054 * spy list), we do it, but he will pay the price.
1055 * Note that to get here, you need both WIRELESS_SPY
1056 * compiled in AND some addresses in the list !!!
1057 */
1058 /* Note : gcc will optimise the whole section away if
1059 * WIRELESS_SPY is not defined... - Jean II */
1060 if (SPY_NUMBER(priv)) {
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001061 orinoco_spy_gather(dev, skb_mac_header(skb) + ETH_ALEN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 desc->signal, desc->silence);
1063 }
1064}
1065
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001066/*
1067 * orinoco_rx_monitor - handle received monitor frames.
1068 *
1069 * Arguments:
1070 * dev network device
1071 * rxfid received FID
1072 * desc rx descriptor of the frame
1073 *
1074 * Call context: interrupt
1075 */
1076static void orinoco_rx_monitor(struct net_device *dev, u16 rxfid,
1077 struct hermes_rx_descriptor *desc)
1078{
1079 u32 hdrlen = 30; /* return full header by default */
1080 u32 datalen = 0;
1081 u16 fc;
1082 int err;
1083 int len;
1084 struct sk_buff *skb;
1085 struct orinoco_private *priv = netdev_priv(dev);
1086 struct net_device_stats *stats = &priv->stats;
1087 hermes_t *hw = &priv->hw;
1088
1089 len = le16_to_cpu(desc->data_len);
1090
1091 /* Determine the size of the header and the data */
1092 fc = le16_to_cpu(desc->frame_ctl);
1093 switch (fc & IEEE80211_FCTL_FTYPE) {
1094 case IEEE80211_FTYPE_DATA:
1095 if ((fc & IEEE80211_FCTL_TODS)
1096 && (fc & IEEE80211_FCTL_FROMDS))
1097 hdrlen = 30;
1098 else
1099 hdrlen = 24;
1100 datalen = len;
1101 break;
1102 case IEEE80211_FTYPE_MGMT:
1103 hdrlen = 24;
1104 datalen = len;
1105 break;
1106 case IEEE80211_FTYPE_CTL:
1107 switch (fc & IEEE80211_FCTL_STYPE) {
1108 case IEEE80211_STYPE_PSPOLL:
1109 case IEEE80211_STYPE_RTS:
1110 case IEEE80211_STYPE_CFEND:
1111 case IEEE80211_STYPE_CFENDACK:
1112 hdrlen = 16;
1113 break;
1114 case IEEE80211_STYPE_CTS:
1115 case IEEE80211_STYPE_ACK:
1116 hdrlen = 10;
1117 break;
1118 }
1119 break;
1120 default:
1121 /* Unknown frame type */
1122 break;
1123 }
1124
1125 /* sanity check the length */
1126 if (datalen > IEEE80211_DATA_LEN + 12) {
1127 printk(KERN_DEBUG "%s: oversized monitor frame, "
1128 "data length = %d\n", dev->name, datalen);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001129 stats->rx_length_errors++;
1130 goto update_stats;
1131 }
1132
1133 skb = dev_alloc_skb(hdrlen + datalen);
1134 if (!skb) {
1135 printk(KERN_WARNING "%s: Cannot allocate skb for monitor frame\n",
1136 dev->name);
Florin Malitabb6e0932006-05-22 22:35:30 -07001137 goto update_stats;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001138 }
1139
1140 /* Copy the 802.11 header to the skb */
1141 memcpy(skb_put(skb, hdrlen), &(desc->frame_ctl), hdrlen);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001142 skb_reset_mac_header(skb);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001143
1144 /* If any, copy the data from the card to the skb */
1145 if (datalen > 0) {
1146 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, datalen),
1147 ALIGN(datalen, 2), rxfid,
1148 HERMES_802_2_OFFSET);
1149 if (err) {
1150 printk(KERN_ERR "%s: error %d reading monitor frame\n",
1151 dev->name, err);
1152 goto drop;
1153 }
1154 }
1155
1156 skb->dev = dev;
1157 skb->ip_summed = CHECKSUM_NONE;
1158 skb->pkt_type = PACKET_OTHERHOST;
1159 skb->protocol = __constant_htons(ETH_P_802_2);
1160
1161 dev->last_rx = jiffies;
1162 stats->rx_packets++;
1163 stats->rx_bytes += skb->len;
1164
1165 netif_rx(skb);
1166 return;
1167
1168 drop:
1169 dev_kfree_skb_irq(skb);
1170 update_stats:
1171 stats->rx_errors++;
1172 stats->rx_dropped++;
1173}
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175static void __orinoco_ev_rx(struct net_device *dev, hermes_t *hw)
1176{
1177 struct orinoco_private *priv = netdev_priv(dev);
1178 struct net_device_stats *stats = &priv->stats;
1179 struct iw_statistics *wstats = &priv->wstats;
1180 struct sk_buff *skb = NULL;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001181 u16 rxfid, status, fc;
1182 int length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 struct hermes_rx_descriptor desc;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001184 struct ethhdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 int err;
1186
1187 rxfid = hermes_read_regn(hw, RXFID);
1188
1189 err = hermes_bap_pread(hw, IRQ_BAP, &desc, sizeof(desc),
1190 rxfid, 0);
1191 if (err) {
1192 printk(KERN_ERR "%s: error %d reading Rx descriptor. "
1193 "Frame dropped.\n", dev->name, err);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001194 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
1196
1197 status = le16_to_cpu(desc.status);
1198
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001199 if (status & HERMES_RXSTAT_BADCRC) {
1200 DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n",
1201 dev->name);
1202 stats->rx_crc_errors++;
1203 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
1205
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001206 /* Handle frames in monitor mode */
1207 if (priv->iw_mode == IW_MODE_MONITOR) {
1208 orinoco_rx_monitor(dev, rxfid, &desc);
1209 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
1211
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001212 if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
1213 DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
1214 dev->name);
1215 wstats->discard.code++;
1216 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 }
1218
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001219 length = le16_to_cpu(desc.data_len);
1220 fc = le16_to_cpu(desc.frame_ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 /* Sanity checks */
1223 if (length < 3) { /* No for even an 802.2 LLC header */
1224 /* At least on Symbol firmware with PCF we get quite a
1225 lot of these legitimately - Poll frames with no
1226 data. */
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001227 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
Jeff Garzikb4538722005-05-12 22:48:20 -04001229 if (length > IEEE80211_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
1231 dev->name, length);
1232 stats->rx_length_errors++;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001233 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235
1236 /* We need space for the packet data itself, plus an ethernet
1237 header, plus 2 bytes so we can align the IP header on a
1238 32bit boundary, plus 1 byte so we can read in odd length
1239 packets from the card, which has an IO granularity of 16
1240 bits */
1241 skb = dev_alloc_skb(length+ETH_HLEN+2+1);
1242 if (!skb) {
1243 printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
1244 dev->name);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001245 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001248 /* We'll prepend the header, so reserve space for it. The worst
1249 case is no decapsulation, when 802.3 header is prepended and
1250 nothing is removed. 2 is for aligning the IP header. */
1251 skb_reserve(skb, ETH_HLEN + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001253 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, length),
1254 ALIGN(length, 2), rxfid,
1255 HERMES_802_2_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (err) {
1257 printk(KERN_ERR "%s: error %d reading frame. "
1258 "Frame dropped.\n", dev->name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 goto drop;
1260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 /* Handle decapsulation
1263 * In most cases, the firmware tell us about SNAP frames.
1264 * For some reason, the SNAP frames sent by LinkSys APs
1265 * are not properly recognised by most firmwares.
1266 * So, check ourselves */
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001267 if (length >= ENCAPS_OVERHEAD &&
1268 (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
1269 ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
1270 is_ethersnap(skb->data))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 /* These indicate a SNAP within 802.2 LLC within
1272 802.11 frame which we'll need to de-encapsulate to
1273 the original EthernetII frame. */
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001274 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN - ENCAPS_OVERHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 } else {
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001276 /* 802.3 frame - prepend 802.3 header as is */
1277 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN);
1278 hdr->h_proto = htons(length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001280 memcpy(hdr->h_dest, desc.addr1, ETH_ALEN);
1281 if (fc & IEEE80211_FCTL_FROMDS)
1282 memcpy(hdr->h_source, desc.addr3, ETH_ALEN);
1283 else
1284 memcpy(hdr->h_source, desc.addr2, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 dev->last_rx = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 skb->protocol = eth_type_trans(skb, dev);
1288 skb->ip_summed = CHECKSUM_NONE;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001289 if (fc & IEEE80211_FCTL_TODS)
1290 skb->pkt_type = PACKET_OTHERHOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 /* Process the wireless stats if needed */
1293 orinoco_stat_gather(dev, skb, &desc);
1294
1295 /* Pass the packet to the networking stack */
1296 netif_rx(skb);
1297 stats->rx_packets++;
1298 stats->rx_bytes += length;
1299
1300 return;
1301
1302 drop:
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001303 dev_kfree_skb_irq(skb);
1304 update_stats:
1305 stats->rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 stats->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307}
1308
1309/********************************************************************/
1310/* Rx path (info frames) */
1311/********************************************************************/
1312
1313static void print_linkstatus(struct net_device *dev, u16 status)
1314{
1315 char * s;
1316
1317 if (suppress_linkstatus)
1318 return;
1319
1320 switch (status) {
1321 case HERMES_LINKSTATUS_NOT_CONNECTED:
1322 s = "Not Connected";
1323 break;
1324 case HERMES_LINKSTATUS_CONNECTED:
1325 s = "Connected";
1326 break;
1327 case HERMES_LINKSTATUS_DISCONNECTED:
1328 s = "Disconnected";
1329 break;
1330 case HERMES_LINKSTATUS_AP_CHANGE:
1331 s = "AP Changed";
1332 break;
1333 case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
1334 s = "AP Out of Range";
1335 break;
1336 case HERMES_LINKSTATUS_AP_IN_RANGE:
1337 s = "AP In Range";
1338 break;
1339 case HERMES_LINKSTATUS_ASSOC_FAILED:
1340 s = "Association Failed";
1341 break;
1342 default:
1343 s = "UNKNOWN";
1344 }
1345
1346 printk(KERN_INFO "%s: New link status: %s (%04x)\n",
1347 dev->name, s, status);
1348}
1349
Christoph Hellwig16739b02005-06-19 01:27:51 +02001350/* Search scan results for requested BSSID, join it if found */
David Howellsc4028952006-11-22 14:57:56 +00001351static void orinoco_join_ap(struct work_struct *work)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001352{
David Howellsc4028952006-11-22 14:57:56 +00001353 struct orinoco_private *priv =
1354 container_of(work, struct orinoco_private, join_work);
1355 struct net_device *dev = priv->ndev;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001356 struct hermes *hw = &priv->hw;
1357 int err;
1358 unsigned long flags;
1359 struct join_req {
1360 u8 bssid[ETH_ALEN];
Pavel Roskind133ae42005-09-23 04:18:06 -04001361 __le16 channel;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001362 } __attribute__ ((packed)) req;
1363 const int atom_len = offsetof(struct prism2_scan_apinfo, atim);
Pavel Roskinc89cc222005-09-01 20:06:06 -04001364 struct prism2_scan_apinfo *atom = NULL;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001365 int offset = 4;
Pavel Roskinc89cc222005-09-01 20:06:06 -04001366 int found = 0;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001367 u8 *buf;
1368 u16 len;
1369
1370 /* Allocate buffer for scan results */
1371 buf = kmalloc(MAX_SCAN_LEN, GFP_KERNEL);
1372 if (! buf)
1373 return;
1374
1375 if (orinoco_lock(priv, &flags) != 0)
Pavel Roskinf3cb4cc2005-09-23 04:18:06 -04001376 goto fail_lock;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001377
1378 /* Sanity checks in case user changed something in the meantime */
1379 if (! priv->bssid_fixed)
1380 goto out;
1381
1382 if (strlen(priv->desired_essid) == 0)
1383 goto out;
1384
1385 /* Read scan results from the firmware */
1386 err = hermes_read_ltv(hw, USER_BAP,
1387 HERMES_RID_SCANRESULTSTABLE,
1388 MAX_SCAN_LEN, &len, buf);
1389 if (err) {
1390 printk(KERN_ERR "%s: Cannot read scan results\n",
1391 dev->name);
1392 goto out;
1393 }
1394
1395 len = HERMES_RECLEN_TO_BYTES(len);
1396
1397 /* Go through the scan results looking for the channel of the AP
1398 * we were requested to join */
1399 for (; offset + atom_len <= len; offset += atom_len) {
1400 atom = (struct prism2_scan_apinfo *) (buf + offset);
Pavel Roskinc89cc222005-09-01 20:06:06 -04001401 if (memcmp(&atom->bssid, priv->desired_bssid, ETH_ALEN) == 0) {
1402 found = 1;
1403 break;
1404 }
Christoph Hellwig16739b02005-06-19 01:27:51 +02001405 }
1406
Pavel Roskinc89cc222005-09-01 20:06:06 -04001407 if (! found) {
1408 DEBUG(1, "%s: Requested AP not found in scan results\n",
1409 dev->name);
1410 goto out;
1411 }
Christoph Hellwig16739b02005-06-19 01:27:51 +02001412
Christoph Hellwig16739b02005-06-19 01:27:51 +02001413 memcpy(req.bssid, priv->desired_bssid, ETH_ALEN);
1414 req.channel = atom->channel; /* both are little-endian */
1415 err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNFJOINREQUEST,
1416 &req);
1417 if (err)
1418 printk(KERN_ERR "%s: Error issuing join request\n", dev->name);
1419
1420 out:
Christoph Hellwig16739b02005-06-19 01:27:51 +02001421 orinoco_unlock(priv, &flags);
Pavel Roskinf3cb4cc2005-09-23 04:18:06 -04001422
1423 fail_lock:
1424 kfree(buf);
Christoph Hellwig16739b02005-06-19 01:27:51 +02001425}
1426
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001427/* Send new BSSID to userspace */
David Kilroy6cd90b12008-08-21 23:28:00 +01001428static void orinoco_send_bssid_wevent(struct orinoco_private *priv)
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001429{
David Howellsc4028952006-11-22 14:57:56 +00001430 struct net_device *dev = priv->ndev;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001431 struct hermes *hw = &priv->hw;
1432 union iwreq_data wrqu;
1433 int err;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001434
1435 err = hermes_read_ltv(hw, IRQ_BAP, HERMES_RID_CURRENTBSSID,
1436 ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
1437 if (err != 0)
David Kilroy6cd90b12008-08-21 23:28:00 +01001438 return;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001439
1440 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1441
1442 /* Send event to user space */
1443 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001444}
1445
David Kilroy06009fd2008-08-21 23:28:03 +01001446static void orinoco_send_assocreqie_wevent(struct orinoco_private *priv)
1447{
1448 struct net_device *dev = priv->ndev;
1449 struct hermes *hw = &priv->hw;
1450 union iwreq_data wrqu;
1451 int err;
1452 u8 buf[88];
1453 u8 *ie;
1454
1455 if (!priv->has_wpa)
1456 return;
1457
1458 err = hermes_read_ltv(hw, IRQ_BAP, HERMES_RID_CURRENT_ASSOC_REQ_INFO,
1459 sizeof(buf), NULL, &buf);
1460 if (err != 0)
1461 return;
1462
1463 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1464 if (ie) {
1465 int rem = sizeof(buf) - (ie - &buf[0]);
1466 wrqu.data.length = ie[1] + 2;
1467 if (wrqu.data.length > rem)
1468 wrqu.data.length = rem;
1469
1470 if (wrqu.data.length)
1471 /* Send event to user space */
1472 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, ie);
1473 }
1474}
1475
1476static void orinoco_send_assocrespie_wevent(struct orinoco_private *priv)
1477{
1478 struct net_device *dev = priv->ndev;
1479 struct hermes *hw = &priv->hw;
1480 union iwreq_data wrqu;
1481 int err;
1482 u8 buf[88]; /* TODO: verify max size or IW_GENERIC_IE_MAX */
1483 u8 *ie;
1484
1485 if (!priv->has_wpa)
1486 return;
1487
1488 err = hermes_read_ltv(hw, IRQ_BAP, HERMES_RID_CURRENT_ASSOC_RESP_INFO,
1489 sizeof(buf), NULL, &buf);
1490 if (err != 0)
1491 return;
1492
1493 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1494 if (ie) {
1495 int rem = sizeof(buf) - (ie - &buf[0]);
1496 wrqu.data.length = ie[1] + 2;
1497 if (wrqu.data.length > rem)
1498 wrqu.data.length = rem;
1499
1500 if (wrqu.data.length)
1501 /* Send event to user space */
1502 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, ie);
1503 }
1504}
1505
David Kilroy6cd90b12008-08-21 23:28:00 +01001506static void orinoco_send_wevents(struct work_struct *work)
1507{
1508 struct orinoco_private *priv =
1509 container_of(work, struct orinoco_private, wevent_work);
1510 unsigned long flags;
1511
1512 if (orinoco_lock(priv, &flags) != 0)
1513 return;
1514
David Kilroy06009fd2008-08-21 23:28:03 +01001515 orinoco_send_assocreqie_wevent(priv);
1516 orinoco_send_assocrespie_wevent(priv);
David Kilroy6cd90b12008-08-21 23:28:00 +01001517 orinoco_send_bssid_wevent(priv);
1518
1519 orinoco_unlock(priv, &flags);
1520}
Dan Williams1e3428e2007-10-10 23:56:25 -04001521
1522static inline void orinoco_clear_scan_results(struct orinoco_private *priv,
1523 unsigned long scan_age)
1524{
David Kilroy01632fa2008-08-21 23:27:58 +01001525 if (priv->has_ext_scan) {
1526 struct xbss_element *bss;
1527 struct xbss_element *tmp_bss;
Dan Williams1e3428e2007-10-10 23:56:25 -04001528
David Kilroy01632fa2008-08-21 23:27:58 +01001529 /* Blow away current list of scan results */
1530 list_for_each_entry_safe(bss, tmp_bss, &priv->bss_list, list) {
1531 if (!scan_age ||
1532 time_after(jiffies, bss->last_scanned + scan_age)) {
1533 list_move_tail(&bss->list,
1534 &priv->bss_free_list);
1535 /* Don't blow away ->list, just BSS data */
1536 memset(&bss->bss, 0, sizeof(bss->bss));
1537 bss->last_scanned = 0;
1538 }
Dan Williams1e3428e2007-10-10 23:56:25 -04001539 }
David Kilroy01632fa2008-08-21 23:27:58 +01001540 } else {
1541 struct bss_element *bss;
1542 struct bss_element *tmp_bss;
1543
1544 /* Blow away current list of scan results */
1545 list_for_each_entry_safe(bss, tmp_bss, &priv->bss_list, list) {
1546 if (!scan_age ||
1547 time_after(jiffies, bss->last_scanned + scan_age)) {
1548 list_move_tail(&bss->list,
1549 &priv->bss_free_list);
1550 /* Don't blow away ->list, just BSS data */
1551 memset(&bss->bss, 0, sizeof(bss->bss));
1552 bss->last_scanned = 0;
1553 }
1554 }
1555 }
1556}
1557
1558static void orinoco_add_ext_scan_result(struct orinoco_private *priv,
1559 struct agere_ext_scan_info *atom)
1560{
1561 struct xbss_element *bss = NULL;
1562 int found = 0;
1563
1564 /* Try to update an existing bss first */
1565 list_for_each_entry(bss, &priv->bss_list, list) {
1566 if (compare_ether_addr(bss->bss.bssid, atom->bssid))
1567 continue;
1568 /* ESSID lengths */
1569 if (bss->bss.data[1] != atom->data[1])
1570 continue;
1571 if (memcmp(&bss->bss.data[2], &atom->data[2],
1572 atom->data[1]))
1573 continue;
1574 found = 1;
1575 break;
1576 }
1577
1578 /* Grab a bss off the free list */
1579 if (!found && !list_empty(&priv->bss_free_list)) {
1580 bss = list_entry(priv->bss_free_list.next,
1581 struct xbss_element, list);
1582 list_del(priv->bss_free_list.next);
1583
1584 list_add_tail(&bss->list, &priv->bss_list);
1585 }
1586
1587 if (bss) {
1588 /* Always update the BSS to get latest beacon info */
1589 memcpy(&bss->bss, atom, sizeof(bss->bss));
1590 bss->last_scanned = jiffies;
Dan Williams1e3428e2007-10-10 23:56:25 -04001591 }
1592}
1593
1594static int orinoco_process_scan_results(struct net_device *dev,
1595 unsigned char *buf,
1596 int len)
1597{
1598 struct orinoco_private *priv = netdev_priv(dev);
1599 int offset; /* In the scan data */
1600 union hermes_scan_info *atom;
1601 int atom_len;
1602
1603 switch (priv->firmware_type) {
1604 case FIRMWARE_TYPE_AGERE:
1605 atom_len = sizeof(struct agere_scan_apinfo);
1606 offset = 0;
1607 break;
1608 case FIRMWARE_TYPE_SYMBOL:
1609 /* Lack of documentation necessitates this hack.
1610 * Different firmwares have 68 or 76 byte long atoms.
1611 * We try modulo first. If the length divides by both,
1612 * we check what would be the channel in the second
1613 * frame for a 68-byte atom. 76-byte atoms have 0 there.
1614 * Valid channel cannot be 0. */
1615 if (len % 76)
1616 atom_len = 68;
1617 else if (len % 68)
1618 atom_len = 76;
1619 else if (len >= 1292 && buf[68] == 0)
1620 atom_len = 76;
1621 else
1622 atom_len = 68;
1623 offset = 0;
1624 break;
1625 case FIRMWARE_TYPE_INTERSIL:
1626 offset = 4;
1627 if (priv->has_hostscan) {
1628 atom_len = le16_to_cpup((__le16 *)buf);
1629 /* Sanity check for atom_len */
1630 if (atom_len < sizeof(struct prism2_scan_apinfo)) {
1631 printk(KERN_ERR "%s: Invalid atom_len in scan "
1632 "data: %d\n", dev->name, atom_len);
1633 return -EIO;
1634 }
1635 } else
1636 atom_len = offsetof(struct prism2_scan_apinfo, atim);
1637 break;
1638 default:
1639 return -EOPNOTSUPP;
1640 }
1641
1642 /* Check that we got an whole number of atoms */
1643 if ((len - offset) % atom_len) {
1644 printk(KERN_ERR "%s: Unexpected scan data length %d, "
1645 "atom_len %d, offset %d\n", dev->name, len,
1646 atom_len, offset);
1647 return -EIO;
1648 }
1649
1650 orinoco_clear_scan_results(priv, msecs_to_jiffies(15000));
1651
1652 /* Read the entries one by one */
1653 for (; offset + atom_len <= len; offset += atom_len) {
1654 int found = 0;
David Kilroy3056c402008-08-21 23:27:57 +01001655 struct bss_element *bss = NULL;
Dan Williams1e3428e2007-10-10 23:56:25 -04001656
1657 /* Get next atom */
1658 atom = (union hermes_scan_info *) (buf + offset);
1659
1660 /* Try to update an existing bss first */
1661 list_for_each_entry(bss, &priv->bss_list, list) {
1662 if (compare_ether_addr(bss->bss.a.bssid, atom->a.bssid))
1663 continue;
1664 if (le16_to_cpu(bss->bss.a.essid_len) !=
1665 le16_to_cpu(atom->a.essid_len))
1666 continue;
1667 if (memcmp(bss->bss.a.essid, atom->a.essid,
1668 le16_to_cpu(atom->a.essid_len)))
1669 continue;
Dan Williams1e3428e2007-10-10 23:56:25 -04001670 found = 1;
1671 break;
1672 }
1673
1674 /* Grab a bss off the free list */
1675 if (!found && !list_empty(&priv->bss_free_list)) {
1676 bss = list_entry(priv->bss_free_list.next,
David Kilroy3056c402008-08-21 23:27:57 +01001677 struct bss_element, list);
Dan Williams1e3428e2007-10-10 23:56:25 -04001678 list_del(priv->bss_free_list.next);
1679
Dan Williams1e3428e2007-10-10 23:56:25 -04001680 list_add_tail(&bss->list, &priv->bss_list);
1681 }
Dan Williams22367612007-12-05 11:01:23 -05001682
1683 if (bss) {
1684 /* Always update the BSS to get latest beacon info */
1685 memcpy(&bss->bss, atom, sizeof(bss->bss));
1686 bss->last_scanned = jiffies;
1687 }
Dan Williams1e3428e2007-10-10 23:56:25 -04001688 }
1689
1690 return 0;
1691}
1692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693static void __orinoco_ev_info(struct net_device *dev, hermes_t *hw)
1694{
1695 struct orinoco_private *priv = netdev_priv(dev);
1696 u16 infofid;
1697 struct {
Pavel Roskind133ae42005-09-23 04:18:06 -04001698 __le16 len;
1699 __le16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 } __attribute__ ((packed)) info;
1701 int len, type;
1702 int err;
1703
1704 /* This is an answer to an INQUIRE command that we did earlier,
1705 * or an information "event" generated by the card
1706 * The controller return to us a pseudo frame containing
1707 * the information in question - Jean II */
1708 infofid = hermes_read_regn(hw, INFOFID);
1709
1710 /* Read the info frame header - don't try too hard */
1711 err = hermes_bap_pread(hw, IRQ_BAP, &info, sizeof(info),
1712 infofid, 0);
1713 if (err) {
1714 printk(KERN_ERR "%s: error %d reading info frame. "
1715 "Frame dropped.\n", dev->name, err);
1716 return;
1717 }
1718
1719 len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
1720 type = le16_to_cpu(info.type);
1721
1722 switch (type) {
1723 case HERMES_INQ_TALLIES: {
1724 struct hermes_tallies_frame tallies;
1725 struct iw_statistics *wstats = &priv->wstats;
1726
1727 if (len > sizeof(tallies)) {
1728 printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
1729 dev->name, len);
1730 len = sizeof(tallies);
1731 }
1732
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001733 err = hermes_bap_pread(hw, IRQ_BAP, &tallies, len,
1734 infofid, sizeof(info));
1735 if (err)
1736 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738 /* Increment our various counters */
1739 /* wstats->discard.nwid - no wrong BSSID stuff */
1740 wstats->discard.code +=
1741 le16_to_cpu(tallies.RxWEPUndecryptable);
1742 if (len == sizeof(tallies))
1743 wstats->discard.code +=
1744 le16_to_cpu(tallies.RxDiscards_WEPICVError) +
1745 le16_to_cpu(tallies.RxDiscards_WEPExcluded);
1746 wstats->discard.misc +=
1747 le16_to_cpu(tallies.TxDiscardsWrongSA);
1748 wstats->discard.fragment +=
1749 le16_to_cpu(tallies.RxMsgInBadMsgFragments);
1750 wstats->discard.retries +=
1751 le16_to_cpu(tallies.TxRetryLimitExceeded);
1752 /* wstats->miss.beacon - no match */
1753 }
1754 break;
1755 case HERMES_INQ_LINKSTATUS: {
1756 struct hermes_linkstatus linkstatus;
1757 u16 newstatus;
1758 int connected;
1759
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001760 if (priv->iw_mode == IW_MODE_MONITOR)
1761 break;
1762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 if (len != sizeof(linkstatus)) {
1764 printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
1765 dev->name, len);
1766 break;
1767 }
1768
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001769 err = hermes_bap_pread(hw, IRQ_BAP, &linkstatus, len,
1770 infofid, sizeof(info));
1771 if (err)
1772 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 newstatus = le16_to_cpu(linkstatus.linkstatus);
1774
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001775 /* Symbol firmware uses "out of range" to signal that
1776 * the hostscan frame can be requested. */
1777 if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
1778 priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
1779 priv->has_hostscan && priv->scan_inprogress) {
1780 hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
1781 break;
1782 }
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
1785 || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
1786 || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
1787
1788 if (connected)
1789 netif_carrier_on(dev);
David Gibson7bb7c3a2005-05-12 20:02:10 -04001790 else if (!ignore_disconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 netif_carrier_off(dev);
1792
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001793 if (newstatus != priv->last_linkstatus) {
1794 priv->last_linkstatus = newstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 print_linkstatus(dev, newstatus);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001796 /* The info frame contains only one word which is the
1797 * status (see hermes.h). The status is pretty boring
1798 * in itself, that's why we export the new BSSID...
1799 * Jean II */
1800 schedule_work(&priv->wevent_work);
1801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 }
1803 break;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001804 case HERMES_INQ_SCAN:
1805 if (!priv->scan_inprogress && priv->bssid_fixed &&
1806 priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
1807 schedule_work(&priv->join_work);
1808 break;
1809 }
1810 /* fall through */
1811 case HERMES_INQ_HOSTSCAN:
1812 case HERMES_INQ_HOSTSCAN_SYMBOL: {
1813 /* Result of a scanning. Contains information about
1814 * cells in the vicinity - Jean II */
1815 union iwreq_data wrqu;
1816 unsigned char *buf;
1817
Dan Williams1e3428e2007-10-10 23:56:25 -04001818 /* Scan is no longer in progress */
1819 priv->scan_inprogress = 0;
1820
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001821 /* Sanity check */
1822 if (len > 4096) {
1823 printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
1824 dev->name, len);
1825 break;
1826 }
1827
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001828 /* Allocate buffer for results */
1829 buf = kmalloc(len, GFP_ATOMIC);
1830 if (buf == NULL)
1831 /* No memory, so can't printk()... */
1832 break;
1833
1834 /* Read scan data */
1835 err = hermes_bap_pread(hw, IRQ_BAP, (void *) buf, len,
1836 infofid, sizeof(info));
Pavel Roskin708218b2005-09-01 20:05:19 -04001837 if (err) {
1838 kfree(buf);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001839 break;
Pavel Roskin708218b2005-09-01 20:05:19 -04001840 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001841
1842#ifdef ORINOCO_DEBUG
1843 {
1844 int i;
1845 printk(KERN_DEBUG "Scan result [%02X", buf[0]);
1846 for(i = 1; i < (len * 2); i++)
1847 printk(":%02X", buf[i]);
1848 printk("]\n");
1849 }
1850#endif /* ORINOCO_DEBUG */
1851
Dan Williams1e3428e2007-10-10 23:56:25 -04001852 if (orinoco_process_scan_results(dev, buf, len) == 0) {
1853 /* Send an empty event to user space.
1854 * We don't send the received data on the event because
1855 * it would require us to do complex transcoding, and
1856 * we want to minimise the work done in the irq handler
1857 * Use a request to extract the data - Jean II */
1858 wrqu.data.length = 0;
1859 wrqu.data.flags = 0;
1860 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
1861 }
1862 kfree(buf);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001863 }
1864 break;
David Kilroy01632fa2008-08-21 23:27:58 +01001865 case HERMES_INQ_CHANNELINFO:
1866 {
1867 struct agere_ext_scan_info *bss;
1868
1869 if (!priv->scan_inprogress) {
1870 printk(KERN_DEBUG "%s: Got chaninfo without scan, "
1871 "len=%d\n", dev->name, len);
1872 break;
1873 }
1874
1875 /* An empty result indicates that the scan is complete */
1876 if (len == 0) {
1877 union iwreq_data wrqu;
1878
1879 /* Scan is no longer in progress */
1880 priv->scan_inprogress = 0;
1881
1882 wrqu.data.length = 0;
1883 wrqu.data.flags = 0;
1884 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
1885 break;
1886 }
1887
1888 /* Sanity check */
1889 else if (len > sizeof(*bss)) {
1890 printk(KERN_WARNING
1891 "%s: Ext scan results too large (%d bytes). "
1892 "Truncating results to %zd bytes.\n",
1893 dev->name, len, sizeof(*bss));
1894 len = sizeof(*bss);
1895 } else if (len < (offsetof(struct agere_ext_scan_info,
1896 data) + 2)) {
1897 /* Drop this result now so we don't have to
1898 * keep checking later */
1899 printk(KERN_WARNING
1900 "%s: Ext scan results too short (%d bytes)\n",
1901 dev->name, len);
1902 break;
1903 }
1904
1905 bss = kmalloc(sizeof(*bss), GFP_ATOMIC);
1906 if (bss == NULL)
1907 break;
1908
1909 /* Read scan data */
1910 err = hermes_bap_pread(hw, IRQ_BAP, (void *) bss, len,
1911 infofid, sizeof(info));
1912 if (err) {
1913 kfree(bss);
1914 break;
1915 }
1916
1917 orinoco_add_ext_scan_result(priv, bss);
1918
1919 kfree(bss);
1920 break;
1921 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001922 case HERMES_INQ_SEC_STAT_AGERE:
1923 /* Security status (Agere specific) */
1924 /* Ignore this frame for now */
1925 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
1926 break;
1927 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 default:
1929 printk(KERN_DEBUG "%s: Unknown information frame received: "
1930 "type 0x%04x, length %d\n", dev->name, type, len);
1931 /* We don't actually do anything about it */
1932 break;
1933 }
1934}
1935
1936static void __orinoco_ev_infdrop(struct net_device *dev, hermes_t *hw)
1937{
1938 if (net_ratelimit())
1939 printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
1940}
1941
1942/********************************************************************/
1943/* Internal hardware control routines */
1944/********************************************************************/
1945
1946int __orinoco_up(struct net_device *dev)
1947{
1948 struct orinoco_private *priv = netdev_priv(dev);
1949 struct hermes *hw = &priv->hw;
1950 int err;
1951
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001952 netif_carrier_off(dev); /* just to make sure */
1953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 err = __orinoco_program_rids(dev);
1955 if (err) {
1956 printk(KERN_ERR "%s: Error %d configuring card\n",
1957 dev->name, err);
1958 return err;
1959 }
1960
1961 /* Fire things up again */
1962 hermes_set_irqmask(hw, ORINOCO_INTEN);
1963 err = hermes_enable_port(hw, 0);
1964 if (err) {
1965 printk(KERN_ERR "%s: Error %d enabling MAC port\n",
1966 dev->name, err);
1967 return err;
1968 }
1969
1970 netif_start_queue(dev);
1971
1972 return 0;
1973}
1974
1975int __orinoco_down(struct net_device *dev)
1976{
1977 struct orinoco_private *priv = netdev_priv(dev);
1978 struct hermes *hw = &priv->hw;
1979 int err;
1980
1981 netif_stop_queue(dev);
1982
1983 if (! priv->hw_unavailable) {
1984 if (! priv->broken_disableport) {
1985 err = hermes_disable_port(hw, 0);
1986 if (err) {
1987 /* Some firmwares (e.g. Intersil 1.3.x) seem
1988 * to have problems disabling the port, oh
1989 * well, too bad. */
1990 printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
1991 dev->name, err);
1992 priv->broken_disableport = 1;
1993 }
1994 }
1995 hermes_set_irqmask(hw, 0);
1996 hermes_write_regn(hw, EVACK, 0xffff);
1997 }
1998
1999 /* firmware will have to reassociate */
2000 netif_carrier_off(dev);
2001 priv->last_linkstatus = 0xffff;
2002
2003 return 0;
2004}
2005
Pavel Roskin37a6c612006-04-07 04:10:49 -04002006static int orinoco_allocate_fid(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007{
2008 struct orinoco_private *priv = netdev_priv(dev);
2009 struct hermes *hw = &priv->hw;
2010 int err;
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 err = hermes_allocate(hw, priv->nicbuf_size, &priv->txfid);
David Gibsonb24d4582005-05-12 20:04:16 -04002013 if (err == -EIO && priv->nicbuf_size > TX_NICBUF_SIZE_BUG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 /* Try workaround for old Symbol firmware bug */
2015 printk(KERN_WARNING "%s: firmware ALLOC bug detected "
2016 "(old Symbol firmware?). Trying to work around... ",
2017 dev->name);
2018
2019 priv->nicbuf_size = TX_NICBUF_SIZE_BUG;
2020 err = hermes_allocate(hw, priv->nicbuf_size, &priv->txfid);
2021 if (err)
2022 printk("failed!\n");
2023 else
2024 printk("ok.\n");
2025 }
2026
2027 return err;
2028}
2029
Pavel Roskin37a6c612006-04-07 04:10:49 -04002030int orinoco_reinit_firmware(struct net_device *dev)
2031{
2032 struct orinoco_private *priv = netdev_priv(dev);
2033 struct hermes *hw = &priv->hw;
2034 int err;
2035
2036 err = hermes_init(hw);
2037 if (!err)
2038 err = orinoco_allocate_fid(dev);
2039
2040 return err;
2041}
2042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043static int __orinoco_hw_set_bitrate(struct orinoco_private *priv)
2044{
2045 hermes_t *hw = &priv->hw;
2046 int err = 0;
2047
2048 if (priv->bitratemode >= BITRATE_TABLE_SIZE) {
2049 printk(KERN_ERR "%s: BUG: Invalid bitrate mode %d\n",
2050 priv->ndev->name, priv->bitratemode);
2051 return -EINVAL;
2052 }
2053
2054 switch (priv->firmware_type) {
2055 case FIRMWARE_TYPE_AGERE:
2056 err = hermes_write_wordrec(hw, USER_BAP,
2057 HERMES_RID_CNFTXRATECONTROL,
2058 bitrate_table[priv->bitratemode].agere_txratectrl);
2059 break;
2060 case FIRMWARE_TYPE_INTERSIL:
2061 case FIRMWARE_TYPE_SYMBOL:
2062 err = hermes_write_wordrec(hw, USER_BAP,
2063 HERMES_RID_CNFTXRATECONTROL,
2064 bitrate_table[priv->bitratemode].intersil_txratectrl);
2065 break;
2066 default:
2067 BUG();
2068 }
2069
2070 return err;
2071}
2072
Christoph Hellwig16739b02005-06-19 01:27:51 +02002073/* Set fixed AP address */
2074static int __orinoco_hw_set_wap(struct orinoco_private *priv)
2075{
2076 int roaming_flag;
2077 int err = 0;
2078 hermes_t *hw = &priv->hw;
2079
2080 switch (priv->firmware_type) {
2081 case FIRMWARE_TYPE_AGERE:
2082 /* not supported */
2083 break;
2084 case FIRMWARE_TYPE_INTERSIL:
2085 if (priv->bssid_fixed)
2086 roaming_flag = 2;
2087 else
2088 roaming_flag = 1;
2089
2090 err = hermes_write_wordrec(hw, USER_BAP,
2091 HERMES_RID_CNFROAMINGMODE,
2092 roaming_flag);
2093 break;
2094 case FIRMWARE_TYPE_SYMBOL:
2095 err = HERMES_WRITE_RECORD(hw, USER_BAP,
2096 HERMES_RID_CNFMANDATORYBSSID_SYMBOL,
2097 &priv->desired_bssid);
2098 break;
2099 }
2100 return err;
2101}
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103/* Change the WEP keys and/or the current keys. Can be called
David Kilroyd03032a2008-08-21 23:28:02 +01002104 * either from __orinoco_hw_setup_enc() or directly from
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 * orinoco_ioctl_setiwencode(). In the later case the association
2106 * with the AP is not broken (if the firmware can handle it),
2107 * which is needed for 802.1x implementations. */
2108static int __orinoco_hw_setup_wepkeys(struct orinoco_private *priv)
2109{
2110 hermes_t *hw = &priv->hw;
2111 int err = 0;
2112
2113 switch (priv->firmware_type) {
2114 case FIRMWARE_TYPE_AGERE:
2115 err = HERMES_WRITE_RECORD(hw, USER_BAP,
2116 HERMES_RID_CNFWEPKEYS_AGERE,
2117 &priv->keys);
2118 if (err)
2119 return err;
2120 err = hermes_write_wordrec(hw, USER_BAP,
2121 HERMES_RID_CNFTXKEY_AGERE,
2122 priv->tx_key);
2123 if (err)
2124 return err;
2125 break;
2126 case FIRMWARE_TYPE_INTERSIL:
2127 case FIRMWARE_TYPE_SYMBOL:
2128 {
2129 int keylen;
2130 int i;
2131
2132 /* Force uniform key length to work around firmware bugs */
2133 keylen = le16_to_cpu(priv->keys[priv->tx_key].len);
2134
2135 if (keylen > LARGE_KEY_SIZE) {
2136 printk(KERN_ERR "%s: BUG: Key %d has oversize length %d.\n",
2137 priv->ndev->name, priv->tx_key, keylen);
2138 return -E2BIG;
2139 }
2140
2141 /* Write all 4 keys */
2142 for(i = 0; i < ORINOCO_MAX_KEYS; i++) {
2143 err = hermes_write_ltv(hw, USER_BAP,
2144 HERMES_RID_CNFDEFAULTKEY0 + i,
2145 HERMES_BYTES_TO_RECLEN(keylen),
2146 priv->keys[i].data);
2147 if (err)
2148 return err;
2149 }
2150
2151 /* Write the index of the key used in transmission */
2152 err = hermes_write_wordrec(hw, USER_BAP,
2153 HERMES_RID_CNFWEPDEFAULTKEYID,
2154 priv->tx_key);
2155 if (err)
2156 return err;
2157 }
2158 break;
2159 }
2160
2161 return 0;
2162}
2163
David Kilroyd03032a2008-08-21 23:28:02 +01002164static int __orinoco_hw_setup_enc(struct orinoco_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
2166 hermes_t *hw = &priv->hw;
2167 int err = 0;
2168 int master_wep_flag;
2169 int auth_flag;
David Kilroy4ae6ee22008-08-21 23:27:59 +01002170 int enc_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
David Kilroyd03032a2008-08-21 23:28:02 +01002172 /* Setup WEP keys for WEP and WPA */
2173 if (priv->encode_alg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 __orinoco_hw_setup_wepkeys(priv);
2175
2176 if (priv->wep_restrict)
2177 auth_flag = HERMES_AUTH_SHARED_KEY;
2178 else
2179 auth_flag = HERMES_AUTH_OPEN;
2180
David Kilroyd03032a2008-08-21 23:28:02 +01002181 if (priv->wpa_enabled)
2182 enc_flag = 2;
2183 else if (priv->encode_alg == IW_ENCODE_ALG_WEP)
David Kilroy4ae6ee22008-08-21 23:27:59 +01002184 enc_flag = 1;
2185 else
2186 enc_flag = 0;
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 switch (priv->firmware_type) {
2189 case FIRMWARE_TYPE_AGERE: /* Agere style WEP */
David Kilroy4ae6ee22008-08-21 23:27:59 +01002190 if (priv->encode_alg == IW_ENCODE_ALG_WEP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 /* Enable the shared-key authentication. */
2192 err = hermes_write_wordrec(hw, USER_BAP,
2193 HERMES_RID_CNFAUTHENTICATION_AGERE,
2194 auth_flag);
2195 }
2196 err = hermes_write_wordrec(hw, USER_BAP,
2197 HERMES_RID_CNFWEPENABLED_AGERE,
David Kilroy4ae6ee22008-08-21 23:27:59 +01002198 enc_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 if (err)
2200 return err;
David Kilroyd03032a2008-08-21 23:28:02 +01002201
2202 if (priv->has_wpa) {
2203 /* Set WPA key management */
2204 err = hermes_write_wordrec(hw, USER_BAP,
2205 HERMES_RID_CNFSETWPAAUTHMGMTSUITE_AGERE,
2206 priv->key_mgmt);
2207 if (err)
2208 return err;
2209 }
2210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 break;
2212
2213 case FIRMWARE_TYPE_INTERSIL: /* Intersil style WEP */
2214 case FIRMWARE_TYPE_SYMBOL: /* Symbol style WEP */
David Kilroy4ae6ee22008-08-21 23:27:59 +01002215 if (priv->encode_alg == IW_ENCODE_ALG_WEP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 if (priv->wep_restrict ||
2217 (priv->firmware_type == FIRMWARE_TYPE_SYMBOL))
2218 master_wep_flag = HERMES_WEP_PRIVACY_INVOKED |
2219 HERMES_WEP_EXCL_UNENCRYPTED;
2220 else
2221 master_wep_flag = HERMES_WEP_PRIVACY_INVOKED;
2222
2223 err = hermes_write_wordrec(hw, USER_BAP,
2224 HERMES_RID_CNFAUTHENTICATION,
2225 auth_flag);
2226 if (err)
2227 return err;
2228 } else
2229 master_wep_flag = 0;
2230
2231 if (priv->iw_mode == IW_MODE_MONITOR)
2232 master_wep_flag |= HERMES_WEP_HOST_DECRYPT;
2233
2234 /* Master WEP setting : on/off */
2235 err = hermes_write_wordrec(hw, USER_BAP,
2236 HERMES_RID_CNFWEPFLAGS_INTERSIL,
2237 master_wep_flag);
2238 if (err)
2239 return err;
2240
2241 break;
2242 }
2243
2244 return 0;
2245}
2246
David Kilroyd03032a2008-08-21 23:28:02 +01002247/* key must be 32 bytes, including the tx and rx MIC keys.
2248 * rsc must be 8 bytes
2249 * tsc must be 8 bytes or NULL
2250 */
2251static int __orinoco_hw_set_tkip_key(hermes_t *hw, int key_idx, int set_tx,
2252 u8 *key, u8 *rsc, u8 *tsc)
2253{
2254 struct {
2255 __le16 idx;
2256 u8 rsc[IW_ENCODE_SEQ_MAX_SIZE];
2257 u8 key[TKIP_KEYLEN];
2258 u8 tx_mic[MIC_KEYLEN];
2259 u8 rx_mic[MIC_KEYLEN];
2260 u8 tsc[IW_ENCODE_SEQ_MAX_SIZE];
2261 } __attribute__ ((packed)) buf;
2262 int ret;
2263 int err;
2264 int k;
2265 u16 xmitting;
2266
2267 key_idx &= 0x3;
2268
2269 if (set_tx)
2270 key_idx |= 0x8000;
2271
2272 buf.idx = cpu_to_le16(key_idx);
2273 memcpy(buf.key, key,
2274 sizeof(buf.key) + sizeof(buf.tx_mic) + sizeof(buf.rx_mic));
2275
2276 if (rsc == NULL)
2277 memset(buf.rsc, 0, sizeof(buf.rsc));
2278 else
2279 memcpy(buf.rsc, rsc, sizeof(buf.rsc));
2280
2281 if (tsc == NULL) {
2282 memset(buf.tsc, 0, sizeof(buf.tsc));
2283 buf.tsc[4] = 0x10;
2284 } else {
2285 memcpy(buf.tsc, tsc, sizeof(buf.tsc));
2286 }
2287
2288 /* Wait upto 100ms for tx queue to empty */
2289 k = 100;
2290 do {
2291 k--;
2292 udelay(1000);
2293 ret = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_TXQUEUEEMPTY,
2294 &xmitting);
2295 if (ret)
2296 break;
2297 } while ((k > 0) && xmitting);
2298
2299 if (k == 0)
2300 ret = -ETIMEDOUT;
2301
2302 err = HERMES_WRITE_RECORD(hw, USER_BAP,
2303 HERMES_RID_CNFADDDEFAULTTKIPKEY_AGERE,
2304 &buf);
2305
2306 return ret ? ret : err;
2307}
2308
2309static int orinoco_clear_tkip_key(struct orinoco_private *priv,
2310 int key_idx)
2311{
2312 hermes_t *hw = &priv->hw;
2313 int err;
2314
2315 memset(&priv->tkip_key[key_idx], 0, sizeof(priv->tkip_key[key_idx]));
2316 err = hermes_write_wordrec(hw, USER_BAP,
2317 HERMES_RID_CNFREMDEFAULTTKIPKEY_AGERE,
2318 key_idx);
2319 if (err)
2320 printk(KERN_WARNING "%s: Error %d clearing TKIP key %d\n",
2321 priv->ndev->name, err, key_idx);
2322 return err;
2323}
2324
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325static int __orinoco_program_rids(struct net_device *dev)
2326{
2327 struct orinoco_private *priv = netdev_priv(dev);
2328 hermes_t *hw = &priv->hw;
2329 int err;
2330 struct hermes_idstring idbuf;
2331
2332 /* Set the MAC address */
2333 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
2334 HERMES_BYTES_TO_RECLEN(ETH_ALEN), dev->dev_addr);
2335 if (err) {
2336 printk(KERN_ERR "%s: Error %d setting MAC address\n",
2337 dev->name, err);
2338 return err;
2339 }
2340
2341 /* Set up the link mode */
2342 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFPORTTYPE,
2343 priv->port_type);
2344 if (err) {
2345 printk(KERN_ERR "%s: Error %d setting port type\n",
2346 dev->name, err);
2347 return err;
2348 }
2349 /* Set the channel/frequency */
David Gibsond51d8b12005-05-12 20:03:36 -04002350 if (priv->channel != 0 && priv->iw_mode != IW_MODE_INFRA) {
2351 err = hermes_write_wordrec(hw, USER_BAP,
2352 HERMES_RID_CNFOWNCHANNEL,
2353 priv->channel);
2354 if (err) {
2355 printk(KERN_ERR "%s: Error %d setting channel %d\n",
2356 dev->name, err, priv->channel);
2357 return err;
2358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 }
2360
2361 if (priv->has_ibss) {
2362 u16 createibss;
2363
2364 if ((strlen(priv->desired_essid) == 0) && (priv->createibss)) {
2365 printk(KERN_WARNING "%s: This firmware requires an "
2366 "ESSID in IBSS-Ad-Hoc mode.\n", dev->name);
2367 /* With wvlan_cs, in this case, we would crash.
2368 * hopefully, this driver will behave better...
2369 * Jean II */
2370 createibss = 0;
2371 } else {
2372 createibss = priv->createibss;
2373 }
2374
2375 err = hermes_write_wordrec(hw, USER_BAP,
2376 HERMES_RID_CNFCREATEIBSS,
2377 createibss);
2378 if (err) {
2379 printk(KERN_ERR "%s: Error %d setting CREATEIBSS\n",
2380 dev->name, err);
2381 return err;
2382 }
2383 }
2384
Christoph Hellwig16739b02005-06-19 01:27:51 +02002385 /* Set the desired BSSID */
2386 err = __orinoco_hw_set_wap(priv);
2387 if (err) {
2388 printk(KERN_ERR "%s: Error %d setting AP address\n",
2389 dev->name, err);
2390 return err;
2391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 /* Set the desired ESSID */
2393 idbuf.len = cpu_to_le16(strlen(priv->desired_essid));
2394 memcpy(&idbuf.val, priv->desired_essid, sizeof(idbuf.val));
2395 /* WinXP wants partner to configure OWNSSID even in IBSS mode. (jimc) */
2396 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNSSID,
2397 HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
2398 &idbuf);
2399 if (err) {
2400 printk(KERN_ERR "%s: Error %d setting OWNSSID\n",
2401 dev->name, err);
2402 return err;
2403 }
2404 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFDESIREDSSID,
2405 HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
2406 &idbuf);
2407 if (err) {
2408 printk(KERN_ERR "%s: Error %d setting DESIREDSSID\n",
2409 dev->name, err);
2410 return err;
2411 }
2412
2413 /* Set the station name */
2414 idbuf.len = cpu_to_le16(strlen(priv->nick));
2415 memcpy(&idbuf.val, priv->nick, sizeof(idbuf.val));
2416 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
2417 HERMES_BYTES_TO_RECLEN(strlen(priv->nick)+2),
2418 &idbuf);
2419 if (err) {
2420 printk(KERN_ERR "%s: Error %d setting nickname\n",
2421 dev->name, err);
2422 return err;
2423 }
2424
2425 /* Set AP density */
2426 if (priv->has_sensitivity) {
2427 err = hermes_write_wordrec(hw, USER_BAP,
2428 HERMES_RID_CNFSYSTEMSCALE,
2429 priv->ap_density);
2430 if (err) {
2431 printk(KERN_WARNING "%s: Error %d setting SYSTEMSCALE. "
2432 "Disabling sensitivity control\n",
2433 dev->name, err);
2434
2435 priv->has_sensitivity = 0;
2436 }
2437 }
2438
2439 /* Set RTS threshold */
2440 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
2441 priv->rts_thresh);
2442 if (err) {
2443 printk(KERN_ERR "%s: Error %d setting RTS threshold\n",
2444 dev->name, err);
2445 return err;
2446 }
2447
2448 /* Set fragmentation threshold or MWO robustness */
2449 if (priv->has_mwo)
2450 err = hermes_write_wordrec(hw, USER_BAP,
2451 HERMES_RID_CNFMWOROBUST_AGERE,
2452 priv->mwo_robust);
2453 else
2454 err = hermes_write_wordrec(hw, USER_BAP,
2455 HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
2456 priv->frag_thresh);
2457 if (err) {
2458 printk(KERN_ERR "%s: Error %d setting fragmentation\n",
2459 dev->name, err);
2460 return err;
2461 }
2462
2463 /* Set bitrate */
2464 err = __orinoco_hw_set_bitrate(priv);
2465 if (err) {
2466 printk(KERN_ERR "%s: Error %d setting bitrate\n",
2467 dev->name, err);
2468 return err;
2469 }
2470
2471 /* Set power management */
2472 if (priv->has_pm) {
2473 err = hermes_write_wordrec(hw, USER_BAP,
2474 HERMES_RID_CNFPMENABLED,
2475 priv->pm_on);
2476 if (err) {
2477 printk(KERN_ERR "%s: Error %d setting up PM\n",
2478 dev->name, err);
2479 return err;
2480 }
2481
2482 err = hermes_write_wordrec(hw, USER_BAP,
2483 HERMES_RID_CNFMULTICASTRECEIVE,
2484 priv->pm_mcast);
2485 if (err) {
2486 printk(KERN_ERR "%s: Error %d setting up PM\n",
2487 dev->name, err);
2488 return err;
2489 }
2490 err = hermes_write_wordrec(hw, USER_BAP,
2491 HERMES_RID_CNFMAXSLEEPDURATION,
2492 priv->pm_period);
2493 if (err) {
2494 printk(KERN_ERR "%s: Error %d setting up PM\n",
2495 dev->name, err);
2496 return err;
2497 }
2498 err = hermes_write_wordrec(hw, USER_BAP,
2499 HERMES_RID_CNFPMHOLDOVERDURATION,
2500 priv->pm_timeout);
2501 if (err) {
2502 printk(KERN_ERR "%s: Error %d setting up PM\n",
2503 dev->name, err);
2504 return err;
2505 }
2506 }
2507
2508 /* Set preamble - only for Symbol so far... */
2509 if (priv->has_preamble) {
2510 err = hermes_write_wordrec(hw, USER_BAP,
2511 HERMES_RID_CNFPREAMBLE_SYMBOL,
2512 priv->preamble);
2513 if (err) {
2514 printk(KERN_ERR "%s: Error %d setting preamble\n",
2515 dev->name, err);
2516 return err;
2517 }
2518 }
2519
2520 /* Set up encryption */
David Kilroyd03032a2008-08-21 23:28:02 +01002521 if (priv->has_wep || priv->has_wpa) {
2522 err = __orinoco_hw_setup_enc(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 if (err) {
David Kilroyd03032a2008-08-21 23:28:02 +01002524 printk(KERN_ERR "%s: Error %d activating encryption\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 dev->name, err);
2526 return err;
2527 }
2528 }
2529
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02002530 if (priv->iw_mode == IW_MODE_MONITOR) {
2531 /* Enable monitor mode */
2532 dev->type = ARPHRD_IEEE80211;
2533 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
2534 HERMES_TEST_MONITOR, 0, NULL);
2535 } else {
2536 /* Disable monitor mode */
2537 dev->type = ARPHRD_ETHER;
2538 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
2539 HERMES_TEST_STOP, 0, NULL);
2540 }
2541 if (err)
2542 return err;
2543
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 /* Set promiscuity / multicast*/
2545 priv->promiscuous = 0;
2546 priv->mc_count = 0;
Herbert Xu932ff272006-06-09 12:20:56 -07002547
2548 /* FIXME: what about netif_tx_lock */
2549 __orinoco_set_multicast_list(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
2551 return 0;
2552}
2553
2554/* FIXME: return int? */
2555static void
2556__orinoco_set_multicast_list(struct net_device *dev)
2557{
2558 struct orinoco_private *priv = netdev_priv(dev);
2559 hermes_t *hw = &priv->hw;
2560 int err = 0;
2561 int promisc, mc_count;
2562
2563 /* The Hermes doesn't seem to have an allmulti mode, so we go
2564 * into promiscuous mode and let the upper levels deal. */
2565 if ( (dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
2566 (dev->mc_count > MAX_MULTICAST(priv)) ) {
2567 promisc = 1;
2568 mc_count = 0;
2569 } else {
2570 promisc = 0;
2571 mc_count = dev->mc_count;
2572 }
2573
2574 if (promisc != priv->promiscuous) {
2575 err = hermes_write_wordrec(hw, USER_BAP,
2576 HERMES_RID_CNFPROMISCUOUSMODE,
2577 promisc);
2578 if (err) {
2579 printk(KERN_ERR "%s: Error %d setting PROMISCUOUSMODE to 1.\n",
2580 dev->name, err);
2581 } else
2582 priv->promiscuous = promisc;
2583 }
2584
2585 if (! promisc && (mc_count || priv->mc_count) ) {
2586 struct dev_mc_list *p = dev->mc_list;
2587 struct hermes_multicast mclist;
2588 int i;
2589
2590 for (i = 0; i < mc_count; i++) {
2591 /* paranoia: is list shorter than mc_count? */
2592 BUG_ON(! p);
2593 /* paranoia: bad address size in list? */
2594 BUG_ON(p->dmi_addrlen != ETH_ALEN);
2595
2596 memcpy(mclist.addr[i], p->dmi_addr, ETH_ALEN);
2597 p = p->next;
2598 }
2599
2600 if (p)
2601 printk(KERN_WARNING "%s: Multicast list is "
2602 "longer than mc_count\n", dev->name);
2603
2604 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFGROUPADDRESSES,
2605 HERMES_BYTES_TO_RECLEN(priv->mc_count * ETH_ALEN),
2606 &mclist);
2607 if (err)
2608 printk(KERN_ERR "%s: Error %d setting multicast list.\n",
2609 dev->name, err);
2610 else
2611 priv->mc_count = mc_count;
2612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613}
2614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615/* This must be called from user context, without locks held - use
2616 * schedule_work() */
David Howellsc4028952006-11-22 14:57:56 +00002617static void orinoco_reset(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618{
David Howellsc4028952006-11-22 14:57:56 +00002619 struct orinoco_private *priv =
2620 container_of(work, struct orinoco_private, reset_work);
2621 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 struct hermes *hw = &priv->hw;
Christoph Hellwig8551cb92005-05-14 17:30:04 +02002623 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 unsigned long flags;
2625
2626 if (orinoco_lock(priv, &flags) != 0)
2627 /* When the hardware becomes available again, whatever
2628 * detects that is responsible for re-initializing
2629 * it. So no need for anything further */
2630 return;
2631
2632 netif_stop_queue(dev);
2633
2634 /* Shut off interrupts. Depending on what state the hardware
2635 * is in, this might not work, but we'll try anyway */
2636 hermes_set_irqmask(hw, 0);
2637 hermes_write_regn(hw, EVACK, 0xffff);
2638
2639 priv->hw_unavailable++;
2640 priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
2641 netif_carrier_off(dev);
2642
2643 orinoco_unlock(priv, &flags);
2644
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002645 /* Scanning support: Cleanup of driver struct */
Dan Williams1e3428e2007-10-10 23:56:25 -04002646 orinoco_clear_scan_results(priv, 0);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002647 priv->scan_inprogress = 0;
2648
Christoph Hellwig8551cb92005-05-14 17:30:04 +02002649 if (priv->hard_reset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 err = (*priv->hard_reset)(priv);
Christoph Hellwig8551cb92005-05-14 17:30:04 +02002651 if (err) {
2652 printk(KERN_ERR "%s: orinoco_reset: Error %d "
2653 "performing hard reset\n", dev->name, err);
2654 goto disable;
2655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 }
2657
David Kilroy3994d502008-08-21 23:27:54 +01002658 if (priv->do_fw_download) {
2659 err = orinoco_download(priv);
2660 if (err)
2661 priv->do_fw_download = 0;
2662 }
2663
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 err = orinoco_reinit_firmware(dev);
2665 if (err) {
2666 printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
2667 dev->name, err);
Christoph Hellwig8551cb92005-05-14 17:30:04 +02002668 goto disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 }
2670
2671 spin_lock_irq(&priv->lock); /* This has to be called from user context */
2672
2673 priv->hw_unavailable--;
2674
2675 /* priv->open or priv->hw_unavailable might have changed while
2676 * we dropped the lock */
2677 if (priv->open && (! priv->hw_unavailable)) {
2678 err = __orinoco_up(dev);
2679 if (err) {
2680 printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
2681 dev->name, err);
2682 } else
2683 dev->trans_start = jiffies;
2684 }
2685
2686 spin_unlock_irq(&priv->lock);
2687
2688 return;
Christoph Hellwig8551cb92005-05-14 17:30:04 +02002689 disable:
2690 hermes_set_irqmask(hw, 0);
2691 netif_device_detach(dev);
2692 printk(KERN_ERR "%s: Device has been disabled!\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693}
2694
2695/********************************************************************/
2696/* Interrupt handler */
2697/********************************************************************/
2698
2699static void __orinoco_ev_tick(struct net_device *dev, hermes_t *hw)
2700{
2701 printk(KERN_DEBUG "%s: TICK\n", dev->name);
2702}
2703
2704static void __orinoco_ev_wterr(struct net_device *dev, hermes_t *hw)
2705{
2706 /* This seems to happen a fair bit under load, but ignoring it
2707 seems to work fine...*/
2708 printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
2709 dev->name);
2710}
2711
David Howells7d12e782006-10-05 14:55:46 +01002712irqreturn_t orinoco_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713{
Jeff Garzikc31f28e2006-10-06 14:56:04 -04002714 struct net_device *dev = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 struct orinoco_private *priv = netdev_priv(dev);
2716 hermes_t *hw = &priv->hw;
2717 int count = MAX_IRQLOOPS_PER_IRQ;
2718 u16 evstat, events;
2719 /* These are used to detect a runaway interrupt situation */
2720 /* If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
2721 * we panic and shut down the hardware */
2722 static int last_irq_jiffy = 0; /* jiffies value the last time
2723 * we were called */
2724 static int loops_this_jiffy = 0;
2725 unsigned long flags;
2726
2727 if (orinoco_lock(priv, &flags) != 0) {
2728 /* If hw is unavailable - we don't know if the irq was
2729 * for us or not */
2730 return IRQ_HANDLED;
2731 }
2732
2733 evstat = hermes_read_regn(hw, EVSTAT);
2734 events = evstat & hw->inten;
2735 if (! events) {
2736 orinoco_unlock(priv, &flags);
2737 return IRQ_NONE;
2738 }
2739
2740 if (jiffies != last_irq_jiffy)
2741 loops_this_jiffy = 0;
2742 last_irq_jiffy = jiffies;
2743
2744 while (events && count--) {
2745 if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
2746 printk(KERN_WARNING "%s: IRQ handler is looping too "
2747 "much! Resetting.\n", dev->name);
2748 /* Disable interrupts for now */
2749 hermes_set_irqmask(hw, 0);
2750 schedule_work(&priv->reset_work);
2751 break;
2752 }
2753
2754 /* Check the card hasn't been removed */
2755 if (! hermes_present(hw)) {
2756 DEBUG(0, "orinoco_interrupt(): card removed\n");
2757 break;
2758 }
2759
2760 if (events & HERMES_EV_TICK)
2761 __orinoco_ev_tick(dev, hw);
2762 if (events & HERMES_EV_WTERR)
2763 __orinoco_ev_wterr(dev, hw);
2764 if (events & HERMES_EV_INFDROP)
2765 __orinoco_ev_infdrop(dev, hw);
2766 if (events & HERMES_EV_INFO)
2767 __orinoco_ev_info(dev, hw);
2768 if (events & HERMES_EV_RX)
2769 __orinoco_ev_rx(dev, hw);
2770 if (events & HERMES_EV_TXEXC)
2771 __orinoco_ev_txexc(dev, hw);
2772 if (events & HERMES_EV_TX)
2773 __orinoco_ev_tx(dev, hw);
2774 if (events & HERMES_EV_ALLOC)
2775 __orinoco_ev_alloc(dev, hw);
2776
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02002777 hermes_write_regn(hw, EVACK, evstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
2779 evstat = hermes_read_regn(hw, EVSTAT);
2780 events = evstat & hw->inten;
2781 };
2782
2783 orinoco_unlock(priv, &flags);
2784 return IRQ_HANDLED;
2785}
2786
2787/********************************************************************/
2788/* Initialization */
2789/********************************************************************/
2790
2791struct comp_id {
2792 u16 id, variant, major, minor;
2793} __attribute__ ((packed));
2794
2795static inline fwtype_t determine_firmware_type(struct comp_id *nic_id)
2796{
2797 if (nic_id->id < 0x8000)
2798 return FIRMWARE_TYPE_AGERE;
2799 else if (nic_id->id == 0x8000 && nic_id->major == 0)
2800 return FIRMWARE_TYPE_SYMBOL;
2801 else
2802 return FIRMWARE_TYPE_INTERSIL;
2803}
2804
2805/* Set priv->firmware type, determine firmware properties */
2806static int determine_firmware(struct net_device *dev)
2807{
2808 struct orinoco_private *priv = netdev_priv(dev);
2809 hermes_t *hw = &priv->hw;
2810 int err;
2811 struct comp_id nic_id, sta_id;
2812 unsigned int firmver;
Hennerich, Michaeldde6d432007-02-05 16:41:35 -08002813 char tmp[SYMBOL_MAX_VER_LEN+1] __attribute__((aligned(2)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
2815 /* Get the hardware version */
2816 err = HERMES_READ_RECORD(hw, USER_BAP, HERMES_RID_NICID, &nic_id);
2817 if (err) {
2818 printk(KERN_ERR "%s: Cannot read hardware identity: error %d\n",
2819 dev->name, err);
2820 return err;
2821 }
2822
2823 le16_to_cpus(&nic_id.id);
2824 le16_to_cpus(&nic_id.variant);
2825 le16_to_cpus(&nic_id.major);
2826 le16_to_cpus(&nic_id.minor);
2827 printk(KERN_DEBUG "%s: Hardware identity %04x:%04x:%04x:%04x\n",
2828 dev->name, nic_id.id, nic_id.variant,
2829 nic_id.major, nic_id.minor);
2830
2831 priv->firmware_type = determine_firmware_type(&nic_id);
2832
2833 /* Get the firmware version */
2834 err = HERMES_READ_RECORD(hw, USER_BAP, HERMES_RID_STAID, &sta_id);
2835 if (err) {
2836 printk(KERN_ERR "%s: Cannot read station identity: error %d\n",
2837 dev->name, err);
2838 return err;
2839 }
2840
2841 le16_to_cpus(&sta_id.id);
2842 le16_to_cpus(&sta_id.variant);
2843 le16_to_cpus(&sta_id.major);
2844 le16_to_cpus(&sta_id.minor);
2845 printk(KERN_DEBUG "%s: Station identity %04x:%04x:%04x:%04x\n",
2846 dev->name, sta_id.id, sta_id.variant,
2847 sta_id.major, sta_id.minor);
2848
2849 switch (sta_id.id) {
2850 case 0x15:
2851 printk(KERN_ERR "%s: Primary firmware is active\n",
2852 dev->name);
2853 return -ENODEV;
2854 case 0x14b:
2855 printk(KERN_ERR "%s: Tertiary firmware is active\n",
2856 dev->name);
2857 return -ENODEV;
2858 case 0x1f: /* Intersil, Agere, Symbol Spectrum24 */
2859 case 0x21: /* Symbol Spectrum24 Trilogy */
2860 break;
2861 default:
2862 printk(KERN_NOTICE "%s: Unknown station ID, please report\n",
2863 dev->name);
2864 break;
2865 }
2866
2867 /* Default capabilities */
2868 priv->has_sensitivity = 1;
2869 priv->has_mwo = 0;
2870 priv->has_preamble = 0;
2871 priv->has_port3 = 1;
2872 priv->has_ibss = 1;
2873 priv->has_wep = 0;
2874 priv->has_big_wep = 0;
David Kilroy6eecad72008-08-21 23:27:56 +01002875 priv->has_alt_txcntl = 0;
David Kilroy01632fa2008-08-21 23:27:58 +01002876 priv->has_ext_scan = 0;
David Kilroyd03032a2008-08-21 23:28:02 +01002877 priv->has_wpa = 0;
David Kilroy3994d502008-08-21 23:27:54 +01002878 priv->do_fw_download = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
2880 /* Determine capabilities from the firmware version */
2881 switch (priv->firmware_type) {
2882 case FIRMWARE_TYPE_AGERE:
2883 /* Lucent Wavelan IEEE, Lucent Orinoco, Cabletron RoamAbout,
2884 ELSA, Melco, HP, IBM, Dell 1150, Compaq 110/210 */
2885 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
2886 "Lucent/Agere %d.%02d", sta_id.major, sta_id.minor);
2887
2888 firmver = ((unsigned long)sta_id.major << 16) | sta_id.minor;
2889
2890 priv->has_ibss = (firmver >= 0x60006);
2891 priv->has_wep = (firmver >= 0x40020);
2892 priv->has_big_wep = 1; /* FIXME: this is wrong - how do we tell
2893 Gold cards from the others? */
2894 priv->has_mwo = (firmver >= 0x60000);
2895 priv->has_pm = (firmver >= 0x40020); /* Don't work in 7.52 ? */
2896 priv->ibss_port = 1;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002897 priv->has_hostscan = (firmver >= 0x8000a);
David Kilroy3994d502008-08-21 23:27:54 +01002898 priv->do_fw_download = 1;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02002899 priv->broken_monitor = (firmver >= 0x80000);
David Kilroy6eecad72008-08-21 23:27:56 +01002900 priv->has_alt_txcntl = (firmver >= 0x90000); /* All 9.x ? */
David Kilroy01632fa2008-08-21 23:27:58 +01002901 priv->has_ext_scan = (firmver >= 0x90000); /* All 9.x ? */
David Kilroyd03032a2008-08-21 23:28:02 +01002902 priv->has_wpa = (firmver >= 0x9002a);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 /* Tested with Agere firmware :
2904 * 1.16 ; 4.08 ; 4.52 ; 6.04 ; 6.16 ; 7.28 => Jean II
2905 * Tested CableTron firmware : 4.32 => Anton */
2906 break;
2907 case FIRMWARE_TYPE_SYMBOL:
2908 /* Symbol , 3Com AirConnect, Intel, Ericsson WLAN */
2909 /* Intel MAC : 00:02:B3:* */
2910 /* 3Com MAC : 00:50:DA:* */
2911 memset(tmp, 0, sizeof(tmp));
2912 /* Get the Symbol firmware version */
2913 err = hermes_read_ltv(hw, USER_BAP,
2914 HERMES_RID_SECONDARYVERSION_SYMBOL,
2915 SYMBOL_MAX_VER_LEN, NULL, &tmp);
2916 if (err) {
2917 printk(KERN_WARNING
2918 "%s: Error %d reading Symbol firmware info. Wildly guessing capabilities...\n",
2919 dev->name, err);
2920 firmver = 0;
2921 tmp[0] = '\0';
2922 } else {
2923 /* The firmware revision is a string, the format is
2924 * something like : "V2.20-01".
2925 * Quick and dirty parsing... - Jean II
2926 */
2927 firmver = ((tmp[1] - '0') << 16) | ((tmp[3] - '0') << 12)
2928 | ((tmp[4] - '0') << 8) | ((tmp[6] - '0') << 4)
2929 | (tmp[7] - '0');
2930
2931 tmp[SYMBOL_MAX_VER_LEN] = '\0';
2932 }
2933
2934 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
2935 "Symbol %s", tmp);
2936
2937 priv->has_ibss = (firmver >= 0x20000);
2938 priv->has_wep = (firmver >= 0x15012);
2939 priv->has_big_wep = (firmver >= 0x20000);
2940 priv->has_pm = (firmver >= 0x20000 && firmver < 0x22000) ||
2941 (firmver >= 0x29000 && firmver < 0x30000) ||
2942 firmver >= 0x31000;
2943 priv->has_preamble = (firmver >= 0x20000);
2944 priv->ibss_port = 4;
David Kilroy3994d502008-08-21 23:27:54 +01002945
2946 /* Symbol firmware is found on various cards, but
2947 * there has been no attempt to check firmware
2948 * download on non-spectrum_cs based cards.
2949 *
2950 * Given that the Agere firmware download works
2951 * differently, we should avoid doing a firmware
2952 * download with the Symbol algorithm on non-spectrum
2953 * cards.
2954 *
2955 * For now we can identify a spectrum_cs based card
2956 * because it has a firmware reset function.
2957 */
2958 priv->do_fw_download = (priv->stop_fw != NULL);
2959
Christoph Hellwig649e59e2005-05-14 17:30:10 +02002960 priv->broken_disableport = (firmver == 0x25013) ||
2961 (firmver >= 0x30000 && firmver <= 0x31000);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002962 priv->has_hostscan = (firmver >= 0x31001) ||
2963 (firmver >= 0x29057 && firmver < 0x30000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 /* Tested with Intel firmware : 0x20015 => Jean II */
2965 /* Tested with 3Com firmware : 0x15012 & 0x22001 => Jean II */
2966 break;
2967 case FIRMWARE_TYPE_INTERSIL:
2968 /* D-Link, Linksys, Adtron, ZoomAir, and many others...
2969 * Samsung, Compaq 100/200 and Proxim are slightly
2970 * different and less well tested */
2971 /* D-Link MAC : 00:40:05:* */
2972 /* Addtron MAC : 00:90:D1:* */
2973 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
2974 "Intersil %d.%d.%d", sta_id.major, sta_id.minor,
2975 sta_id.variant);
2976
2977 firmver = ((unsigned long)sta_id.major << 16) |
2978 ((unsigned long)sta_id.minor << 8) | sta_id.variant;
2979
2980 priv->has_ibss = (firmver >= 0x000700); /* FIXME */
2981 priv->has_big_wep = priv->has_wep = (firmver >= 0x000800);
2982 priv->has_pm = (firmver >= 0x000700);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002983 priv->has_hostscan = (firmver >= 0x010301);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
2985 if (firmver >= 0x000800)
2986 priv->ibss_port = 0;
2987 else {
2988 printk(KERN_NOTICE "%s: Intersil firmware earlier "
2989 "than v0.8.x - several features not supported\n",
2990 dev->name);
2991 priv->ibss_port = 1;
2992 }
2993 break;
2994 }
2995 printk(KERN_DEBUG "%s: Firmware determined as %s\n", dev->name,
2996 priv->fw_name);
2997
2998 return 0;
2999}
3000
3001static int orinoco_init(struct net_device *dev)
3002{
3003 struct orinoco_private *priv = netdev_priv(dev);
3004 hermes_t *hw = &priv->hw;
3005 int err = 0;
3006 struct hermes_idstring nickbuf;
3007 u16 reclen;
3008 int len;
Joe Perches0795af52007-10-03 17:59:30 -07003009 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 /* No need to lock, the hw_unavailable flag is already set in
3012 * alloc_orinocodev() */
Jeff Garzikb4538722005-05-12 22:48:20 -04003013 priv->nicbuf_size = IEEE80211_FRAME_LEN + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
3015 /* Initialize the firmware */
Pavel Roskin37a6c612006-04-07 04:10:49 -04003016 err = hermes_init(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 if (err != 0) {
3018 printk(KERN_ERR "%s: failed to initialize firmware (err = %d)\n",
3019 dev->name, err);
3020 goto out;
3021 }
3022
3023 err = determine_firmware(dev);
3024 if (err != 0) {
3025 printk(KERN_ERR "%s: Incompatible firmware, aborting\n",
3026 dev->name);
3027 goto out;
3028 }
3029
David Kilroy3994d502008-08-21 23:27:54 +01003030 if (priv->do_fw_download) {
3031 err = orinoco_download(priv);
3032 if (err)
3033 priv->do_fw_download = 0;
3034
3035 /* Check firmware version again */
3036 err = determine_firmware(dev);
3037 if (err != 0) {
3038 printk(KERN_ERR "%s: Incompatible firmware, aborting\n",
3039 dev->name);
3040 goto out;
3041 }
3042 }
3043
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 if (priv->has_port3)
3045 printk(KERN_DEBUG "%s: Ad-hoc demo mode supported\n", dev->name);
3046 if (priv->has_ibss)
3047 printk(KERN_DEBUG "%s: IEEE standard IBSS ad-hoc mode supported\n",
3048 dev->name);
3049 if (priv->has_wep) {
3050 printk(KERN_DEBUG "%s: WEP supported, ", dev->name);
3051 if (priv->has_big_wep)
3052 printk("104-bit key\n");
3053 else
3054 printk("40-bit key\n");
3055 }
David Kilroyd03032a2008-08-21 23:28:02 +01003056 if (priv->has_wpa)
3057 printk(KERN_DEBUG "%s: WPA-PSK supported\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
David Kilroy01632fa2008-08-21 23:27:58 +01003059 /* Now we have the firmware capabilities, allocate appropiate
3060 * sized scan buffers */
3061 if (orinoco_bss_data_allocate(priv))
3062 goto out;
3063 orinoco_bss_data_init(priv);
3064
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 /* Get the MAC address */
3066 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
3067 ETH_ALEN, NULL, dev->dev_addr);
3068 if (err) {
3069 printk(KERN_WARNING "%s: failed to read MAC address!\n",
3070 dev->name);
3071 goto out;
3072 }
3073
Joe Perches0795af52007-10-03 17:59:30 -07003074 printk(KERN_DEBUG "%s: MAC address %s\n",
3075 dev->name, print_mac(mac, dev->dev_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
3077 /* Get the station name */
3078 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
3079 sizeof(nickbuf), &reclen, &nickbuf);
3080 if (err) {
3081 printk(KERN_ERR "%s: failed to read station name\n",
3082 dev->name);
3083 goto out;
3084 }
3085 if (nickbuf.len)
3086 len = min(IW_ESSID_MAX_SIZE, (int)le16_to_cpu(nickbuf.len));
3087 else
3088 len = min(IW_ESSID_MAX_SIZE, 2 * reclen);
3089 memcpy(priv->nick, &nickbuf.val, len);
3090 priv->nick[len] = '\0';
3091
3092 printk(KERN_DEBUG "%s: Station name \"%s\"\n", dev->name, priv->nick);
3093
Pavel Roskin37a6c612006-04-07 04:10:49 -04003094 err = orinoco_allocate_fid(dev);
3095 if (err) {
3096 printk(KERN_ERR "%s: failed to allocate NIC buffer!\n",
3097 dev->name);
3098 goto out;
3099 }
3100
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 /* Get allowed channels */
3102 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CHANNELLIST,
3103 &priv->channel_mask);
3104 if (err) {
3105 printk(KERN_ERR "%s: failed to read channel list!\n",
3106 dev->name);
3107 goto out;
3108 }
3109
3110 /* Get initial AP density */
3111 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFSYSTEMSCALE,
3112 &priv->ap_density);
3113 if (err || priv->ap_density < 1 || priv->ap_density > 3) {
3114 priv->has_sensitivity = 0;
3115 }
3116
3117 /* Get initial RTS threshold */
3118 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
3119 &priv->rts_thresh);
3120 if (err) {
3121 printk(KERN_ERR "%s: failed to read RTS threshold!\n",
3122 dev->name);
3123 goto out;
3124 }
3125
3126 /* Get initial fragmentation settings */
3127 if (priv->has_mwo)
3128 err = hermes_read_wordrec(hw, USER_BAP,
3129 HERMES_RID_CNFMWOROBUST_AGERE,
3130 &priv->mwo_robust);
3131 else
3132 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
3133 &priv->frag_thresh);
3134 if (err) {
3135 printk(KERN_ERR "%s: failed to read fragmentation settings!\n",
3136 dev->name);
3137 goto out;
3138 }
3139
3140 /* Power management setup */
3141 if (priv->has_pm) {
3142 priv->pm_on = 0;
3143 priv->pm_mcast = 1;
3144 err = hermes_read_wordrec(hw, USER_BAP,
3145 HERMES_RID_CNFMAXSLEEPDURATION,
3146 &priv->pm_period);
3147 if (err) {
3148 printk(KERN_ERR "%s: failed to read power management period!\n",
3149 dev->name);
3150 goto out;
3151 }
3152 err = hermes_read_wordrec(hw, USER_BAP,
3153 HERMES_RID_CNFPMHOLDOVERDURATION,
3154 &priv->pm_timeout);
3155 if (err) {
3156 printk(KERN_ERR "%s: failed to read power management timeout!\n",
3157 dev->name);
3158 goto out;
3159 }
3160 }
3161
3162 /* Preamble setup */
3163 if (priv->has_preamble) {
3164 err = hermes_read_wordrec(hw, USER_BAP,
3165 HERMES_RID_CNFPREAMBLE_SYMBOL,
3166 &priv->preamble);
3167 if (err)
3168 goto out;
3169 }
3170
3171 /* Set up the default configuration */
3172 priv->iw_mode = IW_MODE_INFRA;
3173 /* By default use IEEE/IBSS ad-hoc mode if we have it */
3174 priv->prefer_port3 = priv->has_port3 && (! priv->has_ibss);
3175 set_port_type(priv);
David Gibsond51d8b12005-05-12 20:03:36 -04003176 priv->channel = 0; /* use firmware default */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
3178 priv->promiscuous = 0;
David Kilroy4ae6ee22008-08-21 23:27:59 +01003179 priv->encode_alg = IW_ENCODE_ALG_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 priv->tx_key = 0;
David Kilroyd03032a2008-08-21 23:28:02 +01003181 priv->wpa_enabled = 0;
3182 priv->tkip_cm_active = 0;
3183 priv->key_mgmt = 0;
3184 priv->wpa_ie_len = 0;
3185 priv->wpa_ie = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 /* Make the hardware available, as long as it hasn't been
3188 * removed elsewhere (e.g. by PCMCIA hot unplug) */
3189 spin_lock_irq(&priv->lock);
3190 priv->hw_unavailable--;
3191 spin_unlock_irq(&priv->lock);
3192
3193 printk(KERN_DEBUG "%s: ready\n", dev->name);
3194
3195 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 return err;
3197}
3198
David Kilroy3994d502008-08-21 23:27:54 +01003199struct net_device
3200*alloc_orinocodev(int sizeof_card,
3201 struct device *device,
3202 int (*hard_reset)(struct orinoco_private *),
3203 int (*stop_fw)(struct orinoco_private *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204{
3205 struct net_device *dev;
3206 struct orinoco_private *priv;
3207
3208 dev = alloc_etherdev(sizeof(struct orinoco_private) + sizeof_card);
3209 if (! dev)
3210 return NULL;
3211 priv = netdev_priv(dev);
3212 priv->ndev = dev;
3213 if (sizeof_card)
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02003214 priv->card = (void *)((unsigned long)priv
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 + sizeof(struct orinoco_private));
3216 else
3217 priv->card = NULL;
David Kilroy3994d502008-08-21 23:27:54 +01003218 priv->dev = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
3220 /* Setup / override net_device fields */
3221 dev->init = orinoco_init;
3222 dev->hard_start_xmit = orinoco_xmit;
3223 dev->tx_timeout = orinoco_tx_timeout;
3224 dev->watchdog_timeo = HZ; /* 1 second timeout */
3225 dev->get_stats = orinoco_get_stats;
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02003226 dev->ethtool_ops = &orinoco_ethtool_ops;
Christoph Hellwig620554e2005-06-19 01:27:33 +02003227 dev->wireless_handlers = (struct iw_handler_def *)&orinoco_handler_def;
Pavel Roskin343c6862005-09-09 18:43:02 -04003228#ifdef WIRELESS_SPY
3229 priv->wireless_data.spy_data = &priv->spy_data;
3230 dev->wireless_data = &priv->wireless_data;
3231#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 dev->change_mtu = orinoco_change_mtu;
3233 dev->set_multicast_list = orinoco_set_multicast_list;
3234 /* we use the default eth_mac_addr for setting the MAC addr */
3235
3236 /* Set up default callbacks */
3237 dev->open = orinoco_open;
3238 dev->stop = orinoco_stop;
3239 priv->hard_reset = hard_reset;
David Kilroy3994d502008-08-21 23:27:54 +01003240 priv->stop_fw = stop_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
3242 spin_lock_init(&priv->lock);
3243 priv->open = 0;
3244 priv->hw_unavailable = 1; /* orinoco_init() must clear this
3245 * before anything else touches the
3246 * hardware */
David Howellsc4028952006-11-22 14:57:56 +00003247 INIT_WORK(&priv->reset_work, orinoco_reset);
3248 INIT_WORK(&priv->join_work, orinoco_join_ap);
3249 INIT_WORK(&priv->wevent_work, orinoco_send_wevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
3251 netif_carrier_off(dev);
3252 priv->last_linkstatus = 0xffff;
3253
3254 return dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255}
3256
3257void free_orinocodev(struct net_device *dev)
3258{
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003259 struct orinoco_private *priv = netdev_priv(dev);
3260
David Kilroyd03032a2008-08-21 23:28:02 +01003261 priv->wpa_ie_len = 0;
3262 kfree(priv->wpa_ie);
Dan Williams1e3428e2007-10-10 23:56:25 -04003263 orinoco_bss_data_free(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 free_netdev(dev);
3265}
3266
3267/********************************************************************/
3268/* Wireless extensions */
3269/********************************************************************/
3270
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07003271/* Return : < 0 -> error code ; >= 0 -> length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272static int orinoco_hw_get_essid(struct orinoco_private *priv, int *active,
3273 char buf[IW_ESSID_MAX_SIZE+1])
3274{
3275 hermes_t *hw = &priv->hw;
3276 int err = 0;
3277 struct hermes_idstring essidbuf;
3278 char *p = (char *)(&essidbuf.val);
3279 int len;
3280 unsigned long flags;
3281
3282 if (orinoco_lock(priv, &flags) != 0)
3283 return -EBUSY;
3284
3285 if (strlen(priv->desired_essid) > 0) {
3286 /* We read the desired SSID from the hardware rather
3287 than from priv->desired_essid, just in case the
3288 firmware is allowed to change it on us. I'm not
3289 sure about this */
3290 /* My guess is that the OWNSSID should always be whatever
3291 * we set to the card, whereas CURRENT_SSID is the one that
3292 * may change... - Jean II */
3293 u16 rid;
3294
3295 *active = 1;
3296
3297 rid = (priv->port_type == 3) ? HERMES_RID_CNFOWNSSID :
3298 HERMES_RID_CNFDESIREDSSID;
3299
3300 err = hermes_read_ltv(hw, USER_BAP, rid, sizeof(essidbuf),
3301 NULL, &essidbuf);
3302 if (err)
3303 goto fail_unlock;
3304 } else {
3305 *active = 0;
3306
3307 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTSSID,
3308 sizeof(essidbuf), NULL, &essidbuf);
3309 if (err)
3310 goto fail_unlock;
3311 }
3312
3313 len = le16_to_cpu(essidbuf.len);
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02003314 BUG_ON(len > IW_ESSID_MAX_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07003316 memset(buf, 0, IW_ESSID_MAX_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 memcpy(buf, p, len);
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07003318 err = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319
3320 fail_unlock:
3321 orinoco_unlock(priv, &flags);
3322
3323 return err;
3324}
3325
3326static long orinoco_hw_get_freq(struct orinoco_private *priv)
3327{
3328
3329 hermes_t *hw = &priv->hw;
3330 int err = 0;
3331 u16 channel;
3332 long freq = 0;
3333 unsigned long flags;
3334
3335 if (orinoco_lock(priv, &flags) != 0)
3336 return -EBUSY;
3337
3338 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CURRENTCHANNEL, &channel);
3339 if (err)
3340 goto out;
3341
3342 /* Intersil firmware 1.3.5 returns 0 when the interface is down */
3343 if (channel == 0) {
3344 err = -EBUSY;
3345 goto out;
3346 }
3347
3348 if ( (channel < 1) || (channel > NUM_CHANNELS) ) {
3349 printk(KERN_WARNING "%s: Channel out of range (%d)!\n",
3350 priv->ndev->name, channel);
3351 err = -EBUSY;
3352 goto out;
3353
3354 }
3355 freq = channel_frequency[channel-1] * 100000;
3356
3357 out:
3358 orinoco_unlock(priv, &flags);
3359
3360 if (err > 0)
3361 err = -EBUSY;
3362 return err ? err : freq;
3363}
3364
3365static int orinoco_hw_get_bitratelist(struct orinoco_private *priv,
3366 int *numrates, s32 *rates, int max)
3367{
3368 hermes_t *hw = &priv->hw;
3369 struct hermes_idstring list;
3370 unsigned char *p = (unsigned char *)&list.val;
3371 int err = 0;
3372 int num;
3373 int i;
3374 unsigned long flags;
3375
3376 if (orinoco_lock(priv, &flags) != 0)
3377 return -EBUSY;
3378
3379 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_SUPPORTEDDATARATES,
3380 sizeof(list), NULL, &list);
3381 orinoco_unlock(priv, &flags);
3382
3383 if (err)
3384 return err;
3385
3386 num = le16_to_cpu(list.len);
3387 *numrates = num;
3388 num = min(num, max);
3389
3390 for (i = 0; i < num; i++) {
3391 rates[i] = (p[i] & 0x7f) * 500000; /* convert to bps */
3392 }
3393
3394 return 0;
3395}
3396
Christoph Hellwig620554e2005-06-19 01:27:33 +02003397static int orinoco_ioctl_getname(struct net_device *dev,
3398 struct iw_request_info *info,
3399 char *name,
3400 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401{
3402 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 int numrates;
Christoph Hellwig620554e2005-06-19 01:27:33 +02003404 int err;
3405
3406 err = orinoco_hw_get_bitratelist(priv, &numrates, NULL, 0);
3407
3408 if (!err && (numrates > 2))
3409 strcpy(name, "IEEE 802.11b");
3410 else
3411 strcpy(name, "IEEE 802.11-DS");
3412
3413 return 0;
3414}
3415
Christoph Hellwig16739b02005-06-19 01:27:51 +02003416static int orinoco_ioctl_setwap(struct net_device *dev,
3417 struct iw_request_info *info,
3418 struct sockaddr *ap_addr,
3419 char *extra)
3420{
3421 struct orinoco_private *priv = netdev_priv(dev);
3422 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 unsigned long flags;
Christoph Hellwig16739b02005-06-19 01:27:51 +02003424 static const u8 off_addr[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3425 static const u8 any_addr[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
3427 if (orinoco_lock(priv, &flags) != 0)
3428 return -EBUSY;
3429
Christoph Hellwig16739b02005-06-19 01:27:51 +02003430 /* Enable automatic roaming - no sanity checks are needed */
3431 if (memcmp(&ap_addr->sa_data, off_addr, ETH_ALEN) == 0 ||
3432 memcmp(&ap_addr->sa_data, any_addr, ETH_ALEN) == 0) {
3433 priv->bssid_fixed = 0;
3434 memset(priv->desired_bssid, 0, ETH_ALEN);
3435
3436 /* "off" means keep existing connection */
3437 if (ap_addr->sa_data[0] == 0) {
3438 __orinoco_hw_set_wap(priv);
3439 err = 0;
3440 }
3441 goto out;
3442 }
3443
3444 if (priv->firmware_type == FIRMWARE_TYPE_AGERE) {
3445 printk(KERN_WARNING "%s: Lucent/Agere firmware doesn't "
3446 "support manual roaming\n",
3447 dev->name);
3448 err = -EOPNOTSUPP;
3449 goto out;
3450 }
3451
3452 if (priv->iw_mode != IW_MODE_INFRA) {
3453 printk(KERN_WARNING "%s: Manual roaming supported only in "
3454 "managed mode\n", dev->name);
3455 err = -EOPNOTSUPP;
3456 goto out;
3457 }
3458
3459 /* Intersil firmware hangs without Desired ESSID */
3460 if (priv->firmware_type == FIRMWARE_TYPE_INTERSIL &&
3461 strlen(priv->desired_essid) == 0) {
3462 printk(KERN_WARNING "%s: Desired ESSID must be set for "
3463 "manual roaming\n", dev->name);
3464 err = -EOPNOTSUPP;
3465 goto out;
3466 }
3467
3468 /* Finally, enable manual roaming */
3469 priv->bssid_fixed = 1;
3470 memcpy(priv->desired_bssid, &ap_addr->sa_data, ETH_ALEN);
3471
3472 out:
3473 orinoco_unlock(priv, &flags);
3474 return err;
3475}
3476
Christoph Hellwig620554e2005-06-19 01:27:33 +02003477static int orinoco_ioctl_getwap(struct net_device *dev,
3478 struct iw_request_info *info,
3479 struct sockaddr *ap_addr,
3480 char *extra)
3481{
3482 struct orinoco_private *priv = netdev_priv(dev);
3483
3484 hermes_t *hw = &priv->hw;
3485 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 unsigned long flags;
3487
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 if (orinoco_lock(priv, &flags) != 0)
3489 return -EBUSY;
3490
Christoph Hellwig620554e2005-06-19 01:27:33 +02003491 ap_addr->sa_family = ARPHRD_ETHER;
3492 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
3493 ETH_ALEN, NULL, ap_addr->sa_data);
3494
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 orinoco_unlock(priv, &flags);
3496
Christoph Hellwig620554e2005-06-19 01:27:33 +02003497 return err;
3498}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499
Christoph Hellwig620554e2005-06-19 01:27:33 +02003500static int orinoco_ioctl_setmode(struct net_device *dev,
3501 struct iw_request_info *info,
3502 u32 *mode,
3503 char *extra)
3504{
3505 struct orinoco_private *priv = netdev_priv(dev);
3506 int err = -EINPROGRESS; /* Call commit handler */
3507 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508
Christoph Hellwig620554e2005-06-19 01:27:33 +02003509 if (priv->iw_mode == *mode)
3510 return 0;
3511
3512 if (orinoco_lock(priv, &flags) != 0)
3513 return -EBUSY;
3514
3515 switch (*mode) {
3516 case IW_MODE_ADHOC:
3517 if (!priv->has_ibss && !priv->has_port3)
3518 err = -EOPNOTSUPP;
3519 break;
3520
3521 case IW_MODE_INFRA:
3522 break;
3523
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02003524 case IW_MODE_MONITOR:
3525 if (priv->broken_monitor && !force_monitor) {
3526 printk(KERN_WARNING "%s: Monitor mode support is "
3527 "buggy in this firmware, not enabling\n",
3528 dev->name);
3529 err = -EOPNOTSUPP;
3530 }
3531 break;
3532
Christoph Hellwig620554e2005-06-19 01:27:33 +02003533 default:
3534 err = -EOPNOTSUPP;
3535 break;
3536 }
3537
3538 if (err == -EINPROGRESS) {
3539 priv->iw_mode = *mode;
3540 set_port_type(priv);
3541 }
3542
3543 orinoco_unlock(priv, &flags);
3544
3545 return err;
3546}
3547
3548static int orinoco_ioctl_getmode(struct net_device *dev,
3549 struct iw_request_info *info,
3550 u32 *mode,
3551 char *extra)
3552{
3553 struct orinoco_private *priv = netdev_priv(dev);
3554
3555 *mode = priv->iw_mode;
3556 return 0;
3557}
3558
3559static int orinoco_ioctl_getiwrange(struct net_device *dev,
3560 struct iw_request_info *info,
3561 struct iw_point *rrq,
3562 char *extra)
3563{
3564 struct orinoco_private *priv = netdev_priv(dev);
3565 int err = 0;
3566 struct iw_range *range = (struct iw_range *) extra;
3567 int numrates;
3568 int i, k;
3569
Christoph Hellwig620554e2005-06-19 01:27:33 +02003570 rrq->length = sizeof(struct iw_range);
3571 memset(range, 0, sizeof(struct iw_range));
3572
3573 range->we_version_compiled = WIRELESS_EXT;
David Kilroyd03032a2008-08-21 23:28:02 +01003574 range->we_version_source = 22;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575
3576 /* Set available channels/frequencies */
Christoph Hellwig620554e2005-06-19 01:27:33 +02003577 range->num_channels = NUM_CHANNELS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 k = 0;
3579 for (i = 0; i < NUM_CHANNELS; i++) {
3580 if (priv->channel_mask & (1 << i)) {
Christoph Hellwig620554e2005-06-19 01:27:33 +02003581 range->freq[k].i = i + 1;
3582 range->freq[k].m = channel_frequency[i] * 100000;
3583 range->freq[k].e = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 k++;
3585 }
3586
3587 if (k >= IW_MAX_FREQUENCIES)
3588 break;
3589 }
Christoph Hellwig620554e2005-06-19 01:27:33 +02003590 range->num_frequency = k;
3591 range->sensitivity = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592
Christoph Hellwig620554e2005-06-19 01:27:33 +02003593 if (priv->has_wep) {
3594 range->max_encoding_tokens = ORINOCO_MAX_KEYS;
3595 range->encoding_size[0] = SMALL_KEY_SIZE;
3596 range->num_encoding_sizes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
Christoph Hellwig620554e2005-06-19 01:27:33 +02003598 if (priv->has_big_wep) {
3599 range->encoding_size[1] = LARGE_KEY_SIZE;
3600 range->num_encoding_sizes = 2;
3601 }
3602 }
3603
David Kilroyd03032a2008-08-21 23:28:02 +01003604 if (priv->has_wpa)
3605 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_CIPHER_TKIP;
3606
Pavel Roskin343c6862005-09-09 18:43:02 -04003607 if ((priv->iw_mode == IW_MODE_ADHOC) && (!SPY_NUMBER(priv))){
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 /* Quality stats meaningless in ad-hoc mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 } else {
Christoph Hellwig620554e2005-06-19 01:27:33 +02003610 range->max_qual.qual = 0x8b - 0x2f;
3611 range->max_qual.level = 0x2f - 0x95 - 1;
3612 range->max_qual.noise = 0x2f - 0x95 - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 /* Need to get better values */
Christoph Hellwig620554e2005-06-19 01:27:33 +02003614 range->avg_qual.qual = 0x24;
3615 range->avg_qual.level = 0xC2;
3616 range->avg_qual.noise = 0x9E;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 }
3618
3619 err = orinoco_hw_get_bitratelist(priv, &numrates,
Christoph Hellwig620554e2005-06-19 01:27:33 +02003620 range->bitrate, IW_MAX_BITRATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 if (err)
3622 return err;
Christoph Hellwig620554e2005-06-19 01:27:33 +02003623 range->num_bitrates = numrates;
3624
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 /* Set an indication of the max TCP throughput in bit/s that we can
3626 * expect using this interface. May be use for QoS stuff...
3627 * Jean II */
Christoph Hellwig620554e2005-06-19 01:27:33 +02003628 if (numrates > 2)
3629 range->throughput = 5 * 1000 * 1000; /* ~5 Mb/s */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 else
Christoph Hellwig620554e2005-06-19 01:27:33 +02003631 range->throughput = 1.5 * 1000 * 1000; /* ~1.5 Mb/s */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
Christoph Hellwig620554e2005-06-19 01:27:33 +02003633 range->min_rts = 0;
3634 range->max_rts = 2347;
3635 range->min_frag = 256;
3636 range->max_frag = 2346;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637
Christoph Hellwig620554e2005-06-19 01:27:33 +02003638 range->min_pmp = 0;
3639 range->max_pmp = 65535000;
3640 range->min_pmt = 0;
3641 range->max_pmt = 65535 * 1000; /* ??? */
3642 range->pmp_flags = IW_POWER_PERIOD;
3643 range->pmt_flags = IW_POWER_TIMEOUT;
3644 range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_UNICAST_R;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645
Christoph Hellwig620554e2005-06-19 01:27:33 +02003646 range->retry_capa = IW_RETRY_LIMIT | IW_RETRY_LIFETIME;
3647 range->retry_flags = IW_RETRY_LIMIT;
3648 range->r_time_flags = IW_RETRY_LIFETIME;
3649 range->min_retry = 0;
3650 range->max_retry = 65535; /* ??? */
3651 range->min_r_time = 0;
3652 range->max_r_time = 65535 * 1000; /* ??? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
David Kilroy0753bba2008-08-21 23:27:46 +01003654 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
3655 range->scan_capa = IW_SCAN_CAPA_ESSID;
3656 else
3657 range->scan_capa = IW_SCAN_CAPA_NONE;
3658
Pavel Roskin343c6862005-09-09 18:43:02 -04003659 /* Event capability (kernel) */
3660 IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
3661 /* Event capability (driver) */
3662 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWTHRSPY);
3663 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
3664 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
3665 IW_EVENT_CAPA_SET(range->event_capa, IWEVTXDROP);
3666
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 return 0;
3668}
3669
Christoph Hellwig620554e2005-06-19 01:27:33 +02003670static int orinoco_ioctl_setiwencode(struct net_device *dev,
3671 struct iw_request_info *info,
3672 struct iw_point *erq,
3673 char *keybuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674{
3675 struct orinoco_private *priv = netdev_priv(dev);
3676 int index = (erq->flags & IW_ENCODE_INDEX) - 1;
3677 int setindex = priv->tx_key;
David Kilroy4ae6ee22008-08-21 23:27:59 +01003678 int encode_alg = priv->encode_alg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 int restricted = priv->wep_restrict;
3680 u16 xlen = 0;
Christoph Hellwig620554e2005-06-19 01:27:33 +02003681 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 unsigned long flags;
3683
3684 if (! priv->has_wep)
3685 return -EOPNOTSUPP;
3686
3687 if (erq->pointer) {
3688 /* We actually have a key to set - check its length */
3689 if (erq->length > LARGE_KEY_SIZE)
3690 return -E2BIG;
3691
3692 if ( (erq->length > SMALL_KEY_SIZE) && !priv->has_big_wep )
3693 return -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 }
3695
3696 if (orinoco_lock(priv, &flags) != 0)
3697 return -EBUSY;
3698
David Kilroyd03032a2008-08-21 23:28:02 +01003699 /* Clear any TKIP key we have */
3700 if ((priv->has_wpa) && (priv->encode_alg == IW_ENCODE_ALG_TKIP))
3701 (void) orinoco_clear_tkip_key(priv, setindex);
3702
Dan Williamsfe397d42006-07-14 11:41:47 -04003703 if (erq->length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
3705 index = priv->tx_key;
3706
3707 /* Adjust key length to a supported value */
3708 if (erq->length > SMALL_KEY_SIZE) {
3709 xlen = LARGE_KEY_SIZE;
3710 } else if (erq->length > 0) {
3711 xlen = SMALL_KEY_SIZE;
3712 } else
3713 xlen = 0;
3714
3715 /* Switch on WEP if off */
David Kilroy4ae6ee22008-08-21 23:27:59 +01003716 if ((encode_alg != IW_ENCODE_ALG_WEP) && (xlen > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 setindex = index;
David Kilroy4ae6ee22008-08-21 23:27:59 +01003718 encode_alg = IW_ENCODE_ALG_WEP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 }
3720 } else {
3721 /* Important note : if the user do "iwconfig eth0 enc off",
3722 * we will arrive there with an index of -1. This is valid
3723 * but need to be taken care off... Jean II */
3724 if ((index < 0) || (index >= ORINOCO_MAX_KEYS)) {
3725 if((index != -1) || (erq->flags == 0)) {
3726 err = -EINVAL;
3727 goto out;
3728 }
3729 } else {
3730 /* Set the index : Check that the key is valid */
3731 if(priv->keys[index].len == 0) {
3732 err = -EINVAL;
3733 goto out;
3734 }
3735 setindex = index;
3736 }
3737 }
3738
3739 if (erq->flags & IW_ENCODE_DISABLED)
David Kilroy4ae6ee22008-08-21 23:27:59 +01003740 encode_alg = IW_ENCODE_ALG_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 if (erq->flags & IW_ENCODE_OPEN)
3742 restricted = 0;
3743 if (erq->flags & IW_ENCODE_RESTRICTED)
3744 restricted = 1;
3745
Dan Williamsfe397d42006-07-14 11:41:47 -04003746 if (erq->pointer && erq->length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 priv->keys[index].len = cpu_to_le16(xlen);
3748 memset(priv->keys[index].data, 0,
3749 sizeof(priv->keys[index].data));
3750 memcpy(priv->keys[index].data, keybuf, erq->length);
3751 }
3752 priv->tx_key = setindex;
3753
3754 /* Try fast key change if connected and only keys are changed */
David Kilroy4ae6ee22008-08-21 23:27:59 +01003755 if ((priv->encode_alg == encode_alg) &&
3756 (priv->wep_restrict == restricted) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 netif_carrier_ok(dev)) {
3758 err = __orinoco_hw_setup_wepkeys(priv);
3759 /* No need to commit if successful */
3760 goto out;
3761 }
3762
David Kilroy4ae6ee22008-08-21 23:27:59 +01003763 priv->encode_alg = encode_alg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 priv->wep_restrict = restricted;
3765
3766 out:
3767 orinoco_unlock(priv, &flags);
3768
3769 return err;
3770}
3771
Christoph Hellwig620554e2005-06-19 01:27:33 +02003772static int orinoco_ioctl_getiwencode(struct net_device *dev,
3773 struct iw_request_info *info,
3774 struct iw_point *erq,
3775 char *keybuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776{
3777 struct orinoco_private *priv = netdev_priv(dev);
3778 int index = (erq->flags & IW_ENCODE_INDEX) - 1;
3779 u16 xlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 unsigned long flags;
3781
3782 if (! priv->has_wep)
3783 return -EOPNOTSUPP;
3784
3785 if (orinoco_lock(priv, &flags) != 0)
3786 return -EBUSY;
3787
3788 if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
3789 index = priv->tx_key;
3790
3791 erq->flags = 0;
David Kilroy4ae6ee22008-08-21 23:27:59 +01003792 if (!priv->encode_alg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 erq->flags |= IW_ENCODE_DISABLED;
3794 erq->flags |= index + 1;
3795
3796 if (priv->wep_restrict)
3797 erq->flags |= IW_ENCODE_RESTRICTED;
3798 else
3799 erq->flags |= IW_ENCODE_OPEN;
3800
3801 xlen = le16_to_cpu(priv->keys[index].len);
3802
3803 erq->length = xlen;
3804
3805 memcpy(keybuf, priv->keys[index].data, ORINOCO_MAX_KEY_SIZE);
3806
3807 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 return 0;
3809}
3810
Christoph Hellwig620554e2005-06-19 01:27:33 +02003811static int orinoco_ioctl_setessid(struct net_device *dev,
3812 struct iw_request_info *info,
3813 struct iw_point *erq,
3814 char *essidbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815{
3816 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 unsigned long flags;
3818
3819 /* Note : ESSID is ignored in Ad-Hoc demo mode, but we can set it
3820 * anyway... - Jean II */
3821
Christoph Hellwig620554e2005-06-19 01:27:33 +02003822 /* Hum... Should not use Wireless Extension constant (may change),
3823 * should use our own... - Jean II */
3824 if (erq->length > IW_ESSID_MAX_SIZE)
3825 return -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
3827 if (orinoco_lock(priv, &flags) != 0)
3828 return -EBUSY;
3829
Christoph Hellwig620554e2005-06-19 01:27:33 +02003830 /* NULL the string (for NULL termination & ESSID = ANY) - Jean II */
3831 memset(priv->desired_essid, 0, sizeof(priv->desired_essid));
3832
3833 /* If not ANY, get the new ESSID */
3834 if (erq->flags) {
3835 memcpy(priv->desired_essid, essidbuf, erq->length);
3836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837
3838 orinoco_unlock(priv, &flags);
3839
Christoph Hellwig620554e2005-06-19 01:27:33 +02003840 return -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841}
3842
Christoph Hellwig620554e2005-06-19 01:27:33 +02003843static int orinoco_ioctl_getessid(struct net_device *dev,
3844 struct iw_request_info *info,
3845 struct iw_point *erq,
3846 char *essidbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847{
3848 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 int active;
3850 int err = 0;
3851 unsigned long flags;
3852
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 if (netif_running(dev)) {
3854 err = orinoco_hw_get_essid(priv, &active, essidbuf);
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07003855 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 return err;
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07003857 erq->length = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 } else {
3859 if (orinoco_lock(priv, &flags) != 0)
3860 return -EBUSY;
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07003861 memcpy(essidbuf, priv->desired_essid, IW_ESSID_MAX_SIZE);
3862 erq->length = strlen(priv->desired_essid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 orinoco_unlock(priv, &flags);
3864 }
3865
3866 erq->flags = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 return 0;
3869}
3870
Christoph Hellwig620554e2005-06-19 01:27:33 +02003871static int orinoco_ioctl_setnick(struct net_device *dev,
3872 struct iw_request_info *info,
3873 struct iw_point *nrq,
3874 char *nickbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875{
3876 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 unsigned long flags;
3878
3879 if (nrq->length > IW_ESSID_MAX_SIZE)
3880 return -E2BIG;
3881
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 if (orinoco_lock(priv, &flags) != 0)
3883 return -EBUSY;
3884
Christoph Hellwig620554e2005-06-19 01:27:33 +02003885 memset(priv->nick, 0, sizeof(priv->nick));
3886 memcpy(priv->nick, nickbuf, nrq->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887
3888 orinoco_unlock(priv, &flags);
3889
Christoph Hellwig620554e2005-06-19 01:27:33 +02003890 return -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891}
3892
Christoph Hellwig620554e2005-06-19 01:27:33 +02003893static int orinoco_ioctl_getnick(struct net_device *dev,
3894 struct iw_request_info *info,
3895 struct iw_point *nrq,
3896 char *nickbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897{
3898 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 unsigned long flags;
3900
3901 if (orinoco_lock(priv, &flags) != 0)
3902 return -EBUSY;
3903
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07003904 memcpy(nickbuf, priv->nick, IW_ESSID_MAX_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 orinoco_unlock(priv, &flags);
3906
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07003907 nrq->length = strlen(priv->nick);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 return 0;
3910}
3911
Christoph Hellwig620554e2005-06-19 01:27:33 +02003912static int orinoco_ioctl_setfreq(struct net_device *dev,
3913 struct iw_request_info *info,
3914 struct iw_freq *frq,
3915 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916{
3917 struct orinoco_private *priv = netdev_priv(dev);
3918 int chan = -1;
3919 unsigned long flags;
Christoph Hellwig620554e2005-06-19 01:27:33 +02003920 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02003922 /* In infrastructure mode the AP sets the channel */
3923 if (priv->iw_mode == IW_MODE_INFRA)
3924 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925
3926 if ( (frq->e == 0) && (frq->m <= 1000) ) {
3927 /* Setting by channel number */
3928 chan = frq->m;
3929 } else {
3930 /* Setting by frequency - search the table */
3931 int mult = 1;
3932 int i;
3933
3934 for (i = 0; i < (6 - frq->e); i++)
3935 mult *= 10;
3936
3937 for (i = 0; i < NUM_CHANNELS; i++)
3938 if (frq->m == (channel_frequency[i] * mult))
3939 chan = i+1;
3940 }
3941
3942 if ( (chan < 1) || (chan > NUM_CHANNELS) ||
3943 ! (priv->channel_mask & (1 << (chan-1)) ) )
3944 return -EINVAL;
3945
3946 if (orinoco_lock(priv, &flags) != 0)
3947 return -EBUSY;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02003948
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 priv->channel = chan;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02003950 if (priv->iw_mode == IW_MODE_MONITOR) {
3951 /* Fast channel change - no commit if successful */
3952 hermes_t *hw = &priv->hw;
3953 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
3954 HERMES_TEST_SET_CHANNEL,
3955 chan, NULL);
3956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 orinoco_unlock(priv, &flags);
3958
Christoph Hellwig620554e2005-06-19 01:27:33 +02003959 return err;
3960}
3961
3962static int orinoco_ioctl_getfreq(struct net_device *dev,
3963 struct iw_request_info *info,
3964 struct iw_freq *frq,
3965 char *extra)
3966{
3967 struct orinoco_private *priv = netdev_priv(dev);
3968 int tmp;
3969
3970 /* Locking done in there */
3971 tmp = orinoco_hw_get_freq(priv);
3972 if (tmp < 0) {
3973 return tmp;
3974 }
3975
3976 frq->m = tmp;
3977 frq->e = 1;
3978
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 return 0;
3980}
3981
Christoph Hellwig620554e2005-06-19 01:27:33 +02003982static int orinoco_ioctl_getsens(struct net_device *dev,
3983 struct iw_request_info *info,
3984 struct iw_param *srq,
3985 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986{
3987 struct orinoco_private *priv = netdev_priv(dev);
3988 hermes_t *hw = &priv->hw;
3989 u16 val;
3990 int err;
3991 unsigned long flags;
3992
3993 if (!priv->has_sensitivity)
3994 return -EOPNOTSUPP;
3995
3996 if (orinoco_lock(priv, &flags) != 0)
3997 return -EBUSY;
3998 err = hermes_read_wordrec(hw, USER_BAP,
3999 HERMES_RID_CNFSYSTEMSCALE, &val);
4000 orinoco_unlock(priv, &flags);
4001
4002 if (err)
4003 return err;
4004
4005 srq->value = val;
4006 srq->fixed = 0; /* auto */
4007
4008 return 0;
4009}
4010
Christoph Hellwig620554e2005-06-19 01:27:33 +02004011static int orinoco_ioctl_setsens(struct net_device *dev,
4012 struct iw_request_info *info,
4013 struct iw_param *srq,
4014 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015{
4016 struct orinoco_private *priv = netdev_priv(dev);
4017 int val = srq->value;
4018 unsigned long flags;
4019
4020 if (!priv->has_sensitivity)
4021 return -EOPNOTSUPP;
4022
4023 if ((val < 1) || (val > 3))
4024 return -EINVAL;
4025
4026 if (orinoco_lock(priv, &flags) != 0)
4027 return -EBUSY;
4028 priv->ap_density = val;
4029 orinoco_unlock(priv, &flags);
4030
Christoph Hellwig620554e2005-06-19 01:27:33 +02004031 return -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032}
4033
Christoph Hellwig620554e2005-06-19 01:27:33 +02004034static int orinoco_ioctl_setrts(struct net_device *dev,
4035 struct iw_request_info *info,
4036 struct iw_param *rrq,
4037 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038{
4039 struct orinoco_private *priv = netdev_priv(dev);
4040 int val = rrq->value;
4041 unsigned long flags;
4042
4043 if (rrq->disabled)
4044 val = 2347;
4045
4046 if ( (val < 0) || (val > 2347) )
4047 return -EINVAL;
4048
4049 if (orinoco_lock(priv, &flags) != 0)
4050 return -EBUSY;
4051
4052 priv->rts_thresh = val;
4053 orinoco_unlock(priv, &flags);
4054
Christoph Hellwig620554e2005-06-19 01:27:33 +02004055 return -EINPROGRESS; /* Call commit handler */
4056}
4057
4058static int orinoco_ioctl_getrts(struct net_device *dev,
4059 struct iw_request_info *info,
4060 struct iw_param *rrq,
4061 char *extra)
4062{
4063 struct orinoco_private *priv = netdev_priv(dev);
4064
4065 rrq->value = priv->rts_thresh;
4066 rrq->disabled = (rrq->value == 2347);
4067 rrq->fixed = 1;
4068
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 return 0;
4070}
4071
Christoph Hellwig620554e2005-06-19 01:27:33 +02004072static int orinoco_ioctl_setfrag(struct net_device *dev,
4073 struct iw_request_info *info,
4074 struct iw_param *frq,
4075 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076{
4077 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004078 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 unsigned long flags;
4080
4081 if (orinoco_lock(priv, &flags) != 0)
4082 return -EBUSY;
4083
4084 if (priv->has_mwo) {
4085 if (frq->disabled)
4086 priv->mwo_robust = 0;
4087 else {
4088 if (frq->fixed)
4089 printk(KERN_WARNING "%s: Fixed fragmentation is "
4090 "not supported on this firmware. "
4091 "Using MWO robust instead.\n", dev->name);
4092 priv->mwo_robust = 1;
4093 }
4094 } else {
4095 if (frq->disabled)
4096 priv->frag_thresh = 2346;
4097 else {
4098 if ( (frq->value < 256) || (frq->value > 2346) )
4099 err = -EINVAL;
4100 else
4101 priv->frag_thresh = frq->value & ~0x1; /* must be even */
4102 }
4103 }
4104
4105 orinoco_unlock(priv, &flags);
4106
4107 return err;
4108}
4109
Christoph Hellwig620554e2005-06-19 01:27:33 +02004110static int orinoco_ioctl_getfrag(struct net_device *dev,
4111 struct iw_request_info *info,
4112 struct iw_param *frq,
4113 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114{
4115 struct orinoco_private *priv = netdev_priv(dev);
4116 hermes_t *hw = &priv->hw;
Christoph Hellwig620554e2005-06-19 01:27:33 +02004117 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 u16 val;
4119 unsigned long flags;
4120
4121 if (orinoco_lock(priv, &flags) != 0)
4122 return -EBUSY;
4123
4124 if (priv->has_mwo) {
4125 err = hermes_read_wordrec(hw, USER_BAP,
4126 HERMES_RID_CNFMWOROBUST_AGERE,
4127 &val);
4128 if (err)
4129 val = 0;
4130
4131 frq->value = val ? 2347 : 0;
4132 frq->disabled = ! val;
4133 frq->fixed = 0;
4134 } else {
4135 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
4136 &val);
4137 if (err)
4138 val = 0;
4139
4140 frq->value = val;
4141 frq->disabled = (val >= 2346);
4142 frq->fixed = 1;
4143 }
4144
4145 orinoco_unlock(priv, &flags);
4146
4147 return err;
4148}
4149
Christoph Hellwig620554e2005-06-19 01:27:33 +02004150static int orinoco_ioctl_setrate(struct net_device *dev,
4151 struct iw_request_info *info,
4152 struct iw_param *rrq,
4153 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154{
4155 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 int ratemode = -1;
4157 int bitrate; /* 100s of kilobits */
4158 int i;
4159 unsigned long flags;
4160
4161 /* As the user space doesn't know our highest rate, it uses -1
4162 * to ask us to set the highest rate. Test it using "iwconfig
4163 * ethX rate auto" - Jean II */
4164 if (rrq->value == -1)
4165 bitrate = 110;
4166 else {
4167 if (rrq->value % 100000)
4168 return -EINVAL;
4169 bitrate = rrq->value / 100000;
4170 }
4171
4172 if ( (bitrate != 10) && (bitrate != 20) &&
4173 (bitrate != 55) && (bitrate != 110) )
4174 return -EINVAL;
4175
4176 for (i = 0; i < BITRATE_TABLE_SIZE; i++)
4177 if ( (bitrate_table[i].bitrate == bitrate) &&
4178 (bitrate_table[i].automatic == ! rrq->fixed) ) {
4179 ratemode = i;
4180 break;
4181 }
4182
4183 if (ratemode == -1)
4184 return -EINVAL;
4185
4186 if (orinoco_lock(priv, &flags) != 0)
4187 return -EBUSY;
4188 priv->bitratemode = ratemode;
4189 orinoco_unlock(priv, &flags);
4190
Christoph Hellwig620554e2005-06-19 01:27:33 +02004191 return -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192}
4193
Christoph Hellwig620554e2005-06-19 01:27:33 +02004194static int orinoco_ioctl_getrate(struct net_device *dev,
4195 struct iw_request_info *info,
4196 struct iw_param *rrq,
4197 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198{
4199 struct orinoco_private *priv = netdev_priv(dev);
4200 hermes_t *hw = &priv->hw;
4201 int err = 0;
4202 int ratemode;
4203 int i;
4204 u16 val;
4205 unsigned long flags;
4206
4207 if (orinoco_lock(priv, &flags) != 0)
4208 return -EBUSY;
4209
4210 ratemode = priv->bitratemode;
4211
4212 BUG_ON((ratemode < 0) || (ratemode >= BITRATE_TABLE_SIZE));
4213
4214 rrq->value = bitrate_table[ratemode].bitrate * 100000;
4215 rrq->fixed = ! bitrate_table[ratemode].automatic;
4216 rrq->disabled = 0;
4217
4218 /* If the interface is running we try to find more about the
4219 current mode */
4220 if (netif_running(dev)) {
4221 err = hermes_read_wordrec(hw, USER_BAP,
4222 HERMES_RID_CURRENTTXRATE, &val);
4223 if (err)
4224 goto out;
4225
4226 switch (priv->firmware_type) {
4227 case FIRMWARE_TYPE_AGERE: /* Lucent style rate */
4228 /* Note : in Lucent firmware, the return value of
4229 * HERMES_RID_CURRENTTXRATE is the bitrate in Mb/s,
4230 * and therefore is totally different from the
4231 * encoding of HERMES_RID_CNFTXRATECONTROL.
4232 * Don't forget that 6Mb/s is really 5.5Mb/s */
4233 if (val == 6)
4234 rrq->value = 5500000;
4235 else
4236 rrq->value = val * 1000000;
4237 break;
4238 case FIRMWARE_TYPE_INTERSIL: /* Intersil style rate */
4239 case FIRMWARE_TYPE_SYMBOL: /* Symbol style rate */
4240 for (i = 0; i < BITRATE_TABLE_SIZE; i++)
4241 if (bitrate_table[i].intersil_txratectrl == val) {
4242 ratemode = i;
4243 break;
4244 }
4245 if (i >= BITRATE_TABLE_SIZE)
4246 printk(KERN_INFO "%s: Unable to determine current bitrate (0x%04hx)\n",
4247 dev->name, val);
4248
4249 rrq->value = bitrate_table[ratemode].bitrate * 100000;
4250 break;
4251 default:
4252 BUG();
4253 }
4254 }
4255
4256 out:
4257 orinoco_unlock(priv, &flags);
4258
4259 return err;
4260}
4261
Christoph Hellwig620554e2005-06-19 01:27:33 +02004262static int orinoco_ioctl_setpower(struct net_device *dev,
4263 struct iw_request_info *info,
4264 struct iw_param *prq,
4265 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266{
4267 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004268 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 unsigned long flags;
4270
4271 if (orinoco_lock(priv, &flags) != 0)
4272 return -EBUSY;
4273
4274 if (prq->disabled) {
4275 priv->pm_on = 0;
4276 } else {
4277 switch (prq->flags & IW_POWER_MODE) {
4278 case IW_POWER_UNICAST_R:
4279 priv->pm_mcast = 0;
4280 priv->pm_on = 1;
4281 break;
4282 case IW_POWER_ALL_R:
4283 priv->pm_mcast = 1;
4284 priv->pm_on = 1;
4285 break;
4286 case IW_POWER_ON:
4287 /* No flags : but we may have a value - Jean II */
4288 break;
4289 default:
4290 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 goto out;
Pavel Roskinc08ad1e2005-11-29 02:59:27 -05004292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293
4294 if (prq->flags & IW_POWER_TIMEOUT) {
4295 priv->pm_on = 1;
4296 priv->pm_timeout = prq->value / 1000;
4297 }
4298 if (prq->flags & IW_POWER_PERIOD) {
4299 priv->pm_on = 1;
4300 priv->pm_period = prq->value / 1000;
4301 }
4302 /* It's valid to not have a value if we are just toggling
4303 * the flags... Jean II */
4304 if(!priv->pm_on) {
4305 err = -EINVAL;
4306 goto out;
4307 }
4308 }
4309
4310 out:
4311 orinoco_unlock(priv, &flags);
4312
4313 return err;
4314}
4315
Christoph Hellwig620554e2005-06-19 01:27:33 +02004316static int orinoco_ioctl_getpower(struct net_device *dev,
4317 struct iw_request_info *info,
4318 struct iw_param *prq,
4319 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320{
4321 struct orinoco_private *priv = netdev_priv(dev);
4322 hermes_t *hw = &priv->hw;
4323 int err = 0;
4324 u16 enable, period, timeout, mcast;
4325 unsigned long flags;
4326
4327 if (orinoco_lock(priv, &flags) != 0)
4328 return -EBUSY;
4329
4330 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFPMENABLED, &enable);
4331 if (err)
4332 goto out;
4333
4334 err = hermes_read_wordrec(hw, USER_BAP,
4335 HERMES_RID_CNFMAXSLEEPDURATION, &period);
4336 if (err)
4337 goto out;
4338
4339 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFPMHOLDOVERDURATION, &timeout);
4340 if (err)
4341 goto out;
4342
4343 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFMULTICASTRECEIVE, &mcast);
4344 if (err)
4345 goto out;
4346
4347 prq->disabled = !enable;
4348 /* Note : by default, display the period */
4349 if ((prq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
4350 prq->flags = IW_POWER_TIMEOUT;
4351 prq->value = timeout * 1000;
4352 } else {
4353 prq->flags = IW_POWER_PERIOD;
4354 prq->value = period * 1000;
4355 }
4356 if (mcast)
4357 prq->flags |= IW_POWER_ALL_R;
4358 else
4359 prq->flags |= IW_POWER_UNICAST_R;
4360
4361 out:
4362 orinoco_unlock(priv, &flags);
4363
4364 return err;
4365}
4366
David Kilroyd03032a2008-08-21 23:28:02 +01004367static int orinoco_ioctl_set_encodeext(struct net_device *dev,
4368 struct iw_request_info *info,
4369 union iwreq_data *wrqu,
4370 char *extra)
4371{
4372 struct orinoco_private *priv = netdev_priv(dev);
4373 struct iw_point *encoding = &wrqu->encoding;
4374 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
4375 int idx, alg = ext->alg, set_key = 1;
4376 unsigned long flags;
4377 int err = -EINVAL;
4378 u16 key_len;
4379
4380 if (orinoco_lock(priv, &flags) != 0)
4381 return -EBUSY;
4382
4383 /* Determine and validate the key index */
4384 idx = encoding->flags & IW_ENCODE_INDEX;
4385 if (idx) {
4386 if ((idx < 1) || (idx > WEP_KEYS))
4387 goto out;
4388 idx--;
4389 } else
4390 idx = priv->tx_key;
4391
4392 if (encoding->flags & IW_ENCODE_DISABLED)
4393 alg = IW_ENCODE_ALG_NONE;
4394
4395 if (priv->has_wpa && (alg != IW_ENCODE_ALG_TKIP)) {
4396 /* Clear any TKIP TX key we had */
4397 (void) orinoco_clear_tkip_key(priv, priv->tx_key);
4398 }
4399
4400 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
4401 priv->tx_key = idx;
4402 set_key = ((alg == IW_ENCODE_ALG_TKIP) ||
4403 (ext->key_len > 0)) ? 1 : 0;
4404 }
4405
4406 if (set_key) {
4407 /* Set the requested key first */
4408 switch (alg) {
4409 case IW_ENCODE_ALG_NONE:
4410 priv->encode_alg = alg;
4411 priv->keys[idx].len = 0;
4412 break;
4413
4414 case IW_ENCODE_ALG_WEP:
4415 if (ext->key_len > SMALL_KEY_SIZE)
4416 key_len = LARGE_KEY_SIZE;
4417 else if (ext->key_len > 0)
4418 key_len = SMALL_KEY_SIZE;
4419 else
4420 goto out;
4421
4422 priv->encode_alg = alg;
4423 priv->keys[idx].len = cpu_to_le16(key_len);
4424
4425 key_len = min(ext->key_len, key_len);
4426
4427 memset(priv->keys[idx].data, 0, ORINOCO_MAX_KEY_SIZE);
4428 memcpy(priv->keys[idx].data, ext->key, key_len);
4429 break;
4430
4431 case IW_ENCODE_ALG_TKIP:
4432 {
4433 hermes_t *hw = &priv->hw;
4434 u8 *tkip_iv = NULL;
4435
4436 if (!priv->has_wpa ||
4437 (ext->key_len > sizeof(priv->tkip_key[0])))
4438 goto out;
4439
4440 priv->encode_alg = alg;
4441 memset(&priv->tkip_key[idx], 0,
4442 sizeof(priv->tkip_key[idx]));
4443 memcpy(&priv->tkip_key[idx], ext->key, ext->key_len);
4444
4445 if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID)
4446 tkip_iv = &ext->rx_seq[0];
4447
4448 err = __orinoco_hw_set_tkip_key(hw, idx,
4449 ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY,
4450 (u8 *) &priv->tkip_key[idx],
4451 tkip_iv, NULL);
4452 if (err)
4453 printk(KERN_ERR "%s: Error %d setting TKIP key"
4454 "\n", dev->name, err);
4455
4456 goto out;
4457 }
4458 default:
4459 goto out;
4460 }
4461 }
4462 err = -EINPROGRESS;
4463 out:
4464 orinoco_unlock(priv, &flags);
4465
4466 return err;
4467}
4468
4469static int orinoco_ioctl_get_encodeext(struct net_device *dev,
4470 struct iw_request_info *info,
4471 union iwreq_data *wrqu,
4472 char *extra)
4473{
4474 struct orinoco_private *priv = netdev_priv(dev);
4475 struct iw_point *encoding = &wrqu->encoding;
4476 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
4477 int idx, max_key_len;
4478 unsigned long flags;
4479 int err;
4480
4481 if (orinoco_lock(priv, &flags) != 0)
4482 return -EBUSY;
4483
4484 err = -EINVAL;
4485 max_key_len = encoding->length - sizeof(*ext);
4486 if (max_key_len < 0)
4487 goto out;
4488
4489 idx = encoding->flags & IW_ENCODE_INDEX;
4490 if (idx) {
4491 if ((idx < 1) || (idx > WEP_KEYS))
4492 goto out;
4493 idx--;
4494 } else
4495 idx = priv->tx_key;
4496
4497 encoding->flags = idx + 1;
4498 memset(ext, 0, sizeof(*ext));
4499
4500 ext->alg = priv->encode_alg;
4501 switch (priv->encode_alg) {
4502 case IW_ENCODE_ALG_NONE:
4503 ext->key_len = 0;
4504 encoding->flags |= IW_ENCODE_DISABLED;
4505 break;
4506 case IW_ENCODE_ALG_WEP:
4507 ext->key_len = min(le16_to_cpu(priv->keys[idx].len),
4508 (u16) max_key_len);
4509 memcpy(ext->key, priv->keys[idx].data, ext->key_len);
4510 encoding->flags |= IW_ENCODE_ENABLED;
4511 break;
4512 case IW_ENCODE_ALG_TKIP:
4513 ext->key_len = min((u16) sizeof(struct orinoco_tkip_key),
4514 (u16) max_key_len);
4515 memcpy(ext->key, &priv->tkip_key[idx], ext->key_len);
4516 encoding->flags |= IW_ENCODE_ENABLED;
4517 break;
4518 }
4519
4520 err = 0;
4521 out:
4522 orinoco_unlock(priv, &flags);
4523
4524 return err;
4525}
4526
4527static int orinoco_ioctl_set_auth(struct net_device *dev,
4528 struct iw_request_info *info,
4529 union iwreq_data *wrqu, char *extra)
4530{
4531 struct orinoco_private *priv = netdev_priv(dev);
4532 hermes_t *hw = &priv->hw;
4533 struct iw_param *param = &wrqu->param;
4534 unsigned long flags;
4535 int ret = -EINPROGRESS;
4536
4537 if (orinoco_lock(priv, &flags) != 0)
4538 return -EBUSY;
4539
4540 switch (param->flags & IW_AUTH_INDEX) {
4541 case IW_AUTH_WPA_VERSION:
4542 case IW_AUTH_CIPHER_PAIRWISE:
4543 case IW_AUTH_CIPHER_GROUP:
4544 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
4545 case IW_AUTH_PRIVACY_INVOKED:
4546 case IW_AUTH_DROP_UNENCRYPTED:
4547 /*
4548 * orinoco does not use these parameters
4549 */
4550 break;
4551
4552 case IW_AUTH_KEY_MGMT:
4553 /* wl_lkm implies value 2 == PSK for Hermes I
4554 * which ties in with WEXT
4555 * no other hints tho :(
4556 */
4557 priv->key_mgmt = param->value;
4558 break;
4559
4560 case IW_AUTH_TKIP_COUNTERMEASURES:
4561 /* When countermeasures are enabled, shut down the
4562 * card; when disabled, re-enable the card. This must
4563 * take effect immediately.
4564 *
4565 * TODO: Make sure that the EAPOL message is getting
4566 * out before card disabled
4567 */
4568 if (param->value) {
4569 priv->tkip_cm_active = 1;
4570 ret = hermes_enable_port(hw, 0);
4571 } else {
4572 priv->tkip_cm_active = 0;
4573 ret = hermes_disable_port(hw, 0);
4574 }
4575 break;
4576
4577 case IW_AUTH_80211_AUTH_ALG:
4578 if (param->value & IW_AUTH_ALG_SHARED_KEY)
4579 priv->wep_restrict = 1;
4580 else if (param->value & IW_AUTH_ALG_OPEN_SYSTEM)
4581 priv->wep_restrict = 0;
4582 else
4583 ret = -EINVAL;
4584 break;
4585
4586 case IW_AUTH_WPA_ENABLED:
4587 if (priv->has_wpa) {
4588 priv->wpa_enabled = param->value ? 1 : 0;
4589 } else {
4590 if (param->value)
4591 ret = -EOPNOTSUPP;
4592 /* else silently accept disable of WPA */
4593 priv->wpa_enabled = 0;
4594 }
4595 break;
4596
4597 default:
4598 ret = -EOPNOTSUPP;
4599 }
4600
4601 orinoco_unlock(priv, &flags);
4602 return ret;
4603}
4604
4605static int orinoco_ioctl_get_auth(struct net_device *dev,
4606 struct iw_request_info *info,
4607 union iwreq_data *wrqu, char *extra)
4608{
4609 struct orinoco_private *priv = netdev_priv(dev);
4610 struct iw_param *param = &wrqu->param;
4611 unsigned long flags;
4612 int ret = 0;
4613
4614 if (orinoco_lock(priv, &flags) != 0)
4615 return -EBUSY;
4616
4617 switch (param->flags & IW_AUTH_INDEX) {
4618 case IW_AUTH_KEY_MGMT:
4619 param->value = priv->key_mgmt;
4620 break;
4621
4622 case IW_AUTH_TKIP_COUNTERMEASURES:
4623 param->value = priv->tkip_cm_active;
4624 break;
4625
4626 case IW_AUTH_80211_AUTH_ALG:
4627 if (priv->wep_restrict)
4628 param->value = IW_AUTH_ALG_SHARED_KEY;
4629 else
4630 param->value = IW_AUTH_ALG_OPEN_SYSTEM;
4631 break;
4632
4633 case IW_AUTH_WPA_ENABLED:
4634 param->value = priv->wpa_enabled;
4635 break;
4636
4637 default:
4638 ret = -EOPNOTSUPP;
4639 }
4640
4641 orinoco_unlock(priv, &flags);
4642 return ret;
4643}
4644
4645static int orinoco_ioctl_set_genie(struct net_device *dev,
4646 struct iw_request_info *info,
4647 union iwreq_data *wrqu, char *extra)
4648{
4649 struct orinoco_private *priv = netdev_priv(dev);
4650 u8 *buf;
4651 unsigned long flags;
4652 int err = 0;
4653
4654 if ((wrqu->data.length > MAX_WPA_IE_LEN) ||
4655 (wrqu->data.length && (extra == NULL)))
4656 return -EINVAL;
4657
4658 if (orinoco_lock(priv, &flags) != 0)
4659 return -EBUSY;
4660
4661 if (wrqu->data.length) {
4662 buf = kmalloc(wrqu->data.length, GFP_KERNEL);
4663 if (buf == NULL) {
4664 err = -ENOMEM;
4665 goto out;
4666 }
4667
4668 memcpy(buf, extra, wrqu->data.length);
4669 kfree(priv->wpa_ie);
4670 priv->wpa_ie = buf;
4671 priv->wpa_ie_len = wrqu->data.length;
4672 } else {
4673 kfree(priv->wpa_ie);
4674 priv->wpa_ie = NULL;
4675 priv->wpa_ie_len = 0;
4676 }
4677
4678 if (priv->wpa_ie) {
4679 /* Looks like wl_lkm wants to check the auth alg, and
4680 * somehow pass it to the firmware.
4681 * Instead it just calls the key mgmt rid
4682 * - we do this in set auth.
4683 */
4684 }
4685
4686out:
4687 orinoco_unlock(priv, &flags);
4688 return err;
4689}
4690
4691static int orinoco_ioctl_get_genie(struct net_device *dev,
4692 struct iw_request_info *info,
4693 union iwreq_data *wrqu, char *extra)
4694{
4695 struct orinoco_private *priv = netdev_priv(dev);
4696 unsigned long flags;
4697 int err = 0;
4698
4699 if (orinoco_lock(priv, &flags) != 0)
4700 return -EBUSY;
4701
4702 if ((priv->wpa_ie_len == 0) || (priv->wpa_ie == NULL)) {
4703 wrqu->data.length = 0;
4704 goto out;
4705 }
4706
4707 if (wrqu->data.length < priv->wpa_ie_len) {
4708 err = -E2BIG;
4709 goto out;
4710 }
4711
4712 wrqu->data.length = priv->wpa_ie_len;
4713 memcpy(extra, priv->wpa_ie, priv->wpa_ie_len);
4714
4715out:
4716 orinoco_unlock(priv, &flags);
4717 return err;
4718}
4719
4720static int orinoco_ioctl_set_mlme(struct net_device *dev,
4721 struct iw_request_info *info,
4722 union iwreq_data *wrqu, char *extra)
4723{
4724 struct orinoco_private *priv = netdev_priv(dev);
4725 hermes_t *hw = &priv->hw;
4726 struct iw_mlme *mlme = (struct iw_mlme *)extra;
4727 unsigned long flags;
4728 int ret = 0;
4729
4730 if (orinoco_lock(priv, &flags) != 0)
4731 return -EBUSY;
4732
4733 switch (mlme->cmd) {
4734 case IW_MLME_DEAUTH:
4735 /* silently ignore */
4736 break;
4737
4738 case IW_MLME_DISASSOC:
4739 {
4740 struct {
4741 u8 addr[ETH_ALEN];
4742 __le16 reason_code;
4743 } __attribute__ ((packed)) buf;
4744
4745 memcpy(buf.addr, mlme->addr.sa_data, ETH_ALEN);
4746 buf.reason_code = cpu_to_le16(mlme->reason_code);
4747 ret = HERMES_WRITE_RECORD(hw, USER_BAP,
4748 HERMES_RID_CNFDISASSOCIATE,
4749 &buf);
4750 break;
4751 }
4752 default:
4753 ret = -EOPNOTSUPP;
4754 }
4755
4756 orinoco_unlock(priv, &flags);
4757 return ret;
4758}
4759
Christoph Hellwig620554e2005-06-19 01:27:33 +02004760static int orinoco_ioctl_getretry(struct net_device *dev,
4761 struct iw_request_info *info,
4762 struct iw_param *rrq,
4763 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764{
4765 struct orinoco_private *priv = netdev_priv(dev);
4766 hermes_t *hw = &priv->hw;
4767 int err = 0;
4768 u16 short_limit, long_limit, lifetime;
4769 unsigned long flags;
4770
4771 if (orinoco_lock(priv, &flags) != 0)
4772 return -EBUSY;
4773
4774 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_SHORTRETRYLIMIT,
4775 &short_limit);
4776 if (err)
4777 goto out;
4778
4779 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_LONGRETRYLIMIT,
4780 &long_limit);
4781 if (err)
4782 goto out;
4783
4784 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_MAXTRANSMITLIFETIME,
4785 &lifetime);
4786 if (err)
4787 goto out;
4788
4789 rrq->disabled = 0; /* Can't be disabled */
4790
4791 /* Note : by default, display the retry number */
4792 if ((rrq->flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME) {
4793 rrq->flags = IW_RETRY_LIFETIME;
4794 rrq->value = lifetime * 1000; /* ??? */
4795 } else {
4796 /* By default, display the min number */
Jean Tourrilheseeec9f12006-08-29 18:02:31 -07004797 if ((rrq->flags & IW_RETRY_LONG)) {
4798 rrq->flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 rrq->value = long_limit;
4800 } else {
4801 rrq->flags = IW_RETRY_LIMIT;
4802 rrq->value = short_limit;
4803 if(short_limit != long_limit)
Jean Tourrilheseeec9f12006-08-29 18:02:31 -07004804 rrq->flags |= IW_RETRY_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 }
4806 }
4807
4808 out:
4809 orinoco_unlock(priv, &flags);
4810
4811 return err;
4812}
4813
Christoph Hellwig620554e2005-06-19 01:27:33 +02004814static int orinoco_ioctl_reset(struct net_device *dev,
4815 struct iw_request_info *info,
4816 void *wrqu,
4817 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818{
4819 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004820
4821 if (! capable(CAP_NET_ADMIN))
4822 return -EPERM;
4823
4824 if (info->cmd == (SIOCIWFIRSTPRIV + 0x1)) {
4825 printk(KERN_DEBUG "%s: Forcing reset!\n", dev->name);
4826
4827 /* Firmware reset */
David Howellsc4028952006-11-22 14:57:56 +00004828 orinoco_reset(&priv->reset_work);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004829 } else {
4830 printk(KERN_DEBUG "%s: Force scheduling reset!\n", dev->name);
4831
4832 schedule_work(&priv->reset_work);
4833 }
4834
4835 return 0;
4836}
4837
4838static int orinoco_ioctl_setibssport(struct net_device *dev,
4839 struct iw_request_info *info,
4840 void *wrqu,
4841 char *extra)
4842
4843{
4844 struct orinoco_private *priv = netdev_priv(dev);
4845 int val = *( (int *) extra );
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 unsigned long flags;
4847
4848 if (orinoco_lock(priv, &flags) != 0)
4849 return -EBUSY;
4850
4851 priv->ibss_port = val ;
4852
4853 /* Actually update the mode we are using */
4854 set_port_type(priv);
4855
4856 orinoco_unlock(priv, &flags);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004857 return -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858}
4859
Christoph Hellwig620554e2005-06-19 01:27:33 +02004860static int orinoco_ioctl_getibssport(struct net_device *dev,
4861 struct iw_request_info *info,
4862 void *wrqu,
4863 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864{
4865 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004866 int *val = (int *) extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867
4868 *val = priv->ibss_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 return 0;
4870}
4871
Christoph Hellwig620554e2005-06-19 01:27:33 +02004872static int orinoco_ioctl_setport3(struct net_device *dev,
4873 struct iw_request_info *info,
4874 void *wrqu,
4875 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876{
4877 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004878 int val = *( (int *) extra );
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 int err = 0;
4880 unsigned long flags;
4881
4882 if (orinoco_lock(priv, &flags) != 0)
4883 return -EBUSY;
4884
4885 switch (val) {
4886 case 0: /* Try to do IEEE ad-hoc mode */
4887 if (! priv->has_ibss) {
4888 err = -EINVAL;
4889 break;
4890 }
4891 priv->prefer_port3 = 0;
4892
4893 break;
4894
4895 case 1: /* Try to do Lucent proprietary ad-hoc mode */
4896 if (! priv->has_port3) {
4897 err = -EINVAL;
4898 break;
4899 }
4900 priv->prefer_port3 = 1;
4901 break;
4902
4903 default:
4904 err = -EINVAL;
4905 }
4906
Christoph Hellwig620554e2005-06-19 01:27:33 +02004907 if (! err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 /* Actually update the mode we are using */
4909 set_port_type(priv);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004910 err = -EINPROGRESS;
4911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912
4913 orinoco_unlock(priv, &flags);
4914
4915 return err;
4916}
4917
Christoph Hellwig620554e2005-06-19 01:27:33 +02004918static int orinoco_ioctl_getport3(struct net_device *dev,
4919 struct iw_request_info *info,
4920 void *wrqu,
4921 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922{
4923 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004924 int *val = (int *) extra;
4925
4926 *val = priv->prefer_port3;
4927 return 0;
4928}
4929
4930static int orinoco_ioctl_setpreamble(struct net_device *dev,
4931 struct iw_request_info *info,
4932 void *wrqu,
4933 char *extra)
4934{
4935 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 unsigned long flags;
Christoph Hellwig620554e2005-06-19 01:27:33 +02004937 int val;
4938
4939 if (! priv->has_preamble)
4940 return -EOPNOTSUPP;
4941
4942 /* 802.11b has recently defined some short preamble.
4943 * Basically, the Phy header has been reduced in size.
4944 * This increase performance, especially at high rates
4945 * (the preamble is transmitted at 1Mb/s), unfortunately
4946 * this give compatibility troubles... - Jean II */
4947 val = *( (int *) extra );
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948
4949 if (orinoco_lock(priv, &flags) != 0)
4950 return -EBUSY;
4951
Christoph Hellwig620554e2005-06-19 01:27:33 +02004952 if (val)
4953 priv->preamble = 1;
4954 else
4955 priv->preamble = 0;
4956
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 orinoco_unlock(priv, &flags);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004958
4959 return -EINPROGRESS; /* Call commit handler */
4960}
4961
4962static int orinoco_ioctl_getpreamble(struct net_device *dev,
4963 struct iw_request_info *info,
4964 void *wrqu,
4965 char *extra)
4966{
4967 struct orinoco_private *priv = netdev_priv(dev);
4968 int *val = (int *) extra;
4969
4970 if (! priv->has_preamble)
4971 return -EOPNOTSUPP;
4972
4973 *val = priv->preamble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 return 0;
4975}
4976
Christoph Hellwig620554e2005-06-19 01:27:33 +02004977/* ioctl interface to hermes_read_ltv()
4978 * To use with iwpriv, pass the RID as the token argument, e.g.
4979 * iwpriv get_rid [0xfc00]
4980 * At least Wireless Tools 25 is required to use iwpriv.
4981 * For Wireless Tools 25 and 26 append "dummy" are the end. */
4982static int orinoco_ioctl_getrid(struct net_device *dev,
4983 struct iw_request_info *info,
4984 struct iw_point *data,
4985 char *extra)
4986{
4987 struct orinoco_private *priv = netdev_priv(dev);
4988 hermes_t *hw = &priv->hw;
4989 int rid = data->flags;
4990 u16 length;
4991 int err;
4992 unsigned long flags;
4993
4994 /* It's a "get" function, but we don't want users to access the
4995 * WEP key and other raw firmware data */
4996 if (! capable(CAP_NET_ADMIN))
4997 return -EPERM;
4998
4999 if (rid < 0xfc00 || rid > 0xffff)
5000 return -EINVAL;
5001
5002 if (orinoco_lock(priv, &flags) != 0)
5003 return -EBUSY;
5004
5005 err = hermes_read_ltv(hw, USER_BAP, rid, MAX_RID_LEN, &length,
5006 extra);
5007 if (err)
5008 goto out;
5009
5010 data->length = min_t(u16, HERMES_RECLEN_TO_BYTES(length),
5011 MAX_RID_LEN);
5012
5013 out:
5014 orinoco_unlock(priv, &flags);
5015 return err;
5016}
5017
David Kilroy17a1a882008-08-21 23:27:47 +01005018/* Trigger a scan (look for other cells in the vicinity) */
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005019static int orinoco_ioctl_setscan(struct net_device *dev,
5020 struct iw_request_info *info,
5021 struct iw_param *srq,
5022 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023{
5024 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005025 hermes_t *hw = &priv->hw;
David Kilroy0753bba2008-08-21 23:27:46 +01005026 struct iw_scan_req *si = (struct iw_scan_req *) extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 unsigned long flags;
5029
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005030 /* Note : you may have realised that, as this is a SET operation,
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -08005031 * this is privileged and therefore a normal user can't
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005032 * perform scanning.
5033 * This is not an error, while the device perform scanning,
5034 * traffic doesn't flow, so it's a perfect DoS...
5035 * Jean II */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005037 if (orinoco_lock(priv, &flags) != 0)
5038 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005040 /* Scanning with port 0 disabled would fail */
5041 if (!netif_running(dev)) {
5042 err = -ENETDOWN;
5043 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005046 /* In monitor mode, the scan results are always empty.
5047 * Probe responses are passed to the driver as received
5048 * frames and could be processed in software. */
5049 if (priv->iw_mode == IW_MODE_MONITOR) {
5050 err = -EOPNOTSUPP;
5051 goto out;
5052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005054 /* Note : because we don't lock out the irq handler, the way
5055 * we access scan variables in priv is critical.
5056 * o scan_inprogress : not touched by irq handler
5057 * o scan_mode : not touched by irq handler
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005058 * Before modifying anything on those variables, please think hard !
5059 * Jean II */
5060
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005061 /* Save flags */
5062 priv->scan_mode = srq->flags;
5063
5064 /* Always trigger scanning, even if it's in progress.
5065 * This way, if the info frame get lost, we will recover somewhat
5066 * gracefully - Jean II */
5067
5068 if (priv->has_hostscan) {
5069 switch (priv->firmware_type) {
5070 case FIRMWARE_TYPE_SYMBOL:
5071 err = hermes_write_wordrec(hw, USER_BAP,
5072 HERMES_RID_CNFHOSTSCAN_SYMBOL,
5073 HERMES_HOSTSCAN_SYMBOL_ONCE |
5074 HERMES_HOSTSCAN_SYMBOL_BCAST);
5075 break;
5076 case FIRMWARE_TYPE_INTERSIL: {
Pavel Roskind133ae42005-09-23 04:18:06 -04005077 __le16 req[3];
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005078
5079 req[0] = cpu_to_le16(0x3fff); /* All channels */
5080 req[1] = cpu_to_le16(0x0001); /* rate 1 Mbps */
5081 req[2] = 0; /* Any ESSID */
5082 err = HERMES_WRITE_RECORD(hw, USER_BAP,
5083 HERMES_RID_CNFHOSTSCAN, &req);
5084 }
5085 break;
5086 case FIRMWARE_TYPE_AGERE:
David Kilroy0753bba2008-08-21 23:27:46 +01005087 if (priv->scan_mode & IW_SCAN_THIS_ESSID) {
5088 struct hermes_idstring idbuf;
5089 size_t len = min(sizeof(idbuf.val),
5090 (size_t) si->essid_len);
5091 idbuf.len = cpu_to_le16(len);
5092 memcpy(idbuf.val, si->essid, len);
5093
5094 err = hermes_write_ltv(hw, USER_BAP,
5095 HERMES_RID_CNFSCANSSID_AGERE,
5096 HERMES_BYTES_TO_RECLEN(len + 2),
5097 &idbuf);
5098 } else
5099 err = hermes_write_wordrec(hw, USER_BAP,
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005100 HERMES_RID_CNFSCANSSID_AGERE,
5101 0); /* Any ESSID */
5102 if (err)
5103 break;
5104
David Kilroy01632fa2008-08-21 23:27:58 +01005105 if (priv->has_ext_scan) {
5106 /* Clear scan results at the start of
5107 * an extended scan */
5108 orinoco_clear_scan_results(priv,
5109 msecs_to_jiffies(15000));
5110
5111 /* TODO: Is this available on older firmware?
5112 * Can we use it to scan specific channels
5113 * for IW_SCAN_THIS_FREQ? */
5114 err = hermes_write_wordrec(hw, USER_BAP,
5115 HERMES_RID_CNFSCANCHANNELS2GHZ,
5116 0x7FFF);
5117 if (err)
5118 goto out;
5119
5120 err = hermes_inquire(hw,
5121 HERMES_INQ_CHANNELINFO);
5122 } else
5123 err = hermes_inquire(hw, HERMES_INQ_SCAN);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005124 break;
5125 }
5126 } else
5127 err = hermes_inquire(hw, HERMES_INQ_SCAN);
5128
5129 /* One more client */
5130 if (! err)
5131 priv->scan_inprogress = 1;
5132
5133 out:
5134 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 return err;
5136}
5137
Dan Williams1e3428e2007-10-10 23:56:25 -04005138#define MAX_CUSTOM_LEN 64
5139
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005140/* Translate scan data returned from the card to a card independant
David Kilroy17a1a882008-08-21 23:27:47 +01005141 * format that the Wireless Tools will understand - Jean II */
Dan Williams1e3428e2007-10-10 23:56:25 -04005142static inline char *orinoco_translate_scan(struct net_device *dev,
David S. Millerccc58052008-06-16 18:50:49 -07005143 struct iw_request_info *info,
Dan Williams1e3428e2007-10-10 23:56:25 -04005144 char *current_ev,
5145 char *end_buf,
5146 union hermes_scan_info *bss,
5147 unsigned int last_scanned)
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005148{
5149 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005150 u16 capabilities;
5151 u16 channel;
5152 struct iw_event iwe; /* Temporary buffer */
Dan Williams1e3428e2007-10-10 23:56:25 -04005153 char custom[MAX_CUSTOM_LEN];
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005154
David Kilroy17a1a882008-08-21 23:27:47 +01005155 memset(&iwe, 0, sizeof(iwe));
5156
Dan Williams1e3428e2007-10-10 23:56:25 -04005157 /* First entry *MUST* be the AP MAC address */
5158 iwe.cmd = SIOCGIWAP;
5159 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
5160 memcpy(iwe.u.ap_addr.sa_data, bss->a.bssid, ETH_ALEN);
David S. Millerccc58052008-06-16 18:50:49 -07005161 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5162 &iwe, IW_EV_ADDR_LEN);
Dan Williams1e3428e2007-10-10 23:56:25 -04005163
5164 /* Other entries will be displayed in the order we give them */
5165
5166 /* Add the ESSID */
5167 iwe.u.data.length = le16_to_cpu(bss->a.essid_len);
5168 if (iwe.u.data.length > 32)
5169 iwe.u.data.length = 32;
5170 iwe.cmd = SIOCGIWESSID;
5171 iwe.u.data.flags = 1;
David S. Millerccc58052008-06-16 18:50:49 -07005172 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5173 &iwe, bss->a.essid);
Dan Williams1e3428e2007-10-10 23:56:25 -04005174
5175 /* Add mode */
5176 iwe.cmd = SIOCGIWMODE;
5177 capabilities = le16_to_cpu(bss->a.capabilities);
David Kilroy17a1a882008-08-21 23:27:47 +01005178 if (capabilities & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
5179 if (capabilities & WLAN_CAPABILITY_ESS)
Dan Williams1e3428e2007-10-10 23:56:25 -04005180 iwe.u.mode = IW_MODE_MASTER;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005181 else
Dan Williams1e3428e2007-10-10 23:56:25 -04005182 iwe.u.mode = IW_MODE_ADHOC;
David S. Millerccc58052008-06-16 18:50:49 -07005183 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5184 &iwe, IW_EV_UINT_LEN);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005185 }
5186
Dan Williams1e3428e2007-10-10 23:56:25 -04005187 channel = bss->s.channel;
5188 if ((channel >= 1) && (channel <= NUM_CHANNELS)) {
David Kilroy17a1a882008-08-21 23:27:47 +01005189 /* Add channel and frequency */
Dan Williams1e3428e2007-10-10 23:56:25 -04005190 iwe.cmd = SIOCGIWFREQ;
David Kilroy17a1a882008-08-21 23:27:47 +01005191 iwe.u.freq.m = channel;
5192 iwe.u.freq.e = 0;
5193 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5194 &iwe, IW_EV_FREQ_LEN);
5195
Dan Williams1e3428e2007-10-10 23:56:25 -04005196 iwe.u.freq.m = channel_frequency[channel-1] * 100000;
5197 iwe.u.freq.e = 1;
David S. Millerccc58052008-06-16 18:50:49 -07005198 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
Dan Williams1e3428e2007-10-10 23:56:25 -04005199 &iwe, IW_EV_FREQ_LEN);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005200 }
5201
David Kilroy17a1a882008-08-21 23:27:47 +01005202 /* Add quality statistics. level and noise in dB. No link quality */
Dan Williams1e3428e2007-10-10 23:56:25 -04005203 iwe.cmd = IWEVQUAL;
David Kilroy17a1a882008-08-21 23:27:47 +01005204 iwe.u.qual.updated = IW_QUAL_DBM | IW_QUAL_QUAL_INVALID;
Dan Williams1e3428e2007-10-10 23:56:25 -04005205 iwe.u.qual.level = (__u8) le16_to_cpu(bss->a.level) - 0x95;
5206 iwe.u.qual.noise = (__u8) le16_to_cpu(bss->a.noise) - 0x95;
5207 /* Wireless tools prior to 27.pre22 will show link quality
5208 * anyway, so we provide a reasonable value. */
5209 if (iwe.u.qual.level > iwe.u.qual.noise)
5210 iwe.u.qual.qual = iwe.u.qual.level - iwe.u.qual.noise;
5211 else
5212 iwe.u.qual.qual = 0;
David S. Millerccc58052008-06-16 18:50:49 -07005213 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5214 &iwe, IW_EV_QUAL_LEN);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005215
Dan Williams1e3428e2007-10-10 23:56:25 -04005216 /* Add encryption capability */
5217 iwe.cmd = SIOCGIWENCODE;
David Kilroy17a1a882008-08-21 23:27:47 +01005218 if (capabilities & WLAN_CAPABILITY_PRIVACY)
Dan Williams1e3428e2007-10-10 23:56:25 -04005219 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
5220 else
5221 iwe.u.data.flags = IW_ENCODE_DISABLED;
5222 iwe.u.data.length = 0;
David S. Millerccc58052008-06-16 18:50:49 -07005223 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
David Kilroy17a1a882008-08-21 23:27:47 +01005224 &iwe, NULL);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005225
Dan Williams1e3428e2007-10-10 23:56:25 -04005226 /* Bit rate is not available in Lucent/Agere firmwares */
5227 if (priv->firmware_type != FIRMWARE_TYPE_AGERE) {
David S. Millerccc58052008-06-16 18:50:49 -07005228 char *current_val = current_ev + iwe_stream_lcp_len(info);
Dan Williams1e3428e2007-10-10 23:56:25 -04005229 int i;
5230 int step;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005231
Dan Williams1e3428e2007-10-10 23:56:25 -04005232 if (priv->firmware_type == FIRMWARE_TYPE_SYMBOL)
5233 step = 2;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005234 else
Dan Williams1e3428e2007-10-10 23:56:25 -04005235 step = 1;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005236
Dan Williams1e3428e2007-10-10 23:56:25 -04005237 iwe.cmd = SIOCGIWRATE;
5238 /* Those two flags are ignored... */
5239 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
5240 /* Max 10 values */
5241 for (i = 0; i < 10; i += step) {
5242 /* NULL terminated */
5243 if (bss->p.rates[i] == 0x0)
5244 break;
5245 /* Bit rate given in 500 kb/s units (+ 0x80) */
David Kilroy17a1a882008-08-21 23:27:47 +01005246 iwe.u.bitrate.value =
5247 ((bss->p.rates[i] & 0x7f) * 500000);
David S. Millerccc58052008-06-16 18:50:49 -07005248 current_val = iwe_stream_add_value(info, current_ev,
5249 current_val,
Dan Williams1e3428e2007-10-10 23:56:25 -04005250 end_buf, &iwe,
5251 IW_EV_PARAM_LEN);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005252 }
Dan Williams1e3428e2007-10-10 23:56:25 -04005253 /* Check if we added any event */
David S. Millerccc58052008-06-16 18:50:49 -07005254 if ((current_val - current_ev) > iwe_stream_lcp_len(info))
Dan Williams1e3428e2007-10-10 23:56:25 -04005255 current_ev = current_val;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005256 }
Dan Williams1e3428e2007-10-10 23:56:25 -04005257
David Kilroy17a1a882008-08-21 23:27:47 +01005258 /* Beacon interval */
5259 iwe.cmd = IWEVCUSTOM;
5260 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5261 "bcn_int=%d",
5262 le16_to_cpu(bss->a.beacon_interv));
5263 if (iwe.u.data.length)
5264 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5265 &iwe, custom);
5266
5267 /* Capabilites */
5268 iwe.cmd = IWEVCUSTOM;
5269 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5270 "capab=0x%04x",
5271 capabilities);
5272 if (iwe.u.data.length)
5273 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5274 &iwe, custom);
5275
5276 /* Add EXTRA: Age to display seconds since last beacon/probe response
5277 * for given network. */
5278 iwe.cmd = IWEVCUSTOM;
5279 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5280 " Last beacon: %dms ago",
5281 jiffies_to_msecs(jiffies - last_scanned));
5282 if (iwe.u.data.length)
5283 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5284 &iwe, custom);
5285
Dan Williams1e3428e2007-10-10 23:56:25 -04005286 return current_ev;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005287}
5288
David Kilroy01632fa2008-08-21 23:27:58 +01005289static inline char *orinoco_translate_ext_scan(struct net_device *dev,
5290 struct iw_request_info *info,
5291 char *current_ev,
5292 char *end_buf,
5293 struct agere_ext_scan_info *bss,
5294 unsigned int last_scanned)
5295{
5296 u16 capabilities;
5297 u16 channel;
5298 struct iw_event iwe; /* Temporary buffer */
5299 char custom[MAX_CUSTOM_LEN];
5300 u8 *ie;
5301
5302 memset(&iwe, 0, sizeof(iwe));
5303
5304 /* First entry *MUST* be the AP MAC address */
5305 iwe.cmd = SIOCGIWAP;
5306 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
5307 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
5308 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5309 &iwe, IW_EV_ADDR_LEN);
5310
5311 /* Other entries will be displayed in the order we give them */
5312
5313 /* Add the ESSID */
5314 ie = bss->data;
5315 iwe.u.data.length = ie[1];
5316 if (iwe.u.data.length) {
5317 if (iwe.u.data.length > 32)
5318 iwe.u.data.length = 32;
5319 iwe.cmd = SIOCGIWESSID;
5320 iwe.u.data.flags = 1;
5321 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5322 &iwe, &ie[2]);
5323 }
5324
5325 /* Add mode */
5326 capabilities = le16_to_cpu(bss->capabilities);
5327 if (capabilities & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
5328 iwe.cmd = SIOCGIWMODE;
5329 if (capabilities & WLAN_CAPABILITY_ESS)
5330 iwe.u.mode = IW_MODE_MASTER;
5331 else
5332 iwe.u.mode = IW_MODE_ADHOC;
5333 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5334 &iwe, IW_EV_UINT_LEN);
5335 }
5336
5337 ie = orinoco_get_ie(bss->data, sizeof(bss->data), MFIE_TYPE_DS_SET);
5338 channel = ie ? ie[2] : 0;
5339 if ((channel >= 1) && (channel <= NUM_CHANNELS)) {
5340 /* Add channel and frequency */
5341 iwe.cmd = SIOCGIWFREQ;
5342 iwe.u.freq.m = channel;
5343 iwe.u.freq.e = 0;
5344 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5345 &iwe, IW_EV_FREQ_LEN);
5346
5347 iwe.u.freq.m = channel_frequency[channel-1] * 100000;
5348 iwe.u.freq.e = 1;
5349 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5350 &iwe, IW_EV_FREQ_LEN);
5351 }
5352
5353 /* Add quality statistics. level and noise in dB. No link quality */
5354 iwe.cmd = IWEVQUAL;
5355 iwe.u.qual.updated = IW_QUAL_DBM | IW_QUAL_QUAL_INVALID;
5356 iwe.u.qual.level = bss->level - 0x95;
5357 iwe.u.qual.noise = bss->noise - 0x95;
5358 /* Wireless tools prior to 27.pre22 will show link quality
5359 * anyway, so we provide a reasonable value. */
5360 if (iwe.u.qual.level > iwe.u.qual.noise)
5361 iwe.u.qual.qual = iwe.u.qual.level - iwe.u.qual.noise;
5362 else
5363 iwe.u.qual.qual = 0;
5364 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
5365 &iwe, IW_EV_QUAL_LEN);
5366
5367 /* Add encryption capability */
5368 iwe.cmd = SIOCGIWENCODE;
5369 if (capabilities & WLAN_CAPABILITY_PRIVACY)
5370 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
5371 else
5372 iwe.u.data.flags = IW_ENCODE_DISABLED;
5373 iwe.u.data.length = 0;
5374 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5375 &iwe, NULL);
5376
5377 /* WPA IE */
5378 ie = orinoco_get_wpa_ie(bss->data, sizeof(bss->data));
5379 if (ie) {
5380 iwe.cmd = IWEVGENIE;
5381 iwe.u.data.length = ie[1] + 2;
5382 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5383 &iwe, ie);
5384 }
5385
5386 /* RSN IE */
5387 ie = orinoco_get_ie(bss->data, sizeof(bss->data), MFIE_TYPE_RSN);
5388 if (ie) {
5389 iwe.cmd = IWEVGENIE;
5390 iwe.u.data.length = ie[1] + 2;
5391 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5392 &iwe, ie);
5393 }
5394
5395 ie = orinoco_get_ie(bss->data, sizeof(bss->data), MFIE_TYPE_RATES);
5396 if (ie) {
5397 char *p = current_ev + iwe_stream_lcp_len(info);
5398 int i;
5399
5400 iwe.cmd = SIOCGIWRATE;
5401 /* Those two flags are ignored... */
5402 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
5403
5404 for (i = 2; i < (ie[1] + 2); i++) {
5405 iwe.u.bitrate.value = ((ie[i] & 0x7F) * 500000);
5406 p = iwe_stream_add_value(info, current_ev, p, end_buf,
5407 &iwe, IW_EV_PARAM_LEN);
5408 }
5409 /* Check if we added any event */
5410 if (p > (current_ev + iwe_stream_lcp_len(info)))
5411 current_ev = p;
5412 }
5413
5414 /* Timestamp */
5415 iwe.cmd = IWEVCUSTOM;
5416 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5417 "tsf=%016llx",
5418 le64_to_cpu(bss->timestamp));
5419 if (iwe.u.data.length)
5420 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5421 &iwe, custom);
5422
5423 /* Beacon interval */
5424 iwe.cmd = IWEVCUSTOM;
5425 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5426 "bcn_int=%d",
5427 le16_to_cpu(bss->beacon_interval));
5428 if (iwe.u.data.length)
5429 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5430 &iwe, custom);
5431
5432 /* Capabilites */
5433 iwe.cmd = IWEVCUSTOM;
5434 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5435 "capab=0x%04x",
5436 capabilities);
5437 if (iwe.u.data.length)
5438 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5439 &iwe, custom);
5440
5441 /* Add EXTRA: Age to display seconds since last beacon/probe response
5442 * for given network. */
5443 iwe.cmd = IWEVCUSTOM;
5444 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
5445 " Last beacon: %dms ago",
5446 jiffies_to_msecs(jiffies - last_scanned));
5447 if (iwe.u.data.length)
5448 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
5449 &iwe, custom);
5450
5451 return current_ev;
5452}
5453
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005454/* Return results of a scan */
5455static int orinoco_ioctl_getscan(struct net_device *dev,
5456 struct iw_request_info *info,
5457 struct iw_point *srq,
5458 char *extra)
5459{
5460 struct orinoco_private *priv = netdev_priv(dev);
5461 int err = 0;
5462 unsigned long flags;
Dan Williams1e3428e2007-10-10 23:56:25 -04005463 char *current_ev = extra;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005464
5465 if (orinoco_lock(priv, &flags) != 0)
5466 return -EBUSY;
5467
Dan Williams1e3428e2007-10-10 23:56:25 -04005468 if (priv->scan_inprogress) {
5469 /* Important note : we don't want to block the caller
5470 * until results are ready for various reasons.
5471 * First, managing wait queues is complex and racy.
5472 * Second, we grab some rtnetlink lock before comming
5473 * here (in dev_ioctl()).
5474 * Third, we generate an Wireless Event, so the
5475 * caller can wait itself on that - Jean II */
5476 err = -EAGAIN;
5477 goto out;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005478 }
Dan Williams1e3428e2007-10-10 23:56:25 -04005479
David Kilroy01632fa2008-08-21 23:27:58 +01005480 if (priv->has_ext_scan) {
5481 struct xbss_element *bss;
Dan Williams1e3428e2007-10-10 23:56:25 -04005482
David Kilroy01632fa2008-08-21 23:27:58 +01005483 list_for_each_entry(bss, &priv->bss_list, list) {
5484 /* Translate this entry to WE format */
5485 current_ev =
5486 orinoco_translate_ext_scan(dev, info,
5487 current_ev,
5488 extra + srq->length,
5489 &bss->bss,
5490 bss->last_scanned);
5491
5492 /* Check if there is space for one more entry */
5493 if ((extra + srq->length - current_ev)
5494 <= IW_EV_ADDR_LEN) {
5495 /* Ask user space to try again with a
5496 * bigger buffer */
5497 err = -E2BIG;
5498 goto out;
5499 }
5500 }
5501
5502 } else {
5503 struct bss_element *bss;
5504
5505 list_for_each_entry(bss, &priv->bss_list, list) {
5506 /* Translate this entry to WE format */
5507 current_ev = orinoco_translate_scan(dev, info,
5508 current_ev,
5509 extra + srq->length,
5510 &bss->bss,
5511 bss->last_scanned);
5512
5513 /* Check if there is space for one more entry */
5514 if ((extra + srq->length - current_ev)
5515 <= IW_EV_ADDR_LEN) {
5516 /* Ask user space to try again with a
5517 * bigger buffer */
5518 err = -E2BIG;
5519 goto out;
5520 }
Dan Williams1e3428e2007-10-10 23:56:25 -04005521 }
5522 }
5523
5524 srq->length = (current_ev - extra);
5525 srq->flags = (__u16) priv->scan_mode;
5526
5527out:
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02005528 orinoco_unlock(priv, &flags);
5529 return err;
5530}
5531
Christoph Hellwig620554e2005-06-19 01:27:33 +02005532/* Commit handler, called after set operations */
5533static int orinoco_ioctl_commit(struct net_device *dev,
5534 struct iw_request_info *info,
5535 void *wrqu,
5536 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537{
5538 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005539 struct hermes *hw = &priv->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 unsigned long flags;
Christoph Hellwig620554e2005-06-19 01:27:33 +02005541 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542
Christoph Hellwig620554e2005-06-19 01:27:33 +02005543 if (!priv->open)
5544 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545
Christoph Hellwig620554e2005-06-19 01:27:33 +02005546 if (priv->broken_disableport) {
David Howellsc4028952006-11-22 14:57:56 +00005547 orinoco_reset(&priv->reset_work);
Christoph Hellwig620554e2005-06-19 01:27:33 +02005548 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550
Christoph Hellwig620554e2005-06-19 01:27:33 +02005551 if (orinoco_lock(priv, &flags) != 0)
5552 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553
Christoph Hellwig620554e2005-06-19 01:27:33 +02005554 err = hermes_disable_port(hw, 0);
5555 if (err) {
5556 printk(KERN_WARNING "%s: Unable to disable port "
5557 "while reconfiguring card\n", dev->name);
5558 priv->broken_disableport = 1;
5559 goto out;
5560 }
5561
5562 err = __orinoco_program_rids(dev);
5563 if (err) {
5564 printk(KERN_WARNING "%s: Unable to reconfigure card\n",
5565 dev->name);
5566 goto out;
5567 }
5568
5569 err = hermes_enable_port(hw, 0);
5570 if (err) {
5571 printk(KERN_WARNING "%s: Unable to enable port while reconfiguring card\n",
5572 dev->name);
5573 goto out;
5574 }
5575
5576 out:
5577 if (err) {
5578 printk(KERN_WARNING "%s: Resetting instead...\n", dev->name);
5579 schedule_work(&priv->reset_work);
5580 err = 0;
5581 }
5582
5583 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 return err;
5585}
5586
Christoph Hellwig620554e2005-06-19 01:27:33 +02005587static const struct iw_priv_args orinoco_privtab[] = {
5588 { SIOCIWFIRSTPRIV + 0x0, 0, 0, "force_reset" },
5589 { SIOCIWFIRSTPRIV + 0x1, 0, 0, "card_reset" },
5590 { SIOCIWFIRSTPRIV + 0x2, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5591 0, "set_port3" },
5592 { SIOCIWFIRSTPRIV + 0x3, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5593 "get_port3" },
5594 { SIOCIWFIRSTPRIV + 0x4, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5595 0, "set_preamble" },
5596 { SIOCIWFIRSTPRIV + 0x5, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5597 "get_preamble" },
5598 { SIOCIWFIRSTPRIV + 0x6, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5599 0, "set_ibssport" },
5600 { SIOCIWFIRSTPRIV + 0x7, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5601 "get_ibssport" },
5602 { SIOCIWFIRSTPRIV + 0x9, 0, IW_PRIV_TYPE_BYTE | MAX_RID_LEN,
5603 "get_rid" },
5604};
5605
5606
5607/*
5608 * Structures to export the Wireless Handlers
5609 */
5610
David Kilroy409644a2008-08-21 23:28:01 +01005611#define STD_IW_HANDLER(id, func) \
5612 [IW_IOCTL_IDX(id)] = (iw_handler) func
Christoph Hellwig620554e2005-06-19 01:27:33 +02005613static const iw_handler orinoco_handler[] = {
David Kilroy409644a2008-08-21 23:28:01 +01005614 STD_IW_HANDLER(SIOCSIWCOMMIT, orinoco_ioctl_commit),
5615 STD_IW_HANDLER(SIOCGIWNAME, orinoco_ioctl_getname),
5616 STD_IW_HANDLER(SIOCSIWFREQ, orinoco_ioctl_setfreq),
5617 STD_IW_HANDLER(SIOCGIWFREQ, orinoco_ioctl_getfreq),
5618 STD_IW_HANDLER(SIOCSIWMODE, orinoco_ioctl_setmode),
5619 STD_IW_HANDLER(SIOCGIWMODE, orinoco_ioctl_getmode),
5620 STD_IW_HANDLER(SIOCSIWSENS, orinoco_ioctl_setsens),
5621 STD_IW_HANDLER(SIOCGIWSENS, orinoco_ioctl_getsens),
5622 STD_IW_HANDLER(SIOCGIWRANGE, orinoco_ioctl_getiwrange),
5623 STD_IW_HANDLER(SIOCSIWSPY, iw_handler_set_spy),
5624 STD_IW_HANDLER(SIOCGIWSPY, iw_handler_get_spy),
5625 STD_IW_HANDLER(SIOCSIWTHRSPY, iw_handler_set_thrspy),
5626 STD_IW_HANDLER(SIOCGIWTHRSPY, iw_handler_get_thrspy),
5627 STD_IW_HANDLER(SIOCSIWAP, orinoco_ioctl_setwap),
5628 STD_IW_HANDLER(SIOCGIWAP, orinoco_ioctl_getwap),
5629 STD_IW_HANDLER(SIOCSIWSCAN, orinoco_ioctl_setscan),
5630 STD_IW_HANDLER(SIOCGIWSCAN, orinoco_ioctl_getscan),
5631 STD_IW_HANDLER(SIOCSIWESSID, orinoco_ioctl_setessid),
5632 STD_IW_HANDLER(SIOCGIWESSID, orinoco_ioctl_getessid),
5633 STD_IW_HANDLER(SIOCSIWNICKN, orinoco_ioctl_setnick),
5634 STD_IW_HANDLER(SIOCGIWNICKN, orinoco_ioctl_getnick),
5635 STD_IW_HANDLER(SIOCSIWRATE, orinoco_ioctl_setrate),
5636 STD_IW_HANDLER(SIOCGIWRATE, orinoco_ioctl_getrate),
5637 STD_IW_HANDLER(SIOCSIWRTS, orinoco_ioctl_setrts),
5638 STD_IW_HANDLER(SIOCGIWRTS, orinoco_ioctl_getrts),
5639 STD_IW_HANDLER(SIOCSIWFRAG, orinoco_ioctl_setfrag),
5640 STD_IW_HANDLER(SIOCGIWFRAG, orinoco_ioctl_getfrag),
5641 STD_IW_HANDLER(SIOCGIWRETRY, orinoco_ioctl_getretry),
5642 STD_IW_HANDLER(SIOCSIWENCODE, orinoco_ioctl_setiwencode),
5643 STD_IW_HANDLER(SIOCGIWENCODE, orinoco_ioctl_getiwencode),
5644 STD_IW_HANDLER(SIOCSIWPOWER, orinoco_ioctl_setpower),
5645 STD_IW_HANDLER(SIOCGIWPOWER, orinoco_ioctl_getpower),
David Kilroyd03032a2008-08-21 23:28:02 +01005646 STD_IW_HANDLER(SIOCSIWGENIE, orinoco_ioctl_set_genie),
5647 STD_IW_HANDLER(SIOCGIWGENIE, orinoco_ioctl_get_genie),
5648 STD_IW_HANDLER(SIOCSIWMLME, orinoco_ioctl_set_mlme),
5649 STD_IW_HANDLER(SIOCSIWAUTH, orinoco_ioctl_set_auth),
5650 STD_IW_HANDLER(SIOCGIWAUTH, orinoco_ioctl_get_auth),
5651 STD_IW_HANDLER(SIOCSIWENCODEEXT, orinoco_ioctl_set_encodeext),
5652 STD_IW_HANDLER(SIOCGIWENCODEEXT, orinoco_ioctl_get_encodeext),
Christoph Hellwig620554e2005-06-19 01:27:33 +02005653};
5654
5655
5656/*
5657 Added typecasting since we no longer use iwreq_data -- Moustafa
5658 */
5659static const iw_handler orinoco_private_handler[] = {
Peter Hagervall6b9b97c2005-07-27 01:14:46 -07005660 [0] = (iw_handler) orinoco_ioctl_reset,
5661 [1] = (iw_handler) orinoco_ioctl_reset,
5662 [2] = (iw_handler) orinoco_ioctl_setport3,
5663 [3] = (iw_handler) orinoco_ioctl_getport3,
5664 [4] = (iw_handler) orinoco_ioctl_setpreamble,
5665 [5] = (iw_handler) orinoco_ioctl_getpreamble,
5666 [6] = (iw_handler) orinoco_ioctl_setibssport,
5667 [7] = (iw_handler) orinoco_ioctl_getibssport,
5668 [9] = (iw_handler) orinoco_ioctl_getrid,
Christoph Hellwig620554e2005-06-19 01:27:33 +02005669};
5670
5671static const struct iw_handler_def orinoco_handler_def = {
5672 .num_standard = ARRAY_SIZE(orinoco_handler),
5673 .num_private = ARRAY_SIZE(orinoco_private_handler),
5674 .num_private_args = ARRAY_SIZE(orinoco_privtab),
5675 .standard = orinoco_handler,
5676 .private = orinoco_private_handler,
5677 .private_args = orinoco_privtab,
Pavel Roskin343c6862005-09-09 18:43:02 -04005678 .get_wireless_stats = orinoco_get_wireless_stats,
Christoph Hellwig620554e2005-06-19 01:27:33 +02005679};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02005681static void orinoco_get_drvinfo(struct net_device *dev,
5682 struct ethtool_drvinfo *info)
5683{
5684 struct orinoco_private *priv = netdev_priv(dev);
5685
5686 strncpy(info->driver, DRIVER_NAME, sizeof(info->driver) - 1);
5687 strncpy(info->version, DRIVER_VERSION, sizeof(info->version) - 1);
5688 strncpy(info->fw_version, priv->fw_name, sizeof(info->fw_version) - 1);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07005689 if (dev->dev.parent)
5690 strncpy(info->bus_info, dev->dev.parent->bus_id,
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02005691 sizeof(info->bus_info) - 1);
5692 else
5693 snprintf(info->bus_info, sizeof(info->bus_info) - 1,
5694 "PCMCIA %p", priv->hw.iobase);
5695}
5696
Jeff Garzik7282d492006-09-13 14:30:00 -04005697static const struct ethtool_ops orinoco_ethtool_ops = {
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02005698 .get_drvinfo = orinoco_get_drvinfo,
5699 .get_link = ethtool_op_get_link,
5700};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701
5702/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703/* Module initialization */
5704/********************************************************************/
5705
5706EXPORT_SYMBOL(alloc_orinocodev);
5707EXPORT_SYMBOL(free_orinocodev);
5708
5709EXPORT_SYMBOL(__orinoco_up);
5710EXPORT_SYMBOL(__orinoco_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711EXPORT_SYMBOL(orinoco_reinit_firmware);
5712
5713EXPORT_SYMBOL(orinoco_interrupt);
5714
5715/* Can't be declared "const" or the whole __initdata section will
5716 * become const */
5717static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
5718 " (David Gibson <hermes@gibson.dropbear.id.au>, "
5719 "Pavel Roskin <proski@gnu.org>, et al)";
5720
5721static int __init init_orinoco(void)
5722{
5723 printk(KERN_DEBUG "%s\n", version);
5724 return 0;
5725}
5726
5727static void __exit exit_orinoco(void)
5728{
5729}
5730
5731module_init(init_orinoco);
5732module_exit(exit_orinoco);