Merge tag 'v3.7-rc3' into for-linus to sync up with recent USB changes
[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         tasklet_schedule(&wlandev->rx_bh);
244 }
245
246 /*----------------------------------------------------------------
247 * p80211netdev_rx_bh
248 *
249 * Deferred processing of all received frames.
250 *
251 * Arguments:
252 *       wlandev         WLAN network device structure
253 *       skb             skbuff containing a full 802.11 frame.
254 * Returns:
255 *       nothing
256 * Side effects:
257 *
258 ----------------------------------------------------------------*/
259 static void p80211netdev_rx_bh(unsigned long arg)
260 {
261         wlandevice_t *wlandev = (wlandevice_t *) arg;
262         struct sk_buff *skb = NULL;
263         netdevice_t *dev = wlandev->netdev;
264         struct p80211_hdr_a3 *hdr;
265         u16 fc;
266
267         /* Let's empty our our queue */
268         while ((skb = skb_dequeue(&wlandev->nsd_rxq))) {
269                 if (wlandev->state == WLAN_DEVICE_OPEN) {
270
271                         if (dev->type != ARPHRD_ETHER) {
272                                 /* RAW frame; we shouldn't convert it */
273                                 /* XXX Append the Prism Header here instead. */
274
275                                 /* set up various data fields */
276                                 skb->dev = dev;
277                                 skb_reset_mac_header(skb);
278                                 skb->ip_summed = CHECKSUM_NONE;
279                                 skb->pkt_type = PACKET_OTHERHOST;
280                                 skb->protocol = htons(ETH_P_80211_RAW);
281                                 dev->last_rx = jiffies;
282
283                                 wlandev->linux_stats.rx_packets++;
284                                 wlandev->linux_stats.rx_bytes += skb->len;
285                                 netif_rx_ni(skb);
286                                 continue;
287                         } else {
288                                 hdr = (struct p80211_hdr_a3 *) skb->data;
289                                 fc = le16_to_cpu(hdr->fc);
290                                 if (p80211_rx_typedrop(wlandev, fc)) {
291                                         dev_kfree_skb(skb);
292                                         continue;
293                                 }
294
295                                 /* perform mcast filtering */
296                                 if (wlandev->netdev->flags & IFF_ALLMULTI) {
297                                         /* allow my local address through */
298                                         if (memcmp
299                                             (hdr->a1, wlandev->netdev->dev_addr,
300                                              ETH_ALEN) != 0) {
301                                                 /* but reject anything else that
302                                                    isn't multicast */
303                                                 if (!(hdr->a1[0] & 0x01)) {
304                                                         dev_kfree_skb(skb);
305                                                         continue;
306                                                 }
307                                         }
308                                 }
309
310                                 if (skb_p80211_to_ether
311                                     (wlandev, wlandev->ethconv, skb) == 0) {
312                                         skb->dev->last_rx = jiffies;
313                                         wlandev->linux_stats.rx_packets++;
314                                         wlandev->linux_stats.rx_bytes +=
315                                             skb->len;
316                                         netif_rx_ni(skb);
317                                         continue;
318                                 }
319                                 pr_debug("p80211_to_ether failed.\n");
320                         }
321                 }
322                 dev_kfree_skb(skb);
323         }
324 }
325
326 /*----------------------------------------------------------------
327 * p80211knetdev_hard_start_xmit
328 *
329 * Linux netdevice method for transmitting a frame.
330 *
331 * Arguments:
332 *       skb     Linux sk_buff containing the frame.
333 *       netdev  Linux netdevice.
334 *
335 * Side effects:
336 *       If the lower layers report that buffers are full. netdev->tbusy
337 *       will be set to prevent higher layers from sending more traffic.
338 *
339 *       Note: If this function returns non-zero, higher layers retain
340 *             ownership of the skb.
341 *
342 * Returns:
343 *       zero on success, non-zero on failure.
344 ----------------------------------------------------------------*/
345 static int p80211knetdev_hard_start_xmit(struct sk_buff *skb,
346                                          netdevice_t *netdev)
347 {
348         int result = 0;
349         int txresult = -1;
350         wlandevice_t *wlandev = netdev->ml_priv;
351         union p80211_hdr p80211_hdr;
352         struct p80211_metawep p80211_wep;
353
354         if (skb == NULL)
355                 return NETDEV_TX_OK;
356
357         if (wlandev->state != WLAN_DEVICE_OPEN) {
358                 result = 1;
359                 goto failed;
360         }
361
362         memset(&p80211_hdr, 0, sizeof(union p80211_hdr));
363         memset(&p80211_wep, 0, sizeof(struct p80211_metawep));
364
365         if (netif_queue_stopped(netdev)) {
366                 pr_debug("called when queue stopped.\n");
367                 result = 1;
368                 goto failed;
369         }
370
371         netif_stop_queue(netdev);
372
373         /* Check to see that a valid mode is set */
374         switch (wlandev->macmode) {
375         case WLAN_MACMODE_IBSS_STA:
376         case WLAN_MACMODE_ESS_STA:
377         case WLAN_MACMODE_ESS_AP:
378                 break;
379         default:
380                 /* Mode isn't set yet, just drop the frame
381                  * and return success .
382                  * TODO: we need a saner way to handle this
383                  */
384                 if (skb->protocol != ETH_P_80211_RAW) {
385                         netif_start_queue(wlandev->netdev);
386                         printk(KERN_NOTICE
387                                "Tx attempt prior to association, frame dropped.\n");
388                         wlandev->linux_stats.tx_dropped++;
389                         result = 0;
390                         goto failed;
391                 }
392                 break;
393         }
394
395         /* Check for raw transmits */
396         if (skb->protocol == ETH_P_80211_RAW) {
397                 if (!capable(CAP_NET_ADMIN)) {
398                         result = 1;
399                         goto failed;
400                 }
401                 /* move the header over */
402                 memcpy(&p80211_hdr, skb->data, sizeof(union p80211_hdr));
403                 skb_pull(skb, sizeof(union p80211_hdr));
404         } else {
405                 if (skb_ether_to_p80211
406                     (wlandev, wlandev->ethconv, skb, &p80211_hdr,
407                      &p80211_wep) != 0) {
408                         /* convert failed */
409                         pr_debug("ether_to_80211(%d) failed.\n",
410                                  wlandev->ethconv);
411                         result = 1;
412                         goto failed;
413                 }
414         }
415         if (wlandev->txframe == NULL) {
416                 result = 1;
417                 goto failed;
418         }
419
420         netdev->trans_start = jiffies;
421
422         wlandev->linux_stats.tx_packets++;
423         /* count only the packet payload */
424         wlandev->linux_stats.tx_bytes += skb->len;
425
426         txresult = wlandev->txframe(wlandev, skb, &p80211_hdr, &p80211_wep);
427
428         if (txresult == 0) {
429                 /* success and more buf */
430                 /* avail, re: hw_txdata */
431                 netif_wake_queue(wlandev->netdev);
432                 result = NETDEV_TX_OK;
433         } else if (txresult == 1) {
434                 /* success, no more avail */
435                 pr_debug("txframe success, no more bufs\n");
436                 /* netdev->tbusy = 1;  don't set here, irqhdlr */
437                 /*   may have already cleared it */
438                 result = NETDEV_TX_OK;
439         } else if (txresult == 2) {
440                 /* alloc failure, drop frame */
441                 pr_debug("txframe returned alloc_fail\n");
442                 result = NETDEV_TX_BUSY;
443         } else {
444                 /* buffer full or queue busy, drop frame. */
445                 pr_debug("txframe returned full or busy\n");
446                 result = NETDEV_TX_BUSY;
447         }
448
449 failed:
450         /* Free up the WEP buffer if it's not the same as the skb */
451         if ((p80211_wep.data) && (p80211_wep.data != skb->data))
452                 kzfree(p80211_wep.data);
453
454         /* we always free the skb here, never in a lower level. */
455         if (!result)
456                 dev_kfree_skb(skb);
457
458         return result;
459 }
460
461 /*----------------------------------------------------------------
462 * p80211knetdev_set_multicast_list
463 *
464 * Called from higher layers whenever there's a need to set/clear
465 * promiscuous mode or rewrite the multicast list.
466 *
467 * Arguments:
468 *       none
469 *
470 * Returns:
471 *       nothing
472 ----------------------------------------------------------------*/
473 static void p80211knetdev_set_multicast_list(netdevice_t *dev)
474 {
475         wlandevice_t *wlandev = dev->ml_priv;
476
477         /* TODO:  real multicast support as well */
478
479         if (wlandev->set_multicast_list)
480                 wlandev->set_multicast_list(wlandev, dev);
481
482 }
483
484 #ifdef SIOCETHTOOL
485
486 static int p80211netdev_ethtool(wlandevice_t *wlandev, void __user *useraddr)
487 {
488         u32 ethcmd;
489         struct ethtool_drvinfo info;
490         struct ethtool_value edata;
491
492         memset(&info, 0, sizeof(info));
493         memset(&edata, 0, sizeof(edata));
494
495         if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
496                 return -EFAULT;
497
498         switch (ethcmd) {
499         case ETHTOOL_GDRVINFO:
500                 info.cmd = ethcmd;
501                 snprintf(info.driver, sizeof(info.driver), "p80211_%s",
502                          wlandev->nsdname);
503                 snprintf(info.version, sizeof(info.version), "%s",
504                          WLAN_RELEASE);
505
506                 if (copy_to_user(useraddr, &info, sizeof(info)))
507                         return -EFAULT;
508                 return 0;
509 #ifdef ETHTOOL_GLINK
510         case ETHTOOL_GLINK:
511                 edata.cmd = ethcmd;
512
513                 if (wlandev->linkstatus &&
514                     (wlandev->macmode != WLAN_MACMODE_NONE)) {
515                         edata.data = 1;
516                 } else {
517                         edata.data = 0;
518                 }
519
520                 if (copy_to_user(useraddr, &edata, sizeof(edata)))
521                         return -EFAULT;
522                 return 0;
523 #endif
524         }
525
526         return -EOPNOTSUPP;
527 }
528
529 #endif
530
531 /*----------------------------------------------------------------
532 * p80211knetdev_do_ioctl
533 *
534 * Handle an ioctl call on one of our devices.  Everything Linux
535 * ioctl specific is done here.  Then we pass the contents of the
536 * ifr->data to the request message handler.
537 *
538 * Arguments:
539 *       dev     Linux kernel netdevice
540 *       ifr     Our private ioctl request structure, typed for the
541 *               generic struct ifreq so we can use ptr to func
542 *               w/o cast.
543 *
544 * Returns:
545 *       zero on success, a negative errno on failure.  Possible values:
546 *               -ENETDOWN Device isn't up.
547 *               -EBUSY  cmd already in progress
548 *               -ETIME  p80211 cmd timed out (MSD may have its own timers)
549 *               -EFAULT memory fault copying msg from user buffer
550 *               -ENOMEM unable to allocate kernel msg buffer
551 *               -ENOSYS bad magic, it the cmd really for us?
552 *               -EintR  sleeping on cmd, awakened by signal, cmd cancelled.
553 *
554 * Call Context:
555 *       Process thread (ioctl caller).  TODO: SMP support may require
556 *       locks.
557 ----------------------------------------------------------------*/
558 static int p80211knetdev_do_ioctl(netdevice_t *dev, struct ifreq *ifr, int cmd)
559 {
560         int result = 0;
561         struct p80211ioctl_req *req = (struct p80211ioctl_req *) ifr;
562         wlandevice_t *wlandev = dev->ml_priv;
563         u8 *msgbuf;
564
565         pr_debug("rx'd ioctl, cmd=%d, len=%d\n", cmd, req->len);
566
567 #ifdef SIOCETHTOOL
568         if (cmd == SIOCETHTOOL) {
569                 result =
570                     p80211netdev_ethtool(wlandev, (void __user *)ifr->ifr_data);
571                 goto bail;
572         }
573 #endif
574
575         /* Test the magic, assume ifr is good if it's there */
576         if (req->magic != P80211_IOCTL_MAGIC) {
577                 result = -ENOSYS;
578                 goto bail;
579         }
580
581         if (cmd == P80211_IFTEST) {
582                 result = 0;
583                 goto bail;
584         } else if (cmd != P80211_IFREQ) {
585                 result = -ENOSYS;
586                 goto bail;
587         }
588
589         /* Allocate a buf of size req->len */
590         msgbuf = kmalloc(req->len, GFP_KERNEL);
591         if (msgbuf) {
592                 if (copy_from_user(msgbuf, (void __user *)req->data, req->len))
593                         result = -EFAULT;
594                 else
595                         result = p80211req_dorequest(wlandev, msgbuf);
596
597                 if (result == 0) {
598                         if (copy_to_user
599                             ((void __user *)req->data, msgbuf, req->len)) {
600                                 result = -EFAULT;
601                         }
602                 }
603                 kfree(msgbuf);
604         } else {
605                 result = -ENOMEM;
606         }
607 bail:
608         /* If allocate,copyfrom or copyto fails, return errno */
609         return result;
610 }
611
612 /*----------------------------------------------------------------
613 * p80211knetdev_set_mac_address
614 *
615 * Handles the ioctl for changing the MACAddress of a netdevice
616 *
617 * references: linux/netdevice.h and drivers/net/net_init.c
618 *
619 * NOTE: [MSM] We only prevent address changes when the netdev is
620 * up.  We don't control anything based on dot11 state.  If the
621 * address is changed on a STA that's currently associated, you
622 * will probably lose the ability to send and receive data frames.
623 * Just be aware.  Therefore, this should usually only be done
624 * prior to scan/join/auth/assoc.
625 *
626 * Arguments:
627 *       dev     netdevice struct
628 *       addr    the new MACAddress (a struct)
629 *
630 * Returns:
631 *       zero on success, a negative errno on failure.  Possible values:
632 *               -EBUSY  device is bussy (cmd not possible)
633 *               -and errors returned by: p80211req_dorequest(..)
634 *
635 * by: Collin R. Mulliner <collin@mulliner.org>
636 ----------------------------------------------------------------*/
637 static int p80211knetdev_set_mac_address(netdevice_t *dev, void *addr)
638 {
639         struct sockaddr *new_addr = addr;
640         struct p80211msg_dot11req_mibset dot11req;
641         p80211item_unk392_t *mibattr;
642         p80211item_pstr6_t *macaddr;
643         p80211item_uint32_t *resultcode;
644         int result;
645
646         /* If we're running, we don't allow MAC address changes */
647         if (netif_running(dev))
648                 return -EBUSY;
649
650         /* Set up some convenience pointers. */
651         mibattr = &dot11req.mibattribute;
652         macaddr = (p80211item_pstr6_t *) &mibattr->data;
653         resultcode = &dot11req.resultcode;
654
655         /* Set up a dot11req_mibset */
656         memset(&dot11req, 0, sizeof(struct p80211msg_dot11req_mibset));
657         dot11req.msgcode = DIDmsg_dot11req_mibset;
658         dot11req.msglen = sizeof(struct p80211msg_dot11req_mibset);
659         memcpy(dot11req.devname,
660                ((wlandevice_t *) dev->ml_priv)->name, WLAN_DEVNAMELEN_MAX - 1);
661
662         /* Set up the mibattribute argument */
663         mibattr->did = DIDmsg_dot11req_mibset_mibattribute;
664         mibattr->status = P80211ENUM_msgitem_status_data_ok;
665         mibattr->len = sizeof(mibattr->data);
666
667         macaddr->did = DIDmib_dot11mac_dot11OperationTable_dot11MACAddress;
668         macaddr->status = P80211ENUM_msgitem_status_data_ok;
669         macaddr->len = sizeof(macaddr->data);
670         macaddr->data.len = ETH_ALEN;
671         memcpy(&macaddr->data.data, new_addr->sa_data, ETH_ALEN);
672
673         /* Set up the resultcode argument */
674         resultcode->did = DIDmsg_dot11req_mibset_resultcode;
675         resultcode->status = P80211ENUM_msgitem_status_no_value;
676         resultcode->len = sizeof(resultcode->data);
677         resultcode->data = 0;
678
679         /* now fire the request */
680         result = p80211req_dorequest(dev->ml_priv, (u8 *) &dot11req);
681
682         /* If the request wasn't successful, report an error and don't
683          * change the netdev address
684          */
685         if (result != 0 || resultcode->data != P80211ENUM_resultcode_success) {
686                 printk(KERN_ERR
687                        "Low-level driver failed dot11req_mibset(dot11MACAddress).\n");
688                 result = -EADDRNOTAVAIL;
689         } else {
690                 /* everything's ok, change the addr in netdev */
691                 memcpy(dev->dev_addr, new_addr->sa_data, dev->addr_len);
692         }
693
694         return result;
695 }
696
697 static int wlan_change_mtu(netdevice_t *dev, int new_mtu)
698 {
699         /* 2312 is max 802.11 payload, 20 is overhead, (ether + llc +snap)
700            and another 8 for wep. */
701         if ((new_mtu < 68) || (new_mtu > (2312 - 20 - 8)))
702                 return -EINVAL;
703
704         dev->mtu = new_mtu;
705
706         return 0;
707 }
708
709 static const struct net_device_ops p80211_netdev_ops = {
710         .ndo_init = p80211knetdev_init,
711         .ndo_open = p80211knetdev_open,
712         .ndo_stop = p80211knetdev_stop,
713         .ndo_get_stats = p80211knetdev_get_stats,
714         .ndo_start_xmit = p80211knetdev_hard_start_xmit,
715         .ndo_set_rx_mode = p80211knetdev_set_multicast_list,
716         .ndo_do_ioctl = p80211knetdev_do_ioctl,
717         .ndo_set_mac_address = p80211knetdev_set_mac_address,
718         .ndo_tx_timeout = p80211knetdev_tx_timeout,
719         .ndo_change_mtu = wlan_change_mtu,
720         .ndo_validate_addr = eth_validate_addr,
721 };
722
723 /*----------------------------------------------------------------
724 * wlan_setup
725 *
726 * Roughly matches the functionality of ether_setup.  Here
727 * we set up any members of the wlandevice structure that are common
728 * to all devices.  Additionally, we allocate a linux 'struct device'
729 * and perform the same setup as ether_setup.
730 *
731 * Note: It's important that the caller have setup the wlandev->name
732 *       ptr prior to calling this function.
733 *
734 * Arguments:
735 *       wlandev         ptr to the wlandev structure for the
736 *                       interface.
737 *       physdev         ptr to usb device
738 * Returns:
739 *       zero on success, non-zero otherwise.
740 * Call Context:
741 *       Should be process thread.  We'll assume it might be
742 *       interrupt though.  When we add support for statically
743 *       compiled drivers, this function will be called in the
744 *       context of the kernel startup code.
745 ----------------------------------------------------------------*/
746 int wlan_setup(wlandevice_t *wlandev, struct device *physdev)
747 {
748         int result = 0;
749         netdevice_t *netdev;
750         struct wiphy *wiphy;
751         struct wireless_dev *wdev;
752
753         /* Set up the wlandev */
754         wlandev->state = WLAN_DEVICE_CLOSED;
755         wlandev->ethconv = WLAN_ETHCONV_8021h;
756         wlandev->macmode = WLAN_MACMODE_NONE;
757
758         /* Set up the rx queue */
759         skb_queue_head_init(&wlandev->nsd_rxq);
760         tasklet_init(&wlandev->rx_bh,
761                      p80211netdev_rx_bh, (unsigned long)wlandev);
762
763         /* Allocate and initialize the wiphy struct */
764         wiphy = wlan_create_wiphy(physdev, wlandev);
765         if (wiphy == NULL) {
766                 printk(KERN_ERR "Failed to alloc wiphy.\n");
767                 return 1;
768         }
769
770         /* Allocate and initialize the struct device */
771         netdev = alloc_netdev(sizeof(struct wireless_dev), "wlan%d",
772                                 ether_setup);
773         if (netdev == NULL) {
774                 printk(KERN_ERR "Failed to alloc netdev.\n");
775                 wlan_free_wiphy(wiphy);
776                 result = 1;
777         } else {
778                 wlandev->netdev = netdev;
779                 netdev->ml_priv = wlandev;
780                 netdev->netdev_ops = &p80211_netdev_ops;
781                 wdev = netdev_priv(netdev);
782                 wdev->wiphy = wiphy;
783                 wdev->iftype = NL80211_IFTYPE_STATION;
784                 netdev->ieee80211_ptr = wdev;
785
786                 netif_stop_queue(netdev);
787                 netif_carrier_off(netdev);
788         }
789
790         return result;
791 }
792
793 /*----------------------------------------------------------------
794 * wlan_unsetup
795 *
796 * This function is paired with the wlan_setup routine.  It should
797 * be called after unregister_wlandev.  Basically, all it does is
798 * free the 'struct device' that's associated with the wlandev.
799 * We do it here because the 'struct device' isn't allocated
800 * explicitly in the driver code, it's done in wlan_setup.  To
801 * do the free in the driver might seem like 'magic'.
802 *
803 * Arguments:
804 *       wlandev         ptr to the wlandev structure for the
805 *                       interface.
806 * Call Context:
807 *       Should be process thread.  We'll assume it might be
808 *       interrupt though.  When we add support for statically
809 *       compiled drivers, this function will be called in the
810 *       context of the kernel startup code.
811 ----------------------------------------------------------------*/
812 void wlan_unsetup(wlandevice_t *wlandev)
813 {
814         struct wireless_dev *wdev;
815
816         tasklet_kill(&wlandev->rx_bh);
817
818         if (wlandev->netdev) {
819                 wdev = netdev_priv(wlandev->netdev);
820                 if (wdev->wiphy)
821                         wlan_free_wiphy(wdev->wiphy);
822                 free_netdev(wlandev->netdev);
823                 wlandev->netdev = NULL;
824         }
825 }
826
827 /*----------------------------------------------------------------
828 * register_wlandev
829 *
830 * Roughly matches the functionality of register_netdev.  This function
831 * is called after the driver has successfully probed and set up the
832 * resources for the device.  It's now ready to become a named device
833 * in the Linux system.
834 *
835 * First we allocate a name for the device (if not already set), then
836 * we call the Linux function register_netdevice.
837 *
838 * Arguments:
839 *       wlandev         ptr to the wlandev structure for the
840 *                       interface.
841 * Returns:
842 *       zero on success, non-zero otherwise.
843 * Call Context:
844 *       Can be either interrupt or not.
845 ----------------------------------------------------------------*/
846 int register_wlandev(wlandevice_t *wlandev)
847 {
848         return register_netdev(wlandev->netdev);
849 }
850
851 /*----------------------------------------------------------------
852 * unregister_wlandev
853 *
854 * Roughly matches the functionality of unregister_netdev.  This
855 * function is called to remove a named device from the system.
856 *
857 * First we tell linux that the device should no longer exist.
858 * Then we remove it from the list of known wlan devices.
859 *
860 * Arguments:
861 *       wlandev         ptr to the wlandev structure for the
862 *                       interface.
863 * Returns:
864 *       zero on success, non-zero otherwise.
865 * Call Context:
866 *       Can be either interrupt or not.
867 ----------------------------------------------------------------*/
868 int unregister_wlandev(wlandevice_t *wlandev)
869 {
870         struct sk_buff *skb;
871
872         unregister_netdev(wlandev->netdev);
873
874         /* Now to clean out the rx queue */
875         while ((skb = skb_dequeue(&wlandev->nsd_rxq)))
876                 dev_kfree_skb(skb);
877
878         return 0;
879 }
880
881 /*----------------------------------------------------------------
882 * p80211netdev_hwremoved
883 *
884 * Hardware removed notification. This function should be called
885 * immediately after an MSD has detected that the underlying hardware
886 * has been yanked out from under us.  The primary things we need
887 * to do are:
888 *   - Mark the wlandev
889 *   - Prevent any further traffic from the knetdev i/f
890 *   - Prevent any further requests from mgmt i/f
891 *   - If there are any waitq'd mgmt requests or mgmt-frame exchanges,
892 *     shut them down.
893 *   - Call the MSD hwremoved function.
894 *
895 * The remainder of the cleanup will be handled by unregister().
896 * Our primary goal here is to prevent as much tickling of the MSD
897 * as possible since the MSD is already in a 'wounded' state.
898 *
899 * TODO: As new features are added, this function should be
900 *       updated.
901 *
902 * Arguments:
903 *       wlandev         WLAN network device structure
904 * Returns:
905 *       nothing
906 * Side effects:
907 *
908 * Call context:
909 *       Usually interrupt.
910 ----------------------------------------------------------------*/
911 void p80211netdev_hwremoved(wlandevice_t *wlandev)
912 {
913         wlandev->hwremoved = 1;
914         if (wlandev->state == WLAN_DEVICE_OPEN)
915                 netif_stop_queue(wlandev->netdev);
916
917         netif_device_detach(wlandev->netdev);
918 }
919
920 /*----------------------------------------------------------------
921 * p80211_rx_typedrop
922 *
923 * Classifies the frame, increments the appropriate counter, and
924 * returns 0|1|2 indicating whether the driver should handle, ignore, or
925 * drop the frame
926 *
927 * Arguments:
928 *       wlandev         wlan device structure
929 *       fc              frame control field
930 *
931 * Returns:
932 *       zero if the frame should be handled by the driver,
933 *       one if the frame should be ignored
934 *       anything else means we drop it.
935 *
936 * Side effects:
937 *
938 * Call context:
939 *       interrupt
940 ----------------------------------------------------------------*/
941 static int p80211_rx_typedrop(wlandevice_t *wlandev, u16 fc)
942 {
943         u16 ftype;
944         u16 fstype;
945         int drop = 0;
946         /* Classify frame, increment counter */
947         ftype = WLAN_GET_FC_FTYPE(fc);
948         fstype = WLAN_GET_FC_FSTYPE(fc);
949 #if 0
950         pr_debug("rx_typedrop : ftype=%d fstype=%d.\n", ftype, fstype);
951 #endif
952         switch (ftype) {
953         case WLAN_FTYPE_MGMT:
954                 if ((wlandev->netdev->flags & IFF_PROMISC) ||
955                     (wlandev->netdev->flags & IFF_ALLMULTI)) {
956                         drop = 1;
957                         break;
958                 }
959                 pr_debug("rx'd mgmt:\n");
960                 wlandev->rx.mgmt++;
961                 switch (fstype) {
962                 case WLAN_FSTYPE_ASSOCREQ:
963                         /* printk("assocreq"); */
964                         wlandev->rx.assocreq++;
965                         break;
966                 case WLAN_FSTYPE_ASSOCRESP:
967                         /* printk("assocresp"); */
968                         wlandev->rx.assocresp++;
969                         break;
970                 case WLAN_FSTYPE_REASSOCREQ:
971                         /* printk("reassocreq"); */
972                         wlandev->rx.reassocreq++;
973                         break;
974                 case WLAN_FSTYPE_REASSOCRESP:
975                         /* printk("reassocresp"); */
976                         wlandev->rx.reassocresp++;
977                         break;
978                 case WLAN_FSTYPE_PROBEREQ:
979                         /* printk("probereq"); */
980                         wlandev->rx.probereq++;
981                         break;
982                 case WLAN_FSTYPE_PROBERESP:
983                         /* printk("proberesp"); */
984                         wlandev->rx.proberesp++;
985                         break;
986                 case WLAN_FSTYPE_BEACON:
987                         /* printk("beacon"); */
988                         wlandev->rx.beacon++;
989                         break;
990                 case WLAN_FSTYPE_ATIM:
991                         /* printk("atim"); */
992                         wlandev->rx.atim++;
993                         break;
994                 case WLAN_FSTYPE_DISASSOC:
995                         /* printk("disassoc"); */
996                         wlandev->rx.disassoc++;
997                         break;
998                 case WLAN_FSTYPE_AUTHEN:
999                         /* printk("authen"); */
1000                         wlandev->rx.authen++;
1001                         break;
1002                 case WLAN_FSTYPE_DEAUTHEN:
1003                         /* printk("deauthen"); */
1004                         wlandev->rx.deauthen++;
1005                         break;
1006                 default:
1007                         /* printk("unknown"); */
1008                         wlandev->rx.mgmt_unknown++;
1009                         break;
1010                 }
1011                 /* printk("\n"); */
1012                 drop = 2;
1013                 break;
1014
1015         case WLAN_FTYPE_CTL:
1016                 if ((wlandev->netdev->flags & IFF_PROMISC) ||
1017                     (wlandev->netdev->flags & IFF_ALLMULTI)) {
1018                         drop = 1;
1019                         break;
1020                 }
1021                 pr_debug("rx'd ctl:\n");
1022                 wlandev->rx.ctl++;
1023                 switch (fstype) {
1024                 case WLAN_FSTYPE_PSPOLL:
1025                         /* printk("pspoll"); */
1026                         wlandev->rx.pspoll++;
1027                         break;
1028                 case WLAN_FSTYPE_RTS:
1029                         /* printk("rts"); */
1030                         wlandev->rx.rts++;
1031                         break;
1032                 case WLAN_FSTYPE_CTS:
1033                         /* printk("cts"); */
1034                         wlandev->rx.cts++;
1035                         break;
1036                 case WLAN_FSTYPE_ACK:
1037                         /* printk("ack"); */
1038                         wlandev->rx.ack++;
1039                         break;
1040                 case WLAN_FSTYPE_CFEND:
1041                         /* printk("cfend"); */
1042                         wlandev->rx.cfend++;
1043                         break;
1044                 case WLAN_FSTYPE_CFENDCFACK:
1045                         /* printk("cfendcfack"); */
1046                         wlandev->rx.cfendcfack++;
1047                         break;
1048                 default:
1049                         /* printk("unknown"); */
1050                         wlandev->rx.ctl_unknown++;
1051                         break;
1052                 }
1053                 /* printk("\n"); */
1054                 drop = 2;
1055                 break;
1056
1057         case WLAN_FTYPE_DATA:
1058                 wlandev->rx.data++;
1059                 switch (fstype) {
1060                 case WLAN_FSTYPE_DATAONLY:
1061                         wlandev->rx.dataonly++;
1062                         break;
1063                 case WLAN_FSTYPE_DATA_CFACK:
1064                         wlandev->rx.data_cfack++;
1065                         break;
1066                 case WLAN_FSTYPE_DATA_CFPOLL:
1067                         wlandev->rx.data_cfpoll++;
1068                         break;
1069                 case WLAN_FSTYPE_DATA_CFACK_CFPOLL:
1070                         wlandev->rx.data__cfack_cfpoll++;
1071                         break;
1072                 case WLAN_FSTYPE_NULL:
1073                         pr_debug("rx'd data:null\n");
1074                         wlandev->rx.null++;
1075                         break;
1076                 case WLAN_FSTYPE_CFACK:
1077                         pr_debug("rx'd data:cfack\n");
1078                         wlandev->rx.cfack++;
1079                         break;
1080                 case WLAN_FSTYPE_CFPOLL:
1081                         pr_debug("rx'd data:cfpoll\n");
1082                         wlandev->rx.cfpoll++;
1083                         break;
1084                 case WLAN_FSTYPE_CFACK_CFPOLL:
1085                         pr_debug("rx'd data:cfack_cfpoll\n");
1086                         wlandev->rx.cfack_cfpoll++;
1087                         break;
1088                 default:
1089                         /* printk("unknown"); */
1090                         wlandev->rx.data_unknown++;
1091                         break;
1092                 }
1093
1094                 break;
1095         }
1096         return drop;
1097 }
1098
1099 static void p80211knetdev_tx_timeout(netdevice_t *netdev)
1100 {
1101         wlandevice_t *wlandev = netdev->ml_priv;
1102
1103         if (wlandev->tx_timeout) {
1104                 wlandev->tx_timeout(wlandev);
1105         } else {
1106                 printk(KERN_WARNING "Implement tx_timeout for %s\n",
1107                        wlandev->nsdname);
1108                 netif_wake_queue(wlandev->netdev);
1109         }
1110 }