cmd.c 40 KB

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