main.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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. #include <linux/moduleparam.h>
  7. #include <linux/delay.h>
  8. #include <linux/etherdevice.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/if_arp.h>
  11. #include <linux/kthread.h>
  12. #include <linux/kfifo.h>
  13. #include <linux/slab.h>
  14. #include <net/cfg80211.h>
  15. #include "host.h"
  16. #include "decl.h"
  17. #include "dev.h"
  18. #include "cfg.h"
  19. #include "debugfs.h"
  20. #include "cmd.h"
  21. #define DRIVER_RELEASE_VERSION "323.p0"
  22. const char lbs_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
  23. #ifdef DEBUG
  24. "-dbg"
  25. #endif
  26. "";
  27. /* Module parameters */
  28. unsigned int lbs_debug;
  29. EXPORT_SYMBOL_GPL(lbs_debug);
  30. module_param_named(libertas_debug, lbs_debug, int, 0644);
  31. /* This global structure is used to send the confirm_sleep command as
  32. * fast as possible down to the firmware. */
  33. struct cmd_confirm_sleep confirm_sleep;
  34. /**
  35. * the table to keep region code
  36. */
  37. u16 lbs_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
  38. { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
  39. /**
  40. * FW rate table. FW refers to rates by their index in this table, not by the
  41. * rate value itself. Values of 0x00 are
  42. * reserved positions.
  43. */
  44. static u8 fw_data_rates[MAX_RATES] =
  45. { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12,
  46. 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00
  47. };
  48. /**
  49. * @brief use index to get the data rate
  50. *
  51. * @param idx The index of data rate
  52. * @return data rate or 0
  53. */
  54. u32 lbs_fw_index_to_data_rate(u8 idx)
  55. {
  56. if (idx >= sizeof(fw_data_rates))
  57. idx = 0;
  58. return fw_data_rates[idx];
  59. }
  60. /**
  61. * @brief use rate to get the index
  62. *
  63. * @param rate data rate
  64. * @return index or 0
  65. */
  66. u8 lbs_data_rate_to_fw_index(u32 rate)
  67. {
  68. u8 i;
  69. if (!rate)
  70. return 0;
  71. for (i = 0; i < sizeof(fw_data_rates); i++) {
  72. if (rate == fw_data_rates[i])
  73. return i;
  74. }
  75. return 0;
  76. }
  77. /**
  78. * @brief This function opens the ethX interface
  79. *
  80. * @param dev A pointer to net_device structure
  81. * @return 0 or -EBUSY if monitor mode active
  82. */
  83. static int lbs_dev_open(struct net_device *dev)
  84. {
  85. struct lbs_private *priv = dev->ml_priv;
  86. int ret = 0;
  87. lbs_deb_enter(LBS_DEB_NET);
  88. spin_lock_irq(&priv->driver_lock);
  89. if (priv->connect_status == LBS_CONNECTED)
  90. netif_carrier_on(dev);
  91. else
  92. netif_carrier_off(dev);
  93. if (!priv->tx_pending_len)
  94. netif_wake_queue(dev);
  95. spin_unlock_irq(&priv->driver_lock);
  96. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  97. return ret;
  98. }
  99. /**
  100. * @brief This function closes the ethX interface
  101. *
  102. * @param dev A pointer to net_device structure
  103. * @return 0
  104. */
  105. static int lbs_eth_stop(struct net_device *dev)
  106. {
  107. struct lbs_private *priv = dev->ml_priv;
  108. lbs_deb_enter(LBS_DEB_NET);
  109. spin_lock_irq(&priv->driver_lock);
  110. netif_stop_queue(dev);
  111. spin_unlock_irq(&priv->driver_lock);
  112. schedule_work(&priv->mcast_work);
  113. lbs_deb_leave(LBS_DEB_NET);
  114. return 0;
  115. }
  116. static void lbs_tx_timeout(struct net_device *dev)
  117. {
  118. struct lbs_private *priv = dev->ml_priv;
  119. lbs_deb_enter(LBS_DEB_TX);
  120. lbs_pr_err("tx watch dog timeout\n");
  121. dev->trans_start = jiffies; /* prevent tx timeout */
  122. if (priv->currenttxskb)
  123. lbs_send_tx_feedback(priv, 0);
  124. /* XX: Shouldn't we also call into the hw-specific driver
  125. to kick it somehow? */
  126. lbs_host_to_card_done(priv);
  127. /* FIXME: reset the card */
  128. lbs_deb_leave(LBS_DEB_TX);
  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. * @brief This function 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. * @param data A pointer to lbs_thread structure
  268. * @return 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. lbs_pr_info("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. lbs_pr_alert("ignore PS_SleepConfirm in "
  396. "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. /* Wake-up command waiters which can't sleep in
  411. * lbs_prepare_and_send_command
  412. */
  413. if (!list_empty(&priv->cmdpendingq))
  414. wake_up_all(&priv->cmd_pending);
  415. spin_lock_irq(&priv->driver_lock);
  416. if (!priv->dnld_sent && priv->tx_pending_len > 0) {
  417. int ret = priv->hw_host_to_card(priv, MVMS_DAT,
  418. priv->tx_pending_buf,
  419. priv->tx_pending_len);
  420. if (ret) {
  421. lbs_deb_tx("host_to_card failed %d\n", ret);
  422. priv->dnld_sent = DNLD_RES_RECEIVED;
  423. }
  424. priv->tx_pending_len = 0;
  425. if (!priv->currenttxskb) {
  426. /* We can wake the queues immediately if we aren't
  427. waiting for TX feedback */
  428. if (priv->connect_status == LBS_CONNECTED)
  429. netif_wake_queue(priv->dev);
  430. if (priv->mesh_dev &&
  431. lbs_mesh_connected(priv))
  432. netif_wake_queue(priv->mesh_dev);
  433. }
  434. }
  435. spin_unlock_irq(&priv->driver_lock);
  436. }
  437. del_timer(&priv->command_timer);
  438. del_timer(&priv->auto_deepsleep_timer);
  439. wake_up_all(&priv->cmd_pending);
  440. lbs_deb_leave(LBS_DEB_THREAD);
  441. return 0;
  442. }
  443. int lbs_suspend(struct lbs_private *priv)
  444. {
  445. int ret;
  446. lbs_deb_enter(LBS_DEB_FW);
  447. if (priv->is_deep_sleep) {
  448. ret = lbs_set_deep_sleep(priv, 0);
  449. if (ret) {
  450. lbs_pr_err("deep sleep cancellation failed: %d\n", ret);
  451. return ret;
  452. }
  453. priv->deep_sleep_required = 1;
  454. }
  455. ret = lbs_set_host_sleep(priv, 1);
  456. netif_device_detach(priv->dev);
  457. if (priv->mesh_dev)
  458. netif_device_detach(priv->mesh_dev);
  459. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  460. return ret;
  461. }
  462. EXPORT_SYMBOL_GPL(lbs_suspend);
  463. int lbs_resume(struct lbs_private *priv)
  464. {
  465. int ret;
  466. lbs_deb_enter(LBS_DEB_FW);
  467. ret = lbs_set_host_sleep(priv, 0);
  468. netif_device_attach(priv->dev);
  469. if (priv->mesh_dev)
  470. netif_device_attach(priv->mesh_dev);
  471. if (priv->deep_sleep_required) {
  472. priv->deep_sleep_required = 0;
  473. ret = lbs_set_deep_sleep(priv, 1);
  474. if (ret)
  475. lbs_pr_err("deep sleep activation failed: %d\n", ret);
  476. }
  477. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  478. return ret;
  479. }
  480. EXPORT_SYMBOL_GPL(lbs_resume);
  481. /**
  482. * @brief This function gets the HW spec from the firmware and sets
  483. * some basic parameters.
  484. *
  485. * @param priv A pointer to struct lbs_private structure
  486. * @return 0 or -1
  487. */
  488. static int lbs_setup_firmware(struct lbs_private *priv)
  489. {
  490. int ret = -1;
  491. s16 curlevel = 0, minlevel = 0, maxlevel = 0;
  492. lbs_deb_enter(LBS_DEB_FW);
  493. /* Read MAC address from firmware */
  494. memset(priv->current_addr, 0xff, ETH_ALEN);
  495. ret = lbs_update_hw_spec(priv);
  496. if (ret)
  497. goto done;
  498. /* Read power levels if available */
  499. ret = lbs_get_tx_power(priv, &curlevel, &minlevel, &maxlevel);
  500. if (ret == 0) {
  501. priv->txpower_cur = curlevel;
  502. priv->txpower_min = minlevel;
  503. priv->txpower_max = maxlevel;
  504. }
  505. /* Send cmd to FW to enable 11D function */
  506. ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_11D_ENABLE, 1);
  507. lbs_set_mac_control(priv);
  508. done:
  509. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  510. return ret;
  511. }
  512. /**
  513. * This function handles the timeout of command sending.
  514. * It will re-send the same command again.
  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. lbs_pr_info("command 0x%04x timed out\n",
  525. le16_to_cpu(priv->cur_cmd->cmdbuf->command));
  526. priv->cmd_timed_out = 1;
  527. wake_up_interruptible(&priv->waitq);
  528. out:
  529. spin_unlock_irqrestore(&priv->driver_lock, flags);
  530. lbs_deb_leave(LBS_DEB_CMD);
  531. }
  532. /**
  533. * This function put the device back to deep sleep mode when timer expires
  534. * and no activity (command, event, data etc.) is detected.
  535. */
  536. static void auto_deepsleep_timer_fn(unsigned long data)
  537. {
  538. struct lbs_private *priv = (struct lbs_private *)data;
  539. lbs_deb_enter(LBS_DEB_CMD);
  540. if (priv->is_activity_detected) {
  541. priv->is_activity_detected = 0;
  542. } else {
  543. if (priv->is_auto_deep_sleep_enabled &&
  544. (!priv->wakeup_dev_required) &&
  545. (priv->connect_status != LBS_CONNECTED)) {
  546. struct cmd_header cmd;
  547. lbs_deb_main("Entering auto deep sleep mode...\n");
  548. memset(&cmd, 0, sizeof(cmd));
  549. cmd.size = cpu_to_le16(sizeof(cmd));
  550. lbs_cmd_async(priv, CMD_802_11_DEEP_SLEEP, &cmd,
  551. sizeof(cmd));
  552. }
  553. }
  554. mod_timer(&priv->auto_deepsleep_timer , jiffies +
  555. (priv->auto_deep_sleep_timeout * HZ)/1000);
  556. lbs_deb_leave(LBS_DEB_CMD);
  557. }
  558. int lbs_enter_auto_deep_sleep(struct lbs_private *priv)
  559. {
  560. lbs_deb_enter(LBS_DEB_SDIO);
  561. priv->is_auto_deep_sleep_enabled = 1;
  562. if (priv->is_deep_sleep)
  563. priv->wakeup_dev_required = 1;
  564. mod_timer(&priv->auto_deepsleep_timer ,
  565. jiffies + (priv->auto_deep_sleep_timeout * HZ)/1000);
  566. lbs_deb_leave(LBS_DEB_SDIO);
  567. return 0;
  568. }
  569. int lbs_exit_auto_deep_sleep(struct lbs_private *priv)
  570. {
  571. lbs_deb_enter(LBS_DEB_SDIO);
  572. priv->is_auto_deep_sleep_enabled = 0;
  573. priv->auto_deep_sleep_timeout = 0;
  574. del_timer(&priv->auto_deepsleep_timer);
  575. lbs_deb_leave(LBS_DEB_SDIO);
  576. return 0;
  577. }
  578. static int lbs_init_adapter(struct lbs_private *priv)
  579. {
  580. int ret;
  581. lbs_deb_enter(LBS_DEB_MAIN);
  582. memset(priv->current_addr, 0xff, ETH_ALEN);
  583. priv->connect_status = LBS_DISCONNECTED;
  584. priv->channel = DEFAULT_AD_HOC_CHANNEL;
  585. priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
  586. priv->radio_on = 1;
  587. priv->psmode = LBS802_11POWERMODECAM;
  588. priv->psstate = PS_STATE_FULL_POWER;
  589. priv->is_deep_sleep = 0;
  590. priv->is_auto_deep_sleep_enabled = 0;
  591. priv->deep_sleep_required = 0;
  592. priv->wakeup_dev_required = 0;
  593. init_waitqueue_head(&priv->ds_awake_q);
  594. priv->authtype_auto = 1;
  595. priv->is_host_sleep_configured = 0;
  596. priv->is_host_sleep_activated = 0;
  597. init_waitqueue_head(&priv->host_sleep_q);
  598. mutex_init(&priv->lock);
  599. setup_timer(&priv->command_timer, lbs_cmd_timeout_handler,
  600. (unsigned long)priv);
  601. setup_timer(&priv->auto_deepsleep_timer, auto_deepsleep_timer_fn,
  602. (unsigned long)priv);
  603. INIT_LIST_HEAD(&priv->cmdfreeq);
  604. INIT_LIST_HEAD(&priv->cmdpendingq);
  605. spin_lock_init(&priv->driver_lock);
  606. init_waitqueue_head(&priv->cmd_pending);
  607. /* Allocate the command buffers */
  608. if (lbs_allocate_cmd_buffer(priv)) {
  609. lbs_pr_err("Out of memory allocating command buffers\n");
  610. ret = -ENOMEM;
  611. goto out;
  612. }
  613. priv->resp_idx = 0;
  614. priv->resp_len[0] = priv->resp_len[1] = 0;
  615. /* Create the event FIFO */
  616. ret = kfifo_alloc(&priv->event_fifo, sizeof(u32) * 16, GFP_KERNEL);
  617. if (ret) {
  618. lbs_pr_err("Out of memory allocating event FIFO buffer\n");
  619. goto out;
  620. }
  621. out:
  622. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  623. return ret;
  624. }
  625. static void lbs_free_adapter(struct lbs_private *priv)
  626. {
  627. lbs_deb_enter(LBS_DEB_MAIN);
  628. lbs_free_cmd_buffer(priv);
  629. kfifo_free(&priv->event_fifo);
  630. del_timer(&priv->command_timer);
  631. del_timer(&priv->auto_deepsleep_timer);
  632. lbs_deb_leave(LBS_DEB_MAIN);
  633. }
  634. static const struct net_device_ops lbs_netdev_ops = {
  635. .ndo_open = lbs_dev_open,
  636. .ndo_stop = lbs_eth_stop,
  637. .ndo_start_xmit = lbs_hard_start_xmit,
  638. .ndo_set_mac_address = lbs_set_mac_address,
  639. .ndo_tx_timeout = lbs_tx_timeout,
  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. * @brief This function adds the card. it will probe the
  646. * card, allocate the lbs_priv and initialize the device.
  647. *
  648. * @param card A pointer to card
  649. * @return A pointer to struct lbs_private structure
  650. */
  651. struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
  652. {
  653. struct net_device *dev;
  654. struct wireless_dev *wdev;
  655. struct lbs_private *priv = NULL;
  656. lbs_deb_enter(LBS_DEB_MAIN);
  657. /* Allocate an Ethernet device and register it */
  658. wdev = lbs_cfg_alloc(dmdev);
  659. if (IS_ERR(wdev)) {
  660. lbs_pr_err("cfg80211 init failed\n");
  661. goto done;
  662. }
  663. wdev->iftype = NL80211_IFTYPE_STATION;
  664. priv = wdev_priv(wdev);
  665. priv->wdev = wdev;
  666. if (lbs_init_adapter(priv)) {
  667. lbs_pr_err("failed to initialize adapter structure.\n");
  668. goto err_wdev;
  669. }
  670. dev = alloc_netdev(0, "wlan%d", ether_setup);
  671. if (!dev) {
  672. dev_err(dmdev, "no memory for network device instance\n");
  673. goto err_adapter;
  674. }
  675. dev->ieee80211_ptr = wdev;
  676. dev->ml_priv = priv;
  677. SET_NETDEV_DEV(dev, dmdev);
  678. wdev->netdev = dev;
  679. priv->dev = dev;
  680. dev->netdev_ops = &lbs_netdev_ops;
  681. dev->watchdog_timeo = 5 * HZ;
  682. dev->ethtool_ops = &lbs_ethtool_ops;
  683. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  684. priv->card = card;
  685. strcpy(dev->name, "wlan%d");
  686. lbs_deb_thread("Starting main thread...\n");
  687. init_waitqueue_head(&priv->waitq);
  688. priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
  689. if (IS_ERR(priv->main_thread)) {
  690. lbs_deb_thread("Error creating main thread.\n");
  691. goto err_ndev;
  692. }
  693. priv->work_thread = create_singlethread_workqueue("lbs_worker");
  694. INIT_WORK(&priv->mcast_work, lbs_set_mcast_worker);
  695. priv->wol_criteria = 0xffffffff;
  696. priv->wol_gpio = 0xff;
  697. priv->wol_gap = 20;
  698. goto done;
  699. err_ndev:
  700. free_netdev(dev);
  701. err_adapter:
  702. lbs_free_adapter(priv);
  703. err_wdev:
  704. lbs_cfg_free(priv);
  705. priv = NULL;
  706. done:
  707. lbs_deb_leave_args(LBS_DEB_MAIN, "priv %p", priv);
  708. return priv;
  709. }
  710. EXPORT_SYMBOL_GPL(lbs_add_card);
  711. void lbs_remove_card(struct lbs_private *priv)
  712. {
  713. struct net_device *dev = priv->dev;
  714. lbs_deb_enter(LBS_DEB_MAIN);
  715. lbs_remove_mesh(priv);
  716. lbs_scan_deinit(priv);
  717. dev = priv->dev;
  718. cancel_work_sync(&priv->mcast_work);
  719. /* worker thread destruction blocks on the in-flight command which
  720. * should have been cleared already in lbs_stop_card().
  721. */
  722. lbs_deb_main("destroying worker thread\n");
  723. destroy_workqueue(priv->work_thread);
  724. lbs_deb_main("done destroying worker thread\n");
  725. if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
  726. priv->psmode = LBS802_11POWERMODECAM;
  727. lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS, true);
  728. }
  729. if (priv->is_deep_sleep) {
  730. priv->is_deep_sleep = 0;
  731. wake_up_interruptible(&priv->ds_awake_q);
  732. }
  733. priv->is_host_sleep_configured = 0;
  734. priv->is_host_sleep_activated = 0;
  735. wake_up_interruptible(&priv->host_sleep_q);
  736. /* Stop the thread servicing the interrupts */
  737. priv->surpriseremoved = 1;
  738. kthread_stop(priv->main_thread);
  739. lbs_free_adapter(priv);
  740. lbs_cfg_free(priv);
  741. priv->dev = NULL;
  742. free_netdev(dev);
  743. lbs_deb_leave(LBS_DEB_MAIN);
  744. }
  745. EXPORT_SYMBOL_GPL(lbs_remove_card);
  746. int lbs_rtap_supported(struct lbs_private *priv)
  747. {
  748. if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V5)
  749. return 1;
  750. /* newer firmware use a capability mask */
  751. return ((MRVL_FW_MAJOR_REV(priv->fwrelease) >= MRVL_FW_V10) &&
  752. (priv->fwcapinfo & MESH_CAPINFO_ENABLE_MASK));
  753. }
  754. int lbs_start_card(struct lbs_private *priv)
  755. {
  756. struct net_device *dev = priv->dev;
  757. int ret = -1;
  758. lbs_deb_enter(LBS_DEB_MAIN);
  759. /* poke the firmware */
  760. ret = lbs_setup_firmware(priv);
  761. if (ret)
  762. goto done;
  763. if (lbs_cfg_register(priv)) {
  764. lbs_pr_err("cannot register device\n");
  765. goto done;
  766. }
  767. lbs_update_channel(priv);
  768. lbs_init_mesh(priv);
  769. lbs_debugfs_init_one(priv, dev);
  770. lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
  771. ret = 0;
  772. done:
  773. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  774. return ret;
  775. }
  776. EXPORT_SYMBOL_GPL(lbs_start_card);
  777. void lbs_stop_card(struct lbs_private *priv)
  778. {
  779. struct net_device *dev;
  780. struct cmd_ctrl_node *cmdnode;
  781. unsigned long flags;
  782. lbs_deb_enter(LBS_DEB_MAIN);
  783. if (!priv)
  784. goto out;
  785. dev = priv->dev;
  786. netif_stop_queue(dev);
  787. netif_carrier_off(dev);
  788. lbs_debugfs_remove_one(priv);
  789. lbs_deinit_mesh(priv);
  790. /* Delete the timeout of the currently processing command */
  791. del_timer_sync(&priv->command_timer);
  792. del_timer_sync(&priv->auto_deepsleep_timer);
  793. /* Flush pending command nodes */
  794. spin_lock_irqsave(&priv->driver_lock, flags);
  795. lbs_deb_main("clearing pending commands\n");
  796. list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
  797. cmdnode->result = -ENOENT;
  798. cmdnode->cmdwaitqwoken = 1;
  799. wake_up_interruptible(&cmdnode->cmdwait_q);
  800. }
  801. /* Flush the command the card is currently processing */
  802. if (priv->cur_cmd) {
  803. lbs_deb_main("clearing current command\n");
  804. priv->cur_cmd->result = -ENOENT;
  805. priv->cur_cmd->cmdwaitqwoken = 1;
  806. wake_up_interruptible(&priv->cur_cmd->cmdwait_q);
  807. }
  808. lbs_deb_main("done clearing commands\n");
  809. spin_unlock_irqrestore(&priv->driver_lock, flags);
  810. unregister_netdev(dev);
  811. out:
  812. lbs_deb_leave(LBS_DEB_MAIN);
  813. }
  814. EXPORT_SYMBOL_GPL(lbs_stop_card);
  815. void lbs_queue_event(struct lbs_private *priv, u32 event)
  816. {
  817. unsigned long flags;
  818. lbs_deb_enter(LBS_DEB_THREAD);
  819. spin_lock_irqsave(&priv->driver_lock, flags);
  820. if (priv->psstate == PS_STATE_SLEEP)
  821. priv->psstate = PS_STATE_AWAKE;
  822. kfifo_in(&priv->event_fifo, (unsigned char *) &event, sizeof(u32));
  823. wake_up_interruptible(&priv->waitq);
  824. spin_unlock_irqrestore(&priv->driver_lock, flags);
  825. lbs_deb_leave(LBS_DEB_THREAD);
  826. }
  827. EXPORT_SYMBOL_GPL(lbs_queue_event);
  828. void lbs_notify_command_response(struct lbs_private *priv, u8 resp_idx)
  829. {
  830. lbs_deb_enter(LBS_DEB_THREAD);
  831. if (priv->psstate == PS_STATE_SLEEP)
  832. priv->psstate = PS_STATE_AWAKE;
  833. /* Swap buffers by flipping the response index */
  834. BUG_ON(resp_idx > 1);
  835. priv->resp_idx = resp_idx;
  836. wake_up_interruptible(&priv->waitq);
  837. lbs_deb_leave(LBS_DEB_THREAD);
  838. }
  839. EXPORT_SYMBOL_GPL(lbs_notify_command_response);
  840. static int __init lbs_init_module(void)
  841. {
  842. lbs_deb_enter(LBS_DEB_MAIN);
  843. memset(&confirm_sleep, 0, sizeof(confirm_sleep));
  844. confirm_sleep.hdr.command = cpu_to_le16(CMD_802_11_PS_MODE);
  845. confirm_sleep.hdr.size = cpu_to_le16(sizeof(confirm_sleep));
  846. confirm_sleep.action = cpu_to_le16(PS_MODE_ACTION_SLEEP_CONFIRMED);
  847. lbs_debugfs_init();
  848. lbs_deb_leave(LBS_DEB_MAIN);
  849. return 0;
  850. }
  851. static void __exit lbs_exit_module(void)
  852. {
  853. lbs_deb_enter(LBS_DEB_MAIN);
  854. lbs_debugfs_remove();
  855. lbs_deb_leave(LBS_DEB_MAIN);
  856. }
  857. module_init(lbs_init_module);
  858. module_exit(lbs_exit_module);
  859. MODULE_DESCRIPTION("Libertas WLAN Driver Library");
  860. MODULE_AUTHOR("Marvell International Ltd.");
  861. MODULE_LICENSE("GPL");