main.c 28 KB

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