main.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616
  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 <linux/netdevice.h>
  31. #include "wl1251.h"
  32. #include "wl12xx_80211.h"
  33. #include "reg.h"
  34. #include "io.h"
  35. #include "cmd.h"
  36. #include "event.h"
  37. #include "tx.h"
  38. #include "rx.h"
  39. #include "ps.h"
  40. #include "init.h"
  41. #include "debugfs.h"
  42. #include "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 int wl1251_power_off(struct wl1251 *wl)
  52. {
  53. return wl->if_ops->power(wl, false);
  54. }
  55. static int wl1251_power_on(struct wl1251 *wl)
  56. {
  57. return wl->if_ops->power(wl, 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 = kmemdup(fw->data, 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. 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_write_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
  121. elp_reg = wl1251_read_elp(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;
  128. ret = wl1251_power_on(wl);
  129. if (ret < 0)
  130. return ret;
  131. msleep(WL1251_POWER_ON_SLEEP);
  132. wl->if_ops->reset(wl);
  133. /* We don't need a real memory partition here, because we only want
  134. * to use the registers at this point. */
  135. wl1251_set_partition(wl,
  136. 0x00000000,
  137. 0x00000000,
  138. REGISTERS_BASE,
  139. REGISTERS_DOWN_SIZE);
  140. /* ELP module wake up */
  141. wl1251_fw_wakeup(wl);
  142. /* whal_FwCtrl_BootSm() */
  143. /* 0. read chip id from CHIP_ID */
  144. wl->chip_id = wl1251_reg_read32(wl, CHIP_ID_B);
  145. /* 1. check if chip id is valid */
  146. switch (wl->chip_id) {
  147. case CHIP_ID_1251_PG12:
  148. wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG12)",
  149. wl->chip_id);
  150. break;
  151. case CHIP_ID_1251_PG11:
  152. wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG11)",
  153. wl->chip_id);
  154. break;
  155. case CHIP_ID_1251_PG10:
  156. default:
  157. wl1251_error("unsupported chip id: 0x%x", wl->chip_id);
  158. ret = -ENODEV;
  159. goto out;
  160. }
  161. if (wl->fw == NULL) {
  162. ret = wl1251_fetch_firmware(wl);
  163. if (ret < 0)
  164. goto out;
  165. }
  166. if (wl->nvs == NULL && !wl->use_eeprom) {
  167. /* No NVS from netlink, try to get it from the filesystem */
  168. ret = wl1251_fetch_nvs(wl);
  169. if (ret < 0)
  170. goto out;
  171. }
  172. out:
  173. return ret;
  174. }
  175. #define WL1251_IRQ_LOOP_COUNT 10
  176. static void wl1251_irq_work(struct work_struct *work)
  177. {
  178. u32 intr, ctr = WL1251_IRQ_LOOP_COUNT;
  179. struct wl1251 *wl =
  180. container_of(work, struct wl1251, irq_work);
  181. int ret;
  182. mutex_lock(&wl->mutex);
  183. wl1251_debug(DEBUG_IRQ, "IRQ work");
  184. if (wl->state == WL1251_STATE_OFF)
  185. goto out;
  186. ret = wl1251_ps_elp_wakeup(wl);
  187. if (ret < 0)
  188. goto out;
  189. wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, WL1251_ACX_INTR_ALL);
  190. intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
  191. wl1251_debug(DEBUG_IRQ, "intr: 0x%x", intr);
  192. do {
  193. if (wl->data_path) {
  194. wl->rx_counter = wl1251_mem_read32(
  195. wl, wl->data_path->rx_control_addr);
  196. /* We handle a frmware bug here */
  197. switch ((wl->rx_counter - wl->rx_handled) & 0xf) {
  198. case 0:
  199. wl1251_debug(DEBUG_IRQ,
  200. "RX: FW and host in sync");
  201. intr &= ~WL1251_ACX_INTR_RX0_DATA;
  202. intr &= ~WL1251_ACX_INTR_RX1_DATA;
  203. break;
  204. case 1:
  205. wl1251_debug(DEBUG_IRQ, "RX: FW +1");
  206. intr |= WL1251_ACX_INTR_RX0_DATA;
  207. intr &= ~WL1251_ACX_INTR_RX1_DATA;
  208. break;
  209. case 2:
  210. wl1251_debug(DEBUG_IRQ, "RX: FW +2");
  211. intr |= WL1251_ACX_INTR_RX0_DATA;
  212. intr |= WL1251_ACX_INTR_RX1_DATA;
  213. break;
  214. default:
  215. wl1251_warning(
  216. "RX: FW and host out of sync: %d",
  217. wl->rx_counter - wl->rx_handled);
  218. break;
  219. }
  220. wl->rx_handled = wl->rx_counter;
  221. wl1251_debug(DEBUG_IRQ, "RX counter: %d",
  222. wl->rx_counter);
  223. }
  224. intr &= wl->intr_mask;
  225. if (intr == 0) {
  226. wl1251_debug(DEBUG_IRQ, "INTR is 0");
  227. goto out_sleep;
  228. }
  229. if (intr & WL1251_ACX_INTR_RX0_DATA) {
  230. wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX0_DATA");
  231. wl1251_rx(wl);
  232. }
  233. if (intr & WL1251_ACX_INTR_RX1_DATA) {
  234. wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX1_DATA");
  235. wl1251_rx(wl);
  236. }
  237. if (intr & WL1251_ACX_INTR_TX_RESULT) {
  238. wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_TX_RESULT");
  239. wl1251_tx_complete(wl);
  240. }
  241. if (intr & WL1251_ACX_INTR_EVENT_A) {
  242. wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_EVENT_A");
  243. wl1251_event_handle(wl, 0);
  244. }
  245. if (intr & WL1251_ACX_INTR_EVENT_B) {
  246. wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_EVENT_B");
  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. /*
  273. * Join command applies filters, and if we are not associated,
  274. * BSSID filter must be disabled for association to work.
  275. */
  276. if (is_zero_ether_addr(wl->bssid))
  277. wl->rx_config &= ~CFG_BSSID_FILTER_EN;
  278. ret = wl1251_cmd_join(wl, bss_type, channel, beacon_interval,
  279. dtim_period);
  280. if (ret < 0)
  281. goto out;
  282. ret = wl1251_event_wait(wl, JOIN_EVENT_COMPLETE_ID, 100);
  283. if (ret < 0)
  284. wl1251_warning("join timeout");
  285. out:
  286. return ret;
  287. }
  288. static void wl1251_op_tx(struct ieee80211_hw *hw,
  289. struct ieee80211_tx_control *control,
  290. struct sk_buff *skb)
  291. {
  292. struct wl1251 *wl = hw->priv;
  293. unsigned long flags;
  294. skb_queue_tail(&wl->tx_queue, skb);
  295. /*
  296. * The chip specific setup must run before the first TX packet -
  297. * before that, the tx_work will not be initialized!
  298. */
  299. ieee80211_queue_work(wl->hw, &wl->tx_work);
  300. /*
  301. * The workqueue is slow to process the tx_queue and we need stop
  302. * the queue here, otherwise the queue will get too long.
  303. */
  304. if (skb_queue_len(&wl->tx_queue) >= WL1251_TX_QUEUE_HIGH_WATERMARK) {
  305. wl1251_debug(DEBUG_TX, "op_tx: tx_queue full, stop queues");
  306. spin_lock_irqsave(&wl->wl_lock, flags);
  307. ieee80211_stop_queues(wl->hw);
  308. wl->tx_queue_stopped = true;
  309. spin_unlock_irqrestore(&wl->wl_lock, flags);
  310. }
  311. }
  312. static int wl1251_op_start(struct ieee80211_hw *hw)
  313. {
  314. struct wl1251 *wl = hw->priv;
  315. struct wiphy *wiphy = hw->wiphy;
  316. int ret = 0;
  317. wl1251_debug(DEBUG_MAC80211, "mac80211 start");
  318. mutex_lock(&wl->mutex);
  319. if (wl->state != WL1251_STATE_OFF) {
  320. wl1251_error("cannot start because not in off state: %d",
  321. wl->state);
  322. ret = -EBUSY;
  323. goto out;
  324. }
  325. ret = wl1251_chip_wakeup(wl);
  326. if (ret < 0)
  327. goto out;
  328. ret = wl1251_boot(wl);
  329. if (ret < 0)
  330. goto out;
  331. ret = wl1251_hw_init(wl);
  332. if (ret < 0)
  333. goto out;
  334. ret = wl1251_acx_station_id(wl);
  335. if (ret < 0)
  336. goto out;
  337. wl->state = WL1251_STATE_ON;
  338. wl1251_info("firmware booted (%s)", wl->fw_ver);
  339. /* update hw/fw version info in wiphy struct */
  340. wiphy->hw_version = wl->chip_id;
  341. strncpy(wiphy->fw_version, wl->fw_ver, sizeof(wiphy->fw_version));
  342. out:
  343. if (ret < 0)
  344. wl1251_power_off(wl);
  345. mutex_unlock(&wl->mutex);
  346. return ret;
  347. }
  348. static void wl1251_op_stop(struct ieee80211_hw *hw)
  349. {
  350. struct wl1251 *wl = hw->priv;
  351. wl1251_info("down");
  352. wl1251_debug(DEBUG_MAC80211, "mac80211 stop");
  353. mutex_lock(&wl->mutex);
  354. WARN_ON(wl->state != WL1251_STATE_ON);
  355. if (wl->scanning) {
  356. ieee80211_scan_completed(wl->hw, true);
  357. wl->scanning = false;
  358. }
  359. wl->state = WL1251_STATE_OFF;
  360. wl1251_disable_interrupts(wl);
  361. mutex_unlock(&wl->mutex);
  362. cancel_work_sync(&wl->irq_work);
  363. cancel_work_sync(&wl->tx_work);
  364. cancel_delayed_work_sync(&wl->elp_work);
  365. mutex_lock(&wl->mutex);
  366. /* let's notify MAC80211 about the remaining pending TX frames */
  367. wl1251_tx_flush(wl);
  368. wl1251_power_off(wl);
  369. memset(wl->bssid, 0, ETH_ALEN);
  370. wl->listen_int = 1;
  371. wl->bss_type = MAX_BSS_TYPE;
  372. wl->data_in_count = 0;
  373. wl->rx_counter = 0;
  374. wl->rx_handled = 0;
  375. wl->rx_current_buffer = 0;
  376. wl->rx_last_id = 0;
  377. wl->next_tx_complete = 0;
  378. wl->elp = false;
  379. wl->station_mode = STATION_ACTIVE_MODE;
  380. wl->psm_entry_retry = 0;
  381. wl->tx_queue_stopped = false;
  382. wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
  383. wl->rssi_thold = 0;
  384. wl->channel = WL1251_DEFAULT_CHANNEL;
  385. wl->monitor_present = false;
  386. wl->joined = false;
  387. wl1251_debugfs_reset(wl);
  388. mutex_unlock(&wl->mutex);
  389. }
  390. static int wl1251_op_add_interface(struct ieee80211_hw *hw,
  391. struct ieee80211_vif *vif)
  392. {
  393. struct wl1251 *wl = hw->priv;
  394. int ret = 0;
  395. vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
  396. IEEE80211_VIF_SUPPORTS_CQM_RSSI;
  397. wl1251_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
  398. vif->type, vif->addr);
  399. mutex_lock(&wl->mutex);
  400. if (wl->vif) {
  401. ret = -EBUSY;
  402. goto out;
  403. }
  404. wl->vif = vif;
  405. switch (vif->type) {
  406. case NL80211_IFTYPE_STATION:
  407. wl->bss_type = BSS_TYPE_STA_BSS;
  408. break;
  409. case NL80211_IFTYPE_ADHOC:
  410. wl->bss_type = BSS_TYPE_IBSS;
  411. break;
  412. default:
  413. ret = -EOPNOTSUPP;
  414. goto out;
  415. }
  416. if (!ether_addr_equal_unaligned(wl->mac_addr, vif->addr)) {
  417. memcpy(wl->mac_addr, vif->addr, ETH_ALEN);
  418. SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
  419. ret = wl1251_acx_station_id(wl);
  420. if (ret < 0)
  421. goto out;
  422. }
  423. out:
  424. mutex_unlock(&wl->mutex);
  425. return ret;
  426. }
  427. static void wl1251_op_remove_interface(struct ieee80211_hw *hw,
  428. struct ieee80211_vif *vif)
  429. {
  430. struct wl1251 *wl = hw->priv;
  431. mutex_lock(&wl->mutex);
  432. wl1251_debug(DEBUG_MAC80211, "mac80211 remove interface");
  433. wl->vif = NULL;
  434. memset(wl->bssid, 0, ETH_ALEN);
  435. mutex_unlock(&wl->mutex);
  436. }
  437. static int wl1251_build_null_data(struct wl1251 *wl)
  438. {
  439. struct sk_buff *skb = NULL;
  440. int size;
  441. void *ptr;
  442. int ret = -ENOMEM;
  443. if (wl->bss_type == BSS_TYPE_IBSS) {
  444. size = sizeof(struct wl12xx_null_data_template);
  445. ptr = NULL;
  446. } else {
  447. skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
  448. if (!skb)
  449. goto out;
  450. size = skb->len;
  451. ptr = skb->data;
  452. }
  453. ret = wl1251_cmd_template_set(wl, CMD_NULL_DATA, ptr, size);
  454. out:
  455. dev_kfree_skb(skb);
  456. if (ret)
  457. wl1251_warning("cmd buld null data failed: %d", ret);
  458. return ret;
  459. }
  460. static int wl1251_build_qos_null_data(struct wl1251 *wl)
  461. {
  462. struct ieee80211_qos_hdr template;
  463. memset(&template, 0, sizeof(template));
  464. memcpy(template.addr1, wl->bssid, ETH_ALEN);
  465. memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
  466. memcpy(template.addr3, wl->bssid, ETH_ALEN);
  467. template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  468. IEEE80211_STYPE_QOS_NULLFUNC |
  469. IEEE80211_FCTL_TODS);
  470. /* FIXME: not sure what priority to use here */
  471. template.qos_ctrl = cpu_to_le16(0);
  472. return wl1251_cmd_template_set(wl, CMD_QOS_NULL_DATA, &template,
  473. sizeof(template));
  474. }
  475. static bool wl1251_can_do_pm(struct ieee80211_conf *conf, struct wl1251 *wl)
  476. {
  477. return (conf->flags & IEEE80211_CONF_PS) && !wl->monitor_present;
  478. }
  479. static int wl1251_op_config(struct ieee80211_hw *hw, u32 changed)
  480. {
  481. struct wl1251 *wl = hw->priv;
  482. struct ieee80211_conf *conf = &hw->conf;
  483. int channel, ret = 0;
  484. channel = ieee80211_frequency_to_channel(
  485. conf->chandef.chan->center_freq);
  486. wl1251_debug(DEBUG_MAC80211,
  487. "mac80211 config ch %d monitor %s psm %s power %d",
  488. channel,
  489. conf->flags & IEEE80211_CONF_MONITOR ? "on" : "off",
  490. conf->flags & IEEE80211_CONF_PS ? "on" : "off",
  491. conf->power_level);
  492. mutex_lock(&wl->mutex);
  493. ret = wl1251_ps_elp_wakeup(wl);
  494. if (ret < 0)
  495. goto out;
  496. if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
  497. u32 mode;
  498. if (conf->flags & IEEE80211_CONF_MONITOR) {
  499. wl->monitor_present = true;
  500. mode = DF_SNIFF_MODE_ENABLE | DF_ENCRYPTION_DISABLE;
  501. } else {
  502. wl->monitor_present = false;
  503. mode = 0;
  504. }
  505. ret = wl1251_acx_feature_cfg(wl, mode);
  506. if (ret < 0)
  507. goto out_sleep;
  508. }
  509. if (channel != wl->channel) {
  510. wl->channel = channel;
  511. /*
  512. * Use ENABLE_RX command for channel switching when no
  513. * interface is present (monitor mode only).
  514. * This leaves the tx path disabled in firmware, whereas
  515. * the usual JOIN command seems to transmit some frames
  516. * at firmware level.
  517. */
  518. if (wl->vif == NULL) {
  519. wl->joined = false;
  520. ret = wl1251_cmd_data_path_rx(wl, wl->channel, 1);
  521. } else {
  522. ret = wl1251_join(wl, wl->bss_type, wl->channel,
  523. wl->beacon_int, wl->dtim_period);
  524. }
  525. if (ret < 0)
  526. goto out_sleep;
  527. }
  528. if (wl1251_can_do_pm(conf, wl) && !wl->psm_requested) {
  529. wl1251_debug(DEBUG_PSM, "psm enabled");
  530. wl->psm_requested = true;
  531. wl->dtim_period = conf->ps_dtim_period;
  532. ret = wl1251_acx_wr_tbtt_and_dtim(wl, wl->beacon_int,
  533. wl->dtim_period);
  534. /*
  535. * mac80211 enables PSM only if we're already associated.
  536. */
  537. ret = wl1251_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
  538. if (ret < 0)
  539. goto out_sleep;
  540. } else if (!wl1251_can_do_pm(conf, wl) && wl->psm_requested) {
  541. wl1251_debug(DEBUG_PSM, "psm disabled");
  542. wl->psm_requested = false;
  543. if (wl->station_mode != STATION_ACTIVE_MODE) {
  544. ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
  545. if (ret < 0)
  546. goto out_sleep;
  547. }
  548. }
  549. if (changed & IEEE80211_CONF_CHANGE_IDLE && !wl->scanning) {
  550. if (conf->flags & IEEE80211_CONF_IDLE) {
  551. ret = wl1251_ps_set_mode(wl, STATION_IDLE);
  552. if (ret < 0)
  553. goto out_sleep;
  554. } else {
  555. ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
  556. if (ret < 0)
  557. goto out_sleep;
  558. ret = wl1251_join(wl, wl->bss_type, wl->channel,
  559. wl->beacon_int, wl->dtim_period);
  560. if (ret < 0)
  561. goto out_sleep;
  562. }
  563. }
  564. if (conf->power_level != wl->power_level) {
  565. ret = wl1251_acx_tx_power(wl, conf->power_level);
  566. if (ret < 0)
  567. goto out_sleep;
  568. wl->power_level = conf->power_level;
  569. }
  570. out_sleep:
  571. wl1251_ps_elp_sleep(wl);
  572. out:
  573. mutex_unlock(&wl->mutex);
  574. return ret;
  575. }
  576. struct wl1251_filter_params {
  577. bool enabled;
  578. int mc_list_length;
  579. u8 mc_list[ACX_MC_ADDRESS_GROUP_MAX][ETH_ALEN];
  580. };
  581. static u64 wl1251_op_prepare_multicast(struct ieee80211_hw *hw,
  582. struct netdev_hw_addr_list *mc_list)
  583. {
  584. struct wl1251_filter_params *fp;
  585. struct netdev_hw_addr *ha;
  586. struct wl1251 *wl = hw->priv;
  587. if (unlikely(wl->state == WL1251_STATE_OFF))
  588. return 0;
  589. fp = kzalloc(sizeof(*fp), GFP_ATOMIC);
  590. if (!fp) {
  591. wl1251_error("Out of memory setting filters.");
  592. return 0;
  593. }
  594. /* update multicast filtering parameters */
  595. fp->mc_list_length = 0;
  596. if (netdev_hw_addr_list_count(mc_list) > ACX_MC_ADDRESS_GROUP_MAX) {
  597. fp->enabled = false;
  598. } else {
  599. fp->enabled = true;
  600. netdev_hw_addr_list_for_each(ha, mc_list) {
  601. memcpy(fp->mc_list[fp->mc_list_length],
  602. ha->addr, ETH_ALEN);
  603. fp->mc_list_length++;
  604. }
  605. }
  606. return (u64)(unsigned long)fp;
  607. }
  608. #define WL1251_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
  609. FIF_ALLMULTI | \
  610. FIF_FCSFAIL | \
  611. FIF_BCN_PRBRESP_PROMISC | \
  612. FIF_CONTROL | \
  613. FIF_OTHER_BSS | \
  614. FIF_PROBE_REQ)
  615. static void wl1251_op_configure_filter(struct ieee80211_hw *hw,
  616. unsigned int changed,
  617. unsigned int *total, u64 multicast)
  618. {
  619. struct wl1251_filter_params *fp = (void *)(unsigned long)multicast;
  620. struct wl1251 *wl = hw->priv;
  621. int ret;
  622. wl1251_debug(DEBUG_MAC80211, "mac80211 configure filter");
  623. *total &= WL1251_SUPPORTED_FILTERS;
  624. changed &= WL1251_SUPPORTED_FILTERS;
  625. if (changed == 0) {
  626. /* no filters which we support changed */
  627. kfree(fp);
  628. return;
  629. }
  630. mutex_lock(&wl->mutex);
  631. wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
  632. wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
  633. if (*total & FIF_PROMISC_IN_BSS) {
  634. wl->rx_config |= CFG_BSSID_FILTER_EN;
  635. wl->rx_config |= CFG_RX_ALL_GOOD;
  636. }
  637. if (*total & FIF_ALLMULTI)
  638. /*
  639. * CFG_MC_FILTER_EN in rx_config needs to be 0 to receive
  640. * all multicast frames
  641. */
  642. wl->rx_config &= ~CFG_MC_FILTER_EN;
  643. if (*total & FIF_FCSFAIL)
  644. wl->rx_filter |= CFG_RX_FCS_ERROR;
  645. if (*total & FIF_BCN_PRBRESP_PROMISC) {
  646. wl->rx_config &= ~CFG_BSSID_FILTER_EN;
  647. wl->rx_config &= ~CFG_SSID_FILTER_EN;
  648. }
  649. if (*total & FIF_CONTROL)
  650. wl->rx_filter |= CFG_RX_CTL_EN;
  651. if (*total & FIF_OTHER_BSS || is_zero_ether_addr(wl->bssid))
  652. wl->rx_config &= ~CFG_BSSID_FILTER_EN;
  653. if (*total & FIF_PROBE_REQ)
  654. wl->rx_filter |= CFG_RX_PREQ_EN;
  655. if (wl->state == WL1251_STATE_OFF)
  656. goto out;
  657. ret = wl1251_ps_elp_wakeup(wl);
  658. if (ret < 0)
  659. goto out;
  660. if (*total & FIF_ALLMULTI || *total & FIF_PROMISC_IN_BSS)
  661. ret = wl1251_acx_group_address_tbl(wl, false, NULL, 0);
  662. else if (fp)
  663. ret = wl1251_acx_group_address_tbl(wl, fp->enabled,
  664. fp->mc_list,
  665. fp->mc_list_length);
  666. if (ret < 0)
  667. goto out;
  668. /* send filters to firmware */
  669. wl1251_acx_rx_config(wl, wl->rx_config, wl->rx_filter);
  670. wl1251_ps_elp_sleep(wl);
  671. out:
  672. mutex_unlock(&wl->mutex);
  673. kfree(fp);
  674. }
  675. /* HW encryption */
  676. static int wl1251_set_key_type(struct wl1251 *wl,
  677. struct wl1251_cmd_set_keys *key,
  678. enum set_key_cmd cmd,
  679. struct ieee80211_key_conf *mac80211_key,
  680. const u8 *addr)
  681. {
  682. switch (mac80211_key->cipher) {
  683. case WLAN_CIPHER_SUITE_WEP40:
  684. case WLAN_CIPHER_SUITE_WEP104:
  685. if (is_broadcast_ether_addr(addr))
  686. key->key_type = KEY_WEP_DEFAULT;
  687. else
  688. key->key_type = KEY_WEP_ADDR;
  689. mac80211_key->hw_key_idx = mac80211_key->keyidx;
  690. break;
  691. case WLAN_CIPHER_SUITE_TKIP:
  692. if (is_broadcast_ether_addr(addr))
  693. key->key_type = KEY_TKIP_MIC_GROUP;
  694. else
  695. key->key_type = KEY_TKIP_MIC_PAIRWISE;
  696. mac80211_key->hw_key_idx = mac80211_key->keyidx;
  697. break;
  698. case WLAN_CIPHER_SUITE_CCMP:
  699. if (is_broadcast_ether_addr(addr))
  700. key->key_type = KEY_AES_GROUP;
  701. else
  702. key->key_type = KEY_AES_PAIRWISE;
  703. mac80211_key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  704. break;
  705. default:
  706. wl1251_error("Unknown key cipher 0x%x", mac80211_key->cipher);
  707. return -EOPNOTSUPP;
  708. }
  709. return 0;
  710. }
  711. static int wl1251_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  712. struct ieee80211_vif *vif,
  713. struct ieee80211_sta *sta,
  714. struct ieee80211_key_conf *key)
  715. {
  716. struct wl1251 *wl = hw->priv;
  717. struct wl1251_cmd_set_keys *wl_cmd;
  718. const u8 *addr;
  719. int ret;
  720. static const u8 bcast_addr[ETH_ALEN] =
  721. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  722. wl1251_debug(DEBUG_MAC80211, "mac80211 set key");
  723. wl_cmd = kzalloc(sizeof(*wl_cmd), GFP_KERNEL);
  724. if (!wl_cmd) {
  725. ret = -ENOMEM;
  726. goto out;
  727. }
  728. addr = sta ? sta->addr : bcast_addr;
  729. wl1251_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
  730. wl1251_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
  731. wl1251_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
  732. key->cipher, key->keyidx, key->keylen, key->flags);
  733. wl1251_dump(DEBUG_CRYPT, "KEY: ", key->key, key->keylen);
  734. if (is_zero_ether_addr(addr)) {
  735. /* We dont support TX only encryption */
  736. ret = -EOPNOTSUPP;
  737. goto out;
  738. }
  739. mutex_lock(&wl->mutex);
  740. switch (cmd) {
  741. case SET_KEY:
  742. if (wl->monitor_present) {
  743. ret = -EOPNOTSUPP;
  744. goto out_unlock;
  745. }
  746. wl_cmd->key_action = KEY_ADD_OR_REPLACE;
  747. break;
  748. case DISABLE_KEY:
  749. wl_cmd->key_action = KEY_REMOVE;
  750. break;
  751. default:
  752. wl1251_error("Unsupported key cmd 0x%x", cmd);
  753. break;
  754. }
  755. ret = wl1251_ps_elp_wakeup(wl);
  756. if (ret < 0)
  757. goto out_unlock;
  758. ret = wl1251_set_key_type(wl, wl_cmd, cmd, key, addr);
  759. if (ret < 0) {
  760. wl1251_error("Set KEY type failed");
  761. goto out_sleep;
  762. }
  763. if (wl_cmd->key_type != KEY_WEP_DEFAULT)
  764. memcpy(wl_cmd->addr, addr, ETH_ALEN);
  765. if ((wl_cmd->key_type == KEY_TKIP_MIC_GROUP) ||
  766. (wl_cmd->key_type == KEY_TKIP_MIC_PAIRWISE)) {
  767. /*
  768. * We get the key in the following form:
  769. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  770. * but the target is expecting:
  771. * TKIP - RX MIC - TX MIC
  772. */
  773. memcpy(wl_cmd->key, key->key, 16);
  774. memcpy(wl_cmd->key + 16, key->key + 24, 8);
  775. memcpy(wl_cmd->key + 24, key->key + 16, 8);
  776. } else {
  777. memcpy(wl_cmd->key, key->key, key->keylen);
  778. }
  779. wl_cmd->key_size = key->keylen;
  780. wl_cmd->id = key->keyidx;
  781. wl_cmd->ssid_profile = 0;
  782. wl1251_dump(DEBUG_CRYPT, "TARGET KEY: ", wl_cmd, sizeof(*wl_cmd));
  783. ret = wl1251_cmd_send(wl, CMD_SET_KEYS, wl_cmd, sizeof(*wl_cmd));
  784. if (ret < 0) {
  785. wl1251_warning("could not set keys");
  786. goto out_sleep;
  787. }
  788. out_sleep:
  789. wl1251_ps_elp_sleep(wl);
  790. out_unlock:
  791. mutex_unlock(&wl->mutex);
  792. out:
  793. kfree(wl_cmd);
  794. return ret;
  795. }
  796. static int wl1251_op_hw_scan(struct ieee80211_hw *hw,
  797. struct ieee80211_vif *vif,
  798. struct ieee80211_scan_request *hw_req)
  799. {
  800. struct cfg80211_scan_request *req = &hw_req->req;
  801. struct wl1251 *wl = hw->priv;
  802. struct sk_buff *skb;
  803. size_t ssid_len = 0;
  804. u8 *ssid = NULL;
  805. int ret;
  806. wl1251_debug(DEBUG_MAC80211, "mac80211 hw scan");
  807. if (req->n_ssids) {
  808. ssid = req->ssids[0].ssid;
  809. ssid_len = req->ssids[0].ssid_len;
  810. }
  811. mutex_lock(&wl->mutex);
  812. if (wl->scanning) {
  813. wl1251_debug(DEBUG_SCAN, "scan already in progress");
  814. ret = -EINVAL;
  815. goto out;
  816. }
  817. ret = wl1251_ps_elp_wakeup(wl);
  818. if (ret < 0)
  819. goto out;
  820. if (hw->conf.flags & IEEE80211_CONF_IDLE) {
  821. ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
  822. if (ret < 0)
  823. goto out_sleep;
  824. ret = wl1251_join(wl, wl->bss_type, wl->channel,
  825. wl->beacon_int, wl->dtim_period);
  826. if (ret < 0)
  827. goto out_sleep;
  828. }
  829. skb = ieee80211_probereq_get(wl->hw, wl->vif->addr, ssid, ssid_len,
  830. req->ie_len);
  831. if (!skb) {
  832. ret = -ENOMEM;
  833. goto out_idle;
  834. }
  835. if (req->ie_len)
  836. memcpy(skb_put(skb, req->ie_len), req->ie, req->ie_len);
  837. ret = wl1251_cmd_template_set(wl, CMD_PROBE_REQ, skb->data,
  838. skb->len);
  839. dev_kfree_skb(skb);
  840. if (ret < 0)
  841. goto out_idle;
  842. ret = wl1251_cmd_trigger_scan_to(wl, 0);
  843. if (ret < 0)
  844. goto out_idle;
  845. wl->scanning = true;
  846. ret = wl1251_cmd_scan(wl, ssid, ssid_len, req->channels,
  847. req->n_channels, WL1251_SCAN_NUM_PROBES);
  848. if (ret < 0) {
  849. wl1251_debug(DEBUG_SCAN, "scan failed %d", ret);
  850. wl->scanning = false;
  851. goto out_idle;
  852. }
  853. goto out_sleep;
  854. out_idle:
  855. if (hw->conf.flags & IEEE80211_CONF_IDLE)
  856. ret = wl1251_ps_set_mode(wl, STATION_IDLE);
  857. out_sleep:
  858. wl1251_ps_elp_sleep(wl);
  859. out:
  860. mutex_unlock(&wl->mutex);
  861. return ret;
  862. }
  863. static int wl1251_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  864. {
  865. struct wl1251 *wl = hw->priv;
  866. int ret;
  867. mutex_lock(&wl->mutex);
  868. ret = wl1251_ps_elp_wakeup(wl);
  869. if (ret < 0)
  870. goto out;
  871. ret = wl1251_acx_rts_threshold(wl, (u16) value);
  872. if (ret < 0)
  873. wl1251_warning("wl1251_op_set_rts_threshold failed: %d", ret);
  874. wl1251_ps_elp_sleep(wl);
  875. out:
  876. mutex_unlock(&wl->mutex);
  877. return ret;
  878. }
  879. static void wl1251_op_bss_info_changed(struct ieee80211_hw *hw,
  880. struct ieee80211_vif *vif,
  881. struct ieee80211_bss_conf *bss_conf,
  882. u32 changed)
  883. {
  884. struct wl1251 *wl = hw->priv;
  885. struct sk_buff *beacon, *skb;
  886. bool enable;
  887. int ret;
  888. wl1251_debug(DEBUG_MAC80211, "mac80211 bss info changed");
  889. mutex_lock(&wl->mutex);
  890. ret = wl1251_ps_elp_wakeup(wl);
  891. if (ret < 0)
  892. goto out;
  893. if (changed & BSS_CHANGED_CQM) {
  894. ret = wl1251_acx_low_rssi(wl, bss_conf->cqm_rssi_thold,
  895. WL1251_DEFAULT_LOW_RSSI_WEIGHT,
  896. WL1251_DEFAULT_LOW_RSSI_DEPTH,
  897. WL1251_ACX_LOW_RSSI_TYPE_EDGE);
  898. if (ret < 0)
  899. goto out;
  900. wl->rssi_thold = bss_conf->cqm_rssi_thold;
  901. }
  902. if ((changed & BSS_CHANGED_BSSID) &&
  903. memcmp(wl->bssid, bss_conf->bssid, ETH_ALEN)) {
  904. memcpy(wl->bssid, bss_conf->bssid, ETH_ALEN);
  905. if (!is_zero_ether_addr(wl->bssid)) {
  906. ret = wl1251_build_null_data(wl);
  907. if (ret < 0)
  908. goto out_sleep;
  909. ret = wl1251_build_qos_null_data(wl);
  910. if (ret < 0)
  911. goto out_sleep;
  912. ret = wl1251_join(wl, wl->bss_type, wl->channel,
  913. wl->beacon_int, wl->dtim_period);
  914. if (ret < 0)
  915. goto out_sleep;
  916. }
  917. }
  918. if (changed & BSS_CHANGED_ASSOC) {
  919. if (bss_conf->assoc) {
  920. wl->beacon_int = bss_conf->beacon_int;
  921. skb = ieee80211_pspoll_get(wl->hw, wl->vif);
  922. if (!skb)
  923. goto out_sleep;
  924. ret = wl1251_cmd_template_set(wl, CMD_PS_POLL,
  925. skb->data,
  926. skb->len);
  927. dev_kfree_skb(skb);
  928. if (ret < 0)
  929. goto out_sleep;
  930. ret = wl1251_acx_aid(wl, bss_conf->aid);
  931. if (ret < 0)
  932. goto out_sleep;
  933. } else {
  934. /* use defaults when not associated */
  935. wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
  936. wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
  937. }
  938. }
  939. if (changed & BSS_CHANGED_ERP_SLOT) {
  940. if (bss_conf->use_short_slot)
  941. ret = wl1251_acx_slot(wl, SLOT_TIME_SHORT);
  942. else
  943. ret = wl1251_acx_slot(wl, SLOT_TIME_LONG);
  944. if (ret < 0) {
  945. wl1251_warning("Set slot time failed %d", ret);
  946. goto out_sleep;
  947. }
  948. }
  949. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  950. if (bss_conf->use_short_preamble)
  951. wl1251_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
  952. else
  953. wl1251_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
  954. }
  955. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  956. if (bss_conf->use_cts_prot)
  957. ret = wl1251_acx_cts_protect(wl, CTSPROTECT_ENABLE);
  958. else
  959. ret = wl1251_acx_cts_protect(wl, CTSPROTECT_DISABLE);
  960. if (ret < 0) {
  961. wl1251_warning("Set ctsprotect failed %d", ret);
  962. goto out_sleep;
  963. }
  964. }
  965. if (changed & BSS_CHANGED_ARP_FILTER) {
  966. __be32 addr = bss_conf->arp_addr_list[0];
  967. WARN_ON(wl->bss_type != BSS_TYPE_STA_BSS);
  968. enable = bss_conf->arp_addr_cnt == 1 && bss_conf->assoc;
  969. wl1251_acx_arp_ip_filter(wl, enable, addr);
  970. if (ret < 0)
  971. goto out_sleep;
  972. }
  973. if (changed & BSS_CHANGED_BEACON) {
  974. beacon = ieee80211_beacon_get(hw, vif);
  975. if (!beacon)
  976. goto out_sleep;
  977. ret = wl1251_cmd_template_set(wl, CMD_BEACON, beacon->data,
  978. beacon->len);
  979. if (ret < 0) {
  980. dev_kfree_skb(beacon);
  981. goto out_sleep;
  982. }
  983. ret = wl1251_cmd_template_set(wl, CMD_PROBE_RESP, beacon->data,
  984. beacon->len);
  985. dev_kfree_skb(beacon);
  986. if (ret < 0)
  987. goto out_sleep;
  988. ret = wl1251_join(wl, wl->bss_type, wl->channel,
  989. wl->beacon_int, wl->dtim_period);
  990. if (ret < 0)
  991. goto out_sleep;
  992. }
  993. out_sleep:
  994. wl1251_ps_elp_sleep(wl);
  995. out:
  996. mutex_unlock(&wl->mutex);
  997. }
  998. /* can't be const, mac80211 writes to this */
  999. static struct ieee80211_rate wl1251_rates[] = {
  1000. { .bitrate = 10,
  1001. .hw_value = 0x1,
  1002. .hw_value_short = 0x1, },
  1003. { .bitrate = 20,
  1004. .hw_value = 0x2,
  1005. .hw_value_short = 0x2,
  1006. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  1007. { .bitrate = 55,
  1008. .hw_value = 0x4,
  1009. .hw_value_short = 0x4,
  1010. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  1011. { .bitrate = 110,
  1012. .hw_value = 0x20,
  1013. .hw_value_short = 0x20,
  1014. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  1015. { .bitrate = 60,
  1016. .hw_value = 0x8,
  1017. .hw_value_short = 0x8, },
  1018. { .bitrate = 90,
  1019. .hw_value = 0x10,
  1020. .hw_value_short = 0x10, },
  1021. { .bitrate = 120,
  1022. .hw_value = 0x40,
  1023. .hw_value_short = 0x40, },
  1024. { .bitrate = 180,
  1025. .hw_value = 0x80,
  1026. .hw_value_short = 0x80, },
  1027. { .bitrate = 240,
  1028. .hw_value = 0x200,
  1029. .hw_value_short = 0x200, },
  1030. { .bitrate = 360,
  1031. .hw_value = 0x400,
  1032. .hw_value_short = 0x400, },
  1033. { .bitrate = 480,
  1034. .hw_value = 0x800,
  1035. .hw_value_short = 0x800, },
  1036. { .bitrate = 540,
  1037. .hw_value = 0x1000,
  1038. .hw_value_short = 0x1000, },
  1039. };
  1040. /* can't be const, mac80211 writes to this */
  1041. static struct ieee80211_channel wl1251_channels[] = {
  1042. { .hw_value = 1, .center_freq = 2412},
  1043. { .hw_value = 2, .center_freq = 2417},
  1044. { .hw_value = 3, .center_freq = 2422},
  1045. { .hw_value = 4, .center_freq = 2427},
  1046. { .hw_value = 5, .center_freq = 2432},
  1047. { .hw_value = 6, .center_freq = 2437},
  1048. { .hw_value = 7, .center_freq = 2442},
  1049. { .hw_value = 8, .center_freq = 2447},
  1050. { .hw_value = 9, .center_freq = 2452},
  1051. { .hw_value = 10, .center_freq = 2457},
  1052. { .hw_value = 11, .center_freq = 2462},
  1053. { .hw_value = 12, .center_freq = 2467},
  1054. { .hw_value = 13, .center_freq = 2472},
  1055. };
  1056. static int wl1251_op_conf_tx(struct ieee80211_hw *hw,
  1057. struct ieee80211_vif *vif, u16 queue,
  1058. const struct ieee80211_tx_queue_params *params)
  1059. {
  1060. enum wl1251_acx_ps_scheme ps_scheme;
  1061. struct wl1251 *wl = hw->priv;
  1062. int ret;
  1063. mutex_lock(&wl->mutex);
  1064. wl1251_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
  1065. ret = wl1251_ps_elp_wakeup(wl);
  1066. if (ret < 0)
  1067. goto out;
  1068. /* mac80211 uses units of 32 usec */
  1069. ret = wl1251_acx_ac_cfg(wl, wl1251_tx_get_queue(queue),
  1070. params->cw_min, params->cw_max,
  1071. params->aifs, params->txop * 32);
  1072. if (ret < 0)
  1073. goto out_sleep;
  1074. if (params->uapsd)
  1075. ps_scheme = WL1251_ACX_PS_SCHEME_UPSD_TRIGGER;
  1076. else
  1077. ps_scheme = WL1251_ACX_PS_SCHEME_LEGACY;
  1078. ret = wl1251_acx_tid_cfg(wl, wl1251_tx_get_queue(queue),
  1079. CHANNEL_TYPE_EDCF,
  1080. wl1251_tx_get_queue(queue), ps_scheme,
  1081. WL1251_ACX_ACK_POLICY_LEGACY);
  1082. if (ret < 0)
  1083. goto out_sleep;
  1084. out_sleep:
  1085. wl1251_ps_elp_sleep(wl);
  1086. out:
  1087. mutex_unlock(&wl->mutex);
  1088. return ret;
  1089. }
  1090. static int wl1251_op_get_survey(struct ieee80211_hw *hw, int idx,
  1091. struct survey_info *survey)
  1092. {
  1093. struct wl1251 *wl = hw->priv;
  1094. struct ieee80211_conf *conf = &hw->conf;
  1095. if (idx != 0)
  1096. return -ENOENT;
  1097. survey->channel = conf->chandef.chan;
  1098. survey->filled = SURVEY_INFO_NOISE_DBM;
  1099. survey->noise = wl->noise;
  1100. return 0;
  1101. }
  1102. /* can't be const, mac80211 writes to this */
  1103. static struct ieee80211_supported_band wl1251_band_2ghz = {
  1104. .channels = wl1251_channels,
  1105. .n_channels = ARRAY_SIZE(wl1251_channels),
  1106. .bitrates = wl1251_rates,
  1107. .n_bitrates = ARRAY_SIZE(wl1251_rates),
  1108. };
  1109. static const struct ieee80211_ops wl1251_ops = {
  1110. .start = wl1251_op_start,
  1111. .stop = wl1251_op_stop,
  1112. .add_interface = wl1251_op_add_interface,
  1113. .remove_interface = wl1251_op_remove_interface,
  1114. .config = wl1251_op_config,
  1115. .prepare_multicast = wl1251_op_prepare_multicast,
  1116. .configure_filter = wl1251_op_configure_filter,
  1117. .tx = wl1251_op_tx,
  1118. .set_key = wl1251_op_set_key,
  1119. .hw_scan = wl1251_op_hw_scan,
  1120. .bss_info_changed = wl1251_op_bss_info_changed,
  1121. .set_rts_threshold = wl1251_op_set_rts_threshold,
  1122. .conf_tx = wl1251_op_conf_tx,
  1123. .get_survey = wl1251_op_get_survey,
  1124. };
  1125. static int wl1251_read_eeprom_byte(struct wl1251 *wl, off_t offset, u8 *data)
  1126. {
  1127. unsigned long timeout;
  1128. wl1251_reg_write32(wl, EE_ADDR, offset);
  1129. wl1251_reg_write32(wl, EE_CTL, EE_CTL_READ);
  1130. /* EE_CTL_READ clears when data is ready */
  1131. timeout = jiffies + msecs_to_jiffies(100);
  1132. while (1) {
  1133. if (!(wl1251_reg_read32(wl, EE_CTL) & EE_CTL_READ))
  1134. break;
  1135. if (time_after(jiffies, timeout))
  1136. return -ETIMEDOUT;
  1137. msleep(1);
  1138. }
  1139. *data = wl1251_reg_read32(wl, EE_DATA);
  1140. return 0;
  1141. }
  1142. static int wl1251_read_eeprom(struct wl1251 *wl, off_t offset,
  1143. u8 *data, size_t len)
  1144. {
  1145. size_t i;
  1146. int ret;
  1147. wl1251_reg_write32(wl, EE_START, 0);
  1148. for (i = 0; i < len; i++) {
  1149. ret = wl1251_read_eeprom_byte(wl, offset + i, &data[i]);
  1150. if (ret < 0)
  1151. return ret;
  1152. }
  1153. return 0;
  1154. }
  1155. static int wl1251_read_eeprom_mac(struct wl1251 *wl)
  1156. {
  1157. u8 mac[ETH_ALEN];
  1158. int i, ret;
  1159. wl1251_set_partition(wl, 0, 0, REGISTERS_BASE, REGISTERS_DOWN_SIZE);
  1160. ret = wl1251_read_eeprom(wl, 0x1c, mac, sizeof(mac));
  1161. if (ret < 0) {
  1162. wl1251_warning("failed to read MAC address from EEPROM");
  1163. return ret;
  1164. }
  1165. /* MAC is stored in reverse order */
  1166. for (i = 0; i < ETH_ALEN; i++)
  1167. wl->mac_addr[i] = mac[ETH_ALEN - i - 1];
  1168. return 0;
  1169. }
  1170. static int wl1251_register_hw(struct wl1251 *wl)
  1171. {
  1172. int ret;
  1173. if (wl->mac80211_registered)
  1174. return 0;
  1175. SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
  1176. ret = ieee80211_register_hw(wl->hw);
  1177. if (ret < 0) {
  1178. wl1251_error("unable to register mac80211 hw: %d", ret);
  1179. return ret;
  1180. }
  1181. wl->mac80211_registered = true;
  1182. wl1251_notice("loaded");
  1183. return 0;
  1184. }
  1185. int wl1251_init_ieee80211(struct wl1251 *wl)
  1186. {
  1187. int ret;
  1188. /* The tx descriptor buffer and the TKIP space */
  1189. wl->hw->extra_tx_headroom = sizeof(struct tx_double_buffer_desc)
  1190. + WL1251_TKIP_IV_SPACE;
  1191. /* unit us */
  1192. /* FIXME: find a proper value */
  1193. wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
  1194. IEEE80211_HW_SUPPORTS_PS |
  1195. IEEE80211_HW_SUPPORTS_UAPSD;
  1196. wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  1197. BIT(NL80211_IFTYPE_ADHOC);
  1198. wl->hw->wiphy->max_scan_ssids = 1;
  1199. wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1251_band_2ghz;
  1200. wl->hw->queues = 4;
  1201. if (wl->use_eeprom)
  1202. wl1251_read_eeprom_mac(wl);
  1203. ret = wl1251_register_hw(wl);
  1204. if (ret)
  1205. goto out;
  1206. wl1251_debugfs_init(wl);
  1207. wl1251_notice("initialized");
  1208. ret = 0;
  1209. out:
  1210. return ret;
  1211. }
  1212. EXPORT_SYMBOL_GPL(wl1251_init_ieee80211);
  1213. struct ieee80211_hw *wl1251_alloc_hw(void)
  1214. {
  1215. struct ieee80211_hw *hw;
  1216. struct wl1251 *wl;
  1217. int i;
  1218. static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
  1219. hw = ieee80211_alloc_hw(sizeof(*wl), &wl1251_ops);
  1220. if (!hw) {
  1221. wl1251_error("could not alloc ieee80211_hw");
  1222. return ERR_PTR(-ENOMEM);
  1223. }
  1224. wl = hw->priv;
  1225. memset(wl, 0, sizeof(*wl));
  1226. wl->hw = hw;
  1227. wl->data_in_count = 0;
  1228. skb_queue_head_init(&wl->tx_queue);
  1229. INIT_DELAYED_WORK(&wl->elp_work, wl1251_elp_work);
  1230. wl->channel = WL1251_DEFAULT_CHANNEL;
  1231. wl->monitor_present = false;
  1232. wl->joined = false;
  1233. wl->scanning = false;
  1234. wl->bss_type = MAX_BSS_TYPE;
  1235. wl->default_key = 0;
  1236. wl->listen_int = 1;
  1237. wl->rx_counter = 0;
  1238. wl->rx_handled = 0;
  1239. wl->rx_current_buffer = 0;
  1240. wl->rx_last_id = 0;
  1241. wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
  1242. wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
  1243. wl->elp = false;
  1244. wl->station_mode = STATION_ACTIVE_MODE;
  1245. wl->psm_requested = false;
  1246. wl->psm_entry_retry = 0;
  1247. wl->tx_queue_stopped = false;
  1248. wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
  1249. wl->rssi_thold = 0;
  1250. wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
  1251. wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
  1252. wl->vif = NULL;
  1253. for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
  1254. wl->tx_frames[i] = NULL;
  1255. wl->next_tx_complete = 0;
  1256. INIT_WORK(&wl->irq_work, wl1251_irq_work);
  1257. INIT_WORK(&wl->tx_work, wl1251_tx_work);
  1258. /*
  1259. * In case our MAC address is not correctly set,
  1260. * we use a random but Nokia MAC.
  1261. */
  1262. memcpy(wl->mac_addr, nokia_oui, 3);
  1263. get_random_bytes(wl->mac_addr + 3, 3);
  1264. wl->state = WL1251_STATE_OFF;
  1265. mutex_init(&wl->mutex);
  1266. wl->tx_mgmt_frm_rate = DEFAULT_HW_GEN_TX_RATE;
  1267. wl->tx_mgmt_frm_mod = DEFAULT_HW_GEN_MODULATION_TYPE;
  1268. wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
  1269. if (!wl->rx_descriptor) {
  1270. wl1251_error("could not allocate memory for rx descriptor");
  1271. ieee80211_free_hw(hw);
  1272. return ERR_PTR(-ENOMEM);
  1273. }
  1274. return hw;
  1275. }
  1276. EXPORT_SYMBOL_GPL(wl1251_alloc_hw);
  1277. int wl1251_free_hw(struct wl1251 *wl)
  1278. {
  1279. ieee80211_unregister_hw(wl->hw);
  1280. wl1251_debugfs_exit(wl);
  1281. kfree(wl->target_mem_map);
  1282. kfree(wl->data_path);
  1283. vfree(wl->fw);
  1284. wl->fw = NULL;
  1285. kfree(wl->nvs);
  1286. wl->nvs = NULL;
  1287. kfree(wl->rx_descriptor);
  1288. wl->rx_descriptor = NULL;
  1289. ieee80211_free_hw(wl->hw);
  1290. return 0;
  1291. }
  1292. EXPORT_SYMBOL_GPL(wl1251_free_hw);
  1293. MODULE_DESCRIPTION("TI wl1251 Wireles LAN Driver Core");
  1294. MODULE_LICENSE("GPL");
  1295. MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");
  1296. MODULE_FIRMWARE(WL1251_FW_NAME);
  1297. MODULE_FIRMWARE(WL1251_NVS_NAME);