main.c 28 KB

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