cmd.c 43 KB

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