dhd_linux.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. /*
  2. * Copyright (c) 2010 Broadcom Corporation
  3. *
  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.
  7. *
  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.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/kernel.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/module.h>
  20. #include <net/cfg80211.h>
  21. #include <net/rtnetlink.h>
  22. #include <brcmu_utils.h>
  23. #include <brcmu_wifi.h>
  24. #include "dhd.h"
  25. #include "dhd_bus.h"
  26. #include "dhd_proto.h"
  27. #include "dhd_dbg.h"
  28. #include "wl_cfg80211.h"
  29. #include "fwil.h"
  30. MODULE_AUTHOR("Broadcom Corporation");
  31. MODULE_DESCRIPTION("Broadcom 802.11 wireless LAN fullmac driver.");
  32. MODULE_SUPPORTED_DEVICE("Broadcom 802.11 WLAN fullmac cards");
  33. MODULE_LICENSE("Dual BSD/GPL");
  34. #define MAX_WAIT_FOR_8021X_TX 50 /* msecs */
  35. /* Error bits */
  36. int brcmf_msg_level;
  37. module_param(brcmf_msg_level, int, 0);
  38. char *brcmf_ifname(struct brcmf_pub *drvr, int ifidx)
  39. {
  40. if (ifidx < 0 || ifidx >= BRCMF_MAX_IFS) {
  41. brcmf_err("ifidx %d out of range\n", ifidx);
  42. return "<if_bad>";
  43. }
  44. if (drvr->iflist[ifidx] == NULL) {
  45. brcmf_err("null i/f %d\n", ifidx);
  46. return "<if_null>";
  47. }
  48. if (drvr->iflist[ifidx]->ndev)
  49. return drvr->iflist[ifidx]->ndev->name;
  50. return "<if_none>";
  51. }
  52. static void _brcmf_set_multicast_list(struct work_struct *work)
  53. {
  54. struct brcmf_if *ifp;
  55. struct net_device *ndev;
  56. struct netdev_hw_addr *ha;
  57. u32 cmd_value, cnt;
  58. __le32 cnt_le;
  59. char *buf, *bufp;
  60. u32 buflen;
  61. s32 err;
  62. brcmf_dbg(TRACE, "enter\n");
  63. ifp = container_of(work, struct brcmf_if, multicast_work);
  64. ndev = ifp->ndev;
  65. /* Determine initial value of allmulti flag */
  66. cmd_value = (ndev->flags & IFF_ALLMULTI) ? true : false;
  67. /* Send down the multicast list first. */
  68. cnt = netdev_mc_count(ndev);
  69. buflen = sizeof(cnt) + (cnt * ETH_ALEN);
  70. buf = kmalloc(buflen, GFP_ATOMIC);
  71. if (!buf)
  72. return;
  73. bufp = buf;
  74. cnt_le = cpu_to_le32(cnt);
  75. memcpy(bufp, &cnt_le, sizeof(cnt_le));
  76. bufp += sizeof(cnt_le);
  77. netdev_for_each_mc_addr(ha, ndev) {
  78. if (!cnt)
  79. break;
  80. memcpy(bufp, ha->addr, ETH_ALEN);
  81. bufp += ETH_ALEN;
  82. cnt--;
  83. }
  84. err = brcmf_fil_iovar_data_set(ifp, "mcast_list", buf, buflen);
  85. if (err < 0) {
  86. brcmf_err("Setting mcast_list failed, %d\n", err);
  87. cmd_value = cnt ? true : cmd_value;
  88. }
  89. kfree(buf);
  90. /*
  91. * Now send the allmulti setting. This is based on the setting in the
  92. * net_device flags, but might be modified above to be turned on if we
  93. * were trying to set some addresses and dongle rejected it...
  94. */
  95. err = brcmf_fil_iovar_int_set(ifp, "allmulti", cmd_value);
  96. if (err < 0)
  97. brcmf_err("Setting allmulti failed, %d\n", err);
  98. /*Finally, pick up the PROMISC flag */
  99. cmd_value = (ndev->flags & IFF_PROMISC) ? true : false;
  100. err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_PROMISC, cmd_value);
  101. if (err < 0)
  102. brcmf_err("Setting BRCMF_C_SET_PROMISC failed, %d\n",
  103. err);
  104. }
  105. static void
  106. _brcmf_set_mac_address(struct work_struct *work)
  107. {
  108. struct brcmf_if *ifp;
  109. s32 err;
  110. brcmf_dbg(TRACE, "enter\n");
  111. ifp = container_of(work, struct brcmf_if, setmacaddr_work);
  112. err = brcmf_fil_iovar_data_set(ifp, "cur_etheraddr", ifp->mac_addr,
  113. ETH_ALEN);
  114. if (err < 0) {
  115. brcmf_err("Setting cur_etheraddr failed, %d\n", err);
  116. } else {
  117. brcmf_dbg(TRACE, "MAC address updated to %pM\n",
  118. ifp->mac_addr);
  119. memcpy(ifp->ndev->dev_addr, ifp->mac_addr, ETH_ALEN);
  120. }
  121. }
  122. static int brcmf_netdev_set_mac_address(struct net_device *ndev, void *addr)
  123. {
  124. struct brcmf_if *ifp = netdev_priv(ndev);
  125. struct sockaddr *sa = (struct sockaddr *)addr;
  126. memcpy(&ifp->mac_addr, sa->sa_data, ETH_ALEN);
  127. schedule_work(&ifp->setmacaddr_work);
  128. return 0;
  129. }
  130. static void brcmf_netdev_set_multicast_list(struct net_device *ndev)
  131. {
  132. struct brcmf_if *ifp = netdev_priv(ndev);
  133. schedule_work(&ifp->multicast_work);
  134. }
  135. static int brcmf_netdev_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  136. {
  137. int ret;
  138. struct brcmf_if *ifp = netdev_priv(ndev);
  139. struct brcmf_pub *drvr = ifp->drvr;
  140. brcmf_dbg(TRACE, "Enter\n");
  141. /* Reject if down */
  142. if (!drvr->bus_if->drvr_up ||
  143. (drvr->bus_if->state != BRCMF_BUS_DATA)) {
  144. brcmf_err("xmit rejected drvup=%d state=%d\n",
  145. drvr->bus_if->drvr_up,
  146. drvr->bus_if->state);
  147. netif_stop_queue(ndev);
  148. return -ENODEV;
  149. }
  150. if (!drvr->iflist[ifp->idx]) {
  151. brcmf_err("bad ifidx %d\n", ifp->idx);
  152. netif_stop_queue(ndev);
  153. return -ENODEV;
  154. }
  155. /* Make sure there's enough room for any header */
  156. if (skb_headroom(skb) < drvr->hdrlen) {
  157. struct sk_buff *skb2;
  158. brcmf_dbg(INFO, "%s: insufficient headroom\n",
  159. brcmf_ifname(drvr, ifp->idx));
  160. drvr->bus_if->tx_realloc++;
  161. skb2 = skb_realloc_headroom(skb, drvr->hdrlen);
  162. dev_kfree_skb(skb);
  163. skb = skb2;
  164. if (skb == NULL) {
  165. brcmf_err("%s: skb_realloc_headroom failed\n",
  166. brcmf_ifname(drvr, ifp->idx));
  167. ret = -ENOMEM;
  168. goto done;
  169. }
  170. }
  171. /* Update multicast statistic */
  172. if (skb->len >= ETH_ALEN) {
  173. u8 *pktdata = (u8 *)(skb->data);
  174. struct ethhdr *eh = (struct ethhdr *)pktdata;
  175. if (is_multicast_ether_addr(eh->h_dest))
  176. drvr->tx_multicast++;
  177. if (ntohs(eh->h_proto) == ETH_P_PAE)
  178. atomic_inc(&drvr->pend_8021x_cnt);
  179. }
  180. /* If the protocol uses a data header, apply it */
  181. brcmf_proto_hdrpush(drvr, ifp->idx, skb);
  182. /* Use bus module to send data frame */
  183. ret = brcmf_bus_txdata(drvr->bus_if, skb);
  184. done:
  185. if (ret)
  186. drvr->bus_if->dstats.tx_dropped++;
  187. else
  188. drvr->bus_if->dstats.tx_packets++;
  189. /* Return ok: we always eat the packet */
  190. return 0;
  191. }
  192. void brcmf_txflowblock(struct device *dev, bool state)
  193. {
  194. struct net_device *ndev;
  195. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  196. struct brcmf_pub *drvr = bus_if->drvr;
  197. int i;
  198. brcmf_dbg(TRACE, "Enter\n");
  199. for (i = 0; i < BRCMF_MAX_IFS; i++)
  200. if (drvr->iflist[i]) {
  201. ndev = drvr->iflist[i]->ndev;
  202. if (state)
  203. netif_stop_queue(ndev);
  204. else
  205. netif_wake_queue(ndev);
  206. }
  207. }
  208. void brcmf_rx_frame(struct device *dev, u8 ifidx,
  209. struct sk_buff_head *skb_list)
  210. {
  211. unsigned char *eth;
  212. uint len;
  213. struct sk_buff *skb, *pnext;
  214. struct brcmf_if *ifp;
  215. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  216. struct brcmf_pub *drvr = bus_if->drvr;
  217. brcmf_dbg(TRACE, "Enter\n");
  218. skb_queue_walk_safe(skb_list, skb, pnext) {
  219. skb_unlink(skb, skb_list);
  220. /* Get the protocol, maintain skb around eth_type_trans()
  221. * The main reason for this hack is for the limitation of
  222. * Linux 2.4 where 'eth_type_trans' uses the
  223. * 'net->hard_header_len'
  224. * to perform skb_pull inside vs ETH_HLEN. Since to avoid
  225. * coping of the packet coming from the network stack to add
  226. * BDC, Hardware header etc, during network interface
  227. * registration
  228. * we set the 'net->hard_header_len' to ETH_HLEN + extra space
  229. * required
  230. * for BDC, Hardware header etc. and not just the ETH_HLEN
  231. */
  232. eth = skb->data;
  233. len = skb->len;
  234. ifp = drvr->iflist[ifidx];
  235. if (ifp == NULL)
  236. ifp = drvr->iflist[0];
  237. if (!ifp || !ifp->ndev ||
  238. ifp->ndev->reg_state != NETREG_REGISTERED) {
  239. brcmu_pkt_buf_free_skb(skb);
  240. continue;
  241. }
  242. skb->dev = ifp->ndev;
  243. skb->protocol = eth_type_trans(skb, skb->dev);
  244. if (skb->pkt_type == PACKET_MULTICAST)
  245. bus_if->dstats.multicast++;
  246. skb->data = eth;
  247. skb->len = len;
  248. /* Strip header, count, deliver upward */
  249. skb_pull(skb, ETH_HLEN);
  250. /* Process special event packets and then discard them */
  251. brcmf_fweh_process_skb(drvr, skb, &ifidx);
  252. if (drvr->iflist[ifidx]) {
  253. ifp = drvr->iflist[ifidx];
  254. ifp->ndev->last_rx = jiffies;
  255. }
  256. bus_if->dstats.rx_bytes += skb->len;
  257. bus_if->dstats.rx_packets++; /* Local count */
  258. if (in_interrupt())
  259. netif_rx(skb);
  260. else
  261. /* If the receive is not processed inside an ISR,
  262. * the softirqd must be woken explicitly to service
  263. * the NET_RX_SOFTIRQ. In 2.6 kernels, this is handled
  264. * by netif_rx_ni(), but in earlier kernels, we need
  265. * to do it manually.
  266. */
  267. netif_rx_ni(skb);
  268. }
  269. }
  270. void brcmf_txcomplete(struct device *dev, struct sk_buff *txp, bool success)
  271. {
  272. uint ifidx;
  273. struct ethhdr *eh;
  274. u16 type;
  275. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  276. struct brcmf_pub *drvr = bus_if->drvr;
  277. brcmf_proto_hdrpull(dev, &ifidx, txp);
  278. eh = (struct ethhdr *)(txp->data);
  279. type = ntohs(eh->h_proto);
  280. if (type == ETH_P_PAE) {
  281. atomic_dec(&drvr->pend_8021x_cnt);
  282. if (waitqueue_active(&drvr->pend_8021x_wait))
  283. wake_up(&drvr->pend_8021x_wait);
  284. }
  285. }
  286. static struct net_device_stats *brcmf_netdev_get_stats(struct net_device *ndev)
  287. {
  288. struct brcmf_if *ifp = netdev_priv(ndev);
  289. struct brcmf_bus *bus_if = ifp->drvr->bus_if;
  290. brcmf_dbg(TRACE, "Enter\n");
  291. /* Copy dongle stats to net device stats */
  292. ifp->stats.rx_packets = bus_if->dstats.rx_packets;
  293. ifp->stats.tx_packets = bus_if->dstats.tx_packets;
  294. ifp->stats.rx_bytes = bus_if->dstats.rx_bytes;
  295. ifp->stats.tx_bytes = bus_if->dstats.tx_bytes;
  296. ifp->stats.rx_errors = bus_if->dstats.rx_errors;
  297. ifp->stats.tx_errors = bus_if->dstats.tx_errors;
  298. ifp->stats.rx_dropped = bus_if->dstats.rx_dropped;
  299. ifp->stats.tx_dropped = bus_if->dstats.tx_dropped;
  300. ifp->stats.multicast = bus_if->dstats.multicast;
  301. return &ifp->stats;
  302. }
  303. /*
  304. * Set current toe component enables in toe_ol iovar,
  305. * and set toe global enable iovar
  306. */
  307. static int brcmf_toe_set(struct brcmf_if *ifp, u32 toe_ol)
  308. {
  309. s32 err;
  310. err = brcmf_fil_iovar_int_set(ifp, "toe_ol", toe_ol);
  311. if (err < 0) {
  312. brcmf_err("Setting toe_ol failed, %d\n", err);
  313. return err;
  314. }
  315. err = brcmf_fil_iovar_int_set(ifp, "toe", (toe_ol != 0));
  316. if (err < 0)
  317. brcmf_err("Setting toe failed, %d\n", err);
  318. return err;
  319. }
  320. static void brcmf_ethtool_get_drvinfo(struct net_device *ndev,
  321. struct ethtool_drvinfo *info)
  322. {
  323. struct brcmf_if *ifp = netdev_priv(ndev);
  324. struct brcmf_pub *drvr = ifp->drvr;
  325. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  326. snprintf(info->version, sizeof(info->version), "%lu",
  327. drvr->drv_version);
  328. strlcpy(info->bus_info, dev_name(drvr->bus_if->dev),
  329. sizeof(info->bus_info));
  330. }
  331. static const struct ethtool_ops brcmf_ethtool_ops = {
  332. .get_drvinfo = brcmf_ethtool_get_drvinfo,
  333. };
  334. static int brcmf_ethtool(struct brcmf_if *ifp, void __user *uaddr)
  335. {
  336. struct brcmf_pub *drvr = ifp->drvr;
  337. struct ethtool_drvinfo info;
  338. char drvname[sizeof(info.driver)];
  339. u32 cmd;
  340. struct ethtool_value edata;
  341. u32 toe_cmpnt, csum_dir;
  342. int ret;
  343. brcmf_dbg(TRACE, "Enter\n");
  344. /* all ethtool calls start with a cmd word */
  345. if (copy_from_user(&cmd, uaddr, sizeof(u32)))
  346. return -EFAULT;
  347. switch (cmd) {
  348. case ETHTOOL_GDRVINFO:
  349. /* Copy out any request driver name */
  350. if (copy_from_user(&info, uaddr, sizeof(info)))
  351. return -EFAULT;
  352. strncpy(drvname, info.driver, sizeof(info.driver));
  353. drvname[sizeof(info.driver) - 1] = '\0';
  354. /* clear struct for return */
  355. memset(&info, 0, sizeof(info));
  356. info.cmd = cmd;
  357. /* if requested, identify ourselves */
  358. if (strcmp(drvname, "?dhd") == 0) {
  359. sprintf(info.driver, "dhd");
  360. strcpy(info.version, BRCMF_VERSION_STR);
  361. }
  362. /* otherwise, require dongle to be up */
  363. else if (!drvr->bus_if->drvr_up) {
  364. brcmf_err("dongle is not up\n");
  365. return -ENODEV;
  366. }
  367. /* finally, report dongle driver type */
  368. else
  369. sprintf(info.driver, "wl");
  370. sprintf(info.version, "%lu", drvr->drv_version);
  371. if (copy_to_user(uaddr, &info, sizeof(info)))
  372. return -EFAULT;
  373. brcmf_dbg(CTL, "given %*s, returning %s\n",
  374. (int)sizeof(drvname), drvname, info.driver);
  375. break;
  376. /* Get toe offload components from dongle */
  377. case ETHTOOL_GRXCSUM:
  378. case ETHTOOL_GTXCSUM:
  379. ret = brcmf_fil_iovar_int_get(ifp, "toe_ol", &toe_cmpnt);
  380. if (ret < 0)
  381. return ret;
  382. csum_dir =
  383. (cmd == ETHTOOL_GTXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
  384. edata.cmd = cmd;
  385. edata.data = (toe_cmpnt & csum_dir) ? 1 : 0;
  386. if (copy_to_user(uaddr, &edata, sizeof(edata)))
  387. return -EFAULT;
  388. break;
  389. /* Set toe offload components in dongle */
  390. case ETHTOOL_SRXCSUM:
  391. case ETHTOOL_STXCSUM:
  392. if (copy_from_user(&edata, uaddr, sizeof(edata)))
  393. return -EFAULT;
  394. /* Read the current settings, update and write back */
  395. ret = brcmf_fil_iovar_int_get(ifp, "toe_ol", &toe_cmpnt);
  396. if (ret < 0)
  397. return ret;
  398. csum_dir =
  399. (cmd == ETHTOOL_STXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
  400. if (edata.data != 0)
  401. toe_cmpnt |= csum_dir;
  402. else
  403. toe_cmpnt &= ~csum_dir;
  404. ret = brcmf_toe_set(ifp, toe_cmpnt);
  405. if (ret < 0)
  406. return ret;
  407. /* If setting TX checksum mode, tell Linux the new mode */
  408. if (cmd == ETHTOOL_STXCSUM) {
  409. if (edata.data)
  410. ifp->ndev->features |= NETIF_F_IP_CSUM;
  411. else
  412. ifp->ndev->features &= ~NETIF_F_IP_CSUM;
  413. }
  414. break;
  415. default:
  416. return -EOPNOTSUPP;
  417. }
  418. return 0;
  419. }
  420. static int brcmf_netdev_ioctl_entry(struct net_device *ndev, struct ifreq *ifr,
  421. int cmd)
  422. {
  423. struct brcmf_if *ifp = netdev_priv(ndev);
  424. struct brcmf_pub *drvr = ifp->drvr;
  425. brcmf_dbg(TRACE, "ifidx %d, cmd 0x%04x\n", ifp->idx, cmd);
  426. if (!drvr->iflist[ifp->idx])
  427. return -1;
  428. if (cmd == SIOCETHTOOL)
  429. return brcmf_ethtool(ifp, ifr->ifr_data);
  430. return -EOPNOTSUPP;
  431. }
  432. static int brcmf_netdev_stop(struct net_device *ndev)
  433. {
  434. struct brcmf_if *ifp = netdev_priv(ndev);
  435. struct brcmf_pub *drvr = ifp->drvr;
  436. brcmf_dbg(TRACE, "Enter\n");
  437. if (drvr->bus_if->drvr_up == 0)
  438. return 0;
  439. brcmf_cfg80211_down(ndev);
  440. /* Set state and stop OS transmissions */
  441. drvr->bus_if->drvr_up = false;
  442. netif_stop_queue(ndev);
  443. return 0;
  444. }
  445. static int brcmf_netdev_open(struct net_device *ndev)
  446. {
  447. struct brcmf_if *ifp = netdev_priv(ndev);
  448. struct brcmf_pub *drvr = ifp->drvr;
  449. struct brcmf_bus *bus_if = drvr->bus_if;
  450. u32 toe_ol;
  451. s32 ret = 0;
  452. brcmf_dbg(TRACE, "ifidx %d\n", ifp->idx);
  453. /* If bus is not ready, can't continue */
  454. if (bus_if->state != BRCMF_BUS_DATA) {
  455. brcmf_err("failed bus is not ready\n");
  456. return -EAGAIN;
  457. }
  458. atomic_set(&drvr->pend_8021x_cnt, 0);
  459. memcpy(ndev->dev_addr, drvr->mac, ETH_ALEN);
  460. /* Get current TOE mode from dongle */
  461. if (brcmf_fil_iovar_int_get(ifp, "toe_ol", &toe_ol) >= 0
  462. && (toe_ol & TOE_TX_CSUM_OL) != 0)
  463. drvr->iflist[ifp->idx]->ndev->features |=
  464. NETIF_F_IP_CSUM;
  465. else
  466. drvr->iflist[ifp->idx]->ndev->features &=
  467. ~NETIF_F_IP_CSUM;
  468. /* make sure RF is ready for work */
  469. brcmf_fil_cmd_int_set(ifp, BRCMF_C_UP, 0);
  470. /* Allow transmit calls */
  471. netif_start_queue(ndev);
  472. drvr->bus_if->drvr_up = true;
  473. if (brcmf_cfg80211_up(ndev)) {
  474. brcmf_err("failed to bring up cfg80211\n");
  475. return -1;
  476. }
  477. return ret;
  478. }
  479. static const struct net_device_ops brcmf_netdev_ops_pri = {
  480. .ndo_open = brcmf_netdev_open,
  481. .ndo_stop = brcmf_netdev_stop,
  482. .ndo_get_stats = brcmf_netdev_get_stats,
  483. .ndo_do_ioctl = brcmf_netdev_ioctl_entry,
  484. .ndo_start_xmit = brcmf_netdev_start_xmit,
  485. .ndo_set_mac_address = brcmf_netdev_set_mac_address,
  486. .ndo_set_rx_mode = brcmf_netdev_set_multicast_list
  487. };
  488. static const struct net_device_ops brcmf_netdev_ops_virt = {
  489. .ndo_open = brcmf_cfg80211_up,
  490. .ndo_stop = brcmf_cfg80211_down,
  491. .ndo_get_stats = brcmf_netdev_get_stats,
  492. .ndo_do_ioctl = brcmf_netdev_ioctl_entry,
  493. .ndo_start_xmit = brcmf_netdev_start_xmit,
  494. .ndo_set_mac_address = brcmf_netdev_set_mac_address,
  495. .ndo_set_rx_mode = brcmf_netdev_set_multicast_list
  496. };
  497. int brcmf_net_attach(struct brcmf_if *ifp)
  498. {
  499. struct brcmf_pub *drvr = ifp->drvr;
  500. struct net_device *ndev;
  501. brcmf_dbg(TRACE, "ifidx %d mac %pM\n", ifp->idx, ifp->mac_addr);
  502. ndev = ifp->ndev;
  503. /* set appropriate operations */
  504. if (!ifp->idx)
  505. ndev->netdev_ops = &brcmf_netdev_ops_pri;
  506. else
  507. ndev->netdev_ops = &brcmf_netdev_ops_virt;
  508. ndev->hard_header_len = ETH_HLEN + drvr->hdrlen;
  509. ndev->ethtool_ops = &brcmf_ethtool_ops;
  510. drvr->rxsz = ndev->mtu + ndev->hard_header_len +
  511. drvr->hdrlen;
  512. /* set the mac address */
  513. memcpy(ndev->dev_addr, ifp->mac_addr, ETH_ALEN);
  514. if (register_netdev(ndev) != 0) {
  515. brcmf_err("couldn't register the net device\n");
  516. goto fail;
  517. }
  518. brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
  519. return 0;
  520. fail:
  521. ndev->netdev_ops = NULL;
  522. return -EBADE;
  523. }
  524. struct brcmf_if *brcmf_add_if(struct brcmf_pub *drvr, int ifidx, s32 bssidx,
  525. char *name, u8 *addr_mask)
  526. {
  527. struct brcmf_if *ifp;
  528. struct net_device *ndev;
  529. int i;
  530. brcmf_dbg(TRACE, "idx %d\n", ifidx);
  531. ifp = drvr->iflist[ifidx];
  532. /*
  533. * Delete the existing interface before overwriting it
  534. * in case we missed the BRCMF_E_IF_DEL event.
  535. */
  536. if (ifp) {
  537. brcmf_err("ERROR: netdev:%s already exists\n",
  538. ifp->ndev->name);
  539. if (ifidx) {
  540. netif_stop_queue(ifp->ndev);
  541. unregister_netdev(ifp->ndev);
  542. free_netdev(ifp->ndev);
  543. drvr->iflist[ifidx] = NULL;
  544. } else {
  545. brcmf_err("ignore IF event\n");
  546. return ERR_PTR(-EINVAL);
  547. }
  548. }
  549. /* Allocate netdev, including space for private structure */
  550. ndev = alloc_netdev(sizeof(struct brcmf_if), name, ether_setup);
  551. if (!ndev) {
  552. brcmf_err("OOM - alloc_netdev\n");
  553. return ERR_PTR(-ENOMEM);
  554. }
  555. ifp = netdev_priv(ndev);
  556. ifp->ndev = ndev;
  557. ifp->drvr = drvr;
  558. drvr->iflist[ifidx] = ifp;
  559. ifp->idx = ifidx;
  560. ifp->bssidx = bssidx;
  561. INIT_WORK(&ifp->setmacaddr_work, _brcmf_set_mac_address);
  562. INIT_WORK(&ifp->multicast_work, _brcmf_set_multicast_list);
  563. if (addr_mask != NULL)
  564. for (i = 0; i < ETH_ALEN; i++)
  565. ifp->mac_addr[i] = drvr->mac[i] ^ addr_mask[i];
  566. brcmf_dbg(TRACE, " ==== pid:%x, if:%s (%pM) created ===\n",
  567. current->pid, ifp->ndev->name, ifp->mac_addr);
  568. return ifp;
  569. }
  570. void brcmf_del_if(struct brcmf_pub *drvr, int ifidx)
  571. {
  572. struct brcmf_if *ifp;
  573. brcmf_dbg(TRACE, "idx %d\n", ifidx);
  574. ifp = drvr->iflist[ifidx];
  575. if (!ifp) {
  576. brcmf_err("Null interface\n");
  577. return;
  578. }
  579. if (ifp->ndev) {
  580. if (ifidx == 0) {
  581. if (ifp->ndev->netdev_ops == &brcmf_netdev_ops_pri) {
  582. rtnl_lock();
  583. brcmf_netdev_stop(ifp->ndev);
  584. rtnl_unlock();
  585. }
  586. } else {
  587. netif_stop_queue(ifp->ndev);
  588. }
  589. cancel_work_sync(&ifp->setmacaddr_work);
  590. cancel_work_sync(&ifp->multicast_work);
  591. unregister_netdev(ifp->ndev);
  592. drvr->iflist[ifidx] = NULL;
  593. if (ifidx == 0)
  594. brcmf_cfg80211_detach(drvr->config);
  595. free_netdev(ifp->ndev);
  596. }
  597. }
  598. int brcmf_attach(uint bus_hdrlen, struct device *dev)
  599. {
  600. struct brcmf_pub *drvr = NULL;
  601. int ret = 0;
  602. brcmf_dbg(TRACE, "Enter\n");
  603. /* Allocate primary brcmf_info */
  604. drvr = kzalloc(sizeof(struct brcmf_pub), GFP_ATOMIC);
  605. if (!drvr)
  606. return -ENOMEM;
  607. mutex_init(&drvr->proto_block);
  608. /* Link to bus module */
  609. drvr->hdrlen = bus_hdrlen;
  610. drvr->bus_if = dev_get_drvdata(dev);
  611. drvr->bus_if->drvr = drvr;
  612. /* create device debugfs folder */
  613. brcmf_debugfs_attach(drvr);
  614. /* Attach and link in the protocol */
  615. ret = brcmf_proto_attach(drvr);
  616. if (ret != 0) {
  617. brcmf_err("brcmf_prot_attach failed\n");
  618. goto fail;
  619. }
  620. /* attach firmware event handler */
  621. brcmf_fweh_attach(drvr);
  622. INIT_LIST_HEAD(&drvr->bus_if->dcmd_list);
  623. init_waitqueue_head(&drvr->pend_8021x_wait);
  624. return ret;
  625. fail:
  626. brcmf_detach(dev);
  627. return ret;
  628. }
  629. int brcmf_bus_start(struct device *dev)
  630. {
  631. int ret = -1;
  632. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  633. struct brcmf_pub *drvr = bus_if->drvr;
  634. struct brcmf_if *ifp;
  635. brcmf_dbg(TRACE, "\n");
  636. /* Bring up the bus */
  637. ret = brcmf_bus_init(bus_if);
  638. if (ret != 0) {
  639. brcmf_err("brcmf_sdbrcm_bus_init failed %d\n", ret);
  640. return ret;
  641. }
  642. /* add primary networking interface */
  643. ifp = brcmf_add_if(drvr, 0, 0, "wlan%d", NULL);
  644. if (IS_ERR(ifp))
  645. return PTR_ERR(ifp);
  646. /* signal bus ready */
  647. bus_if->state = BRCMF_BUS_DATA;
  648. /* Bus is ready, do any initialization */
  649. ret = brcmf_c_preinit_dcmds(ifp);
  650. if (ret < 0)
  651. goto fail;
  652. drvr->config = brcmf_cfg80211_attach(drvr, bus_if->dev);
  653. if (drvr->config == NULL) {
  654. ret = -ENOMEM;
  655. goto fail;
  656. }
  657. ret = brcmf_fweh_activate_events(ifp);
  658. if (ret < 0)
  659. goto fail;
  660. ret = brcmf_net_attach(ifp);
  661. fail:
  662. if (ret < 0) {
  663. brcmf_err("failed: %d\n", ret);
  664. if (drvr->config)
  665. brcmf_cfg80211_detach(drvr->config);
  666. free_netdev(drvr->iflist[0]->ndev);
  667. drvr->iflist[0] = NULL;
  668. return ret;
  669. }
  670. return 0;
  671. }
  672. static void brcmf_bus_detach(struct brcmf_pub *drvr)
  673. {
  674. brcmf_dbg(TRACE, "Enter\n");
  675. if (drvr) {
  676. /* Stop the protocol module */
  677. brcmf_proto_stop(drvr);
  678. /* Stop the bus module */
  679. brcmf_bus_stop(drvr->bus_if);
  680. }
  681. }
  682. void brcmf_detach(struct device *dev)
  683. {
  684. int i;
  685. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  686. struct brcmf_pub *drvr = bus_if->drvr;
  687. brcmf_dbg(TRACE, "Enter\n");
  688. if (drvr == NULL)
  689. return;
  690. /* stop firmware event handling */
  691. brcmf_fweh_detach(drvr);
  692. /* make sure primary interface removed last */
  693. for (i = BRCMF_MAX_IFS-1; i > -1; i--)
  694. if (drvr->iflist[i])
  695. brcmf_del_if(drvr, i);
  696. brcmf_bus_detach(drvr);
  697. if (drvr->prot) {
  698. brcmf_proto_detach(drvr);
  699. }
  700. brcmf_debugfs_detach(drvr);
  701. bus_if->drvr = NULL;
  702. kfree(drvr);
  703. }
  704. static int brcmf_get_pend_8021x_cnt(struct brcmf_pub *drvr)
  705. {
  706. return atomic_read(&drvr->pend_8021x_cnt);
  707. }
  708. int brcmf_netdev_wait_pend8021x(struct net_device *ndev)
  709. {
  710. struct brcmf_if *ifp = netdev_priv(ndev);
  711. struct brcmf_pub *drvr = ifp->drvr;
  712. int err;
  713. err = wait_event_timeout(drvr->pend_8021x_wait,
  714. !brcmf_get_pend_8021x_cnt(drvr),
  715. msecs_to_jiffies(MAX_WAIT_FOR_8021X_TX));
  716. WARN_ON(!err);
  717. return !err;
  718. }
  719. static void brcmf_driver_init(struct work_struct *work)
  720. {
  721. brcmf_debugfs_init();
  722. #ifdef CONFIG_BRCMFMAC_SDIO
  723. brcmf_sdio_init();
  724. #endif
  725. #ifdef CONFIG_BRCMFMAC_USB
  726. brcmf_usb_init();
  727. #endif
  728. }
  729. static DECLARE_WORK(brcmf_driver_work, brcmf_driver_init);
  730. static int __init brcmfmac_module_init(void)
  731. {
  732. if (!schedule_work(&brcmf_driver_work))
  733. return -EBUSY;
  734. return 0;
  735. }
  736. static void __exit brcmfmac_module_exit(void)
  737. {
  738. cancel_work_sync(&brcmf_driver_work);
  739. #ifdef CONFIG_BRCMFMAC_SDIO
  740. brcmf_sdio_exit();
  741. #endif
  742. #ifdef CONFIG_BRCMFMAC_USB
  743. brcmf_usb_exit();
  744. #endif
  745. brcmf_debugfs_exit();
  746. }
  747. module_init(brcmfmac_module_init);
  748. module_exit(brcmfmac_module_exit);