main.c 31 KB

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