main.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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. priv->authtype_auto = 1;
  589. priv->is_host_sleep_configured = 0;
  590. priv->is_host_sleep_activated = 0;
  591. init_waitqueue_head(&priv->host_sleep_q);
  592. mutex_init(&priv->lock);
  593. setup_timer(&priv->command_timer, lbs_cmd_timeout_handler,
  594. (unsigned long)priv);
  595. setup_timer(&priv->auto_deepsleep_timer, auto_deepsleep_timer_fn,
  596. (unsigned long)priv);
  597. INIT_LIST_HEAD(&priv->cmdfreeq);
  598. INIT_LIST_HEAD(&priv->cmdpendingq);
  599. spin_lock_init(&priv->driver_lock);
  600. /* Allocate the command buffers */
  601. if (lbs_allocate_cmd_buffer(priv)) {
  602. lbs_pr_err("Out of memory allocating command buffers\n");
  603. ret = -ENOMEM;
  604. goto out;
  605. }
  606. priv->resp_idx = 0;
  607. priv->resp_len[0] = priv->resp_len[1] = 0;
  608. /* Create the event FIFO */
  609. ret = kfifo_alloc(&priv->event_fifo, sizeof(u32) * 16, GFP_KERNEL);
  610. if (ret) {
  611. lbs_pr_err("Out of memory allocating event FIFO buffer\n");
  612. goto out;
  613. }
  614. out:
  615. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  616. return ret;
  617. }
  618. static void lbs_free_adapter(struct lbs_private *priv)
  619. {
  620. lbs_deb_enter(LBS_DEB_MAIN);
  621. lbs_free_cmd_buffer(priv);
  622. kfifo_free(&priv->event_fifo);
  623. del_timer(&priv->command_timer);
  624. del_timer(&priv->auto_deepsleep_timer);
  625. lbs_deb_leave(LBS_DEB_MAIN);
  626. }
  627. static const struct net_device_ops lbs_netdev_ops = {
  628. .ndo_open = lbs_dev_open,
  629. .ndo_stop = lbs_eth_stop,
  630. .ndo_start_xmit = lbs_hard_start_xmit,
  631. .ndo_set_mac_address = lbs_set_mac_address,
  632. .ndo_tx_timeout = lbs_tx_timeout,
  633. .ndo_set_multicast_list = lbs_set_multicast_list,
  634. .ndo_change_mtu = eth_change_mtu,
  635. .ndo_validate_addr = eth_validate_addr,
  636. };
  637. /**
  638. * @brief This function adds the card. it will probe the
  639. * card, allocate the lbs_priv and initialize the device.
  640. *
  641. * @param card A pointer to card
  642. * @return A pointer to struct lbs_private structure
  643. */
  644. struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
  645. {
  646. struct net_device *dev;
  647. struct wireless_dev *wdev;
  648. struct lbs_private *priv = NULL;
  649. lbs_deb_enter(LBS_DEB_MAIN);
  650. /* Allocate an Ethernet device and register it */
  651. wdev = lbs_cfg_alloc(dmdev);
  652. if (IS_ERR(wdev)) {
  653. lbs_pr_err("cfg80211 init failed\n");
  654. goto done;
  655. }
  656. wdev->iftype = NL80211_IFTYPE_STATION;
  657. priv = wdev_priv(wdev);
  658. priv->wdev = wdev;
  659. if (lbs_init_adapter(priv)) {
  660. lbs_pr_err("failed to initialize adapter structure.\n");
  661. goto err_wdev;
  662. }
  663. dev = alloc_netdev(0, "wlan%d", ether_setup);
  664. if (!dev) {
  665. dev_err(dmdev, "no memory for network device instance\n");
  666. goto err_adapter;
  667. }
  668. dev->ieee80211_ptr = wdev;
  669. dev->ml_priv = priv;
  670. SET_NETDEV_DEV(dev, dmdev);
  671. wdev->netdev = dev;
  672. priv->dev = dev;
  673. dev->netdev_ops = &lbs_netdev_ops;
  674. dev->watchdog_timeo = 5 * HZ;
  675. dev->ethtool_ops = &lbs_ethtool_ops;
  676. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  677. priv->card = card;
  678. strcpy(dev->name, "wlan%d");
  679. lbs_deb_thread("Starting main thread...\n");
  680. init_waitqueue_head(&priv->waitq);
  681. priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
  682. if (IS_ERR(priv->main_thread)) {
  683. lbs_deb_thread("Error creating main thread.\n");
  684. goto err_ndev;
  685. }
  686. priv->work_thread = create_singlethread_workqueue("lbs_worker");
  687. INIT_WORK(&priv->mcast_work, lbs_set_mcast_worker);
  688. priv->wol_criteria = 0xffffffff;
  689. priv->wol_gpio = 0xff;
  690. priv->wol_gap = 20;
  691. goto done;
  692. err_ndev:
  693. free_netdev(dev);
  694. err_adapter:
  695. lbs_free_adapter(priv);
  696. err_wdev:
  697. lbs_cfg_free(priv);
  698. priv = NULL;
  699. done:
  700. lbs_deb_leave_args(LBS_DEB_MAIN, "priv %p", priv);
  701. return priv;
  702. }
  703. EXPORT_SYMBOL_GPL(lbs_add_card);
  704. void lbs_remove_card(struct lbs_private *priv)
  705. {
  706. struct net_device *dev = priv->dev;
  707. lbs_deb_enter(LBS_DEB_MAIN);
  708. lbs_remove_mesh(priv);
  709. lbs_scan_deinit(priv);
  710. dev = priv->dev;
  711. cancel_work_sync(&priv->mcast_work);
  712. /* worker thread destruction blocks on the in-flight command which
  713. * should have been cleared already in lbs_stop_card().
  714. */
  715. lbs_deb_main("destroying worker thread\n");
  716. destroy_workqueue(priv->work_thread);
  717. lbs_deb_main("done destroying worker thread\n");
  718. if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
  719. priv->psmode = LBS802_11POWERMODECAM;
  720. lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS, true);
  721. }
  722. if (priv->is_deep_sleep) {
  723. priv->is_deep_sleep = 0;
  724. wake_up_interruptible(&priv->ds_awake_q);
  725. }
  726. priv->is_host_sleep_configured = 0;
  727. priv->is_host_sleep_activated = 0;
  728. wake_up_interruptible(&priv->host_sleep_q);
  729. /* Stop the thread servicing the interrupts */
  730. priv->surpriseremoved = 1;
  731. kthread_stop(priv->main_thread);
  732. lbs_free_adapter(priv);
  733. lbs_cfg_free(priv);
  734. priv->dev = NULL;
  735. free_netdev(dev);
  736. lbs_deb_leave(LBS_DEB_MAIN);
  737. }
  738. EXPORT_SYMBOL_GPL(lbs_remove_card);
  739. int lbs_rtap_supported(struct lbs_private *priv)
  740. {
  741. if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V5)
  742. return 1;
  743. /* newer firmware use a capability mask */
  744. return ((MRVL_FW_MAJOR_REV(priv->fwrelease) >= MRVL_FW_V10) &&
  745. (priv->fwcapinfo & MESH_CAPINFO_ENABLE_MASK));
  746. }
  747. int lbs_start_card(struct lbs_private *priv)
  748. {
  749. struct net_device *dev = priv->dev;
  750. int ret = -1;
  751. lbs_deb_enter(LBS_DEB_MAIN);
  752. /* poke the firmware */
  753. ret = lbs_setup_firmware(priv);
  754. if (ret)
  755. goto done;
  756. if (lbs_cfg_register(priv)) {
  757. lbs_pr_err("cannot register device\n");
  758. goto done;
  759. }
  760. lbs_update_channel(priv);
  761. lbs_init_mesh(priv);
  762. lbs_debugfs_init_one(priv, dev);
  763. lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
  764. ret = 0;
  765. done:
  766. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  767. return ret;
  768. }
  769. EXPORT_SYMBOL_GPL(lbs_start_card);
  770. void lbs_stop_card(struct lbs_private *priv)
  771. {
  772. struct net_device *dev;
  773. struct cmd_ctrl_node *cmdnode;
  774. unsigned long flags;
  775. lbs_deb_enter(LBS_DEB_MAIN);
  776. if (!priv)
  777. goto out;
  778. dev = priv->dev;
  779. netif_stop_queue(dev);
  780. netif_carrier_off(dev);
  781. lbs_debugfs_remove_one(priv);
  782. lbs_deinit_mesh(priv);
  783. /* Delete the timeout of the currently processing command */
  784. del_timer_sync(&priv->command_timer);
  785. del_timer_sync(&priv->auto_deepsleep_timer);
  786. /* Flush pending command nodes */
  787. spin_lock_irqsave(&priv->driver_lock, flags);
  788. lbs_deb_main("clearing pending commands\n");
  789. list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
  790. cmdnode->result = -ENOENT;
  791. cmdnode->cmdwaitqwoken = 1;
  792. wake_up_interruptible(&cmdnode->cmdwait_q);
  793. }
  794. /* Flush the command the card is currently processing */
  795. if (priv->cur_cmd) {
  796. lbs_deb_main("clearing current command\n");
  797. priv->cur_cmd->result = -ENOENT;
  798. priv->cur_cmd->cmdwaitqwoken = 1;
  799. wake_up_interruptible(&priv->cur_cmd->cmdwait_q);
  800. }
  801. lbs_deb_main("done clearing commands\n");
  802. spin_unlock_irqrestore(&priv->driver_lock, flags);
  803. unregister_netdev(dev);
  804. out:
  805. lbs_deb_leave(LBS_DEB_MAIN);
  806. }
  807. EXPORT_SYMBOL_GPL(lbs_stop_card);
  808. void lbs_queue_event(struct lbs_private *priv, u32 event)
  809. {
  810. unsigned long flags;
  811. lbs_deb_enter(LBS_DEB_THREAD);
  812. spin_lock_irqsave(&priv->driver_lock, flags);
  813. if (priv->psstate == PS_STATE_SLEEP)
  814. priv->psstate = PS_STATE_AWAKE;
  815. kfifo_in(&priv->event_fifo, (unsigned char *) &event, sizeof(u32));
  816. wake_up_interruptible(&priv->waitq);
  817. spin_unlock_irqrestore(&priv->driver_lock, flags);
  818. lbs_deb_leave(LBS_DEB_THREAD);
  819. }
  820. EXPORT_SYMBOL_GPL(lbs_queue_event);
  821. void lbs_notify_command_response(struct lbs_private *priv, u8 resp_idx)
  822. {
  823. lbs_deb_enter(LBS_DEB_THREAD);
  824. if (priv->psstate == PS_STATE_SLEEP)
  825. priv->psstate = PS_STATE_AWAKE;
  826. /* Swap buffers by flipping the response index */
  827. BUG_ON(resp_idx > 1);
  828. priv->resp_idx = resp_idx;
  829. wake_up_interruptible(&priv->waitq);
  830. lbs_deb_leave(LBS_DEB_THREAD);
  831. }
  832. EXPORT_SYMBOL_GPL(lbs_notify_command_response);
  833. static int __init lbs_init_module(void)
  834. {
  835. lbs_deb_enter(LBS_DEB_MAIN);
  836. memset(&confirm_sleep, 0, sizeof(confirm_sleep));
  837. confirm_sleep.hdr.command = cpu_to_le16(CMD_802_11_PS_MODE);
  838. confirm_sleep.hdr.size = cpu_to_le16(sizeof(confirm_sleep));
  839. confirm_sleep.action = cpu_to_le16(PS_MODE_ACTION_SLEEP_CONFIRMED);
  840. lbs_debugfs_init();
  841. lbs_deb_leave(LBS_DEB_MAIN);
  842. return 0;
  843. }
  844. static void __exit lbs_exit_module(void)
  845. {
  846. lbs_deb_enter(LBS_DEB_MAIN);
  847. lbs_debugfs_remove();
  848. lbs_deb_leave(LBS_DEB_MAIN);
  849. }
  850. module_init(lbs_init_module);
  851. module_exit(lbs_exit_module);
  852. MODULE_DESCRIPTION("Libertas WLAN Driver Library");
  853. MODULE_AUTHOR("Marvell International Ltd.");
  854. MODULE_LICENSE("GPL");