cmd.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609
  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. default:
  65. break;
  66. }
  67. return 0;
  68. }
  69. /**
  70. * @brief This function checks if the command is allowed.
  71. *
  72. * @param priv A pointer to lbs_private structure
  73. * @return allowed or not allowed.
  74. */
  75. static int lbs_is_cmd_allowed(struct lbs_private *priv)
  76. {
  77. int ret = 1;
  78. lbs_deb_enter(LBS_DEB_CMD);
  79. if (!priv->is_auto_deep_sleep_enabled) {
  80. if (priv->is_deep_sleep) {
  81. lbs_deb_cmd("command not allowed in deep sleep\n");
  82. ret = 0;
  83. }
  84. }
  85. lbs_deb_leave(LBS_DEB_CMD);
  86. return ret;
  87. }
  88. /**
  89. * @brief Updates the hardware details like MAC address and regulatory region
  90. *
  91. * @param priv A pointer to struct lbs_private structure
  92. *
  93. * @return 0 on success, error on failure
  94. */
  95. int lbs_update_hw_spec(struct lbs_private *priv)
  96. {
  97. struct cmd_ds_get_hw_spec cmd;
  98. int ret = -1;
  99. u32 i;
  100. lbs_deb_enter(LBS_DEB_CMD);
  101. memset(&cmd, 0, sizeof(cmd));
  102. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  103. memcpy(cmd.permanentaddr, priv->current_addr, ETH_ALEN);
  104. ret = lbs_cmd_with_response(priv, CMD_GET_HW_SPEC, &cmd);
  105. if (ret)
  106. goto out;
  107. priv->fwcapinfo = le32_to_cpu(cmd.fwcapinfo);
  108. /* The firmware release is in an interesting format: the patch
  109. * level is in the most significant nibble ... so fix that: */
  110. priv->fwrelease = le32_to_cpu(cmd.fwrelease);
  111. priv->fwrelease = (priv->fwrelease << 8) |
  112. (priv->fwrelease >> 24 & 0xff);
  113. /* Some firmware capabilities:
  114. * CF card firmware 5.0.16p0: cap 0x00000303
  115. * USB dongle firmware 5.110.17p2: cap 0x00000303
  116. */
  117. lbs_pr_info("%pM, fw %u.%u.%up%u, cap 0x%08x\n",
  118. cmd.permanentaddr,
  119. priv->fwrelease >> 24 & 0xff,
  120. priv->fwrelease >> 16 & 0xff,
  121. priv->fwrelease >> 8 & 0xff,
  122. priv->fwrelease & 0xff,
  123. priv->fwcapinfo);
  124. lbs_deb_cmd("GET_HW_SPEC: hardware interface 0x%x, hardware spec 0x%04x\n",
  125. cmd.hwifversion, cmd.version);
  126. /* Clamp region code to 8-bit since FW spec indicates that it should
  127. * only ever be 8-bit, even though the field size is 16-bit. Some firmware
  128. * returns non-zero high 8 bits here.
  129. *
  130. * Firmware version 4.0.102 used in CF8381 has region code shifted. We
  131. * need to check for this problem and handle it properly.
  132. */
  133. if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V4)
  134. priv->regioncode = (le16_to_cpu(cmd.regioncode) >> 8) & 0xFF;
  135. else
  136. priv->regioncode = le16_to_cpu(cmd.regioncode) & 0xFF;
  137. for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
  138. /* use the region code to search for the index */
  139. if (priv->regioncode == lbs_region_code_to_index[i])
  140. break;
  141. }
  142. /* if it's unidentified region code, use the default (USA) */
  143. if (i >= MRVDRV_MAX_REGION_CODE) {
  144. priv->regioncode = 0x10;
  145. lbs_pr_info("unidentified region code; using the default (USA)\n");
  146. }
  147. if (priv->current_addr[0] == 0xff)
  148. memmove(priv->current_addr, cmd.permanentaddr, ETH_ALEN);
  149. memcpy(priv->dev->dev_addr, priv->current_addr, ETH_ALEN);
  150. if (priv->mesh_dev)
  151. memcpy(priv->mesh_dev->dev_addr, priv->current_addr, ETH_ALEN);
  152. if (lbs_set_regiontable(priv, priv->regioncode, 0)) {
  153. ret = -1;
  154. goto out;
  155. }
  156. out:
  157. lbs_deb_leave(LBS_DEB_CMD);
  158. return ret;
  159. }
  160. int lbs_host_sleep_cfg(struct lbs_private *priv, uint32_t criteria,
  161. struct wol_config *p_wol_config)
  162. {
  163. struct cmd_ds_host_sleep cmd_config;
  164. int ret;
  165. cmd_config.hdr.size = cpu_to_le16(sizeof(cmd_config));
  166. cmd_config.criteria = cpu_to_le32(criteria);
  167. cmd_config.gpio = priv->wol_gpio;
  168. cmd_config.gap = priv->wol_gap;
  169. if (p_wol_config != NULL)
  170. memcpy((uint8_t *)&cmd_config.wol_conf, (uint8_t *)p_wol_config,
  171. sizeof(struct wol_config));
  172. else
  173. cmd_config.wol_conf.action = CMD_ACT_ACTION_NONE;
  174. ret = lbs_cmd_with_response(priv, CMD_802_11_HOST_SLEEP_CFG, &cmd_config);
  175. if (!ret) {
  176. if (criteria) {
  177. lbs_deb_cmd("Set WOL criteria to %x\n", criteria);
  178. priv->wol_criteria = criteria;
  179. } else
  180. memcpy((uint8_t *) p_wol_config,
  181. (uint8_t *)&cmd_config.wol_conf,
  182. sizeof(struct wol_config));
  183. } else {
  184. lbs_pr_info("HOST_SLEEP_CFG failed %d\n", ret);
  185. }
  186. return ret;
  187. }
  188. EXPORT_SYMBOL_GPL(lbs_host_sleep_cfg);
  189. static int lbs_cmd_802_11_ps_mode(struct cmd_ds_command *cmd,
  190. u16 cmd_action)
  191. {
  192. struct cmd_ds_802_11_ps_mode *psm = &cmd->params.psmode;
  193. lbs_deb_enter(LBS_DEB_CMD);
  194. cmd->command = cpu_to_le16(CMD_802_11_PS_MODE);
  195. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ps_mode) +
  196. sizeof(struct cmd_header));
  197. psm->action = cpu_to_le16(cmd_action);
  198. psm->multipledtim = 0;
  199. switch (cmd_action) {
  200. case CMD_SUBCMD_ENTER_PS:
  201. lbs_deb_cmd("PS command:" "SubCode- Enter PS\n");
  202. psm->locallisteninterval = 0;
  203. psm->nullpktinterval = 0;
  204. psm->multipledtim =
  205. cpu_to_le16(MRVDRV_DEFAULT_MULTIPLE_DTIM);
  206. break;
  207. case CMD_SUBCMD_EXIT_PS:
  208. lbs_deb_cmd("PS command:" "SubCode- Exit PS\n");
  209. break;
  210. case CMD_SUBCMD_SLEEP_CONFIRMED:
  211. lbs_deb_cmd("PS command: SubCode- sleep confirm\n");
  212. break;
  213. default:
  214. break;
  215. }
  216. lbs_deb_leave(LBS_DEB_CMD);
  217. return 0;
  218. }
  219. int lbs_cmd_802_11_sleep_params(struct lbs_private *priv, uint16_t cmd_action,
  220. struct sleep_params *sp)
  221. {
  222. struct cmd_ds_802_11_sleep_params cmd;
  223. int ret;
  224. lbs_deb_enter(LBS_DEB_CMD);
  225. if (cmd_action == CMD_ACT_GET) {
  226. memset(&cmd, 0, sizeof(cmd));
  227. } else {
  228. cmd.error = cpu_to_le16(sp->sp_error);
  229. cmd.offset = cpu_to_le16(sp->sp_offset);
  230. cmd.stabletime = cpu_to_le16(sp->sp_stabletime);
  231. cmd.calcontrol = sp->sp_calcontrol;
  232. cmd.externalsleepclk = sp->sp_extsleepclk;
  233. cmd.reserved = cpu_to_le16(sp->sp_reserved);
  234. }
  235. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  236. cmd.action = cpu_to_le16(cmd_action);
  237. ret = lbs_cmd_with_response(priv, CMD_802_11_SLEEP_PARAMS, &cmd);
  238. if (!ret) {
  239. lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, "
  240. "calcontrol 0x%x extsleepclk 0x%x\n",
  241. le16_to_cpu(cmd.error), le16_to_cpu(cmd.offset),
  242. le16_to_cpu(cmd.stabletime), cmd.calcontrol,
  243. cmd.externalsleepclk);
  244. sp->sp_error = le16_to_cpu(cmd.error);
  245. sp->sp_offset = le16_to_cpu(cmd.offset);
  246. sp->sp_stabletime = le16_to_cpu(cmd.stabletime);
  247. sp->sp_calcontrol = cmd.calcontrol;
  248. sp->sp_extsleepclk = cmd.externalsleepclk;
  249. sp->sp_reserved = le16_to_cpu(cmd.reserved);
  250. }
  251. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  252. return 0;
  253. }
  254. static int lbs_wait_for_ds_awake(struct lbs_private *priv)
  255. {
  256. int ret = 0;
  257. lbs_deb_enter(LBS_DEB_CMD);
  258. if (priv->is_deep_sleep) {
  259. if (!wait_event_interruptible_timeout(priv->ds_awake_q,
  260. !priv->is_deep_sleep, (10 * HZ))) {
  261. lbs_pr_err("ds_awake_q: timer expired\n");
  262. ret = -1;
  263. }
  264. }
  265. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  266. return ret;
  267. }
  268. int lbs_set_deep_sleep(struct lbs_private *priv, int deep_sleep)
  269. {
  270. int ret = 0;
  271. lbs_deb_enter(LBS_DEB_CMD);
  272. if (deep_sleep) {
  273. if (priv->is_deep_sleep != 1) {
  274. lbs_deb_cmd("deep sleep: sleep\n");
  275. BUG_ON(!priv->enter_deep_sleep);
  276. ret = priv->enter_deep_sleep(priv);
  277. if (!ret) {
  278. netif_stop_queue(priv->dev);
  279. netif_carrier_off(priv->dev);
  280. }
  281. } else {
  282. lbs_pr_err("deep sleep: already enabled\n");
  283. }
  284. } else {
  285. if (priv->is_deep_sleep) {
  286. lbs_deb_cmd("deep sleep: wakeup\n");
  287. BUG_ON(!priv->exit_deep_sleep);
  288. ret = priv->exit_deep_sleep(priv);
  289. if (!ret) {
  290. ret = lbs_wait_for_ds_awake(priv);
  291. if (ret)
  292. lbs_pr_err("deep sleep: wakeup"
  293. "failed\n");
  294. }
  295. }
  296. }
  297. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  298. return ret;
  299. }
  300. /**
  301. * @brief Set an SNMP MIB value
  302. *
  303. * @param priv A pointer to struct lbs_private structure
  304. * @param oid The OID to set in the firmware
  305. * @param val Value to set the OID to
  306. *
  307. * @return 0 on success, error on failure
  308. */
  309. int lbs_set_snmp_mib(struct lbs_private *priv, u32 oid, u16 val)
  310. {
  311. struct cmd_ds_802_11_snmp_mib cmd;
  312. int ret;
  313. lbs_deb_enter(LBS_DEB_CMD);
  314. memset(&cmd, 0, sizeof (cmd));
  315. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  316. cmd.action = cpu_to_le16(CMD_ACT_SET);
  317. cmd.oid = cpu_to_le16((u16) oid);
  318. switch (oid) {
  319. case SNMP_MIB_OID_BSS_TYPE:
  320. cmd.bufsize = cpu_to_le16(sizeof(u8));
  321. cmd.value[0] = val;
  322. break;
  323. case SNMP_MIB_OID_11D_ENABLE:
  324. case SNMP_MIB_OID_FRAG_THRESHOLD:
  325. case SNMP_MIB_OID_RTS_THRESHOLD:
  326. case SNMP_MIB_OID_SHORT_RETRY_LIMIT:
  327. case SNMP_MIB_OID_LONG_RETRY_LIMIT:
  328. cmd.bufsize = cpu_to_le16(sizeof(u16));
  329. *((__le16 *)(&cmd.value)) = cpu_to_le16(val);
  330. break;
  331. default:
  332. lbs_deb_cmd("SNMP_CMD: (set) unhandled OID 0x%x\n", oid);
  333. ret = -EINVAL;
  334. goto out;
  335. }
  336. lbs_deb_cmd("SNMP_CMD: (set) oid 0x%x, oid size 0x%x, value 0x%x\n",
  337. le16_to_cpu(cmd.oid), le16_to_cpu(cmd.bufsize), val);
  338. ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
  339. out:
  340. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  341. return ret;
  342. }
  343. /**
  344. * @brief Get an SNMP MIB value
  345. *
  346. * @param priv A pointer to struct lbs_private structure
  347. * @param oid The OID to retrieve from the firmware
  348. * @param out_val Location for the returned value
  349. *
  350. * @return 0 on success, error on failure
  351. */
  352. int lbs_get_snmp_mib(struct lbs_private *priv, u32 oid, u16 *out_val)
  353. {
  354. struct cmd_ds_802_11_snmp_mib cmd;
  355. int ret;
  356. lbs_deb_enter(LBS_DEB_CMD);
  357. memset(&cmd, 0, sizeof (cmd));
  358. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  359. cmd.action = cpu_to_le16(CMD_ACT_GET);
  360. cmd.oid = cpu_to_le16(oid);
  361. ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
  362. if (ret)
  363. goto out;
  364. switch (le16_to_cpu(cmd.bufsize)) {
  365. case sizeof(u8):
  366. *out_val = cmd.value[0];
  367. break;
  368. case sizeof(u16):
  369. *out_val = le16_to_cpu(*((__le16 *)(&cmd.value)));
  370. break;
  371. default:
  372. lbs_deb_cmd("SNMP_CMD: (get) unhandled OID 0x%x size %d\n",
  373. oid, le16_to_cpu(cmd.bufsize));
  374. break;
  375. }
  376. out:
  377. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  378. return ret;
  379. }
  380. /**
  381. * @brief Get the min, max, and current TX power
  382. *
  383. * @param priv A pointer to struct lbs_private structure
  384. * @param curlevel Current power level in dBm
  385. * @param minlevel Minimum supported power level in dBm (optional)
  386. * @param maxlevel Maximum supported power level in dBm (optional)
  387. *
  388. * @return 0 on success, error on failure
  389. */
  390. int lbs_get_tx_power(struct lbs_private *priv, s16 *curlevel, s16 *minlevel,
  391. s16 *maxlevel)
  392. {
  393. struct cmd_ds_802_11_rf_tx_power cmd;
  394. int ret;
  395. lbs_deb_enter(LBS_DEB_CMD);
  396. memset(&cmd, 0, sizeof(cmd));
  397. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  398. cmd.action = cpu_to_le16(CMD_ACT_GET);
  399. ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
  400. if (ret == 0) {
  401. *curlevel = le16_to_cpu(cmd.curlevel);
  402. if (minlevel)
  403. *minlevel = cmd.minlevel;
  404. if (maxlevel)
  405. *maxlevel = cmd.maxlevel;
  406. }
  407. lbs_deb_leave(LBS_DEB_CMD);
  408. return ret;
  409. }
  410. /**
  411. * @brief Set the TX power
  412. *
  413. * @param priv A pointer to struct lbs_private structure
  414. * @param dbm The desired power level in dBm
  415. *
  416. * @return 0 on success, error on failure
  417. */
  418. int lbs_set_tx_power(struct lbs_private *priv, s16 dbm)
  419. {
  420. struct cmd_ds_802_11_rf_tx_power cmd;
  421. int ret;
  422. lbs_deb_enter(LBS_DEB_CMD);
  423. memset(&cmd, 0, sizeof(cmd));
  424. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  425. cmd.action = cpu_to_le16(CMD_ACT_SET);
  426. cmd.curlevel = cpu_to_le16(dbm);
  427. lbs_deb_cmd("SET_RF_TX_POWER: %d dBm\n", dbm);
  428. ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
  429. lbs_deb_leave(LBS_DEB_CMD);
  430. return ret;
  431. }
  432. static int lbs_cmd_802_11_monitor_mode(struct cmd_ds_command *cmd,
  433. u16 cmd_action, void *pdata_buf)
  434. {
  435. struct cmd_ds_802_11_monitor_mode *monitor = &cmd->params.monitor;
  436. cmd->command = cpu_to_le16(CMD_802_11_MONITOR_MODE);
  437. cmd->size =
  438. cpu_to_le16(sizeof(struct cmd_ds_802_11_monitor_mode) +
  439. sizeof(struct cmd_header));
  440. monitor->action = cpu_to_le16(cmd_action);
  441. if (cmd_action == CMD_ACT_SET) {
  442. monitor->mode =
  443. cpu_to_le16((u16) (*(u32 *) pdata_buf));
  444. }
  445. return 0;
  446. }
  447. /**
  448. * @brief Get the radio channel
  449. *
  450. * @param priv A pointer to struct lbs_private structure
  451. *
  452. * @return The channel on success, error on failure
  453. */
  454. static int lbs_get_channel(struct lbs_private *priv)
  455. {
  456. struct cmd_ds_802_11_rf_channel cmd;
  457. int ret = 0;
  458. lbs_deb_enter(LBS_DEB_CMD);
  459. memset(&cmd, 0, sizeof(cmd));
  460. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  461. cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_GET);
  462. ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
  463. if (ret)
  464. goto out;
  465. ret = le16_to_cpu(cmd.channel);
  466. lbs_deb_cmd("current radio channel is %d\n", ret);
  467. out:
  468. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  469. return ret;
  470. }
  471. int lbs_update_channel(struct lbs_private *priv)
  472. {
  473. int ret;
  474. /* the channel in f/w could be out of sync; get the current channel */
  475. lbs_deb_enter(LBS_DEB_ASSOC);
  476. ret = lbs_get_channel(priv);
  477. if (ret > 0) {
  478. priv->channel = ret;
  479. ret = 0;
  480. }
  481. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  482. return ret;
  483. }
  484. /**
  485. * @brief Set the radio channel
  486. *
  487. * @param priv A pointer to struct lbs_private structure
  488. * @param channel The desired channel, or 0 to clear a locked channel
  489. *
  490. * @return 0 on success, error on failure
  491. */
  492. int lbs_set_channel(struct lbs_private *priv, u8 channel)
  493. {
  494. struct cmd_ds_802_11_rf_channel cmd;
  495. #ifdef DEBUG
  496. u8 old_channel = priv->channel;
  497. #endif
  498. int ret = 0;
  499. lbs_deb_enter(LBS_DEB_CMD);
  500. memset(&cmd, 0, sizeof(cmd));
  501. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  502. cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
  503. cmd.channel = cpu_to_le16(channel);
  504. ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
  505. if (ret)
  506. goto out;
  507. priv->channel = (uint8_t) le16_to_cpu(cmd.channel);
  508. lbs_deb_cmd("channel switch from %d to %d\n", old_channel,
  509. priv->channel);
  510. out:
  511. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  512. return ret;
  513. }
  514. static int lbs_cmd_reg_access(struct cmd_ds_command *cmdptr,
  515. u8 cmd_action, void *pdata_buf)
  516. {
  517. struct lbs_offset_value *offval;
  518. lbs_deb_enter(LBS_DEB_CMD);
  519. offval = (struct lbs_offset_value *)pdata_buf;
  520. switch (le16_to_cpu(cmdptr->command)) {
  521. case CMD_MAC_REG_ACCESS:
  522. {
  523. struct cmd_ds_mac_reg_access *macreg;
  524. cmdptr->size =
  525. cpu_to_le16(sizeof (struct cmd_ds_mac_reg_access)
  526. + sizeof(struct cmd_header));
  527. macreg =
  528. (struct cmd_ds_mac_reg_access *)&cmdptr->params.
  529. macreg;
  530. macreg->action = cpu_to_le16(cmd_action);
  531. macreg->offset = cpu_to_le16((u16) offval->offset);
  532. macreg->value = cpu_to_le32(offval->value);
  533. break;
  534. }
  535. case CMD_BBP_REG_ACCESS:
  536. {
  537. struct cmd_ds_bbp_reg_access *bbpreg;
  538. cmdptr->size =
  539. cpu_to_le16(sizeof
  540. (struct cmd_ds_bbp_reg_access)
  541. + sizeof(struct cmd_header));
  542. bbpreg =
  543. (struct cmd_ds_bbp_reg_access *)&cmdptr->params.
  544. bbpreg;
  545. bbpreg->action = cpu_to_le16(cmd_action);
  546. bbpreg->offset = cpu_to_le16((u16) offval->offset);
  547. bbpreg->value = (u8) offval->value;
  548. break;
  549. }
  550. case CMD_RF_REG_ACCESS:
  551. {
  552. struct cmd_ds_rf_reg_access *rfreg;
  553. cmdptr->size =
  554. cpu_to_le16(sizeof
  555. (struct cmd_ds_rf_reg_access) +
  556. sizeof(struct cmd_header));
  557. rfreg =
  558. (struct cmd_ds_rf_reg_access *)&cmdptr->params.
  559. rfreg;
  560. rfreg->action = cpu_to_le16(cmd_action);
  561. rfreg->offset = cpu_to_le16((u16) offval->offset);
  562. rfreg->value = (u8) offval->value;
  563. break;
  564. }
  565. default:
  566. break;
  567. }
  568. lbs_deb_leave(LBS_DEB_CMD);
  569. return 0;
  570. }
  571. static void lbs_queue_cmd(struct lbs_private *priv,
  572. struct cmd_ctrl_node *cmdnode)
  573. {
  574. unsigned long flags;
  575. int addtail = 1;
  576. lbs_deb_enter(LBS_DEB_HOST);
  577. if (!cmdnode) {
  578. lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
  579. goto done;
  580. }
  581. if (!cmdnode->cmdbuf->size) {
  582. lbs_deb_host("DNLD_CMD: cmd size is zero\n");
  583. goto done;
  584. }
  585. cmdnode->result = 0;
  586. /* Exit_PS command needs to be queued in the header always. */
  587. if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_PS_MODE) {
  588. struct cmd_ds_802_11_ps_mode *psm = (void *) &cmdnode->cmdbuf[1];
  589. if (psm->action == cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
  590. if (priv->psstate != PS_STATE_FULL_POWER)
  591. addtail = 0;
  592. }
  593. }
  594. spin_lock_irqsave(&priv->driver_lock, flags);
  595. if (addtail)
  596. list_add_tail(&cmdnode->list, &priv->cmdpendingq);
  597. else
  598. list_add(&cmdnode->list, &priv->cmdpendingq);
  599. spin_unlock_irqrestore(&priv->driver_lock, flags);
  600. lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
  601. le16_to_cpu(cmdnode->cmdbuf->command));
  602. done:
  603. lbs_deb_leave(LBS_DEB_HOST);
  604. }
  605. static void lbs_submit_command(struct lbs_private *priv,
  606. struct cmd_ctrl_node *cmdnode)
  607. {
  608. unsigned long flags;
  609. struct cmd_header *cmd;
  610. uint16_t cmdsize;
  611. uint16_t command;
  612. int timeo = 3 * HZ;
  613. int ret;
  614. lbs_deb_enter(LBS_DEB_HOST);
  615. cmd = cmdnode->cmdbuf;
  616. spin_lock_irqsave(&priv->driver_lock, flags);
  617. priv->cur_cmd = cmdnode;
  618. priv->cur_cmd_retcode = 0;
  619. spin_unlock_irqrestore(&priv->driver_lock, flags);
  620. cmdsize = le16_to_cpu(cmd->size);
  621. command = le16_to_cpu(cmd->command);
  622. /* These commands take longer */
  623. if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE)
  624. timeo = 5 * HZ;
  625. lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
  626. command, le16_to_cpu(cmd->seqnum), cmdsize);
  627. lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
  628. ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
  629. if (ret) {
  630. lbs_pr_info("DNLD_CMD: hw_host_to_card failed: %d\n", ret);
  631. /* Let the timer kick in and retry, and potentially reset
  632. the whole thing if the condition persists */
  633. timeo = HZ/4;
  634. }
  635. if (command == CMD_802_11_DEEP_SLEEP) {
  636. if (priv->is_auto_deep_sleep_enabled) {
  637. priv->wakeup_dev_required = 1;
  638. priv->dnld_sent = 0;
  639. }
  640. priv->is_deep_sleep = 1;
  641. lbs_complete_command(priv, cmdnode, 0);
  642. } else {
  643. /* Setup the timer after transmit command */
  644. mod_timer(&priv->command_timer, jiffies + timeo);
  645. }
  646. lbs_deb_leave(LBS_DEB_HOST);
  647. }
  648. /**
  649. * This function inserts command node to cmdfreeq
  650. * after cleans it. Requires priv->driver_lock held.
  651. */
  652. static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
  653. struct cmd_ctrl_node *cmdnode)
  654. {
  655. lbs_deb_enter(LBS_DEB_HOST);
  656. if (!cmdnode)
  657. goto out;
  658. cmdnode->callback = NULL;
  659. cmdnode->callback_arg = 0;
  660. memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
  661. list_add_tail(&cmdnode->list, &priv->cmdfreeq);
  662. out:
  663. lbs_deb_leave(LBS_DEB_HOST);
  664. }
  665. static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
  666. struct cmd_ctrl_node *ptempcmd)
  667. {
  668. unsigned long flags;
  669. spin_lock_irqsave(&priv->driver_lock, flags);
  670. __lbs_cleanup_and_insert_cmd(priv, ptempcmd);
  671. spin_unlock_irqrestore(&priv->driver_lock, flags);
  672. }
  673. void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
  674. int result)
  675. {
  676. if (cmd == priv->cur_cmd)
  677. priv->cur_cmd_retcode = result;
  678. cmd->result = result;
  679. cmd->cmdwaitqwoken = 1;
  680. wake_up_interruptible(&cmd->cmdwait_q);
  681. if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
  682. __lbs_cleanup_and_insert_cmd(priv, cmd);
  683. priv->cur_cmd = NULL;
  684. }
  685. int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
  686. {
  687. struct cmd_ds_802_11_radio_control cmd;
  688. int ret = -EINVAL;
  689. lbs_deb_enter(LBS_DEB_CMD);
  690. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  691. cmd.action = cpu_to_le16(CMD_ACT_SET);
  692. /* Only v8 and below support setting the preamble */
  693. if (priv->fwrelease < 0x09000000) {
  694. switch (preamble) {
  695. case RADIO_PREAMBLE_SHORT:
  696. case RADIO_PREAMBLE_AUTO:
  697. case RADIO_PREAMBLE_LONG:
  698. cmd.control = cpu_to_le16(preamble);
  699. break;
  700. default:
  701. goto out;
  702. }
  703. }
  704. if (radio_on)
  705. cmd.control |= cpu_to_le16(0x1);
  706. else {
  707. cmd.control &= cpu_to_le16(~0x1);
  708. priv->txpower_cur = 0;
  709. }
  710. lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
  711. radio_on ? "ON" : "OFF", preamble);
  712. priv->radio_on = radio_on;
  713. ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
  714. out:
  715. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  716. return ret;
  717. }
  718. void lbs_set_mac_control(struct lbs_private *priv)
  719. {
  720. struct cmd_ds_mac_control cmd;
  721. lbs_deb_enter(LBS_DEB_CMD);
  722. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  723. cmd.action = cpu_to_le16(priv->mac_control);
  724. cmd.reserved = 0;
  725. lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
  726. lbs_deb_leave(LBS_DEB_CMD);
  727. }
  728. /**
  729. * @brief This function prepare the command before send to firmware.
  730. *
  731. * @param priv A pointer to struct lbs_private structure
  732. * @param cmd_no command number
  733. * @param cmd_action command action: GET or SET
  734. * @param wait_option wait option: wait response or not
  735. * @param cmd_oid cmd oid: treated as sub command
  736. * @param pdata_buf A pointer to informaion buffer
  737. * @return 0 or -1
  738. */
  739. int lbs_prepare_and_send_command(struct lbs_private *priv,
  740. u16 cmd_no,
  741. u16 cmd_action,
  742. u16 wait_option, u32 cmd_oid, void *pdata_buf)
  743. {
  744. int ret = 0;
  745. struct cmd_ctrl_node *cmdnode;
  746. struct cmd_ds_command *cmdptr;
  747. unsigned long flags;
  748. lbs_deb_enter(LBS_DEB_HOST);
  749. if (!priv) {
  750. lbs_deb_host("PREP_CMD: priv is NULL\n");
  751. ret = -1;
  752. goto done;
  753. }
  754. if (priv->surpriseremoved) {
  755. lbs_deb_host("PREP_CMD: card removed\n");
  756. ret = -1;
  757. goto done;
  758. }
  759. if (!lbs_is_cmd_allowed(priv)) {
  760. ret = -EBUSY;
  761. goto done;
  762. }
  763. cmdnode = lbs_get_cmd_ctrl_node(priv);
  764. if (cmdnode == NULL) {
  765. lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
  766. /* Wake up main thread to execute next command */
  767. wake_up_interruptible(&priv->waitq);
  768. ret = -1;
  769. goto done;
  770. }
  771. cmdnode->callback = NULL;
  772. cmdnode->callback_arg = (unsigned long)pdata_buf;
  773. cmdptr = (struct cmd_ds_command *)cmdnode->cmdbuf;
  774. lbs_deb_host("PREP_CMD: command 0x%04x\n", cmd_no);
  775. /* Set sequence number, command and INT option */
  776. priv->seqnum++;
  777. cmdptr->seqnum = cpu_to_le16(priv->seqnum);
  778. cmdptr->command = cpu_to_le16(cmd_no);
  779. cmdptr->result = 0;
  780. switch (cmd_no) {
  781. case CMD_802_11_PS_MODE:
  782. ret = lbs_cmd_802_11_ps_mode(cmdptr, cmd_action);
  783. break;
  784. case CMD_MAC_REG_ACCESS:
  785. case CMD_BBP_REG_ACCESS:
  786. case CMD_RF_REG_ACCESS:
  787. ret = lbs_cmd_reg_access(cmdptr, cmd_action, pdata_buf);
  788. break;
  789. case CMD_802_11_MONITOR_MODE:
  790. ret = lbs_cmd_802_11_monitor_mode(cmdptr,
  791. cmd_action, pdata_buf);
  792. break;
  793. case CMD_802_11_RSSI:
  794. ret = lbs_cmd_802_11_rssi(priv, cmdptr);
  795. break;
  796. case CMD_802_11_SET_AFC:
  797. case CMD_802_11_GET_AFC:
  798. cmdptr->command = cpu_to_le16(cmd_no);
  799. cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_afc) +
  800. sizeof(struct cmd_header));
  801. memmove(&cmdptr->params.afc,
  802. pdata_buf, sizeof(struct cmd_ds_802_11_afc));
  803. ret = 0;
  804. goto done;
  805. case CMD_802_11_TPC_CFG:
  806. cmdptr->command = cpu_to_le16(CMD_802_11_TPC_CFG);
  807. cmdptr->size =
  808. cpu_to_le16(sizeof(struct cmd_ds_802_11_tpc_cfg) +
  809. sizeof(struct cmd_header));
  810. memmove(&cmdptr->params.tpccfg,
  811. pdata_buf, sizeof(struct cmd_ds_802_11_tpc_cfg));
  812. ret = 0;
  813. break;
  814. #ifdef CONFIG_LIBERTAS_MESH
  815. case CMD_BT_ACCESS:
  816. ret = lbs_cmd_bt_access(cmdptr, cmd_action, pdata_buf);
  817. break;
  818. case CMD_FWT_ACCESS:
  819. ret = lbs_cmd_fwt_access(cmdptr, cmd_action, pdata_buf);
  820. break;
  821. #endif
  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);