main.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  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. int lbs_suspend(struct lbs_private *priv)
  449. {
  450. int ret;
  451. lbs_deb_enter(LBS_DEB_FW);
  452. if (priv->is_deep_sleep) {
  453. ret = lbs_set_deep_sleep(priv, 0);
  454. if (ret) {
  455. lbs_pr_err("deep sleep cancellation failed: %d\n", ret);
  456. return ret;
  457. }
  458. priv->deep_sleep_required = 1;
  459. }
  460. ret = lbs_set_host_sleep(priv, 1);
  461. netif_device_detach(priv->dev);
  462. if (priv->mesh_dev)
  463. netif_device_detach(priv->mesh_dev);
  464. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  465. return ret;
  466. }
  467. EXPORT_SYMBOL_GPL(lbs_suspend);
  468. int lbs_resume(struct lbs_private *priv)
  469. {
  470. int ret;
  471. lbs_deb_enter(LBS_DEB_FW);
  472. ret = lbs_set_host_sleep(priv, 0);
  473. netif_device_attach(priv->dev);
  474. if (priv->mesh_dev)
  475. netif_device_attach(priv->mesh_dev);
  476. if (priv->deep_sleep_required) {
  477. priv->deep_sleep_required = 0;
  478. ret = lbs_set_deep_sleep(priv, 1);
  479. if (ret)
  480. lbs_pr_err("deep sleep activation failed: %d\n", ret);
  481. }
  482. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  483. return ret;
  484. }
  485. EXPORT_SYMBOL_GPL(lbs_resume);
  486. /**
  487. * @brief This function gets the HW spec from the firmware and sets
  488. * some basic parameters.
  489. *
  490. * @param priv A pointer to struct lbs_private structure
  491. * @return 0 or -1
  492. */
  493. static int lbs_setup_firmware(struct lbs_private *priv)
  494. {
  495. int ret = -1;
  496. s16 curlevel = 0, minlevel = 0, maxlevel = 0;
  497. lbs_deb_enter(LBS_DEB_FW);
  498. /* Read MAC address from firmware */
  499. memset(priv->current_addr, 0xff, ETH_ALEN);
  500. ret = lbs_update_hw_spec(priv);
  501. if (ret)
  502. goto done;
  503. /* Read power levels if available */
  504. ret = lbs_get_tx_power(priv, &curlevel, &minlevel, &maxlevel);
  505. if (ret == 0) {
  506. priv->txpower_cur = curlevel;
  507. priv->txpower_min = minlevel;
  508. priv->txpower_max = maxlevel;
  509. }
  510. /* Send cmd to FW to enable 11D function */
  511. ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_11D_ENABLE, 1);
  512. lbs_set_mac_control(priv);
  513. done:
  514. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  515. return ret;
  516. }
  517. /**
  518. * This function handles the timeout of command sending.
  519. * It will re-send the same command again.
  520. */
  521. static void lbs_cmd_timeout_handler(unsigned long data)
  522. {
  523. struct lbs_private *priv = (struct lbs_private *)data;
  524. unsigned long flags;
  525. lbs_deb_enter(LBS_DEB_CMD);
  526. spin_lock_irqsave(&priv->driver_lock, flags);
  527. if (!priv->cur_cmd)
  528. goto out;
  529. lbs_pr_info("command 0x%04x timed out\n",
  530. le16_to_cpu(priv->cur_cmd->cmdbuf->command));
  531. priv->cmd_timed_out = 1;
  532. wake_up_interruptible(&priv->waitq);
  533. out:
  534. spin_unlock_irqrestore(&priv->driver_lock, flags);
  535. lbs_deb_leave(LBS_DEB_CMD);
  536. }
  537. /**
  538. * This function put the device back to deep sleep mode when timer expires
  539. * and no activity (command, event, data etc.) is detected.
  540. */
  541. static void auto_deepsleep_timer_fn(unsigned long data)
  542. {
  543. struct lbs_private *priv = (struct lbs_private *)data;
  544. int ret;
  545. lbs_deb_enter(LBS_DEB_CMD);
  546. if (priv->is_activity_detected) {
  547. priv->is_activity_detected = 0;
  548. } else {
  549. if (priv->is_auto_deep_sleep_enabled &&
  550. (!priv->wakeup_dev_required) &&
  551. (priv->connect_status != LBS_CONNECTED)) {
  552. lbs_deb_main("Entering auto deep sleep mode...\n");
  553. ret = lbs_prepare_and_send_command(priv,
  554. CMD_802_11_DEEP_SLEEP, 0,
  555. 0, 0, NULL);
  556. if (ret)
  557. lbs_pr_err("Enter Deep Sleep command failed\n");
  558. }
  559. }
  560. mod_timer(&priv->auto_deepsleep_timer , jiffies +
  561. (priv->auto_deep_sleep_timeout * HZ)/1000);
  562. lbs_deb_leave(LBS_DEB_CMD);
  563. }
  564. int lbs_enter_auto_deep_sleep(struct lbs_private *priv)
  565. {
  566. lbs_deb_enter(LBS_DEB_SDIO);
  567. priv->is_auto_deep_sleep_enabled = 1;
  568. if (priv->is_deep_sleep)
  569. priv->wakeup_dev_required = 1;
  570. mod_timer(&priv->auto_deepsleep_timer ,
  571. jiffies + (priv->auto_deep_sleep_timeout * HZ)/1000);
  572. lbs_deb_leave(LBS_DEB_SDIO);
  573. return 0;
  574. }
  575. int lbs_exit_auto_deep_sleep(struct lbs_private *priv)
  576. {
  577. lbs_deb_enter(LBS_DEB_SDIO);
  578. priv->is_auto_deep_sleep_enabled = 0;
  579. priv->auto_deep_sleep_timeout = 0;
  580. del_timer(&priv->auto_deepsleep_timer);
  581. lbs_deb_leave(LBS_DEB_SDIO);
  582. return 0;
  583. }
  584. static int lbs_init_adapter(struct lbs_private *priv)
  585. {
  586. int ret;
  587. lbs_deb_enter(LBS_DEB_MAIN);
  588. memset(priv->current_addr, 0xff, ETH_ALEN);
  589. priv->connect_status = LBS_DISCONNECTED;
  590. priv->channel = DEFAULT_AD_HOC_CHANNEL;
  591. priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
  592. priv->radio_on = 1;
  593. priv->psmode = LBS802_11POWERMODECAM;
  594. priv->psstate = PS_STATE_FULL_POWER;
  595. priv->is_deep_sleep = 0;
  596. priv->is_auto_deep_sleep_enabled = 0;
  597. priv->deep_sleep_required = 0;
  598. priv->wakeup_dev_required = 0;
  599. init_waitqueue_head(&priv->ds_awake_q);
  600. priv->authtype_auto = 1;
  601. priv->is_host_sleep_configured = 0;
  602. priv->is_host_sleep_activated = 0;
  603. init_waitqueue_head(&priv->host_sleep_q);
  604. mutex_init(&priv->lock);
  605. setup_timer(&priv->command_timer, lbs_cmd_timeout_handler,
  606. (unsigned long)priv);
  607. setup_timer(&priv->auto_deepsleep_timer, auto_deepsleep_timer_fn,
  608. (unsigned long)priv);
  609. INIT_LIST_HEAD(&priv->cmdfreeq);
  610. INIT_LIST_HEAD(&priv->cmdpendingq);
  611. spin_lock_init(&priv->driver_lock);
  612. init_waitqueue_head(&priv->cmd_pending);
  613. /* Allocate the command buffers */
  614. if (lbs_allocate_cmd_buffer(priv)) {
  615. lbs_pr_err("Out of memory allocating command buffers\n");
  616. ret = -ENOMEM;
  617. goto out;
  618. }
  619. priv->resp_idx = 0;
  620. priv->resp_len[0] = priv->resp_len[1] = 0;
  621. /* Create the event FIFO */
  622. ret = kfifo_alloc(&priv->event_fifo, sizeof(u32) * 16, GFP_KERNEL);
  623. if (ret) {
  624. lbs_pr_err("Out of memory allocating event FIFO buffer\n");
  625. goto out;
  626. }
  627. out:
  628. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  629. return ret;
  630. }
  631. static void lbs_free_adapter(struct lbs_private *priv)
  632. {
  633. lbs_deb_enter(LBS_DEB_MAIN);
  634. lbs_free_cmd_buffer(priv);
  635. kfifo_free(&priv->event_fifo);
  636. del_timer(&priv->command_timer);
  637. del_timer(&priv->auto_deepsleep_timer);
  638. lbs_deb_leave(LBS_DEB_MAIN);
  639. }
  640. static const struct net_device_ops lbs_netdev_ops = {
  641. .ndo_open = lbs_dev_open,
  642. .ndo_stop = lbs_eth_stop,
  643. .ndo_start_xmit = lbs_hard_start_xmit,
  644. .ndo_set_mac_address = lbs_set_mac_address,
  645. .ndo_tx_timeout = lbs_tx_timeout,
  646. .ndo_set_multicast_list = lbs_set_multicast_list,
  647. .ndo_change_mtu = eth_change_mtu,
  648. .ndo_validate_addr = eth_validate_addr,
  649. };
  650. /**
  651. * @brief This function adds the card. it will probe the
  652. * card, allocate the lbs_priv and initialize the device.
  653. *
  654. * @param card A pointer to card
  655. * @return A pointer to struct lbs_private structure
  656. */
  657. struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
  658. {
  659. struct net_device *dev;
  660. struct wireless_dev *wdev;
  661. struct lbs_private *priv = NULL;
  662. lbs_deb_enter(LBS_DEB_MAIN);
  663. /* Allocate an Ethernet device and register it */
  664. wdev = lbs_cfg_alloc(dmdev);
  665. if (IS_ERR(wdev)) {
  666. lbs_pr_err("cfg80211 init failed\n");
  667. goto done;
  668. }
  669. wdev->iftype = NL80211_IFTYPE_STATION;
  670. priv = wdev_priv(wdev);
  671. priv->wdev = wdev;
  672. if (lbs_init_adapter(priv)) {
  673. lbs_pr_err("failed to initialize adapter structure.\n");
  674. goto err_wdev;
  675. }
  676. dev = alloc_netdev(0, "wlan%d", ether_setup);
  677. if (!dev) {
  678. dev_err(dmdev, "no memory for network device instance\n");
  679. goto err_adapter;
  680. }
  681. dev->ieee80211_ptr = wdev;
  682. dev->ml_priv = priv;
  683. SET_NETDEV_DEV(dev, dmdev);
  684. wdev->netdev = dev;
  685. priv->dev = dev;
  686. dev->netdev_ops = &lbs_netdev_ops;
  687. dev->watchdog_timeo = 5 * HZ;
  688. dev->ethtool_ops = &lbs_ethtool_ops;
  689. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  690. priv->card = card;
  691. strcpy(dev->name, "wlan%d");
  692. lbs_deb_thread("Starting main thread...\n");
  693. init_waitqueue_head(&priv->waitq);
  694. priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
  695. if (IS_ERR(priv->main_thread)) {
  696. lbs_deb_thread("Error creating main thread.\n");
  697. goto err_ndev;
  698. }
  699. priv->work_thread = create_singlethread_workqueue("lbs_worker");
  700. INIT_WORK(&priv->mcast_work, lbs_set_mcast_worker);
  701. priv->wol_criteria = 0xffffffff;
  702. priv->wol_gpio = 0xff;
  703. priv->wol_gap = 20;
  704. goto done;
  705. err_ndev:
  706. free_netdev(dev);
  707. err_adapter:
  708. lbs_free_adapter(priv);
  709. err_wdev:
  710. lbs_cfg_free(priv);
  711. priv = NULL;
  712. done:
  713. lbs_deb_leave_args(LBS_DEB_MAIN, "priv %p", priv);
  714. return priv;
  715. }
  716. EXPORT_SYMBOL_GPL(lbs_add_card);
  717. void lbs_remove_card(struct lbs_private *priv)
  718. {
  719. struct net_device *dev = priv->dev;
  720. lbs_deb_enter(LBS_DEB_MAIN);
  721. lbs_remove_mesh(priv);
  722. lbs_scan_deinit(priv);
  723. dev = priv->dev;
  724. cancel_work_sync(&priv->mcast_work);
  725. /* worker thread destruction blocks on the in-flight command which
  726. * should have been cleared already in lbs_stop_card().
  727. */
  728. lbs_deb_main("destroying worker thread\n");
  729. destroy_workqueue(priv->work_thread);
  730. lbs_deb_main("done destroying worker thread\n");
  731. if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
  732. priv->psmode = LBS802_11POWERMODECAM;
  733. lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
  734. }
  735. if (priv->is_deep_sleep) {
  736. priv->is_deep_sleep = 0;
  737. wake_up_interruptible(&priv->ds_awake_q);
  738. }
  739. priv->is_host_sleep_configured = 0;
  740. priv->is_host_sleep_activated = 0;
  741. wake_up_interruptible(&priv->host_sleep_q);
  742. /* Stop the thread servicing the interrupts */
  743. priv->surpriseremoved = 1;
  744. kthread_stop(priv->main_thread);
  745. lbs_free_adapter(priv);
  746. lbs_cfg_free(priv);
  747. priv->dev = NULL;
  748. free_netdev(dev);
  749. lbs_deb_leave(LBS_DEB_MAIN);
  750. }
  751. EXPORT_SYMBOL_GPL(lbs_remove_card);
  752. int lbs_rtap_supported(struct lbs_private *priv)
  753. {
  754. if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V5)
  755. return 1;
  756. /* newer firmware use a capability mask */
  757. return ((MRVL_FW_MAJOR_REV(priv->fwrelease) >= MRVL_FW_V10) &&
  758. (priv->fwcapinfo & MESH_CAPINFO_ENABLE_MASK));
  759. }
  760. int lbs_start_card(struct lbs_private *priv)
  761. {
  762. struct net_device *dev = priv->dev;
  763. int ret = -1;
  764. lbs_deb_enter(LBS_DEB_MAIN);
  765. /* poke the firmware */
  766. ret = lbs_setup_firmware(priv);
  767. if (ret)
  768. goto done;
  769. if (lbs_cfg_register(priv)) {
  770. lbs_pr_err("cannot register device\n");
  771. goto done;
  772. }
  773. lbs_update_channel(priv);
  774. lbs_init_mesh(priv);
  775. lbs_debugfs_init_one(priv, dev);
  776. lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
  777. ret = 0;
  778. done:
  779. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  780. return ret;
  781. }
  782. EXPORT_SYMBOL_GPL(lbs_start_card);
  783. void lbs_stop_card(struct lbs_private *priv)
  784. {
  785. struct net_device *dev;
  786. struct cmd_ctrl_node *cmdnode;
  787. unsigned long flags;
  788. lbs_deb_enter(LBS_DEB_MAIN);
  789. if (!priv)
  790. goto out;
  791. dev = priv->dev;
  792. netif_stop_queue(dev);
  793. netif_carrier_off(dev);
  794. lbs_debugfs_remove_one(priv);
  795. lbs_deinit_mesh(priv);
  796. /* Delete the timeout of the currently processing command */
  797. del_timer_sync(&priv->command_timer);
  798. del_timer_sync(&priv->auto_deepsleep_timer);
  799. /* Flush pending command nodes */
  800. spin_lock_irqsave(&priv->driver_lock, flags);
  801. lbs_deb_main("clearing pending commands\n");
  802. list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
  803. cmdnode->result = -ENOENT;
  804. cmdnode->cmdwaitqwoken = 1;
  805. wake_up_interruptible(&cmdnode->cmdwait_q);
  806. }
  807. /* Flush the command the card is currently processing */
  808. if (priv->cur_cmd) {
  809. lbs_deb_main("clearing current command\n");
  810. priv->cur_cmd->result = -ENOENT;
  811. priv->cur_cmd->cmdwaitqwoken = 1;
  812. wake_up_interruptible(&priv->cur_cmd->cmdwait_q);
  813. }
  814. lbs_deb_main("done clearing commands\n");
  815. spin_unlock_irqrestore(&priv->driver_lock, flags);
  816. unregister_netdev(dev);
  817. out:
  818. lbs_deb_leave(LBS_DEB_MAIN);
  819. }
  820. EXPORT_SYMBOL_GPL(lbs_stop_card);
  821. void lbs_queue_event(struct lbs_private *priv, u32 event)
  822. {
  823. unsigned long flags;
  824. lbs_deb_enter(LBS_DEB_THREAD);
  825. spin_lock_irqsave(&priv->driver_lock, flags);
  826. if (priv->psstate == PS_STATE_SLEEP)
  827. priv->psstate = PS_STATE_AWAKE;
  828. kfifo_in(&priv->event_fifo, (unsigned char *) &event, sizeof(u32));
  829. wake_up_interruptible(&priv->waitq);
  830. spin_unlock_irqrestore(&priv->driver_lock, flags);
  831. lbs_deb_leave(LBS_DEB_THREAD);
  832. }
  833. EXPORT_SYMBOL_GPL(lbs_queue_event);
  834. void lbs_notify_command_response(struct lbs_private *priv, u8 resp_idx)
  835. {
  836. lbs_deb_enter(LBS_DEB_THREAD);
  837. if (priv->psstate == PS_STATE_SLEEP)
  838. priv->psstate = PS_STATE_AWAKE;
  839. /* Swap buffers by flipping the response index */
  840. BUG_ON(resp_idx > 1);
  841. priv->resp_idx = resp_idx;
  842. wake_up_interruptible(&priv->waitq);
  843. lbs_deb_leave(LBS_DEB_THREAD);
  844. }
  845. EXPORT_SYMBOL_GPL(lbs_notify_command_response);
  846. static int __init lbs_init_module(void)
  847. {
  848. lbs_deb_enter(LBS_DEB_MAIN);
  849. memset(&confirm_sleep, 0, sizeof(confirm_sleep));
  850. confirm_sleep.hdr.command = cpu_to_le16(CMD_802_11_PS_MODE);
  851. confirm_sleep.hdr.size = cpu_to_le16(sizeof(confirm_sleep));
  852. confirm_sleep.action = cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);
  853. lbs_debugfs_init();
  854. lbs_deb_leave(LBS_DEB_MAIN);
  855. return 0;
  856. }
  857. static void __exit lbs_exit_module(void)
  858. {
  859. lbs_deb_enter(LBS_DEB_MAIN);
  860. lbs_debugfs_remove();
  861. lbs_deb_leave(LBS_DEB_MAIN);
  862. }
  863. module_init(lbs_init_module);
  864. module_exit(lbs_exit_module);
  865. MODULE_DESCRIPTION("Libertas WLAN Driver Library");
  866. MODULE_AUTHOR("Marvell International Ltd.");
  867. MODULE_LICENSE("GPL");