main.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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. /* More often than not, this actually happens because the
  128. firmware has crapped itself -- rather than just a very
  129. busy medium. So send a harmless command, and if/when
  130. _that_ times out, we'll kick it in the head. */
  131. lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
  132. 0, 0, NULL);
  133. lbs_deb_leave(LBS_DEB_TX);
  134. }
  135. void lbs_host_to_card_done(struct lbs_private *priv)
  136. {
  137. unsigned long flags;
  138. lbs_deb_enter(LBS_DEB_THREAD);
  139. spin_lock_irqsave(&priv->driver_lock, flags);
  140. priv->dnld_sent = DNLD_RES_RECEIVED;
  141. /* Wake main thread if commands are pending */
  142. if (!priv->cur_cmd || priv->tx_pending_len > 0) {
  143. if (!priv->wakeup_dev_required)
  144. wake_up_interruptible(&priv->waitq);
  145. }
  146. spin_unlock_irqrestore(&priv->driver_lock, flags);
  147. lbs_deb_leave(LBS_DEB_THREAD);
  148. }
  149. EXPORT_SYMBOL_GPL(lbs_host_to_card_done);
  150. int lbs_set_mac_address(struct net_device *dev, void *addr)
  151. {
  152. int ret = 0;
  153. struct lbs_private *priv = dev->ml_priv;
  154. struct sockaddr *phwaddr = addr;
  155. struct cmd_ds_802_11_mac_address cmd;
  156. lbs_deb_enter(LBS_DEB_NET);
  157. /* In case it was called from the mesh device */
  158. dev = priv->dev;
  159. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  160. cmd.action = cpu_to_le16(CMD_ACT_SET);
  161. memcpy(cmd.macadd, phwaddr->sa_data, ETH_ALEN);
  162. ret = lbs_cmd_with_response(priv, CMD_802_11_MAC_ADDRESS, &cmd);
  163. if (ret) {
  164. lbs_deb_net("set MAC address failed\n");
  165. goto done;
  166. }
  167. memcpy(priv->current_addr, phwaddr->sa_data, ETH_ALEN);
  168. memcpy(dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
  169. if (priv->mesh_dev)
  170. memcpy(priv->mesh_dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
  171. done:
  172. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  173. return ret;
  174. }
  175. static inline int mac_in_list(unsigned char *list, int list_len,
  176. unsigned char *mac)
  177. {
  178. while (list_len) {
  179. if (!memcmp(list, mac, ETH_ALEN))
  180. return 1;
  181. list += ETH_ALEN;
  182. list_len--;
  183. }
  184. return 0;
  185. }
  186. static int lbs_add_mcast_addrs(struct cmd_ds_mac_multicast_adr *cmd,
  187. struct net_device *dev, int nr_addrs)
  188. {
  189. int i = nr_addrs;
  190. struct netdev_hw_addr *ha;
  191. int cnt;
  192. if ((dev->flags & (IFF_UP|IFF_MULTICAST)) != (IFF_UP|IFF_MULTICAST))
  193. return nr_addrs;
  194. netif_addr_lock_bh(dev);
  195. cnt = netdev_mc_count(dev);
  196. netdev_for_each_mc_addr(ha, dev) {
  197. if (mac_in_list(cmd->maclist, nr_addrs, ha->addr)) {
  198. lbs_deb_net("mcast address %s:%pM skipped\n", dev->name,
  199. ha->addr);
  200. cnt--;
  201. continue;
  202. }
  203. if (i == MRVDRV_MAX_MULTICAST_LIST_SIZE)
  204. break;
  205. memcpy(&cmd->maclist[6*i], ha->addr, ETH_ALEN);
  206. lbs_deb_net("mcast address %s:%pM added to filter\n", dev->name,
  207. ha->addr);
  208. i++;
  209. cnt--;
  210. }
  211. netif_addr_unlock_bh(dev);
  212. if (cnt)
  213. return -EOVERFLOW;
  214. return i;
  215. }
  216. static void lbs_set_mcast_worker(struct work_struct *work)
  217. {
  218. struct lbs_private *priv = container_of(work, struct lbs_private, mcast_work);
  219. struct cmd_ds_mac_multicast_adr mcast_cmd;
  220. int dev_flags;
  221. int nr_addrs;
  222. int old_mac_control = priv->mac_control;
  223. lbs_deb_enter(LBS_DEB_NET);
  224. dev_flags = priv->dev->flags;
  225. if (priv->mesh_dev)
  226. dev_flags |= priv->mesh_dev->flags;
  227. if (dev_flags & IFF_PROMISC) {
  228. priv->mac_control |= CMD_ACT_MAC_PROMISCUOUS_ENABLE;
  229. priv->mac_control &= ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
  230. CMD_ACT_MAC_MULTICAST_ENABLE);
  231. goto out_set_mac_control;
  232. } else if (dev_flags & IFF_ALLMULTI) {
  233. do_allmulti:
  234. priv->mac_control |= CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
  235. priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
  236. CMD_ACT_MAC_MULTICAST_ENABLE);
  237. goto out_set_mac_control;
  238. }
  239. /* Once for priv->dev, again for priv->mesh_dev if it exists */
  240. nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->dev, 0);
  241. if (nr_addrs >= 0 && priv->mesh_dev)
  242. nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->mesh_dev, nr_addrs);
  243. if (nr_addrs < 0)
  244. goto do_allmulti;
  245. if (nr_addrs) {
  246. int size = offsetof(struct cmd_ds_mac_multicast_adr,
  247. maclist[6*nr_addrs]);
  248. mcast_cmd.action = cpu_to_le16(CMD_ACT_SET);
  249. mcast_cmd.hdr.size = cpu_to_le16(size);
  250. mcast_cmd.nr_of_adrs = cpu_to_le16(nr_addrs);
  251. lbs_cmd_async(priv, CMD_MAC_MULTICAST_ADR, &mcast_cmd.hdr, size);
  252. priv->mac_control |= CMD_ACT_MAC_MULTICAST_ENABLE;
  253. } else
  254. priv->mac_control &= ~CMD_ACT_MAC_MULTICAST_ENABLE;
  255. priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
  256. CMD_ACT_MAC_ALL_MULTICAST_ENABLE);
  257. out_set_mac_control:
  258. if (priv->mac_control != old_mac_control)
  259. lbs_set_mac_control(priv);
  260. lbs_deb_leave(LBS_DEB_NET);
  261. }
  262. void lbs_set_multicast_list(struct net_device *dev)
  263. {
  264. struct lbs_private *priv = dev->ml_priv;
  265. schedule_work(&priv->mcast_work);
  266. }
  267. /**
  268. * @brief This function handles the major jobs in the LBS driver.
  269. * It handles all events generated by firmware, RX data received
  270. * from firmware and TX data sent from kernel.
  271. *
  272. * @param data A pointer to lbs_thread structure
  273. * @return 0
  274. */
  275. static int lbs_thread(void *data)
  276. {
  277. struct net_device *dev = data;
  278. struct lbs_private *priv = dev->ml_priv;
  279. wait_queue_t wait;
  280. lbs_deb_enter(LBS_DEB_THREAD);
  281. init_waitqueue_entry(&wait, current);
  282. for (;;) {
  283. int shouldsleep;
  284. u8 resp_idx;
  285. lbs_deb_thread("1: currenttxskb %p, dnld_sent %d\n",
  286. priv->currenttxskb, priv->dnld_sent);
  287. add_wait_queue(&priv->waitq, &wait);
  288. set_current_state(TASK_INTERRUPTIBLE);
  289. spin_lock_irq(&priv->driver_lock);
  290. if (kthread_should_stop())
  291. shouldsleep = 0; /* Bye */
  292. else if (priv->surpriseremoved)
  293. shouldsleep = 1; /* We need to wait until we're _told_ to die */
  294. else if (priv->psstate == PS_STATE_SLEEP)
  295. shouldsleep = 1; /* Sleep mode. Nothing we can do till it wakes */
  296. else if (priv->cmd_timed_out)
  297. shouldsleep = 0; /* Command timed out. Recover */
  298. else if (!priv->fw_ready)
  299. shouldsleep = 1; /* Firmware not ready. We're waiting for it */
  300. else if (priv->dnld_sent)
  301. shouldsleep = 1; /* Something is en route to the device already */
  302. else if (priv->tx_pending_len > 0)
  303. shouldsleep = 0; /* We've a packet to send */
  304. else if (priv->resp_len[priv->resp_idx])
  305. shouldsleep = 0; /* We have a command response */
  306. else if (priv->cur_cmd)
  307. shouldsleep = 1; /* Can't send a command; one already running */
  308. else if (!list_empty(&priv->cmdpendingq) &&
  309. !(priv->wakeup_dev_required))
  310. shouldsleep = 0; /* We have a command to send */
  311. else if (kfifo_len(&priv->event_fifo))
  312. shouldsleep = 0; /* We have an event to process */
  313. else
  314. shouldsleep = 1; /* No command */
  315. if (shouldsleep) {
  316. lbs_deb_thread("sleeping, connect_status %d, "
  317. "psmode %d, psstate %d\n",
  318. priv->connect_status,
  319. priv->psmode, priv->psstate);
  320. spin_unlock_irq(&priv->driver_lock);
  321. schedule();
  322. } else
  323. spin_unlock_irq(&priv->driver_lock);
  324. lbs_deb_thread("2: currenttxskb %p, dnld_send %d\n",
  325. priv->currenttxskb, priv->dnld_sent);
  326. set_current_state(TASK_RUNNING);
  327. remove_wait_queue(&priv->waitq, &wait);
  328. lbs_deb_thread("3: currenttxskb %p, dnld_sent %d\n",
  329. priv->currenttxskb, priv->dnld_sent);
  330. if (kthread_should_stop()) {
  331. lbs_deb_thread("break from main thread\n");
  332. break;
  333. }
  334. if (priv->surpriseremoved) {
  335. lbs_deb_thread("adapter removed; waiting to die...\n");
  336. continue;
  337. }
  338. lbs_deb_thread("4: currenttxskb %p, dnld_sent %d\n",
  339. priv->currenttxskb, priv->dnld_sent);
  340. /* Process any pending command response */
  341. spin_lock_irq(&priv->driver_lock);
  342. resp_idx = priv->resp_idx;
  343. if (priv->resp_len[resp_idx]) {
  344. spin_unlock_irq(&priv->driver_lock);
  345. lbs_process_command_response(priv,
  346. priv->resp_buf[resp_idx],
  347. priv->resp_len[resp_idx]);
  348. spin_lock_irq(&priv->driver_lock);
  349. priv->resp_len[resp_idx] = 0;
  350. }
  351. spin_unlock_irq(&priv->driver_lock);
  352. /* Process hardware events, e.g. card removed, link lost */
  353. spin_lock_irq(&priv->driver_lock);
  354. while (kfifo_len(&priv->event_fifo)) {
  355. u32 event;
  356. if (kfifo_out(&priv->event_fifo,
  357. (unsigned char *) &event, sizeof(event)) !=
  358. sizeof(event))
  359. break;
  360. spin_unlock_irq(&priv->driver_lock);
  361. lbs_process_event(priv, event);
  362. spin_lock_irq(&priv->driver_lock);
  363. }
  364. spin_unlock_irq(&priv->driver_lock);
  365. if (priv->wakeup_dev_required) {
  366. lbs_deb_thread("Waking up device...\n");
  367. /* Wake up device */
  368. if (priv->exit_deep_sleep(priv))
  369. lbs_deb_thread("Wakeup device failed\n");
  370. continue;
  371. }
  372. /* command timeout stuff */
  373. if (priv->cmd_timed_out && priv->cur_cmd) {
  374. struct cmd_ctrl_node *cmdnode = priv->cur_cmd;
  375. lbs_pr_info("Timeout submitting command 0x%04x\n",
  376. le16_to_cpu(cmdnode->cmdbuf->command));
  377. lbs_complete_command(priv, cmdnode, -ETIMEDOUT);
  378. if (priv->reset_card)
  379. priv->reset_card(priv);
  380. }
  381. priv->cmd_timed_out = 0;
  382. if (!priv->fw_ready)
  383. continue;
  384. /* Check if we need to confirm Sleep Request received previously */
  385. if (priv->psstate == PS_STATE_PRE_SLEEP &&
  386. !priv->dnld_sent && !priv->cur_cmd) {
  387. if (priv->connect_status == LBS_CONNECTED) {
  388. lbs_deb_thread("pre-sleep, currenttxskb %p, "
  389. "dnld_sent %d, cur_cmd %p\n",
  390. priv->currenttxskb, priv->dnld_sent,
  391. priv->cur_cmd);
  392. lbs_ps_confirm_sleep(priv);
  393. } else {
  394. /* workaround for firmware sending
  395. * deauth/linkloss event immediately
  396. * after sleep request; remove this
  397. * after firmware fixes it
  398. */
  399. priv->psstate = PS_STATE_AWAKE;
  400. lbs_pr_alert("ignore PS_SleepConfirm in "
  401. "non-connected state\n");
  402. }
  403. }
  404. /* The PS state is changed during processing of Sleep Request
  405. * event above
  406. */
  407. if ((priv->psstate == PS_STATE_SLEEP) ||
  408. (priv->psstate == PS_STATE_PRE_SLEEP))
  409. continue;
  410. if (priv->is_deep_sleep)
  411. continue;
  412. /* Execute the next command */
  413. if (!priv->dnld_sent && !priv->cur_cmd)
  414. lbs_execute_next_command(priv);
  415. /* Wake-up command waiters which can't sleep in
  416. * lbs_prepare_and_send_command
  417. */
  418. if (!list_empty(&priv->cmdpendingq))
  419. wake_up_all(&priv->cmd_pending);
  420. spin_lock_irq(&priv->driver_lock);
  421. if (!priv->dnld_sent && priv->tx_pending_len > 0) {
  422. int ret = priv->hw_host_to_card(priv, MVMS_DAT,
  423. priv->tx_pending_buf,
  424. priv->tx_pending_len);
  425. if (ret) {
  426. lbs_deb_tx("host_to_card failed %d\n", ret);
  427. priv->dnld_sent = DNLD_RES_RECEIVED;
  428. }
  429. priv->tx_pending_len = 0;
  430. if (!priv->currenttxskb) {
  431. /* We can wake the queues immediately if we aren't
  432. waiting for TX feedback */
  433. if (priv->connect_status == LBS_CONNECTED)
  434. netif_wake_queue(priv->dev);
  435. if (priv->mesh_dev &&
  436. lbs_mesh_connected(priv))
  437. netif_wake_queue(priv->mesh_dev);
  438. }
  439. }
  440. spin_unlock_irq(&priv->driver_lock);
  441. }
  442. del_timer(&priv->command_timer);
  443. del_timer(&priv->auto_deepsleep_timer);
  444. wake_up_all(&priv->cmd_pending);
  445. lbs_deb_leave(LBS_DEB_THREAD);
  446. return 0;
  447. }
  448. static int lbs_ret_host_sleep_activate(struct lbs_private *priv,
  449. unsigned long dummy,
  450. struct cmd_header *cmd)
  451. {
  452. lbs_deb_enter(LBS_DEB_FW);
  453. priv->is_host_sleep_activated = 1;
  454. wake_up_interruptible(&priv->host_sleep_q);
  455. lbs_deb_leave(LBS_DEB_FW);
  456. return 0;
  457. }
  458. int lbs_suspend(struct lbs_private *priv)
  459. {
  460. struct cmd_header cmd;
  461. int ret;
  462. lbs_deb_enter(LBS_DEB_FW);
  463. if (priv->is_deep_sleep) {
  464. ret = lbs_set_deep_sleep(priv, 0);
  465. if (ret) {
  466. lbs_pr_err("deep sleep cancellation failed: %d\n", ret);
  467. return ret;
  468. }
  469. priv->deep_sleep_required = 1;
  470. }
  471. memset(&cmd, 0, sizeof(cmd));
  472. ret = lbs_host_sleep_cfg(priv, priv->wol_criteria,
  473. (struct wol_config *)NULL);
  474. if (ret) {
  475. lbs_pr_info("Host sleep configuration failed: %d\n", ret);
  476. return ret;
  477. }
  478. if (priv->psstate == PS_STATE_FULL_POWER) {
  479. ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_ACTIVATE, &cmd,
  480. sizeof(cmd), lbs_ret_host_sleep_activate, 0);
  481. if (ret)
  482. lbs_pr_info("HOST_SLEEP_ACTIVATE failed: %d\n", ret);
  483. }
  484. if (!wait_event_interruptible_timeout(priv->host_sleep_q,
  485. priv->is_host_sleep_activated, (10 * HZ))) {
  486. lbs_pr_err("host_sleep_q: timer expired\n");
  487. ret = -1;
  488. }
  489. netif_device_detach(priv->dev);
  490. if (priv->mesh_dev)
  491. netif_device_detach(priv->mesh_dev);
  492. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  493. return ret;
  494. }
  495. EXPORT_SYMBOL_GPL(lbs_suspend);
  496. int lbs_resume(struct lbs_private *priv)
  497. {
  498. int ret;
  499. uint32_t criteria = EHS_REMOVE_WAKEUP;
  500. lbs_deb_enter(LBS_DEB_FW);
  501. ret = lbs_host_sleep_cfg(priv, criteria, (struct wol_config *)NULL);
  502. netif_device_attach(priv->dev);
  503. if (priv->mesh_dev)
  504. netif_device_attach(priv->mesh_dev);
  505. if (priv->deep_sleep_required) {
  506. priv->deep_sleep_required = 0;
  507. ret = lbs_set_deep_sleep(priv, 1);
  508. if (ret)
  509. lbs_pr_err("deep sleep activation failed: %d\n", ret);
  510. }
  511. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  512. return ret;
  513. }
  514. EXPORT_SYMBOL_GPL(lbs_resume);
  515. /**
  516. * @brief This function gets the HW spec from the firmware and sets
  517. * some basic parameters.
  518. *
  519. * @param priv A pointer to struct lbs_private structure
  520. * @return 0 or -1
  521. */
  522. static int lbs_setup_firmware(struct lbs_private *priv)
  523. {
  524. int ret = -1;
  525. s16 curlevel = 0, minlevel = 0, maxlevel = 0;
  526. lbs_deb_enter(LBS_DEB_FW);
  527. /* Read MAC address from firmware */
  528. memset(priv->current_addr, 0xff, ETH_ALEN);
  529. ret = lbs_update_hw_spec(priv);
  530. if (ret)
  531. goto done;
  532. /* Read power levels if available */
  533. ret = lbs_get_tx_power(priv, &curlevel, &minlevel, &maxlevel);
  534. if (ret == 0) {
  535. priv->txpower_cur = curlevel;
  536. priv->txpower_min = minlevel;
  537. priv->txpower_max = maxlevel;
  538. }
  539. /* Send cmd to FW to enable 11D function */
  540. ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_11D_ENABLE, 1);
  541. lbs_set_mac_control(priv);
  542. done:
  543. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  544. return ret;
  545. }
  546. /**
  547. * This function handles the timeout of command sending.
  548. * It will re-send the same command again.
  549. */
  550. static void lbs_cmd_timeout_handler(unsigned long data)
  551. {
  552. struct lbs_private *priv = (struct lbs_private *)data;
  553. unsigned long flags;
  554. lbs_deb_enter(LBS_DEB_CMD);
  555. spin_lock_irqsave(&priv->driver_lock, flags);
  556. if (!priv->cur_cmd)
  557. goto out;
  558. lbs_pr_info("command 0x%04x timed out\n",
  559. le16_to_cpu(priv->cur_cmd->cmdbuf->command));
  560. priv->cmd_timed_out = 1;
  561. wake_up_interruptible(&priv->waitq);
  562. out:
  563. spin_unlock_irqrestore(&priv->driver_lock, flags);
  564. lbs_deb_leave(LBS_DEB_CMD);
  565. }
  566. /**
  567. * This function put the device back to deep sleep mode when timer expires
  568. * and no activity (command, event, data etc.) is detected.
  569. */
  570. static void auto_deepsleep_timer_fn(unsigned long data)
  571. {
  572. struct lbs_private *priv = (struct lbs_private *)data;
  573. int ret;
  574. lbs_deb_enter(LBS_DEB_CMD);
  575. if (priv->is_activity_detected) {
  576. priv->is_activity_detected = 0;
  577. } else {
  578. if (priv->is_auto_deep_sleep_enabled &&
  579. (!priv->wakeup_dev_required) &&
  580. (priv->connect_status != LBS_CONNECTED)) {
  581. lbs_deb_main("Entering auto deep sleep mode...\n");
  582. ret = lbs_prepare_and_send_command(priv,
  583. CMD_802_11_DEEP_SLEEP, 0,
  584. 0, 0, NULL);
  585. if (ret)
  586. lbs_pr_err("Enter Deep Sleep command failed\n");
  587. }
  588. }
  589. mod_timer(&priv->auto_deepsleep_timer , jiffies +
  590. (priv->auto_deep_sleep_timeout * HZ)/1000);
  591. lbs_deb_leave(LBS_DEB_CMD);
  592. }
  593. int lbs_enter_auto_deep_sleep(struct lbs_private *priv)
  594. {
  595. lbs_deb_enter(LBS_DEB_SDIO);
  596. priv->is_auto_deep_sleep_enabled = 1;
  597. if (priv->is_deep_sleep)
  598. priv->wakeup_dev_required = 1;
  599. mod_timer(&priv->auto_deepsleep_timer ,
  600. jiffies + (priv->auto_deep_sleep_timeout * HZ)/1000);
  601. lbs_deb_leave(LBS_DEB_SDIO);
  602. return 0;
  603. }
  604. int lbs_exit_auto_deep_sleep(struct lbs_private *priv)
  605. {
  606. lbs_deb_enter(LBS_DEB_SDIO);
  607. priv->is_auto_deep_sleep_enabled = 0;
  608. priv->auto_deep_sleep_timeout = 0;
  609. del_timer(&priv->auto_deepsleep_timer);
  610. lbs_deb_leave(LBS_DEB_SDIO);
  611. return 0;
  612. }
  613. static int lbs_init_adapter(struct lbs_private *priv)
  614. {
  615. int ret;
  616. lbs_deb_enter(LBS_DEB_MAIN);
  617. memset(priv->current_addr, 0xff, ETH_ALEN);
  618. priv->connect_status = LBS_DISCONNECTED;
  619. priv->channel = DEFAULT_AD_HOC_CHANNEL;
  620. priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
  621. priv->radio_on = 1;
  622. priv->psmode = LBS802_11POWERMODECAM;
  623. priv->psstate = PS_STATE_FULL_POWER;
  624. priv->is_deep_sleep = 0;
  625. priv->is_auto_deep_sleep_enabled = 0;
  626. priv->deep_sleep_required = 0;
  627. priv->wakeup_dev_required = 0;
  628. init_waitqueue_head(&priv->ds_awake_q);
  629. priv->authtype_auto = 1;
  630. priv->is_host_sleep_configured = 0;
  631. priv->is_host_sleep_activated = 0;
  632. init_waitqueue_head(&priv->host_sleep_q);
  633. mutex_init(&priv->lock);
  634. setup_timer(&priv->command_timer, lbs_cmd_timeout_handler,
  635. (unsigned long)priv);
  636. setup_timer(&priv->auto_deepsleep_timer, auto_deepsleep_timer_fn,
  637. (unsigned long)priv);
  638. INIT_LIST_HEAD(&priv->cmdfreeq);
  639. INIT_LIST_HEAD(&priv->cmdpendingq);
  640. spin_lock_init(&priv->driver_lock);
  641. init_waitqueue_head(&priv->cmd_pending);
  642. /* Allocate the command buffers */
  643. if (lbs_allocate_cmd_buffer(priv)) {
  644. lbs_pr_err("Out of memory allocating command buffers\n");
  645. ret = -ENOMEM;
  646. goto out;
  647. }
  648. priv->resp_idx = 0;
  649. priv->resp_len[0] = priv->resp_len[1] = 0;
  650. /* Create the event FIFO */
  651. ret = kfifo_alloc(&priv->event_fifo, sizeof(u32) * 16, GFP_KERNEL);
  652. if (ret) {
  653. lbs_pr_err("Out of memory allocating event FIFO buffer\n");
  654. goto out;
  655. }
  656. out:
  657. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  658. return ret;
  659. }
  660. static void lbs_free_adapter(struct lbs_private *priv)
  661. {
  662. lbs_deb_enter(LBS_DEB_MAIN);
  663. lbs_free_cmd_buffer(priv);
  664. kfifo_free(&priv->event_fifo);
  665. del_timer(&priv->command_timer);
  666. del_timer(&priv->auto_deepsleep_timer);
  667. lbs_deb_leave(LBS_DEB_MAIN);
  668. }
  669. static const struct net_device_ops lbs_netdev_ops = {
  670. .ndo_open = lbs_dev_open,
  671. .ndo_stop = lbs_eth_stop,
  672. .ndo_start_xmit = lbs_hard_start_xmit,
  673. .ndo_set_mac_address = lbs_set_mac_address,
  674. .ndo_tx_timeout = lbs_tx_timeout,
  675. .ndo_set_multicast_list = lbs_set_multicast_list,
  676. .ndo_change_mtu = eth_change_mtu,
  677. .ndo_validate_addr = eth_validate_addr,
  678. };
  679. /**
  680. * @brief This function adds the card. it will probe the
  681. * card, allocate the lbs_priv and initialize the device.
  682. *
  683. * @param card A pointer to card
  684. * @return A pointer to struct lbs_private structure
  685. */
  686. struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
  687. {
  688. struct net_device *dev;
  689. struct wireless_dev *wdev;
  690. struct lbs_private *priv = NULL;
  691. lbs_deb_enter(LBS_DEB_MAIN);
  692. /* Allocate an Ethernet device and register it */
  693. wdev = lbs_cfg_alloc(dmdev);
  694. if (IS_ERR(wdev)) {
  695. lbs_pr_err("cfg80211 init failed\n");
  696. goto done;
  697. }
  698. wdev->iftype = NL80211_IFTYPE_STATION;
  699. priv = wdev_priv(wdev);
  700. priv->wdev = wdev;
  701. if (lbs_init_adapter(priv)) {
  702. lbs_pr_err("failed to initialize adapter structure.\n");
  703. goto err_wdev;
  704. }
  705. dev = alloc_netdev(0, "wlan%d", ether_setup);
  706. if (!dev) {
  707. dev_err(dmdev, "no memory for network device instance\n");
  708. goto err_adapter;
  709. }
  710. dev->ieee80211_ptr = wdev;
  711. dev->ml_priv = priv;
  712. SET_NETDEV_DEV(dev, dmdev);
  713. wdev->netdev = dev;
  714. priv->dev = dev;
  715. dev->netdev_ops = &lbs_netdev_ops;
  716. dev->watchdog_timeo = 5 * HZ;
  717. dev->ethtool_ops = &lbs_ethtool_ops;
  718. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  719. priv->card = card;
  720. strcpy(dev->name, "wlan%d");
  721. lbs_deb_thread("Starting main thread...\n");
  722. init_waitqueue_head(&priv->waitq);
  723. priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
  724. if (IS_ERR(priv->main_thread)) {
  725. lbs_deb_thread("Error creating main thread.\n");
  726. goto err_ndev;
  727. }
  728. priv->work_thread = create_singlethread_workqueue("lbs_worker");
  729. INIT_WORK(&priv->mcast_work, lbs_set_mcast_worker);
  730. priv->wol_criteria = 0xffffffff;
  731. priv->wol_gpio = 0xff;
  732. priv->wol_gap = 20;
  733. goto done;
  734. err_ndev:
  735. free_netdev(dev);
  736. err_adapter:
  737. lbs_free_adapter(priv);
  738. err_wdev:
  739. lbs_cfg_free(priv);
  740. priv = NULL;
  741. done:
  742. lbs_deb_leave_args(LBS_DEB_MAIN, "priv %p", priv);
  743. return priv;
  744. }
  745. EXPORT_SYMBOL_GPL(lbs_add_card);
  746. void lbs_remove_card(struct lbs_private *priv)
  747. {
  748. struct net_device *dev = priv->dev;
  749. lbs_deb_enter(LBS_DEB_MAIN);
  750. lbs_remove_mesh(priv);
  751. lbs_scan_deinit(priv);
  752. dev = priv->dev;
  753. cancel_work_sync(&priv->mcast_work);
  754. /* worker thread destruction blocks on the in-flight command which
  755. * should have been cleared already in lbs_stop_card().
  756. */
  757. lbs_deb_main("destroying worker thread\n");
  758. destroy_workqueue(priv->work_thread);
  759. lbs_deb_main("done destroying worker thread\n");
  760. if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
  761. priv->psmode = LBS802_11POWERMODECAM;
  762. lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
  763. }
  764. if (priv->is_deep_sleep) {
  765. priv->is_deep_sleep = 0;
  766. wake_up_interruptible(&priv->ds_awake_q);
  767. }
  768. priv->is_host_sleep_configured = 0;
  769. priv->is_host_sleep_activated = 0;
  770. wake_up_interruptible(&priv->host_sleep_q);
  771. /* Stop the thread servicing the interrupts */
  772. priv->surpriseremoved = 1;
  773. kthread_stop(priv->main_thread);
  774. lbs_free_adapter(priv);
  775. lbs_cfg_free(priv);
  776. priv->dev = NULL;
  777. free_netdev(dev);
  778. lbs_deb_leave(LBS_DEB_MAIN);
  779. }
  780. EXPORT_SYMBOL_GPL(lbs_remove_card);
  781. int lbs_rtap_supported(struct lbs_private *priv)
  782. {
  783. if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V5)
  784. return 1;
  785. /* newer firmware use a capability mask */
  786. return ((MRVL_FW_MAJOR_REV(priv->fwrelease) >= MRVL_FW_V10) &&
  787. (priv->fwcapinfo & MESH_CAPINFO_ENABLE_MASK));
  788. }
  789. int lbs_start_card(struct lbs_private *priv)
  790. {
  791. struct net_device *dev = priv->dev;
  792. int ret = -1;
  793. lbs_deb_enter(LBS_DEB_MAIN);
  794. /* poke the firmware */
  795. ret = lbs_setup_firmware(priv);
  796. if (ret)
  797. goto done;
  798. if (lbs_cfg_register(priv)) {
  799. lbs_pr_err("cannot register device\n");
  800. goto done;
  801. }
  802. lbs_update_channel(priv);
  803. lbs_init_mesh(priv);
  804. lbs_debugfs_init_one(priv, dev);
  805. lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
  806. ret = 0;
  807. done:
  808. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  809. return ret;
  810. }
  811. EXPORT_SYMBOL_GPL(lbs_start_card);
  812. void lbs_stop_card(struct lbs_private *priv)
  813. {
  814. struct net_device *dev;
  815. struct cmd_ctrl_node *cmdnode;
  816. unsigned long flags;
  817. lbs_deb_enter(LBS_DEB_MAIN);
  818. if (!priv)
  819. goto out;
  820. dev = priv->dev;
  821. netif_stop_queue(dev);
  822. netif_carrier_off(dev);
  823. lbs_debugfs_remove_one(priv);
  824. lbs_deinit_mesh(priv);
  825. /* Delete the timeout of the currently processing command */
  826. del_timer_sync(&priv->command_timer);
  827. del_timer_sync(&priv->auto_deepsleep_timer);
  828. /* Flush pending command nodes */
  829. spin_lock_irqsave(&priv->driver_lock, flags);
  830. lbs_deb_main("clearing pending commands\n");
  831. list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
  832. cmdnode->result = -ENOENT;
  833. cmdnode->cmdwaitqwoken = 1;
  834. wake_up_interruptible(&cmdnode->cmdwait_q);
  835. }
  836. /* Flush the command the card is currently processing */
  837. if (priv->cur_cmd) {
  838. lbs_deb_main("clearing current command\n");
  839. priv->cur_cmd->result = -ENOENT;
  840. priv->cur_cmd->cmdwaitqwoken = 1;
  841. wake_up_interruptible(&priv->cur_cmd->cmdwait_q);
  842. }
  843. lbs_deb_main("done clearing commands\n");
  844. spin_unlock_irqrestore(&priv->driver_lock, flags);
  845. unregister_netdev(dev);
  846. out:
  847. lbs_deb_leave(LBS_DEB_MAIN);
  848. }
  849. EXPORT_SYMBOL_GPL(lbs_stop_card);
  850. void lbs_queue_event(struct lbs_private *priv, u32 event)
  851. {
  852. unsigned long flags;
  853. lbs_deb_enter(LBS_DEB_THREAD);
  854. spin_lock_irqsave(&priv->driver_lock, flags);
  855. if (priv->psstate == PS_STATE_SLEEP)
  856. priv->psstate = PS_STATE_AWAKE;
  857. kfifo_in(&priv->event_fifo, (unsigned char *) &event, sizeof(u32));
  858. wake_up_interruptible(&priv->waitq);
  859. spin_unlock_irqrestore(&priv->driver_lock, flags);
  860. lbs_deb_leave(LBS_DEB_THREAD);
  861. }
  862. EXPORT_SYMBOL_GPL(lbs_queue_event);
  863. void lbs_notify_command_response(struct lbs_private *priv, u8 resp_idx)
  864. {
  865. lbs_deb_enter(LBS_DEB_THREAD);
  866. if (priv->psstate == PS_STATE_SLEEP)
  867. priv->psstate = PS_STATE_AWAKE;
  868. /* Swap buffers by flipping the response index */
  869. BUG_ON(resp_idx > 1);
  870. priv->resp_idx = resp_idx;
  871. wake_up_interruptible(&priv->waitq);
  872. lbs_deb_leave(LBS_DEB_THREAD);
  873. }
  874. EXPORT_SYMBOL_GPL(lbs_notify_command_response);
  875. static int __init lbs_init_module(void)
  876. {
  877. lbs_deb_enter(LBS_DEB_MAIN);
  878. memset(&confirm_sleep, 0, sizeof(confirm_sleep));
  879. confirm_sleep.hdr.command = cpu_to_le16(CMD_802_11_PS_MODE);
  880. confirm_sleep.hdr.size = cpu_to_le16(sizeof(confirm_sleep));
  881. confirm_sleep.action = cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);
  882. lbs_debugfs_init();
  883. lbs_deb_leave(LBS_DEB_MAIN);
  884. return 0;
  885. }
  886. static void __exit lbs_exit_module(void)
  887. {
  888. lbs_deb_enter(LBS_DEB_MAIN);
  889. lbs_debugfs_remove();
  890. lbs_deb_leave(LBS_DEB_MAIN);
  891. }
  892. module_init(lbs_init_module);
  893. module_exit(lbs_exit_module);
  894. MODULE_DESCRIPTION("Libertas WLAN Driver Library");
  895. MODULE_AUTHOR("Marvell International Ltd.");
  896. MODULE_LICENSE("GPL");