main.c 29 KB

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