staging/wlan-ng: Fix 'Branch condition evaluates to a garbage value' in p80211netdev.c
[pandora-kernel.git] / drivers / staging / wlan-ng / p80211netdev.c
1 /* src/p80211/p80211knetdev.c
2 *
3 * Linux Kernel net device interface
4 *
5 * Copyright (C) 1999 AbsoluteValue Systems, Inc.  All Rights Reserved.
6 * --------------------------------------------------------------------
7 *
8 * linux-wlan
9 *
10 *   The contents of this file are subject to the Mozilla Public
11 *   License Version 1.1 (the "License"); you may not use this file
12 *   except in compliance with the License. You may obtain a copy of
13 *   the License at http://www.mozilla.org/MPL/
14 *
15 *   Software distributed under the License is distributed on an "AS
16 *   IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
17 *   implied. See the License for the specific language governing
18 *   rights and limitations under the License.
19 *
20 *   Alternatively, the contents of this file may be used under the
21 *   terms of the GNU Public License version 2 (the "GPL"), in which
22 *   case the provisions of the GPL are applicable instead of the
23 *   above.  If you wish to allow the use of your version of this file
24 *   only under the terms of the GPL and not to allow others to use
25 *   your version of this file under the MPL, indicate your decision
26 *   by deleting the provisions above and replace them with the notice
27 *   and other provisions required by the GPL.  If you do not delete
28 *   the provisions above, a recipient may use your version of this
29 *   file under either the MPL or the GPL.
30 *
31 * --------------------------------------------------------------------
32 *
33 * Inquiries regarding the linux-wlan Open Source project can be
34 * made directly to:
35 *
36 * AbsoluteValue Systems Inc.
37 * info@linux-wlan.com
38 * http://www.linux-wlan.com
39 *
40 * --------------------------------------------------------------------
41 *
42 * Portions of the development of this software were funded by
43 * Intersil Corporation as part of PRISM(R) chipset product development.
44 *
45 * --------------------------------------------------------------------
46 *
47 * The functions required for a Linux network device are defined here.
48 *
49 * --------------------------------------------------------------------
50 */
51
52 #include <linux/module.h>
53 #include <linux/kernel.h>
54 #include <linux/sched.h>
55 #include <linux/types.h>
56 #include <linux/skbuff.h>
57 #include <linux/slab.h>
58 #include <linux/proc_fs.h>
59 #include <linux/interrupt.h>
60 #include <linux/netdevice.h>
61 #include <linux/kmod.h>
62 #include <linux/if_arp.h>
63 #include <linux/wireless.h>
64 #include <linux/sockios.h>
65 #include <linux/etherdevice.h>
66 #include <linux/if_ether.h>
67 #include <linux/byteorder/generic.h>
68 #include <linux/bitops.h>
69 #include <linux/uaccess.h>
70 #include <asm/byteorder.h>
71
72 #ifdef SIOCETHTOOL
73 #include <linux/ethtool.h>
74 #endif
75
76 #include <net/iw_handler.h>
77 #include <net/net_namespace.h>
78 #include <net/cfg80211.h>
79
80 #include "p80211types.h"
81 #include "p80211hdr.h"
82 #include "p80211conv.h"
83 #include "p80211mgmt.h"
84 #include "p80211msg.h"
85 #include "p80211netdev.h"
86 #include "p80211ioctl.h"
87 #include "p80211req.h"
88 #include "p80211metastruct.h"
89 #include "p80211metadef.h"
90
91 #include "cfg80211.c"
92
93 /* Support functions */
94 static void p80211netdev_rx_bh(unsigned long arg);
95
96 /* netdevice method functions */
97 static int p80211knetdev_init(netdevice_t *netdev);
98 static struct net_device_stats *p80211knetdev_get_stats(netdevice_t *netdev);
99 static int p80211knetdev_open(netdevice_t *netdev);
100 static int p80211knetdev_stop(netdevice_t *netdev);
101 static int p80211knetdev_hard_start_xmit(struct sk_buff *skb,
102                                          netdevice_t *netdev);
103 static void p80211knetdev_set_multicast_list(netdevice_t *dev);
104 static int p80211knetdev_do_ioctl(netdevice_t *dev, struct ifreq *ifr,
105                                   int cmd);
106 static int p80211knetdev_set_mac_address(netdevice_t *dev, void *addr);
107 static void p80211knetdev_tx_timeout(netdevice_t *netdev);
108 static int p80211_rx_typedrop(wlandevice_t *wlandev, u16 fc);
109
110 int wlan_watchdog = 5000;
111 module_param(wlan_watchdog, int, 0644);
112 MODULE_PARM_DESC(wlan_watchdog, "transmit timeout in milliseconds");
113
114 int wlan_wext_write = 1;
115 module_param(wlan_wext_write, int, 0644);
116 MODULE_PARM_DESC(wlan_wext_write, "enable write wireless extensions");
117
118 /*----------------------------------------------------------------
119 * p80211knetdev_init
120 *
121 * Init method for a Linux netdevice.  Called in response to
122 * register_netdev.
123 *
124 * Arguments:
125 *       none
126 *
127 * Returns:
128 *       nothing
129 ----------------------------------------------------------------*/
130 static int p80211knetdev_init(netdevice_t *netdev)
131 {
132         /* Called in response to register_netdev */
133         /* This is usually the probe function, but the probe has */
134         /* already been done by the MSD and the create_kdev */
135         /* function.  All we do here is return success */
136         return 0;
137 }
138
139 /*----------------------------------------------------------------
140 * p80211knetdev_get_stats
141 *
142 * Statistics retrieval for linux netdevices.  Here we're reporting
143 * the Linux i/f level statistics.  Hence, for the primary numbers,
144 * we don't want to report the numbers from the MIB.  Eventually,
145 * it might be useful to collect some of the error counters though.
146 *
147 * Arguments:
148 *       netdev          Linux netdevice
149 *
150 * Returns:
151 *       the address of the statistics structure
152 ----------------------------------------------------------------*/
153 static struct net_device_stats *p80211knetdev_get_stats(netdevice_t * netdev)
154 {
155         wlandevice_t *wlandev = netdev->ml_priv;
156
157         /* TODO: review the MIB stats for items that correspond to
158            linux stats */
159
160         return &(wlandev->linux_stats);
161 }
162
163 /*----------------------------------------------------------------
164 * p80211knetdev_open
165 *
166 * Linux netdevice open method.  Following a successful call here,
167 * the device is supposed to be ready for tx and rx.  In our
168 * situation that may not be entirely true due to the state of the
169 * MAC below.
170 *
171 * Arguments:
172 *       netdev          Linux network device structure
173 *
174 * Returns:
175 *       zero on success, non-zero otherwise
176 ----------------------------------------------------------------*/
177 static int p80211knetdev_open(netdevice_t *netdev)
178 {
179         int result = 0;         /* success */
180         wlandevice_t *wlandev = netdev->ml_priv;
181
182         /* Check to make sure the MSD is running */
183         if (wlandev->msdstate != WLAN_MSD_RUNNING)
184                 return -ENODEV;
185
186         /* Tell the MSD to open */
187         if (wlandev->open != NULL) {
188                 result = wlandev->open(wlandev);
189                 if (result == 0) {
190                         netif_start_queue(wlandev->netdev);
191                         wlandev->state = WLAN_DEVICE_OPEN;
192                 }
193         } else {
194                 result = -EAGAIN;
195         }
196
197         return result;
198 }
199
200 /*----------------------------------------------------------------
201 * p80211knetdev_stop
202 *
203 * Linux netdevice stop (close) method.  Following this call,
204 * no frames should go up or down through this interface.
205 *
206 * Arguments:
207 *       netdev          Linux network device structure
208 *
209 * Returns:
210 *       zero on success, non-zero otherwise
211 ----------------------------------------------------------------*/
212 static int p80211knetdev_stop(netdevice_t *netdev)
213 {
214         int result = 0;
215         wlandevice_t *wlandev = netdev->ml_priv;
216
217         if (wlandev->close != NULL)
218                 result = wlandev->close(wlandev);
219
220         netif_stop_queue(wlandev->netdev);
221         wlandev->state = WLAN_DEVICE_CLOSED;
222
223         return result;
224 }
225
226 /*----------------------------------------------------------------
227 * p80211netdev_rx
228 *
229 * Frame receive function called by the mac specific driver.
230 *
231 * Arguments:
232 *       wlandev         WLAN network device structure
233 *       skb             skbuff containing a full 802.11 frame.
234 * Returns:
235 *       nothing
236 * Side effects:
237 *
238 ----------------------------------------------------------------*/
239 void p80211netdev_rx(wlandevice_t *wlandev, struct sk_buff *skb)
240 {
241         /* Enqueue for post-irq processing */
242         skb_queue_tail(&wlandev->nsd_rxq, skb);
243
244         tasklet_schedule(&wlandev->rx_bh);
245
246         return;
247 }
248
249 /*----------------------------------------------------------------
250 * p80211netdev_rx_bh
251 *
252 * Deferred processing of all received frames.
253 *
254 * Arguments:
255 *       wlandev         WLAN network device structure
256 *       skb             skbuff containing a full 802.11 frame.
257 * Returns:
258 *       nothing
259 * Side effects:
260 *
261 ----------------------------------------------------------------*/
262 static void p80211netdev_rx_bh(unsigned long arg)
263 {
264         wlandevice_t *wlandev = (wlandevice_t *) arg;
265         struct sk_buff *skb = NULL;
266         netdevice_t *dev = wlandev->netdev;
267         struct p80211_hdr_a3 *hdr;
268         u16 fc;
269
270         /* Let's empty our our queue */
271         while ((skb = skb_dequeue(&wlandev->nsd_rxq))) {
272                 if (wlandev->state == WLAN_DEVICE_OPEN) {
273
274                         if (dev->type != ARPHRD_ETHER) {
275                                 /* RAW frame; we shouldn't convert it */
276                                 /* XXX Append the Prism Header here instead. */
277
278                                 /* set up various data fields */
279                                 skb->dev = dev;
280                                 skb_reset_mac_header(skb);
281                                 skb->ip_summed = CHECKSUM_NONE;
282                                 skb->pkt_type = PACKET_OTHERHOST;
283                                 skb->protocol = htons(ETH_P_80211_RAW);
284                                 dev->last_rx = jiffies;
285
286                                 wlandev->linux_stats.rx_packets++;
287                                 wlandev->linux_stats.rx_bytes += skb->len;
288                                 netif_rx_ni(skb);
289                                 continue;
290                         } else {
291                                 hdr = (struct p80211_hdr_a3 *) skb->data;
292                                 fc = le16_to_cpu(hdr->fc);
293                                 if (p80211_rx_typedrop(wlandev, fc)) {
294                                         dev_kfree_skb(skb);
295                                         continue;
296                                 }
297
298                                 /* perform mcast filtering */
299                                 if (wlandev->netdev->flags & IFF_ALLMULTI) {
300                                         /* allow my local address through */
301                                         if (memcmp
302                                             (hdr->a1, wlandev->netdev->dev_addr,
303                                              ETH_ALEN) != 0) {
304                                                 /* but reject anything else that
305                                                    isn't multicast */
306                                                 if (!(hdr->a1[0] & 0x01)) {
307                                                         dev_kfree_skb(skb);
308                                                         continue;
309                                                 }
310                                         }
311                                 }
312
313                                 if (skb_p80211_to_ether
314                                     (wlandev, wlandev->ethconv, skb) == 0) {
315                                         skb->dev->last_rx = jiffies;
316                                         wlandev->linux_stats.rx_packets++;
317                                         wlandev->linux_stats.rx_bytes +=
318                                             skb->len;
319                                         netif_rx_ni(skb);
320                                         continue;
321                                 }
322                                 pr_debug("p80211_to_ether failed.\n");
323                         }
324                 }
325                 dev_kfree_skb(skb);
326         }
327 }
328
329 /*----------------------------------------------------------------
330 * p80211knetdev_hard_start_xmit
331 *
332 * Linux netdevice method for transmitting a frame.
333 *
334 * Arguments:
335 *       skb     Linux sk_buff containing the frame.
336 *       netdev  Linux netdevice.
337 *
338 * Side effects:
339 *       If the lower layers report that buffers are full. netdev->tbusy
340 *       will be set to prevent higher layers from sending more traffic.
341 *
342 *       Note: If this function returns non-zero, higher layers retain
343 *             ownership of the skb.
344 *
345 * Returns:
346 *       zero on success, non-zero on failure.
347 ----------------------------------------------------------------*/
348 static int p80211knetdev_hard_start_xmit(struct sk_buff *skb,
349                                          netdevice_t *netdev)
350 {
351         int result = 0;
352         int txresult = -1;
353         wlandevice_t *wlandev = netdev->ml_priv;
354         union p80211_hdr p80211_hdr;
355         struct p80211_metawep p80211_wep;
356
357         p80211_wep.data = NULL;
358
359         if (skb == NULL)
360                 return NETDEV_TX_OK;
361
362         if (wlandev->state != WLAN_DEVICE_OPEN) {
363                 result = 1;
364                 goto failed;
365         }
366
367         memset(&p80211_hdr, 0, sizeof(union p80211_hdr));
368         memset(&p80211_wep, 0, sizeof(struct p80211_metawep));
369
370         if (netif_queue_stopped(netdev)) {
371                 pr_debug("called when queue stopped.\n");
372                 result = 1;
373                 goto failed;
374         }
375
376         netif_stop_queue(netdev);
377
378         /* Check to see that a valid mode is set */
379         switch (wlandev->macmode) {
380         case WLAN_MACMODE_IBSS_STA:
381         case WLAN_MACMODE_ESS_STA:
382         case WLAN_MACMODE_ESS_AP:
383                 break;
384         default:
385                 /* Mode isn't set yet, just drop the frame
386                  * and return success .
387                  * TODO: we need a saner way to handle this
388                  */
389                 if (skb->protocol != ETH_P_80211_RAW) {
390                         netif_start_queue(wlandev->netdev);
391                         printk(KERN_NOTICE
392                                "Tx attempt prior to association, frame dropped.\n");
393                         wlandev->linux_stats.tx_dropped++;
394                         result = 0;
395                         goto failed;
396                 }
397                 break;
398         }
399
400         /* Check for raw transmits */
401         if (skb->protocol == ETH_P_80211_RAW) {
402                 if (!capable(CAP_NET_ADMIN)) {
403                         result = 1;
404                         goto failed;
405                 }
406                 /* move the header over */
407                 memcpy(&p80211_hdr, skb->data, sizeof(union p80211_hdr));
408                 skb_pull(skb, sizeof(union p80211_hdr));
409         } else {
410                 if (skb_ether_to_p80211
411                     (wlandev, wlandev->ethconv, skb, &p80211_hdr,
412                      &p80211_wep) != 0) {
413                         /* convert failed */
414                         pr_debug("ether_to_80211(%d) failed.\n",
415                                  wlandev->ethconv);
416                         result = 1;
417                         goto failed;
418                 }
419         }
420         if (wlandev->txframe == NULL) {
421                 result = 1;
422                 goto failed;
423         }
424
425         netdev->trans_start = jiffies;
426
427         wlandev->linux_stats.tx_packets++;
428         /* count only the packet payload */
429         wlandev->linux_stats.tx_bytes += skb->len;
430
431         txresult = wlandev->txframe(wlandev, skb, &p80211_hdr, &p80211_wep);
432
433         if (txresult == 0) {
434                 /* success and more buf */
435                 /* avail, re: hw_txdata */
436                 netif_wake_queue(wlandev->netdev);
437                 result = NETDEV_TX_OK;
438         } else if (txresult == 1) {
439                 /* success, no more avail */
440                 pr_debug("txframe success, no more bufs\n");
441                 /* netdev->tbusy = 1;  don't set here, irqhdlr */
442                 /*   may have already cleared it */
443                 result = NETDEV_TX_OK;
444         } else if (txresult == 2) {
445                 /* alloc failure, drop frame */
446                 pr_debug("txframe returned alloc_fail\n");
447                 result = NETDEV_TX_BUSY;
448         } else {
449                 /* buffer full or queue busy, drop frame. */
450                 pr_debug("txframe returned full or busy\n");
451                 result = NETDEV_TX_BUSY;
452         }
453
454 failed:
455         /* Free up the WEP buffer if it's not the same as the skb */
456         if ((p80211_wep.data) && (p80211_wep.data != skb->data))
457                 kzfree(p80211_wep.data);
458
459         /* we always free the skb here, never in a lower level. */
460         if (!result)
461                 dev_kfree_skb(skb);
462
463         return result;
464 }
465
466 /*----------------------------------------------------------------
467 * p80211knetdev_set_multicast_list
468 *
469 * Called from higher lavers whenever there's a need to set/clear
470 * promiscuous mode or rewrite the multicast list.
471 *
472 * Arguments:
473 *       none
474 *
475 * Returns:
476 *       nothing
477 ----------------------------------------------------------------*/
478 static void p80211knetdev_set_multicast_list(netdevice_t *dev)
479 {
480         wlandevice_t *wlandev = dev->ml_priv;
481
482         /* TODO:  real multicast support as well */
483
484         if (wlandev->set_multicast_list)
485                 wlandev->set_multicast_list(wlandev, dev);
486
487 }
488
489 #ifdef SIOCETHTOOL
490
491 static int p80211netdev_ethtool(wlandevice_t *wlandev, void __user *useraddr)
492 {
493         u32 ethcmd;
494         struct ethtool_drvinfo info;
495         struct ethtool_value edata;
496
497         memset(&info, 0, sizeof(info));
498         memset(&edata, 0, sizeof(edata));
499
500         if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
501                 return -EFAULT;
502
503         switch (ethcmd) {
504         case ETHTOOL_GDRVINFO:
505                 info.cmd = ethcmd;
506                 snprintf(info.driver, sizeof(info.driver), "p80211_%s",
507                          wlandev->nsdname);
508                 snprintf(info.version, sizeof(info.version), "%s",
509                          WLAN_RELEASE);
510
511                 if (copy_to_user(useraddr, &info, sizeof(info)))
512                         return -EFAULT;
513                 return 0;
514 #ifdef ETHTOOL_GLINK
515         case ETHTOOL_GLINK:
516                 edata.cmd = ethcmd;
517
518                 if (wlandev->linkstatus &&
519                     (wlandev->macmode != WLAN_MACMODE_NONE)) {
520                         edata.data = 1;
521                 } else {
522                         edata.data = 0;
523                 }
524
525                 if (copy_to_user(useraddr, &edata, sizeof(edata)))
526                         return -EFAULT;
527                 return 0;
528 #endif
529         }
530
531         return -EOPNOTSUPP;
532 }
533
534 #endif
535
536 /*----------------------------------------------------------------
537 * p80211knetdev_do_ioctl
538 *
539 * Handle an ioctl call on one of our devices.  Everything Linux
540 * ioctl specific is done here.  Then we pass the contents of the
541 * ifr->data to the request message handler.
542 *
543 * Arguments:
544 *       dev     Linux kernel netdevice
545 *       ifr     Our private ioctl request structure, typed for the
546 *               generic struct ifreq so we can use ptr to func
547 *               w/o cast.
548 *
549 * Returns:
550 *       zero on success, a negative errno on failure.  Possible values:
551 *               -ENETDOWN Device isn't up.
552 *               -EBUSY  cmd already in progress
553 *               -ETIME  p80211 cmd timed out (MSD may have its own timers)
554 *               -EFAULT memory fault copying msg from user buffer
555 *               -ENOMEM unable to allocate kernel msg buffer
556 *               -ENOSYS bad magic, it the cmd really for us?
557 *               -EintR  sleeping on cmd, awakened by signal, cmd cancelled.
558 *
559 * Call Context:
560 *       Process thread (ioctl caller).  TODO: SMP support may require
561 *       locks.
562 ----------------------------------------------------------------*/
563 static int p80211knetdev_do_ioctl(netdevice_t *dev, struct ifreq *ifr, int cmd)
564 {
565         int result = 0;
566         struct p80211ioctl_req *req = (struct p80211ioctl_req *) ifr;
567         wlandevice_t *wlandev = dev->ml_priv;
568         u8 *msgbuf;
569
570         pr_debug("rx'd ioctl, cmd=%d, len=%d\n", cmd, req->len);
571
572 #ifdef SIOCETHTOOL
573         if (cmd == SIOCETHTOOL) {
574                 result =
575                     p80211netdev_ethtool(wlandev, (void __user *)ifr->ifr_data);
576                 goto bail;
577         }
578 #endif
579
580         /* Test the magic, assume ifr is good if it's there */
581         if (req->magic != P80211_IOCTL_MAGIC) {
582                 result = -ENOSYS;
583                 goto bail;
584         }
585
586         if (cmd == P80211_IFTEST) {
587                 result = 0;
588                 goto bail;
589         } else if (cmd != P80211_IFREQ) {
590                 result = -ENOSYS;
591                 goto bail;
592         }
593
594         /* Allocate a buf of size req->len */
595         msgbuf = kmalloc(req->len, GFP_KERNEL);
596         if (msgbuf) {
597                 if (copy_from_user(msgbuf, (void __user *)req->data, req->len))
598                         result = -EFAULT;
599                 else
600                         result = p80211req_dorequest(wlandev, msgbuf);
601
602                 if (result == 0) {
603                         if (copy_to_user
604                             ((void __user *)req->data, msgbuf, req->len)) {
605                                 result = -EFAULT;
606                         }
607                 }
608                 kfree(msgbuf);
609         } else {
610                 result = -ENOMEM;
611         }
612 bail:
613         /* If allocate,copyfrom or copyto fails, return errno */
614         return result;
615 }
616
617 /*----------------------------------------------------------------
618 * p80211knetdev_set_mac_address
619 *
620 * Handles the ioctl for changing the MACAddress of a netdevice
621 *
622 * references: linux/netdevice.h and drivers/net/net_init.c
623 *
624 * NOTE: [MSM] We only prevent address changes when the netdev is
625 * up.  We don't control anything based on dot11 state.  If the
626 * address is changed on a STA that's currently associated, you
627 * will probably lose the ability to send and receive data frames.
628 * Just be aware.  Therefore, this should usually only be done
629 * prior to scan/join/auth/assoc.
630 *
631 * Arguments:
632 *       dev     netdevice struct
633 *       addr    the new MACAddress (a struct)
634 *
635 * Returns:
636 *       zero on success, a negative errno on failure.  Possible values:
637 *               -EBUSY  device is bussy (cmd not possible)
638 *               -and errors returned by: p80211req_dorequest(..)
639 *
640 * by: Collin R. Mulliner <collin@mulliner.org>
641 ----------------------------------------------------------------*/
642 static int p80211knetdev_set_mac_address(netdevice_t *dev, void *addr)
643 {
644         struct sockaddr *new_addr = addr;
645         struct p80211msg_dot11req_mibset dot11req;
646         p80211item_unk392_t *mibattr;
647         p80211item_pstr6_t *macaddr;
648         p80211item_uint32_t *resultcode;
649         int result = 0;
650
651         /* If we're running, we don't allow MAC address changes */
652         if (netif_running(dev))
653                 return -EBUSY;
654
655         /* Set up some convenience pointers. */
656         mibattr = &dot11req.mibattribute;
657         macaddr = (p80211item_pstr6_t *) &mibattr->data;
658         resultcode = &dot11req.resultcode;
659
660         /* Set up a dot11req_mibset */
661         memset(&dot11req, 0, sizeof(struct p80211msg_dot11req_mibset));
662         dot11req.msgcode = DIDmsg_dot11req_mibset;
663         dot11req.msglen = sizeof(struct p80211msg_dot11req_mibset);
664         memcpy(dot11req.devname,
665                ((wlandevice_t *) dev->ml_priv)->name, WLAN_DEVNAMELEN_MAX - 1);
666
667         /* Set up the mibattribute argument */
668         mibattr->did = DIDmsg_dot11req_mibset_mibattribute;
669         mibattr->status = P80211ENUM_msgitem_status_data_ok;
670         mibattr->len = sizeof(mibattr->data);
671
672         macaddr->did = DIDmib_dot11mac_dot11OperationTable_dot11MACAddress;
673         macaddr->status = P80211ENUM_msgitem_status_data_ok;
674         macaddr->len = sizeof(macaddr->data);
675         macaddr->data.len = ETH_ALEN;
676         memcpy(&macaddr->data.data, new_addr->sa_data, ETH_ALEN);
677
678         /* Set up the resultcode argument */
679         resultcode->did = DIDmsg_dot11req_mibset_resultcode;
680         resultcode->status = P80211ENUM_msgitem_status_no_value;
681         resultcode->len = sizeof(resultcode->data);
682         resultcode->data = 0;
683
684         /* now fire the request */
685         result = p80211req_dorequest(dev->ml_priv, (u8 *) &dot11req);
686
687         /* If the request wasn't successful, report an error and don't
688          * change the netdev address
689          */
690         if (result != 0 || resultcode->data != P80211ENUM_resultcode_success) {
691                 printk(KERN_ERR
692                        "Low-level driver failed dot11req_mibset(dot11MACAddress).\n");
693                 result = -EADDRNOTAVAIL;
694         } else {
695                 /* everything's ok, change the addr in netdev */
696                 memcpy(dev->dev_addr, new_addr->sa_data, dev->addr_len);
697         }
698
699         return result;
700 }
701
702 static int wlan_change_mtu(netdevice_t *dev, int new_mtu)
703 {
704         /* 2312 is max 802.11 payload, 20 is overhead, (ether + llc +snap)
705            and another 8 for wep. */
706         if ((new_mtu < 68) || (new_mtu > (2312 - 20 - 8)))
707                 return -EINVAL;
708
709         dev->mtu = new_mtu;
710
711         return 0;
712 }
713
714 static const struct net_device_ops p80211_netdev_ops = {
715         .ndo_init = p80211knetdev_init,
716         .ndo_open = p80211knetdev_open,
717         .ndo_stop = p80211knetdev_stop,
718         .ndo_get_stats = p80211knetdev_get_stats,
719         .ndo_start_xmit = p80211knetdev_hard_start_xmit,
720         .ndo_set_rx_mode = p80211knetdev_set_multicast_list,
721         .ndo_do_ioctl = p80211knetdev_do_ioctl,
722         .ndo_set_mac_address = p80211knetdev_set_mac_address,
723         .ndo_tx_timeout = p80211knetdev_tx_timeout,
724         .ndo_change_mtu = wlan_change_mtu,
725         .ndo_validate_addr = eth_validate_addr,
726 };
727
728 /*----------------------------------------------------------------
729 * wlan_setup
730 *
731 * Roughly matches the functionality of ether_setup.  Here
732 * we set up any members of the wlandevice structure that are common
733 * to all devices.  Additionally, we allocate a linux 'struct device'
734 * and perform the same setup as ether_setup.
735 *
736 * Note: It's important that the caller have setup the wlandev->name
737 *       ptr prior to calling this function.
738 *
739 * Arguments:
740 *       wlandev         ptr to the wlandev structure for the
741 *                       interface.
742 *       physdev         ptr to usb device
743 * Returns:
744 *       zero on success, non-zero otherwise.
745 * Call Context:
746 *       Should be process thread.  We'll assume it might be
747 *       interrupt though.  When we add support for statically
748 *       compiled drivers, this function will be called in the
749 *       context of the kernel startup code.
750 ----------------------------------------------------------------*/
751 int wlan_setup(wlandevice_t *wlandev, struct device *physdev)
752 {
753         int result = 0;
754         netdevice_t *netdev;
755         struct wiphy *wiphy;
756         struct wireless_dev *wdev;
757
758         /* Set up the wlandev */
759         wlandev->state = WLAN_DEVICE_CLOSED;
760         wlandev->ethconv = WLAN_ETHCONV_8021h;
761         wlandev->macmode = WLAN_MACMODE_NONE;
762
763         /* Set up the rx queue */
764         skb_queue_head_init(&wlandev->nsd_rxq);
765         tasklet_init(&wlandev->rx_bh,
766                      p80211netdev_rx_bh, (unsigned long)wlandev);
767
768         /* Allocate and initialize the wiphy struct */
769         wiphy = wlan_create_wiphy(physdev, wlandev);
770         if (wiphy == NULL) {
771                 printk(KERN_ERR "Failed to alloc wiphy.\n");
772                 return 1;
773         }
774
775         /* Allocate and initialize the struct device */
776         netdev = alloc_netdev(sizeof(struct wireless_dev), "wlan%d",
777                                 ether_setup);
778         if (netdev == NULL) {
779                 printk(KERN_ERR "Failed to alloc netdev.\n");
780                 wlan_free_wiphy(wiphy);
781                 result = 1;
782         } else {
783                 wlandev->netdev = netdev;
784                 netdev->ml_priv = wlandev;
785                 netdev->netdev_ops = &p80211_netdev_ops;
786                 wdev = netdev_priv(netdev);
787                 wdev->wiphy = wiphy;
788                 wdev->iftype = NL80211_IFTYPE_STATION;
789                 netdev->ieee80211_ptr = wdev;
790
791                 netif_stop_queue(netdev);
792                 netif_carrier_off(netdev);
793         }
794
795         return result;
796 }
797
798 /*----------------------------------------------------------------
799 * wlan_unsetup
800 *
801 * This function is paired with the wlan_setup routine.  It should
802 * be called after unregister_wlandev.  Basically, all it does is
803 * free the 'struct device' that's associated with the wlandev.
804 * We do it here because the 'struct device' isn't allocated
805 * explicitly in the driver code, it's done in wlan_setup.  To
806 * do the free in the driver might seem like 'magic'.
807 *
808 * Arguments:
809 *       wlandev         ptr to the wlandev structure for the
810 *                       interface.
811 * Returns:
812 *       zero on success, non-zero otherwise.
813 * Call Context:
814 *       Should be process thread.  We'll assume it might be
815 *       interrupt though.  When we add support for statically
816 *       compiled drivers, this function will be called in the
817 *       context of the kernel startup code.
818 ----------------------------------------------------------------*/
819 int wlan_unsetup(wlandevice_t *wlandev)
820 {
821         struct wireless_dev *wdev;
822
823         tasklet_kill(&wlandev->rx_bh);
824
825         if (wlandev->netdev) {
826                 wdev = netdev_priv(wlandev->netdev);
827                 if (wdev->wiphy)
828                         wlan_free_wiphy(wdev->wiphy);
829                 free_netdev(wlandev->netdev);
830                 wlandev->netdev = NULL;
831         }
832
833         return 0;
834 }
835
836 /*----------------------------------------------------------------
837 * register_wlandev
838 *
839 * Roughly matches the functionality of register_netdev.  This function
840 * is called after the driver has successfully probed and set up the
841 * resources for the device.  It's now ready to become a named device
842 * in the Linux system.
843 *
844 * First we allocate a name for the device (if not already set), then
845 * we call the Linux function register_netdevice.
846 *
847 * Arguments:
848 *       wlandev         ptr to the wlandev structure for the
849 *                       interface.
850 * Returns:
851 *       zero on success, non-zero otherwise.
852 * Call Context:
853 *       Can be either interrupt or not.
854 ----------------------------------------------------------------*/
855 int register_wlandev(wlandevice_t *wlandev)
856 {
857         int i = 0;
858
859         i = register_netdev(wlandev->netdev);
860         if (i)
861                 return i;
862
863         return 0;
864 }
865
866 /*----------------------------------------------------------------
867 * unregister_wlandev
868 *
869 * Roughly matches the functionality of unregister_netdev.  This
870 * function is called to remove a named device from the system.
871 *
872 * First we tell linux that the device should no longer exist.
873 * Then we remove it from the list of known wlan devices.
874 *
875 * Arguments:
876 *       wlandev         ptr to the wlandev structure for the
877 *                       interface.
878 * Returns:
879 *       zero on success, non-zero otherwise.
880 * Call Context:
881 *       Can be either interrupt or not.
882 ----------------------------------------------------------------*/
883 int unregister_wlandev(wlandevice_t *wlandev)
884 {
885         struct sk_buff *skb;
886
887         unregister_netdev(wlandev->netdev);
888
889         /* Now to clean out the rx queue */
890         while ((skb = skb_dequeue(&wlandev->nsd_rxq)))
891                 dev_kfree_skb(skb);
892
893         return 0;
894 }
895
896 /*----------------------------------------------------------------
897 * p80211netdev_hwremoved
898 *
899 * Hardware removed notification. This function should be called
900 * immediately after an MSD has detected that the underlying hardware
901 * has been yanked out from under us.  The primary things we need
902 * to do are:
903 *   - Mark the wlandev
904 *   - Prevent any further traffic from the knetdev i/f
905 *   - Prevent any further requests from mgmt i/f
906 *   - If there are any waitq'd mgmt requests or mgmt-frame exchanges,
907 *     shut them down.
908 *   - Call the MSD hwremoved function.
909 *
910 * The remainder of the cleanup will be handled by unregister().
911 * Our primary goal here is to prevent as much tickling of the MSD
912 * as possible since the MSD is already in a 'wounded' state.
913 *
914 * TODO: As new features are added, this function should be
915 *       updated.
916 *
917 * Arguments:
918 *       wlandev         WLAN network device structure
919 * Returns:
920 *       nothing
921 * Side effects:
922 *
923 * Call context:
924 *       Usually interrupt.
925 ----------------------------------------------------------------*/
926 void p80211netdev_hwremoved(wlandevice_t *wlandev)
927 {
928         wlandev->hwremoved = 1;
929         if (wlandev->state == WLAN_DEVICE_OPEN)
930                 netif_stop_queue(wlandev->netdev);
931
932         netif_device_detach(wlandev->netdev);
933 }
934
935 /*----------------------------------------------------------------
936 * p80211_rx_typedrop
937 *
938 * Classifies the frame, increments the appropriate counter, and
939 * returns 0|1|2 indicating whether the driver should handle, ignore, or
940 * drop the frame
941 *
942 * Arguments:
943 *       wlandev         wlan device structure
944 *       fc              frame control field
945 *
946 * Returns:
947 *       zero if the frame should be handled by the driver,
948 *       one if the frame should be ignored
949 *       anything else means we drop it.
950 *
951 * Side effects:
952 *
953 * Call context:
954 *       interrupt
955 ----------------------------------------------------------------*/
956 static int p80211_rx_typedrop(wlandevice_t *wlandev, u16 fc)
957 {
958         u16 ftype;
959         u16 fstype;
960         int drop = 0;
961         /* Classify frame, increment counter */
962         ftype = WLAN_GET_FC_FTYPE(fc);
963         fstype = WLAN_GET_FC_FSTYPE(fc);
964 #if 0
965         pr_debug("rx_typedrop : ftype=%d fstype=%d.\n", ftype, fstype);
966 #endif
967         switch (ftype) {
968         case WLAN_FTYPE_MGMT:
969                 if ((wlandev->netdev->flags & IFF_PROMISC) ||
970                     (wlandev->netdev->flags & IFF_ALLMULTI)) {
971                         drop = 1;
972                         break;
973                 }
974                 pr_debug("rx'd mgmt:\n");
975                 wlandev->rx.mgmt++;
976                 switch (fstype) {
977                 case WLAN_FSTYPE_ASSOCREQ:
978                         /* printk("assocreq"); */
979                         wlandev->rx.assocreq++;
980                         break;
981                 case WLAN_FSTYPE_ASSOCRESP:
982                         /* printk("assocresp"); */
983                         wlandev->rx.assocresp++;
984                         break;
985                 case WLAN_FSTYPE_REASSOCREQ:
986                         /* printk("reassocreq"); */
987                         wlandev->rx.reassocreq++;
988                         break;
989                 case WLAN_FSTYPE_REASSOCRESP:
990                         /* printk("reassocresp"); */
991                         wlandev->rx.reassocresp++;
992                         break;
993                 case WLAN_FSTYPE_PROBEREQ:
994                         /* printk("probereq"); */
995                         wlandev->rx.probereq++;
996                         break;
997                 case WLAN_FSTYPE_PROBERESP:
998                         /* printk("proberesp"); */
999                         wlandev->rx.proberesp++;
1000                         break;
1001                 case WLAN_FSTYPE_BEACON:
1002                         /* printk("beacon"); */
1003                         wlandev->rx.beacon++;
1004                         break;
1005                 case WLAN_FSTYPE_ATIM:
1006                         /* printk("atim"); */
1007                         wlandev->rx.atim++;
1008                         break;
1009                 case WLAN_FSTYPE_DISASSOC:
1010                         /* printk("disassoc"); */
1011                         wlandev->rx.disassoc++;
1012                         break;
1013                 case WLAN_FSTYPE_AUTHEN:
1014                         /* printk("authen"); */
1015                         wlandev->rx.authen++;
1016                         break;
1017                 case WLAN_FSTYPE_DEAUTHEN:
1018                         /* printk("deauthen"); */
1019                         wlandev->rx.deauthen++;
1020                         break;
1021                 default:
1022                         /* printk("unknown"); */
1023                         wlandev->rx.mgmt_unknown++;
1024                         break;
1025                 }
1026                 /* printk("\n"); */
1027                 drop = 2;
1028                 break;
1029
1030         case WLAN_FTYPE_CTL:
1031                 if ((wlandev->netdev->flags & IFF_PROMISC) ||
1032                     (wlandev->netdev->flags & IFF_ALLMULTI)) {
1033                         drop = 1;
1034                         break;
1035                 }
1036                 pr_debug("rx'd ctl:\n");
1037                 wlandev->rx.ctl++;
1038                 switch (fstype) {
1039                 case WLAN_FSTYPE_PSPOLL:
1040                         /* printk("pspoll"); */
1041                         wlandev->rx.pspoll++;
1042                         break;
1043                 case WLAN_FSTYPE_RTS:
1044                         /* printk("rts"); */
1045                         wlandev->rx.rts++;
1046                         break;
1047                 case WLAN_FSTYPE_CTS:
1048                         /* printk("cts"); */
1049                         wlandev->rx.cts++;
1050                         break;
1051                 case WLAN_FSTYPE_ACK:
1052                         /* printk("ack"); */
1053                         wlandev->rx.ack++;
1054                         break;
1055                 case WLAN_FSTYPE_CFEND:
1056                         /* printk("cfend"); */
1057                         wlandev->rx.cfend++;
1058                         break;
1059                 case WLAN_FSTYPE_CFENDCFACK:
1060                         /* printk("cfendcfack"); */
1061                         wlandev->rx.cfendcfack++;
1062                         break;
1063                 default:
1064                         /* printk("unknown"); */
1065                         wlandev->rx.ctl_unknown++;
1066                         break;
1067                 }
1068                 /* printk("\n"); */
1069                 drop = 2;
1070                 break;
1071
1072         case WLAN_FTYPE_DATA:
1073                 wlandev->rx.data++;
1074                 switch (fstype) {
1075                 case WLAN_FSTYPE_DATAONLY:
1076                         wlandev->rx.dataonly++;
1077                         break;
1078                 case WLAN_FSTYPE_DATA_CFACK:
1079                         wlandev->rx.data_cfack++;
1080                         break;
1081                 case WLAN_FSTYPE_DATA_CFPOLL:
1082                         wlandev->rx.data_cfpoll++;
1083                         break;
1084                 case WLAN_FSTYPE_DATA_CFACK_CFPOLL:
1085                         wlandev->rx.data__cfack_cfpoll++;
1086                         break;
1087                 case WLAN_FSTYPE_NULL:
1088                         pr_debug("rx'd data:null\n");
1089                         wlandev->rx.null++;
1090                         break;
1091                 case WLAN_FSTYPE_CFACK:
1092                         pr_debug("rx'd data:cfack\n");
1093                         wlandev->rx.cfack++;
1094                         break;
1095                 case WLAN_FSTYPE_CFPOLL:
1096                         pr_debug("rx'd data:cfpoll\n");
1097                         wlandev->rx.cfpoll++;
1098                         break;
1099                 case WLAN_FSTYPE_CFACK_CFPOLL:
1100                         pr_debug("rx'd data:cfack_cfpoll\n");
1101                         wlandev->rx.cfack_cfpoll++;
1102                         break;
1103                 default:
1104                         /* printk("unknown"); */
1105                         wlandev->rx.data_unknown++;
1106                         break;
1107                 }
1108
1109                 break;
1110         }
1111         return drop;
1112 }
1113
1114 static void p80211knetdev_tx_timeout(netdevice_t *netdev)
1115 {
1116         wlandevice_t *wlandev = netdev->ml_priv;
1117
1118         if (wlandev->tx_timeout) {
1119                 wlandev->tx_timeout(wlandev);
1120         } else {
1121                 printk(KERN_WARNING "Implement tx_timeout for %s\n",
1122                        wlandev->nsdname);
1123                 netif_wake_queue(wlandev->netdev);
1124         }
1125 }