wl1271_main.c 33 KB

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