2 * Copyright (c) 2010 Broadcom Corporation
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/kthread.h>
20 #include <linux/slab.h>
21 #include <linux/skbuff.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/mmc/sdio_func.h>
25 #include <linux/random.h>
26 #include <linux/spinlock.h>
27 #include <linux/ethtool.h>
28 #include <linux/fcntl.h>
30 #include <linux/uaccess.h>
31 #include <net/cfg80211.h>
32 #if defined(CONFIG_HAS_EARLYSUSPEND)
33 #include <linux/earlysuspend.h>
36 #include <brcmu_utils.h>
37 #include <brcmu_wifi.h>
39 #include "dngl_stats.h"
42 #include "dhd_proto.h"
44 #include "wl_cfg80211.h"
46 /* Global ASSERT type flag */
49 #if defined(CONFIG_PM_SLEEP)
50 #include <linux/suspend.h>
51 atomic_t brcmf_mmc_suspend;
52 DECLARE_WAIT_QUEUE_HEAD(dhd_dpc_wait);
53 #endif /* defined(CONFIG_PM_SLEEP) */
55 MODULE_AUTHOR("Broadcom Corporation");
56 MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN fullmac driver.");
57 MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN fullmac cards");
58 MODULE_LICENSE("Dual BSD/GPL");
61 /* Interface control information */
62 typedef struct dhd_if {
63 struct dhd_info *info; /* back pointer to dhd_info */
64 /* OS/stack specifics */
65 struct net_device *net;
66 struct net_device_stats stats;
67 int idx; /* iface idx in dongle */
68 int state; /* interface state */
69 uint subunit; /* subunit */
70 u8 mac_addr[ETH_ALEN]; /* assigned MAC address */
71 bool attached; /* Delayed attachment when unset */
72 bool txflowcontrol; /* Per interface flow control indicator */
73 char name[IFNAMSIZ]; /* linux interface name */
76 /* Local private structure (extension of pub) */
77 typedef struct dhd_info {
80 /* OS/stack specifics */
81 dhd_if_t *iflist[DHD_MAX_IFS];
83 struct semaphore proto_sem;
84 wait_queue_head_t ioctl_resp_wait;
85 struct timer_list timer;
87 struct tasklet_struct tasklet;
89 /* Thread based operation */
91 struct semaphore sdsem;
92 struct task_struct *watchdog_tsk;
93 struct semaphore watchdog_sem;
94 struct task_struct *dpc_tsk;
95 struct semaphore dpc_sem;
97 /* Thread to issue ioctl for multicast */
98 struct task_struct *sysioc_tsk;
99 struct semaphore sysioc_sem;
102 u8 macvalue[ETH_ALEN];
103 wait_queue_head_t ctrl_wait;
104 atomic_t pend_8021x_cnt;
106 #ifdef CONFIG_HAS_EARLYSUSPEND
107 struct early_suspend early_suspend;
108 #endif /* CONFIG_HAS_EARLYSUSPEND */
111 /* Definitions to provide path to the firmware and nvram
112 * example nvram_path[MOD_PARAM_PATHLEN]="/projects/wlan/nvram.txt"
114 char firmware_path[MOD_PARAM_PATHLEN];
115 char nvram_path[MOD_PARAM_PATHLEN];
117 /* load firmware and/or nvram values from the filesystem */
118 module_param_string(firmware_path, firmware_path, MOD_PARAM_PATHLEN, 0);
119 module_param_string(nvram_path, nvram_path, MOD_PARAM_PATHLEN, 0);
121 /* No firmware required */
122 bool brcmf_no_fw_req;
123 module_param(brcmf_no_fw_req, bool, 0);
126 module_param(brcmf_msg_level, int, 0);
128 /* Spawn a thread for system ioctls (set mac, set mcast) */
129 uint brcmf_sysioc = true;
130 module_param(brcmf_sysioc, uint, 0);
132 /* Watchdog interval */
133 uint brcmf_watchdog_ms = 10;
134 module_param(brcmf_watchdog_ms, uint, 0);
137 /* Console poll interval */
138 uint brcmf_console_ms;
139 module_param(brcmf_console_ms, uint, 0);
142 /* ARP offload agent mode : Enable ARP Host Auto-Reply
143 and ARP Peer Auto-Reply */
144 uint brcmf_arp_mode = 0xb;
145 module_param(brcmf_arp_mode, uint, 0);
147 /* ARP offload enable */
148 uint brcmf_arp_enable = true;
149 module_param(brcmf_arp_enable, uint, 0);
151 /* Global Pkt filter enable control */
152 uint brcmf_pkt_filter_enable = true;
153 module_param(brcmf_pkt_filter_enable, uint, 0);
155 /* Pkt filter init setup */
156 uint brcmf_pkt_filter_init;
157 module_param(brcmf_pkt_filter_init, uint, 0);
159 /* Pkt filter mode control */
160 uint brcmf_master_mode = true;
161 module_param(brcmf_master_mode, uint, 1);
163 /* Watchdog thread priority, -1 to use kernel timer */
164 int brcmf_watchdog_prio = 97;
165 module_param(brcmf_watchdog_prio, int, 0);
167 /* DPC thread priority, -1 to use tasklet */
168 int brcmf_dpc_prio = 98;
169 module_param(brcmf_dpc_prio, int, 0);
171 /* DPC thread priority, -1 to use tasklet */
172 extern int brcmf_dongle_memsize;
173 module_param(brcmf_dongle_memsize, int, 0);
175 /* Contorl fw roaming */
178 /* Control radio state */
179 uint brcmf_radio_up = 1;
181 /* Network inteface name */
182 char iface_name[IFNAMSIZ] = "wlan";
183 module_param_string(iface_name, iface_name, IFNAMSIZ, 0);
185 /* The following are specific to the SDIO dongle */
187 /* IOCTL response timeout */
188 int brcmf_ioctl_timeout_msec = IOCTL_RESP_TIMEOUT;
190 /* Idle timeout for backplane clock */
191 int brcmf_idletime = BRCMF_IDLETIME_TICKS;
192 module_param(brcmf_idletime, int, 0);
196 module_param(brcmf_poll, uint, 0);
199 uint brcmf_intr = true;
200 module_param(brcmf_intr, uint, 0);
202 /* SDIO Drive Strength (in milliamps) */
203 uint brcmf_sdiod_drive_strength = 6;
204 module_param(brcmf_sdiod_drive_strength, uint, 0);
207 extern uint brcmf_txbound;
208 extern uint brcmf_rxbound;
209 module_param(brcmf_txbound, uint, 0);
210 module_param(brcmf_rxbound, uint, 0);
212 /* Deferred transmits */
213 extern uint brcmf_deferred_tx;
214 module_param(brcmf_deferred_tx, uint, 0);
217 /* Echo packet generator (pkts/s) */
219 module_param(brcmf_pktgen, uint, 0);
221 /* Echo packet len (0 => sawtooth, max 2040) */
222 uint brcmf_pktgen_len;
223 module_param(brcmf_pktgen_len, uint, 0);
226 /* Version string to report */
228 #define DHD_COMPILED "\nCompiled in " SRCBASE
233 static void brcmf_dpc(unsigned long data);
234 static int brcmf_toe_get(dhd_info_t *dhd, int idx, u32 *toe_ol);
235 static int brcmf_toe_set(dhd_info_t *dhd, int idx, u32 toe_ol);
236 static int brcmf_host_event(dhd_info_t *dhd, int *ifidx, void *pktdata,
237 brcmf_event_msg_t *event_ptr, void **data_ptr);
239 static void brcmf_set_packet_filter(int value, dhd_pub_t *dhd)
241 DHD_TRACE(("%s: %d\n", __func__, value));
242 /* 1 - Enable packet filter, only allow unicast packet to send up */
243 /* 0 - Disable packet filter */
244 if (brcmf_pkt_filter_enable) {
247 for (i = 0; i < dhd->pktfilter_count; i++) {
248 brcmf_c_pktfilter_offload_set(dhd, dhd->pktfilter[i]);
249 brcmf_c_pktfilter_offload_enable(dhd, dhd->pktfilter[i],
250 value, brcmf_master_mode);
255 #if defined(CONFIG_HAS_EARLYSUSPEND)
256 static int brcmf_set_suspend(int value, dhd_pub_t *dhd)
258 int power_mode = PM_MAX;
259 /* wl_pkt_filter_enable_t enable_parm; */
263 DHD_TRACE(("%s: enter, value = %d in_suspend=%d\n",
264 __func__, value, dhd->in_suspend));
266 if (dhd && dhd->up) {
267 if (value && dhd->in_suspend) {
269 /* Kernel suspended */
270 DHD_TRACE(("%s: force extra Suspend setting\n",
273 brcmf_proto_cdc_set_ioctl(dhd, 0, BRCMF_C_SET_PM,
277 /* Enable packet filter, only allow unicast
279 brcmf_set_packet_filter(1, dhd);
281 /* if dtim skip setup as default force it
282 * to wake each third dtim
283 * for better power saving.
284 * Note that side effect is chance to miss BC/MC
287 if ((dhd->dtim_skip == 0) || (dhd->dtim_skip == 1))
290 bcn_li_dtim = dhd->dtim_skip;
291 brcmu_mkiovar("bcn_li_dtim", (char *)&bcn_li_dtim,
292 4, iovbuf, sizeof(iovbuf));
293 brcmf_proto_cdc_set_ioctl(dhd, 0, BRCMF_C_SET_VAR,
294 iovbuf, sizeof(iovbuf));
298 DHD_TRACE(("%s: Remove extra suspend setting\n",
301 power_mode = PM_FAST;
302 brcmf_proto_cdc_set_ioctl(dhd, 0, BRCMF_C_SET_PM,
306 /* disable pkt filter */
307 brcmf_set_packet_filter(0, dhd);
309 /* restore pre-suspend setting for dtim_skip */
310 brcmu_mkiovar("bcn_li_dtim", (char *)&dhd->dtim_skip,
311 4, iovbuf, sizeof(iovbuf));
313 brcmf_proto_cdc_set_ioctl(dhd, 0, BRCMF_C_SET_VAR,
314 iovbuf, sizeof(iovbuf));
321 static void brcmf_suspend_resume_helper(struct dhd_info *dhd, int val)
323 dhd_pub_t *dhdp = &dhd->pub;
325 brcmf_os_proto_block(dhdp);
326 /* Set flag when early suspend was called */
327 dhdp->in_suspend = val;
328 if (!dhdp->suspend_disable_flag)
329 brcmf_set_suspend(val, dhdp);
330 brcmf_os_proto_unblock(dhdp);
333 static void brcmf_early_suspend(struct early_suspend *h)
335 struct dhd_info *dhd = container_of(h, struct dhd_info, early_suspend);
337 DHD_TRACE(("%s: enter\n", __func__));
340 dhd_suspend_resume_helper(dhd, 1);
344 static void brcmf_late_resume(struct early_suspend *h)
346 struct dhd_info *dhd = container_of(h, struct dhd_info, early_suspend);
348 DHD_TRACE(("%s: enter\n", __func__));
351 dhd_suspend_resume_helper(dhd, 0);
353 #endif /* defined(CONFIG_HAS_EARLYSUSPEND) */
356 * Generalized timeout mechanism. Uses spin sleep with exponential
358 * the sleep time reaches one jiffy, then switches over to task delay. Usage:
360 * brcmf_timeout_start(&tmo, usec);
361 * while (!brcmf_timeout_expired(&tmo))
362 * if (poll_something())
364 * if (brcmf_timeout_expired(&tmo))
368 void brcmf_timeout_start(dhd_timeout_t *tmo, uint usec)
373 tmo->tick = 1000000 / HZ;
376 int brcmf_timeout_expired(dhd_timeout_t *tmo)
378 /* Does nothing the first call */
379 if (tmo->increment == 0) {
384 if (tmo->elapsed >= tmo->limit)
387 /* Add the delay that's about to take place */
388 tmo->elapsed += tmo->increment;
390 if (tmo->increment < tmo->tick) {
391 udelay(tmo->increment);
393 if (tmo->increment > tmo->tick)
394 tmo->increment = tmo->tick;
396 wait_queue_head_t delay_wait;
397 DECLARE_WAITQUEUE(wait, current);
399 init_waitqueue_head(&delay_wait);
400 add_wait_queue(&delay_wait, &wait);
401 set_current_state(TASK_INTERRUPTIBLE);
403 pending = signal_pending(current);
404 remove_wait_queue(&delay_wait, &wait);
405 set_current_state(TASK_RUNNING);
407 return 1; /* Interrupted */
413 static int brcmf_net2idx(dhd_info_t *dhd, struct net_device *net)
418 while (i < DHD_MAX_IFS) {
419 if (dhd->iflist[i] && (dhd->iflist[i]->net == net))
427 int brcmf_ifname2idx(dhd_info_t *dhd, char *name)
433 if (name == NULL || *name == '\0')
438 && !strncmp(dhd->iflist[i]->name, name, IFNAMSIZ))
441 DHD_TRACE(("%s: return idx %d for \"%s\"\n", __func__, i, name));
443 return i; /* default - the primary interface */
446 char *brcmf_ifname(dhd_pub_t *dhdp, int ifidx)
448 dhd_info_t *dhd = (dhd_info_t *) dhdp->info;
452 if (ifidx < 0 || ifidx >= DHD_MAX_IFS) {
453 DHD_ERROR(("%s: ifidx %d out of range\n", __func__, ifidx));
457 if (dhd->iflist[ifidx] == NULL) {
458 DHD_ERROR(("%s: null i/f %d\n", __func__, ifidx));
462 if (dhd->iflist[ifidx]->net)
463 return dhd->iflist[ifidx]->net->name;
468 static void _brcmf_set_multicast_list(dhd_info_t *dhd, int ifidx)
470 struct net_device *dev;
471 struct netdev_hw_addr *ha;
479 ASSERT(dhd && dhd->iflist[ifidx]);
480 dev = dhd->iflist[ifidx]->net;
481 cnt = netdev_mc_count(dev);
483 /* Determine initial value of allmulti flag */
484 allmulti = (dev->flags & IFF_ALLMULTI) ? true : false;
486 /* Send down the multicast list first. */
488 buflen = sizeof("mcast_list") + sizeof(cnt) + (cnt * ETH_ALEN);
489 bufp = buf = kmalloc(buflen, GFP_ATOMIC);
491 DHD_ERROR(("%s: out of memory for mcast_list, cnt %d\n",
492 brcmf_ifname(&dhd->pub, ifidx), cnt));
496 strcpy(bufp, "mcast_list");
497 bufp += strlen("mcast_list") + 1;
499 cnt = cpu_to_le32(cnt);
500 memcpy(bufp, &cnt, sizeof(cnt));
503 netdev_for_each_mc_addr(ha, dev) {
506 memcpy(bufp, ha->addr, ETH_ALEN);
511 memset(&ioc, 0, sizeof(ioc));
512 ioc.cmd = BRCMF_C_SET_VAR;
517 ret = brcmf_proto_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
519 DHD_ERROR(("%s: set mcast_list failed, cnt %d\n",
520 brcmf_ifname(&dhd->pub, ifidx), cnt));
521 allmulti = cnt ? true : allmulti;
526 /* Now send the allmulti setting. This is based on the setting in the
527 * net_device flags, but might be modified above to be turned on if we
528 * were trying to set some addresses and dongle rejected it...
531 buflen = sizeof("allmulti") + sizeof(allmulti);
532 buf = kmalloc(buflen, GFP_ATOMIC);
534 DHD_ERROR(("%s: out of memory for allmulti\n",
535 brcmf_ifname(&dhd->pub, ifidx)));
538 allmulti = cpu_to_le32(allmulti);
541 ("allmulti", (void *)&allmulti, sizeof(allmulti), buf, buflen)) {
542 DHD_ERROR(("%s: mkiovar failed for allmulti, datalen %d "
543 "buflen %u\n", brcmf_ifname(&dhd->pub, ifidx),
544 (int)sizeof(allmulti), buflen));
549 memset(&ioc, 0, sizeof(ioc));
550 ioc.cmd = BRCMF_C_SET_VAR;
555 ret = brcmf_proto_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
557 DHD_ERROR(("%s: set allmulti %d failed\n",
558 brcmf_ifname(&dhd->pub, ifidx),
559 le32_to_cpu(allmulti)));
564 /* Finally, pick up the PROMISC flag as well, like the NIC
567 allmulti = (dev->flags & IFF_PROMISC) ? true : false;
568 allmulti = cpu_to_le32(allmulti);
570 memset(&ioc, 0, sizeof(ioc));
571 ioc.cmd = BRCMF_C_SET_PROMISC;
573 ioc.len = sizeof(allmulti);
576 ret = brcmf_proto_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
578 DHD_ERROR(("%s: set promisc %d failed\n",
579 brcmf_ifname(&dhd->pub, ifidx),
580 le32_to_cpu(allmulti)));
584 static int _brcmf_set_mac_address(dhd_info_t *dhd, int ifidx, u8 *addr)
590 DHD_TRACE(("%s enter\n", __func__));
592 ("cur_etheraddr", (char *)addr, ETH_ALEN, buf, 32)) {
593 DHD_ERROR(("%s: mkiovar failed for cur_etheraddr\n",
594 brcmf_ifname(&dhd->pub, ifidx)));
597 memset(&ioc, 0, sizeof(ioc));
598 ioc.cmd = BRCMF_C_SET_VAR;
603 ret = brcmf_proto_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
605 DHD_ERROR(("%s: set cur_etheraddr failed\n",
606 brcmf_ifname(&dhd->pub, ifidx)));
608 memcpy(dhd->iflist[ifidx]->net->dev_addr, addr, ETH_ALEN);
615 extern struct net_device *ap_net_dev;
618 static void brcmf_op_if(dhd_if_t *ifp)
621 int ret = 0, err = 0;
623 ASSERT(ifp && ifp->info && ifp->idx); /* Virtual interfaces only */
627 DHD_TRACE(("%s: idx %d, state %d\n", __func__, ifp->idx, ifp->state));
629 switch (ifp->state) {
632 * Delete the existing interface before overwriting it
633 * in case we missed the BRCMF_E_IF_DEL event.
635 if (ifp->net != NULL) {
636 DHD_ERROR(("%s: ERROR: netdev:%s already exists, "
637 "try free & unregister\n",
638 __func__, ifp->net->name));
639 netif_stop_queue(ifp->net);
640 unregister_netdev(ifp->net);
641 free_netdev(ifp->net);
643 /* Allocate etherdev, including space for private structure */
644 ifp->net = alloc_etherdev(sizeof(dhd));
646 DHD_ERROR(("%s: OOM - alloc_etherdev\n", __func__));
650 strcpy(ifp->net->name, ifp->name);
651 memcpy(netdev_priv(ifp->net), &dhd, sizeof(dhd));
652 err = brcmf_net_attach(&dhd->pub, ifp->idx);
654 DHD_ERROR(("%s: dhd_net_attach failed, "
660 /* semaphore that the soft AP CODE
662 extern struct semaphore ap_eth_sema;
664 /* save ptr to wl0.1 netdev for use
666 ap_net_dev = ifp->net;
667 /* signal to the SOFTAP 'sleeper' thread,
671 DHD_TRACE(("\n ==== pid:%x, net_device for "
672 "if:%s created ===\n\n",
673 current->pid, ifp->net->name));
679 if (ifp->net != NULL) {
680 DHD_TRACE(("\n%s: got 'WLC_E_IF_DEL' state\n",
682 netif_stop_queue(ifp->net);
683 unregister_netdev(ifp->net);
684 ret = DHD_DEL_IF; /* Make sure the free_netdev()
689 DHD_ERROR(("%s: bad op %d\n", __func__, ifp->state));
696 free_netdev(ifp->net);
698 dhd->iflist[ifp->idx] = NULL;
701 if (ifp->net == ap_net_dev)
702 ap_net_dev = NULL; /* NULL SOFTAP global
708 static int _brcmf_sysioc_thread(void *data)
710 dhd_info_t *dhd = (dhd_info_t *) data;
716 allow_signal(SIGTERM);
718 while (down_interruptible(&dhd->sysioc_sem) == 0) {
719 if (kthread_should_stop())
721 for (i = 0; i < DHD_MAX_IFS; i++) {
722 if (dhd->iflist[i]) {
724 in_ap = (ap_net_dev != NULL);
726 if (dhd->iflist[i]->state)
727 brcmf_op_if(dhd->iflist[i]);
729 if (dhd->iflist[i] == NULL) {
730 DHD_TRACE(("\n\n %s: interface %d "
731 "removed!\n", __func__, i));
735 if (in_ap && dhd->set_macaddress) {
736 DHD_TRACE(("attempt to set MAC for %s "
737 "in AP Mode," "blocked.\n",
738 dhd->iflist[i]->net->name));
739 dhd->set_macaddress = false;
743 if (in_ap && dhd->set_multicast) {
744 DHD_TRACE(("attempt to set MULTICAST list for %s" "in AP Mode, blocked.\n",
745 dhd->iflist[i]->net->name));
746 dhd->set_multicast = false;
750 if (dhd->set_multicast) {
751 dhd->set_multicast = false;
752 _brcmf_set_multicast_list(dhd, i);
754 if (dhd->set_macaddress) {
755 dhd->set_macaddress = false;
756 _brcmf_set_mac_address(dhd, i,
765 static int brcmf_netdev_set_mac_address(struct net_device *dev, void *addr)
769 dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(dev);
770 struct sockaddr *sa = (struct sockaddr *)addr;
773 ifidx = brcmf_net2idx(dhd, dev);
774 if (ifidx == DHD_BAD_IF)
777 ASSERT(dhd->sysioc_tsk);
778 memcpy(&dhd->macvalue, sa->sa_data, ETH_ALEN);
779 dhd->set_macaddress = true;
780 up(&dhd->sysioc_sem);
785 static void brcmf_netdev_set_multicast_list(struct net_device *dev)
787 dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(dev);
790 ifidx = brcmf_net2idx(dhd, dev);
791 if (ifidx == DHD_BAD_IF)
794 ASSERT(dhd->sysioc_tsk);
795 dhd->set_multicast = true;
796 up(&dhd->sysioc_sem);
799 int brcmf_sendpkt(dhd_pub_t *dhdp, int ifidx, struct sk_buff *pktbuf)
801 dhd_info_t *dhd = (dhd_info_t *) (dhdp->info);
804 if (!dhdp->up || (dhdp->busstate == DHD_BUS_DOWN))
807 /* Update multicast statistic */
808 if (pktbuf->len >= ETH_ALEN) {
809 u8 *pktdata = (u8 *) (pktbuf->data);
810 struct ethhdr *eh = (struct ethhdr *)pktdata;
812 if (is_multicast_ether_addr(eh->h_dest))
813 dhdp->tx_multicast++;
814 if (ntohs(eh->h_proto) == ETH_P_PAE)
815 atomic_inc(&dhd->pend_8021x_cnt);
818 /* If the protocol uses a data header, apply it */
819 brcmf_proto_hdrpush(dhdp, ifidx, pktbuf);
821 /* Use bus module to send data frame */
822 return brcmf_sdbrcm_bus_txdata(dhdp->bus, pktbuf);
825 static int brcmf_netdev_start_xmit(struct sk_buff *skb, struct net_device *net)
828 dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
831 DHD_TRACE(("%s: Enter\n", __func__));
834 if (!dhd->pub.up || (dhd->pub.busstate == DHD_BUS_DOWN)) {
835 DHD_ERROR(("%s: xmit rejected pub.up=%d busstate=%d\n",
836 __func__, dhd->pub.up, dhd->pub.busstate));
837 netif_stop_queue(net);
841 ifidx = brcmf_net2idx(dhd, net);
842 if (ifidx == DHD_BAD_IF) {
843 DHD_ERROR(("%s: bad ifidx %d\n", __func__, ifidx));
844 netif_stop_queue(net);
848 /* Make sure there's enough room for any header */
849 if (skb_headroom(skb) < dhd->pub.hdrlen) {
850 struct sk_buff *skb2;
852 DHD_INFO(("%s: insufficient headroom\n",
853 brcmf_ifname(&dhd->pub, ifidx)));
854 dhd->pub.tx_realloc++;
855 skb2 = skb_realloc_headroom(skb, dhd->pub.hdrlen);
859 DHD_ERROR(("%s: skb_realloc_headroom failed\n",
860 brcmf_ifname(&dhd->pub, ifidx)));
866 ret = brcmf_sendpkt(&dhd->pub, ifidx, skb);
870 dhd->pub.dstats.tx_dropped++;
872 dhd->pub.tx_packets++;
874 /* Return ok: we always eat the packet */
878 void brcmf_txflowcontrol(dhd_pub_t *dhdp, int ifidx, bool state)
880 struct net_device *net;
881 dhd_info_t *dhd = dhdp->info;
883 DHD_TRACE(("%s: Enter\n", __func__));
886 ASSERT(dhd && dhd->iflist[ifidx]);
887 net = dhd->iflist[ifidx]->net;
889 netif_stop_queue(net);
891 netif_wake_queue(net);
894 void brcmf_rx_frame(dhd_pub_t *dhdp, int ifidx, struct sk_buff *skb,
897 dhd_info_t *dhd = (dhd_info_t *) dhdp->info;
901 struct sk_buff *pnext, *save_pktbuf;
904 brcmf_event_msg_t event;
906 DHD_TRACE(("%s: Enter\n", __func__));
910 for (i = 0; skb && i < numpkt; i++, skb = pnext) {
915 /* Get the protocol, maintain skb around eth_type_trans()
916 * The main reason for this hack is for the limitation of
917 * Linux 2.4 where 'eth_type_trans' uses the
918 * 'net->hard_header_len'
919 * to perform skb_pull inside vs ETH_HLEN. Since to avoid
920 * coping of the packet coming from the network stack to add
921 * BDC, Hardware header etc, during network interface
923 * we set the 'net->hard_header_len' to ETH_HLEN + extra space
925 * for BDC, Hardware header etc. and not just the ETH_HLEN
930 ifp = dhd->iflist[ifidx];
932 ifp = dhd->iflist[0];
936 skb->protocol = eth_type_trans(skb, skb->dev);
938 if (skb->pkt_type == PACKET_MULTICAST)
939 dhd->pub.rx_multicast++;
944 /* Strip header, count, deliver upward */
945 skb_pull(skb, ETH_HLEN);
947 /* Process special event packets and then discard them */
948 if (ntohs(skb->protocol) == ETH_P_LINK_CTL)
949 brcmf_host_event(dhd, &ifidx,
953 ASSERT(ifidx < DHD_MAX_IFS && dhd->iflist[ifidx]);
954 if (dhd->iflist[ifidx] && !dhd->iflist[ifidx]->state)
955 ifp = dhd->iflist[ifidx];
958 ifp->net->last_rx = jiffies;
960 dhdp->dstats.rx_bytes += skb->len;
961 dhdp->rx_packets++; /* Local count */
963 if (in_interrupt()) {
966 /* If the receive is not processed inside an ISR,
967 * the softirqd must be woken explicitly to service
968 * the NET_RX_SOFTIRQ. In 2.6 kernels, this is handled
969 * by netif_rx_ni(), but in earlier kernels, we need
977 void brcmf_event(struct dhd_info *dhd, char *evpkt, int evlen, int ifidx)
979 /* Linux version has nothing to do */
983 void brcmf_txcomplete(dhd_pub_t *dhdp, struct sk_buff *txp, bool success)
986 dhd_info_t *dhd = (dhd_info_t *) (dhdp->info);
990 brcmf_proto_hdrpull(dhdp, &ifidx, txp);
992 eh = (struct ethhdr *)(txp->data);
993 type = ntohs(eh->h_proto);
995 if (type == ETH_P_PAE)
996 atomic_dec(&dhd->pend_8021x_cnt);
1000 static struct net_device_stats *brcmf_netdev_get_stats(struct net_device *net)
1002 dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1006 DHD_TRACE(("%s: Enter\n", __func__));
1008 ifidx = brcmf_net2idx(dhd, net);
1009 if (ifidx == DHD_BAD_IF)
1012 ifp = dhd->iflist[ifidx];
1016 /* Use the protocol to get dongle stats */
1017 brcmf_proto_dstats(&dhd->pub);
1020 /* Copy dongle stats to net device stats */
1021 ifp->stats.rx_packets = dhd->pub.dstats.rx_packets;
1022 ifp->stats.tx_packets = dhd->pub.dstats.tx_packets;
1023 ifp->stats.rx_bytes = dhd->pub.dstats.rx_bytes;
1024 ifp->stats.tx_bytes = dhd->pub.dstats.tx_bytes;
1025 ifp->stats.rx_errors = dhd->pub.dstats.rx_errors;
1026 ifp->stats.tx_errors = dhd->pub.dstats.tx_errors;
1027 ifp->stats.rx_dropped = dhd->pub.dstats.rx_dropped;
1028 ifp->stats.tx_dropped = dhd->pub.dstats.tx_dropped;
1029 ifp->stats.multicast = dhd->pub.dstats.multicast;
1034 static int brcmf_watchdog_thread(void *data)
1036 dhd_info_t *dhd = (dhd_info_t *) data;
1038 /* This thread doesn't need any user-level access,
1039 * so get rid of all our resources
1041 if (brcmf_watchdog_prio > 0) {
1042 struct sched_param param;
1043 param.sched_priority = (brcmf_watchdog_prio < MAX_RT_PRIO) ?
1044 brcmf_watchdog_prio : (MAX_RT_PRIO - 1);
1045 sched_setscheduler(current, SCHED_FIFO, ¶m);
1048 allow_signal(SIGTERM);
1049 /* Run until signal received */
1051 if (kthread_should_stop())
1053 if (down_interruptible(&dhd->watchdog_sem) == 0) {
1054 if (dhd->pub.dongle_reset == false) {
1055 /* Call the bus module watchdog */
1056 brcmf_sdbrcm_bus_watchdog(&dhd->pub);
1058 /* Count the tick for reference */
1066 static void brcmf_watchdog(unsigned long data)
1068 dhd_info_t *dhd = (dhd_info_t *) data;
1070 if (dhd->watchdog_tsk) {
1071 up(&dhd->watchdog_sem);
1073 /* Reschedule the watchdog */
1074 if (dhd->wd_timer_valid) {
1075 mod_timer(&dhd->timer,
1076 jiffies + brcmf_watchdog_ms * HZ / 1000);
1081 /* Call the bus module watchdog */
1082 brcmf_sdbrcm_bus_watchdog(&dhd->pub);
1084 /* Count the tick for reference */
1087 /* Reschedule the watchdog */
1088 if (dhd->wd_timer_valid)
1089 mod_timer(&dhd->timer, jiffies + brcmf_watchdog_ms * HZ / 1000);
1092 static int brcmf_dpc_thread(void *data)
1094 dhd_info_t *dhd = (dhd_info_t *) data;
1096 /* This thread doesn't need any user-level access,
1097 * so get rid of all our resources
1099 if (brcmf_dpc_prio > 0) {
1100 struct sched_param param;
1101 param.sched_priority =
1103 MAX_RT_PRIO) ? brcmf_dpc_prio : (MAX_RT_PRIO - 1);
1104 sched_setscheduler(current, SCHED_FIFO, ¶m);
1107 allow_signal(SIGTERM);
1108 /* Run until signal received */
1110 if (kthread_should_stop())
1112 if (down_interruptible(&dhd->dpc_sem) == 0) {
1113 /* Call bus dpc unless it indicated down
1114 (then clean stop) */
1115 if (dhd->pub.busstate != DHD_BUS_DOWN) {
1116 if (dhd_bus_dpc(dhd->pub.bus)) {
1120 brcmf_sdbrcm_bus_stop(dhd->pub.bus, true);
1128 static void brcmf_dpc(unsigned long data)
1132 dhd = (dhd_info_t *) data;
1134 /* Call bus dpc unless it indicated down (then clean stop) */
1135 if (dhd->pub.busstate != DHD_BUS_DOWN) {
1136 if (dhd_bus_dpc(dhd->pub.bus))
1137 tasklet_schedule(&dhd->tasklet);
1139 brcmf_sdbrcm_bus_stop(dhd->pub.bus, true);
1143 void brcmf_sched_dpc(dhd_pub_t *dhdp)
1145 dhd_info_t *dhd = (dhd_info_t *) dhdp->info;
1152 tasklet_schedule(&dhd->tasklet);
1155 /* Retrieve current toe component enables, which are kept
1156 as a bitmap in toe_ol iovar */
1157 static int brcmf_toe_get(dhd_info_t *dhd, int ifidx, u32 *toe_ol)
1163 memset(&ioc, 0, sizeof(ioc));
1165 ioc.cmd = BRCMF_C_GET_VAR;
1167 ioc.len = (uint) sizeof(buf);
1170 strcpy(buf, "toe_ol");
1171 ret = brcmf_proto_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
1173 /* Check for older dongle image that doesn't support toe_ol */
1175 DHD_ERROR(("%s: toe not supported by device\n",
1176 brcmf_ifname(&dhd->pub, ifidx)));
1180 DHD_INFO(("%s: could not get toe_ol: ret=%d\n",
1181 brcmf_ifname(&dhd->pub, ifidx), ret));
1185 memcpy(toe_ol, buf, sizeof(u32));
1189 /* Set current toe component enables in toe_ol iovar,
1190 and set toe global enable iovar */
1191 static int brcmf_toe_set(dhd_info_t *dhd, int ifidx, u32 toe_ol)
1197 memset(&ioc, 0, sizeof(ioc));
1199 ioc.cmd = BRCMF_C_SET_VAR;
1201 ioc.len = (uint) sizeof(buf);
1204 /* Set toe_ol as requested */
1206 strcpy(buf, "toe_ol");
1207 memcpy(&buf[sizeof("toe_ol")], &toe_ol, sizeof(u32));
1209 ret = brcmf_proto_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
1211 DHD_ERROR(("%s: could not set toe_ol: ret=%d\n",
1212 brcmf_ifname(&dhd->pub, ifidx), ret));
1216 /* Enable toe globally only if any components are enabled. */
1218 toe = (toe_ol != 0);
1221 memcpy(&buf[sizeof("toe")], &toe, sizeof(u32));
1223 ret = brcmf_proto_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
1225 DHD_ERROR(("%s: could not set toe: ret=%d\n",
1226 brcmf_ifname(&dhd->pub, ifidx), ret));
1233 static void brcmf_ethtool_get_drvinfo(struct net_device *net,
1234 struct ethtool_drvinfo *info)
1236 dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1238 sprintf(info->driver, KBUILD_MODNAME);
1239 sprintf(info->version, "%lu", dhd->pub.drv_version);
1240 sprintf(info->fw_version, "%s", wl_cfg80211_get_fwname());
1241 sprintf(info->bus_info, "%s", dev_name(&wl_cfg80211_get_sdio_func()->dev));
1244 struct ethtool_ops brcmf_ethtool_ops = {
1245 .get_drvinfo = brcmf_ethtool_get_drvinfo
1248 static int brcmf_ethtool(dhd_info_t *dhd, void *uaddr)
1250 struct ethtool_drvinfo info;
1251 char drvname[sizeof(info.driver)];
1253 struct ethtool_value edata;
1254 u32 toe_cmpnt, csum_dir;
1257 DHD_TRACE(("%s: Enter\n", __func__));
1259 /* all ethtool calls start with a cmd word */
1260 if (copy_from_user(&cmd, uaddr, sizeof(u32)))
1264 case ETHTOOL_GDRVINFO:
1265 /* Copy out any request driver name */
1266 if (copy_from_user(&info, uaddr, sizeof(info)))
1268 strncpy(drvname, info.driver, sizeof(info.driver));
1269 drvname[sizeof(info.driver) - 1] = '\0';
1271 /* clear struct for return */
1272 memset(&info, 0, sizeof(info));
1275 /* if dhd requested, identify ourselves */
1276 if (strcmp(drvname, "?dhd") == 0) {
1277 sprintf(info.driver, "dhd");
1278 strcpy(info.version, BRCMF_VERSION_STR);
1281 /* otherwise, require dongle to be up */
1282 else if (!dhd->pub.up) {
1283 DHD_ERROR(("%s: dongle is not up\n", __func__));
1287 /* finally, report dongle driver type */
1288 else if (dhd->pub.iswl)
1289 sprintf(info.driver, "wl");
1291 sprintf(info.driver, "xx");
1293 sprintf(info.version, "%lu", dhd->pub.drv_version);
1294 if (copy_to_user(uaddr, &info, sizeof(info)))
1296 DHD_CTL(("%s: given %*s, returning %s\n", __func__,
1297 (int)sizeof(drvname), drvname, info.driver));
1300 /* Get toe offload components from dongle */
1301 case ETHTOOL_GRXCSUM:
1302 case ETHTOOL_GTXCSUM:
1303 ret = brcmf_toe_get(dhd, 0, &toe_cmpnt);
1308 (cmd == ETHTOOL_GTXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
1311 edata.data = (toe_cmpnt & csum_dir) ? 1 : 0;
1313 if (copy_to_user(uaddr, &edata, sizeof(edata)))
1317 /* Set toe offload components in dongle */
1318 case ETHTOOL_SRXCSUM:
1319 case ETHTOOL_STXCSUM:
1320 if (copy_from_user(&edata, uaddr, sizeof(edata)))
1323 /* Read the current settings, update and write back */
1324 ret = brcmf_toe_get(dhd, 0, &toe_cmpnt);
1329 (cmd == ETHTOOL_STXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
1331 if (edata.data != 0)
1332 toe_cmpnt |= csum_dir;
1334 toe_cmpnt &= ~csum_dir;
1336 ret = brcmf_toe_set(dhd, 0, toe_cmpnt);
1340 /* If setting TX checksum mode, tell Linux the new mode */
1341 if (cmd == ETHTOOL_STXCSUM) {
1343 dhd->iflist[0]->net->features |=
1346 dhd->iflist[0]->net->features &=
1359 static int brcmf_netdev_ioctl_entry(struct net_device *net, struct ifreq *ifr,
1362 dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1369 bool is_set_key_cmd;
1371 ifidx = brcmf_net2idx(dhd, net);
1372 DHD_TRACE(("%s: ifidx %d, cmd 0x%04x\n", __func__, ifidx, cmd));
1374 if (ifidx == DHD_BAD_IF)
1377 if (cmd == SIOCETHTOOL)
1378 return brcmf_ethtool(dhd, (void *)ifr->ifr_data);
1380 if (cmd != SIOCDEVPRIVATE)
1383 memset(&ioc, 0, sizeof(ioc));
1385 /* Copy the ioc control structure part of ioctl request */
1386 if (copy_from_user(&ioc, ifr->ifr_data, sizeof(wl_ioctl_t))) {
1391 /* Copy out any buffer passed */
1393 buflen = min_t(int, ioc.len, DHD_IOCTL_MAXLEN);
1394 /* optimization for direct ioctl calls from kernel */
1396 if (segment_eq(get_fs(), KERNEL_DS)) {
1401 buf = kmalloc(buflen, GFP_ATOMIC);
1406 if (copy_from_user(buf, ioc.buf, buflen)) {
1413 /* To differentiate between wl and dhd read 4 more byes */
1414 if ((copy_from_user(&driver, (char *)ifr->ifr_data + sizeof(wl_ioctl_t),
1415 sizeof(uint)) != 0)) {
1420 if (!capable(CAP_NET_ADMIN)) {
1425 /* check for local dhd ioctl and handle it */
1426 if (driver == DHD_IOCTL_MAGIC) {
1427 bcmerror = brcmf_c_ioctl((void *)&dhd->pub, &ioc, buf, buflen);
1429 dhd->pub.bcmerror = bcmerror;
1433 /* send to dongle (must be up, and wl) */
1434 if ((dhd->pub.busstate != DHD_BUS_DATA)) {
1435 DHD_ERROR(("%s DONGLE_DOWN,__func__\n", __func__));
1440 if (!dhd->pub.iswl) {
1446 * Intercept BRCMF_C_SET_KEY IOCTL - serialize M4 send and
1447 * set key IOCTL to prevent M4 encryption.
1449 is_set_key_cmd = ((ioc.cmd == BRCMF_C_SET_KEY) ||
1450 ((ioc.cmd == BRCMF_C_SET_VAR) &&
1451 !(strncmp("wsec_key", ioc.buf, 9))) ||
1452 ((ioc.cmd == BRCMF_C_SET_VAR) &&
1453 !(strncmp("bsscfg:wsec_key", ioc.buf, 15))));
1455 brcmf_netdev_wait_pend8021x(net);
1458 brcmf_proto_ioctl(&dhd->pub, ifidx, (wl_ioctl_t *)&ioc, buf,
1462 if (!bcmerror && buf && ioc.buf) {
1463 if (copy_to_user(ioc.buf, buf, buflen))
1475 static int brcmf_netdev_stop(struct net_device *net)
1477 #if !defined(IGNORE_ETH0_DOWN)
1478 dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1480 DHD_TRACE(("%s: Enter\n", __func__));
1482 if (dhd->pub.up == 0)
1485 /* Set state and stop OS transmissions */
1487 netif_stop_queue(net);
1489 DHD_ERROR(("BYPASS %s:due to BRCM compilation : under investigation\n",
1491 #endif /* !defined(IGNORE_ETH0_DOWN) */
1496 static int brcmf_netdev_open(struct net_device *net)
1498 dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1500 int ifidx = brcmf_net2idx(dhd, net);
1503 DHD_TRACE(("%s: ifidx %d\n", __func__, ifidx));
1505 if (ifidx == 0) { /* do it only for primary eth0 */
1507 /* try to bring up bus */
1508 ret = brcmf_bus_start(&dhd->pub);
1510 DHD_ERROR(("%s: failed with code %d\n", __func__, ret));
1513 atomic_set(&dhd->pend_8021x_cnt, 0);
1515 memcpy(net->dev_addr, dhd->pub.mac, ETH_ALEN);
1517 /* Get current TOE mode from dongle */
1518 if (brcmf_toe_get(dhd, ifidx, &toe_ol) >= 0
1519 && (toe_ol & TOE_TX_CSUM_OL) != 0)
1520 dhd->iflist[ifidx]->net->features |= NETIF_F_IP_CSUM;
1522 dhd->iflist[ifidx]->net->features &= ~NETIF_F_IP_CSUM;
1524 /* Allow transmit calls */
1525 netif_start_queue(net);
1527 if (unlikely(wl_cfg80211_up())) {
1528 DHD_ERROR(("%s: failed to bring up cfg80211\n",
1537 brcmf_add_if(dhd_info_t *dhd, int ifidx, void *handle, char *name,
1538 u8 *mac_addr, u32 flags, u8 bssidx)
1542 DHD_TRACE(("%s: idx %d, handle->%p\n", __func__, ifidx, handle));
1544 ASSERT(dhd && (ifidx < DHD_MAX_IFS));
1546 ifp = dhd->iflist[ifidx];
1548 ifp = kmalloc(sizeof(dhd_if_t), GFP_ATOMIC);
1550 DHD_ERROR(("%s: OOM - dhd_if_t\n", __func__));
1555 memset(ifp, 0, sizeof(dhd_if_t));
1557 dhd->iflist[ifidx] = ifp;
1558 strlcpy(ifp->name, name, IFNAMSIZ);
1559 if (mac_addr != NULL)
1560 memcpy(&ifp->mac_addr, mac_addr, ETH_ALEN);
1562 if (handle == NULL) {
1563 ifp->state = BRCMF_E_IF_ADD;
1565 ASSERT(dhd->sysioc_tsk);
1566 up(&dhd->sysioc_sem);
1568 ifp->net = (struct net_device *)handle;
1573 void brcmf_del_if(dhd_info_t *dhd, int ifidx)
1577 DHD_TRACE(("%s: idx %d\n", __func__, ifidx));
1579 ASSERT(dhd && ifidx && (ifidx < DHD_MAX_IFS));
1580 ifp = dhd->iflist[ifidx];
1582 DHD_ERROR(("%s: Null interface\n", __func__));
1586 ifp->state = BRCMF_E_IF_DEL;
1588 ASSERT(dhd->sysioc_tsk);
1589 up(&dhd->sysioc_sem);
1592 dhd_pub_t *brcmf_attach(struct dhd_bus *bus, uint bus_hdrlen)
1594 dhd_info_t *dhd = NULL;
1595 struct net_device *net;
1597 DHD_TRACE(("%s: Enter\n", __func__));
1598 /* updates firmware nvram path if it was provided as module
1600 if ((firmware_path != NULL) && (firmware_path[0] != '\0'))
1601 strcpy(brcmf_fw_path, firmware_path);
1602 if ((nvram_path != NULL) && (nvram_path[0] != '\0'))
1603 strcpy(brcmf_nv_path, nvram_path);
1605 /* Allocate etherdev, including space for private structure */
1606 net = alloc_etherdev(sizeof(dhd));
1608 DHD_ERROR(("%s: OOM - alloc_etherdev\n", __func__));
1612 /* Allocate primary dhd_info */
1613 dhd = kzalloc(sizeof(dhd_info_t), GFP_ATOMIC);
1615 DHD_ERROR(("%s: OOM - alloc dhd_info\n", __func__));
1620 * Save the dhd_info into the priv
1622 memcpy(netdev_priv(net), &dhd, sizeof(dhd));
1624 /* Set network interface name if it was provided as module parameter */
1625 if (iface_name[0]) {
1628 strncpy(net->name, iface_name, IFNAMSIZ);
1629 net->name[IFNAMSIZ - 1] = 0;
1630 len = strlen(net->name);
1631 ch = net->name[len - 1];
1632 if ((ch > '9' || ch < '0') && (len < IFNAMSIZ - 2))
1633 strcat(net->name, "%d");
1636 if (brcmf_add_if(dhd, 0, (void *)net, net->name, NULL, 0, 0) ==
1640 net->netdev_ops = NULL;
1641 sema_init(&dhd->proto_sem, 1);
1642 /* Initialize other structure content */
1643 init_waitqueue_head(&dhd->ioctl_resp_wait);
1644 init_waitqueue_head(&dhd->ctrl_wait);
1646 /* Initialize the spinlocks */
1647 spin_lock_init(&dhd->sdlock);
1649 /* Link to info module */
1650 dhd->pub.info = dhd;
1652 /* Link to bus module */
1654 dhd->pub.hdrlen = bus_hdrlen;
1656 /* Attach and link in the protocol */
1657 if (brcmf_proto_attach(&dhd->pub) != 0) {
1658 DHD_ERROR(("dhd_prot_attach failed\n"));
1662 /* Attach and link in the cfg80211 */
1663 if (unlikely(wl_cfg80211_attach(net, &dhd->pub))) {
1664 DHD_ERROR(("wl_cfg80211_attach failed\n"));
1667 if (!brcmf_no_fw_req) {
1668 strcpy(brcmf_fw_path, wl_cfg80211_get_fwname());
1669 strcpy(brcmf_nv_path, wl_cfg80211_get_nvramname());
1672 /* Set up the watchdog timer */
1673 init_timer(&dhd->timer);
1674 dhd->timer.data = (unsigned long) dhd;
1675 dhd->timer.function = brcmf_watchdog;
1677 /* Initialize thread based operation and lock */
1678 sema_init(&dhd->sdsem, 1);
1679 if ((brcmf_watchdog_prio >= 0) && (brcmf_dpc_prio >= 0))
1680 dhd->threads_only = true;
1682 dhd->threads_only = false;
1684 if (brcmf_dpc_prio >= 0) {
1685 /* Initialize watchdog thread */
1686 sema_init(&dhd->watchdog_sem, 0);
1687 dhd->watchdog_tsk = kthread_run(brcmf_watchdog_thread, dhd,
1689 if (IS_ERR(dhd->watchdog_tsk)) {
1691 "dhd_watchdog thread failed to start\n");
1692 dhd->watchdog_tsk = NULL;
1695 dhd->watchdog_tsk = NULL;
1698 /* Set up the bottom half handler */
1699 if (brcmf_dpc_prio >= 0) {
1700 /* Initialize DPC thread */
1701 sema_init(&dhd->dpc_sem, 0);
1702 dhd->dpc_tsk = kthread_run(brcmf_dpc_thread, dhd, "dhd_dpc");
1703 if (IS_ERR(dhd->dpc_tsk)) {
1705 "dhd_dpc thread failed to start\n");
1706 dhd->dpc_tsk = NULL;
1709 tasklet_init(&dhd->tasklet, brcmf_dpc, (unsigned long) dhd);
1710 dhd->dpc_tsk = NULL;
1714 sema_init(&dhd->sysioc_sem, 0);
1715 dhd->sysioc_tsk = kthread_run(_brcmf_sysioc_thread, dhd,
1717 if (IS_ERR(dhd->sysioc_tsk)) {
1719 "_dhd_sysioc thread failed to start\n");
1720 dhd->sysioc_tsk = NULL;
1723 dhd->sysioc_tsk = NULL;
1726 * Save the dhd_info into the priv
1728 memcpy(netdev_priv(net), &dhd, sizeof(dhd));
1730 #if defined(CUSTOMER_HW2) && defined(CONFIG_WIFI_CONTROL_FUNC)
1733 #if defined(CONFIG_PM_SLEEP)
1734 atomic_set(&brcmf_mmc_suspend, false);
1735 #endif /* defined(CONFIG_PM_SLEEP) */
1736 /* && defined(DHD_GPL) */
1737 /* Init lock suspend to prevent kernel going to suspend */
1738 #ifdef CONFIG_HAS_EARLYSUSPEND
1739 dhd->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 20;
1740 dhd->early_suspend.suspend = brcmf_early_suspend;
1741 dhd->early_suspend.resume = brcmf_late_resume;
1742 register_early_suspend(&dhd->early_suspend);
1751 brcmf_detach(&dhd->pub);
1756 int brcmf_bus_start(dhd_pub_t *dhdp)
1759 dhd_info_t *dhd = (dhd_info_t *) dhdp->info;
1760 char iovbuf[WL_EVENTING_MASK_LEN + 12]; /* Room for "event_msgs" +
1765 DHD_TRACE(("%s:\n", __func__));
1767 /* try to download image and nvram to the dongle */
1768 if (dhd->pub.busstate == DHD_BUS_DOWN) {
1769 if (!(dhd_bus_download_firmware(dhd->pub.bus, brcmf_fw_path,
1771 DHD_ERROR(("%s: dhd_bus_download_firmware failed. "
1772 "firmware = %s nvram = %s\n",
1773 __func__, brcmf_fw_path, brcmf_nv_path));
1778 /* Start the watchdog timer */
1779 dhd->pub.tickcnt = 0;
1780 brcmf_os_wd_timer(&dhd->pub, brcmf_watchdog_ms);
1782 /* Bring up the bus */
1783 ret = brcmf_sdbrcm_bus_init(&dhd->pub, true);
1785 DHD_ERROR(("%s, brcmf_sdbrcm_bus_init failed %d\n", __func__,
1790 /* If bus is not ready, can't come up */
1791 if (dhd->pub.busstate != DHD_BUS_DATA) {
1792 del_timer_sync(&dhd->timer);
1793 dhd->wd_timer_valid = false;
1794 DHD_ERROR(("%s failed bus is not ready\n", __func__));
1798 brcmu_mkiovar("event_msgs", dhdp->eventmask, WL_EVENTING_MASK_LEN,
1799 iovbuf, sizeof(iovbuf));
1800 brcmf_proto_cdc_query_ioctl(dhdp, 0, BRCMF_C_GET_VAR, iovbuf,
1802 memcpy(dhdp->eventmask, iovbuf, WL_EVENTING_MASK_LEN);
1804 setbit(dhdp->eventmask, BRCMF_E_SET_SSID);
1805 setbit(dhdp->eventmask, BRCMF_E_PRUNE);
1806 setbit(dhdp->eventmask, BRCMF_E_AUTH);
1807 setbit(dhdp->eventmask, BRCMF_E_REASSOC);
1808 setbit(dhdp->eventmask, BRCMF_E_REASSOC_IND);
1809 setbit(dhdp->eventmask, BRCMF_E_DEAUTH_IND);
1810 setbit(dhdp->eventmask, BRCMF_E_DISASSOC_IND);
1811 setbit(dhdp->eventmask, BRCMF_E_DISASSOC);
1812 setbit(dhdp->eventmask, BRCMF_E_JOIN);
1813 setbit(dhdp->eventmask, BRCMF_E_ASSOC_IND);
1814 setbit(dhdp->eventmask, BRCMF_E_PSK_SUP);
1815 setbit(dhdp->eventmask, BRCMF_E_LINK);
1816 setbit(dhdp->eventmask, BRCMF_E_NDIS_LINK);
1817 setbit(dhdp->eventmask, BRCMF_E_MIC_ERROR);
1818 setbit(dhdp->eventmask, BRCMF_E_PMKID_CACHE);
1819 setbit(dhdp->eventmask, BRCMF_E_TXFAIL);
1820 setbit(dhdp->eventmask, BRCMF_E_JOIN_START);
1821 setbit(dhdp->eventmask, BRCMF_E_SCAN_COMPLETE);
1823 /* enable dongle roaming event */
1825 dhdp->pktfilter_count = 1;
1826 /* Setup filter to allow only unicast */
1827 dhdp->pktfilter[0] = "100 0 0 0 0x01 0x00";
1829 /* Bus is ready, do any protocol initialization */
1830 ret = brcmf_proto_init(&dhd->pub);
1837 int brcmf_iovar(dhd_pub_t *pub, int ifidx, char *name, char *cmd_buf,
1838 uint cmd_len, int set)
1840 char buf[strlen(name) + 1 + cmd_len];
1841 int len = sizeof(buf);
1845 len = brcmu_mkiovar(name, cmd_buf, cmd_len, buf, len);
1847 memset(&ioc, 0, sizeof(ioc));
1849 ioc.cmd = set ? BRCMF_C_SET_VAR : BRCMF_C_GET_VAR;
1854 ret = brcmf_proto_ioctl(pub, ifidx, &ioc, ioc.buf, ioc.len);
1855 if (!set && ret >= 0)
1856 memcpy(cmd_buf, buf, cmd_len);
1861 static struct net_device_ops brcmf_netdev_ops_pri = {
1862 .ndo_open = brcmf_netdev_open,
1863 .ndo_stop = brcmf_netdev_stop,
1864 .ndo_get_stats = brcmf_netdev_get_stats,
1865 .ndo_do_ioctl = brcmf_netdev_ioctl_entry,
1866 .ndo_start_xmit = brcmf_netdev_start_xmit,
1867 .ndo_set_mac_address = brcmf_netdev_set_mac_address,
1868 .ndo_set_multicast_list = brcmf_netdev_set_multicast_list
1871 int brcmf_net_attach(dhd_pub_t *dhdp, int ifidx)
1873 dhd_info_t *dhd = (dhd_info_t *) dhdp->info;
1874 struct net_device *net;
1875 u8 temp_addr[ETH_ALEN] = {
1876 0x00, 0x90, 0x4c, 0x11, 0x22, 0x33};
1878 DHD_TRACE(("%s: ifidx %d\n", __func__, ifidx));
1880 ASSERT(dhd && dhd->iflist[ifidx]);
1882 net = dhd->iflist[ifidx]->net;
1885 ASSERT(!net->netdev_ops);
1886 net->netdev_ops = &brcmf_netdev_ops_pri;
1889 * We have to use the primary MAC for virtual interfaces
1892 /* for virtual interfaces use the primary MAC */
1893 memcpy(temp_addr, dhd->pub.mac, ETH_ALEN);
1898 DHD_TRACE(("%s ACCESS POINT MAC:\n", __func__));
1899 /* ACCESSPOINT INTERFACE CASE */
1900 temp_addr[0] |= 0X02; /* set bit 2 ,
1901 - Locally Administered address */
1904 net->hard_header_len = ETH_HLEN + dhd->pub.hdrlen;
1905 net->ethtool_ops = &brcmf_ethtool_ops;
1907 dhd->pub.rxsz = net->mtu + net->hard_header_len + dhd->pub.hdrlen;
1909 memcpy(net->dev_addr, temp_addr, ETH_ALEN);
1911 if (register_netdev(net) != 0) {
1912 DHD_ERROR(("%s: couldn't register the net device\n",
1917 DHD_INFO(("%s: Broadcom Dongle Host Driver\n", net->name));
1922 net->netdev_ops = NULL;
1926 static void brcmf_bus_detach(dhd_pub_t *dhdp)
1930 DHD_TRACE(("%s: Enter\n", __func__));
1933 dhd = (dhd_info_t *) dhdp->info;
1935 /* Stop the protocol module */
1936 brcmf_proto_stop(&dhd->pub);
1938 /* Stop the bus module */
1939 brcmf_sdbrcm_bus_stop(dhd->pub.bus, true);
1941 /* Clear the watchdog timer */
1942 del_timer_sync(&dhd->timer);
1943 dhd->wd_timer_valid = false;
1948 void brcmf_detach(dhd_pub_t *dhdp)
1952 DHD_TRACE(("%s: Enter\n", __func__));
1955 dhd = (dhd_info_t *) dhdp->info;
1960 #if defined(CONFIG_HAS_EARLYSUSPEND)
1961 if (dhd->early_suspend.suspend)
1962 unregister_early_suspend(&dhd->early_suspend);
1963 #endif /* defined(CONFIG_HAS_EARLYSUSPEND) */
1965 for (i = 1; i < DHD_MAX_IFS; i++)
1967 brcmf_del_if(dhd, i);
1969 ifp = dhd->iflist[0];
1971 if (ifp->net->netdev_ops == &brcmf_netdev_ops_pri) {
1972 brcmf_netdev_stop(ifp->net);
1973 unregister_netdev(ifp->net);
1976 if (dhd->watchdog_tsk) {
1977 send_sig(SIGTERM, dhd->watchdog_tsk, 1);
1978 kthread_stop(dhd->watchdog_tsk);
1979 dhd->watchdog_tsk = NULL;
1983 send_sig(SIGTERM, dhd->dpc_tsk, 1);
1984 kthread_stop(dhd->dpc_tsk);
1985 dhd->dpc_tsk = NULL;
1987 tasklet_kill(&dhd->tasklet);
1989 if (dhd->sysioc_tsk) {
1990 send_sig(SIGTERM, dhd->sysioc_tsk, 1);
1991 kthread_stop(dhd->sysioc_tsk);
1992 dhd->sysioc_tsk = NULL;
1995 brcmf_bus_detach(dhdp);
1998 brcmf_proto_detach(dhdp);
2000 wl_cfg80211_detach();
2002 /* && defined(DHD_GPL) */
2003 free_netdev(ifp->net);
2010 static void __exit brcmf_module_cleanup(void)
2012 DHD_TRACE(("%s: Enter\n", __func__));
2014 dhd_bus_unregister();
2017 static int __init brcmf_module_init(void)
2021 DHD_TRACE(("%s: Enter\n", __func__));
2023 /* Sanity check on the module parameters */
2025 /* Both watchdog and DPC as tasklets are ok */
2026 if ((brcmf_watchdog_prio < 0) && (brcmf_dpc_prio < 0))
2029 /* If both watchdog and DPC are threads, TX must be deferred */
2030 if ((brcmf_watchdog_prio >= 0) && (brcmf_dpc_prio >= 0)
2031 && brcmf_deferred_tx)
2034 DHD_ERROR(("Invalid module parameters.\n"));
2038 error = dhd_bus_register();
2041 DHD_ERROR(("%s: dhd_bus_register failed\n", __func__));
2050 module_init(brcmf_module_init);
2051 module_exit(brcmf_module_cleanup);
2054 * OS specific functions required to implement DHD driver in OS independent way
2056 int brcmf_os_proto_block(dhd_pub_t *pub)
2058 dhd_info_t *dhd = (dhd_info_t *) (pub->info);
2061 down(&dhd->proto_sem);
2067 int brcmf_os_proto_unblock(dhd_pub_t *pub)
2069 dhd_info_t *dhd = (dhd_info_t *) (pub->info);
2072 up(&dhd->proto_sem);
2079 unsigned int brcmf_os_get_ioctl_resp_timeout(void)
2081 return (unsigned int)brcmf_ioctl_timeout_msec;
2084 void brcmf_os_set_ioctl_resp_timeout(unsigned int timeout_msec)
2086 brcmf_ioctl_timeout_msec = (int)timeout_msec;
2089 int brcmf_os_ioctl_resp_wait(dhd_pub_t *pub, uint *condition, bool *pending)
2091 dhd_info_t *dhd = (dhd_info_t *) (pub->info);
2092 DECLARE_WAITQUEUE(wait, current);
2093 int timeout = brcmf_ioctl_timeout_msec;
2095 /* Convert timeout in millsecond to jiffies */
2096 timeout = timeout * HZ / 1000;
2098 /* Wait until control frame is available */
2099 add_wait_queue(&dhd->ioctl_resp_wait, &wait);
2100 set_current_state(TASK_INTERRUPTIBLE);
2102 while (!(*condition) && (!signal_pending(current) && timeout))
2103 timeout = schedule_timeout(timeout);
2105 if (signal_pending(current))
2108 set_current_state(TASK_RUNNING);
2109 remove_wait_queue(&dhd->ioctl_resp_wait, &wait);
2114 int brcmf_os_ioctl_resp_wake(dhd_pub_t *pub)
2116 dhd_info_t *dhd = (dhd_info_t *) (pub->info);
2118 if (waitqueue_active(&dhd->ioctl_resp_wait))
2119 wake_up_interruptible(&dhd->ioctl_resp_wait);
2124 void brcmf_os_wd_timer(void *bus, uint wdtick)
2126 dhd_pub_t *pub = bus;
2127 static uint save_dhd_watchdog_ms;
2128 dhd_info_t *dhd = (dhd_info_t *) pub->info;
2130 /* don't start the wd until fw is loaded */
2131 if (pub->busstate == DHD_BUS_DOWN)
2134 /* Totally stop the timer */
2135 if (!wdtick && dhd->wd_timer_valid == true) {
2136 del_timer_sync(&dhd->timer);
2137 dhd->wd_timer_valid = false;
2138 save_dhd_watchdog_ms = wdtick;
2143 brcmf_watchdog_ms = (uint) wdtick;
2145 if (save_dhd_watchdog_ms != brcmf_watchdog_ms) {
2147 if (dhd->wd_timer_valid == true)
2148 /* Stop timer and restart at new value */
2149 del_timer_sync(&dhd->timer);
2151 /* Create timer again when watchdog period is
2152 dynamically changed or in the first instance
2154 dhd->timer.expires =
2155 jiffies + brcmf_watchdog_ms * HZ / 1000;
2156 add_timer(&dhd->timer);
2159 /* Re arm the timer, at last watchdog period */
2160 mod_timer(&dhd->timer,
2161 jiffies + brcmf_watchdog_ms * HZ / 1000);
2164 dhd->wd_timer_valid = true;
2165 save_dhd_watchdog_ms = wdtick;
2169 void *brcmf_os_open_image(char *filename)
2173 if (!brcmf_no_fw_req)
2174 return wl_cfg80211_request_fw(filename);
2176 fp = filp_open(filename, O_RDONLY, 0);
2178 * 2.6.11 (FC4) supports filp_open() but later revs don't?
2180 * fp = open_namei(AT_FDCWD, filename, O_RD, 0);
2189 int brcmf_os_get_image_block(char *buf, int len, void *image)
2191 struct file *fp = (struct file *)image;
2194 if (!brcmf_no_fw_req)
2195 return wl_cfg80211_read_fw(buf, len);
2200 rdlen = kernel_read(fp, fp->f_pos, buf, len);
2207 void brcmf_os_close_image(void *image)
2209 if (!brcmf_no_fw_req)
2210 return wl_cfg80211_release_fw();
2212 filp_close((struct file *)image, NULL);
2215 void brcmf_os_sdlock(dhd_pub_t *pub)
2219 dhd = (dhd_info_t *) (pub->info);
2221 if (dhd->threads_only)
2224 spin_lock_bh(&dhd->sdlock);
2227 void brcmf_os_sdunlock(dhd_pub_t *pub)
2231 dhd = (dhd_info_t *) (pub->info);
2233 if (dhd->threads_only)
2236 spin_unlock_bh(&dhd->sdlock);
2239 static int brcmf_host_event(dhd_info_t *dhd, int *ifidx, void *pktdata,
2240 brcmf_event_msg_t *event, void **data)
2244 ASSERT(dhd != NULL);
2246 bcmerror = brcmf_c_host_event(dhd, ifidx, pktdata, event, data);
2250 ASSERT(dhd->iflist[*ifidx] != NULL);
2251 ASSERT(dhd->iflist[*ifidx]->net != NULL);
2252 if (dhd->iflist[*ifidx]->net)
2253 wl_cfg80211_event(dhd->iflist[*ifidx]->net, event, *data);
2258 void brcmf_wait_for_event(dhd_pub_t *dhd, bool *lockvar)
2260 struct dhd_info *dhdinfo = dhd->info;
2261 brcmf_os_sdunlock(dhd);
2262 wait_event_interruptible_timeout(dhdinfo->ctrl_wait,
2263 (*lockvar == false), HZ * 2);
2264 brcmf_os_sdlock(dhd);
2268 void brcmf_wait_event_wakeup(dhd_pub_t *dhd)
2270 struct dhd_info *dhdinfo = dhd->info;
2271 if (waitqueue_active(&dhdinfo->ctrl_wait))
2272 wake_up_interruptible(&dhdinfo->ctrl_wait);
2276 int brcmf_netdev_reset(struct net_device *dev, u8 flag)
2278 dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2280 /* Turning off watchdog */
2282 brcmf_os_wd_timer(&dhd->pub, 0);
2284 brcmf_bus_devreset(&dhd->pub, flag);
2286 /* Turning on watchdog back */
2288 brcmf_os_wd_timer(&dhd->pub, brcmf_watchdog_ms);
2289 DHD_ERROR(("%s: WLAN OFF DONE\n", __func__));
2294 int brcmf_netdev_set_suspend_disable(struct net_device *dev, int val)
2296 dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2300 ret = dhd->pub.suspend_disable_flag;
2301 dhd->pub.suspend_disable_flag = val;
2306 int brcmf_netdev_set_suspend(struct net_device *dev, int val)
2309 #if defined(CONFIG_HAS_EARLYSUSPEND)
2310 dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2313 brcmf_os_proto_block(&dhd->pub);
2314 ret = brcmf_set_suspend(val, &dhd->pub);
2315 brcmf_os_proto_unblock(&dhd->pub);
2317 #endif /* defined(CONFIG_HAS_EARLYSUSPEND) */
2321 int brcmf_netdev_set_dtim_skip(struct net_device *dev, int val)
2323 dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(dev);
2326 dhd->pub.dtim_skip = val;
2331 int brcmf_netdev_set_packet_filter(struct net_device *dev, int val)
2333 dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(dev);
2336 /* Packet filtering is set only if we still in early-suspend and
2337 * we need either to turn it ON or turn it OFF
2338 * We can always turn it OFF in case of early-suspend, but we turn it
2339 * back ON only if suspend_disable_flag was not set
2341 if (dhd && dhd->pub.up) {
2342 brcmf_os_proto_block(&dhd->pub);
2343 if (dhd->pub.in_suspend) {
2344 if (!val || (val && !dhd->pub.suspend_disable_flag))
2345 brcmf_set_packet_filter(val, &dhd->pub);
2347 brcmf_os_proto_unblock(&dhd->pub);
2352 void brcmf_netdev_init_ioctl(struct net_device *dev)
2354 dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2356 brcmf_c_preinit_ioctls(&dhd->pub);
2359 static int brcmf_get_pend_8021x_cnt(dhd_info_t *dhd)
2361 return atomic_read(&dhd->pend_8021x_cnt);
2364 #define MAX_WAIT_FOR_8021X_TX 10
2366 int brcmf_netdev_wait_pend8021x(struct net_device *dev)
2368 dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2369 int timeout = 10 * HZ / 1000;
2370 int ntimes = MAX_WAIT_FOR_8021X_TX;
2371 int pend = brcmf_get_pend_8021x_cnt(dhd);
2373 while (ntimes && pend) {
2375 set_current_state(TASK_INTERRUPTIBLE);
2376 schedule_timeout(timeout);
2377 set_current_state(TASK_RUNNING);
2380 pend = brcmf_get_pend_8021x_cnt(dhd);
2386 int brcmf_write_to_file(dhd_pub_t *dhd, u8 *buf, int size)
2390 mm_segment_t old_fs;
2393 /* change to KERNEL_DS address limit */
2397 /* open file to write */
2398 fp = filp_open("/tmp/mem_dump", O_WRONLY | O_CREAT, 0640);
2400 DHD_ERROR(("%s: open file error\n", __func__));
2405 /* Write buf to file */
2406 fp->f_op->write(fp, buf, size, &pos);
2409 /* free buf before return */
2411 /* close file before return */
2413 filp_close(fp, current->files);
2414 /* restore previous address limit */
2422 void osl_assert(char *exp, char *file, int line)
2427 basename = strrchr(file, '/');
2435 snprintf(tempbuf, 256,
2436 "assertion \"%s\" failed: file \"%s\", line %d\n", exp,
2440 * Print assert message and give it time to
2441 * be written to /var/log/messages
2443 if (!in_interrupt()) {
2444 const int delay = 3;
2445 printk(KERN_ERR "%s", tempbuf);
2446 printk(KERN_ERR "panic in %d seconds\n", delay);
2447 set_current_state(TASK_INTERRUPTIBLE);
2448 schedule_timeout(delay * HZ);
2451 switch (g_assert_type) {
2453 panic(KERN_ERR "%s", tempbuf);
2456 printk(KERN_ERR "%s", tempbuf);
2460 printk(KERN_ERR "%s", tempbuf);
2466 #endif /* defined(BCMDBG) */