cmd.c 55 KB

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