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