cmd.c 51 KB

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