main.c 28 KB

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