cmd.c 54 KB

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