cmd.c 50 KB

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