cmd.c 39 KB

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