cmd.c 51 KB

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