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