main.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  1. /*
  2. * This file contains the major functions in WLAN
  3. * driver. It includes init, exit, open, close and main
  4. * thread etc..
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/module.h>
  8. #include <linux/delay.h>
  9. #include <linux/etherdevice.h>
  10. #include <linux/hardirq.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/if_arp.h>
  13. #include <linux/kthread.h>
  14. #include <linux/kfifo.h>
  15. #include <linux/slab.h>
  16. #include <net/cfg80211.h>
  17. #include "host.h"
  18. #include "decl.h"
  19. #include "dev.h"
  20. #include "cfg.h"
  21. #include "debugfs.h"
  22. #include "cmd.h"
  23. #include "mesh.h"
  24. #define DRIVER_RELEASE_VERSION "323.p0"
  25. const char lbs_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
  26. #ifdef DEBUG
  27. "-dbg"
  28. #endif
  29. "";
  30. /* Module parameters */
  31. unsigned int lbs_debug;
  32. EXPORT_SYMBOL_GPL(lbs_debug);
  33. module_param_named(libertas_debug, lbs_debug, int, 0644);
  34. unsigned int lbs_disablemesh;
  35. EXPORT_SYMBOL_GPL(lbs_disablemesh);
  36. module_param_named(libertas_disablemesh, lbs_disablemesh, int, 0644);
  37. /*
  38. * This global structure is used to send the confirm_sleep command as
  39. * fast as possible down to the firmware.
  40. */
  41. struct cmd_confirm_sleep confirm_sleep;
  42. /*
  43. * the table to keep region code
  44. */
  45. u16 lbs_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
  46. { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
  47. /*
  48. * FW rate table. FW refers to rates by their index in this table, not by the
  49. * rate value itself. Values of 0x00 are
  50. * reserved positions.
  51. */
  52. static u8 fw_data_rates[MAX_RATES] =
  53. { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12,
  54. 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00
  55. };
  56. /**
  57. * lbs_fw_index_to_data_rate - use index to get the data rate
  58. *
  59. * @idx: The index of data rate
  60. * returns: data rate or 0
  61. */
  62. u32 lbs_fw_index_to_data_rate(u8 idx)
  63. {
  64. if (idx >= sizeof(fw_data_rates))
  65. idx = 0;
  66. return fw_data_rates[idx];
  67. }
  68. /**
  69. * lbs_data_rate_to_fw_index - use rate to get the index
  70. *
  71. * @rate: data rate
  72. * returns: index or 0
  73. */
  74. u8 lbs_data_rate_to_fw_index(u32 rate)
  75. {
  76. u8 i;
  77. if (!rate)
  78. return 0;
  79. for (i = 0; i < sizeof(fw_data_rates); i++) {
  80. if (rate == fw_data_rates[i])
  81. return i;
  82. }
  83. return 0;
  84. }
  85. int lbs_set_iface_type(struct lbs_private *priv, enum nl80211_iftype type)
  86. {
  87. int ret = 0;
  88. switch (type) {
  89. case NL80211_IFTYPE_MONITOR:
  90. ret = lbs_set_monitor_mode(priv, 1);
  91. break;
  92. case NL80211_IFTYPE_STATION:
  93. if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR)
  94. ret = lbs_set_monitor_mode(priv, 0);
  95. if (!ret)
  96. ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, 1);
  97. break;
  98. case NL80211_IFTYPE_ADHOC:
  99. if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR)
  100. ret = lbs_set_monitor_mode(priv, 0);
  101. if (!ret)
  102. ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, 2);
  103. break;
  104. default:
  105. ret = -ENOTSUPP;
  106. }
  107. return ret;
  108. }
  109. int lbs_start_iface(struct lbs_private *priv)
  110. {
  111. struct cmd_ds_802_11_mac_address cmd;
  112. int ret;
  113. if (priv->power_restore) {
  114. ret = priv->power_restore(priv);
  115. if (ret)
  116. return ret;
  117. }
  118. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  119. cmd.action = cpu_to_le16(CMD_ACT_SET);
  120. memcpy(cmd.macadd, priv->current_addr, ETH_ALEN);
  121. ret = lbs_cmd_with_response(priv, CMD_802_11_MAC_ADDRESS, &cmd);
  122. if (ret) {
  123. lbs_deb_net("set MAC address failed\n");
  124. goto err;
  125. }
  126. ret = lbs_set_iface_type(priv, priv->wdev->iftype);
  127. if (ret) {
  128. lbs_deb_net("set iface type failed\n");
  129. goto err;
  130. }
  131. ret = lbs_set_11d_domain_info(priv);
  132. if (ret) {
  133. lbs_deb_net("set 11d domain info failed\n");
  134. goto err;
  135. }
  136. lbs_update_channel(priv);
  137. priv->iface_running = true;
  138. return 0;
  139. err:
  140. if (priv->power_save)
  141. priv->power_save(priv);
  142. return ret;
  143. }
  144. /**
  145. * lbs_dev_open - open the ethX interface
  146. *
  147. * @dev: A pointer to &net_device structure
  148. * returns: 0 or -EBUSY if monitor mode active
  149. */
  150. static int lbs_dev_open(struct net_device *dev)
  151. {
  152. struct lbs_private *priv = dev->ml_priv;
  153. int ret = 0;
  154. lbs_deb_enter(LBS_DEB_NET);
  155. if (!priv->iface_running) {
  156. ret = lbs_start_iface(priv);
  157. if (ret)
  158. goto out;
  159. }
  160. spin_lock_irq(&priv->driver_lock);
  161. netif_carrier_off(dev);
  162. if (!priv->tx_pending_len)
  163. netif_wake_queue(dev);
  164. spin_unlock_irq(&priv->driver_lock);
  165. out:
  166. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  167. return ret;
  168. }
  169. static bool lbs_command_queue_empty(struct lbs_private *priv)
  170. {
  171. unsigned long flags;
  172. bool ret;
  173. spin_lock_irqsave(&priv->driver_lock, flags);
  174. ret = priv->cur_cmd == NULL && list_empty(&priv->cmdpendingq);
  175. spin_unlock_irqrestore(&priv->driver_lock, flags);
  176. return ret;
  177. }
  178. int lbs_stop_iface(struct lbs_private *priv)
  179. {
  180. unsigned long flags;
  181. int ret = 0;
  182. lbs_deb_enter(LBS_DEB_MAIN);
  183. spin_lock_irqsave(&priv->driver_lock, flags);
  184. priv->iface_running = false;
  185. kfree_skb(priv->currenttxskb);
  186. priv->currenttxskb = NULL;
  187. priv->tx_pending_len = 0;
  188. spin_unlock_irqrestore(&priv->driver_lock, flags);
  189. cancel_work_sync(&priv->mcast_work);
  190. del_timer_sync(&priv->tx_lockup_timer);
  191. /* Disable command processing, and wait for all commands to complete */
  192. lbs_deb_main("waiting for commands to complete\n");
  193. wait_event(priv->waitq, lbs_command_queue_empty(priv));
  194. lbs_deb_main("all commands completed\n");
  195. if (priv->power_save)
  196. ret = priv->power_save(priv);
  197. lbs_deb_leave(LBS_DEB_MAIN);
  198. return ret;
  199. }
  200. /**
  201. * lbs_eth_stop - close the ethX interface
  202. *
  203. * @dev: A pointer to &net_device structure
  204. * returns: 0
  205. */
  206. static int lbs_eth_stop(struct net_device *dev)
  207. {
  208. struct lbs_private *priv = dev->ml_priv;
  209. lbs_deb_enter(LBS_DEB_NET);
  210. if (priv->connect_status == LBS_CONNECTED)
  211. lbs_disconnect(priv, WLAN_REASON_DEAUTH_LEAVING);
  212. spin_lock_irq(&priv->driver_lock);
  213. netif_stop_queue(dev);
  214. spin_unlock_irq(&priv->driver_lock);
  215. lbs_update_mcast(priv);
  216. cancel_delayed_work_sync(&priv->scan_work);
  217. if (priv->scan_req)
  218. lbs_scan_done(priv);
  219. netif_carrier_off(priv->dev);
  220. if (!lbs_iface_active(priv))
  221. lbs_stop_iface(priv);
  222. lbs_deb_leave(LBS_DEB_NET);
  223. return 0;
  224. }
  225. void lbs_host_to_card_done(struct lbs_private *priv)
  226. {
  227. unsigned long flags;
  228. lbs_deb_enter(LBS_DEB_THREAD);
  229. spin_lock_irqsave(&priv->driver_lock, flags);
  230. del_timer(&priv->tx_lockup_timer);
  231. priv->dnld_sent = DNLD_RES_RECEIVED;
  232. /* Wake main thread if commands are pending */
  233. if (!priv->cur_cmd || priv->tx_pending_len > 0) {
  234. if (!priv->wakeup_dev_required)
  235. wake_up(&priv->waitq);
  236. }
  237. spin_unlock_irqrestore(&priv->driver_lock, flags);
  238. lbs_deb_leave(LBS_DEB_THREAD);
  239. }
  240. EXPORT_SYMBOL_GPL(lbs_host_to_card_done);
  241. int lbs_set_mac_address(struct net_device *dev, void *addr)
  242. {
  243. int ret = 0;
  244. struct lbs_private *priv = dev->ml_priv;
  245. struct sockaddr *phwaddr = addr;
  246. lbs_deb_enter(LBS_DEB_NET);
  247. /*
  248. * Can only set MAC address when all interfaces are down, to be written
  249. * to the hardware when one of them is brought up.
  250. */
  251. if (lbs_iface_active(priv))
  252. return -EBUSY;
  253. /* In case it was called from the mesh device */
  254. dev = priv->dev;
  255. memcpy(priv->current_addr, phwaddr->sa_data, ETH_ALEN);
  256. memcpy(dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
  257. if (priv->mesh_dev)
  258. memcpy(priv->mesh_dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
  259. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  260. return ret;
  261. }
  262. static inline int mac_in_list(unsigned char *list, int list_len,
  263. unsigned char *mac)
  264. {
  265. while (list_len) {
  266. if (!memcmp(list, mac, ETH_ALEN))
  267. return 1;
  268. list += ETH_ALEN;
  269. list_len--;
  270. }
  271. return 0;
  272. }
  273. static int lbs_add_mcast_addrs(struct cmd_ds_mac_multicast_adr *cmd,
  274. struct net_device *dev, int nr_addrs)
  275. {
  276. int i = nr_addrs;
  277. struct netdev_hw_addr *ha;
  278. int cnt;
  279. if ((dev->flags & (IFF_UP|IFF_MULTICAST)) != (IFF_UP|IFF_MULTICAST))
  280. return nr_addrs;
  281. netif_addr_lock_bh(dev);
  282. cnt = netdev_mc_count(dev);
  283. netdev_for_each_mc_addr(ha, dev) {
  284. if (mac_in_list(cmd->maclist, nr_addrs, ha->addr)) {
  285. lbs_deb_net("mcast address %s:%pM skipped\n", dev->name,
  286. ha->addr);
  287. cnt--;
  288. continue;
  289. }
  290. if (i == MRVDRV_MAX_MULTICAST_LIST_SIZE)
  291. break;
  292. memcpy(&cmd->maclist[6*i], ha->addr, ETH_ALEN);
  293. lbs_deb_net("mcast address %s:%pM added to filter\n", dev->name,
  294. ha->addr);
  295. i++;
  296. cnt--;
  297. }
  298. netif_addr_unlock_bh(dev);
  299. if (cnt)
  300. return -EOVERFLOW;
  301. return i;
  302. }
  303. void lbs_update_mcast(struct lbs_private *priv)
  304. {
  305. struct cmd_ds_mac_multicast_adr mcast_cmd;
  306. int dev_flags = 0;
  307. int nr_addrs;
  308. int old_mac_control = priv->mac_control;
  309. lbs_deb_enter(LBS_DEB_NET);
  310. if (netif_running(priv->dev))
  311. dev_flags |= priv->dev->flags;
  312. if (priv->mesh_dev && netif_running(priv->mesh_dev))
  313. dev_flags |= priv->mesh_dev->flags;
  314. if (dev_flags & IFF_PROMISC) {
  315. priv->mac_control |= CMD_ACT_MAC_PROMISCUOUS_ENABLE;
  316. priv->mac_control &= ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
  317. CMD_ACT_MAC_MULTICAST_ENABLE);
  318. goto out_set_mac_control;
  319. } else if (dev_flags & IFF_ALLMULTI) {
  320. do_allmulti:
  321. priv->mac_control |= CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
  322. priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
  323. CMD_ACT_MAC_MULTICAST_ENABLE);
  324. goto out_set_mac_control;
  325. }
  326. /* Once for priv->dev, again for priv->mesh_dev if it exists */
  327. nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->dev, 0);
  328. if (nr_addrs >= 0 && priv->mesh_dev)
  329. nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->mesh_dev, nr_addrs);
  330. if (nr_addrs < 0)
  331. goto do_allmulti;
  332. if (nr_addrs) {
  333. int size = offsetof(struct cmd_ds_mac_multicast_adr,
  334. maclist[6*nr_addrs]);
  335. mcast_cmd.action = cpu_to_le16(CMD_ACT_SET);
  336. mcast_cmd.hdr.size = cpu_to_le16(size);
  337. mcast_cmd.nr_of_adrs = cpu_to_le16(nr_addrs);
  338. lbs_cmd_async(priv, CMD_MAC_MULTICAST_ADR, &mcast_cmd.hdr, size);
  339. priv->mac_control |= CMD_ACT_MAC_MULTICAST_ENABLE;
  340. } else
  341. priv->mac_control &= ~CMD_ACT_MAC_MULTICAST_ENABLE;
  342. priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
  343. CMD_ACT_MAC_ALL_MULTICAST_ENABLE);
  344. out_set_mac_control:
  345. if (priv->mac_control != old_mac_control)
  346. lbs_set_mac_control(priv);
  347. lbs_deb_leave(LBS_DEB_NET);
  348. }
  349. static void lbs_set_mcast_worker(struct work_struct *work)
  350. {
  351. struct lbs_private *priv = container_of(work, struct lbs_private, mcast_work);
  352. lbs_update_mcast(priv);
  353. }
  354. void lbs_set_multicast_list(struct net_device *dev)
  355. {
  356. struct lbs_private *priv = dev->ml_priv;
  357. schedule_work(&priv->mcast_work);
  358. }
  359. /**
  360. * lbs_thread - handles the major jobs in the LBS driver.
  361. * It handles all events generated by firmware, RX data received
  362. * from firmware and TX data sent from kernel.
  363. *
  364. * @data: A pointer to &lbs_thread structure
  365. * returns: 0
  366. */
  367. static int lbs_thread(void *data)
  368. {
  369. struct net_device *dev = data;
  370. struct lbs_private *priv = dev->ml_priv;
  371. wait_queue_t wait;
  372. lbs_deb_enter(LBS_DEB_THREAD);
  373. init_waitqueue_entry(&wait, current);
  374. for (;;) {
  375. int shouldsleep;
  376. u8 resp_idx;
  377. lbs_deb_thread("1: currenttxskb %p, dnld_sent %d\n",
  378. priv->currenttxskb, priv->dnld_sent);
  379. add_wait_queue(&priv->waitq, &wait);
  380. set_current_state(TASK_INTERRUPTIBLE);
  381. spin_lock_irq(&priv->driver_lock);
  382. if (kthread_should_stop())
  383. shouldsleep = 0; /* Bye */
  384. else if (priv->surpriseremoved)
  385. shouldsleep = 1; /* We need to wait until we're _told_ to die */
  386. else if (priv->psstate == PS_STATE_SLEEP)
  387. shouldsleep = 1; /* Sleep mode. Nothing we can do till it wakes */
  388. else if (priv->cmd_timed_out)
  389. shouldsleep = 0; /* Command timed out. Recover */
  390. else if (!priv->fw_ready)
  391. shouldsleep = 1; /* Firmware not ready. We're waiting for it */
  392. else if (priv->dnld_sent)
  393. shouldsleep = 1; /* Something is en route to the device already */
  394. else if (priv->tx_pending_len > 0)
  395. shouldsleep = 0; /* We've a packet to send */
  396. else if (priv->resp_len[priv->resp_idx])
  397. shouldsleep = 0; /* We have a command response */
  398. else if (priv->cur_cmd)
  399. shouldsleep = 1; /* Can't send a command; one already running */
  400. else if (!list_empty(&priv->cmdpendingq) &&
  401. !(priv->wakeup_dev_required))
  402. shouldsleep = 0; /* We have a command to send */
  403. else if (kfifo_len(&priv->event_fifo))
  404. shouldsleep = 0; /* We have an event to process */
  405. else
  406. shouldsleep = 1; /* No command */
  407. if (shouldsleep) {
  408. lbs_deb_thread("sleeping, connect_status %d, "
  409. "psmode %d, psstate %d\n",
  410. priv->connect_status,
  411. priv->psmode, priv->psstate);
  412. spin_unlock_irq(&priv->driver_lock);
  413. schedule();
  414. } else
  415. spin_unlock_irq(&priv->driver_lock);
  416. lbs_deb_thread("2: currenttxskb %p, dnld_send %d\n",
  417. priv->currenttxskb, priv->dnld_sent);
  418. set_current_state(TASK_RUNNING);
  419. remove_wait_queue(&priv->waitq, &wait);
  420. lbs_deb_thread("3: currenttxskb %p, dnld_sent %d\n",
  421. priv->currenttxskb, priv->dnld_sent);
  422. if (kthread_should_stop()) {
  423. lbs_deb_thread("break from main thread\n");
  424. break;
  425. }
  426. if (priv->surpriseremoved) {
  427. lbs_deb_thread("adapter removed; waiting to die...\n");
  428. continue;
  429. }
  430. lbs_deb_thread("4: currenttxskb %p, dnld_sent %d\n",
  431. priv->currenttxskb, priv->dnld_sent);
  432. /* Process any pending command response */
  433. spin_lock_irq(&priv->driver_lock);
  434. resp_idx = priv->resp_idx;
  435. if (priv->resp_len[resp_idx]) {
  436. spin_unlock_irq(&priv->driver_lock);
  437. lbs_process_command_response(priv,
  438. priv->resp_buf[resp_idx],
  439. priv->resp_len[resp_idx]);
  440. spin_lock_irq(&priv->driver_lock);
  441. priv->resp_len[resp_idx] = 0;
  442. }
  443. spin_unlock_irq(&priv->driver_lock);
  444. /* Process hardware events, e.g. card removed, link lost */
  445. spin_lock_irq(&priv->driver_lock);
  446. while (kfifo_len(&priv->event_fifo)) {
  447. u32 event;
  448. if (kfifo_out(&priv->event_fifo,
  449. (unsigned char *) &event, sizeof(event)) !=
  450. sizeof(event))
  451. break;
  452. spin_unlock_irq(&priv->driver_lock);
  453. lbs_process_event(priv, event);
  454. spin_lock_irq(&priv->driver_lock);
  455. }
  456. spin_unlock_irq(&priv->driver_lock);
  457. if (priv->wakeup_dev_required) {
  458. lbs_deb_thread("Waking up device...\n");
  459. /* Wake up device */
  460. if (priv->exit_deep_sleep(priv))
  461. lbs_deb_thread("Wakeup device failed\n");
  462. continue;
  463. }
  464. /* command timeout stuff */
  465. if (priv->cmd_timed_out && priv->cur_cmd) {
  466. struct cmd_ctrl_node *cmdnode = priv->cur_cmd;
  467. netdev_info(dev, "Timeout submitting command 0x%04x\n",
  468. le16_to_cpu(cmdnode->cmdbuf->command));
  469. lbs_complete_command(priv, cmdnode, -ETIMEDOUT);
  470. /* Reset card, but only when it isn't in the process
  471. * of being shutdown anyway. */
  472. if (!dev->dismantle && priv->reset_card)
  473. priv->reset_card(priv);
  474. }
  475. priv->cmd_timed_out = 0;
  476. if (!priv->fw_ready)
  477. continue;
  478. /* Check if we need to confirm Sleep Request received previously */
  479. if (priv->psstate == PS_STATE_PRE_SLEEP &&
  480. !priv->dnld_sent && !priv->cur_cmd) {
  481. if (priv->connect_status == LBS_CONNECTED) {
  482. lbs_deb_thread("pre-sleep, currenttxskb %p, "
  483. "dnld_sent %d, cur_cmd %p\n",
  484. priv->currenttxskb, priv->dnld_sent,
  485. priv->cur_cmd);
  486. lbs_ps_confirm_sleep(priv);
  487. } else {
  488. /* workaround for firmware sending
  489. * deauth/linkloss event immediately
  490. * after sleep request; remove this
  491. * after firmware fixes it
  492. */
  493. priv->psstate = PS_STATE_AWAKE;
  494. netdev_alert(dev,
  495. "ignore PS_SleepConfirm in non-connected state\n");
  496. }
  497. }
  498. /* The PS state is changed during processing of Sleep Request
  499. * event above
  500. */
  501. if ((priv->psstate == PS_STATE_SLEEP) ||
  502. (priv->psstate == PS_STATE_PRE_SLEEP))
  503. continue;
  504. if (priv->is_deep_sleep)
  505. continue;
  506. /* Execute the next command */
  507. if (!priv->dnld_sent && !priv->cur_cmd)
  508. lbs_execute_next_command(priv);
  509. spin_lock_irq(&priv->driver_lock);
  510. if (!priv->dnld_sent && priv->tx_pending_len > 0) {
  511. int ret = priv->hw_host_to_card(priv, MVMS_DAT,
  512. priv->tx_pending_buf,
  513. priv->tx_pending_len);
  514. if (ret) {
  515. lbs_deb_tx("host_to_card failed %d\n", ret);
  516. priv->dnld_sent = DNLD_RES_RECEIVED;
  517. } else {
  518. mod_timer(&priv->tx_lockup_timer,
  519. jiffies + (HZ * 5));
  520. }
  521. priv->tx_pending_len = 0;
  522. if (!priv->currenttxskb) {
  523. /* We can wake the queues immediately if we aren't
  524. waiting for TX feedback */
  525. if (priv->connect_status == LBS_CONNECTED)
  526. netif_wake_queue(priv->dev);
  527. if (priv->mesh_dev &&
  528. netif_running(priv->mesh_dev))
  529. netif_wake_queue(priv->mesh_dev);
  530. }
  531. }
  532. spin_unlock_irq(&priv->driver_lock);
  533. }
  534. del_timer(&priv->command_timer);
  535. del_timer(&priv->tx_lockup_timer);
  536. del_timer(&priv->auto_deepsleep_timer);
  537. lbs_deb_leave(LBS_DEB_THREAD);
  538. return 0;
  539. }
  540. /**
  541. * lbs_setup_firmware - gets the HW spec from the firmware and sets
  542. * some basic parameters
  543. *
  544. * @priv: A pointer to &struct lbs_private structure
  545. * returns: 0 or -1
  546. */
  547. static int lbs_setup_firmware(struct lbs_private *priv)
  548. {
  549. int ret = -1;
  550. s16 curlevel = 0, minlevel = 0, maxlevel = 0;
  551. lbs_deb_enter(LBS_DEB_FW);
  552. /* Read MAC address from firmware */
  553. eth_broadcast_addr(priv->current_addr);
  554. ret = lbs_update_hw_spec(priv);
  555. if (ret)
  556. goto done;
  557. /* Read power levels if available */
  558. ret = lbs_get_tx_power(priv, &curlevel, &minlevel, &maxlevel);
  559. if (ret == 0) {
  560. priv->txpower_cur = curlevel;
  561. priv->txpower_min = minlevel;
  562. priv->txpower_max = maxlevel;
  563. }
  564. /* Send cmd to FW to enable 11D function */
  565. ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_11D_ENABLE, 1);
  566. if (ret)
  567. goto done;
  568. ret = lbs_set_mac_control_sync(priv);
  569. done:
  570. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  571. return ret;
  572. }
  573. int lbs_suspend(struct lbs_private *priv)
  574. {
  575. int ret;
  576. lbs_deb_enter(LBS_DEB_FW);
  577. if (priv->is_deep_sleep) {
  578. ret = lbs_set_deep_sleep(priv, 0);
  579. if (ret) {
  580. netdev_err(priv->dev,
  581. "deep sleep cancellation failed: %d\n", ret);
  582. return ret;
  583. }
  584. priv->deep_sleep_required = 1;
  585. }
  586. ret = lbs_set_host_sleep(priv, 1);
  587. netif_device_detach(priv->dev);
  588. if (priv->mesh_dev)
  589. netif_device_detach(priv->mesh_dev);
  590. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  591. return ret;
  592. }
  593. EXPORT_SYMBOL_GPL(lbs_suspend);
  594. int lbs_resume(struct lbs_private *priv)
  595. {
  596. int ret;
  597. lbs_deb_enter(LBS_DEB_FW);
  598. ret = lbs_set_host_sleep(priv, 0);
  599. netif_device_attach(priv->dev);
  600. if (priv->mesh_dev)
  601. netif_device_attach(priv->mesh_dev);
  602. if (priv->deep_sleep_required) {
  603. priv->deep_sleep_required = 0;
  604. ret = lbs_set_deep_sleep(priv, 1);
  605. if (ret)
  606. netdev_err(priv->dev,
  607. "deep sleep activation failed: %d\n", ret);
  608. }
  609. if (priv->setup_fw_on_resume)
  610. ret = lbs_setup_firmware(priv);
  611. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  612. return ret;
  613. }
  614. EXPORT_SYMBOL_GPL(lbs_resume);
  615. /**
  616. * lbs_cmd_timeout_handler - handles the timeout of command sending.
  617. * It will re-send the same command again.
  618. *
  619. * @data: &struct lbs_private pointer
  620. */
  621. static void lbs_cmd_timeout_handler(unsigned long data)
  622. {
  623. struct lbs_private *priv = (struct lbs_private *)data;
  624. unsigned long flags;
  625. lbs_deb_enter(LBS_DEB_CMD);
  626. spin_lock_irqsave(&priv->driver_lock, flags);
  627. if (!priv->cur_cmd)
  628. goto out;
  629. netdev_info(priv->dev, "command 0x%04x timed out\n",
  630. le16_to_cpu(priv->cur_cmd->cmdbuf->command));
  631. priv->cmd_timed_out = 1;
  632. /*
  633. * If the device didn't even acknowledge the command, reset the state
  634. * so that we don't block all future commands due to this one timeout.
  635. */
  636. if (priv->dnld_sent == DNLD_CMD_SENT)
  637. priv->dnld_sent = DNLD_RES_RECEIVED;
  638. wake_up(&priv->waitq);
  639. out:
  640. spin_unlock_irqrestore(&priv->driver_lock, flags);
  641. lbs_deb_leave(LBS_DEB_CMD);
  642. }
  643. /**
  644. * lbs_tx_lockup_handler - handles the timeout of the passing of TX frames
  645. * to the hardware. This is known to frequently happen with SD8686 when
  646. * waking up after a Wake-on-WLAN-triggered resume.
  647. *
  648. * @data: &struct lbs_private pointer
  649. */
  650. static void lbs_tx_lockup_handler(unsigned long data)
  651. {
  652. struct lbs_private *priv = (struct lbs_private *)data;
  653. unsigned long flags;
  654. lbs_deb_enter(LBS_DEB_TX);
  655. spin_lock_irqsave(&priv->driver_lock, flags);
  656. netdev_info(priv->dev, "TX lockup detected\n");
  657. if (priv->reset_card)
  658. priv->reset_card(priv);
  659. priv->dnld_sent = DNLD_RES_RECEIVED;
  660. wake_up_interruptible(&priv->waitq);
  661. spin_unlock_irqrestore(&priv->driver_lock, flags);
  662. lbs_deb_leave(LBS_DEB_TX);
  663. }
  664. /**
  665. * auto_deepsleep_timer_fn - put the device back to deep sleep mode when
  666. * timer expires and no activity (command, event, data etc.) is detected.
  667. * @data: &struct lbs_private pointer
  668. * returns: N/A
  669. */
  670. static void auto_deepsleep_timer_fn(unsigned long data)
  671. {
  672. struct lbs_private *priv = (struct lbs_private *)data;
  673. lbs_deb_enter(LBS_DEB_CMD);
  674. if (priv->is_activity_detected) {
  675. priv->is_activity_detected = 0;
  676. } else {
  677. if (priv->is_auto_deep_sleep_enabled &&
  678. (!priv->wakeup_dev_required) &&
  679. (priv->connect_status != LBS_CONNECTED)) {
  680. struct cmd_header cmd;
  681. lbs_deb_main("Entering auto deep sleep mode...\n");
  682. memset(&cmd, 0, sizeof(cmd));
  683. cmd.size = cpu_to_le16(sizeof(cmd));
  684. lbs_cmd_async(priv, CMD_802_11_DEEP_SLEEP, &cmd,
  685. sizeof(cmd));
  686. }
  687. }
  688. mod_timer(&priv->auto_deepsleep_timer , jiffies +
  689. (priv->auto_deep_sleep_timeout * HZ)/1000);
  690. lbs_deb_leave(LBS_DEB_CMD);
  691. }
  692. int lbs_enter_auto_deep_sleep(struct lbs_private *priv)
  693. {
  694. lbs_deb_enter(LBS_DEB_SDIO);
  695. priv->is_auto_deep_sleep_enabled = 1;
  696. if (priv->is_deep_sleep)
  697. priv->wakeup_dev_required = 1;
  698. mod_timer(&priv->auto_deepsleep_timer ,
  699. jiffies + (priv->auto_deep_sleep_timeout * HZ)/1000);
  700. lbs_deb_leave(LBS_DEB_SDIO);
  701. return 0;
  702. }
  703. int lbs_exit_auto_deep_sleep(struct lbs_private *priv)
  704. {
  705. lbs_deb_enter(LBS_DEB_SDIO);
  706. priv->is_auto_deep_sleep_enabled = 0;
  707. priv->auto_deep_sleep_timeout = 0;
  708. del_timer(&priv->auto_deepsleep_timer);
  709. lbs_deb_leave(LBS_DEB_SDIO);
  710. return 0;
  711. }
  712. static int lbs_init_adapter(struct lbs_private *priv)
  713. {
  714. int ret;
  715. lbs_deb_enter(LBS_DEB_MAIN);
  716. eth_broadcast_addr(priv->current_addr);
  717. priv->connect_status = LBS_DISCONNECTED;
  718. priv->channel = DEFAULT_AD_HOC_CHANNEL;
  719. priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
  720. priv->radio_on = 1;
  721. priv->psmode = LBS802_11POWERMODECAM;
  722. priv->psstate = PS_STATE_FULL_POWER;
  723. priv->is_deep_sleep = 0;
  724. priv->is_auto_deep_sleep_enabled = 0;
  725. priv->deep_sleep_required = 0;
  726. priv->wakeup_dev_required = 0;
  727. init_waitqueue_head(&priv->ds_awake_q);
  728. init_waitqueue_head(&priv->scan_q);
  729. priv->authtype_auto = 1;
  730. priv->is_host_sleep_configured = 0;
  731. priv->is_host_sleep_activated = 0;
  732. init_waitqueue_head(&priv->host_sleep_q);
  733. init_waitqueue_head(&priv->fw_waitq);
  734. mutex_init(&priv->lock);
  735. setup_timer(&priv->command_timer, lbs_cmd_timeout_handler,
  736. (unsigned long)priv);
  737. setup_timer(&priv->tx_lockup_timer, lbs_tx_lockup_handler,
  738. (unsigned long)priv);
  739. setup_timer(&priv->auto_deepsleep_timer, auto_deepsleep_timer_fn,
  740. (unsigned long)priv);
  741. INIT_LIST_HEAD(&priv->cmdfreeq);
  742. INIT_LIST_HEAD(&priv->cmdpendingq);
  743. spin_lock_init(&priv->driver_lock);
  744. /* Allocate the command buffers */
  745. if (lbs_allocate_cmd_buffer(priv)) {
  746. pr_err("Out of memory allocating command buffers\n");
  747. ret = -ENOMEM;
  748. goto out;
  749. }
  750. priv->resp_idx = 0;
  751. priv->resp_len[0] = priv->resp_len[1] = 0;
  752. /* Create the event FIFO */
  753. ret = kfifo_alloc(&priv->event_fifo, sizeof(u32) * 16, GFP_KERNEL);
  754. if (ret) {
  755. pr_err("Out of memory allocating event FIFO buffer\n");
  756. goto out;
  757. }
  758. out:
  759. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  760. return ret;
  761. }
  762. static void lbs_free_adapter(struct lbs_private *priv)
  763. {
  764. lbs_deb_enter(LBS_DEB_MAIN);
  765. lbs_free_cmd_buffer(priv);
  766. kfifo_free(&priv->event_fifo);
  767. del_timer(&priv->command_timer);
  768. del_timer(&priv->tx_lockup_timer);
  769. del_timer(&priv->auto_deepsleep_timer);
  770. lbs_deb_leave(LBS_DEB_MAIN);
  771. }
  772. static const struct net_device_ops lbs_netdev_ops = {
  773. .ndo_open = lbs_dev_open,
  774. .ndo_stop = lbs_eth_stop,
  775. .ndo_start_xmit = lbs_hard_start_xmit,
  776. .ndo_set_mac_address = lbs_set_mac_address,
  777. .ndo_set_rx_mode = lbs_set_multicast_list,
  778. .ndo_change_mtu = eth_change_mtu,
  779. .ndo_validate_addr = eth_validate_addr,
  780. };
  781. /**
  782. * lbs_add_card - adds the card. It will probe the
  783. * card, allocate the lbs_priv and initialize the device.
  784. *
  785. * @card: A pointer to card
  786. * @dmdev: A pointer to &struct device
  787. * returns: A pointer to &struct lbs_private structure
  788. */
  789. struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
  790. {
  791. struct net_device *dev;
  792. struct wireless_dev *wdev;
  793. struct lbs_private *priv = NULL;
  794. lbs_deb_enter(LBS_DEB_MAIN);
  795. /* Allocate an Ethernet device and register it */
  796. wdev = lbs_cfg_alloc(dmdev);
  797. if (IS_ERR(wdev)) {
  798. pr_err("cfg80211 init failed\n");
  799. goto done;
  800. }
  801. wdev->iftype = NL80211_IFTYPE_STATION;
  802. priv = wdev_priv(wdev);
  803. priv->wdev = wdev;
  804. if (lbs_init_adapter(priv)) {
  805. pr_err("failed to initialize adapter structure\n");
  806. goto err_wdev;
  807. }
  808. dev = alloc_netdev(0, "wlan%d", NET_NAME_UNKNOWN, ether_setup);
  809. if (!dev) {
  810. dev_err(dmdev, "no memory for network device instance\n");
  811. goto err_adapter;
  812. }
  813. dev->ieee80211_ptr = wdev;
  814. dev->ml_priv = priv;
  815. SET_NETDEV_DEV(dev, dmdev);
  816. wdev->netdev = dev;
  817. priv->dev = dev;
  818. dev->netdev_ops = &lbs_netdev_ops;
  819. dev->watchdog_timeo = 5 * HZ;
  820. dev->ethtool_ops = &lbs_ethtool_ops;
  821. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  822. priv->card = card;
  823. strcpy(dev->name, "wlan%d");
  824. lbs_deb_thread("Starting main thread...\n");
  825. init_waitqueue_head(&priv->waitq);
  826. priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
  827. if (IS_ERR(priv->main_thread)) {
  828. lbs_deb_thread("Error creating main thread.\n");
  829. goto err_ndev;
  830. }
  831. priv->work_thread = create_singlethread_workqueue("lbs_worker");
  832. INIT_WORK(&priv->mcast_work, lbs_set_mcast_worker);
  833. priv->wol_criteria = EHS_REMOVE_WAKEUP;
  834. priv->wol_gpio = 0xff;
  835. priv->wol_gap = 20;
  836. priv->ehs_remove_supported = true;
  837. goto done;
  838. err_ndev:
  839. free_netdev(dev);
  840. err_adapter:
  841. lbs_free_adapter(priv);
  842. err_wdev:
  843. lbs_cfg_free(priv);
  844. priv = NULL;
  845. done:
  846. lbs_deb_leave_args(LBS_DEB_MAIN, "priv %p", priv);
  847. return priv;
  848. }
  849. EXPORT_SYMBOL_GPL(lbs_add_card);
  850. void lbs_remove_card(struct lbs_private *priv)
  851. {
  852. struct net_device *dev = priv->dev;
  853. lbs_deb_enter(LBS_DEB_MAIN);
  854. lbs_remove_mesh(priv);
  855. if (priv->wiphy_registered)
  856. lbs_scan_deinit(priv);
  857. lbs_wait_for_firmware_load(priv);
  858. /* worker thread destruction blocks on the in-flight command which
  859. * should have been cleared already in lbs_stop_card().
  860. */
  861. lbs_deb_main("destroying worker thread\n");
  862. destroy_workqueue(priv->work_thread);
  863. lbs_deb_main("done destroying worker thread\n");
  864. if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
  865. priv->psmode = LBS802_11POWERMODECAM;
  866. lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS, true);
  867. }
  868. if (priv->is_deep_sleep) {
  869. priv->is_deep_sleep = 0;
  870. wake_up_interruptible(&priv->ds_awake_q);
  871. }
  872. priv->is_host_sleep_configured = 0;
  873. priv->is_host_sleep_activated = 0;
  874. wake_up_interruptible(&priv->host_sleep_q);
  875. /* Stop the thread servicing the interrupts */
  876. priv->surpriseremoved = 1;
  877. kthread_stop(priv->main_thread);
  878. lbs_free_adapter(priv);
  879. lbs_cfg_free(priv);
  880. free_netdev(dev);
  881. lbs_deb_leave(LBS_DEB_MAIN);
  882. }
  883. EXPORT_SYMBOL_GPL(lbs_remove_card);
  884. int lbs_rtap_supported(struct lbs_private *priv)
  885. {
  886. if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V5)
  887. return 1;
  888. /* newer firmware use a capability mask */
  889. return ((MRVL_FW_MAJOR_REV(priv->fwrelease) >= MRVL_FW_V10) &&
  890. (priv->fwcapinfo & MESH_CAPINFO_ENABLE_MASK));
  891. }
  892. int lbs_start_card(struct lbs_private *priv)
  893. {
  894. struct net_device *dev = priv->dev;
  895. int ret = -1;
  896. lbs_deb_enter(LBS_DEB_MAIN);
  897. /* poke the firmware */
  898. ret = lbs_setup_firmware(priv);
  899. if (ret)
  900. goto done;
  901. if (!lbs_disablemesh)
  902. lbs_init_mesh(priv);
  903. else
  904. pr_info("%s: mesh disabled\n", dev->name);
  905. if (lbs_cfg_register(priv)) {
  906. pr_err("cannot register device\n");
  907. goto done;
  908. }
  909. if (lbs_mesh_activated(priv))
  910. lbs_start_mesh(priv);
  911. lbs_debugfs_init_one(priv, dev);
  912. netdev_info(dev, "Marvell WLAN 802.11 adapter\n");
  913. ret = 0;
  914. done:
  915. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  916. return ret;
  917. }
  918. EXPORT_SYMBOL_GPL(lbs_start_card);
  919. void lbs_stop_card(struct lbs_private *priv)
  920. {
  921. struct net_device *dev;
  922. lbs_deb_enter(LBS_DEB_MAIN);
  923. if (!priv)
  924. goto out;
  925. dev = priv->dev;
  926. /* If the netdev isn't registered, it means that lbs_start_card() was
  927. * never called so we have nothing to do here. */
  928. if (dev->reg_state != NETREG_REGISTERED)
  929. goto out;
  930. netif_stop_queue(dev);
  931. netif_carrier_off(dev);
  932. lbs_debugfs_remove_one(priv);
  933. lbs_deinit_mesh(priv);
  934. unregister_netdev(dev);
  935. out:
  936. lbs_deb_leave(LBS_DEB_MAIN);
  937. }
  938. EXPORT_SYMBOL_GPL(lbs_stop_card);
  939. void lbs_queue_event(struct lbs_private *priv, u32 event)
  940. {
  941. unsigned long flags;
  942. lbs_deb_enter(LBS_DEB_THREAD);
  943. spin_lock_irqsave(&priv->driver_lock, flags);
  944. if (priv->psstate == PS_STATE_SLEEP)
  945. priv->psstate = PS_STATE_AWAKE;
  946. kfifo_in(&priv->event_fifo, (unsigned char *) &event, sizeof(u32));
  947. wake_up(&priv->waitq);
  948. spin_unlock_irqrestore(&priv->driver_lock, flags);
  949. lbs_deb_leave(LBS_DEB_THREAD);
  950. }
  951. EXPORT_SYMBOL_GPL(lbs_queue_event);
  952. void lbs_notify_command_response(struct lbs_private *priv, u8 resp_idx)
  953. {
  954. lbs_deb_enter(LBS_DEB_THREAD);
  955. if (priv->psstate == PS_STATE_SLEEP)
  956. priv->psstate = PS_STATE_AWAKE;
  957. /* Swap buffers by flipping the response index */
  958. BUG_ON(resp_idx > 1);
  959. priv->resp_idx = resp_idx;
  960. wake_up(&priv->waitq);
  961. lbs_deb_leave(LBS_DEB_THREAD);
  962. }
  963. EXPORT_SYMBOL_GPL(lbs_notify_command_response);
  964. static int __init lbs_init_module(void)
  965. {
  966. lbs_deb_enter(LBS_DEB_MAIN);
  967. memset(&confirm_sleep, 0, sizeof(confirm_sleep));
  968. confirm_sleep.hdr.command = cpu_to_le16(CMD_802_11_PS_MODE);
  969. confirm_sleep.hdr.size = cpu_to_le16(sizeof(confirm_sleep));
  970. confirm_sleep.action = cpu_to_le16(PS_MODE_ACTION_SLEEP_CONFIRMED);
  971. lbs_debugfs_init();
  972. lbs_deb_leave(LBS_DEB_MAIN);
  973. return 0;
  974. }
  975. static void __exit lbs_exit_module(void)
  976. {
  977. lbs_deb_enter(LBS_DEB_MAIN);
  978. lbs_debugfs_remove();
  979. lbs_deb_leave(LBS_DEB_MAIN);
  980. }
  981. module_init(lbs_init_module);
  982. module_exit(lbs_exit_module);
  983. MODULE_DESCRIPTION("Libertas WLAN Driver Library");
  984. MODULE_AUTHOR("Marvell International Ltd.");
  985. MODULE_LICENSE("GPL");