main.c 29 KB

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