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