cmd.c 52 KB

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