PageRenderTime 28ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/abioy/linux
C | 1296 lines | 905 code | 251 blank | 140 comment | 147 complexity | dada764e147e0d9bc29c5c622ea40391 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, 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/stddef.h>
  14. #include <linux/ieee80211.h>
  15. #include <linux/slab.h>
  16. #include <net/iw_handler.h>
  17. #include <net/cfg80211.h>
  18. #include "host.h"
  19. #include "decl.h"
  20. #include "dev.h"
  21. #include "wext.h"
  22. #include "cfg.h"
  23. #include "debugfs.h"
  24. #include "scan.h"
  25. #include "assoc.h"
  26. #include "cmd.h"
  27. #define DRIVER_RELEASE_VERSION "323.p0"
  28. const char lbs_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
  29. #ifdef DEBUG
  30. "-dbg"
  31. #endif
  32. "";
  33. /* Module parameters */
  34. unsigned int lbs_debug;
  35. EXPORT_SYMBOL_GPL(lbs_debug);
  36. module_param_named(libertas_debug, lbs_debug, int, 0644);
  37. /* This global structure is used to send the confirm_sleep command as
  38. * fast as possible down to the firmware. */
  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. * @brief use index to get the data rate
  56. *
  57. * @param idx The index of data rate
  58. * @return 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. * @brief use rate to get the index
  68. *
  69. * @param rate data rate
  70. * @return 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. static int lbs_add_rtap(struct lbs_private *priv);
  84. static void lbs_remove_rtap(struct lbs_private *priv);
  85. /**
  86. * Get function for sysfs attribute rtap
  87. */
  88. static ssize_t lbs_rtap_get(struct device *dev,
  89. struct device_attribute *attr, char * buf)
  90. {
  91. struct lbs_private *priv = to_net_dev(dev)->ml_priv;
  92. return snprintf(buf, 5, "0x%X\n", priv->monitormode);
  93. }
  94. /**
  95. * Set function for sysfs attribute rtap
  96. */
  97. static ssize_t lbs_rtap_set(struct device *dev,
  98. struct device_attribute *attr, const char * buf, size_t count)
  99. {
  100. int monitor_mode;
  101. struct lbs_private *priv = to_net_dev(dev)->ml_priv;
  102. sscanf(buf, "%x", &monitor_mode);
  103. if (monitor_mode) {
  104. if (priv->monitormode == monitor_mode)
  105. return strlen(buf);
  106. if (!priv->monitormode) {
  107. if (priv->infra_open || lbs_mesh_open(priv))
  108. return -EBUSY;
  109. if (priv->mode == IW_MODE_INFRA)
  110. lbs_cmd_80211_deauthenticate(priv,
  111. priv->curbssparams.bssid,
  112. WLAN_REASON_DEAUTH_LEAVING);
  113. else if (priv->mode == IW_MODE_ADHOC)
  114. lbs_adhoc_stop(priv);
  115. lbs_add_rtap(priv);
  116. }
  117. priv->monitormode = monitor_mode;
  118. } else {
  119. if (!priv->monitormode)
  120. return strlen(buf);
  121. priv->monitormode = 0;
  122. lbs_remove_rtap(priv);
  123. if (priv->currenttxskb) {
  124. dev_kfree_skb_any(priv->currenttxskb);
  125. priv->currenttxskb = NULL;
  126. }
  127. /* Wake queues, command thread, etc. */
  128. lbs_host_to_card_done(priv);
  129. }
  130. lbs_prepare_and_send_command(priv,
  131. CMD_802_11_MONITOR_MODE, CMD_ACT_SET,
  132. CMD_OPTION_WAITFORRSP, 0, &priv->monitormode);
  133. return strlen(buf);
  134. }
  135. /**
  136. * lbs_rtap attribute to be exported per ethX interface
  137. * through sysfs (/sys/class/net/ethX/lbs_rtap)
  138. */
  139. static DEVICE_ATTR(lbs_rtap, 0644, lbs_rtap_get, lbs_rtap_set );
  140. /**
  141. * @brief This function opens the ethX interface
  142. *
  143. * @param dev A pointer to net_device structure
  144. * @return 0 or -EBUSY if monitor mode active
  145. */
  146. static int lbs_dev_open(struct net_device *dev)
  147. {
  148. struct lbs_private *priv = dev->ml_priv;
  149. int ret = 0;
  150. lbs_deb_enter(LBS_DEB_NET);
  151. spin_lock_irq(&priv->driver_lock);
  152. if (priv->monitormode) {
  153. ret = -EBUSY;
  154. goto out;
  155. }
  156. priv->infra_open = 1;
  157. if (priv->connect_status == LBS_CONNECTED)
  158. netif_carrier_on(dev);
  159. else
  160. netif_carrier_off(dev);
  161. if (!priv->tx_pending_len)
  162. netif_wake_queue(dev);
  163. out:
  164. spin_unlock_irq(&priv->driver_lock);
  165. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  166. return ret;
  167. }
  168. /**
  169. * @brief This function closes the ethX interface
  170. *
  171. * @param dev A pointer to net_device structure
  172. * @return 0
  173. */
  174. static int lbs_eth_stop(struct net_device *dev)
  175. {
  176. struct lbs_private *priv = dev->ml_priv;
  177. lbs_deb_enter(LBS_DEB_NET);
  178. spin_lock_irq(&priv->driver_lock);
  179. priv->infra_open = 0;
  180. netif_stop_queue(dev);
  181. spin_unlock_irq(&priv->driver_lock);
  182. schedule_work(&priv->mcast_work);
  183. lbs_deb_leave(LBS_DEB_NET);
  184. return 0;
  185. }
  186. static void lbs_tx_timeout(struct net_device *dev)
  187. {
  188. struct lbs_private *priv = dev->ml_priv;
  189. lbs_deb_enter(LBS_DEB_TX);
  190. lbs_pr_err("tx watch dog timeout\n");
  191. dev->trans_start = jiffies;
  192. if (priv->currenttxskb)
  193. lbs_send_tx_feedback(priv, 0);
  194. /* XX: Shouldn't we also call into the hw-specific driver
  195. to kick it somehow? */
  196. lbs_host_to_card_done(priv);
  197. /* More often than not, this actually happens because the
  198. firmware has crapped itself -- rather than just a very
  199. busy medium. So send a harmless command, and if/when
  200. _that_ times out, we'll kick it in the head. */
  201. lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
  202. 0, 0, NULL);
  203. lbs_deb_leave(LBS_DEB_TX);
  204. }
  205. void lbs_host_to_card_done(struct lbs_private *priv)
  206. {
  207. unsigned long flags;
  208. lbs_deb_enter(LBS_DEB_THREAD);
  209. spin_lock_irqsave(&priv->driver_lock, flags);
  210. priv->dnld_sent = DNLD_RES_RECEIVED;
  211. /* Wake main thread if commands are pending */
  212. if (!priv->cur_cmd || priv->tx_pending_len > 0) {
  213. if (!priv->wakeup_dev_required)
  214. wake_up_interruptible(&priv->waitq);
  215. }
  216. spin_unlock_irqrestore(&priv->driver_lock, flags);
  217. lbs_deb_leave(LBS_DEB_THREAD);
  218. }
  219. EXPORT_SYMBOL_GPL(lbs_host_to_card_done);
  220. int lbs_set_mac_address(struct net_device *dev, void *addr)
  221. {
  222. int ret = 0;
  223. struct lbs_private *priv = dev->ml_priv;
  224. struct sockaddr *phwaddr = addr;
  225. struct cmd_ds_802_11_mac_address cmd;
  226. lbs_deb_enter(LBS_DEB_NET);
  227. /* In case it was called from the mesh device */
  228. dev = priv->dev;
  229. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  230. cmd.action = cpu_to_le16(CMD_ACT_SET);
  231. memcpy(cmd.macadd, phwaddr->sa_data, ETH_ALEN);
  232. ret = lbs_cmd_with_response(priv, CMD_802_11_MAC_ADDRESS, &cmd);
  233. if (ret) {
  234. lbs_deb_net("set MAC address failed\n");
  235. goto done;
  236. }
  237. memcpy(priv->current_addr, phwaddr->sa_data, ETH_ALEN);
  238. memcpy(dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
  239. if (priv->mesh_dev)
  240. memcpy(priv->mesh_dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
  241. done:
  242. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  243. return ret;
  244. }
  245. static inline int mac_in_list(unsigned char *list, int list_len,
  246. unsigned char *mac)
  247. {
  248. while (list_len) {
  249. if (!memcmp(list, mac, ETH_ALEN))
  250. return 1;
  251. list += ETH_ALEN;
  252. list_len--;
  253. }
  254. return 0;
  255. }
  256. static int lbs_add_mcast_addrs(struct cmd_ds_mac_multicast_adr *cmd,
  257. struct net_device *dev, int nr_addrs)
  258. {
  259. int i = nr_addrs;
  260. struct dev_mc_list *mc_list;
  261. int cnt;
  262. if ((dev->flags & (IFF_UP|IFF_MULTICAST)) != (IFF_UP|IFF_MULTICAST))
  263. return nr_addrs;
  264. netif_addr_lock_bh(dev);
  265. cnt = netdev_mc_count(dev);
  266. netdev_for_each_mc_addr(mc_list, dev) {
  267. if (mac_in_list(cmd->maclist, nr_addrs, mc_list->dmi_addr)) {
  268. lbs_deb_net("mcast address %s:%pM skipped\n", dev->name,
  269. mc_list->dmi_addr);
  270. cnt--;
  271. continue;
  272. }
  273. if (i == MRVDRV_MAX_MULTICAST_LIST_SIZE)
  274. break;
  275. memcpy(&cmd->maclist[6*i], mc_list->dmi_addr, ETH_ALEN);
  276. lbs_deb_net("mcast address %s:%pM added to filter\n", dev->name,
  277. mc_list->dmi_addr);
  278. i++;
  279. cnt--;
  280. }
  281. netif_addr_unlock_bh(dev);
  282. if (cnt)
  283. return -EOVERFLOW;
  284. return i;
  285. }
  286. static void lbs_set_mcast_worker(struct work_struct *work)
  287. {
  288. struct lbs_private *priv = container_of(work, struct lbs_private, mcast_work);
  289. struct cmd_ds_mac_multicast_adr mcast_cmd;
  290. int dev_flags;
  291. int nr_addrs;
  292. int old_mac_control = priv->mac_control;
  293. lbs_deb_enter(LBS_DEB_NET);
  294. dev_flags = priv->dev->flags;
  295. if (priv->mesh_dev)
  296. dev_flags |= priv->mesh_dev->flags;
  297. if (dev_flags & IFF_PROMISC) {
  298. priv->mac_control |= CMD_ACT_MAC_PROMISCUOUS_ENABLE;
  299. priv->mac_control &= ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
  300. CMD_ACT_MAC_MULTICAST_ENABLE);
  301. goto out_set_mac_control;
  302. } else if (dev_flags & IFF_ALLMULTI) {
  303. do_allmulti:
  304. priv->mac_control |= CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
  305. priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
  306. CMD_ACT_MAC_MULTICAST_ENABLE);
  307. goto out_set_mac_control;
  308. }
  309. /* Once for priv->dev, again for priv->mesh_dev if it exists */
  310. nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->dev, 0);
  311. if (nr_addrs >= 0 && priv->mesh_dev)
  312. nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->mesh_dev, nr_addrs);
  313. if (nr_addrs < 0)
  314. goto do_allmulti;
  315. if (nr_addrs) {
  316. int size = offsetof(struct cmd_ds_mac_multicast_adr,
  317. maclist[6*nr_addrs]);
  318. mcast_cmd.action = cpu_to_le16(CMD_ACT_SET);
  319. mcast_cmd.hdr.size = cpu_to_le16(size);
  320. mcast_cmd.nr_of_adrs = cpu_to_le16(nr_addrs);
  321. lbs_cmd_async(priv, CMD_MAC_MULTICAST_ADR, &mcast_cmd.hdr, size);
  322. priv->mac_control |= CMD_ACT_MAC_MULTICAST_ENABLE;
  323. } else
  324. priv->mac_control &= ~CMD_ACT_MAC_MULTICAST_ENABLE;
  325. priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
  326. CMD_ACT_MAC_ALL_MULTICAST_ENABLE);
  327. out_set_mac_control:
  328. if (priv->mac_control != old_mac_control)
  329. lbs_set_mac_control(priv);
  330. lbs_deb_leave(LBS_DEB_NET);
  331. }
  332. void lbs_set_multicast_list(struct net_device *dev)
  333. {
  334. struct lbs_private *priv = dev->ml_priv;
  335. schedule_work(&priv->mcast_work);
  336. }
  337. /**
  338. * @brief This function handles the major jobs in the LBS driver.
  339. * It handles all events generated by firmware, RX data received
  340. * from firmware and TX data sent from kernel.
  341. *
  342. * @param data A pointer to lbs_thread structure
  343. * @return 0
  344. */
  345. static int lbs_thread(void *data)
  346. {
  347. struct net_device *dev = data;
  348. struct lbs_private *priv = dev->ml_priv;
  349. wait_queue_t wait;
  350. lbs_deb_enter(LBS_DEB_THREAD);
  351. init_waitqueue_entry(&wait, current);
  352. for (;;) {
  353. int shouldsleep;
  354. u8 resp_idx;
  355. lbs_deb_thread("1: currenttxskb %p, dnld_sent %d\n",
  356. priv->currenttxskb, priv->dnld_sent);
  357. add_wait_queue(&priv->waitq, &wait);
  358. set_current_state(TASK_INTERRUPTIBLE);
  359. spin_lock_irq(&priv->driver_lock);
  360. if (kthread_should_stop())
  361. shouldsleep = 0; /* Bye */
  362. else if (priv->surpriseremoved)
  363. shouldsleep = 1; /* We need to wait until we're _told_ to die */
  364. else if (priv->psstate == PS_STATE_SLEEP)
  365. shouldsleep = 1; /* Sleep mode. Nothing we can do till it wakes */
  366. else if (priv->cmd_timed_out)
  367. shouldsleep = 0; /* Command timed out. Recover */
  368. else if (!priv->fw_ready)
  369. shouldsleep = 1; /* Firmware not ready. We're waiting for it */
  370. else if (priv->dnld_sent)
  371. shouldsleep = 1; /* Something is en route to the device already */
  372. else if (priv->tx_pending_len > 0)
  373. shouldsleep = 0; /* We've a packet to send */
  374. else if (priv->resp_len[priv->resp_idx])
  375. shouldsleep = 0; /* We have a command response */
  376. else if (priv->cur_cmd)
  377. shouldsleep = 1; /* Can't send a command; one already running */
  378. else if (!list_empty(&priv->cmdpendingq) &&
  379. !(priv->wakeup_dev_required))
  380. shouldsleep = 0; /* We have a command to send */
  381. else if (kfifo_len(&priv->event_fifo))
  382. shouldsleep = 0; /* We have an event to process */
  383. else
  384. shouldsleep = 1; /* No command */
  385. if (shouldsleep) {
  386. lbs_deb_thread("sleeping, connect_status %d, "
  387. "psmode %d, psstate %d\n",
  388. priv->connect_status,
  389. priv->psmode, priv->psstate);
  390. spin_unlock_irq(&priv->driver_lock);
  391. schedule();
  392. } else
  393. spin_unlock_irq(&priv->driver_lock);
  394. lbs_deb_thread("2: currenttxskb %p, dnld_send %d\n",
  395. priv->currenttxskb, priv->dnld_sent);
  396. set_current_state(TASK_RUNNING);
  397. remove_wait_queue(&priv->waitq, &wait);
  398. lbs_deb_thread("3: currenttxskb %p, dnld_sent %d\n",
  399. priv->currenttxskb, priv->dnld_sent);
  400. if (kthread_should_stop()) {
  401. lbs_deb_thread("break from main thread\n");
  402. break;
  403. }
  404. if (priv->surpriseremoved) {
  405. lbs_deb_thread("adapter removed; waiting to die...\n");
  406. continue;
  407. }
  408. lbs_deb_thread("4: currenttxskb %p, dnld_sent %d\n",
  409. priv->currenttxskb, priv->dnld_sent);
  410. /* Process any pending command response */
  411. spin_lock_irq(&priv->driver_lock);
  412. resp_idx = priv->resp_idx;
  413. if (priv->resp_len[resp_idx]) {
  414. spin_unlock_irq(&priv->driver_lock);
  415. lbs_process_command_response(priv,
  416. priv->resp_buf[resp_idx],
  417. priv->resp_len[resp_idx]);
  418. spin_lock_irq(&priv->driver_lock);
  419. priv->resp_len[resp_idx] = 0;
  420. }
  421. spin_unlock_irq(&priv->driver_lock);
  422. /* Process hardware events, e.g. card removed, link lost */
  423. spin_lock_irq(&priv->driver_lock);
  424. while (kfifo_len(&priv->event_fifo)) {
  425. u32 event;
  426. if (kfifo_out(&priv->event_fifo,
  427. (unsigned char *) &event, sizeof(event)) !=
  428. sizeof(event))
  429. break;
  430. spin_unlock_irq(&priv->driver_lock);
  431. lbs_process_event(priv, event);
  432. spin_lock_irq(&priv->driver_lock);
  433. }
  434. spin_unlock_irq(&priv->driver_lock);
  435. if (priv->wakeup_dev_required) {
  436. lbs_deb_thread("Waking up device...\n");
  437. /* Wake up device */
  438. if (priv->exit_deep_sleep(priv))
  439. lbs_deb_thread("Wakeup device failed\n");
  440. continue;
  441. }
  442. /* command timeout stuff */
  443. if (priv->cmd_timed_out && priv->cur_cmd) {
  444. struct cmd_ctrl_node *cmdnode = priv->cur_cmd;
  445. lbs_pr_info("Timeout submitting command 0x%04x\n",
  446. le16_to_cpu(cmdnode->cmdbuf->command));
  447. lbs_complete_command(priv, cmdnode, -ETIMEDOUT);
  448. if (priv->reset_card)
  449. priv->reset_card(priv);
  450. }
  451. priv->cmd_timed_out = 0;
  452. if (!priv->fw_ready)
  453. continue;
  454. /* Check if we need to confirm Sleep Request received previously */
  455. if (priv->psstate == PS_STATE_PRE_SLEEP &&
  456. !priv->dnld_sent && !priv->cur_cmd) {
  457. if (priv->connect_status == LBS_CONNECTED) {
  458. lbs_deb_thread("pre-sleep, currenttxskb %p, "
  459. "dnld_sent %d, cur_cmd %p\n",
  460. priv->currenttxskb, priv->dnld_sent,
  461. priv->cur_cmd);
  462. lbs_ps_confirm_sleep(priv);
  463. } else {
  464. /* workaround for firmware sending
  465. * deauth/linkloss event immediately
  466. * after sleep request; remove this
  467. * after firmware fixes it
  468. */
  469. priv->psstate = PS_STATE_AWAKE;
  470. lbs_pr_alert("ignore PS_SleepConfirm in "
  471. "non-connected state\n");
  472. }
  473. }
  474. /* The PS state is changed during processing of Sleep Request
  475. * event above
  476. */
  477. if ((priv->psstate == PS_STATE_SLEEP) ||
  478. (priv->psstate == PS_STATE_PRE_SLEEP))
  479. continue;
  480. if (priv->is_deep_sleep)
  481. continue;
  482. /* Execute the next command */
  483. if (!priv->dnld_sent && !priv->cur_cmd)
  484. lbs_execute_next_command(priv);
  485. /* Wake-up command waiters which can't sleep in
  486. * lbs_prepare_and_send_command
  487. */
  488. if (!list_empty(&priv->cmdpendingq))
  489. wake_up_all(&priv->cmd_pending);
  490. spin_lock_irq(&priv->driver_lock);
  491. if (!priv->dnld_sent && priv->tx_pending_len > 0) {
  492. int ret = priv->hw_host_to_card(priv, MVMS_DAT,
  493. priv->tx_pending_buf,
  494. priv->tx_pending_len);
  495. if (ret) {
  496. lbs_deb_tx("host_to_card failed %d\n", ret);
  497. priv->dnld_sent = DNLD_RES_RECEIVED;
  498. }
  499. priv->tx_pending_len = 0;
  500. if (!priv->currenttxskb) {
  501. /* We can wake the queues immediately if we aren't
  502. waiting for TX feedback */
  503. if (priv->connect_status == LBS_CONNECTED)
  504. netif_wake_queue(priv->dev);
  505. if (priv->mesh_dev &&
  506. lbs_mesh_connected(priv))
  507. netif_wake_queue(priv->mesh_dev);
  508. }
  509. }
  510. spin_unlock_irq(&priv->driver_lock);
  511. }
  512. del_timer(&priv->command_timer);
  513. del_timer(&priv->auto_deepsleep_timer);
  514. wake_up_all(&priv->cmd_pending);
  515. lbs_deb_leave(LBS_DEB_THREAD);
  516. return 0;
  517. }
  518. static int lbs_suspend_callback(struct lbs_private *priv, unsigned long dummy,
  519. struct cmd_header *cmd)
  520. {
  521. lbs_deb_enter(LBS_DEB_FW);
  522. netif_device_detach(priv->dev);
  523. if (priv->mesh_dev)
  524. netif_device_detach(priv->mesh_dev);
  525. priv->fw_ready = 0;
  526. lbs_deb_leave(LBS_DEB_FW);
  527. return 0;
  528. }
  529. int lbs_suspend(struct lbs_private *priv)
  530. {
  531. struct cmd_header cmd;
  532. int ret;
  533. lbs_deb_enter(LBS_DEB_FW);
  534. if (priv->wol_criteria == 0xffffffff) {
  535. lbs_pr_info("Suspend attempt without configuring wake params!\n");
  536. return -EINVAL;
  537. }
  538. memset(&cmd, 0, sizeof(cmd));
  539. ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_ACTIVATE, &cmd,
  540. sizeof(cmd), lbs_suspend_callback, 0);
  541. if (ret)
  542. lbs_pr_info("HOST_SLEEP_ACTIVATE failed: %d\n", ret);
  543. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  544. return ret;
  545. }
  546. EXPORT_SYMBOL_GPL(lbs_suspend);
  547. void lbs_resume(struct lbs_private *priv)
  548. {
  549. lbs_deb_enter(LBS_DEB_FW);
  550. priv->fw_ready = 1;
  551. /* Firmware doesn't seem to give us RX packets any more
  552. until we send it some command. Might as well update */
  553. lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
  554. 0, 0, NULL);
  555. netif_device_attach(priv->dev);
  556. if (priv->mesh_dev)
  557. netif_device_attach(priv->mesh_dev);
  558. lbs_deb_leave(LBS_DEB_FW);
  559. }
  560. EXPORT_SYMBOL_GPL(lbs_resume);
  561. /**
  562. * @brief This function gets the HW spec from the firmware and sets
  563. * some basic parameters.
  564. *
  565. * @param priv A pointer to struct lbs_private structure
  566. * @return 0 or -1
  567. */
  568. static int lbs_setup_firmware(struct lbs_private *priv)
  569. {
  570. int ret = -1;
  571. s16 curlevel = 0, minlevel = 0, maxlevel = 0;
  572. lbs_deb_enter(LBS_DEB_FW);
  573. /* Read MAC address from firmware */
  574. memset(priv->current_addr, 0xff, ETH_ALEN);
  575. ret = lbs_update_hw_spec(priv);
  576. if (ret)
  577. goto done;
  578. /* Read power levels if available */
  579. ret = lbs_get_tx_power(priv, &curlevel, &minlevel, &maxlevel);
  580. if (ret == 0) {
  581. priv->txpower_cur = curlevel;
  582. priv->txpower_min = minlevel;
  583. priv->txpower_max = maxlevel;
  584. }
  585. lbs_set_mac_control(priv);
  586. done:
  587. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  588. return ret;
  589. }
  590. /**
  591. * This function handles the timeout of command sending.
  592. * It will re-send the same command again.
  593. */
  594. static void lbs_cmd_timeout_handler(unsigned long data)
  595. {
  596. struct lbs_private *priv = (struct lbs_private *)data;
  597. unsigned long flags;
  598. lbs_deb_enter(LBS_DEB_CMD);
  599. spin_lock_irqsave(&priv->driver_lock, flags);
  600. if (!priv->cur_cmd)
  601. goto out;
  602. lbs_pr_info("command 0x%04x timed out\n",
  603. le16_to_cpu(priv->cur_cmd->cmdbuf->command));
  604. priv->cmd_timed_out = 1;
  605. wake_up_interruptible(&priv->waitq);
  606. out:
  607. spin_unlock_irqrestore(&priv->driver_lock, flags);
  608. lbs_deb_leave(LBS_DEB_CMD);
  609. }
  610. /**
  611. * This function put the device back to deep sleep mode when timer expires
  612. * and no activity (command, event, data etc.) is detected.
  613. */
  614. static void auto_deepsleep_timer_fn(unsigned long data)
  615. {
  616. struct lbs_private *priv = (struct lbs_private *)data;
  617. int ret;
  618. lbs_deb_enter(LBS_DEB_CMD);
  619. if (priv->is_activity_detected) {
  620. priv->is_activity_detected = 0;
  621. } else {
  622. if (priv->is_auto_deep_sleep_enabled &&
  623. (!priv->wakeup_dev_required) &&
  624. (priv->connect_status != LBS_CONNECTED)) {
  625. lbs_deb_main("Entering auto deep sleep mode...\n");
  626. ret = lbs_prepare_and_send_command(priv,
  627. CMD_802_11_DEEP_SLEEP, 0,
  628. 0, 0, NULL);
  629. if (ret)
  630. lbs_pr_err("Enter Deep Sleep command failed\n");
  631. }
  632. }
  633. mod_timer(&priv->auto_deepsleep_timer , jiffies +
  634. (priv->auto_deep_sleep_timeout * HZ)/1000);
  635. lbs_deb_leave(LBS_DEB_CMD);
  636. }
  637. int lbs_enter_auto_deep_sleep(struct lbs_private *priv)
  638. {
  639. lbs_deb_enter(LBS_DEB_SDIO);
  640. priv->is_auto_deep_sleep_enabled = 1;
  641. if (priv->is_deep_sleep)
  642. priv->wakeup_dev_required = 1;
  643. mod_timer(&priv->auto_deepsleep_timer ,
  644. jiffies + (priv->auto_deep_sleep_timeout * HZ)/1000);
  645. lbs_deb_leave(LBS_DEB_SDIO);
  646. return 0;
  647. }
  648. int lbs_exit_auto_deep_sleep(struct lbs_private *priv)
  649. {
  650. lbs_deb_enter(LBS_DEB_SDIO);
  651. priv->is_auto_deep_sleep_enabled = 0;
  652. priv->auto_deep_sleep_timeout = 0;
  653. del_timer(&priv->auto_deepsleep_timer);
  654. lbs_deb_leave(LBS_DEB_SDIO);
  655. return 0;
  656. }
  657. static int lbs_init_adapter(struct lbs_private *priv)
  658. {
  659. size_t bufsize;
  660. int i, ret = 0;
  661. lbs_deb_enter(LBS_DEB_MAIN);
  662. /* Allocate buffer to store the BSSID list */
  663. bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
  664. priv->networks = kzalloc(bufsize, GFP_KERNEL);
  665. if (!priv->networks) {
  666. lbs_pr_err("Out of memory allocating beacons\n");
  667. ret = -1;
  668. goto out;
  669. }
  670. /* Initialize scan result lists */
  671. INIT_LIST_HEAD(&priv->network_free_list);
  672. INIT_LIST_HEAD(&priv->network_list);
  673. for (i = 0; i < MAX_NETWORK_COUNT; i++) {
  674. list_add_tail(&priv->networks[i].list,
  675. &priv->network_free_list);
  676. }
  677. memset(priv->current_addr, 0xff, ETH_ALEN);
  678. priv->connect_status = LBS_DISCONNECTED;
  679. priv->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
  680. priv->mode = IW_MODE_INFRA;
  681. priv->channel = DEFAULT_AD_HOC_CHANNEL;
  682. priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
  683. priv->radio_on = 1;
  684. priv->enablehwauto = 1;
  685. priv->psmode = LBS802_11POWERMODECAM;
  686. priv->psstate = PS_STATE_FULL_POWER;
  687. priv->is_deep_sleep = 0;
  688. priv->is_auto_deep_sleep_enabled = 0;
  689. priv->wakeup_dev_required = 0;
  690. init_waitqueue_head(&priv->ds_awake_q);
  691. mutex_init(&priv->lock);
  692. setup_timer(&priv->command_timer, lbs_cmd_timeout_handler,
  693. (unsigned long)priv);
  694. setup_timer(&priv->auto_deepsleep_timer, auto_deepsleep_timer_fn,
  695. (unsigned long)priv);
  696. INIT_LIST_HEAD(&priv->cmdfreeq);
  697. INIT_LIST_HEAD(&priv->cmdpendingq);
  698. spin_lock_init(&priv->driver_lock);
  699. init_waitqueue_head(&priv->cmd_pending);
  700. /* Allocate the command buffers */
  701. if (lbs_allocate_cmd_buffer(priv)) {
  702. lbs_pr_err("Out of memory allocating command buffers\n");
  703. ret = -ENOMEM;
  704. goto out;
  705. }
  706. priv->resp_idx = 0;
  707. priv->resp_len[0] = priv->resp_len[1] = 0;
  708. /* Create the event FIFO */
  709. ret = kfifo_alloc(&priv->event_fifo, sizeof(u32) * 16, GFP_KERNEL);
  710. if (ret) {
  711. lbs_pr_err("Out of memory allocating event FIFO buffer\n");
  712. goto out;
  713. }
  714. out:
  715. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  716. return ret;
  717. }
  718. static void lbs_free_adapter(struct lbs_private *priv)
  719. {
  720. lbs_deb_enter(LBS_DEB_MAIN);
  721. lbs_free_cmd_buffer(priv);
  722. kfifo_free(&priv->event_fifo);
  723. del_timer(&priv->command_timer);
  724. del_timer(&priv->auto_deepsleep_timer);
  725. kfree(priv->networks);
  726. priv->networks = NULL;
  727. lbs_deb_leave(LBS_DEB_MAIN);
  728. }
  729. static const struct net_device_ops lbs_netdev_ops = {
  730. .ndo_open = lbs_dev_open,
  731. .ndo_stop = lbs_eth_stop,
  732. .ndo_start_xmit = lbs_hard_start_xmit,
  733. .ndo_set_mac_address = lbs_set_mac_address,
  734. .ndo_tx_timeout = lbs_tx_timeout,
  735. .ndo_set_multicast_list = lbs_set_multicast_list,
  736. .ndo_change_mtu = eth_change_mtu,
  737. .ndo_validate_addr = eth_validate_addr,
  738. };
  739. /**
  740. * @brief This function adds the card. it will probe the
  741. * card, allocate the lbs_priv and initialize the device.
  742. *
  743. * @param card A pointer to card
  744. * @return A pointer to struct lbs_private structure
  745. */
  746. struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
  747. {
  748. struct net_device *dev;
  749. struct wireless_dev *wdev;
  750. struct lbs_private *priv = NULL;
  751. lbs_deb_enter(LBS_DEB_MAIN);
  752. /* Allocate an Ethernet device and register it */
  753. wdev = lbs_cfg_alloc(dmdev);
  754. if (IS_ERR(wdev)) {
  755. lbs_pr_err("cfg80211 init failed\n");
  756. goto done;
  757. }
  758. /* TODO? */
  759. wdev->iftype = NL80211_IFTYPE_STATION;
  760. priv = wdev_priv(wdev);
  761. priv->wdev = wdev;
  762. if (lbs_init_adapter(priv)) {
  763. lbs_pr_err("failed to initialize adapter structure.\n");
  764. goto err_wdev;
  765. }
  766. //TODO? dev = alloc_netdev_mq(0, "wlan%d", ether_setup, IWM_TX_QUEUES);
  767. dev = alloc_netdev(0, "wlan%d", ether_setup);
  768. if (!dev) {
  769. dev_err(dmdev, "no memory for network device instance\n");
  770. goto err_adapter;
  771. }
  772. dev->ieee80211_ptr = wdev;
  773. dev->ml_priv = priv;
  774. SET_NETDEV_DEV(dev, dmdev);
  775. wdev->netdev = dev;
  776. priv->dev = dev;
  777. dev->netdev_ops = &lbs_netdev_ops;
  778. dev->watchdog_timeo = 5 * HZ;
  779. dev->ethtool_ops = &lbs_ethtool_ops;
  780. #ifdef WIRELESS_EXT
  781. dev->wireless_handlers = &lbs_handler_def;
  782. #endif
  783. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  784. // TODO: kzalloc + iwm_init_default_profile(iwm, iwm->umac_profile); ??
  785. priv->card = card;
  786. priv->infra_open = 0;
  787. priv->rtap_net_dev = NULL;
  788. strcpy(dev->name, "wlan%d");
  789. lbs_deb_thread("Starting main thread...\n");
  790. init_waitqueue_head(&priv->waitq);
  791. priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
  792. if (IS_ERR(priv->main_thread)) {
  793. lbs_deb_thread("Error creating main thread.\n");
  794. goto err_ndev;
  795. }
  796. priv->work_thread = create_singlethread_workqueue("lbs_worker");
  797. INIT_DELAYED_WORK(&priv->assoc_work, lbs_association_worker);
  798. INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
  799. INIT_WORK(&priv->mcast_work, lbs_set_mcast_worker);
  800. priv->wol_criteria = 0xffffffff;
  801. priv->wol_gpio = 0xff;
  802. goto done;
  803. err_ndev:
  804. free_netdev(dev);
  805. err_adapter:
  806. lbs_free_adapter(priv);
  807. err_wdev:
  808. lbs_cfg_free(priv);
  809. priv = NULL;
  810. done:
  811. lbs_deb_leave_args(LBS_DEB_MAIN, "priv %p", priv);
  812. return priv;
  813. }
  814. EXPORT_SYMBOL_GPL(lbs_add_card);
  815. void lbs_remove_card(struct lbs_private *priv)
  816. {
  817. struct net_device *dev = priv->dev;
  818. lbs_deb_enter(LBS_DEB_MAIN);
  819. lbs_remove_mesh(priv);
  820. lbs_remove_rtap(priv);
  821. dev = priv->dev;
  822. cancel_delayed_work_sync(&priv->scan_work);
  823. cancel_delayed_work_sync(&priv->assoc_work);
  824. cancel_work_sync(&priv->mcast_work);
  825. /* worker thread destruction blocks on the in-flight command which
  826. * should have been cleared already in lbs_stop_card().
  827. */
  828. lbs_deb_main("destroying worker thread\n");
  829. destroy_workqueue(priv->work_thread);
  830. lbs_deb_main("done destroying worker thread\n");
  831. if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
  832. priv->psmode = LBS802_11POWERMODECAM;
  833. lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
  834. }
  835. lbs_send_disconnect_notification(priv);
  836. if (priv->is_deep_sleep) {
  837. priv->is_deep_sleep = 0;
  838. wake_up_interruptible(&priv->ds_awake_q);
  839. }
  840. /* Stop the thread servicing the interrupts */
  841. priv->surpriseremoved = 1;
  842. kthread_stop(priv->main_thread);
  843. lbs_free_adapter(priv);
  844. lbs_cfg_free(priv);
  845. priv->dev = NULL;
  846. free_netdev(dev);
  847. lbs_deb_leave(LBS_DEB_MAIN);
  848. }
  849. EXPORT_SYMBOL_GPL(lbs_remove_card);
  850. static int lbs_rtap_supported(struct lbs_private *priv)
  851. {
  852. if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V5)
  853. return 1;
  854. /* newer firmware use a capability mask */
  855. return ((MRVL_FW_MAJOR_REV(priv->fwrelease) >= MRVL_FW_V10) &&
  856. (priv->fwcapinfo & MESH_CAPINFO_ENABLE_MASK));
  857. }
  858. int lbs_start_card(struct lbs_private *priv)
  859. {
  860. struct net_device *dev = priv->dev;
  861. int ret = -1;
  862. lbs_deb_enter(LBS_DEB_MAIN);
  863. /* poke the firmware */
  864. ret = lbs_setup_firmware(priv);
  865. if (ret)
  866. goto done;
  867. if (lbs_cfg_register(priv)) {
  868. lbs_pr_err("cannot register device\n");
  869. goto done;
  870. }
  871. lbs_update_channel(priv);
  872. lbs_init_mesh(priv);
  873. /*
  874. * While rtap isn't related to mesh, only mesh-enabled
  875. * firmware implements the rtap functionality via
  876. * CMD_802_11_MONITOR_MODE.
  877. */
  878. if (lbs_rtap_supported(priv)) {
  879. if (device_create_file(&dev->dev, &dev_attr_lbs_rtap))
  880. lbs_pr_err("cannot register lbs_rtap attribute\n");
  881. }
  882. lbs_debugfs_init_one(priv, dev);
  883. lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
  884. ret = 0;
  885. done:
  886. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  887. return ret;
  888. }
  889. EXPORT_SYMBOL_GPL(lbs_start_card);
  890. void lbs_stop_card(struct lbs_private *priv)
  891. {
  892. struct net_device *dev;
  893. struct cmd_ctrl_node *cmdnode;
  894. unsigned long flags;
  895. lbs_deb_enter(LBS_DEB_MAIN);
  896. if (!priv)
  897. goto out;
  898. dev = priv->dev;
  899. netif_stop_queue(dev);
  900. netif_carrier_off(dev);
  901. lbs_debugfs_remove_one(priv);
  902. lbs_deinit_mesh(priv);
  903. if (lbs_rtap_supported(priv))
  904. device_remove_file(&dev->dev, &dev_attr_lbs_rtap);
  905. /* Delete the timeout of the currently processing command */
  906. del_timer_sync(&priv->command_timer);
  907. del_timer_sync(&priv->auto_deepsleep_timer);
  908. /* Flush pending command nodes */
  909. spin_lock_irqsave(&priv->driver_lock, flags);
  910. lbs_deb_main("clearing pending commands\n");
  911. list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
  912. cmdnode->result = -ENOENT;
  913. cmdnode->cmdwaitqwoken = 1;
  914. wake_up_interruptible(&cmdnode->cmdwait_q);
  915. }
  916. /* Flush the command the card is currently processing */
  917. if (priv->cur_cmd) {
  918. lbs_deb_main("clearing current command\n");
  919. priv->cur_cmd->result = -ENOENT;
  920. priv->cur_cmd->cmdwaitqwoken = 1;
  921. wake_up_interruptible(&priv->cur_cmd->cmdwait_q);
  922. }
  923. lbs_deb_main("done clearing commands\n");
  924. spin_unlock_irqrestore(&priv->driver_lock, flags);
  925. unregister_netdev(dev);
  926. out:
  927. lbs_deb_leave(LBS_DEB_MAIN);
  928. }
  929. EXPORT_SYMBOL_GPL(lbs_stop_card);
  930. void lbs_queue_event(struct lbs_private *priv, u32 event)
  931. {
  932. unsigned long flags;
  933. lbs_deb_enter(LBS_DEB_THREAD);
  934. spin_lock_irqsave(&priv->driver_lock, flags);
  935. if (priv->psstate == PS_STATE_SLEEP)
  936. priv->psstate = PS_STATE_AWAKE;
  937. kfifo_in(&priv->event_fifo, (unsigned char *) &event, sizeof(u32));
  938. wake_up_interruptible(&priv->waitq);
  939. spin_unlock_irqrestore(&priv->driver_lock, flags);
  940. lbs_deb_leave(LBS_DEB_THREAD);
  941. }
  942. EXPORT_SYMBOL_GPL(lbs_queue_event);
  943. void lbs_notify_command_response(struct lbs_private *priv, u8 resp_idx)
  944. {
  945. lbs_deb_enter(LBS_DEB_THREAD);
  946. if (priv->psstate == PS_STATE_SLEEP)
  947. priv->psstate = PS_STATE_AWAKE;
  948. /* Swap buffers by flipping the response index */
  949. BUG_ON(resp_idx > 1);
  950. priv->resp_idx = resp_idx;
  951. wake_up_interruptible(&priv->waitq);
  952. lbs_deb_leave(LBS_DEB_THREAD);
  953. }
  954. EXPORT_SYMBOL_GPL(lbs_notify_command_response);
  955. static int __init lbs_init_module(void)
  956. {
  957. lbs_deb_enter(LBS_DEB_MAIN);
  958. memset(&confirm_sleep, 0, sizeof(confirm_sleep));
  959. confirm_sleep.hdr.command = cpu_to_le16(CMD_802_11_PS_MODE);
  960. confirm_sleep.hdr.size = cpu_to_le16(sizeof(confirm_sleep));
  961. confirm_sleep.action = cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);
  962. lbs_debugfs_init();
  963. lbs_deb_leave(LBS_DEB_MAIN);
  964. return 0;
  965. }
  966. static void __exit lbs_exit_module(void)
  967. {
  968. lbs_deb_enter(LBS_DEB_MAIN);
  969. lbs_debugfs_remove();
  970. lbs_deb_leave(LBS_DEB_MAIN);
  971. }
  972. /*
  973. * rtap interface support fuctions
  974. */
  975. static int lbs_rtap_open(struct net_device *dev)
  976. {
  977. /* Yes, _stop_ the queue. Because we don't support injection */
  978. lbs_deb_enter(LBS_DEB_MAIN);
  979. netif_carrier_off(dev);
  980. netif_stop_queue(dev);
  981. lbs_deb_leave(LBS_DEB_LEAVE);
  982. return 0;
  983. }
  984. static int lbs_rtap_stop(struct net_device *dev)
  985. {
  986. lbs_deb_enter(LBS_DEB_MAIN);
  987. lbs_deb_leave(LBS_DEB_MAIN);
  988. return 0;
  989. }
  990. static netdev_tx_t lbs_rtap_hard_start_xmit(struct sk_buff *skb,
  991. struct net_device *dev)
  992. {
  993. netif_stop_queue(dev);
  994. return NETDEV_TX_BUSY;
  995. }
  996. static void lbs_remove_rtap(struct lbs_private *priv)
  997. {
  998. lbs_deb_enter(LBS_DEB_MAIN);
  999. if (priv->rtap_net_dev == NULL)
  1000. goto out;
  1001. unregister_netdev(priv->rtap_net_dev);
  1002. free_netdev(priv->rtap_net_dev);
  1003. priv->rtap_net_dev = NULL;
  1004. out:
  1005. lbs_deb_leave(LBS_DEB_MAIN);
  1006. }
  1007. static const struct net_device_ops rtap_netdev_ops = {
  1008. .ndo_open = lbs_rtap_open,
  1009. .ndo_stop = lbs_rtap_stop,
  1010. .ndo_start_xmit = lbs_rtap_hard_start_xmit,
  1011. };
  1012. static int lbs_add_rtap(struct lbs_private *priv)
  1013. {
  1014. int ret = 0;
  1015. struct net_device *rtap_dev;
  1016. lbs_deb_enter(LBS_DEB_MAIN);
  1017. if (priv->rtap_net_dev) {
  1018. ret = -EPERM;
  1019. goto out;
  1020. }
  1021. rtap_dev = alloc_netdev(0, "rtap%d", ether_setup);
  1022. if (rtap_dev == NULL) {
  1023. ret = -ENOMEM;
  1024. goto out;
  1025. }
  1026. memcpy(rtap_dev->dev_addr, priv->current_addr, ETH_ALEN);
  1027. rtap_dev->type = ARPHRD_IEEE80211_RADIOTAP;
  1028. rtap_dev->netdev_ops = &rtap_netdev_ops;
  1029. rtap_dev->ml_priv = priv;
  1030. SET_NETDEV_DEV(rtap_dev, priv->dev->dev.parent);
  1031. ret = register_netdev(rtap_dev);
  1032. if (ret) {
  1033. free_netdev(rtap_dev);
  1034. goto out;
  1035. }
  1036. priv->rtap_net_dev = rtap_dev;
  1037. out:
  1038. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1039. return ret;
  1040. }
  1041. module_init(lbs_init_module);
  1042. module_exit(lbs_exit_module);
  1043. MODULE_DESCRIPTION("Libertas WLAN Driver Library");
  1044. MODULE_AUTHOR("Marvell International Ltd.");
  1045. MODULE_LICENSE("GPL");