main.c 29 KB

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