cmd.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760
  1. /**
  2. * This file contains the handling of command.
  3. * It prepares command and sends it to firmware when it is ready.
  4. */
  5. #include <linux/kfifo.h>
  6. #include <linux/sched.h>
  7. #include <linux/slab.h>
  8. #include "decl.h"
  9. #include "cfg.h"
  10. #include "cmd.h"
  11. static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv);
  12. /**
  13. * @brief Simple callback that copies response back into command
  14. *
  15. * @param priv A pointer to struct lbs_private structure
  16. * @param extra A pointer to the original command structure for which
  17. * 'resp' is a response
  18. * @param resp A pointer to the command response
  19. *
  20. * @return 0 on success, error on failure
  21. */
  22. int lbs_cmd_copyback(struct lbs_private *priv, unsigned long extra,
  23. struct cmd_header *resp)
  24. {
  25. struct cmd_header *buf = (void *)extra;
  26. uint16_t copy_len;
  27. copy_len = min(le16_to_cpu(buf->size), le16_to_cpu(resp->size));
  28. memcpy(buf, resp, copy_len);
  29. return 0;
  30. }
  31. EXPORT_SYMBOL_GPL(lbs_cmd_copyback);
  32. /**
  33. * @brief Simple callback that ignores the result. Use this if
  34. * you just want to send a command to the hardware, but don't
  35. * care for the result.
  36. *
  37. * @param priv ignored
  38. * @param extra ignored
  39. * @param resp ignored
  40. *
  41. * @return 0 for success
  42. */
  43. static int lbs_cmd_async_callback(struct lbs_private *priv, unsigned long extra,
  44. struct cmd_header *resp)
  45. {
  46. return 0;
  47. }
  48. /**
  49. * @brief Checks whether a command is allowed in Power Save mode
  50. *
  51. * @param command the command ID
  52. * @return 1 if allowed, 0 if not allowed
  53. */
  54. static u8 is_command_allowed_in_ps(u16 cmd)
  55. {
  56. switch (cmd) {
  57. case CMD_802_11_RSSI:
  58. return 1;
  59. case CMD_802_11_HOST_SLEEP_CFG:
  60. return 1;
  61. default:
  62. break;
  63. }
  64. return 0;
  65. }
  66. /**
  67. * @brief This function checks if the command is allowed.
  68. *
  69. * @param priv A pointer to lbs_private structure
  70. * @return allowed or not allowed.
  71. */
  72. static int lbs_is_cmd_allowed(struct lbs_private *priv)
  73. {
  74. int ret = 1;
  75. lbs_deb_enter(LBS_DEB_CMD);
  76. if (!priv->is_auto_deep_sleep_enabled) {
  77. if (priv->is_deep_sleep) {
  78. lbs_deb_cmd("command not allowed in deep sleep\n");
  79. ret = 0;
  80. }
  81. }
  82. lbs_deb_leave(LBS_DEB_CMD);
  83. return ret;
  84. }
  85. /**
  86. * @brief Updates the hardware details like MAC address and regulatory region
  87. *
  88. * @param priv A pointer to struct lbs_private structure
  89. *
  90. * @return 0 on success, error on failure
  91. */
  92. int lbs_update_hw_spec(struct lbs_private *priv)
  93. {
  94. struct cmd_ds_get_hw_spec cmd;
  95. int ret = -1;
  96. u32 i;
  97. lbs_deb_enter(LBS_DEB_CMD);
  98. memset(&cmd, 0, sizeof(cmd));
  99. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  100. memcpy(cmd.permanentaddr, priv->current_addr, ETH_ALEN);
  101. ret = lbs_cmd_with_response(priv, CMD_GET_HW_SPEC, &cmd);
  102. if (ret)
  103. goto out;
  104. priv->fwcapinfo = le32_to_cpu(cmd.fwcapinfo);
  105. /* The firmware release is in an interesting format: the patch
  106. * level is in the most significant nibble ... so fix that: */
  107. priv->fwrelease = le32_to_cpu(cmd.fwrelease);
  108. priv->fwrelease = (priv->fwrelease << 8) |
  109. (priv->fwrelease >> 24 & 0xff);
  110. /* Some firmware capabilities:
  111. * CF card firmware 5.0.16p0: cap 0x00000303
  112. * USB dongle firmware 5.110.17p2: cap 0x00000303
  113. */
  114. lbs_pr_info("%pM, fw %u.%u.%up%u, cap 0x%08x\n",
  115. cmd.permanentaddr,
  116. priv->fwrelease >> 24 & 0xff,
  117. priv->fwrelease >> 16 & 0xff,
  118. priv->fwrelease >> 8 & 0xff,
  119. priv->fwrelease & 0xff,
  120. priv->fwcapinfo);
  121. lbs_deb_cmd("GET_HW_SPEC: hardware interface 0x%x, hardware spec 0x%04x\n",
  122. cmd.hwifversion, cmd.version);
  123. /* Clamp region code to 8-bit since FW spec indicates that it should
  124. * only ever be 8-bit, even though the field size is 16-bit. Some firmware
  125. * returns non-zero high 8 bits here.
  126. *
  127. * Firmware version 4.0.102 used in CF8381 has region code shifted. We
  128. * need to check for this problem and handle it properly.
  129. */
  130. if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V4)
  131. priv->regioncode = (le16_to_cpu(cmd.regioncode) >> 8) & 0xFF;
  132. else
  133. priv->regioncode = le16_to_cpu(cmd.regioncode) & 0xFF;
  134. for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
  135. /* use the region code to search for the index */
  136. if (priv->regioncode == lbs_region_code_to_index[i])
  137. break;
  138. }
  139. /* if it's unidentified region code, use the default (USA) */
  140. if (i >= MRVDRV_MAX_REGION_CODE) {
  141. priv->regioncode = 0x10;
  142. lbs_pr_info("unidentified region code; using the default (USA)\n");
  143. }
  144. if (priv->current_addr[0] == 0xff)
  145. memmove(priv->current_addr, cmd.permanentaddr, ETH_ALEN);
  146. memcpy(priv->dev->dev_addr, priv->current_addr, ETH_ALEN);
  147. if (priv->mesh_dev)
  148. memcpy(priv->mesh_dev->dev_addr, priv->current_addr, ETH_ALEN);
  149. out:
  150. lbs_deb_leave(LBS_DEB_CMD);
  151. return ret;
  152. }
  153. static int lbs_ret_host_sleep_cfg(struct lbs_private *priv, unsigned long dummy,
  154. struct cmd_header *resp)
  155. {
  156. lbs_deb_enter(LBS_DEB_CMD);
  157. if (priv->is_host_sleep_activated) {
  158. priv->is_host_sleep_configured = 0;
  159. if (priv->psstate == PS_STATE_FULL_POWER) {
  160. priv->is_host_sleep_activated = 0;
  161. wake_up_interruptible(&priv->host_sleep_q);
  162. }
  163. } else {
  164. priv->is_host_sleep_configured = 1;
  165. }
  166. lbs_deb_leave(LBS_DEB_CMD);
  167. return 0;
  168. }
  169. int lbs_host_sleep_cfg(struct lbs_private *priv, uint32_t criteria,
  170. struct wol_config *p_wol_config)
  171. {
  172. struct cmd_ds_host_sleep cmd_config;
  173. int ret;
  174. cmd_config.hdr.size = cpu_to_le16(sizeof(cmd_config));
  175. cmd_config.criteria = cpu_to_le32(criteria);
  176. cmd_config.gpio = priv->wol_gpio;
  177. cmd_config.gap = priv->wol_gap;
  178. if (p_wol_config != NULL)
  179. memcpy((uint8_t *)&cmd_config.wol_conf, (uint8_t *)p_wol_config,
  180. sizeof(struct wol_config));
  181. else
  182. cmd_config.wol_conf.action = CMD_ACT_ACTION_NONE;
  183. ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_CFG, &cmd_config.hdr,
  184. le16_to_cpu(cmd_config.hdr.size),
  185. lbs_ret_host_sleep_cfg, 0);
  186. if (!ret) {
  187. if (p_wol_config)
  188. memcpy((uint8_t *) p_wol_config,
  189. (uint8_t *)&cmd_config.wol_conf,
  190. sizeof(struct wol_config));
  191. } else {
  192. lbs_pr_info("HOST_SLEEP_CFG failed %d\n", ret);
  193. }
  194. return ret;
  195. }
  196. EXPORT_SYMBOL_GPL(lbs_host_sleep_cfg);
  197. static int lbs_cmd_802_11_ps_mode(struct cmd_ds_command *cmd,
  198. u16 cmd_action)
  199. {
  200. struct cmd_ds_802_11_ps_mode *psm = &cmd->params.psmode;
  201. lbs_deb_enter(LBS_DEB_CMD);
  202. cmd->command = cpu_to_le16(CMD_802_11_PS_MODE);
  203. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ps_mode) +
  204. sizeof(struct cmd_header));
  205. psm->action = cpu_to_le16(cmd_action);
  206. psm->multipledtim = 0;
  207. switch (cmd_action) {
  208. case CMD_SUBCMD_ENTER_PS:
  209. lbs_deb_cmd("PS command:" "SubCode- Enter PS\n");
  210. psm->locallisteninterval = 0;
  211. psm->nullpktinterval = 0;
  212. psm->multipledtim =
  213. cpu_to_le16(MRVDRV_DEFAULT_MULTIPLE_DTIM);
  214. break;
  215. case CMD_SUBCMD_EXIT_PS:
  216. lbs_deb_cmd("PS command:" "SubCode- Exit PS\n");
  217. break;
  218. case CMD_SUBCMD_SLEEP_CONFIRMED:
  219. lbs_deb_cmd("PS command: SubCode- sleep confirm\n");
  220. break;
  221. default:
  222. break;
  223. }
  224. lbs_deb_leave(LBS_DEB_CMD);
  225. return 0;
  226. }
  227. int lbs_cmd_802_11_sleep_params(struct lbs_private *priv, uint16_t cmd_action,
  228. struct sleep_params *sp)
  229. {
  230. struct cmd_ds_802_11_sleep_params cmd;
  231. int ret;
  232. lbs_deb_enter(LBS_DEB_CMD);
  233. if (cmd_action == CMD_ACT_GET) {
  234. memset(&cmd, 0, sizeof(cmd));
  235. } else {
  236. cmd.error = cpu_to_le16(sp->sp_error);
  237. cmd.offset = cpu_to_le16(sp->sp_offset);
  238. cmd.stabletime = cpu_to_le16(sp->sp_stabletime);
  239. cmd.calcontrol = sp->sp_calcontrol;
  240. cmd.externalsleepclk = sp->sp_extsleepclk;
  241. cmd.reserved = cpu_to_le16(sp->sp_reserved);
  242. }
  243. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  244. cmd.action = cpu_to_le16(cmd_action);
  245. ret = lbs_cmd_with_response(priv, CMD_802_11_SLEEP_PARAMS, &cmd);
  246. if (!ret) {
  247. lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, "
  248. "calcontrol 0x%x extsleepclk 0x%x\n",
  249. le16_to_cpu(cmd.error), le16_to_cpu(cmd.offset),
  250. le16_to_cpu(cmd.stabletime), cmd.calcontrol,
  251. cmd.externalsleepclk);
  252. sp->sp_error = le16_to_cpu(cmd.error);
  253. sp->sp_offset = le16_to_cpu(cmd.offset);
  254. sp->sp_stabletime = le16_to_cpu(cmd.stabletime);
  255. sp->sp_calcontrol = cmd.calcontrol;
  256. sp->sp_extsleepclk = cmd.externalsleepclk;
  257. sp->sp_reserved = le16_to_cpu(cmd.reserved);
  258. }
  259. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  260. return 0;
  261. }
  262. static int lbs_wait_for_ds_awake(struct lbs_private *priv)
  263. {
  264. int ret = 0;
  265. lbs_deb_enter(LBS_DEB_CMD);
  266. if (priv->is_deep_sleep) {
  267. if (!wait_event_interruptible_timeout(priv->ds_awake_q,
  268. !priv->is_deep_sleep, (10 * HZ))) {
  269. lbs_pr_err("ds_awake_q: timer expired\n");
  270. ret = -1;
  271. }
  272. }
  273. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  274. return ret;
  275. }
  276. int lbs_set_deep_sleep(struct lbs_private *priv, int deep_sleep)
  277. {
  278. int ret = 0;
  279. lbs_deb_enter(LBS_DEB_CMD);
  280. if (deep_sleep) {
  281. if (priv->is_deep_sleep != 1) {
  282. lbs_deb_cmd("deep sleep: sleep\n");
  283. BUG_ON(!priv->enter_deep_sleep);
  284. ret = priv->enter_deep_sleep(priv);
  285. if (!ret) {
  286. netif_stop_queue(priv->dev);
  287. netif_carrier_off(priv->dev);
  288. }
  289. } else {
  290. lbs_pr_err("deep sleep: already enabled\n");
  291. }
  292. } else {
  293. if (priv->is_deep_sleep) {
  294. lbs_deb_cmd("deep sleep: wakeup\n");
  295. BUG_ON(!priv->exit_deep_sleep);
  296. ret = priv->exit_deep_sleep(priv);
  297. if (!ret) {
  298. ret = lbs_wait_for_ds_awake(priv);
  299. if (ret)
  300. lbs_pr_err("deep sleep: wakeup"
  301. "failed\n");
  302. }
  303. }
  304. }
  305. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  306. return ret;
  307. }
  308. static int lbs_ret_host_sleep_activate(struct lbs_private *priv,
  309. unsigned long dummy,
  310. struct cmd_header *cmd)
  311. {
  312. lbs_deb_enter(LBS_DEB_FW);
  313. priv->is_host_sleep_activated = 1;
  314. wake_up_interruptible(&priv->host_sleep_q);
  315. lbs_deb_leave(LBS_DEB_FW);
  316. return 0;
  317. }
  318. int lbs_set_host_sleep(struct lbs_private *priv, int host_sleep)
  319. {
  320. struct cmd_header cmd;
  321. int ret = 0;
  322. uint32_t criteria = EHS_REMOVE_WAKEUP;
  323. lbs_deb_enter(LBS_DEB_CMD);
  324. if (host_sleep) {
  325. if (priv->is_host_sleep_activated != 1) {
  326. memset(&cmd, 0, sizeof(cmd));
  327. ret = lbs_host_sleep_cfg(priv, priv->wol_criteria,
  328. (struct wol_config *)NULL);
  329. if (ret) {
  330. lbs_pr_info("Host sleep configuration failed: "
  331. "%d\n", ret);
  332. return ret;
  333. }
  334. if (priv->psstate == PS_STATE_FULL_POWER) {
  335. ret = __lbs_cmd(priv,
  336. CMD_802_11_HOST_SLEEP_ACTIVATE,
  337. &cmd,
  338. sizeof(cmd),
  339. lbs_ret_host_sleep_activate, 0);
  340. if (ret)
  341. lbs_pr_info("HOST_SLEEP_ACTIVATE "
  342. "failed: %d\n", ret);
  343. }
  344. if (!wait_event_interruptible_timeout(
  345. priv->host_sleep_q,
  346. priv->is_host_sleep_activated,
  347. (10 * HZ))) {
  348. lbs_pr_err("host_sleep_q: timer expired\n");
  349. ret = -1;
  350. }
  351. } else {
  352. lbs_pr_err("host sleep: already enabled\n");
  353. }
  354. } else {
  355. if (priv->is_host_sleep_activated)
  356. ret = lbs_host_sleep_cfg(priv, criteria,
  357. (struct wol_config *)NULL);
  358. }
  359. return ret;
  360. }
  361. /**
  362. * @brief Set an SNMP MIB value
  363. *
  364. * @param priv A pointer to struct lbs_private structure
  365. * @param oid The OID to set in the firmware
  366. * @param val Value to set the OID to
  367. *
  368. * @return 0 on success, error on failure
  369. */
  370. int lbs_set_snmp_mib(struct lbs_private *priv, u32 oid, u16 val)
  371. {
  372. struct cmd_ds_802_11_snmp_mib cmd;
  373. int ret;
  374. lbs_deb_enter(LBS_DEB_CMD);
  375. memset(&cmd, 0, sizeof (cmd));
  376. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  377. cmd.action = cpu_to_le16(CMD_ACT_SET);
  378. cmd.oid = cpu_to_le16((u16) oid);
  379. switch (oid) {
  380. case SNMP_MIB_OID_BSS_TYPE:
  381. cmd.bufsize = cpu_to_le16(sizeof(u8));
  382. cmd.value[0] = val;
  383. break;
  384. case SNMP_MIB_OID_11D_ENABLE:
  385. case SNMP_MIB_OID_FRAG_THRESHOLD:
  386. case SNMP_MIB_OID_RTS_THRESHOLD:
  387. case SNMP_MIB_OID_SHORT_RETRY_LIMIT:
  388. case SNMP_MIB_OID_LONG_RETRY_LIMIT:
  389. cmd.bufsize = cpu_to_le16(sizeof(u16));
  390. *((__le16 *)(&cmd.value)) = cpu_to_le16(val);
  391. break;
  392. default:
  393. lbs_deb_cmd("SNMP_CMD: (set) unhandled OID 0x%x\n", oid);
  394. ret = -EINVAL;
  395. goto out;
  396. }
  397. lbs_deb_cmd("SNMP_CMD: (set) oid 0x%x, oid size 0x%x, value 0x%x\n",
  398. le16_to_cpu(cmd.oid), le16_to_cpu(cmd.bufsize), val);
  399. ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
  400. out:
  401. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  402. return ret;
  403. }
  404. /**
  405. * @brief Get an SNMP MIB value
  406. *
  407. * @param priv A pointer to struct lbs_private structure
  408. * @param oid The OID to retrieve from the firmware
  409. * @param out_val Location for the returned value
  410. *
  411. * @return 0 on success, error on failure
  412. */
  413. int lbs_get_snmp_mib(struct lbs_private *priv, u32 oid, u16 *out_val)
  414. {
  415. struct cmd_ds_802_11_snmp_mib cmd;
  416. int ret;
  417. lbs_deb_enter(LBS_DEB_CMD);
  418. memset(&cmd, 0, sizeof (cmd));
  419. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  420. cmd.action = cpu_to_le16(CMD_ACT_GET);
  421. cmd.oid = cpu_to_le16(oid);
  422. ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
  423. if (ret)
  424. goto out;
  425. switch (le16_to_cpu(cmd.bufsize)) {
  426. case sizeof(u8):
  427. *out_val = cmd.value[0];
  428. break;
  429. case sizeof(u16):
  430. *out_val = le16_to_cpu(*((__le16 *)(&cmd.value)));
  431. break;
  432. default:
  433. lbs_deb_cmd("SNMP_CMD: (get) unhandled OID 0x%x size %d\n",
  434. oid, le16_to_cpu(cmd.bufsize));
  435. break;
  436. }
  437. out:
  438. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  439. return ret;
  440. }
  441. /**
  442. * @brief Get the min, max, and current TX power
  443. *
  444. * @param priv A pointer to struct lbs_private structure
  445. * @param curlevel Current power level in dBm
  446. * @param minlevel Minimum supported power level in dBm (optional)
  447. * @param maxlevel Maximum supported power level in dBm (optional)
  448. *
  449. * @return 0 on success, error on failure
  450. */
  451. int lbs_get_tx_power(struct lbs_private *priv, s16 *curlevel, s16 *minlevel,
  452. s16 *maxlevel)
  453. {
  454. struct cmd_ds_802_11_rf_tx_power cmd;
  455. int ret;
  456. lbs_deb_enter(LBS_DEB_CMD);
  457. memset(&cmd, 0, sizeof(cmd));
  458. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  459. cmd.action = cpu_to_le16(CMD_ACT_GET);
  460. ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
  461. if (ret == 0) {
  462. *curlevel = le16_to_cpu(cmd.curlevel);
  463. if (minlevel)
  464. *minlevel = cmd.minlevel;
  465. if (maxlevel)
  466. *maxlevel = cmd.maxlevel;
  467. }
  468. lbs_deb_leave(LBS_DEB_CMD);
  469. return ret;
  470. }
  471. /**
  472. * @brief Set the TX power
  473. *
  474. * @param priv A pointer to struct lbs_private structure
  475. * @param dbm The desired power level in dBm
  476. *
  477. * @return 0 on success, error on failure
  478. */
  479. int lbs_set_tx_power(struct lbs_private *priv, s16 dbm)
  480. {
  481. struct cmd_ds_802_11_rf_tx_power cmd;
  482. int ret;
  483. lbs_deb_enter(LBS_DEB_CMD);
  484. memset(&cmd, 0, sizeof(cmd));
  485. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  486. cmd.action = cpu_to_le16(CMD_ACT_SET);
  487. cmd.curlevel = cpu_to_le16(dbm);
  488. lbs_deb_cmd("SET_RF_TX_POWER: %d dBm\n", dbm);
  489. ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
  490. lbs_deb_leave(LBS_DEB_CMD);
  491. return ret;
  492. }
  493. static int lbs_cmd_802_11_monitor_mode(struct cmd_ds_command *cmd,
  494. u16 cmd_action, void *pdata_buf)
  495. {
  496. struct cmd_ds_802_11_monitor_mode *monitor = &cmd->params.monitor;
  497. cmd->command = cpu_to_le16(CMD_802_11_MONITOR_MODE);
  498. cmd->size =
  499. cpu_to_le16(sizeof(struct cmd_ds_802_11_monitor_mode) +
  500. sizeof(struct cmd_header));
  501. monitor->action = cpu_to_le16(cmd_action);
  502. if (cmd_action == CMD_ACT_SET) {
  503. monitor->mode =
  504. cpu_to_le16((u16) (*(u32 *) pdata_buf));
  505. }
  506. return 0;
  507. }
  508. /**
  509. * @brief Get the radio channel
  510. *
  511. * @param priv A pointer to struct lbs_private structure
  512. *
  513. * @return The channel on success, error on failure
  514. */
  515. static int lbs_get_channel(struct lbs_private *priv)
  516. {
  517. struct cmd_ds_802_11_rf_channel cmd;
  518. int ret = 0;
  519. lbs_deb_enter(LBS_DEB_CMD);
  520. memset(&cmd, 0, sizeof(cmd));
  521. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  522. cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_GET);
  523. ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
  524. if (ret)
  525. goto out;
  526. ret = le16_to_cpu(cmd.channel);
  527. lbs_deb_cmd("current radio channel is %d\n", ret);
  528. out:
  529. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  530. return ret;
  531. }
  532. int lbs_update_channel(struct lbs_private *priv)
  533. {
  534. int ret;
  535. /* the channel in f/w could be out of sync; get the current channel */
  536. lbs_deb_enter(LBS_DEB_ASSOC);
  537. ret = lbs_get_channel(priv);
  538. if (ret > 0) {
  539. priv->channel = ret;
  540. ret = 0;
  541. }
  542. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  543. return ret;
  544. }
  545. /**
  546. * @brief Set the radio channel
  547. *
  548. * @param priv A pointer to struct lbs_private structure
  549. * @param channel The desired channel, or 0 to clear a locked channel
  550. *
  551. * @return 0 on success, error on failure
  552. */
  553. int lbs_set_channel(struct lbs_private *priv, u8 channel)
  554. {
  555. struct cmd_ds_802_11_rf_channel cmd;
  556. #ifdef DEBUG
  557. u8 old_channel = priv->channel;
  558. #endif
  559. int ret = 0;
  560. lbs_deb_enter(LBS_DEB_CMD);
  561. memset(&cmd, 0, sizeof(cmd));
  562. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  563. cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
  564. cmd.channel = cpu_to_le16(channel);
  565. ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
  566. if (ret)
  567. goto out;
  568. priv->channel = (uint8_t) le16_to_cpu(cmd.channel);
  569. lbs_deb_cmd("channel switch from %d to %d\n", old_channel,
  570. priv->channel);
  571. out:
  572. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  573. return ret;
  574. }
  575. static int lbs_cmd_reg_access(struct cmd_ds_command *cmdptr,
  576. u8 cmd_action, void *pdata_buf)
  577. {
  578. struct lbs_offset_value *offval;
  579. lbs_deb_enter(LBS_DEB_CMD);
  580. offval = (struct lbs_offset_value *)pdata_buf;
  581. switch (le16_to_cpu(cmdptr->command)) {
  582. case CMD_MAC_REG_ACCESS:
  583. {
  584. struct cmd_ds_mac_reg_access *macreg;
  585. cmdptr->size =
  586. cpu_to_le16(sizeof (struct cmd_ds_mac_reg_access)
  587. + sizeof(struct cmd_header));
  588. macreg =
  589. (struct cmd_ds_mac_reg_access *)&cmdptr->params.
  590. macreg;
  591. macreg->action = cpu_to_le16(cmd_action);
  592. macreg->offset = cpu_to_le16((u16) offval->offset);
  593. macreg->value = cpu_to_le32(offval->value);
  594. break;
  595. }
  596. case CMD_BBP_REG_ACCESS:
  597. {
  598. struct cmd_ds_bbp_reg_access *bbpreg;
  599. cmdptr->size =
  600. cpu_to_le16(sizeof
  601. (struct cmd_ds_bbp_reg_access)
  602. + sizeof(struct cmd_header));
  603. bbpreg =
  604. (struct cmd_ds_bbp_reg_access *)&cmdptr->params.
  605. bbpreg;
  606. bbpreg->action = cpu_to_le16(cmd_action);
  607. bbpreg->offset = cpu_to_le16((u16) offval->offset);
  608. bbpreg->value = (u8) offval->value;
  609. break;
  610. }
  611. case CMD_RF_REG_ACCESS:
  612. {
  613. struct cmd_ds_rf_reg_access *rfreg;
  614. cmdptr->size =
  615. cpu_to_le16(sizeof
  616. (struct cmd_ds_rf_reg_access) +
  617. sizeof(struct cmd_header));
  618. rfreg =
  619. (struct cmd_ds_rf_reg_access *)&cmdptr->params.
  620. rfreg;
  621. rfreg->action = cpu_to_le16(cmd_action);
  622. rfreg->offset = cpu_to_le16((u16) offval->offset);
  623. rfreg->value = (u8) offval->value;
  624. break;
  625. }
  626. default:
  627. break;
  628. }
  629. lbs_deb_leave(LBS_DEB_CMD);
  630. return 0;
  631. }
  632. static void lbs_queue_cmd(struct lbs_private *priv,
  633. struct cmd_ctrl_node *cmdnode)
  634. {
  635. unsigned long flags;
  636. int addtail = 1;
  637. lbs_deb_enter(LBS_DEB_HOST);
  638. if (!cmdnode) {
  639. lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
  640. goto done;
  641. }
  642. if (!cmdnode->cmdbuf->size) {
  643. lbs_deb_host("DNLD_CMD: cmd size is zero\n");
  644. goto done;
  645. }
  646. cmdnode->result = 0;
  647. /* Exit_PS command needs to be queued in the header always. */
  648. if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_PS_MODE) {
  649. struct cmd_ds_802_11_ps_mode *psm = (void *) &cmdnode->cmdbuf[1];
  650. if (psm->action == cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
  651. if (priv->psstate != PS_STATE_FULL_POWER)
  652. addtail = 0;
  653. }
  654. }
  655. if (le16_to_cpu(cmdnode->cmdbuf->command) ==
  656. CMD_802_11_WAKEUP_CONFIRM)
  657. addtail = 0;
  658. spin_lock_irqsave(&priv->driver_lock, flags);
  659. if (addtail)
  660. list_add_tail(&cmdnode->list, &priv->cmdpendingq);
  661. else
  662. list_add(&cmdnode->list, &priv->cmdpendingq);
  663. spin_unlock_irqrestore(&priv->driver_lock, flags);
  664. lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
  665. le16_to_cpu(cmdnode->cmdbuf->command));
  666. done:
  667. lbs_deb_leave(LBS_DEB_HOST);
  668. }
  669. static void lbs_submit_command(struct lbs_private *priv,
  670. struct cmd_ctrl_node *cmdnode)
  671. {
  672. unsigned long flags;
  673. struct cmd_header *cmd;
  674. uint16_t cmdsize;
  675. uint16_t command;
  676. int timeo = 3 * HZ;
  677. int ret;
  678. lbs_deb_enter(LBS_DEB_HOST);
  679. cmd = cmdnode->cmdbuf;
  680. spin_lock_irqsave(&priv->driver_lock, flags);
  681. priv->cur_cmd = cmdnode;
  682. priv->cur_cmd_retcode = 0;
  683. spin_unlock_irqrestore(&priv->driver_lock, flags);
  684. cmdsize = le16_to_cpu(cmd->size);
  685. command = le16_to_cpu(cmd->command);
  686. /* These commands take longer */
  687. if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE)
  688. timeo = 5 * HZ;
  689. lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
  690. command, le16_to_cpu(cmd->seqnum), cmdsize);
  691. lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
  692. ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
  693. if (ret) {
  694. lbs_pr_info("DNLD_CMD: hw_host_to_card failed: %d\n", ret);
  695. /* Let the timer kick in and retry, and potentially reset
  696. the whole thing if the condition persists */
  697. timeo = HZ/4;
  698. }
  699. if (command == CMD_802_11_DEEP_SLEEP) {
  700. if (priv->is_auto_deep_sleep_enabled) {
  701. priv->wakeup_dev_required = 1;
  702. priv->dnld_sent = 0;
  703. }
  704. priv->is_deep_sleep = 1;
  705. lbs_complete_command(priv, cmdnode, 0);
  706. } else {
  707. /* Setup the timer after transmit command */
  708. mod_timer(&priv->command_timer, jiffies + timeo);
  709. }
  710. lbs_deb_leave(LBS_DEB_HOST);
  711. }
  712. /**
  713. * This function inserts command node to cmdfreeq
  714. * after cleans it. Requires priv->driver_lock held.
  715. */
  716. static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
  717. struct cmd_ctrl_node *cmdnode)
  718. {
  719. lbs_deb_enter(LBS_DEB_HOST);
  720. if (!cmdnode)
  721. goto out;
  722. cmdnode->callback = NULL;
  723. cmdnode->callback_arg = 0;
  724. memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
  725. list_add_tail(&cmdnode->list, &priv->cmdfreeq);
  726. out:
  727. lbs_deb_leave(LBS_DEB_HOST);
  728. }
  729. static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
  730. struct cmd_ctrl_node *ptempcmd)
  731. {
  732. unsigned long flags;
  733. spin_lock_irqsave(&priv->driver_lock, flags);
  734. __lbs_cleanup_and_insert_cmd(priv, ptempcmd);
  735. spin_unlock_irqrestore(&priv->driver_lock, flags);
  736. }
  737. void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
  738. int result)
  739. {
  740. if (cmd == priv->cur_cmd)
  741. priv->cur_cmd_retcode = result;
  742. cmd->result = result;
  743. cmd->cmdwaitqwoken = 1;
  744. wake_up_interruptible(&cmd->cmdwait_q);
  745. if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
  746. __lbs_cleanup_and_insert_cmd(priv, cmd);
  747. priv->cur_cmd = NULL;
  748. }
  749. int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
  750. {
  751. struct cmd_ds_802_11_radio_control cmd;
  752. int ret = -EINVAL;
  753. lbs_deb_enter(LBS_DEB_CMD);
  754. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  755. cmd.action = cpu_to_le16(CMD_ACT_SET);
  756. /* Only v8 and below support setting the preamble */
  757. if (priv->fwrelease < 0x09000000) {
  758. switch (preamble) {
  759. case RADIO_PREAMBLE_SHORT:
  760. case RADIO_PREAMBLE_AUTO:
  761. case RADIO_PREAMBLE_LONG:
  762. cmd.control = cpu_to_le16(preamble);
  763. break;
  764. default:
  765. goto out;
  766. }
  767. }
  768. if (radio_on)
  769. cmd.control |= cpu_to_le16(0x1);
  770. else {
  771. cmd.control &= cpu_to_le16(~0x1);
  772. priv->txpower_cur = 0;
  773. }
  774. lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
  775. radio_on ? "ON" : "OFF", preamble);
  776. priv->radio_on = radio_on;
  777. ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
  778. out:
  779. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  780. return ret;
  781. }
  782. void lbs_set_mac_control(struct lbs_private *priv)
  783. {
  784. struct cmd_ds_mac_control cmd;
  785. lbs_deb_enter(LBS_DEB_CMD);
  786. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  787. cmd.action = cpu_to_le16(priv->mac_control);
  788. cmd.reserved = 0;
  789. lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
  790. lbs_deb_leave(LBS_DEB_CMD);
  791. }
  792. /**
  793. * @brief This function implements command CMD_802_11D_DOMAIN_INFO
  794. * @param priv pointer to struct lbs_private
  795. * @param cmd pointer to cmd buffer
  796. * @param cmdno cmd ID
  797. * @param cmdOption cmd action
  798. * @return 0
  799. */
  800. int lbs_cmd_802_11d_domain_info(struct lbs_private *priv,
  801. struct cmd_ds_command *cmd,
  802. u16 cmdoption)
  803. {
  804. struct cmd_ds_802_11d_domain_info *pdomaininfo =
  805. &cmd->params.domaininfo;
  806. struct mrvl_ie_domain_param_set *domain = &pdomaininfo->domain;
  807. u8 nr_triplet = priv->domain_reg.no_triplet;
  808. lbs_deb_enter(LBS_DEB_11D);
  809. lbs_deb_11d("nr_triplet=%x\n", nr_triplet);
  810. pdomaininfo->action = cpu_to_le16(cmdoption);
  811. if (cmdoption == CMD_ACT_GET) {
  812. cmd->size = cpu_to_le16(sizeof(pdomaininfo->action) +
  813. sizeof(struct cmd_header));
  814. lbs_deb_hex(LBS_DEB_11D, "802_11D_DOMAIN_INFO", (u8 *) cmd,
  815. le16_to_cpu(cmd->size));
  816. goto done;
  817. }
  818. domain->header.type = cpu_to_le16(TLV_TYPE_DOMAIN);
  819. memcpy(domain->countrycode, priv->domain_reg.country_code,
  820. sizeof(domain->countrycode));
  821. domain->header.len = cpu_to_le16(nr_triplet
  822. * sizeof(struct ieee80211_country_ie_triplet)
  823. + sizeof(domain->countrycode));
  824. if (nr_triplet) {
  825. memcpy(domain->triplet, priv->domain_reg.triplet,
  826. nr_triplet *
  827. sizeof(struct ieee80211_country_ie_triplet));
  828. cmd->size = cpu_to_le16(sizeof(pdomaininfo->action) +
  829. le16_to_cpu(domain->header.len) +
  830. sizeof(struct mrvl_ie_header) +
  831. sizeof(struct cmd_header));
  832. } else {
  833. cmd->size = cpu_to_le16(sizeof(pdomaininfo->action) +
  834. sizeof(struct cmd_header));
  835. }
  836. lbs_deb_hex(LBS_DEB_11D, "802_11D_DOMAIN_INFO", (u8 *) cmd,
  837. le16_to_cpu(cmd->size));
  838. done:
  839. lbs_deb_enter(LBS_DEB_11D);
  840. return 0;
  841. }
  842. /**
  843. * @brief This function prepare the command before send to firmware.
  844. *
  845. * @param priv A pointer to struct lbs_private structure
  846. * @param cmd_no command number
  847. * @param cmd_action command action: GET or SET
  848. * @param wait_option wait option: wait response or not
  849. * @param cmd_oid cmd oid: treated as sub command
  850. * @param pdata_buf A pointer to informaion buffer
  851. * @return 0 or -1
  852. */
  853. int lbs_prepare_and_send_command(struct lbs_private *priv,
  854. u16 cmd_no,
  855. u16 cmd_action,
  856. u16 wait_option, u32 cmd_oid, void *pdata_buf)
  857. {
  858. int ret = 0;
  859. struct cmd_ctrl_node *cmdnode;
  860. struct cmd_ds_command *cmdptr;
  861. unsigned long flags;
  862. lbs_deb_enter(LBS_DEB_HOST);
  863. if (!priv) {
  864. lbs_deb_host("PREP_CMD: priv is NULL\n");
  865. ret = -1;
  866. goto done;
  867. }
  868. if (priv->surpriseremoved) {
  869. lbs_deb_host("PREP_CMD: card removed\n");
  870. ret = -1;
  871. goto done;
  872. }
  873. if (!lbs_is_cmd_allowed(priv)) {
  874. ret = -EBUSY;
  875. goto done;
  876. }
  877. cmdnode = lbs_get_cmd_ctrl_node(priv);
  878. if (cmdnode == NULL) {
  879. lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
  880. /* Wake up main thread to execute next command */
  881. wake_up_interruptible(&priv->waitq);
  882. ret = -1;
  883. goto done;
  884. }
  885. cmdnode->callback = NULL;
  886. cmdnode->callback_arg = (unsigned long)pdata_buf;
  887. cmdptr = (struct cmd_ds_command *)cmdnode->cmdbuf;
  888. lbs_deb_host("PREP_CMD: command 0x%04x\n", cmd_no);
  889. /* Set sequence number, command and INT option */
  890. priv->seqnum++;
  891. cmdptr->seqnum = cpu_to_le16(priv->seqnum);
  892. cmdptr->command = cpu_to_le16(cmd_no);
  893. cmdptr->result = 0;
  894. switch (cmd_no) {
  895. case CMD_802_11_PS_MODE:
  896. ret = lbs_cmd_802_11_ps_mode(cmdptr, cmd_action);
  897. break;
  898. case CMD_MAC_REG_ACCESS:
  899. case CMD_BBP_REG_ACCESS:
  900. case CMD_RF_REG_ACCESS:
  901. ret = lbs_cmd_reg_access(cmdptr, cmd_action, pdata_buf);
  902. break;
  903. case CMD_802_11_MONITOR_MODE:
  904. ret = lbs_cmd_802_11_monitor_mode(cmdptr,
  905. cmd_action, pdata_buf);
  906. break;
  907. case CMD_802_11_RSSI:
  908. ret = lbs_cmd_802_11_rssi(priv, cmdptr);
  909. break;
  910. case CMD_802_11_SET_AFC:
  911. case CMD_802_11_GET_AFC:
  912. cmdptr->command = cpu_to_le16(cmd_no);
  913. cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_afc) +
  914. sizeof(struct cmd_header));
  915. memmove(&cmdptr->params.afc,
  916. pdata_buf, sizeof(struct cmd_ds_802_11_afc));
  917. ret = 0;
  918. goto done;
  919. case CMD_802_11D_DOMAIN_INFO:
  920. cmdptr->command = cpu_to_le16(cmd_no);
  921. ret = lbs_cmd_802_11d_domain_info(priv, cmdptr, cmd_action);
  922. break;
  923. case CMD_802_11_TPC_CFG:
  924. cmdptr->command = cpu_to_le16(CMD_802_11_TPC_CFG);
  925. cmdptr->size =
  926. cpu_to_le16(sizeof(struct cmd_ds_802_11_tpc_cfg) +
  927. sizeof(struct cmd_header));
  928. memmove(&cmdptr->params.tpccfg,
  929. pdata_buf, sizeof(struct cmd_ds_802_11_tpc_cfg));
  930. ret = 0;
  931. break;
  932. #ifdef CONFIG_LIBERTAS_MESH
  933. case CMD_BT_ACCESS:
  934. ret = lbs_cmd_bt_access(cmdptr, cmd_action, pdata_buf);
  935. break;
  936. case CMD_FWT_ACCESS:
  937. ret = lbs_cmd_fwt_access(cmdptr, cmd_action, pdata_buf);
  938. break;
  939. #endif
  940. case CMD_802_11_BEACON_CTRL:
  941. ret = lbs_cmd_bcn_ctrl(priv, cmdptr, cmd_action);
  942. break;
  943. case CMD_802_11_DEEP_SLEEP:
  944. cmdptr->command = cpu_to_le16(CMD_802_11_DEEP_SLEEP);
  945. cmdptr->size = cpu_to_le16(sizeof(struct cmd_header));
  946. break;
  947. default:
  948. lbs_pr_err("PREP_CMD: unknown command 0x%04x\n", cmd_no);
  949. ret = -1;
  950. break;
  951. }
  952. /* return error, since the command preparation failed */
  953. if (ret != 0) {
  954. lbs_deb_host("PREP_CMD: command preparation failed\n");
  955. lbs_cleanup_and_insert_cmd(priv, cmdnode);
  956. ret = -1;
  957. goto done;
  958. }
  959. cmdnode->cmdwaitqwoken = 0;
  960. lbs_queue_cmd(priv, cmdnode);
  961. wake_up_interruptible(&priv->waitq);
  962. if (wait_option & CMD_OPTION_WAITFORRSP) {
  963. lbs_deb_host("PREP_CMD: wait for response\n");
  964. might_sleep();
  965. wait_event_interruptible(cmdnode->cmdwait_q,
  966. cmdnode->cmdwaitqwoken);
  967. }
  968. spin_lock_irqsave(&priv->driver_lock, flags);
  969. if (priv->cur_cmd_retcode) {
  970. lbs_deb_host("PREP_CMD: command failed with return code %d\n",
  971. priv->cur_cmd_retcode);
  972. priv->cur_cmd_retcode = 0;
  973. ret = -1;
  974. }
  975. spin_unlock_irqrestore(&priv->driver_lock, flags);
  976. done:
  977. lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
  978. return ret;
  979. }
  980. /**
  981. * @brief This function allocates the command buffer and link
  982. * it to command free queue.
  983. *
  984. * @param priv A pointer to struct lbs_private structure
  985. * @return 0 or -1
  986. */
  987. int lbs_allocate_cmd_buffer(struct lbs_private *priv)
  988. {
  989. int ret = 0;
  990. u32 bufsize;
  991. u32 i;
  992. struct cmd_ctrl_node *cmdarray;
  993. lbs_deb_enter(LBS_DEB_HOST);
  994. /* Allocate and initialize the command array */
  995. bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
  996. if (!(cmdarray = kzalloc(bufsize, GFP_KERNEL))) {
  997. lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
  998. ret = -1;
  999. goto done;
  1000. }
  1001. priv->cmd_array = cmdarray;
  1002. /* Allocate and initialize each command buffer in the command array */
  1003. for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
  1004. cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
  1005. if (!cmdarray[i].cmdbuf) {
  1006. lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
  1007. ret = -1;
  1008. goto done;
  1009. }
  1010. }
  1011. for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
  1012. init_waitqueue_head(&cmdarray[i].cmdwait_q);
  1013. lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
  1014. }
  1015. ret = 0;
  1016. done:
  1017. lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
  1018. return ret;
  1019. }
  1020. /**
  1021. * @brief This function frees the command buffer.
  1022. *
  1023. * @param priv A pointer to struct lbs_private structure
  1024. * @return 0 or -1
  1025. */
  1026. int lbs_free_cmd_buffer(struct lbs_private *priv)
  1027. {
  1028. struct cmd_ctrl_node *cmdarray;
  1029. unsigned int i;
  1030. lbs_deb_enter(LBS_DEB_HOST);
  1031. /* need to check if cmd array is allocated or not */
  1032. if (priv->cmd_array == NULL) {
  1033. lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
  1034. goto done;
  1035. }
  1036. cmdarray = priv->cmd_array;
  1037. /* Release shared memory buffers */
  1038. for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
  1039. if (cmdarray[i].cmdbuf) {
  1040. kfree(cmdarray[i].cmdbuf);
  1041. cmdarray[i].cmdbuf = NULL;
  1042. }
  1043. }
  1044. /* Release cmd_ctrl_node */
  1045. if (priv->cmd_array) {
  1046. kfree(priv->cmd_array);
  1047. priv->cmd_array = NULL;
  1048. }
  1049. done:
  1050. lbs_deb_leave(LBS_DEB_HOST);
  1051. return 0;
  1052. }
  1053. /**
  1054. * @brief This function gets a free command node if available in
  1055. * command free queue.
  1056. *
  1057. * @param priv A pointer to struct lbs_private structure
  1058. * @return cmd_ctrl_node A pointer to cmd_ctrl_node structure or NULL
  1059. */
  1060. static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv)
  1061. {
  1062. struct cmd_ctrl_node *tempnode;
  1063. unsigned long flags;
  1064. lbs_deb_enter(LBS_DEB_HOST);
  1065. if (!priv)
  1066. return NULL;
  1067. spin_lock_irqsave(&priv->driver_lock, flags);
  1068. if (!list_empty(&priv->cmdfreeq)) {
  1069. tempnode = list_first_entry(&priv->cmdfreeq,
  1070. struct cmd_ctrl_node, list);
  1071. list_del(&tempnode->list);
  1072. } else {
  1073. lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
  1074. tempnode = NULL;
  1075. }
  1076. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1077. lbs_deb_leave(LBS_DEB_HOST);
  1078. return tempnode;
  1079. }
  1080. /**
  1081. * @brief This function executes next command in command
  1082. * pending queue. It will put firmware back to PS mode
  1083. * if applicable.
  1084. *
  1085. * @param priv A pointer to struct lbs_private structure
  1086. * @return 0 or -1
  1087. */
  1088. int lbs_execute_next_command(struct lbs_private *priv)
  1089. {
  1090. struct cmd_ctrl_node *cmdnode = NULL;
  1091. struct cmd_header *cmd;
  1092. unsigned long flags;
  1093. int ret = 0;
  1094. /* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
  1095. * only caller to us is lbs_thread() and we get even when a
  1096. * data packet is received */
  1097. lbs_deb_enter(LBS_DEB_THREAD);
  1098. spin_lock_irqsave(&priv->driver_lock, flags);
  1099. if (priv->cur_cmd) {
  1100. lbs_pr_alert( "EXEC_NEXT_CMD: already processing command!\n");
  1101. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1102. ret = -1;
  1103. goto done;
  1104. }
  1105. if (!list_empty(&priv->cmdpendingq)) {
  1106. cmdnode = list_first_entry(&priv->cmdpendingq,
  1107. struct cmd_ctrl_node, list);
  1108. }
  1109. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1110. if (cmdnode) {
  1111. cmd = cmdnode->cmdbuf;
  1112. if (is_command_allowed_in_ps(le16_to_cpu(cmd->command))) {
  1113. if ((priv->psstate == PS_STATE_SLEEP) ||
  1114. (priv->psstate == PS_STATE_PRE_SLEEP)) {
  1115. lbs_deb_host(
  1116. "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
  1117. le16_to_cpu(cmd->command),
  1118. priv->psstate);
  1119. ret = -1;
  1120. goto done;
  1121. }
  1122. lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
  1123. "0x%04x in psstate %d\n",
  1124. le16_to_cpu(cmd->command), priv->psstate);
  1125. } else if (priv->psstate != PS_STATE_FULL_POWER) {
  1126. /*
  1127. * 1. Non-PS command:
  1128. * Queue it. set needtowakeup to TRUE if current state
  1129. * is SLEEP, otherwise call lbs_ps_wakeup to send Exit_PS.
  1130. * 2. PS command but not Exit_PS:
  1131. * Ignore it.
  1132. * 3. PS command Exit_PS:
  1133. * Set needtowakeup to TRUE if current state is SLEEP,
  1134. * otherwise send this command down to firmware
  1135. * immediately.
  1136. */
  1137. if (cmd->command != cpu_to_le16(CMD_802_11_PS_MODE)) {
  1138. /* Prepare to send Exit PS,
  1139. * this non PS command will be sent later */
  1140. if ((priv->psstate == PS_STATE_SLEEP)
  1141. || (priv->psstate == PS_STATE_PRE_SLEEP)
  1142. ) {
  1143. /* w/ new scheme, it will not reach here.
  1144. since it is blocked in main_thread. */
  1145. priv->needtowakeup = 1;
  1146. } else
  1147. lbs_ps_wakeup(priv, 0);
  1148. ret = 0;
  1149. goto done;
  1150. } else {
  1151. /*
  1152. * PS command. Ignore it if it is not Exit_PS.
  1153. * otherwise send it down immediately.
  1154. */
  1155. struct cmd_ds_802_11_ps_mode *psm = (void *)&cmd[1];
  1156. lbs_deb_host(
  1157. "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
  1158. psm->action);
  1159. if (psm->action !=
  1160. cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
  1161. lbs_deb_host(
  1162. "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
  1163. list_del(&cmdnode->list);
  1164. spin_lock_irqsave(&priv->driver_lock, flags);
  1165. lbs_complete_command(priv, cmdnode, 0);
  1166. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1167. ret = 0;
  1168. goto done;
  1169. }
  1170. if ((priv->psstate == PS_STATE_SLEEP) ||
  1171. (priv->psstate == PS_STATE_PRE_SLEEP)) {
  1172. lbs_deb_host(
  1173. "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
  1174. list_del(&cmdnode->list);
  1175. spin_lock_irqsave(&priv->driver_lock, flags);
  1176. lbs_complete_command(priv, cmdnode, 0);
  1177. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1178. priv->needtowakeup = 1;
  1179. ret = 0;
  1180. goto done;
  1181. }
  1182. lbs_deb_host(
  1183. "EXEC_NEXT_CMD: sending EXIT_PS\n");
  1184. }
  1185. }
  1186. list_del(&cmdnode->list);
  1187. lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
  1188. le16_to_cpu(cmd->command));
  1189. lbs_submit_command(priv, cmdnode);
  1190. } else {
  1191. /*
  1192. * check if in power save mode, if yes, put the device back
  1193. * to PS mode
  1194. */
  1195. #ifdef TODO
  1196. /*
  1197. * This was the old code for libertas+wext. Someone that
  1198. * understands this beast should re-code it in a sane way.
  1199. *
  1200. * I actually don't understand why this is related to WPA
  1201. * and to connection status, shouldn't powering should be
  1202. * independ of such things?
  1203. */
  1204. if ((priv->psmode != LBS802_11POWERMODECAM) &&
  1205. (priv->psstate == PS_STATE_FULL_POWER) &&
  1206. ((priv->connect_status == LBS_CONNECTED) ||
  1207. lbs_mesh_connected(priv))) {
  1208. if (priv->secinfo.WPAenabled ||
  1209. priv->secinfo.WPA2enabled) {
  1210. /* check for valid WPA group keys */
  1211. if (priv->wpa_mcast_key.len ||
  1212. priv->wpa_unicast_key.len) {
  1213. lbs_deb_host(
  1214. "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
  1215. " go back to PS_SLEEP");
  1216. lbs_ps_sleep(priv, 0);
  1217. }
  1218. } else {
  1219. lbs_deb_host(
  1220. "EXEC_NEXT_CMD: cmdpendingq empty, "
  1221. "go back to PS_SLEEP");
  1222. lbs_ps_sleep(priv, 0);
  1223. }
  1224. }
  1225. #endif
  1226. }
  1227. ret = 0;
  1228. done:
  1229. lbs_deb_leave(LBS_DEB_THREAD);
  1230. return ret;
  1231. }
  1232. static void lbs_send_confirmsleep(struct lbs_private *priv)
  1233. {
  1234. unsigned long flags;
  1235. int ret;
  1236. lbs_deb_enter(LBS_DEB_HOST);
  1237. lbs_deb_hex(LBS_DEB_HOST, "sleep confirm", (u8 *) &confirm_sleep,
  1238. sizeof(confirm_sleep));
  1239. ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &confirm_sleep,
  1240. sizeof(confirm_sleep));
  1241. if (ret) {
  1242. lbs_pr_alert("confirm_sleep failed\n");
  1243. goto out;
  1244. }
  1245. spin_lock_irqsave(&priv->driver_lock, flags);
  1246. /* We don't get a response on the sleep-confirmation */
  1247. priv->dnld_sent = DNLD_RES_RECEIVED;
  1248. if (priv->is_host_sleep_configured) {
  1249. priv->is_host_sleep_activated = 1;
  1250. wake_up_interruptible(&priv->host_sleep_q);
  1251. }
  1252. /* If nothing to do, go back to sleep (?) */
  1253. if (!kfifo_len(&priv->event_fifo) && !priv->resp_len[priv->resp_idx])
  1254. priv->psstate = PS_STATE_SLEEP;
  1255. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1256. out:
  1257. lbs_deb_leave(LBS_DEB_HOST);
  1258. }
  1259. void lbs_ps_sleep(struct lbs_private *priv, int wait_option)
  1260. {
  1261. lbs_deb_enter(LBS_DEB_HOST);
  1262. /*
  1263. * PS is currently supported only in Infrastructure mode
  1264. * Remove this check if it is to be supported in IBSS mode also
  1265. */
  1266. lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
  1267. CMD_SUBCMD_ENTER_PS, wait_option, 0, NULL);
  1268. lbs_deb_leave(LBS_DEB_HOST);
  1269. }
  1270. /**
  1271. * @brief This function sends Exit_PS command to firmware.
  1272. *
  1273. * @param priv A pointer to struct lbs_private structure
  1274. * @param wait_option wait response or not
  1275. * @return n/a
  1276. */
  1277. void lbs_ps_wakeup(struct lbs_private *priv, int wait_option)
  1278. {
  1279. __le32 Localpsmode;
  1280. lbs_deb_enter(LBS_DEB_HOST);
  1281. Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
  1282. lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
  1283. CMD_SUBCMD_EXIT_PS,
  1284. wait_option, 0, &Localpsmode);
  1285. lbs_deb_leave(LBS_DEB_HOST);
  1286. }
  1287. /**
  1288. * @brief This function checks condition and prepares to
  1289. * send sleep confirm command to firmware if ok.
  1290. *
  1291. * @param priv A pointer to struct lbs_private structure
  1292. * @param psmode Power Saving mode
  1293. * @return n/a
  1294. */
  1295. void lbs_ps_confirm_sleep(struct lbs_private *priv)
  1296. {
  1297. unsigned long flags =0;
  1298. int allowed = 1;
  1299. lbs_deb_enter(LBS_DEB_HOST);
  1300. spin_lock_irqsave(&priv->driver_lock, flags);
  1301. if (priv->dnld_sent) {
  1302. allowed = 0;
  1303. lbs_deb_host("dnld_sent was set\n");
  1304. }
  1305. /* In-progress command? */
  1306. if (priv->cur_cmd) {
  1307. allowed = 0;
  1308. lbs_deb_host("cur_cmd was set\n");
  1309. }
  1310. /* Pending events or command responses? */
  1311. if (kfifo_len(&priv->event_fifo) || priv->resp_len[priv->resp_idx]) {
  1312. allowed = 0;
  1313. lbs_deb_host("pending events or command responses\n");
  1314. }
  1315. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1316. if (allowed) {
  1317. lbs_deb_host("sending lbs_ps_confirm_sleep\n");
  1318. lbs_send_confirmsleep(priv);
  1319. } else {
  1320. lbs_deb_host("sleep confirm has been delayed\n");
  1321. }
  1322. lbs_deb_leave(LBS_DEB_HOST);
  1323. }
  1324. /**
  1325. * @brief Configures the transmission power control functionality.
  1326. *
  1327. * @param priv A pointer to struct lbs_private structure
  1328. * @param enable Transmission power control enable
  1329. * @param p0 Power level when link quality is good (dBm).
  1330. * @param p1 Power level when link quality is fair (dBm).
  1331. * @param p2 Power level when link quality is poor (dBm).
  1332. * @param usesnr Use Signal to Noise Ratio in TPC
  1333. *
  1334. * @return 0 on success
  1335. */
  1336. int lbs_set_tpc_cfg(struct lbs_private *priv, int enable, int8_t p0, int8_t p1,
  1337. int8_t p2, int usesnr)
  1338. {
  1339. struct cmd_ds_802_11_tpc_cfg cmd;
  1340. int ret;
  1341. memset(&cmd, 0, sizeof(cmd));
  1342. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1343. cmd.action = cpu_to_le16(CMD_ACT_SET);
  1344. cmd.enable = !!enable;
  1345. cmd.usesnr = !!usesnr;
  1346. cmd.P0 = p0;
  1347. cmd.P1 = p1;
  1348. cmd.P2 = p2;
  1349. ret = lbs_cmd_with_response(priv, CMD_802_11_TPC_CFG, &cmd);
  1350. return ret;
  1351. }
  1352. /**
  1353. * @brief Configures the power adaptation settings.
  1354. *
  1355. * @param priv A pointer to struct lbs_private structure
  1356. * @param enable Power adaptation enable
  1357. * @param p0 Power level for 1, 2, 5.5 and 11 Mbps (dBm).
  1358. * @param p1 Power level for 6, 9, 12, 18, 22, 24 and 36 Mbps (dBm).
  1359. * @param p2 Power level for 48 and 54 Mbps (dBm).
  1360. *
  1361. * @return 0 on Success
  1362. */
  1363. int lbs_set_power_adapt_cfg(struct lbs_private *priv, int enable, int8_t p0,
  1364. int8_t p1, int8_t p2)
  1365. {
  1366. struct cmd_ds_802_11_pa_cfg cmd;
  1367. int ret;
  1368. memset(&cmd, 0, sizeof(cmd));
  1369. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1370. cmd.action = cpu_to_le16(CMD_ACT_SET);
  1371. cmd.enable = !!enable;
  1372. cmd.P0 = p0;
  1373. cmd.P1 = p1;
  1374. cmd.P2 = p2;
  1375. ret = lbs_cmd_with_response(priv, CMD_802_11_PA_CFG , &cmd);
  1376. return ret;
  1377. }
  1378. struct cmd_ctrl_node *__lbs_cmd_async(struct lbs_private *priv,
  1379. uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
  1380. int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
  1381. unsigned long callback_arg)
  1382. {
  1383. struct cmd_ctrl_node *cmdnode;
  1384. lbs_deb_enter(LBS_DEB_HOST);
  1385. if (priv->surpriseremoved) {
  1386. lbs_deb_host("PREP_CMD: card removed\n");
  1387. cmdnode = ERR_PTR(-ENOENT);
  1388. goto done;
  1389. }
  1390. if (!lbs_is_cmd_allowed(priv)) {
  1391. cmdnode = ERR_PTR(-EBUSY);
  1392. goto done;
  1393. }
  1394. cmdnode = lbs_get_cmd_ctrl_node(priv);
  1395. if (cmdnode == NULL) {
  1396. lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
  1397. /* Wake up main thread to execute next command */
  1398. wake_up_interruptible(&priv->waitq);
  1399. cmdnode = ERR_PTR(-ENOBUFS);
  1400. goto done;
  1401. }
  1402. cmdnode->callback = callback;
  1403. cmdnode->callback_arg = callback_arg;
  1404. /* Copy the incoming command to the buffer */
  1405. memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
  1406. /* Set sequence number, clean result, move to buffer */
  1407. priv->seqnum++;
  1408. cmdnode->cmdbuf->command = cpu_to_le16(command);
  1409. cmdnode->cmdbuf->size = cpu_to_le16(in_cmd_size);
  1410. cmdnode->cmdbuf->seqnum = cpu_to_le16(priv->seqnum);
  1411. cmdnode->cmdbuf->result = 0;
  1412. lbs_deb_host("PREP_CMD: command 0x%04x\n", command);
  1413. cmdnode->cmdwaitqwoken = 0;
  1414. lbs_queue_cmd(priv, cmdnode);
  1415. wake_up_interruptible(&priv->waitq);
  1416. done:
  1417. lbs_deb_leave_args(LBS_DEB_HOST, "ret %p", cmdnode);
  1418. return cmdnode;
  1419. }
  1420. void lbs_cmd_async(struct lbs_private *priv, uint16_t command,
  1421. struct cmd_header *in_cmd, int in_cmd_size)
  1422. {
  1423. lbs_deb_enter(LBS_DEB_CMD);
  1424. __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
  1425. lbs_cmd_async_callback, 0);
  1426. lbs_deb_leave(LBS_DEB_CMD);
  1427. }
  1428. int __lbs_cmd(struct lbs_private *priv, uint16_t command,
  1429. struct cmd_header *in_cmd, int in_cmd_size,
  1430. int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
  1431. unsigned long callback_arg)
  1432. {
  1433. struct cmd_ctrl_node *cmdnode;
  1434. unsigned long flags;
  1435. int ret = 0;
  1436. lbs_deb_enter(LBS_DEB_HOST);
  1437. cmdnode = __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
  1438. callback, callback_arg);
  1439. if (IS_ERR(cmdnode)) {
  1440. ret = PTR_ERR(cmdnode);
  1441. goto done;
  1442. }
  1443. might_sleep();
  1444. wait_event_interruptible(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);
  1445. spin_lock_irqsave(&priv->driver_lock, flags);
  1446. ret = cmdnode->result;
  1447. if (ret)
  1448. lbs_pr_info("PREP_CMD: command 0x%04x failed: %d\n",
  1449. command, ret);
  1450. __lbs_cleanup_and_insert_cmd(priv, cmdnode);
  1451. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1452. done:
  1453. lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
  1454. return ret;
  1455. }
  1456. EXPORT_SYMBOL_GPL(__lbs_cmd);