wl1251_main.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  1. /*
  2. * This file is part of wl1251
  3. *
  4. * Copyright (C) 2008-2009 Nokia Corporation
  5. *
  6. * Contact: Kalle Valo <kalle.valo@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/firmware.h>
  26. #include <linux/delay.h>
  27. #include <linux/irq.h>
  28. #include <linux/crc32.h>
  29. #include <linux/etherdevice.h>
  30. #include <linux/vmalloc.h>
  31. #include "wl1251.h"
  32. #include "wl12xx_80211.h"
  33. #include "wl1251_reg.h"
  34. #include "wl1251_io.h"
  35. #include "wl1251_cmd.h"
  36. #include "wl1251_event.h"
  37. #include "wl1251_tx.h"
  38. #include "wl1251_rx.h"
  39. #include "wl1251_ps.h"
  40. #include "wl1251_init.h"
  41. #include "wl1251_debugfs.h"
  42. #include "wl1251_boot.h"
  43. void wl1251_enable_interrupts(struct wl1251 *wl)
  44. {
  45. wl->if_ops->enable_irq(wl);
  46. }
  47. void wl1251_disable_interrupts(struct wl1251 *wl)
  48. {
  49. wl->if_ops->disable_irq(wl);
  50. }
  51. static void wl1251_power_off(struct wl1251 *wl)
  52. {
  53. wl->set_power(false);
  54. }
  55. static void wl1251_power_on(struct wl1251 *wl)
  56. {
  57. wl->set_power(true);
  58. }
  59. static int wl1251_fetch_firmware(struct wl1251 *wl)
  60. {
  61. const struct firmware *fw;
  62. struct device *dev = wiphy_dev(wl->hw->wiphy);
  63. int ret;
  64. ret = request_firmware(&fw, WL1251_FW_NAME, dev);
  65. if (ret < 0) {
  66. wl1251_error("could not get firmware: %d", ret);
  67. return ret;
  68. }
  69. if (fw->size % 4) {
  70. wl1251_error("firmware size is not multiple of 32 bits: %zu",
  71. fw->size);
  72. ret = -EILSEQ;
  73. goto out;
  74. }
  75. wl->fw_len = fw->size;
  76. wl->fw = vmalloc(wl->fw_len);
  77. if (!wl->fw) {
  78. wl1251_error("could not allocate memory for the firmware");
  79. ret = -ENOMEM;
  80. goto out;
  81. }
  82. memcpy(wl->fw, fw->data, wl->fw_len);
  83. ret = 0;
  84. out:
  85. release_firmware(fw);
  86. return ret;
  87. }
  88. static int wl1251_fetch_nvs(struct wl1251 *wl)
  89. {
  90. const struct firmware *fw;
  91. struct device *dev = wiphy_dev(wl->hw->wiphy);
  92. int ret;
  93. ret = request_firmware(&fw, WL1251_NVS_NAME, dev);
  94. if (ret < 0) {
  95. wl1251_error("could not get nvs file: %d", ret);
  96. return ret;
  97. }
  98. if (fw->size % 4) {
  99. wl1251_error("nvs size is not multiple of 32 bits: %zu",
  100. fw->size);
  101. ret = -EILSEQ;
  102. goto out;
  103. }
  104. wl->nvs_len = fw->size;
  105. wl->nvs = kmalloc(wl->nvs_len, GFP_KERNEL);
  106. if (!wl->nvs) {
  107. wl1251_error("could not allocate memory for the nvs file");
  108. ret = -ENOMEM;
  109. goto out;
  110. }
  111. memcpy(wl->nvs, fw->data, wl->nvs_len);
  112. ret = 0;
  113. out:
  114. release_firmware(fw);
  115. return ret;
  116. }
  117. static void wl1251_fw_wakeup(struct wl1251 *wl)
  118. {
  119. u32 elp_reg;
  120. elp_reg = ELPCTRL_WAKE_UP;
  121. wl1251_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
  122. elp_reg = wl1251_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
  123. if (!(elp_reg & ELPCTRL_WLAN_READY))
  124. wl1251_warning("WLAN not ready");
  125. }
  126. static int wl1251_chip_wakeup(struct wl1251 *wl)
  127. {
  128. int ret = 0;
  129. wl1251_power_on(wl);
  130. msleep(WL1251_POWER_ON_SLEEP);
  131. wl->if_ops->reset(wl);
  132. /* We don't need a real memory partition here, because we only want
  133. * to use the registers at this point. */
  134. wl1251_set_partition(wl,
  135. 0x00000000,
  136. 0x00000000,
  137. REGISTERS_BASE,
  138. REGISTERS_DOWN_SIZE);
  139. /* ELP module wake up */
  140. wl1251_fw_wakeup(wl);
  141. /* whal_FwCtrl_BootSm() */
  142. /* 0. read chip id from CHIP_ID */
  143. wl->chip_id = wl1251_reg_read32(wl, CHIP_ID_B);
  144. /* 1. check if chip id is valid */
  145. switch (wl->chip_id) {
  146. case CHIP_ID_1251_PG12:
  147. wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG12)",
  148. wl->chip_id);
  149. break;
  150. case CHIP_ID_1251_PG11:
  151. wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG11)",
  152. wl->chip_id);
  153. break;
  154. case CHIP_ID_1251_PG10:
  155. default:
  156. wl1251_error("unsupported chip id: 0x%x", wl->chip_id);
  157. ret = -ENODEV;
  158. goto out;
  159. }
  160. if (wl->fw == NULL) {
  161. ret = wl1251_fetch_firmware(wl);
  162. if (ret < 0)
  163. goto out;
  164. }
  165. /* No NVS from netlink, try to get it from the filesystem */
  166. if (wl->nvs == NULL) {
  167. ret = wl1251_fetch_nvs(wl);
  168. if (ret < 0)
  169. goto out;
  170. }
  171. out:
  172. return ret;
  173. }
  174. #define WL1251_IRQ_LOOP_COUNT 10
  175. static void wl1251_irq_work(struct work_struct *work)
  176. {
  177. u32 intr, ctr = WL1251_IRQ_LOOP_COUNT;
  178. struct wl1251 *wl =
  179. container_of(work, struct wl1251, irq_work);
  180. int ret;
  181. mutex_lock(&wl->mutex);
  182. wl1251_debug(DEBUG_IRQ, "IRQ work");
  183. if (wl->state == WL1251_STATE_OFF)
  184. goto out;
  185. ret = wl1251_ps_elp_wakeup(wl);
  186. if (ret < 0)
  187. goto out;
  188. wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, WL1251_ACX_INTR_ALL);
  189. intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
  190. wl1251_debug(DEBUG_IRQ, "intr: 0x%x", intr);
  191. do {
  192. if (wl->data_path) {
  193. wl->rx_counter = wl1251_mem_read32(
  194. wl, wl->data_path->rx_control_addr);
  195. /* We handle a frmware bug here */
  196. switch ((wl->rx_counter - wl->rx_handled) & 0xf) {
  197. case 0:
  198. wl1251_debug(DEBUG_IRQ,
  199. "RX: FW and host in sync");
  200. intr &= ~WL1251_ACX_INTR_RX0_DATA;
  201. intr &= ~WL1251_ACX_INTR_RX1_DATA;
  202. break;
  203. case 1:
  204. wl1251_debug(DEBUG_IRQ, "RX: FW +1");
  205. intr |= WL1251_ACX_INTR_RX0_DATA;
  206. intr &= ~WL1251_ACX_INTR_RX1_DATA;
  207. break;
  208. case 2:
  209. wl1251_debug(DEBUG_IRQ, "RX: FW +2");
  210. intr |= WL1251_ACX_INTR_RX0_DATA;
  211. intr |= WL1251_ACX_INTR_RX1_DATA;
  212. break;
  213. default:
  214. wl1251_warning(
  215. "RX: FW and host out of sync: %d",
  216. wl->rx_counter - wl->rx_handled);
  217. break;
  218. }
  219. wl->rx_handled = wl->rx_counter;
  220. wl1251_debug(DEBUG_IRQ, "RX counter: %d",
  221. wl->rx_counter);
  222. }
  223. intr &= wl->intr_mask;
  224. if (intr == 0) {
  225. wl1251_debug(DEBUG_IRQ, "INTR is 0");
  226. goto out_sleep;
  227. }
  228. if (intr & WL1251_ACX_INTR_RX0_DATA) {
  229. wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX0_DATA");
  230. wl1251_rx(wl);
  231. }
  232. if (intr & WL1251_ACX_INTR_RX1_DATA) {
  233. wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX1_DATA");
  234. wl1251_rx(wl);
  235. }
  236. if (intr & WL1251_ACX_INTR_TX_RESULT) {
  237. wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_TX_RESULT");
  238. wl1251_tx_complete(wl);
  239. }
  240. if (intr & (WL1251_ACX_INTR_EVENT_A |
  241. WL1251_ACX_INTR_EVENT_B)) {
  242. wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_EVENT (0x%x)",
  243. intr);
  244. if (intr & WL1251_ACX_INTR_EVENT_A)
  245. wl1251_event_handle(wl, 0);
  246. else
  247. wl1251_event_handle(wl, 1);
  248. }
  249. if (intr & WL1251_ACX_INTR_INIT_COMPLETE)
  250. wl1251_debug(DEBUG_IRQ,
  251. "WL1251_ACX_INTR_INIT_COMPLETE");
  252. if (--ctr == 0)
  253. break;
  254. intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
  255. } while (intr);
  256. out_sleep:
  257. wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, ~(wl->intr_mask));
  258. wl1251_ps_elp_sleep(wl);
  259. out:
  260. mutex_unlock(&wl->mutex);
  261. }
  262. static int wl1251_join(struct wl1251 *wl, u8 bss_type, u8 channel,
  263. u16 beacon_interval, u8 dtim_period)
  264. {
  265. int ret;
  266. ret = wl1251_acx_frame_rates(wl, DEFAULT_HW_GEN_TX_RATE,
  267. DEFAULT_HW_GEN_MODULATION_TYPE,
  268. wl->tx_mgmt_frm_rate,
  269. wl->tx_mgmt_frm_mod);
  270. if (ret < 0)
  271. goto out;
  272. ret = wl1251_cmd_join(wl, bss_type, channel, beacon_interval,
  273. dtim_period);
  274. if (ret < 0)
  275. goto out;
  276. /*
  277. * FIXME: we should wait for JOIN_EVENT_COMPLETE_ID but to simplify
  278. * locking we just sleep instead, for now
  279. */
  280. msleep(10);
  281. out:
  282. return ret;
  283. }
  284. static void wl1251_filter_work(struct work_struct *work)
  285. {
  286. struct wl1251 *wl =
  287. container_of(work, struct wl1251, filter_work);
  288. int ret;
  289. mutex_lock(&wl->mutex);
  290. if (wl->state == WL1251_STATE_OFF)
  291. goto out;
  292. ret = wl1251_ps_elp_wakeup(wl);
  293. if (ret < 0)
  294. goto out;
  295. ret = wl1251_join(wl, wl->bss_type, wl->channel, wl->beacon_int,
  296. wl->dtim_period);
  297. if (ret < 0)
  298. goto out_sleep;
  299. out_sleep:
  300. wl1251_ps_elp_sleep(wl);
  301. out:
  302. mutex_unlock(&wl->mutex);
  303. }
  304. static int wl1251_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  305. {
  306. struct wl1251 *wl = hw->priv;
  307. skb_queue_tail(&wl->tx_queue, skb);
  308. /*
  309. * The chip specific setup must run before the first TX packet -
  310. * before that, the tx_work will not be initialized!
  311. */
  312. ieee80211_queue_work(wl->hw, &wl->tx_work);
  313. /*
  314. * The workqueue is slow to process the tx_queue and we need stop
  315. * the queue here, otherwise the queue will get too long.
  316. */
  317. if (skb_queue_len(&wl->tx_queue) >= WL1251_TX_QUEUE_MAX_LENGTH) {
  318. wl1251_debug(DEBUG_TX, "op_tx: tx_queue full, stop queues");
  319. ieee80211_stop_queues(wl->hw);
  320. /*
  321. * FIXME: this is racy, the variable is not properly
  322. * protected. Maybe fix this by removing the stupid
  323. * variable altogether and checking the real queue state?
  324. */
  325. wl->tx_queue_stopped = true;
  326. }
  327. return NETDEV_TX_OK;
  328. }
  329. static int wl1251_op_start(struct ieee80211_hw *hw)
  330. {
  331. struct wl1251 *wl = hw->priv;
  332. int ret = 0;
  333. wl1251_debug(DEBUG_MAC80211, "mac80211 start");
  334. mutex_lock(&wl->mutex);
  335. if (wl->state != WL1251_STATE_OFF) {
  336. wl1251_error("cannot start because not in off state: %d",
  337. wl->state);
  338. ret = -EBUSY;
  339. goto out;
  340. }
  341. ret = wl1251_chip_wakeup(wl);
  342. if (ret < 0)
  343. goto out;
  344. ret = wl1251_boot(wl);
  345. if (ret < 0)
  346. goto out;
  347. ret = wl1251_hw_init(wl);
  348. if (ret < 0)
  349. goto out;
  350. ret = wl1251_acx_station_id(wl);
  351. if (ret < 0)
  352. goto out;
  353. wl->state = WL1251_STATE_ON;
  354. wl1251_info("firmware booted (%s)", wl->fw_ver);
  355. out:
  356. if (ret < 0)
  357. wl1251_power_off(wl);
  358. mutex_unlock(&wl->mutex);
  359. return ret;
  360. }
  361. static void wl1251_op_stop(struct ieee80211_hw *hw)
  362. {
  363. struct wl1251 *wl = hw->priv;
  364. wl1251_info("down");
  365. wl1251_debug(DEBUG_MAC80211, "mac80211 stop");
  366. mutex_lock(&wl->mutex);
  367. WARN_ON(wl->state != WL1251_STATE_ON);
  368. if (wl->scanning) {
  369. mutex_unlock(&wl->mutex);
  370. ieee80211_scan_completed(wl->hw, true);
  371. mutex_lock(&wl->mutex);
  372. wl->scanning = false;
  373. }
  374. wl->state = WL1251_STATE_OFF;
  375. wl1251_disable_interrupts(wl);
  376. mutex_unlock(&wl->mutex);
  377. cancel_work_sync(&wl->irq_work);
  378. cancel_work_sync(&wl->tx_work);
  379. cancel_work_sync(&wl->filter_work);
  380. mutex_lock(&wl->mutex);
  381. /* let's notify MAC80211 about the remaining pending TX frames */
  382. wl1251_tx_flush(wl);
  383. wl1251_power_off(wl);
  384. memset(wl->bssid, 0, ETH_ALEN);
  385. wl->listen_int = 1;
  386. wl->bss_type = MAX_BSS_TYPE;
  387. wl->data_in_count = 0;
  388. wl->rx_counter = 0;
  389. wl->rx_handled = 0;
  390. wl->rx_current_buffer = 0;
  391. wl->rx_last_id = 0;
  392. wl->next_tx_complete = 0;
  393. wl->elp = false;
  394. wl->psm = 0;
  395. wl->tx_queue_stopped = false;
  396. wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
  397. wl->channel = WL1251_DEFAULT_CHANNEL;
  398. wl1251_debugfs_reset(wl);
  399. mutex_unlock(&wl->mutex);
  400. }
  401. static int wl1251_op_add_interface(struct ieee80211_hw *hw,
  402. struct ieee80211_vif *vif)
  403. {
  404. struct wl1251 *wl = hw->priv;
  405. int ret = 0;
  406. wl1251_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
  407. vif->type, vif->addr);
  408. mutex_lock(&wl->mutex);
  409. if (wl->vif) {
  410. ret = -EBUSY;
  411. goto out;
  412. }
  413. wl->vif = vif;
  414. switch (vif->type) {
  415. case NL80211_IFTYPE_STATION:
  416. wl->bss_type = BSS_TYPE_STA_BSS;
  417. break;
  418. case NL80211_IFTYPE_ADHOC:
  419. wl->bss_type = BSS_TYPE_IBSS;
  420. break;
  421. default:
  422. ret = -EOPNOTSUPP;
  423. goto out;
  424. }
  425. if (memcmp(wl->mac_addr, vif->addr, ETH_ALEN)) {
  426. memcpy(wl->mac_addr, vif->addr, ETH_ALEN);
  427. SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
  428. ret = wl1251_acx_station_id(wl);
  429. if (ret < 0)
  430. goto out;
  431. }
  432. out:
  433. mutex_unlock(&wl->mutex);
  434. return ret;
  435. }
  436. static void wl1251_op_remove_interface(struct ieee80211_hw *hw,
  437. struct ieee80211_vif *vif)
  438. {
  439. struct wl1251 *wl = hw->priv;
  440. mutex_lock(&wl->mutex);
  441. wl1251_debug(DEBUG_MAC80211, "mac80211 remove interface");
  442. wl->vif = NULL;
  443. mutex_unlock(&wl->mutex);
  444. }
  445. static int wl1251_build_null_data(struct wl1251 *wl)
  446. {
  447. struct wl12xx_null_data_template template;
  448. if (!is_zero_ether_addr(wl->bssid)) {
  449. memcpy(template.header.da, wl->bssid, ETH_ALEN);
  450. memcpy(template.header.bssid, wl->bssid, ETH_ALEN);
  451. } else {
  452. memset(template.header.da, 0xff, ETH_ALEN);
  453. memset(template.header.bssid, 0xff, ETH_ALEN);
  454. }
  455. memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
  456. template.header.frame_ctl = cpu_to_le16(IEEE80211_FTYPE_DATA |
  457. IEEE80211_STYPE_NULLFUNC |
  458. IEEE80211_FCTL_TODS);
  459. return wl1251_cmd_template_set(wl, CMD_NULL_DATA, &template,
  460. sizeof(template));
  461. }
  462. static int wl1251_build_ps_poll(struct wl1251 *wl, u16 aid)
  463. {
  464. struct wl12xx_ps_poll_template template;
  465. memcpy(template.bssid, wl->bssid, ETH_ALEN);
  466. memcpy(template.ta, wl->mac_addr, ETH_ALEN);
  467. /* aid in PS-Poll has its two MSBs each set to 1 */
  468. template.aid = cpu_to_le16(1 << 15 | 1 << 14 | aid);
  469. template.fc = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
  470. return wl1251_cmd_template_set(wl, CMD_PS_POLL, &template,
  471. sizeof(template));
  472. }
  473. static int wl1251_op_config(struct ieee80211_hw *hw, u32 changed)
  474. {
  475. struct wl1251 *wl = hw->priv;
  476. struct ieee80211_conf *conf = &hw->conf;
  477. int channel, ret = 0;
  478. channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
  479. wl1251_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
  480. channel,
  481. conf->flags & IEEE80211_CONF_PS ? "on" : "off",
  482. conf->power_level);
  483. mutex_lock(&wl->mutex);
  484. ret = wl1251_ps_elp_wakeup(wl);
  485. if (ret < 0)
  486. goto out;
  487. if (channel != wl->channel) {
  488. wl->channel = channel;
  489. ret = wl1251_join(wl, wl->bss_type, wl->channel,
  490. wl->beacon_int, wl->dtim_period);
  491. if (ret < 0)
  492. goto out_sleep;
  493. }
  494. if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
  495. wl1251_debug(DEBUG_PSM, "psm enabled");
  496. wl->psm_requested = true;
  497. /*
  498. * We enter PSM only if we're already associated.
  499. * If we're not, we'll enter it when joining an SSID,
  500. * through the bss_info_changed() hook.
  501. */
  502. ret = wl1251_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
  503. if (ret < 0)
  504. goto out_sleep;
  505. } else if (!(conf->flags & IEEE80211_CONF_PS) &&
  506. wl->psm_requested) {
  507. wl1251_debug(DEBUG_PSM, "psm disabled");
  508. wl->psm_requested = false;
  509. if (wl->psm) {
  510. ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
  511. if (ret < 0)
  512. goto out_sleep;
  513. }
  514. }
  515. if (conf->power_level != wl->power_level) {
  516. ret = wl1251_acx_tx_power(wl, conf->power_level);
  517. if (ret < 0)
  518. goto out_sleep;
  519. wl->power_level = conf->power_level;
  520. }
  521. out_sleep:
  522. wl1251_ps_elp_sleep(wl);
  523. out:
  524. mutex_unlock(&wl->mutex);
  525. return ret;
  526. }
  527. #define WL1251_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
  528. FIF_ALLMULTI | \
  529. FIF_FCSFAIL | \
  530. FIF_BCN_PRBRESP_PROMISC | \
  531. FIF_CONTROL | \
  532. FIF_OTHER_BSS)
  533. static void wl1251_op_configure_filter(struct ieee80211_hw *hw,
  534. unsigned int changed,
  535. unsigned int *total,u64 multicast)
  536. {
  537. struct wl1251 *wl = hw->priv;
  538. wl1251_debug(DEBUG_MAC80211, "mac80211 configure filter");
  539. *total &= WL1251_SUPPORTED_FILTERS;
  540. changed &= WL1251_SUPPORTED_FILTERS;
  541. if (changed == 0)
  542. /* no filters which we support changed */
  543. return;
  544. /* FIXME: wl->rx_config and wl->rx_filter are not protected */
  545. wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
  546. wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
  547. if (*total & FIF_PROMISC_IN_BSS) {
  548. wl->rx_config |= CFG_BSSID_FILTER_EN;
  549. wl->rx_config |= CFG_RX_ALL_GOOD;
  550. }
  551. if (*total & FIF_ALLMULTI)
  552. /*
  553. * CFG_MC_FILTER_EN in rx_config needs to be 0 to receive
  554. * all multicast frames
  555. */
  556. wl->rx_config &= ~CFG_MC_FILTER_EN;
  557. if (*total & FIF_FCSFAIL)
  558. wl->rx_filter |= CFG_RX_FCS_ERROR;
  559. if (*total & FIF_BCN_PRBRESP_PROMISC) {
  560. wl->rx_config &= ~CFG_BSSID_FILTER_EN;
  561. wl->rx_config &= ~CFG_SSID_FILTER_EN;
  562. }
  563. if (*total & FIF_CONTROL)
  564. wl->rx_filter |= CFG_RX_CTL_EN;
  565. if (*total & FIF_OTHER_BSS)
  566. wl->rx_filter &= ~CFG_BSSID_FILTER_EN;
  567. /*
  568. * FIXME: workqueues need to be properly cancelled on stop(), for
  569. * now let's just disable changing the filter settings. They will
  570. * be updated any on config().
  571. */
  572. /* schedule_work(&wl->filter_work); */
  573. }
  574. /* HW encryption */
  575. static int wl1251_set_key_type(struct wl1251 *wl,
  576. struct wl1251_cmd_set_keys *key,
  577. enum set_key_cmd cmd,
  578. struct ieee80211_key_conf *mac80211_key,
  579. const u8 *addr)
  580. {
  581. switch (mac80211_key->alg) {
  582. case ALG_WEP:
  583. if (is_broadcast_ether_addr(addr))
  584. key->key_type = KEY_WEP_DEFAULT;
  585. else
  586. key->key_type = KEY_WEP_ADDR;
  587. mac80211_key->hw_key_idx = mac80211_key->keyidx;
  588. break;
  589. case ALG_TKIP:
  590. if (is_broadcast_ether_addr(addr))
  591. key->key_type = KEY_TKIP_MIC_GROUP;
  592. else
  593. key->key_type = KEY_TKIP_MIC_PAIRWISE;
  594. mac80211_key->hw_key_idx = mac80211_key->keyidx;
  595. break;
  596. case ALG_CCMP:
  597. if (is_broadcast_ether_addr(addr))
  598. key->key_type = KEY_AES_GROUP;
  599. else
  600. key->key_type = KEY_AES_PAIRWISE;
  601. mac80211_key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  602. break;
  603. default:
  604. wl1251_error("Unknown key algo 0x%x", mac80211_key->alg);
  605. return -EOPNOTSUPP;
  606. }
  607. return 0;
  608. }
  609. static int wl1251_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  610. struct ieee80211_vif *vif,
  611. struct ieee80211_sta *sta,
  612. struct ieee80211_key_conf *key)
  613. {
  614. struct wl1251 *wl = hw->priv;
  615. struct wl1251_cmd_set_keys *wl_cmd;
  616. const u8 *addr;
  617. int ret;
  618. static const u8 bcast_addr[ETH_ALEN] =
  619. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  620. wl1251_debug(DEBUG_MAC80211, "mac80211 set key");
  621. wl_cmd = kzalloc(sizeof(*wl_cmd), GFP_KERNEL);
  622. if (!wl_cmd) {
  623. ret = -ENOMEM;
  624. goto out;
  625. }
  626. addr = sta ? sta->addr : bcast_addr;
  627. wl1251_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
  628. wl1251_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
  629. wl1251_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
  630. key->alg, key->keyidx, key->keylen, key->flags);
  631. wl1251_dump(DEBUG_CRYPT, "KEY: ", key->key, key->keylen);
  632. if (is_zero_ether_addr(addr)) {
  633. /* We dont support TX only encryption */
  634. ret = -EOPNOTSUPP;
  635. goto out;
  636. }
  637. mutex_lock(&wl->mutex);
  638. ret = wl1251_ps_elp_wakeup(wl);
  639. if (ret < 0)
  640. goto out_unlock;
  641. switch (cmd) {
  642. case SET_KEY:
  643. wl_cmd->key_action = KEY_ADD_OR_REPLACE;
  644. break;
  645. case DISABLE_KEY:
  646. wl_cmd->key_action = KEY_REMOVE;
  647. break;
  648. default:
  649. wl1251_error("Unsupported key cmd 0x%x", cmd);
  650. break;
  651. }
  652. ret = wl1251_set_key_type(wl, wl_cmd, cmd, key, addr);
  653. if (ret < 0) {
  654. wl1251_error("Set KEY type failed");
  655. goto out_sleep;
  656. }
  657. if (wl_cmd->key_type != KEY_WEP_DEFAULT)
  658. memcpy(wl_cmd->addr, addr, ETH_ALEN);
  659. if ((wl_cmd->key_type == KEY_TKIP_MIC_GROUP) ||
  660. (wl_cmd->key_type == KEY_TKIP_MIC_PAIRWISE)) {
  661. /*
  662. * We get the key in the following form:
  663. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  664. * but the target is expecting:
  665. * TKIP - RX MIC - TX MIC
  666. */
  667. memcpy(wl_cmd->key, key->key, 16);
  668. memcpy(wl_cmd->key + 16, key->key + 24, 8);
  669. memcpy(wl_cmd->key + 24, key->key + 16, 8);
  670. } else {
  671. memcpy(wl_cmd->key, key->key, key->keylen);
  672. }
  673. wl_cmd->key_size = key->keylen;
  674. wl_cmd->id = key->keyidx;
  675. wl_cmd->ssid_profile = 0;
  676. wl1251_dump(DEBUG_CRYPT, "TARGET KEY: ", wl_cmd, sizeof(*wl_cmd));
  677. ret = wl1251_cmd_send(wl, CMD_SET_KEYS, wl_cmd, sizeof(*wl_cmd));
  678. if (ret < 0) {
  679. wl1251_warning("could not set keys");
  680. goto out_sleep;
  681. }
  682. out_sleep:
  683. wl1251_ps_elp_sleep(wl);
  684. out_unlock:
  685. mutex_unlock(&wl->mutex);
  686. out:
  687. kfree(wl_cmd);
  688. return ret;
  689. }
  690. static int wl1251_build_basic_rates(char *rates)
  691. {
  692. u8 index = 0;
  693. rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
  694. rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
  695. rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
  696. rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
  697. return index;
  698. }
  699. static int wl1251_build_extended_rates(char *rates)
  700. {
  701. u8 index = 0;
  702. rates[index++] = IEEE80211_OFDM_RATE_6MB;
  703. rates[index++] = IEEE80211_OFDM_RATE_9MB;
  704. rates[index++] = IEEE80211_OFDM_RATE_12MB;
  705. rates[index++] = IEEE80211_OFDM_RATE_18MB;
  706. rates[index++] = IEEE80211_OFDM_RATE_24MB;
  707. rates[index++] = IEEE80211_OFDM_RATE_36MB;
  708. rates[index++] = IEEE80211_OFDM_RATE_48MB;
  709. rates[index++] = IEEE80211_OFDM_RATE_54MB;
  710. return index;
  711. }
  712. static int wl1251_build_probe_req(struct wl1251 *wl, u8 *ssid, size_t ssid_len)
  713. {
  714. struct wl12xx_probe_req_template template;
  715. struct wl12xx_ie_rates *rates;
  716. char *ptr;
  717. u16 size;
  718. ptr = (char *)&template;
  719. size = sizeof(struct ieee80211_header);
  720. memset(template.header.da, 0xff, ETH_ALEN);
  721. memset(template.header.bssid, 0xff, ETH_ALEN);
  722. memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
  723. template.header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  724. /* IEs */
  725. /* SSID */
  726. template.ssid.header.id = WLAN_EID_SSID;
  727. template.ssid.header.len = ssid_len;
  728. if (ssid_len && ssid)
  729. memcpy(template.ssid.ssid, ssid, ssid_len);
  730. size += sizeof(struct wl12xx_ie_header) + ssid_len;
  731. ptr += size;
  732. /* Basic Rates */
  733. rates = (struct wl12xx_ie_rates *)ptr;
  734. rates->header.id = WLAN_EID_SUPP_RATES;
  735. rates->header.len = wl1251_build_basic_rates(rates->rates);
  736. size += sizeof(struct wl12xx_ie_header) + rates->header.len;
  737. ptr += sizeof(struct wl12xx_ie_header) + rates->header.len;
  738. /* Extended rates */
  739. rates = (struct wl12xx_ie_rates *)ptr;
  740. rates->header.id = WLAN_EID_EXT_SUPP_RATES;
  741. rates->header.len = wl1251_build_extended_rates(rates->rates);
  742. size += sizeof(struct wl12xx_ie_header) + rates->header.len;
  743. wl1251_dump(DEBUG_SCAN, "PROBE REQ: ", &template, size);
  744. return wl1251_cmd_template_set(wl, CMD_PROBE_REQ, &template,
  745. size);
  746. }
  747. static int wl1251_hw_scan(struct wl1251 *wl, u8 *ssid, size_t len,
  748. u8 active_scan, u8 high_prio, u8 num_channels,
  749. u8 probe_requests)
  750. {
  751. struct wl1251_cmd_trigger_scan_to *trigger = NULL;
  752. struct cmd_scan *params = NULL;
  753. int i, ret;
  754. u16 scan_options = 0;
  755. if (wl->scanning)
  756. return -EINVAL;
  757. params = kzalloc(sizeof(*params), GFP_KERNEL);
  758. if (!params)
  759. return -ENOMEM;
  760. params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
  761. params->params.rx_filter_options =
  762. cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
  763. /* High priority scan */
  764. if (!active_scan)
  765. scan_options |= SCAN_PASSIVE;
  766. if (high_prio)
  767. scan_options |= SCAN_PRIORITY_HIGH;
  768. params->params.scan_options = scan_options;
  769. params->params.num_channels = num_channels;
  770. params->params.num_probe_requests = probe_requests;
  771. params->params.tx_rate = cpu_to_le16(1 << 1); /* 2 Mbps */
  772. params->params.tid_trigger = 0;
  773. for (i = 0; i < num_channels; i++) {
  774. params->channels[i].min_duration = cpu_to_le32(30000);
  775. params->channels[i].max_duration = cpu_to_le32(60000);
  776. memset(&params->channels[i].bssid_lsb, 0xff, 4);
  777. memset(&params->channels[i].bssid_msb, 0xff, 2);
  778. params->channels[i].early_termination = 0;
  779. params->channels[i].tx_power_att = 0;
  780. params->channels[i].channel = i + 1;
  781. memset(params->channels[i].pad, 0, 3);
  782. }
  783. for (i = num_channels; i < SCAN_MAX_NUM_OF_CHANNELS; i++)
  784. memset(&params->channels[i], 0,
  785. sizeof(struct basic_scan_channel_parameters));
  786. if (len && ssid) {
  787. params->params.ssid_len = len;
  788. memcpy(params->params.ssid, ssid, len);
  789. } else {
  790. params->params.ssid_len = 0;
  791. memset(params->params.ssid, 0, 32);
  792. }
  793. ret = wl1251_build_probe_req(wl, ssid, len);
  794. if (ret < 0) {
  795. wl1251_error("PROBE request template failed");
  796. goto out;
  797. }
  798. trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
  799. if (!trigger)
  800. goto out;
  801. trigger->timeout = 0;
  802. ret = wl1251_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
  803. sizeof(*trigger));
  804. if (ret < 0) {
  805. wl1251_error("trigger scan to failed for hw scan");
  806. goto out;
  807. }
  808. wl1251_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
  809. wl->scanning = true;
  810. ret = wl1251_cmd_send(wl, CMD_SCAN, params, sizeof(*params));
  811. if (ret < 0)
  812. wl1251_error("SCAN failed");
  813. wl1251_mem_read(wl, wl->cmd_box_addr, params, sizeof(*params));
  814. if (params->header.status != CMD_STATUS_SUCCESS) {
  815. wl1251_error("TEST command answer error: %d",
  816. params->header.status);
  817. wl->scanning = false;
  818. ret = -EIO;
  819. goto out;
  820. }
  821. out:
  822. kfree(params);
  823. return ret;
  824. }
  825. static int wl1251_op_hw_scan(struct ieee80211_hw *hw,
  826. struct cfg80211_scan_request *req)
  827. {
  828. struct wl1251 *wl = hw->priv;
  829. int ret;
  830. u8 *ssid = NULL;
  831. size_t ssid_len = 0;
  832. wl1251_debug(DEBUG_MAC80211, "mac80211 hw scan");
  833. if (req->n_ssids) {
  834. ssid = req->ssids[0].ssid;
  835. ssid_len = req->ssids[0].ssid_len;
  836. }
  837. mutex_lock(&wl->mutex);
  838. ret = wl1251_ps_elp_wakeup(wl);
  839. if (ret < 0)
  840. goto out;
  841. ret = wl1251_hw_scan(hw->priv, ssid, ssid_len, 1, 0, 13, 3);
  842. wl1251_ps_elp_sleep(wl);
  843. out:
  844. mutex_unlock(&wl->mutex);
  845. return ret;
  846. }
  847. static int wl1251_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  848. {
  849. struct wl1251 *wl = hw->priv;
  850. int ret;
  851. mutex_lock(&wl->mutex);
  852. ret = wl1251_ps_elp_wakeup(wl);
  853. if (ret < 0)
  854. goto out;
  855. ret = wl1251_acx_rts_threshold(wl, (u16) value);
  856. if (ret < 0)
  857. wl1251_warning("wl1251_op_set_rts_threshold failed: %d", ret);
  858. wl1251_ps_elp_sleep(wl);
  859. out:
  860. mutex_unlock(&wl->mutex);
  861. return ret;
  862. }
  863. static void wl1251_op_bss_info_changed(struct ieee80211_hw *hw,
  864. struct ieee80211_vif *vif,
  865. struct ieee80211_bss_conf *bss_conf,
  866. u32 changed)
  867. {
  868. enum wl1251_cmd_ps_mode mode;
  869. struct wl1251 *wl = hw->priv;
  870. struct sk_buff *beacon;
  871. int ret;
  872. wl1251_debug(DEBUG_MAC80211, "mac80211 bss info changed");
  873. mutex_lock(&wl->mutex);
  874. ret = wl1251_ps_elp_wakeup(wl);
  875. if (ret < 0)
  876. goto out;
  877. if (changed & BSS_CHANGED_BSSID) {
  878. memcpy(wl->bssid, bss_conf->bssid, ETH_ALEN);
  879. ret = wl1251_build_null_data(wl);
  880. if (ret < 0)
  881. goto out;
  882. if (wl->bss_type != BSS_TYPE_IBSS) {
  883. ret = wl1251_join(wl, wl->bss_type, wl->channel,
  884. wl->beacon_int, wl->dtim_period);
  885. if (ret < 0)
  886. goto out_sleep;
  887. }
  888. }
  889. if (changed & BSS_CHANGED_ASSOC) {
  890. if (bss_conf->assoc) {
  891. wl->beacon_int = bss_conf->beacon_int;
  892. wl->dtim_period = bss_conf->dtim_period;
  893. ret = wl1251_acx_wr_tbtt_and_dtim(wl, wl->beacon_int,
  894. wl->dtim_period);
  895. wl->aid = bss_conf->aid;
  896. ret = wl1251_build_ps_poll(wl, wl->aid);
  897. if (ret < 0)
  898. goto out_sleep;
  899. ret = wl1251_acx_aid(wl, wl->aid);
  900. if (ret < 0)
  901. goto out_sleep;
  902. /* If we want to go in PSM but we're not there yet */
  903. if (wl->psm_requested && !wl->psm) {
  904. mode = STATION_POWER_SAVE_MODE;
  905. ret = wl1251_ps_set_mode(wl, mode);
  906. if (ret < 0)
  907. goto out_sleep;
  908. }
  909. } else {
  910. /* use defaults when not associated */
  911. wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
  912. wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
  913. }
  914. }
  915. if (changed & BSS_CHANGED_ERP_SLOT) {
  916. if (bss_conf->use_short_slot)
  917. ret = wl1251_acx_slot(wl, SLOT_TIME_SHORT);
  918. else
  919. ret = wl1251_acx_slot(wl, SLOT_TIME_LONG);
  920. if (ret < 0) {
  921. wl1251_warning("Set slot time failed %d", ret);
  922. goto out_sleep;
  923. }
  924. }
  925. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  926. if (bss_conf->use_short_preamble)
  927. wl1251_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
  928. else
  929. wl1251_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
  930. }
  931. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  932. if (bss_conf->use_cts_prot)
  933. ret = wl1251_acx_cts_protect(wl, CTSPROTECT_ENABLE);
  934. else
  935. ret = wl1251_acx_cts_protect(wl, CTSPROTECT_DISABLE);
  936. if (ret < 0) {
  937. wl1251_warning("Set ctsprotect failed %d", ret);
  938. goto out;
  939. }
  940. }
  941. if (changed & BSS_CHANGED_BEACON) {
  942. beacon = ieee80211_beacon_get(hw, vif);
  943. ret = wl1251_cmd_template_set(wl, CMD_BEACON, beacon->data,
  944. beacon->len);
  945. if (ret < 0) {
  946. dev_kfree_skb(beacon);
  947. goto out;
  948. }
  949. ret = wl1251_cmd_template_set(wl, CMD_PROBE_RESP, beacon->data,
  950. beacon->len);
  951. dev_kfree_skb(beacon);
  952. if (ret < 0)
  953. goto out;
  954. ret = wl1251_join(wl, wl->bss_type, wl->beacon_int,
  955. wl->channel, wl->dtim_period);
  956. if (ret < 0)
  957. goto out;
  958. }
  959. out_sleep:
  960. wl1251_ps_elp_sleep(wl);
  961. out:
  962. mutex_unlock(&wl->mutex);
  963. }
  964. /* can't be const, mac80211 writes to this */
  965. static struct ieee80211_rate wl1251_rates[] = {
  966. { .bitrate = 10,
  967. .hw_value = 0x1,
  968. .hw_value_short = 0x1, },
  969. { .bitrate = 20,
  970. .hw_value = 0x2,
  971. .hw_value_short = 0x2,
  972. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  973. { .bitrate = 55,
  974. .hw_value = 0x4,
  975. .hw_value_short = 0x4,
  976. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  977. { .bitrate = 110,
  978. .hw_value = 0x20,
  979. .hw_value_short = 0x20,
  980. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  981. { .bitrate = 60,
  982. .hw_value = 0x8,
  983. .hw_value_short = 0x8, },
  984. { .bitrate = 90,
  985. .hw_value = 0x10,
  986. .hw_value_short = 0x10, },
  987. { .bitrate = 120,
  988. .hw_value = 0x40,
  989. .hw_value_short = 0x40, },
  990. { .bitrate = 180,
  991. .hw_value = 0x80,
  992. .hw_value_short = 0x80, },
  993. { .bitrate = 240,
  994. .hw_value = 0x200,
  995. .hw_value_short = 0x200, },
  996. { .bitrate = 360,
  997. .hw_value = 0x400,
  998. .hw_value_short = 0x400, },
  999. { .bitrate = 480,
  1000. .hw_value = 0x800,
  1001. .hw_value_short = 0x800, },
  1002. { .bitrate = 540,
  1003. .hw_value = 0x1000,
  1004. .hw_value_short = 0x1000, },
  1005. };
  1006. /* can't be const, mac80211 writes to this */
  1007. static struct ieee80211_channel wl1251_channels[] = {
  1008. { .hw_value = 1, .center_freq = 2412},
  1009. { .hw_value = 2, .center_freq = 2417},
  1010. { .hw_value = 3, .center_freq = 2422},
  1011. { .hw_value = 4, .center_freq = 2427},
  1012. { .hw_value = 5, .center_freq = 2432},
  1013. { .hw_value = 6, .center_freq = 2437},
  1014. { .hw_value = 7, .center_freq = 2442},
  1015. { .hw_value = 8, .center_freq = 2447},
  1016. { .hw_value = 9, .center_freq = 2452},
  1017. { .hw_value = 10, .center_freq = 2457},
  1018. { .hw_value = 11, .center_freq = 2462},
  1019. { .hw_value = 12, .center_freq = 2467},
  1020. { .hw_value = 13, .center_freq = 2472},
  1021. };
  1022. static int wl1251_op_conf_tx(struct ieee80211_hw *hw, u16 queue,
  1023. const struct ieee80211_tx_queue_params *params)
  1024. {
  1025. struct wl1251 *wl = hw->priv;
  1026. int ret;
  1027. mutex_lock(&wl->mutex);
  1028. wl1251_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
  1029. ret = wl1251_ps_elp_wakeup(wl);
  1030. if (ret < 0)
  1031. goto out;
  1032. ret = wl1251_acx_ac_cfg(wl, wl1251_tx_get_queue(queue),
  1033. params->cw_min, params->cw_max,
  1034. params->aifs, params->txop);
  1035. if (ret < 0)
  1036. goto out_sleep;
  1037. ret = wl1251_acx_tid_cfg(wl, wl1251_tx_get_queue(queue),
  1038. CHANNEL_TYPE_EDCF,
  1039. wl1251_tx_get_queue(queue),
  1040. WL1251_ACX_PS_SCHEME_LEGACY,
  1041. WL1251_ACX_ACK_POLICY_LEGACY);
  1042. if (ret < 0)
  1043. goto out_sleep;
  1044. out_sleep:
  1045. wl1251_ps_elp_sleep(wl);
  1046. out:
  1047. mutex_unlock(&wl->mutex);
  1048. return ret;
  1049. }
  1050. /* can't be const, mac80211 writes to this */
  1051. static struct ieee80211_supported_band wl1251_band_2ghz = {
  1052. .channels = wl1251_channels,
  1053. .n_channels = ARRAY_SIZE(wl1251_channels),
  1054. .bitrates = wl1251_rates,
  1055. .n_bitrates = ARRAY_SIZE(wl1251_rates),
  1056. };
  1057. static const struct ieee80211_ops wl1251_ops = {
  1058. .start = wl1251_op_start,
  1059. .stop = wl1251_op_stop,
  1060. .add_interface = wl1251_op_add_interface,
  1061. .remove_interface = wl1251_op_remove_interface,
  1062. .config = wl1251_op_config,
  1063. .configure_filter = wl1251_op_configure_filter,
  1064. .tx = wl1251_op_tx,
  1065. .set_key = wl1251_op_set_key,
  1066. .hw_scan = wl1251_op_hw_scan,
  1067. .bss_info_changed = wl1251_op_bss_info_changed,
  1068. .set_rts_threshold = wl1251_op_set_rts_threshold,
  1069. .conf_tx = wl1251_op_conf_tx,
  1070. };
  1071. static int wl1251_register_hw(struct wl1251 *wl)
  1072. {
  1073. int ret;
  1074. if (wl->mac80211_registered)
  1075. return 0;
  1076. SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
  1077. ret = ieee80211_register_hw(wl->hw);
  1078. if (ret < 0) {
  1079. wl1251_error("unable to register mac80211 hw: %d", ret);
  1080. return ret;
  1081. }
  1082. wl->mac80211_registered = true;
  1083. wl1251_notice("loaded");
  1084. return 0;
  1085. }
  1086. int wl1251_init_ieee80211(struct wl1251 *wl)
  1087. {
  1088. int ret;
  1089. /* The tx descriptor buffer and the TKIP space */
  1090. wl->hw->extra_tx_headroom = sizeof(struct tx_double_buffer_desc)
  1091. + WL1251_TKIP_IV_SPACE;
  1092. /* unit us */
  1093. /* FIXME: find a proper value */
  1094. wl->hw->channel_change_time = 10000;
  1095. wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
  1096. IEEE80211_HW_NOISE_DBM |
  1097. IEEE80211_HW_SUPPORTS_PS |
  1098. IEEE80211_HW_BEACON_FILTER;
  1099. wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  1100. wl->hw->wiphy->max_scan_ssids = 1;
  1101. wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1251_band_2ghz;
  1102. wl->hw->queues = 4;
  1103. ret = wl1251_register_hw(wl);
  1104. if (ret)
  1105. goto out;
  1106. wl1251_debugfs_init(wl);
  1107. wl1251_notice("initialized");
  1108. ret = 0;
  1109. out:
  1110. return ret;
  1111. }
  1112. EXPORT_SYMBOL_GPL(wl1251_init_ieee80211);
  1113. struct ieee80211_hw *wl1251_alloc_hw(void)
  1114. {
  1115. struct ieee80211_hw *hw;
  1116. struct wl1251 *wl;
  1117. int i;
  1118. static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
  1119. hw = ieee80211_alloc_hw(sizeof(*wl), &wl1251_ops);
  1120. if (!hw) {
  1121. wl1251_error("could not alloc ieee80211_hw");
  1122. return ERR_PTR(-ENOMEM);
  1123. }
  1124. wl = hw->priv;
  1125. memset(wl, 0, sizeof(*wl));
  1126. wl->hw = hw;
  1127. wl->data_in_count = 0;
  1128. skb_queue_head_init(&wl->tx_queue);
  1129. INIT_WORK(&wl->filter_work, wl1251_filter_work);
  1130. INIT_DELAYED_WORK(&wl->elp_work, wl1251_elp_work);
  1131. wl->channel = WL1251_DEFAULT_CHANNEL;
  1132. wl->scanning = false;
  1133. wl->default_key = 0;
  1134. wl->listen_int = 1;
  1135. wl->rx_counter = 0;
  1136. wl->rx_handled = 0;
  1137. wl->rx_current_buffer = 0;
  1138. wl->rx_last_id = 0;
  1139. wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
  1140. wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
  1141. wl->elp = false;
  1142. wl->psm = 0;
  1143. wl->psm_requested = false;
  1144. wl->tx_queue_stopped = false;
  1145. wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
  1146. wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
  1147. wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
  1148. wl->vif = NULL;
  1149. for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
  1150. wl->tx_frames[i] = NULL;
  1151. wl->next_tx_complete = 0;
  1152. INIT_WORK(&wl->irq_work, wl1251_irq_work);
  1153. INIT_WORK(&wl->tx_work, wl1251_tx_work);
  1154. /*
  1155. * In case our MAC address is not correctly set,
  1156. * we use a random but Nokia MAC.
  1157. */
  1158. memcpy(wl->mac_addr, nokia_oui, 3);
  1159. get_random_bytes(wl->mac_addr + 3, 3);
  1160. wl->state = WL1251_STATE_OFF;
  1161. mutex_init(&wl->mutex);
  1162. wl->tx_mgmt_frm_rate = DEFAULT_HW_GEN_TX_RATE;
  1163. wl->tx_mgmt_frm_mod = DEFAULT_HW_GEN_MODULATION_TYPE;
  1164. wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
  1165. if (!wl->rx_descriptor) {
  1166. wl1251_error("could not allocate memory for rx descriptor");
  1167. ieee80211_free_hw(hw);
  1168. return ERR_PTR(-ENOMEM);
  1169. }
  1170. return hw;
  1171. }
  1172. EXPORT_SYMBOL_GPL(wl1251_alloc_hw);
  1173. int wl1251_free_hw(struct wl1251 *wl)
  1174. {
  1175. ieee80211_unregister_hw(wl->hw);
  1176. wl1251_debugfs_exit(wl);
  1177. kfree(wl->target_mem_map);
  1178. kfree(wl->data_path);
  1179. vfree(wl->fw);
  1180. wl->fw = NULL;
  1181. kfree(wl->nvs);
  1182. wl->nvs = NULL;
  1183. kfree(wl->rx_descriptor);
  1184. wl->rx_descriptor = NULL;
  1185. ieee80211_free_hw(wl->hw);
  1186. return 0;
  1187. }
  1188. EXPORT_SYMBOL_GPL(wl1251_free_hw);
  1189. MODULE_DESCRIPTION("TI wl1251 Wireles LAN Driver Core");
  1190. MODULE_LICENSE("GPL");
  1191. MODULE_AUTHOR("Kalle Valo <kalle.valo@nokia.com>");
  1192. MODULE_ALIAS("spi:wl1251");
  1193. MODULE_FIRMWARE(WL1251_FW_NAME);