main.c 29 KB

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