main.c 32 KB

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