main.c 31 KB

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