PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/net/wireless/libertas/main.c

https://bitbucket.org/sola/android_board_beagleboard_kernel
C | 1184 lines | 816 code | 230 blank | 138 comment | 146 complexity | b0efed402edbeb2ffab4ec55a22fca90 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  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. priv->stopping = false;
  90. if (priv->connect_status == LBS_CONNECTED)
  91. netif_carrier_on(dev);
  92. else
  93. netif_carrier_off(dev);
  94. if (!priv->tx_pending_len)
  95. netif_wake_queue(dev);
  96. spin_unlock_irq(&priv->driver_lock);
  97. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  98. return ret;
  99. }
  100. /**
  101. * @brief This function closes the ethX interface
  102. *
  103. * @param dev A pointer to net_device structure
  104. * @return 0
  105. */
  106. static int lbs_eth_stop(struct net_device *dev)
  107. {
  108. struct lbs_private *priv = dev->ml_priv;
  109. lbs_deb_enter(LBS_DEB_NET);
  110. spin_lock_irq(&priv->driver_lock);
  111. priv->stopping = true;
  112. netif_stop_queue(dev);
  113. spin_unlock_irq(&priv->driver_lock);
  114. schedule_work(&priv->mcast_work);
  115. cancel_delayed_work_sync(&priv->scan_work);
  116. if (priv->scan_req) {
  117. cfg80211_scan_done(priv->scan_req, false);
  118. priv->scan_req = NULL;
  119. }
  120. lbs_deb_leave(LBS_DEB_NET);
  121. return 0;
  122. }
  123. static void lbs_tx_timeout(struct net_device *dev)
  124. {
  125. struct lbs_private *priv = dev->ml_priv;
  126. lbs_deb_enter(LBS_DEB_TX);
  127. lbs_pr_err("tx watch dog timeout\n");
  128. dev->trans_start = jiffies; /* prevent tx timeout */
  129. if (priv->currenttxskb)
  130. lbs_send_tx_feedback(priv, 0);
  131. /* XX: Shouldn't we also call into the hw-specific driver
  132. to kick it somehow? */
  133. lbs_host_to_card_done(priv);
  134. /* FIXME: reset the card */
  135. lbs_deb_leave(LBS_DEB_TX);
  136. }
  137. void lbs_host_to_card_done(struct lbs_private *priv)
  138. {
  139. unsigned long flags;
  140. lbs_deb_enter(LBS_DEB_THREAD);
  141. spin_lock_irqsave(&priv->driver_lock, flags);
  142. priv->dnld_sent = DNLD_RES_RECEIVED;
  143. /* Wake main thread if commands are pending */
  144. if (!priv->cur_cmd || priv->tx_pending_len > 0) {
  145. if (!priv->wakeup_dev_required)
  146. wake_up_interruptible(&priv->waitq);
  147. }
  148. spin_unlock_irqrestore(&priv->driver_lock, flags);
  149. lbs_deb_leave(LBS_DEB_THREAD);
  150. }
  151. EXPORT_SYMBOL_GPL(lbs_host_to_card_done);
  152. int lbs_set_mac_address(struct net_device *dev, void *addr)
  153. {
  154. int ret = 0;
  155. struct lbs_private *priv = dev->ml_priv;
  156. struct sockaddr *phwaddr = addr;
  157. struct cmd_ds_802_11_mac_address cmd;
  158. lbs_deb_enter(LBS_DEB_NET);
  159. /* In case it was called from the mesh device */
  160. dev = priv->dev;
  161. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  162. cmd.action = cpu_to_le16(CMD_ACT_SET);
  163. memcpy(cmd.macadd, phwaddr->sa_data, ETH_ALEN);
  164. ret = lbs_cmd_with_response(priv, CMD_802_11_MAC_ADDRESS, &cmd);
  165. if (ret) {
  166. lbs_deb_net("set MAC address failed\n");
  167. goto done;
  168. }
  169. memcpy(priv->current_addr, phwaddr->sa_data, ETH_ALEN);
  170. memcpy(dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
  171. if (priv->mesh_dev)
  172. memcpy(priv->mesh_dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
  173. done:
  174. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  175. return ret;
  176. }
  177. static inline int mac_in_list(unsigned char *list, int list_len,
  178. unsigned char *mac)
  179. {
  180. while (list_len) {
  181. if (!memcmp(list, mac, ETH_ALEN))
  182. return 1;
  183. list += ETH_ALEN;
  184. list_len--;
  185. }
  186. return 0;
  187. }
  188. static int lbs_add_mcast_addrs(struct cmd_ds_mac_multicast_adr *cmd,
  189. struct net_device *dev, int nr_addrs)
  190. {
  191. int i = nr_addrs;
  192. struct netdev_hw_addr *ha;
  193. int cnt;
  194. if ((dev->flags & (IFF_UP|IFF_MULTICAST)) != (IFF_UP|IFF_MULTICAST))
  195. return nr_addrs;
  196. netif_addr_lock_bh(dev);
  197. cnt = netdev_mc_count(dev);
  198. netdev_for_each_mc_addr(ha, dev) {
  199. if (mac_in_list(cmd->maclist, nr_addrs, ha->addr)) {
  200. lbs_deb_net("mcast address %s:%pM skipped\n", dev->name,
  201. ha->addr);
  202. cnt--;
  203. continue;
  204. }
  205. if (i == MRVDRV_MAX_MULTICAST_LIST_SIZE)
  206. break;
  207. memcpy(&cmd->maclist[6*i], ha->addr, ETH_ALEN);
  208. lbs_deb_net("mcast address %s:%pM added to filter\n", dev->name,
  209. ha->addr);
  210. i++;
  211. cnt--;
  212. }
  213. netif_addr_unlock_bh(dev);
  214. if (cnt)
  215. return -EOVERFLOW;
  216. return i;
  217. }
  218. static void lbs_set_mcast_worker(struct work_struct *work)
  219. {
  220. struct lbs_private *priv = container_of(work, struct lbs_private, mcast_work);
  221. struct cmd_ds_mac_multicast_adr mcast_cmd;
  222. int dev_flags;
  223. int nr_addrs;
  224. int old_mac_control = priv->mac_control;
  225. lbs_deb_enter(LBS_DEB_NET);
  226. dev_flags = priv->dev->flags;
  227. if (priv->mesh_dev)
  228. dev_flags |= priv->mesh_dev->flags;
  229. if (dev_flags & IFF_PROMISC) {
  230. priv->mac_control |= CMD_ACT_MAC_PROMISCUOUS_ENABLE;
  231. priv->mac_control &= ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
  232. CMD_ACT_MAC_MULTICAST_ENABLE);
  233. goto out_set_mac_control;
  234. } else if (dev_flags & IFF_ALLMULTI) {
  235. do_allmulti:
  236. priv->mac_control |= CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
  237. priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
  238. CMD_ACT_MAC_MULTICAST_ENABLE);
  239. goto out_set_mac_control;
  240. }
  241. /* Once for priv->dev, again for priv->mesh_dev if it exists */
  242. nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->dev, 0);
  243. if (nr_addrs >= 0 && priv->mesh_dev)
  244. nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->mesh_dev, nr_addrs);
  245. if (nr_addrs < 0)
  246. goto do_allmulti;
  247. if (nr_addrs) {
  248. int size = offsetof(struct cmd_ds_mac_multicast_adr,
  249. maclist[6*nr_addrs]);
  250. mcast_cmd.action = cpu_to_le16(CMD_ACT_SET);
  251. mcast_cmd.hdr.size = cpu_to_le16(size);
  252. mcast_cmd.nr_of_adrs = cpu_to_le16(nr_addrs);
  253. lbs_cmd_async(priv, CMD_MAC_MULTICAST_ADR, &mcast_cmd.hdr, size);
  254. priv->mac_control |= CMD_ACT_MAC_MULTICAST_ENABLE;
  255. } else
  256. priv->mac_control &= ~CMD_ACT_MAC_MULTICAST_ENABLE;
  257. priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
  258. CMD_ACT_MAC_ALL_MULTICAST_ENABLE);
  259. out_set_mac_control:
  260. if (priv->mac_control != old_mac_control)
  261. lbs_set_mac_control(priv);
  262. lbs_deb_leave(LBS_DEB_NET);
  263. }
  264. void lbs_set_multicast_list(struct net_device *dev)
  265. {
  266. struct lbs_private *priv = dev->ml_priv;
  267. schedule_work(&priv->mcast_work);
  268. }
  269. /**
  270. * @brief This function handles the major jobs in the LBS driver.
  271. * It handles all events generated by firmware, RX data received
  272. * from firmware and TX data sent from kernel.
  273. *
  274. * @param data A pointer to lbs_thread structure
  275. * @return 0
  276. */
  277. static int lbs_thread(void *data)
  278. {
  279. struct net_device *dev = data;
  280. struct lbs_private *priv = dev->ml_priv;
  281. wait_queue_t wait;
  282. lbs_deb_enter(LBS_DEB_THREAD);
  283. init_waitqueue_entry(&wait, current);
  284. for (;;) {
  285. int shouldsleep;
  286. u8 resp_idx;
  287. lbs_deb_thread("1: currenttxskb %p, dnld_sent %d\n",
  288. priv->currenttxskb, priv->dnld_sent);
  289. add_wait_queue(&priv->waitq, &wait);
  290. set_current_state(TASK_INTERRUPTIBLE);
  291. spin_lock_irq(&priv->driver_lock);
  292. if (kthread_should_stop())
  293. shouldsleep = 0; /* Bye */
  294. else if (priv->surpriseremoved)
  295. shouldsleep = 1; /* We need to wait until we're _told_ to die */
  296. else if (priv->psstate == PS_STATE_SLEEP)
  297. shouldsleep = 1; /* Sleep mode. Nothing we can do till it wakes */
  298. else if (priv->cmd_timed_out)
  299. shouldsleep = 0; /* Command timed out. Recover */
  300. else if (!priv->fw_ready)
  301. shouldsleep = 1; /* Firmware not ready. We're waiting for it */
  302. else if (priv->dnld_sent)
  303. shouldsleep = 1; /* Something is en route to the device already */
  304. else if (priv->tx_pending_len > 0)
  305. shouldsleep = 0; /* We've a packet to send */
  306. else if (priv->resp_len[priv->resp_idx])
  307. shouldsleep = 0; /* We have a command response */
  308. else if (priv->cur_cmd)
  309. shouldsleep = 1; /* Can't send a command; one already running */
  310. else if (!list_empty(&priv->cmdpendingq) &&
  311. !(priv->wakeup_dev_required))
  312. shouldsleep = 0; /* We have a command to send */
  313. else if (kfifo_len(&priv->event_fifo))
  314. shouldsleep = 0; /* We have an event to process */
  315. else
  316. shouldsleep = 1; /* No command */
  317. if (shouldsleep) {
  318. lbs_deb_thread("sleeping, connect_status %d, "
  319. "psmode %d, psstate %d\n",
  320. priv->connect_status,
  321. priv->psmode, priv->psstate);
  322. spin_unlock_irq(&priv->driver_lock);
  323. schedule();
  324. } else
  325. spin_unlock_irq(&priv->driver_lock);
  326. lbs_deb_thread("2: currenttxskb %p, dnld_send %d\n",
  327. priv->currenttxskb, priv->dnld_sent);
  328. set_current_state(TASK_RUNNING);
  329. remove_wait_queue(&priv->waitq, &wait);
  330. lbs_deb_thread("3: currenttxskb %p, dnld_sent %d\n",
  331. priv->currenttxskb, priv->dnld_sent);
  332. if (kthread_should_stop()) {
  333. lbs_deb_thread("break from main thread\n");
  334. break;
  335. }
  336. if (priv->surpriseremoved) {
  337. lbs_deb_thread("adapter removed; waiting to die...\n");
  338. continue;
  339. }
  340. lbs_deb_thread("4: currenttxskb %p, dnld_sent %d\n",
  341. priv->currenttxskb, priv->dnld_sent);
  342. /* Process any pending command response */
  343. spin_lock_irq(&priv->driver_lock);
  344. resp_idx = priv->resp_idx;
  345. if (priv->resp_len[resp_idx]) {
  346. spin_unlock_irq(&priv->driver_lock);
  347. lbs_process_command_response(priv,
  348. priv->resp_buf[resp_idx],
  349. priv->resp_len[resp_idx]);
  350. spin_lock_irq(&priv->driver_lock);
  351. priv->resp_len[resp_idx] = 0;
  352. }
  353. spin_unlock_irq(&priv->driver_lock);
  354. /* Process hardware events, e.g. card removed, link lost */
  355. spin_lock_irq(&priv->driver_lock);
  356. while (kfifo_len(&priv->event_fifo)) {
  357. u32 event;
  358. if (kfifo_out(&priv->event_fifo,
  359. (unsigned char *) &event, sizeof(event)) !=
  360. sizeof(event))
  361. break;
  362. spin_unlock_irq(&priv->driver_lock);
  363. lbs_process_event(priv, event);
  364. spin_lock_irq(&priv->driver_lock);
  365. }
  366. spin_unlock_irq(&priv->driver_lock);
  367. if (priv->wakeup_dev_required) {
  368. lbs_deb_thread("Waking up device...\n");
  369. /* Wake up device */
  370. if (priv->exit_deep_sleep(priv))
  371. lbs_deb_thread("Wakeup device failed\n");
  372. continue;
  373. }
  374. /* command timeout stuff */
  375. if (priv->cmd_timed_out && priv->cur_cmd) {
  376. struct cmd_ctrl_node *cmdnode = priv->cur_cmd;
  377. lbs_pr_info("Timeout submitting command 0x%04x\n",
  378. le16_to_cpu(cmdnode->cmdbuf->command));
  379. lbs_complete_command(priv, cmdnode, -ETIMEDOUT);
  380. if (priv->reset_card)
  381. priv->reset_card(priv);
  382. }
  383. priv->cmd_timed_out = 0;
  384. if (!priv->fw_ready)
  385. continue;
  386. /* Check if we need to confirm Sleep Request received previously */
  387. if (priv->psstate == PS_STATE_PRE_SLEEP &&
  388. !priv->dnld_sent && !priv->cur_cmd) {
  389. if (priv->connect_status == LBS_CONNECTED) {
  390. lbs_deb_thread("pre-sleep, currenttxskb %p, "
  391. "dnld_sent %d, cur_cmd %p\n",
  392. priv->currenttxskb, priv->dnld_sent,
  393. priv->cur_cmd);
  394. lbs_ps_confirm_sleep(priv);
  395. } else {
  396. /* workaround for firmware sending
  397. * deauth/linkloss event immediately
  398. * after sleep request; remove this
  399. * after firmware fixes it
  400. */
  401. priv->psstate = PS_STATE_AWAKE;
  402. lbs_pr_alert("ignore PS_SleepConfirm in "
  403. "non-connected state\n");
  404. }
  405. }
  406. /* The PS state is changed during processing of Sleep Request
  407. * event above
  408. */
  409. if ((priv->psstate == PS_STATE_SLEEP) ||
  410. (priv->psstate == PS_STATE_PRE_SLEEP))
  411. continue;
  412. if (priv->is_deep_sleep)
  413. continue;
  414. /* Execute the next command */
  415. if (!priv->dnld_sent && !priv->cur_cmd)
  416. lbs_execute_next_command(priv);
  417. spin_lock_irq(&priv->driver_lock);
  418. if (!priv->dnld_sent && priv->tx_pending_len > 0) {
  419. int ret = priv->hw_host_to_card(priv, MVMS_DAT,
  420. priv->tx_pending_buf,
  421. priv->tx_pending_len);
  422. if (ret) {
  423. lbs_deb_tx("host_to_card failed %d\n", ret);
  424. priv->dnld_sent = DNLD_RES_RECEIVED;
  425. }
  426. priv->tx_pending_len = 0;
  427. if (!priv->currenttxskb) {
  428. /* We can wake the queues immediately if we aren't
  429. waiting for TX feedback */
  430. if (priv->connect_status == LBS_CONNECTED)
  431. netif_wake_queue(priv->dev);
  432. if (priv->mesh_dev &&
  433. lbs_mesh_connected(priv))
  434. netif_wake_queue(priv->mesh_dev);
  435. }
  436. }
  437. spin_unlock_irq(&priv->driver_lock);
  438. }
  439. del_timer(&priv->command_timer);
  440. del_timer(&priv->auto_deepsleep_timer);
  441. lbs_deb_leave(LBS_DEB_THREAD);
  442. return 0;
  443. }
  444. int lbs_suspend(struct lbs_private *priv)
  445. {
  446. int ret;
  447. lbs_deb_enter(LBS_DEB_FW);
  448. if (priv->is_deep_sleep) {
  449. ret = lbs_set_deep_sleep(priv, 0);
  450. if (ret) {
  451. lbs_pr_err("deep sleep cancellation failed: %d\n", ret);
  452. return ret;
  453. }
  454. priv->deep_sleep_required = 1;
  455. }
  456. ret = lbs_set_host_sleep(priv, 1);
  457. netif_device_detach(priv->dev);
  458. if (priv->mesh_dev)
  459. netif_device_detach(priv->mesh_dev);
  460. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  461. return ret;
  462. }
  463. EXPORT_SYMBOL_GPL(lbs_suspend);
  464. int lbs_resume(struct lbs_private *priv)
  465. {
  466. int ret;
  467. lbs_deb_enter(LBS_DEB_FW);
  468. ret = lbs_set_host_sleep(priv, 0);
  469. netif_device_attach(priv->dev);
  470. if (priv->mesh_dev)
  471. netif_device_attach(priv->mesh_dev);
  472. if (priv->deep_sleep_required) {
  473. priv->deep_sleep_required = 0;
  474. ret = lbs_set_deep_sleep(priv, 1);
  475. if (ret)
  476. lbs_pr_err("deep sleep activation failed: %d\n", ret);
  477. }
  478. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  479. return ret;
  480. }
  481. EXPORT_SYMBOL_GPL(lbs_resume);
  482. /**
  483. * @brief This function gets the HW spec from the firmware and sets
  484. * some basic parameters.
  485. *
  486. * @param priv A pointer to struct lbs_private structure
  487. * @return 0 or -1
  488. */
  489. static int lbs_setup_firmware(struct lbs_private *priv)
  490. {
  491. int ret = -1;
  492. s16 curlevel = 0, minlevel = 0, maxlevel = 0;
  493. lbs_deb_enter(LBS_DEB_FW);
  494. /* Read MAC address from firmware */
  495. memset(priv->current_addr, 0xff, ETH_ALEN);
  496. ret = lbs_update_hw_spec(priv);
  497. if (ret)
  498. goto done;
  499. /* Read power levels if available */
  500. ret = lbs_get_tx_power(priv, &curlevel, &minlevel, &maxlevel);
  501. if (ret == 0) {
  502. priv->txpower_cur = curlevel;
  503. priv->txpower_min = minlevel;
  504. priv->txpower_max = maxlevel;
  505. }
  506. /* Send cmd to FW to enable 11D function */
  507. ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_11D_ENABLE, 1);
  508. lbs_set_mac_control(priv);
  509. done:
  510. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  511. return ret;
  512. }
  513. /**
  514. * This function handles the timeout of command sending.
  515. * It will re-send the same command again.
  516. */
  517. static void lbs_cmd_timeout_handler(unsigned long data)
  518. {
  519. struct lbs_private *priv = (struct lbs_private *)data;
  520. unsigned long flags;
  521. lbs_deb_enter(LBS_DEB_CMD);
  522. spin_lock_irqsave(&priv->driver_lock, flags);
  523. if (!priv->cur_cmd)
  524. goto out;
  525. lbs_pr_info("command 0x%04x timed out\n",
  526. le16_to_cpu(priv->cur_cmd->cmdbuf->command));
  527. priv->cmd_timed_out = 1;
  528. wake_up_interruptible(&priv->waitq);
  529. out:
  530. spin_unlock_irqrestore(&priv->driver_lock, flags);
  531. lbs_deb_leave(LBS_DEB_CMD);
  532. }
  533. /**
  534. * This function put the device back to deep sleep mode when timer expires
  535. * and no activity (command, event, data etc.) is detected.
  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. lbs_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. lbs_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_tx_timeout = lbs_tx_timeout,
  641. .ndo_set_multicast_list = lbs_set_multicast_list,
  642. .ndo_change_mtu = eth_change_mtu,
  643. .ndo_validate_addr = eth_validate_addr,
  644. };
  645. /**
  646. * @brief This function adds the card. it will probe the
  647. * card, allocate the lbs_priv and initialize the device.
  648. *
  649. * @param card A pointer to card
  650. * @return 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. lbs_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. lbs_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 = 0xffffffff;
  697. priv->wol_gpio = 0xff;
  698. priv->wol_gap = 20;
  699. goto done;
  700. err_ndev:
  701. free_netdev(dev);
  702. err_adapter:
  703. lbs_free_adapter(priv);
  704. err_wdev:
  705. lbs_cfg_free(priv);
  706. priv = NULL;
  707. done:
  708. lbs_deb_leave_args(LBS_DEB_MAIN, "priv %p", priv);
  709. return priv;
  710. }
  711. EXPORT_SYMBOL_GPL(lbs_add_card);
  712. void lbs_remove_card(struct lbs_private *priv)
  713. {
  714. struct net_device *dev = priv->dev;
  715. lbs_deb_enter(LBS_DEB_MAIN);
  716. lbs_remove_mesh(priv);
  717. lbs_scan_deinit(priv);
  718. dev = priv->dev;
  719. cancel_work_sync(&priv->mcast_work);
  720. /* worker thread destruction blocks on the in-flight command which
  721. * should have been cleared already in lbs_stop_card().
  722. */
  723. lbs_deb_main("destroying worker thread\n");
  724. destroy_workqueue(priv->work_thread);
  725. lbs_deb_main("done destroying worker thread\n");
  726. if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
  727. priv->psmode = LBS802_11POWERMODECAM;
  728. lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS, true);
  729. }
  730. if (priv->is_deep_sleep) {
  731. priv->is_deep_sleep = 0;
  732. wake_up_interruptible(&priv->ds_awake_q);
  733. }
  734. priv->is_host_sleep_configured = 0;
  735. priv->is_host_sleep_activated = 0;
  736. wake_up_interruptible(&priv->host_sleep_q);
  737. /* Stop the thread servicing the interrupts */
  738. priv->surpriseremoved = 1;
  739. kthread_stop(priv->main_thread);
  740. lbs_free_adapter(priv);
  741. lbs_cfg_free(priv);
  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. /**
  841. * @brief Retrieves two-stage firmware
  842. *
  843. * @param dev A pointer to device structure
  844. * @param user_helper User-defined helper firmware file
  845. * @param user_mainfw User-defined main firmware file
  846. * @param card_model Bus-specific card model ID used to filter firmware table
  847. * elements
  848. * @param fw_table Table of firmware file names and device model numbers
  849. * terminated by an entry with a NULL helper name
  850. * @param helper On success, the helper firmware; caller must free
  851. * @param mainfw On success, the main firmware; caller must free
  852. *
  853. * @return 0 on success, non-zero on failure
  854. */
  855. int lbs_get_firmware(struct device *dev, const char *user_helper,
  856. const char *user_mainfw, u32 card_model,
  857. const struct lbs_fw_table *fw_table,
  858. const struct firmware **helper,
  859. const struct firmware **mainfw)
  860. {
  861. const struct lbs_fw_table *iter;
  862. int ret;
  863. BUG_ON(helper == NULL);
  864. BUG_ON(mainfw == NULL);
  865. /* Try user-specified firmware first */
  866. if (user_helper) {
  867. ret = request_firmware(helper, user_helper, dev);
  868. if (ret) {
  869. lbs_pr_err("couldn't find helper firmware %s",
  870. user_helper);
  871. goto fail;
  872. }
  873. }
  874. if (user_mainfw) {
  875. ret = request_firmware(mainfw, user_mainfw, dev);
  876. if (ret) {
  877. lbs_pr_err("couldn't find main firmware %s",
  878. user_mainfw);
  879. goto fail;
  880. }
  881. }
  882. if (*helper && *mainfw)
  883. return 0;
  884. /* Otherwise search for firmware to use. If neither the helper or
  885. * the main firmware were specified by the user, then we need to
  886. * make sure that found helper & main are from the same entry in
  887. * fw_table.
  888. */
  889. iter = fw_table;
  890. while (iter && iter->helper) {
  891. if (iter->model != card_model)
  892. goto next;
  893. if (*helper == NULL) {
  894. ret = request_firmware(helper, iter->helper, dev);
  895. if (ret)
  896. goto next;
  897. /* If the device has one-stage firmware (ie cf8305) and
  898. * we've got it then we don't need to bother with the
  899. * main firmware.
  900. */
  901. if (iter->fwname == NULL)
  902. return 0;
  903. }
  904. if (*mainfw == NULL) {
  905. ret = request_firmware(mainfw, iter->fwname, dev);
  906. if (ret && !user_helper) {
  907. /* Clear the helper if it wasn't user-specified
  908. * and the main firmware load failed, to ensure
  909. * we don't have mismatched firmware pairs.
  910. */
  911. release_firmware(*helper);
  912. *helper = NULL;
  913. }
  914. }
  915. if (*helper && *mainfw)
  916. return 0;
  917. next:
  918. iter++;
  919. }
  920. fail:
  921. /* Failed */
  922. if (*helper) {
  923. release_firmware(*helper);
  924. *helper = NULL;
  925. }
  926. if (*mainfw) {
  927. release_firmware(*mainfw);
  928. *mainfw = NULL;
  929. }
  930. return -ENOENT;
  931. }
  932. EXPORT_SYMBOL_GPL(lbs_get_firmware);
  933. static int __init lbs_init_module(void)
  934. {
  935. lbs_deb_enter(LBS_DEB_MAIN);
  936. memset(&confirm_sleep, 0, sizeof(confirm_sleep));
  937. confirm_sleep.hdr.command = cpu_to_le16(CMD_802_11_PS_MODE);
  938. confirm_sleep.hdr.size = cpu_to_le16(sizeof(confirm_sleep));
  939. confirm_sleep.action = cpu_to_le16(PS_MODE_ACTION_SLEEP_CONFIRMED);
  940. lbs_debugfs_init();
  941. lbs_deb_leave(LBS_DEB_MAIN);
  942. return 0;
  943. }
  944. static void __exit lbs_exit_module(void)
  945. {
  946. lbs_deb_enter(LBS_DEB_MAIN);
  947. lbs_debugfs_remove();
  948. lbs_deb_leave(LBS_DEB_MAIN);
  949. }
  950. module_init(lbs_init_module);
  951. module_exit(lbs_exit_module);
  952. MODULE_DESCRIPTION("Libertas WLAN Driver Library");
  953. MODULE_AUTHOR("Marvell International Ltd.");
  954. MODULE_LICENSE("GPL");