main.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. /**
  2. * This file contains the major functions in WLAN
  3. * driver. It includes init, exit, open, close and main
  4. * thread etc..
  5. */
  6. #include <linux/moduleparam.h>
  7. #include <linux/delay.h>
  8. #include <linux/etherdevice.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/if_arp.h>
  11. #include <linux/kthread.h>
  12. #include <linux/kfifo.h>
  13. #include <linux/slab.h>
  14. #include <net/cfg80211.h>
  15. #include "host.h"
  16. #include "decl.h"
  17. #include "dev.h"
  18. #include "cfg.h"
  19. #include "debugfs.h"
  20. #include "cmd.h"
  21. #define DRIVER_RELEASE_VERSION "323.p0"
  22. const char lbs_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
  23. #ifdef DEBUG
  24. "-dbg"
  25. #endif
  26. "";
  27. /* Module parameters */
  28. unsigned int lbs_debug;
  29. EXPORT_SYMBOL_GPL(lbs_debug);
  30. module_param_named(libertas_debug, lbs_debug, int, 0644);
  31. /* This global structure is used to send the confirm_sleep command as
  32. * fast as possible down to the firmware. */
  33. struct cmd_confirm_sleep confirm_sleep;
  34. /**
  35. * the table to keep region code
  36. */
  37. u16 lbs_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
  38. { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
  39. /**
  40. * FW rate table. FW refers to rates by their index in this table, not by the
  41. * rate value itself. Values of 0x00 are
  42. * reserved positions.
  43. */
  44. static u8 fw_data_rates[MAX_RATES] =
  45. { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12,
  46. 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00
  47. };
  48. /**
  49. * @brief use index to get the data rate
  50. *
  51. * @param idx The index of data rate
  52. * @return data rate or 0
  53. */
  54. u32 lbs_fw_index_to_data_rate(u8 idx)
  55. {
  56. if (idx >= sizeof(fw_data_rates))
  57. idx = 0;
  58. return fw_data_rates[idx];
  59. }
  60. /**
  61. * @brief use rate to get the index
  62. *
  63. * @param rate data rate
  64. * @return index or 0
  65. */
  66. u8 lbs_data_rate_to_fw_index(u32 rate)
  67. {
  68. u8 i;
  69. if (!rate)
  70. return 0;
  71. for (i = 0; i < sizeof(fw_data_rates); i++) {
  72. if (rate == fw_data_rates[i])
  73. return i;
  74. }
  75. return 0;
  76. }
  77. /**
  78. * @brief This function opens the ethX interface
  79. *
  80. * @param dev A pointer to net_device structure
  81. * @return 0 or -EBUSY if monitor mode active
  82. */
  83. static int lbs_dev_open(struct net_device *dev)
  84. {
  85. struct lbs_private *priv = dev->ml_priv;
  86. int ret = 0;
  87. lbs_deb_enter(LBS_DEB_NET);
  88. spin_lock_irq(&priv->driver_lock);
  89. priv->stopping = false;
  90. if (priv->connect_status == LBS_CONNECTED)
  91. netif_carrier_on(dev);
  92. else
  93. netif_carrier_off(dev);
  94. if (!priv->tx_pending_len)
  95. netif_wake_queue(dev);
  96. spin_unlock_irq(&priv->driver_lock);
  97. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  98. return ret;
  99. }
  100. /**
  101. * @brief This function closes the ethX interface
  102. *
  103. * @param dev A pointer to net_device structure
  104. * @return 0
  105. */
  106. static int lbs_eth_stop(struct net_device *dev)
  107. {
  108. struct lbs_private *priv = dev->ml_priv;
  109. lbs_deb_enter(LBS_DEB_NET);
  110. spin_lock_irq(&priv->driver_lock);
  111. priv->stopping = true;
  112. netif_stop_queue(dev);
  113. spin_unlock_irq(&priv->driver_lock);
  114. schedule_work(&priv->mcast_work);
  115. cancel_delayed_work_sync(&priv->scan_work);
  116. if (priv->scan_req) {
  117. cfg80211_scan_done(priv->scan_req, false);
  118. priv->scan_req = NULL;
  119. }
  120. lbs_deb_leave(LBS_DEB_NET);
  121. return 0;
  122. }
  123. static void lbs_tx_timeout(struct net_device *dev)
  124. {
  125. struct lbs_private *priv = dev->ml_priv;
  126. lbs_deb_enter(LBS_DEB_TX);
  127. lbs_pr_err("tx watch dog timeout\n");
  128. dev->trans_start = jiffies; /* prevent tx timeout */
  129. if (priv->currenttxskb)
  130. lbs_send_tx_feedback(priv, 0);
  131. /* XX: Shouldn't we also call into the hw-specific driver
  132. to kick it somehow? */
  133. lbs_host_to_card_done(priv);
  134. /* FIXME: reset the card */
  135. lbs_deb_leave(LBS_DEB_TX);
  136. }
  137. void lbs_host_to_card_done(struct lbs_private *priv)
  138. {
  139. unsigned long flags;
  140. lbs_deb_enter(LBS_DEB_THREAD);
  141. spin_lock_irqsave(&priv->driver_lock, flags);
  142. priv->dnld_sent = DNLD_RES_RECEIVED;
  143. /* Wake main thread if commands are pending */
  144. if (!priv->cur_cmd || priv->tx_pending_len > 0) {
  145. if (!priv->wakeup_dev_required)
  146. wake_up_interruptible(&priv->waitq);
  147. }
  148. spin_unlock_irqrestore(&priv->driver_lock, flags);
  149. lbs_deb_leave(LBS_DEB_THREAD);
  150. }
  151. EXPORT_SYMBOL_GPL(lbs_host_to_card_done);
  152. int lbs_set_mac_address(struct net_device *dev, void *addr)
  153. {
  154. int ret = 0;
  155. struct lbs_private *priv = dev->ml_priv;
  156. struct sockaddr *phwaddr = addr;
  157. struct cmd_ds_802_11_mac_address cmd;
  158. lbs_deb_enter(LBS_DEB_NET);
  159. /* In case it was called from the mesh device */
  160. dev = priv->dev;
  161. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  162. cmd.action = cpu_to_le16(CMD_ACT_SET);
  163. memcpy(cmd.macadd, phwaddr->sa_data, ETH_ALEN);
  164. ret = lbs_cmd_with_response(priv, CMD_802_11_MAC_ADDRESS, &cmd);
  165. if (ret) {
  166. lbs_deb_net("set MAC address failed\n");
  167. goto done;
  168. }
  169. memcpy(priv->current_addr, phwaddr->sa_data, ETH_ALEN);
  170. memcpy(dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
  171. if (priv->mesh_dev)
  172. memcpy(priv->mesh_dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
  173. done:
  174. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  175. return ret;
  176. }
  177. static inline int mac_in_list(unsigned char *list, int list_len,
  178. unsigned char *mac)
  179. {
  180. while (list_len) {
  181. if (!memcmp(list, mac, ETH_ALEN))
  182. return 1;
  183. list += ETH_ALEN;
  184. list_len--;
  185. }
  186. return 0;
  187. }
  188. static int lbs_add_mcast_addrs(struct cmd_ds_mac_multicast_adr *cmd,
  189. struct net_device *dev, int nr_addrs)
  190. {
  191. int i = nr_addrs;
  192. struct netdev_hw_addr *ha;
  193. int cnt;
  194. if ((dev->flags & (IFF_UP|IFF_MULTICAST)) != (IFF_UP|IFF_MULTICAST))
  195. return nr_addrs;
  196. netif_addr_lock_bh(dev);
  197. cnt = netdev_mc_count(dev);
  198. netdev_for_each_mc_addr(ha, dev) {
  199. if (mac_in_list(cmd->maclist, nr_addrs, ha->addr)) {
  200. lbs_deb_net("mcast address %s:%pM skipped\n", dev->name,
  201. ha->addr);
  202. cnt--;
  203. continue;
  204. }
  205. if (i == MRVDRV_MAX_MULTICAST_LIST_SIZE)
  206. break;
  207. memcpy(&cmd->maclist[6*i], ha->addr, ETH_ALEN);
  208. lbs_deb_net("mcast address %s:%pM added to filter\n", dev->name,
  209. ha->addr);
  210. i++;
  211. cnt--;
  212. }
  213. netif_addr_unlock_bh(dev);
  214. if (cnt)
  215. return -EOVERFLOW;
  216. return i;
  217. }
  218. static void lbs_set_mcast_worker(struct work_struct *work)
  219. {
  220. struct lbs_private *priv = container_of(work, struct lbs_private, mcast_work);
  221. struct cmd_ds_mac_multicast_adr mcast_cmd;
  222. int dev_flags;
  223. int nr_addrs;
  224. int old_mac_control = priv->mac_control;
  225. lbs_deb_enter(LBS_DEB_NET);
  226. dev_flags = priv->dev->flags;
  227. if (priv->mesh_dev)
  228. dev_flags |= priv->mesh_dev->flags;
  229. if (dev_flags & IFF_PROMISC) {
  230. priv->mac_control |= CMD_ACT_MAC_PROMISCUOUS_ENABLE;
  231. priv->mac_control &= ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
  232. CMD_ACT_MAC_MULTICAST_ENABLE);
  233. goto out_set_mac_control;
  234. } else if (dev_flags & IFF_ALLMULTI) {
  235. do_allmulti:
  236. priv->mac_control |= CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
  237. priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
  238. CMD_ACT_MAC_MULTICAST_ENABLE);
  239. goto out_set_mac_control;
  240. }
  241. /* Once for priv->dev, again for priv->mesh_dev if it exists */
  242. nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->dev, 0);
  243. if (nr_addrs >= 0 && priv->mesh_dev)
  244. nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->mesh_dev, nr_addrs);
  245. if (nr_addrs < 0)
  246. goto do_allmulti;
  247. if (nr_addrs) {
  248. int size = offsetof(struct cmd_ds_mac_multicast_adr,
  249. maclist[6*nr_addrs]);
  250. mcast_cmd.action = cpu_to_le16(CMD_ACT_SET);
  251. mcast_cmd.hdr.size = cpu_to_le16(size);
  252. mcast_cmd.nr_of_adrs = cpu_to_le16(nr_addrs);
  253. lbs_cmd_async(priv, CMD_MAC_MULTICAST_ADR, &mcast_cmd.hdr, size);
  254. priv->mac_control |= CMD_ACT_MAC_MULTICAST_ENABLE;
  255. } else
  256. priv->mac_control &= ~CMD_ACT_MAC_MULTICAST_ENABLE;
  257. priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
  258. CMD_ACT_MAC_ALL_MULTICAST_ENABLE);
  259. out_set_mac_control:
  260. if (priv->mac_control != old_mac_control)
  261. lbs_set_mac_control(priv);
  262. lbs_deb_leave(LBS_DEB_NET);
  263. }
  264. void lbs_set_multicast_list(struct net_device *dev)
  265. {
  266. struct lbs_private *priv = dev->ml_priv;
  267. schedule_work(&priv->mcast_work);
  268. }
  269. /**
  270. * @brief This function handles the major jobs in the LBS driver.
  271. * It handles all events generated by firmware, RX data received
  272. * from firmware and TX data sent from kernel.
  273. *
  274. * @param data A pointer to lbs_thread structure
  275. * @return 0
  276. */
  277. static int lbs_thread(void *data)
  278. {
  279. struct net_device *dev = data;
  280. struct lbs_private *priv = dev->ml_priv;
  281. wait_queue_t wait;
  282. lbs_deb_enter(LBS_DEB_THREAD);
  283. init_waitqueue_entry(&wait, current);
  284. for (;;) {
  285. int shouldsleep;
  286. u8 resp_idx;
  287. lbs_deb_thread("1: currenttxskb %p, dnld_sent %d\n",
  288. priv->currenttxskb, priv->dnld_sent);
  289. add_wait_queue(&priv->waitq, &wait);
  290. set_current_state(TASK_INTERRUPTIBLE);
  291. spin_lock_irq(&priv->driver_lock);
  292. if (kthread_should_stop())
  293. shouldsleep = 0; /* Bye */
  294. else if (priv->surpriseremoved)
  295. shouldsleep = 1; /* We need to wait until we're _told_ to die */
  296. else if (priv->psstate == PS_STATE_SLEEP)
  297. shouldsleep = 1; /* Sleep mode. Nothing we can do till it wakes */
  298. else if (priv->cmd_timed_out)
  299. shouldsleep = 0; /* Command timed out. Recover */
  300. else if (!priv->fw_ready)
  301. shouldsleep = 1; /* Firmware not ready. We're waiting for it */
  302. else if (priv->dnld_sent)
  303. shouldsleep = 1; /* Something is en route to the device already */
  304. else if (priv->tx_pending_len > 0)
  305. shouldsleep = 0; /* We've a packet to send */
  306. else if (priv->resp_len[priv->resp_idx])
  307. shouldsleep = 0; /* We have a command response */
  308. else if (priv->cur_cmd)
  309. shouldsleep = 1; /* Can't send a command; one already running */
  310. else if (!list_empty(&priv->cmdpendingq) &&
  311. !(priv->wakeup_dev_required))
  312. shouldsleep = 0; /* We have a command to send */
  313. else if (kfifo_len(&priv->event_fifo))
  314. shouldsleep = 0; /* We have an event to process */
  315. else
  316. shouldsleep = 1; /* No command */
  317. if (shouldsleep) {
  318. lbs_deb_thread("sleeping, connect_status %d, "
  319. "psmode %d, psstate %d\n",
  320. priv->connect_status,
  321. priv->psmode, priv->psstate);
  322. spin_unlock_irq(&priv->driver_lock);
  323. schedule();
  324. } else
  325. spin_unlock_irq(&priv->driver_lock);
  326. lbs_deb_thread("2: currenttxskb %p, dnld_send %d\n",
  327. priv->currenttxskb, priv->dnld_sent);
  328. set_current_state(TASK_RUNNING);
  329. remove_wait_queue(&priv->waitq, &wait);
  330. lbs_deb_thread("3: currenttxskb %p, dnld_sent %d\n",
  331. priv->currenttxskb, priv->dnld_sent);
  332. if (kthread_should_stop()) {
  333. lbs_deb_thread("break from main thread\n");
  334. break;
  335. }
  336. if (priv->surpriseremoved) {
  337. lbs_deb_thread("adapter removed; waiting to die...\n");
  338. continue;
  339. }
  340. lbs_deb_thread("4: currenttxskb %p, dnld_sent %d\n",
  341. priv->currenttxskb, priv->dnld_sent);
  342. /* Process any pending command response */
  343. spin_lock_irq(&priv->driver_lock);
  344. resp_idx = priv->resp_idx;
  345. if (priv->resp_len[resp_idx]) {
  346. spin_unlock_irq(&priv->driver_lock);
  347. lbs_process_command_response(priv,
  348. priv->resp_buf[resp_idx],
  349. priv->resp_len[resp_idx]);
  350. spin_lock_irq(&priv->driver_lock);
  351. priv->resp_len[resp_idx] = 0;
  352. }
  353. spin_unlock_irq(&priv->driver_lock);
  354. /* Process hardware events, e.g. card removed, link lost */
  355. spin_lock_irq(&priv->driver_lock);
  356. while (kfifo_len(&priv->event_fifo)) {
  357. u32 event;
  358. if (kfifo_out(&priv->event_fifo,
  359. (unsigned char *) &event, sizeof(event)) !=
  360. sizeof(event))
  361. break;
  362. spin_unlock_irq(&priv->driver_lock);
  363. lbs_process_event(priv, event);
  364. spin_lock_irq(&priv->driver_lock);
  365. }
  366. spin_unlock_irq(&priv->driver_lock);
  367. if (priv->wakeup_dev_required) {
  368. lbs_deb_thread("Waking up device...\n");
  369. /* Wake up device */
  370. if (priv->exit_deep_sleep(priv))
  371. lbs_deb_thread("Wakeup device failed\n");
  372. continue;
  373. }
  374. /* command timeout stuff */
  375. if (priv->cmd_timed_out && priv->cur_cmd) {
  376. struct cmd_ctrl_node *cmdnode = priv->cur_cmd;
  377. lbs_pr_info("Timeout submitting command 0x%04x\n",
  378. le16_to_cpu(cmdnode->cmdbuf->command));
  379. lbs_complete_command(priv, cmdnode, -ETIMEDOUT);
  380. if (priv->reset_card)
  381. priv->reset_card(priv);
  382. }
  383. priv->cmd_timed_out = 0;
  384. if (!priv->fw_ready)
  385. continue;
  386. /* Check if we need to confirm Sleep Request received previously */
  387. if (priv->psstate == PS_STATE_PRE_SLEEP &&
  388. !priv->dnld_sent && !priv->cur_cmd) {
  389. if (priv->connect_status == LBS_CONNECTED) {
  390. lbs_deb_thread("pre-sleep, currenttxskb %p, "
  391. "dnld_sent %d, cur_cmd %p\n",
  392. priv->currenttxskb, priv->dnld_sent,
  393. priv->cur_cmd);
  394. lbs_ps_confirm_sleep(priv);
  395. } else {
  396. /* workaround for firmware sending
  397. * deauth/linkloss event immediately
  398. * after sleep request; remove this
  399. * after firmware fixes it
  400. */
  401. priv->psstate = PS_STATE_AWAKE;
  402. lbs_pr_alert("ignore PS_SleepConfirm in "
  403. "non-connected state\n");
  404. }
  405. }
  406. /* The PS state is changed during processing of Sleep Request
  407. * event above
  408. */
  409. if ((priv->psstate == PS_STATE_SLEEP) ||
  410. (priv->psstate == PS_STATE_PRE_SLEEP))
  411. continue;
  412. if (priv->is_deep_sleep)
  413. continue;
  414. /* Execute the next command */
  415. if (!priv->dnld_sent && !priv->cur_cmd)
  416. lbs_execute_next_command(priv);
  417. spin_lock_irq(&priv->driver_lock);
  418. if (!priv->dnld_sent && priv->tx_pending_len > 0) {
  419. int ret = priv->hw_host_to_card(priv, MVMS_DAT,
  420. priv->tx_pending_buf,
  421. priv->tx_pending_len);
  422. if (ret) {
  423. lbs_deb_tx("host_to_card failed %d\n", ret);
  424. priv->dnld_sent = DNLD_RES_RECEIVED;
  425. }
  426. priv->tx_pending_len = 0;
  427. if (!priv->currenttxskb) {
  428. /* We can wake the queues immediately if we aren't
  429. waiting for TX feedback */
  430. if (priv->connect_status == LBS_CONNECTED)
  431. netif_wake_queue(priv->dev);
  432. if (priv->mesh_dev &&
  433. lbs_mesh_connected(priv))
  434. netif_wake_queue(priv->mesh_dev);
  435. }
  436. }
  437. spin_unlock_irq(&priv->driver_lock);
  438. }
  439. del_timer(&priv->command_timer);
  440. del_timer(&priv->auto_deepsleep_timer);
  441. lbs_deb_leave(LBS_DEB_THREAD);
  442. return 0;
  443. }
  444. /**
  445. * @brief This function gets the HW spec from the firmware and sets
  446. * some basic parameters.
  447. *
  448. * @param priv A pointer to struct lbs_private structure
  449. * @return 0 or -1
  450. */
  451. static int lbs_setup_firmware(struct lbs_private *priv)
  452. {
  453. int ret = -1;
  454. s16 curlevel = 0, minlevel = 0, maxlevel = 0;
  455. lbs_deb_enter(LBS_DEB_FW);
  456. /* Read MAC address from firmware */
  457. memset(priv->current_addr, 0xff, ETH_ALEN);
  458. ret = lbs_update_hw_spec(priv);
  459. if (ret)
  460. goto done;
  461. /* Read power levels if available */
  462. ret = lbs_get_tx_power(priv, &curlevel, &minlevel, &maxlevel);
  463. if (ret == 0) {
  464. priv->txpower_cur = curlevel;
  465. priv->txpower_min = minlevel;
  466. priv->txpower_max = maxlevel;
  467. }
  468. /* Send cmd to FW to enable 11D function */
  469. ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_11D_ENABLE, 1);
  470. lbs_set_mac_control(priv);
  471. done:
  472. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  473. return ret;
  474. }
  475. int lbs_suspend(struct lbs_private *priv)
  476. {
  477. int ret;
  478. lbs_deb_enter(LBS_DEB_FW);
  479. if (priv->is_deep_sleep) {
  480. ret = lbs_set_deep_sleep(priv, 0);
  481. if (ret) {
  482. lbs_pr_err("deep sleep cancellation failed: %d\n", ret);
  483. return ret;
  484. }
  485. priv->deep_sleep_required = 1;
  486. }
  487. ret = lbs_set_host_sleep(priv, 1);
  488. netif_device_detach(priv->dev);
  489. if (priv->mesh_dev)
  490. netif_device_detach(priv->mesh_dev);
  491. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  492. return ret;
  493. }
  494. EXPORT_SYMBOL_GPL(lbs_suspend);
  495. int lbs_resume(struct lbs_private *priv)
  496. {
  497. int ret;
  498. lbs_deb_enter(LBS_DEB_FW);
  499. ret = lbs_set_host_sleep(priv, 0);
  500. netif_device_attach(priv->dev);
  501. if (priv->mesh_dev)
  502. netif_device_attach(priv->mesh_dev);
  503. if (priv->deep_sleep_required) {
  504. priv->deep_sleep_required = 0;
  505. ret = lbs_set_deep_sleep(priv, 1);
  506. if (ret)
  507. lbs_pr_err("deep sleep activation failed: %d\n", ret);
  508. }
  509. if (priv->setup_fw_on_resume)
  510. ret = lbs_setup_firmware(priv);
  511. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  512. return ret;
  513. }
  514. EXPORT_SYMBOL_GPL(lbs_resume);
  515. /**
  516. * This function handles the timeout of command sending.
  517. * It will re-send the same command again.
  518. */
  519. static void lbs_cmd_timeout_handler(unsigned long data)
  520. {
  521. struct lbs_private *priv = (struct lbs_private *)data;
  522. unsigned long flags;
  523. lbs_deb_enter(LBS_DEB_CMD);
  524. spin_lock_irqsave(&priv->driver_lock, flags);
  525. if (!priv->cur_cmd)
  526. goto out;
  527. lbs_pr_info("command 0x%04x timed out\n",
  528. le16_to_cpu(priv->cur_cmd->cmdbuf->command));
  529. priv->cmd_timed_out = 1;
  530. wake_up_interruptible(&priv->waitq);
  531. out:
  532. spin_unlock_irqrestore(&priv->driver_lock, flags);
  533. lbs_deb_leave(LBS_DEB_CMD);
  534. }
  535. /**
  536. * This function put the device back to deep sleep mode when timer expires
  537. * and no activity (command, event, data etc.) is detected.
  538. */
  539. static void auto_deepsleep_timer_fn(unsigned long data)
  540. {
  541. struct lbs_private *priv = (struct lbs_private *)data;
  542. lbs_deb_enter(LBS_DEB_CMD);
  543. if (priv->is_activity_detected) {
  544. priv->is_activity_detected = 0;
  545. } else {
  546. if (priv->is_auto_deep_sleep_enabled &&
  547. (!priv->wakeup_dev_required) &&
  548. (priv->connect_status != LBS_CONNECTED)) {
  549. struct cmd_header cmd;
  550. lbs_deb_main("Entering auto deep sleep mode...\n");
  551. memset(&cmd, 0, sizeof(cmd));
  552. cmd.size = cpu_to_le16(sizeof(cmd));
  553. lbs_cmd_async(priv, CMD_802_11_DEEP_SLEEP, &cmd,
  554. sizeof(cmd));
  555. }
  556. }
  557. mod_timer(&priv->auto_deepsleep_timer , jiffies +
  558. (priv->auto_deep_sleep_timeout * HZ)/1000);
  559. lbs_deb_leave(LBS_DEB_CMD);
  560. }
  561. int lbs_enter_auto_deep_sleep(struct lbs_private *priv)
  562. {
  563. lbs_deb_enter(LBS_DEB_SDIO);
  564. priv->is_auto_deep_sleep_enabled = 1;
  565. if (priv->is_deep_sleep)
  566. priv->wakeup_dev_required = 1;
  567. mod_timer(&priv->auto_deepsleep_timer ,
  568. jiffies + (priv->auto_deep_sleep_timeout * HZ)/1000);
  569. lbs_deb_leave(LBS_DEB_SDIO);
  570. return 0;
  571. }
  572. int lbs_exit_auto_deep_sleep(struct lbs_private *priv)
  573. {
  574. lbs_deb_enter(LBS_DEB_SDIO);
  575. priv->is_auto_deep_sleep_enabled = 0;
  576. priv->auto_deep_sleep_timeout = 0;
  577. del_timer(&priv->auto_deepsleep_timer);
  578. lbs_deb_leave(LBS_DEB_SDIO);
  579. return 0;
  580. }
  581. static int lbs_init_adapter(struct lbs_private *priv)
  582. {
  583. int ret;
  584. lbs_deb_enter(LBS_DEB_MAIN);
  585. memset(priv->current_addr, 0xff, ETH_ALEN);
  586. priv->connect_status = LBS_DISCONNECTED;
  587. priv->channel = DEFAULT_AD_HOC_CHANNEL;
  588. priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
  589. priv->radio_on = 1;
  590. priv->psmode = LBS802_11POWERMODECAM;
  591. priv->psstate = PS_STATE_FULL_POWER;
  592. priv->is_deep_sleep = 0;
  593. priv->is_auto_deep_sleep_enabled = 0;
  594. priv->deep_sleep_required = 0;
  595. priv->wakeup_dev_required = 0;
  596. init_waitqueue_head(&priv->ds_awake_q);
  597. init_waitqueue_head(&priv->scan_q);
  598. priv->authtype_auto = 1;
  599. priv->is_host_sleep_configured = 0;
  600. priv->is_host_sleep_activated = 0;
  601. init_waitqueue_head(&priv->host_sleep_q);
  602. mutex_init(&priv->lock);
  603. setup_timer(&priv->command_timer, lbs_cmd_timeout_handler,
  604. (unsigned long)priv);
  605. setup_timer(&priv->auto_deepsleep_timer, auto_deepsleep_timer_fn,
  606. (unsigned long)priv);
  607. INIT_LIST_HEAD(&priv->cmdfreeq);
  608. INIT_LIST_HEAD(&priv->cmdpendingq);
  609. spin_lock_init(&priv->driver_lock);
  610. /* Allocate the command buffers */
  611. if (lbs_allocate_cmd_buffer(priv)) {
  612. lbs_pr_err("Out of memory allocating command buffers\n");
  613. ret = -ENOMEM;
  614. goto out;
  615. }
  616. priv->resp_idx = 0;
  617. priv->resp_len[0] = priv->resp_len[1] = 0;
  618. /* Create the event FIFO */
  619. ret = kfifo_alloc(&priv->event_fifo, sizeof(u32) * 16, GFP_KERNEL);
  620. if (ret) {
  621. lbs_pr_err("Out of memory allocating event FIFO buffer\n");
  622. goto out;
  623. }
  624. out:
  625. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  626. return ret;
  627. }
  628. static void lbs_free_adapter(struct lbs_private *priv)
  629. {
  630. lbs_deb_enter(LBS_DEB_MAIN);
  631. lbs_free_cmd_buffer(priv);
  632. kfifo_free(&priv->event_fifo);
  633. del_timer(&priv->command_timer);
  634. del_timer(&priv->auto_deepsleep_timer);
  635. lbs_deb_leave(LBS_DEB_MAIN);
  636. }
  637. static const struct net_device_ops lbs_netdev_ops = {
  638. .ndo_open = lbs_dev_open,
  639. .ndo_stop = lbs_eth_stop,
  640. .ndo_start_xmit = lbs_hard_start_xmit,
  641. .ndo_set_mac_address = lbs_set_mac_address,
  642. .ndo_tx_timeout = lbs_tx_timeout,
  643. .ndo_set_multicast_list = lbs_set_multicast_list,
  644. .ndo_change_mtu = eth_change_mtu,
  645. .ndo_validate_addr = eth_validate_addr,
  646. };
  647. /**
  648. * @brief This function adds the card. it will probe the
  649. * card, allocate the lbs_priv and initialize the device.
  650. *
  651. * @param card A pointer to card
  652. * @return A pointer to struct lbs_private structure
  653. */
  654. struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
  655. {
  656. struct net_device *dev;
  657. struct wireless_dev *wdev;
  658. struct lbs_private *priv = NULL;
  659. lbs_deb_enter(LBS_DEB_MAIN);
  660. /* Allocate an Ethernet device and register it */
  661. wdev = lbs_cfg_alloc(dmdev);
  662. if (IS_ERR(wdev)) {
  663. lbs_pr_err("cfg80211 init failed\n");
  664. goto done;
  665. }
  666. wdev->iftype = NL80211_IFTYPE_STATION;
  667. priv = wdev_priv(wdev);
  668. priv->wdev = wdev;
  669. if (lbs_init_adapter(priv)) {
  670. lbs_pr_err("failed to initialize adapter structure.\n");
  671. goto err_wdev;
  672. }
  673. dev = alloc_netdev(0, "wlan%d", ether_setup);
  674. if (!dev) {
  675. dev_err(dmdev, "no memory for network device instance\n");
  676. goto err_adapter;
  677. }
  678. dev->ieee80211_ptr = wdev;
  679. dev->ml_priv = priv;
  680. SET_NETDEV_DEV(dev, dmdev);
  681. wdev->netdev = dev;
  682. priv->dev = dev;
  683. dev->netdev_ops = &lbs_netdev_ops;
  684. dev->watchdog_timeo = 5 * HZ;
  685. dev->ethtool_ops = &lbs_ethtool_ops;
  686. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  687. priv->card = card;
  688. strcpy(dev->name, "wlan%d");
  689. lbs_deb_thread("Starting main thread...\n");
  690. init_waitqueue_head(&priv->waitq);
  691. priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
  692. if (IS_ERR(priv->main_thread)) {
  693. lbs_deb_thread("Error creating main thread.\n");
  694. goto err_ndev;
  695. }
  696. priv->work_thread = create_singlethread_workqueue("lbs_worker");
  697. INIT_WORK(&priv->mcast_work, lbs_set_mcast_worker);
  698. priv->wol_criteria = EHS_REMOVE_WAKEUP;
  699. priv->wol_gpio = 0xff;
  700. priv->wol_gap = 20;
  701. priv->ehs_remove_supported = true;
  702. goto done;
  703. err_ndev:
  704. free_netdev(dev);
  705. err_adapter:
  706. lbs_free_adapter(priv);
  707. err_wdev:
  708. lbs_cfg_free(priv);
  709. priv = NULL;
  710. done:
  711. lbs_deb_leave_args(LBS_DEB_MAIN, "priv %p", priv);
  712. return priv;
  713. }
  714. EXPORT_SYMBOL_GPL(lbs_add_card);
  715. void lbs_remove_card(struct lbs_private *priv)
  716. {
  717. struct net_device *dev = priv->dev;
  718. lbs_deb_enter(LBS_DEB_MAIN);
  719. lbs_remove_mesh(priv);
  720. lbs_scan_deinit(priv);
  721. dev = priv->dev;
  722. cancel_work_sync(&priv->mcast_work);
  723. /* worker thread destruction blocks on the in-flight command which
  724. * should have been cleared already in lbs_stop_card().
  725. */
  726. lbs_deb_main("destroying worker thread\n");
  727. destroy_workqueue(priv->work_thread);
  728. lbs_deb_main("done destroying worker thread\n");
  729. if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
  730. priv->psmode = LBS802_11POWERMODECAM;
  731. lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS, true);
  732. }
  733. if (priv->is_deep_sleep) {
  734. priv->is_deep_sleep = 0;
  735. wake_up_interruptible(&priv->ds_awake_q);
  736. }
  737. priv->is_host_sleep_configured = 0;
  738. priv->is_host_sleep_activated = 0;
  739. wake_up_interruptible(&priv->host_sleep_q);
  740. /* Stop the thread servicing the interrupts */
  741. priv->surpriseremoved = 1;
  742. kthread_stop(priv->main_thread);
  743. lbs_free_adapter(priv);
  744. lbs_cfg_free(priv);
  745. free_netdev(dev);
  746. lbs_deb_leave(LBS_DEB_MAIN);
  747. }
  748. EXPORT_SYMBOL_GPL(lbs_remove_card);
  749. int lbs_rtap_supported(struct lbs_private *priv)
  750. {
  751. if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V5)
  752. return 1;
  753. /* newer firmware use a capability mask */
  754. return ((MRVL_FW_MAJOR_REV(priv->fwrelease) >= MRVL_FW_V10) &&
  755. (priv->fwcapinfo & MESH_CAPINFO_ENABLE_MASK));
  756. }
  757. int lbs_start_card(struct lbs_private *priv)
  758. {
  759. struct net_device *dev = priv->dev;
  760. int ret = -1;
  761. lbs_deb_enter(LBS_DEB_MAIN);
  762. /* poke the firmware */
  763. ret = lbs_setup_firmware(priv);
  764. if (ret)
  765. goto done;
  766. if (lbs_cfg_register(priv)) {
  767. lbs_pr_err("cannot register device\n");
  768. goto done;
  769. }
  770. lbs_update_channel(priv);
  771. lbs_init_mesh(priv);
  772. lbs_debugfs_init_one(priv, dev);
  773. lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
  774. ret = 0;
  775. done:
  776. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  777. return ret;
  778. }
  779. EXPORT_SYMBOL_GPL(lbs_start_card);
  780. void lbs_stop_card(struct lbs_private *priv)
  781. {
  782. struct net_device *dev;
  783. struct cmd_ctrl_node *cmdnode;
  784. unsigned long flags;
  785. lbs_deb_enter(LBS_DEB_MAIN);
  786. if (!priv)
  787. goto out;
  788. dev = priv->dev;
  789. netif_stop_queue(dev);
  790. netif_carrier_off(dev);
  791. lbs_debugfs_remove_one(priv);
  792. lbs_deinit_mesh(priv);
  793. /* Delete the timeout of the currently processing command */
  794. del_timer_sync(&priv->command_timer);
  795. del_timer_sync(&priv->auto_deepsleep_timer);
  796. /* Flush pending command nodes */
  797. spin_lock_irqsave(&priv->driver_lock, flags);
  798. lbs_deb_main("clearing pending commands\n");
  799. list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
  800. cmdnode->result = -ENOENT;
  801. cmdnode->cmdwaitqwoken = 1;
  802. wake_up_interruptible(&cmdnode->cmdwait_q);
  803. }
  804. /* Flush the command the card is currently processing */
  805. if (priv->cur_cmd) {
  806. lbs_deb_main("clearing current command\n");
  807. priv->cur_cmd->result = -ENOENT;
  808. priv->cur_cmd->cmdwaitqwoken = 1;
  809. wake_up_interruptible(&priv->cur_cmd->cmdwait_q);
  810. }
  811. lbs_deb_main("done clearing commands\n");
  812. spin_unlock_irqrestore(&priv->driver_lock, flags);
  813. unregister_netdev(dev);
  814. out:
  815. lbs_deb_leave(LBS_DEB_MAIN);
  816. }
  817. EXPORT_SYMBOL_GPL(lbs_stop_card);
  818. void lbs_queue_event(struct lbs_private *priv, u32 event)
  819. {
  820. unsigned long flags;
  821. lbs_deb_enter(LBS_DEB_THREAD);
  822. spin_lock_irqsave(&priv->driver_lock, flags);
  823. if (priv->psstate == PS_STATE_SLEEP)
  824. priv->psstate = PS_STATE_AWAKE;
  825. kfifo_in(&priv->event_fifo, (unsigned char *) &event, sizeof(u32));
  826. wake_up_interruptible(&priv->waitq);
  827. spin_unlock_irqrestore(&priv->driver_lock, flags);
  828. lbs_deb_leave(LBS_DEB_THREAD);
  829. }
  830. EXPORT_SYMBOL_GPL(lbs_queue_event);
  831. void lbs_notify_command_response(struct lbs_private *priv, u8 resp_idx)
  832. {
  833. lbs_deb_enter(LBS_DEB_THREAD);
  834. if (priv->psstate == PS_STATE_SLEEP)
  835. priv->psstate = PS_STATE_AWAKE;
  836. /* Swap buffers by flipping the response index */
  837. BUG_ON(resp_idx > 1);
  838. priv->resp_idx = resp_idx;
  839. wake_up_interruptible(&priv->waitq);
  840. lbs_deb_leave(LBS_DEB_THREAD);
  841. }
  842. EXPORT_SYMBOL_GPL(lbs_notify_command_response);
  843. /**
  844. * @brief Retrieves two-stage firmware
  845. *
  846. * @param dev A pointer to device structure
  847. * @param user_helper User-defined helper firmware file
  848. * @param user_mainfw User-defined main firmware file
  849. * @param card_model Bus-specific card model ID used to filter firmware table
  850. * elements
  851. * @param fw_table Table of firmware file names and device model numbers
  852. * terminated by an entry with a NULL helper name
  853. * @param helper On success, the helper firmware; caller must free
  854. * @param mainfw On success, the main firmware; caller must free
  855. *
  856. * @return 0 on success, non-zero on failure
  857. */
  858. int lbs_get_firmware(struct device *dev, const char *user_helper,
  859. const char *user_mainfw, u32 card_model,
  860. const struct lbs_fw_table *fw_table,
  861. const struct firmware **helper,
  862. const struct firmware **mainfw)
  863. {
  864. const struct lbs_fw_table *iter;
  865. int ret;
  866. BUG_ON(helper == NULL);
  867. BUG_ON(mainfw == NULL);
  868. /* Try user-specified firmware first */
  869. if (user_helper) {
  870. ret = request_firmware(helper, user_helper, dev);
  871. if (ret) {
  872. lbs_pr_err("couldn't find helper firmware %s",
  873. user_helper);
  874. goto fail;
  875. }
  876. }
  877. if (user_mainfw) {
  878. ret = request_firmware(mainfw, user_mainfw, dev);
  879. if (ret) {
  880. lbs_pr_err("couldn't find main firmware %s",
  881. user_mainfw);
  882. goto fail;
  883. }
  884. }
  885. if (*helper && *mainfw)
  886. return 0;
  887. /* Otherwise search for firmware to use. If neither the helper or
  888. * the main firmware were specified by the user, then we need to
  889. * make sure that found helper & main are from the same entry in
  890. * fw_table.
  891. */
  892. iter = fw_table;
  893. while (iter && iter->helper) {
  894. if (iter->model != card_model)
  895. goto next;
  896. if (*helper == NULL) {
  897. ret = request_firmware(helper, iter->helper, dev);
  898. if (ret)
  899. goto next;
  900. /* If the device has one-stage firmware (ie cf8305) and
  901. * we've got it then we don't need to bother with the
  902. * main firmware.
  903. */
  904. if (iter->fwname == NULL)
  905. return 0;
  906. }
  907. if (*mainfw == NULL) {
  908. ret = request_firmware(mainfw, iter->fwname, dev);
  909. if (ret && !user_helper) {
  910. /* Clear the helper if it wasn't user-specified
  911. * and the main firmware load failed, to ensure
  912. * we don't have mismatched firmware pairs.
  913. */
  914. release_firmware(*helper);
  915. *helper = NULL;
  916. }
  917. }
  918. if (*helper && *mainfw)
  919. return 0;
  920. next:
  921. iter++;
  922. }
  923. fail:
  924. /* Failed */
  925. if (*helper) {
  926. release_firmware(*helper);
  927. *helper = NULL;
  928. }
  929. if (*mainfw) {
  930. release_firmware(*mainfw);
  931. *mainfw = NULL;
  932. }
  933. return -ENOENT;
  934. }
  935. EXPORT_SYMBOL_GPL(lbs_get_firmware);
  936. static int __init lbs_init_module(void)
  937. {
  938. lbs_deb_enter(LBS_DEB_MAIN);
  939. memset(&confirm_sleep, 0, sizeof(confirm_sleep));
  940. confirm_sleep.hdr.command = cpu_to_le16(CMD_802_11_PS_MODE);
  941. confirm_sleep.hdr.size = cpu_to_le16(sizeof(confirm_sleep));
  942. confirm_sleep.action = cpu_to_le16(PS_MODE_ACTION_SLEEP_CONFIRMED);
  943. lbs_debugfs_init();
  944. lbs_deb_leave(LBS_DEB_MAIN);
  945. return 0;
  946. }
  947. static void __exit lbs_exit_module(void)
  948. {
  949. lbs_deb_enter(LBS_DEB_MAIN);
  950. lbs_debugfs_remove();
  951. lbs_deb_leave(LBS_DEB_MAIN);
  952. }
  953. module_init(lbs_init_module);
  954. module_exit(lbs_exit_module);
  955. MODULE_DESCRIPTION("Libertas WLAN Driver Library");
  956. MODULE_AUTHOR("Marvell International Ltd.");
  957. MODULE_LICENSE("GPL");