wsm.c 43 KB

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