wl1271_main.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544
  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_reg_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_reg_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_reg_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_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_reg_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_cmd_join(wl);
  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. 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. IEEE80211_HW_BEACON_FILTER;
  1035. wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  1036. wl->hw->wiphy->max_scan_ssids = 1;
  1037. wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1271_band_2ghz;
  1038. SET_IEEE80211_DEV(wl->hw, &wl->spi->dev);
  1039. return 0;
  1040. }
  1041. static void wl1271_device_release(struct device *dev)
  1042. {
  1043. }
  1044. static struct platform_device wl1271_device = {
  1045. .name = "wl1271",
  1046. .id = -1,
  1047. /* device model insists to have a release function */
  1048. .dev = {
  1049. .release = wl1271_device_release,
  1050. },
  1051. };
  1052. #define WL1271_DEFAULT_CHANNEL 0
  1053. static int __devinit wl1271_probe(struct spi_device *spi)
  1054. {
  1055. struct wl12xx_platform_data *pdata;
  1056. struct ieee80211_hw *hw;
  1057. struct wl1271 *wl;
  1058. int ret, i;
  1059. static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
  1060. pdata = spi->dev.platform_data;
  1061. if (!pdata) {
  1062. wl1271_error("no platform data");
  1063. return -ENODEV;
  1064. }
  1065. hw = ieee80211_alloc_hw(sizeof(*wl), &wl1271_ops);
  1066. if (!hw) {
  1067. wl1271_error("could not alloc ieee80211_hw");
  1068. return -ENOMEM;
  1069. }
  1070. wl = hw->priv;
  1071. memset(wl, 0, sizeof(*wl));
  1072. wl->hw = hw;
  1073. dev_set_drvdata(&spi->dev, wl);
  1074. wl->spi = spi;
  1075. skb_queue_head_init(&wl->tx_queue);
  1076. INIT_WORK(&wl->filter_work, wl1271_filter_work);
  1077. INIT_DELAYED_WORK(&wl->elp_work, wl1271_elp_work);
  1078. wl->channel = WL1271_DEFAULT_CHANNEL;
  1079. wl->scanning = false;
  1080. wl->default_key = 0;
  1081. wl->listen_int = 1;
  1082. wl->rx_counter = 0;
  1083. wl->rx_config = WL1271_DEFAULT_RX_CONFIG;
  1084. wl->rx_filter = WL1271_DEFAULT_RX_FILTER;
  1085. wl->elp = false;
  1086. wl->psm = 0;
  1087. wl->psm_requested = false;
  1088. wl->tx_queue_stopped = false;
  1089. wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
  1090. wl->beacon_int = WL1271_DEFAULT_BEACON_INT;
  1091. wl->dtim_period = WL1271_DEFAULT_DTIM_PERIOD;
  1092. wl->basic_rate_set = WL1271_DEFAULT_BASIC_RATE_SET;
  1093. wl->band = IEEE80211_BAND_2GHZ;
  1094. wl->vif = NULL;
  1095. wl->joined = false;
  1096. for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
  1097. wl->tx_frames[i] = NULL;
  1098. spin_lock_init(&wl->wl_lock);
  1099. /*
  1100. * In case our MAC address is not correctly set,
  1101. * we use a random but Nokia MAC.
  1102. */
  1103. memcpy(wl->mac_addr, nokia_oui, 3);
  1104. get_random_bytes(wl->mac_addr + 3, 3);
  1105. wl->state = WL1271_STATE_OFF;
  1106. mutex_init(&wl->mutex);
  1107. wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
  1108. if (!wl->rx_descriptor) {
  1109. wl1271_error("could not allocate memory for rx descriptor");
  1110. ret = -ENOMEM;
  1111. goto out_free;
  1112. }
  1113. /* This is the only SPI value that we need to set here, the rest
  1114. * comes from the board-peripherals file */
  1115. spi->bits_per_word = 32;
  1116. ret = spi_setup(spi);
  1117. if (ret < 0) {
  1118. wl1271_error("spi_setup failed");
  1119. goto out_free;
  1120. }
  1121. wl->set_power = pdata->set_power;
  1122. if (!wl->set_power) {
  1123. wl1271_error("set power function missing in platform data");
  1124. ret = -ENODEV;
  1125. goto out_free;
  1126. }
  1127. wl->irq = spi->irq;
  1128. if (wl->irq < 0) {
  1129. wl1271_error("irq missing in platform data");
  1130. ret = -ENODEV;
  1131. goto out_free;
  1132. }
  1133. ret = request_irq(wl->irq, wl1271_irq, 0, DRIVER_NAME, wl);
  1134. if (ret < 0) {
  1135. wl1271_error("request_irq() failed: %d", ret);
  1136. goto out_free;
  1137. }
  1138. set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
  1139. disable_irq(wl->irq);
  1140. ret = platform_device_register(&wl1271_device);
  1141. if (ret) {
  1142. wl1271_error("couldn't register platform device");
  1143. goto out_irq;
  1144. }
  1145. dev_set_drvdata(&wl1271_device.dev, wl);
  1146. ret = wl1271_init_ieee80211(wl);
  1147. if (ret)
  1148. goto out_platform;
  1149. ret = wl1271_register_hw(wl);
  1150. if (ret)
  1151. goto out_platform;
  1152. wl1271_debugfs_init(wl);
  1153. wl1271_notice("initialized");
  1154. return 0;
  1155. out_platform:
  1156. platform_device_unregister(&wl1271_device);
  1157. out_irq:
  1158. free_irq(wl->irq, wl);
  1159. out_free:
  1160. kfree(wl->rx_descriptor);
  1161. wl->rx_descriptor = NULL;
  1162. ieee80211_free_hw(hw);
  1163. return ret;
  1164. }
  1165. static int __devexit wl1271_remove(struct spi_device *spi)
  1166. {
  1167. struct wl1271 *wl = dev_get_drvdata(&spi->dev);
  1168. ieee80211_unregister_hw(wl->hw);
  1169. wl1271_debugfs_exit(wl);
  1170. platform_device_unregister(&wl1271_device);
  1171. free_irq(wl->irq, wl);
  1172. kfree(wl->target_mem_map);
  1173. vfree(wl->fw);
  1174. wl->fw = NULL;
  1175. kfree(wl->nvs);
  1176. wl->nvs = NULL;
  1177. kfree(wl->rx_descriptor);
  1178. wl->rx_descriptor = NULL;
  1179. kfree(wl->fw_status);
  1180. kfree(wl->tx_res_if);
  1181. ieee80211_free_hw(wl->hw);
  1182. return 0;
  1183. }
  1184. static struct spi_driver wl1271_spi_driver = {
  1185. .driver = {
  1186. .name = "wl1271",
  1187. .bus = &spi_bus_type,
  1188. .owner = THIS_MODULE,
  1189. },
  1190. .probe = wl1271_probe,
  1191. .remove = __devexit_p(wl1271_remove),
  1192. };
  1193. static int __init wl1271_init(void)
  1194. {
  1195. int ret;
  1196. ret = spi_register_driver(&wl1271_spi_driver);
  1197. if (ret < 0) {
  1198. wl1271_error("failed to register spi driver: %d", ret);
  1199. goto out;
  1200. }
  1201. out:
  1202. return ret;
  1203. }
  1204. static void __exit wl1271_exit(void)
  1205. {
  1206. spi_unregister_driver(&wl1271_spi_driver);
  1207. wl1271_notice("unloaded");
  1208. }
  1209. module_init(wl1271_init);
  1210. module_exit(wl1271_exit);
  1211. MODULE_LICENSE("GPL");
  1212. MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
  1213. MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");