wl1271_main.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542
  1. /*
  2. * This file is part of wl1271
  3. *
  4. * Copyright (C) 2008-2009 Nokia Corporation
  5. *
  6. * Contact: Luciano Coelho <luciano.coelho@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/platform_device.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/firmware.h>
  27. #include <linux/delay.h>
  28. #include <linux/irq.h>
  29. #include <linux/spi/spi.h>
  30. #include <linux/crc32.h>
  31. #include <linux/etherdevice.h>
  32. #include <linux/vmalloc.h>
  33. #include <linux/spi/wl12xx.h>
  34. #include "wl1271.h"
  35. #include "wl12xx_80211.h"
  36. #include "wl1271_reg.h"
  37. #include "wl1271_spi.h"
  38. #include "wl1271_event.h"
  39. #include "wl1271_tx.h"
  40. #include "wl1271_rx.h"
  41. #include "wl1271_ps.h"
  42. #include "wl1271_init.h"
  43. #include "wl1271_debugfs.h"
  44. #include "wl1271_cmd.h"
  45. #include "wl1271_boot.h"
  46. static int wl1271_plt_init(struct wl1271 *wl)
  47. {
  48. int ret;
  49. ret = wl1271_acx_init_mem_config(wl);
  50. if (ret < 0)
  51. return ret;
  52. ret = wl1271_cmd_data_path(wl, wl->channel, 1);
  53. if (ret < 0)
  54. return ret;
  55. return 0;
  56. }
  57. static void wl1271_disable_interrupts(struct wl1271 *wl)
  58. {
  59. disable_irq(wl->irq);
  60. }
  61. static void wl1271_power_off(struct wl1271 *wl)
  62. {
  63. wl->set_power(false);
  64. }
  65. static void wl1271_power_on(struct wl1271 *wl)
  66. {
  67. wl->set_power(true);
  68. }
  69. static void wl1271_fw_status(struct wl1271 *wl, struct wl1271_fw_status *status)
  70. {
  71. u32 total = 0;
  72. int i;
  73. /*
  74. * FIXME: Reading the FW status directly from the registers seems to
  75. * be the right thing to do, but it doesn't work. And in the
  76. * reference driver, there is a workaround called
  77. * USE_SDIO_24M_WORKAROUND, which reads the status from memory
  78. * instead, so we do the same here.
  79. */
  80. wl1271_spi_mem_read(wl, STATUS_MEM_ADDRESS, status, sizeof(*status));
  81. wl1271_debug(DEBUG_IRQ, "intr: 0x%x (fw_rx_counter = %d, "
  82. "drv_rx_counter = %d, tx_results_counter = %d)",
  83. status->intr,
  84. status->fw_rx_counter,
  85. status->drv_rx_counter,
  86. status->tx_results_counter);
  87. /* update number of available TX blocks */
  88. for (i = 0; i < NUM_TX_QUEUES; i++) {
  89. u32 cnt = status->tx_released_blks[i] - wl->tx_blocks_freed[i];
  90. wl->tx_blocks_freed[i] = status->tx_released_blks[i];
  91. wl->tx_blocks_available += cnt;
  92. total += cnt;
  93. }
  94. /* if more blocks are available now, schedule some tx work */
  95. if (total && !skb_queue_empty(&wl->tx_queue))
  96. ieee80211_queue_work(wl->hw, &wl->tx_work);
  97. /* update the host-chipset time offset */
  98. wl->time_offset = jiffies_to_usecs(jiffies) - status->fw_localtime;
  99. }
  100. #define WL1271_IRQ_MAX_LOOPS 10
  101. static void wl1271_irq_work(struct work_struct *work)
  102. {
  103. u32 intr, ctr = WL1271_IRQ_MAX_LOOPS;
  104. int ret;
  105. struct wl1271 *wl =
  106. container_of(work, struct wl1271, irq_work);
  107. mutex_lock(&wl->mutex);
  108. wl1271_debug(DEBUG_IRQ, "IRQ work");
  109. if (wl->state == WL1271_STATE_OFF)
  110. goto out;
  111. ret = wl1271_ps_elp_wakeup(wl, true);
  112. if (ret < 0)
  113. goto out;
  114. wl1271_reg_write32(wl, ACX_REG_INTERRUPT_MASK, WL1271_ACX_INTR_ALL);
  115. intr = wl1271_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
  116. if (!intr) {
  117. wl1271_debug(DEBUG_IRQ, "Zero interrupt received.");
  118. goto out_sleep;
  119. }
  120. intr &= WL1271_INTR_MASK;
  121. do {
  122. wl1271_fw_status(wl, wl->fw_status);
  123. if (intr & (WL1271_ACX_INTR_EVENT_A |
  124. WL1271_ACX_INTR_EVENT_B)) {
  125. wl1271_debug(DEBUG_IRQ,
  126. "WL1271_ACX_INTR_EVENT (0x%x)", intr);
  127. if (intr & WL1271_ACX_INTR_EVENT_A)
  128. wl1271_event_handle(wl, 0);
  129. else
  130. wl1271_event_handle(wl, 1);
  131. }
  132. if (intr & WL1271_ACX_INTR_INIT_COMPLETE)
  133. wl1271_debug(DEBUG_IRQ,
  134. "WL1271_ACX_INTR_INIT_COMPLETE");
  135. if (intr & WL1271_ACX_INTR_HW_AVAILABLE)
  136. wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_HW_AVAILABLE");
  137. if (intr & WL1271_ACX_INTR_DATA) {
  138. u8 tx_res_cnt = wl->fw_status->tx_results_counter -
  139. wl->tx_results_count;
  140. wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_DATA");
  141. /* check for tx results */
  142. if (tx_res_cnt)
  143. wl1271_tx_complete(wl, tx_res_cnt);
  144. wl1271_rx(wl, wl->fw_status);
  145. }
  146. intr = wl1271_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
  147. intr &= WL1271_INTR_MASK;
  148. } while (intr && --ctr);
  149. out_sleep:
  150. wl1271_reg_write32(wl, ACX_REG_INTERRUPT_MASK,
  151. WL1271_ACX_INTR_ALL & ~(WL1271_INTR_MASK));
  152. wl1271_ps_elp_sleep(wl);
  153. out:
  154. mutex_unlock(&wl->mutex);
  155. }
  156. static irqreturn_t wl1271_irq(int irq, void *cookie)
  157. {
  158. struct wl1271 *wl;
  159. unsigned long flags;
  160. wl1271_debug(DEBUG_IRQ, "IRQ");
  161. wl = cookie;
  162. /* complete the ELP completion */
  163. spin_lock_irqsave(&wl->wl_lock, flags);
  164. if (wl->elp_compl) {
  165. complete(wl->elp_compl);
  166. wl->elp_compl = NULL;
  167. }
  168. ieee80211_queue_work(wl->hw, &wl->irq_work);
  169. spin_unlock_irqrestore(&wl->wl_lock, flags);
  170. return IRQ_HANDLED;
  171. }
  172. static int wl1271_fetch_firmware(struct wl1271 *wl)
  173. {
  174. const struct firmware *fw;
  175. int ret;
  176. ret = request_firmware(&fw, WL1271_FW_NAME, &wl->spi->dev);
  177. if (ret < 0) {
  178. wl1271_error("could not get firmware: %d", ret);
  179. return ret;
  180. }
  181. if (fw->size % 4) {
  182. wl1271_error("firmware size is not multiple of 32 bits: %zu",
  183. fw->size);
  184. ret = -EILSEQ;
  185. goto out;
  186. }
  187. wl->fw_len = fw->size;
  188. wl->fw = vmalloc(wl->fw_len);
  189. if (!wl->fw) {
  190. wl1271_error("could not allocate memory for the firmware");
  191. ret = -ENOMEM;
  192. goto out;
  193. }
  194. memcpy(wl->fw, fw->data, wl->fw_len);
  195. ret = 0;
  196. out:
  197. release_firmware(fw);
  198. return ret;
  199. }
  200. static int wl1271_fetch_nvs(struct wl1271 *wl)
  201. {
  202. const struct firmware *fw;
  203. int ret;
  204. ret = request_firmware(&fw, WL1271_NVS_NAME, &wl->spi->dev);
  205. if (ret < 0) {
  206. wl1271_error("could not get nvs file: %d", ret);
  207. return ret;
  208. }
  209. if (fw->size % 4) {
  210. wl1271_error("nvs size is not multiple of 32 bits: %zu",
  211. fw->size);
  212. ret = -EILSEQ;
  213. goto out;
  214. }
  215. wl->nvs_len = fw->size;
  216. wl->nvs = kmalloc(wl->nvs_len, GFP_KERNEL);
  217. if (!wl->nvs) {
  218. wl1271_error("could not allocate memory for the nvs file");
  219. ret = -ENOMEM;
  220. goto out;
  221. }
  222. memcpy(wl->nvs, fw->data, wl->nvs_len);
  223. ret = 0;
  224. out:
  225. release_firmware(fw);
  226. return ret;
  227. }
  228. static void wl1271_fw_wakeup(struct wl1271 *wl)
  229. {
  230. u32 elp_reg;
  231. elp_reg = ELPCTRL_WAKE_UP;
  232. wl1271_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
  233. }
  234. static int wl1271_setup(struct wl1271 *wl)
  235. {
  236. wl->fw_status = kmalloc(sizeof(*wl->fw_status), GFP_KERNEL);
  237. if (!wl->fw_status)
  238. return -ENOMEM;
  239. wl->tx_res_if = kmalloc(sizeof(*wl->tx_res_if), GFP_KERNEL);
  240. if (!wl->tx_res_if) {
  241. kfree(wl->fw_status);
  242. return -ENOMEM;
  243. }
  244. INIT_WORK(&wl->irq_work, wl1271_irq_work);
  245. INIT_WORK(&wl->tx_work, wl1271_tx_work);
  246. return 0;
  247. }
  248. static int wl1271_chip_wakeup(struct wl1271 *wl)
  249. {
  250. int ret = 0;
  251. wl1271_power_on(wl);
  252. msleep(WL1271_POWER_ON_SLEEP);
  253. wl1271_spi_reset(wl);
  254. wl1271_spi_init(wl);
  255. /* We don't need a real memory partition here, because we only want
  256. * to use the registers at this point. */
  257. wl1271_set_partition(wl,
  258. 0x00000000,
  259. 0x00000000,
  260. REGISTERS_BASE,
  261. REGISTERS_DOWN_SIZE);
  262. /* ELP module wake up */
  263. wl1271_fw_wakeup(wl);
  264. /* whal_FwCtrl_BootSm() */
  265. /* 0. read chip id from CHIP_ID */
  266. wl->chip.id = wl1271_reg_read32(wl, CHIP_ID_B);
  267. /* 1. check if chip id is valid */
  268. switch (wl->chip.id) {
  269. case CHIP_ID_1271_PG10:
  270. wl1271_warning("chip id 0x%x (1271 PG10) support is obsolete",
  271. wl->chip.id);
  272. ret = wl1271_setup(wl);
  273. if (ret < 0)
  274. goto out;
  275. break;
  276. case CHIP_ID_1271_PG20:
  277. wl1271_debug(DEBUG_BOOT, "chip id 0x%x (1271 PG20)",
  278. wl->chip.id);
  279. ret = wl1271_setup(wl);
  280. if (ret < 0)
  281. goto out;
  282. break;
  283. default:
  284. wl1271_error("unsupported chip id: 0x%x", wl->chip.id);
  285. ret = -ENODEV;
  286. goto out;
  287. }
  288. if (wl->fw == NULL) {
  289. ret = wl1271_fetch_firmware(wl);
  290. if (ret < 0)
  291. goto out;
  292. }
  293. /* No NVS from netlink, try to get it from the filesystem */
  294. if (wl->nvs == NULL) {
  295. ret = wl1271_fetch_nvs(wl);
  296. if (ret < 0)
  297. goto out;
  298. }
  299. out:
  300. return ret;
  301. }
  302. struct wl1271_filter_params {
  303. unsigned int filters;
  304. unsigned int changed;
  305. int mc_list_length;
  306. u8 mc_list[ACX_MC_ADDRESS_GROUP_MAX][ETH_ALEN];
  307. };
  308. #define WL1271_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
  309. FIF_ALLMULTI | \
  310. FIF_FCSFAIL | \
  311. FIF_BCN_PRBRESP_PROMISC | \
  312. FIF_CONTROL | \
  313. FIF_OTHER_BSS)
  314. static void wl1271_filter_work(struct work_struct *work)
  315. {
  316. struct wl1271 *wl =
  317. container_of(work, struct wl1271, filter_work);
  318. struct wl1271_filter_params *fp;
  319. unsigned long flags;
  320. bool enabled = true;
  321. int ret;
  322. /* first, get the filter parameters */
  323. spin_lock_irqsave(&wl->wl_lock, flags);
  324. fp = wl->filter_params;
  325. wl->filter_params = NULL;
  326. spin_unlock_irqrestore(&wl->wl_lock, flags);
  327. if (!fp)
  328. return;
  329. /* then, lock the mutex without risk of lock-up */
  330. mutex_lock(&wl->mutex);
  331. if (wl->state == WL1271_STATE_OFF)
  332. goto out;
  333. ret = wl1271_ps_elp_wakeup(wl, false);
  334. if (ret < 0)
  335. goto out;
  336. /* configure the mc filter regardless of the changed flags */
  337. if (fp->filters & FIF_ALLMULTI)
  338. enabled = false;
  339. ret = wl1271_acx_group_address_tbl(wl, enabled,
  340. fp->mc_list, fp->mc_list_length);
  341. if (ret < 0)
  342. goto out_sleep;
  343. /* determine, whether supported filter values have changed */
  344. if (fp->changed == 0)
  345. goto out;
  346. /* apply configured filters */
  347. ret = wl1271_cmd_join(wl);
  348. if (ret < 0)
  349. goto out_sleep;
  350. out_sleep:
  351. wl1271_ps_elp_sleep(wl);
  352. out:
  353. mutex_unlock(&wl->mutex);
  354. kfree(fp);
  355. }
  356. int wl1271_plt_start(struct wl1271 *wl)
  357. {
  358. int ret;
  359. mutex_lock(&wl->mutex);
  360. wl1271_notice("power up");
  361. if (wl->state != WL1271_STATE_OFF) {
  362. wl1271_error("cannot go into PLT state because not "
  363. "in off state: %d", wl->state);
  364. ret = -EBUSY;
  365. goto out;
  366. }
  367. wl->state = WL1271_STATE_PLT;
  368. ret = wl1271_chip_wakeup(wl);
  369. if (ret < 0)
  370. goto out;
  371. ret = wl1271_boot(wl);
  372. if (ret < 0)
  373. goto out;
  374. wl1271_notice("firmware booted in PLT mode (%s)", wl->chip.fw_ver);
  375. ret = wl1271_plt_init(wl);
  376. if (ret < 0)
  377. goto out;
  378. out:
  379. mutex_unlock(&wl->mutex);
  380. return ret;
  381. }
  382. int wl1271_plt_stop(struct wl1271 *wl)
  383. {
  384. int ret = 0;
  385. mutex_lock(&wl->mutex);
  386. wl1271_notice("power down");
  387. if (wl->state != WL1271_STATE_PLT) {
  388. wl1271_error("cannot power down because not in PLT "
  389. "state: %d", wl->state);
  390. ret = -EBUSY;
  391. goto out;
  392. }
  393. wl1271_disable_interrupts(wl);
  394. wl1271_power_off(wl);
  395. wl->state = WL1271_STATE_OFF;
  396. out:
  397. mutex_unlock(&wl->mutex);
  398. return ret;
  399. }
  400. static int wl1271_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  401. {
  402. struct wl1271 *wl = hw->priv;
  403. skb_queue_tail(&wl->tx_queue, skb);
  404. /*
  405. * The chip specific setup must run before the first TX packet -
  406. * before that, the tx_work will not be initialized!
  407. */
  408. ieee80211_queue_work(wl->hw, &wl->tx_work);
  409. /*
  410. * The workqueue is slow to process the tx_queue and we need stop
  411. * the queue here, otherwise the queue will get too long.
  412. */
  413. if (skb_queue_len(&wl->tx_queue) >= WL1271_TX_QUEUE_MAX_LENGTH) {
  414. ieee80211_stop_queues(wl->hw);
  415. /*
  416. * FIXME: this is racy, the variable is not properly
  417. * protected. Maybe fix this by removing the stupid
  418. * variable altogether and checking the real queue state?
  419. */
  420. wl->tx_queue_stopped = true;
  421. }
  422. return NETDEV_TX_OK;
  423. }
  424. static int wl1271_op_start(struct ieee80211_hw *hw)
  425. {
  426. struct wl1271 *wl = hw->priv;
  427. int ret = 0;
  428. wl1271_debug(DEBUG_MAC80211, "mac80211 start");
  429. mutex_lock(&wl->mutex);
  430. if (wl->state != WL1271_STATE_OFF) {
  431. wl1271_error("cannot start because not in off state: %d",
  432. wl->state);
  433. ret = -EBUSY;
  434. goto out;
  435. }
  436. ret = wl1271_chip_wakeup(wl);
  437. if (ret < 0)
  438. goto out;
  439. ret = wl1271_boot(wl);
  440. if (ret < 0)
  441. goto out;
  442. ret = wl1271_hw_init(wl);
  443. if (ret < 0)
  444. goto out;
  445. wl->state = WL1271_STATE_ON;
  446. wl1271_info("firmware booted (%s)", wl->chip.fw_ver);
  447. out:
  448. if (ret < 0)
  449. wl1271_power_off(wl);
  450. mutex_unlock(&wl->mutex);
  451. return ret;
  452. }
  453. static void wl1271_op_stop(struct ieee80211_hw *hw)
  454. {
  455. struct wl1271 *wl = hw->priv;
  456. unsigned long flags;
  457. int i;
  458. wl1271_info("down");
  459. wl1271_debug(DEBUG_MAC80211, "mac80211 stop");
  460. /* complete/cancel ongoing work */
  461. cancel_work_sync(&wl->filter_work);
  462. spin_lock_irqsave(&wl->wl_lock, flags);
  463. kfree(wl->filter_params);
  464. wl->filter_params = NULL;
  465. spin_unlock_irqrestore(&wl->wl_lock, flags);
  466. mutex_lock(&wl->mutex);
  467. WARN_ON(wl->state != WL1271_STATE_ON);
  468. if (wl->scanning) {
  469. mutex_unlock(&wl->mutex);
  470. ieee80211_scan_completed(wl->hw, true);
  471. mutex_lock(&wl->mutex);
  472. wl->scanning = false;
  473. }
  474. wl->state = WL1271_STATE_OFF;
  475. wl1271_disable_interrupts(wl);
  476. mutex_unlock(&wl->mutex);
  477. cancel_work_sync(&wl->irq_work);
  478. cancel_work_sync(&wl->tx_work);
  479. cancel_work_sync(&wl->filter_work);
  480. mutex_lock(&wl->mutex);
  481. /* let's notify MAC80211 about the remaining pending TX frames */
  482. wl1271_tx_flush(wl);
  483. wl1271_power_off(wl);
  484. memset(wl->bssid, 0, ETH_ALEN);
  485. memset(wl->ssid, 0, IW_ESSID_MAX_SIZE + 1);
  486. wl->ssid_len = 0;
  487. wl->listen_int = 1;
  488. wl->bss_type = MAX_BSS_TYPE;
  489. wl->band = IEEE80211_BAND_2GHZ;
  490. wl->rx_counter = 0;
  491. wl->elp = false;
  492. wl->psm = 0;
  493. wl->tx_queue_stopped = false;
  494. wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
  495. wl->tx_blocks_available = 0;
  496. wl->tx_results_count = 0;
  497. wl->tx_packets_count = 0;
  498. wl->tx_security_last_seq = 0;
  499. wl->tx_security_seq_16 = 0;
  500. wl->tx_security_seq_32 = 0;
  501. wl->time_offset = 0;
  502. wl->session_counter = 0;
  503. for (i = 0; i < NUM_TX_QUEUES; i++)
  504. wl->tx_blocks_freed[i] = 0;
  505. wl1271_debugfs_reset(wl);
  506. mutex_unlock(&wl->mutex);
  507. }
  508. static int wl1271_op_add_interface(struct ieee80211_hw *hw,
  509. struct ieee80211_if_init_conf *conf)
  510. {
  511. struct wl1271 *wl = hw->priv;
  512. int ret = 0;
  513. wl1271_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
  514. conf->type, conf->mac_addr);
  515. mutex_lock(&wl->mutex);
  516. switch (conf->type) {
  517. case NL80211_IFTYPE_STATION:
  518. wl->bss_type = BSS_TYPE_STA_BSS;
  519. break;
  520. case NL80211_IFTYPE_ADHOC:
  521. wl->bss_type = BSS_TYPE_IBSS;
  522. break;
  523. default:
  524. ret = -EOPNOTSUPP;
  525. goto out;
  526. }
  527. /* FIXME: what if conf->mac_addr changes? */
  528. out:
  529. mutex_unlock(&wl->mutex);
  530. return ret;
  531. }
  532. static void wl1271_op_remove_interface(struct ieee80211_hw *hw,
  533. struct ieee80211_if_init_conf *conf)
  534. {
  535. wl1271_debug(DEBUG_MAC80211, "mac80211 remove interface");
  536. }
  537. #if 0
  538. static int wl1271_op_config_interface(struct ieee80211_hw *hw,
  539. struct ieee80211_vif *vif,
  540. struct ieee80211_if_conf *conf)
  541. {
  542. struct wl1271 *wl = hw->priv;
  543. struct sk_buff *beacon;
  544. int ret;
  545. wl1271_debug(DEBUG_MAC80211, "mac80211 config_interface bssid %pM",
  546. conf->bssid);
  547. wl1271_dump_ascii(DEBUG_MAC80211, "ssid: ", conf->ssid,
  548. conf->ssid_len);
  549. mutex_lock(&wl->mutex);
  550. ret = wl1271_ps_elp_wakeup(wl, false);
  551. if (ret < 0)
  552. goto out;
  553. memcpy(wl->bssid, conf->bssid, ETH_ALEN);
  554. ret = wl1271_cmd_build_null_data(wl);
  555. if (ret < 0)
  556. goto out_sleep;
  557. wl->ssid_len = conf->ssid_len;
  558. if (wl->ssid_len)
  559. memcpy(wl->ssid, conf->ssid, wl->ssid_len);
  560. if (wl->bss_type != BSS_TYPE_IBSS) {
  561. ret = wl1271_cmd_join(wl);
  562. if (ret < 0)
  563. goto out_sleep;
  564. }
  565. if (conf->changed & IEEE80211_IFCC_BEACON) {
  566. beacon = ieee80211_beacon_get(hw, vif);
  567. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_BEACON,
  568. beacon->data, beacon->len);
  569. if (ret < 0) {
  570. dev_kfree_skb(beacon);
  571. goto out_sleep;
  572. }
  573. ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PROBE_RESPONSE,
  574. beacon->data, beacon->len);
  575. dev_kfree_skb(beacon);
  576. if (ret < 0)
  577. goto out_sleep;
  578. ret = wl1271_cmd_join(wl);
  579. if (ret < 0)
  580. goto out_sleep;
  581. }
  582. out_sleep:
  583. wl1271_ps_elp_sleep(wl);
  584. out:
  585. mutex_unlock(&wl->mutex);
  586. return ret;
  587. }
  588. #endif
  589. static int wl1271_op_config(struct ieee80211_hw *hw, u32 changed)
  590. {
  591. struct wl1271 *wl = hw->priv;
  592. struct ieee80211_conf *conf = &hw->conf;
  593. int channel, ret = 0;
  594. channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
  595. wl1271_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
  596. channel,
  597. conf->flags & IEEE80211_CONF_PS ? "on" : "off",
  598. conf->power_level);
  599. mutex_lock(&wl->mutex);
  600. wl->band = conf->channel->band;
  601. ret = wl1271_ps_elp_wakeup(wl, false);
  602. if (ret < 0)
  603. goto out;
  604. if (channel != wl->channel) {
  605. u8 old_channel = wl->channel;
  606. wl->channel = channel;
  607. ret = wl1271_cmd_join(wl);
  608. if (ret < 0) {
  609. wl->channel = old_channel;
  610. goto out_sleep;
  611. }
  612. }
  613. ret = wl1271_cmd_build_null_data(wl);
  614. if (ret < 0)
  615. goto out_sleep;
  616. if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
  617. wl1271_info("psm enabled");
  618. wl->psm_requested = true;
  619. /*
  620. * We enter PSM only if we're already associated.
  621. * If we're not, we'll enter it when joining an SSID,
  622. * through the bss_info_changed() hook.
  623. */
  624. ret = wl1271_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
  625. } else if (!(conf->flags & IEEE80211_CONF_PS) &&
  626. wl->psm_requested) {
  627. wl1271_info("psm disabled");
  628. wl->psm_requested = false;
  629. if (wl->psm)
  630. ret = wl1271_ps_set_mode(wl, STATION_ACTIVE_MODE);
  631. }
  632. if (conf->power_level != wl->power_level) {
  633. ret = wl1271_acx_tx_power(wl, conf->power_level);
  634. if (ret < 0)
  635. goto out;
  636. wl->power_level = conf->power_level;
  637. }
  638. out_sleep:
  639. wl1271_ps_elp_sleep(wl);
  640. out:
  641. mutex_unlock(&wl->mutex);
  642. return ret;
  643. }
  644. static u64 wl1271_op_prepare_multicast(struct ieee80211_hw *hw, int mc_count,
  645. struct dev_addr_list *mc_list)
  646. {
  647. struct wl1271 *wl = hw->priv;
  648. struct wl1271_filter_params *fp;
  649. unsigned long flags;
  650. int i;
  651. /*
  652. * FIXME: we should return a hash that will be passed to
  653. * configure_filter() instead of saving everything in the context.
  654. */
  655. fp = kzalloc(sizeof(*fp), GFP_KERNEL);
  656. if (!fp) {
  657. wl1271_error("Out of memory setting filters.");
  658. return 0;
  659. }
  660. /* update multicast filtering parameters */
  661. if (mc_count > ACX_MC_ADDRESS_GROUP_MAX) {
  662. mc_count = 0;
  663. fp->filters |= FIF_ALLMULTI;
  664. }
  665. fp->mc_list_length = 0;
  666. for (i = 0; i < mc_count; i++) {
  667. if (mc_list->da_addrlen == ETH_ALEN) {
  668. memcpy(fp->mc_list[fp->mc_list_length],
  669. mc_list->da_addr, ETH_ALEN);
  670. fp->mc_list_length++;
  671. } else
  672. wl1271_warning("Unknown mc address length.");
  673. }
  674. spin_lock_irqsave(&wl->wl_lock, flags);
  675. kfree(wl->filter_params);
  676. wl->filter_params = fp;
  677. spin_unlock_irqrestore(&wl->wl_lock, flags);
  678. return 1;
  679. }
  680. static void wl1271_op_configure_filter(struct ieee80211_hw *hw,
  681. unsigned int changed,
  682. unsigned int *total, u64 multicast)
  683. {
  684. struct wl1271 *wl = hw->priv;
  685. wl1271_debug(DEBUG_MAC80211, "mac80211 configure filter");
  686. *total &= WL1271_SUPPORTED_FILTERS;
  687. changed &= WL1271_SUPPORTED_FILTERS;
  688. if (!multicast)
  689. return;
  690. /*
  691. * FIXME: for now we are still using a workqueue for filter
  692. * configuration, but with the new mac80211, this is not needed,
  693. * since configure_filter can now sleep. We now have
  694. * prepare_multicast, which needs to be atomic instead.
  695. */
  696. /* store current filter config */
  697. wl->filter_params->filters = *total;
  698. wl->filter_params->changed = changed;
  699. ieee80211_queue_work(wl->hw, &wl->filter_work);
  700. }
  701. static int wl1271_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  702. struct ieee80211_vif *vif,
  703. struct ieee80211_sta *sta,
  704. struct ieee80211_key_conf *key_conf)
  705. {
  706. struct wl1271 *wl = hw->priv;
  707. const u8 *addr;
  708. int ret;
  709. u32 tx_seq_32 = 0;
  710. u16 tx_seq_16 = 0;
  711. u8 key_type;
  712. static const u8 bcast_addr[ETH_ALEN] =
  713. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  714. wl1271_debug(DEBUG_MAC80211, "mac80211 set key");
  715. addr = sta ? sta->addr : bcast_addr;
  716. wl1271_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
  717. wl1271_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
  718. wl1271_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
  719. key_conf->alg, key_conf->keyidx,
  720. key_conf->keylen, key_conf->flags);
  721. wl1271_dump(DEBUG_CRYPT, "KEY: ", key_conf->key, key_conf->keylen);
  722. if (is_zero_ether_addr(addr)) {
  723. /* We dont support TX only encryption */
  724. ret = -EOPNOTSUPP;
  725. goto out;
  726. }
  727. mutex_lock(&wl->mutex);
  728. ret = wl1271_ps_elp_wakeup(wl, false);
  729. if (ret < 0)
  730. goto out_unlock;
  731. switch (key_conf->alg) {
  732. case ALG_WEP:
  733. key_type = KEY_WEP;
  734. key_conf->hw_key_idx = key_conf->keyidx;
  735. break;
  736. case ALG_TKIP:
  737. key_type = KEY_TKIP;
  738. key_conf->hw_key_idx = key_conf->keyidx;
  739. tx_seq_32 = wl->tx_security_seq_32;
  740. tx_seq_16 = wl->tx_security_seq_16;
  741. break;
  742. case ALG_CCMP:
  743. key_type = KEY_AES;
  744. key_conf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  745. tx_seq_32 = wl->tx_security_seq_32;
  746. tx_seq_16 = wl->tx_security_seq_16;
  747. break;
  748. default:
  749. wl1271_error("Unknown key algo 0x%x", key_conf->alg);
  750. ret = -EOPNOTSUPP;
  751. goto out_sleep;
  752. }
  753. switch (cmd) {
  754. case SET_KEY:
  755. ret = wl1271_cmd_set_key(wl, KEY_ADD_OR_REPLACE,
  756. key_conf->keyidx, key_type,
  757. key_conf->keylen, key_conf->key,
  758. addr, tx_seq_32, tx_seq_16);
  759. if (ret < 0) {
  760. wl1271_error("Could not add or replace key");
  761. goto out_sleep;
  762. }
  763. break;
  764. case DISABLE_KEY:
  765. ret = wl1271_cmd_set_key(wl, KEY_REMOVE,
  766. key_conf->keyidx, key_type,
  767. key_conf->keylen, key_conf->key,
  768. addr, 0, 0);
  769. if (ret < 0) {
  770. wl1271_error("Could not remove key");
  771. goto out_sleep;
  772. }
  773. break;
  774. default:
  775. wl1271_error("Unsupported key cmd 0x%x", cmd);
  776. ret = -EOPNOTSUPP;
  777. goto out_sleep;
  778. break;
  779. }
  780. out_sleep:
  781. wl1271_ps_elp_sleep(wl);
  782. out_unlock:
  783. mutex_unlock(&wl->mutex);
  784. out:
  785. return ret;
  786. }
  787. static int wl1271_op_hw_scan(struct ieee80211_hw *hw,
  788. struct cfg80211_scan_request *req)
  789. {
  790. struct wl1271 *wl = hw->priv;
  791. int ret;
  792. u8 *ssid = NULL;
  793. size_t ssid_len = 0;
  794. wl1271_debug(DEBUG_MAC80211, "mac80211 hw scan");
  795. if (req->n_ssids) {
  796. ssid = req->ssids[0].ssid;
  797. ssid_len = req->ssids[0].ssid_len;
  798. }
  799. mutex_lock(&wl->mutex);
  800. ret = wl1271_ps_elp_wakeup(wl, false);
  801. if (ret < 0)
  802. goto out;
  803. ret = wl1271_cmd_scan(hw->priv, ssid, ssid_len, 1, 0, 13, 3);
  804. wl1271_ps_elp_sleep(wl);
  805. out:
  806. mutex_unlock(&wl->mutex);
  807. return ret;
  808. }
  809. static int wl1271_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  810. {
  811. struct wl1271 *wl = hw->priv;
  812. int ret;
  813. mutex_lock(&wl->mutex);
  814. ret = wl1271_ps_elp_wakeup(wl, false);
  815. if (ret < 0)
  816. goto out;
  817. ret = wl1271_acx_rts_threshold(wl, (u16) value);
  818. if (ret < 0)
  819. wl1271_warning("wl1271_op_set_rts_threshold failed: %d", ret);
  820. wl1271_ps_elp_sleep(wl);
  821. out:
  822. mutex_unlock(&wl->mutex);
  823. return ret;
  824. }
  825. static u32 wl1271_enabled_rates_get(struct wl1271 *wl, u64 basic_rate_set)
  826. {
  827. struct ieee80211_supported_band *band;
  828. u32 enabled_rates = 0;
  829. int bit;
  830. band = wl->hw->wiphy->bands[wl->band];
  831. for (bit = 0; bit < band->n_bitrates; bit++) {
  832. if (basic_rate_set & 0x1)
  833. enabled_rates |= band->bitrates[bit].hw_value;
  834. basic_rate_set >>= 1;
  835. }
  836. return enabled_rates;
  837. }
  838. static void wl1271_op_bss_info_changed(struct ieee80211_hw *hw,
  839. struct ieee80211_vif *vif,
  840. struct ieee80211_bss_conf *bss_conf,
  841. u32 changed)
  842. {
  843. enum wl1271_cmd_ps_mode mode;
  844. struct wl1271 *wl = hw->priv;
  845. int ret;
  846. wl1271_debug(DEBUG_MAC80211, "mac80211 bss info changed");
  847. mutex_lock(&wl->mutex);
  848. ret = wl1271_ps_elp_wakeup(wl, false);
  849. if (ret < 0)
  850. goto out;
  851. if (changed & BSS_CHANGED_ASSOC) {
  852. if (bss_conf->assoc) {
  853. wl->beacon_int = bss_conf->beacon_int;
  854. wl->dtim_period = bss_conf->dtim_period;
  855. wl->aid = bss_conf->aid;
  856. ret = wl1271_cmd_join(wl);
  857. if (ret < 0) {
  858. wl1271_warning("Association configuration "
  859. "failed %d", ret);
  860. goto out_sleep;
  861. }
  862. ret = wl1271_cmd_build_ps_poll(wl, wl->aid);
  863. if (ret < 0)
  864. goto out_sleep;
  865. ret = wl1271_acx_aid(wl, wl->aid);
  866. if (ret < 0)
  867. goto out_sleep;
  868. /* If we want to go in PSM but we're not there yet */
  869. if (wl->psm_requested && !wl->psm) {
  870. mode = STATION_POWER_SAVE_MODE;
  871. ret = wl1271_ps_set_mode(wl, mode);
  872. if (ret < 0)
  873. goto out_sleep;
  874. }
  875. } else {
  876. /* use defaults when not associated */
  877. wl->beacon_int = WL1271_DEFAULT_BEACON_INT;
  878. wl->dtim_period = WL1271_DEFAULT_DTIM_PERIOD;
  879. wl->basic_rate_set = WL1271_DEFAULT_BASIC_RATE_SET;
  880. wl->aid = 0;
  881. }
  882. }
  883. if (changed & BSS_CHANGED_ERP_SLOT) {
  884. if (bss_conf->use_short_slot)
  885. ret = wl1271_acx_slot(wl, SLOT_TIME_SHORT);
  886. else
  887. ret = wl1271_acx_slot(wl, SLOT_TIME_LONG);
  888. if (ret < 0) {
  889. wl1271_warning("Set slot time failed %d", ret);
  890. goto out_sleep;
  891. }
  892. }
  893. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  894. if (bss_conf->use_short_preamble)
  895. wl1271_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
  896. else
  897. wl1271_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
  898. }
  899. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  900. if (bss_conf->use_cts_prot)
  901. ret = wl1271_acx_cts_protect(wl, CTSPROTECT_ENABLE);
  902. else
  903. ret = wl1271_acx_cts_protect(wl, CTSPROTECT_DISABLE);
  904. if (ret < 0) {
  905. wl1271_warning("Set ctsprotect failed %d", ret);
  906. goto out_sleep;
  907. }
  908. }
  909. if (changed & BSS_CHANGED_BASIC_RATES) {
  910. wl->basic_rate_set = wl1271_enabled_rates_get(
  911. wl, bss_conf->basic_rates);
  912. ret = wl1271_acx_rate_policies(wl, wl->basic_rate_set);
  913. if (ret < 0) {
  914. wl1271_warning("Set rate policies failed %d", ret);
  915. goto out_sleep;
  916. }
  917. ret = wl1271_cmd_join(wl);
  918. if (ret < 0) {
  919. wl1271_warning("Join with new basic rate "
  920. "set failed %d", ret);
  921. goto out_sleep;
  922. }
  923. }
  924. out_sleep:
  925. wl1271_ps_elp_sleep(wl);
  926. out:
  927. mutex_unlock(&wl->mutex);
  928. }
  929. /* can't be const, mac80211 writes to this */
  930. static struct ieee80211_rate wl1271_rates[] = {
  931. { .bitrate = 10,
  932. .hw_value = 0x1,
  933. .hw_value_short = 0x1, },
  934. { .bitrate = 20,
  935. .hw_value = 0x2,
  936. .hw_value_short = 0x2,
  937. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  938. { .bitrate = 55,
  939. .hw_value = 0x4,
  940. .hw_value_short = 0x4,
  941. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  942. { .bitrate = 110,
  943. .hw_value = 0x20,
  944. .hw_value_short = 0x20,
  945. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  946. { .bitrate = 60,
  947. .hw_value = 0x8,
  948. .hw_value_short = 0x8, },
  949. { .bitrate = 90,
  950. .hw_value = 0x10,
  951. .hw_value_short = 0x10, },
  952. { .bitrate = 120,
  953. .hw_value = 0x40,
  954. .hw_value_short = 0x40, },
  955. { .bitrate = 180,
  956. .hw_value = 0x80,
  957. .hw_value_short = 0x80, },
  958. { .bitrate = 240,
  959. .hw_value = 0x200,
  960. .hw_value_short = 0x200, },
  961. { .bitrate = 360,
  962. .hw_value = 0x400,
  963. .hw_value_short = 0x400, },
  964. { .bitrate = 480,
  965. .hw_value = 0x800,
  966. .hw_value_short = 0x800, },
  967. { .bitrate = 540,
  968. .hw_value = 0x1000,
  969. .hw_value_short = 0x1000, },
  970. };
  971. /* can't be const, mac80211 writes to this */
  972. static struct ieee80211_channel wl1271_channels[] = {
  973. { .hw_value = 1, .center_freq = 2412},
  974. { .hw_value = 2, .center_freq = 2417},
  975. { .hw_value = 3, .center_freq = 2422},
  976. { .hw_value = 4, .center_freq = 2427},
  977. { .hw_value = 5, .center_freq = 2432},
  978. { .hw_value = 6, .center_freq = 2437},
  979. { .hw_value = 7, .center_freq = 2442},
  980. { .hw_value = 8, .center_freq = 2447},
  981. { .hw_value = 9, .center_freq = 2452},
  982. { .hw_value = 10, .center_freq = 2457},
  983. { .hw_value = 11, .center_freq = 2462},
  984. { .hw_value = 12, .center_freq = 2467},
  985. { .hw_value = 13, .center_freq = 2472},
  986. };
  987. /* can't be const, mac80211 writes to this */
  988. static struct ieee80211_supported_band wl1271_band_2ghz = {
  989. .channels = wl1271_channels,
  990. .n_channels = ARRAY_SIZE(wl1271_channels),
  991. .bitrates = wl1271_rates,
  992. .n_bitrates = ARRAY_SIZE(wl1271_rates),
  993. };
  994. static const struct ieee80211_ops wl1271_ops = {
  995. .start = wl1271_op_start,
  996. .stop = wl1271_op_stop,
  997. .add_interface = wl1271_op_add_interface,
  998. .remove_interface = wl1271_op_remove_interface,
  999. .config = wl1271_op_config,
  1000. /* .config_interface = wl1271_op_config_interface, */
  1001. .prepare_multicast = wl1271_op_prepare_multicast,
  1002. .configure_filter = wl1271_op_configure_filter,
  1003. .tx = wl1271_op_tx,
  1004. .set_key = wl1271_op_set_key,
  1005. .hw_scan = wl1271_op_hw_scan,
  1006. .bss_info_changed = wl1271_op_bss_info_changed,
  1007. .set_rts_threshold = wl1271_op_set_rts_threshold,
  1008. };
  1009. static int wl1271_register_hw(struct wl1271 *wl)
  1010. {
  1011. int ret;
  1012. if (wl->mac80211_registered)
  1013. return 0;
  1014. SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
  1015. ret = ieee80211_register_hw(wl->hw);
  1016. if (ret < 0) {
  1017. wl1271_error("unable to register mac80211 hw: %d", ret);
  1018. return ret;
  1019. }
  1020. wl->mac80211_registered = true;
  1021. wl1271_notice("loaded");
  1022. return 0;
  1023. }
  1024. static int wl1271_init_ieee80211(struct wl1271 *wl)
  1025. {
  1026. /* The tx descriptor buffer and the TKIP space. */
  1027. wl->hw->extra_tx_headroom = WL1271_TKIP_IV_SPACE +
  1028. sizeof(struct wl1271_tx_hw_descr);
  1029. /* unit us */
  1030. /* FIXME: find a proper value */
  1031. wl->hw->channel_change_time = 10000;
  1032. wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
  1033. IEEE80211_HW_NOISE_DBM;
  1034. wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  1035. wl->hw->wiphy->max_scan_ssids = 1;
  1036. wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1271_band_2ghz;
  1037. SET_IEEE80211_DEV(wl->hw, &wl->spi->dev);
  1038. return 0;
  1039. }
  1040. static void wl1271_device_release(struct device *dev)
  1041. {
  1042. }
  1043. static struct platform_device wl1271_device = {
  1044. .name = "wl1271",
  1045. .id = -1,
  1046. /* device model insists to have a release function */
  1047. .dev = {
  1048. .release = wl1271_device_release,
  1049. },
  1050. };
  1051. #define WL1271_DEFAULT_CHANNEL 0
  1052. static int __devinit wl1271_probe(struct spi_device *spi)
  1053. {
  1054. struct wl12xx_platform_data *pdata;
  1055. struct ieee80211_hw *hw;
  1056. struct wl1271 *wl;
  1057. int ret, i;
  1058. static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
  1059. pdata = spi->dev.platform_data;
  1060. if (!pdata) {
  1061. wl1271_error("no platform data");
  1062. return -ENODEV;
  1063. }
  1064. hw = ieee80211_alloc_hw(sizeof(*wl), &wl1271_ops);
  1065. if (!hw) {
  1066. wl1271_error("could not alloc ieee80211_hw");
  1067. return -ENOMEM;
  1068. }
  1069. wl = hw->priv;
  1070. memset(wl, 0, sizeof(*wl));
  1071. wl->hw = hw;
  1072. dev_set_drvdata(&spi->dev, wl);
  1073. wl->spi = spi;
  1074. skb_queue_head_init(&wl->tx_queue);
  1075. INIT_WORK(&wl->filter_work, wl1271_filter_work);
  1076. INIT_DELAYED_WORK(&wl->elp_work, wl1271_elp_work);
  1077. wl->channel = WL1271_DEFAULT_CHANNEL;
  1078. wl->scanning = false;
  1079. wl->default_key = 0;
  1080. wl->listen_int = 1;
  1081. wl->rx_counter = 0;
  1082. wl->rx_config = WL1271_DEFAULT_RX_CONFIG;
  1083. wl->rx_filter = WL1271_DEFAULT_RX_FILTER;
  1084. wl->elp = false;
  1085. wl->psm = 0;
  1086. wl->psm_requested = false;
  1087. wl->tx_queue_stopped = false;
  1088. wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
  1089. wl->beacon_int = WL1271_DEFAULT_BEACON_INT;
  1090. wl->dtim_period = WL1271_DEFAULT_DTIM_PERIOD;
  1091. wl->basic_rate_set = WL1271_DEFAULT_BASIC_RATE_SET;
  1092. wl->band = IEEE80211_BAND_2GHZ;
  1093. for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
  1094. wl->tx_frames[i] = NULL;
  1095. spin_lock_init(&wl->wl_lock);
  1096. /*
  1097. * In case our MAC address is not correctly set,
  1098. * we use a random but Nokia MAC.
  1099. */
  1100. memcpy(wl->mac_addr, nokia_oui, 3);
  1101. get_random_bytes(wl->mac_addr + 3, 3);
  1102. wl->state = WL1271_STATE_OFF;
  1103. mutex_init(&wl->mutex);
  1104. wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
  1105. if (!wl->rx_descriptor) {
  1106. wl1271_error("could not allocate memory for rx descriptor");
  1107. ret = -ENOMEM;
  1108. goto out_free;
  1109. }
  1110. /* This is the only SPI value that we need to set here, the rest
  1111. * comes from the board-peripherals file */
  1112. spi->bits_per_word = 32;
  1113. ret = spi_setup(spi);
  1114. if (ret < 0) {
  1115. wl1271_error("spi_setup failed");
  1116. goto out_free;
  1117. }
  1118. wl->set_power = pdata->set_power;
  1119. if (!wl->set_power) {
  1120. wl1271_error("set power function missing in platform data");
  1121. ret = -ENODEV;
  1122. goto out_free;
  1123. }
  1124. wl->irq = spi->irq;
  1125. if (wl->irq < 0) {
  1126. wl1271_error("irq missing in platform data");
  1127. ret = -ENODEV;
  1128. goto out_free;
  1129. }
  1130. ret = request_irq(wl->irq, wl1271_irq, 0, DRIVER_NAME, wl);
  1131. if (ret < 0) {
  1132. wl1271_error("request_irq() failed: %d", ret);
  1133. goto out_free;
  1134. }
  1135. set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
  1136. disable_irq(wl->irq);
  1137. ret = platform_device_register(&wl1271_device);
  1138. if (ret) {
  1139. wl1271_error("couldn't register platform device");
  1140. goto out_irq;
  1141. }
  1142. dev_set_drvdata(&wl1271_device.dev, wl);
  1143. ret = wl1271_init_ieee80211(wl);
  1144. if (ret)
  1145. goto out_platform;
  1146. ret = wl1271_register_hw(wl);
  1147. if (ret)
  1148. goto out_platform;
  1149. wl1271_debugfs_init(wl);
  1150. wl1271_notice("initialized");
  1151. return 0;
  1152. out_platform:
  1153. platform_device_unregister(&wl1271_device);
  1154. out_irq:
  1155. free_irq(wl->irq, wl);
  1156. out_free:
  1157. kfree(wl->rx_descriptor);
  1158. wl->rx_descriptor = NULL;
  1159. ieee80211_free_hw(hw);
  1160. return ret;
  1161. }
  1162. static int __devexit wl1271_remove(struct spi_device *spi)
  1163. {
  1164. struct wl1271 *wl = dev_get_drvdata(&spi->dev);
  1165. ieee80211_unregister_hw(wl->hw);
  1166. wl1271_debugfs_exit(wl);
  1167. platform_device_unregister(&wl1271_device);
  1168. free_irq(wl->irq, wl);
  1169. kfree(wl->target_mem_map);
  1170. vfree(wl->fw);
  1171. wl->fw = NULL;
  1172. kfree(wl->nvs);
  1173. wl->nvs = NULL;
  1174. kfree(wl->rx_descriptor);
  1175. wl->rx_descriptor = NULL;
  1176. kfree(wl->fw_status);
  1177. kfree(wl->tx_res_if);
  1178. ieee80211_free_hw(wl->hw);
  1179. return 0;
  1180. }
  1181. static struct spi_driver wl1271_spi_driver = {
  1182. .driver = {
  1183. .name = "wl1271",
  1184. .bus = &spi_bus_type,
  1185. .owner = THIS_MODULE,
  1186. },
  1187. .probe = wl1271_probe,
  1188. .remove = __devexit_p(wl1271_remove),
  1189. };
  1190. static int __init wl1271_init(void)
  1191. {
  1192. int ret;
  1193. ret = spi_register_driver(&wl1271_spi_driver);
  1194. if (ret < 0) {
  1195. wl1271_error("failed to register spi driver: %d", ret);
  1196. goto out;
  1197. }
  1198. out:
  1199. return ret;
  1200. }
  1201. static void __exit wl1271_exit(void)
  1202. {
  1203. spi_unregister_driver(&wl1271_spi_driver);
  1204. wl1271_notice("unloaded");
  1205. }
  1206. module_init(wl1271_init);
  1207. module_exit(wl1271_exit);
  1208. MODULE_LICENSE("GPL");
  1209. MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
  1210. MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");