main.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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. if (priv->reset_card)
  471. priv->reset_card(priv);
  472. }
  473. priv->cmd_timed_out = 0;
  474. if (!priv->fw_ready)
  475. continue;
  476. /* Check if we need to confirm Sleep Request received previously */
  477. if (priv->psstate == PS_STATE_PRE_SLEEP &&
  478. !priv->dnld_sent && !priv->cur_cmd) {
  479. if (priv->connect_status == LBS_CONNECTED) {
  480. lbs_deb_thread("pre-sleep, currenttxskb %p, "
  481. "dnld_sent %d, cur_cmd %p\n",
  482. priv->currenttxskb, priv->dnld_sent,
  483. priv->cur_cmd);
  484. lbs_ps_confirm_sleep(priv);
  485. } else {
  486. /* workaround for firmware sending
  487. * deauth/linkloss event immediately
  488. * after sleep request; remove this
  489. * after firmware fixes it
  490. */
  491. priv->psstate = PS_STATE_AWAKE;
  492. netdev_alert(dev,
  493. "ignore PS_SleepConfirm in non-connected state\n");
  494. }
  495. }
  496. /* The PS state is changed during processing of Sleep Request
  497. * event above
  498. */
  499. if ((priv->psstate == PS_STATE_SLEEP) ||
  500. (priv->psstate == PS_STATE_PRE_SLEEP))
  501. continue;
  502. if (priv->is_deep_sleep)
  503. continue;
  504. /* Execute the next command */
  505. if (!priv->dnld_sent && !priv->cur_cmd)
  506. lbs_execute_next_command(priv);
  507. spin_lock_irq(&priv->driver_lock);
  508. if (!priv->dnld_sent && priv->tx_pending_len > 0) {
  509. int ret = priv->hw_host_to_card(priv, MVMS_DAT,
  510. priv->tx_pending_buf,
  511. priv->tx_pending_len);
  512. if (ret) {
  513. lbs_deb_tx("host_to_card failed %d\n", ret);
  514. priv->dnld_sent = DNLD_RES_RECEIVED;
  515. } else {
  516. mod_timer(&priv->tx_lockup_timer,
  517. jiffies + (HZ * 5));
  518. }
  519. priv->tx_pending_len = 0;
  520. if (!priv->currenttxskb) {
  521. /* We can wake the queues immediately if we aren't
  522. waiting for TX feedback */
  523. if (priv->connect_status == LBS_CONNECTED)
  524. netif_wake_queue(priv->dev);
  525. if (priv->mesh_dev &&
  526. netif_running(priv->mesh_dev))
  527. netif_wake_queue(priv->mesh_dev);
  528. }
  529. }
  530. spin_unlock_irq(&priv->driver_lock);
  531. }
  532. del_timer(&priv->command_timer);
  533. del_timer(&priv->tx_lockup_timer);
  534. del_timer(&priv->auto_deepsleep_timer);
  535. lbs_deb_leave(LBS_DEB_THREAD);
  536. return 0;
  537. }
  538. /**
  539. * lbs_setup_firmware - gets the HW spec from the firmware and sets
  540. * some basic parameters
  541. *
  542. * @priv: A pointer to &struct lbs_private structure
  543. * returns: 0 or -1
  544. */
  545. static int lbs_setup_firmware(struct lbs_private *priv)
  546. {
  547. int ret = -1;
  548. s16 curlevel = 0, minlevel = 0, maxlevel = 0;
  549. lbs_deb_enter(LBS_DEB_FW);
  550. /* Read MAC address from firmware */
  551. memset(priv->current_addr, 0xff, ETH_ALEN);
  552. ret = lbs_update_hw_spec(priv);
  553. if (ret)
  554. goto done;
  555. /* Read power levels if available */
  556. ret = lbs_get_tx_power(priv, &curlevel, &minlevel, &maxlevel);
  557. if (ret == 0) {
  558. priv->txpower_cur = curlevel;
  559. priv->txpower_min = minlevel;
  560. priv->txpower_max = maxlevel;
  561. }
  562. /* Send cmd to FW to enable 11D function */
  563. ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_11D_ENABLE, 1);
  564. lbs_set_mac_control(priv);
  565. done:
  566. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  567. return ret;
  568. }
  569. int lbs_suspend(struct lbs_private *priv)
  570. {
  571. int ret;
  572. lbs_deb_enter(LBS_DEB_FW);
  573. if (priv->is_deep_sleep) {
  574. ret = lbs_set_deep_sleep(priv, 0);
  575. if (ret) {
  576. netdev_err(priv->dev,
  577. "deep sleep cancellation failed: %d\n", ret);
  578. return ret;
  579. }
  580. priv->deep_sleep_required = 1;
  581. }
  582. ret = lbs_set_host_sleep(priv, 1);
  583. netif_device_detach(priv->dev);
  584. if (priv->mesh_dev)
  585. netif_device_detach(priv->mesh_dev);
  586. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  587. return ret;
  588. }
  589. EXPORT_SYMBOL_GPL(lbs_suspend);
  590. int lbs_resume(struct lbs_private *priv)
  591. {
  592. int ret;
  593. lbs_deb_enter(LBS_DEB_FW);
  594. ret = lbs_set_host_sleep(priv, 0);
  595. netif_device_attach(priv->dev);
  596. if (priv->mesh_dev)
  597. netif_device_attach(priv->mesh_dev);
  598. if (priv->deep_sleep_required) {
  599. priv->deep_sleep_required = 0;
  600. ret = lbs_set_deep_sleep(priv, 1);
  601. if (ret)
  602. netdev_err(priv->dev,
  603. "deep sleep activation failed: %d\n", ret);
  604. }
  605. if (priv->setup_fw_on_resume)
  606. ret = lbs_setup_firmware(priv);
  607. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  608. return ret;
  609. }
  610. EXPORT_SYMBOL_GPL(lbs_resume);
  611. /**
  612. * lbs_cmd_timeout_handler - handles the timeout of command sending.
  613. * It will re-send the same command again.
  614. *
  615. * @data: &struct lbs_private pointer
  616. */
  617. static void lbs_cmd_timeout_handler(unsigned long data)
  618. {
  619. struct lbs_private *priv = (struct lbs_private *)data;
  620. unsigned long flags;
  621. lbs_deb_enter(LBS_DEB_CMD);
  622. spin_lock_irqsave(&priv->driver_lock, flags);
  623. if (!priv->cur_cmd)
  624. goto out;
  625. netdev_info(priv->dev, "command 0x%04x timed out\n",
  626. le16_to_cpu(priv->cur_cmd->cmdbuf->command));
  627. priv->cmd_timed_out = 1;
  628. /*
  629. * If the device didn't even acknowledge the command, reset the state
  630. * so that we don't block all future commands due to this one timeout.
  631. */
  632. if (priv->dnld_sent == DNLD_CMD_SENT)
  633. priv->dnld_sent = DNLD_RES_RECEIVED;
  634. wake_up(&priv->waitq);
  635. out:
  636. spin_unlock_irqrestore(&priv->driver_lock, flags);
  637. lbs_deb_leave(LBS_DEB_CMD);
  638. }
  639. /**
  640. * lbs_tx_lockup_handler - handles the timeout of the passing of TX frames
  641. * to the hardware. This is known to frequently happen with SD8686 when
  642. * waking up after a Wake-on-WLAN-triggered resume.
  643. *
  644. * @data: &struct lbs_private pointer
  645. */
  646. static void lbs_tx_lockup_handler(unsigned long data)
  647. {
  648. struct lbs_private *priv = (struct lbs_private *)data;
  649. unsigned long flags;
  650. lbs_deb_enter(LBS_DEB_TX);
  651. spin_lock_irqsave(&priv->driver_lock, flags);
  652. netdev_info(priv->dev, "TX lockup detected\n");
  653. if (priv->reset_card)
  654. priv->reset_card(priv);
  655. priv->dnld_sent = DNLD_RES_RECEIVED;
  656. wake_up_interruptible(&priv->waitq);
  657. spin_unlock_irqrestore(&priv->driver_lock, flags);
  658. lbs_deb_leave(LBS_DEB_TX);
  659. }
  660. /**
  661. * auto_deepsleep_timer_fn - put the device back to deep sleep mode when
  662. * timer expires and no activity (command, event, data etc.) is detected.
  663. * @data: &struct lbs_private pointer
  664. * returns: N/A
  665. */
  666. static void auto_deepsleep_timer_fn(unsigned long data)
  667. {
  668. struct lbs_private *priv = (struct lbs_private *)data;
  669. lbs_deb_enter(LBS_DEB_CMD);
  670. if (priv->is_activity_detected) {
  671. priv->is_activity_detected = 0;
  672. } else {
  673. if (priv->is_auto_deep_sleep_enabled &&
  674. (!priv->wakeup_dev_required) &&
  675. (priv->connect_status != LBS_CONNECTED)) {
  676. struct cmd_header cmd;
  677. lbs_deb_main("Entering auto deep sleep mode...\n");
  678. memset(&cmd, 0, sizeof(cmd));
  679. cmd.size = cpu_to_le16(sizeof(cmd));
  680. lbs_cmd_async(priv, CMD_802_11_DEEP_SLEEP, &cmd,
  681. sizeof(cmd));
  682. }
  683. }
  684. mod_timer(&priv->auto_deepsleep_timer , jiffies +
  685. (priv->auto_deep_sleep_timeout * HZ)/1000);
  686. lbs_deb_leave(LBS_DEB_CMD);
  687. }
  688. int lbs_enter_auto_deep_sleep(struct lbs_private *priv)
  689. {
  690. lbs_deb_enter(LBS_DEB_SDIO);
  691. priv->is_auto_deep_sleep_enabled = 1;
  692. if (priv->is_deep_sleep)
  693. priv->wakeup_dev_required = 1;
  694. mod_timer(&priv->auto_deepsleep_timer ,
  695. jiffies + (priv->auto_deep_sleep_timeout * HZ)/1000);
  696. lbs_deb_leave(LBS_DEB_SDIO);
  697. return 0;
  698. }
  699. int lbs_exit_auto_deep_sleep(struct lbs_private *priv)
  700. {
  701. lbs_deb_enter(LBS_DEB_SDIO);
  702. priv->is_auto_deep_sleep_enabled = 0;
  703. priv->auto_deep_sleep_timeout = 0;
  704. del_timer(&priv->auto_deepsleep_timer);
  705. lbs_deb_leave(LBS_DEB_SDIO);
  706. return 0;
  707. }
  708. static int lbs_init_adapter(struct lbs_private *priv)
  709. {
  710. int ret;
  711. lbs_deb_enter(LBS_DEB_MAIN);
  712. memset(priv->current_addr, 0xff, ETH_ALEN);
  713. priv->connect_status = LBS_DISCONNECTED;
  714. priv->channel = DEFAULT_AD_HOC_CHANNEL;
  715. priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
  716. priv->radio_on = 1;
  717. priv->psmode = LBS802_11POWERMODECAM;
  718. priv->psstate = PS_STATE_FULL_POWER;
  719. priv->is_deep_sleep = 0;
  720. priv->is_auto_deep_sleep_enabled = 0;
  721. priv->deep_sleep_required = 0;
  722. priv->wakeup_dev_required = 0;
  723. init_waitqueue_head(&priv->ds_awake_q);
  724. init_waitqueue_head(&priv->scan_q);
  725. priv->authtype_auto = 1;
  726. priv->is_host_sleep_configured = 0;
  727. priv->is_host_sleep_activated = 0;
  728. init_waitqueue_head(&priv->host_sleep_q);
  729. init_waitqueue_head(&priv->fw_waitq);
  730. mutex_init(&priv->lock);
  731. setup_timer(&priv->command_timer, lbs_cmd_timeout_handler,
  732. (unsigned long)priv);
  733. setup_timer(&priv->tx_lockup_timer, lbs_tx_lockup_handler,
  734. (unsigned long)priv);
  735. setup_timer(&priv->auto_deepsleep_timer, auto_deepsleep_timer_fn,
  736. (unsigned long)priv);
  737. INIT_LIST_HEAD(&priv->cmdfreeq);
  738. INIT_LIST_HEAD(&priv->cmdpendingq);
  739. spin_lock_init(&priv->driver_lock);
  740. /* Allocate the command buffers */
  741. if (lbs_allocate_cmd_buffer(priv)) {
  742. pr_err("Out of memory allocating command buffers\n");
  743. ret = -ENOMEM;
  744. goto out;
  745. }
  746. priv->resp_idx = 0;
  747. priv->resp_len[0] = priv->resp_len[1] = 0;
  748. /* Create the event FIFO */
  749. ret = kfifo_alloc(&priv->event_fifo, sizeof(u32) * 16, GFP_KERNEL);
  750. if (ret) {
  751. pr_err("Out of memory allocating event FIFO buffer\n");
  752. goto out;
  753. }
  754. out:
  755. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  756. return ret;
  757. }
  758. static void lbs_free_adapter(struct lbs_private *priv)
  759. {
  760. lbs_deb_enter(LBS_DEB_MAIN);
  761. lbs_free_cmd_buffer(priv);
  762. kfifo_free(&priv->event_fifo);
  763. del_timer(&priv->command_timer);
  764. del_timer(&priv->tx_lockup_timer);
  765. del_timer(&priv->auto_deepsleep_timer);
  766. lbs_deb_leave(LBS_DEB_MAIN);
  767. }
  768. static const struct net_device_ops lbs_netdev_ops = {
  769. .ndo_open = lbs_dev_open,
  770. .ndo_stop = lbs_eth_stop,
  771. .ndo_start_xmit = lbs_hard_start_xmit,
  772. .ndo_set_mac_address = lbs_set_mac_address,
  773. .ndo_set_rx_mode = lbs_set_multicast_list,
  774. .ndo_change_mtu = eth_change_mtu,
  775. .ndo_validate_addr = eth_validate_addr,
  776. };
  777. /**
  778. * lbs_add_card - adds the card. It will probe the
  779. * card, allocate the lbs_priv and initialize the device.
  780. *
  781. * @card: A pointer to card
  782. * @dmdev: A pointer to &struct device
  783. * returns: A pointer to &struct lbs_private structure
  784. */
  785. struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
  786. {
  787. struct net_device *dev;
  788. struct wireless_dev *wdev;
  789. struct lbs_private *priv = NULL;
  790. lbs_deb_enter(LBS_DEB_MAIN);
  791. /* Allocate an Ethernet device and register it */
  792. wdev = lbs_cfg_alloc(dmdev);
  793. if (IS_ERR(wdev)) {
  794. pr_err("cfg80211 init failed\n");
  795. goto done;
  796. }
  797. wdev->iftype = NL80211_IFTYPE_STATION;
  798. priv = wdev_priv(wdev);
  799. priv->wdev = wdev;
  800. if (lbs_init_adapter(priv)) {
  801. pr_err("failed to initialize adapter structure\n");
  802. goto err_wdev;
  803. }
  804. dev = alloc_netdev(0, "wlan%d", ether_setup);
  805. if (!dev) {
  806. dev_err(dmdev, "no memory for network device instance\n");
  807. goto err_adapter;
  808. }
  809. dev->ieee80211_ptr = wdev;
  810. dev->ml_priv = priv;
  811. SET_NETDEV_DEV(dev, dmdev);
  812. wdev->netdev = dev;
  813. priv->dev = dev;
  814. dev->netdev_ops = &lbs_netdev_ops;
  815. dev->watchdog_timeo = 5 * HZ;
  816. dev->ethtool_ops = &lbs_ethtool_ops;
  817. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  818. priv->card = card;
  819. strcpy(dev->name, "wlan%d");
  820. lbs_deb_thread("Starting main thread...\n");
  821. init_waitqueue_head(&priv->waitq);
  822. priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
  823. if (IS_ERR(priv->main_thread)) {
  824. lbs_deb_thread("Error creating main thread.\n");
  825. goto err_ndev;
  826. }
  827. priv->work_thread = create_singlethread_workqueue("lbs_worker");
  828. INIT_WORK(&priv->mcast_work, lbs_set_mcast_worker);
  829. priv->wol_criteria = EHS_REMOVE_WAKEUP;
  830. priv->wol_gpio = 0xff;
  831. priv->wol_gap = 20;
  832. priv->ehs_remove_supported = true;
  833. goto done;
  834. err_ndev:
  835. free_netdev(dev);
  836. err_adapter:
  837. lbs_free_adapter(priv);
  838. err_wdev:
  839. lbs_cfg_free(priv);
  840. priv = NULL;
  841. done:
  842. lbs_deb_leave_args(LBS_DEB_MAIN, "priv %p", priv);
  843. return priv;
  844. }
  845. EXPORT_SYMBOL_GPL(lbs_add_card);
  846. void lbs_remove_card(struct lbs_private *priv)
  847. {
  848. struct net_device *dev = priv->dev;
  849. lbs_deb_enter(LBS_DEB_MAIN);
  850. lbs_remove_mesh(priv);
  851. if (priv->wiphy_registered)
  852. lbs_scan_deinit(priv);
  853. lbs_wait_for_firmware_load(priv);
  854. /* worker thread destruction blocks on the in-flight command which
  855. * should have been cleared already in lbs_stop_card().
  856. */
  857. lbs_deb_main("destroying worker thread\n");
  858. destroy_workqueue(priv->work_thread);
  859. lbs_deb_main("done destroying worker thread\n");
  860. if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
  861. priv->psmode = LBS802_11POWERMODECAM;
  862. lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS, true);
  863. }
  864. if (priv->is_deep_sleep) {
  865. priv->is_deep_sleep = 0;
  866. wake_up_interruptible(&priv->ds_awake_q);
  867. }
  868. priv->is_host_sleep_configured = 0;
  869. priv->is_host_sleep_activated = 0;
  870. wake_up_interruptible(&priv->host_sleep_q);
  871. /* Stop the thread servicing the interrupts */
  872. priv->surpriseremoved = 1;
  873. kthread_stop(priv->main_thread);
  874. lbs_free_adapter(priv);
  875. lbs_cfg_free(priv);
  876. free_netdev(dev);
  877. lbs_deb_leave(LBS_DEB_MAIN);
  878. }
  879. EXPORT_SYMBOL_GPL(lbs_remove_card);
  880. int lbs_rtap_supported(struct lbs_private *priv)
  881. {
  882. if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V5)
  883. return 1;
  884. /* newer firmware use a capability mask */
  885. return ((MRVL_FW_MAJOR_REV(priv->fwrelease) >= MRVL_FW_V10) &&
  886. (priv->fwcapinfo & MESH_CAPINFO_ENABLE_MASK));
  887. }
  888. int lbs_start_card(struct lbs_private *priv)
  889. {
  890. struct net_device *dev = priv->dev;
  891. int ret = -1;
  892. lbs_deb_enter(LBS_DEB_MAIN);
  893. /* poke the firmware */
  894. ret = lbs_setup_firmware(priv);
  895. if (ret)
  896. goto done;
  897. if (!lbs_disablemesh)
  898. lbs_init_mesh(priv);
  899. else
  900. pr_info("%s: mesh disabled\n", dev->name);
  901. if (lbs_cfg_register(priv)) {
  902. pr_err("cannot register device\n");
  903. goto done;
  904. }
  905. if (lbs_mesh_activated(priv))
  906. lbs_start_mesh(priv);
  907. lbs_debugfs_init_one(priv, dev);
  908. netdev_info(dev, "Marvell WLAN 802.11 adapter\n");
  909. ret = 0;
  910. done:
  911. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  912. return ret;
  913. }
  914. EXPORT_SYMBOL_GPL(lbs_start_card);
  915. void lbs_stop_card(struct lbs_private *priv)
  916. {
  917. struct net_device *dev;
  918. lbs_deb_enter(LBS_DEB_MAIN);
  919. if (!priv)
  920. goto out;
  921. dev = priv->dev;
  922. /* If the netdev isn't registered, it means that lbs_start_card() was
  923. * never called so we have nothing to do here. */
  924. if (dev->reg_state != NETREG_REGISTERED)
  925. goto out;
  926. netif_stop_queue(dev);
  927. netif_carrier_off(dev);
  928. lbs_debugfs_remove_one(priv);
  929. lbs_deinit_mesh(priv);
  930. unregister_netdev(dev);
  931. out:
  932. lbs_deb_leave(LBS_DEB_MAIN);
  933. }
  934. EXPORT_SYMBOL_GPL(lbs_stop_card);
  935. void lbs_queue_event(struct lbs_private *priv, u32 event)
  936. {
  937. unsigned long flags;
  938. lbs_deb_enter(LBS_DEB_THREAD);
  939. spin_lock_irqsave(&priv->driver_lock, flags);
  940. if (priv->psstate == PS_STATE_SLEEP)
  941. priv->psstate = PS_STATE_AWAKE;
  942. kfifo_in(&priv->event_fifo, (unsigned char *) &event, sizeof(u32));
  943. wake_up(&priv->waitq);
  944. spin_unlock_irqrestore(&priv->driver_lock, flags);
  945. lbs_deb_leave(LBS_DEB_THREAD);
  946. }
  947. EXPORT_SYMBOL_GPL(lbs_queue_event);
  948. void lbs_notify_command_response(struct lbs_private *priv, u8 resp_idx)
  949. {
  950. lbs_deb_enter(LBS_DEB_THREAD);
  951. if (priv->psstate == PS_STATE_SLEEP)
  952. priv->psstate = PS_STATE_AWAKE;
  953. /* Swap buffers by flipping the response index */
  954. BUG_ON(resp_idx > 1);
  955. priv->resp_idx = resp_idx;
  956. wake_up(&priv->waitq);
  957. lbs_deb_leave(LBS_DEB_THREAD);
  958. }
  959. EXPORT_SYMBOL_GPL(lbs_notify_command_response);
  960. static int __init lbs_init_module(void)
  961. {
  962. lbs_deb_enter(LBS_DEB_MAIN);
  963. memset(&confirm_sleep, 0, sizeof(confirm_sleep));
  964. confirm_sleep.hdr.command = cpu_to_le16(CMD_802_11_PS_MODE);
  965. confirm_sleep.hdr.size = cpu_to_le16(sizeof(confirm_sleep));
  966. confirm_sleep.action = cpu_to_le16(PS_MODE_ACTION_SLEEP_CONFIRMED);
  967. lbs_debugfs_init();
  968. lbs_deb_leave(LBS_DEB_MAIN);
  969. return 0;
  970. }
  971. static void __exit lbs_exit_module(void)
  972. {
  973. lbs_deb_enter(LBS_DEB_MAIN);
  974. lbs_debugfs_remove();
  975. lbs_deb_leave(LBS_DEB_MAIN);
  976. }
  977. module_init(lbs_init_module);
  978. module_exit(lbs_exit_module);
  979. MODULE_DESCRIPTION("Libertas WLAN Driver Library");
  980. MODULE_AUTHOR("Marvell International Ltd.");
  981. MODULE_LICENSE("GPL");