wl1271_main.c 42 KB

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