wl1271_main.c 34 KB

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