main.c 29 KB

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