cmd.c 43 KB

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