blob: 22718e8176fff7210e3b717ad4389bfa4622e71e [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <linux/etherdevice.h>
Christoph Hellwig1fab2e82005-06-19 01:27:40 +020084#include <linux/ethtool.h>
David Kilroy3994d502008-08-21 23:27:54 +010085#include <linux/firmware.h>
Pavel Roskin9c974fb2006-08-15 20:45:03 -040086#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#include <linux/wireless.h>
Christoph Hellwig620554e2005-06-19 01:27:33 +020088#include <net/iw_handler.h>
Christoph Hellwig5d558b72005-06-19 01:27:28 +020089#include <net/ieee80211.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#include "hermes_rid.h"
David Kilroy3994d502008-08-21 23:27:54 +010092#include "hermes_dld.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#include "orinoco.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95/********************************************************************/
96/* Module information */
97/********************************************************************/
98
99MODULE_AUTHOR("Pavel Roskin <proski@gnu.org> & David Gibson <hermes@gibson.dropbear.id.au>");
100MODULE_DESCRIPTION("Driver for Lucent Orinoco, Prism II based and similar wireless cards");
101MODULE_LICENSE("Dual MPL/GPL");
102
103/* Level of debugging. Used in the macros in orinoco.h */
104#ifdef ORINOCO_DEBUG
105int orinoco_debug = ORINOCO_DEBUG;
106module_param(orinoco_debug, int, 0644);
107MODULE_PARM_DESC(orinoco_debug, "Debug level");
108EXPORT_SYMBOL(orinoco_debug);
109#endif
110
111static int suppress_linkstatus; /* = 0 */
112module_param(suppress_linkstatus, bool, 0644);
113MODULE_PARM_DESC(suppress_linkstatus, "Don't log link status changes");
David Gibson7bb7c3a2005-05-12 20:02:10 -0400114static int ignore_disconnect; /* = 0 */
115module_param(ignore_disconnect, int, 0644);
116MODULE_PARM_DESC(ignore_disconnect, "Don't report lost link to the network layer");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200118static int force_monitor; /* = 0 */
119module_param(force_monitor, int, 0644);
120MODULE_PARM_DESC(force_monitor, "Allow monitor mode for all firmware versions");
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/********************************************************************/
123/* Compile time configuration and compatibility stuff */
124/********************************************************************/
125
126/* We do this this way to avoid ifdefs in the actual code */
127#ifdef WIRELESS_SPY
Pavel Roskin343c6862005-09-09 18:43:02 -0400128#define SPY_NUMBER(priv) (priv->spy_data.spy_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#else
130#define SPY_NUMBER(priv) 0
131#endif /* WIRELESS_SPY */
132
133/********************************************************************/
134/* Internal constants */
135/********************************************************************/
136
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200137/* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
138static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
139#define ENCAPS_OVERHEAD (sizeof(encaps_hdr) + 2)
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141#define ORINOCO_MIN_MTU 256
Jeff Garzikb4538722005-05-12 22:48:20 -0400142#define ORINOCO_MAX_MTU (IEEE80211_DATA_LEN - ENCAPS_OVERHEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144#define SYMBOL_MAX_VER_LEN (14)
145#define USER_BAP 0
146#define IRQ_BAP 1
147#define MAX_IRQLOOPS_PER_IRQ 10
148#define MAX_IRQLOOPS_PER_JIFFY (20000/HZ) /* Based on a guestimate of
149 * how many events the
150 * device could
151 * legitimately generate */
152#define SMALL_KEY_SIZE 5
153#define LARGE_KEY_SIZE 13
154#define TX_NICBUF_SIZE_BUG 1585 /* Bug in Symbol firmware */
155
156#define DUMMY_FID 0xFFFF
157
158/*#define MAX_MULTICAST(priv) (priv->firmware_type == FIRMWARE_TYPE_AGERE ? \
159 HERMES_MAX_MULTICAST : 0)*/
160#define MAX_MULTICAST(priv) (HERMES_MAX_MULTICAST)
161
162#define ORINOCO_INTEN (HERMES_EV_RX | HERMES_EV_ALLOC \
163 | HERMES_EV_TX | HERMES_EV_TXEXC \
164 | HERMES_EV_WTERR | HERMES_EV_INFO \
165 | HERMES_EV_INFDROP )
166
Christoph Hellwig620554e2005-06-19 01:27:33 +0200167#define MAX_RID_LEN 1024
168
169static const struct iw_handler_def orinoco_handler_def;
Jeff Garzik7282d492006-09-13 14:30:00 -0400170static const struct ethtool_ops orinoco_ethtool_ops;
Christoph Hellwig620554e2005-06-19 01:27:33 +0200171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/********************************************************************/
173/* Data tables */
174/********************************************************************/
175
176/* The frequency of each channel in MHz */
177static const long channel_frequency[] = {
178 2412, 2417, 2422, 2427, 2432, 2437, 2442,
179 2447, 2452, 2457, 2462, 2467, 2472, 2484
180};
181#define NUM_CHANNELS ARRAY_SIZE(channel_frequency)
182
183/* This tables gives the actual meanings of the bitrate IDs returned
184 * by the firmware. */
185static struct {
186 int bitrate; /* in 100s of kilobits */
187 int automatic;
188 u16 agere_txratectrl;
189 u16 intersil_txratectrl;
190} bitrate_table[] = {
191 {110, 1, 3, 15}, /* Entry 0 is the default */
192 {10, 0, 1, 1},
193 {10, 1, 1, 1},
194 {20, 0, 2, 2},
195 {20, 1, 6, 3},
196 {55, 0, 4, 4},
197 {55, 1, 7, 7},
198 {110, 0, 5, 8},
199};
200#define BITRATE_TABLE_SIZE ARRAY_SIZE(bitrate_table)
201
202/********************************************************************/
203/* Data types */
204/********************************************************************/
205
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400206/* Beginning of the Tx descriptor, used in TxExc handling */
207struct hermes_txexc_data {
208 struct hermes_tx_descriptor desc;
Pavel Roskind133ae42005-09-23 04:18:06 -0400209 __le16 frame_ctl;
210 __le16 duration_id;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200211 u8 addr1[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212} __attribute__ ((packed));
213
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200214/* Rx frame header except compatibility 802.3 header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215struct hermes_rx_descriptor {
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200216 /* Control */
Pavel Roskind133ae42005-09-23 04:18:06 -0400217 __le16 status;
218 __le32 time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 u8 silence;
220 u8 signal;
221 u8 rate;
222 u8 rxflow;
Pavel Roskind133ae42005-09-23 04:18:06 -0400223 __le32 reserved;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200224
225 /* 802.11 header */
Pavel Roskind133ae42005-09-23 04:18:06 -0400226 __le16 frame_ctl;
227 __le16 duration_id;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200228 u8 addr1[ETH_ALEN];
229 u8 addr2[ETH_ALEN];
230 u8 addr3[ETH_ALEN];
Pavel Roskind133ae42005-09-23 04:18:06 -0400231 __le16 seq_ctl;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200232 u8 addr4[ETH_ALEN];
233
234 /* Data length */
Pavel Roskind133ae42005-09-23 04:18:06 -0400235 __le16 data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236} __attribute__ ((packed));
237
238/********************************************************************/
239/* Function prototypes */
240/********************************************************************/
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242static int __orinoco_program_rids(struct net_device *dev);
243static void __orinoco_set_multicast_list(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245/********************************************************************/
246/* Internal helper functions */
247/********************************************************************/
248
249static inline void set_port_type(struct orinoco_private *priv)
250{
251 switch (priv->iw_mode) {
252 case IW_MODE_INFRA:
253 priv->port_type = 1;
254 priv->createibss = 0;
255 break;
256 case IW_MODE_ADHOC:
257 if (priv->prefer_port3) {
258 priv->port_type = 3;
259 priv->createibss = 0;
260 } else {
261 priv->port_type = priv->ibss_port;
262 priv->createibss = 1;
263 }
264 break;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200265 case IW_MODE_MONITOR:
266 priv->port_type = 3;
267 priv->createibss = 0;
268 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 default:
270 printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
271 priv->ndev->name);
272 }
273}
274
Dan Williams1e3428e2007-10-10 23:56:25 -0400275#define ORINOCO_MAX_BSS_COUNT 64
276static int orinoco_bss_data_allocate(struct orinoco_private *priv)
277{
David Kilroy01632fa2008-08-21 23:27:58 +0100278 if (priv->bss_xbss_data)
Dan Williams1e3428e2007-10-10 23:56:25 -0400279 return 0;
280
David Kilroy01632fa2008-08-21 23:27:58 +0100281 if (priv->has_ext_scan)
282 priv->bss_xbss_data = kzalloc(ORINOCO_MAX_BSS_COUNT *
283 sizeof(struct xbss_element),
284 GFP_KERNEL);
285 else
286 priv->bss_xbss_data = kzalloc(ORINOCO_MAX_BSS_COUNT *
287 sizeof(struct bss_element),
288 GFP_KERNEL);
289
290 if (!priv->bss_xbss_data) {
Dan Williams1e3428e2007-10-10 23:56:25 -0400291 printk(KERN_WARNING "Out of memory allocating beacons");
292 return -ENOMEM;
293 }
294 return 0;
295}
296
297static void orinoco_bss_data_free(struct orinoco_private *priv)
298{
David Kilroy01632fa2008-08-21 23:27:58 +0100299 kfree(priv->bss_xbss_data);
300 priv->bss_xbss_data = NULL;
Dan Williams1e3428e2007-10-10 23:56:25 -0400301}
302
David Kilroy01632fa2008-08-21 23:27:58 +0100303#define PRIV_BSS ((struct bss_element *)priv->bss_xbss_data)
304#define PRIV_XBSS ((struct xbss_element *)priv->bss_xbss_data)
Dan Williams1e3428e2007-10-10 23:56:25 -0400305static void orinoco_bss_data_init(struct orinoco_private *priv)
306{
307 int i;
308
309 INIT_LIST_HEAD(&priv->bss_free_list);
310 INIT_LIST_HEAD(&priv->bss_list);
David Kilroy01632fa2008-08-21 23:27:58 +0100311 if (priv->has_ext_scan)
312 for (i = 0; i < ORINOCO_MAX_BSS_COUNT; i++)
313 list_add_tail(&(PRIV_XBSS[i].list),
314 &priv->bss_free_list);
315 else
316 for (i = 0; i < ORINOCO_MAX_BSS_COUNT; i++)
317 list_add_tail(&(PRIV_BSS[i].list),
318 &priv->bss_free_list);
319
320}
321
322static inline u8 *orinoco_get_ie(u8 *data, size_t len,
323 enum ieee80211_mfie eid)
324{
325 u8 *p = data;
326 while ((p + 2) < (data + len)) {
327 if (p[0] == eid)
328 return p;
329 p += p[1] + 2;
330 }
331 return NULL;
332}
333
334#define WPA_OUI_TYPE "\x00\x50\xF2\x01"
335#define WPA_SELECTOR_LEN 4
336static inline u8 *orinoco_get_wpa_ie(u8 *data, size_t len)
337{
338 u8 *p = data;
339 while ((p + 2 + WPA_SELECTOR_LEN) < (data + len)) {
340 if ((p[0] == MFIE_TYPE_GENERIC) &&
341 (memcmp(&p[2], WPA_OUI_TYPE, WPA_SELECTOR_LEN) == 0))
342 return p;
343 p += p[1] + 2;
344 }
345 return NULL;
Dan Williams1e3428e2007-10-10 23:56:25 -0400346}
347
David Kilroy3994d502008-08-21 23:27:54 +0100348
349/********************************************************************/
350/* Download functionality */
351/********************************************************************/
352
353struct fw_info {
354 char *pri_fw;
355 char *sta_fw;
356 char *ap_fw;
357 u32 pda_addr;
358 u16 pda_size;
359};
360
361const static struct fw_info orinoco_fw[] = {
362 { "", "agere_sta_fw.bin", "agere_ap_fw.bin", 0x00390000, 1000 },
363 { "", "prism_sta_fw.bin", "prism_ap_fw.bin", 0, 1024 },
364 { "symbol_sp24t_prim_fw", "symbol_sp24t_sec_fw", "", 0x00003100, 0x100 }
365};
366
367/* Structure used to access fields in FW
368 * Make sure LE decoding macros are used
369 */
370struct orinoco_fw_header {
371 char hdr_vers[6]; /* ASCII string for header version */
372 __le16 headersize; /* Total length of header */
373 __le32 entry_point; /* NIC entry point */
374 __le32 blocks; /* Number of blocks to program */
375 __le32 block_offset; /* Offset of block data from eof header */
376 __le32 pdr_offset; /* Offset to PDR data from eof header */
377 __le32 pri_offset; /* Offset to primary plug data */
378 __le32 compat_offset; /* Offset to compatibility data*/
379 char signature[0]; /* FW signature length headersize-20 */
380} __attribute__ ((packed));
381
382/* Download either STA or AP firmware into the card. */
383static int
384orinoco_dl_firmware(struct orinoco_private *priv,
385 const struct fw_info *fw,
386 int ap)
387{
388 /* Plug Data Area (PDA) */
389 __le16 pda[512] = { 0 };
390
391 hermes_t *hw = &priv->hw;
392 const struct firmware *fw_entry;
393 const struct orinoco_fw_header *hdr;
394 const unsigned char *first_block;
395 const unsigned char *end;
396 const char *firmware;
397 struct net_device *dev = priv->ndev;
398 int err;
399
400 if (ap)
401 firmware = fw->ap_fw;
402 else
403 firmware = fw->sta_fw;
404
405 printk(KERN_DEBUG "%s: Attempting to download firmware %s\n",
406 dev->name, firmware);
407
408 /* Read current plug data */
409 err = hermes_read_pda(hw, pda, fw->pda_addr,
410 min_t(u16, fw->pda_size, sizeof(pda)), 0);
411 printk(KERN_DEBUG "%s: Read PDA returned %d\n", dev->name, err);
412 if (err)
413 return err;
414
415 err = request_firmware(&fw_entry, firmware, priv->dev);
416 if (err) {
417 printk(KERN_ERR "%s: Cannot find firmware %s\n",
418 dev->name, firmware);
419 return -ENOENT;
420 }
421
422 hdr = (const struct orinoco_fw_header *) fw_entry->data;
423
424 /* Enable aux port to allow programming */
425 err = hermesi_program_init(hw, le32_to_cpu(hdr->entry_point));
426 printk(KERN_DEBUG "%s: Program init returned %d\n", dev->name, err);
427 if (err != 0)
428 goto abort;
429
430 /* Program data */
431 first_block = (fw_entry->data +
432 le16_to_cpu(hdr->headersize) +
433 le32_to_cpu(hdr->block_offset));
434 end = fw_entry->data + fw_entry->size;
435
436 err = hermes_program(hw, first_block, end);
437 printk(KERN_DEBUG "%s: Program returned %d\n", dev->name, err);
438 if (err != 0)
439 goto abort;
440
441 /* Update production data */
442 first_block = (fw_entry->data +
443 le16_to_cpu(hdr->headersize) +
444 le32_to_cpu(hdr->pdr_offset));
445
446 err = hermes_apply_pda_with_defaults(hw, first_block, pda);
447 printk(KERN_DEBUG "%s: Apply PDA returned %d\n", dev->name, err);
448 if (err)
449 goto abort;
450
451 /* Tell card we've finished */
452 err = hermesi_program_end(hw);
453 printk(KERN_DEBUG "%s: Program end returned %d\n", dev->name, err);
454 if (err != 0)
455 goto abort;
456
457 /* Check if we're running */
458 printk(KERN_DEBUG "%s: hermes_present returned %d\n",
459 dev->name, hermes_present(hw));
460
461abort:
462 release_firmware(fw_entry);
463 return err;
464}
465
466/* End markers */
467#define TEXT_END 0x1A /* End of text header */
468
469/*
470 * Process a firmware image - stop the card, load the firmware, reset
471 * the card and make sure it responds. For the secondary firmware take
472 * care of the PDA - read it and then write it on top of the firmware.
473 */
474static int
475symbol_dl_image(struct orinoco_private *priv, const struct fw_info *fw,
476 const unsigned char *image, const unsigned char *end,
477 int secondary)
478{
479 hermes_t *hw = &priv->hw;
480 int ret;
481 const unsigned char *ptr;
482 const unsigned char *first_block;
483
484 /* Plug Data Area (PDA) */
485 __le16 pda[256];
486
487 /* Binary block begins after the 0x1A marker */
488 ptr = image;
489 while (*ptr++ != TEXT_END);
490 first_block = ptr;
491
492 /* Read the PDA from EEPROM */
493 if (secondary) {
494 ret = hermes_read_pda(hw, pda, fw->pda_addr, sizeof(pda), 1);
495 if (ret)
496 return ret;
497 }
498
499 /* Stop the firmware, so that it can be safely rewritten */
500 if (priv->stop_fw) {
501 ret = priv->stop_fw(priv, 1);
502 if (ret)
503 return ret;
504 }
505
506 /* Program the adapter with new firmware */
507 ret = hermes_program(hw, first_block, end);
508 if (ret)
509 return ret;
510
511 /* Write the PDA to the adapter */
512 if (secondary) {
513 size_t len = hermes_blocks_length(first_block);
514 ptr = first_block + len;
515 ret = hermes_apply_pda(hw, ptr, pda);
516 if (ret)
517 return ret;
518 }
519
520 /* Run the firmware */
521 if (priv->stop_fw) {
522 ret = priv->stop_fw(priv, 0);
523 if (ret)
524 return ret;
525 }
526
527 /* Reset hermes chip and make sure it responds */
528 ret = hermes_init(hw);
529
530 /* hermes_reset() should return 0 with the secondary firmware */
531 if (secondary && ret != 0)
532 return -ENODEV;
533
534 /* And this should work with any firmware */
535 if (!hermes_present(hw))
536 return -ENODEV;
537
538 return 0;
539}
540
541
542/*
543 * Download the firmware into the card, this also does a PCMCIA soft
544 * reset on the card, to make sure it's in a sane state.
545 */
546static int
547symbol_dl_firmware(struct orinoco_private *priv,
548 const struct fw_info *fw)
549{
550 struct net_device *dev = priv->ndev;
551 int ret;
552 const struct firmware *fw_entry;
553
554 if (request_firmware(&fw_entry, fw->pri_fw,
555 priv->dev) != 0) {
556 printk(KERN_ERR "%s: Cannot find firmware: %s\n",
557 dev->name, fw->pri_fw);
558 return -ENOENT;
559 }
560
561 /* Load primary firmware */
562 ret = symbol_dl_image(priv, fw, fw_entry->data,
563 fw_entry->data + fw_entry->size, 0);
564 release_firmware(fw_entry);
565 if (ret) {
566 printk(KERN_ERR "%s: Primary firmware download failed\n",
567 dev->name);
568 return ret;
569 }
570
571 if (request_firmware(&fw_entry, fw->sta_fw,
572 priv->dev) != 0) {
573 printk(KERN_ERR "%s: Cannot find firmware: %s\n",
574 dev->name, fw->sta_fw);
575 return -ENOENT;
576 }
577
578 /* Load secondary firmware */
579 ret = symbol_dl_image(priv, fw, fw_entry->data,
580 fw_entry->data + fw_entry->size, 1);
581 release_firmware(fw_entry);
582 if (ret) {
583 printk(KERN_ERR "%s: Secondary firmware download failed\n",
584 dev->name);
585 }
586
587 return ret;
588}
589
590static int orinoco_download(struct orinoco_private *priv)
591{
592 int err = 0;
593 /* Reload firmware */
594 switch (priv->firmware_type) {
595 case FIRMWARE_TYPE_AGERE:
596 /* case FIRMWARE_TYPE_INTERSIL: */
597 err = orinoco_dl_firmware(priv,
598 &orinoco_fw[priv->firmware_type], 0);
599 break;
600
601 case FIRMWARE_TYPE_SYMBOL:
602 err = symbol_dl_firmware(priv,
603 &orinoco_fw[priv->firmware_type]);
604 break;
605 case FIRMWARE_TYPE_INTERSIL:
606 break;
607 }
608 /* TODO: if we fail we probably need to reinitialise
609 * the driver */
610
611 return err;
612}
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614/********************************************************************/
615/* Device methods */
616/********************************************************************/
617
618static int orinoco_open(struct net_device *dev)
619{
620 struct orinoco_private *priv = netdev_priv(dev);
621 unsigned long flags;
622 int err;
623
624 if (orinoco_lock(priv, &flags) != 0)
625 return -EBUSY;
626
627 err = __orinoco_up(dev);
628
629 if (! err)
630 priv->open = 1;
631
632 orinoco_unlock(priv, &flags);
633
634 return err;
635}
636
Christoph Hellwigad8f4512005-05-14 17:30:17 +0200637static int orinoco_stop(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 struct orinoco_private *priv = netdev_priv(dev);
640 int err = 0;
641
642 /* We mustn't use orinoco_lock() here, because we need to be
643 able to close the interface even if hw_unavailable is set
644 (e.g. as we're released after a PC Card removal) */
645 spin_lock_irq(&priv->lock);
646
647 priv->open = 0;
648
649 err = __orinoco_down(dev);
650
651 spin_unlock_irq(&priv->lock);
652
653 return err;
654}
655
656static struct net_device_stats *orinoco_get_stats(struct net_device *dev)
657{
658 struct orinoco_private *priv = netdev_priv(dev);
659
660 return &priv->stats;
661}
662
663static struct iw_statistics *orinoco_get_wireless_stats(struct net_device *dev)
664{
665 struct orinoco_private *priv = netdev_priv(dev);
666 hermes_t *hw = &priv->hw;
667 struct iw_statistics *wstats = &priv->wstats;
David Gibsone67d9d92005-05-12 20:01:22 -0400668 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 unsigned long flags;
670
671 if (! netif_device_present(dev)) {
672 printk(KERN_WARNING "%s: get_wireless_stats() called while device not present\n",
673 dev->name);
674 return NULL; /* FIXME: Can we do better than this? */
675 }
676
David Gibsone67d9d92005-05-12 20:01:22 -0400677 /* If busy, return the old stats. Returning NULL may cause
678 * the interface to disappear from /proc/net/wireless */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (orinoco_lock(priv, &flags) != 0)
David Gibsone67d9d92005-05-12 20:01:22 -0400680 return wstats;
681
682 /* We can't really wait for the tallies inquiry command to
683 * complete, so we just use the previous results and trigger
684 * a new tallies inquiry command for next time - Jean II */
685 /* FIXME: Really we should wait for the inquiry to come back -
686 * as it is the stats we give don't make a whole lot of sense.
687 * Unfortunately, it's not clear how to do that within the
688 * wireless extensions framework: I think we're in user
689 * context, but a lock seems to be held by the time we get in
690 * here so we're not safe to sleep here. */
691 hermes_inquire(hw, HERMES_INQ_TALLIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 if (priv->iw_mode == IW_MODE_ADHOC) {
694 memset(&wstats->qual, 0, sizeof(wstats->qual));
695 /* If a spy address is defined, we report stats of the
696 * first spy address - Jean II */
697 if (SPY_NUMBER(priv)) {
Pavel Roskin343c6862005-09-09 18:43:02 -0400698 wstats->qual.qual = priv->spy_data.spy_stat[0].qual;
699 wstats->qual.level = priv->spy_data.spy_stat[0].level;
700 wstats->qual.noise = priv->spy_data.spy_stat[0].noise;
701 wstats->qual.updated = priv->spy_data.spy_stat[0].updated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
703 } else {
704 struct {
Pavel Roskina208c4e2006-04-07 04:10:26 -0400705 __le16 qual, signal, noise, unused;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 } __attribute__ ((packed)) cq;
707
708 err = HERMES_READ_RECORD(hw, USER_BAP,
709 HERMES_RID_COMMSQUALITY, &cq);
David Gibsone67d9d92005-05-12 20:01:22 -0400710
711 if (!err) {
712 wstats->qual.qual = (int)le16_to_cpu(cq.qual);
713 wstats->qual.level = (int)le16_to_cpu(cq.signal) - 0x95;
714 wstats->qual.noise = (int)le16_to_cpu(cq.noise) - 0x95;
715 wstats->qual.updated = 7;
716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return wstats;
721}
722
723static void orinoco_set_multicast_list(struct net_device *dev)
724{
725 struct orinoco_private *priv = netdev_priv(dev);
726 unsigned long flags;
727
728 if (orinoco_lock(priv, &flags) != 0) {
729 printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
730 "called when hw_unavailable\n", dev->name);
731 return;
732 }
733
734 __orinoco_set_multicast_list(dev);
735 orinoco_unlock(priv, &flags);
736}
737
738static int orinoco_change_mtu(struct net_device *dev, int new_mtu)
739{
740 struct orinoco_private *priv = netdev_priv(dev);
741
742 if ( (new_mtu < ORINOCO_MIN_MTU) || (new_mtu > ORINOCO_MAX_MTU) )
743 return -EINVAL;
744
Jeff Garzikb4538722005-05-12 22:48:20 -0400745 if ( (new_mtu + ENCAPS_OVERHEAD + IEEE80211_HLEN) >
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 (priv->nicbuf_size - ETH_HLEN) )
747 return -EINVAL;
748
749 dev->mtu = new_mtu;
750
751 return 0;
752}
753
754/********************************************************************/
755/* Tx path */
756/********************************************************************/
757
758static int orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
759{
760 struct orinoco_private *priv = netdev_priv(dev);
761 struct net_device_stats *stats = &priv->stats;
762 hermes_t *hw = &priv->hw;
763 int err = 0;
764 u16 txfid = priv->txfid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 struct ethhdr *eh;
Pavel Roskina28dc812006-04-07 04:10:45 -0400766 int data_off;
David Kilroy6eecad72008-08-21 23:27:56 +0100767 int tx_control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 unsigned long flags;
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (! netif_running(dev)) {
771 printk(KERN_ERR "%s: Tx on stopped device!\n",
772 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400773 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775
776 if (netif_queue_stopped(dev)) {
777 printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
778 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400779 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
781
782 if (orinoco_lock(priv, &flags) != 0) {
783 printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n",
784 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400785 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200788 if (! netif_carrier_ok(dev) || (priv->iw_mode == IW_MODE_MONITOR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 /* Oops, the firmware hasn't established a connection,
790 silently drop the packet (this seems to be the
791 safest approach). */
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400792 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
794
Pavel Roskin8d5be082006-04-07 04:10:41 -0400795 /* Check packet length */
Pavel Roskina28dc812006-04-07 04:10:45 -0400796 if (skb->len < ETH_HLEN)
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400797 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 eh = (struct ethhdr *)skb->data;
800
David Kilroy6eecad72008-08-21 23:27:56 +0100801 tx_control = HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
David Kilroy6eecad72008-08-21 23:27:56 +0100803 if (priv->has_alt_txcntl) {
804 /* WPA enabled firmwares have tx_cntl at the end of
805 * the 802.11 header. So write zeroed descriptor and
806 * 802.11 header at the same time
807 */
808 char desc[HERMES_802_3_OFFSET];
809 __le16 *txcntl = (__le16 *) &desc[HERMES_TXCNTL2_OFFSET];
810
811 memset(&desc, 0, sizeof(desc));
812
813 *txcntl = cpu_to_le16(tx_control);
814 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
815 txfid, 0);
816 if (err) {
817 if (net_ratelimit())
818 printk(KERN_ERR "%s: Error %d writing Tx "
819 "descriptor to BAP\n", dev->name, err);
820 goto busy;
821 }
822 } else {
823 struct hermes_tx_descriptor desc;
824
825 memset(&desc, 0, sizeof(desc));
826
827 desc.tx_control = cpu_to_le16(tx_control);
828 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
829 txfid, 0);
830 if (err) {
831 if (net_ratelimit())
832 printk(KERN_ERR "%s: Error %d writing Tx "
833 "descriptor to BAP\n", dev->name, err);
834 goto busy;
835 }
836
837 /* Clear the 802.11 header and data length fields - some
838 * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
839 * if this isn't done. */
840 hermes_clear_words(hw, HERMES_DATA0,
841 HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 /* Encapsulate Ethernet-II frames */
845 if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
Pavel Roskina28dc812006-04-07 04:10:45 -0400846 struct header_struct {
847 struct ethhdr eth; /* 802.3 header */
848 u8 encap[6]; /* 802.2 header */
849 } __attribute__ ((packed)) hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Pavel Roskina28dc812006-04-07 04:10:45 -0400851 /* Strip destination and source from the data */
852 skb_pull(skb, 2 * ETH_ALEN);
853 data_off = HERMES_802_2_OFFSET + sizeof(encaps_hdr);
854
855 /* And move them to a separate header */
856 memcpy(&hdr.eth, eh, 2 * ETH_ALEN);
857 hdr.eth.h_proto = htons(sizeof(encaps_hdr) + skb->len);
858 memcpy(hdr.encap, encaps_hdr, sizeof(encaps_hdr));
859
860 err = hermes_bap_pwrite(hw, USER_BAP, &hdr, sizeof(hdr),
861 txfid, HERMES_802_3_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (err) {
863 if (net_ratelimit())
864 printk(KERN_ERR "%s: Error %d writing packet "
865 "header to BAP\n", dev->name, err);
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400866 goto busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868 } else { /* IEEE 802.3 frame */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 data_off = HERMES_802_3_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
871
Pavel Roskina28dc812006-04-07 04:10:45 -0400872 err = hermes_bap_pwrite(hw, USER_BAP, skb->data, skb->len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 txfid, data_off);
874 if (err) {
875 printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
876 dev->name, err);
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400877 goto busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879
880 /* Finally, we actually initiate the send */
881 netif_stop_queue(dev);
882
883 err = hermes_docmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
884 txfid, NULL);
885 if (err) {
886 netif_start_queue(dev);
Andrew Mortonc367c212005-10-19 21:23:44 -0700887 if (net_ratelimit())
888 printk(KERN_ERR "%s: Error %d transmitting packet\n",
889 dev->name, err);
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400890 goto busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
892
893 dev->trans_start = jiffies;
Pavel Roskina28dc812006-04-07 04:10:45 -0400894 stats->tx_bytes += data_off + skb->len;
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400895 goto ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400897 drop:
898 stats->tx_errors++;
899 stats->tx_dropped++;
900
901 ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 dev_kfree_skb(skb);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400904 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Pavel Roskin470e2aa2006-04-07 04:10:43 -0400906 busy:
Jiri Benc2c1bd262006-04-07 04:10:47 -0400907 if (err == -EIO)
908 schedule_work(&priv->reset_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 orinoco_unlock(priv, &flags);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400910 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
913static void __orinoco_ev_alloc(struct net_device *dev, hermes_t *hw)
914{
915 struct orinoco_private *priv = netdev_priv(dev);
916 u16 fid = hermes_read_regn(hw, ALLOCFID);
917
918 if (fid != priv->txfid) {
919 if (fid != DUMMY_FID)
920 printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
921 dev->name, fid);
922 return;
923 }
924
925 hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
926}
927
928static void __orinoco_ev_tx(struct net_device *dev, hermes_t *hw)
929{
930 struct orinoco_private *priv = netdev_priv(dev);
931 struct net_device_stats *stats = &priv->stats;
932
933 stats->tx_packets++;
934
935 netif_wake_queue(dev);
936
937 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
938}
939
940static void __orinoco_ev_txexc(struct net_device *dev, hermes_t *hw)
941{
942 struct orinoco_private *priv = netdev_priv(dev);
943 struct net_device_stats *stats = &priv->stats;
944 u16 fid = hermes_read_regn(hw, TXCOMPLFID);
Pavel Roskind133ae42005-09-23 04:18:06 -0400945 u16 status;
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400946 struct hermes_txexc_data hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 int err = 0;
948
949 if (fid == DUMMY_FID)
950 return; /* Nothing's really happened */
951
Pavel Roskin48ca7032005-09-23 04:18:06 -0400952 /* Read part of the frame header - we need status and addr1 */
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200953 err = hermes_bap_pread(hw, IRQ_BAP, &hdr,
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400954 sizeof(struct hermes_txexc_data),
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200955 fid, 0);
956
957 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
958 stats->tx_errors++;
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (err) {
961 printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
962 "(FID=%04X error %d)\n",
963 dev->name, fid, err);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200964 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200967 DEBUG(1, "%s: Tx error, err %d (FID=%04X)\n", dev->name,
968 err, fid);
969
970 /* We produce a TXDROP event only for retry or lifetime
971 * exceeded, because that's the only status that really mean
972 * that this particular node went away.
973 * Other errors means that *we* screwed up. - Jean II */
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400974 status = le16_to_cpu(hdr.desc.status);
Pavel Roskind133ae42005-09-23 04:18:06 -0400975 if (status & (HERMES_TXSTAT_RETRYERR | HERMES_TXSTAT_AGEDERR)) {
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200976 union iwreq_data wrqu;
977
978 /* Copy 802.11 dest address.
979 * We use the 802.11 header because the frame may
980 * not be 802.3 or may be mangled...
981 * In Ad-Hoc mode, it will be the node address.
982 * In managed mode, it will be most likely the AP addr
983 * User space will figure out how to convert it to
984 * whatever it needs (IP address or else).
985 * - Jean II */
986 memcpy(wrqu.addr.sa_data, hdr.addr1, ETH_ALEN);
987 wrqu.addr.sa_family = ARPHRD_ETHER;
988
989 /* Send event to user space */
990 wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
996static void orinoco_tx_timeout(struct net_device *dev)
997{
998 struct orinoco_private *priv = netdev_priv(dev);
999 struct net_device_stats *stats = &priv->stats;
1000 struct hermes *hw = &priv->hw;
1001
1002 printk(KERN_WARNING "%s: Tx timeout! "
1003 "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
1004 dev->name, hermes_read_regn(hw, ALLOCFID),
1005 hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
1006
1007 stats->tx_errors++;
1008
1009 schedule_work(&priv->reset_work);
1010}
1011
1012/********************************************************************/
1013/* Rx path (data frames) */
1014/********************************************************************/
1015
1016/* Does the frame have a SNAP header indicating it should be
1017 * de-encapsulated to Ethernet-II? */
1018static inline int is_ethersnap(void *_hdr)
1019{
1020 u8 *hdr = _hdr;
1021
1022 /* We de-encapsulate all packets which, a) have SNAP headers
1023 * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
1024 * and where b) the OUI of the SNAP header is 00:00:00 or
1025 * 00:00:f8 - we need both because different APs appear to use
1026 * different OUIs for some reason */
1027 return (memcmp(hdr, &encaps_hdr, 5) == 0)
1028 && ( (hdr[5] == 0x00) || (hdr[5] == 0xf8) );
1029}
1030
1031static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
1032 int level, int noise)
1033{
Pavel Roskin343c6862005-09-09 18:43:02 -04001034 struct iw_quality wstats;
1035 wstats.level = level - 0x95;
1036 wstats.noise = noise - 0x95;
1037 wstats.qual = (level > noise) ? (level - noise) : 0;
1038 wstats.updated = 7;
1039 /* Update spy records */
1040 wireless_spy_update(dev, mac, &wstats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
1043static void orinoco_stat_gather(struct net_device *dev,
1044 struct sk_buff *skb,
1045 struct hermes_rx_descriptor *desc)
1046{
1047 struct orinoco_private *priv = netdev_priv(dev);
1048
1049 /* Using spy support with lots of Rx packets, like in an
1050 * infrastructure (AP), will really slow down everything, because
1051 * the MAC address must be compared to each entry of the spy list.
1052 * If the user really asks for it (set some address in the
1053 * spy list), we do it, but he will pay the price.
1054 * Note that to get here, you need both WIRELESS_SPY
1055 * compiled in AND some addresses in the list !!!
1056 */
1057 /* Note : gcc will optimise the whole section away if
1058 * WIRELESS_SPY is not defined... - Jean II */
1059 if (SPY_NUMBER(priv)) {
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001060 orinoco_spy_gather(dev, skb_mac_header(skb) + ETH_ALEN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 desc->signal, desc->silence);
1062 }
1063}
1064
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001065/*
1066 * orinoco_rx_monitor - handle received monitor frames.
1067 *
1068 * Arguments:
1069 * dev network device
1070 * rxfid received FID
1071 * desc rx descriptor of the frame
1072 *
1073 * Call context: interrupt
1074 */
1075static void orinoco_rx_monitor(struct net_device *dev, u16 rxfid,
1076 struct hermes_rx_descriptor *desc)
1077{
1078 u32 hdrlen = 30; /* return full header by default */
1079 u32 datalen = 0;
1080 u16 fc;
1081 int err;
1082 int len;
1083 struct sk_buff *skb;
1084 struct orinoco_private *priv = netdev_priv(dev);
1085 struct net_device_stats *stats = &priv->stats;
1086 hermes_t *hw = &priv->hw;
1087
1088 len = le16_to_cpu(desc->data_len);
1089
1090 /* Determine the size of the header and the data */
1091 fc = le16_to_cpu(desc->frame_ctl);
1092 switch (fc & IEEE80211_FCTL_FTYPE) {
1093 case IEEE80211_FTYPE_DATA:
1094 if ((fc & IEEE80211_FCTL_TODS)
1095 && (fc & IEEE80211_FCTL_FROMDS))
1096 hdrlen = 30;
1097 else
1098 hdrlen = 24;
1099 datalen = len;
1100 break;
1101 case IEEE80211_FTYPE_MGMT:
1102 hdrlen = 24;
1103 datalen = len;
1104 break;
1105 case IEEE80211_FTYPE_CTL:
1106 switch (fc & IEEE80211_FCTL_STYPE) {
1107 case IEEE80211_STYPE_PSPOLL:
1108 case IEEE80211_STYPE_RTS:
1109 case IEEE80211_STYPE_CFEND:
1110 case IEEE80211_STYPE_CFENDACK:
1111 hdrlen = 16;
1112 break;
1113 case IEEE80211_STYPE_CTS:
1114 case IEEE80211_STYPE_ACK:
1115 hdrlen = 10;
1116 break;
1117 }
1118 break;
1119 default:
1120 /* Unknown frame type */
1121 break;
1122 }
1123
1124 /* sanity check the length */
1125 if (datalen > IEEE80211_DATA_LEN + 12) {
1126 printk(KERN_DEBUG "%s: oversized monitor frame, "
1127 "data length = %d\n", dev->name, datalen);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001128 stats->rx_length_errors++;
1129 goto update_stats;
1130 }
1131
1132 skb = dev_alloc_skb(hdrlen + datalen);
1133 if (!skb) {
1134 printk(KERN_WARNING "%s: Cannot allocate skb for monitor frame\n",
1135 dev->name);
Florin Malitabb6e0932006-05-22 22:35:30 -07001136 goto update_stats;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001137 }
1138
1139 /* Copy the 802.11 header to the skb */
1140 memcpy(skb_put(skb, hdrlen), &(desc->frame_ctl), hdrlen);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001141 skb_reset_mac_header(skb);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001142
1143 /* If any, copy the data from the card to the skb */
1144 if (datalen > 0) {
1145 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, datalen),
1146 ALIGN(datalen, 2), rxfid,
1147 HERMES_802_2_OFFSET);
1148 if (err) {
1149 printk(KERN_ERR "%s: error %d reading monitor frame\n",
1150 dev->name, err);
1151 goto drop;
1152 }
1153 }
1154
1155 skb->dev = dev;
1156 skb->ip_summed = CHECKSUM_NONE;
1157 skb->pkt_type = PACKET_OTHERHOST;
1158 skb->protocol = __constant_htons(ETH_P_802_2);
1159
1160 dev->last_rx = jiffies;
1161 stats->rx_packets++;
1162 stats->rx_bytes += skb->len;
1163
1164 netif_rx(skb);
1165 return;
1166
1167 drop:
1168 dev_kfree_skb_irq(skb);
1169 update_stats:
1170 stats->rx_errors++;
1171 stats->rx_dropped++;
1172}
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174static void __orinoco_ev_rx(struct net_device *dev, hermes_t *hw)
1175{
1176 struct orinoco_private *priv = netdev_priv(dev);
1177 struct net_device_stats *stats = &priv->stats;
1178 struct iw_statistics *wstats = &priv->wstats;
1179 struct sk_buff *skb = NULL;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001180 u16 rxfid, status, fc;
1181 int length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 struct hermes_rx_descriptor desc;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001183 struct ethhdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 int err;
1185
1186 rxfid = hermes_read_regn(hw, RXFID);
1187
1188 err = hermes_bap_pread(hw, IRQ_BAP, &desc, sizeof(desc),
1189 rxfid, 0);
1190 if (err) {
1191 printk(KERN_ERR "%s: error %d reading Rx descriptor. "
1192 "Frame dropped.\n", dev->name, err);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001193 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
1195
1196 status = le16_to_cpu(desc.status);
1197
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001198 if (status & HERMES_RXSTAT_BADCRC) {
1199 DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n",
1200 dev->name);
1201 stats->rx_crc_errors++;
1202 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
1204
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001205 /* Handle frames in monitor mode */
1206 if (priv->iw_mode == IW_MODE_MONITOR) {
1207 orinoco_rx_monitor(dev, rxfid, &desc);
1208 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
1210
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001211 if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
1212 DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
1213 dev->name);
1214 wstats->discard.code++;
1215 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001218 length = le16_to_cpu(desc.data_len);
1219 fc = le16_to_cpu(desc.frame_ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 /* Sanity checks */
1222 if (length < 3) { /* No for even an 802.2 LLC header */
1223 /* At least on Symbol firmware with PCF we get quite a
1224 lot of these legitimately - Poll frames with no
1225 data. */
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001226 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
Jeff Garzikb4538722005-05-12 22:48:20 -04001228 if (length > IEEE80211_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
1230 dev->name, length);
1231 stats->rx_length_errors++;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001232 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234
1235 /* We need space for the packet data itself, plus an ethernet
1236 header, plus 2 bytes so we can align the IP header on a
1237 32bit boundary, plus 1 byte so we can read in odd length
1238 packets from the card, which has an IO granularity of 16
1239 bits */
1240 skb = dev_alloc_skb(length+ETH_HLEN+2+1);
1241 if (!skb) {
1242 printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
1243 dev->name);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001244 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
1246
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001247 /* We'll prepend the header, so reserve space for it. The worst
1248 case is no decapsulation, when 802.3 header is prepended and
1249 nothing is removed. 2 is for aligning the IP header. */
1250 skb_reserve(skb, ETH_HLEN + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001252 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, length),
1253 ALIGN(length, 2), rxfid,
1254 HERMES_802_2_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (err) {
1256 printk(KERN_ERR "%s: error %d reading frame. "
1257 "Frame dropped.\n", dev->name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 goto drop;
1259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 /* Handle decapsulation
1262 * In most cases, the firmware tell us about SNAP frames.
1263 * For some reason, the SNAP frames sent by LinkSys APs
1264 * are not properly recognised by most firmwares.
1265 * So, check ourselves */
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001266 if (length >= ENCAPS_OVERHEAD &&
1267 (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
1268 ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
1269 is_ethersnap(skb->data))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 /* These indicate a SNAP within 802.2 LLC within
1271 802.11 frame which we'll need to de-encapsulate to
1272 the original EthernetII frame. */
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001273 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN - ENCAPS_OVERHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 } else {
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001275 /* 802.3 frame - prepend 802.3 header as is */
1276 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN);
1277 hdr->h_proto = htons(length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001279 memcpy(hdr->h_dest, desc.addr1, ETH_ALEN);
1280 if (fc & IEEE80211_FCTL_FROMDS)
1281 memcpy(hdr->h_source, desc.addr3, ETH_ALEN);
1282 else
1283 memcpy(hdr->h_source, desc.addr2, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 dev->last_rx = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 skb->protocol = eth_type_trans(skb, dev);
1287 skb->ip_summed = CHECKSUM_NONE;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001288 if (fc & IEEE80211_FCTL_TODS)
1289 skb->pkt_type = PACKET_OTHERHOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 /* Process the wireless stats if needed */
1292 orinoco_stat_gather(dev, skb, &desc);
1293
1294 /* Pass the packet to the networking stack */
1295 netif_rx(skb);
1296 stats->rx_packets++;
1297 stats->rx_bytes += length;
1298
1299 return;
1300
1301 drop:
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001302 dev_kfree_skb_irq(skb);
1303 update_stats:
1304 stats->rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 stats->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
1308/********************************************************************/
1309/* Rx path (info frames) */
1310/********************************************************************/
1311
1312static void print_linkstatus(struct net_device *dev, u16 status)
1313{
1314 char * s;
1315
1316 if (suppress_linkstatus)
1317 return;
1318
1319 switch (status) {
1320 case HERMES_LINKSTATUS_NOT_CONNECTED:
1321 s = "Not Connected";
1322 break;
1323 case HERMES_LINKSTATUS_CONNECTED:
1324 s = "Connected";
1325 break;
1326 case HERMES_LINKSTATUS_DISCONNECTED:
1327 s = "Disconnected";
1328 break;
1329 case HERMES_LINKSTATUS_AP_CHANGE:
1330 s = "AP Changed";
1331 break;
1332 case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
1333 s = "AP Out of Range";
1334 break;
1335 case HERMES_LINKSTATUS_AP_IN_RANGE:
1336 s = "AP In Range";
1337 break;
1338 case HERMES_LINKSTATUS_ASSOC_FAILED:
1339 s = "Association Failed";
1340 break;
1341 default:
1342 s = "UNKNOWN";
1343 }
1344
1345 printk(KERN_INFO "%s: New link status: %s (%04x)\n",
1346 dev->name, s, status);
1347}
1348
Christoph Hellwig16739b02005-06-19 01:27:51 +02001349/* Search scan results for requested BSSID, join it if found */
David Howellsc4028952006-11-22 14:57:56 +00001350static void orinoco_join_ap(struct work_struct *work)
Christoph Hellwig16739b02005-06-19 01:27:51 +02001351{
David Howellsc4028952006-11-22 14:57:56 +00001352 struct orinoco_private *priv =
1353 container_of(work, struct orinoco_private, join_work);
1354 struct net_device *dev = priv->ndev;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001355 struct hermes *hw = &priv->hw;
1356 int err;
1357 unsigned long flags;
1358 struct join_req {
1359 u8 bssid[ETH_ALEN];
Pavel Roskind133ae42005-09-23 04:18:06 -04001360 __le16 channel;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001361 } __attribute__ ((packed)) req;
1362 const int atom_len = offsetof(struct prism2_scan_apinfo, atim);
Pavel Roskinc89cc222005-09-01 20:06:06 -04001363 struct prism2_scan_apinfo *atom = NULL;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001364 int offset = 4;
Pavel Roskinc89cc222005-09-01 20:06:06 -04001365 int found = 0;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001366 u8 *buf;
1367 u16 len;
1368
1369 /* Allocate buffer for scan results */
1370 buf = kmalloc(MAX_SCAN_LEN, GFP_KERNEL);
1371 if (! buf)
1372 return;
1373
1374 if (orinoco_lock(priv, &flags) != 0)
Pavel Roskinf3cb4cc2005-09-23 04:18:06 -04001375 goto fail_lock;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001376
1377 /* Sanity checks in case user changed something in the meantime */
1378 if (! priv->bssid_fixed)
1379 goto out;
1380
1381 if (strlen(priv->desired_essid) == 0)
1382 goto out;
1383
1384 /* Read scan results from the firmware */
1385 err = hermes_read_ltv(hw, USER_BAP,
1386 HERMES_RID_SCANRESULTSTABLE,
1387 MAX_SCAN_LEN, &len, buf);
1388 if (err) {
1389 printk(KERN_ERR "%s: Cannot read scan results\n",
1390 dev->name);
1391 goto out;
1392 }
1393
1394 len = HERMES_RECLEN_TO_BYTES(len);
1395
1396 /* Go through the scan results looking for the channel of the AP
1397 * we were requested to join */
1398 for (; offset + atom_len <= len; offset += atom_len) {
1399 atom = (struct prism2_scan_apinfo *) (buf + offset);
Pavel Roskinc89cc222005-09-01 20:06:06 -04001400 if (memcmp(&atom->bssid, priv->desired_bssid, ETH_ALEN) == 0) {
1401 found = 1;
1402 break;
1403 }
Christoph Hellwig16739b02005-06-19 01:27:51 +02001404 }
1405
Pavel Roskinc89cc222005-09-01 20:06:06 -04001406 if (! found) {
1407 DEBUG(1, "%s: Requested AP not found in scan results\n",
1408 dev->name);
1409 goto out;
1410 }
Christoph Hellwig16739b02005-06-19 01:27:51 +02001411
Christoph Hellwig16739b02005-06-19 01:27:51 +02001412 memcpy(req.bssid, priv->desired_bssid, ETH_ALEN);
1413 req.channel = atom->channel; /* both are little-endian */
1414 err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNFJOINREQUEST,
1415 &req);
1416 if (err)
1417 printk(KERN_ERR "%s: Error issuing join request\n", dev->name);
1418
1419 out:
Christoph Hellwig16739b02005-06-19 01:27:51 +02001420 orinoco_unlock(priv, &flags);
Pavel Roskinf3cb4cc2005-09-23 04:18:06 -04001421
1422 fail_lock:
1423 kfree(buf);
Christoph Hellwig16739b02005-06-19 01:27:51 +02001424}
1425
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001426/* Send new BSSID to userspace */
David Howellsc4028952006-11-22 14:57:56 +00001427static void orinoco_send_wevents(struct work_struct *work)
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001428{
David Howellsc4028952006-11-22 14:57:56 +00001429 struct orinoco_private *priv =
1430 container_of(work, struct orinoco_private, wevent_work);
1431 struct net_device *dev = priv->ndev;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001432 struct hermes *hw = &priv->hw;
1433 union iwreq_data wrqu;
1434 int err;
1435 unsigned long flags;
1436
1437 if (orinoco_lock(priv, &flags) != 0)
1438 return;
1439
1440 err = hermes_read_ltv(hw, IRQ_BAP, HERMES_RID_CURRENTBSSID,
1441 ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
1442 if (err != 0)
Pavel Roskin8aeabc32005-09-23 04:18:06 -04001443 goto out;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001444
1445 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1446
1447 /* Send event to user space */
1448 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Pavel Roskin8aeabc32005-09-23 04:18:06 -04001449
1450 out:
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001451 orinoco_unlock(priv, &flags);
1452}
1453
Dan Williams1e3428e2007-10-10 23:56:25 -04001454
1455static inline void orinoco_clear_scan_results(struct orinoco_private *priv,
1456 unsigned long scan_age)
1457{
David Kilroy01632fa2008-08-21 23:27:58 +01001458 if (priv->has_ext_scan) {
1459 struct xbss_element *bss;
1460 struct xbss_element *tmp_bss;
Dan Williams1e3428e2007-10-10 23:56:25 -04001461
David Kilroy01632fa2008-08-21 23:27:58 +01001462 /* Blow away current list of scan results */
1463 list_for_each_entry_safe(bss, tmp_bss, &priv->bss_list, list) {
1464 if (!scan_age ||
1465 time_after(jiffies, bss->last_scanned + scan_age)) {
1466 list_move_tail(&bss->list,
1467 &priv->bss_free_list);
1468 /* Don't blow away ->list, just BSS data */
1469 memset(&bss->bss, 0, sizeof(bss->bss));
1470 bss->last_scanned = 0;
1471 }
Dan Williams1e3428e2007-10-10 23:56:25 -04001472 }
David Kilroy01632fa2008-08-21 23:27:58 +01001473 } else {
1474 struct bss_element *bss;
1475 struct bss_element *tmp_bss;
1476
1477 /* Blow away current list of scan results */
1478 list_for_each_entry_safe(bss, tmp_bss, &priv->bss_list, list) {
1479 if (!scan_age ||
1480 time_after(jiffies, bss->last_scanned + scan_age)) {
1481 list_move_tail(&bss->list,
1482 &priv->bss_free_list);
1483 /* Don't blow away ->list, just BSS data */
1484 memset(&bss->bss, 0, sizeof(bss->bss));
1485 bss->last_scanned = 0;
1486 }
1487 }
1488 }
1489}
1490
1491static void orinoco_add_ext_scan_result(struct orinoco_private *priv,
1492 struct agere_ext_scan_info *atom)
1493{
1494 struct xbss_element *bss = NULL;
1495 int found = 0;
1496
1497 /* Try to update an existing bss first */
1498 list_for_each_entry(bss, &priv->bss_list, list) {
1499 if (compare_ether_addr(bss->bss.bssid, atom->bssid))
1500 continue;
1501 /* ESSID lengths */
1502 if (bss->bss.data[1] != atom->data[1])
1503 continue;
1504 if (memcmp(&bss->bss.data[2], &atom->data[2],
1505 atom->data[1]))
1506 continue;
1507 found = 1;
1508 break;
1509 }
1510
1511 /* Grab a bss off the free list */
1512 if (!found && !list_empty(&priv->bss_free_list)) {
1513 bss = list_entry(priv->bss_free_list.next,
1514 struct xbss_element, list);
1515 list_del(priv->bss_free_list.next);
1516
1517 list_add_tail(&bss->list, &priv->bss_list);
1518 }
1519
1520 if (bss) {
1521 /* Always update the BSS to get latest beacon info */
1522 memcpy(&bss->bss, atom, sizeof(bss->bss));
1523 bss->last_scanned = jiffies;
Dan Williams1e3428e2007-10-10 23:56:25 -04001524 }
1525}
1526
1527static int orinoco_process_scan_results(struct net_device *dev,
1528 unsigned char *buf,
1529 int len)
1530{
1531 struct orinoco_private *priv = netdev_priv(dev);
1532 int offset; /* In the scan data */
1533 union hermes_scan_info *atom;
1534 int atom_len;
1535
1536 switch (priv->firmware_type) {
1537 case FIRMWARE_TYPE_AGERE:
1538 atom_len = sizeof(struct agere_scan_apinfo);
1539 offset = 0;
1540 break;
1541 case FIRMWARE_TYPE_SYMBOL:
1542 /* Lack of documentation necessitates this hack.
1543 * Different firmwares have 68 or 76 byte long atoms.
1544 * We try modulo first. If the length divides by both,
1545 * we check what would be the channel in the second
1546 * frame for a 68-byte atom. 76-byte atoms have 0 there.
1547 * Valid channel cannot be 0. */
1548 if (len % 76)
1549 atom_len = 68;
1550 else if (len % 68)
1551 atom_len = 76;
1552 else if (len >= 1292 && buf[68] == 0)
1553 atom_len = 76;
1554 else
1555 atom_len = 68;
1556 offset = 0;
1557 break;
1558 case FIRMWARE_TYPE_INTERSIL:
1559 offset = 4;
1560 if (priv->has_hostscan) {
1561 atom_len = le16_to_cpup((__le16 *)buf);
1562 /* Sanity check for atom_len */
1563 if (atom_len < sizeof(struct prism2_scan_apinfo)) {
1564 printk(KERN_ERR "%s: Invalid atom_len in scan "
1565 "data: %d\n", dev->name, atom_len);
1566 return -EIO;
1567 }
1568 } else
1569 atom_len = offsetof(struct prism2_scan_apinfo, atim);
1570 break;
1571 default:
1572 return -EOPNOTSUPP;
1573 }
1574
1575 /* Check that we got an whole number of atoms */
1576 if ((len - offset) % atom_len) {
1577 printk(KERN_ERR "%s: Unexpected scan data length %d, "
1578 "atom_len %d, offset %d\n", dev->name, len,
1579 atom_len, offset);
1580 return -EIO;
1581 }
1582
1583 orinoco_clear_scan_results(priv, msecs_to_jiffies(15000));
1584
1585 /* Read the entries one by one */
1586 for (; offset + atom_len <= len; offset += atom_len) {
1587 int found = 0;
David Kilroy3056c402008-08-21 23:27:57 +01001588 struct bss_element *bss = NULL;
Dan Williams1e3428e2007-10-10 23:56:25 -04001589
1590 /* Get next atom */
1591 atom = (union hermes_scan_info *) (buf + offset);
1592
1593 /* Try to update an existing bss first */
1594 list_for_each_entry(bss, &priv->bss_list, list) {
1595 if (compare_ether_addr(bss->bss.a.bssid, atom->a.bssid))
1596 continue;
1597 if (le16_to_cpu(bss->bss.a.essid_len) !=
1598 le16_to_cpu(atom->a.essid_len))
1599 continue;
1600 if (memcmp(bss->bss.a.essid, atom->a.essid,
1601 le16_to_cpu(atom->a.essid_len)))
1602 continue;
Dan Williams1e3428e2007-10-10 23:56:25 -04001603 found = 1;
1604 break;
1605 }
1606
1607 /* Grab a bss off the free list */
1608 if (!found && !list_empty(&priv->bss_free_list)) {
1609 bss = list_entry(priv->bss_free_list.next,
David Kilroy3056c402008-08-21 23:27:57 +01001610 struct bss_element, list);
Dan Williams1e3428e2007-10-10 23:56:25 -04001611 list_del(priv->bss_free_list.next);
1612
Dan Williams1e3428e2007-10-10 23:56:25 -04001613 list_add_tail(&bss->list, &priv->bss_list);
1614 }
Dan Williams22367612007-12-05 11:01:23 -05001615
1616 if (bss) {
1617 /* Always update the BSS to get latest beacon info */
1618 memcpy(&bss->bss, atom, sizeof(bss->bss));
1619 bss->last_scanned = jiffies;
1620 }
Dan Williams1e3428e2007-10-10 23:56:25 -04001621 }
1622
1623 return 0;
1624}
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626static void __orinoco_ev_info(struct net_device *dev, hermes_t *hw)
1627{
1628 struct orinoco_private *priv = netdev_priv(dev);
1629 u16 infofid;
1630 struct {
Pavel Roskind133ae42005-09-23 04:18:06 -04001631 __le16 len;
1632 __le16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 } __attribute__ ((packed)) info;
1634 int len, type;
1635 int err;
1636
1637 /* This is an answer to an INQUIRE command that we did earlier,
1638 * or an information "event" generated by the card
1639 * The controller return to us a pseudo frame containing
1640 * the information in question - Jean II */
1641 infofid = hermes_read_regn(hw, INFOFID);
1642
1643 /* Read the info frame header - don't try too hard */
1644 err = hermes_bap_pread(hw, IRQ_BAP, &info, sizeof(info),
1645 infofid, 0);
1646 if (err) {
1647 printk(KERN_ERR "%s: error %d reading info frame. "
1648 "Frame dropped.\n", dev->name, err);
1649 return;
1650 }
1651
1652 len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
1653 type = le16_to_cpu(info.type);
1654
1655 switch (type) {
1656 case HERMES_INQ_TALLIES: {
1657 struct hermes_tallies_frame tallies;
1658 struct iw_statistics *wstats = &priv->wstats;
1659
1660 if (len > sizeof(tallies)) {
1661 printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
1662 dev->name, len);
1663 len = sizeof(tallies);
1664 }
1665
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001666 err = hermes_bap_pread(hw, IRQ_BAP, &tallies, len,
1667 infofid, sizeof(info));
1668 if (err)
1669 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671 /* Increment our various counters */
1672 /* wstats->discard.nwid - no wrong BSSID stuff */
1673 wstats->discard.code +=
1674 le16_to_cpu(tallies.RxWEPUndecryptable);
1675 if (len == sizeof(tallies))
1676 wstats->discard.code +=
1677 le16_to_cpu(tallies.RxDiscards_WEPICVError) +
1678 le16_to_cpu(tallies.RxDiscards_WEPExcluded);
1679 wstats->discard.misc +=
1680 le16_to_cpu(tallies.TxDiscardsWrongSA);
1681 wstats->discard.fragment +=
1682 le16_to_cpu(tallies.RxMsgInBadMsgFragments);
1683 wstats->discard.retries +=
1684 le16_to_cpu(tallies.TxRetryLimitExceeded);
1685 /* wstats->miss.beacon - no match */
1686 }
1687 break;
1688 case HERMES_INQ_LINKSTATUS: {
1689 struct hermes_linkstatus linkstatus;
1690 u16 newstatus;
1691 int connected;
1692
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001693 if (priv->iw_mode == IW_MODE_MONITOR)
1694 break;
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 if (len != sizeof(linkstatus)) {
1697 printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
1698 dev->name, len);
1699 break;
1700 }
1701
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001702 err = hermes_bap_pread(hw, IRQ_BAP, &linkstatus, len,
1703 infofid, sizeof(info));
1704 if (err)
1705 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 newstatus = le16_to_cpu(linkstatus.linkstatus);
1707
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001708 /* Symbol firmware uses "out of range" to signal that
1709 * the hostscan frame can be requested. */
1710 if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
1711 priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
1712 priv->has_hostscan && priv->scan_inprogress) {
1713 hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
1714 break;
1715 }
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
1718 || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
1719 || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
1720
1721 if (connected)
1722 netif_carrier_on(dev);
David Gibson7bb7c3a2005-05-12 20:02:10 -04001723 else if (!ignore_disconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 netif_carrier_off(dev);
1725
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001726 if (newstatus != priv->last_linkstatus) {
1727 priv->last_linkstatus = newstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 print_linkstatus(dev, newstatus);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001729 /* The info frame contains only one word which is the
1730 * status (see hermes.h). The status is pretty boring
1731 * in itself, that's why we export the new BSSID...
1732 * Jean II */
1733 schedule_work(&priv->wevent_work);
1734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 }
1736 break;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001737 case HERMES_INQ_SCAN:
1738 if (!priv->scan_inprogress && priv->bssid_fixed &&
1739 priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
1740 schedule_work(&priv->join_work);
1741 break;
1742 }
1743 /* fall through */
1744 case HERMES_INQ_HOSTSCAN:
1745 case HERMES_INQ_HOSTSCAN_SYMBOL: {
1746 /* Result of a scanning. Contains information about
1747 * cells in the vicinity - Jean II */
1748 union iwreq_data wrqu;
1749 unsigned char *buf;
1750
Dan Williams1e3428e2007-10-10 23:56:25 -04001751 /* Scan is no longer in progress */
1752 priv->scan_inprogress = 0;
1753
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001754 /* Sanity check */
1755 if (len > 4096) {
1756 printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
1757 dev->name, len);
1758 break;
1759 }
1760
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001761 /* Allocate buffer for results */
1762 buf = kmalloc(len, GFP_ATOMIC);
1763 if (buf == NULL)
1764 /* No memory, so can't printk()... */
1765 break;
1766
1767 /* Read scan data */
1768 err = hermes_bap_pread(hw, IRQ_BAP, (void *) buf, len,
1769 infofid, sizeof(info));
Pavel Roskin708218b2005-09-01 20:05:19 -04001770 if (err) {
1771 kfree(buf);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001772 break;
Pavel Roskin708218b2005-09-01 20:05:19 -04001773 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001774
1775#ifdef ORINOCO_DEBUG
1776 {
1777 int i;
1778 printk(KERN_DEBUG "Scan result [%02X", buf[0]);
1779 for(i = 1; i < (len * 2); i++)
1780 printk(":%02X", buf[i]);
1781 printk("]\n");
1782 }
1783#endif /* ORINOCO_DEBUG */
1784
Dan Williams1e3428e2007-10-10 23:56:25 -04001785 if (orinoco_process_scan_results(dev, buf, len) == 0) {
1786 /* Send an empty event to user space.
1787 * We don't send the received data on the event because
1788 * it would require us to do complex transcoding, and
1789 * we want to minimise the work done in the irq handler
1790 * Use a request to extract the data - Jean II */
1791 wrqu.data.length = 0;
1792 wrqu.data.flags = 0;
1793 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
1794 }
1795 kfree(buf);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001796 }
1797 break;
David Kilroy01632fa2008-08-21 23:27:58 +01001798 case HERMES_INQ_CHANNELINFO:
1799 {
1800 struct agere_ext_scan_info *bss;
1801
1802 if (!priv->scan_inprogress) {
1803 printk(KERN_DEBUG "%s: Got chaninfo without scan, "
1804 "len=%d\n", dev->name, len);
1805 break;
1806 }
1807
1808 /* An empty result indicates that the scan is complete */
1809 if (len == 0) {
1810 union iwreq_data wrqu;
1811
1812 /* Scan is no longer in progress */
1813 priv->scan_inprogress = 0;
1814
1815 wrqu.data.length = 0;
1816 wrqu.data.flags = 0;
1817 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
1818 break;
1819 }
1820
1821 /* Sanity check */
1822 else if (len > sizeof(*bss)) {
1823 printk(KERN_WARNING
1824 "%s: Ext scan results too large (%d bytes). "
1825 "Truncating results to %zd bytes.\n",
1826 dev->name, len, sizeof(*bss));
1827 len = sizeof(*bss);
1828 } else if (len < (offsetof(struct agere_ext_scan_info,
1829 data) + 2)) {
1830 /* Drop this result now so we don't have to
1831 * keep checking later */
1832 printk(KERN_WARNING
1833 "%s: Ext scan results too short (%d bytes)\n",
1834 dev->name, len);
1835 break;
1836 }
1837
1838 bss = kmalloc(sizeof(*bss), GFP_ATOMIC);
1839 if (bss == NULL)
1840 break;
1841
1842 /* Read scan data */
1843 err = hermes_bap_pread(hw, IRQ_BAP, (void *) bss, len,
1844 infofid, sizeof(info));
1845 if (err) {
1846 kfree(bss);
1847 break;
1848 }
1849
1850 orinoco_add_ext_scan_result(priv, bss);
1851
1852 kfree(bss);
1853 break;
1854 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001855 case HERMES_INQ_SEC_STAT_AGERE:
1856 /* Security status (Agere specific) */
1857 /* Ignore this frame for now */
1858 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
1859 break;
1860 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 default:
1862 printk(KERN_DEBUG "%s: Unknown information frame received: "
1863 "type 0x%04x, length %d\n", dev->name, type, len);
1864 /* We don't actually do anything about it */
1865 break;
1866 }
1867}
1868
1869static void __orinoco_ev_infdrop(struct net_device *dev, hermes_t *hw)
1870{
1871 if (net_ratelimit())
1872 printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
1873}
1874
1875/********************************************************************/
1876/* Internal hardware control routines */
1877/********************************************************************/
1878
1879int __orinoco_up(struct net_device *dev)
1880{
1881 struct orinoco_private *priv = netdev_priv(dev);
1882 struct hermes *hw = &priv->hw;
1883 int err;
1884
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001885 netif_carrier_off(dev); /* just to make sure */
1886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 err = __orinoco_program_rids(dev);
1888 if (err) {
1889 printk(KERN_ERR "%s: Error %d configuring card\n",
1890 dev->name, err);
1891 return err;
1892 }
1893
1894 /* Fire things up again */
1895 hermes_set_irqmask(hw, ORINOCO_INTEN);
1896 err = hermes_enable_port(hw, 0);
1897 if (err) {
1898 printk(KERN_ERR "%s: Error %d enabling MAC port\n",
1899 dev->name, err);
1900 return err;
1901 }
1902
1903 netif_start_queue(dev);
1904
1905 return 0;
1906}
1907
1908int __orinoco_down(struct net_device *dev)
1909{
1910 struct orinoco_private *priv = netdev_priv(dev);
1911 struct hermes *hw = &priv->hw;
1912 int err;
1913
1914 netif_stop_queue(dev);
1915
1916 if (! priv->hw_unavailable) {
1917 if (! priv->broken_disableport) {
1918 err = hermes_disable_port(hw, 0);
1919 if (err) {
1920 /* Some firmwares (e.g. Intersil 1.3.x) seem
1921 * to have problems disabling the port, oh
1922 * well, too bad. */
1923 printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
1924 dev->name, err);
1925 priv->broken_disableport = 1;
1926 }
1927 }
1928 hermes_set_irqmask(hw, 0);
1929 hermes_write_regn(hw, EVACK, 0xffff);
1930 }
1931
1932 /* firmware will have to reassociate */
1933 netif_carrier_off(dev);
1934 priv->last_linkstatus = 0xffff;
1935
1936 return 0;
1937}
1938
Pavel Roskin37a6c612006-04-07 04:10:49 -04001939static int orinoco_allocate_fid(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
1941 struct orinoco_private *priv = netdev_priv(dev);
1942 struct hermes *hw = &priv->hw;
1943 int err;
1944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 err = hermes_allocate(hw, priv->nicbuf_size, &priv->txfid);
David Gibsonb24d4582005-05-12 20:04:16 -04001946 if (err == -EIO && priv->nicbuf_size > TX_NICBUF_SIZE_BUG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 /* Try workaround for old Symbol firmware bug */
1948 printk(KERN_WARNING "%s: firmware ALLOC bug detected "
1949 "(old Symbol firmware?). Trying to work around... ",
1950 dev->name);
1951
1952 priv->nicbuf_size = TX_NICBUF_SIZE_BUG;
1953 err = hermes_allocate(hw, priv->nicbuf_size, &priv->txfid);
1954 if (err)
1955 printk("failed!\n");
1956 else
1957 printk("ok.\n");
1958 }
1959
1960 return err;
1961}
1962
Pavel Roskin37a6c612006-04-07 04:10:49 -04001963int orinoco_reinit_firmware(struct net_device *dev)
1964{
1965 struct orinoco_private *priv = netdev_priv(dev);
1966 struct hermes *hw = &priv->hw;
1967 int err;
1968
1969 err = hermes_init(hw);
1970 if (!err)
1971 err = orinoco_allocate_fid(dev);
1972
1973 return err;
1974}
1975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976static int __orinoco_hw_set_bitrate(struct orinoco_private *priv)
1977{
1978 hermes_t *hw = &priv->hw;
1979 int err = 0;
1980
1981 if (priv->bitratemode >= BITRATE_TABLE_SIZE) {
1982 printk(KERN_ERR "%s: BUG: Invalid bitrate mode %d\n",
1983 priv->ndev->name, priv->bitratemode);
1984 return -EINVAL;
1985 }
1986
1987 switch (priv->firmware_type) {
1988 case FIRMWARE_TYPE_AGERE:
1989 err = hermes_write_wordrec(hw, USER_BAP,
1990 HERMES_RID_CNFTXRATECONTROL,
1991 bitrate_table[priv->bitratemode].agere_txratectrl);
1992 break;
1993 case FIRMWARE_TYPE_INTERSIL:
1994 case FIRMWARE_TYPE_SYMBOL:
1995 err = hermes_write_wordrec(hw, USER_BAP,
1996 HERMES_RID_CNFTXRATECONTROL,
1997 bitrate_table[priv->bitratemode].intersil_txratectrl);
1998 break;
1999 default:
2000 BUG();
2001 }
2002
2003 return err;
2004}
2005
Christoph Hellwig16739b02005-06-19 01:27:51 +02002006/* Set fixed AP address */
2007static int __orinoco_hw_set_wap(struct orinoco_private *priv)
2008{
2009 int roaming_flag;
2010 int err = 0;
2011 hermes_t *hw = &priv->hw;
2012
2013 switch (priv->firmware_type) {
2014 case FIRMWARE_TYPE_AGERE:
2015 /* not supported */
2016 break;
2017 case FIRMWARE_TYPE_INTERSIL:
2018 if (priv->bssid_fixed)
2019 roaming_flag = 2;
2020 else
2021 roaming_flag = 1;
2022
2023 err = hermes_write_wordrec(hw, USER_BAP,
2024 HERMES_RID_CNFROAMINGMODE,
2025 roaming_flag);
2026 break;
2027 case FIRMWARE_TYPE_SYMBOL:
2028 err = HERMES_WRITE_RECORD(hw, USER_BAP,
2029 HERMES_RID_CNFMANDATORYBSSID_SYMBOL,
2030 &priv->desired_bssid);
2031 break;
2032 }
2033 return err;
2034}
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036/* Change the WEP keys and/or the current keys. Can be called
2037 * either from __orinoco_hw_setup_wep() or directly from
2038 * orinoco_ioctl_setiwencode(). In the later case the association
2039 * with the AP is not broken (if the firmware can handle it),
2040 * which is needed for 802.1x implementations. */
2041static int __orinoco_hw_setup_wepkeys(struct orinoco_private *priv)
2042{
2043 hermes_t *hw = &priv->hw;
2044 int err = 0;
2045
2046 switch (priv->firmware_type) {
2047 case FIRMWARE_TYPE_AGERE:
2048 err = HERMES_WRITE_RECORD(hw, USER_BAP,
2049 HERMES_RID_CNFWEPKEYS_AGERE,
2050 &priv->keys);
2051 if (err)
2052 return err;
2053 err = hermes_write_wordrec(hw, USER_BAP,
2054 HERMES_RID_CNFTXKEY_AGERE,
2055 priv->tx_key);
2056 if (err)
2057 return err;
2058 break;
2059 case FIRMWARE_TYPE_INTERSIL:
2060 case FIRMWARE_TYPE_SYMBOL:
2061 {
2062 int keylen;
2063 int i;
2064
2065 /* Force uniform key length to work around firmware bugs */
2066 keylen = le16_to_cpu(priv->keys[priv->tx_key].len);
2067
2068 if (keylen > LARGE_KEY_SIZE) {
2069 printk(KERN_ERR "%s: BUG: Key %d has oversize length %d.\n",
2070 priv->ndev->name, priv->tx_key, keylen);
2071 return -E2BIG;
2072 }
2073
2074 /* Write all 4 keys */
2075 for(i = 0; i < ORINOCO_MAX_KEYS; i++) {
2076 err = hermes_write_ltv(hw, USER_BAP,
2077 HERMES_RID_CNFDEFAULTKEY0 + i,
2078 HERMES_BYTES_TO_RECLEN(keylen),
2079 priv->keys[i].data);
2080 if (err)
2081 return err;
2082 }
2083
2084 /* Write the index of the key used in transmission */
2085 err = hermes_write_wordrec(hw, USER_BAP,
2086 HERMES_RID_CNFWEPDEFAULTKEYID,
2087 priv->tx_key);
2088 if (err)
2089 return err;
2090 }
2091 break;
2092 }
2093
2094 return 0;
2095}
2096
2097static int __orinoco_hw_setup_wep(struct orinoco_private *priv)
2098{
2099 hermes_t *hw = &priv->hw;
2100 int err = 0;
2101 int master_wep_flag;
2102 int auth_flag;
2103
2104 if (priv->wep_on)
2105 __orinoco_hw_setup_wepkeys(priv);
2106
2107 if (priv->wep_restrict)
2108 auth_flag = HERMES_AUTH_SHARED_KEY;
2109 else
2110 auth_flag = HERMES_AUTH_OPEN;
2111
2112 switch (priv->firmware_type) {
2113 case FIRMWARE_TYPE_AGERE: /* Agere style WEP */
2114 if (priv->wep_on) {
2115 /* Enable the shared-key authentication. */
2116 err = hermes_write_wordrec(hw, USER_BAP,
2117 HERMES_RID_CNFAUTHENTICATION_AGERE,
2118 auth_flag);
2119 }
2120 err = hermes_write_wordrec(hw, USER_BAP,
2121 HERMES_RID_CNFWEPENABLED_AGERE,
2122 priv->wep_on);
2123 if (err)
2124 return err;
2125 break;
2126
2127 case FIRMWARE_TYPE_INTERSIL: /* Intersil style WEP */
2128 case FIRMWARE_TYPE_SYMBOL: /* Symbol style WEP */
2129 if (priv->wep_on) {
2130 if (priv->wep_restrict ||
2131 (priv->firmware_type == FIRMWARE_TYPE_SYMBOL))
2132 master_wep_flag = HERMES_WEP_PRIVACY_INVOKED |
2133 HERMES_WEP_EXCL_UNENCRYPTED;
2134 else
2135 master_wep_flag = HERMES_WEP_PRIVACY_INVOKED;
2136
2137 err = hermes_write_wordrec(hw, USER_BAP,
2138 HERMES_RID_CNFAUTHENTICATION,
2139 auth_flag);
2140 if (err)
2141 return err;
2142 } else
2143 master_wep_flag = 0;
2144
2145 if (priv->iw_mode == IW_MODE_MONITOR)
2146 master_wep_flag |= HERMES_WEP_HOST_DECRYPT;
2147
2148 /* Master WEP setting : on/off */
2149 err = hermes_write_wordrec(hw, USER_BAP,
2150 HERMES_RID_CNFWEPFLAGS_INTERSIL,
2151 master_wep_flag);
2152 if (err)
2153 return err;
2154
2155 break;
2156 }
2157
2158 return 0;
2159}
2160
2161static int __orinoco_program_rids(struct net_device *dev)
2162{
2163 struct orinoco_private *priv = netdev_priv(dev);
2164 hermes_t *hw = &priv->hw;
2165 int err;
2166 struct hermes_idstring idbuf;
2167
2168 /* Set the MAC address */
2169 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
2170 HERMES_BYTES_TO_RECLEN(ETH_ALEN), dev->dev_addr);
2171 if (err) {
2172 printk(KERN_ERR "%s: Error %d setting MAC address\n",
2173 dev->name, err);
2174 return err;
2175 }
2176
2177 /* Set up the link mode */
2178 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFPORTTYPE,
2179 priv->port_type);
2180 if (err) {
2181 printk(KERN_ERR "%s: Error %d setting port type\n",
2182 dev->name, err);
2183 return err;
2184 }
2185 /* Set the channel/frequency */
David Gibsond51d8b12005-05-12 20:03:36 -04002186 if (priv->channel != 0 && priv->iw_mode != IW_MODE_INFRA) {
2187 err = hermes_write_wordrec(hw, USER_BAP,
2188 HERMES_RID_CNFOWNCHANNEL,
2189 priv->channel);
2190 if (err) {
2191 printk(KERN_ERR "%s: Error %d setting channel %d\n",
2192 dev->name, err, priv->channel);
2193 return err;
2194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 }
2196
2197 if (priv->has_ibss) {
2198 u16 createibss;
2199
2200 if ((strlen(priv->desired_essid) == 0) && (priv->createibss)) {
2201 printk(KERN_WARNING "%s: This firmware requires an "
2202 "ESSID in IBSS-Ad-Hoc mode.\n", dev->name);
2203 /* With wvlan_cs, in this case, we would crash.
2204 * hopefully, this driver will behave better...
2205 * Jean II */
2206 createibss = 0;
2207 } else {
2208 createibss = priv->createibss;
2209 }
2210
2211 err = hermes_write_wordrec(hw, USER_BAP,
2212 HERMES_RID_CNFCREATEIBSS,
2213 createibss);
2214 if (err) {
2215 printk(KERN_ERR "%s: Error %d setting CREATEIBSS\n",
2216 dev->name, err);
2217 return err;
2218 }
2219 }
2220
Christoph Hellwig16739b02005-06-19 01:27:51 +02002221 /* Set the desired BSSID */
2222 err = __orinoco_hw_set_wap(priv);
2223 if (err) {
2224 printk(KERN_ERR "%s: Error %d setting AP address\n",
2225 dev->name, err);
2226 return err;
2227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 /* Set the desired ESSID */
2229 idbuf.len = cpu_to_le16(strlen(priv->desired_essid));
2230 memcpy(&idbuf.val, priv->desired_essid, sizeof(idbuf.val));
2231 /* WinXP wants partner to configure OWNSSID even in IBSS mode. (jimc) */
2232 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNSSID,
2233 HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
2234 &idbuf);
2235 if (err) {
2236 printk(KERN_ERR "%s: Error %d setting OWNSSID\n",
2237 dev->name, err);
2238 return err;
2239 }
2240 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFDESIREDSSID,
2241 HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
2242 &idbuf);
2243 if (err) {
2244 printk(KERN_ERR "%s: Error %d setting DESIREDSSID\n",
2245 dev->name, err);
2246 return err;
2247 }
2248
2249 /* Set the station name */
2250 idbuf.len = cpu_to_le16(strlen(priv->nick));
2251 memcpy(&idbuf.val, priv->nick, sizeof(idbuf.val));
2252 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
2253 HERMES_BYTES_TO_RECLEN(strlen(priv->nick)+2),
2254 &idbuf);
2255 if (err) {
2256 printk(KERN_ERR "%s: Error %d setting nickname\n",
2257 dev->name, err);
2258 return err;
2259 }
2260
2261 /* Set AP density */
2262 if (priv->has_sensitivity) {
2263 err = hermes_write_wordrec(hw, USER_BAP,
2264 HERMES_RID_CNFSYSTEMSCALE,
2265 priv->ap_density);
2266 if (err) {
2267 printk(KERN_WARNING "%s: Error %d setting SYSTEMSCALE. "
2268 "Disabling sensitivity control\n",
2269 dev->name, err);
2270
2271 priv->has_sensitivity = 0;
2272 }
2273 }
2274
2275 /* Set RTS threshold */
2276 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
2277 priv->rts_thresh);
2278 if (err) {
2279 printk(KERN_ERR "%s: Error %d setting RTS threshold\n",
2280 dev->name, err);
2281 return err;
2282 }
2283
2284 /* Set fragmentation threshold or MWO robustness */
2285 if (priv->has_mwo)
2286 err = hermes_write_wordrec(hw, USER_BAP,
2287 HERMES_RID_CNFMWOROBUST_AGERE,
2288 priv->mwo_robust);
2289 else
2290 err = hermes_write_wordrec(hw, USER_BAP,
2291 HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
2292 priv->frag_thresh);
2293 if (err) {
2294 printk(KERN_ERR "%s: Error %d setting fragmentation\n",
2295 dev->name, err);
2296 return err;
2297 }
2298
2299 /* Set bitrate */
2300 err = __orinoco_hw_set_bitrate(priv);
2301 if (err) {
2302 printk(KERN_ERR "%s: Error %d setting bitrate\n",
2303 dev->name, err);
2304 return err;
2305 }
2306
2307 /* Set power management */
2308 if (priv->has_pm) {
2309 err = hermes_write_wordrec(hw, USER_BAP,
2310 HERMES_RID_CNFPMENABLED,
2311 priv->pm_on);
2312 if (err) {
2313 printk(KERN_ERR "%s: Error %d setting up PM\n",
2314 dev->name, err);
2315 return err;
2316 }
2317
2318 err = hermes_write_wordrec(hw, USER_BAP,
2319 HERMES_RID_CNFMULTICASTRECEIVE,
2320 priv->pm_mcast);
2321 if (err) {
2322 printk(KERN_ERR "%s: Error %d setting up PM\n",
2323 dev->name, err);
2324 return err;
2325 }
2326 err = hermes_write_wordrec(hw, USER_BAP,
2327 HERMES_RID_CNFMAXSLEEPDURATION,
2328 priv->pm_period);
2329 if (err) {
2330 printk(KERN_ERR "%s: Error %d setting up PM\n",
2331 dev->name, err);
2332 return err;
2333 }
2334 err = hermes_write_wordrec(hw, USER_BAP,
2335 HERMES_RID_CNFPMHOLDOVERDURATION,
2336 priv->pm_timeout);
2337 if (err) {
2338 printk(KERN_ERR "%s: Error %d setting up PM\n",
2339 dev->name, err);
2340 return err;
2341 }
2342 }
2343
2344 /* Set preamble - only for Symbol so far... */
2345 if (priv->has_preamble) {
2346 err = hermes_write_wordrec(hw, USER_BAP,
2347 HERMES_RID_CNFPREAMBLE_SYMBOL,
2348 priv->preamble);
2349 if (err) {
2350 printk(KERN_ERR "%s: Error %d setting preamble\n",
2351 dev->name, err);
2352 return err;
2353 }
2354 }
2355
2356 /* Set up encryption */
2357 if (priv->has_wep) {
2358 err = __orinoco_hw_setup_wep(priv);
2359 if (err) {
2360 printk(KERN_ERR "%s: Error %d activating WEP\n",
2361 dev->name, err);
2362 return err;
2363 }
2364 }
2365
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02002366 if (priv->iw_mode == IW_MODE_MONITOR) {
2367 /* Enable monitor mode */
2368 dev->type = ARPHRD_IEEE80211;
2369 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
2370 HERMES_TEST_MONITOR, 0, NULL);
2371 } else {
2372 /* Disable monitor mode */
2373 dev->type = ARPHRD_ETHER;
2374 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
2375 HERMES_TEST_STOP, 0, NULL);
2376 }
2377 if (err)
2378 return err;
2379
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 /* Set promiscuity / multicast*/
2381 priv->promiscuous = 0;
2382 priv->mc_count = 0;
Herbert Xu932ff272006-06-09 12:20:56 -07002383
2384 /* FIXME: what about netif_tx_lock */
2385 __orinoco_set_multicast_list(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
2387 return 0;
2388}
2389
2390/* FIXME: return int? */
2391static void
2392__orinoco_set_multicast_list(struct net_device *dev)
2393{
2394 struct orinoco_private *priv = netdev_priv(dev);
2395 hermes_t *hw = &priv->hw;
2396 int err = 0;
2397 int promisc, mc_count;
2398
2399 /* The Hermes doesn't seem to have an allmulti mode, so we go
2400 * into promiscuous mode and let the upper levels deal. */
2401 if ( (dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
2402 (dev->mc_count > MAX_MULTICAST(priv)) ) {
2403 promisc = 1;
2404 mc_count = 0;
2405 } else {
2406 promisc = 0;
2407 mc_count = dev->mc_count;
2408 }
2409
2410 if (promisc != priv->promiscuous) {
2411 err = hermes_write_wordrec(hw, USER_BAP,
2412 HERMES_RID_CNFPROMISCUOUSMODE,
2413 promisc);
2414 if (err) {
2415 printk(KERN_ERR "%s: Error %d setting PROMISCUOUSMODE to 1.\n",
2416 dev->name, err);
2417 } else
2418 priv->promiscuous = promisc;
2419 }
2420
2421 if (! promisc && (mc_count || priv->mc_count) ) {
2422 struct dev_mc_list *p = dev->mc_list;
2423 struct hermes_multicast mclist;
2424 int i;
2425
2426 for (i = 0; i < mc_count; i++) {
2427 /* paranoia: is list shorter than mc_count? */
2428 BUG_ON(! p);
2429 /* paranoia: bad address size in list? */
2430 BUG_ON(p->dmi_addrlen != ETH_ALEN);
2431
2432 memcpy(mclist.addr[i], p->dmi_addr, ETH_ALEN);
2433 p = p->next;
2434 }
2435
2436 if (p)
2437 printk(KERN_WARNING "%s: Multicast list is "
2438 "longer than mc_count\n", dev->name);
2439
2440 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFGROUPADDRESSES,
2441 HERMES_BYTES_TO_RECLEN(priv->mc_count * ETH_ALEN),
2442 &mclist);
2443 if (err)
2444 printk(KERN_ERR "%s: Error %d setting multicast list.\n",
2445 dev->name, err);
2446 else
2447 priv->mc_count = mc_count;
2448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449}
2450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451/* This must be called from user context, without locks held - use
2452 * schedule_work() */
David Howellsc4028952006-11-22 14:57:56 +00002453static void orinoco_reset(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
David Howellsc4028952006-11-22 14:57:56 +00002455 struct orinoco_private *priv =
2456 container_of(work, struct orinoco_private, reset_work);
2457 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 struct hermes *hw = &priv->hw;
Christoph Hellwig8551cb92005-05-14 17:30:04 +02002459 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 unsigned long flags;
2461
2462 if (orinoco_lock(priv, &flags) != 0)
2463 /* When the hardware becomes available again, whatever
2464 * detects that is responsible for re-initializing
2465 * it. So no need for anything further */
2466 return;
2467
2468 netif_stop_queue(dev);
2469
2470 /* Shut off interrupts. Depending on what state the hardware
2471 * is in, this might not work, but we'll try anyway */
2472 hermes_set_irqmask(hw, 0);
2473 hermes_write_regn(hw, EVACK, 0xffff);
2474
2475 priv->hw_unavailable++;
2476 priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
2477 netif_carrier_off(dev);
2478
2479 orinoco_unlock(priv, &flags);
2480
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002481 /* Scanning support: Cleanup of driver struct */
Dan Williams1e3428e2007-10-10 23:56:25 -04002482 orinoco_clear_scan_results(priv, 0);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002483 priv->scan_inprogress = 0;
2484
Christoph Hellwig8551cb92005-05-14 17:30:04 +02002485 if (priv->hard_reset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 err = (*priv->hard_reset)(priv);
Christoph Hellwig8551cb92005-05-14 17:30:04 +02002487 if (err) {
2488 printk(KERN_ERR "%s: orinoco_reset: Error %d "
2489 "performing hard reset\n", dev->name, err);
2490 goto disable;
2491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 }
2493
David Kilroy3994d502008-08-21 23:27:54 +01002494 if (priv->do_fw_download) {
2495 err = orinoco_download(priv);
2496 if (err)
2497 priv->do_fw_download = 0;
2498 }
2499
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 err = orinoco_reinit_firmware(dev);
2501 if (err) {
2502 printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
2503 dev->name, err);
Christoph Hellwig8551cb92005-05-14 17:30:04 +02002504 goto disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 }
2506
2507 spin_lock_irq(&priv->lock); /* This has to be called from user context */
2508
2509 priv->hw_unavailable--;
2510
2511 /* priv->open or priv->hw_unavailable might have changed while
2512 * we dropped the lock */
2513 if (priv->open && (! priv->hw_unavailable)) {
2514 err = __orinoco_up(dev);
2515 if (err) {
2516 printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
2517 dev->name, err);
2518 } else
2519 dev->trans_start = jiffies;
2520 }
2521
2522 spin_unlock_irq(&priv->lock);
2523
2524 return;
Christoph Hellwig8551cb92005-05-14 17:30:04 +02002525 disable:
2526 hermes_set_irqmask(hw, 0);
2527 netif_device_detach(dev);
2528 printk(KERN_ERR "%s: Device has been disabled!\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529}
2530
2531/********************************************************************/
2532/* Interrupt handler */
2533/********************************************************************/
2534
2535static void __orinoco_ev_tick(struct net_device *dev, hermes_t *hw)
2536{
2537 printk(KERN_DEBUG "%s: TICK\n", dev->name);
2538}
2539
2540static void __orinoco_ev_wterr(struct net_device *dev, hermes_t *hw)
2541{
2542 /* This seems to happen a fair bit under load, but ignoring it
2543 seems to work fine...*/
2544 printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
2545 dev->name);
2546}
2547
David Howells7d12e782006-10-05 14:55:46 +01002548irqreturn_t orinoco_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549{
Jeff Garzikc31f28e2006-10-06 14:56:04 -04002550 struct net_device *dev = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 struct orinoco_private *priv = netdev_priv(dev);
2552 hermes_t *hw = &priv->hw;
2553 int count = MAX_IRQLOOPS_PER_IRQ;
2554 u16 evstat, events;
2555 /* These are used to detect a runaway interrupt situation */
2556 /* If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
2557 * we panic and shut down the hardware */
2558 static int last_irq_jiffy = 0; /* jiffies value the last time
2559 * we were called */
2560 static int loops_this_jiffy = 0;
2561 unsigned long flags;
2562
2563 if (orinoco_lock(priv, &flags) != 0) {
2564 /* If hw is unavailable - we don't know if the irq was
2565 * for us or not */
2566 return IRQ_HANDLED;
2567 }
2568
2569 evstat = hermes_read_regn(hw, EVSTAT);
2570 events = evstat & hw->inten;
2571 if (! events) {
2572 orinoco_unlock(priv, &flags);
2573 return IRQ_NONE;
2574 }
2575
2576 if (jiffies != last_irq_jiffy)
2577 loops_this_jiffy = 0;
2578 last_irq_jiffy = jiffies;
2579
2580 while (events && count--) {
2581 if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
2582 printk(KERN_WARNING "%s: IRQ handler is looping too "
2583 "much! Resetting.\n", dev->name);
2584 /* Disable interrupts for now */
2585 hermes_set_irqmask(hw, 0);
2586 schedule_work(&priv->reset_work);
2587 break;
2588 }
2589
2590 /* Check the card hasn't been removed */
2591 if (! hermes_present(hw)) {
2592 DEBUG(0, "orinoco_interrupt(): card removed\n");
2593 break;
2594 }
2595
2596 if (events & HERMES_EV_TICK)
2597 __orinoco_ev_tick(dev, hw);
2598 if (events & HERMES_EV_WTERR)
2599 __orinoco_ev_wterr(dev, hw);
2600 if (events & HERMES_EV_INFDROP)
2601 __orinoco_ev_infdrop(dev, hw);
2602 if (events & HERMES_EV_INFO)
2603 __orinoco_ev_info(dev, hw);
2604 if (events & HERMES_EV_RX)
2605 __orinoco_ev_rx(dev, hw);
2606 if (events & HERMES_EV_TXEXC)
2607 __orinoco_ev_txexc(dev, hw);
2608 if (events & HERMES_EV_TX)
2609 __orinoco_ev_tx(dev, hw);
2610 if (events & HERMES_EV_ALLOC)
2611 __orinoco_ev_alloc(dev, hw);
2612
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02002613 hermes_write_regn(hw, EVACK, evstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
2615 evstat = hermes_read_regn(hw, EVSTAT);
2616 events = evstat & hw->inten;
2617 };
2618
2619 orinoco_unlock(priv, &flags);
2620 return IRQ_HANDLED;
2621}
2622
2623/********************************************************************/
2624/* Initialization */
2625/********************************************************************/
2626
2627struct comp_id {
2628 u16 id, variant, major, minor;
2629} __attribute__ ((packed));
2630
2631static inline fwtype_t determine_firmware_type(struct comp_id *nic_id)
2632{
2633 if (nic_id->id < 0x8000)
2634 return FIRMWARE_TYPE_AGERE;
2635 else if (nic_id->id == 0x8000 && nic_id->major == 0)
2636 return FIRMWARE_TYPE_SYMBOL;
2637 else
2638 return FIRMWARE_TYPE_INTERSIL;
2639}
2640
2641/* Set priv->firmware type, determine firmware properties */
2642static int determine_firmware(struct net_device *dev)
2643{
2644 struct orinoco_private *priv = netdev_priv(dev);
2645 hermes_t *hw = &priv->hw;
2646 int err;
2647 struct comp_id nic_id, sta_id;
2648 unsigned int firmver;
Hennerich, Michaeldde6d432007-02-05 16:41:35 -08002649 char tmp[SYMBOL_MAX_VER_LEN+1] __attribute__((aligned(2)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
2651 /* Get the hardware version */
2652 err = HERMES_READ_RECORD(hw, USER_BAP, HERMES_RID_NICID, &nic_id);
2653 if (err) {
2654 printk(KERN_ERR "%s: Cannot read hardware identity: error %d\n",
2655 dev->name, err);
2656 return err;
2657 }
2658
2659 le16_to_cpus(&nic_id.id);
2660 le16_to_cpus(&nic_id.variant);
2661 le16_to_cpus(&nic_id.major);
2662 le16_to_cpus(&nic_id.minor);
2663 printk(KERN_DEBUG "%s: Hardware identity %04x:%04x:%04x:%04x\n",
2664 dev->name, nic_id.id, nic_id.variant,
2665 nic_id.major, nic_id.minor);
2666
2667 priv->firmware_type = determine_firmware_type(&nic_id);
2668
2669 /* Get the firmware version */
2670 err = HERMES_READ_RECORD(hw, USER_BAP, HERMES_RID_STAID, &sta_id);
2671 if (err) {
2672 printk(KERN_ERR "%s: Cannot read station identity: error %d\n",
2673 dev->name, err);
2674 return err;
2675 }
2676
2677 le16_to_cpus(&sta_id.id);
2678 le16_to_cpus(&sta_id.variant);
2679 le16_to_cpus(&sta_id.major);
2680 le16_to_cpus(&sta_id.minor);
2681 printk(KERN_DEBUG "%s: Station identity %04x:%04x:%04x:%04x\n",
2682 dev->name, sta_id.id, sta_id.variant,
2683 sta_id.major, sta_id.minor);
2684
2685 switch (sta_id.id) {
2686 case 0x15:
2687 printk(KERN_ERR "%s: Primary firmware is active\n",
2688 dev->name);
2689 return -ENODEV;
2690 case 0x14b:
2691 printk(KERN_ERR "%s: Tertiary firmware is active\n",
2692 dev->name);
2693 return -ENODEV;
2694 case 0x1f: /* Intersil, Agere, Symbol Spectrum24 */
2695 case 0x21: /* Symbol Spectrum24 Trilogy */
2696 break;
2697 default:
2698 printk(KERN_NOTICE "%s: Unknown station ID, please report\n",
2699 dev->name);
2700 break;
2701 }
2702
2703 /* Default capabilities */
2704 priv->has_sensitivity = 1;
2705 priv->has_mwo = 0;
2706 priv->has_preamble = 0;
2707 priv->has_port3 = 1;
2708 priv->has_ibss = 1;
2709 priv->has_wep = 0;
2710 priv->has_big_wep = 0;
David Kilroy6eecad72008-08-21 23:27:56 +01002711 priv->has_alt_txcntl = 0;
David Kilroy01632fa2008-08-21 23:27:58 +01002712 priv->has_ext_scan = 0;
David Kilroy3994d502008-08-21 23:27:54 +01002713 priv->do_fw_download = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
2715 /* Determine capabilities from the firmware version */
2716 switch (priv->firmware_type) {
2717 case FIRMWARE_TYPE_AGERE:
2718 /* Lucent Wavelan IEEE, Lucent Orinoco, Cabletron RoamAbout,
2719 ELSA, Melco, HP, IBM, Dell 1150, Compaq 110/210 */
2720 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
2721 "Lucent/Agere %d.%02d", sta_id.major, sta_id.minor);
2722
2723 firmver = ((unsigned long)sta_id.major << 16) | sta_id.minor;
2724
2725 priv->has_ibss = (firmver >= 0x60006);
2726 priv->has_wep = (firmver >= 0x40020);
2727 priv->has_big_wep = 1; /* FIXME: this is wrong - how do we tell
2728 Gold cards from the others? */
2729 priv->has_mwo = (firmver >= 0x60000);
2730 priv->has_pm = (firmver >= 0x40020); /* Don't work in 7.52 ? */
2731 priv->ibss_port = 1;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002732 priv->has_hostscan = (firmver >= 0x8000a);
David Kilroy3994d502008-08-21 23:27:54 +01002733 priv->do_fw_download = 1;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02002734 priv->broken_monitor = (firmver >= 0x80000);
David Kilroy6eecad72008-08-21 23:27:56 +01002735 priv->has_alt_txcntl = (firmver >= 0x90000); /* All 9.x ? */
David Kilroy01632fa2008-08-21 23:27:58 +01002736 priv->has_ext_scan = (firmver >= 0x90000); /* All 9.x ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 /* Tested with Agere firmware :
2738 * 1.16 ; 4.08 ; 4.52 ; 6.04 ; 6.16 ; 7.28 => Jean II
2739 * Tested CableTron firmware : 4.32 => Anton */
2740 break;
2741 case FIRMWARE_TYPE_SYMBOL:
2742 /* Symbol , 3Com AirConnect, Intel, Ericsson WLAN */
2743 /* Intel MAC : 00:02:B3:* */
2744 /* 3Com MAC : 00:50:DA:* */
2745 memset(tmp, 0, sizeof(tmp));
2746 /* Get the Symbol firmware version */
2747 err = hermes_read_ltv(hw, USER_BAP,
2748 HERMES_RID_SECONDARYVERSION_SYMBOL,
2749 SYMBOL_MAX_VER_LEN, NULL, &tmp);
2750 if (err) {
2751 printk(KERN_WARNING
2752 "%s: Error %d reading Symbol firmware info. Wildly guessing capabilities...\n",
2753 dev->name, err);
2754 firmver = 0;
2755 tmp[0] = '\0';
2756 } else {
2757 /* The firmware revision is a string, the format is
2758 * something like : "V2.20-01".
2759 * Quick and dirty parsing... - Jean II
2760 */
2761 firmver = ((tmp[1] - '0') << 16) | ((tmp[3] - '0') << 12)
2762 | ((tmp[4] - '0') << 8) | ((tmp[6] - '0') << 4)
2763 | (tmp[7] - '0');
2764
2765 tmp[SYMBOL_MAX_VER_LEN] = '\0';
2766 }
2767
2768 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
2769 "Symbol %s", tmp);
2770
2771 priv->has_ibss = (firmver >= 0x20000);
2772 priv->has_wep = (firmver >= 0x15012);
2773 priv->has_big_wep = (firmver >= 0x20000);
2774 priv->has_pm = (firmver >= 0x20000 && firmver < 0x22000) ||
2775 (firmver >= 0x29000 && firmver < 0x30000) ||
2776 firmver >= 0x31000;
2777 priv->has_preamble = (firmver >= 0x20000);
2778 priv->ibss_port = 4;
David Kilroy3994d502008-08-21 23:27:54 +01002779
2780 /* Symbol firmware is found on various cards, but
2781 * there has been no attempt to check firmware
2782 * download on non-spectrum_cs based cards.
2783 *
2784 * Given that the Agere firmware download works
2785 * differently, we should avoid doing a firmware
2786 * download with the Symbol algorithm on non-spectrum
2787 * cards.
2788 *
2789 * For now we can identify a spectrum_cs based card
2790 * because it has a firmware reset function.
2791 */
2792 priv->do_fw_download = (priv->stop_fw != NULL);
2793
Christoph Hellwig649e59e2005-05-14 17:30:10 +02002794 priv->broken_disableport = (firmver == 0x25013) ||
2795 (firmver >= 0x30000 && firmver <= 0x31000);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002796 priv->has_hostscan = (firmver >= 0x31001) ||
2797 (firmver >= 0x29057 && firmver < 0x30000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 /* Tested with Intel firmware : 0x20015 => Jean II */
2799 /* Tested with 3Com firmware : 0x15012 & 0x22001 => Jean II */
2800 break;
2801 case FIRMWARE_TYPE_INTERSIL:
2802 /* D-Link, Linksys, Adtron, ZoomAir, and many others...
2803 * Samsung, Compaq 100/200 and Proxim are slightly
2804 * different and less well tested */
2805 /* D-Link MAC : 00:40:05:* */
2806 /* Addtron MAC : 00:90:D1:* */
2807 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
2808 "Intersil %d.%d.%d", sta_id.major, sta_id.minor,
2809 sta_id.variant);
2810
2811 firmver = ((unsigned long)sta_id.major << 16) |
2812 ((unsigned long)sta_id.minor << 8) | sta_id.variant;
2813
2814 priv->has_ibss = (firmver >= 0x000700); /* FIXME */
2815 priv->has_big_wep = priv->has_wep = (firmver >= 0x000800);
2816 priv->has_pm = (firmver >= 0x000700);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002817 priv->has_hostscan = (firmver >= 0x010301);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
2819 if (firmver >= 0x000800)
2820 priv->ibss_port = 0;
2821 else {
2822 printk(KERN_NOTICE "%s: Intersil firmware earlier "
2823 "than v0.8.x - several features not supported\n",
2824 dev->name);
2825 priv->ibss_port = 1;
2826 }
2827 break;
2828 }
2829 printk(KERN_DEBUG "%s: Firmware determined as %s\n", dev->name,
2830 priv->fw_name);
2831
2832 return 0;
2833}
2834
2835static int orinoco_init(struct net_device *dev)
2836{
2837 struct orinoco_private *priv = netdev_priv(dev);
2838 hermes_t *hw = &priv->hw;
2839 int err = 0;
2840 struct hermes_idstring nickbuf;
2841 u16 reclen;
2842 int len;
Joe Perches0795af52007-10-03 17:59:30 -07002843 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 /* No need to lock, the hw_unavailable flag is already set in
2846 * alloc_orinocodev() */
Jeff Garzikb4538722005-05-12 22:48:20 -04002847 priv->nicbuf_size = IEEE80211_FRAME_LEN + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
2849 /* Initialize the firmware */
Pavel Roskin37a6c612006-04-07 04:10:49 -04002850 err = hermes_init(hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 if (err != 0) {
2852 printk(KERN_ERR "%s: failed to initialize firmware (err = %d)\n",
2853 dev->name, err);
2854 goto out;
2855 }
2856
2857 err = determine_firmware(dev);
2858 if (err != 0) {
2859 printk(KERN_ERR "%s: Incompatible firmware, aborting\n",
2860 dev->name);
2861 goto out;
2862 }
2863
David Kilroy3994d502008-08-21 23:27:54 +01002864 if (priv->do_fw_download) {
2865 err = orinoco_download(priv);
2866 if (err)
2867 priv->do_fw_download = 0;
2868
2869 /* Check firmware version again */
2870 err = determine_firmware(dev);
2871 if (err != 0) {
2872 printk(KERN_ERR "%s: Incompatible firmware, aborting\n",
2873 dev->name);
2874 goto out;
2875 }
2876 }
2877
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 if (priv->has_port3)
2879 printk(KERN_DEBUG "%s: Ad-hoc demo mode supported\n", dev->name);
2880 if (priv->has_ibss)
2881 printk(KERN_DEBUG "%s: IEEE standard IBSS ad-hoc mode supported\n",
2882 dev->name);
2883 if (priv->has_wep) {
2884 printk(KERN_DEBUG "%s: WEP supported, ", dev->name);
2885 if (priv->has_big_wep)
2886 printk("104-bit key\n");
2887 else
2888 printk("40-bit key\n");
2889 }
2890
David Kilroy01632fa2008-08-21 23:27:58 +01002891 /* Now we have the firmware capabilities, allocate appropiate
2892 * sized scan buffers */
2893 if (orinoco_bss_data_allocate(priv))
2894 goto out;
2895 orinoco_bss_data_init(priv);
2896
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 /* Get the MAC address */
2898 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
2899 ETH_ALEN, NULL, dev->dev_addr);
2900 if (err) {
2901 printk(KERN_WARNING "%s: failed to read MAC address!\n",
2902 dev->name);
2903 goto out;
2904 }
2905
Joe Perches0795af52007-10-03 17:59:30 -07002906 printk(KERN_DEBUG "%s: MAC address %s\n",
2907 dev->name, print_mac(mac, dev->dev_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
2909 /* Get the station name */
2910 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
2911 sizeof(nickbuf), &reclen, &nickbuf);
2912 if (err) {
2913 printk(KERN_ERR "%s: failed to read station name\n",
2914 dev->name);
2915 goto out;
2916 }
2917 if (nickbuf.len)
2918 len = min(IW_ESSID_MAX_SIZE, (int)le16_to_cpu(nickbuf.len));
2919 else
2920 len = min(IW_ESSID_MAX_SIZE, 2 * reclen);
2921 memcpy(priv->nick, &nickbuf.val, len);
2922 priv->nick[len] = '\0';
2923
2924 printk(KERN_DEBUG "%s: Station name \"%s\"\n", dev->name, priv->nick);
2925
Pavel Roskin37a6c612006-04-07 04:10:49 -04002926 err = orinoco_allocate_fid(dev);
2927 if (err) {
2928 printk(KERN_ERR "%s: failed to allocate NIC buffer!\n",
2929 dev->name);
2930 goto out;
2931 }
2932
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 /* Get allowed channels */
2934 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CHANNELLIST,
2935 &priv->channel_mask);
2936 if (err) {
2937 printk(KERN_ERR "%s: failed to read channel list!\n",
2938 dev->name);
2939 goto out;
2940 }
2941
2942 /* Get initial AP density */
2943 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFSYSTEMSCALE,
2944 &priv->ap_density);
2945 if (err || priv->ap_density < 1 || priv->ap_density > 3) {
2946 priv->has_sensitivity = 0;
2947 }
2948
2949 /* Get initial RTS threshold */
2950 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
2951 &priv->rts_thresh);
2952 if (err) {
2953 printk(KERN_ERR "%s: failed to read RTS threshold!\n",
2954 dev->name);
2955 goto out;
2956 }
2957
2958 /* Get initial fragmentation settings */
2959 if (priv->has_mwo)
2960 err = hermes_read_wordrec(hw, USER_BAP,
2961 HERMES_RID_CNFMWOROBUST_AGERE,
2962 &priv->mwo_robust);
2963 else
2964 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
2965 &priv->frag_thresh);
2966 if (err) {
2967 printk(KERN_ERR "%s: failed to read fragmentation settings!\n",
2968 dev->name);
2969 goto out;
2970 }
2971
2972 /* Power management setup */
2973 if (priv->has_pm) {
2974 priv->pm_on = 0;
2975 priv->pm_mcast = 1;
2976 err = hermes_read_wordrec(hw, USER_BAP,
2977 HERMES_RID_CNFMAXSLEEPDURATION,
2978 &priv->pm_period);
2979 if (err) {
2980 printk(KERN_ERR "%s: failed to read power management period!\n",
2981 dev->name);
2982 goto out;
2983 }
2984 err = hermes_read_wordrec(hw, USER_BAP,
2985 HERMES_RID_CNFPMHOLDOVERDURATION,
2986 &priv->pm_timeout);
2987 if (err) {
2988 printk(KERN_ERR "%s: failed to read power management timeout!\n",
2989 dev->name);
2990 goto out;
2991 }
2992 }
2993
2994 /* Preamble setup */
2995 if (priv->has_preamble) {
2996 err = hermes_read_wordrec(hw, USER_BAP,
2997 HERMES_RID_CNFPREAMBLE_SYMBOL,
2998 &priv->preamble);
2999 if (err)
3000 goto out;
3001 }
3002
3003 /* Set up the default configuration */
3004 priv->iw_mode = IW_MODE_INFRA;
3005 /* By default use IEEE/IBSS ad-hoc mode if we have it */
3006 priv->prefer_port3 = priv->has_port3 && (! priv->has_ibss);
3007 set_port_type(priv);
David Gibsond51d8b12005-05-12 20:03:36 -04003008 priv->channel = 0; /* use firmware default */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
3010 priv->promiscuous = 0;
3011 priv->wep_on = 0;
3012 priv->tx_key = 0;
3013
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 /* Make the hardware available, as long as it hasn't been
3015 * removed elsewhere (e.g. by PCMCIA hot unplug) */
3016 spin_lock_irq(&priv->lock);
3017 priv->hw_unavailable--;
3018 spin_unlock_irq(&priv->lock);
3019
3020 printk(KERN_DEBUG "%s: ready\n", dev->name);
3021
3022 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 return err;
3024}
3025
David Kilroy3994d502008-08-21 23:27:54 +01003026struct net_device
3027*alloc_orinocodev(int sizeof_card,
3028 struct device *device,
3029 int (*hard_reset)(struct orinoco_private *),
3030 int (*stop_fw)(struct orinoco_private *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
3032 struct net_device *dev;
3033 struct orinoco_private *priv;
3034
3035 dev = alloc_etherdev(sizeof(struct orinoco_private) + sizeof_card);
3036 if (! dev)
3037 return NULL;
3038 priv = netdev_priv(dev);
3039 priv->ndev = dev;
3040 if (sizeof_card)
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02003041 priv->card = (void *)((unsigned long)priv
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 + sizeof(struct orinoco_private));
3043 else
3044 priv->card = NULL;
David Kilroy3994d502008-08-21 23:27:54 +01003045 priv->dev = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
3047 /* Setup / override net_device fields */
3048 dev->init = orinoco_init;
3049 dev->hard_start_xmit = orinoco_xmit;
3050 dev->tx_timeout = orinoco_tx_timeout;
3051 dev->watchdog_timeo = HZ; /* 1 second timeout */
3052 dev->get_stats = orinoco_get_stats;
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02003053 dev->ethtool_ops = &orinoco_ethtool_ops;
Christoph Hellwig620554e2005-06-19 01:27:33 +02003054 dev->wireless_handlers = (struct iw_handler_def *)&orinoco_handler_def;
Pavel Roskin343c6862005-09-09 18:43:02 -04003055#ifdef WIRELESS_SPY
3056 priv->wireless_data.spy_data = &priv->spy_data;
3057 dev->wireless_data = &priv->wireless_data;
3058#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 dev->change_mtu = orinoco_change_mtu;
3060 dev->set_multicast_list = orinoco_set_multicast_list;
3061 /* we use the default eth_mac_addr for setting the MAC addr */
3062
3063 /* Set up default callbacks */
3064 dev->open = orinoco_open;
3065 dev->stop = orinoco_stop;
3066 priv->hard_reset = hard_reset;
David Kilroy3994d502008-08-21 23:27:54 +01003067 priv->stop_fw = stop_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
3069 spin_lock_init(&priv->lock);
3070 priv->open = 0;
3071 priv->hw_unavailable = 1; /* orinoco_init() must clear this
3072 * before anything else touches the
3073 * hardware */
David Howellsc4028952006-11-22 14:57:56 +00003074 INIT_WORK(&priv->reset_work, orinoco_reset);
3075 INIT_WORK(&priv->join_work, orinoco_join_ap);
3076 INIT_WORK(&priv->wevent_work, orinoco_send_wevents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
3078 netif_carrier_off(dev);
3079 priv->last_linkstatus = 0xffff;
3080
3081 return dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082}
3083
3084void free_orinocodev(struct net_device *dev)
3085{
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003086 struct orinoco_private *priv = netdev_priv(dev);
3087
Dan Williams1e3428e2007-10-10 23:56:25 -04003088 orinoco_bss_data_free(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 free_netdev(dev);
3090}
3091
3092/********************************************************************/
3093/* Wireless extensions */
3094/********************************************************************/
3095
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07003096/* Return : < 0 -> error code ; >= 0 -> length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097static int orinoco_hw_get_essid(struct orinoco_private *priv, int *active,
3098 char buf[IW_ESSID_MAX_SIZE+1])
3099{
3100 hermes_t *hw = &priv->hw;
3101 int err = 0;
3102 struct hermes_idstring essidbuf;
3103 char *p = (char *)(&essidbuf.val);
3104 int len;
3105 unsigned long flags;
3106
3107 if (orinoco_lock(priv, &flags) != 0)
3108 return -EBUSY;
3109
3110 if (strlen(priv->desired_essid) > 0) {
3111 /* We read the desired SSID from the hardware rather
3112 than from priv->desired_essid, just in case the
3113 firmware is allowed to change it on us. I'm not
3114 sure about this */
3115 /* My guess is that the OWNSSID should always be whatever
3116 * we set to the card, whereas CURRENT_SSID is the one that
3117 * may change... - Jean II */
3118 u16 rid;
3119
3120 *active = 1;
3121
3122 rid = (priv->port_type == 3) ? HERMES_RID_CNFOWNSSID :
3123 HERMES_RID_CNFDESIREDSSID;
3124
3125 err = hermes_read_ltv(hw, USER_BAP, rid, sizeof(essidbuf),
3126 NULL, &essidbuf);
3127 if (err)
3128 goto fail_unlock;
3129 } else {
3130 *active = 0;
3131
3132 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTSSID,
3133 sizeof(essidbuf), NULL, &essidbuf);
3134 if (err)
3135 goto fail_unlock;
3136 }
3137
3138 len = le16_to_cpu(essidbuf.len);
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02003139 BUG_ON(len > IW_ESSID_MAX_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07003141 memset(buf, 0, IW_ESSID_MAX_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 memcpy(buf, p, len);
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07003143 err = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
3145 fail_unlock:
3146 orinoco_unlock(priv, &flags);
3147
3148 return err;
3149}
3150
3151static long orinoco_hw_get_freq(struct orinoco_private *priv)
3152{
3153
3154 hermes_t *hw = &priv->hw;
3155 int err = 0;
3156 u16 channel;
3157 long freq = 0;
3158 unsigned long flags;
3159
3160 if (orinoco_lock(priv, &flags) != 0)
3161 return -EBUSY;
3162
3163 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CURRENTCHANNEL, &channel);
3164 if (err)
3165 goto out;
3166
3167 /* Intersil firmware 1.3.5 returns 0 when the interface is down */
3168 if (channel == 0) {
3169 err = -EBUSY;
3170 goto out;
3171 }
3172
3173 if ( (channel < 1) || (channel > NUM_CHANNELS) ) {
3174 printk(KERN_WARNING "%s: Channel out of range (%d)!\n",
3175 priv->ndev->name, channel);
3176 err = -EBUSY;
3177 goto out;
3178
3179 }
3180 freq = channel_frequency[channel-1] * 100000;
3181
3182 out:
3183 orinoco_unlock(priv, &flags);
3184
3185 if (err > 0)
3186 err = -EBUSY;
3187 return err ? err : freq;
3188}
3189
3190static int orinoco_hw_get_bitratelist(struct orinoco_private *priv,
3191 int *numrates, s32 *rates, int max)
3192{
3193 hermes_t *hw = &priv->hw;
3194 struct hermes_idstring list;
3195 unsigned char *p = (unsigned char *)&list.val;
3196 int err = 0;
3197 int num;
3198 int i;
3199 unsigned long flags;
3200
3201 if (orinoco_lock(priv, &flags) != 0)
3202 return -EBUSY;
3203
3204 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_SUPPORTEDDATARATES,
3205 sizeof(list), NULL, &list);
3206 orinoco_unlock(priv, &flags);
3207
3208 if (err)
3209 return err;
3210
3211 num = le16_to_cpu(list.len);
3212 *numrates = num;
3213 num = min(num, max);
3214
3215 for (i = 0; i < num; i++) {
3216 rates[i] = (p[i] & 0x7f) * 500000; /* convert to bps */
3217 }
3218
3219 return 0;
3220}
3221
Christoph Hellwig620554e2005-06-19 01:27:33 +02003222static int orinoco_ioctl_getname(struct net_device *dev,
3223 struct iw_request_info *info,
3224 char *name,
3225 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226{
3227 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 int numrates;
Christoph Hellwig620554e2005-06-19 01:27:33 +02003229 int err;
3230
3231 err = orinoco_hw_get_bitratelist(priv, &numrates, NULL, 0);
3232
3233 if (!err && (numrates > 2))
3234 strcpy(name, "IEEE 802.11b");
3235 else
3236 strcpy(name, "IEEE 802.11-DS");
3237
3238 return 0;
3239}
3240
Christoph Hellwig16739b02005-06-19 01:27:51 +02003241static int orinoco_ioctl_setwap(struct net_device *dev,
3242 struct iw_request_info *info,
3243 struct sockaddr *ap_addr,
3244 char *extra)
3245{
3246 struct orinoco_private *priv = netdev_priv(dev);
3247 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 unsigned long flags;
Christoph Hellwig16739b02005-06-19 01:27:51 +02003249 static const u8 off_addr[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3250 static const u8 any_addr[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
3252 if (orinoco_lock(priv, &flags) != 0)
3253 return -EBUSY;
3254
Christoph Hellwig16739b02005-06-19 01:27:51 +02003255 /* Enable automatic roaming - no sanity checks are needed */
3256 if (memcmp(&ap_addr->sa_data, off_addr, ETH_ALEN) == 0 ||
3257 memcmp(&ap_addr->sa_data, any_addr, ETH_ALEN) == 0) {
3258 priv->bssid_fixed = 0;
3259 memset(priv->desired_bssid, 0, ETH_ALEN);
3260
3261 /* "off" means keep existing connection */
3262 if (ap_addr->sa_data[0] == 0) {
3263 __orinoco_hw_set_wap(priv);
3264 err = 0;
3265 }
3266 goto out;
3267 }
3268
3269 if (priv->firmware_type == FIRMWARE_TYPE_AGERE) {
3270 printk(KERN_WARNING "%s: Lucent/Agere firmware doesn't "
3271 "support manual roaming\n",
3272 dev->name);
3273 err = -EOPNOTSUPP;
3274 goto out;
3275 }
3276
3277 if (priv->iw_mode != IW_MODE_INFRA) {
3278 printk(KERN_WARNING "%s: Manual roaming supported only in "
3279 "managed mode\n", dev->name);
3280 err = -EOPNOTSUPP;
3281 goto out;
3282 }
3283
3284 /* Intersil firmware hangs without Desired ESSID */
3285 if (priv->firmware_type == FIRMWARE_TYPE_INTERSIL &&
3286 strlen(priv->desired_essid) == 0) {
3287 printk(KERN_WARNING "%s: Desired ESSID must be set for "
3288 "manual roaming\n", dev->name);
3289 err = -EOPNOTSUPP;
3290 goto out;
3291 }
3292
3293 /* Finally, enable manual roaming */
3294 priv->bssid_fixed = 1;
3295 memcpy(priv->desired_bssid, &ap_addr->sa_data, ETH_ALEN);
3296
3297 out:
3298 orinoco_unlock(priv, &flags);
3299 return err;
3300}
3301
Christoph Hellwig620554e2005-06-19 01:27:33 +02003302static int orinoco_ioctl_getwap(struct net_device *dev,
3303 struct iw_request_info *info,
3304 struct sockaddr *ap_addr,
3305 char *extra)
3306{
3307 struct orinoco_private *priv = netdev_priv(dev);
3308
3309 hermes_t *hw = &priv->hw;
3310 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 unsigned long flags;
3312
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 if (orinoco_lock(priv, &flags) != 0)
3314 return -EBUSY;
3315
Christoph Hellwig620554e2005-06-19 01:27:33 +02003316 ap_addr->sa_family = ARPHRD_ETHER;
3317 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
3318 ETH_ALEN, NULL, ap_addr->sa_data);
3319
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 orinoco_unlock(priv, &flags);
3321
Christoph Hellwig620554e2005-06-19 01:27:33 +02003322 return err;
3323}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324
Christoph Hellwig620554e2005-06-19 01:27:33 +02003325static int orinoco_ioctl_setmode(struct net_device *dev,
3326 struct iw_request_info *info,
3327 u32 *mode,
3328 char *extra)
3329{
3330 struct orinoco_private *priv = netdev_priv(dev);
3331 int err = -EINPROGRESS; /* Call commit handler */
3332 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
Christoph Hellwig620554e2005-06-19 01:27:33 +02003334 if (priv->iw_mode == *mode)
3335 return 0;
3336
3337 if (orinoco_lock(priv, &flags) != 0)
3338 return -EBUSY;
3339
3340 switch (*mode) {
3341 case IW_MODE_ADHOC:
3342 if (!priv->has_ibss && !priv->has_port3)
3343 err = -EOPNOTSUPP;
3344 break;
3345
3346 case IW_MODE_INFRA:
3347 break;
3348
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02003349 case IW_MODE_MONITOR:
3350 if (priv->broken_monitor && !force_monitor) {
3351 printk(KERN_WARNING "%s: Monitor mode support is "
3352 "buggy in this firmware, not enabling\n",
3353 dev->name);
3354 err = -EOPNOTSUPP;
3355 }
3356 break;
3357
Christoph Hellwig620554e2005-06-19 01:27:33 +02003358 default:
3359 err = -EOPNOTSUPP;
3360 break;
3361 }
3362
3363 if (err == -EINPROGRESS) {
3364 priv->iw_mode = *mode;
3365 set_port_type(priv);
3366 }
3367
3368 orinoco_unlock(priv, &flags);
3369
3370 return err;
3371}
3372
3373static int orinoco_ioctl_getmode(struct net_device *dev,
3374 struct iw_request_info *info,
3375 u32 *mode,
3376 char *extra)
3377{
3378 struct orinoco_private *priv = netdev_priv(dev);
3379
3380 *mode = priv->iw_mode;
3381 return 0;
3382}
3383
3384static int orinoco_ioctl_getiwrange(struct net_device *dev,
3385 struct iw_request_info *info,
3386 struct iw_point *rrq,
3387 char *extra)
3388{
3389 struct orinoco_private *priv = netdev_priv(dev);
3390 int err = 0;
3391 struct iw_range *range = (struct iw_range *) extra;
3392 int numrates;
3393 int i, k;
3394
Christoph Hellwig620554e2005-06-19 01:27:33 +02003395 rrq->length = sizeof(struct iw_range);
3396 memset(range, 0, sizeof(struct iw_range));
3397
3398 range->we_version_compiled = WIRELESS_EXT;
3399 range->we_version_source = 14;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400
3401 /* Set available channels/frequencies */
Christoph Hellwig620554e2005-06-19 01:27:33 +02003402 range->num_channels = NUM_CHANNELS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 k = 0;
3404 for (i = 0; i < NUM_CHANNELS; i++) {
3405 if (priv->channel_mask & (1 << i)) {
Christoph Hellwig620554e2005-06-19 01:27:33 +02003406 range->freq[k].i = i + 1;
3407 range->freq[k].m = channel_frequency[i] * 100000;
3408 range->freq[k].e = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 k++;
3410 }
3411
3412 if (k >= IW_MAX_FREQUENCIES)
3413 break;
3414 }
Christoph Hellwig620554e2005-06-19 01:27:33 +02003415 range->num_frequency = k;
3416 range->sensitivity = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417
Christoph Hellwig620554e2005-06-19 01:27:33 +02003418 if (priv->has_wep) {
3419 range->max_encoding_tokens = ORINOCO_MAX_KEYS;
3420 range->encoding_size[0] = SMALL_KEY_SIZE;
3421 range->num_encoding_sizes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422
Christoph Hellwig620554e2005-06-19 01:27:33 +02003423 if (priv->has_big_wep) {
3424 range->encoding_size[1] = LARGE_KEY_SIZE;
3425 range->num_encoding_sizes = 2;
3426 }
3427 }
3428
Pavel Roskin343c6862005-09-09 18:43:02 -04003429 if ((priv->iw_mode == IW_MODE_ADHOC) && (!SPY_NUMBER(priv))){
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 /* Quality stats meaningless in ad-hoc mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 } else {
Christoph Hellwig620554e2005-06-19 01:27:33 +02003432 range->max_qual.qual = 0x8b - 0x2f;
3433 range->max_qual.level = 0x2f - 0x95 - 1;
3434 range->max_qual.noise = 0x2f - 0x95 - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 /* Need to get better values */
Christoph Hellwig620554e2005-06-19 01:27:33 +02003436 range->avg_qual.qual = 0x24;
3437 range->avg_qual.level = 0xC2;
3438 range->avg_qual.noise = 0x9E;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 }
3440
3441 err = orinoco_hw_get_bitratelist(priv, &numrates,
Christoph Hellwig620554e2005-06-19 01:27:33 +02003442 range->bitrate, IW_MAX_BITRATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 if (err)
3444 return err;
Christoph Hellwig620554e2005-06-19 01:27:33 +02003445 range->num_bitrates = numrates;
3446
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 /* Set an indication of the max TCP throughput in bit/s that we can
3448 * expect using this interface. May be use for QoS stuff...
3449 * Jean II */
Christoph Hellwig620554e2005-06-19 01:27:33 +02003450 if (numrates > 2)
3451 range->throughput = 5 * 1000 * 1000; /* ~5 Mb/s */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 else
Christoph Hellwig620554e2005-06-19 01:27:33 +02003453 range->throughput = 1.5 * 1000 * 1000; /* ~1.5 Mb/s */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
Christoph Hellwig620554e2005-06-19 01:27:33 +02003455 range->min_rts = 0;
3456 range->max_rts = 2347;
3457 range->min_frag = 256;
3458 range->max_frag = 2346;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
Christoph Hellwig620554e2005-06-19 01:27:33 +02003460 range->min_pmp = 0;
3461 range->max_pmp = 65535000;
3462 range->min_pmt = 0;
3463 range->max_pmt = 65535 * 1000; /* ??? */
3464 range->pmp_flags = IW_POWER_PERIOD;
3465 range->pmt_flags = IW_POWER_TIMEOUT;
3466 range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_UNICAST_R;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467
Christoph Hellwig620554e2005-06-19 01:27:33 +02003468 range->retry_capa = IW_RETRY_LIMIT | IW_RETRY_LIFETIME;
3469 range->retry_flags = IW_RETRY_LIMIT;
3470 range->r_time_flags = IW_RETRY_LIFETIME;
3471 range->min_retry = 0;
3472 range->max_retry = 65535; /* ??? */
3473 range->min_r_time = 0;
3474 range->max_r_time = 65535 * 1000; /* ??? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
David Kilroy0753bba2008-08-21 23:27:46 +01003476 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
3477 range->scan_capa = IW_SCAN_CAPA_ESSID;
3478 else
3479 range->scan_capa = IW_SCAN_CAPA_NONE;
3480
Pavel Roskin343c6862005-09-09 18:43:02 -04003481 /* Event capability (kernel) */
3482 IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
3483 /* Event capability (driver) */
3484 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWTHRSPY);
3485 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
3486 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
3487 IW_EVENT_CAPA_SET(range->event_capa, IWEVTXDROP);
3488
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 return 0;
3490}
3491
Christoph Hellwig620554e2005-06-19 01:27:33 +02003492static int orinoco_ioctl_setiwencode(struct net_device *dev,
3493 struct iw_request_info *info,
3494 struct iw_point *erq,
3495 char *keybuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496{
3497 struct orinoco_private *priv = netdev_priv(dev);
3498 int index = (erq->flags & IW_ENCODE_INDEX) - 1;
3499 int setindex = priv->tx_key;
3500 int enable = priv->wep_on;
3501 int restricted = priv->wep_restrict;
3502 u16 xlen = 0;
Christoph Hellwig620554e2005-06-19 01:27:33 +02003503 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 unsigned long flags;
3505
3506 if (! priv->has_wep)
3507 return -EOPNOTSUPP;
3508
3509 if (erq->pointer) {
3510 /* We actually have a key to set - check its length */
3511 if (erq->length > LARGE_KEY_SIZE)
3512 return -E2BIG;
3513
3514 if ( (erq->length > SMALL_KEY_SIZE) && !priv->has_big_wep )
3515 return -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 }
3517
3518 if (orinoco_lock(priv, &flags) != 0)
3519 return -EBUSY;
3520
Dan Williamsfe397d42006-07-14 11:41:47 -04003521 if (erq->length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
3523 index = priv->tx_key;
3524
3525 /* Adjust key length to a supported value */
3526 if (erq->length > SMALL_KEY_SIZE) {
3527 xlen = LARGE_KEY_SIZE;
3528 } else if (erq->length > 0) {
3529 xlen = SMALL_KEY_SIZE;
3530 } else
3531 xlen = 0;
3532
3533 /* Switch on WEP if off */
3534 if ((!enable) && (xlen > 0)) {
3535 setindex = index;
3536 enable = 1;
3537 }
3538 } else {
3539 /* Important note : if the user do "iwconfig eth0 enc off",
3540 * we will arrive there with an index of -1. This is valid
3541 * but need to be taken care off... Jean II */
3542 if ((index < 0) || (index >= ORINOCO_MAX_KEYS)) {
3543 if((index != -1) || (erq->flags == 0)) {
3544 err = -EINVAL;
3545 goto out;
3546 }
3547 } else {
3548 /* Set the index : Check that the key is valid */
3549 if(priv->keys[index].len == 0) {
3550 err = -EINVAL;
3551 goto out;
3552 }
3553 setindex = index;
3554 }
3555 }
3556
3557 if (erq->flags & IW_ENCODE_DISABLED)
3558 enable = 0;
3559 if (erq->flags & IW_ENCODE_OPEN)
3560 restricted = 0;
3561 if (erq->flags & IW_ENCODE_RESTRICTED)
3562 restricted = 1;
3563
Dan Williamsfe397d42006-07-14 11:41:47 -04003564 if (erq->pointer && erq->length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 priv->keys[index].len = cpu_to_le16(xlen);
3566 memset(priv->keys[index].data, 0,
3567 sizeof(priv->keys[index].data));
3568 memcpy(priv->keys[index].data, keybuf, erq->length);
3569 }
3570 priv->tx_key = setindex;
3571
3572 /* Try fast key change if connected and only keys are changed */
3573 if (priv->wep_on && enable && (priv->wep_restrict == restricted) &&
3574 netif_carrier_ok(dev)) {
3575 err = __orinoco_hw_setup_wepkeys(priv);
3576 /* No need to commit if successful */
3577 goto out;
3578 }
3579
3580 priv->wep_on = enable;
3581 priv->wep_restrict = restricted;
3582
3583 out:
3584 orinoco_unlock(priv, &flags);
3585
3586 return err;
3587}
3588
Christoph Hellwig620554e2005-06-19 01:27:33 +02003589static int orinoco_ioctl_getiwencode(struct net_device *dev,
3590 struct iw_request_info *info,
3591 struct iw_point *erq,
3592 char *keybuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593{
3594 struct orinoco_private *priv = netdev_priv(dev);
3595 int index = (erq->flags & IW_ENCODE_INDEX) - 1;
3596 u16 xlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 unsigned long flags;
3598
3599 if (! priv->has_wep)
3600 return -EOPNOTSUPP;
3601
3602 if (orinoco_lock(priv, &flags) != 0)
3603 return -EBUSY;
3604
3605 if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
3606 index = priv->tx_key;
3607
3608 erq->flags = 0;
3609 if (! priv->wep_on)
3610 erq->flags |= IW_ENCODE_DISABLED;
3611 erq->flags |= index + 1;
3612
3613 if (priv->wep_restrict)
3614 erq->flags |= IW_ENCODE_RESTRICTED;
3615 else
3616 erq->flags |= IW_ENCODE_OPEN;
3617
3618 xlen = le16_to_cpu(priv->keys[index].len);
3619
3620 erq->length = xlen;
3621
3622 memcpy(keybuf, priv->keys[index].data, ORINOCO_MAX_KEY_SIZE);
3623
3624 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 return 0;
3626}
3627
Christoph Hellwig620554e2005-06-19 01:27:33 +02003628static int orinoco_ioctl_setessid(struct net_device *dev,
3629 struct iw_request_info *info,
3630 struct iw_point *erq,
3631 char *essidbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632{
3633 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 unsigned long flags;
3635
3636 /* Note : ESSID is ignored in Ad-Hoc demo mode, but we can set it
3637 * anyway... - Jean II */
3638
Christoph Hellwig620554e2005-06-19 01:27:33 +02003639 /* Hum... Should not use Wireless Extension constant (may change),
3640 * should use our own... - Jean II */
3641 if (erq->length > IW_ESSID_MAX_SIZE)
3642 return -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643
3644 if (orinoco_lock(priv, &flags) != 0)
3645 return -EBUSY;
3646
Christoph Hellwig620554e2005-06-19 01:27:33 +02003647 /* NULL the string (for NULL termination & ESSID = ANY) - Jean II */
3648 memset(priv->desired_essid, 0, sizeof(priv->desired_essid));
3649
3650 /* If not ANY, get the new ESSID */
3651 if (erq->flags) {
3652 memcpy(priv->desired_essid, essidbuf, erq->length);
3653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654
3655 orinoco_unlock(priv, &flags);
3656
Christoph Hellwig620554e2005-06-19 01:27:33 +02003657 return -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658}
3659
Christoph Hellwig620554e2005-06-19 01:27:33 +02003660static int orinoco_ioctl_getessid(struct net_device *dev,
3661 struct iw_request_info *info,
3662 struct iw_point *erq,
3663 char *essidbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664{
3665 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 int active;
3667 int err = 0;
3668 unsigned long flags;
3669
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 if (netif_running(dev)) {
3671 err = orinoco_hw_get_essid(priv, &active, essidbuf);
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07003672 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 return err;
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07003674 erq->length = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 } else {
3676 if (orinoco_lock(priv, &flags) != 0)
3677 return -EBUSY;
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07003678 memcpy(essidbuf, priv->desired_essid, IW_ESSID_MAX_SIZE);
3679 erq->length = strlen(priv->desired_essid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 orinoco_unlock(priv, &flags);
3681 }
3682
3683 erq->flags = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 return 0;
3686}
3687
Christoph Hellwig620554e2005-06-19 01:27:33 +02003688static int orinoco_ioctl_setnick(struct net_device *dev,
3689 struct iw_request_info *info,
3690 struct iw_point *nrq,
3691 char *nickbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692{
3693 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 unsigned long flags;
3695
3696 if (nrq->length > IW_ESSID_MAX_SIZE)
3697 return -E2BIG;
3698
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 if (orinoco_lock(priv, &flags) != 0)
3700 return -EBUSY;
3701
Christoph Hellwig620554e2005-06-19 01:27:33 +02003702 memset(priv->nick, 0, sizeof(priv->nick));
3703 memcpy(priv->nick, nickbuf, nrq->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704
3705 orinoco_unlock(priv, &flags);
3706
Christoph Hellwig620554e2005-06-19 01:27:33 +02003707 return -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708}
3709
Christoph Hellwig620554e2005-06-19 01:27:33 +02003710static int orinoco_ioctl_getnick(struct net_device *dev,
3711 struct iw_request_info *info,
3712 struct iw_point *nrq,
3713 char *nickbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714{
3715 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 unsigned long flags;
3717
3718 if (orinoco_lock(priv, &flags) != 0)
3719 return -EBUSY;
3720
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07003721 memcpy(nickbuf, priv->nick, IW_ESSID_MAX_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 orinoco_unlock(priv, &flags);
3723
Jean Tourrilhes7e4e8d92006-10-10 14:45:44 -07003724 nrq->length = strlen(priv->nick);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 return 0;
3727}
3728
Christoph Hellwig620554e2005-06-19 01:27:33 +02003729static int orinoco_ioctl_setfreq(struct net_device *dev,
3730 struct iw_request_info *info,
3731 struct iw_freq *frq,
3732 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733{
3734 struct orinoco_private *priv = netdev_priv(dev);
3735 int chan = -1;
3736 unsigned long flags;
Christoph Hellwig620554e2005-06-19 01:27:33 +02003737 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02003739 /* In infrastructure mode the AP sets the channel */
3740 if (priv->iw_mode == IW_MODE_INFRA)
3741 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742
3743 if ( (frq->e == 0) && (frq->m <= 1000) ) {
3744 /* Setting by channel number */
3745 chan = frq->m;
3746 } else {
3747 /* Setting by frequency - search the table */
3748 int mult = 1;
3749 int i;
3750
3751 for (i = 0; i < (6 - frq->e); i++)
3752 mult *= 10;
3753
3754 for (i = 0; i < NUM_CHANNELS; i++)
3755 if (frq->m == (channel_frequency[i] * mult))
3756 chan = i+1;
3757 }
3758
3759 if ( (chan < 1) || (chan > NUM_CHANNELS) ||
3760 ! (priv->channel_mask & (1 << (chan-1)) ) )
3761 return -EINVAL;
3762
3763 if (orinoco_lock(priv, &flags) != 0)
3764 return -EBUSY;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02003765
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 priv->channel = chan;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02003767 if (priv->iw_mode == IW_MODE_MONITOR) {
3768 /* Fast channel change - no commit if successful */
3769 hermes_t *hw = &priv->hw;
3770 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
3771 HERMES_TEST_SET_CHANNEL,
3772 chan, NULL);
3773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 orinoco_unlock(priv, &flags);
3775
Christoph Hellwig620554e2005-06-19 01:27:33 +02003776 return err;
3777}
3778
3779static int orinoco_ioctl_getfreq(struct net_device *dev,
3780 struct iw_request_info *info,
3781 struct iw_freq *frq,
3782 char *extra)
3783{
3784 struct orinoco_private *priv = netdev_priv(dev);
3785 int tmp;
3786
3787 /* Locking done in there */
3788 tmp = orinoco_hw_get_freq(priv);
3789 if (tmp < 0) {
3790 return tmp;
3791 }
3792
3793 frq->m = tmp;
3794 frq->e = 1;
3795
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 return 0;
3797}
3798
Christoph Hellwig620554e2005-06-19 01:27:33 +02003799static int orinoco_ioctl_getsens(struct net_device *dev,
3800 struct iw_request_info *info,
3801 struct iw_param *srq,
3802 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803{
3804 struct orinoco_private *priv = netdev_priv(dev);
3805 hermes_t *hw = &priv->hw;
3806 u16 val;
3807 int err;
3808 unsigned long flags;
3809
3810 if (!priv->has_sensitivity)
3811 return -EOPNOTSUPP;
3812
3813 if (orinoco_lock(priv, &flags) != 0)
3814 return -EBUSY;
3815 err = hermes_read_wordrec(hw, USER_BAP,
3816 HERMES_RID_CNFSYSTEMSCALE, &val);
3817 orinoco_unlock(priv, &flags);
3818
3819 if (err)
3820 return err;
3821
3822 srq->value = val;
3823 srq->fixed = 0; /* auto */
3824
3825 return 0;
3826}
3827
Christoph Hellwig620554e2005-06-19 01:27:33 +02003828static int orinoco_ioctl_setsens(struct net_device *dev,
3829 struct iw_request_info *info,
3830 struct iw_param *srq,
3831 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832{
3833 struct orinoco_private *priv = netdev_priv(dev);
3834 int val = srq->value;
3835 unsigned long flags;
3836
3837 if (!priv->has_sensitivity)
3838 return -EOPNOTSUPP;
3839
3840 if ((val < 1) || (val > 3))
3841 return -EINVAL;
3842
3843 if (orinoco_lock(priv, &flags) != 0)
3844 return -EBUSY;
3845 priv->ap_density = val;
3846 orinoco_unlock(priv, &flags);
3847
Christoph Hellwig620554e2005-06-19 01:27:33 +02003848 return -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849}
3850
Christoph Hellwig620554e2005-06-19 01:27:33 +02003851static int orinoco_ioctl_setrts(struct net_device *dev,
3852 struct iw_request_info *info,
3853 struct iw_param *rrq,
3854 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855{
3856 struct orinoco_private *priv = netdev_priv(dev);
3857 int val = rrq->value;
3858 unsigned long flags;
3859
3860 if (rrq->disabled)
3861 val = 2347;
3862
3863 if ( (val < 0) || (val > 2347) )
3864 return -EINVAL;
3865
3866 if (orinoco_lock(priv, &flags) != 0)
3867 return -EBUSY;
3868
3869 priv->rts_thresh = val;
3870 orinoco_unlock(priv, &flags);
3871
Christoph Hellwig620554e2005-06-19 01:27:33 +02003872 return -EINPROGRESS; /* Call commit handler */
3873}
3874
3875static int orinoco_ioctl_getrts(struct net_device *dev,
3876 struct iw_request_info *info,
3877 struct iw_param *rrq,
3878 char *extra)
3879{
3880 struct orinoco_private *priv = netdev_priv(dev);
3881
3882 rrq->value = priv->rts_thresh;
3883 rrq->disabled = (rrq->value == 2347);
3884 rrq->fixed = 1;
3885
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 return 0;
3887}
3888
Christoph Hellwig620554e2005-06-19 01:27:33 +02003889static int orinoco_ioctl_setfrag(struct net_device *dev,
3890 struct iw_request_info *info,
3891 struct iw_param *frq,
3892 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893{
3894 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02003895 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 unsigned long flags;
3897
3898 if (orinoco_lock(priv, &flags) != 0)
3899 return -EBUSY;
3900
3901 if (priv->has_mwo) {
3902 if (frq->disabled)
3903 priv->mwo_robust = 0;
3904 else {
3905 if (frq->fixed)
3906 printk(KERN_WARNING "%s: Fixed fragmentation is "
3907 "not supported on this firmware. "
3908 "Using MWO robust instead.\n", dev->name);
3909 priv->mwo_robust = 1;
3910 }
3911 } else {
3912 if (frq->disabled)
3913 priv->frag_thresh = 2346;
3914 else {
3915 if ( (frq->value < 256) || (frq->value > 2346) )
3916 err = -EINVAL;
3917 else
3918 priv->frag_thresh = frq->value & ~0x1; /* must be even */
3919 }
3920 }
3921
3922 orinoco_unlock(priv, &flags);
3923
3924 return err;
3925}
3926
Christoph Hellwig620554e2005-06-19 01:27:33 +02003927static int orinoco_ioctl_getfrag(struct net_device *dev,
3928 struct iw_request_info *info,
3929 struct iw_param *frq,
3930 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931{
3932 struct orinoco_private *priv = netdev_priv(dev);
3933 hermes_t *hw = &priv->hw;
Christoph Hellwig620554e2005-06-19 01:27:33 +02003934 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 u16 val;
3936 unsigned long flags;
3937
3938 if (orinoco_lock(priv, &flags) != 0)
3939 return -EBUSY;
3940
3941 if (priv->has_mwo) {
3942 err = hermes_read_wordrec(hw, USER_BAP,
3943 HERMES_RID_CNFMWOROBUST_AGERE,
3944 &val);
3945 if (err)
3946 val = 0;
3947
3948 frq->value = val ? 2347 : 0;
3949 frq->disabled = ! val;
3950 frq->fixed = 0;
3951 } else {
3952 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
3953 &val);
3954 if (err)
3955 val = 0;
3956
3957 frq->value = val;
3958 frq->disabled = (val >= 2346);
3959 frq->fixed = 1;
3960 }
3961
3962 orinoco_unlock(priv, &flags);
3963
3964 return err;
3965}
3966
Christoph Hellwig620554e2005-06-19 01:27:33 +02003967static int orinoco_ioctl_setrate(struct net_device *dev,
3968 struct iw_request_info *info,
3969 struct iw_param *rrq,
3970 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971{
3972 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 int ratemode = -1;
3974 int bitrate; /* 100s of kilobits */
3975 int i;
3976 unsigned long flags;
3977
3978 /* As the user space doesn't know our highest rate, it uses -1
3979 * to ask us to set the highest rate. Test it using "iwconfig
3980 * ethX rate auto" - Jean II */
3981 if (rrq->value == -1)
3982 bitrate = 110;
3983 else {
3984 if (rrq->value % 100000)
3985 return -EINVAL;
3986 bitrate = rrq->value / 100000;
3987 }
3988
3989 if ( (bitrate != 10) && (bitrate != 20) &&
3990 (bitrate != 55) && (bitrate != 110) )
3991 return -EINVAL;
3992
3993 for (i = 0; i < BITRATE_TABLE_SIZE; i++)
3994 if ( (bitrate_table[i].bitrate == bitrate) &&
3995 (bitrate_table[i].automatic == ! rrq->fixed) ) {
3996 ratemode = i;
3997 break;
3998 }
3999
4000 if (ratemode == -1)
4001 return -EINVAL;
4002
4003 if (orinoco_lock(priv, &flags) != 0)
4004 return -EBUSY;
4005 priv->bitratemode = ratemode;
4006 orinoco_unlock(priv, &flags);
4007
Christoph Hellwig620554e2005-06-19 01:27:33 +02004008 return -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009}
4010
Christoph Hellwig620554e2005-06-19 01:27:33 +02004011static int orinoco_ioctl_getrate(struct net_device *dev,
4012 struct iw_request_info *info,
4013 struct iw_param *rrq,
4014 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015{
4016 struct orinoco_private *priv = netdev_priv(dev);
4017 hermes_t *hw = &priv->hw;
4018 int err = 0;
4019 int ratemode;
4020 int i;
4021 u16 val;
4022 unsigned long flags;
4023
4024 if (orinoco_lock(priv, &flags) != 0)
4025 return -EBUSY;
4026
4027 ratemode = priv->bitratemode;
4028
4029 BUG_ON((ratemode < 0) || (ratemode >= BITRATE_TABLE_SIZE));
4030
4031 rrq->value = bitrate_table[ratemode].bitrate * 100000;
4032 rrq->fixed = ! bitrate_table[ratemode].automatic;
4033 rrq->disabled = 0;
4034
4035 /* If the interface is running we try to find more about the
4036 current mode */
4037 if (netif_running(dev)) {
4038 err = hermes_read_wordrec(hw, USER_BAP,
4039 HERMES_RID_CURRENTTXRATE, &val);
4040 if (err)
4041 goto out;
4042
4043 switch (priv->firmware_type) {
4044 case FIRMWARE_TYPE_AGERE: /* Lucent style rate */
4045 /* Note : in Lucent firmware, the return value of
4046 * HERMES_RID_CURRENTTXRATE is the bitrate in Mb/s,
4047 * and therefore is totally different from the
4048 * encoding of HERMES_RID_CNFTXRATECONTROL.
4049 * Don't forget that 6Mb/s is really 5.5Mb/s */
4050 if (val == 6)
4051 rrq->value = 5500000;
4052 else
4053 rrq->value = val * 1000000;
4054 break;
4055 case FIRMWARE_TYPE_INTERSIL: /* Intersil style rate */
4056 case FIRMWARE_TYPE_SYMBOL: /* Symbol style rate */
4057 for (i = 0; i < BITRATE_TABLE_SIZE; i++)
4058 if (bitrate_table[i].intersil_txratectrl == val) {
4059 ratemode = i;
4060 break;
4061 }
4062 if (i >= BITRATE_TABLE_SIZE)
4063 printk(KERN_INFO "%s: Unable to determine current bitrate (0x%04hx)\n",
4064 dev->name, val);
4065
4066 rrq->value = bitrate_table[ratemode].bitrate * 100000;
4067 break;
4068 default:
4069 BUG();
4070 }
4071 }
4072
4073 out:
4074 orinoco_unlock(priv, &flags);
4075
4076 return err;
4077}
4078
Christoph Hellwig620554e2005-06-19 01:27:33 +02004079static int orinoco_ioctl_setpower(struct net_device *dev,
4080 struct iw_request_info *info,
4081 struct iw_param *prq,
4082 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083{
4084 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004085 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 unsigned long flags;
4087
4088 if (orinoco_lock(priv, &flags) != 0)
4089 return -EBUSY;
4090
4091 if (prq->disabled) {
4092 priv->pm_on = 0;
4093 } else {
4094 switch (prq->flags & IW_POWER_MODE) {
4095 case IW_POWER_UNICAST_R:
4096 priv->pm_mcast = 0;
4097 priv->pm_on = 1;
4098 break;
4099 case IW_POWER_ALL_R:
4100 priv->pm_mcast = 1;
4101 priv->pm_on = 1;
4102 break;
4103 case IW_POWER_ON:
4104 /* No flags : but we may have a value - Jean II */
4105 break;
4106 default:
4107 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 goto out;
Pavel Roskinc08ad1e2005-11-29 02:59:27 -05004109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110
4111 if (prq->flags & IW_POWER_TIMEOUT) {
4112 priv->pm_on = 1;
4113 priv->pm_timeout = prq->value / 1000;
4114 }
4115 if (prq->flags & IW_POWER_PERIOD) {
4116 priv->pm_on = 1;
4117 priv->pm_period = prq->value / 1000;
4118 }
4119 /* It's valid to not have a value if we are just toggling
4120 * the flags... Jean II */
4121 if(!priv->pm_on) {
4122 err = -EINVAL;
4123 goto out;
4124 }
4125 }
4126
4127 out:
4128 orinoco_unlock(priv, &flags);
4129
4130 return err;
4131}
4132
Christoph Hellwig620554e2005-06-19 01:27:33 +02004133static int orinoco_ioctl_getpower(struct net_device *dev,
4134 struct iw_request_info *info,
4135 struct iw_param *prq,
4136 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137{
4138 struct orinoco_private *priv = netdev_priv(dev);
4139 hermes_t *hw = &priv->hw;
4140 int err = 0;
4141 u16 enable, period, timeout, mcast;
4142 unsigned long flags;
4143
4144 if (orinoco_lock(priv, &flags) != 0)
4145 return -EBUSY;
4146
4147 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFPMENABLED, &enable);
4148 if (err)
4149 goto out;
4150
4151 err = hermes_read_wordrec(hw, USER_BAP,
4152 HERMES_RID_CNFMAXSLEEPDURATION, &period);
4153 if (err)
4154 goto out;
4155
4156 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFPMHOLDOVERDURATION, &timeout);
4157 if (err)
4158 goto out;
4159
4160 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFMULTICASTRECEIVE, &mcast);
4161 if (err)
4162 goto out;
4163
4164 prq->disabled = !enable;
4165 /* Note : by default, display the period */
4166 if ((prq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
4167 prq->flags = IW_POWER_TIMEOUT;
4168 prq->value = timeout * 1000;
4169 } else {
4170 prq->flags = IW_POWER_PERIOD;
4171 prq->value = period * 1000;
4172 }
4173 if (mcast)
4174 prq->flags |= IW_POWER_ALL_R;
4175 else
4176 prq->flags |= IW_POWER_UNICAST_R;
4177
4178 out:
4179 orinoco_unlock(priv, &flags);
4180
4181 return err;
4182}
4183
Christoph Hellwig620554e2005-06-19 01:27:33 +02004184static int orinoco_ioctl_getretry(struct net_device *dev,
4185 struct iw_request_info *info,
4186 struct iw_param *rrq,
4187 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188{
4189 struct orinoco_private *priv = netdev_priv(dev);
4190 hermes_t *hw = &priv->hw;
4191 int err = 0;
4192 u16 short_limit, long_limit, lifetime;
4193 unsigned long flags;
4194
4195 if (orinoco_lock(priv, &flags) != 0)
4196 return -EBUSY;
4197
4198 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_SHORTRETRYLIMIT,
4199 &short_limit);
4200 if (err)
4201 goto out;
4202
4203 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_LONGRETRYLIMIT,
4204 &long_limit);
4205 if (err)
4206 goto out;
4207
4208 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_MAXTRANSMITLIFETIME,
4209 &lifetime);
4210 if (err)
4211 goto out;
4212
4213 rrq->disabled = 0; /* Can't be disabled */
4214
4215 /* Note : by default, display the retry number */
4216 if ((rrq->flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME) {
4217 rrq->flags = IW_RETRY_LIFETIME;
4218 rrq->value = lifetime * 1000; /* ??? */
4219 } else {
4220 /* By default, display the min number */
Jean Tourrilheseeec9f12006-08-29 18:02:31 -07004221 if ((rrq->flags & IW_RETRY_LONG)) {
4222 rrq->flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 rrq->value = long_limit;
4224 } else {
4225 rrq->flags = IW_RETRY_LIMIT;
4226 rrq->value = short_limit;
4227 if(short_limit != long_limit)
Jean Tourrilheseeec9f12006-08-29 18:02:31 -07004228 rrq->flags |= IW_RETRY_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 }
4230 }
4231
4232 out:
4233 orinoco_unlock(priv, &flags);
4234
4235 return err;
4236}
4237
Christoph Hellwig620554e2005-06-19 01:27:33 +02004238static int orinoco_ioctl_reset(struct net_device *dev,
4239 struct iw_request_info *info,
4240 void *wrqu,
4241 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242{
4243 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004244
4245 if (! capable(CAP_NET_ADMIN))
4246 return -EPERM;
4247
4248 if (info->cmd == (SIOCIWFIRSTPRIV + 0x1)) {
4249 printk(KERN_DEBUG "%s: Forcing reset!\n", dev->name);
4250
4251 /* Firmware reset */
David Howellsc4028952006-11-22 14:57:56 +00004252 orinoco_reset(&priv->reset_work);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004253 } else {
4254 printk(KERN_DEBUG "%s: Force scheduling reset!\n", dev->name);
4255
4256 schedule_work(&priv->reset_work);
4257 }
4258
4259 return 0;
4260}
4261
4262static int orinoco_ioctl_setibssport(struct net_device *dev,
4263 struct iw_request_info *info,
4264 void *wrqu,
4265 char *extra)
4266
4267{
4268 struct orinoco_private *priv = netdev_priv(dev);
4269 int val = *( (int *) extra );
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 unsigned long flags;
4271
4272 if (orinoco_lock(priv, &flags) != 0)
4273 return -EBUSY;
4274
4275 priv->ibss_port = val ;
4276
4277 /* Actually update the mode we are using */
4278 set_port_type(priv);
4279
4280 orinoco_unlock(priv, &flags);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004281 return -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282}
4283
Christoph Hellwig620554e2005-06-19 01:27:33 +02004284static int orinoco_ioctl_getibssport(struct net_device *dev,
4285 struct iw_request_info *info,
4286 void *wrqu,
4287 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288{
4289 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004290 int *val = (int *) extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291
4292 *val = priv->ibss_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 return 0;
4294}
4295
Christoph Hellwig620554e2005-06-19 01:27:33 +02004296static int orinoco_ioctl_setport3(struct net_device *dev,
4297 struct iw_request_info *info,
4298 void *wrqu,
4299 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300{
4301 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004302 int val = *( (int *) extra );
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 int err = 0;
4304 unsigned long flags;
4305
4306 if (orinoco_lock(priv, &flags) != 0)
4307 return -EBUSY;
4308
4309 switch (val) {
4310 case 0: /* Try to do IEEE ad-hoc mode */
4311 if (! priv->has_ibss) {
4312 err = -EINVAL;
4313 break;
4314 }
4315 priv->prefer_port3 = 0;
4316
4317 break;
4318
4319 case 1: /* Try to do Lucent proprietary ad-hoc mode */
4320 if (! priv->has_port3) {
4321 err = -EINVAL;
4322 break;
4323 }
4324 priv->prefer_port3 = 1;
4325 break;
4326
4327 default:
4328 err = -EINVAL;
4329 }
4330
Christoph Hellwig620554e2005-06-19 01:27:33 +02004331 if (! err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 /* Actually update the mode we are using */
4333 set_port_type(priv);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004334 err = -EINPROGRESS;
4335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336
4337 orinoco_unlock(priv, &flags);
4338
4339 return err;
4340}
4341
Christoph Hellwig620554e2005-06-19 01:27:33 +02004342static int orinoco_ioctl_getport3(struct net_device *dev,
4343 struct iw_request_info *info,
4344 void *wrqu,
4345 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346{
4347 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004348 int *val = (int *) extra;
4349
4350 *val = priv->prefer_port3;
4351 return 0;
4352}
4353
4354static int orinoco_ioctl_setpreamble(struct net_device *dev,
4355 struct iw_request_info *info,
4356 void *wrqu,
4357 char *extra)
4358{
4359 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 unsigned long flags;
Christoph Hellwig620554e2005-06-19 01:27:33 +02004361 int val;
4362
4363 if (! priv->has_preamble)
4364 return -EOPNOTSUPP;
4365
4366 /* 802.11b has recently defined some short preamble.
4367 * Basically, the Phy header has been reduced in size.
4368 * This increase performance, especially at high rates
4369 * (the preamble is transmitted at 1Mb/s), unfortunately
4370 * this give compatibility troubles... - Jean II */
4371 val = *( (int *) extra );
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372
4373 if (orinoco_lock(priv, &flags) != 0)
4374 return -EBUSY;
4375
Christoph Hellwig620554e2005-06-19 01:27:33 +02004376 if (val)
4377 priv->preamble = 1;
4378 else
4379 priv->preamble = 0;
4380
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 orinoco_unlock(priv, &flags);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004382
4383 return -EINPROGRESS; /* Call commit handler */
4384}
4385
4386static int orinoco_ioctl_getpreamble(struct net_device *dev,
4387 struct iw_request_info *info,
4388 void *wrqu,
4389 char *extra)
4390{
4391 struct orinoco_private *priv = netdev_priv(dev);
4392 int *val = (int *) extra;
4393
4394 if (! priv->has_preamble)
4395 return -EOPNOTSUPP;
4396
4397 *val = priv->preamble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 return 0;
4399}
4400
Christoph Hellwig620554e2005-06-19 01:27:33 +02004401/* ioctl interface to hermes_read_ltv()
4402 * To use with iwpriv, pass the RID as the token argument, e.g.
4403 * iwpriv get_rid [0xfc00]
4404 * At least Wireless Tools 25 is required to use iwpriv.
4405 * For Wireless Tools 25 and 26 append "dummy" are the end. */
4406static int orinoco_ioctl_getrid(struct net_device *dev,
4407 struct iw_request_info *info,
4408 struct iw_point *data,
4409 char *extra)
4410{
4411 struct orinoco_private *priv = netdev_priv(dev);
4412 hermes_t *hw = &priv->hw;
4413 int rid = data->flags;
4414 u16 length;
4415 int err;
4416 unsigned long flags;
4417
4418 /* It's a "get" function, but we don't want users to access the
4419 * WEP key and other raw firmware data */
4420 if (! capable(CAP_NET_ADMIN))
4421 return -EPERM;
4422
4423 if (rid < 0xfc00 || rid > 0xffff)
4424 return -EINVAL;
4425
4426 if (orinoco_lock(priv, &flags) != 0)
4427 return -EBUSY;
4428
4429 err = hermes_read_ltv(hw, USER_BAP, rid, MAX_RID_LEN, &length,
4430 extra);
4431 if (err)
4432 goto out;
4433
4434 data->length = min_t(u16, HERMES_RECLEN_TO_BYTES(length),
4435 MAX_RID_LEN);
4436
4437 out:
4438 orinoco_unlock(priv, &flags);
4439 return err;
4440}
4441
David Kilroy17a1a882008-08-21 23:27:47 +01004442/* Trigger a scan (look for other cells in the vicinity) */
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004443static int orinoco_ioctl_setscan(struct net_device *dev,
4444 struct iw_request_info *info,
4445 struct iw_param *srq,
4446 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447{
4448 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004449 hermes_t *hw = &priv->hw;
David Kilroy0753bba2008-08-21 23:27:46 +01004450 struct iw_scan_req *si = (struct iw_scan_req *) extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 unsigned long flags;
4453
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004454 /* Note : you may have realised that, as this is a SET operation,
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -08004455 * this is privileged and therefore a normal user can't
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004456 * perform scanning.
4457 * This is not an error, while the device perform scanning,
4458 * traffic doesn't flow, so it's a perfect DoS...
4459 * Jean II */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004461 if (orinoco_lock(priv, &flags) != 0)
4462 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004464 /* Scanning with port 0 disabled would fail */
4465 if (!netif_running(dev)) {
4466 err = -ENETDOWN;
4467 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004470 /* In monitor mode, the scan results are always empty.
4471 * Probe responses are passed to the driver as received
4472 * frames and could be processed in software. */
4473 if (priv->iw_mode == IW_MODE_MONITOR) {
4474 err = -EOPNOTSUPP;
4475 goto out;
4476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004478 /* Note : because we don't lock out the irq handler, the way
4479 * we access scan variables in priv is critical.
4480 * o scan_inprogress : not touched by irq handler
4481 * o scan_mode : not touched by irq handler
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004482 * Before modifying anything on those variables, please think hard !
4483 * Jean II */
4484
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004485 /* Save flags */
4486 priv->scan_mode = srq->flags;
4487
4488 /* Always trigger scanning, even if it's in progress.
4489 * This way, if the info frame get lost, we will recover somewhat
4490 * gracefully - Jean II */
4491
4492 if (priv->has_hostscan) {
4493 switch (priv->firmware_type) {
4494 case FIRMWARE_TYPE_SYMBOL:
4495 err = hermes_write_wordrec(hw, USER_BAP,
4496 HERMES_RID_CNFHOSTSCAN_SYMBOL,
4497 HERMES_HOSTSCAN_SYMBOL_ONCE |
4498 HERMES_HOSTSCAN_SYMBOL_BCAST);
4499 break;
4500 case FIRMWARE_TYPE_INTERSIL: {
Pavel Roskind133ae42005-09-23 04:18:06 -04004501 __le16 req[3];
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004502
4503 req[0] = cpu_to_le16(0x3fff); /* All channels */
4504 req[1] = cpu_to_le16(0x0001); /* rate 1 Mbps */
4505 req[2] = 0; /* Any ESSID */
4506 err = HERMES_WRITE_RECORD(hw, USER_BAP,
4507 HERMES_RID_CNFHOSTSCAN, &req);
4508 }
4509 break;
4510 case FIRMWARE_TYPE_AGERE:
David Kilroy0753bba2008-08-21 23:27:46 +01004511 if (priv->scan_mode & IW_SCAN_THIS_ESSID) {
4512 struct hermes_idstring idbuf;
4513 size_t len = min(sizeof(idbuf.val),
4514 (size_t) si->essid_len);
4515 idbuf.len = cpu_to_le16(len);
4516 memcpy(idbuf.val, si->essid, len);
4517
4518 err = hermes_write_ltv(hw, USER_BAP,
4519 HERMES_RID_CNFSCANSSID_AGERE,
4520 HERMES_BYTES_TO_RECLEN(len + 2),
4521 &idbuf);
4522 } else
4523 err = hermes_write_wordrec(hw, USER_BAP,
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004524 HERMES_RID_CNFSCANSSID_AGERE,
4525 0); /* Any ESSID */
4526 if (err)
4527 break;
4528
David Kilroy01632fa2008-08-21 23:27:58 +01004529 if (priv->has_ext_scan) {
4530 /* Clear scan results at the start of
4531 * an extended scan */
4532 orinoco_clear_scan_results(priv,
4533 msecs_to_jiffies(15000));
4534
4535 /* TODO: Is this available on older firmware?
4536 * Can we use it to scan specific channels
4537 * for IW_SCAN_THIS_FREQ? */
4538 err = hermes_write_wordrec(hw, USER_BAP,
4539 HERMES_RID_CNFSCANCHANNELS2GHZ,
4540 0x7FFF);
4541 if (err)
4542 goto out;
4543
4544 err = hermes_inquire(hw,
4545 HERMES_INQ_CHANNELINFO);
4546 } else
4547 err = hermes_inquire(hw, HERMES_INQ_SCAN);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004548 break;
4549 }
4550 } else
4551 err = hermes_inquire(hw, HERMES_INQ_SCAN);
4552
4553 /* One more client */
4554 if (! err)
4555 priv->scan_inprogress = 1;
4556
4557 out:
4558 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 return err;
4560}
4561
Dan Williams1e3428e2007-10-10 23:56:25 -04004562#define MAX_CUSTOM_LEN 64
4563
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004564/* Translate scan data returned from the card to a card independant
David Kilroy17a1a882008-08-21 23:27:47 +01004565 * format that the Wireless Tools will understand - Jean II */
Dan Williams1e3428e2007-10-10 23:56:25 -04004566static inline char *orinoco_translate_scan(struct net_device *dev,
David S. Millerccc58052008-06-16 18:50:49 -07004567 struct iw_request_info *info,
Dan Williams1e3428e2007-10-10 23:56:25 -04004568 char *current_ev,
4569 char *end_buf,
4570 union hermes_scan_info *bss,
4571 unsigned int last_scanned)
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004572{
4573 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004574 u16 capabilities;
4575 u16 channel;
4576 struct iw_event iwe; /* Temporary buffer */
Dan Williams1e3428e2007-10-10 23:56:25 -04004577 char custom[MAX_CUSTOM_LEN];
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004578
David Kilroy17a1a882008-08-21 23:27:47 +01004579 memset(&iwe, 0, sizeof(iwe));
4580
Dan Williams1e3428e2007-10-10 23:56:25 -04004581 /* First entry *MUST* be the AP MAC address */
4582 iwe.cmd = SIOCGIWAP;
4583 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
4584 memcpy(iwe.u.ap_addr.sa_data, bss->a.bssid, ETH_ALEN);
David S. Millerccc58052008-06-16 18:50:49 -07004585 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
4586 &iwe, IW_EV_ADDR_LEN);
Dan Williams1e3428e2007-10-10 23:56:25 -04004587
4588 /* Other entries will be displayed in the order we give them */
4589
4590 /* Add the ESSID */
4591 iwe.u.data.length = le16_to_cpu(bss->a.essid_len);
4592 if (iwe.u.data.length > 32)
4593 iwe.u.data.length = 32;
4594 iwe.cmd = SIOCGIWESSID;
4595 iwe.u.data.flags = 1;
David S. Millerccc58052008-06-16 18:50:49 -07004596 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4597 &iwe, bss->a.essid);
Dan Williams1e3428e2007-10-10 23:56:25 -04004598
4599 /* Add mode */
4600 iwe.cmd = SIOCGIWMODE;
4601 capabilities = le16_to_cpu(bss->a.capabilities);
David Kilroy17a1a882008-08-21 23:27:47 +01004602 if (capabilities & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
4603 if (capabilities & WLAN_CAPABILITY_ESS)
Dan Williams1e3428e2007-10-10 23:56:25 -04004604 iwe.u.mode = IW_MODE_MASTER;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004605 else
Dan Williams1e3428e2007-10-10 23:56:25 -04004606 iwe.u.mode = IW_MODE_ADHOC;
David S. Millerccc58052008-06-16 18:50:49 -07004607 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
4608 &iwe, IW_EV_UINT_LEN);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004609 }
4610
Dan Williams1e3428e2007-10-10 23:56:25 -04004611 channel = bss->s.channel;
4612 if ((channel >= 1) && (channel <= NUM_CHANNELS)) {
David Kilroy17a1a882008-08-21 23:27:47 +01004613 /* Add channel and frequency */
Dan Williams1e3428e2007-10-10 23:56:25 -04004614 iwe.cmd = SIOCGIWFREQ;
David Kilroy17a1a882008-08-21 23:27:47 +01004615 iwe.u.freq.m = channel;
4616 iwe.u.freq.e = 0;
4617 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
4618 &iwe, IW_EV_FREQ_LEN);
4619
Dan Williams1e3428e2007-10-10 23:56:25 -04004620 iwe.u.freq.m = channel_frequency[channel-1] * 100000;
4621 iwe.u.freq.e = 1;
David S. Millerccc58052008-06-16 18:50:49 -07004622 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
Dan Williams1e3428e2007-10-10 23:56:25 -04004623 &iwe, IW_EV_FREQ_LEN);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004624 }
4625
David Kilroy17a1a882008-08-21 23:27:47 +01004626 /* Add quality statistics. level and noise in dB. No link quality */
Dan Williams1e3428e2007-10-10 23:56:25 -04004627 iwe.cmd = IWEVQUAL;
David Kilroy17a1a882008-08-21 23:27:47 +01004628 iwe.u.qual.updated = IW_QUAL_DBM | IW_QUAL_QUAL_INVALID;
Dan Williams1e3428e2007-10-10 23:56:25 -04004629 iwe.u.qual.level = (__u8) le16_to_cpu(bss->a.level) - 0x95;
4630 iwe.u.qual.noise = (__u8) le16_to_cpu(bss->a.noise) - 0x95;
4631 /* Wireless tools prior to 27.pre22 will show link quality
4632 * anyway, so we provide a reasonable value. */
4633 if (iwe.u.qual.level > iwe.u.qual.noise)
4634 iwe.u.qual.qual = iwe.u.qual.level - iwe.u.qual.noise;
4635 else
4636 iwe.u.qual.qual = 0;
David S. Millerccc58052008-06-16 18:50:49 -07004637 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
4638 &iwe, IW_EV_QUAL_LEN);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004639
Dan Williams1e3428e2007-10-10 23:56:25 -04004640 /* Add encryption capability */
4641 iwe.cmd = SIOCGIWENCODE;
David Kilroy17a1a882008-08-21 23:27:47 +01004642 if (capabilities & WLAN_CAPABILITY_PRIVACY)
Dan Williams1e3428e2007-10-10 23:56:25 -04004643 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
4644 else
4645 iwe.u.data.flags = IW_ENCODE_DISABLED;
4646 iwe.u.data.length = 0;
David S. Millerccc58052008-06-16 18:50:49 -07004647 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
David Kilroy17a1a882008-08-21 23:27:47 +01004648 &iwe, NULL);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004649
Dan Williams1e3428e2007-10-10 23:56:25 -04004650 /* Bit rate is not available in Lucent/Agere firmwares */
4651 if (priv->firmware_type != FIRMWARE_TYPE_AGERE) {
David S. Millerccc58052008-06-16 18:50:49 -07004652 char *current_val = current_ev + iwe_stream_lcp_len(info);
Dan Williams1e3428e2007-10-10 23:56:25 -04004653 int i;
4654 int step;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004655
Dan Williams1e3428e2007-10-10 23:56:25 -04004656 if (priv->firmware_type == FIRMWARE_TYPE_SYMBOL)
4657 step = 2;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004658 else
Dan Williams1e3428e2007-10-10 23:56:25 -04004659 step = 1;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004660
Dan Williams1e3428e2007-10-10 23:56:25 -04004661 iwe.cmd = SIOCGIWRATE;
4662 /* Those two flags are ignored... */
4663 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
4664 /* Max 10 values */
4665 for (i = 0; i < 10; i += step) {
4666 /* NULL terminated */
4667 if (bss->p.rates[i] == 0x0)
4668 break;
4669 /* Bit rate given in 500 kb/s units (+ 0x80) */
David Kilroy17a1a882008-08-21 23:27:47 +01004670 iwe.u.bitrate.value =
4671 ((bss->p.rates[i] & 0x7f) * 500000);
David S. Millerccc58052008-06-16 18:50:49 -07004672 current_val = iwe_stream_add_value(info, current_ev,
4673 current_val,
Dan Williams1e3428e2007-10-10 23:56:25 -04004674 end_buf, &iwe,
4675 IW_EV_PARAM_LEN);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004676 }
Dan Williams1e3428e2007-10-10 23:56:25 -04004677 /* Check if we added any event */
David S. Millerccc58052008-06-16 18:50:49 -07004678 if ((current_val - current_ev) > iwe_stream_lcp_len(info))
Dan Williams1e3428e2007-10-10 23:56:25 -04004679 current_ev = current_val;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004680 }
Dan Williams1e3428e2007-10-10 23:56:25 -04004681
David Kilroy17a1a882008-08-21 23:27:47 +01004682 /* Beacon interval */
4683 iwe.cmd = IWEVCUSTOM;
4684 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
4685 "bcn_int=%d",
4686 le16_to_cpu(bss->a.beacon_interv));
4687 if (iwe.u.data.length)
4688 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4689 &iwe, custom);
4690
4691 /* Capabilites */
4692 iwe.cmd = IWEVCUSTOM;
4693 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
4694 "capab=0x%04x",
4695 capabilities);
4696 if (iwe.u.data.length)
4697 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4698 &iwe, custom);
4699
4700 /* Add EXTRA: Age to display seconds since last beacon/probe response
4701 * for given network. */
4702 iwe.cmd = IWEVCUSTOM;
4703 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
4704 " Last beacon: %dms ago",
4705 jiffies_to_msecs(jiffies - last_scanned));
4706 if (iwe.u.data.length)
4707 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4708 &iwe, custom);
4709
Dan Williams1e3428e2007-10-10 23:56:25 -04004710 return current_ev;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004711}
4712
David Kilroy01632fa2008-08-21 23:27:58 +01004713static inline char *orinoco_translate_ext_scan(struct net_device *dev,
4714 struct iw_request_info *info,
4715 char *current_ev,
4716 char *end_buf,
4717 struct agere_ext_scan_info *bss,
4718 unsigned int last_scanned)
4719{
4720 u16 capabilities;
4721 u16 channel;
4722 struct iw_event iwe; /* Temporary buffer */
4723 char custom[MAX_CUSTOM_LEN];
4724 u8 *ie;
4725
4726 memset(&iwe, 0, sizeof(iwe));
4727
4728 /* First entry *MUST* be the AP MAC address */
4729 iwe.cmd = SIOCGIWAP;
4730 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
4731 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
4732 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
4733 &iwe, IW_EV_ADDR_LEN);
4734
4735 /* Other entries will be displayed in the order we give them */
4736
4737 /* Add the ESSID */
4738 ie = bss->data;
4739 iwe.u.data.length = ie[1];
4740 if (iwe.u.data.length) {
4741 if (iwe.u.data.length > 32)
4742 iwe.u.data.length = 32;
4743 iwe.cmd = SIOCGIWESSID;
4744 iwe.u.data.flags = 1;
4745 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4746 &iwe, &ie[2]);
4747 }
4748
4749 /* Add mode */
4750 capabilities = le16_to_cpu(bss->capabilities);
4751 if (capabilities & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
4752 iwe.cmd = SIOCGIWMODE;
4753 if (capabilities & WLAN_CAPABILITY_ESS)
4754 iwe.u.mode = IW_MODE_MASTER;
4755 else
4756 iwe.u.mode = IW_MODE_ADHOC;
4757 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
4758 &iwe, IW_EV_UINT_LEN);
4759 }
4760
4761 ie = orinoco_get_ie(bss->data, sizeof(bss->data), MFIE_TYPE_DS_SET);
4762 channel = ie ? ie[2] : 0;
4763 if ((channel >= 1) && (channel <= NUM_CHANNELS)) {
4764 /* Add channel and frequency */
4765 iwe.cmd = SIOCGIWFREQ;
4766 iwe.u.freq.m = channel;
4767 iwe.u.freq.e = 0;
4768 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
4769 &iwe, IW_EV_FREQ_LEN);
4770
4771 iwe.u.freq.m = channel_frequency[channel-1] * 100000;
4772 iwe.u.freq.e = 1;
4773 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
4774 &iwe, IW_EV_FREQ_LEN);
4775 }
4776
4777 /* Add quality statistics. level and noise in dB. No link quality */
4778 iwe.cmd = IWEVQUAL;
4779 iwe.u.qual.updated = IW_QUAL_DBM | IW_QUAL_QUAL_INVALID;
4780 iwe.u.qual.level = bss->level - 0x95;
4781 iwe.u.qual.noise = bss->noise - 0x95;
4782 /* Wireless tools prior to 27.pre22 will show link quality
4783 * anyway, so we provide a reasonable value. */
4784 if (iwe.u.qual.level > iwe.u.qual.noise)
4785 iwe.u.qual.qual = iwe.u.qual.level - iwe.u.qual.noise;
4786 else
4787 iwe.u.qual.qual = 0;
4788 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
4789 &iwe, IW_EV_QUAL_LEN);
4790
4791 /* Add encryption capability */
4792 iwe.cmd = SIOCGIWENCODE;
4793 if (capabilities & WLAN_CAPABILITY_PRIVACY)
4794 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
4795 else
4796 iwe.u.data.flags = IW_ENCODE_DISABLED;
4797 iwe.u.data.length = 0;
4798 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4799 &iwe, NULL);
4800
4801 /* WPA IE */
4802 ie = orinoco_get_wpa_ie(bss->data, sizeof(bss->data));
4803 if (ie) {
4804 iwe.cmd = IWEVGENIE;
4805 iwe.u.data.length = ie[1] + 2;
4806 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4807 &iwe, ie);
4808 }
4809
4810 /* RSN IE */
4811 ie = orinoco_get_ie(bss->data, sizeof(bss->data), MFIE_TYPE_RSN);
4812 if (ie) {
4813 iwe.cmd = IWEVGENIE;
4814 iwe.u.data.length = ie[1] + 2;
4815 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4816 &iwe, ie);
4817 }
4818
4819 ie = orinoco_get_ie(bss->data, sizeof(bss->data), MFIE_TYPE_RATES);
4820 if (ie) {
4821 char *p = current_ev + iwe_stream_lcp_len(info);
4822 int i;
4823
4824 iwe.cmd = SIOCGIWRATE;
4825 /* Those two flags are ignored... */
4826 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
4827
4828 for (i = 2; i < (ie[1] + 2); i++) {
4829 iwe.u.bitrate.value = ((ie[i] & 0x7F) * 500000);
4830 p = iwe_stream_add_value(info, current_ev, p, end_buf,
4831 &iwe, IW_EV_PARAM_LEN);
4832 }
4833 /* Check if we added any event */
4834 if (p > (current_ev + iwe_stream_lcp_len(info)))
4835 current_ev = p;
4836 }
4837
4838 /* Timestamp */
4839 iwe.cmd = IWEVCUSTOM;
4840 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
4841 "tsf=%016llx",
4842 le64_to_cpu(bss->timestamp));
4843 if (iwe.u.data.length)
4844 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4845 &iwe, custom);
4846
4847 /* Beacon interval */
4848 iwe.cmd = IWEVCUSTOM;
4849 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
4850 "bcn_int=%d",
4851 le16_to_cpu(bss->beacon_interval));
4852 if (iwe.u.data.length)
4853 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4854 &iwe, custom);
4855
4856 /* Capabilites */
4857 iwe.cmd = IWEVCUSTOM;
4858 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
4859 "capab=0x%04x",
4860 capabilities);
4861 if (iwe.u.data.length)
4862 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4863 &iwe, custom);
4864
4865 /* Add EXTRA: Age to display seconds since last beacon/probe response
4866 * for given network. */
4867 iwe.cmd = IWEVCUSTOM;
4868 iwe.u.data.length = snprintf(custom, MAX_CUSTOM_LEN,
4869 " Last beacon: %dms ago",
4870 jiffies_to_msecs(jiffies - last_scanned));
4871 if (iwe.u.data.length)
4872 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4873 &iwe, custom);
4874
4875 return current_ev;
4876}
4877
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004878/* Return results of a scan */
4879static int orinoco_ioctl_getscan(struct net_device *dev,
4880 struct iw_request_info *info,
4881 struct iw_point *srq,
4882 char *extra)
4883{
4884 struct orinoco_private *priv = netdev_priv(dev);
4885 int err = 0;
4886 unsigned long flags;
Dan Williams1e3428e2007-10-10 23:56:25 -04004887 char *current_ev = extra;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004888
4889 if (orinoco_lock(priv, &flags) != 0)
4890 return -EBUSY;
4891
Dan Williams1e3428e2007-10-10 23:56:25 -04004892 if (priv->scan_inprogress) {
4893 /* Important note : we don't want to block the caller
4894 * until results are ready for various reasons.
4895 * First, managing wait queues is complex and racy.
4896 * Second, we grab some rtnetlink lock before comming
4897 * here (in dev_ioctl()).
4898 * Third, we generate an Wireless Event, so the
4899 * caller can wait itself on that - Jean II */
4900 err = -EAGAIN;
4901 goto out;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004902 }
Dan Williams1e3428e2007-10-10 23:56:25 -04004903
David Kilroy01632fa2008-08-21 23:27:58 +01004904 if (priv->has_ext_scan) {
4905 struct xbss_element *bss;
Dan Williams1e3428e2007-10-10 23:56:25 -04004906
David Kilroy01632fa2008-08-21 23:27:58 +01004907 list_for_each_entry(bss, &priv->bss_list, list) {
4908 /* Translate this entry to WE format */
4909 current_ev =
4910 orinoco_translate_ext_scan(dev, info,
4911 current_ev,
4912 extra + srq->length,
4913 &bss->bss,
4914 bss->last_scanned);
4915
4916 /* Check if there is space for one more entry */
4917 if ((extra + srq->length - current_ev)
4918 <= IW_EV_ADDR_LEN) {
4919 /* Ask user space to try again with a
4920 * bigger buffer */
4921 err = -E2BIG;
4922 goto out;
4923 }
4924 }
4925
4926 } else {
4927 struct bss_element *bss;
4928
4929 list_for_each_entry(bss, &priv->bss_list, list) {
4930 /* Translate this entry to WE format */
4931 current_ev = orinoco_translate_scan(dev, info,
4932 current_ev,
4933 extra + srq->length,
4934 &bss->bss,
4935 bss->last_scanned);
4936
4937 /* Check if there is space for one more entry */
4938 if ((extra + srq->length - current_ev)
4939 <= IW_EV_ADDR_LEN) {
4940 /* Ask user space to try again with a
4941 * bigger buffer */
4942 err = -E2BIG;
4943 goto out;
4944 }
Dan Williams1e3428e2007-10-10 23:56:25 -04004945 }
4946 }
4947
4948 srq->length = (current_ev - extra);
4949 srq->flags = (__u16) priv->scan_mode;
4950
4951out:
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004952 orinoco_unlock(priv, &flags);
4953 return err;
4954}
4955
Christoph Hellwig620554e2005-06-19 01:27:33 +02004956/* Commit handler, called after set operations */
4957static int orinoco_ioctl_commit(struct net_device *dev,
4958 struct iw_request_info *info,
4959 void *wrqu,
4960 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961{
4962 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004963 struct hermes *hw = &priv->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 unsigned long flags;
Christoph Hellwig620554e2005-06-19 01:27:33 +02004965 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966
Christoph Hellwig620554e2005-06-19 01:27:33 +02004967 if (!priv->open)
4968 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969
Christoph Hellwig620554e2005-06-19 01:27:33 +02004970 if (priv->broken_disableport) {
David Howellsc4028952006-11-22 14:57:56 +00004971 orinoco_reset(&priv->reset_work);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004972 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974
Christoph Hellwig620554e2005-06-19 01:27:33 +02004975 if (orinoco_lock(priv, &flags) != 0)
4976 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
Christoph Hellwig620554e2005-06-19 01:27:33 +02004978 err = hermes_disable_port(hw, 0);
4979 if (err) {
4980 printk(KERN_WARNING "%s: Unable to disable port "
4981 "while reconfiguring card\n", dev->name);
4982 priv->broken_disableport = 1;
4983 goto out;
4984 }
4985
4986 err = __orinoco_program_rids(dev);
4987 if (err) {
4988 printk(KERN_WARNING "%s: Unable to reconfigure card\n",
4989 dev->name);
4990 goto out;
4991 }
4992
4993 err = hermes_enable_port(hw, 0);
4994 if (err) {
4995 printk(KERN_WARNING "%s: Unable to enable port while reconfiguring card\n",
4996 dev->name);
4997 goto out;
4998 }
4999
5000 out:
5001 if (err) {
5002 printk(KERN_WARNING "%s: Resetting instead...\n", dev->name);
5003 schedule_work(&priv->reset_work);
5004 err = 0;
5005 }
5006
5007 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 return err;
5009}
5010
Christoph Hellwig620554e2005-06-19 01:27:33 +02005011static const struct iw_priv_args orinoco_privtab[] = {
5012 { SIOCIWFIRSTPRIV + 0x0, 0, 0, "force_reset" },
5013 { SIOCIWFIRSTPRIV + 0x1, 0, 0, "card_reset" },
5014 { SIOCIWFIRSTPRIV + 0x2, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5015 0, "set_port3" },
5016 { SIOCIWFIRSTPRIV + 0x3, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5017 "get_port3" },
5018 { SIOCIWFIRSTPRIV + 0x4, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5019 0, "set_preamble" },
5020 { SIOCIWFIRSTPRIV + 0x5, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5021 "get_preamble" },
5022 { SIOCIWFIRSTPRIV + 0x6, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5023 0, "set_ibssport" },
5024 { SIOCIWFIRSTPRIV + 0x7, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
5025 "get_ibssport" },
5026 { SIOCIWFIRSTPRIV + 0x9, 0, IW_PRIV_TYPE_BYTE | MAX_RID_LEN,
5027 "get_rid" },
5028};
5029
5030
5031/*
5032 * Structures to export the Wireless Handlers
5033 */
5034
5035static const iw_handler orinoco_handler[] = {
Peter Hagervall6b9b97c2005-07-27 01:14:46 -07005036 [SIOCSIWCOMMIT-SIOCIWFIRST] = (iw_handler) orinoco_ioctl_commit,
5037 [SIOCGIWNAME -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getname,
5038 [SIOCSIWFREQ -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setfreq,
5039 [SIOCGIWFREQ -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getfreq,
5040 [SIOCSIWMODE -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setmode,
5041 [SIOCGIWMODE -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getmode,
5042 [SIOCSIWSENS -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setsens,
5043 [SIOCGIWSENS -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getsens,
5044 [SIOCGIWRANGE -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getiwrange,
Pavel Roskin343c6862005-09-09 18:43:02 -04005045 [SIOCSIWSPY -SIOCIWFIRST] = (iw_handler) iw_handler_set_spy,
5046 [SIOCGIWSPY -SIOCIWFIRST] = (iw_handler) iw_handler_get_spy,
5047 [SIOCSIWTHRSPY-SIOCIWFIRST] = (iw_handler) iw_handler_set_thrspy,
5048 [SIOCGIWTHRSPY-SIOCIWFIRST] = (iw_handler) iw_handler_get_thrspy,
Peter Hagervall6b9b97c2005-07-27 01:14:46 -07005049 [SIOCSIWAP -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setwap,
5050 [SIOCGIWAP -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getwap,
5051 [SIOCSIWSCAN -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setscan,
5052 [SIOCGIWSCAN -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getscan,
5053 [SIOCSIWESSID -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setessid,
5054 [SIOCGIWESSID -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getessid,
5055 [SIOCSIWNICKN -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setnick,
5056 [SIOCGIWNICKN -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getnick,
5057 [SIOCSIWRATE -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setrate,
5058 [SIOCGIWRATE -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getrate,
5059 [SIOCSIWRTS -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setrts,
5060 [SIOCGIWRTS -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getrts,
5061 [SIOCSIWFRAG -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setfrag,
5062 [SIOCGIWFRAG -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getfrag,
5063 [SIOCGIWRETRY -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getretry,
5064 [SIOCSIWENCODE-SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setiwencode,
5065 [SIOCGIWENCODE-SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getiwencode,
5066 [SIOCSIWPOWER -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setpower,
5067 [SIOCGIWPOWER -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getpower,
Christoph Hellwig620554e2005-06-19 01:27:33 +02005068};
5069
5070
5071/*
5072 Added typecasting since we no longer use iwreq_data -- Moustafa
5073 */
5074static const iw_handler orinoco_private_handler[] = {
Peter Hagervall6b9b97c2005-07-27 01:14:46 -07005075 [0] = (iw_handler) orinoco_ioctl_reset,
5076 [1] = (iw_handler) orinoco_ioctl_reset,
5077 [2] = (iw_handler) orinoco_ioctl_setport3,
5078 [3] = (iw_handler) orinoco_ioctl_getport3,
5079 [4] = (iw_handler) orinoco_ioctl_setpreamble,
5080 [5] = (iw_handler) orinoco_ioctl_getpreamble,
5081 [6] = (iw_handler) orinoco_ioctl_setibssport,
5082 [7] = (iw_handler) orinoco_ioctl_getibssport,
5083 [9] = (iw_handler) orinoco_ioctl_getrid,
Christoph Hellwig620554e2005-06-19 01:27:33 +02005084};
5085
5086static const struct iw_handler_def orinoco_handler_def = {
5087 .num_standard = ARRAY_SIZE(orinoco_handler),
5088 .num_private = ARRAY_SIZE(orinoco_private_handler),
5089 .num_private_args = ARRAY_SIZE(orinoco_privtab),
5090 .standard = orinoco_handler,
5091 .private = orinoco_private_handler,
5092 .private_args = orinoco_privtab,
Pavel Roskin343c6862005-09-09 18:43:02 -04005093 .get_wireless_stats = orinoco_get_wireless_stats,
Christoph Hellwig620554e2005-06-19 01:27:33 +02005094};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02005096static void orinoco_get_drvinfo(struct net_device *dev,
5097 struct ethtool_drvinfo *info)
5098{
5099 struct orinoco_private *priv = netdev_priv(dev);
5100
5101 strncpy(info->driver, DRIVER_NAME, sizeof(info->driver) - 1);
5102 strncpy(info->version, DRIVER_VERSION, sizeof(info->version) - 1);
5103 strncpy(info->fw_version, priv->fw_name, sizeof(info->fw_version) - 1);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07005104 if (dev->dev.parent)
5105 strncpy(info->bus_info, dev->dev.parent->bus_id,
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02005106 sizeof(info->bus_info) - 1);
5107 else
5108 snprintf(info->bus_info, sizeof(info->bus_info) - 1,
5109 "PCMCIA %p", priv->hw.iobase);
5110}
5111
Jeff Garzik7282d492006-09-13 14:30:00 -04005112static const struct ethtool_ops orinoco_ethtool_ops = {
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02005113 .get_drvinfo = orinoco_get_drvinfo,
5114 .get_link = ethtool_op_get_link,
5115};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116
5117/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118/* Module initialization */
5119/********************************************************************/
5120
5121EXPORT_SYMBOL(alloc_orinocodev);
5122EXPORT_SYMBOL(free_orinocodev);
5123
5124EXPORT_SYMBOL(__orinoco_up);
5125EXPORT_SYMBOL(__orinoco_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126EXPORT_SYMBOL(orinoco_reinit_firmware);
5127
5128EXPORT_SYMBOL(orinoco_interrupt);
5129
5130/* Can't be declared "const" or the whole __initdata section will
5131 * become const */
5132static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
5133 " (David Gibson <hermes@gibson.dropbear.id.au>, "
5134 "Pavel Roskin <proski@gnu.org>, et al)";
5135
5136static int __init init_orinoco(void)
5137{
5138 printk(KERN_DEBUG "%s\n", version);
5139 return 0;
5140}
5141
5142static void __exit exit_orinoco(void)
5143{
5144}
5145
5146module_init(init_orinoco);
5147module_exit(exit_orinoco);