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