cmd.c 53 KB

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