cmd.c 42 KB

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