main.c 28 KB

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