wl1271_main.c 41 KB

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