wl1251_main.c 31 KB

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