wl1251_main.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  1. /*
  2. * This file is part of wl1251
  3. *
  4. * Copyright (C) 2008-2009 Nokia Corporation
  5. *
  6. * Contact: Kalle Valo <kalle.valo@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/firmware.h>
  26. #include <linux/delay.h>
  27. #include <linux/irq.h>
  28. #include <linux/spi/spi.h>
  29. #include <linux/crc32.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/spi/wl12xx.h>
  32. #include "wl1251.h"
  33. #include "wl12xx_80211.h"
  34. #include "reg.h"
  35. #include "wl1251_ops.h"
  36. #include "wl1251_spi.h"
  37. #include "wl1251_event.h"
  38. #include "wl1251_tx.h"
  39. #include "wl1251_rx.h"
  40. #include "wl1251_ps.h"
  41. #include "wl1251_init.h"
  42. #include "wl1251_debugfs.h"
  43. static void wl1251_disable_interrupts(struct wl1251 *wl)
  44. {
  45. disable_irq(wl->irq);
  46. }
  47. static void wl1251_power_off(struct wl1251 *wl)
  48. {
  49. wl->set_power(false);
  50. }
  51. static void wl1251_power_on(struct wl1251 *wl)
  52. {
  53. wl->set_power(true);
  54. }
  55. static irqreturn_t wl1251_irq(int irq, void *cookie)
  56. {
  57. struct wl1251 *wl;
  58. wl1251_debug(DEBUG_IRQ, "IRQ");
  59. wl = cookie;
  60. schedule_work(&wl->irq_work);
  61. return IRQ_HANDLED;
  62. }
  63. static int wl1251_fetch_firmware(struct wl1251 *wl)
  64. {
  65. const struct firmware *fw;
  66. int ret;
  67. ret = request_firmware(&fw, wl->chip.fw_filename, &wl->spi->dev);
  68. if (ret < 0) {
  69. wl1251_error("could not get firmware: %d", ret);
  70. return ret;
  71. }
  72. if (fw->size % 4) {
  73. wl1251_error("firmware size is not multiple of 32 bits: %zu",
  74. fw->size);
  75. ret = -EILSEQ;
  76. goto out;
  77. }
  78. wl->fw_len = fw->size;
  79. wl->fw = kmalloc(wl->fw_len, GFP_KERNEL);
  80. if (!wl->fw) {
  81. wl1251_error("could not allocate memory for the firmware");
  82. ret = -ENOMEM;
  83. goto out;
  84. }
  85. memcpy(wl->fw, fw->data, wl->fw_len);
  86. ret = 0;
  87. out:
  88. release_firmware(fw);
  89. return ret;
  90. }
  91. static int wl1251_fetch_nvs(struct wl1251 *wl)
  92. {
  93. const struct firmware *fw;
  94. int ret;
  95. ret = request_firmware(&fw, wl->chip.nvs_filename, &wl->spi->dev);
  96. if (ret < 0) {
  97. wl1251_error("could not get nvs file: %d", ret);
  98. return ret;
  99. }
  100. if (fw->size % 4) {
  101. wl1251_error("nvs size is not multiple of 32 bits: %zu",
  102. fw->size);
  103. ret = -EILSEQ;
  104. goto out;
  105. }
  106. wl->nvs_len = fw->size;
  107. wl->nvs = kmalloc(wl->nvs_len, GFP_KERNEL);
  108. if (!wl->nvs) {
  109. wl1251_error("could not allocate memory for the nvs file");
  110. ret = -ENOMEM;
  111. goto out;
  112. }
  113. memcpy(wl->nvs, fw->data, wl->nvs_len);
  114. ret = 0;
  115. out:
  116. release_firmware(fw);
  117. return ret;
  118. }
  119. static void wl1251_fw_wakeup(struct wl1251 *wl)
  120. {
  121. u32 elp_reg;
  122. elp_reg = ELPCTRL_WAKE_UP;
  123. wl1251_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
  124. elp_reg = wl1251_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
  125. if (!(elp_reg & ELPCTRL_WLAN_READY))
  126. wl1251_warning("WLAN not ready");
  127. }
  128. static int wl1251_chip_wakeup(struct wl1251 *wl)
  129. {
  130. int ret = 0;
  131. wl1251_power_on(wl);
  132. msleep(wl->chip.power_on_sleep);
  133. wl1251_spi_reset(wl);
  134. wl1251_spi_init(wl);
  135. /* We don't need a real memory partition here, because we only want
  136. * to use the registers at this point. */
  137. wl1251_set_partition(wl,
  138. 0x00000000,
  139. 0x00000000,
  140. REGISTERS_BASE,
  141. REGISTERS_DOWN_SIZE);
  142. /* ELP module wake up */
  143. wl1251_fw_wakeup(wl);
  144. /* whal_FwCtrl_BootSm() */
  145. /* 0. read chip id from CHIP_ID */
  146. wl->chip.id = wl1251_reg_read32(wl, CHIP_ID_B);
  147. /* 1. check if chip id is valid */
  148. switch (wl->chip.id) {
  149. case CHIP_ID_1251_PG12:
  150. wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG12)",
  151. wl->chip.id);
  152. wl1251_setup(wl);
  153. break;
  154. case CHIP_ID_1251_PG10:
  155. case CHIP_ID_1251_PG11:
  156. default:
  157. wl1251_error("unsupported chip id: 0x%x", wl->chip.id);
  158. ret = -ENODEV;
  159. goto out;
  160. }
  161. if (wl->fw == NULL) {
  162. ret = wl1251_fetch_firmware(wl);
  163. if (ret < 0)
  164. goto out;
  165. }
  166. /* No NVS from netlink, try to get it from the filesystem */
  167. if (wl->nvs == NULL) {
  168. ret = wl1251_fetch_nvs(wl);
  169. if (ret < 0)
  170. goto out;
  171. }
  172. out:
  173. return ret;
  174. }
  175. static void wl1251_filter_work(struct work_struct *work)
  176. {
  177. struct wl1251 *wl =
  178. container_of(work, struct wl1251, filter_work);
  179. int ret;
  180. mutex_lock(&wl->mutex);
  181. if (wl->state == WL1251_STATE_OFF)
  182. goto out;
  183. ret = wl1251_ps_elp_wakeup(wl);
  184. if (ret < 0)
  185. goto out;
  186. /* FIXME: replace the magic numbers with proper definitions */
  187. ret = wl->chip.op_cmd_join(wl, wl->bss_type, 1, 100, 0);
  188. if (ret < 0)
  189. goto out_sleep;
  190. out_sleep:
  191. wl1251_ps_elp_sleep(wl);
  192. out:
  193. mutex_unlock(&wl->mutex);
  194. }
  195. int wl1251_plt_start(struct wl1251 *wl)
  196. {
  197. int ret;
  198. mutex_lock(&wl->mutex);
  199. wl1251_notice("power up");
  200. if (wl->state != WL1251_STATE_OFF) {
  201. wl1251_error("cannot go into PLT state because not "
  202. "in off state: %d", wl->state);
  203. return -EBUSY;
  204. }
  205. wl->state = WL1251_STATE_PLT;
  206. ret = wl1251_chip_wakeup(wl);
  207. if (ret < 0)
  208. return ret;
  209. ret = wl->chip.op_boot(wl);
  210. if (ret < 0)
  211. return ret;
  212. wl1251_notice("firmware booted in PLT mode (%s)", wl->chip.fw_ver);
  213. ret = wl->chip.op_plt_init(wl);
  214. if (ret < 0)
  215. return ret;
  216. return 0;
  217. }
  218. int wl1251_plt_stop(struct wl1251 *wl)
  219. {
  220. mutex_lock(&wl->mutex);
  221. wl1251_notice("power down");
  222. if (wl->state != WL1251_STATE_PLT) {
  223. wl1251_error("cannot power down because not in PLT "
  224. "state: %d", wl->state);
  225. return -EBUSY;
  226. }
  227. wl1251_disable_interrupts(wl);
  228. wl1251_power_off(wl);
  229. wl->state = WL1251_STATE_OFF;
  230. return 0;
  231. }
  232. static int wl1251_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  233. {
  234. struct wl1251 *wl = hw->priv;
  235. skb_queue_tail(&wl->tx_queue, skb);
  236. /*
  237. * The chip specific setup must run before the first TX packet -
  238. * before that, the tx_work will not be initialized!
  239. */
  240. schedule_work(&wl->tx_work);
  241. /*
  242. * The workqueue is slow to process the tx_queue and we need stop
  243. * the queue here, otherwise the queue will get too long.
  244. */
  245. if (skb_queue_len(&wl->tx_queue) >= WL1251_TX_QUEUE_MAX_LENGTH) {
  246. ieee80211_stop_queues(wl->hw);
  247. /*
  248. * FIXME: this is racy, the variable is not properly
  249. * protected. Maybe fix this by removing the stupid
  250. * variable altogether and checking the real queue state?
  251. */
  252. wl->tx_queue_stopped = true;
  253. }
  254. return NETDEV_TX_OK;
  255. }
  256. static int wl1251_op_start(struct ieee80211_hw *hw)
  257. {
  258. struct wl1251 *wl = hw->priv;
  259. int ret = 0;
  260. wl1251_debug(DEBUG_MAC80211, "mac80211 start");
  261. mutex_lock(&wl->mutex);
  262. if (wl->state != WL1251_STATE_OFF) {
  263. wl1251_error("cannot start because not in off state: %d",
  264. wl->state);
  265. ret = -EBUSY;
  266. goto out;
  267. }
  268. ret = wl1251_chip_wakeup(wl);
  269. if (ret < 0)
  270. return ret;
  271. ret = wl->chip.op_boot(wl);
  272. if (ret < 0)
  273. goto out;
  274. ret = wl->chip.op_hw_init(wl);
  275. if (ret < 0)
  276. goto out;
  277. ret = wl1251_acx_station_id(wl);
  278. if (ret < 0)
  279. goto out;
  280. wl->state = WL1251_STATE_ON;
  281. wl1251_info("firmware booted (%s)", wl->chip.fw_ver);
  282. out:
  283. if (ret < 0)
  284. wl1251_power_off(wl);
  285. mutex_unlock(&wl->mutex);
  286. return ret;
  287. }
  288. static void wl1251_op_stop(struct ieee80211_hw *hw)
  289. {
  290. struct wl1251 *wl = hw->priv;
  291. wl1251_info("down");
  292. wl1251_debug(DEBUG_MAC80211, "mac80211 stop");
  293. mutex_lock(&wl->mutex);
  294. WARN_ON(wl->state != WL1251_STATE_ON);
  295. if (wl->scanning) {
  296. mutex_unlock(&wl->mutex);
  297. ieee80211_scan_completed(wl->hw, true);
  298. mutex_lock(&wl->mutex);
  299. wl->scanning = false;
  300. }
  301. wl->state = WL1251_STATE_OFF;
  302. wl1251_disable_interrupts(wl);
  303. mutex_unlock(&wl->mutex);
  304. cancel_work_sync(&wl->irq_work);
  305. cancel_work_sync(&wl->tx_work);
  306. cancel_work_sync(&wl->filter_work);
  307. mutex_lock(&wl->mutex);
  308. /* let's notify MAC80211 about the remaining pending TX frames */
  309. wl->chip.op_tx_flush(wl);
  310. wl1251_power_off(wl);
  311. memset(wl->bssid, 0, ETH_ALEN);
  312. wl->listen_int = 1;
  313. wl->bss_type = MAX_BSS_TYPE;
  314. wl->data_in_count = 0;
  315. wl->rx_counter = 0;
  316. wl->rx_handled = 0;
  317. wl->rx_current_buffer = 0;
  318. wl->rx_last_id = 0;
  319. wl->next_tx_complete = 0;
  320. wl->elp = false;
  321. wl->psm = 0;
  322. wl->tx_queue_stopped = false;
  323. wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
  324. wl1251_debugfs_reset(wl);
  325. mutex_unlock(&wl->mutex);
  326. }
  327. static int wl1251_op_add_interface(struct ieee80211_hw *hw,
  328. struct ieee80211_if_init_conf *conf)
  329. {
  330. struct wl1251 *wl = hw->priv;
  331. DECLARE_MAC_BUF(mac);
  332. int ret = 0;
  333. wl1251_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %s",
  334. conf->type, print_mac(mac, conf->mac_addr));
  335. mutex_lock(&wl->mutex);
  336. switch (conf->type) {
  337. case NL80211_IFTYPE_STATION:
  338. wl->bss_type = BSS_TYPE_STA_BSS;
  339. break;
  340. case NL80211_IFTYPE_ADHOC:
  341. wl->bss_type = BSS_TYPE_IBSS;
  342. break;
  343. default:
  344. ret = -EOPNOTSUPP;
  345. goto out;
  346. }
  347. if (memcmp(wl->mac_addr, conf->mac_addr, ETH_ALEN)) {
  348. memcpy(wl->mac_addr, conf->mac_addr, ETH_ALEN);
  349. SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
  350. ret = wl1251_acx_station_id(wl);
  351. if (ret < 0)
  352. goto out;
  353. }
  354. out:
  355. mutex_unlock(&wl->mutex);
  356. return ret;
  357. }
  358. static void wl1251_op_remove_interface(struct ieee80211_hw *hw,
  359. struct ieee80211_if_init_conf *conf)
  360. {
  361. wl1251_debug(DEBUG_MAC80211, "mac80211 remove interface");
  362. }
  363. static int wl1251_build_null_data(struct wl1251 *wl)
  364. {
  365. struct wl12xx_null_data_template template;
  366. if (!is_zero_ether_addr(wl->bssid)) {
  367. memcpy(template.header.da, wl->bssid, ETH_ALEN);
  368. memcpy(template.header.bssid, wl->bssid, ETH_ALEN);
  369. } else {
  370. memset(template.header.da, 0xff, ETH_ALEN);
  371. memset(template.header.bssid, 0xff, ETH_ALEN);
  372. }
  373. memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
  374. template.header.frame_ctl = cpu_to_le16(IEEE80211_FTYPE_DATA |
  375. IEEE80211_STYPE_NULLFUNC);
  376. return wl1251_cmd_template_set(wl, CMD_NULL_DATA, &template,
  377. sizeof(template));
  378. }
  379. static int wl1251_build_ps_poll(struct wl1251 *wl, u16 aid)
  380. {
  381. struct wl12xx_ps_poll_template template;
  382. memcpy(template.bssid, wl->bssid, ETH_ALEN);
  383. memcpy(template.ta, wl->mac_addr, ETH_ALEN);
  384. template.aid = aid;
  385. template.fc = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
  386. return wl1251_cmd_template_set(wl, CMD_PS_POLL, &template,
  387. sizeof(template));
  388. }
  389. static int wl1251_op_config(struct ieee80211_hw *hw, u32 changed)
  390. {
  391. struct wl1251 *wl = hw->priv;
  392. struct ieee80211_conf *conf = &hw->conf;
  393. int channel, ret = 0;
  394. channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
  395. wl1251_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
  396. channel,
  397. conf->flags & IEEE80211_CONF_PS ? "on" : "off",
  398. conf->power_level);
  399. mutex_lock(&wl->mutex);
  400. ret = wl1251_ps_elp_wakeup(wl);
  401. if (ret < 0)
  402. goto out;
  403. if (channel != wl->channel) {
  404. /* FIXME: use beacon interval provided by mac80211 */
  405. ret = wl->chip.op_cmd_join(wl, wl->bss_type, 1, 100, 0);
  406. if (ret < 0)
  407. goto out_sleep;
  408. wl->channel = channel;
  409. }
  410. ret = wl1251_build_null_data(wl);
  411. if (ret < 0)
  412. goto out_sleep;
  413. if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
  414. wl1251_debug(DEBUG_PSM, "psm enabled");
  415. wl->psm_requested = true;
  416. /*
  417. * We enter PSM only if we're already associated.
  418. * If we're not, we'll enter it when joining an SSID,
  419. * through the bss_info_changed() hook.
  420. */
  421. ret = wl1251_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
  422. } else if (!(conf->flags & IEEE80211_CONF_PS) &&
  423. wl->psm_requested) {
  424. wl1251_debug(DEBUG_PSM, "psm disabled");
  425. wl->psm_requested = false;
  426. if (wl->psm)
  427. ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
  428. }
  429. if (conf->power_level != wl->power_level) {
  430. ret = wl1251_acx_tx_power(wl, conf->power_level);
  431. if (ret < 0)
  432. goto out;
  433. wl->power_level = conf->power_level;
  434. }
  435. out_sleep:
  436. wl1251_ps_elp_sleep(wl);
  437. out:
  438. mutex_unlock(&wl->mutex);
  439. return ret;
  440. }
  441. #define WL1251_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
  442. FIF_ALLMULTI | \
  443. FIF_FCSFAIL | \
  444. FIF_BCN_PRBRESP_PROMISC | \
  445. FIF_CONTROL | \
  446. FIF_OTHER_BSS)
  447. static void wl1251_op_configure_filter(struct ieee80211_hw *hw,
  448. unsigned int changed,
  449. unsigned int *total,
  450. int mc_count,
  451. struct dev_addr_list *mc_list)
  452. {
  453. struct wl1251 *wl = hw->priv;
  454. wl1251_debug(DEBUG_MAC80211, "mac80211 configure filter");
  455. *total &= WL1251_SUPPORTED_FILTERS;
  456. changed &= WL1251_SUPPORTED_FILTERS;
  457. if (changed == 0)
  458. /* no filters which we support changed */
  459. return;
  460. /* FIXME: wl->rx_config and wl->rx_filter are not protected */
  461. wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
  462. wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
  463. if (*total & FIF_PROMISC_IN_BSS) {
  464. wl->rx_config |= CFG_BSSID_FILTER_EN;
  465. wl->rx_config |= CFG_RX_ALL_GOOD;
  466. }
  467. if (*total & FIF_ALLMULTI)
  468. /*
  469. * CFG_MC_FILTER_EN in rx_config needs to be 0 to receive
  470. * all multicast frames
  471. */
  472. wl->rx_config &= ~CFG_MC_FILTER_EN;
  473. if (*total & FIF_FCSFAIL)
  474. wl->rx_filter |= CFG_RX_FCS_ERROR;
  475. if (*total & FIF_BCN_PRBRESP_PROMISC) {
  476. wl->rx_config &= ~CFG_BSSID_FILTER_EN;
  477. wl->rx_config &= ~CFG_SSID_FILTER_EN;
  478. }
  479. if (*total & FIF_CONTROL)
  480. wl->rx_filter |= CFG_RX_CTL_EN;
  481. if (*total & FIF_OTHER_BSS)
  482. wl->rx_filter &= ~CFG_BSSID_FILTER_EN;
  483. /*
  484. * FIXME: workqueues need to be properly cancelled on stop(), for
  485. * now let's just disable changing the filter settings. They will
  486. * be updated any on config().
  487. */
  488. /* schedule_work(&wl->filter_work); */
  489. }
  490. /* HW encryption */
  491. static int wl1251_set_key_type(struct wl1251 *wl,
  492. struct wl1251_cmd_set_keys *key,
  493. enum set_key_cmd cmd,
  494. struct ieee80211_key_conf *mac80211_key,
  495. const u8 *addr)
  496. {
  497. switch (mac80211_key->alg) {
  498. case ALG_WEP:
  499. if (is_broadcast_ether_addr(addr))
  500. key->key_type = KEY_WEP_DEFAULT;
  501. else
  502. key->key_type = KEY_WEP_ADDR;
  503. mac80211_key->hw_key_idx = mac80211_key->keyidx;
  504. break;
  505. case ALG_TKIP:
  506. if (is_broadcast_ether_addr(addr))
  507. key->key_type = KEY_TKIP_MIC_GROUP;
  508. else
  509. key->key_type = KEY_TKIP_MIC_PAIRWISE;
  510. mac80211_key->hw_key_idx = mac80211_key->keyidx;
  511. break;
  512. case ALG_CCMP:
  513. if (is_broadcast_ether_addr(addr))
  514. key->key_type = KEY_AES_GROUP;
  515. else
  516. key->key_type = KEY_AES_PAIRWISE;
  517. mac80211_key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  518. break;
  519. default:
  520. wl1251_error("Unknown key algo 0x%x", mac80211_key->alg);
  521. return -EOPNOTSUPP;
  522. }
  523. return 0;
  524. }
  525. static int wl1251_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  526. struct ieee80211_vif *vif,
  527. struct ieee80211_sta *sta,
  528. struct ieee80211_key_conf *key)
  529. {
  530. struct wl1251 *wl = hw->priv;
  531. struct wl1251_cmd_set_keys *wl_cmd;
  532. const u8 *addr;
  533. int ret;
  534. static const u8 bcast_addr[ETH_ALEN] =
  535. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  536. wl1251_debug(DEBUG_MAC80211, "mac80211 set key");
  537. wl_cmd = kzalloc(sizeof(*wl_cmd), GFP_KERNEL);
  538. if (!wl_cmd) {
  539. ret = -ENOMEM;
  540. goto out;
  541. }
  542. addr = sta ? sta->addr : bcast_addr;
  543. wl1251_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
  544. wl1251_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
  545. wl1251_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
  546. key->alg, key->keyidx, key->keylen, key->flags);
  547. wl1251_dump(DEBUG_CRYPT, "KEY: ", key->key, key->keylen);
  548. if (is_zero_ether_addr(addr)) {
  549. /* We dont support TX only encryption */
  550. ret = -EOPNOTSUPP;
  551. goto out;
  552. }
  553. mutex_lock(&wl->mutex);
  554. ret = wl1251_ps_elp_wakeup(wl);
  555. if (ret < 0)
  556. goto out_unlock;
  557. switch (cmd) {
  558. case SET_KEY:
  559. wl_cmd->key_action = KEY_ADD_OR_REPLACE;
  560. break;
  561. case DISABLE_KEY:
  562. wl_cmd->key_action = KEY_REMOVE;
  563. break;
  564. default:
  565. wl1251_error("Unsupported key cmd 0x%x", cmd);
  566. break;
  567. }
  568. ret = wl1251_set_key_type(wl, wl_cmd, cmd, key, addr);
  569. if (ret < 0) {
  570. wl1251_error("Set KEY type failed");
  571. goto out_sleep;
  572. }
  573. if (wl_cmd->key_type != KEY_WEP_DEFAULT)
  574. memcpy(wl_cmd->addr, addr, ETH_ALEN);
  575. if ((wl_cmd->key_type == KEY_TKIP_MIC_GROUP) ||
  576. (wl_cmd->key_type == KEY_TKIP_MIC_PAIRWISE)) {
  577. /*
  578. * We get the key in the following form:
  579. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  580. * but the target is expecting:
  581. * TKIP - RX MIC - TX MIC
  582. */
  583. memcpy(wl_cmd->key, key->key, 16);
  584. memcpy(wl_cmd->key + 16, key->key + 24, 8);
  585. memcpy(wl_cmd->key + 24, key->key + 16, 8);
  586. } else {
  587. memcpy(wl_cmd->key, key->key, key->keylen);
  588. }
  589. wl_cmd->key_size = key->keylen;
  590. wl_cmd->id = key->keyidx;
  591. wl_cmd->ssid_profile = 0;
  592. wl1251_dump(DEBUG_CRYPT, "TARGET KEY: ", wl_cmd, sizeof(*wl_cmd));
  593. ret = wl1251_cmd_send(wl, CMD_SET_KEYS, wl_cmd, sizeof(*wl_cmd));
  594. if (ret < 0) {
  595. wl1251_warning("could not set keys");
  596. goto out_sleep;
  597. }
  598. out_sleep:
  599. wl1251_ps_elp_sleep(wl);
  600. out_unlock:
  601. mutex_unlock(&wl->mutex);
  602. out:
  603. kfree(wl_cmd);
  604. return ret;
  605. }
  606. static int wl1251_build_basic_rates(char *rates)
  607. {
  608. u8 index = 0;
  609. rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
  610. rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
  611. rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
  612. rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
  613. return index;
  614. }
  615. static int wl1251_build_extended_rates(char *rates)
  616. {
  617. u8 index = 0;
  618. rates[index++] = IEEE80211_OFDM_RATE_6MB;
  619. rates[index++] = IEEE80211_OFDM_RATE_9MB;
  620. rates[index++] = IEEE80211_OFDM_RATE_12MB;
  621. rates[index++] = IEEE80211_OFDM_RATE_18MB;
  622. rates[index++] = IEEE80211_OFDM_RATE_24MB;
  623. rates[index++] = IEEE80211_OFDM_RATE_36MB;
  624. rates[index++] = IEEE80211_OFDM_RATE_48MB;
  625. rates[index++] = IEEE80211_OFDM_RATE_54MB;
  626. return index;
  627. }
  628. static int wl1251_build_probe_req(struct wl1251 *wl, u8 *ssid, size_t ssid_len)
  629. {
  630. struct wl12xx_probe_req_template template;
  631. struct wl12xx_ie_rates *rates;
  632. char *ptr;
  633. u16 size;
  634. ptr = (char *)&template;
  635. size = sizeof(struct ieee80211_header);
  636. memset(template.header.da, 0xff, ETH_ALEN);
  637. memset(template.header.bssid, 0xff, ETH_ALEN);
  638. memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
  639. template.header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  640. /* IEs */
  641. /* SSID */
  642. template.ssid.header.id = WLAN_EID_SSID;
  643. template.ssid.header.len = ssid_len;
  644. if (ssid_len && ssid)
  645. memcpy(template.ssid.ssid, ssid, ssid_len);
  646. size += sizeof(struct wl12xx_ie_header) + ssid_len;
  647. ptr += size;
  648. /* Basic Rates */
  649. rates = (struct wl12xx_ie_rates *)ptr;
  650. rates->header.id = WLAN_EID_SUPP_RATES;
  651. rates->header.len = wl1251_build_basic_rates(rates->rates);
  652. size += sizeof(struct wl12xx_ie_header) + rates->header.len;
  653. ptr += sizeof(struct wl12xx_ie_header) + rates->header.len;
  654. /* Extended rates */
  655. rates = (struct wl12xx_ie_rates *)ptr;
  656. rates->header.id = WLAN_EID_EXT_SUPP_RATES;
  657. rates->header.len = wl1251_build_extended_rates(rates->rates);
  658. size += sizeof(struct wl12xx_ie_header) + rates->header.len;
  659. wl1251_dump(DEBUG_SCAN, "PROBE REQ: ", &template, size);
  660. return wl1251_cmd_template_set(wl, CMD_PROBE_REQ, &template,
  661. size);
  662. }
  663. static int wl1251_hw_scan(struct wl1251 *wl, u8 *ssid, size_t len,
  664. u8 active_scan, u8 high_prio, u8 num_channels,
  665. u8 probe_requests)
  666. {
  667. struct wl1251_cmd_trigger_scan_to *trigger = NULL;
  668. struct cmd_scan *params = NULL;
  669. int i, ret;
  670. u16 scan_options = 0;
  671. if (wl->scanning)
  672. return -EINVAL;
  673. params = kzalloc(sizeof(*params), GFP_KERNEL);
  674. if (!params)
  675. return -ENOMEM;
  676. params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
  677. params->params.rx_filter_options =
  678. cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
  679. /* High priority scan */
  680. if (!active_scan)
  681. scan_options |= SCAN_PASSIVE;
  682. if (high_prio)
  683. scan_options |= SCAN_PRIORITY_HIGH;
  684. params->params.scan_options = scan_options;
  685. params->params.num_channels = num_channels;
  686. params->params.num_probe_requests = probe_requests;
  687. params->params.tx_rate = cpu_to_le16(1 << 1); /* 2 Mbps */
  688. params->params.tid_trigger = 0;
  689. for (i = 0; i < num_channels; i++) {
  690. params->channels[i].min_duration = cpu_to_le32(30000);
  691. params->channels[i].max_duration = cpu_to_le32(60000);
  692. memset(&params->channels[i].bssid_lsb, 0xff, 4);
  693. memset(&params->channels[i].bssid_msb, 0xff, 2);
  694. params->channels[i].early_termination = 0;
  695. params->channels[i].tx_power_att = 0;
  696. params->channels[i].channel = i + 1;
  697. memset(params->channels[i].pad, 0, 3);
  698. }
  699. for (i = num_channels; i < SCAN_MAX_NUM_OF_CHANNELS; i++)
  700. memset(&params->channels[i], 0,
  701. sizeof(struct basic_scan_channel_parameters));
  702. if (len && ssid) {
  703. params->params.ssid_len = len;
  704. memcpy(params->params.ssid, ssid, len);
  705. } else {
  706. params->params.ssid_len = 0;
  707. memset(params->params.ssid, 0, 32);
  708. }
  709. ret = wl1251_build_probe_req(wl, ssid, len);
  710. if (ret < 0) {
  711. wl1251_error("PROBE request template failed");
  712. goto out;
  713. }
  714. trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
  715. if (!trigger)
  716. goto out;
  717. trigger->timeout = 0;
  718. ret = wl1251_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
  719. sizeof(*trigger));
  720. if (ret < 0) {
  721. wl1251_error("trigger scan to failed for hw scan");
  722. goto out;
  723. }
  724. wl1251_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
  725. wl->scanning = true;
  726. ret = wl1251_cmd_send(wl, CMD_SCAN, params, sizeof(*params));
  727. if (ret < 0)
  728. wl1251_error("SCAN failed");
  729. wl1251_spi_mem_read(wl, wl->cmd_box_addr, params, sizeof(*params));
  730. if (params->header.status != CMD_STATUS_SUCCESS) {
  731. wl1251_error("TEST command answer error: %d",
  732. params->header.status);
  733. wl->scanning = false;
  734. ret = -EIO;
  735. goto out;
  736. }
  737. out:
  738. kfree(params);
  739. return ret;
  740. }
  741. static int wl1251_op_hw_scan(struct ieee80211_hw *hw,
  742. struct cfg80211_scan_request *req)
  743. {
  744. struct wl1251 *wl = hw->priv;
  745. int ret;
  746. u8 *ssid = NULL;
  747. size_t ssid_len = 0;
  748. wl1251_debug(DEBUG_MAC80211, "mac80211 hw scan");
  749. if (req->n_ssids) {
  750. ssid = req->ssids[0].ssid;
  751. ssid_len = req->ssids[0].ssid_len;
  752. }
  753. mutex_lock(&wl->mutex);
  754. ret = wl1251_ps_elp_wakeup(wl);
  755. if (ret < 0)
  756. goto out;
  757. ret = wl1251_hw_scan(hw->priv, ssid, ssid_len, 1, 0, 13, 3);
  758. wl1251_ps_elp_sleep(wl);
  759. out:
  760. mutex_unlock(&wl->mutex);
  761. return ret;
  762. }
  763. static int wl1251_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  764. {
  765. struct wl1251 *wl = hw->priv;
  766. int ret;
  767. mutex_lock(&wl->mutex);
  768. ret = wl1251_ps_elp_wakeup(wl);
  769. if (ret < 0)
  770. goto out;
  771. ret = wl1251_acx_rts_threshold(wl, (u16) value);
  772. if (ret < 0)
  773. wl1251_warning("wl1251_op_set_rts_threshold failed: %d", ret);
  774. wl1251_ps_elp_sleep(wl);
  775. out:
  776. mutex_unlock(&wl->mutex);
  777. return ret;
  778. }
  779. static void wl1251_op_bss_info_changed(struct ieee80211_hw *hw,
  780. struct ieee80211_vif *vif,
  781. struct ieee80211_bss_conf *bss_conf,
  782. u32 changed)
  783. {
  784. enum wl1251_cmd_ps_mode mode;
  785. struct wl1251 *wl = hw->priv;
  786. struct sk_buff *beacon;
  787. int ret;
  788. wl1251_debug(DEBUG_MAC80211, "mac80211 bss info changed");
  789. mutex_lock(&wl->mutex);
  790. ret = wl1251_ps_elp_wakeup(wl);
  791. if (ret < 0)
  792. goto out;
  793. if (changed & BSS_CHANGED_ASSOC) {
  794. if (bss_conf->assoc) {
  795. wl->aid = bss_conf->aid;
  796. ret = wl1251_build_ps_poll(wl, wl->aid);
  797. if (ret < 0)
  798. goto out_sleep;
  799. ret = wl1251_acx_aid(wl, wl->aid);
  800. if (ret < 0)
  801. goto out_sleep;
  802. /* If we want to go in PSM but we're not there yet */
  803. if (wl->psm_requested && !wl->psm) {
  804. mode = STATION_POWER_SAVE_MODE;
  805. ret = wl1251_ps_set_mode(wl, mode);
  806. if (ret < 0)
  807. goto out_sleep;
  808. }
  809. }
  810. }
  811. if (changed & BSS_CHANGED_ERP_SLOT) {
  812. if (bss_conf->use_short_slot)
  813. ret = wl1251_acx_slot(wl, SLOT_TIME_SHORT);
  814. else
  815. ret = wl1251_acx_slot(wl, SLOT_TIME_LONG);
  816. if (ret < 0) {
  817. wl1251_warning("Set slot time failed %d", ret);
  818. goto out_sleep;
  819. }
  820. }
  821. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  822. if (bss_conf->use_short_preamble)
  823. wl1251_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
  824. else
  825. wl1251_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
  826. }
  827. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  828. if (bss_conf->use_cts_prot)
  829. ret = wl1251_acx_cts_protect(wl, CTSPROTECT_ENABLE);
  830. else
  831. ret = wl1251_acx_cts_protect(wl, CTSPROTECT_DISABLE);
  832. if (ret < 0) {
  833. wl1251_warning("Set ctsprotect failed %d", ret);
  834. goto out;
  835. }
  836. }
  837. if (changed & BSS_CHANGED_BSSID) {
  838. memcpy(wl->bssid, bss_conf->bssid, ETH_ALEN);
  839. ret = wl1251_build_null_data(wl);
  840. if (ret < 0)
  841. goto out;
  842. if (wl->bss_type != BSS_TYPE_IBSS) {
  843. ret = wl1251_cmd_join(wl, wl->bss_type, 5, 100, 1);
  844. if (ret < 0)
  845. goto out_sleep;
  846. wl1251_warning("Set ctsprotect failed %d", ret);
  847. goto out_sleep;
  848. }
  849. }
  850. if (changed & BSS_CHANGED_BEACON) {
  851. beacon = ieee80211_beacon_get(hw, vif);
  852. ret = wl1251_cmd_template_set(wl, CMD_BEACON, beacon->data,
  853. beacon->len);
  854. if (ret < 0) {
  855. dev_kfree_skb(beacon);
  856. goto out;
  857. }
  858. ret = wl1251_cmd_template_set(wl, CMD_PROBE_RESP, beacon->data,
  859. beacon->len);
  860. dev_kfree_skb(beacon);
  861. if (ret < 0)
  862. goto out;
  863. ret = wl->chip.op_cmd_join(wl, wl->bss_type, 1, 100, 0);
  864. if (ret < 0)
  865. goto out;
  866. }
  867. out_sleep:
  868. wl1251_ps_elp_sleep(wl);
  869. out:
  870. mutex_unlock(&wl->mutex);
  871. }
  872. /* can't be const, mac80211 writes to this */
  873. static struct ieee80211_rate wl1251_rates[] = {
  874. { .bitrate = 10,
  875. .hw_value = 0x1,
  876. .hw_value_short = 0x1, },
  877. { .bitrate = 20,
  878. .hw_value = 0x2,
  879. .hw_value_short = 0x2,
  880. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  881. { .bitrate = 55,
  882. .hw_value = 0x4,
  883. .hw_value_short = 0x4,
  884. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  885. { .bitrate = 110,
  886. .hw_value = 0x20,
  887. .hw_value_short = 0x20,
  888. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  889. { .bitrate = 60,
  890. .hw_value = 0x8,
  891. .hw_value_short = 0x8, },
  892. { .bitrate = 90,
  893. .hw_value = 0x10,
  894. .hw_value_short = 0x10, },
  895. { .bitrate = 120,
  896. .hw_value = 0x40,
  897. .hw_value_short = 0x40, },
  898. { .bitrate = 180,
  899. .hw_value = 0x80,
  900. .hw_value_short = 0x80, },
  901. { .bitrate = 240,
  902. .hw_value = 0x200,
  903. .hw_value_short = 0x200, },
  904. { .bitrate = 360,
  905. .hw_value = 0x400,
  906. .hw_value_short = 0x400, },
  907. { .bitrate = 480,
  908. .hw_value = 0x800,
  909. .hw_value_short = 0x800, },
  910. { .bitrate = 540,
  911. .hw_value = 0x1000,
  912. .hw_value_short = 0x1000, },
  913. };
  914. /* can't be const, mac80211 writes to this */
  915. static struct ieee80211_channel wl1251_channels[] = {
  916. { .hw_value = 1, .center_freq = 2412},
  917. { .hw_value = 2, .center_freq = 2417},
  918. { .hw_value = 3, .center_freq = 2422},
  919. { .hw_value = 4, .center_freq = 2427},
  920. { .hw_value = 5, .center_freq = 2432},
  921. { .hw_value = 6, .center_freq = 2437},
  922. { .hw_value = 7, .center_freq = 2442},
  923. { .hw_value = 8, .center_freq = 2447},
  924. { .hw_value = 9, .center_freq = 2452},
  925. { .hw_value = 10, .center_freq = 2457},
  926. { .hw_value = 11, .center_freq = 2462},
  927. { .hw_value = 12, .center_freq = 2467},
  928. { .hw_value = 13, .center_freq = 2472},
  929. };
  930. /* can't be const, mac80211 writes to this */
  931. static struct ieee80211_supported_band wl1251_band_2ghz = {
  932. .channels = wl1251_channels,
  933. .n_channels = ARRAY_SIZE(wl1251_channels),
  934. .bitrates = wl1251_rates,
  935. .n_bitrates = ARRAY_SIZE(wl1251_rates),
  936. };
  937. static const struct ieee80211_ops wl1251_ops = {
  938. .start = wl1251_op_start,
  939. .stop = wl1251_op_stop,
  940. .add_interface = wl1251_op_add_interface,
  941. .remove_interface = wl1251_op_remove_interface,
  942. .config = wl1251_op_config,
  943. .configure_filter = wl1251_op_configure_filter,
  944. .tx = wl1251_op_tx,
  945. .set_key = wl1251_op_set_key,
  946. .hw_scan = wl1251_op_hw_scan,
  947. .bss_info_changed = wl1251_op_bss_info_changed,
  948. .set_rts_threshold = wl1251_op_set_rts_threshold,
  949. };
  950. static int wl1251_register_hw(struct wl1251 *wl)
  951. {
  952. int ret;
  953. if (wl->mac80211_registered)
  954. return 0;
  955. SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
  956. ret = ieee80211_register_hw(wl->hw);
  957. if (ret < 0) {
  958. wl1251_error("unable to register mac80211 hw: %d", ret);
  959. return ret;
  960. }
  961. wl->mac80211_registered = true;
  962. wl1251_notice("loaded");
  963. return 0;
  964. }
  965. static int wl1251_init_ieee80211(struct wl1251 *wl)
  966. {
  967. /* The tx descriptor buffer and the TKIP space */
  968. wl->hw->extra_tx_headroom = sizeof(struct tx_double_buffer_desc)
  969. + WL1251_TKIP_IV_SPACE;
  970. /* unit us */
  971. /* FIXME: find a proper value */
  972. wl->hw->channel_change_time = 10000;
  973. wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
  974. IEEE80211_HW_NOISE_DBM;
  975. wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  976. wl->hw->wiphy->max_scan_ssids = 1;
  977. wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1251_band_2ghz;
  978. SET_IEEE80211_DEV(wl->hw, &wl->spi->dev);
  979. return 0;
  980. }
  981. #define WL1251_DEFAULT_CHANNEL 1
  982. static int __devinit wl1251_probe(struct spi_device *spi)
  983. {
  984. struct wl12xx_platform_data *pdata;
  985. struct ieee80211_hw *hw;
  986. struct wl1251 *wl;
  987. int ret, i;
  988. static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
  989. pdata = spi->dev.platform_data;
  990. if (!pdata) {
  991. wl1251_error("no platform data");
  992. return -ENODEV;
  993. }
  994. hw = ieee80211_alloc_hw(sizeof(*wl), &wl1251_ops);
  995. if (!hw) {
  996. wl1251_error("could not alloc ieee80211_hw");
  997. return -ENOMEM;
  998. }
  999. wl = hw->priv;
  1000. memset(wl, 0, sizeof(*wl));
  1001. wl->hw = hw;
  1002. dev_set_drvdata(&spi->dev, wl);
  1003. wl->spi = spi;
  1004. wl->data_in_count = 0;
  1005. skb_queue_head_init(&wl->tx_queue);
  1006. INIT_WORK(&wl->filter_work, wl1251_filter_work);
  1007. wl->channel = WL1251_DEFAULT_CHANNEL;
  1008. wl->scanning = false;
  1009. wl->default_key = 0;
  1010. wl->listen_int = 1;
  1011. wl->rx_counter = 0;
  1012. wl->rx_handled = 0;
  1013. wl->rx_current_buffer = 0;
  1014. wl->rx_last_id = 0;
  1015. wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
  1016. wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
  1017. wl->elp = false;
  1018. wl->psm = 0;
  1019. wl->psm_requested = false;
  1020. wl->tx_queue_stopped = false;
  1021. wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
  1022. /* We use the default power on sleep time until we know which chip
  1023. * we're using */
  1024. wl->chip.power_on_sleep = WL1251_DEFAULT_POWER_ON_SLEEP;
  1025. for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
  1026. wl->tx_frames[i] = NULL;
  1027. wl->next_tx_complete = 0;
  1028. /*
  1029. * In case our MAC address is not correctly set,
  1030. * we use a random but Nokia MAC.
  1031. */
  1032. memcpy(wl->mac_addr, nokia_oui, 3);
  1033. get_random_bytes(wl->mac_addr + 3, 3);
  1034. wl->state = WL1251_STATE_OFF;
  1035. mutex_init(&wl->mutex);
  1036. wl->tx_mgmt_frm_rate = DEFAULT_HW_GEN_TX_RATE;
  1037. wl->tx_mgmt_frm_mod = DEFAULT_HW_GEN_MODULATION_TYPE;
  1038. wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
  1039. if (!wl->rx_descriptor) {
  1040. wl1251_error("could not allocate memory for rx descriptor");
  1041. ret = -ENOMEM;
  1042. goto out_free;
  1043. }
  1044. /* This is the only SPI value that we need to set here, the rest
  1045. * comes from the board-peripherals file */
  1046. spi->bits_per_word = 32;
  1047. ret = spi_setup(spi);
  1048. if (ret < 0) {
  1049. wl1251_error("spi_setup failed");
  1050. goto out_free;
  1051. }
  1052. wl->set_power = pdata->set_power;
  1053. if (!wl->set_power) {
  1054. wl1251_error("set power function missing in platform data");
  1055. ret = -ENODEV;
  1056. goto out_free;
  1057. }
  1058. wl->irq = spi->irq;
  1059. if (wl->irq < 0) {
  1060. wl1251_error("irq missing in platform data");
  1061. ret = -ENODEV;
  1062. goto out_free;
  1063. }
  1064. ret = request_irq(wl->irq, wl1251_irq, 0, DRIVER_NAME, wl);
  1065. if (ret < 0) {
  1066. wl1251_error("request_irq() failed: %d", ret);
  1067. goto out_free;
  1068. }
  1069. set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
  1070. disable_irq(wl->irq);
  1071. ret = wl1251_init_ieee80211(wl);
  1072. if (ret)
  1073. goto out_irq;
  1074. ret = wl1251_register_hw(wl);
  1075. if (ret)
  1076. goto out_irq;
  1077. wl1251_debugfs_init(wl);
  1078. wl1251_notice("initialized");
  1079. return 0;
  1080. out_irq:
  1081. free_irq(wl->irq, wl);
  1082. out_free:
  1083. kfree(wl->rx_descriptor);
  1084. wl->rx_descriptor = NULL;
  1085. ieee80211_free_hw(hw);
  1086. return ret;
  1087. }
  1088. static int __devexit wl1251_remove(struct spi_device *spi)
  1089. {
  1090. struct wl1251 *wl = dev_get_drvdata(&spi->dev);
  1091. ieee80211_unregister_hw(wl->hw);
  1092. wl1251_debugfs_exit(wl);
  1093. free_irq(wl->irq, wl);
  1094. kfree(wl->target_mem_map);
  1095. kfree(wl->data_path);
  1096. kfree(wl->fw);
  1097. wl->fw = NULL;
  1098. kfree(wl->nvs);
  1099. wl->nvs = NULL;
  1100. kfree(wl->rx_descriptor);
  1101. wl->rx_descriptor = NULL;
  1102. ieee80211_free_hw(wl->hw);
  1103. return 0;
  1104. }
  1105. static struct spi_driver wl1251_spi_driver = {
  1106. .driver = {
  1107. /* FIXME: use wl12xx name to not break the user space */
  1108. .name = "wl12xx",
  1109. .bus = &spi_bus_type,
  1110. .owner = THIS_MODULE,
  1111. },
  1112. .probe = wl1251_probe,
  1113. .remove = __devexit_p(wl1251_remove),
  1114. };
  1115. static int __init wl1251_init(void)
  1116. {
  1117. int ret;
  1118. ret = spi_register_driver(&wl1251_spi_driver);
  1119. if (ret < 0) {
  1120. wl1251_error("failed to register spi driver: %d", ret);
  1121. goto out;
  1122. }
  1123. out:
  1124. return ret;
  1125. }
  1126. static void __exit wl1251_exit(void)
  1127. {
  1128. spi_unregister_driver(&wl1251_spi_driver);
  1129. wl1251_notice("unloaded");
  1130. }
  1131. module_init(wl1251_init);
  1132. module_exit(wl1251_exit);
  1133. MODULE_LICENSE("GPL");
  1134. MODULE_AUTHOR("Kalle Valo <Kalle.Valo@nokia.com>, "
  1135. "Luciano Coelho <luciano.coelho@nokia.com>");