wl1251_main.c 31 KB

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