blob: 9fde17bebebf6117b73bce57860e1c9f02fc43ac [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
79#include <linux/config.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/module.h>
81#include <linux/kernel.h>
82#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include <linux/etherdevice.h>
Christoph Hellwig1fab2e82005-06-19 01:27:40 +020085#include <linux/ethtool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#include <linux/wireless.h>
Christoph Hellwig620554e2005-06-19 01:27:33 +020087#include <net/iw_handler.h>
Christoph Hellwig5d558b72005-06-19 01:27:28 +020088#include <net/ieee80211.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#include "hermes_rid.h"
91#include "orinoco.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93/********************************************************************/
94/* Module information */
95/********************************************************************/
96
97MODULE_AUTHOR("Pavel Roskin <proski@gnu.org> & David Gibson <hermes@gibson.dropbear.id.au>");
98MODULE_DESCRIPTION("Driver for Lucent Orinoco, Prism II based and similar wireless cards");
99MODULE_LICENSE("Dual MPL/GPL");
100
101/* Level of debugging. Used in the macros in orinoco.h */
102#ifdef ORINOCO_DEBUG
103int orinoco_debug = ORINOCO_DEBUG;
104module_param(orinoco_debug, int, 0644);
105MODULE_PARM_DESC(orinoco_debug, "Debug level");
106EXPORT_SYMBOL(orinoco_debug);
107#endif
108
109static int suppress_linkstatus; /* = 0 */
110module_param(suppress_linkstatus, bool, 0644);
111MODULE_PARM_DESC(suppress_linkstatus, "Don't log link status changes");
David Gibson7bb7c3a2005-05-12 20:02:10 -0400112static int ignore_disconnect; /* = 0 */
113module_param(ignore_disconnect, int, 0644);
114MODULE_PARM_DESC(ignore_disconnect, "Don't report lost link to the network layer");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200116static int force_monitor; /* = 0 */
117module_param(force_monitor, int, 0644);
118MODULE_PARM_DESC(force_monitor, "Allow monitor mode for all firmware versions");
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/********************************************************************/
121/* Compile time configuration and compatibility stuff */
122/********************************************************************/
123
124/* We do this this way to avoid ifdefs in the actual code */
125#ifdef WIRELESS_SPY
Pavel Roskin343c6862005-09-09 18:43:02 -0400126#define SPY_NUMBER(priv) (priv->spy_data.spy_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#else
128#define SPY_NUMBER(priv) 0
129#endif /* WIRELESS_SPY */
130
131/********************************************************************/
132/* Internal constants */
133/********************************************************************/
134
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200135/* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
136static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
137#define ENCAPS_OVERHEAD (sizeof(encaps_hdr) + 2)
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#define ORINOCO_MIN_MTU 256
Jeff Garzikb4538722005-05-12 22:48:20 -0400140#define ORINOCO_MAX_MTU (IEEE80211_DATA_LEN - ENCAPS_OVERHEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142#define SYMBOL_MAX_VER_LEN (14)
143#define USER_BAP 0
144#define IRQ_BAP 1
145#define MAX_IRQLOOPS_PER_IRQ 10
146#define MAX_IRQLOOPS_PER_JIFFY (20000/HZ) /* Based on a guestimate of
147 * how many events the
148 * device could
149 * legitimately generate */
150#define SMALL_KEY_SIZE 5
151#define LARGE_KEY_SIZE 13
152#define TX_NICBUF_SIZE_BUG 1585 /* Bug in Symbol firmware */
153
154#define DUMMY_FID 0xFFFF
155
156/*#define MAX_MULTICAST(priv) (priv->firmware_type == FIRMWARE_TYPE_AGERE ? \
157 HERMES_MAX_MULTICAST : 0)*/
158#define MAX_MULTICAST(priv) (HERMES_MAX_MULTICAST)
159
160#define ORINOCO_INTEN (HERMES_EV_RX | HERMES_EV_ALLOC \
161 | HERMES_EV_TX | HERMES_EV_TXEXC \
162 | HERMES_EV_WTERR | HERMES_EV_INFO \
163 | HERMES_EV_INFDROP )
164
Christoph Hellwig620554e2005-06-19 01:27:33 +0200165#define MAX_RID_LEN 1024
166
167static const struct iw_handler_def orinoco_handler_def;
Christoph Hellwig1fab2e82005-06-19 01:27:40 +0200168static struct ethtool_ops orinoco_ethtool_ops;
Christoph Hellwig620554e2005-06-19 01:27:33 +0200169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/********************************************************************/
171/* Data tables */
172/********************************************************************/
173
174/* The frequency of each channel in MHz */
175static const long channel_frequency[] = {
176 2412, 2417, 2422, 2427, 2432, 2437, 2442,
177 2447, 2452, 2457, 2462, 2467, 2472, 2484
178};
179#define NUM_CHANNELS ARRAY_SIZE(channel_frequency)
180
181/* This tables gives the actual meanings of the bitrate IDs returned
182 * by the firmware. */
183static struct {
184 int bitrate; /* in 100s of kilobits */
185 int automatic;
186 u16 agere_txratectrl;
187 u16 intersil_txratectrl;
188} bitrate_table[] = {
189 {110, 1, 3, 15}, /* Entry 0 is the default */
190 {10, 0, 1, 1},
191 {10, 1, 1, 1},
192 {20, 0, 2, 2},
193 {20, 1, 6, 3},
194 {55, 0, 4, 4},
195 {55, 1, 7, 7},
196 {110, 0, 5, 8},
197};
198#define BITRATE_TABLE_SIZE ARRAY_SIZE(bitrate_table)
199
200/********************************************************************/
201/* Data types */
202/********************************************************************/
203
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400204/* Beginning of the Tx descriptor, used in TxExc handling */
205struct hermes_txexc_data {
206 struct hermes_tx_descriptor desc;
Pavel Roskind133ae42005-09-23 04:18:06 -0400207 __le16 frame_ctl;
208 __le16 duration_id;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200209 u8 addr1[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210} __attribute__ ((packed));
211
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200212/* Rx frame header except compatibility 802.3 header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213struct hermes_rx_descriptor {
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200214 /* Control */
Pavel Roskind133ae42005-09-23 04:18:06 -0400215 __le16 status;
216 __le32 time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 u8 silence;
218 u8 signal;
219 u8 rate;
220 u8 rxflow;
Pavel Roskind133ae42005-09-23 04:18:06 -0400221 __le32 reserved;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200222
223 /* 802.11 header */
Pavel Roskind133ae42005-09-23 04:18:06 -0400224 __le16 frame_ctl;
225 __le16 duration_id;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200226 u8 addr1[ETH_ALEN];
227 u8 addr2[ETH_ALEN];
228 u8 addr3[ETH_ALEN];
Pavel Roskind133ae42005-09-23 04:18:06 -0400229 __le16 seq_ctl;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200230 u8 addr4[ETH_ALEN];
231
232 /* Data length */
Pavel Roskind133ae42005-09-23 04:18:06 -0400233 __le16 data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234} __attribute__ ((packed));
235
236/********************************************************************/
237/* Function prototypes */
238/********************************************************************/
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240static int __orinoco_program_rids(struct net_device *dev);
241static void __orinoco_set_multicast_list(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243/********************************************************************/
244/* Internal helper functions */
245/********************************************************************/
246
247static inline void set_port_type(struct orinoco_private *priv)
248{
249 switch (priv->iw_mode) {
250 case IW_MODE_INFRA:
251 priv->port_type = 1;
252 priv->createibss = 0;
253 break;
254 case IW_MODE_ADHOC:
255 if (priv->prefer_port3) {
256 priv->port_type = 3;
257 priv->createibss = 0;
258 } else {
259 priv->port_type = priv->ibss_port;
260 priv->createibss = 1;
261 }
262 break;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200263 case IW_MODE_MONITOR:
264 priv->port_type = 3;
265 priv->createibss = 0;
266 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 default:
268 printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
269 priv->ndev->name);
270 }
271}
272
273/********************************************************************/
274/* Device methods */
275/********************************************************************/
276
277static int orinoco_open(struct net_device *dev)
278{
279 struct orinoco_private *priv = netdev_priv(dev);
280 unsigned long flags;
281 int err;
282
283 if (orinoco_lock(priv, &flags) != 0)
284 return -EBUSY;
285
286 err = __orinoco_up(dev);
287
288 if (! err)
289 priv->open = 1;
290
291 orinoco_unlock(priv, &flags);
292
293 return err;
294}
295
Christoph Hellwigad8f4512005-05-14 17:30:17 +0200296static int orinoco_stop(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 struct orinoco_private *priv = netdev_priv(dev);
299 int err = 0;
300
301 /* We mustn't use orinoco_lock() here, because we need to be
302 able to close the interface even if hw_unavailable is set
303 (e.g. as we're released after a PC Card removal) */
304 spin_lock_irq(&priv->lock);
305
306 priv->open = 0;
307
308 err = __orinoco_down(dev);
309
310 spin_unlock_irq(&priv->lock);
311
312 return err;
313}
314
315static struct net_device_stats *orinoco_get_stats(struct net_device *dev)
316{
317 struct orinoco_private *priv = netdev_priv(dev);
318
319 return &priv->stats;
320}
321
322static struct iw_statistics *orinoco_get_wireless_stats(struct net_device *dev)
323{
324 struct orinoco_private *priv = netdev_priv(dev);
325 hermes_t *hw = &priv->hw;
326 struct iw_statistics *wstats = &priv->wstats;
David Gibsone67d9d92005-05-12 20:01:22 -0400327 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 unsigned long flags;
329
330 if (! netif_device_present(dev)) {
331 printk(KERN_WARNING "%s: get_wireless_stats() called while device not present\n",
332 dev->name);
333 return NULL; /* FIXME: Can we do better than this? */
334 }
335
David Gibsone67d9d92005-05-12 20:01:22 -0400336 /* If busy, return the old stats. Returning NULL may cause
337 * the interface to disappear from /proc/net/wireless */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (orinoco_lock(priv, &flags) != 0)
David Gibsone67d9d92005-05-12 20:01:22 -0400339 return wstats;
340
341 /* We can't really wait for the tallies inquiry command to
342 * complete, so we just use the previous results and trigger
343 * a new tallies inquiry command for next time - Jean II */
344 /* FIXME: Really we should wait for the inquiry to come back -
345 * as it is the stats we give don't make a whole lot of sense.
346 * Unfortunately, it's not clear how to do that within the
347 * wireless extensions framework: I think we're in user
348 * context, but a lock seems to be held by the time we get in
349 * here so we're not safe to sleep here. */
350 hermes_inquire(hw, HERMES_INQ_TALLIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 if (priv->iw_mode == IW_MODE_ADHOC) {
353 memset(&wstats->qual, 0, sizeof(wstats->qual));
354 /* If a spy address is defined, we report stats of the
355 * first spy address - Jean II */
356 if (SPY_NUMBER(priv)) {
Pavel Roskin343c6862005-09-09 18:43:02 -0400357 wstats->qual.qual = priv->spy_data.spy_stat[0].qual;
358 wstats->qual.level = priv->spy_data.spy_stat[0].level;
359 wstats->qual.noise = priv->spy_data.spy_stat[0].noise;
360 wstats->qual.updated = priv->spy_data.spy_stat[0].updated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362 } else {
363 struct {
Pavel Roskina208c4e2006-04-07 04:10:26 -0400364 __le16 qual, signal, noise, unused;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 } __attribute__ ((packed)) cq;
366
367 err = HERMES_READ_RECORD(hw, USER_BAP,
368 HERMES_RID_COMMSQUALITY, &cq);
David Gibsone67d9d92005-05-12 20:01:22 -0400369
370 if (!err) {
371 wstats->qual.qual = (int)le16_to_cpu(cq.qual);
372 wstats->qual.level = (int)le16_to_cpu(cq.signal) - 0x95;
373 wstats->qual.noise = (int)le16_to_cpu(cq.noise) - 0x95;
374 wstats->qual.updated = 7;
375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return wstats;
380}
381
382static void orinoco_set_multicast_list(struct net_device *dev)
383{
384 struct orinoco_private *priv = netdev_priv(dev);
385 unsigned long flags;
386
387 if (orinoco_lock(priv, &flags) != 0) {
388 printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
389 "called when hw_unavailable\n", dev->name);
390 return;
391 }
392
393 __orinoco_set_multicast_list(dev);
394 orinoco_unlock(priv, &flags);
395}
396
397static int orinoco_change_mtu(struct net_device *dev, int new_mtu)
398{
399 struct orinoco_private *priv = netdev_priv(dev);
400
401 if ( (new_mtu < ORINOCO_MIN_MTU) || (new_mtu > ORINOCO_MAX_MTU) )
402 return -EINVAL;
403
Jeff Garzikb4538722005-05-12 22:48:20 -0400404 if ( (new_mtu + ENCAPS_OVERHEAD + IEEE80211_HLEN) >
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 (priv->nicbuf_size - ETH_HLEN) )
406 return -EINVAL;
407
408 dev->mtu = new_mtu;
409
410 return 0;
411}
412
413/********************************************************************/
414/* Tx path */
415/********************************************************************/
416
417static int orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
418{
419 struct orinoco_private *priv = netdev_priv(dev);
420 struct net_device_stats *stats = &priv->stats;
421 hermes_t *hw = &priv->hw;
422 int err = 0;
423 u16 txfid = priv->txfid;
424 char *p;
425 struct ethhdr *eh;
Pavel Roskin8d5be082006-04-07 04:10:41 -0400426 int data_len, data_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 struct hermes_tx_descriptor desc;
428 unsigned long flags;
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (! netif_running(dev)) {
431 printk(KERN_ERR "%s: Tx on stopped device!\n",
432 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400433 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435
436 if (netif_queue_stopped(dev)) {
437 printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
438 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400439 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
442 if (orinoco_lock(priv, &flags) != 0) {
443 printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n",
444 dev->name);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400445 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
447
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200448 if (! netif_carrier_ok(dev) || (priv->iw_mode == IW_MODE_MONITOR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /* Oops, the firmware hasn't established a connection,
450 silently drop the packet (this seems to be the
451 safest approach). */
452 stats->tx_errors++;
453 orinoco_unlock(priv, &flags);
454 dev_kfree_skb(skb);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400455 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457
Pavel Roskin8d5be082006-04-07 04:10:41 -0400458 /* Check packet length */
459 data_len = skb->len;
460 if (data_len < ETH_HLEN)
John W. Linville36841c92005-10-18 21:30:58 -0400461 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 eh = (struct ethhdr *)skb->data;
464
465 memset(&desc, 0, sizeof(desc));
466 desc.tx_control = cpu_to_le16(HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX);
467 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc), txfid, 0);
468 if (err) {
469 if (net_ratelimit())
470 printk(KERN_ERR "%s: Error %d writing Tx descriptor "
471 "to BAP\n", dev->name, err);
472 stats->tx_errors++;
473 goto fail;
474 }
475
476 /* Clear the 802.11 header and data length fields - some
477 * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
478 * if this isn't done. */
479 hermes_clear_words(hw, HERMES_DATA0,
480 HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
481
482 /* Encapsulate Ethernet-II frames */
483 if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
484 struct header_struct hdr;
Pavel Roskin8d5be082006-04-07 04:10:41 -0400485 data_len = skb->len - ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 data_off = HERMES_802_3_OFFSET + sizeof(hdr);
487 p = skb->data + ETH_HLEN;
488
489 /* 802.3 header */
490 memcpy(hdr.dest, eh->h_dest, ETH_ALEN);
491 memcpy(hdr.src, eh->h_source, ETH_ALEN);
492 hdr.len = htons(data_len + ENCAPS_OVERHEAD);
493
494 /* 802.2 header */
495 memcpy(&hdr.dsap, &encaps_hdr, sizeof(encaps_hdr));
496
497 hdr.ethertype = eh->h_proto;
498 err = hermes_bap_pwrite(hw, USER_BAP, &hdr, sizeof(hdr),
499 txfid, HERMES_802_3_OFFSET);
500 if (err) {
501 if (net_ratelimit())
502 printk(KERN_ERR "%s: Error %d writing packet "
503 "header to BAP\n", dev->name, err);
504 stats->tx_errors++;
505 goto fail;
506 }
507 } else { /* IEEE 802.3 frame */
Pavel Roskin8d5be082006-04-07 04:10:41 -0400508 data_len = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 data_off = HERMES_802_3_OFFSET;
510 p = skb->data;
511 }
512
Pavel Roskin8d5be082006-04-07 04:10:41 -0400513 err = hermes_bap_pwrite(hw, USER_BAP, p, data_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 txfid, data_off);
515 if (err) {
516 printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
517 dev->name, err);
518 stats->tx_errors++;
519 goto fail;
520 }
521
522 /* Finally, we actually initiate the send */
523 netif_stop_queue(dev);
524
525 err = hermes_docmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
526 txfid, NULL);
527 if (err) {
528 netif_start_queue(dev);
Andrew Mortonc367c212005-10-19 21:23:44 -0700529 if (net_ratelimit())
530 printk(KERN_ERR "%s: Error %d transmitting packet\n",
531 dev->name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 stats->tx_errors++;
533 goto fail;
534 }
535
536 dev->trans_start = jiffies;
537 stats->tx_bytes += data_off + data_len;
538
539 orinoco_unlock(priv, &flags);
540
541 dev_kfree_skb(skb);
542
Pavel Roskinb34b8672006-04-07 04:10:36 -0400543 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 orinoco_unlock(priv, &flags);
Pavel Roskinb34b8672006-04-07 04:10:36 -0400547 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550static void __orinoco_ev_alloc(struct net_device *dev, hermes_t *hw)
551{
552 struct orinoco_private *priv = netdev_priv(dev);
553 u16 fid = hermes_read_regn(hw, ALLOCFID);
554
555 if (fid != priv->txfid) {
556 if (fid != DUMMY_FID)
557 printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
558 dev->name, fid);
559 return;
560 }
561
562 hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
563}
564
565static void __orinoco_ev_tx(struct net_device *dev, hermes_t *hw)
566{
567 struct orinoco_private *priv = netdev_priv(dev);
568 struct net_device_stats *stats = &priv->stats;
569
570 stats->tx_packets++;
571
572 netif_wake_queue(dev);
573
574 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
575}
576
577static void __orinoco_ev_txexc(struct net_device *dev, hermes_t *hw)
578{
579 struct orinoco_private *priv = netdev_priv(dev);
580 struct net_device_stats *stats = &priv->stats;
581 u16 fid = hermes_read_regn(hw, TXCOMPLFID);
Pavel Roskind133ae42005-09-23 04:18:06 -0400582 u16 status;
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400583 struct hermes_txexc_data hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 int err = 0;
585
586 if (fid == DUMMY_FID)
587 return; /* Nothing's really happened */
588
Pavel Roskin48ca7032005-09-23 04:18:06 -0400589 /* Read part of the frame header - we need status and addr1 */
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200590 err = hermes_bap_pread(hw, IRQ_BAP, &hdr,
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400591 sizeof(struct hermes_txexc_data),
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200592 fid, 0);
593
594 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
595 stats->tx_errors++;
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (err) {
598 printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
599 "(FID=%04X error %d)\n",
600 dev->name, fid, err);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200601 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200604 DEBUG(1, "%s: Tx error, err %d (FID=%04X)\n", dev->name,
605 err, fid);
606
607 /* We produce a TXDROP event only for retry or lifetime
608 * exceeded, because that's the only status that really mean
609 * that this particular node went away.
610 * Other errors means that *we* screwed up. - Jean II */
Pavel Roskin30c2d3b2006-04-07 04:10:34 -0400611 status = le16_to_cpu(hdr.desc.status);
Pavel Roskind133ae42005-09-23 04:18:06 -0400612 if (status & (HERMES_TXSTAT_RETRYERR | HERMES_TXSTAT_AGEDERR)) {
Christoph Hellwig95dd91f2005-06-19 01:27:56 +0200613 union iwreq_data wrqu;
614
615 /* Copy 802.11 dest address.
616 * We use the 802.11 header because the frame may
617 * not be 802.3 or may be mangled...
618 * In Ad-Hoc mode, it will be the node address.
619 * In managed mode, it will be most likely the AP addr
620 * User space will figure out how to convert it to
621 * whatever it needs (IP address or else).
622 * - Jean II */
623 memcpy(wrqu.addr.sa_data, hdr.addr1, ETH_ALEN);
624 wrqu.addr.sa_family = ARPHRD_ETHER;
625
626 /* Send event to user space */
627 wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
633static void orinoco_tx_timeout(struct net_device *dev)
634{
635 struct orinoco_private *priv = netdev_priv(dev);
636 struct net_device_stats *stats = &priv->stats;
637 struct hermes *hw = &priv->hw;
638
639 printk(KERN_WARNING "%s: Tx timeout! "
640 "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
641 dev->name, hermes_read_regn(hw, ALLOCFID),
642 hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
643
644 stats->tx_errors++;
645
646 schedule_work(&priv->reset_work);
647}
648
649/********************************************************************/
650/* Rx path (data frames) */
651/********************************************************************/
652
653/* Does the frame have a SNAP header indicating it should be
654 * de-encapsulated to Ethernet-II? */
655static inline int is_ethersnap(void *_hdr)
656{
657 u8 *hdr = _hdr;
658
659 /* We de-encapsulate all packets which, a) have SNAP headers
660 * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
661 * and where b) the OUI of the SNAP header is 00:00:00 or
662 * 00:00:f8 - we need both because different APs appear to use
663 * different OUIs for some reason */
664 return (memcmp(hdr, &encaps_hdr, 5) == 0)
665 && ( (hdr[5] == 0x00) || (hdr[5] == 0xf8) );
666}
667
668static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
669 int level, int noise)
670{
Pavel Roskin343c6862005-09-09 18:43:02 -0400671 struct iw_quality wstats;
672 wstats.level = level - 0x95;
673 wstats.noise = noise - 0x95;
674 wstats.qual = (level > noise) ? (level - noise) : 0;
675 wstats.updated = 7;
676 /* Update spy records */
677 wireless_spy_update(dev, mac, &wstats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
680static void orinoco_stat_gather(struct net_device *dev,
681 struct sk_buff *skb,
682 struct hermes_rx_descriptor *desc)
683{
684 struct orinoco_private *priv = netdev_priv(dev);
685
686 /* Using spy support with lots of Rx packets, like in an
687 * infrastructure (AP), will really slow down everything, because
688 * the MAC address must be compared to each entry of the spy list.
689 * If the user really asks for it (set some address in the
690 * spy list), we do it, but he will pay the price.
691 * Note that to get here, you need both WIRELESS_SPY
692 * compiled in AND some addresses in the list !!!
693 */
694 /* Note : gcc will optimise the whole section away if
695 * WIRELESS_SPY is not defined... - Jean II */
696 if (SPY_NUMBER(priv)) {
697 orinoco_spy_gather(dev, skb->mac.raw + ETH_ALEN,
698 desc->signal, desc->silence);
699 }
700}
701
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200702/*
703 * orinoco_rx_monitor - handle received monitor frames.
704 *
705 * Arguments:
706 * dev network device
707 * rxfid received FID
708 * desc rx descriptor of the frame
709 *
710 * Call context: interrupt
711 */
712static void orinoco_rx_monitor(struct net_device *dev, u16 rxfid,
713 struct hermes_rx_descriptor *desc)
714{
715 u32 hdrlen = 30; /* return full header by default */
716 u32 datalen = 0;
717 u16 fc;
718 int err;
719 int len;
720 struct sk_buff *skb;
721 struct orinoco_private *priv = netdev_priv(dev);
722 struct net_device_stats *stats = &priv->stats;
723 hermes_t *hw = &priv->hw;
724
725 len = le16_to_cpu(desc->data_len);
726
727 /* Determine the size of the header and the data */
728 fc = le16_to_cpu(desc->frame_ctl);
729 switch (fc & IEEE80211_FCTL_FTYPE) {
730 case IEEE80211_FTYPE_DATA:
731 if ((fc & IEEE80211_FCTL_TODS)
732 && (fc & IEEE80211_FCTL_FROMDS))
733 hdrlen = 30;
734 else
735 hdrlen = 24;
736 datalen = len;
737 break;
738 case IEEE80211_FTYPE_MGMT:
739 hdrlen = 24;
740 datalen = len;
741 break;
742 case IEEE80211_FTYPE_CTL:
743 switch (fc & IEEE80211_FCTL_STYPE) {
744 case IEEE80211_STYPE_PSPOLL:
745 case IEEE80211_STYPE_RTS:
746 case IEEE80211_STYPE_CFEND:
747 case IEEE80211_STYPE_CFENDACK:
748 hdrlen = 16;
749 break;
750 case IEEE80211_STYPE_CTS:
751 case IEEE80211_STYPE_ACK:
752 hdrlen = 10;
753 break;
754 }
755 break;
756 default:
757 /* Unknown frame type */
758 break;
759 }
760
761 /* sanity check the length */
762 if (datalen > IEEE80211_DATA_LEN + 12) {
763 printk(KERN_DEBUG "%s: oversized monitor frame, "
764 "data length = %d\n", dev->name, datalen);
765 err = -EIO;
766 stats->rx_length_errors++;
767 goto update_stats;
768 }
769
770 skb = dev_alloc_skb(hdrlen + datalen);
771 if (!skb) {
772 printk(KERN_WARNING "%s: Cannot allocate skb for monitor frame\n",
773 dev->name);
774 err = -ENOMEM;
775 goto drop;
776 }
777
778 /* Copy the 802.11 header to the skb */
779 memcpy(skb_put(skb, hdrlen), &(desc->frame_ctl), hdrlen);
780 skb->mac.raw = skb->data;
781
782 /* If any, copy the data from the card to the skb */
783 if (datalen > 0) {
784 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, datalen),
785 ALIGN(datalen, 2), rxfid,
786 HERMES_802_2_OFFSET);
787 if (err) {
788 printk(KERN_ERR "%s: error %d reading monitor frame\n",
789 dev->name, err);
790 goto drop;
791 }
792 }
793
794 skb->dev = dev;
795 skb->ip_summed = CHECKSUM_NONE;
796 skb->pkt_type = PACKET_OTHERHOST;
797 skb->protocol = __constant_htons(ETH_P_802_2);
798
799 dev->last_rx = jiffies;
800 stats->rx_packets++;
801 stats->rx_bytes += skb->len;
802
803 netif_rx(skb);
804 return;
805
806 drop:
807 dev_kfree_skb_irq(skb);
808 update_stats:
809 stats->rx_errors++;
810 stats->rx_dropped++;
811}
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813static void __orinoco_ev_rx(struct net_device *dev, hermes_t *hw)
814{
815 struct orinoco_private *priv = netdev_priv(dev);
816 struct net_device_stats *stats = &priv->stats;
817 struct iw_statistics *wstats = &priv->wstats;
818 struct sk_buff *skb = NULL;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200819 u16 rxfid, status, fc;
820 int length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 struct hermes_rx_descriptor desc;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200822 struct ethhdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 int err;
824
825 rxfid = hermes_read_regn(hw, RXFID);
826
827 err = hermes_bap_pread(hw, IRQ_BAP, &desc, sizeof(desc),
828 rxfid, 0);
829 if (err) {
830 printk(KERN_ERR "%s: error %d reading Rx descriptor. "
831 "Frame dropped.\n", dev->name, err);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200832 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834
835 status = le16_to_cpu(desc.status);
836
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200837 if (status & HERMES_RXSTAT_BADCRC) {
838 DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n",
839 dev->name);
840 stats->rx_crc_errors++;
841 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
843
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200844 /* Handle frames in monitor mode */
845 if (priv->iw_mode == IW_MODE_MONITOR) {
846 orinoco_rx_monitor(dev, rxfid, &desc);
847 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200850 if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
851 DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
852 dev->name);
853 wstats->discard.code++;
854 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200857 length = le16_to_cpu(desc.data_len);
858 fc = le16_to_cpu(desc.frame_ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 /* Sanity checks */
861 if (length < 3) { /* No for even an 802.2 LLC header */
862 /* At least on Symbol firmware with PCF we get quite a
863 lot of these legitimately - Poll frames with no
864 data. */
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200865 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
Jeff Garzikb4538722005-05-12 22:48:20 -0400867 if (length > IEEE80211_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
869 dev->name, length);
870 stats->rx_length_errors++;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200871 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
873
874 /* We need space for the packet data itself, plus an ethernet
875 header, plus 2 bytes so we can align the IP header on a
876 32bit boundary, plus 1 byte so we can read in odd length
877 packets from the card, which has an IO granularity of 16
878 bits */
879 skb = dev_alloc_skb(length+ETH_HLEN+2+1);
880 if (!skb) {
881 printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
882 dev->name);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200883 goto update_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200886 /* We'll prepend the header, so reserve space for it. The worst
887 case is no decapsulation, when 802.3 header is prepended and
888 nothing is removed. 2 is for aligning the IP header. */
889 skb_reserve(skb, ETH_HLEN + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200891 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, length),
892 ALIGN(length, 2), rxfid,
893 HERMES_802_2_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if (err) {
895 printk(KERN_ERR "%s: error %d reading frame. "
896 "Frame dropped.\n", dev->name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 goto drop;
898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 /* Handle decapsulation
901 * In most cases, the firmware tell us about SNAP frames.
902 * For some reason, the SNAP frames sent by LinkSys APs
903 * are not properly recognised by most firmwares.
904 * So, check ourselves */
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200905 if (length >= ENCAPS_OVERHEAD &&
906 (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
907 ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
908 is_ethersnap(skb->data))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 /* These indicate a SNAP within 802.2 LLC within
910 802.11 frame which we'll need to de-encapsulate to
911 the original EthernetII frame. */
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200912 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN - ENCAPS_OVERHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 } else {
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200914 /* 802.3 frame - prepend 802.3 header as is */
915 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN);
916 hdr->h_proto = htons(length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200918 memcpy(hdr->h_dest, desc.addr1, ETH_ALEN);
919 if (fc & IEEE80211_FCTL_FROMDS)
920 memcpy(hdr->h_source, desc.addr3, ETH_ALEN);
921 else
922 memcpy(hdr->h_source, desc.addr2, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 dev->last_rx = jiffies;
925 skb->dev = dev;
926 skb->protocol = eth_type_trans(skb, dev);
927 skb->ip_summed = CHECKSUM_NONE;
Christoph Hellwig8f2abf42005-06-19 01:28:02 +0200928 if (fc & IEEE80211_FCTL_TODS)
929 skb->pkt_type = PACKET_OTHERHOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 /* Process the wireless stats if needed */
932 orinoco_stat_gather(dev, skb, &desc);
933
934 /* Pass the packet to the networking stack */
935 netif_rx(skb);
936 stats->rx_packets++;
937 stats->rx_bytes += length;
938
939 return;
940
941 drop:
Christoph Hellwig98c4cae2005-06-19 01:28:06 +0200942 dev_kfree_skb_irq(skb);
943 update_stats:
944 stats->rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 stats->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
947
948/********************************************************************/
949/* Rx path (info frames) */
950/********************************************************************/
951
952static void print_linkstatus(struct net_device *dev, u16 status)
953{
954 char * s;
955
956 if (suppress_linkstatus)
957 return;
958
959 switch (status) {
960 case HERMES_LINKSTATUS_NOT_CONNECTED:
961 s = "Not Connected";
962 break;
963 case HERMES_LINKSTATUS_CONNECTED:
964 s = "Connected";
965 break;
966 case HERMES_LINKSTATUS_DISCONNECTED:
967 s = "Disconnected";
968 break;
969 case HERMES_LINKSTATUS_AP_CHANGE:
970 s = "AP Changed";
971 break;
972 case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
973 s = "AP Out of Range";
974 break;
975 case HERMES_LINKSTATUS_AP_IN_RANGE:
976 s = "AP In Range";
977 break;
978 case HERMES_LINKSTATUS_ASSOC_FAILED:
979 s = "Association Failed";
980 break;
981 default:
982 s = "UNKNOWN";
983 }
984
985 printk(KERN_INFO "%s: New link status: %s (%04x)\n",
986 dev->name, s, status);
987}
988
Christoph Hellwig16739b02005-06-19 01:27:51 +0200989/* Search scan results for requested BSSID, join it if found */
990static void orinoco_join_ap(struct net_device *dev)
991{
992 struct orinoco_private *priv = netdev_priv(dev);
993 struct hermes *hw = &priv->hw;
994 int err;
995 unsigned long flags;
996 struct join_req {
997 u8 bssid[ETH_ALEN];
Pavel Roskind133ae42005-09-23 04:18:06 -0400998 __le16 channel;
Christoph Hellwig16739b02005-06-19 01:27:51 +0200999 } __attribute__ ((packed)) req;
1000 const int atom_len = offsetof(struct prism2_scan_apinfo, atim);
Pavel Roskinc89cc222005-09-01 20:06:06 -04001001 struct prism2_scan_apinfo *atom = NULL;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001002 int offset = 4;
Pavel Roskinc89cc222005-09-01 20:06:06 -04001003 int found = 0;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001004 u8 *buf;
1005 u16 len;
1006
1007 /* Allocate buffer for scan results */
1008 buf = kmalloc(MAX_SCAN_LEN, GFP_KERNEL);
1009 if (! buf)
1010 return;
1011
1012 if (orinoco_lock(priv, &flags) != 0)
Pavel Roskinf3cb4cc2005-09-23 04:18:06 -04001013 goto fail_lock;
Christoph Hellwig16739b02005-06-19 01:27:51 +02001014
1015 /* Sanity checks in case user changed something in the meantime */
1016 if (! priv->bssid_fixed)
1017 goto out;
1018
1019 if (strlen(priv->desired_essid) == 0)
1020 goto out;
1021
1022 /* Read scan results from the firmware */
1023 err = hermes_read_ltv(hw, USER_BAP,
1024 HERMES_RID_SCANRESULTSTABLE,
1025 MAX_SCAN_LEN, &len, buf);
1026 if (err) {
1027 printk(KERN_ERR "%s: Cannot read scan results\n",
1028 dev->name);
1029 goto out;
1030 }
1031
1032 len = HERMES_RECLEN_TO_BYTES(len);
1033
1034 /* Go through the scan results looking for the channel of the AP
1035 * we were requested to join */
1036 for (; offset + atom_len <= len; offset += atom_len) {
1037 atom = (struct prism2_scan_apinfo *) (buf + offset);
Pavel Roskinc89cc222005-09-01 20:06:06 -04001038 if (memcmp(&atom->bssid, priv->desired_bssid, ETH_ALEN) == 0) {
1039 found = 1;
1040 break;
1041 }
Christoph Hellwig16739b02005-06-19 01:27:51 +02001042 }
1043
Pavel Roskinc89cc222005-09-01 20:06:06 -04001044 if (! found) {
1045 DEBUG(1, "%s: Requested AP not found in scan results\n",
1046 dev->name);
1047 goto out;
1048 }
Christoph Hellwig16739b02005-06-19 01:27:51 +02001049
Christoph Hellwig16739b02005-06-19 01:27:51 +02001050 memcpy(req.bssid, priv->desired_bssid, ETH_ALEN);
1051 req.channel = atom->channel; /* both are little-endian */
1052 err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNFJOINREQUEST,
1053 &req);
1054 if (err)
1055 printk(KERN_ERR "%s: Error issuing join request\n", dev->name);
1056
1057 out:
Christoph Hellwig16739b02005-06-19 01:27:51 +02001058 orinoco_unlock(priv, &flags);
Pavel Roskinf3cb4cc2005-09-23 04:18:06 -04001059
1060 fail_lock:
1061 kfree(buf);
Christoph Hellwig16739b02005-06-19 01:27:51 +02001062}
1063
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001064/* Send new BSSID to userspace */
1065static void orinoco_send_wevents(struct net_device *dev)
1066{
1067 struct orinoco_private *priv = netdev_priv(dev);
1068 struct hermes *hw = &priv->hw;
1069 union iwreq_data wrqu;
1070 int err;
1071 unsigned long flags;
1072
1073 if (orinoco_lock(priv, &flags) != 0)
1074 return;
1075
1076 err = hermes_read_ltv(hw, IRQ_BAP, HERMES_RID_CURRENTBSSID,
1077 ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
1078 if (err != 0)
Pavel Roskin8aeabc32005-09-23 04:18:06 -04001079 goto out;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001080
1081 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1082
1083 /* Send event to user space */
1084 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Pavel Roskin8aeabc32005-09-23 04:18:06 -04001085
1086 out:
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001087 orinoco_unlock(priv, &flags);
1088}
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090static void __orinoco_ev_info(struct net_device *dev, hermes_t *hw)
1091{
1092 struct orinoco_private *priv = netdev_priv(dev);
1093 u16 infofid;
1094 struct {
Pavel Roskind133ae42005-09-23 04:18:06 -04001095 __le16 len;
1096 __le16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 } __attribute__ ((packed)) info;
1098 int len, type;
1099 int err;
1100
1101 /* This is an answer to an INQUIRE command that we did earlier,
1102 * or an information "event" generated by the card
1103 * The controller return to us a pseudo frame containing
1104 * the information in question - Jean II */
1105 infofid = hermes_read_regn(hw, INFOFID);
1106
1107 /* Read the info frame header - don't try too hard */
1108 err = hermes_bap_pread(hw, IRQ_BAP, &info, sizeof(info),
1109 infofid, 0);
1110 if (err) {
1111 printk(KERN_ERR "%s: error %d reading info frame. "
1112 "Frame dropped.\n", dev->name, err);
1113 return;
1114 }
1115
1116 len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
1117 type = le16_to_cpu(info.type);
1118
1119 switch (type) {
1120 case HERMES_INQ_TALLIES: {
1121 struct hermes_tallies_frame tallies;
1122 struct iw_statistics *wstats = &priv->wstats;
1123
1124 if (len > sizeof(tallies)) {
1125 printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
1126 dev->name, len);
1127 len = sizeof(tallies);
1128 }
1129
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001130 err = hermes_bap_pread(hw, IRQ_BAP, &tallies, len,
1131 infofid, sizeof(info));
1132 if (err)
1133 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 /* Increment our various counters */
1136 /* wstats->discard.nwid - no wrong BSSID stuff */
1137 wstats->discard.code +=
1138 le16_to_cpu(tallies.RxWEPUndecryptable);
1139 if (len == sizeof(tallies))
1140 wstats->discard.code +=
1141 le16_to_cpu(tallies.RxDiscards_WEPICVError) +
1142 le16_to_cpu(tallies.RxDiscards_WEPExcluded);
1143 wstats->discard.misc +=
1144 le16_to_cpu(tallies.TxDiscardsWrongSA);
1145 wstats->discard.fragment +=
1146 le16_to_cpu(tallies.RxMsgInBadMsgFragments);
1147 wstats->discard.retries +=
1148 le16_to_cpu(tallies.TxRetryLimitExceeded);
1149 /* wstats->miss.beacon - no match */
1150 }
1151 break;
1152 case HERMES_INQ_LINKSTATUS: {
1153 struct hermes_linkstatus linkstatus;
1154 u16 newstatus;
1155 int connected;
1156
Christoph Hellwig8f2abf42005-06-19 01:28:02 +02001157 if (priv->iw_mode == IW_MODE_MONITOR)
1158 break;
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (len != sizeof(linkstatus)) {
1161 printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
1162 dev->name, len);
1163 break;
1164 }
1165
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001166 err = hermes_bap_pread(hw, IRQ_BAP, &linkstatus, len,
1167 infofid, sizeof(info));
1168 if (err)
1169 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 newstatus = le16_to_cpu(linkstatus.linkstatus);
1171
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001172 /* Symbol firmware uses "out of range" to signal that
1173 * the hostscan frame can be requested. */
1174 if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
1175 priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
1176 priv->has_hostscan && priv->scan_inprogress) {
1177 hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
1178 break;
1179 }
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
1182 || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
1183 || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
1184
1185 if (connected)
1186 netif_carrier_on(dev);
David Gibson7bb7c3a2005-05-12 20:02:10 -04001187 else if (!ignore_disconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 netif_carrier_off(dev);
1189
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001190 if (newstatus != priv->last_linkstatus) {
1191 priv->last_linkstatus = newstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 print_linkstatus(dev, newstatus);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001193 /* The info frame contains only one word which is the
1194 * status (see hermes.h). The status is pretty boring
1195 * in itself, that's why we export the new BSSID...
1196 * Jean II */
1197 schedule_work(&priv->wevent_work);
1198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
1200 break;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001201 case HERMES_INQ_SCAN:
1202 if (!priv->scan_inprogress && priv->bssid_fixed &&
1203 priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
1204 schedule_work(&priv->join_work);
1205 break;
1206 }
1207 /* fall through */
1208 case HERMES_INQ_HOSTSCAN:
1209 case HERMES_INQ_HOSTSCAN_SYMBOL: {
1210 /* Result of a scanning. Contains information about
1211 * cells in the vicinity - Jean II */
1212 union iwreq_data wrqu;
1213 unsigned char *buf;
1214
1215 /* Sanity check */
1216 if (len > 4096) {
1217 printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
1218 dev->name, len);
1219 break;
1220 }
1221
1222 /* We are a strict producer. If the previous scan results
1223 * have not been consumed, we just have to drop this
1224 * frame. We can't remove the previous results ourselves,
1225 * that would be *very* racy... Jean II */
1226 if (priv->scan_result != NULL) {
1227 printk(KERN_WARNING "%s: Previous scan results not consumed, dropping info frame.\n", dev->name);
1228 break;
1229 }
1230
1231 /* Allocate buffer for results */
1232 buf = kmalloc(len, GFP_ATOMIC);
1233 if (buf == NULL)
1234 /* No memory, so can't printk()... */
1235 break;
1236
1237 /* Read scan data */
1238 err = hermes_bap_pread(hw, IRQ_BAP, (void *) buf, len,
1239 infofid, sizeof(info));
Pavel Roskin708218b2005-09-01 20:05:19 -04001240 if (err) {
1241 kfree(buf);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001242 break;
Pavel Roskin708218b2005-09-01 20:05:19 -04001243 }
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001244
1245#ifdef ORINOCO_DEBUG
1246 {
1247 int i;
1248 printk(KERN_DEBUG "Scan result [%02X", buf[0]);
1249 for(i = 1; i < (len * 2); i++)
1250 printk(":%02X", buf[i]);
1251 printk("]\n");
1252 }
1253#endif /* ORINOCO_DEBUG */
1254
1255 /* Allow the clients to access the results */
1256 priv->scan_len = len;
1257 priv->scan_result = buf;
1258
1259 /* Send an empty event to user space.
1260 * We don't send the received data on the event because
1261 * it would require us to do complex transcoding, and
1262 * we want to minimise the work done in the irq handler
1263 * Use a request to extract the data - Jean II */
1264 wrqu.data.length = 0;
1265 wrqu.data.flags = 0;
1266 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
1267 }
1268 break;
1269 case HERMES_INQ_SEC_STAT_AGERE:
1270 /* Security status (Agere specific) */
1271 /* Ignore this frame for now */
1272 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
1273 break;
1274 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 default:
1276 printk(KERN_DEBUG "%s: Unknown information frame received: "
1277 "type 0x%04x, length %d\n", dev->name, type, len);
1278 /* We don't actually do anything about it */
1279 break;
1280 }
1281}
1282
1283static void __orinoco_ev_infdrop(struct net_device *dev, hermes_t *hw)
1284{
1285 if (net_ratelimit())
1286 printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
1287}
1288
1289/********************************************************************/
1290/* Internal hardware control routines */
1291/********************************************************************/
1292
1293int __orinoco_up(struct net_device *dev)
1294{
1295 struct orinoco_private *priv = netdev_priv(dev);
1296 struct hermes *hw = &priv->hw;
1297 int err;
1298
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02001299 netif_carrier_off(dev); /* just to make sure */
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 err = __orinoco_program_rids(dev);
1302 if (err) {
1303 printk(KERN_ERR "%s: Error %d configuring card\n",
1304 dev->name, err);
1305 return err;
1306 }
1307
1308 /* Fire things up again */
1309 hermes_set_irqmask(hw, ORINOCO_INTEN);
1310 err = hermes_enable_port(hw, 0);
1311 if (err) {
1312 printk(KERN_ERR "%s: Error %d enabling MAC port\n",
1313 dev->name, err);
1314 return err;
1315 }
1316
1317 netif_start_queue(dev);
1318
1319 return 0;
1320}
1321
1322int __orinoco_down(struct net_device *dev)
1323{
1324 struct orinoco_private *priv = netdev_priv(dev);
1325 struct hermes *hw = &priv->hw;
1326 int err;
1327
1328 netif_stop_queue(dev);
1329
1330 if (! priv->hw_unavailable) {
1331 if (! priv->broken_disableport) {
1332 err = hermes_disable_port(hw, 0);
1333 if (err) {
1334 /* Some firmwares (e.g. Intersil 1.3.x) seem
1335 * to have problems disabling the port, oh
1336 * well, too bad. */
1337 printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
1338 dev->name, err);
1339 priv->broken_disableport = 1;
1340 }
1341 }
1342 hermes_set_irqmask(hw, 0);
1343 hermes_write_regn(hw, EVACK, 0xffff);
1344 }
1345
1346 /* firmware will have to reassociate */
1347 netif_carrier_off(dev);
1348 priv->last_linkstatus = 0xffff;
1349
1350 return 0;
1351}
1352
1353int orinoco_reinit_firmware(struct net_device *dev)
1354{
1355 struct orinoco_private *priv = netdev_priv(dev);
1356 struct hermes *hw = &priv->hw;
1357 int err;
1358
1359 err = hermes_init(hw);
1360 if (err)
1361 return err;
1362
1363 err = hermes_allocate(hw, priv->nicbuf_size, &priv->txfid);
David Gibsonb24d4582005-05-12 20:04:16 -04001364 if (err == -EIO && priv->nicbuf_size > TX_NICBUF_SIZE_BUG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 /* Try workaround for old Symbol firmware bug */
1366 printk(KERN_WARNING "%s: firmware ALLOC bug detected "
1367 "(old Symbol firmware?). Trying to work around... ",
1368 dev->name);
1369
1370 priv->nicbuf_size = TX_NICBUF_SIZE_BUG;
1371 err = hermes_allocate(hw, priv->nicbuf_size, &priv->txfid);
1372 if (err)
1373 printk("failed!\n");
1374 else
1375 printk("ok.\n");
1376 }
1377
1378 return err;
1379}
1380
1381static int __orinoco_hw_set_bitrate(struct orinoco_private *priv)
1382{
1383 hermes_t *hw = &priv->hw;
1384 int err = 0;
1385
1386 if (priv->bitratemode >= BITRATE_TABLE_SIZE) {
1387 printk(KERN_ERR "%s: BUG: Invalid bitrate mode %d\n",
1388 priv->ndev->name, priv->bitratemode);
1389 return -EINVAL;
1390 }
1391
1392 switch (priv->firmware_type) {
1393 case FIRMWARE_TYPE_AGERE:
1394 err = hermes_write_wordrec(hw, USER_BAP,
1395 HERMES_RID_CNFTXRATECONTROL,
1396 bitrate_table[priv->bitratemode].agere_txratectrl);
1397 break;
1398 case FIRMWARE_TYPE_INTERSIL:
1399 case FIRMWARE_TYPE_SYMBOL:
1400 err = hermes_write_wordrec(hw, USER_BAP,
1401 HERMES_RID_CNFTXRATECONTROL,
1402 bitrate_table[priv->bitratemode].intersil_txratectrl);
1403 break;
1404 default:
1405 BUG();
1406 }
1407
1408 return err;
1409}
1410
Christoph Hellwig16739b02005-06-19 01:27:51 +02001411/* Set fixed AP address */
1412static int __orinoco_hw_set_wap(struct orinoco_private *priv)
1413{
1414 int roaming_flag;
1415 int err = 0;
1416 hermes_t *hw = &priv->hw;
1417
1418 switch (priv->firmware_type) {
1419 case FIRMWARE_TYPE_AGERE:
1420 /* not supported */
1421 break;
1422 case FIRMWARE_TYPE_INTERSIL:
1423 if (priv->bssid_fixed)
1424 roaming_flag = 2;
1425 else
1426 roaming_flag = 1;
1427
1428 err = hermes_write_wordrec(hw, USER_BAP,
1429 HERMES_RID_CNFROAMINGMODE,
1430 roaming_flag);
1431 break;
1432 case FIRMWARE_TYPE_SYMBOL:
1433 err = HERMES_WRITE_RECORD(hw, USER_BAP,
1434 HERMES_RID_CNFMANDATORYBSSID_SYMBOL,
1435 &priv->desired_bssid);
1436 break;
1437 }
1438 return err;
1439}
1440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441/* Change the WEP keys and/or the current keys. Can be called
1442 * either from __orinoco_hw_setup_wep() or directly from
1443 * orinoco_ioctl_setiwencode(). In the later case the association
1444 * with the AP is not broken (if the firmware can handle it),
1445 * which is needed for 802.1x implementations. */
1446static int __orinoco_hw_setup_wepkeys(struct orinoco_private *priv)
1447{
1448 hermes_t *hw = &priv->hw;
1449 int err = 0;
1450
1451 switch (priv->firmware_type) {
1452 case FIRMWARE_TYPE_AGERE:
1453 err = HERMES_WRITE_RECORD(hw, USER_BAP,
1454 HERMES_RID_CNFWEPKEYS_AGERE,
1455 &priv->keys);
1456 if (err)
1457 return err;
1458 err = hermes_write_wordrec(hw, USER_BAP,
1459 HERMES_RID_CNFTXKEY_AGERE,
1460 priv->tx_key);
1461 if (err)
1462 return err;
1463 break;
1464 case FIRMWARE_TYPE_INTERSIL:
1465 case FIRMWARE_TYPE_SYMBOL:
1466 {
1467 int keylen;
1468 int i;
1469
1470 /* Force uniform key length to work around firmware bugs */
1471 keylen = le16_to_cpu(priv->keys[priv->tx_key].len);
1472
1473 if (keylen > LARGE_KEY_SIZE) {
1474 printk(KERN_ERR "%s: BUG: Key %d has oversize length %d.\n",
1475 priv->ndev->name, priv->tx_key, keylen);
1476 return -E2BIG;
1477 }
1478
1479 /* Write all 4 keys */
1480 for(i = 0; i < ORINOCO_MAX_KEYS; i++) {
1481 err = hermes_write_ltv(hw, USER_BAP,
1482 HERMES_RID_CNFDEFAULTKEY0 + i,
1483 HERMES_BYTES_TO_RECLEN(keylen),
1484 priv->keys[i].data);
1485 if (err)
1486 return err;
1487 }
1488
1489 /* Write the index of the key used in transmission */
1490 err = hermes_write_wordrec(hw, USER_BAP,
1491 HERMES_RID_CNFWEPDEFAULTKEYID,
1492 priv->tx_key);
1493 if (err)
1494 return err;
1495 }
1496 break;
1497 }
1498
1499 return 0;
1500}
1501
1502static int __orinoco_hw_setup_wep(struct orinoco_private *priv)
1503{
1504 hermes_t *hw = &priv->hw;
1505 int err = 0;
1506 int master_wep_flag;
1507 int auth_flag;
1508
1509 if (priv->wep_on)
1510 __orinoco_hw_setup_wepkeys(priv);
1511
1512 if (priv->wep_restrict)
1513 auth_flag = HERMES_AUTH_SHARED_KEY;
1514 else
1515 auth_flag = HERMES_AUTH_OPEN;
1516
1517 switch (priv->firmware_type) {
1518 case FIRMWARE_TYPE_AGERE: /* Agere style WEP */
1519 if (priv->wep_on) {
1520 /* Enable the shared-key authentication. */
1521 err = hermes_write_wordrec(hw, USER_BAP,
1522 HERMES_RID_CNFAUTHENTICATION_AGERE,
1523 auth_flag);
1524 }
1525 err = hermes_write_wordrec(hw, USER_BAP,
1526 HERMES_RID_CNFWEPENABLED_AGERE,
1527 priv->wep_on);
1528 if (err)
1529 return err;
1530 break;
1531
1532 case FIRMWARE_TYPE_INTERSIL: /* Intersil style WEP */
1533 case FIRMWARE_TYPE_SYMBOL: /* Symbol style WEP */
1534 if (priv->wep_on) {
1535 if (priv->wep_restrict ||
1536 (priv->firmware_type == FIRMWARE_TYPE_SYMBOL))
1537 master_wep_flag = HERMES_WEP_PRIVACY_INVOKED |
1538 HERMES_WEP_EXCL_UNENCRYPTED;
1539 else
1540 master_wep_flag = HERMES_WEP_PRIVACY_INVOKED;
1541
1542 err = hermes_write_wordrec(hw, USER_BAP,
1543 HERMES_RID_CNFAUTHENTICATION,
1544 auth_flag);
1545 if (err)
1546 return err;
1547 } else
1548 master_wep_flag = 0;
1549
1550 if (priv->iw_mode == IW_MODE_MONITOR)
1551 master_wep_flag |= HERMES_WEP_HOST_DECRYPT;
1552
1553 /* Master WEP setting : on/off */
1554 err = hermes_write_wordrec(hw, USER_BAP,
1555 HERMES_RID_CNFWEPFLAGS_INTERSIL,
1556 master_wep_flag);
1557 if (err)
1558 return err;
1559
1560 break;
1561 }
1562
1563 return 0;
1564}
1565
1566static int __orinoco_program_rids(struct net_device *dev)
1567{
1568 struct orinoco_private *priv = netdev_priv(dev);
1569 hermes_t *hw = &priv->hw;
1570 int err;
1571 struct hermes_idstring idbuf;
1572
1573 /* Set the MAC address */
1574 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
1575 HERMES_BYTES_TO_RECLEN(ETH_ALEN), dev->dev_addr);
1576 if (err) {
1577 printk(KERN_ERR "%s: Error %d setting MAC address\n",
1578 dev->name, err);
1579 return err;
1580 }
1581
1582 /* Set up the link mode */
1583 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFPORTTYPE,
1584 priv->port_type);
1585 if (err) {
1586 printk(KERN_ERR "%s: Error %d setting port type\n",
1587 dev->name, err);
1588 return err;
1589 }
1590 /* Set the channel/frequency */
David Gibsond51d8b12005-05-12 20:03:36 -04001591 if (priv->channel != 0 && priv->iw_mode != IW_MODE_INFRA) {
1592 err = hermes_write_wordrec(hw, USER_BAP,
1593 HERMES_RID_CNFOWNCHANNEL,
1594 priv->channel);
1595 if (err) {
1596 printk(KERN_ERR "%s: Error %d setting channel %d\n",
1597 dev->name, err, priv->channel);
1598 return err;
1599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 }
1601
1602 if (priv->has_ibss) {
1603 u16 createibss;
1604
1605 if ((strlen(priv->desired_essid) == 0) && (priv->createibss)) {
1606 printk(KERN_WARNING "%s: This firmware requires an "
1607 "ESSID in IBSS-Ad-Hoc mode.\n", dev->name);
1608 /* With wvlan_cs, in this case, we would crash.
1609 * hopefully, this driver will behave better...
1610 * Jean II */
1611 createibss = 0;
1612 } else {
1613 createibss = priv->createibss;
1614 }
1615
1616 err = hermes_write_wordrec(hw, USER_BAP,
1617 HERMES_RID_CNFCREATEIBSS,
1618 createibss);
1619 if (err) {
1620 printk(KERN_ERR "%s: Error %d setting CREATEIBSS\n",
1621 dev->name, err);
1622 return err;
1623 }
1624 }
1625
Christoph Hellwig16739b02005-06-19 01:27:51 +02001626 /* Set the desired BSSID */
1627 err = __orinoco_hw_set_wap(priv);
1628 if (err) {
1629 printk(KERN_ERR "%s: Error %d setting AP address\n",
1630 dev->name, err);
1631 return err;
1632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 /* Set the desired ESSID */
1634 idbuf.len = cpu_to_le16(strlen(priv->desired_essid));
1635 memcpy(&idbuf.val, priv->desired_essid, sizeof(idbuf.val));
1636 /* WinXP wants partner to configure OWNSSID even in IBSS mode. (jimc) */
1637 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNSSID,
1638 HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
1639 &idbuf);
1640 if (err) {
1641 printk(KERN_ERR "%s: Error %d setting OWNSSID\n",
1642 dev->name, err);
1643 return err;
1644 }
1645 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFDESIREDSSID,
1646 HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
1647 &idbuf);
1648 if (err) {
1649 printk(KERN_ERR "%s: Error %d setting DESIREDSSID\n",
1650 dev->name, err);
1651 return err;
1652 }
1653
1654 /* Set the station name */
1655 idbuf.len = cpu_to_le16(strlen(priv->nick));
1656 memcpy(&idbuf.val, priv->nick, sizeof(idbuf.val));
1657 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
1658 HERMES_BYTES_TO_RECLEN(strlen(priv->nick)+2),
1659 &idbuf);
1660 if (err) {
1661 printk(KERN_ERR "%s: Error %d setting nickname\n",
1662 dev->name, err);
1663 return err;
1664 }
1665
1666 /* Set AP density */
1667 if (priv->has_sensitivity) {
1668 err = hermes_write_wordrec(hw, USER_BAP,
1669 HERMES_RID_CNFSYSTEMSCALE,
1670 priv->ap_density);
1671 if (err) {
1672 printk(KERN_WARNING "%s: Error %d setting SYSTEMSCALE. "
1673 "Disabling sensitivity control\n",
1674 dev->name, err);
1675
1676 priv->has_sensitivity = 0;
1677 }
1678 }
1679
1680 /* Set RTS threshold */
1681 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
1682 priv->rts_thresh);
1683 if (err) {
1684 printk(KERN_ERR "%s: Error %d setting RTS threshold\n",
1685 dev->name, err);
1686 return err;
1687 }
1688
1689 /* Set fragmentation threshold or MWO robustness */
1690 if (priv->has_mwo)
1691 err = hermes_write_wordrec(hw, USER_BAP,
1692 HERMES_RID_CNFMWOROBUST_AGERE,
1693 priv->mwo_robust);
1694 else
1695 err = hermes_write_wordrec(hw, USER_BAP,
1696 HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
1697 priv->frag_thresh);
1698 if (err) {
1699 printk(KERN_ERR "%s: Error %d setting fragmentation\n",
1700 dev->name, err);
1701 return err;
1702 }
1703
1704 /* Set bitrate */
1705 err = __orinoco_hw_set_bitrate(priv);
1706 if (err) {
1707 printk(KERN_ERR "%s: Error %d setting bitrate\n",
1708 dev->name, err);
1709 return err;
1710 }
1711
1712 /* Set power management */
1713 if (priv->has_pm) {
1714 err = hermes_write_wordrec(hw, USER_BAP,
1715 HERMES_RID_CNFPMENABLED,
1716 priv->pm_on);
1717 if (err) {
1718 printk(KERN_ERR "%s: Error %d setting up PM\n",
1719 dev->name, err);
1720 return err;
1721 }
1722
1723 err = hermes_write_wordrec(hw, USER_BAP,
1724 HERMES_RID_CNFMULTICASTRECEIVE,
1725 priv->pm_mcast);
1726 if (err) {
1727 printk(KERN_ERR "%s: Error %d setting up PM\n",
1728 dev->name, err);
1729 return err;
1730 }
1731 err = hermes_write_wordrec(hw, USER_BAP,
1732 HERMES_RID_CNFMAXSLEEPDURATION,
1733 priv->pm_period);
1734 if (err) {
1735 printk(KERN_ERR "%s: Error %d setting up PM\n",
1736 dev->name, err);
1737 return err;
1738 }
1739 err = hermes_write_wordrec(hw, USER_BAP,
1740 HERMES_RID_CNFPMHOLDOVERDURATION,
1741 priv->pm_timeout);
1742 if (err) {
1743 printk(KERN_ERR "%s: Error %d setting up PM\n",
1744 dev->name, err);
1745 return err;
1746 }
1747 }
1748
1749 /* Set preamble - only for Symbol so far... */
1750 if (priv->has_preamble) {
1751 err = hermes_write_wordrec(hw, USER_BAP,
1752 HERMES_RID_CNFPREAMBLE_SYMBOL,
1753 priv->preamble);
1754 if (err) {
1755 printk(KERN_ERR "%s: Error %d setting preamble\n",
1756 dev->name, err);
1757 return err;
1758 }
1759 }
1760
1761 /* Set up encryption */
1762 if (priv->has_wep) {
1763 err = __orinoco_hw_setup_wep(priv);
1764 if (err) {
1765 printk(KERN_ERR "%s: Error %d activating WEP\n",
1766 dev->name, err);
1767 return err;
1768 }
1769 }
1770
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02001771 if (priv->iw_mode == IW_MODE_MONITOR) {
1772 /* Enable monitor mode */
1773 dev->type = ARPHRD_IEEE80211;
1774 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
1775 HERMES_TEST_MONITOR, 0, NULL);
1776 } else {
1777 /* Disable monitor mode */
1778 dev->type = ARPHRD_ETHER;
1779 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
1780 HERMES_TEST_STOP, 0, NULL);
1781 }
1782 if (err)
1783 return err;
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 /* Set promiscuity / multicast*/
1786 priv->promiscuous = 0;
1787 priv->mc_count = 0;
1788 __orinoco_set_multicast_list(dev); /* FIXME: what about the xmit_lock */
1789
1790 return 0;
1791}
1792
1793/* FIXME: return int? */
1794static void
1795__orinoco_set_multicast_list(struct net_device *dev)
1796{
1797 struct orinoco_private *priv = netdev_priv(dev);
1798 hermes_t *hw = &priv->hw;
1799 int err = 0;
1800 int promisc, mc_count;
1801
1802 /* The Hermes doesn't seem to have an allmulti mode, so we go
1803 * into promiscuous mode and let the upper levels deal. */
1804 if ( (dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
1805 (dev->mc_count > MAX_MULTICAST(priv)) ) {
1806 promisc = 1;
1807 mc_count = 0;
1808 } else {
1809 promisc = 0;
1810 mc_count = dev->mc_count;
1811 }
1812
1813 if (promisc != priv->promiscuous) {
1814 err = hermes_write_wordrec(hw, USER_BAP,
1815 HERMES_RID_CNFPROMISCUOUSMODE,
1816 promisc);
1817 if (err) {
1818 printk(KERN_ERR "%s: Error %d setting PROMISCUOUSMODE to 1.\n",
1819 dev->name, err);
1820 } else
1821 priv->promiscuous = promisc;
1822 }
1823
1824 if (! promisc && (mc_count || priv->mc_count) ) {
1825 struct dev_mc_list *p = dev->mc_list;
1826 struct hermes_multicast mclist;
1827 int i;
1828
1829 for (i = 0; i < mc_count; i++) {
1830 /* paranoia: is list shorter than mc_count? */
1831 BUG_ON(! p);
1832 /* paranoia: bad address size in list? */
1833 BUG_ON(p->dmi_addrlen != ETH_ALEN);
1834
1835 memcpy(mclist.addr[i], p->dmi_addr, ETH_ALEN);
1836 p = p->next;
1837 }
1838
1839 if (p)
1840 printk(KERN_WARNING "%s: Multicast list is "
1841 "longer than mc_count\n", dev->name);
1842
1843 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFGROUPADDRESSES,
1844 HERMES_BYTES_TO_RECLEN(priv->mc_count * ETH_ALEN),
1845 &mclist);
1846 if (err)
1847 printk(KERN_ERR "%s: Error %d setting multicast list.\n",
1848 dev->name, err);
1849 else
1850 priv->mc_count = mc_count;
1851 }
1852
1853 /* Since we can set the promiscuous flag when it wasn't asked
1854 for, make sure the net_device knows about it. */
1855 if (priv->promiscuous)
1856 dev->flags |= IFF_PROMISC;
1857 else
1858 dev->flags &= ~IFF_PROMISC;
1859}
1860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861/* This must be called from user context, without locks held - use
1862 * schedule_work() */
1863static void orinoco_reset(struct net_device *dev)
1864{
1865 struct orinoco_private *priv = netdev_priv(dev);
1866 struct hermes *hw = &priv->hw;
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001867 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 unsigned long flags;
1869
1870 if (orinoco_lock(priv, &flags) != 0)
1871 /* When the hardware becomes available again, whatever
1872 * detects that is responsible for re-initializing
1873 * it. So no need for anything further */
1874 return;
1875
1876 netif_stop_queue(dev);
1877
1878 /* Shut off interrupts. Depending on what state the hardware
1879 * is in, this might not work, but we'll try anyway */
1880 hermes_set_irqmask(hw, 0);
1881 hermes_write_regn(hw, EVACK, 0xffff);
1882
1883 priv->hw_unavailable++;
1884 priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
1885 netif_carrier_off(dev);
1886
1887 orinoco_unlock(priv, &flags);
1888
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02001889 /* Scanning support: Cleanup of driver struct */
1890 kfree(priv->scan_result);
1891 priv->scan_result = NULL;
1892 priv->scan_inprogress = 0;
1893
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001894 if (priv->hard_reset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 err = (*priv->hard_reset)(priv);
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001896 if (err) {
1897 printk(KERN_ERR "%s: orinoco_reset: Error %d "
1898 "performing hard reset\n", dev->name, err);
1899 goto disable;
1900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
1902
1903 err = orinoco_reinit_firmware(dev);
1904 if (err) {
1905 printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
1906 dev->name, err);
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001907 goto disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 }
1909
1910 spin_lock_irq(&priv->lock); /* This has to be called from user context */
1911
1912 priv->hw_unavailable--;
1913
1914 /* priv->open or priv->hw_unavailable might have changed while
1915 * we dropped the lock */
1916 if (priv->open && (! priv->hw_unavailable)) {
1917 err = __orinoco_up(dev);
1918 if (err) {
1919 printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
1920 dev->name, err);
1921 } else
1922 dev->trans_start = jiffies;
1923 }
1924
1925 spin_unlock_irq(&priv->lock);
1926
1927 return;
Christoph Hellwig8551cb92005-05-14 17:30:04 +02001928 disable:
1929 hermes_set_irqmask(hw, 0);
1930 netif_device_detach(dev);
1931 printk(KERN_ERR "%s: Device has been disabled!\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932}
1933
1934/********************************************************************/
1935/* Interrupt handler */
1936/********************************************************************/
1937
1938static void __orinoco_ev_tick(struct net_device *dev, hermes_t *hw)
1939{
1940 printk(KERN_DEBUG "%s: TICK\n", dev->name);
1941}
1942
1943static void __orinoco_ev_wterr(struct net_device *dev, hermes_t *hw)
1944{
1945 /* This seems to happen a fair bit under load, but ignoring it
1946 seems to work fine...*/
1947 printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
1948 dev->name);
1949}
1950
1951irqreturn_t orinoco_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1952{
1953 struct net_device *dev = (struct net_device *)dev_id;
1954 struct orinoco_private *priv = netdev_priv(dev);
1955 hermes_t *hw = &priv->hw;
1956 int count = MAX_IRQLOOPS_PER_IRQ;
1957 u16 evstat, events;
1958 /* These are used to detect a runaway interrupt situation */
1959 /* If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
1960 * we panic and shut down the hardware */
1961 static int last_irq_jiffy = 0; /* jiffies value the last time
1962 * we were called */
1963 static int loops_this_jiffy = 0;
1964 unsigned long flags;
1965
1966 if (orinoco_lock(priv, &flags) != 0) {
1967 /* If hw is unavailable - we don't know if the irq was
1968 * for us or not */
1969 return IRQ_HANDLED;
1970 }
1971
1972 evstat = hermes_read_regn(hw, EVSTAT);
1973 events = evstat & hw->inten;
1974 if (! events) {
1975 orinoco_unlock(priv, &flags);
1976 return IRQ_NONE;
1977 }
1978
1979 if (jiffies != last_irq_jiffy)
1980 loops_this_jiffy = 0;
1981 last_irq_jiffy = jiffies;
1982
1983 while (events && count--) {
1984 if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
1985 printk(KERN_WARNING "%s: IRQ handler is looping too "
1986 "much! Resetting.\n", dev->name);
1987 /* Disable interrupts for now */
1988 hermes_set_irqmask(hw, 0);
1989 schedule_work(&priv->reset_work);
1990 break;
1991 }
1992
1993 /* Check the card hasn't been removed */
1994 if (! hermes_present(hw)) {
1995 DEBUG(0, "orinoco_interrupt(): card removed\n");
1996 break;
1997 }
1998
1999 if (events & HERMES_EV_TICK)
2000 __orinoco_ev_tick(dev, hw);
2001 if (events & HERMES_EV_WTERR)
2002 __orinoco_ev_wterr(dev, hw);
2003 if (events & HERMES_EV_INFDROP)
2004 __orinoco_ev_infdrop(dev, hw);
2005 if (events & HERMES_EV_INFO)
2006 __orinoco_ev_info(dev, hw);
2007 if (events & HERMES_EV_RX)
2008 __orinoco_ev_rx(dev, hw);
2009 if (events & HERMES_EV_TXEXC)
2010 __orinoco_ev_txexc(dev, hw);
2011 if (events & HERMES_EV_TX)
2012 __orinoco_ev_tx(dev, hw);
2013 if (events & HERMES_EV_ALLOC)
2014 __orinoco_ev_alloc(dev, hw);
2015
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02002016 hermes_write_regn(hw, EVACK, evstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
2018 evstat = hermes_read_regn(hw, EVSTAT);
2019 events = evstat & hw->inten;
2020 };
2021
2022 orinoco_unlock(priv, &flags);
2023 return IRQ_HANDLED;
2024}
2025
2026/********************************************************************/
2027/* Initialization */
2028/********************************************************************/
2029
2030struct comp_id {
2031 u16 id, variant, major, minor;
2032} __attribute__ ((packed));
2033
2034static inline fwtype_t determine_firmware_type(struct comp_id *nic_id)
2035{
2036 if (nic_id->id < 0x8000)
2037 return FIRMWARE_TYPE_AGERE;
2038 else if (nic_id->id == 0x8000 && nic_id->major == 0)
2039 return FIRMWARE_TYPE_SYMBOL;
2040 else
2041 return FIRMWARE_TYPE_INTERSIL;
2042}
2043
2044/* Set priv->firmware type, determine firmware properties */
2045static int determine_firmware(struct net_device *dev)
2046{
2047 struct orinoco_private *priv = netdev_priv(dev);
2048 hermes_t *hw = &priv->hw;
2049 int err;
2050 struct comp_id nic_id, sta_id;
2051 unsigned int firmver;
2052 char tmp[SYMBOL_MAX_VER_LEN+1];
2053
2054 /* Get the hardware version */
2055 err = HERMES_READ_RECORD(hw, USER_BAP, HERMES_RID_NICID, &nic_id);
2056 if (err) {
2057 printk(KERN_ERR "%s: Cannot read hardware identity: error %d\n",
2058 dev->name, err);
2059 return err;
2060 }
2061
2062 le16_to_cpus(&nic_id.id);
2063 le16_to_cpus(&nic_id.variant);
2064 le16_to_cpus(&nic_id.major);
2065 le16_to_cpus(&nic_id.minor);
2066 printk(KERN_DEBUG "%s: Hardware identity %04x:%04x:%04x:%04x\n",
2067 dev->name, nic_id.id, nic_id.variant,
2068 nic_id.major, nic_id.minor);
2069
2070 priv->firmware_type = determine_firmware_type(&nic_id);
2071
2072 /* Get the firmware version */
2073 err = HERMES_READ_RECORD(hw, USER_BAP, HERMES_RID_STAID, &sta_id);
2074 if (err) {
2075 printk(KERN_ERR "%s: Cannot read station identity: error %d\n",
2076 dev->name, err);
2077 return err;
2078 }
2079
2080 le16_to_cpus(&sta_id.id);
2081 le16_to_cpus(&sta_id.variant);
2082 le16_to_cpus(&sta_id.major);
2083 le16_to_cpus(&sta_id.minor);
2084 printk(KERN_DEBUG "%s: Station identity %04x:%04x:%04x:%04x\n",
2085 dev->name, sta_id.id, sta_id.variant,
2086 sta_id.major, sta_id.minor);
2087
2088 switch (sta_id.id) {
2089 case 0x15:
2090 printk(KERN_ERR "%s: Primary firmware is active\n",
2091 dev->name);
2092 return -ENODEV;
2093 case 0x14b:
2094 printk(KERN_ERR "%s: Tertiary firmware is active\n",
2095 dev->name);
2096 return -ENODEV;
2097 case 0x1f: /* Intersil, Agere, Symbol Spectrum24 */
2098 case 0x21: /* Symbol Spectrum24 Trilogy */
2099 break;
2100 default:
2101 printk(KERN_NOTICE "%s: Unknown station ID, please report\n",
2102 dev->name);
2103 break;
2104 }
2105
2106 /* Default capabilities */
2107 priv->has_sensitivity = 1;
2108 priv->has_mwo = 0;
2109 priv->has_preamble = 0;
2110 priv->has_port3 = 1;
2111 priv->has_ibss = 1;
2112 priv->has_wep = 0;
2113 priv->has_big_wep = 0;
2114
2115 /* Determine capabilities from the firmware version */
2116 switch (priv->firmware_type) {
2117 case FIRMWARE_TYPE_AGERE:
2118 /* Lucent Wavelan IEEE, Lucent Orinoco, Cabletron RoamAbout,
2119 ELSA, Melco, HP, IBM, Dell 1150, Compaq 110/210 */
2120 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
2121 "Lucent/Agere %d.%02d", sta_id.major, sta_id.minor);
2122
2123 firmver = ((unsigned long)sta_id.major << 16) | sta_id.minor;
2124
2125 priv->has_ibss = (firmver >= 0x60006);
2126 priv->has_wep = (firmver >= 0x40020);
2127 priv->has_big_wep = 1; /* FIXME: this is wrong - how do we tell
2128 Gold cards from the others? */
2129 priv->has_mwo = (firmver >= 0x60000);
2130 priv->has_pm = (firmver >= 0x40020); /* Don't work in 7.52 ? */
2131 priv->ibss_port = 1;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002132 priv->has_hostscan = (firmver >= 0x8000a);
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02002133 priv->broken_monitor = (firmver >= 0x80000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
2135 /* Tested with Agere firmware :
2136 * 1.16 ; 4.08 ; 4.52 ; 6.04 ; 6.16 ; 7.28 => Jean II
2137 * Tested CableTron firmware : 4.32 => Anton */
2138 break;
2139 case FIRMWARE_TYPE_SYMBOL:
2140 /* Symbol , 3Com AirConnect, Intel, Ericsson WLAN */
2141 /* Intel MAC : 00:02:B3:* */
2142 /* 3Com MAC : 00:50:DA:* */
2143 memset(tmp, 0, sizeof(tmp));
2144 /* Get the Symbol firmware version */
2145 err = hermes_read_ltv(hw, USER_BAP,
2146 HERMES_RID_SECONDARYVERSION_SYMBOL,
2147 SYMBOL_MAX_VER_LEN, NULL, &tmp);
2148 if (err) {
2149 printk(KERN_WARNING
2150 "%s: Error %d reading Symbol firmware info. Wildly guessing capabilities...\n",
2151 dev->name, err);
2152 firmver = 0;
2153 tmp[0] = '\0';
2154 } else {
2155 /* The firmware revision is a string, the format is
2156 * something like : "V2.20-01".
2157 * Quick and dirty parsing... - Jean II
2158 */
2159 firmver = ((tmp[1] - '0') << 16) | ((tmp[3] - '0') << 12)
2160 | ((tmp[4] - '0') << 8) | ((tmp[6] - '0') << 4)
2161 | (tmp[7] - '0');
2162
2163 tmp[SYMBOL_MAX_VER_LEN] = '\0';
2164 }
2165
2166 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
2167 "Symbol %s", tmp);
2168
2169 priv->has_ibss = (firmver >= 0x20000);
2170 priv->has_wep = (firmver >= 0x15012);
2171 priv->has_big_wep = (firmver >= 0x20000);
2172 priv->has_pm = (firmver >= 0x20000 && firmver < 0x22000) ||
2173 (firmver >= 0x29000 && firmver < 0x30000) ||
2174 firmver >= 0x31000;
2175 priv->has_preamble = (firmver >= 0x20000);
2176 priv->ibss_port = 4;
Christoph Hellwig649e59e2005-05-14 17:30:10 +02002177 priv->broken_disableport = (firmver == 0x25013) ||
2178 (firmver >= 0x30000 && firmver <= 0x31000);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002179 priv->has_hostscan = (firmver >= 0x31001) ||
2180 (firmver >= 0x29057 && firmver < 0x30000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 /* Tested with Intel firmware : 0x20015 => Jean II */
2182 /* Tested with 3Com firmware : 0x15012 & 0x22001 => Jean II */
2183 break;
2184 case FIRMWARE_TYPE_INTERSIL:
2185 /* D-Link, Linksys, Adtron, ZoomAir, and many others...
2186 * Samsung, Compaq 100/200 and Proxim are slightly
2187 * different and less well tested */
2188 /* D-Link MAC : 00:40:05:* */
2189 /* Addtron MAC : 00:90:D1:* */
2190 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
2191 "Intersil %d.%d.%d", sta_id.major, sta_id.minor,
2192 sta_id.variant);
2193
2194 firmver = ((unsigned long)sta_id.major << 16) |
2195 ((unsigned long)sta_id.minor << 8) | sta_id.variant;
2196
2197 priv->has_ibss = (firmver >= 0x000700); /* FIXME */
2198 priv->has_big_wep = priv->has_wep = (firmver >= 0x000800);
2199 priv->has_pm = (firmver >= 0x000700);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002200 priv->has_hostscan = (firmver >= 0x010301);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
2202 if (firmver >= 0x000800)
2203 priv->ibss_port = 0;
2204 else {
2205 printk(KERN_NOTICE "%s: Intersil firmware earlier "
2206 "than v0.8.x - several features not supported\n",
2207 dev->name);
2208 priv->ibss_port = 1;
2209 }
2210 break;
2211 }
2212 printk(KERN_DEBUG "%s: Firmware determined as %s\n", dev->name,
2213 priv->fw_name);
2214
2215 return 0;
2216}
2217
2218static int orinoco_init(struct net_device *dev)
2219{
2220 struct orinoco_private *priv = netdev_priv(dev);
2221 hermes_t *hw = &priv->hw;
2222 int err = 0;
2223 struct hermes_idstring nickbuf;
2224 u16 reclen;
2225 int len;
2226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 /* No need to lock, the hw_unavailable flag is already set in
2228 * alloc_orinocodev() */
Jeff Garzikb4538722005-05-12 22:48:20 -04002229 priv->nicbuf_size = IEEE80211_FRAME_LEN + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
2231 /* Initialize the firmware */
David Gibsonb24d4582005-05-12 20:04:16 -04002232 err = orinoco_reinit_firmware(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 if (err != 0) {
2234 printk(KERN_ERR "%s: failed to initialize firmware (err = %d)\n",
2235 dev->name, err);
2236 goto out;
2237 }
2238
2239 err = determine_firmware(dev);
2240 if (err != 0) {
2241 printk(KERN_ERR "%s: Incompatible firmware, aborting\n",
2242 dev->name);
2243 goto out;
2244 }
2245
2246 if (priv->has_port3)
2247 printk(KERN_DEBUG "%s: Ad-hoc demo mode supported\n", dev->name);
2248 if (priv->has_ibss)
2249 printk(KERN_DEBUG "%s: IEEE standard IBSS ad-hoc mode supported\n",
2250 dev->name);
2251 if (priv->has_wep) {
2252 printk(KERN_DEBUG "%s: WEP supported, ", dev->name);
2253 if (priv->has_big_wep)
2254 printk("104-bit key\n");
2255 else
2256 printk("40-bit key\n");
2257 }
2258
2259 /* Get the MAC address */
2260 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
2261 ETH_ALEN, NULL, dev->dev_addr);
2262 if (err) {
2263 printk(KERN_WARNING "%s: failed to read MAC address!\n",
2264 dev->name);
2265 goto out;
2266 }
2267
2268 printk(KERN_DEBUG "%s: MAC address %02X:%02X:%02X:%02X:%02X:%02X\n",
2269 dev->name, dev->dev_addr[0], dev->dev_addr[1],
2270 dev->dev_addr[2], dev->dev_addr[3], dev->dev_addr[4],
2271 dev->dev_addr[5]);
2272
2273 /* Get the station name */
2274 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
2275 sizeof(nickbuf), &reclen, &nickbuf);
2276 if (err) {
2277 printk(KERN_ERR "%s: failed to read station name\n",
2278 dev->name);
2279 goto out;
2280 }
2281 if (nickbuf.len)
2282 len = min(IW_ESSID_MAX_SIZE, (int)le16_to_cpu(nickbuf.len));
2283 else
2284 len = min(IW_ESSID_MAX_SIZE, 2 * reclen);
2285 memcpy(priv->nick, &nickbuf.val, len);
2286 priv->nick[len] = '\0';
2287
2288 printk(KERN_DEBUG "%s: Station name \"%s\"\n", dev->name, priv->nick);
2289
2290 /* Get allowed channels */
2291 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CHANNELLIST,
2292 &priv->channel_mask);
2293 if (err) {
2294 printk(KERN_ERR "%s: failed to read channel list!\n",
2295 dev->name);
2296 goto out;
2297 }
2298
2299 /* Get initial AP density */
2300 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFSYSTEMSCALE,
2301 &priv->ap_density);
2302 if (err || priv->ap_density < 1 || priv->ap_density > 3) {
2303 priv->has_sensitivity = 0;
2304 }
2305
2306 /* Get initial RTS threshold */
2307 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
2308 &priv->rts_thresh);
2309 if (err) {
2310 printk(KERN_ERR "%s: failed to read RTS threshold!\n",
2311 dev->name);
2312 goto out;
2313 }
2314
2315 /* Get initial fragmentation settings */
2316 if (priv->has_mwo)
2317 err = hermes_read_wordrec(hw, USER_BAP,
2318 HERMES_RID_CNFMWOROBUST_AGERE,
2319 &priv->mwo_robust);
2320 else
2321 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
2322 &priv->frag_thresh);
2323 if (err) {
2324 printk(KERN_ERR "%s: failed to read fragmentation settings!\n",
2325 dev->name);
2326 goto out;
2327 }
2328
2329 /* Power management setup */
2330 if (priv->has_pm) {
2331 priv->pm_on = 0;
2332 priv->pm_mcast = 1;
2333 err = hermes_read_wordrec(hw, USER_BAP,
2334 HERMES_RID_CNFMAXSLEEPDURATION,
2335 &priv->pm_period);
2336 if (err) {
2337 printk(KERN_ERR "%s: failed to read power management period!\n",
2338 dev->name);
2339 goto out;
2340 }
2341 err = hermes_read_wordrec(hw, USER_BAP,
2342 HERMES_RID_CNFPMHOLDOVERDURATION,
2343 &priv->pm_timeout);
2344 if (err) {
2345 printk(KERN_ERR "%s: failed to read power management timeout!\n",
2346 dev->name);
2347 goto out;
2348 }
2349 }
2350
2351 /* Preamble setup */
2352 if (priv->has_preamble) {
2353 err = hermes_read_wordrec(hw, USER_BAP,
2354 HERMES_RID_CNFPREAMBLE_SYMBOL,
2355 &priv->preamble);
2356 if (err)
2357 goto out;
2358 }
2359
2360 /* Set up the default configuration */
2361 priv->iw_mode = IW_MODE_INFRA;
2362 /* By default use IEEE/IBSS ad-hoc mode if we have it */
2363 priv->prefer_port3 = priv->has_port3 && (! priv->has_ibss);
2364 set_port_type(priv);
David Gibsond51d8b12005-05-12 20:03:36 -04002365 priv->channel = 0; /* use firmware default */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
2367 priv->promiscuous = 0;
2368 priv->wep_on = 0;
2369 priv->tx_key = 0;
2370
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 /* Make the hardware available, as long as it hasn't been
2372 * removed elsewhere (e.g. by PCMCIA hot unplug) */
2373 spin_lock_irq(&priv->lock);
2374 priv->hw_unavailable--;
2375 spin_unlock_irq(&priv->lock);
2376
2377 printk(KERN_DEBUG "%s: ready\n", dev->name);
2378
2379 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 return err;
2381}
2382
2383struct net_device *alloc_orinocodev(int sizeof_card,
2384 int (*hard_reset)(struct orinoco_private *))
2385{
2386 struct net_device *dev;
2387 struct orinoco_private *priv;
2388
2389 dev = alloc_etherdev(sizeof(struct orinoco_private) + sizeof_card);
2390 if (! dev)
2391 return NULL;
2392 priv = netdev_priv(dev);
2393 priv->ndev = dev;
2394 if (sizeof_card)
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02002395 priv->card = (void *)((unsigned long)priv
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 + sizeof(struct orinoco_private));
2397 else
2398 priv->card = NULL;
2399
2400 /* Setup / override net_device fields */
2401 dev->init = orinoco_init;
2402 dev->hard_start_xmit = orinoco_xmit;
2403 dev->tx_timeout = orinoco_tx_timeout;
2404 dev->watchdog_timeo = HZ; /* 1 second timeout */
2405 dev->get_stats = orinoco_get_stats;
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02002406 dev->ethtool_ops = &orinoco_ethtool_ops;
Christoph Hellwig620554e2005-06-19 01:27:33 +02002407 dev->wireless_handlers = (struct iw_handler_def *)&orinoco_handler_def;
Pavel Roskin343c6862005-09-09 18:43:02 -04002408#ifdef WIRELESS_SPY
2409 priv->wireless_data.spy_data = &priv->spy_data;
2410 dev->wireless_data = &priv->wireless_data;
2411#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 dev->change_mtu = orinoco_change_mtu;
2413 dev->set_multicast_list = orinoco_set_multicast_list;
2414 /* we use the default eth_mac_addr for setting the MAC addr */
2415
2416 /* Set up default callbacks */
2417 dev->open = orinoco_open;
2418 dev->stop = orinoco_stop;
2419 priv->hard_reset = hard_reset;
2420
2421 spin_lock_init(&priv->lock);
2422 priv->open = 0;
2423 priv->hw_unavailable = 1; /* orinoco_init() must clear this
2424 * before anything else touches the
2425 * hardware */
2426 INIT_WORK(&priv->reset_work, (void (*)(void *))orinoco_reset, dev);
Christoph Hellwig16739b02005-06-19 01:27:51 +02002427 INIT_WORK(&priv->join_work, (void (*)(void *))orinoco_join_ap, dev);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002428 INIT_WORK(&priv->wevent_work, (void (*)(void *))orinoco_send_wevents, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
2430 netif_carrier_off(dev);
2431 priv->last_linkstatus = 0xffff;
2432
2433 return dev;
2434
2435}
2436
2437void free_orinocodev(struct net_device *dev)
2438{
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02002439 struct orinoco_private *priv = netdev_priv(dev);
2440
2441 kfree(priv->scan_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 free_netdev(dev);
2443}
2444
2445/********************************************************************/
2446/* Wireless extensions */
2447/********************************************************************/
2448
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449static int orinoco_hw_get_essid(struct orinoco_private *priv, int *active,
2450 char buf[IW_ESSID_MAX_SIZE+1])
2451{
2452 hermes_t *hw = &priv->hw;
2453 int err = 0;
2454 struct hermes_idstring essidbuf;
2455 char *p = (char *)(&essidbuf.val);
2456 int len;
2457 unsigned long flags;
2458
2459 if (orinoco_lock(priv, &flags) != 0)
2460 return -EBUSY;
2461
2462 if (strlen(priv->desired_essid) > 0) {
2463 /* We read the desired SSID from the hardware rather
2464 than from priv->desired_essid, just in case the
2465 firmware is allowed to change it on us. I'm not
2466 sure about this */
2467 /* My guess is that the OWNSSID should always be whatever
2468 * we set to the card, whereas CURRENT_SSID is the one that
2469 * may change... - Jean II */
2470 u16 rid;
2471
2472 *active = 1;
2473
2474 rid = (priv->port_type == 3) ? HERMES_RID_CNFOWNSSID :
2475 HERMES_RID_CNFDESIREDSSID;
2476
2477 err = hermes_read_ltv(hw, USER_BAP, rid, sizeof(essidbuf),
2478 NULL, &essidbuf);
2479 if (err)
2480 goto fail_unlock;
2481 } else {
2482 *active = 0;
2483
2484 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTSSID,
2485 sizeof(essidbuf), NULL, &essidbuf);
2486 if (err)
2487 goto fail_unlock;
2488 }
2489
2490 len = le16_to_cpu(essidbuf.len);
Christoph Hellwig84d8a2f2005-05-14 17:30:22 +02002491 BUG_ON(len > IW_ESSID_MAX_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
2493 memset(buf, 0, IW_ESSID_MAX_SIZE+1);
2494 memcpy(buf, p, len);
2495 buf[len] = '\0';
2496
2497 fail_unlock:
2498 orinoco_unlock(priv, &flags);
2499
2500 return err;
2501}
2502
2503static long orinoco_hw_get_freq(struct orinoco_private *priv)
2504{
2505
2506 hermes_t *hw = &priv->hw;
2507 int err = 0;
2508 u16 channel;
2509 long freq = 0;
2510 unsigned long flags;
2511
2512 if (orinoco_lock(priv, &flags) != 0)
2513 return -EBUSY;
2514
2515 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CURRENTCHANNEL, &channel);
2516 if (err)
2517 goto out;
2518
2519 /* Intersil firmware 1.3.5 returns 0 when the interface is down */
2520 if (channel == 0) {
2521 err = -EBUSY;
2522 goto out;
2523 }
2524
2525 if ( (channel < 1) || (channel > NUM_CHANNELS) ) {
2526 printk(KERN_WARNING "%s: Channel out of range (%d)!\n",
2527 priv->ndev->name, channel);
2528 err = -EBUSY;
2529 goto out;
2530
2531 }
2532 freq = channel_frequency[channel-1] * 100000;
2533
2534 out:
2535 orinoco_unlock(priv, &flags);
2536
2537 if (err > 0)
2538 err = -EBUSY;
2539 return err ? err : freq;
2540}
2541
2542static int orinoco_hw_get_bitratelist(struct orinoco_private *priv,
2543 int *numrates, s32 *rates, int max)
2544{
2545 hermes_t *hw = &priv->hw;
2546 struct hermes_idstring list;
2547 unsigned char *p = (unsigned char *)&list.val;
2548 int err = 0;
2549 int num;
2550 int i;
2551 unsigned long flags;
2552
2553 if (orinoco_lock(priv, &flags) != 0)
2554 return -EBUSY;
2555
2556 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_SUPPORTEDDATARATES,
2557 sizeof(list), NULL, &list);
2558 orinoco_unlock(priv, &flags);
2559
2560 if (err)
2561 return err;
2562
2563 num = le16_to_cpu(list.len);
2564 *numrates = num;
2565 num = min(num, max);
2566
2567 for (i = 0; i < num; i++) {
2568 rates[i] = (p[i] & 0x7f) * 500000; /* convert to bps */
2569 }
2570
2571 return 0;
2572}
2573
Christoph Hellwig620554e2005-06-19 01:27:33 +02002574static int orinoco_ioctl_getname(struct net_device *dev,
2575 struct iw_request_info *info,
2576 char *name,
2577 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578{
2579 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 int numrates;
Christoph Hellwig620554e2005-06-19 01:27:33 +02002581 int err;
2582
2583 err = orinoco_hw_get_bitratelist(priv, &numrates, NULL, 0);
2584
2585 if (!err && (numrates > 2))
2586 strcpy(name, "IEEE 802.11b");
2587 else
2588 strcpy(name, "IEEE 802.11-DS");
2589
2590 return 0;
2591}
2592
Christoph Hellwig16739b02005-06-19 01:27:51 +02002593static int orinoco_ioctl_setwap(struct net_device *dev,
2594 struct iw_request_info *info,
2595 struct sockaddr *ap_addr,
2596 char *extra)
2597{
2598 struct orinoco_private *priv = netdev_priv(dev);
2599 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 unsigned long flags;
Christoph Hellwig16739b02005-06-19 01:27:51 +02002601 static const u8 off_addr[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
2602 static const u8 any_addr[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
2604 if (orinoco_lock(priv, &flags) != 0)
2605 return -EBUSY;
2606
Christoph Hellwig16739b02005-06-19 01:27:51 +02002607 /* Enable automatic roaming - no sanity checks are needed */
2608 if (memcmp(&ap_addr->sa_data, off_addr, ETH_ALEN) == 0 ||
2609 memcmp(&ap_addr->sa_data, any_addr, ETH_ALEN) == 0) {
2610 priv->bssid_fixed = 0;
2611 memset(priv->desired_bssid, 0, ETH_ALEN);
2612
2613 /* "off" means keep existing connection */
2614 if (ap_addr->sa_data[0] == 0) {
2615 __orinoco_hw_set_wap(priv);
2616 err = 0;
2617 }
2618 goto out;
2619 }
2620
2621 if (priv->firmware_type == FIRMWARE_TYPE_AGERE) {
2622 printk(KERN_WARNING "%s: Lucent/Agere firmware doesn't "
2623 "support manual roaming\n",
2624 dev->name);
2625 err = -EOPNOTSUPP;
2626 goto out;
2627 }
2628
2629 if (priv->iw_mode != IW_MODE_INFRA) {
2630 printk(KERN_WARNING "%s: Manual roaming supported only in "
2631 "managed mode\n", dev->name);
2632 err = -EOPNOTSUPP;
2633 goto out;
2634 }
2635
2636 /* Intersil firmware hangs without Desired ESSID */
2637 if (priv->firmware_type == FIRMWARE_TYPE_INTERSIL &&
2638 strlen(priv->desired_essid) == 0) {
2639 printk(KERN_WARNING "%s: Desired ESSID must be set for "
2640 "manual roaming\n", dev->name);
2641 err = -EOPNOTSUPP;
2642 goto out;
2643 }
2644
2645 /* Finally, enable manual roaming */
2646 priv->bssid_fixed = 1;
2647 memcpy(priv->desired_bssid, &ap_addr->sa_data, ETH_ALEN);
2648
2649 out:
2650 orinoco_unlock(priv, &flags);
2651 return err;
2652}
2653
Christoph Hellwig620554e2005-06-19 01:27:33 +02002654static int orinoco_ioctl_getwap(struct net_device *dev,
2655 struct iw_request_info *info,
2656 struct sockaddr *ap_addr,
2657 char *extra)
2658{
2659 struct orinoco_private *priv = netdev_priv(dev);
2660
2661 hermes_t *hw = &priv->hw;
2662 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 unsigned long flags;
2664
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 if (orinoco_lock(priv, &flags) != 0)
2666 return -EBUSY;
2667
Christoph Hellwig620554e2005-06-19 01:27:33 +02002668 ap_addr->sa_family = ARPHRD_ETHER;
2669 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
2670 ETH_ALEN, NULL, ap_addr->sa_data);
2671
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 orinoco_unlock(priv, &flags);
2673
Christoph Hellwig620554e2005-06-19 01:27:33 +02002674 return err;
2675}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
Christoph Hellwig620554e2005-06-19 01:27:33 +02002677static int orinoco_ioctl_setmode(struct net_device *dev,
2678 struct iw_request_info *info,
2679 u32 *mode,
2680 char *extra)
2681{
2682 struct orinoco_private *priv = netdev_priv(dev);
2683 int err = -EINPROGRESS; /* Call commit handler */
2684 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
Christoph Hellwig620554e2005-06-19 01:27:33 +02002686 if (priv->iw_mode == *mode)
2687 return 0;
2688
2689 if (orinoco_lock(priv, &flags) != 0)
2690 return -EBUSY;
2691
2692 switch (*mode) {
2693 case IW_MODE_ADHOC:
2694 if (!priv->has_ibss && !priv->has_port3)
2695 err = -EOPNOTSUPP;
2696 break;
2697
2698 case IW_MODE_INFRA:
2699 break;
2700
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02002701 case IW_MODE_MONITOR:
2702 if (priv->broken_monitor && !force_monitor) {
2703 printk(KERN_WARNING "%s: Monitor mode support is "
2704 "buggy in this firmware, not enabling\n",
2705 dev->name);
2706 err = -EOPNOTSUPP;
2707 }
2708 break;
2709
Christoph Hellwig620554e2005-06-19 01:27:33 +02002710 default:
2711 err = -EOPNOTSUPP;
2712 break;
2713 }
2714
2715 if (err == -EINPROGRESS) {
2716 priv->iw_mode = *mode;
2717 set_port_type(priv);
2718 }
2719
2720 orinoco_unlock(priv, &flags);
2721
2722 return err;
2723}
2724
2725static int orinoco_ioctl_getmode(struct net_device *dev,
2726 struct iw_request_info *info,
2727 u32 *mode,
2728 char *extra)
2729{
2730 struct orinoco_private *priv = netdev_priv(dev);
2731
2732 *mode = priv->iw_mode;
2733 return 0;
2734}
2735
2736static int orinoco_ioctl_getiwrange(struct net_device *dev,
2737 struct iw_request_info *info,
2738 struct iw_point *rrq,
2739 char *extra)
2740{
2741 struct orinoco_private *priv = netdev_priv(dev);
2742 int err = 0;
2743 struct iw_range *range = (struct iw_range *) extra;
2744 int numrates;
2745 int i, k;
2746
Christoph Hellwig620554e2005-06-19 01:27:33 +02002747 rrq->length = sizeof(struct iw_range);
2748 memset(range, 0, sizeof(struct iw_range));
2749
2750 range->we_version_compiled = WIRELESS_EXT;
2751 range->we_version_source = 14;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
2753 /* Set available channels/frequencies */
Christoph Hellwig620554e2005-06-19 01:27:33 +02002754 range->num_channels = NUM_CHANNELS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 k = 0;
2756 for (i = 0; i < NUM_CHANNELS; i++) {
2757 if (priv->channel_mask & (1 << i)) {
Christoph Hellwig620554e2005-06-19 01:27:33 +02002758 range->freq[k].i = i + 1;
2759 range->freq[k].m = channel_frequency[i] * 100000;
2760 range->freq[k].e = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 k++;
2762 }
2763
2764 if (k >= IW_MAX_FREQUENCIES)
2765 break;
2766 }
Christoph Hellwig620554e2005-06-19 01:27:33 +02002767 range->num_frequency = k;
2768 range->sensitivity = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
Christoph Hellwig620554e2005-06-19 01:27:33 +02002770 if (priv->has_wep) {
2771 range->max_encoding_tokens = ORINOCO_MAX_KEYS;
2772 range->encoding_size[0] = SMALL_KEY_SIZE;
2773 range->num_encoding_sizes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Christoph Hellwig620554e2005-06-19 01:27:33 +02002775 if (priv->has_big_wep) {
2776 range->encoding_size[1] = LARGE_KEY_SIZE;
2777 range->num_encoding_sizes = 2;
2778 }
2779 }
2780
Pavel Roskin343c6862005-09-09 18:43:02 -04002781 if ((priv->iw_mode == IW_MODE_ADHOC) && (!SPY_NUMBER(priv))){
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 /* Quality stats meaningless in ad-hoc mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 } else {
Christoph Hellwig620554e2005-06-19 01:27:33 +02002784 range->max_qual.qual = 0x8b - 0x2f;
2785 range->max_qual.level = 0x2f - 0x95 - 1;
2786 range->max_qual.noise = 0x2f - 0x95 - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 /* Need to get better values */
Christoph Hellwig620554e2005-06-19 01:27:33 +02002788 range->avg_qual.qual = 0x24;
2789 range->avg_qual.level = 0xC2;
2790 range->avg_qual.noise = 0x9E;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 }
2792
2793 err = orinoco_hw_get_bitratelist(priv, &numrates,
Christoph Hellwig620554e2005-06-19 01:27:33 +02002794 range->bitrate, IW_MAX_BITRATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 if (err)
2796 return err;
Christoph Hellwig620554e2005-06-19 01:27:33 +02002797 range->num_bitrates = numrates;
2798
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 /* Set an indication of the max TCP throughput in bit/s that we can
2800 * expect using this interface. May be use for QoS stuff...
2801 * Jean II */
Christoph Hellwig620554e2005-06-19 01:27:33 +02002802 if (numrates > 2)
2803 range->throughput = 5 * 1000 * 1000; /* ~5 Mb/s */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 else
Christoph Hellwig620554e2005-06-19 01:27:33 +02002805 range->throughput = 1.5 * 1000 * 1000; /* ~1.5 Mb/s */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
Christoph Hellwig620554e2005-06-19 01:27:33 +02002807 range->min_rts = 0;
2808 range->max_rts = 2347;
2809 range->min_frag = 256;
2810 range->max_frag = 2346;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
Christoph Hellwig620554e2005-06-19 01:27:33 +02002812 range->min_pmp = 0;
2813 range->max_pmp = 65535000;
2814 range->min_pmt = 0;
2815 range->max_pmt = 65535 * 1000; /* ??? */
2816 range->pmp_flags = IW_POWER_PERIOD;
2817 range->pmt_flags = IW_POWER_TIMEOUT;
2818 range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_UNICAST_R;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819
Christoph Hellwig620554e2005-06-19 01:27:33 +02002820 range->retry_capa = IW_RETRY_LIMIT | IW_RETRY_LIFETIME;
2821 range->retry_flags = IW_RETRY_LIMIT;
2822 range->r_time_flags = IW_RETRY_LIFETIME;
2823 range->min_retry = 0;
2824 range->max_retry = 65535; /* ??? */
2825 range->min_r_time = 0;
2826 range->max_r_time = 65535 * 1000; /* ??? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
Pavel Roskin343c6862005-09-09 18:43:02 -04002828 /* Event capability (kernel) */
2829 IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
2830 /* Event capability (driver) */
2831 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWTHRSPY);
2832 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
2833 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
2834 IW_EVENT_CAPA_SET(range->event_capa, IWEVTXDROP);
2835
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 return 0;
2837}
2838
Christoph Hellwig620554e2005-06-19 01:27:33 +02002839static int orinoco_ioctl_setiwencode(struct net_device *dev,
2840 struct iw_request_info *info,
2841 struct iw_point *erq,
2842 char *keybuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843{
2844 struct orinoco_private *priv = netdev_priv(dev);
2845 int index = (erq->flags & IW_ENCODE_INDEX) - 1;
2846 int setindex = priv->tx_key;
2847 int enable = priv->wep_on;
2848 int restricted = priv->wep_restrict;
2849 u16 xlen = 0;
Christoph Hellwig620554e2005-06-19 01:27:33 +02002850 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 unsigned long flags;
2852
2853 if (! priv->has_wep)
2854 return -EOPNOTSUPP;
2855
2856 if (erq->pointer) {
2857 /* We actually have a key to set - check its length */
2858 if (erq->length > LARGE_KEY_SIZE)
2859 return -E2BIG;
2860
2861 if ( (erq->length > SMALL_KEY_SIZE) && !priv->has_big_wep )
2862 return -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 }
2864
2865 if (orinoco_lock(priv, &flags) != 0)
2866 return -EBUSY;
2867
2868 if (erq->pointer) {
2869 if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
2870 index = priv->tx_key;
2871
2872 /* Adjust key length to a supported value */
2873 if (erq->length > SMALL_KEY_SIZE) {
2874 xlen = LARGE_KEY_SIZE;
2875 } else if (erq->length > 0) {
2876 xlen = SMALL_KEY_SIZE;
2877 } else
2878 xlen = 0;
2879
2880 /* Switch on WEP if off */
2881 if ((!enable) && (xlen > 0)) {
2882 setindex = index;
2883 enable = 1;
2884 }
2885 } else {
2886 /* Important note : if the user do "iwconfig eth0 enc off",
2887 * we will arrive there with an index of -1. This is valid
2888 * but need to be taken care off... Jean II */
2889 if ((index < 0) || (index >= ORINOCO_MAX_KEYS)) {
2890 if((index != -1) || (erq->flags == 0)) {
2891 err = -EINVAL;
2892 goto out;
2893 }
2894 } else {
2895 /* Set the index : Check that the key is valid */
2896 if(priv->keys[index].len == 0) {
2897 err = -EINVAL;
2898 goto out;
2899 }
2900 setindex = index;
2901 }
2902 }
2903
2904 if (erq->flags & IW_ENCODE_DISABLED)
2905 enable = 0;
2906 if (erq->flags & IW_ENCODE_OPEN)
2907 restricted = 0;
2908 if (erq->flags & IW_ENCODE_RESTRICTED)
2909 restricted = 1;
2910
2911 if (erq->pointer) {
2912 priv->keys[index].len = cpu_to_le16(xlen);
2913 memset(priv->keys[index].data, 0,
2914 sizeof(priv->keys[index].data));
2915 memcpy(priv->keys[index].data, keybuf, erq->length);
2916 }
2917 priv->tx_key = setindex;
2918
2919 /* Try fast key change if connected and only keys are changed */
2920 if (priv->wep_on && enable && (priv->wep_restrict == restricted) &&
2921 netif_carrier_ok(dev)) {
2922 err = __orinoco_hw_setup_wepkeys(priv);
2923 /* No need to commit if successful */
2924 goto out;
2925 }
2926
2927 priv->wep_on = enable;
2928 priv->wep_restrict = restricted;
2929
2930 out:
2931 orinoco_unlock(priv, &flags);
2932
2933 return err;
2934}
2935
Christoph Hellwig620554e2005-06-19 01:27:33 +02002936static int orinoco_ioctl_getiwencode(struct net_device *dev,
2937 struct iw_request_info *info,
2938 struct iw_point *erq,
2939 char *keybuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940{
2941 struct orinoco_private *priv = netdev_priv(dev);
2942 int index = (erq->flags & IW_ENCODE_INDEX) - 1;
2943 u16 xlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 unsigned long flags;
2945
2946 if (! priv->has_wep)
2947 return -EOPNOTSUPP;
2948
2949 if (orinoco_lock(priv, &flags) != 0)
2950 return -EBUSY;
2951
2952 if ((index < 0) || (index >= ORINOCO_MAX_KEYS))
2953 index = priv->tx_key;
2954
2955 erq->flags = 0;
2956 if (! priv->wep_on)
2957 erq->flags |= IW_ENCODE_DISABLED;
2958 erq->flags |= index + 1;
2959
2960 if (priv->wep_restrict)
2961 erq->flags |= IW_ENCODE_RESTRICTED;
2962 else
2963 erq->flags |= IW_ENCODE_OPEN;
2964
2965 xlen = le16_to_cpu(priv->keys[index].len);
2966
2967 erq->length = xlen;
2968
2969 memcpy(keybuf, priv->keys[index].data, ORINOCO_MAX_KEY_SIZE);
2970
2971 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 return 0;
2973}
2974
Christoph Hellwig620554e2005-06-19 01:27:33 +02002975static int orinoco_ioctl_setessid(struct net_device *dev,
2976 struct iw_request_info *info,
2977 struct iw_point *erq,
2978 char *essidbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979{
2980 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 unsigned long flags;
2982
2983 /* Note : ESSID is ignored in Ad-Hoc demo mode, but we can set it
2984 * anyway... - Jean II */
2985
Christoph Hellwig620554e2005-06-19 01:27:33 +02002986 /* Hum... Should not use Wireless Extension constant (may change),
2987 * should use our own... - Jean II */
2988 if (erq->length > IW_ESSID_MAX_SIZE)
2989 return -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
2991 if (orinoco_lock(priv, &flags) != 0)
2992 return -EBUSY;
2993
Christoph Hellwig620554e2005-06-19 01:27:33 +02002994 /* NULL the string (for NULL termination & ESSID = ANY) - Jean II */
2995 memset(priv->desired_essid, 0, sizeof(priv->desired_essid));
2996
2997 /* If not ANY, get the new ESSID */
2998 if (erq->flags) {
2999 memcpy(priv->desired_essid, essidbuf, erq->length);
3000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
3002 orinoco_unlock(priv, &flags);
3003
Christoph Hellwig620554e2005-06-19 01:27:33 +02003004 return -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005}
3006
Christoph Hellwig620554e2005-06-19 01:27:33 +02003007static int orinoco_ioctl_getessid(struct net_device *dev,
3008 struct iw_request_info *info,
3009 struct iw_point *erq,
3010 char *essidbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011{
3012 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 int active;
3014 int err = 0;
3015 unsigned long flags;
3016
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 if (netif_running(dev)) {
3018 err = orinoco_hw_get_essid(priv, &active, essidbuf);
3019 if (err)
3020 return err;
3021 } else {
3022 if (orinoco_lock(priv, &flags) != 0)
3023 return -EBUSY;
Christoph Hellwig620554e2005-06-19 01:27:33 +02003024 memcpy(essidbuf, priv->desired_essid, IW_ESSID_MAX_SIZE + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 orinoco_unlock(priv, &flags);
3026 }
3027
3028 erq->flags = 1;
3029 erq->length = strlen(essidbuf) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 return 0;
3032}
3033
Christoph Hellwig620554e2005-06-19 01:27:33 +02003034static int orinoco_ioctl_setnick(struct net_device *dev,
3035 struct iw_request_info *info,
3036 struct iw_point *nrq,
3037 char *nickbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
3039 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 unsigned long flags;
3041
3042 if (nrq->length > IW_ESSID_MAX_SIZE)
3043 return -E2BIG;
3044
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 if (orinoco_lock(priv, &flags) != 0)
3046 return -EBUSY;
3047
Christoph Hellwig620554e2005-06-19 01:27:33 +02003048 memset(priv->nick, 0, sizeof(priv->nick));
3049 memcpy(priv->nick, nickbuf, nrq->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
3051 orinoco_unlock(priv, &flags);
3052
Christoph Hellwig620554e2005-06-19 01:27:33 +02003053 return -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054}
3055
Christoph Hellwig620554e2005-06-19 01:27:33 +02003056static int orinoco_ioctl_getnick(struct net_device *dev,
3057 struct iw_request_info *info,
3058 struct iw_point *nrq,
3059 char *nickbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060{
3061 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 unsigned long flags;
3063
3064 if (orinoco_lock(priv, &flags) != 0)
3065 return -EBUSY;
3066
3067 memcpy(nickbuf, priv->nick, IW_ESSID_MAX_SIZE+1);
3068 orinoco_unlock(priv, &flags);
3069
3070 nrq->length = strlen(nickbuf)+1;
3071
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 return 0;
3073}
3074
Christoph Hellwig620554e2005-06-19 01:27:33 +02003075static int orinoco_ioctl_setfreq(struct net_device *dev,
3076 struct iw_request_info *info,
3077 struct iw_freq *frq,
3078 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079{
3080 struct orinoco_private *priv = netdev_priv(dev);
3081 int chan = -1;
3082 unsigned long flags;
Christoph Hellwig620554e2005-06-19 01:27:33 +02003083 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02003085 /* In infrastructure mode the AP sets the channel */
3086 if (priv->iw_mode == IW_MODE_INFRA)
3087 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
3089 if ( (frq->e == 0) && (frq->m <= 1000) ) {
3090 /* Setting by channel number */
3091 chan = frq->m;
3092 } else {
3093 /* Setting by frequency - search the table */
3094 int mult = 1;
3095 int i;
3096
3097 for (i = 0; i < (6 - frq->e); i++)
3098 mult *= 10;
3099
3100 for (i = 0; i < NUM_CHANNELS; i++)
3101 if (frq->m == (channel_frequency[i] * mult))
3102 chan = i+1;
3103 }
3104
3105 if ( (chan < 1) || (chan > NUM_CHANNELS) ||
3106 ! (priv->channel_mask & (1 << (chan-1)) ) )
3107 return -EINVAL;
3108
3109 if (orinoco_lock(priv, &flags) != 0)
3110 return -EBUSY;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02003111
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 priv->channel = chan;
Christoph Hellwig98c4cae2005-06-19 01:28:06 +02003113 if (priv->iw_mode == IW_MODE_MONITOR) {
3114 /* Fast channel change - no commit if successful */
3115 hermes_t *hw = &priv->hw;
3116 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
3117 HERMES_TEST_SET_CHANNEL,
3118 chan, NULL);
3119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 orinoco_unlock(priv, &flags);
3121
Christoph Hellwig620554e2005-06-19 01:27:33 +02003122 return err;
3123}
3124
3125static int orinoco_ioctl_getfreq(struct net_device *dev,
3126 struct iw_request_info *info,
3127 struct iw_freq *frq,
3128 char *extra)
3129{
3130 struct orinoco_private *priv = netdev_priv(dev);
3131 int tmp;
3132
3133 /* Locking done in there */
3134 tmp = orinoco_hw_get_freq(priv);
3135 if (tmp < 0) {
3136 return tmp;
3137 }
3138
3139 frq->m = tmp;
3140 frq->e = 1;
3141
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 return 0;
3143}
3144
Christoph Hellwig620554e2005-06-19 01:27:33 +02003145static int orinoco_ioctl_getsens(struct net_device *dev,
3146 struct iw_request_info *info,
3147 struct iw_param *srq,
3148 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149{
3150 struct orinoco_private *priv = netdev_priv(dev);
3151 hermes_t *hw = &priv->hw;
3152 u16 val;
3153 int err;
3154 unsigned long flags;
3155
3156 if (!priv->has_sensitivity)
3157 return -EOPNOTSUPP;
3158
3159 if (orinoco_lock(priv, &flags) != 0)
3160 return -EBUSY;
3161 err = hermes_read_wordrec(hw, USER_BAP,
3162 HERMES_RID_CNFSYSTEMSCALE, &val);
3163 orinoco_unlock(priv, &flags);
3164
3165 if (err)
3166 return err;
3167
3168 srq->value = val;
3169 srq->fixed = 0; /* auto */
3170
3171 return 0;
3172}
3173
Christoph Hellwig620554e2005-06-19 01:27:33 +02003174static int orinoco_ioctl_setsens(struct net_device *dev,
3175 struct iw_request_info *info,
3176 struct iw_param *srq,
3177 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178{
3179 struct orinoco_private *priv = netdev_priv(dev);
3180 int val = srq->value;
3181 unsigned long flags;
3182
3183 if (!priv->has_sensitivity)
3184 return -EOPNOTSUPP;
3185
3186 if ((val < 1) || (val > 3))
3187 return -EINVAL;
3188
3189 if (orinoco_lock(priv, &flags) != 0)
3190 return -EBUSY;
3191 priv->ap_density = val;
3192 orinoco_unlock(priv, &flags);
3193
Christoph Hellwig620554e2005-06-19 01:27:33 +02003194 return -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195}
3196
Christoph Hellwig620554e2005-06-19 01:27:33 +02003197static int orinoco_ioctl_setrts(struct net_device *dev,
3198 struct iw_request_info *info,
3199 struct iw_param *rrq,
3200 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201{
3202 struct orinoco_private *priv = netdev_priv(dev);
3203 int val = rrq->value;
3204 unsigned long flags;
3205
3206 if (rrq->disabled)
3207 val = 2347;
3208
3209 if ( (val < 0) || (val > 2347) )
3210 return -EINVAL;
3211
3212 if (orinoco_lock(priv, &flags) != 0)
3213 return -EBUSY;
3214
3215 priv->rts_thresh = val;
3216 orinoco_unlock(priv, &flags);
3217
Christoph Hellwig620554e2005-06-19 01:27:33 +02003218 return -EINPROGRESS; /* Call commit handler */
3219}
3220
3221static int orinoco_ioctl_getrts(struct net_device *dev,
3222 struct iw_request_info *info,
3223 struct iw_param *rrq,
3224 char *extra)
3225{
3226 struct orinoco_private *priv = netdev_priv(dev);
3227
3228 rrq->value = priv->rts_thresh;
3229 rrq->disabled = (rrq->value == 2347);
3230 rrq->fixed = 1;
3231
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 return 0;
3233}
3234
Christoph Hellwig620554e2005-06-19 01:27:33 +02003235static int orinoco_ioctl_setfrag(struct net_device *dev,
3236 struct iw_request_info *info,
3237 struct iw_param *frq,
3238 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239{
3240 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02003241 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 unsigned long flags;
3243
3244 if (orinoco_lock(priv, &flags) != 0)
3245 return -EBUSY;
3246
3247 if (priv->has_mwo) {
3248 if (frq->disabled)
3249 priv->mwo_robust = 0;
3250 else {
3251 if (frq->fixed)
3252 printk(KERN_WARNING "%s: Fixed fragmentation is "
3253 "not supported on this firmware. "
3254 "Using MWO robust instead.\n", dev->name);
3255 priv->mwo_robust = 1;
3256 }
3257 } else {
3258 if (frq->disabled)
3259 priv->frag_thresh = 2346;
3260 else {
3261 if ( (frq->value < 256) || (frq->value > 2346) )
3262 err = -EINVAL;
3263 else
3264 priv->frag_thresh = frq->value & ~0x1; /* must be even */
3265 }
3266 }
3267
3268 orinoco_unlock(priv, &flags);
3269
3270 return err;
3271}
3272
Christoph Hellwig620554e2005-06-19 01:27:33 +02003273static int orinoco_ioctl_getfrag(struct net_device *dev,
3274 struct iw_request_info *info,
3275 struct iw_param *frq,
3276 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277{
3278 struct orinoco_private *priv = netdev_priv(dev);
3279 hermes_t *hw = &priv->hw;
Christoph Hellwig620554e2005-06-19 01:27:33 +02003280 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 u16 val;
3282 unsigned long flags;
3283
3284 if (orinoco_lock(priv, &flags) != 0)
3285 return -EBUSY;
3286
3287 if (priv->has_mwo) {
3288 err = hermes_read_wordrec(hw, USER_BAP,
3289 HERMES_RID_CNFMWOROBUST_AGERE,
3290 &val);
3291 if (err)
3292 val = 0;
3293
3294 frq->value = val ? 2347 : 0;
3295 frq->disabled = ! val;
3296 frq->fixed = 0;
3297 } else {
3298 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
3299 &val);
3300 if (err)
3301 val = 0;
3302
3303 frq->value = val;
3304 frq->disabled = (val >= 2346);
3305 frq->fixed = 1;
3306 }
3307
3308 orinoco_unlock(priv, &flags);
3309
3310 return err;
3311}
3312
Christoph Hellwig620554e2005-06-19 01:27:33 +02003313static int orinoco_ioctl_setrate(struct net_device *dev,
3314 struct iw_request_info *info,
3315 struct iw_param *rrq,
3316 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317{
3318 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 int ratemode = -1;
3320 int bitrate; /* 100s of kilobits */
3321 int i;
3322 unsigned long flags;
3323
3324 /* As the user space doesn't know our highest rate, it uses -1
3325 * to ask us to set the highest rate. Test it using "iwconfig
3326 * ethX rate auto" - Jean II */
3327 if (rrq->value == -1)
3328 bitrate = 110;
3329 else {
3330 if (rrq->value % 100000)
3331 return -EINVAL;
3332 bitrate = rrq->value / 100000;
3333 }
3334
3335 if ( (bitrate != 10) && (bitrate != 20) &&
3336 (bitrate != 55) && (bitrate != 110) )
3337 return -EINVAL;
3338
3339 for (i = 0; i < BITRATE_TABLE_SIZE; i++)
3340 if ( (bitrate_table[i].bitrate == bitrate) &&
3341 (bitrate_table[i].automatic == ! rrq->fixed) ) {
3342 ratemode = i;
3343 break;
3344 }
3345
3346 if (ratemode == -1)
3347 return -EINVAL;
3348
3349 if (orinoco_lock(priv, &flags) != 0)
3350 return -EBUSY;
3351 priv->bitratemode = ratemode;
3352 orinoco_unlock(priv, &flags);
3353
Christoph Hellwig620554e2005-06-19 01:27:33 +02003354 return -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355}
3356
Christoph Hellwig620554e2005-06-19 01:27:33 +02003357static int orinoco_ioctl_getrate(struct net_device *dev,
3358 struct iw_request_info *info,
3359 struct iw_param *rrq,
3360 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361{
3362 struct orinoco_private *priv = netdev_priv(dev);
3363 hermes_t *hw = &priv->hw;
3364 int err = 0;
3365 int ratemode;
3366 int i;
3367 u16 val;
3368 unsigned long flags;
3369
3370 if (orinoco_lock(priv, &flags) != 0)
3371 return -EBUSY;
3372
3373 ratemode = priv->bitratemode;
3374
3375 BUG_ON((ratemode < 0) || (ratemode >= BITRATE_TABLE_SIZE));
3376
3377 rrq->value = bitrate_table[ratemode].bitrate * 100000;
3378 rrq->fixed = ! bitrate_table[ratemode].automatic;
3379 rrq->disabled = 0;
3380
3381 /* If the interface is running we try to find more about the
3382 current mode */
3383 if (netif_running(dev)) {
3384 err = hermes_read_wordrec(hw, USER_BAP,
3385 HERMES_RID_CURRENTTXRATE, &val);
3386 if (err)
3387 goto out;
3388
3389 switch (priv->firmware_type) {
3390 case FIRMWARE_TYPE_AGERE: /* Lucent style rate */
3391 /* Note : in Lucent firmware, the return value of
3392 * HERMES_RID_CURRENTTXRATE is the bitrate in Mb/s,
3393 * and therefore is totally different from the
3394 * encoding of HERMES_RID_CNFTXRATECONTROL.
3395 * Don't forget that 6Mb/s is really 5.5Mb/s */
3396 if (val == 6)
3397 rrq->value = 5500000;
3398 else
3399 rrq->value = val * 1000000;
3400 break;
3401 case FIRMWARE_TYPE_INTERSIL: /* Intersil style rate */
3402 case FIRMWARE_TYPE_SYMBOL: /* Symbol style rate */
3403 for (i = 0; i < BITRATE_TABLE_SIZE; i++)
3404 if (bitrate_table[i].intersil_txratectrl == val) {
3405 ratemode = i;
3406 break;
3407 }
3408 if (i >= BITRATE_TABLE_SIZE)
3409 printk(KERN_INFO "%s: Unable to determine current bitrate (0x%04hx)\n",
3410 dev->name, val);
3411
3412 rrq->value = bitrate_table[ratemode].bitrate * 100000;
3413 break;
3414 default:
3415 BUG();
3416 }
3417 }
3418
3419 out:
3420 orinoco_unlock(priv, &flags);
3421
3422 return err;
3423}
3424
Christoph Hellwig620554e2005-06-19 01:27:33 +02003425static int orinoco_ioctl_setpower(struct net_device *dev,
3426 struct iw_request_info *info,
3427 struct iw_param *prq,
3428 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429{
3430 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02003431 int err = -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 unsigned long flags;
3433
3434 if (orinoco_lock(priv, &flags) != 0)
3435 return -EBUSY;
3436
3437 if (prq->disabled) {
3438 priv->pm_on = 0;
3439 } else {
3440 switch (prq->flags & IW_POWER_MODE) {
3441 case IW_POWER_UNICAST_R:
3442 priv->pm_mcast = 0;
3443 priv->pm_on = 1;
3444 break;
3445 case IW_POWER_ALL_R:
3446 priv->pm_mcast = 1;
3447 priv->pm_on = 1;
3448 break;
3449 case IW_POWER_ON:
3450 /* No flags : but we may have a value - Jean II */
3451 break;
3452 default:
3453 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 goto out;
Pavel Roskinc08ad1e2005-11-29 02:59:27 -05003455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
3457 if (prq->flags & IW_POWER_TIMEOUT) {
3458 priv->pm_on = 1;
3459 priv->pm_timeout = prq->value / 1000;
3460 }
3461 if (prq->flags & IW_POWER_PERIOD) {
3462 priv->pm_on = 1;
3463 priv->pm_period = prq->value / 1000;
3464 }
3465 /* It's valid to not have a value if we are just toggling
3466 * the flags... Jean II */
3467 if(!priv->pm_on) {
3468 err = -EINVAL;
3469 goto out;
3470 }
3471 }
3472
3473 out:
3474 orinoco_unlock(priv, &flags);
3475
3476 return err;
3477}
3478
Christoph Hellwig620554e2005-06-19 01:27:33 +02003479static int orinoco_ioctl_getpower(struct net_device *dev,
3480 struct iw_request_info *info,
3481 struct iw_param *prq,
3482 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483{
3484 struct orinoco_private *priv = netdev_priv(dev);
3485 hermes_t *hw = &priv->hw;
3486 int err = 0;
3487 u16 enable, period, timeout, mcast;
3488 unsigned long flags;
3489
3490 if (orinoco_lock(priv, &flags) != 0)
3491 return -EBUSY;
3492
3493 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFPMENABLED, &enable);
3494 if (err)
3495 goto out;
3496
3497 err = hermes_read_wordrec(hw, USER_BAP,
3498 HERMES_RID_CNFMAXSLEEPDURATION, &period);
3499 if (err)
3500 goto out;
3501
3502 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFPMHOLDOVERDURATION, &timeout);
3503 if (err)
3504 goto out;
3505
3506 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFMULTICASTRECEIVE, &mcast);
3507 if (err)
3508 goto out;
3509
3510 prq->disabled = !enable;
3511 /* Note : by default, display the period */
3512 if ((prq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
3513 prq->flags = IW_POWER_TIMEOUT;
3514 prq->value = timeout * 1000;
3515 } else {
3516 prq->flags = IW_POWER_PERIOD;
3517 prq->value = period * 1000;
3518 }
3519 if (mcast)
3520 prq->flags |= IW_POWER_ALL_R;
3521 else
3522 prq->flags |= IW_POWER_UNICAST_R;
3523
3524 out:
3525 orinoco_unlock(priv, &flags);
3526
3527 return err;
3528}
3529
Christoph Hellwig620554e2005-06-19 01:27:33 +02003530static int orinoco_ioctl_getretry(struct net_device *dev,
3531 struct iw_request_info *info,
3532 struct iw_param *rrq,
3533 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534{
3535 struct orinoco_private *priv = netdev_priv(dev);
3536 hermes_t *hw = &priv->hw;
3537 int err = 0;
3538 u16 short_limit, long_limit, lifetime;
3539 unsigned long flags;
3540
3541 if (orinoco_lock(priv, &flags) != 0)
3542 return -EBUSY;
3543
3544 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_SHORTRETRYLIMIT,
3545 &short_limit);
3546 if (err)
3547 goto out;
3548
3549 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_LONGRETRYLIMIT,
3550 &long_limit);
3551 if (err)
3552 goto out;
3553
3554 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_MAXTRANSMITLIFETIME,
3555 &lifetime);
3556 if (err)
3557 goto out;
3558
3559 rrq->disabled = 0; /* Can't be disabled */
3560
3561 /* Note : by default, display the retry number */
3562 if ((rrq->flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME) {
3563 rrq->flags = IW_RETRY_LIFETIME;
3564 rrq->value = lifetime * 1000; /* ??? */
3565 } else {
3566 /* By default, display the min number */
3567 if ((rrq->flags & IW_RETRY_MAX)) {
3568 rrq->flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
3569 rrq->value = long_limit;
3570 } else {
3571 rrq->flags = IW_RETRY_LIMIT;
3572 rrq->value = short_limit;
3573 if(short_limit != long_limit)
3574 rrq->flags |= IW_RETRY_MIN;
3575 }
3576 }
3577
3578 out:
3579 orinoco_unlock(priv, &flags);
3580
3581 return err;
3582}
3583
Christoph Hellwig620554e2005-06-19 01:27:33 +02003584static int orinoco_ioctl_reset(struct net_device *dev,
3585 struct iw_request_info *info,
3586 void *wrqu,
3587 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588{
3589 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02003590
3591 if (! capable(CAP_NET_ADMIN))
3592 return -EPERM;
3593
3594 if (info->cmd == (SIOCIWFIRSTPRIV + 0x1)) {
3595 printk(KERN_DEBUG "%s: Forcing reset!\n", dev->name);
3596
3597 /* Firmware reset */
3598 orinoco_reset(dev);
3599 } else {
3600 printk(KERN_DEBUG "%s: Force scheduling reset!\n", dev->name);
3601
3602 schedule_work(&priv->reset_work);
3603 }
3604
3605 return 0;
3606}
3607
3608static int orinoco_ioctl_setibssport(struct net_device *dev,
3609 struct iw_request_info *info,
3610 void *wrqu,
3611 char *extra)
3612
3613{
3614 struct orinoco_private *priv = netdev_priv(dev);
3615 int val = *( (int *) extra );
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 unsigned long flags;
3617
3618 if (orinoco_lock(priv, &flags) != 0)
3619 return -EBUSY;
3620
3621 priv->ibss_port = val ;
3622
3623 /* Actually update the mode we are using */
3624 set_port_type(priv);
3625
3626 orinoco_unlock(priv, &flags);
Christoph Hellwig620554e2005-06-19 01:27:33 +02003627 return -EINPROGRESS; /* Call commit handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628}
3629
Christoph Hellwig620554e2005-06-19 01:27:33 +02003630static int orinoco_ioctl_getibssport(struct net_device *dev,
3631 struct iw_request_info *info,
3632 void *wrqu,
3633 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634{
3635 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02003636 int *val = (int *) extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637
3638 *val = priv->ibss_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 return 0;
3640}
3641
Christoph Hellwig620554e2005-06-19 01:27:33 +02003642static int orinoco_ioctl_setport3(struct net_device *dev,
3643 struct iw_request_info *info,
3644 void *wrqu,
3645 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646{
3647 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02003648 int val = *( (int *) extra );
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 int err = 0;
3650 unsigned long flags;
3651
3652 if (orinoco_lock(priv, &flags) != 0)
3653 return -EBUSY;
3654
3655 switch (val) {
3656 case 0: /* Try to do IEEE ad-hoc mode */
3657 if (! priv->has_ibss) {
3658 err = -EINVAL;
3659 break;
3660 }
3661 priv->prefer_port3 = 0;
3662
3663 break;
3664
3665 case 1: /* Try to do Lucent proprietary ad-hoc mode */
3666 if (! priv->has_port3) {
3667 err = -EINVAL;
3668 break;
3669 }
3670 priv->prefer_port3 = 1;
3671 break;
3672
3673 default:
3674 err = -EINVAL;
3675 }
3676
Christoph Hellwig620554e2005-06-19 01:27:33 +02003677 if (! err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 /* Actually update the mode we are using */
3679 set_port_type(priv);
Christoph Hellwig620554e2005-06-19 01:27:33 +02003680 err = -EINPROGRESS;
3681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682
3683 orinoco_unlock(priv, &flags);
3684
3685 return err;
3686}
3687
Christoph Hellwig620554e2005-06-19 01:27:33 +02003688static int orinoco_ioctl_getport3(struct net_device *dev,
3689 struct iw_request_info *info,
3690 void *wrqu,
3691 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692{
3693 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02003694 int *val = (int *) extra;
3695
3696 *val = priv->prefer_port3;
3697 return 0;
3698}
3699
3700static int orinoco_ioctl_setpreamble(struct net_device *dev,
3701 struct iw_request_info *info,
3702 void *wrqu,
3703 char *extra)
3704{
3705 struct orinoco_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 unsigned long flags;
Christoph Hellwig620554e2005-06-19 01:27:33 +02003707 int val;
3708
3709 if (! priv->has_preamble)
3710 return -EOPNOTSUPP;
3711
3712 /* 802.11b has recently defined some short preamble.
3713 * Basically, the Phy header has been reduced in size.
3714 * This increase performance, especially at high rates
3715 * (the preamble is transmitted at 1Mb/s), unfortunately
3716 * this give compatibility troubles... - Jean II */
3717 val = *( (int *) extra );
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718
3719 if (orinoco_lock(priv, &flags) != 0)
3720 return -EBUSY;
3721
Christoph Hellwig620554e2005-06-19 01:27:33 +02003722 if (val)
3723 priv->preamble = 1;
3724 else
3725 priv->preamble = 0;
3726
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 orinoco_unlock(priv, &flags);
Christoph Hellwig620554e2005-06-19 01:27:33 +02003728
3729 return -EINPROGRESS; /* Call commit handler */
3730}
3731
3732static int orinoco_ioctl_getpreamble(struct net_device *dev,
3733 struct iw_request_info *info,
3734 void *wrqu,
3735 char *extra)
3736{
3737 struct orinoco_private *priv = netdev_priv(dev);
3738 int *val = (int *) extra;
3739
3740 if (! priv->has_preamble)
3741 return -EOPNOTSUPP;
3742
3743 *val = priv->preamble;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 return 0;
3745}
3746
Christoph Hellwig620554e2005-06-19 01:27:33 +02003747/* ioctl interface to hermes_read_ltv()
3748 * To use with iwpriv, pass the RID as the token argument, e.g.
3749 * iwpriv get_rid [0xfc00]
3750 * At least Wireless Tools 25 is required to use iwpriv.
3751 * For Wireless Tools 25 and 26 append "dummy" are the end. */
3752static int orinoco_ioctl_getrid(struct net_device *dev,
3753 struct iw_request_info *info,
3754 struct iw_point *data,
3755 char *extra)
3756{
3757 struct orinoco_private *priv = netdev_priv(dev);
3758 hermes_t *hw = &priv->hw;
3759 int rid = data->flags;
3760 u16 length;
3761 int err;
3762 unsigned long flags;
3763
3764 /* It's a "get" function, but we don't want users to access the
3765 * WEP key and other raw firmware data */
3766 if (! capable(CAP_NET_ADMIN))
3767 return -EPERM;
3768
3769 if (rid < 0xfc00 || rid > 0xffff)
3770 return -EINVAL;
3771
3772 if (orinoco_lock(priv, &flags) != 0)
3773 return -EBUSY;
3774
3775 err = hermes_read_ltv(hw, USER_BAP, rid, MAX_RID_LEN, &length,
3776 extra);
3777 if (err)
3778 goto out;
3779
3780 data->length = min_t(u16, HERMES_RECLEN_TO_BYTES(length),
3781 MAX_RID_LEN);
3782
3783 out:
3784 orinoco_unlock(priv, &flags);
3785 return err;
3786}
3787
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003788/* Trigger a scan (look for other cells in the vicinity */
3789static int orinoco_ioctl_setscan(struct net_device *dev,
3790 struct iw_request_info *info,
3791 struct iw_param *srq,
3792 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793{
3794 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003795 hermes_t *hw = &priv->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 unsigned long flags;
3798
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003799 /* Note : you may have realised that, as this is a SET operation,
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -08003800 * this is privileged and therefore a normal user can't
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003801 * perform scanning.
3802 * This is not an error, while the device perform scanning,
3803 * traffic doesn't flow, so it's a perfect DoS...
3804 * Jean II */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003806 if (orinoco_lock(priv, &flags) != 0)
3807 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003809 /* Scanning with port 0 disabled would fail */
3810 if (!netif_running(dev)) {
3811 err = -ENETDOWN;
3812 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003815 /* In monitor mode, the scan results are always empty.
3816 * Probe responses are passed to the driver as received
3817 * frames and could be processed in software. */
3818 if (priv->iw_mode == IW_MODE_MONITOR) {
3819 err = -EOPNOTSUPP;
3820 goto out;
3821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003823 /* Note : because we don't lock out the irq handler, the way
3824 * we access scan variables in priv is critical.
3825 * o scan_inprogress : not touched by irq handler
3826 * o scan_mode : not touched by irq handler
3827 * o scan_result : irq is strict producer, non-irq is strict
3828 * consumer.
3829 * o scan_len : synchronised with scan_result
3830 * Before modifying anything on those variables, please think hard !
3831 * Jean II */
3832
3833 /* If there is still some left-over scan results, get rid of it */
3834 if (priv->scan_result != NULL) {
3835 /* What's likely is that a client did crash or was killed
3836 * between triggering the scan request and reading the
3837 * results, so we need to reset everything.
3838 * Some clients that are too slow may suffer from that...
3839 * Jean II */
3840 kfree(priv->scan_result);
3841 priv->scan_result = NULL;
3842 }
3843
3844 /* Save flags */
3845 priv->scan_mode = srq->flags;
3846
3847 /* Always trigger scanning, even if it's in progress.
3848 * This way, if the info frame get lost, we will recover somewhat
3849 * gracefully - Jean II */
3850
3851 if (priv->has_hostscan) {
3852 switch (priv->firmware_type) {
3853 case FIRMWARE_TYPE_SYMBOL:
3854 err = hermes_write_wordrec(hw, USER_BAP,
3855 HERMES_RID_CNFHOSTSCAN_SYMBOL,
3856 HERMES_HOSTSCAN_SYMBOL_ONCE |
3857 HERMES_HOSTSCAN_SYMBOL_BCAST);
3858 break;
3859 case FIRMWARE_TYPE_INTERSIL: {
Pavel Roskind133ae42005-09-23 04:18:06 -04003860 __le16 req[3];
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003861
3862 req[0] = cpu_to_le16(0x3fff); /* All channels */
3863 req[1] = cpu_to_le16(0x0001); /* rate 1 Mbps */
3864 req[2] = 0; /* Any ESSID */
3865 err = HERMES_WRITE_RECORD(hw, USER_BAP,
3866 HERMES_RID_CNFHOSTSCAN, &req);
3867 }
3868 break;
3869 case FIRMWARE_TYPE_AGERE:
3870 err = hermes_write_wordrec(hw, USER_BAP,
3871 HERMES_RID_CNFSCANSSID_AGERE,
3872 0); /* Any ESSID */
3873 if (err)
3874 break;
3875
3876 err = hermes_inquire(hw, HERMES_INQ_SCAN);
3877 break;
3878 }
3879 } else
3880 err = hermes_inquire(hw, HERMES_INQ_SCAN);
3881
3882 /* One more client */
3883 if (! err)
3884 priv->scan_inprogress = 1;
3885
3886 out:
3887 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 return err;
3889}
3890
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003891/* Translate scan data returned from the card to a card independant
Pavel Roskin70817c42005-09-01 20:02:50 -04003892 * format that the Wireless Tools will understand - Jean II
3893 * Return message length or -errno for fatal errors */
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003894static inline int orinoco_translate_scan(struct net_device *dev,
3895 char *buffer,
3896 char *scan,
3897 int scan_len)
3898{
3899 struct orinoco_private *priv = netdev_priv(dev);
3900 int offset; /* In the scan data */
3901 union hermes_scan_info *atom;
3902 int atom_len;
3903 u16 capabilities;
3904 u16 channel;
3905 struct iw_event iwe; /* Temporary buffer */
3906 char * current_ev = buffer;
3907 char * end_buf = buffer + IW_SCAN_MAX_DATA;
3908
3909 switch (priv->firmware_type) {
3910 case FIRMWARE_TYPE_AGERE:
3911 atom_len = sizeof(struct agere_scan_apinfo);
3912 offset = 0;
3913 break;
3914 case FIRMWARE_TYPE_SYMBOL:
3915 /* Lack of documentation necessitates this hack.
3916 * Different firmwares have 68 or 76 byte long atoms.
3917 * We try modulo first. If the length divides by both,
3918 * we check what would be the channel in the second
3919 * frame for a 68-byte atom. 76-byte atoms have 0 there.
3920 * Valid channel cannot be 0. */
3921 if (scan_len % 76)
3922 atom_len = 68;
3923 else if (scan_len % 68)
3924 atom_len = 76;
3925 else if (scan_len >= 1292 && scan[68] == 0)
3926 atom_len = 76;
3927 else
3928 atom_len = 68;
3929 offset = 0;
3930 break;
3931 case FIRMWARE_TYPE_INTERSIL:
3932 offset = 4;
Pavel Roskin70817c42005-09-01 20:02:50 -04003933 if (priv->has_hostscan) {
Pavel Roskind133ae42005-09-23 04:18:06 -04003934 atom_len = le16_to_cpup((__le16 *)scan);
Pavel Roskin70817c42005-09-01 20:02:50 -04003935 /* Sanity check for atom_len */
3936 if (atom_len < sizeof(struct prism2_scan_apinfo)) {
3937 printk(KERN_ERR "%s: Invalid atom_len in scan data: %d\n",
3938 dev->name, atom_len);
3939 return -EIO;
3940 }
3941 } else
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003942 atom_len = offsetof(struct prism2_scan_apinfo, atim);
3943 break;
3944 default:
Pavel Roskin70817c42005-09-01 20:02:50 -04003945 return -EOPNOTSUPP;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003946 }
3947
3948 /* Check that we got an whole number of atoms */
3949 if ((scan_len - offset) % atom_len) {
3950 printk(KERN_ERR "%s: Unexpected scan data length %d, "
3951 "atom_len %d, offset %d\n", dev->name, scan_len,
3952 atom_len, offset);
Pavel Roskin70817c42005-09-01 20:02:50 -04003953 return -EIO;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02003954 }
3955
3956 /* Read the entries one by one */
3957 for (; offset + atom_len <= scan_len; offset += atom_len) {
3958 /* Get next atom */
3959 atom = (union hermes_scan_info *) (scan + offset);
3960
3961 /* First entry *MUST* be the AP MAC address */
3962 iwe.cmd = SIOCGIWAP;
3963 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
3964 memcpy(iwe.u.ap_addr.sa_data, atom->a.bssid, ETH_ALEN);
3965 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_ADDR_LEN);
3966
3967 /* Other entries will be displayed in the order we give them */
3968
3969 /* Add the ESSID */
3970 iwe.u.data.length = le16_to_cpu(atom->a.essid_len);
3971 if (iwe.u.data.length > 32)
3972 iwe.u.data.length = 32;
3973 iwe.cmd = SIOCGIWESSID;
3974 iwe.u.data.flags = 1;
3975 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, atom->a.essid);
3976
3977 /* Add mode */
3978 iwe.cmd = SIOCGIWMODE;
3979 capabilities = le16_to_cpu(atom->a.capabilities);
3980 if (capabilities & 0x3) {
3981 if (capabilities & 0x1)
3982 iwe.u.mode = IW_MODE_MASTER;
3983 else
3984 iwe.u.mode = IW_MODE_ADHOC;
3985 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_UINT_LEN);
3986 }
3987
3988 channel = atom->s.channel;
3989 if ( (channel >= 1) && (channel <= NUM_CHANNELS) ) {
3990 /* Add frequency */
3991 iwe.cmd = SIOCGIWFREQ;
3992 iwe.u.freq.m = channel_frequency[channel-1] * 100000;
3993 iwe.u.freq.e = 1;
3994 current_ev = iwe_stream_add_event(current_ev, end_buf,
3995 &iwe, IW_EV_FREQ_LEN);
3996 }
3997
3998 /* Add quality statistics */
3999 iwe.cmd = IWEVQUAL;
4000 iwe.u.qual.updated = 0x10; /* no link quality */
4001 iwe.u.qual.level = (__u8) le16_to_cpu(atom->a.level) - 0x95;
4002 iwe.u.qual.noise = (__u8) le16_to_cpu(atom->a.noise) - 0x95;
4003 /* Wireless tools prior to 27.pre22 will show link quality
4004 * anyway, so we provide a reasonable value. */
4005 if (iwe.u.qual.level > iwe.u.qual.noise)
4006 iwe.u.qual.qual = iwe.u.qual.level - iwe.u.qual.noise;
4007 else
4008 iwe.u.qual.qual = 0;
4009 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_QUAL_LEN);
4010
4011 /* Add encryption capability */
4012 iwe.cmd = SIOCGIWENCODE;
4013 if (capabilities & 0x10)
4014 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
4015 else
4016 iwe.u.data.flags = IW_ENCODE_DISABLED;
4017 iwe.u.data.length = 0;
4018 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, atom->a.essid);
4019
4020 /* Bit rate is not available in Lucent/Agere firmwares */
4021 if (priv->firmware_type != FIRMWARE_TYPE_AGERE) {
4022 char * current_val = current_ev + IW_EV_LCP_LEN;
4023 int i;
4024 int step;
4025
4026 if (priv->firmware_type == FIRMWARE_TYPE_SYMBOL)
4027 step = 2;
4028 else
4029 step = 1;
4030
4031 iwe.cmd = SIOCGIWRATE;
4032 /* Those two flags are ignored... */
4033 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
4034 /* Max 10 values */
4035 for (i = 0; i < 10; i += step) {
4036 /* NULL terminated */
4037 if (atom->p.rates[i] == 0x0)
4038 break;
4039 /* Bit rate given in 500 kb/s units (+ 0x80) */
4040 iwe.u.bitrate.value = ((atom->p.rates[i] & 0x7f) * 500000);
4041 current_val = iwe_stream_add_value(current_ev, current_val,
4042 end_buf, &iwe,
4043 IW_EV_PARAM_LEN);
4044 }
4045 /* Check if we added any event */
4046 if ((current_val - current_ev) > IW_EV_LCP_LEN)
4047 current_ev = current_val;
4048 }
4049
4050 /* The other data in the scan result are not really
4051 * interesting, so for now drop it - Jean II */
4052 }
4053 return current_ev - buffer;
4054}
4055
4056/* Return results of a scan */
4057static int orinoco_ioctl_getscan(struct net_device *dev,
4058 struct iw_request_info *info,
4059 struct iw_point *srq,
4060 char *extra)
4061{
4062 struct orinoco_private *priv = netdev_priv(dev);
4063 int err = 0;
4064 unsigned long flags;
4065
4066 if (orinoco_lock(priv, &flags) != 0)
4067 return -EBUSY;
4068
4069 /* If no results yet, ask to try again later */
4070 if (priv->scan_result == NULL) {
4071 if (priv->scan_inprogress)
4072 /* Important note : we don't want to block the caller
4073 * until results are ready for various reasons.
4074 * First, managing wait queues is complex and racy.
4075 * Second, we grab some rtnetlink lock before comming
4076 * here (in dev_ioctl()).
4077 * Third, we generate an Wireless Event, so the
4078 * caller can wait itself on that - Jean II */
4079 err = -EAGAIN;
4080 else
4081 /* Client error, no scan results...
4082 * The caller need to restart the scan. */
4083 err = -ENODATA;
4084 } else {
4085 /* We have some results to push back to user space */
4086
4087 /* Translate to WE format */
Pavel Roskin70817c42005-09-01 20:02:50 -04004088 int ret = orinoco_translate_scan(dev, extra,
4089 priv->scan_result,
4090 priv->scan_len);
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004091
Pavel Roskin70817c42005-09-01 20:02:50 -04004092 if (ret < 0) {
4093 err = ret;
4094 kfree(priv->scan_result);
4095 priv->scan_result = NULL;
4096 } else {
4097 srq->length = ret;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004098
Pavel Roskin70817c42005-09-01 20:02:50 -04004099 /* Return flags */
4100 srq->flags = (__u16) priv->scan_mode;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004101
Pavel Roskin70817c42005-09-01 20:02:50 -04004102 /* In any case, Scan results will be cleaned up in the
4103 * reset function and when exiting the driver.
4104 * The person triggering the scanning may never come to
4105 * pick the results, so we need to do it in those places.
4106 * Jean II */
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004107
4108#ifdef SCAN_SINGLE_READ
Pavel Roskin70817c42005-09-01 20:02:50 -04004109 /* If you enable this option, only one client (the first
4110 * one) will be able to read the result (and only one
4111 * time). If there is multiple concurent clients that
4112 * want to read scan results, this behavior is not
4113 * advisable - Jean II */
4114 kfree(priv->scan_result);
4115 priv->scan_result = NULL;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004116#endif /* SCAN_SINGLE_READ */
Pavel Roskin70817c42005-09-01 20:02:50 -04004117 /* Here, if too much time has elapsed since last scan,
4118 * we may want to clean up scan results... - Jean II */
4119 }
4120
4121 /* Scan is no longer in progress */
4122 priv->scan_inprogress = 0;
Christoph Hellwig95dd91f2005-06-19 01:27:56 +02004123 }
4124
4125 orinoco_unlock(priv, &flags);
4126 return err;
4127}
4128
Christoph Hellwig620554e2005-06-19 01:27:33 +02004129/* Commit handler, called after set operations */
4130static int orinoco_ioctl_commit(struct net_device *dev,
4131 struct iw_request_info *info,
4132 void *wrqu,
4133 char *extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134{
4135 struct orinoco_private *priv = netdev_priv(dev);
Christoph Hellwig620554e2005-06-19 01:27:33 +02004136 struct hermes *hw = &priv->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 unsigned long flags;
Christoph Hellwig620554e2005-06-19 01:27:33 +02004138 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
Christoph Hellwig620554e2005-06-19 01:27:33 +02004140 if (!priv->open)
4141 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142
Christoph Hellwig620554e2005-06-19 01:27:33 +02004143 if (priv->broken_disableport) {
4144 orinoco_reset(dev);
4145 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147
Christoph Hellwig620554e2005-06-19 01:27:33 +02004148 if (orinoco_lock(priv, &flags) != 0)
4149 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150
Christoph Hellwig620554e2005-06-19 01:27:33 +02004151 err = hermes_disable_port(hw, 0);
4152 if (err) {
4153 printk(KERN_WARNING "%s: Unable to disable port "
4154 "while reconfiguring card\n", dev->name);
4155 priv->broken_disableport = 1;
4156 goto out;
4157 }
4158
4159 err = __orinoco_program_rids(dev);
4160 if (err) {
4161 printk(KERN_WARNING "%s: Unable to reconfigure card\n",
4162 dev->name);
4163 goto out;
4164 }
4165
4166 err = hermes_enable_port(hw, 0);
4167 if (err) {
4168 printk(KERN_WARNING "%s: Unable to enable port while reconfiguring card\n",
4169 dev->name);
4170 goto out;
4171 }
4172
4173 out:
4174 if (err) {
4175 printk(KERN_WARNING "%s: Resetting instead...\n", dev->name);
4176 schedule_work(&priv->reset_work);
4177 err = 0;
4178 }
4179
4180 orinoco_unlock(priv, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 return err;
4182}
4183
Christoph Hellwig620554e2005-06-19 01:27:33 +02004184static const struct iw_priv_args orinoco_privtab[] = {
4185 { SIOCIWFIRSTPRIV + 0x0, 0, 0, "force_reset" },
4186 { SIOCIWFIRSTPRIV + 0x1, 0, 0, "card_reset" },
4187 { SIOCIWFIRSTPRIV + 0x2, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
4188 0, "set_port3" },
4189 { SIOCIWFIRSTPRIV + 0x3, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
4190 "get_port3" },
4191 { SIOCIWFIRSTPRIV + 0x4, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
4192 0, "set_preamble" },
4193 { SIOCIWFIRSTPRIV + 0x5, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
4194 "get_preamble" },
4195 { SIOCIWFIRSTPRIV + 0x6, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
4196 0, "set_ibssport" },
4197 { SIOCIWFIRSTPRIV + 0x7, 0, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
4198 "get_ibssport" },
4199 { SIOCIWFIRSTPRIV + 0x9, 0, IW_PRIV_TYPE_BYTE | MAX_RID_LEN,
4200 "get_rid" },
4201};
4202
4203
4204/*
4205 * Structures to export the Wireless Handlers
4206 */
4207
4208static const iw_handler orinoco_handler[] = {
Peter Hagervall6b9b97c2005-07-27 01:14:46 -07004209 [SIOCSIWCOMMIT-SIOCIWFIRST] = (iw_handler) orinoco_ioctl_commit,
4210 [SIOCGIWNAME -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getname,
4211 [SIOCSIWFREQ -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setfreq,
4212 [SIOCGIWFREQ -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getfreq,
4213 [SIOCSIWMODE -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setmode,
4214 [SIOCGIWMODE -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getmode,
4215 [SIOCSIWSENS -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setsens,
4216 [SIOCGIWSENS -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getsens,
4217 [SIOCGIWRANGE -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getiwrange,
Pavel Roskin343c6862005-09-09 18:43:02 -04004218 [SIOCSIWSPY -SIOCIWFIRST] = (iw_handler) iw_handler_set_spy,
4219 [SIOCGIWSPY -SIOCIWFIRST] = (iw_handler) iw_handler_get_spy,
4220 [SIOCSIWTHRSPY-SIOCIWFIRST] = (iw_handler) iw_handler_set_thrspy,
4221 [SIOCGIWTHRSPY-SIOCIWFIRST] = (iw_handler) iw_handler_get_thrspy,
Peter Hagervall6b9b97c2005-07-27 01:14:46 -07004222 [SIOCSIWAP -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setwap,
4223 [SIOCGIWAP -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getwap,
4224 [SIOCSIWSCAN -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setscan,
4225 [SIOCGIWSCAN -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getscan,
4226 [SIOCSIWESSID -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setessid,
4227 [SIOCGIWESSID -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getessid,
4228 [SIOCSIWNICKN -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setnick,
4229 [SIOCGIWNICKN -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getnick,
4230 [SIOCSIWRATE -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setrate,
4231 [SIOCGIWRATE -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getrate,
4232 [SIOCSIWRTS -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setrts,
4233 [SIOCGIWRTS -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getrts,
4234 [SIOCSIWFRAG -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setfrag,
4235 [SIOCGIWFRAG -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getfrag,
4236 [SIOCGIWRETRY -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getretry,
4237 [SIOCSIWENCODE-SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setiwencode,
4238 [SIOCGIWENCODE-SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getiwencode,
4239 [SIOCSIWPOWER -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_setpower,
4240 [SIOCGIWPOWER -SIOCIWFIRST] = (iw_handler) orinoco_ioctl_getpower,
Christoph Hellwig620554e2005-06-19 01:27:33 +02004241};
4242
4243
4244/*
4245 Added typecasting since we no longer use iwreq_data -- Moustafa
4246 */
4247static const iw_handler orinoco_private_handler[] = {
Peter Hagervall6b9b97c2005-07-27 01:14:46 -07004248 [0] = (iw_handler) orinoco_ioctl_reset,
4249 [1] = (iw_handler) orinoco_ioctl_reset,
4250 [2] = (iw_handler) orinoco_ioctl_setport3,
4251 [3] = (iw_handler) orinoco_ioctl_getport3,
4252 [4] = (iw_handler) orinoco_ioctl_setpreamble,
4253 [5] = (iw_handler) orinoco_ioctl_getpreamble,
4254 [6] = (iw_handler) orinoco_ioctl_setibssport,
4255 [7] = (iw_handler) orinoco_ioctl_getibssport,
4256 [9] = (iw_handler) orinoco_ioctl_getrid,
Christoph Hellwig620554e2005-06-19 01:27:33 +02004257};
4258
4259static const struct iw_handler_def orinoco_handler_def = {
4260 .num_standard = ARRAY_SIZE(orinoco_handler),
4261 .num_private = ARRAY_SIZE(orinoco_private_handler),
4262 .num_private_args = ARRAY_SIZE(orinoco_privtab),
4263 .standard = orinoco_handler,
4264 .private = orinoco_private_handler,
4265 .private_args = orinoco_privtab,
Pavel Roskin343c6862005-09-09 18:43:02 -04004266 .get_wireless_stats = orinoco_get_wireless_stats,
Christoph Hellwig620554e2005-06-19 01:27:33 +02004267};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268
Christoph Hellwig1fab2e82005-06-19 01:27:40 +02004269static void orinoco_get_drvinfo(struct net_device *dev,
4270 struct ethtool_drvinfo *info)
4271{
4272 struct orinoco_private *priv = netdev_priv(dev);
4273
4274 strncpy(info->driver, DRIVER_NAME, sizeof(info->driver) - 1);
4275 strncpy(info->version, DRIVER_VERSION, sizeof(info->version) - 1);
4276 strncpy(info->fw_version, priv->fw_name, sizeof(info->fw_version) - 1);
4277 if (dev->class_dev.dev)
4278 strncpy(info->bus_info, dev->class_dev.dev->bus_id,
4279 sizeof(info->bus_info) - 1);
4280 else
4281 snprintf(info->bus_info, sizeof(info->bus_info) - 1,
4282 "PCMCIA %p", priv->hw.iobase);
4283}
4284
4285static struct ethtool_ops orinoco_ethtool_ops = {
4286 .get_drvinfo = orinoco_get_drvinfo,
4287 .get_link = ethtool_op_get_link,
4288};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289
4290/********************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291/* Module initialization */
4292/********************************************************************/
4293
4294EXPORT_SYMBOL(alloc_orinocodev);
4295EXPORT_SYMBOL(free_orinocodev);
4296
4297EXPORT_SYMBOL(__orinoco_up);
4298EXPORT_SYMBOL(__orinoco_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299EXPORT_SYMBOL(orinoco_reinit_firmware);
4300
4301EXPORT_SYMBOL(orinoco_interrupt);
4302
4303/* Can't be declared "const" or the whole __initdata section will
4304 * become const */
4305static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
4306 " (David Gibson <hermes@gibson.dropbear.id.au>, "
4307 "Pavel Roskin <proski@gnu.org>, et al)";
4308
4309static int __init init_orinoco(void)
4310{
4311 printk(KERN_DEBUG "%s\n", version);
4312 return 0;
4313}
4314
4315static void __exit exit_orinoco(void)
4316{
4317}
4318
4319module_init(init_orinoco);
4320module_exit(exit_orinoco);