cmd.c 54 KB

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