wl1251_main.c 32 KB

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