dhd_linux.c 21 KB

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