cmd.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926
  1. /*
  2. * This file is part of wl1271
  3. *
  4. * Copyright (C) 2009-2010 Nokia Corporation
  5. *
  6. * Contact: Luciano Coelho <luciano.coelho@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/spi/spi.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/ieee80211.h>
  28. #include <linux/slab.h>
  29. #include "wlcore.h"
  30. #include "debug.h"
  31. #include "io.h"
  32. #include "acx.h"
  33. #include "wl12xx_80211.h"
  34. #include "cmd.h"
  35. #include "event.h"
  36. #include "tx.h"
  37. #include "hw_ops.h"
  38. #define WL1271_CMD_FAST_POLL_COUNT 50
  39. #define WL1271_WAIT_EVENT_FAST_POLL_COUNT 20
  40. /*
  41. * send command to firmware
  42. *
  43. * @wl: wl struct
  44. * @id: command id
  45. * @buf: buffer containing the command, must work with dma
  46. * @len: length of the buffer
  47. */
  48. int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len,
  49. size_t res_len)
  50. {
  51. struct wl1271_cmd_header *cmd;
  52. unsigned long timeout;
  53. u32 intr;
  54. int ret = 0;
  55. u16 status;
  56. u16 poll_count = 0;
  57. if (WARN_ON(unlikely(wl->state == WLCORE_STATE_RESTARTING)))
  58. return -EIO;
  59. cmd = buf;
  60. cmd->id = cpu_to_le16(id);
  61. cmd->status = 0;
  62. WARN_ON(len % 4 != 0);
  63. WARN_ON(test_bit(WL1271_FLAG_IN_ELP, &wl->flags));
  64. ret = wlcore_write(wl, wl->cmd_box_addr, buf, len, false);
  65. if (ret < 0)
  66. goto fail;
  67. /*
  68. * TODO: we just need this because one bit is in a different
  69. * place. Is there any better way?
  70. */
  71. ret = wl->ops->trigger_cmd(wl, wl->cmd_box_addr, buf, len);
  72. if (ret < 0)
  73. goto fail;
  74. timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
  75. ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &intr);
  76. if (ret < 0)
  77. goto fail;
  78. while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
  79. if (time_after(jiffies, timeout)) {
  80. wl1271_error("command complete timeout");
  81. ret = -ETIMEDOUT;
  82. goto fail;
  83. }
  84. poll_count++;
  85. if (poll_count < WL1271_CMD_FAST_POLL_COUNT)
  86. udelay(10);
  87. else
  88. msleep(1);
  89. ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &intr);
  90. if (ret < 0)
  91. goto fail;
  92. }
  93. /* read back the status code of the command */
  94. if (res_len == 0)
  95. res_len = sizeof(struct wl1271_cmd_header);
  96. ret = wlcore_read(wl, wl->cmd_box_addr, cmd, res_len, false);
  97. if (ret < 0)
  98. goto fail;
  99. status = le16_to_cpu(cmd->status);
  100. if (status != CMD_STATUS_SUCCESS) {
  101. wl1271_error("command execute failure %d", status);
  102. ret = -EIO;
  103. goto fail;
  104. }
  105. ret = wlcore_write_reg(wl, REG_INTERRUPT_ACK,
  106. WL1271_ACX_INTR_CMD_COMPLETE);
  107. if (ret < 0)
  108. goto fail;
  109. return 0;
  110. fail:
  111. wl12xx_queue_recovery_work(wl);
  112. return ret;
  113. }
  114. EXPORT_SYMBOL_GPL(wl1271_cmd_send);
  115. /*
  116. * Poll the mailbox event field until any of the bits in the mask is set or a
  117. * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
  118. */
  119. int wlcore_cmd_wait_for_event_or_timeout(struct wl1271 *wl,
  120. u32 mask, bool *timeout)
  121. {
  122. u32 *events_vector;
  123. u32 event;
  124. unsigned long timeout_time;
  125. u16 poll_count = 0;
  126. int ret = 0;
  127. *timeout = false;
  128. events_vector = kmalloc(sizeof(*events_vector), GFP_KERNEL | GFP_DMA);
  129. if (!events_vector)
  130. return -ENOMEM;
  131. timeout_time = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
  132. do {
  133. if (time_after(jiffies, timeout_time)) {
  134. wl1271_debug(DEBUG_CMD, "timeout waiting for event %d",
  135. (int)mask);
  136. *timeout = true;
  137. goto out;
  138. }
  139. poll_count++;
  140. if (poll_count < WL1271_WAIT_EVENT_FAST_POLL_COUNT)
  141. usleep_range(50, 51);
  142. else
  143. usleep_range(1000, 5000);
  144. /* read from both event fields */
  145. ret = wlcore_read(wl, wl->mbox_ptr[0], events_vector,
  146. sizeof(*events_vector), false);
  147. if (ret < 0)
  148. goto out;
  149. event = *events_vector & mask;
  150. ret = wlcore_read(wl, wl->mbox_ptr[1], events_vector,
  151. sizeof(*events_vector), false);
  152. if (ret < 0)
  153. goto out;
  154. event |= *events_vector & mask;
  155. } while (!event);
  156. out:
  157. kfree(events_vector);
  158. return ret;
  159. }
  160. EXPORT_SYMBOL_GPL(wlcore_cmd_wait_for_event_or_timeout);
  161. int wl12xx_cmd_role_enable(struct wl1271 *wl, u8 *addr, u8 role_type,
  162. u8 *role_id)
  163. {
  164. struct wl12xx_cmd_role_enable *cmd;
  165. int ret;
  166. wl1271_debug(DEBUG_CMD, "cmd role enable");
  167. if (WARN_ON(*role_id != WL12XX_INVALID_ROLE_ID))
  168. return -EBUSY;
  169. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  170. if (!cmd) {
  171. ret = -ENOMEM;
  172. goto out;
  173. }
  174. /* get role id */
  175. cmd->role_id = find_first_zero_bit(wl->roles_map, WL12XX_MAX_ROLES);
  176. if (cmd->role_id >= WL12XX_MAX_ROLES) {
  177. ret = -EBUSY;
  178. goto out_free;
  179. }
  180. memcpy(cmd->mac_address, addr, ETH_ALEN);
  181. cmd->role_type = role_type;
  182. ret = wl1271_cmd_send(wl, CMD_ROLE_ENABLE, cmd, sizeof(*cmd), 0);
  183. if (ret < 0) {
  184. wl1271_error("failed to initiate cmd role enable");
  185. goto out_free;
  186. }
  187. __set_bit(cmd->role_id, wl->roles_map);
  188. *role_id = cmd->role_id;
  189. out_free:
  190. kfree(cmd);
  191. out:
  192. return ret;
  193. }
  194. int wl12xx_cmd_role_disable(struct wl1271 *wl, u8 *role_id)
  195. {
  196. struct wl12xx_cmd_role_disable *cmd;
  197. int ret;
  198. wl1271_debug(DEBUG_CMD, "cmd role disable");
  199. if (WARN_ON(*role_id == WL12XX_INVALID_ROLE_ID))
  200. return -ENOENT;
  201. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  202. if (!cmd) {
  203. ret = -ENOMEM;
  204. goto out;
  205. }
  206. cmd->role_id = *role_id;
  207. ret = wl1271_cmd_send(wl, CMD_ROLE_DISABLE, cmd, sizeof(*cmd), 0);
  208. if (ret < 0) {
  209. wl1271_error("failed to initiate cmd role disable");
  210. goto out_free;
  211. }
  212. __clear_bit(*role_id, wl->roles_map);
  213. *role_id = WL12XX_INVALID_ROLE_ID;
  214. out_free:
  215. kfree(cmd);
  216. out:
  217. return ret;
  218. }
  219. static int wlcore_get_new_session_id(struct wl1271 *wl, u8 hlid)
  220. {
  221. if (wl->session_ids[hlid] >= SESSION_COUNTER_MAX)
  222. wl->session_ids[hlid] = 0;
  223. wl->session_ids[hlid]++;
  224. return wl->session_ids[hlid];
  225. }
  226. int wl12xx_allocate_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid)
  227. {
  228. unsigned long flags;
  229. u8 link = find_first_zero_bit(wl->links_map, WL12XX_MAX_LINKS);
  230. if (link >= WL12XX_MAX_LINKS)
  231. return -EBUSY;
  232. wl->session_ids[link] = wlcore_get_new_session_id(wl, link);
  233. /* these bits are used by op_tx */
  234. spin_lock_irqsave(&wl->wl_lock, flags);
  235. __set_bit(link, wl->links_map);
  236. __set_bit(link, wlvif->links_map);
  237. spin_unlock_irqrestore(&wl->wl_lock, flags);
  238. *hlid = link;
  239. return 0;
  240. }
  241. void wl12xx_free_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid)
  242. {
  243. unsigned long flags;
  244. if (*hlid == WL12XX_INVALID_LINK_ID)
  245. return;
  246. /* these bits are used by op_tx */
  247. spin_lock_irqsave(&wl->wl_lock, flags);
  248. __clear_bit(*hlid, wl->links_map);
  249. __clear_bit(*hlid, wlvif->links_map);
  250. spin_unlock_irqrestore(&wl->wl_lock, flags);
  251. /*
  252. * At this point op_tx() will not add more packets to the queues. We
  253. * can purge them.
  254. */
  255. wl1271_tx_reset_link_queues(wl, *hlid);
  256. *hlid = WL12XX_INVALID_LINK_ID;
  257. }
  258. static u8 wlcore_get_native_channel_type(u8 nl_channel_type)
  259. {
  260. switch (nl_channel_type) {
  261. case NL80211_CHAN_NO_HT:
  262. return WLCORE_CHAN_NO_HT;
  263. case NL80211_CHAN_HT20:
  264. return WLCORE_CHAN_HT20;
  265. case NL80211_CHAN_HT40MINUS:
  266. return WLCORE_CHAN_HT40MINUS;
  267. case NL80211_CHAN_HT40PLUS:
  268. return WLCORE_CHAN_HT40PLUS;
  269. default:
  270. WARN_ON(1);
  271. return WLCORE_CHAN_NO_HT;
  272. }
  273. }
  274. static int wl12xx_cmd_role_start_dev(struct wl1271 *wl,
  275. struct wl12xx_vif *wlvif,
  276. enum ieee80211_band band,
  277. int channel)
  278. {
  279. struct wl12xx_cmd_role_start *cmd;
  280. int ret;
  281. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  282. if (!cmd) {
  283. ret = -ENOMEM;
  284. goto out;
  285. }
  286. wl1271_debug(DEBUG_CMD, "cmd role start dev %d", wlvif->dev_role_id);
  287. cmd->role_id = wlvif->dev_role_id;
  288. if (band == IEEE80211_BAND_5GHZ)
  289. cmd->band = WLCORE_BAND_5GHZ;
  290. cmd->channel = channel;
  291. if (wlvif->dev_hlid == WL12XX_INVALID_LINK_ID) {
  292. ret = wl12xx_allocate_link(wl, wlvif, &wlvif->dev_hlid);
  293. if (ret)
  294. goto out_free;
  295. }
  296. cmd->device.hlid = wlvif->dev_hlid;
  297. cmd->device.session = wl->session_ids[wlvif->dev_hlid];
  298. wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d",
  299. cmd->role_id, cmd->device.hlid, cmd->device.session);
  300. ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
  301. if (ret < 0) {
  302. wl1271_error("failed to initiate cmd role enable");
  303. goto err_hlid;
  304. }
  305. goto out_free;
  306. err_hlid:
  307. /* clear links on error */
  308. wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid);
  309. out_free:
  310. kfree(cmd);
  311. out:
  312. return ret;
  313. }
  314. static int wl12xx_cmd_role_stop_dev(struct wl1271 *wl,
  315. struct wl12xx_vif *wlvif)
  316. {
  317. struct wl12xx_cmd_role_stop *cmd;
  318. int ret;
  319. if (WARN_ON(wlvif->dev_hlid == WL12XX_INVALID_LINK_ID))
  320. return -EINVAL;
  321. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  322. if (!cmd) {
  323. ret = -ENOMEM;
  324. goto out;
  325. }
  326. wl1271_debug(DEBUG_CMD, "cmd role stop dev");
  327. cmd->role_id = wlvif->dev_role_id;
  328. cmd->disc_type = DISCONNECT_IMMEDIATE;
  329. cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
  330. ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
  331. if (ret < 0) {
  332. wl1271_error("failed to initiate cmd role stop");
  333. goto out_free;
  334. }
  335. wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid);
  336. out_free:
  337. kfree(cmd);
  338. out:
  339. return ret;
  340. }
  341. int wl12xx_cmd_role_start_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  342. {
  343. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  344. struct wl12xx_cmd_role_start *cmd;
  345. u32 supported_rates;
  346. int ret;
  347. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  348. if (!cmd) {
  349. ret = -ENOMEM;
  350. goto out;
  351. }
  352. wl1271_debug(DEBUG_CMD, "cmd role start sta %d", wlvif->role_id);
  353. cmd->role_id = wlvif->role_id;
  354. if (wlvif->band == IEEE80211_BAND_5GHZ)
  355. cmd->band = WLCORE_BAND_5GHZ;
  356. cmd->channel = wlvif->channel;
  357. cmd->sta.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
  358. cmd->sta.beacon_interval = cpu_to_le16(wlvif->beacon_int);
  359. cmd->sta.ssid_type = WL12XX_SSID_TYPE_ANY;
  360. cmd->sta.ssid_len = wlvif->ssid_len;
  361. memcpy(cmd->sta.ssid, wlvif->ssid, wlvif->ssid_len);
  362. memcpy(cmd->sta.bssid, vif->bss_conf.bssid, ETH_ALEN);
  363. supported_rates = CONF_TX_ENABLED_RATES | CONF_TX_MCS_RATES |
  364. wlcore_hw_sta_get_ap_rate_mask(wl, wlvif);
  365. if (wlvif->p2p)
  366. supported_rates &= ~CONF_TX_CCK_RATES;
  367. cmd->sta.local_rates = cpu_to_le32(supported_rates);
  368. cmd->channel_type = wlcore_get_native_channel_type(wlvif->channel_type);
  369. if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
  370. ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid);
  371. if (ret)
  372. goto out_free;
  373. }
  374. cmd->sta.hlid = wlvif->sta.hlid;
  375. cmd->sta.session = wl->session_ids[wlvif->sta.hlid];
  376. /*
  377. * We don't have the correct remote rates in this stage, and there
  378. * is no way to update them later, so use our supported rates instead.
  379. * The fw will take the configured rate policies into account anyway.
  380. */
  381. cmd->sta.remote_rates = cpu_to_le32(supported_rates);
  382. wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
  383. "basic_rate_set: 0x%x, remote_rates: 0x%x",
  384. wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
  385. wlvif->basic_rate_set, wlvif->rate_set);
  386. ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
  387. if (ret < 0) {
  388. wl1271_error("failed to initiate cmd role start sta");
  389. goto err_hlid;
  390. }
  391. wlvif->sta.role_chan_type = wlvif->channel_type;
  392. goto out_free;
  393. err_hlid:
  394. /* clear links on error. */
  395. wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
  396. out_free:
  397. kfree(cmd);
  398. out:
  399. return ret;
  400. }
  401. /* use this function to stop ibss as well */
  402. int wl12xx_cmd_role_stop_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  403. {
  404. struct wl12xx_cmd_role_stop *cmd;
  405. int ret;
  406. if (WARN_ON(wlvif->sta.hlid == WL12XX_INVALID_LINK_ID))
  407. return -EINVAL;
  408. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  409. if (!cmd) {
  410. ret = -ENOMEM;
  411. goto out;
  412. }
  413. wl1271_debug(DEBUG_CMD, "cmd role stop sta %d", wlvif->role_id);
  414. cmd->role_id = wlvif->role_id;
  415. cmd->disc_type = DISCONNECT_IMMEDIATE;
  416. cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
  417. ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
  418. if (ret < 0) {
  419. wl1271_error("failed to initiate cmd role stop sta");
  420. goto out_free;
  421. }
  422. wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
  423. out_free:
  424. kfree(cmd);
  425. out:
  426. return ret;
  427. }
  428. int wl12xx_cmd_role_start_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  429. {
  430. struct wl12xx_cmd_role_start *cmd;
  431. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  432. struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
  433. u32 supported_rates;
  434. int ret;
  435. wl1271_debug(DEBUG_CMD, "cmd role start ap %d", wlvif->role_id);
  436. /* trying to use hidden SSID with an old hostapd version */
  437. if (wlvif->ssid_len == 0 && !bss_conf->hidden_ssid) {
  438. wl1271_error("got a null SSID from beacon/bss");
  439. ret = -EINVAL;
  440. goto out;
  441. }
  442. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  443. if (!cmd) {
  444. ret = -ENOMEM;
  445. goto out;
  446. }
  447. ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.global_hlid);
  448. if (ret < 0)
  449. goto out_free;
  450. ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.bcast_hlid);
  451. if (ret < 0)
  452. goto out_free_global;
  453. cmd->role_id = wlvif->role_id;
  454. cmd->ap.aging_period = cpu_to_le16(wl->conf.tx.ap_aging_period);
  455. cmd->ap.bss_index = WL1271_AP_BSS_INDEX;
  456. cmd->ap.global_hlid = wlvif->ap.global_hlid;
  457. cmd->ap.broadcast_hlid = wlvif->ap.bcast_hlid;
  458. cmd->ap.global_session_id = wl->session_ids[wlvif->ap.global_hlid];
  459. cmd->ap.bcast_session_id = wl->session_ids[wlvif->ap.bcast_hlid];
  460. cmd->ap.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
  461. cmd->ap.beacon_interval = cpu_to_le16(wlvif->beacon_int);
  462. cmd->ap.dtim_interval = bss_conf->dtim_period;
  463. cmd->ap.beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
  464. /* FIXME: Change when adding DFS */
  465. cmd->ap.reset_tsf = 1; /* By default reset AP TSF */
  466. cmd->ap.wmm = wlvif->wmm_enabled;
  467. cmd->channel = wlvif->channel;
  468. cmd->channel_type = wlcore_get_native_channel_type(wlvif->channel_type);
  469. if (!bss_conf->hidden_ssid) {
  470. /* take the SSID from the beacon for backward compatibility */
  471. cmd->ap.ssid_type = WL12XX_SSID_TYPE_PUBLIC;
  472. cmd->ap.ssid_len = wlvif->ssid_len;
  473. memcpy(cmd->ap.ssid, wlvif->ssid, wlvif->ssid_len);
  474. } else {
  475. cmd->ap.ssid_type = WL12XX_SSID_TYPE_HIDDEN;
  476. cmd->ap.ssid_len = bss_conf->ssid_len;
  477. memcpy(cmd->ap.ssid, bss_conf->ssid, bss_conf->ssid_len);
  478. }
  479. supported_rates = CONF_TX_ENABLED_RATES | CONF_TX_MCS_RATES |
  480. wlcore_hw_ap_get_mimo_wide_rate_mask(wl, wlvif);
  481. if (wlvif->p2p)
  482. supported_rates &= ~CONF_TX_CCK_RATES;
  483. wl1271_debug(DEBUG_CMD, "cmd role start ap with supported_rates 0x%08x",
  484. supported_rates);
  485. cmd->ap.local_rates = cpu_to_le32(supported_rates);
  486. switch (wlvif->band) {
  487. case IEEE80211_BAND_2GHZ:
  488. cmd->band = WLCORE_BAND_2_4GHZ;
  489. break;
  490. case IEEE80211_BAND_5GHZ:
  491. cmd->band = WLCORE_BAND_5GHZ;
  492. break;
  493. default:
  494. wl1271_warning("ap start - unknown band: %d", (int)wlvif->band);
  495. cmd->band = WLCORE_BAND_2_4GHZ;
  496. break;
  497. }
  498. ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
  499. if (ret < 0) {
  500. wl1271_error("failed to initiate cmd role start ap");
  501. goto out_free_bcast;
  502. }
  503. goto out_free;
  504. out_free_bcast:
  505. wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
  506. out_free_global:
  507. wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
  508. out_free:
  509. kfree(cmd);
  510. out:
  511. return ret;
  512. }
  513. int wl12xx_cmd_role_stop_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  514. {
  515. struct wl12xx_cmd_role_stop *cmd;
  516. int ret;
  517. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  518. if (!cmd) {
  519. ret = -ENOMEM;
  520. goto out;
  521. }
  522. wl1271_debug(DEBUG_CMD, "cmd role stop ap %d", wlvif->role_id);
  523. cmd->role_id = wlvif->role_id;
  524. ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
  525. if (ret < 0) {
  526. wl1271_error("failed to initiate cmd role stop ap");
  527. goto out_free;
  528. }
  529. wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
  530. wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
  531. out_free:
  532. kfree(cmd);
  533. out:
  534. return ret;
  535. }
  536. int wl12xx_cmd_role_start_ibss(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  537. {
  538. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  539. struct wl12xx_cmd_role_start *cmd;
  540. struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
  541. int ret;
  542. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  543. if (!cmd) {
  544. ret = -ENOMEM;
  545. goto out;
  546. }
  547. wl1271_debug(DEBUG_CMD, "cmd role start ibss %d", wlvif->role_id);
  548. cmd->role_id = wlvif->role_id;
  549. if (wlvif->band == IEEE80211_BAND_5GHZ)
  550. cmd->band = WLCORE_BAND_5GHZ;
  551. cmd->channel = wlvif->channel;
  552. cmd->ibss.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
  553. cmd->ibss.beacon_interval = cpu_to_le16(wlvif->beacon_int);
  554. cmd->ibss.dtim_interval = bss_conf->dtim_period;
  555. cmd->ibss.ssid_type = WL12XX_SSID_TYPE_ANY;
  556. cmd->ibss.ssid_len = wlvif->ssid_len;
  557. memcpy(cmd->ibss.ssid, wlvif->ssid, wlvif->ssid_len);
  558. memcpy(cmd->ibss.bssid, vif->bss_conf.bssid, ETH_ALEN);
  559. cmd->sta.local_rates = cpu_to_le32(wlvif->rate_set);
  560. if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
  561. ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid);
  562. if (ret)
  563. goto out_free;
  564. }
  565. cmd->ibss.hlid = wlvif->sta.hlid;
  566. cmd->ibss.remote_rates = cpu_to_le32(wlvif->rate_set);
  567. wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
  568. "basic_rate_set: 0x%x, remote_rates: 0x%x",
  569. wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
  570. wlvif->basic_rate_set, wlvif->rate_set);
  571. wl1271_debug(DEBUG_CMD, "vif->bss_conf.bssid = %pM",
  572. vif->bss_conf.bssid);
  573. ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
  574. if (ret < 0) {
  575. wl1271_error("failed to initiate cmd role enable");
  576. goto err_hlid;
  577. }
  578. goto out_free;
  579. err_hlid:
  580. /* clear links on error. */
  581. wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
  582. out_free:
  583. kfree(cmd);
  584. out:
  585. return ret;
  586. }
  587. /**
  588. * send test command to firmware
  589. *
  590. * @wl: wl struct
  591. * @buf: buffer containing the command, with all headers, must work with dma
  592. * @len: length of the buffer
  593. * @answer: is answer needed
  594. */
  595. int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
  596. {
  597. int ret;
  598. size_t res_len = 0;
  599. wl1271_debug(DEBUG_CMD, "cmd test");
  600. if (answer)
  601. res_len = buf_len;
  602. ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
  603. if (ret < 0) {
  604. wl1271_warning("TEST command failed");
  605. return ret;
  606. }
  607. return ret;
  608. }
  609. EXPORT_SYMBOL_GPL(wl1271_cmd_test);
  610. /**
  611. * read acx from firmware
  612. *
  613. * @wl: wl struct
  614. * @id: acx id
  615. * @buf: buffer for the response, including all headers, must work with dma
  616. * @len: length of buf
  617. */
  618. int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
  619. {
  620. struct acx_header *acx = buf;
  621. int ret;
  622. wl1271_debug(DEBUG_CMD, "cmd interrogate");
  623. acx->id = cpu_to_le16(id);
  624. /* payload length, does not include any headers */
  625. acx->len = cpu_to_le16(len - sizeof(*acx));
  626. ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
  627. if (ret < 0)
  628. wl1271_error("INTERROGATE command failed");
  629. return ret;
  630. }
  631. /**
  632. * write acx value to firmware
  633. *
  634. * @wl: wl struct
  635. * @id: acx id
  636. * @buf: buffer containing acx, including all headers, must work with dma
  637. * @len: length of buf
  638. */
  639. int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
  640. {
  641. struct acx_header *acx = buf;
  642. int ret;
  643. wl1271_debug(DEBUG_CMD, "cmd configure (%d)", id);
  644. acx->id = cpu_to_le16(id);
  645. /* payload length, does not include any headers */
  646. acx->len = cpu_to_le16(len - sizeof(*acx));
  647. ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
  648. if (ret < 0) {
  649. wl1271_warning("CONFIGURE command NOK");
  650. return ret;
  651. }
  652. return 0;
  653. }
  654. EXPORT_SYMBOL_GPL(wl1271_cmd_configure);
  655. int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
  656. {
  657. struct cmd_enabledisable_path *cmd;
  658. int ret;
  659. u16 cmd_rx, cmd_tx;
  660. wl1271_debug(DEBUG_CMD, "cmd data path");
  661. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  662. if (!cmd) {
  663. ret = -ENOMEM;
  664. goto out;
  665. }
  666. /* the channel here is only used for calibration, so hardcoded to 1 */
  667. cmd->channel = 1;
  668. if (enable) {
  669. cmd_rx = CMD_ENABLE_RX;
  670. cmd_tx = CMD_ENABLE_TX;
  671. } else {
  672. cmd_rx = CMD_DISABLE_RX;
  673. cmd_tx = CMD_DISABLE_TX;
  674. }
  675. ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
  676. if (ret < 0) {
  677. wl1271_error("rx %s cmd for channel %d failed",
  678. enable ? "start" : "stop", cmd->channel);
  679. goto out;
  680. }
  681. wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
  682. enable ? "start" : "stop", cmd->channel);
  683. ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
  684. if (ret < 0) {
  685. wl1271_error("tx %s cmd for channel %d failed",
  686. enable ? "start" : "stop", cmd->channel);
  687. goto out;
  688. }
  689. wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
  690. enable ? "start" : "stop", cmd->channel);
  691. out:
  692. kfree(cmd);
  693. return ret;
  694. }
  695. EXPORT_SYMBOL_GPL(wl1271_cmd_data_path);
  696. int wl1271_cmd_ps_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  697. u8 ps_mode, u16 auto_ps_timeout)
  698. {
  699. struct wl1271_cmd_ps_params *ps_params = NULL;
  700. int ret = 0;
  701. wl1271_debug(DEBUG_CMD, "cmd set ps mode");
  702. ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
  703. if (!ps_params) {
  704. ret = -ENOMEM;
  705. goto out;
  706. }
  707. ps_params->role_id = wlvif->role_id;
  708. ps_params->ps_mode = ps_mode;
  709. ps_params->auto_ps_timeout = auto_ps_timeout;
  710. ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
  711. sizeof(*ps_params), 0);
  712. if (ret < 0) {
  713. wl1271_error("cmd set_ps_mode failed");
  714. goto out;
  715. }
  716. out:
  717. kfree(ps_params);
  718. return ret;
  719. }
  720. int wl1271_cmd_template_set(struct wl1271 *wl, u8 role_id,
  721. u16 template_id, void *buf, size_t buf_len,
  722. int index, u32 rates)
  723. {
  724. struct wl1271_cmd_template_set *cmd;
  725. int ret = 0;
  726. wl1271_debug(DEBUG_CMD, "cmd template_set %d (role %d)",
  727. template_id, role_id);
  728. WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
  729. buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
  730. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  731. if (!cmd) {
  732. ret = -ENOMEM;
  733. goto out;
  734. }
  735. /* during initialization wlvif is NULL */
  736. cmd->role_id = role_id;
  737. cmd->len = cpu_to_le16(buf_len);
  738. cmd->template_type = template_id;
  739. cmd->enabled_rates = cpu_to_le32(rates);
  740. cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
  741. cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
  742. cmd->index = index;
  743. if (buf)
  744. memcpy(cmd->template_data, buf, buf_len);
  745. ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
  746. if (ret < 0) {
  747. wl1271_warning("cmd set_template failed: %d", ret);
  748. goto out_free;
  749. }
  750. out_free:
  751. kfree(cmd);
  752. out:
  753. return ret;
  754. }
  755. int wl12xx_cmd_build_null_data(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  756. {
  757. struct sk_buff *skb = NULL;
  758. int size;
  759. void *ptr;
  760. int ret = -ENOMEM;
  761. if (wlvif->bss_type == BSS_TYPE_IBSS) {
  762. size = sizeof(struct wl12xx_null_data_template);
  763. ptr = NULL;
  764. } else {
  765. skb = ieee80211_nullfunc_get(wl->hw,
  766. wl12xx_wlvif_to_vif(wlvif));
  767. if (!skb)
  768. goto out;
  769. size = skb->len;
  770. ptr = skb->data;
  771. }
  772. ret = wl1271_cmd_template_set(wl, wlvif->role_id,
  773. CMD_TEMPL_NULL_DATA, ptr, size, 0,
  774. wlvif->basic_rate);
  775. out:
  776. dev_kfree_skb(skb);
  777. if (ret)
  778. wl1271_warning("cmd buld null data failed %d", ret);
  779. return ret;
  780. }
  781. int wl12xx_cmd_build_klv_null_data(struct wl1271 *wl,
  782. struct wl12xx_vif *wlvif)
  783. {
  784. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  785. struct sk_buff *skb = NULL;
  786. int ret = -ENOMEM;
  787. skb = ieee80211_nullfunc_get(wl->hw, vif);
  788. if (!skb)
  789. goto out;
  790. ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_KLV,
  791. skb->data, skb->len,
  792. wlvif->sta.klv_template_id,
  793. wlvif->basic_rate);
  794. out:
  795. dev_kfree_skb(skb);
  796. if (ret)
  797. wl1271_warning("cmd build klv null data failed %d", ret);
  798. return ret;
  799. }
  800. int wl1271_cmd_build_ps_poll(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  801. u16 aid)
  802. {
  803. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  804. struct sk_buff *skb;
  805. int ret = 0;
  806. skb = ieee80211_pspoll_get(wl->hw, vif);
  807. if (!skb)
  808. goto out;
  809. ret = wl1271_cmd_template_set(wl, wlvif->role_id,
  810. CMD_TEMPL_PS_POLL, skb->data,
  811. skb->len, 0, wlvif->basic_rate_set);
  812. out:
  813. dev_kfree_skb(skb);
  814. return ret;
  815. }
  816. int wl12xx_cmd_build_probe_req(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  817. u8 role_id, u8 band,
  818. const u8 *ssid, size_t ssid_len,
  819. const u8 *ie, size_t ie_len, bool sched_scan)
  820. {
  821. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  822. struct sk_buff *skb;
  823. int ret;
  824. u32 rate;
  825. u16 template_id_2_4 = wl->scan_templ_id_2_4;
  826. u16 template_id_5 = wl->scan_templ_id_5;
  827. skb = ieee80211_probereq_get(wl->hw, vif, ssid, ssid_len,
  828. ie, ie_len);
  829. if (!skb) {
  830. ret = -ENOMEM;
  831. goto out;
  832. }
  833. wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
  834. if (sched_scan &&
  835. (wl->quirks & WLCORE_QUIRK_DUAL_PROBE_TMPL)) {
  836. template_id_2_4 = wl->sched_scan_templ_id_2_4;
  837. template_id_5 = wl->sched_scan_templ_id_5;
  838. }
  839. rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
  840. if (band == IEEE80211_BAND_2GHZ)
  841. ret = wl1271_cmd_template_set(wl, role_id,
  842. template_id_2_4,
  843. skb->data, skb->len, 0, rate);
  844. else
  845. ret = wl1271_cmd_template_set(wl, role_id,
  846. template_id_5,
  847. skb->data, skb->len, 0, rate);
  848. out:
  849. dev_kfree_skb(skb);
  850. return ret;
  851. }
  852. EXPORT_SYMBOL_GPL(wl12xx_cmd_build_probe_req);
  853. struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
  854. struct wl12xx_vif *wlvif,
  855. struct sk_buff *skb)
  856. {
  857. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  858. int ret;
  859. u32 rate;
  860. if (!skb)
  861. skb = ieee80211_ap_probereq_get(wl->hw, vif);
  862. if (!skb)
  863. goto out;
  864. wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
  865. rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[wlvif->band]);
  866. if (wlvif->band == IEEE80211_BAND_2GHZ)
  867. ret = wl1271_cmd_template_set(wl, wlvif->role_id,
  868. CMD_TEMPL_CFG_PROBE_REQ_2_4,
  869. skb->data, skb->len, 0, rate);
  870. else
  871. ret = wl1271_cmd_template_set(wl, wlvif->role_id,
  872. CMD_TEMPL_CFG_PROBE_REQ_5,
  873. skb->data, skb->len, 0, rate);
  874. if (ret < 0)
  875. wl1271_error("Unable to set ap probe request template.");
  876. out:
  877. return skb;
  878. }
  879. int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  880. {
  881. int ret, extra = 0;
  882. u16 fc;
  883. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  884. struct sk_buff *skb;
  885. struct wl12xx_arp_rsp_template *tmpl;
  886. struct ieee80211_hdr_3addr *hdr;
  887. struct arphdr *arp_hdr;
  888. skb = dev_alloc_skb(sizeof(*hdr) + sizeof(__le16) + sizeof(*tmpl) +
  889. WL1271_EXTRA_SPACE_MAX);
  890. if (!skb) {
  891. wl1271_error("failed to allocate buffer for arp rsp template");
  892. return -ENOMEM;
  893. }
  894. skb_reserve(skb, sizeof(*hdr) + WL1271_EXTRA_SPACE_MAX);
  895. tmpl = (struct wl12xx_arp_rsp_template *)skb_put(skb, sizeof(*tmpl));
  896. memset(tmpl, 0, sizeof(*tmpl));
  897. /* llc layer */
  898. memcpy(tmpl->llc_hdr, rfc1042_header, sizeof(rfc1042_header));
  899. tmpl->llc_type = cpu_to_be16(ETH_P_ARP);
  900. /* arp header */
  901. arp_hdr = &tmpl->arp_hdr;
  902. arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
  903. arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
  904. arp_hdr->ar_hln = ETH_ALEN;
  905. arp_hdr->ar_pln = 4;
  906. arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
  907. /* arp payload */
  908. memcpy(tmpl->sender_hw, vif->addr, ETH_ALEN);
  909. tmpl->sender_ip = wlvif->ip_addr;
  910. /* encryption space */
  911. switch (wlvif->encryption_type) {
  912. case KEY_TKIP:
  913. if (wl->quirks & WLCORE_QUIRK_TKIP_HEADER_SPACE)
  914. extra = WL1271_EXTRA_SPACE_TKIP;
  915. break;
  916. case KEY_AES:
  917. extra = WL1271_EXTRA_SPACE_AES;
  918. break;
  919. case KEY_NONE:
  920. case KEY_WEP:
  921. case KEY_GEM:
  922. extra = 0;
  923. break;
  924. default:
  925. wl1271_warning("Unknown encryption type: %d",
  926. wlvif->encryption_type);
  927. ret = -EINVAL;
  928. goto out;
  929. }
  930. if (extra) {
  931. u8 *space = skb_push(skb, extra);
  932. memset(space, 0, extra);
  933. }
  934. /* QoS header - BE */
  935. if (wlvif->sta.qos)
  936. memset(skb_push(skb, sizeof(__le16)), 0, sizeof(__le16));
  937. /* mac80211 header */
  938. hdr = (struct ieee80211_hdr_3addr *)skb_push(skb, sizeof(*hdr));
  939. memset(hdr, 0, sizeof(*hdr));
  940. fc = IEEE80211_FTYPE_DATA | IEEE80211_FCTL_TODS;
  941. if (wlvif->sta.qos)
  942. fc |= IEEE80211_STYPE_QOS_DATA;
  943. else
  944. fc |= IEEE80211_STYPE_DATA;
  945. if (wlvif->encryption_type != KEY_NONE)
  946. fc |= IEEE80211_FCTL_PROTECTED;
  947. hdr->frame_control = cpu_to_le16(fc);
  948. memcpy(hdr->addr1, vif->bss_conf.bssid, ETH_ALEN);
  949. memcpy(hdr->addr2, vif->addr, ETH_ALEN);
  950. memset(hdr->addr3, 0xff, ETH_ALEN);
  951. ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_ARP_RSP,
  952. skb->data, skb->len, 0,
  953. wlvif->basic_rate);
  954. out:
  955. dev_kfree_skb(skb);
  956. return ret;
  957. }
  958. int wl1271_build_qos_null_data(struct wl1271 *wl, struct ieee80211_vif *vif)
  959. {
  960. struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
  961. struct ieee80211_qos_hdr template;
  962. memset(&template, 0, sizeof(template));
  963. memcpy(template.addr1, vif->bss_conf.bssid, ETH_ALEN);
  964. memcpy(template.addr2, vif->addr, ETH_ALEN);
  965. memcpy(template.addr3, vif->bss_conf.bssid, ETH_ALEN);
  966. template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  967. IEEE80211_STYPE_QOS_NULLFUNC |
  968. IEEE80211_FCTL_TODS);
  969. /* FIXME: not sure what priority to use here */
  970. template.qos_ctrl = cpu_to_le16(0);
  971. return wl1271_cmd_template_set(wl, wlvif->role_id,
  972. CMD_TEMPL_QOS_NULL_DATA, &template,
  973. sizeof(template), 0,
  974. wlvif->basic_rate);
  975. }
  976. int wl12xx_cmd_set_default_wep_key(struct wl1271 *wl, u8 id, u8 hlid)
  977. {
  978. struct wl1271_cmd_set_keys *cmd;
  979. int ret = 0;
  980. wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
  981. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  982. if (!cmd) {
  983. ret = -ENOMEM;
  984. goto out;
  985. }
  986. cmd->hlid = hlid;
  987. cmd->key_id = id;
  988. cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
  989. cmd->key_action = cpu_to_le16(KEY_SET_ID);
  990. cmd->key_type = KEY_WEP;
  991. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  992. if (ret < 0) {
  993. wl1271_warning("cmd set_default_wep_key failed: %d", ret);
  994. goto out;
  995. }
  996. out:
  997. kfree(cmd);
  998. return ret;
  999. }
  1000. int wl1271_cmd_set_sta_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1001. u16 action, u8 id, u8 key_type,
  1002. u8 key_size, const u8 *key, const u8 *addr,
  1003. u32 tx_seq_32, u16 tx_seq_16)
  1004. {
  1005. struct wl1271_cmd_set_keys *cmd;
  1006. int ret = 0;
  1007. /* hlid might have already been deleted */
  1008. if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
  1009. return 0;
  1010. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1011. if (!cmd) {
  1012. ret = -ENOMEM;
  1013. goto out;
  1014. }
  1015. cmd->hlid = wlvif->sta.hlid;
  1016. if (key_type == KEY_WEP)
  1017. cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
  1018. else if (is_broadcast_ether_addr(addr))
  1019. cmd->lid_key_type = BROADCAST_LID_TYPE;
  1020. else
  1021. cmd->lid_key_type = UNICAST_LID_TYPE;
  1022. cmd->key_action = cpu_to_le16(action);
  1023. cmd->key_size = key_size;
  1024. cmd->key_type = key_type;
  1025. cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
  1026. cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
  1027. cmd->key_id = id;
  1028. if (key_type == KEY_TKIP) {
  1029. /*
  1030. * We get the key in the following form:
  1031. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  1032. * but the target is expecting:
  1033. * TKIP - RX MIC - TX MIC
  1034. */
  1035. memcpy(cmd->key, key, 16);
  1036. memcpy(cmd->key + 16, key + 24, 8);
  1037. memcpy(cmd->key + 24, key + 16, 8);
  1038. } else {
  1039. memcpy(cmd->key, key, key_size);
  1040. }
  1041. wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
  1042. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  1043. if (ret < 0) {
  1044. wl1271_warning("could not set keys");
  1045. goto out;
  1046. }
  1047. out:
  1048. kfree(cmd);
  1049. return ret;
  1050. }
  1051. /*
  1052. * TODO: merge with sta/ibss into 1 set_key function.
  1053. * note there are slight diffs
  1054. */
  1055. int wl1271_cmd_set_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1056. u16 action, u8 id, u8 key_type,
  1057. u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
  1058. u16 tx_seq_16)
  1059. {
  1060. struct wl1271_cmd_set_keys *cmd;
  1061. int ret = 0;
  1062. u8 lid_type;
  1063. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1064. if (!cmd)
  1065. return -ENOMEM;
  1066. if (hlid == wlvif->ap.bcast_hlid) {
  1067. if (key_type == KEY_WEP)
  1068. lid_type = WEP_DEFAULT_LID_TYPE;
  1069. else
  1070. lid_type = BROADCAST_LID_TYPE;
  1071. } else {
  1072. lid_type = UNICAST_LID_TYPE;
  1073. }
  1074. wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
  1075. " hlid: %d", (int)action, (int)id, (int)lid_type,
  1076. (int)key_type, (int)hlid);
  1077. cmd->lid_key_type = lid_type;
  1078. cmd->hlid = hlid;
  1079. cmd->key_action = cpu_to_le16(action);
  1080. cmd->key_size = key_size;
  1081. cmd->key_type = key_type;
  1082. cmd->key_id = id;
  1083. cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
  1084. cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
  1085. if (key_type == KEY_TKIP) {
  1086. /*
  1087. * We get the key in the following form:
  1088. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  1089. * but the target is expecting:
  1090. * TKIP - RX MIC - TX MIC
  1091. */
  1092. memcpy(cmd->key, key, 16);
  1093. memcpy(cmd->key + 16, key + 24, 8);
  1094. memcpy(cmd->key + 24, key + 16, 8);
  1095. } else {
  1096. memcpy(cmd->key, key, key_size);
  1097. }
  1098. wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
  1099. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  1100. if (ret < 0) {
  1101. wl1271_warning("could not set ap keys");
  1102. goto out;
  1103. }
  1104. out:
  1105. kfree(cmd);
  1106. return ret;
  1107. }
  1108. int wl12xx_cmd_set_peer_state(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1109. u8 hlid)
  1110. {
  1111. struct wl12xx_cmd_set_peer_state *cmd;
  1112. int ret = 0;
  1113. wl1271_debug(DEBUG_CMD, "cmd set peer state (hlid=%d)", hlid);
  1114. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1115. if (!cmd) {
  1116. ret = -ENOMEM;
  1117. goto out;
  1118. }
  1119. cmd->hlid = hlid;
  1120. cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
  1121. /* wmm param is valid only for station role */
  1122. if (wlvif->bss_type == BSS_TYPE_STA_BSS)
  1123. cmd->wmm = wlvif->wmm_enabled;
  1124. ret = wl1271_cmd_send(wl, CMD_SET_PEER_STATE, cmd, sizeof(*cmd), 0);
  1125. if (ret < 0) {
  1126. wl1271_error("failed to send set peer state command");
  1127. goto out_free;
  1128. }
  1129. out_free:
  1130. kfree(cmd);
  1131. out:
  1132. return ret;
  1133. }
  1134. int wl12xx_cmd_add_peer(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1135. struct ieee80211_sta *sta, u8 hlid)
  1136. {
  1137. struct wl12xx_cmd_add_peer *cmd;
  1138. int i, ret;
  1139. u32 sta_rates;
  1140. wl1271_debug(DEBUG_CMD, "cmd add peer %d", (int)hlid);
  1141. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1142. if (!cmd) {
  1143. ret = -ENOMEM;
  1144. goto out;
  1145. }
  1146. memcpy(cmd->addr, sta->addr, ETH_ALEN);
  1147. cmd->bss_index = WL1271_AP_BSS_INDEX;
  1148. cmd->aid = sta->aid;
  1149. cmd->hlid = hlid;
  1150. cmd->sp_len = sta->max_sp;
  1151. cmd->wmm = sta->wme ? 1 : 0;
  1152. cmd->session_id = wl->session_ids[hlid];
  1153. for (i = 0; i < NUM_ACCESS_CATEGORIES_COPY; i++)
  1154. if (sta->wme && (sta->uapsd_queues & BIT(i)))
  1155. cmd->psd_type[NUM_ACCESS_CATEGORIES_COPY-1-i] =
  1156. WL1271_PSD_UPSD_TRIGGER;
  1157. else
  1158. cmd->psd_type[NUM_ACCESS_CATEGORIES_COPY-1-i] =
  1159. WL1271_PSD_LEGACY;
  1160. sta_rates = sta->supp_rates[wlvif->band];
  1161. if (sta->ht_cap.ht_supported)
  1162. sta_rates |=
  1163. (sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET) |
  1164. (sta->ht_cap.mcs.rx_mask[1] << HW_MIMO_RATES_OFFSET);
  1165. cmd->supported_rates =
  1166. cpu_to_le32(wl1271_tx_enabled_rates_get(wl, sta_rates,
  1167. wlvif->band));
  1168. wl1271_debug(DEBUG_CMD, "new peer rates=0x%x queues=0x%x",
  1169. cmd->supported_rates, sta->uapsd_queues);
  1170. ret = wl1271_cmd_send(wl, CMD_ADD_PEER, cmd, sizeof(*cmd), 0);
  1171. if (ret < 0) {
  1172. wl1271_error("failed to initiate cmd add peer");
  1173. goto out_free;
  1174. }
  1175. out_free:
  1176. kfree(cmd);
  1177. out:
  1178. return ret;
  1179. }
  1180. int wl12xx_cmd_remove_peer(struct wl1271 *wl, u8 hlid)
  1181. {
  1182. struct wl12xx_cmd_remove_peer *cmd;
  1183. int ret;
  1184. bool timeout = false;
  1185. wl1271_debug(DEBUG_CMD, "cmd remove peer %d", (int)hlid);
  1186. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1187. if (!cmd) {
  1188. ret = -ENOMEM;
  1189. goto out;
  1190. }
  1191. cmd->hlid = hlid;
  1192. /* We never send a deauth, mac80211 is in charge of this */
  1193. cmd->reason_opcode = 0;
  1194. cmd->send_deauth_flag = 0;
  1195. ret = wl1271_cmd_send(wl, CMD_REMOVE_PEER, cmd, sizeof(*cmd), 0);
  1196. if (ret < 0) {
  1197. wl1271_error("failed to initiate cmd remove peer");
  1198. goto out_free;
  1199. }
  1200. ret = wl->ops->wait_for_event(wl,
  1201. WLCORE_EVENT_PEER_REMOVE_COMPLETE,
  1202. &timeout);
  1203. /*
  1204. * We are ok with a timeout here. The event is sometimes not sent
  1205. * due to a firmware bug. In case of another error (like SDIO timeout)
  1206. * queue a recovery.
  1207. */
  1208. if (ret)
  1209. wl12xx_queue_recovery_work(wl);
  1210. out_free:
  1211. kfree(cmd);
  1212. out:
  1213. return ret;
  1214. }
  1215. static int wlcore_get_reg_conf_ch_idx(enum ieee80211_band band, u16 ch)
  1216. {
  1217. int idx = -1;
  1218. switch (band) {
  1219. case IEEE80211_BAND_5GHZ:
  1220. if (ch >= 8 && ch <= 16)
  1221. idx = ((ch-8)/4 + 18);
  1222. else if (ch >= 34 && ch <= 64)
  1223. idx = ((ch-34)/2 + 3 + 18);
  1224. else if (ch >= 100 && ch <= 140)
  1225. idx = ((ch-100)/4 + 15 + 18);
  1226. else if (ch >= 149 && ch <= 165)
  1227. idx = ((ch-149)/4 + 26 + 18);
  1228. else
  1229. idx = -1;
  1230. break;
  1231. case IEEE80211_BAND_2GHZ:
  1232. if (ch >= 1 && ch <= 14)
  1233. idx = ch - 1;
  1234. else
  1235. idx = -1;
  1236. break;
  1237. default:
  1238. wl1271_error("get reg conf ch idx - unknown band: %d",
  1239. (int)band);
  1240. }
  1241. return idx;
  1242. }
  1243. void wlcore_set_pending_regdomain_ch(struct wl1271 *wl, u16 channel,
  1244. enum ieee80211_band band)
  1245. {
  1246. int ch_bit_idx = 0;
  1247. if (!(wl->quirks & WLCORE_QUIRK_REGDOMAIN_CONF))
  1248. return;
  1249. ch_bit_idx = wlcore_get_reg_conf_ch_idx(band, channel);
  1250. if (ch_bit_idx > 0 && ch_bit_idx <= WL1271_MAX_CHANNELS)
  1251. set_bit(ch_bit_idx, (long *)wl->reg_ch_conf_pending);
  1252. }
  1253. int wlcore_cmd_regdomain_config_locked(struct wl1271 *wl)
  1254. {
  1255. struct wl12xx_cmd_regdomain_dfs_config *cmd = NULL;
  1256. int ret = 0, i, b, ch_bit_idx;
  1257. struct ieee80211_channel *channel;
  1258. u32 tmp_ch_bitmap[2];
  1259. u16 ch;
  1260. struct wiphy *wiphy = wl->hw->wiphy;
  1261. struct ieee80211_supported_band *band;
  1262. bool timeout = false;
  1263. if (!(wl->quirks & WLCORE_QUIRK_REGDOMAIN_CONF))
  1264. return 0;
  1265. wl1271_debug(DEBUG_CMD, "cmd reg domain config");
  1266. memset(tmp_ch_bitmap, 0, sizeof(tmp_ch_bitmap));
  1267. for (b = IEEE80211_BAND_2GHZ; b <= IEEE80211_BAND_5GHZ; b++) {
  1268. band = wiphy->bands[b];
  1269. for (i = 0; i < band->n_channels; i++) {
  1270. channel = &band->channels[i];
  1271. ch = channel->hw_value;
  1272. if (channel->flags & (IEEE80211_CHAN_DISABLED |
  1273. IEEE80211_CHAN_RADAR |
  1274. IEEE80211_CHAN_PASSIVE_SCAN))
  1275. continue;
  1276. ch_bit_idx = wlcore_get_reg_conf_ch_idx(b, ch);
  1277. if (ch_bit_idx < 0)
  1278. continue;
  1279. set_bit(ch_bit_idx, (long *)tmp_ch_bitmap);
  1280. }
  1281. }
  1282. tmp_ch_bitmap[0] |= wl->reg_ch_conf_pending[0];
  1283. tmp_ch_bitmap[1] |= wl->reg_ch_conf_pending[1];
  1284. if (!memcmp(tmp_ch_bitmap, wl->reg_ch_conf_last, sizeof(tmp_ch_bitmap)))
  1285. goto out;
  1286. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1287. if (!cmd) {
  1288. ret = -ENOMEM;
  1289. goto out;
  1290. }
  1291. cmd->ch_bit_map1 = cpu_to_le32(tmp_ch_bitmap[0]);
  1292. cmd->ch_bit_map2 = cpu_to_le32(tmp_ch_bitmap[1]);
  1293. wl1271_debug(DEBUG_CMD,
  1294. "cmd reg domain bitmap1: 0x%08x, bitmap2: 0x%08x",
  1295. cmd->ch_bit_map1, cmd->ch_bit_map2);
  1296. ret = wl1271_cmd_send(wl, CMD_DFS_CHANNEL_CONFIG, cmd, sizeof(*cmd), 0);
  1297. if (ret < 0) {
  1298. wl1271_error("failed to send reg domain dfs config");
  1299. goto out;
  1300. }
  1301. ret = wl->ops->wait_for_event(wl,
  1302. WLCORE_EVENT_DFS_CONFIG_COMPLETE,
  1303. &timeout);
  1304. if (ret < 0 || timeout) {
  1305. wl1271_error("reg domain conf %serror",
  1306. timeout ? "completion " : "");
  1307. ret = timeout ? -ETIMEDOUT : ret;
  1308. goto out;
  1309. }
  1310. memcpy(wl->reg_ch_conf_last, tmp_ch_bitmap, sizeof(tmp_ch_bitmap));
  1311. memset(wl->reg_ch_conf_pending, 0, sizeof(wl->reg_ch_conf_pending));
  1312. out:
  1313. kfree(cmd);
  1314. return ret;
  1315. }
  1316. int wl12xx_cmd_config_fwlog(struct wl1271 *wl)
  1317. {
  1318. struct wl12xx_cmd_config_fwlog *cmd;
  1319. int ret = 0;
  1320. wl1271_debug(DEBUG_CMD, "cmd config firmware logger");
  1321. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1322. if (!cmd) {
  1323. ret = -ENOMEM;
  1324. goto out;
  1325. }
  1326. cmd->logger_mode = wl->conf.fwlog.mode;
  1327. cmd->log_severity = wl->conf.fwlog.severity;
  1328. cmd->timestamp = wl->conf.fwlog.timestamp;
  1329. cmd->output = wl->conf.fwlog.output;
  1330. cmd->threshold = wl->conf.fwlog.threshold;
  1331. ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0);
  1332. if (ret < 0) {
  1333. wl1271_error("failed to send config firmware logger command");
  1334. goto out_free;
  1335. }
  1336. out_free:
  1337. kfree(cmd);
  1338. out:
  1339. return ret;
  1340. }
  1341. int wl12xx_cmd_start_fwlog(struct wl1271 *wl)
  1342. {
  1343. struct wl12xx_cmd_start_fwlog *cmd;
  1344. int ret = 0;
  1345. wl1271_debug(DEBUG_CMD, "cmd start firmware logger");
  1346. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1347. if (!cmd) {
  1348. ret = -ENOMEM;
  1349. goto out;
  1350. }
  1351. ret = wl1271_cmd_send(wl, CMD_START_FWLOGGER, cmd, sizeof(*cmd), 0);
  1352. if (ret < 0) {
  1353. wl1271_error("failed to send start firmware logger command");
  1354. goto out_free;
  1355. }
  1356. out_free:
  1357. kfree(cmd);
  1358. out:
  1359. return ret;
  1360. }
  1361. int wl12xx_cmd_stop_fwlog(struct wl1271 *wl)
  1362. {
  1363. struct wl12xx_cmd_stop_fwlog *cmd;
  1364. int ret = 0;
  1365. wl1271_debug(DEBUG_CMD, "cmd stop firmware logger");
  1366. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1367. if (!cmd) {
  1368. ret = -ENOMEM;
  1369. goto out;
  1370. }
  1371. ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0);
  1372. if (ret < 0) {
  1373. wl1271_error("failed to send stop firmware logger command");
  1374. goto out_free;
  1375. }
  1376. out_free:
  1377. kfree(cmd);
  1378. out:
  1379. return ret;
  1380. }
  1381. static int wl12xx_cmd_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1382. u8 role_id, enum ieee80211_band band, u8 channel)
  1383. {
  1384. struct wl12xx_cmd_roc *cmd;
  1385. int ret = 0;
  1386. wl1271_debug(DEBUG_CMD, "cmd roc %d (%d)", channel, role_id);
  1387. if (WARN_ON(role_id == WL12XX_INVALID_ROLE_ID))
  1388. return -EINVAL;
  1389. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1390. if (!cmd) {
  1391. ret = -ENOMEM;
  1392. goto out;
  1393. }
  1394. cmd->role_id = role_id;
  1395. cmd->channel = channel;
  1396. switch (band) {
  1397. case IEEE80211_BAND_2GHZ:
  1398. cmd->band = WLCORE_BAND_2_4GHZ;
  1399. break;
  1400. case IEEE80211_BAND_5GHZ:
  1401. cmd->band = WLCORE_BAND_5GHZ;
  1402. break;
  1403. default:
  1404. wl1271_error("roc - unknown band: %d", (int)wlvif->band);
  1405. ret = -EINVAL;
  1406. goto out_free;
  1407. }
  1408. ret = wl1271_cmd_send(wl, CMD_REMAIN_ON_CHANNEL, cmd, sizeof(*cmd), 0);
  1409. if (ret < 0) {
  1410. wl1271_error("failed to send ROC command");
  1411. goto out_free;
  1412. }
  1413. out_free:
  1414. kfree(cmd);
  1415. out:
  1416. return ret;
  1417. }
  1418. static int wl12xx_cmd_croc(struct wl1271 *wl, u8 role_id)
  1419. {
  1420. struct wl12xx_cmd_croc *cmd;
  1421. int ret = 0;
  1422. wl1271_debug(DEBUG_CMD, "cmd croc (%d)", role_id);
  1423. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1424. if (!cmd) {
  1425. ret = -ENOMEM;
  1426. goto out;
  1427. }
  1428. cmd->role_id = role_id;
  1429. ret = wl1271_cmd_send(wl, CMD_CANCEL_REMAIN_ON_CHANNEL, cmd,
  1430. sizeof(*cmd), 0);
  1431. if (ret < 0) {
  1432. wl1271_error("failed to send ROC command");
  1433. goto out_free;
  1434. }
  1435. out_free:
  1436. kfree(cmd);
  1437. out:
  1438. return ret;
  1439. }
  1440. int wl12xx_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 role_id,
  1441. enum ieee80211_band band, u8 channel)
  1442. {
  1443. int ret = 0;
  1444. if (WARN_ON(test_bit(role_id, wl->roc_map)))
  1445. return 0;
  1446. ret = wl12xx_cmd_roc(wl, wlvif, role_id, band, channel);
  1447. if (ret < 0)
  1448. goto out;
  1449. __set_bit(role_id, wl->roc_map);
  1450. out:
  1451. return ret;
  1452. }
  1453. int wl12xx_croc(struct wl1271 *wl, u8 role_id)
  1454. {
  1455. int ret = 0;
  1456. if (WARN_ON(!test_bit(role_id, wl->roc_map)))
  1457. return 0;
  1458. ret = wl12xx_cmd_croc(wl, role_id);
  1459. if (ret < 0)
  1460. goto out;
  1461. __clear_bit(role_id, wl->roc_map);
  1462. /*
  1463. * Rearm the tx watchdog when removing the last ROC. This prevents
  1464. * recoveries due to just finished ROCs - when Tx hasn't yet had
  1465. * a chance to get out.
  1466. */
  1467. if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) >= WL12XX_MAX_ROLES)
  1468. wl12xx_rearm_tx_watchdog_locked(wl);
  1469. out:
  1470. return ret;
  1471. }
  1472. int wl12xx_cmd_stop_channel_switch(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  1473. {
  1474. struct wl12xx_cmd_stop_channel_switch *cmd;
  1475. int ret;
  1476. wl1271_debug(DEBUG_ACX, "cmd stop channel switch");
  1477. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1478. if (!cmd) {
  1479. ret = -ENOMEM;
  1480. goto out;
  1481. }
  1482. cmd->role_id = wlvif->role_id;
  1483. ret = wl1271_cmd_send(wl, CMD_STOP_CHANNEL_SWICTH, cmd, sizeof(*cmd), 0);
  1484. if (ret < 0) {
  1485. wl1271_error("failed to stop channel switch command");
  1486. goto out_free;
  1487. }
  1488. out_free:
  1489. kfree(cmd);
  1490. out:
  1491. return ret;
  1492. }
  1493. /* start dev role and roc on its channel */
  1494. int wl12xx_start_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1495. enum ieee80211_band band, int channel)
  1496. {
  1497. int ret;
  1498. if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
  1499. wlvif->bss_type == BSS_TYPE_IBSS)))
  1500. return -EINVAL;
  1501. ret = wl12xx_cmd_role_enable(wl,
  1502. wl12xx_wlvif_to_vif(wlvif)->addr,
  1503. WL1271_ROLE_DEVICE,
  1504. &wlvif->dev_role_id);
  1505. if (ret < 0)
  1506. goto out;
  1507. ret = wl12xx_cmd_role_start_dev(wl, wlvif, band, channel);
  1508. if (ret < 0)
  1509. goto out_disable;
  1510. ret = wl12xx_roc(wl, wlvif, wlvif->dev_role_id, band, channel);
  1511. if (ret < 0)
  1512. goto out_stop;
  1513. return 0;
  1514. out_stop:
  1515. wl12xx_cmd_role_stop_dev(wl, wlvif);
  1516. out_disable:
  1517. wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
  1518. out:
  1519. return ret;
  1520. }
  1521. /* croc dev hlid, and stop the role */
  1522. int wl12xx_stop_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  1523. {
  1524. int ret;
  1525. if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
  1526. wlvif->bss_type == BSS_TYPE_IBSS)))
  1527. return -EINVAL;
  1528. /* flush all pending packets */
  1529. ret = wlcore_tx_work_locked(wl);
  1530. if (ret < 0)
  1531. goto out;
  1532. if (test_bit(wlvif->dev_role_id, wl->roc_map)) {
  1533. ret = wl12xx_croc(wl, wlvif->dev_role_id);
  1534. if (ret < 0)
  1535. goto out;
  1536. }
  1537. ret = wl12xx_cmd_role_stop_dev(wl, wlvif);
  1538. if (ret < 0)
  1539. goto out;
  1540. ret = wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
  1541. if (ret < 0)
  1542. goto out;
  1543. out:
  1544. return ret;
  1545. }