cmd.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220
  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 <net/iw_handler.h>
  6. #include <net/lib80211.h>
  7. #include <linux/kfifo.h>
  8. #include "host.h"
  9. #include "hostcmd.h"
  10. #include "decl.h"
  11. #include "defs.h"
  12. #include "dev.h"
  13. #include "assoc.h"
  14. #include "wext.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. /* Determine mesh_fw_ver from fwrelease and fwcapinfo */
  127. /* 5.0.16p0 9.0.0.p0 is known to NOT support any mesh */
  128. /* 5.110.22 have mesh command with 0xa3 command id */
  129. /* 10.0.0.p0 FW brings in mesh config command with different id */
  130. /* Check FW version MSB and initialize mesh_fw_ver */
  131. if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V5)
  132. priv->mesh_fw_ver = MESH_FW_OLD;
  133. else if ((MRVL_FW_MAJOR_REV(priv->fwrelease) >= MRVL_FW_V10) &&
  134. (priv->fwcapinfo & MESH_CAPINFO_ENABLE_MASK))
  135. priv->mesh_fw_ver = MESH_FW_NEW;
  136. else
  137. priv->mesh_fw_ver = MESH_NONE;
  138. /* Clamp region code to 8-bit since FW spec indicates that it should
  139. * only ever be 8-bit, even though the field size is 16-bit. Some firmware
  140. * returns non-zero high 8 bits here.
  141. *
  142. * Firmware version 4.0.102 used in CF8381 has region code shifted. We
  143. * need to check for this problem and handle it properly.
  144. */
  145. if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V4)
  146. priv->regioncode = (le16_to_cpu(cmd.regioncode) >> 8) & 0xFF;
  147. else
  148. priv->regioncode = le16_to_cpu(cmd.regioncode) & 0xFF;
  149. for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
  150. /* use the region code to search for the index */
  151. if (priv->regioncode == lbs_region_code_to_index[i])
  152. break;
  153. }
  154. /* if it's unidentified region code, use the default (USA) */
  155. if (i >= MRVDRV_MAX_REGION_CODE) {
  156. priv->regioncode = 0x10;
  157. lbs_pr_info("unidentified region code; using the default (USA)\n");
  158. }
  159. if (priv->current_addr[0] == 0xff)
  160. memmove(priv->current_addr, cmd.permanentaddr, ETH_ALEN);
  161. memcpy(priv->dev->dev_addr, priv->current_addr, ETH_ALEN);
  162. if (priv->mesh_dev)
  163. memcpy(priv->mesh_dev->dev_addr, priv->current_addr, ETH_ALEN);
  164. if (lbs_set_regiontable(priv, priv->regioncode, 0)) {
  165. ret = -1;
  166. goto out;
  167. }
  168. if (lbs_set_universaltable(priv, 0)) {
  169. ret = -1;
  170. goto out;
  171. }
  172. out:
  173. lbs_deb_leave(LBS_DEB_CMD);
  174. return ret;
  175. }
  176. int lbs_host_sleep_cfg(struct lbs_private *priv, uint32_t criteria,
  177. struct wol_config *p_wol_config)
  178. {
  179. struct cmd_ds_host_sleep cmd_config;
  180. int ret;
  181. cmd_config.hdr.size = cpu_to_le16(sizeof(cmd_config));
  182. cmd_config.criteria = cpu_to_le32(criteria);
  183. cmd_config.gpio = priv->wol_gpio;
  184. cmd_config.gap = priv->wol_gap;
  185. if (p_wol_config != NULL)
  186. memcpy((uint8_t *)&cmd_config.wol_conf, (uint8_t *)p_wol_config,
  187. sizeof(struct wol_config));
  188. else
  189. cmd_config.wol_conf.action = CMD_ACT_ACTION_NONE;
  190. ret = lbs_cmd_with_response(priv, CMD_802_11_HOST_SLEEP_CFG, &cmd_config);
  191. if (!ret) {
  192. if (criteria) {
  193. lbs_deb_cmd("Set WOL criteria to %x\n", criteria);
  194. priv->wol_criteria = criteria;
  195. } else
  196. memcpy((uint8_t *) p_wol_config,
  197. (uint8_t *)&cmd_config.wol_conf,
  198. sizeof(struct wol_config));
  199. } else {
  200. lbs_pr_info("HOST_SLEEP_CFG failed %d\n", ret);
  201. }
  202. return ret;
  203. }
  204. EXPORT_SYMBOL_GPL(lbs_host_sleep_cfg);
  205. static int lbs_cmd_802_11_ps_mode(struct cmd_ds_command *cmd,
  206. u16 cmd_action)
  207. {
  208. struct cmd_ds_802_11_ps_mode *psm = &cmd->params.psmode;
  209. lbs_deb_enter(LBS_DEB_CMD);
  210. cmd->command = cpu_to_le16(CMD_802_11_PS_MODE);
  211. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ps_mode) +
  212. S_DS_GEN);
  213. psm->action = cpu_to_le16(cmd_action);
  214. psm->multipledtim = 0;
  215. switch (cmd_action) {
  216. case CMD_SUBCMD_ENTER_PS:
  217. lbs_deb_cmd("PS command:" "SubCode- Enter PS\n");
  218. psm->locallisteninterval = 0;
  219. psm->nullpktinterval = 0;
  220. psm->multipledtim =
  221. cpu_to_le16(MRVDRV_DEFAULT_MULTIPLE_DTIM);
  222. break;
  223. case CMD_SUBCMD_EXIT_PS:
  224. lbs_deb_cmd("PS command:" "SubCode- Exit PS\n");
  225. break;
  226. case CMD_SUBCMD_SLEEP_CONFIRMED:
  227. lbs_deb_cmd("PS command: SubCode- sleep confirm\n");
  228. break;
  229. default:
  230. break;
  231. }
  232. lbs_deb_leave(LBS_DEB_CMD);
  233. return 0;
  234. }
  235. int lbs_cmd_802_11_inactivity_timeout(struct lbs_private *priv,
  236. uint16_t cmd_action, uint16_t *timeout)
  237. {
  238. struct cmd_ds_802_11_inactivity_timeout cmd;
  239. int ret;
  240. lbs_deb_enter(LBS_DEB_CMD);
  241. cmd.hdr.command = cpu_to_le16(CMD_802_11_INACTIVITY_TIMEOUT);
  242. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  243. cmd.action = cpu_to_le16(cmd_action);
  244. if (cmd_action == CMD_ACT_SET)
  245. cmd.timeout = cpu_to_le16(*timeout);
  246. else
  247. cmd.timeout = 0;
  248. ret = lbs_cmd_with_response(priv, CMD_802_11_INACTIVITY_TIMEOUT, &cmd);
  249. if (!ret)
  250. *timeout = le16_to_cpu(cmd.timeout);
  251. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  252. return 0;
  253. }
  254. int lbs_cmd_802_11_sleep_params(struct lbs_private *priv, uint16_t cmd_action,
  255. struct sleep_params *sp)
  256. {
  257. struct cmd_ds_802_11_sleep_params cmd;
  258. int ret;
  259. lbs_deb_enter(LBS_DEB_CMD);
  260. if (cmd_action == CMD_ACT_GET) {
  261. memset(&cmd, 0, sizeof(cmd));
  262. } else {
  263. cmd.error = cpu_to_le16(sp->sp_error);
  264. cmd.offset = cpu_to_le16(sp->sp_offset);
  265. cmd.stabletime = cpu_to_le16(sp->sp_stabletime);
  266. cmd.calcontrol = sp->sp_calcontrol;
  267. cmd.externalsleepclk = sp->sp_extsleepclk;
  268. cmd.reserved = cpu_to_le16(sp->sp_reserved);
  269. }
  270. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  271. cmd.action = cpu_to_le16(cmd_action);
  272. ret = lbs_cmd_with_response(priv, CMD_802_11_SLEEP_PARAMS, &cmd);
  273. if (!ret) {
  274. lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, "
  275. "calcontrol 0x%x extsleepclk 0x%x\n",
  276. le16_to_cpu(cmd.error), le16_to_cpu(cmd.offset),
  277. le16_to_cpu(cmd.stabletime), cmd.calcontrol,
  278. cmd.externalsleepclk);
  279. sp->sp_error = le16_to_cpu(cmd.error);
  280. sp->sp_offset = le16_to_cpu(cmd.offset);
  281. sp->sp_stabletime = le16_to_cpu(cmd.stabletime);
  282. sp->sp_calcontrol = cmd.calcontrol;
  283. sp->sp_extsleepclk = cmd.externalsleepclk;
  284. sp->sp_reserved = le16_to_cpu(cmd.reserved);
  285. }
  286. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  287. return 0;
  288. }
  289. static int lbs_wait_for_ds_awake(struct lbs_private *priv)
  290. {
  291. int ret = 0;
  292. lbs_deb_enter(LBS_DEB_CMD);
  293. if (priv->is_deep_sleep) {
  294. if (!wait_event_interruptible_timeout(priv->ds_awake_q,
  295. !priv->is_deep_sleep, (10 * HZ))) {
  296. lbs_pr_err("ds_awake_q: timer expired\n");
  297. ret = -1;
  298. }
  299. }
  300. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  301. return ret;
  302. }
  303. int lbs_set_deep_sleep(struct lbs_private *priv, int deep_sleep)
  304. {
  305. int ret = 0;
  306. lbs_deb_enter(LBS_DEB_CMD);
  307. if (deep_sleep) {
  308. if (priv->is_deep_sleep != 1) {
  309. lbs_deb_cmd("deep sleep: sleep\n");
  310. BUG_ON(!priv->enter_deep_sleep);
  311. ret = priv->enter_deep_sleep(priv);
  312. if (!ret) {
  313. netif_stop_queue(priv->dev);
  314. netif_carrier_off(priv->dev);
  315. }
  316. } else {
  317. lbs_pr_err("deep sleep: already enabled\n");
  318. }
  319. } else {
  320. if (priv->is_deep_sleep) {
  321. lbs_deb_cmd("deep sleep: wakeup\n");
  322. BUG_ON(!priv->exit_deep_sleep);
  323. ret = priv->exit_deep_sleep(priv);
  324. if (!ret) {
  325. ret = lbs_wait_for_ds_awake(priv);
  326. if (ret)
  327. lbs_pr_err("deep sleep: wakeup"
  328. "failed\n");
  329. }
  330. }
  331. }
  332. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  333. return ret;
  334. }
  335. int lbs_cmd_802_11_set_wep(struct lbs_private *priv, uint16_t cmd_action,
  336. struct assoc_request *assoc)
  337. {
  338. struct cmd_ds_802_11_set_wep cmd;
  339. int ret = 0;
  340. lbs_deb_enter(LBS_DEB_CMD);
  341. memset(&cmd, 0, sizeof(cmd));
  342. cmd.hdr.command = cpu_to_le16(CMD_802_11_SET_WEP);
  343. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  344. cmd.action = cpu_to_le16(cmd_action);
  345. if (cmd_action == CMD_ACT_ADD) {
  346. int i;
  347. /* default tx key index */
  348. cmd.keyindex = cpu_to_le16(assoc->wep_tx_keyidx &
  349. CMD_WEP_KEY_INDEX_MASK);
  350. /* Copy key types and material to host command structure */
  351. for (i = 0; i < 4; i++) {
  352. struct enc_key *pkey = &assoc->wep_keys[i];
  353. switch (pkey->len) {
  354. case KEY_LEN_WEP_40:
  355. cmd.keytype[i] = CMD_TYPE_WEP_40_BIT;
  356. memmove(cmd.keymaterial[i], pkey->key, pkey->len);
  357. lbs_deb_cmd("SET_WEP: add key %d (40 bit)\n", i);
  358. break;
  359. case KEY_LEN_WEP_104:
  360. cmd.keytype[i] = CMD_TYPE_WEP_104_BIT;
  361. memmove(cmd.keymaterial[i], pkey->key, pkey->len);
  362. lbs_deb_cmd("SET_WEP: add key %d (104 bit)\n", i);
  363. break;
  364. case 0:
  365. break;
  366. default:
  367. lbs_deb_cmd("SET_WEP: invalid key %d, length %d\n",
  368. i, pkey->len);
  369. ret = -1;
  370. goto done;
  371. break;
  372. }
  373. }
  374. } else if (cmd_action == CMD_ACT_REMOVE) {
  375. /* ACT_REMOVE clears _all_ WEP keys */
  376. /* default tx key index */
  377. cmd.keyindex = cpu_to_le16(priv->wep_tx_keyidx &
  378. CMD_WEP_KEY_INDEX_MASK);
  379. lbs_deb_cmd("SET_WEP: remove key %d\n", priv->wep_tx_keyidx);
  380. }
  381. ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
  382. done:
  383. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  384. return ret;
  385. }
  386. int lbs_cmd_802_11_enable_rsn(struct lbs_private *priv, uint16_t cmd_action,
  387. uint16_t *enable)
  388. {
  389. struct cmd_ds_802_11_enable_rsn cmd;
  390. int ret;
  391. lbs_deb_enter(LBS_DEB_CMD);
  392. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  393. cmd.action = cpu_to_le16(cmd_action);
  394. if (cmd_action == CMD_ACT_GET)
  395. cmd.enable = 0;
  396. else {
  397. if (*enable)
  398. cmd.enable = cpu_to_le16(CMD_ENABLE_RSN);
  399. else
  400. cmd.enable = cpu_to_le16(CMD_DISABLE_RSN);
  401. lbs_deb_cmd("ENABLE_RSN: %d\n", *enable);
  402. }
  403. ret = lbs_cmd_with_response(priv, CMD_802_11_ENABLE_RSN, &cmd);
  404. if (!ret && cmd_action == CMD_ACT_GET)
  405. *enable = le16_to_cpu(cmd.enable);
  406. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  407. return ret;
  408. }
  409. static void set_one_wpa_key(struct MrvlIEtype_keyParamSet *keyparam,
  410. struct enc_key *key)
  411. {
  412. lbs_deb_enter(LBS_DEB_CMD);
  413. if (key->flags & KEY_INFO_WPA_ENABLED)
  414. keyparam->keyinfo |= cpu_to_le16(KEY_INFO_WPA_ENABLED);
  415. if (key->flags & KEY_INFO_WPA_UNICAST)
  416. keyparam->keyinfo |= cpu_to_le16(KEY_INFO_WPA_UNICAST);
  417. if (key->flags & KEY_INFO_WPA_MCAST)
  418. keyparam->keyinfo |= cpu_to_le16(KEY_INFO_WPA_MCAST);
  419. keyparam->type = cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
  420. keyparam->keytypeid = cpu_to_le16(key->type);
  421. keyparam->keylen = cpu_to_le16(key->len);
  422. memcpy(keyparam->key, key->key, key->len);
  423. /* Length field doesn't include the {type,length} header */
  424. keyparam->length = cpu_to_le16(sizeof(*keyparam) - 4);
  425. lbs_deb_leave(LBS_DEB_CMD);
  426. }
  427. int lbs_cmd_802_11_key_material(struct lbs_private *priv, uint16_t cmd_action,
  428. struct assoc_request *assoc)
  429. {
  430. struct cmd_ds_802_11_key_material cmd;
  431. int ret = 0;
  432. int index = 0;
  433. lbs_deb_enter(LBS_DEB_CMD);
  434. cmd.action = cpu_to_le16(cmd_action);
  435. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  436. if (cmd_action == CMD_ACT_GET) {
  437. cmd.hdr.size = cpu_to_le16(S_DS_GEN + 2);
  438. } else {
  439. memset(cmd.keyParamSet, 0, sizeof(cmd.keyParamSet));
  440. if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc->flags)) {
  441. set_one_wpa_key(&cmd.keyParamSet[index],
  442. &assoc->wpa_unicast_key);
  443. index++;
  444. }
  445. if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc->flags)) {
  446. set_one_wpa_key(&cmd.keyParamSet[index],
  447. &assoc->wpa_mcast_key);
  448. index++;
  449. }
  450. /* The common header and as many keys as we included */
  451. cmd.hdr.size = cpu_to_le16(offsetof(typeof(cmd),
  452. keyParamSet[index]));
  453. }
  454. ret = lbs_cmd_with_response(priv, CMD_802_11_KEY_MATERIAL, &cmd);
  455. /* Copy the returned key to driver private data */
  456. if (!ret && cmd_action == CMD_ACT_GET) {
  457. void *buf_ptr = cmd.keyParamSet;
  458. void *resp_end = &(&cmd)[1];
  459. while (buf_ptr < resp_end) {
  460. struct MrvlIEtype_keyParamSet *keyparam = buf_ptr;
  461. struct enc_key *key;
  462. uint16_t param_set_len = le16_to_cpu(keyparam->length);
  463. uint16_t key_len = le16_to_cpu(keyparam->keylen);
  464. uint16_t key_flags = le16_to_cpu(keyparam->keyinfo);
  465. uint16_t key_type = le16_to_cpu(keyparam->keytypeid);
  466. void *end;
  467. end = (void *)keyparam + sizeof(keyparam->type)
  468. + sizeof(keyparam->length) + param_set_len;
  469. /* Make sure we don't access past the end of the IEs */
  470. if (end > resp_end)
  471. break;
  472. if (key_flags & KEY_INFO_WPA_UNICAST)
  473. key = &priv->wpa_unicast_key;
  474. else if (key_flags & KEY_INFO_WPA_MCAST)
  475. key = &priv->wpa_mcast_key;
  476. else
  477. break;
  478. /* Copy returned key into driver */
  479. memset(key, 0, sizeof(struct enc_key));
  480. if (key_len > sizeof(key->key))
  481. break;
  482. key->type = key_type;
  483. key->flags = key_flags;
  484. key->len = key_len;
  485. memcpy(key->key, keyparam->key, key->len);
  486. buf_ptr = end + 1;
  487. }
  488. }
  489. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  490. return ret;
  491. }
  492. /**
  493. * @brief Set an SNMP MIB value
  494. *
  495. * @param priv A pointer to struct lbs_private structure
  496. * @param oid The OID to set in the firmware
  497. * @param val Value to set the OID to
  498. *
  499. * @return 0 on success, error on failure
  500. */
  501. int lbs_set_snmp_mib(struct lbs_private *priv, u32 oid, u16 val)
  502. {
  503. struct cmd_ds_802_11_snmp_mib cmd;
  504. int ret;
  505. lbs_deb_enter(LBS_DEB_CMD);
  506. memset(&cmd, 0, sizeof (cmd));
  507. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  508. cmd.action = cpu_to_le16(CMD_ACT_SET);
  509. cmd.oid = cpu_to_le16((u16) oid);
  510. switch (oid) {
  511. case SNMP_MIB_OID_BSS_TYPE:
  512. cmd.bufsize = cpu_to_le16(sizeof(u8));
  513. cmd.value[0] = (val == IW_MODE_ADHOC) ? 2 : 1;
  514. break;
  515. case SNMP_MIB_OID_11D_ENABLE:
  516. case SNMP_MIB_OID_FRAG_THRESHOLD:
  517. case SNMP_MIB_OID_RTS_THRESHOLD:
  518. case SNMP_MIB_OID_SHORT_RETRY_LIMIT:
  519. case SNMP_MIB_OID_LONG_RETRY_LIMIT:
  520. cmd.bufsize = cpu_to_le16(sizeof(u16));
  521. *((__le16 *)(&cmd.value)) = cpu_to_le16(val);
  522. break;
  523. default:
  524. lbs_deb_cmd("SNMP_CMD: (set) unhandled OID 0x%x\n", oid);
  525. ret = -EINVAL;
  526. goto out;
  527. }
  528. lbs_deb_cmd("SNMP_CMD: (set) oid 0x%x, oid size 0x%x, value 0x%x\n",
  529. le16_to_cpu(cmd.oid), le16_to_cpu(cmd.bufsize), val);
  530. ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
  531. out:
  532. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  533. return ret;
  534. }
  535. /**
  536. * @brief Get an SNMP MIB value
  537. *
  538. * @param priv A pointer to struct lbs_private structure
  539. * @param oid The OID to retrieve from the firmware
  540. * @param out_val Location for the returned value
  541. *
  542. * @return 0 on success, error on failure
  543. */
  544. int lbs_get_snmp_mib(struct lbs_private *priv, u32 oid, u16 *out_val)
  545. {
  546. struct cmd_ds_802_11_snmp_mib cmd;
  547. int ret;
  548. lbs_deb_enter(LBS_DEB_CMD);
  549. memset(&cmd, 0, sizeof (cmd));
  550. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  551. cmd.action = cpu_to_le16(CMD_ACT_GET);
  552. cmd.oid = cpu_to_le16(oid);
  553. ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
  554. if (ret)
  555. goto out;
  556. switch (le16_to_cpu(cmd.bufsize)) {
  557. case sizeof(u8):
  558. if (oid == SNMP_MIB_OID_BSS_TYPE) {
  559. if (cmd.value[0] == 2)
  560. *out_val = IW_MODE_ADHOC;
  561. else
  562. *out_val = IW_MODE_INFRA;
  563. } else
  564. *out_val = cmd.value[0];
  565. break;
  566. case sizeof(u16):
  567. *out_val = le16_to_cpu(*((__le16 *)(&cmd.value)));
  568. break;
  569. default:
  570. lbs_deb_cmd("SNMP_CMD: (get) unhandled OID 0x%x size %d\n",
  571. oid, le16_to_cpu(cmd.bufsize));
  572. break;
  573. }
  574. out:
  575. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  576. return ret;
  577. }
  578. /**
  579. * @brief Get the min, max, and current TX power
  580. *
  581. * @param priv A pointer to struct lbs_private structure
  582. * @param curlevel Current power level in dBm
  583. * @param minlevel Minimum supported power level in dBm (optional)
  584. * @param maxlevel Maximum supported power level in dBm (optional)
  585. *
  586. * @return 0 on success, error on failure
  587. */
  588. int lbs_get_tx_power(struct lbs_private *priv, s16 *curlevel, s16 *minlevel,
  589. s16 *maxlevel)
  590. {
  591. struct cmd_ds_802_11_rf_tx_power cmd;
  592. int ret;
  593. lbs_deb_enter(LBS_DEB_CMD);
  594. memset(&cmd, 0, sizeof(cmd));
  595. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  596. cmd.action = cpu_to_le16(CMD_ACT_GET);
  597. ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
  598. if (ret == 0) {
  599. *curlevel = le16_to_cpu(cmd.curlevel);
  600. if (minlevel)
  601. *minlevel = cmd.minlevel;
  602. if (maxlevel)
  603. *maxlevel = cmd.maxlevel;
  604. }
  605. lbs_deb_leave(LBS_DEB_CMD);
  606. return ret;
  607. }
  608. /**
  609. * @brief Set the TX power
  610. *
  611. * @param priv A pointer to struct lbs_private structure
  612. * @param dbm The desired power level in dBm
  613. *
  614. * @return 0 on success, error on failure
  615. */
  616. int lbs_set_tx_power(struct lbs_private *priv, s16 dbm)
  617. {
  618. struct cmd_ds_802_11_rf_tx_power cmd;
  619. int ret;
  620. lbs_deb_enter(LBS_DEB_CMD);
  621. memset(&cmd, 0, sizeof(cmd));
  622. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  623. cmd.action = cpu_to_le16(CMD_ACT_SET);
  624. cmd.curlevel = cpu_to_le16(dbm);
  625. lbs_deb_cmd("SET_RF_TX_POWER: %d dBm\n", dbm);
  626. ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
  627. lbs_deb_leave(LBS_DEB_CMD);
  628. return ret;
  629. }
  630. static int lbs_cmd_802_11_monitor_mode(struct cmd_ds_command *cmd,
  631. u16 cmd_action, void *pdata_buf)
  632. {
  633. struct cmd_ds_802_11_monitor_mode *monitor = &cmd->params.monitor;
  634. cmd->command = cpu_to_le16(CMD_802_11_MONITOR_MODE);
  635. cmd->size =
  636. cpu_to_le16(sizeof(struct cmd_ds_802_11_monitor_mode) +
  637. S_DS_GEN);
  638. monitor->action = cpu_to_le16(cmd_action);
  639. if (cmd_action == CMD_ACT_SET) {
  640. monitor->mode =
  641. cpu_to_le16((u16) (*(u32 *) pdata_buf));
  642. }
  643. return 0;
  644. }
  645. static __le16 lbs_rate_to_fw_bitmap(int rate, int lower_rates_ok)
  646. {
  647. /* Bit Rate
  648. * 15:13 Reserved
  649. * 12 54 Mbps
  650. * 11 48 Mbps
  651. * 10 36 Mbps
  652. * 9 24 Mbps
  653. * 8 18 Mbps
  654. * 7 12 Mbps
  655. * 6 9 Mbps
  656. * 5 6 Mbps
  657. * 4 Reserved
  658. * 3 11 Mbps
  659. * 2 5.5 Mbps
  660. * 1 2 Mbps
  661. * 0 1 Mbps
  662. **/
  663. uint16_t ratemask;
  664. int i = lbs_data_rate_to_fw_index(rate);
  665. if (lower_rates_ok)
  666. ratemask = (0x1fef >> (12 - i));
  667. else
  668. ratemask = (1 << i);
  669. return cpu_to_le16(ratemask);
  670. }
  671. int lbs_cmd_802_11_rate_adapt_rateset(struct lbs_private *priv,
  672. uint16_t cmd_action)
  673. {
  674. struct cmd_ds_802_11_rate_adapt_rateset cmd;
  675. int ret;
  676. lbs_deb_enter(LBS_DEB_CMD);
  677. if (!priv->cur_rate && !priv->enablehwauto)
  678. return -EINVAL;
  679. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  680. cmd.action = cpu_to_le16(cmd_action);
  681. cmd.enablehwauto = cpu_to_le16(priv->enablehwauto);
  682. cmd.bitmap = lbs_rate_to_fw_bitmap(priv->cur_rate, priv->enablehwauto);
  683. ret = lbs_cmd_with_response(priv, CMD_802_11_RATE_ADAPT_RATESET, &cmd);
  684. if (!ret && cmd_action == CMD_ACT_GET) {
  685. priv->ratebitmap = le16_to_cpu(cmd.bitmap);
  686. priv->enablehwauto = le16_to_cpu(cmd.enablehwauto);
  687. }
  688. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  689. return ret;
  690. }
  691. EXPORT_SYMBOL_GPL(lbs_cmd_802_11_rate_adapt_rateset);
  692. /**
  693. * @brief Set the data rate
  694. *
  695. * @param priv A pointer to struct lbs_private structure
  696. * @param rate The desired data rate, or 0 to clear a locked rate
  697. *
  698. * @return 0 on success, error on failure
  699. */
  700. int lbs_set_data_rate(struct lbs_private *priv, u8 rate)
  701. {
  702. struct cmd_ds_802_11_data_rate cmd;
  703. int ret = 0;
  704. lbs_deb_enter(LBS_DEB_CMD);
  705. memset(&cmd, 0, sizeof(cmd));
  706. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  707. if (rate > 0) {
  708. cmd.action = cpu_to_le16(CMD_ACT_SET_TX_FIX_RATE);
  709. cmd.rates[0] = lbs_data_rate_to_fw_index(rate);
  710. if (cmd.rates[0] == 0) {
  711. lbs_deb_cmd("DATA_RATE: invalid requested rate of"
  712. " 0x%02X\n", rate);
  713. ret = 0;
  714. goto out;
  715. }
  716. lbs_deb_cmd("DATA_RATE: set fixed 0x%02X\n", cmd.rates[0]);
  717. } else {
  718. cmd.action = cpu_to_le16(CMD_ACT_SET_TX_AUTO);
  719. lbs_deb_cmd("DATA_RATE: setting auto\n");
  720. }
  721. ret = lbs_cmd_with_response(priv, CMD_802_11_DATA_RATE, &cmd);
  722. if (ret)
  723. goto out;
  724. lbs_deb_hex(LBS_DEB_CMD, "DATA_RATE_RESP", (u8 *) &cmd, sizeof (cmd));
  725. /* FIXME: get actual rates FW can do if this command actually returns
  726. * all data rates supported.
  727. */
  728. priv->cur_rate = lbs_fw_index_to_data_rate(cmd.rates[0]);
  729. lbs_deb_cmd("DATA_RATE: current rate is 0x%02x\n", priv->cur_rate);
  730. out:
  731. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  732. return ret;
  733. }
  734. /**
  735. * @brief Get the radio channel
  736. *
  737. * @param priv A pointer to struct lbs_private structure
  738. *
  739. * @return The channel on success, error on failure
  740. */
  741. int lbs_get_channel(struct lbs_private *priv)
  742. {
  743. struct cmd_ds_802_11_rf_channel cmd;
  744. int ret = 0;
  745. lbs_deb_enter(LBS_DEB_CMD);
  746. memset(&cmd, 0, sizeof(cmd));
  747. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  748. cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_GET);
  749. ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
  750. if (ret)
  751. goto out;
  752. ret = le16_to_cpu(cmd.channel);
  753. lbs_deb_cmd("current radio channel is %d\n", ret);
  754. out:
  755. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  756. return ret;
  757. }
  758. int lbs_update_channel(struct lbs_private *priv)
  759. {
  760. int ret;
  761. /* the channel in f/w could be out of sync; get the current channel */
  762. lbs_deb_enter(LBS_DEB_ASSOC);
  763. ret = lbs_get_channel(priv);
  764. if (ret > 0) {
  765. priv->curbssparams.channel = ret;
  766. ret = 0;
  767. }
  768. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  769. return ret;
  770. }
  771. /**
  772. * @brief Set the radio channel
  773. *
  774. * @param priv A pointer to struct lbs_private structure
  775. * @param channel The desired channel, or 0 to clear a locked channel
  776. *
  777. * @return 0 on success, error on failure
  778. */
  779. int lbs_set_channel(struct lbs_private *priv, u8 channel)
  780. {
  781. struct cmd_ds_802_11_rf_channel cmd;
  782. #ifdef DEBUG
  783. u8 old_channel = priv->curbssparams.channel;
  784. #endif
  785. int ret = 0;
  786. lbs_deb_enter(LBS_DEB_CMD);
  787. memset(&cmd, 0, sizeof(cmd));
  788. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  789. cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
  790. cmd.channel = cpu_to_le16(channel);
  791. ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
  792. if (ret)
  793. goto out;
  794. priv->curbssparams.channel = (uint8_t) le16_to_cpu(cmd.channel);
  795. lbs_deb_cmd("channel switch from %d to %d\n", old_channel,
  796. priv->curbssparams.channel);
  797. out:
  798. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  799. return ret;
  800. }
  801. static int lbs_cmd_802_11_rssi(struct lbs_private *priv,
  802. struct cmd_ds_command *cmd)
  803. {
  804. lbs_deb_enter(LBS_DEB_CMD);
  805. cmd->command = cpu_to_le16(CMD_802_11_RSSI);
  806. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_rssi) + S_DS_GEN);
  807. cmd->params.rssi.N = cpu_to_le16(DEFAULT_BCN_AVG_FACTOR);
  808. /* reset Beacon SNR/NF/RSSI values */
  809. priv->SNR[TYPE_BEACON][TYPE_NOAVG] = 0;
  810. priv->SNR[TYPE_BEACON][TYPE_AVG] = 0;
  811. priv->NF[TYPE_BEACON][TYPE_NOAVG] = 0;
  812. priv->NF[TYPE_BEACON][TYPE_AVG] = 0;
  813. priv->RSSI[TYPE_BEACON][TYPE_NOAVG] = 0;
  814. priv->RSSI[TYPE_BEACON][TYPE_AVG] = 0;
  815. lbs_deb_leave(LBS_DEB_CMD);
  816. return 0;
  817. }
  818. static int lbs_cmd_reg_access(struct cmd_ds_command *cmdptr,
  819. u8 cmd_action, void *pdata_buf)
  820. {
  821. struct lbs_offset_value *offval;
  822. lbs_deb_enter(LBS_DEB_CMD);
  823. offval = (struct lbs_offset_value *)pdata_buf;
  824. switch (le16_to_cpu(cmdptr->command)) {
  825. case CMD_MAC_REG_ACCESS:
  826. {
  827. struct cmd_ds_mac_reg_access *macreg;
  828. cmdptr->size =
  829. cpu_to_le16(sizeof (struct cmd_ds_mac_reg_access)
  830. + S_DS_GEN);
  831. macreg =
  832. (struct cmd_ds_mac_reg_access *)&cmdptr->params.
  833. macreg;
  834. macreg->action = cpu_to_le16(cmd_action);
  835. macreg->offset = cpu_to_le16((u16) offval->offset);
  836. macreg->value = cpu_to_le32(offval->value);
  837. break;
  838. }
  839. case CMD_BBP_REG_ACCESS:
  840. {
  841. struct cmd_ds_bbp_reg_access *bbpreg;
  842. cmdptr->size =
  843. cpu_to_le16(sizeof
  844. (struct cmd_ds_bbp_reg_access)
  845. + S_DS_GEN);
  846. bbpreg =
  847. (struct cmd_ds_bbp_reg_access *)&cmdptr->params.
  848. bbpreg;
  849. bbpreg->action = cpu_to_le16(cmd_action);
  850. bbpreg->offset = cpu_to_le16((u16) offval->offset);
  851. bbpreg->value = (u8) offval->value;
  852. break;
  853. }
  854. case CMD_RF_REG_ACCESS:
  855. {
  856. struct cmd_ds_rf_reg_access *rfreg;
  857. cmdptr->size =
  858. cpu_to_le16(sizeof
  859. (struct cmd_ds_rf_reg_access) +
  860. S_DS_GEN);
  861. rfreg =
  862. (struct cmd_ds_rf_reg_access *)&cmdptr->params.
  863. rfreg;
  864. rfreg->action = cpu_to_le16(cmd_action);
  865. rfreg->offset = cpu_to_le16((u16) offval->offset);
  866. rfreg->value = (u8) offval->value;
  867. break;
  868. }
  869. default:
  870. break;
  871. }
  872. lbs_deb_leave(LBS_DEB_CMD);
  873. return 0;
  874. }
  875. static int lbs_cmd_bt_access(struct cmd_ds_command *cmd,
  876. u16 cmd_action, void *pdata_buf)
  877. {
  878. struct cmd_ds_bt_access *bt_access = &cmd->params.bt;
  879. lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
  880. cmd->command = cpu_to_le16(CMD_BT_ACCESS);
  881. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_bt_access) + S_DS_GEN);
  882. cmd->result = 0;
  883. bt_access->action = cpu_to_le16(cmd_action);
  884. switch (cmd_action) {
  885. case CMD_ACT_BT_ACCESS_ADD:
  886. memcpy(bt_access->addr1, pdata_buf, 2 * ETH_ALEN);
  887. lbs_deb_hex(LBS_DEB_MESH, "BT_ADD: blinded MAC addr", bt_access->addr1, 6);
  888. break;
  889. case CMD_ACT_BT_ACCESS_DEL:
  890. memcpy(bt_access->addr1, pdata_buf, 1 * ETH_ALEN);
  891. lbs_deb_hex(LBS_DEB_MESH, "BT_DEL: blinded MAC addr", bt_access->addr1, 6);
  892. break;
  893. case CMD_ACT_BT_ACCESS_LIST:
  894. bt_access->id = cpu_to_le32(*(u32 *) pdata_buf);
  895. break;
  896. case CMD_ACT_BT_ACCESS_RESET:
  897. break;
  898. case CMD_ACT_BT_ACCESS_SET_INVERT:
  899. bt_access->id = cpu_to_le32(*(u32 *) pdata_buf);
  900. break;
  901. case CMD_ACT_BT_ACCESS_GET_INVERT:
  902. break;
  903. default:
  904. break;
  905. }
  906. lbs_deb_leave(LBS_DEB_CMD);
  907. return 0;
  908. }
  909. static int lbs_cmd_fwt_access(struct cmd_ds_command *cmd,
  910. u16 cmd_action, void *pdata_buf)
  911. {
  912. struct cmd_ds_fwt_access *fwt_access = &cmd->params.fwt;
  913. lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
  914. cmd->command = cpu_to_le16(CMD_FWT_ACCESS);
  915. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_fwt_access) + S_DS_GEN);
  916. cmd->result = 0;
  917. if (pdata_buf)
  918. memcpy(fwt_access, pdata_buf, sizeof(*fwt_access));
  919. else
  920. memset(fwt_access, 0, sizeof(*fwt_access));
  921. fwt_access->action = cpu_to_le16(cmd_action);
  922. lbs_deb_leave(LBS_DEB_CMD);
  923. return 0;
  924. }
  925. int lbs_mesh_access(struct lbs_private *priv, uint16_t cmd_action,
  926. struct cmd_ds_mesh_access *cmd)
  927. {
  928. int ret;
  929. lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
  930. cmd->hdr.command = cpu_to_le16(CMD_MESH_ACCESS);
  931. cmd->hdr.size = cpu_to_le16(sizeof(*cmd));
  932. cmd->hdr.result = 0;
  933. cmd->action = cpu_to_le16(cmd_action);
  934. ret = lbs_cmd_with_response(priv, CMD_MESH_ACCESS, cmd);
  935. lbs_deb_leave(LBS_DEB_CMD);
  936. return ret;
  937. }
  938. static int __lbs_mesh_config_send(struct lbs_private *priv,
  939. struct cmd_ds_mesh_config *cmd,
  940. uint16_t action, uint16_t type)
  941. {
  942. int ret;
  943. u16 command = CMD_MESH_CONFIG_OLD;
  944. lbs_deb_enter(LBS_DEB_CMD);
  945. /*
  946. * Command id is 0xac for v10 FW along with mesh interface
  947. * id in bits 14-13-12.
  948. */
  949. if (priv->mesh_fw_ver == MESH_FW_NEW)
  950. command = CMD_MESH_CONFIG |
  951. (MESH_IFACE_ID << MESH_IFACE_BIT_OFFSET);
  952. cmd->hdr.command = cpu_to_le16(command);
  953. cmd->hdr.size = cpu_to_le16(sizeof(struct cmd_ds_mesh_config));
  954. cmd->hdr.result = 0;
  955. cmd->type = cpu_to_le16(type);
  956. cmd->action = cpu_to_le16(action);
  957. ret = lbs_cmd_with_response(priv, command, cmd);
  958. lbs_deb_leave(LBS_DEB_CMD);
  959. return ret;
  960. }
  961. int lbs_mesh_config_send(struct lbs_private *priv,
  962. struct cmd_ds_mesh_config *cmd,
  963. uint16_t action, uint16_t type)
  964. {
  965. int ret;
  966. if (!(priv->fwcapinfo & FW_CAPINFO_PERSISTENT_CONFIG))
  967. return -EOPNOTSUPP;
  968. ret = __lbs_mesh_config_send(priv, cmd, action, type);
  969. return ret;
  970. }
  971. /* This function is the CMD_MESH_CONFIG legacy function. It only handles the
  972. * START and STOP actions. The extended actions supported by CMD_MESH_CONFIG
  973. * are all handled by preparing a struct cmd_ds_mesh_config and passing it to
  974. * lbs_mesh_config_send.
  975. */
  976. int lbs_mesh_config(struct lbs_private *priv, uint16_t action, uint16_t chan)
  977. {
  978. struct cmd_ds_mesh_config cmd;
  979. struct mrvl_meshie *ie;
  980. DECLARE_SSID_BUF(ssid);
  981. memset(&cmd, 0, sizeof(cmd));
  982. cmd.channel = cpu_to_le16(chan);
  983. ie = (struct mrvl_meshie *)cmd.data;
  984. switch (action) {
  985. case CMD_ACT_MESH_CONFIG_START:
  986. ie->id = WLAN_EID_GENERIC;
  987. ie->val.oui[0] = 0x00;
  988. ie->val.oui[1] = 0x50;
  989. ie->val.oui[2] = 0x43;
  990. ie->val.type = MARVELL_MESH_IE_TYPE;
  991. ie->val.subtype = MARVELL_MESH_IE_SUBTYPE;
  992. ie->val.version = MARVELL_MESH_IE_VERSION;
  993. ie->val.active_protocol_id = MARVELL_MESH_PROTO_ID_HWMP;
  994. ie->val.active_metric_id = MARVELL_MESH_METRIC_ID;
  995. ie->val.mesh_capability = MARVELL_MESH_CAPABILITY;
  996. ie->val.mesh_id_len = priv->mesh_ssid_len;
  997. memcpy(ie->val.mesh_id, priv->mesh_ssid, priv->mesh_ssid_len);
  998. ie->len = sizeof(struct mrvl_meshie_val) -
  999. IW_ESSID_MAX_SIZE + priv->mesh_ssid_len;
  1000. cmd.length = cpu_to_le16(sizeof(struct mrvl_meshie_val));
  1001. break;
  1002. case CMD_ACT_MESH_CONFIG_STOP:
  1003. break;
  1004. default:
  1005. return -1;
  1006. }
  1007. lbs_deb_cmd("mesh config action %d type %x channel %d SSID %s\n",
  1008. action, priv->mesh_tlv, chan,
  1009. print_ssid(ssid, priv->mesh_ssid, priv->mesh_ssid_len));
  1010. return __lbs_mesh_config_send(priv, &cmd, action, priv->mesh_tlv);
  1011. }
  1012. static int lbs_cmd_bcn_ctrl(struct lbs_private * priv,
  1013. struct cmd_ds_command *cmd,
  1014. u16 cmd_action)
  1015. {
  1016. struct cmd_ds_802_11_beacon_control
  1017. *bcn_ctrl = &cmd->params.bcn_ctrl;
  1018. lbs_deb_enter(LBS_DEB_CMD);
  1019. cmd->size =
  1020. cpu_to_le16(sizeof(struct cmd_ds_802_11_beacon_control)
  1021. + S_DS_GEN);
  1022. cmd->command = cpu_to_le16(CMD_802_11_BEACON_CTRL);
  1023. bcn_ctrl->action = cpu_to_le16(cmd_action);
  1024. bcn_ctrl->beacon_enable = cpu_to_le16(priv->beacon_enable);
  1025. bcn_ctrl->beacon_period = cpu_to_le16(priv->beacon_period);
  1026. lbs_deb_leave(LBS_DEB_CMD);
  1027. return 0;
  1028. }
  1029. static void lbs_queue_cmd(struct lbs_private *priv,
  1030. struct cmd_ctrl_node *cmdnode)
  1031. {
  1032. unsigned long flags;
  1033. int addtail = 1;
  1034. lbs_deb_enter(LBS_DEB_HOST);
  1035. if (!cmdnode) {
  1036. lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
  1037. goto done;
  1038. }
  1039. if (!cmdnode->cmdbuf->size) {
  1040. lbs_deb_host("DNLD_CMD: cmd size is zero\n");
  1041. goto done;
  1042. }
  1043. cmdnode->result = 0;
  1044. /* Exit_PS command needs to be queued in the header always. */
  1045. if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_PS_MODE) {
  1046. struct cmd_ds_802_11_ps_mode *psm = (void *) &cmdnode->cmdbuf[1];
  1047. if (psm->action == cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
  1048. if (priv->psstate != PS_STATE_FULL_POWER)
  1049. addtail = 0;
  1050. }
  1051. }
  1052. spin_lock_irqsave(&priv->driver_lock, flags);
  1053. if (addtail)
  1054. list_add_tail(&cmdnode->list, &priv->cmdpendingq);
  1055. else
  1056. list_add(&cmdnode->list, &priv->cmdpendingq);
  1057. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1058. lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
  1059. le16_to_cpu(cmdnode->cmdbuf->command));
  1060. done:
  1061. lbs_deb_leave(LBS_DEB_HOST);
  1062. }
  1063. static void lbs_submit_command(struct lbs_private *priv,
  1064. struct cmd_ctrl_node *cmdnode)
  1065. {
  1066. unsigned long flags;
  1067. struct cmd_header *cmd;
  1068. uint16_t cmdsize;
  1069. uint16_t command;
  1070. int timeo = 3 * HZ;
  1071. int ret;
  1072. lbs_deb_enter(LBS_DEB_HOST);
  1073. cmd = cmdnode->cmdbuf;
  1074. spin_lock_irqsave(&priv->driver_lock, flags);
  1075. priv->cur_cmd = cmdnode;
  1076. priv->cur_cmd_retcode = 0;
  1077. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1078. cmdsize = le16_to_cpu(cmd->size);
  1079. command = le16_to_cpu(cmd->command);
  1080. /* These commands take longer */
  1081. if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE)
  1082. timeo = 5 * HZ;
  1083. lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
  1084. command, le16_to_cpu(cmd->seqnum), cmdsize);
  1085. lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
  1086. ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
  1087. if (ret) {
  1088. lbs_pr_info("DNLD_CMD: hw_host_to_card failed: %d\n", ret);
  1089. /* Let the timer kick in and retry, and potentially reset
  1090. the whole thing if the condition persists */
  1091. timeo = HZ/4;
  1092. }
  1093. if (command == CMD_802_11_DEEP_SLEEP) {
  1094. if (priv->is_auto_deep_sleep_enabled) {
  1095. priv->wakeup_dev_required = 1;
  1096. priv->dnld_sent = 0;
  1097. }
  1098. priv->is_deep_sleep = 1;
  1099. lbs_complete_command(priv, cmdnode, 0);
  1100. } else {
  1101. /* Setup the timer after transmit command */
  1102. mod_timer(&priv->command_timer, jiffies + timeo);
  1103. }
  1104. lbs_deb_leave(LBS_DEB_HOST);
  1105. }
  1106. /**
  1107. * This function inserts command node to cmdfreeq
  1108. * after cleans it. Requires priv->driver_lock held.
  1109. */
  1110. static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
  1111. struct cmd_ctrl_node *cmdnode)
  1112. {
  1113. lbs_deb_enter(LBS_DEB_HOST);
  1114. if (!cmdnode)
  1115. goto out;
  1116. cmdnode->callback = NULL;
  1117. cmdnode->callback_arg = 0;
  1118. memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
  1119. list_add_tail(&cmdnode->list, &priv->cmdfreeq);
  1120. out:
  1121. lbs_deb_leave(LBS_DEB_HOST);
  1122. }
  1123. static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
  1124. struct cmd_ctrl_node *ptempcmd)
  1125. {
  1126. unsigned long flags;
  1127. spin_lock_irqsave(&priv->driver_lock, flags);
  1128. __lbs_cleanup_and_insert_cmd(priv, ptempcmd);
  1129. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1130. }
  1131. void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
  1132. int result)
  1133. {
  1134. if (cmd == priv->cur_cmd)
  1135. priv->cur_cmd_retcode = result;
  1136. cmd->result = result;
  1137. cmd->cmdwaitqwoken = 1;
  1138. wake_up_interruptible(&cmd->cmdwait_q);
  1139. if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
  1140. __lbs_cleanup_and_insert_cmd(priv, cmd);
  1141. priv->cur_cmd = NULL;
  1142. }
  1143. int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
  1144. {
  1145. struct cmd_ds_802_11_radio_control cmd;
  1146. int ret = -EINVAL;
  1147. lbs_deb_enter(LBS_DEB_CMD);
  1148. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1149. cmd.action = cpu_to_le16(CMD_ACT_SET);
  1150. /* Only v8 and below support setting the preamble */
  1151. if (priv->fwrelease < 0x09000000) {
  1152. switch (preamble) {
  1153. case RADIO_PREAMBLE_SHORT:
  1154. if (!(priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
  1155. goto out;
  1156. /* Fall through */
  1157. case RADIO_PREAMBLE_AUTO:
  1158. case RADIO_PREAMBLE_LONG:
  1159. cmd.control = cpu_to_le16(preamble);
  1160. break;
  1161. default:
  1162. goto out;
  1163. }
  1164. }
  1165. if (radio_on)
  1166. cmd.control |= cpu_to_le16(0x1);
  1167. else {
  1168. cmd.control &= cpu_to_le16(~0x1);
  1169. priv->txpower_cur = 0;
  1170. }
  1171. lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
  1172. radio_on ? "ON" : "OFF", preamble);
  1173. priv->radio_on = radio_on;
  1174. ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
  1175. out:
  1176. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  1177. return ret;
  1178. }
  1179. void lbs_set_mac_control(struct lbs_private *priv)
  1180. {
  1181. struct cmd_ds_mac_control cmd;
  1182. lbs_deb_enter(LBS_DEB_CMD);
  1183. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1184. cmd.action = cpu_to_le16(priv->mac_control);
  1185. cmd.reserved = 0;
  1186. lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
  1187. lbs_deb_leave(LBS_DEB_CMD);
  1188. }
  1189. /**
  1190. * @brief This function prepare the command before send to firmware.
  1191. *
  1192. * @param priv A pointer to struct lbs_private structure
  1193. * @param cmd_no command number
  1194. * @param cmd_action command action: GET or SET
  1195. * @param wait_option wait option: wait response or not
  1196. * @param cmd_oid cmd oid: treated as sub command
  1197. * @param pdata_buf A pointer to informaion buffer
  1198. * @return 0 or -1
  1199. */
  1200. int lbs_prepare_and_send_command(struct lbs_private *priv,
  1201. u16 cmd_no,
  1202. u16 cmd_action,
  1203. u16 wait_option, u32 cmd_oid, void *pdata_buf)
  1204. {
  1205. int ret = 0;
  1206. struct cmd_ctrl_node *cmdnode;
  1207. struct cmd_ds_command *cmdptr;
  1208. unsigned long flags;
  1209. lbs_deb_enter(LBS_DEB_HOST);
  1210. if (!priv) {
  1211. lbs_deb_host("PREP_CMD: priv is NULL\n");
  1212. ret = -1;
  1213. goto done;
  1214. }
  1215. if (priv->surpriseremoved) {
  1216. lbs_deb_host("PREP_CMD: card removed\n");
  1217. ret = -1;
  1218. goto done;
  1219. }
  1220. if (!lbs_is_cmd_allowed(priv)) {
  1221. ret = -EBUSY;
  1222. goto done;
  1223. }
  1224. cmdnode = lbs_get_cmd_ctrl_node(priv);
  1225. if (cmdnode == NULL) {
  1226. lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
  1227. /* Wake up main thread to execute next command */
  1228. wake_up_interruptible(&priv->waitq);
  1229. ret = -1;
  1230. goto done;
  1231. }
  1232. cmdnode->callback = NULL;
  1233. cmdnode->callback_arg = (unsigned long)pdata_buf;
  1234. cmdptr = (struct cmd_ds_command *)cmdnode->cmdbuf;
  1235. lbs_deb_host("PREP_CMD: command 0x%04x\n", cmd_no);
  1236. /* Set sequence number, command and INT option */
  1237. priv->seqnum++;
  1238. cmdptr->seqnum = cpu_to_le16(priv->seqnum);
  1239. cmdptr->command = cpu_to_le16(cmd_no);
  1240. cmdptr->result = 0;
  1241. switch (cmd_no) {
  1242. case CMD_802_11_PS_MODE:
  1243. ret = lbs_cmd_802_11_ps_mode(cmdptr, cmd_action);
  1244. break;
  1245. case CMD_MAC_REG_ACCESS:
  1246. case CMD_BBP_REG_ACCESS:
  1247. case CMD_RF_REG_ACCESS:
  1248. ret = lbs_cmd_reg_access(cmdptr, cmd_action, pdata_buf);
  1249. break;
  1250. case CMD_802_11_MONITOR_MODE:
  1251. ret = lbs_cmd_802_11_monitor_mode(cmdptr,
  1252. cmd_action, pdata_buf);
  1253. break;
  1254. case CMD_802_11_RSSI:
  1255. ret = lbs_cmd_802_11_rssi(priv, cmdptr);
  1256. break;
  1257. case CMD_802_11_SET_AFC:
  1258. case CMD_802_11_GET_AFC:
  1259. cmdptr->command = cpu_to_le16(cmd_no);
  1260. cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_afc) +
  1261. S_DS_GEN);
  1262. memmove(&cmdptr->params.afc,
  1263. pdata_buf, sizeof(struct cmd_ds_802_11_afc));
  1264. ret = 0;
  1265. goto done;
  1266. case CMD_802_11D_DOMAIN_INFO:
  1267. ret = lbs_cmd_802_11d_domain_info(priv, cmdptr,
  1268. cmd_no, cmd_action);
  1269. break;
  1270. case CMD_802_11_TPC_CFG:
  1271. cmdptr->command = cpu_to_le16(CMD_802_11_TPC_CFG);
  1272. cmdptr->size =
  1273. cpu_to_le16(sizeof(struct cmd_ds_802_11_tpc_cfg) +
  1274. S_DS_GEN);
  1275. memmove(&cmdptr->params.tpccfg,
  1276. pdata_buf, sizeof(struct cmd_ds_802_11_tpc_cfg));
  1277. ret = 0;
  1278. break;
  1279. case CMD_802_11_LED_GPIO_CTRL:
  1280. {
  1281. struct mrvl_ie_ledgpio *gpio =
  1282. (struct mrvl_ie_ledgpio*)
  1283. cmdptr->params.ledgpio.data;
  1284. memmove(&cmdptr->params.ledgpio,
  1285. pdata_buf,
  1286. sizeof(struct cmd_ds_802_11_led_ctrl));
  1287. cmdptr->command =
  1288. cpu_to_le16(CMD_802_11_LED_GPIO_CTRL);
  1289. #define ACTION_NUMLED_TLVTYPE_LEN_FIELDS_LEN 8
  1290. cmdptr->size =
  1291. cpu_to_le16(le16_to_cpu(gpio->header.len)
  1292. + S_DS_GEN
  1293. + ACTION_NUMLED_TLVTYPE_LEN_FIELDS_LEN);
  1294. gpio->header.len = gpio->header.len;
  1295. ret = 0;
  1296. break;
  1297. }
  1298. case CMD_BT_ACCESS:
  1299. ret = lbs_cmd_bt_access(cmdptr, cmd_action, pdata_buf);
  1300. break;
  1301. case CMD_FWT_ACCESS:
  1302. ret = lbs_cmd_fwt_access(cmdptr, cmd_action, pdata_buf);
  1303. break;
  1304. case CMD_GET_TSF:
  1305. cmdptr->command = cpu_to_le16(CMD_GET_TSF);
  1306. cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_get_tsf) +
  1307. S_DS_GEN);
  1308. ret = 0;
  1309. break;
  1310. case CMD_802_11_BEACON_CTRL:
  1311. ret = lbs_cmd_bcn_ctrl(priv, cmdptr, cmd_action);
  1312. break;
  1313. case CMD_802_11_DEEP_SLEEP:
  1314. cmdptr->command = cpu_to_le16(CMD_802_11_DEEP_SLEEP);
  1315. cmdptr->size = cpu_to_le16(S_DS_GEN);
  1316. break;
  1317. default:
  1318. lbs_pr_err("PREP_CMD: unknown command 0x%04x\n", cmd_no);
  1319. ret = -1;
  1320. break;
  1321. }
  1322. /* return error, since the command preparation failed */
  1323. if (ret != 0) {
  1324. lbs_deb_host("PREP_CMD: command preparation failed\n");
  1325. lbs_cleanup_and_insert_cmd(priv, cmdnode);
  1326. ret = -1;
  1327. goto done;
  1328. }
  1329. cmdnode->cmdwaitqwoken = 0;
  1330. lbs_queue_cmd(priv, cmdnode);
  1331. wake_up_interruptible(&priv->waitq);
  1332. if (wait_option & CMD_OPTION_WAITFORRSP) {
  1333. lbs_deb_host("PREP_CMD: wait for response\n");
  1334. might_sleep();
  1335. wait_event_interruptible(cmdnode->cmdwait_q,
  1336. cmdnode->cmdwaitqwoken);
  1337. }
  1338. spin_lock_irqsave(&priv->driver_lock, flags);
  1339. if (priv->cur_cmd_retcode) {
  1340. lbs_deb_host("PREP_CMD: command failed with return code %d\n",
  1341. priv->cur_cmd_retcode);
  1342. priv->cur_cmd_retcode = 0;
  1343. ret = -1;
  1344. }
  1345. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1346. done:
  1347. lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
  1348. return ret;
  1349. }
  1350. /**
  1351. * @brief This function allocates the command buffer and link
  1352. * it to command free queue.
  1353. *
  1354. * @param priv A pointer to struct lbs_private structure
  1355. * @return 0 or -1
  1356. */
  1357. int lbs_allocate_cmd_buffer(struct lbs_private *priv)
  1358. {
  1359. int ret = 0;
  1360. u32 bufsize;
  1361. u32 i;
  1362. struct cmd_ctrl_node *cmdarray;
  1363. lbs_deb_enter(LBS_DEB_HOST);
  1364. /* Allocate and initialize the command array */
  1365. bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
  1366. if (!(cmdarray = kzalloc(bufsize, GFP_KERNEL))) {
  1367. lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
  1368. ret = -1;
  1369. goto done;
  1370. }
  1371. priv->cmd_array = cmdarray;
  1372. /* Allocate and initialize each command buffer in the command array */
  1373. for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
  1374. cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
  1375. if (!cmdarray[i].cmdbuf) {
  1376. lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
  1377. ret = -1;
  1378. goto done;
  1379. }
  1380. }
  1381. for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
  1382. init_waitqueue_head(&cmdarray[i].cmdwait_q);
  1383. lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
  1384. }
  1385. ret = 0;
  1386. done:
  1387. lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
  1388. return ret;
  1389. }
  1390. /**
  1391. * @brief This function frees the command buffer.
  1392. *
  1393. * @param priv A pointer to struct lbs_private structure
  1394. * @return 0 or -1
  1395. */
  1396. int lbs_free_cmd_buffer(struct lbs_private *priv)
  1397. {
  1398. struct cmd_ctrl_node *cmdarray;
  1399. unsigned int i;
  1400. lbs_deb_enter(LBS_DEB_HOST);
  1401. /* need to check if cmd array is allocated or not */
  1402. if (priv->cmd_array == NULL) {
  1403. lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
  1404. goto done;
  1405. }
  1406. cmdarray = priv->cmd_array;
  1407. /* Release shared memory buffers */
  1408. for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
  1409. if (cmdarray[i].cmdbuf) {
  1410. kfree(cmdarray[i].cmdbuf);
  1411. cmdarray[i].cmdbuf = NULL;
  1412. }
  1413. }
  1414. /* Release cmd_ctrl_node */
  1415. if (priv->cmd_array) {
  1416. kfree(priv->cmd_array);
  1417. priv->cmd_array = NULL;
  1418. }
  1419. done:
  1420. lbs_deb_leave(LBS_DEB_HOST);
  1421. return 0;
  1422. }
  1423. /**
  1424. * @brief This function gets a free command node if available in
  1425. * command free queue.
  1426. *
  1427. * @param priv A pointer to struct lbs_private structure
  1428. * @return cmd_ctrl_node A pointer to cmd_ctrl_node structure or NULL
  1429. */
  1430. static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv)
  1431. {
  1432. struct cmd_ctrl_node *tempnode;
  1433. unsigned long flags;
  1434. lbs_deb_enter(LBS_DEB_HOST);
  1435. if (!priv)
  1436. return NULL;
  1437. spin_lock_irqsave(&priv->driver_lock, flags);
  1438. if (!list_empty(&priv->cmdfreeq)) {
  1439. tempnode = list_first_entry(&priv->cmdfreeq,
  1440. struct cmd_ctrl_node, list);
  1441. list_del(&tempnode->list);
  1442. } else {
  1443. lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
  1444. tempnode = NULL;
  1445. }
  1446. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1447. lbs_deb_leave(LBS_DEB_HOST);
  1448. return tempnode;
  1449. }
  1450. /**
  1451. * @brief This function executes next command in command
  1452. * pending queue. It will put firmware back to PS mode
  1453. * if applicable.
  1454. *
  1455. * @param priv A pointer to struct lbs_private structure
  1456. * @return 0 or -1
  1457. */
  1458. int lbs_execute_next_command(struct lbs_private *priv)
  1459. {
  1460. struct cmd_ctrl_node *cmdnode = NULL;
  1461. struct cmd_header *cmd;
  1462. unsigned long flags;
  1463. int ret = 0;
  1464. /* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
  1465. * only caller to us is lbs_thread() and we get even when a
  1466. * data packet is received */
  1467. lbs_deb_enter(LBS_DEB_THREAD);
  1468. spin_lock_irqsave(&priv->driver_lock, flags);
  1469. if (priv->cur_cmd) {
  1470. lbs_pr_alert( "EXEC_NEXT_CMD: already processing command!\n");
  1471. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1472. ret = -1;
  1473. goto done;
  1474. }
  1475. if (!list_empty(&priv->cmdpendingq)) {
  1476. cmdnode = list_first_entry(&priv->cmdpendingq,
  1477. struct cmd_ctrl_node, list);
  1478. }
  1479. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1480. if (cmdnode) {
  1481. cmd = cmdnode->cmdbuf;
  1482. if (is_command_allowed_in_ps(le16_to_cpu(cmd->command))) {
  1483. if ((priv->psstate == PS_STATE_SLEEP) ||
  1484. (priv->psstate == PS_STATE_PRE_SLEEP)) {
  1485. lbs_deb_host(
  1486. "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
  1487. le16_to_cpu(cmd->command),
  1488. priv->psstate);
  1489. ret = -1;
  1490. goto done;
  1491. }
  1492. lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
  1493. "0x%04x in psstate %d\n",
  1494. le16_to_cpu(cmd->command), priv->psstate);
  1495. } else if (priv->psstate != PS_STATE_FULL_POWER) {
  1496. /*
  1497. * 1. Non-PS command:
  1498. * Queue it. set needtowakeup to TRUE if current state
  1499. * is SLEEP, otherwise call lbs_ps_wakeup to send Exit_PS.
  1500. * 2. PS command but not Exit_PS:
  1501. * Ignore it.
  1502. * 3. PS command Exit_PS:
  1503. * Set needtowakeup to TRUE if current state is SLEEP,
  1504. * otherwise send this command down to firmware
  1505. * immediately.
  1506. */
  1507. if (cmd->command != cpu_to_le16(CMD_802_11_PS_MODE)) {
  1508. /* Prepare to send Exit PS,
  1509. * this non PS command will be sent later */
  1510. if ((priv->psstate == PS_STATE_SLEEP)
  1511. || (priv->psstate == PS_STATE_PRE_SLEEP)
  1512. ) {
  1513. /* w/ new scheme, it will not reach here.
  1514. since it is blocked in main_thread. */
  1515. priv->needtowakeup = 1;
  1516. } else
  1517. lbs_ps_wakeup(priv, 0);
  1518. ret = 0;
  1519. goto done;
  1520. } else {
  1521. /*
  1522. * PS command. Ignore it if it is not Exit_PS.
  1523. * otherwise send it down immediately.
  1524. */
  1525. struct cmd_ds_802_11_ps_mode *psm = (void *)&cmd[1];
  1526. lbs_deb_host(
  1527. "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
  1528. psm->action);
  1529. if (psm->action !=
  1530. cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
  1531. lbs_deb_host(
  1532. "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
  1533. list_del(&cmdnode->list);
  1534. spin_lock_irqsave(&priv->driver_lock, flags);
  1535. lbs_complete_command(priv, cmdnode, 0);
  1536. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1537. ret = 0;
  1538. goto done;
  1539. }
  1540. if ((priv->psstate == PS_STATE_SLEEP) ||
  1541. (priv->psstate == PS_STATE_PRE_SLEEP)) {
  1542. lbs_deb_host(
  1543. "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
  1544. list_del(&cmdnode->list);
  1545. spin_lock_irqsave(&priv->driver_lock, flags);
  1546. lbs_complete_command(priv, cmdnode, 0);
  1547. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1548. priv->needtowakeup = 1;
  1549. ret = 0;
  1550. goto done;
  1551. }
  1552. lbs_deb_host(
  1553. "EXEC_NEXT_CMD: sending EXIT_PS\n");
  1554. }
  1555. }
  1556. list_del(&cmdnode->list);
  1557. lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
  1558. le16_to_cpu(cmd->command));
  1559. lbs_submit_command(priv, cmdnode);
  1560. } else {
  1561. /*
  1562. * check if in power save mode, if yes, put the device back
  1563. * to PS mode
  1564. */
  1565. if ((priv->psmode != LBS802_11POWERMODECAM) &&
  1566. (priv->psstate == PS_STATE_FULL_POWER) &&
  1567. ((priv->connect_status == LBS_CONNECTED) ||
  1568. (priv->mesh_connect_status == LBS_CONNECTED))) {
  1569. if (priv->secinfo.WPAenabled ||
  1570. priv->secinfo.WPA2enabled) {
  1571. /* check for valid WPA group keys */
  1572. if (priv->wpa_mcast_key.len ||
  1573. priv->wpa_unicast_key.len) {
  1574. lbs_deb_host(
  1575. "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
  1576. " go back to PS_SLEEP");
  1577. lbs_ps_sleep(priv, 0);
  1578. }
  1579. } else {
  1580. lbs_deb_host(
  1581. "EXEC_NEXT_CMD: cmdpendingq empty, "
  1582. "go back to PS_SLEEP");
  1583. lbs_ps_sleep(priv, 0);
  1584. }
  1585. }
  1586. }
  1587. ret = 0;
  1588. done:
  1589. lbs_deb_leave(LBS_DEB_THREAD);
  1590. return ret;
  1591. }
  1592. void lbs_send_iwevcustom_event(struct lbs_private *priv, s8 *str)
  1593. {
  1594. union iwreq_data iwrq;
  1595. u8 buf[50];
  1596. lbs_deb_enter(LBS_DEB_WEXT);
  1597. memset(&iwrq, 0, sizeof(union iwreq_data));
  1598. memset(buf, 0, sizeof(buf));
  1599. snprintf(buf, sizeof(buf) - 1, "%s", str);
  1600. iwrq.data.length = strlen(buf) + 1 + IW_EV_LCP_LEN;
  1601. /* Send Event to upper layer */
  1602. lbs_deb_wext("event indication string %s\n", (char *)buf);
  1603. lbs_deb_wext("event indication length %d\n", iwrq.data.length);
  1604. lbs_deb_wext("sending wireless event IWEVCUSTOM for %s\n", str);
  1605. wireless_send_event(priv->dev, IWEVCUSTOM, &iwrq, buf);
  1606. lbs_deb_leave(LBS_DEB_WEXT);
  1607. }
  1608. static void lbs_send_confirmsleep(struct lbs_private *priv)
  1609. {
  1610. unsigned long flags;
  1611. int ret;
  1612. lbs_deb_enter(LBS_DEB_HOST);
  1613. lbs_deb_hex(LBS_DEB_HOST, "sleep confirm", (u8 *) &confirm_sleep,
  1614. sizeof(confirm_sleep));
  1615. ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &confirm_sleep,
  1616. sizeof(confirm_sleep));
  1617. if (ret) {
  1618. lbs_pr_alert("confirm_sleep failed\n");
  1619. goto out;
  1620. }
  1621. spin_lock_irqsave(&priv->driver_lock, flags);
  1622. /* We don't get a response on the sleep-confirmation */
  1623. priv->dnld_sent = DNLD_RES_RECEIVED;
  1624. /* If nothing to do, go back to sleep (?) */
  1625. if (!__kfifo_len(priv->event_fifo) && !priv->resp_len[priv->resp_idx])
  1626. priv->psstate = PS_STATE_SLEEP;
  1627. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1628. out:
  1629. lbs_deb_leave(LBS_DEB_HOST);
  1630. }
  1631. void lbs_ps_sleep(struct lbs_private *priv, int wait_option)
  1632. {
  1633. lbs_deb_enter(LBS_DEB_HOST);
  1634. /*
  1635. * PS is currently supported only in Infrastructure mode
  1636. * Remove this check if it is to be supported in IBSS mode also
  1637. */
  1638. lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
  1639. CMD_SUBCMD_ENTER_PS, wait_option, 0, NULL);
  1640. lbs_deb_leave(LBS_DEB_HOST);
  1641. }
  1642. /**
  1643. * @brief This function sends Exit_PS command to firmware.
  1644. *
  1645. * @param priv A pointer to struct lbs_private structure
  1646. * @param wait_option wait response or not
  1647. * @return n/a
  1648. */
  1649. void lbs_ps_wakeup(struct lbs_private *priv, int wait_option)
  1650. {
  1651. __le32 Localpsmode;
  1652. lbs_deb_enter(LBS_DEB_HOST);
  1653. Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
  1654. lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
  1655. CMD_SUBCMD_EXIT_PS,
  1656. wait_option, 0, &Localpsmode);
  1657. lbs_deb_leave(LBS_DEB_HOST);
  1658. }
  1659. /**
  1660. * @brief This function checks condition and prepares to
  1661. * send sleep confirm command to firmware if ok.
  1662. *
  1663. * @param priv A pointer to struct lbs_private structure
  1664. * @param psmode Power Saving mode
  1665. * @return n/a
  1666. */
  1667. void lbs_ps_confirm_sleep(struct lbs_private *priv)
  1668. {
  1669. unsigned long flags =0;
  1670. int allowed = 1;
  1671. lbs_deb_enter(LBS_DEB_HOST);
  1672. spin_lock_irqsave(&priv->driver_lock, flags);
  1673. if (priv->dnld_sent) {
  1674. allowed = 0;
  1675. lbs_deb_host("dnld_sent was set\n");
  1676. }
  1677. /* In-progress command? */
  1678. if (priv->cur_cmd) {
  1679. allowed = 0;
  1680. lbs_deb_host("cur_cmd was set\n");
  1681. }
  1682. /* Pending events or command responses? */
  1683. if (__kfifo_len(priv->event_fifo) || priv->resp_len[priv->resp_idx]) {
  1684. allowed = 0;
  1685. lbs_deb_host("pending events or command responses\n");
  1686. }
  1687. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1688. if (allowed) {
  1689. lbs_deb_host("sending lbs_ps_confirm_sleep\n");
  1690. lbs_send_confirmsleep(priv);
  1691. } else {
  1692. lbs_deb_host("sleep confirm has been delayed\n");
  1693. }
  1694. lbs_deb_leave(LBS_DEB_HOST);
  1695. }
  1696. /**
  1697. * @brief Configures the transmission power control functionality.
  1698. *
  1699. * @param priv A pointer to struct lbs_private structure
  1700. * @param enable Transmission power control enable
  1701. * @param p0 Power level when link quality is good (dBm).
  1702. * @param p1 Power level when link quality is fair (dBm).
  1703. * @param p2 Power level when link quality is poor (dBm).
  1704. * @param usesnr Use Signal to Noise Ratio in TPC
  1705. *
  1706. * @return 0 on success
  1707. */
  1708. int lbs_set_tpc_cfg(struct lbs_private *priv, int enable, int8_t p0, int8_t p1,
  1709. int8_t p2, int usesnr)
  1710. {
  1711. struct cmd_ds_802_11_tpc_cfg cmd;
  1712. int ret;
  1713. memset(&cmd, 0, sizeof(cmd));
  1714. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1715. cmd.action = cpu_to_le16(CMD_ACT_SET);
  1716. cmd.enable = !!enable;
  1717. cmd.usesnr = !!usesnr;
  1718. cmd.P0 = p0;
  1719. cmd.P1 = p1;
  1720. cmd.P2 = p2;
  1721. ret = lbs_cmd_with_response(priv, CMD_802_11_TPC_CFG, &cmd);
  1722. return ret;
  1723. }
  1724. /**
  1725. * @brief Configures the power adaptation settings.
  1726. *
  1727. * @param priv A pointer to struct lbs_private structure
  1728. * @param enable Power adaptation enable
  1729. * @param p0 Power level for 1, 2, 5.5 and 11 Mbps (dBm).
  1730. * @param p1 Power level for 6, 9, 12, 18, 22, 24 and 36 Mbps (dBm).
  1731. * @param p2 Power level for 48 and 54 Mbps (dBm).
  1732. *
  1733. * @return 0 on Success
  1734. */
  1735. int lbs_set_power_adapt_cfg(struct lbs_private *priv, int enable, int8_t p0,
  1736. int8_t p1, int8_t p2)
  1737. {
  1738. struct cmd_ds_802_11_pa_cfg cmd;
  1739. int ret;
  1740. memset(&cmd, 0, sizeof(cmd));
  1741. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1742. cmd.action = cpu_to_le16(CMD_ACT_SET);
  1743. cmd.enable = !!enable;
  1744. cmd.P0 = p0;
  1745. cmd.P1 = p1;
  1746. cmd.P2 = p2;
  1747. ret = lbs_cmd_with_response(priv, CMD_802_11_PA_CFG , &cmd);
  1748. return ret;
  1749. }
  1750. static struct cmd_ctrl_node *__lbs_cmd_async(struct lbs_private *priv,
  1751. uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
  1752. int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
  1753. unsigned long callback_arg)
  1754. {
  1755. struct cmd_ctrl_node *cmdnode;
  1756. lbs_deb_enter(LBS_DEB_HOST);
  1757. if (priv->surpriseremoved) {
  1758. lbs_deb_host("PREP_CMD: card removed\n");
  1759. cmdnode = ERR_PTR(-ENOENT);
  1760. goto done;
  1761. }
  1762. if (!lbs_is_cmd_allowed(priv)) {
  1763. cmdnode = ERR_PTR(-EBUSY);
  1764. goto done;
  1765. }
  1766. cmdnode = lbs_get_cmd_ctrl_node(priv);
  1767. if (cmdnode == NULL) {
  1768. lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
  1769. /* Wake up main thread to execute next command */
  1770. wake_up_interruptible(&priv->waitq);
  1771. cmdnode = ERR_PTR(-ENOBUFS);
  1772. goto done;
  1773. }
  1774. cmdnode->callback = callback;
  1775. cmdnode->callback_arg = callback_arg;
  1776. /* Copy the incoming command to the buffer */
  1777. memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
  1778. /* Set sequence number, clean result, move to buffer */
  1779. priv->seqnum++;
  1780. cmdnode->cmdbuf->command = cpu_to_le16(command);
  1781. cmdnode->cmdbuf->size = cpu_to_le16(in_cmd_size);
  1782. cmdnode->cmdbuf->seqnum = cpu_to_le16(priv->seqnum);
  1783. cmdnode->cmdbuf->result = 0;
  1784. lbs_deb_host("PREP_CMD: command 0x%04x\n", command);
  1785. cmdnode->cmdwaitqwoken = 0;
  1786. lbs_queue_cmd(priv, cmdnode);
  1787. wake_up_interruptible(&priv->waitq);
  1788. done:
  1789. lbs_deb_leave_args(LBS_DEB_HOST, "ret %p", cmdnode);
  1790. return cmdnode;
  1791. }
  1792. void lbs_cmd_async(struct lbs_private *priv, uint16_t command,
  1793. struct cmd_header *in_cmd, int in_cmd_size)
  1794. {
  1795. lbs_deb_enter(LBS_DEB_CMD);
  1796. __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
  1797. lbs_cmd_async_callback, 0);
  1798. lbs_deb_leave(LBS_DEB_CMD);
  1799. }
  1800. int __lbs_cmd(struct lbs_private *priv, uint16_t command,
  1801. struct cmd_header *in_cmd, int in_cmd_size,
  1802. int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
  1803. unsigned long callback_arg)
  1804. {
  1805. struct cmd_ctrl_node *cmdnode;
  1806. unsigned long flags;
  1807. int ret = 0;
  1808. lbs_deb_enter(LBS_DEB_HOST);
  1809. cmdnode = __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
  1810. callback, callback_arg);
  1811. if (IS_ERR(cmdnode)) {
  1812. ret = PTR_ERR(cmdnode);
  1813. goto done;
  1814. }
  1815. might_sleep();
  1816. wait_event_interruptible(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);
  1817. spin_lock_irqsave(&priv->driver_lock, flags);
  1818. ret = cmdnode->result;
  1819. if (ret)
  1820. lbs_pr_info("PREP_CMD: command 0x%04x failed: %d\n",
  1821. command, ret);
  1822. __lbs_cleanup_and_insert_cmd(priv, cmdnode);
  1823. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1824. done:
  1825. lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
  1826. return ret;
  1827. }
  1828. EXPORT_SYMBOL_GPL(__lbs_cmd);