cmd.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670
  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 <linux/kfifo.h>
  6. #include <linux/sched.h>
  7. #include <linux/slab.h>
  8. #include <linux/if_arp.h>
  9. #include "decl.h"
  10. #include "cfg.h"
  11. #include "cmd.h"
  12. #define CAL_NF(nf) ((s32)(-(s32)(nf)))
  13. #define CAL_RSSI(snr, nf) ((s32)((s32)(snr) + CAL_NF(nf)))
  14. /**
  15. * @brief Simple callback that copies response back into command
  16. *
  17. * @param priv A pointer to struct lbs_private structure
  18. * @param extra A pointer to the original command structure for which
  19. * 'resp' is a response
  20. * @param resp A pointer to the command response
  21. *
  22. * @return 0 on success, error on failure
  23. */
  24. int lbs_cmd_copyback(struct lbs_private *priv, unsigned long extra,
  25. struct cmd_header *resp)
  26. {
  27. struct cmd_header *buf = (void *)extra;
  28. uint16_t copy_len;
  29. copy_len = min(le16_to_cpu(buf->size), le16_to_cpu(resp->size));
  30. memcpy(buf, resp, copy_len);
  31. return 0;
  32. }
  33. EXPORT_SYMBOL_GPL(lbs_cmd_copyback);
  34. /**
  35. * @brief Simple callback that ignores the result. Use this if
  36. * you just want to send a command to the hardware, but don't
  37. * care for the result.
  38. *
  39. * @param priv ignored
  40. * @param extra ignored
  41. * @param resp ignored
  42. *
  43. * @return 0 for success
  44. */
  45. static int lbs_cmd_async_callback(struct lbs_private *priv, unsigned long extra,
  46. struct cmd_header *resp)
  47. {
  48. return 0;
  49. }
  50. /**
  51. * @brief Checks whether a command is allowed in Power Save mode
  52. *
  53. * @param command the command ID
  54. * @return 1 if allowed, 0 if not allowed
  55. */
  56. static u8 is_command_allowed_in_ps(u16 cmd)
  57. {
  58. switch (cmd) {
  59. case CMD_802_11_RSSI:
  60. return 1;
  61. case CMD_802_11_HOST_SLEEP_CFG:
  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. lbs_deb_enter(LBS_DEB_CMD);
  81. memset(&cmd, 0, sizeof(cmd));
  82. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  83. memcpy(cmd.permanentaddr, priv->current_addr, ETH_ALEN);
  84. ret = lbs_cmd_with_response(priv, CMD_GET_HW_SPEC, &cmd);
  85. if (ret)
  86. goto out;
  87. priv->fwcapinfo = le32_to_cpu(cmd.fwcapinfo);
  88. /* The firmware release is in an interesting format: the patch
  89. * level is in the most significant nibble ... so fix that: */
  90. priv->fwrelease = le32_to_cpu(cmd.fwrelease);
  91. priv->fwrelease = (priv->fwrelease << 8) |
  92. (priv->fwrelease >> 24 & 0xff);
  93. /* Some firmware capabilities:
  94. * CF card firmware 5.0.16p0: cap 0x00000303
  95. * USB dongle firmware 5.110.17p2: cap 0x00000303
  96. */
  97. lbs_pr_info("%pM, fw %u.%u.%up%u, cap 0x%08x\n",
  98. cmd.permanentaddr,
  99. priv->fwrelease >> 24 & 0xff,
  100. priv->fwrelease >> 16 & 0xff,
  101. priv->fwrelease >> 8 & 0xff,
  102. priv->fwrelease & 0xff,
  103. priv->fwcapinfo);
  104. lbs_deb_cmd("GET_HW_SPEC: hardware interface 0x%x, hardware spec 0x%04x\n",
  105. cmd.hwifversion, cmd.version);
  106. /* Clamp region code to 8-bit since FW spec indicates that it should
  107. * only ever be 8-bit, even though the field size is 16-bit. Some firmware
  108. * returns non-zero high 8 bits here.
  109. *
  110. * Firmware version 4.0.102 used in CF8381 has region code shifted. We
  111. * need to check for this problem and handle it properly.
  112. */
  113. if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V4)
  114. priv->regioncode = (le16_to_cpu(cmd.regioncode) >> 8) & 0xFF;
  115. else
  116. priv->regioncode = le16_to_cpu(cmd.regioncode) & 0xFF;
  117. for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
  118. /* use the region code to search for the index */
  119. if (priv->regioncode == lbs_region_code_to_index[i])
  120. break;
  121. }
  122. /* if it's unidentified region code, use the default (USA) */
  123. if (i >= MRVDRV_MAX_REGION_CODE) {
  124. priv->regioncode = 0x10;
  125. lbs_pr_info("unidentified region code; using the default (USA)\n");
  126. }
  127. if (priv->current_addr[0] == 0xff)
  128. memmove(priv->current_addr, cmd.permanentaddr, ETH_ALEN);
  129. if (!priv->copied_hwaddr) {
  130. memcpy(priv->dev->dev_addr, priv->current_addr, ETH_ALEN);
  131. if (priv->mesh_dev)
  132. memcpy(priv->mesh_dev->dev_addr,
  133. priv->current_addr, ETH_ALEN);
  134. priv->copied_hwaddr = 1;
  135. }
  136. out:
  137. lbs_deb_leave(LBS_DEB_CMD);
  138. return ret;
  139. }
  140. static int lbs_ret_host_sleep_cfg(struct lbs_private *priv, unsigned long dummy,
  141. struct cmd_header *resp)
  142. {
  143. lbs_deb_enter(LBS_DEB_CMD);
  144. if (priv->is_host_sleep_activated) {
  145. priv->is_host_sleep_configured = 0;
  146. if (priv->psstate == PS_STATE_FULL_POWER) {
  147. priv->is_host_sleep_activated = 0;
  148. wake_up_interruptible(&priv->host_sleep_q);
  149. }
  150. } else {
  151. priv->is_host_sleep_configured = 1;
  152. }
  153. lbs_deb_leave(LBS_DEB_CMD);
  154. return 0;
  155. }
  156. int lbs_host_sleep_cfg(struct lbs_private *priv, uint32_t criteria,
  157. struct wol_config *p_wol_config)
  158. {
  159. struct cmd_ds_host_sleep cmd_config;
  160. int ret;
  161. /*
  162. * Certain firmware versions do not support EHS_REMOVE_WAKEUP command
  163. * and the card will return a failure. Since we need to be
  164. * able to reset the mask, in those cases we set a 0 mask instead.
  165. */
  166. if (criteria == EHS_REMOVE_WAKEUP && !priv->ehs_remove_supported)
  167. criteria = 0;
  168. cmd_config.hdr.size = cpu_to_le16(sizeof(cmd_config));
  169. cmd_config.criteria = cpu_to_le32(criteria);
  170. cmd_config.gpio = priv->wol_gpio;
  171. cmd_config.gap = priv->wol_gap;
  172. if (p_wol_config != NULL)
  173. memcpy((uint8_t *)&cmd_config.wol_conf, (uint8_t *)p_wol_config,
  174. sizeof(struct wol_config));
  175. else
  176. cmd_config.wol_conf.action = CMD_ACT_ACTION_NONE;
  177. ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_CFG, &cmd_config.hdr,
  178. le16_to_cpu(cmd_config.hdr.size),
  179. lbs_ret_host_sleep_cfg, 0);
  180. if (!ret) {
  181. if (p_wol_config)
  182. memcpy((uint8_t *) p_wol_config,
  183. (uint8_t *)&cmd_config.wol_conf,
  184. sizeof(struct wol_config));
  185. } else {
  186. lbs_pr_info("HOST_SLEEP_CFG failed %d\n", ret);
  187. }
  188. return ret;
  189. }
  190. EXPORT_SYMBOL_GPL(lbs_host_sleep_cfg);
  191. /**
  192. * @brief Sets the Power Save mode
  193. *
  194. * @param priv A pointer to struct lbs_private structure
  195. * @param cmd_action The Power Save operation (PS_MODE_ACTION_ENTER_PS or
  196. * PS_MODE_ACTION_EXIT_PS)
  197. * @param block Whether to block on a response or not
  198. *
  199. * @return 0 on success, error on failure
  200. */
  201. int lbs_set_ps_mode(struct lbs_private *priv, u16 cmd_action, bool block)
  202. {
  203. struct cmd_ds_802_11_ps_mode cmd;
  204. int ret = 0;
  205. lbs_deb_enter(LBS_DEB_CMD);
  206. memset(&cmd, 0, sizeof(cmd));
  207. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  208. cmd.action = cpu_to_le16(cmd_action);
  209. if (cmd_action == PS_MODE_ACTION_ENTER_PS) {
  210. lbs_deb_cmd("PS_MODE: action ENTER_PS\n");
  211. cmd.multipledtim = cpu_to_le16(1); /* Default DTIM multiple */
  212. } else if (cmd_action == PS_MODE_ACTION_EXIT_PS) {
  213. lbs_deb_cmd("PS_MODE: action EXIT_PS\n");
  214. } else {
  215. /* We don't handle CONFIRM_SLEEP here because it needs to
  216. * be fastpathed to the firmware.
  217. */
  218. lbs_deb_cmd("PS_MODE: unknown action 0x%X\n", cmd_action);
  219. ret = -EOPNOTSUPP;
  220. goto out;
  221. }
  222. if (block)
  223. ret = lbs_cmd_with_response(priv, CMD_802_11_PS_MODE, &cmd);
  224. else
  225. lbs_cmd_async(priv, CMD_802_11_PS_MODE, &cmd.hdr, sizeof (cmd));
  226. out:
  227. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  228. return ret;
  229. }
  230. int lbs_cmd_802_11_sleep_params(struct lbs_private *priv, uint16_t cmd_action,
  231. struct sleep_params *sp)
  232. {
  233. struct cmd_ds_802_11_sleep_params cmd;
  234. int ret;
  235. lbs_deb_enter(LBS_DEB_CMD);
  236. if (cmd_action == CMD_ACT_GET) {
  237. memset(&cmd, 0, sizeof(cmd));
  238. } else {
  239. cmd.error = cpu_to_le16(sp->sp_error);
  240. cmd.offset = cpu_to_le16(sp->sp_offset);
  241. cmd.stabletime = cpu_to_le16(sp->sp_stabletime);
  242. cmd.calcontrol = sp->sp_calcontrol;
  243. cmd.externalsleepclk = sp->sp_extsleepclk;
  244. cmd.reserved = cpu_to_le16(sp->sp_reserved);
  245. }
  246. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  247. cmd.action = cpu_to_le16(cmd_action);
  248. ret = lbs_cmd_with_response(priv, CMD_802_11_SLEEP_PARAMS, &cmd);
  249. if (!ret) {
  250. lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, "
  251. "calcontrol 0x%x extsleepclk 0x%x\n",
  252. le16_to_cpu(cmd.error), le16_to_cpu(cmd.offset),
  253. le16_to_cpu(cmd.stabletime), cmd.calcontrol,
  254. cmd.externalsleepclk);
  255. sp->sp_error = le16_to_cpu(cmd.error);
  256. sp->sp_offset = le16_to_cpu(cmd.offset);
  257. sp->sp_stabletime = le16_to_cpu(cmd.stabletime);
  258. sp->sp_calcontrol = cmd.calcontrol;
  259. sp->sp_extsleepclk = cmd.externalsleepclk;
  260. sp->sp_reserved = le16_to_cpu(cmd.reserved);
  261. }
  262. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  263. return 0;
  264. }
  265. static int lbs_wait_for_ds_awake(struct lbs_private *priv)
  266. {
  267. int ret = 0;
  268. lbs_deb_enter(LBS_DEB_CMD);
  269. if (priv->is_deep_sleep) {
  270. if (!wait_event_interruptible_timeout(priv->ds_awake_q,
  271. !priv->is_deep_sleep, (10 * HZ))) {
  272. lbs_pr_err("ds_awake_q: timer expired\n");
  273. ret = -1;
  274. }
  275. }
  276. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  277. return ret;
  278. }
  279. int lbs_set_deep_sleep(struct lbs_private *priv, int deep_sleep)
  280. {
  281. int ret = 0;
  282. lbs_deb_enter(LBS_DEB_CMD);
  283. if (deep_sleep) {
  284. if (priv->is_deep_sleep != 1) {
  285. lbs_deb_cmd("deep sleep: sleep\n");
  286. BUG_ON(!priv->enter_deep_sleep);
  287. ret = priv->enter_deep_sleep(priv);
  288. if (!ret) {
  289. netif_stop_queue(priv->dev);
  290. netif_carrier_off(priv->dev);
  291. }
  292. } else {
  293. lbs_pr_err("deep sleep: already enabled\n");
  294. }
  295. } else {
  296. if (priv->is_deep_sleep) {
  297. lbs_deb_cmd("deep sleep: wakeup\n");
  298. BUG_ON(!priv->exit_deep_sleep);
  299. ret = priv->exit_deep_sleep(priv);
  300. if (!ret) {
  301. ret = lbs_wait_for_ds_awake(priv);
  302. if (ret)
  303. lbs_pr_err("deep sleep: wakeup"
  304. "failed\n");
  305. }
  306. }
  307. }
  308. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  309. return ret;
  310. }
  311. static int lbs_ret_host_sleep_activate(struct lbs_private *priv,
  312. unsigned long dummy,
  313. struct cmd_header *cmd)
  314. {
  315. lbs_deb_enter(LBS_DEB_FW);
  316. priv->is_host_sleep_activated = 1;
  317. wake_up_interruptible(&priv->host_sleep_q);
  318. lbs_deb_leave(LBS_DEB_FW);
  319. return 0;
  320. }
  321. int lbs_set_host_sleep(struct lbs_private *priv, int host_sleep)
  322. {
  323. struct cmd_header cmd;
  324. int ret = 0;
  325. uint32_t criteria = EHS_REMOVE_WAKEUP;
  326. lbs_deb_enter(LBS_DEB_CMD);
  327. if (host_sleep) {
  328. if (priv->is_host_sleep_activated != 1) {
  329. memset(&cmd, 0, sizeof(cmd));
  330. ret = lbs_host_sleep_cfg(priv, priv->wol_criteria,
  331. (struct wol_config *)NULL);
  332. if (ret) {
  333. lbs_pr_info("Host sleep configuration failed: "
  334. "%d\n", ret);
  335. return ret;
  336. }
  337. if (priv->psstate == PS_STATE_FULL_POWER) {
  338. ret = __lbs_cmd(priv,
  339. CMD_802_11_HOST_SLEEP_ACTIVATE,
  340. &cmd,
  341. sizeof(cmd),
  342. lbs_ret_host_sleep_activate, 0);
  343. if (ret)
  344. lbs_pr_info("HOST_SLEEP_ACTIVATE "
  345. "failed: %d\n", ret);
  346. }
  347. if (!wait_event_interruptible_timeout(
  348. priv->host_sleep_q,
  349. priv->is_host_sleep_activated,
  350. (10 * HZ))) {
  351. lbs_pr_err("host_sleep_q: timer expired\n");
  352. ret = -1;
  353. }
  354. } else {
  355. lbs_pr_err("host sleep: already enabled\n");
  356. }
  357. } else {
  358. if (priv->is_host_sleep_activated)
  359. ret = lbs_host_sleep_cfg(priv, criteria,
  360. (struct wol_config *)NULL);
  361. }
  362. return ret;
  363. }
  364. /**
  365. * @brief Set an SNMP MIB value
  366. *
  367. * @param priv A pointer to struct lbs_private structure
  368. * @param oid The OID to set in the firmware
  369. * @param val Value to set the OID to
  370. *
  371. * @return 0 on success, error on failure
  372. */
  373. int lbs_set_snmp_mib(struct lbs_private *priv, u32 oid, u16 val)
  374. {
  375. struct cmd_ds_802_11_snmp_mib cmd;
  376. int ret;
  377. lbs_deb_enter(LBS_DEB_CMD);
  378. memset(&cmd, 0, sizeof (cmd));
  379. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  380. cmd.action = cpu_to_le16(CMD_ACT_SET);
  381. cmd.oid = cpu_to_le16((u16) oid);
  382. switch (oid) {
  383. case SNMP_MIB_OID_BSS_TYPE:
  384. cmd.bufsize = cpu_to_le16(sizeof(u8));
  385. cmd.value[0] = val;
  386. break;
  387. case SNMP_MIB_OID_11D_ENABLE:
  388. case SNMP_MIB_OID_FRAG_THRESHOLD:
  389. case SNMP_MIB_OID_RTS_THRESHOLD:
  390. case SNMP_MIB_OID_SHORT_RETRY_LIMIT:
  391. case SNMP_MIB_OID_LONG_RETRY_LIMIT:
  392. cmd.bufsize = cpu_to_le16(sizeof(u16));
  393. *((__le16 *)(&cmd.value)) = cpu_to_le16(val);
  394. break;
  395. default:
  396. lbs_deb_cmd("SNMP_CMD: (set) unhandled OID 0x%x\n", oid);
  397. ret = -EINVAL;
  398. goto out;
  399. }
  400. lbs_deb_cmd("SNMP_CMD: (set) oid 0x%x, oid size 0x%x, value 0x%x\n",
  401. le16_to_cpu(cmd.oid), le16_to_cpu(cmd.bufsize), val);
  402. ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
  403. out:
  404. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  405. return ret;
  406. }
  407. /**
  408. * @brief Get an SNMP MIB value
  409. *
  410. * @param priv A pointer to struct lbs_private structure
  411. * @param oid The OID to retrieve from the firmware
  412. * @param out_val Location for the returned value
  413. *
  414. * @return 0 on success, error on failure
  415. */
  416. int lbs_get_snmp_mib(struct lbs_private *priv, u32 oid, u16 *out_val)
  417. {
  418. struct cmd_ds_802_11_snmp_mib cmd;
  419. int ret;
  420. lbs_deb_enter(LBS_DEB_CMD);
  421. memset(&cmd, 0, sizeof (cmd));
  422. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  423. cmd.action = cpu_to_le16(CMD_ACT_GET);
  424. cmd.oid = cpu_to_le16(oid);
  425. ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
  426. if (ret)
  427. goto out;
  428. switch (le16_to_cpu(cmd.bufsize)) {
  429. case sizeof(u8):
  430. *out_val = cmd.value[0];
  431. break;
  432. case sizeof(u16):
  433. *out_val = le16_to_cpu(*((__le16 *)(&cmd.value)));
  434. break;
  435. default:
  436. lbs_deb_cmd("SNMP_CMD: (get) unhandled OID 0x%x size %d\n",
  437. oid, le16_to_cpu(cmd.bufsize));
  438. break;
  439. }
  440. out:
  441. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  442. return ret;
  443. }
  444. /**
  445. * @brief Get the min, max, and current TX power
  446. *
  447. * @param priv A pointer to struct lbs_private structure
  448. * @param curlevel Current power level in dBm
  449. * @param minlevel Minimum supported power level in dBm (optional)
  450. * @param maxlevel Maximum supported power level in dBm (optional)
  451. *
  452. * @return 0 on success, error on failure
  453. */
  454. int lbs_get_tx_power(struct lbs_private *priv, s16 *curlevel, s16 *minlevel,
  455. s16 *maxlevel)
  456. {
  457. struct cmd_ds_802_11_rf_tx_power cmd;
  458. int ret;
  459. lbs_deb_enter(LBS_DEB_CMD);
  460. memset(&cmd, 0, sizeof(cmd));
  461. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  462. cmd.action = cpu_to_le16(CMD_ACT_GET);
  463. ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
  464. if (ret == 0) {
  465. *curlevel = le16_to_cpu(cmd.curlevel);
  466. if (minlevel)
  467. *minlevel = cmd.minlevel;
  468. if (maxlevel)
  469. *maxlevel = cmd.maxlevel;
  470. }
  471. lbs_deb_leave(LBS_DEB_CMD);
  472. return ret;
  473. }
  474. /**
  475. * @brief Set the TX power
  476. *
  477. * @param priv A pointer to struct lbs_private structure
  478. * @param dbm The desired power level in dBm
  479. *
  480. * @return 0 on success, error on failure
  481. */
  482. int lbs_set_tx_power(struct lbs_private *priv, s16 dbm)
  483. {
  484. struct cmd_ds_802_11_rf_tx_power cmd;
  485. int ret;
  486. lbs_deb_enter(LBS_DEB_CMD);
  487. memset(&cmd, 0, sizeof(cmd));
  488. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  489. cmd.action = cpu_to_le16(CMD_ACT_SET);
  490. cmd.curlevel = cpu_to_le16(dbm);
  491. lbs_deb_cmd("SET_RF_TX_POWER: %d dBm\n", dbm);
  492. ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
  493. lbs_deb_leave(LBS_DEB_CMD);
  494. return ret;
  495. }
  496. /**
  497. * @brief Enable or disable monitor mode (only implemented on OLPC usb8388 FW)
  498. *
  499. * @param priv A pointer to struct lbs_private structure
  500. * @param enable 1 to enable monitor mode, 0 to disable
  501. *
  502. * @return 0 on success, error on failure
  503. */
  504. int lbs_set_monitor_mode(struct lbs_private *priv, int enable)
  505. {
  506. struct cmd_ds_802_11_monitor_mode cmd;
  507. int ret;
  508. memset(&cmd, 0, sizeof(cmd));
  509. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  510. cmd.action = cpu_to_le16(CMD_ACT_SET);
  511. if (enable)
  512. cmd.mode = cpu_to_le16(0x1);
  513. lbs_deb_cmd("SET_MONITOR_MODE: %d\n", enable);
  514. ret = lbs_cmd_with_response(priv, CMD_802_11_MONITOR_MODE, &cmd);
  515. if (ret == 0) {
  516. priv->dev->type = enable ? ARPHRD_IEEE80211_RADIOTAP :
  517. ARPHRD_ETHER;
  518. }
  519. lbs_deb_leave(LBS_DEB_CMD);
  520. return ret;
  521. }
  522. /**
  523. * @brief Get the radio channel
  524. *
  525. * @param priv A pointer to struct lbs_private structure
  526. *
  527. * @return The channel on success, error on failure
  528. */
  529. static int lbs_get_channel(struct lbs_private *priv)
  530. {
  531. struct cmd_ds_802_11_rf_channel cmd;
  532. int ret = 0;
  533. lbs_deb_enter(LBS_DEB_CMD);
  534. memset(&cmd, 0, sizeof(cmd));
  535. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  536. cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_GET);
  537. ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
  538. if (ret)
  539. goto out;
  540. ret = le16_to_cpu(cmd.channel);
  541. lbs_deb_cmd("current radio channel is %d\n", ret);
  542. out:
  543. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  544. return ret;
  545. }
  546. int lbs_update_channel(struct lbs_private *priv)
  547. {
  548. int ret;
  549. /* the channel in f/w could be out of sync; get the current channel */
  550. lbs_deb_enter(LBS_DEB_ASSOC);
  551. ret = lbs_get_channel(priv);
  552. if (ret > 0) {
  553. priv->channel = ret;
  554. ret = 0;
  555. }
  556. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  557. return ret;
  558. }
  559. /**
  560. * @brief Set the radio channel
  561. *
  562. * @param priv A pointer to struct lbs_private structure
  563. * @param channel The desired channel, or 0 to clear a locked channel
  564. *
  565. * @return 0 on success, error on failure
  566. */
  567. int lbs_set_channel(struct lbs_private *priv, u8 channel)
  568. {
  569. struct cmd_ds_802_11_rf_channel cmd;
  570. #ifdef DEBUG
  571. u8 old_channel = priv->channel;
  572. #endif
  573. int ret = 0;
  574. lbs_deb_enter(LBS_DEB_CMD);
  575. memset(&cmd, 0, sizeof(cmd));
  576. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  577. cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
  578. cmd.channel = cpu_to_le16(channel);
  579. ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
  580. if (ret)
  581. goto out;
  582. priv->channel = (uint8_t) le16_to_cpu(cmd.channel);
  583. lbs_deb_cmd("channel switch from %d to %d\n", old_channel,
  584. priv->channel);
  585. out:
  586. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  587. return ret;
  588. }
  589. /**
  590. * @brief Get current RSSI and noise floor
  591. *
  592. * @param priv A pointer to struct lbs_private structure
  593. * @param rssi On successful return, signal level in mBm
  594. *
  595. * @return The channel on success, error on failure
  596. */
  597. int lbs_get_rssi(struct lbs_private *priv, s8 *rssi, s8 *nf)
  598. {
  599. struct cmd_ds_802_11_rssi cmd;
  600. int ret = 0;
  601. lbs_deb_enter(LBS_DEB_CMD);
  602. BUG_ON(rssi == NULL);
  603. BUG_ON(nf == NULL);
  604. memset(&cmd, 0, sizeof(cmd));
  605. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  606. /* Average SNR over last 8 beacons */
  607. cmd.n_or_snr = cpu_to_le16(8);
  608. ret = lbs_cmd_with_response(priv, CMD_802_11_RSSI, &cmd);
  609. if (ret == 0) {
  610. *nf = CAL_NF(le16_to_cpu(cmd.nf));
  611. *rssi = CAL_RSSI(le16_to_cpu(cmd.n_or_snr), le16_to_cpu(cmd.nf));
  612. }
  613. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  614. return ret;
  615. }
  616. /**
  617. * @brief Send regulatory and 802.11d domain information to the firmware
  618. *
  619. * @param priv pointer to struct lbs_private
  620. * @param request cfg80211 regulatory request structure
  621. * @param bands the device's supported bands and channels
  622. *
  623. * @return 0 on success, error code on failure
  624. */
  625. int lbs_set_11d_domain_info(struct lbs_private *priv,
  626. struct regulatory_request *request,
  627. struct ieee80211_supported_band **bands)
  628. {
  629. struct cmd_ds_802_11d_domain_info cmd;
  630. struct mrvl_ie_domain_param_set *domain = &cmd.domain;
  631. struct ieee80211_country_ie_triplet *t;
  632. enum ieee80211_band band;
  633. struct ieee80211_channel *ch;
  634. u8 num_triplet = 0;
  635. u8 num_parsed_chan = 0;
  636. u8 first_channel = 0, next_chan = 0, max_pwr = 0;
  637. u8 i, flag = 0;
  638. size_t triplet_size;
  639. int ret;
  640. lbs_deb_enter(LBS_DEB_11D);
  641. memset(&cmd, 0, sizeof(cmd));
  642. cmd.action = cpu_to_le16(CMD_ACT_SET);
  643. lbs_deb_11d("Setting country code '%c%c'\n",
  644. request->alpha2[0], request->alpha2[1]);
  645. domain->header.type = cpu_to_le16(TLV_TYPE_DOMAIN);
  646. /* Set country code */
  647. domain->country_code[0] = request->alpha2[0];
  648. domain->country_code[1] = request->alpha2[1];
  649. domain->country_code[2] = ' ';
  650. /* Now set up the channel triplets; firmware is somewhat picky here
  651. * and doesn't validate channel numbers and spans; hence it would
  652. * interpret a triplet of (36, 4, 20) as channels 36, 37, 38, 39. Since
  653. * the last 3 aren't valid channels, the driver is responsible for
  654. * splitting that up into 4 triplet pairs of (36, 1, 20) + (40, 1, 20)
  655. * etc.
  656. */
  657. for (band = 0;
  658. (band < IEEE80211_NUM_BANDS) && (num_triplet < MAX_11D_TRIPLETS);
  659. band++) {
  660. if (!bands[band])
  661. continue;
  662. for (i = 0;
  663. (i < bands[band]->n_channels) && (num_triplet < MAX_11D_TRIPLETS);
  664. i++) {
  665. ch = &bands[band]->channels[i];
  666. if (ch->flags & IEEE80211_CHAN_DISABLED)
  667. continue;
  668. if (!flag) {
  669. flag = 1;
  670. next_chan = first_channel = (u32) ch->hw_value;
  671. max_pwr = ch->max_power;
  672. num_parsed_chan = 1;
  673. continue;
  674. }
  675. if ((ch->hw_value == next_chan + 1) &&
  676. (ch->max_power == max_pwr)) {
  677. /* Consolidate adjacent channels */
  678. next_chan++;
  679. num_parsed_chan++;
  680. } else {
  681. /* Add this triplet */
  682. lbs_deb_11d("11D triplet (%d, %d, %d)\n",
  683. first_channel, num_parsed_chan,
  684. max_pwr);
  685. t = &domain->triplet[num_triplet];
  686. t->chans.first_channel = first_channel;
  687. t->chans.num_channels = num_parsed_chan;
  688. t->chans.max_power = max_pwr;
  689. num_triplet++;
  690. flag = 0;
  691. }
  692. }
  693. if (flag) {
  694. /* Add last triplet */
  695. lbs_deb_11d("11D triplet (%d, %d, %d)\n", first_channel,
  696. num_parsed_chan, max_pwr);
  697. t = &domain->triplet[num_triplet];
  698. t->chans.first_channel = first_channel;
  699. t->chans.num_channels = num_parsed_chan;
  700. t->chans.max_power = max_pwr;
  701. num_triplet++;
  702. }
  703. }
  704. lbs_deb_11d("# triplets %d\n", num_triplet);
  705. /* Set command header sizes */
  706. triplet_size = num_triplet * sizeof(struct ieee80211_country_ie_triplet);
  707. domain->header.len = cpu_to_le16(sizeof(domain->country_code) +
  708. triplet_size);
  709. lbs_deb_hex(LBS_DEB_11D, "802.11D domain param set",
  710. (u8 *) &cmd.domain.country_code,
  711. le16_to_cpu(domain->header.len));
  712. cmd.hdr.size = cpu_to_le16(sizeof(cmd.hdr) +
  713. sizeof(cmd.action) +
  714. sizeof(cmd.domain.header) +
  715. sizeof(cmd.domain.country_code) +
  716. triplet_size);
  717. ret = lbs_cmd_with_response(priv, CMD_802_11D_DOMAIN_INFO, &cmd);
  718. lbs_deb_leave_args(LBS_DEB_11D, "ret %d", ret);
  719. return ret;
  720. }
  721. /**
  722. * @brief Read a MAC, Baseband, or RF register
  723. *
  724. * @param priv pointer to struct lbs_private
  725. * @param cmd register command, one of CMD_MAC_REG_ACCESS,
  726. * CMD_BBP_REG_ACCESS, or CMD_RF_REG_ACCESS
  727. * @param offset byte offset of the register to get
  728. * @param value on success, the value of the register at 'offset'
  729. *
  730. * @return 0 on success, error code on failure
  731. */
  732. int lbs_get_reg(struct lbs_private *priv, u16 reg, u16 offset, u32 *value)
  733. {
  734. struct cmd_ds_reg_access cmd;
  735. int ret = 0;
  736. lbs_deb_enter(LBS_DEB_CMD);
  737. BUG_ON(value == NULL);
  738. memset(&cmd, 0, sizeof(cmd));
  739. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  740. cmd.action = cpu_to_le16(CMD_ACT_GET);
  741. if (reg != CMD_MAC_REG_ACCESS &&
  742. reg != CMD_BBP_REG_ACCESS &&
  743. reg != CMD_RF_REG_ACCESS) {
  744. ret = -EINVAL;
  745. goto out;
  746. }
  747. ret = lbs_cmd_with_response(priv, reg, &cmd);
  748. if (ret) {
  749. if (reg == CMD_BBP_REG_ACCESS || reg == CMD_RF_REG_ACCESS)
  750. *value = cmd.value.bbp_rf;
  751. else if (reg == CMD_MAC_REG_ACCESS)
  752. *value = le32_to_cpu(cmd.value.mac);
  753. }
  754. out:
  755. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  756. return ret;
  757. }
  758. /**
  759. * @brief Write a MAC, Baseband, or RF register
  760. *
  761. * @param priv pointer to struct lbs_private
  762. * @param cmd register command, one of CMD_MAC_REG_ACCESS,
  763. * CMD_BBP_REG_ACCESS, or CMD_RF_REG_ACCESS
  764. * @param offset byte offset of the register to set
  765. * @param value the value to write to the register at 'offset'
  766. *
  767. * @return 0 on success, error code on failure
  768. */
  769. int lbs_set_reg(struct lbs_private *priv, u16 reg, u16 offset, u32 value)
  770. {
  771. struct cmd_ds_reg_access cmd;
  772. int ret = 0;
  773. lbs_deb_enter(LBS_DEB_CMD);
  774. memset(&cmd, 0, sizeof(cmd));
  775. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  776. cmd.action = cpu_to_le16(CMD_ACT_SET);
  777. if (reg == CMD_BBP_REG_ACCESS || reg == CMD_RF_REG_ACCESS)
  778. cmd.value.bbp_rf = (u8) (value & 0xFF);
  779. else if (reg == CMD_MAC_REG_ACCESS)
  780. cmd.value.mac = cpu_to_le32(value);
  781. else {
  782. ret = -EINVAL;
  783. goto out;
  784. }
  785. ret = lbs_cmd_with_response(priv, reg, &cmd);
  786. out:
  787. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  788. return ret;
  789. }
  790. static void lbs_queue_cmd(struct lbs_private *priv,
  791. struct cmd_ctrl_node *cmdnode)
  792. {
  793. unsigned long flags;
  794. int addtail = 1;
  795. lbs_deb_enter(LBS_DEB_HOST);
  796. if (!cmdnode) {
  797. lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
  798. goto done;
  799. }
  800. if (!cmdnode->cmdbuf->size) {
  801. lbs_deb_host("DNLD_CMD: cmd size is zero\n");
  802. goto done;
  803. }
  804. cmdnode->result = 0;
  805. /* Exit_PS command needs to be queued in the header always. */
  806. if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_PS_MODE) {
  807. struct cmd_ds_802_11_ps_mode *psm = (void *) &cmdnode->cmdbuf;
  808. if (psm->action == cpu_to_le16(PS_MODE_ACTION_EXIT_PS)) {
  809. if (priv->psstate != PS_STATE_FULL_POWER)
  810. addtail = 0;
  811. }
  812. }
  813. if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_WAKEUP_CONFIRM)
  814. addtail = 0;
  815. spin_lock_irqsave(&priv->driver_lock, flags);
  816. if (addtail)
  817. list_add_tail(&cmdnode->list, &priv->cmdpendingq);
  818. else
  819. list_add(&cmdnode->list, &priv->cmdpendingq);
  820. spin_unlock_irqrestore(&priv->driver_lock, flags);
  821. lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
  822. le16_to_cpu(cmdnode->cmdbuf->command));
  823. done:
  824. lbs_deb_leave(LBS_DEB_HOST);
  825. }
  826. static void lbs_submit_command(struct lbs_private *priv,
  827. struct cmd_ctrl_node *cmdnode)
  828. {
  829. unsigned long flags;
  830. struct cmd_header *cmd;
  831. uint16_t cmdsize;
  832. uint16_t command;
  833. int timeo = 3 * HZ;
  834. int ret;
  835. lbs_deb_enter(LBS_DEB_HOST);
  836. cmd = cmdnode->cmdbuf;
  837. spin_lock_irqsave(&priv->driver_lock, flags);
  838. priv->cur_cmd = cmdnode;
  839. spin_unlock_irqrestore(&priv->driver_lock, flags);
  840. cmdsize = le16_to_cpu(cmd->size);
  841. command = le16_to_cpu(cmd->command);
  842. /* These commands take longer */
  843. if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE)
  844. timeo = 5 * HZ;
  845. lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
  846. command, le16_to_cpu(cmd->seqnum), cmdsize);
  847. lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
  848. ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
  849. if (ret) {
  850. lbs_pr_info("DNLD_CMD: hw_host_to_card failed: %d\n", ret);
  851. /* Let the timer kick in and retry, and potentially reset
  852. the whole thing if the condition persists */
  853. timeo = HZ/4;
  854. }
  855. if (command == CMD_802_11_DEEP_SLEEP) {
  856. if (priv->is_auto_deep_sleep_enabled) {
  857. priv->wakeup_dev_required = 1;
  858. priv->dnld_sent = 0;
  859. }
  860. priv->is_deep_sleep = 1;
  861. lbs_complete_command(priv, cmdnode, 0);
  862. } else {
  863. /* Setup the timer after transmit command */
  864. mod_timer(&priv->command_timer, jiffies + timeo);
  865. }
  866. lbs_deb_leave(LBS_DEB_HOST);
  867. }
  868. /**
  869. * This function inserts command node to cmdfreeq
  870. * after cleans it. Requires priv->driver_lock held.
  871. */
  872. static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
  873. struct cmd_ctrl_node *cmdnode)
  874. {
  875. lbs_deb_enter(LBS_DEB_HOST);
  876. if (!cmdnode)
  877. goto out;
  878. cmdnode->callback = NULL;
  879. cmdnode->callback_arg = 0;
  880. memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
  881. list_add_tail(&cmdnode->list, &priv->cmdfreeq);
  882. out:
  883. lbs_deb_leave(LBS_DEB_HOST);
  884. }
  885. static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
  886. struct cmd_ctrl_node *ptempcmd)
  887. {
  888. unsigned long flags;
  889. spin_lock_irqsave(&priv->driver_lock, flags);
  890. __lbs_cleanup_and_insert_cmd(priv, ptempcmd);
  891. spin_unlock_irqrestore(&priv->driver_lock, flags);
  892. }
  893. void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
  894. int result)
  895. {
  896. cmd->result = result;
  897. cmd->cmdwaitqwoken = 1;
  898. wake_up_interruptible(&cmd->cmdwait_q);
  899. if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
  900. __lbs_cleanup_and_insert_cmd(priv, cmd);
  901. priv->cur_cmd = NULL;
  902. }
  903. int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
  904. {
  905. struct cmd_ds_802_11_radio_control cmd;
  906. int ret = -EINVAL;
  907. lbs_deb_enter(LBS_DEB_CMD);
  908. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  909. cmd.action = cpu_to_le16(CMD_ACT_SET);
  910. /* Only v8 and below support setting the preamble */
  911. if (priv->fwrelease < 0x09000000) {
  912. switch (preamble) {
  913. case RADIO_PREAMBLE_SHORT:
  914. case RADIO_PREAMBLE_AUTO:
  915. case RADIO_PREAMBLE_LONG:
  916. cmd.control = cpu_to_le16(preamble);
  917. break;
  918. default:
  919. goto out;
  920. }
  921. }
  922. if (radio_on)
  923. cmd.control |= cpu_to_le16(0x1);
  924. else {
  925. cmd.control &= cpu_to_le16(~0x1);
  926. priv->txpower_cur = 0;
  927. }
  928. lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
  929. radio_on ? "ON" : "OFF", preamble);
  930. priv->radio_on = radio_on;
  931. ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
  932. out:
  933. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  934. return ret;
  935. }
  936. void lbs_set_mac_control(struct lbs_private *priv)
  937. {
  938. struct cmd_ds_mac_control cmd;
  939. lbs_deb_enter(LBS_DEB_CMD);
  940. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  941. cmd.action = cpu_to_le16(priv->mac_control);
  942. cmd.reserved = 0;
  943. lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
  944. lbs_deb_leave(LBS_DEB_CMD);
  945. }
  946. /**
  947. * @brief This function allocates the command buffer and link
  948. * it to command free queue.
  949. *
  950. * @param priv A pointer to struct lbs_private structure
  951. * @return 0 or -1
  952. */
  953. int lbs_allocate_cmd_buffer(struct lbs_private *priv)
  954. {
  955. int ret = 0;
  956. u32 bufsize;
  957. u32 i;
  958. struct cmd_ctrl_node *cmdarray;
  959. lbs_deb_enter(LBS_DEB_HOST);
  960. /* Allocate and initialize the command array */
  961. bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
  962. if (!(cmdarray = kzalloc(bufsize, GFP_KERNEL))) {
  963. lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
  964. ret = -1;
  965. goto done;
  966. }
  967. priv->cmd_array = cmdarray;
  968. /* Allocate and initialize each command buffer in the command array */
  969. for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
  970. cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
  971. if (!cmdarray[i].cmdbuf) {
  972. lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
  973. ret = -1;
  974. goto done;
  975. }
  976. }
  977. for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
  978. init_waitqueue_head(&cmdarray[i].cmdwait_q);
  979. lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
  980. }
  981. ret = 0;
  982. done:
  983. lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
  984. return ret;
  985. }
  986. /**
  987. * @brief This function frees the command buffer.
  988. *
  989. * @param priv A pointer to struct lbs_private structure
  990. * @return 0 or -1
  991. */
  992. int lbs_free_cmd_buffer(struct lbs_private *priv)
  993. {
  994. struct cmd_ctrl_node *cmdarray;
  995. unsigned int i;
  996. lbs_deb_enter(LBS_DEB_HOST);
  997. /* need to check if cmd array is allocated or not */
  998. if (priv->cmd_array == NULL) {
  999. lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
  1000. goto done;
  1001. }
  1002. cmdarray = priv->cmd_array;
  1003. /* Release shared memory buffers */
  1004. for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
  1005. if (cmdarray[i].cmdbuf) {
  1006. kfree(cmdarray[i].cmdbuf);
  1007. cmdarray[i].cmdbuf = NULL;
  1008. }
  1009. }
  1010. /* Release cmd_ctrl_node */
  1011. if (priv->cmd_array) {
  1012. kfree(priv->cmd_array);
  1013. priv->cmd_array = NULL;
  1014. }
  1015. done:
  1016. lbs_deb_leave(LBS_DEB_HOST);
  1017. return 0;
  1018. }
  1019. /**
  1020. * @brief This function gets a free command node if available in
  1021. * command free queue.
  1022. *
  1023. * @param priv A pointer to struct lbs_private structure
  1024. * @return cmd_ctrl_node A pointer to cmd_ctrl_node structure or NULL
  1025. */
  1026. static struct cmd_ctrl_node *lbs_get_free_cmd_node(struct lbs_private *priv)
  1027. {
  1028. struct cmd_ctrl_node *tempnode;
  1029. unsigned long flags;
  1030. lbs_deb_enter(LBS_DEB_HOST);
  1031. if (!priv)
  1032. return NULL;
  1033. spin_lock_irqsave(&priv->driver_lock, flags);
  1034. if (!list_empty(&priv->cmdfreeq)) {
  1035. tempnode = list_first_entry(&priv->cmdfreeq,
  1036. struct cmd_ctrl_node, list);
  1037. list_del(&tempnode->list);
  1038. } else {
  1039. lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
  1040. tempnode = NULL;
  1041. }
  1042. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1043. lbs_deb_leave(LBS_DEB_HOST);
  1044. return tempnode;
  1045. }
  1046. /**
  1047. * @brief This function executes next command in command
  1048. * pending queue. It will put firmware back to PS mode
  1049. * if applicable.
  1050. *
  1051. * @param priv A pointer to struct lbs_private structure
  1052. * @return 0 or -1
  1053. */
  1054. int lbs_execute_next_command(struct lbs_private *priv)
  1055. {
  1056. struct cmd_ctrl_node *cmdnode = NULL;
  1057. struct cmd_header *cmd;
  1058. unsigned long flags;
  1059. int ret = 0;
  1060. /* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
  1061. * only caller to us is lbs_thread() and we get even when a
  1062. * data packet is received */
  1063. lbs_deb_enter(LBS_DEB_THREAD);
  1064. spin_lock_irqsave(&priv->driver_lock, flags);
  1065. if (priv->cur_cmd) {
  1066. lbs_pr_alert( "EXEC_NEXT_CMD: already processing command!\n");
  1067. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1068. ret = -1;
  1069. goto done;
  1070. }
  1071. if (!list_empty(&priv->cmdpendingq)) {
  1072. cmdnode = list_first_entry(&priv->cmdpendingq,
  1073. struct cmd_ctrl_node, list);
  1074. }
  1075. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1076. if (cmdnode) {
  1077. cmd = cmdnode->cmdbuf;
  1078. if (is_command_allowed_in_ps(le16_to_cpu(cmd->command))) {
  1079. if ((priv->psstate == PS_STATE_SLEEP) ||
  1080. (priv->psstate == PS_STATE_PRE_SLEEP)) {
  1081. lbs_deb_host(
  1082. "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
  1083. le16_to_cpu(cmd->command),
  1084. priv->psstate);
  1085. ret = -1;
  1086. goto done;
  1087. }
  1088. lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
  1089. "0x%04x in psstate %d\n",
  1090. le16_to_cpu(cmd->command), priv->psstate);
  1091. } else if (priv->psstate != PS_STATE_FULL_POWER) {
  1092. /*
  1093. * 1. Non-PS command:
  1094. * Queue it. set needtowakeup to TRUE if current state
  1095. * is SLEEP, otherwise call send EXIT_PS.
  1096. * 2. PS command but not EXIT_PS:
  1097. * Ignore it.
  1098. * 3. PS command EXIT_PS:
  1099. * Set needtowakeup to TRUE if current state is SLEEP,
  1100. * otherwise send this command down to firmware
  1101. * immediately.
  1102. */
  1103. if (cmd->command != cpu_to_le16(CMD_802_11_PS_MODE)) {
  1104. /* Prepare to send Exit PS,
  1105. * this non PS command will be sent later */
  1106. if ((priv->psstate == PS_STATE_SLEEP)
  1107. || (priv->psstate == PS_STATE_PRE_SLEEP)
  1108. ) {
  1109. /* w/ new scheme, it will not reach here.
  1110. since it is blocked in main_thread. */
  1111. priv->needtowakeup = 1;
  1112. } else {
  1113. lbs_set_ps_mode(priv,
  1114. PS_MODE_ACTION_EXIT_PS,
  1115. false);
  1116. }
  1117. ret = 0;
  1118. goto done;
  1119. } else {
  1120. /*
  1121. * PS command. Ignore it if it is not Exit_PS.
  1122. * otherwise send it down immediately.
  1123. */
  1124. struct cmd_ds_802_11_ps_mode *psm = (void *)&cmd[1];
  1125. lbs_deb_host(
  1126. "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
  1127. psm->action);
  1128. if (psm->action !=
  1129. cpu_to_le16(PS_MODE_ACTION_EXIT_PS)) {
  1130. lbs_deb_host(
  1131. "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
  1132. spin_lock_irqsave(&priv->driver_lock, flags);
  1133. list_del(&cmdnode->list);
  1134. lbs_complete_command(priv, cmdnode, 0);
  1135. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1136. ret = 0;
  1137. goto done;
  1138. }
  1139. if ((priv->psstate == PS_STATE_SLEEP) ||
  1140. (priv->psstate == PS_STATE_PRE_SLEEP)) {
  1141. lbs_deb_host(
  1142. "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
  1143. spin_lock_irqsave(&priv->driver_lock, flags);
  1144. list_del(&cmdnode->list);
  1145. lbs_complete_command(priv, cmdnode, 0);
  1146. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1147. priv->needtowakeup = 1;
  1148. ret = 0;
  1149. goto done;
  1150. }
  1151. lbs_deb_host(
  1152. "EXEC_NEXT_CMD: sending EXIT_PS\n");
  1153. }
  1154. }
  1155. spin_lock_irqsave(&priv->driver_lock, flags);
  1156. list_del(&cmdnode->list);
  1157. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1158. lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
  1159. le16_to_cpu(cmd->command));
  1160. lbs_submit_command(priv, cmdnode);
  1161. } else {
  1162. /*
  1163. * check if in power save mode, if yes, put the device back
  1164. * to PS mode
  1165. */
  1166. #ifdef TODO
  1167. /*
  1168. * This was the old code for libertas+wext. Someone that
  1169. * understands this beast should re-code it in a sane way.
  1170. *
  1171. * I actually don't understand why this is related to WPA
  1172. * and to connection status, shouldn't powering should be
  1173. * independ of such things?
  1174. */
  1175. if ((priv->psmode != LBS802_11POWERMODECAM) &&
  1176. (priv->psstate == PS_STATE_FULL_POWER) &&
  1177. ((priv->connect_status == LBS_CONNECTED) ||
  1178. lbs_mesh_connected(priv))) {
  1179. if (priv->secinfo.WPAenabled ||
  1180. priv->secinfo.WPA2enabled) {
  1181. /* check for valid WPA group keys */
  1182. if (priv->wpa_mcast_key.len ||
  1183. priv->wpa_unicast_key.len) {
  1184. lbs_deb_host(
  1185. "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
  1186. " go back to PS_SLEEP");
  1187. lbs_set_ps_mode(priv,
  1188. PS_MODE_ACTION_ENTER_PS,
  1189. false);
  1190. }
  1191. } else {
  1192. lbs_deb_host(
  1193. "EXEC_NEXT_CMD: cmdpendingq empty, "
  1194. "go back to PS_SLEEP");
  1195. lbs_set_ps_mode(priv, PS_MODE_ACTION_ENTER_PS,
  1196. false);
  1197. }
  1198. }
  1199. #endif
  1200. }
  1201. ret = 0;
  1202. done:
  1203. lbs_deb_leave(LBS_DEB_THREAD);
  1204. return ret;
  1205. }
  1206. static void lbs_send_confirmsleep(struct lbs_private *priv)
  1207. {
  1208. unsigned long flags;
  1209. int ret;
  1210. lbs_deb_enter(LBS_DEB_HOST);
  1211. lbs_deb_hex(LBS_DEB_HOST, "sleep confirm", (u8 *) &confirm_sleep,
  1212. sizeof(confirm_sleep));
  1213. ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &confirm_sleep,
  1214. sizeof(confirm_sleep));
  1215. if (ret) {
  1216. lbs_pr_alert("confirm_sleep failed\n");
  1217. goto out;
  1218. }
  1219. spin_lock_irqsave(&priv->driver_lock, flags);
  1220. /* We don't get a response on the sleep-confirmation */
  1221. priv->dnld_sent = DNLD_RES_RECEIVED;
  1222. if (priv->is_host_sleep_configured) {
  1223. priv->is_host_sleep_activated = 1;
  1224. wake_up_interruptible(&priv->host_sleep_q);
  1225. }
  1226. /* If nothing to do, go back to sleep (?) */
  1227. if (!kfifo_len(&priv->event_fifo) && !priv->resp_len[priv->resp_idx])
  1228. priv->psstate = PS_STATE_SLEEP;
  1229. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1230. out:
  1231. lbs_deb_leave(LBS_DEB_HOST);
  1232. }
  1233. /**
  1234. * @brief This function checks condition and prepares to
  1235. * send sleep confirm command to firmware if ok.
  1236. *
  1237. * @param priv A pointer to struct lbs_private structure
  1238. * @param psmode Power Saving mode
  1239. * @return n/a
  1240. */
  1241. void lbs_ps_confirm_sleep(struct lbs_private *priv)
  1242. {
  1243. unsigned long flags =0;
  1244. int allowed = 1;
  1245. lbs_deb_enter(LBS_DEB_HOST);
  1246. spin_lock_irqsave(&priv->driver_lock, flags);
  1247. if (priv->dnld_sent) {
  1248. allowed = 0;
  1249. lbs_deb_host("dnld_sent was set\n");
  1250. }
  1251. /* In-progress command? */
  1252. if (priv->cur_cmd) {
  1253. allowed = 0;
  1254. lbs_deb_host("cur_cmd was set\n");
  1255. }
  1256. /* Pending events or command responses? */
  1257. if (kfifo_len(&priv->event_fifo) || priv->resp_len[priv->resp_idx]) {
  1258. allowed = 0;
  1259. lbs_deb_host("pending events or command responses\n");
  1260. }
  1261. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1262. if (allowed) {
  1263. lbs_deb_host("sending lbs_ps_confirm_sleep\n");
  1264. lbs_send_confirmsleep(priv);
  1265. } else {
  1266. lbs_deb_host("sleep confirm has been delayed\n");
  1267. }
  1268. lbs_deb_leave(LBS_DEB_HOST);
  1269. }
  1270. /**
  1271. * @brief Configures the transmission power control functionality.
  1272. *
  1273. * @param priv A pointer to struct lbs_private structure
  1274. * @param enable Transmission power control enable
  1275. * @param p0 Power level when link quality is good (dBm).
  1276. * @param p1 Power level when link quality is fair (dBm).
  1277. * @param p2 Power level when link quality is poor (dBm).
  1278. * @param usesnr Use Signal to Noise Ratio in TPC
  1279. *
  1280. * @return 0 on success
  1281. */
  1282. int lbs_set_tpc_cfg(struct lbs_private *priv, int enable, int8_t p0, int8_t p1,
  1283. int8_t p2, int usesnr)
  1284. {
  1285. struct cmd_ds_802_11_tpc_cfg cmd;
  1286. int ret;
  1287. memset(&cmd, 0, sizeof(cmd));
  1288. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1289. cmd.action = cpu_to_le16(CMD_ACT_SET);
  1290. cmd.enable = !!enable;
  1291. cmd.usesnr = !!usesnr;
  1292. cmd.P0 = p0;
  1293. cmd.P1 = p1;
  1294. cmd.P2 = p2;
  1295. ret = lbs_cmd_with_response(priv, CMD_802_11_TPC_CFG, &cmd);
  1296. return ret;
  1297. }
  1298. /**
  1299. * @brief Configures the power adaptation settings.
  1300. *
  1301. * @param priv A pointer to struct lbs_private structure
  1302. * @param enable Power adaptation enable
  1303. * @param p0 Power level for 1, 2, 5.5 and 11 Mbps (dBm).
  1304. * @param p1 Power level for 6, 9, 12, 18, 22, 24 and 36 Mbps (dBm).
  1305. * @param p2 Power level for 48 and 54 Mbps (dBm).
  1306. *
  1307. * @return 0 on Success
  1308. */
  1309. int lbs_set_power_adapt_cfg(struct lbs_private *priv, int enable, int8_t p0,
  1310. int8_t p1, int8_t p2)
  1311. {
  1312. struct cmd_ds_802_11_pa_cfg cmd;
  1313. int ret;
  1314. memset(&cmd, 0, sizeof(cmd));
  1315. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1316. cmd.action = cpu_to_le16(CMD_ACT_SET);
  1317. cmd.enable = !!enable;
  1318. cmd.P0 = p0;
  1319. cmd.P1 = p1;
  1320. cmd.P2 = p2;
  1321. ret = lbs_cmd_with_response(priv, CMD_802_11_PA_CFG , &cmd);
  1322. return ret;
  1323. }
  1324. struct cmd_ctrl_node *__lbs_cmd_async(struct lbs_private *priv,
  1325. uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
  1326. int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
  1327. unsigned long callback_arg)
  1328. {
  1329. struct cmd_ctrl_node *cmdnode;
  1330. lbs_deb_enter(LBS_DEB_HOST);
  1331. if (priv->surpriseremoved) {
  1332. lbs_deb_host("PREP_CMD: card removed\n");
  1333. cmdnode = ERR_PTR(-ENOENT);
  1334. goto done;
  1335. }
  1336. /* No commands are allowed in Deep Sleep until we toggle the GPIO
  1337. * to wake up the card and it has signaled that it's ready.
  1338. */
  1339. if (!priv->is_auto_deep_sleep_enabled) {
  1340. if (priv->is_deep_sleep) {
  1341. lbs_deb_cmd("command not allowed in deep sleep\n");
  1342. cmdnode = ERR_PTR(-EBUSY);
  1343. goto done;
  1344. }
  1345. }
  1346. cmdnode = lbs_get_free_cmd_node(priv);
  1347. if (cmdnode == NULL) {
  1348. lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
  1349. /* Wake up main thread to execute next command */
  1350. wake_up_interruptible(&priv->waitq);
  1351. cmdnode = ERR_PTR(-ENOBUFS);
  1352. goto done;
  1353. }
  1354. cmdnode->callback = callback;
  1355. cmdnode->callback_arg = callback_arg;
  1356. /* Copy the incoming command to the buffer */
  1357. memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
  1358. /* Set sequence number, clean result, move to buffer */
  1359. priv->seqnum++;
  1360. cmdnode->cmdbuf->command = cpu_to_le16(command);
  1361. cmdnode->cmdbuf->size = cpu_to_le16(in_cmd_size);
  1362. cmdnode->cmdbuf->seqnum = cpu_to_le16(priv->seqnum);
  1363. cmdnode->cmdbuf->result = 0;
  1364. lbs_deb_host("PREP_CMD: command 0x%04x\n", command);
  1365. cmdnode->cmdwaitqwoken = 0;
  1366. lbs_queue_cmd(priv, cmdnode);
  1367. wake_up_interruptible(&priv->waitq);
  1368. done:
  1369. lbs_deb_leave_args(LBS_DEB_HOST, "ret %p", cmdnode);
  1370. return cmdnode;
  1371. }
  1372. void lbs_cmd_async(struct lbs_private *priv, uint16_t command,
  1373. struct cmd_header *in_cmd, int in_cmd_size)
  1374. {
  1375. lbs_deb_enter(LBS_DEB_CMD);
  1376. __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
  1377. lbs_cmd_async_callback, 0);
  1378. lbs_deb_leave(LBS_DEB_CMD);
  1379. }
  1380. int __lbs_cmd(struct lbs_private *priv, uint16_t command,
  1381. struct cmd_header *in_cmd, int in_cmd_size,
  1382. int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
  1383. unsigned long callback_arg)
  1384. {
  1385. struct cmd_ctrl_node *cmdnode;
  1386. unsigned long flags;
  1387. int ret = 0;
  1388. lbs_deb_enter(LBS_DEB_HOST);
  1389. cmdnode = __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
  1390. callback, callback_arg);
  1391. if (IS_ERR(cmdnode)) {
  1392. ret = PTR_ERR(cmdnode);
  1393. goto done;
  1394. }
  1395. might_sleep();
  1396. wait_event_interruptible(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);
  1397. spin_lock_irqsave(&priv->driver_lock, flags);
  1398. ret = cmdnode->result;
  1399. if (ret)
  1400. lbs_pr_info("PREP_CMD: command 0x%04x failed: %d\n",
  1401. command, ret);
  1402. __lbs_cleanup_and_insert_cmd(priv, cmdnode);
  1403. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1404. done:
  1405. lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
  1406. return ret;
  1407. }
  1408. EXPORT_SYMBOL_GPL(__lbs_cmd);