main.c 32 KB

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