wsm.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822
  1. /*
  2. * WSM host interface (HI) implementation for
  3. * ST-Ericsson CW1200 mac80211 drivers.
  4. *
  5. * Copyright (c) 2010, ST-Ericsson
  6. * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/skbuff.h>
  13. #include <linux/wait.h>
  14. #include <linux/delay.h>
  15. #include <linux/sched.h>
  16. #include <linux/random.h>
  17. #include "cw1200.h"
  18. #include "wsm.h"
  19. #include "bh.h"
  20. #include "sta.h"
  21. #include "debug.h"
  22. #define WSM_CMD_TIMEOUT (2 * HZ) /* With respect to interrupt loss */
  23. #define WSM_CMD_START_TIMEOUT (7 * HZ)
  24. #define WSM_CMD_RESET_TIMEOUT (3 * HZ) /* 2 sec. timeout was observed. */
  25. #define WSM_CMD_MAX_TIMEOUT (3 * HZ)
  26. #define WSM_SKIP(buf, size) \
  27. do { \
  28. if ((buf)->data + size > (buf)->end) \
  29. goto underflow; \
  30. (buf)->data += size; \
  31. } while (0)
  32. #define WSM_GET(buf, ptr, size) \
  33. do { \
  34. if ((buf)->data + size > (buf)->end) \
  35. goto underflow; \
  36. memcpy(ptr, (buf)->data, size); \
  37. (buf)->data += size; \
  38. } while (0)
  39. #define __WSM_GET(buf, type, type2, cvt) \
  40. ({ \
  41. type val; \
  42. if ((buf)->data + sizeof(type) > (buf)->end) \
  43. goto underflow; \
  44. val = cvt(*(type2 *)(buf)->data); \
  45. (buf)->data += sizeof(type); \
  46. val; \
  47. })
  48. #define WSM_GET8(buf) __WSM_GET(buf, u8, u8, (u8))
  49. #define WSM_GET16(buf) __WSM_GET(buf, u16, __le16, __le16_to_cpu)
  50. #define WSM_GET32(buf) __WSM_GET(buf, u32, __le32, __le32_to_cpu)
  51. #define WSM_PUT(buf, ptr, size) \
  52. do { \
  53. if ((buf)->data + size > (buf)->end) \
  54. if (wsm_buf_reserve((buf), size)) \
  55. goto nomem; \
  56. memcpy((buf)->data, ptr, size); \
  57. (buf)->data += size; \
  58. } while (0)
  59. #define __WSM_PUT(buf, val, type, type2, cvt) \
  60. do { \
  61. if ((buf)->data + sizeof(type) > (buf)->end) \
  62. if (wsm_buf_reserve((buf), sizeof(type))) \
  63. goto nomem; \
  64. *(type2 *)(buf)->data = cvt(val); \
  65. (buf)->data += sizeof(type); \
  66. } while (0)
  67. #define WSM_PUT8(buf, val) __WSM_PUT(buf, val, u8, u8, (u8))
  68. #define WSM_PUT16(buf, val) __WSM_PUT(buf, val, u16, __le16, __cpu_to_le16)
  69. #define WSM_PUT32(buf, val) __WSM_PUT(buf, val, u32, __le32, __cpu_to_le32)
  70. static void wsm_buf_reset(struct wsm_buf *buf);
  71. static int wsm_buf_reserve(struct wsm_buf *buf, size_t extra_size);
  72. static int wsm_cmd_send(struct cw1200_common *priv,
  73. struct wsm_buf *buf,
  74. void *arg, u16 cmd, long tmo);
  75. #define wsm_cmd_lock(__priv) mutex_lock(&((__priv)->wsm_cmd_mux))
  76. #define wsm_cmd_unlock(__priv) mutex_unlock(&((__priv)->wsm_cmd_mux))
  77. /* ******************************************************************** */
  78. /* WSM API implementation */
  79. static int wsm_generic_confirm(struct cw1200_common *priv,
  80. void *arg,
  81. struct wsm_buf *buf)
  82. {
  83. u32 status = WSM_GET32(buf);
  84. if (status != WSM_STATUS_SUCCESS)
  85. return -EINVAL;
  86. return 0;
  87. underflow:
  88. WARN_ON(1);
  89. return -EINVAL;
  90. }
  91. int wsm_configuration(struct cw1200_common *priv, struct wsm_configuration *arg)
  92. {
  93. int ret;
  94. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  95. wsm_cmd_lock(priv);
  96. WSM_PUT32(buf, arg->dot11MaxTransmitMsduLifeTime);
  97. WSM_PUT32(buf, arg->dot11MaxReceiveLifeTime);
  98. WSM_PUT32(buf, arg->dot11RtsThreshold);
  99. /* DPD block. */
  100. WSM_PUT16(buf, arg->dpdData_size + 12);
  101. WSM_PUT16(buf, 1); /* DPD version */
  102. WSM_PUT(buf, arg->dot11StationId, ETH_ALEN);
  103. WSM_PUT16(buf, 5); /* DPD flags */
  104. WSM_PUT(buf, arg->dpdData, arg->dpdData_size);
  105. ret = wsm_cmd_send(priv, buf, arg,
  106. WSM_CONFIGURATION_REQ_ID, WSM_CMD_TIMEOUT);
  107. wsm_cmd_unlock(priv);
  108. return ret;
  109. nomem:
  110. wsm_cmd_unlock(priv);
  111. return -ENOMEM;
  112. }
  113. static int wsm_configuration_confirm(struct cw1200_common *priv,
  114. struct wsm_configuration *arg,
  115. struct wsm_buf *buf)
  116. {
  117. int i;
  118. int status;
  119. status = WSM_GET32(buf);
  120. if (WARN_ON(status != WSM_STATUS_SUCCESS))
  121. return -EINVAL;
  122. WSM_GET(buf, arg->dot11StationId, ETH_ALEN);
  123. arg->dot11FrequencyBandsSupported = WSM_GET8(buf);
  124. WSM_SKIP(buf, 1);
  125. arg->supportedRateMask = WSM_GET32(buf);
  126. for (i = 0; i < 2; ++i) {
  127. arg->txPowerRange[i].min_power_level = WSM_GET32(buf);
  128. arg->txPowerRange[i].max_power_level = WSM_GET32(buf);
  129. arg->txPowerRange[i].stepping = WSM_GET32(buf);
  130. }
  131. return 0;
  132. underflow:
  133. WARN_ON(1);
  134. return -EINVAL;
  135. }
  136. /* ******************************************************************** */
  137. int wsm_reset(struct cw1200_common *priv, const struct wsm_reset *arg)
  138. {
  139. int ret;
  140. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  141. u16 cmd = WSM_RESET_REQ_ID | WSM_TX_LINK_ID(arg->link_id);
  142. wsm_cmd_lock(priv);
  143. WSM_PUT32(buf, arg->reset_statistics ? 0 : 1);
  144. ret = wsm_cmd_send(priv, buf, NULL, cmd, WSM_CMD_RESET_TIMEOUT);
  145. wsm_cmd_unlock(priv);
  146. return ret;
  147. nomem:
  148. wsm_cmd_unlock(priv);
  149. return -ENOMEM;
  150. }
  151. /* ******************************************************************** */
  152. struct wsm_mib {
  153. u16 mib_id;
  154. void *buf;
  155. size_t buf_size;
  156. };
  157. int wsm_read_mib(struct cw1200_common *priv, u16 mib_id, void *_buf,
  158. size_t buf_size)
  159. {
  160. int ret;
  161. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  162. struct wsm_mib mib_buf = {
  163. .mib_id = mib_id,
  164. .buf = _buf,
  165. .buf_size = buf_size,
  166. };
  167. wsm_cmd_lock(priv);
  168. WSM_PUT16(buf, mib_id);
  169. WSM_PUT16(buf, 0);
  170. ret = wsm_cmd_send(priv, buf, &mib_buf,
  171. WSM_READ_MIB_REQ_ID, WSM_CMD_TIMEOUT);
  172. wsm_cmd_unlock(priv);
  173. return ret;
  174. nomem:
  175. wsm_cmd_unlock(priv);
  176. return -ENOMEM;
  177. }
  178. static int wsm_read_mib_confirm(struct cw1200_common *priv,
  179. struct wsm_mib *arg,
  180. struct wsm_buf *buf)
  181. {
  182. u16 size;
  183. if (WARN_ON(WSM_GET32(buf) != WSM_STATUS_SUCCESS))
  184. return -EINVAL;
  185. if (WARN_ON(WSM_GET16(buf) != arg->mib_id))
  186. return -EINVAL;
  187. size = WSM_GET16(buf);
  188. if (size > arg->buf_size)
  189. size = arg->buf_size;
  190. WSM_GET(buf, arg->buf, size);
  191. arg->buf_size = size;
  192. return 0;
  193. underflow:
  194. WARN_ON(1);
  195. return -EINVAL;
  196. }
  197. /* ******************************************************************** */
  198. int wsm_write_mib(struct cw1200_common *priv, u16 mib_id, void *_buf,
  199. size_t buf_size)
  200. {
  201. int ret;
  202. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  203. struct wsm_mib mib_buf = {
  204. .mib_id = mib_id,
  205. .buf = _buf,
  206. .buf_size = buf_size,
  207. };
  208. wsm_cmd_lock(priv);
  209. WSM_PUT16(buf, mib_id);
  210. WSM_PUT16(buf, buf_size);
  211. WSM_PUT(buf, _buf, buf_size);
  212. ret = wsm_cmd_send(priv, buf, &mib_buf,
  213. WSM_WRITE_MIB_REQ_ID, WSM_CMD_TIMEOUT);
  214. wsm_cmd_unlock(priv);
  215. return ret;
  216. nomem:
  217. wsm_cmd_unlock(priv);
  218. return -ENOMEM;
  219. }
  220. static int wsm_write_mib_confirm(struct cw1200_common *priv,
  221. struct wsm_mib *arg,
  222. struct wsm_buf *buf)
  223. {
  224. int ret;
  225. ret = wsm_generic_confirm(priv, arg, buf);
  226. if (ret)
  227. return ret;
  228. if (arg->mib_id == WSM_MIB_ID_OPERATIONAL_POWER_MODE) {
  229. /* OperationalMode: update PM status. */
  230. const char *p = arg->buf;
  231. cw1200_enable_powersave(priv, (p[0] & 0x0F) ? true : false);
  232. }
  233. return 0;
  234. }
  235. /* ******************************************************************** */
  236. int wsm_scan(struct cw1200_common *priv, const struct wsm_scan *arg)
  237. {
  238. int i;
  239. int ret;
  240. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  241. if (arg->num_channels > 48)
  242. return -EINVAL;
  243. if (arg->num_ssids > 2)
  244. return -EINVAL;
  245. if (arg->band > 1)
  246. return -EINVAL;
  247. wsm_cmd_lock(priv);
  248. WSM_PUT8(buf, arg->band);
  249. WSM_PUT8(buf, arg->type);
  250. WSM_PUT8(buf, arg->flags);
  251. WSM_PUT8(buf, arg->max_tx_rate);
  252. WSM_PUT32(buf, arg->auto_scan_interval);
  253. WSM_PUT8(buf, arg->num_probes);
  254. WSM_PUT8(buf, arg->num_channels);
  255. WSM_PUT8(buf, arg->num_ssids);
  256. WSM_PUT8(buf, arg->probe_delay);
  257. for (i = 0; i < arg->num_channels; ++i) {
  258. WSM_PUT16(buf, arg->ch[i].number);
  259. WSM_PUT16(buf, 0);
  260. WSM_PUT32(buf, arg->ch[i].min_chan_time);
  261. WSM_PUT32(buf, arg->ch[i].max_chan_time);
  262. WSM_PUT32(buf, 0);
  263. }
  264. for (i = 0; i < arg->num_ssids; ++i) {
  265. WSM_PUT32(buf, arg->ssids[i].length);
  266. WSM_PUT(buf, &arg->ssids[i].ssid[0],
  267. sizeof(arg->ssids[i].ssid));
  268. }
  269. ret = wsm_cmd_send(priv, buf, NULL,
  270. WSM_START_SCAN_REQ_ID, WSM_CMD_TIMEOUT);
  271. wsm_cmd_unlock(priv);
  272. return ret;
  273. nomem:
  274. wsm_cmd_unlock(priv);
  275. return -ENOMEM;
  276. }
  277. /* ******************************************************************** */
  278. int wsm_stop_scan(struct cw1200_common *priv)
  279. {
  280. int ret;
  281. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  282. wsm_cmd_lock(priv);
  283. ret = wsm_cmd_send(priv, buf, NULL,
  284. WSM_STOP_SCAN_REQ_ID, WSM_CMD_TIMEOUT);
  285. wsm_cmd_unlock(priv);
  286. return ret;
  287. }
  288. static int wsm_tx_confirm(struct cw1200_common *priv,
  289. struct wsm_buf *buf,
  290. int link_id)
  291. {
  292. struct wsm_tx_confirm tx_confirm;
  293. tx_confirm.packet_id = WSM_GET32(buf);
  294. tx_confirm.status = WSM_GET32(buf);
  295. tx_confirm.tx_rate = WSM_GET8(buf);
  296. tx_confirm.ack_failures = WSM_GET8(buf);
  297. tx_confirm.flags = WSM_GET16(buf);
  298. tx_confirm.media_delay = WSM_GET32(buf);
  299. tx_confirm.tx_queue_delay = WSM_GET32(buf);
  300. cw1200_tx_confirm_cb(priv, link_id, &tx_confirm);
  301. return 0;
  302. underflow:
  303. WARN_ON(1);
  304. return -EINVAL;
  305. }
  306. static int wsm_multi_tx_confirm(struct cw1200_common *priv,
  307. struct wsm_buf *buf, int link_id)
  308. {
  309. int ret;
  310. int count;
  311. count = WSM_GET32(buf);
  312. if (WARN_ON(count <= 0))
  313. return -EINVAL;
  314. if (count > 1) {
  315. /* We already released one buffer, now for the rest */
  316. ret = wsm_release_tx_buffer(priv, count - 1);
  317. if (ret < 0)
  318. return ret;
  319. else if (ret > 0)
  320. cw1200_bh_wakeup(priv);
  321. }
  322. cw1200_debug_txed_multi(priv, count);
  323. do {
  324. ret = wsm_tx_confirm(priv, buf, link_id);
  325. } while (!ret && --count);
  326. return ret;
  327. underflow:
  328. WARN_ON(1);
  329. return -EINVAL;
  330. }
  331. /* ******************************************************************** */
  332. static int wsm_join_confirm(struct cw1200_common *priv,
  333. struct wsm_join_cnf *arg,
  334. struct wsm_buf *buf)
  335. {
  336. arg->status = WSM_GET32(buf);
  337. if (WARN_ON(arg->status) != WSM_STATUS_SUCCESS)
  338. return -EINVAL;
  339. arg->min_power_level = WSM_GET32(buf);
  340. arg->max_power_level = WSM_GET32(buf);
  341. return 0;
  342. underflow:
  343. WARN_ON(1);
  344. return -EINVAL;
  345. }
  346. int wsm_join(struct cw1200_common *priv, struct wsm_join *arg)
  347. {
  348. int ret;
  349. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  350. struct wsm_join_cnf resp;
  351. wsm_cmd_lock(priv);
  352. WSM_PUT8(buf, arg->mode);
  353. WSM_PUT8(buf, arg->band);
  354. WSM_PUT16(buf, arg->channel_number);
  355. WSM_PUT(buf, &arg->bssid[0], sizeof(arg->bssid));
  356. WSM_PUT16(buf, arg->atim_window);
  357. WSM_PUT8(buf, arg->preamble_type);
  358. WSM_PUT8(buf, arg->probe_for_join);
  359. WSM_PUT8(buf, arg->dtim_period);
  360. WSM_PUT8(buf, arg->flags);
  361. WSM_PUT32(buf, arg->ssid_len);
  362. WSM_PUT(buf, &arg->ssid[0], sizeof(arg->ssid));
  363. WSM_PUT32(buf, arg->beacon_interval);
  364. WSM_PUT32(buf, arg->basic_rate_set);
  365. priv->tx_burst_idx = -1;
  366. ret = wsm_cmd_send(priv, buf, &resp,
  367. WSM_JOIN_REQ_ID, WSM_CMD_TIMEOUT);
  368. /* TODO: Update state based on resp.min|max_power_level */
  369. priv->join_complete_status = resp.status;
  370. wsm_cmd_unlock(priv);
  371. return ret;
  372. nomem:
  373. wsm_cmd_unlock(priv);
  374. return -ENOMEM;
  375. }
  376. /* ******************************************************************** */
  377. int wsm_set_bss_params(struct cw1200_common *priv,
  378. const struct wsm_set_bss_params *arg)
  379. {
  380. int ret;
  381. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  382. wsm_cmd_lock(priv);
  383. WSM_PUT8(buf, (arg->reset_beacon_loss ? 0x1 : 0));
  384. WSM_PUT8(buf, arg->beacon_lost_count);
  385. WSM_PUT16(buf, arg->aid);
  386. WSM_PUT32(buf, arg->operational_rate_set);
  387. ret = wsm_cmd_send(priv, buf, NULL,
  388. WSM_SET_BSS_PARAMS_REQ_ID, WSM_CMD_TIMEOUT);
  389. wsm_cmd_unlock(priv);
  390. return ret;
  391. nomem:
  392. wsm_cmd_unlock(priv);
  393. return -ENOMEM;
  394. }
  395. /* ******************************************************************** */
  396. int wsm_add_key(struct cw1200_common *priv, const struct wsm_add_key *arg)
  397. {
  398. int ret;
  399. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  400. wsm_cmd_lock(priv);
  401. WSM_PUT(buf, arg, sizeof(*arg));
  402. ret = wsm_cmd_send(priv, buf, NULL,
  403. WSM_ADD_KEY_REQ_ID, WSM_CMD_TIMEOUT);
  404. wsm_cmd_unlock(priv);
  405. return ret;
  406. nomem:
  407. wsm_cmd_unlock(priv);
  408. return -ENOMEM;
  409. }
  410. /* ******************************************************************** */
  411. int wsm_remove_key(struct cw1200_common *priv, const struct wsm_remove_key *arg)
  412. {
  413. int ret;
  414. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  415. wsm_cmd_lock(priv);
  416. WSM_PUT8(buf, arg->index);
  417. WSM_PUT8(buf, 0);
  418. WSM_PUT16(buf, 0);
  419. ret = wsm_cmd_send(priv, buf, NULL,
  420. WSM_REMOVE_KEY_REQ_ID, WSM_CMD_TIMEOUT);
  421. wsm_cmd_unlock(priv);
  422. return ret;
  423. nomem:
  424. wsm_cmd_unlock(priv);
  425. return -ENOMEM;
  426. }
  427. /* ******************************************************************** */
  428. int wsm_set_tx_queue_params(struct cw1200_common *priv,
  429. const struct wsm_set_tx_queue_params *arg, u8 id)
  430. {
  431. int ret;
  432. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  433. u8 queue_id_to_wmm_aci[] = {3, 2, 0, 1};
  434. wsm_cmd_lock(priv);
  435. WSM_PUT8(buf, queue_id_to_wmm_aci[id]);
  436. WSM_PUT8(buf, 0);
  437. WSM_PUT8(buf, arg->ackPolicy);
  438. WSM_PUT8(buf, 0);
  439. WSM_PUT32(buf, arg->maxTransmitLifetime);
  440. WSM_PUT16(buf, arg->allowedMediumTime);
  441. WSM_PUT16(buf, 0);
  442. ret = wsm_cmd_send(priv, buf, NULL, 0x0012, WSM_CMD_TIMEOUT);
  443. wsm_cmd_unlock(priv);
  444. return ret;
  445. nomem:
  446. wsm_cmd_unlock(priv);
  447. return -ENOMEM;
  448. }
  449. /* ******************************************************************** */
  450. int wsm_set_edca_params(struct cw1200_common *priv,
  451. const struct wsm_edca_params *arg)
  452. {
  453. int ret;
  454. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  455. wsm_cmd_lock(priv);
  456. /* Implemented according to specification. */
  457. WSM_PUT16(buf, arg->params[3].cwmin);
  458. WSM_PUT16(buf, arg->params[2].cwmin);
  459. WSM_PUT16(buf, arg->params[1].cwmin);
  460. WSM_PUT16(buf, arg->params[0].cwmin);
  461. WSM_PUT16(buf, arg->params[3].cwmax);
  462. WSM_PUT16(buf, arg->params[2].cwmax);
  463. WSM_PUT16(buf, arg->params[1].cwmax);
  464. WSM_PUT16(buf, arg->params[0].cwmax);
  465. WSM_PUT8(buf, arg->params[3].aifns);
  466. WSM_PUT8(buf, arg->params[2].aifns);
  467. WSM_PUT8(buf, arg->params[1].aifns);
  468. WSM_PUT8(buf, arg->params[0].aifns);
  469. WSM_PUT16(buf, arg->params[3].txop_limit);
  470. WSM_PUT16(buf, arg->params[2].txop_limit);
  471. WSM_PUT16(buf, arg->params[1].txop_limit);
  472. WSM_PUT16(buf, arg->params[0].txop_limit);
  473. WSM_PUT32(buf, arg->params[3].max_rx_lifetime);
  474. WSM_PUT32(buf, arg->params[2].max_rx_lifetime);
  475. WSM_PUT32(buf, arg->params[1].max_rx_lifetime);
  476. WSM_PUT32(buf, arg->params[0].max_rx_lifetime);
  477. ret = wsm_cmd_send(priv, buf, NULL,
  478. WSM_EDCA_PARAMS_REQ_ID, WSM_CMD_TIMEOUT);
  479. wsm_cmd_unlock(priv);
  480. return ret;
  481. nomem:
  482. wsm_cmd_unlock(priv);
  483. return -ENOMEM;
  484. }
  485. /* ******************************************************************** */
  486. int wsm_switch_channel(struct cw1200_common *priv,
  487. const struct wsm_switch_channel *arg)
  488. {
  489. int ret;
  490. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  491. wsm_cmd_lock(priv);
  492. WSM_PUT8(buf, arg->mode);
  493. WSM_PUT8(buf, arg->switch_count);
  494. WSM_PUT16(buf, arg->channel_number);
  495. priv->channel_switch_in_progress = 1;
  496. ret = wsm_cmd_send(priv, buf, NULL,
  497. WSM_SWITCH_CHANNEL_REQ_ID, WSM_CMD_TIMEOUT);
  498. if (ret)
  499. priv->channel_switch_in_progress = 0;
  500. wsm_cmd_unlock(priv);
  501. return ret;
  502. nomem:
  503. wsm_cmd_unlock(priv);
  504. return -ENOMEM;
  505. }
  506. /* ******************************************************************** */
  507. int wsm_set_pm(struct cw1200_common *priv, const struct wsm_set_pm *arg)
  508. {
  509. int ret;
  510. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  511. priv->ps_mode_switch_in_progress = 1;
  512. wsm_cmd_lock(priv);
  513. WSM_PUT8(buf, arg->mode);
  514. WSM_PUT8(buf, arg->fast_psm_idle_period);
  515. WSM_PUT8(buf, arg->ap_psm_change_period);
  516. WSM_PUT8(buf, arg->min_auto_pspoll_period);
  517. ret = wsm_cmd_send(priv, buf, NULL,
  518. WSM_SET_PM_REQ_ID, WSM_CMD_TIMEOUT);
  519. wsm_cmd_unlock(priv);
  520. return ret;
  521. nomem:
  522. wsm_cmd_unlock(priv);
  523. return -ENOMEM;
  524. }
  525. /* ******************************************************************** */
  526. int wsm_start(struct cw1200_common *priv, const struct wsm_start *arg)
  527. {
  528. int ret;
  529. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  530. wsm_cmd_lock(priv);
  531. WSM_PUT8(buf, arg->mode);
  532. WSM_PUT8(buf, arg->band);
  533. WSM_PUT16(buf, arg->channel_number);
  534. WSM_PUT32(buf, arg->ct_window);
  535. WSM_PUT32(buf, arg->beacon_interval);
  536. WSM_PUT8(buf, arg->dtim_period);
  537. WSM_PUT8(buf, arg->preamble);
  538. WSM_PUT8(buf, arg->probe_delay);
  539. WSM_PUT8(buf, arg->ssid_len);
  540. WSM_PUT(buf, arg->ssid, sizeof(arg->ssid));
  541. WSM_PUT32(buf, arg->basic_rate_set);
  542. priv->tx_burst_idx = -1;
  543. ret = wsm_cmd_send(priv, buf, NULL,
  544. WSM_START_REQ_ID, WSM_CMD_START_TIMEOUT);
  545. wsm_cmd_unlock(priv);
  546. return ret;
  547. nomem:
  548. wsm_cmd_unlock(priv);
  549. return -ENOMEM;
  550. }
  551. /* ******************************************************************** */
  552. int wsm_beacon_transmit(struct cw1200_common *priv,
  553. const struct wsm_beacon_transmit *arg)
  554. {
  555. int ret;
  556. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  557. wsm_cmd_lock(priv);
  558. WSM_PUT32(buf, arg->enable_beaconing ? 1 : 0);
  559. ret = wsm_cmd_send(priv, buf, NULL,
  560. WSM_BEACON_TRANSMIT_REQ_ID, WSM_CMD_TIMEOUT);
  561. wsm_cmd_unlock(priv);
  562. return ret;
  563. nomem:
  564. wsm_cmd_unlock(priv);
  565. return -ENOMEM;
  566. }
  567. /* ******************************************************************** */
  568. int wsm_start_find(struct cw1200_common *priv)
  569. {
  570. int ret;
  571. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  572. wsm_cmd_lock(priv);
  573. ret = wsm_cmd_send(priv, buf, NULL, 0x0019, WSM_CMD_TIMEOUT);
  574. wsm_cmd_unlock(priv);
  575. return ret;
  576. }
  577. /* ******************************************************************** */
  578. int wsm_stop_find(struct cw1200_common *priv)
  579. {
  580. int ret;
  581. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  582. wsm_cmd_lock(priv);
  583. ret = wsm_cmd_send(priv, buf, NULL, 0x001A, WSM_CMD_TIMEOUT);
  584. wsm_cmd_unlock(priv);
  585. return ret;
  586. }
  587. /* ******************************************************************** */
  588. int wsm_map_link(struct cw1200_common *priv, const struct wsm_map_link *arg)
  589. {
  590. int ret;
  591. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  592. u16 cmd = 0x001C | WSM_TX_LINK_ID(arg->link_id);
  593. wsm_cmd_lock(priv);
  594. WSM_PUT(buf, &arg->mac_addr[0], sizeof(arg->mac_addr));
  595. WSM_PUT16(buf, 0);
  596. ret = wsm_cmd_send(priv, buf, NULL, cmd, WSM_CMD_TIMEOUT);
  597. wsm_cmd_unlock(priv);
  598. return ret;
  599. nomem:
  600. wsm_cmd_unlock(priv);
  601. return -ENOMEM;
  602. }
  603. /* ******************************************************************** */
  604. int wsm_update_ie(struct cw1200_common *priv,
  605. const struct wsm_update_ie *arg)
  606. {
  607. int ret;
  608. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  609. wsm_cmd_lock(priv);
  610. WSM_PUT16(buf, arg->what);
  611. WSM_PUT16(buf, arg->count);
  612. WSM_PUT(buf, arg->ies, arg->length);
  613. ret = wsm_cmd_send(priv, buf, NULL, 0x001B, WSM_CMD_TIMEOUT);
  614. wsm_cmd_unlock(priv);
  615. return ret;
  616. nomem:
  617. wsm_cmd_unlock(priv);
  618. return -ENOMEM;
  619. }
  620. /* ******************************************************************** */
  621. int wsm_set_probe_responder(struct cw1200_common *priv, bool enable)
  622. {
  623. priv->rx_filter.probeResponder = enable;
  624. return wsm_set_rx_filter(priv, &priv->rx_filter);
  625. }
  626. /* ******************************************************************** */
  627. /* WSM indication events implementation */
  628. const char * const cw1200_fw_types[] = {
  629. "ETF",
  630. "WFM",
  631. "WSM",
  632. "HI test",
  633. "Platform test"
  634. };
  635. static int wsm_startup_indication(struct cw1200_common *priv,
  636. struct wsm_buf *buf)
  637. {
  638. priv->wsm_caps.input_buffers = WSM_GET16(buf);
  639. priv->wsm_caps.input_buffer_size = WSM_GET16(buf);
  640. priv->wsm_caps.hw_id = WSM_GET16(buf);
  641. priv->wsm_caps.hw_subid = WSM_GET16(buf);
  642. priv->wsm_caps.status = WSM_GET16(buf);
  643. priv->wsm_caps.fw_cap = WSM_GET16(buf);
  644. priv->wsm_caps.fw_type = WSM_GET16(buf);
  645. priv->wsm_caps.fw_api = WSM_GET16(buf);
  646. priv->wsm_caps.fw_build = WSM_GET16(buf);
  647. priv->wsm_caps.fw_ver = WSM_GET16(buf);
  648. WSM_GET(buf, priv->wsm_caps.fw_label, sizeof(priv->wsm_caps.fw_label));
  649. priv->wsm_caps.fw_label[sizeof(priv->wsm_caps.fw_label) - 1] = 0; /* Do not trust FW too much... */
  650. if (WARN_ON(priv->wsm_caps.status))
  651. return -EINVAL;
  652. if (WARN_ON(priv->wsm_caps.fw_type > 4))
  653. return -EINVAL;
  654. pr_info("CW1200 WSM init done.\n"
  655. " Input buffers: %d x %d bytes\n"
  656. " Hardware: %d.%d\n"
  657. " %s firmware [%s], ver: %d, build: %d,"
  658. " api: %d, cap: 0x%.4X\n",
  659. priv->wsm_caps.input_buffers,
  660. priv->wsm_caps.input_buffer_size,
  661. priv->wsm_caps.hw_id, priv->wsm_caps.hw_subid,
  662. cw1200_fw_types[priv->wsm_caps.fw_type],
  663. priv->wsm_caps.fw_label, priv->wsm_caps.fw_ver,
  664. priv->wsm_caps.fw_build,
  665. priv->wsm_caps.fw_api, priv->wsm_caps.fw_cap);
  666. /* Disable unsupported frequency bands */
  667. if (!(priv->wsm_caps.fw_cap & 0x1))
  668. priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
  669. if (!(priv->wsm_caps.fw_cap & 0x2))
  670. priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
  671. priv->firmware_ready = 1;
  672. wake_up(&priv->wsm_startup_done);
  673. return 0;
  674. underflow:
  675. WARN_ON(1);
  676. return -EINVAL;
  677. }
  678. static int wsm_receive_indication(struct cw1200_common *priv,
  679. int link_id,
  680. struct wsm_buf *buf,
  681. struct sk_buff **skb_p)
  682. {
  683. struct wsm_rx rx;
  684. struct ieee80211_hdr *hdr;
  685. size_t hdr_len;
  686. __le16 fctl;
  687. rx.status = WSM_GET32(buf);
  688. rx.channel_number = WSM_GET16(buf);
  689. rx.rx_rate = WSM_GET8(buf);
  690. rx.rcpi_rssi = WSM_GET8(buf);
  691. rx.flags = WSM_GET32(buf);
  692. /* FW Workaround: Drop probe resp or
  693. beacon when RSSI is 0
  694. */
  695. hdr = (struct ieee80211_hdr *)(*skb_p)->data;
  696. if (!rx.rcpi_rssi &&
  697. (ieee80211_is_probe_resp(hdr->frame_control) ||
  698. ieee80211_is_beacon(hdr->frame_control)))
  699. return 0;
  700. /* If no RSSI subscription has been made,
  701. * convert RCPI to RSSI here
  702. */
  703. if (!priv->cqm_use_rssi)
  704. rx.rcpi_rssi = rx.rcpi_rssi / 2 - 110;
  705. fctl = *(__le16 *)buf->data;
  706. hdr_len = buf->data - buf->begin;
  707. skb_pull(*skb_p, hdr_len);
  708. if (!rx.status && ieee80211_is_deauth(fctl)) {
  709. if (priv->join_status == CW1200_JOIN_STATUS_STA) {
  710. /* Shedule unjoin work */
  711. pr_debug("[WSM] Issue unjoin command (RX).\n");
  712. wsm_lock_tx_async(priv);
  713. if (queue_work(priv->workqueue,
  714. &priv->unjoin_work) <= 0)
  715. wsm_unlock_tx(priv);
  716. }
  717. }
  718. cw1200_rx_cb(priv, &rx, link_id, skb_p);
  719. if (*skb_p)
  720. skb_push(*skb_p, hdr_len);
  721. return 0;
  722. underflow:
  723. return -EINVAL;
  724. }
  725. static int wsm_event_indication(struct cw1200_common *priv, struct wsm_buf *buf)
  726. {
  727. int first;
  728. struct cw1200_wsm_event *event;
  729. if (priv->mode == NL80211_IFTYPE_UNSPECIFIED) {
  730. /* STA is stopped. */
  731. return 0;
  732. }
  733. event = kzalloc(sizeof(struct cw1200_wsm_event), GFP_KERNEL);
  734. if (!event)
  735. return -ENOMEM;
  736. event->evt.id = WSM_GET32(buf);
  737. event->evt.data = WSM_GET32(buf);
  738. pr_debug("[WSM] Event: %d(%d)\n",
  739. event->evt.id, event->evt.data);
  740. spin_lock(&priv->event_queue_lock);
  741. first = list_empty(&priv->event_queue);
  742. list_add_tail(&event->link, &priv->event_queue);
  743. spin_unlock(&priv->event_queue_lock);
  744. if (first)
  745. queue_work(priv->workqueue, &priv->event_handler);
  746. return 0;
  747. underflow:
  748. kfree(event);
  749. return -EINVAL;
  750. }
  751. static int wsm_channel_switch_indication(struct cw1200_common *priv,
  752. struct wsm_buf *buf)
  753. {
  754. WARN_ON(WSM_GET32(buf));
  755. priv->channel_switch_in_progress = 0;
  756. wake_up(&priv->channel_switch_done);
  757. wsm_unlock_tx(priv);
  758. return 0;
  759. underflow:
  760. return -EINVAL;
  761. }
  762. static int wsm_set_pm_indication(struct cw1200_common *priv,
  763. struct wsm_buf *buf)
  764. {
  765. /* TODO: Check buf (struct wsm_set_pm_complete) for validity */
  766. if (priv->ps_mode_switch_in_progress) {
  767. priv->ps_mode_switch_in_progress = 0;
  768. wake_up(&priv->ps_mode_switch_done);
  769. }
  770. return 0;
  771. }
  772. static int wsm_scan_started(struct cw1200_common *priv, void *arg,
  773. struct wsm_buf *buf)
  774. {
  775. u32 status = WSM_GET32(buf);
  776. if (status != WSM_STATUS_SUCCESS) {
  777. cw1200_scan_failed_cb(priv);
  778. return -EINVAL;
  779. }
  780. return 0;
  781. underflow:
  782. WARN_ON(1);
  783. return -EINVAL;
  784. }
  785. static int wsm_scan_complete_indication(struct cw1200_common *priv,
  786. struct wsm_buf *buf)
  787. {
  788. struct wsm_scan_complete arg;
  789. arg.status = WSM_GET32(buf);
  790. arg.psm = WSM_GET8(buf);
  791. arg.num_channels = WSM_GET8(buf);
  792. cw1200_scan_complete_cb(priv, &arg);
  793. return 0;
  794. underflow:
  795. return -EINVAL;
  796. }
  797. static int wsm_join_complete_indication(struct cw1200_common *priv,
  798. struct wsm_buf *buf)
  799. {
  800. struct wsm_join_complete arg;
  801. arg.status = WSM_GET32(buf);
  802. pr_debug("[WSM] Join complete indication, status: %d\n", arg.status);
  803. cw1200_join_complete_cb(priv, &arg);
  804. return 0;
  805. underflow:
  806. return -EINVAL;
  807. }
  808. static int wsm_find_complete_indication(struct cw1200_common *priv,
  809. struct wsm_buf *buf)
  810. {
  811. pr_warn("Implement find_complete_indication\n");
  812. return 0;
  813. }
  814. static int wsm_ba_timeout_indication(struct cw1200_common *priv,
  815. struct wsm_buf *buf)
  816. {
  817. u32 dummy;
  818. u8 tid;
  819. u8 dummy2;
  820. u8 addr[ETH_ALEN];
  821. dummy = WSM_GET32(buf);
  822. tid = WSM_GET8(buf);
  823. dummy2 = WSM_GET8(buf);
  824. WSM_GET(buf, addr, ETH_ALEN);
  825. pr_info("BlockACK timeout, tid %d, addr %pM\n",
  826. tid, addr);
  827. return 0;
  828. underflow:
  829. return -EINVAL;
  830. }
  831. static int wsm_suspend_resume_indication(struct cw1200_common *priv,
  832. int link_id, struct wsm_buf *buf)
  833. {
  834. u32 flags;
  835. struct wsm_suspend_resume arg;
  836. flags = WSM_GET32(buf);
  837. arg.link_id = link_id;
  838. arg.stop = !(flags & 1);
  839. arg.multicast = !!(flags & 8);
  840. arg.queue = (flags >> 1) & 3;
  841. cw1200_suspend_resume(priv, &arg);
  842. return 0;
  843. underflow:
  844. return -EINVAL;
  845. }
  846. /* ******************************************************************** */
  847. /* WSM TX */
  848. static int wsm_cmd_send(struct cw1200_common *priv,
  849. struct wsm_buf *buf,
  850. void *arg, u16 cmd, long tmo)
  851. {
  852. size_t buf_len = buf->data - buf->begin;
  853. int ret;
  854. /* Don't bother if we're dead. */
  855. if (priv->bh_error) {
  856. ret = 0;
  857. goto done;
  858. }
  859. /* Block until the cmd buffer is completed. Tortuous. */
  860. spin_lock(&priv->wsm_cmd.lock);
  861. while (!priv->wsm_cmd.done) {
  862. spin_unlock(&priv->wsm_cmd.lock);
  863. spin_lock(&priv->wsm_cmd.lock);
  864. }
  865. priv->wsm_cmd.done = 0;
  866. spin_unlock(&priv->wsm_cmd.lock);
  867. if (cmd == WSM_WRITE_MIB_REQ_ID ||
  868. cmd == WSM_READ_MIB_REQ_ID)
  869. pr_debug("[WSM] >>> 0x%.4X [MIB: 0x%.4X] (%zu)\n",
  870. cmd, __le16_to_cpu(((__le16 *)buf->begin)[2]),
  871. buf_len);
  872. else
  873. pr_debug("[WSM] >>> 0x%.4X (%zu)\n", cmd, buf_len);
  874. /* Due to buggy SPI on CW1200, we need to
  875. * pad the message by a few bytes to ensure
  876. * that it's completely received.
  877. */
  878. buf_len += 4;
  879. /* Fill HI message header */
  880. /* BH will add sequence number */
  881. ((__le16 *)buf->begin)[0] = __cpu_to_le16(buf_len);
  882. ((__le16 *)buf->begin)[1] = __cpu_to_le16(cmd);
  883. spin_lock(&priv->wsm_cmd.lock);
  884. BUG_ON(priv->wsm_cmd.ptr);
  885. priv->wsm_cmd.ptr = buf->begin;
  886. priv->wsm_cmd.len = buf_len;
  887. priv->wsm_cmd.arg = arg;
  888. priv->wsm_cmd.cmd = cmd;
  889. spin_unlock(&priv->wsm_cmd.lock);
  890. cw1200_bh_wakeup(priv);
  891. /* Wait for command completion */
  892. ret = wait_event_timeout(priv->wsm_cmd_wq,
  893. priv->wsm_cmd.done, tmo);
  894. if (!ret && !priv->wsm_cmd.done) {
  895. spin_lock(&priv->wsm_cmd.lock);
  896. priv->wsm_cmd.done = 1;
  897. priv->wsm_cmd.ptr = NULL;
  898. spin_unlock(&priv->wsm_cmd.lock);
  899. if (priv->bh_error) {
  900. /* Return ok to help system cleanup */
  901. ret = 0;
  902. } else {
  903. pr_err("CMD req (0x%04x) stuck in firmware, killing BH\n", priv->wsm_cmd.cmd);
  904. print_hex_dump_bytes("REQDUMP: ", DUMP_PREFIX_NONE,
  905. buf->begin, buf_len);
  906. pr_err("Outstanding outgoing frames: %d\n", priv->hw_bufs_used);
  907. /* Kill BH thread to report the error to the top layer. */
  908. atomic_add(1, &priv->bh_term);
  909. wake_up(&priv->bh_wq);
  910. ret = -ETIMEDOUT;
  911. }
  912. } else {
  913. spin_lock(&priv->wsm_cmd.lock);
  914. BUG_ON(!priv->wsm_cmd.done);
  915. ret = priv->wsm_cmd.ret;
  916. spin_unlock(&priv->wsm_cmd.lock);
  917. }
  918. done:
  919. wsm_buf_reset(buf);
  920. return ret;
  921. }
  922. /* ******************************************************************** */
  923. /* WSM TX port control */
  924. void wsm_lock_tx(struct cw1200_common *priv)
  925. {
  926. wsm_cmd_lock(priv);
  927. if (atomic_add_return(1, &priv->tx_lock) == 1) {
  928. if (wsm_flush_tx(priv))
  929. pr_debug("[WSM] TX is locked.\n");
  930. }
  931. wsm_cmd_unlock(priv);
  932. }
  933. void wsm_lock_tx_async(struct cw1200_common *priv)
  934. {
  935. if (atomic_add_return(1, &priv->tx_lock) == 1)
  936. pr_debug("[WSM] TX is locked (async).\n");
  937. }
  938. bool wsm_flush_tx(struct cw1200_common *priv)
  939. {
  940. unsigned long timestamp = jiffies;
  941. bool pending = false;
  942. long timeout;
  943. int i;
  944. /* Flush must be called with TX lock held. */
  945. BUG_ON(!atomic_read(&priv->tx_lock));
  946. /* First check if we really need to do something.
  947. * It is safe to use unprotected access, as hw_bufs_used
  948. * can only decrements.
  949. */
  950. if (!priv->hw_bufs_used)
  951. return true;
  952. if (priv->bh_error) {
  953. /* In case of failure do not wait for magic. */
  954. pr_err("[WSM] Fatal error occurred, will not flush TX.\n");
  955. return false;
  956. } else {
  957. /* Get a timestamp of "oldest" frame */
  958. for (i = 0; i < 4; ++i)
  959. pending |= cw1200_queue_get_xmit_timestamp(
  960. &priv->tx_queue[i],
  961. &timestamp, 0xffffffff);
  962. /* If there's nothing pending, we're good */
  963. if (!pending)
  964. return true;
  965. timeout = timestamp + WSM_CMD_LAST_CHANCE_TIMEOUT - jiffies;
  966. if (timeout < 0 || wait_event_timeout(priv->bh_evt_wq,
  967. !priv->hw_bufs_used,
  968. timeout) <= 0) {
  969. /* Hmmm... Not good. Frame had stuck in firmware. */
  970. priv->bh_error = 1;
  971. wiphy_err(priv->hw->wiphy, "[WSM] TX Frames (%d) stuck in firmware, killing BH\n", priv->hw_bufs_used);
  972. wake_up(&priv->bh_wq);
  973. return false;
  974. }
  975. /* Ok, everything is flushed. */
  976. return true;
  977. }
  978. }
  979. void wsm_unlock_tx(struct cw1200_common *priv)
  980. {
  981. int tx_lock;
  982. tx_lock = atomic_sub_return(1, &priv->tx_lock);
  983. BUG_ON(tx_lock < 0);
  984. if (tx_lock == 0) {
  985. if (!priv->bh_error)
  986. cw1200_bh_wakeup(priv);
  987. pr_debug("[WSM] TX is unlocked.\n");
  988. }
  989. }
  990. /* ******************************************************************** */
  991. /* WSM RX */
  992. int wsm_handle_exception(struct cw1200_common *priv, u8 *data, size_t len)
  993. {
  994. struct wsm_buf buf;
  995. u32 reason;
  996. u32 reg[18];
  997. char fname[48];
  998. unsigned int i;
  999. static const char * const reason_str[] = {
  1000. "undefined instruction",
  1001. "prefetch abort",
  1002. "data abort",
  1003. "unknown error",
  1004. };
  1005. buf.begin = buf.data = data;
  1006. buf.end = &buf.begin[len];
  1007. reason = WSM_GET32(&buf);
  1008. for (i = 0; i < ARRAY_SIZE(reg); ++i)
  1009. reg[i] = WSM_GET32(&buf);
  1010. WSM_GET(&buf, fname, sizeof(fname));
  1011. if (reason < 4)
  1012. wiphy_err(priv->hw->wiphy,
  1013. "Firmware exception: %s.\n",
  1014. reason_str[reason]);
  1015. else
  1016. wiphy_err(priv->hw->wiphy,
  1017. "Firmware assert at %.*s, line %d\n",
  1018. (int) sizeof(fname), fname, reg[1]);
  1019. for (i = 0; i < 12; i += 4)
  1020. wiphy_err(priv->hw->wiphy,
  1021. "R%d: 0x%.8X, R%d: 0x%.8X, R%d: 0x%.8X, R%d: 0x%.8X,\n",
  1022. i + 0, reg[i + 0], i + 1, reg[i + 1],
  1023. i + 2, reg[i + 2], i + 3, reg[i + 3]);
  1024. wiphy_err(priv->hw->wiphy,
  1025. "R12: 0x%.8X, SP: 0x%.8X, LR: 0x%.8X, PC: 0x%.8X,\n",
  1026. reg[i + 0], reg[i + 1], reg[i + 2], reg[i + 3]);
  1027. i += 4;
  1028. wiphy_err(priv->hw->wiphy,
  1029. "CPSR: 0x%.8X, SPSR: 0x%.8X\n",
  1030. reg[i + 0], reg[i + 1]);
  1031. print_hex_dump_bytes("R1: ", DUMP_PREFIX_NONE,
  1032. fname, sizeof(fname));
  1033. return 0;
  1034. underflow:
  1035. wiphy_err(priv->hw->wiphy, "Firmware exception.\n");
  1036. print_hex_dump_bytes("Exception: ", DUMP_PREFIX_NONE,
  1037. data, len);
  1038. return -EINVAL;
  1039. }
  1040. int wsm_handle_rx(struct cw1200_common *priv, u16 id,
  1041. struct wsm_hdr *wsm, struct sk_buff **skb_p)
  1042. {
  1043. int ret = 0;
  1044. struct wsm_buf wsm_buf;
  1045. int link_id = (id >> 6) & 0x0F;
  1046. /* Strip link id. */
  1047. id &= ~WSM_TX_LINK_ID(WSM_TX_LINK_ID_MAX);
  1048. wsm_buf.begin = (u8 *)&wsm[0];
  1049. wsm_buf.data = (u8 *)&wsm[1];
  1050. wsm_buf.end = &wsm_buf.begin[__le16_to_cpu(wsm->len)];
  1051. pr_debug("[WSM] <<< 0x%.4X (%td)\n", id,
  1052. wsm_buf.end - wsm_buf.begin);
  1053. if (id == WSM_TX_CONFIRM_IND_ID) {
  1054. ret = wsm_tx_confirm(priv, &wsm_buf, link_id);
  1055. } else if (id == WSM_MULTI_TX_CONFIRM_ID) {
  1056. ret = wsm_multi_tx_confirm(priv, &wsm_buf, link_id);
  1057. } else if (id & 0x0400) {
  1058. void *wsm_arg;
  1059. u16 wsm_cmd;
  1060. /* Do not trust FW too much. Protection against repeated
  1061. * response and race condition removal (see above).
  1062. */
  1063. spin_lock(&priv->wsm_cmd.lock);
  1064. wsm_arg = priv->wsm_cmd.arg;
  1065. wsm_cmd = priv->wsm_cmd.cmd &
  1066. ~WSM_TX_LINK_ID(WSM_TX_LINK_ID_MAX);
  1067. priv->wsm_cmd.cmd = 0xFFFF;
  1068. spin_unlock(&priv->wsm_cmd.lock);
  1069. if (WARN_ON((id & ~0x0400) != wsm_cmd)) {
  1070. /* Note that any non-zero is a fatal retcode. */
  1071. ret = -EINVAL;
  1072. goto out;
  1073. }
  1074. /* Note that wsm_arg can be NULL in case of timeout in
  1075. * wsm_cmd_send().
  1076. */
  1077. switch (id) {
  1078. case WSM_READ_MIB_RESP_ID:
  1079. if (wsm_arg)
  1080. ret = wsm_read_mib_confirm(priv, wsm_arg,
  1081. &wsm_buf);
  1082. break;
  1083. case WSM_WRITE_MIB_RESP_ID:
  1084. if (wsm_arg)
  1085. ret = wsm_write_mib_confirm(priv, wsm_arg,
  1086. &wsm_buf);
  1087. break;
  1088. case WSM_START_SCAN_RESP_ID:
  1089. if (wsm_arg)
  1090. ret = wsm_scan_started(priv, wsm_arg, &wsm_buf);
  1091. break;
  1092. case WSM_CONFIGURATION_RESP_ID:
  1093. if (wsm_arg)
  1094. ret = wsm_configuration_confirm(priv, wsm_arg,
  1095. &wsm_buf);
  1096. break;
  1097. case WSM_JOIN_RESP_ID:
  1098. if (wsm_arg)
  1099. ret = wsm_join_confirm(priv, wsm_arg, &wsm_buf);
  1100. break;
  1101. case WSM_STOP_SCAN_RESP_ID:
  1102. case WSM_RESET_RESP_ID:
  1103. case WSM_ADD_KEY_RESP_ID:
  1104. case WSM_REMOVE_KEY_RESP_ID:
  1105. case WSM_SET_PM_RESP_ID:
  1106. case WSM_SET_BSS_PARAMS_RESP_ID:
  1107. case 0x0412: /* set_tx_queue_params */
  1108. case WSM_EDCA_PARAMS_RESP_ID:
  1109. case WSM_SWITCH_CHANNEL_RESP_ID:
  1110. case WSM_START_RESP_ID:
  1111. case WSM_BEACON_TRANSMIT_RESP_ID:
  1112. case 0x0419: /* start_find */
  1113. case 0x041A: /* stop_find */
  1114. case 0x041B: /* update_ie */
  1115. case 0x041C: /* map_link */
  1116. WARN_ON(wsm_arg != NULL);
  1117. ret = wsm_generic_confirm(priv, wsm_arg, &wsm_buf);
  1118. if (ret) {
  1119. wiphy_warn(priv->hw->wiphy,
  1120. "wsm_generic_confirm failed for request 0x%04x.\n",
  1121. id & ~0x0400);
  1122. /* often 0x407 and 0x410 occur, this means we're dead.. */
  1123. if (priv->join_status >= CW1200_JOIN_STATUS_JOINING) {
  1124. wsm_lock_tx(priv);
  1125. if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
  1126. wsm_unlock_tx(priv);
  1127. }
  1128. }
  1129. break;
  1130. default:
  1131. wiphy_warn(priv->hw->wiphy,
  1132. "Unrecognized confirmation 0x%04x\n",
  1133. id & ~0x0400);
  1134. }
  1135. spin_lock(&priv->wsm_cmd.lock);
  1136. priv->wsm_cmd.ret = ret;
  1137. priv->wsm_cmd.done = 1;
  1138. spin_unlock(&priv->wsm_cmd.lock);
  1139. ret = 0; /* Error response from device should ne stop BH. */
  1140. wake_up(&priv->wsm_cmd_wq);
  1141. } else if (id & 0x0800) {
  1142. switch (id) {
  1143. case WSM_STARTUP_IND_ID:
  1144. ret = wsm_startup_indication(priv, &wsm_buf);
  1145. break;
  1146. case WSM_RECEIVE_IND_ID:
  1147. ret = wsm_receive_indication(priv, link_id,
  1148. &wsm_buf, skb_p);
  1149. break;
  1150. case 0x0805:
  1151. ret = wsm_event_indication(priv, &wsm_buf);
  1152. break;
  1153. case WSM_SCAN_COMPLETE_IND_ID:
  1154. ret = wsm_scan_complete_indication(priv, &wsm_buf);
  1155. break;
  1156. case 0x0808:
  1157. ret = wsm_ba_timeout_indication(priv, &wsm_buf);
  1158. break;
  1159. case 0x0809:
  1160. ret = wsm_set_pm_indication(priv, &wsm_buf);
  1161. break;
  1162. case 0x080A:
  1163. ret = wsm_channel_switch_indication(priv, &wsm_buf);
  1164. break;
  1165. case 0x080B:
  1166. ret = wsm_find_complete_indication(priv, &wsm_buf);
  1167. break;
  1168. case 0x080C:
  1169. ret = wsm_suspend_resume_indication(priv,
  1170. link_id, &wsm_buf);
  1171. break;
  1172. case 0x080F:
  1173. ret = wsm_join_complete_indication(priv, &wsm_buf);
  1174. break;
  1175. default:
  1176. pr_warn("Unrecognised WSM ID %04x\n", id);
  1177. }
  1178. } else {
  1179. WARN_ON(1);
  1180. ret = -EINVAL;
  1181. }
  1182. out:
  1183. return ret;
  1184. }
  1185. static bool wsm_handle_tx_data(struct cw1200_common *priv,
  1186. struct wsm_tx *wsm,
  1187. const struct ieee80211_tx_info *tx_info,
  1188. const struct cw1200_txpriv *txpriv,
  1189. struct cw1200_queue *queue)
  1190. {
  1191. bool handled = false;
  1192. const struct ieee80211_hdr *frame =
  1193. (struct ieee80211_hdr *)&((u8 *)wsm)[txpriv->offset];
  1194. __le16 fctl = frame->frame_control;
  1195. enum {
  1196. do_probe,
  1197. do_drop,
  1198. do_wep,
  1199. do_tx,
  1200. } action = do_tx;
  1201. switch (priv->mode) {
  1202. case NL80211_IFTYPE_STATION:
  1203. if (priv->join_status == CW1200_JOIN_STATUS_MONITOR)
  1204. action = do_tx;
  1205. else if (priv->join_status < CW1200_JOIN_STATUS_PRE_STA)
  1206. action = do_drop;
  1207. break;
  1208. case NL80211_IFTYPE_AP:
  1209. if (!priv->join_status) {
  1210. action = do_drop;
  1211. } else if (!(BIT(txpriv->raw_link_id) &
  1212. (BIT(0) | priv->link_id_map))) {
  1213. wiphy_warn(priv->hw->wiphy,
  1214. "A frame with expired link id is dropped.\n");
  1215. action = do_drop;
  1216. }
  1217. if (cw1200_queue_get_generation(wsm->packet_id) >
  1218. CW1200_MAX_REQUEUE_ATTEMPTS) {
  1219. /* HACK!!! WSM324 firmware has tendency to requeue
  1220. * multicast frames in a loop, causing performance
  1221. * drop and high power consumption of the driver.
  1222. * In this situation it is better just to drop
  1223. * the problematic frame.
  1224. */
  1225. wiphy_warn(priv->hw->wiphy,
  1226. "Too many attempts to requeue a frame; dropped.\n");
  1227. action = do_drop;
  1228. }
  1229. break;
  1230. case NL80211_IFTYPE_ADHOC:
  1231. if (priv->join_status != CW1200_JOIN_STATUS_IBSS)
  1232. action = do_drop;
  1233. break;
  1234. case NL80211_IFTYPE_MESH_POINT:
  1235. action = do_tx; /* TODO: Test me! */
  1236. break;
  1237. case NL80211_IFTYPE_MONITOR:
  1238. default:
  1239. action = do_drop;
  1240. break;
  1241. }
  1242. if (action == do_tx) {
  1243. if (ieee80211_is_nullfunc(fctl)) {
  1244. spin_lock(&priv->bss_loss_lock);
  1245. if (priv->bss_loss_state) {
  1246. priv->bss_loss_confirm_id = wsm->packet_id;
  1247. wsm->queue_id = WSM_QUEUE_VOICE;
  1248. }
  1249. spin_unlock(&priv->bss_loss_lock);
  1250. } else if (ieee80211_is_probe_req(fctl)) {
  1251. action = do_probe;
  1252. } else if (ieee80211_is_deauth(fctl) &&
  1253. priv->mode != NL80211_IFTYPE_AP) {
  1254. pr_debug("[WSM] Issue unjoin command due to tx deauth.\n");
  1255. wsm_lock_tx_async(priv);
  1256. if (queue_work(priv->workqueue,
  1257. &priv->unjoin_work) <= 0)
  1258. wsm_unlock_tx(priv);
  1259. } else if (ieee80211_has_protected(fctl) &&
  1260. tx_info->control.hw_key &&
  1261. tx_info->control.hw_key->keyidx != priv->wep_default_key_id &&
  1262. (tx_info->control.hw_key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  1263. tx_info->control.hw_key->cipher == WLAN_CIPHER_SUITE_WEP104)) {
  1264. action = do_wep;
  1265. }
  1266. }
  1267. switch (action) {
  1268. case do_probe:
  1269. /* An interesting FW "feature". Device filters probe responses.
  1270. * The easiest way to get it back is to convert
  1271. * probe request into WSM start_scan command.
  1272. */
  1273. pr_debug("[WSM] Convert probe request to scan.\n");
  1274. wsm_lock_tx_async(priv);
  1275. priv->pending_frame_id = wsm->packet_id;
  1276. if (queue_delayed_work(priv->workqueue,
  1277. &priv->scan.probe_work, 0) <= 0)
  1278. wsm_unlock_tx(priv);
  1279. handled = true;
  1280. break;
  1281. case do_drop:
  1282. pr_debug("[WSM] Drop frame (0x%.4X).\n", fctl);
  1283. BUG_ON(cw1200_queue_remove(queue, wsm->packet_id));
  1284. handled = true;
  1285. break;
  1286. case do_wep:
  1287. pr_debug("[WSM] Issue set_default_wep_key.\n");
  1288. wsm_lock_tx_async(priv);
  1289. priv->wep_default_key_id = tx_info->control.hw_key->keyidx;
  1290. priv->pending_frame_id = wsm->packet_id;
  1291. if (queue_work(priv->workqueue, &priv->wep_key_work) <= 0)
  1292. wsm_unlock_tx(priv);
  1293. handled = true;
  1294. break;
  1295. case do_tx:
  1296. pr_debug("[WSM] Transmit frame.\n");
  1297. break;
  1298. default:
  1299. /* Do nothing */
  1300. break;
  1301. }
  1302. return handled;
  1303. }
  1304. static int cw1200_get_prio_queue(struct cw1200_common *priv,
  1305. u32 link_id_map, int *total)
  1306. {
  1307. static const int urgent = BIT(CW1200_LINK_ID_AFTER_DTIM) |
  1308. BIT(CW1200_LINK_ID_UAPSD);
  1309. struct wsm_edca_queue_params *edca;
  1310. unsigned score, best = -1;
  1311. int winner = -1;
  1312. int queued;
  1313. int i;
  1314. /* search for a winner using edca params */
  1315. for (i = 0; i < 4; ++i) {
  1316. queued = cw1200_queue_get_num_queued(&priv->tx_queue[i],
  1317. link_id_map);
  1318. if (!queued)
  1319. continue;
  1320. *total += queued;
  1321. edca = &priv->edca.params[i];
  1322. score = ((edca->aifns + edca->cwmin) << 16) +
  1323. ((edca->cwmax - edca->cwmin) *
  1324. (get_random_int() & 0xFFFF));
  1325. if (score < best && (winner < 0 || i != 3)) {
  1326. best = score;
  1327. winner = i;
  1328. }
  1329. }
  1330. /* override winner if bursting */
  1331. if (winner >= 0 && priv->tx_burst_idx >= 0 &&
  1332. winner != priv->tx_burst_idx &&
  1333. !cw1200_queue_get_num_queued(
  1334. &priv->tx_queue[winner],
  1335. link_id_map & urgent) &&
  1336. cw1200_queue_get_num_queued(
  1337. &priv->tx_queue[priv->tx_burst_idx],
  1338. link_id_map))
  1339. winner = priv->tx_burst_idx;
  1340. return winner;
  1341. }
  1342. static int wsm_get_tx_queue_and_mask(struct cw1200_common *priv,
  1343. struct cw1200_queue **queue_p,
  1344. u32 *tx_allowed_mask_p,
  1345. bool *more)
  1346. {
  1347. int idx;
  1348. u32 tx_allowed_mask;
  1349. int total = 0;
  1350. /* Search for a queue with multicast frames buffered */
  1351. if (priv->tx_multicast) {
  1352. tx_allowed_mask = BIT(CW1200_LINK_ID_AFTER_DTIM);
  1353. idx = cw1200_get_prio_queue(priv,
  1354. tx_allowed_mask, &total);
  1355. if (idx >= 0) {
  1356. *more = total > 1;
  1357. goto found;
  1358. }
  1359. }
  1360. /* Search for unicast traffic */
  1361. tx_allowed_mask = ~priv->sta_asleep_mask;
  1362. tx_allowed_mask |= BIT(CW1200_LINK_ID_UAPSD);
  1363. if (priv->sta_asleep_mask) {
  1364. tx_allowed_mask |= priv->pspoll_mask;
  1365. tx_allowed_mask &= ~BIT(CW1200_LINK_ID_AFTER_DTIM);
  1366. } else {
  1367. tx_allowed_mask |= BIT(CW1200_LINK_ID_AFTER_DTIM);
  1368. }
  1369. idx = cw1200_get_prio_queue(priv,
  1370. tx_allowed_mask, &total);
  1371. if (idx < 0)
  1372. return -ENOENT;
  1373. found:
  1374. *queue_p = &priv->tx_queue[idx];
  1375. *tx_allowed_mask_p = tx_allowed_mask;
  1376. return 0;
  1377. }
  1378. int wsm_get_tx(struct cw1200_common *priv, u8 **data,
  1379. size_t *tx_len, int *burst)
  1380. {
  1381. struct wsm_tx *wsm = NULL;
  1382. struct ieee80211_tx_info *tx_info;
  1383. struct cw1200_queue *queue = NULL;
  1384. int queue_num;
  1385. u32 tx_allowed_mask = 0;
  1386. const struct cw1200_txpriv *txpriv = NULL;
  1387. int count = 0;
  1388. /* More is used only for broadcasts. */
  1389. bool more = false;
  1390. if (priv->wsm_cmd.ptr) { /* CMD request */
  1391. ++count;
  1392. spin_lock(&priv->wsm_cmd.lock);
  1393. BUG_ON(!priv->wsm_cmd.ptr);
  1394. *data = priv->wsm_cmd.ptr;
  1395. *tx_len = priv->wsm_cmd.len;
  1396. *burst = 1;
  1397. spin_unlock(&priv->wsm_cmd.lock);
  1398. } else {
  1399. for (;;) {
  1400. int ret;
  1401. if (atomic_add_return(0, &priv->tx_lock))
  1402. break;
  1403. spin_lock_bh(&priv->ps_state_lock);
  1404. ret = wsm_get_tx_queue_and_mask(priv, &queue,
  1405. &tx_allowed_mask, &more);
  1406. queue_num = queue - priv->tx_queue;
  1407. if (priv->buffered_multicasts &&
  1408. (ret || !more) &&
  1409. (priv->tx_multicast || !priv->sta_asleep_mask)) {
  1410. priv->buffered_multicasts = false;
  1411. if (priv->tx_multicast) {
  1412. priv->tx_multicast = false;
  1413. queue_work(priv->workqueue,
  1414. &priv->multicast_stop_work);
  1415. }
  1416. }
  1417. spin_unlock_bh(&priv->ps_state_lock);
  1418. if (ret)
  1419. break;
  1420. if (cw1200_queue_get(queue,
  1421. tx_allowed_mask,
  1422. &wsm, &tx_info, &txpriv))
  1423. continue;
  1424. if (wsm_handle_tx_data(priv, wsm,
  1425. tx_info, txpriv, queue))
  1426. continue; /* Handled by WSM */
  1427. wsm->hdr.id &= __cpu_to_le16(
  1428. ~WSM_TX_LINK_ID(WSM_TX_LINK_ID_MAX));
  1429. wsm->hdr.id |= cpu_to_le16(
  1430. WSM_TX_LINK_ID(txpriv->raw_link_id));
  1431. priv->pspoll_mask &= ~BIT(txpriv->raw_link_id);
  1432. *data = (u8 *)wsm;
  1433. *tx_len = __le16_to_cpu(wsm->hdr.len);
  1434. /* allow bursting if txop is set */
  1435. if (priv->edca.params[queue_num].txop_limit)
  1436. *burst = min(*burst,
  1437. (int)cw1200_queue_get_num_queued(queue, tx_allowed_mask) + 1);
  1438. else
  1439. *burst = 1;
  1440. /* store index of bursting queue */
  1441. if (*burst > 1)
  1442. priv->tx_burst_idx = queue_num;
  1443. else
  1444. priv->tx_burst_idx = -1;
  1445. if (more) {
  1446. struct ieee80211_hdr *hdr =
  1447. (struct ieee80211_hdr *)
  1448. &((u8 *)wsm)[txpriv->offset];
  1449. /* more buffered multicast/broadcast frames
  1450. * ==> set MoreData flag in IEEE 802.11 header
  1451. * to inform PS STAs
  1452. */
  1453. hdr->frame_control |=
  1454. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1455. }
  1456. pr_debug("[WSM] >>> 0x%.4X (%zu) %p %c\n",
  1457. 0x0004, *tx_len, *data,
  1458. wsm->more ? 'M' : ' ');
  1459. ++count;
  1460. break;
  1461. }
  1462. }
  1463. return count;
  1464. }
  1465. void wsm_txed(struct cw1200_common *priv, u8 *data)
  1466. {
  1467. if (data == priv->wsm_cmd.ptr) {
  1468. spin_lock(&priv->wsm_cmd.lock);
  1469. priv->wsm_cmd.ptr = NULL;
  1470. spin_unlock(&priv->wsm_cmd.lock);
  1471. }
  1472. }
  1473. /* ******************************************************************** */
  1474. /* WSM buffer */
  1475. void wsm_buf_init(struct wsm_buf *buf)
  1476. {
  1477. BUG_ON(buf->begin);
  1478. buf->begin = kmalloc(FWLOAD_BLOCK_SIZE, GFP_KERNEL | GFP_DMA);
  1479. buf->end = buf->begin ? &buf->begin[FWLOAD_BLOCK_SIZE] : buf->begin;
  1480. wsm_buf_reset(buf);
  1481. }
  1482. void wsm_buf_deinit(struct wsm_buf *buf)
  1483. {
  1484. kfree(buf->begin);
  1485. buf->begin = buf->data = buf->end = NULL;
  1486. }
  1487. static void wsm_buf_reset(struct wsm_buf *buf)
  1488. {
  1489. if (buf->begin) {
  1490. buf->data = &buf->begin[4];
  1491. *(u32 *)buf->begin = 0;
  1492. } else {
  1493. buf->data = buf->begin;
  1494. }
  1495. }
  1496. static int wsm_buf_reserve(struct wsm_buf *buf, size_t extra_size)
  1497. {
  1498. size_t pos = buf->data - buf->begin;
  1499. size_t size = pos + extra_size;
  1500. u8 *tmp;
  1501. size = round_up(size, FWLOAD_BLOCK_SIZE);
  1502. tmp = krealloc(buf->begin, size, GFP_KERNEL | GFP_DMA);
  1503. if (!tmp) {
  1504. wsm_buf_deinit(buf);
  1505. return -ENOMEM;
  1506. }
  1507. buf->begin = tmp;
  1508. buf->data = &buf->begin[pos];
  1509. buf->end = &buf->begin[size];
  1510. return 0;
  1511. }