]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/wireless/orinoco/main.c
drivers/net: use __packed annotation
[net-next-2.6.git] / drivers / net / wireless / orinoco / main.c
CommitLineData
47445cb9 1/* main.c - (formerly known as dldwd_cs.c, orinoco_cs.c and orinoco.c)
1da177e4
LT
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/*
1da177e4 49 * TODO
1da177e4
LT
50 * o Handle de-encapsulation within network layer, provide 802.11
51 * headers (patch from Thomas 'Dent' Mirlacher)
1da177e4
LT
52 * 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
1da177e4
LT
79#include <linux/module.h>
80#include <linux/kernel.h>
5a0e3ad6 81#include <linux/slab.h>
1da177e4 82#include <linux/init.h>
d03032af 83#include <linux/delay.h>
8e638267 84#include <linux/device.h>
1da177e4 85#include <linux/netdevice.h>
1da177e4 86#include <linux/etherdevice.h>
39d1ffee 87#include <linux/suspend.h>
9c974fb1 88#include <linux/if_arp.h>
1da177e4 89#include <linux/wireless.h>
2c706002 90#include <linux/ieee80211.h>
620554e4 91#include <net/iw_handler.h>
ea60a6aa 92#include <net/cfg80211.h>
1da177e4 93
1da177e4 94#include "hermes_rid.h"
3994d502 95#include "hermes_dld.h"
712a4342 96#include "hw.h"
fb791b1c 97#include "scan.h"
4adb474b 98#include "mic.h"
37a2e566 99#include "fw.h"
cb1576a8 100#include "wext.h"
ea60a6aa 101#include "cfg.h"
cb1576a8 102#include "main.h"
fb791b1c 103
1da177e4 104#include "orinoco.h"
1da177e4
LT
105
106/********************************************************************/
107/* Module information */
108/********************************************************************/
109
b2f30a0a
DK
110MODULE_AUTHOR("Pavel Roskin <proski@gnu.org> & "
111 "David Gibson <hermes@gibson.dropbear.id.au>");
112MODULE_DESCRIPTION("Driver for Lucent Orinoco, Prism II based "
113 "and similar wireless cards");
1da177e4
LT
114MODULE_LICENSE("Dual MPL/GPL");
115
116/* Level of debugging. Used in the macros in orinoco.h */
117#ifdef ORINOCO_DEBUG
118int orinoco_debug = ORINOCO_DEBUG;
2131266d 119EXPORT_SYMBOL(orinoco_debug);
1da177e4
LT
120module_param(orinoco_debug, int, 0644);
121MODULE_PARM_DESC(orinoco_debug, "Debug level");
1da177e4
LT
122#endif
123
124static int suppress_linkstatus; /* = 0 */
125module_param(suppress_linkstatus, bool, 0644);
126MODULE_PARM_DESC(suppress_linkstatus, "Don't log link status changes");
b2f30a0a 127
7bb7c3a3
DG
128static int ignore_disconnect; /* = 0 */
129module_param(ignore_disconnect, int, 0644);
b2f30a0a
DK
130MODULE_PARM_DESC(ignore_disconnect,
131 "Don't report lost link to the network layer");
1da177e4 132
cb1576a8 133int force_monitor; /* = 0 */
98c4cae1
CH
134module_param(force_monitor, int, 0644);
135MODULE_PARM_DESC(force_monitor, "Allow monitor mode for all firmware versions");
136
1da177e4
LT
137/********************************************************************/
138/* Internal constants */
139/********************************************************************/
140
95dd91fb
CH
141/* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
142static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
143#define ENCAPS_OVERHEAD (sizeof(encaps_hdr) + 2)
144
1da177e4 145#define ORINOCO_MIN_MTU 256
2c706002 146#define ORINOCO_MAX_MTU (IEEE80211_MAX_DATA_LEN - ENCAPS_OVERHEAD)
1da177e4 147
1da177e4
LT
148#define MAX_IRQLOOPS_PER_IRQ 10
149#define MAX_IRQLOOPS_PER_JIFFY (20000/HZ) /* Based on a guestimate of
150 * how many events the
151 * device could
152 * legitimately generate */
1da177e4
LT
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 \
a94e8427 163 | HERMES_EV_INFDROP)
1da177e4 164
1da177e4
LT
165/********************************************************************/
166/* Data types */
167/********************************************************************/
168
30c2d3b4
PR
169/* Beginning of the Tx descriptor, used in TxExc handling */
170struct hermes_txexc_data {
171 struct hermes_tx_descriptor desc;
d133ae4c
PR
172 __le16 frame_ctl;
173 __le16 duration_id;
95dd91fb 174 u8 addr1[ETH_ALEN];
ba2d3587 175} __packed;
1da177e4 176
8f2abf44 177/* Rx frame header except compatibility 802.3 header */
1da177e4 178struct hermes_rx_descriptor {
8f2abf44 179 /* Control */
d133ae4c
PR
180 __le16 status;
181 __le32 time;
1da177e4
LT
182 u8 silence;
183 u8 signal;
184 u8 rate;
185 u8 rxflow;
d133ae4c 186 __le32 reserved;
8f2abf44
CH
187
188 /* 802.11 header */
d133ae4c
PR
189 __le16 frame_ctl;
190 __le16 duration_id;
8f2abf44
CH
191 u8 addr1[ETH_ALEN];
192 u8 addr2[ETH_ALEN];
193 u8 addr3[ETH_ALEN];
d133ae4c 194 __le16 seq_ctl;
8f2abf44
CH
195 u8 addr4[ETH_ALEN];
196
197 /* Data length */
d133ae4c 198 __le16 data_len;
ba2d3587 199} __packed;
1da177e4 200
47166791
DK
201struct orinoco_rx_data {
202 struct hermes_rx_descriptor *desc;
203 struct sk_buff *skb;
204 struct list_head list;
205};
206
c63cdbe8
DK
207struct orinoco_scan_data {
208 void *buf;
209 size_t len;
210 int type;
211 struct list_head list;
212};
213
1da177e4
LT
214/********************************************************************/
215/* Function prototypes */
216/********************************************************************/
217
721aa2f7 218static int __orinoco_set_multicast_list(struct net_device *dev);
6415f7df
DK
219static int __orinoco_up(struct orinoco_private *priv);
220static int __orinoco_down(struct orinoco_private *priv);
721aa2f7 221static int __orinoco_commit(struct orinoco_private *priv);
1da177e4
LT
222
223/********************************************************************/
224/* Internal helper functions */
225/********************************************************************/
226
cb1576a8 227void set_port_type(struct orinoco_private *priv)
1da177e4
LT
228{
229 switch (priv->iw_mode) {
5217c571 230 case NL80211_IFTYPE_STATION:
1da177e4
LT
231 priv->port_type = 1;
232 priv->createibss = 0;
233 break;
5217c571 234 case NL80211_IFTYPE_ADHOC:
1da177e4
LT
235 if (priv->prefer_port3) {
236 priv->port_type = 3;
237 priv->createibss = 0;
238 } else {
239 priv->port_type = priv->ibss_port;
240 priv->createibss = 1;
241 }
242 break;
5217c571 243 case NL80211_IFTYPE_MONITOR:
98c4cae1
CH
244 priv->port_type = 3;
245 priv->createibss = 0;
246 break;
1da177e4
LT
247 default:
248 printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
249 priv->ndev->name);
250 }
251}
252
253/********************************************************************/
254/* Device methods */
255/********************************************************************/
256
593ef09c 257int orinoco_open(struct net_device *dev)
1da177e4 258{
ea60a6aa 259 struct orinoco_private *priv = ndev_priv(dev);
1da177e4
LT
260 unsigned long flags;
261 int err;
262
263 if (orinoco_lock(priv, &flags) != 0)
264 return -EBUSY;
265
a2608362 266 err = __orinoco_up(priv);
1da177e4 267
a94e8427 268 if (!err)
1da177e4
LT
269 priv->open = 1;
270
271 orinoco_unlock(priv, &flags);
272
273 return err;
274}
593ef09c 275EXPORT_SYMBOL(orinoco_open);
1da177e4 276
593ef09c 277int orinoco_stop(struct net_device *dev)
1da177e4 278{
ea60a6aa 279 struct orinoco_private *priv = ndev_priv(dev);
1da177e4
LT
280 int err = 0;
281
282 /* We mustn't use orinoco_lock() here, because we need to be
283 able to close the interface even if hw_unavailable is set
284 (e.g. as we're released after a PC Card removal) */
bcad6e80 285 orinoco_lock_irq(priv);
1da177e4
LT
286
287 priv->open = 0;
288
a2608362 289 err = __orinoco_down(priv);
1da177e4 290
bcad6e80 291 orinoco_unlock_irq(priv);
1da177e4
LT
292
293 return err;
294}
593ef09c 295EXPORT_SYMBOL(orinoco_stop);
1da177e4 296
593ef09c 297struct net_device_stats *orinoco_get_stats(struct net_device *dev)
1da177e4 298{
ea60a6aa 299 struct orinoco_private *priv = ndev_priv(dev);
6fe9deb1 300
1da177e4
LT
301 return &priv->stats;
302}
593ef09c 303EXPORT_SYMBOL(orinoco_get_stats);
1da177e4 304
593ef09c 305void orinoco_set_multicast_list(struct net_device *dev)
1da177e4 306{
ea60a6aa 307 struct orinoco_private *priv = ndev_priv(dev);
1da177e4
LT
308 unsigned long flags;
309
310 if (orinoco_lock(priv, &flags) != 0) {
311 printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
312 "called when hw_unavailable\n", dev->name);
313 return;
314 }
315
316 __orinoco_set_multicast_list(dev);
317 orinoco_unlock(priv, &flags);
318}
593ef09c 319EXPORT_SYMBOL(orinoco_set_multicast_list);
1da177e4 320
593ef09c 321int orinoco_change_mtu(struct net_device *dev, int new_mtu)
1da177e4 322{
ea60a6aa 323 struct orinoco_private *priv = ndev_priv(dev);
1da177e4 324
a94e8427 325 if ((new_mtu < ORINOCO_MIN_MTU) || (new_mtu > ORINOCO_MAX_MTU))
1da177e4
LT
326 return -EINVAL;
327
2c706002 328 /* MTU + encapsulation + header length */
a94e8427
DK
329 if ((new_mtu + ENCAPS_OVERHEAD + sizeof(struct ieee80211_hdr)) >
330 (priv->nicbuf_size - ETH_HLEN))
1da177e4
LT
331 return -EINVAL;
332
333 dev->mtu = new_mtu;
334
335 return 0;
336}
593ef09c 337EXPORT_SYMBOL(orinoco_change_mtu);
1da177e4
LT
338
339/********************************************************************/
340/* Tx path */
341/********************************************************************/
342
bac6fafd
DK
343/* Add encapsulation and MIC to the existing SKB.
344 * The main xmit routine will then send the whole lot to the card.
345 * Need 8 bytes headroom
346 * Need 8 bytes tailroom
347 *
348 * With encapsulated ethernet II frame
349 * --------
350 * 803.3 header (14 bytes)
351 * dst[6]
352 * -------- src[6]
353 * 803.3 header (14 bytes) len[2]
354 * dst[6] 803.2 header (8 bytes)
355 * src[6] encaps[6]
356 * len[2] <- leave alone -> len[2]
357 * -------- -------- <-- 0
358 * Payload Payload
359 * ... ...
360 *
361 * -------- --------
362 * MIC (8 bytes)
363 * --------
364 *
365 * returns 0 on success, -ENOMEM on error.
366 */
367int orinoco_process_xmit_skb(struct sk_buff *skb,
368 struct net_device *dev,
369 struct orinoco_private *priv,
370 int *tx_control,
371 u8 *mic_buf)
372{
373 struct orinoco_tkip_key *key;
374 struct ethhdr *eh;
375 int do_mic;
376
377 key = (struct orinoco_tkip_key *) priv->keys[priv->tx_key].key;
378
379 do_mic = ((priv->encode_alg == ORINOCO_ALG_TKIP) &&
380 (key != NULL));
381
382 if (do_mic)
383 *tx_control |= (priv->tx_key << HERMES_MIC_KEY_ID_SHIFT) |
384 HERMES_TXCTRL_MIC;
385
386 eh = (struct ethhdr *)skb->data;
387
388 /* Encapsulate Ethernet-II frames */
389 if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
390 struct header_struct {
391 struct ethhdr eth; /* 802.3 header */
392 u8 encap[6]; /* 802.2 header */
ba2d3587 393 } __packed hdr;
bac6fafd
DK
394 int len = skb->len + sizeof(encaps_hdr) - (2 * ETH_ALEN);
395
396 if (skb_headroom(skb) < ENCAPS_OVERHEAD) {
397 if (net_ratelimit())
398 printk(KERN_ERR
399 "%s: Not enough headroom for 802.2 headers %d\n",
400 dev->name, skb_headroom(skb));
401 return -ENOMEM;
402 }
403
404 /* Fill in new header */
405 memcpy(&hdr.eth, eh, 2 * ETH_ALEN);
406 hdr.eth.h_proto = htons(len);
407 memcpy(hdr.encap, encaps_hdr, sizeof(encaps_hdr));
408
409 /* Make room for the new header, and copy it in */
410 eh = (struct ethhdr *) skb_push(skb, ENCAPS_OVERHEAD);
411 memcpy(eh, &hdr, sizeof(hdr));
412 }
413
414 /* Calculate Michael MIC */
415 if (do_mic) {
416 size_t len = skb->len - ETH_HLEN;
417 u8 *mic = &mic_buf[0];
418
419 /* Have to write to an even address, so copy the spare
420 * byte across */
421 if (skb->len % 2) {
422 *mic = skb->data[skb->len - 1];
423 mic++;
424 }
425
426 orinoco_mic(priv->tx_tfm_mic, key->tx_mic,
427 eh->h_dest, eh->h_source, 0 /* priority */,
428 skb->data + ETH_HLEN,
429 len, mic);
430 }
431
432 return 0;
433}
434EXPORT_SYMBOL(orinoco_process_xmit_skb);
435
d0cf9c0d 436static netdev_tx_t orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
1da177e4 437{
ea60a6aa 438 struct orinoco_private *priv = ndev_priv(dev);
1da177e4
LT
439 struct net_device_stats *stats = &priv->stats;
440 hermes_t *hw = &priv->hw;
441 int err = 0;
442 u16 txfid = priv->txfid;
6eecad77 443 int tx_control;
1da177e4 444 unsigned long flags;
bac6fafd 445 u8 mic_buf[MICHAEL_MIC_LEN+1];
1da177e4 446
a94e8427 447 if (!netif_running(dev)) {
1da177e4
LT
448 printk(KERN_ERR "%s: Tx on stopped device!\n",
449 dev->name);
b34b867e 450 return NETDEV_TX_BUSY;
1da177e4 451 }
6fe9deb1 452
1da177e4 453 if (netif_queue_stopped(dev)) {
6fe9deb1 454 printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
1da177e4 455 dev->name);
b34b867e 456 return NETDEV_TX_BUSY;
1da177e4 457 }
6fe9deb1 458
1da177e4
LT
459 if (orinoco_lock(priv, &flags) != 0) {
460 printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n",
461 dev->name);
b34b867e 462 return NETDEV_TX_BUSY;
1da177e4
LT
463 }
464
5217c571
DK
465 if (!netif_carrier_ok(dev) ||
466 (priv->iw_mode == NL80211_IFTYPE_MONITOR)) {
1da177e4 467 /* Oops, the firmware hasn't established a connection,
6fe9deb1
DK
468 silently drop the packet (this seems to be the
469 safest approach). */
470e2aa6 470 goto drop;
1da177e4
LT
471 }
472
8d5be088 473 /* Check packet length */
a28dc81d 474 if (skb->len < ETH_HLEN)
470e2aa6 475 goto drop;
1da177e4 476
6eecad77
DK
477 tx_control = HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX;
478
bac6fafd
DK
479 err = orinoco_process_xmit_skb(skb, dev, priv, &tx_control,
480 &mic_buf[0]);
481 if (err)
482 goto drop;
23edcc41 483
6eecad77
DK
484 if (priv->has_alt_txcntl) {
485 /* WPA enabled firmwares have tx_cntl at the end of
486 * the 802.11 header. So write zeroed descriptor and
487 * 802.11 header at the same time
488 */
489 char desc[HERMES_802_3_OFFSET];
490 __le16 *txcntl = (__le16 *) &desc[HERMES_TXCNTL2_OFFSET];
491
492 memset(&desc, 0, sizeof(desc));
493
494 *txcntl = cpu_to_le16(tx_control);
b42f2074
DK
495 err = hw->ops->bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
496 txfid, 0);
6eecad77
DK
497 if (err) {
498 if (net_ratelimit())
499 printk(KERN_ERR "%s: Error %d writing Tx "
500 "descriptor to BAP\n", dev->name, err);
501 goto busy;
502 }
503 } else {
504 struct hermes_tx_descriptor desc;
505
506 memset(&desc, 0, sizeof(desc));
507
508 desc.tx_control = cpu_to_le16(tx_control);
b42f2074
DK
509 err = hw->ops->bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
510 txfid, 0);
6eecad77
DK
511 if (err) {
512 if (net_ratelimit())
513 printk(KERN_ERR "%s: Error %d writing Tx "
514 "descriptor to BAP\n", dev->name, err);
515 goto busy;
516 }
1da177e4 517
6eecad77
DK
518 /* Clear the 802.11 header and data length fields - some
519 * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
520 * if this isn't done. */
521 hermes_clear_words(hw, HERMES_DATA0,
522 HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
523 }
1da177e4 524
b42f2074
DK
525 err = hw->ops->bap_pwrite(hw, USER_BAP, skb->data, skb->len,
526 txfid, HERMES_802_3_OFFSET);
1da177e4
LT
527 if (err) {
528 printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
529 dev->name, err);
470e2aa6 530 goto busy;
1da177e4
LT
531 }
532
bac6fafd
DK
533 if (tx_control & HERMES_TXCTRL_MIC) {
534 size_t offset = HERMES_802_3_OFFSET + skb->len;
535 size_t len = MICHAEL_MIC_LEN;
23edcc41 536
bac6fafd
DK
537 if (offset % 2) {
538 offset--;
539 len++;
23edcc41 540 }
b42f2074 541 err = hw->ops->bap_pwrite(hw, USER_BAP, &mic_buf[0], len,
bac6fafd 542 txfid, offset);
23edcc41
DK
543 if (err) {
544 printk(KERN_ERR "%s: Error %d writing MIC to BAP\n",
545 dev->name, err);
546 goto busy;
547 }
548 }
549
1da177e4
LT
550 /* Finally, we actually initiate the send */
551 netif_stop_queue(dev);
552
b42f2074 553 err = hw->ops->cmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
1da177e4
LT
554 txfid, NULL);
555 if (err) {
556 netif_start_queue(dev);
c367c21c
AM
557 if (net_ratelimit())
558 printk(KERN_ERR "%s: Error %d transmitting packet\n",
559 dev->name, err);
470e2aa6 560 goto busy;
1da177e4
LT
561 }
562
23edcc41 563 stats->tx_bytes += HERMES_802_3_OFFSET + skb->len;
470e2aa6 564 goto ok;
1da177e4 565
470e2aa6
PR
566 drop:
567 stats->tx_errors++;
568 stats->tx_dropped++;
1da177e4 569
470e2aa6
PR
570 ok:
571 orinoco_unlock(priv, &flags);
1da177e4 572 dev_kfree_skb(skb);
b34b867e 573 return NETDEV_TX_OK;
1da177e4 574
470e2aa6 575 busy:
2c1bd260
JB
576 if (err == -EIO)
577 schedule_work(&priv->reset_work);
1da177e4 578 orinoco_unlock(priv, &flags);
b34b867e 579 return NETDEV_TX_BUSY;
1da177e4
LT
580}
581
582static void __orinoco_ev_alloc(struct net_device *dev, hermes_t *hw)
583{
ea60a6aa 584 struct orinoco_private *priv = ndev_priv(dev);
1da177e4
LT
585 u16 fid = hermes_read_regn(hw, ALLOCFID);
586
587 if (fid != priv->txfid) {
588 if (fid != DUMMY_FID)
589 printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
590 dev->name, fid);
591 return;
592 }
593
594 hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
595}
596
597static void __orinoco_ev_tx(struct net_device *dev, hermes_t *hw)
598{
ea60a6aa 599 struct orinoco_private *priv = ndev_priv(dev);
1da177e4
LT
600 struct net_device_stats *stats = &priv->stats;
601
602 stats->tx_packets++;
603
604 netif_wake_queue(dev);
605
606 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
607}
608
609static void __orinoco_ev_txexc(struct net_device *dev, hermes_t *hw)
610{
ea60a6aa 611 struct orinoco_private *priv = ndev_priv(dev);
1da177e4
LT
612 struct net_device_stats *stats = &priv->stats;
613 u16 fid = hermes_read_regn(hw, TXCOMPLFID);
d133ae4c 614 u16 status;
30c2d3b4 615 struct hermes_txexc_data hdr;
1da177e4
LT
616 int err = 0;
617
618 if (fid == DUMMY_FID)
619 return; /* Nothing's really happened */
620
48ca7038 621 /* Read part of the frame header - we need status and addr1 */
b42f2074
DK
622 err = hw->ops->bap_pread(hw, IRQ_BAP, &hdr,
623 sizeof(struct hermes_txexc_data),
624 fid, 0);
95dd91fb
CH
625
626 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
627 stats->tx_errors++;
628
1da177e4
LT
629 if (err) {
630 printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
631 "(FID=%04X error %d)\n",
632 dev->name, fid, err);
95dd91fb 633 return;
1da177e4 634 }
6fe9deb1 635
95dd91fb
CH
636 DEBUG(1, "%s: Tx error, err %d (FID=%04X)\n", dev->name,
637 err, fid);
6fe9deb1 638
95dd91fb
CH
639 /* We produce a TXDROP event only for retry or lifetime
640 * exceeded, because that's the only status that really mean
641 * that this particular node went away.
642 * Other errors means that *we* screwed up. - Jean II */
30c2d3b4 643 status = le16_to_cpu(hdr.desc.status);
d133ae4c 644 if (status & (HERMES_TXSTAT_RETRYERR | HERMES_TXSTAT_AGEDERR)) {
95dd91fb
CH
645 union iwreq_data wrqu;
646
647 /* Copy 802.11 dest address.
648 * We use the 802.11 header because the frame may
649 * not be 802.3 or may be mangled...
650 * In Ad-Hoc mode, it will be the node address.
651 * In managed mode, it will be most likely the AP addr
652 * User space will figure out how to convert it to
653 * whatever it needs (IP address or else).
654 * - Jean II */
655 memcpy(wrqu.addr.sa_data, hdr.addr1, ETH_ALEN);
656 wrqu.addr.sa_family = ARPHRD_ETHER;
657
658 /* Send event to user space */
659 wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
660 }
1da177e4
LT
661
662 netif_wake_queue(dev);
1da177e4
LT
663}
664
593ef09c 665void orinoco_tx_timeout(struct net_device *dev)
1da177e4 666{
ea60a6aa 667 struct orinoco_private *priv = ndev_priv(dev);
1da177e4
LT
668 struct net_device_stats *stats = &priv->stats;
669 struct hermes *hw = &priv->hw;
670
671 printk(KERN_WARNING "%s: Tx timeout! "
672 "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
673 dev->name, hermes_read_regn(hw, ALLOCFID),
674 hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
675
676 stats->tx_errors++;
677
678 schedule_work(&priv->reset_work);
679}
593ef09c 680EXPORT_SYMBOL(orinoco_tx_timeout);
1da177e4
LT
681
682/********************************************************************/
683/* Rx path (data frames) */
684/********************************************************************/
685
686/* Does the frame have a SNAP header indicating it should be
687 * de-encapsulated to Ethernet-II? */
688static inline int is_ethersnap(void *_hdr)
689{
690 u8 *hdr = _hdr;
691
692 /* We de-encapsulate all packets which, a) have SNAP headers
693 * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
694 * and where b) the OUI of the SNAP header is 00:00:00 or
695 * 00:00:f8 - we need both because different APs appear to use
696 * different OUIs for some reason */
697 return (memcmp(hdr, &encaps_hdr, 5) == 0)
a94e8427 698 && ((hdr[5] == 0x00) || (hdr[5] == 0xf8));
1da177e4
LT
699}
700
701static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
702 int level, int noise)
703{
343c686c
PR
704 struct iw_quality wstats;
705 wstats.level = level - 0x95;
706 wstats.noise = noise - 0x95;
707 wstats.qual = (level > noise) ? (level - noise) : 0;
f941f859 708 wstats.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
343c686c
PR
709 /* Update spy records */
710 wireless_spy_update(dev, mac, &wstats);
1da177e4
LT
711}
712
713static void orinoco_stat_gather(struct net_device *dev,
714 struct sk_buff *skb,
715 struct hermes_rx_descriptor *desc)
716{
ea60a6aa 717 struct orinoco_private *priv = ndev_priv(dev);
1da177e4
LT
718
719 /* Using spy support with lots of Rx packets, like in an
720 * infrastructure (AP), will really slow down everything, because
721 * the MAC address must be compared to each entry of the spy list.
722 * If the user really asks for it (set some address in the
723 * spy list), we do it, but he will pay the price.
724 * Note that to get here, you need both WIRELESS_SPY
725 * compiled in AND some addresses in the list !!!
726 */
727 /* Note : gcc will optimise the whole section away if
728 * WIRELESS_SPY is not defined... - Jean II */
729 if (SPY_NUMBER(priv)) {
98e399f8 730 orinoco_spy_gather(dev, skb_mac_header(skb) + ETH_ALEN,
1da177e4
LT
731 desc->signal, desc->silence);
732 }
733}
734
98c4cae1
CH
735/*
736 * orinoco_rx_monitor - handle received monitor frames.
737 *
738 * Arguments:
739 * dev network device
740 * rxfid received FID
741 * desc rx descriptor of the frame
742 *
743 * Call context: interrupt
744 */
745static void orinoco_rx_monitor(struct net_device *dev, u16 rxfid,
746 struct hermes_rx_descriptor *desc)
747{
748 u32 hdrlen = 30; /* return full header by default */
749 u32 datalen = 0;
750 u16 fc;
751 int err;
752 int len;
753 struct sk_buff *skb;
ea60a6aa 754 struct orinoco_private *priv = ndev_priv(dev);
98c4cae1
CH
755 struct net_device_stats *stats = &priv->stats;
756 hermes_t *hw = &priv->hw;
757
758 len = le16_to_cpu(desc->data_len);
759
760 /* Determine the size of the header and the data */
761 fc = le16_to_cpu(desc->frame_ctl);
762 switch (fc & IEEE80211_FCTL_FTYPE) {
763 case IEEE80211_FTYPE_DATA:
764 if ((fc & IEEE80211_FCTL_TODS)
765 && (fc & IEEE80211_FCTL_FROMDS))
766 hdrlen = 30;
767 else
768 hdrlen = 24;
769 datalen = len;
770 break;
771 case IEEE80211_FTYPE_MGMT:
772 hdrlen = 24;
773 datalen = len;
774 break;
775 case IEEE80211_FTYPE_CTL:
776 switch (fc & IEEE80211_FCTL_STYPE) {
777 case IEEE80211_STYPE_PSPOLL:
778 case IEEE80211_STYPE_RTS:
779 case IEEE80211_STYPE_CFEND:
780 case IEEE80211_STYPE_CFENDACK:
781 hdrlen = 16;
782 break;
783 case IEEE80211_STYPE_CTS:
784 case IEEE80211_STYPE_ACK:
785 hdrlen = 10;
786 break;
787 }
788 break;
789 default:
790 /* Unknown frame type */
791 break;
792 }
793
794 /* sanity check the length */
2c706002 795 if (datalen > IEEE80211_MAX_DATA_LEN + 12) {
98c4cae1
CH
796 printk(KERN_DEBUG "%s: oversized monitor frame, "
797 "data length = %d\n", dev->name, datalen);
98c4cae1
CH
798 stats->rx_length_errors++;
799 goto update_stats;
800 }
801
802 skb = dev_alloc_skb(hdrlen + datalen);
803 if (!skb) {
804 printk(KERN_WARNING "%s: Cannot allocate skb for monitor frame\n",
805 dev->name);
bb6e093d 806 goto update_stats;
98c4cae1
CH
807 }
808
809 /* Copy the 802.11 header to the skb */
810 memcpy(skb_put(skb, hdrlen), &(desc->frame_ctl), hdrlen);
459a98ed 811 skb_reset_mac_header(skb);
98c4cae1
CH
812
813 /* If any, copy the data from the card to the skb */
814 if (datalen > 0) {
b42f2074
DK
815 err = hw->ops->bap_pread(hw, IRQ_BAP, skb_put(skb, datalen),
816 ALIGN(datalen, 2), rxfid,
817 HERMES_802_2_OFFSET);
98c4cae1
CH
818 if (err) {
819 printk(KERN_ERR "%s: error %d reading monitor frame\n",
820 dev->name, err);
821 goto drop;
822 }
823 }
824
825 skb->dev = dev;
826 skb->ip_summed = CHECKSUM_NONE;
827 skb->pkt_type = PACKET_OTHERHOST;
c1b4aa3f 828 skb->protocol = cpu_to_be16(ETH_P_802_2);
6fe9deb1 829
98c4cae1
CH
830 stats->rx_packets++;
831 stats->rx_bytes += skb->len;
832
833 netif_rx(skb);
834 return;
835
836 drop:
837 dev_kfree_skb_irq(skb);
838 update_stats:
839 stats->rx_errors++;
840 stats->rx_dropped++;
841}
842
9afac70a 843void __orinoco_ev_rx(struct net_device *dev, hermes_t *hw)
1da177e4 844{
ea60a6aa 845 struct orinoco_private *priv = ndev_priv(dev);
1da177e4
LT
846 struct net_device_stats *stats = &priv->stats;
847 struct iw_statistics *wstats = &priv->wstats;
848 struct sk_buff *skb = NULL;
31afcef3 849 u16 rxfid, status;
8f2abf44 850 int length;
31afcef3
DK
851 struct hermes_rx_descriptor *desc;
852 struct orinoco_rx_data *rx_data;
1da177e4
LT
853 int err;
854
31afcef3
DK
855 desc = kmalloc(sizeof(*desc), GFP_ATOMIC);
856 if (!desc) {
857 printk(KERN_WARNING
858 "%s: Can't allocate space for RX descriptor\n",
859 dev->name);
860 goto update_stats;
861 }
862
1da177e4
LT
863 rxfid = hermes_read_regn(hw, RXFID);
864
b42f2074
DK
865 err = hw->ops->bap_pread(hw, IRQ_BAP, desc, sizeof(*desc),
866 rxfid, 0);
1da177e4
LT
867 if (err) {
868 printk(KERN_ERR "%s: error %d reading Rx descriptor. "
869 "Frame dropped.\n", dev->name, err);
98c4cae1 870 goto update_stats;
1da177e4
LT
871 }
872
31afcef3 873 status = le16_to_cpu(desc->status);
1da177e4 874
98c4cae1
CH
875 if (status & HERMES_RXSTAT_BADCRC) {
876 DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n",
877 dev->name);
878 stats->rx_crc_errors++;
879 goto update_stats;
1da177e4
LT
880 }
881
98c4cae1 882 /* Handle frames in monitor mode */
5217c571 883 if (priv->iw_mode == NL80211_IFTYPE_MONITOR) {
31afcef3
DK
884 orinoco_rx_monitor(dev, rxfid, desc);
885 goto out;
98c4cae1 886 }
1da177e4 887
98c4cae1
CH
888 if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
889 DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
890 dev->name);
891 wstats->discard.code++;
892 goto update_stats;
1da177e4
LT
893 }
894
31afcef3 895 length = le16_to_cpu(desc->data_len);
8f2abf44 896
1da177e4
LT
897 /* Sanity checks */
898 if (length < 3) { /* No for even an 802.2 LLC header */
899 /* At least on Symbol firmware with PCF we get quite a
6fe9deb1
DK
900 lot of these legitimately - Poll frames with no
901 data. */
31afcef3 902 goto out;
1da177e4 903 }
2c706002 904 if (length > IEEE80211_MAX_DATA_LEN) {
1da177e4
LT
905 printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
906 dev->name, length);
907 stats->rx_length_errors++;
98c4cae1 908 goto update_stats;
1da177e4
LT
909 }
910
23edcc41
DK
911 /* Payload size does not include Michael MIC. Increase payload
912 * size to read it together with the data. */
913 if (status & HERMES_RXSTAT_MIC)
914 length += MICHAEL_MIC_LEN;
915
1da177e4
LT
916 /* We need space for the packet data itself, plus an ethernet
917 header, plus 2 bytes so we can align the IP header on a
918 32bit boundary, plus 1 byte so we can read in odd length
919 packets from the card, which has an IO granularity of 16
6fe9deb1 920 bits */
1da177e4
LT
921 skb = dev_alloc_skb(length+ETH_HLEN+2+1);
922 if (!skb) {
923 printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
924 dev->name);
98c4cae1 925 goto update_stats;
1da177e4
LT
926 }
927
8f2abf44
CH
928 /* We'll prepend the header, so reserve space for it. The worst
929 case is no decapsulation, when 802.3 header is prepended and
930 nothing is removed. 2 is for aligning the IP header. */
931 skb_reserve(skb, ETH_HLEN + 2);
932
b42f2074
DK
933 err = hw->ops->bap_pread(hw, IRQ_BAP, skb_put(skb, length),
934 ALIGN(length, 2), rxfid,
935 HERMES_802_2_OFFSET);
8f2abf44
CH
936 if (err) {
937 printk(KERN_ERR "%s: error %d reading frame. "
938 "Frame dropped.\n", dev->name, err);
8f2abf44
CH
939 goto drop;
940 }
1da177e4 941
31afcef3
DK
942 /* Add desc and skb to rx queue */
943 rx_data = kzalloc(sizeof(*rx_data), GFP_ATOMIC);
944 if (!rx_data) {
945 printk(KERN_WARNING "%s: Can't allocate RX packet\n",
946 dev->name);
947 goto drop;
948 }
949 rx_data->desc = desc;
950 rx_data->skb = skb;
951 list_add_tail(&rx_data->list, &priv->rx_list);
952 tasklet_schedule(&priv->rx_tasklet);
953
954 return;
955
956drop:
957 dev_kfree_skb_irq(skb);
958update_stats:
959 stats->rx_errors++;
960 stats->rx_dropped++;
961out:
962 kfree(desc);
963}
9afac70a 964EXPORT_SYMBOL(__orinoco_ev_rx);
31afcef3
DK
965
966static void orinoco_rx(struct net_device *dev,
967 struct hermes_rx_descriptor *desc,
968 struct sk_buff *skb)
969{
ea60a6aa 970 struct orinoco_private *priv = ndev_priv(dev);
31afcef3
DK
971 struct net_device_stats *stats = &priv->stats;
972 u16 status, fc;
973 int length;
974 struct ethhdr *hdr;
975
976 status = le16_to_cpu(desc->status);
977 length = le16_to_cpu(desc->data_len);
978 fc = le16_to_cpu(desc->frame_ctl);
979
23edcc41
DK
980 /* Calculate and check MIC */
981 if (status & HERMES_RXSTAT_MIC) {
4af198fb 982 struct orinoco_tkip_key *key;
23edcc41
DK
983 int key_id = ((status & HERMES_RXSTAT_MIC_KEY_ID) >>
984 HERMES_MIC_KEY_ID_SHIFT);
985 u8 mic[MICHAEL_MIC_LEN];
986 u8 *rxmic;
987 u8 *src = (fc & IEEE80211_FCTL_FROMDS) ?
988 desc->addr3 : desc->addr2;
989
990 /* Extract Michael MIC from payload */
991 rxmic = skb->data + skb->len - MICHAEL_MIC_LEN;
992
993 skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
994 length -= MICHAEL_MIC_LEN;
995
4af198fb
DK
996 key = (struct orinoco_tkip_key *) priv->keys[key_id].key;
997
998 if (!key) {
999 printk(KERN_WARNING "%s: Received encrypted frame from "
1000 "%pM using key %i, but key is not installed\n",
1001 dev->name, src, key_id);
1002 goto drop;
1003 }
1004
1005 orinoco_mic(priv->rx_tfm_mic, key->rx_mic, desc->addr1, src,
23edcc41 1006 0, /* priority or QoS? */
4af198fb 1007 skb->data, skb->len, &mic[0]);
23edcc41
DK
1008
1009 if (memcmp(mic, rxmic,
1010 MICHAEL_MIC_LEN)) {
1011 union iwreq_data wrqu;
1012 struct iw_michaelmicfailure wxmic;
23edcc41
DK
1013
1014 printk(KERN_WARNING "%s: "
e174961c 1015 "Invalid Michael MIC in data frame from %pM, "
23edcc41 1016 "using key %i\n",
e174961c 1017 dev->name, src, key_id);
23edcc41
DK
1018
1019 /* TODO: update stats */
1020
1021 /* Notify userspace */
1022 memset(&wxmic, 0, sizeof(wxmic));
1023 wxmic.flags = key_id & IW_MICFAILURE_KEY_ID;
1024 wxmic.flags |= (desc->addr1[0] & 1) ?
1025 IW_MICFAILURE_GROUP : IW_MICFAILURE_PAIRWISE;
1026 wxmic.src_addr.sa_family = ARPHRD_ETHER;
1027 memcpy(wxmic.src_addr.sa_data, src, ETH_ALEN);
1028
1029 (void) orinoco_hw_get_tkip_iv(priv, key_id,
1030 &wxmic.tsc[0]);
1031
1032 memset(&wrqu, 0, sizeof(wrqu));
1033 wrqu.data.length = sizeof(wxmic);
1034 wireless_send_event(dev, IWEVMICHAELMICFAILURE, &wrqu,
1035 (char *) &wxmic);
1036
1037 goto drop;
1038 }
1039 }
1040
1da177e4
LT
1041 /* Handle decapsulation
1042 * In most cases, the firmware tell us about SNAP frames.
1043 * For some reason, the SNAP frames sent by LinkSys APs
1044 * are not properly recognised by most firmwares.
1045 * So, check ourselves */
8f2abf44
CH
1046 if (length >= ENCAPS_OVERHEAD &&
1047 (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
1048 ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
1049 is_ethersnap(skb->data))) {
1da177e4
LT
1050 /* These indicate a SNAP within 802.2 LLC within
1051 802.11 frame which we'll need to de-encapsulate to
1052 the original EthernetII frame. */
b2f30a0a
DK
1053 hdr = (struct ethhdr *)skb_push(skb,
1054 ETH_HLEN - ENCAPS_OVERHEAD);
1da177e4 1055 } else {
8f2abf44
CH
1056 /* 802.3 frame - prepend 802.3 header as is */
1057 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN);
1058 hdr->h_proto = htons(length);
1da177e4 1059 }
31afcef3 1060 memcpy(hdr->h_dest, desc->addr1, ETH_ALEN);
8f2abf44 1061 if (fc & IEEE80211_FCTL_FROMDS)
31afcef3 1062 memcpy(hdr->h_source, desc->addr3, ETH_ALEN);
8f2abf44 1063 else
31afcef3 1064 memcpy(hdr->h_source, desc->addr2, ETH_ALEN);
1da177e4 1065
1da177e4
LT
1066 skb->protocol = eth_type_trans(skb, dev);
1067 skb->ip_summed = CHECKSUM_NONE;
8f2abf44
CH
1068 if (fc & IEEE80211_FCTL_TODS)
1069 skb->pkt_type = PACKET_OTHERHOST;
6fe9deb1 1070
1da177e4 1071 /* Process the wireless stats if needed */
31afcef3 1072 orinoco_stat_gather(dev, skb, desc);
1da177e4
LT
1073
1074 /* Pass the packet to the networking stack */
1075 netif_rx(skb);
1076 stats->rx_packets++;
1077 stats->rx_bytes += length;
1078
1079 return;
23edcc41
DK
1080
1081 drop:
1082 dev_kfree_skb(skb);
1083 stats->rx_errors++;
1084 stats->rx_dropped++;
31afcef3 1085}
1da177e4 1086
31afcef3
DK
1087static void orinoco_rx_isr_tasklet(unsigned long data)
1088{
5381956b
DK
1089 struct orinoco_private *priv = (struct orinoco_private *) data;
1090 struct net_device *dev = priv->ndev;
31afcef3
DK
1091 struct orinoco_rx_data *rx_data, *temp;
1092 struct hermes_rx_descriptor *desc;
1093 struct sk_buff *skb;
20953ad6
DK
1094 unsigned long flags;
1095
1096 /* orinoco_rx requires the driver lock, and we also need to
1097 * protect priv->rx_list, so just hold the lock over the
1098 * lot.
1099 *
1100 * If orinoco_lock fails, we've unplugged the card. In this
1101 * case just abort. */
1102 if (orinoco_lock(priv, &flags) != 0)
1103 return;
31afcef3
DK
1104
1105 /* extract desc and skb from queue */
1106 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
1107 desc = rx_data->desc;
1108 skb = rx_data->skb;
1109 list_del(&rx_data->list);
1110 kfree(rx_data);
1111
1112 orinoco_rx(dev, desc, skb);
1113
1114 kfree(desc);
1115 }
20953ad6
DK
1116
1117 orinoco_unlock(priv, &flags);
1da177e4
LT
1118}
1119
1120/********************************************************************/
1121/* Rx path (info frames) */
1122/********************************************************************/
1123
1124static void print_linkstatus(struct net_device *dev, u16 status)
1125{
2131266d 1126 char *s;
1da177e4
LT
1127
1128 if (suppress_linkstatus)
1129 return;
1130
1131 switch (status) {
1132 case HERMES_LINKSTATUS_NOT_CONNECTED:
1133 s = "Not Connected";
1134 break;
1135 case HERMES_LINKSTATUS_CONNECTED:
1136 s = "Connected";
1137 break;
1138 case HERMES_LINKSTATUS_DISCONNECTED:
1139 s = "Disconnected";
1140 break;
1141 case HERMES_LINKSTATUS_AP_CHANGE:
1142 s = "AP Changed";
1143 break;
1144 case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
1145 s = "AP Out of Range";
1146 break;
1147 case HERMES_LINKSTATUS_AP_IN_RANGE:
1148 s = "AP In Range";
1149 break;
1150 case HERMES_LINKSTATUS_ASSOC_FAILED:
1151 s = "Association Failed";
1152 break;
1153 default:
1154 s = "UNKNOWN";
1155 }
6fe9deb1 1156
11eaea41 1157 printk(KERN_DEBUG "%s: New link status: %s (%04x)\n",
1da177e4
LT
1158 dev->name, s, status);
1159}
1160
16739b06 1161/* Search scan results for requested BSSID, join it if found */
c4028958 1162static void orinoco_join_ap(struct work_struct *work)
16739b06 1163{
c4028958
DH
1164 struct orinoco_private *priv =
1165 container_of(work, struct orinoco_private, join_work);
1166 struct net_device *dev = priv->ndev;
16739b06
CH
1167 struct hermes *hw = &priv->hw;
1168 int err;
1169 unsigned long flags;
1170 struct join_req {
1171 u8 bssid[ETH_ALEN];
d133ae4c 1172 __le16 channel;
ba2d3587 1173 } __packed req;
16739b06 1174 const int atom_len = offsetof(struct prism2_scan_apinfo, atim);
c89cc225 1175 struct prism2_scan_apinfo *atom = NULL;
16739b06 1176 int offset = 4;
c89cc225 1177 int found = 0;
16739b06
CH
1178 u8 *buf;
1179 u16 len;
1180
1181 /* Allocate buffer for scan results */
1182 buf = kmalloc(MAX_SCAN_LEN, GFP_KERNEL);
a94e8427 1183 if (!buf)
16739b06
CH
1184 return;
1185
1186 if (orinoco_lock(priv, &flags) != 0)
f3cb4cc1 1187 goto fail_lock;
16739b06
CH
1188
1189 /* Sanity checks in case user changed something in the meantime */
a94e8427 1190 if (!priv->bssid_fixed)
16739b06
CH
1191 goto out;
1192
1193 if (strlen(priv->desired_essid) == 0)
1194 goto out;
1195
1196 /* Read scan results from the firmware */
b42f2074
DK
1197 err = hw->ops->read_ltv(hw, USER_BAP,
1198 HERMES_RID_SCANRESULTSTABLE,
1199 MAX_SCAN_LEN, &len, buf);
16739b06
CH
1200 if (err) {
1201 printk(KERN_ERR "%s: Cannot read scan results\n",
1202 dev->name);
1203 goto out;
1204 }
1205
1206 len = HERMES_RECLEN_TO_BYTES(len);
1207
1208 /* Go through the scan results looking for the channel of the AP
1209 * we were requested to join */
1210 for (; offset + atom_len <= len; offset += atom_len) {
1211 atom = (struct prism2_scan_apinfo *) (buf + offset);
c89cc225
PR
1212 if (memcmp(&atom->bssid, priv->desired_bssid, ETH_ALEN) == 0) {
1213 found = 1;
1214 break;
1215 }
16739b06
CH
1216 }
1217
a94e8427 1218 if (!found) {
c89cc225
PR
1219 DEBUG(1, "%s: Requested AP not found in scan results\n",
1220 dev->name);
1221 goto out;
1222 }
16739b06 1223
16739b06
CH
1224 memcpy(req.bssid, priv->desired_bssid, ETH_ALEN);
1225 req.channel = atom->channel; /* both are little-endian */
1226 err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNFJOINREQUEST,
1227 &req);
1228 if (err)
1229 printk(KERN_ERR "%s: Error issuing join request\n", dev->name);
1230
1231 out:
16739b06 1232 orinoco_unlock(priv, &flags);
f3cb4cc1
PR
1233
1234 fail_lock:
1235 kfree(buf);
16739b06
CH
1236}
1237
95dd91fb 1238/* Send new BSSID to userspace */
6cd90b1c 1239static void orinoco_send_bssid_wevent(struct orinoco_private *priv)
95dd91fb 1240{
c4028958 1241 struct net_device *dev = priv->ndev;
95dd91fb
CH
1242 struct hermes *hw = &priv->hw;
1243 union iwreq_data wrqu;
1244 int err;
95dd91fb 1245
b42f2074
DK
1246 err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
1247 ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
95dd91fb 1248 if (err != 0)
6cd90b1c 1249 return;
95dd91fb
CH
1250
1251 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1252
1253 /* Send event to user space */
1254 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
6cd90b1c
DK
1255}
1256
06009fda
DK
1257static void orinoco_send_assocreqie_wevent(struct orinoco_private *priv)
1258{
1259 struct net_device *dev = priv->ndev;
1260 struct hermes *hw = &priv->hw;
1261 union iwreq_data wrqu;
1262 int err;
1263 u8 buf[88];
1264 u8 *ie;
1265
1266 if (!priv->has_wpa)
1267 return;
1268
b42f2074
DK
1269 err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_REQ_INFO,
1270 sizeof(buf), NULL, &buf);
06009fda
DK
1271 if (err != 0)
1272 return;
1273
1274 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1275 if (ie) {
1276 int rem = sizeof(buf) - (ie - &buf[0]);
1277 wrqu.data.length = ie[1] + 2;
1278 if (wrqu.data.length > rem)
1279 wrqu.data.length = rem;
1280
1281 if (wrqu.data.length)
1282 /* Send event to user space */
1283 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, ie);
1284 }
1285}
1286
1287static void orinoco_send_assocrespie_wevent(struct orinoco_private *priv)
1288{
1289 struct net_device *dev = priv->ndev;
1290 struct hermes *hw = &priv->hw;
1291 union iwreq_data wrqu;
1292 int err;
1293 u8 buf[88]; /* TODO: verify max size or IW_GENERIC_IE_MAX */
1294 u8 *ie;
1295
1296 if (!priv->has_wpa)
1297 return;
1298
b42f2074
DK
1299 err = hw->ops->read_ltv(hw, USER_BAP,
1300 HERMES_RID_CURRENT_ASSOC_RESP_INFO,
1301 sizeof(buf), NULL, &buf);
06009fda
DK
1302 if (err != 0)
1303 return;
1304
1305 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1306 if (ie) {
1307 int rem = sizeof(buf) - (ie - &buf[0]);
1308 wrqu.data.length = ie[1] + 2;
1309 if (wrqu.data.length > rem)
1310 wrqu.data.length = rem;
1311
1312 if (wrqu.data.length)
1313 /* Send event to user space */
1314 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, ie);
1315 }
1316}
1317
6cd90b1c
DK
1318static void orinoco_send_wevents(struct work_struct *work)
1319{
1320 struct orinoco_private *priv =
1321 container_of(work, struct orinoco_private, wevent_work);
1322 unsigned long flags;
1323
1324 if (orinoco_lock(priv, &flags) != 0)
1325 return;
1326
06009fda
DK
1327 orinoco_send_assocreqie_wevent(priv);
1328 orinoco_send_assocrespie_wevent(priv);
6cd90b1c 1329 orinoco_send_bssid_wevent(priv);
8aeabc37 1330
95dd91fb
CH
1331 orinoco_unlock(priv, &flags);
1332}
1333
c63cdbe8
DK
1334static void qbuf_scan(struct orinoco_private *priv, void *buf,
1335 int len, int type)
1336{
1337 struct orinoco_scan_data *sd;
1338 unsigned long flags;
1339
1340 sd = kmalloc(sizeof(*sd), GFP_ATOMIC);
1341 sd->buf = buf;
1342 sd->len = len;
1343 sd->type = type;
1344
1345 spin_lock_irqsave(&priv->scan_lock, flags);
1346 list_add_tail(&sd->list, &priv->scan_list);
1347 spin_unlock_irqrestore(&priv->scan_lock, flags);
1348
1349 schedule_work(&priv->process_scan);
1350}
1351
1352static void qabort_scan(struct orinoco_private *priv)
1353{
1354 struct orinoco_scan_data *sd;
1355 unsigned long flags;
1356
1357 sd = kmalloc(sizeof(*sd), GFP_ATOMIC);
1358 sd->len = -1; /* Abort */
1359
1360 spin_lock_irqsave(&priv->scan_lock, flags);
1361 list_add_tail(&sd->list, &priv->scan_list);
1362 spin_unlock_irqrestore(&priv->scan_lock, flags);
1363
1364 schedule_work(&priv->process_scan);
1365}
1366
1367static void orinoco_process_scan_results(struct work_struct *work)
1368{
1369 struct orinoco_private *priv =
1370 container_of(work, struct orinoco_private, process_scan);
1371 struct orinoco_scan_data *sd, *temp;
1372 unsigned long flags;
1373 void *buf;
1374 int len;
1375 int type;
1376
1377 spin_lock_irqsave(&priv->scan_lock, flags);
1378 list_for_each_entry_safe(sd, temp, &priv->scan_list, list) {
1379 spin_unlock_irqrestore(&priv->scan_lock, flags);
1380
1381 buf = sd->buf;
1382 len = sd->len;
1383 type = sd->type;
1384
1385 list_del(&sd->list);
1386 kfree(sd);
1387
1388 if (len > 0) {
1389 if (type == HERMES_INQ_CHANNELINFO)
1390 orinoco_add_extscan_result(priv, buf, len);
1391 else
1392 orinoco_add_hostscan_results(priv, buf, len);
1393
1394 kfree(buf);
1395 } else if (priv->scan_request) {
1396 /* Either abort or complete the scan */
1397 cfg80211_scan_done(priv->scan_request, (len < 0));
1398 priv->scan_request = NULL;
1399 }
1400
1401 spin_lock_irqsave(&priv->scan_lock, flags);
1402 }
1403 spin_unlock_irqrestore(&priv->scan_lock, flags);
1404}
1405
9afac70a 1406void __orinoco_ev_info(struct net_device *dev, hermes_t *hw)
1da177e4 1407{
ea60a6aa 1408 struct orinoco_private *priv = ndev_priv(dev);
1da177e4
LT
1409 u16 infofid;
1410 struct {
d133ae4c
PR
1411 __le16 len;
1412 __le16 type;
ba2d3587 1413 } __packed info;
1da177e4
LT
1414 int len, type;
1415 int err;
1416
1417 /* This is an answer to an INQUIRE command that we did earlier,
1418 * or an information "event" generated by the card
1419 * The controller return to us a pseudo frame containing
1420 * the information in question - Jean II */
1421 infofid = hermes_read_regn(hw, INFOFID);
1422
1423 /* Read the info frame header - don't try too hard */
b42f2074
DK
1424 err = hw->ops->bap_pread(hw, IRQ_BAP, &info, sizeof(info),
1425 infofid, 0);
1da177e4
LT
1426 if (err) {
1427 printk(KERN_ERR "%s: error %d reading info frame. "
1428 "Frame dropped.\n", dev->name, err);
1429 return;
1430 }
6fe9deb1 1431
1da177e4
LT
1432 len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
1433 type = le16_to_cpu(info.type);
1434
1435 switch (type) {
1436 case HERMES_INQ_TALLIES: {
1437 struct hermes_tallies_frame tallies;
1438 struct iw_statistics *wstats = &priv->wstats;
6fe9deb1 1439
1da177e4
LT
1440 if (len > sizeof(tallies)) {
1441 printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
1442 dev->name, len);
1443 len = sizeof(tallies);
1444 }
6fe9deb1 1445
b42f2074
DK
1446 err = hw->ops->bap_pread(hw, IRQ_BAP, &tallies, len,
1447 infofid, sizeof(info));
84d8a2fb
CH
1448 if (err)
1449 break;
6fe9deb1 1450
1da177e4
LT
1451 /* Increment our various counters */
1452 /* wstats->discard.nwid - no wrong BSSID stuff */
1453 wstats->discard.code +=
1454 le16_to_cpu(tallies.RxWEPUndecryptable);
6fe9deb1 1455 if (len == sizeof(tallies))
1da177e4
LT
1456 wstats->discard.code +=
1457 le16_to_cpu(tallies.RxDiscards_WEPICVError) +
1458 le16_to_cpu(tallies.RxDiscards_WEPExcluded);
1459 wstats->discard.misc +=
1460 le16_to_cpu(tallies.TxDiscardsWrongSA);
1461 wstats->discard.fragment +=
1462 le16_to_cpu(tallies.RxMsgInBadMsgFragments);
1463 wstats->discard.retries +=
1464 le16_to_cpu(tallies.TxRetryLimitExceeded);
1465 /* wstats->miss.beacon - no match */
1466 }
1467 break;
1468 case HERMES_INQ_LINKSTATUS: {
1469 struct hermes_linkstatus linkstatus;
1470 u16 newstatus;
1471 int connected;
1472
5217c571 1473 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
8f2abf44
CH
1474 break;
1475
1da177e4
LT
1476 if (len != sizeof(linkstatus)) {
1477 printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
1478 dev->name, len);
1479 break;
1480 }
1481
b42f2074
DK
1482 err = hw->ops->bap_pread(hw, IRQ_BAP, &linkstatus, len,
1483 infofid, sizeof(info));
84d8a2fb
CH
1484 if (err)
1485 break;
1da177e4
LT
1486 newstatus = le16_to_cpu(linkstatus.linkstatus);
1487
95dd91fb
CH
1488 /* Symbol firmware uses "out of range" to signal that
1489 * the hostscan frame can be requested. */
1490 if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
1491 priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
c63cdbe8 1492 priv->has_hostscan && priv->scan_request) {
95dd91fb
CH
1493 hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
1494 break;
1495 }
1496
1da177e4
LT
1497 connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
1498 || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
1499 || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
1500
1501 if (connected)
1502 netif_carrier_on(dev);
7bb7c3a3 1503 else if (!ignore_disconnect)
1da177e4
LT
1504 netif_carrier_off(dev);
1505
95dd91fb
CH
1506 if (newstatus != priv->last_linkstatus) {
1507 priv->last_linkstatus = newstatus;
1da177e4 1508 print_linkstatus(dev, newstatus);
95dd91fb
CH
1509 /* The info frame contains only one word which is the
1510 * status (see hermes.h). The status is pretty boring
1511 * in itself, that's why we export the new BSSID...
1512 * Jean II */
1513 schedule_work(&priv->wevent_work);
1514 }
1515 }
1516 break;
1517 case HERMES_INQ_SCAN:
c63cdbe8 1518 if (!priv->scan_request && priv->bssid_fixed &&
95dd91fb
CH
1519 priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
1520 schedule_work(&priv->join_work);
1521 break;
1522 }
1523 /* fall through */
1524 case HERMES_INQ_HOSTSCAN:
1525 case HERMES_INQ_HOSTSCAN_SYMBOL: {
1526 /* Result of a scanning. Contains information about
1527 * cells in the vicinity - Jean II */
95dd91fb
CH
1528 unsigned char *buf;
1529
1530 /* Sanity check */
1531 if (len > 4096) {
1532 printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
1533 dev->name, len);
c63cdbe8 1534 qabort_scan(priv);
95dd91fb
CH
1535 break;
1536 }
1537
95dd91fb
CH
1538 /* Allocate buffer for results */
1539 buf = kmalloc(len, GFP_ATOMIC);
c63cdbe8 1540 if (buf == NULL) {
95dd91fb 1541 /* No memory, so can't printk()... */
c63cdbe8 1542 qabort_scan(priv);
95dd91fb 1543 break;
c63cdbe8 1544 }
95dd91fb
CH
1545
1546 /* Read scan data */
b42f2074
DK
1547 err = hw->ops->bap_pread(hw, IRQ_BAP, (void *) buf, len,
1548 infofid, sizeof(info));
708218b0
PR
1549 if (err) {
1550 kfree(buf);
c63cdbe8 1551 qabort_scan(priv);
95dd91fb 1552 break;
708218b0 1553 }
1da177e4 1554
95dd91fb
CH
1555#ifdef ORINOCO_DEBUG
1556 {
1557 int i;
1558 printk(KERN_DEBUG "Scan result [%02X", buf[0]);
a94e8427 1559 for (i = 1; i < (len * 2); i++)
95dd91fb
CH
1560 printk(":%02X", buf[i]);
1561 printk("]\n");
1562 }
1563#endif /* ORINOCO_DEBUG */
1564
c63cdbe8 1565 qbuf_scan(priv, buf, len, type);
1da177e4
LT
1566 }
1567 break;
01632fa4
DK
1568 case HERMES_INQ_CHANNELINFO:
1569 {
1570 struct agere_ext_scan_info *bss;
1571
c63cdbe8 1572 if (!priv->scan_request) {
01632fa4
DK
1573 printk(KERN_DEBUG "%s: Got chaninfo without scan, "
1574 "len=%d\n", dev->name, len);
1575 break;
1576 }
1577
1578 /* An empty result indicates that the scan is complete */
1579 if (len == 0) {
c63cdbe8 1580 qbuf_scan(priv, NULL, len, type);
01632fa4
DK
1581 break;
1582 }
1583
1584 /* Sanity check */
c63cdbe8 1585 else if (len < (offsetof(struct agere_ext_scan_info,
01632fa4
DK
1586 data) + 2)) {
1587 /* Drop this result now so we don't have to
1588 * keep checking later */
1589 printk(KERN_WARNING
1590 "%s: Ext scan results too short (%d bytes)\n",
1591 dev->name, len);
1592 break;
1593 }
1594
c63cdbe8 1595 bss = kmalloc(len, GFP_ATOMIC);
01632fa4
DK
1596 if (bss == NULL)
1597 break;
1598
1599 /* Read scan data */
b42f2074
DK
1600 err = hw->ops->bap_pread(hw, IRQ_BAP, (void *) bss, len,
1601 infofid, sizeof(info));
c63cdbe8 1602 if (err)
01632fa4 1603 kfree(bss);
c63cdbe8
DK
1604 else
1605 qbuf_scan(priv, bss, len, type);
01632fa4 1606
01632fa4
DK
1607 break;
1608 }
95dd91fb
CH
1609 case HERMES_INQ_SEC_STAT_AGERE:
1610 /* Security status (Agere specific) */
1611 /* Ignore this frame for now */
1612 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
1613 break;
1614 /* fall through */
1da177e4
LT
1615 default:
1616 printk(KERN_DEBUG "%s: Unknown information frame received: "
1617 "type 0x%04x, length %d\n", dev->name, type, len);
1618 /* We don't actually do anything about it */
1619 break;
1620 }
1621}
9afac70a 1622EXPORT_SYMBOL(__orinoco_ev_info);
1da177e4
LT
1623
1624static void __orinoco_ev_infdrop(struct net_device *dev, hermes_t *hw)
1625{
1626 if (net_ratelimit())
1627 printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
1628}
1629
1630/********************************************************************/
1631/* Internal hardware control routines */
1632/********************************************************************/
1633
6415f7df 1634static int __orinoco_up(struct orinoco_private *priv)
1da177e4 1635{
a2608362 1636 struct net_device *dev = priv->ndev;
1da177e4
LT
1637 struct hermes *hw = &priv->hw;
1638 int err;
1639
84d8a2fb
CH
1640 netif_carrier_off(dev); /* just to make sure */
1641
721aa2f7 1642 err = __orinoco_commit(priv);
1da177e4
LT
1643 if (err) {
1644 printk(KERN_ERR "%s: Error %d configuring card\n",
1645 dev->name, err);
1646 return err;
1647 }
1648
1649 /* Fire things up again */
1650 hermes_set_irqmask(hw, ORINOCO_INTEN);
1651 err = hermes_enable_port(hw, 0);
1652 if (err) {
1653 printk(KERN_ERR "%s: Error %d enabling MAC port\n",
1654 dev->name, err);
1655 return err;
1656 }
1657
1658 netif_start_queue(dev);
1659
1660 return 0;
1661}
1662
6415f7df 1663static int __orinoco_down(struct orinoco_private *priv)
1da177e4 1664{
a2608362 1665 struct net_device *dev = priv->ndev;
1da177e4
LT
1666 struct hermes *hw = &priv->hw;
1667 int err;
1668
1669 netif_stop_queue(dev);
1670
a94e8427
DK
1671 if (!priv->hw_unavailable) {
1672 if (!priv->broken_disableport) {
1da177e4
LT
1673 err = hermes_disable_port(hw, 0);
1674 if (err) {
1675 /* Some firmwares (e.g. Intersil 1.3.x) seem
1676 * to have problems disabling the port, oh
1677 * well, too bad. */
1678 printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
1679 dev->name, err);
1680 priv->broken_disableport = 1;
1681 }
1682 }
1683 hermes_set_irqmask(hw, 0);
1684 hermes_write_regn(hw, EVACK, 0xffff);
1685 }
6fe9deb1 1686
1da177e4
LT
1687 /* firmware will have to reassociate */
1688 netif_carrier_off(dev);
1689 priv->last_linkstatus = 0xffff;
1690
1691 return 0;
1692}
1693
6415f7df 1694static int orinoco_reinit_firmware(struct orinoco_private *priv)
37a6c611 1695{
37a6c611
PR
1696 struct hermes *hw = &priv->hw;
1697 int err;
1698
b42f2074 1699 err = hw->ops->init(hw);
0df6cbb7
AB
1700 if (priv->do_fw_download && !err) {
1701 err = orinoco_download(priv);
1702 if (err)
1703 priv->do_fw_download = 0;
1704 }
37a6c611 1705 if (!err)
42a51b93 1706 err = orinoco_hw_allocate_fid(priv);
37a6c611
PR
1707
1708 return err;
1709}
1710
721aa2f7 1711static int
5865d015
DK
1712__orinoco_set_multicast_list(struct net_device *dev)
1713{
ea60a6aa 1714 struct orinoco_private *priv = ndev_priv(dev);
5865d015
DK
1715 int err = 0;
1716 int promisc, mc_count;
1717
1718 /* The Hermes doesn't seem to have an allmulti mode, so we go
1719 * into promiscuous mode and let the upper levels deal. */
1720 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
4cd24eaf 1721 (netdev_mc_count(dev) > MAX_MULTICAST(priv))) {
5865d015
DK
1722 promisc = 1;
1723 mc_count = 0;
1724 } else {
1725 promisc = 0;
4cd24eaf 1726 mc_count = netdev_mc_count(dev);
5865d015
DK
1727 }
1728
655ffee2 1729 err = __orinoco_hw_set_multicast_list(priv, dev, mc_count, promisc);
721aa2f7
DK
1730
1731 return err;
1da177e4
LT
1732}
1733
1da177e4
LT
1734/* This must be called from user context, without locks held - use
1735 * schedule_work() */
cb1576a8 1736void orinoco_reset(struct work_struct *work)
1da177e4 1737{
c4028958
DH
1738 struct orinoco_private *priv =
1739 container_of(work, struct orinoco_private, reset_work);
1740 struct net_device *dev = priv->ndev;
1da177e4 1741 struct hermes *hw = &priv->hw;
8551cb98 1742 int err;
1da177e4
LT
1743 unsigned long flags;
1744
1745 if (orinoco_lock(priv, &flags) != 0)
1746 /* When the hardware becomes available again, whatever
1747 * detects that is responsible for re-initializing
1748 * it. So no need for anything further */
1749 return;
1750
1751 netif_stop_queue(dev);
1752
1753 /* Shut off interrupts. Depending on what state the hardware
1754 * is in, this might not work, but we'll try anyway */
1755 hermes_set_irqmask(hw, 0);
1756 hermes_write_regn(hw, EVACK, 0xffff);
1757
1758 priv->hw_unavailable++;
1759 priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
1760 netif_carrier_off(dev);
1761
1762 orinoco_unlock(priv, &flags);
1763
c63cdbe8
DK
1764 /* Scanning support: Notify scan cancellation */
1765 if (priv->scan_request) {
1766 cfg80211_scan_done(priv->scan_request, 1);
1767 priv->scan_request = NULL;
1768 }
95dd91fb 1769
8551cb98 1770 if (priv->hard_reset) {
1da177e4 1771 err = (*priv->hard_reset)(priv);
8551cb98
CH
1772 if (err) {
1773 printk(KERN_ERR "%s: orinoco_reset: Error %d "
1774 "performing hard reset\n", dev->name, err);
1775 goto disable;
1776 }
1da177e4
LT
1777 }
1778
a2608362 1779 err = orinoco_reinit_firmware(priv);
1da177e4
LT
1780 if (err) {
1781 printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
1782 dev->name, err);
8551cb98 1783 goto disable;
1da177e4
LT
1784 }
1785
b2f30a0a 1786 /* This has to be called from user context */
bcad6e80 1787 orinoco_lock_irq(priv);
1da177e4
LT
1788
1789 priv->hw_unavailable--;
1790
1791 /* priv->open or priv->hw_unavailable might have changed while
1792 * we dropped the lock */
a94e8427 1793 if (priv->open && (!priv->hw_unavailable)) {
a2608362 1794 err = __orinoco_up(priv);
1da177e4
LT
1795 if (err) {
1796 printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
1797 dev->name, err);
1798 } else
1799 dev->trans_start = jiffies;
1800 }
1801
bcad6e80 1802 orinoco_unlock_irq(priv);
1da177e4
LT
1803
1804 return;
8551cb98
CH
1805 disable:
1806 hermes_set_irqmask(hw, 0);
1807 netif_device_detach(dev);
1808 printk(KERN_ERR "%s: Device has been disabled!\n", dev->name);
1da177e4
LT
1809}
1810
721aa2f7
DK
1811static int __orinoco_commit(struct orinoco_private *priv)
1812{
1813 struct net_device *dev = priv->ndev;
1814 int err = 0;
1815
1816 err = orinoco_hw_program_rids(priv);
1817
1818 /* FIXME: what about netif_tx_lock */
1819 (void) __orinoco_set_multicast_list(dev);
1820
1821 return err;
1822}
1823
1824/* Ensures configuration changes are applied. May result in a reset.
1825 * The caller should hold priv->lock
1826 */
1827int orinoco_commit(struct orinoco_private *priv)
1828{
1829 struct net_device *dev = priv->ndev;
1830 hermes_t *hw = &priv->hw;
1831 int err;
1832
1833 if (priv->broken_disableport) {
1834 schedule_work(&priv->reset_work);
1835 return 0;
1836 }
1837
1838 err = hermes_disable_port(hw, 0);
1839 if (err) {
1840 printk(KERN_WARNING "%s: Unable to disable port "
1841 "while reconfiguring card\n", dev->name);
1842 priv->broken_disableport = 1;
1843 goto out;
1844 }
1845
1846 err = __orinoco_commit(priv);
1847 if (err) {
1848 printk(KERN_WARNING "%s: Unable to reconfigure card\n",
1849 dev->name);
1850 goto out;
1851 }
1852
1853 err = hermes_enable_port(hw, 0);
1854 if (err) {
1855 printk(KERN_WARNING "%s: Unable to enable port while reconfiguring card\n",
1856 dev->name);
1857 goto out;
1858 }
1859
1860 out:
1861 if (err) {
1862 printk(KERN_WARNING "%s: Resetting instead...\n", dev->name);
1863 schedule_work(&priv->reset_work);
1864 err = 0;
1865 }
1866 return err;
1867}
1868
1da177e4
LT
1869/********************************************************************/
1870/* Interrupt handler */
1871/********************************************************************/
1872
1873static void __orinoco_ev_tick(struct net_device *dev, hermes_t *hw)
1874{
1875 printk(KERN_DEBUG "%s: TICK\n", dev->name);
1876}
1877
1878static void __orinoco_ev_wterr(struct net_device *dev, hermes_t *hw)
1879{
1880 /* This seems to happen a fair bit under load, but ignoring it
1881 seems to work fine...*/
1882 printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
1883 dev->name);
1884}
1885
7d12e780 1886irqreturn_t orinoco_interrupt(int irq, void *dev_id)
1da177e4 1887{
a2608362
DK
1888 struct orinoco_private *priv = dev_id;
1889 struct net_device *dev = priv->ndev;
1da177e4
LT
1890 hermes_t *hw = &priv->hw;
1891 int count = MAX_IRQLOOPS_PER_IRQ;
1892 u16 evstat, events;
2131266d
DK
1893 /* These are used to detect a runaway interrupt situation.
1894 *
1895 * If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
1896 * we panic and shut down the hardware
1897 */
1898 /* jiffies value the last time we were called */
1899 static int last_irq_jiffy; /* = 0 */
1900 static int loops_this_jiffy; /* = 0 */
1da177e4
LT
1901 unsigned long flags;
1902
1903 if (orinoco_lock(priv, &flags) != 0) {
1904 /* If hw is unavailable - we don't know if the irq was
1905 * for us or not */
1906 return IRQ_HANDLED;
1907 }
1908
1909 evstat = hermes_read_regn(hw, EVSTAT);
1910 events = evstat & hw->inten;
a94e8427 1911 if (!events) {
1da177e4
LT
1912 orinoco_unlock(priv, &flags);
1913 return IRQ_NONE;
1914 }
6fe9deb1 1915
1da177e4
LT
1916 if (jiffies != last_irq_jiffy)
1917 loops_this_jiffy = 0;
1918 last_irq_jiffy = jiffies;
1919
1920 while (events && count--) {
1921 if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
1922 printk(KERN_WARNING "%s: IRQ handler is looping too "
1923 "much! Resetting.\n", dev->name);
1924 /* Disable interrupts for now */
1925 hermes_set_irqmask(hw, 0);
1926 schedule_work(&priv->reset_work);
1927 break;
1928 }
1929
1930 /* Check the card hasn't been removed */
a94e8427 1931 if (!hermes_present(hw)) {
1da177e4
LT
1932 DEBUG(0, "orinoco_interrupt(): card removed\n");
1933 break;
1934 }
1935
1936 if (events & HERMES_EV_TICK)
1937 __orinoco_ev_tick(dev, hw);
1938 if (events & HERMES_EV_WTERR)
1939 __orinoco_ev_wterr(dev, hw);
1940 if (events & HERMES_EV_INFDROP)
1941 __orinoco_ev_infdrop(dev, hw);
1942 if (events & HERMES_EV_INFO)
1943 __orinoco_ev_info(dev, hw);
1944 if (events & HERMES_EV_RX)
1945 __orinoco_ev_rx(dev, hw);
1946 if (events & HERMES_EV_TXEXC)
1947 __orinoco_ev_txexc(dev, hw);
1948 if (events & HERMES_EV_TX)
1949 __orinoco_ev_tx(dev, hw);
1950 if (events & HERMES_EV_ALLOC)
1951 __orinoco_ev_alloc(dev, hw);
6fe9deb1 1952
84d8a2fb 1953 hermes_write_regn(hw, EVACK, evstat);
1da177e4
LT
1954
1955 evstat = hermes_read_regn(hw, EVSTAT);
1956 events = evstat & hw->inten;
1957 };
1958
1959 orinoco_unlock(priv, &flags);
1960 return IRQ_HANDLED;
1961}
2131266d 1962EXPORT_SYMBOL(orinoco_interrupt);
1da177e4 1963
39d1ffee
DK
1964/********************************************************************/
1965/* Power management */
1966/********************************************************************/
1967#if defined(CONFIG_PM_SLEEP) && !defined(CONFIG_HERMES_CACHE_FW_ON_INIT)
1968static int orinoco_pm_notifier(struct notifier_block *notifier,
1969 unsigned long pm_event,
1970 void *unused)
1971{
1972 struct orinoco_private *priv = container_of(notifier,
1973 struct orinoco_private,
1974 pm_notifier);
1975
1976 /* All we need to do is cache the firmware before suspend, and
1977 * release it when we come out.
1978 *
1979 * Only need to do this if we're downloading firmware. */
1980 if (!priv->do_fw_download)
1981 return NOTIFY_DONE;
1982
1983 switch (pm_event) {
1984 case PM_HIBERNATION_PREPARE:
1985 case PM_SUSPEND_PREPARE:
1986 orinoco_cache_fw(priv, 0);
1987 break;
1988
1989 case PM_POST_RESTORE:
1990 /* Restore from hibernation failed. We need to clean
1991 * up in exactly the same way, so fall through. */
1992 case PM_POST_HIBERNATION:
1993 case PM_POST_SUSPEND:
1994 orinoco_uncache_fw(priv);
1995 break;
1996
1997 case PM_RESTORE_PREPARE:
1998 default:
1999 break;
2000 }
2001
2002 return NOTIFY_DONE;
2003}
f11c179e
DM
2004
2005static void orinoco_register_pm_notifier(struct orinoco_private *priv)
2006{
2007 priv->pm_notifier.notifier_call = orinoco_pm_notifier;
2008 register_pm_notifier(&priv->pm_notifier);
2009}
2010
2011static void orinoco_unregister_pm_notifier(struct orinoco_private *priv)
2012{
2013 unregister_pm_notifier(&priv->pm_notifier);
2014}
39d1ffee 2015#else /* !PM_SLEEP || HERMES_CACHE_FW_ON_INIT */
f11c179e
DM
2016#define orinoco_register_pm_notifier(priv) do { } while(0)
2017#define orinoco_unregister_pm_notifier(priv) do { } while(0)
39d1ffee
DK
2018#endif
2019
1da177e4
LT
2020/********************************************************************/
2021/* Initialization */
2022/********************************************************************/
2023
8e638267 2024int orinoco_init(struct orinoco_private *priv)
1da177e4 2025{
8e638267 2026 struct device *dev = priv->dev;
ea60a6aa 2027 struct wiphy *wiphy = priv_to_wiphy(priv);
1da177e4
LT
2028 hermes_t *hw = &priv->hw;
2029 int err = 0;
1da177e4 2030
1da177e4
LT
2031 /* No need to lock, the hw_unavailable flag is already set in
2032 * alloc_orinocodev() */
2c706002 2033 priv->nicbuf_size = IEEE80211_MAX_FRAME_LEN + ETH_HLEN;
1da177e4
LT
2034
2035 /* Initialize the firmware */
b42f2074 2036 err = hw->ops->init(hw);
1da177e4 2037 if (err != 0) {
8e638267
DK
2038 dev_err(dev, "Failed to initialize firmware (err = %d)\n",
2039 err);
1da177e4
LT
2040 goto out;
2041 }
2042
3414fc3f
DK
2043 err = determine_fw_capabilities(priv, wiphy->fw_version,
2044 sizeof(wiphy->fw_version),
2045 &wiphy->hw_version);
1da177e4 2046 if (err != 0) {
8e638267 2047 dev_err(dev, "Incompatible firmware, aborting\n");
1da177e4
LT
2048 goto out;
2049 }
2050
3994d502 2051 if (priv->do_fw_download) {
39d1ffee 2052#ifdef CONFIG_HERMES_CACHE_FW_ON_INIT
74734312 2053 orinoco_cache_fw(priv, 0);
39d1ffee 2054#endif
74734312 2055
3994d502
DK
2056 err = orinoco_download(priv);
2057 if (err)
2058 priv->do_fw_download = 0;
2059
2060 /* Check firmware version again */
3414fc3f
DK
2061 err = determine_fw_capabilities(priv, wiphy->fw_version,
2062 sizeof(wiphy->fw_version),
2063 &wiphy->hw_version);
3994d502 2064 if (err != 0) {
8e638267 2065 dev_err(dev, "Incompatible firmware, aborting\n");
3994d502
DK
2066 goto out;
2067 }
2068 }
2069
1da177e4 2070 if (priv->has_port3)
8e638267 2071 dev_info(dev, "Ad-hoc demo mode supported\n");
1da177e4 2072 if (priv->has_ibss)
8e638267
DK
2073 dev_info(dev, "IEEE standard IBSS ad-hoc mode supported\n");
2074 if (priv->has_wep)
2075 dev_info(dev, "WEP supported, %s-bit key\n",
2076 priv->has_big_wep ? "104" : "40");
23edcc41 2077 if (priv->has_wpa) {
8e638267 2078 dev_info(dev, "WPA-PSK supported\n");
23edcc41 2079 if (orinoco_mic_init(priv)) {
8e638267
DK
2080 dev_err(dev, "Failed to setup MIC crypto algorithm. "
2081 "Disabling WPA support\n");
23edcc41
DK
2082 priv->has_wpa = 0;
2083 }
2084 }
1da177e4 2085
ea60a6aa 2086 err = orinoco_hw_read_card_settings(priv, wiphy->perm_addr);
e9e3d010 2087 if (err)
1da177e4 2088 goto out;
1da177e4 2089
42a51b93 2090 err = orinoco_hw_allocate_fid(priv);
37a6c611 2091 if (err) {
8e638267 2092 dev_err(dev, "Failed to allocate NIC buffer!\n");
37a6c611
PR
2093 goto out;
2094 }
2095
1da177e4 2096 /* Set up the default configuration */
5217c571 2097 priv->iw_mode = NL80211_IFTYPE_STATION;
1da177e4 2098 /* By default use IEEE/IBSS ad-hoc mode if we have it */
a94e8427 2099 priv->prefer_port3 = priv->has_port3 && (!priv->has_ibss);
1da177e4 2100 set_port_type(priv);
d51d8b1f 2101 priv->channel = 0; /* use firmware default */
1da177e4
LT
2102
2103 priv->promiscuous = 0;
5c9f41e2 2104 priv->encode_alg = ORINOCO_ALG_NONE;
1da177e4 2105 priv->tx_key = 0;
d03032af
DK
2106 priv->wpa_enabled = 0;
2107 priv->tkip_cm_active = 0;
2108 priv->key_mgmt = 0;
2109 priv->wpa_ie_len = 0;
2110 priv->wpa_ie = NULL;
1da177e4 2111
ea60a6aa
DK
2112 if (orinoco_wiphy_register(wiphy)) {
2113 err = -ENODEV;
2114 goto out;
2115 }
2116
1da177e4
LT
2117 /* Make the hardware available, as long as it hasn't been
2118 * removed elsewhere (e.g. by PCMCIA hot unplug) */
bcad6e80 2119 orinoco_lock_irq(priv);
1da177e4 2120 priv->hw_unavailable--;
bcad6e80 2121 orinoco_unlock_irq(priv);
1da177e4 2122
8e638267 2123 dev_dbg(dev, "Ready\n");
1da177e4
LT
2124
2125 out:
1da177e4
LT
2126 return err;
2127}
8e638267 2128EXPORT_SYMBOL(orinoco_init);
1da177e4 2129
89ea4090 2130static const struct net_device_ops orinoco_netdev_ops = {
89ea4090
AB
2131 .ndo_open = orinoco_open,
2132 .ndo_stop = orinoco_stop,
2133 .ndo_start_xmit = orinoco_xmit,
2134 .ndo_set_multicast_list = orinoco_set_multicast_list,
2135 .ndo_change_mtu = orinoco_change_mtu,
240c102d
BH
2136 .ndo_set_mac_address = eth_mac_addr,
2137 .ndo_validate_addr = eth_validate_addr,
89ea4090
AB
2138 .ndo_tx_timeout = orinoco_tx_timeout,
2139 .ndo_get_stats = orinoco_get_stats,
2140};
2141
ea60a6aa
DK
2142/* Allocate private data.
2143 *
2144 * This driver has a number of structures associated with it
2145 * netdev - Net device structure for each network interface
2146 * wiphy - structure associated with wireless phy
2147 * wireless_dev (wdev) - structure for each wireless interface
2148 * hw - structure for hermes chip info
2149 * card - card specific structure for use by the card driver
2150 * (airport, orinoco_cs)
2151 * priv - orinoco private data
2152 * device - generic linux device structure
2153 *
2154 * +---------+ +---------+
2155 * | wiphy | | netdev |
2156 * | +-------+ | +-------+
2157 * | | priv | | | wdev |
2158 * | | +-----+ +-+-------+
2159 * | | | hw |
2160 * | +-+-----+
2161 * | | card |
2162 * +-+-------+
2163 *
2164 * priv has a link to netdev and device
2165 * wdev has a link to wiphy
2166 */
a2608362 2167struct orinoco_private
3994d502
DK
2168*alloc_orinocodev(int sizeof_card,
2169 struct device *device,
2170 int (*hard_reset)(struct orinoco_private *),
2171 int (*stop_fw)(struct orinoco_private *, int))
1da177e4 2172{
1da177e4 2173 struct orinoco_private *priv;
ea60a6aa
DK
2174 struct wiphy *wiphy;
2175
2176 /* allocate wiphy
2177 * NOTE: We only support a single virtual interface
2178 * but this may change when monitor mode is added
2179 */
2180 wiphy = wiphy_new(&orinoco_cfg_ops,
2181 sizeof(struct orinoco_private) + sizeof_card);
2182 if (!wiphy)
2183 return NULL;
1da177e4 2184
ea60a6aa 2185 priv = wiphy_priv(wiphy);
5381956b 2186 priv->dev = device;
ea60a6aa 2187
1da177e4 2188 if (sizeof_card)
84d8a2fb 2189 priv->card = (void *)((unsigned long)priv
1da177e4
LT
2190 + sizeof(struct orinoco_private));
2191 else
2192 priv->card = NULL;
2193
ea60a6aa
DK
2194 orinoco_wiphy_init(wiphy);
2195
343c686c
PR
2196#ifdef WIRELESS_SPY
2197 priv->wireless_data.spy_data = &priv->spy_data;
343c686c 2198#endif
23edcc41 2199
1da177e4 2200 /* Set up default callbacks */
1da177e4 2201 priv->hard_reset = hard_reset;
3994d502 2202 priv->stop_fw = stop_fw;
1da177e4
LT
2203
2204 spin_lock_init(&priv->lock);
2205 priv->open = 0;
2206 priv->hw_unavailable = 1; /* orinoco_init() must clear this
2207 * before anything else touches the
2208 * hardware */
c4028958
DH
2209 INIT_WORK(&priv->reset_work, orinoco_reset);
2210 INIT_WORK(&priv->join_work, orinoco_join_ap);
2211 INIT_WORK(&priv->wevent_work, orinoco_send_wevents);
1da177e4 2212
31afcef3
DK
2213 INIT_LIST_HEAD(&priv->rx_list);
2214 tasklet_init(&priv->rx_tasklet, orinoco_rx_isr_tasklet,
5381956b 2215 (unsigned long) priv);
31afcef3 2216
c63cdbe8
DK
2217 spin_lock_init(&priv->scan_lock);
2218 INIT_LIST_HEAD(&priv->scan_list);
2219 INIT_WORK(&priv->process_scan, orinoco_process_scan_results);
2220
1da177e4
LT
2221 priv->last_linkstatus = 0xffff;
2222
2bfc5cb5 2223#if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
2cea7b26 2224 priv->cached_pri_fw = NULL;
4fb30784 2225 priv->cached_fw = NULL;
2bfc5cb5 2226#endif
4fb30784 2227
39d1ffee 2228 /* Register PM notifiers */
f11c179e 2229 orinoco_register_pm_notifier(priv);
39d1ffee 2230
a2608362 2231 return priv;
1da177e4 2232}
2131266d 2233EXPORT_SYMBOL(alloc_orinocodev);
1da177e4 2234
5381956b
DK
2235/* We can only support a single interface. We provide a separate
2236 * function to set it up to distinguish between hardware
2237 * initialisation and interface setup.
2238 *
2239 * The base_addr and irq parameters are passed on to netdev for use
2240 * with SIOCGIFMAP.
2241 */
2242int orinoco_if_add(struct orinoco_private *priv,
2243 unsigned long base_addr,
593ef09c
DK
2244 unsigned int irq,
2245 const struct net_device_ops *ops)
5381956b
DK
2246{
2247 struct wiphy *wiphy = priv_to_wiphy(priv);
2248 struct wireless_dev *wdev;
2249 struct net_device *dev;
2250 int ret;
2251
2252 dev = alloc_etherdev(sizeof(struct wireless_dev));
2253
2254 if (!dev)
2255 return -ENOMEM;
2256
2257 /* Initialise wireless_dev */
2258 wdev = netdev_priv(dev);
2259 wdev->wiphy = wiphy;
2260 wdev->iftype = NL80211_IFTYPE_STATION;
2261
2262 /* Setup / override net_device fields */
2263 dev->ieee80211_ptr = wdev;
5381956b 2264 dev->watchdog_timeo = HZ; /* 1 second timeout */
5381956b
DK
2265 dev->wireless_handlers = &orinoco_handler_def;
2266#ifdef WIRELESS_SPY
2267 dev->wireless_data = &priv->wireless_data;
2268#endif
593ef09c
DK
2269 /* Default to standard ops if not set */
2270 if (ops)
2271 dev->netdev_ops = ops;
2272 else
2273 dev->netdev_ops = &orinoco_netdev_ops;
2274
5381956b
DK
2275 /* we use the default eth_mac_addr for setting the MAC addr */
2276
2277 /* Reserve space in skb for the SNAP header */
bac6fafd 2278 dev->needed_headroom = ENCAPS_OVERHEAD;
5381956b
DK
2279
2280 netif_carrier_off(dev);
2281
2282 memcpy(dev->dev_addr, wiphy->perm_addr, ETH_ALEN);
cf32ed92 2283 memcpy(dev->perm_addr, wiphy->perm_addr, ETH_ALEN);
5381956b
DK
2284
2285 dev->base_addr = base_addr;
2286 dev->irq = irq;
2287
2288 SET_NETDEV_DEV(dev, priv->dev);
2289 ret = register_netdev(dev);
2290 if (ret)
2291 goto fail;
2292
2293 priv->ndev = dev;
2294
2295 /* Report what we've done */
2296 dev_dbg(priv->dev, "Registerred interface %s.\n", dev->name);
2297
2298 return 0;
2299
2300 fail:
2301 free_netdev(dev);
2302 return ret;
2303}
2304EXPORT_SYMBOL(orinoco_if_add);
2305
2306void orinoco_if_del(struct orinoco_private *priv)
1da177e4 2307{
a2608362 2308 struct net_device *dev = priv->ndev;
5381956b
DK
2309
2310 unregister_netdev(dev);
2311 free_netdev(dev);
2312}
2313EXPORT_SYMBOL(orinoco_if_del);
2314
2315void free_orinocodev(struct orinoco_private *priv)
2316{
ea60a6aa 2317 struct wiphy *wiphy = priv_to_wiphy(priv);
20953ad6 2318 struct orinoco_rx_data *rx_data, *temp;
c63cdbe8 2319 struct orinoco_scan_data *sd, *sdtemp;
95dd91fb 2320
ea60a6aa
DK
2321 wiphy_unregister(wiphy);
2322
20953ad6
DK
2323 /* If the tasklet is scheduled when we call tasklet_kill it
2324 * will run one final time. However the tasklet will only
2325 * drain priv->rx_list if the hw is still available. */
31afcef3 2326 tasklet_kill(&priv->rx_tasklet);
74734312 2327
20953ad6
DK
2328 /* Explicitly drain priv->rx_list */
2329 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
2330 list_del(&rx_data->list);
2331
2332 dev_kfree_skb(rx_data->skb);
2333 kfree(rx_data->desc);
2334 kfree(rx_data);
2335 }
2336
c63cdbe8
DK
2337 cancel_work_sync(&priv->process_scan);
2338 /* Explicitly drain priv->scan_list */
2339 list_for_each_entry_safe(sd, sdtemp, &priv->scan_list, list) {
2340 list_del(&sd->list);
2341
2342 if ((sd->len > 0) && sd->buf)
2343 kfree(sd->buf);
2344 kfree(sd);
2345 }
2346
f11c179e 2347 orinoco_unregister_pm_notifier(priv);
74734312
DK
2348 orinoco_uncache_fw(priv);
2349
d03032af
DK
2350 priv->wpa_ie_len = 0;
2351 kfree(priv->wpa_ie);
23edcc41 2352 orinoco_mic_free(priv);
ea60a6aa 2353 wiphy_free(wiphy);
1da177e4 2354}
2131266d 2355EXPORT_SYMBOL(free_orinocodev);
1da177e4 2356
6415f7df
DK
2357int orinoco_up(struct orinoco_private *priv)
2358{
2359 struct net_device *dev = priv->ndev;
2360 unsigned long flags;
2361 int err;
2362
bcad6e80 2363 priv->hw.ops->lock_irqsave(&priv->lock, &flags);
6415f7df
DK
2364
2365 err = orinoco_reinit_firmware(priv);
2366 if (err) {
2367 printk(KERN_ERR "%s: Error %d re-initializing firmware\n",
2368 dev->name, err);
2369 goto exit;
2370 }
2371
2372 netif_device_attach(dev);
2373 priv->hw_unavailable--;
2374
2375 if (priv->open && !priv->hw_unavailable) {
2376 err = __orinoco_up(priv);
2377 if (err)
2378 printk(KERN_ERR "%s: Error %d restarting card\n",
2379 dev->name, err);
2380 }
2381
2382exit:
bcad6e80 2383 priv->hw.ops->unlock_irqrestore(&priv->lock, &flags);
6415f7df
DK
2384
2385 return 0;
2386}
2387EXPORT_SYMBOL(orinoco_up);
2388
2389void orinoco_down(struct orinoco_private *priv)
2390{
2391 struct net_device *dev = priv->ndev;
2392 unsigned long flags;
2393 int err;
2394
bcad6e80 2395 priv->hw.ops->lock_irqsave(&priv->lock, &flags);
6415f7df
DK
2396 err = __orinoco_down(priv);
2397 if (err)
2398 printk(KERN_WARNING "%s: Error %d downing interface\n",
2399 dev->name, err);
2400
2401 netif_device_detach(dev);
2402 priv->hw_unavailable++;
bcad6e80 2403 priv->hw.ops->unlock_irqrestore(&priv->lock, &flags);
6415f7df
DK
2404}
2405EXPORT_SYMBOL(orinoco_down);
2406
1da177e4
LT
2407/********************************************************************/
2408/* Module initialization */
2409/********************************************************************/
2410
1da177e4
LT
2411/* Can't be declared "const" or the whole __initdata section will
2412 * become const */
2413static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
2414 " (David Gibson <hermes@gibson.dropbear.id.au>, "
2415 "Pavel Roskin <proski@gnu.org>, et al)";
2416
2417static int __init init_orinoco(void)
2418{
2419 printk(KERN_DEBUG "%s\n", version);
2420 return 0;
2421}
2422
2423static void __exit exit_orinoco(void)
2424{
2425}
2426
2427module_init(init_orinoco);
2428module_exit(exit_orinoco);